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 rtcpscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/
5
6 import (
7 "encoding/binaryscip-go gomod github.com/golang/go/src go1.24.0 `encoding/binary`/"
8 )
9
10 // ApplicationDefined represents an RTCP application-defined packet.
11 type ApplicationDefinedscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ApplicationDefined# struct {
12 SubTypescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ApplicationDefined#SubType. uint8
13 SSRCscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ApplicationDefined#SSRC. uint32
14 Namescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ApplicationDefined#Name. string
15 Datascip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ApplicationDefined#Data. []byte
16 }
17
18 // DestinationSSRC returns the SSRC value for this packet.
19 func (a ApplicationDefinedscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ApplicationDefined#) DestinationSSRCscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ApplicationDefined#DestinationSSRC().() []uint32 {
20 return []uint32{a.SSRCscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ApplicationDefined#SSRC.}
21 }
22
23 // Marshal serializes the application-defined struct into a byte slice with padding.
24 func (a ApplicationDefinedscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ApplicationDefined#) Marshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ApplicationDefined#Marshal().() ([]byte, error) {
25 dataLength := len(a.Datascip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ApplicationDefined#Data.)
26 if dataLength > 0xFFFF-12 {
27 return nil, errAppDefinedDataTooLargescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errAppDefinedDataTooLarge.
28 }
29 if len(a.Namescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ApplicationDefined#Name.) != 4 {
30 return nil, errAppDefinedInvalidNamescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/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.MarshalSizescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ApplicationDefined#MarshalSize().()
39 header := Headerscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#{
40 Typescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Type.: TypeApplicationDefinedscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/TypeApplicationDefined.,
41 Lengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Length.: uint16((packetSize / 4) - 1), //nolint:gosec // G115
42 Paddingscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Padding.: paddingSize != 0,
43 Countscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Count.: a.SubTypescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ApplicationDefined#SubType.,
44 }
45
46 headerBytes, err := header.Marshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Marshal().()
47 if err != nil {
48 return nil, err
49 }
50
51 rawPacket := make([]byte, packetSize)
52 copy(rawPacket, headerBytes)
53 binaryscip-go gomod github.com/golang/go/src go1.24.0 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.24.0 `encoding/binary`/BigEndian..PutUint32scip-go gomod github.com/golang/go/src go1.24.0 `encoding/binary`/bigEndian#PutUint32().(rawPacket[4:8], a.SSRCscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ApplicationDefined#SSRC.)
54 copy(rawPacket[8:12], a.Namescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ApplicationDefined#Name.)
55 copy(rawPacket[12:], a.Datascip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ApplicationDefined#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 *ApplicationDefinedscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ApplicationDefined#) Unmarshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/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 := Headerscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#{}
83 err := header.Unmarshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Unmarshal().(rawPacket)
84 if err != nil {
85 return err
86 }
87 if len(rawPacket) < 12 {
88 return errPacketTooShortscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errPacketTooShort.
89 }
90
91 if (int(header.Lengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Length.)+1)*4 != len(rawPacket) {
92 return errAppDefinedInvalidLengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errAppDefinedInvalidLength.
93 }
94
95 a.SubTypescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ApplicationDefined#SubType. = header.Countscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Count.
96 a.SSRCscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ApplicationDefined#SSRC. = binaryscip-go gomod github.com/golang/go/src go1.24.0 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.24.0 `encoding/binary`/BigEndian..Uint32scip-go gomod github.com/golang/go/src go1.24.0 `encoding/binary`/bigEndian#Uint32().(rawPacket[4:8])
97 a.Namescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ApplicationDefined#Name. = string(rawPacket[8:12])
98
99 // Check for padding.
100 paddingSize := 0
101 if header.Paddingscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Padding. {
102 paddingSize = int(rawPacket[len(rawPacket)-1])
103 if paddingSize > len(rawPacket)-12 {
104 return errWrongPaddingscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errWrongPadding.
105 }
106 }
107
108 a.Datascip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ApplicationDefined#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 *ApplicationDefinedscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ApplicationDefined#) MarshalSizescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ApplicationDefined#MarshalSize().() int {
115 dataLength := len(a.Datascip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ApplicationDefined#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 }