| 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 | // A FIREntry is a (SSRC, seqno) pair, as carried by FullIntraRequest. |
| 13 | type FIREntry struct { |
| 14 | SSRC uint32 |
| 15 | SequenceNumber uint8 |
| 16 | } |
| 17 | |
| 18 | // The FullIntraRequest packet is used to reliably request an Intra frame |
| 19 | // in a video stream. See RFC 5104 Section 3.5.1. This is not for loss |
| 20 | // recovery, which should use PictureLossIndication (PLI) instead. |
| 21 | type FullIntraRequest struct { |
| 22 | SenderSSRC uint32 |
| 23 | MediaSSRC uint32 |
| 24 | |
| 25 | FIR []FIREntry |
| 26 | } |
| 27 | |
| 28 | const ( |
| 29 | firOffset = 8 |
| 30 | ) |
| 31 | |
| 32 | var _ Packet = (*FullIntraRequest)(nil) |
| 33 | |
| 34 | // Marshal encodes the FullIntraRequest. |
| 35 | func (p FullIntraRequest) Marshal() ([]byte, error) { |
| 36 | rawPacket := make([]byte, firOffset+(len(p.FIR)*8)) |
| 37 | binary.BigEndian.PutUint32(rawPacket, p.SenderSSRC) |
| 38 | binary.BigEndian.PutUint32(rawPacket[4:], p.MediaSSRC) |
| 39 | for i, fir := range p.FIR { |
| 40 | binary.BigEndian.PutUint32(rawPacket[firOffset+8*i:], fir.SSRC) |
| 41 | rawPacket[firOffset+8*i+4] = fir.SequenceNumber |
| 42 | } |
| 43 | h := p.Header() |
| 44 | hData, err := h.Marshal() |
| 45 | if err != nil { |
| 46 | return nil, err |
| 47 | } |
| 48 | |
| 49 | return append(hData, rawPacket...), nil |
| 50 | } |
| 51 | |
| 52 | // Unmarshal decodes the TransportLayerNack. |
| 53 | func (p *FullIntraRequest) Unmarshal(rawPacket []byte) error { |
| 54 | if len(rawPacket) < (headerLength + ssrcLength) { |
| 55 | return errPacketTooShort |
| 56 | } |
| 57 | |
| 58 | var header Header |
| 59 | if err := header.Unmarshal(rawPacket); err != nil { |
| 60 | return err |
| 61 | } |
| 62 | |
| 63 | if len(rawPacket) < (headerLength + 4*int(header.Length)) { |
| 64 | return errPacketTooShort |
| 65 | } |
| 66 | |
| 67 | if header.Type != TypePayloadSpecificFeedback || header.Count != FormatFIR { |
| 68 | return errWrongType |
| 69 | } |
| 70 | |
| 71 | // The FCI field MUST contain one or more FIR entries |
| 72 | if 4*int(header.Length)-firOffset <= 0 || (4*int(header.Length))%8 != 0 { |
| 73 | return errBadLength |
| 74 | } |
| 75 | |
| 76 | p.SenderSSRC = binary.BigEndian.Uint32(rawPacket[headerLength:]) |
| 77 | p.MediaSSRC = binary.BigEndian.Uint32(rawPacket[headerLength+ssrcLength:]) |
| 78 | for i := headerLength + firOffset; i < (headerLength + 4*int(header.Length)); i += 8 { |
| 79 | entry := rawPacket[i : i+8] |
| 80 | p.FIR = append(p.FIR, FIREntry{ |
| 81 | binary.BigEndian.Uint32(entry), |
| 82 | entry[4], |
| 83 | }) |
| 84 | } |
| 85 | |
| 86 | return nil |
| 87 | } |
| 88 | |
| 89 | // Header returns the Header associated with this packet. |
| 90 | func (p *FullIntraRequest) Header() Header { |
| 91 | return Header{ |
| 92 | Count: FormatFIR, |
| 93 | Type: TypePayloadSpecificFeedback, |
| 94 | Length: uint16((p.MarshalSize() / 4) - 1), //nolint:gosec // G115 |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // MarshalSize returns the size of the packet once marshaled. |
| 99 | func (p *FullIntraRequest) MarshalSize() int { |
| 100 | return headerLength + firOffset + len(p.FIR)*8 |
| 101 | } |
| 102 | |
| 103 | func (p *FullIntraRequest) String() string { |
| 104 | var out strings.Builder |
| 105 | fmt.Fprintf(&out, "FullIntraRequest %x %x", |
| 106 | p.SenderSSRC, p.MediaSSRC) |
| 107 | for _, e := range p.FIR { |
| 108 | fmt.Fprintf(&out, " (%x %v)", e.SSRC, e.SequenceNumber) |
| 109 | } |
| 110 | |
| 111 | return out.String() |
| 112 | } |
| 113 | |
| 114 | // DestinationSSRC returns an array of SSRC values that this packet refers to. |
| 115 | func (p *FullIntraRequest) DestinationSSRC() []uint32 { |
| 116 | ssrcs := make([]uint32, 0, len(p.FIR)) |
| 117 | for _, entry := range p.FIR { |
| 118 | ssrcs = append(ssrcs, entry.SSRC) |
| 119 | } |
| 120 | |
| 121 | return ssrcs |
| 122 | } |