| 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_output_controller.h" | 5 #include "media/audio/audio_output_controller.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/environment.h" | 12 #include "base/environment.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 17 #include "base/test/test_message_loop.h" | 17 #include "base/test/test_message_loop.h" |
| 18 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
| 19 #include "media/audio/audio_device_description.h" | 19 #include "media/audio/audio_device_description.h" |
| 20 #include "media/audio/audio_source_diverter.h" |
| 20 #include "media/base/audio_bus.h" | 21 #include "media/base/audio_bus.h" |
| 21 #include "media/base/audio_parameters.h" | 22 #include "media/base/audio_parameters.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 25 |
| 25 using ::testing::_; | 26 using ::testing::_; |
| 26 using ::testing::AtLeast; | 27 using ::testing::AtLeast; |
| 27 using ::testing::DoAll; | 28 using ::testing::DoAll; |
| 28 using ::testing::Invoke; | 29 using ::testing::Invoke; |
| 29 using ::testing::NotNull; | 30 using ::testing::NotNull; |
| 30 using ::testing::Return; | 31 using ::testing::Return; |
| 31 | 32 |
| 32 namespace media { | 33 namespace media { |
| 33 | 34 |
| 34 static const int kSampleRate = AudioParameters::kAudioCDSampleRate; | 35 static const int kSampleRate = AudioParameters::kAudioCDSampleRate; |
| 35 static const int kBitsPerSample = 16; | 36 static const int kBitsPerSample = 16; |
| 36 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO; | 37 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO; |
| 37 static const int kSamplesPerPacket = kSampleRate / 100; | 38 static const int kSamplesPerPacket = kSampleRate / 100; |
| 38 static const double kTestVolume = 0.25; | 39 static const double kTestVolume = 0.25; |
| 40 static const float kBufferNonZeroData = 1.0f; |
| 39 | 41 |
| 40 class MockAudioOutputControllerEventHandler | 42 class MockAudioOutputControllerEventHandler |
| 41 : public AudioOutputController::EventHandler { | 43 : public AudioOutputController::EventHandler { |
| 42 public: | 44 public: |
| 43 MockAudioOutputControllerEventHandler() {} | 45 MockAudioOutputControllerEventHandler() {} |
| 44 | 46 |
| 45 MOCK_METHOD0(OnCreated, void()); | 47 MOCK_METHOD0(OnCreated, void()); |
| 46 MOCK_METHOD0(OnPlaying, void()); | 48 MOCK_METHOD0(OnPlaying, void()); |
| 47 MOCK_METHOD0(OnPaused, void()); | 49 MOCK_METHOD0(OnPaused, void()); |
| 48 MOCK_METHOD0(OnError, void()); | 50 MOCK_METHOD0(OnError, void()); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 75 MOCK_METHOD0(Close, void()); | 77 MOCK_METHOD0(Close, void()); |
| 76 | 78 |
| 77 // Set/get the callback passed to Start(). | 79 // Set/get the callback passed to Start(). |
| 78 AudioSourceCallback* callback() const { return callback_; } | 80 AudioSourceCallback* callback() const { return callback_; } |
| 79 void SetCallback(AudioSourceCallback* asc) { callback_ = asc; } | 81 void SetCallback(AudioSourceCallback* asc) { callback_ = asc; } |
| 80 | 82 |
| 81 private: | 83 private: |
| 82 AudioSourceCallback* callback_; | 84 AudioSourceCallback* callback_; |
| 83 }; | 85 }; |
| 84 | 86 |
| 85 static const float kBufferNonZeroData = 1.0f; | 87 class MockAudioPushSink : public AudioPushSink { |
| 88 public: |
| 89 MOCK_METHOD0(Close, void()); |
| 90 MOCK_METHOD1(OnDataCheck, void(float)); |
| 91 |
| 92 void OnData(std::unique_ptr<AudioBus> source, |
| 93 base::TimeTicks reference_time) override { |
| 94 OnDataCheck(source->channel(0)[0]); |
| 95 } |
| 96 }; |
| 97 |
| 86 ACTION(PopulateBuffer) { | 98 ACTION(PopulateBuffer) { |
| 87 arg0->Zero(); | 99 arg0->Zero(); |
| 88 // Note: To confirm the buffer will be populated in these tests, it's | 100 // Note: To confirm the buffer will be populated in these tests, it's |
| 89 // sufficient that only the first float in channel 0 is set to the value. | 101 // sufficient that only the first float in channel 0 is set to the value. |
| 90 arg0->channel(0)[0] = kBufferNonZeroData; | 102 arg0->channel(0)[0] = kBufferNonZeroData; |
| 91 } | 103 } |
| 92 | 104 |
| 93 class AudioOutputControllerTest : public testing::Test { | 105 class AudioOutputControllerTest : public testing::Test { |
| 94 public: | 106 public: |
| 95 AudioOutputControllerTest() | 107 AudioOutputControllerTest() |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 .WillRepeatedly( | 182 .WillRepeatedly( |
| 171 Invoke(&mock_stream_, &MockAudioOutputStream::SetCallback)); | 183 Invoke(&mock_stream_, &MockAudioOutputStream::SetCallback)); |
| 172 EXPECT_CALL(mock_stream_, Stop()) | 184 EXPECT_CALL(mock_stream_, Stop()) |
| 173 .Times(num_times_to_be_started); | 185 .Times(num_times_to_be_started); |
| 174 } | 186 } |
| 175 | 187 |
| 176 controller_->StartDiverting(&mock_stream_); | 188 controller_->StartDiverting(&mock_stream_); |
| 177 base::RunLoop().RunUntilIdle(); | 189 base::RunLoop().RunUntilIdle(); |
| 178 } | 190 } |
| 179 | 191 |
| 192 void StartDuplicating(MockAudioPushSink* sink) { |
| 193 controller_->StartDuplicating(sink); |
| 194 base::RunLoop().RunUntilIdle(); |
| 195 } |
| 196 |
| 180 void ReadDivertedAudioData() { | 197 void ReadDivertedAudioData() { |
| 181 std::unique_ptr<AudioBus> dest = AudioBus::Create(params_); | 198 std::unique_ptr<AudioBus> dest = AudioBus::Create(params_); |
| 182 ASSERT_TRUE(mock_stream_.callback()); | 199 ASSERT_TRUE(mock_stream_.callback()); |
| 183 const int frames_read = | 200 const int frames_read = |
| 184 mock_stream_.callback()->OnMoreData(dest.get(), 0, 0); | 201 mock_stream_.callback()->OnMoreData(dest.get(), 0, 0); |
| 185 EXPECT_LT(0, frames_read); | 202 EXPECT_LT(0, frames_read); |
| 186 EXPECT_EQ(kBufferNonZeroData, dest->channel(0)[0]); | 203 EXPECT_EQ(kBufferNonZeroData, dest->channel(0)[0]); |
| 187 } | 204 } |
| 188 | 205 |
| 206 void ReadDuplicatedAudioData(const std::vector<MockAudioPushSink*>& sinks) { |
| 207 for (size_t i = 0; i < sinks.size(); i++) { |
| 208 EXPECT_CALL(*sinks[i], OnDataCheck(kBufferNonZeroData)); |
| 209 } |
| 210 |
| 211 std::unique_ptr<AudioBus> dest = AudioBus::Create(params_); |
| 212 |
| 213 // It is this OnMoreData() call that triggers |sink|'s OnData(). |
| 214 const int frames_read = controller_->OnMoreData(dest.get(), 0, 0); |
| 215 |
| 216 EXPECT_LT(0, frames_read); |
| 217 EXPECT_EQ(kBufferNonZeroData, dest->channel(0)[0]); |
| 218 |
| 219 base::RunLoop().RunUntilIdle(); |
| 220 } |
| 221 |
| 189 void Revert(bool was_playing) { | 222 void Revert(bool was_playing) { |
| 190 if (was_playing) { | 223 if (was_playing) { |
| 191 // Expect the handler to receive one OnPlaying() call as a result of the | 224 // Expect the handler to receive one OnPlaying() call as a result of the |
| 192 // stream switching back. | 225 // stream switching back. |
| 193 EXPECT_CALL(mock_event_handler_, OnPlaying()); | 226 EXPECT_CALL(mock_event_handler_, OnPlaying()); |
| 194 } | 227 } |
| 195 | 228 |
| 196 EXPECT_CALL(mock_stream_, Close()); | 229 EXPECT_CALL(mock_stream_, Close()); |
| 197 | 230 |
| 198 controller_->StopDiverting(); | 231 controller_->StopDiverting(); |
| 199 base::RunLoop().RunUntilIdle(); | 232 base::RunLoop().RunUntilIdle(); |
| 200 } | 233 } |
| 201 | 234 |
| 235 void StopDuplicating(MockAudioPushSink* sink) { |
| 236 EXPECT_CALL(*sink, Close()); |
| 237 controller_->StopDuplicating(sink); |
| 238 base::RunLoop().RunUntilIdle(); |
| 239 } |
| 240 |
| 202 void SwitchDevice(bool diverting) { | 241 void SwitchDevice(bool diverting) { |
| 203 if (!diverting) { | 242 if (!diverting) { |
| 204 // Expect the current stream to close and a new stream to start | 243 // Expect the current stream to close and a new stream to start |
| 205 // playing if not diverting. When diverting, nothing happens | 244 // playing if not diverting. When diverting, nothing happens |
| 206 // until diverting is stopped. | 245 // until diverting is stopped. |
| 207 EXPECT_CALL(mock_event_handler_, OnPlaying()); | 246 EXPECT_CALL(mock_event_handler_, OnPlaying()); |
| 208 } | 247 } |
| 209 | 248 |
| 210 controller_->SwitchOutputDevice( | 249 controller_->SwitchOutputDevice( |
| 211 AudioDeviceDescription::GetDefaultDeviceName(), | 250 AudioDeviceDescription::GetDefaultDeviceName(), |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 Close(); | 368 Close(); |
| 330 } | 369 } |
| 331 | 370 |
| 332 TEST_F(AudioOutputControllerTest, DivertRevertClose) { | 371 TEST_F(AudioOutputControllerTest, DivertRevertClose) { |
| 333 Create(kSamplesPerPacket); | 372 Create(kSamplesPerPacket); |
| 334 DivertNeverPlaying(); | 373 DivertNeverPlaying(); |
| 335 RevertWasNotPlaying(); | 374 RevertWasNotPlaying(); |
| 336 Close(); | 375 Close(); |
| 337 } | 376 } |
| 338 | 377 |
| 378 TEST_F(AudioOutputControllerTest, PlayDuplicateStopClose) { |
| 379 Create(kSamplesPerPacket); |
| 380 MockAudioPushSink mock_sink; |
| 381 Play(); |
| 382 StartDuplicating(&mock_sink); |
| 383 ReadDuplicatedAudioData({&mock_sink}); |
| 384 StopDuplicating(&mock_sink); |
| 385 Close(); |
| 386 } |
| 387 |
| 388 TEST_F(AudioOutputControllerTest, TwoDuplicates) { |
| 389 Create(kSamplesPerPacket); |
| 390 MockAudioPushSink mock_sink_1; |
| 391 MockAudioPushSink mock_sink_2; |
| 392 Play(); |
| 393 StartDuplicating(&mock_sink_1); |
| 394 StartDuplicating(&mock_sink_2); |
| 395 ReadDuplicatedAudioData({&mock_sink_1, &mock_sink_2}); |
| 396 StopDuplicating(&mock_sink_1); |
| 397 StopDuplicating(&mock_sink_2); |
| 398 Close(); |
| 399 } |
| 400 |
| 401 TEST_F(AudioOutputControllerTest, DuplicateDivertInteract) { |
| 402 Create(kSamplesPerPacket); |
| 403 MockAudioPushSink mock_sink; |
| 404 Play(); |
| 405 StartDuplicating(&mock_sink); |
| 406 DivertWhilePlaying(); |
| 407 |
| 408 // When diverted stream pulls data, it would trigger a push to sink. |
| 409 EXPECT_CALL(mock_sink, OnDataCheck(kBufferNonZeroData)); |
| 410 ReadDivertedAudioData(); |
| 411 |
| 412 StopDuplicating(&mock_sink); |
| 413 RevertWhilePlaying(); |
| 414 Close(); |
| 415 } |
| 416 |
| 417 TEST_F(AudioOutputControllerTest, DuplicateSwitchDeviceInteract) { |
| 418 Create(kSamplesPerPacket); |
| 419 MockAudioPushSink mock_sink; |
| 420 Play(); |
| 421 StartDuplicating(&mock_sink); |
| 422 ReadDuplicatedAudioData({&mock_sink}); |
| 423 |
| 424 // Switching device would trigger a read, and in turn it would trigger a push |
| 425 // to sink. |
| 426 EXPECT_CALL(mock_sink, OnDataCheck(kBufferNonZeroData)); |
| 427 SwitchDevice(false); |
| 428 |
| 429 StopDuplicating(&mock_sink); |
| 430 Close(); |
| 431 } |
| 432 |
| 339 } // namespace media | 433 } // namespace media |
| OLD | NEW |