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