blob: fa6aa2cab6387cfabddb51174399ee3bd19c4b54 [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
stefanc1aeaf02015-10-15 07:26:07 -070028#include "talk/app/webrtc/fakemediacontroller.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
53class ChannelManagerTest : public testing::Test {
54 protected:
deadbeefcbecd352015-09-23 11:50:27 -070055 ChannelManagerTest()
stefanc1aeaf02015-10-15 07:26:07 -070056 : fme_(new cricket::FakeMediaEngine()),
57 fdme_(new cricket::FakeDataEngine()),
58 fcm_(new cricket::FakeCaptureManager()),
59 cm_(new cricket::ChannelManager(fme_,
60 fdme_,
61 fcm_,
62 rtc::Thread::Current())),
63 fake_call_(webrtc::Call::Config()),
64 fake_mc_(cm_, &fake_call_),
65 transport_controller_(
66 new cricket::FakeTransportController(ICEROLE_CONTROLLING)) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067
68 virtual void SetUp() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 fme_->SetAudioCodecs(MAKE_VECTOR(kAudioCodecs));
70 fme_->SetVideoCodecs(MAKE_VECTOR(kVideoCodecs));
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071 }
72
73 virtual void TearDown() {
deadbeefcbecd352015-09-23 11:50:27 -070074 delete transport_controller_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 delete cm_;
76 cm_ = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077 fcm_ = NULL;
78 fdme_ = NULL;
79 fme_ = NULL;
80 }
81
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000082 rtc::Thread worker_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083 cricket::FakeMediaEngine* fme_;
84 cricket::FakeDataEngine* fdme_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085 cricket::FakeCaptureManager* fcm_;
86 cricket::ChannelManager* cm_;
stefanc1aeaf02015-10-15 07:26:07 -070087 cricket::FakeCall fake_call_;
88 cricket::FakeMediaController fake_mc_;
deadbeefcbecd352015-09-23 11:50:27 -070089 cricket::FakeTransportController* transport_controller_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090};
91
92// Test that we startup/shutdown properly.
93TEST_F(ChannelManagerTest, StartupShutdown) {
94 EXPECT_FALSE(cm_->initialized());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000095 EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 EXPECT_TRUE(cm_->Init());
97 EXPECT_TRUE(cm_->initialized());
98 cm_->Terminate();
99 EXPECT_FALSE(cm_->initialized());
100}
101
102// Test that we startup/shutdown properly with a worker thread.
103TEST_F(ChannelManagerTest, StartupShutdownOnThread) {
104 worker_.Start();
105 EXPECT_FALSE(cm_->initialized());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000106 EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107 EXPECT_TRUE(cm_->set_worker_thread(&worker_));
108 EXPECT_EQ(&worker_, cm_->worker_thread());
109 EXPECT_TRUE(cm_->Init());
110 EXPECT_TRUE(cm_->initialized());
111 // Setting the worker thread while initialized should fail.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000112 EXPECT_FALSE(cm_->set_worker_thread(rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 cm_->Terminate();
114 EXPECT_FALSE(cm_->initialized());
115}
116
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117// Test that we can create and destroy a voice and video channel.
118TEST_F(ChannelManagerTest, CreateDestroyChannels) {
119 EXPECT_TRUE(cm_->Init());
deadbeefcbecd352015-09-23 11:50:27 -0700120 cricket::VoiceChannel* voice_channel =
121 cm_->CreateVoiceChannel(&fake_mc_, transport_controller_,
122 cricket::CN_AUDIO, false, AudioOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200123 EXPECT_TRUE(voice_channel != nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700124 cricket::VideoChannel* video_channel =
125 cm_->CreateVideoChannel(&fake_mc_, transport_controller_,
126 cricket::CN_VIDEO, false, VideoOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200127 EXPECT_TRUE(video_channel != nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700128 cricket::DataChannel* data_channel = cm_->CreateDataChannel(
129 transport_controller_, cricket::CN_DATA, false, cricket::DCT_RTP);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200130 EXPECT_TRUE(data_channel != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 cm_->DestroyVideoChannel(video_channel);
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200132 cm_->DestroyVoiceChannel(voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 cm_->DestroyDataChannel(data_channel);
134 cm_->Terminate();
135}
136
137// Test that we can create and destroy a voice and video channel with a worker.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000138TEST_F(ChannelManagerTest, CreateDestroyChannelsOnThread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 worker_.Start();
140 EXPECT_TRUE(cm_->set_worker_thread(&worker_));
141 EXPECT_TRUE(cm_->Init());
deadbeefcbecd352015-09-23 11:50:27 -0700142 delete transport_controller_;
143 transport_controller_ =
144 new cricket::FakeTransportController(&worker_, ICEROLE_CONTROLLING);
145 cricket::VoiceChannel* voice_channel =
146 cm_->CreateVoiceChannel(&fake_mc_, transport_controller_,
147 cricket::CN_AUDIO, false, AudioOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200148 EXPECT_TRUE(voice_channel != nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700149 cricket::VideoChannel* video_channel =
150 cm_->CreateVideoChannel(&fake_mc_, transport_controller_,
151 cricket::CN_VIDEO, false, VideoOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200152 EXPECT_TRUE(video_channel != nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700153 cricket::DataChannel* data_channel = cm_->CreateDataChannel(
154 transport_controller_, cricket::CN_DATA, false, cricket::DCT_RTP);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200155 EXPECT_TRUE(data_channel != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156 cm_->DestroyVideoChannel(video_channel);
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200157 cm_->DestroyVoiceChannel(voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158 cm_->DestroyDataChannel(data_channel);
159 cm_->Terminate();
160}
161
162// Test that we fail to create a voice/video channel if the session is unable
163// to create a cricket::TransportChannel
164TEST_F(ChannelManagerTest, NoTransportChannelTest) {
165 EXPECT_TRUE(cm_->Init());
deadbeefcbecd352015-09-23 11:50:27 -0700166 transport_controller_->set_fail_channel_creation(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 // The test is useless unless the session does not fail creating
168 // cricket::TransportChannel.
deadbeefcbecd352015-09-23 11:50:27 -0700169 ASSERT_TRUE(transport_controller_->CreateTransportChannel_w(
Jelena Marusicc28a8962015-05-29 15:05:44 +0200170 "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171
deadbeefcbecd352015-09-23 11:50:27 -0700172 cricket::VoiceChannel* voice_channel =
173 cm_->CreateVoiceChannel(&fake_mc_, transport_controller_,
174 cricket::CN_AUDIO, false, AudioOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200175 EXPECT_TRUE(voice_channel == nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700176 cricket::VideoChannel* video_channel =
177 cm_->CreateVideoChannel(&fake_mc_, transport_controller_,
178 cricket::CN_VIDEO, false, VideoOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200179 EXPECT_TRUE(video_channel == nullptr);
deadbeefcbecd352015-09-23 11:50:27 -0700180 cricket::DataChannel* data_channel = cm_->CreateDataChannel(
181 transport_controller_, cricket::CN_DATA, false, cricket::DCT_RTP);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200182 EXPECT_TRUE(data_channel == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 cm_->Terminate();
184}
185
186// Test that SetDefaultVideoCodec passes through the right values.
187TEST_F(ChannelManagerTest, SetDefaultVideoEncoderConfig) {
188 cricket::VideoCodec codec(96, "G264", 1280, 720, 60, 0);
189 cricket::VideoEncoderConfig config(codec, 1, 2);
190 EXPECT_TRUE(cm_->Init());
191 EXPECT_TRUE(cm_->SetDefaultVideoEncoderConfig(config));
192 EXPECT_EQ(config, fme_->default_video_encoder_config());
193}
194
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000195struct GetCapturerFrameSize : public sigslot::has_slots<> {
196 void OnVideoFrame(VideoCapturer* capturer, const VideoFrame* frame) {
197 width = frame->GetWidth();
198 height = frame->GetHeight();
199 }
200 GetCapturerFrameSize(VideoCapturer* capturer) : width(0), height(0) {
201 capturer->SignalVideoFrame.connect(this,
202 &GetCapturerFrameSize::OnVideoFrame);
203 static_cast<FakeVideoCapturer*>(capturer)->CaptureFrame();
204 }
205 size_t width;
206 size_t height;
207};
208
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000209// Test that SetDefaultVideoCodec passes through the right values.
210TEST_F(ChannelManagerTest, SetDefaultVideoCodecBeforeInit) {
211 cricket::VideoCodec codec(96, "G264", 1280, 720, 60, 0);
212 cricket::VideoEncoderConfig config(codec, 1, 2);
213 EXPECT_TRUE(cm_->SetDefaultVideoEncoderConfig(config));
214 EXPECT_TRUE(cm_->Init());
215 EXPECT_EQ(config, fme_->default_video_encoder_config());
216}
217
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218TEST_F(ChannelManagerTest, GetSetOutputVolumeBeforeInit) {
219 int level;
220 // Before init, SetOutputVolume() remembers the volume but does not change the
221 // volume of the engine. GetOutputVolume() should fail.
222 EXPECT_EQ(-1, fme_->output_volume());
223 EXPECT_FALSE(cm_->GetOutputVolume(&level));
224 EXPECT_FALSE(cm_->SetOutputVolume(-1)); // Invalid volume.
225 EXPECT_TRUE(cm_->SetOutputVolume(99));
226 EXPECT_EQ(-1, fme_->output_volume());
227
228 // Init() will apply the remembered volume.
229 EXPECT_TRUE(cm_->Init());
230 EXPECT_TRUE(cm_->GetOutputVolume(&level));
231 EXPECT_EQ(99, level);
232 EXPECT_EQ(level, fme_->output_volume());
233
234 EXPECT_TRUE(cm_->SetOutputVolume(60));
235 EXPECT_TRUE(cm_->GetOutputVolume(&level));
236 EXPECT_EQ(60, level);
237 EXPECT_EQ(level, fme_->output_volume());
238}
239
240TEST_F(ChannelManagerTest, GetSetOutputVolume) {
241 int level;
242 EXPECT_TRUE(cm_->Init());
243 EXPECT_TRUE(cm_->GetOutputVolume(&level));
244 EXPECT_EQ(level, fme_->output_volume());
245
246 EXPECT_FALSE(cm_->SetOutputVolume(-1)); // Invalid volume.
247 EXPECT_TRUE(cm_->SetOutputVolume(60));
248 EXPECT_EQ(60, fme_->output_volume());
249 EXPECT_TRUE(cm_->GetOutputVolume(&level));
250 EXPECT_EQ(60, level);
251}
252
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253// Test that logging options set before Init are applied properly,
254// and retained even after Init.
255TEST_F(ChannelManagerTest, SetLoggingBeforeInit) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000256 cm_->SetVoiceLogging(rtc::LS_INFO, "test-voice");
257 cm_->SetVideoLogging(rtc::LS_VERBOSE, "test-video");
258 EXPECT_EQ(rtc::LS_INFO, fme_->voice_loglevel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259 EXPECT_STREQ("test-voice", fme_->voice_logfilter().c_str());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000260 EXPECT_EQ(rtc::LS_VERBOSE, fme_->video_loglevel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000261 EXPECT_STREQ("test-video", fme_->video_logfilter().c_str());
262 EXPECT_TRUE(cm_->Init());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000263 EXPECT_EQ(rtc::LS_INFO, fme_->voice_loglevel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000264 EXPECT_STREQ("test-voice", fme_->voice_logfilter().c_str());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000265 EXPECT_EQ(rtc::LS_VERBOSE, fme_->video_loglevel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266 EXPECT_STREQ("test-video", fme_->video_logfilter().c_str());
267}
268
269// Test that logging options set after Init are applied properly.
270TEST_F(ChannelManagerTest, SetLogging) {
271 EXPECT_TRUE(cm_->Init());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000272 cm_->SetVoiceLogging(rtc::LS_INFO, "test-voice");
273 cm_->SetVideoLogging(rtc::LS_VERBOSE, "test-video");
274 EXPECT_EQ(rtc::LS_INFO, fme_->voice_loglevel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 EXPECT_STREQ("test-voice", fme_->voice_logfilter().c_str());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000276 EXPECT_EQ(rtc::LS_VERBOSE, fme_->video_loglevel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277 EXPECT_STREQ("test-video", fme_->video_logfilter().c_str());
278}
279
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280TEST_F(ChannelManagerTest, SetVideoRtxEnabled) {
281 std::vector<VideoCodec> codecs;
282 const VideoCodec rtx_codec(96, "rtx", 0, 0, 0, 0);
283
284 // By default RTX is disabled.
285 cm_->GetSupportedVideoCodecs(&codecs);
286 EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec));
287
288 // Enable and check.
289 EXPECT_TRUE(cm_->SetVideoRtxEnabled(true));
290 cm_->GetSupportedVideoCodecs(&codecs);
291 EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec));
292
293 // Disable and check.
294 EXPECT_TRUE(cm_->SetVideoRtxEnabled(false));
295 cm_->GetSupportedVideoCodecs(&codecs);
296 EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec));
297
298 // Cannot toggle rtx after initialization.
299 EXPECT_TRUE(cm_->Init());
300 EXPECT_FALSE(cm_->SetVideoRtxEnabled(true));
301 EXPECT_FALSE(cm_->SetVideoRtxEnabled(false));
302
303 // Can set again after terminate.
304 cm_->Terminate();
305 EXPECT_TRUE(cm_->SetVideoRtxEnabled(true));
306 cm_->GetSupportedVideoCodecs(&codecs);
307 EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec));
308}
309
310} // namespace cricket