Home | History | Annotate | Download | only in webrtc
      1 /*
      2  * libjingle
      3  * Copyright 2004 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 #include "talk/base/fakecpumonitor.h"
     29 #include "talk/base/gunit.h"
     30 #include "talk/base/logging.h"
     31 #include "talk/base/scoped_ptr.h"
     32 #include "talk/base/stream.h"
     33 #include "talk/media/base/constants.h"
     34 #include "talk/media/base/fakemediaprocessor.h"
     35 #include "talk/media/base/fakenetworkinterface.h"
     36 #include "talk/media/base/fakevideorenderer.h"
     37 #include "talk/media/base/mediachannel.h"
     38 #include "talk/media/base/testutils.h"
     39 #include "talk/media/base/videoadapter.h"
     40 #include "talk/media/base/videoengine_unittest.h"
     41 #include "talk/media/webrtc/fakewebrtcvideocapturemodule.h"
     42 #include "talk/media/webrtc/fakewebrtcvideoengine.h"
     43 #include "talk/media/webrtc/fakewebrtcvoiceengine.h"
     44 #include "talk/media/webrtc/webrtcvideocapturer.h"
     45 #include "talk/media/webrtc/webrtcvideoengine.h"
     46 #include "talk/media/webrtc/webrtcvideoframe.h"
     47 #include "talk/media/webrtc/webrtcvoiceengine.h"
     48 #include "talk/session/media/mediasession.h"
     49 #include "webrtc/system_wrappers/interface/trace.h"
     50 
     51 // Tests for the WebRtcVideoEngine/VideoChannel code.
     52 
     53 using cricket::kRtpTimestampOffsetHeaderExtension;
     54 using cricket::kRtpAbsoluteSenderTimeHeaderExtension;
     55 
     56 static const cricket::VideoCodec kVP8Codec720p(100, "VP8", 1280, 720, 30, 0);
     57 static const cricket::VideoCodec kVP8Codec360p(100, "VP8", 640, 360, 30, 0);
     58 static const cricket::VideoCodec kVP8Codec270p(100, "VP8", 480, 270, 30, 0);
     59 static const cricket::VideoCodec kVP8Codec180p(100, "VP8", 320, 180, 30, 0);
     60 
     61 static const cricket::VideoCodec kVP8Codec(100, "VP8", 640, 400, 30, 0);
     62 static const cricket::VideoCodec kRedCodec(101, "red", 0, 0, 0, 0);
     63 static const cricket::VideoCodec kUlpFecCodec(102, "ulpfec", 0, 0, 0, 0);
     64 static const cricket::VideoCodec* const kVideoCodecs[] = {
     65     &kVP8Codec,
     66     &kRedCodec,
     67     &kUlpFecCodec
     68 };
     69 
     70 static const unsigned int kStartBandwidthKbps = 300;
     71 static const unsigned int kMinBandwidthKbps = 50;
     72 static const unsigned int kMaxBandwidthKbps = 2000;
     73 
     74 static const uint32 kSsrcs1[] = {1};
     75 static const uint32 kSsrcs2[] = {1, 2};
     76 static const uint32 kSsrcs3[] = {1, 2, 3};
     77 static const uint32 kRtxSsrcs1[] = {4};
     78 static const uint32 kRtxSsrcs3[] = {4, 5, 6};
     79 
     80 
     81 class FakeViEWrapper : public cricket::ViEWrapper {
     82  public:
     83   explicit FakeViEWrapper(cricket::FakeWebRtcVideoEngine* engine)
     84       : cricket::ViEWrapper(engine,  // base
     85                             engine,  // codec
     86                             engine,  // capture
     87                             engine,  // network
     88                             engine,  // render
     89                             engine,  // rtp
     90                             engine,  // image
     91                             engine) {  // external decoder
     92   }
     93 };
     94 
     95 // Test fixture to test WebRtcVideoEngine with a fake webrtc::VideoEngine.
     96 // Useful for testing failure paths.
     97 class WebRtcVideoEngineTestFake : public testing::Test,
     98   public sigslot::has_slots<> {
     99  public:
    100   WebRtcVideoEngineTestFake()
    101       : vie_(kVideoCodecs, ARRAY_SIZE(kVideoCodecs)),
    102         cpu_monitor_(new talk_base::FakeCpuMonitor(
    103             talk_base::Thread::Current())),
    104         engine_(NULL,  // cricket::WebRtcVoiceEngine
    105                 new FakeViEWrapper(&vie_), cpu_monitor_),
    106         channel_(NULL),
    107         voice_channel_(NULL),
    108         last_error_(cricket::VideoMediaChannel::ERROR_NONE) {
    109   }
    110   bool SetupEngine() {
    111     bool result = engine_.Init(talk_base::Thread::Current());
    112     if (result) {
    113       channel_ = engine_.CreateChannel(voice_channel_);
    114       channel_->SignalMediaError.connect(this,
    115           &WebRtcVideoEngineTestFake::OnMediaError);
    116       result = (channel_ != NULL);
    117     }
    118     return result;
    119   }
    120   void OnMediaError(uint32 ssrc, cricket::VideoMediaChannel::Error error) {
    121     last_error_ = error;
    122   }
    123   bool SendI420Frame(int width, int height) {
    124     if (NULL == channel_) {
    125       return false;
    126     }
    127     cricket::WebRtcVideoFrame frame;
    128     if (!frame.InitToBlack(width, height, 1, 1, 0, 0)) {
    129       return false;
    130     }
    131     cricket::FakeVideoCapturer capturer;
    132     channel_->SendFrame(&capturer, &frame);
    133     return true;
    134   }
    135   bool SendI420ScreencastFrame(int width, int height) {
    136     return SendI420ScreencastFrameWithTimestamp(width, height, 0);
    137   }
    138   bool SendI420ScreencastFrameWithTimestamp(
    139       int width, int height, int64 timestamp) {
    140     if (NULL == channel_) {
    141       return false;
    142     }
    143     cricket::WebRtcVideoFrame frame;
    144     if (!frame.InitToBlack(width, height, 1, 1, 0, 0)) {
    145       return false;
    146     }
    147     cricket::FakeVideoCapturer capturer;
    148     capturer.SetScreencast(true);
    149     channel_->SendFrame(&capturer, &frame);
    150     return true;
    151   }
    152   void TestSetSendRtpHeaderExtensions(const std::string& ext) {
    153     EXPECT_TRUE(SetupEngine());
    154     int channel_num = vie_.GetLastChannel();
    155 
    156     // Verify extensions are off by default.
    157     EXPECT_EQ(-1, vie_.GetSendRtpExtensionId(channel_num, ext));
    158 
    159     // Enable extension.
    160     const int id = 1;
    161     std::vector<cricket::RtpHeaderExtension> extensions;
    162     extensions.push_back(cricket::RtpHeaderExtension(ext, id));
    163 
    164     // Verify the send extension id.
    165     EXPECT_TRUE(channel_->SetSendRtpHeaderExtensions(extensions));
    166     EXPECT_EQ(id, vie_.GetSendRtpExtensionId(channel_num, ext));
    167     // Verify call with same set of extensions returns true.
    168     EXPECT_TRUE(channel_->SetSendRtpHeaderExtensions(extensions));
    169     EXPECT_EQ(id, vie_.GetSendRtpExtensionId(channel_num, ext));
    170 
    171     // Add a new send stream and verify the extension is set.
    172     // The first send stream to occupy the default channel.
    173     EXPECT_TRUE(
    174         channel_->AddSendStream(cricket::StreamParams::CreateLegacy(123)));
    175     EXPECT_TRUE(
    176         channel_->AddSendStream(cricket::StreamParams::CreateLegacy(234)));
    177     int new_send_channel_num = vie_.GetLastChannel();
    178     EXPECT_NE(channel_num, new_send_channel_num);
    179     EXPECT_EQ(id, vie_.GetSendRtpExtensionId(new_send_channel_num, ext));
    180 
    181     // Remove the extension id.
    182     std::vector<cricket::RtpHeaderExtension> empty_extensions;
    183     EXPECT_TRUE(channel_->SetSendRtpHeaderExtensions(empty_extensions));
    184     EXPECT_EQ(-1, vie_.GetSendRtpExtensionId(channel_num, ext));
    185     EXPECT_EQ(-1, vie_.GetSendRtpExtensionId(new_send_channel_num, ext));
    186   }
    187   void TestSetRecvRtpHeaderExtensions(const std::string& ext) {
    188     EXPECT_TRUE(SetupEngine());
    189     int channel_num = vie_.GetLastChannel();
    190 
    191     // Verify extensions are off by default.
    192     EXPECT_EQ(-1, vie_.GetReceiveRtpExtensionId(channel_num, ext));
    193 
    194     // Enable extension.
    195     const int id = 2;
    196     std::vector<cricket::RtpHeaderExtension> extensions;
    197     extensions.push_back(cricket::RtpHeaderExtension(ext, id));
    198 
    199     // Verify receive extension id.
    200     EXPECT_TRUE(channel_->SetRecvRtpHeaderExtensions(extensions));
    201     EXPECT_EQ(id, vie_.GetReceiveRtpExtensionId(channel_num, ext));
    202     // Verify call with same set of extensions returns true.
    203     EXPECT_TRUE(channel_->SetRecvRtpHeaderExtensions(extensions));
    204     EXPECT_EQ(id, vie_.GetReceiveRtpExtensionId(channel_num, ext));
    205 
    206     // Add a new receive stream and verify the extension is set.
    207     // The first send stream to occupy the default channel.
    208     EXPECT_TRUE(
    209         channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(345)));
    210     EXPECT_TRUE(
    211         channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(456)));
    212     int new_recv_channel_num = vie_.GetLastChannel();
    213     EXPECT_NE(channel_num, new_recv_channel_num);
    214     EXPECT_EQ(id, vie_.GetReceiveRtpExtensionId(new_recv_channel_num, ext));
    215 
    216     // Remove the extension id.
    217     std::vector<cricket::RtpHeaderExtension> empty_extensions;
    218     EXPECT_TRUE(channel_->SetRecvRtpHeaderExtensions(empty_extensions));
    219     EXPECT_EQ(-1, vie_.GetReceiveRtpExtensionId(channel_num, ext));
    220     EXPECT_EQ(-1, vie_.GetReceiveRtpExtensionId(new_recv_channel_num, ext));
    221   }
    222   void VerifyCodecFeedbackParams(const cricket::VideoCodec& codec) {
    223     EXPECT_TRUE(codec.HasFeedbackParam(
    224         cricket::FeedbackParam(cricket::kRtcpFbParamNack,
    225                                cricket::kParamValueEmpty)));
    226     EXPECT_TRUE(codec.HasFeedbackParam(
    227         cricket::FeedbackParam(cricket::kRtcpFbParamNack,
    228                                cricket::kRtcpFbNackParamPli)));
    229     EXPECT_TRUE(codec.HasFeedbackParam(
    230         cricket::FeedbackParam(cricket::kRtcpFbParamRemb,
    231                                cricket::kParamValueEmpty)));
    232     EXPECT_TRUE(codec.HasFeedbackParam(
    233         cricket::FeedbackParam(cricket::kRtcpFbParamCcm,
    234                                cricket::kRtcpFbCcmParamFir)));
    235   }
    236   void VerifyVP8SendCodec(int channel_num,
    237                           unsigned int width,
    238                           unsigned int height,
    239                           unsigned int layers = 0,
    240                           unsigned int max_bitrate = kMaxBandwidthKbps,
    241                           unsigned int min_bitrate = kMinBandwidthKbps,
    242                           unsigned int start_bitrate = kStartBandwidthKbps,
    243                           unsigned int fps = 30,
    244                           unsigned int max_quantization = 0
    245                           ) {
    246     webrtc::VideoCodec gcodec;
    247     EXPECT_EQ(0, vie_.GetSendCodec(channel_num, gcodec));
    248 
    249     // Video codec properties.
    250     EXPECT_EQ(webrtc::kVideoCodecVP8, gcodec.codecType);
    251     EXPECT_STREQ("VP8", gcodec.plName);
    252     EXPECT_EQ(100, gcodec.plType);
    253     EXPECT_EQ(width, gcodec.width);
    254     EXPECT_EQ(height, gcodec.height);
    255     EXPECT_EQ(talk_base::_min(start_bitrate, max_bitrate), gcodec.startBitrate);
    256     EXPECT_EQ(max_bitrate, gcodec.maxBitrate);
    257     EXPECT_EQ(min_bitrate, gcodec.minBitrate);
    258     EXPECT_EQ(fps, gcodec.maxFramerate);
    259     // VP8 specific.
    260     EXPECT_FALSE(gcodec.codecSpecific.VP8.pictureLossIndicationOn);
    261     EXPECT_FALSE(gcodec.codecSpecific.VP8.feedbackModeOn);
    262     EXPECT_EQ(webrtc::kComplexityNormal, gcodec.codecSpecific.VP8.complexity);
    263     EXPECT_EQ(webrtc::kResilienceOff, gcodec.codecSpecific.VP8.resilience);
    264     EXPECT_EQ(max_quantization, gcodec.qpMax);
    265   }
    266   virtual void TearDown() {
    267     delete channel_;
    268     engine_.Terminate();
    269   }
    270 
    271  protected:
    272   cricket::FakeWebRtcVideoEngine vie_;
    273   cricket::FakeWebRtcVideoDecoderFactory decoder_factory_;
    274   cricket::FakeWebRtcVideoEncoderFactory encoder_factory_;
    275   talk_base::FakeCpuMonitor* cpu_monitor_;
    276   cricket::WebRtcVideoEngine engine_;
    277   cricket::WebRtcVideoMediaChannel* channel_;
    278   cricket::WebRtcVoiceMediaChannel* voice_channel_;
    279   cricket::VideoMediaChannel::Error last_error_;
    280 };
    281 
    282 // Test fixtures to test WebRtcVideoEngine with a real webrtc::VideoEngine.
    283 class WebRtcVideoEngineTest
    284     : public VideoEngineTest<cricket::WebRtcVideoEngine> {
    285  protected:
    286   typedef VideoEngineTest<cricket::WebRtcVideoEngine> Base;
    287 };
    288 class WebRtcVideoMediaChannelTest
    289     : public VideoMediaChannelTest<
    290         cricket::WebRtcVideoEngine, cricket::WebRtcVideoMediaChannel> {
    291  protected:
    292   typedef VideoMediaChannelTest<cricket::WebRtcVideoEngine,
    293        cricket::WebRtcVideoMediaChannel> Base;
    294   virtual cricket::VideoCodec DefaultCodec() { return kVP8Codec; }
    295   virtual void SetUp() {
    296     Base::SetUp();
    297   }
    298   virtual void TearDown() {
    299     Base::TearDown();
    300   }
    301 };
    302 
    303 /////////////////////////
    304 // Tests with fake ViE //
    305 /////////////////////////
    306 
    307 // Tests that our stub library "works".
    308 TEST_F(WebRtcVideoEngineTestFake, StartupShutdown) {
    309   EXPECT_FALSE(vie_.IsInited());
    310   EXPECT_TRUE(engine_.Init(talk_base::Thread::Current()));
    311   EXPECT_TRUE(vie_.IsInited());
    312   engine_.Terminate();
    313 }
    314 
    315 // Tests that webrtc logs are logged when they should be.
    316 TEST_F(WebRtcVideoEngineTest, WebRtcShouldLog) {
    317   const char webrtc_log[] = "WebRtcVideoEngineTest.WebRtcShouldLog";
    318   EXPECT_TRUE(engine_.Init(talk_base::Thread::Current()));
    319   engine_.SetLogging(talk_base::LS_INFO, "");
    320   std::string str;
    321   talk_base::StringStream stream(str);
    322   talk_base::LogMessage::AddLogToStream(&stream, talk_base::LS_INFO);
    323   EXPECT_EQ(talk_base::LS_INFO, talk_base::LogMessage::GetLogToStream(&stream));
    324   webrtc::Trace::Add(webrtc::kTraceStateInfo, webrtc::kTraceUndefined, 0,
    325                      webrtc_log);
    326   talk_base::Thread::Current()->ProcessMessages(100);
    327   talk_base::LogMessage::RemoveLogToStream(&stream);
    328   // Access |str| after LogMessage is done with it to avoid data racing.
    329   EXPECT_NE(std::string::npos, str.find(webrtc_log));
    330 }
    331 
    332 // Tests that webrtc logs are not logged when they should't be.
    333 TEST_F(WebRtcVideoEngineTest, WebRtcShouldNotLog) {
    334   const char webrtc_log[] = "WebRtcVideoEngineTest.WebRtcShouldNotLog";
    335   EXPECT_TRUE(engine_.Init(talk_base::Thread::Current()));
    336   // WebRTC should never be logged lower than LS_INFO.
    337   engine_.SetLogging(talk_base::LS_WARNING, "");
    338   std::string str;
    339   talk_base::StringStream stream(str);
    340   // Make sure that WebRTC is not logged, even at lowest severity
    341   talk_base::LogMessage::AddLogToStream(&stream, talk_base::LS_SENSITIVE);
    342   EXPECT_EQ(talk_base::LS_SENSITIVE,
    343             talk_base::LogMessage::GetLogToStream(&stream));
    344   webrtc::Trace::Add(webrtc::kTraceStateInfo, webrtc::kTraceUndefined, 0,
    345                      webrtc_log);
    346   talk_base::Thread::Current()->ProcessMessages(10);
    347   EXPECT_EQ(std::string::npos, str.find(webrtc_log));
    348   talk_base::LogMessage::RemoveLogToStream(&stream);
    349 }
    350 
    351 // Tests that we can create and destroy a channel.
    352 TEST_F(WebRtcVideoEngineTestFake, CreateChannel) {
    353   EXPECT_TRUE(engine_.Init(talk_base::Thread::Current()));
    354   channel_ = engine_.CreateChannel(voice_channel_);
    355   EXPECT_TRUE(channel_ != NULL);
    356   EXPECT_EQ(1, engine_.GetNumOfChannels());
    357   delete channel_;
    358   channel_ = NULL;
    359   EXPECT_EQ(0, engine_.GetNumOfChannels());
    360 }
    361 
    362 // Tests that we properly handle failures in CreateChannel.
    363 TEST_F(WebRtcVideoEngineTestFake, CreateChannelFail) {
    364   vie_.set_fail_create_channel(true);
    365   EXPECT_TRUE(engine_.Init(talk_base::Thread::Current()));
    366   channel_ = engine_.CreateChannel(voice_channel_);
    367   EXPECT_TRUE(channel_ == NULL);
    368 }
    369 
    370 // Tests that we properly handle failures in AllocateExternalCaptureDevice.
    371 TEST_F(WebRtcVideoEngineTestFake, AllocateExternalCaptureDeviceFail) {
    372   vie_.set_fail_alloc_capturer(true);
    373   EXPECT_TRUE(engine_.Init(talk_base::Thread::Current()));
    374   channel_ = engine_.CreateChannel(voice_channel_);
    375   EXPECT_TRUE(channel_ == NULL);
    376 }
    377 
    378 // Test that we apply our default codecs properly.
    379 TEST_F(WebRtcVideoEngineTestFake, SetSendCodecs) {
    380   EXPECT_TRUE(SetupEngine());
    381   int channel_num = vie_.GetLastChannel();
    382   std::vector<cricket::VideoCodec> codecs(engine_.codecs());
    383   EXPECT_TRUE(channel_->SetSendCodecs(codecs));
    384   VerifyVP8SendCodec(channel_num, kVP8Codec.width, kVP8Codec.height);
    385   EXPECT_TRUE(vie_.GetHybridNackFecStatus(channel_num));
    386   EXPECT_FALSE(vie_.GetNackStatus(channel_num));
    387   EXPECT_EQ(1, vie_.GetNumSetSendCodecs());
    388   // TODO(juberti): Check RTCP, PLI, TMMBR.
    389 }
    390 
    391 // Test that ViE Channel doesn't call SetSendCodec again if same codec is tried
    392 // to apply.
    393 TEST_F(WebRtcVideoEngineTestFake, DontResetSetSendCodec) {
    394   EXPECT_TRUE(SetupEngine());
    395   int channel_num = vie_.GetLastChannel();
    396   std::vector<cricket::VideoCodec> codecs(engine_.codecs());
    397   EXPECT_TRUE(channel_->SetSendCodecs(codecs));
    398   VerifyVP8SendCodec(channel_num, kVP8Codec.width, kVP8Codec.height);
    399   EXPECT_TRUE(vie_.GetHybridNackFecStatus(channel_num));
    400   EXPECT_FALSE(vie_.GetNackStatus(channel_num));
    401   EXPECT_EQ(1, vie_.GetNumSetSendCodecs());
    402   // Try setting same code again.
    403   EXPECT_TRUE(channel_->SetSendCodecs(codecs));
    404   // Since it's exact same codec which is already set, media channel shouldn't
    405   // send the codec to ViE.
    406   EXPECT_EQ(1, vie_.GetNumSetSendCodecs());
    407 }
    408 
    409 TEST_F(WebRtcVideoEngineTestFake, SetSendCodecsWithMinMaxBitrate) {
    410   EXPECT_TRUE(SetupEngine());
    411   int channel_num = vie_.GetLastChannel();
    412   std::vector<cricket::VideoCodec> codecs(engine_.codecs());
    413   codecs[0].params[cricket::kCodecParamMinBitrate] = "10";
    414   codecs[0].params[cricket::kCodecParamMaxBitrate] = "20";
    415   EXPECT_TRUE(channel_->SetSendCodecs(codecs));
    416 
    417   VerifyVP8SendCodec(
    418       channel_num, kVP8Codec.width, kVP8Codec.height, 0, 20, 10, 20);
    419 
    420   cricket::VideoCodec codec;
    421   EXPECT_TRUE(channel_->GetSendCodec(&codec));
    422   EXPECT_EQ("10", codec.params[cricket::kCodecParamMinBitrate]);
    423   EXPECT_EQ("20", codec.params[cricket::kCodecParamMaxBitrate]);
    424 }
    425 
    426 TEST_F(WebRtcVideoEngineTestFake, SetSendCodecsWithStartBitrate) {
    427   EXPECT_TRUE(SetupEngine());
    428   int channel_num = vie_.GetLastChannel();
    429   std::vector<cricket::VideoCodec> codecs(engine_.codecs());
    430   codecs[0].params[cricket::kCodecParamStartBitrate] = "450";
    431   EXPECT_TRUE(channel_->SetSendCodecs(codecs));
    432 
    433   VerifyVP8SendCodec(
    434       channel_num, kVP8Codec.width, kVP8Codec.height, 0, 2000, 50, 450);
    435 
    436   cricket::VideoCodec codec;
    437   EXPECT_TRUE(channel_->GetSendCodec(&codec));
    438   EXPECT_EQ("450", codec.params[cricket::kCodecParamStartBitrate]);
    439 }
    440 
    441 TEST_F(WebRtcVideoEngineTestFake, SetSendCodecsWithMinMaxStartBitrate) {
    442   EXPECT_TRUE(SetupEngine());
    443   int channel_num = vie_.GetLastChannel();
    444   std::vector<cricket::VideoCodec> codecs(engine_.codecs());
    445   codecs[0].params[cricket::kCodecParamMinBitrate] = "10";
    446   codecs[0].params[cricket::kCodecParamMaxBitrate] = "20";
    447   codecs[0].params[cricket::kCodecParamStartBitrate] = "14";
    448   EXPECT_TRUE(channel_->SetSendCodecs(codecs));
    449 
    450   VerifyVP8SendCodec(
    451       channel_num, kVP8Codec.width, kVP8Codec.height, 0, 20, 10, 14);
    452 
    453   cricket::VideoCodec codec;
    454   EXPECT_TRUE(channel_->GetSendCodec(&codec));
    455   EXPECT_EQ("10", codec.params[cricket::kCodecParamMinBitrate]);
    456   EXPECT_EQ("20", codec.params[cricket::kCodecParamMaxBitrate]);
    457   EXPECT_EQ("14", codec.params[cricket::kCodecParamStartBitrate]);
    458 }
    459 
    460 TEST_F(WebRtcVideoEngineTestFake, SetSendCodecsWithMinMaxBitrateInvalid) {
    461   EXPECT_TRUE(SetupEngine());
    462   std::vector<cricket::VideoCodec> codecs(engine_.codecs());
    463   codecs[0].params[cricket::kCodecParamMinBitrate] = "30";
    464   codecs[0].params[cricket::kCodecParamMaxBitrate] = "20";
    465   EXPECT_FALSE(channel_->SetSendCodecs(codecs));
    466 }
    467 
    468 TEST_F(WebRtcVideoEngineTestFake, SetSendCodecsWithLargeMinMaxBitrate) {
    469   EXPECT_TRUE(SetupEngine());
    470   int channel_num = vie_.GetLastChannel();
    471   std::vector<cricket::VideoCodec> codecs(engine_.codecs());
    472   codecs[0].params[cricket::kCodecParamMinBitrate] = "1000";
    473   codecs[0].params[cricket::kCodecParamMaxBitrate] = "2000";
    474   EXPECT_TRUE(channel_->SetSendCodecs(codecs));
    475 
    476   VerifyVP8SendCodec(
    477       channel_num, kVP8Codec.width, kVP8Codec.height, 0, 2000, 1000,
    478       1000);
    479 }
    480 
    481 TEST_F(WebRtcVideoEngineTestFake, SetSendCodecsWithMaxQuantization) {
    482   EXPECT_TRUE(SetupEngine());
    483   int channel_num = vie_.GetLastChannel();
    484   std::vector<cricket::VideoCodec> codecs(engine_.codecs());
    485   codecs[0].params[cricket::kCodecParamMaxQuantization] = "21";
    486   EXPECT_TRUE(channel_->SetSendCodecs(codecs));
    487 
    488   VerifyVP8SendCodec(
    489       channel_num, kVP8Codec.width, kVP8Codec.height, 0, 2000, 50, 300,
    490       30, 21);
    491 
    492   cricket::VideoCodec codec;
    493   EXPECT_TRUE(channel_->GetSendCodec(&codec));
    494   EXPECT_EQ("21", codec.params[cricket::kCodecParamMaxQuantization]);
    495 }
    496 
    497 TEST_F(WebRtcVideoEngineTestFake, SetOptionsWithMaxBitrate) {
    498   EXPECT_TRUE(SetupEngine());
    499   int channel_num = vie_.GetLastChannel();
    500   std::vector<cricket::VideoCodec> codecs(engine_.codecs());
    501   codecs[0].params[cricket::kCodecParamMinBitrate] = "10";
    502   codecs[0].params[cricket::kCodecParamMaxBitrate] = "20";
    503   EXPECT_TRUE(channel_->SetSendCodecs(codecs));
    504 
    505   VerifyVP8SendCodec(
    506       channel_num, kVP8Codec.width, kVP8Codec.height, 0, 20, 10, 20);
    507 
    508   // Verify that max bitrate doesn't change after SetOptions().
    509   cricket::VideoOptions options;
    510   options.video_noise_reduction.Set(true);
    511   EXPECT_TRUE(channel_->SetOptions(options));
    512   VerifyVP8SendCodec(
    513       channel_num, kVP8Codec.width, kVP8Codec.height, 0, 20, 10, 20);
    514 
    515   options.video_noise_reduction.Set(false);
    516   options.conference_mode.Set(false);
    517   EXPECT_TRUE(channel_->SetOptions(options));
    518   VerifyVP8SendCodec(
    519       channel_num, kVP8Codec.width, kVP8Codec.height, 0, 20, 10, 20);
    520 }
    521 
    522 TEST_F(WebRtcVideoEngineTestFake, SetOptionsWithLoweredBitrate) {
    523   EXPECT_TRUE(SetupEngine());
    524   int channel_num = vie_.GetLastChannel();
    525   std::vector<cricket::VideoCodec> codecs(engine_.codecs());
    526   codecs[0].params[cricket::kCodecParamMinBitrate] = "50";
    527   codecs[0].params[cricket::kCodecParamMaxBitrate] = "100";
    528   EXPECT_TRUE(channel_->SetSendCodecs(codecs));
    529 
    530   VerifyVP8SendCodec(
    531       channel_num, kVP8Codec.width, kVP8Codec.height, 0, 100, 50, 100);
    532 
    533   // Verify that min bitrate changes after SetOptions().
    534   cricket::VideoOptions options;
    535   options.lower_min_bitrate.Set(true);
    536   EXPECT_TRUE(channel_->SetOptions(options));
    537   VerifyVP8SendCodec(
    538       channel_num, kVP8Codec.width, kVP8Codec.height, 0, 100, 30, 100);
    539 }
    540 
    541 TEST_F(WebRtcVideoEngineTestFake, MaxBitrateResetWithConferenceMode) {
    542   EXPECT_TRUE(SetupEngine());
    543   int channel_num = vie_.GetLastChannel();
    544   std::vector<cricket::VideoCodec> codecs(engine_.codecs());
    545   codecs[0].params[cricket::kCodecParamMinBitrate] = "10";
    546   codecs[0].params[cricket::kCodecParamMaxBitrate] = "20";
    547   EXPECT_TRUE(channel_->SetSendCodecs(codecs));
    548 
    549   VerifyVP8SendCodec(
    550       channel_num, kVP8Codec.width, kVP8Codec.height, 0, 20, 10, 20);
    551 
    552   cricket::VideoOptions options;
    553   options.conference_mode.Set(true);
    554   EXPECT_TRUE(channel_->SetOptions(options));
    555   options.conference_mode.Set(false);
    556   EXPECT_TRUE(channel_->SetOptions(options));
    557   VerifyVP8SendCodec(
    558       channel_num, kVP8Codec.width, kVP8Codec.height, 0,
    559       kMaxBandwidthKbps, 10, kStartBandwidthKbps);
    560 }
    561 
    562 // Verify the current send bitrate is used as start bitrate when reconfiguring
    563 // the send codec.
    564 TEST_F(WebRtcVideoEngineTestFake, StartSendBitrate) {
    565   EXPECT_TRUE(SetupEngine());
    566   EXPECT_TRUE(channel_->AddSendStream(
    567       cricket::StreamParams::CreateLegacy(1)));
    568   int send_channel = vie_.GetLastChannel();
    569   cricket::VideoCodec codec(kVP8Codec);
    570   std::vector<cricket::VideoCodec> codec_list;
    571   codec_list.push_back(codec);
    572   EXPECT_TRUE(channel_->SetSendCodecs(codec_list));
    573   const unsigned int kVideoMaxSendBitrateKbps = 2000;
    574   const unsigned int kVideoMinSendBitrateKbps = 50;
    575   const unsigned int kVideoDefaultStartSendBitrateKbps = 300;
    576   VerifyVP8SendCodec(send_channel, kVP8Codec.width, kVP8Codec.height, 0,
    577                      kVideoMaxSendBitrateKbps, kVideoMinSendBitrateKbps,
    578                      kVideoDefaultStartSendBitrateKbps);
    579   EXPECT_EQ(0, vie_.StartSend(send_channel));
    580 
    581   // Increase the send bitrate and verify it is used as start bitrate.
    582   const unsigned int kVideoSendBitrateBps = 768000;
    583   vie_.SetSendBitrates(send_channel, kVideoSendBitrateBps, 0, 0);
    584   EXPECT_TRUE(channel_->SetSendCodecs(codec_list));
    585   VerifyVP8SendCodec(send_channel, kVP8Codec.width, kVP8Codec.height, 0,
    586                      kVideoMaxSendBitrateKbps, kVideoMinSendBitrateKbps,
    587                      kVideoSendBitrateBps / 1000);
    588 
    589   // Never set a start bitrate higher than the max bitrate.
    590   vie_.SetSendBitrates(send_channel, kVideoMaxSendBitrateKbps + 500, 0, 0);
    591   EXPECT_TRUE(channel_->SetSendCodecs(codec_list));
    592   VerifyVP8SendCodec(send_channel, kVP8Codec.width, kVP8Codec.height, 0,
    593                      kVideoMaxSendBitrateKbps, kVideoMinSendBitrateKbps,
    594                      kVideoDefaultStartSendBitrateKbps);
    595 
    596   // Use the default start bitrate if the send bitrate is lower.
    597   vie_.SetSendBitrates(send_channel, kVideoDefaultStartSendBitrateKbps - 50, 0,
    598                        0);
    599   EXPECT_TRUE(channel_->SetSendCodecs(codec_list));
    600   VerifyVP8SendCodec(send_channel, kVP8Codec.width, kVP8Codec.height, 0,
    601                      kVideoMaxSendBitrateKbps, kVideoMinSendBitrateKbps,
    602                      kVideoDefaultStartSendBitrateKbps);
    603 }
    604 
    605 
    606 // Test that we constrain send codecs properly.
    607 TEST_F(WebRtcVideoEngineTestFake, ConstrainSendCodecs) {
    608   EXPECT_TRUE(SetupEngine());
    609   int channel_num = vie_.GetLastChannel();
    610 
    611   // Set max settings of 640x400x30.
    612   EXPECT_TRUE(engine_.SetDefaultEncoderConfig(
    613     cricket::VideoEncoderConfig(kVP8Codec)));
    614 
    615   // Send codec format bigger than max setting.
    616   cricket::VideoCodec codec(kVP8Codec);
    617   codec.width = 1280;
    618   codec.height = 800;
    619   codec.framerate = 60;
    620   std::vector<cricket::VideoCodec> codec_list;
    621   codec_list.push_back(codec);
    622 
    623   // Set send codec and verify codec has been constrained.
    624   EXPECT_TRUE(channel_->SetSendCodecs(codec_list));
    625   VerifyVP8SendCodec(channel_num, kVP8Codec.width, kVP8Codec.height);
    626 }
    627 
    628 // Test that SetSendCodecs rejects bad format.
    629 TEST_F(WebRtcVideoEngineTestFake, SetSendCodecsRejectBadFormat) {
    630   EXPECT_TRUE(SetupEngine());
    631   int channel_num = vie_.GetLastChannel();
    632 
    633   // Set w = 0.
    634   cricket::VideoCodec codec(kVP8Codec);
    635   codec.width = 0;
    636   std::vector<cricket::VideoCodec> codec_list;
    637   codec_list.push_back(codec);
    638 
    639   // Verify SetSendCodecs failed and send codec is not changed on engine.
    640   EXPECT_FALSE(channel_->SetSendCodecs(codec_list));
    641   webrtc::VideoCodec gcodec;
    642   // Set plType to something other than the value to test against ensuring
    643   // that failure will happen if it is not changed.
    644   gcodec.plType = 1;
    645   EXPECT_EQ(0, vie_.GetSendCodec(channel_num, gcodec));
    646   EXPECT_EQ(0, gcodec.plType);
    647 
    648   // Set h = 0.
    649   codec_list[0].width = 640;
    650   codec_list[0].height = 0;
    651 
    652   // Verify SetSendCodecs failed and send codec is not changed on engine.
    653   EXPECT_FALSE(channel_->SetSendCodecs(codec_list));
    654   // Set plType to something other than the value to test against ensuring
    655   // that failure will happen if it is not changed.
    656   gcodec.plType = 1;
    657   EXPECT_EQ(0, vie_.GetSendCodec(channel_num, gcodec));
    658   EXPECT_EQ(0, gcodec.plType);
    659 }
    660 
    661 // Test that SetSendCodecs rejects bad codec.
    662 TEST_F(WebRtcVideoEngineTestFake, SetSendCodecsRejectBadCodec) {
    663   EXPECT_TRUE(SetupEngine());
    664   int channel_num = vie_.GetLastChannel();
    665 
    666   // Set bad codec name.
    667   cricket::VideoCodec codec(kVP8Codec);
    668   codec.name = "bad";
    669   std::vector<cricket::VideoCodec> codec_list;
    670   codec_list.push_back(codec);
    671 
    672   // Verify SetSendCodecs failed and send codec is not changed on engine.
    673   EXPECT_FALSE(channel_->SetSendCodecs(codec_list));
    674   webrtc::VideoCodec gcodec;
    675   // Set plType to something other than the value to test against ensuring
    676   // that failure will happen if it is not changed.
    677   gcodec.plType = 1;
    678   EXPECT_EQ(0, vie_.GetSendCodec(channel_num, gcodec));
    679   EXPECT_EQ(0, gcodec.plType);
    680 }
    681 
    682 // Test that vie send codec is reset on new video frame size.
    683 TEST_F(WebRtcVideoEngineTestFake, ResetVieSendCodecOnNewFrameSize) {
    684   EXPECT_TRUE(SetupEngine());
    685   int channel_num = vie_.GetLastChannel();
    686 
    687   // Set send codec.
    688   std::vector<cricket::VideoCodec> codec_list;
    689   codec_list.push_back(kVP8Codec);
    690   EXPECT_TRUE(channel_->SetSendCodecs(codec_list));
    691   EXPECT_TRUE(channel_->AddSendStream(
    692       cricket::StreamParams::CreateLegacy(123)));
    693   EXPECT_TRUE(channel_->SetSend(true));
    694 
    695   // Capture a smaller frame and verify vie send codec has been reset to
    696   // the new size.
    697   SendI420Frame(kVP8Codec.width / 2, kVP8Codec.height / 2);
    698   VerifyVP8SendCodec(channel_num, kVP8Codec.width / 2, kVP8Codec.height / 2);
    699 
    700   // Capture a frame bigger than send_codec_ and verify vie send codec has been
    701   // reset (and clipped) to send_codec_.
    702   SendI420Frame(kVP8Codec.width * 2, kVP8Codec.height * 2);
    703   VerifyVP8SendCodec(channel_num, kVP8Codec.width, kVP8Codec.height);
    704 }
    705 
    706 // Test that we set our inbound codecs properly.
    707 TEST_F(WebRtcVideoEngineTestFake, SetRecvCodecs) {
    708   EXPECT_TRUE(SetupEngine());
    709   int channel_num = vie_.GetLastChannel();
    710 
    711   std::vector<cricket::VideoCodec> codecs;
    712   codecs.push_back(kVP8Codec);
    713   EXPECT_TRUE(channel_->SetRecvCodecs(codecs));
    714 
    715   webrtc::VideoCodec wcodec;
    716   EXPECT_TRUE(engine_.ConvertFromCricketVideoCodec(kVP8Codec, &wcodec));
    717   EXPECT_TRUE(vie_.ReceiveCodecRegistered(channel_num, wcodec));
    718 }
    719 
    720 // Test that we set our inbound RTX codecs properly.
    721 TEST_F(WebRtcVideoEngineTestFake, SetRecvCodecsWithRtx) {
    722   EXPECT_TRUE(SetupEngine());
    723   int channel_num = vie_.GetLastChannel();
    724 
    725   std::vector<cricket::VideoCodec> codecs;
    726   cricket::VideoCodec rtx_codec(96, "rtx", 0, 0, 0, 0);
    727   codecs.push_back(rtx_codec);
    728   // Should fail since there's no associated payload type set.
    729   EXPECT_FALSE(channel_->SetRecvCodecs(codecs));
    730 
    731   codecs[0].SetParam("apt", 97);
    732   // Should still fail since the we don't support RTX on this APT.
    733   EXPECT_FALSE(channel_->SetRecvCodecs(codecs));
    734 
    735   codecs[0].SetParam("apt", kVP8Codec.id);
    736   // Should still fail since the associated payload type is unknown.
    737   EXPECT_FALSE(channel_->SetRecvCodecs(codecs));
    738 
    739   codecs.push_back(kVP8Codec);
    740   EXPECT_TRUE(channel_->SetRecvCodecs(codecs));
    741 
    742   webrtc::VideoCodec wcodec;
    743   // Should not have been registered as a WebRTC codec.
    744   EXPECT_TRUE(engine_.ConvertFromCricketVideoCodec(rtx_codec, &wcodec));
    745   EXPECT_STREQ("rtx", wcodec.plName);
    746   EXPECT_FALSE(vie_.ReceiveCodecRegistered(channel_num, wcodec));
    747 
    748   // The RTX payload type should have been set.
    749   EXPECT_EQ(rtx_codec.id, vie_.GetRtxRecvPayloadType(channel_num));
    750 }
    751 
    752 // Test that RTX packets are routed to the default video channel if
    753 // there's only one recv stream.
    754 TEST_F(WebRtcVideoEngineTestFake, TestReceiveRtxOneStream) {
    755   EXPECT_TRUE(SetupEngine());
    756 
    757   // Setup one channel with an associated RTX stream.
    758   cricket::StreamParams params =
    759     cricket::StreamParams::CreateLegacy(kSsrcs1[0]);
    760   params.AddFidSsrc(kSsrcs1[0], kRtxSsrcs1[0]);
    761   EXPECT_TRUE(channel_->AddRecvStream(params));
    762   int channel_num = vie_.GetLastChannel();
    763   EXPECT_EQ(static_cast<int>(kRtxSsrcs1[0]),
    764             vie_.GetRemoteRtxSsrc(channel_num));
    765 
    766   // Register codecs.
    767   std::vector<cricket::VideoCodec> codec_list;
    768   codec_list.push_back(kVP8Codec720p);
    769   cricket::VideoCodec rtx_codec(96, "rtx", 0, 0, 0, 0);
    770   rtx_codec.SetParam("apt", kVP8Codec.id);
    771   codec_list.push_back(rtx_codec);
    772   EXPECT_TRUE(channel_->SetRecvCodecs(codec_list));
    773 
    774   // Construct a fake RTX packet and verify that it is passed to the
    775   // right WebRTC channel.
    776   const size_t kDataLength = 12;
    777   uint8_t data[kDataLength];
    778   memset(data, 0, sizeof(data));
    779   data[0] = 0x80;
    780   data[1] = rtx_codec.id;
    781   talk_base::SetBE32(&data[8], kRtxSsrcs1[0]);
    782   talk_base::Buffer packet(data, kDataLength);
    783   talk_base::PacketTime packet_time;
    784   channel_->OnPacketReceived(&packet, packet_time);
    785   EXPECT_EQ(rtx_codec.id, vie_.GetLastRecvdPayloadType(channel_num));
    786 }
    787 
    788 // Test that RTX packets are routed to the correct video channel.
    789 TEST_F(WebRtcVideoEngineTestFake, TestReceiveRtxThreeStreams) {
    790   EXPECT_TRUE(SetupEngine());
    791 
    792   // Setup three channels with associated RTX streams.
    793   int channel_num[ARRAY_SIZE(kSsrcs3)];
    794   for (size_t i = 0; i < ARRAY_SIZE(kSsrcs3); ++i) {
    795     cricket::StreamParams params =
    796       cricket::StreamParams::CreateLegacy(kSsrcs3[i]);
    797     params.AddFidSsrc(kSsrcs3[i], kRtxSsrcs3[i]);
    798     EXPECT_TRUE(channel_->AddRecvStream(params));
    799     channel_num[i] = vie_.GetLastChannel();
    800   }
    801 
    802   // Register codecs.
    803   std::vector<cricket::VideoCodec> codec_list;
    804   codec_list.push_back(kVP8Codec720p);
    805   cricket::VideoCodec rtx_codec(96, "rtx", 0, 0, 0, 0);
    806   rtx_codec.SetParam("apt", kVP8Codec.id);
    807   codec_list.push_back(rtx_codec);
    808   EXPECT_TRUE(channel_->SetRecvCodecs(codec_list));
    809 
    810   // Construct a fake RTX packet and verify that it is passed to the
    811   // right WebRTC channel.
    812   const size_t kDataLength = 12;
    813   uint8_t data[kDataLength];
    814   memset(data, 0, sizeof(data));
    815   data[0] = 0x80;
    816   data[1] = rtx_codec.id;
    817   talk_base::SetBE32(&data[8], kRtxSsrcs3[1]);
    818   talk_base::Buffer packet(data, kDataLength);
    819   talk_base::PacketTime packet_time;
    820   channel_->OnPacketReceived(&packet, packet_time);
    821   EXPECT_NE(rtx_codec.id, vie_.GetLastRecvdPayloadType(channel_num[0]));
    822   EXPECT_EQ(rtx_codec.id, vie_.GetLastRecvdPayloadType(channel_num[1]));
    823   EXPECT_NE(rtx_codec.id, vie_.GetLastRecvdPayloadType(channel_num[2]));
    824 }
    825 
    826 // Test that channel connects and disconnects external capturer correctly.
    827 TEST_F(WebRtcVideoEngineTestFake, HasExternalCapturer) {
    828   EXPECT_TRUE(SetupEngine());
    829   int channel_num = vie_.GetLastChannel();
    830 
    831   EXPECT_EQ(1, vie_.GetNumCapturers());
    832   int capture_id = vie_.GetCaptureId(channel_num);
    833   EXPECT_EQ(channel_num, vie_.GetCaptureChannelId(capture_id));
    834 
    835   // Delete the channel should disconnect the capturer.
    836   delete channel_;
    837   channel_ = NULL;
    838   EXPECT_EQ(0, vie_.GetNumCapturers());
    839 }
    840 
    841 // Test that channel adds and removes renderer correctly.
    842 TEST_F(WebRtcVideoEngineTestFake, HasRenderer) {
    843   EXPECT_TRUE(SetupEngine());
    844   int channel_num = vie_.GetLastChannel();
    845 
    846   EXPECT_TRUE(vie_.GetHasRenderer(channel_num));
    847   EXPECT_FALSE(vie_.GetRenderStarted(channel_num));
    848 }
    849 
    850 // Test that rtcp is enabled on the channel.
    851 TEST_F(WebRtcVideoEngineTestFake, RtcpEnabled) {
    852   EXPECT_TRUE(SetupEngine());
    853   int channel_num = vie_.GetLastChannel();
    854   EXPECT_EQ(webrtc::kRtcpCompound_RFC4585, vie_.GetRtcpStatus(channel_num));
    855 }
    856 
    857 // Test that key frame request method is set on the channel.
    858 TEST_F(WebRtcVideoEngineTestFake, KeyFrameRequestEnabled) {
    859   EXPECT_TRUE(SetupEngine());
    860   int channel_num = vie_.GetLastChannel();
    861   EXPECT_EQ(webrtc::kViEKeyFrameRequestPliRtcp,
    862             vie_.GetKeyFrameRequestMethod(channel_num));
    863 }
    864 
    865 // Test that remb receive and send is enabled for the default channel in a 1:1
    866 // call.
    867 TEST_F(WebRtcVideoEngineTestFake, RembEnabled) {
    868   EXPECT_TRUE(SetupEngine());
    869   int channel_num = vie_.GetLastChannel();
    870   EXPECT_TRUE(channel_->AddSendStream(
    871       cricket::StreamParams::CreateLegacy(1)));
    872   EXPECT_TRUE(channel_->SetSendCodecs(engine_.codecs()));
    873   EXPECT_TRUE(vie_.GetRembStatusBwPartition(channel_num));
    874   EXPECT_TRUE(channel_->SetSend(true));
    875   EXPECT_TRUE(vie_.GetRembStatusBwPartition(channel_num));
    876   EXPECT_TRUE(vie_.GetRembStatusContribute(channel_num));
    877 }
    878 
    879 // When in conference mode, test that remb is enabled on a receive channel but
    880 // not for the default channel and that it uses the default channel for sending
    881 // remb packets.
    882 TEST_F(WebRtcVideoEngineTestFake, RembEnabledOnReceiveChannels) {
    883   EXPECT_TRUE(SetupEngine());
    884   int default_channel = vie_.GetLastChannel();
    885   cricket::VideoOptions options;
    886   options.conference_mode.Set(true);
    887   EXPECT_TRUE(channel_->SetOptions(options));
    888   EXPECT_TRUE(channel_->AddSendStream(
    889       cricket::StreamParams::CreateLegacy(1)));
    890   EXPECT_TRUE(channel_->SetSendCodecs(engine_.codecs()));
    891   EXPECT_TRUE(vie_.GetRembStatusBwPartition(default_channel));
    892   EXPECT_TRUE(vie_.GetRembStatusContribute(default_channel));
    893   EXPECT_TRUE(channel_->SetSend(true));
    894   EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(1)));
    895   int new_channel_num = vie_.GetLastChannel();
    896   EXPECT_NE(default_channel, new_channel_num);
    897 
    898   EXPECT_TRUE(vie_.GetRembStatusBwPartition(default_channel));
    899   EXPECT_TRUE(vie_.GetRembStatusContribute(default_channel));
    900   EXPECT_FALSE(vie_.GetRembStatusBwPartition(new_channel_num));
    901   EXPECT_TRUE(vie_.GetRembStatusContribute(new_channel_num));
    902 }
    903 
    904 TEST_F(WebRtcVideoEngineTestFake, RecvStreamWithRtx) {
    905   EXPECT_TRUE(SetupEngine());
    906   int default_channel = vie_.GetLastChannel();
    907   cricket::VideoOptions options;
    908   options.conference_mode.Set(true);
    909   EXPECT_TRUE(channel_->SetOptions(options));
    910   EXPECT_TRUE(channel_->AddSendStream(
    911       cricket::CreateSimWithRtxStreamParams("cname",
    912                                             MAKE_VECTOR(kSsrcs3),
    913                                             MAKE_VECTOR(kRtxSsrcs3))));
    914   EXPECT_TRUE(channel_->SetSendCodecs(engine_.codecs()));
    915   EXPECT_TRUE(channel_->SetSend(true));
    916   EXPECT_TRUE(channel_->AddRecvStream(
    917       cricket::CreateSimWithRtxStreamParams("cname",
    918                                             MAKE_VECTOR(kSsrcs1),
    919                                             MAKE_VECTOR(kRtxSsrcs1))));
    920   int new_channel_num = vie_.GetLastChannel();
    921   EXPECT_NE(default_channel, new_channel_num);
    922   EXPECT_EQ(4, vie_.GetRemoteRtxSsrc(new_channel_num));
    923 }
    924 
    925 TEST_F(WebRtcVideoEngineTestFake, RecvStreamNoRtx) {
    926   EXPECT_TRUE(SetupEngine());
    927   int default_channel = vie_.GetLastChannel();
    928   cricket::VideoOptions options;
    929   options.conference_mode.Set(true);
    930   EXPECT_TRUE(channel_->SetOptions(options));
    931   EXPECT_TRUE(channel_->AddSendStream(
    932       cricket::CreateSimWithRtxStreamParams("cname",
    933                                             MAKE_VECTOR(kSsrcs3),
    934                                             MAKE_VECTOR(kRtxSsrcs3))));
    935   EXPECT_TRUE(channel_->SetSendCodecs(engine_.codecs()));
    936   EXPECT_TRUE(channel_->SetSend(true));
    937   EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(1)));
    938   int new_channel_num = vie_.GetLastChannel();
    939   EXPECT_NE(default_channel, new_channel_num);
    940   EXPECT_EQ(-1, vie_.GetRemoteRtxSsrc(new_channel_num));
    941 }
    942 
    943 // Test support for RTP timestamp offset header extension.
    944 TEST_F(WebRtcVideoEngineTestFake, SendRtpTimestampOffsetHeaderExtensions) {
    945   TestSetSendRtpHeaderExtensions(kRtpTimestampOffsetHeaderExtension);
    946 }
    947 TEST_F(WebRtcVideoEngineTestFake, RecvRtpTimestampOffsetHeaderExtensions) {
    948   TestSetRecvRtpHeaderExtensions(kRtpTimestampOffsetHeaderExtension);
    949 }
    950 
    951 // Test support for absolute send time header extension.
    952 TEST_F(WebRtcVideoEngineTestFake, SendAbsoluteSendTimeHeaderExtensions) {
    953   TestSetSendRtpHeaderExtensions(kRtpAbsoluteSenderTimeHeaderExtension);
    954 }
    955 TEST_F(WebRtcVideoEngineTestFake, RecvAbsoluteSendTimeHeaderExtensions) {
    956   TestSetRecvRtpHeaderExtensions(kRtpAbsoluteSenderTimeHeaderExtension);
    957 }
    958 
    959 TEST_F(WebRtcVideoEngineTestFake, LeakyBucketTest) {
    960   EXPECT_TRUE(SetupEngine());
    961 
    962   // Verify this is on by default.
    963   EXPECT_TRUE(channel_->AddSendStream(cricket::StreamParams::CreateLegacy(1)));
    964   int first_send_channel = vie_.GetLastChannel();
    965   EXPECT_TRUE(vie_.GetTransmissionSmoothingStatus(first_send_channel));
    966 
    967   // Disable the experiment and verify.
    968   cricket::VideoOptions options;
    969   options.conference_mode.Set(true);
    970   options.video_leaky_bucket.Set(false);
    971   EXPECT_TRUE(channel_->SetOptions(options));
    972   EXPECT_FALSE(vie_.GetTransmissionSmoothingStatus(first_send_channel));
    973 
    974   // Add a receive channel and verify leaky bucket isn't enabled.
    975   EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(2)));
    976   int recv_channel_num = vie_.GetLastChannel();
    977   EXPECT_NE(first_send_channel, recv_channel_num);
    978   EXPECT_FALSE(vie_.GetTransmissionSmoothingStatus(recv_channel_num));
    979 
    980   // Add a new send stream and verify leaky bucket is disabled from start.
    981   EXPECT_TRUE(channel_->AddSendStream(cricket::StreamParams::CreateLegacy(3)));
    982   int second_send_channel = vie_.GetLastChannel();
    983   EXPECT_NE(first_send_channel, second_send_channel);
    984   EXPECT_FALSE(vie_.GetTransmissionSmoothingStatus(second_send_channel));
    985 
    986   // Reenable leaky bucket.
    987   options.video_leaky_bucket.Set(true);
    988   EXPECT_TRUE(channel_->SetOptions(options));
    989   EXPECT_TRUE(vie_.GetTransmissionSmoothingStatus(first_send_channel));
    990   EXPECT_TRUE(vie_.GetTransmissionSmoothingStatus(second_send_channel));
    991 }
    992 
    993 // Verify that SuspendBelowMinBitrate is enabled if it is set in the options.
    994 TEST_F(WebRtcVideoEngineTestFake, SuspendBelowMinBitrateTest) {
    995   EXPECT_TRUE(SetupEngine());
    996 
    997   // Verify this is off by default.
    998   EXPECT_TRUE(channel_->AddSendStream(cricket::StreamParams::CreateLegacy(1)));
    999   int first_send_channel = vie_.GetLastChannel();
   1000   EXPECT_FALSE(vie_.GetSuspendBelowMinBitrateStatus(first_send_channel));
   1001 
   1002   // Enable the experiment and verify.
   1003   cricket::VideoOptions options;
   1004   options.suspend_below_min_bitrate.Set(true);
   1005   EXPECT_TRUE(channel_->SetOptions(options));
   1006   EXPECT_TRUE(vie_.GetSuspendBelowMinBitrateStatus(first_send_channel));
   1007 
   1008   // Add a new send stream and verify suspend_below_min_bitrate is enabled.
   1009   EXPECT_TRUE(channel_->AddSendStream(cricket::StreamParams::CreateLegacy(2)));
   1010   int second_send_channel = vie_.GetLastChannel();
   1011   EXPECT_NE(first_send_channel, second_send_channel);
   1012   EXPECT_TRUE(vie_.GetSuspendBelowMinBitrateStatus(second_send_channel));
   1013 }
   1014 
   1015 TEST_F(WebRtcVideoEngineTestFake, BufferedModeLatency) {
   1016   EXPECT_TRUE(SetupEngine());
   1017 
   1018   // Verify this is off by default.
   1019   EXPECT_TRUE(channel_->AddSendStream(cricket::StreamParams::CreateLegacy(1)));
   1020   int first_send_channel = vie_.GetLastChannel();
   1021   EXPECT_EQ(0, vie_.GetSenderTargetDelay(first_send_channel));
   1022   EXPECT_EQ(0, vie_.GetReceiverTargetDelay(first_send_channel));
   1023 
   1024   // Enable the experiment and verify. The default channel will have both
   1025   // sender and receiver buffered mode enabled.
   1026   cricket::VideoOptions options;
   1027   options.conference_mode.Set(true);
   1028   options.buffered_mode_latency.Set(100);
   1029   EXPECT_TRUE(channel_->SetOptions(options));
   1030   EXPECT_EQ(100, vie_.GetSenderTargetDelay(first_send_channel));
   1031   EXPECT_EQ(100, vie_.GetReceiverTargetDelay(first_send_channel));
   1032 
   1033   // Add a receive channel and verify sender buffered mode isn't enabled.
   1034   EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(2)));
   1035   int recv_channel_num = vie_.GetLastChannel();
   1036   EXPECT_NE(first_send_channel, recv_channel_num);
   1037   EXPECT_EQ(0, vie_.GetSenderTargetDelay(recv_channel_num));
   1038   EXPECT_EQ(100, vie_.GetReceiverTargetDelay(recv_channel_num));
   1039 
   1040   // Add a new send stream and verify sender buffered mode is enabled.
   1041   EXPECT_TRUE(channel_->AddSendStream(cricket::StreamParams::CreateLegacy(3)));
   1042   int second_send_channel = vie_.GetLastChannel();
   1043   EXPECT_NE(first_send_channel, second_send_channel);
   1044   EXPECT_EQ(100, vie_.GetSenderTargetDelay(second_send_channel));
   1045   EXPECT_EQ(0, vie_.GetReceiverTargetDelay(second_send_channel));
   1046 
   1047   // Disable sender buffered mode and verify.
   1048   options.buffered_mode_latency.Set(cricket::kBufferedModeDisabled);
   1049   EXPECT_TRUE(channel_->SetOptions(options));
   1050   EXPECT_EQ(0, vie_.GetSenderTargetDelay(first_send_channel));
   1051   EXPECT_EQ(0, vie_.GetReceiverTargetDelay(first_send_channel));
   1052   EXPECT_EQ(0, vie_.GetSenderTargetDelay(second_send_channel));
   1053   EXPECT_EQ(0, vie_.GetReceiverTargetDelay(second_send_channel));
   1054   EXPECT_EQ(0, vie_.GetSenderTargetDelay(recv_channel_num));
   1055   EXPECT_EQ(0, vie_.GetReceiverTargetDelay(recv_channel_num));
   1056 }
   1057 
   1058 TEST_F(WebRtcVideoEngineTestFake, AdditiveVideoOptions) {
   1059   EXPECT_TRUE(SetupEngine());
   1060 
   1061   EXPECT_TRUE(channel_->AddSendStream(cricket::StreamParams::CreateLegacy(1)));
   1062   int first_send_channel = vie_.GetLastChannel();
   1063   EXPECT_EQ(0, vie_.GetSenderTargetDelay(first_send_channel));
   1064   EXPECT_EQ(0, vie_.GetReceiverTargetDelay(first_send_channel));
   1065 
   1066   cricket::VideoOptions options1;
   1067   options1.buffered_mode_latency.Set(100);
   1068   EXPECT_TRUE(channel_->SetOptions(options1));
   1069   EXPECT_EQ(100, vie_.GetSenderTargetDelay(first_send_channel));
   1070   EXPECT_EQ(100, vie_.GetReceiverTargetDelay(first_send_channel));
   1071   EXPECT_TRUE(vie_.GetTransmissionSmoothingStatus(first_send_channel));
   1072 
   1073   cricket::VideoOptions options2;
   1074   options2.video_leaky_bucket.Set(false);
   1075   EXPECT_TRUE(channel_->SetOptions(options2));
   1076   EXPECT_FALSE(vie_.GetTransmissionSmoothingStatus(first_send_channel));
   1077   // The buffered_mode_latency still takes effect.
   1078   EXPECT_EQ(100, vie_.GetSenderTargetDelay(first_send_channel));
   1079   EXPECT_EQ(100, vie_.GetReceiverTargetDelay(first_send_channel));
   1080 
   1081   options1.buffered_mode_latency.Set(50);
   1082   EXPECT_TRUE(channel_->SetOptions(options1));
   1083   EXPECT_EQ(50, vie_.GetSenderTargetDelay(first_send_channel));
   1084   EXPECT_EQ(50, vie_.GetReceiverTargetDelay(first_send_channel));
   1085   // The video_leaky_bucket still takes effect.
   1086   EXPECT_FALSE(vie_.GetTransmissionSmoothingStatus(first_send_channel));
   1087 }
   1088 
   1089 TEST_F(WebRtcVideoEngineTestFake, SetCpuOveruseOptionsWithCaptureJitterMethod) {
   1090   EXPECT_TRUE(SetupEngine());
   1091 
   1092   // Verify this is off by default.
   1093   EXPECT_TRUE(channel_->AddSendStream(cricket::StreamParams::CreateLegacy(1)));
   1094   int first_send_channel = vie_.GetLastChannel();
   1095   webrtc::CpuOveruseOptions cpu_option =
   1096       vie_.GetCpuOveruseOptions(first_send_channel);
   1097   EXPECT_EQ(0, cpu_option.low_capture_jitter_threshold_ms);
   1098   EXPECT_EQ(0, cpu_option.high_capture_jitter_threshold_ms);
   1099   EXPECT_FALSE(cpu_option.enable_capture_jitter_method);
   1100   EXPECT_FALSE(cpu_option.enable_encode_usage_method);
   1101 
   1102   // Set low and high threshold and verify that cpu options are set.
   1103   cricket::VideoOptions options;
   1104   options.conference_mode.Set(true);
   1105   options.cpu_underuse_threshold.Set(10);
   1106   options.cpu_overuse_threshold.Set(20);
   1107   EXPECT_TRUE(channel_->SetOptions(options));
   1108   cpu_option = vie_.GetCpuOveruseOptions(first_send_channel);
   1109   EXPECT_EQ(10, cpu_option.low_capture_jitter_threshold_ms);
   1110   EXPECT_EQ(20, cpu_option.high_capture_jitter_threshold_ms);
   1111   EXPECT_TRUE(cpu_option.enable_capture_jitter_method);
   1112   EXPECT_FALSE(cpu_option.enable_encode_usage_method);
   1113 
   1114   // Add a receive channel and verify that cpu options are not set.
   1115   EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(2)));
   1116   int recv_channel_num = vie_.GetLastChannel();
   1117   EXPECT_NE(first_send_channel, recv_channel_num);
   1118   cpu_option = vie_.GetCpuOveruseOptions(recv_channel_num);
   1119   EXPECT_EQ(0, cpu_option.low_capture_jitter_threshold_ms);
   1120   EXPECT_EQ(0, cpu_option.high_capture_jitter_threshold_ms);
   1121   EXPECT_FALSE(cpu_option.enable_capture_jitter_method);
   1122   EXPECT_FALSE(cpu_option.enable_encode_usage_method);
   1123 
   1124   // Add a new send stream and verify that cpu options are set from start.
   1125   EXPECT_TRUE(channel_->AddSendStream(cricket::StreamParams::CreateLegacy(3)));
   1126   int second_send_channel = vie_.GetLastChannel();
   1127   EXPECT_NE(first_send_channel, second_send_channel);
   1128   cpu_option = vie_.GetCpuOveruseOptions(second_send_channel);
   1129   EXPECT_EQ(10, cpu_option.low_capture_jitter_threshold_ms);
   1130   EXPECT_EQ(20, cpu_option.high_capture_jitter_threshold_ms);
   1131   EXPECT_TRUE(cpu_option.enable_capture_jitter_method);
   1132   EXPECT_FALSE(cpu_option.enable_encode_usage_method);
   1133 }
   1134 
   1135 TEST_F(WebRtcVideoEngineTestFake, SetInvalidCpuOveruseThresholds) {
   1136   EXPECT_TRUE(SetupEngine());
   1137   EXPECT_TRUE(channel_->AddSendStream(cricket::StreamParams::CreateLegacy(1)));
   1138   int channel_num = vie_.GetLastChannel();
   1139 
   1140   // Only low threshold set. Verify that cpu options are not set.
   1141   cricket::VideoOptions options;
   1142   options.conference_mode.Set(true);
   1143   options.cpu_underuse_threshold.Set(10);
   1144   EXPECT_TRUE(channel_->SetOptions(options));
   1145   webrtc::CpuOveruseOptions cpu_option = vie_.GetCpuOveruseOptions(channel_num);
   1146   EXPECT_EQ(0, cpu_option.low_capture_jitter_threshold_ms);
   1147   EXPECT_EQ(0, cpu_option.high_capture_jitter_threshold_ms);
   1148   EXPECT_FALSE(cpu_option.enable_capture_jitter_method);
   1149   EXPECT_FALSE(cpu_option.enable_encode_usage_method);
   1150 
   1151   // Set high threshold to a negative value. Verify that options are not set.
   1152   options.cpu_overuse_threshold.Set(-1);
   1153   EXPECT_TRUE(channel_->SetOptions(options));
   1154   cpu_option = vie_.GetCpuOveruseOptions(channel_num);
   1155   EXPECT_EQ(0, cpu_option.low_capture_jitter_threshold_ms);
   1156   EXPECT_EQ(0, cpu_option.high_capture_jitter_threshold_ms);
   1157   EXPECT_FALSE(cpu_option.enable_capture_jitter_method);
   1158   EXPECT_FALSE(cpu_option.enable_encode_usage_method);
   1159 
   1160   // Low and high threshold valid. Verify that cpu options are set.
   1161   options.cpu_overuse_threshold.Set(20);
   1162   EXPECT_TRUE(channel_->SetOptions(options));
   1163   cpu_option = vie_.GetCpuOveruseOptions(channel_num);
   1164   EXPECT_EQ(10, cpu_option.low_capture_jitter_threshold_ms);
   1165   EXPECT_EQ(20, cpu_option.high_capture_jitter_threshold_ms);
   1166   EXPECT_TRUE(cpu_option.enable_capture_jitter_method);
   1167   EXPECT_FALSE(cpu_option.enable_encode_usage_method);
   1168 }
   1169 
   1170 TEST_F(WebRtcVideoEngineTestFake, SetCpuOveruseOptionsWithEncodeUsageMethod) {
   1171   EXPECT_TRUE(SetupEngine());
   1172   EXPECT_TRUE(channel_->AddSendStream(cricket::StreamParams::CreateLegacy(1)));
   1173   int first_send_channel = vie_.GetLastChannel();
   1174 
   1175   // Set low and high threshold and enable encode usage method.
   1176   // Verify that cpu options are set.
   1177   cricket::VideoOptions options;
   1178   options.conference_mode.Set(true);
   1179   options.cpu_underuse_threshold.Set(10);
   1180   options.cpu_overuse_threshold.Set(20);
   1181   options.cpu_overuse_encode_usage.Set(true);
   1182   EXPECT_TRUE(channel_->SetOptions(options));
   1183   webrtc::CpuOveruseOptions cpu_option =
   1184       vie_.GetCpuOveruseOptions(first_send_channel);
   1185   EXPECT_EQ(10, cpu_option.low_encode_usage_threshold_percent);
   1186   EXPECT_EQ(20, cpu_option.high_encode_usage_threshold_percent);
   1187   EXPECT_FALSE(cpu_option.enable_capture_jitter_method);
   1188   EXPECT_TRUE(cpu_option.enable_encode_usage_method);
   1189 #ifdef USE_WEBRTC_DEV_BRANCH
   1190   // Verify that optional encode rsd thresholds are not set.
   1191   EXPECT_EQ(-1, cpu_option.low_encode_time_rsd_threshold);
   1192   EXPECT_EQ(-1, cpu_option.high_encode_time_rsd_threshold);
   1193 #endif
   1194 
   1195   // Add a new send stream and verify that cpu options are set from start.
   1196   EXPECT_TRUE(channel_->AddSendStream(cricket::StreamParams::CreateLegacy(3)));
   1197   int second_send_channel = vie_.GetLastChannel();
   1198   EXPECT_NE(first_send_channel, second_send_channel);
   1199   cpu_option = vie_.GetCpuOveruseOptions(second_send_channel);
   1200   EXPECT_EQ(10, cpu_option.low_encode_usage_threshold_percent);
   1201   EXPECT_EQ(20, cpu_option.high_encode_usage_threshold_percent);
   1202   EXPECT_FALSE(cpu_option.enable_capture_jitter_method);
   1203   EXPECT_TRUE(cpu_option.enable_encode_usage_method);
   1204 #ifdef USE_WEBRTC_DEV_BRANCH
   1205   // Verify that optional encode rsd thresholds are not set.
   1206   EXPECT_EQ(-1, cpu_option.low_encode_time_rsd_threshold);
   1207   EXPECT_EQ(-1, cpu_option.high_encode_time_rsd_threshold);
   1208 #endif
   1209 }
   1210 
   1211 TEST_F(WebRtcVideoEngineTestFake, SetCpuOveruseOptionsWithEncodeRsdThresholds) {
   1212   EXPECT_TRUE(SetupEngine());
   1213   EXPECT_TRUE(channel_->AddSendStream(cricket::StreamParams::CreateLegacy(1)));
   1214   int first_send_channel = vie_.GetLastChannel();
   1215 
   1216   // Set optional encode rsd thresholds and verify cpu options.
   1217   cricket::VideoOptions options;
   1218   options.conference_mode.Set(true);
   1219   options.cpu_underuse_threshold.Set(10);
   1220   options.cpu_overuse_threshold.Set(20);
   1221   options.cpu_underuse_encode_rsd_threshold.Set(30);
   1222   options.cpu_overuse_encode_rsd_threshold.Set(40);
   1223   options.cpu_overuse_encode_usage.Set(true);
   1224   EXPECT_TRUE(channel_->SetOptions(options));
   1225   webrtc::CpuOveruseOptions cpu_option =
   1226       vie_.GetCpuOveruseOptions(first_send_channel);
   1227   EXPECT_EQ(10, cpu_option.low_encode_usage_threshold_percent);
   1228   EXPECT_EQ(20, cpu_option.high_encode_usage_threshold_percent);
   1229   EXPECT_FALSE(cpu_option.enable_capture_jitter_method);
   1230   EXPECT_TRUE(cpu_option.enable_encode_usage_method);
   1231 #ifdef USE_WEBRTC_DEV_BRANCH
   1232   EXPECT_EQ(30, cpu_option.low_encode_time_rsd_threshold);
   1233   EXPECT_EQ(40, cpu_option.high_encode_time_rsd_threshold);
   1234 #endif
   1235 
   1236   // Add a new send stream and verify that cpu options are set from start.
   1237   EXPECT_TRUE(channel_->AddSendStream(cricket::StreamParams::CreateLegacy(3)));
   1238   int second_send_channel = vie_.GetLastChannel();
   1239   EXPECT_NE(first_send_channel, second_send_channel);
   1240   cpu_option = vie_.GetCpuOveruseOptions(second_send_channel);
   1241   EXPECT_EQ(10, cpu_option.low_encode_usage_threshold_percent);
   1242   EXPECT_EQ(20, cpu_option.high_encode_usage_threshold_percent);
   1243   EXPECT_FALSE(cpu_option.enable_capture_jitter_method);
   1244   EXPECT_TRUE(cpu_option.enable_encode_usage_method);
   1245 #ifdef USE_WEBRTC_DEV_BRANCH
   1246   EXPECT_EQ(30, cpu_option.low_encode_time_rsd_threshold);
   1247   EXPECT_EQ(40, cpu_option.high_encode_time_rsd_threshold);
   1248 #endif
   1249 }
   1250 
   1251 // Test that AddRecvStream doesn't create new channel for 1:1 call.
   1252 TEST_F(WebRtcVideoEngineTestFake, AddRecvStream1On1) {
   1253   EXPECT_TRUE(SetupEngine());
   1254   int channel_num = vie_.GetLastChannel();
   1255   EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(1)));
   1256   EXPECT_EQ(channel_num, vie_.GetLastChannel());
   1257 }
   1258 
   1259 // Test that NACK, PLI and REMB are enabled for internal codec.
   1260 TEST_F(WebRtcVideoEngineTestFake, InternalCodecFeedbackParams) {
   1261   EXPECT_TRUE(SetupEngine());
   1262 
   1263   std::vector<cricket::VideoCodec> codecs(engine_.codecs());
   1264   // Vp8 will appear at the beginning.
   1265   size_t pos = 0;
   1266   EXPECT_EQ("VP8", codecs[pos].name);
   1267   VerifyCodecFeedbackParams(codecs[pos]);
   1268 }
   1269 
   1270 // Test that AddRecvStream doesn't change remb for 1:1 call.
   1271 TEST_F(WebRtcVideoEngineTestFake, NoRembChangeAfterAddRecvStream) {
   1272   EXPECT_TRUE(SetupEngine());
   1273   int channel_num = vie_.GetLastChannel();
   1274   EXPECT_TRUE(channel_->AddSendStream(
   1275       cricket::StreamParams::CreateLegacy(1)));
   1276   EXPECT_TRUE(channel_->SetSendCodecs(engine_.codecs()));
   1277   EXPECT_TRUE(vie_.GetRembStatusBwPartition(channel_num));
   1278   EXPECT_TRUE(vie_.GetRembStatusContribute(channel_num));
   1279   EXPECT_TRUE(channel_->SetSend(true));
   1280   EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(1)));
   1281   EXPECT_TRUE(vie_.GetRembStatusBwPartition(channel_num));
   1282   EXPECT_TRUE(vie_.GetRembStatusContribute(channel_num));
   1283 }
   1284 
   1285 // Verify default REMB setting and that it can be turned on and off.
   1286 TEST_F(WebRtcVideoEngineTestFake, RembOnOff) {
   1287   EXPECT_TRUE(SetupEngine());
   1288   int channel_num = vie_.GetLastChannel();
   1289   // Verify REMB sending is always off by default.
   1290   EXPECT_FALSE(vie_.GetRembStatusBwPartition(channel_num));
   1291 
   1292   // Verify that REMB is turned on when setting default codecs since the
   1293   // default codecs have REMB enabled.
   1294   EXPECT_TRUE(channel_->SetSendCodecs(engine_.codecs()));
   1295   EXPECT_TRUE(vie_.GetRembStatusBwPartition(channel_num));
   1296 
   1297   // Verify that REMB is turned off when codecs without REMB are set.
   1298   std::vector<cricket::VideoCodec> codecs = engine_.codecs();
   1299   // Clearing the codecs' FeedbackParams and setting send codecs should disable
   1300   // REMB.
   1301   for (std::vector<cricket::VideoCodec>::iterator iter = codecs.begin();
   1302        iter != codecs.end(); ++iter) {
   1303     // Intersecting with empty will clear the FeedbackParams.
   1304     cricket::FeedbackParams empty_params;
   1305     iter->feedback_params.Intersect(empty_params);
   1306     EXPECT_TRUE(iter->feedback_params.params().empty());
   1307   }
   1308   EXPECT_TRUE(channel_->SetSendCodecs(codecs));
   1309   EXPECT_FALSE(vie_.GetRembStatusBwPartition(channel_num));
   1310 }
   1311 
   1312 // Test that nack is enabled on the channel if we don't offer red/fec.
   1313 TEST_F(WebRtcVideoEngineTestFake, NackEnabled) {
   1314   EXPECT_TRUE(SetupEngine());
   1315   int channel_num = vie_.GetLastChannel();
   1316   std::vector<cricket::VideoCodec> codecs(engine_.codecs());
   1317   codecs.resize(1);  // toss out red and ulpfec
   1318   EXPECT_TRUE(channel_->SetSendCodecs(codecs));
   1319   EXPECT_TRUE(vie_.GetNackStatus(channel_num));
   1320 }
   1321 
   1322 // Test that we enable hybrid NACK FEC mode.
   1323 TEST_F(WebRtcVideoEngineTestFake, HybridNackFec) {
   1324   EXPECT_TRUE(SetupEngine());
   1325   int channel_num = vie_.GetLastChannel();
   1326   EXPECT_TRUE(channel_->SetRecvCodecs(engine_.codecs()));
   1327   EXPECT_TRUE(channel_->SetSendCodecs(engine_.codecs()));
   1328   EXPECT_TRUE(vie_.GetHybridNackFecStatus(channel_num));
   1329   EXPECT_FALSE(vie_.GetNackStatus(channel_num));
   1330 }
   1331 
   1332 // Test that we enable hybrid NACK FEC mode when calling SetSendCodecs and
   1333 // SetReceiveCodecs in reversed order.
   1334 TEST_F(WebRtcVideoEngineTestFake, HybridNackFecReversedOrder) {
   1335   EXPECT_TRUE(SetupEngine());
   1336   int channel_num = vie_.GetLastChannel();
   1337   EXPECT_TRUE(channel_->SetSendCodecs(engine_.codecs()));
   1338   EXPECT_TRUE(channel_->SetRecvCodecs(engine_.codecs()));
   1339   EXPECT_TRUE(vie_.GetHybridNackFecStatus(channel_num));
   1340   EXPECT_FALSE(vie_.GetNackStatus(channel_num));
   1341 }
   1342 
   1343 // Test NACK vs Hybrid NACK/FEC interop call setup, i.e. only use NACK even if
   1344 // red/fec is offered as receive codec.
   1345 TEST_F(WebRtcVideoEngineTestFake, VideoProtectionInterop) {
   1346   EXPECT_TRUE(SetupEngine());
   1347   int channel_num = vie_.GetLastChannel();
   1348   std::vector<cricket::VideoCodec> recv_codecs(engine_.codecs());
   1349   std::vector<cricket::VideoCodec> send_codecs(engine_.codecs());
   1350   // Only add VP8 as send codec.
   1351   send_codecs.resize(1);
   1352   EXPECT_TRUE(channel_->SetRecvCodecs(recv_codecs));
   1353   EXPECT_TRUE(channel_->SetSendCodecs(send_codecs));
   1354   EXPECT_FALSE(vie_.GetHybridNackFecStatus(channel_num));
   1355   EXPECT_TRUE(vie_.GetNackStatus(channel_num));
   1356 }
   1357 
   1358 // Test NACK vs Hybrid NACK/FEC interop call setup, i.e. only use NACK even if
   1359 // red/fec is offered as receive codec. Call order reversed compared to
   1360 // VideoProtectionInterop.
   1361 TEST_F(WebRtcVideoEngineTestFake, VideoProtectionInteropReversed) {
   1362   EXPECT_TRUE(SetupEngine());
   1363   int channel_num = vie_.GetLastChannel();
   1364   std::vector<cricket::VideoCodec> recv_codecs(engine_.codecs());
   1365   std::vector<cricket::VideoCodec> send_codecs(engine_.codecs());
   1366   // Only add VP8 as send codec.
   1367   send_codecs.resize(1);
   1368   EXPECT_TRUE(channel_->SetSendCodecs(send_codecs));
   1369   EXPECT_TRUE(channel_->SetRecvCodecs(recv_codecs));
   1370   EXPECT_FALSE(vie_.GetHybridNackFecStatus(channel_num));
   1371   EXPECT_TRUE(vie_.GetNackStatus(channel_num));
   1372 }
   1373 
   1374 // Test that NACK, not hybrid mode, is enabled in conference mode.
   1375 TEST_F(WebRtcVideoEngineTestFake, HybridNackFecConference) {
   1376   EXPECT_TRUE(SetupEngine());
   1377   // Setup the send channel.
   1378   int send_channel_num = vie_.GetLastChannel();
   1379   cricket::VideoOptions options;
   1380   options.conference_mode.Set(true);
   1381   EXPECT_TRUE(channel_->SetOptions(options));
   1382   EXPECT_TRUE(channel_->SetRecvCodecs(engine_.codecs()));
   1383   EXPECT_TRUE(channel_->SetSendCodecs(engine_.codecs()));
   1384   EXPECT_FALSE(vie_.GetHybridNackFecStatus(send_channel_num));
   1385   EXPECT_TRUE(vie_.GetNackStatus(send_channel_num));
   1386   // Add a receive stream.
   1387   EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(1)));
   1388   int receive_channel_num = vie_.GetLastChannel();
   1389   EXPECT_FALSE(vie_.GetHybridNackFecStatus(receive_channel_num));
   1390   EXPECT_TRUE(vie_.GetNackStatus(receive_channel_num));
   1391 }
   1392 
   1393 // Test that when AddRecvStream in conference mode, a new channel is created
   1394 // for receiving. And the new channel's "original channel" is the send channel.
   1395 TEST_F(WebRtcVideoEngineTestFake, AddRemoveRecvStreamConference) {
   1396   EXPECT_TRUE(SetupEngine());
   1397   // Setup the send channel.
   1398   int send_channel_num = vie_.GetLastChannel();
   1399   cricket::VideoOptions options;
   1400   options.conference_mode.Set(true);
   1401   EXPECT_TRUE(channel_->SetOptions(options));
   1402   // Add a receive stream.
   1403   EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(1)));
   1404   int receive_channel_num = vie_.GetLastChannel();
   1405   EXPECT_EQ(send_channel_num, vie_.GetOriginalChannelId(receive_channel_num));
   1406   EXPECT_TRUE(channel_->RemoveRecvStream(1));
   1407   EXPECT_FALSE(vie_.IsChannel(receive_channel_num));
   1408 }
   1409 
   1410 // Test that adding/removing stream with 0 ssrc should fail (and not crash).
   1411 // For crbug/351699 and 350988.
   1412 TEST_F(WebRtcVideoEngineTestFake, AddRemoveRecvStreamWith0Ssrc) {
   1413   EXPECT_TRUE(SetupEngine());
   1414   EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(1)));
   1415   EXPECT_FALSE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(0)));
   1416   EXPECT_FALSE(channel_->RemoveRecvStream(0));
   1417   EXPECT_TRUE(channel_->RemoveRecvStream(1));
   1418 }
   1419 
   1420 TEST_F(WebRtcVideoEngineTestFake, AddRemoveSendStreamWith0Ssrc) {
   1421   EXPECT_TRUE(SetupEngine());
   1422   EXPECT_TRUE(channel_->AddSendStream(cricket::StreamParams::CreateLegacy(1)));
   1423   EXPECT_FALSE(channel_->AddSendStream(cricket::StreamParams::CreateLegacy(0)));
   1424   EXPECT_FALSE(channel_->RemoveSendStream(0));
   1425   EXPECT_TRUE(channel_->RemoveSendStream(1));
   1426 }
   1427 
   1428 // Test that we can create a channel and start/stop rendering out on it.
   1429 TEST_F(WebRtcVideoEngineTestFake, SetRender) {
   1430   EXPECT_TRUE(SetupEngine());
   1431   int channel_num = vie_.GetLastChannel();
   1432 
   1433   // Verify we can start/stop/start/stop rendering.
   1434   EXPECT_TRUE(channel_->SetRender(true));
   1435   EXPECT_TRUE(vie_.GetRenderStarted(channel_num));
   1436   EXPECT_TRUE(channel_->SetRender(false));
   1437   EXPECT_FALSE(vie_.GetRenderStarted(channel_num));
   1438   EXPECT_TRUE(channel_->SetRender(true));
   1439   EXPECT_TRUE(vie_.GetRenderStarted(channel_num));
   1440   EXPECT_TRUE(channel_->SetRender(false));
   1441   EXPECT_FALSE(vie_.GetRenderStarted(channel_num));
   1442 }
   1443 
   1444 // Test that we can create a channel and start/stop sending out on it.
   1445 TEST_F(WebRtcVideoEngineTestFake, SetSend) {
   1446   EXPECT_TRUE(SetupEngine());
   1447   int channel_num = vie_.GetLastChannel();
   1448   // Verify receiving is also started.
   1449   EXPECT_TRUE(vie_.GetReceive(channel_num));
   1450 
   1451   // Set send codecs on the channel.
   1452   std::vector<cricket::VideoCodec> codecs;
   1453   codecs.push_back(kVP8Codec);
   1454   EXPECT_TRUE(channel_->SetSendCodecs(codecs));
   1455   EXPECT_TRUE(channel_->AddSendStream(
   1456       cricket::StreamParams::CreateLegacy(123)));
   1457 
   1458   // Verify we can start/stop/start/stop sending.
   1459   EXPECT_TRUE(channel_->SetSend(true));
   1460   EXPECT_TRUE(vie_.GetSend(channel_num));
   1461   EXPECT_TRUE(channel_->SetSend(false));
   1462   EXPECT_FALSE(vie_.GetSend(channel_num));
   1463   EXPECT_TRUE(channel_->SetSend(true));
   1464   EXPECT_TRUE(vie_.GetSend(channel_num));
   1465   EXPECT_TRUE(channel_->SetSend(false));
   1466   EXPECT_FALSE(vie_.GetSend(channel_num));
   1467 }
   1468 
   1469 // Test that we set bandwidth properly when using full auto bandwidth mode.
   1470 TEST_F(WebRtcVideoEngineTestFake, SetBandwidthAuto) {
   1471   EXPECT_TRUE(SetupEngine());
   1472   int channel_num = vie_.GetLastChannel();
   1473   EXPECT_TRUE(channel_->SetSendCodecs(engine_.codecs()));
   1474   EXPECT_TRUE(channel_->SetMaxSendBandwidth(cricket::kAutoBandwidth));
   1475   VerifyVP8SendCodec(channel_num, kVP8Codec.width, kVP8Codec.height);
   1476 }
   1477 
   1478 // Test that we set bandwidth properly when using auto with upper bound.
   1479 TEST_F(WebRtcVideoEngineTestFake, SetBandwidthCapped) {
   1480   EXPECT_TRUE(SetupEngine());
   1481   int channel_num = vie_.GetLastChannel();
   1482   EXPECT_TRUE(channel_->SetSendCodecs(engine_.codecs()));
   1483   EXPECT_TRUE(channel_->SetMaxSendBandwidth(768000));
   1484   VerifyVP8SendCodec(channel_num, kVP8Codec.width, kVP8Codec.height, 0, 768U);
   1485 }
   1486 
   1487 // Test that we reduce the start bandwidth when the requested max is less than
   1488 // the default start bandwidth.
   1489 TEST_F(WebRtcVideoEngineTestFake, SetMaxBandwidthBelowDefaultStart) {
   1490   EXPECT_TRUE(SetupEngine());
   1491   int channel_num = vie_.GetLastChannel();
   1492   EXPECT_TRUE(channel_->SetSendCodecs(engine_.codecs()));
   1493   int max_bandwidth_kbps = (kMinBandwidthKbps + kStartBandwidthKbps) / 2;
   1494   EXPECT_TRUE(channel_->SetMaxSendBandwidth(max_bandwidth_kbps * 1000));
   1495   VerifyVP8SendCodec(channel_num, kVP8Codec.width, kVP8Codec.height, 0,
   1496       max_bandwidth_kbps, kMinBandwidthKbps, max_bandwidth_kbps);
   1497 }
   1498 
   1499 // Test that we reduce the min bandwidth when the requested max is less than
   1500 // the min bandwidth.
   1501 TEST_F(WebRtcVideoEngineTestFake, SetMaxBandwidthBelowMin) {
   1502   EXPECT_TRUE(SetupEngine());
   1503   int channel_num = vie_.GetLastChannel();
   1504   EXPECT_TRUE(channel_->SetSendCodecs(engine_.codecs()));
   1505   int max_bandwidth_kbps = kMinBandwidthKbps / 2;
   1506   EXPECT_TRUE(channel_->SetMaxSendBandwidth(max_bandwidth_kbps * 1000));
   1507   VerifyVP8SendCodec(channel_num, kVP8Codec.width, kVP8Codec.height, 0,
   1508       max_bandwidth_kbps, max_bandwidth_kbps, max_bandwidth_kbps);
   1509 }
   1510 
   1511 // Test that the start bandwidth can be controlled separately from the max
   1512 // bandwidth.
   1513 TEST_F(WebRtcVideoEngineTestFake, SetStartBandwidth) {
   1514   EXPECT_TRUE(SetupEngine());
   1515   int channel_num = vie_.GetLastChannel();
   1516   EXPECT_TRUE(channel_->SetSendCodecs(engine_.codecs()));
   1517   int start_bandwidth_kbps = kStartBandwidthKbps + 1;
   1518   EXPECT_TRUE(channel_->SetStartSendBandwidth(start_bandwidth_kbps * 1000));
   1519   VerifyVP8SendCodec(channel_num, kVP8Codec.width, kVP8Codec.height, 0,
   1520       kMaxBandwidthKbps, kMinBandwidthKbps, start_bandwidth_kbps);
   1521 
   1522   // Check that SetMaxSendBandwidth doesn't overwrite the start bandwidth.
   1523   int max_bandwidth_kbps = kMaxBandwidthKbps + 1;
   1524   EXPECT_TRUE(channel_->SetMaxSendBandwidth(max_bandwidth_kbps * 1000));
   1525   VerifyVP8SendCodec(channel_num, kVP8Codec.width, kVP8Codec.height, 0,
   1526       max_bandwidth_kbps, kMinBandwidthKbps, start_bandwidth_kbps);
   1527 }
   1528 
   1529 // Test that the start bandwidth can be controlled by experiment.
   1530 TEST_F(WebRtcVideoEngineTestFake, SetStartBandwidthOption) {
   1531   EXPECT_TRUE(SetupEngine());
   1532   int channel_num = vie_.GetLastChannel();
   1533   EXPECT_TRUE(channel_->SetSendCodecs(engine_.codecs()));
   1534   int start_bandwidth_kbps = kStartBandwidthKbps;
   1535   EXPECT_TRUE(channel_->SetStartSendBandwidth(start_bandwidth_kbps * 1000));
   1536   VerifyVP8SendCodec(channel_num, kVP8Codec.width, kVP8Codec.height, 0,
   1537       kMaxBandwidthKbps, kMinBandwidthKbps, start_bandwidth_kbps);
   1538 
   1539   // Set the start bitrate option.
   1540   start_bandwidth_kbps = 1000;
   1541   cricket::VideoOptions options;
   1542   options.video_start_bitrate.Set(
   1543       start_bandwidth_kbps);
   1544   EXPECT_TRUE(channel_->SetOptions(options));
   1545 
   1546   // Check that start bitrate has changed to the new value.
   1547   VerifyVP8SendCodec(channel_num, kVP8Codec.width, kVP8Codec.height, 0,
   1548       kMaxBandwidthKbps, kMinBandwidthKbps, start_bandwidth_kbps);
   1549 }
   1550 
   1551 // Test that SetMaxSendBandwidth works as expected in conference mode.
   1552 TEST_F(WebRtcVideoEngineTestFake, SetBandwidthInConference) {
   1553   EXPECT_TRUE(SetupEngine());
   1554   int channel_num = vie_.GetLastChannel();
   1555   cricket::VideoOptions options;
   1556   options.conference_mode.Set(true);
   1557   EXPECT_TRUE(channel_->SetOptions(options));
   1558   EXPECT_TRUE(channel_->SetSendCodecs(engine_.codecs()));
   1559   VerifyVP8SendCodec(channel_num, kVP8Codec.width, kVP8Codec.height);
   1560 
   1561   // Set send bandwidth.
   1562   EXPECT_TRUE(channel_->SetMaxSendBandwidth(768000));
   1563 
   1564   // Verify that the max bitrate has changed.
   1565   VerifyVP8SendCodec(channel_num, kVP8Codec.width, kVP8Codec.height, 0,
   1566                      768, kMinBandwidthKbps, kStartBandwidthKbps);
   1567 }
   1568 
   1569 
   1570 // Test that sending screencast frames doesn't change bitrate.
   1571 TEST_F(WebRtcVideoEngineTestFake, SetBandwidthScreencast) {
   1572   EXPECT_TRUE(SetupEngine());
   1573   int channel_num = vie_.GetLastChannel();
   1574 
   1575   // Set send codec.
   1576   cricket::VideoCodec codec(kVP8Codec);
   1577   std::vector<cricket::VideoCodec> codec_list;
   1578   codec_list.push_back(codec);
   1579   EXPECT_TRUE(channel_->AddSendStream(
   1580       cricket::StreamParams::CreateLegacy(123)));
   1581   EXPECT_TRUE(channel_->SetSendCodecs(codec_list));
   1582   EXPECT_TRUE(channel_->SetMaxSendBandwidth(111000));
   1583   EXPECT_TRUE(channel_->SetSend(true));
   1584 
   1585   SendI420ScreencastFrame(kVP8Codec.width, kVP8Codec.height);
   1586   VerifyVP8SendCodec(channel_num, kVP8Codec.width, kVP8Codec.height, 0, 111);
   1587 }
   1588 
   1589 
   1590 // Test SetSendSsrc.
   1591 TEST_F(WebRtcVideoEngineTestFake, SetSendSsrcAndCname) {
   1592   EXPECT_TRUE(SetupEngine());
   1593   int channel_num = vie_.GetLastChannel();
   1594 
   1595   cricket::StreamParams stream;
   1596   stream.ssrcs.push_back(1234);
   1597   stream.cname = "cname";
   1598   channel_->AddSendStream(stream);
   1599 
   1600   unsigned int ssrc = 0;
   1601   EXPECT_EQ(0, vie_.GetLocalSSRC(channel_num, ssrc));
   1602   EXPECT_EQ(1234U, ssrc);
   1603   EXPECT_EQ(1, vie_.GetNumSsrcs(channel_num));
   1604 
   1605   char rtcp_cname[256];
   1606   EXPECT_EQ(0, vie_.GetRTCPCName(channel_num, rtcp_cname));
   1607   EXPECT_STREQ("cname", rtcp_cname);
   1608 }
   1609 
   1610 
   1611 // Test that the local SSRC is the same on sending and receiving channels if the
   1612 // receive channel is created before the send channel.
   1613 TEST_F(WebRtcVideoEngineTestFake, SetSendSsrcAfterCreatingReceiveChannel) {
   1614   EXPECT_TRUE(SetupEngine());
   1615 
   1616   EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(1)));
   1617   int receive_channel_num = vie_.GetLastChannel();
   1618   cricket::StreamParams stream = cricket::StreamParams::CreateLegacy(1234);
   1619   EXPECT_TRUE(channel_->AddSendStream(stream));
   1620   int send_channel_num = vie_.GetLastChannel();
   1621   unsigned int ssrc = 0;
   1622   EXPECT_EQ(0, vie_.GetLocalSSRC(send_channel_num, ssrc));
   1623   EXPECT_EQ(1234U, ssrc);
   1624   EXPECT_EQ(1, vie_.GetNumSsrcs(send_channel_num));
   1625   ssrc = 0;
   1626   EXPECT_EQ(0, vie_.GetLocalSSRC(receive_channel_num, ssrc));
   1627   EXPECT_EQ(1234U, ssrc);
   1628   EXPECT_EQ(1, vie_.GetNumSsrcs(receive_channel_num));
   1629 }
   1630 
   1631 
   1632 // Test SetOptions with denoising flag.
   1633 TEST_F(WebRtcVideoEngineTestFake, SetOptionsWithDenoising) {
   1634   EXPECT_TRUE(SetupEngine());
   1635   EXPECT_EQ(1, vie_.GetNumCapturers());
   1636   int channel_num = vie_.GetLastChannel();
   1637   int capture_id = vie_.GetCaptureId(channel_num);
   1638   // Set send codecs on the channel.
   1639   std::vector<cricket::VideoCodec> codecs;
   1640   codecs.push_back(kVP8Codec);
   1641   EXPECT_TRUE(channel_->SetSendCodecs(codecs));
   1642 
   1643   // Set options with OPT_VIDEO_NOISE_REDUCTION flag.
   1644   cricket::VideoOptions options;
   1645   options.video_noise_reduction.Set(true);
   1646   EXPECT_TRUE(channel_->SetOptions(options));
   1647 
   1648   // Verify capture has denoising turned on.
   1649   webrtc::VideoCodec send_codec;
   1650   memset(&send_codec, 0, sizeof(send_codec));  // avoid uninitialized warning
   1651   EXPECT_EQ(0, vie_.GetSendCodec(channel_num, send_codec));
   1652   EXPECT_TRUE(send_codec.codecSpecific.VP8.denoisingOn);
   1653   EXPECT_FALSE(vie_.GetCaptureDenoising(capture_id));
   1654 
   1655   // Set options back to zero.
   1656   options.video_noise_reduction.Set(false);
   1657   EXPECT_TRUE(channel_->SetOptions(options));
   1658 
   1659   // Verify capture has denoising turned off.
   1660   EXPECT_EQ(0, vie_.GetSendCodec(channel_num, send_codec));
   1661   EXPECT_FALSE(send_codec.codecSpecific.VP8.denoisingOn);
   1662   EXPECT_FALSE(vie_.GetCaptureDenoising(capture_id));
   1663 }
   1664 
   1665 TEST_F(WebRtcVideoEngineTestFake, MultipleSendStreamsWithOneCapturer) {
   1666   EXPECT_TRUE(SetupEngine());
   1667 
   1668   // Start the capturer
   1669   cricket::FakeVideoCapturer capturer;
   1670   cricket::VideoFormat capture_format_vga = cricket::VideoFormat(640, 480,
   1671         cricket::VideoFormat::FpsToInterval(30), cricket::FOURCC_I420);
   1672   EXPECT_EQ(cricket::CS_RUNNING, capturer.Start(capture_format_vga));
   1673 
   1674   // Add send streams and connect the capturer
   1675   for (unsigned int i = 0; i < sizeof(kSsrcs2)/sizeof(kSsrcs2[0]); ++i) {
   1676     EXPECT_TRUE(channel_->AddSendStream(
   1677         cricket::StreamParams::CreateLegacy(kSsrcs2[i])));
   1678     // Register the capturer to the ssrc.
   1679     EXPECT_TRUE(channel_->SetCapturer(kSsrcs2[i], &capturer));
   1680   }
   1681 
   1682   const int channel0 = vie_.GetChannelFromLocalSsrc(kSsrcs2[0]);
   1683   ASSERT_NE(-1, channel0);
   1684   const int channel1 = vie_.GetChannelFromLocalSsrc(kSsrcs2[1]);
   1685   ASSERT_NE(-1, channel1);
   1686   ASSERT_NE(channel0, channel1);
   1687 
   1688   // Both channels should have started receiving after created.
   1689   EXPECT_TRUE(vie_.GetReceive(channel0));
   1690   EXPECT_TRUE(vie_.GetReceive(channel1));
   1691 
   1692   // Set send codec.
   1693   std::vector<cricket::VideoCodec> codecs;
   1694   cricket::VideoCodec send_codec(100, "VP8", 640, 480, 30, 0);
   1695   codecs.push_back(send_codec);
   1696   EXPECT_TRUE(channel_->SetSendCodecs(codecs));
   1697 
   1698   EXPECT_TRUE(channel_->SetSend(true));
   1699   EXPECT_TRUE(vie_.GetSend(channel0));
   1700   EXPECT_TRUE(vie_.GetSend(channel1));
   1701 
   1702   EXPECT_TRUE(capturer.CaptureFrame());
   1703   EXPECT_EQ(1, vie_.GetIncomingFrameNum(channel0));
   1704   EXPECT_EQ(1, vie_.GetIncomingFrameNum(channel1));
   1705 
   1706   EXPECT_TRUE(channel_->RemoveSendStream(kSsrcs2[0]));
   1707   EXPECT_TRUE(capturer.CaptureFrame());
   1708   // channel0 is the default channel, so it won't be deleted.
   1709   // But it should be disconnected from the capturer.
   1710   EXPECT_EQ(1, vie_.GetIncomingFrameNum(channel0));
   1711   EXPECT_EQ(2, vie_.GetIncomingFrameNum(channel1));
   1712 
   1713   EXPECT_TRUE(channel_->RemoveSendStream(kSsrcs2[1]));
   1714   EXPECT_TRUE(capturer.CaptureFrame());
   1715   EXPECT_EQ(1, vie_.GetIncomingFrameNum(channel0));
   1716   // channel1 has already been deleted.
   1717   EXPECT_EQ(-1, vie_.GetIncomingFrameNum(channel1));
   1718 }
   1719 
   1720 
   1721 TEST_F(WebRtcVideoEngineTestFake, SendReceiveBitratesStats) {
   1722   EXPECT_TRUE(SetupEngine());
   1723   cricket::VideoOptions options;
   1724   options.conference_mode.Set(true);
   1725   EXPECT_TRUE(channel_->SetOptions(options));
   1726   EXPECT_TRUE(channel_->AddSendStream(
   1727       cricket::StreamParams::CreateLegacy(1)));
   1728   int first_send_channel = vie_.GetLastChannel();
   1729   EXPECT_TRUE(channel_->AddSendStream(
   1730       cricket::StreamParams::CreateLegacy(2)));
   1731   int second_send_channel = vie_.GetLastChannel();
   1732   cricket::VideoCodec codec(kVP8Codec720p);
   1733   std::vector<cricket::VideoCodec> codec_list;
   1734   codec_list.push_back(codec);
   1735   EXPECT_TRUE(channel_->SetSendCodecs(codec_list));
   1736 
   1737   EXPECT_TRUE(channel_->AddRecvStream(
   1738       cricket::StreamParams::CreateLegacy(3)));
   1739   int first_receive_channel = vie_.GetLastChannel();
   1740   EXPECT_NE(first_send_channel, first_receive_channel);
   1741   EXPECT_TRUE(channel_->AddRecvStream(
   1742       cricket::StreamParams::CreateLegacy(4)));
   1743   int second_receive_channel = vie_.GetLastChannel();
   1744   EXPECT_NE(first_receive_channel, second_receive_channel);
   1745 
   1746   cricket::VideoMediaInfo info;
   1747   EXPECT_TRUE(channel_->GetStats(cricket::StatsOptions(), &info));
   1748   ASSERT_EQ(1U, info.bw_estimations.size());
   1749   ASSERT_EQ(0, info.bw_estimations[0].actual_enc_bitrate);
   1750   ASSERT_EQ(0, info.bw_estimations[0].transmit_bitrate);
   1751   ASSERT_EQ(0, info.bw_estimations[0].retransmit_bitrate);
   1752   ASSERT_EQ(0, info.bw_estimations[0].available_send_bandwidth);
   1753   ASSERT_EQ(0, info.bw_estimations[0].available_recv_bandwidth);
   1754   ASSERT_EQ(0, info.bw_estimations[0].target_enc_bitrate);
   1755 
   1756   // Start sending and receiving on one of the channels and verify bitrates.
   1757   EXPECT_EQ(0, vie_.StartSend(first_send_channel));
   1758   int send_video_bitrate = 800;
   1759   int send_fec_bitrate = 100;
   1760   int send_nack_bitrate = 20;
   1761   int send_total_bitrate = send_video_bitrate + send_fec_bitrate +
   1762       send_nack_bitrate;
   1763   int send_bandwidth = 1900;
   1764   vie_.SetSendBitrates(first_send_channel, send_video_bitrate, send_fec_bitrate,
   1765                        send_nack_bitrate);
   1766   vie_.SetSendBandwidthEstimate(first_send_channel, send_bandwidth);
   1767 
   1768   EXPECT_EQ(0, vie_.StartReceive(first_receive_channel));
   1769   int receive_bandwidth = 600;
   1770   vie_.SetReceiveBandwidthEstimate(first_receive_channel, receive_bandwidth);
   1771 
   1772   info.Clear();
   1773   EXPECT_TRUE(channel_->GetStats(cricket::StatsOptions(), &info));
   1774   ASSERT_EQ(1U, info.bw_estimations.size());
   1775   ASSERT_EQ(send_video_bitrate, info.bw_estimations[0].actual_enc_bitrate);
   1776   ASSERT_EQ(send_total_bitrate, info.bw_estimations[0].transmit_bitrate);
   1777   ASSERT_EQ(send_nack_bitrate, info.bw_estimations[0].retransmit_bitrate);
   1778   ASSERT_EQ(send_bandwidth, info.bw_estimations[0].available_send_bandwidth);
   1779   ASSERT_EQ(receive_bandwidth, info.bw_estimations[0].available_recv_bandwidth);
   1780   ASSERT_EQ(send_video_bitrate, info.bw_estimations[0].target_enc_bitrate);
   1781 
   1782   // Start receiving on the second channel and verify received rate.
   1783   EXPECT_EQ(0, vie_.StartSend(second_send_channel));
   1784   vie_.SetSendBitrates(second_send_channel,
   1785                        send_video_bitrate,
   1786                        send_fec_bitrate,
   1787                        send_nack_bitrate);
   1788   EXPECT_EQ(0, vie_.StartReceive(second_receive_channel));
   1789 
   1790   info.Clear();
   1791   EXPECT_TRUE(channel_->GetStats(cricket::StatsOptions(), &info));
   1792   ASSERT_EQ(1U, info.bw_estimations.size());
   1793   ASSERT_EQ(2 * send_video_bitrate, info.bw_estimations[0].actual_enc_bitrate);
   1794   ASSERT_EQ(2 * send_total_bitrate, info.bw_estimations[0].transmit_bitrate);
   1795   ASSERT_EQ(2 * send_nack_bitrate, info.bw_estimations[0].retransmit_bitrate);
   1796   ASSERT_EQ(send_bandwidth, info.bw_estimations[0].available_send_bandwidth);
   1797   ASSERT_EQ(receive_bandwidth, info.bw_estimations[0].available_recv_bandwidth);
   1798   ASSERT_EQ(2 * send_video_bitrate, info.bw_estimations[0].target_enc_bitrate);
   1799 }
   1800 
   1801 TEST_F(WebRtcVideoEngineTestFake, TestSetAdaptInputToCpuUsage) {
   1802   EXPECT_TRUE(SetupEngine());
   1803   cricket::VideoOptions options_in, options_out;
   1804   bool cpu_adapt = false;
   1805   channel_->SetOptions(options_in);
   1806   EXPECT_TRUE(channel_->GetOptions(&options_out));
   1807   EXPECT_FALSE(options_out.adapt_input_to_cpu_usage.Get(&cpu_adapt));
   1808   // Set adapt input CPU usage option.
   1809   options_in.adapt_input_to_cpu_usage.Set(true);
   1810   EXPECT_TRUE(channel_->SetOptions(options_in));
   1811   EXPECT_TRUE(channel_->GetOptions(&options_out));
   1812   EXPECT_TRUE(options_out.adapt_input_to_cpu_usage.Get(&cpu_adapt));
   1813   EXPECT_TRUE(cpu_adapt);
   1814 }
   1815 
   1816 TEST_F(WebRtcVideoEngineTestFake, TestSetCpuThreshold) {
   1817   EXPECT_TRUE(SetupEngine());
   1818   float low, high;
   1819   cricket::VideoOptions options_in, options_out;
   1820   // Verify that initial values are set.
   1821   EXPECT_TRUE(channel_->GetOptions(&options_out));
   1822   EXPECT_TRUE(options_out.system_low_adaptation_threshhold.Get(&low));
   1823   EXPECT_EQ(low, 0.65f);
   1824   EXPECT_TRUE(options_out.system_high_adaptation_threshhold.Get(&high));
   1825   EXPECT_EQ(high, 0.85f);
   1826   // Set new CPU threshold values.
   1827   options_in.system_low_adaptation_threshhold.Set(0.45f);
   1828   options_in.system_high_adaptation_threshhold.Set(0.95f);
   1829   EXPECT_TRUE(channel_->SetOptions(options_in));
   1830   EXPECT_TRUE(channel_->GetOptions(&options_out));
   1831   EXPECT_TRUE(options_out.system_low_adaptation_threshhold.Get(&low));
   1832   EXPECT_EQ(low, 0.45f);
   1833   EXPECT_TRUE(options_out.system_high_adaptation_threshhold.Get(&high));
   1834   EXPECT_EQ(high, 0.95f);
   1835 }
   1836 
   1837 TEST_F(WebRtcVideoEngineTestFake, TestSetInvalidCpuThreshold) {
   1838   EXPECT_TRUE(SetupEngine());
   1839   float low, high;
   1840   cricket::VideoOptions options_in, options_out;
   1841   // Valid range is [0, 1].
   1842   options_in.system_low_adaptation_threshhold.Set(-1.5f);
   1843   options_in.system_high_adaptation_threshhold.Set(1.5f);
   1844   EXPECT_TRUE(channel_->SetOptions(options_in));
   1845   EXPECT_TRUE(channel_->GetOptions(&options_out));
   1846   EXPECT_TRUE(options_out.system_low_adaptation_threshhold.Get(&low));
   1847   EXPECT_EQ(low, 0.0f);
   1848   EXPECT_TRUE(options_out.system_high_adaptation_threshhold.Get(&high));
   1849   EXPECT_EQ(high, 1.0f);
   1850 }
   1851 
   1852 
   1853 TEST_F(WebRtcVideoEngineTestFake, ResetCodecOnScreencast) {
   1854   EXPECT_TRUE(SetupEngine());
   1855   cricket::VideoOptions options;
   1856   options.video_noise_reduction.Set(true);
   1857   EXPECT_TRUE(channel_->SetOptions(options));
   1858 
   1859   // Set send codec.
   1860   cricket::VideoCodec codec(kVP8Codec);
   1861   std::vector<cricket::VideoCodec> codec_list;
   1862   codec_list.push_back(codec);
   1863   EXPECT_TRUE(channel_->AddSendStream(
   1864       cricket::StreamParams::CreateLegacy(123)));
   1865   EXPECT_TRUE(channel_->SetSendCodecs(codec_list));
   1866   EXPECT_TRUE(channel_->SetSend(true));
   1867   EXPECT_EQ(1, vie_.GetNumSetSendCodecs());
   1868 
   1869   webrtc::VideoCodec gcodec;
   1870   memset(&gcodec, 0, sizeof(gcodec));
   1871   int channel_num = vie_.GetLastChannel();
   1872   EXPECT_EQ(0, vie_.GetSendCodec(channel_num, gcodec));
   1873   EXPECT_TRUE(gcodec.codecSpecific.VP8.denoisingOn);
   1874 
   1875   // Send a screencast frame with the same size.
   1876   // Verify that denoising is turned off.
   1877   SendI420ScreencastFrame(kVP8Codec.width, kVP8Codec.height);
   1878   EXPECT_EQ(2, vie_.GetNumSetSendCodecs());
   1879   EXPECT_EQ(0, vie_.GetSendCodec(channel_num, gcodec));
   1880   EXPECT_FALSE(gcodec.codecSpecific.VP8.denoisingOn);
   1881 }
   1882 
   1883 
   1884 TEST_F(WebRtcVideoEngineTestFake, DontRegisterDecoderIfFactoryIsNotGiven) {
   1885   engine_.SetExternalDecoderFactory(NULL);
   1886   EXPECT_TRUE(SetupEngine());
   1887   int channel_num = vie_.GetLastChannel();
   1888 
   1889   std::vector<cricket::VideoCodec> codecs;
   1890   codecs.push_back(kVP8Codec);
   1891   EXPECT_TRUE(channel_->SetRecvCodecs(codecs));
   1892 
   1893   EXPECT_EQ(0, vie_.GetNumExternalDecoderRegistered(channel_num));
   1894 }
   1895 
   1896 TEST_F(WebRtcVideoEngineTestFake, RegisterDecoderIfFactoryIsGiven) {
   1897   decoder_factory_.AddSupportedVideoCodecType(webrtc::kVideoCodecVP8);
   1898   engine_.SetExternalDecoderFactory(&decoder_factory_);
   1899   EXPECT_TRUE(SetupEngine());
   1900   int channel_num = vie_.GetLastChannel();
   1901 
   1902   std::vector<cricket::VideoCodec> codecs;
   1903   codecs.push_back(kVP8Codec);
   1904   EXPECT_TRUE(channel_->SetRecvCodecs(codecs));
   1905 
   1906   EXPECT_TRUE(vie_.ExternalDecoderRegistered(channel_num, 100));
   1907   EXPECT_EQ(1, vie_.GetNumExternalDecoderRegistered(channel_num));
   1908 }
   1909 
   1910 TEST_F(WebRtcVideoEngineTestFake, DontRegisterDecoderMultipleTimes) {
   1911   decoder_factory_.AddSupportedVideoCodecType(webrtc::kVideoCodecVP8);
   1912   engine_.SetExternalDecoderFactory(&decoder_factory_);
   1913   EXPECT_TRUE(SetupEngine());
   1914   int channel_num = vie_.GetLastChannel();
   1915 
   1916   std::vector<cricket::VideoCodec> codecs;
   1917   codecs.push_back(kVP8Codec);
   1918   EXPECT_TRUE(channel_->SetRecvCodecs(codecs));
   1919 
   1920   EXPECT_TRUE(vie_.ExternalDecoderRegistered(channel_num, 100));
   1921   EXPECT_EQ(1, vie_.GetNumExternalDecoderRegistered(channel_num));
   1922   EXPECT_EQ(1, decoder_factory_.GetNumCreatedDecoders());
   1923 
   1924   EXPECT_TRUE(channel_->SetRecvCodecs(codecs));
   1925   EXPECT_EQ(1, vie_.GetNumExternalDecoderRegistered(channel_num));
   1926   EXPECT_EQ(1, decoder_factory_.GetNumCreatedDecoders());
   1927 }
   1928 
   1929 TEST_F(WebRtcVideoEngineTestFake, DontRegisterDecoderForNonVP8) {
   1930   decoder_factory_.AddSupportedVideoCodecType(webrtc::kVideoCodecVP8);
   1931   engine_.SetExternalDecoderFactory(&decoder_factory_);
   1932   EXPECT_TRUE(SetupEngine());
   1933   int channel_num = vie_.GetLastChannel();
   1934 
   1935   std::vector<cricket::VideoCodec> codecs;
   1936   codecs.push_back(kRedCodec);
   1937   EXPECT_TRUE(channel_->SetRecvCodecs(codecs));
   1938 
   1939   EXPECT_EQ(0, vie_.GetNumExternalDecoderRegistered(channel_num));
   1940 }
   1941 
   1942 TEST_F(WebRtcVideoEngineTestFake, DontRegisterEncoderIfFactoryIsNotGiven) {
   1943   engine_.SetExternalEncoderFactory(NULL);
   1944   EXPECT_TRUE(SetupEngine());
   1945   int channel_num = vie_.GetLastChannel();
   1946 
   1947   std::vector<cricket::VideoCodec> codecs;
   1948   codecs.push_back(kVP8Codec);
   1949   EXPECT_TRUE(channel_->SetSendCodecs(codecs));
   1950 
   1951   EXPECT_EQ(0, vie_.GetNumExternalEncoderRegistered(channel_num));
   1952 }
   1953 
   1954 TEST_F(WebRtcVideoEngineTestFake, RegisterEncoderIfFactoryIsGiven) {
   1955   encoder_factory_.AddSupportedVideoCodecType(webrtc::kVideoCodecVP8, "VP8");
   1956   engine_.SetExternalEncoderFactory(&encoder_factory_);
   1957   EXPECT_TRUE(SetupEngine());
   1958   int channel_num = vie_.GetLastChannel();
   1959 
   1960   std::vector<cricket::VideoCodec> codecs;
   1961   codecs.push_back(kVP8Codec);
   1962   EXPECT_TRUE(channel_->SetSendCodecs(codecs));
   1963 
   1964   EXPECT_TRUE(channel_->AddSendStream(
   1965       cricket::StreamParams::CreateLegacy(kSsrc)));
   1966 
   1967   EXPECT_TRUE(vie_.ExternalEncoderRegistered(channel_num, 100));
   1968   EXPECT_EQ(1, vie_.GetNumExternalEncoderRegistered(channel_num));
   1969 
   1970   // Remove stream previously added to free the external encoder instance.
   1971   EXPECT_TRUE(channel_->RemoveSendStream(kSsrc));
   1972 }
   1973 
   1974 TEST_F(WebRtcVideoEngineTestFake, DontRegisterEncoderMultipleTimes) {
   1975   encoder_factory_.AddSupportedVideoCodecType(webrtc::kVideoCodecVP8, "VP8");
   1976   engine_.SetExternalEncoderFactory(&encoder_factory_);
   1977   EXPECT_TRUE(SetupEngine());
   1978   int channel_num = vie_.GetLastChannel();
   1979 
   1980   std::vector<cricket::VideoCodec> codecs;
   1981   codecs.push_back(kVP8Codec);
   1982   EXPECT_TRUE(channel_->SetSendCodecs(codecs));
   1983 
   1984   EXPECT_TRUE(channel_->AddSendStream(
   1985       cricket::StreamParams::CreateLegacy(kSsrc)));
   1986 
   1987   EXPECT_TRUE(vie_.ExternalEncoderRegistered(channel_num, 100));
   1988   EXPECT_EQ(1, vie_.GetNumExternalEncoderRegistered(channel_num));
   1989 
   1990   EXPECT_TRUE(channel_->SetSendCodecs(codecs));
   1991   EXPECT_EQ(1, vie_.GetNumExternalEncoderRegistered(channel_num));
   1992 
   1993   // Remove stream previously added to free the external encoder instance.
   1994   EXPECT_TRUE(channel_->RemoveSendStream(kSsrc));
   1995 }
   1996 
   1997 TEST_F(WebRtcVideoEngineTestFake, RegisterEncoderWithMultipleSendStreams) {
   1998   encoder_factory_.AddSupportedVideoCodecType(webrtc::kVideoCodecVP8, "VP8");
   1999   engine_.SetExternalEncoderFactory(&encoder_factory_);
   2000   EXPECT_TRUE(SetupEngine());
   2001 
   2002   std::vector<cricket::VideoCodec> codecs;
   2003   codecs.push_back(kVP8Codec);
   2004   EXPECT_TRUE(channel_->SetSendCodecs(codecs));
   2005   EXPECT_EQ(1, vie_.GetTotalNumExternalEncoderRegistered());
   2006 
   2007   // When we add the first stream (1234), it reuses the default send channel,
   2008   // so it doesn't increase the registration count of external encoders.
   2009   EXPECT_TRUE(channel_->AddSendStream(
   2010       cricket::StreamParams::CreateLegacy(1234)));
   2011   EXPECT_EQ(1, vie_.GetTotalNumExternalEncoderRegistered());
   2012 
   2013   // When we add the second stream (2345), it creates a new channel and
   2014   // increments the registration count.
   2015   EXPECT_TRUE(channel_->AddSendStream(
   2016       cricket::StreamParams::CreateLegacy(2345)));
   2017   EXPECT_EQ(2, vie_.GetTotalNumExternalEncoderRegistered());
   2018 
   2019   // At this moment the total registration count is two, but only one encoder
   2020   // is registered per channel.
   2021   int channel_num = vie_.GetLastChannel();
   2022   EXPECT_EQ(1, vie_.GetNumExternalEncoderRegistered(channel_num));
   2023 
   2024   // Removing send streams decrements the registration count.
   2025   EXPECT_TRUE(channel_->RemoveSendStream(1234));
   2026   EXPECT_EQ(1, vie_.GetTotalNumExternalEncoderRegistered());
   2027 
   2028   // When we remove the last send stream, it also destroys the last send
   2029   // channel and causes the registration count to drop to zero. It is a little
   2030   // weird, but not a bug.
   2031   EXPECT_TRUE(channel_->RemoveSendStream(2345));
   2032   EXPECT_EQ(0, vie_.GetTotalNumExternalEncoderRegistered());
   2033 }
   2034 
   2035 TEST_F(WebRtcVideoEngineTestFake, DontRegisterEncoderForNonVP8) {
   2036   encoder_factory_.AddSupportedVideoCodecType(webrtc::kVideoCodecGeneric,
   2037                                               "GENERIC");
   2038   engine_.SetExternalEncoderFactory(&encoder_factory_);
   2039   EXPECT_TRUE(SetupEngine());
   2040   int channel_num = vie_.GetLastChannel();
   2041 
   2042   // Note: unlike the SetRecvCodecs, we must set a valid video codec for
   2043   // channel_->SetSendCodecs() to succeed.
   2044   std::vector<cricket::VideoCodec> codecs;
   2045   codecs.push_back(kVP8Codec);
   2046   EXPECT_TRUE(channel_->SetSendCodecs(codecs));
   2047 
   2048   EXPECT_EQ(0, vie_.GetNumExternalEncoderRegistered(channel_num));
   2049 }
   2050 
   2051 // Test that NACK, PLI and REMB are enabled for external codec.
   2052 TEST_F(WebRtcVideoEngineTestFake, ExternalCodecFeedbackParams) {
   2053   encoder_factory_.AddSupportedVideoCodecType(webrtc::kVideoCodecGeneric,
   2054                                               "GENERIC");
   2055   engine_.SetExternalEncoderFactory(&encoder_factory_);
   2056   encoder_factory_.NotifyCodecsAvailable();
   2057   EXPECT_TRUE(SetupEngine());
   2058 
   2059   std::vector<cricket::VideoCodec> codecs(engine_.codecs());
   2060   // The external codec will appear at last.
   2061   size_t pos = codecs.size() - 1;
   2062   EXPECT_EQ("GENERIC", codecs[pos].name);
   2063   VerifyCodecFeedbackParams(codecs[pos]);
   2064 }
   2065 
   2066 // Test external codec with be added to the end of the supported codec list.
   2067 TEST_F(WebRtcVideoEngineTestFake, ExternalCodecAddedToTheEnd) {
   2068   EXPECT_TRUE(SetupEngine());
   2069 
   2070   std::vector<cricket::VideoCodec> codecs(engine_.codecs());
   2071   EXPECT_EQ("VP8", codecs[0].name);
   2072 
   2073   encoder_factory_.AddSupportedVideoCodecType(webrtc::kVideoCodecGeneric,
   2074                                               "GENERIC");
   2075   engine_.SetExternalEncoderFactory(&encoder_factory_);
   2076   encoder_factory_.NotifyCodecsAvailable();
   2077 
   2078   codecs = engine_.codecs();
   2079   cricket::VideoCodec internal_codec = codecs[0];
   2080   cricket::VideoCodec external_codec = codecs[codecs.size() - 1];
   2081   // The external codec will appear at last.
   2082   EXPECT_EQ("GENERIC", external_codec.name);
   2083   // The internal codec is preferred.
   2084   EXPECT_GE(internal_codec.preference, external_codec.preference);
   2085 }
   2086 
   2087 // Test that external codec with be ignored if it has the same name as one of
   2088 // the internal codecs.
   2089 TEST_F(WebRtcVideoEngineTestFake, ExternalCodecIgnored) {
   2090   EXPECT_TRUE(SetupEngine());
   2091 
   2092   std::vector<cricket::VideoCodec> internal_codecs(engine_.codecs());
   2093   EXPECT_EQ("VP8", internal_codecs[0].name);
   2094 
   2095   encoder_factory_.AddSupportedVideoCodecType(webrtc::kVideoCodecVP8, "VP8");
   2096   engine_.SetExternalEncoderFactory(&encoder_factory_);
   2097   encoder_factory_.NotifyCodecsAvailable();
   2098 
   2099   std::vector<cricket::VideoCodec> codecs = engine_.codecs();
   2100   EXPECT_EQ("VP8", codecs[0].name);
   2101   EXPECT_EQ(internal_codecs[0].height, codecs[0].height);
   2102   EXPECT_EQ(internal_codecs[0].width, codecs[0].width);
   2103   // Verify the last codec is not the external codec.
   2104   EXPECT_NE("VP8", codecs[codecs.size() - 1].name);
   2105 }
   2106 
   2107 TEST_F(WebRtcVideoEngineTestFake, UpdateEncoderCodecsAfterSetFactory) {
   2108   engine_.SetExternalEncoderFactory(&encoder_factory_);
   2109   EXPECT_TRUE(SetupEngine());
   2110   int channel_num = vie_.GetLastChannel();
   2111 
   2112   encoder_factory_.AddSupportedVideoCodecType(webrtc::kVideoCodecVP8, "VP8");
   2113   encoder_factory_.NotifyCodecsAvailable();
   2114   std::vector<cricket::VideoCodec> codecs;
   2115   codecs.push_back(kVP8Codec);
   2116   EXPECT_TRUE(channel_->SetSendCodecs(codecs));
   2117 
   2118   EXPECT_TRUE(channel_->AddSendStream(
   2119       cricket::StreamParams::CreateLegacy(kSsrc)));
   2120 
   2121   EXPECT_TRUE(vie_.ExternalEncoderRegistered(channel_num, 100));
   2122   EXPECT_EQ(1, vie_.GetNumExternalEncoderRegistered(channel_num));
   2123 
   2124   // Remove stream previously added to free the external encoder instance.
   2125   EXPECT_TRUE(channel_->RemoveSendStream(kSsrc));
   2126 }
   2127 
   2128 // Tests that OnReadyToSend will be propagated into ViE.
   2129 TEST_F(WebRtcVideoEngineTestFake, OnReadyToSend) {
   2130   EXPECT_TRUE(SetupEngine());
   2131   int channel_num = vie_.GetLastChannel();
   2132   EXPECT_TRUE(vie_.GetIsTransmitting(channel_num));
   2133 
   2134   channel_->OnReadyToSend(false);
   2135   EXPECT_FALSE(vie_.GetIsTransmitting(channel_num));
   2136 
   2137   channel_->OnReadyToSend(true);
   2138   EXPECT_TRUE(vie_.GetIsTransmitting(channel_num));
   2139 }
   2140 
   2141 #if 0
   2142 TEST_F(WebRtcVideoEngineTestFake, CaptureFrameTimestampToNtpTimestamp) {
   2143   EXPECT_TRUE(SetupEngine());
   2144   int capture_id = vie_.GetCaptureId(vie_.GetLastChannel());
   2145 
   2146   // Set send codec.
   2147   cricket::VideoCodec codec(kVP8Codec);
   2148   std::vector<cricket::VideoCodec> codec_list;
   2149   codec_list.push_back(codec);
   2150   EXPECT_TRUE(channel_->AddSendStream(
   2151       cricket::StreamParams::CreateLegacy(123)));
   2152   EXPECT_TRUE(channel_->SetSendCodecs(codec_list));
   2153   EXPECT_TRUE(channel_->SetSend(true));
   2154 
   2155   int64 timestamp = time(NULL) * talk_base::kNumNanosecsPerSec;
   2156   SendI420ScreencastFrameWithTimestamp(
   2157       kVP8Codec.width, kVP8Codec.height, timestamp);
   2158   EXPECT_EQ(talk_base::UnixTimestampNanosecsToNtpMillisecs(timestamp),
   2159       vie_.GetCaptureLastTimestamp(capture_id));
   2160 
   2161   SendI420ScreencastFrameWithTimestamp(kVP8Codec.width, kVP8Codec.height, 0);
   2162   EXPECT_EQ(0, vie_.GetCaptureLastTimestamp(capture_id));
   2163 }
   2164 #endif
   2165 
   2166 /////////////////////////
   2167 // Tests with real ViE //
   2168 /////////////////////////
   2169 
   2170 // Tests that we can find codecs by name or id.
   2171 TEST_F(WebRtcVideoEngineTest, FindCodec) {
   2172   // We should not need to init engine in order to get codecs.
   2173   const std::vector<cricket::VideoCodec>& c = engine_.codecs();
   2174   EXPECT_EQ(4U, c.size());
   2175 
   2176   cricket::VideoCodec vp8(104, "VP8", 320, 200, 30, 0);
   2177   EXPECT_TRUE(engine_.FindCodec(vp8));
   2178 
   2179   cricket::VideoCodec vp8_ci(104, "vp8", 320, 200, 30, 0);
   2180   EXPECT_TRUE(engine_.FindCodec(vp8));
   2181 
   2182   cricket::VideoCodec vp8_diff_fr_diff_pref(104, "VP8", 320, 200, 50, 50);
   2183   EXPECT_TRUE(engine_.FindCodec(vp8_diff_fr_diff_pref));
   2184 
   2185   cricket::VideoCodec vp8_diff_id(95, "VP8", 320, 200, 30, 0);
   2186   EXPECT_FALSE(engine_.FindCodec(vp8_diff_id));
   2187   vp8_diff_id.id = 97;
   2188   EXPECT_TRUE(engine_.FindCodec(vp8_diff_id));
   2189 
   2190   cricket::VideoCodec vp8_diff_res(104, "VP8", 320, 111, 30, 0);
   2191   EXPECT_FALSE(engine_.FindCodec(vp8_diff_res));
   2192 
   2193   // PeerConnection doesn't negotiate the resolution at this point.
   2194   // Test that FindCodec can handle the case when width/height is 0.
   2195   cricket::VideoCodec vp8_zero_res(104, "VP8", 0, 0, 30, 0);
   2196   EXPECT_TRUE(engine_.FindCodec(vp8_zero_res));
   2197 
   2198   cricket::VideoCodec red(101, "RED", 0, 0, 30, 0);
   2199   EXPECT_TRUE(engine_.FindCodec(red));
   2200 
   2201   cricket::VideoCodec red_ci(101, "red", 0, 0, 30, 0);
   2202   EXPECT_TRUE(engine_.FindCodec(red));
   2203 
   2204   cricket::VideoCodec fec(102, "ULPFEC", 0, 0, 30, 0);
   2205   EXPECT_TRUE(engine_.FindCodec(fec));
   2206 
   2207   cricket::VideoCodec fec_ci(102, "ulpfec", 0, 0, 30, 0);
   2208   EXPECT_TRUE(engine_.FindCodec(fec));
   2209 
   2210   cricket::VideoCodec rtx(96, "rtx", 0, 0, 30, 0);
   2211   rtx.SetParam("apt", kVP8Codec.id);
   2212   EXPECT_TRUE(engine_.FindCodec(rtx));
   2213 }
   2214 
   2215 TEST_F(WebRtcVideoEngineTest, RtxCodecHasAptSet) {
   2216   std::vector<cricket::VideoCodec>::const_iterator it;
   2217   bool apt_checked = false;
   2218   for (it = engine_.codecs().begin(); it != engine_.codecs().end(); ++it) {
   2219     if (_stricmp(cricket::kRtxCodecName, it->name.c_str()) && it->id != 96) {
   2220       continue;
   2221     }
   2222     int apt;
   2223     EXPECT_TRUE(it->GetParam("apt", &apt));
   2224     EXPECT_EQ(100, apt);
   2225     apt_checked = true;
   2226   }
   2227   EXPECT_TRUE(apt_checked);
   2228 }
   2229 
   2230 TEST_F(WebRtcVideoEngineTest, StartupShutdown) {
   2231   EXPECT_TRUE(engine_.Init(talk_base::Thread::Current()));
   2232   engine_.Terminate();
   2233 }
   2234 
   2235 TEST_PRE_VIDEOENGINE_INIT(WebRtcVideoEngineTest, ConstrainNewCodec)
   2236 TEST_POST_VIDEOENGINE_INIT(WebRtcVideoEngineTest, ConstrainNewCodec)
   2237 
   2238 TEST_PRE_VIDEOENGINE_INIT(WebRtcVideoEngineTest, ConstrainRunningCodec)
   2239 TEST_POST_VIDEOENGINE_INIT(WebRtcVideoEngineTest, ConstrainRunningCodec)
   2240 
   2241 // TODO(juberti): Figure out why ViE is munging the COM refcount.
   2242 #ifdef WIN32
   2243 TEST_F(WebRtcVideoEngineTest, DISABLED_CheckCoInitialize) {
   2244   Base::CheckCoInitialize();
   2245 }
   2246 #endif
   2247 
   2248 TEST_F(WebRtcVideoEngineTest, CreateChannel) {
   2249   EXPECT_TRUE(engine_.Init(talk_base::Thread::Current()));
   2250   cricket::VideoMediaChannel* channel = engine_.CreateChannel(NULL);
   2251   EXPECT_TRUE(channel != NULL);
   2252   delete channel;
   2253 }
   2254 
   2255 TEST_F(WebRtcVideoMediaChannelTest, SetRecvCodecs) {
   2256   std::vector<cricket::VideoCodec> codecs;
   2257   codecs.push_back(kVP8Codec);
   2258   EXPECT_TRUE(channel_->SetRecvCodecs(codecs));
   2259 }
   2260 TEST_F(WebRtcVideoMediaChannelTest, SetRecvCodecsWrongPayloadType) {
   2261   std::vector<cricket::VideoCodec> codecs;
   2262   codecs.push_back(kVP8Codec);
   2263   codecs[0].id = 99;
   2264   EXPECT_TRUE(channel_->SetRecvCodecs(codecs));
   2265 }
   2266 TEST_F(WebRtcVideoMediaChannelTest, SetRecvCodecsUnsupportedCodec) {
   2267   std::vector<cricket::VideoCodec> codecs;
   2268   codecs.push_back(kVP8Codec);
   2269   codecs.push_back(cricket::VideoCodec(101, "VP1", 640, 400, 30, 0));
   2270   EXPECT_FALSE(channel_->SetRecvCodecs(codecs));
   2271 }
   2272 
   2273 TEST_F(WebRtcVideoMediaChannelTest, GetRtpSendTimeExtension) {
   2274   // Enable RTP timestamp extension.
   2275   const int id = 12;
   2276   std::vector<cricket::RtpHeaderExtension> extensions;
   2277   extensions.push_back(cricket::RtpHeaderExtension(
   2278       "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time", id));
   2279 
   2280   // Verify the send extension id.
   2281   EXPECT_TRUE(channel_->SetSendRtpHeaderExtensions(extensions));
   2282   EXPECT_EQ(id, channel_->GetRtpSendTimeExtnId());
   2283 }
   2284 
   2285 TEST_F(WebRtcVideoMediaChannelTest, SetSend) {
   2286   Base::SetSend();
   2287 }
   2288 TEST_F(WebRtcVideoMediaChannelTest, SetSendWithoutCodecs) {
   2289   Base::SetSendWithoutCodecs();
   2290 }
   2291 TEST_F(WebRtcVideoMediaChannelTest, SetSendSetsTransportBufferSizes) {
   2292   Base::SetSendSetsTransportBufferSizes();
   2293 }
   2294 
   2295 TEST_F(WebRtcVideoMediaChannelTest, SendAndReceiveVp8Vga) {
   2296   SendAndReceive(cricket::VideoCodec(100, "VP8", 640, 400, 30, 0));
   2297 }
   2298 TEST_F(WebRtcVideoMediaChannelTest, SendAndReceiveVp8Qvga) {
   2299   SendAndReceive(cricket::VideoCodec(100, "VP8", 320, 200, 30, 0));
   2300 }
   2301 TEST_F(WebRtcVideoMediaChannelTest, SendAndReceiveH264SvcQqvga) {
   2302   SendAndReceive(cricket::VideoCodec(100, "VP8", 160, 100, 30, 0));
   2303 }
   2304 TEST_F(WebRtcVideoMediaChannelTest, SendManyResizeOnce) {
   2305   SendManyResizeOnce();
   2306 }
   2307 
   2308 TEST_F(WebRtcVideoMediaChannelTest, DISABLED_SendVp8HdAndReceiveAdaptedVp8Vga) {
   2309   EXPECT_TRUE(channel_->SetCapturer(kSsrc, NULL));
   2310   channel_->UpdateAspectRatio(1280, 720);
   2311   video_capturer_.reset(new cricket::FakeVideoCapturer);
   2312   const std::vector<cricket::VideoFormat>* formats =
   2313       video_capturer_->GetSupportedFormats();
   2314   cricket::VideoFormat capture_format_hd = (*formats)[0];
   2315   EXPECT_EQ(cricket::CS_RUNNING, video_capturer_->Start(capture_format_hd));
   2316   EXPECT_TRUE(channel_->SetCapturer(kSsrc, video_capturer_.get()));
   2317 
   2318   // Capture format HD -> adapt (OnOutputFormatRequest VGA) -> VGA.
   2319   cricket::VideoCodec codec(100, "VP8", 1280, 720, 30, 0);
   2320   EXPECT_TRUE(SetOneCodec(codec));
   2321   codec.width /= 2;
   2322   codec.height /= 2;
   2323   EXPECT_TRUE(SetSend(true));
   2324   EXPECT_TRUE(channel_->SetRender(true));
   2325   EXPECT_EQ(0, renderer_.num_rendered_frames());
   2326   EXPECT_TRUE(SendFrame());
   2327   EXPECT_FRAME_WAIT(1, codec.width, codec.height, kTimeout);
   2328 }
   2329 
   2330 #ifdef USE_WEBRTC_DEV_BRANCH
   2331 TEST_F(WebRtcVideoMediaChannelTest, GetStats) {
   2332 #else
   2333 TEST_F(WebRtcVideoMediaChannelTest, DISABLED_GetStats) {
   2334 #endif
   2335   Base::GetStats();
   2336 }
   2337 
   2338 #ifdef USE_WEBRTC_DEV_BRANCH
   2339 TEST_F(WebRtcVideoMediaChannelTest, GetStatsMultipleRecvStreams) {
   2340 #else
   2341 TEST_F(WebRtcVideoMediaChannelTest, DISABLED_GetStatsMultipleRecvStreams) {
   2342 #endif
   2343   Base::GetStatsMultipleRecvStreams();
   2344 }
   2345 
   2346 TEST_F(WebRtcVideoMediaChannelTest, GetStatsMultipleSendStreams) {
   2347   Base::GetStatsMultipleSendStreams();
   2348 }
   2349 
   2350 TEST_F(WebRtcVideoMediaChannelTest, SetSendBandwidth) {
   2351   Base::SetSendBandwidth();
   2352 }
   2353 TEST_F(WebRtcVideoMediaChannelTest, SetSendSsrc) {
   2354   Base::SetSendSsrc();
   2355 }
   2356 TEST_F(WebRtcVideoMediaChannelTest, SetSendSsrcAfterSetCodecs) {
   2357   Base::SetSendSsrcAfterSetCodecs();
   2358 }
   2359 
   2360 TEST_F(WebRtcVideoMediaChannelTest, SetRenderer) {
   2361   Base::SetRenderer();
   2362 }
   2363 
   2364 TEST_F(WebRtcVideoMediaChannelTest, AddRemoveRecvStreams) {
   2365   Base::AddRemoveRecvStreams();
   2366 }
   2367 
   2368 TEST_F(WebRtcVideoMediaChannelTest, AddRemoveRecvStreamAndRender) {
   2369   Base::AddRemoveRecvStreamAndRender();
   2370 }
   2371 
   2372 TEST_F(WebRtcVideoMediaChannelTest, AddRemoveRecvStreamsNoConference) {
   2373   Base::AddRemoveRecvStreamsNoConference();
   2374 }
   2375 
   2376 TEST_F(WebRtcVideoMediaChannelTest, AddRemoveSendStreams) {
   2377   Base::AddRemoveSendStreams();
   2378 }
   2379 
   2380 TEST_F(WebRtcVideoMediaChannelTest, SimulateConference) {
   2381   Base::SimulateConference();
   2382 }
   2383 
   2384 TEST_F(WebRtcVideoMediaChannelTest, AddRemoveCapturer) {
   2385   Base::AddRemoveCapturer();
   2386 }
   2387 
   2388 TEST_F(WebRtcVideoMediaChannelTest, RemoveCapturerWithoutAdd) {
   2389   Base::RemoveCapturerWithoutAdd();
   2390 }
   2391 
   2392 TEST_F(WebRtcVideoMediaChannelTest, AddRemoveCapturerMultipleSources) {
   2393   Base::AddRemoveCapturerMultipleSources();
   2394 }
   2395 
   2396 // This test verifies DSCP settings are properly applied on video media channel.
   2397 TEST_F(WebRtcVideoMediaChannelTest, TestSetDscpOptions) {
   2398   talk_base::scoped_ptr<cricket::FakeNetworkInterface> network_interface(
   2399       new cricket::FakeNetworkInterface);
   2400   channel_->SetInterface(network_interface.get());
   2401   cricket::VideoOptions options;
   2402   options.dscp.Set(true);
   2403   EXPECT_TRUE(channel_->SetOptions(options));
   2404   EXPECT_EQ(talk_base::DSCP_AF41, network_interface->dscp());
   2405   // Verify previous value is not modified if dscp option is not set.
   2406   cricket::VideoOptions options1;
   2407   EXPECT_TRUE(channel_->SetOptions(options1));
   2408   EXPECT_EQ(talk_base::DSCP_AF41, network_interface->dscp());
   2409   options.dscp.Set(false);
   2410   EXPECT_TRUE(channel_->SetOptions(options));
   2411   EXPECT_EQ(talk_base::DSCP_DEFAULT, network_interface->dscp());
   2412   channel_->SetInterface(NULL);
   2413 }
   2414 
   2415 
   2416 TEST_F(WebRtcVideoMediaChannelTest, SetOptionsSucceedsWhenSending) {
   2417   cricket::VideoOptions options;
   2418   options.conference_mode.Set(true);
   2419   EXPECT_TRUE(channel_->SetOptions(options));
   2420 
   2421   // Verify SetOptions returns true on a different options.
   2422   cricket::VideoOptions options2;
   2423   options2.adapt_input_to_cpu_usage.Set(true);
   2424   EXPECT_TRUE(channel_->SetOptions(options2));
   2425 
   2426   // Set send codecs on the channel and start sending.
   2427   std::vector<cricket::VideoCodec> codecs;
   2428   codecs.push_back(kVP8Codec);
   2429   EXPECT_TRUE(channel_->SetSendCodecs(codecs));
   2430   EXPECT_TRUE(channel_->SetSend(true));
   2431 
   2432   // Verify SetOptions returns true if channel is already sending.
   2433   cricket::VideoOptions options3;
   2434   options3.conference_mode.Set(true);
   2435   EXPECT_TRUE(channel_->SetOptions(options3));
   2436 }
   2437 
   2438 // Tests empty StreamParams is rejected.
   2439 TEST_F(WebRtcVideoMediaChannelTest, RejectEmptyStreamParams) {
   2440   Base::RejectEmptyStreamParams();
   2441 }
   2442 
   2443 
   2444 TEST_F(WebRtcVideoMediaChannelTest, AdaptResolution16x10) {
   2445   Base::AdaptResolution16x10();
   2446 }
   2447 
   2448 TEST_F(WebRtcVideoMediaChannelTest, AdaptResolution4x3) {
   2449   Base::AdaptResolution4x3();
   2450 }
   2451 
   2452 TEST_F(WebRtcVideoMediaChannelTest, MuteStream) {
   2453   Base::MuteStream();
   2454 }
   2455 
   2456 TEST_F(WebRtcVideoMediaChannelTest, MultipleSendStreams) {
   2457   Base::MultipleSendStreams();
   2458 }
   2459 
   2460 // TODO(juberti): Restore this test once we support sending 0 fps.
   2461 TEST_F(WebRtcVideoMediaChannelTest, DISABLED_AdaptDropAllFrames) {
   2462   Base::AdaptDropAllFrames();
   2463 }
   2464 // TODO(juberti): Understand why we get decode errors on this test.
   2465 TEST_F(WebRtcVideoMediaChannelTest, DISABLED_AdaptFramerate) {
   2466   Base::AdaptFramerate();
   2467 }
   2468 
   2469 TEST_F(WebRtcVideoMediaChannelTest, SetSendStreamFormat0x0) {
   2470   Base::SetSendStreamFormat0x0();
   2471 }
   2472 
   2473 // TODO(zhurunz): Fix the flakey test.
   2474 TEST_F(WebRtcVideoMediaChannelTest, DISABLED_SetSendStreamFormat) {
   2475   Base::SetSendStreamFormat();
   2476 }
   2477 
   2478 TEST_F(WebRtcVideoMediaChannelTest, TwoStreamsSendAndReceive) {
   2479   Base::TwoStreamsSendAndReceive(cricket::VideoCodec(100, "VP8", 640, 400, 30,
   2480                                                      0));
   2481 }
   2482 
   2483 TEST_F(WebRtcVideoMediaChannelTest, TwoStreamsReUseFirstStream) {
   2484   Base::TwoStreamsReUseFirstStream(cricket::VideoCodec(100, "VP8", 640, 400, 30,
   2485                                                        0));
   2486 }
   2487 
   2488 TEST_F(WebRtcVideoMediaChannelTest, DISABLED_TwoStreamsSendAndUnsignalledRecv) {
   2489   Base::TwoStreamsSendAndUnsignalledRecv(cricket::VideoCodec(100, "VP8", 640,
   2490                                                              400, 30, 0));
   2491 }
   2492 
   2493 TEST_F(WebRtcVideoMediaChannelTest,
   2494        TwoStreamsSendAndFailUnsignalledRecv) {
   2495   webrtc::Trace::set_level_filter(webrtc::kTraceAll);
   2496   Base::TwoStreamsSendAndFailUnsignalledRecv(
   2497       cricket::VideoCodec(100, "VP8", 640, 400, 30, 0));
   2498 }
   2499 
   2500 TEST_F(WebRtcVideoMediaChannelTest,
   2501        TwoStreamsSendAndFailUnsignalledRecvInOneToOne) {
   2502   Base::TwoStreamsSendAndFailUnsignalledRecvInOneToOne(
   2503       cricket::VideoCodec(100, "VP8", 640, 400, 30, 0));
   2504 }
   2505 
   2506 TEST_F(WebRtcVideoMediaChannelTest,
   2507        TwoStreamsAddAndRemoveUnsignalledRecv) {
   2508   Base::TwoStreamsAddAndRemoveUnsignalledRecv(cricket::VideoCodec(100, "VP8",
   2509                                                                   640, 400, 30,
   2510                                                                   0));
   2511 }
   2512