Home | History | Annotate | Download | only in base
      1 /*
      2  * libjingle
      3  * Copyright 2014 Google Inc.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are met:
      7  *
      8  *  1. Redistributions of source code must retain the above copyright notice,
      9  *     this list of conditions and the following disclaimer.
     10  *  2. Redistributions in binary form must reproduce the above copyright notice,
     11  *     this list of conditions and the following disclaimer in the documentation
     12  *     and/or other materials provided with the distribution.
     13  *  3. The name of the author may not be used to endorse or promote products
     14  *     derived from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
     19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #ifndef TALK_MEDIA_BASE_VIDEOENGINE_UNITTEST_H_  // NOLINT
     29 #define TALK_MEDIA_BASE_VIDEOENGINE_UNITTEST_H_
     30 
     31 #include <string>
     32 #include <vector>
     33 
     34 #include "talk/media/base/fakenetworkinterface.h"
     35 #include "talk/media/base/fakevideocapturer.h"
     36 #include "talk/media/base/fakevideorenderer.h"
     37 #include "talk/media/base/mediachannel.h"
     38 #include "talk/media/base/streamparams.h"
     39 #include "talk/media/webrtc/fakewebrtccall.h"
     40 #include "webrtc/base/bytebuffer.h"
     41 #include "webrtc/base/gunit.h"
     42 #include "webrtc/base/timeutils.h"
     43 #include "webrtc/call.h"
     44 
     45 #define EXPECT_FRAME_WAIT(c, w, h, t) \
     46   EXPECT_EQ_WAIT((c), renderer_.num_rendered_frames(), (t)); \
     47   EXPECT_EQ((w), renderer_.width()); \
     48   EXPECT_EQ((h), renderer_.height()); \
     49   EXPECT_EQ(0, renderer_.errors()); \
     50 
     51 #define EXPECT_FRAME_ON_RENDERER_WAIT(r, c, w, h, t) \
     52   EXPECT_EQ_WAIT((c), (r).num_rendered_frames(), (t)); \
     53   EXPECT_EQ((w), (r).width()); \
     54   EXPECT_EQ((h), (r).height()); \
     55   EXPECT_EQ(0, (r).errors()); \
     56 
     57 #define EXPECT_GT_FRAME_ON_RENDERER_WAIT(r, c, w, h, t) \
     58   EXPECT_TRUE_WAIT((r).num_rendered_frames() >= (c) && \
     59                    (w) == (r).width() && \
     60                    (h) == (r).height(), (t)); \
     61   EXPECT_EQ(0, (r).errors());
     62 
     63 static const uint32_t kTimeout = 5000U;
     64 static const uint32_t kDefaultReceiveSsrc = 0;
     65 static const uint32_t kSsrc = 1234u;
     66 static const uint32_t kRtxSsrc = 4321u;
     67 static const uint32_t kSsrcs4[] = {1, 2, 3, 4};
     68 
     69 inline bool IsEqualRes(const cricket::VideoCodec& a, int w, int h, int fps) {
     70   return a.width == w && a.height == h && a.framerate == fps;
     71 }
     72 
     73 inline bool IsEqualCodec(const cricket::VideoCodec& a,
     74                          const cricket::VideoCodec& b) {
     75   return a.id == b.id && a.name == b.name &&
     76       IsEqualRes(a, b.width, b.height, b.framerate);
     77 }
     78 
     79 namespace std {
     80 inline std::ostream& operator<<(std::ostream& s, const cricket::VideoCodec& c) {
     81   s << "{" << c.name << "(" << c.id << "), "
     82     << c.width << "x" << c.height << "x" << c.framerate << "}";
     83   return s;
     84 }
     85 }  // namespace std
     86 
     87 inline int TimeBetweenSend(const cricket::VideoCodec& codec) {
     88   return static_cast<int>(
     89       cricket::VideoFormat::FpsToInterval(codec.framerate) /
     90       rtc::kNumNanosecsPerMillisec);
     91 }
     92 
     93 // Fake video engine that makes it possible to test enabling and disabling
     94 // capturer (checking that the engine state is updated and that the capturer
     95 // is indeed capturing) without having to create a channel. It also makes it
     96 // possible to test that the media processors are indeed being called when
     97 // registered.
     98 template<class T>
     99 class VideoEngineOverride : public T {
    100  public:
    101   VideoEngineOverride() : T() {
    102   }
    103   virtual ~VideoEngineOverride() {
    104   }
    105   bool is_camera_on() const { return T::GetVideoCapturer()->IsRunning(); }
    106   void set_has_senders(bool has_senders) {
    107     cricket::VideoCapturer* video_capturer = T::GetVideoCapturer();
    108     if (has_senders) {
    109       video_capturer->SignalVideoFrame.connect(this,
    110           &VideoEngineOverride<T>::OnLocalFrame);
    111     } else {
    112       video_capturer->SignalVideoFrame.disconnect(this);
    113     }
    114   }
    115   void OnLocalFrame(cricket::VideoCapturer*,
    116                     const cricket::VideoFrame*) {
    117   }
    118   void OnLocalFrameFormat(cricket::VideoCapturer*,
    119                           const cricket::VideoFormat*) {
    120   }
    121 
    122   void TriggerMediaFrame(uint32_t ssrc,
    123                          cricket::VideoFrame* frame,
    124                          bool* drop_frame) {
    125     T::SignalMediaFrame(ssrc, frame, drop_frame);
    126   }
    127 };
    128 
    129 template<class E, class C>
    130 class VideoMediaChannelTest : public testing::Test,
    131                               public sigslot::has_slots<> {
    132  protected:
    133   VideoMediaChannelTest<E, C>()
    134       : call_(webrtc::Call::Create(webrtc::Call::Config())) {}
    135 
    136   virtual cricket::VideoCodec DefaultCodec() = 0;
    137 
    138   virtual cricket::StreamParams DefaultSendStreamParams() {
    139     return cricket::StreamParams::CreateLegacy(kSsrc);
    140   }
    141 
    142   virtual void SetUp() {
    143     cricket::Device device("test", "device");
    144     engine_.Init();
    145     channel_.reset(
    146         engine_.CreateChannel(call_.get(), cricket::VideoOptions()));
    147     EXPECT_TRUE(channel_.get() != NULL);
    148     network_interface_.SetDestination(channel_.get());
    149     channel_->SetInterface(&network_interface_);
    150     media_error_ = cricket::VideoMediaChannel::ERROR_NONE;
    151     cricket::VideoRecvParameters parameters;
    152     parameters.codecs = engine_.codecs();
    153     channel_->SetRecvParameters(parameters);
    154     EXPECT_TRUE(channel_->AddSendStream(DefaultSendStreamParams()));
    155     video_capturer_.reset(CreateFakeVideoCapturer());
    156     cricket::VideoFormat format(640, 480,
    157                                 cricket::VideoFormat::FpsToInterval(30),
    158                                 cricket::FOURCC_I420);
    159     EXPECT_EQ(cricket::CS_RUNNING, video_capturer_->Start(format));
    160     EXPECT_TRUE(channel_->SetCapturer(kSsrc, video_capturer_.get()));
    161   }
    162 
    163   virtual cricket::FakeVideoCapturer* CreateFakeVideoCapturer() {
    164     return new cricket::FakeVideoCapturer();
    165   }
    166 
    167   // Utility method to setup an additional stream to send and receive video.
    168   // Used to test send and recv between two streams.
    169   void SetUpSecondStream() {
    170     SetUpSecondStreamWithNoRecv();
    171     // Setup recv for second stream.
    172     EXPECT_TRUE(channel_->AddRecvStream(
    173         cricket::StreamParams::CreateLegacy(kSsrc + 2)));
    174     // Make the second renderer available for use by a new stream.
    175     EXPECT_TRUE(channel_->SetRenderer(kSsrc + 2, &renderer2_));
    176   }
    177   // Setup an additional stream just to send video. Defer add recv stream.
    178   // This is required if you want to test unsignalled recv of video rtp packets.
    179   void SetUpSecondStreamWithNoRecv() {
    180     // SetUp() already added kSsrc make sure duplicate SSRCs cant be added.
    181     EXPECT_TRUE(channel_->AddRecvStream(
    182         cricket::StreamParams::CreateLegacy(kSsrc)));
    183     EXPECT_TRUE(channel_->SetRenderer(kSsrc, &renderer_));
    184     EXPECT_FALSE(channel_->AddSendStream(
    185         cricket::StreamParams::CreateLegacy(kSsrc)));
    186     EXPECT_TRUE(channel_->AddSendStream(
    187         cricket::StreamParams::CreateLegacy(kSsrc + 2)));
    188     // We dont add recv for the second stream.
    189 
    190     // Setup the receive and renderer for second stream after send.
    191     video_capturer_2_.reset(CreateFakeVideoCapturer());
    192     cricket::VideoFormat format(640, 480,
    193                                 cricket::VideoFormat::FpsToInterval(30),
    194                                 cricket::FOURCC_I420);
    195     EXPECT_EQ(cricket::CS_RUNNING, video_capturer_2_->Start(format));
    196 
    197     EXPECT_TRUE(channel_->SetCapturer(kSsrc + 2, video_capturer_2_.get()));
    198   }
    199   virtual void TearDown() {
    200     channel_.reset();
    201   }
    202   bool SetDefaultCodec() {
    203     return SetOneCodec(DefaultCodec());
    204   }
    205 
    206   bool SetOneCodec(int pt, const char* name, int w, int h, int fr) {
    207     return SetOneCodec(cricket::VideoCodec(pt, name, w, h, fr, 0));
    208   }
    209   bool SetOneCodec(const cricket::VideoCodec& codec) {
    210     cricket::VideoFormat capture_format(codec.width, codec.height,
    211         cricket::VideoFormat::FpsToInterval(codec.framerate),
    212         cricket::FOURCC_I420);
    213 
    214     if (video_capturer_) {
    215       EXPECT_EQ(cricket::CS_RUNNING, video_capturer_->Start(capture_format));
    216     }
    217     if (video_capturer_2_) {
    218       EXPECT_EQ(cricket::CS_RUNNING, video_capturer_2_->Start(capture_format));
    219     }
    220 
    221     bool sending = channel_->sending();
    222     bool success = SetSend(false);
    223     if (success) {
    224       cricket::VideoSendParameters parameters;
    225       parameters.codecs.push_back(codec);
    226       success = channel_->SetSendParameters(parameters);
    227     }
    228     if (success) {
    229       success = SetSend(sending);
    230     }
    231     return success;
    232   }
    233   bool SetSend(bool send) {
    234     return channel_->SetSend(send);
    235   }
    236   bool SetSendStreamFormat(uint32_t ssrc, const cricket::VideoCodec& codec) {
    237     return channel_->SetSendStreamFormat(ssrc, cricket::VideoFormat(
    238         codec.width, codec.height,
    239         cricket::VideoFormat::FpsToInterval(codec.framerate),
    240         cricket::FOURCC_ANY));
    241   }
    242   int DrainOutgoingPackets() {
    243     int packets = 0;
    244     do {
    245       packets = NumRtpPackets();
    246       // 100 ms should be long enough.
    247       rtc::Thread::Current()->ProcessMessages(100);
    248     } while (NumRtpPackets() > packets);
    249     return NumRtpPackets();
    250   }
    251   bool SendFrame() {
    252     if (video_capturer_2_) {
    253       video_capturer_2_->CaptureFrame();
    254     }
    255     return video_capturer_.get() &&
    256         video_capturer_->CaptureFrame();
    257   }
    258   bool WaitAndSendFrame(int wait_ms) {
    259     bool ret = rtc::Thread::Current()->ProcessMessages(wait_ms);
    260     ret &= SendFrame();
    261     return ret;
    262   }
    263   // Sends frames and waits for the decoder to be fully initialized.
    264   // Returns the number of frames that were sent.
    265   int WaitForDecoder() {
    266 #if defined(HAVE_OPENMAX)
    267     // Send enough frames for the OpenMAX decoder to continue processing, and
    268     // return the number of frames sent.
    269     // Send frames for a full kTimeout's worth of 15fps video.
    270     int frame_count = 0;
    271     while (frame_count < static_cast<int>(kTimeout) / 66) {
    272       EXPECT_TRUE(WaitAndSendFrame(66));
    273       ++frame_count;
    274     }
    275     return frame_count;
    276 #else
    277     return 0;
    278 #endif
    279   }
    280   bool SendCustomVideoFrame(int w, int h) {
    281     if (!video_capturer_.get()) return false;
    282     return video_capturer_->CaptureCustomFrame(w, h, cricket::FOURCC_I420);
    283   }
    284   int NumRtpBytes() {
    285     return network_interface_.NumRtpBytes();
    286   }
    287   int NumRtpBytes(uint32_t ssrc) {
    288     return network_interface_.NumRtpBytes(ssrc);
    289   }
    290   int NumRtpPackets() {
    291     return network_interface_.NumRtpPackets();
    292   }
    293   int NumRtpPackets(uint32_t ssrc) {
    294     return network_interface_.NumRtpPackets(ssrc);
    295   }
    296   int NumSentSsrcs() {
    297     return network_interface_.NumSentSsrcs();
    298   }
    299   const rtc::Buffer* GetRtpPacket(int index) {
    300     return network_interface_.GetRtpPacket(index);
    301   }
    302   int NumRtcpPackets() {
    303     return network_interface_.NumRtcpPackets();
    304   }
    305   const rtc::Buffer* GetRtcpPacket(int index) {
    306     return network_interface_.GetRtcpPacket(index);
    307   }
    308   static int GetPayloadType(const rtc::Buffer* p) {
    309     int pt = -1;
    310     ParseRtpPacket(p, NULL, &pt, NULL, NULL, NULL, NULL);
    311     return pt;
    312   }
    313   static bool ParseRtpPacket(const rtc::Buffer* p,
    314                              bool* x,
    315                              int* pt,
    316                              int* seqnum,
    317                              uint32_t* tstamp,
    318                              uint32_t* ssrc,
    319                              std::string* payload) {
    320     rtc::ByteBuffer buf(*p);
    321     uint8_t u08 = 0;
    322     uint16_t u16 = 0;
    323     uint32_t u32 = 0;
    324 
    325     // Read X and CC fields.
    326     if (!buf.ReadUInt8(&u08)) return false;
    327     bool extension = ((u08 & 0x10) != 0);
    328     uint8_t cc = (u08 & 0x0F);
    329     if (x) *x = extension;
    330 
    331     // Read PT field.
    332     if (!buf.ReadUInt8(&u08)) return false;
    333     if (pt) *pt = (u08 & 0x7F);
    334 
    335     // Read Sequence Number field.
    336     if (!buf.ReadUInt16(&u16)) return false;
    337     if (seqnum) *seqnum = u16;
    338 
    339     // Read Timestamp field.
    340     if (!buf.ReadUInt32(&u32)) return false;
    341     if (tstamp) *tstamp = u32;
    342 
    343     // Read SSRC field.
    344     if (!buf.ReadUInt32(&u32)) return false;
    345     if (ssrc) *ssrc = u32;
    346 
    347     // Skip CSRCs.
    348     for (uint8_t i = 0; i < cc; ++i) {
    349       if (!buf.ReadUInt32(&u32)) return false;
    350     }
    351 
    352     // Skip extension header.
    353     if (extension) {
    354       // Read Profile-specific extension header ID
    355       if (!buf.ReadUInt16(&u16)) return false;
    356 
    357       // Read Extension header length
    358       if (!buf.ReadUInt16(&u16)) return false;
    359       uint16_t ext_header_len = u16;
    360 
    361       // Read Extension header
    362       for (uint16_t i = 0; i < ext_header_len; ++i) {
    363         if (!buf.ReadUInt32(&u32)) return false;
    364       }
    365     }
    366 
    367     if (payload) {
    368       return buf.ReadString(payload, buf.Length());
    369     }
    370     return true;
    371   }
    372 
    373   // Parse all RTCP packet, from start_index to stop_index, and count how many
    374   // FIR (PT=206 and FMT=4 according to RFC 5104). If successful, set the count
    375   // and return true.
    376   bool CountRtcpFir(int start_index, int stop_index, int* fir_count) {
    377     int count = 0;
    378     for (int i = start_index; i < stop_index; ++i) {
    379       rtc::scoped_ptr<const rtc::Buffer> p(GetRtcpPacket(i));
    380       rtc::ByteBuffer buf(*p);
    381       size_t total_len = 0;
    382       // The packet may be a compound RTCP packet.
    383       while (total_len < p->size()) {
    384         // Read FMT, type and length.
    385         uint8_t fmt = 0;
    386         uint8_t type = 0;
    387         uint16_t length = 0;
    388         if (!buf.ReadUInt8(&fmt)) return false;
    389         fmt &= 0x1F;
    390         if (!buf.ReadUInt8(&type)) return false;
    391         if (!buf.ReadUInt16(&length)) return false;
    392         buf.Consume(length * 4);  // Skip RTCP data.
    393         total_len += (length + 1) * 4;
    394         if ((192 == type) || ((206 == type) && (4 == fmt))) {
    395           ++count;
    396         }
    397       }
    398     }
    399 
    400     if (fir_count) {
    401       *fir_count = count;
    402     }
    403     return true;
    404   }
    405 
    406   void OnVideoChannelError(uint32_t ssrc,
    407                            cricket::VideoMediaChannel::Error error) {
    408     media_error_ = error;
    409   }
    410 
    411   // Test that SetSend works.
    412   void SetSend() {
    413     EXPECT_FALSE(channel_->sending());
    414     EXPECT_TRUE(channel_->SetCapturer(kSsrc, video_capturer_.get()));
    415     EXPECT_TRUE(SetOneCodec(DefaultCodec()));
    416     EXPECT_FALSE(channel_->sending());
    417     EXPECT_TRUE(SetSend(true));
    418     EXPECT_TRUE(channel_->sending());
    419     EXPECT_TRUE(SendFrame());
    420     EXPECT_TRUE_WAIT(NumRtpPackets() > 0, kTimeout);
    421     EXPECT_TRUE(SetSend(false));
    422     EXPECT_FALSE(channel_->sending());
    423   }
    424   // Test that SetSend fails without codecs being set.
    425   void SetSendWithoutCodecs() {
    426     EXPECT_FALSE(channel_->sending());
    427     EXPECT_FALSE(SetSend(true));
    428     EXPECT_FALSE(channel_->sending());
    429   }
    430   // Test that we properly set the send and recv buffer sizes by the time
    431   // SetSend is called.
    432   void SetSendSetsTransportBufferSizes() {
    433     EXPECT_TRUE(SetOneCodec(DefaultCodec()));
    434     EXPECT_TRUE(SetSend(true));
    435     EXPECT_EQ(64 * 1024, network_interface_.sendbuf_size());
    436     EXPECT_EQ(64 * 1024, network_interface_.recvbuf_size());
    437   }
    438   // Tests that we can send frames and the right payload type is used.
    439   void Send(const cricket::VideoCodec& codec) {
    440     EXPECT_TRUE(SetOneCodec(codec));
    441     EXPECT_TRUE(SetSend(true));
    442     EXPECT_TRUE(SendFrame());
    443     EXPECT_TRUE_WAIT(NumRtpPackets() > 0, kTimeout);
    444     rtc::scoped_ptr<const rtc::Buffer> p(GetRtpPacket(0));
    445     EXPECT_EQ(codec.id, GetPayloadType(p.get()));
    446   }
    447   // Tests that we can send and receive frames.
    448   void SendAndReceive(const cricket::VideoCodec& codec) {
    449     EXPECT_TRUE(SetOneCodec(codec));
    450     EXPECT_TRUE(SetSend(true));
    451     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
    452     EXPECT_EQ(0, renderer_.num_rendered_frames());
    453     EXPECT_TRUE(SendFrame());
    454     EXPECT_FRAME_WAIT(1, codec.width, codec.height, kTimeout);
    455     rtc::scoped_ptr<const rtc::Buffer> p(GetRtpPacket(0));
    456     EXPECT_EQ(codec.id, GetPayloadType(p.get()));
    457   }
    458   // Tests that we only get a VideoRenderer::SetSize() callback when needed.
    459   void SendManyResizeOnce() {
    460     cricket::VideoCodec codec(DefaultCodec());
    461     EXPECT_TRUE(SetOneCodec(codec));
    462     EXPECT_TRUE(SetSend(true));
    463     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
    464     EXPECT_EQ(0, renderer_.num_rendered_frames());
    465     EXPECT_TRUE(WaitAndSendFrame(30));
    466     EXPECT_FRAME_WAIT(1, codec.width, codec.height, kTimeout);
    467     EXPECT_TRUE(WaitAndSendFrame(30));
    468     EXPECT_FRAME_WAIT(2, codec.width, codec.height, kTimeout);
    469     rtc::scoped_ptr<const rtc::Buffer> p(GetRtpPacket(0));
    470     EXPECT_EQ(codec.id, GetPayloadType(p.get()));
    471     EXPECT_EQ(1, renderer_.num_set_sizes());
    472 
    473     codec.width /= 2;
    474     codec.height /= 2;
    475     EXPECT_TRUE(SetOneCodec(codec));
    476     EXPECT_TRUE(WaitAndSendFrame(30));
    477     EXPECT_FRAME_WAIT(3, codec.width, codec.height, kTimeout);
    478     EXPECT_EQ(2, renderer_.num_set_sizes());
    479   }
    480   void SendReceiveManyAndGetStats(const cricket::VideoCodec& codec,
    481                                   int duration_sec, int fps) {
    482     EXPECT_TRUE(SetOneCodec(codec));
    483     EXPECT_TRUE(SetSend(true));
    484     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
    485     EXPECT_EQ(0, renderer_.num_rendered_frames());
    486     for (int i = 0; i < duration_sec; ++i) {
    487       for (int frame = 1; frame <= fps; ++frame) {
    488         EXPECT_TRUE(WaitAndSendFrame(1000 / fps));
    489         EXPECT_FRAME_WAIT(frame + i * fps, codec.width, codec.height, kTimeout);
    490       }
    491     }
    492     rtc::scoped_ptr<const rtc::Buffer> p(GetRtpPacket(0));
    493     EXPECT_EQ(codec.id, GetPayloadType(p.get()));
    494   }
    495 
    496   // Test that stats work properly for a 1-1 call.
    497   void GetStats() {
    498     const int kDurationSec = 3;
    499     const int kFps = 10;
    500     SendReceiveManyAndGetStats(DefaultCodec(), kDurationSec, kFps);
    501 
    502     cricket::VideoMediaInfo info;
    503     EXPECT_TRUE(channel_->GetStats(&info));
    504 
    505     ASSERT_EQ(1U, info.senders.size());
    506     // TODO(whyuan): bytes_sent and bytes_rcvd are different. Are both payload?
    507     // For webrtc, bytes_sent does not include the RTP header length.
    508     EXPECT_GT(info.senders[0].bytes_sent, 0);
    509     EXPECT_EQ(NumRtpPackets(), info.senders[0].packets_sent);
    510     EXPECT_EQ(0.0, info.senders[0].fraction_lost);
    511     EXPECT_EQ(0, info.senders[0].firs_rcvd);
    512     EXPECT_EQ(0, info.senders[0].plis_rcvd);
    513     EXPECT_EQ(0, info.senders[0].nacks_rcvd);
    514     EXPECT_EQ(DefaultCodec().width, info.senders[0].send_frame_width);
    515     EXPECT_EQ(DefaultCodec().height, info.senders[0].send_frame_height);
    516     EXPECT_GT(info.senders[0].framerate_input, 0);
    517     EXPECT_GT(info.senders[0].framerate_sent, 0);
    518 
    519     ASSERT_EQ(1U, info.receivers.size());
    520     EXPECT_EQ(1U, info.senders[0].ssrcs().size());
    521     EXPECT_EQ(1U, info.receivers[0].ssrcs().size());
    522     EXPECT_EQ(info.senders[0].ssrcs()[0], info.receivers[0].ssrcs()[0]);
    523     EXPECT_EQ(NumRtpBytes(), info.receivers[0].bytes_rcvd);
    524     EXPECT_EQ(NumRtpPackets(), info.receivers[0].packets_rcvd);
    525     EXPECT_EQ(0.0, info.receivers[0].fraction_lost);
    526     EXPECT_EQ(0, info.receivers[0].packets_lost);
    527     // TODO(asapersson): Not set for webrtc. Handle missing stats.
    528     // EXPECT_EQ(0, info.receivers[0].packets_concealed);
    529     EXPECT_EQ(0, info.receivers[0].firs_sent);
    530     EXPECT_EQ(0, info.receivers[0].plis_sent);
    531     EXPECT_EQ(0, info.receivers[0].nacks_sent);
    532     EXPECT_EQ(DefaultCodec().width, info.receivers[0].frame_width);
    533     EXPECT_EQ(DefaultCodec().height, info.receivers[0].frame_height);
    534     EXPECT_GT(info.receivers[0].framerate_rcvd, 0);
    535     EXPECT_GT(info.receivers[0].framerate_decoded, 0);
    536     EXPECT_GT(info.receivers[0].framerate_output, 0);
    537   }
    538 
    539   cricket::VideoSenderInfo GetSenderStats(size_t i) {
    540     cricket::VideoMediaInfo info;
    541     EXPECT_TRUE(channel_->GetStats(&info));
    542     return info.senders[i];
    543   }
    544 
    545   cricket::VideoReceiverInfo GetReceiverStats(size_t i) {
    546     cricket::VideoMediaInfo info;
    547     EXPECT_TRUE(channel_->GetStats(&info));
    548     return info.receivers[i];
    549   }
    550 
    551   // Test that stats work properly for a conf call with multiple recv streams.
    552   void GetStatsMultipleRecvStreams() {
    553     cricket::FakeVideoRenderer renderer1, renderer2;
    554     EXPECT_TRUE(SetOneCodec(DefaultCodec()));
    555     cricket::VideoSendParameters parameters;
    556     parameters.codecs.push_back(DefaultCodec());
    557     parameters.options.conference_mode = rtc::Optional<bool>(true);
    558     EXPECT_TRUE(channel_->SetSendParameters(parameters));
    559     EXPECT_TRUE(SetSend(true));
    560     EXPECT_TRUE(channel_->AddRecvStream(
    561         cricket::StreamParams::CreateLegacy(1)));
    562     EXPECT_TRUE(channel_->AddRecvStream(
    563         cricket::StreamParams::CreateLegacy(2)));
    564     EXPECT_TRUE(channel_->SetRenderer(1, &renderer1));
    565     EXPECT_TRUE(channel_->SetRenderer(2, &renderer2));
    566     EXPECT_EQ(0, renderer1.num_rendered_frames());
    567     EXPECT_EQ(0, renderer2.num_rendered_frames());
    568     std::vector<uint32_t> ssrcs;
    569     ssrcs.push_back(1);
    570     ssrcs.push_back(2);
    571     network_interface_.SetConferenceMode(true, ssrcs);
    572     EXPECT_TRUE(SendFrame());
    573     EXPECT_FRAME_ON_RENDERER_WAIT(
    574         renderer1, 1, DefaultCodec().width, DefaultCodec().height, kTimeout);
    575     EXPECT_FRAME_ON_RENDERER_WAIT(
    576         renderer2, 1, DefaultCodec().width, DefaultCodec().height, kTimeout);
    577 
    578     EXPECT_TRUE(channel_->SetSend(false));
    579 
    580     cricket::VideoMediaInfo info;
    581     EXPECT_TRUE(channel_->GetStats(&info));
    582     ASSERT_EQ(1U, info.senders.size());
    583     // TODO(whyuan): bytes_sent and bytes_rcvd are different. Are both payload?
    584     // For webrtc, bytes_sent does not include the RTP header length.
    585     EXPECT_GT(GetSenderStats(0).bytes_sent, 0);
    586     EXPECT_EQ_WAIT(NumRtpPackets(), GetSenderStats(0).packets_sent, kTimeout);
    587     EXPECT_EQ(DefaultCodec().width, GetSenderStats(0).send_frame_width);
    588     EXPECT_EQ(DefaultCodec().height, GetSenderStats(0).send_frame_height);
    589 
    590     ASSERT_EQ(2U, info.receivers.size());
    591     for (size_t i = 0; i < info.receivers.size(); ++i) {
    592       EXPECT_EQ(1U, GetReceiverStats(i).ssrcs().size());
    593       EXPECT_EQ(i + 1, GetReceiverStats(i).ssrcs()[0]);
    594       EXPECT_EQ_WAIT(NumRtpBytes(), GetReceiverStats(i).bytes_rcvd, kTimeout);
    595       EXPECT_EQ_WAIT(NumRtpPackets(), GetReceiverStats(i).packets_rcvd,
    596                      kTimeout);
    597       EXPECT_EQ(DefaultCodec().width, GetReceiverStats(i).frame_width);
    598       EXPECT_EQ(DefaultCodec().height, GetReceiverStats(i).frame_height);
    599     }
    600   }
    601   // Test that stats work properly for a conf call with multiple send streams.
    602   void GetStatsMultipleSendStreams() {
    603     // Normal setup; note that we set the SSRC explicitly to ensure that
    604     // it will come first in the senders map.
    605     EXPECT_TRUE(SetOneCodec(DefaultCodec()));
    606     cricket::VideoSendParameters parameters;
    607     parameters.codecs.push_back(DefaultCodec());
    608     parameters.options.conference_mode = rtc::Optional<bool>(true);
    609     EXPECT_TRUE(channel_->SetSendParameters(parameters));
    610     EXPECT_TRUE(channel_->AddRecvStream(
    611         cricket::StreamParams::CreateLegacy(kSsrc)));
    612     EXPECT_TRUE(channel_->SetRenderer(kSsrc, &renderer_));
    613     channel_->UpdateAspectRatio(640, 400);
    614     EXPECT_TRUE(SetSend(true));
    615     EXPECT_TRUE(SendFrame());
    616     EXPECT_TRUE_WAIT(NumRtpPackets() > 0, kTimeout);
    617     EXPECT_FRAME_WAIT(1, DefaultCodec().width, DefaultCodec().height, kTimeout);
    618 
    619     // Add an additional capturer, and hook up a renderer to receive it.
    620     cricket::FakeVideoRenderer renderer2;
    621     rtc::scoped_ptr<cricket::FakeVideoCapturer> capturer(
    622         CreateFakeVideoCapturer());
    623     capturer->SetScreencast(true);
    624     const int kTestWidth = 160;
    625     const int kTestHeight = 120;
    626     cricket::VideoFormat format(kTestWidth, kTestHeight,
    627                                 cricket::VideoFormat::FpsToInterval(5),
    628                                 cricket::FOURCC_I420);
    629     EXPECT_EQ(cricket::CS_RUNNING, capturer->Start(format));
    630     EXPECT_TRUE(channel_->AddSendStream(
    631         cricket::StreamParams::CreateLegacy(5678)));
    632     EXPECT_TRUE(channel_->SetCapturer(5678, capturer.get()));
    633     EXPECT_TRUE(channel_->AddRecvStream(
    634         cricket::StreamParams::CreateLegacy(5678)));
    635     EXPECT_TRUE(channel_->SetRenderer(5678, &renderer2));
    636     EXPECT_TRUE(capturer->CaptureCustomFrame(
    637         kTestWidth, kTestHeight, cricket::FOURCC_I420));
    638     EXPECT_FRAME_ON_RENDERER_WAIT(
    639         renderer2, 1, kTestWidth, kTestHeight, kTimeout);
    640 
    641     // Get stats, and make sure they are correct for two senders. We wait until
    642     // the number of expected packets have been sent to avoid races where we
    643     // check stats before it has been updated.
    644     cricket::VideoMediaInfo info;
    645     for (uint32_t i = 0; i < kTimeout; ++i) {
    646       rtc::Thread::Current()->ProcessMessages(1);
    647       EXPECT_TRUE(channel_->GetStats(&info));
    648       ASSERT_EQ(2U, info.senders.size());
    649       if (info.senders[0].packets_sent + info.senders[1].packets_sent ==
    650           NumRtpPackets()) {
    651         // Stats have been updated for both sent frames, expectations can be
    652         // checked now.
    653         break;
    654       }
    655     }
    656     EXPECT_EQ(NumRtpPackets(),
    657               info.senders[0].packets_sent + info.senders[1].packets_sent)
    658         << "Timed out while waiting for packet counts for all sent packets.";
    659     EXPECT_EQ(1U, info.senders[0].ssrcs().size());
    660     EXPECT_EQ(1234U, info.senders[0].ssrcs()[0]);
    661     EXPECT_EQ(DefaultCodec().width, info.senders[0].send_frame_width);
    662     EXPECT_EQ(DefaultCodec().height, info.senders[0].send_frame_height);
    663     EXPECT_EQ(1U, info.senders[1].ssrcs().size());
    664     EXPECT_EQ(5678U, info.senders[1].ssrcs()[0]);
    665     EXPECT_EQ(kTestWidth, info.senders[1].send_frame_width);
    666     EXPECT_EQ(kTestHeight, info.senders[1].send_frame_height);
    667     // The capturer must be unregistered here as it runs out of it's scope next.
    668     EXPECT_TRUE(channel_->SetCapturer(5678, NULL));
    669   }
    670 
    671   // Test that we can set the bandwidth.
    672   void SetSendBandwidth() {
    673     cricket::VideoSendParameters parameters;
    674     parameters.codecs.push_back(DefaultCodec());
    675     parameters.max_bandwidth_bps = -1;  // <= 0 means unlimited.
    676     EXPECT_TRUE(channel_->SetSendParameters(parameters));
    677     parameters.max_bandwidth_bps = 128 * 1024;
    678     EXPECT_TRUE(channel_->SetSendParameters(parameters));
    679   }
    680   // Test that we can set the SSRC for the default send source.
    681   void SetSendSsrc() {
    682     EXPECT_TRUE(SetDefaultCodec());
    683     EXPECT_TRUE(SetSendStreamFormat(kSsrc, DefaultCodec()));
    684     EXPECT_TRUE(SetSend(true));
    685     EXPECT_TRUE(SendFrame());
    686     EXPECT_TRUE_WAIT(NumRtpPackets() > 0, kTimeout);
    687     uint32_t ssrc = 0;
    688     rtc::scoped_ptr<const rtc::Buffer> p(GetRtpPacket(0));
    689     ParseRtpPacket(p.get(), NULL, NULL, NULL, NULL, &ssrc, NULL);
    690     EXPECT_EQ(kSsrc, ssrc);
    691     // Packets are being paced out, so these can mismatch between the first and
    692     // second call to NumRtpPackets until pending packets are paced out.
    693     EXPECT_EQ_WAIT(NumRtpPackets(), NumRtpPackets(ssrc), kTimeout);
    694     EXPECT_EQ_WAIT(NumRtpBytes(), NumRtpBytes(ssrc), kTimeout);
    695     EXPECT_EQ(1, NumSentSsrcs());
    696     EXPECT_EQ(0, NumRtpPackets(kSsrc - 1));
    697     EXPECT_EQ(0, NumRtpBytes(kSsrc - 1));
    698   }
    699   // Test that we can set the SSRC even after codecs are set.
    700   void SetSendSsrcAfterSetCodecs() {
    701     // Remove stream added in Setup.
    702     EXPECT_TRUE(channel_->RemoveSendStream(kSsrc));
    703     EXPECT_TRUE(SetDefaultCodec());
    704     EXPECT_TRUE(channel_->AddSendStream(
    705         cricket::StreamParams::CreateLegacy(999)));
    706     EXPECT_TRUE(channel_->SetCapturer(999u, video_capturer_.get()));
    707     EXPECT_TRUE(SetSendStreamFormat(999u, DefaultCodec()));
    708     EXPECT_TRUE(SetSend(true));
    709     EXPECT_TRUE(WaitAndSendFrame(0));
    710     EXPECT_TRUE_WAIT(NumRtpPackets() > 0, kTimeout);
    711     uint32_t ssrc = 0;
    712     rtc::scoped_ptr<const rtc::Buffer> p(GetRtpPacket(0));
    713     ParseRtpPacket(p.get(), NULL, NULL, NULL, NULL, &ssrc, NULL);
    714     EXPECT_EQ(999u, ssrc);
    715     // Packets are being paced out, so these can mismatch between the first and
    716     // second call to NumRtpPackets until pending packets are paced out.
    717     EXPECT_EQ_WAIT(NumRtpPackets(), NumRtpPackets(ssrc), kTimeout);
    718     EXPECT_EQ_WAIT(NumRtpBytes(), NumRtpBytes(ssrc), kTimeout);
    719     EXPECT_EQ(1, NumSentSsrcs());
    720     EXPECT_EQ(0, NumRtpPackets(kSsrc));
    721     EXPECT_EQ(0, NumRtpBytes(kSsrc));
    722   }
    723   // Test that we can set the default video renderer before and after
    724   // media is received.
    725   void SetRenderer() {
    726     uint8_t data1[] = {
    727         0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    728 
    729     rtc::Buffer packet1(data1, sizeof(data1));
    730     rtc::SetBE32(packet1.data() + 8, kSsrc);
    731     channel_->SetRenderer(kDefaultReceiveSsrc, NULL);
    732     EXPECT_TRUE(SetDefaultCodec());
    733     EXPECT_TRUE(SetSend(true));
    734     EXPECT_EQ(0, renderer_.num_rendered_frames());
    735     channel_->OnPacketReceived(&packet1, rtc::PacketTime());
    736     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
    737     EXPECT_TRUE(SendFrame());
    738     EXPECT_FRAME_WAIT(1, DefaultCodec().width, DefaultCodec().height, kTimeout);
    739   }
    740 
    741   // Tests empty StreamParams is rejected.
    742   void RejectEmptyStreamParams() {
    743     // Remove the send stream that was added during Setup.
    744     EXPECT_TRUE(channel_->RemoveSendStream(kSsrc));
    745 
    746     cricket::StreamParams empty;
    747     EXPECT_FALSE(channel_->AddSendStream(empty));
    748     EXPECT_TRUE(channel_->AddSendStream(
    749         cricket::StreamParams::CreateLegacy(789u)));
    750   }
    751 
    752   // Tests setting up and configuring a send stream.
    753   void AddRemoveSendStreams() {
    754     EXPECT_TRUE(SetOneCodec(DefaultCodec()));
    755     EXPECT_TRUE(SetSend(true));
    756     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
    757     EXPECT_TRUE(SendFrame());
    758     EXPECT_FRAME_WAIT(1, DefaultCodec().width, DefaultCodec().height, kTimeout);
    759     EXPECT_GT(NumRtpPackets(), 0);
    760     uint32_t ssrc = 0;
    761     size_t last_packet = NumRtpPackets() - 1;
    762     rtc::scoped_ptr<const rtc::Buffer>
    763         p(GetRtpPacket(static_cast<int>(last_packet)));
    764     ParseRtpPacket(p.get(), NULL, NULL, NULL, NULL, &ssrc, NULL);
    765     EXPECT_EQ(kSsrc, ssrc);
    766 
    767     // Remove the send stream that was added during Setup.
    768     EXPECT_TRUE(channel_->RemoveSendStream(kSsrc));
    769     int rtp_packets = NumRtpPackets();
    770 
    771     EXPECT_TRUE(channel_->AddSendStream(
    772         cricket::StreamParams::CreateLegacy(789u)));
    773     EXPECT_TRUE(channel_->SetCapturer(789u, video_capturer_.get()));
    774     EXPECT_EQ(rtp_packets, NumRtpPackets());
    775     // Wait 30ms to guarantee the engine does not drop the frame.
    776     EXPECT_TRUE(WaitAndSendFrame(30));
    777     EXPECT_TRUE_WAIT(NumRtpPackets() > rtp_packets, kTimeout);
    778 
    779     last_packet = NumRtpPackets() - 1;
    780     p.reset(GetRtpPacket(static_cast<int>(last_packet)));
    781     ParseRtpPacket(p.get(), NULL, NULL, NULL, NULL, &ssrc, NULL);
    782     EXPECT_EQ(789u, ssrc);
    783   }
    784 
    785   // Tests setting up and configuring multiple incoming streams.
    786   void AddRemoveRecvStreams() {
    787     cricket::FakeVideoRenderer renderer1, renderer2;
    788     cricket::VideoSendParameters parameters;
    789     parameters.codecs.push_back(DefaultCodec());
    790     EXPECT_TRUE(channel_->SetSendParameters(parameters));
    791 
    792     // Ensure we can't set the renderer on a non-existent stream.
    793     EXPECT_FALSE(channel_->SetRenderer(1, &renderer1));
    794     EXPECT_FALSE(channel_->SetRenderer(2, &renderer2));
    795     cricket::VideoRenderer* renderer;
    796     EXPECT_FALSE(channel_->GetRenderer(1, &renderer));
    797     EXPECT_FALSE(channel_->GetRenderer(2, &renderer));
    798 
    799     // Ensure we can add streams.
    800     EXPECT_TRUE(channel_->AddRecvStream(
    801         cricket::StreamParams::CreateLegacy(1)));
    802     EXPECT_TRUE(channel_->AddRecvStream(
    803         cricket::StreamParams::CreateLegacy(2)));
    804     EXPECT_TRUE(channel_->GetRenderer(1, &renderer));
    805     EXPECT_TRUE(renderer == NULL);
    806     EXPECT_TRUE(channel_->GetRenderer(2, &renderer));
    807     EXPECT_TRUE(NULL == renderer);
    808 
    809     // Ensure we can now set the renderers.
    810     EXPECT_TRUE(channel_->SetRenderer(1, &renderer1));
    811     EXPECT_TRUE(channel_->SetRenderer(2, &renderer2));
    812     EXPECT_TRUE(channel_->GetRenderer(1, &renderer));
    813     EXPECT_TRUE(&renderer1 == renderer);
    814     EXPECT_TRUE(channel_->GetRenderer(2, &renderer));
    815     EXPECT_TRUE(&renderer2 == renderer);
    816 
    817     // Ensure we can change the renderers if needed.
    818     EXPECT_TRUE(channel_->SetRenderer(1, &renderer2));
    819     EXPECT_TRUE(channel_->SetRenderer(2, &renderer1));
    820     EXPECT_TRUE(channel_->GetRenderer(1, &renderer));
    821     EXPECT_TRUE(&renderer2 == renderer);
    822     EXPECT_TRUE(channel_->GetRenderer(2, &renderer));
    823     EXPECT_TRUE(&renderer1 == renderer);
    824 
    825     EXPECT_TRUE(channel_->RemoveRecvStream(2));
    826     EXPECT_TRUE(channel_->RemoveRecvStream(1));
    827     EXPECT_FALSE(channel_->GetRenderer(1, &renderer));
    828     EXPECT_FALSE(channel_->GetRenderer(2, &renderer));
    829   }
    830 
    831   // Tests setting up and configuring multiple incoming streams in a
    832   // non-conference call.
    833   void AddRemoveRecvStreamsNoConference() {
    834     cricket::FakeVideoRenderer renderer1, renderer2;
    835     // Ensure we can't set the renderer on a non-existent stream.
    836     EXPECT_FALSE(channel_->SetRenderer(1, &renderer1));
    837     EXPECT_FALSE(channel_->SetRenderer(2, &renderer2));
    838     cricket::VideoRenderer* renderer;
    839     EXPECT_FALSE(channel_->GetRenderer(1, &renderer));
    840     EXPECT_FALSE(channel_->GetRenderer(2, &renderer));
    841 
    842     // Ensure we can add streams.
    843     EXPECT_TRUE(channel_->AddRecvStream(
    844         cricket::StreamParams::CreateLegacy(1)));
    845     EXPECT_TRUE(channel_->AddRecvStream(
    846         cricket::StreamParams::CreateLegacy(2)));
    847     EXPECT_TRUE(channel_->GetRenderer(1, &renderer));
    848     // Verify the first AddRecvStream hook up to the default renderer.
    849     EXPECT_TRUE(renderer == NULL);
    850     EXPECT_TRUE(channel_->GetRenderer(2, &renderer));
    851     EXPECT_TRUE(NULL == renderer);
    852 
    853     // Ensure we can now set the renderers.
    854     EXPECT_TRUE(channel_->SetRenderer(1, &renderer1));
    855     EXPECT_TRUE(channel_->SetRenderer(2, &renderer2));
    856     EXPECT_TRUE(channel_->GetRenderer(1, &renderer));
    857     EXPECT_TRUE(&renderer1 == renderer);
    858     EXPECT_TRUE(channel_->GetRenderer(2, &renderer));
    859     EXPECT_TRUE(&renderer2 == renderer);
    860 
    861     // Ensure we can change the renderers if needed.
    862     EXPECT_TRUE(channel_->SetRenderer(1, &renderer2));
    863     EXPECT_TRUE(channel_->SetRenderer(2, &renderer1));
    864     EXPECT_TRUE(channel_->GetRenderer(1, &renderer));
    865     EXPECT_TRUE(&renderer2 == renderer);
    866     EXPECT_TRUE(channel_->GetRenderer(2, &renderer));
    867     EXPECT_TRUE(&renderer1 == renderer);
    868 
    869     EXPECT_TRUE(channel_->RemoveRecvStream(2));
    870     EXPECT_TRUE(channel_->RemoveRecvStream(1));
    871     EXPECT_FALSE(channel_->GetRenderer(1, &renderer));
    872     EXPECT_FALSE(channel_->GetRenderer(2, &renderer));
    873   }
    874 
    875   // Test that no frames are rendered after the receive stream have been
    876   // removed.
    877   void AddRemoveRecvStreamAndRender() {
    878     cricket::FakeVideoRenderer renderer1;
    879     EXPECT_TRUE(SetDefaultCodec());
    880     EXPECT_TRUE(SetSend(true));
    881     EXPECT_TRUE(channel_->AddRecvStream(
    882         cricket::StreamParams::CreateLegacy(kSsrc)));
    883     EXPECT_TRUE(channel_->SetRenderer(kSsrc, &renderer1));
    884 
    885     EXPECT_TRUE(SendFrame());
    886     EXPECT_FRAME_ON_RENDERER_WAIT(
    887         renderer1, 1, DefaultCodec().width, DefaultCodec().height, kTimeout);
    888     EXPECT_TRUE(channel_->RemoveRecvStream(kSsrc));
    889     // Send three more frames. This is to avoid that the test might be flaky
    890     // due to frame dropping.
    891     for (size_t i = 0; i < 3; ++i)
    892       EXPECT_TRUE(WaitAndSendFrame(100));
    893 
    894     // Test that no more frames have been rendered.
    895     EXPECT_EQ(1, renderer1.num_rendered_frames());
    896 
    897     // Re-add the stream again and make sure it renders.
    898     EXPECT_TRUE(channel_->AddRecvStream(
    899         cricket::StreamParams::CreateLegacy(kSsrc)));
    900     // Force the next frame to be a key frame to make the receiving
    901     // decoder happy.
    902     EXPECT_TRUE(channel_->SendIntraFrame());
    903 
    904     EXPECT_TRUE(channel_->SetRenderer(kSsrc, &renderer1));
    905     EXPECT_TRUE(SendFrame());
    906     // Because the default channel is used, RemoveRecvStream above is not going
    907     // to delete the channel. As a result the engine will continue to receive
    908     // and decode the 3 frames sent above. So it is possible we will receive
    909     // some (e.g. 1) of these 3 frames after the renderer is set again.
    910     EXPECT_GT_FRAME_ON_RENDERER_WAIT(
    911         renderer1, 2, DefaultCodec().width, DefaultCodec().height, kTimeout);
    912     // Detach |renderer1| before exit as there might be frames come late.
    913     EXPECT_TRUE(channel_->SetRenderer(kSsrc, NULL));
    914   }
    915 
    916   // Tests the behavior of incoming streams in a conference scenario.
    917   void SimulateConference() {
    918     cricket::FakeVideoRenderer renderer1, renderer2;
    919     EXPECT_TRUE(SetDefaultCodec());
    920     cricket::VideoSendParameters parameters;
    921     parameters.codecs.push_back(DefaultCodec());
    922     parameters.options.conference_mode = rtc::Optional<bool>(true);
    923     EXPECT_TRUE(channel_->SetSendParameters(parameters));
    924     EXPECT_TRUE(SetSend(true));
    925     EXPECT_TRUE(channel_->AddRecvStream(
    926         cricket::StreamParams::CreateLegacy(1)));
    927     EXPECT_TRUE(channel_->AddRecvStream(
    928         cricket::StreamParams::CreateLegacy(2)));
    929     EXPECT_TRUE(channel_->SetRenderer(1, &renderer1));
    930     EXPECT_TRUE(channel_->SetRenderer(2, &renderer2));
    931     EXPECT_EQ(0, renderer1.num_rendered_frames());
    932     EXPECT_EQ(0, renderer2.num_rendered_frames());
    933     std::vector<uint32_t> ssrcs;
    934     ssrcs.push_back(1);
    935     ssrcs.push_back(2);
    936     network_interface_.SetConferenceMode(true, ssrcs);
    937     EXPECT_TRUE(SendFrame());
    938     EXPECT_FRAME_ON_RENDERER_WAIT(
    939         renderer1, 1, DefaultCodec().width, DefaultCodec().height, kTimeout);
    940     EXPECT_FRAME_ON_RENDERER_WAIT(
    941         renderer2, 1, DefaultCodec().width, DefaultCodec().height, kTimeout);
    942 
    943     rtc::scoped_ptr<const rtc::Buffer> p(GetRtpPacket(0));
    944     EXPECT_EQ(DefaultCodec().id, GetPayloadType(p.get()));
    945     EXPECT_EQ(DefaultCodec().width, renderer1.width());
    946     EXPECT_EQ(DefaultCodec().height, renderer1.height());
    947     EXPECT_EQ(DefaultCodec().width, renderer2.width());
    948     EXPECT_EQ(DefaultCodec().height, renderer2.height());
    949     EXPECT_TRUE(channel_->RemoveRecvStream(2));
    950     EXPECT_TRUE(channel_->RemoveRecvStream(1));
    951   }
    952 
    953   // Tests that we can add and remove capturers and frames are sent out properly
    954   void AddRemoveCapturer() {
    955     cricket::VideoCodec codec = DefaultCodec();
    956     codec.width = 320;
    957     codec.height = 240;
    958     const int time_between_send = TimeBetweenSend(codec);
    959     EXPECT_TRUE(SetOneCodec(codec));
    960     EXPECT_TRUE(SetSend(true));
    961     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
    962     EXPECT_EQ(0, renderer_.num_rendered_frames());
    963     EXPECT_TRUE(SendFrame());
    964     EXPECT_FRAME_WAIT(1, codec.width, codec.height, kTimeout);
    965     rtc::scoped_ptr<cricket::FakeVideoCapturer> capturer(
    966         CreateFakeVideoCapturer());
    967     capturer->SetScreencast(true);
    968     cricket::VideoFormat format(480, 360,
    969                                 cricket::VideoFormat::FpsToInterval(30),
    970                                 cricket::FOURCC_I420);
    971     EXPECT_EQ(cricket::CS_RUNNING, capturer->Start(format));
    972     // All capturers start generating frames with the same timestamp. ViE does
    973     // not allow the same timestamp to be used. Capture one frame before
    974     // associating the capturer with the channel.
    975     EXPECT_TRUE(capturer->CaptureCustomFrame(format.width, format.height,
    976                                              cricket::FOURCC_I420));
    977 
    978     int captured_frames = 1;
    979     for (int iterations = 0; iterations < 2; ++iterations) {
    980       EXPECT_TRUE(channel_->SetCapturer(kSsrc, capturer.get()));
    981       rtc::Thread::Current()->ProcessMessages(time_between_send);
    982       EXPECT_TRUE(capturer->CaptureCustomFrame(format.width, format.height,
    983                                                cricket::FOURCC_I420));
    984       ++captured_frames;
    985       // Wait until frame of right size is captured.
    986       EXPECT_TRUE_WAIT(renderer_.num_rendered_frames() >= captured_frames &&
    987                        format.width == renderer_.width() &&
    988                        format.height == renderer_.height() &&
    989                        !renderer_.black_frame(), kTimeout);
    990       EXPECT_GE(renderer_.num_rendered_frames(), captured_frames);
    991       EXPECT_EQ(format.width, renderer_.width());
    992       EXPECT_EQ(format.height, renderer_.height());
    993       captured_frames = renderer_.num_rendered_frames() + 1;
    994       EXPECT_FALSE(renderer_.black_frame());
    995       EXPECT_TRUE(channel_->SetCapturer(kSsrc, NULL));
    996       // Make sure a black frame is generated within the specified timeout.
    997       // The black frame should be the resolution of the previous frame to
    998       // prevent expensive encoder reconfigurations.
    999       EXPECT_TRUE_WAIT(renderer_.num_rendered_frames() >= captured_frames &&
   1000                        format.width == renderer_.width() &&
   1001                        format.height == renderer_.height() &&
   1002                        renderer_.black_frame(), kTimeout);
   1003       EXPECT_GE(renderer_.num_rendered_frames(), captured_frames);
   1004       EXPECT_EQ(format.width, renderer_.width());
   1005       EXPECT_EQ(format.height, renderer_.height());
   1006       EXPECT_TRUE(renderer_.black_frame());
   1007 
   1008       // The black frame has the same timestamp as the next frame since it's
   1009       // timestamp is set to the last frame's timestamp + interval. WebRTC will
   1010       // not render a frame with the same timestamp so capture another frame
   1011       // with the frame capturer to increment the next frame's timestamp.
   1012       EXPECT_TRUE(capturer->CaptureCustomFrame(format.width, format.height,
   1013                                                cricket::FOURCC_I420));
   1014     }
   1015   }
   1016 
   1017   // Tests that if RemoveCapturer is called without a capturer ever being
   1018   // added, the plugin shouldn't crash (and no black frame should be sent).
   1019   void RemoveCapturerWithoutAdd() {
   1020     EXPECT_TRUE(SetOneCodec(DefaultCodec()));
   1021     EXPECT_TRUE(SetSend(true));
   1022     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
   1023     EXPECT_EQ(0, renderer_.num_rendered_frames());
   1024     EXPECT_TRUE(SendFrame());
   1025     EXPECT_FRAME_WAIT(1, 640, 400, kTimeout);
   1026     // Wait for one frame so they don't get dropped because we send frames too
   1027     // tightly.
   1028     rtc::Thread::Current()->ProcessMessages(30);
   1029     // Remove the capturer.
   1030     EXPECT_TRUE(channel_->SetCapturer(kSsrc, NULL));
   1031     // Wait for one black frame for removing the capturer.
   1032     EXPECT_FRAME_WAIT(2, 640, 400, kTimeout);
   1033 
   1034     // No capturer was added, so this RemoveCapturer should
   1035     // fail.
   1036     EXPECT_FALSE(channel_->SetCapturer(kSsrc, NULL));
   1037     rtc::Thread::Current()->ProcessMessages(300);
   1038     // Verify no more frames were sent.
   1039     EXPECT_EQ(2, renderer_.num_rendered_frames());
   1040   }
   1041 
   1042   // Tests that we can add and remove capturer as unique sources.
   1043   void AddRemoveCapturerMultipleSources() {
   1044     // WebRTC implementation will drop frames if pushed to quickly. Wait the
   1045     // interval time to avoid that.
   1046     // WebRTC implementation will drop frames if pushed to quickly. Wait the
   1047     // interval time to avoid that.
   1048     // Set up the stream associated with the engine.
   1049     EXPECT_TRUE(channel_->AddRecvStream(
   1050         cricket::StreamParams::CreateLegacy(kSsrc)));
   1051     EXPECT_TRUE(channel_->SetRenderer(kSsrc, &renderer_));
   1052     cricket::VideoFormat capture_format;  // default format
   1053     capture_format.interval = cricket::VideoFormat::FpsToInterval(30);
   1054     // Set up additional stream 1.
   1055     cricket::FakeVideoRenderer renderer1;
   1056     EXPECT_FALSE(channel_->SetRenderer(1, &renderer1));
   1057     EXPECT_TRUE(channel_->AddRecvStream(
   1058         cricket::StreamParams::CreateLegacy(1)));
   1059     EXPECT_TRUE(channel_->SetRenderer(1, &renderer1));
   1060     EXPECT_TRUE(channel_->AddSendStream(
   1061         cricket::StreamParams::CreateLegacy(1)));
   1062     rtc::scoped_ptr<cricket::FakeVideoCapturer> capturer1(
   1063         CreateFakeVideoCapturer());
   1064     capturer1->SetScreencast(true);
   1065     EXPECT_EQ(cricket::CS_RUNNING, capturer1->Start(capture_format));
   1066     // Set up additional stream 2.
   1067     cricket::FakeVideoRenderer renderer2;
   1068     EXPECT_FALSE(channel_->SetRenderer(2, &renderer2));
   1069     EXPECT_TRUE(channel_->AddRecvStream(
   1070         cricket::StreamParams::CreateLegacy(2)));
   1071     EXPECT_TRUE(channel_->SetRenderer(2, &renderer2));
   1072     EXPECT_TRUE(channel_->AddSendStream(
   1073         cricket::StreamParams::CreateLegacy(2)));
   1074     rtc::scoped_ptr<cricket::FakeVideoCapturer> capturer2(
   1075         CreateFakeVideoCapturer());
   1076     capturer2->SetScreencast(true);
   1077     EXPECT_EQ(cricket::CS_RUNNING, capturer2->Start(capture_format));
   1078     // State for all the streams.
   1079     EXPECT_TRUE(SetOneCodec(DefaultCodec()));
   1080     // A limitation in the lmi implementation requires that SetCapturer() is
   1081     // called after SetOneCodec().
   1082     // TODO(hellner): this seems like an unnecessary constraint, fix it.
   1083     EXPECT_TRUE(channel_->SetCapturer(1, capturer1.get()));
   1084     EXPECT_TRUE(channel_->SetCapturer(2, capturer2.get()));
   1085     EXPECT_TRUE(SetSend(true));
   1086     // Test capturer associated with engine.
   1087     const int kTestWidth = 160;
   1088     const int kTestHeight = 120;
   1089     EXPECT_TRUE(capturer1->CaptureCustomFrame(
   1090         kTestWidth, kTestHeight, cricket::FOURCC_I420));
   1091     EXPECT_FRAME_ON_RENDERER_WAIT(
   1092         renderer1, 1, kTestWidth, kTestHeight, kTimeout);
   1093     // Capture a frame with additional capturer2, frames should be received
   1094     EXPECT_TRUE(capturer2->CaptureCustomFrame(
   1095         kTestWidth, kTestHeight, cricket::FOURCC_I420));
   1096     EXPECT_FRAME_ON_RENDERER_WAIT(
   1097         renderer2, 1, kTestWidth, kTestHeight, kTimeout);
   1098     // Successfully remove the capturer.
   1099     EXPECT_TRUE(channel_->SetCapturer(kSsrc, NULL));
   1100     // Fail to re-remove the capturer.
   1101     EXPECT_FALSE(channel_->SetCapturer(kSsrc, NULL));
   1102     // The capturers must be unregistered here as it runs out of it's scope
   1103     // next.
   1104     EXPECT_TRUE(channel_->SetCapturer(1, NULL));
   1105     EXPECT_TRUE(channel_->SetCapturer(2, NULL));
   1106   }
   1107 
   1108   void HighAspectHighHeightCapturer() {
   1109     const int kWidth  = 80;
   1110     const int kHeight = 10000;
   1111     const int kScaledWidth = 20;
   1112     const int kScaledHeight = 2500;
   1113 
   1114     cricket::VideoCodec codec(DefaultCodec());
   1115     EXPECT_TRUE(SetOneCodec(codec));
   1116     EXPECT_TRUE(SetSend(true));
   1117 
   1118     cricket::FakeVideoRenderer renderer;
   1119     EXPECT_TRUE(channel_->AddRecvStream(
   1120         cricket::StreamParams::CreateLegacy(kSsrc)));
   1121     EXPECT_TRUE(channel_->SetRenderer(kSsrc, &renderer));
   1122     EXPECT_EQ(0, renderer.num_rendered_frames());
   1123 
   1124     EXPECT_TRUE(SendFrame());
   1125     EXPECT_GT_FRAME_ON_RENDERER_WAIT(
   1126         renderer, 1, codec.width, codec.height, kTimeout);
   1127 
   1128     // Registering an external capturer is currently the same as screen casting
   1129     // (update the test when this changes).
   1130     rtc::scoped_ptr<cricket::FakeVideoCapturer> capturer(
   1131         CreateFakeVideoCapturer());
   1132     capturer->SetScreencast(true);
   1133     const std::vector<cricket::VideoFormat>* formats =
   1134         capturer->GetSupportedFormats();
   1135     cricket::VideoFormat capture_format = (*formats)[0];
   1136     EXPECT_EQ(cricket::CS_RUNNING, capturer->Start(capture_format));
   1137     // Capture frame to not get same frame timestamps as previous capturer.
   1138     capturer->CaptureFrame();
   1139     EXPECT_TRUE(channel_->SetCapturer(kSsrc, capturer.get()));
   1140     EXPECT_TRUE(rtc::Thread::Current()->ProcessMessages(30));
   1141     EXPECT_TRUE(capturer->CaptureCustomFrame(kWidth, kHeight,
   1142                                              cricket::FOURCC_ARGB));
   1143     EXPECT_GT_FRAME_ON_RENDERER_WAIT(
   1144         renderer, 2, kScaledWidth, kScaledHeight, kTimeout);
   1145     EXPECT_TRUE(channel_->SetCapturer(kSsrc, NULL));
   1146   }
   1147 
   1148   // Tests that we can adapt video resolution with 16:10 aspect ratio properly.
   1149   void AdaptResolution16x10() {
   1150     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
   1151     cricket::VideoCodec codec(DefaultCodec());
   1152     codec.width = 640;
   1153     codec.height = 400;
   1154     SendAndReceive(codec);
   1155     codec.width /= 2;
   1156     codec.height /= 2;
   1157     // Adapt the resolution.
   1158     EXPECT_TRUE(SetOneCodec(codec));
   1159     EXPECT_TRUE(WaitAndSendFrame(30));
   1160     EXPECT_FRAME_WAIT(2, codec.width, codec.height, kTimeout);
   1161   }
   1162   // Tests that we can adapt video resolution with 4:3 aspect ratio properly.
   1163   void AdaptResolution4x3() {
   1164     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
   1165     cricket::VideoCodec codec(DefaultCodec());
   1166     codec.width = 640;
   1167     codec.height = 400;
   1168     SendAndReceive(codec);
   1169     codec.width /= 2;
   1170     codec.height /= 2;
   1171     // Adapt the resolution.
   1172     EXPECT_TRUE(SetOneCodec(codec));
   1173     EXPECT_TRUE(WaitAndSendFrame(30));
   1174     EXPECT_FRAME_WAIT(2, codec.width, codec.height, kTimeout);
   1175   }
   1176   // Tests that we can drop all frames properly.
   1177   void AdaptDropAllFrames() {
   1178     // Set the channel codec's resolution to 0, which will require the adapter
   1179     // to drop all frames.
   1180     cricket::VideoCodec codec(DefaultCodec());
   1181     codec.width = codec.height = codec.framerate = 0;
   1182     EXPECT_TRUE(SetOneCodec(codec));
   1183     EXPECT_TRUE(SetSend(true));
   1184     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
   1185     EXPECT_EQ(0, renderer_.num_rendered_frames());
   1186     EXPECT_TRUE(SendFrame());
   1187     EXPECT_TRUE(SendFrame());
   1188     rtc::Thread::Current()->ProcessMessages(500);
   1189     EXPECT_EQ(0, renderer_.num_rendered_frames());
   1190   }
   1191   // Tests that we can reduce the frame rate on demand properly.
   1192   // TODO(fbarchard): This test is flakey on pulse.  Fix and re-enable
   1193   void AdaptFramerate() {
   1194     cricket::VideoCodec codec(DefaultCodec());
   1195     int frame_count = 0;
   1196     // The capturer runs at 30 fps. The channel requires 30 fps.
   1197     EXPECT_TRUE(SetOneCodec(codec));
   1198     EXPECT_TRUE(SetSend(true));
   1199     EXPECT_EQ(frame_count, renderer_.num_rendered_frames());
   1200     EXPECT_TRUE(WaitAndSendFrame(0));  // Should be rendered.
   1201     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be rendered.
   1202     frame_count += 2;
   1203     EXPECT_FRAME_WAIT(frame_count, codec.width, codec.height, kTimeout);
   1204     rtc::scoped_ptr<const rtc::Buffer> p(GetRtpPacket(0));
   1205     EXPECT_EQ(codec.id, GetPayloadType(p.get()));
   1206 
   1207     // The channel requires 15 fps.
   1208     codec.framerate = 15;
   1209     EXPECT_TRUE(SetOneCodec(codec));
   1210     EXPECT_TRUE(WaitAndSendFrame(0));  // Should be rendered.
   1211     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be dropped.
   1212     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be rendered.
   1213     frame_count += 2;
   1214     EXPECT_EQ_WAIT(frame_count, renderer_.num_rendered_frames(), kTimeout);
   1215 
   1216     // The channel requires 10 fps.
   1217     codec.framerate = 10;
   1218     EXPECT_TRUE(SetOneCodec(codec));
   1219     EXPECT_TRUE(WaitAndSendFrame(0));  // Should be rendered.
   1220     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be dropped.
   1221     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be dropped.
   1222     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be rendered.
   1223     frame_count += 2;
   1224     EXPECT_EQ_WAIT(frame_count, renderer_.num_rendered_frames(), kTimeout);
   1225 
   1226     // The channel requires 8 fps. The adapter adapts to 10 fps, which is the
   1227     // closest factor of 30.
   1228     codec.framerate = 8;
   1229     EXPECT_TRUE(SetOneCodec(codec));
   1230     EXPECT_TRUE(WaitAndSendFrame(0));  // Should be rendered.
   1231     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be dropped.
   1232     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be dropped.
   1233     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be rendered.
   1234     frame_count += 2;
   1235     EXPECT_EQ_WAIT(frame_count, renderer_.num_rendered_frames(), kTimeout);
   1236   }
   1237   // Tests that adapted frames won't be upscaled to a higher resolution.
   1238   void SendsLowerResolutionOnSmallerFrames() {
   1239     cricket::VideoCodec codec = DefaultCodec();
   1240     codec.width = 320;
   1241     codec.height = 240;
   1242     EXPECT_TRUE(SetOneCodec(codec));
   1243     EXPECT_TRUE(SetSend(true));
   1244     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
   1245     EXPECT_EQ(0, renderer_.num_rendered_frames());
   1246     EXPECT_TRUE(SendFrame());
   1247     EXPECT_FRAME_WAIT(1, codec.width, codec.height, kTimeout);
   1248 
   1249     // Check that we send smaller frames at the new resolution.
   1250     EXPECT_TRUE(rtc::Thread::Current()->ProcessMessages(33));
   1251     EXPECT_TRUE(video_capturer_->CaptureCustomFrame(
   1252         codec.width / 2, codec.height / 2, cricket::FOURCC_I420));
   1253     EXPECT_FRAME_WAIT(2, codec.width / 2, codec.height / 2, kTimeout);
   1254   }
   1255   // Tests that we can set the send stream format properly.
   1256   void SetSendStreamFormat() {
   1257     cricket::VideoCodec codec(DefaultCodec());
   1258     SendAndReceive(codec);
   1259     int frame_count = 1;
   1260     EXPECT_FRAME_WAIT(frame_count, codec.width, codec.height, kTimeout);
   1261 
   1262     // Adapt the resolution and frame rate to half.
   1263     cricket::VideoFormat format(
   1264         codec.width / 2,
   1265         codec.height / 2,
   1266         cricket::VideoFormat::FpsToInterval(codec.framerate / 2),
   1267         cricket::FOURCC_I420);
   1268     // The SSRC differs from the send SSRC.
   1269     EXPECT_FALSE(channel_->SetSendStreamFormat(kSsrc - 1, format));
   1270     EXPECT_TRUE(channel_->SetSendStreamFormat(kSsrc, format));
   1271 
   1272     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be dropped.
   1273     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be rendered.
   1274     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be dropped.
   1275     frame_count += 1;
   1276     EXPECT_FRAME_WAIT(frame_count, format.width, format.height, kTimeout);
   1277 
   1278     // Adapt the resolution to 0x0, which should drop all frames.
   1279     format.width = 0;
   1280     format.height = 0;
   1281     EXPECT_TRUE(channel_->SetSendStreamFormat(kSsrc, format));
   1282     EXPECT_TRUE(SendFrame());
   1283     EXPECT_TRUE(SendFrame());
   1284     rtc::Thread::Current()->ProcessMessages(500);
   1285     EXPECT_EQ(frame_count, renderer_.num_rendered_frames());
   1286   }
   1287   // Test that setting send stream format to 0x0 resolution will result in
   1288   // frames being dropped.
   1289   void SetSendStreamFormat0x0() {
   1290     EXPECT_TRUE(SetOneCodec(DefaultCodec()));
   1291     EXPECT_TRUE(SetSendStreamFormat(kSsrc, DefaultCodec()));
   1292     EXPECT_TRUE(SetSend(true));
   1293     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
   1294     EXPECT_EQ(0, renderer_.num_rendered_frames());
   1295     // This frame should be received.
   1296     EXPECT_TRUE(SendFrame());
   1297     EXPECT_FRAME_WAIT(1, DefaultCodec().width, DefaultCodec().height, kTimeout);
   1298     const int64_t interval =
   1299         cricket::VideoFormat::FpsToInterval(DefaultCodec().framerate);
   1300     cricket::VideoFormat format(
   1301         0,
   1302         0,
   1303         interval,
   1304         cricket::FOURCC_I420);
   1305     EXPECT_TRUE(channel_->SetSendStreamFormat(kSsrc, format));
   1306     // This frame should not be received.
   1307     EXPECT_TRUE(WaitAndSendFrame(
   1308         static_cast<int>(interval/rtc::kNumNanosecsPerMillisec)));
   1309     rtc::Thread::Current()->ProcessMessages(500);
   1310     EXPECT_EQ(1, renderer_.num_rendered_frames());
   1311   }
   1312 
   1313   // Tests that we can mute and unmute the channel properly.
   1314   void MuteStream() {
   1315     EXPECT_TRUE(SetDefaultCodec());
   1316     cricket::FakeVideoCapturer video_capturer;
   1317     video_capturer.Start(
   1318         cricket::VideoFormat(
   1319             640, 480,
   1320             cricket::VideoFormat::FpsToInterval(30),
   1321             cricket::FOURCC_I420));
   1322     EXPECT_TRUE(channel_->SetCapturer(kSsrc, &video_capturer));
   1323     EXPECT_TRUE(SetSend(true));
   1324     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
   1325     EXPECT_EQ(0, renderer_.num_rendered_frames());
   1326     // Mute the channel and expect black output frame.
   1327     int frame_count = 0;
   1328     EXPECT_TRUE(channel_->SetVideoSend(kSsrc, false, nullptr));
   1329     EXPECT_TRUE(video_capturer.CaptureFrame());
   1330     ++frame_count;
   1331     EXPECT_EQ_WAIT(frame_count, renderer_.num_rendered_frames(), kTimeout);
   1332     EXPECT_TRUE(renderer_.black_frame());
   1333     // Unmute the channel and expect non-black output frame.
   1334     EXPECT_TRUE(channel_->SetVideoSend(kSsrc, true, nullptr));
   1335     EXPECT_TRUE(rtc::Thread::Current()->ProcessMessages(30));
   1336     EXPECT_TRUE(video_capturer.CaptureFrame());
   1337     ++frame_count;
   1338     EXPECT_EQ_WAIT(frame_count, renderer_.num_rendered_frames(), kTimeout);
   1339     EXPECT_FALSE(renderer_.black_frame());
   1340     // Test that we can also Mute using the correct send stream SSRC.
   1341     EXPECT_TRUE(channel_->SetVideoSend(kSsrc, false, nullptr));
   1342     EXPECT_TRUE(rtc::Thread::Current()->ProcessMessages(30));
   1343     EXPECT_TRUE(video_capturer.CaptureFrame());
   1344     ++frame_count;
   1345     EXPECT_EQ_WAIT(frame_count, renderer_.num_rendered_frames(), kTimeout);
   1346     EXPECT_TRUE(renderer_.black_frame());
   1347     EXPECT_TRUE(channel_->SetVideoSend(kSsrc, true, nullptr));
   1348     EXPECT_TRUE(rtc::Thread::Current()->ProcessMessages(30));
   1349     EXPECT_TRUE(video_capturer.CaptureFrame());
   1350     ++frame_count;
   1351     EXPECT_EQ_WAIT(frame_count, renderer_.num_rendered_frames(), kTimeout);
   1352     EXPECT_FALSE(renderer_.black_frame());
   1353     // Test that muting an existing stream succeeds even if it's muted.
   1354     EXPECT_TRUE(channel_->SetVideoSend(kSsrc, false, nullptr));
   1355     EXPECT_TRUE(channel_->SetVideoSend(kSsrc, false, nullptr));
   1356     // Test that unmuting an existing stream succeeds even if it's not muted.
   1357     EXPECT_TRUE(channel_->SetVideoSend(kSsrc, true, nullptr));
   1358     EXPECT_TRUE(channel_->SetVideoSend(kSsrc, true, nullptr));
   1359     // Test that muting an invalid stream fails.
   1360     EXPECT_FALSE(channel_->SetVideoSend(kSsrc+1, false, nullptr));
   1361     EXPECT_TRUE(channel_->SetCapturer(kSsrc, NULL));
   1362   }
   1363 
   1364   // Test that multiple send streams can be created and deleted properly.
   1365   void MultipleSendStreams() {
   1366     // Remove stream added in Setup. I.e. remove stream corresponding to default
   1367     // channel.
   1368     EXPECT_TRUE(channel_->RemoveSendStream(kSsrc));
   1369     const unsigned int kSsrcsSize = sizeof(kSsrcs4)/sizeof(kSsrcs4[0]);
   1370     for (unsigned int i = 0; i < kSsrcsSize; ++i) {
   1371       EXPECT_TRUE(channel_->AddSendStream(
   1372           cricket::StreamParams::CreateLegacy(kSsrcs4[i])));
   1373     }
   1374     // Delete one of the non default channel streams, let the destructor delete
   1375     // the remaining ones.
   1376     EXPECT_TRUE(channel_->RemoveSendStream(kSsrcs4[kSsrcsSize - 1]));
   1377     // Stream should already be deleted.
   1378     EXPECT_FALSE(channel_->RemoveSendStream(kSsrcs4[kSsrcsSize - 1]));
   1379   }
   1380 
   1381   // Two streams one channel tests.
   1382 
   1383   // Tests that we can send and receive frames.
   1384   void TwoStreamsSendAndReceive(const cricket::VideoCodec& codec) {
   1385     SetUpSecondStream();
   1386     // Test sending and receiving on first stream.
   1387     SendAndReceive(codec);
   1388     // Test sending and receiving on second stream.
   1389     EXPECT_EQ_WAIT(1, renderer2_.num_rendered_frames(), kTimeout);
   1390     EXPECT_GT(NumRtpPackets(), 0);
   1391     EXPECT_EQ(1, renderer2_.num_rendered_frames());
   1392   }
   1393 
   1394   // Set up 2 streams where the first stream uses the default channel.
   1395   // Then disconnect the first stream and verify default channel becomes
   1396   // available.
   1397   // Then add a new stream with |new_ssrc|. The new stream should re-use the
   1398   // default channel.
   1399   void TwoStreamsReUseFirstStream(const cricket::VideoCodec& codec) {
   1400     SetUpSecondStream();
   1401     // Default channel used by the first stream.
   1402     EXPECT_EQ(kSsrc, channel_->GetDefaultSendChannelSsrc());
   1403     EXPECT_TRUE(channel_->RemoveRecvStream(kSsrc));
   1404     EXPECT_FALSE(channel_->RemoveRecvStream(kSsrc));
   1405     EXPECT_TRUE(channel_->RemoveSendStream(kSsrc));
   1406     EXPECT_FALSE(channel_->RemoveSendStream(kSsrc));
   1407     // Default channel is no longer used by a stream.
   1408     EXPECT_EQ(0u, channel_->GetDefaultSendChannelSsrc());
   1409     uint32_t new_ssrc = kSsrc + 100;
   1410     EXPECT_TRUE(channel_->AddSendStream(
   1411         cricket::StreamParams::CreateLegacy(new_ssrc)));
   1412     // Re-use default channel.
   1413     EXPECT_EQ(new_ssrc, channel_->GetDefaultSendChannelSsrc());
   1414     EXPECT_FALSE(channel_->AddSendStream(
   1415         cricket::StreamParams::CreateLegacy(new_ssrc)));
   1416     EXPECT_TRUE(channel_->AddRecvStream(
   1417         cricket::StreamParams::CreateLegacy(new_ssrc)));
   1418     EXPECT_TRUE(channel_->SetRenderer(new_ssrc, &renderer_));
   1419     EXPECT_FALSE(channel_->AddRecvStream(
   1420         cricket::StreamParams::CreateLegacy(new_ssrc)));
   1421 
   1422     EXPECT_TRUE(channel_->SetCapturer(new_ssrc, video_capturer_.get()));
   1423 
   1424     SendAndReceive(codec);
   1425     EXPECT_TRUE(channel_->RemoveSendStream(new_ssrc));
   1426     EXPECT_EQ(0u, channel_->GetDefaultSendChannelSsrc());
   1427   }
   1428 
   1429   // Tests that we can send and receive frames with early receive.
   1430   void TwoStreamsSendAndUnsignalledRecv(const cricket::VideoCodec& codec) {
   1431     cricket::VideoSendParameters parameters;
   1432     parameters.options.conference_mode = rtc::Optional<bool>(true);
   1433     parameters.options.unsignalled_recv_stream_limit = rtc::Optional<int>(1);
   1434     EXPECT_TRUE(channel_->SetSendParameters(parameters));
   1435     SetUpSecondStreamWithNoRecv();
   1436     // Test sending and receiving on first stream.
   1437     Send(codec);
   1438     EXPECT_EQ_WAIT(2, NumRtpPackets(), kTimeout);
   1439     EXPECT_EQ_WAIT(1, renderer_.num_rendered_frames(), kTimeout);
   1440     // The first send is not expected to yield frames, because the ssrc
   1441     // is not signalled yet. With unsignalled recv enabled, we will drop frames
   1442     // instead of packets.
   1443     EXPECT_EQ(0, renderer2_.num_rendered_frames());
   1444     // Give a chance for the decoder to process before adding the receiver.
   1445     rtc::Thread::Current()->ProcessMessages(100);
   1446     // Test sending and receiving on second stream.
   1447     EXPECT_TRUE(channel_->AddRecvStream(
   1448         cricket::StreamParams::CreateLegacy(kSsrc + 2)));
   1449     EXPECT_TRUE(channel_->SetRenderer(kSsrc + 2, &renderer2_));
   1450     SendFrame();
   1451     EXPECT_EQ_WAIT(2, renderer_.num_rendered_frames(), kTimeout);
   1452     EXPECT_EQ(4, NumRtpPackets());
   1453     // The second send is expected to yield frame as the ssrc is signalled now.
   1454     // Decode should succeed here, though we received the key frame earlier.
   1455     // Without early recv, we would have dropped it and decoding would have
   1456     // failed.
   1457     EXPECT_EQ_WAIT(1, renderer2_.num_rendered_frames(), kTimeout);
   1458   }
   1459 
   1460   // Tests that we drop key frames when conference mode is enabled and we
   1461   // receive rtp packets on unsignalled streams. Removal of a unsignalled recv
   1462   // stream is successful.
   1463   void TwoStreamsAddAndRemoveUnsignalledRecv(
   1464       const cricket::VideoCodec& codec) {
   1465     cricket::VideoOptions vmo;
   1466     vmo.conference_mode = rtc::Optional<bool>(true);
   1467     vmo.unsignalled_recv_stream_limit = rtc::Optional<int>(1);
   1468     EXPECT_TRUE(channel_->SetOptions(vmo));
   1469     SetUpSecondStreamWithNoRecv();
   1470     // Sending and receiving on first stream.
   1471     Send(codec);
   1472     EXPECT_EQ_WAIT(2, NumRtpPackets(), kTimeout);
   1473     EXPECT_EQ_WAIT(1, renderer_.num_rendered_frames(), kTimeout);
   1474     // The first send is not expected to yield frames, because the ssrc
   1475     // is no signalled yet. With unsignalled recv enabled, we will drop frames
   1476     // instead of packets.
   1477     EXPECT_EQ(0, renderer2_.num_rendered_frames());
   1478     // Give a chance for the decoder to process before adding the receiver.
   1479     rtc::Thread::Current()->ProcessMessages(100);
   1480     // Ensure that we can remove the unsignalled recv stream that was created
   1481     // when the first video packet with unsignalled recv ssrc is received.
   1482     EXPECT_TRUE(channel_->RemoveRecvStream(kSsrc + 2));
   1483   }
   1484 
   1485   const rtc::scoped_ptr<webrtc::Call> call_;
   1486   VideoEngineOverride<E> engine_;
   1487   rtc::scoped_ptr<cricket::FakeVideoCapturer> video_capturer_;
   1488   rtc::scoped_ptr<cricket::FakeVideoCapturer> video_capturer_2_;
   1489   rtc::scoped_ptr<C> channel_;
   1490   cricket::FakeNetworkInterface network_interface_;
   1491   cricket::FakeVideoRenderer renderer_;
   1492   cricket::VideoMediaChannel::Error media_error_;
   1493 
   1494   // Used by test cases where 2 streams are run on the same channel.
   1495   cricket::FakeVideoRenderer renderer2_;
   1496 };
   1497 
   1498 #endif  // TALK_MEDIA_BASE_VIDEOENGINE_UNITTEST_H_  NOLINT
   1499