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