Debian Codegraph @

goodbye.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 // The Goodbye packet indicates that one or more sources are no longer active.
13 type Goodbye struct {
14 // The SSRC/CSRC identifiers that are no longer active
15 Sources []uint32
16 // Optional text indicating the reason for leaving, e.g., "camera malfunction" or "RTP loop detected"
17 Reason string
18 }
19
20 // Marshal encodes the Goodbye packet in binary.
21 func (g Goodbye) Marshal() ([]byte, error) {
22 /*
23 * 0 1 2 3
24 * 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
25 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
26 * |V=2|P| SC | PT=BYE=203 | length |
27 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
28 * | SSRC/CSRC |
29 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
30 * : ... :
31 * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
32 * (opt) | length | reason for leaving ...
33 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 */
35
36 rawPacket := make([]byte, g.MarshalSize())
37 packetBody := rawPacket[headerLength:]
38
39 if len(g.Sources) > countMax {
40 return nil, errTooManySources
41 }
42
43 for i, s := range g.Sources {
44 binary.BigEndian.PutUint32(packetBody[i*ssrcLength:], s)
45 }
46
47 if g.Reason != "" {
48 reason := []byte(g.Reason)
49
50 if len(reason) > sdesMaxOctetCount {
51 return nil, errReasonTooLong
52 }
53
54 reasonOffset := len(g.Sources) * ssrcLength
55 packetBody[reasonOffset] = uint8(len(reason)) //nolint:gosec // G115
56 copy(packetBody[reasonOffset+1:], reason)
57 }
58
59 hData, err := g.Header().Marshal()
60 if err != nil {
61 return nil, err
62 }
63 copy(rawPacket, hData)
64
65 return rawPacket, nil
66 }
67
68 // Unmarshal decodes the Goodbye packet from binary.
69 func (g *Goodbye) Unmarshal(rawPacket []byte) error {
70 /*
71 * 0 1 2 3
72 * 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
73 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
74 * |V=2|P| SC | PT=BYE=203 | length |
75 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
76 * | SSRC/CSRC |
77 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
78 * : ... :
79 * +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
80 * (opt) | length | reason for leaving ...
81 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
82 */
83
84 var header Header
85 if err := header.Unmarshal(rawPacket); err != nil {
86 return err
87 }
88
89 if header.Type != TypeGoodbye {
90 return errWrongType
91 }
92
93 if getPadding(len(rawPacket)) != 0 {
94 return errPacketTooShort
95 }
96
97 g.Sources = make([]uint32, header.Count)
98
99 reasonOffset := int(headerLength + header.Count*ssrcLength)
100 if reasonOffset > len(rawPacket) {
101 return errPacketTooShort
102 }
103
104 for i := 0; i < int(header.Count); i++ {
105 offset := headerLength + i*ssrcLength
106
107 g.Sources[i] = binary.BigEndian.Uint32(rawPacket[offset:])
108 }
109
110 if reasonOffset < len(rawPacket) {
111 reasonLen := int(rawPacket[reasonOffset])
112 reasonEnd := reasonOffset + 1 + reasonLen
113
114 if reasonEnd > len(rawPacket) {
115 return errPacketTooShort
116 }
117
118 g.Reason = string(rawPacket[reasonOffset+1 : reasonEnd])
119 }
120
121 return nil
122 }
123
124 // Header returns the Header associated with this packet.
125 func (g *Goodbye) Header() Header {
126 return Header{
127 Padding: false,
128 Count: uint8(len(g.Sources)), //nolint:gosec //G115
129 Type: TypeGoodbye,
130 Length: uint16((g.MarshalSize() / 4) - 1), //nolint:gosec //G115
131 }
132 }
133
134 // MarshalSize returns the size of the packet once marshaled.
135 func (g *Goodbye) MarshalSize() int {
136 srcsLength := len(g.Sources) * ssrcLength
137 // reason is optional
138 reasonLength := len(g.Reason)
139 if reasonLength > 0 {
140 reasonLength++
141 }
142
143 l := headerLength + srcsLength + reasonLength
144
145 // align to 32-bit boundary
146 return l + getPadding(l)
147 }
148
149 // DestinationSSRC returns an array of SSRC values that this packet refers to.
150 func (g *Goodbye) DestinationSSRC() []uint32 {
151 out := make([]uint32, len(g.Sources))
152 copy(out, g.Sources)
153
154 return out
155 }
156
157 func (g Goodbye) String() string {
158 var out strings.Builder
159 out.WriteString("Goodbye\n")
160 for i, s := range g.Sources {
161 fmt.Fprintf(&out, "\tSource %d: %x\n", i, s)
162 }
163 fmt.Fprintf(&out, "\tReason: %s\n", g.Reason)
164
165 return out.String()
166 }