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