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