Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / session / media / channelmanager_unittest.cc
1 // libjingle
2 // Copyright 2008 Google Inc.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 //  1. Redistributions of source code must retain the above copyright notice,
8 //     this list of conditions and the following disclaimer.
9 //  2. Redistributions in binary form must reproduce the above copyright notice,
10 //     this list of conditions and the following disclaimer in the documentation
11 //     and/or other materials provided with the distribution.
12 //  3. The name of the author may not be used to endorse or promote products
13 //     derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26 #include "talk/media/base/fakecapturemanager.h"
27 #include "talk/media/base/fakemediaengine.h"
28 #include "talk/media/base/fakemediaprocessor.h"
29 #include "talk/media/base/nullvideorenderer.h"
30 #include "talk/media/base/testutils.h"
31 #include "talk/media/devices/fakedevicemanager.h"
32 #include "webrtc/p2p/base/fakesession.h"
33 #include "talk/session/media/channelmanager.h"
34 #include "webrtc/base/gunit.h"
35 #include "webrtc/base/logging.h"
36 #include "webrtc/base/thread.h"
37
38 namespace cricket {
39
40 static const AudioCodec kAudioCodecs[] = {
41   AudioCodec(97, "voice", 1, 2, 3, 0),
42   AudioCodec(110, "CELT", 32000, 48000, 2, 0),
43   AudioCodec(111, "OPUS", 48000, 32000, 2, 0),
44 };
45
46 static 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
52 class 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(
65         fme_, fdme_, fdm_, fcm_, rtc::Thread::Current());
66     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
90   rtc::Thread worker_;
91   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.
100 TEST_F(ChannelManagerTest, StartupShutdown) {
101   EXPECT_FALSE(cm_->initialized());
102   EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread());
103   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.
110 TEST_F(ChannelManagerTest, StartupShutdownOnThread) {
111   worker_.Start();
112   EXPECT_FALSE(cm_->initialized());
113   EXPECT_EQ(rtc::Thread::Current(), cm_->worker_thread());
114   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.
119   EXPECT_FALSE(cm_->set_worker_thread(rtc::Thread::Current()));
120   cm_->Terminate();
121   EXPECT_FALSE(cm_->initialized());
122 }
123
124 // Test that we can create and destroy a voice and video channel.
125 TEST_F(ChannelManagerTest, CreateDestroyChannels) {
126   EXPECT_TRUE(cm_->Init());
127   cricket::VoiceChannel* voice_channel = cm_->CreateVoiceChannel(
128       session_, cricket::CN_AUDIO, false);
129   EXPECT_TRUE(voice_channel != NULL);
130   cricket::VideoChannel* video_channel = cm_->CreateVideoChannel(
131       session_, cricket::CN_VIDEO, false, VideoOptions(), voice_channel);
132   EXPECT_TRUE(video_channel != NULL);
133   cricket::DataChannel* data_channel =
134       cm_->CreateDataChannel(session_, cricket::CN_DATA,
135                              false, cricket::DCT_RTP);
136   EXPECT_TRUE(data_channel != NULL);
137   cm_->DestroyVideoChannel(video_channel);
138   cm_->DestroyVoiceChannel(voice_channel);
139   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.
144 TEST_F(ChannelManagerTest, CreateDestroyChannelsOnThread) {
145   worker_.Start();
146   EXPECT_TRUE(cm_->set_worker_thread(&worker_));
147   EXPECT_TRUE(cm_->Init());
148   delete session_;
149   session_ = new cricket::FakeSession(&worker_, true);
150   cricket::VoiceChannel* voice_channel = cm_->CreateVoiceChannel(
151       session_, cricket::CN_AUDIO, false);
152   EXPECT_TRUE(voice_channel != NULL);
153   cricket::VideoChannel* video_channel = cm_->CreateVideoChannel(
154       session_, cricket::CN_VIDEO, false, VideoOptions(), voice_channel);
155   EXPECT_TRUE(video_channel != NULL);
156   cricket::DataChannel* data_channel =
157       cm_->CreateDataChannel(session_, cricket::CN_DATA,
158                              false, cricket::DCT_RTP);
159   EXPECT_TRUE(data_channel != NULL);
160   cm_->DestroyVideoChannel(video_channel);
161   cm_->DestroyVoiceChannel(voice_channel);
162   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
168 TEST_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(
174       "audio", "rtp", cricket::ICE_CANDIDATE_COMPONENT_RTP) == NULL);
175
176   cricket::VoiceChannel* voice_channel = cm_->CreateVoiceChannel(
177       session_, cricket::CN_AUDIO, false);
178   EXPECT_TRUE(voice_channel == NULL);
179   cricket::VideoChannel* video_channel = cm_->CreateVideoChannel(
180       session_, cricket::CN_VIDEO, false, VideoOptions(), voice_channel);
181   EXPECT_TRUE(video_channel == NULL);
182   cricket::DataChannel* data_channel =
183       cm_->CreateDataChannel(session_, cricket::CN_DATA,
184                              false, cricket::DCT_RTP);
185   EXPECT_TRUE(data_channel == NULL);
186   cm_->Terminate();
187 }
188
189 // Test that SetDefaultVideoCodec passes through the right values.
190 TEST_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
198 struct 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
212 TEST_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
218   // set aspect ratio, so it'll be 4:3 (based on the fake video capture impl).
219   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);
224   EXPECT_EQ(480u, size.height);
225   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
237 // Test that SetDefaultVideoCodec passes through the right values.
238 TEST_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
246 TEST_F(ChannelManagerTest, SetAudioOptionsBeforeInit) {
247   // Test that values that we set before Init are applied.
248   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);
259   EXPECT_TRUE(cm_->Init());
260   // 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());
267   EXPECT_EQ(cricket::kDefaultAudioDelayOffset,
268             fme_->audio_delay_offset());
269 }
270
271 TEST_F(ChannelManagerTest, GetAudioOptionsWithNullParameters) {
272   std::string audio_in, audio_out;
273   AudioOptions options;
274   options.echo_cancellation.Set(true);
275   EXPECT_TRUE(cm_->SetAudioOptions("audio-in2", "audio-out2", options));
276   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);
280   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);
285 }
286
287 TEST_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());
294   EXPECT_EQ(cricket::kDefaultAudioDelayOffset,
295             fme_->audio_delay_offset());
296   // Test setting specific values.
297   AudioOptions options;
298   options.auto_gain_control.Set(true);
299   EXPECT_TRUE(cm_->SetAudioOptions("audio-in1", "audio-out1", options));
300   EXPECT_EQ("audio-in1", fme_->audio_in_device());
301   EXPECT_EQ("audio-out1", fme_->audio_out_device());
302   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);
306   EXPECT_EQ(cricket::kDefaultAudioDelayOffset,
307             fme_->audio_delay_offset());
308   // Test setting bad values.
309   EXPECT_FALSE(cm_->SetAudioOptions("audio-in9", "audio-out2", options));
310 }
311
312 TEST_F(ChannelManagerTest, SetEngineAudioOptions) {
313   EXPECT_TRUE(cm_->Init());
314   // Test setting specific values.
315   AudioOptions options;
316   options.experimental_ns.Set(true);
317   EXPECT_TRUE(cm_->SetEngineAudioOptions(options));
318   bool experimental_ns = false;
319   EXPECT_TRUE(fme_->audio_options().experimental_ns.Get(&experimental_ns));
320   EXPECT_TRUE(experimental_ns);
321 }
322
323 TEST_F(ChannelManagerTest, SetEngineAudioOptionsBeforeInitFails) {
324   // Test that values that we set before Init are not applied.
325   AudioOptions options;
326   options.experimental_ns.Set(true);
327   EXPECT_FALSE(cm_->SetEngineAudioOptions(options));
328   EXPECT_FALSE(fme_->audio_options().experimental_ns.IsSet());
329 }
330
331 TEST_F(ChannelManagerTest, SetCaptureDeviceBeforeInit) {
332   // Test that values that we set before Init are applied.
333   EXPECT_TRUE(cm_->SetCaptureDevice("video-in2"));
334   EXPECT_TRUE(cm_->Init());
335   EXPECT_EQ("video-in2", cm_->video_device_name());
336 }
337
338 TEST_F(ChannelManagerTest, GetCaptureDeviceBeforeInit) {
339   std::string video_in;
340   // Test that GetCaptureDevice works before Init.
341   EXPECT_TRUE(cm_->SetCaptureDevice("video-in1"));
342   EXPECT_TRUE(cm_->GetCaptureDevice(&video_in));
343   EXPECT_EQ("video-in1", video_in);
344   // Test that options set before Init can be gotten after Init.
345   EXPECT_TRUE(cm_->SetCaptureDevice("video-in2"));
346   EXPECT_TRUE(cm_->Init());
347   EXPECT_TRUE(cm_->GetCaptureDevice(&video_in));
348   EXPECT_EQ("video-in2", video_in);
349 }
350
351 TEST_F(ChannelManagerTest, SetCaptureDevice) {
352   // Test setting defaults.
353   EXPECT_TRUE(cm_->Init());
354   EXPECT_TRUE(cm_->SetCaptureDevice(""));  // will use DeviceManager default
355   EXPECT_EQ("video-in1", cm_->video_device_name());
356   // Test setting specific values.
357   EXPECT_TRUE(cm_->SetCaptureDevice("video-in2"));
358   EXPECT_EQ("video-in2", cm_->video_device_name());
359   // TODO(juberti): Add test for invalid value here.
360 }
361
362 // Test unplugging and plugging back the preferred devices. When the preferred
363 // device is unplugged, we fall back to the default device. When the preferred
364 // device is plugged back, we use it.
365 TEST_F(ChannelManagerTest, SetAudioOptionsUnplugPlug) {
366   // Set preferences "audio-in1" and "audio-out1" before init.
367   AudioOptions options;
368   EXPECT_TRUE(cm_->SetAudioOptions("audio-in1", "audio-out1", options));
369   // Unplug device "audio-in1" and "audio-out1".
370   std::vector<std::string> in_device_list, out_device_list;
371   in_device_list.push_back("audio-in2");
372   out_device_list.push_back("audio-out2");
373   fdm_->SetAudioInputDevices(in_device_list);
374   fdm_->SetAudioOutputDevices(out_device_list);
375   // Init should fall back to default devices.
376   EXPECT_TRUE(cm_->Init());
377   // The media engine should use the default.
378   EXPECT_EQ("", fme_->audio_in_device());
379   EXPECT_EQ("", fme_->audio_out_device());
380   // The channel manager keeps the preferences "audio-in1" and "audio-out1".
381   std::string audio_in, audio_out;
382   EXPECT_TRUE(cm_->GetAudioOptions(&audio_in, &audio_out, NULL));
383   EXPECT_EQ("audio-in1", audio_in);
384   EXPECT_EQ("audio-out1", audio_out);
385   cm_->Terminate();
386
387   // Plug devices "audio-in2" and "audio-out2" back.
388   in_device_list.push_back("audio-in1");
389   out_device_list.push_back("audio-out1");
390   fdm_->SetAudioInputDevices(in_device_list);
391   fdm_->SetAudioOutputDevices(out_device_list);
392   // Init again. The preferences, "audio-in2" and "audio-out2", are used.
393   EXPECT_TRUE(cm_->Init());
394   EXPECT_EQ("audio-in1", fme_->audio_in_device());
395   EXPECT_EQ("audio-out1", fme_->audio_out_device());
396   EXPECT_TRUE(cm_->GetAudioOptions(&audio_in, &audio_out, NULL));
397   EXPECT_EQ("audio-in1", audio_in);
398   EXPECT_EQ("audio-out1", audio_out);
399 }
400
401 // We have one camera. Unplug it, fall back to no camera.
402 TEST_F(ChannelManagerTest, SetCaptureDeviceUnplugPlugOneCamera) {
403   // Set preferences "video-in1" before init.
404   std::vector<std::string> vid_device_list;
405   vid_device_list.push_back("video-in1");
406   fdm_->SetVideoCaptureDevices(vid_device_list);
407   EXPECT_TRUE(cm_->SetCaptureDevice("video-in1"));
408
409   // Unplug "video-in1".
410   vid_device_list.clear();
411   fdm_->SetVideoCaptureDevices(vid_device_list);
412
413   // Init should fall back to avatar.
414   EXPECT_TRUE(cm_->Init());
415   // The media engine should use no camera.
416   EXPECT_EQ("", cm_->video_device_name());
417   // The channel manager keeps the user preference "video-in".
418   std::string video_in;
419   EXPECT_TRUE(cm_->GetCaptureDevice(&video_in));
420   EXPECT_EQ("video-in1", video_in);
421   cm_->Terminate();
422
423   // Plug device "video-in1" back.
424   vid_device_list.push_back("video-in1");
425   fdm_->SetVideoCaptureDevices(vid_device_list);
426   // Init again. The user preferred device, "video-in1", is used.
427   EXPECT_TRUE(cm_->Init());
428   EXPECT_EQ("video-in1", cm_->video_device_name());
429   EXPECT_TRUE(cm_->GetCaptureDevice(&video_in));
430   EXPECT_EQ("video-in1", video_in);
431 }
432
433 // We have multiple cameras. Unplug the preferred, fall back to another camera.
434 TEST_F(ChannelManagerTest, SetCaptureDeviceUnplugPlugTwoDevices) {
435   // Set video device to "video-in1" before init.
436   EXPECT_TRUE(cm_->SetCaptureDevice("video-in1"));
437   // Unplug device "video-in1".
438   std::vector<std::string> vid_device_list;
439   vid_device_list.push_back("video-in2");
440   fdm_->SetVideoCaptureDevices(vid_device_list);
441   // Init should fall back to default device "video-in2".
442   EXPECT_TRUE(cm_->Init());
443   // The media engine should use the default device "video-in2".
444   EXPECT_EQ("video-in2", cm_->video_device_name());
445   // The channel manager keeps the user preference "video-in".
446   std::string video_in;
447   EXPECT_TRUE(cm_->GetCaptureDevice(&video_in));
448   EXPECT_EQ("video-in1", video_in);
449   cm_->Terminate();
450
451   // Plug device "video-in1" back.
452   vid_device_list.push_back("video-in1");
453   fdm_->SetVideoCaptureDevices(vid_device_list);
454   // Init again. The user preferred device, "video-in1", is used.
455   EXPECT_TRUE(cm_->Init());
456   EXPECT_EQ("video-in1", cm_->video_device_name());
457   EXPECT_TRUE(cm_->GetCaptureDevice(&video_in));
458   EXPECT_EQ("video-in1", video_in);
459 }
460
461 TEST_F(ChannelManagerTest, GetCaptureDevice) {
462   std::string video_in;
463   // Test setting/getting defaults.
464   EXPECT_TRUE(cm_->Init());
465   EXPECT_TRUE(cm_->SetCaptureDevice(""));
466   EXPECT_TRUE(cm_->GetCaptureDevice(&video_in));
467   EXPECT_EQ("video-in1", video_in);
468   // Test setting/getting specific values.
469   EXPECT_TRUE(cm_->SetCaptureDevice("video-in2"));
470   EXPECT_TRUE(cm_->GetCaptureDevice(&video_in));
471   EXPECT_EQ("video-in2", video_in);
472 }
473
474 TEST_F(ChannelManagerTest, GetSetOutputVolumeBeforeInit) {
475   int level;
476   // Before init, SetOutputVolume() remembers the volume but does not change the
477   // volume of the engine. GetOutputVolume() should fail.
478   EXPECT_EQ(-1, fme_->output_volume());
479   EXPECT_FALSE(cm_->GetOutputVolume(&level));
480   EXPECT_FALSE(cm_->SetOutputVolume(-1));  // Invalid volume.
481   EXPECT_TRUE(cm_->SetOutputVolume(99));
482   EXPECT_EQ(-1, fme_->output_volume());
483
484   // Init() will apply the remembered volume.
485   EXPECT_TRUE(cm_->Init());
486   EXPECT_TRUE(cm_->GetOutputVolume(&level));
487   EXPECT_EQ(99, level);
488   EXPECT_EQ(level, fme_->output_volume());
489
490   EXPECT_TRUE(cm_->SetOutputVolume(60));
491   EXPECT_TRUE(cm_->GetOutputVolume(&level));
492   EXPECT_EQ(60, level);
493   EXPECT_EQ(level, fme_->output_volume());
494 }
495
496 TEST_F(ChannelManagerTest, GetSetOutputVolume) {
497   int level;
498   EXPECT_TRUE(cm_->Init());
499   EXPECT_TRUE(cm_->GetOutputVolume(&level));
500   EXPECT_EQ(level, fme_->output_volume());
501
502   EXPECT_FALSE(cm_->SetOutputVolume(-1));  // Invalid volume.
503   EXPECT_TRUE(cm_->SetOutputVolume(60));
504   EXPECT_EQ(60, fme_->output_volume());
505   EXPECT_TRUE(cm_->GetOutputVolume(&level));
506   EXPECT_EQ(60, level);
507 }
508
509 // Test that logging options set before Init are applied properly,
510 // and retained even after Init.
511 TEST_F(ChannelManagerTest, SetLoggingBeforeInit) {
512   cm_->SetVoiceLogging(rtc::LS_INFO, "test-voice");
513   cm_->SetVideoLogging(rtc::LS_VERBOSE, "test-video");
514   EXPECT_EQ(rtc::LS_INFO, fme_->voice_loglevel());
515   EXPECT_STREQ("test-voice", fme_->voice_logfilter().c_str());
516   EXPECT_EQ(rtc::LS_VERBOSE, fme_->video_loglevel());
517   EXPECT_STREQ("test-video", fme_->video_logfilter().c_str());
518   EXPECT_TRUE(cm_->Init());
519   EXPECT_EQ(rtc::LS_INFO, fme_->voice_loglevel());
520   EXPECT_STREQ("test-voice", fme_->voice_logfilter().c_str());
521   EXPECT_EQ(rtc::LS_VERBOSE, fme_->video_loglevel());
522   EXPECT_STREQ("test-video", fme_->video_logfilter().c_str());
523 }
524
525 // Test that logging options set after Init are applied properly.
526 TEST_F(ChannelManagerTest, SetLogging) {
527   EXPECT_TRUE(cm_->Init());
528   cm_->SetVoiceLogging(rtc::LS_INFO, "test-voice");
529   cm_->SetVideoLogging(rtc::LS_VERBOSE, "test-video");
530   EXPECT_EQ(rtc::LS_INFO, fme_->voice_loglevel());
531   EXPECT_STREQ("test-voice", fme_->voice_logfilter().c_str());
532   EXPECT_EQ(rtc::LS_VERBOSE, fme_->video_loglevel());
533   EXPECT_STREQ("test-video", fme_->video_logfilter().c_str());
534 }
535
536 // Test that the Video/Voice Processors register and unregister
537 TEST_F(ChannelManagerTest, RegisterProcessors) {
538   cricket::FakeMediaProcessor fmp;
539   EXPECT_TRUE(cm_->Init());
540   EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_TX));
541   EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_RX));
542
543   EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_TX));
544   EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_RX));
545
546   EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_TX));
547   EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_RX));
548
549   EXPECT_TRUE(cm_->RegisterVoiceProcessor(1,
550                                           &fmp,
551                                           cricket::MPD_RX));
552   EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_TX));
553   EXPECT_TRUE(fme_->voice_processor_registered(cricket::MPD_RX));
554
555
556   EXPECT_TRUE(cm_->UnregisterVoiceProcessor(1,
557                                             &fmp,
558                                             cricket::MPD_RX));
559   EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_TX));
560   EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_RX));
561
562   EXPECT_TRUE(cm_->RegisterVoiceProcessor(1,
563                                           &fmp,
564                                           cricket::MPD_TX));
565   EXPECT_TRUE(fme_->voice_processor_registered(cricket::MPD_TX));
566   EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_RX));
567
568   EXPECT_TRUE(cm_->UnregisterVoiceProcessor(1,
569                                             &fmp,
570                                             cricket::MPD_TX));
571   EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_TX));
572   EXPECT_FALSE(fme_->voice_processor_registered(cricket::MPD_RX));
573 }
574
575 TEST_F(ChannelManagerTest, SetVideoRtxEnabled) {
576   std::vector<VideoCodec> codecs;
577   const VideoCodec rtx_codec(96, "rtx", 0, 0, 0, 0);
578
579   // By default RTX is disabled.
580   cm_->GetSupportedVideoCodecs(&codecs);
581   EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec));
582
583   // Enable and check.
584   EXPECT_TRUE(cm_->SetVideoRtxEnabled(true));
585   cm_->GetSupportedVideoCodecs(&codecs);
586   EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec));
587
588   // Disable and check.
589   EXPECT_TRUE(cm_->SetVideoRtxEnabled(false));
590   cm_->GetSupportedVideoCodecs(&codecs);
591   EXPECT_FALSE(ContainsMatchingCodec(codecs, rtx_codec));
592
593   // Cannot toggle rtx after initialization.
594   EXPECT_TRUE(cm_->Init());
595   EXPECT_FALSE(cm_->SetVideoRtxEnabled(true));
596   EXPECT_FALSE(cm_->SetVideoRtxEnabled(false));
597
598   // Can set again after terminate.
599   cm_->Terminate();
600   EXPECT_TRUE(cm_->SetVideoRtxEnabled(true));
601   cm_->GetSupportedVideoCodecs(&codecs);
602   EXPECT_TRUE(ContainsMatchingCodec(codecs, rtx_codec));
603 }
604
605 }  // namespace cricket