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