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