Debian Codegraph @

compound_packet.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 "fmt"
8 "strings"
9 )
10
11 // A CompoundPacket is a collection of RTCP packets transmitted as a single packet with
12 // the underlying protocol (for example UDP).
13 //
14 // To maximize the resolution of receiption statistics, the first Packet in a CompoundPacket
15 // must always be either a SenderReport or a ReceiverReport. This is true even if no data
16 // has been sent or received, in which case an empty ReceiverReport must be sent, and even
17 // if the only other RTCP packet in the compound packet is a Goodbye.
18 //
19 // Next, a SourceDescription containing a CNAME item must be included in each CompoundPacket
20 // to identify the source and to begin associating media for purposes such as lip-sync.
21 //
22 // Other RTCP packet types may follow in any order. Packet types may appear more than once.
23 type CompoundPacket []Packet
24
25 // Validate returns an error if this is not an RFC-compliant CompoundPacket.
26 //
27 //nolint:cyclop
28 func (c CompoundPacket) Validate() error {
29 if len(c) == 0 {
30 return errEmptyCompound
31 }
32
33 // SenderReport and ReceiverReport are the only types that
34 // are allowed to be the first packet in a compound datagram
35 switch c[0].(type) {
36 case *SenderReport, *ReceiverReport:
37 // ok
38 default:
39 return errBadFirstPacket
40 }
41
42 for _, pkt := range c[1:] {
43 switch p := pkt.(type) {
44 // If the number of RecetpionReports exceeds 31 additional ReceiverReports
45 // can be included here.
46 case *ReceiverReport:
47 continue
48
49 // A SourceDescription containing a CNAME must be included in every
50 // CompoundPacket.
51 case *SourceDescription:
52 var hasCNAME bool
53 for _, c := range p.Chunks {
54 for _, it := range c.Items {
55 if it.Type == SDESCNAME {
56 hasCNAME = true
57 }
58 }
59 }
60
61 if !hasCNAME {
62 return errMissingCNAME
63 }
64
65 return nil
66
67 // Other packets are not permitted before the CNAME
68 default:
69 return errPacketBeforeCNAME
70 }
71 }
72
73 // CNAME never reached
74 return errMissingCNAME
75 }
76
77 // CNAME returns the CNAME that *must* be present in every CompoundPacket.
78 func (c CompoundPacket) CNAME() (string, error) {
79 var err error
80
81 if len(c) < 1 {
82 return "", errEmptyCompound
83 }
84
85 for _, pkt := range c[1:] {
86 sdes, ok := pkt.(*SourceDescription)
87 if ok {
88 for _, c := range sdes.Chunks {
89 for _, it := range c.Items {
90 if it.Type == SDESCNAME {
91 return it.Text, err
92 }
93 }
94 }
95 } else {
96 _, ok := pkt.(*ReceiverReport)
97 if !ok {
98 err = errPacketBeforeCNAME
99 }
100 }
101 }
102
103 return "", errMissingCNAME
104 }
105
106 // Marshal encodes the CompoundPacket as binary.
107 func (c CompoundPacket) Marshal() ([]byte, error) {
108 if err := c.Validate(); err != nil {
109 return nil, err
110 }
111
112 p := []Packet(c)
113
114 return Marshal(p)
115 }
116
117 // MarshalSize returns the size of the packet once marshaled.
118 func (c CompoundPacket) MarshalSize() int {
119 l := 0
120 for _, p := range c {
121 l += p.MarshalSize()
122 }
123
124 return l
125 }
126
127 // Unmarshal decodes a CompoundPacket from binary.
128 func (c *CompoundPacket) Unmarshal(rawData []byte) error {
129 out := make(CompoundPacket, 0)
130 for len(rawData) != 0 {
131 p, processed, err := unmarshal(rawData)
132 if err != nil {
133 return err
134 }
135
136 out = append(out, p)
137 rawData = rawData[processed:]
138 }
139 *c = out
140
141 return c.Validate()
142 }
143
144 // DestinationSSRC returns the synchronization sources associated with this
145 // CompoundPacket's reception report.
146 func (c CompoundPacket) DestinationSSRC() []uint32 {
147 if len(c) == 0 {
148 return nil
149 }
150
151 return c[0].DestinationSSRC()
152 }
153
154 func (c CompoundPacket) String() string {
155 out := "CompoundPacket\n"
156 for _, p := range c {
157 stringer, canString := p.(fmt.Stringer)
158 if canString {
159 out += stringer.String()
160 } else {
161 out += stringify(p)
162 }
163 }
164 out = strings.TrimSuffix(strings.ReplaceAll(out, "\n", "\n\t"), "\t")
165
166 return out
167 }