blob: e5e7b4f7de400821528c4a04e8e4c3f574f9cbf3 [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
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#include "webrtc/api/fakemediacontroller.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000012#include "webrtc/base/gunit.h"
13#include "webrtc/base/logging.h"
14#include "webrtc/base/thread.h"
kjellandera96e2d72016-02-04 23:52:28 -080015#include "webrtc/media/base/fakemediaengine.h"
16#include "webrtc/media/base/fakevideocapturer.h"
17#include "webrtc/media/base/testutils.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010018#include "webrtc/media/engine/fakewebrtccall.h"
deadbeefcbecd352015-09-23 11:50:27 -070019#include "webrtc/p2p/base/faketransportcontroller.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010020#include "webrtc/pc/channelmanager.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
22namespace cricket {
23
24static const AudioCodec kAudioCodecs[] = {
deadbeef67cf2c12016-04-13 10:07:16 -070025 AudioCodec(97, "voice", 1, 2, 3), AudioCodec(111, "OPUS", 48000, 32000, 2),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000026};
27
28static const VideoCodec kVideoCodecs[] = {
deadbeef67cf2c12016-04-13 10:07:16 -070029 VideoCodec(99, "H264", 100, 200, 300),
30 VideoCodec(100, "VP8", 100, 200, 300), VideoCodec(96, "rtx", 100, 200, 300),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031};
32
33class ChannelManagerTest : public testing::Test {
34 protected:
deadbeefcbecd352015-09-23 11:50:27 -070035 ChannelManagerTest()
stefanc1aeaf02015-10-15 07:26:07 -070036 : fme_(new cricket::FakeMediaEngine()),
37 fdme_(new cricket::FakeDataEngine()),
stefanc1aeaf02015-10-15 07:26:07 -070038 cm_(new cricket::ChannelManager(fme_,
39 fdme_,
stefanc1aeaf02015-10-15 07:26:07 -070040 rtc::Thread::Current())),
41 fake_call_(webrtc::Call::Config()),
42 fake_mc_(cm_, &fake_call_),
43 transport_controller_(
44 new cricket::FakeTransportController(ICEROLE_CONTROLLING)) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045
46 virtual void SetUp() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047 fme_->SetAudioCodecs(MAKE_VECTOR(kAudioCodecs));
48 fme_->SetVideoCodecs(MAKE_VECTOR(kVideoCodecs));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049 }
50
51 virtual void TearDown() {
deadbeefcbecd352015-09-23 11:50:27 -070052 delete transport_controller_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053 delete cm_;
54 cm_ = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055 fdme_ = NULL;
56 fme_ = NULL;
57 }
58
Danil Chapovalov33b01f22016-05-11 19:55:27 +020059 rtc::Thread network_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000060 rtc::Thread worker_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061 cricket::FakeMediaEngine* fme_;
62 cricket::FakeDataEngine* fdme_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063 cricket::ChannelManager* cm_;
stefanc1aeaf02015-10-15 07:26:07 -070064 cricket::FakeCall fake_call_;
65 cricket::FakeMediaController fake_mc_;
deadbeefcbecd352015-09-23 11:50:27 -070066 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());
skvlad6c87a672016-05-17 17:49:52 -0700101 cricket::VoiceChannel* voice_channel = cm_->CreateVoiceChannel(
102 &fake_mc_, transport_controller_, cricket::CN_AUDIO, nullptr, false,
103 AudioOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200104 EXPECT_TRUE(voice_channel != nullptr);
skvlad6c87a672016-05-17 17:49:52 -0700105 cricket::VideoChannel* video_channel = cm_->CreateVideoChannel(
106 &fake_mc_, transport_controller_, cricket::CN_VIDEO, nullptr, false,
107 VideoOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200108 EXPECT_TRUE(video_channel != nullptr);
skvlad6c87a672016-05-17 17:49:52 -0700109 cricket::DataChannel* data_channel =
110 cm_->CreateDataChannel(transport_controller_, cricket::CN_DATA, nullptr,
111 false, cricket::DCT_RTP);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200112 EXPECT_TRUE(data_channel != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 cm_->DestroyVideoChannel(video_channel);
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200114 cm_->DestroyVoiceChannel(voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 cm_->DestroyDataChannel(data_channel);
116 cm_->Terminate();
117}
118
119// Test that we can create and destroy a voice and video channel with a worker.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000120TEST_F(ChannelManagerTest, CreateDestroyChannelsOnThread) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200121 network_.Start();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122 worker_.Start();
123 EXPECT_TRUE(cm_->set_worker_thread(&worker_));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200124 EXPECT_TRUE(cm_->set_network_thread(&network_));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 EXPECT_TRUE(cm_->Init());
deadbeefcbecd352015-09-23 11:50:27 -0700126 delete transport_controller_;
127 transport_controller_ =
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200128 new cricket::FakeTransportController(&network_, ICEROLE_CONTROLLING);
skvlad6c87a672016-05-17 17:49:52 -0700129 cricket::VoiceChannel* voice_channel = cm_->CreateVoiceChannel(
130 &fake_mc_, transport_controller_, cricket::CN_AUDIO, nullptr, false,
131 AudioOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200132 EXPECT_TRUE(voice_channel != nullptr);
skvlad6c87a672016-05-17 17:49:52 -0700133 cricket::VideoChannel* video_channel = cm_->CreateVideoChannel(
134 &fake_mc_, transport_controller_, cricket::CN_VIDEO, nullptr, false,
135 VideoOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200136 EXPECT_TRUE(video_channel != nullptr);
skvlad6c87a672016-05-17 17:49:52 -0700137 cricket::DataChannel* data_channel =
138 cm_->CreateDataChannel(transport_controller_, cricket::CN_DATA, nullptr,
139 false, cricket::DCT_RTP);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200140 EXPECT_TRUE(data_channel != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141 cm_->DestroyVideoChannel(video_channel);
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200142 cm_->DestroyVoiceChannel(voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143 cm_->DestroyDataChannel(data_channel);
144 cm_->Terminate();
145}
146
147// Test that we fail to create a voice/video channel if the session is unable
148// to create a cricket::TransportChannel
149TEST_F(ChannelManagerTest, NoTransportChannelTest) {
150 EXPECT_TRUE(cm_->Init());
deadbeefcbecd352015-09-23 11:50:27 -0700151 transport_controller_->set_fail_channel_creation(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152 // The test is useless unless the session does not fail creating
153 // cricket::TransportChannel.
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200154 ASSERT_TRUE(transport_controller_->CreateTransportChannel_n(
Jelena Marusicc28a8962015-05-29 15:05:44 +0200155 "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156
skvlad6c87a672016-05-17 17:49:52 -0700157 cricket::VoiceChannel* voice_channel = cm_->CreateVoiceChannel(
158 &fake_mc_, transport_controller_, cricket::CN_AUDIO, nullptr, false,
159 AudioOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200160 EXPECT_TRUE(voice_channel == nullptr);
skvlad6c87a672016-05-17 17:49:52 -0700161 cricket::VideoChannel* video_channel = cm_->CreateVideoChannel(
162 &fake_mc_, transport_controller_, cricket::CN_VIDEO, nullptr, false,
163 VideoOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200164 EXPECT_TRUE(video_channel == nullptr);
skvlad6c87a672016-05-17 17:49:52 -0700165 cricket::DataChannel* data_channel =
166 cm_->CreateDataChannel(transport_controller_, cricket::CN_DATA, nullptr,
167 false, cricket::DCT_RTP);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200168 EXPECT_TRUE(data_channel == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169 cm_->Terminate();
170}
171
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172TEST_F(ChannelManagerTest, GetSetOutputVolumeBeforeInit) {
173 int level;
174 // Before init, SetOutputVolume() remembers the volume but does not change the
175 // volume of the engine. GetOutputVolume() should fail.
176 EXPECT_EQ(-1, fme_->output_volume());
177 EXPECT_FALSE(cm_->GetOutputVolume(&level));
178 EXPECT_FALSE(cm_->SetOutputVolume(-1)); // Invalid volume.
179 EXPECT_TRUE(cm_->SetOutputVolume(99));
180 EXPECT_EQ(-1, fme_->output_volume());
181
182 // Init() will apply the remembered volume.
183 EXPECT_TRUE(cm_->Init());
184 EXPECT_TRUE(cm_->GetOutputVolume(&level));
185 EXPECT_EQ(99, level);
186 EXPECT_EQ(level, fme_->output_volume());
187
188 EXPECT_TRUE(cm_->SetOutputVolume(60));
189 EXPECT_TRUE(cm_->GetOutputVolume(&level));
190 EXPECT_EQ(60, level);
191 EXPECT_EQ(level, fme_->output_volume());
192}
193
194TEST_F(ChannelManagerTest, GetSetOutputVolume) {
195 int level;
196 EXPECT_TRUE(cm_->Init());
197 EXPECT_TRUE(cm_->GetOutputVolume(&level));
198 EXPECT_EQ(level, fme_->output_volume());
199
200 EXPECT_FALSE(cm_->SetOutputVolume(-1)); // Invalid volume.
201 EXPECT_TRUE(cm_->SetOutputVolume(60));
202 EXPECT_EQ(60, fme_->output_volume());
203 EXPECT_TRUE(cm_->GetOutputVolume(&level));
204 EXPECT_EQ(60, level);
205}
206
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207TEST_F(ChannelManagerTest, SetVideoRtxEnabled) {
208 std::vector<VideoCodec> codecs;
deadbeef67cf2c12016-04-13 10:07:16 -0700209 const VideoCodec rtx_codec(96, "rtx", 0, 0, 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210
211 // By default RTX is disabled.
212 cm_->GetSupportedVideoCodecs(&codecs);
213 EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec));
214
215 // Enable and check.
216 EXPECT_TRUE(cm_->SetVideoRtxEnabled(true));
217 cm_->GetSupportedVideoCodecs(&codecs);
218 EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec));
219
220 // Disable and check.
221 EXPECT_TRUE(cm_->SetVideoRtxEnabled(false));
222 cm_->GetSupportedVideoCodecs(&codecs);
223 EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec));
224
225 // Cannot toggle rtx after initialization.
226 EXPECT_TRUE(cm_->Init());
227 EXPECT_FALSE(cm_->SetVideoRtxEnabled(true));
228 EXPECT_FALSE(cm_->SetVideoRtxEnabled(false));
229
230 // Can set again after terminate.
231 cm_->Terminate();
232 EXPECT_TRUE(cm_->SetVideoRtxEnabled(true));
233 cm_->GetSupportedVideoCodecs(&codecs);
234 EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec));
235}
236
237} // namespace cricket