blob: deba6a11554f2046770dbbfcf5c5c0e8cc9adeaf [file] [log] [blame]
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00001/*
kjellander65c7f672016-02-12 00:05:01 -08002 * Copyright 2008 The WebRTC project authors. All Rights Reserved.
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 *
kjellander65c7f672016-02-12 00:05:01 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00009 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +000010
deadbeef112b2e92017-02-10 20:13:37 -080011#include <memory>
12
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000013#include "webrtc/base/gunit.h"
14#include "webrtc/base/logging.h"
15#include "webrtc/base/thread.h"
skvlad11a9cbf2016-10-07 11:53:05 -070016#include "webrtc/logging/rtc_event_log/rtc_event_log.h"
kjellandera96e2d72016-02-04 23:52:28 -080017#include "webrtc/media/base/fakemediaengine.h"
18#include "webrtc/media/base/fakevideocapturer.h"
19#include "webrtc/media/base/testutils.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010020#include "webrtc/media/engine/fakewebrtccall.h"
deadbeefcbecd352015-09-23 11:50:27 -070021#include "webrtc/p2p/base/faketransportcontroller.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010022#include "webrtc/pc/channelmanager.h"
ossu7bb87ee2017-01-23 04:56:25 -080023#include "webrtc/pc/fakemediacontroller.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024
25namespace cricket {
deadbeefac22f702017-01-12 21:59:29 -080026const bool kDefaultRtcpMuxRequired = true;
deadbeef7af91dd2016-12-13 11:29:11 -080027const bool kDefaultSrtpRequired = true;
28}
29
30namespace cricket {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031
32static const AudioCodec kAudioCodecs[] = {
deadbeef67cf2c12016-04-13 10:07:16 -070033 AudioCodec(97, "voice", 1, 2, 3), AudioCodec(111, "OPUS", 48000, 32000, 2),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034};
35
36static const VideoCodec kVideoCodecs[] = {
perkj26752742016-10-24 01:21:16 -070037 VideoCodec(99, "H264"), VideoCodec(100, "VP8"), VideoCodec(96, "rtx"),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038};
39
40class ChannelManagerTest : public testing::Test {
41 protected:
deadbeefcbecd352015-09-23 11:50:27 -070042 ChannelManagerTest()
stefanc1aeaf02015-10-15 07:26:07 -070043 : fme_(new cricket::FakeMediaEngine()),
44 fdme_(new cricket::FakeDataEngine()),
deadbeef112b2e92017-02-10 20:13:37 -080045 cm_(new cricket::ChannelManager(
46 std::unique_ptr<MediaEngineInterface>(fme_),
47 std::unique_ptr<DataEngineInterface>(fdme_),
48 rtc::Thread::Current())),
skvlad11a9cbf2016-10-07 11:53:05 -070049 fake_call_(webrtc::Call::Config(&event_log_)),
deadbeef112b2e92017-02-10 20:13:37 -080050 fake_mc_(cm_.get(), &fake_call_),
stefanc1aeaf02015-10-15 07:26:07 -070051 transport_controller_(
deadbeef112b2e92017-02-10 20:13:37 -080052 new cricket::FakeTransportController(ICEROLE_CONTROLLING)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053 fme_->SetAudioCodecs(MAKE_VECTOR(kAudioCodecs));
54 fme_->SetVideoCodecs(MAKE_VECTOR(kVideoCodecs));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055 }
56
skvlad11a9cbf2016-10-07 11:53:05 -070057 webrtc::RtcEventLogNullImpl event_log_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +020058 rtc::Thread network_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000059 rtc::Thread worker_;
deadbeef112b2e92017-02-10 20:13:37 -080060 // |fme_| and |fdme_| are actually owned by |cm_|.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061 cricket::FakeMediaEngine* fme_;
62 cricket::FakeDataEngine* fdme_;
deadbeef112b2e92017-02-10 20:13:37 -080063 std::unique_ptr<cricket::ChannelManager> cm_;
stefanc1aeaf02015-10-15 07:26:07 -070064 cricket::FakeCall fake_call_;
65 cricket::FakeMediaController fake_mc_;
deadbeef112b2e92017-02-10 20:13:37 -080066 std::unique_ptr<cricket::FakeTransportController> transport_controller_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067};
68
69// Test that we startup/shutdown properly.
70TEST_F(ChannelManagerTest, StartupShutdown) {
71 EXPECT_FALSE(cm_->initialized());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000072 EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073 EXPECT_TRUE(cm_->Init());
74 EXPECT_TRUE(cm_->initialized());
75 cm_->Terminate();
76 EXPECT_FALSE(cm_->initialized());
77}
78
79// Test that we startup/shutdown properly with a worker thread.
80TEST_F(ChannelManagerTest, StartupShutdownOnThread) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +020081 network_.Start();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 worker_.Start();
83 EXPECT_FALSE(cm_->initialized());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000084 EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread());
Danil Chapovalov33b01f22016-05-11 19:55:27 +020085 EXPECT_TRUE(cm_->set_network_thread(&network_));
86 EXPECT_EQ(&network_, cm_->network_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087 EXPECT_TRUE(cm_->set_worker_thread(&worker_));
88 EXPECT_EQ(&worker_, cm_->worker_thread());
89 EXPECT_TRUE(cm_->Init());
90 EXPECT_TRUE(cm_->initialized());
Danil Chapovalov33b01f22016-05-11 19:55:27 +020091 // Setting the network or worker thread while initialized should fail.
92 EXPECT_FALSE(cm_->set_network_thread(rtc::Thread::Current()));
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000093 EXPECT_FALSE(cm_->set_worker_thread(rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 cm_->Terminate();
95 EXPECT_FALSE(cm_->initialized());
96}
97
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098// Test that we can create and destroy a voice and video channel.
99TEST_F(ChannelManagerTest, CreateDestroyChannels) {
100 EXPECT_TRUE(cm_->Init());
zhihuangb2cdd932017-01-19 16:54:25 -0800101 cricket::DtlsTransportInternal* rtp_transport =
102 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -0800103 cricket::CN_AUDIO, cricket::ICE_CANDIDATE_COMPONENT_RTP);
skvlad6c87a672016-05-17 17:49:52 -0700104 cricket::VoiceChannel* voice_channel = cm_->CreateVoiceChannel(
zhihuangf5b251b2017-01-12 19:37:48 -0800105 &fake_mc_, rtp_transport, nullptr /*rtcp_transport*/,
deadbeefac22f702017-01-12 21:59:29 -0800106 rtc::Thread::Current(), cricket::CN_AUDIO, nullptr,
107 kDefaultRtcpMuxRequired, kDefaultSrtpRequired, AudioOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200108 EXPECT_TRUE(voice_channel != nullptr);
skvlad6c87a672016-05-17 17:49:52 -0700109 cricket::VideoChannel* video_channel = cm_->CreateVideoChannel(
zhihuangf5b251b2017-01-12 19:37:48 -0800110 &fake_mc_, rtp_transport, nullptr /*rtcp_transport*/,
deadbeefac22f702017-01-12 21:59:29 -0800111 rtc::Thread::Current(), cricket::CN_VIDEO, nullptr,
112 kDefaultRtcpMuxRequired, kDefaultSrtpRequired, VideoOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200113 EXPECT_TRUE(video_channel != nullptr);
deadbeef953c2ce2017-01-09 14:53:41 -0800114 cricket::RtpDataChannel* rtp_data_channel = cm_->CreateRtpDataChannel(
zhihuangf5b251b2017-01-12 19:37:48 -0800115 &fake_mc_, rtp_transport, nullptr /*rtcp_transport*/,
deadbeefac22f702017-01-12 21:59:29 -0800116 rtc::Thread::Current(), cricket::CN_DATA, nullptr,
117 kDefaultRtcpMuxRequired, kDefaultSrtpRequired);
deadbeef953c2ce2017-01-09 14:53:41 -0800118 EXPECT_TRUE(rtp_data_channel != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119 cm_->DestroyVideoChannel(video_channel);
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200120 cm_->DestroyVoiceChannel(voice_channel);
deadbeef953c2ce2017-01-09 14:53:41 -0800121 cm_->DestroyRtpDataChannel(rtp_data_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122 cm_->Terminate();
123}
124
125// Test that we can create and destroy a voice and video channel with a worker.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000126TEST_F(ChannelManagerTest, CreateDestroyChannelsOnThread) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200127 network_.Start();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 worker_.Start();
129 EXPECT_TRUE(cm_->set_worker_thread(&worker_));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200130 EXPECT_TRUE(cm_->set_network_thread(&network_));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 EXPECT_TRUE(cm_->Init());
deadbeef112b2e92017-02-10 20:13:37 -0800132 transport_controller_.reset(
133 new cricket::FakeTransportController(&network_, ICEROLE_CONTROLLING));
zhihuangb2cdd932017-01-19 16:54:25 -0800134 cricket::DtlsTransportInternal* rtp_transport =
135 transport_controller_->CreateDtlsTransport(
zhihuangf5b251b2017-01-12 19:37:48 -0800136 cricket::CN_AUDIO, cricket::ICE_CANDIDATE_COMPONENT_RTP);
skvlad6c87a672016-05-17 17:49:52 -0700137 cricket::VoiceChannel* voice_channel = cm_->CreateVoiceChannel(
zhihuangf5b251b2017-01-12 19:37:48 -0800138 &fake_mc_, rtp_transport, nullptr /*rtcp_transport*/,
deadbeefac22f702017-01-12 21:59:29 -0800139 rtc::Thread::Current(), cricket::CN_AUDIO, nullptr,
140 kDefaultRtcpMuxRequired, kDefaultSrtpRequired, AudioOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200141 EXPECT_TRUE(voice_channel != nullptr);
skvlad6c87a672016-05-17 17:49:52 -0700142 cricket::VideoChannel* video_channel = cm_->CreateVideoChannel(
zhihuangf5b251b2017-01-12 19:37:48 -0800143 &fake_mc_, rtp_transport, nullptr /*rtcp_transport*/,
deadbeefac22f702017-01-12 21:59:29 -0800144 rtc::Thread::Current(), cricket::CN_VIDEO, nullptr,
145 kDefaultRtcpMuxRequired, kDefaultSrtpRequired, VideoOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200146 EXPECT_TRUE(video_channel != nullptr);
deadbeef953c2ce2017-01-09 14:53:41 -0800147 cricket::RtpDataChannel* rtp_data_channel = cm_->CreateRtpDataChannel(
zhihuangf5b251b2017-01-12 19:37:48 -0800148 &fake_mc_, rtp_transport, nullptr /*rtcp_transport*/,
deadbeefac22f702017-01-12 21:59:29 -0800149 rtc::Thread::Current(), cricket::CN_DATA, nullptr,
150 kDefaultRtcpMuxRequired, kDefaultSrtpRequired);
deadbeef953c2ce2017-01-09 14:53:41 -0800151 EXPECT_TRUE(rtp_data_channel != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152 cm_->DestroyVideoChannel(video_channel);
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200153 cm_->DestroyVoiceChannel(voice_channel);
deadbeef953c2ce2017-01-09 14:53:41 -0800154 cm_->DestroyRtpDataChannel(rtp_data_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155 cm_->Terminate();
156}
157
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158TEST_F(ChannelManagerTest, SetVideoRtxEnabled) {
159 std::vector<VideoCodec> codecs;
perkj26752742016-10-24 01:21:16 -0700160 const VideoCodec rtx_codec(96, "rtx");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161
162 // By default RTX is disabled.
magjed3cf8ece2016-11-10 03:36:53 -0800163 cm_->GetSupportedVideoCodecs(&codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164 EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec));
165
166 // Enable and check.
167 EXPECT_TRUE(cm_->SetVideoRtxEnabled(true));
magjed3cf8ece2016-11-10 03:36:53 -0800168 cm_->GetSupportedVideoCodecs(&codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169 EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec));
170
171 // Disable and check.
172 EXPECT_TRUE(cm_->SetVideoRtxEnabled(false));
magjed3cf8ece2016-11-10 03:36:53 -0800173 cm_->GetSupportedVideoCodecs(&codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174 EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec));
175
176 // Cannot toggle rtx after initialization.
177 EXPECT_TRUE(cm_->Init());
178 EXPECT_FALSE(cm_->SetVideoRtxEnabled(true));
179 EXPECT_FALSE(cm_->SetVideoRtxEnabled(false));
180
181 // Can set again after terminate.
182 cm_->Terminate();
183 EXPECT_TRUE(cm_->SetVideoRtxEnabled(true));
magjed3cf8ece2016-11-10 03:36:53 -0800184 cm_->GetSupportedVideoCodecs(&codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec));
186}
187
188} // namespace cricket