1 /* 2 * Copyright (c) 2011 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 11 #include <string.h> 12 13 #include <list> 14 15 #include "testing/gtest/include/gtest/gtest.h" 16 #include "webrtc/modules/video_coding/main/source/frame_buffer.h" 17 #include "webrtc/modules/video_coding/main/source/jitter_buffer.h" 18 #include "webrtc/modules/video_coding/main/source/media_opt_util.h" 19 #include "webrtc/modules/video_coding/main/source/packet.h" 20 #include "webrtc/modules/video_coding/main/source/test/stream_generator.h" 21 #include "webrtc/modules/video_coding/main/test/test_util.h" 22 #include "webrtc/system_wrappers/interface/clock.h" 23 24 namespace webrtc { 25 26 class TestBasicJitterBuffer : public ::testing::Test { 27 protected: 28 virtual void SetUp() { 29 clock_.reset(new SimulatedClock(0)); 30 jitter_buffer_.reset( 31 new VCMJitterBuffer(clock_.get(), &event_factory_)); 32 jitter_buffer_->Start(); 33 seq_num_ = 1234; 34 timestamp_ = 0; 35 size_ = 1400; 36 // Data vector - 0, 0, 0x80, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0x80, 3.... 37 data_[0] = 0; 38 data_[1] = 0; 39 data_[2] = 0x80; 40 int count = 3; 41 for (unsigned int i = 3; i < sizeof(data_) - 3; ++i) { 42 data_[i] = count; 43 count++; 44 if (count == 10) { 45 data_[i + 1] = 0; 46 data_[i + 2] = 0; 47 data_[i + 3] = 0x80; 48 count = 3; 49 i += 3; 50 } 51 } 52 packet_.reset(new VCMPacket(data_, size_, seq_num_, timestamp_, true)); 53 } 54 55 VCMEncodedFrame* DecodeCompleteFrame() { 56 uint32_t timestamp = 0; 57 bool found_frame = jitter_buffer_->NextCompleteTimestamp(10, ×tamp); 58 if (!found_frame) 59 return NULL; 60 VCMEncodedFrame* frame = jitter_buffer_->ExtractAndSetDecode(timestamp); 61 return frame; 62 } 63 64 VCMEncodedFrame* DecodeIncompleteFrame() { 65 uint32_t timestamp = 0; 66 bool found_frame = jitter_buffer_->NextMaybeIncompleteTimestamp(×tamp); 67 if (!found_frame) 68 return NULL; 69 VCMEncodedFrame* frame = jitter_buffer_->ExtractAndSetDecode(timestamp); 70 return frame; 71 } 72 73 void CheckOutFrame(VCMEncodedFrame* frame_out, 74 unsigned int size, 75 bool startCode) { 76 ASSERT_TRUE(frame_out); 77 78 const uint8_t* outData = frame_out->Buffer(); 79 unsigned int i = 0; 80 81 if (startCode) { 82 EXPECT_EQ(0, outData[0]); 83 EXPECT_EQ(0, outData[1]); 84 EXPECT_EQ(0, outData[2]); 85 EXPECT_EQ(1, outData[3]); 86 i += 4; 87 } 88 89 EXPECT_EQ(size, frame_out->Length()); 90 int count = 3; 91 for (; i < size; i++) { 92 if (outData[i] == 0 && outData[i + 1] == 0 && outData[i + 2] == 0x80) { 93 i += 2; 94 } else if (startCode && outData[i] == 0 && outData[i + 1] == 0) { 95 EXPECT_EQ(0, outData[0]); 96 EXPECT_EQ(0, outData[1]); 97 EXPECT_EQ(0, outData[2]); 98 EXPECT_EQ(1, outData[3]); 99 i += 3; 100 } else { 101 EXPECT_EQ(count, outData[i]); 102 count++; 103 if (count == 10) { 104 count = 3; 105 } 106 } 107 } 108 } 109 110 uint16_t seq_num_; 111 uint32_t timestamp_; 112 int size_; 113 uint8_t data_[1500]; 114 scoped_ptr<VCMJitterBuffer> jitter_buffer_; 115 scoped_ptr<VCMPacket> packet_; 116 scoped_ptr<SimulatedClock> clock_; 117 NullEventFactory event_factory_; 118 }; 119 120 121 class TestRunningJitterBuffer : public ::testing::Test { 122 protected: 123 enum { kDataBufferSize = 10 }; 124 125 virtual void SetUp() { 126 clock_.reset(new SimulatedClock(0)); 127 max_nack_list_size_ = 150; 128 oldest_packet_to_nack_ = 250; 129 jitter_buffer_ = new VCMJitterBuffer(clock_.get(), &event_factory_); 130 stream_generator_ = new StreamGenerator(0, 0, clock_->TimeInMilliseconds()); 131 jitter_buffer_->Start(); 132 jitter_buffer_->SetNackSettings(max_nack_list_size_, 133 oldest_packet_to_nack_, 0); 134 memset(data_buffer_, 0, kDataBufferSize); 135 } 136 137 virtual void TearDown() { 138 jitter_buffer_->Stop(); 139 delete stream_generator_; 140 delete jitter_buffer_; 141 } 142 143 VCMFrameBufferEnum InsertPacketAndPop(int index) { 144 VCMPacket packet; 145 packet.dataPtr = data_buffer_; 146 bool packet_available = stream_generator_->PopPacket(&packet, index); 147 EXPECT_TRUE(packet_available); 148 if (!packet_available) 149 return kGeneralError; // Return here to avoid crashes below. 150 bool retransmitted = false; 151 return jitter_buffer_->InsertPacket(packet, &retransmitted); 152 } 153 154 VCMFrameBufferEnum InsertPacket(int index) { 155 VCMPacket packet; 156 packet.dataPtr = data_buffer_; 157 bool packet_available = stream_generator_->GetPacket(&packet, index); 158 EXPECT_TRUE(packet_available); 159 if (!packet_available) 160 return kGeneralError; // Return here to avoid crashes below. 161 bool retransmitted = false; 162 return jitter_buffer_->InsertPacket(packet, &retransmitted); 163 } 164 165 VCMFrameBufferEnum InsertFrame(FrameType frame_type) { 166 stream_generator_->GenerateFrame(frame_type, 167 (frame_type != kFrameEmpty) ? 1 : 0, 168 (frame_type == kFrameEmpty) ? 1 : 0, 169 clock_->TimeInMilliseconds()); 170 VCMFrameBufferEnum ret = InsertPacketAndPop(0); 171 clock_->AdvanceTimeMilliseconds(kDefaultFramePeriodMs); 172 return ret; 173 } 174 175 VCMFrameBufferEnum InsertFrames(int num_frames, FrameType frame_type) { 176 VCMFrameBufferEnum ret_for_all = kNoError; 177 for (int i = 0; i < num_frames; ++i) { 178 VCMFrameBufferEnum ret = InsertFrame(frame_type); 179 if (ret < kNoError) { 180 ret_for_all = ret; 181 } else if (ret_for_all >= kNoError) { 182 ret_for_all = ret; 183 } 184 } 185 return ret_for_all; 186 } 187 188 void DropFrame(int num_packets) { 189 stream_generator_->GenerateFrame(kVideoFrameDelta, num_packets, 0, 190 clock_->TimeInMilliseconds()); 191 for (int i = 0; i < num_packets; ++i) 192 stream_generator_->DropLastPacket(); 193 clock_->AdvanceTimeMilliseconds(kDefaultFramePeriodMs); 194 } 195 196 bool DecodeCompleteFrame() { 197 uint32_t timestamp = 0; 198 bool found_frame = jitter_buffer_->NextCompleteTimestamp(0, ×tamp); 199 if (!found_frame) 200 return false; 201 202 VCMEncodedFrame* frame = jitter_buffer_->ExtractAndSetDecode(timestamp); 203 bool ret = (frame != NULL); 204 jitter_buffer_->ReleaseFrame(frame); 205 return ret; 206 } 207 208 bool DecodeIncompleteFrame() { 209 uint32_t timestamp = 0; 210 bool found_frame = jitter_buffer_->NextMaybeIncompleteTimestamp(×tamp); 211 if (!found_frame) 212 return false; 213 VCMEncodedFrame* frame = jitter_buffer_->ExtractAndSetDecode(timestamp); 214 bool ret = (frame != NULL); 215 jitter_buffer_->ReleaseFrame(frame); 216 return ret; 217 } 218 219 VCMJitterBuffer* jitter_buffer_; 220 StreamGenerator* stream_generator_; 221 scoped_ptr<SimulatedClock> clock_; 222 NullEventFactory event_factory_; 223 size_t max_nack_list_size_; 224 int oldest_packet_to_nack_; 225 uint8_t data_buffer_[kDataBufferSize]; 226 }; 227 228 class TestJitterBufferNack : public TestRunningJitterBuffer { 229 protected: 230 virtual void SetUp() { 231 TestRunningJitterBuffer::SetUp(); 232 jitter_buffer_->SetNackMode(kNack, -1, -1); 233 } 234 235 virtual void TearDown() { 236 TestRunningJitterBuffer::TearDown(); 237 } 238 }; 239 240 TEST_F(TestBasicJitterBuffer, StopRunning) { 241 jitter_buffer_->Stop(); 242 EXPECT_TRUE(NULL == DecodeCompleteFrame()); 243 EXPECT_TRUE(NULL == DecodeIncompleteFrame()); 244 jitter_buffer_->Start(); 245 // Allow selective errors. 246 jitter_buffer_->SetDecodeErrorMode(kSelectiveErrors); 247 248 // No packets inserted. 249 EXPECT_TRUE(NULL == DecodeCompleteFrame()); 250 EXPECT_TRUE(NULL == DecodeIncompleteFrame()); 251 252 // Allow decoding with errors. 253 jitter_buffer_->SetDecodeErrorMode(kWithErrors); 254 255 // No packets inserted. 256 EXPECT_TRUE(NULL == DecodeCompleteFrame()); 257 EXPECT_TRUE(NULL == DecodeIncompleteFrame()); 258 } 259 260 TEST_F(TestBasicJitterBuffer, SinglePacketFrame) { 261 // Always start with a complete key frame when not allowing errors. 262 jitter_buffer_->SetDecodeErrorMode(kNoErrors); 263 packet_->frameType = kVideoFrameKey; 264 packet_->isFirstPacket = true; 265 packet_->markerBit = true; 266 packet_->timestamp += 123 * 90; 267 268 // Insert the packet to the jitter buffer and get a frame. 269 bool retransmitted = false; 270 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 271 &retransmitted)); 272 VCMEncodedFrame* frame_out = DecodeCompleteFrame(); 273 CheckOutFrame(frame_out, size_, false); 274 EXPECT_EQ(kVideoFrameKey, frame_out->FrameType()); 275 } 276 277 TEST_F(TestBasicJitterBuffer, DualPacketFrame) { 278 packet_->frameType = kVideoFrameKey; 279 packet_->isFirstPacket = true; 280 packet_->markerBit = false; 281 282 bool retransmitted = false; 283 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 284 &retransmitted)); 285 VCMEncodedFrame* frame_out = DecodeCompleteFrame(); 286 // Should not be complete. 287 EXPECT_TRUE(frame_out == NULL); 288 289 ++seq_num_; 290 packet_->isFirstPacket = false; 291 packet_->markerBit = true; 292 packet_->seqNum = seq_num_; 293 294 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 295 &retransmitted)); 296 297 frame_out = DecodeCompleteFrame(); 298 CheckOutFrame(frame_out, 2 * size_, false); 299 300 EXPECT_EQ(kVideoFrameKey, frame_out->FrameType()); 301 } 302 303 TEST_F(TestBasicJitterBuffer, 100PacketKeyFrame) { 304 packet_->frameType = kVideoFrameKey; 305 packet_->isFirstPacket = true; 306 packet_->markerBit = false; 307 308 bool retransmitted = false; 309 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 310 &retransmitted)); 311 312 VCMEncodedFrame* frame_out = DecodeCompleteFrame(); 313 314 // Frame should not be complete. 315 EXPECT_TRUE(frame_out == NULL); 316 317 // Insert 98 frames. 318 int loop = 0; 319 do { 320 seq_num_++; 321 packet_->isFirstPacket = false; 322 packet_->markerBit = false; 323 packet_->seqNum = seq_num_; 324 325 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 326 &retransmitted)); 327 loop++; 328 } while (loop < 98); 329 330 // Insert last packet. 331 ++seq_num_; 332 packet_->isFirstPacket = false; 333 packet_->markerBit = true; 334 packet_->seqNum = seq_num_; 335 336 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 337 &retransmitted)); 338 339 frame_out = DecodeCompleteFrame(); 340 341 CheckOutFrame(frame_out, 100 * size_, false); 342 EXPECT_EQ(kVideoFrameKey, frame_out->FrameType()); 343 } 344 345 TEST_F(TestBasicJitterBuffer, 100PacketDeltaFrame) { 346 // Always start with a complete key frame. 347 packet_->frameType = kVideoFrameKey; 348 packet_->isFirstPacket = true; 349 packet_->markerBit = true; 350 351 bool retransmitted = false; 352 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 353 &retransmitted)); 354 VCMEncodedFrame* frame_out = DecodeCompleteFrame(); 355 EXPECT_FALSE(frame_out == NULL); 356 357 ++seq_num_; 358 packet_->seqNum = seq_num_; 359 packet_->markerBit = false; 360 packet_->frameType = kVideoFrameDelta; 361 packet_->timestamp += 33 * 90; 362 363 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 364 &retransmitted)); 365 366 frame_out = DecodeCompleteFrame(); 367 368 // Frame should not be complete. 369 EXPECT_TRUE(frame_out == NULL); 370 371 packet_->isFirstPacket = false; 372 // Insert 98 frames. 373 int loop = 0; 374 do { 375 ++seq_num_; 376 packet_->seqNum = seq_num_; 377 378 // Insert a packet into a frame. 379 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 380 &retransmitted)); 381 loop++; 382 } while (loop < 98); 383 384 // Insert the last packet. 385 ++seq_num_; 386 packet_->isFirstPacket = false; 387 packet_->markerBit = true; 388 packet_->seqNum = seq_num_; 389 390 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 391 &retransmitted)); 392 393 frame_out = DecodeCompleteFrame(); 394 395 CheckOutFrame(frame_out, 100 * size_, false); 396 EXPECT_EQ(kVideoFrameDelta, frame_out->FrameType()); 397 } 398 399 TEST_F(TestBasicJitterBuffer, PacketReorderingReverseOrder) { 400 // Insert the "first" packet last. 401 seq_num_ += 100; 402 packet_->frameType = kVideoFrameKey; 403 packet_->isFirstPacket = false; 404 packet_->markerBit = true; 405 packet_->seqNum = seq_num_; 406 packet_->timestamp = timestamp_; 407 408 bool retransmitted = false; 409 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 410 &retransmitted)); 411 412 VCMEncodedFrame* frame_out = DecodeCompleteFrame(); 413 414 EXPECT_TRUE(frame_out == NULL); 415 416 // Insert 98 packets. 417 int loop = 0; 418 do { 419 seq_num_--; 420 packet_->isFirstPacket = false; 421 packet_->markerBit = false; 422 packet_->seqNum = seq_num_; 423 424 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 425 &retransmitted)); 426 loop++; 427 } while (loop < 98); 428 429 // Insert the last packet. 430 seq_num_--; 431 packet_->isFirstPacket = true; 432 packet_->markerBit = false; 433 packet_->seqNum = seq_num_; 434 435 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 436 &retransmitted)); 437 438 frame_out = DecodeCompleteFrame();; 439 440 CheckOutFrame(frame_out, 100 * size_, false); 441 442 EXPECT_EQ(kVideoFrameKey, frame_out->FrameType()); 443 } 444 445 TEST_F(TestBasicJitterBuffer, FrameReordering2Frames2PacketsEach) { 446 packet_->frameType = kVideoFrameDelta; 447 packet_->isFirstPacket = true; 448 packet_->markerBit = false; 449 450 bool retransmitted = false; 451 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 452 &retransmitted)); 453 454 VCMEncodedFrame* frame_out = DecodeCompleteFrame(); 455 456 EXPECT_TRUE(frame_out == NULL); 457 458 seq_num_++; 459 packet_->isFirstPacket = false; 460 packet_->markerBit = true; 461 packet_->seqNum = seq_num_; 462 463 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 464 &retransmitted)); 465 466 // check that we fail to get frame since seqnum is not continuous 467 frame_out = DecodeCompleteFrame(); 468 EXPECT_TRUE(frame_out == NULL); 469 470 seq_num_ -= 3; 471 timestamp_ -= 33*90; 472 packet_->frameType = kVideoFrameKey; 473 packet_->isFirstPacket = true; 474 packet_->markerBit = false; 475 packet_->seqNum = seq_num_; 476 packet_->timestamp = timestamp_; 477 478 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 479 &retransmitted)); 480 481 frame_out = DecodeCompleteFrame(); 482 483 // It should not be complete. 484 EXPECT_TRUE(frame_out == NULL); 485 486 seq_num_++; 487 packet_->isFirstPacket = false; 488 packet_->markerBit = true; 489 packet_->seqNum = seq_num_; 490 491 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 492 &retransmitted)); 493 494 frame_out = DecodeCompleteFrame(); 495 496 CheckOutFrame(frame_out, 2 * size_, false); 497 498 EXPECT_EQ(kVideoFrameKey, frame_out->FrameType()); 499 500 jitter_buffer_->ReleaseFrame(frame_out); 501 502 frame_out = DecodeCompleteFrame(); 503 504 CheckOutFrame(frame_out, 2 * size_, false); 505 506 EXPECT_EQ(kVideoFrameDelta, frame_out->FrameType()); 507 } 508 509 TEST_F(TestBasicJitterBuffer, DuplicatePackets) { 510 packet_->frameType = kVideoFrameKey; 511 packet_->isFirstPacket = true; 512 packet_->markerBit = false; 513 packet_->seqNum = seq_num_; 514 packet_->timestamp = timestamp_; 515 516 bool retransmitted = false; 517 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 518 &retransmitted)); 519 520 VCMEncodedFrame* frame_out = DecodeCompleteFrame(); 521 522 EXPECT_TRUE(frame_out == NULL); 523 524 packet_->isFirstPacket = false; 525 packet_->markerBit = true; 526 527 // Insert a packet into a frame. 528 EXPECT_EQ(kDuplicatePacket, jitter_buffer_->InsertPacket(*packet_, 529 &retransmitted)); 530 531 seq_num_++; 532 packet_->seqNum = seq_num_; 533 534 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 535 &retransmitted)); 536 537 frame_out = DecodeCompleteFrame(); 538 539 CheckOutFrame(frame_out, 2 * size_, false); 540 541 EXPECT_EQ(kVideoFrameKey, frame_out->FrameType()); 542 } 543 544 TEST_F(TestBasicJitterBuffer, H264InsertStartCode) { 545 packet_->frameType = kVideoFrameKey; 546 packet_->isFirstPacket = true; 547 packet_->markerBit = false; 548 packet_->seqNum = seq_num_; 549 packet_->timestamp = timestamp_; 550 packet_->insertStartCode = true; 551 552 bool retransmitted = false; 553 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 554 &retransmitted)); 555 556 VCMEncodedFrame* frame_out = DecodeCompleteFrame(); 557 558 // Frame should not be complete. 559 EXPECT_TRUE(frame_out == NULL); 560 561 seq_num_++; 562 packet_->isFirstPacket = false; 563 packet_->markerBit = true; 564 packet_->seqNum = seq_num_; 565 566 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 567 &retransmitted)); 568 569 frame_out = DecodeCompleteFrame(); 570 CheckOutFrame(frame_out, size_ * 2 + 4 * 2, true); 571 EXPECT_EQ(kVideoFrameKey, frame_out->FrameType()); 572 } 573 574 // Test threshold conditions of decodable state. 575 TEST_F(TestBasicJitterBuffer, PacketLossWithSelectiveErrorsThresholdCheck) { 576 jitter_buffer_->SetDecodeErrorMode(kSelectiveErrors); 577 // Always start with a key frame. Use 10 packets to test Decodable State 578 // boundaries. 579 packet_->frameType = kVideoFrameKey; 580 packet_->isFirstPacket = true; 581 packet_->markerBit = false; 582 packet_->seqNum = seq_num_; 583 packet_->timestamp = timestamp_; 584 585 bool retransmitted = false; 586 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 587 &retransmitted)); 588 uint32_t timestamp = 0; 589 EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, ×tamp)); 590 EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(×tamp)); 591 592 packet_->isFirstPacket = false; 593 for (int i = 1; i < 9; ++i) { 594 packet_->seqNum++; 595 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 596 &retransmitted)); 597 EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, ×tamp)); 598 EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(×tamp)); 599 } 600 601 // last packet 602 packet_->markerBit = true; 603 packet_->seqNum++; 604 605 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 606 &retransmitted)); 607 VCMEncodedFrame* frame_out = DecodeCompleteFrame(); 608 CheckOutFrame(frame_out, 10 * size_, false); 609 EXPECT_EQ(kVideoFrameKey, frame_out->FrameType()); 610 611 // An incomplete frame can only be decoded once a subsequent frame has begun 612 // to arrive. Insert packet in distant frame for this purpose. 613 packet_->frameType = kVideoFrameDelta; 614 packet_->isFirstPacket = true; 615 packet_->markerBit = false; 616 packet_->seqNum += 100; 617 packet_->timestamp += 33 * 90 * 8; 618 619 EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_, 620 &retransmitted)); 621 EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, ×tamp)); 622 EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(×tamp)); 623 624 // Insert second frame 625 packet_->seqNum -= 99; 626 packet_->timestamp -= 33 * 90 * 7; 627 628 EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_, 629 &retransmitted)); 630 EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, ×tamp)); 631 EXPECT_TRUE(jitter_buffer_->NextMaybeIncompleteTimestamp(×tamp)); 632 633 packet_->isFirstPacket = false; 634 for (int i = 1; i < 8; ++i) { 635 packet_->seqNum++; 636 EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_, 637 &retransmitted)); 638 EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, ×tamp)); 639 EXPECT_TRUE(jitter_buffer_->NextMaybeIncompleteTimestamp(×tamp)); 640 } 641 642 packet_->seqNum++; 643 EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_, 644 &retransmitted)); 645 EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, ×tamp)); 646 EXPECT_TRUE(jitter_buffer_->NextMaybeIncompleteTimestamp(×tamp)); 647 648 frame_out = DecodeIncompleteFrame(); 649 ASSERT_FALSE(NULL == frame_out); 650 CheckOutFrame(frame_out, 9 * size_, false); 651 EXPECT_EQ(kVideoFrameDelta, frame_out->FrameType()); 652 653 packet_->markerBit = true; 654 packet_->seqNum++; 655 EXPECT_EQ(kOldPacket, jitter_buffer_->InsertPacket(*packet_, 656 &retransmitted)); 657 } 658 659 // Make sure first packet is present before a frame can be decoded. 660 TEST_F(TestBasicJitterBuffer, PacketLossWithSelectiveErrorsIncompleteKey) { 661 jitter_buffer_->SetDecodeErrorMode(kSelectiveErrors); 662 // Always start with a key frame. 663 packet_->frameType = kVideoFrameKey; 664 packet_->isFirstPacket = true; 665 packet_->markerBit = true; 666 packet_->seqNum = seq_num_; 667 packet_->timestamp = timestamp_; 668 669 bool retransmitted = false; 670 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 671 &retransmitted)); 672 VCMEncodedFrame* frame_out = DecodeCompleteFrame(); 673 CheckOutFrame(frame_out, size_, false); 674 EXPECT_EQ(kVideoFrameKey, frame_out->FrameType()); 675 676 // An incomplete frame can only be decoded once a subsequent frame has begun 677 // to arrive. Insert packet in distant frame for this purpose. 678 packet_->frameType = kVideoFrameDelta; 679 packet_->isFirstPacket = false; 680 packet_->markerBit = false; 681 packet_->seqNum += 100; 682 packet_->timestamp += 33*90*8; 683 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 684 &retransmitted)); 685 uint32_t timestamp; 686 EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, ×tamp)); 687 EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(×tamp)); 688 689 // Insert second frame - an incomplete key frame. 690 packet_->frameType = kVideoFrameKey; 691 packet_->isFirstPacket = true; 692 packet_->seqNum -= 99; 693 packet_->timestamp -= 33*90*7; 694 695 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 696 &retransmitted)); 697 EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, ×tamp)); 698 EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(×tamp)); 699 700 // Insert a few more packets. Make sure we're waiting for the key frame to be 701 // complete. 702 packet_->isFirstPacket = false; 703 for (int i = 1; i < 5; ++i) { 704 packet_->seqNum++; 705 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 706 &retransmitted)); 707 EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, ×tamp)); 708 EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(×tamp)); 709 } 710 711 // Complete key frame. 712 packet_->markerBit = true; 713 packet_->seqNum++; 714 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 715 &retransmitted)); 716 frame_out = DecodeCompleteFrame(); 717 CheckOutFrame(frame_out, 6 * size_, false); 718 EXPECT_EQ(kVideoFrameKey, frame_out->FrameType()); 719 } 720 721 // Make sure first packet is present before a frame can be decoded. 722 TEST_F(TestBasicJitterBuffer, PacketLossWithSelectiveErrorsMissingFirstPacket) { 723 jitter_buffer_->SetDecodeErrorMode(kSelectiveErrors); 724 // Always start with a key frame. 725 packet_->frameType = kVideoFrameKey; 726 packet_->isFirstPacket = true; 727 packet_->markerBit = true; 728 packet_->seqNum = seq_num_; 729 packet_->timestamp = timestamp_; 730 731 bool retransmitted = false; 732 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 733 &retransmitted)); 734 VCMEncodedFrame* frame_out = DecodeCompleteFrame(); 735 CheckOutFrame(frame_out, size_, false); 736 EXPECT_EQ(kVideoFrameKey, frame_out->FrameType()); 737 738 // An incomplete frame can only be decoded once a subsequent frame has begun 739 // to arrive. Insert packet in distant frame for this purpose. 740 packet_->frameType = kVideoFrameDelta; 741 packet_->isFirstPacket = false; 742 packet_->markerBit = false; 743 packet_->seqNum += 100; 744 packet_->timestamp += 33*90*8; 745 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 746 &retransmitted)); 747 uint32_t timestamp; 748 EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, ×tamp)); 749 EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(×tamp)); 750 751 // Insert second frame with the first packet missing. Make sure we're waiting 752 // for the key frame to be complete. 753 packet_->seqNum -= 98; 754 packet_->timestamp -= 33*90*7; 755 756 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 757 &retransmitted)); 758 EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, ×tamp)); 759 EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(×tamp)); 760 761 for (int i = 0; i < 5; ++i) { 762 packet_->seqNum++; 763 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 764 &retransmitted)); 765 EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, ×tamp)); 766 EXPECT_FALSE(jitter_buffer_->NextMaybeIncompleteTimestamp(×tamp)); 767 } 768 769 // Add first packet. Frame should now be decodable, but incomplete. 770 packet_->isFirstPacket = true; 771 packet_->seqNum -= 6; 772 EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_, 773 &retransmitted)); 774 EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, ×tamp)); 775 EXPECT_TRUE(jitter_buffer_->NextMaybeIncompleteTimestamp(×tamp)); 776 777 frame_out = DecodeIncompleteFrame(); 778 CheckOutFrame(frame_out, 7 * size_, false); 779 EXPECT_EQ(kVideoFrameDelta, frame_out->FrameType()); 780 } 781 782 TEST_F(TestBasicJitterBuffer, DiscontinuousStreamWhenDecodingWithErrors) { 783 // Will use one packet per frame. 784 jitter_buffer_->SetDecodeErrorMode(kWithErrors); 785 packet_->frameType = kVideoFrameKey; 786 packet_->isFirstPacket = true; 787 packet_->markerBit = true; 788 packet_->seqNum = seq_num_; 789 packet_->timestamp = timestamp_; 790 bool retransmitted = false; 791 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 792 &retransmitted)); 793 uint32_t next_timestamp; 794 EXPECT_TRUE(jitter_buffer_->NextCompleteTimestamp(0, &next_timestamp)); 795 EXPECT_EQ(packet_->timestamp, next_timestamp); 796 VCMEncodedFrame* frame = jitter_buffer_->ExtractAndSetDecode(next_timestamp); 797 EXPECT_TRUE(frame != NULL); 798 jitter_buffer_->ReleaseFrame(frame); 799 800 // Drop a complete frame. 801 timestamp_ += 2 * 33 * 90; 802 seq_num_ += 2; 803 packet_->frameType = kVideoFrameDelta; 804 packet_->isFirstPacket = true; 805 packet_->markerBit = false; 806 packet_->seqNum = seq_num_; 807 packet_->timestamp = timestamp_; 808 EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_, 809 &retransmitted)); 810 // Insert a packet (so the previous one will be released). 811 timestamp_ += 33 * 90; 812 seq_num_ += 2; 813 packet_->frameType = kVideoFrameDelta; 814 packet_->isFirstPacket = true; 815 packet_->markerBit = false; 816 packet_->seqNum = seq_num_; 817 packet_->timestamp = timestamp_; 818 EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_, 819 &retransmitted)); 820 EXPECT_FALSE(jitter_buffer_->NextCompleteTimestamp(0, &next_timestamp)); 821 EXPECT_TRUE(jitter_buffer_->NextMaybeIncompleteTimestamp(&next_timestamp)); 822 EXPECT_EQ(packet_->timestamp - 33 * 90, next_timestamp); 823 } 824 825 TEST_F(TestBasicJitterBuffer, PacketLoss) { 826 // Verify missing packets statistics and not decodable packets statistics. 827 // Insert 10 frames consisting of 4 packets and remove one from all of them. 828 // The last packet is an empty (non-media) packet. 829 830 // Select a start seqNum which triggers a difficult wrap situation 831 // The JB will only output (incomplete)frames if the next one has started 832 // to arrive. Start by inserting one frame (key). 833 jitter_buffer_->SetDecodeErrorMode(kWithErrors); 834 seq_num_ = 0xffff - 4; 835 seq_num_++; 836 packet_->frameType = kVideoFrameKey; 837 packet_->isFirstPacket = true; 838 packet_->markerBit = false; 839 packet_->seqNum = seq_num_; 840 packet_->timestamp = timestamp_; 841 packet_->completeNALU = kNaluStart; 842 843 bool retransmitted = false; 844 EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_, 845 &retransmitted)); 846 for (int i = 0; i < 11; ++i) { 847 webrtc::FrameType frametype = kVideoFrameDelta; 848 seq_num_++; 849 timestamp_ += 33*90; 850 packet_->frameType = frametype; 851 packet_->isFirstPacket = true; 852 packet_->markerBit = false; 853 packet_->seqNum = seq_num_; 854 packet_->timestamp = timestamp_; 855 packet_->completeNALU = kNaluStart; 856 857 EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_, 858 &retransmitted)); 859 860 VCMEncodedFrame* frame_out = DecodeCompleteFrame(); 861 862 // Should not be complete. 863 EXPECT_TRUE(frame_out == NULL); 864 865 seq_num_ += 2; 866 packet_->isFirstPacket = false; 867 packet_->markerBit = true; 868 packet_->seqNum = seq_num_; 869 packet_->completeNALU = kNaluEnd; 870 871 EXPECT_EQ(jitter_buffer_->InsertPacket(*packet_, &retransmitted), 872 kDecodableSession); 873 874 // Insert an empty (non-media) packet. 875 seq_num_++; 876 packet_->isFirstPacket = false; 877 packet_->markerBit = false; 878 packet_->seqNum = seq_num_; 879 packet_->completeNALU = kNaluEnd; 880 packet_->frameType = kFrameEmpty; 881 882 EXPECT_EQ(jitter_buffer_->InsertPacket(*packet_, &retransmitted), 883 kDecodableSession); 884 frame_out = DecodeIncompleteFrame(); 885 886 // One of the packets has been discarded by the jitter buffer. 887 // Last frame can't be extracted yet. 888 if (i < 10) { 889 CheckOutFrame(frame_out, size_, false); 890 891 if (i == 0) { 892 EXPECT_EQ(kVideoFrameKey, frame_out->FrameType()); 893 } else { 894 EXPECT_EQ(frametype, frame_out->FrameType()); 895 } 896 EXPECT_FALSE(frame_out->Complete()); 897 EXPECT_FALSE(frame_out->MissingFrame()); 898 } 899 900 jitter_buffer_->ReleaseFrame(frame_out); 901 } 902 903 // Insert 3 old packets and verify that we have 3 discarded packets 904 // Match value to actual latest timestamp decoded. 905 timestamp_ -= 33 * 90; 906 packet_->timestamp = timestamp_ - 1000; 907 908 EXPECT_EQ(kOldPacket, jitter_buffer_->InsertPacket(*packet_, 909 &retransmitted)); 910 911 packet_->timestamp = timestamp_ - 500; 912 913 EXPECT_EQ(kOldPacket, jitter_buffer_->InsertPacket(*packet_, 914 &retransmitted)); 915 916 packet_->timestamp = timestamp_ - 100; 917 918 EXPECT_EQ(kOldPacket, jitter_buffer_->InsertPacket(*packet_, 919 &retransmitted)); 920 921 EXPECT_EQ(3, jitter_buffer_->num_discarded_packets()); 922 923 jitter_buffer_->Flush(); 924 925 // This statistic shouldn't be reset by a flush. 926 EXPECT_EQ(3, jitter_buffer_->num_discarded_packets()); 927 } 928 929 TEST_F(TestBasicJitterBuffer, DeltaFrame100PacketsWithSeqNumWrap) { 930 seq_num_ = 0xfff0; 931 packet_->frameType = kVideoFrameKey; 932 packet_->isFirstPacket = true; 933 packet_->markerBit = false; 934 packet_->seqNum = seq_num_; 935 packet_->timestamp = timestamp_; 936 937 bool retransmitted = false; 938 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 939 &retransmitted)); 940 941 VCMEncodedFrame* frame_out = DecodeCompleteFrame(); 942 943 EXPECT_TRUE(frame_out == NULL); 944 945 int loop = 0; 946 do { 947 seq_num_++; 948 packet_->isFirstPacket = false; 949 packet_->markerBit = false; 950 packet_->seqNum = seq_num_; 951 952 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 953 &retransmitted)); 954 955 frame_out = DecodeCompleteFrame(); 956 957 EXPECT_TRUE(frame_out == NULL); 958 959 loop++; 960 } while (loop < 98); 961 962 seq_num_++; 963 packet_->isFirstPacket = false; 964 packet_->markerBit = true; 965 packet_->seqNum = seq_num_; 966 967 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 968 &retransmitted)); 969 970 frame_out = DecodeCompleteFrame(); 971 972 CheckOutFrame(frame_out, 100 * size_, false); 973 974 EXPECT_EQ(kVideoFrameKey, frame_out->FrameType()); 975 } 976 977 TEST_F(TestBasicJitterBuffer, PacketReorderingReverseWithNegSeqNumWrap) { 978 // Insert "first" packet last seqnum. 979 seq_num_ = 10; 980 packet_->frameType = kVideoFrameKey; 981 packet_->isFirstPacket = false; 982 packet_->markerBit = true; 983 packet_->seqNum = seq_num_; 984 985 bool retransmitted = false; 986 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 987 &retransmitted)); 988 VCMEncodedFrame* frame_out = DecodeCompleteFrame(); 989 990 // Should not be complete. 991 EXPECT_TRUE(frame_out == NULL); 992 993 // Insert 98 frames. 994 int loop = 0; 995 do { 996 seq_num_--; 997 packet_->isFirstPacket = false; 998 packet_->markerBit = false; 999 packet_->seqNum = seq_num_; 1000 1001 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 1002 &retransmitted)); 1003 1004 frame_out = DecodeCompleteFrame(); 1005 1006 EXPECT_TRUE(frame_out == NULL); 1007 1008 loop++; 1009 } while (loop < 98); 1010 1011 // Insert last packet. 1012 seq_num_--; 1013 packet_->isFirstPacket = true; 1014 packet_->markerBit = false; 1015 packet_->seqNum = seq_num_; 1016 1017 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 1018 &retransmitted)); 1019 1020 frame_out = DecodeCompleteFrame(); 1021 CheckOutFrame(frame_out, 100 * size_, false); 1022 EXPECT_EQ(kVideoFrameKey, frame_out->FrameType()); 1023 } 1024 1025 TEST_F(TestBasicJitterBuffer, TestInsertOldFrame) { 1026 // ------- ------- 1027 // | 2 | | 1 | 1028 // ------- ------- 1029 // t = 3000 t = 2000 1030 seq_num_ = 2; 1031 timestamp_ = 3000; 1032 packet_->frameType = kVideoFrameKey; 1033 packet_->isFirstPacket = true; 1034 packet_->markerBit = true; 1035 packet_->timestamp = timestamp_; 1036 packet_->seqNum = seq_num_; 1037 1038 bool retransmitted = false; 1039 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 1040 &retransmitted)); 1041 1042 VCMEncodedFrame* frame_out = DecodeCompleteFrame(); 1043 EXPECT_EQ(3000u, frame_out->TimeStamp()); 1044 1045 CheckOutFrame(frame_out, size_, false); 1046 1047 EXPECT_EQ(kVideoFrameKey, frame_out->FrameType()); 1048 1049 jitter_buffer_->ReleaseFrame(frame_out); 1050 1051 seq_num_--; 1052 timestamp_ = 2000; 1053 packet_->frameType = kVideoFrameDelta; 1054 packet_->isFirstPacket = true; 1055 packet_->markerBit = true; 1056 packet_->seqNum = seq_num_; 1057 packet_->timestamp = timestamp_; 1058 1059 EXPECT_EQ(kOldPacket, jitter_buffer_->InsertPacket(*packet_, 1060 &retransmitted)); 1061 } 1062 1063 TEST_F(TestBasicJitterBuffer, TestInsertOldFrameWithSeqNumWrap) { 1064 // ------- ------- 1065 // | 2 | | 1 | 1066 // ------- ------- 1067 // t = 3000 t = 0xffffff00 1068 1069 seq_num_ = 2; 1070 timestamp_ = 3000; 1071 packet_->frameType = kVideoFrameKey; 1072 packet_->isFirstPacket = true; 1073 packet_->markerBit = true; 1074 packet_->seqNum = seq_num_; 1075 packet_->timestamp = timestamp_; 1076 1077 bool retransmitted = false; 1078 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 1079 &retransmitted)); 1080 1081 VCMEncodedFrame* frame_out = DecodeCompleteFrame(); 1082 EXPECT_EQ(timestamp_, frame_out->TimeStamp()); 1083 1084 CheckOutFrame(frame_out, size_, false); 1085 1086 EXPECT_EQ(kVideoFrameKey, frame_out->FrameType()); 1087 1088 jitter_buffer_->ReleaseFrame(frame_out); 1089 1090 seq_num_--; 1091 timestamp_ = 0xffffff00; 1092 packet_->frameType = kVideoFrameDelta; 1093 packet_->isFirstPacket = true; 1094 packet_->markerBit = true; 1095 packet_->seqNum = seq_num_; 1096 packet_->timestamp = timestamp_; 1097 1098 1099 // This timestamp is old. 1100 EXPECT_EQ(kOldPacket, jitter_buffer_->InsertPacket(*packet_, 1101 &retransmitted)); 1102 } 1103 1104 TEST_F(TestBasicJitterBuffer, TimestampWrap) { 1105 // --------------- --------------- 1106 // | 1 | 2 | | 3 | 4 | 1107 // --------------- --------------- 1108 // t = 0xffffff00 t = 33*90 1109 1110 timestamp_ = 0xffffff00; 1111 packet_->frameType = kVideoFrameKey; 1112 packet_->isFirstPacket = true; 1113 packet_->markerBit = false; 1114 packet_->seqNum = seq_num_; 1115 packet_->timestamp = timestamp_; 1116 1117 bool retransmitted = false; 1118 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 1119 &retransmitted)); 1120 1121 VCMEncodedFrame* frame_out = DecodeCompleteFrame(); 1122 1123 EXPECT_TRUE(frame_out == NULL); 1124 1125 seq_num_++; 1126 packet_->isFirstPacket = false; 1127 packet_->markerBit = true; 1128 packet_->seqNum = seq_num_; 1129 1130 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 1131 &retransmitted)); 1132 1133 frame_out = DecodeCompleteFrame(); 1134 1135 CheckOutFrame(frame_out, 2 * size_, false); 1136 1137 jitter_buffer_->ReleaseFrame(frame_out); 1138 1139 seq_num_++; 1140 timestamp_ += 33*90; 1141 packet_->frameType = kVideoFrameDelta; 1142 packet_->isFirstPacket = true; 1143 packet_->markerBit = false; 1144 packet_->seqNum = seq_num_; 1145 packet_->timestamp = timestamp_; 1146 1147 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 1148 &retransmitted)); 1149 1150 frame_out = DecodeCompleteFrame(); 1151 1152 EXPECT_TRUE(frame_out == NULL); 1153 1154 seq_num_++; 1155 packet_->isFirstPacket = false; 1156 packet_->markerBit = true; 1157 packet_->seqNum = seq_num_; 1158 1159 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 1160 &retransmitted)); 1161 1162 frame_out = DecodeCompleteFrame(); 1163 1164 CheckOutFrame(frame_out, 2 * size_, false); 1165 1166 EXPECT_EQ(kVideoFrameDelta, frame_out->FrameType()); 1167 } 1168 1169 TEST_F(TestBasicJitterBuffer, 2FrameWithTimestampWrap) { 1170 // ------- ------- 1171 // | 1 | | 2 | 1172 // ------- ------- 1173 // t = 0xffffff00 t = 2700 1174 1175 timestamp_ = 0xffffff00; 1176 packet_->frameType = kVideoFrameKey; 1177 packet_->isFirstPacket = true; 1178 packet_->markerBit = true; 1179 packet_->timestamp = timestamp_; 1180 1181 bool retransmitted = false; 1182 // Insert first frame (session will be complete). 1183 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 1184 &retransmitted)); 1185 1186 // Insert next frame. 1187 seq_num_++; 1188 timestamp_ = 2700; 1189 packet_->frameType = kVideoFrameDelta; 1190 packet_->isFirstPacket = true; 1191 packet_->markerBit = true; 1192 packet_->seqNum = seq_num_; 1193 packet_->timestamp = timestamp_; 1194 1195 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 1196 &retransmitted)); 1197 1198 VCMEncodedFrame* frame_out = DecodeCompleteFrame(); 1199 EXPECT_EQ(0xffffff00, frame_out->TimeStamp()); 1200 1201 CheckOutFrame(frame_out, size_, false); 1202 1203 EXPECT_EQ(kVideoFrameKey, frame_out->FrameType()); 1204 1205 VCMEncodedFrame* frame_out2 = DecodeCompleteFrame(); 1206 EXPECT_EQ(2700u, frame_out2->TimeStamp()); 1207 1208 CheckOutFrame(frame_out2, size_, false); 1209 1210 EXPECT_EQ(kVideoFrameDelta, frame_out2->FrameType()); 1211 } 1212 1213 TEST_F(TestBasicJitterBuffer, Insert2FramesReOrderedWithTimestampWrap) { 1214 // ------- ------- 1215 // | 2 | | 1 | 1216 // ------- ------- 1217 // t = 2700 t = 0xffffff00 1218 1219 seq_num_ = 2; 1220 timestamp_ = 2700; 1221 packet_->frameType = kVideoFrameDelta; 1222 packet_->isFirstPacket = true; 1223 packet_->markerBit = true; 1224 packet_->seqNum = seq_num_; 1225 packet_->timestamp = timestamp_; 1226 1227 bool retransmitted = false; 1228 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 1229 &retransmitted)); 1230 1231 // Insert second frame 1232 seq_num_--; 1233 timestamp_ = 0xffffff00; 1234 packet_->frameType = kVideoFrameKey; 1235 packet_->isFirstPacket = true; 1236 packet_->markerBit = true; 1237 packet_->seqNum = seq_num_; 1238 packet_->timestamp = timestamp_; 1239 1240 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 1241 &retransmitted)); 1242 1243 VCMEncodedFrame* frame_out = DecodeCompleteFrame(); 1244 EXPECT_EQ(0xffffff00, frame_out->TimeStamp()); 1245 1246 CheckOutFrame(frame_out, size_, false); 1247 1248 EXPECT_EQ(kVideoFrameKey, frame_out->FrameType()); 1249 1250 VCMEncodedFrame* frame_out2 = DecodeCompleteFrame(); 1251 EXPECT_EQ(2700u, frame_out2->TimeStamp()); 1252 1253 CheckOutFrame(frame_out2, size_, false); 1254 1255 EXPECT_EQ(kVideoFrameDelta, frame_out2->FrameType()); 1256 } 1257 1258 TEST_F(TestBasicJitterBuffer, DeltaFrameWithMoreThanMaxNumberOfPackets) { 1259 int loop = 0; 1260 bool firstPacket = true; 1261 bool retransmitted = false; 1262 // Insert kMaxPacketsInJitterBuffer into frame. 1263 do { 1264 seq_num_++; 1265 packet_->isFirstPacket = false; 1266 packet_->markerBit = false; 1267 packet_->seqNum = seq_num_; 1268 1269 if (firstPacket) { 1270 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 1271 &retransmitted)); 1272 firstPacket = false; 1273 } else { 1274 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(*packet_, 1275 &retransmitted)); 1276 } 1277 1278 loop++; 1279 } while (loop < kMaxPacketsInSession); 1280 1281 // Max number of packets inserted. 1282 // Insert one more packet. 1283 seq_num_++; 1284 packet_->isFirstPacket = false; 1285 packet_->markerBit = true; 1286 packet_->seqNum = seq_num_; 1287 1288 // Insert the packet -> frame recycled. 1289 EXPECT_EQ(kSizeError, jitter_buffer_->InsertPacket(*packet_, 1290 &retransmitted)); 1291 EXPECT_TRUE(NULL == DecodeCompleteFrame()); 1292 1293 } 1294 1295 TEST_F(TestBasicJitterBuffer, ExceedNumOfFrameWithSeqNumWrap) { 1296 // TEST fill JB with more than max number of frame (50 delta frames + 1297 // 51 key frames) with wrap in seq_num_ 1298 // 1299 // -------------------------------------------------------------- 1300 // | 65485 | 65486 | 65487 | .... | 65535 | 0 | 1 | 2 | .....| 50 | 1301 // -------------------------------------------------------------- 1302 // |<-----------delta frames------------->|<------key frames----->| 1303 1304 int loop = 0; 1305 seq_num_ = 65485; 1306 uint32_t first_key_frame_timestamp = 0; 1307 bool retransmitted = false; 1308 // Insert MAX_NUMBER_OF_FRAMES frames. 1309 do { 1310 timestamp_ += 33*90; 1311 seq_num_++; 1312 packet_->isFirstPacket = true; 1313 packet_->markerBit = true; 1314 packet_->seqNum = seq_num_; 1315 packet_->timestamp = timestamp_; 1316 1317 if (loop == 50) { 1318 first_key_frame_timestamp = packet_->timestamp; 1319 packet_->frameType = kVideoFrameKey; 1320 } 1321 1322 // Insert frame. 1323 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 1324 &retransmitted)); 1325 1326 loop++; 1327 } while (loop < kMaxNumberOfFrames); 1328 1329 // Max number of frames inserted. 1330 1331 // Insert one more frame. 1332 timestamp_ += 33*90; 1333 seq_num_++; 1334 packet_->isFirstPacket = true; 1335 packet_->markerBit = true; 1336 packet_->seqNum = seq_num_; 1337 packet_->timestamp = timestamp_; 1338 1339 // Now, no free frame - frames will be recycled until first key frame. 1340 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 1341 &retransmitted)); 1342 1343 VCMEncodedFrame* frame_out = DecodeCompleteFrame(); 1344 EXPECT_EQ(first_key_frame_timestamp, frame_out->TimeStamp()); 1345 1346 CheckOutFrame(frame_out, size_, false); 1347 1348 EXPECT_EQ(kVideoFrameKey, frame_out->FrameType()); 1349 } 1350 1351 TEST_F(TestBasicJitterBuffer, EmptyLastFrame) { 1352 jitter_buffer_->SetDecodeErrorMode(kWithErrors); 1353 seq_num_ = 3; 1354 // Insert one empty packet per frame, should never return the last timestamp 1355 // inserted. Only return empty frames in the presence of subsequent frames. 1356 int maxSize = 1000; 1357 bool retransmitted = false; 1358 for (int i = 0; i < maxSize + 10; i++) { 1359 timestamp_ += 33 * 90; 1360 seq_num_++; 1361 packet_->isFirstPacket = false; 1362 packet_->markerBit = false; 1363 packet_->seqNum = seq_num_; 1364 packet_->timestamp = timestamp_; 1365 packet_->frameType = kFrameEmpty; 1366 1367 EXPECT_EQ(kNoError, jitter_buffer_->InsertPacket(*packet_, 1368 &retransmitted)); 1369 VCMEncodedFrame* testFrame = DecodeIncompleteFrame(); 1370 // Timestamp should never be the last TS inserted. 1371 if (testFrame != NULL) { 1372 EXPECT_TRUE(testFrame->TimeStamp() < timestamp_); 1373 } 1374 } 1375 } 1376 1377 TEST_F(TestBasicJitterBuffer, H264IncompleteNalu) { 1378 jitter_buffer_->SetNackMode(kNoNack, -1, -1); 1379 jitter_buffer_->SetDecodeErrorMode(kWithErrors); 1380 ++seq_num_; 1381 timestamp_ += 33 * 90; 1382 int insertedLength = 0; 1383 packet_->seqNum = seq_num_; 1384 packet_->timestamp = timestamp_; 1385 packet_->frameType = kVideoFrameKey; 1386 packet_->isFirstPacket = true; 1387 packet_->completeNALU = kNaluStart; 1388 packet_->markerBit = false; 1389 bool retransmitted = false; 1390 1391 EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_, 1392 &retransmitted)); 1393 1394 seq_num_ += 2; // Skip one packet. 1395 packet_->seqNum = seq_num_; 1396 packet_->frameType = kVideoFrameKey; 1397 packet_->isFirstPacket = false; 1398 packet_->completeNALU = kNaluIncomplete; 1399 packet_->markerBit = false; 1400 1401 EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_, 1402 &retransmitted)); 1403 1404 seq_num_++; 1405 packet_->seqNum = seq_num_; 1406 packet_->frameType = kVideoFrameKey; 1407 packet_->isFirstPacket = false; 1408 packet_->completeNALU = kNaluEnd; 1409 packet_->markerBit = false; 1410 1411 EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_, 1412 &retransmitted)); 1413 1414 seq_num_++; 1415 packet_->seqNum = seq_num_; 1416 packet_->completeNALU = kNaluComplete; 1417 packet_->markerBit = true; // Last packet. 1418 EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_, 1419 &retransmitted)); 1420 // The JB will only output (incomplete) frames if a packet belonging to a 1421 // subsequent frame was already inserted. Insert one packet of a subsequent 1422 // frame. place high timestamp so the JB would always have a next frame 1423 // (otherwise, for every inserted frame we need to take care of the next 1424 // frame as well). 1425 packet_->seqNum = 1; 1426 packet_->timestamp = timestamp_ + 33 * 90 * 10; 1427 packet_->frameType = kVideoFrameDelta; 1428 packet_->isFirstPacket = false; 1429 packet_->completeNALU = kNaluStart; 1430 packet_->markerBit = false; 1431 1432 EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_, 1433 &retransmitted)); 1434 1435 VCMEncodedFrame* frame_out = DecodeIncompleteFrame(); 1436 1437 // We can decode everything from a NALU until a packet has been lost. 1438 // Thus we can decode the first packet of the first NALU and the second NALU 1439 // which consists of one packet. 1440 CheckOutFrame(frame_out, packet_->sizeBytes * 2, false); 1441 jitter_buffer_->ReleaseFrame(frame_out); 1442 1443 // Test reordered start frame + 1 lost. 1444 seq_num_ += 2; // Re-order 1 frame. 1445 timestamp_ += 33*90; 1446 insertedLength = 0; 1447 1448 packet_->seqNum = seq_num_; 1449 packet_->timestamp = timestamp_; 1450 packet_->frameType = kVideoFrameKey; 1451 packet_->isFirstPacket = false; 1452 packet_->completeNALU = kNaluEnd; 1453 packet_->markerBit = false; 1454 EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_, 1455 &retransmitted)); 1456 insertedLength += packet_->sizeBytes; // This packet should be decoded. 1457 seq_num_--; 1458 packet_->seqNum = seq_num_; 1459 packet_->timestamp = timestamp_; 1460 packet_->frameType = kVideoFrameKey; 1461 packet_->isFirstPacket = true; 1462 packet_->completeNALU = kNaluStart; 1463 packet_->markerBit = false; 1464 1465 EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_, 1466 &retransmitted)); 1467 insertedLength += packet_->sizeBytes; // This packet should be decoded. 1468 1469 seq_num_ += 3; // One packet drop. 1470 packet_->seqNum = seq_num_; 1471 packet_->timestamp = timestamp_; 1472 packet_->frameType = kVideoFrameKey; 1473 packet_->isFirstPacket = false; 1474 packet_->completeNALU = kNaluComplete; 1475 packet_->markerBit = false; 1476 EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_, 1477 &retransmitted)); 1478 insertedLength += packet_->sizeBytes; // This packet should be decoded. 1479 seq_num_++; 1480 packet_->seqNum = seq_num_; 1481 packet_->timestamp = timestamp_; 1482 packet_->frameType = kVideoFrameKey; 1483 packet_->isFirstPacket = false; 1484 packet_->completeNALU = kNaluStart; 1485 packet_->markerBit = false; 1486 EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_, 1487 &retransmitted)); 1488 // This packet should be decoded since it's the beginning of a NAL. 1489 insertedLength += packet_->sizeBytes; 1490 1491 seq_num_ += 2; 1492 packet_->seqNum = seq_num_; 1493 packet_->timestamp = timestamp_; 1494 packet_->frameType = kVideoFrameKey; 1495 packet_->isFirstPacket = false; 1496 packet_->completeNALU = kNaluEnd; 1497 packet_->markerBit = true; 1498 EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_, 1499 &retransmitted)); 1500 // This packet should not be decoded because it is an incomplete NAL if it 1501 // is the last. 1502 frame_out = DecodeIncompleteFrame(); 1503 // Only last NALU is complete. 1504 CheckOutFrame(frame_out, insertedLength, false); 1505 jitter_buffer_->ReleaseFrame(frame_out); 1506 1507 // Test to insert empty packet. 1508 seq_num_++; 1509 timestamp_ += 33 * 90; 1510 VCMPacket emptypacket(data_, 0, seq_num_, timestamp_, true); 1511 emptypacket.seqNum = seq_num_; 1512 emptypacket.timestamp = timestamp_; 1513 emptypacket.frameType = kVideoFrameKey; 1514 emptypacket.isFirstPacket = true; 1515 emptypacket.completeNALU = kNaluComplete; 1516 emptypacket.markerBit = true; 1517 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(emptypacket, 1518 &retransmitted)); 1519 // This packet should not be decoded because it is an incomplete NAL if it 1520 // is the last. 1521 1522 // Will be sent to the decoder, as a packet belonging to a subsequent frame 1523 // has arrived. 1524 frame_out = DecodeIncompleteFrame(); 1525 1526 1527 // Test that a frame can include an empty packet. 1528 seq_num_++; 1529 timestamp_ += 33 * 90; 1530 1531 packet_->seqNum = seq_num_; 1532 packet_->timestamp = timestamp_; 1533 packet_->frameType = kVideoFrameKey; 1534 packet_->isFirstPacket = true; 1535 packet_->completeNALU = kNaluComplete; 1536 packet_->markerBit = false; 1537 1538 EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_, 1539 &retransmitted)); 1540 1541 seq_num_++; 1542 emptypacket.seqNum = seq_num_; 1543 emptypacket.timestamp = timestamp_; 1544 emptypacket.frameType = kVideoFrameKey; 1545 emptypacket.isFirstPacket = true; 1546 emptypacket.completeNALU = kNaluComplete; 1547 emptypacket.markerBit = true; 1548 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(emptypacket, 1549 &retransmitted)); 1550 1551 frame_out = DecodeCompleteFrame(); 1552 // Only last NALU is complete 1553 CheckOutFrame(frame_out, packet_->sizeBytes, false); 1554 } 1555 1556 TEST_F(TestBasicJitterBuffer, NextFrameWhenIncomplete) { 1557 // Test that a we cannot get incomplete frames from the JB if we haven't 1558 // received the marker bit, unless we have received a packet from a later 1559 // timestamp. 1560 jitter_buffer_->SetDecodeErrorMode(kWithErrors); 1561 // Start with a complete key frame - insert and decode. 1562 packet_->frameType = kVideoFrameKey; 1563 packet_->isFirstPacket = true; 1564 packet_->markerBit = true; 1565 bool retransmitted = false; 1566 1567 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(*packet_, 1568 &retransmitted)); 1569 VCMEncodedFrame* frame_out = DecodeCompleteFrame(); 1570 EXPECT_TRUE(frame_out != NULL); 1571 1572 packet_->seqNum += 2; 1573 packet_->timestamp += 33 * 90; 1574 packet_->frameType = kVideoFrameDelta; 1575 packet_->isFirstPacket = false; 1576 packet_->markerBit = false; 1577 1578 1579 EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_, 1580 &retransmitted)); 1581 1582 frame_out = DecodeIncompleteFrame(); 1583 EXPECT_TRUE(frame_out == NULL); 1584 1585 packet_->seqNum += 2; 1586 packet_->timestamp += 33 * 90; 1587 packet_->isFirstPacket = true; 1588 1589 EXPECT_EQ(kDecodableSession, jitter_buffer_->InsertPacket(*packet_, 1590 &retransmitted)); 1591 1592 frame_out = DecodeIncompleteFrame(); 1593 1594 CheckOutFrame(frame_out, packet_->sizeBytes, false); 1595 } 1596 1597 TEST_F(TestRunningJitterBuffer, Full) { 1598 // Insert a key frame and decode it. 1599 EXPECT_GE(InsertFrame(kVideoFrameKey), kNoError); 1600 EXPECT_TRUE(DecodeCompleteFrame()); 1601 DropFrame(1); 1602 // Fill the jitter buffer. 1603 EXPECT_GE(InsertFrames(kMaxNumberOfFrames, kVideoFrameDelta), kNoError); 1604 // Make sure we can't decode these frames. 1605 EXPECT_FALSE(DecodeCompleteFrame()); 1606 // This frame will make the jitter buffer recycle frames until a key frame. 1607 // Since none is found it will have to wait until the next key frame before 1608 // decoding. 1609 EXPECT_GE(InsertFrame(kVideoFrameDelta), kNoError); 1610 EXPECT_FALSE(DecodeCompleteFrame()); 1611 } 1612 1613 TEST_F(TestRunningJitterBuffer, EmptyPackets) { 1614 // Make sure a frame can get complete even though empty packets are missing. 1615 stream_generator_->GenerateFrame(kVideoFrameKey, 3, 3, 1616 clock_->TimeInMilliseconds()); 1617 bool request_key_frame = false; 1618 // Insert empty packet. 1619 EXPECT_EQ(kNoError, InsertPacketAndPop(4)); 1620 EXPECT_FALSE(request_key_frame); 1621 // Insert 3 media packets. 1622 EXPECT_EQ(kIncomplete, InsertPacketAndPop(0)); 1623 EXPECT_FALSE(request_key_frame); 1624 EXPECT_EQ(kIncomplete, InsertPacketAndPop(0)); 1625 EXPECT_FALSE(request_key_frame); 1626 EXPECT_EQ(kCompleteSession, InsertPacketAndPop(0)); 1627 EXPECT_FALSE(request_key_frame); 1628 // Insert empty packet. 1629 EXPECT_EQ(kCompleteSession, InsertPacketAndPop(0)); 1630 EXPECT_FALSE(request_key_frame); 1631 } 1632 1633 TEST_F(TestRunningJitterBuffer, StatisticsTest) { 1634 std::map<FrameType, uint32_t> frame_stats(jitter_buffer_->FrameStatistics()); 1635 EXPECT_EQ(0u, frame_stats[kVideoFrameDelta]); 1636 EXPECT_EQ(0u, frame_stats[kVideoFrameKey]); 1637 1638 uint32_t framerate = 0; 1639 uint32_t bitrate = 0; 1640 jitter_buffer_->IncomingRateStatistics(&framerate, &bitrate); 1641 EXPECT_EQ(0u, framerate); 1642 EXPECT_EQ(0u, bitrate); 1643 1644 // Insert a couple of key and delta frames. 1645 InsertFrame(kVideoFrameKey); 1646 InsertFrame(kVideoFrameDelta); 1647 InsertFrame(kVideoFrameDelta); 1648 InsertFrame(kVideoFrameKey); 1649 InsertFrame(kVideoFrameDelta); 1650 // Decode some of them to make sure the statistics doesn't depend on frames 1651 // being decoded. 1652 EXPECT_TRUE(DecodeCompleteFrame()); 1653 EXPECT_TRUE(DecodeCompleteFrame()); 1654 frame_stats = jitter_buffer_->FrameStatistics(); 1655 EXPECT_EQ(3u, frame_stats[kVideoFrameDelta]); 1656 EXPECT_EQ(2u, frame_stats[kVideoFrameKey]); 1657 1658 // Insert 20 more frames to get estimates of bitrate and framerate over 1659 // 1 second. 1660 for (int i = 0; i < 20; ++i) { 1661 InsertFrame(kVideoFrameDelta); 1662 } 1663 jitter_buffer_->IncomingRateStatistics(&framerate, &bitrate); 1664 // TODO(holmer): The current implementation returns the average of the last 1665 // two framerate calculations, which is why it takes two calls to reach the 1666 // actual framerate. This should be fixed. 1667 EXPECT_EQ(kDefaultFrameRate / 2u, framerate); 1668 EXPECT_EQ(kDefaultBitrateKbps, bitrate); 1669 // Insert 25 more frames to get estimates of bitrate and framerate over 1670 // 2 seconds. 1671 for (int i = 0; i < 25; ++i) { 1672 InsertFrame(kVideoFrameDelta); 1673 } 1674 jitter_buffer_->IncomingRateStatistics(&framerate, &bitrate); 1675 EXPECT_EQ(kDefaultFrameRate, framerate); 1676 EXPECT_EQ(kDefaultBitrateKbps, bitrate); 1677 } 1678 1679 TEST_F(TestRunningJitterBuffer, SkipToKeyFrame) { 1680 // Insert delta frames. 1681 EXPECT_GE(InsertFrames(5, kVideoFrameDelta), kNoError); 1682 // Can't decode without a key frame. 1683 EXPECT_FALSE(DecodeCompleteFrame()); 1684 InsertFrame(kVideoFrameKey); 1685 // Skip to the next key frame. 1686 EXPECT_TRUE(DecodeCompleteFrame()); 1687 } 1688 1689 TEST_F(TestRunningJitterBuffer, DontSkipToKeyFrameIfDecodable) { 1690 InsertFrame(kVideoFrameKey); 1691 EXPECT_TRUE(DecodeCompleteFrame()); 1692 const int kNumDeltaFrames = 5; 1693 EXPECT_GE(InsertFrames(kNumDeltaFrames, kVideoFrameDelta), kNoError); 1694 InsertFrame(kVideoFrameKey); 1695 for (int i = 0; i < kNumDeltaFrames + 1; ++i) { 1696 EXPECT_TRUE(DecodeCompleteFrame()); 1697 } 1698 } 1699 1700 TEST_F(TestRunningJitterBuffer, KeyDeltaKeyDelta) { 1701 InsertFrame(kVideoFrameKey); 1702 EXPECT_TRUE(DecodeCompleteFrame()); 1703 const int kNumDeltaFrames = 5; 1704 EXPECT_GE(InsertFrames(kNumDeltaFrames, kVideoFrameDelta), kNoError); 1705 InsertFrame(kVideoFrameKey); 1706 EXPECT_GE(InsertFrames(kNumDeltaFrames, kVideoFrameDelta), kNoError); 1707 InsertFrame(kVideoFrameKey); 1708 for (int i = 0; i < 2 * (kNumDeltaFrames + 1); ++i) { 1709 EXPECT_TRUE(DecodeCompleteFrame()); 1710 } 1711 } 1712 1713 TEST_F(TestRunningJitterBuffer, TwoPacketsNonContinuous) { 1714 InsertFrame(kVideoFrameKey); 1715 EXPECT_TRUE(DecodeCompleteFrame()); 1716 stream_generator_->GenerateFrame(kVideoFrameDelta, 1, 0, 1717 clock_->TimeInMilliseconds()); 1718 clock_->AdvanceTimeMilliseconds(kDefaultFramePeriodMs); 1719 stream_generator_->GenerateFrame(kVideoFrameDelta, 2, 0, 1720 clock_->TimeInMilliseconds()); 1721 EXPECT_EQ(kIncomplete, InsertPacketAndPop(1)); 1722 EXPECT_EQ(kCompleteSession, InsertPacketAndPop(1)); 1723 EXPECT_FALSE(DecodeCompleteFrame()); 1724 EXPECT_EQ(kCompleteSession, InsertPacketAndPop(0)); 1725 EXPECT_TRUE(DecodeCompleteFrame()); 1726 EXPECT_TRUE(DecodeCompleteFrame()); 1727 } 1728 1729 TEST_F(TestJitterBufferNack, EmptyPackets) { 1730 // Make sure empty packets doesn't clog the jitter buffer. 1731 jitter_buffer_->SetNackMode(kNack, media_optimization::kLowRttNackMs, -1); 1732 EXPECT_GE(InsertFrames(kMaxNumberOfFrames, kFrameEmpty), kNoError); 1733 InsertFrame(kVideoFrameKey); 1734 EXPECT_TRUE(DecodeCompleteFrame()); 1735 } 1736 1737 TEST_F(TestJitterBufferNack, NackTooOldPackets) { 1738 // Insert a key frame and decode it. 1739 EXPECT_GE(InsertFrame(kVideoFrameKey), kNoError); 1740 EXPECT_TRUE(DecodeCompleteFrame()); 1741 1742 // Drop one frame and insert |kNackHistoryLength| to trigger NACKing a too 1743 // old packet. 1744 DropFrame(1); 1745 // Insert a frame which should trigger a recycle until the next key frame. 1746 EXPECT_EQ(kFlushIndicator, InsertFrames(oldest_packet_to_nack_ + 1, 1747 kVideoFrameDelta)); 1748 EXPECT_FALSE(DecodeCompleteFrame()); 1749 1750 uint16_t nack_list_length = max_nack_list_size_; 1751 bool request_key_frame = false; 1752 uint16_t* nack_list = jitter_buffer_->GetNackList(&nack_list_length, 1753 &request_key_frame); 1754 // No key frame will be requested since the jitter buffer is empty. 1755 EXPECT_FALSE(request_key_frame); 1756 EXPECT_TRUE(nack_list == NULL); 1757 EXPECT_EQ(0, nack_list_length); 1758 1759 EXPECT_GE(InsertFrame(kVideoFrameDelta), kNoError); 1760 // Verify that the jitter buffer requests a key frame since we need one to 1761 // start decoding. 1762 EXPECT_FALSE(request_key_frame); 1763 EXPECT_TRUE(nack_list == NULL); 1764 EXPECT_EQ(0, nack_list_length); 1765 // Waiting for a key frame. 1766 EXPECT_FALSE(DecodeCompleteFrame()); 1767 EXPECT_FALSE(DecodeIncompleteFrame()); 1768 1769 // The next complete continuous frame isn't a key frame, but we're waiting 1770 // for one. 1771 EXPECT_FALSE(DecodeCompleteFrame()); 1772 EXPECT_GE(InsertFrame(kVideoFrameKey), kNoError); 1773 // Skipping ahead to the key frame. 1774 EXPECT_TRUE(DecodeCompleteFrame()); 1775 } 1776 1777 TEST_F(TestJitterBufferNack, NackLargeJitterBuffer) { 1778 // Insert a key frame and decode it. 1779 EXPECT_GE(InsertFrame(kVideoFrameKey), kNoError); 1780 EXPECT_TRUE(DecodeCompleteFrame()); 1781 1782 // Insert a frame which should trigger a recycle until the next key frame. 1783 EXPECT_GE(InsertFrames(oldest_packet_to_nack_, kVideoFrameDelta), kNoError); 1784 1785 uint16_t nack_list_length = max_nack_list_size_; 1786 bool request_key_frame = false; 1787 jitter_buffer_->GetNackList(&nack_list_length, &request_key_frame); 1788 // Verify that the jitter buffer does not request a key frame. 1789 EXPECT_FALSE(request_key_frame); 1790 // Verify that no packets are NACKed. 1791 EXPECT_EQ(0, nack_list_length); 1792 // Verify that we can decode the next frame. 1793 EXPECT_TRUE(DecodeCompleteFrame()); 1794 } 1795 1796 TEST_F(TestJitterBufferNack, NackListFull) { 1797 // Insert a key frame and decode it. 1798 EXPECT_GE(InsertFrame(kVideoFrameKey), kNoError); 1799 EXPECT_TRUE(DecodeCompleteFrame()); 1800 1801 // Generate and drop |kNackHistoryLength| packets to fill the NACK list. 1802 DropFrame(max_nack_list_size_ + 1); 1803 // Insert a frame which should trigger a recycle until the next key frame. 1804 EXPECT_EQ(kFlushIndicator, InsertFrame(kVideoFrameDelta)); 1805 EXPECT_FALSE(DecodeCompleteFrame()); 1806 1807 uint16_t nack_list_length = max_nack_list_size_; 1808 bool request_key_frame = false; 1809 jitter_buffer_->GetNackList(&nack_list_length, &request_key_frame); 1810 // The jitter buffer is empty, so we won't request key frames until we get a 1811 // packet. 1812 EXPECT_FALSE(request_key_frame); 1813 1814 EXPECT_GE(InsertFrame(kVideoFrameDelta), kNoError); 1815 // Now we have a packet in the jitter buffer, a key frame will be requested 1816 // since it's not a key frame. 1817 jitter_buffer_->GetNackList(&nack_list_length, &request_key_frame); 1818 // The jitter buffer is empty, so we won't request key frames until we get a 1819 // packet. 1820 EXPECT_TRUE(request_key_frame); 1821 // The next complete continuous frame isn't a key frame, but we're waiting 1822 // for one. 1823 EXPECT_FALSE(DecodeCompleteFrame()); 1824 EXPECT_FALSE(DecodeIncompleteFrame()); 1825 EXPECT_GE(InsertFrame(kVideoFrameKey), kNoError); 1826 // Skipping ahead to the key frame. 1827 EXPECT_TRUE(DecodeCompleteFrame()); 1828 } 1829 1830 TEST_F(TestJitterBufferNack, NoNackListReturnedBeforeFirstDecode) { 1831 DropFrame(10); 1832 // Insert a frame and try to generate a NACK list. Shouldn't get one. 1833 EXPECT_GE(InsertFrame(kVideoFrameDelta), kNoError); 1834 uint16_t nack_list_size = 0; 1835 bool request_key_frame = false; 1836 uint16_t* list = jitter_buffer_->GetNackList(&nack_list_size, 1837 &request_key_frame); 1838 // No list generated, and a key frame request is signaled. 1839 EXPECT_TRUE(list == NULL); 1840 EXPECT_EQ(0, nack_list_size); 1841 EXPECT_TRUE(request_key_frame); 1842 } 1843 1844 TEST_F(TestJitterBufferNack, NackListBuiltBeforeFirstDecode) { 1845 stream_generator_->Init(0, 0, clock_->TimeInMilliseconds()); 1846 InsertFrame(kVideoFrameKey); 1847 stream_generator_->GenerateFrame(kVideoFrameDelta, 2, 0, 1848 clock_->TimeInMilliseconds()); 1849 stream_generator_->NextPacket(NULL); // Drop packet. 1850 EXPECT_EQ(kIncomplete, InsertPacketAndPop(0)); 1851 EXPECT_TRUE(DecodeCompleteFrame()); 1852 uint16_t nack_list_size = 0; 1853 bool extended = false; 1854 uint16_t* list = jitter_buffer_->GetNackList(&nack_list_size, &extended); 1855 EXPECT_EQ(1, nack_list_size); 1856 EXPECT_TRUE(list != NULL); 1857 } 1858 1859 TEST_F(TestJitterBufferNack, VerifyRetransmittedFlag) { 1860 stream_generator_->Init(0, 0, clock_->TimeInMilliseconds()); 1861 stream_generator_->GenerateFrame(kVideoFrameKey, 3, 0, 1862 clock_->TimeInMilliseconds()); 1863 VCMPacket packet; 1864 stream_generator_->PopPacket(&packet, 0); 1865 bool retransmitted = false; 1866 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(packet, &retransmitted)); 1867 EXPECT_FALSE(retransmitted); 1868 // Drop second packet. 1869 stream_generator_->PopPacket(&packet, 1); 1870 EXPECT_EQ(kIncomplete, jitter_buffer_->InsertPacket(packet, &retransmitted)); 1871 EXPECT_FALSE(retransmitted); 1872 EXPECT_FALSE(DecodeCompleteFrame()); 1873 uint16_t nack_list_size = 0; 1874 bool extended = false; 1875 uint16_t* list = jitter_buffer_->GetNackList(&nack_list_size, &extended); 1876 EXPECT_EQ(1, nack_list_size); 1877 ASSERT_TRUE(list != NULL); 1878 stream_generator_->PopPacket(&packet, 0); 1879 EXPECT_EQ(packet.seqNum, list[0]); 1880 EXPECT_EQ(kCompleteSession, jitter_buffer_->InsertPacket(packet, 1881 &retransmitted)); 1882 EXPECT_TRUE(retransmitted); 1883 EXPECT_TRUE(DecodeCompleteFrame()); 1884 } 1885 1886 TEST_F(TestJitterBufferNack, UseNackToRecoverFirstKeyFrame) { 1887 stream_generator_->Init(0, 0, clock_->TimeInMilliseconds()); 1888 stream_generator_->GenerateFrame(kVideoFrameKey, 3, 0, 1889 clock_->TimeInMilliseconds()); 1890 EXPECT_EQ(kIncomplete, InsertPacketAndPop(0)); 1891 // Drop second packet. 1892 EXPECT_EQ(kIncomplete, InsertPacketAndPop(1)); 1893 EXPECT_FALSE(DecodeCompleteFrame()); 1894 uint16_t nack_list_size = 0; 1895 bool extended = false; 1896 uint16_t* list = jitter_buffer_->GetNackList(&nack_list_size, &extended); 1897 EXPECT_EQ(1, nack_list_size); 1898 ASSERT_TRUE(list != NULL); 1899 VCMPacket packet; 1900 stream_generator_->GetPacket(&packet, 0); 1901 EXPECT_EQ(packet.seqNum, list[0]); 1902 } 1903 1904 TEST_F(TestJitterBufferNack, UseNackToRecoverFirstKeyFrameSecondInQueue) { 1905 VCMPacket packet; 1906 stream_generator_->Init(0, 0, clock_->TimeInMilliseconds()); 1907 // First frame is delta. 1908 stream_generator_->GenerateFrame(kVideoFrameDelta, 3, 0, 1909 clock_->TimeInMilliseconds()); 1910 EXPECT_EQ(kIncomplete, InsertPacketAndPop(0)); 1911 // Drop second packet in frame. 1912 ASSERT_TRUE(stream_generator_->PopPacket(&packet, 0)); 1913 EXPECT_EQ(kIncomplete, InsertPacketAndPop(0)); 1914 // Second frame is key. 1915 stream_generator_->GenerateFrame(kVideoFrameKey, 3, 0, 1916 clock_->TimeInMilliseconds() + 10); 1917 EXPECT_EQ(kIncomplete, InsertPacketAndPop(0)); 1918 // Drop second packet in frame. 1919 EXPECT_EQ(kIncomplete, InsertPacketAndPop(1)); 1920 EXPECT_FALSE(DecodeCompleteFrame()); 1921 uint16_t nack_list_size = 0; 1922 bool extended = false; 1923 uint16_t* list = jitter_buffer_->GetNackList(&nack_list_size, &extended); 1924 EXPECT_EQ(1, nack_list_size); 1925 ASSERT_TRUE(list != NULL); 1926 stream_generator_->GetPacket(&packet, 0); 1927 EXPECT_EQ(packet.seqNum, list[0]); 1928 } 1929 1930 TEST_F(TestJitterBufferNack, NormalOperation) { 1931 EXPECT_EQ(kNack, jitter_buffer_->nack_mode()); 1932 jitter_buffer_->SetDecodeErrorMode(kWithErrors); 1933 1934 EXPECT_GE(InsertFrame(kVideoFrameKey), kNoError); 1935 EXPECT_TRUE(DecodeIncompleteFrame()); 1936 1937 // ---------------------------------------------------------------- 1938 // | 1 | 2 | .. | 8 | 9 | x | 11 | 12 | .. | 19 | x | 21 | .. | 100 | 1939 // ---------------------------------------------------------------- 1940 stream_generator_->GenerateFrame(kVideoFrameKey, 100, 0, 1941 clock_->TimeInMilliseconds()); 1942 clock_->AdvanceTimeMilliseconds(kDefaultFramePeriodMs); 1943 EXPECT_EQ(kDecodableSession, InsertPacketAndPop(0)); 1944 // Verify that the frame is incomplete. 1945 EXPECT_FALSE(DecodeCompleteFrame()); 1946 while (stream_generator_->PacketsRemaining() > 1) { 1947 if (stream_generator_->NextSequenceNumber() % 10 != 0) { 1948 EXPECT_EQ(kDecodableSession, InsertPacketAndPop(0)); 1949 } else { 1950 stream_generator_->NextPacket(NULL); // Drop packet 1951 } 1952 } 1953 EXPECT_EQ(kDecodableSession, InsertPacketAndPop(0)); 1954 EXPECT_EQ(0, stream_generator_->PacketsRemaining()); 1955 EXPECT_FALSE(DecodeCompleteFrame()); 1956 EXPECT_FALSE(DecodeIncompleteFrame()); 1957 uint16_t nack_list_size = 0; 1958 bool request_key_frame = false; 1959 uint16_t* list = jitter_buffer_->GetNackList(&nack_list_size, 1960 &request_key_frame); 1961 // Verify the NACK list. 1962 const int kExpectedNackSize = 9; 1963 ASSERT_EQ(kExpectedNackSize, nack_list_size); 1964 for (int i = 0; i < nack_list_size; ++i) 1965 EXPECT_EQ((1 + i) * 10, list[i]); 1966 } 1967 1968 TEST_F(TestJitterBufferNack, NormalOperationWrap) { 1969 bool request_key_frame = false; 1970 // ------- ------------------------------------------------------------ 1971 // | 65532 | | 65533 | 65534 | 65535 | x | 1 | .. | 9 | x | 11 |.....| 96 | 1972 // ------- ------------------------------------------------------------ 1973 stream_generator_->Init(65532, 0, clock_->TimeInMilliseconds()); 1974 InsertFrame(kVideoFrameKey); 1975 EXPECT_FALSE(request_key_frame); 1976 EXPECT_TRUE(DecodeCompleteFrame()); 1977 stream_generator_->GenerateFrame(kVideoFrameDelta, 100, 0, 1978 clock_->TimeInMilliseconds()); 1979 EXPECT_EQ(kIncomplete, InsertPacketAndPop(0)); 1980 while (stream_generator_->PacketsRemaining() > 1) { 1981 if (stream_generator_->NextSequenceNumber() % 10 != 0) { 1982 EXPECT_EQ(kIncomplete, InsertPacketAndPop(0)); 1983 EXPECT_FALSE(request_key_frame); 1984 } else { 1985 stream_generator_->NextPacket(NULL); // Drop packet 1986 } 1987 } 1988 EXPECT_EQ(kIncomplete, InsertPacketAndPop(0)); 1989 EXPECT_FALSE(request_key_frame); 1990 EXPECT_EQ(0, stream_generator_->PacketsRemaining()); 1991 EXPECT_FALSE(DecodeCompleteFrame()); 1992 EXPECT_FALSE(DecodeCompleteFrame()); 1993 uint16_t nack_list_size = 0; 1994 bool extended = false; 1995 uint16_t* list = jitter_buffer_->GetNackList(&nack_list_size, &extended); 1996 // Verify the NACK list. 1997 const int kExpectedNackSize = 10; 1998 ASSERT_EQ(kExpectedNackSize, nack_list_size); 1999 for (int i = 0; i < nack_list_size; ++i) 2000 EXPECT_EQ(i * 10, list[i]); 2001 } 2002 2003 TEST_F(TestJitterBufferNack, NormalOperationWrap2) { 2004 bool request_key_frame = false; 2005 // ----------------------------------- 2006 // | 65532 | 65533 | 65534 | x | 0 | 1 | 2007 // ----------------------------------- 2008 stream_generator_->Init(65532, 0, clock_->TimeInMilliseconds()); 2009 InsertFrame(kVideoFrameKey); 2010 EXPECT_FALSE(request_key_frame); 2011 EXPECT_TRUE(DecodeCompleteFrame()); 2012 stream_generator_->GenerateFrame(kVideoFrameDelta, 1, 0, 2013 clock_->TimeInMilliseconds()); 2014 clock_->AdvanceTimeMilliseconds(kDefaultFramePeriodMs); 2015 for (int i = 0; i < 5; ++i) { 2016 if (stream_generator_->NextSequenceNumber() != 65535) { 2017 EXPECT_EQ(kCompleteSession, InsertPacketAndPop(0)); 2018 EXPECT_FALSE(request_key_frame); 2019 } else { 2020 stream_generator_->NextPacket(NULL); // Drop packet 2021 } 2022 stream_generator_->GenerateFrame(kVideoFrameDelta, 1, 0, 2023 clock_->TimeInMilliseconds()); 2024 clock_->AdvanceTimeMilliseconds(kDefaultFramePeriodMs); 2025 } 2026 EXPECT_EQ(kCompleteSession, InsertPacketAndPop(0)); 2027 EXPECT_FALSE(request_key_frame); 2028 uint16_t nack_list_size = 0; 2029 bool extended = false; 2030 uint16_t* list = jitter_buffer_->GetNackList(&nack_list_size, &extended); 2031 // Verify the NACK list. 2032 ASSERT_EQ(1, nack_list_size); 2033 EXPECT_EQ(65535, list[0]); 2034 } 2035 2036 TEST_F(TestJitterBufferNack, ResetByFutureKeyFrameDoesntError) { 2037 stream_generator_->Init(0, 0, clock_->TimeInMilliseconds()); 2038 InsertFrame(kVideoFrameKey); 2039 EXPECT_TRUE(DecodeCompleteFrame()); 2040 uint16_t nack_list_size = 0; 2041 bool extended = false; 2042 jitter_buffer_->GetNackList(&nack_list_size, &extended); 2043 EXPECT_EQ(0, nack_list_size); 2044 2045 // Far-into-the-future video frame, could be caused by resetting the encoder 2046 // or otherwise restarting. This should not fail when error when the packet is 2047 // a keyframe, even if all of the nack list needs to be flushed. 2048 stream_generator_->Init(10000, 0, clock_->TimeInMilliseconds()); 2049 clock_->AdvanceTimeMilliseconds(kDefaultFramePeriodMs); 2050 InsertFrame(kVideoFrameKey); 2051 EXPECT_TRUE(DecodeCompleteFrame()); 2052 jitter_buffer_->GetNackList(&nack_list_size, &extended); 2053 EXPECT_EQ(0, nack_list_size); 2054 2055 // Stream should be decodable from this point. 2056 clock_->AdvanceTimeMilliseconds(kDefaultFramePeriodMs); 2057 InsertFrame(kVideoFrameDelta); 2058 EXPECT_TRUE(DecodeCompleteFrame()); 2059 jitter_buffer_->GetNackList(&nack_list_size, &extended); 2060 EXPECT_EQ(0, nack_list_size); 2061 } 2062 2063 } // namespace webrtc 2064