| 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 #include <vector> | 10 #include <vector> |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO; | 41 static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO; |
| 42 static const int kSamplesPerPacket = kSampleRate / 100; | 42 static const int kSamplesPerPacket = kSampleRate / 100; |
| 43 static const double kTestVolume = 0.25; | 43 static const double kTestVolume = 0.25; |
| 44 static const float kBufferNonZeroData = 1.0f; | 44 static const float kBufferNonZeroData = 1.0f; |
| 45 | 45 |
| 46 class MockAudioOutputControllerEventHandler | 46 class MockAudioOutputControllerEventHandler |
| 47 : public AudioOutputController::EventHandler { | 47 : public AudioOutputController::EventHandler { |
| 48 public: | 48 public: |
| 49 MockAudioOutputControllerEventHandler() {} | 49 MockAudioOutputControllerEventHandler() {} |
| 50 | 50 |
| 51 MOCK_METHOD0(OnCreated, void()); | 51 MOCK_METHOD0(OnControllerCreated, void()); |
| 52 MOCK_METHOD0(OnPlaying, void()); | 52 MOCK_METHOD0(OnControllerPlaying, void()); |
| 53 MOCK_METHOD0(OnPaused, void()); | 53 MOCK_METHOD0(OnControllerPaused, void()); |
| 54 MOCK_METHOD0(OnError, void()); | 54 MOCK_METHOD0(OnControllerError, void()); |
| 55 | 55 |
| 56 private: | 56 private: |
| 57 DISALLOW_COPY_AND_ASSIGN(MockAudioOutputControllerEventHandler); | 57 DISALLOW_COPY_AND_ASSIGN(MockAudioOutputControllerEventHandler); |
| 58 }; | 58 }; |
| 59 | 59 |
| 60 class MockAudioOutputControllerSyncReader | 60 class MockAudioOutputControllerSyncReader |
| 61 : public AudioOutputController::SyncReader { | 61 : public AudioOutputController::SyncReader { |
| 62 public: | 62 public: |
| 63 MockAudioOutputControllerSyncReader() {} | 63 MockAudioOutputControllerSyncReader() {} |
| 64 | 64 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 | 118 |
| 119 ~AudioOutputControllerTest() override {} | 119 ~AudioOutputControllerTest() override {} |
| 120 | 120 |
| 121 protected: | 121 protected: |
| 122 void Create(int samples_per_packet) { | 122 void Create(int samples_per_packet) { |
| 123 params_ = AudioParameters( | 123 params_ = AudioParameters( |
| 124 AudioParameters::AUDIO_FAKE, kChannelLayout, | 124 AudioParameters::AUDIO_FAKE, kChannelLayout, |
| 125 kSampleRate, kBitsPerSample, samples_per_packet); | 125 kSampleRate, kBitsPerSample, samples_per_packet); |
| 126 | 126 |
| 127 if (params_.IsValid()) { | 127 if (params_.IsValid()) { |
| 128 EXPECT_CALL(mock_event_handler_, OnCreated()); | 128 EXPECT_CALL(mock_event_handler_, OnControllerCreated()); |
| 129 } | 129 } |
| 130 | 130 |
| 131 controller_ = AudioOutputController::Create( | 131 controller_ = AudioOutputController::Create( |
| 132 audio_manager_.get(), &mock_event_handler_, params_, std::string(), | 132 audio_manager_.get(), &mock_event_handler_, params_, std::string(), |
| 133 &mock_sync_reader_); | 133 &mock_sync_reader_); |
| 134 if (controller_.get()) | 134 if (controller_.get()) |
| 135 controller_->SetVolume(kTestVolume); | 135 controller_->SetVolume(kTestVolume); |
| 136 | 136 |
| 137 EXPECT_EQ(params_.IsValid(), controller_.get() != NULL); | 137 EXPECT_EQ(params_.IsValid(), controller_.get() != NULL); |
| 138 base::RunLoop().RunUntilIdle(); | 138 base::RunLoop().RunUntilIdle(); |
| 139 } | 139 } |
| 140 | 140 |
| 141 void Play() { | 141 void Play() { |
| 142 // Expect the event handler to receive one OnPlaying() call. | 142 // Expect the event handler to receive one OnControllerPlaying() call. |
| 143 EXPECT_CALL(mock_event_handler_, OnPlaying()); | 143 EXPECT_CALL(mock_event_handler_, OnControllerPlaying()); |
| 144 | 144 |
| 145 // During playback, the mock pretends to provide audio data rendered and | 145 // During playback, the mock pretends to provide audio data rendered and |
| 146 // sent from the render process. | 146 // sent from the render process. |
| 147 EXPECT_CALL(mock_sync_reader_, RequestMoreData(_, _, _)).Times(AtLeast(1)); | 147 EXPECT_CALL(mock_sync_reader_, RequestMoreData(_, _, _)).Times(AtLeast(1)); |
| 148 EXPECT_CALL(mock_sync_reader_, Read(_)).WillRepeatedly(PopulateBuffer()); | 148 EXPECT_CALL(mock_sync_reader_, Read(_)).WillRepeatedly(PopulateBuffer()); |
| 149 controller_->Play(); | 149 controller_->Play(); |
| 150 base::RunLoop().RunUntilIdle(); | 150 base::RunLoop().RunUntilIdle(); |
| 151 } | 151 } |
| 152 | 152 |
| 153 void Pause() { | 153 void Pause() { |
| 154 // Expect the event handler to receive one OnPaused() call. | 154 // Expect the event handler to receive one OnControllerPaused() call. |
| 155 EXPECT_CALL(mock_event_handler_, OnPaused()); | 155 EXPECT_CALL(mock_event_handler_, OnControllerPaused()); |
| 156 | 156 |
| 157 controller_->Pause(); | 157 controller_->Pause(); |
| 158 base::RunLoop().RunUntilIdle(); | 158 base::RunLoop().RunUntilIdle(); |
| 159 } | 159 } |
| 160 | 160 |
| 161 void ChangeDevice() { | 161 void ChangeDevice() { |
| 162 // Expect the event handler to receive one OnPaying() call and no OnPaused() | 162 // Expect the event handler to receive one OnControllerPaying() call and no |
| 163 // OnControllerPaused() |
| 163 // call. | 164 // call. |
| 164 EXPECT_CALL(mock_event_handler_, OnPlaying()); | 165 EXPECT_CALL(mock_event_handler_, OnControllerPlaying()); |
| 165 EXPECT_CALL(mock_event_handler_, OnPaused()) | 166 EXPECT_CALL(mock_event_handler_, OnControllerPaused()).Times(0); |
| 166 .Times(0); | |
| 167 | 167 |
| 168 // Simulate a device change event to AudioOutputController from the | 168 // Simulate a device change event to AudioOutputController from the |
| 169 // AudioManager. | 169 // AudioManager. |
| 170 audio_manager_->GetTaskRunner()->PostTask( | 170 audio_manager_->GetTaskRunner()->PostTask( |
| 171 FROM_HERE, | 171 FROM_HERE, |
| 172 base::Bind(&AudioOutputController::OnDeviceChange, controller_)); | 172 base::Bind(&AudioOutputController::OnDeviceChange, controller_)); |
| 173 } | 173 } |
| 174 | 174 |
| 175 void Divert(bool was_playing, int num_times_to_be_started) { | 175 void Divert(bool was_playing, int num_times_to_be_started) { |
| 176 if (was_playing) { | 176 if (was_playing) { |
| 177 // Expect the handler to receive one OnPlaying() call as a result of the | 177 // Expect the handler to receive one OnControllerPlaying() call as a |
| 178 // result of the |
| 178 // stream switching. | 179 // stream switching. |
| 179 EXPECT_CALL(mock_event_handler_, OnPlaying()); | 180 EXPECT_CALL(mock_event_handler_, OnControllerPlaying()); |
| 180 } | 181 } |
| 181 | 182 |
| 182 EXPECT_CALL(mock_stream_, Open()) | 183 EXPECT_CALL(mock_stream_, Open()) |
| 183 .WillOnce(Return(true)); | 184 .WillOnce(Return(true)); |
| 184 EXPECT_CALL(mock_stream_, SetVolume(kTestVolume)); | 185 EXPECT_CALL(mock_stream_, SetVolume(kTestVolume)); |
| 185 if (num_times_to_be_started > 0) { | 186 if (num_times_to_be_started > 0) { |
| 186 EXPECT_CALL(mock_stream_, Start(NotNull())) | 187 EXPECT_CALL(mock_stream_, Start(NotNull())) |
| 187 .Times(num_times_to_be_started) | 188 .Times(num_times_to_be_started) |
| 188 .WillRepeatedly( | 189 .WillRepeatedly( |
| 189 Invoke(&mock_stream_, &MockAudioOutputStream::SetCallback)); | 190 Invoke(&mock_stream_, &MockAudioOutputStream::SetCallback)); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 base::TimeDelta(), base::TimeTicks::Now(), 0, dest.get()); | 222 base::TimeDelta(), base::TimeTicks::Now(), 0, dest.get()); |
| 222 | 223 |
| 223 EXPECT_LT(0, frames_read); | 224 EXPECT_LT(0, frames_read); |
| 224 EXPECT_EQ(kBufferNonZeroData, dest->channel(0)[0]); | 225 EXPECT_EQ(kBufferNonZeroData, dest->channel(0)[0]); |
| 225 | 226 |
| 226 base::RunLoop().RunUntilIdle(); | 227 base::RunLoop().RunUntilIdle(); |
| 227 } | 228 } |
| 228 | 229 |
| 229 void Revert(bool was_playing) { | 230 void Revert(bool was_playing) { |
| 230 if (was_playing) { | 231 if (was_playing) { |
| 231 // Expect the handler to receive one OnPlaying() call as a result of the | 232 // Expect the handler to receive one OnControllerPlaying() call as a |
| 233 // result of the |
| 232 // stream switching back. | 234 // stream switching back. |
| 233 EXPECT_CALL(mock_event_handler_, OnPlaying()); | 235 EXPECT_CALL(mock_event_handler_, OnControllerPlaying()); |
| 234 } | 236 } |
| 235 | 237 |
| 236 EXPECT_CALL(mock_stream_, Close()); | 238 EXPECT_CALL(mock_stream_, Close()); |
| 237 | 239 |
| 238 controller_->StopDiverting(); | 240 controller_->StopDiverting(); |
| 239 base::RunLoop().RunUntilIdle(); | 241 base::RunLoop().RunUntilIdle(); |
| 240 } | 242 } |
| 241 | 243 |
| 242 void StopDuplicating(MockAudioPushSink* sink) { | 244 void StopDuplicating(MockAudioPushSink* sink) { |
| 243 EXPECT_CALL(*sink, Close()); | 245 EXPECT_CALL(*sink, Close()); |
| 244 controller_->StopDuplicating(sink); | 246 controller_->StopDuplicating(sink); |
| 245 base::RunLoop().RunUntilIdle(); | 247 base::RunLoop().RunUntilIdle(); |
| 246 } | 248 } |
| 247 | 249 |
| 248 void SwitchDevice(bool diverting) { | 250 void SwitchDevice(bool diverting) { |
| 249 if (!diverting) { | 251 if (!diverting) { |
| 250 // Expect the current stream to close and a new stream to start | 252 // Expect the current stream to close and a new stream to start |
| 251 // playing if not diverting. When diverting, nothing happens | 253 // playing if not diverting. When diverting, nothing happens |
| 252 // until diverting is stopped. | 254 // until diverting is stopped. |
| 253 EXPECT_CALL(mock_event_handler_, OnPlaying()); | 255 EXPECT_CALL(mock_event_handler_, OnControllerPlaying()); |
| 254 } | 256 } |
| 255 | 257 |
| 256 controller_->SwitchOutputDevice( | 258 controller_->SwitchOutputDevice( |
| 257 AudioDeviceDescription::GetDefaultDeviceName(), | 259 AudioDeviceDescription::GetDefaultDeviceName(), |
| 258 base::Bind(&base::DoNothing)); | 260 base::Bind(&base::DoNothing)); |
| 259 base::RunLoop().RunUntilIdle(); | 261 base::RunLoop().RunUntilIdle(); |
| 260 } | 262 } |
| 261 | 263 |
| 262 void Close() { | 264 void Close() { |
| 263 EXPECT_CALL(mock_sync_reader_, Close()); | 265 EXPECT_CALL(mock_sync_reader_, Close()); |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 431 // Switching device would trigger a read, and in turn it would trigger a push | 433 // Switching device would trigger a read, and in turn it would trigger a push |
| 432 // to sink. | 434 // to sink. |
| 433 EXPECT_CALL(mock_sink, OnDataCheck(kBufferNonZeroData)); | 435 EXPECT_CALL(mock_sink, OnDataCheck(kBufferNonZeroData)); |
| 434 SwitchDevice(false); | 436 SwitchDevice(false); |
| 435 | 437 |
| 436 StopDuplicating(&mock_sink); | 438 StopDuplicating(&mock_sink); |
| 437 Close(); | 439 Close(); |
| 438 } | 440 } |
| 439 | 441 |
| 440 } // namespace media | 442 } // namespace media |
| OLD | NEW |