| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/audio/audio_input_controller.h" | 5 #include "media/audio/audio_input_controller.h" |
| 6 #include "base/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 12 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
| 13 #include "base/test/test_timeouts.h" | 13 #include "base/test/test_timeouts.h" |
| 14 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
| 15 #include "media/audio/audio_device_description.h" | 15 #include "media/audio/audio_device_description.h" |
| 16 #include "media/audio/audio_thread_impl.h" | 16 #include "media/audio/audio_thread_impl.h" |
| 17 #include "media/audio/fake_audio_input_stream.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 20 |
| 20 using ::testing::_; | 21 using ::testing::_; |
| 21 using ::testing::AnyNumber; | 22 using ::testing::AnyNumber; |
| 22 using ::testing::AtLeast; | 23 using ::testing::AtLeast; |
| 23 using ::testing::Exactly; | 24 using ::testing::Exactly; |
| 24 using ::testing::InvokeWithoutArgs; | 25 using ::testing::InvokeWithoutArgs; |
| 25 using ::testing::NotNull; | 26 using ::testing::NotNull; |
| 26 using base::WaitableEvent; | 27 using base::WaitableEvent; |
| 27 | 28 |
| 28 namespace media { | 29 namespace media { |
| 29 | 30 |
| 30 static const int kSampleRate = AudioParameters::kAudioCDSampleRate; | 31 static const int kSampleRate = AudioParameters::kAudioCDSampleRate; |
| 31 static const int kBitsPerSample = 16; | 32 static const int kBitsPerSample = 16; |
| 32 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO; | 33 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO; |
| 33 static const int kSamplesPerPacket = kSampleRate / 10; | 34 static const int kSamplesPerPacket = kSampleRate / 10; |
| 34 | 35 |
| 36 // AudioInputController will poll once every second, so wait at most a bit |
| 37 // more than that for the callbacks. |
| 38 static const int kOnMuteWaitTimeoutMs = 1500; |
| 39 |
| 35 ACTION_P(QuitRunLoop, run_loop) { | 40 ACTION_P(QuitRunLoop, run_loop) { |
| 36 run_loop->QuitWhenIdle(); | 41 run_loop->QuitWhenIdle(); |
| 37 } | 42 } |
| 38 | 43 |
| 39 // Posts base::MessageLoop::QuitWhenIdleClosure() on specified message loop | 44 // Posts base::MessageLoop::QuitWhenIdleClosure() on specified message loop |
| 40 // after a certain number of calls given by |limit|. | 45 // after a certain number of calls given by |limit|. |
| 41 ACTION_P3(CheckCountAndPostQuitTask, count, limit, loop_or_proxy) { | 46 ACTION_P3(CheckCountAndPostQuitTask, count, limit, loop_or_proxy) { |
| 42 if (++*count >= limit) { | 47 if (++*count >= limit) { |
| 43 loop_or_proxy->PostTask(FROM_HERE, | 48 loop_or_proxy->PostTask(FROM_HERE, |
| 44 base::MessageLoop::QuitWhenIdleClosure()); | 49 base::MessageLoop::QuitWhenIdleClosure()); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 55 : public AudioInputController::EventHandler { | 60 : public AudioInputController::EventHandler { |
| 56 public: | 61 public: |
| 57 MockAudioInputControllerEventHandler() {} | 62 MockAudioInputControllerEventHandler() {} |
| 58 | 63 |
| 59 MOCK_METHOD1(OnCreated, void(AudioInputController* controller)); | 64 MOCK_METHOD1(OnCreated, void(AudioInputController* controller)); |
| 60 MOCK_METHOD2(OnError, void(AudioInputController* controller, | 65 MOCK_METHOD2(OnError, void(AudioInputController* controller, |
| 61 AudioInputController::ErrorCode error_code)); | 66 AudioInputController::ErrorCode error_code)); |
| 62 MOCK_METHOD2(OnLog, | 67 MOCK_METHOD2(OnLog, |
| 63 void(AudioInputController* controller, | 68 void(AudioInputController* controller, |
| 64 const std::string& message)); | 69 const std::string& message)); |
| 70 MOCK_METHOD2(OnMuted, void(AudioInputController* controller, bool is_muted)); |
| 65 | 71 |
| 66 private: | 72 private: |
| 67 DISALLOW_COPY_AND_ASSIGN(MockAudioInputControllerEventHandler); | 73 DISALLOW_COPY_AND_ASSIGN(MockAudioInputControllerEventHandler); |
| 68 }; | 74 }; |
| 69 | 75 |
| 70 class MockSyncWriter : public AudioInputController::SyncWriter { | 76 class MockSyncWriter : public AudioInputController::SyncWriter { |
| 71 public: | 77 public: |
| 72 MockSyncWriter() {} | 78 MockSyncWriter() {} |
| 73 | 79 |
| 74 MOCK_METHOD4(Write, | 80 MOCK_METHOD4(Write, |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 | 222 |
| 217 controller->Record(); | 223 controller->Record(); |
| 218 | 224 |
| 219 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); | 225 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); |
| 220 base::RunLoop().Run(); | 226 base::RunLoop().Run(); |
| 221 | 227 |
| 222 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); | 228 controller->Close(base::MessageLoop::QuitWhenIdleClosure()); |
| 223 base::RunLoop().Run(); | 229 base::RunLoop().Run(); |
| 224 } | 230 } |
| 225 | 231 |
| 232 namespace { |
| 233 void RunLoopWithTimeout(base::RunLoop* run_loop, base::TimeDelta timeout) { |
| 234 base::OneShotTimer timeout_timer; |
| 235 timeout_timer.Start(FROM_HERE, timeout, run_loop->QuitClosure()); |
| 236 run_loop->Run(); |
| 237 } |
| 238 } |
| 239 |
| 240 // Test that AudioInputController sends OnMute callbacks properly. |
| 241 TEST_F(AudioInputControllerTest, TestOnmutedCallbackInitiallyUnmuted) { |
| 242 const auto timeout = base::TimeDelta::FromMilliseconds(kOnMuteWaitTimeoutMs); |
| 243 MockAudioInputControllerEventHandler event_handler; |
| 244 MockSyncWriter sync_writer; |
| 245 scoped_refptr<AudioInputController> controller; |
| 246 WaitableEvent callback_event(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 247 WaitableEvent::InitialState::NOT_SIGNALED); |
| 248 |
| 249 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, |
| 250 kSampleRate, kBitsPerSample, kSamplesPerPacket); |
| 251 |
| 252 base::RunLoop unmute_run_loop; |
| 253 base::RunLoop mute_run_loop; |
| 254 base::RunLoop setup_run_loop; |
| 255 EXPECT_CALL(event_handler, OnCreated(_)).Times(Exactly(1)); |
| 256 EXPECT_CALL(event_handler, OnLog(_, _)).Times(Exactly(3)); |
| 257 EXPECT_CALL(sync_writer, Close()).Times(Exactly(1)); |
| 258 EXPECT_CALL(event_handler, OnMuted(_, true)) |
| 259 .WillOnce(InvokeWithoutArgs([&] { mute_run_loop.Quit(); })); |
| 260 EXPECT_CALL(event_handler, OnMuted(_, false)) |
| 261 .WillOnce(InvokeWithoutArgs([&] { unmute_run_loop.Quit(); })); |
| 262 |
| 263 FakeAudioInputStream::SetGlobalMutedState(false); |
| 264 controller = AudioInputController::Create( |
| 265 audio_manager_.get(), &event_handler, &sync_writer, nullptr, params, |
| 266 AudioDeviceDescription::kDefaultDeviceId, false); |
| 267 ASSERT_TRUE(controller.get()); |
| 268 setup_run_loop.RunUntilIdle(); |
| 269 |
| 270 FakeAudioInputStream::SetGlobalMutedState(true); |
| 271 RunLoopWithTimeout(&mute_run_loop, timeout); |
| 272 FakeAudioInputStream::SetGlobalMutedState(false); |
| 273 RunLoopWithTimeout(&unmute_run_loop, timeout); |
| 274 |
| 275 CloseAudioController(controller.get()); |
| 276 } |
| 277 |
| 278 TEST_F(AudioInputControllerTest, TestOnmutedCallbackInitiallyMuted) { |
| 279 const auto timeout = base::TimeDelta::FromMilliseconds(kOnMuteWaitTimeoutMs); |
| 280 MockAudioInputControllerEventHandler event_handler; |
| 281 MockSyncWriter sync_writer; |
| 282 scoped_refptr<AudioInputController> controller; |
| 283 WaitableEvent callback_event(WaitableEvent::ResetPolicy::AUTOMATIC, |
| 284 WaitableEvent::InitialState::NOT_SIGNALED); |
| 285 |
| 286 AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout, |
| 287 kSampleRate, kBitsPerSample, kSamplesPerPacket); |
| 288 |
| 289 base::RunLoop unmute_run_loop; |
| 290 base::RunLoop setup_run_loop; |
| 291 EXPECT_CALL(event_handler, OnCreated(_)).Times(Exactly(1)); |
| 292 EXPECT_CALL(event_handler, OnLog(_, _)).Times(Exactly(3)); |
| 293 EXPECT_CALL(sync_writer, Close()).Times(Exactly(1)); |
| 294 EXPECT_CALL(event_handler, OnMuted(_, true)) |
| 295 .WillOnce(InvokeWithoutArgs([&] { setup_run_loop.QuitWhenIdle(); })); |
| 296 EXPECT_CALL(event_handler, OnMuted(_, false)) |
| 297 .WillOnce(InvokeWithoutArgs([&] { unmute_run_loop.Quit(); })); |
| 298 |
| 299 FakeAudioInputStream::SetGlobalMutedState(true); |
| 300 controller = AudioInputController::Create( |
| 301 audio_manager_.get(), &event_handler, &sync_writer, nullptr, params, |
| 302 AudioDeviceDescription::kDefaultDeviceId, false); |
| 303 ASSERT_TRUE(controller.get()); |
| 304 RunLoopWithTimeout(&setup_run_loop, timeout); |
| 305 |
| 306 FakeAudioInputStream::SetGlobalMutedState(false); |
| 307 RunLoopWithTimeout(&unmute_run_loop, timeout); |
| 308 |
| 309 CloseAudioController(controller.get()); |
| 310 } |
| 311 |
| 226 } // namespace media | 312 } // namespace media |
| OLD | NEW |