| 1 | // SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 | // SPDX-License-Identifier: MIT |
| 3 | |
| 4 | package rtcp |
| 5 | |
| 6 | import ( |
| 7 | "fmt" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/stretchr/testify/assert" |
| 11 | ) |
| 12 | |
| 13 | // Assert that ExtendedReport is a Packet. |
| 14 | var _ Packet = (*ExtendedReport)(nil) |
| 15 | |
| 16 | // Assert that all the extended report blocks implement the interface. |
| 17 | var ( |
| 18 | _ ReportBlock = (*LossRLEReportBlock)(nil) |
| 19 | _ ReportBlock = (*DuplicateRLEReportBlock)(nil) |
| 20 | _ ReportBlock = (*PacketReceiptTimesReportBlock)(nil) |
| 21 | _ ReportBlock = (*ReceiverReferenceTimeReportBlock)(nil) |
| 22 | _ ReportBlock = (*DLRRReportBlock)(nil) |
| 23 | _ ReportBlock = (*StatisticsSummaryReportBlock)(nil) |
| 24 | _ ReportBlock = (*VoIPMetricsReportBlock)(nil) |
| 25 | _ ReportBlock = (*UnknownReportBlock)(nil) |
| 26 | ) |
| 27 | |
| 28 | func testPacket() Packet { |
| 29 | return &ExtendedReport{ |
| 30 | SenderSSRC: 0x01020304, |
| 31 | Reports: []ReportBlock{ |
| 32 | &LossRLEReportBlock{ |
| 33 | XRHeader: XRHeader{ |
| 34 | BlockType: LossRLEReportBlockType, |
| 35 | }, |
| 36 | T: 12, |
| 37 | SSRC: 0x12345689, |
| 38 | BeginSeq: 5, |
| 39 | EndSeq: 12, |
| 40 | Chunks: []Chunk{ |
| 41 | Chunk(0x4006), |
| 42 | Chunk(0x0006), |
| 43 | Chunk(0x8765), |
| 44 | Chunk(0x0000), |
| 45 | }, |
| 46 | }, |
| 47 | &DuplicateRLEReportBlock{ |
| 48 | XRHeader: XRHeader{ |
| 49 | BlockType: DuplicateRLEReportBlockType, |
| 50 | }, |
| 51 | T: 6, |
| 52 | SSRC: 0x12345689, |
| 53 | BeginSeq: 5, |
| 54 | EndSeq: 12, |
| 55 | Chunks: []Chunk{ |
| 56 | Chunk(0x4123), |
| 57 | Chunk(0x3FFF), |
| 58 | Chunk(0xFFFF), |
| 59 | Chunk(0x0000), |
| 60 | }, |
| 61 | }, |
| 62 | &PacketReceiptTimesReportBlock{ |
| 63 | XRHeader: XRHeader{ |
| 64 | BlockType: PacketReceiptTimesReportBlockType, |
| 65 | }, |
| 66 | T: 3, |
| 67 | SSRC: 0x98765432, |
| 68 | BeginSeq: 15432, |
| 69 | EndSeq: 15577, |
| 70 | ReceiptTime: []uint32{ |
| 71 | 0x11111111, |
| 72 | 0x22222222, |
| 73 | 0x33333333, |
| 74 | 0x44444444, |
| 75 | 0x55555555, |
| 76 | }, |
| 77 | }, |
| 78 | &ReceiverReferenceTimeReportBlock{ |
| 79 | XRHeader: XRHeader{ |
| 80 | BlockType: ReceiverReferenceTimeReportBlockType, |
| 81 | }, |
| 82 | NTPTimestamp: 0x0102030405060708, |
| 83 | }, |
| 84 | &DLRRReportBlock{ |
| 85 | XRHeader: XRHeader{ |
| 86 | BlockType: DLRRReportBlockType, |
| 87 | }, |
| 88 | Reports: []DLRRReport{ |
| 89 | { |
| 90 | SSRC: 0x88888888, |
| 91 | LastRR: 0x12345678, |
| 92 | DLRR: 0x99999999, |
| 93 | }, |
| 94 | { |
| 95 | SSRC: 0x09090909, |
| 96 | LastRR: 0x12345678, |
| 97 | DLRR: 0x99999999, |
| 98 | }, |
| 99 | { |
| 100 | SSRC: 0x11223344, |
| 101 | LastRR: 0x12345678, |
| 102 | DLRR: 0x99999999, |
| 103 | }, |
| 104 | }, |
| 105 | }, |
| 106 | &StatisticsSummaryReportBlock{ |
| 107 | XRHeader{ |
| 108 | BlockType: StatisticsSummaryReportBlockType, |
| 109 | }, |
| 110 | true, true, true, ToHIPv4, |
| 111 | 0xFEDCBA98, |
| 112 | 0x1234, 0x5678, |
| 113 | 0x11111111, |
| 114 | 0x22222222, |
| 115 | 0x33333333, |
| 116 | 0x44444444, |
| 117 | 0x55555555, |
| 118 | 0x66666666, |
| 119 | 0x01, 0x02, 0x03, 0x04, |
| 120 | }, |
| 121 | &VoIPMetricsReportBlock{ |
| 122 | XRHeader{ |
| 123 | BlockType: VoIPMetricsReportBlockType, |
| 124 | }, |
| 125 | 0x89ABCDEF, |
| 126 | 0x05, 0x06, 0x07, 0x08, |
| 127 | 0x1111, 0x2222, 0x3333, 0x4444, |
| 128 | 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, |
| 129 | 0x00, |
| 130 | 0x1122, 0x3344, 0x5566, |
| 131 | }, |
| 132 | }, |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | func encodedPacket() []byte { |
| 137 | return []byte{ |
| 138 | // RTP Header |
| 139 | 0x80, 0xCF, 0x00, 0x33, // byte 0 - 3 |
| 140 | // Sender SSRC |
| 141 | 0x01, 0x02, 0x03, 0x04, |
| 142 | // Loss RLE Report Block |
| 143 | 0x01, 0x0C, 0x00, 0x04, // byte 8 - 11 |
| 144 | // Source SSRC |
| 145 | 0x12, 0x34, 0x56, 0x89, |
| 146 | // Begin & End Seq |
| 147 | 0x00, 0x05, 0x00, 0x0C, // byte 16 - 19 |
| 148 | // Chunks |
| 149 | 0x40, 0x06, 0x00, 0x06, |
| 150 | 0x87, 0x65, 0x00, 0x00, // byte 24 - 27 |
| 151 | // Duplicate RLE Report Block |
| 152 | 0x02, 0x06, 0x00, 0x04, |
| 153 | // Source SSRC |
| 154 | 0x12, 0x34, 0x56, 0x89, // byte 32 - 35 |
| 155 | // Begin & End Seq |
| 156 | 0x00, 0x05, 0x00, 0x0C, |
| 157 | // Chunks |
| 158 | 0x41, 0x23, 0x3F, 0xFF, // byte 40 - 43 |
| 159 | 0xFF, 0xFF, 0x00, 0x00, |
| 160 | // Packet Receipt Times Report Block |
| 161 | 0x03, 0x03, 0x00, 0x07, // byte 48 - 51 |
| 162 | // Source SSRC |
| 163 | 0x98, 0x76, 0x54, 0x32, |
| 164 | // Begin & End Seq |
| 165 | 0x3C, 0x48, 0x3C, 0xD9, // byte 56 - 59 |
| 166 | // Receipt times |
| 167 | 0x11, 0x11, 0x11, 0x11, |
| 168 | 0x22, 0x22, 0x22, 0x22, // byte 64 - 67 |
| 169 | 0x33, 0x33, 0x33, 0x33, |
| 170 | 0x44, 0x44, 0x44, 0x44, // byte 72 - 75 |
| 171 | 0x55, 0x55, 0x55, 0x55, |
| 172 | // Receiver Reference Time Report |
| 173 | 0x04, 0x00, 0x00, 0x02, // byte 80 - 83 |
| 174 | // Timestamp |
| 175 | 0x01, 0x02, 0x03, 0x04, |
| 176 | 0x05, 0x06, 0x07, 0x08, // byte 88 - 91 |
| 177 | // DLRR Report |
| 178 | 0x05, 0x00, 0x00, 0x09, |
| 179 | // SSRC 1 |
| 180 | 0x88, 0x88, 0x88, 0x88, // byte 96 - 99 |
| 181 | // LastRR 1 |
| 182 | 0x12, 0x34, 0x56, 0x78, |
| 183 | // DLRR 1 |
| 184 | 0x99, 0x99, 0x99, 0x99, // byte 104 - 107 |
| 185 | // SSRC 2 |
| 186 | 0x09, 0x09, 0x09, 0x09, |
| 187 | // LastRR 2 |
| 188 | 0x12, 0x34, 0x56, 0x78, // byte 112 - 115 |
| 189 | // DLRR 2 |
| 190 | 0x99, 0x99, 0x99, 0x99, |
| 191 | // SSRC 3 |
| 192 | 0x11, 0x22, 0x33, 0x44, // byte 120 - 123 |
| 193 | // LastRR 3 |
| 194 | 0x12, 0x34, 0x56, 0x78, |
| 195 | // DLRR 3 |
| 196 | 0x99, 0x99, 0x99, 0x99, // byte 128 - 131 |
| 197 | // Statistics Summary Report |
| 198 | 0x06, 0xE8, 0x00, 0x09, |
| 199 | // SSRC |
| 200 | 0xFE, 0xDC, 0xBA, 0x98, // byte 136 - 139 |
| 201 | // Various statistics |
| 202 | 0x12, 0x34, 0x56, 0x78, |
| 203 | 0x11, 0x11, 0x11, 0x11, // byte 144 - 147 |
| 204 | 0x22, 0x22, 0x22, 0x22, |
| 205 | 0x33, 0x33, 0x33, 0x33, // byte 152 - 155 |
| 206 | 0x44, 0x44, 0x44, 0x44, |
| 207 | 0x55, 0x55, 0x55, 0x55, // byte 160 - 163 |
| 208 | 0x66, 0x66, 0x66, 0x66, |
| 209 | 0x01, 0x02, 0x03, 0x04, // byte 168 - 171 |
| 210 | // VoIP Metrics Report |
| 211 | 0x07, 0x00, 0x00, 0x08, |
| 212 | // SSRC |
| 213 | 0x89, 0xAB, 0xCD, 0xEF, // byte 176 - 179 |
| 214 | // Various statistics |
| 215 | 0x05, 0x06, 0x07, 0x08, |
| 216 | 0x11, 0x11, 0x22, 0x22, // byte 184 - 187 |
| 217 | 0x33, 0x33, 0x44, 0x44, |
| 218 | 0x11, 0x22, 0x33, 0x44, // byte 192 - 195 |
| 219 | 0x55, 0x66, 0x77, 0x88, |
| 220 | 0x99, 0x00, 0x11, 0x22, // byte 200 - 203 |
| 221 | 0x33, 0x44, 0x55, 0x66, // byte 204 - 207 |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | func TestEncode(t *testing.T) { |
| 226 | expected := encodedPacket() |
| 227 | packet := testPacket() |
| 228 | rawPacket, err := packet.Marshal() |
| 229 | assert.NoError(t, err) |
| 230 | assert.Equal(t, len(expected), len(rawPacket), "Encoded message length does not match expected length") |
| 231 | |
| 232 | for i := range rawPacket { |
| 233 | assert.Equalf(t, expected[i], rawPacket[i], "Byte %d of encoded packet does not match", i) |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | func TestDecode(t *testing.T) { |
| 238 | encoded := encodedPacket() |
| 239 | expected := testPacket() |
| 240 | |
| 241 | // We need to make sure the header has been set up correctly |
| 242 | // before we test for equality |
| 243 | extendedReports, ok := expected.(*ExtendedReport) |
| 244 | assert.True(t, ok) |
| 245 | |
| 246 | for _, p := range extendedReports.Reports { |
| 247 | p.setupBlockHeader() |
| 248 | } |
| 249 | |
| 250 | report := new(ExtendedReport) |
| 251 | err := report.Unmarshal(encoded) |
| 252 | assert.NoError(t, err) |
| 253 | assert.Equal(t, expected, report) |
| 254 | |
| 255 | pktStringer, ok := expected.(fmt.Stringer) |
| 256 | assert.True(t, ok) |
| 257 | assert.Equal(t, report.String(), pktStringer.String(), "Decoded packet does not match expected packet") |
| 258 | |
| 259 | var includeSenderSSRC bool |
| 260 | for _, ssrc := range report.DestinationSSRC() { |
| 261 | if ssrc == report.SenderSSRC { |
| 262 | includeSenderSSRC = true |
| 263 | } |
| 264 | } |
| 265 | assert.True(t, includeSenderSSRC, "DestinationSSRC does not include the SenderSSRC") |
| 266 | } |