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