blob: 1ffdaf2836955e3938cc5da22fa560c6dbeaeeea [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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028#include "talk/media/base/fakecapturemanager.h"
29#include "talk/media/base/fakemediaengine.h"
30#include "talk/media/base/fakemediaprocessor.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031#include "talk/media/base/testutils.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000032#include "talk/media/devices/fakedevicemanager.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000033#include "webrtc/p2p/base/fakesession.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"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038
39namespace cricket {
40
41static const AudioCodec kAudioCodecs[] = {
42 AudioCodec(97, "voice", 1, 2, 3, 0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043 AudioCodec(111, "OPUS", 48000, 32000, 2, 0),
44};
45
46static const VideoCodec kVideoCodecs[] = {
47 VideoCodec(99, "H264", 100, 200, 300, 0),
48 VideoCodec(100, "VP8", 100, 200, 300, 0),
49 VideoCodec(96, "rtx", 100, 200, 300, 0),
50};
51
52class ChannelManagerTest : public testing::Test {
53 protected:
54 ChannelManagerTest() : fme_(NULL), fdm_(NULL), fcm_(NULL), cm_(NULL) {
55 }
56
57 virtual void SetUp() {
58 fme_ = new cricket::FakeMediaEngine();
59 fme_->SetAudioCodecs(MAKE_VECTOR(kAudioCodecs));
60 fme_->SetVideoCodecs(MAKE_VECTOR(kVideoCodecs));
61 fdme_ = new cricket::FakeDataEngine();
62 fdm_ = new cricket::FakeDeviceManager();
63 fcm_ = new cricket::FakeCaptureManager();
64 cm_ = new cricket::ChannelManager(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000065 fme_, fdme_, fdm_, fcm_, rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 session_ = new cricket::FakeSession(true);
67
68 std::vector<std::string> in_device_list, out_device_list, vid_device_list;
69 in_device_list.push_back("audio-in1");
70 in_device_list.push_back("audio-in2");
71 out_device_list.push_back("audio-out1");
72 out_device_list.push_back("audio-out2");
73 vid_device_list.push_back("video-in1");
74 vid_device_list.push_back("video-in2");
75 fdm_->SetAudioInputDevices(in_device_list);
76 fdm_->SetAudioOutputDevices(out_device_list);
77 fdm_->SetVideoCaptureDevices(vid_device_list);
78 }
79
80 virtual void TearDown() {
81 delete session_;
82 delete cm_;
83 cm_ = NULL;
84 fdm_ = NULL;
85 fcm_ = NULL;
86 fdme_ = NULL;
87 fme_ = NULL;
88 }
89
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000090 rtc::Thread worker_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 cricket::FakeMediaEngine* fme_;
92 cricket::FakeDataEngine* fdme_;
93 cricket::FakeDeviceManager* fdm_;
94 cricket::FakeCaptureManager* fcm_;
95 cricket::ChannelManager* cm_;
96 cricket::FakeSession* session_;
97};
98
99// Test that we startup/shutdown properly.
100TEST_F(ChannelManagerTest, StartupShutdown) {
101 EXPECT_FALSE(cm_->initialized());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000102 EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103 EXPECT_TRUE(cm_->Init());
104 EXPECT_TRUE(cm_->initialized());
105 cm_->Terminate();
106 EXPECT_FALSE(cm_->initialized());
107}
108
109// Test that we startup/shutdown properly with a worker thread.
110TEST_F(ChannelManagerTest, StartupShutdownOnThread) {
111 worker_.Start();
112 EXPECT_FALSE(cm_->initialized());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000113 EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 EXPECT_TRUE(cm_->set_worker_thread(&worker_));
115 EXPECT_EQ(&worker_, cm_->worker_thread());
116 EXPECT_TRUE(cm_->Init());
117 EXPECT_TRUE(cm_->initialized());
118 // Setting the worker thread while initialized should fail.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000119 EXPECT_FALSE(cm_->set_worker_thread(rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120 cm_->Terminate();
121 EXPECT_FALSE(cm_->initialized());
122}
123
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124// Test that we can create and destroy a voice and video channel.
125TEST_F(ChannelManagerTest, CreateDestroyChannels) {
126 EXPECT_TRUE(cm_->Init());
127 cricket::VoiceChannel* voice_channel = cm_->CreateVoiceChannel(
Jelena Marusicc28a8962015-05-29 15:05:44 +0200128 session_, cricket::CN_AUDIO, false, AudioOptions());
129 EXPECT_TRUE(voice_channel != nullptr);
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000130 cricket::VideoChannel* video_channel = cm_->CreateVideoChannel(
131 session_, cricket::CN_VIDEO, false, VideoOptions(), voice_channel);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200132 EXPECT_TRUE(video_channel != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 cricket::DataChannel* data_channel =
134 cm_->CreateDataChannel(session_, cricket::CN_DATA,
135 false, cricket::DCT_RTP);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200136 EXPECT_TRUE(data_channel != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137 cm_->DestroyVideoChannel(video_channel);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200138 cm_->DestroyVoiceChannel(voice_channel, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 cm_->DestroyDataChannel(data_channel);
140 cm_->Terminate();
141}
142
143// Test that we can create and destroy a voice and video channel with a worker.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000144TEST_F(ChannelManagerTest, CreateDestroyChannelsOnThread) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145 worker_.Start();
146 EXPECT_TRUE(cm_->set_worker_thread(&worker_));
147 EXPECT_TRUE(cm_->Init());
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000148 delete session_;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000149 session_ = new cricket::FakeSession(&worker_, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150 cricket::VoiceChannel* voice_channel = cm_->CreateVoiceChannel(
Jelena Marusicc28a8962015-05-29 15:05:44 +0200151 session_, cricket::CN_AUDIO, false, AudioOptions());
152 EXPECT_TRUE(voice_channel != nullptr);
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000153 cricket::VideoChannel* video_channel = cm_->CreateVideoChannel(
154 session_, cricket::CN_VIDEO, false, VideoOptions(), voice_channel);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200155 EXPECT_TRUE(video_channel != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156 cricket::DataChannel* data_channel =
157 cm_->CreateDataChannel(session_, cricket::CN_DATA,
158 false, cricket::DCT_RTP);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200159 EXPECT_TRUE(data_channel != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160 cm_->DestroyVideoChannel(video_channel);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +0200161 cm_->DestroyVoiceChannel(voice_channel, nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162 cm_->DestroyDataChannel(data_channel);
163 cm_->Terminate();
164}
165
166// Test that we fail to create a voice/video channel if the session is unable
167// to create a cricket::TransportChannel
168TEST_F(ChannelManagerTest, NoTransportChannelTest) {
169 EXPECT_TRUE(cm_->Init());
170 session_->set_fail_channel_creation(true);
171 // The test is useless unless the session does not fail creating
172 // cricket::TransportChannel.
173 ASSERT_TRUE(session_->CreateChannel(
Jelena Marusicc28a8962015-05-29 15:05:44 +0200174 "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP) == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175
176 cricket::VoiceChannel* voice_channel = cm_->CreateVoiceChannel(
Jelena Marusicc28a8962015-05-29 15:05:44 +0200177 session_, cricket::CN_AUDIO, false, AudioOptions());
178 EXPECT_TRUE(voice_channel == nullptr);
buildbot@webrtc.org1ecbe452014-10-14 20:29:28 +0000179 cricket::VideoChannel* video_channel = cm_->CreateVideoChannel(
180 session_, cricket::CN_VIDEO, false, VideoOptions(), voice_channel);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200181 EXPECT_TRUE(video_channel == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 cricket::DataChannel* data_channel =
183 cm_->CreateDataChannel(session_, cricket::CN_DATA,
184 false, cricket::DCT_RTP);
Jelena Marusicc28a8962015-05-29 15:05:44 +0200185 EXPECT_TRUE(data_channel == nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186 cm_->Terminate();
187}
188
189// Test that SetDefaultVideoCodec passes through the right values.
190TEST_F(ChannelManagerTest, SetDefaultVideoEncoderConfig) {
191 cricket::VideoCodec codec(96, "G264", 1280, 720, 60, 0);
192 cricket::VideoEncoderConfig config(codec, 1, 2);
193 EXPECT_TRUE(cm_->Init());
194 EXPECT_TRUE(cm_->SetDefaultVideoEncoderConfig(config));
195 EXPECT_EQ(config, fme_->default_video_encoder_config());
196}
197
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000198struct GetCapturerFrameSize : public sigslot::has_slots<> {
199 void OnVideoFrame(VideoCapturer* capturer, const VideoFrame* frame) {
200 width = frame->GetWidth();
201 height = frame->GetHeight();
202 }
203 GetCapturerFrameSize(VideoCapturer* capturer) : width(0), height(0) {
204 capturer->SignalVideoFrame.connect(this,
205 &GetCapturerFrameSize::OnVideoFrame);
206 static_cast<FakeVideoCapturer*>(capturer)->CaptureFrame();
207 }
208 size_t width;
209 size_t height;
210};
211
212TEST_F(ChannelManagerTest, DefaultCapturerAspectRatio) {
213 VideoCodec codec(100, "VP8", 640, 360, 30, 0);
214 VideoFormat format(640, 360, 33, FOURCC_ANY);
215 VideoEncoderConfig config(codec, 1, 2);
216 EXPECT_TRUE(cm_->Init());
217 // A capturer created before the default encoder config is set will have no
magjed@webrtc.org35c1ace2014-11-13 16:21:49 +0000218 // set aspect ratio, so it'll be 4:3 (based on the fake video capture impl).
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000219 VideoCapturer* capturer = cm_->CreateVideoCapturer();
220 ASSERT_TRUE(capturer != NULL);
221 EXPECT_EQ(CS_RUNNING, capturer->Start(format));
222 GetCapturerFrameSize size(capturer);
223 EXPECT_EQ(640u, size.width);
magjed@webrtc.org35c1ace2014-11-13 16:21:49 +0000224 EXPECT_EQ(480u, size.height);
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000225 delete capturer;
226 // Try again, but with the encoder config set to 16:9.
227 EXPECT_TRUE(cm_->SetDefaultVideoEncoderConfig(config));
228 capturer = cm_->CreateVideoCapturer();
229 ASSERT_TRUE(capturer != NULL);
230 EXPECT_EQ(CS_RUNNING, capturer->Start(format));
231 GetCapturerFrameSize cropped_size(capturer);
232 EXPECT_EQ(640u, cropped_size.width);
233 EXPECT_EQ(360u, cropped_size.height);
234 delete capturer;
235}
236
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000237// Test that SetDefaultVideoCodec passes through the right values.
238TEST_F(ChannelManagerTest, SetDefaultVideoCodecBeforeInit) {
239 cricket::VideoCodec codec(96, "G264", 1280, 720, 60, 0);
240 cricket::VideoEncoderConfig config(codec, 1, 2);
241 EXPECT_TRUE(cm_->SetDefaultVideoEncoderConfig(config));
242 EXPECT_TRUE(cm_->Init());
243 EXPECT_EQ(config, fme_->default_video_encoder_config());
244}
245
246TEST_F(ChannelManagerTest, SetAudioOptionsBeforeInit) {
247 // Test that values that we set before Init are applied.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000248 AudioOptions options;
249 options.auto_gain_control.Set(true);
250 options.echo_cancellation.Set(false);
251 EXPECT_TRUE(cm_->SetAudioOptions("audio-in1", "audio-out1", options));
252 std::string audio_in, audio_out;
253 AudioOptions set_options;
254 // Check options before Init.
255 EXPECT_TRUE(cm_->GetAudioOptions(&audio_in, &audio_out, &set_options));
256 EXPECT_EQ("audio-in1", audio_in);
257 EXPECT_EQ("audio-out1", audio_out);
258 EXPECT_EQ(options, set_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259 EXPECT_TRUE(cm_->Init());
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000260 // Check options after Init.
261 EXPECT_TRUE(cm_->GetAudioOptions(&audio_in, &audio_out, &set_options));
262 EXPECT_EQ("audio-in1", audio_in);
263 EXPECT_EQ("audio-out1", audio_out);
264 EXPECT_EQ(options, set_options);
265 // At this point, the media engine should also be initialized.
266 EXPECT_EQ(options, fme_->audio_options());
henrike@webrtc.org0481f152014-08-19 14:56:59 +0000267 EXPECT_EQ(cricket::kDefaultAudioDelayOffset,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268 fme_->audio_delay_offset());
269}
270
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271TEST_F(ChannelManagerTest, GetAudioOptionsWithNullParameters) {
272 std::string audio_in, audio_out;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000273 AudioOptions options;
274 options.echo_cancellation.Set(true);
275 EXPECT_TRUE(cm_->SetAudioOptions("audio-in2", "audio-out2", options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276 EXPECT_TRUE(cm_->GetAudioOptions(&audio_in, NULL, NULL));
277 EXPECT_EQ("audio-in2", audio_in);
278 EXPECT_TRUE(cm_->GetAudioOptions(NULL, &audio_out, NULL));
279 EXPECT_EQ("audio-out2", audio_out);
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000280 AudioOptions out_options;
281 EXPECT_TRUE(cm_->GetAudioOptions(NULL, NULL, &out_options));
282 bool echo_cancellation = false;
283 EXPECT_TRUE(out_options.echo_cancellation.Get(&echo_cancellation));
284 EXPECT_TRUE(echo_cancellation);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285}
286
287TEST_F(ChannelManagerTest, SetAudioOptions) {
288 // Test initial state.
289 EXPECT_TRUE(cm_->Init());
290 EXPECT_EQ(std::string(cricket::DeviceManagerInterface::kDefaultDeviceName),
291 fme_->audio_in_device());
292 EXPECT_EQ(std::string(cricket::DeviceManagerInterface::kDefaultDeviceName),
293 fme_->audio_out_device());
henrike@webrtc.org0481f152014-08-19 14:56:59 +0000294 EXPECT_EQ(cricket::kDefaultAudioDelayOffset,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295 fme_->audio_delay_offset());
296 // Test setting specific values.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000297 AudioOptions options;
298 options.auto_gain_control.Set(true);
299 EXPECT_TRUE(cm_->SetAudioOptions("audio-in1", "audio-out1", options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300 EXPECT_EQ("audio-in1", fme_->audio_in_device());
301 EXPECT_EQ("audio-out1", fme_->audio_out_device());
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000302 bool auto_gain_control = false;
303 EXPECT_TRUE(
304 fme_->audio_options().auto_gain_control.Get(&auto_gain_control));
305 EXPECT_TRUE(auto_gain_control);
henrike@webrtc.org0481f152014-08-19 14:56:59 +0000306 EXPECT_EQ(cricket::kDefaultAudioDelayOffset,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307 fme_->audio_delay_offset());
308 // Test setting bad values.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000309 EXPECT_FALSE(cm_->SetAudioOptions("audio-in9", "audio-out2", options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310}
311
312TEST_F(ChannelManagerTest, SetCaptureDeviceBeforeInit) {
313 // Test that values that we set before Init are applied.
314 EXPECT_TRUE(cm_->SetCaptureDevice("video-in2"));
315 EXPECT_TRUE(cm_->Init());
316 EXPECT_EQ("video-in2", cm_->video_device_name());
317}
318
319TEST_F(ChannelManagerTest, GetCaptureDeviceBeforeInit) {
320 std::string video_in;
321 // Test that GetCaptureDevice works before Init.
322 EXPECT_TRUE(cm_->SetCaptureDevice("video-in1"));
323 EXPECT_TRUE(cm_->GetCaptureDevice(&video_in));
324 EXPECT_EQ("video-in1", video_in);
325 // Test that options set before Init can be gotten after Init.
326 EXPECT_TRUE(cm_->SetCaptureDevice("video-in2"));
327 EXPECT_TRUE(cm_->Init());
328 EXPECT_TRUE(cm_->GetCaptureDevice(&video_in));
329 EXPECT_EQ("video-in2", video_in);
330}
331
332TEST_F(ChannelManagerTest, SetCaptureDevice) {
333 // Test setting defaults.
334 EXPECT_TRUE(cm_->Init());
335 EXPECT_TRUE(cm_->SetCaptureDevice("")); // will use DeviceManager default
336 EXPECT_EQ("video-in1", cm_->video_device_name());
337 // Test setting specific values.
338 EXPECT_TRUE(cm_->SetCaptureDevice("video-in2"));
339 EXPECT_EQ("video-in2", cm_->video_device_name());
340 // TODO(juberti): Add test for invalid value here.
341}
342
343// Test unplugging and plugging back the preferred devices. When the preferred
344// device is unplugged, we fall back to the default device. When the preferred
345// device is plugged back, we use it.
346TEST_F(ChannelManagerTest, SetAudioOptionsUnplugPlug) {
347 // Set preferences "audio-in1" and "audio-out1" before init.
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +0000348 AudioOptions options;
349 EXPECT_TRUE(cm_->SetAudioOptions("audio-in1", "audio-out1", options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000350 // Unplug device "audio-in1" and "audio-out1".
351 std::vector<std::string> in_device_list, out_device_list;
352 in_device_list.push_back("audio-in2");
353 out_device_list.push_back("audio-out2");
354 fdm_->SetAudioInputDevices(in_device_list);
355 fdm_->SetAudioOutputDevices(out_device_list);
356 // Init should fall back to default devices.
357 EXPECT_TRUE(cm_->Init());
358 // The media engine should use the default.
359 EXPECT_EQ("", fme_->audio_in_device());
360 EXPECT_EQ("", fme_->audio_out_device());
361 // The channel manager keeps the preferences "audio-in1" and "audio-out1".
362 std::string audio_in, audio_out;
363 EXPECT_TRUE(cm_->GetAudioOptions(&audio_in, &audio_out, NULL));
364 EXPECT_EQ("audio-in1", audio_in);
365 EXPECT_EQ("audio-out1", audio_out);
366 cm_->Terminate();
367
368 // Plug devices "audio-in2" and "audio-out2" back.
369 in_device_list.push_back("audio-in1");
370 out_device_list.push_back("audio-out1");
371 fdm_->SetAudioInputDevices(in_device_list);
372 fdm_->SetAudioOutputDevices(out_device_list);
373 // Init again. The preferences, "audio-in2" and "audio-out2", are used.
374 EXPECT_TRUE(cm_->Init());
375 EXPECT_EQ("audio-in1", fme_->audio_in_device());
376 EXPECT_EQ("audio-out1", fme_->audio_out_device());
377 EXPECT_TRUE(cm_->GetAudioOptions(&audio_in, &audio_out, NULL));
378 EXPECT_EQ("audio-in1", audio_in);
379 EXPECT_EQ("audio-out1", audio_out);
380}
381
382// We have one camera. Unplug it, fall back to no camera.
383TEST_F(ChannelManagerTest, SetCaptureDeviceUnplugPlugOneCamera) {
384 // Set preferences "video-in1" before init.
385 std::vector<std::string> vid_device_list;
386 vid_device_list.push_back("video-in1");
387 fdm_->SetVideoCaptureDevices(vid_device_list);
388 EXPECT_TRUE(cm_->SetCaptureDevice("video-in1"));
389
390 // Unplug "video-in1".
391 vid_device_list.clear();
392 fdm_->SetVideoCaptureDevices(vid_device_list);
393
394 // Init should fall back to avatar.
395 EXPECT_TRUE(cm_->Init());
396 // The media engine should use no camera.
397 EXPECT_EQ("", cm_->video_device_name());
398 // The channel manager keeps the user preference "video-in".
399 std::string video_in;
400 EXPECT_TRUE(cm_->GetCaptureDevice(&video_in));
401 EXPECT_EQ("video-in1", video_in);
402 cm_->Terminate();
403
404 // Plug device "video-in1" back.
405 vid_device_list.push_back("video-in1");
406 fdm_->SetVideoCaptureDevices(vid_device_list);
407 // Init again. The user preferred device, "video-in1", is used.
408 EXPECT_TRUE(cm_->Init());
409 EXPECT_EQ("video-in1", cm_->video_device_name());
410 EXPECT_TRUE(cm_->GetCaptureDevice(&video_in));
411 EXPECT_EQ("video-in1", video_in);
412}
413
414// We have multiple cameras. Unplug the preferred, fall back to another camera.
415TEST_F(ChannelManagerTest, SetCaptureDeviceUnplugPlugTwoDevices) {
416 // Set video device to "video-in1" before init.
417 EXPECT_TRUE(cm_->SetCaptureDevice("video-in1"));
418 // Unplug device "video-in1".
419 std::vector<std::string> vid_device_list;
420 vid_device_list.push_back("video-in2");
421 fdm_->SetVideoCaptureDevices(vid_device_list);
422 // Init should fall back to default device "video-in2".
423 EXPECT_TRUE(cm_->Init());
424 // The media engine should use the default device "video-in2".
425 EXPECT_EQ("video-in2", cm_->video_device_name());
426 // The channel manager keeps the user preference "video-in".
427 std::string video_in;
428 EXPECT_TRUE(cm_->GetCaptureDevice(&video_in));
429 EXPECT_EQ("video-in1", video_in);
430 cm_->Terminate();
431
432 // Plug device "video-in1" back.
433 vid_device_list.push_back("video-in1");
434 fdm_->SetVideoCaptureDevices(vid_device_list);
435 // Init again. The user preferred device, "video-in1", is used.
436 EXPECT_TRUE(cm_->Init());
437 EXPECT_EQ("video-in1", cm_->video_device_name());
438 EXPECT_TRUE(cm_->GetCaptureDevice(&video_in));
439 EXPECT_EQ("video-in1", video_in);
440}
441
442TEST_F(ChannelManagerTest, GetCaptureDevice) {
443 std::string video_in;
444 // Test setting/getting defaults.
445 EXPECT_TRUE(cm_->Init());
446 EXPECT_TRUE(cm_->SetCaptureDevice(""));
447 EXPECT_TRUE(cm_->GetCaptureDevice(&video_in));
448 EXPECT_EQ("video-in1", video_in);
449 // Test setting/getting specific values.
450 EXPECT_TRUE(cm_->SetCaptureDevice("video-in2"));
451 EXPECT_TRUE(cm_->GetCaptureDevice(&video_in));
452 EXPECT_EQ("video-in2", video_in);
453}
454
455TEST_F(ChannelManagerTest, GetSetOutputVolumeBeforeInit) {
456 int level;
457 // Before init, SetOutputVolume() remembers the volume but does not change the
458 // volume of the engine. GetOutputVolume() should fail.
459 EXPECT_EQ(-1, fme_->output_volume());
460 EXPECT_FALSE(cm_->GetOutputVolume(&level));
461 EXPECT_FALSE(cm_->SetOutputVolume(-1)); // Invalid volume.
462 EXPECT_TRUE(cm_->SetOutputVolume(99));
463 EXPECT_EQ(-1, fme_->output_volume());
464
465 // Init() will apply the remembered volume.
466 EXPECT_TRUE(cm_->Init());
467 EXPECT_TRUE(cm_->GetOutputVolume(&level));
468 EXPECT_EQ(99, level);
469 EXPECT_EQ(level, fme_->output_volume());
470
471 EXPECT_TRUE(cm_->SetOutputVolume(60));
472 EXPECT_TRUE(cm_->GetOutputVolume(&level));
473 EXPECT_EQ(60, level);
474 EXPECT_EQ(level, fme_->output_volume());
475}
476
477TEST_F(ChannelManagerTest, GetSetOutputVolume) {
478 int level;
479 EXPECT_TRUE(cm_->Init());
480 EXPECT_TRUE(cm_->GetOutputVolume(&level));
481 EXPECT_EQ(level, fme_->output_volume());
482
483 EXPECT_FALSE(cm_->SetOutputVolume(-1)); // Invalid volume.
484 EXPECT_TRUE(cm_->SetOutputVolume(60));
485 EXPECT_EQ(60, fme_->output_volume());
486 EXPECT_TRUE(cm_->GetOutputVolume(&level));
487 EXPECT_EQ(60, level);
488}
489
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490// Test that logging options set before Init are applied properly,
491// and retained even after Init.
492TEST_F(ChannelManagerTest, SetLoggingBeforeInit) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000493 cm_->SetVoiceLogging(rtc::LS_INFO, "test-voice");
494 cm_->SetVideoLogging(rtc::LS_VERBOSE, "test-video");
495 EXPECT_EQ(rtc::LS_INFO, fme_->voice_loglevel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496 EXPECT_STREQ("test-voice", fme_->voice_logfilter().c_str());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000497 EXPECT_EQ(rtc::LS_VERBOSE, fme_->video_loglevel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498 EXPECT_STREQ("test-video", fme_->video_logfilter().c_str());
499 EXPECT_TRUE(cm_->Init());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000500 EXPECT_EQ(rtc::LS_INFO, fme_->voice_loglevel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501 EXPECT_STREQ("test-voice", fme_->voice_logfilter().c_str());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000502 EXPECT_EQ(rtc::LS_VERBOSE, fme_->video_loglevel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503 EXPECT_STREQ("test-video", fme_->video_logfilter().c_str());
504}
505
506// Test that logging options set after Init are applied properly.
507TEST_F(ChannelManagerTest, SetLogging) {
508 EXPECT_TRUE(cm_->Init());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000509 cm_->SetVoiceLogging(rtc::LS_INFO, "test-voice");
510 cm_->SetVideoLogging(rtc::LS_VERBOSE, "test-video");
511 EXPECT_EQ(rtc::LS_INFO, fme_->voice_loglevel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512 EXPECT_STREQ("test-voice", fme_->voice_logfilter().c_str());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000513 EXPECT_EQ(rtc::LS_VERBOSE, fme_->video_loglevel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514 EXPECT_STREQ("test-video", fme_->video_logfilter().c_str());
515}
516
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517// Test that the Video/Voice Processors register and unregister
518TEST_F(ChannelManagerTest, RegisterProcessors) {
519 cricket::FakeMediaProcessor fmp;
520 EXPECT_TRUE(cm_->Init());
521 EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_TX));
522 EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_RX));
523
524 EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_TX));
525 EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_RX));
526
527 EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_TX));
528 EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_RX));
529
530 EXPECT_TRUE(cm_->RegisterVoiceProcessor(1,
531 &fmp,
532 cricket::MPD_RX));
533 EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_TX));
534 EXPECT_TRUE(fme_->voice_processor_registered(cricket::MPD_RX));
535
536
537 EXPECT_TRUE(cm_->UnregisterVoiceProcessor(1,
538 &fmp,
539 cricket::MPD_RX));
540 EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_TX));
541 EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_RX));
542
543 EXPECT_TRUE(cm_->RegisterVoiceProcessor(1,
544 &fmp,
545 cricket::MPD_TX));
546 EXPECT_TRUE(fme_->voice_processor_registered(cricket::MPD_TX));
547 EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_RX));
548
549 EXPECT_TRUE(cm_->UnregisterVoiceProcessor(1,
550 &fmp,
551 cricket::MPD_TX));
552 EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_TX));
553 EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_RX));
554}
555
556TEST_F(ChannelManagerTest, SetVideoRtxEnabled) {
557 std::vector<VideoCodec> codecs;
558 const VideoCodec rtx_codec(96, "rtx", 0, 0, 0, 0);
559
560 // By default RTX is disabled.
561 cm_->GetSupportedVideoCodecs(&codecs);
562 EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec));
563
564 // Enable and check.
565 EXPECT_TRUE(cm_->SetVideoRtxEnabled(true));
566 cm_->GetSupportedVideoCodecs(&codecs);
567 EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec));
568
569 // Disable and check.
570 EXPECT_TRUE(cm_->SetVideoRtxEnabled(false));
571 cm_->GetSupportedVideoCodecs(&codecs);
572 EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec));
573
574 // Cannot toggle rtx after initialization.
575 EXPECT_TRUE(cm_->Init());
576 EXPECT_FALSE(cm_->SetVideoRtxEnabled(true));
577 EXPECT_FALSE(cm_->SetVideoRtxEnabled(false));
578
579 // Can set again after terminate.
580 cm_->Terminate();
581 EXPECT_TRUE(cm_->SetVideoRtxEnabled(true));
582 cm_->GetSupportedVideoCodecs(&codecs);
583 EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec));
584}
585
586} // namespace cricket