Home | History | Annotate | Download | only in video
      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 <assert.h>
     11 
     12 #include <algorithm>
     13 #include <map>
     14 #include <sstream>
     15 #include <string>
     16 
     17 #include "testing/gtest/include/gtest/gtest.h"
     18 
     19 #include "webrtc/call.h"
     20 #include "webrtc/frame_callback.h"
     21 #include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h"
     22 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
     23 #include "webrtc/system_wrappers/interface/event_wrapper.h"
     24 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
     25 #include "webrtc/system_wrappers/interface/sleep.h"
     26 #include "webrtc/test/call_test.h"
     27 #include "webrtc/test/direct_transport.h"
     28 #include "webrtc/test/encoder_settings.h"
     29 #include "webrtc/test/fake_audio_device.h"
     30 #include "webrtc/test/fake_decoder.h"
     31 #include "webrtc/test/fake_encoder.h"
     32 #include "webrtc/test/frame_generator.h"
     33 #include "webrtc/test/frame_generator_capturer.h"
     34 #include "webrtc/test/null_transport.h"
     35 #include "webrtc/test/rtp_rtcp_observer.h"
     36 #include "webrtc/test/testsupport/fileutils.h"
     37 #include "webrtc/test/testsupport/gtest_disable.h"
     38 #include "webrtc/test/testsupport/perf_test.h"
     39 #include "webrtc/video/transport_adapter.h"
     40 #include "webrtc/video_encoder.h"
     41 
     42 namespace webrtc {
     43 
     44 static const unsigned long kSilenceTimeoutMs = 2000;
     45 
     46 class EndToEndTest : public test::CallTest {
     47  public:
     48   EndToEndTest() {}
     49 
     50   virtual ~EndToEndTest() {
     51     EXPECT_EQ(NULL, send_stream_);
     52     EXPECT_TRUE(receive_streams_.empty());
     53   }
     54 
     55  protected:
     56   class UnusedTransport : public newapi::Transport {
     57    private:
     58     virtual bool SendRtp(const uint8_t* packet, size_t length) OVERRIDE {
     59       ADD_FAILURE() << "Unexpected RTP sent.";
     60       return false;
     61     }
     62 
     63     virtual bool SendRtcp(const uint8_t* packet, size_t length) OVERRIDE {
     64       ADD_FAILURE() << "Unexpected RTCP sent.";
     65       return false;
     66     }
     67   };
     68 
     69   void DecodesRetransmittedFrame(bool retransmit_over_rtx);
     70   void ReceivesPliAndRecovers(int rtp_history_ms);
     71   void RespectsRtcpMode(newapi::RtcpMode rtcp_mode);
     72   void TestXrReceiverReferenceTimeReport(bool enable_rrtr);
     73   void TestSendsSetSsrcs(size_t num_ssrcs, bool send_single_ssrc_first);
     74   void TestRtpStatePreservation(bool use_rtx);
     75 };
     76 
     77 TEST_F(EndToEndTest, ReceiverCanBeStartedTwice) {
     78   test::NullTransport transport;
     79   CreateCalls(Call::Config(&transport), Call::Config(&transport));
     80 
     81   CreateSendConfig(1);
     82   CreateMatchingReceiveConfigs();
     83 
     84   CreateStreams();
     85 
     86   receive_streams_[0]->Start();
     87   receive_streams_[0]->Start();
     88 
     89   DestroyStreams();
     90 }
     91 
     92 TEST_F(EndToEndTest, ReceiverCanBeStoppedTwice) {
     93   test::NullTransport transport;
     94   CreateCalls(Call::Config(&transport), Call::Config(&transport));
     95 
     96   CreateSendConfig(1);
     97   CreateMatchingReceiveConfigs();
     98 
     99   CreateStreams();
    100 
    101   receive_streams_[0]->Stop();
    102   receive_streams_[0]->Stop();
    103 
    104   DestroyStreams();
    105 }
    106 
    107 TEST_F(EndToEndTest, RendersSingleDelayedFrame) {
    108   static const int kWidth = 320;
    109   static const int kHeight = 240;
    110   // This constant is chosen to be higher than the timeout in the video_render
    111   // module. This makes sure that frames aren't dropped if there are no other
    112   // frames in the queue.
    113   static const int kDelayRenderCallbackMs = 1000;
    114 
    115   class Renderer : public VideoRenderer {
    116    public:
    117     Renderer() : event_(EventWrapper::Create()) {}
    118 
    119     virtual void RenderFrame(const I420VideoFrame& video_frame,
    120                              int /*time_to_render_ms*/) OVERRIDE {
    121       event_->Set();
    122     }
    123 
    124     EventTypeWrapper Wait() { return event_->Wait(kDefaultTimeoutMs); }
    125 
    126     scoped_ptr<EventWrapper> event_;
    127   } renderer;
    128 
    129   class TestFrameCallback : public I420FrameCallback {
    130    public:
    131     TestFrameCallback() : event_(EventWrapper::Create()) {}
    132 
    133     EventTypeWrapper Wait() { return event_->Wait(kDefaultTimeoutMs); }
    134 
    135    private:
    136     virtual void FrameCallback(I420VideoFrame* frame) OVERRIDE {
    137       SleepMs(kDelayRenderCallbackMs);
    138       event_->Set();
    139     }
    140 
    141     scoped_ptr<EventWrapper> event_;
    142   };
    143 
    144   test::DirectTransport sender_transport, receiver_transport;
    145 
    146   CreateCalls(Call::Config(&sender_transport),
    147               Call::Config(&receiver_transport));
    148 
    149   sender_transport.SetReceiver(receiver_call_->Receiver());
    150   receiver_transport.SetReceiver(sender_call_->Receiver());
    151 
    152   CreateSendConfig(1);
    153   CreateMatchingReceiveConfigs();
    154 
    155   TestFrameCallback pre_render_callback;
    156   receive_configs_[0].pre_render_callback = &pre_render_callback;
    157   receive_configs_[0].renderer = &renderer;
    158 
    159   CreateStreams();
    160   Start();
    161 
    162   // Create frames that are smaller than the send width/height, this is done to
    163   // check that the callbacks are done after processing video.
    164   scoped_ptr<test::FrameGenerator> frame_generator(
    165       test::FrameGenerator::Create(kWidth, kHeight));
    166   send_stream_->Input()->SwapFrame(frame_generator->NextFrame());
    167   EXPECT_EQ(kEventSignaled, pre_render_callback.Wait())
    168       << "Timed out while waiting for pre-render callback.";
    169   EXPECT_EQ(kEventSignaled, renderer.Wait())
    170       << "Timed out while waiting for the frame to render.";
    171 
    172   Stop();
    173 
    174   sender_transport.StopSending();
    175   receiver_transport.StopSending();
    176 
    177   DestroyStreams();
    178 }
    179 
    180 TEST_F(EndToEndTest, TransmitsFirstFrame) {
    181   class Renderer : public VideoRenderer {
    182    public:
    183     Renderer() : event_(EventWrapper::Create()) {}
    184 
    185     virtual void RenderFrame(const I420VideoFrame& video_frame,
    186                              int /*time_to_render_ms*/) OVERRIDE {
    187       event_->Set();
    188     }
    189 
    190     EventTypeWrapper Wait() { return event_->Wait(kDefaultTimeoutMs); }
    191 
    192     scoped_ptr<EventWrapper> event_;
    193   } renderer;
    194 
    195   test::DirectTransport sender_transport, receiver_transport;
    196 
    197   CreateCalls(Call::Config(&sender_transport),
    198               Call::Config(&receiver_transport));
    199 
    200   sender_transport.SetReceiver(receiver_call_->Receiver());
    201   receiver_transport.SetReceiver(sender_call_->Receiver());
    202 
    203   CreateSendConfig(1);
    204   CreateMatchingReceiveConfigs();
    205   receive_configs_[0].renderer = &renderer;
    206 
    207   CreateStreams();
    208   Start();
    209 
    210   scoped_ptr<test::FrameGenerator> frame_generator(test::FrameGenerator::Create(
    211       encoder_config_.streams[0].width, encoder_config_.streams[0].height));
    212   send_stream_->Input()->SwapFrame(frame_generator->NextFrame());
    213 
    214   EXPECT_EQ(kEventSignaled, renderer.Wait())
    215       << "Timed out while waiting for the frame to render.";
    216 
    217   Stop();
    218 
    219   sender_transport.StopSending();
    220   receiver_transport.StopSending();
    221 
    222   DestroyStreams();
    223 }
    224 
    225 TEST_F(EndToEndTest, SendsAndReceivesH264) {
    226   class H264Observer : public test::EndToEndTest, public VideoRenderer {
    227    public:
    228     H264Observer()
    229         : EndToEndTest(2 * kDefaultTimeoutMs),
    230           fake_encoder_(Clock::GetRealTimeClock()),
    231           frame_counter_(0) {}
    232 
    233     virtual void PerformTest() OVERRIDE {
    234       EXPECT_EQ(kEventSignaled, Wait())
    235           << "Timed out while waiting for enough frames to be decoded.";
    236     }
    237 
    238     virtual void ModifyConfigs(
    239         VideoSendStream::Config* send_config,
    240         std::vector<VideoReceiveStream::Config>* receive_configs,
    241         VideoEncoderConfig* encoder_config) OVERRIDE {
    242       send_config->encoder_settings.encoder = &fake_encoder_;
    243       send_config->encoder_settings.payload_name = "H264";
    244       send_config->encoder_settings.payload_type = kFakeSendPayloadType;
    245       encoder_config->streams[0].min_bitrate_bps = 50000;
    246       encoder_config->streams[0].target_bitrate_bps =
    247           encoder_config->streams[0].max_bitrate_bps = 2000000;
    248 
    249       (*receive_configs)[0].renderer = this;
    250       VideoCodec codec =
    251           test::CreateDecoderVideoCodec(send_config->encoder_settings);
    252       (*receive_configs)[0].codecs.resize(1);
    253       (*receive_configs)[0].codecs[0] = codec;
    254       (*receive_configs)[0].external_decoders.resize(1);
    255       (*receive_configs)[0].external_decoders[0].payload_type =
    256           send_config->encoder_settings.payload_type;
    257       (*receive_configs)[0].external_decoders[0].decoder = &fake_decoder_;
    258     }
    259 
    260     virtual void RenderFrame(const I420VideoFrame& video_frame,
    261                              int time_to_render_ms) OVERRIDE {
    262       const int kRequiredFrames = 500;
    263       if (++frame_counter_ == kRequiredFrames)
    264         observation_complete_->Set();
    265     }
    266 
    267    private:
    268     test::FakeH264Decoder fake_decoder_;
    269     test::FakeH264Encoder fake_encoder_;
    270     int frame_counter_;
    271   } test;
    272 
    273   RunBaseTest(&test);
    274 }
    275 
    276 TEST_F(EndToEndTest, ReceiverUsesLocalSsrc) {
    277   class SyncRtcpObserver : public test::EndToEndTest {
    278    public:
    279     SyncRtcpObserver() : EndToEndTest(kDefaultTimeoutMs) {}
    280 
    281     virtual Action OnReceiveRtcp(const uint8_t* packet,
    282                                  size_t length) OVERRIDE {
    283       RTCPUtility::RTCPParserV2 parser(packet, length, true);
    284       EXPECT_TRUE(parser.IsValid());
    285       uint32_t ssrc = 0;
    286       ssrc |= static_cast<uint32_t>(packet[4]) << 24;
    287       ssrc |= static_cast<uint32_t>(packet[5]) << 16;
    288       ssrc |= static_cast<uint32_t>(packet[6]) << 8;
    289       ssrc |= static_cast<uint32_t>(packet[7]) << 0;
    290       EXPECT_EQ(kReceiverLocalSsrc, ssrc);
    291       observation_complete_->Set();
    292 
    293       return SEND_PACKET;
    294     }
    295 
    296     virtual void PerformTest() OVERRIDE {
    297       EXPECT_EQ(kEventSignaled, Wait())
    298           << "Timed out while waiting for a receiver RTCP packet to be sent.";
    299     }
    300   } test;
    301 
    302   RunBaseTest(&test);
    303 }
    304 
    305 TEST_F(EndToEndTest, ReceivesAndRetransmitsNack) {
    306   static const int kNumberOfNacksToObserve = 2;
    307   static const int kLossBurstSize = 2;
    308   static const int kPacketsBetweenLossBursts = 9;
    309   class NackObserver : public test::EndToEndTest {
    310    public:
    311     NackObserver()
    312         : EndToEndTest(kLongTimeoutMs),
    313           rtp_parser_(RtpHeaderParser::Create()),
    314           sent_rtp_packets_(0),
    315           packets_left_to_drop_(0),
    316           nacks_left_(kNumberOfNacksToObserve) {}
    317 
    318    private:
    319     virtual Action OnSendRtp(const uint8_t* packet, size_t length) OVERRIDE {
    320       RTPHeader header;
    321       EXPECT_TRUE(rtp_parser_->Parse(packet, length, &header));
    322 
    323       // Never drop retransmitted packets.
    324       if (dropped_packets_.find(header.sequenceNumber) !=
    325           dropped_packets_.end()) {
    326         retransmitted_packets_.insert(header.sequenceNumber);
    327         if (nacks_left_ == 0 &&
    328             retransmitted_packets_.size() == dropped_packets_.size()) {
    329           observation_complete_->Set();
    330         }
    331         return SEND_PACKET;
    332       }
    333 
    334       ++sent_rtp_packets_;
    335 
    336       // Enough NACKs received, stop dropping packets.
    337       if (nacks_left_ == 0)
    338         return SEND_PACKET;
    339 
    340       // Check if it's time for a new loss burst.
    341       if (sent_rtp_packets_ % kPacketsBetweenLossBursts == 0)
    342         packets_left_to_drop_ = kLossBurstSize;
    343 
    344       if (packets_left_to_drop_ > 0) {
    345         --packets_left_to_drop_;
    346         dropped_packets_.insert(header.sequenceNumber);
    347         return DROP_PACKET;
    348       }
    349 
    350       return SEND_PACKET;
    351     }
    352 
    353     virtual Action OnReceiveRtcp(const uint8_t* packet,
    354                                  size_t length) OVERRIDE {
    355       RTCPUtility::RTCPParserV2 parser(packet, length, true);
    356       EXPECT_TRUE(parser.IsValid());
    357 
    358       RTCPUtility::RTCPPacketTypes packet_type = parser.Begin();
    359       while (packet_type != RTCPUtility::kRtcpNotValidCode) {
    360         if (packet_type == RTCPUtility::kRtcpRtpfbNackCode) {
    361           --nacks_left_;
    362           break;
    363         }
    364         packet_type = parser.Iterate();
    365       }
    366       return SEND_PACKET;
    367     }
    368 
    369     virtual void ModifyConfigs(
    370         VideoSendStream::Config* send_config,
    371         std::vector<VideoReceiveStream::Config>* receive_configs,
    372         VideoEncoderConfig* encoder_config) OVERRIDE {
    373       send_config->rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
    374       (*receive_configs)[0].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
    375     }
    376 
    377     virtual void PerformTest() OVERRIDE {
    378       EXPECT_EQ(kEventSignaled, Wait())
    379           << "Timed out waiting for packets to be NACKed, retransmitted and "
    380              "rendered.";
    381     }
    382 
    383     scoped_ptr<RtpHeaderParser> rtp_parser_;
    384     std::set<uint16_t> dropped_packets_;
    385     std::set<uint16_t> retransmitted_packets_;
    386     uint64_t sent_rtp_packets_;
    387     int packets_left_to_drop_;
    388     int nacks_left_;
    389   } test;
    390 
    391   RunBaseTest(&test);
    392 }
    393 
    394 // TODO(pbos): Flaky, webrtc:3269
    395 TEST_F(EndToEndTest, DISABLED_CanReceiveFec) {
    396   class FecRenderObserver : public test::EndToEndTest, public VideoRenderer {
    397    public:
    398     FecRenderObserver()
    399         : EndToEndTest(kDefaultTimeoutMs),
    400           state_(kFirstPacket),
    401           protected_sequence_number_(0),
    402           protected_frame_timestamp_(0) {}
    403 
    404    private:
    405     virtual Action OnSendRtp(const uint8_t* packet, size_t length) OVERRIDE
    406         EXCLUSIVE_LOCKS_REQUIRED(crit_) {
    407       RTPHeader header;
    408       EXPECT_TRUE(parser_->Parse(packet, length, &header));
    409 
    410       EXPECT_EQ(kRedPayloadType, header.payloadType);
    411       int encapsulated_payload_type =
    412           static_cast<int>(packet[header.headerLength]);
    413       if (encapsulated_payload_type != kFakeSendPayloadType)
    414         EXPECT_EQ(kUlpfecPayloadType, encapsulated_payload_type);
    415 
    416       switch (state_) {
    417         case kFirstPacket:
    418           state_ = kDropEveryOtherPacketUntilFec;
    419           break;
    420         case kDropEveryOtherPacketUntilFec:
    421           if (encapsulated_payload_type == kUlpfecPayloadType) {
    422             state_ = kDropNextMediaPacket;
    423             return SEND_PACKET;
    424           }
    425           if (header.sequenceNumber % 2 == 0)
    426             return DROP_PACKET;
    427           break;
    428         case kDropNextMediaPacket:
    429           if (encapsulated_payload_type == kFakeSendPayloadType) {
    430             protected_sequence_number_ = header.sequenceNumber;
    431             protected_frame_timestamp_ = header.timestamp;
    432             state_ = kProtectedPacketDropped;
    433             return DROP_PACKET;
    434           }
    435           break;
    436         case kProtectedPacketDropped:
    437           EXPECT_NE(header.sequenceNumber, protected_sequence_number_)
    438               << "Protected packet retransmitted. Should not happen with FEC.";
    439           break;
    440       }
    441 
    442       return SEND_PACKET;
    443     }
    444 
    445     virtual void RenderFrame(const I420VideoFrame& video_frame,
    446                              int time_to_render_ms) OVERRIDE {
    447       CriticalSectionScoped lock(crit_.get());
    448       // Rendering frame with timestamp associated with dropped packet -> FEC
    449       // protection worked.
    450       if (state_ == kProtectedPacketDropped &&
    451           video_frame.timestamp() == protected_frame_timestamp_) {
    452         observation_complete_->Set();
    453       }
    454     }
    455 
    456     enum {
    457       kFirstPacket,
    458       kDropEveryOtherPacketUntilFec,
    459       kDropNextMediaPacket,
    460       kProtectedPacketDropped,
    461     } state_;
    462 
    463     virtual void ModifyConfigs(
    464         VideoSendStream::Config* send_config,
    465         std::vector<VideoReceiveStream::Config>* receive_configs,
    466         VideoEncoderConfig* encoder_config) OVERRIDE {
    467       // TODO(pbos): Run this test with combined NACK/FEC enabled as well.
    468       // int rtp_history_ms = 1000;
    469       // (*receive_configs)[0].rtp.nack.rtp_history_ms = rtp_history_ms;
    470       // send_config->rtp.nack.rtp_history_ms = rtp_history_ms;
    471       send_config->rtp.fec.red_payload_type = kRedPayloadType;
    472       send_config->rtp.fec.ulpfec_payload_type = kUlpfecPayloadType;
    473 
    474       (*receive_configs)[0].rtp.fec.red_payload_type = kRedPayloadType;
    475       (*receive_configs)[0].rtp.fec.ulpfec_payload_type = kUlpfecPayloadType;
    476       (*receive_configs)[0].renderer = this;
    477     }
    478 
    479     virtual void PerformTest() OVERRIDE {
    480       EXPECT_EQ(kEventSignaled, Wait())
    481           << "Timed out while waiting for retransmitted NACKed frames to be "
    482              "rendered again.";
    483     }
    484 
    485     uint32_t protected_sequence_number_ GUARDED_BY(crit_);
    486     uint32_t protected_frame_timestamp_ GUARDED_BY(crit_);
    487   } test;
    488 
    489   RunBaseTest(&test);
    490 }
    491 
    492 // This test drops second RTP packet with a marker bit set, makes sure it's
    493 // retransmitted and renders. Retransmission SSRCs are also checked.
    494 void EndToEndTest::DecodesRetransmittedFrame(bool retransmit_over_rtx) {
    495   static const int kDroppedFrameNumber = 2;
    496   class RetransmissionObserver : public test::EndToEndTest,
    497                                  public I420FrameCallback {
    498    public:
    499     explicit RetransmissionObserver(bool expect_rtx)
    500         : EndToEndTest(kDefaultTimeoutMs),
    501           retransmission_ssrc_(expect_rtx ? kSendRtxSsrcs[0] : kSendSsrcs[0]),
    502           retransmission_payload_type_(expect_rtx ? kSendRtxPayloadType
    503                                                   : kFakeSendPayloadType),
    504           marker_bits_observed_(0),
    505           retransmitted_timestamp_(0),
    506           frame_retransmitted_(false) {}
    507 
    508    private:
    509     virtual Action OnSendRtp(const uint8_t* packet, size_t length) OVERRIDE {
    510       RTPHeader header;
    511       EXPECT_TRUE(parser_->Parse(packet, length, &header));
    512 
    513       if (header.timestamp == retransmitted_timestamp_) {
    514         EXPECT_EQ(retransmission_ssrc_, header.ssrc);
    515         EXPECT_EQ(retransmission_payload_type_, header.payloadType);
    516         frame_retransmitted_ = true;
    517         return SEND_PACKET;
    518       }
    519 
    520       EXPECT_EQ(kSendSsrcs[0], header.ssrc);
    521       EXPECT_EQ(kFakeSendPayloadType, header.payloadType);
    522 
    523       // Found the second frame's final packet, drop this and expect a
    524       // retransmission.
    525       if (header.markerBit && ++marker_bits_observed_ == kDroppedFrameNumber) {
    526         retransmitted_timestamp_ = header.timestamp;
    527         return DROP_PACKET;
    528       }
    529 
    530       return SEND_PACKET;
    531     }
    532 
    533     virtual void FrameCallback(I420VideoFrame* frame) OVERRIDE {
    534       CriticalSectionScoped lock(crit_.get());
    535       if (frame->timestamp() == retransmitted_timestamp_) {
    536         EXPECT_TRUE(frame_retransmitted_);
    537         observation_complete_->Set();
    538       }
    539     }
    540 
    541     virtual void ModifyConfigs(
    542         VideoSendStream::Config* send_config,
    543         std::vector<VideoReceiveStream::Config>* receive_configs,
    544         VideoEncoderConfig* encoder_config) OVERRIDE {
    545       send_config->rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
    546       (*receive_configs)[0].pre_render_callback = this;
    547       (*receive_configs)[0].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
    548       if (retransmission_ssrc_ == kSendRtxSsrcs[0]) {
    549         send_config->rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[0]);
    550         send_config->rtp.rtx.payload_type = kSendRtxPayloadType;
    551         (*receive_configs)[0].rtp.rtx[kSendRtxPayloadType].ssrc =
    552             kSendRtxSsrcs[0];
    553         (*receive_configs)[0].rtp.rtx[kSendRtxPayloadType].payload_type =
    554             kSendRtxPayloadType;
    555       }
    556     }
    557 
    558     virtual void PerformTest() OVERRIDE {
    559       EXPECT_EQ(kEventSignaled, Wait())
    560           << "Timed out while waiting for retransmission to render.";
    561     }
    562 
    563     const uint32_t retransmission_ssrc_;
    564     const int retransmission_payload_type_;
    565     int marker_bits_observed_;
    566     uint32_t retransmitted_timestamp_;
    567     bool frame_retransmitted_;
    568   } test(retransmit_over_rtx);
    569 
    570   RunBaseTest(&test);
    571 }
    572 
    573 TEST_F(EndToEndTest, DecodesRetransmittedFrame) {
    574   DecodesRetransmittedFrame(false);
    575 }
    576 
    577 TEST_F(EndToEndTest, DecodesRetransmittedFrameOverRtx) {
    578   DecodesRetransmittedFrame(true);
    579 }
    580 
    581 TEST_F(EndToEndTest, UsesFrameCallbacks) {
    582   static const int kWidth = 320;
    583   static const int kHeight = 240;
    584 
    585   class Renderer : public VideoRenderer {
    586    public:
    587     Renderer() : event_(EventWrapper::Create()) {}
    588 
    589     virtual void RenderFrame(const I420VideoFrame& video_frame,
    590                              int /*time_to_render_ms*/) OVERRIDE {
    591       EXPECT_EQ(0, *video_frame.buffer(kYPlane))
    592           << "Rendered frame should have zero luma which is applied by the "
    593              "pre-render callback.";
    594       event_->Set();
    595     }
    596 
    597     EventTypeWrapper Wait() { return event_->Wait(kDefaultTimeoutMs); }
    598     scoped_ptr<EventWrapper> event_;
    599   } renderer;
    600 
    601   class TestFrameCallback : public I420FrameCallback {
    602    public:
    603     TestFrameCallback(int expected_luma_byte, int next_luma_byte)
    604         : event_(EventWrapper::Create()),
    605           expected_luma_byte_(expected_luma_byte),
    606           next_luma_byte_(next_luma_byte) {}
    607 
    608     EventTypeWrapper Wait() { return event_->Wait(kDefaultTimeoutMs); }
    609 
    610    private:
    611     virtual void FrameCallback(I420VideoFrame* frame) {
    612       EXPECT_EQ(kWidth, frame->width())
    613           << "Width not as expected, callback done before resize?";
    614       EXPECT_EQ(kHeight, frame->height())
    615           << "Height not as expected, callback done before resize?";
    616 
    617       // Previous luma specified, observed luma should be fairly close.
    618       if (expected_luma_byte_ != -1) {
    619         EXPECT_NEAR(expected_luma_byte_, *frame->buffer(kYPlane), 10);
    620       }
    621 
    622       memset(frame->buffer(kYPlane),
    623              next_luma_byte_,
    624              frame->allocated_size(kYPlane));
    625 
    626       event_->Set();
    627     }
    628 
    629     scoped_ptr<EventWrapper> event_;
    630     int expected_luma_byte_;
    631     int next_luma_byte_;
    632   };
    633 
    634   TestFrameCallback pre_encode_callback(-1, 255);  // Changes luma to 255.
    635   TestFrameCallback pre_render_callback(255, 0);  // Changes luma from 255 to 0.
    636 
    637   test::DirectTransport sender_transport, receiver_transport;
    638 
    639   CreateCalls(Call::Config(&sender_transport),
    640               Call::Config(&receiver_transport));
    641 
    642   sender_transport.SetReceiver(receiver_call_->Receiver());
    643   receiver_transport.SetReceiver(sender_call_->Receiver());
    644 
    645   CreateSendConfig(1);
    646   scoped_ptr<VideoEncoder> encoder(
    647       VideoEncoder::Create(VideoEncoder::kVp8));
    648   send_config_.encoder_settings.encoder = encoder.get();
    649   send_config_.encoder_settings.payload_name = "VP8";
    650   ASSERT_EQ(1u, encoder_config_.streams.size()) << "Test setup error.";
    651   encoder_config_.streams[0].width = kWidth;
    652   encoder_config_.streams[0].height = kHeight;
    653   send_config_.pre_encode_callback = &pre_encode_callback;
    654 
    655   CreateMatchingReceiveConfigs();
    656   receive_configs_[0].pre_render_callback = &pre_render_callback;
    657   receive_configs_[0].renderer = &renderer;
    658 
    659   CreateStreams();
    660   Start();
    661 
    662   // Create frames that are smaller than the send width/height, this is done to
    663   // check that the callbacks are done after processing video.
    664   scoped_ptr<test::FrameGenerator> frame_generator(
    665       test::FrameGenerator::Create(kWidth / 2, kHeight / 2));
    666   send_stream_->Input()->SwapFrame(frame_generator->NextFrame());
    667 
    668   EXPECT_EQ(kEventSignaled, pre_encode_callback.Wait())
    669       << "Timed out while waiting for pre-encode callback.";
    670   EXPECT_EQ(kEventSignaled, pre_render_callback.Wait())
    671       << "Timed out while waiting for pre-render callback.";
    672   EXPECT_EQ(kEventSignaled, renderer.Wait())
    673       << "Timed out while waiting for the frame to render.";
    674 
    675   Stop();
    676 
    677   sender_transport.StopSending();
    678   receiver_transport.StopSending();
    679 
    680   DestroyStreams();
    681 }
    682 
    683 void EndToEndTest::ReceivesPliAndRecovers(int rtp_history_ms) {
    684   static const int kPacketsToDrop = 1;
    685 
    686   class PliObserver : public test::EndToEndTest, public VideoRenderer {
    687    public:
    688     explicit PliObserver(int rtp_history_ms)
    689         : EndToEndTest(kLongTimeoutMs),
    690           rtp_history_ms_(rtp_history_ms),
    691           nack_enabled_(rtp_history_ms > 0),
    692           highest_dropped_timestamp_(0),
    693           frames_to_drop_(0),
    694           received_pli_(false) {}
    695 
    696    private:
    697     virtual Action OnSendRtp(const uint8_t* packet, size_t length) OVERRIDE {
    698       RTPHeader header;
    699       EXPECT_TRUE(parser_->Parse(packet, length, &header));
    700 
    701       // Drop all retransmitted packets to force a PLI.
    702       if (header.timestamp <= highest_dropped_timestamp_)
    703         return DROP_PACKET;
    704 
    705       if (frames_to_drop_ > 0) {
    706         highest_dropped_timestamp_ = header.timestamp;
    707         --frames_to_drop_;
    708         return DROP_PACKET;
    709       }
    710 
    711       return SEND_PACKET;
    712     }
    713 
    714     virtual Action OnReceiveRtcp(const uint8_t* packet,
    715                                  size_t length) OVERRIDE {
    716       RTCPUtility::RTCPParserV2 parser(packet, length, true);
    717       EXPECT_TRUE(parser.IsValid());
    718 
    719       for (RTCPUtility::RTCPPacketTypes packet_type = parser.Begin();
    720            packet_type != RTCPUtility::kRtcpNotValidCode;
    721            packet_type = parser.Iterate()) {
    722         if (!nack_enabled_)
    723           EXPECT_NE(packet_type, RTCPUtility::kRtcpRtpfbNackCode);
    724 
    725         if (packet_type == RTCPUtility::kRtcpPsfbPliCode) {
    726           received_pli_ = true;
    727           break;
    728         }
    729       }
    730       return SEND_PACKET;
    731     }
    732 
    733     virtual void RenderFrame(const I420VideoFrame& video_frame,
    734                              int time_to_render_ms) OVERRIDE {
    735       CriticalSectionScoped lock(crit_.get());
    736       if (received_pli_ &&
    737           video_frame.timestamp() > highest_dropped_timestamp_) {
    738         observation_complete_->Set();
    739       }
    740       if (!received_pli_)
    741         frames_to_drop_ = kPacketsToDrop;
    742     }
    743 
    744     virtual void ModifyConfigs(
    745         VideoSendStream::Config* send_config,
    746         std::vector<VideoReceiveStream::Config>* receive_configs,
    747         VideoEncoderConfig* encoder_config) OVERRIDE {
    748       send_config->rtp.nack.rtp_history_ms = rtp_history_ms_;
    749       (*receive_configs)[0].rtp.nack.rtp_history_ms = rtp_history_ms_;
    750       (*receive_configs)[0].renderer = this;
    751     }
    752 
    753     virtual void PerformTest() OVERRIDE {
    754       EXPECT_EQ(kEventSignaled, Wait()) << "Timed out waiting for PLI to be "
    755                                            "received and a frame to be "
    756                                            "rendered afterwards.";
    757     }
    758 
    759     int rtp_history_ms_;
    760     bool nack_enabled_;
    761     uint32_t highest_dropped_timestamp_;
    762     int frames_to_drop_;
    763     bool received_pli_;
    764   } test(rtp_history_ms);
    765 
    766   RunBaseTest(&test);
    767 }
    768 
    769 TEST_F(EndToEndTest, ReceivesPliAndRecoversWithNack) {
    770   ReceivesPliAndRecovers(1000);
    771 }
    772 
    773 // TODO(pbos): Enable this when 2250 is resolved.
    774 TEST_F(EndToEndTest, DISABLED_ReceivesPliAndRecoversWithoutNack) {
    775   ReceivesPliAndRecovers(0);
    776 }
    777 
    778 TEST_F(EndToEndTest, UnknownRtpPacketGivesUnknownSsrcReturnCode) {
    779   class PacketInputObserver : public PacketReceiver {
    780    public:
    781     explicit PacketInputObserver(PacketReceiver* receiver)
    782         : receiver_(receiver), delivered_packet_(EventWrapper::Create()) {}
    783 
    784     EventTypeWrapper Wait() {
    785       return delivered_packet_->Wait(kDefaultTimeoutMs);
    786     }
    787 
    788    private:
    789     virtual DeliveryStatus DeliverPacket(const uint8_t* packet,
    790                                          size_t length) OVERRIDE {
    791       if (RtpHeaderParser::IsRtcp(packet, length)) {
    792         return receiver_->DeliverPacket(packet, length);
    793       } else {
    794         DeliveryStatus delivery_status =
    795             receiver_->DeliverPacket(packet, length);
    796         EXPECT_EQ(DELIVERY_UNKNOWN_SSRC, delivery_status);
    797         delivered_packet_->Set();
    798         return delivery_status;
    799       }
    800     }
    801 
    802     PacketReceiver* receiver_;
    803     scoped_ptr<EventWrapper> delivered_packet_;
    804   };
    805 
    806   test::DirectTransport send_transport, receive_transport;
    807 
    808   CreateCalls(Call::Config(&send_transport), Call::Config(&receive_transport));
    809   PacketInputObserver input_observer(receiver_call_->Receiver());
    810 
    811   send_transport.SetReceiver(&input_observer);
    812   receive_transport.SetReceiver(sender_call_->Receiver());
    813 
    814   CreateSendConfig(1);
    815   CreateMatchingReceiveConfigs();
    816 
    817   CreateStreams();
    818   CreateFrameGeneratorCapturer();
    819   Start();
    820 
    821   receiver_call_->DestroyVideoReceiveStream(receive_streams_[0]);
    822   receive_streams_.clear();
    823 
    824   // Wait() waits for a received packet.
    825   EXPECT_EQ(kEventSignaled, input_observer.Wait());
    826 
    827   Stop();
    828 
    829   DestroyStreams();
    830 
    831   send_transport.StopSending();
    832   receive_transport.StopSending();
    833 }
    834 
    835 void EndToEndTest::RespectsRtcpMode(newapi::RtcpMode rtcp_mode) {
    836   static const int kNumCompoundRtcpPacketsToObserve = 10;
    837   class RtcpModeObserver : public test::EndToEndTest {
    838    public:
    839     explicit RtcpModeObserver(newapi::RtcpMode rtcp_mode)
    840         : EndToEndTest(kDefaultTimeoutMs),
    841           rtcp_mode_(rtcp_mode),
    842           sent_rtp_(0),
    843           sent_rtcp_(0) {}
    844 
    845    private:
    846     virtual Action OnSendRtp(const uint8_t* packet, size_t length) OVERRIDE {
    847       if (++sent_rtp_ % 3 == 0)
    848         return DROP_PACKET;
    849 
    850       return SEND_PACKET;
    851     }
    852 
    853     virtual Action OnReceiveRtcp(const uint8_t* packet,
    854                                  size_t length) OVERRIDE {
    855       ++sent_rtcp_;
    856       RTCPUtility::RTCPParserV2 parser(packet, length, true);
    857       EXPECT_TRUE(parser.IsValid());
    858 
    859       RTCPUtility::RTCPPacketTypes packet_type = parser.Begin();
    860       bool has_report_block = false;
    861       while (packet_type != RTCPUtility::kRtcpNotValidCode) {
    862         EXPECT_NE(RTCPUtility::kRtcpSrCode, packet_type);
    863         if (packet_type == RTCPUtility::kRtcpRrCode) {
    864           has_report_block = true;
    865           break;
    866         }
    867         packet_type = parser.Iterate();
    868       }
    869 
    870       switch (rtcp_mode_) {
    871         case newapi::kRtcpCompound:
    872           if (!has_report_block) {
    873             ADD_FAILURE() << "Received RTCP packet without receiver report for "
    874                              "kRtcpCompound.";
    875             observation_complete_->Set();
    876           }
    877 
    878           if (sent_rtcp_ >= kNumCompoundRtcpPacketsToObserve)
    879             observation_complete_->Set();
    880 
    881           break;
    882         case newapi::kRtcpReducedSize:
    883           if (!has_report_block)
    884             observation_complete_->Set();
    885           break;
    886       }
    887 
    888       return SEND_PACKET;
    889     }
    890 
    891     virtual void ModifyConfigs(
    892         VideoSendStream::Config* send_config,
    893         std::vector<VideoReceiveStream::Config>* receive_configs,
    894         VideoEncoderConfig* encoder_config) OVERRIDE {
    895       send_config->rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
    896       (*receive_configs)[0].rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
    897       (*receive_configs)[0].rtp.rtcp_mode = rtcp_mode_;
    898     }
    899 
    900     virtual void PerformTest() OVERRIDE {
    901       EXPECT_EQ(kEventSignaled, Wait())
    902           << (rtcp_mode_ == newapi::kRtcpCompound
    903                   ? "Timed out before observing enough compound packets."
    904                   : "Timed out before receiving a non-compound RTCP packet.");
    905     }
    906 
    907     newapi::RtcpMode rtcp_mode_;
    908     int sent_rtp_;
    909     int sent_rtcp_;
    910   } test(rtcp_mode);
    911 
    912   RunBaseTest(&test);
    913 }
    914 
    915 TEST_F(EndToEndTest, UsesRtcpCompoundMode) {
    916   RespectsRtcpMode(newapi::kRtcpCompound);
    917 }
    918 
    919 TEST_F(EndToEndTest, UsesRtcpReducedSizeMode) {
    920   RespectsRtcpMode(newapi::kRtcpReducedSize);
    921 }
    922 
    923 // Test sets up a Call multiple senders with different resolutions and SSRCs.
    924 // Another is set up to receive all three of these with different renderers.
    925 // Each renderer verifies that it receives the expected resolution, and as soon
    926 // as every renderer has received a frame, the test finishes.
    927 TEST_F(EndToEndTest, SendsAndReceivesMultipleStreams) {
    928   static const size_t kNumStreams = 3;
    929 
    930   class VideoOutputObserver : public VideoRenderer {
    931    public:
    932     VideoOutputObserver(test::FrameGeneratorCapturer** capturer,
    933                         int width,
    934                         int height)
    935         : capturer_(capturer),
    936           width_(width),
    937           height_(height),
    938           done_(EventWrapper::Create()) {}
    939 
    940     virtual void RenderFrame(const I420VideoFrame& video_frame,
    941                              int time_to_render_ms) OVERRIDE {
    942       EXPECT_EQ(width_, video_frame.width());
    943       EXPECT_EQ(height_, video_frame.height());
    944       (*capturer_)->Stop();
    945       done_->Set();
    946     }
    947 
    948     EventTypeWrapper Wait() { return done_->Wait(kDefaultTimeoutMs); }
    949 
    950    private:
    951     test::FrameGeneratorCapturer** capturer_;
    952     int width_;
    953     int height_;
    954     scoped_ptr<EventWrapper> done_;
    955   };
    956 
    957   struct {
    958     uint32_t ssrc;
    959     int width;
    960     int height;
    961   } codec_settings[kNumStreams] = {{1, 640, 480}, {2, 320, 240}, {3, 240, 160}};
    962 
    963   test::DirectTransport sender_transport, receiver_transport;
    964   scoped_ptr<Call> sender_call(Call::Create(Call::Config(&sender_transport)));
    965   scoped_ptr<Call> receiver_call(
    966       Call::Create(Call::Config(&receiver_transport)));
    967   sender_transport.SetReceiver(receiver_call->Receiver());
    968   receiver_transport.SetReceiver(sender_call->Receiver());
    969 
    970   VideoSendStream* send_streams[kNumStreams];
    971   VideoReceiveStream* receive_streams[kNumStreams];
    972 
    973   VideoOutputObserver* observers[kNumStreams];
    974   test::FrameGeneratorCapturer* frame_generators[kNumStreams];
    975 
    976   scoped_ptr<VideoEncoder> encoders[kNumStreams];
    977   for (size_t i = 0; i < kNumStreams; ++i)
    978     encoders[i].reset(VideoEncoder::Create(VideoEncoder::kVp8));
    979 
    980   for (size_t i = 0; i < kNumStreams; ++i) {
    981     uint32_t ssrc = codec_settings[i].ssrc;
    982     int width = codec_settings[i].width;
    983     int height = codec_settings[i].height;
    984     observers[i] = new VideoOutputObserver(&frame_generators[i], width, height);
    985 
    986     VideoSendStream::Config send_config;
    987     send_config.rtp.ssrcs.push_back(ssrc);
    988     send_config.encoder_settings.encoder = encoders[i].get();
    989     send_config.encoder_settings.payload_name = "VP8";
    990     send_config.encoder_settings.payload_type = 124;
    991     VideoEncoderConfig encoder_config;
    992     encoder_config.streams = test::CreateVideoStreams(1);
    993     VideoStream* stream = &encoder_config.streams[0];
    994     stream->width = width;
    995     stream->height = height;
    996     stream->max_framerate = 5;
    997     stream->min_bitrate_bps = stream->target_bitrate_bps =
    998         stream->max_bitrate_bps = 100000;
    999     send_streams[i] =
   1000         sender_call->CreateVideoSendStream(send_config, encoder_config);
   1001     send_streams[i]->Start();
   1002 
   1003     VideoReceiveStream::Config receive_config;
   1004     receive_config.renderer = observers[i];
   1005     receive_config.rtp.remote_ssrc = ssrc;
   1006     receive_config.rtp.local_ssrc = kReceiverLocalSsrc;
   1007     VideoCodec codec =
   1008         test::CreateDecoderVideoCodec(send_config.encoder_settings);
   1009     receive_config.codecs.push_back(codec);
   1010     receive_streams[i] =
   1011         receiver_call->CreateVideoReceiveStream(receive_config);
   1012     receive_streams[i]->Start();
   1013 
   1014     frame_generators[i] = test::FrameGeneratorCapturer::Create(
   1015         send_streams[i]->Input(), width, height, 30, Clock::GetRealTimeClock());
   1016     frame_generators[i]->Start();
   1017   }
   1018 
   1019   for (size_t i = 0; i < kNumStreams; ++i) {
   1020     EXPECT_EQ(kEventSignaled, observers[i]->Wait())
   1021         << "Timed out while waiting for observer " << i << " to render.";
   1022   }
   1023 
   1024   for (size_t i = 0; i < kNumStreams; ++i) {
   1025     frame_generators[i]->Stop();
   1026     sender_call->DestroyVideoSendStream(send_streams[i]);
   1027     receiver_call->DestroyVideoReceiveStream(receive_streams[i]);
   1028     delete frame_generators[i];
   1029     delete observers[i];
   1030   }
   1031 
   1032   sender_transport.StopSending();
   1033   receiver_transport.StopSending();
   1034 }
   1035 
   1036 TEST_F(EndToEndTest, ObserversEncodedFrames) {
   1037   class EncodedFrameTestObserver : public EncodedFrameObserver {
   1038    public:
   1039     EncodedFrameTestObserver()
   1040         : length_(0),
   1041           frame_type_(kFrameEmpty),
   1042           called_(EventWrapper::Create()) {}
   1043     virtual ~EncodedFrameTestObserver() {}
   1044 
   1045     virtual void EncodedFrameCallback(const EncodedFrame& encoded_frame) {
   1046       frame_type_ = encoded_frame.frame_type_;
   1047       length_ = encoded_frame.length_;
   1048       buffer_.reset(new uint8_t[length_]);
   1049       memcpy(buffer_.get(), encoded_frame.data_, length_);
   1050       called_->Set();
   1051     }
   1052 
   1053     EventTypeWrapper Wait() { return called_->Wait(kDefaultTimeoutMs); }
   1054 
   1055     void ExpectEqualFrames(const EncodedFrameTestObserver& observer) {
   1056       ASSERT_EQ(length_, observer.length_)
   1057           << "Observed frames are of different lengths.";
   1058       EXPECT_EQ(frame_type_, observer.frame_type_)
   1059           << "Observed frames have different frame types.";
   1060       EXPECT_EQ(0, memcmp(buffer_.get(), observer.buffer_.get(), length_))
   1061           << "Observed encoded frames have different content.";
   1062     }
   1063 
   1064    private:
   1065     scoped_ptr<uint8_t[]> buffer_;
   1066     size_t length_;
   1067     FrameType frame_type_;
   1068     scoped_ptr<EventWrapper> called_;
   1069   };
   1070 
   1071   EncodedFrameTestObserver post_encode_observer;
   1072   EncodedFrameTestObserver pre_decode_observer;
   1073 
   1074   test::DirectTransport sender_transport, receiver_transport;
   1075 
   1076   CreateCalls(Call::Config(&sender_transport),
   1077               Call::Config(&receiver_transport));
   1078 
   1079   sender_transport.SetReceiver(receiver_call_->Receiver());
   1080   receiver_transport.SetReceiver(sender_call_->Receiver());
   1081 
   1082   CreateSendConfig(1);
   1083   CreateMatchingReceiveConfigs();
   1084   send_config_.post_encode_callback = &post_encode_observer;
   1085   receive_configs_[0].pre_decode_callback = &pre_decode_observer;
   1086 
   1087   CreateStreams();
   1088   Start();
   1089 
   1090   scoped_ptr<test::FrameGenerator> frame_generator(test::FrameGenerator::Create(
   1091       encoder_config_.streams[0].width, encoder_config_.streams[0].height));
   1092   send_stream_->Input()->SwapFrame(frame_generator->NextFrame());
   1093 
   1094   EXPECT_EQ(kEventSignaled, post_encode_observer.Wait())
   1095       << "Timed out while waiting for send-side encoded-frame callback.";
   1096 
   1097   EXPECT_EQ(kEventSignaled, pre_decode_observer.Wait())
   1098       << "Timed out while waiting for pre-decode encoded-frame callback.";
   1099 
   1100   post_encode_observer.ExpectEqualFrames(pre_decode_observer);
   1101 
   1102   Stop();
   1103 
   1104   sender_transport.StopSending();
   1105   receiver_transport.StopSending();
   1106 
   1107   DestroyStreams();
   1108 }
   1109 
   1110 TEST_F(EndToEndTest, ReceiveStreamSendsRemb) {
   1111   class RembObserver : public test::EndToEndTest {
   1112    public:
   1113     RembObserver() : EndToEndTest(kDefaultTimeoutMs) {}
   1114 
   1115     virtual Action OnReceiveRtcp(const uint8_t* packet,
   1116                                  size_t length) OVERRIDE {
   1117       RTCPUtility::RTCPParserV2 parser(packet, length, true);
   1118       EXPECT_TRUE(parser.IsValid());
   1119 
   1120       bool received_psfb = false;
   1121       bool received_remb = false;
   1122       RTCPUtility::RTCPPacketTypes packet_type = parser.Begin();
   1123       while (packet_type != RTCPUtility::kRtcpNotValidCode) {
   1124         if (packet_type == RTCPUtility::kRtcpPsfbRembCode) {
   1125           const RTCPUtility::RTCPPacket& packet = parser.Packet();
   1126           EXPECT_EQ(packet.PSFBAPP.SenderSSRC, kReceiverLocalSsrc);
   1127           received_psfb = true;
   1128         } else if (packet_type == RTCPUtility::kRtcpPsfbRembItemCode) {
   1129           const RTCPUtility::RTCPPacket& packet = parser.Packet();
   1130           EXPECT_GT(packet.REMBItem.BitRate, 0u);
   1131           EXPECT_EQ(packet.REMBItem.NumberOfSSRCs, 1u);
   1132           EXPECT_EQ(packet.REMBItem.SSRCs[0], kSendSsrcs[0]);
   1133           received_remb = true;
   1134         }
   1135         packet_type = parser.Iterate();
   1136       }
   1137       if (received_psfb && received_remb)
   1138         observation_complete_->Set();
   1139       return SEND_PACKET;
   1140     }
   1141     virtual void PerformTest() OVERRIDE {
   1142       EXPECT_EQ(kEventSignaled, Wait()) << "Timed out while waiting for a "
   1143                                            "receiver RTCP REMB packet to be "
   1144                                            "sent.";
   1145     }
   1146   } test;
   1147 
   1148   RunBaseTest(&test);
   1149 }
   1150 
   1151 void EndToEndTest::TestXrReceiverReferenceTimeReport(bool enable_rrtr) {
   1152   static const int kNumRtcpReportPacketsToObserve = 5;
   1153   class RtcpXrObserver : public test::EndToEndTest {
   1154    public:
   1155     explicit RtcpXrObserver(bool enable_rrtr)
   1156         : EndToEndTest(kDefaultTimeoutMs),
   1157           enable_rrtr_(enable_rrtr),
   1158           sent_rtcp_sr_(0),
   1159           sent_rtcp_rr_(0),
   1160           sent_rtcp_rrtr_(0),
   1161           sent_rtcp_dlrr_(0) {}
   1162 
   1163    private:
   1164     // Receive stream should send RR packets (and RRTR packets if enabled).
   1165     virtual Action OnReceiveRtcp(const uint8_t* packet,
   1166                                  size_t length) OVERRIDE {
   1167       RTCPUtility::RTCPParserV2 parser(packet, length, true);
   1168       EXPECT_TRUE(parser.IsValid());
   1169 
   1170       RTCPUtility::RTCPPacketTypes packet_type = parser.Begin();
   1171       while (packet_type != RTCPUtility::kRtcpNotValidCode) {
   1172         if (packet_type == RTCPUtility::kRtcpRrCode) {
   1173           ++sent_rtcp_rr_;
   1174         } else if (packet_type ==
   1175                    RTCPUtility::kRtcpXrReceiverReferenceTimeCode) {
   1176           ++sent_rtcp_rrtr_;
   1177         }
   1178         EXPECT_NE(packet_type, RTCPUtility::kRtcpSrCode);
   1179         EXPECT_NE(packet_type, RTCPUtility::kRtcpXrDlrrReportBlockItemCode);
   1180         packet_type = parser.Iterate();
   1181       }
   1182       return SEND_PACKET;
   1183     }
   1184     // Send stream should send SR packets (and DLRR packets if enabled).
   1185     virtual Action OnSendRtcp(const uint8_t* packet, size_t length) {
   1186       RTCPUtility::RTCPParserV2 parser(packet, length, true);
   1187       EXPECT_TRUE(parser.IsValid());
   1188 
   1189       RTCPUtility::RTCPPacketTypes packet_type = parser.Begin();
   1190       while (packet_type != RTCPUtility::kRtcpNotValidCode) {
   1191         if (packet_type == RTCPUtility::kRtcpSrCode) {
   1192           ++sent_rtcp_sr_;
   1193         } else if (packet_type == RTCPUtility::kRtcpXrDlrrReportBlockItemCode) {
   1194           ++sent_rtcp_dlrr_;
   1195         }
   1196         EXPECT_NE(packet_type, RTCPUtility::kRtcpXrReceiverReferenceTimeCode);
   1197         packet_type = parser.Iterate();
   1198       }
   1199       if (sent_rtcp_sr_ > kNumRtcpReportPacketsToObserve &&
   1200           sent_rtcp_rr_ > kNumRtcpReportPacketsToObserve) {
   1201         if (enable_rrtr_) {
   1202           EXPECT_GT(sent_rtcp_rrtr_, 0);
   1203           EXPECT_GT(sent_rtcp_dlrr_, 0);
   1204         } else {
   1205           EXPECT_EQ(0, sent_rtcp_rrtr_);
   1206           EXPECT_EQ(0, sent_rtcp_dlrr_);
   1207         }
   1208         observation_complete_->Set();
   1209       }
   1210       return SEND_PACKET;
   1211     }
   1212 
   1213     virtual void ModifyConfigs(
   1214         VideoSendStream::Config* send_config,
   1215         std::vector<VideoReceiveStream::Config>* receive_configs,
   1216         VideoEncoderConfig* encoder_config) OVERRIDE {
   1217       (*receive_configs)[0].rtp.rtcp_mode = newapi::kRtcpReducedSize;
   1218       (*receive_configs)[0].rtp.rtcp_xr.receiver_reference_time_report =
   1219           enable_rrtr_;
   1220     }
   1221 
   1222     virtual void PerformTest() OVERRIDE {
   1223       EXPECT_EQ(kEventSignaled, Wait())
   1224           << "Timed out while waiting for RTCP SR/RR packets to be sent.";
   1225     }
   1226 
   1227     bool enable_rrtr_;
   1228     int sent_rtcp_sr_;
   1229     int sent_rtcp_rr_;
   1230     int sent_rtcp_rrtr_;
   1231     int sent_rtcp_dlrr_;
   1232   } test(enable_rrtr);
   1233 
   1234   RunBaseTest(&test);
   1235 }
   1236 
   1237 void EndToEndTest::TestSendsSetSsrcs(size_t num_ssrcs,
   1238                                      bool send_single_ssrc_first) {
   1239   class SendsSetSsrcs : public test::EndToEndTest {
   1240    public:
   1241     SendsSetSsrcs(const uint32_t* ssrcs,
   1242                   size_t num_ssrcs,
   1243                   bool send_single_ssrc_first)
   1244         : EndToEndTest(kDefaultTimeoutMs),
   1245           num_ssrcs_(num_ssrcs),
   1246           send_single_ssrc_first_(send_single_ssrc_first),
   1247           ssrcs_to_observe_(num_ssrcs),
   1248           expect_single_ssrc_(send_single_ssrc_first) {
   1249       for (size_t i = 0; i < num_ssrcs; ++i)
   1250         valid_ssrcs_[ssrcs[i]] = true;
   1251     }
   1252 
   1253    private:
   1254     virtual Action OnSendRtp(const uint8_t* packet, size_t length) OVERRIDE {
   1255       RTPHeader header;
   1256       EXPECT_TRUE(parser_->Parse(packet, length, &header));
   1257 
   1258       EXPECT_TRUE(valid_ssrcs_[header.ssrc])
   1259           << "Received unknown SSRC: " << header.ssrc;
   1260 
   1261       if (!valid_ssrcs_[header.ssrc])
   1262         observation_complete_->Set();
   1263 
   1264       if (!is_observed_[header.ssrc]) {
   1265         is_observed_[header.ssrc] = true;
   1266         --ssrcs_to_observe_;
   1267         if (expect_single_ssrc_) {
   1268           expect_single_ssrc_ = false;
   1269           observation_complete_->Set();
   1270         }
   1271       }
   1272 
   1273       if (ssrcs_to_observe_ == 0)
   1274         observation_complete_->Set();
   1275 
   1276       return SEND_PACKET;
   1277     }
   1278 
   1279     virtual size_t GetNumStreams() const OVERRIDE { return num_ssrcs_; }
   1280 
   1281     virtual void ModifyConfigs(
   1282         VideoSendStream::Config* send_config,
   1283         std::vector<VideoReceiveStream::Config>* receive_configs,
   1284         VideoEncoderConfig* encoder_config) OVERRIDE {
   1285       if (num_ssrcs_ > 1) {
   1286         // Set low simulcast bitrates to not have to wait for bandwidth ramp-up.
   1287         for (size_t i = 0; i < encoder_config->streams.size(); ++i) {
   1288           encoder_config->streams[i].min_bitrate_bps = 10000;
   1289           encoder_config->streams[i].target_bitrate_bps = 15000;
   1290           encoder_config->streams[i].max_bitrate_bps = 20000;
   1291         }
   1292       }
   1293 
   1294       encoder_config_all_streams_ = *encoder_config;
   1295       if (send_single_ssrc_first_)
   1296         encoder_config->streams.resize(1);
   1297     }
   1298 
   1299     virtual void OnStreamsCreated(
   1300         VideoSendStream* send_stream,
   1301         const std::vector<VideoReceiveStream*>& receive_streams) OVERRIDE {
   1302       send_stream_ = send_stream;
   1303     }
   1304 
   1305     virtual void PerformTest() OVERRIDE {
   1306       EXPECT_EQ(kEventSignaled, Wait())
   1307           << "Timed out while waiting for "
   1308           << (send_single_ssrc_first_ ? "first SSRC." : "SSRCs.");
   1309 
   1310       if (send_single_ssrc_first_) {
   1311         // Set full simulcast and continue with the rest of the SSRCs.
   1312         send_stream_->ReconfigureVideoEncoder(encoder_config_all_streams_);
   1313         EXPECT_EQ(kEventSignaled, Wait())
   1314             << "Timed out while waiting on additional SSRCs.";
   1315       }
   1316     }
   1317 
   1318    private:
   1319     std::map<uint32_t, bool> valid_ssrcs_;
   1320     std::map<uint32_t, bool> is_observed_;
   1321 
   1322     const size_t num_ssrcs_;
   1323     const bool send_single_ssrc_first_;
   1324 
   1325     size_t ssrcs_to_observe_;
   1326     bool expect_single_ssrc_;
   1327 
   1328     VideoSendStream* send_stream_;
   1329     VideoEncoderConfig encoder_config_all_streams_;
   1330   } test(kSendSsrcs, num_ssrcs, send_single_ssrc_first);
   1331 
   1332   RunBaseTest(&test);
   1333 }
   1334 
   1335 TEST_F(EndToEndTest, GetStats) {
   1336   class StatsObserver : public test::EndToEndTest, public I420FrameCallback {
   1337    public:
   1338     StatsObserver()
   1339         : EndToEndTest(kLongTimeoutMs),
   1340           receive_stream_(NULL),
   1341           send_stream_(NULL),
   1342           expected_receive_ssrc_(),
   1343           expected_send_ssrcs_(),
   1344           check_stats_event_(EventWrapper::Create()) {}
   1345 
   1346    private:
   1347     virtual Action OnSendRtp(const uint8_t* packet, size_t length) OVERRIDE {
   1348       check_stats_event_->Set();
   1349       return SEND_PACKET;
   1350     }
   1351 
   1352     virtual Action OnSendRtcp(const uint8_t* packet, size_t length) OVERRIDE {
   1353       check_stats_event_->Set();
   1354       return SEND_PACKET;
   1355     }
   1356 
   1357     virtual Action OnReceiveRtp(const uint8_t* packet, size_t length) OVERRIDE {
   1358       check_stats_event_->Set();
   1359       return SEND_PACKET;
   1360     }
   1361 
   1362     virtual Action OnReceiveRtcp(const uint8_t* packet,
   1363                                  size_t length) OVERRIDE {
   1364       check_stats_event_->Set();
   1365       return SEND_PACKET;
   1366     }
   1367 
   1368     virtual void FrameCallback(I420VideoFrame* video_frame) OVERRIDE {
   1369       // Ensure that we have at least 5ms send side delay.
   1370       int64_t render_time = video_frame->render_time_ms();
   1371       if (render_time > 0)
   1372         video_frame->set_render_time_ms(render_time - 5);
   1373     }
   1374 
   1375     bool CheckReceiveStats() {
   1376       assert(receive_stream_ != NULL);
   1377       VideoReceiveStream::Stats stats = receive_stream_->GetStats();
   1378       EXPECT_EQ(expected_receive_ssrc_, stats.ssrc);
   1379 
   1380       // Make sure all fields have been populated.
   1381 
   1382       receive_stats_filled_["IncomingRate"] |=
   1383           stats.network_frame_rate != 0 || stats.bitrate_bps != 0;
   1384 
   1385       receive_stats_filled_["FrameCallback"] |= stats.decode_frame_rate != 0;
   1386 
   1387       receive_stats_filled_["FrameRendered"] |= stats.render_frame_rate != 0;
   1388 
   1389       receive_stats_filled_["StatisticsUpdated"] |=
   1390           stats.rtcp_stats.cumulative_lost != 0 ||
   1391           stats.rtcp_stats.extended_max_sequence_number != 0 ||
   1392           stats.rtcp_stats.fraction_lost != 0 || stats.rtcp_stats.jitter != 0;
   1393 
   1394       receive_stats_filled_["DataCountersUpdated"] |=
   1395           stats.rtp_stats.bytes != 0 || stats.rtp_stats.fec_packets != 0 ||
   1396           stats.rtp_stats.header_bytes != 0 || stats.rtp_stats.packets != 0 ||
   1397           stats.rtp_stats.padding_bytes != 0 ||
   1398           stats.rtp_stats.retransmitted_packets != 0;
   1399 
   1400       receive_stats_filled_["CodecStats"] |=
   1401           stats.avg_delay_ms != 0 || stats.discarded_packets != 0 ||
   1402           stats.key_frames != 0 || stats.delta_frames != 0;
   1403 
   1404       return AllStatsFilled(receive_stats_filled_);
   1405     }
   1406 
   1407     bool CheckSendStats() {
   1408       assert(send_stream_ != NULL);
   1409       VideoSendStream::Stats stats = send_stream_->GetStats();
   1410 
   1411       send_stats_filled_["NumStreams"] |=
   1412           stats.substreams.size() == expected_send_ssrcs_.size();
   1413 
   1414       for (std::map<uint32_t, StreamStats>::const_iterator it =
   1415                stats.substreams.begin();
   1416            it != stats.substreams.end();
   1417            ++it) {
   1418         EXPECT_TRUE(expected_send_ssrcs_.find(it->first) !=
   1419                     expected_send_ssrcs_.end());
   1420 
   1421         send_stats_filled_[CompoundKey("IncomingRate", it->first)] |=
   1422             stats.input_frame_rate != 0;
   1423 
   1424         const StreamStats& stream_stats = it->second;
   1425 
   1426         send_stats_filled_[CompoundKey("StatisticsUpdated", it->first)] |=
   1427             stream_stats.rtcp_stats.cumulative_lost != 0 ||
   1428             stream_stats.rtcp_stats.extended_max_sequence_number != 0 ||
   1429             stream_stats.rtcp_stats.fraction_lost != 0;
   1430 
   1431         send_stats_filled_[CompoundKey("DataCountersUpdated", it->first)] |=
   1432             stream_stats.rtp_stats.fec_packets != 0 ||
   1433             stream_stats.rtp_stats.padding_bytes != 0 ||
   1434             stream_stats.rtp_stats.retransmitted_packets != 0 ||
   1435             stream_stats.rtp_stats.packets != 0;
   1436 
   1437         send_stats_filled_[CompoundKey("BitrateStatisticsObserver",
   1438                                        it->first)] |=
   1439             stream_stats.bitrate_bps != 0;
   1440 
   1441         send_stats_filled_[CompoundKey("FrameCountObserver", it->first)] |=
   1442             stream_stats.delta_frames != 0 || stream_stats.key_frames != 0;
   1443 
   1444         send_stats_filled_[CompoundKey("OutgoingRate", it->first)] |=
   1445             stats.encode_frame_rate != 0;
   1446 
   1447         send_stats_filled_[CompoundKey("Delay", it->first)] |=
   1448             stream_stats.avg_delay_ms != 0 || stream_stats.max_delay_ms != 0;
   1449       }
   1450 
   1451       return AllStatsFilled(send_stats_filled_);
   1452     }
   1453 
   1454     std::string CompoundKey(const char* name, uint32_t ssrc) {
   1455       std::ostringstream oss;
   1456       oss << name << "_" << ssrc;
   1457       return oss.str();
   1458     }
   1459 
   1460     bool AllStatsFilled(const std::map<std::string, bool>& stats_map) {
   1461       for (std::map<std::string, bool>::const_iterator it = stats_map.begin();
   1462            it != stats_map.end();
   1463            ++it) {
   1464         if (!it->second)
   1465           return false;
   1466       }
   1467       return true;
   1468     }
   1469 
   1470     virtual void ModifyConfigs(
   1471         VideoSendStream::Config* send_config,
   1472         std::vector<VideoReceiveStream::Config>* receive_configs,
   1473         VideoEncoderConfig* encoder_config) OVERRIDE {
   1474       send_config->pre_encode_callback = this;  // Used to inject delay.
   1475       send_config->rtp.c_name = "SomeCName";
   1476 
   1477       expected_receive_ssrc_ = (*receive_configs)[0].rtp.local_ssrc;
   1478       const std::vector<uint32_t>& ssrcs = send_config->rtp.ssrcs;
   1479       for (size_t i = 0; i < ssrcs.size(); ++i)
   1480         expected_send_ssrcs_.insert(ssrcs[i]);
   1481 
   1482       expected_cname_ = send_config->rtp.c_name;
   1483     }
   1484 
   1485     virtual void OnStreamsCreated(
   1486         VideoSendStream* send_stream,
   1487         const std::vector<VideoReceiveStream*>& receive_streams) OVERRIDE {
   1488       send_stream_ = send_stream;
   1489       receive_stream_ = receive_streams[0];
   1490     }
   1491 
   1492     virtual void PerformTest() OVERRIDE {
   1493       Clock* clock = Clock::GetRealTimeClock();
   1494       int64_t now = clock->TimeInMilliseconds();
   1495       int64_t stop_time = now + test::CallTest::kLongTimeoutMs;
   1496       bool receive_ok = false;
   1497       bool send_ok = false;
   1498 
   1499       while (now < stop_time) {
   1500         if (!receive_ok)
   1501           receive_ok = CheckReceiveStats();
   1502         if (!send_ok)
   1503           send_ok = CheckSendStats();
   1504 
   1505         if (receive_ok && send_ok)
   1506           return;
   1507 
   1508         int64_t time_until_timout_ = stop_time - now;
   1509         if (time_until_timout_ > 0)
   1510           check_stats_event_->Wait(time_until_timout_);
   1511         now = clock->TimeInMilliseconds();
   1512       }
   1513 
   1514       ADD_FAILURE() << "Timed out waiting for filled stats.";
   1515       for (std::map<std::string, bool>::const_iterator it =
   1516                receive_stats_filled_.begin();
   1517            it != receive_stats_filled_.end();
   1518            ++it) {
   1519         if (!it->second) {
   1520           ADD_FAILURE() << "Missing receive stats: " << it->first;
   1521         }
   1522       }
   1523 
   1524       for (std::map<std::string, bool>::const_iterator it =
   1525                send_stats_filled_.begin();
   1526            it != send_stats_filled_.end();
   1527            ++it) {
   1528         if (!it->second) {
   1529           ADD_FAILURE() << "Missing send stats: " << it->first;
   1530         }
   1531       }
   1532     }
   1533 
   1534     VideoReceiveStream* receive_stream_;
   1535     std::map<std::string, bool> receive_stats_filled_;
   1536 
   1537     VideoSendStream* send_stream_;
   1538     std::map<std::string, bool> send_stats_filled_;
   1539 
   1540     uint32_t expected_receive_ssrc_;
   1541     std::set<uint32_t> expected_send_ssrcs_;
   1542     std::string expected_cname_;
   1543 
   1544     scoped_ptr<EventWrapper> check_stats_event_;
   1545   } test;
   1546 
   1547   RunBaseTest(&test);
   1548 }
   1549 
   1550 TEST_F(EndToEndTest, ReceiverReferenceTimeReportEnabled) {
   1551   TestXrReceiverReferenceTimeReport(true);
   1552 }
   1553 
   1554 TEST_F(EndToEndTest, ReceiverReferenceTimeReportDisabled) {
   1555   TestXrReceiverReferenceTimeReport(false);
   1556 }
   1557 
   1558 TEST_F(EndToEndTest, TestReceivedRtpPacketStats) {
   1559   static const size_t kNumRtpPacketsToSend = 5;
   1560   class ReceivedRtpStatsObserver : public test::EndToEndTest {
   1561    public:
   1562     ReceivedRtpStatsObserver()
   1563         : EndToEndTest(kDefaultTimeoutMs),
   1564           receive_stream_(NULL),
   1565           sent_rtp_(0) {}
   1566 
   1567    private:
   1568     virtual void OnStreamsCreated(
   1569         VideoSendStream* send_stream,
   1570         const std::vector<VideoReceiveStream*>& receive_streams) OVERRIDE {
   1571       receive_stream_ = receive_streams[0];
   1572     }
   1573 
   1574     virtual Action OnSendRtp(const uint8_t* packet, size_t length) OVERRIDE {
   1575       if (sent_rtp_ >= kNumRtpPacketsToSend) {
   1576         VideoReceiveStream::Stats stats = receive_stream_->GetStats();
   1577         if (kNumRtpPacketsToSend == stats.rtp_stats.packets) {
   1578           observation_complete_->Set();
   1579         }
   1580         return DROP_PACKET;
   1581       }
   1582       ++sent_rtp_;
   1583       return SEND_PACKET;
   1584     }
   1585 
   1586     virtual void PerformTest() OVERRIDE {
   1587       EXPECT_EQ(kEventSignaled, Wait())
   1588           << "Timed out while verifying number of received RTP packets.";
   1589     }
   1590 
   1591     VideoReceiveStream* receive_stream_;
   1592     uint32_t sent_rtp_;
   1593   } test;
   1594 
   1595   RunBaseTest(&test);
   1596 }
   1597 
   1598 TEST_F(EndToEndTest, SendsSetSsrc) { TestSendsSetSsrcs(1, false); }
   1599 
   1600 TEST_F(EndToEndTest, SendsSetSimulcastSsrcs) {
   1601   TestSendsSetSsrcs(kNumSsrcs, false);
   1602 }
   1603 
   1604 TEST_F(EndToEndTest, CanSwitchToUseAllSsrcs) {
   1605   TestSendsSetSsrcs(kNumSsrcs, true);
   1606 }
   1607 
   1608 TEST_F(EndToEndTest, DISABLED_RedundantPayloadsTransmittedOnAllSsrcs) {
   1609   class ObserveRedundantPayloads: public test::EndToEndTest {
   1610    public:
   1611     ObserveRedundantPayloads()
   1612         : EndToEndTest(kDefaultTimeoutMs), ssrcs_to_observe_(kNumSsrcs) {
   1613           for (size_t i = 0; i < kNumSsrcs; ++i) {
   1614             registered_rtx_ssrc_[kSendRtxSsrcs[i]] = true;
   1615           }
   1616         }
   1617 
   1618    private:
   1619     virtual Action OnSendRtp(const uint8_t* packet, size_t length) OVERRIDE {
   1620       RTPHeader header;
   1621       EXPECT_TRUE(parser_->Parse(packet, length, &header));
   1622 
   1623       if (!registered_rtx_ssrc_[header.ssrc])
   1624         return SEND_PACKET;
   1625 
   1626       EXPECT_LE(static_cast<size_t>(header.headerLength + header.paddingLength),
   1627                 length);
   1628       const bool packet_is_redundant_payload =
   1629           static_cast<size_t>(header.headerLength + header.paddingLength) <
   1630           length;
   1631 
   1632       if (!packet_is_redundant_payload)
   1633         return SEND_PACKET;
   1634 
   1635       if (!observed_redundant_retransmission_[header.ssrc]) {
   1636         observed_redundant_retransmission_[header.ssrc] = true;
   1637         if (--ssrcs_to_observe_ == 0)
   1638           observation_complete_->Set();
   1639       }
   1640 
   1641       return SEND_PACKET;
   1642     }
   1643 
   1644     virtual size_t GetNumStreams() const OVERRIDE { return kNumSsrcs; }
   1645 
   1646     virtual void ModifyConfigs(
   1647         VideoSendStream::Config* send_config,
   1648         std::vector<VideoReceiveStream::Config>* receive_configs,
   1649         VideoEncoderConfig* encoder_config) OVERRIDE {
   1650       // Set low simulcast bitrates to not have to wait for bandwidth ramp-up.
   1651       for (size_t i = 0; i < encoder_config->streams.size(); ++i) {
   1652         encoder_config->streams[i].min_bitrate_bps = 10000;
   1653         encoder_config->streams[i].target_bitrate_bps = 15000;
   1654         encoder_config->streams[i].max_bitrate_bps = 20000;
   1655       }
   1656       // Significantly higher than max bitrates for all video streams -> forcing
   1657       // padding to trigger redundant padding on all RTX SSRCs.
   1658       send_config->rtp.min_transmit_bitrate_bps = 100000;
   1659 
   1660       send_config->rtp.rtx.payload_type = kSendRtxPayloadType;
   1661       send_config->rtp.rtx.pad_with_redundant_payloads = true;
   1662 
   1663       for (size_t i = 0; i < kNumSsrcs; ++i)
   1664         send_config->rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[i]);
   1665     }
   1666 
   1667     virtual void PerformTest() OVERRIDE {
   1668       EXPECT_EQ(kEventSignaled, Wait())
   1669           << "Timed out while waiting for redundant payloads on all SSRCs.";
   1670     }
   1671 
   1672    private:
   1673     size_t ssrcs_to_observe_;
   1674     std::map<uint32_t, bool> observed_redundant_retransmission_;
   1675     std::map<uint32_t, bool> registered_rtx_ssrc_;
   1676   } test;
   1677 
   1678   RunBaseTest(&test);
   1679 }
   1680 
   1681 void EndToEndTest::TestRtpStatePreservation(bool use_rtx) {
   1682   static const uint32_t kMaxSequenceNumberGap = 100;
   1683   static const uint64_t kMaxTimestampGap = kDefaultTimeoutMs * 90;
   1684   class RtpSequenceObserver : public test::RtpRtcpObserver {
   1685    public:
   1686     explicit RtpSequenceObserver(bool use_rtx)
   1687         : test::RtpRtcpObserver(kDefaultTimeoutMs),
   1688           crit_(CriticalSectionWrapper::CreateCriticalSection()),
   1689           ssrcs_to_observe_(kNumSsrcs) {
   1690       for (size_t i = 0; i < kNumSsrcs; ++i) {
   1691         configured_ssrcs_[kSendSsrcs[i]] = true;
   1692         if (use_rtx)
   1693           configured_ssrcs_[kSendRtxSsrcs[i]] = true;
   1694       }
   1695     }
   1696 
   1697     void ResetExpectedSsrcs(size_t num_expected_ssrcs) {
   1698       CriticalSectionScoped lock(crit_.get());
   1699       ssrc_observed_.clear();
   1700       ssrcs_to_observe_ = num_expected_ssrcs;
   1701     }
   1702 
   1703    private:
   1704     virtual Action OnSendRtp(const uint8_t* packet, size_t length) OVERRIDE {
   1705       RTPHeader header;
   1706       EXPECT_TRUE(parser_->Parse(packet, length, &header));
   1707       const uint32_t ssrc = header.ssrc;
   1708       const uint16_t sequence_number = header.sequenceNumber;
   1709       const uint32_t timestamp = header.timestamp;
   1710       const bool only_padding =
   1711           static_cast<size_t>(header.headerLength + header.paddingLength) ==
   1712           length;
   1713 
   1714       EXPECT_TRUE(configured_ssrcs_[ssrc])
   1715           << "Received SSRC that wasn't configured: " << ssrc;
   1716 
   1717       std::map<uint32_t, uint16_t>::iterator it =
   1718           last_observed_sequence_number_.find(header.ssrc);
   1719       if (it == last_observed_sequence_number_.end()) {
   1720         last_observed_sequence_number_[ssrc] = sequence_number;
   1721         last_observed_timestamp_[ssrc] = timestamp;
   1722       } else {
   1723         // Verify sequence numbers are reasonably close.
   1724         uint32_t extended_sequence_number = sequence_number;
   1725         // Check for roll-over.
   1726         if (sequence_number < last_observed_sequence_number_[ssrc])
   1727           extended_sequence_number += 0xFFFFu + 1;
   1728         EXPECT_LE(
   1729             extended_sequence_number - last_observed_sequence_number_[ssrc],
   1730             kMaxSequenceNumberGap)
   1731             << "Gap in sequence numbers ("
   1732             << last_observed_sequence_number_[ssrc] << " -> " << sequence_number
   1733             << ") too large for SSRC: " << ssrc << ".";
   1734         last_observed_sequence_number_[ssrc] = sequence_number;
   1735 
   1736         // TODO(pbos): Remove this check if we ever have monotonically
   1737         // increasing timestamps. Right now padding packets add a delta which
   1738         // can cause reordering between padding packets and regular packets,
   1739         // hence we drop padding-only packets to not flake.
   1740         if (only_padding) {
   1741           // Verify that timestamps are reasonably close.
   1742           uint64_t extended_timestamp = timestamp;
   1743           // Check for roll-over.
   1744           if (timestamp < last_observed_timestamp_[ssrc])
   1745             extended_timestamp += static_cast<uint64_t>(0xFFFFFFFFu) + 1;
   1746           EXPECT_LE(extended_timestamp - last_observed_timestamp_[ssrc],
   1747                     kMaxTimestampGap)
   1748               << "Gap in timestamps (" << last_observed_timestamp_[ssrc]
   1749               << " -> " << timestamp << ") too large for SSRC: " << ssrc << ".";
   1750         }
   1751         last_observed_timestamp_[ssrc] = timestamp;
   1752       }
   1753 
   1754       CriticalSectionScoped lock(crit_.get());
   1755       // Wait for media packets on all ssrcs.
   1756       if (!ssrc_observed_[ssrc] && !only_padding) {
   1757         ssrc_observed_[ssrc] = true;
   1758         if (--ssrcs_to_observe_ == 0)
   1759           observation_complete_->Set();
   1760       }
   1761 
   1762       return SEND_PACKET;
   1763     }
   1764 
   1765     std::map<uint32_t, uint16_t> last_observed_sequence_number_;
   1766     std::map<uint32_t, uint32_t> last_observed_timestamp_;
   1767     std::map<uint32_t, bool> configured_ssrcs_;
   1768 
   1769     scoped_ptr<CriticalSectionWrapper> crit_;
   1770     size_t ssrcs_to_observe_ GUARDED_BY(crit_);
   1771     std::map<uint32_t, bool> ssrc_observed_ GUARDED_BY(crit_);
   1772   } observer(use_rtx);
   1773 
   1774   CreateCalls(Call::Config(observer.SendTransport()),
   1775               Call::Config(observer.ReceiveTransport()));
   1776   observer.SetReceivers(sender_call_->Receiver(), NULL);
   1777 
   1778   CreateSendConfig(kNumSsrcs);
   1779 
   1780   if (use_rtx) {
   1781     for (size_t i = 0; i < kNumSsrcs; ++i) {
   1782       send_config_.rtp.rtx.ssrcs.push_back(kSendRtxSsrcs[i]);
   1783     }
   1784     send_config_.rtp.rtx.payload_type = kSendRtxPayloadType;
   1785   }
   1786 
   1787   // Lower bitrates so that all streams send initially.
   1788   for (size_t i = 0; i < encoder_config_.streams.size(); ++i) {
   1789     encoder_config_.streams[i].min_bitrate_bps = 10000;
   1790     encoder_config_.streams[i].target_bitrate_bps = 15000;
   1791     encoder_config_.streams[i].max_bitrate_bps = 20000;
   1792   }
   1793 
   1794   CreateMatchingReceiveConfigs();
   1795 
   1796   CreateStreams();
   1797   CreateFrameGeneratorCapturer();
   1798 
   1799   Start();
   1800   EXPECT_EQ(kEventSignaled, observer.Wait())
   1801       << "Timed out waiting for all SSRCs to send packets.";
   1802 
   1803   // Test stream resetting more than once to make sure that the state doesn't
   1804   // get set once (this could be due to using std::map::insert for instance).
   1805   for (size_t i = 0; i < 3; ++i) {
   1806     frame_generator_capturer_->Stop();
   1807     sender_call_->DestroyVideoSendStream(send_stream_);
   1808 
   1809     // Re-create VideoSendStream with only one stream.
   1810     VideoEncoderConfig one_stream = encoder_config_;
   1811     one_stream.streams.resize(1);
   1812     send_stream_ =
   1813         sender_call_->CreateVideoSendStream(send_config_, one_stream);
   1814     send_stream_->Start();
   1815     CreateFrameGeneratorCapturer();
   1816     frame_generator_capturer_->Start();
   1817 
   1818     observer.ResetExpectedSsrcs(1);
   1819     EXPECT_EQ(kEventSignaled, observer.Wait())
   1820         << "Timed out waiting for single RTP packet.";
   1821 
   1822     // Reconfigure back to use all streams.
   1823     send_stream_->ReconfigureVideoEncoder(encoder_config_);
   1824     observer.ResetExpectedSsrcs(kNumSsrcs);
   1825     EXPECT_EQ(kEventSignaled, observer.Wait())
   1826         << "Timed out waiting for all SSRCs to send packets.";
   1827 
   1828     // Reconfigure down to one stream.
   1829     send_stream_->ReconfigureVideoEncoder(one_stream);
   1830     observer.ResetExpectedSsrcs(1);
   1831     EXPECT_EQ(kEventSignaled, observer.Wait())
   1832         << "Timed out waiting for single RTP packet.";
   1833 
   1834     // Reconfigure back to use all streams.
   1835     send_stream_->ReconfigureVideoEncoder(encoder_config_);
   1836     observer.ResetExpectedSsrcs(kNumSsrcs);
   1837     EXPECT_EQ(kEventSignaled, observer.Wait())
   1838         << "Timed out waiting for all SSRCs to send packets.";
   1839   }
   1840 
   1841   observer.StopSending();
   1842 
   1843   Stop();
   1844   DestroyStreams();
   1845 }
   1846 
   1847 TEST_F(EndToEndTest, DISABLED_RestartingSendStreamPreservesRtpState) {
   1848   TestRtpStatePreservation(false);
   1849 }
   1850 
   1851 TEST_F(EndToEndTest, RestartingSendStreamPreservesRtpStatesWithRtx) {
   1852   TestRtpStatePreservation(true);
   1853 }
   1854 
   1855 TEST_F(EndToEndTest, RespectsNetworkState) {
   1856   // TODO(pbos): Remove accepted downtime packets etc. when signaling network
   1857   // down blocks until no more packets will be sent.
   1858 
   1859   // Pacer will send from its packet list and then send required padding before
   1860   // checking paused_ again. This should be enough for one round of pacing,
   1861   // otherwise increase.
   1862   static const int kNumAcceptedDowntimeRtp = 5;
   1863   // A single RTCP may be in the pipeline.
   1864   static const int kNumAcceptedDowntimeRtcp = 1;
   1865   class NetworkStateTest : public test::EndToEndTest, public test::FakeEncoder {
   1866    public:
   1867     NetworkStateTest()
   1868         : EndToEndTest(kDefaultTimeoutMs),
   1869           FakeEncoder(Clock::GetRealTimeClock()),
   1870           test_crit_(CriticalSectionWrapper::CreateCriticalSection()),
   1871           encoded_frames_(EventWrapper::Create()),
   1872           sender_packets_(EventWrapper::Create()),
   1873           receiver_packets_(EventWrapper::Create()),
   1874           sender_state_(Call::kNetworkUp),
   1875           down_sender_rtp_(0),
   1876           down_sender_rtcp_(0),
   1877           receiver_state_(Call::kNetworkUp),
   1878           down_receiver_rtcp_(0),
   1879           down_frames_(0) {}
   1880 
   1881     virtual Action OnSendRtp(const uint8_t* packet, size_t length) OVERRIDE {
   1882       CriticalSectionScoped lock(test_crit_.get());
   1883       if (sender_state_ == Call::kNetworkDown) {
   1884         ++down_sender_rtp_;
   1885         EXPECT_LE(down_sender_rtp_, kNumAcceptedDowntimeRtp)
   1886             << "RTP sent during sender-side downtime.";
   1887         if (down_sender_rtp_> kNumAcceptedDowntimeRtp)
   1888           sender_packets_->Set();
   1889       } else {
   1890         sender_packets_->Set();
   1891       }
   1892       return SEND_PACKET;
   1893     }
   1894 
   1895     virtual Action OnSendRtcp(const uint8_t* packet, size_t length) OVERRIDE {
   1896       CriticalSectionScoped lock(test_crit_.get());
   1897       if (sender_state_ == Call::kNetworkDown) {
   1898         ++down_sender_rtcp_;
   1899         EXPECT_LE(down_sender_rtcp_, kNumAcceptedDowntimeRtcp)
   1900             << "RTCP sent during sender-side downtime.";
   1901         if (down_sender_rtcp_ > kNumAcceptedDowntimeRtcp)
   1902           sender_packets_->Set();
   1903       } else {
   1904         sender_packets_->Set();
   1905       }
   1906       return SEND_PACKET;
   1907     }
   1908 
   1909     virtual Action OnReceiveRtp(const uint8_t* packet, size_t length) OVERRIDE {
   1910       ADD_FAILURE() << "Unexpected receiver RTP, should not be sending.";
   1911       return SEND_PACKET;
   1912     }
   1913 
   1914     virtual Action OnReceiveRtcp(const uint8_t* packet,
   1915                                  size_t length) OVERRIDE {
   1916       CriticalSectionScoped lock(test_crit_.get());
   1917       if (receiver_state_ == Call::kNetworkDown) {
   1918         ++down_receiver_rtcp_;
   1919         EXPECT_LE(down_receiver_rtcp_, kNumAcceptedDowntimeRtcp)
   1920             << "RTCP sent during receiver-side downtime.";
   1921         if (down_receiver_rtcp_ > kNumAcceptedDowntimeRtcp)
   1922           receiver_packets_->Set();
   1923       } else {
   1924         receiver_packets_->Set();
   1925       }
   1926       return SEND_PACKET;
   1927     }
   1928 
   1929     virtual void OnCallsCreated(Call* sender_call,
   1930                                 Call* receiver_call) OVERRIDE {
   1931       sender_call_ = sender_call;
   1932       receiver_call_ = receiver_call;
   1933     }
   1934 
   1935     virtual void ModifyConfigs(
   1936         VideoSendStream::Config* send_config,
   1937         std::vector<VideoReceiveStream::Config>* receive_configs,
   1938         VideoEncoderConfig* encoder_config) OVERRIDE {
   1939       send_config->encoder_settings.encoder = this;
   1940     }
   1941 
   1942     virtual void PerformTest() OVERRIDE {
   1943       EXPECT_EQ(kEventSignaled, encoded_frames_->Wait(kDefaultTimeoutMs))
   1944           << "No frames received by the encoder.";
   1945       EXPECT_EQ(kEventSignaled, sender_packets_->Wait(kDefaultTimeoutMs))
   1946           << "Timed out waiting for send-side packets.";
   1947       EXPECT_EQ(kEventSignaled, receiver_packets_->Wait(kDefaultTimeoutMs))
   1948           << "Timed out waiting for receiver-side packets.";
   1949 
   1950       // Sender-side network down.
   1951       sender_call_->SignalNetworkState(Call::kNetworkDown);
   1952       {
   1953         CriticalSectionScoped lock(test_crit_.get());
   1954         sender_packets_->Reset();  // Earlier packets should not count.
   1955         sender_state_ = Call::kNetworkDown;
   1956       }
   1957       EXPECT_EQ(kEventTimeout, sender_packets_->Wait(kSilenceTimeoutMs))
   1958           << "Packets sent during sender-network downtime.";
   1959       EXPECT_EQ(kEventSignaled, receiver_packets_->Wait(kDefaultTimeoutMs))
   1960           << "Timed out waiting for receiver-side packets.";
   1961       // Receiver-side network down.
   1962       receiver_call_->SignalNetworkState(Call::kNetworkDown);
   1963       {
   1964         CriticalSectionScoped lock(test_crit_.get());
   1965         receiver_packets_->Reset();  // Earlier packets should not count.
   1966         receiver_state_ = Call::kNetworkDown;
   1967       }
   1968       EXPECT_EQ(kEventTimeout, receiver_packets_->Wait(kSilenceTimeoutMs))
   1969           << "Packets sent during receiver-network downtime.";
   1970 
   1971       // Network back up again for both.
   1972       {
   1973         CriticalSectionScoped lock(test_crit_.get());
   1974         sender_packets_->Reset();  // Earlier packets should not count.
   1975         receiver_packets_->Reset();  // Earlier packets should not count.
   1976         sender_state_ = receiver_state_ = Call::kNetworkUp;
   1977       }
   1978       sender_call_->SignalNetworkState(Call::kNetworkUp);
   1979       receiver_call_->SignalNetworkState(Call::kNetworkUp);
   1980       EXPECT_EQ(kEventSignaled, sender_packets_->Wait(kDefaultTimeoutMs))
   1981           << "Timed out waiting for send-side packets.";
   1982       EXPECT_EQ(kEventSignaled, receiver_packets_->Wait(kDefaultTimeoutMs))
   1983           << "Timed out waiting for receiver-side packets.";
   1984     }
   1985 
   1986     virtual int32_t Encode(const I420VideoFrame& input_image,
   1987                            const CodecSpecificInfo* codec_specific_info,
   1988                            const std::vector<VideoFrameType>* frame_types)
   1989         OVERRIDE {
   1990       {
   1991         CriticalSectionScoped lock(test_crit_.get());
   1992         if (sender_state_ == Call::kNetworkDown) {
   1993           ++down_frames_;
   1994           EXPECT_LE(down_frames_, 1)
   1995               << "Encoding more than one frame while network is down.";
   1996           if (down_frames_ > 1)
   1997             encoded_frames_->Set();
   1998         } else {
   1999           encoded_frames_->Set();
   2000         }
   2001       }
   2002       return test::FakeEncoder::Encode(
   2003           input_image, codec_specific_info, frame_types);
   2004     }
   2005 
   2006    private:
   2007     const scoped_ptr<CriticalSectionWrapper> test_crit_;
   2008     scoped_ptr<EventWrapper> encoded_frames_;
   2009     scoped_ptr<EventWrapper> sender_packets_;
   2010     scoped_ptr<EventWrapper> receiver_packets_;
   2011     Call* sender_call_;
   2012     Call* receiver_call_;
   2013     Call::NetworkState sender_state_ GUARDED_BY(test_crit_);
   2014     int down_sender_rtp_ GUARDED_BY(test_crit_);
   2015     int down_sender_rtcp_ GUARDED_BY(test_crit_);
   2016     Call::NetworkState receiver_state_ GUARDED_BY(test_crit_);
   2017     int down_receiver_rtcp_ GUARDED_BY(test_crit_);
   2018     int down_frames_ GUARDED_BY(test_crit_);
   2019   } test;
   2020 
   2021   RunBaseTest(&test);
   2022 }
   2023 
   2024 TEST_F(EndToEndTest, NewSendStreamsRespectNetworkDown) {
   2025   class UnusedEncoder : public test::FakeEncoder {
   2026     public:
   2027      UnusedEncoder() : FakeEncoder(Clock::GetRealTimeClock()) {}
   2028     virtual int32_t Encode(const I420VideoFrame& input_image,
   2029                            const CodecSpecificInfo* codec_specific_info,
   2030                            const std::vector<VideoFrameType>* frame_types)
   2031         OVERRIDE {
   2032       ADD_FAILURE() << "Unexpected frame encode.";
   2033       return test::FakeEncoder::Encode(
   2034           input_image, codec_specific_info, frame_types);
   2035     }
   2036   };
   2037 
   2038   UnusedTransport transport;
   2039   CreateSenderCall(Call::Config(&transport));
   2040   sender_call_->SignalNetworkState(Call::kNetworkDown);
   2041 
   2042   CreateSendConfig(1);
   2043   UnusedEncoder unused_encoder;
   2044   send_config_.encoder_settings.encoder = &unused_encoder;
   2045   CreateStreams();
   2046   CreateFrameGeneratorCapturer();
   2047 
   2048   Start();
   2049   SleepMs(kSilenceTimeoutMs);
   2050   Stop();
   2051 
   2052   DestroyStreams();
   2053 }
   2054 
   2055 TEST_F(EndToEndTest, NewReceiveStreamsRespectNetworkDown) {
   2056   test::DirectTransport sender_transport;
   2057   CreateSenderCall(Call::Config(&sender_transport));
   2058   UnusedTransport transport;
   2059   CreateReceiverCall(Call::Config(&transport));
   2060   sender_transport.SetReceiver(receiver_call_->Receiver());
   2061 
   2062   receiver_call_->SignalNetworkState(Call::kNetworkDown);
   2063 
   2064   CreateSendConfig(1);
   2065   CreateMatchingReceiveConfigs();
   2066   CreateStreams();
   2067   CreateFrameGeneratorCapturer();
   2068 
   2069   Start();
   2070   SleepMs(kSilenceTimeoutMs);
   2071   Stop();
   2072 
   2073   sender_transport.StopSending();
   2074 
   2075   DestroyStreams();
   2076 }
   2077 }  // namespace webrtc
   2078