Upstream version 9.38.198.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(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     // TODO(sriniv): Remove or re-enable this.
787     // As part of b/8030474, send-buffer is size now controlled through
788     // portallocator flags. Its not set by channels.
789     // EXPECT_EQ(64 * 1024, network_interface_.sendbuf_size());
790     EXPECT_EQ(64 * 1024, network_interface_.recvbuf_size());
791   }
792   // Tests that we can send frames and the right payload type is used.
793   void Send(const cricket::VideoCodec& codec) {
794     EXPECT_TRUE(SetOneCodec(codec));
795     EXPECT_TRUE(SetSend(true));
796     EXPECT_TRUE(SendFrame());
797     EXPECT_TRUE_WAIT(NumRtpPackets() > 0, kTimeout);
798     rtc::scoped_ptr<const rtc::Buffer> p(GetRtpPacket(0));
799     EXPECT_EQ(codec.id, GetPayloadType(p.get()));
800   }
801   // Tests that we can send and receive frames.
802   void SendAndReceive(const cricket::VideoCodec& codec) {
803     EXPECT_TRUE(SetOneCodec(codec));
804     EXPECT_TRUE(SetSend(true));
805     EXPECT_TRUE(channel_->SetRender(true));
806     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
807     EXPECT_EQ(0, renderer_.num_rendered_frames());
808     EXPECT_TRUE(SendFrame());
809     EXPECT_FRAME_WAIT(1, codec.width, codec.height, kTimeout);
810     rtc::scoped_ptr<const rtc::Buffer> p(GetRtpPacket(0));
811     EXPECT_EQ(codec.id, GetPayloadType(p.get()));
812   }
813   // Tests that we only get a VideoRenderer::SetSize() callback when needed.
814   void SendManyResizeOnce() {
815     cricket::VideoCodec codec(DefaultCodec());
816     EXPECT_TRUE(SetOneCodec(codec));
817     EXPECT_TRUE(SetSend(true));
818     EXPECT_TRUE(channel_->SetRender(true));
819     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
820     EXPECT_EQ(0, renderer_.num_rendered_frames());
821     EXPECT_TRUE(WaitAndSendFrame(30));
822     EXPECT_FRAME_WAIT(1, codec.width, codec.height, kTimeout);
823     EXPECT_TRUE(WaitAndSendFrame(30));
824     EXPECT_FRAME_WAIT(2, codec.width, codec.height, kTimeout);
825     rtc::scoped_ptr<const rtc::Buffer> p(GetRtpPacket(0));
826     EXPECT_EQ(codec.id, GetPayloadType(p.get()));
827     EXPECT_EQ(1, renderer_.num_set_sizes());
828
829     codec.width /= 2;
830     codec.height /= 2;
831     EXPECT_TRUE(SetOneCodec(codec));
832     EXPECT_TRUE(WaitAndSendFrame(30));
833     EXPECT_FRAME_WAIT(3, codec.width, codec.height, kTimeout);
834     EXPECT_EQ(2, renderer_.num_set_sizes());
835   }
836   void SendReceiveManyAndGetStats(const cricket::VideoCodec& codec,
837                                   int duration_sec, int fps) {
838     EXPECT_TRUE(SetOneCodec(codec));
839     EXPECT_TRUE(SetSend(true));
840     EXPECT_TRUE(channel_->SetRender(true));
841     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
842     EXPECT_EQ(0, renderer_.num_rendered_frames());
843     for (int i = 0; i < duration_sec; ++i) {
844       for (int frame = 1; frame <= fps; ++frame) {
845         EXPECT_TRUE(WaitAndSendFrame(1000 / fps));
846         EXPECT_FRAME_WAIT(frame + i * fps, codec.width, codec.height, kTimeout);
847       }
848       cricket::VideoMediaInfo info;
849       EXPECT_TRUE(channel_->GetStats(cricket::StatsOptions(), &info));
850       // For webrtc, |framerate_sent| and |framerate_rcvd| depend on periodic
851       // callbacks (1 sec).
852       // Received |fraction_lost| and |packets_lost| are from sent RTCP packet.
853       // One sent packet needed (sent about once per second).
854       // |framerate_input|, |framerate_decoded| and |framerate_output| are using
855       // RateTracker. RateTracker needs to be called twice (with >1 second in
856       // b/w calls) before a framerate is calculated.
857       // Therefore insert frames (and call GetStats each sec) for a few seconds
858       // before testing stats.
859     }
860     rtc::scoped_ptr<const rtc::Buffer> p(GetRtpPacket(0));
861     EXPECT_EQ(codec.id, GetPayloadType(p.get()));
862   }
863
864   // Test that stats work properly for a 1-1 call.
865   void GetStats() {
866     const int kDurationSec = 3;
867     const int kFps = 10;
868     SendReceiveManyAndGetStats(DefaultCodec(), kDurationSec, kFps);
869
870     cricket::VideoMediaInfo info;
871     EXPECT_TRUE(channel_->GetStats(cricket::StatsOptions(), &info));
872
873     ASSERT_EQ(1U, info.senders.size());
874     // TODO(whyuan): bytes_sent and bytes_rcvd are different. Are both payload?
875     // For webrtc, bytes_sent does not include the RTP header length.
876     EXPECT_GT(info.senders[0].bytes_sent, 0);
877     EXPECT_EQ(NumRtpPackets(), info.senders[0].packets_sent);
878     EXPECT_EQ(0.0, info.senders[0].fraction_lost);
879     EXPECT_EQ(0, info.senders[0].firs_rcvd);
880     EXPECT_EQ(0, info.senders[0].plis_rcvd);
881     EXPECT_EQ(0, info.senders[0].nacks_rcvd);
882     EXPECT_EQ(DefaultCodec().width, info.senders[0].send_frame_width);
883     EXPECT_EQ(DefaultCodec().height, info.senders[0].send_frame_height);
884     EXPECT_GT(info.senders[0].framerate_input, 0);
885     EXPECT_GT(info.senders[0].framerate_sent, 0);
886
887     ASSERT_EQ(1U, info.receivers.size());
888     EXPECT_EQ(1U, info.senders[0].ssrcs().size());
889     EXPECT_EQ(1U, info.receivers[0].ssrcs().size());
890     EXPECT_EQ(info.senders[0].ssrcs()[0], info.receivers[0].ssrcs()[0]);
891     EXPECT_EQ(NumRtpBytes(), info.receivers[0].bytes_rcvd);
892     EXPECT_EQ(NumRtpPackets(), info.receivers[0].packets_rcvd);
893     EXPECT_EQ(0.0, info.receivers[0].fraction_lost);
894     EXPECT_EQ(0, info.receivers[0].packets_lost);
895     // TODO(asapersson): Not set for webrtc. Handle missing stats.
896     // EXPECT_EQ(0, info.receivers[0].packets_concealed);
897     EXPECT_EQ(0, info.receivers[0].firs_sent);
898     EXPECT_EQ(0, info.receivers[0].plis_sent);
899     EXPECT_EQ(0, info.receivers[0].nacks_sent);
900     EXPECT_EQ(DefaultCodec().width, info.receivers[0].frame_width);
901     EXPECT_EQ(DefaultCodec().height, info.receivers[0].frame_height);
902     EXPECT_GT(info.receivers[0].framerate_rcvd, 0);
903     EXPECT_GT(info.receivers[0].framerate_decoded, 0);
904     EXPECT_GT(info.receivers[0].framerate_output, 0);
905   }
906   // Test that stats work properly for a conf call with multiple recv streams.
907   void GetStatsMultipleRecvStreams() {
908     cricket::FakeVideoRenderer renderer1, renderer2;
909     EXPECT_TRUE(SetOneCodec(DefaultCodec()));
910     cricket::VideoOptions vmo;
911     vmo.conference_mode.Set(true);
912     EXPECT_TRUE(channel_->SetOptions(vmo));
913     EXPECT_TRUE(SetSend(true));
914     EXPECT_TRUE(channel_->AddRecvStream(
915         cricket::StreamParams::CreateLegacy(1)));
916     EXPECT_TRUE(channel_->AddRecvStream(
917         cricket::StreamParams::CreateLegacy(2)));
918     EXPECT_TRUE(channel_->SetRenderer(1, &renderer1));
919     EXPECT_TRUE(channel_->SetRenderer(2, &renderer2));
920     EXPECT_TRUE(channel_->SetRender(true));
921     EXPECT_EQ(0, renderer1.num_rendered_frames());
922     EXPECT_EQ(0, renderer2.num_rendered_frames());
923     std::vector<uint32> ssrcs;
924     ssrcs.push_back(1);
925     ssrcs.push_back(2);
926     network_interface_.SetConferenceMode(true, ssrcs);
927     EXPECT_TRUE(SendFrame());
928     EXPECT_FRAME_ON_RENDERER_WAIT(
929         renderer1, 1, DefaultCodec().width, DefaultCodec().height, kTimeout);
930     EXPECT_FRAME_ON_RENDERER_WAIT(
931         renderer2, 1, DefaultCodec().width, DefaultCodec().height, kTimeout);
932     cricket::VideoMediaInfo info;
933     EXPECT_TRUE(channel_->GetStats(cricket::StatsOptions(), &info));
934
935     ASSERT_EQ(1U, info.senders.size());
936     // TODO(whyuan): bytes_sent and bytes_rcvd are different. Are both payload?
937     // For webrtc, bytes_sent does not include the RTP header length.
938     EXPECT_GT(info.senders[0].bytes_sent, 0);
939     EXPECT_EQ(NumRtpPackets(), info.senders[0].packets_sent);
940     EXPECT_EQ(DefaultCodec().width, info.senders[0].send_frame_width);
941     EXPECT_EQ(DefaultCodec().height, info.senders[0].send_frame_height);
942
943     ASSERT_EQ(2U, info.receivers.size());
944     for (size_t i = 0; i < info.receivers.size(); ++i) {
945       EXPECT_EQ(1U, info.receivers[i].ssrcs().size());
946       EXPECT_EQ(i + 1, info.receivers[i].ssrcs()[0]);
947       EXPECT_EQ(NumRtpBytes(), info.receivers[i].bytes_rcvd);
948       EXPECT_EQ(NumRtpPackets(), info.receivers[i].packets_rcvd);
949       EXPECT_EQ(DefaultCodec().width, info.receivers[i].frame_width);
950       EXPECT_EQ(DefaultCodec().height, info.receivers[i].frame_height);
951     }
952   }
953   // Test that stats work properly for a conf call with multiple send streams.
954   void GetStatsMultipleSendStreams() {
955     // Normal setup; note that we set the SSRC explicitly to ensure that
956     // it will come first in the senders map.
957     EXPECT_TRUE(SetOneCodec(DefaultCodec()));
958     cricket::VideoOptions vmo;
959     vmo.conference_mode.Set(true);
960     EXPECT_TRUE(channel_->SetOptions(vmo));
961     EXPECT_TRUE(channel_->AddRecvStream(
962         cricket::StreamParams::CreateLegacy(kSsrc)));
963     EXPECT_TRUE(channel_->SetRenderer(kSsrc, &renderer_));
964     channel_->UpdateAspectRatio(640, 400);
965     EXPECT_TRUE(SetSend(true));
966     EXPECT_TRUE(channel_->SetRender(true));
967     EXPECT_TRUE(SendFrame());
968     EXPECT_TRUE_WAIT(NumRtpPackets() > 0, kTimeout);
969     EXPECT_FRAME_WAIT(1, DefaultCodec().width, DefaultCodec().height, kTimeout);
970
971     // Add an additional capturer, and hook up a renderer to receive it.
972     cricket::FakeVideoRenderer renderer1;
973     rtc::scoped_ptr<cricket::FakeVideoCapturer> capturer(
974         CreateFakeVideoCapturer());
975     capturer->SetScreencast(true);
976     const int kTestWidth = 160;
977     const int kTestHeight = 120;
978     cricket::VideoFormat format(kTestWidth, kTestHeight,
979                                 cricket::VideoFormat::FpsToInterval(5),
980                                 cricket::FOURCC_I420);
981     EXPECT_EQ(cricket::CS_RUNNING, capturer->Start(format));
982     EXPECT_TRUE(channel_->AddSendStream(
983         cricket::StreamParams::CreateLegacy(5678)));
984     EXPECT_TRUE(channel_->SetCapturer(5678, capturer.get()));
985     EXPECT_TRUE(channel_->AddRecvStream(
986         cricket::StreamParams::CreateLegacy(5678)));
987     EXPECT_TRUE(channel_->SetRenderer(5678, &renderer1));
988     EXPECT_TRUE(capturer->CaptureCustomFrame(
989         kTestWidth, kTestHeight, cricket::FOURCC_I420));
990     EXPECT_FRAME_ON_RENDERER_WAIT(
991         renderer1, 1, kTestWidth, kTestHeight, kTimeout);
992
993     // Get stats, and make sure they are correct for two senders.
994     cricket::VideoMediaInfo info;
995     EXPECT_TRUE(channel_->GetStats(cricket::StatsOptions(), &info));
996     ASSERT_EQ(2U, info.senders.size());
997     EXPECT_EQ(NumRtpPackets(),
998         info.senders[0].packets_sent + info.senders[1].packets_sent);
999     EXPECT_EQ(1U, info.senders[0].ssrcs().size());
1000     EXPECT_EQ(1234U, info.senders[0].ssrcs()[0]);
1001     EXPECT_EQ(DefaultCodec().width, info.senders[0].send_frame_width);
1002     EXPECT_EQ(DefaultCodec().height, info.senders[0].send_frame_height);
1003     EXPECT_EQ(1U, info.senders[1].ssrcs().size());
1004     EXPECT_EQ(5678U, info.senders[1].ssrcs()[0]);
1005     EXPECT_EQ(kTestWidth, info.senders[1].send_frame_width);
1006     EXPECT_EQ(kTestHeight, info.senders[1].send_frame_height);
1007     // The capturer must be unregistered here as it runs out of it's scope next.
1008     EXPECT_TRUE(channel_->SetCapturer(5678, NULL));
1009   }
1010
1011   // Test that we can set the bandwidth.
1012   void SetSendBandwidth() {
1013     EXPECT_TRUE(channel_->SetStartSendBandwidth(64 * 1024));
1014     EXPECT_TRUE(channel_->SetMaxSendBandwidth(-1));  // <= 0 means unlimited.
1015     EXPECT_TRUE(channel_->SetMaxSendBandwidth(128 * 1024));
1016   }
1017   // Test that we can set the SSRC for the default send source.
1018   void SetSendSsrc() {
1019     EXPECT_TRUE(SetDefaultCodec());
1020     EXPECT_TRUE(SetSendStreamFormat(kSsrc, DefaultCodec()));
1021     EXPECT_TRUE(SetSend(true));
1022     EXPECT_TRUE(SendFrame());
1023     EXPECT_TRUE_WAIT(NumRtpPackets() > 0, kTimeout);
1024     uint32 ssrc = 0;
1025     rtc::scoped_ptr<const rtc::Buffer> p(GetRtpPacket(0));
1026     ParseRtpPacket(p.get(), NULL, NULL, NULL, NULL, &ssrc, NULL);
1027     EXPECT_EQ(kSsrc, ssrc);
1028     EXPECT_EQ(NumRtpPackets(), NumRtpPackets(ssrc));
1029     EXPECT_EQ(NumRtpBytes(), NumRtpBytes(ssrc));
1030     EXPECT_EQ(1, NumSentSsrcs());
1031     EXPECT_EQ(0, NumRtpPackets(kSsrc - 1));
1032     EXPECT_EQ(0, NumRtpBytes(kSsrc - 1));
1033   }
1034   // Test that we can set the SSRC even after codecs are set.
1035   void SetSendSsrcAfterSetCodecs() {
1036     // Remove stream added in Setup.
1037     EXPECT_TRUE(channel_->RemoveSendStream(kSsrc));
1038     EXPECT_TRUE(SetDefaultCodec());
1039     EXPECT_TRUE(channel_->AddSendStream(
1040         cricket::StreamParams::CreateLegacy(999)));
1041     EXPECT_TRUE(channel_->SetCapturer(999u, video_capturer_.get()));
1042     EXPECT_TRUE(SetSendStreamFormat(999u, DefaultCodec()));
1043     EXPECT_TRUE(SetSend(true));
1044     EXPECT_TRUE(WaitAndSendFrame(0));
1045     EXPECT_TRUE_WAIT(NumRtpPackets() > 0, kTimeout);
1046     uint32 ssrc = 0;
1047     rtc::scoped_ptr<const rtc::Buffer> p(GetRtpPacket(0));
1048     ParseRtpPacket(p.get(), NULL, NULL, NULL, NULL, &ssrc, NULL);
1049     EXPECT_EQ(999u, ssrc);
1050     EXPECT_EQ(NumRtpPackets(), NumRtpPackets(ssrc));
1051     EXPECT_EQ(NumRtpBytes(), NumRtpBytes(ssrc));
1052     EXPECT_EQ(1, NumSentSsrcs());
1053     EXPECT_EQ(0, NumRtpPackets(kSsrc));
1054     EXPECT_EQ(0, NumRtpBytes(kSsrc));
1055   }
1056   // Test that we can set the default video renderer before and after
1057   // media is received.
1058   void SetRenderer() {
1059     uint8 data1[] = {
1060         0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1061     };
1062
1063     rtc::Buffer packet1(data1, sizeof(data1));
1064     rtc::SetBE32(packet1.data() + 8, kSsrc);
1065     channel_->SetRenderer(kDefaultReceiveSsrc, NULL);
1066     EXPECT_TRUE(SetDefaultCodec());
1067     EXPECT_TRUE(SetSend(true));
1068     EXPECT_TRUE(channel_->SetRender(true));
1069     EXPECT_EQ(0, renderer_.num_rendered_frames());
1070     channel_->OnPacketReceived(&packet1, rtc::PacketTime());
1071     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
1072     EXPECT_TRUE(SendFrame());
1073     EXPECT_FRAME_WAIT(1, DefaultCodec().width, DefaultCodec().height, kTimeout);
1074   }
1075
1076   // Tests empty StreamParams is rejected.
1077   void RejectEmptyStreamParams() {
1078     // Remove the send stream that was added during Setup.
1079     EXPECT_TRUE(channel_->RemoveSendStream(kSsrc));
1080
1081     cricket::StreamParams empty;
1082     EXPECT_FALSE(channel_->AddSendStream(empty));
1083     EXPECT_TRUE(channel_->AddSendStream(
1084         cricket::StreamParams::CreateLegacy(789u)));
1085   }
1086
1087   // Tests setting up and configuring a send stream.
1088   void AddRemoveSendStreams() {
1089     EXPECT_TRUE(SetOneCodec(DefaultCodec()));
1090     EXPECT_TRUE(SetSend(true));
1091     EXPECT_TRUE(channel_->SetRender(true));
1092     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
1093     EXPECT_TRUE(SendFrame());
1094     EXPECT_FRAME_WAIT(1, DefaultCodec().width, DefaultCodec().height, kTimeout);
1095     EXPECT_GE(2, NumRtpPackets());
1096     uint32 ssrc = 0;
1097     size_t last_packet = NumRtpPackets() - 1;
1098     rtc::scoped_ptr<const rtc::Buffer>
1099         p(GetRtpPacket(static_cast<int>(last_packet)));
1100     ParseRtpPacket(p.get(), NULL, NULL, NULL, NULL, &ssrc, NULL);
1101     EXPECT_EQ(kSsrc, ssrc);
1102
1103     // Remove the send stream that was added during Setup.
1104     EXPECT_TRUE(channel_->RemoveSendStream(kSsrc));
1105     int rtp_packets = NumRtpPackets();
1106
1107     EXPECT_TRUE(channel_->AddSendStream(
1108         cricket::StreamParams::CreateLegacy(789u)));
1109     EXPECT_TRUE(channel_->SetCapturer(789u, video_capturer_.get()));
1110     EXPECT_EQ(rtp_packets, NumRtpPackets());
1111     // Wait 30ms to guarantee the engine does not drop the frame.
1112     EXPECT_TRUE(WaitAndSendFrame(30));
1113     EXPECT_TRUE_WAIT(NumRtpPackets() > rtp_packets, kTimeout);
1114
1115     last_packet = NumRtpPackets() - 1;
1116     p.reset(GetRtpPacket(static_cast<int>(last_packet)));
1117     ParseRtpPacket(p.get(), NULL, NULL, NULL, NULL, &ssrc, NULL);
1118     EXPECT_EQ(789u, ssrc);
1119   }
1120
1121   // Tests adding streams already exists returns false.
1122   void AddRecvStreamsAlreadyExist() {
1123     cricket::VideoOptions vmo;
1124     vmo.conference_mode.Set(true);
1125     EXPECT_TRUE(channel_->SetOptions(vmo));
1126
1127     EXPECT_FALSE(channel_->AddRecvStream(
1128         cricket::StreamParams::CreateLegacy(0)));
1129
1130     EXPECT_TRUE(channel_->AddRecvStream(
1131         cricket::StreamParams::CreateLegacy(1)));
1132     EXPECT_FALSE(channel_->AddRecvStream(
1133         cricket::StreamParams::CreateLegacy(1)));
1134
1135     EXPECT_TRUE(channel_->RemoveRecvStream(1));
1136     EXPECT_FALSE(channel_->AddRecvStream(
1137         cricket::StreamParams::CreateLegacy(0)));
1138     EXPECT_TRUE(channel_->AddRecvStream(
1139         cricket::StreamParams::CreateLegacy(1)));
1140   }
1141
1142   // Tests setting up and configuring multiple incoming streams.
1143   void AddRemoveRecvStreams() {
1144     cricket::FakeVideoRenderer renderer1, renderer2;
1145     cricket::VideoOptions vmo;
1146     vmo.conference_mode.Set(true);
1147     EXPECT_TRUE(channel_->SetOptions(vmo));
1148     // Ensure we can't set the renderer on a non-existent stream.
1149     EXPECT_FALSE(channel_->SetRenderer(1, &renderer1));
1150     EXPECT_FALSE(channel_->SetRenderer(2, &renderer2));
1151     cricket::VideoRenderer* renderer;
1152     EXPECT_FALSE(channel_->GetRenderer(1, &renderer));
1153     EXPECT_FALSE(channel_->GetRenderer(2, &renderer));
1154
1155     // Ensure we can add streams.
1156     EXPECT_TRUE(channel_->AddRecvStream(
1157         cricket::StreamParams::CreateLegacy(1)));
1158     EXPECT_TRUE(channel_->AddRecvStream(
1159         cricket::StreamParams::CreateLegacy(2)));
1160     EXPECT_TRUE(channel_->GetRenderer(1, &renderer));
1161     EXPECT_TRUE(renderer == NULL);
1162     EXPECT_TRUE(channel_->GetRenderer(2, &renderer));
1163     EXPECT_TRUE(NULL == renderer);
1164
1165     // Ensure we can now set the renderers.
1166     EXPECT_TRUE(channel_->SetRenderer(1, &renderer1));
1167     EXPECT_TRUE(channel_->SetRenderer(2, &renderer2));
1168     EXPECT_TRUE(channel_->GetRenderer(1, &renderer));
1169     EXPECT_TRUE(&renderer1 == renderer);
1170     EXPECT_TRUE(channel_->GetRenderer(2, &renderer));
1171     EXPECT_TRUE(&renderer2 == renderer);
1172
1173     // Ensure we can change the renderers if needed.
1174     EXPECT_TRUE(channel_->SetRenderer(1, &renderer2));
1175     EXPECT_TRUE(channel_->SetRenderer(2, &renderer1));
1176     EXPECT_TRUE(channel_->GetRenderer(1, &renderer));
1177     EXPECT_TRUE(&renderer2 == renderer);
1178     EXPECT_TRUE(channel_->GetRenderer(2, &renderer));
1179     EXPECT_TRUE(&renderer1 == renderer);
1180
1181     EXPECT_TRUE(channel_->RemoveRecvStream(2));
1182     EXPECT_TRUE(channel_->RemoveRecvStream(1));
1183     EXPECT_FALSE(channel_->GetRenderer(1, &renderer));
1184     EXPECT_FALSE(channel_->GetRenderer(2, &renderer));
1185   }
1186
1187   // Tests setting up and configuring multiple incoming streams in a
1188   // non-conference call.
1189   void AddRemoveRecvStreamsNoConference() {
1190     cricket::FakeVideoRenderer renderer1, renderer2;
1191     // Ensure we can't set the renderer on a non-existent stream.
1192     EXPECT_FALSE(channel_->SetRenderer(1, &renderer1));
1193     EXPECT_FALSE(channel_->SetRenderer(2, &renderer2));
1194     cricket::VideoRenderer* renderer;
1195     EXPECT_FALSE(channel_->GetRenderer(1, &renderer));
1196     EXPECT_FALSE(channel_->GetRenderer(2, &renderer));
1197
1198     // Ensure we can add streams.
1199     EXPECT_TRUE(channel_->AddRecvStream(
1200         cricket::StreamParams::CreateLegacy(1)));
1201     EXPECT_TRUE(channel_->AddRecvStream(
1202         cricket::StreamParams::CreateLegacy(2)));
1203     EXPECT_TRUE(channel_->GetRenderer(1, &renderer));
1204     // Verify the first AddRecvStream hook up to the default renderer.
1205     EXPECT_TRUE(renderer == NULL);
1206     EXPECT_TRUE(channel_->GetRenderer(2, &renderer));
1207     EXPECT_TRUE(NULL == renderer);
1208
1209     // Ensure we can now set the renderers.
1210     EXPECT_TRUE(channel_->SetRenderer(1, &renderer1));
1211     EXPECT_TRUE(channel_->SetRenderer(2, &renderer2));
1212     EXPECT_TRUE(channel_->GetRenderer(1, &renderer));
1213     EXPECT_TRUE(&renderer1 == renderer);
1214     EXPECT_TRUE(channel_->GetRenderer(2, &renderer));
1215     EXPECT_TRUE(&renderer2 == renderer);
1216
1217     // Ensure we can change the renderers if needed.
1218     EXPECT_TRUE(channel_->SetRenderer(1, &renderer2));
1219     EXPECT_TRUE(channel_->SetRenderer(2, &renderer1));
1220     EXPECT_TRUE(channel_->GetRenderer(1, &renderer));
1221     EXPECT_TRUE(&renderer2 == renderer);
1222     EXPECT_TRUE(channel_->GetRenderer(2, &renderer));
1223     EXPECT_TRUE(&renderer1 == renderer);
1224
1225     EXPECT_TRUE(channel_->RemoveRecvStream(2));
1226     EXPECT_TRUE(channel_->RemoveRecvStream(1));
1227     EXPECT_FALSE(channel_->GetRenderer(1, &renderer));
1228     EXPECT_FALSE(channel_->GetRenderer(2, &renderer));
1229   }
1230
1231   // Test that no frames are rendered after the receive stream have been
1232   // removed.
1233   void AddRemoveRecvStreamAndRender() {
1234     cricket::FakeVideoRenderer renderer1;
1235     EXPECT_TRUE(SetDefaultCodec());
1236     EXPECT_TRUE(SetSend(true));
1237     EXPECT_TRUE(channel_->SetRender(true));
1238     EXPECT_TRUE(channel_->AddRecvStream(
1239         cricket::StreamParams::CreateLegacy(kSsrc)));
1240     EXPECT_TRUE(channel_->SetRenderer(kSsrc, &renderer1));
1241
1242     EXPECT_TRUE(SendFrame());
1243     EXPECT_FRAME_ON_RENDERER_WAIT(
1244         renderer1, 1, DefaultCodec().width, DefaultCodec().height, kTimeout);
1245     EXPECT_TRUE(channel_->RemoveRecvStream(kSsrc));
1246     // Send three more frames. This is to avoid that the test might be flaky
1247     // due to frame dropping.
1248     for (size_t i = 0; i < 3; ++i)
1249       EXPECT_TRUE(WaitAndSendFrame(100));
1250
1251     // Test that no more frames have been rendered.
1252     EXPECT_EQ(1, renderer1.num_rendered_frames());
1253
1254     // Re-add the stream again and make sure it renders.
1255     EXPECT_TRUE(channel_->AddRecvStream(
1256         cricket::StreamParams::CreateLegacy(kSsrc)));
1257     // Force the next frame to be a key frame to make the receiving
1258     // decoder happy.
1259     EXPECT_TRUE(channel_->SendIntraFrame());
1260
1261     EXPECT_TRUE(channel_->SetRenderer(kSsrc, &renderer1));
1262     EXPECT_TRUE(SendFrame());
1263     // Because the default channel is used, RemoveRecvStream above is not going
1264     // to delete the channel. As a result the engine will continue to receive
1265     // and decode the 3 frames sent above. So it is possible we will receive
1266     // some (e.g. 1) of these 3 frames after the renderer is set again.
1267     EXPECT_GT_FRAME_ON_RENDERER_WAIT(
1268         renderer1, 2, DefaultCodec().width, DefaultCodec().height, kTimeout);
1269     // Detach |renderer1| before exit as there might be frames come late.
1270     EXPECT_TRUE(channel_->SetRenderer(kSsrc, NULL));
1271   }
1272
1273   // Tests the behavior of incoming streams in a conference scenario.
1274   void SimulateConference() {
1275     cricket::FakeVideoRenderer renderer1, renderer2;
1276     EXPECT_TRUE(SetDefaultCodec());
1277     cricket::VideoOptions vmo;
1278     vmo.conference_mode.Set(true);
1279     EXPECT_TRUE(channel_->SetOptions(vmo));
1280     EXPECT_TRUE(SetSend(true));
1281     EXPECT_TRUE(channel_->SetRender(true));
1282     EXPECT_TRUE(channel_->AddRecvStream(
1283         cricket::StreamParams::CreateLegacy(1)));
1284     EXPECT_TRUE(channel_->AddRecvStream(
1285         cricket::StreamParams::CreateLegacy(2)));
1286     EXPECT_TRUE(channel_->SetRenderer(1, &renderer1));
1287     EXPECT_TRUE(channel_->SetRenderer(2, &renderer2));
1288     EXPECT_EQ(0, renderer1.num_rendered_frames());
1289     EXPECT_EQ(0, renderer2.num_rendered_frames());
1290     std::vector<uint32> ssrcs;
1291     ssrcs.push_back(1);
1292     ssrcs.push_back(2);
1293     network_interface_.SetConferenceMode(true, ssrcs);
1294     EXPECT_TRUE(SendFrame());
1295     EXPECT_FRAME_ON_RENDERER_WAIT(
1296         renderer1, 1, DefaultCodec().width, DefaultCodec().height, kTimeout);
1297     EXPECT_FRAME_ON_RENDERER_WAIT(
1298         renderer2, 1, DefaultCodec().width, DefaultCodec().height, kTimeout);
1299
1300     rtc::scoped_ptr<const rtc::Buffer> p(GetRtpPacket(0));
1301     EXPECT_EQ(DefaultCodec().id, GetPayloadType(p.get()));
1302     EXPECT_EQ(DefaultCodec().width, renderer1.width());
1303     EXPECT_EQ(DefaultCodec().height, renderer1.height());
1304     EXPECT_EQ(DefaultCodec().width, renderer2.width());
1305     EXPECT_EQ(DefaultCodec().height, renderer2.height());
1306     EXPECT_TRUE(channel_->RemoveRecvStream(2));
1307     EXPECT_TRUE(channel_->RemoveRecvStream(1));
1308   }
1309
1310   // Tests that we can add and remove capturers and frames are sent out properly
1311   void AddRemoveCapturer() {
1312     cricket::VideoCodec codec = DefaultCodec();
1313     codec.width = 320;
1314     codec.height = 240;
1315     const int time_between_send = TimeBetweenSend(codec);
1316     EXPECT_TRUE(SetOneCodec(codec));
1317     EXPECT_TRUE(SetSend(true));
1318     EXPECT_TRUE(channel_->SetRender(true));
1319     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
1320     EXPECT_EQ(0, renderer_.num_rendered_frames());
1321     EXPECT_TRUE(SendFrame());
1322     EXPECT_FRAME_WAIT(1, codec.width, codec.height, kTimeout);
1323     rtc::scoped_ptr<cricket::FakeVideoCapturer> capturer(
1324         CreateFakeVideoCapturer());
1325     capturer->SetScreencast(true);
1326     cricket::VideoFormat format(480, 360,
1327                                 cricket::VideoFormat::FpsToInterval(30),
1328                                 cricket::FOURCC_I420);
1329     EXPECT_EQ(cricket::CS_RUNNING, capturer->Start(format));
1330     // All capturers start generating frames with the same timestamp. ViE does
1331     // not allow the same timestamp to be used. Capture one frame before
1332     // associating the capturer with the channel.
1333     EXPECT_TRUE(capturer->CaptureCustomFrame(format.width, format.height,
1334                                              cricket::FOURCC_I420));
1335
1336     int captured_frames = 1;
1337     for (int iterations = 0; iterations < 2; ++iterations) {
1338       EXPECT_TRUE(channel_->SetCapturer(kSsrc, capturer.get()));
1339       rtc::Thread::Current()->ProcessMessages(time_between_send);
1340       EXPECT_TRUE(capturer->CaptureCustomFrame(format.width, format.height,
1341                                                cricket::FOURCC_I420));
1342       ++captured_frames;
1343       // Wait until frame of right size is captured.
1344       EXPECT_TRUE_WAIT(renderer_.num_rendered_frames() >= captured_frames &&
1345                        format.width == renderer_.width() &&
1346                        format.height == renderer_.height() &&
1347                        !renderer_.black_frame(), kTimeout);
1348       EXPECT_GE(renderer_.num_rendered_frames(), captured_frames);
1349       EXPECT_EQ(format.width, renderer_.width());
1350       EXPECT_EQ(format.height, renderer_.height());
1351       captured_frames = renderer_.num_rendered_frames() + 1;
1352       EXPECT_FALSE(renderer_.black_frame());
1353       EXPECT_TRUE(channel_->SetCapturer(kSsrc, NULL));
1354       // Make sure a black frame is generated within the specified timeout.
1355       // The black frame should be the resolution of the send codec.
1356       EXPECT_TRUE_WAIT(renderer_.num_rendered_frames() >= captured_frames &&
1357                        codec.width == renderer_.width() &&
1358                        codec.height == renderer_.height() &&
1359                        renderer_.black_frame(), kTimeout);
1360       EXPECT_GE(renderer_.num_rendered_frames(), captured_frames);
1361       EXPECT_EQ(codec.width, renderer_.width());
1362       EXPECT_EQ(codec.height, renderer_.height());
1363       EXPECT_TRUE(renderer_.black_frame());
1364
1365       // The black frame has the same timestamp as the next frame since it's
1366       // timestamp is set to the last frame's timestamp + interval. WebRTC will
1367       // not render a frame with the same timestamp so capture another frame
1368       // with the frame capturer to increment the next frame's timestamp.
1369       EXPECT_TRUE(capturer->CaptureCustomFrame(format.width, format.height,
1370                                                cricket::FOURCC_I420));
1371     }
1372   }
1373
1374   // Tests that if RemoveCapturer is called without a capturer ever being
1375   // added, the plugin shouldn't crash (and no black frame should be sent).
1376   void RemoveCapturerWithoutAdd() {
1377     EXPECT_TRUE(SetOneCodec(DefaultCodec()));
1378     EXPECT_TRUE(SetSend(true));
1379     EXPECT_TRUE(channel_->SetRender(true));
1380     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
1381     EXPECT_EQ(0, renderer_.num_rendered_frames());
1382     EXPECT_TRUE(SendFrame());
1383     EXPECT_FRAME_WAIT(1, 640, 400, kTimeout);
1384     // Remove the capturer.
1385     EXPECT_TRUE(channel_->SetCapturer(kSsrc, NULL));
1386     // Wait for one black frame for removing the capturer.
1387     EXPECT_FRAME_WAIT(2, 640, 400, kTimeout);
1388
1389     // No capturer was added, so this RemoveCapturer should
1390     // fail.
1391     EXPECT_FALSE(channel_->SetCapturer(kSsrc, NULL));
1392     rtc::Thread::Current()->ProcessMessages(300);
1393     // Verify no more frames were sent.
1394     EXPECT_EQ(2, renderer_.num_rendered_frames());
1395   }
1396
1397   // Tests that we can add and remove capturer as unique sources.
1398   void AddRemoveCapturerMultipleSources() {
1399     // WebRTC implementation will drop frames if pushed to quickly. Wait the
1400     // interval time to avoid that.
1401     // WebRTC implementation will drop frames if pushed to quickly. Wait the
1402     // interval time to avoid that.
1403     // Set up the stream associated with the engine.
1404     EXPECT_TRUE(channel_->AddRecvStream(
1405         cricket::StreamParams::CreateLegacy(kSsrc)));
1406     EXPECT_TRUE(channel_->SetRenderer(kSsrc, &renderer_));
1407     cricket::VideoFormat capture_format;  // default format
1408     capture_format.interval = cricket::VideoFormat::FpsToInterval(30);
1409     // Set up additional stream 1.
1410     cricket::FakeVideoRenderer renderer1;
1411     EXPECT_FALSE(channel_->SetRenderer(1, &renderer1));
1412     EXPECT_TRUE(channel_->AddRecvStream(
1413         cricket::StreamParams::CreateLegacy(1)));
1414     EXPECT_TRUE(channel_->SetRenderer(1, &renderer1));
1415     EXPECT_TRUE(channel_->AddSendStream(
1416         cricket::StreamParams::CreateLegacy(1)));
1417     rtc::scoped_ptr<cricket::FakeVideoCapturer> capturer1(
1418         CreateFakeVideoCapturer());
1419     capturer1->SetScreencast(true);
1420     EXPECT_EQ(cricket::CS_RUNNING, capturer1->Start(capture_format));
1421     // Set up additional stream 2.
1422     cricket::FakeVideoRenderer renderer2;
1423     EXPECT_FALSE(channel_->SetRenderer(2, &renderer2));
1424     EXPECT_TRUE(channel_->AddRecvStream(
1425         cricket::StreamParams::CreateLegacy(2)));
1426     EXPECT_TRUE(channel_->SetRenderer(2, &renderer2));
1427     EXPECT_TRUE(channel_->AddSendStream(
1428         cricket::StreamParams::CreateLegacy(2)));
1429     rtc::scoped_ptr<cricket::FakeVideoCapturer> capturer2(
1430         CreateFakeVideoCapturer());
1431     capturer2->SetScreencast(true);
1432     EXPECT_EQ(cricket::CS_RUNNING, capturer2->Start(capture_format));
1433     // State for all the streams.
1434     EXPECT_TRUE(SetOneCodec(DefaultCodec()));
1435     // A limitation in the lmi implementation requires that SetCapturer() is
1436     // called after SetOneCodec().
1437     // TODO(hellner): this seems like an unnecessary constraint, fix it.
1438     EXPECT_TRUE(channel_->SetCapturer(1, capturer1.get()));
1439     EXPECT_TRUE(channel_->SetCapturer(2, capturer2.get()));
1440     EXPECT_TRUE(SetSend(true));
1441     EXPECT_TRUE(channel_->SetRender(true));
1442     // Test capturer associated with engine.
1443     const int kTestWidth = 160;
1444     const int kTestHeight = 120;
1445     EXPECT_TRUE(capturer1->CaptureCustomFrame(
1446         kTestWidth, kTestHeight, cricket::FOURCC_I420));
1447     EXPECT_FRAME_ON_RENDERER_WAIT(
1448         renderer1, 1, kTestWidth, kTestHeight, kTimeout);
1449     // Capture a frame with additional capturer2, frames should be received
1450     EXPECT_TRUE(capturer2->CaptureCustomFrame(
1451         kTestWidth, kTestHeight, cricket::FOURCC_I420));
1452     EXPECT_FRAME_ON_RENDERER_WAIT(
1453         renderer2, 1, kTestWidth, kTestHeight, kTimeout);
1454     // Successfully remove the capturer.
1455     EXPECT_TRUE(channel_->SetCapturer(kSsrc, NULL));
1456     // Fail to re-remove the capturer.
1457     EXPECT_FALSE(channel_->SetCapturer(kSsrc, NULL));
1458     // The capturers must be unregistered here as it runs out of it's scope
1459     // next.
1460     EXPECT_TRUE(channel_->SetCapturer(1, NULL));
1461     EXPECT_TRUE(channel_->SetCapturer(2, NULL));
1462   }
1463
1464   void HighAspectHighHeightCapturer() {
1465     const int kWidth  = 80;
1466     const int kHeight = 10000;
1467     const int kScaledWidth = 20;
1468     const int kScaledHeight = 2500;
1469
1470     cricket::VideoCodec codec(DefaultCodec());
1471     EXPECT_TRUE(SetOneCodec(codec));
1472     EXPECT_TRUE(SetSend(true));
1473
1474     cricket::FakeVideoRenderer renderer;
1475     EXPECT_TRUE(channel_->AddRecvStream(
1476         cricket::StreamParams::CreateLegacy(kSsrc)));
1477     EXPECT_TRUE(channel_->SetRenderer(kSsrc, &renderer));
1478     EXPECT_TRUE(channel_->SetRender(true));
1479     EXPECT_EQ(0, renderer.num_rendered_frames());
1480
1481     EXPECT_TRUE(SendFrame());
1482     EXPECT_GT_FRAME_ON_RENDERER_WAIT(
1483         renderer, 1, codec.width, codec.height, kTimeout);
1484
1485     // Registering an external capturer is currently the same as screen casting
1486     // (update the test when this changes).
1487     rtc::scoped_ptr<cricket::FakeVideoCapturer> capturer(
1488         CreateFakeVideoCapturer());
1489     capturer->SetScreencast(true);
1490     const std::vector<cricket::VideoFormat>* formats =
1491         capturer->GetSupportedFormats();
1492     cricket::VideoFormat capture_format = (*formats)[0];
1493     EXPECT_EQ(cricket::CS_RUNNING, capturer->Start(capture_format));
1494     // Capture frame to not get same frame timestamps as previous capturer.
1495     capturer->CaptureFrame();
1496     EXPECT_TRUE(channel_->SetCapturer(kSsrc, capturer.get()));
1497     EXPECT_TRUE(rtc::Thread::Current()->ProcessMessages(30));
1498     EXPECT_TRUE(capturer->CaptureCustomFrame(kWidth, kHeight,
1499                                              cricket::FOURCC_ARGB));
1500     EXPECT_GT_FRAME_ON_RENDERER_WAIT(
1501         renderer, 2, kScaledWidth, kScaledHeight, kTimeout);
1502     EXPECT_TRUE(channel_->SetCapturer(kSsrc, NULL));
1503   }
1504
1505   // Tests that we can adapt video resolution with 16:10 aspect ratio properly.
1506   void AdaptResolution16x10() {
1507     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
1508     cricket::VideoCodec codec(DefaultCodec());
1509     codec.width = 640;
1510     codec.height = 400;
1511     SendAndReceive(codec);
1512     codec.width /= 2;
1513     codec.height /= 2;
1514     // Adapt the resolution.
1515     EXPECT_TRUE(SetOneCodec(codec));
1516     EXPECT_TRUE(WaitAndSendFrame(30));
1517     EXPECT_FRAME_WAIT(2, codec.width, codec.height, kTimeout);
1518   }
1519   // Tests that we can adapt video resolution with 4:3 aspect ratio properly.
1520   void AdaptResolution4x3() {
1521     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
1522     cricket::VideoCodec codec(DefaultCodec());
1523     codec.width = 640;
1524     codec.height = 400;
1525     SendAndReceive(codec);
1526     codec.width /= 2;
1527     codec.height /= 2;
1528     // Adapt the resolution.
1529     EXPECT_TRUE(SetOneCodec(codec));
1530     EXPECT_TRUE(WaitAndSendFrame(30));
1531     EXPECT_FRAME_WAIT(2, codec.width, codec.height, kTimeout);
1532   }
1533   // Tests that we can drop all frames properly.
1534   void AdaptDropAllFrames() {
1535     // Set the channel codec's resolution to 0, which will require the adapter
1536     // to drop all frames.
1537     cricket::VideoCodec codec(DefaultCodec());
1538     codec.width = codec.height = codec.framerate = 0;
1539     EXPECT_TRUE(SetOneCodec(codec));
1540     EXPECT_TRUE(SetSend(true));
1541     EXPECT_TRUE(channel_->SetRender(true));
1542     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
1543     EXPECT_EQ(0, renderer_.num_rendered_frames());
1544     EXPECT_TRUE(SendFrame());
1545     EXPECT_TRUE(SendFrame());
1546     rtc::Thread::Current()->ProcessMessages(500);
1547     EXPECT_EQ(0, renderer_.num_rendered_frames());
1548   }
1549   // Tests that we can reduce the frame rate on demand properly.
1550   // TODO(fbarchard): This test is flakey on pulse.  Fix and re-enable
1551   void AdaptFramerate() {
1552     cricket::VideoCodec codec(DefaultCodec());
1553     int frame_count = 0;
1554     // The capturer runs at 30 fps. The channel requires 30 fps.
1555     EXPECT_TRUE(SetOneCodec(codec));
1556     EXPECT_TRUE(SetSend(true));
1557     EXPECT_TRUE(channel_->SetRender(true));
1558     EXPECT_EQ(frame_count, renderer_.num_rendered_frames());
1559     EXPECT_TRUE(WaitAndSendFrame(0));  // Should be rendered.
1560     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be rendered.
1561     frame_count += 2;
1562     EXPECT_FRAME_WAIT(frame_count, codec.width, codec.height, kTimeout);
1563     rtc::scoped_ptr<const rtc::Buffer> p(GetRtpPacket(0));
1564     EXPECT_EQ(codec.id, GetPayloadType(p.get()));
1565
1566     // The channel requires 15 fps.
1567     codec.framerate = 15;
1568     EXPECT_TRUE(SetOneCodec(codec));
1569     EXPECT_TRUE(WaitAndSendFrame(0));  // Should be rendered.
1570     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be dropped.
1571     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be rendered.
1572     frame_count += 2;
1573     EXPECT_EQ_WAIT(frame_count, renderer_.num_rendered_frames(), kTimeout);
1574
1575     // The channel requires 10 fps.
1576     codec.framerate = 10;
1577     EXPECT_TRUE(SetOneCodec(codec));
1578     EXPECT_TRUE(WaitAndSendFrame(0));  // Should be rendered.
1579     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be dropped.
1580     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be dropped.
1581     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be rendered.
1582     frame_count += 2;
1583     EXPECT_EQ_WAIT(frame_count, renderer_.num_rendered_frames(), kTimeout);
1584
1585     // The channel requires 8 fps. The adapter adapts to 10 fps, which is the
1586     // closest factor of 30.
1587     codec.framerate = 8;
1588     EXPECT_TRUE(SetOneCodec(codec));
1589     EXPECT_TRUE(WaitAndSendFrame(0));  // Should be rendered.
1590     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be dropped.
1591     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be dropped.
1592     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be rendered.
1593     frame_count += 2;
1594     EXPECT_EQ_WAIT(frame_count, renderer_.num_rendered_frames(), kTimeout);
1595   }
1596   // Tests that we can set the send stream format properly.
1597   void SetSendStreamFormat() {
1598     cricket::VideoCodec codec(DefaultCodec());
1599     SendAndReceive(codec);
1600     int frame_count = 1;
1601     EXPECT_FRAME_WAIT(frame_count, codec.width, codec.height, kTimeout);
1602
1603     // Adapt the resolution and frame rate to half.
1604     cricket::VideoFormat format(
1605         codec.width / 2,
1606         codec.height / 2,
1607         cricket::VideoFormat::FpsToInterval(codec.framerate / 2),
1608         cricket::FOURCC_I420);
1609     // The SSRC differs from the send SSRC.
1610     EXPECT_FALSE(channel_->SetSendStreamFormat(kSsrc - 1, format));
1611     EXPECT_TRUE(channel_->SetSendStreamFormat(kSsrc, format));
1612
1613     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be dropped.
1614     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be rendered.
1615     EXPECT_TRUE(WaitAndSendFrame(30));  // Should be dropped.
1616     frame_count += 1;
1617     EXPECT_FRAME_WAIT(frame_count, format.width, format.height, kTimeout);
1618
1619     // Adapt the resolution to 0x0, which should drop all frames.
1620     format.width = 0;
1621     format.height = 0;
1622     EXPECT_TRUE(channel_->SetSendStreamFormat(kSsrc, format));
1623     EXPECT_TRUE(SendFrame());
1624     EXPECT_TRUE(SendFrame());
1625     rtc::Thread::Current()->ProcessMessages(500);
1626     EXPECT_EQ(frame_count, renderer_.num_rendered_frames());
1627   }
1628   // Test that setting send stream format to 0x0 resolution will result in
1629   // frames being dropped.
1630   void SetSendStreamFormat0x0() {
1631     EXPECT_TRUE(SetOneCodec(DefaultCodec()));
1632     EXPECT_TRUE(SetSendStreamFormat(kSsrc, DefaultCodec()));
1633     EXPECT_TRUE(SetSend(true));
1634     EXPECT_TRUE(channel_->SetRender(true));
1635     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
1636     EXPECT_EQ(0, renderer_.num_rendered_frames());
1637     // This frame should be received.
1638     EXPECT_TRUE(SendFrame());
1639     EXPECT_FRAME_WAIT(1, DefaultCodec().width, DefaultCodec().height, kTimeout);
1640     const int64 interval = cricket::VideoFormat::FpsToInterval(
1641         DefaultCodec().framerate);
1642     cricket::VideoFormat format(
1643         0,
1644         0,
1645         interval,
1646         cricket::FOURCC_I420);
1647     EXPECT_TRUE(channel_->SetSendStreamFormat(kSsrc, format));
1648     // This frame should not be received.
1649     EXPECT_TRUE(WaitAndSendFrame(
1650         static_cast<int>(interval/rtc::kNumNanosecsPerMillisec)));
1651     rtc::Thread::Current()->ProcessMessages(500);
1652     EXPECT_EQ(1, renderer_.num_rendered_frames());
1653   }
1654
1655   // Tests that we can mute and unmute the channel properly.
1656   void MuteStream() {
1657     EXPECT_TRUE(SetDefaultCodec());
1658     cricket::FakeVideoCapturer video_capturer;
1659     video_capturer.Start(
1660         cricket::VideoFormat(
1661             640, 480,
1662             cricket::VideoFormat::FpsToInterval(30),
1663             cricket::FOURCC_I420));
1664     EXPECT_TRUE(channel_->SetCapturer(kSsrc, &video_capturer));
1665     EXPECT_TRUE(SetSend(true));
1666     EXPECT_TRUE(channel_->SetRender(true));
1667     EXPECT_TRUE(channel_->SetRenderer(kDefaultReceiveSsrc, &renderer_));
1668     EXPECT_EQ(0, renderer_.num_rendered_frames());
1669
1670     // Mute the channel and expect black output frame.
1671     int frame_count = 0;
1672     EXPECT_TRUE(channel_->MuteStream(kSsrc, true));
1673     EXPECT_TRUE(video_capturer.CaptureFrame());
1674     ++frame_count;
1675     EXPECT_EQ_WAIT(frame_count, renderer_.num_rendered_frames(), kTimeout);
1676     EXPECT_TRUE(renderer_.black_frame());
1677
1678     // Unmute the channel and expect non-black output frame.
1679     EXPECT_TRUE(channel_->MuteStream(kSsrc, false));
1680     EXPECT_TRUE(rtc::Thread::Current()->ProcessMessages(30));
1681     EXPECT_TRUE(video_capturer.CaptureFrame());
1682     ++frame_count;
1683     EXPECT_EQ_WAIT(frame_count, renderer_.num_rendered_frames(), kTimeout);
1684     EXPECT_FALSE(renderer_.black_frame());
1685
1686     // Test that we can also Mute using the correct send stream SSRC.
1687     EXPECT_TRUE(channel_->MuteStream(kSsrc, true));
1688     EXPECT_TRUE(rtc::Thread::Current()->ProcessMessages(30));
1689     EXPECT_TRUE(video_capturer.CaptureFrame());
1690     ++frame_count;
1691     EXPECT_EQ_WAIT(frame_count, renderer_.num_rendered_frames(), kTimeout);
1692     EXPECT_TRUE(renderer_.black_frame());
1693
1694     EXPECT_TRUE(channel_->MuteStream(kSsrc, false));
1695     EXPECT_TRUE(rtc::Thread::Current()->ProcessMessages(30));
1696     EXPECT_TRUE(video_capturer.CaptureFrame());
1697     ++frame_count;
1698     EXPECT_EQ_WAIT(frame_count, renderer_.num_rendered_frames(), kTimeout);
1699     EXPECT_FALSE(renderer_.black_frame());
1700
1701     // Test that muting an existing stream succeeds even if it's muted.
1702     EXPECT_TRUE(channel_->MuteStream(kSsrc, true));
1703     EXPECT_TRUE(channel_->MuteStream(kSsrc, true));
1704
1705     // Test that unmuting an existing stream succeeds even if it's not muted.
1706     EXPECT_TRUE(channel_->MuteStream(kSsrc, false));
1707     EXPECT_TRUE(channel_->MuteStream(kSsrc, false));
1708
1709     // Test that muting an invalid stream fails.
1710     EXPECT_FALSE(channel_->MuteStream(kSsrc+1, true));
1711     EXPECT_TRUE(channel_->SetCapturer(kSsrc, NULL));
1712   }
1713
1714   // Test that multiple send streams can be created and deleted properly.
1715   void MultipleSendStreams() {
1716     // Remove stream added in Setup. I.e. remove stream corresponding to default
1717     // channel.
1718     EXPECT_TRUE(channel_->RemoveSendStream(kSsrc));
1719     const unsigned int kSsrcsSize = sizeof(kSsrcs4)/sizeof(kSsrcs4[0]);
1720     for (unsigned int i = 0; i < kSsrcsSize; ++i) {
1721       EXPECT_TRUE(channel_->AddSendStream(
1722           cricket::StreamParams::CreateLegacy(kSsrcs4[i])));
1723     }
1724     // Delete one of the non default channel streams, let the destructor delete
1725     // the remaining ones.
1726     EXPECT_TRUE(channel_->RemoveSendStream(kSsrcs4[kSsrcsSize - 1]));
1727     // Stream should already be deleted.
1728     EXPECT_FALSE(channel_->RemoveSendStream(kSsrcs4[kSsrcsSize - 1]));
1729   }
1730
1731   // Two streams one channel tests.
1732
1733   // Tests that we can send and receive frames.
1734   void TwoStreamsSendAndReceive(const cricket::VideoCodec& codec) {
1735     SetUpSecondStream();
1736     // Test sending and receiving on first stream.
1737     SendAndReceive(codec);
1738     // Test sending and receiving on second stream.
1739     EXPECT_EQ_WAIT(1, renderer2_.num_rendered_frames(), kTimeout);
1740     EXPECT_EQ(2, NumRtpPackets());
1741     EXPECT_EQ(1, renderer2_.num_rendered_frames());
1742   }
1743
1744   // Set up 2 streams where the first stream uses the default channel.
1745   // Then disconnect the first stream and verify default channel becomes
1746   // available.
1747   // Then add a new stream with |new_ssrc|. The new stream should re-use the
1748   // default channel.
1749   void TwoStreamsReUseFirstStream(const cricket::VideoCodec& codec) {
1750     SetUpSecondStream();
1751     // Default channel used by the first stream.
1752     EXPECT_EQ(kSsrc, channel_->GetDefaultSendChannelSsrc());
1753     EXPECT_TRUE(channel_->RemoveRecvStream(kSsrc));
1754     EXPECT_FALSE(channel_->RemoveRecvStream(kSsrc));
1755     EXPECT_TRUE(channel_->RemoveSendStream(kSsrc));
1756     EXPECT_FALSE(channel_->RemoveSendStream(kSsrc));
1757     // Default channel is no longer used by a stream.
1758     EXPECT_EQ(0u, channel_->GetDefaultSendChannelSsrc());
1759     uint32 new_ssrc = kSsrc + 100;
1760     EXPECT_TRUE(channel_->AddSendStream(
1761         cricket::StreamParams::CreateLegacy(new_ssrc)));
1762     // Re-use default channel.
1763     EXPECT_EQ(new_ssrc, channel_->GetDefaultSendChannelSsrc());
1764     EXPECT_FALSE(channel_->AddSendStream(
1765         cricket::StreamParams::CreateLegacy(new_ssrc)));
1766     EXPECT_TRUE(channel_->AddRecvStream(
1767         cricket::StreamParams::CreateLegacy(new_ssrc)));
1768     EXPECT_TRUE(channel_->SetRenderer(new_ssrc, &renderer_));
1769     EXPECT_FALSE(channel_->AddRecvStream(
1770         cricket::StreamParams::CreateLegacy(new_ssrc)));
1771
1772     EXPECT_TRUE(channel_->SetCapturer(new_ssrc, video_capturer_.get()));
1773
1774     SendAndReceive(codec);
1775     EXPECT_TRUE(channel_->RemoveSendStream(new_ssrc));
1776     EXPECT_EQ(0u, channel_->GetDefaultSendChannelSsrc());
1777   }
1778
1779   // Tests that we can send and receive frames with early receive.
1780   void TwoStreamsSendAndUnsignalledRecv(const cricket::VideoCodec& codec) {
1781     cricket::VideoOptions vmo;
1782     vmo.conference_mode.Set(true);
1783     vmo.unsignalled_recv_stream_limit.Set(1);
1784     EXPECT_TRUE(channel_->SetOptions(vmo));
1785     SetUpSecondStreamWithNoRecv();
1786     // Test sending and receiving on first stream.
1787     EXPECT_TRUE(channel_->SetRender(true));
1788     Send(codec);
1789     EXPECT_EQ_WAIT(2, NumRtpPackets(), kTimeout);
1790     EXPECT_EQ_WAIT(1, renderer_.num_rendered_frames(), kTimeout);
1791     // The first send is not expected to yield frames, because the ssrc
1792     // is not signalled yet. With unsignalled recv enabled, we will drop frames
1793     // instead of packets.
1794     EXPECT_EQ(0, renderer2_.num_rendered_frames());
1795     // Give a chance for the decoder to process before adding the receiver.
1796     rtc::Thread::Current()->ProcessMessages(100);
1797     // Test sending and receiving on second stream.
1798     EXPECT_TRUE(channel_->AddRecvStream(
1799         cricket::StreamParams::CreateLegacy(kSsrc + 2)));
1800     EXPECT_TRUE(channel_->SetRenderer(kSsrc + 2, &renderer2_));
1801     SendFrame();
1802     EXPECT_EQ_WAIT(2, renderer_.num_rendered_frames(), kTimeout);
1803     EXPECT_EQ(4, NumRtpPackets());
1804     // The second send is expected to yield frame as the ssrc is signalled now.
1805     // Decode should succeed here, though we received the key frame earlier.
1806     // Without early recv, we would have dropped it and decoding would have
1807     // failed.
1808     EXPECT_EQ_WAIT(1, renderer2_.num_rendered_frames(), kTimeout);
1809   }
1810
1811   // Tests that we cannot receive key frames with unsignalled recv disabled.
1812   void TwoStreamsSendAndFailUnsignalledRecv(const cricket::VideoCodec& codec) {
1813     cricket::VideoOptions vmo;
1814     vmo.conference_mode.Set(true);
1815     vmo.unsignalled_recv_stream_limit.Set(0);
1816     EXPECT_TRUE(channel_->SetOptions(vmo));
1817     SetUpSecondStreamWithNoRecv();
1818     // Test sending and receiving on first stream.
1819     EXPECT_TRUE(channel_->SetRender(true));
1820     Send(codec);
1821     EXPECT_EQ_WAIT(2, NumRtpPackets(), kTimeout);
1822     rtc::Thread::Current()->ProcessMessages(100);
1823     EXPECT_EQ_WAIT(1, renderer_.num_rendered_frames(), kTimeout);
1824     EXPECT_EQ_WAIT(0, renderer2_.num_rendered_frames(), kTimeout);
1825     // Give a chance for the decoder to process before adding the receiver.
1826     rtc::Thread::Current()->ProcessMessages(10);
1827     // Test sending and receiving on second stream.
1828     EXPECT_TRUE(channel_->AddRecvStream(
1829         cricket::StreamParams::CreateLegacy(kSsrc + 2)));
1830     EXPECT_TRUE(channel_->SetRenderer(kSsrc + 2, &renderer2_));
1831     SendFrame();
1832     EXPECT_TRUE_WAIT(renderer_.num_rendered_frames() >= 1, kTimeout);
1833     EXPECT_EQ_WAIT(4, NumRtpPackets(), kTimeout);
1834     // We dont expect any frames here, because the key frame would have been
1835     // lost in the earlier packet. This is the case we want to solve with early
1836     // receive.
1837     EXPECT_EQ(0, renderer2_.num_rendered_frames());
1838   }
1839
1840   // Tests that we drop key frames when conference mode is disabled and we
1841   // receive rtp packets on unsignalled streams.
1842   void TwoStreamsSendAndFailUnsignalledRecvInOneToOne(
1843       const cricket::VideoCodec& codec) {
1844     cricket::VideoOptions vmo;
1845     vmo.conference_mode.Set(false);
1846     vmo.unsignalled_recv_stream_limit.Set(1);
1847     EXPECT_TRUE(channel_->SetOptions(vmo));
1848     SetUpSecondStreamWithNoRecv();
1849     // Test sending and receiving on first stream.
1850     EXPECT_TRUE(channel_->SetRender(true));
1851     Send(codec);
1852     EXPECT_EQ_WAIT(2, NumRtpPackets(), kTimeout);
1853     // In one-to-one mode, we deliver frames to the default channel if there
1854     // is no registered recv channel for the ssrc.
1855     EXPECT_TRUE_WAIT(renderer_.num_rendered_frames() >= 1, kTimeout);
1856     // Give a chance for the decoder to process before adding the receiver.
1857     rtc::Thread::Current()->ProcessMessages(100);
1858     // Test sending and receiving on second stream.
1859     EXPECT_TRUE(channel_->AddRecvStream(
1860         cricket::StreamParams::CreateLegacy(kSsrc + 2)));
1861     EXPECT_TRUE(channel_->SetRenderer(kSsrc + 2, &renderer2_));
1862     SendFrame();
1863     EXPECT_TRUE_WAIT(renderer_.num_rendered_frames() >= 1, kTimeout);
1864     EXPECT_EQ_WAIT(4, NumRtpPackets(), kTimeout);
1865     // We dont expect any frames here, because the key frame would have been
1866     // delivered to default channel.
1867     EXPECT_EQ(0, renderer2_.num_rendered_frames());
1868   }
1869
1870   // Tests that we drop key frames when conference mode is enabled and we
1871   // receive rtp packets on unsignalled streams. Removal of a unsignalled recv
1872   // stream is successful.
1873   void TwoStreamsAddAndRemoveUnsignalledRecv(
1874       const cricket::VideoCodec& codec) {
1875     cricket::VideoOptions vmo;
1876     vmo.conference_mode.Set(true);
1877     vmo.unsignalled_recv_stream_limit.Set(1);
1878     EXPECT_TRUE(channel_->SetOptions(vmo));
1879     SetUpSecondStreamWithNoRecv();
1880     // Sending and receiving on first stream.
1881     EXPECT_TRUE(channel_->SetRender(true));
1882     Send(codec);
1883     EXPECT_EQ_WAIT(2, NumRtpPackets(), kTimeout);
1884     EXPECT_EQ_WAIT(1, renderer_.num_rendered_frames(), kTimeout);
1885     // The first send is not expected to yield frames, because the ssrc
1886     // is no signalled yet. With unsignalled recv enabled, we will drop frames
1887     // instead of packets.
1888     EXPECT_EQ(0, renderer2_.num_rendered_frames());
1889     // Give a chance for the decoder to process before adding the receiver.
1890     rtc::Thread::Current()->ProcessMessages(100);
1891     // Ensure that we can remove the unsignalled recv stream that was created
1892     // when the first video packet with unsignalled recv ssrc is received.
1893     EXPECT_TRUE(channel_->RemoveRecvStream(kSsrc + 2));
1894   }
1895
1896   VideoEngineOverride<E> engine_;
1897   rtc::scoped_ptr<cricket::FakeVideoCapturer> video_capturer_;
1898   rtc::scoped_ptr<cricket::FakeVideoCapturer> video_capturer_2_;
1899   rtc::scoped_ptr<C> channel_;
1900   cricket::FakeNetworkInterface network_interface_;
1901   cricket::FakeVideoRenderer renderer_;
1902   cricket::VideoMediaChannel::Error media_error_;
1903
1904   // Used by test cases where 2 streams are run on the same channel.
1905   cricket::FakeVideoRenderer renderer2_;
1906 };
1907
1908 #endif  // TALK_MEDIA_BASE_VIDEOENGINE_UNITTEST_H_  NOLINT