blob: e48fd741812db0e1d3c34f0e750e9b1aa2e95cb6 [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"
31#include "talk/media/base/fakemediaprocessor.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000032#include "talk/media/base/testutils.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000033#include "talk/media/devices/fakedevicemanager.h"
Fredrik Solenberg709ed672015-09-15 12:26:33 +020034#include "talk/media/webrtc/fakewebrtccall.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000035#include "webrtc/p2p/base/fakesession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036#include "talk/session/media/channelmanager.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000037#include "webrtc/base/gunit.h"
38#include "webrtc/base/logging.h"
39#include "webrtc/base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040
41namespace cricket {
42
43static const AudioCodec kAudioCodecs[] = {
44 AudioCodec(97, "voice", 1, 2, 3, 0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045 AudioCodec(111, "OPUS", 48000, 32000, 2, 0),
46};
47
48static const VideoCodec kVideoCodecs[] = {
49 VideoCodec(99, "H264", 100, 200, 300, 0),
50 VideoCodec(100, "VP8", 100, 200, 300, 0),
51 VideoCodec(96, "rtx", 100, 200, 300, 0),
52};
53
Fredrik Solenberg709ed672015-09-15 12:26:33 +020054class FakeMediaController : public webrtc::MediaControllerInterface {
55 public:
56 explicit FakeMediaController(webrtc::Call* call) : call_(call) {
57 DCHECK(nullptr != call);
58 }
59 ~FakeMediaController() override {}
60 webrtc::Call* call_w() override { return call_; }
61 private:
62 webrtc::Call* call_;
63};
64
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065class ChannelManagerTest : public testing::Test {
66 protected:
Fredrik Solenberg709ed672015-09-15 12:26:33 +020067 ChannelManagerTest() : fake_call_(webrtc::Call::Config()),
68 fake_mc_(&fake_call_), fme_(NULL), fdm_(NULL), fcm_(NULL), cm_(NULL) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069
70 virtual void SetUp() {
71 fme_ = new cricket::FakeMediaEngine();
72 fme_->SetAudioCodecs(MAKE_VECTOR(kAudioCodecs));
73 fme_->SetVideoCodecs(MAKE_VECTOR(kVideoCodecs));
74 fdme_ = new cricket::FakeDataEngine();
75 fdm_ = new cricket::FakeDeviceManager();
76 fcm_ = new cricket::FakeCaptureManager();
77 cm_ = new cricket::ChannelManager(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000078 fme_, fdme_, fdm_, fcm_, rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 session_ = new cricket::FakeSession(true);
80
81 std::vector<std::string> in_device_list, out_device_list, vid_device_list;
82 in_device_list.push_back("audio-in1");
83 in_device_list.push_back("audio-in2");
84 out_device_list.push_back("audio-out1");
85 out_device_list.push_back("audio-out2");
86 vid_device_list.push_back("video-in1");
87 vid_device_list.push_back("video-in2");
88 fdm_->SetAudioInputDevices(in_device_list);
89 fdm_->SetAudioOutputDevices(out_device_list);
90 fdm_->SetVideoCaptureDevices(vid_device_list);
91 }
92
93 virtual void TearDown() {
94 delete session_;
95 delete cm_;
96 cm_ = NULL;
97 fdm_ = NULL;
98 fcm_ = NULL;
99 fdme_ = NULL;
100 fme_ = NULL;
101 }
102
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000103 rtc::Thread worker_;
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200104 cricket::FakeCall fake_call_;
105 cricket::FakeMediaController fake_mc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106 cricket::FakeMediaEngine* fme_;
107 cricket::FakeDataEngine* fdme_;
108 cricket::FakeDeviceManager* fdm_;
109 cricket::FakeCaptureManager* fcm_;
110 cricket::ChannelManager* cm_;
111 cricket::FakeSession* session_;
112};
113
114// Test that we startup/shutdown properly.
115TEST_F(ChannelManagerTest, StartupShutdown) {
116 EXPECT_FALSE(cm_->initialized());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000117 EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118 EXPECT_TRUE(cm_->Init());
119 EXPECT_TRUE(cm_->initialized());
120 cm_->Terminate();
121 EXPECT_FALSE(cm_->initialized());
122}
123
124// Test that we startup/shutdown properly with a worker thread.
125TEST_F(ChannelManagerTest, StartupShutdownOnThread) {
126 worker_.Start();
127 EXPECT_FALSE(cm_->initialized());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000128 EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 EXPECT_TRUE(cm_->set_worker_thread(&worker_));
130 EXPECT_EQ(&worker_, cm_->worker_thread());
131 EXPECT_TRUE(cm_->Init());
132 EXPECT_TRUE(cm_->initialized());
133 // Setting the worker thread while initialized should fail.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000134 EXPECT_FALSE(cm_->set_worker_thread(rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135 cm_->Terminate();
136 EXPECT_FALSE(cm_->initialized());
137}
138
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139// Test that we can create and destroy a voice and video channel.
140TEST_F(ChannelManagerTest, CreateDestroyChannels) {
141 EXPECT_TRUE(cm_->Init());
142 cricket::VoiceChannel* voice_channel = cm_->CreateVoiceChannel(
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200143 &fake_mc_, session_, cricket::CN_AUDIO, false, AudioOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200144 EXPECT_TRUE(voice_channel != nullptr);
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000145 cricket::VideoChannel* video_channel = cm_->CreateVideoChannel(
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200146 &fake_mc_, session_, cricket::CN_VIDEO, false, VideoOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200147 EXPECT_TRUE(video_channel != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 cricket::DataChannel* data_channel =
149 cm_->CreateDataChannel(session_, cricket::CN_DATA,
150 false, cricket::DCT_RTP);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200151 EXPECT_TRUE(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);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154 cm_->DestroyDataChannel(data_channel);
155 cm_->Terminate();
156}
157
158// Test that we can create and destroy a voice and video channel with a worker.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000159TEST_F(ChannelManagerTest, CreateDestroyChannelsOnThread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160 worker_.Start();
161 EXPECT_TRUE(cm_->set_worker_thread(&worker_));
162 EXPECT_TRUE(cm_->Init());
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000163 delete session_;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000164 session_ = new cricket::FakeSession(&worker_, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165 cricket::VoiceChannel* voice_channel = cm_->CreateVoiceChannel(
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200166 &fake_mc_, session_, cricket::CN_AUDIO, false, AudioOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200167 EXPECT_TRUE(voice_channel != nullptr);
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000168 cricket::VideoChannel* video_channel = cm_->CreateVideoChannel(
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200169 &fake_mc_, session_, cricket::CN_VIDEO, false, VideoOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200170 EXPECT_TRUE(video_channel != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171 cricket::DataChannel* data_channel =
172 cm_->CreateDataChannel(session_, cricket::CN_DATA,
173 false, cricket::DCT_RTP);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200174 EXPECT_TRUE(data_channel != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 cm_->DestroyVideoChannel(video_channel);
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200176 cm_->DestroyVoiceChannel(voice_channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 cm_->DestroyDataChannel(data_channel);
178 cm_->Terminate();
179}
180
181// Test that we fail to create a voice/video channel if the session is unable
182// to create a cricket::TransportChannel
183TEST_F(ChannelManagerTest, NoTransportChannelTest) {
184 EXPECT_TRUE(cm_->Init());
185 session_->set_fail_channel_creation(true);
186 // The test is useless unless the session does not fail creating
187 // cricket::TransportChannel.
188 ASSERT_TRUE(session_->CreateChannel(
Jelena Marusicc28a8962015-05-29 15:05:44 +0200189 "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190
191 cricket::VoiceChannel* voice_channel = cm_->CreateVoiceChannel(
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200192 &fake_mc_, session_, cricket::CN_AUDIO, false, AudioOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200193 EXPECT_TRUE(voice_channel == nullptr);
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000194 cricket::VideoChannel* video_channel = cm_->CreateVideoChannel(
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200195 &fake_mc_, session_, cricket::CN_VIDEO, false, VideoOptions());
Jelena Marusicc28a8962015-05-29 15:05:44 +0200196 EXPECT_TRUE(video_channel == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197 cricket::DataChannel* data_channel =
198 cm_->CreateDataChannel(session_, cricket::CN_DATA,
199 false, cricket::DCT_RTP);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200200 EXPECT_TRUE(data_channel == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201 cm_->Terminate();
202}
203
204// Test that SetDefaultVideoCodec passes through the right values.
205TEST_F(ChannelManagerTest, SetDefaultVideoEncoderConfig) {
206 cricket::VideoCodec codec(96, "G264", 1280, 720, 60, 0);
207 cricket::VideoEncoderConfig config(codec, 1, 2);
208 EXPECT_TRUE(cm_->Init());
209 EXPECT_TRUE(cm_->SetDefaultVideoEncoderConfig(config));
210 EXPECT_EQ(config, fme_->default_video_encoder_config());
211}
212
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000213struct GetCapturerFrameSize : public sigslot::has_slots<> {
214 void OnVideoFrame(VideoCapturer* capturer, const VideoFrame* frame) {
215 width = frame->GetWidth();
216 height = frame->GetHeight();
217 }
218 GetCapturerFrameSize(VideoCapturer* capturer) : width(0), height(0) {
219 capturer->SignalVideoFrame.connect(this,
220 &GetCapturerFrameSize::OnVideoFrame);
221 static_cast<FakeVideoCapturer*>(capturer)->CaptureFrame();
222 }
223 size_t width;
224 size_t height;
225};
226
227TEST_F(ChannelManagerTest, DefaultCapturerAspectRatio) {
228 VideoCodec codec(100, "VP8", 640, 360, 30, 0);
229 VideoFormat format(640, 360, 33, FOURCC_ANY);
230 VideoEncoderConfig config(codec, 1, 2);
231 EXPECT_TRUE(cm_->Init());
232 // A capturer created before the default encoder config is set will have no
magjed@webrtc.org35c1ace2014-11-13 16:21:49 +0000233 // set aspect ratio, so it'll be 4:3 (based on the fake video capture impl).
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000234 VideoCapturer* capturer = cm_->CreateVideoCapturer();
235 ASSERT_TRUE(capturer != NULL);
236 EXPECT_EQ(CS_RUNNING, capturer->Start(format));
237 GetCapturerFrameSize size(capturer);
238 EXPECT_EQ(640u, size.width);
magjed@webrtc.org35c1ace2014-11-13 16:21:49 +0000239 EXPECT_EQ(480u, size.height);
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000240 delete capturer;
241 // Try again, but with the encoder config set to 16:9.
242 EXPECT_TRUE(cm_->SetDefaultVideoEncoderConfig(config));
243 capturer = cm_->CreateVideoCapturer();
244 ASSERT_TRUE(capturer != NULL);
245 EXPECT_EQ(CS_RUNNING, capturer->Start(format));
246 GetCapturerFrameSize cropped_size(capturer);
247 EXPECT_EQ(640u, cropped_size.width);
248 EXPECT_EQ(360u, cropped_size.height);
249 delete capturer;
250}
251
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252// Test that SetDefaultVideoCodec passes through the right values.
253TEST_F(ChannelManagerTest, SetDefaultVideoCodecBeforeInit) {
254 cricket::VideoCodec codec(96, "G264", 1280, 720, 60, 0);
255 cricket::VideoEncoderConfig config(codec, 1, 2);
256 EXPECT_TRUE(cm_->SetDefaultVideoEncoderConfig(config));
257 EXPECT_TRUE(cm_->Init());
258 EXPECT_EQ(config, fme_->default_video_encoder_config());
259}
260
261TEST_F(ChannelManagerTest, SetAudioOptionsBeforeInit) {
262 // Test that values that we set before Init are applied.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000263 AudioOptions options;
264 options.auto_gain_control.Set(true);
265 options.echo_cancellation.Set(false);
266 EXPECT_TRUE(cm_->SetAudioOptions("audio-in1", "audio-out1", options));
267 std::string audio_in, audio_out;
268 AudioOptions set_options;
269 // Check options before Init.
270 EXPECT_TRUE(cm_->GetAudioOptions(&audio_in, &audio_out, &set_options));
271 EXPECT_EQ("audio-in1", audio_in);
272 EXPECT_EQ("audio-out1", audio_out);
273 EXPECT_EQ(options, set_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274 EXPECT_TRUE(cm_->Init());
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000275 // Check options after Init.
276 EXPECT_TRUE(cm_->GetAudioOptions(&audio_in, &audio_out, &set_options));
277 EXPECT_EQ("audio-in1", audio_in);
278 EXPECT_EQ("audio-out1", audio_out);
279 EXPECT_EQ(options, set_options);
280 // At this point, the media engine should also be initialized.
281 EXPECT_EQ(options, fme_->audio_options());
henrike@webrtc.org0481f152014-08-19 14:56:59 +0000282 EXPECT_EQ(cricket::kDefaultAudioDelayOffset,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283 fme_->audio_delay_offset());
284}
285
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286TEST_F(ChannelManagerTest, GetAudioOptionsWithNullParameters) {
287 std::string audio_in, audio_out;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000288 AudioOptions options;
289 options.echo_cancellation.Set(true);
290 EXPECT_TRUE(cm_->SetAudioOptions("audio-in2", "audio-out2", options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000291 EXPECT_TRUE(cm_->GetAudioOptions(&audio_in, NULL, NULL));
292 EXPECT_EQ("audio-in2", audio_in);
293 EXPECT_TRUE(cm_->GetAudioOptions(NULL, &audio_out, NULL));
294 EXPECT_EQ("audio-out2", audio_out);
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000295 AudioOptions out_options;
296 EXPECT_TRUE(cm_->GetAudioOptions(NULL, NULL, &out_options));
297 bool echo_cancellation = false;
298 EXPECT_TRUE(out_options.echo_cancellation.Get(&echo_cancellation));
299 EXPECT_TRUE(echo_cancellation);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300}
301
302TEST_F(ChannelManagerTest, SetAudioOptions) {
303 // Test initial state.
304 EXPECT_TRUE(cm_->Init());
305 EXPECT_EQ(std::string(cricket::DeviceManagerInterface::kDefaultDeviceName),
306 fme_->audio_in_device());
307 EXPECT_EQ(std::string(cricket::DeviceManagerInterface::kDefaultDeviceName),
308 fme_->audio_out_device());
henrike@webrtc.org0481f152014-08-19 14:56:59 +0000309 EXPECT_EQ(cricket::kDefaultAudioDelayOffset,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310 fme_->audio_delay_offset());
311 // Test setting specific values.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000312 AudioOptions options;
313 options.auto_gain_control.Set(true);
314 EXPECT_TRUE(cm_->SetAudioOptions("audio-in1", "audio-out1", options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315 EXPECT_EQ("audio-in1", fme_->audio_in_device());
316 EXPECT_EQ("audio-out1", fme_->audio_out_device());
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000317 bool auto_gain_control = false;
318 EXPECT_TRUE(
319 fme_->audio_options().auto_gain_control.Get(&auto_gain_control));
320 EXPECT_TRUE(auto_gain_control);
henrike@webrtc.org0481f152014-08-19 14:56:59 +0000321 EXPECT_EQ(cricket::kDefaultAudioDelayOffset,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322 fme_->audio_delay_offset());
323 // Test setting bad values.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000324 EXPECT_FALSE(cm_->SetAudioOptions("audio-in9", "audio-out2", options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325}
326
327TEST_F(ChannelManagerTest, SetCaptureDeviceBeforeInit) {
328 // Test that values that we set before Init are applied.
329 EXPECT_TRUE(cm_->SetCaptureDevice("video-in2"));
330 EXPECT_TRUE(cm_->Init());
331 EXPECT_EQ("video-in2", cm_->video_device_name());
332}
333
334TEST_F(ChannelManagerTest, GetCaptureDeviceBeforeInit) {
335 std::string video_in;
336 // Test that GetCaptureDevice works before Init.
337 EXPECT_TRUE(cm_->SetCaptureDevice("video-in1"));
338 EXPECT_TRUE(cm_->GetCaptureDevice(&video_in));
339 EXPECT_EQ("video-in1", video_in);
340 // Test that options set before Init can be gotten after Init.
341 EXPECT_TRUE(cm_->SetCaptureDevice("video-in2"));
342 EXPECT_TRUE(cm_->Init());
343 EXPECT_TRUE(cm_->GetCaptureDevice(&video_in));
344 EXPECT_EQ("video-in2", video_in);
345}
346
347TEST_F(ChannelManagerTest, SetCaptureDevice) {
348 // Test setting defaults.
349 EXPECT_TRUE(cm_->Init());
350 EXPECT_TRUE(cm_->SetCaptureDevice("")); // will use DeviceManager default
351 EXPECT_EQ("video-in1", cm_->video_device_name());
352 // Test setting specific values.
353 EXPECT_TRUE(cm_->SetCaptureDevice("video-in2"));
354 EXPECT_EQ("video-in2", cm_->video_device_name());
355 // TODO(juberti): Add test for invalid value here.
356}
357
358// Test unplugging and plugging back the preferred devices. When the preferred
359// device is unplugged, we fall back to the default device. When the preferred
360// device is plugged back, we use it.
361TEST_F(ChannelManagerTest, SetAudioOptionsUnplugPlug) {
362 // Set preferences "audio-in1" and "audio-out1" before init.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000363 AudioOptions options;
364 EXPECT_TRUE(cm_->SetAudioOptions("audio-in1", "audio-out1", options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000365 // Unplug device "audio-in1" and "audio-out1".
366 std::vector<std::string> in_device_list, out_device_list;
367 in_device_list.push_back("audio-in2");
368 out_device_list.push_back("audio-out2");
369 fdm_->SetAudioInputDevices(in_device_list);
370 fdm_->SetAudioOutputDevices(out_device_list);
371 // Init should fall back to default devices.
372 EXPECT_TRUE(cm_->Init());
373 // The media engine should use the default.
374 EXPECT_EQ("", fme_->audio_in_device());
375 EXPECT_EQ("", fme_->audio_out_device());
376 // The channel manager keeps the preferences "audio-in1" and "audio-out1".
377 std::string audio_in, audio_out;
378 EXPECT_TRUE(cm_->GetAudioOptions(&audio_in, &audio_out, NULL));
379 EXPECT_EQ("audio-in1", audio_in);
380 EXPECT_EQ("audio-out1", audio_out);
381 cm_->Terminate();
382
383 // Plug devices "audio-in2" and "audio-out2" back.
384 in_device_list.push_back("audio-in1");
385 out_device_list.push_back("audio-out1");
386 fdm_->SetAudioInputDevices(in_device_list);
387 fdm_->SetAudioOutputDevices(out_device_list);
388 // Init again. The preferences, "audio-in2" and "audio-out2", are used.
389 EXPECT_TRUE(cm_->Init());
390 EXPECT_EQ("audio-in1", fme_->audio_in_device());
391 EXPECT_EQ("audio-out1", fme_->audio_out_device());
392 EXPECT_TRUE(cm_->GetAudioOptions(&audio_in, &audio_out, NULL));
393 EXPECT_EQ("audio-in1", audio_in);
394 EXPECT_EQ("audio-out1", audio_out);
395}
396
397// We have one camera. Unplug it, fall back to no camera.
398TEST_F(ChannelManagerTest, SetCaptureDeviceUnplugPlugOneCamera) {
399 // Set preferences "video-in1" before init.
400 std::vector<std::string> vid_device_list;
401 vid_device_list.push_back("video-in1");
402 fdm_->SetVideoCaptureDevices(vid_device_list);
403 EXPECT_TRUE(cm_->SetCaptureDevice("video-in1"));
404
405 // Unplug "video-in1".
406 vid_device_list.clear();
407 fdm_->SetVideoCaptureDevices(vid_device_list);
408
409 // Init should fall back to avatar.
410 EXPECT_TRUE(cm_->Init());
411 // The media engine should use no camera.
412 EXPECT_EQ("", cm_->video_device_name());
413 // The channel manager keeps the user preference "video-in".
414 std::string video_in;
415 EXPECT_TRUE(cm_->GetCaptureDevice(&video_in));
416 EXPECT_EQ("video-in1", video_in);
417 cm_->Terminate();
418
419 // Plug device "video-in1" back.
420 vid_device_list.push_back("video-in1");
421 fdm_->SetVideoCaptureDevices(vid_device_list);
422 // Init again. The user preferred device, "video-in1", is used.
423 EXPECT_TRUE(cm_->Init());
424 EXPECT_EQ("video-in1", cm_->video_device_name());
425 EXPECT_TRUE(cm_->GetCaptureDevice(&video_in));
426 EXPECT_EQ("video-in1", video_in);
427}
428
429// We have multiple cameras. Unplug the preferred, fall back to another camera.
430TEST_F(ChannelManagerTest, SetCaptureDeviceUnplugPlugTwoDevices) {
431 // Set video device to "video-in1" before init.
432 EXPECT_TRUE(cm_->SetCaptureDevice("video-in1"));
433 // Unplug device "video-in1".
434 std::vector<std::string> vid_device_list;
435 vid_device_list.push_back("video-in2");
436 fdm_->SetVideoCaptureDevices(vid_device_list);
437 // Init should fall back to default device "video-in2".
438 EXPECT_TRUE(cm_->Init());
439 // The media engine should use the default device "video-in2".
440 EXPECT_EQ("video-in2", cm_->video_device_name());
441 // The channel manager keeps the user preference "video-in".
442 std::string video_in;
443 EXPECT_TRUE(cm_->GetCaptureDevice(&video_in));
444 EXPECT_EQ("video-in1", video_in);
445 cm_->Terminate();
446
447 // Plug device "video-in1" back.
448 vid_device_list.push_back("video-in1");
449 fdm_->SetVideoCaptureDevices(vid_device_list);
450 // Init again. The user preferred device, "video-in1", is used.
451 EXPECT_TRUE(cm_->Init());
452 EXPECT_EQ("video-in1", cm_->video_device_name());
453 EXPECT_TRUE(cm_->GetCaptureDevice(&video_in));
454 EXPECT_EQ("video-in1", video_in);
455}
456
457TEST_F(ChannelManagerTest, GetCaptureDevice) {
458 std::string video_in;
459 // Test setting/getting defaults.
460 EXPECT_TRUE(cm_->Init());
461 EXPECT_TRUE(cm_->SetCaptureDevice(""));
462 EXPECT_TRUE(cm_->GetCaptureDevice(&video_in));
463 EXPECT_EQ("video-in1", video_in);
464 // Test setting/getting specific values.
465 EXPECT_TRUE(cm_->SetCaptureDevice("video-in2"));
466 EXPECT_TRUE(cm_->GetCaptureDevice(&video_in));
467 EXPECT_EQ("video-in2", video_in);
468}
469
470TEST_F(ChannelManagerTest, GetSetOutputVolumeBeforeInit) {
471 int level;
472 // Before init, SetOutputVolume() remembers the volume but does not change the
473 // volume of the engine. GetOutputVolume() should fail.
474 EXPECT_EQ(-1, fme_->output_volume());
475 EXPECT_FALSE(cm_->GetOutputVolume(&level));
476 EXPECT_FALSE(cm_->SetOutputVolume(-1)); // Invalid volume.
477 EXPECT_TRUE(cm_->SetOutputVolume(99));
478 EXPECT_EQ(-1, fme_->output_volume());
479
480 // Init() will apply the remembered volume.
481 EXPECT_TRUE(cm_->Init());
482 EXPECT_TRUE(cm_->GetOutputVolume(&level));
483 EXPECT_EQ(99, level);
484 EXPECT_EQ(level, fme_->output_volume());
485
486 EXPECT_TRUE(cm_->SetOutputVolume(60));
487 EXPECT_TRUE(cm_->GetOutputVolume(&level));
488 EXPECT_EQ(60, level);
489 EXPECT_EQ(level, fme_->output_volume());
490}
491
492TEST_F(ChannelManagerTest, GetSetOutputVolume) {
493 int level;
494 EXPECT_TRUE(cm_->Init());
495 EXPECT_TRUE(cm_->GetOutputVolume(&level));
496 EXPECT_EQ(level, fme_->output_volume());
497
498 EXPECT_FALSE(cm_->SetOutputVolume(-1)); // Invalid volume.
499 EXPECT_TRUE(cm_->SetOutputVolume(60));
500 EXPECT_EQ(60, fme_->output_volume());
501 EXPECT_TRUE(cm_->GetOutputVolume(&level));
502 EXPECT_EQ(60, level);
503}
504
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505// Test that logging options set before Init are applied properly,
506// and retained even after Init.
507TEST_F(ChannelManagerTest, SetLoggingBeforeInit) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000508 cm_->SetVoiceLogging(rtc::LS_INFO, "test-voice");
509 cm_->SetVideoLogging(rtc::LS_VERBOSE, "test-video");
510 EXPECT_EQ(rtc::LS_INFO, fme_->voice_loglevel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 EXPECT_STREQ("test-voice", fme_->voice_logfilter().c_str());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000512 EXPECT_EQ(rtc::LS_VERBOSE, fme_->video_loglevel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513 EXPECT_STREQ("test-video", fme_->video_logfilter().c_str());
514 EXPECT_TRUE(cm_->Init());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000515 EXPECT_EQ(rtc::LS_INFO, fme_->voice_loglevel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000516 EXPECT_STREQ("test-voice", fme_->voice_logfilter().c_str());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000517 EXPECT_EQ(rtc::LS_VERBOSE, fme_->video_loglevel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000518 EXPECT_STREQ("test-video", fme_->video_logfilter().c_str());
519}
520
521// Test that logging options set after Init are applied properly.
522TEST_F(ChannelManagerTest, SetLogging) {
523 EXPECT_TRUE(cm_->Init());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000524 cm_->SetVoiceLogging(rtc::LS_INFO, "test-voice");
525 cm_->SetVideoLogging(rtc::LS_VERBOSE, "test-video");
526 EXPECT_EQ(rtc::LS_INFO, fme_->voice_loglevel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527 EXPECT_STREQ("test-voice", fme_->voice_logfilter().c_str());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000528 EXPECT_EQ(rtc::LS_VERBOSE, fme_->video_loglevel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529 EXPECT_STREQ("test-video", fme_->video_logfilter().c_str());
530}
531
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532// Test that the Video/Voice Processors register and unregister
533TEST_F(ChannelManagerTest, RegisterProcessors) {
534 cricket::FakeMediaProcessor fmp;
535 EXPECT_TRUE(cm_->Init());
536 EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_TX));
537 EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_RX));
538
539 EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_TX));
540 EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_RX));
541
542 EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_TX));
543 EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_RX));
544
545 EXPECT_TRUE(cm_->RegisterVoiceProcessor(1,
546 &fmp,
547 cricket::MPD_RX));
548 EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_TX));
549 EXPECT_TRUE(fme_->voice_processor_registered(cricket::MPD_RX));
550
551
552 EXPECT_TRUE(cm_->UnregisterVoiceProcessor(1,
553 &fmp,
554 cricket::MPD_RX));
555 EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_TX));
556 EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_RX));
557
558 EXPECT_TRUE(cm_->RegisterVoiceProcessor(1,
559 &fmp,
560 cricket::MPD_TX));
561 EXPECT_TRUE(fme_->voice_processor_registered(cricket::MPD_TX));
562 EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_RX));
563
564 EXPECT_TRUE(cm_->UnregisterVoiceProcessor(1,
565 &fmp,
566 cricket::MPD_TX));
567 EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_TX));
568 EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_RX));
569}
570
571TEST_F(ChannelManagerTest, SetVideoRtxEnabled) {
572 std::vector<VideoCodec> codecs;
573 const VideoCodec rtx_codec(96, "rtx", 0, 0, 0, 0);
574
575 // By default RTX is disabled.
576 cm_->GetSupportedVideoCodecs(&codecs);
577 EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec));
578
579 // Enable and check.
580 EXPECT_TRUE(cm_->SetVideoRtxEnabled(true));
581 cm_->GetSupportedVideoCodecs(&codecs);
582 EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec));
583
584 // Disable and check.
585 EXPECT_TRUE(cm_->SetVideoRtxEnabled(false));
586 cm_->GetSupportedVideoCodecs(&codecs);
587 EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec));
588
589 // Cannot toggle rtx after initialization.
590 EXPECT_TRUE(cm_->Init());
591 EXPECT_FALSE(cm_->SetVideoRtxEnabled(true));
592 EXPECT_FALSE(cm_->SetVideoRtxEnabled(false));
593
594 // Can set again after terminate.
595 cm_->Terminate();
596 EXPECT_TRUE(cm_->SetVideoRtxEnabled(true));
597 cm_->GetSupportedVideoCodecs(&codecs);
598 EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec));
599}
600
601} // namespace cricket