Debian Codegraph @

application_defined.go

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
2 // SPDX-License-Identifier: MIT
3
4 package rtcp
5
6 import (
7 "encoding/binary"
8 )
9
10 // ApplicationDefined represents an RTCP application-defined packet.
11 type ApplicationDefined struct {
12 SubType uint8
13 SSRC uint32
14 Name string
15 Data []byte
16 }
17
18 // DestinationSSRC returns the SSRC value for this packet.
19 func (a ApplicationDefined) DestinationSSRC() []uint32 {
20 return []uint32{a.SSRC}
21 }
22
23 // Marshal serializes the application-defined struct into a byte slice with padding.
24 func (a ApplicationDefined) Marshal() ([]byte, error) {
25 dataLength := len(a.Data)
26 if dataLength > 0xFFFF-12 {
27 return nil, errAppDefinedDataTooLarge
28 }
29 if len(a.Name) != 4 {
30 return nil, errAppDefinedInvalidName
31 }
32 // Calculate the padding size to be added to make the packet length a multiple of 4 bytes.
33 paddingSize := 4 - (dataLength % 4)
34 if paddingSize == 4 {
35 paddingSize = 0
36 }
37
38 packetSize := a.MarshalSize()
39 header := Header{
40 Type: TypeApplicationDefined,
41 Length: uint16((packetSize / 4) - 1), //nolint:gosec // G115
42 Padding: paddingSize != 0,
43 Count: a.SubType,
44 }
45
46 headerBytes, err := header.Marshal()
47 if err != nil {
48 return nil, err
49 }
50
51 rawPacket := make([]byte, packetSize)
52 copy(rawPacket, headerBytes)
53 binary.BigEndian.PutUint32(rawPacket[4:8], a.SSRC)
54 copy(rawPacket[8:12], a.Name)
55 copy(rawPacket[12:], a.Data)
56
57 // Add padding if necessary.
58 if paddingSize > 0 {
59 for i := 0; i < paddingSize; i++ {
60 rawPacket[12+dataLength+i] = byte(paddingSize)
61 }
62 }
63
64 return rawPacket, nil
65 }
66
67 // Unmarshal parses the given raw packet into an application-defined struct, handling padding.
68 func (a *ApplicationDefined) Unmarshal(rawPacket []byte) error {
69 /*
70 0 1 2 3
71 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
72 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
73 |V=2|P| subtype | PT=APP=204 | length |
74 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
75 | SSRC/CSRC |
76 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
77 | name (ASCII) |
78 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
79 | application-dependent data ...
80 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
81 */
82 header := Header{}
83 err := header.Unmarshal(rawPacket)
84 if err != nil {
85 return err
86 }
87 if len(rawPacket) < 12 {
88 return errPacketTooShort
89 }
90
91 if (int(header.Length)+1)*4 != len(rawPacket) {
92 return errAppDefinedInvalidLength
93 }
94
95 a.SubType = header.Count
96 a.SSRC = binary.BigEndian.Uint32(rawPacket[4:8])
97 a.Name = string(rawPacket[8:12])
98
99 // Check for padding.
100 paddingSize := 0
101 if header.Padding {
102 paddingSize = int(rawPacket[len(rawPacket)-1])
103 if paddingSize > len(rawPacket)-12 {
104 return errWrongPadding
105 }
106 }
107
108 a.Data = rawPacket[12 : len(rawPacket)-paddingSize]
109
110 return nil
111 }
112
113 // MarshalSize returns the size of the packet once marshaled.
114 func (a *ApplicationDefined) MarshalSize() int {
115 dataLength := len(a.Data)
116 // Calculate the padding size to be added to make the packet length a multiple of 4 bytes.
117 paddingSize := 4 - (dataLength % 4)
118 if paddingSize == 4 {
119 paddingSize = 0
120 }
121
122 return 12 + dataLength + paddingSize
123 }