| 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 | "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/" |
| 9 | "stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/" |
| 10 | ) |
| 11 | |
| 12 | // The Goodbye packet indicates that one or more sources are no longer active. |
| 13 | type Goodbyescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye# struct { |
| 14 | // The SSRC/CSRC identifiers that are no longer active |
| 15 | Sourcesscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#Sources. []uint32 |
| 16 | // Optional text indicating the reason for leaving, e.g., "camera malfunction" or "RTP loop detected" |
| 17 | Reasonscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#Reason. string |
| 18 | } |
| 19 | |
| 20 | // Marshal encodes the Goodbye packet in binary. |
| 21 | func (g Goodbyescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#) Marshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/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.MarshalSizescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#MarshalSize().()) |
| 37 | packetBody := rawPacket[headerLengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/headerLength.:] |
| 38 | |
| 39 | if len(g.Sourcesscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#Sources.) > countMaxscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/countMax. { |
| 40 | return nil, errTooManySourcesscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errTooManySources. |
| 41 | } |
| 42 | |
| 43 | for i, s := range g.Sourcesscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#Sources. { |
| 44 | 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().(packetBody[i*ssrcLengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ssrcLength.:], s) |
| 45 | } |
| 46 | |
| 47 | if g.Reasonscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#Reason. != "" { |
| 48 | reason := []byte(g.Reasonscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#Reason.) |
| 49 | |
| 50 | if len(reason) > sdesMaxOctetCountscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/sdesMaxOctetCount. { |
| 51 | return nil, errReasonTooLongscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errReasonTooLong. |
| 52 | } |
| 53 | |
| 54 | reasonOffset := len(g.Sourcesscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#Sources.) * ssrcLengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ssrcLength. |
| 55 | packetBody[reasonOffset] = uint8(len(reason)) //nolint:gosec // G115 |
| 56 | copy(packetBody[reasonOffset+1:], reason) |
| 57 | } |
| 58 | |
| 59 | hData, err := g.Headerscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#Header().().Marshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/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 *Goodbyescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#) Unmarshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/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 Headerscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header# |
| 85 | if err := header.Unmarshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Unmarshal().(rawPacket); err != nil { |
| 86 | return err |
| 87 | } |
| 88 | |
| 89 | if header.Typescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Type. != TypeGoodbyescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/TypeGoodbye. { |
| 90 | return errWrongTypescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errWrongType. |
| 91 | } |
| 92 | |
| 93 | if getPaddingscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/getPadding().(len(rawPacket)) != 0 { |
| 94 | return errPacketTooShortscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errPacketTooShort. |
| 95 | } |
| 96 | |
| 97 | g.Sourcesscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#Sources. = make([]uint32, header.Countscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Count.) |
| 98 | |
| 99 | reasonOffset := int(headerLengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/headerLength. + header.Countscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Count.*ssrcLengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ssrcLength.) |
| 100 | if reasonOffset > len(rawPacket) { |
| 101 | return errPacketTooShortscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errPacketTooShort. |
| 102 | } |
| 103 | |
| 104 | for i := 0; i < int(header.Countscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Count.); i++ { |
| 105 | offset := headerLengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/headerLength. + i*ssrcLengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ssrcLength. |
| 106 | |
| 107 | g.Sourcesscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#Sources.[i] = 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[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 errPacketTooShortscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errPacketTooShort. |
| 116 | } |
| 117 | |
| 118 | g.Reasonscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#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 *Goodbyescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#) Headerscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#Header().() Headerscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header# { |
| 126 | return Headerscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#{ |
| 127 | Paddingscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Padding.: false, |
| 128 | Countscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Count.: uint8(len(g.Sourcesscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#Sources.)), //nolint:gosec //G115 |
| 129 | Typescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Type.: TypeGoodbyescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/TypeGoodbye., |
| 130 | Lengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Header#Length.: uint16((g.MarshalSizescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#MarshalSize().() / 4) - 1), //nolint:gosec //G115 |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // MarshalSize returns the size of the packet once marshaled. |
| 135 | func (g *Goodbyescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#) MarshalSizescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#MarshalSize().() int { |
| 136 | srcsLength := len(g.Sourcesscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#Sources.) * ssrcLengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ssrcLength. |
| 137 | // reason is optional |
| 138 | reasonLength := len(g.Reasonscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#Reason.) |
| 139 | if reasonLength > 0 { |
| 140 | reasonLength++ |
| 141 | } |
| 142 | |
| 143 | l := headerLengthscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/headerLength. + srcsLength + reasonLength |
| 144 | |
| 145 | // align to 32-bit boundary |
| 146 | return l + getPaddingscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/getPadding().(l) |
| 147 | } |
| 148 | |
| 149 | // DestinationSSRC returns an array of SSRC values that this packet refers to. |
| 150 | func (g *Goodbyescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#) DestinationSSRCscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#DestinationSSRC().() []uint32 { |
| 151 | out := make([]uint32, len(g.Sourcesscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#Sources.)) |
| 152 | copy(out, g.Sourcesscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#Sources.) |
| 153 | |
| 154 | return out |
| 155 | } |
| 156 | |
| 157 | func (g Goodbyescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#) Stringscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#String().() string { |
| 158 | var out stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.Builderscip-go gomod github.com/golang/go/src go1.24.0 strings/Builder# |
| 159 | out.WriteStringscip-go gomod github.com/golang/go/src go1.24.0 strings/Builder#WriteString().("Goodbye\n") |
| 160 | for i, s := range g.Sourcesscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#Sources. { |
| 161 | fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Fprintf().(&out, "\tSource %d: %x\n", i, s) |
| 162 | } |
| 163 | fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Fprintf().(&out, "\tReason: %s\n", g.Reasonscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Goodbye#Reason.) |
| 164 | |
| 165 | return out.Stringscip-go gomod github.com/golang/go/src go1.24.0 strings/Builder#String().() |
| 166 | } |