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