| 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 | "math" |
| 10 | ) |
| 11 | |
| 12 | // SLIEntry represents a single entry to the SLI packet's |
| 13 | // list of lost slices. |
| 14 | type SLIEntry struct { |
| 15 | // ID of first lost slice |
| 16 | First uint16 |
| 17 | |
| 18 | // Number of lost slices |
| 19 | Number uint16 |
| 20 | |
| 21 | // ID of related picture |
| 22 | Picture uint8 |
| 23 | } |
| 24 | |
| 25 | // The SliceLossIndication packet informs the encoder about the loss of a picture slice. |
| 26 | type SliceLossIndication struct { |
| 27 | // SSRC of sender |
| 28 | SenderSSRC uint32 |
| 29 | |
| 30 | // SSRC of the media source |
| 31 | MediaSSRC uint32 |
| 32 | |
| 33 | SLI []SLIEntry |
| 34 | } |
| 35 | |
| 36 | const ( |
| 37 | sliLength = 2 |
| 38 | sliOffset = 8 |
| 39 | ) |
| 40 | |
| 41 | // Marshal encodes the SliceLossIndication in binary. |
| 42 | func (p SliceLossIndication) Marshal() ([]byte, error) { |
| 43 | if len(p.SLI)+sliLength > math.MaxUint8 { |
| 44 | return nil, errTooManyReports |
| 45 | } |
| 46 | |
| 47 | rawPacket := make([]byte, sliOffset+(len(p.SLI)*4)) |
| 48 | binary.BigEndian.PutUint32(rawPacket, p.SenderSSRC) |
| 49 | binary.BigEndian.PutUint32(rawPacket[4:], p.MediaSSRC) |
| 50 | for i, s := range p.SLI { |
| 51 | sli := ((uint32(s.First) & 0x1FFF) << 19) | |
| 52 | ((uint32(s.Number) & 0x1FFF) << 6) | |
| 53 | (uint32(s.Picture) & 0x3F) |
| 54 | binary.BigEndian.PutUint32(rawPacket[sliOffset+(4*i):], sli) |
| 55 | } |
| 56 | hData, err := p.Header().Marshal() |
| 57 | if err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | |
| 61 | return append(hData, rawPacket...), nil |
| 62 | } |
| 63 | |
| 64 | // Unmarshal decodes the SliceLossIndication from binary. |
| 65 | func (p *SliceLossIndication) Unmarshal(rawPacket []byte) error { |
| 66 | if len(rawPacket) < (headerLength + ssrcLength) { |
| 67 | return errPacketTooShort |
| 68 | } |
| 69 | |
| 70 | var header Header |
| 71 | if err := header.Unmarshal(rawPacket); err != nil { |
| 72 | return err |
| 73 | } |
| 74 | |
| 75 | if len(rawPacket) < (headerLength + 4*int(header.Length)) { |
| 76 | return errPacketTooShort |
| 77 | } |
| 78 | |
| 79 | if header.Type != TypeTransportSpecificFeedback || header.Count != FormatSLI { |
| 80 | return errWrongType |
| 81 | } |
| 82 | |
| 83 | p.SenderSSRC = binary.BigEndian.Uint32(rawPacket[headerLength:]) |
| 84 | p.MediaSSRC = binary.BigEndian.Uint32(rawPacket[headerLength+ssrcLength:]) |
| 85 | for i := headerLength + sliOffset; i < (headerLength + 4*int(header.Length)); i += 4 { |
| 86 | sli := binary.BigEndian.Uint32(rawPacket[i:]) |
| 87 | p.SLI = append(p.SLI, SLIEntry{ |
| 88 | First: uint16((sli >> 19) & 0x1FFF), //nolint:gosec // G115 |
| 89 | Number: uint16((sli >> 6) & 0x1FFF), //nolint:gosec // G115 |
| 90 | Picture: uint8(sli & 0x3F), //nolint:gosec // G115 |
| 91 | }) |
| 92 | } |
| 93 | |
| 94 | return nil |
| 95 | } |
| 96 | |
| 97 | // MarshalSize returns the size of the packet once marshaled. |
| 98 | func (p *SliceLossIndication) MarshalSize() int { |
| 99 | return headerLength + sliOffset + (len(p.SLI) * 4) |
| 100 | } |
| 101 | |
| 102 | // Header returns the Header associated with this packet. |
| 103 | func (p *SliceLossIndication) Header() Header { |
| 104 | return Header{ |
| 105 | Count: FormatSLI, |
| 106 | Type: TypeTransportSpecificFeedback, |
| 107 | Length: uint16((p.MarshalSize() / 4) - 1), //nolint:gosec // G115 |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | func (p *SliceLossIndication) String() string { |
| 112 | return fmt.Sprintf("SliceLossIndication %x %x %+v", p.SenderSSRC, p.MediaSSRC, p.SLI) |
| 113 | } |
| 114 | |
| 115 | // DestinationSSRC returns an array of SSRC values that this packet refers to. |
| 116 | func (p *SliceLossIndication) DestinationSSRC() []uint32 { |
| 117 | return []uint32{p.MediaSSRC} |
| 118 | } |