1 /* 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 * 10 * This file includes unit tests for the RtcpPacket. 11 */ 12 13 #include "testing/gtest/include/gtest/gtest.h" 14 15 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet.h" 16 #include "webrtc/test/rtcp_packet_parser.h" 17 18 using webrtc::rtcp::App; 19 using webrtc::rtcp::Bye; 20 using webrtc::rtcp::Empty; 21 using webrtc::rtcp::Fir; 22 using webrtc::rtcp::Ij; 23 using webrtc::rtcp::Nack; 24 using webrtc::rtcp::Pli; 25 using webrtc::rtcp::Sdes; 26 using webrtc::rtcp::SenderReport; 27 using webrtc::rtcp::Sli; 28 using webrtc::rtcp::RawPacket; 29 using webrtc::rtcp::ReceiverReport; 30 using webrtc::rtcp::ReportBlock; 31 using webrtc::rtcp::Rpsi; 32 using webrtc::test::RtcpPacketParser; 33 34 namespace webrtc { 35 36 const uint32_t kSenderSsrc = 0x12345678; 37 const uint32_t kRemoteSsrc = 0x23456789; 38 39 TEST(RtcpPacketTest, Rr) { 40 ReceiverReport rr; 41 rr.From(kSenderSsrc); 42 43 RawPacket packet = rr.Build(); 44 RtcpPacketParser parser; 45 parser.Parse(packet.buffer(), packet.buffer_length()); 46 EXPECT_EQ(1, parser.receiver_report()->num_packets()); 47 EXPECT_EQ(kSenderSsrc, parser.receiver_report()->Ssrc()); 48 EXPECT_EQ(0, parser.report_block()->num_packets()); 49 } 50 51 TEST(RtcpPacketTest, RrWithOneReportBlock) { 52 ReportBlock rb; 53 rb.To(kRemoteSsrc); 54 rb.WithFractionLost(55); 55 rb.WithCumulativeLost(0x111111); 56 rb.WithExtHighestSeqNum(0x22222222); 57 rb.WithJitter(0x33333333); 58 rb.WithLastSr(0x44444444); 59 rb.WithDelayLastSr(0x55555555); 60 61 ReceiverReport rr; 62 rr.From(kSenderSsrc); 63 rr.WithReportBlock(&rb); 64 65 RawPacket packet = rr.Build(); 66 RtcpPacketParser parser; 67 parser.Parse(packet.buffer(), packet.buffer_length()); 68 EXPECT_EQ(1, parser.receiver_report()->num_packets()); 69 EXPECT_EQ(kSenderSsrc, parser.receiver_report()->Ssrc()); 70 EXPECT_EQ(1, parser.report_block()->num_packets()); 71 EXPECT_EQ(kRemoteSsrc, parser.report_block()->Ssrc()); 72 EXPECT_EQ(55U, parser.report_block()->FractionLost()); 73 EXPECT_EQ(0x111111U, parser.report_block()->CumPacketLost()); 74 EXPECT_EQ(0x22222222U, parser.report_block()->ExtHighestSeqNum()); 75 EXPECT_EQ(0x33333333U, parser.report_block()->Jitter()); 76 EXPECT_EQ(0x44444444U, parser.report_block()->LastSr()); 77 EXPECT_EQ(0x55555555U, parser.report_block()->DelayLastSr()); 78 } 79 80 TEST(RtcpPacketTest, RrWithTwoReportBlocks) { 81 ReportBlock rb1; 82 rb1.To(kRemoteSsrc); 83 ReportBlock rb2; 84 rb2.To(kRemoteSsrc + 1); 85 86 ReceiverReport rr; 87 rr.From(kSenderSsrc); 88 rr.WithReportBlock(&rb1); 89 rr.WithReportBlock(&rb2); 90 91 RawPacket packet = rr.Build(); 92 RtcpPacketParser parser; 93 parser.Parse(packet.buffer(), packet.buffer_length()); 94 EXPECT_EQ(1, parser.receiver_report()->num_packets()); 95 EXPECT_EQ(kSenderSsrc, parser.receiver_report()->Ssrc()); 96 EXPECT_EQ(2, parser.report_block()->num_packets()); 97 EXPECT_EQ(1, parser.report_blocks_per_ssrc(kRemoteSsrc)); 98 EXPECT_EQ(1, parser.report_blocks_per_ssrc(kRemoteSsrc + 1)); 99 } 100 101 TEST(RtcpPacketTest, Sr) { 102 SenderReport sr; 103 sr.From(kSenderSsrc); 104 sr.WithNtpSec(0x11111111); 105 sr.WithNtpFrac(0x22222222); 106 sr.WithRtpTimestamp(0x33333333); 107 sr.WithPacketCount(0x44444444); 108 sr.WithOctetCount(0x55555555); 109 110 RawPacket packet = sr.Build(); 111 RtcpPacketParser parser; 112 parser.Parse(packet.buffer(), packet.buffer_length()); 113 114 EXPECT_EQ(1, parser.sender_report()->num_packets()); 115 EXPECT_EQ(kSenderSsrc, parser.sender_report()->Ssrc()); 116 EXPECT_EQ(0x11111111U, parser.sender_report()->NtpSec()); 117 EXPECT_EQ(0x22222222U, parser.sender_report()->NtpFrac()); 118 EXPECT_EQ(0x33333333U, parser.sender_report()->RtpTimestamp()); 119 EXPECT_EQ(0x44444444U, parser.sender_report()->PacketCount()); 120 EXPECT_EQ(0x55555555U, parser.sender_report()->OctetCount()); 121 EXPECT_EQ(0, parser.report_block()->num_packets()); 122 } 123 124 TEST(RtcpPacketTest, SrWithOneReportBlock) { 125 ReportBlock rb; 126 rb.To(kRemoteSsrc); 127 128 SenderReport sr; 129 sr.From(kSenderSsrc); 130 sr.WithReportBlock(&rb); 131 132 RawPacket packet = sr.Build(); 133 RtcpPacketParser parser; 134 parser.Parse(packet.buffer(), packet.buffer_length()); 135 EXPECT_EQ(1, parser.sender_report()->num_packets()); 136 EXPECT_EQ(kSenderSsrc, parser.sender_report()->Ssrc()); 137 EXPECT_EQ(1, parser.report_block()->num_packets()); 138 EXPECT_EQ(kRemoteSsrc, parser.report_block()->Ssrc()); 139 } 140 141 TEST(RtcpPacketTest, SrWithTwoReportBlocks) { 142 ReportBlock rb1; 143 rb1.To(kRemoteSsrc); 144 ReportBlock rb2; 145 rb2.To(kRemoteSsrc + 1); 146 147 SenderReport sr; 148 sr.From(kSenderSsrc); 149 sr.WithReportBlock(&rb1); 150 sr.WithReportBlock(&rb2); 151 152 RawPacket packet = sr.Build(); 153 RtcpPacketParser parser; 154 parser.Parse(packet.buffer(), packet.buffer_length()); 155 EXPECT_EQ(1, parser.sender_report()->num_packets()); 156 EXPECT_EQ(kSenderSsrc, parser.sender_report()->Ssrc()); 157 EXPECT_EQ(2, parser.report_block()->num_packets()); 158 EXPECT_EQ(1, parser.report_blocks_per_ssrc(kRemoteSsrc)); 159 EXPECT_EQ(1, parser.report_blocks_per_ssrc(kRemoteSsrc + 1)); 160 } 161 162 TEST(RtcpPacketTest, IjNoItem) { 163 Ij ij; 164 165 RawPacket packet = ij.Build(); 166 RtcpPacketParser parser; 167 parser.Parse(packet.buffer(), packet.buffer_length()); 168 EXPECT_EQ(1, parser.ij()->num_packets()); 169 EXPECT_EQ(0, parser.ij_item()->num_packets()); 170 } 171 172 TEST(RtcpPacketTest, IjOneItem) { 173 Ij ij; 174 ij.WithJitterItem(0x11111111); 175 176 RawPacket packet = ij.Build(); 177 RtcpPacketParser parser; 178 parser.Parse(packet.buffer(), packet.buffer_length()); 179 EXPECT_EQ(1, parser.ij()->num_packets()); 180 EXPECT_EQ(1, parser.ij_item()->num_packets()); 181 EXPECT_EQ(0x11111111U, parser.ij_item()->Jitter()); 182 } 183 184 TEST(RtcpPacketTest, IjTwoItems) { 185 Ij ij; 186 ij.WithJitterItem(0x11111111); 187 ij.WithJitterItem(0x22222222); 188 189 RawPacket packet = ij.Build(); 190 RtcpPacketParser parser; 191 parser.Parse(packet.buffer(), packet.buffer_length()); 192 EXPECT_EQ(1, parser.ij()->num_packets()); 193 EXPECT_EQ(2, parser.ij_item()->num_packets()); 194 EXPECT_EQ(0x22222222U, parser.ij_item()->Jitter()); 195 } 196 197 TEST(RtcpPacketTest, AppWithNoData) { 198 App app; 199 app.WithSubType(30); 200 uint32_t name = 'n' << 24; 201 name += 'a' << 16; 202 name += 'm' << 8; 203 name += 'e'; 204 app.WithName(name); 205 206 RawPacket packet = app.Build(); 207 RtcpPacketParser parser; 208 parser.Parse(packet.buffer(), packet.buffer_length()); 209 EXPECT_EQ(1, parser.app()->num_packets()); 210 EXPECT_EQ(30U, parser.app()->SubType()); 211 EXPECT_EQ(name, parser.app()->Name()); 212 EXPECT_EQ(0, parser.app_item()->num_packets()); 213 } 214 215 TEST(RtcpPacketTest, App) { 216 App app; 217 app.From(kSenderSsrc); 218 app.WithSubType(30); 219 uint32_t name = 'n' << 24; 220 name += 'a' << 16; 221 name += 'm' << 8; 222 name += 'e'; 223 app.WithName(name); 224 const char kData[] = {'t', 'e', 's', 't', 'd', 'a', 't', 'a'}; 225 const size_t kDataLength = sizeof(kData) / sizeof(kData[0]); 226 app.WithData((const uint8_t*)kData, kDataLength); 227 228 RawPacket packet = app.Build(); 229 RtcpPacketParser parser; 230 parser.Parse(packet.buffer(), packet.buffer_length()); 231 EXPECT_EQ(1, parser.app()->num_packets()); 232 EXPECT_EQ(30U, parser.app()->SubType()); 233 EXPECT_EQ(name, parser.app()->Name()); 234 EXPECT_EQ(1, parser.app_item()->num_packets()); 235 EXPECT_EQ(kDataLength, parser.app_item()->DataLength()); 236 EXPECT_EQ(0, strncmp(kData, (const char*)parser.app_item()->Data(), 237 parser.app_item()->DataLength())); 238 } 239 240 TEST(RtcpPacketTest, SdesWithOneChunk) { 241 Sdes sdes; 242 sdes.WithCName(kSenderSsrc, "alice@host"); 243 244 RawPacket packet = sdes.Build(); 245 RtcpPacketParser parser; 246 parser.Parse(packet.buffer(), packet.buffer_length()); 247 EXPECT_EQ(1, parser.sdes()->num_packets()); 248 EXPECT_EQ(1, parser.sdes_chunk()->num_packets()); 249 EXPECT_EQ(kSenderSsrc, parser.sdes_chunk()->Ssrc()); 250 EXPECT_EQ("alice@host", parser.sdes_chunk()->Cname()); 251 } 252 253 TEST(RtcpPacketTest, SdesWithMultipleChunks) { 254 Sdes sdes; 255 sdes.WithCName(kSenderSsrc, "a"); 256 sdes.WithCName(kSenderSsrc + 1, "ab"); 257 sdes.WithCName(kSenderSsrc + 2, "abc"); 258 sdes.WithCName(kSenderSsrc + 3, "abcd"); 259 sdes.WithCName(kSenderSsrc + 4, "abcde"); 260 sdes.WithCName(kSenderSsrc + 5, "abcdef"); 261 262 RawPacket packet = sdes.Build(); 263 RtcpPacketParser parser; 264 parser.Parse(packet.buffer(), packet.buffer_length()); 265 EXPECT_EQ(1, parser.sdes()->num_packets()); 266 EXPECT_EQ(6, parser.sdes_chunk()->num_packets()); 267 EXPECT_EQ(kSenderSsrc + 5, parser.sdes_chunk()->Ssrc()); 268 EXPECT_EQ("abcdef", parser.sdes_chunk()->Cname()); 269 } 270 271 TEST(RtcpPacketTest, CnameItemWithEmptyString) { 272 Sdes sdes; 273 sdes.WithCName(kSenderSsrc, ""); 274 275 RawPacket packet = sdes.Build(); 276 RtcpPacketParser parser; 277 parser.Parse(packet.buffer(), packet.buffer_length()); 278 EXPECT_EQ(1, parser.sdes()->num_packets()); 279 EXPECT_EQ(1, parser.sdes_chunk()->num_packets()); 280 EXPECT_EQ(kSenderSsrc, parser.sdes_chunk()->Ssrc()); 281 EXPECT_EQ("", parser.sdes_chunk()->Cname()); 282 } 283 284 TEST(RtcpPacketTest, Pli) { 285 Pli pli; 286 pli.From(kSenderSsrc); 287 pli.To(kRemoteSsrc); 288 289 RawPacket packet = pli.Build(); 290 RtcpPacketParser parser; 291 parser.Parse(packet.buffer(), packet.buffer_length()); 292 EXPECT_EQ(1, parser.pli()->num_packets()); 293 EXPECT_EQ(kSenderSsrc, parser.pli()->Ssrc()); 294 EXPECT_EQ(kRemoteSsrc, parser.pli()->MediaSsrc()); 295 } 296 297 TEST(RtcpPacketTest, Sli) { 298 const uint16_t kFirstMb = 7777; 299 const uint16_t kNumberOfMb = 6666; 300 const uint8_t kPictureId = 60; 301 Sli sli; 302 sli.From(kSenderSsrc); 303 sli.To(kRemoteSsrc); 304 sli.WithFirstMb(kFirstMb); 305 sli.WithNumberOfMb(kNumberOfMb); 306 sli.WithPictureId(kPictureId); 307 308 RawPacket packet = sli.Build(); 309 RtcpPacketParser parser; 310 parser.Parse(packet.buffer(), packet.buffer_length()); 311 EXPECT_EQ(1, parser.sli()->num_packets()); 312 EXPECT_EQ(kSenderSsrc, parser.sli()->Ssrc()); 313 EXPECT_EQ(kRemoteSsrc, parser.sli()->MediaSsrc()); 314 EXPECT_EQ(1, parser.sli_item()->num_packets()); 315 EXPECT_EQ(kFirstMb, parser.sli_item()->FirstMb()); 316 EXPECT_EQ(kNumberOfMb, parser.sli_item()->NumberOfMb()); 317 EXPECT_EQ(kPictureId, parser.sli_item()->PictureId()); 318 } 319 320 TEST(RtcpPacketTest, Nack) { 321 Nack nack; 322 const uint16_t kList[] = {0, 1, 3, 8, 16}; 323 const uint16_t kListLength = sizeof(kList) / sizeof(kList[0]); 324 nack.From(kSenderSsrc); 325 nack.To(kRemoteSsrc); 326 nack.WithList(kList, kListLength); 327 RawPacket packet = nack.Build(); 328 RtcpPacketParser parser; 329 parser.Parse(packet.buffer(), packet.buffer_length()); 330 EXPECT_EQ(1, parser.nack()->num_packets()); 331 EXPECT_EQ(kSenderSsrc, parser.nack()->Ssrc()); 332 EXPECT_EQ(kRemoteSsrc, parser.nack()->MediaSsrc()); 333 EXPECT_EQ(1, parser.nack_item()->num_packets()); 334 std::vector<uint16_t> seqs = parser.nack_item()->last_nack_list(); 335 EXPECT_EQ(kListLength, seqs.size()); 336 for (size_t i = 0; i < kListLength; ++i) { 337 EXPECT_EQ(kList[i], seqs[i]); 338 } 339 } 340 341 TEST(RtcpPacketTest, NackWithWrap) { 342 Nack nack; 343 const uint16_t kList[] = {65500, 65516, 65534, 65535, 0, 1, 3, 20, 100}; 344 const uint16_t kListLength = sizeof(kList) / sizeof(kList[0]); 345 nack.From(kSenderSsrc); 346 nack.To(kRemoteSsrc); 347 nack.WithList(kList, kListLength); 348 RawPacket packet = nack.Build(); 349 RtcpPacketParser parser; 350 parser.Parse(packet.buffer(), packet.buffer_length()); 351 EXPECT_EQ(1, parser.nack()->num_packets()); 352 EXPECT_EQ(kSenderSsrc, parser.nack()->Ssrc()); 353 EXPECT_EQ(kRemoteSsrc, parser.nack()->MediaSsrc()); 354 EXPECT_EQ(4, parser.nack_item()->num_packets()); 355 std::vector<uint16_t> seqs = parser.nack_item()->last_nack_list(); 356 EXPECT_EQ(kListLength, seqs.size()); 357 for (size_t i = 0; i < kListLength; ++i) { 358 EXPECT_EQ(kList[i], seqs[i]); 359 } 360 } 361 362 TEST(RtcpPacketTest, Rpsi) { 363 Rpsi rpsi; 364 // 1000001 (7 bits = 1 byte in native string). 365 const uint64_t kPictureId = 0x41; 366 const uint16_t kNumberOfValidBytes = 1; 367 rpsi.WithPayloadType(100); 368 rpsi.WithPictureId(kPictureId); 369 370 RawPacket packet = rpsi.Build(); 371 RtcpPacketParser parser; 372 parser.Parse(packet.buffer(), packet.buffer_length()); 373 EXPECT_EQ(100, parser.rpsi()->PayloadType()); 374 EXPECT_EQ(kNumberOfValidBytes * 8, parser.rpsi()->NumberOfValidBits()); 375 EXPECT_EQ(kPictureId, parser.rpsi()->PictureId()); 376 } 377 378 TEST(RtcpPacketTest, RpsiWithTwoByteNativeString) { 379 Rpsi rpsi; 380 // |1 0000001 (7 bits = 1 byte in native string). 381 const uint64_t kPictureId = 0x81; 382 const uint16_t kNumberOfValidBytes = 2; 383 rpsi.WithPictureId(kPictureId); 384 385 RawPacket packet = rpsi.Build(); 386 RtcpPacketParser parser; 387 parser.Parse(packet.buffer(), packet.buffer_length()); 388 EXPECT_EQ(kNumberOfValidBytes * 8, parser.rpsi()->NumberOfValidBits()); 389 EXPECT_EQ(kPictureId, parser.rpsi()->PictureId()); 390 } 391 392 TEST(RtcpPacketTest, RpsiWithThreeByteNativeString) { 393 Rpsi rpsi; 394 // 10000|00 100000|0 1000000 (7 bits = 1 byte in native string). 395 const uint64_t kPictureId = 0x102040; 396 const uint16_t kNumberOfValidBytes = 3; 397 rpsi.WithPictureId(kPictureId); 398 399 RawPacket packet = rpsi.Build(); 400 RtcpPacketParser parser; 401 parser.Parse(packet.buffer(), packet.buffer_length()); 402 EXPECT_EQ(kNumberOfValidBytes * 8, parser.rpsi()->NumberOfValidBits()); 403 EXPECT_EQ(kPictureId, parser.rpsi()->PictureId()); 404 } 405 406 TEST(RtcpPacketTest, RpsiWithFourByteNativeString) { 407 Rpsi rpsi; 408 // 1000|001 00001|01 100001|1 1000010 (7 bits = 1 byte in native string). 409 const uint64_t kPictureId = 0x84161C2; 410 const uint16_t kNumberOfValidBytes = 4; 411 rpsi.WithPictureId(kPictureId); 412 413 RawPacket packet = rpsi.Build(); 414 RtcpPacketParser parser; 415 parser.Parse(packet.buffer(), packet.buffer_length()); 416 EXPECT_EQ(kNumberOfValidBytes * 8, parser.rpsi()->NumberOfValidBits()); 417 EXPECT_EQ(kPictureId, parser.rpsi()->PictureId()); 418 } 419 420 TEST(RtcpPacketTest, RpsiWithMaxPictureId) { 421 Rpsi rpsi; 422 // 1 1111111| 1111111 1|111111 11|11111 111|1111 1111|111 11111| 423 // 11 111111|1 1111111 (7 bits = 1 byte in native string). 424 const uint64_t kPictureId = 0xffffffffffffffff; 425 const uint16_t kNumberOfValidBytes = 10; 426 rpsi.WithPictureId(kPictureId); 427 428 RawPacket packet = rpsi.Build(); 429 RtcpPacketParser parser; 430 parser.Parse(packet.buffer(), packet.buffer_length()); 431 EXPECT_EQ(kNumberOfValidBytes * 8, parser.rpsi()->NumberOfValidBits()); 432 EXPECT_EQ(kPictureId, parser.rpsi()->PictureId()); 433 } 434 435 TEST(RtcpPacketTest, Fir) { 436 Fir fir; 437 fir.From(kSenderSsrc); 438 fir.To(kRemoteSsrc); 439 fir.WithCommandSeqNum(123); 440 441 RawPacket packet = fir.Build(); 442 RtcpPacketParser parser; 443 parser.Parse(packet.buffer(), packet.buffer_length()); 444 EXPECT_EQ(1, parser.fir()->num_packets()); 445 EXPECT_EQ(kSenderSsrc, parser.fir()->Ssrc()); 446 EXPECT_EQ(1, parser.fir_item()->num_packets()); 447 EXPECT_EQ(kRemoteSsrc, parser.fir_item()->Ssrc()); 448 EXPECT_EQ(123U, parser.fir_item()->SeqNum()); 449 } 450 451 TEST(RtcpPacketTest, AppendPacket) { 452 Fir fir; 453 ReportBlock rb; 454 ReceiverReport rr; 455 rr.From(kSenderSsrc); 456 rr.WithReportBlock(&rb); 457 rr.Append(&fir); 458 459 RawPacket packet = rr.Build(); 460 RtcpPacketParser parser; 461 parser.Parse(packet.buffer(), packet.buffer_length()); 462 EXPECT_EQ(1, parser.receiver_report()->num_packets()); 463 EXPECT_EQ(kSenderSsrc, parser.receiver_report()->Ssrc()); 464 EXPECT_EQ(1, parser.report_block()->num_packets()); 465 EXPECT_EQ(1, parser.fir()->num_packets()); 466 } 467 468 TEST(RtcpPacketTest, AppendPacketOnEmpty) { 469 Empty empty; 470 ReceiverReport rr; 471 rr.From(kSenderSsrc); 472 empty.Append(&rr); 473 474 RawPacket packet = empty.Build(); 475 RtcpPacketParser parser; 476 parser.Parse(packet.buffer(), packet.buffer_length()); 477 EXPECT_EQ(1, parser.receiver_report()->num_packets()); 478 EXPECT_EQ(0, parser.report_block()->num_packets()); 479 } 480 481 TEST(RtcpPacketTest, AppendPacketWithOwnAppendedPacket) { 482 Fir fir; 483 Bye bye; 484 ReportBlock rb; 485 486 ReceiverReport rr; 487 rr.WithReportBlock(&rb); 488 rr.Append(&fir); 489 490 SenderReport sr; 491 sr.Append(&bye); 492 sr.Append(&rr); 493 494 RawPacket packet = sr.Build(); 495 RtcpPacketParser parser; 496 parser.Parse(packet.buffer(), packet.buffer_length()); 497 EXPECT_EQ(1, parser.sender_report()->num_packets()); 498 EXPECT_EQ(1, parser.receiver_report()->num_packets()); 499 EXPECT_EQ(1, parser.report_block()->num_packets()); 500 EXPECT_EQ(1, parser.bye()->num_packets()); 501 EXPECT_EQ(1, parser.fir()->num_packets()); 502 } 503 504 TEST(RtcpPacketTest, Bye) { 505 Bye bye; 506 bye.From(kSenderSsrc); 507 508 RawPacket packet = bye.Build(); 509 RtcpPacketParser parser; 510 parser.Parse(packet.buffer(), packet.buffer_length()); 511 EXPECT_EQ(1, parser.bye()->num_packets()); 512 EXPECT_EQ(kSenderSsrc, parser.bye()->Ssrc()); 513 } 514 515 TEST(RtcpPacketTest, ByeWithCsrcs) { 516 Fir fir; 517 Bye bye; 518 bye.From(kSenderSsrc); 519 bye.WithCsrc(0x22222222); 520 bye.WithCsrc(0x33333333); 521 bye.Append(&fir); 522 523 RawPacket packet = bye.Build(); 524 RtcpPacketParser parser; 525 parser.Parse(packet.buffer(), packet.buffer_length()); 526 EXPECT_EQ(1, parser.bye()->num_packets()); 527 EXPECT_EQ(kSenderSsrc, parser.bye()->Ssrc()); 528 EXPECT_EQ(1, parser.fir()->num_packets()); 529 } 530 531 TEST(RtcpPacketTest, BuildWithInputBuffer) { 532 Fir fir; 533 ReportBlock rb; 534 ReceiverReport rr; 535 rr.From(kSenderSsrc); 536 rr.WithReportBlock(&rb); 537 rr.Append(&fir); 538 539 const size_t kRrLength = 8; 540 const size_t kReportBlockLength = 24; 541 const size_t kFirLength = 20; 542 543 size_t len = 0; 544 uint8_t packet[kRrLength + kReportBlockLength + kFirLength]; 545 rr.Build(packet, &len, kRrLength + kReportBlockLength + kFirLength); 546 547 RtcpPacketParser parser; 548 parser.Parse(packet, len); 549 EXPECT_EQ(1, parser.receiver_report()->num_packets()); 550 EXPECT_EQ(1, parser.report_block()->num_packets()); 551 EXPECT_EQ(1, parser.fir()->num_packets()); 552 } 553 554 TEST(RtcpPacketTest, BuildWithTooSmallBuffer) { 555 ReportBlock rb; 556 ReceiverReport rr; 557 rr.From(kSenderSsrc); 558 rr.WithReportBlock(&rb); 559 560 const size_t kRrLength = 8; 561 const size_t kReportBlockLength = 24; 562 563 // No packet. 564 size_t len = 0; 565 uint8_t packet[kRrLength + kReportBlockLength - 1]; 566 rr.Build(packet, &len, kRrLength + kReportBlockLength - 1); 567 RtcpPacketParser parser; 568 parser.Parse(packet, len); 569 EXPECT_EQ(0U, len); 570 } 571 572 TEST(RtcpPacketTest, BuildWithTooSmallBuffer_LastBlockFits) { 573 Fir fir; 574 ReportBlock rb; 575 ReceiverReport rr; 576 rr.From(kSenderSsrc); 577 rr.WithReportBlock(&rb); 578 rr.Append(&fir); 579 580 const size_t kRrLength = 8; 581 const size_t kReportBlockLength = 24; 582 583 size_t len = 0; 584 uint8_t packet[kRrLength + kReportBlockLength - 1]; 585 rr.Build(packet, &len, kRrLength + kReportBlockLength - 1); 586 RtcpPacketParser parser; 587 parser.Parse(packet, len); 588 EXPECT_EQ(0, parser.receiver_report()->num_packets()); 589 EXPECT_EQ(0, parser.report_block()->num_packets()); 590 EXPECT_EQ(1, parser.fir()->num_packets()); 591 } 592 } // namespace webrtc 593