Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / session / media / channel_unittest.cc
1 // libjingle
2 // Copyright 2009 Google Inc.
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 #include "talk/base/fileutils.h"
27 #include "talk/base/gunit.h"
28 #include "talk/base/helpers.h"
29 #include "talk/base/logging.h"
30 #include "talk/base/pathutils.h"
31 #include "talk/base/signalthread.h"
32 #include "talk/base/ssladapter.h"
33 #include "talk/base/sslidentity.h"
34 #include "talk/base/window.h"
35 #include "talk/media/base/fakemediaengine.h"
36 #include "talk/media/base/fakertp.h"
37 #include "talk/media/base/fakevideocapturer.h"
38 #include "talk/media/base/mediachannel.h"
39 #include "talk/media/base/rtpdump.h"
40 #include "talk/media/base/screencastid.h"
41 #include "talk/media/base/testutils.h"
42 #include "talk/p2p/base/fakesession.h"
43 #include "talk/session/media/channel.h"
44 #include "talk/session/media/mediamessages.h"
45 #include "talk/session/media/mediarecorder.h"
46 #include "talk/session/media/mediasessionclient.h"
47 #include "talk/session/media/typingmonitor.h"
48
49 #define MAYBE_SKIP_TEST(feature)                    \
50   if (!(talk_base::SSLStreamAdapter::feature())) {  \
51     LOG(LS_INFO) << "Feature disabled... skipping"; \
52     return;                                         \
53   }
54
55 using cricket::CA_OFFER;
56 using cricket::CA_PRANSWER;
57 using cricket::CA_ANSWER;
58 using cricket::CA_UPDATE;
59 using cricket::FakeVoiceMediaChannel;
60 using cricket::ScreencastId;
61 using cricket::StreamParams;
62 using cricket::TransportChannel;
63 using talk_base::WindowId;
64
65 static const cricket::AudioCodec kPcmuCodec(0, "PCMU", 64000, 8000, 1, 0);
66 static const cricket::AudioCodec kPcmaCodec(8, "PCMA", 64000, 8000, 1, 0);
67 static const cricket::AudioCodec kIsacCodec(103, "ISAC", 40000, 16000, 1, 0);
68 static const cricket::VideoCodec kH264Codec(97, "H264", 640, 400, 30, 0);
69 static const cricket::VideoCodec kH264SvcCodec(99, "H264-SVC", 320, 200, 15, 0);
70 static const cricket::DataCodec kGoogleDataCodec(101, "google-data", 0);
71 static const uint32 kSsrc1 = 0x1111;
72 static const uint32 kSsrc2 = 0x2222;
73 static const uint32 kSsrc3 = 0x3333;
74 static const int kAudioPts[] = {0, 8};
75 static const int kVideoPts[] = {97, 99};
76
77 template<class ChannelT,
78          class MediaChannelT,
79          class ContentT,
80          class CodecT,
81          class MediaInfoT>
82 class Traits {
83  public:
84   typedef ChannelT Channel;
85   typedef MediaChannelT MediaChannel;
86   typedef ContentT Content;
87   typedef CodecT Codec;
88   typedef MediaInfoT MediaInfo;
89 };
90
91 class FakeScreenCaptureFactory
92     : public cricket::VideoChannel::ScreenCapturerFactory,
93       public sigslot::has_slots<> {
94  public:
95   FakeScreenCaptureFactory()
96       : window_capturer_(NULL),
97         capture_state_(cricket::CS_STOPPED) {}
98
99   virtual cricket::VideoCapturer* CreateScreenCapturer(
100       const ScreencastId& window) {
101     if (window_capturer_ != NULL) {
102       // Class is only designed to handle one fake screencapturer.
103       ADD_FAILURE();
104       return NULL;
105     }
106     window_capturer_ = new cricket::FakeVideoCapturer;
107     window_capturer_->SignalDestroyed.connect(
108         this,
109         &FakeScreenCaptureFactory::OnWindowCapturerDestroyed);
110     window_capturer_->SignalStateChange.connect(
111         this,
112         &FakeScreenCaptureFactory::OnStateChange);
113     return window_capturer_;
114   }
115
116   cricket::FakeVideoCapturer* window_capturer() { return window_capturer_; }
117
118   cricket::CaptureState capture_state() { return capture_state_; }
119
120  private:
121   void OnWindowCapturerDestroyed(cricket::FakeVideoCapturer* capturer) {
122     if (capturer == window_capturer_) {
123       window_capturer_ = NULL;
124     }
125   }
126   void OnStateChange(cricket::VideoCapturer*, cricket::CaptureState state) {
127     capture_state_ = state;
128   }
129
130   cricket::FakeVideoCapturer* window_capturer_;
131   cricket::CaptureState capture_state_;
132 };
133
134 // Controls how long we wait for a session to send messages that we
135 // expect, in milliseconds.  We put it high to avoid flaky tests.
136 static const int kEventTimeout = 5000;
137
138 class VoiceTraits : public Traits<cricket::VoiceChannel,
139                                   cricket::FakeVoiceMediaChannel,
140                                   cricket::AudioContentDescription,
141                                   cricket::AudioCodec,
142                                   cricket::VoiceMediaInfo> {
143 };
144
145 class VideoTraits : public Traits<cricket::VideoChannel,
146                                   cricket::FakeVideoMediaChannel,
147                                   cricket::VideoContentDescription,
148                                   cricket::VideoCodec,
149                                   cricket::VideoMediaInfo> {
150 };
151
152 class DataTraits : public Traits<cricket::DataChannel,
153                                  cricket::FakeDataMediaChannel,
154                                  cricket::DataContentDescription,
155                                  cricket::DataCodec,
156                                  cricket::DataMediaInfo> {
157 };
158
159
160 talk_base::StreamInterface* Open(const std::string& path) {
161   return talk_base::Filesystem::OpenFile(
162       talk_base::Pathname(path), "wb");
163 }
164
165 // Base class for Voice/VideoChannel tests
166 template<class T>
167 class ChannelTest : public testing::Test, public sigslot::has_slots<> {
168  public:
169   enum Flags { RTCP = 0x1, RTCP_MUX = 0x2, SECURE = 0x4, SSRC_MUX = 0x8,
170                DTLS = 0x10 };
171
172   ChannelTest(const uint8* rtp_data, int rtp_len,
173               const uint8* rtcp_data, int rtcp_len)
174       : session1_(true),
175         session2_(false),
176         media_channel1_(NULL),
177         media_channel2_(NULL),
178         rtp_packet_(reinterpret_cast<const char*>(rtp_data), rtp_len),
179         rtcp_packet_(reinterpret_cast<const char*>(rtcp_data), rtcp_len),
180         media_info_callbacks1_(),
181         media_info_callbacks2_(),
182         mute_callback_recved_(false),
183         mute_callback_value_(false),
184         ssrc_(0),
185         error_(T::MediaChannel::ERROR_NONE) {
186   }
187
188   static void SetUpTestCase() {
189     talk_base::InitializeSSL();
190   }
191
192   static void TearDownTestCase() {
193     talk_base::CleanupSSL();
194   }
195
196   void CreateChannels(int flags1, int flags2) {
197     CreateChannels(new typename T::MediaChannel(NULL),
198                    new typename T::MediaChannel(NULL),
199                    flags1, flags2, talk_base::Thread::Current());
200   }
201   void CreateChannels(int flags) {
202      CreateChannels(new typename T::MediaChannel(NULL),
203                     new typename T::MediaChannel(NULL),
204                     flags, talk_base::Thread::Current());
205   }
206   void CreateChannels(int flags1, int flags2,
207                       talk_base::Thread* thread) {
208     CreateChannels(new typename T::MediaChannel(NULL),
209                    new typename T::MediaChannel(NULL),
210                    flags1, flags2, thread);
211   }
212   void CreateChannels(int flags,
213                       talk_base::Thread* thread) {
214     CreateChannels(new typename T::MediaChannel(NULL),
215                    new typename T::MediaChannel(NULL),
216                    flags, thread);
217   }
218   void CreateChannels(
219       typename T::MediaChannel* ch1, typename T::MediaChannel* ch2,
220       int flags1, int flags2, talk_base::Thread* thread) {
221     media_channel1_ = ch1;
222     media_channel2_ = ch2;
223     channel1_.reset(CreateChannel(thread, &media_engine_, ch1, &session1_,
224                                   (flags1 & RTCP) != 0));
225     channel2_.reset(CreateChannel(thread, &media_engine_, ch2, &session2_,
226                                   (flags2 & RTCP) != 0));
227     channel1_->SignalMediaMonitor.connect(
228         this, &ChannelTest<T>::OnMediaMonitor);
229     channel2_->SignalMediaMonitor.connect(
230         this, &ChannelTest<T>::OnMediaMonitor);
231     channel1_->SignalMediaError.connect(
232         this, &ChannelTest<T>::OnMediaChannelError);
233     channel2_->SignalMediaError.connect(
234         this, &ChannelTest<T>::OnMediaChannelError);
235     channel1_->SignalAutoMuted.connect(
236         this, &ChannelTest<T>::OnMediaMuted);
237     if ((flags1 & DTLS) && (flags2 & DTLS)) {
238       flags1 = (flags1 & ~SECURE);
239       flags2 = (flags2 & ~SECURE);
240     }
241     CreateContent(flags1, kPcmuCodec, kH264Codec,
242                   &local_media_content1_);
243     CreateContent(flags2, kPcmuCodec, kH264Codec,
244                   &local_media_content2_);
245     CopyContent(local_media_content1_, &remote_media_content1_);
246     CopyContent(local_media_content2_, &remote_media_content2_);
247
248     if (flags1 & DTLS) {
249       identity1_.reset(talk_base::SSLIdentity::Generate("session1"));
250       session1_.set_ssl_identity(identity1_.get());
251     }
252     if (flags2 & DTLS) {
253       identity2_.reset(talk_base::SSLIdentity::Generate("session2"));
254       session2_.set_ssl_identity(identity2_.get());
255     }
256
257     // Add stream information (SSRC) to the local content but not to the remote
258     // content. This means that we per default know the SSRC of what we send but
259     // not what we receive.
260     AddLegacyStreamInContent(kSsrc1, flags1, &local_media_content1_);
261     AddLegacyStreamInContent(kSsrc2, flags2, &local_media_content2_);
262
263     // If SSRC_MUX is used we also need to know the SSRC of the incoming stream.
264     if (flags1 & SSRC_MUX) {
265       AddLegacyStreamInContent(kSsrc1, flags1, &remote_media_content1_);
266     }
267     if (flags2 & SSRC_MUX) {
268       AddLegacyStreamInContent(kSsrc2, flags2, &remote_media_content2_);
269     }
270   }
271
272   void CreateChannels(
273       typename T::MediaChannel* ch1, typename T::MediaChannel* ch2,
274       int flags, talk_base::Thread* thread) {
275     media_channel1_ = ch1;
276     media_channel2_ = ch2;
277
278     channel1_.reset(CreateChannel(thread, &media_engine_, ch1, &session1_,
279                                   (flags & RTCP) != 0));
280     channel2_.reset(CreateChannel(thread, &media_engine_, ch2, &session1_,
281                                   (flags & RTCP) != 0));
282     channel1_->SignalMediaMonitor.connect(
283         this, &ChannelTest<T>::OnMediaMonitor);
284     channel2_->SignalMediaMonitor.connect(
285         this, &ChannelTest<T>::OnMediaMonitor);
286     channel2_->SignalMediaError.connect(
287         this, &ChannelTest<T>::OnMediaChannelError);
288     CreateContent(flags, kPcmuCodec, kH264Codec,
289                   &local_media_content1_);
290     CreateContent(flags, kPcmuCodec, kH264Codec,
291                   &local_media_content2_);
292     CopyContent(local_media_content1_, &remote_media_content1_);
293     CopyContent(local_media_content2_, &remote_media_content2_);
294     // Add stream information (SSRC) to the local content but not to the remote
295     // content. This means that we per default know the SSRC of what we send but
296     // not what we receive.
297     AddLegacyStreamInContent(kSsrc1, flags, &local_media_content1_);
298     AddLegacyStreamInContent(kSsrc2, flags, &local_media_content2_);
299
300     // If SSRC_MUX is used we also need to know the SSRC of the incoming stream.
301     if (flags & SSRC_MUX) {
302       AddLegacyStreamInContent(kSsrc1, flags, &remote_media_content1_);
303       AddLegacyStreamInContent(kSsrc2, flags, &remote_media_content2_);
304     }
305   }
306
307   typename T::Channel* CreateChannel(talk_base::Thread* thread,
308                                      cricket::MediaEngineInterface* engine,
309                                      typename T::MediaChannel* ch,
310                                      cricket::BaseSession* session,
311                                      bool rtcp) {
312     typename T::Channel* channel = new typename T::Channel(
313         thread, engine, ch, session, cricket::CN_AUDIO, rtcp);
314     if (!channel->Init()) {
315       delete channel;
316       channel = NULL;
317     }
318     return channel;
319   }
320
321   bool SendInitiate() {
322     bool result = channel1_->SetLocalContent(&local_media_content1_,
323                                              CA_OFFER, NULL);
324     if (result) {
325       channel1_->Enable(true);
326       result = channel2_->SetRemoteContent(&remote_media_content1_,
327                                            CA_OFFER, NULL);
328       if (result) {
329         session1_.Connect(&session2_);
330
331         result = channel2_->SetLocalContent(&local_media_content2_,
332                                             CA_ANSWER, NULL);
333       }
334     }
335     return result;
336   }
337
338   bool SendAccept() {
339     channel2_->Enable(true);
340     return channel1_->SetRemoteContent(&remote_media_content2_,
341                                        CA_ANSWER, NULL);
342   }
343
344   bool SendOffer() {
345     bool result = channel1_->SetLocalContent(&local_media_content1_,
346                                              CA_OFFER, NULL);
347     if (result) {
348       channel1_->Enable(true);
349       result = channel2_->SetRemoteContent(&remote_media_content1_,
350                                            CA_OFFER, NULL);
351     }
352     return result;
353   }
354
355   bool SendProvisionalAnswer() {
356     bool result = channel2_->SetLocalContent(&local_media_content2_,
357                                              CA_PRANSWER, NULL);
358     if (result) {
359       channel2_->Enable(true);
360       result = channel1_->SetRemoteContent(&remote_media_content2_,
361                                            CA_PRANSWER, NULL);
362       session1_.Connect(&session2_);
363     }
364     return result;
365   }
366
367   bool SendFinalAnswer() {
368     bool result = channel2_->SetLocalContent(&local_media_content2_,
369                                              CA_ANSWER, NULL);
370     if (result)
371       result = channel1_->SetRemoteContent(&remote_media_content2_,
372                                            CA_ANSWER, NULL);
373     return result;
374   }
375
376   bool SendTerminate() {
377     channel1_.reset();
378     channel2_.reset();
379     return true;
380   }
381
382   bool AddStream1(int id) {
383     return channel1_->AddRecvStream(cricket::StreamParams::CreateLegacy(id));
384   }
385   bool RemoveStream1(int id) {
386     return channel1_->RemoveRecvStream(id);
387   }
388
389   cricket::FakeTransport* GetTransport1() {
390     return session1_.GetTransport(channel1_->content_name());
391   }
392   cricket::FakeTransport* GetTransport2() {
393     return session2_.GetTransport(channel2_->content_name());
394   }
395
396   bool SendRtp1() {
397     return media_channel1_->SendRtp(rtp_packet_.c_str(),
398                                     static_cast<int>(rtp_packet_.size()));
399   }
400   bool SendRtp2() {
401     return media_channel2_->SendRtp(rtp_packet_.c_str(),
402                                     static_cast<int>(rtp_packet_.size()));
403   }
404   bool SendRtcp1() {
405     return media_channel1_->SendRtcp(rtcp_packet_.c_str(),
406                                      static_cast<int>(rtcp_packet_.size()));
407   }
408   bool SendRtcp2() {
409     return media_channel2_->SendRtcp(rtcp_packet_.c_str(),
410                                      static_cast<int>(rtcp_packet_.size()));
411   }
412   // Methods to send custom data.
413   bool SendCustomRtp1(uint32 ssrc, int sequence_number, int pl_type = -1) {
414     std::string data(CreateRtpData(ssrc, sequence_number, pl_type));
415     return media_channel1_->SendRtp(data.c_str(),
416                                     static_cast<int>(data.size()));
417   }
418   bool SendCustomRtp2(uint32 ssrc, int sequence_number, int pl_type = -1) {
419     std::string data(CreateRtpData(ssrc, sequence_number, pl_type));
420     return media_channel2_->SendRtp(data.c_str(),
421                                     static_cast<int>(data.size()));
422   }
423   bool SendCustomRtcp1(uint32 ssrc) {
424     std::string data(CreateRtcpData(ssrc));
425     return media_channel1_->SendRtcp(data.c_str(),
426                                      static_cast<int>(data.size()));
427   }
428   bool SendCustomRtcp2(uint32 ssrc) {
429     std::string data(CreateRtcpData(ssrc));
430     return media_channel2_->SendRtcp(data.c_str(),
431                                      static_cast<int>(data.size()));
432   }
433   bool CheckRtp1() {
434     return media_channel1_->CheckRtp(rtp_packet_.c_str(),
435                                      static_cast<int>(rtp_packet_.size()));
436   }
437   bool CheckRtp2() {
438     return media_channel2_->CheckRtp(rtp_packet_.c_str(),
439                                      static_cast<int>(rtp_packet_.size()));
440   }
441   bool CheckRtcp1() {
442     return media_channel1_->CheckRtcp(rtcp_packet_.c_str(),
443                                       static_cast<int>(rtcp_packet_.size()));
444   }
445   bool CheckRtcp2() {
446     return media_channel2_->CheckRtcp(rtcp_packet_.c_str(),
447                                       static_cast<int>(rtcp_packet_.size()));
448   }
449   // Methods to check custom data.
450   bool CheckCustomRtp1(uint32 ssrc, int sequence_number, int pl_type = -1 ) {
451     std::string data(CreateRtpData(ssrc, sequence_number, pl_type));
452     return media_channel1_->CheckRtp(data.c_str(),
453                                      static_cast<int>(data.size()));
454   }
455   bool CheckCustomRtp2(uint32 ssrc, int sequence_number, int pl_type = -1) {
456     std::string data(CreateRtpData(ssrc, sequence_number, pl_type));
457     return media_channel2_->CheckRtp(data.c_str(),
458                                      static_cast<int>(data.size()));
459   }
460   bool CheckCustomRtcp1(uint32 ssrc) {
461     std::string data(CreateRtcpData(ssrc));
462     return media_channel1_->CheckRtcp(data.c_str(),
463                                       static_cast<int>(data.size()));
464   }
465   bool CheckCustomRtcp2(uint32 ssrc) {
466     std::string data(CreateRtcpData(ssrc));
467     return media_channel2_->CheckRtcp(data.c_str(),
468                                       static_cast<int>(data.size()));
469   }
470   std::string CreateRtpData(uint32 ssrc, int sequence_number, int pl_type) {
471     std::string data(rtp_packet_);
472     // Set SSRC in the rtp packet copy.
473     talk_base::SetBE32(const_cast<char*>(data.c_str()) + 8, ssrc);
474     talk_base::SetBE16(const_cast<char*>(data.c_str()) + 2, sequence_number);
475     if (pl_type >= 0) {
476       talk_base::Set8(const_cast<char*>(data.c_str()), 1, pl_type);
477     }
478     return data;
479   }
480   std::string CreateRtcpData(uint32 ssrc) {
481     std::string data(rtcp_packet_);
482     // Set SSRC in the rtcp packet copy.
483     talk_base::SetBE32(const_cast<char*>(data.c_str()) + 4, ssrc);
484     return data;
485   }
486
487   bool CheckNoRtp1() {
488     return media_channel1_->CheckNoRtp();
489   }
490   bool CheckNoRtp2() {
491     return media_channel2_->CheckNoRtp();
492   }
493   bool CheckNoRtcp1() {
494     return media_channel1_->CheckNoRtcp();
495   }
496   bool CheckNoRtcp2() {
497     return media_channel2_->CheckNoRtcp();
498   }
499
500   void CreateContent(int flags,
501                      const cricket::AudioCodec& audio_codec,
502                      const cricket::VideoCodec& video_codec,
503                      typename T::Content* content) {
504     // overridden in specialized classes
505   }
506   void CopyContent(const typename T::Content& source,
507                    typename T::Content* content) {
508     // overridden in specialized classes
509   }
510
511   // Creates a cricket::SessionDescription with one MediaContent and one stream.
512   // kPcmuCodec is used as audio codec and kH264Codec is used as video codec.
513   cricket::SessionDescription* CreateSessionDescriptionWithStream(uint32 ssrc) {
514      typename T::Content content;
515      cricket::SessionDescription* sdesc = new cricket::SessionDescription();
516      CreateContent(SECURE, kPcmuCodec, kH264Codec, &content);
517      AddLegacyStreamInContent(ssrc, 0, &content);
518      sdesc->AddContent("DUMMY_CONTENT_NAME",
519                        cricket::NS_JINGLE_RTP, content.Copy());
520      return sdesc;
521   }
522
523   class CallThread : public talk_base::SignalThread {
524    public:
525     typedef bool (ChannelTest<T>::*Method)();
526     CallThread(ChannelTest<T>* obj, Method method, bool* result)
527         : obj_(obj),
528           method_(method),
529           result_(result) {
530       *result = false;
531     }
532     virtual void DoWork() {
533       bool result = (*obj_.*method_)();
534       if (result_) {
535         *result_ = result;
536       }
537     }
538    private:
539     ChannelTest<T>* obj_;
540     Method method_;
541     bool* result_;
542   };
543   void CallOnThread(typename CallThread::Method method, bool* result) {
544     CallThread* thread = new CallThread(this, method, result);
545     thread->Start();
546     thread->Release();
547   }
548
549   void CallOnThreadAndWaitForDone(typename CallThread::Method method,
550                                   bool* result) {
551     CallThread* thread = new CallThread(this, method, result);
552     thread->Start();
553     thread->Destroy(true);
554   }
555
556   bool CodecMatches(const typename T::Codec& c1, const typename T::Codec& c2) {
557     return false;  // overridden in specialized classes
558   }
559
560   void OnMediaMonitor(typename T::Channel* channel,
561                       const typename T::MediaInfo& info) {
562     if (channel == channel1_.get()) {
563       media_info_callbacks1_++;
564     } else if (channel == channel2_.get()) {
565       media_info_callbacks2_++;
566     }
567   }
568
569   void OnMediaChannelError(typename T::Channel* channel,
570                            uint32 ssrc,
571                            typename T::MediaChannel::Error error) {
572     ssrc_ = ssrc;
573     error_ = error;
574   }
575
576   void OnMediaMuted(cricket::BaseChannel* channel, bool muted) {
577     mute_callback_recved_ = true;
578     mute_callback_value_ = muted;
579   }
580
581   void AddLegacyStreamInContent(uint32 ssrc, int flags,
582                         typename T::Content* content) {
583     // Base implementation.
584   }
585
586   // Tests that can be used by derived classes.
587
588   // Basic sanity check.
589   void TestInit() {
590     CreateChannels(0, 0);
591     EXPECT_FALSE(channel1_->secure());
592     EXPECT_FALSE(media_channel1_->sending());
593     EXPECT_FALSE(media_channel1_->playout());
594     EXPECT_TRUE(media_channel1_->codecs().empty());
595     EXPECT_TRUE(media_channel1_->recv_streams().empty());
596     EXPECT_TRUE(media_channel1_->rtp_packets().empty());
597     EXPECT_TRUE(media_channel1_->rtcp_packets().empty());
598   }
599
600   // Test that SetLocalContent and SetRemoteContent properly configure
601   // the codecs.
602   void TestSetContents() {
603     CreateChannels(0, 0);
604     typename T::Content content;
605     CreateContent(0, kPcmuCodec, kH264Codec, &content);
606     EXPECT_TRUE(channel1_->SetLocalContent(&content, CA_OFFER, NULL));
607     EXPECT_EQ(0U, media_channel1_->codecs().size());
608     EXPECT_TRUE(channel1_->SetRemoteContent(&content, CA_ANSWER, NULL));
609     ASSERT_EQ(1U, media_channel1_->codecs().size());
610     EXPECT_TRUE(CodecMatches(content.codecs()[0],
611                              media_channel1_->codecs()[0]));
612   }
613
614   // Test that SetLocalContent and SetRemoteContent properly deals
615   // with an empty offer.
616   void TestSetContentsNullOffer() {
617     CreateChannels(0, 0);
618     typename T::Content content;
619     EXPECT_TRUE(channel1_->SetLocalContent(&content, CA_OFFER, NULL));
620     CreateContent(0, kPcmuCodec, kH264Codec, &content);
621     EXPECT_EQ(0U, media_channel1_->codecs().size());
622     EXPECT_TRUE(channel1_->SetRemoteContent(&content, CA_ANSWER, NULL));
623     ASSERT_EQ(1U, media_channel1_->codecs().size());
624     EXPECT_TRUE(CodecMatches(content.codecs()[0],
625                              media_channel1_->codecs()[0]));
626   }
627
628   // Test that SetLocalContent and SetRemoteContent properly set RTCP
629   // mux.
630   void TestSetContentsRtcpMux() {
631     CreateChannels(RTCP, RTCP);
632     EXPECT_TRUE(channel1_->rtcp_transport_channel() != NULL);
633     EXPECT_TRUE(channel2_->rtcp_transport_channel() != NULL);
634     typename T::Content content;
635     CreateContent(0, kPcmuCodec, kH264Codec, &content);
636     // Both sides agree on mux. Should no longer be a separate RTCP channel.
637     content.set_rtcp_mux(true);
638     EXPECT_TRUE(channel1_->SetLocalContent(&content, CA_OFFER, NULL));
639     EXPECT_TRUE(channel1_->SetRemoteContent(&content, CA_ANSWER, NULL));
640     EXPECT_TRUE(channel1_->rtcp_transport_channel() == NULL);
641     // Only initiator supports mux. Should still have a separate RTCP channel.
642     EXPECT_TRUE(channel2_->SetLocalContent(&content, CA_OFFER, NULL));
643     content.set_rtcp_mux(false);
644     EXPECT_TRUE(channel2_->SetRemoteContent(&content, CA_ANSWER, NULL));
645     EXPECT_TRUE(channel2_->rtcp_transport_channel() != NULL);
646   }
647
648   // Test that SetLocalContent and SetRemoteContent properly set RTCP
649   // mux when a provisional answer is received.
650   void TestSetContentsRtcpMuxWithPrAnswer() {
651     CreateChannels(RTCP, RTCP);
652     EXPECT_TRUE(channel1_->rtcp_transport_channel() != NULL);
653     EXPECT_TRUE(channel2_->rtcp_transport_channel() != NULL);
654     typename T::Content content;
655     CreateContent(0, kPcmuCodec, kH264Codec, &content);
656     content.set_rtcp_mux(true);
657     EXPECT_TRUE(channel1_->SetLocalContent(&content, CA_OFFER, NULL));
658     EXPECT_TRUE(channel1_->SetRemoteContent(&content, CA_PRANSWER, NULL));
659     EXPECT_TRUE(channel1_->rtcp_transport_channel() != NULL);
660     EXPECT_TRUE(channel1_->SetRemoteContent(&content, CA_ANSWER, NULL));
661     // Both sides agree on mux. Should no longer be a separate RTCP channel.
662     EXPECT_TRUE(channel1_->rtcp_transport_channel() == NULL);
663     // Only initiator supports mux. Should still have a separate RTCP channel.
664     EXPECT_TRUE(channel2_->SetLocalContent(&content, CA_OFFER, NULL));
665     content.set_rtcp_mux(false);
666     EXPECT_TRUE(channel2_->SetRemoteContent(&content, CA_PRANSWER, NULL));
667     EXPECT_TRUE(channel2_->SetRemoteContent(&content, CA_ANSWER, NULL));
668     EXPECT_TRUE(channel2_->rtcp_transport_channel() != NULL);
669   }
670
671   // Test that SetLocalContent and SetRemoteContent properly set
672   // video options to the media channel.
673   void TestSetContentsVideoOptions() {
674     CreateChannels(0, 0);
675     typename T::Content content;
676     CreateContent(0, kPcmuCodec, kH264Codec, &content);
677     content.set_buffered_mode_latency(101);
678     EXPECT_TRUE(channel1_->SetLocalContent(&content, CA_OFFER, NULL));
679     EXPECT_EQ(0U, media_channel1_->codecs().size());
680     cricket::VideoOptions options;
681     ASSERT_TRUE(media_channel1_->GetOptions(&options));
682     int latency = 0;
683     EXPECT_TRUE(options.buffered_mode_latency.Get(&latency));
684     EXPECT_EQ(101, latency);
685     content.set_buffered_mode_latency(102);
686     EXPECT_TRUE(channel1_->SetRemoteContent(&content, CA_ANSWER, NULL));
687     ASSERT_EQ(1U, media_channel1_->codecs().size());
688     EXPECT_TRUE(CodecMatches(content.codecs()[0],
689                              media_channel1_->codecs()[0]));
690     ASSERT_TRUE(media_channel1_->GetOptions(&options));
691     EXPECT_TRUE(options.buffered_mode_latency.Get(&latency));
692     EXPECT_EQ(102, latency);
693   }
694
695   // Test that SetRemoteContent properly deals with a content update.
696   void TestSetRemoteContentUpdate() {
697     CreateChannels(0, 0);
698     typename T::Content content;
699     CreateContent(RTCP | RTCP_MUX | SECURE,
700                   kPcmuCodec, kH264Codec,
701                   &content);
702     EXPECT_EQ(0U, media_channel1_->codecs().size());
703     EXPECT_TRUE(channel1_->SetLocalContent(&content, CA_OFFER, NULL));
704     EXPECT_TRUE(channel1_->SetRemoteContent(&content, CA_ANSWER, NULL));
705     ASSERT_EQ(1U, media_channel1_->codecs().size());
706     EXPECT_TRUE(CodecMatches(content.codecs()[0],
707                              media_channel1_->codecs()[0]));
708     // Now update with other codecs.
709     typename T::Content update_content;
710     update_content.set_partial(true);
711     CreateContent(0, kIsacCodec, kH264SvcCodec,
712                   &update_content);
713     EXPECT_TRUE(channel1_->SetRemoteContent(&update_content, CA_UPDATE, NULL));
714     ASSERT_EQ(1U, media_channel1_->codecs().size());
715     EXPECT_TRUE(CodecMatches(update_content.codecs()[0],
716                              media_channel1_->codecs()[0]));
717     // Now update without any codecs. This is ignored.
718     typename T::Content empty_content;
719     empty_content.set_partial(true);
720     EXPECT_TRUE(channel1_->SetRemoteContent(&empty_content, CA_UPDATE, NULL));
721     ASSERT_EQ(1U, media_channel1_->codecs().size());
722     EXPECT_TRUE(CodecMatches(update_content.codecs()[0],
723                              media_channel1_->codecs()[0]));
724   }
725
726   // Test that Add/RemoveStream properly forward to the media channel.
727   void TestStreams() {
728     CreateChannels(0, 0);
729     EXPECT_TRUE(AddStream1(1));
730     EXPECT_TRUE(AddStream1(2));
731     EXPECT_EQ(2U, media_channel1_->recv_streams().size());
732     EXPECT_TRUE(RemoveStream1(2));
733     EXPECT_EQ(1U, media_channel1_->recv_streams().size());
734     EXPECT_TRUE(RemoveStream1(1));
735     EXPECT_EQ(0U, media_channel1_->recv_streams().size());
736   }
737
738   // Test that SetLocalContent properly handles adding and removing StreamParams
739   // to the local content description.
740   // This test uses the CA_UPDATE action that don't require a full
741   // MediaContentDescription to do an update.
742   void TestUpdateStreamsInLocalContent() {
743     cricket::StreamParams stream1;
744     stream1.groupid = "group1";
745     stream1.id = "stream1";
746     stream1.ssrcs.push_back(kSsrc1);
747     stream1.cname = "stream1_cname";
748
749     cricket::StreamParams stream2;
750     stream2.groupid = "group2";
751     stream2.id = "stream2";
752     stream2.ssrcs.push_back(kSsrc2);
753     stream2.cname = "stream2_cname";
754
755     cricket::StreamParams stream3;
756     stream3.groupid = "group3";
757     stream3.id = "stream3";
758     stream3.ssrcs.push_back(kSsrc3);
759     stream3.cname = "stream3_cname";
760
761     CreateChannels(0, 0);
762     typename T::Content content1;
763     CreateContent(0, kPcmuCodec, kH264Codec, &content1);
764     content1.AddStream(stream1);
765     EXPECT_EQ(0u, media_channel1_->send_streams().size());
766     EXPECT_TRUE(channel1_->SetLocalContent(&content1, CA_OFFER, NULL));
767
768     ASSERT_EQ(1u, media_channel1_->send_streams().size());
769     EXPECT_EQ(stream1, media_channel1_->send_streams()[0]);
770
771     // Update the local streams by adding another sending stream.
772     // Use a partial updated session description.
773     typename T::Content content2;
774     content2.AddStream(stream2);
775     content2.AddStream(stream3);
776     content2.set_partial(true);
777     EXPECT_TRUE(channel1_->SetLocalContent(&content2, CA_UPDATE, NULL));
778     ASSERT_EQ(3u, media_channel1_->send_streams().size());
779     EXPECT_EQ(stream1, media_channel1_->send_streams()[0]);
780     EXPECT_EQ(stream2, media_channel1_->send_streams()[1]);
781     EXPECT_EQ(stream3, media_channel1_->send_streams()[2]);
782
783     // Update the local streams by removing the first sending stream.
784     // This is done by removing all SSRCS for this particular stream.
785     typename T::Content content3;
786     stream1.ssrcs.clear();
787     content3.AddStream(stream1);
788     content3.set_partial(true);
789     EXPECT_TRUE(channel1_->SetLocalContent(&content3, CA_UPDATE, NULL));
790     ASSERT_EQ(2u, media_channel1_->send_streams().size());
791     EXPECT_EQ(stream2, media_channel1_->send_streams()[0]);
792     EXPECT_EQ(stream3, media_channel1_->send_streams()[1]);
793
794     // Update the local streams with a stream that does not change.
795     // THe update is ignored.
796     typename T::Content content4;
797     content4.AddStream(stream2);
798     content4.set_partial(true);
799     EXPECT_TRUE(channel1_->SetLocalContent(&content4, CA_UPDATE, NULL));
800     ASSERT_EQ(2u, media_channel1_->send_streams().size());
801     EXPECT_EQ(stream2, media_channel1_->send_streams()[0]);
802     EXPECT_EQ(stream3, media_channel1_->send_streams()[1]);
803   }
804
805   // Test that SetRemoteContent properly handles adding and removing
806   // StreamParams to the remote content description.
807   // This test uses the CA_UPDATE action that don't require a full
808   // MediaContentDescription to do an update.
809   void TestUpdateStreamsInRemoteContent() {
810     cricket::StreamParams stream1;
811     stream1.id = "Stream1";
812     stream1.groupid = "1";
813     stream1.ssrcs.push_back(kSsrc1);
814     stream1.cname = "stream1_cname";
815
816     cricket::StreamParams stream2;
817     stream2.id = "Stream2";
818     stream2.groupid = "2";
819     stream2.ssrcs.push_back(kSsrc2);
820     stream2.cname = "stream2_cname";
821
822     cricket::StreamParams stream3;
823     stream3.id = "Stream3";
824     stream3.groupid = "3";
825     stream3.ssrcs.push_back(kSsrc3);
826     stream3.cname = "stream3_cname";
827
828     CreateChannels(0, 0);
829     typename T::Content content1;
830     CreateContent(0, kPcmuCodec, kH264Codec, &content1);
831     content1.AddStream(stream1);
832     EXPECT_EQ(0u, media_channel1_->recv_streams().size());
833     EXPECT_TRUE(channel1_->SetRemoteContent(&content1, CA_OFFER, NULL));
834
835     ASSERT_EQ(1u, media_channel1_->codecs().size());
836     ASSERT_EQ(1u, media_channel1_->recv_streams().size());
837     EXPECT_EQ(stream1, media_channel1_->recv_streams()[0]);
838
839     // Update the remote streams by adding another sending stream.
840     // Use a partial updated session description.
841     typename T::Content content2;
842     content2.AddStream(stream2);
843     content2.AddStream(stream3);
844     content2.set_partial(true);
845     EXPECT_TRUE(channel1_->SetRemoteContent(&content2, CA_UPDATE, NULL));
846     ASSERT_EQ(3u, media_channel1_->recv_streams().size());
847     EXPECT_EQ(stream1, media_channel1_->recv_streams()[0]);
848     EXPECT_EQ(stream2, media_channel1_->recv_streams()[1]);
849     EXPECT_EQ(stream3, media_channel1_->recv_streams()[2]);
850
851     // Update the remote streams by removing the first stream.
852     // This is done by removing all SSRCS for this particular stream.
853     typename T::Content content3;
854     stream1.ssrcs.clear();
855     content3.AddStream(stream1);
856     content3.set_partial(true);
857     EXPECT_TRUE(channel1_->SetRemoteContent(&content3, CA_UPDATE, NULL));
858     ASSERT_EQ(2u, media_channel1_->recv_streams().size());
859     EXPECT_EQ(stream2, media_channel1_->recv_streams()[0]);
860     EXPECT_EQ(stream3, media_channel1_->recv_streams()[1]);
861
862     // Update the remote streams with a stream that does not change.
863     // The update is ignored.
864     typename T::Content content4;
865     content4.AddStream(stream2);
866     content4.set_partial(true);
867     EXPECT_TRUE(channel1_->SetRemoteContent(&content4, CA_UPDATE, NULL));
868     ASSERT_EQ(2u, media_channel1_->recv_streams().size());
869     EXPECT_EQ(stream2, media_channel1_->recv_streams()[0]);
870     EXPECT_EQ(stream3, media_channel1_->recv_streams()[1]);
871   }
872
873   // Test that SetLocalContent and SetRemoteContent properly
874   // handles adding and removing StreamParams when the action is a full
875   // CA_OFFER / CA_ANSWER.
876   void TestChangeStreamParamsInContent() {
877     cricket::StreamParams stream1;
878     stream1.groupid = "group1";
879     stream1.id = "stream1";
880     stream1.ssrcs.push_back(kSsrc1);
881     stream1.cname = "stream1_cname";
882
883     cricket::StreamParams stream2;
884     stream2.groupid = "group1";
885     stream2.id = "stream2";
886     stream2.ssrcs.push_back(kSsrc2);
887     stream2.cname = "stream2_cname";
888
889     // Setup a call where channel 1 send |stream1| to channel 2.
890     CreateChannels(0, 0);
891     typename T::Content content1;
892     CreateContent(0, kPcmuCodec, kH264Codec, &content1);
893     content1.AddStream(stream1);
894     EXPECT_TRUE(channel1_->SetLocalContent(&content1, CA_OFFER, NULL));
895     EXPECT_TRUE(channel1_->Enable(true));
896     EXPECT_EQ(1u, media_channel1_->send_streams().size());
897
898     EXPECT_TRUE(channel2_->SetRemoteContent(&content1, CA_OFFER, NULL));
899     EXPECT_EQ(1u, media_channel2_->recv_streams().size());
900     session1_.Connect(&session2_);
901
902     // Channel 2 do not send anything.
903     typename T::Content content2;
904     CreateContent(0, kPcmuCodec, kH264Codec, &content2);
905     EXPECT_TRUE(channel1_->SetRemoteContent(&content2, CA_ANSWER, NULL));
906     EXPECT_EQ(0u, media_channel1_->recv_streams().size());
907     EXPECT_TRUE(channel2_->SetLocalContent(&content2, CA_ANSWER, NULL));
908     EXPECT_TRUE(channel2_->Enable(true));
909     EXPECT_EQ(0u, media_channel2_->send_streams().size());
910
911     EXPECT_TRUE(SendCustomRtp1(kSsrc1, 0));
912     EXPECT_TRUE(CheckCustomRtp2(kSsrc1, 0));
913
914     // Let channel 2 update the content by sending |stream2| and enable SRTP.
915     typename T::Content content3;
916     CreateContent(SECURE, kPcmuCodec, kH264Codec, &content3);
917     content3.AddStream(stream2);
918     EXPECT_TRUE(channel2_->SetLocalContent(&content3, CA_OFFER, NULL));
919     ASSERT_EQ(1u, media_channel2_->send_streams().size());
920     EXPECT_EQ(stream2, media_channel2_->send_streams()[0]);
921
922     EXPECT_TRUE(channel1_->SetRemoteContent(&content3, CA_OFFER, NULL));
923     ASSERT_EQ(1u, media_channel1_->recv_streams().size());
924     EXPECT_EQ(stream2, media_channel1_->recv_streams()[0]);
925
926     // Channel 1 replies but stop sending stream1.
927     typename T::Content content4;
928     CreateContent(SECURE, kPcmuCodec, kH264Codec, &content4);
929     EXPECT_TRUE(channel1_->SetLocalContent(&content4, CA_ANSWER, NULL));
930     EXPECT_EQ(0u, media_channel1_->send_streams().size());
931
932     EXPECT_TRUE(channel2_->SetRemoteContent(&content4, CA_ANSWER, NULL));
933     EXPECT_EQ(0u, media_channel2_->recv_streams().size());
934
935     EXPECT_TRUE(channel1_->secure());
936     EXPECT_TRUE(channel2_->secure());
937     EXPECT_TRUE(SendCustomRtp2(kSsrc2, 0));
938     EXPECT_TRUE(CheckCustomRtp1(kSsrc2, 0));
939   }
940
941   // Test that we only start playout and sending at the right times.
942   void TestPlayoutAndSendingStates() {
943     CreateChannels(0, 0);
944     EXPECT_FALSE(media_channel1_->playout());
945     EXPECT_FALSE(media_channel1_->sending());
946     EXPECT_FALSE(media_channel2_->playout());
947     EXPECT_FALSE(media_channel2_->sending());
948     EXPECT_TRUE(channel1_->Enable(true));
949     EXPECT_FALSE(media_channel1_->playout());
950     EXPECT_FALSE(media_channel1_->sending());
951     EXPECT_TRUE(channel1_->SetLocalContent(&local_media_content1_,
952                                            CA_OFFER, NULL));
953     EXPECT_TRUE(media_channel1_->playout());
954     EXPECT_FALSE(media_channel1_->sending());
955     EXPECT_TRUE(channel2_->SetRemoteContent(&local_media_content1_,
956                                             CA_OFFER, NULL));
957     EXPECT_FALSE(media_channel2_->playout());
958     EXPECT_FALSE(media_channel2_->sending());
959     EXPECT_TRUE(channel2_->SetLocalContent(&local_media_content2_,
960                                            CA_ANSWER, NULL));
961     EXPECT_FALSE(media_channel2_->playout());
962     EXPECT_FALSE(media_channel2_->sending());
963     session1_.Connect(&session2_);
964     EXPECT_TRUE(media_channel1_->playout());
965     EXPECT_FALSE(media_channel1_->sending());
966     EXPECT_FALSE(media_channel2_->playout());
967     EXPECT_FALSE(media_channel2_->sending());
968     EXPECT_TRUE(channel2_->Enable(true));
969     EXPECT_TRUE(media_channel2_->playout());
970     EXPECT_TRUE(media_channel2_->sending());
971     EXPECT_TRUE(channel1_->SetRemoteContent(&local_media_content2_,
972                                             CA_ANSWER, NULL));
973     EXPECT_TRUE(media_channel1_->playout());
974     EXPECT_TRUE(media_channel1_->sending());
975   }
976
977   void TestMuteStream() {
978     CreateChannels(0, 0);
979     // Test that we can Mute the default channel even though the sending SSRC is
980     // unknown.
981     EXPECT_FALSE(media_channel1_->IsStreamMuted(0));
982     EXPECT_TRUE(channel1_->MuteStream(0, true));
983     EXPECT_TRUE(media_channel1_->IsStreamMuted(0));
984     EXPECT_TRUE(channel1_->MuteStream(0, false));
985     EXPECT_FALSE(media_channel1_->IsStreamMuted(0));
986
987     // Test that we can not mute an unknown SSRC.
988     EXPECT_FALSE(channel1_->MuteStream(kSsrc1, true));
989
990     SendInitiate();
991     // After the local session description has been set, we can mute a stream
992     // with its SSRC.
993     EXPECT_TRUE(channel1_->MuteStream(kSsrc1, true));
994     EXPECT_TRUE(media_channel1_->IsStreamMuted(kSsrc1));
995     EXPECT_TRUE(channel1_->MuteStream(kSsrc1, false));
996     EXPECT_FALSE(media_channel1_->IsStreamMuted(kSsrc1));
997   }
998
999   // Test that changing the MediaContentDirection in the local and remote
1000   // session description start playout and sending at the right time.
1001   void TestMediaContentDirection() {
1002     CreateChannels(0, 0);
1003     typename T::Content content1;
1004     CreateContent(0, kPcmuCodec, kH264Codec, &content1);
1005     typename T::Content content2;
1006     CreateContent(0, kPcmuCodec, kH264Codec, &content2);
1007     // Set |content2| to be InActive.
1008     content2.set_direction(cricket::MD_INACTIVE);
1009
1010     EXPECT_TRUE(channel1_->Enable(true));
1011     EXPECT_TRUE(channel2_->Enable(true));
1012     EXPECT_FALSE(media_channel1_->playout());
1013     EXPECT_FALSE(media_channel1_->sending());
1014     EXPECT_FALSE(media_channel2_->playout());
1015     EXPECT_FALSE(media_channel2_->sending());
1016
1017     EXPECT_TRUE(channel1_->SetLocalContent(&content1, CA_OFFER, NULL));
1018     EXPECT_TRUE(channel2_->SetRemoteContent(&content1, CA_OFFER, NULL));
1019     EXPECT_TRUE(channel2_->SetLocalContent(&content2, CA_PRANSWER, NULL));
1020     EXPECT_TRUE(channel1_->SetRemoteContent(&content2, CA_PRANSWER, NULL));
1021     session1_.Connect(&session2_);
1022
1023     EXPECT_TRUE(media_channel1_->playout());
1024     EXPECT_FALSE(media_channel1_->sending());  // remote InActive
1025     EXPECT_FALSE(media_channel2_->playout());  // local InActive
1026     EXPECT_FALSE(media_channel2_->sending());  // local InActive
1027
1028     // Update |content2| to be RecvOnly.
1029     content2.set_direction(cricket::MD_RECVONLY);
1030     EXPECT_TRUE(channel2_->SetLocalContent(&content2, CA_PRANSWER, NULL));
1031     EXPECT_TRUE(channel1_->SetRemoteContent(&content2, CA_PRANSWER, NULL));
1032
1033     EXPECT_TRUE(media_channel1_->playout());
1034     EXPECT_TRUE(media_channel1_->sending());
1035     EXPECT_TRUE(media_channel2_->playout());  // local RecvOnly
1036     EXPECT_FALSE(media_channel2_->sending());  // local RecvOnly
1037
1038     // Update |content2| to be SendRecv.
1039     content2.set_direction(cricket::MD_SENDRECV);
1040     EXPECT_TRUE(channel2_->SetLocalContent(&content2, CA_ANSWER, NULL));
1041     EXPECT_TRUE(channel1_->SetRemoteContent(&content2, CA_ANSWER, NULL));
1042
1043     EXPECT_TRUE(media_channel1_->playout());
1044     EXPECT_TRUE(media_channel1_->sending());
1045     EXPECT_TRUE(media_channel2_->playout());
1046     EXPECT_TRUE(media_channel2_->sending());
1047   }
1048
1049   // Test setting up a call.
1050   void TestCallSetup() {
1051     CreateChannels(0, 0);
1052     EXPECT_FALSE(channel1_->secure());
1053     EXPECT_TRUE(SendInitiate());
1054     EXPECT_TRUE(media_channel1_->playout());
1055     EXPECT_FALSE(media_channel1_->sending());
1056     EXPECT_TRUE(SendAccept());
1057     EXPECT_FALSE(channel1_->secure());
1058     EXPECT_TRUE(media_channel1_->sending());
1059     EXPECT_EQ(1U, media_channel1_->codecs().size());
1060     EXPECT_TRUE(media_channel2_->playout());
1061     EXPECT_TRUE(media_channel2_->sending());
1062     EXPECT_EQ(1U, media_channel2_->codecs().size());
1063   }
1064
1065   // Test that we don't crash if packets are sent during call teardown
1066   // when RTCP mux is enabled. This is a regression test against a specific
1067   // race condition that would only occur when a RTCP packet was sent during
1068   // teardown of a channel on which RTCP mux was enabled.
1069   void TestCallTeardownRtcpMux() {
1070     class LastWordMediaChannel : public T::MediaChannel {
1071      public:
1072       LastWordMediaChannel() : T::MediaChannel(NULL) {}
1073       ~LastWordMediaChannel() {
1074         T::MediaChannel::SendRtp(kPcmuFrame, sizeof(kPcmuFrame));
1075         T::MediaChannel::SendRtcp(kRtcpReport, sizeof(kRtcpReport));
1076       }
1077     };
1078     CreateChannels(new LastWordMediaChannel(), new LastWordMediaChannel(),
1079                    RTCP | RTCP_MUX, RTCP | RTCP_MUX,
1080                    talk_base::Thread::Current());
1081     EXPECT_TRUE(SendInitiate());
1082     EXPECT_TRUE(SendAccept());
1083     EXPECT_TRUE(SendTerminate());
1084   }
1085
1086   // Send voice RTP data to the other side and ensure it gets there.
1087   void SendRtpToRtp() {
1088     CreateChannels(0, 0);
1089     EXPECT_TRUE(SendInitiate());
1090     EXPECT_TRUE(SendAccept());
1091     EXPECT_EQ(1U, GetTransport1()->channels().size());
1092     EXPECT_EQ(1U, GetTransport2()->channels().size());
1093     EXPECT_TRUE(SendRtp1());
1094     EXPECT_TRUE(SendRtp2());
1095     EXPECT_TRUE(CheckRtp1());
1096     EXPECT_TRUE(CheckRtp2());
1097     EXPECT_TRUE(CheckNoRtp1());
1098     EXPECT_TRUE(CheckNoRtp2());
1099   }
1100
1101   // Check that RTCP is not transmitted if both sides don't support RTCP.
1102   void SendNoRtcpToNoRtcp() {
1103     CreateChannels(0, 0);
1104     EXPECT_TRUE(SendInitiate());
1105     EXPECT_TRUE(SendAccept());
1106     EXPECT_EQ(1U, GetTransport1()->channels().size());
1107     EXPECT_EQ(1U, GetTransport2()->channels().size());
1108     EXPECT_FALSE(SendRtcp1());
1109     EXPECT_FALSE(SendRtcp2());
1110     EXPECT_TRUE(CheckNoRtcp1());
1111     EXPECT_TRUE(CheckNoRtcp2());
1112   }
1113
1114   // Check that RTCP is not transmitted if the callee doesn't support RTCP.
1115   void SendNoRtcpToRtcp() {
1116     CreateChannels(0, RTCP);
1117     EXPECT_TRUE(SendInitiate());
1118     EXPECT_TRUE(SendAccept());
1119     EXPECT_EQ(1U, GetTransport1()->channels().size());
1120     EXPECT_EQ(2U, GetTransport2()->channels().size());
1121     EXPECT_FALSE(SendRtcp1());
1122     EXPECT_FALSE(SendRtcp2());
1123     EXPECT_TRUE(CheckNoRtcp1());
1124     EXPECT_TRUE(CheckNoRtcp2());
1125   }
1126
1127   // Check that RTCP is not transmitted if the caller doesn't support RTCP.
1128   void SendRtcpToNoRtcp() {
1129     CreateChannels(RTCP, 0);
1130     EXPECT_TRUE(SendInitiate());
1131     EXPECT_TRUE(SendAccept());
1132     EXPECT_EQ(2U, GetTransport1()->channels().size());
1133     EXPECT_EQ(1U, GetTransport2()->channels().size());
1134     EXPECT_FALSE(SendRtcp1());
1135     EXPECT_FALSE(SendRtcp2());
1136     EXPECT_TRUE(CheckNoRtcp1());
1137     EXPECT_TRUE(CheckNoRtcp2());
1138   }
1139
1140   // Check that RTCP is transmitted if both sides support RTCP.
1141   void SendRtcpToRtcp() {
1142     CreateChannels(RTCP, RTCP);
1143     EXPECT_TRUE(SendInitiate());
1144     EXPECT_TRUE(SendAccept());
1145     EXPECT_EQ(2U, GetTransport1()->channels().size());
1146     EXPECT_EQ(2U, GetTransport2()->channels().size());
1147     EXPECT_TRUE(SendRtcp1());
1148     EXPECT_TRUE(SendRtcp2());
1149     EXPECT_TRUE(CheckRtcp1());
1150     EXPECT_TRUE(CheckRtcp2());
1151     EXPECT_TRUE(CheckNoRtcp1());
1152     EXPECT_TRUE(CheckNoRtcp2());
1153   }
1154
1155   // Check that RTCP is transmitted if only the initiator supports mux.
1156   void SendRtcpMuxToRtcp() {
1157     CreateChannels(RTCP | RTCP_MUX, RTCP);
1158     EXPECT_TRUE(SendInitiate());
1159     EXPECT_TRUE(SendAccept());
1160     EXPECT_EQ(2U, GetTransport1()->channels().size());
1161     EXPECT_EQ(2U, GetTransport2()->channels().size());
1162     EXPECT_TRUE(SendRtcp1());
1163     EXPECT_TRUE(SendRtcp2());
1164     EXPECT_TRUE(CheckRtcp1());
1165     EXPECT_TRUE(CheckRtcp2());
1166     EXPECT_TRUE(CheckNoRtcp1());
1167     EXPECT_TRUE(CheckNoRtcp2());
1168   }
1169
1170   // Check that RTP and RTCP are transmitted ok when both sides support mux.
1171   void SendRtcpMuxToRtcpMux() {
1172     CreateChannels(RTCP | RTCP_MUX, RTCP | RTCP_MUX);
1173     EXPECT_TRUE(SendInitiate());
1174     EXPECT_EQ(2U, GetTransport1()->channels().size());
1175     EXPECT_EQ(1U, GetTransport2()->channels().size());
1176     EXPECT_TRUE(SendAccept());
1177     EXPECT_EQ(1U, GetTransport1()->channels().size());
1178     EXPECT_TRUE(SendRtp1());
1179     EXPECT_TRUE(SendRtp2());
1180     EXPECT_TRUE(SendRtcp1());
1181     EXPECT_TRUE(SendRtcp2());
1182     EXPECT_TRUE(CheckRtp1());
1183     EXPECT_TRUE(CheckRtp2());
1184     EXPECT_TRUE(CheckNoRtp1());
1185     EXPECT_TRUE(CheckNoRtp2());
1186     EXPECT_TRUE(CheckRtcp1());
1187     EXPECT_TRUE(CheckRtcp2());
1188     EXPECT_TRUE(CheckNoRtcp1());
1189     EXPECT_TRUE(CheckNoRtcp2());
1190   }
1191
1192   // Check that RTCP data sent by the initiator before the accept is not muxed.
1193   void SendEarlyRtcpMuxToRtcp() {
1194     CreateChannels(RTCP | RTCP_MUX, RTCP);
1195     EXPECT_TRUE(SendInitiate());
1196     EXPECT_EQ(2U, GetTransport1()->channels().size());
1197     EXPECT_EQ(2U, GetTransport2()->channels().size());
1198
1199     // RTCP can be sent before the call is accepted, if the transport is ready.
1200     // It should not be muxed though, as the remote side doesn't support mux.
1201     EXPECT_TRUE(SendRtcp1());
1202     EXPECT_TRUE(CheckNoRtp2());
1203     EXPECT_TRUE(CheckRtcp2());
1204
1205     // Send RTCP packet from callee and verify that it is received.
1206     EXPECT_TRUE(SendRtcp2());
1207     EXPECT_TRUE(CheckNoRtp1());
1208     EXPECT_TRUE(CheckRtcp1());
1209
1210     // Complete call setup and ensure everything is still OK.
1211     EXPECT_TRUE(SendAccept());
1212     EXPECT_EQ(2U, GetTransport1()->channels().size());
1213     EXPECT_TRUE(SendRtcp1());
1214     EXPECT_TRUE(CheckRtcp2());
1215     EXPECT_TRUE(SendRtcp2());
1216     EXPECT_TRUE(CheckRtcp1());
1217   }
1218
1219
1220   // Check that RTCP data is not muxed until both sides have enabled muxing,
1221   // but that we properly demux before we get the accept message, since there
1222   // is a race between RTP data and the jingle accept.
1223   void SendEarlyRtcpMuxToRtcpMux() {
1224     CreateChannels(RTCP | RTCP_MUX, RTCP | RTCP_MUX);
1225     EXPECT_TRUE(SendInitiate());
1226     EXPECT_EQ(2U, GetTransport1()->channels().size());
1227     EXPECT_EQ(1U, GetTransport2()->channels().size());
1228
1229     // RTCP can't be sent yet, since the RTCP transport isn't writable, and
1230     // we haven't yet received the accept that says we should mux.
1231     EXPECT_FALSE(SendRtcp1());
1232
1233     // Send muxed RTCP packet from callee and verify that it is received.
1234     EXPECT_TRUE(SendRtcp2());
1235     EXPECT_TRUE(CheckNoRtp1());
1236     EXPECT_TRUE(CheckRtcp1());
1237
1238     // Complete call setup and ensure everything is still OK.
1239     EXPECT_TRUE(SendAccept());
1240     EXPECT_EQ(1U, GetTransport1()->channels().size());
1241     EXPECT_TRUE(SendRtcp1());
1242     EXPECT_TRUE(CheckRtcp2());
1243     EXPECT_TRUE(SendRtcp2());
1244     EXPECT_TRUE(CheckRtcp1());
1245   }
1246
1247   // Test that we properly send SRTP with RTCP in both directions.
1248   // You can pass in DTLS and/or RTCP_MUX as flags.
1249   void SendSrtpToSrtp(int flags1_in = 0, int flags2_in = 0) {
1250     ASSERT((flags1_in & ~(RTCP_MUX | DTLS)) == 0);
1251     ASSERT((flags2_in & ~(RTCP_MUX | DTLS)) == 0);
1252
1253     int flags1 = RTCP | SECURE | flags1_in;
1254     int flags2 = RTCP | SECURE | flags2_in;
1255     bool dtls1 = !!(flags1_in & DTLS);
1256     bool dtls2 = !!(flags2_in & DTLS);
1257     CreateChannels(flags1, flags2);
1258     EXPECT_FALSE(channel1_->secure());
1259     EXPECT_FALSE(channel2_->secure());
1260     EXPECT_TRUE(SendInitiate());
1261     EXPECT_TRUE_WAIT(channel1_->writable(), kEventTimeout);
1262     EXPECT_TRUE_WAIT(channel2_->writable(), kEventTimeout);
1263     EXPECT_TRUE(SendAccept());
1264     EXPECT_TRUE(channel1_->secure());
1265     EXPECT_TRUE(channel2_->secure());
1266     EXPECT_EQ(dtls1 && dtls2, channel1_->secure_dtls());
1267     EXPECT_EQ(dtls1 && dtls2, channel2_->secure_dtls());
1268     EXPECT_TRUE(SendRtp1());
1269     EXPECT_TRUE(SendRtp2());
1270     EXPECT_TRUE(SendRtcp1());
1271     EXPECT_TRUE(SendRtcp2());
1272     EXPECT_TRUE(CheckRtp1());
1273     EXPECT_TRUE(CheckRtp2());
1274     EXPECT_TRUE(CheckNoRtp1());
1275     EXPECT_TRUE(CheckNoRtp2());
1276     EXPECT_TRUE(CheckRtcp1());
1277     EXPECT_TRUE(CheckRtcp2());
1278     EXPECT_TRUE(CheckNoRtcp1());
1279     EXPECT_TRUE(CheckNoRtcp2());
1280   }
1281
1282   // Test that we properly handling SRTP negotiating down to RTP.
1283   void SendSrtpToRtp() {
1284     CreateChannels(RTCP | SECURE, RTCP);
1285     EXPECT_FALSE(channel1_->secure());
1286     EXPECT_FALSE(channel2_->secure());
1287     EXPECT_TRUE(SendInitiate());
1288     EXPECT_TRUE(SendAccept());
1289     EXPECT_FALSE(channel1_->secure());
1290     EXPECT_FALSE(channel2_->secure());
1291     EXPECT_TRUE(SendRtp1());
1292     EXPECT_TRUE(SendRtp2());
1293     EXPECT_TRUE(SendRtcp1());
1294     EXPECT_TRUE(SendRtcp2());
1295     EXPECT_TRUE(CheckRtp1());
1296     EXPECT_TRUE(CheckRtp2());
1297     EXPECT_TRUE(CheckNoRtp1());
1298     EXPECT_TRUE(CheckNoRtp2());
1299     EXPECT_TRUE(CheckRtcp1());
1300     EXPECT_TRUE(CheckRtcp2());
1301     EXPECT_TRUE(CheckNoRtcp1());
1302     EXPECT_TRUE(CheckNoRtcp2());
1303   }
1304
1305   // Test that we can send and receive early media when a provisional answer is
1306   // sent and received. The test uses SRTP, RTCP mux and SSRC mux.
1307   void SendEarlyMediaUsingRtcpMuxSrtp() {
1308       int sequence_number1_1 = 0, sequence_number2_2 = 0;
1309
1310       CreateChannels(SSRC_MUX | RTCP | RTCP_MUX | SECURE,
1311                      SSRC_MUX | RTCP | RTCP_MUX | SECURE);
1312       EXPECT_TRUE(SendOffer());
1313       EXPECT_TRUE(SendProvisionalAnswer());
1314       EXPECT_TRUE(channel1_->secure());
1315       EXPECT_TRUE(channel2_->secure());
1316       EXPECT_EQ(2U, GetTransport1()->channels().size());
1317       EXPECT_EQ(2U, GetTransport2()->channels().size());
1318       EXPECT_TRUE(SendCustomRtcp1(kSsrc1));
1319       EXPECT_TRUE(CheckCustomRtcp2(kSsrc1));
1320       EXPECT_TRUE(SendCustomRtp1(kSsrc1, ++sequence_number1_1));
1321       EXPECT_TRUE(CheckCustomRtp2(kSsrc1, sequence_number1_1));
1322
1323       // Send packets from callee and verify that it is received.
1324       EXPECT_TRUE(SendCustomRtcp2(kSsrc2));
1325       EXPECT_TRUE(CheckCustomRtcp1(kSsrc2));
1326       EXPECT_TRUE(SendCustomRtp2(kSsrc2, ++sequence_number2_2));
1327       EXPECT_TRUE(CheckCustomRtp1(kSsrc2, sequence_number2_2));
1328
1329       // Complete call setup and ensure everything is still OK.
1330       EXPECT_TRUE(SendFinalAnswer());
1331       EXPECT_EQ(1U, GetTransport1()->channels().size());
1332       EXPECT_EQ(1U, GetTransport2()->channels().size());
1333       EXPECT_TRUE(channel1_->secure());
1334       EXPECT_TRUE(channel2_->secure());
1335       EXPECT_TRUE(SendCustomRtcp1(kSsrc1));
1336       EXPECT_TRUE(CheckCustomRtcp2(kSsrc1));
1337       EXPECT_TRUE(SendCustomRtp1(kSsrc1, ++sequence_number1_1));
1338       EXPECT_TRUE(CheckCustomRtp2(kSsrc1, sequence_number1_1));
1339       EXPECT_TRUE(SendCustomRtcp2(kSsrc2));
1340       EXPECT_TRUE(CheckCustomRtcp1(kSsrc2));
1341       EXPECT_TRUE(SendCustomRtp2(kSsrc2, ++sequence_number2_2));
1342       EXPECT_TRUE(CheckCustomRtp1(kSsrc2, sequence_number2_2));
1343   }
1344
1345   // Test that we properly send RTP without SRTP from a thread.
1346   void SendRtpToRtpOnThread() {
1347     bool sent_rtp1, sent_rtp2, sent_rtcp1, sent_rtcp2;
1348     CreateChannels(RTCP, RTCP);
1349     EXPECT_TRUE(SendInitiate());
1350     EXPECT_TRUE(SendAccept());
1351     CallOnThread(&ChannelTest<T>::SendRtp1, &sent_rtp1);
1352     CallOnThread(&ChannelTest<T>::SendRtp2, &sent_rtp2);
1353     CallOnThread(&ChannelTest<T>::SendRtcp1, &sent_rtcp1);
1354     CallOnThread(&ChannelTest<T>::SendRtcp2, &sent_rtcp2);
1355     EXPECT_TRUE_WAIT(CheckRtp1(), 1000);
1356     EXPECT_TRUE_WAIT(CheckRtp2(), 1000);
1357     EXPECT_TRUE_WAIT(sent_rtp1, 1000);
1358     EXPECT_TRUE_WAIT(sent_rtp2, 1000);
1359     EXPECT_TRUE(CheckNoRtp1());
1360     EXPECT_TRUE(CheckNoRtp2());
1361     EXPECT_TRUE_WAIT(CheckRtcp1(), 1000);
1362     EXPECT_TRUE_WAIT(CheckRtcp2(), 1000);
1363     EXPECT_TRUE_WAIT(sent_rtcp1, 1000);
1364     EXPECT_TRUE_WAIT(sent_rtcp2, 1000);
1365     EXPECT_TRUE(CheckNoRtcp1());
1366     EXPECT_TRUE(CheckNoRtcp2());
1367   }
1368
1369   // Test that we properly send SRTP with RTCP from a thread.
1370   void SendSrtpToSrtpOnThread() {
1371     bool sent_rtp1, sent_rtp2, sent_rtcp1, sent_rtcp2;
1372     CreateChannels(RTCP | SECURE, RTCP | SECURE);
1373     EXPECT_TRUE(SendInitiate());
1374     EXPECT_TRUE(SendAccept());
1375     CallOnThread(&ChannelTest<T>::SendRtp1, &sent_rtp1);
1376     CallOnThread(&ChannelTest<T>::SendRtp2, &sent_rtp2);
1377     CallOnThread(&ChannelTest<T>::SendRtcp1, &sent_rtcp1);
1378     CallOnThread(&ChannelTest<T>::SendRtcp2, &sent_rtcp2);
1379     EXPECT_TRUE_WAIT(CheckRtp1(), 1000);
1380     EXPECT_TRUE_WAIT(CheckRtp2(), 1000);
1381     EXPECT_TRUE_WAIT(sent_rtp1, 1000);
1382     EXPECT_TRUE_WAIT(sent_rtp2, 1000);
1383     EXPECT_TRUE(CheckNoRtp1());
1384     EXPECT_TRUE(CheckNoRtp2());
1385     EXPECT_TRUE_WAIT(CheckRtcp1(), 1000);
1386     EXPECT_TRUE_WAIT(CheckRtcp2(), 1000);
1387     EXPECT_TRUE_WAIT(sent_rtcp1, 1000);
1388     EXPECT_TRUE_WAIT(sent_rtcp2, 1000);
1389     EXPECT_TRUE(CheckNoRtcp1());
1390     EXPECT_TRUE(CheckNoRtcp2());
1391   }
1392
1393   // Test that the mediachannel retains its sending state after the transport
1394   // becomes non-writable.
1395   void SendWithWritabilityLoss() {
1396     CreateChannels(0, 0);
1397     EXPECT_TRUE(SendInitiate());
1398     EXPECT_TRUE(SendAccept());
1399     EXPECT_EQ(1U, GetTransport1()->channels().size());
1400     EXPECT_EQ(1U, GetTransport2()->channels().size());
1401     EXPECT_TRUE(SendRtp1());
1402     EXPECT_TRUE(SendRtp2());
1403     EXPECT_TRUE(CheckRtp1());
1404     EXPECT_TRUE(CheckRtp2());
1405     EXPECT_TRUE(CheckNoRtp1());
1406     EXPECT_TRUE(CheckNoRtp2());
1407
1408     // Lose writability, which should fail.
1409     GetTransport1()->SetWritable(false);
1410     EXPECT_FALSE(SendRtp1());
1411     EXPECT_TRUE(SendRtp2());
1412     EXPECT_TRUE(CheckRtp1());
1413     EXPECT_TRUE(CheckNoRtp2());
1414
1415     // Regain writability
1416     GetTransport1()->SetWritable(true);
1417     EXPECT_TRUE(media_channel1_->sending());
1418     EXPECT_TRUE(SendRtp1());
1419     EXPECT_TRUE(SendRtp2());
1420     EXPECT_TRUE(CheckRtp1());
1421     EXPECT_TRUE(CheckRtp2());
1422     EXPECT_TRUE(CheckNoRtp1());
1423     EXPECT_TRUE(CheckNoRtp2());
1424
1425     // Lose writability completely
1426     GetTransport1()->SetDestination(NULL);
1427     EXPECT_TRUE(media_channel1_->sending());
1428
1429     // Should fail also.
1430     EXPECT_FALSE(SendRtp1());
1431     EXPECT_TRUE(SendRtp2());
1432     EXPECT_TRUE(CheckRtp1());
1433     EXPECT_TRUE(CheckNoRtp2());
1434
1435     // Gain writability back
1436     GetTransport1()->SetDestination(GetTransport2());
1437     EXPECT_TRUE(media_channel1_->sending());
1438     EXPECT_TRUE(SendRtp1());
1439     EXPECT_TRUE(SendRtp2());
1440     EXPECT_TRUE(CheckRtp1());
1441     EXPECT_TRUE(CheckRtp2());
1442     EXPECT_TRUE(CheckNoRtp1());
1443     EXPECT_TRUE(CheckNoRtp2());
1444   }
1445
1446   void SendBundleToBundle(
1447       const int* pl_types, int len, bool rtcp_mux, bool secure) {
1448     ASSERT_EQ(2, len);
1449     int sequence_number1_1 = 0, sequence_number2_2 = 0;
1450     // Only pl_type1 was added to the bundle filter for both |channel1_|
1451     // and |channel2_|.
1452     int pl_type1 = pl_types[0];
1453     int pl_type2 = pl_types[1];
1454     int flags = SSRC_MUX | RTCP;
1455     if (secure) flags |= SECURE;
1456     uint32 expected_channels = 2U;
1457     if (rtcp_mux) {
1458       flags |= RTCP_MUX;
1459       expected_channels = 1U;
1460     }
1461     CreateChannels(flags, flags);
1462     EXPECT_TRUE(SendInitiate());
1463     EXPECT_EQ(2U, GetTransport1()->channels().size());
1464     EXPECT_EQ(expected_channels, GetTransport2()->channels().size());
1465     EXPECT_TRUE(SendAccept());
1466     EXPECT_EQ(expected_channels, GetTransport1()->channels().size());
1467     EXPECT_EQ(expected_channels, GetTransport2()->channels().size());
1468     EXPECT_TRUE(channel1_->bundle_filter()->FindPayloadType(pl_type1));
1469     EXPECT_TRUE(channel2_->bundle_filter()->FindPayloadType(pl_type1));
1470     EXPECT_FALSE(channel1_->bundle_filter()->FindPayloadType(pl_type2));
1471     EXPECT_FALSE(channel2_->bundle_filter()->FindPayloadType(pl_type2));
1472     // channel1 - should only have media_content2 as remote. i.e. kSsrc2
1473     EXPECT_TRUE(channel1_->bundle_filter()->FindStream(kSsrc2));
1474     EXPECT_FALSE(channel1_->bundle_filter()->FindStream(kSsrc1));
1475     // channel2 - should only have media_content1 as remote. i.e. kSsrc1
1476     EXPECT_TRUE(channel2_->bundle_filter()->FindStream(kSsrc1));
1477     EXPECT_FALSE(channel2_->bundle_filter()->FindStream(kSsrc2));
1478
1479     // Both channels can receive pl_type1 only.
1480     EXPECT_TRUE(SendCustomRtp1(kSsrc1, ++sequence_number1_1, pl_type1));
1481     EXPECT_TRUE(CheckCustomRtp2(kSsrc1, sequence_number1_1, pl_type1));
1482     EXPECT_TRUE(SendCustomRtp2(kSsrc2, ++sequence_number2_2, pl_type1));
1483     EXPECT_TRUE(CheckCustomRtp1(kSsrc2, sequence_number2_2, pl_type1));
1484     EXPECT_TRUE(CheckNoRtp1());
1485     EXPECT_TRUE(CheckNoRtp2());
1486
1487     // RTCP test
1488     EXPECT_TRUE(SendCustomRtp1(kSsrc1, ++sequence_number1_1, pl_type2));
1489     EXPECT_FALSE(CheckCustomRtp2(kSsrc1, sequence_number1_1, pl_type2));
1490     EXPECT_TRUE(SendCustomRtp2(kSsrc2, ++sequence_number2_2, pl_type2));
1491     EXPECT_FALSE(CheckCustomRtp1(kSsrc2, sequence_number2_2, pl_type2));
1492
1493     EXPECT_TRUE(SendCustomRtcp1(kSsrc1));
1494     EXPECT_TRUE(SendCustomRtcp2(kSsrc2));
1495     EXPECT_TRUE(CheckCustomRtcp1(kSsrc2));
1496     EXPECT_TRUE(CheckNoRtcp1());
1497     EXPECT_TRUE(CheckCustomRtcp2(kSsrc1));
1498     EXPECT_TRUE(CheckNoRtcp2());
1499
1500     EXPECT_TRUE(SendCustomRtcp1(kSsrc2));
1501     EXPECT_TRUE(SendCustomRtcp2(kSsrc1));
1502     EXPECT_FALSE(CheckCustomRtcp1(kSsrc1));
1503     EXPECT_FALSE(CheckCustomRtcp2(kSsrc2));
1504   }
1505
1506   // Test that the media monitor can be run and gives timely callbacks.
1507   void TestMediaMonitor() {
1508     static const int kTimeout = 500;
1509     CreateChannels(0, 0);
1510     EXPECT_TRUE(SendInitiate());
1511     EXPECT_TRUE(SendAccept());
1512     channel1_->StartMediaMonitor(100);
1513     channel2_->StartMediaMonitor(100);
1514     // Ensure we get callbacks and stop.
1515     EXPECT_TRUE_WAIT(media_info_callbacks1_ > 0, kTimeout);
1516     EXPECT_TRUE_WAIT(media_info_callbacks2_ > 0, kTimeout);
1517     channel1_->StopMediaMonitor();
1518     channel2_->StopMediaMonitor();
1519     // Ensure a restart of a stopped monitor works.
1520     channel1_->StartMediaMonitor(100);
1521     EXPECT_TRUE_WAIT(media_info_callbacks1_ > 0, kTimeout);
1522     channel1_->StopMediaMonitor();
1523     // Ensure stopping a stopped monitor is OK.
1524     channel1_->StopMediaMonitor();
1525   }
1526
1527   void TestMediaSinks() {
1528     CreateChannels(0, 0);
1529     EXPECT_TRUE(SendInitiate());
1530     EXPECT_TRUE(SendAccept());
1531     EXPECT_FALSE(channel1_->HasSendSinks(cricket::SINK_POST_CRYPTO));
1532     EXPECT_FALSE(channel1_->HasRecvSinks(cricket::SINK_POST_CRYPTO));
1533     EXPECT_FALSE(channel1_->HasSendSinks(cricket::SINK_PRE_CRYPTO));
1534     EXPECT_FALSE(channel1_->HasRecvSinks(cricket::SINK_PRE_CRYPTO));
1535
1536     talk_base::Pathname path;
1537     EXPECT_TRUE(talk_base::Filesystem::GetTemporaryFolder(path, true, NULL));
1538     path.SetFilename("sink-test.rtpdump");
1539     talk_base::scoped_ptr<cricket::RtpDumpSink> sink(
1540         new cricket::RtpDumpSink(Open(path.pathname())));
1541     sink->set_packet_filter(cricket::PF_ALL);
1542     EXPECT_TRUE(sink->Enable(true));
1543     channel1_->RegisterSendSink(
1544         sink.get(), &cricket::RtpDumpSink::OnPacket, cricket::SINK_POST_CRYPTO);
1545     EXPECT_TRUE(channel1_->HasSendSinks(cricket::SINK_POST_CRYPTO));
1546     EXPECT_FALSE(channel1_->HasRecvSinks(cricket::SINK_POST_CRYPTO));
1547     EXPECT_FALSE(channel1_->HasSendSinks(cricket::SINK_PRE_CRYPTO));
1548     EXPECT_FALSE(channel1_->HasRecvSinks(cricket::SINK_PRE_CRYPTO));
1549
1550     // The first packet is recorded with header + data.
1551     EXPECT_TRUE(SendRtp1());
1552     // The second packet is recorded with header only.
1553     sink->set_packet_filter(cricket::PF_RTPHEADER);
1554     EXPECT_TRUE(SendRtp1());
1555     // The third packet is not recorded since sink is disabled.
1556     EXPECT_TRUE(sink->Enable(false));
1557     EXPECT_TRUE(SendRtp1());
1558      // The fourth packet is not recorded since sink is unregistered.
1559     EXPECT_TRUE(sink->Enable(true));
1560     channel1_->UnregisterSendSink(sink.get(), cricket::SINK_POST_CRYPTO);
1561     EXPECT_TRUE(SendRtp1());
1562     sink.reset();  // This will close the file.
1563
1564     // Read the recorded file and verify two packets.
1565     talk_base::scoped_ptr<talk_base::StreamInterface> stream(
1566         talk_base::Filesystem::OpenFile(path, "rb"));
1567
1568     cricket::RtpDumpReader reader(stream.get());
1569     cricket::RtpDumpPacket packet;
1570     EXPECT_EQ(talk_base::SR_SUCCESS, reader.ReadPacket(&packet));
1571     std::string read_packet(reinterpret_cast<const char*>(&packet.data[0]),
1572         packet.data.size());
1573     EXPECT_EQ(rtp_packet_, read_packet);
1574
1575     EXPECT_EQ(talk_base::SR_SUCCESS, reader.ReadPacket(&packet));
1576     size_t len = 0;
1577     packet.GetRtpHeaderLen(&len);
1578     EXPECT_EQ(len, packet.data.size());
1579     EXPECT_EQ(0, memcmp(&packet.data[0], rtp_packet_.c_str(), len));
1580
1581     EXPECT_EQ(talk_base::SR_EOS, reader.ReadPacket(&packet));
1582
1583     // Delete the file for media recording.
1584     stream.reset();
1585     EXPECT_TRUE(talk_base::Filesystem::DeleteFile(path));
1586   }
1587
1588   void TestSetContentFailure() {
1589     CreateChannels(0, 0);
1590     typename T::Content content;
1591     cricket::SessionDescription* sdesc_loc = new cricket::SessionDescription();
1592     cricket::SessionDescription* sdesc_rem = new cricket::SessionDescription();
1593
1594     // Set up the session description.
1595     CreateContent(0, kPcmuCodec, kH264Codec, &content);
1596     sdesc_loc->AddContent(cricket::CN_AUDIO, cricket::NS_JINGLE_RTP,
1597                           new cricket::AudioContentDescription());
1598     sdesc_loc->AddContent(cricket::CN_VIDEO, cricket::NS_JINGLE_RTP,
1599                           new cricket::VideoContentDescription());
1600     EXPECT_TRUE(session1_.set_local_description(sdesc_loc));
1601     sdesc_rem->AddContent(cricket::CN_AUDIO, cricket::NS_JINGLE_RTP,
1602                           new cricket::AudioContentDescription());
1603     sdesc_rem->AddContent(cricket::CN_VIDEO, cricket::NS_JINGLE_RTP,
1604                           new cricket::VideoContentDescription());
1605     EXPECT_TRUE(session1_.set_remote_description(sdesc_rem));
1606
1607     // Test failures in SetLocalContent.
1608     media_channel1_->set_fail_set_recv_codecs(true);
1609     session1_.SetError(cricket::BaseSession::ERROR_NONE, "");
1610     session1_.SetState(cricket::Session::STATE_SENTINITIATE);
1611     EXPECT_EQ(cricket::BaseSession::ERROR_CONTENT, session1_.error());
1612     media_channel1_->set_fail_set_recv_codecs(true);
1613     session1_.SetError(cricket::BaseSession::ERROR_NONE, "");
1614     session1_.SetState(cricket::Session::STATE_SENTACCEPT);
1615     EXPECT_EQ(cricket::BaseSession::ERROR_CONTENT, session1_.error());
1616
1617     // Test failures in SetRemoteContent.
1618     media_channel1_->set_fail_set_send_codecs(true);
1619     session1_.SetError(cricket::BaseSession::ERROR_NONE, "");
1620     session1_.SetState(cricket::Session::STATE_RECEIVEDINITIATE);
1621     EXPECT_EQ(cricket::BaseSession::ERROR_CONTENT, session1_.error());
1622     media_channel1_->set_fail_set_send_codecs(true);
1623     session1_.SetError(cricket::BaseSession::ERROR_NONE, "");
1624     session1_.SetState(cricket::Session::STATE_RECEIVEDACCEPT);
1625     EXPECT_EQ(cricket::BaseSession::ERROR_CONTENT, session1_.error());
1626   }
1627
1628   void TestSendTwoOffers() {
1629     CreateChannels(0, 0);
1630
1631     // Set up the initial session description.
1632     cricket::SessionDescription* sdesc = CreateSessionDescriptionWithStream(1);
1633     EXPECT_TRUE(session1_.set_local_description(sdesc));
1634
1635     session1_.SetError(cricket::BaseSession::ERROR_NONE, "");
1636     session1_.SetState(cricket::Session::STATE_SENTINITIATE);
1637     EXPECT_EQ(cricket::BaseSession::ERROR_NONE, session1_.error());
1638     EXPECT_TRUE(media_channel1_->HasSendStream(1));
1639
1640     // Update the local description and set the state again.
1641     sdesc = CreateSessionDescriptionWithStream(2);
1642     EXPECT_TRUE(session1_.set_local_description(sdesc));
1643
1644     session1_.SetState(cricket::Session::STATE_SENTINITIATE);
1645     EXPECT_EQ(cricket::BaseSession::ERROR_NONE, session1_.error());
1646     EXPECT_FALSE(media_channel1_->HasSendStream(1));
1647     EXPECT_TRUE(media_channel1_->HasSendStream(2));
1648   }
1649
1650   void TestReceiveTwoOffers() {
1651     CreateChannels(0, 0);
1652
1653     // Set up the initial session description.
1654     cricket::SessionDescription* sdesc = CreateSessionDescriptionWithStream(1);
1655     EXPECT_TRUE(session1_.set_remote_description(sdesc));
1656
1657     session1_.SetError(cricket::BaseSession::ERROR_NONE, "");
1658     session1_.SetState(cricket::Session::STATE_RECEIVEDINITIATE);
1659     EXPECT_EQ(cricket::BaseSession::ERROR_NONE, session1_.error());
1660     EXPECT_TRUE(media_channel1_->HasRecvStream(1));
1661
1662     sdesc = CreateSessionDescriptionWithStream(2);
1663     EXPECT_TRUE(session1_.set_remote_description(sdesc));
1664     session1_.SetState(cricket::Session::STATE_RECEIVEDINITIATE);
1665     EXPECT_EQ(cricket::BaseSession::ERROR_NONE, session1_.error());
1666     EXPECT_FALSE(media_channel1_->HasRecvStream(1));
1667     EXPECT_TRUE(media_channel1_->HasRecvStream(2));
1668   }
1669
1670   void TestSendPrAnswer() {
1671     CreateChannels(0, 0);
1672
1673     // Set up the initial session description.
1674     cricket::SessionDescription* sdesc = CreateSessionDescriptionWithStream(1);
1675     EXPECT_TRUE(session1_.set_remote_description(sdesc));
1676
1677     session1_.SetError(cricket::BaseSession::ERROR_NONE, "");
1678     session1_.SetState(cricket::Session::STATE_RECEIVEDINITIATE);
1679     EXPECT_EQ(cricket::BaseSession::ERROR_NONE, session1_.error());
1680     EXPECT_TRUE(media_channel1_->HasRecvStream(1));
1681
1682     // Send PRANSWER
1683     sdesc = CreateSessionDescriptionWithStream(2);
1684     EXPECT_TRUE(session1_.set_local_description(sdesc));
1685
1686     session1_.SetState(cricket::Session::STATE_SENTPRACCEPT);
1687     EXPECT_EQ(cricket::BaseSession::ERROR_NONE, session1_.error());
1688     EXPECT_TRUE(media_channel1_->HasRecvStream(1));
1689     EXPECT_TRUE(media_channel1_->HasSendStream(2));
1690
1691     // Send ACCEPT
1692     sdesc = CreateSessionDescriptionWithStream(3);
1693     EXPECT_TRUE(session1_.set_local_description(sdesc));
1694
1695     session1_.SetState(cricket::Session::STATE_SENTACCEPT);
1696     EXPECT_EQ(cricket::BaseSession::ERROR_NONE, session1_.error());
1697     EXPECT_TRUE(media_channel1_->HasRecvStream(1));
1698     EXPECT_FALSE(media_channel1_->HasSendStream(2));
1699     EXPECT_TRUE(media_channel1_->HasSendStream(3));
1700   }
1701
1702   void TestReceivePrAnswer() {
1703     CreateChannels(0, 0);
1704
1705     // Set up the initial session description.
1706     cricket::SessionDescription* sdesc = CreateSessionDescriptionWithStream(1);
1707     EXPECT_TRUE(session1_.set_local_description(sdesc));
1708
1709     session1_.SetError(cricket::BaseSession::ERROR_NONE, "");
1710     session1_.SetState(cricket::Session::STATE_SENTINITIATE);
1711     EXPECT_EQ(cricket::BaseSession::ERROR_NONE, session1_.error());
1712     EXPECT_TRUE(media_channel1_->HasSendStream(1));
1713
1714     // Receive PRANSWER
1715     sdesc = CreateSessionDescriptionWithStream(2);
1716     EXPECT_TRUE(session1_.set_remote_description(sdesc));
1717
1718     session1_.SetState(cricket::Session::STATE_RECEIVEDPRACCEPT);
1719     EXPECT_EQ(cricket::BaseSession::ERROR_NONE, session1_.error());
1720     EXPECT_TRUE(media_channel1_->HasSendStream(1));
1721     EXPECT_TRUE(media_channel1_->HasRecvStream(2));
1722
1723     // Receive ACCEPT
1724     sdesc = CreateSessionDescriptionWithStream(3);
1725     EXPECT_TRUE(session1_.set_remote_description(sdesc));
1726
1727     session1_.SetState(cricket::Session::STATE_RECEIVEDACCEPT);
1728     EXPECT_EQ(cricket::BaseSession::ERROR_NONE, session1_.error());
1729     EXPECT_TRUE(media_channel1_->HasSendStream(1));
1730     EXPECT_FALSE(media_channel1_->HasRecvStream(2));
1731     EXPECT_TRUE(media_channel1_->HasRecvStream(3));
1732   }
1733
1734   void TestFlushRtcp() {
1735     bool send_rtcp1;
1736
1737     CreateChannels(RTCP, RTCP);
1738     EXPECT_TRUE(SendInitiate());
1739     EXPECT_TRUE(SendAccept());
1740     EXPECT_EQ(2U, GetTransport1()->channels().size());
1741     EXPECT_EQ(2U, GetTransport2()->channels().size());
1742
1743     // Send RTCP1 from a different thread.
1744     CallOnThreadAndWaitForDone(&ChannelTest<T>::SendRtcp1, &send_rtcp1);
1745     EXPECT_TRUE(send_rtcp1);
1746     // The sending message is only posted.  channel2_ should be empty.
1747     EXPECT_TRUE(CheckNoRtcp2());
1748
1749     // When channel1_ is deleted, the RTCP packet should be sent out to
1750     // channel2_.
1751     channel1_.reset();
1752     EXPECT_TRUE(CheckRtcp2());
1753   }
1754
1755   void TestChangeStateError() {
1756     CreateChannels(RTCP, RTCP);
1757     EXPECT_TRUE(SendInitiate());
1758     media_channel2_->set_fail_set_send(true);
1759     EXPECT_TRUE(channel2_->Enable(true));
1760     EXPECT_EQ(cricket::VoiceMediaChannel::ERROR_REC_DEVICE_OPEN_FAILED,
1761               error_);
1762   }
1763
1764   void TestSrtpError(int pl_type) {
1765     // For Audio, only pl_type 0 is added to the bundle filter.
1766     // For Video, only pl_type 97 is added to the bundle filter.
1767     // So we need to pass in pl_type so that the packet can pass through
1768     // the bundle filter before it can be processed by the srtp filter.
1769     // The packet is not a valid srtp packet because it is too short.
1770     unsigned const char kBadPacket[] = {
1771       0x84, pl_type, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
1772     };
1773     CreateChannels(RTCP | SECURE, RTCP | SECURE);
1774     EXPECT_FALSE(channel1_->secure());
1775     EXPECT_FALSE(channel2_->secure());
1776     EXPECT_TRUE(SendInitiate());
1777     EXPECT_TRUE(SendAccept());
1778     EXPECT_TRUE(channel1_->secure());
1779     EXPECT_TRUE(channel2_->secure());
1780     channel2_->set_srtp_signal_silent_time(200);
1781
1782     // Testing failures in sending packets.
1783     EXPECT_FALSE(media_channel2_->SendRtp(kBadPacket, sizeof(kBadPacket)));
1784     // The first failure will trigger an error.
1785     EXPECT_EQ_WAIT(T::MediaChannel::ERROR_REC_SRTP_ERROR, error_, 500);
1786     error_ = T::MediaChannel::ERROR_NONE;
1787     // The next 1 sec failures will not trigger an error.
1788     EXPECT_FALSE(media_channel2_->SendRtp(kBadPacket, sizeof(kBadPacket)));
1789     // Wait for a while to ensure no message comes in.
1790     talk_base::Thread::Current()->ProcessMessages(210);
1791     EXPECT_EQ(T::MediaChannel::ERROR_NONE, error_);
1792     // The error will be triggered again.
1793     EXPECT_FALSE(media_channel2_->SendRtp(kBadPacket, sizeof(kBadPacket)));
1794     EXPECT_EQ_WAIT(T::MediaChannel::ERROR_REC_SRTP_ERROR, error_, 500);
1795
1796     // Testing failures in receiving packets.
1797     error_ = T::MediaChannel::ERROR_NONE;
1798     cricket::TransportChannel* transport_channel =
1799         channel2_->transport_channel();
1800     transport_channel->SignalReadPacket(
1801         transport_channel, reinterpret_cast<const char*>(kBadPacket),
1802         sizeof(kBadPacket), talk_base::PacketTime(), 0);
1803     EXPECT_EQ_WAIT(T::MediaChannel::ERROR_PLAY_SRTP_ERROR, error_, 500);
1804   }
1805
1806   void TestOnReadyToSend() {
1807     CreateChannels(RTCP, RTCP);
1808     TransportChannel* rtp = channel1_->transport_channel();
1809     TransportChannel* rtcp = channel1_->rtcp_transport_channel();
1810     EXPECT_FALSE(media_channel1_->ready_to_send());
1811     rtp->SignalReadyToSend(rtp);
1812     EXPECT_FALSE(media_channel1_->ready_to_send());
1813     rtcp->SignalReadyToSend(rtcp);
1814     // MediaChannel::OnReadyToSend only be called when both rtp and rtcp
1815     // channel are ready to send.
1816     EXPECT_TRUE(media_channel1_->ready_to_send());
1817
1818     // rtp channel becomes not ready to send will be propagated to mediachannel
1819     channel1_->SetReadyToSend(rtp, false);
1820     EXPECT_FALSE(media_channel1_->ready_to_send());
1821     channel1_->SetReadyToSend(rtp, true);
1822     EXPECT_TRUE(media_channel1_->ready_to_send());
1823
1824     // rtcp channel becomes not ready to send will be propagated to mediachannel
1825     channel1_->SetReadyToSend(rtcp, false);
1826     EXPECT_FALSE(media_channel1_->ready_to_send());
1827     channel1_->SetReadyToSend(rtcp, true);
1828     EXPECT_TRUE(media_channel1_->ready_to_send());
1829   }
1830
1831   void TestOnReadyToSendWithRtcpMux() {
1832     CreateChannels(RTCP, RTCP);
1833     typename T::Content content;
1834     CreateContent(0, kPcmuCodec, kH264Codec, &content);
1835     // Both sides agree on mux. Should no longer be a separate RTCP channel.
1836     content.set_rtcp_mux(true);
1837     EXPECT_TRUE(channel1_->SetLocalContent(&content, CA_OFFER, NULL));
1838     EXPECT_TRUE(channel1_->SetRemoteContent(&content, CA_ANSWER, NULL));
1839     EXPECT_TRUE(channel1_->rtcp_transport_channel() == NULL);
1840     TransportChannel* rtp = channel1_->transport_channel();
1841     EXPECT_FALSE(media_channel1_->ready_to_send());
1842     // In the case of rtcp mux, the SignalReadyToSend() from rtp channel
1843     // should trigger the MediaChannel's OnReadyToSend.
1844     rtp->SignalReadyToSend(rtp);
1845     EXPECT_TRUE(media_channel1_->ready_to_send());
1846     channel1_->SetReadyToSend(rtp, false);
1847     EXPECT_FALSE(media_channel1_->ready_to_send());
1848   }
1849
1850  protected:
1851   cricket::FakeSession session1_;
1852   cricket::FakeSession session2_;
1853   cricket::FakeMediaEngine media_engine_;
1854   // The media channels are owned by the voice channel objects below.
1855   typename T::MediaChannel* media_channel1_;
1856   typename T::MediaChannel* media_channel2_;
1857   talk_base::scoped_ptr<typename T::Channel> channel1_;
1858   talk_base::scoped_ptr<typename T::Channel> channel2_;
1859   typename T::Content local_media_content1_;
1860   typename T::Content local_media_content2_;
1861   typename T::Content remote_media_content1_;
1862   typename T::Content remote_media_content2_;
1863   talk_base::scoped_ptr<talk_base::SSLIdentity> identity1_;
1864   talk_base::scoped_ptr<talk_base::SSLIdentity> identity2_;
1865   // The RTP and RTCP packets to send in the tests.
1866   std::string rtp_packet_;
1867   std::string rtcp_packet_;
1868   int media_info_callbacks1_;
1869   int media_info_callbacks2_;
1870   bool mute_callback_recved_;
1871   bool mute_callback_value_;
1872
1873   uint32 ssrc_;
1874   typename T::MediaChannel::Error error_;
1875 };
1876
1877
1878 template<>
1879 void ChannelTest<VoiceTraits>::CreateContent(
1880     int flags,
1881     const cricket::AudioCodec& audio_codec,
1882     const cricket::VideoCodec& video_codec,
1883     cricket::AudioContentDescription* audio) {
1884   audio->AddCodec(audio_codec);
1885   audio->set_rtcp_mux((flags & RTCP_MUX) != 0);
1886   if (flags & SECURE) {
1887     audio->AddCrypto(cricket::CryptoParams(
1888         1, cricket::CS_AES_CM_128_HMAC_SHA1_32,
1889         "inline:" + talk_base::CreateRandomString(40), ""));
1890   }
1891 }
1892
1893 template<>
1894 void ChannelTest<VoiceTraits>::CopyContent(
1895     const cricket::AudioContentDescription& source,
1896     cricket::AudioContentDescription* audio) {
1897   *audio = source;
1898 }
1899
1900 template<>
1901 bool ChannelTest<VoiceTraits>::CodecMatches(const cricket::AudioCodec& c1,
1902                                             const cricket::AudioCodec& c2) {
1903   return c1.name == c2.name && c1.clockrate == c2.clockrate &&
1904       c1.bitrate == c2.bitrate && c1.channels == c2.channels;
1905 }
1906
1907 template<>
1908 void ChannelTest<VoiceTraits>::AddLegacyStreamInContent(
1909     uint32 ssrc, int flags, cricket::AudioContentDescription* audio) {
1910   audio->AddLegacyStream(ssrc);
1911 }
1912
1913 class VoiceChannelTest
1914     : public ChannelTest<VoiceTraits> {
1915  public:
1916   typedef ChannelTest<VoiceTraits>
1917   Base;
1918   VoiceChannelTest() : Base(kPcmuFrame, sizeof(kPcmuFrame),
1919                             kRtcpReport, sizeof(kRtcpReport)) {
1920   }
1921
1922   void TestSetChannelOptions() {
1923     CreateChannels(0, 0);
1924
1925     cricket::AudioOptions options1;
1926     options1.echo_cancellation.Set(false);
1927     cricket::AudioOptions options2;
1928     options2.echo_cancellation.Set(true);
1929
1930     channel1_->SetChannelOptions(options1);
1931     channel2_->SetChannelOptions(options1);
1932     cricket::AudioOptions actual_options;
1933     ASSERT_TRUE(media_channel1_->GetOptions(&actual_options));
1934     EXPECT_EQ(options1, actual_options);
1935     ASSERT_TRUE(media_channel2_->GetOptions(&actual_options));
1936     EXPECT_EQ(options1, actual_options);
1937
1938     channel1_->SetChannelOptions(options2);
1939     channel2_->SetChannelOptions(options2);
1940     ASSERT_TRUE(media_channel1_->GetOptions(&actual_options));
1941     EXPECT_EQ(options2, actual_options);
1942     ASSERT_TRUE(media_channel2_->GetOptions(&actual_options));
1943     EXPECT_EQ(options2, actual_options);
1944   }
1945 };
1946
1947 // override to add NULL parameter
1948 template<>
1949 cricket::VideoChannel* ChannelTest<VideoTraits>::CreateChannel(
1950     talk_base::Thread* thread, cricket::MediaEngineInterface* engine,
1951     cricket::FakeVideoMediaChannel* ch, cricket::BaseSession* session,
1952     bool rtcp) {
1953   cricket::VideoChannel* channel = new cricket::VideoChannel(
1954       thread, engine, ch, session, cricket::CN_VIDEO, rtcp, NULL);
1955   if (!channel->Init()) {
1956     delete channel;
1957     channel = NULL;
1958   }
1959   return channel;
1960 }
1961
1962 // override to add 0 parameter
1963 template<>
1964 bool ChannelTest<VideoTraits>::AddStream1(int id) {
1965   return channel1_->AddRecvStream(cricket::StreamParams::CreateLegacy(id));
1966 }
1967
1968 template<>
1969 void ChannelTest<VideoTraits>::CreateContent(
1970     int flags,
1971     const cricket::AudioCodec& audio_codec,
1972     const cricket::VideoCodec& video_codec,
1973     cricket::VideoContentDescription* video) {
1974   video->AddCodec(video_codec);
1975   video->set_rtcp_mux((flags & RTCP_MUX) != 0);
1976   if (flags & SECURE) {
1977     video->AddCrypto(cricket::CryptoParams(
1978         1, cricket::CS_AES_CM_128_HMAC_SHA1_80,
1979         "inline:" + talk_base::CreateRandomString(40), ""));
1980   }
1981 }
1982
1983 template<>
1984 void ChannelTest<VideoTraits>::CopyContent(
1985     const cricket::VideoContentDescription& source,
1986     cricket::VideoContentDescription* video) {
1987   *video = source;
1988 }
1989
1990 template<>
1991 bool ChannelTest<VideoTraits>::CodecMatches(const cricket::VideoCodec& c1,
1992                                             const cricket::VideoCodec& c2) {
1993   return c1.name == c2.name && c1.width == c2.width && c1.height == c2.height &&
1994       c1.framerate == c2.framerate;
1995 }
1996
1997 template<>
1998 void ChannelTest<VideoTraits>::AddLegacyStreamInContent(
1999     uint32 ssrc, int flags, cricket::VideoContentDescription* video) {
2000   video->AddLegacyStream(ssrc);
2001 }
2002
2003 class VideoChannelTest
2004     : public ChannelTest<VideoTraits> {
2005  public:
2006   typedef ChannelTest<VideoTraits>
2007   Base;
2008   VideoChannelTest() : Base(kH264Packet, sizeof(kH264Packet),
2009                             kRtcpReport, sizeof(kRtcpReport)) {
2010   }
2011
2012   void TestSetChannelOptions() {
2013     CreateChannels(0, 0);
2014
2015     cricket::VideoOptions o1, o2;
2016     o1.video_noise_reduction.Set(true);
2017
2018     channel1_->SetChannelOptions(o1);
2019     channel2_->SetChannelOptions(o1);
2020     EXPECT_TRUE(media_channel1_->GetOptions(&o2));
2021     EXPECT_EQ(o1, o2);
2022     EXPECT_TRUE(media_channel2_->GetOptions(&o2));
2023     EXPECT_EQ(o1, o2);
2024
2025     o1.video_leaky_bucket.Set(true);
2026     channel1_->SetChannelOptions(o1);
2027     channel2_->SetChannelOptions(o1);
2028     EXPECT_TRUE(media_channel1_->GetOptions(&o2));
2029     EXPECT_EQ(o1, o2);
2030     EXPECT_TRUE(media_channel2_->GetOptions(&o2));
2031     EXPECT_EQ(o1, o2);
2032   }
2033 };
2034
2035
2036 // VoiceChannelTest
2037
2038 TEST_F(VoiceChannelTest, TestInit) {
2039   Base::TestInit();
2040   EXPECT_FALSE(media_channel1_->IsStreamMuted(0));
2041   EXPECT_TRUE(media_channel1_->dtmf_info_queue().empty());
2042 }
2043
2044 TEST_F(VoiceChannelTest, TestSetContents) {
2045   Base::TestSetContents();
2046 }
2047
2048 TEST_F(VoiceChannelTest, TestSetContentsNullOffer) {
2049   Base::TestSetContentsNullOffer();
2050 }
2051
2052 TEST_F(VoiceChannelTest, TestSetContentsRtcpMux) {
2053   Base::TestSetContentsRtcpMux();
2054 }
2055
2056 TEST_F(VoiceChannelTest, TestSetContentsRtcpMuxWithPrAnswer) {
2057   Base::TestSetContentsRtcpMux();
2058 }
2059
2060 TEST_F(VoiceChannelTest, TestSetRemoteContentUpdate) {
2061   Base::TestSetRemoteContentUpdate();
2062 }
2063
2064 TEST_F(VoiceChannelTest, TestStreams) {
2065   Base::TestStreams();
2066 }
2067
2068 TEST_F(VoiceChannelTest, TestUpdateStreamsInLocalContent) {
2069   Base::TestUpdateStreamsInLocalContent();
2070 }
2071
2072 TEST_F(VoiceChannelTest, TestUpdateRemoteStreamsInContent) {
2073   Base::TestUpdateStreamsInRemoteContent();
2074 }
2075
2076 TEST_F(VoiceChannelTest, TestChangeStreamParamsInContent) {
2077   Base::TestChangeStreamParamsInContent();
2078 }
2079
2080 TEST_F(VoiceChannelTest, TestPlayoutAndSendingStates) {
2081   Base::TestPlayoutAndSendingStates();
2082 }
2083
2084 TEST_F(VoiceChannelTest, TestMuteStream) {
2085   Base::TestMuteStream();
2086 }
2087
2088 TEST_F(VoiceChannelTest, TestMediaContentDirection) {
2089   Base::TestMediaContentDirection();
2090 }
2091
2092 TEST_F(VoiceChannelTest, TestCallSetup) {
2093   Base::TestCallSetup();
2094 }
2095
2096 TEST_F(VoiceChannelTest, TestCallTeardownRtcpMux) {
2097   Base::TestCallTeardownRtcpMux();
2098 }
2099
2100 TEST_F(VoiceChannelTest, SendRtpToRtp) {
2101   Base::SendRtpToRtp();
2102 }
2103
2104 TEST_F(VoiceChannelTest, SendNoRtcpToNoRtcp) {
2105   Base::SendNoRtcpToNoRtcp();
2106 }
2107
2108 TEST_F(VoiceChannelTest, SendNoRtcpToRtcp) {
2109   Base::SendNoRtcpToRtcp();
2110 }
2111
2112 TEST_F(VoiceChannelTest, SendRtcpToNoRtcp) {
2113   Base::SendRtcpToNoRtcp();
2114 }
2115
2116 TEST_F(VoiceChannelTest, SendRtcpToRtcp) {
2117   Base::SendRtcpToRtcp();
2118 }
2119
2120 TEST_F(VoiceChannelTest, SendRtcpMuxToRtcp) {
2121   Base::SendRtcpMuxToRtcp();
2122 }
2123
2124 TEST_F(VoiceChannelTest, SendRtcpMuxToRtcpMux) {
2125   Base::SendRtcpMuxToRtcpMux();
2126 }
2127
2128 TEST_F(VoiceChannelTest, SendEarlyRtcpMuxToRtcp) {
2129   Base::SendEarlyRtcpMuxToRtcp();
2130 }
2131
2132 TEST_F(VoiceChannelTest, SendEarlyRtcpMuxToRtcpMux) {
2133   Base::SendEarlyRtcpMuxToRtcpMux();
2134 }
2135
2136 TEST_F(VoiceChannelTest, SendSrtpToSrtpRtcpMux) {
2137   Base::SendSrtpToSrtp(RTCP_MUX, RTCP_MUX);
2138 }
2139
2140 TEST_F(VoiceChannelTest, SendSrtpToRtp) {
2141   Base::SendSrtpToSrtp();
2142 }
2143
2144 TEST_F(VoiceChannelTest, SendSrtcpMux) {
2145   Base::SendSrtpToSrtp(RTCP_MUX, RTCP_MUX);
2146 }
2147
2148 TEST_F(VoiceChannelTest, SendDtlsSrtpToSrtp) {
2149   MAYBE_SKIP_TEST(HaveDtlsSrtp);
2150   Base::SendSrtpToSrtp(DTLS, 0);
2151 }
2152
2153 TEST_F(VoiceChannelTest, SendDtlsSrtpToDtlsSrtp) {
2154   MAYBE_SKIP_TEST(HaveDtlsSrtp);
2155   Base::SendSrtpToSrtp(DTLS, DTLS);
2156 }
2157
2158 TEST_F(VoiceChannelTest, SendDtlsSrtpToDtlsSrtpRtcpMux) {
2159   MAYBE_SKIP_TEST(HaveDtlsSrtp);
2160   Base::SendSrtpToSrtp(DTLS | RTCP_MUX, DTLS | RTCP_MUX);
2161 }
2162
2163 TEST_F(VoiceChannelTest, SendEarlyMediaUsingRtcpMuxSrtp) {
2164   Base::SendEarlyMediaUsingRtcpMuxSrtp();
2165 }
2166
2167 TEST_F(VoiceChannelTest, SendRtpToRtpOnThread) {
2168   Base::SendRtpToRtpOnThread();
2169 }
2170
2171 TEST_F(VoiceChannelTest, SendSrtpToSrtpOnThread) {
2172   Base::SendSrtpToSrtpOnThread();
2173 }
2174
2175 TEST_F(VoiceChannelTest, SendWithWritabilityLoss) {
2176   Base::SendWithWritabilityLoss();
2177 }
2178
2179 TEST_F(VoiceChannelTest, TestMediaMonitor) {
2180   Base::TestMediaMonitor();
2181 }
2182
2183 // Test that MuteStream properly forwards to the media channel and does
2184 // not signal.
2185 TEST_F(VoiceChannelTest, TestVoiceSpecificMuteStream) {
2186   CreateChannels(0, 0);
2187   EXPECT_FALSE(media_channel1_->IsStreamMuted(0));
2188   EXPECT_FALSE(mute_callback_recved_);
2189   EXPECT_TRUE(channel1_->MuteStream(0, true));
2190   EXPECT_TRUE(media_channel1_->IsStreamMuted(0));
2191   EXPECT_FALSE(mute_callback_recved_);
2192   EXPECT_TRUE(channel1_->MuteStream(0, false));
2193   EXPECT_FALSE(media_channel1_->IsStreamMuted(0));
2194   EXPECT_FALSE(mute_callback_recved_);
2195 }
2196
2197 // Test that keyboard automute works correctly and signals upwards.
2198 TEST_F(VoiceChannelTest, DISABLED_TestKeyboardMute) {
2199   CreateChannels(0, 0);
2200   EXPECT_FALSE(media_channel1_->IsStreamMuted(0));
2201   EXPECT_EQ(cricket::VoiceMediaChannel::ERROR_NONE, error_);
2202
2203   cricket::VoiceMediaChannel::Error e =
2204       cricket::VoiceMediaChannel::ERROR_REC_TYPING_NOISE_DETECTED;
2205
2206   // Typing doesn't mute automatically unless typing monitor has been installed
2207   media_channel1_->TriggerError(0, e);
2208   talk_base::Thread::Current()->ProcessMessages(0);
2209   EXPECT_EQ(e, error_);
2210   EXPECT_FALSE(media_channel1_->IsStreamMuted(0));
2211   EXPECT_FALSE(mute_callback_recved_);
2212
2213   cricket::TypingMonitorOptions o = {0};
2214   o.mute_period = 1500;
2215   channel1_->StartTypingMonitor(o);
2216   media_channel1_->TriggerError(0, e);
2217   talk_base::Thread::Current()->ProcessMessages(0);
2218   EXPECT_TRUE(media_channel1_->IsStreamMuted(0));
2219   EXPECT_TRUE(mute_callback_recved_);
2220 }
2221
2222 // Test that PressDTMF properly forwards to the media channel.
2223 TEST_F(VoiceChannelTest, TestDtmf) {
2224   CreateChannels(0, 0);
2225   EXPECT_TRUE(SendInitiate());
2226   EXPECT_TRUE(SendAccept());
2227   EXPECT_EQ(0U, media_channel1_->dtmf_info_queue().size());
2228
2229   EXPECT_TRUE(channel1_->PressDTMF(1, true));
2230   EXPECT_TRUE(channel1_->PressDTMF(8, false));
2231
2232   ASSERT_EQ(2U, media_channel1_->dtmf_info_queue().size());
2233   EXPECT_TRUE(CompareDtmfInfo(media_channel1_->dtmf_info_queue()[0],
2234                               0, 1, 160, cricket::DF_PLAY | cricket::DF_SEND));
2235   EXPECT_TRUE(CompareDtmfInfo(media_channel1_->dtmf_info_queue()[1],
2236                               0, 8, 160, cricket::DF_SEND));
2237 }
2238
2239 // Test that InsertDtmf properly forwards to the media channel.
2240 TEST_F(VoiceChannelTest, TestInsertDtmf) {
2241   CreateChannels(0, 0);
2242   EXPECT_TRUE(SendInitiate());
2243   EXPECT_TRUE(SendAccept());
2244   EXPECT_EQ(0U, media_channel1_->dtmf_info_queue().size());
2245
2246   EXPECT_TRUE(channel1_->InsertDtmf(1, 3, 100, cricket::DF_SEND));
2247   EXPECT_TRUE(channel1_->InsertDtmf(2, 5, 110, cricket::DF_PLAY));
2248   EXPECT_TRUE(channel1_->InsertDtmf(3, 7, 120,
2249                                     cricket::DF_PLAY | cricket::DF_SEND));
2250
2251   ASSERT_EQ(3U, media_channel1_->dtmf_info_queue().size());
2252   EXPECT_TRUE(CompareDtmfInfo(media_channel1_->dtmf_info_queue()[0],
2253                               1, 3, 100, cricket::DF_SEND));
2254   EXPECT_TRUE(CompareDtmfInfo(media_channel1_->dtmf_info_queue()[1],
2255                               2, 5, 110, cricket::DF_PLAY));
2256   EXPECT_TRUE(CompareDtmfInfo(media_channel1_->dtmf_info_queue()[2],
2257                               3, 7, 120, cricket::DF_PLAY | cricket::DF_SEND));
2258 }
2259
2260 TEST_F(VoiceChannelTest, TestMediaSinks) {
2261   Base::TestMediaSinks();
2262 }
2263
2264 TEST_F(VoiceChannelTest, TestSetContentFailure) {
2265   Base::TestSetContentFailure();
2266 }
2267
2268 TEST_F(VoiceChannelTest, TestSendTwoOffers) {
2269   Base::TestSendTwoOffers();
2270 }
2271
2272 TEST_F(VoiceChannelTest, TestReceiveTwoOffers) {
2273   Base::TestReceiveTwoOffers();
2274 }
2275
2276 TEST_F(VoiceChannelTest, TestSendPrAnswer) {
2277   Base::TestSendPrAnswer();
2278 }
2279
2280 TEST_F(VoiceChannelTest, TestReceivePrAnswer) {
2281   Base::TestReceivePrAnswer();
2282 }
2283
2284 TEST_F(VoiceChannelTest, TestFlushRtcp) {
2285   Base::TestFlushRtcp();
2286 }
2287
2288 TEST_F(VoiceChannelTest, TestChangeStateError) {
2289   Base::TestChangeStateError();
2290 }
2291
2292 TEST_F(VoiceChannelTest, TestSrtpError) {
2293   Base::TestSrtpError(kAudioPts[0]);
2294 }
2295
2296 TEST_F(VoiceChannelTest, TestOnReadyToSend) {
2297   Base::TestOnReadyToSend();
2298 }
2299
2300 TEST_F(VoiceChannelTest, TestOnReadyToSendWithRtcpMux) {
2301   Base::TestOnReadyToSendWithRtcpMux();
2302 }
2303
2304 // Test that we can play a ringback tone properly.
2305 TEST_F(VoiceChannelTest, TestRingbackTone) {
2306   CreateChannels(RTCP, RTCP);
2307   EXPECT_FALSE(media_channel1_->ringback_tone_play());
2308   EXPECT_TRUE(channel1_->SetRingbackTone("RIFF", 4));
2309   EXPECT_TRUE(SendInitiate());
2310   EXPECT_TRUE(SendAccept());
2311   // Play ringback tone, no loop.
2312   EXPECT_TRUE(channel1_->PlayRingbackTone(0, true, false));
2313   EXPECT_EQ(0U, media_channel1_->ringback_tone_ssrc());
2314   EXPECT_TRUE(media_channel1_->ringback_tone_play());
2315   EXPECT_FALSE(media_channel1_->ringback_tone_loop());
2316   // Stop the ringback tone.
2317   EXPECT_TRUE(channel1_->PlayRingbackTone(0, false, false));
2318   EXPECT_FALSE(media_channel1_->ringback_tone_play());
2319   // Add a stream.
2320   EXPECT_TRUE(AddStream1(1));
2321   // Play ringback tone, looping, on the new stream.
2322   EXPECT_TRUE(channel1_->PlayRingbackTone(1, true, true));
2323   EXPECT_EQ(1U, media_channel1_->ringback_tone_ssrc());
2324   EXPECT_TRUE(media_channel1_->ringback_tone_play());
2325   EXPECT_TRUE(media_channel1_->ringback_tone_loop());
2326   // Stop the ringback tone.
2327   EXPECT_TRUE(channel1_->PlayRingbackTone(1, false, false));
2328   EXPECT_FALSE(media_channel1_->ringback_tone_play());
2329 }
2330
2331 // Test that we can scale the output volume properly for 1:1 calls.
2332 TEST_F(VoiceChannelTest, TestScaleVolume1to1Call) {
2333   CreateChannels(RTCP, RTCP);
2334   EXPECT_TRUE(SendInitiate());
2335   EXPECT_TRUE(SendAccept());
2336   double left, right;
2337
2338   // Default is (1.0, 1.0).
2339   EXPECT_TRUE(media_channel1_->GetOutputScaling(0, &left, &right));
2340   EXPECT_DOUBLE_EQ(1.0, left);
2341   EXPECT_DOUBLE_EQ(1.0, right);
2342   // invalid ssrc.
2343   EXPECT_FALSE(media_channel1_->GetOutputScaling(3, &left, &right));
2344
2345   // Set scale to (1.5, 0.5).
2346   EXPECT_TRUE(channel1_->SetOutputScaling(0, 1.5, 0.5));
2347   EXPECT_TRUE(media_channel1_->GetOutputScaling(0, &left, &right));
2348   EXPECT_DOUBLE_EQ(1.5, left);
2349   EXPECT_DOUBLE_EQ(0.5, right);
2350
2351   // Set scale to (0, 0).
2352   EXPECT_TRUE(channel1_->SetOutputScaling(0, 0.0, 0.0));
2353   EXPECT_TRUE(media_channel1_->GetOutputScaling(0, &left, &right));
2354   EXPECT_DOUBLE_EQ(0.0, left);
2355   EXPECT_DOUBLE_EQ(0.0, right);
2356 }
2357
2358 // Test that we can scale the output volume properly for multiway calls.
2359 TEST_F(VoiceChannelTest, TestScaleVolumeMultiwayCall) {
2360   CreateChannels(RTCP, RTCP);
2361   EXPECT_TRUE(SendInitiate());
2362   EXPECT_TRUE(SendAccept());
2363   EXPECT_TRUE(AddStream1(1));
2364   EXPECT_TRUE(AddStream1(2));
2365
2366   double left, right;
2367   // Default is (1.0, 1.0).
2368   EXPECT_TRUE(media_channel1_->GetOutputScaling(0, &left, &right));
2369   EXPECT_DOUBLE_EQ(1.0, left);
2370   EXPECT_DOUBLE_EQ(1.0, right);
2371   EXPECT_TRUE(media_channel1_->GetOutputScaling(1, &left, &right));
2372   EXPECT_DOUBLE_EQ(1.0, left);
2373   EXPECT_DOUBLE_EQ(1.0, right);
2374   EXPECT_TRUE(media_channel1_->GetOutputScaling(2, &left, &right));
2375   EXPECT_DOUBLE_EQ(1.0, left);
2376   EXPECT_DOUBLE_EQ(1.0, right);
2377   // invalid ssrc.
2378   EXPECT_FALSE(media_channel1_->GetOutputScaling(3, &left, &right));
2379
2380   // Set scale to (1.5, 0.5) for ssrc = 1.
2381   EXPECT_TRUE(channel1_->SetOutputScaling(1, 1.5, 0.5));
2382   EXPECT_TRUE(media_channel1_->GetOutputScaling(1, &left, &right));
2383   EXPECT_DOUBLE_EQ(1.5, left);
2384   EXPECT_DOUBLE_EQ(0.5, right);
2385   EXPECT_TRUE(media_channel1_->GetOutputScaling(2, &left, &right));
2386   EXPECT_DOUBLE_EQ(1.0, left);
2387   EXPECT_DOUBLE_EQ(1.0, right);
2388   EXPECT_TRUE(media_channel1_->GetOutputScaling(0, &left, &right));
2389   EXPECT_DOUBLE_EQ(1.0, left);
2390   EXPECT_DOUBLE_EQ(1.0, right);
2391
2392   // Set scale to (0, 0) for all ssrcs.
2393   EXPECT_TRUE(channel1_->SetOutputScaling(0,  0.0, 0.0));
2394   EXPECT_TRUE(media_channel1_->GetOutputScaling(0, &left, &right));
2395   EXPECT_DOUBLE_EQ(0.0, left);
2396   EXPECT_DOUBLE_EQ(0.0, right);
2397   EXPECT_TRUE(media_channel1_->GetOutputScaling(1, &left, &right));
2398   EXPECT_DOUBLE_EQ(0.0, left);
2399   EXPECT_DOUBLE_EQ(0.0, right);
2400   EXPECT_TRUE(media_channel1_->GetOutputScaling(2, &left, &right));
2401   EXPECT_DOUBLE_EQ(0.0, left);
2402   EXPECT_DOUBLE_EQ(0.0, right);
2403 }
2404
2405 TEST_F(VoiceChannelTest, SendBundleToBundle) {
2406   Base::SendBundleToBundle(kAudioPts, ARRAY_SIZE(kAudioPts), false, false);
2407 }
2408
2409 TEST_F(VoiceChannelTest, SendBundleToBundleSecure) {
2410   Base::SendBundleToBundle(kAudioPts, ARRAY_SIZE(kAudioPts), false, true);
2411 }
2412
2413 TEST_F(VoiceChannelTest, SendBundleToBundleWithRtcpMux) {
2414   Base::SendBundleToBundle(
2415       kAudioPts, ARRAY_SIZE(kAudioPts), true, false);
2416 }
2417
2418 TEST_F(VoiceChannelTest, SendBundleToBundleWithRtcpMuxSecure) {
2419   Base::SendBundleToBundle(
2420       kAudioPts, ARRAY_SIZE(kAudioPts), true, true);
2421 }
2422
2423 TEST_F(VoiceChannelTest, TestSetChannelOptions) {
2424   TestSetChannelOptions();
2425 }
2426
2427 // VideoChannelTest
2428 TEST_F(VideoChannelTest, TestInit) {
2429   Base::TestInit();
2430 }
2431
2432 TEST_F(VideoChannelTest, TestSetContents) {
2433   Base::TestSetContents();
2434 }
2435
2436 TEST_F(VideoChannelTest, TestSetContentsNullOffer) {
2437   Base::TestSetContentsNullOffer();
2438 }
2439
2440 TEST_F(VideoChannelTest, TestSetContentsRtcpMux) {
2441   Base::TestSetContentsRtcpMux();
2442 }
2443
2444 TEST_F(VideoChannelTest, TestSetContentsRtcpMuxWithPrAnswer) {
2445   Base::TestSetContentsRtcpMux();
2446 }
2447
2448 TEST_F(VideoChannelTest, TestSetContentsVideoOptions) {
2449   Base::TestSetContentsVideoOptions();
2450 }
2451
2452 TEST_F(VideoChannelTest, TestSetRemoteContentUpdate) {
2453   Base::TestSetRemoteContentUpdate();
2454 }
2455
2456 TEST_F(VideoChannelTest, TestStreams) {
2457   Base::TestStreams();
2458 }
2459
2460 TEST_F(VideoChannelTest, TestScreencastEvents) {
2461   const int kTimeoutMs = 500;
2462   TestInit();
2463   FakeScreenCaptureFactory* screencapture_factory =
2464       new FakeScreenCaptureFactory();
2465   channel1_->SetScreenCaptureFactory(screencapture_factory);
2466   cricket::ScreencastEventCatcher catcher;
2467   channel1_->SignalScreencastWindowEvent.connect(
2468       &catcher,
2469       &cricket::ScreencastEventCatcher::OnEvent);
2470   EXPECT_TRUE(channel1_->AddScreencast(0, ScreencastId(WindowId(0))) != NULL);
2471   ASSERT_TRUE(screencapture_factory->window_capturer() != NULL);
2472   EXPECT_EQ_WAIT(cricket::CS_STOPPED, screencapture_factory->capture_state(),
2473                  kTimeoutMs);
2474   screencapture_factory->window_capturer()->SignalStateChange(
2475       screencapture_factory->window_capturer(), cricket::CS_PAUSED);
2476   EXPECT_EQ_WAIT(talk_base::WE_MINIMIZE, catcher.event(), kTimeoutMs);
2477   screencapture_factory->window_capturer()->SignalStateChange(
2478       screencapture_factory->window_capturer(), cricket::CS_RUNNING);
2479   EXPECT_EQ_WAIT(talk_base::WE_RESTORE, catcher.event(), kTimeoutMs);
2480   screencapture_factory->window_capturer()->SignalStateChange(
2481       screencapture_factory->window_capturer(), cricket::CS_STOPPED);
2482   EXPECT_EQ_WAIT(talk_base::WE_CLOSE, catcher.event(), kTimeoutMs);
2483   EXPECT_TRUE(channel1_->RemoveScreencast(0));
2484   ASSERT_TRUE(screencapture_factory->window_capturer() == NULL);
2485 }
2486
2487 TEST_F(VideoChannelTest, TestUpdateStreamsInLocalContent) {
2488   Base::TestUpdateStreamsInLocalContent();
2489 }
2490
2491 TEST_F(VideoChannelTest, TestUpdateRemoteStreamsInContent) {
2492   Base::TestUpdateStreamsInRemoteContent();
2493 }
2494
2495 TEST_F(VideoChannelTest, TestChangeStreamParamsInContent) {
2496   Base::TestChangeStreamParamsInContent();
2497 }
2498
2499 TEST_F(VideoChannelTest, TestPlayoutAndSendingStates) {
2500   Base::TestPlayoutAndSendingStates();
2501 }
2502
2503 TEST_F(VideoChannelTest, TestMuteStream) {
2504   Base::TestMuteStream();
2505 }
2506
2507 TEST_F(VideoChannelTest, TestMediaContentDirection) {
2508   Base::TestMediaContentDirection();
2509 }
2510
2511 TEST_F(VideoChannelTest, TestCallSetup) {
2512   Base::TestCallSetup();
2513 }
2514
2515 TEST_F(VideoChannelTest, TestCallTeardownRtcpMux) {
2516   Base::TestCallTeardownRtcpMux();
2517 }
2518
2519 TEST_F(VideoChannelTest, SendRtpToRtp) {
2520   Base::SendRtpToRtp();
2521 }
2522
2523 TEST_F(VideoChannelTest, SendNoRtcpToNoRtcp) {
2524   Base::SendNoRtcpToNoRtcp();
2525 }
2526
2527 TEST_F(VideoChannelTest, SendNoRtcpToRtcp) {
2528   Base::SendNoRtcpToRtcp();
2529 }
2530
2531 TEST_F(VideoChannelTest, SendRtcpToNoRtcp) {
2532   Base::SendRtcpToNoRtcp();
2533 }
2534
2535 TEST_F(VideoChannelTest, SendRtcpToRtcp) {
2536   Base::SendRtcpToRtcp();
2537 }
2538
2539 TEST_F(VideoChannelTest, SendRtcpMuxToRtcp) {
2540   Base::SendRtcpMuxToRtcp();
2541 }
2542
2543 TEST_F(VideoChannelTest, SendRtcpMuxToRtcpMux) {
2544   Base::SendRtcpMuxToRtcpMux();
2545 }
2546
2547 TEST_F(VideoChannelTest, SendEarlyRtcpMuxToRtcp) {
2548   Base::SendEarlyRtcpMuxToRtcp();
2549 }
2550
2551 TEST_F(VideoChannelTest, SendEarlyRtcpMuxToRtcpMux) {
2552   Base::SendEarlyRtcpMuxToRtcpMux();
2553 }
2554
2555 TEST_F(VideoChannelTest, SendSrtpToSrtp) {
2556   Base::SendSrtpToSrtp();
2557 }
2558
2559 TEST_F(VideoChannelTest, SendSrtpToRtp) {
2560   Base::SendSrtpToSrtp();
2561 }
2562
2563 TEST_F(VideoChannelTest, SendDtlsSrtpToSrtp) {
2564   MAYBE_SKIP_TEST(HaveDtlsSrtp);
2565   Base::SendSrtpToSrtp(DTLS, 0);
2566 }
2567
2568 TEST_F(VideoChannelTest, SendDtlsSrtpToDtlsSrtp) {
2569   MAYBE_SKIP_TEST(HaveDtlsSrtp);
2570   Base::SendSrtpToSrtp(DTLS, DTLS);
2571 }
2572
2573 TEST_F(VideoChannelTest, SendDtlsSrtpToDtlsSrtpRtcpMux) {
2574   MAYBE_SKIP_TEST(HaveDtlsSrtp);
2575   Base::SendSrtpToSrtp(DTLS | RTCP_MUX, DTLS | RTCP_MUX);
2576 }
2577
2578 TEST_F(VideoChannelTest, SendSrtcpMux) {
2579   Base::SendSrtpToSrtp(RTCP_MUX, RTCP_MUX);
2580 }
2581
2582 TEST_F(VideoChannelTest, SendEarlyMediaUsingRtcpMuxSrtp) {
2583   Base::SendEarlyMediaUsingRtcpMuxSrtp();
2584 }
2585
2586 TEST_F(VideoChannelTest, SendRtpToRtpOnThread) {
2587   Base::SendRtpToRtpOnThread();
2588 }
2589
2590 TEST_F(VideoChannelTest, SendSrtpToSrtpOnThread) {
2591   Base::SendSrtpToSrtpOnThread();
2592 }
2593
2594 TEST_F(VideoChannelTest, SendWithWritabilityLoss) {
2595   Base::SendWithWritabilityLoss();
2596 }
2597
2598 TEST_F(VideoChannelTest, TestMediaMonitor) {
2599   Base::TestMediaMonitor();
2600 }
2601
2602 TEST_F(VideoChannelTest, TestMediaSinks) {
2603   Base::TestMediaSinks();
2604 }
2605
2606 TEST_F(VideoChannelTest, TestSetContentFailure) {
2607   Base::TestSetContentFailure();
2608 }
2609
2610 TEST_F(VideoChannelTest, TestSendTwoOffers) {
2611   Base::TestSendTwoOffers();
2612 }
2613
2614 TEST_F(VideoChannelTest, TestReceiveTwoOffers) {
2615   Base::TestReceiveTwoOffers();
2616 }
2617
2618 TEST_F(VideoChannelTest, TestSendPrAnswer) {
2619   Base::TestSendPrAnswer();
2620 }
2621
2622 TEST_F(VideoChannelTest, TestReceivePrAnswer) {
2623   Base::TestReceivePrAnswer();
2624 }
2625
2626 TEST_F(VideoChannelTest, TestFlushRtcp) {
2627   Base::TestFlushRtcp();
2628 }
2629
2630 TEST_F(VideoChannelTest, SendBundleToBundle) {
2631   Base::SendBundleToBundle(kVideoPts, ARRAY_SIZE(kVideoPts), false, false);
2632 }
2633
2634 TEST_F(VideoChannelTest, SendBundleToBundleSecure) {
2635   Base::SendBundleToBundle(kVideoPts, ARRAY_SIZE(kVideoPts), false, true);
2636 }
2637
2638 TEST_F(VideoChannelTest, SendBundleToBundleWithRtcpMux) {
2639   Base::SendBundleToBundle(
2640       kVideoPts, ARRAY_SIZE(kVideoPts), true, false);
2641 }
2642
2643 TEST_F(VideoChannelTest, SendBundleToBundleWithRtcpMuxSecure) {
2644   Base::SendBundleToBundle(
2645       kVideoPts, ARRAY_SIZE(kVideoPts), true, true);
2646 }
2647
2648 // TODO(gangji): Add VideoChannelTest.TestChangeStateError.
2649
2650 TEST_F(VideoChannelTest, TestSrtpError) {
2651   Base::TestSrtpError(kVideoPts[0]);
2652 }
2653
2654 TEST_F(VideoChannelTest, TestOnReadyToSend) {
2655   Base::TestOnReadyToSend();
2656 }
2657
2658 TEST_F(VideoChannelTest, TestOnReadyToSendWithRtcpMux) {
2659   Base::TestOnReadyToSendWithRtcpMux();
2660 }
2661
2662 TEST_F(VideoChannelTest, TestApplyViewRequest) {
2663   CreateChannels(0, 0);
2664   cricket::StreamParams stream2;
2665   stream2.id = "stream2";
2666   stream2.ssrcs.push_back(2222);
2667   local_media_content1_.AddStream(stream2);
2668
2669   EXPECT_TRUE(SendInitiate());
2670   EXPECT_TRUE(SendAccept());
2671
2672   cricket::VideoFormat send_format;
2673   EXPECT_TRUE(media_channel1_->GetSendStreamFormat(kSsrc1, &send_format));
2674   EXPECT_EQ(640, send_format.width);
2675   EXPECT_EQ(400, send_format.height);
2676   EXPECT_EQ(cricket::VideoFormat::FpsToInterval(30), send_format.interval);
2677
2678   cricket::ViewRequest request;
2679   // stream1: 320x200x15; stream2: 0x0x0
2680   request.static_video_views.push_back(cricket::StaticVideoView(
2681       cricket::StreamSelector(kSsrc1), 320, 200, 15));
2682   EXPECT_TRUE(channel1_->ApplyViewRequest(request));
2683   EXPECT_TRUE(media_channel1_->GetSendStreamFormat(kSsrc1, &send_format));
2684   EXPECT_EQ(320, send_format.width);
2685   EXPECT_EQ(200, send_format.height);
2686   EXPECT_EQ(cricket::VideoFormat::FpsToInterval(15), send_format.interval);
2687   EXPECT_TRUE(media_channel1_->GetSendStreamFormat(2222, &send_format));
2688   EXPECT_EQ(0, send_format.width);
2689   EXPECT_EQ(0, send_format.height);
2690
2691   // stream1: 160x100x8; stream2: 0x0x0
2692   request.static_video_views.clear();
2693   request.static_video_views.push_back(cricket::StaticVideoView(
2694       cricket::StreamSelector(kSsrc1), 160, 100, 8));
2695   EXPECT_TRUE(channel1_->ApplyViewRequest(request));
2696   EXPECT_TRUE(media_channel1_->GetSendStreamFormat(kSsrc1, &send_format));
2697   EXPECT_EQ(160, send_format.width);
2698   EXPECT_EQ(100, send_format.height);
2699   EXPECT_EQ(cricket::VideoFormat::FpsToInterval(8), send_format.interval);
2700
2701   // stream1: 0x0x0; stream2: 640x400x30
2702   request.static_video_views.clear();
2703   request.static_video_views.push_back(cricket::StaticVideoView(
2704       cricket::StreamSelector("", stream2.id), 640, 400, 30));
2705   EXPECT_TRUE(channel1_->ApplyViewRequest(request));
2706   EXPECT_TRUE(media_channel1_->GetSendStreamFormat(kSsrc1, &send_format));
2707   EXPECT_EQ(0, send_format.width);
2708   EXPECT_EQ(0, send_format.height);
2709   EXPECT_TRUE(media_channel1_->GetSendStreamFormat(2222, &send_format));
2710   EXPECT_EQ(640, send_format.width);
2711   EXPECT_EQ(400, send_format.height);
2712   EXPECT_EQ(cricket::VideoFormat::FpsToInterval(30), send_format.interval);
2713
2714   // stream1: 0x0x0; stream2: 0x0x0
2715   request.static_video_views.clear();
2716   EXPECT_TRUE(channel1_->ApplyViewRequest(request));
2717   EXPECT_TRUE(media_channel1_->GetSendStreamFormat(kSsrc1, &send_format));
2718   EXPECT_EQ(0, send_format.width);
2719   EXPECT_EQ(0, send_format.height);
2720 }
2721
2722 TEST_F(VideoChannelTest, TestSetChannelOptions) {
2723   TestSetChannelOptions();
2724 }
2725
2726
2727 // DataChannelTest
2728
2729 class DataChannelTest
2730     : public ChannelTest<DataTraits> {
2731  public:
2732   typedef ChannelTest<DataTraits>
2733   Base;
2734   DataChannelTest() : Base(kDataPacket, sizeof(kDataPacket),
2735                            kRtcpReport, sizeof(kRtcpReport)) {
2736   }
2737 };
2738
2739 // Override to avoid engine channel parameter.
2740 template<>
2741 cricket::DataChannel* ChannelTest<DataTraits>::CreateChannel(
2742     talk_base::Thread* thread, cricket::MediaEngineInterface* engine,
2743     cricket::FakeDataMediaChannel* ch, cricket::BaseSession* session,
2744     bool rtcp) {
2745   cricket::DataChannel* channel = new cricket::DataChannel(
2746       thread, ch, session, cricket::CN_DATA, rtcp);
2747   if (!channel->Init()) {
2748     delete channel;
2749     channel = NULL;
2750   }
2751   return channel;
2752 }
2753
2754 template<>
2755 void ChannelTest<DataTraits>::CreateContent(
2756     int flags,
2757     const cricket::AudioCodec& audio_codec,
2758     const cricket::VideoCodec& video_codec,
2759     cricket::DataContentDescription* data) {
2760   data->AddCodec(kGoogleDataCodec);
2761   data->set_rtcp_mux((flags & RTCP_MUX) != 0);
2762   if (flags & SECURE) {
2763     data->AddCrypto(cricket::CryptoParams(
2764         1, cricket::CS_AES_CM_128_HMAC_SHA1_32,
2765         "inline:" + talk_base::CreateRandomString(40), ""));
2766   }
2767 }
2768
2769 template<>
2770 void ChannelTest<DataTraits>::CopyContent(
2771     const cricket::DataContentDescription& source,
2772     cricket::DataContentDescription* data) {
2773   *data = source;
2774 }
2775
2776 template<>
2777 bool ChannelTest<DataTraits>::CodecMatches(const cricket::DataCodec& c1,
2778                                            const cricket::DataCodec& c2) {
2779   return c1.name == c2.name;
2780 }
2781
2782 template<>
2783 void ChannelTest<DataTraits>::AddLegacyStreamInContent(
2784     uint32 ssrc, int flags, cricket::DataContentDescription* data) {
2785   data->AddLegacyStream(ssrc);
2786 }
2787
2788 TEST_F(DataChannelTest, TestInit) {
2789   Base::TestInit();
2790   EXPECT_FALSE(media_channel1_->IsStreamMuted(0));
2791 }
2792
2793 TEST_F(DataChannelTest, TestSetContents) {
2794   Base::TestSetContents();
2795 }
2796
2797 TEST_F(DataChannelTest, TestSetContentsNullOffer) {
2798   Base::TestSetContentsNullOffer();
2799 }
2800
2801 TEST_F(DataChannelTest, TestSetContentsRtcpMux) {
2802   Base::TestSetContentsRtcpMux();
2803 }
2804
2805 TEST_F(DataChannelTest, TestSetRemoteContentUpdate) {
2806   Base::TestSetRemoteContentUpdate();
2807 }
2808
2809 TEST_F(DataChannelTest, TestStreams) {
2810   Base::TestStreams();
2811 }
2812
2813 TEST_F(DataChannelTest, TestUpdateStreamsInLocalContent) {
2814   Base::TestUpdateStreamsInLocalContent();
2815 }
2816
2817 TEST_F(DataChannelTest, TestUpdateRemoteStreamsInContent) {
2818   Base::TestUpdateStreamsInRemoteContent();
2819 }
2820
2821 TEST_F(DataChannelTest, TestChangeStreamParamsInContent) {
2822   Base::TestChangeStreamParamsInContent();
2823 }
2824
2825 TEST_F(DataChannelTest, TestPlayoutAndSendingStates) {
2826   Base::TestPlayoutAndSendingStates();
2827 }
2828
2829 TEST_F(DataChannelTest, TestMediaContentDirection) {
2830   Base::TestMediaContentDirection();
2831 }
2832
2833 TEST_F(DataChannelTest, TestCallSetup) {
2834   Base::TestCallSetup();
2835 }
2836
2837 TEST_F(DataChannelTest, TestCallTeardownRtcpMux) {
2838   Base::TestCallTeardownRtcpMux();
2839 }
2840
2841 TEST_F(DataChannelTest, TestOnReadyToSend) {
2842   Base::TestOnReadyToSend();
2843 }
2844
2845 TEST_F(DataChannelTest, TestOnReadyToSendWithRtcpMux) {
2846   Base::TestOnReadyToSendWithRtcpMux();
2847 }
2848
2849 TEST_F(DataChannelTest, SendRtpToRtp) {
2850   Base::SendRtpToRtp();
2851 }
2852
2853 TEST_F(DataChannelTest, SendNoRtcpToNoRtcp) {
2854   Base::SendNoRtcpToNoRtcp();
2855 }
2856
2857 TEST_F(DataChannelTest, SendNoRtcpToRtcp) {
2858   Base::SendNoRtcpToRtcp();
2859 }
2860
2861 TEST_F(DataChannelTest, SendRtcpToNoRtcp) {
2862   Base::SendRtcpToNoRtcp();
2863 }
2864
2865 TEST_F(DataChannelTest, SendRtcpToRtcp) {
2866   Base::SendRtcpToRtcp();
2867 }
2868
2869 TEST_F(DataChannelTest, SendRtcpMuxToRtcp) {
2870   Base::SendRtcpMuxToRtcp();
2871 }
2872
2873 TEST_F(DataChannelTest, SendRtcpMuxToRtcpMux) {
2874   Base::SendRtcpMuxToRtcpMux();
2875 }
2876
2877 TEST_F(DataChannelTest, SendEarlyRtcpMuxToRtcp) {
2878   Base::SendEarlyRtcpMuxToRtcp();
2879 }
2880
2881 TEST_F(DataChannelTest, SendEarlyRtcpMuxToRtcpMux) {
2882   Base::SendEarlyRtcpMuxToRtcpMux();
2883 }
2884
2885 TEST_F(DataChannelTest, SendSrtpToSrtp) {
2886   Base::SendSrtpToSrtp();
2887 }
2888
2889 TEST_F(DataChannelTest, SendSrtpToRtp) {
2890   Base::SendSrtpToSrtp();
2891 }
2892
2893 TEST_F(DataChannelTest, SendSrtcpMux) {
2894   Base::SendSrtpToSrtp(RTCP_MUX, RTCP_MUX);
2895 }
2896
2897 TEST_F(DataChannelTest, SendRtpToRtpOnThread) {
2898   Base::SendRtpToRtpOnThread();
2899 }
2900
2901 TEST_F(DataChannelTest, SendSrtpToSrtpOnThread) {
2902   Base::SendSrtpToSrtpOnThread();
2903 }
2904
2905 TEST_F(DataChannelTest, SendWithWritabilityLoss) {
2906   Base::SendWithWritabilityLoss();
2907 }
2908
2909 TEST_F(DataChannelTest, TestMediaMonitor) {
2910   Base::TestMediaMonitor();
2911 }
2912
2913 TEST_F(DataChannelTest, TestSendData) {
2914   CreateChannels(0, 0);
2915   EXPECT_TRUE(SendInitiate());
2916   EXPECT_TRUE(SendAccept());
2917
2918   cricket::SendDataParams params;
2919   params.ssrc = 42;
2920   unsigned char data[] = {
2921     'f', 'o', 'o'
2922   };
2923   talk_base::Buffer payload(data, 3);
2924   cricket::SendDataResult result;
2925   ASSERT_TRUE(media_channel1_->SendData(params, payload, &result));
2926   EXPECT_EQ(params.ssrc,
2927             media_channel1_->last_sent_data_params().ssrc);
2928   EXPECT_EQ("foo", media_channel1_->last_sent_data());
2929 }
2930
2931 // TODO(pthatcher): TestSetReceiver?