1 /* 2 * Copyright (c) 2013 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 #include <algorithm> 11 #include <map> 12 #include <sstream> 13 #include <string> 14 15 #include "testing/gtest/include/gtest/gtest.h" 16 17 #include "webrtc/base/checks.h" 18 #include "webrtc/base/event.h" 19 #include "webrtc/base/scoped_ptr.h" 20 #include "webrtc/call.h" 21 #include "webrtc/call/transport_adapter.h" 22 #include "webrtc/frame_callback.h" 23 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" 24 #include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h" 25 #include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h" 26 #include "webrtc/modules/video_coding/codecs/vp9/include/vp9.h" 27 #include "webrtc/modules/video_coding/include/video_coding_defines.h" 28 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" 29 #include "webrtc/system_wrappers/include/metrics.h" 30 #include "webrtc/system_wrappers/include/sleep.h" 31 #include "webrtc/test/call_test.h" 32 #include "webrtc/test/direct_transport.h" 33 #include "webrtc/test/encoder_settings.h" 34 #include "webrtc/test/fake_decoder.h" 35 #include "webrtc/test/fake_encoder.h" 36 #include "webrtc/test/frame_generator.h" 37 #include "webrtc/test/frame_generator_capturer.h" 38 #include "webrtc/test/histogram.h" 39 #include "webrtc/test/null_transport.h" 40 #include "webrtc/test/rtcp_packet_parser.h" 41 #include "webrtc/test/rtp_rtcp_observer.h" 42 #include "webrtc/test/testsupport/fileutils.h" 43 #include "webrtc/test/testsupport/perf_test.h" 44 #include "webrtc/video_encoder.h" 45 46 namespace webrtc { 47 48 static const int kSilenceTimeoutMs = 2000; 49 50 class EndToEndTest : public test::CallTest { 51 public: 52 EndToEndTest() {} 53 54 virtual ~EndToEndTest() { 55 EXPECT_EQ(nullptr, video_send_stream_); 56 EXPECT_TRUE(video_receive_streams_.empty()); 57 } 58 59 protected: 60 class UnusedTransport : public Transport { 61 private: 62 bool SendRtp(const uint8_t* packet, 63 size_t length, 64 const PacketOptions& options) override { 65 ADD_FAILURE() << "Unexpected RTP sent."; 66 return false; 67 } 68 69 bool SendRtcp(const uint8_t* packet, size_t length) override { 70 ADD_FAILURE() << "Unexpected RTCP sent."; 71 return false; 72 } 73 }; 74 75 void DecodesRetransmittedFrame(bool use_rtx, bool use_red); 76 void ReceivesPliAndRecovers(int rtp_history_ms); 77 void RespectsRtcpMode(RtcpMode rtcp_mode); 78 void TestXrReceiverReferenceTimeReport(bool enable_rrtr); 79 void TestSendsSetSsrcs(size_t num_ssrcs, bool send_single_ssrc_first); 80 void TestRtpStatePreservation(bool use_rtx); 81 void VerifyHistogramStats(bool use_rtx, bool use_red, bool screenshare); 82 }; 83 84 TEST_F(EndToEndTest, ReceiverCanBeStartedTwice) { 85 CreateCalls(Call::Config(), Call::Config()); 86 87 test::NullTransport transport; 88 CreateSendConfig(1, 0, &transport); 89 CreateMatchingReceiveConfigs(&transport); 90 91 CreateVideoStreams(); 92 93 video_receive_streams_[0]->Start(); 94 video_receive_streams_[0]->Start(); 95 96 DestroyStreams(); 97 } 98 99 TEST_F(EndToEndTest, ReceiverCanBeStoppedTwice) { 100 CreateCalls(Call::Config(), Call::Config()); 101 102 test::NullTransport transport; 103 CreateSendConfig(1, 0, &transport); 104 CreateMatchingReceiveConfigs(&transport); 105 106 CreateVideoStreams(); 107 108 video_receive_streams_[0]->Stop(); 109 video_receive_streams_[0]->Stop(); 110 111 DestroyStreams(); 112 } 113 114 TEST_F(EndToEndTest, RendersSingleDelayedFrame) { 115 static const int kWidth = 320; 116 static const int kHeight = 240; 117 // This constant is chosen to be higher than the timeout in the video_render 118 // module. This makes sure that frames aren't dropped if there are no other 119 // frames in the queue. 120 static const int kDelayRenderCallbackMs = 1000; 121 122 class Renderer : public VideoRenderer { 123 public: 124 Renderer() : event_(false, false) {} 125 126 void RenderFrame(const VideoFrame& video_frame, 127 int /*time_to_render_ms*/) override { 128 event_.Set(); 129 } 130 131 bool IsTextureSupported() const override { return false; } 132 133 bool Wait() { return event_.Wait(kDefaultTimeoutMs); } 134 135 rtc::Event event_; 136 } renderer; 137 138 class TestFrameCallback : public I420FrameCallback { 139 public: 140 TestFrameCallback() : event_(false, false) {} 141 142 bool Wait() { return event_.Wait(kDefaultTimeoutMs); } 143 144 private: 145 void FrameCallback(VideoFrame* frame) override { 146 SleepMs(kDelayRenderCallbackMs); 147 event_.Set(); 148 } 149 150 rtc::Event event_; 151 }; 152 153 CreateCalls(Call::Config(), Call::Config()); 154 155 test::DirectTransport sender_transport(sender_call_.get()); 156 test::DirectTransport receiver_transport(receiver_call_.get()); 157 sender_transport.SetReceiver(receiver_call_->Receiver()); 158 receiver_transport.SetReceiver(sender_call_->Receiver()); 159 160 CreateSendConfig(1, 0, &sender_transport); 161 CreateMatchingReceiveConfigs(&receiver_transport); 162 163 TestFrameCallback pre_render_callback; 164 video_receive_configs_[0].pre_render_callback = &pre_render_callback; 165 video_receive_configs_[0].renderer = &renderer; 166 167 CreateVideoStreams(); 168 Start(); 169 170 // Create frames that are smaller than the send width/height, this is done to 171 // check that the callbacks are done after processing video. 172 rtc::scoped_ptr<test::FrameGenerator> frame_generator( 173 test::FrameGenerator::CreateChromaGenerator(kWidth, kHeight)); 174 video_send_stream_->Input()->IncomingCapturedFrame( 175 *frame_generator->NextFrame()); 176 EXPECT_TRUE(pre_render_callback.Wait()) 177 << "Timed out while waiting for pre-render callback."; 178 EXPECT_TRUE(renderer.Wait()) 179 << "Timed out while waiting for the frame to render."; 180 181 Stop(); 182 183 sender_transport.StopSending(); 184 receiver_transport.StopSending(); 185 186 DestroyStreams(); 187 } 188 189 TEST_F(EndToEndTest, TransmitsFirstFrame) { 190 class Renderer : public VideoRenderer { 191 public: 192 Renderer() : event_(false, false) {} 193 194 void RenderFrame(const VideoFrame& video_frame, 195 int /*time_to_render_ms*/) override { 196 event_.Set(); 197 } 198 bool IsTextureSupported() const override { return false; } 199 200 bool Wait() { return event_.Wait(kDefaultTimeoutMs); } 201 202 rtc::Event event_; 203 } renderer; 204 205 CreateCalls(Call::Config(), Call::Config()); 206 207 test::DirectTransport sender_transport(sender_call_.get()); 208 test::DirectTransport receiver_transport(receiver_call_.get()); 209 sender_transport.SetReceiver(receiver_call_->Receiver()); 210 receiver_transport.SetReceiver(sender_call_->Receiver()); 211 212 CreateSendConfig(1, 0, &sender_transport); 213 CreateMatchingReceiveConfigs(&receiver_transport); 214 video_receive_configs_[0].renderer = &renderer; 215 216 CreateVideoStreams(); 217 Start(); 218 219 rtc::scoped_ptr<test::FrameGenerator> frame_generator( 220 test::FrameGenerator::CreateChromaGenerator( 221 video_encoder_config_.streams[0].width, 222 video_encoder_config_.streams[0].height)); 223 video_send_stream_->Input()->IncomingCapturedFrame( 224 *frame_generator->NextFrame()); 225 226 EXPECT_TRUE(renderer.Wait()) 227 << "Timed out while waiting for the frame to render."; 228 229 Stop(); 230 231 sender_transport.StopSending(); 232 receiver_transport.StopSending(); 233 234 DestroyStreams(); 235 } 236 237 TEST_F(EndToEndTest, SendsAndReceivesVP9) { 238 class VP9Observer : public test::EndToEndTest, public VideoRenderer { 239 public: 240 VP9Observer() 241 : EndToEndTest(2 * kDefaultTimeoutMs), 242 encoder_(VideoEncoder::Create(VideoEncoder::kVp9)), 243 decoder_(VP9Decoder::Create()), 244 frame_counter_(0) {} 245 246 void PerformTest() override { 247 EXPECT_TRUE(Wait()) 248 << "Timed out while waiting for enough frames to be decoded."; 249 } 250 251 void ModifyVideoConfigs( 252 VideoSendStream::Config* send_config, 253 std::vector<VideoReceiveStream::Config>* receive_configs, 254 VideoEncoderConfig* encoder_config) override { 255 send_config->encoder_settings.encoder = encoder_.get(); 256 send_config->encoder_settings.payload_name = "VP9"; 257 send_config->encoder_settings.payload_type = 124; 258 encoder_config->streams[0].min_bitrate_bps = 50000; 259 encoder_config->streams[0].target_bitrate_bps = 260 encoder_config->streams[0].max_bitrate_bps = 2000000; 261 262 (*receive_configs)[0].renderer = this; 263 (*receive_configs)[0].decoders.resize(1); 264 (*receive_configs)[0].decoders[0].payload_type = 265 send_config->encoder_settings.payload_type; 266 (*receive_configs)[0].decoders[0].payload_name = 267 send_config->encoder_settings.payload_name; 268 (*receive_configs)[0].decoders[0].decoder = decoder_.get(); 269 } 270 271 void RenderFrame(const VideoFrame& video_frame, 272 int time_to_render_ms) override { 273 const int kRequiredFrames = 500; 274 if (++frame_counter_ == kRequiredFrames) 275 observation_complete_.Set(); 276 } 277 278 bool IsTextureSupported() const override { return false; } 279 280 private: 281 rtc::scoped_ptr<webrtc::VideoEncoder> encoder_; 282 rtc::scoped_ptr<webrtc::VideoDecoder> decoder_; 283 int frame_counter_; 284 } test; 285 286 RunBaseTest(&test); 287 } 288 289 TEST_F(EndToEndTest, SendsAndReceivesH264) { 290 class H264Observer : public test::EndToEndTest, public VideoRenderer { 291 public: 292 H264Observer() 293 : EndToEndTest(2 * kDefaultTimeoutMs), 294 fake_encoder_(Clock::GetRealTimeClock()), 295 frame_counter_(0) {} 296 297 void PerformTest() override { 298 EXPECT_TRUE(Wait()) 299 << "Timed out while waiting for enough frames to be decoded."; 300 } 301 302 void ModifyVideoConfigs( 303 VideoSendStream::Config* send_config, 304 std::vector<VideoReceiveStream::Config>* receive_configs, 305 VideoEncoderConfig* encoder_config) override { 306 send_config->rtp.nack.rtp_history_ms = 307 (*receive_configs)[0].rtp.nack.rtp_history_ms = kNackRtpHistoryMs; 308 send_config->encoder_settings.encoder = &fake_encoder_; 309 send_config->encoder_settings.payload_name = "H264"; 310 send_config->encoder_settings.payload_type = kFakeVideoSendPayloadType; 311 encoder_config->streams[0].min_bitrate_bps = 50000; 312 encoder_config->streams[0].target_bitrate_bps = 313 encoder_config->streams[0].max_bitrate_bps = 2000000; 314 315 (*receive_configs)[0].renderer = this; 316 (*receive_configs)[0].decoders.resize(1); 317 (*receive_configs)[0].decoders[0].payload_type = 318 send_config->encoder_settings.payload_type; 319 (*receive_configs)[0].decoders[0].payload_name = 320 send_config->encoder_settings.payload_name; 321 (*receive_configs)[0].decoders[0].decoder = &fake_decoder_; 322 } 323 324 void RenderFrame(const VideoFrame& video_frame, 325 int time_to_render_ms) override { 326 const int kRequiredFrames = 500; 327 if (++frame_counter_ == kRequiredFrames) 328 observation_complete_.Set(); 329 } 330 331 bool IsTextureSupported() const override { return false; } 332 333 private: 334 test::FakeH264Decoder fake_decoder_; 335 test::FakeH264Encoder fake_encoder_; 336 int frame_counter_; 337 } test; 338 339 RunBaseTest(&test); 340 } 341 342 TEST_F(EndToEndTest, ReceiverUsesLocalSsrc) { 343 class SyncRtcpObserver : public test::EndToEndTest { 344 public: 345 SyncRtcpObserver() : EndToEndTest(kDefaultTimeoutMs) {} 346 347 Action OnReceiveRtcp(const uint8_t* packet, size_t length) override { 348 RTCPUtility::RTCPParserV2 parser(packet, length, true); 349 EXPECT_TRUE(parser.IsValid()); 350 uint32_t ssrc = 0; 351 ssrc |= static_cast<uint32_t>(packet[4]) << 24; 352 ssrc |= static_cast<uint32_t>(packet[5]) << 16; 353 ssrc |= static_cast<uint32_t>(packet[6]) << 8; 354 ssrc |= static_cast<uint32_t>(packet[7]) << 0; 355 EXPECT_EQ(kReceiverLocalVideoSsrc, ssrc); 356 observation_complete_.Set(); 357 358 return SEND_PACKET; 359 } 360 361 void PerformTest() override { 362 EXPECT_TRUE(Wait()) 363 << "Timed out while waiting for a receiver RTCP packet to be sent."; 364 } 365 } test; 366 367 RunBaseTest(&test); 368 } 369 370 TEST_F(EndToEndTest, ReceivesAndRetransmitsNack) { 371 static const int kNumberOfNacksToObserve = 2; 372 static const int kLossBurstSize = 2; 373 static const int kPacketsBetweenLossBursts = 9; 374 class NackObserver : public test::EndToEndTest { 375 public: 376 NackObserver() 377 : EndToEndTest(kLongTimeoutMs), 378 sent_rtp_packets_(0), 379 packets_left_to_drop_(0), 380 nacks_left_(kNumberOfNacksToObserve) {} 381 382 private: 383 Action OnSendRtp(const uint8_t* packet, size_t length) override { 384 rtc::CritScope lock(&crit_); 385 RTPHeader header; 386 EXPECT_TRUE(parser_->Parse(packet, length, &header)); 387 388 // Never drop retransmitted packets. 389 if (dropped_packets_.find(header.sequenceNumber) != 390 dropped_packets_.end()) { 391 retransmitted_packets_.insert(header.sequenceNumber); 392 if (nacks_left_ <= 0 && 393 retransmitted_packets_.size() == dropped_packets_.size()) { 394 observation_complete_.Set(); 395 } 396 return SEND_PACKET; 397 } 398 399 ++sent_rtp_packets_; 400 401 // Enough NACKs received, stop dropping packets. 402 if (nacks_left_ <= 0) 403 return SEND_PACKET; 404 405 // Check if it's time for a new loss burst. 406 if (sent_rtp_packets_ % kPacketsBetweenLossBursts == 0) 407 packets_left_to_drop_ = kLossBurstSize; 408 409 // Never drop padding packets as those won't be retransmitted. 410 if (packets_left_to_drop_ > 0 && header.paddingLength == 0) { 411 --packets_left_to_drop_; 412 dropped_packets_.insert(header.sequenceNumber); 413 return DROP_PACKET; 414 } 415 416 return SEND_PACKET; 417 } 418 419 Action OnReceiveRtcp(const uint8_t* packet, size_t length) override { 420 rtc::CritScope lock(&crit_); 421 RTCPUtility::RTCPParserV2 parser(packet, length, true); 422 EXPECT_TRUE(parser.IsValid()); 423 424 RTCPUtility::RTCPPacketTypes packet_type = parser.Begin(); 425 while (packet_type != RTCPUtility::RTCPPacketTypes::kInvalid) { 426 if (packet_type == RTCPUtility::RTCPPacketTypes::kRtpfbNack) { 427 --nacks_left_; 428 break; 429 } 430 packet_type = parser.Iterate(); 431 } 432 return SEND_PACKET; 433 } 434 435 void ModifyVideoConfigs( 436 VideoSendStream::Config* send_config, 437 std::vector<VideoReceiveStream::Config>* receive_configs, 438 VideoEncoderConfig* encoder_config) override { 439 send_config->rtp.nack.rtp_history_ms = kNackRtpHistoryMs; 440 (*receive_configs)[0].rtp.nack.rtp_history_ms = kNackRtpHistoryMs; 441 } 442 443 void PerformTest() override { 444 EXPECT_TRUE(Wait()) 445 << "Timed out waiting for packets to be NACKed, retransmitted and " 446 "rendered."; 447 } 448 449 rtc::CriticalSection crit_; 450 std::set<uint16_t> dropped_packets_; 451 std::set<uint16_t> retransmitted_packets_; 452 uint64_t sent_rtp_packets_; 453 int packets_left_to_drop_; 454 int nacks_left_ GUARDED_BY(&crit_); 455 } test; 456 457 RunBaseTest(&test); 458 } 459 460 TEST_F(EndToEndTest, CanReceiveFec) { 461 class FecRenderObserver : public test::EndToEndTest, public VideoRenderer { 462 public: 463 FecRenderObserver() 464 : EndToEndTest(kDefaultTimeoutMs), state_(kFirstPacket) {} 465 466 private: 467 Action OnSendRtp(const uint8_t* packet, size_t length) override { 468 rtc::CritScope lock(&crit_); 469 RTPHeader header; 470 EXPECT_TRUE(parser_->Parse(packet, length, &header)); 471 472 int encapsulated_payload_type = -1; 473 if (header.payloadType == kRedPayloadType) { 474 encapsulated_payload_type = 475 static_cast<int>(packet[header.headerLength]); 476 if (encapsulated_payload_type != kFakeVideoSendPayloadType) 477 EXPECT_EQ(kUlpfecPayloadType, encapsulated_payload_type); 478 } else { 479 EXPECT_EQ(kFakeVideoSendPayloadType, header.payloadType); 480 } 481 482 if (protected_sequence_numbers_.count(header.sequenceNumber) != 0) { 483 // Retransmitted packet, should not count. 484 protected_sequence_numbers_.erase(header.sequenceNumber); 485 EXPECT_GT(protected_timestamps_.count(header.timestamp), 0u); 486 protected_timestamps_.erase(header.timestamp); 487 return SEND_PACKET; 488 } 489 490 switch (state_) { 491 case kFirstPacket: 492 state_ = kDropEveryOtherPacketUntilFec; 493 break; 494 case kDropEveryOtherPacketUntilFec: 495 if (encapsulated_payload_type == kUlpfecPayloadType) { 496 state_ = kDropNextMediaPacket; 497 return SEND_PACKET; 498 } 499 if (header.sequenceNumber % 2 == 0) 500 return DROP_PACKET; 501 break; 502 case kDropNextMediaPacket: 503 if (encapsulated_payload_type == kFakeVideoSendPayloadType) { 504 protected_sequence_numbers_.insert(header.sequenceNumber); 505 protected_timestamps_.insert(header.timestamp); 506 state_ = kDropEveryOtherPacketUntilFec; 507 return DROP_PACKET; 508 } 509 break; 510 } 511 512 return SEND_PACKET; 513 } 514 515 void RenderFrame(const VideoFrame& video_frame, 516 int time_to_render_ms) override { 517 rtc::CritScope lock(&crit_); 518 // Rendering frame with timestamp of packet that was dropped -> FEC 519 // protection worked. 520 if (protected_timestamps_.count(video_frame.timestamp()) != 0) 521 observation_complete_.Set(); 522 } 523 524 bool IsTextureSupported() const override { return false; } 525 526 enum { 527 kFirstPacket, 528 kDropEveryOtherPacketUntilFec, 529 kDropNextMediaPacket, 530 } state_; 531 532 void ModifyVideoConfigs( 533 VideoSendStream::Config* send_config, 534 std::vector<VideoReceiveStream::Config>* receive_configs, 535 VideoEncoderConfig* encoder_config) override { 536 // TODO(pbos): Run this test with combined NACK/FEC enabled as well. 537 // int rtp_history_ms = 1000; 538 // (*receive_configs)[0].rtp.nack.rtp_history_ms = rtp_history_ms; 539 // send_config->rtp.nack.rtp_history_ms = rtp_history_ms; 540 send_config->rtp.fec.red_payload_type = kRedPayloadType; 541 send_config->rtp.fec.ulpfec_payload_type = kUlpfecPayloadType; 542 543 (*receive_configs)[0].rtp.fec.red_payload_type = kRedPayloadType; 544 (*receive_configs)[0].rtp.fec.ulpfec_payload_type = kUlpfecPayloadType; 545 (*receive_configs)[0].renderer = this; 546 } 547 548 void PerformTest() override { 549 EXPECT_TRUE(Wait()) 550 << "Timed out waiting for dropped frames frames to be rendered."; 551 } 552 553 rtc::CriticalSection crit_; 554 std::set<uint32_t> protected_sequence_numbers_ GUARDED_BY(crit_); 555 std::set<uint32_t> protected_timestamps_ GUARDED_BY(crit_); 556 } test; 557 558 RunBaseTest(&test); 559 } 560 561 // Flacky on all platforms. See webrtc:4328. 562 TEST_F(EndToEndTest, DISABLED_ReceivedFecPacketsNotNacked) { 563 class FecNackObserver : public test::EndToEndTest { 564 public: 565 FecNackObserver() 566 : EndToEndTest(kDefaultTimeoutMs), 567 state_(kFirstPacket), 568 fec_sequence_number_(0), 569 has_last_sequence_number_(false), 570 last_sequence_number_(0) {} 571 572 private: 573 Action OnSendRtp(const uint8_t* packet, size_t length) override { 574 rtc::CritScope lock_(&crit_); 575 RTPHeader header; 576 EXPECT_TRUE(parser_->Parse(packet, length, &header)); 577 578 int encapsulated_payload_type = -1; 579 if (header.payloadType == kRedPayloadType) { 580 encapsulated_payload_type = 581 static_cast<int>(packet[header.headerLength]); 582 if (encapsulated_payload_type != kFakeVideoSendPayloadType) 583 EXPECT_EQ(kUlpfecPayloadType, encapsulated_payload_type); 584 } else { 585 EXPECT_EQ(kFakeVideoSendPayloadType, header.payloadType); 586 } 587 588 if (has_last_sequence_number_ && 589 !IsNewerSequenceNumber(header.sequenceNumber, 590 last_sequence_number_)) { 591 // Drop retransmitted packets. 592 return DROP_PACKET; 593 } 594 last_sequence_number_ = header.sequenceNumber; 595 has_last_sequence_number_ = true; 596 597 bool fec_packet = encapsulated_payload_type == kUlpfecPayloadType; 598 switch (state_) { 599 case kFirstPacket: 600 state_ = kDropEveryOtherPacketUntilFec; 601 break; 602 case kDropEveryOtherPacketUntilFec: 603 if (fec_packet) { 604 state_ = kDropAllMediaPacketsUntilFec; 605 } else if (header.sequenceNumber % 2 == 0) { 606 return DROP_PACKET; 607 } 608 break; 609 case kDropAllMediaPacketsUntilFec: 610 if (!fec_packet) 611 return DROP_PACKET; 612 fec_sequence_number_ = header.sequenceNumber; 613 state_ = kVerifyFecPacketNotInNackList; 614 break; 615 case kVerifyFecPacketNotInNackList: 616 // Continue to drop packets. Make sure no frame can be decoded. 617 if (fec_packet || header.sequenceNumber % 2 == 0) 618 return DROP_PACKET; 619 break; 620 } 621 return SEND_PACKET; 622 } 623 624 Action OnReceiveRtcp(const uint8_t* packet, size_t length) override { 625 rtc::CritScope lock_(&crit_); 626 if (state_ == kVerifyFecPacketNotInNackList) { 627 test::RtcpPacketParser rtcp_parser; 628 rtcp_parser.Parse(packet, length); 629 std::vector<uint16_t> nacks = rtcp_parser.nack_item()->last_nack_list(); 630 if (!nacks.empty() && 631 IsNewerSequenceNumber(nacks.back(), fec_sequence_number_)) { 632 EXPECT_TRUE(std::find( 633 nacks.begin(), nacks.end(), fec_sequence_number_) == nacks.end()); 634 observation_complete_.Set(); 635 } 636 } 637 return SEND_PACKET; 638 } 639 640 test::PacketTransport* CreateSendTransport(Call* sender_call) override { 641 // At low RTT (< kLowRttNackMs) -> NACK only, no FEC. 642 // Configure some network delay. 643 const int kNetworkDelayMs = 50; 644 FakeNetworkPipe::Config config; 645 config.queue_delay_ms = kNetworkDelayMs; 646 return new test::PacketTransport(sender_call, this, 647 test::PacketTransport::kSender, config); 648 } 649 650 // TODO(holmer): Investigate why we don't send FEC packets when the bitrate 651 // is 10 kbps. 652 Call::Config GetSenderCallConfig() override { 653 Call::Config config; 654 const int kMinBitrateBps = 30000; 655 config.bitrate_config.min_bitrate_bps = kMinBitrateBps; 656 return config; 657 } 658 659 void ModifyVideoConfigs( 660 VideoSendStream::Config* send_config, 661 std::vector<VideoReceiveStream::Config>* receive_configs, 662 VideoEncoderConfig* encoder_config) override { 663 // Configure hybrid NACK/FEC. 664 send_config->rtp.nack.rtp_history_ms = kNackRtpHistoryMs; 665 send_config->rtp.fec.red_payload_type = kRedPayloadType; 666 send_config->rtp.fec.ulpfec_payload_type = kUlpfecPayloadType; 667 (*receive_configs)[0].rtp.nack.rtp_history_ms = kNackRtpHistoryMs; 668 (*receive_configs)[0].rtp.fec.red_payload_type = kRedPayloadType; 669 (*receive_configs)[0].rtp.fec.ulpfec_payload_type = kUlpfecPayloadType; 670 } 671 672 void PerformTest() override { 673 EXPECT_TRUE(Wait()) 674 << "Timed out while waiting for FEC packets to be received."; 675 } 676 677 enum { 678 kFirstPacket, 679 kDropEveryOtherPacketUntilFec, 680 kDropAllMediaPacketsUntilFec, 681 kVerifyFecPacketNotInNackList, 682 } state_; 683 684 rtc::CriticalSection crit_; 685 uint16_t fec_sequence_number_ GUARDED_BY(&crit_); 686 bool has_last_sequence_number_; 687 uint16_t last_sequence_number_; 688 } test; 689 690 RunBaseTest(&test); 691 } 692 693 // This test drops second RTP packet with a marker bit set, makes sure it's 694 // retransmitted and renders. Retransmission SSRCs are also checked. 695 void EndToEndTest::DecodesRetransmittedFrame(bool use_rtx, bool use_red) { 696 // Must be set high enough to allow the bitrate probing to finish. 697 static const int kMinProbePackets = 30; 698 static const int kDroppedFrameNumber = kMinProbePackets + 1; 699 class RetransmissionObserver : public test::EndToEndTest, 700 public I420FrameCallback { 701 public: 702 explicit RetransmissionObserver(bool use_rtx, bool use_red) 703 : EndToEndTest(kDefaultTimeoutMs), 704 payload_type_(GetPayloadType(false, use_red)), 705 retransmission_ssrc_(use_rtx ? kSendRtxSsrcs[0] : kVideoSendSsrcs[0]), 706 retransmission_payload_type_(GetPayloadType(use_rtx, use_red)), 707 marker_bits_observed_(0), 708 num_packets_observed_(0), 709 retransmitted_timestamp_(0), 710 frame_retransmitted_(false) {} 711 712 private: 713 Action OnSendRtp(const uint8_t* packet, size_t length) override { 714 rtc::CritScope lock(&crit_); 715 RTPHeader header; 716 EXPECT_TRUE(parser_->Parse(packet, length, &header)); 717 718 // We accept some padding or RTX packets in the beginning to enable 719 // bitrate probing. 720 if (num_packets_observed_++ < kMinProbePackets && 721 header.payloadType != payload_type_) { 722 EXPECT_TRUE(retransmission_payload_type_ == header.payloadType || 723 length == header.headerLength + header.paddingLength); 724 return SEND_PACKET; 725 } 726 if (header.timestamp == retransmitted_timestamp_) { 727 EXPECT_EQ(retransmission_ssrc_, header.ssrc); 728 EXPECT_EQ(retransmission_payload_type_, header.payloadType); 729 frame_retransmitted_ = true; 730 return SEND_PACKET; 731 } 732 733 EXPECT_EQ(kVideoSendSsrcs[0], header.ssrc); 734 EXPECT_EQ(payload_type_, header.payloadType); 735 736 // Found the final packet of the frame to inflict loss to, drop this and 737 // expect a retransmission. 738 if (header.markerBit && ++marker_bits_observed_ == kDroppedFrameNumber) { 739 retransmitted_timestamp_ = header.timestamp; 740 return DROP_PACKET; 741 } 742 743 return SEND_PACKET; 744 } 745 746 void FrameCallback(VideoFrame* frame) override { 747 rtc::CritScope lock(&crit_); 748 if (frame->timestamp() == retransmitted_timestamp_) { 749 EXPECT_TRUE(frame_retransmitted_); 750 observation_complete_.Set(); 751 } 752 } 753 754 void ModifyVideoConfigs( 755 VideoSendStream::Config* send_config, 756 std::vector<VideoReceiveStream::Config>* receive_configs, 757 VideoEncoderConfig* encoder_config) override { 758 send_config->rtp.nack.rtp_history_ms = kNackRtpHistoryMs; 759 (*receive_configs)[0].pre_render_callback = this; 760 (*receive_configs)[0].rtp.nack.rtp_history_ms = kNackRtpHistoryMs; 761 762 if (payload_type_ == kRedPayloadType) { 763 send_config->rtp.fec.ulpfec_payload_type = kUlpfecPayloadType; 764 send_config->rtp.fec.red_payload_type = kRedPayloadType; 765 (*receive_configs)[0].rtp.fec.red_payload_type = kRedPayloadType; 766 (*receive_configs)[0].rtp.fec.ulpfec_payload_type = kUlpfecPayloadType; 767 } 768 769 if (retransmission_ssrc_ == kSendRtxSsrcs[0]) { 770 send_config->rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[0]); 771 send_config->rtp.rtx.payload_type = kSendRtxPayloadType; 772 (*receive_configs)[0].rtp.rtx[kFakeVideoSendPayloadType].ssrc = 773 kSendRtxSsrcs[0]; 774 (*receive_configs)[0].rtp.rtx[kFakeVideoSendPayloadType].payload_type = 775 kSendRtxPayloadType; 776 } 777 } 778 779 void PerformTest() override { 780 EXPECT_TRUE(Wait()) 781 << "Timed out while waiting for retransmission to render."; 782 } 783 784 int GetPayloadType(bool use_rtx, bool use_red) { 785 return use_rtx ? kSendRtxPayloadType 786 : (use_red ? kRedPayloadType : kFakeVideoSendPayloadType); 787 } 788 789 rtc::CriticalSection crit_; 790 const int payload_type_; 791 const uint32_t retransmission_ssrc_; 792 const int retransmission_payload_type_; 793 int marker_bits_observed_; 794 int num_packets_observed_; 795 uint32_t retransmitted_timestamp_ GUARDED_BY(&crit_); 796 bool frame_retransmitted_; 797 } test(use_rtx, use_red); 798 799 RunBaseTest(&test); 800 } 801 802 TEST_F(EndToEndTest, DecodesRetransmittedFrame) { 803 DecodesRetransmittedFrame(false, false); 804 } 805 806 TEST_F(EndToEndTest, DecodesRetransmittedFrameOverRtx) { 807 DecodesRetransmittedFrame(true, false); 808 } 809 810 TEST_F(EndToEndTest, DecodesRetransmittedFrameByRed) { 811 DecodesRetransmittedFrame(false, true); 812 } 813 814 TEST_F(EndToEndTest, DecodesRetransmittedFrameByRedOverRtx) { 815 DecodesRetransmittedFrame(true, true); 816 } 817 818 TEST_F(EndToEndTest, UsesFrameCallbacks) { 819 static const int kWidth = 320; 820 static const int kHeight = 240; 821 822 class Renderer : public VideoRenderer { 823 public: 824 Renderer() : event_(false, false) {} 825 826 void RenderFrame(const VideoFrame& video_frame, 827 int /*time_to_render_ms*/) override { 828 EXPECT_EQ(0, *video_frame.buffer(kYPlane)) 829 << "Rendered frame should have zero luma which is applied by the " 830 "pre-render callback."; 831 event_.Set(); 832 } 833 834 bool IsTextureSupported() const override { return false; } 835 836 bool Wait() { return event_.Wait(kDefaultTimeoutMs); } 837 rtc::Event event_; 838 } renderer; 839 840 class TestFrameCallback : public I420FrameCallback { 841 public: 842 TestFrameCallback(int expected_luma_byte, int next_luma_byte) 843 : event_(false, false), 844 expected_luma_byte_(expected_luma_byte), 845 next_luma_byte_(next_luma_byte) {} 846 847 bool Wait() { return event_.Wait(kDefaultTimeoutMs); } 848 849 private: 850 virtual void FrameCallback(VideoFrame* frame) { 851 EXPECT_EQ(kWidth, frame->width()) 852 << "Width not as expected, callback done before resize?"; 853 EXPECT_EQ(kHeight, frame->height()) 854 << "Height not as expected, callback done before resize?"; 855 856 // Previous luma specified, observed luma should be fairly close. 857 if (expected_luma_byte_ != -1) { 858 EXPECT_NEAR(expected_luma_byte_, *frame->buffer(kYPlane), 10); 859 } 860 861 memset(frame->buffer(kYPlane), 862 next_luma_byte_, 863 frame->allocated_size(kYPlane)); 864 865 event_.Set(); 866 } 867 868 rtc::Event event_; 869 int expected_luma_byte_; 870 int next_luma_byte_; 871 }; 872 873 TestFrameCallback pre_encode_callback(-1, 255); // Changes luma to 255. 874 TestFrameCallback pre_render_callback(255, 0); // Changes luma from 255 to 0. 875 876 CreateCalls(Call::Config(), Call::Config()); 877 878 test::DirectTransport sender_transport(sender_call_.get()); 879 test::DirectTransport receiver_transport(receiver_call_.get()); 880 sender_transport.SetReceiver(receiver_call_->Receiver()); 881 receiver_transport.SetReceiver(sender_call_->Receiver()); 882 883 CreateSendConfig(1, 0, &sender_transport); 884 rtc::scoped_ptr<VideoEncoder> encoder( 885 VideoEncoder::Create(VideoEncoder::kVp8)); 886 video_send_config_.encoder_settings.encoder = encoder.get(); 887 video_send_config_.encoder_settings.payload_name = "VP8"; 888 ASSERT_EQ(1u, video_encoder_config_.streams.size()) << "Test setup error."; 889 video_encoder_config_.streams[0].width = kWidth; 890 video_encoder_config_.streams[0].height = kHeight; 891 video_send_config_.pre_encode_callback = &pre_encode_callback; 892 893 CreateMatchingReceiveConfigs(&receiver_transport); 894 video_receive_configs_[0].pre_render_callback = &pre_render_callback; 895 video_receive_configs_[0].renderer = &renderer; 896 897 CreateVideoStreams(); 898 Start(); 899 900 // Create frames that are smaller than the send width/height, this is done to 901 // check that the callbacks are done after processing video. 902 rtc::scoped_ptr<test::FrameGenerator> frame_generator( 903 test::FrameGenerator::CreateChromaGenerator(kWidth / 2, kHeight / 2)); 904 video_send_stream_->Input()->IncomingCapturedFrame( 905 *frame_generator->NextFrame()); 906 907 EXPECT_TRUE(pre_encode_callback.Wait()) 908 << "Timed out while waiting for pre-encode callback."; 909 EXPECT_TRUE(pre_render_callback.Wait()) 910 << "Timed out while waiting for pre-render callback."; 911 EXPECT_TRUE(renderer.Wait()) 912 << "Timed out while waiting for the frame to render."; 913 914 Stop(); 915 916 sender_transport.StopSending(); 917 receiver_transport.StopSending(); 918 919 DestroyStreams(); 920 } 921 922 void EndToEndTest::ReceivesPliAndRecovers(int rtp_history_ms) { 923 static const int kPacketsToDrop = 1; 924 925 class PliObserver : public test::EndToEndTest, public VideoRenderer { 926 public: 927 explicit PliObserver(int rtp_history_ms) 928 : EndToEndTest(kLongTimeoutMs), 929 rtp_history_ms_(rtp_history_ms), 930 nack_enabled_(rtp_history_ms > 0), 931 highest_dropped_timestamp_(0), 932 frames_to_drop_(0), 933 received_pli_(false) {} 934 935 private: 936 Action OnSendRtp(const uint8_t* packet, size_t length) override { 937 rtc::CritScope lock(&crit_); 938 RTPHeader header; 939 EXPECT_TRUE(parser_->Parse(packet, length, &header)); 940 941 // Drop all retransmitted packets to force a PLI. 942 if (header.timestamp <= highest_dropped_timestamp_) 943 return DROP_PACKET; 944 945 if (frames_to_drop_ > 0) { 946 highest_dropped_timestamp_ = header.timestamp; 947 --frames_to_drop_; 948 return DROP_PACKET; 949 } 950 951 return SEND_PACKET; 952 } 953 954 Action OnReceiveRtcp(const uint8_t* packet, size_t length) override { 955 rtc::CritScope lock(&crit_); 956 RTCPUtility::RTCPParserV2 parser(packet, length, true); 957 EXPECT_TRUE(parser.IsValid()); 958 959 for (RTCPUtility::RTCPPacketTypes packet_type = parser.Begin(); 960 packet_type != RTCPUtility::RTCPPacketTypes::kInvalid; 961 packet_type = parser.Iterate()) { 962 if (!nack_enabled_) 963 EXPECT_NE(packet_type, RTCPUtility::RTCPPacketTypes::kRtpfbNack); 964 965 if (packet_type == RTCPUtility::RTCPPacketTypes::kPsfbPli) { 966 received_pli_ = true; 967 break; 968 } 969 } 970 return SEND_PACKET; 971 } 972 973 void RenderFrame(const VideoFrame& video_frame, 974 int time_to_render_ms) override { 975 rtc::CritScope lock(&crit_); 976 if (received_pli_ && 977 video_frame.timestamp() > highest_dropped_timestamp_) { 978 observation_complete_.Set(); 979 } 980 if (!received_pli_) 981 frames_to_drop_ = kPacketsToDrop; 982 } 983 984 bool IsTextureSupported() const override { return false; } 985 986 void ModifyVideoConfigs( 987 VideoSendStream::Config* send_config, 988 std::vector<VideoReceiveStream::Config>* receive_configs, 989 VideoEncoderConfig* encoder_config) override { 990 send_config->rtp.nack.rtp_history_ms = rtp_history_ms_; 991 (*receive_configs)[0].rtp.nack.rtp_history_ms = rtp_history_ms_; 992 (*receive_configs)[0].renderer = this; 993 } 994 995 void PerformTest() override { 996 EXPECT_TRUE(Wait()) << "Timed out waiting for PLI to be " 997 "received and a frame to be " 998 "rendered afterwards."; 999 } 1000 1001 rtc::CriticalSection crit_; 1002 int rtp_history_ms_; 1003 bool nack_enabled_; 1004 uint32_t highest_dropped_timestamp_ GUARDED_BY(&crit_); 1005 int frames_to_drop_ GUARDED_BY(&crit_); 1006 bool received_pli_ GUARDED_BY(&crit_); 1007 } test(rtp_history_ms); 1008 1009 RunBaseTest(&test); 1010 } 1011 1012 TEST_F(EndToEndTest, ReceivesPliAndRecoversWithNack) { 1013 ReceivesPliAndRecovers(1000); 1014 } 1015 1016 TEST_F(EndToEndTest, ReceivesPliAndRecoversWithoutNack) { 1017 ReceivesPliAndRecovers(0); 1018 } 1019 1020 TEST_F(EndToEndTest, UnknownRtpPacketGivesUnknownSsrcReturnCode) { 1021 class PacketInputObserver : public PacketReceiver { 1022 public: 1023 explicit PacketInputObserver(PacketReceiver* receiver) 1024 : receiver_(receiver), delivered_packet_(false, false) {} 1025 1026 bool Wait() { return delivered_packet_.Wait(kDefaultTimeoutMs); } 1027 1028 private: 1029 DeliveryStatus DeliverPacket(MediaType media_type, 1030 const uint8_t* packet, 1031 size_t length, 1032 const PacketTime& packet_time) override { 1033 if (RtpHeaderParser::IsRtcp(packet, length)) { 1034 return receiver_->DeliverPacket(media_type, packet, length, 1035 packet_time); 1036 } else { 1037 DeliveryStatus delivery_status = 1038 receiver_->DeliverPacket(media_type, packet, length, packet_time); 1039 EXPECT_EQ(DELIVERY_UNKNOWN_SSRC, delivery_status); 1040 delivered_packet_.Set(); 1041 return delivery_status; 1042 } 1043 } 1044 1045 PacketReceiver* receiver_; 1046 rtc::Event delivered_packet_; 1047 }; 1048 1049 CreateCalls(Call::Config(), Call::Config()); 1050 1051 test::DirectTransport send_transport(sender_call_.get()); 1052 test::DirectTransport receive_transport(receiver_call_.get()); 1053 PacketInputObserver input_observer(receiver_call_->Receiver()); 1054 send_transport.SetReceiver(&input_observer); 1055 receive_transport.SetReceiver(sender_call_->Receiver()); 1056 1057 CreateSendConfig(1, 0, &send_transport); 1058 CreateMatchingReceiveConfigs(&receive_transport); 1059 1060 CreateVideoStreams(); 1061 CreateFrameGeneratorCapturer(); 1062 Start(); 1063 1064 receiver_call_->DestroyVideoReceiveStream(video_receive_streams_[0]); 1065 video_receive_streams_.clear(); 1066 1067 // Wait() waits for a received packet. 1068 EXPECT_TRUE(input_observer.Wait()); 1069 1070 Stop(); 1071 1072 DestroyStreams(); 1073 1074 send_transport.StopSending(); 1075 receive_transport.StopSending(); 1076 } 1077 1078 void EndToEndTest::RespectsRtcpMode(RtcpMode rtcp_mode) { 1079 static const int kNumCompoundRtcpPacketsToObserve = 10; 1080 class RtcpModeObserver : public test::EndToEndTest { 1081 public: 1082 explicit RtcpModeObserver(RtcpMode rtcp_mode) 1083 : EndToEndTest(kDefaultTimeoutMs), 1084 rtcp_mode_(rtcp_mode), 1085 sent_rtp_(0), 1086 sent_rtcp_(0) {} 1087 1088 private: 1089 Action OnSendRtp(const uint8_t* packet, size_t length) override { 1090 if (++sent_rtp_ % 3 == 0) 1091 return DROP_PACKET; 1092 1093 return SEND_PACKET; 1094 } 1095 1096 Action OnReceiveRtcp(const uint8_t* packet, size_t length) override { 1097 ++sent_rtcp_; 1098 RTCPUtility::RTCPParserV2 parser(packet, length, true); 1099 EXPECT_TRUE(parser.IsValid()); 1100 1101 RTCPUtility::RTCPPacketTypes packet_type = parser.Begin(); 1102 bool has_report_block = false; 1103 while (packet_type != RTCPUtility::RTCPPacketTypes::kInvalid) { 1104 EXPECT_NE(RTCPUtility::RTCPPacketTypes::kSr, packet_type); 1105 if (packet_type == RTCPUtility::RTCPPacketTypes::kRr) { 1106 has_report_block = true; 1107 break; 1108 } 1109 packet_type = parser.Iterate(); 1110 } 1111 1112 switch (rtcp_mode_) { 1113 case RtcpMode::kCompound: 1114 if (!has_report_block) { 1115 ADD_FAILURE() << "Received RTCP packet without receiver report for " 1116 "RtcpMode::kCompound."; 1117 observation_complete_.Set(); 1118 } 1119 1120 if (sent_rtcp_ >= kNumCompoundRtcpPacketsToObserve) 1121 observation_complete_.Set(); 1122 1123 break; 1124 case RtcpMode::kReducedSize: 1125 if (!has_report_block) 1126 observation_complete_.Set(); 1127 break; 1128 case RtcpMode::kOff: 1129 RTC_NOTREACHED(); 1130 break; 1131 } 1132 1133 return SEND_PACKET; 1134 } 1135 1136 void ModifyVideoConfigs( 1137 VideoSendStream::Config* send_config, 1138 std::vector<VideoReceiveStream::Config>* receive_configs, 1139 VideoEncoderConfig* encoder_config) override { 1140 send_config->rtp.nack.rtp_history_ms = kNackRtpHistoryMs; 1141 (*receive_configs)[0].rtp.nack.rtp_history_ms = kNackRtpHistoryMs; 1142 (*receive_configs)[0].rtp.rtcp_mode = rtcp_mode_; 1143 } 1144 1145 void PerformTest() override { 1146 EXPECT_TRUE(Wait()) 1147 << (rtcp_mode_ == RtcpMode::kCompound 1148 ? "Timed out before observing enough compound packets." 1149 : "Timed out before receiving a non-compound RTCP packet."); 1150 } 1151 1152 RtcpMode rtcp_mode_; 1153 int sent_rtp_; 1154 int sent_rtcp_; 1155 } test(rtcp_mode); 1156 1157 RunBaseTest(&test); 1158 } 1159 1160 TEST_F(EndToEndTest, UsesRtcpCompoundMode) { 1161 RespectsRtcpMode(RtcpMode::kCompound); 1162 } 1163 1164 TEST_F(EndToEndTest, UsesRtcpReducedSizeMode) { 1165 RespectsRtcpMode(RtcpMode::kReducedSize); 1166 } 1167 1168 // Test sets up a Call multiple senders with different resolutions and SSRCs. 1169 // Another is set up to receive all three of these with different renderers. 1170 class MultiStreamTest { 1171 public: 1172 static const size_t kNumStreams = 3; 1173 struct CodecSettings { 1174 uint32_t ssrc; 1175 int width; 1176 int height; 1177 } codec_settings[kNumStreams]; 1178 1179 MultiStreamTest() { 1180 // TODO(sprang): Cleanup when msvc supports explicit initializers for array. 1181 codec_settings[0] = {1, 640, 480}; 1182 codec_settings[1] = {2, 320, 240}; 1183 codec_settings[2] = {3, 240, 160}; 1184 } 1185 1186 virtual ~MultiStreamTest() {} 1187 1188 void RunTest() { 1189 rtc::scoped_ptr<Call> sender_call(Call::Create(Call::Config())); 1190 rtc::scoped_ptr<Call> receiver_call(Call::Create(Call::Config())); 1191 rtc::scoped_ptr<test::DirectTransport> sender_transport( 1192 CreateSendTransport(sender_call.get())); 1193 rtc::scoped_ptr<test::DirectTransport> receiver_transport( 1194 CreateReceiveTransport(receiver_call.get())); 1195 sender_transport->SetReceiver(receiver_call->Receiver()); 1196 receiver_transport->SetReceiver(sender_call->Receiver()); 1197 1198 rtc::scoped_ptr<VideoEncoder> encoders[kNumStreams]; 1199 for (size_t i = 0; i < kNumStreams; ++i) 1200 encoders[i].reset(VideoEncoder::Create(VideoEncoder::kVp8)); 1201 1202 VideoSendStream* send_streams[kNumStreams]; 1203 VideoReceiveStream* receive_streams[kNumStreams]; 1204 1205 test::FrameGeneratorCapturer* frame_generators[kNumStreams]; 1206 ScopedVector<VideoDecoder> allocated_decoders; 1207 for (size_t i = 0; i < kNumStreams; ++i) { 1208 uint32_t ssrc = codec_settings[i].ssrc; 1209 int width = codec_settings[i].width; 1210 int height = codec_settings[i].height; 1211 1212 VideoSendStream::Config send_config(sender_transport.get()); 1213 send_config.rtp.ssrcs.push_back(ssrc); 1214 send_config.encoder_settings.encoder = encoders[i].get(); 1215 send_config.encoder_settings.payload_name = "VP8"; 1216 send_config.encoder_settings.payload_type = 124; 1217 VideoEncoderConfig encoder_config; 1218 encoder_config.streams = test::CreateVideoStreams(1); 1219 VideoStream* stream = &encoder_config.streams[0]; 1220 stream->width = width; 1221 stream->height = height; 1222 stream->max_framerate = 5; 1223 stream->min_bitrate_bps = stream->target_bitrate_bps = 1224 stream->max_bitrate_bps = 100000; 1225 1226 UpdateSendConfig(i, &send_config, &encoder_config, &frame_generators[i]); 1227 1228 send_streams[i] = 1229 sender_call->CreateVideoSendStream(send_config, encoder_config); 1230 send_streams[i]->Start(); 1231 1232 VideoReceiveStream::Config receive_config(receiver_transport.get()); 1233 receive_config.rtp.remote_ssrc = ssrc; 1234 receive_config.rtp.local_ssrc = test::CallTest::kReceiverLocalVideoSsrc; 1235 VideoReceiveStream::Decoder decoder = 1236 test::CreateMatchingDecoder(send_config.encoder_settings); 1237 allocated_decoders.push_back(decoder.decoder); 1238 receive_config.decoders.push_back(decoder); 1239 1240 UpdateReceiveConfig(i, &receive_config); 1241 1242 receive_streams[i] = 1243 receiver_call->CreateVideoReceiveStream(receive_config); 1244 receive_streams[i]->Start(); 1245 1246 frame_generators[i] = test::FrameGeneratorCapturer::Create( 1247 send_streams[i]->Input(), width, height, 30, 1248 Clock::GetRealTimeClock()); 1249 frame_generators[i]->Start(); 1250 } 1251 1252 Wait(); 1253 1254 for (size_t i = 0; i < kNumStreams; ++i) { 1255 frame_generators[i]->Stop(); 1256 sender_call->DestroyVideoSendStream(send_streams[i]); 1257 receiver_call->DestroyVideoReceiveStream(receive_streams[i]); 1258 delete frame_generators[i]; 1259 } 1260 1261 sender_transport->StopSending(); 1262 receiver_transport->StopSending(); 1263 } 1264 1265 protected: 1266 virtual void Wait() = 0; 1267 // Note: frame_generator is a point-to-pointer, since the actual instance 1268 // hasn't been created at the time of this call. Only when packets/frames 1269 // start flowing should this be dereferenced. 1270 virtual void UpdateSendConfig( 1271 size_t stream_index, 1272 VideoSendStream::Config* send_config, 1273 VideoEncoderConfig* encoder_config, 1274 test::FrameGeneratorCapturer** frame_generator) {} 1275 virtual void UpdateReceiveConfig(size_t stream_index, 1276 VideoReceiveStream::Config* receive_config) { 1277 } 1278 virtual test::DirectTransport* CreateSendTransport(Call* sender_call) { 1279 return new test::DirectTransport(sender_call); 1280 } 1281 virtual test::DirectTransport* CreateReceiveTransport(Call* receiver_call) { 1282 return new test::DirectTransport(receiver_call); 1283 } 1284 }; 1285 1286 // Each renderer verifies that it receives the expected resolution, and as soon 1287 // as every renderer has received a frame, the test finishes. 1288 TEST_F(EndToEndTest, SendsAndReceivesMultipleStreams) { 1289 class VideoOutputObserver : public VideoRenderer { 1290 public: 1291 VideoOutputObserver(const MultiStreamTest::CodecSettings& settings, 1292 uint32_t ssrc, 1293 test::FrameGeneratorCapturer** frame_generator) 1294 : settings_(settings), 1295 ssrc_(ssrc), 1296 frame_generator_(frame_generator), 1297 done_(false, false) {} 1298 1299 void RenderFrame(const VideoFrame& video_frame, 1300 int time_to_render_ms) override { 1301 EXPECT_EQ(settings_.width, video_frame.width()); 1302 EXPECT_EQ(settings_.height, video_frame.height()); 1303 (*frame_generator_)->Stop(); 1304 done_.Set(); 1305 } 1306 1307 uint32_t Ssrc() { return ssrc_; } 1308 1309 bool IsTextureSupported() const override { return false; } 1310 1311 bool Wait() { return done_.Wait(kDefaultTimeoutMs); } 1312 1313 private: 1314 const MultiStreamTest::CodecSettings& settings_; 1315 const uint32_t ssrc_; 1316 test::FrameGeneratorCapturer** const frame_generator_; 1317 rtc::Event done_; 1318 }; 1319 1320 class Tester : public MultiStreamTest { 1321 public: 1322 Tester() {} 1323 virtual ~Tester() {} 1324 1325 protected: 1326 void Wait() override { 1327 for (const auto& observer : observers_) { 1328 EXPECT_TRUE(observer->Wait()) << "Time out waiting for from on ssrc " 1329 << observer->Ssrc(); 1330 } 1331 } 1332 1333 void UpdateSendConfig( 1334 size_t stream_index, 1335 VideoSendStream::Config* send_config, 1336 VideoEncoderConfig* encoder_config, 1337 test::FrameGeneratorCapturer** frame_generator) override { 1338 observers_[stream_index].reset(new VideoOutputObserver( 1339 codec_settings[stream_index], send_config->rtp.ssrcs.front(), 1340 frame_generator)); 1341 } 1342 1343 void UpdateReceiveConfig( 1344 size_t stream_index, 1345 VideoReceiveStream::Config* receive_config) override { 1346 receive_config->renderer = observers_[stream_index].get(); 1347 } 1348 1349 private: 1350 rtc::scoped_ptr<VideoOutputObserver> observers_[kNumStreams]; 1351 } tester; 1352 1353 tester.RunTest(); 1354 } 1355 1356 TEST_F(EndToEndTest, AssignsTransportSequenceNumbers) { 1357 static const int kExtensionId = 5; 1358 1359 class RtpExtensionHeaderObserver : public test::DirectTransport { 1360 public: 1361 RtpExtensionHeaderObserver(Call* sender_call, 1362 const uint32_t& first_media_ssrc, 1363 const std::map<uint32_t, uint32_t>& ssrc_map) 1364 : DirectTransport(sender_call), 1365 done_(false, false), 1366 parser_(RtpHeaderParser::Create()), 1367 first_media_ssrc_(first_media_ssrc), 1368 rtx_to_media_ssrcs_(ssrc_map), 1369 padding_observed_(false), 1370 rtx_padding_observed_(false), 1371 retransmit_observed_(false), 1372 started_(false) { 1373 parser_->RegisterRtpHeaderExtension(kRtpExtensionTransportSequenceNumber, 1374 kExtensionId); 1375 } 1376 virtual ~RtpExtensionHeaderObserver() {} 1377 1378 bool SendRtp(const uint8_t* data, 1379 size_t length, 1380 const PacketOptions& options) override { 1381 { 1382 rtc::CritScope cs(&lock_); 1383 1384 if (IsDone()) 1385 return false; 1386 1387 if (started_) { 1388 RTPHeader header; 1389 EXPECT_TRUE(parser_->Parse(data, length, &header)); 1390 bool drop_packet = false; 1391 1392 EXPECT_TRUE(header.extension.hasTransportSequenceNumber); 1393 EXPECT_EQ(options.packet_id, 1394 header.extension.transportSequenceNumber); 1395 if (!streams_observed_.empty()) { 1396 // Unwrap packet id and verify uniqueness. 1397 int64_t packet_id = unwrapper_.Unwrap(options.packet_id); 1398 EXPECT_TRUE(received_packed_ids_.insert(packet_id).second); 1399 } 1400 1401 // Drop (up to) every 17th packet, so we get retransmits. 1402 // Only drop media, and not on the first stream (otherwise it will be 1403 // hard to distinguish from padding, which is always sent on the first 1404 // stream). 1405 if (header.payloadType != kSendRtxPayloadType && 1406 header.ssrc != first_media_ssrc_ && 1407 header.extension.transportSequenceNumber % 17 == 0) { 1408 dropped_seq_[header.ssrc].insert(header.sequenceNumber); 1409 drop_packet = true; 1410 } 1411 1412 size_t payload_length = 1413 length - (header.headerLength + header.paddingLength); 1414 if (payload_length == 0) { 1415 padding_observed_ = true; 1416 } else if (header.payloadType == kSendRtxPayloadType) { 1417 uint16_t original_sequence_number = 1418 ByteReader<uint16_t>::ReadBigEndian(&data[header.headerLength]); 1419 uint32_t original_ssrc = 1420 rtx_to_media_ssrcs_.find(header.ssrc)->second; 1421 std::set<uint16_t>* seq_no_map = &dropped_seq_[original_ssrc]; 1422 auto it = seq_no_map->find(original_sequence_number); 1423 if (it != seq_no_map->end()) { 1424 retransmit_observed_ = true; 1425 seq_no_map->erase(it); 1426 } else { 1427 rtx_padding_observed_ = true; 1428 } 1429 } else { 1430 streams_observed_.insert(header.ssrc); 1431 } 1432 1433 if (IsDone()) 1434 done_.Set(); 1435 1436 if (drop_packet) 1437 return true; 1438 } 1439 } 1440 1441 return test::DirectTransport::SendRtp(data, length, options); 1442 } 1443 1444 bool IsDone() { 1445 bool observed_types_ok = 1446 streams_observed_.size() == MultiStreamTest::kNumStreams && 1447 padding_observed_ && retransmit_observed_ && rtx_padding_observed_; 1448 if (!observed_types_ok) 1449 return false; 1450 // We should not have any gaps in the sequence number range. 1451 size_t seqno_range = 1452 *received_packed_ids_.rbegin() - *received_packed_ids_.begin() + 1; 1453 return seqno_range == received_packed_ids_.size(); 1454 } 1455 1456 bool Wait() { 1457 { 1458 // Can't be sure until this point that rtx_to_media_ssrcs_ etc have 1459 // been initialized and are OK to read. 1460 rtc::CritScope cs(&lock_); 1461 started_ = true; 1462 } 1463 return done_.Wait(kDefaultTimeoutMs); 1464 } 1465 1466 rtc::CriticalSection lock_; 1467 rtc::Event done_; 1468 rtc::scoped_ptr<RtpHeaderParser> parser_; 1469 SequenceNumberUnwrapper unwrapper_; 1470 std::set<int64_t> received_packed_ids_; 1471 std::set<uint32_t> streams_observed_; 1472 std::map<uint32_t, std::set<uint16_t>> dropped_seq_; 1473 const uint32_t& first_media_ssrc_; 1474 const std::map<uint32_t, uint32_t>& rtx_to_media_ssrcs_; 1475 bool padding_observed_; 1476 bool rtx_padding_observed_; 1477 bool retransmit_observed_; 1478 bool started_; 1479 }; 1480 1481 class TransportSequenceNumberTester : public MultiStreamTest { 1482 public: 1483 TransportSequenceNumberTester() 1484 : first_media_ssrc_(0), observer_(nullptr) {} 1485 virtual ~TransportSequenceNumberTester() {} 1486 1487 protected: 1488 void Wait() override { 1489 RTC_DCHECK(observer_ != nullptr); 1490 EXPECT_TRUE(observer_->Wait()); 1491 } 1492 1493 void UpdateSendConfig( 1494 size_t stream_index, 1495 VideoSendStream::Config* send_config, 1496 VideoEncoderConfig* encoder_config, 1497 test::FrameGeneratorCapturer** frame_generator) override { 1498 send_config->rtp.extensions.clear(); 1499 send_config->rtp.extensions.push_back( 1500 RtpExtension(RtpExtension::kTransportSequenceNumber, kExtensionId)); 1501 1502 // Force some padding to be sent. 1503 const int kPaddingBitrateBps = 50000; 1504 int total_target_bitrate = 0; 1505 for (const VideoStream& stream : encoder_config->streams) 1506 total_target_bitrate += stream.target_bitrate_bps; 1507 encoder_config->min_transmit_bitrate_bps = 1508 total_target_bitrate + kPaddingBitrateBps; 1509 1510 // Configure RTX for redundant payload padding. 1511 send_config->rtp.nack.rtp_history_ms = kNackRtpHistoryMs; 1512 send_config->rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[stream_index]); 1513 send_config->rtp.rtx.payload_type = kSendRtxPayloadType; 1514 rtx_to_media_ssrcs_[kSendRtxSsrcs[stream_index]] = 1515 send_config->rtp.ssrcs[0]; 1516 1517 if (stream_index == 0) 1518 first_media_ssrc_ = send_config->rtp.ssrcs[0]; 1519 } 1520 1521 void UpdateReceiveConfig( 1522 size_t stream_index, 1523 VideoReceiveStream::Config* receive_config) override { 1524 receive_config->rtp.nack.rtp_history_ms = kNackRtpHistoryMs; 1525 receive_config->rtp.extensions.clear(); 1526 receive_config->rtp.extensions.push_back( 1527 RtpExtension(RtpExtension::kTransportSequenceNumber, kExtensionId)); 1528 } 1529 1530 test::DirectTransport* CreateSendTransport(Call* sender_call) override { 1531 observer_ = new RtpExtensionHeaderObserver(sender_call, first_media_ssrc_, 1532 rtx_to_media_ssrcs_); 1533 return observer_; 1534 } 1535 1536 private: 1537 uint32_t first_media_ssrc_; 1538 std::map<uint32_t, uint32_t> rtx_to_media_ssrcs_; 1539 RtpExtensionHeaderObserver* observer_; 1540 } tester; 1541 1542 tester.RunTest(); 1543 } 1544 1545 class TransportFeedbackTester : public test::EndToEndTest { 1546 public: 1547 explicit TransportFeedbackTester(bool feedback_enabled, 1548 size_t num_video_streams, 1549 size_t num_audio_streams) 1550 : EndToEndTest(::webrtc::EndToEndTest::kDefaultTimeoutMs), 1551 feedback_enabled_(feedback_enabled), 1552 num_video_streams_(num_video_streams), 1553 num_audio_streams_(num_audio_streams) { 1554 // Only one stream of each supported for now. 1555 EXPECT_LE(num_video_streams, 1u); 1556 EXPECT_LE(num_audio_streams, 1u); 1557 } 1558 1559 protected: 1560 Action OnSendRtcp(const uint8_t* data, size_t length) override { 1561 EXPECT_FALSE(HasTransportFeedback(data, length)); 1562 return SEND_PACKET; 1563 } 1564 1565 Action OnReceiveRtcp(const uint8_t* data, size_t length) override { 1566 if (HasTransportFeedback(data, length)) 1567 observation_complete_.Set(); 1568 return SEND_PACKET; 1569 } 1570 1571 bool HasTransportFeedback(const uint8_t* data, size_t length) const { 1572 RTCPUtility::RTCPParserV2 parser(data, length, true); 1573 EXPECT_TRUE(parser.IsValid()); 1574 1575 RTCPUtility::RTCPPacketTypes packet_type = parser.Begin(); 1576 while (packet_type != RTCPUtility::RTCPPacketTypes::kInvalid) { 1577 if (packet_type == RTCPUtility::RTCPPacketTypes::kTransportFeedback) 1578 return true; 1579 packet_type = parser.Iterate(); 1580 } 1581 1582 return false; 1583 } 1584 1585 void PerformTest() override { 1586 const int64_t kDisabledFeedbackTimeoutMs = 5000; 1587 EXPECT_EQ(feedback_enabled_, 1588 observation_complete_.Wait(feedback_enabled_ 1589 ? test::CallTest::kDefaultTimeoutMs 1590 : kDisabledFeedbackTimeoutMs)); 1591 } 1592 1593 void OnCallsCreated(Call* sender_call, Call* receiver_call) override { 1594 receiver_call_ = receiver_call; 1595 } 1596 1597 size_t GetNumVideoStreams() const override { return num_video_streams_; } 1598 size_t GetNumAudioStreams() const override { return num_audio_streams_; } 1599 1600 void ModifyVideoConfigs( 1601 VideoSendStream::Config* send_config, 1602 std::vector<VideoReceiveStream::Config>* receive_configs, 1603 VideoEncoderConfig* encoder_config) override { 1604 send_config->rtp.extensions.clear(); 1605 send_config->rtp.extensions.push_back( 1606 RtpExtension(RtpExtension::kTransportSequenceNumber, kExtensionId)); 1607 (*receive_configs)[0].rtp.extensions = send_config->rtp.extensions; 1608 (*receive_configs)[0].rtp.transport_cc = feedback_enabled_; 1609 } 1610 1611 void ModifyAudioConfigs( 1612 AudioSendStream::Config* send_config, 1613 std::vector<AudioReceiveStream::Config>* receive_configs) override { 1614 send_config->rtp.extensions.clear(); 1615 send_config->rtp.extensions.push_back( 1616 RtpExtension(RtpExtension::kTransportSequenceNumber, kExtensionId)); 1617 (*receive_configs)[0].rtp.extensions.clear(); 1618 (*receive_configs)[0].rtp.extensions = send_config->rtp.extensions; 1619 (*receive_configs)[0].rtp.transport_cc = feedback_enabled_; 1620 (*receive_configs)[0].combined_audio_video_bwe = true; 1621 } 1622 1623 private: 1624 static const int kExtensionId = 5; 1625 const bool feedback_enabled_; 1626 const size_t num_video_streams_; 1627 const size_t num_audio_streams_; 1628 Call* receiver_call_; 1629 }; 1630 1631 TEST_F(EndToEndTest, VideoReceivesTransportFeedback) { 1632 TransportFeedbackTester test(true, 1, 0); 1633 RunBaseTest(&test); 1634 } 1635 1636 TEST_F(EndToEndTest, VideoTransportFeedbackNotConfigured) { 1637 TransportFeedbackTester test(false, 1, 0); 1638 RunBaseTest(&test); 1639 } 1640 1641 TEST_F(EndToEndTest, AudioReceivesTransportFeedback) { 1642 TransportFeedbackTester test(true, 0, 1); 1643 RunBaseTest(&test); 1644 } 1645 1646 TEST_F(EndToEndTest, AudioTransportFeedbackNotConfigured) { 1647 TransportFeedbackTester test(false, 0, 1); 1648 RunBaseTest(&test); 1649 } 1650 1651 TEST_F(EndToEndTest, AudioVideoReceivesTransportFeedback) { 1652 TransportFeedbackTester test(true, 1, 1); 1653 RunBaseTest(&test); 1654 } 1655 1656 TEST_F(EndToEndTest, ObserversEncodedFrames) { 1657 class EncodedFrameTestObserver : public EncodedFrameObserver { 1658 public: 1659 EncodedFrameTestObserver() 1660 : length_(0), frame_type_(kEmptyFrame), called_(false, false) {} 1661 virtual ~EncodedFrameTestObserver() {} 1662 1663 virtual void EncodedFrameCallback(const EncodedFrame& encoded_frame) { 1664 frame_type_ = encoded_frame.frame_type_; 1665 length_ = encoded_frame.length_; 1666 buffer_.reset(new uint8_t[length_]); 1667 memcpy(buffer_.get(), encoded_frame.data_, length_); 1668 called_.Set(); 1669 } 1670 1671 bool Wait() { return called_.Wait(kDefaultTimeoutMs); } 1672 1673 void ExpectEqualFrames(const EncodedFrameTestObserver& observer) { 1674 ASSERT_EQ(length_, observer.length_) 1675 << "Observed frames are of different lengths."; 1676 EXPECT_EQ(frame_type_, observer.frame_type_) 1677 << "Observed frames have different frame types."; 1678 EXPECT_EQ(0, memcmp(buffer_.get(), observer.buffer_.get(), length_)) 1679 << "Observed encoded frames have different content."; 1680 } 1681 1682 private: 1683 rtc::scoped_ptr<uint8_t[]> buffer_; 1684 size_t length_; 1685 FrameType frame_type_; 1686 rtc::Event called_; 1687 }; 1688 1689 EncodedFrameTestObserver post_encode_observer; 1690 EncodedFrameTestObserver pre_decode_observer; 1691 1692 CreateCalls(Call::Config(), Call::Config()); 1693 1694 test::DirectTransport sender_transport(sender_call_.get()); 1695 test::DirectTransport receiver_transport(receiver_call_.get()); 1696 sender_transport.SetReceiver(receiver_call_->Receiver()); 1697 receiver_transport.SetReceiver(sender_call_->Receiver()); 1698 1699 CreateSendConfig(1, 0, &sender_transport); 1700 CreateMatchingReceiveConfigs(&receiver_transport); 1701 video_send_config_.post_encode_callback = &post_encode_observer; 1702 video_receive_configs_[0].pre_decode_callback = &pre_decode_observer; 1703 1704 CreateVideoStreams(); 1705 Start(); 1706 1707 rtc::scoped_ptr<test::FrameGenerator> frame_generator( 1708 test::FrameGenerator::CreateChromaGenerator( 1709 video_encoder_config_.streams[0].width, 1710 video_encoder_config_.streams[0].height)); 1711 video_send_stream_->Input()->IncomingCapturedFrame( 1712 *frame_generator->NextFrame()); 1713 1714 EXPECT_TRUE(post_encode_observer.Wait()) 1715 << "Timed out while waiting for send-side encoded-frame callback."; 1716 1717 EXPECT_TRUE(pre_decode_observer.Wait()) 1718 << "Timed out while waiting for pre-decode encoded-frame callback."; 1719 1720 post_encode_observer.ExpectEqualFrames(pre_decode_observer); 1721 1722 Stop(); 1723 1724 sender_transport.StopSending(); 1725 receiver_transport.StopSending(); 1726 1727 DestroyStreams(); 1728 } 1729 1730 TEST_F(EndToEndTest, ReceiveStreamSendsRemb) { 1731 class RembObserver : public test::EndToEndTest { 1732 public: 1733 RembObserver() : EndToEndTest(kDefaultTimeoutMs) {} 1734 1735 Action OnReceiveRtcp(const uint8_t* packet, size_t length) override { 1736 RTCPUtility::RTCPParserV2 parser(packet, length, true); 1737 EXPECT_TRUE(parser.IsValid()); 1738 1739 bool received_psfb = false; 1740 bool received_remb = false; 1741 RTCPUtility::RTCPPacketTypes packet_type = parser.Begin(); 1742 while (packet_type != RTCPUtility::RTCPPacketTypes::kInvalid) { 1743 if (packet_type == RTCPUtility::RTCPPacketTypes::kPsfbRemb) { 1744 const RTCPUtility::RTCPPacket& packet = parser.Packet(); 1745 EXPECT_EQ(packet.PSFBAPP.SenderSSRC, kReceiverLocalVideoSsrc); 1746 received_psfb = true; 1747 } else if (packet_type == RTCPUtility::RTCPPacketTypes::kPsfbRembItem) { 1748 const RTCPUtility::RTCPPacket& packet = parser.Packet(); 1749 EXPECT_GT(packet.REMBItem.BitRate, 0u); 1750 EXPECT_EQ(packet.REMBItem.NumberOfSSRCs, 1u); 1751 EXPECT_EQ(packet.REMBItem.SSRCs[0], kVideoSendSsrcs[0]); 1752 received_remb = true; 1753 } 1754 packet_type = parser.Iterate(); 1755 } 1756 if (received_psfb && received_remb) 1757 observation_complete_.Set(); 1758 return SEND_PACKET; 1759 } 1760 void PerformTest() override { 1761 EXPECT_TRUE(Wait()) << "Timed out while waiting for a " 1762 "receiver RTCP REMB packet to be " 1763 "sent."; 1764 } 1765 } test; 1766 1767 RunBaseTest(&test); 1768 } 1769 1770 TEST_F(EndToEndTest, VerifyBandwidthStats) { 1771 class RtcpObserver : public test::EndToEndTest { 1772 public: 1773 RtcpObserver() 1774 : EndToEndTest(kDefaultTimeoutMs), 1775 sender_call_(nullptr), 1776 receiver_call_(nullptr), 1777 has_seen_pacer_delay_(false) {} 1778 1779 Action OnSendRtp(const uint8_t* packet, size_t length) override { 1780 Call::Stats sender_stats = sender_call_->GetStats(); 1781 Call::Stats receiver_stats = receiver_call_->GetStats(); 1782 if (!has_seen_pacer_delay_) 1783 has_seen_pacer_delay_ = sender_stats.pacer_delay_ms > 0; 1784 if (sender_stats.send_bandwidth_bps > 0 && 1785 receiver_stats.recv_bandwidth_bps > 0 && has_seen_pacer_delay_) { 1786 observation_complete_.Set(); 1787 } 1788 return SEND_PACKET; 1789 } 1790 1791 void OnCallsCreated(Call* sender_call, Call* receiver_call) override { 1792 sender_call_ = sender_call; 1793 receiver_call_ = receiver_call; 1794 } 1795 1796 void PerformTest() override { 1797 EXPECT_TRUE(Wait()) << "Timed out while waiting for " 1798 "non-zero bandwidth stats."; 1799 } 1800 1801 private: 1802 Call* sender_call_; 1803 Call* receiver_call_; 1804 bool has_seen_pacer_delay_; 1805 } test; 1806 1807 RunBaseTest(&test); 1808 } 1809 1810 TEST_F(EndToEndTest, VerifyNackStats) { 1811 static const int kPacketNumberToDrop = 200; 1812 class NackObserver : public test::EndToEndTest { 1813 public: 1814 NackObserver() 1815 : EndToEndTest(kLongTimeoutMs), 1816 sent_rtp_packets_(0), 1817 dropped_rtp_packet_(0), 1818 dropped_rtp_packet_requested_(false), 1819 send_stream_(nullptr), 1820 start_runtime_ms_(-1) {} 1821 1822 private: 1823 Action OnSendRtp(const uint8_t* packet, size_t length) override { 1824 rtc::CritScope lock(&crit_); 1825 if (++sent_rtp_packets_ == kPacketNumberToDrop) { 1826 rtc::scoped_ptr<RtpHeaderParser> parser(RtpHeaderParser::Create()); 1827 RTPHeader header; 1828 EXPECT_TRUE(parser->Parse(packet, length, &header)); 1829 dropped_rtp_packet_ = header.sequenceNumber; 1830 return DROP_PACKET; 1831 } 1832 VerifyStats(); 1833 return SEND_PACKET; 1834 } 1835 1836 Action OnReceiveRtcp(const uint8_t* packet, size_t length) override { 1837 rtc::CritScope lock(&crit_); 1838 test::RtcpPacketParser rtcp_parser; 1839 rtcp_parser.Parse(packet, length); 1840 std::vector<uint16_t> nacks = rtcp_parser.nack_item()->last_nack_list(); 1841 if (!nacks.empty() && std::find( 1842 nacks.begin(), nacks.end(), dropped_rtp_packet_) != nacks.end()) { 1843 dropped_rtp_packet_requested_ = true; 1844 } 1845 return SEND_PACKET; 1846 } 1847 1848 void VerifyStats() EXCLUSIVE_LOCKS_REQUIRED(&crit_) { 1849 if (!dropped_rtp_packet_requested_) 1850 return; 1851 int send_stream_nack_packets = 0; 1852 int receive_stream_nack_packets = 0; 1853 VideoSendStream::Stats stats = send_stream_->GetStats(); 1854 for (std::map<uint32_t, VideoSendStream::StreamStats>::const_iterator it = 1855 stats.substreams.begin(); it != stats.substreams.end(); ++it) { 1856 const VideoSendStream::StreamStats& stream_stats = it->second; 1857 send_stream_nack_packets += 1858 stream_stats.rtcp_packet_type_counts.nack_packets; 1859 } 1860 for (size_t i = 0; i < receive_streams_.size(); ++i) { 1861 VideoReceiveStream::Stats stats = receive_streams_[i]->GetStats(); 1862 receive_stream_nack_packets += 1863 stats.rtcp_packet_type_counts.nack_packets; 1864 } 1865 if (send_stream_nack_packets >= 1 && receive_stream_nack_packets >= 1) { 1866 // NACK packet sent on receive stream and received on sent stream. 1867 if (MinMetricRunTimePassed()) 1868 observation_complete_.Set(); 1869 } 1870 } 1871 1872 bool MinMetricRunTimePassed() { 1873 int64_t now = Clock::GetRealTimeClock()->TimeInMilliseconds(); 1874 if (start_runtime_ms_ == -1) { 1875 start_runtime_ms_ = now; 1876 return false; 1877 } 1878 int64_t elapsed_sec = (now - start_runtime_ms_) / 1000; 1879 return elapsed_sec > metrics::kMinRunTimeInSeconds; 1880 } 1881 1882 void ModifyVideoConfigs( 1883 VideoSendStream::Config* send_config, 1884 std::vector<VideoReceiveStream::Config>* receive_configs, 1885 VideoEncoderConfig* encoder_config) override { 1886 send_config->rtp.nack.rtp_history_ms = kNackRtpHistoryMs; 1887 (*receive_configs)[0].rtp.nack.rtp_history_ms = kNackRtpHistoryMs; 1888 } 1889 1890 void OnVideoStreamsCreated( 1891 VideoSendStream* send_stream, 1892 const std::vector<VideoReceiveStream*>& receive_streams) override { 1893 send_stream_ = send_stream; 1894 receive_streams_ = receive_streams; 1895 } 1896 1897 void PerformTest() override { 1898 EXPECT_TRUE(Wait()) << "Timed out waiting for packet to be NACKed."; 1899 } 1900 1901 rtc::CriticalSection crit_; 1902 uint64_t sent_rtp_packets_; 1903 uint16_t dropped_rtp_packet_ GUARDED_BY(&crit_); 1904 bool dropped_rtp_packet_requested_ GUARDED_BY(&crit_); 1905 std::vector<VideoReceiveStream*> receive_streams_; 1906 VideoSendStream* send_stream_; 1907 int64_t start_runtime_ms_; 1908 } test; 1909 1910 test::ClearHistograms(); 1911 RunBaseTest(&test); 1912 1913 EXPECT_EQ(1, test::NumHistogramSamples( 1914 "WebRTC.Video.UniqueNackRequestsSentInPercent")); 1915 EXPECT_EQ(1, test::NumHistogramSamples( 1916 "WebRTC.Video.UniqueNackRequestsReceivedInPercent")); 1917 EXPECT_GT(test::LastHistogramSample( 1918 "WebRTC.Video.NackPacketsSentPerMinute"), 0); 1919 EXPECT_GT(test::LastHistogramSample( 1920 "WebRTC.Video.NackPacketsReceivedPerMinute"), 0); 1921 } 1922 1923 void EndToEndTest::VerifyHistogramStats(bool use_rtx, 1924 bool use_red, 1925 bool screenshare) { 1926 class StatsObserver : public test::EndToEndTest { 1927 public: 1928 StatsObserver(bool use_rtx, bool use_red, bool screenshare) 1929 : EndToEndTest(kLongTimeoutMs), 1930 use_rtx_(use_rtx), 1931 use_red_(use_red), 1932 screenshare_(screenshare), 1933 sender_call_(nullptr), 1934 receiver_call_(nullptr), 1935 start_runtime_ms_(-1) {} 1936 1937 private: 1938 Action OnSendRtp(const uint8_t* packet, size_t length) override { 1939 if (MinMetricRunTimePassed()) 1940 observation_complete_.Set(); 1941 1942 // GetStats calls GetSendChannelRtcpStatistics 1943 // (via VideoSendStream::GetRtt) which updates ReportBlockStats used by 1944 // WebRTC.Video.SentPacketsLostInPercent. 1945 // TODO(asapersson): Remove dependency on calling GetStats. 1946 sender_call_->GetStats(); 1947 1948 return SEND_PACKET; 1949 } 1950 1951 bool MinMetricRunTimePassed() { 1952 int64_t now = Clock::GetRealTimeClock()->TimeInMilliseconds(); 1953 if (start_runtime_ms_ == -1) { 1954 start_runtime_ms_ = now; 1955 return false; 1956 } 1957 int64_t elapsed_sec = (now - start_runtime_ms_) / 1000; 1958 return elapsed_sec > metrics::kMinRunTimeInSeconds * 2; 1959 } 1960 1961 void ModifyVideoConfigs( 1962 VideoSendStream::Config* send_config, 1963 std::vector<VideoReceiveStream::Config>* receive_configs, 1964 VideoEncoderConfig* encoder_config) override { 1965 // NACK 1966 send_config->rtp.nack.rtp_history_ms = kNackRtpHistoryMs; 1967 (*receive_configs)[0].rtp.nack.rtp_history_ms = kNackRtpHistoryMs; 1968 // FEC 1969 if (use_red_) { 1970 send_config->rtp.fec.ulpfec_payload_type = kUlpfecPayloadType; 1971 send_config->rtp.fec.red_payload_type = kRedPayloadType; 1972 (*receive_configs)[0].rtp.fec.red_payload_type = kRedPayloadType; 1973 (*receive_configs)[0].rtp.fec.ulpfec_payload_type = kUlpfecPayloadType; 1974 } 1975 // RTX 1976 if (use_rtx_) { 1977 send_config->rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[0]); 1978 send_config->rtp.rtx.payload_type = kSendRtxPayloadType; 1979 (*receive_configs)[0].rtp.rtx[kFakeVideoSendPayloadType].ssrc = 1980 kSendRtxSsrcs[0]; 1981 (*receive_configs)[0].rtp.rtx[kFakeVideoSendPayloadType].payload_type = 1982 kSendRtxPayloadType; 1983 } 1984 encoder_config->content_type = 1985 screenshare_ ? VideoEncoderConfig::ContentType::kScreen 1986 : VideoEncoderConfig::ContentType::kRealtimeVideo; 1987 } 1988 1989 void OnCallsCreated(Call* sender_call, Call* receiver_call) override { 1990 sender_call_ = sender_call; 1991 receiver_call_ = receiver_call; 1992 } 1993 1994 void PerformTest() override { 1995 EXPECT_TRUE(Wait()) << "Timed out waiting for packet to be NACKed."; 1996 } 1997 1998 const bool use_rtx_; 1999 const bool use_red_; 2000 const bool screenshare_; 2001 Call* sender_call_; 2002 Call* receiver_call_; 2003 int64_t start_runtime_ms_; 2004 } test(use_rtx, use_red, screenshare); 2005 2006 test::ClearHistograms(); 2007 RunBaseTest(&test); 2008 2009 // Delete the call for Call stats to be reported. 2010 sender_call_.reset(); 2011 receiver_call_.reset(); 2012 2013 std::string video_prefix = 2014 screenshare ? "WebRTC.Video.Screenshare." : "WebRTC.Video."; 2015 2016 // Verify that stats have been updated once. 2017 EXPECT_EQ( 2018 1, test::NumHistogramSamples("WebRTC.Call.VideoBitrateReceivedInKbps")); 2019 EXPECT_EQ(1, 2020 test::NumHistogramSamples("WebRTC.Call.RtcpBitrateReceivedInBps")); 2021 EXPECT_EQ(1, test::NumHistogramSamples("WebRTC.Call.BitrateReceivedInKbps")); 2022 EXPECT_EQ( 2023 1, test::NumHistogramSamples("WebRTC.Call.EstimatedSendBitrateInKbps")); 2024 EXPECT_EQ(1, test::NumHistogramSamples("WebRTC.Call.PacerBitrateInKbps")); 2025 2026 EXPECT_EQ(1, test::NumHistogramSamples( 2027 "WebRTC.Video.NackPacketsSentPerMinute")); 2028 EXPECT_EQ(1, test::NumHistogramSamples( 2029 "WebRTC.Video.NackPacketsReceivedPerMinute")); 2030 EXPECT_EQ(1, test::NumHistogramSamples( 2031 "WebRTC.Video.FirPacketsSentPerMinute")); 2032 EXPECT_EQ(1, test::NumHistogramSamples( 2033 "WebRTC.Video.FirPacketsReceivedPerMinute")); 2034 EXPECT_EQ(1, test::NumHistogramSamples( 2035 "WebRTC.Video.PliPacketsSentPerMinute")); 2036 EXPECT_EQ(1, test::NumHistogramSamples( 2037 "WebRTC.Video.PliPacketsReceivedPerMinute")); 2038 2039 EXPECT_EQ( 2040 1, test::NumHistogramSamples(video_prefix + "KeyFramesSentInPermille")); 2041 EXPECT_EQ(1, test::NumHistogramSamples( 2042 "WebRTC.Video.KeyFramesReceivedInPermille")); 2043 2044 EXPECT_EQ(1, test::NumHistogramSamples( 2045 "WebRTC.Video.SentPacketsLostInPercent")); 2046 EXPECT_EQ(1, test::NumHistogramSamples( 2047 "WebRTC.Video.ReceivedPacketsLostInPercent")); 2048 2049 EXPECT_EQ(1, test::NumHistogramSamples(video_prefix + "InputWidthInPixels")); 2050 EXPECT_EQ(1, test::NumHistogramSamples(video_prefix + "InputHeightInPixels")); 2051 EXPECT_EQ(1, test::NumHistogramSamples(video_prefix + "SentWidthInPixels")); 2052 EXPECT_EQ(1, test::NumHistogramSamples(video_prefix + "SentHeightInPixels")); 2053 EXPECT_EQ(1, test::NumHistogramSamples("WebRTC.Video.ReceivedWidthInPixels")); 2054 EXPECT_EQ(1, 2055 test::NumHistogramSamples("WebRTC.Video.ReceivedHeightInPixels")); 2056 2057 EXPECT_EQ(static_cast<int>(video_encoder_config_.streams[0].width), 2058 test::LastHistogramSample(video_prefix + "InputWidthInPixels")); 2059 EXPECT_EQ(static_cast<int>(video_encoder_config_.streams[0].height), 2060 test::LastHistogramSample(video_prefix + "InputHeightInPixels")); 2061 EXPECT_EQ(static_cast<int>(video_encoder_config_.streams[0].width), 2062 test::LastHistogramSample(video_prefix + "SentWidthInPixels")); 2063 EXPECT_EQ(static_cast<int>(video_encoder_config_.streams[0].height), 2064 test::LastHistogramSample(video_prefix + "SentHeightInPixels")); 2065 EXPECT_EQ(static_cast<int>(video_encoder_config_.streams[0].width), 2066 test::LastHistogramSample("WebRTC.Video.ReceivedWidthInPixels")); 2067 EXPECT_EQ(static_cast<int>(video_encoder_config_.streams[0].height), 2068 test::LastHistogramSample("WebRTC.Video.ReceivedHeightInPixels")); 2069 2070 EXPECT_EQ(1, 2071 test::NumHistogramSamples(video_prefix + "InputFramesPerSecond")); 2072 EXPECT_EQ(1, test::NumHistogramSamples(video_prefix + "SentFramesPerSecond")); 2073 EXPECT_EQ(1, test::NumHistogramSamples( 2074 "WebRTC.Video.DecodedFramesPerSecond")); 2075 EXPECT_EQ(1, test::NumHistogramSamples("WebRTC.Video.RenderFramesPerSecond")); 2076 2077 EXPECT_EQ(1, test::NumHistogramSamples("WebRTC.Video.OnewayDelayInMs")); 2078 EXPECT_EQ( 2079 1, test::NumHistogramSamples("WebRTC.Video.RenderSqrtPixelsPerSecond")); 2080 2081 EXPECT_EQ(1, test::NumHistogramSamples(video_prefix + "EncodeTimeInMs")); 2082 EXPECT_EQ(1, test::NumHistogramSamples("WebRTC.Video.DecodeTimeInMs")); 2083 2084 EXPECT_EQ(1, test::NumHistogramSamples( 2085 "WebRTC.Video.BitrateSentInKbps")); 2086 EXPECT_EQ(1, test::NumHistogramSamples( 2087 "WebRTC.Video.BitrateReceivedInKbps")); 2088 EXPECT_EQ(1, test::NumHistogramSamples( 2089 "WebRTC.Video.MediaBitrateSentInKbps")); 2090 EXPECT_EQ(1, test::NumHistogramSamples( 2091 "WebRTC.Video.MediaBitrateReceivedInKbps")); 2092 EXPECT_EQ(1, test::NumHistogramSamples( 2093 "WebRTC.Video.PaddingBitrateSentInKbps")); 2094 EXPECT_EQ(1, test::NumHistogramSamples( 2095 "WebRTC.Video.PaddingBitrateReceivedInKbps")); 2096 EXPECT_EQ(1, test::NumHistogramSamples( 2097 "WebRTC.Video.RetransmittedBitrateSentInKbps")); 2098 EXPECT_EQ(1, test::NumHistogramSamples( 2099 "WebRTC.Video.RetransmittedBitrateReceivedInKbps")); 2100 2101 EXPECT_EQ(1, test::NumHistogramSamples(video_prefix + "SendSideDelayInMs")); 2102 EXPECT_EQ(1, 2103 test::NumHistogramSamples(video_prefix + "SendSideDelayMaxInMs")); 2104 2105 int num_rtx_samples = use_rtx ? 1 : 0; 2106 EXPECT_EQ(num_rtx_samples, test::NumHistogramSamples( 2107 "WebRTC.Video.RtxBitrateSentInKbps")); 2108 EXPECT_EQ(num_rtx_samples, test::NumHistogramSamples( 2109 "WebRTC.Video.RtxBitrateReceivedInKbps")); 2110 2111 int num_red_samples = use_red ? 1 : 0; 2112 EXPECT_EQ(num_red_samples, test::NumHistogramSamples( 2113 "WebRTC.Video.FecBitrateSentInKbps")); 2114 EXPECT_EQ(num_red_samples, test::NumHistogramSamples( 2115 "WebRTC.Video.FecBitrateReceivedInKbps")); 2116 EXPECT_EQ(num_red_samples, test::NumHistogramSamples( 2117 "WebRTC.Video.ReceivedFecPacketsInPercent")); 2118 } 2119 2120 TEST_F(EndToEndTest, VerifyHistogramStatsWithRtx) { 2121 const bool kEnabledRtx = true; 2122 const bool kEnabledRed = false; 2123 const bool kScreenshare = false; 2124 VerifyHistogramStats(kEnabledRtx, kEnabledRed, kScreenshare); 2125 } 2126 2127 TEST_F(EndToEndTest, VerifyHistogramStatsWithRed) { 2128 const bool kEnabledRtx = false; 2129 const bool kEnabledRed = true; 2130 const bool kScreenshare = false; 2131 VerifyHistogramStats(kEnabledRtx, kEnabledRed, kScreenshare); 2132 } 2133 2134 TEST_F(EndToEndTest, VerifyHistogramStatsWithScreenshare) { 2135 const bool kEnabledRtx = false; 2136 const bool kEnabledRed = false; 2137 const bool kScreenshare = true; 2138 VerifyHistogramStats(kEnabledRtx, kEnabledRed, kScreenshare); 2139 } 2140 2141 void EndToEndTest::TestXrReceiverReferenceTimeReport(bool enable_rrtr) { 2142 static const int kNumRtcpReportPacketsToObserve = 5; 2143 class RtcpXrObserver : public test::EndToEndTest { 2144 public: 2145 explicit RtcpXrObserver(bool enable_rrtr) 2146 : EndToEndTest(kDefaultTimeoutMs), 2147 enable_rrtr_(enable_rrtr), 2148 sent_rtcp_sr_(0), 2149 sent_rtcp_rr_(0), 2150 sent_rtcp_rrtr_(0), 2151 sent_rtcp_dlrr_(0) {} 2152 2153 private: 2154 // Receive stream should send RR packets (and RRTR packets if enabled). 2155 Action OnReceiveRtcp(const uint8_t* packet, size_t length) override { 2156 rtc::CritScope lock(&crit_); 2157 RTCPUtility::RTCPParserV2 parser(packet, length, true); 2158 EXPECT_TRUE(parser.IsValid()); 2159 2160 RTCPUtility::RTCPPacketTypes packet_type = parser.Begin(); 2161 while (packet_type != RTCPUtility::RTCPPacketTypes::kInvalid) { 2162 if (packet_type == RTCPUtility::RTCPPacketTypes::kRr) { 2163 ++sent_rtcp_rr_; 2164 } else if (packet_type == 2165 RTCPUtility::RTCPPacketTypes::kXrReceiverReferenceTime) { 2166 ++sent_rtcp_rrtr_; 2167 } 2168 EXPECT_NE(packet_type, RTCPUtility::RTCPPacketTypes::kSr); 2169 EXPECT_NE(packet_type, 2170 RTCPUtility::RTCPPacketTypes::kXrDlrrReportBlockItem); 2171 packet_type = parser.Iterate(); 2172 } 2173 return SEND_PACKET; 2174 } 2175 // Send stream should send SR packets (and DLRR packets if enabled). 2176 virtual Action OnSendRtcp(const uint8_t* packet, size_t length) { 2177 rtc::CritScope lock(&crit_); 2178 RTCPUtility::RTCPParserV2 parser(packet, length, true); 2179 EXPECT_TRUE(parser.IsValid()); 2180 2181 RTCPUtility::RTCPPacketTypes packet_type = parser.Begin(); 2182 while (packet_type != RTCPUtility::RTCPPacketTypes::kInvalid) { 2183 if (packet_type == RTCPUtility::RTCPPacketTypes::kSr) { 2184 ++sent_rtcp_sr_; 2185 } else if (packet_type == 2186 RTCPUtility::RTCPPacketTypes::kXrDlrrReportBlockItem) { 2187 ++sent_rtcp_dlrr_; 2188 } 2189 EXPECT_NE(packet_type, 2190 RTCPUtility::RTCPPacketTypes::kXrReceiverReferenceTime); 2191 packet_type = parser.Iterate(); 2192 } 2193 if (sent_rtcp_sr_ > kNumRtcpReportPacketsToObserve && 2194 sent_rtcp_rr_ > kNumRtcpReportPacketsToObserve) { 2195 if (enable_rrtr_) { 2196 EXPECT_GT(sent_rtcp_rrtr_, 0); 2197 EXPECT_GT(sent_rtcp_dlrr_, 0); 2198 } else { 2199 EXPECT_EQ(0, sent_rtcp_rrtr_); 2200 EXPECT_EQ(0, sent_rtcp_dlrr_); 2201 } 2202 observation_complete_.Set(); 2203 } 2204 return SEND_PACKET; 2205 } 2206 2207 void ModifyVideoConfigs( 2208 VideoSendStream::Config* send_config, 2209 std::vector<VideoReceiveStream::Config>* receive_configs, 2210 VideoEncoderConfig* encoder_config) override { 2211 (*receive_configs)[0].rtp.rtcp_mode = RtcpMode::kReducedSize; 2212 (*receive_configs)[0].rtp.rtcp_xr.receiver_reference_time_report = 2213 enable_rrtr_; 2214 } 2215 2216 void PerformTest() override { 2217 EXPECT_TRUE(Wait()) 2218 << "Timed out while waiting for RTCP SR/RR packets to be sent."; 2219 } 2220 2221 rtc::CriticalSection crit_; 2222 bool enable_rrtr_; 2223 int sent_rtcp_sr_; 2224 int sent_rtcp_rr_ GUARDED_BY(&crit_); 2225 int sent_rtcp_rrtr_ GUARDED_BY(&crit_); 2226 int sent_rtcp_dlrr_; 2227 } test(enable_rrtr); 2228 2229 RunBaseTest(&test); 2230 } 2231 2232 void EndToEndTest::TestSendsSetSsrcs(size_t num_ssrcs, 2233 bool send_single_ssrc_first) { 2234 class SendsSetSsrcs : public test::EndToEndTest { 2235 public: 2236 SendsSetSsrcs(const uint32_t* ssrcs, 2237 size_t num_ssrcs, 2238 bool send_single_ssrc_first) 2239 : EndToEndTest(kDefaultTimeoutMs), 2240 num_ssrcs_(num_ssrcs), 2241 send_single_ssrc_first_(send_single_ssrc_first), 2242 ssrcs_to_observe_(num_ssrcs), 2243 expect_single_ssrc_(send_single_ssrc_first), 2244 send_stream_(nullptr) { 2245 for (size_t i = 0; i < num_ssrcs; ++i) 2246 valid_ssrcs_[ssrcs[i]] = true; 2247 } 2248 2249 private: 2250 Action OnSendRtp(const uint8_t* packet, size_t length) override { 2251 RTPHeader header; 2252 EXPECT_TRUE(parser_->Parse(packet, length, &header)); 2253 2254 EXPECT_TRUE(valid_ssrcs_[header.ssrc]) 2255 << "Received unknown SSRC: " << header.ssrc; 2256 2257 if (!valid_ssrcs_[header.ssrc]) 2258 observation_complete_.Set(); 2259 2260 if (!is_observed_[header.ssrc]) { 2261 is_observed_[header.ssrc] = true; 2262 --ssrcs_to_observe_; 2263 if (expect_single_ssrc_) { 2264 expect_single_ssrc_ = false; 2265 observation_complete_.Set(); 2266 } 2267 } 2268 2269 if (ssrcs_to_observe_ == 0) 2270 observation_complete_.Set(); 2271 2272 return SEND_PACKET; 2273 } 2274 2275 size_t GetNumVideoStreams() const override { return num_ssrcs_; } 2276 2277 void ModifyVideoConfigs( 2278 VideoSendStream::Config* send_config, 2279 std::vector<VideoReceiveStream::Config>* receive_configs, 2280 VideoEncoderConfig* encoder_config) override { 2281 if (num_ssrcs_ > 1) { 2282 // Set low simulcast bitrates to not have to wait for bandwidth ramp-up. 2283 for (size_t i = 0; i < encoder_config->streams.size(); ++i) { 2284 encoder_config->streams[i].min_bitrate_bps = 10000; 2285 encoder_config->streams[i].target_bitrate_bps = 15000; 2286 encoder_config->streams[i].max_bitrate_bps = 20000; 2287 } 2288 } 2289 2290 video_encoder_config_all_streams_ = *encoder_config; 2291 if (send_single_ssrc_first_) 2292 encoder_config->streams.resize(1); 2293 } 2294 2295 void OnVideoStreamsCreated( 2296 VideoSendStream* send_stream, 2297 const std::vector<VideoReceiveStream*>& receive_streams) override { 2298 send_stream_ = send_stream; 2299 } 2300 2301 void PerformTest() override { 2302 EXPECT_TRUE(Wait()) << "Timed out while waiting for " 2303 << (send_single_ssrc_first_ ? "first SSRC." 2304 : "SSRCs."); 2305 2306 if (send_single_ssrc_first_) { 2307 // Set full simulcast and continue with the rest of the SSRCs. 2308 send_stream_->ReconfigureVideoEncoder( 2309 video_encoder_config_all_streams_); 2310 EXPECT_TRUE(Wait()) << "Timed out while waiting on additional SSRCs."; 2311 } 2312 } 2313 2314 private: 2315 std::map<uint32_t, bool> valid_ssrcs_; 2316 std::map<uint32_t, bool> is_observed_; 2317 2318 const size_t num_ssrcs_; 2319 const bool send_single_ssrc_first_; 2320 2321 size_t ssrcs_to_observe_; 2322 bool expect_single_ssrc_; 2323 2324 VideoSendStream* send_stream_; 2325 VideoEncoderConfig video_encoder_config_all_streams_; 2326 } test(kVideoSendSsrcs, num_ssrcs, send_single_ssrc_first); 2327 2328 RunBaseTest(&test); 2329 } 2330 2331 TEST_F(EndToEndTest, ReportsSetEncoderRates) { 2332 class EncoderRateStatsTest : public test::EndToEndTest, 2333 public test::FakeEncoder { 2334 public: 2335 EncoderRateStatsTest() 2336 : EndToEndTest(kDefaultTimeoutMs), 2337 FakeEncoder(Clock::GetRealTimeClock()), 2338 send_stream_(nullptr), 2339 bitrate_kbps_(0) {} 2340 2341 void OnVideoStreamsCreated( 2342 VideoSendStream* send_stream, 2343 const std::vector<VideoReceiveStream*>& receive_streams) override { 2344 send_stream_ = send_stream; 2345 } 2346 2347 void ModifyVideoConfigs( 2348 VideoSendStream::Config* send_config, 2349 std::vector<VideoReceiveStream::Config>* receive_configs, 2350 VideoEncoderConfig* encoder_config) override { 2351 send_config->encoder_settings.encoder = this; 2352 } 2353 2354 int32_t SetRates(uint32_t new_target_bitrate, uint32_t framerate) override { 2355 // Make sure not to trigger on any default zero bitrates. 2356 if (new_target_bitrate == 0) 2357 return 0; 2358 rtc::CritScope lock(&crit_); 2359 bitrate_kbps_ = new_target_bitrate; 2360 observation_complete_.Set(); 2361 return 0; 2362 } 2363 2364 void PerformTest() override { 2365 ASSERT_TRUE(Wait()) 2366 << "Timed out while waiting for encoder SetRates() call."; 2367 // Wait for GetStats to report a corresponding bitrate. 2368 for (int i = 0; i < kDefaultTimeoutMs; ++i) { 2369 VideoSendStream::Stats stats = send_stream_->GetStats(); 2370 { 2371 rtc::CritScope lock(&crit_); 2372 if ((stats.target_media_bitrate_bps + 500) / 1000 == 2373 static_cast<int>(bitrate_kbps_)) { 2374 return; 2375 } 2376 } 2377 SleepMs(1); 2378 } 2379 FAIL() 2380 << "Timed out waiting for stats reporting the currently set bitrate."; 2381 } 2382 2383 private: 2384 rtc::CriticalSection crit_; 2385 VideoSendStream* send_stream_; 2386 uint32_t bitrate_kbps_ GUARDED_BY(crit_); 2387 } test; 2388 2389 RunBaseTest(&test); 2390 } 2391 2392 TEST_F(EndToEndTest, GetStats) { 2393 static const int kStartBitrateBps = 3000000; 2394 static const int kExpectedRenderDelayMs = 20; 2395 class StatsObserver : public test::EndToEndTest, public I420FrameCallback { 2396 public: 2397 StatsObserver() 2398 : EndToEndTest(kLongTimeoutMs), 2399 send_stream_(nullptr), 2400 expected_send_ssrcs_(), 2401 check_stats_event_(false, false) {} 2402 2403 private: 2404 Action OnSendRtp(const uint8_t* packet, size_t length) override { 2405 check_stats_event_.Set(); 2406 return SEND_PACKET; 2407 } 2408 2409 Action OnSendRtcp(const uint8_t* packet, size_t length) override { 2410 check_stats_event_.Set(); 2411 return SEND_PACKET; 2412 } 2413 2414 Action OnReceiveRtp(const uint8_t* packet, size_t length) override { 2415 check_stats_event_.Set(); 2416 return SEND_PACKET; 2417 } 2418 2419 Action OnReceiveRtcp(const uint8_t* packet, size_t length) override { 2420 check_stats_event_.Set(); 2421 return SEND_PACKET; 2422 } 2423 2424 void FrameCallback(VideoFrame* video_frame) override { 2425 // Ensure that we have at least 5ms send side delay. 2426 int64_t render_time = video_frame->render_time_ms(); 2427 if (render_time > 0) 2428 video_frame->set_render_time_ms(render_time - 5); 2429 } 2430 2431 bool CheckReceiveStats() { 2432 for (size_t i = 0; i < receive_streams_.size(); ++i) { 2433 VideoReceiveStream::Stats stats = receive_streams_[i]->GetStats(); 2434 EXPECT_EQ(expected_receive_ssrcs_[i], stats.ssrc); 2435 2436 // Make sure all fields have been populated. 2437 // TODO(pbos): Use CompoundKey if/when we ever know that all stats are 2438 // always filled for all receivers. 2439 receive_stats_filled_["IncomingRate"] |= 2440 stats.network_frame_rate != 0 || stats.total_bitrate_bps != 0; 2441 2442 send_stats_filled_["DecoderImplementationName"] |= 2443 stats.decoder_implementation_name == 2444 test::FakeDecoder::kImplementationName; 2445 receive_stats_filled_["RenderDelayAsHighAsExpected"] |= 2446 stats.render_delay_ms >= kExpectedRenderDelayMs; 2447 2448 receive_stats_filled_["FrameCallback"] |= stats.decode_frame_rate != 0; 2449 2450 receive_stats_filled_["FrameRendered"] |= stats.render_frame_rate != 0; 2451 2452 receive_stats_filled_["StatisticsUpdated"] |= 2453 stats.rtcp_stats.cumulative_lost != 0 || 2454 stats.rtcp_stats.extended_max_sequence_number != 0 || 2455 stats.rtcp_stats.fraction_lost != 0 || stats.rtcp_stats.jitter != 0; 2456 2457 receive_stats_filled_["DataCountersUpdated"] |= 2458 stats.rtp_stats.transmitted.payload_bytes != 0 || 2459 stats.rtp_stats.fec.packets != 0 || 2460 stats.rtp_stats.transmitted.header_bytes != 0 || 2461 stats.rtp_stats.transmitted.packets != 0 || 2462 stats.rtp_stats.transmitted.padding_bytes != 0 || 2463 stats.rtp_stats.retransmitted.packets != 0; 2464 2465 receive_stats_filled_["CodecStats"] |= 2466 stats.target_delay_ms != 0 || stats.discarded_packets != 0; 2467 2468 receive_stats_filled_["FrameCounts"] |= 2469 stats.frame_counts.key_frames != 0 || 2470 stats.frame_counts.delta_frames != 0; 2471 2472 receive_stats_filled_["CName"] |= !stats.c_name.empty(); 2473 2474 receive_stats_filled_["RtcpPacketTypeCount"] |= 2475 stats.rtcp_packet_type_counts.fir_packets != 0 || 2476 stats.rtcp_packet_type_counts.nack_packets != 0 || 2477 stats.rtcp_packet_type_counts.pli_packets != 0 || 2478 stats.rtcp_packet_type_counts.nack_requests != 0 || 2479 stats.rtcp_packet_type_counts.unique_nack_requests != 0; 2480 2481 assert(stats.current_payload_type == -1 || 2482 stats.current_payload_type == kFakeVideoSendPayloadType); 2483 receive_stats_filled_["IncomingPayloadType"] |= 2484 stats.current_payload_type == kFakeVideoSendPayloadType; 2485 } 2486 2487 return AllStatsFilled(receive_stats_filled_); 2488 } 2489 2490 bool CheckSendStats() { 2491 RTC_DCHECK(send_stream_ != nullptr); 2492 VideoSendStream::Stats stats = send_stream_->GetStats(); 2493 2494 send_stats_filled_["NumStreams"] |= 2495 stats.substreams.size() == expected_send_ssrcs_.size(); 2496 2497 send_stats_filled_["CpuOveruseMetrics"] |= 2498 stats.avg_encode_time_ms != 0 || stats.encode_usage_percent != 0; 2499 2500 send_stats_filled_["EncoderImplementationName"] |= 2501 stats.encoder_implementation_name == 2502 test::FakeEncoder::kImplementationName; 2503 2504 for (std::map<uint32_t, VideoSendStream::StreamStats>::const_iterator it = 2505 stats.substreams.begin(); 2506 it != stats.substreams.end(); ++it) { 2507 EXPECT_TRUE(expected_send_ssrcs_.find(it->first) != 2508 expected_send_ssrcs_.end()); 2509 2510 send_stats_filled_[CompoundKey("CapturedFrameRate", it->first)] |= 2511 stats.input_frame_rate != 0; 2512 2513 const VideoSendStream::StreamStats& stream_stats = it->second; 2514 2515 send_stats_filled_[CompoundKey("StatisticsUpdated", it->first)] |= 2516 stream_stats.rtcp_stats.cumulative_lost != 0 || 2517 stream_stats.rtcp_stats.extended_max_sequence_number != 0 || 2518 stream_stats.rtcp_stats.fraction_lost != 0; 2519 2520 send_stats_filled_[CompoundKey("DataCountersUpdated", it->first)] |= 2521 stream_stats.rtp_stats.fec.packets != 0 || 2522 stream_stats.rtp_stats.transmitted.padding_bytes != 0 || 2523 stream_stats.rtp_stats.retransmitted.packets != 0 || 2524 stream_stats.rtp_stats.transmitted.packets != 0; 2525 2526 send_stats_filled_[CompoundKey("BitrateStatisticsObserver", 2527 it->first)] |= 2528 stream_stats.total_bitrate_bps != 0; 2529 2530 send_stats_filled_[CompoundKey("FrameCountObserver", it->first)] |= 2531 stream_stats.frame_counts.delta_frames != 0 || 2532 stream_stats.frame_counts.key_frames != 0; 2533 2534 send_stats_filled_[CompoundKey("OutgoingRate", it->first)] |= 2535 stats.encode_frame_rate != 0; 2536 2537 send_stats_filled_[CompoundKey("Delay", it->first)] |= 2538 stream_stats.avg_delay_ms != 0 || stream_stats.max_delay_ms != 0; 2539 2540 // TODO(pbos): Use CompoundKey when the test makes sure that all SSRCs 2541 // report dropped packets. 2542 send_stats_filled_["RtcpPacketTypeCount"] |= 2543 stream_stats.rtcp_packet_type_counts.fir_packets != 0 || 2544 stream_stats.rtcp_packet_type_counts.nack_packets != 0 || 2545 stream_stats.rtcp_packet_type_counts.pli_packets != 0 || 2546 stream_stats.rtcp_packet_type_counts.nack_requests != 0 || 2547 stream_stats.rtcp_packet_type_counts.unique_nack_requests != 0; 2548 } 2549 2550 return AllStatsFilled(send_stats_filled_); 2551 } 2552 2553 std::string CompoundKey(const char* name, uint32_t ssrc) { 2554 std::ostringstream oss; 2555 oss << name << "_" << ssrc; 2556 return oss.str(); 2557 } 2558 2559 bool AllStatsFilled(const std::map<std::string, bool>& stats_map) { 2560 for (std::map<std::string, bool>::const_iterator it = stats_map.begin(); 2561 it != stats_map.end(); 2562 ++it) { 2563 if (!it->second) 2564 return false; 2565 } 2566 return true; 2567 } 2568 2569 test::PacketTransport* CreateSendTransport(Call* sender_call) override { 2570 FakeNetworkPipe::Config network_config; 2571 network_config.loss_percent = 5; 2572 return new test::PacketTransport( 2573 sender_call, this, test::PacketTransport::kSender, network_config); 2574 } 2575 2576 Call::Config GetSenderCallConfig() override { 2577 Call::Config config = EndToEndTest::GetSenderCallConfig(); 2578 config.bitrate_config.start_bitrate_bps = kStartBitrateBps; 2579 return config; 2580 } 2581 2582 void ModifyVideoConfigs( 2583 VideoSendStream::Config* send_config, 2584 std::vector<VideoReceiveStream::Config>* receive_configs, 2585 VideoEncoderConfig* encoder_config) override { 2586 send_config->pre_encode_callback = this; // Used to inject delay. 2587 expected_cname_ = send_config->rtp.c_name = "SomeCName"; 2588 2589 const std::vector<uint32_t>& ssrcs = send_config->rtp.ssrcs; 2590 for (size_t i = 0; i < ssrcs.size(); ++i) { 2591 expected_send_ssrcs_.insert(ssrcs[i]); 2592 expected_receive_ssrcs_.push_back( 2593 (*receive_configs)[i].rtp.remote_ssrc); 2594 (*receive_configs)[i].render_delay_ms = kExpectedRenderDelayMs; 2595 } 2596 } 2597 2598 size_t GetNumVideoStreams() const override { return kNumSsrcs; } 2599 2600 void OnVideoStreamsCreated( 2601 VideoSendStream* send_stream, 2602 const std::vector<VideoReceiveStream*>& receive_streams) override { 2603 send_stream_ = send_stream; 2604 receive_streams_ = receive_streams; 2605 } 2606 2607 void PerformTest() override { 2608 Clock* clock = Clock::GetRealTimeClock(); 2609 int64_t now = clock->TimeInMilliseconds(); 2610 int64_t stop_time = now + test::CallTest::kLongTimeoutMs; 2611 bool receive_ok = false; 2612 bool send_ok = false; 2613 2614 while (now < stop_time) { 2615 if (!receive_ok) 2616 receive_ok = CheckReceiveStats(); 2617 if (!send_ok) 2618 send_ok = CheckSendStats(); 2619 2620 if (receive_ok && send_ok) 2621 return; 2622 2623 int64_t time_until_timout_ = stop_time - now; 2624 if (time_until_timout_ > 0) 2625 check_stats_event_.Wait(time_until_timout_); 2626 now = clock->TimeInMilliseconds(); 2627 } 2628 2629 ADD_FAILURE() << "Timed out waiting for filled stats."; 2630 for (std::map<std::string, bool>::const_iterator it = 2631 receive_stats_filled_.begin(); 2632 it != receive_stats_filled_.end(); 2633 ++it) { 2634 if (!it->second) { 2635 ADD_FAILURE() << "Missing receive stats: " << it->first; 2636 } 2637 } 2638 2639 for (std::map<std::string, bool>::const_iterator it = 2640 send_stats_filled_.begin(); 2641 it != send_stats_filled_.end(); 2642 ++it) { 2643 if (!it->second) { 2644 ADD_FAILURE() << "Missing send stats: " << it->first; 2645 } 2646 } 2647 } 2648 2649 std::vector<VideoReceiveStream*> receive_streams_; 2650 std::map<std::string, bool> receive_stats_filled_; 2651 2652 VideoSendStream* send_stream_; 2653 std::map<std::string, bool> send_stats_filled_; 2654 2655 std::vector<uint32_t> expected_receive_ssrcs_; 2656 std::set<uint32_t> expected_send_ssrcs_; 2657 std::string expected_cname_; 2658 2659 rtc::Event check_stats_event_; 2660 } test; 2661 2662 RunBaseTest(&test); 2663 } 2664 2665 TEST_F(EndToEndTest, ReceiverReferenceTimeReportEnabled) { 2666 TestXrReceiverReferenceTimeReport(true); 2667 } 2668 2669 TEST_F(EndToEndTest, ReceiverReferenceTimeReportDisabled) { 2670 TestXrReceiverReferenceTimeReport(false); 2671 } 2672 2673 TEST_F(EndToEndTest, TestReceivedRtpPacketStats) { 2674 static const size_t kNumRtpPacketsToSend = 5; 2675 class ReceivedRtpStatsObserver : public test::EndToEndTest { 2676 public: 2677 ReceivedRtpStatsObserver() 2678 : EndToEndTest(kDefaultTimeoutMs), 2679 receive_stream_(nullptr), 2680 sent_rtp_(0) {} 2681 2682 private: 2683 void OnVideoStreamsCreated( 2684 VideoSendStream* send_stream, 2685 const std::vector<VideoReceiveStream*>& receive_streams) override { 2686 receive_stream_ = receive_streams[0]; 2687 } 2688 2689 Action OnSendRtp(const uint8_t* packet, size_t length) override { 2690 if (sent_rtp_ >= kNumRtpPacketsToSend) { 2691 VideoReceiveStream::Stats stats = receive_stream_->GetStats(); 2692 if (kNumRtpPacketsToSend == stats.rtp_stats.transmitted.packets) { 2693 observation_complete_.Set(); 2694 } 2695 return DROP_PACKET; 2696 } 2697 ++sent_rtp_; 2698 return SEND_PACKET; 2699 } 2700 2701 void PerformTest() override { 2702 EXPECT_TRUE(Wait()) 2703 << "Timed out while verifying number of received RTP packets."; 2704 } 2705 2706 VideoReceiveStream* receive_stream_; 2707 uint32_t sent_rtp_; 2708 } test; 2709 2710 RunBaseTest(&test); 2711 } 2712 2713 TEST_F(EndToEndTest, SendsSetSsrc) { TestSendsSetSsrcs(1, false); } 2714 2715 TEST_F(EndToEndTest, SendsSetSimulcastSsrcs) { 2716 TestSendsSetSsrcs(kNumSsrcs, false); 2717 } 2718 2719 TEST_F(EndToEndTest, CanSwitchToUseAllSsrcs) { 2720 TestSendsSetSsrcs(kNumSsrcs, true); 2721 } 2722 2723 TEST_F(EndToEndTest, DISABLED_RedundantPayloadsTransmittedOnAllSsrcs) { 2724 class ObserveRedundantPayloads: public test::EndToEndTest { 2725 public: 2726 ObserveRedundantPayloads() 2727 : EndToEndTest(kDefaultTimeoutMs), ssrcs_to_observe_(kNumSsrcs) { 2728 for (size_t i = 0; i < kNumSsrcs; ++i) { 2729 registered_rtx_ssrc_[kSendRtxSsrcs[i]] = true; 2730 } 2731 } 2732 2733 private: 2734 Action OnSendRtp(const uint8_t* packet, size_t length) override { 2735 RTPHeader header; 2736 EXPECT_TRUE(parser_->Parse(packet, length, &header)); 2737 2738 if (!registered_rtx_ssrc_[header.ssrc]) 2739 return SEND_PACKET; 2740 2741 EXPECT_LE(header.headerLength + header.paddingLength, length); 2742 const bool packet_is_redundant_payload = 2743 header.headerLength + header.paddingLength < length; 2744 2745 if (!packet_is_redundant_payload) 2746 return SEND_PACKET; 2747 2748 if (!observed_redundant_retransmission_[header.ssrc]) { 2749 observed_redundant_retransmission_[header.ssrc] = true; 2750 if (--ssrcs_to_observe_ == 0) 2751 observation_complete_.Set(); 2752 } 2753 2754 return SEND_PACKET; 2755 } 2756 2757 size_t GetNumVideoStreams() const override { return kNumSsrcs; } 2758 2759 void ModifyVideoConfigs( 2760 VideoSendStream::Config* send_config, 2761 std::vector<VideoReceiveStream::Config>* receive_configs, 2762 VideoEncoderConfig* encoder_config) override { 2763 // Set low simulcast bitrates to not have to wait for bandwidth ramp-up. 2764 for (size_t i = 0; i < encoder_config->streams.size(); ++i) { 2765 encoder_config->streams[i].min_bitrate_bps = 10000; 2766 encoder_config->streams[i].target_bitrate_bps = 15000; 2767 encoder_config->streams[i].max_bitrate_bps = 20000; 2768 } 2769 2770 send_config->rtp.rtx.payload_type = kSendRtxPayloadType; 2771 2772 for (size_t i = 0; i < kNumSsrcs; ++i) 2773 send_config->rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[i]); 2774 2775 // Significantly higher than max bitrates for all video streams -> forcing 2776 // padding to trigger redundant padding on all RTX SSRCs. 2777 encoder_config->min_transmit_bitrate_bps = 100000; 2778 } 2779 2780 void PerformTest() override { 2781 EXPECT_TRUE(Wait()) 2782 << "Timed out while waiting for redundant payloads on all SSRCs."; 2783 } 2784 2785 private: 2786 size_t ssrcs_to_observe_; 2787 std::map<uint32_t, bool> observed_redundant_retransmission_; 2788 std::map<uint32_t, bool> registered_rtx_ssrc_; 2789 } test; 2790 2791 RunBaseTest(&test); 2792 } 2793 2794 void EndToEndTest::TestRtpStatePreservation(bool use_rtx) { 2795 static const uint32_t kMaxSequenceNumberGap = 100; 2796 static const uint64_t kMaxTimestampGap = kDefaultTimeoutMs * 90; 2797 class RtpSequenceObserver : public test::RtpRtcpObserver { 2798 public: 2799 explicit RtpSequenceObserver(bool use_rtx) 2800 : test::RtpRtcpObserver(kDefaultTimeoutMs), 2801 ssrcs_to_observe_(kNumSsrcs) { 2802 for (size_t i = 0; i < kNumSsrcs; ++i) { 2803 configured_ssrcs_[kVideoSendSsrcs[i]] = true; 2804 if (use_rtx) 2805 configured_ssrcs_[kSendRtxSsrcs[i]] = true; 2806 } 2807 } 2808 2809 void ResetExpectedSsrcs(size_t num_expected_ssrcs) { 2810 rtc::CritScope lock(&crit_); 2811 ssrc_observed_.clear(); 2812 ssrcs_to_observe_ = num_expected_ssrcs; 2813 } 2814 2815 private: 2816 Action OnSendRtp(const uint8_t* packet, size_t length) override { 2817 RTPHeader header; 2818 EXPECT_TRUE(parser_->Parse(packet, length, &header)); 2819 const uint32_t ssrc = header.ssrc; 2820 const uint16_t sequence_number = header.sequenceNumber; 2821 const uint32_t timestamp = header.timestamp; 2822 const bool only_padding = 2823 header.headerLength + header.paddingLength == length; 2824 2825 EXPECT_TRUE(configured_ssrcs_[ssrc]) 2826 << "Received SSRC that wasn't configured: " << ssrc; 2827 2828 std::map<uint32_t, uint16_t>::iterator it = 2829 last_observed_sequence_number_.find(header.ssrc); 2830 if (it == last_observed_sequence_number_.end()) { 2831 last_observed_sequence_number_[ssrc] = sequence_number; 2832 last_observed_timestamp_[ssrc] = timestamp; 2833 } else { 2834 // Verify sequence numbers are reasonably close. 2835 uint32_t extended_sequence_number = sequence_number; 2836 // Check for roll-over. 2837 if (sequence_number < last_observed_sequence_number_[ssrc]) 2838 extended_sequence_number += 0xFFFFu + 1; 2839 EXPECT_LE( 2840 extended_sequence_number - last_observed_sequence_number_[ssrc], 2841 kMaxSequenceNumberGap) 2842 << "Gap in sequence numbers (" 2843 << last_observed_sequence_number_[ssrc] << " -> " << sequence_number 2844 << ") too large for SSRC: " << ssrc << "."; 2845 last_observed_sequence_number_[ssrc] = sequence_number; 2846 2847 // TODO(pbos): Remove this check if we ever have monotonically 2848 // increasing timestamps. Right now padding packets add a delta which 2849 // can cause reordering between padding packets and regular packets, 2850 // hence we drop padding-only packets to not flake. 2851 if (only_padding) { 2852 // Verify that timestamps are reasonably close. 2853 uint64_t extended_timestamp = timestamp; 2854 // Check for roll-over. 2855 if (timestamp < last_observed_timestamp_[ssrc]) 2856 extended_timestamp += static_cast<uint64_t>(0xFFFFFFFFu) + 1; 2857 EXPECT_LE(extended_timestamp - last_observed_timestamp_[ssrc], 2858 kMaxTimestampGap) 2859 << "Gap in timestamps (" << last_observed_timestamp_[ssrc] 2860 << " -> " << timestamp << ") too large for SSRC: " << ssrc << "."; 2861 } 2862 last_observed_timestamp_[ssrc] = timestamp; 2863 } 2864 2865 rtc::CritScope lock(&crit_); 2866 // Wait for media packets on all ssrcs. 2867 if (!ssrc_observed_[ssrc] && !only_padding) { 2868 ssrc_observed_[ssrc] = true; 2869 if (--ssrcs_to_observe_ == 0) 2870 observation_complete_.Set(); 2871 } 2872 2873 return SEND_PACKET; 2874 } 2875 2876 std::map<uint32_t, uint16_t> last_observed_sequence_number_; 2877 std::map<uint32_t, uint32_t> last_observed_timestamp_; 2878 std::map<uint32_t, bool> configured_ssrcs_; 2879 2880 rtc::CriticalSection crit_; 2881 size_t ssrcs_to_observe_ GUARDED_BY(crit_); 2882 std::map<uint32_t, bool> ssrc_observed_ GUARDED_BY(crit_); 2883 } observer(use_rtx); 2884 2885 CreateCalls(Call::Config(), Call::Config()); 2886 2887 test::PacketTransport send_transport(sender_call_.get(), &observer, 2888 test::PacketTransport::kSender, 2889 FakeNetworkPipe::Config()); 2890 test::PacketTransport receive_transport(nullptr, &observer, 2891 test::PacketTransport::kReceiver, 2892 FakeNetworkPipe::Config()); 2893 send_transport.SetReceiver(receiver_call_->Receiver()); 2894 receive_transport.SetReceiver(sender_call_->Receiver()); 2895 2896 CreateSendConfig(kNumSsrcs, 0, &send_transport); 2897 2898 if (use_rtx) { 2899 for (size_t i = 0; i < kNumSsrcs; ++i) { 2900 video_send_config_.rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[i]); 2901 } 2902 video_send_config_.rtp.rtx.payload_type = kSendRtxPayloadType; 2903 } 2904 2905 // Lower bitrates so that all streams send initially. 2906 for (size_t i = 0; i < video_encoder_config_.streams.size(); ++i) { 2907 video_encoder_config_.streams[i].min_bitrate_bps = 10000; 2908 video_encoder_config_.streams[i].target_bitrate_bps = 15000; 2909 video_encoder_config_.streams[i].max_bitrate_bps = 20000; 2910 } 2911 2912 // Use the same total bitrates when sending a single stream to avoid lowering 2913 // the bitrate estimate and requiring a subsequent rampup. 2914 VideoEncoderConfig one_stream = video_encoder_config_; 2915 one_stream.streams.resize(1); 2916 for (size_t i = 1; i < video_encoder_config_.streams.size(); ++i) { 2917 one_stream.streams.front().min_bitrate_bps += 2918 video_encoder_config_.streams[i].min_bitrate_bps; 2919 one_stream.streams.front().target_bitrate_bps += 2920 video_encoder_config_.streams[i].target_bitrate_bps; 2921 one_stream.streams.front().max_bitrate_bps += 2922 video_encoder_config_.streams[i].max_bitrate_bps; 2923 } 2924 2925 CreateMatchingReceiveConfigs(&receive_transport); 2926 2927 CreateVideoStreams(); 2928 CreateFrameGeneratorCapturer(); 2929 2930 Start(); 2931 EXPECT_TRUE(observer.Wait()) 2932 << "Timed out waiting for all SSRCs to send packets."; 2933 2934 // Test stream resetting more than once to make sure that the state doesn't 2935 // get set once (this could be due to using std::map::insert for instance). 2936 for (size_t i = 0; i < 3; ++i) { 2937 frame_generator_capturer_->Stop(); 2938 sender_call_->DestroyVideoSendStream(video_send_stream_); 2939 2940 // Re-create VideoSendStream with only one stream. 2941 video_send_stream_ = 2942 sender_call_->CreateVideoSendStream(video_send_config_, one_stream); 2943 video_send_stream_->Start(); 2944 CreateFrameGeneratorCapturer(); 2945 frame_generator_capturer_->Start(); 2946 2947 observer.ResetExpectedSsrcs(1); 2948 EXPECT_TRUE(observer.Wait()) << "Timed out waiting for single RTP packet."; 2949 2950 // Reconfigure back to use all streams. 2951 video_send_stream_->ReconfigureVideoEncoder(video_encoder_config_); 2952 observer.ResetExpectedSsrcs(kNumSsrcs); 2953 EXPECT_TRUE(observer.Wait()) 2954 << "Timed out waiting for all SSRCs to send packets."; 2955 2956 // Reconfigure down to one stream. 2957 video_send_stream_->ReconfigureVideoEncoder(one_stream); 2958 observer.ResetExpectedSsrcs(1); 2959 EXPECT_TRUE(observer.Wait()) << "Timed out waiting for single RTP packet."; 2960 2961 // Reconfigure back to use all streams. 2962 video_send_stream_->ReconfigureVideoEncoder(video_encoder_config_); 2963 observer.ResetExpectedSsrcs(kNumSsrcs); 2964 EXPECT_TRUE(observer.Wait()) 2965 << "Timed out waiting for all SSRCs to send packets."; 2966 } 2967 2968 send_transport.StopSending(); 2969 receive_transport.StopSending(); 2970 2971 Stop(); 2972 DestroyStreams(); 2973 } 2974 2975 TEST_F(EndToEndTest, DISABLED_RestartingSendStreamPreservesRtpState) { 2976 TestRtpStatePreservation(false); 2977 } 2978 2979 TEST_F(EndToEndTest, RestartingSendStreamPreservesRtpStatesWithRtx) { 2980 TestRtpStatePreservation(true); 2981 } 2982 2983 TEST_F(EndToEndTest, RespectsNetworkState) { 2984 // TODO(pbos): Remove accepted downtime packets etc. when signaling network 2985 // down blocks until no more packets will be sent. 2986 2987 // Pacer will send from its packet list and then send required padding before 2988 // checking paused_ again. This should be enough for one round of pacing, 2989 // otherwise increase. 2990 static const int kNumAcceptedDowntimeRtp = 5; 2991 // A single RTCP may be in the pipeline. 2992 static const int kNumAcceptedDowntimeRtcp = 1; 2993 class NetworkStateTest : public test::EndToEndTest, public test::FakeEncoder { 2994 public: 2995 NetworkStateTest() 2996 : EndToEndTest(kDefaultTimeoutMs), 2997 FakeEncoder(Clock::GetRealTimeClock()), 2998 encoded_frames_(false, false), 2999 packet_event_(false, false), 3000 sender_call_(nullptr), 3001 receiver_call_(nullptr), 3002 sender_state_(kNetworkUp), 3003 sender_rtp_(0), 3004 sender_rtcp_(0), 3005 receiver_rtcp_(0), 3006 down_frames_(0) {} 3007 3008 Action OnSendRtp(const uint8_t* packet, size_t length) override { 3009 rtc::CritScope lock(&test_crit_); 3010 ++sender_rtp_; 3011 packet_event_.Set(); 3012 return SEND_PACKET; 3013 } 3014 3015 Action OnSendRtcp(const uint8_t* packet, size_t length) override { 3016 rtc::CritScope lock(&test_crit_); 3017 ++sender_rtcp_; 3018 packet_event_.Set(); 3019 return SEND_PACKET; 3020 } 3021 3022 Action OnReceiveRtp(const uint8_t* packet, size_t length) override { 3023 ADD_FAILURE() << "Unexpected receiver RTP, should not be sending."; 3024 return SEND_PACKET; 3025 } 3026 3027 Action OnReceiveRtcp(const uint8_t* packet, size_t length) override { 3028 rtc::CritScope lock(&test_crit_); 3029 ++receiver_rtcp_; 3030 packet_event_.Set(); 3031 return SEND_PACKET; 3032 } 3033 3034 void OnCallsCreated(Call* sender_call, Call* receiver_call) override { 3035 sender_call_ = sender_call; 3036 receiver_call_ = receiver_call; 3037 } 3038 3039 void ModifyVideoConfigs( 3040 VideoSendStream::Config* send_config, 3041 std::vector<VideoReceiveStream::Config>* receive_configs, 3042 VideoEncoderConfig* encoder_config) override { 3043 send_config->encoder_settings.encoder = this; 3044 } 3045 3046 void PerformTest() override { 3047 EXPECT_TRUE(encoded_frames_.Wait(kDefaultTimeoutMs)) 3048 << "No frames received by the encoder."; 3049 // Wait for packets from both sender/receiver. 3050 WaitForPacketsOrSilence(false, false); 3051 3052 // Sender-side network down. 3053 sender_call_->SignalNetworkState(kNetworkDown); 3054 { 3055 rtc::CritScope lock(&test_crit_); 3056 // After network goes down we shouldn't be encoding more frames. 3057 sender_state_ = kNetworkDown; 3058 } 3059 // Wait for receiver-packets and no sender packets. 3060 WaitForPacketsOrSilence(true, false); 3061 3062 // Receiver-side network down. 3063 receiver_call_->SignalNetworkState(kNetworkDown); 3064 WaitForPacketsOrSilence(true, true); 3065 3066 // Network back up again for both. 3067 { 3068 rtc::CritScope lock(&test_crit_); 3069 // It's OK to encode frames again, as we're about to bring up the 3070 // network. 3071 sender_state_ = kNetworkUp; 3072 } 3073 sender_call_->SignalNetworkState(kNetworkUp); 3074 receiver_call_->SignalNetworkState(kNetworkUp); 3075 WaitForPacketsOrSilence(false, false); 3076 } 3077 3078 int32_t Encode(const VideoFrame& input_image, 3079 const CodecSpecificInfo* codec_specific_info, 3080 const std::vector<FrameType>* frame_types) override { 3081 { 3082 rtc::CritScope lock(&test_crit_); 3083 if (sender_state_ == kNetworkDown) { 3084 ++down_frames_; 3085 EXPECT_LE(down_frames_, 1) 3086 << "Encoding more than one frame while network is down."; 3087 if (down_frames_ > 1) 3088 encoded_frames_.Set(); 3089 } else { 3090 encoded_frames_.Set(); 3091 } 3092 } 3093 return test::FakeEncoder::Encode( 3094 input_image, codec_specific_info, frame_types); 3095 } 3096 3097 private: 3098 void WaitForPacketsOrSilence(bool sender_down, bool receiver_down) { 3099 int64_t initial_time_ms = clock_->TimeInMilliseconds(); 3100 int initial_sender_rtp; 3101 int initial_sender_rtcp; 3102 int initial_receiver_rtcp; 3103 { 3104 rtc::CritScope lock(&test_crit_); 3105 initial_sender_rtp = sender_rtp_; 3106 initial_sender_rtcp = sender_rtcp_; 3107 initial_receiver_rtcp = receiver_rtcp_; 3108 } 3109 bool sender_done = false; 3110 bool receiver_done = false; 3111 while (!sender_done || !receiver_done) { 3112 packet_event_.Wait(kSilenceTimeoutMs); 3113 int64_t time_now_ms = clock_->TimeInMilliseconds(); 3114 rtc::CritScope lock(&test_crit_); 3115 if (sender_down) { 3116 ASSERT_LE(sender_rtp_ - initial_sender_rtp, kNumAcceptedDowntimeRtp) 3117 << "RTP sent during sender-side downtime."; 3118 ASSERT_LE(sender_rtcp_ - initial_sender_rtcp, 3119 kNumAcceptedDowntimeRtcp) 3120 << "RTCP sent during sender-side downtime."; 3121 if (time_now_ms - initial_time_ms >= 3122 static_cast<int64_t>(kSilenceTimeoutMs)) { 3123 sender_done = true; 3124 } 3125 } else { 3126 if (sender_rtp_ > initial_sender_rtp) 3127 sender_done = true; 3128 } 3129 if (receiver_down) { 3130 ASSERT_LE(receiver_rtcp_ - initial_receiver_rtcp, 3131 kNumAcceptedDowntimeRtcp) 3132 << "RTCP sent during receiver-side downtime."; 3133 if (time_now_ms - initial_time_ms >= 3134 static_cast<int64_t>(kSilenceTimeoutMs)) { 3135 receiver_done = true; 3136 } 3137 } else { 3138 if (receiver_rtcp_ > initial_receiver_rtcp) 3139 receiver_done = true; 3140 } 3141 } 3142 } 3143 3144 rtc::CriticalSection test_crit_; 3145 rtc::Event encoded_frames_; 3146 rtc::Event packet_event_; 3147 Call* sender_call_; 3148 Call* receiver_call_; 3149 NetworkState sender_state_ GUARDED_BY(test_crit_); 3150 int sender_rtp_ GUARDED_BY(test_crit_); 3151 int sender_rtcp_ GUARDED_BY(test_crit_); 3152 int receiver_rtcp_ GUARDED_BY(test_crit_); 3153 int down_frames_ GUARDED_BY(test_crit_); 3154 } test; 3155 3156 RunBaseTest(&test); 3157 } 3158 3159 TEST_F(EndToEndTest, CallReportsRttForSender) { 3160 static const int kSendDelayMs = 30; 3161 static const int kReceiveDelayMs = 70; 3162 3163 CreateCalls(Call::Config(), Call::Config()); 3164 3165 FakeNetworkPipe::Config config; 3166 config.queue_delay_ms = kSendDelayMs; 3167 test::DirectTransport sender_transport(config, sender_call_.get()); 3168 config.queue_delay_ms = kReceiveDelayMs; 3169 test::DirectTransport receiver_transport(config, receiver_call_.get()); 3170 sender_transport.SetReceiver(receiver_call_->Receiver()); 3171 receiver_transport.SetReceiver(sender_call_->Receiver()); 3172 3173 CreateSendConfig(1, 0, &sender_transport); 3174 CreateMatchingReceiveConfigs(&receiver_transport); 3175 3176 CreateVideoStreams(); 3177 CreateFrameGeneratorCapturer(); 3178 Start(); 3179 3180 int64_t start_time_ms = clock_->TimeInMilliseconds(); 3181 while (true) { 3182 Call::Stats stats = sender_call_->GetStats(); 3183 ASSERT_GE(start_time_ms + kDefaultTimeoutMs, 3184 clock_->TimeInMilliseconds()) 3185 << "No RTT stats before timeout!"; 3186 if (stats.rtt_ms != -1) { 3187 EXPECT_GE(stats.rtt_ms, kSendDelayMs + kReceiveDelayMs); 3188 break; 3189 } 3190 SleepMs(10); 3191 } 3192 3193 Stop(); 3194 DestroyStreams(); 3195 } 3196 3197 TEST_F(EndToEndTest, NewSendStreamsRespectNetworkDown) { 3198 class UnusedEncoder : public test::FakeEncoder { 3199 public: 3200 UnusedEncoder() : FakeEncoder(Clock::GetRealTimeClock()) {} 3201 int32_t Encode(const VideoFrame& input_image, 3202 const CodecSpecificInfo* codec_specific_info, 3203 const std::vector<FrameType>* frame_types) override { 3204 ADD_FAILURE() << "Unexpected frame encode."; 3205 return test::FakeEncoder::Encode( 3206 input_image, codec_specific_info, frame_types); 3207 } 3208 }; 3209 3210 CreateSenderCall(Call::Config()); 3211 sender_call_->SignalNetworkState(kNetworkDown); 3212 3213 UnusedTransport transport; 3214 CreateSendConfig(1, 0, &transport); 3215 UnusedEncoder unused_encoder; 3216 video_send_config_.encoder_settings.encoder = &unused_encoder; 3217 CreateVideoStreams(); 3218 CreateFrameGeneratorCapturer(); 3219 3220 Start(); 3221 SleepMs(kSilenceTimeoutMs); 3222 Stop(); 3223 3224 DestroyStreams(); 3225 } 3226 3227 TEST_F(EndToEndTest, NewReceiveStreamsRespectNetworkDown) { 3228 CreateCalls(Call::Config(), Call::Config()); 3229 receiver_call_->SignalNetworkState(kNetworkDown); 3230 3231 test::DirectTransport sender_transport(sender_call_.get()); 3232 sender_transport.SetReceiver(receiver_call_->Receiver()); 3233 CreateSendConfig(1, 0, &sender_transport); 3234 UnusedTransport transport; 3235 CreateMatchingReceiveConfigs(&transport); 3236 CreateVideoStreams(); 3237 CreateFrameGeneratorCapturer(); 3238 3239 Start(); 3240 SleepMs(kSilenceTimeoutMs); 3241 Stop(); 3242 3243 sender_transport.StopSending(); 3244 3245 DestroyStreams(); 3246 } 3247 3248 void VerifyEmptyNackConfig(const NackConfig& config) { 3249 EXPECT_EQ(0, config.rtp_history_ms) 3250 << "Enabling NACK requires rtcp-fb: nack negotiation."; 3251 } 3252 3253 void VerifyEmptyFecConfig(const FecConfig& config) { 3254 EXPECT_EQ(-1, config.ulpfec_payload_type) 3255 << "Enabling FEC requires rtpmap: ulpfec negotiation."; 3256 EXPECT_EQ(-1, config.red_payload_type) 3257 << "Enabling FEC requires rtpmap: red negotiation."; 3258 EXPECT_EQ(-1, config.red_rtx_payload_type) 3259 << "Enabling RTX in FEC requires rtpmap: rtx negotiation."; 3260 } 3261 3262 TEST_F(EndToEndTest, VerifyDefaultSendConfigParameters) { 3263 VideoSendStream::Config default_send_config(nullptr); 3264 EXPECT_EQ(0, default_send_config.rtp.nack.rtp_history_ms) 3265 << "Enabling NACK require rtcp-fb: nack negotiation."; 3266 EXPECT_TRUE(default_send_config.rtp.rtx.ssrcs.empty()) 3267 << "Enabling RTX requires rtpmap: rtx negotiation."; 3268 EXPECT_TRUE(default_send_config.rtp.extensions.empty()) 3269 << "Enabling RTP extensions require negotiation."; 3270 3271 VerifyEmptyNackConfig(default_send_config.rtp.nack); 3272 VerifyEmptyFecConfig(default_send_config.rtp.fec); 3273 } 3274 3275 TEST_F(EndToEndTest, VerifyDefaultReceiveConfigParameters) { 3276 VideoReceiveStream::Config default_receive_config(nullptr); 3277 EXPECT_EQ(RtcpMode::kCompound, default_receive_config.rtp.rtcp_mode) 3278 << "Reduced-size RTCP require rtcp-rsize to be negotiated."; 3279 EXPECT_FALSE(default_receive_config.rtp.remb) 3280 << "REMB require rtcp-fb: goog-remb to be negotiated."; 3281 EXPECT_FALSE( 3282 default_receive_config.rtp.rtcp_xr.receiver_reference_time_report) 3283 << "RTCP XR settings require rtcp-xr to be negotiated."; 3284 EXPECT_TRUE(default_receive_config.rtp.rtx.empty()) 3285 << "Enabling RTX requires rtpmap: rtx negotiation."; 3286 EXPECT_TRUE(default_receive_config.rtp.extensions.empty()) 3287 << "Enabling RTP extensions require negotiation."; 3288 3289 VerifyEmptyNackConfig(default_receive_config.rtp.nack); 3290 VerifyEmptyFecConfig(default_receive_config.rtp.fec); 3291 } 3292 3293 TEST_F(EndToEndTest, TransportSeqNumOnAudioAndVideo) { 3294 static const int kExtensionId = 8; 3295 class TransportSequenceNumberTest : public test::EndToEndTest { 3296 public: 3297 TransportSequenceNumberTest() 3298 : EndToEndTest(kDefaultTimeoutMs), 3299 video_observed_(false), 3300 audio_observed_(false) { 3301 parser_->RegisterRtpHeaderExtension(kRtpExtensionTransportSequenceNumber, 3302 kExtensionId); 3303 } 3304 3305 size_t GetNumVideoStreams() const override { return 1; } 3306 size_t GetNumAudioStreams() const override { return 1; } 3307 3308 void ModifyVideoConfigs( 3309 VideoSendStream::Config* send_config, 3310 std::vector<VideoReceiveStream::Config>* receive_configs, 3311 VideoEncoderConfig* encoder_config) override { 3312 send_config->rtp.extensions.clear(); 3313 send_config->rtp.extensions.push_back( 3314 RtpExtension(RtpExtension::kTransportSequenceNumber, kExtensionId)); 3315 (*receive_configs)[0].rtp.extensions = send_config->rtp.extensions; 3316 } 3317 3318 void ModifyAudioConfigs( 3319 AudioSendStream::Config* send_config, 3320 std::vector<AudioReceiveStream::Config>* receive_configs) override { 3321 send_config->rtp.extensions.clear(); 3322 send_config->rtp.extensions.push_back( 3323 RtpExtension(RtpExtension::kTransportSequenceNumber, kExtensionId)); 3324 (*receive_configs)[0].rtp.extensions.clear(); 3325 (*receive_configs)[0].rtp.extensions = send_config->rtp.extensions; 3326 } 3327 3328 Action OnSendRtp(const uint8_t* packet, size_t length) override { 3329 RTPHeader header; 3330 EXPECT_TRUE(parser_->Parse(packet, length, &header)); 3331 EXPECT_TRUE(header.extension.hasTransportSequenceNumber); 3332 // Unwrap packet id and verify uniqueness. 3333 int64_t packet_id = 3334 unwrapper_.Unwrap(header.extension.transportSequenceNumber); 3335 EXPECT_TRUE(received_packet_ids_.insert(packet_id).second); 3336 3337 if (header.ssrc == kVideoSendSsrcs[0]) 3338 video_observed_ = true; 3339 if (header.ssrc == kAudioSendSsrc) 3340 audio_observed_ = true; 3341 if (audio_observed_ && video_observed_ && 3342 received_packet_ids_.size() == 50) { 3343 size_t packet_id_range = 3344 *received_packet_ids_.rbegin() - *received_packet_ids_.begin() + 1; 3345 EXPECT_EQ(received_packet_ids_.size(), packet_id_range); 3346 observation_complete_.Set(); 3347 } 3348 return SEND_PACKET; 3349 } 3350 3351 void PerformTest() override { 3352 EXPECT_TRUE(Wait()) << "Timed out while waiting for audio and video " 3353 "packets with transport sequence number."; 3354 } 3355 3356 private: 3357 bool video_observed_; 3358 bool audio_observed_; 3359 SequenceNumberUnwrapper unwrapper_; 3360 std::set<int64_t> received_packet_ids_; 3361 } test; 3362 3363 RunBaseTest(&test); 3364 } 3365 } // namespace webrtc 3366