| 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 | "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/" |
| 8 | "stringsscip-go gomod github.com/golang/go/src go1.24.0 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 CompoundPacketscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/CompoundPacket# []Packetscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Packet# |
| 24 | |
| 25 | // Validate returns an error if this is not an RFC-compliant CompoundPacket. |
| 26 | // |
| 27 | //nolint:cyclop |
| 28 | func (c CompoundPacketscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/CompoundPacket#) Validatescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/CompoundPacket#Validate().() error { |
| 29 | if len(c) == 0 { |
| 30 | return errEmptyCompoundscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/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 *SenderReportscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/SenderReport#, *ReceiverReportscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#: |
| 37 | // ok |
| 38 | default: |
| 39 | return errBadFirstPacketscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/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 *ReceiverReportscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#: |
| 47 | continue |
| 48 | |
| 49 | // A SourceDescription containing a CNAME must be included in every |
| 50 | // CompoundPacket. |
| 51 | case *SourceDescriptionscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/SourceDescription#: |
| 52 | var hasCNAME bool |
| 53 | for _, c := range p.Chunksscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/SourceDescription#Chunks. { |
| 54 | for _, it := range c.Itemsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/SourceDescriptionChunk#Items. { |
| 55 | if it.Typescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/SourceDescriptionItem#Type. == SDESCNAMEscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/SDESCNAME. { |
| 56 | hasCNAME = true |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | if !hasCNAME { |
| 62 | return errMissingCNAMEscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errMissingCNAME. |
| 63 | } |
| 64 | |
| 65 | return nil |
| 66 | |
| 67 | // Other packets are not permitted before the CNAME |
| 68 | default: |
| 69 | return errPacketBeforeCNAMEscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errPacketBeforeCNAME. |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // CNAME never reached |
| 74 | return errMissingCNAMEscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errMissingCNAME. |
| 75 | } |
| 76 | |
| 77 | // CNAME returns the CNAME that *must* be present in every CompoundPacket. |
| 78 | func (c CompoundPacketscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/CompoundPacket#) CNAMEscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/CompoundPacket#CNAME().() (string, error) { |
| 79 | var err error |
| 80 | |
| 81 | if len(c) < 1 { |
| 82 | return "", errEmptyCompoundscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errEmptyCompound. |
| 83 | } |
| 84 | |
| 85 | for _, pkt := range c[1:] { |
| 86 | sdes, ok := pkt.(*SourceDescriptionscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/SourceDescription#) |
| 87 | if ok { |
| 88 | for _, c := range sdes.Chunksscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/SourceDescription#Chunks. { |
| 89 | for _, it := range c.Itemsscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/SourceDescriptionChunk#Items. { |
| 90 | if it.Typescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/SourceDescriptionItem#Type. == SDESCNAMEscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/SDESCNAME. { |
| 91 | return it.Textscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/SourceDescriptionItem#Text., err |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | } else { |
| 96 | _, ok := pkt.(*ReceiverReportscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/ReceiverReport#) |
| 97 | if !ok { |
| 98 | err = errPacketBeforeCNAMEscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errPacketBeforeCNAME. |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return "", errMissingCNAMEscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/errMissingCNAME. |
| 104 | } |
| 105 | |
| 106 | // Marshal encodes the CompoundPacket as binary. |
| 107 | func (c CompoundPacketscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/CompoundPacket#) Marshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/CompoundPacket#Marshal().() ([]byte, error) { |
| 108 | if err := c.Validatescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/CompoundPacket#Validate().(); err != nil { |
| 109 | return nil, err |
| 110 | } |
| 111 | |
| 112 | p := []Packetscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Packet#(c) |
| 113 | |
| 114 | return Marshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Marshal().(p) |
| 115 | } |
| 116 | |
| 117 | // MarshalSize returns the size of the packet once marshaled. |
| 118 | func (c CompoundPacketscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/CompoundPacket#) MarshalSizescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/CompoundPacket#MarshalSize().() int { |
| 119 | l := 0 |
| 120 | for _, p := range c { |
| 121 | l += p.MarshalSizescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Packet#MarshalSize.() |
| 122 | } |
| 123 | |
| 124 | return l |
| 125 | } |
| 126 | |
| 127 | // Unmarshal decodes a CompoundPacket from binary. |
| 128 | func (c *CompoundPacketscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/CompoundPacket#) Unmarshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/CompoundPacket#Unmarshal().(rawData []byte) error { |
| 129 | out := make(CompoundPacketscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/CompoundPacket#, 0) |
| 130 | for len(rawData) != 0 { |
| 131 | p, processed, err := unmarshalscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/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.Validatescip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/CompoundPacket#Validate().() |
| 142 | } |
| 143 | |
| 144 | // DestinationSSRC returns the synchronization sources associated with this |
| 145 | // CompoundPacket's reception report. |
| 146 | func (c CompoundPacketscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/CompoundPacket#) DestinationSSRCscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/CompoundPacket#DestinationSSRC().() []uint32 { |
| 147 | if len(c) == 0 { |
| 148 | return nil |
| 149 | } |
| 150 | |
| 151 | return c[0].DestinationSSRCscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/Packet#DestinationSSRC.() |
| 152 | } |
| 153 | |
| 154 | func (c CompoundPacketscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/CompoundPacket#) Stringscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/CompoundPacket#String().() string { |
| 155 | out := "CompoundPacket\n" |
| 156 | for _, p := range c { |
| 157 | stringer, canString := p.(fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Stringerscip-go gomod github.com/golang/go/src go1.24.0 fmt/Stringer#) |
| 158 | if canString { |
| 159 | out += stringer.Stringscip-go gomod github.com/golang/go/src go1.24.0 fmt/Stringer#String().() |
| 160 | } else { |
| 161 | out += stringifyscip-go gomod github.com/pion/rtcp . `github.com/pion/rtcp`/stringify().(p) |
| 162 | } |
| 163 | } |
| 164 | out = stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.TrimSuffixscip-go gomod github.com/golang/go/src go1.24.0 strings/TrimSuffix().(stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.ReplaceAllscip-go gomod github.com/golang/go/src go1.24.0 strings/ReplaceAll().(out, "\n", "\n\t"), "\t") |
| 165 | |
| 166 | return out |
| 167 | } |