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