Debian Codegraph @

source_description.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 "fmt"
9 "strings"
10 )
11
12 // SDESType is the item type used in the RTCP SDES control packet.
13 type SDESType uint8
14
15 // RTP SDES item types registered with IANA.
16 // See: https://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml#rtp-parameters-5
17 // .
18 const (
19 SDESEnd SDESType = iota // end of SDES list RFC 3550, 6.5
20 SDESCNAME // canonical name RFC 3550, 6.5.1
21 SDESName // user name RFC 3550, 6.5.2
22 SDESEmail // user's electronic mail address RFC 3550, 6.5.3
23 SDESPhone // user's phone number RFC 3550, 6.5.4
24 SDESLocation // geographic user location RFC 3550, 6.5.5
25 SDESTool // name of application or tool RFC 3550, 6.5.6
26 SDESNote // notice about the source RFC 3550, 6.5.7
27 SDESPrivate // private extensions RFC 3550, 6.5.8 (not implemented)
28 )
29
30 //nolint:cyclop
31 func (s SDESType) String() string {
32 switch s {
33 case SDESEnd:
34 return "END"
35 case SDESCNAME:
36 return "CNAME"
37 case SDESName:
38 return "NAME"
39 case SDESEmail:
40 return "EMAIL"
41 case SDESPhone:
42 return "PHONE"
43 case SDESLocation:
44 return "LOC"
45 case SDESTool:
46 return "TOOL"
47 case SDESNote:
48 return "NOTE"
49 case SDESPrivate:
50 return "PRIV"
51 default:
52 return string(s)
53 }
54 }
55
56 const (
57 sdesSourceLen = 4
58 sdesTypeLen = 1
59 sdesTypeOffset = 0
60 sdesOctetCountLen = 1
61 sdesOctetCountOffset = 1
62 sdesMaxOctetCount = (1 << 8) - 1
63 sdesTextOffset = 2
64 )
65
66 // A SourceDescription (SDES) packet describes the sources in an RTP stream.
67 type SourceDescription struct {
68 Chunks []SourceDescriptionChunk
69 }
70
71 // NewCNAMESourceDescription creates a new SourceDescription with a single CNAME item.
72 func NewCNAMESourceDescription(ssrc uint32, cname string) *SourceDescription {
73 return &SourceDescription{
74 Chunks: []SourceDescriptionChunk{{
75 Source: ssrc,
76 Items: []SourceDescriptionItem{{
77 Type: SDESCNAME,
78 Text: cname,
79 }},
80 }},
81 }
82 }
83
84 // Marshal encodes the SourceDescription in binary.
85 func (s SourceDescription) Marshal() ([]byte, error) {
86 /*
87 * 0 1 2 3
88 * 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
89 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90 * header |V=2|P| SC | PT=SDES=202 | length |
91 * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
92 * chunk | SSRC/CSRC_1 |
93 * 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94 * | SDES items |
95 * | ... |
96 * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
97 * chunk | SSRC/CSRC_2 |
98 * 2 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99 * | SDES items |
100 * | ... |
101 * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
102 */
103
104 rawPacket := make([]byte, s.MarshalSize())
105 packetBody := rawPacket[headerLength:]
106
107 chunkOffset := 0
108 for _, c := range s.Chunks {
109 data, err := c.Marshal()
110 if err != nil {
111 return nil, err
112 }
113 copy(packetBody[chunkOffset:], data)
114 chunkOffset += len(data)
115 }
116
117 if len(s.Chunks) > countMax {
118 return nil, errTooManyChunks
119 }
120
121 hData, err := s.Header().Marshal()
122 if err != nil {
123 return nil, err
124 }
125 copy(rawPacket, hData)
126
127 return rawPacket, nil
128 }
129
130 // Unmarshal decodes the SourceDescription from binary.
131 func (s *SourceDescription) Unmarshal(rawPacket []byte) error {
132 /*
133 * 0 1 2 3
134 * 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
135 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
136 * header |V=2|P| SC | PT=SDES=202 | length |
137 * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
138 * chunk | SSRC/CSRC_1 |
139 * 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
140 * | SDES items |
141 * | ... |
142 * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
143 * chunk | SSRC/CSRC_2 |
144 * 2 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
145 * | SDES items |
146 * | ... |
147 * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
148 */
149
150 var header Header
151 if err := header.Unmarshal(rawPacket); err != nil {
152 return err
153 }
154
155 if header.Type != TypeSourceDescription {
156 return errWrongType
157 }
158
159 for i := headerLength; i < len(rawPacket); {
160 var chunk SourceDescriptionChunk
161 if err := chunk.Unmarshal(rawPacket[i:]); err != nil {
162 return err
163 }
164 s.Chunks = append(s.Chunks, chunk)
165
166 i += chunk.len()
167 }
168
169 if len(s.Chunks) != int(header.Count) {
170 return errInvalidHeader
171 }
172
173 return nil
174 }
175
176 // MarshalSize returns the size of the packet once marshaled.
177 func (s *SourceDescription) MarshalSize() int {
178 chunksLength := 0
179 for _, c := range s.Chunks {
180 chunksLength += c.len()
181 }
182
183 return headerLength + chunksLength
184 }
185
186 // Header returns the Header associated with this packet.
187 func (s *SourceDescription) Header() Header {
188 return Header{
189 Count: uint8(len(s.Chunks)), //nolint:gosec // G115
190 Type: TypeSourceDescription,
191 Length: uint16((s.MarshalSize() / 4) - 1), //nolint:gosec // G115
192 }
193 }
194
195 // A SourceDescriptionChunk contains items describing a single RTP source.
196 type SourceDescriptionChunk struct {
197 // The source (ssrc) or contributing source (csrc) identifier this packet describes
198 Source uint32
199 Items []SourceDescriptionItem
200 }
201
202 // Marshal encodes the SourceDescriptionChunk in binary.
203 func (s SourceDescriptionChunk) Marshal() ([]byte, error) {
204 /*
205 * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
206 * | SSRC/CSRC_1 |
207 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
208 * | SDES items |
209 * | ... |
210 * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
211 */
212
213 rawPacket := make([]byte, sdesSourceLen)
214 binary.BigEndian.PutUint32(rawPacket, s.Source)
215
216 for _, it := range s.Items {
217 data, err := it.Marshal()
218 if err != nil {
219 return nil, err
220 }
221 rawPacket = append(rawPacket, data...) //nolint:makezero
222 }
223
224 // The list of items in each chunk MUST be terminated by one or more null octets
225 rawPacket = append(rawPacket, uint8(SDESEnd)) //nolint:makezero
226
227 // additional null octets MUST be included if needed to pad until the next 32-bit boundary
228 rawPacket = append(rawPacket, make([]byte, getPadding(len(rawPacket)))...) //nolint:makezero
229
230 return rawPacket, nil
231 }
232
233 // Unmarshal decodes the SourceDescriptionChunk from binary.
234 func (s *SourceDescriptionChunk) Unmarshal(rawPacket []byte) error {
235 /*
236 * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
237 * | SSRC/CSRC_1 |
238 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
239 * | SDES items |
240 * | ... |
241 * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
242 */
243
244 if len(rawPacket) < (sdesSourceLen + sdesTypeLen) {
245 return errPacketTooShort
246 }
247
248 s.Source = binary.BigEndian.Uint32(rawPacket)
249
250 for i := 4; i < len(rawPacket); {
251 if pktType := SDESType(rawPacket[i]); pktType == SDESEnd {
252 return nil
253 }
254
255 var it SourceDescriptionItem
256 if err := it.Unmarshal(rawPacket[i:]); err != nil {
257 return err
258 }
259 s.Items = append(s.Items, it)
260 i += it.Len()
261 }
262
263 return errPacketTooShort
264 }
265
266 func (s SourceDescriptionChunk) len() int {
267 chunkLen := sdesSourceLen
268 for _, it := range s.Items {
269 chunkLen += it.Len()
270 }
271 chunkLen += sdesTypeLen // for terminating null octet
272
273 // align to 32-bit boundary
274 chunkLen += getPadding(chunkLen)
275
276 return chunkLen
277 }
278
279 // A SourceDescriptionItem is a part of a SourceDescription that describes a stream.
280 type SourceDescriptionItem struct {
281 // The type identifier for this item. eg, SDESCNAME for canonical name description.
282 //
283 // Type zero or SDESEnd is interpreted as the end of an item list and cannot be used.
284 Type SDESType
285 // Text is a unicode text blob associated with the item. Its meaning varies based on the item's Type.
286 Text string
287 }
288
289 // Len returns the length of the SourceDescriptionItem when encoded as binary.
290 func (s SourceDescriptionItem) Len() int {
291 /*
292 * 0 1 2 3
293 * 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
294 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
295 * | CNAME=1 | length | user and domain name ...
296 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
297 */
298 return sdesTypeLen + sdesOctetCountLen + len([]byte(s.Text))
299 }
300
301 // Marshal encodes the SourceDescriptionItem in binary.
302 func (s SourceDescriptionItem) Marshal() ([]byte, error) {
303 /*
304 * 0 1 2 3
305 * 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
306 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
307 * | CNAME=1 | length | user and domain name ...
308 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
309 */
310
311 if s.Type == SDESEnd {
312 return nil, errSDESMissingType
313 }
314
315 rawPacket := make([]byte, sdesTypeLen+sdesOctetCountLen)
316
317 rawPacket[sdesTypeOffset] = uint8(s.Type) //nolint:gosec // rawPacket is created with length 2
318
319 txtBytes := []byte(s.Text)
320 octetCount := len(txtBytes)
321 if octetCount > sdesMaxOctetCount {
322 return nil, errSDESTextTooLong
323 }
324 rawPacket[sdesOctetCountOffset] = uint8(octetCount) //nolint:gosec // rawPacket is created with length 2
325
326 rawPacket = append(rawPacket, txtBytes...) //nolint:makezero
327
328 return rawPacket, nil
329 }
330
331 // Unmarshal decodes the SourceDescriptionItem from binary.
332 func (s *SourceDescriptionItem) Unmarshal(rawPacket []byte) error {
333 /*
334 * 0 1 2 3
335 * 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
336 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
337 * | CNAME=1 | length | user and domain name ...
338 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
339 */
340
341 if len(rawPacket) < (sdesTypeLen + sdesOctetCountLen) {
342 return errPacketTooShort
343 }
344
345 s.Type = SDESType(rawPacket[sdesTypeOffset])
346
347 octetCount := int(rawPacket[sdesOctetCountOffset])
348 if sdesTextOffset+octetCount > len(rawPacket) {
349 return errPacketTooShort
350 }
351
352 txtBytes := rawPacket[sdesTextOffset : sdesTextOffset+octetCount]
353 s.Text = string(txtBytes)
354
355 return nil
356 }
357
358 // DestinationSSRC returns an array of SSRC values that this packet refers to.
359 func (s *SourceDescription) DestinationSSRC() []uint32 {
360 out := make([]uint32, len(s.Chunks))
361 for i, v := range s.Chunks {
362 out[i] = v.Source
363 }
364
365 return out
366 }
367
368 func (s *SourceDescription) String() string {
369 var out strings.Builder
370 out.WriteString("Source Description:\n")
371 for _, c := range s.Chunks {
372 fmt.Fprintf(&out, "\t%x: %s\n", c.Source, c.Items)
373 }
374
375 return out.String()
376 }