| 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 | ) |
| 10 | |
| 11 | // The RapidResynchronizationRequest packet informs the encoder about the loss of |
| 12 | // an undefined amount of coded video data belonging to one or more pictures. |
| 13 | type RapidResynchronizationRequest struct { |
| 14 | // SSRC of sender |
| 15 | SenderSSRC uint32 |
| 16 | |
| 17 | // SSRC of the media source |
| 18 | MediaSSRC uint32 |
| 19 | } |
| 20 | |
| 21 | // RapidResynchronisationRequest is provided as RFC 6051 spells resynchronization with an s. |
| 22 | // We provide both names to be consistent with other RFCs which spell resynchronization with a z. |
| 23 | type RapidResynchronisationRequest = RapidResynchronizationRequest |
| 24 | |
| 25 | const ( |
| 26 | rrrLength = 2 |
| 27 | rrrHeaderLength = ssrcLength * 2 |
| 28 | rrrMediaOffset = 4 |
| 29 | ) |
| 30 | |
| 31 | // Marshal encodes the RapidResynchronizationRequest in binary. |
| 32 | func (p RapidResynchronizationRequest) Marshal() ([]byte, error) { |
| 33 | /* |
| 34 | * RRR does not require parameters. Therefore, the length field MUST be |
| 35 | * 2, and there MUST NOT be any Feedback Control Information. |
| 36 | * |
| 37 | * The semantics of this FB message is independent of the payload type. |
| 38 | */ |
| 39 | rawPacket := make([]byte, p.MarshalSize()) |
| 40 | packetBody := rawPacket[headerLength:] |
| 41 | |
| 42 | binary.BigEndian.PutUint32(packetBody, p.SenderSSRC) |
| 43 | binary.BigEndian.PutUint32(packetBody[rrrMediaOffset:], p.MediaSSRC) |
| 44 | |
| 45 | hData, err := p.Header().Marshal() |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | copy(rawPacket, hData) |
| 50 | |
| 51 | return rawPacket, nil |
| 52 | } |
| 53 | |
| 54 | // Unmarshal decodes the RapidResynchronizationRequest from binary. |
| 55 | func (p *RapidResynchronizationRequest) Unmarshal(rawPacket []byte) error { |
| 56 | if len(rawPacket) < (headerLength + (ssrcLength * 2)) { |
| 57 | return errPacketTooShort |
| 58 | } |
| 59 | |
| 60 | var h Header |
| 61 | if err := h.Unmarshal(rawPacket); err != nil { |
| 62 | return err |
| 63 | } |
| 64 | |
| 65 | if h.Type != TypeTransportSpecificFeedback || h.Count != FormatRRR { |
| 66 | return errWrongType |
| 67 | } |
| 68 | |
| 69 | p.SenderSSRC = binary.BigEndian.Uint32(rawPacket[headerLength:]) |
| 70 | p.MediaSSRC = binary.BigEndian.Uint32(rawPacket[headerLength+ssrcLength:]) |
| 71 | |
| 72 | return nil |
| 73 | } |
| 74 | |
| 75 | // MarshalSize returns the size of the packet once marshaled. |
| 76 | func (p *RapidResynchronizationRequest) MarshalSize() int { |
| 77 | return headerLength + rrrHeaderLength |
| 78 | } |
| 79 | |
| 80 | // Header returns the Header associated with this packet. |
| 81 | func (p *RapidResynchronizationRequest) Header() Header { |
| 82 | return Header{ |
| 83 | Count: FormatRRR, |
| 84 | Type: TypeTransportSpecificFeedback, |
| 85 | Length: rrrLength, |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // DestinationSSRC returns an array of SSRC values that this packet refers to. |
| 90 | func (p *RapidResynchronizationRequest) DestinationSSRC() []uint32 { |
| 91 | return []uint32{p.MediaSSRC} |
| 92 | } |
| 93 | |
| 94 | func (p *RapidResynchronizationRequest) String() string { |
| 95 | return fmt.Sprintf("RapidResynchronizationRequest %x %x", p.SenderSSRC, p.MediaSSRC) |
| 96 | } |