blob: 6074d6431a1c5586ce30c0640ae661f46f1e86e0 [file] [log] [blame]
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00001/*
2 * libjingle
3 * Copyright 2008 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +000027
Fredrik Solenberg709ed672015-09-15 12:26:33 +020028#include "talk/app/webrtc/mediacontroller.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029#include "talk/media/base/fakecapturemanager.h"
30#include "talk/media/base/fakemediaengine.h"
solenbergfacbbec2015-09-24 00:41:50 -070031#include "talk/media/base/fakevideocapturer.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032#include "talk/media/base/testutils.h"
Fredrik Solenberg709ed672015-09-15 12:26:33 +020033#include "talk/media/webrtc/fakewebrtccall.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034#include "talk/session/media/channelmanager.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000035#include "webrtc/base/gunit.h"
36#include "webrtc/base/logging.h"
37#include "webrtc/base/thread.h"
deadbeefcbecd352015-09-23 11:50:27 -070038#include "webrtc/p2p/base/faketransportcontroller.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039
40namespace cricket {
41
42static const AudioCodec kAudioCodecs[] = {
43 AudioCodec(97, "voice", 1, 2, 3, 0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044 AudioCodec(111, "OPUS", 48000, 32000, 2, 0),
45};
46
47static const VideoCodec kVideoCodecs[] = {
48 VideoCodec(99, "H264", 100, 200, 300, 0),
49 VideoCodec(100, "VP8", 100, 200, 300, 0),
50 VideoCodec(96, "rtx", 100, 200, 300, 0),
51};
52
Fredrik Solenberg709ed672015-09-15 12:26:33 +020053class FakeMediaController : public webrtc::MediaControllerInterface {
54 public:
55 explicit FakeMediaController(webrtc::Call* call) : call_(call) {
henrikg91d6ede2015-09-17 00:24:34 -070056 RTC_DCHECK(nullptr != call);
Fredrik Solenberg709ed672015-09-15 12:26:33 +020057 }
58 ~FakeMediaController() override {}
59 webrtc::Call* call_w() override { return call_; }
deadbeefcbecd352015-09-23 11:50:27 -070060
Fredrik Solenberg709ed672015-09-15 12:26:33 +020061 private:
62 webrtc::Call* call_;
63};
64
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065class ChannelManagerTest : public testing::Test {
66 protected:
deadbeefcbecd352015-09-23 11:50:27 -070067 ChannelManagerTest()
68 : fake_call_(webrtc::Call::Config()),
69 fake_mc_(&fake_call_),
70 fme_(NULL),
deadbeefcbecd352015-09-23 11:50:27 -070071 fcm_(NULL),
72 cm_(NULL) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073
74 virtual void SetUp() {
75 fme_ = new cricket::FakeMediaEngine();
76 fme_->SetAudioCodecs(MAKE_VECTOR(kAudioCodecs));
77 fme_->SetVideoCodecs(MAKE_VECTOR(kVideoCodecs));
78 fdme_ = new cricket::FakeDataEngine();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 fcm_ = new cricket::FakeCaptureManager();
80 cm_ = new cricket::ChannelManager(
solenbergfacbbec2015-09-24 00:41:50 -070081 fme_, fdme_, fcm_, rtc::Thread::Current());
deadbeefcbecd352015-09-23 11:50:27 -070082 transport_controller_ =
83 new cricket::FakeTransportController(ICEROLE_CONTROLLING);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084 }
85
86 virtual void TearDown() {
deadbeefcbecd352015-09-23 11:50:27 -070087 delete transport_controller_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 delete cm_;
89 cm_ = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 fcm_ = NULL;
91 fdme_ = NULL;
92 fme_ = NULL;
93 }
94
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000095 rtc::Thread worker_;
Fredrik Solenberg709ed672015-09-15 12:26:33 +020096 cricket::FakeCall fake_call_;
97 cricket::FakeMediaController fake_mc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 cricket::FakeMediaEngine* fme_;
99 cricket::FakeDataEngine* fdme_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100 cricket::FakeCaptureManager* fcm_;
101 cricket::ChannelManager* cm_;
deadbeefcbecd352015-09-23 11:50:27 -0700102 cricket::FakeTransportController* transport_controller_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103};
104
105// Test that we startup/shutdown properly.
106TEST_F(ChannelManagerTest, StartupShutdown) {
107 EXPECT_FALSE(cm_->initialized());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000108 EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109 EXPECT_TRUE(cm_->Init());
110 EXPECT_TRUE(cm_->initialized());
111 cm_->Terminate();
112 EXPECT_FALSE(cm_->initialized());
113}
114
115// Test that we startup/shutdown properly with a worker thread.
116TEST_F(ChannelManagerTest, StartupShutdownOnThread) {
117 worker_.Start();
118 EXPECT_FALSE(cm_->initialized());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000119 EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120 EXPECT_TRUE(cm_->set_worker_thread(&worker_));
121 EXPECT_EQ(&worker_, cm_->worker_thread());
122 EXPECT_TRUE(cm_->Init());
123 EXPECT_TRUE(cm_->initialized());
124 // Setting the worker thread while initialized should fail.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000125 EXPECT_FALSE(cm_->set_worker_thread(rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126 cm_->Terminate();
127 EXPECT_FALSE(cm_->initialized());
128}
129
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130// Test that we can create and destroy a voice and video channel.
131TEST_F(ChannelManagerTest, CreateDestroyChannels) {
132 EXPECT_TRUE(cm_->Init());
deadbeefcbecd352015-09-23 11:50:27 -0700133 cricket::VoiceChannel* voice_channel =
134 cm_->CreateVoiceChannel(&fake_mc_, transport_controller_,
135 cricket::CN_AUDIO, false, AudioOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200136 EXPECT_TRUE(voice_channel != nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700137 cricket::VideoChannel* video_channel =
138 cm_->CreateVideoChannel(&fake_mc_, transport_controller_,
139 cricket::CN_VIDEO, false, VideoOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200140 EXPECT_TRUE(video_channel != nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700141 cricket::DataChannel* data_channel = cm_->CreateDataChannel(
142 transport_controller_, cricket::CN_DATA, false, cricket::DCT_RTP);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200143 EXPECT_TRUE(data_channel != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000144 cm_->DestroyVideoChannel(video_channel);
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200145 cm_->DestroyVoiceChannel(voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146 cm_->DestroyDataChannel(data_channel);
147 cm_->Terminate();
148}
149
150// Test that we can create and destroy a voice and video channel with a worker.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000151TEST_F(ChannelManagerTest, CreateDestroyChannelsOnThread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152 worker_.Start();
153 EXPECT_TRUE(cm_->set_worker_thread(&worker_));
154 EXPECT_TRUE(cm_->Init());
deadbeefcbecd352015-09-23 11:50:27 -0700155 delete transport_controller_;
156 transport_controller_ =
157 new cricket::FakeTransportController(&worker_, ICEROLE_CONTROLLING);
158 cricket::VoiceChannel* voice_channel =
159 cm_->CreateVoiceChannel(&fake_mc_, transport_controller_,
160 cricket::CN_AUDIO, false, AudioOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200161 EXPECT_TRUE(voice_channel != nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700162 cricket::VideoChannel* video_channel =
163 cm_->CreateVideoChannel(&fake_mc_, transport_controller_,
164 cricket::CN_VIDEO, false, VideoOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200165 EXPECT_TRUE(video_channel != nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700166 cricket::DataChannel* data_channel = cm_->CreateDataChannel(
167 transport_controller_, cricket::CN_DATA, 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_->DestroyVideoChannel(video_channel);
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200170 cm_->DestroyVoiceChannel(voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171 cm_->DestroyDataChannel(data_channel);
172 cm_->Terminate();
173}
174
175// Test that we fail to create a voice/video channel if the session is unable
176// to create a cricket::TransportChannel
177TEST_F(ChannelManagerTest, NoTransportChannelTest) {
178 EXPECT_TRUE(cm_->Init());
deadbeefcbecd352015-09-23 11:50:27 -0700179 transport_controller_->set_fail_channel_creation(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 // The test is useless unless the session does not fail creating
181 // cricket::TransportChannel.
deadbeefcbecd352015-09-23 11:50:27 -0700182 ASSERT_TRUE(transport_controller_->CreateTransportChannel_w(
Jelena Marusicc28a8962015-05-29 15:05:44 +0200183 "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184
deadbeefcbecd352015-09-23 11:50:27 -0700185 cricket::VoiceChannel* voice_channel =
186 cm_->CreateVoiceChannel(&fake_mc_, transport_controller_,
187 cricket::CN_AUDIO, false, AudioOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200188 EXPECT_TRUE(voice_channel == nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700189 cricket::VideoChannel* video_channel =
190 cm_->CreateVideoChannel(&fake_mc_, transport_controller_,
191 cricket::CN_VIDEO, false, VideoOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200192 EXPECT_TRUE(video_channel == nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700193 cricket::DataChannel* data_channel = cm_->CreateDataChannel(
194 transport_controller_, cricket::CN_DATA, false, cricket::DCT_RTP);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200195 EXPECT_TRUE(data_channel == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196 cm_->Terminate();
197}
198
199// Test that SetDefaultVideoCodec passes through the right values.
200TEST_F(ChannelManagerTest, SetDefaultVideoEncoderConfig) {
201 cricket::VideoCodec codec(96, "G264", 1280, 720, 60, 0);
202 cricket::VideoEncoderConfig config(codec, 1, 2);
203 EXPECT_TRUE(cm_->Init());
204 EXPECT_TRUE(cm_->SetDefaultVideoEncoderConfig(config));
205 EXPECT_EQ(config, fme_->default_video_encoder_config());
206}
207
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000208struct GetCapturerFrameSize : public sigslot::has_slots<> {
209 void OnVideoFrame(VideoCapturer* capturer, const VideoFrame* frame) {
210 width = frame->GetWidth();
211 height = frame->GetHeight();
212 }
213 GetCapturerFrameSize(VideoCapturer* capturer) : width(0), height(0) {
214 capturer->SignalVideoFrame.connect(this,
215 &GetCapturerFrameSize::OnVideoFrame);
216 static_cast<FakeVideoCapturer*>(capturer)->CaptureFrame();
217 }
218 size_t width;
219 size_t height;
220};
221
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000222// Test that SetDefaultVideoCodec passes through the right values.
223TEST_F(ChannelManagerTest, SetDefaultVideoCodecBeforeInit) {
224 cricket::VideoCodec codec(96, "G264", 1280, 720, 60, 0);
225 cricket::VideoEncoderConfig config(codec, 1, 2);
226 EXPECT_TRUE(cm_->SetDefaultVideoEncoderConfig(config));
227 EXPECT_TRUE(cm_->Init());
228 EXPECT_EQ(config, fme_->default_video_encoder_config());
229}
230
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231TEST_F(ChannelManagerTest, GetSetOutputVolumeBeforeInit) {
232 int level;
233 // Before init, SetOutputVolume() remembers the volume but does not change the
234 // volume of the engine. GetOutputVolume() should fail.
235 EXPECT_EQ(-1, fme_->output_volume());
236 EXPECT_FALSE(cm_->GetOutputVolume(&level));
237 EXPECT_FALSE(cm_->SetOutputVolume(-1)); // Invalid volume.
238 EXPECT_TRUE(cm_->SetOutputVolume(99));
239 EXPECT_EQ(-1, fme_->output_volume());
240
241 // Init() will apply the remembered volume.
242 EXPECT_TRUE(cm_->Init());
243 EXPECT_TRUE(cm_->GetOutputVolume(&level));
244 EXPECT_EQ(99, level);
245 EXPECT_EQ(level, fme_->output_volume());
246
247 EXPECT_TRUE(cm_->SetOutputVolume(60));
248 EXPECT_TRUE(cm_->GetOutputVolume(&level));
249 EXPECT_EQ(60, level);
250 EXPECT_EQ(level, fme_->output_volume());
251}
252
253TEST_F(ChannelManagerTest, GetSetOutputVolume) {
254 int level;
255 EXPECT_TRUE(cm_->Init());
256 EXPECT_TRUE(cm_->GetOutputVolume(&level));
257 EXPECT_EQ(level, fme_->output_volume());
258
259 EXPECT_FALSE(cm_->SetOutputVolume(-1)); // Invalid volume.
260 EXPECT_TRUE(cm_->SetOutputVolume(60));
261 EXPECT_EQ(60, fme_->output_volume());
262 EXPECT_TRUE(cm_->GetOutputVolume(&level));
263 EXPECT_EQ(60, level);
264}
265
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266// Test that logging options set before Init are applied properly,
267// and retained even after Init.
268TEST_F(ChannelManagerTest, SetLoggingBeforeInit) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000269 cm_->SetVoiceLogging(rtc::LS_INFO, "test-voice");
270 cm_->SetVideoLogging(rtc::LS_VERBOSE, "test-video");
271 EXPECT_EQ(rtc::LS_INFO, fme_->voice_loglevel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272 EXPECT_STREQ("test-voice", fme_->voice_logfilter().c_str());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000273 EXPECT_EQ(rtc::LS_VERBOSE, fme_->video_loglevel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 EXPECT_STREQ("test-video", fme_->video_logfilter().c_str());
275 EXPECT_TRUE(cm_->Init());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000276 EXPECT_EQ(rtc::LS_INFO, fme_->voice_loglevel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277 EXPECT_STREQ("test-voice", fme_->voice_logfilter().c_str());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000278 EXPECT_EQ(rtc::LS_VERBOSE, fme_->video_loglevel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279 EXPECT_STREQ("test-video", fme_->video_logfilter().c_str());
280}
281
282// Test that logging options set after Init are applied properly.
283TEST_F(ChannelManagerTest, SetLogging) {
284 EXPECT_TRUE(cm_->Init());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000285 cm_->SetVoiceLogging(rtc::LS_INFO, "test-voice");
286 cm_->SetVideoLogging(rtc::LS_VERBOSE, "test-video");
287 EXPECT_EQ(rtc::LS_INFO, fme_->voice_loglevel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288 EXPECT_STREQ("test-voice", fme_->voice_logfilter().c_str());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000289 EXPECT_EQ(rtc::LS_VERBOSE, fme_->video_loglevel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290 EXPECT_STREQ("test-video", fme_->video_logfilter().c_str());
291}
292
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293TEST_F(ChannelManagerTest, SetVideoRtxEnabled) {
294 std::vector<VideoCodec> codecs;
295 const VideoCodec rtx_codec(96, "rtx", 0, 0, 0, 0);
296
297 // By default RTX is disabled.
298 cm_->GetSupportedVideoCodecs(&codecs);
299 EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec));
300
301 // Enable and check.
302 EXPECT_TRUE(cm_->SetVideoRtxEnabled(true));
303 cm_->GetSupportedVideoCodecs(&codecs);
304 EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec));
305
306 // Disable and check.
307 EXPECT_TRUE(cm_->SetVideoRtxEnabled(false));
308 cm_->GetSupportedVideoCodecs(&codecs);
309 EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec));
310
311 // Cannot toggle rtx after initialization.
312 EXPECT_TRUE(cm_->Init());
313 EXPECT_FALSE(cm_->SetVideoRtxEnabled(true));
314 EXPECT_FALSE(cm_->SetVideoRtxEnabled(false));
315
316 // Can set again after terminate.
317 cm_->Terminate();
318 EXPECT_TRUE(cm_->SetVideoRtxEnabled(true));
319 cm_->GetSupportedVideoCodecs(&codecs);
320 EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec));
321}
322
323} // namespace cricket