Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / media / base / videoengine_unittest.h
1 // libjingle
2 // Copyright 2004 Google Inc. All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 //  1. Redistributions of source code must retain the above copyright notice,
8 //     this list of conditions and the following disclaimer.
9 //  2. Redistributions in binary form must reproduce the above copyright notice,
10 //     this list of conditions and the following disclaimer in the documentation
11 //     and/or other materials provided with the distribution.
12 //  3. The name of the author may not be used to endorse or promote products
13 //     derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26 #ifndef TALK_MEDIA_BASE_VIDEOENGINE_UNITTEST_H_  // NOLINT
27 #define TALK_MEDIA_BASE_VIDEOENGINE_UNITTEST_H_
28
29 #include <string>
30 #include <vector>
31
32 #include "talk/media/base/fakenetworkinterface.h"
33 #include "talk/media/base/fakevideocapturer.h"
34 #include "talk/media/base/fakevideorenderer.h"
35 #include "talk/media/base/mediachannel.h"
36 #include "talk/media/base/streamparams.h"
37 #include "webrtc/base/bytebuffer.h"
38 #include "webrtc/base/gunit.h"
39 #include "webrtc/base/timeutils.h"
40
41 #ifdef WIN32
42 #include <objbase.h>  // NOLINT
43 #endif
44
45 #define EXPECT_FRAME_WAIT(c, w, h, t) \
46   EXPECT_EQ_WAIT((c), renderer_.num_rendered_frames(), (t)); \
47   EXPECT_EQ((w), renderer_.width()); \
48   EXPECT_EQ((h), renderer_.height()); \
49   EXPECT_EQ(0, renderer_.errors()); \
50
51 #define EXPECT_FRAME_ON_RENDERER_WAIT(r, c, w, h, t) \
52   EXPECT_EQ_WAIT((c), (r).num_rendered_frames(), (t)); \
53   EXPECT_EQ((w), (r).width()); \
54   EXPECT_EQ((h), (r).height()); \
55   EXPECT_EQ(0, (r).errors()); \
56
57 #define EXPECT_GT_FRAME_ON_RENDERER_WAIT(r, c, w, h, t) \
58   EXPECT_TRUE_WAIT((r).num_rendered_frames() >= (c) && \
59                    (w) == (r).width() && \
60                    (h) == (r).height(), (t)); \
61   EXPECT_EQ(0, (r).errors()); \
62
63 static const uint32 kTimeout = 5000U;
64 static const uint32 kDefaultReceiveSsrc = 0;
65 static const uint32 kSsrc = 1234u;
66 static const uint32 kRtxSsrc = 4321u;
67 static const uint32 kSsrcs4[] = {1, 2, 3, 4};
68
69 inline bool IsEqualRes(const cricket::VideoCodec& a, int w, int h, int fps) {
70   return a.width == w && a.height == h && a.framerate == fps;
71 }
72
73 inline bool IsEqualCodec(const cricket::VideoCodec& a,
74                          const cricket::VideoCodec& b) {
75   return a.id == b.id && a.name == b.name &&
76       IsEqualRes(a, b.width, b.height, b.framerate);
77 }
78
79 namespace std {
80 inline std::ostream& operator<<(std::ostream& s, const cricket::VideoCodec& c) {
81   s << "{" << c.name << "(" << c.id << "), "
82     << c.width << "x" << c.height << "x" << c.framerate << "}";
83   return s;
84 }
85 }  // namespace std
86
87 inline int TimeBetweenSend(const cricket::VideoCodec& codec) {
88   return static_cast<int>(
89       cricket::VideoFormat::FpsToInterval(codec.framerate) /
90       rtc::kNumNanosecsPerMillisec);
91 }
92
93 // Fake video engine that makes it possible to test enabling and disabling
94 // capturer (checking that the engine state is updated and that the capturer
95 // is indeed capturing) without having to create a channel. It also makes it
96 // possible to test that the media processors are indeed being called when
97 // registered.
98 template<class T>
99 class VideoEngineOverride : public T {
100  public:
101   VideoEngineOverride() {
102   }
103   virtual ~VideoEngineOverride() {
104   }
105   bool is_camera_on() const { return T::GetVideoCapturer()->IsRunning(); }
106   void set_has_senders(bool has_senders) {
107     cricket::VideoCapturer* video_capturer = T::GetVideoCapturer();
108     if (has_senders) {
109       video_capturer->SignalVideoFrame.connect(this,
110           &VideoEngineOverride<T>::OnLocalFrame);
111     } else {
112       video_capturer->SignalVideoFrame.disconnect(this);
113     }
114   }
115   void OnLocalFrame(cricket::VideoCapturer*,
116                     const cricket::VideoFrame*) {
117   }
118   void OnLocalFrameFormat(cricket::VideoCapturer*,
119                           const cricket::VideoFormat*) {
120   }
121
122   void TriggerMediaFrame(
123       uint32 ssrc, cricket::VideoFrame* frame, bool* drop_frame) {
124     T::SignalMediaFrame(ssrc, frame, drop_frame);
125   }
126 };
127
128 // Macroes that declare test functions for a given test class, before and after
129 // Init().
130 // To use, define a test function called FooBody and pass Foo to the macro.
131 #define TEST_PRE_VIDEOENGINE_INIT(TestClass, func) \
132   TEST_F(TestClass, func##PreInit) { \
133     func##Body(); \
134   }
135 #define TEST_POST_VIDEOENGINE_INIT(TestClass, func) \
136   TEST_F(TestClass, func##PostInit) { \
137     EXPECT_TRUE(engine_.Init(rtc::Thread::Current())); \
138     func##Body(); \
139     engine_.Terminate(); \
140   }
141
142 template<class E>
143 class VideoEngineTest : public testing::Test {
144  protected:
145   // Tests starting and stopping the engine, and creating a channel.
146   void StartupShutdown() {
147     EXPECT_TRUE(engine_.Init(rtc::Thread::Current()));
148     cricket::VideoMediaChannel* channel = engine_.CreateChannel(NULL);
149     EXPECT_TRUE(channel != NULL);
150     delete channel;
151     engine_.Terminate();
152   }
153
154 #ifdef WIN32
155   // Tests that the COM reference count is not munged by the engine.
156   // Test to make sure LMI does not munge the CoInitialize reference count.
157   void CheckCoInitialize() {
158     // Initial refcount should be 0.
159     EXPECT_EQ(S_OK, CoInitializeEx(NULL, COINIT_MULTITHREADED));
160
161     // Engine should start even with COM already inited.
162     EXPECT_TRUE(engine_.Init(rtc::Thread::Current()));
163     engine_.Terminate();
164     // Refcount after terminate should be 1; this tests if it is nonzero.
165     EXPECT_EQ(S_FALSE, CoInitializeEx(NULL, COINIT_MULTITHREADED));
166     // Decrement refcount to (hopefully) 0.
167     CoUninitialize();
168     CoUninitialize();
169
170     // Ensure refcount is 0.
171     EXPECT_EQ(S_OK, CoInitializeEx(NULL, COINIT_MULTITHREADED));
172     CoUninitialize();
173   }
174 #endif
175
176   void ConstrainNewCodecBody() {
177     cricket::VideoCodec empty, in, out;
178     cricket::VideoCodec max_settings(engine_.codecs()[0].id,
179                                      engine_.codecs()[0].name,
180                                      1280, 800, 30, 0);
181
182     // set max settings of 1280x800x30
183     EXPECT_TRUE(engine_.SetDefaultEncoderConfig(
184         cricket::VideoEncoderConfig(max_settings)));
185
186     // don't constrain the max resolution
187     in = max_settings;
188     EXPECT_TRUE(engine_.CanSendCodec(in, empty, &out));
189     EXPECT_PRED2(IsEqualCodec, out, in);
190
191     // constrain resolution greater than the max and wider aspect,
192     // picking best aspect (16:10)
193     in.width = 1380;
194     in.height = 800;
195     EXPECT_TRUE(engine_.CanSendCodec(in, empty, &out));
196     EXPECT_PRED4(IsEqualRes, out, 1280, 720, 30);
197
198     // constrain resolution greater than the max and narrow aspect,
199     // picking best aspect (16:9)
200     in.width = 1280;
201     in.height = 740;
202     EXPECT_TRUE(engine_.CanSendCodec(in, empty, &out));
203     EXPECT_PRED4(IsEqualRes, out, 1280, 720, 30);
204
205     // constrain resolution greater than the max, picking equal aspect (4:3)
206     in.width = 1280;
207     in.height = 960;
208     EXPECT_TRUE(engine_.CanSendCodec(in, empty, &out));
209     EXPECT_PRED4(IsEqualRes, out, 1280, 800, 30);
210
211     // constrain resolution greater than the max, picking equal aspect (16:10)
212     in.width = 1280;
213     in.height = 800;
214     EXPECT_TRUE(engine_.CanSendCodec(in, empty, &out));
215     EXPECT_PRED4(IsEqualRes, out, 1280, 800, 30);
216
217     // reduce max settings to 640x480x30
218     max_settings.width = 640;
219     max_settings.height = 480;
220     EXPECT_TRUE(engine_.SetDefaultEncoderConfig(
221         cricket::VideoEncoderConfig(max_settings)));
222
223     // don't constrain the max resolution
224     in = max_settings;
225     in.width = 640;
226     in.height = 480;
227     EXPECT_TRUE(engine_.CanSendCodec(in, empty, &out));
228     EXPECT_PRED2(IsEqualCodec, out, in);
229
230     // keep 16:10 if they request it
231     in.height = 400;
232     EXPECT_TRUE(engine_.CanSendCodec(in, empty, &out));
233     EXPECT_PRED2(IsEqualCodec, out, in);
234
235     // don't constrain lesser 4:3 resolutions
236     in.width = 320;
237     in.height = 240;
238     EXPECT_TRUE(engine_.CanSendCodec(in, empty, &out));
239     EXPECT_PRED2(IsEqualCodec, out, in);
240
241     // don't constrain lesser 16:10 resolutions
242     in.width = 320;
243     in.height = 200;
244     EXPECT_TRUE(engine_.CanSendCodec(in, empty, &out));
245     EXPECT_PRED2(IsEqualCodec, out, in);
246
247     // requested resolution of 0x0 succeeds
248     in.width = 0;
249     in.height = 0;
250     EXPECT_TRUE(engine_.CanSendCodec(in, empty, &out));
251     EXPECT_PRED2(IsEqualCodec, out, in);
252
253     // constrain resolution lesser than the max and wider aspect,
254     // picking best aspect (16:9)
255     in.width = 350;
256     in.height = 201;
257     EXPECT_TRUE(engine_.CanSendCodec(in, empty, &out));
258     EXPECT_PRED4(IsEqualRes, out, 320, 180, 30);
259
260     // constrain resolution greater than the max and narrow aspect,
261     // picking best aspect (4:3)
262     in.width = 350;
263     in.height = 300;
264     EXPECT_TRUE(engine_.CanSendCodec(in, empty, &out));
265     EXPECT_PRED4(IsEqualRes, out, 320, 240, 30);
266
267     // constrain resolution greater than the max and wider aspect,
268     // picking best aspect (16:9)
269     in.width = 1380;
270     in.height = 800;
271     EXPECT_TRUE(engine_.CanSendCodec(in, empty, &out));
272     EXPECT_PRED4(IsEqualRes, out, 640, 360, 30);
273
274     // constrain resolution greater than the max and narrow aspect,
275     // picking best aspect (4:3)
276     in.width = 1280;
277     in.height = 900;
278     EXPECT_TRUE(engine_.CanSendCodec(in, empty, &out));
279     EXPECT_PRED4(IsEqualRes, out, 640, 480, 30);
280
281     // constrain resolution greater than the max, picking equal aspect (4:3)
282     in.width = 1280;
283     in.height = 960;
284     EXPECT_TRUE(engine_.CanSendCodec(in, empty, &out));
285     EXPECT_PRED4(IsEqualRes, out, 640, 480, 30);
286
287     // constrain resolution greater than the max, picking equal aspect (16:10)
288     in.width = 1280;
289     in.height = 800;
290     EXPECT_TRUE(engine_.CanSendCodec(in, empty, &out));
291     EXPECT_PRED4(IsEqualRes, out, 640, 400, 30);
292
293     // constrain res & fps greater than the max
294     in.framerate = 50;
295     EXPECT_TRUE(engine_.CanSendCodec(in, empty, &out));
296     EXPECT_PRED4(IsEqualRes, out, 640, 400, 30);
297
298     // reduce max settings to 160x100x10
299     max_settings.width = 160;
300     max_settings.height = 100;
301     max_settings.framerate = 10;
302     EXPECT_TRUE(engine_.SetDefaultEncoderConfig(
303         cricket::VideoEncoderConfig(max_settings)));
304
305     // constrain res & fps to new max
306     EXPECT_TRUE(engine_.CanSendCodec(in, empty, &out));
307     EXPECT_PRED4(IsEqualRes, out, 160, 100, 10);
308
309     // allow 4:3 "comparable" resolutions
310     in.width = 160;
311     in.height = 120;
312     in.framerate = 10;
313     EXPECT_TRUE(engine_.CanSendCodec(in, empty, &out));
314     EXPECT_PRED4(IsEqualRes, out, 160, 120, 10);
315   }
316
317   // This is the new way of constraining codec size, where we no longer maintain
318   // a list of the supported formats. Instead, CanSendCodec will just downscale
319   // the resolution by 2 until the width is below clamp.
320   void ConstrainNewCodec2Body() {
321     cricket::VideoCodec empty, in, out;
322     cricket::VideoCodec max_settings(engine_.codecs()[0].id,
323                                      engine_.codecs()[0].name,
324                                      1280, 800, 30, 0);
325
326     // Set max settings of 1280x800x30
327     EXPECT_TRUE(engine_.SetDefaultEncoderConfig(
328         cricket::VideoEncoderConfig(max_settings)));
329
330     // Don't constrain the max resolution
331     in = max_settings;
332     EXPECT_TRUE(engine_.CanSendCodec(in, empty, &out));
333     EXPECT_PRED2(IsEqualCodec, out, in);
334
335     // Constrain resolution greater than the max width.
336     in.width = 1380;
337     in.height = 800;
338     EXPECT_TRUE(engine_.CanSendCodec(in, empty, &out));
339     EXPECT_PRED4(IsEqualRes, out, 690, 400, 30);
340
341     // Don't constrain resolution when only the height is greater than max.
342     in.width = 960;
343     in.height = 1280;
344     EXPECT_TRUE(engine_.CanSendCodec(in, empty, &out));
345     EXPECT_PRED4(IsEqualRes, out, 960, 1280, 30);
346
347     // Don't constrain smaller format.
348     in.width = 640;
349     in.height = 480;
350     EXPECT_TRUE(engine_.CanSendCodec(in, empty, &out));
351     EXPECT_PRED4(IsEqualRes, out, 640, 480, 30);
352   }
353
354   void ConstrainRunningCodecBody() {
355     cricket::VideoCodec in, out, current;
356     cricket::VideoCodec max_settings(engine_.codecs()[0].id,
357                                      engine_.codecs()[0].name,
358                                      1280, 800, 30, 0);
359
360     // set max settings of 1280x960x30
361     EXPECT_TRUE(engine_.SetDefaultEncoderConfig(
362         cricket::VideoEncoderConfig(max_settings)));
363
364     // establish current call at 1280x800x30 (16:10)
365     current = max_settings;
366     current.height = 800;
367
368     // Don't constrain current resolution
369     in = current;
370     EXPECT_TRUE(engine_.CanSendCodec(in, current, &out));
371     EXPECT_PRED2(IsEqualCodec, out, in);
372
373     // requested resolution of 0x0 succeeds
374     in.width = 0;
375     in.height = 0;
376     EXPECT_TRUE(engine_.CanSendCodec(in, current, &out));
377     EXPECT_PRED2(IsEqualCodec, out, in);
378
379     // Reduce an intermediate resolution down to the next lowest one, preserving
380     // aspect ratio.
381     in.width = 800;
382     in.height = 600;
383     EXPECT_TRUE(engine_.CanSendCodec(in, current, &out));
384     EXPECT_PRED4(IsEqualRes, out, 640, 400, 30);
385
386     // Clamping by aspect ratio, but still never return a dimension higher than
387     // requested.
388     in.width = 1280;
389     in.height = 720;
390     EXPECT_TRUE(engine_.CanSendCodec(in, current, &out));
391     EXPECT_PRED4(IsEqualRes, out, 1280, 720, 30);
392
393     in.width = 1279;
394     EXPECT_TRUE(engine_.CanSendCodec(in, current, &out));
395     EXPECT_PRED4(IsEqualRes, out, 960, 600, 30);
396
397     in.width = 1281;
398     EXPECT_TRUE(engine_.CanSendCodec(in, current, &out));
399     EXPECT_PRED4(IsEqualRes, out, 1280, 720, 30);
400
401     // Clamp large resolutions down, always preserving aspect
402     in.width = 1920;
403     in.height = 1080;
404     EXPECT_TRUE(engine_.CanSendCodec(in, current, &out));
405     EXPECT_PRED4(IsEqualRes, out, 1280, 800, 30);
406
407     in.width = 1921;
408     EXPECT_TRUE(engine_.CanSendCodec(in, current, &out));
409     EXPECT_PRED4(IsEqualRes, out, 1280, 800, 30);
410
411     in.width = 1919;
412     EXPECT_TRUE(engine_.CanSendCodec(in, current, &out));
413     EXPECT_PRED4(IsEqualRes, out, 1280, 800, 30);
414
415     // reduce max settings to 640x480x30
416     max_settings.width = 640;
417     max_settings.height = 480;
418     EXPECT_TRUE(engine_.SetDefaultEncoderConfig(
419         cricket::VideoEncoderConfig(max_settings)));
420
421     // establish current call at 640x400x30 (16:10)
422     current = max_settings;
423     current.height = 400;
424
425     // Don't constrain current resolution
426     in = current;
427     EXPECT_TRUE(engine_.CanSendCodec(in, current, &out));
428     EXPECT_PRED2(IsEqualCodec, out, in);
429
430     // requested resolution of 0x0 succeeds
431     in.width = 0;
432     in.height = 0;
433     EXPECT_TRUE(engine_.CanSendCodec(in, current, &out));
434     EXPECT_PRED2(IsEqualCodec, out, in);
435
436     // Reduce an intermediate resolution down to the next lowest one, preserving
437     // aspect ratio.
438     in.width = 400;
439     in.height = 300;
440     EXPECT_TRUE(engine_.CanSendCodec(in, current, &out));
441     EXPECT_PRED4(IsEqualRes, out, 320, 200, 30);
442
443     // Clamping by aspect ratio, but still never return a dimension higher than
444     // requested.
445     in.width = 640;
446     in.height = 360;
447     EXPECT_TRUE(engine_.CanSendCodec(in, current, &out));
448     EXPECT_PRED4(IsEqualRes, out, 640, 360, 30);
449
450     in.width = 639;
451     EXPECT_TRUE(engine_.CanSendCodec(in, current, &out));
452     EXPECT_PRED4(IsEqualRes, out, 480, 300, 30);
453
454     in.width = 641;
455     EXPECT_TRUE(engine_.CanSendCodec(in, current, &out));
456     EXPECT_PRED4(IsEqualRes, out, 640, 360, 30);
457
458     // Clamp large resolutions down, always preserving aspect
459     in.width = 1280;
460     in.height = 800;
461     EXPECT_TRUE(engine_.CanSendCodec(in, current, &out));
462     EXPECT_PRED4(IsEqualRes, out, 640, 400, 30);
463
464     in.width = 1281;
465     EXPECT_TRUE(engine_.CanSendCodec(in, current, &out));
466     EXPECT_PRED4(IsEqualRes, out, 640, 400, 30);
467
468     in.width = 1279;
469     EXPECT_TRUE(engine_.CanSendCodec(in, current, &out));
470     EXPECT_PRED4(IsEqualRes, out, 640, 400, 30);
471
472     // Should fail for any that are smaller than our supported formats
473     in.width = 80;
474     in.height = 80;
475     EXPECT_FALSE(engine_.CanSendCodec(in, current, &out));
476
477     in.height = 50;
478     EXPECT_FALSE(engine_.CanSendCodec(in, current, &out));
479   }
480
481   VideoEngineOverride<E> engine_;
482   rtc::scoped_ptr<cricket::FakeVideoCapturer> video_capturer_;
483 };
484
485 template<class E, class C>
486 class VideoMediaChannelTest : public testing::Test,
487                               public sigslot::has_slots<> {
488  protected:
489   virtual cricket::VideoCodec DefaultCodec() = 0;
490
491   virtual cricket::StreamParams DefaultSendStreamParams() {
492     return cricket::StreamParams::CreateLegacy(kSsrc);
493   }
494
495   virtual void SetUp() {
496     cricket::Device device("test", "device");
497     EXPECT_TRUE(engine_.Init(rtc::Thread::Current()));
498     channel_.reset(engine_.CreateChannel(cricket::VideoOptions(), NULL));
499     EXPECT_TRUE(channel_.get() != NULL);
500     ConnectVideoChannelError();
501     network_interface_.SetDestination(channel_.get());
502     channel_->SetInterface(&network_interface_);
503     media_error_ = cricket::VideoMediaChannel::ERROR_NONE;
504     channel_->SetRecvCodecs(engine_.codecs());
505     EXPECT_TRUE(channel_->AddSendStream(DefaultSendStreamParams()));
506     video_capturer_.reset(CreateFakeVideoCapturer());
507     cricket::VideoFormat format(640, 480,
508                                 cricket::VideoFormat::FpsToInterval(30),
509                                 cricket::FOURCC_I420);
510     EXPECT_EQ(cricket::CS_RUNNING, video_capturer_->Start(format));
511     EXPECT_TRUE(channel_->SetCapturer(kSsrc, video_capturer_.get()));
512   }
513
514   virtual cricket::FakeVideoCapturer* CreateFakeVideoCapturer() {
515     return new cricket::FakeVideoCapturer();
516   }
517
518   // Utility method to setup an additional stream to send and receive video.
519   // Used to test send and recv between two streams.
520   void SetUpSecondStream() {
521     SetUpSecondStreamWithNoRecv();
522     // Setup recv for second stream.
523     EXPECT_TRUE(channel_->AddRecvStream(
524         cricket::StreamParams::CreateLegacy(kSsrc + 2)));
525     // Make the second renderer available for use by a new stream.
526     EXPECT_TRUE(channel_->SetRenderer(kSsrc + 2, &renderer2_));
527   }
528   // Setup an additional stream just to send video. Defer add recv stream.
529   // This is required if you want to test unsignalled recv of video rtp packets.
530   void SetUpSecondStreamWithNoRecv() {
531     // SetUp() already added kSsrc make sure duplicate SSRCs cant be added.
532     EXPECT_TRUE(channel_->AddRecvStream(
533         cricket::StreamParams::CreateLegacy(kSsrc)));
534     EXPECT_TRUE(channel_->SetRenderer(kSsrc, &renderer_));
535     EXPECT_FALSE(channel_->AddSendStream(
536         cricket::StreamParams::CreateLegacy(kSsrc)));
537     EXPECT_TRUE(channel_->AddSendStream(
538         cricket::StreamParams::CreateLegacy(kSsrc + 2)));
539     // We dont add recv for the second stream.
540
541     // Setup the receive and renderer for second stream after send.
542     video_capturer_2_.reset(CreateFakeVideoCapturer());
543     cricket::VideoFormat format(640, 480,
544                                 cricket::VideoFormat::FpsToInterval(30),
545                                 cricket::FOURCC_I420);
546     EXPECT_EQ(cricket::CS_RUNNING, video_capturer_2_->Start(format));
547
548     EXPECT_TRUE(channel_->SetCapturer(kSsrc + 2, video_capturer_2_.get()));
549   }
550   virtual void TearDown() {
551     channel_.reset();
552     engine_.Terminate();
553   }
554   void ConnectVideoChannelError() {
555     channel_->SignalMediaError.connect(this,
556         &VideoMediaChannelTest<E, C>::OnVideoChannelError);
557   }
558   bool SetDefaultCodec() {
559     return SetOneCodec(DefaultCodec());
560   }
561
562   bool SetOneCodec(int pt, const char* name, int w, int h, int fr) {
563     return SetOneCodec(cricket::VideoCodec(pt, name, w, h, fr, 0));
564   }
565   bool SetOneCodec(const cricket::VideoCodec& codec) {
566     std::vector<cricket::VideoCodec> codecs;
567     codecs.push_back(codec);
568
569     cricket::VideoFormat capture_format(codec.width, codec.height,
570         cricket::VideoFormat::FpsToInterval(codec.framerate),
571         cricket::FOURCC_I420);
572
573     if (video_capturer_) {
574       EXPECT_EQ(cricket::CS_RUNNING, video_capturer_->Start(capture_format));
575     }
576     if (video_capturer_2_) {
577       EXPECT_EQ(cricket::CS_RUNNING, video_capturer_2_->Start(capture_format));
578     }
579
580     bool sending = channel_->sending();
581     bool success = SetSend(false);
582     if (success)
583       success = channel_->SetSendCodecs(codecs);
584     if (success)
585       success = SetSend(sending);
586     return success;
587   }
588   bool SetSend(bool send) {
589     return channel_->SetSend(send);
590   }
591   bool SetSendStreamFormat(uint32 ssrc, const cricket::VideoCodec& codec) {
592     return channel_->SetSendStreamFormat(ssrc, cricket::VideoFormat(
593         codec.width, codec.height,
594         cricket::VideoFormat::FpsToInterval(codec.framerate),
595         cricket::FOURCC_ANY));
596   }
597   int DrainOutgoingPackets() {
598     int packets = 0;
599     do {
600       packets = NumRtpPackets();
601       // 100 ms should be long enough.
602       rtc::Thread::Current()->ProcessMessages(100);
603     } while (NumRtpPackets() > packets);
604     return NumRtpPackets();
605   }
606   bool SendFrame() {
607     if (video_capturer_2_) {
608       video_capturer_2_->CaptureFrame();
609     }
610     return video_capturer_.get() &&
611         video_capturer_->CaptureFrame();
612   }
613   bool WaitAndSendFrame(int wait_ms) {
614     bool ret = rtc::Thread::Current()->ProcessMessages(wait_ms);
615     ret &= SendFrame();
616     return ret;
617   }
618   // Sends frames and waits for the decoder to be fully initialized.
619   // Returns the number of frames that were sent.
620   int WaitForDecoder() {
621 #if defined(HAVE_OPENMAX)
622     // Send enough frames for the OpenMAX decoder to continue processing, and
623     // return the number of frames sent.
624     // Send frames for a full kTimeout's worth of 15fps video.
625     int frame_count = 0;
626     while (frame_count < static_cast<int>(kTimeout) / 66) {
627       EXPECT_TRUE(WaitAndSendFrame(66));
628       ++frame_count;
629     }
630     return frame_count;
631 #else
632     return 0;
633 #endif
634   }
635   bool SendCustomVideoFrame(int w, int h) {
636     if (!video_capturer_.get()) return false;
637     return video_capturer_->CaptureCustomFrame(w, h, cricket::FOURCC_I420);
638   }
639   int NumRtpBytes() {
640     return network_interface_.NumRtpBytes();
641   }
642   int NumRtpBytes(uint32 ssrc) {
643     return network_interface_.NumRtpBytes(ssrc);
644   }
645   int NumRtpPackets() {
646     return network_interface_.NumRtpPackets();
647   }
648   int NumRtpPackets(uint32 ssrc) {
649     return network_interface_.NumRtpPackets(ssrc);
650   }
651   int NumSentSsrcs() {
652     return network_interface_.NumSentSsrcs();
653   }
654   const rtc::Buffer* GetRtpPacket(int index) {
655     return network_interface_.GetRtpPacket(index);
656   }
657   int NumRtcpPackets() {
658     return network_interface_.NumRtcpPackets();
659   }
660   const rtc::Buffer* GetRtcpPacket(int index) {
661     return network_interface_.GetRtcpPacket(index);
662   }
663   static int GetPayloadType(const rtc::Buffer* p) {
664     int pt = -1;
665     ParseRtpPacket(p, NULL, &pt, NULL, NULL, NULL, NULL);
666     return pt;
667   }
668   static bool ParseRtpPacket(const rtc::Buffer* p, bool* x, int* pt,
669                              int* seqnum, uint32* tstamp, uint32* ssrc,
670                              std::string* payload) {
671     rtc::ByteBuffer buf(p->data(), p->length());
672     uint8 u08 = 0;
673     uint16 u16 = 0;
674     uint32 u32 = 0;
675
676     // Read X and CC fields.
677     if (!buf.ReadUInt8(&u08)) return false;
678     bool extension = ((u08 & 0x10) != 0);
679     uint8 cc = (u08 & 0x0F);
680     if (x) *x = extension;
681
682     // Read PT field.
683     if (!buf.ReadUInt8(&u08)) return false;
684     if (pt) *pt = (u08 & 0x7F);
685
686     // Read Sequence Number field.
687     if (!buf.ReadUInt16(&u16)) return false;
688     if (seqnum) *seqnum = u16;
689
690     // Read Timestamp field.
691     if (!buf.ReadUInt32(&u32)) return false;
692     if (tstamp) *tstamp = u32;
693
694     // Read SSRC field.
695     if (!buf.ReadUInt32(&u32)) return false;
696     if (ssrc) *ssrc = u32;
697
698     // Skip CSRCs.
699     for (uint8 i = 0; i < cc; ++i) {
700       if (!buf.ReadUInt32(&u32)) return false;
701     }
702
703     // Skip extension header.
704     if (extension) {
705       // Read Profile-specific extension header ID
706       if (!buf.ReadUInt16(&u16)) return false;
707
708       // Read Extension header length
709       if (!buf.ReadUInt16(&u16)) return false;
710       uint16 ext_header_len = u16;
711
712       // Read Extension header
713       for (uint16 i = 0; i < ext_header_len; ++i) {
714         if (!buf.ReadUInt32(&u32)) return false;
715       }
716     }
717
718     if (payload) {
719       return buf.ReadString(payload, buf.Length());
720     }
721     return true;
722   }
723
724   // Parse all RTCP packet, from start_index to stop_index, and count how many
725   // FIR (PT=206 and FMT=4 according to RFC 5104). If successful, set the count
726   // and return true.
727   bool CountRtcpFir(int start_index, int stop_index, int* fir_count) {
728     int count = 0;
729     for (int i = start_index; i < stop_index; ++i) {
730       rtc::scoped_ptr<const rtc::Buffer> p(GetRtcpPacket(i));
731       rtc::ByteBuffer buf(p->data(), p->length());
732       size_t total_len = 0;
733       // The packet may be a compound RTCP packet.
734       while (total_len < p->length()) {
735         // Read FMT, type and length.
736         uint8 fmt = 0;
737         uint8 type = 0;
738         uint16 length = 0;
739         if (!buf.ReadUInt8(&fmt)) return false;
740         fmt &= 0x1F;
741         if (!buf.ReadUInt8(&type)) return false;
742         if (!buf.ReadUInt16(&length)) return false;
743         buf.Consume(length * 4);  // Skip RTCP data.
744         total_len += (length + 1) * 4;
745         if ((192 == type) || ((206 == type) && (4 == fmt))) {
746           ++count;
747         }
748       }
749     }
750
751     if (fir_count) {
752       *fir_count = count;
753     }
754     return true;
755   }
756
757   void OnVideoChannelError(uint32 ssrc,
758                            cricket::VideoMediaChannel::Error error) {
759     media_error_ = error;
760   }
761
762   // Test that SetSend works.
763   void SetSend() {
764     EXPECT_FALSE(channel_->sending());
765     EXPECT_TRUE(channel_->SetCapturer(kSsrc, video_capturer_.get()));
766     EXPECT_TRUE(SetOneCodec(DefaultCodec()));
767     EXPECT_FALSE(channel_->sending());
768     EXPECT_TRUE(SetSend(true));
769     EXPECT_TRUE(channel_->sending());
770     EXPECT_TRUE(SendFrame());
771     EXPECT_TRUE_WAIT(NumRtpPackets() > 0, kTimeout);
772     EXPECT_TRUE(SetSend(false));
773     EXPECT_FALSE(channel_->sending());
774   }
775   // Test that SetSend fails without codecs being set.
776   void SetSendWithoutCodecs() {
777     EXPECT_FALSE(channel_->sending());
778     EXPECT_FALSE(SetSend(true));
779     EXPECT_FALSE(channel_->sending());
780   }
781   // Test that we properly set the send and recv buffer sizes by the time
782   // SetSend is called.
783   void SetSendSetsTransportBufferSizes() {
784     EXPECT_TRUE(SetOneCodec(DefaultCodec()));
785     EXPECT_TRUE(SetSend(true));
786     EXPECT_EQ(64 * 1024, network_interface_.sendbuf_size());
787     EXPECT_EQ(64 * 1024, network_interface_.recvbuf_size());
788   }
789   // Tests that we can send frames and the right payload type is used.
790   void Send(const cricket::VideoCodec& codec) {
791     EXPECT_TRUE(SetOneCodec(codec));
792     EXPECT_TRUE(SetSend(true));
793     EXPECT_TRUE(SendFrame());
794     EXPECT_TRUE_WAIT(NumRtpPackets() > 0, kTimeout);
795     rtc::scoped_ptr<const rtc::Buffer> p(GetRtpPacket(0));
796     EXPECT_EQ(codec.id, GetPayloadType(p.get()));
797   }
798   // Tests that we can send and receive frames.
799   void SendAndReceive(const cricket::VideoCodec& codec) {
800     EXPECT_TRUE(SetOneCodec(codec));
801     EXPECT_TRUE(SetSend(true));
802     EXPECT_TRUE(channel_->SetRender(true));
803     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
804     EXPECT_EQ(0, renderer_.num_rendered_frames());
805     EXPECT_TRUE(SendFrame());
806     EXPECT_FRAME_WAIT(1, codec.width, codec.height, kTimeout);
807     rtc::scoped_ptr<const rtc::Buffer> p(GetRtpPacket(0));
808     EXPECT_EQ(codec.id, GetPayloadType(p.get()));
809   }
810   // Tests that we only get a VideoRenderer::SetSize() callback when needed.
811   void SendManyResizeOnce() {
812     cricket::VideoCodec codec(DefaultCodec());
813     EXPECT_TRUE(SetOneCodec(codec));
814     EXPECT_TRUE(SetSend(true));
815     EXPECT_TRUE(channel_->SetRender(true));
816     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
817     EXPECT_EQ(0, renderer_.num_rendered_frames());
818     EXPECT_TRUE(WaitAndSendFrame(30));
819     EXPECT_FRAME_WAIT(1, codec.width, codec.height, kTimeout);
820     EXPECT_TRUE(WaitAndSendFrame(30));
821     EXPECT_FRAME_WAIT(2, codec.width, codec.height, kTimeout);
822     rtc::scoped_ptr<const rtc::Buffer> p(GetRtpPacket(0));
823     EXPECT_EQ(codec.id, GetPayloadType(p.get()));
824     EXPECT_EQ(1, renderer_.num_set_sizes());
825
826     codec.width /= 2;
827     codec.height /= 2;
828     EXPECT_TRUE(SetOneCodec(codec));
829     EXPECT_TRUE(WaitAndSendFrame(30));
830     EXPECT_FRAME_WAIT(3, codec.width, codec.height, kTimeout);
831     EXPECT_EQ(2, renderer_.num_set_sizes());
832   }
833   void SendReceiveManyAndGetStats(const cricket::VideoCodec& codec,
834                                   int duration_sec, int fps) {
835     EXPECT_TRUE(SetOneCodec(codec));
836     EXPECT_TRUE(SetSend(true));
837     EXPECT_TRUE(channel_->SetRender(true));
838     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
839     EXPECT_EQ(0, renderer_.num_rendered_frames());
840     for (int i = 0; i < duration_sec; ++i) {
841       for (int frame = 1; frame <= fps; ++frame) {
842         EXPECT_TRUE(WaitAndSendFrame(1000 / fps));
843         EXPECT_FRAME_WAIT(frame + i * fps, codec.width, codec.height, kTimeout);
844       }
845       cricket::VideoMediaInfo info;
846       EXPECT_TRUE(channel_->GetStats(cricket::StatsOptions(), &info));
847       // For webrtc, |framerate_sent| and |framerate_rcvd| depend on periodic
848       // callbacks (1 sec).
849       // Received |fraction_lost| and |packets_lost| are from sent RTCP packet.
850       // One sent packet needed (sent about once per second).
851       // |framerate_input|, |framerate_decoded| and |framerate_output| are using
852       // RateTracker. RateTracker needs to be called twice (with >1 second in
853       // b/w calls) before a framerate is calculated.
854       // Therefore insert frames (and call GetStats each sec) for a few seconds
855       // before testing stats.
856     }
857     rtc::scoped_ptr<const rtc::Buffer> p(GetRtpPacket(0));
858     EXPECT_EQ(codec.id, GetPayloadType(p.get()));
859   }
860
861   // Test that stats work properly for a 1-1 call.
862   void GetStats() {
863     const int kDurationSec = 3;
864     const int kFps = 10;
865     SendReceiveManyAndGetStats(DefaultCodec(), kDurationSec, kFps);
866
867     cricket::VideoMediaInfo info;
868     EXPECT_TRUE(channel_->GetStats(cricket::StatsOptions(), &info));
869
870     ASSERT_EQ(1U, info.senders.size());
871     // TODO(whyuan): bytes_sent and bytes_rcvd are different. Are both payload?
872     // For webrtc, bytes_sent does not include the RTP header length.
873     EXPECT_GT(info.senders[0].bytes_sent, 0);
874     EXPECT_EQ(NumRtpPackets(), info.senders[0].packets_sent);
875     EXPECT_EQ(0.0, info.senders[0].fraction_lost);
876     EXPECT_EQ(0, info.senders[0].firs_rcvd);
877     EXPECT_EQ(0, info.senders[0].plis_rcvd);
878     EXPECT_EQ(0, info.senders[0].nacks_rcvd);
879     EXPECT_EQ(DefaultCodec().width, info.senders[0].send_frame_width);
880     EXPECT_EQ(DefaultCodec().height, info.senders[0].send_frame_height);
881     EXPECT_GT(info.senders[0].framerate_input, 0);
882     EXPECT_GT(info.senders[0].framerate_sent, 0);
883
884     ASSERT_EQ(1U, info.receivers.size());
885     EXPECT_EQ(1U, info.senders[0].ssrcs().size());
886     EXPECT_EQ(1U, info.receivers[0].ssrcs().size());
887     EXPECT_EQ(info.senders[0].ssrcs()[0], info.receivers[0].ssrcs()[0]);
888     EXPECT_EQ(NumRtpBytes(), info.receivers[0].bytes_rcvd);
889     EXPECT_EQ(NumRtpPackets(), info.receivers[0].packets_rcvd);
890     EXPECT_EQ(0.0, info.receivers[0].fraction_lost);
891     EXPECT_EQ(0, info.receivers[0].packets_lost);
892     // TODO(asapersson): Not set for webrtc. Handle missing stats.
893     // EXPECT_EQ(0, info.receivers[0].packets_concealed);
894     EXPECT_EQ(0, info.receivers[0].firs_sent);
895     EXPECT_EQ(0, info.receivers[0].plis_sent);
896     EXPECT_EQ(0, info.receivers[0].nacks_sent);
897     EXPECT_EQ(DefaultCodec().width, info.receivers[0].frame_width);
898     EXPECT_EQ(DefaultCodec().height, info.receivers[0].frame_height);
899     EXPECT_GT(info.receivers[0].framerate_rcvd, 0);
900     EXPECT_GT(info.receivers[0].framerate_decoded, 0);
901     EXPECT_GT(info.receivers[0].framerate_output, 0);
902   }
903   // Test that stats work properly for a conf call with multiple recv streams.
904   void GetStatsMultipleRecvStreams() {
905     cricket::FakeVideoRenderer renderer1, renderer2;
906     EXPECT_TRUE(SetOneCodec(DefaultCodec()));
907     cricket::VideoOptions vmo;
908     vmo.conference_mode.Set(true);
909     EXPECT_TRUE(channel_->SetOptions(vmo));
910     EXPECT_TRUE(SetSend(true));
911     EXPECT_TRUE(channel_->AddRecvStream(
912         cricket::StreamParams::CreateLegacy(1)));
913     EXPECT_TRUE(channel_->AddRecvStream(
914         cricket::StreamParams::CreateLegacy(2)));
915     EXPECT_TRUE(channel_->SetRenderer(1, &renderer1));
916     EXPECT_TRUE(channel_->SetRenderer(2, &renderer2));
917     EXPECT_TRUE(channel_->SetRender(true));
918     EXPECT_EQ(0, renderer1.num_rendered_frames());
919     EXPECT_EQ(0, renderer2.num_rendered_frames());
920     std::vector<uint32> ssrcs;
921     ssrcs.push_back(1);
922     ssrcs.push_back(2);
923     network_interface_.SetConferenceMode(true, ssrcs);
924     EXPECT_TRUE(SendFrame());
925     EXPECT_FRAME_ON_RENDERER_WAIT(
926         renderer1, 1, DefaultCodec().width, DefaultCodec().height, kTimeout);
927     EXPECT_FRAME_ON_RENDERER_WAIT(
928         renderer2, 1, DefaultCodec().width, DefaultCodec().height, kTimeout);
929     cricket::VideoMediaInfo info;
930     EXPECT_TRUE(channel_->GetStats(cricket::StatsOptions(), &info));
931
932     ASSERT_EQ(1U, info.senders.size());
933     // TODO(whyuan): bytes_sent and bytes_rcvd are different. Are both payload?
934     // For webrtc, bytes_sent does not include the RTP header length.
935     EXPECT_GT(info.senders[0].bytes_sent, 0);
936     EXPECT_EQ(NumRtpPackets(), info.senders[0].packets_sent);
937     EXPECT_EQ(DefaultCodec().width, info.senders[0].send_frame_width);
938     EXPECT_EQ(DefaultCodec().height, info.senders[0].send_frame_height);
939
940     ASSERT_EQ(2U, info.receivers.size());
941     for (size_t i = 0; i < info.receivers.size(); ++i) {
942       EXPECT_EQ(1U, info.receivers[i].ssrcs().size());
943       EXPECT_EQ(i + 1, info.receivers[i].ssrcs()[0]);
944       EXPECT_EQ(NumRtpBytes(), info.receivers[i].bytes_rcvd);
945       EXPECT_EQ(NumRtpPackets(), info.receivers[i].packets_rcvd);
946       EXPECT_EQ(DefaultCodec().width, info.receivers[i].frame_width);
947       EXPECT_EQ(DefaultCodec().height, info.receivers[i].frame_height);
948     }
949   }
950   // Test that stats work properly for a conf call with multiple send streams.
951   void GetStatsMultipleSendStreams() {
952     // Normal setup; note that we set the SSRC explicitly to ensure that
953     // it will come first in the senders map.
954     EXPECT_TRUE(SetOneCodec(DefaultCodec()));
955     cricket::VideoOptions vmo;
956     vmo.conference_mode.Set(true);
957     EXPECT_TRUE(channel_->SetOptions(vmo));
958     EXPECT_TRUE(channel_->AddRecvStream(
959         cricket::StreamParams::CreateLegacy(kSsrc)));
960     EXPECT_TRUE(channel_->SetRenderer(kSsrc, &renderer_));
961     channel_->UpdateAspectRatio(640, 400);
962     EXPECT_TRUE(SetSend(true));
963     EXPECT_TRUE(channel_->SetRender(true));
964     EXPECT_TRUE(SendFrame());
965     EXPECT_TRUE_WAIT(NumRtpPackets() > 0, kTimeout);
966     EXPECT_FRAME_WAIT(1, DefaultCodec().width, DefaultCodec().height, kTimeout);
967
968     // Add an additional capturer, and hook up a renderer to receive it.
969     cricket::FakeVideoRenderer renderer1;
970     rtc::scoped_ptr<cricket::FakeVideoCapturer> capturer(
971         CreateFakeVideoCapturer());
972     capturer->SetScreencast(true);
973     const int kTestWidth = 160;
974     const int kTestHeight = 120;
975     cricket::VideoFormat format(kTestWidth, kTestHeight,
976                                 cricket::VideoFormat::FpsToInterval(5),
977                                 cricket::FOURCC_I420);
978     EXPECT_EQ(cricket::CS_RUNNING, capturer->Start(format));
979     EXPECT_TRUE(channel_->AddSendStream(
980         cricket::StreamParams::CreateLegacy(5678)));
981     EXPECT_TRUE(channel_->SetCapturer(5678, capturer.get()));
982     EXPECT_TRUE(channel_->AddRecvStream(
983         cricket::StreamParams::CreateLegacy(5678)));
984     EXPECT_TRUE(channel_->SetRenderer(5678, &renderer1));
985     EXPECT_TRUE(capturer->CaptureCustomFrame(
986         kTestWidth, kTestHeight, cricket::FOURCC_I420));
987     EXPECT_FRAME_ON_RENDERER_WAIT(
988         renderer1, 1, kTestWidth, kTestHeight, kTimeout);
989
990     // Get stats, and make sure they are correct for two senders.
991     cricket::VideoMediaInfo info;
992     EXPECT_TRUE(channel_->GetStats(cricket::StatsOptions(), &info));
993     ASSERT_EQ(2U, info.senders.size());
994     EXPECT_EQ(NumRtpPackets(),
995         info.senders[0].packets_sent + info.senders[1].packets_sent);
996     EXPECT_EQ(1U, info.senders[0].ssrcs().size());
997     EXPECT_EQ(1234U, info.senders[0].ssrcs()[0]);
998     EXPECT_EQ(DefaultCodec().width, info.senders[0].send_frame_width);
999     EXPECT_EQ(DefaultCodec().height, info.senders[0].send_frame_height);
1000     EXPECT_EQ(1U, info.senders[1].ssrcs().size());
1001     EXPECT_EQ(5678U, info.senders[1].ssrcs()[0]);
1002     EXPECT_EQ(kTestWidth, info.senders[1].send_frame_width);
1003     EXPECT_EQ(kTestHeight, info.senders[1].send_frame_height);
1004     // The capturer must be unregistered here as it runs out of it's scope next.
1005     EXPECT_TRUE(channel_->SetCapturer(5678, NULL));
1006   }
1007
1008   // Test that we can set the bandwidth.
1009   void SetSendBandwidth() {
1010     EXPECT_TRUE(channel_->SetMaxSendBandwidth(-1));  // <= 0 means unlimited.
1011     EXPECT_TRUE(channel_->SetMaxSendBandwidth(128 * 1024));
1012   }
1013   // Test that we can set the SSRC for the default send source.
1014   void SetSendSsrc() {
1015     EXPECT_TRUE(SetDefaultCodec());
1016     EXPECT_TRUE(SetSendStreamFormat(kSsrc, DefaultCodec()));
1017     EXPECT_TRUE(SetSend(true));
1018     EXPECT_TRUE(SendFrame());
1019     EXPECT_TRUE_WAIT(NumRtpPackets() > 0, kTimeout);
1020     uint32 ssrc = 0;
1021     rtc::scoped_ptr<const rtc::Buffer> p(GetRtpPacket(0));
1022     ParseRtpPacket(p.get(), NULL, NULL, NULL, NULL, &ssrc, NULL);
1023     EXPECT_EQ(kSsrc, ssrc);
1024     EXPECT_EQ(NumRtpPackets(), NumRtpPackets(ssrc));
1025     EXPECT_EQ(NumRtpBytes(), NumRtpBytes(ssrc));
1026     EXPECT_EQ(1, NumSentSsrcs());
1027     EXPECT_EQ(0, NumRtpPackets(kSsrc - 1));
1028     EXPECT_EQ(0, NumRtpBytes(kSsrc - 1));
1029   }
1030   // Test that we can set the SSRC even after codecs are set.
1031   void SetSendSsrcAfterSetCodecs() {
1032     // Remove stream added in Setup.
1033     EXPECT_TRUE(channel_->RemoveSendStream(kSsrc));
1034     EXPECT_TRUE(SetDefaultCodec());
1035     EXPECT_TRUE(channel_->AddSendStream(
1036         cricket::StreamParams::CreateLegacy(999)));
1037     EXPECT_TRUE(channel_->SetCapturer(999u, video_capturer_.get()));
1038     EXPECT_TRUE(SetSendStreamFormat(999u, DefaultCodec()));
1039     EXPECT_TRUE(SetSend(true));
1040     EXPECT_TRUE(WaitAndSendFrame(0));
1041     EXPECT_TRUE_WAIT(NumRtpPackets() > 0, kTimeout);
1042     uint32 ssrc = 0;
1043     rtc::scoped_ptr<const rtc::Buffer> p(GetRtpPacket(0));
1044     ParseRtpPacket(p.get(), NULL, NULL, NULL, NULL, &ssrc, NULL);
1045     EXPECT_EQ(999u, ssrc);
1046     EXPECT_EQ(NumRtpPackets(), NumRtpPackets(ssrc));
1047     EXPECT_EQ(NumRtpBytes(), NumRtpBytes(ssrc));
1048     EXPECT_EQ(1, NumSentSsrcs());
1049     EXPECT_EQ(0, NumRtpPackets(kSsrc));
1050     EXPECT_EQ(0, NumRtpBytes(kSsrc));
1051   }
1052   // Test that we can set the default video renderer before and after
1053   // media is received.
1054   void SetRenderer() {
1055     uint8 data1[] = {
1056         0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1057     };
1058
1059     rtc::Buffer packet1(data1, sizeof(data1));
1060     rtc::SetBE32(packet1.data() + 8, kSsrc);
1061     channel_->SetRenderer(kDefaultReceiveSsrc, NULL);
1062     EXPECT_TRUE(SetDefaultCodec());
1063     EXPECT_TRUE(SetSend(true));
1064     EXPECT_TRUE(channel_->SetRender(true));
1065     EXPECT_EQ(0, renderer_.num_rendered_frames());
1066     channel_->OnPacketReceived(&packet1, rtc::PacketTime());
1067     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
1068     EXPECT_TRUE(SendFrame());
1069     EXPECT_FRAME_WAIT(1, DefaultCodec().width, DefaultCodec().height, kTimeout);
1070   }
1071
1072   // Tests empty StreamParams is rejected.
1073   void RejectEmptyStreamParams() {
1074     // Remove the send stream that was added during Setup.
1075     EXPECT_TRUE(channel_->RemoveSendStream(kSsrc));
1076
1077     cricket::StreamParams empty;
1078     EXPECT_FALSE(channel_->AddSendStream(empty));
1079     EXPECT_TRUE(channel_->AddSendStream(
1080         cricket::StreamParams::CreateLegacy(789u)));
1081   }
1082
1083   // Tests setting up and configuring a send stream.
1084   void AddRemoveSendStreams() {
1085     EXPECT_TRUE(SetOneCodec(DefaultCodec()));
1086     EXPECT_TRUE(SetSend(true));
1087     EXPECT_TRUE(channel_->SetRender(true));
1088     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
1089     EXPECT_TRUE(SendFrame());
1090     EXPECT_FRAME_WAIT(1, DefaultCodec().width, DefaultCodec().height, kTimeout);
1091     EXPECT_GE(2, NumRtpPackets());
1092     uint32 ssrc = 0;
1093     size_t last_packet = NumRtpPackets() - 1;
1094     rtc::scoped_ptr<const rtc::Buffer>
1095         p(GetRtpPacket(static_cast<int>(last_packet)));
1096     ParseRtpPacket(p.get(), NULL, NULL, NULL, NULL, &ssrc, NULL);
1097     EXPECT_EQ(kSsrc, ssrc);
1098
1099     // Remove the send stream that was added during Setup.
1100     EXPECT_TRUE(channel_->RemoveSendStream(kSsrc));
1101     int rtp_packets = NumRtpPackets();
1102
1103     EXPECT_TRUE(channel_->AddSendStream(
1104         cricket::StreamParams::CreateLegacy(789u)));
1105     EXPECT_TRUE(channel_->SetCapturer(789u, video_capturer_.get()));
1106     EXPECT_EQ(rtp_packets, NumRtpPackets());
1107     // Wait 30ms to guarantee the engine does not drop the frame.
1108     EXPECT_TRUE(WaitAndSendFrame(30));
1109     EXPECT_TRUE_WAIT(NumRtpPackets() > rtp_packets, kTimeout);
1110
1111     last_packet = NumRtpPackets() - 1;
1112     p.reset(GetRtpPacket(static_cast<int>(last_packet)));
1113     ParseRtpPacket(p.get(), NULL, NULL, NULL, NULL, &ssrc, NULL);
1114     EXPECT_EQ(789u, ssrc);
1115   }
1116
1117   // Tests adding streams already exists returns false.
1118   void AddRecvStreamsAlreadyExist() {
1119     cricket::VideoOptions vmo;
1120     vmo.conference_mode.Set(true);
1121     EXPECT_TRUE(channel_->SetOptions(vmo));
1122
1123     EXPECT_FALSE(channel_->AddRecvStream(
1124         cricket::StreamParams::CreateLegacy(0)));
1125
1126     EXPECT_TRUE(channel_->AddRecvStream(
1127         cricket::StreamParams::CreateLegacy(1)));
1128     EXPECT_FALSE(channel_->AddRecvStream(
1129         cricket::StreamParams::CreateLegacy(1)));
1130
1131     EXPECT_TRUE(channel_->RemoveRecvStream(1));
1132     EXPECT_FALSE(channel_->AddRecvStream(
1133         cricket::StreamParams::CreateLegacy(0)));
1134     EXPECT_TRUE(channel_->AddRecvStream(
1135         cricket::StreamParams::CreateLegacy(1)));
1136   }
1137
1138   // Tests setting up and configuring multiple incoming streams.
1139   void AddRemoveRecvStreams() {
1140     cricket::FakeVideoRenderer renderer1, renderer2;
1141     cricket::VideoOptions vmo;
1142     vmo.conference_mode.Set(true);
1143     EXPECT_TRUE(channel_->SetOptions(vmo));
1144     // Ensure we can't set the renderer on a non-existent stream.
1145     EXPECT_FALSE(channel_->SetRenderer(1, &renderer1));
1146     EXPECT_FALSE(channel_->SetRenderer(2, &renderer2));
1147     cricket::VideoRenderer* renderer;
1148     EXPECT_FALSE(channel_->GetRenderer(1, &renderer));
1149     EXPECT_FALSE(channel_->GetRenderer(2, &renderer));
1150
1151     // Ensure we can add streams.
1152     EXPECT_TRUE(channel_->AddRecvStream(
1153         cricket::StreamParams::CreateLegacy(1)));
1154     EXPECT_TRUE(channel_->AddRecvStream(
1155         cricket::StreamParams::CreateLegacy(2)));
1156     EXPECT_TRUE(channel_->GetRenderer(1, &renderer));
1157     EXPECT_TRUE(renderer == NULL);
1158     EXPECT_TRUE(channel_->GetRenderer(2, &renderer));
1159     EXPECT_TRUE(NULL == renderer);
1160
1161     // Ensure we can now set the renderers.
1162     EXPECT_TRUE(channel_->SetRenderer(1, &renderer1));
1163     EXPECT_TRUE(channel_->SetRenderer(2, &renderer2));
1164     EXPECT_TRUE(channel_->GetRenderer(1, &renderer));
1165     EXPECT_TRUE(&renderer1 == renderer);
1166     EXPECT_TRUE(channel_->GetRenderer(2, &renderer));
1167     EXPECT_TRUE(&renderer2 == renderer);
1168
1169     // Ensure we can change the renderers if needed.
1170     EXPECT_TRUE(channel_->SetRenderer(1, &renderer2));
1171     EXPECT_TRUE(channel_->SetRenderer(2, &renderer1));
1172     EXPECT_TRUE(channel_->GetRenderer(1, &renderer));
1173     EXPECT_TRUE(&renderer2 == renderer);
1174     EXPECT_TRUE(channel_->GetRenderer(2, &renderer));
1175     EXPECT_TRUE(&renderer1 == renderer);
1176
1177     EXPECT_TRUE(channel_->RemoveRecvStream(2));
1178     EXPECT_TRUE(channel_->RemoveRecvStream(1));
1179     EXPECT_FALSE(channel_->GetRenderer(1, &renderer));
1180     EXPECT_FALSE(channel_->GetRenderer(2, &renderer));
1181   }
1182
1183   // Tests setting up and configuring multiple incoming streams in a
1184   // non-conference call.
1185   void AddRemoveRecvStreamsNoConference() {
1186     cricket::FakeVideoRenderer renderer1, renderer2;
1187     // Ensure we can't set the renderer on a non-existent stream.
1188     EXPECT_FALSE(channel_->SetRenderer(1, &renderer1));
1189     EXPECT_FALSE(channel_->SetRenderer(2, &renderer2));
1190     cricket::VideoRenderer* renderer;
1191     EXPECT_FALSE(channel_->GetRenderer(1, &renderer));
1192     EXPECT_FALSE(channel_->GetRenderer(2, &renderer));
1193
1194     // Ensure we can add streams.
1195     EXPECT_TRUE(channel_->AddRecvStream(
1196         cricket::StreamParams::CreateLegacy(1)));
1197     EXPECT_TRUE(channel_->AddRecvStream(
1198         cricket::StreamParams::CreateLegacy(2)));
1199     EXPECT_TRUE(channel_->GetRenderer(1, &renderer));
1200     // Verify the first AddRecvStream hook up to the default renderer.
1201     EXPECT_TRUE(renderer == NULL);
1202     EXPECT_TRUE(channel_->GetRenderer(2, &renderer));
1203     EXPECT_TRUE(NULL == renderer);
1204
1205     // Ensure we can now set the renderers.
1206     EXPECT_TRUE(channel_->SetRenderer(1, &renderer1));
1207     EXPECT_TRUE(channel_->SetRenderer(2, &renderer2));
1208     EXPECT_TRUE(channel_->GetRenderer(1, &renderer));
1209     EXPECT_TRUE(&renderer1 == renderer);
1210     EXPECT_TRUE(channel_->GetRenderer(2, &renderer));
1211     EXPECT_TRUE(&renderer2 == renderer);
1212
1213     // Ensure we can change the renderers if needed.
1214     EXPECT_TRUE(channel_->SetRenderer(1, &renderer2));
1215     EXPECT_TRUE(channel_->SetRenderer(2, &renderer1));
1216     EXPECT_TRUE(channel_->GetRenderer(1, &renderer));
1217     EXPECT_TRUE(&renderer2 == renderer);
1218     EXPECT_TRUE(channel_->GetRenderer(2, &renderer));
1219     EXPECT_TRUE(&renderer1 == renderer);
1220
1221     EXPECT_TRUE(channel_->RemoveRecvStream(2));
1222     EXPECT_TRUE(channel_->RemoveRecvStream(1));
1223     EXPECT_FALSE(channel_->GetRenderer(1, &renderer));
1224     EXPECT_FALSE(channel_->GetRenderer(2, &renderer));
1225   }
1226
1227   // Test that no frames are rendered after the receive stream have been
1228   // removed.
1229   void AddRemoveRecvStreamAndRender() {
1230     cricket::FakeVideoRenderer renderer1;
1231     EXPECT_TRUE(SetDefaultCodec());
1232     EXPECT_TRUE(SetSend(true));
1233     EXPECT_TRUE(channel_->SetRender(true));
1234     EXPECT_TRUE(channel_->AddRecvStream(
1235         cricket::StreamParams::CreateLegacy(kSsrc)));
1236     EXPECT_TRUE(channel_->SetRenderer(kSsrc, &renderer1));
1237
1238     EXPECT_TRUE(SendFrame());
1239     EXPECT_FRAME_ON_RENDERER_WAIT(
1240         renderer1, 1, DefaultCodec().width, DefaultCodec().height, kTimeout);
1241     EXPECT_TRUE(channel_->RemoveRecvStream(kSsrc));
1242     // Send three more frames. This is to avoid that the test might be flaky
1243     // due to frame dropping.
1244     for (size_t i = 0; i < 3; ++i)
1245       EXPECT_TRUE(WaitAndSendFrame(100));
1246
1247     // Test that no more frames have been rendered.
1248     EXPECT_EQ(1, renderer1.num_rendered_frames());
1249
1250     // Re-add the stream again and make sure it renders.
1251     EXPECT_TRUE(channel_->AddRecvStream(
1252         cricket::StreamParams::CreateLegacy(kSsrc)));
1253     // Force the next frame to be a key frame to make the receiving
1254     // decoder happy.
1255     EXPECT_TRUE(channel_->SendIntraFrame());
1256
1257     EXPECT_TRUE(channel_->SetRenderer(kSsrc, &renderer1));
1258     EXPECT_TRUE(SendFrame());
1259     // Because the default channel is used, RemoveRecvStream above is not going
1260     // to delete the channel. As a result the engine will continue to receive
1261     // and decode the 3 frames sent above. So it is possible we will receive
1262     // some (e.g. 1) of these 3 frames after the renderer is set again.
1263     EXPECT_GT_FRAME_ON_RENDERER_WAIT(
1264         renderer1, 2, DefaultCodec().width, DefaultCodec().height, kTimeout);
1265     // Detach |renderer1| before exit as there might be frames come late.
1266     EXPECT_TRUE(channel_->SetRenderer(kSsrc, NULL));
1267   }
1268
1269   // Tests the behavior of incoming streams in a conference scenario.
1270   void SimulateConference() {
1271     cricket::FakeVideoRenderer renderer1, renderer2;
1272     EXPECT_TRUE(SetDefaultCodec());
1273     cricket::VideoOptions vmo;
1274     vmo.conference_mode.Set(true);
1275     EXPECT_TRUE(channel_->SetOptions(vmo));
1276     EXPECT_TRUE(SetSend(true));
1277     EXPECT_TRUE(channel_->SetRender(true));
1278     EXPECT_TRUE(channel_->AddRecvStream(
1279         cricket::StreamParams::CreateLegacy(1)));
1280     EXPECT_TRUE(channel_->AddRecvStream(
1281         cricket::StreamParams::CreateLegacy(2)));
1282     EXPECT_TRUE(channel_->SetRenderer(1, &renderer1));
1283     EXPECT_TRUE(channel_->SetRenderer(2, &renderer2));
1284     EXPECT_EQ(0, renderer1.num_rendered_frames());
1285     EXPECT_EQ(0, renderer2.num_rendered_frames());
1286     std::vector<uint32> ssrcs;
1287     ssrcs.push_back(1);
1288     ssrcs.push_back(2);
1289     network_interface_.SetConferenceMode(true, ssrcs);
1290     EXPECT_TRUE(SendFrame());
1291     EXPECT_FRAME_ON_RENDERER_WAIT(
1292         renderer1, 1, DefaultCodec().width, DefaultCodec().height, kTimeout);
1293     EXPECT_FRAME_ON_RENDERER_WAIT(
1294         renderer2, 1, DefaultCodec().width, DefaultCodec().height, kTimeout);
1295
1296     rtc::scoped_ptr<const rtc::Buffer> p(GetRtpPacket(0));
1297     EXPECT_EQ(DefaultCodec().id, GetPayloadType(p.get()));
1298     EXPECT_EQ(DefaultCodec().width, renderer1.width());
1299     EXPECT_EQ(DefaultCodec().height, renderer1.height());
1300     EXPECT_EQ(DefaultCodec().width, renderer2.width());
1301     EXPECT_EQ(DefaultCodec().height, renderer2.height());
1302     EXPECT_TRUE(channel_->RemoveRecvStream(2));
1303     EXPECT_TRUE(channel_->RemoveRecvStream(1));
1304   }
1305
1306   // Tests that we can add and remove capturers and frames are sent out properly
1307   void AddRemoveCapturer() {
1308     cricket::VideoCodec codec = DefaultCodec();
1309     codec.width = 320;
1310     codec.height = 240;
1311     const int time_between_send = TimeBetweenSend(codec);
1312     EXPECT_TRUE(SetOneCodec(codec));
1313     EXPECT_TRUE(SetSend(true));
1314     EXPECT_TRUE(channel_->SetRender(true));
1315     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
1316     EXPECT_EQ(0, renderer_.num_rendered_frames());
1317     EXPECT_TRUE(SendFrame());
1318     EXPECT_FRAME_WAIT(1, codec.width, codec.height, kTimeout);
1319     rtc::scoped_ptr<cricket::FakeVideoCapturer> capturer(
1320         CreateFakeVideoCapturer());
1321     capturer->SetScreencast(true);
1322     cricket::VideoFormat format(480, 360,
1323                                 cricket::VideoFormat::FpsToInterval(30),
1324                                 cricket::FOURCC_I420);
1325     EXPECT_EQ(cricket::CS_RUNNING, capturer->Start(format));
1326     // All capturers start generating frames with the same timestamp. ViE does
1327     // not allow the same timestamp to be used. Capture one frame before
1328     // associating the capturer with the channel.
1329     EXPECT_TRUE(capturer->CaptureCustomFrame(format.width, format.height,
1330                                              cricket::FOURCC_I420));
1331
1332     int captured_frames = 1;
1333     for (int iterations = 0; iterations < 2; ++iterations) {
1334       EXPECT_TRUE(channel_->SetCapturer(kSsrc, capturer.get()));
1335       rtc::Thread::Current()->ProcessMessages(time_between_send);
1336       EXPECT_TRUE(capturer->CaptureCustomFrame(format.width, format.height,
1337                                                cricket::FOURCC_I420));
1338       ++captured_frames;
1339       // Wait until frame of right size is captured.
1340       EXPECT_TRUE_WAIT(renderer_.num_rendered_frames() >= captured_frames &&
1341                        format.width == renderer_.width() &&
1342                        format.height == renderer_.height() &&
1343                        !renderer_.black_frame(), kTimeout);
1344       EXPECT_GE(renderer_.num_rendered_frames(), captured_frames);
1345       EXPECT_EQ(format.width, renderer_.width());
1346       EXPECT_EQ(format.height, renderer_.height());
1347       captured_frames = renderer_.num_rendered_frames() + 1;
1348       EXPECT_FALSE(renderer_.black_frame());
1349       EXPECT_TRUE(channel_->SetCapturer(kSsrc, NULL));
1350       // Make sure a black frame is generated within the specified timeout.
1351       // The black frame should be the resolution of the send codec.
1352       EXPECT_TRUE_WAIT(renderer_.num_rendered_frames() >= captured_frames &&
1353                        codec.width == renderer_.width() &&
1354                        codec.height == renderer_.height() &&
1355                        renderer_.black_frame(), kTimeout);
1356       EXPECT_GE(renderer_.num_rendered_frames(), captured_frames);
1357       EXPECT_EQ(codec.width, renderer_.width());
1358       EXPECT_EQ(codec.height, renderer_.height());
1359       EXPECT_TRUE(renderer_.black_frame());
1360
1361       // The black frame has the same timestamp as the next frame since it's
1362       // timestamp is set to the last frame's timestamp + interval. WebRTC will
1363       // not render a frame with the same timestamp so capture another frame
1364       // with the frame capturer to increment the next frame's timestamp.
1365       EXPECT_TRUE(capturer->CaptureCustomFrame(format.width, format.height,
1366                                                cricket::FOURCC_I420));
1367     }
1368   }
1369
1370   // Tests that if RemoveCapturer is called without a capturer ever being
1371   // added, the plugin shouldn't crash (and no black frame should be sent).
1372   void RemoveCapturerWithoutAdd() {
1373     EXPECT_TRUE(SetOneCodec(DefaultCodec()));
1374     EXPECT_TRUE(SetSend(true));
1375     EXPECT_TRUE(channel_->SetRender(true));
1376     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
1377     EXPECT_EQ(0, renderer_.num_rendered_frames());
1378     EXPECT_TRUE(SendFrame());
1379     EXPECT_FRAME_WAIT(1, 640, 400, kTimeout);
1380     // Wait for one frame so they don't get dropped because we send frames too
1381     // tightly.
1382     rtc::Thread::Current()->ProcessMessages(30);
1383     // Remove the capturer.
1384     EXPECT_TRUE(channel_->SetCapturer(kSsrc, NULL));
1385     // Wait for one black frame for removing the capturer.
1386     EXPECT_FRAME_WAIT(2, 640, 400, kTimeout);
1387
1388     // No capturer was added, so this RemoveCapturer should
1389     // fail.
1390     EXPECT_FALSE(channel_->SetCapturer(kSsrc, NULL));
1391     rtc::Thread::Current()->ProcessMessages(300);
1392     // Verify no more frames were sent.
1393     EXPECT_EQ(2, renderer_.num_rendered_frames());
1394   }
1395
1396   // Tests that we can add and remove capturer as unique sources.
1397   void AddRemoveCapturerMultipleSources() {
1398     // WebRTC implementation will drop frames if pushed to quickly. Wait the
1399     // interval time to avoid that.
1400     // WebRTC implementation will drop frames if pushed to quickly. Wait the
1401     // interval time to avoid that.
1402     // Set up the stream associated with the engine.
1403     EXPECT_TRUE(channel_->AddRecvStream(
1404         cricket::StreamParams::CreateLegacy(kSsrc)));
1405     EXPECT_TRUE(channel_->SetRenderer(kSsrc, &renderer_));
1406     cricket::VideoFormat capture_format;  // default format
1407     capture_format.interval = cricket::VideoFormat::FpsToInterval(30);
1408     // Set up additional stream 1.
1409     cricket::FakeVideoRenderer renderer1;
1410     EXPECT_FALSE(channel_->SetRenderer(1, &renderer1));
1411     EXPECT_TRUE(channel_->AddRecvStream(
1412         cricket::StreamParams::CreateLegacy(1)));
1413     EXPECT_TRUE(channel_->SetRenderer(1, &renderer1));
1414     EXPECT_TRUE(channel_->AddSendStream(
1415         cricket::StreamParams::CreateLegacy(1)));
1416     rtc::scoped_ptr<cricket::FakeVideoCapturer> capturer1(
1417         CreateFakeVideoCapturer());
1418     capturer1->SetScreencast(true);
1419     EXPECT_EQ(cricket::CS_RUNNING, capturer1->Start(capture_format));
1420     // Set up additional stream 2.
1421     cricket::FakeVideoRenderer renderer2;
1422     EXPECT_FALSE(channel_->SetRenderer(2, &renderer2));
1423     EXPECT_TRUE(channel_->AddRecvStream(
1424         cricket::StreamParams::CreateLegacy(2)));
1425     EXPECT_TRUE(channel_->SetRenderer(2, &renderer2));
1426     EXPECT_TRUE(channel_->AddSendStream(
1427         cricket::StreamParams::CreateLegacy(2)));
1428     rtc::scoped_ptr<cricket::FakeVideoCapturer> capturer2(
1429         CreateFakeVideoCapturer());
1430     capturer2->SetScreencast(true);
1431     EXPECT_EQ(cricket::CS_RUNNING, capturer2->Start(capture_format));
1432     // State for all the streams.
1433     EXPECT_TRUE(SetOneCodec(DefaultCodec()));
1434     // A limitation in the lmi implementation requires that SetCapturer() is
1435     // called after SetOneCodec().
1436     // TODO(hellner): this seems like an unnecessary constraint, fix it.
1437     EXPECT_TRUE(channel_->SetCapturer(1, capturer1.get()));
1438     EXPECT_TRUE(channel_->SetCapturer(2, capturer2.get()));
1439     EXPECT_TRUE(SetSend(true));
1440     EXPECT_TRUE(channel_->SetRender(true));
1441     // Test capturer associated with engine.
1442     const int kTestWidth = 160;
1443     const int kTestHeight = 120;
1444     EXPECT_TRUE(capturer1->CaptureCustomFrame(
1445         kTestWidth, kTestHeight, cricket::FOURCC_I420));
1446     EXPECT_FRAME_ON_RENDERER_WAIT(
1447         renderer1, 1, kTestWidth, kTestHeight, kTimeout);
1448     // Capture a frame with additional capturer2, frames should be received
1449     EXPECT_TRUE(capturer2->CaptureCustomFrame(
1450         kTestWidth, kTestHeight, cricket::FOURCC_I420));
1451     EXPECT_FRAME_ON_RENDERER_WAIT(
1452         renderer2, 1, kTestWidth, kTestHeight, kTimeout);
1453     // Successfully remove the capturer.
1454     EXPECT_TRUE(channel_->SetCapturer(kSsrc, NULL));
1455     // Fail to re-remove the capturer.
1456     EXPECT_FALSE(channel_->SetCapturer(kSsrc, NULL));
1457     // The capturers must be unregistered here as it runs out of it's scope
1458     // next.
1459     EXPECT_TRUE(channel_->SetCapturer(1, NULL));
1460     EXPECT_TRUE(channel_->SetCapturer(2, NULL));
1461   }
1462
1463   void HighAspectHighHeightCapturer() {
1464     const int kWidth  = 80;
1465     const int kHeight = 10000;
1466     const int kScaledWidth = 20;
1467     const int kScaledHeight = 2500;
1468
1469     cricket::VideoCodec codec(DefaultCodec());
1470     EXPECT_TRUE(SetOneCodec(codec));
1471     EXPECT_TRUE(SetSend(true));
1472
1473     cricket::FakeVideoRenderer renderer;
1474     EXPECT_TRUE(channel_->AddRecvStream(
1475         cricket::StreamParams::CreateLegacy(kSsrc)));
1476     EXPECT_TRUE(channel_->SetRenderer(kSsrc, &renderer));
1477     EXPECT_TRUE(channel_->SetRender(true));
1478     EXPECT_EQ(0, renderer.num_rendered_frames());
1479
1480     EXPECT_TRUE(SendFrame());
1481     EXPECT_GT_FRAME_ON_RENDERER_WAIT(
1482         renderer, 1, codec.width, codec.height, kTimeout);
1483
1484     // Registering an external capturer is currently the same as screen casting
1485     // (update the test when this changes).
1486     rtc::scoped_ptr<cricket::FakeVideoCapturer> capturer(
1487         CreateFakeVideoCapturer());
1488     capturer->SetScreencast(true);
1489     const std::vector<cricket::VideoFormat>* formats =
1490         capturer->GetSupportedFormats();
1491     cricket::VideoFormat capture_format = (*formats)[0];
1492     EXPECT_EQ(cricket::CS_RUNNING, capturer->Start(capture_format));
1493     // Capture frame to not get same frame timestamps as previous capturer.
1494     capturer->CaptureFrame();
1495     EXPECT_TRUE(channel_->SetCapturer(kSsrc, capturer.get()));
1496     EXPECT_TRUE(rtc::Thread::Current()->ProcessMessages(30));
1497     EXPECT_TRUE(capturer->CaptureCustomFrame(kWidth, kHeight,
1498                                              cricket::FOURCC_ARGB));
1499     EXPECT_GT_FRAME_ON_RENDERER_WAIT(
1500         renderer, 2, kScaledWidth, kScaledHeight, kTimeout);
1501     EXPECT_TRUE(channel_->SetCapturer(kSsrc, NULL));
1502   }
1503
1504   // Tests that we can adapt video resolution with 16:10 aspect ratio properly.
1505   void AdaptResolution16x10() {
1506     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
1507     cricket::VideoCodec codec(DefaultCodec());
1508     codec.width = 640;
1509     codec.height = 400;
1510     SendAndReceive(codec);
1511     codec.width /= 2;
1512     codec.height /= 2;
1513     // Adapt the resolution.
1514     EXPECT_TRUE(SetOneCodec(codec));
1515     EXPECT_TRUE(WaitAndSendFrame(30));
1516     EXPECT_FRAME_WAIT(2, codec.width, codec.height, kTimeout);
1517   }
1518   // Tests that we can adapt video resolution with 4:3 aspect ratio properly.
1519   void AdaptResolution4x3() {
1520     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
1521     cricket::VideoCodec codec(DefaultCodec());
1522     codec.width = 640;
1523     codec.height = 400;
1524     SendAndReceive(codec);
1525     codec.width /= 2;
1526     codec.height /= 2;
1527     // Adapt the resolution.
1528     EXPECT_TRUE(SetOneCodec(codec));
1529     EXPECT_TRUE(WaitAndSendFrame(30));
1530     EXPECT_FRAME_WAIT(2, codec.width, codec.height, kTimeout);
1531   }
1532   // Tests that we can drop all frames properly.
1533   void AdaptDropAllFrames() {
1534     // Set the channel codec's resolution to 0, which will require the adapter
1535     // to drop all frames.
1536     cricket::VideoCodec codec(DefaultCodec());
1537     codec.width = codec.height = codec.framerate = 0;
1538     EXPECT_TRUE(SetOneCodec(codec));
1539     EXPECT_TRUE(SetSend(true));
1540     EXPECT_TRUE(channel_->SetRender(true));
1541     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
1542     EXPECT_EQ(0, renderer_.num_rendered_frames());
1543     EXPECT_TRUE(SendFrame());
1544     EXPECT_TRUE(SendFrame());
1545     rtc::Thread::Current()->ProcessMessages(500);
1546     EXPECT_EQ(0, renderer_.num_rendered_frames());
1547   }
1548   // Tests that we can reduce the frame rate on demand properly.
1549   // TODO(fbarchard): This test is flakey on pulse.  Fix and re-enable
1550   void AdaptFramerate() {
1551     cricket::VideoCodec codec(DefaultCodec());
1552     int frame_count = 0;
1553     // The capturer runs at 30 fps. The channel requires 30 fps.
1554     EXPECT_TRUE(SetOneCodec(codec));
1555     EXPECT_TRUE(SetSend(true));
1556     EXPECT_TRUE(channel_->SetRender(true));
1557     EXPECT_EQ(frame_count, renderer_.num_rendered_frames());
1558     EXPECT_TRUE(WaitAndSendFrame(0));  // Should be rendered.
1559     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be rendered.
1560     frame_count += 2;
1561     EXPECT_FRAME_WAIT(frame_count, codec.width, codec.height, kTimeout);
1562     rtc::scoped_ptr<const rtc::Buffer> p(GetRtpPacket(0));
1563     EXPECT_EQ(codec.id, GetPayloadType(p.get()));
1564
1565     // The channel requires 15 fps.
1566     codec.framerate = 15;
1567     EXPECT_TRUE(SetOneCodec(codec));
1568     EXPECT_TRUE(WaitAndSendFrame(0));  // Should be rendered.
1569     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be dropped.
1570     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be rendered.
1571     frame_count += 2;
1572     EXPECT_EQ_WAIT(frame_count, renderer_.num_rendered_frames(), kTimeout);
1573
1574     // The channel requires 10 fps.
1575     codec.framerate = 10;
1576     EXPECT_TRUE(SetOneCodec(codec));
1577     EXPECT_TRUE(WaitAndSendFrame(0));  // Should be rendered.
1578     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be dropped.
1579     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be dropped.
1580     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be rendered.
1581     frame_count += 2;
1582     EXPECT_EQ_WAIT(frame_count, renderer_.num_rendered_frames(), kTimeout);
1583
1584     // The channel requires 8 fps. The adapter adapts to 10 fps, which is the
1585     // closest factor of 30.
1586     codec.framerate = 8;
1587     EXPECT_TRUE(SetOneCodec(codec));
1588     EXPECT_TRUE(WaitAndSendFrame(0));  // Should be rendered.
1589     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be dropped.
1590     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be dropped.
1591     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be rendered.
1592     frame_count += 2;
1593     EXPECT_EQ_WAIT(frame_count, renderer_.num_rendered_frames(), kTimeout);
1594   }
1595   // Tests that adapted frames won't be upscaled to a higher resolution.
1596   void SendsLowerResolutionOnSmallerFrames() {
1597     cricket::VideoCodec codec = DefaultCodec();
1598     codec.width = 320;
1599     codec.height = 240;
1600     EXPECT_TRUE(SetOneCodec(codec));
1601     EXPECT_TRUE(SetSend(true));
1602     EXPECT_TRUE(channel_->SetRender(true));
1603     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
1604     EXPECT_EQ(0, renderer_.num_rendered_frames());
1605     EXPECT_TRUE(SendFrame());
1606     EXPECT_FRAME_WAIT(1, codec.width, codec.height, kTimeout);
1607
1608     // Check that we send smaller frames at the new resolution.
1609     EXPECT_TRUE(rtc::Thread::Current()->ProcessMessages(33));
1610     EXPECT_TRUE(video_capturer_->CaptureCustomFrame(
1611         codec.width / 2, codec.height / 2, cricket::FOURCC_I420));
1612     EXPECT_FRAME_WAIT(2, codec.width / 2, codec.height / 2, kTimeout);
1613   }
1614   // Tests that we can set the send stream format properly.
1615   void SetSendStreamFormat() {
1616     cricket::VideoCodec codec(DefaultCodec());
1617     SendAndReceive(codec);
1618     int frame_count = 1;
1619     EXPECT_FRAME_WAIT(frame_count, codec.width, codec.height, kTimeout);
1620
1621     // Adapt the resolution and frame rate to half.
1622     cricket::VideoFormat format(
1623         codec.width / 2,
1624         codec.height / 2,
1625         cricket::VideoFormat::FpsToInterval(codec.framerate / 2),
1626         cricket::FOURCC_I420);
1627     // The SSRC differs from the send SSRC.
1628     EXPECT_FALSE(channel_->SetSendStreamFormat(kSsrc - 1, format));
1629     EXPECT_TRUE(channel_->SetSendStreamFormat(kSsrc, format));
1630
1631     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be dropped.
1632     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be rendered.
1633     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be dropped.
1634     frame_count += 1;
1635     EXPECT_FRAME_WAIT(frame_count, format.width, format.height, kTimeout);
1636
1637     // Adapt the resolution to 0x0, which should drop all frames.
1638     format.width = 0;
1639     format.height = 0;
1640     EXPECT_TRUE(channel_->SetSendStreamFormat(kSsrc, format));
1641     EXPECT_TRUE(SendFrame());
1642     EXPECT_TRUE(SendFrame());
1643     rtc::Thread::Current()->ProcessMessages(500);
1644     EXPECT_EQ(frame_count, renderer_.num_rendered_frames());
1645   }
1646   // Test that setting send stream format to 0x0 resolution will result in
1647   // frames being dropped.
1648   void SetSendStreamFormat0x0() {
1649     EXPECT_TRUE(SetOneCodec(DefaultCodec()));
1650     EXPECT_TRUE(SetSendStreamFormat(kSsrc, DefaultCodec()));
1651     EXPECT_TRUE(SetSend(true));
1652     EXPECT_TRUE(channel_->SetRender(true));
1653     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
1654     EXPECT_EQ(0, renderer_.num_rendered_frames());
1655     // This frame should be received.
1656     EXPECT_TRUE(SendFrame());
1657     EXPECT_FRAME_WAIT(1, DefaultCodec().width, DefaultCodec().height, kTimeout);
1658     const int64 interval = cricket::VideoFormat::FpsToInterval(
1659         DefaultCodec().framerate);
1660     cricket::VideoFormat format(
1661         0,
1662         0,
1663         interval,
1664         cricket::FOURCC_I420);
1665     EXPECT_TRUE(channel_->SetSendStreamFormat(kSsrc, format));
1666     // This frame should not be received.
1667     EXPECT_TRUE(WaitAndSendFrame(
1668         static_cast<int>(interval/rtc::kNumNanosecsPerMillisec)));
1669     rtc::Thread::Current()->ProcessMessages(500);
1670     EXPECT_EQ(1, renderer_.num_rendered_frames());
1671   }
1672
1673   // Tests that we can mute and unmute the channel properly.
1674   void MuteStream() {
1675     EXPECT_TRUE(SetDefaultCodec());
1676     cricket::FakeVideoCapturer video_capturer;
1677     video_capturer.Start(
1678         cricket::VideoFormat(
1679             640, 480,
1680             cricket::VideoFormat::FpsToInterval(30),
1681             cricket::FOURCC_I420));
1682     EXPECT_TRUE(channel_->SetCapturer(kSsrc, &video_capturer));
1683     EXPECT_TRUE(SetSend(true));
1684     EXPECT_TRUE(channel_->SetRender(true));
1685     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
1686     EXPECT_EQ(0, renderer_.num_rendered_frames());
1687
1688     // Mute the channel and expect black output frame.
1689     int frame_count = 0;
1690     EXPECT_TRUE(channel_->MuteStream(kSsrc, true));
1691     EXPECT_TRUE(video_capturer.CaptureFrame());
1692     ++frame_count;
1693     EXPECT_EQ_WAIT(frame_count, renderer_.num_rendered_frames(), kTimeout);
1694     EXPECT_TRUE(renderer_.black_frame());
1695
1696     // Unmute the channel and expect non-black output frame.
1697     EXPECT_TRUE(channel_->MuteStream(kSsrc, false));
1698     EXPECT_TRUE(rtc::Thread::Current()->ProcessMessages(30));
1699     EXPECT_TRUE(video_capturer.CaptureFrame());
1700     ++frame_count;
1701     EXPECT_EQ_WAIT(frame_count, renderer_.num_rendered_frames(), kTimeout);
1702     EXPECT_FALSE(renderer_.black_frame());
1703
1704     // Test that we can also Mute using the correct send stream SSRC.
1705     EXPECT_TRUE(channel_->MuteStream(kSsrc, true));
1706     EXPECT_TRUE(rtc::Thread::Current()->ProcessMessages(30));
1707     EXPECT_TRUE(video_capturer.CaptureFrame());
1708     ++frame_count;
1709     EXPECT_EQ_WAIT(frame_count, renderer_.num_rendered_frames(), kTimeout);
1710     EXPECT_TRUE(renderer_.black_frame());
1711
1712     EXPECT_TRUE(channel_->MuteStream(kSsrc, false));
1713     EXPECT_TRUE(rtc::Thread::Current()->ProcessMessages(30));
1714     EXPECT_TRUE(video_capturer.CaptureFrame());
1715     ++frame_count;
1716     EXPECT_EQ_WAIT(frame_count, renderer_.num_rendered_frames(), kTimeout);
1717     EXPECT_FALSE(renderer_.black_frame());
1718
1719     // Test that muting an existing stream succeeds even if it's muted.
1720     EXPECT_TRUE(channel_->MuteStream(kSsrc, true));
1721     EXPECT_TRUE(channel_->MuteStream(kSsrc, true));
1722
1723     // Test that unmuting an existing stream succeeds even if it's not muted.
1724     EXPECT_TRUE(channel_->MuteStream(kSsrc, false));
1725     EXPECT_TRUE(channel_->MuteStream(kSsrc, false));
1726
1727     // Test that muting an invalid stream fails.
1728     EXPECT_FALSE(channel_->MuteStream(kSsrc+1, true));
1729     EXPECT_TRUE(channel_->SetCapturer(kSsrc, NULL));
1730   }
1731
1732   // Test that multiple send streams can be created and deleted properly.
1733   void MultipleSendStreams() {
1734     // Remove stream added in Setup. I.e. remove stream corresponding to default
1735     // channel.
1736     EXPECT_TRUE(channel_->RemoveSendStream(kSsrc));
1737     const unsigned int kSsrcsSize = sizeof(kSsrcs4)/sizeof(kSsrcs4[0]);
1738     for (unsigned int i = 0; i < kSsrcsSize; ++i) {
1739       EXPECT_TRUE(channel_->AddSendStream(
1740           cricket::StreamParams::CreateLegacy(kSsrcs4[i])));
1741     }
1742     // Delete one of the non default channel streams, let the destructor delete
1743     // the remaining ones.
1744     EXPECT_TRUE(channel_->RemoveSendStream(kSsrcs4[kSsrcsSize - 1]));
1745     // Stream should already be deleted.
1746     EXPECT_FALSE(channel_->RemoveSendStream(kSsrcs4[kSsrcsSize - 1]));
1747   }
1748
1749   // Two streams one channel tests.
1750
1751   // Tests that we can send and receive frames.
1752   void TwoStreamsSendAndReceive(const cricket::VideoCodec& codec) {
1753     SetUpSecondStream();
1754     // Test sending and receiving on first stream.
1755     SendAndReceive(codec);
1756     // Test sending and receiving on second stream.
1757     EXPECT_EQ_WAIT(1, renderer2_.num_rendered_frames(), kTimeout);
1758     EXPECT_EQ(2, NumRtpPackets());
1759     EXPECT_EQ(1, renderer2_.num_rendered_frames());
1760   }
1761
1762   // Set up 2 streams where the first stream uses the default channel.
1763   // Then disconnect the first stream and verify default channel becomes
1764   // available.
1765   // Then add a new stream with |new_ssrc|. The new stream should re-use the
1766   // default channel.
1767   void TwoStreamsReUseFirstStream(const cricket::VideoCodec& codec) {
1768     SetUpSecondStream();
1769     // Default channel used by the first stream.
1770     EXPECT_EQ(kSsrc, channel_->GetDefaultSendChannelSsrc());
1771     EXPECT_TRUE(channel_->RemoveRecvStream(kSsrc));
1772     EXPECT_FALSE(channel_->RemoveRecvStream(kSsrc));
1773     EXPECT_TRUE(channel_->RemoveSendStream(kSsrc));
1774     EXPECT_FALSE(channel_->RemoveSendStream(kSsrc));
1775     // Default channel is no longer used by a stream.
1776     EXPECT_EQ(0u, channel_->GetDefaultSendChannelSsrc());
1777     uint32 new_ssrc = kSsrc + 100;
1778     EXPECT_TRUE(channel_->AddSendStream(
1779         cricket::StreamParams::CreateLegacy(new_ssrc)));
1780     // Re-use default channel.
1781     EXPECT_EQ(new_ssrc, channel_->GetDefaultSendChannelSsrc());
1782     EXPECT_FALSE(channel_->AddSendStream(
1783         cricket::StreamParams::CreateLegacy(new_ssrc)));
1784     EXPECT_TRUE(channel_->AddRecvStream(
1785         cricket::StreamParams::CreateLegacy(new_ssrc)));
1786     EXPECT_TRUE(channel_->SetRenderer(new_ssrc, &renderer_));
1787     EXPECT_FALSE(channel_->AddRecvStream(
1788         cricket::StreamParams::CreateLegacy(new_ssrc)));
1789
1790     EXPECT_TRUE(channel_->SetCapturer(new_ssrc, video_capturer_.get()));
1791
1792     SendAndReceive(codec);
1793     EXPECT_TRUE(channel_->RemoveSendStream(new_ssrc));
1794     EXPECT_EQ(0u, channel_->GetDefaultSendChannelSsrc());
1795   }
1796
1797   // Tests that we can send and receive frames with early receive.
1798   void TwoStreamsSendAndUnsignalledRecv(const cricket::VideoCodec& codec) {
1799     cricket::VideoOptions vmo;
1800     vmo.conference_mode.Set(true);
1801     vmo.unsignalled_recv_stream_limit.Set(1);
1802     EXPECT_TRUE(channel_->SetOptions(vmo));
1803     SetUpSecondStreamWithNoRecv();
1804     // Test sending and receiving on first stream.
1805     EXPECT_TRUE(channel_->SetRender(true));
1806     Send(codec);
1807     EXPECT_EQ_WAIT(2, NumRtpPackets(), kTimeout);
1808     EXPECT_EQ_WAIT(1, renderer_.num_rendered_frames(), kTimeout);
1809     // The first send is not expected to yield frames, because the ssrc
1810     // is not signalled yet. With unsignalled recv enabled, we will drop frames
1811     // instead of packets.
1812     EXPECT_EQ(0, renderer2_.num_rendered_frames());
1813     // Give a chance for the decoder to process before adding the receiver.
1814     rtc::Thread::Current()->ProcessMessages(100);
1815     // Test sending and receiving on second stream.
1816     EXPECT_TRUE(channel_->AddRecvStream(
1817         cricket::StreamParams::CreateLegacy(kSsrc + 2)));
1818     EXPECT_TRUE(channel_->SetRenderer(kSsrc + 2, &renderer2_));
1819     SendFrame();
1820     EXPECT_EQ_WAIT(2, renderer_.num_rendered_frames(), kTimeout);
1821     EXPECT_EQ(4, NumRtpPackets());
1822     // The second send is expected to yield frame as the ssrc is signalled now.
1823     // Decode should succeed here, though we received the key frame earlier.
1824     // Without early recv, we would have dropped it and decoding would have
1825     // failed.
1826     EXPECT_EQ_WAIT(1, renderer2_.num_rendered_frames(), kTimeout);
1827   }
1828
1829   // Tests that we cannot receive key frames with unsignalled recv disabled.
1830   void TwoStreamsSendAndFailUnsignalledRecv(const cricket::VideoCodec& codec) {
1831     cricket::VideoOptions vmo;
1832     vmo.conference_mode.Set(true);
1833     vmo.unsignalled_recv_stream_limit.Set(0);
1834     EXPECT_TRUE(channel_->SetOptions(vmo));
1835     SetUpSecondStreamWithNoRecv();
1836     // Test sending and receiving on first stream.
1837     EXPECT_TRUE(channel_->SetRender(true));
1838     Send(codec);
1839     EXPECT_EQ_WAIT(2, NumRtpPackets(), kTimeout);
1840     rtc::Thread::Current()->ProcessMessages(100);
1841     EXPECT_EQ_WAIT(1, renderer_.num_rendered_frames(), kTimeout);
1842     EXPECT_EQ_WAIT(0, renderer2_.num_rendered_frames(), kTimeout);
1843     // Give a chance for the decoder to process before adding the receiver.
1844     rtc::Thread::Current()->ProcessMessages(10);
1845     // Test sending and receiving on second stream.
1846     EXPECT_TRUE(channel_->AddRecvStream(
1847         cricket::StreamParams::CreateLegacy(kSsrc + 2)));
1848     EXPECT_TRUE(channel_->SetRenderer(kSsrc + 2, &renderer2_));
1849     SendFrame();
1850     EXPECT_TRUE_WAIT(renderer_.num_rendered_frames() >= 1, kTimeout);
1851     EXPECT_EQ_WAIT(4, NumRtpPackets(), kTimeout);
1852     // We dont expect any frames here, because the key frame would have been
1853     // lost in the earlier packet. This is the case we want to solve with early
1854     // receive.
1855     EXPECT_EQ(0, renderer2_.num_rendered_frames());
1856   }
1857
1858   // Tests that we drop key frames when conference mode is disabled and we
1859   // receive rtp packets on unsignalled streams.
1860   void TwoStreamsSendAndFailUnsignalledRecvInOneToOne(
1861       const cricket::VideoCodec& codec) {
1862     cricket::VideoOptions vmo;
1863     vmo.conference_mode.Set(false);
1864     vmo.unsignalled_recv_stream_limit.Set(1);
1865     EXPECT_TRUE(channel_->SetOptions(vmo));
1866     SetUpSecondStreamWithNoRecv();
1867     // Test sending and receiving on first stream.
1868     EXPECT_TRUE(channel_->SetRender(true));
1869     Send(codec);
1870     EXPECT_EQ_WAIT(2, NumRtpPackets(), kTimeout);
1871     // In one-to-one mode, we deliver frames to the default channel if there
1872     // is no registered recv channel for the ssrc.
1873     EXPECT_TRUE_WAIT(renderer_.num_rendered_frames() >= 1, kTimeout);
1874     // Give a chance for the decoder to process before adding the receiver.
1875     rtc::Thread::Current()->ProcessMessages(100);
1876     // Test sending and receiving on second stream.
1877     EXPECT_TRUE(channel_->AddRecvStream(
1878         cricket::StreamParams::CreateLegacy(kSsrc + 2)));
1879     EXPECT_TRUE(channel_->SetRenderer(kSsrc + 2, &renderer2_));
1880     SendFrame();
1881     EXPECT_TRUE_WAIT(renderer_.num_rendered_frames() >= 1, kTimeout);
1882     EXPECT_EQ_WAIT(4, NumRtpPackets(), kTimeout);
1883     // We dont expect any frames here, because the key frame would have been
1884     // delivered to default channel.
1885     EXPECT_EQ(0, renderer2_.num_rendered_frames());
1886   }
1887
1888   // Tests that we drop key frames when conference mode is enabled and we
1889   // receive rtp packets on unsignalled streams. Removal of a unsignalled recv
1890   // stream is successful.
1891   void TwoStreamsAddAndRemoveUnsignalledRecv(
1892       const cricket::VideoCodec& codec) {
1893     cricket::VideoOptions vmo;
1894     vmo.conference_mode.Set(true);
1895     vmo.unsignalled_recv_stream_limit.Set(1);
1896     EXPECT_TRUE(channel_->SetOptions(vmo));
1897     SetUpSecondStreamWithNoRecv();
1898     // Sending and receiving on first stream.
1899     EXPECT_TRUE(channel_->SetRender(true));
1900     Send(codec);
1901     EXPECT_EQ_WAIT(2, NumRtpPackets(), kTimeout);
1902     EXPECT_EQ_WAIT(1, renderer_.num_rendered_frames(), kTimeout);
1903     // The first send is not expected to yield frames, because the ssrc
1904     // is no signalled yet. With unsignalled recv enabled, we will drop frames
1905     // instead of packets.
1906     EXPECT_EQ(0, renderer2_.num_rendered_frames());
1907     // Give a chance for the decoder to process before adding the receiver.
1908     rtc::Thread::Current()->ProcessMessages(100);
1909     // Ensure that we can remove the unsignalled recv stream that was created
1910     // when the first video packet with unsignalled recv ssrc is received.
1911     EXPECT_TRUE(channel_->RemoveRecvStream(kSsrc + 2));
1912   }
1913
1914   VideoEngineOverride<E> engine_;
1915   rtc::scoped_ptr<cricket::FakeVideoCapturer> video_capturer_;
1916   rtc::scoped_ptr<cricket::FakeVideoCapturer> video_capturer_2_;
1917   rtc::scoped_ptr<C> channel_;
1918   cricket::FakeNetworkInterface network_interface_;
1919   cricket::FakeVideoRenderer renderer_;
1920   cricket::VideoMediaChannel::Error media_error_;
1921
1922   // Used by test cases where 2 streams are run on the same channel.
1923   cricket::FakeVideoRenderer renderer2_;
1924 };
1925
1926 #endif  // TALK_MEDIA_BASE_VIDEOENGINE_UNITTEST_H_  NOLINT