| 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/at_exit.h" | 7 #include "base/at_exit.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/process_util.h" | 9 #include "base/process_util.h" |
| 10 #include "base/shared_memory.h" | 10 #include "base/shared_memory.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 AudioBus* dest, | 42 AudioBus* dest, |
| 43 int audio_delay_milliseconds)); | 43 int audio_delay_milliseconds)); |
| 44 MOCK_METHOD0(OnRenderError, void()); | 44 MOCK_METHOD0(OnRenderError, void()); |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 class MockAudioOutputIPC : public AudioOutputIPC { | 47 class MockAudioOutputIPC : public AudioOutputIPC { |
| 48 public: | 48 public: |
| 49 MockAudioOutputIPC() {} | 49 MockAudioOutputIPC() {} |
| 50 virtual ~MockAudioOutputIPC() {} | 50 virtual ~MockAudioOutputIPC() {} |
| 51 | 51 |
| 52 MOCK_METHOD1(AddDelegate, int(AudioOutputIPCDelegate* delegate)); | 52 MOCK_METHOD2(CreateStream, void(AudioOutputIPCDelegate* delegate, |
| 53 MOCK_METHOD1(RemoveDelegate, void(int stream_id)); | 53 const AudioParameters& params)); |
| 54 | 54 MOCK_METHOD0(PlayStream, void()); |
| 55 MOCK_METHOD2(CreateStream, | 55 MOCK_METHOD0(PauseStream, void()); |
| 56 void(int stream_id, const AudioParameters& params)); | 56 MOCK_METHOD0(CloseStream, void()); |
| 57 MOCK_METHOD1(PlayStream, void(int stream_id)); | 57 MOCK_METHOD1(SetVolume, void(double volume)); |
| 58 MOCK_METHOD1(CloseStream, void(int stream_id)); | |
| 59 MOCK_METHOD2(SetVolume, void(int stream_id, double volume)); | |
| 60 MOCK_METHOD1(PauseStream, void(int stream_id)); | |
| 61 }; | 58 }; |
| 62 | 59 |
| 63 // Creates a copy of a SyncSocket handle that we can give to AudioOutputDevice. | 60 // Creates a copy of a SyncSocket handle that we can give to AudioOutputDevice. |
| 64 // On Windows this means duplicating the pipe handle so that AudioOutputDevice | 61 // On Windows this means duplicating the pipe handle so that AudioOutputDevice |
| 65 // can call CloseHandle() (since ownership has been transferred), but on other | 62 // can call CloseHandle() (since ownership has been transferred), but on other |
| 66 // platforms, we just copy the same socket handle since AudioOutputDevice on | 63 // platforms, we just copy the same socket handle since AudioOutputDevice on |
| 67 // those platforms won't actually own the socket (FileDescriptor.auto_close is | 64 // those platforms won't actually own the socket (FileDescriptor.auto_close is |
| 68 // false). | 65 // false). |
| 69 bool DuplicateSocketHandle(SyncSocket::Handle socket_handle, | 66 bool DuplicateSocketHandle(SyncSocket::Handle socket_handle, |
| 70 SyncSocket::Handle* copy) { | 67 SyncSocket::Handle* copy) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 void WaitUntilRenderCallback(); | 101 void WaitUntilRenderCallback(); |
| 105 void StopAudioDevice(); | 102 void StopAudioDevice(); |
| 106 | 103 |
| 107 protected: | 104 protected: |
| 108 // Used to clean up TLS pointers that the test(s) will initialize. | 105 // Used to clean up TLS pointers that the test(s) will initialize. |
| 109 // Must remain the first member of this class. | 106 // Must remain the first member of this class. |
| 110 base::ShadowingAtExitManager at_exit_manager_; | 107 base::ShadowingAtExitManager at_exit_manager_; |
| 111 MessageLoopForIO io_loop_; | 108 MessageLoopForIO io_loop_; |
| 112 AudioParameters default_audio_parameters_; | 109 AudioParameters default_audio_parameters_; |
| 113 StrictMock<MockRenderCallback> callback_; | 110 StrictMock<MockRenderCallback> callback_; |
| 114 StrictMock<MockAudioOutputIPC> audio_output_ipc_; | 111 MockAudioOutputIPC* audio_output_ipc_; // owned by audio_device_ |
| 115 scoped_refptr<AudioOutputDevice> audio_device_; | 112 scoped_refptr<AudioOutputDevice> audio_device_; |
| 116 | 113 |
| 117 private: | 114 private: |
| 118 int CalculateMemorySize(); | 115 int CalculateMemorySize(); |
| 119 | 116 |
| 120 const bool synchronized_io_; | 117 const bool synchronized_io_; |
| 121 const int input_channels_; | 118 const int input_channels_; |
| 122 SharedMemory shared_memory_; | 119 SharedMemory shared_memory_; |
| 123 CancelableSyncSocket browser_socket_; | 120 CancelableSyncSocket browser_socket_; |
| 124 CancelableSyncSocket renderer_socket_; | 121 CancelableSyncSocket renderer_socket_; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 147 } | 144 } |
| 148 | 145 |
| 149 AudioOutputDeviceTest::AudioOutputDeviceTest() | 146 AudioOutputDeviceTest::AudioOutputDeviceTest() |
| 150 : synchronized_io_(GetParam()), | 147 : synchronized_io_(GetParam()), |
| 151 input_channels_(synchronized_io_ ? 2 : 0) { | 148 input_channels_(synchronized_io_ ? 2 : 0) { |
| 152 default_audio_parameters_.Reset( | 149 default_audio_parameters_.Reset( |
| 153 AudioParameters::AUDIO_PCM_LINEAR, | 150 AudioParameters::AUDIO_PCM_LINEAR, |
| 154 CHANNEL_LAYOUT_STEREO, 2, input_channels_, | 151 CHANNEL_LAYOUT_STEREO, 2, input_channels_, |
| 155 48000, 16, 1024); | 152 48000, 16, 1024); |
| 156 | 153 |
| 154 audio_output_ipc_ = new MockAudioOutputIPC(); |
| 157 audio_device_ = new AudioOutputDevice( | 155 audio_device_ = new AudioOutputDevice( |
| 158 &audio_output_ipc_, io_loop_.message_loop_proxy()); | 156 scoped_ptr<AudioOutputIPC>(audio_output_ipc_), |
| 157 io_loop_.message_loop_proxy()); |
| 159 | 158 |
| 160 audio_device_->Initialize(default_audio_parameters_, | 159 audio_device_->Initialize(default_audio_parameters_, |
| 161 &callback_); | 160 &callback_); |
| 162 | 161 |
| 163 io_loop_.RunUntilIdle(); | 162 io_loop_.RunUntilIdle(); |
| 164 } | 163 } |
| 165 | 164 |
| 166 AudioOutputDeviceTest::~AudioOutputDeviceTest() { | 165 AudioOutputDeviceTest::~AudioOutputDeviceTest() { |
| 167 audio_device_ = NULL; | 166 audio_device_ = NULL; |
| 168 } | 167 } |
| 169 | 168 |
| 170 void AudioOutputDeviceTest::StartAudioDevice() { | 169 void AudioOutputDeviceTest::StartAudioDevice() { |
| 171 audio_device_->Start(); | 170 audio_device_->Start(); |
| 172 | 171 |
| 173 EXPECT_CALL(audio_output_ipc_, AddDelegate(_)).WillOnce(Return(kStreamId)); | 172 EXPECT_CALL(*audio_output_ipc_, CreateStream(audio_device_.get(), _)); |
| 174 EXPECT_CALL(audio_output_ipc_, CreateStream(kStreamId, _)); | |
| 175 | 173 |
| 176 io_loop_.RunUntilIdle(); | 174 io_loop_.RunUntilIdle(); |
| 177 } | 175 } |
| 178 | 176 |
| 179 void AudioOutputDeviceTest::CreateStream() { | 177 void AudioOutputDeviceTest::CreateStream() { |
| 180 const int kMemorySize = CalculateMemorySize(); | 178 const int kMemorySize = CalculateMemorySize(); |
| 181 | 179 |
| 182 ASSERT_TRUE(shared_memory_.CreateAndMapAnonymous(kMemorySize)); | 180 ASSERT_TRUE(shared_memory_.CreateAndMapAnonymous(kMemorySize)); |
| 183 memset(shared_memory_.memory(), 0xff, kMemorySize); | 181 memset(shared_memory_.memory(), 0xff, kMemorySize); |
| 184 | 182 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 200 io_loop_.RunUntilIdle(); | 198 io_loop_.RunUntilIdle(); |
| 201 } | 199 } |
| 202 | 200 |
| 203 void AudioOutputDeviceTest::ExpectRenderCallback() { | 201 void AudioOutputDeviceTest::ExpectRenderCallback() { |
| 204 // We should get a 'play' notification when we call OnStreamCreated(). | 202 // We should get a 'play' notification when we call OnStreamCreated(). |
| 205 // Respond by asking for some audio data. This should ask our callback | 203 // Respond by asking for some audio data. This should ask our callback |
| 206 // to provide some audio data that AudioOutputDevice then writes into the | 204 // to provide some audio data that AudioOutputDevice then writes into the |
| 207 // shared memory section. | 205 // shared memory section. |
| 208 const int kMemorySize = CalculateMemorySize(); | 206 const int kMemorySize = CalculateMemorySize(); |
| 209 | 207 |
| 210 EXPECT_CALL(audio_output_ipc_, PlayStream(kStreamId)) | 208 EXPECT_CALL(*audio_output_ipc_, PlayStream()) |
| 211 .WillOnce(SendPendingBytes(&browser_socket_, kMemorySize)); | 209 .WillOnce(SendPendingBytes(&browser_socket_, kMemorySize)); |
| 212 | 210 |
| 213 // We expect calls to our audio renderer callback, which returns the number | 211 // We expect calls to our audio renderer callback, which returns the number |
| 214 // of frames written to the memory section. | 212 // of frames written to the memory section. |
| 215 // Here's the second place where it gets hacky: There's no way for us to | 213 // Here's the second place where it gets hacky: There's no way for us to |
| 216 // know (without using a sleep loop!) when the AudioOutputDevice has finished | 214 // know (without using a sleep loop!) when the AudioOutputDevice has finished |
| 217 // writing the interleaved audio data into the shared memory section. | 215 // writing the interleaved audio data into the shared memory section. |
| 218 // So, for the sake of this test, we consider the call to Render a sign | 216 // So, for the sake of this test, we consider the call to Render a sign |
| 219 // of success and quit the loop. | 217 // of success and quit the loop. |
| 220 if (synchronized_io_) { | 218 if (synchronized_io_) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 234 void AudioOutputDeviceTest::WaitUntilRenderCallback() { | 232 void AudioOutputDeviceTest::WaitUntilRenderCallback() { |
| 235 // Don't hang the test if we never get the Render() callback. | 233 // Don't hang the test if we never get the Render() callback. |
| 236 io_loop_.PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(), | 234 io_loop_.PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(), |
| 237 TestTimeouts::action_timeout()); | 235 TestTimeouts::action_timeout()); |
| 238 io_loop_.Run(); | 236 io_loop_.Run(); |
| 239 } | 237 } |
| 240 | 238 |
| 241 void AudioOutputDeviceTest::StopAudioDevice() { | 239 void AudioOutputDeviceTest::StopAudioDevice() { |
| 242 audio_device_->Stop(); | 240 audio_device_->Stop(); |
| 243 | 241 |
| 244 EXPECT_CALL(audio_output_ipc_, CloseStream(kStreamId)); | 242 EXPECT_CALL(*audio_output_ipc_, CloseStream()); |
| 245 EXPECT_CALL(audio_output_ipc_, RemoveDelegate(kStreamId)); | |
| 246 | 243 |
| 247 io_loop_.RunUntilIdle(); | 244 io_loop_.RunUntilIdle(); |
| 248 } | 245 } |
| 249 | 246 |
| 250 TEST_P(AudioOutputDeviceTest, Initialize) { | 247 TEST_P(AudioOutputDeviceTest, Initialize) { |
| 251 // Tests that the object can be constructed, initialized and destructed | 248 // Tests that the object can be constructed, initialized and destructed |
| 252 // without having ever been started/stopped. | 249 // without having ever been started/stopped. |
| 253 } | 250 } |
| 254 | 251 |
| 255 // Calls Start() followed by an immediate Stop() and check for the basic message | 252 // Calls Start() followed by an immediate Stop() and check for the basic message |
| (...skipping 14 matching lines...) Expand all Loading... |
| 270 // Simulate receiving OnStreamCreated() prior to processing ShutDownOnIOThread() | 267 // Simulate receiving OnStreamCreated() prior to processing ShutDownOnIOThread() |
| 271 // on the IO loop. | 268 // on the IO loop. |
| 272 TEST_P(AudioOutputDeviceTest, StopBeforeRender) { | 269 TEST_P(AudioOutputDeviceTest, StopBeforeRender) { |
| 273 StartAudioDevice(); | 270 StartAudioDevice(); |
| 274 | 271 |
| 275 // Call Stop() but don't run the IO loop yet. | 272 // Call Stop() but don't run the IO loop yet. |
| 276 audio_device_->Stop(); | 273 audio_device_->Stop(); |
| 277 | 274 |
| 278 // Expect us to shutdown IPC but not to render anything despite the stream | 275 // Expect us to shutdown IPC but not to render anything despite the stream |
| 279 // getting created. | 276 // getting created. |
| 280 EXPECT_CALL(audio_output_ipc_, CloseStream(kStreamId)); | 277 EXPECT_CALL(*audio_output_ipc_, CloseStream()); |
| 281 EXPECT_CALL(audio_output_ipc_, RemoveDelegate(kStreamId)); | |
| 282 CreateStream(); | 278 CreateStream(); |
| 283 } | 279 } |
| 284 | 280 |
| 285 // Full test with output only. | 281 // Full test with output only. |
| 286 TEST_P(AudioOutputDeviceTest, CreateStream) { | 282 TEST_P(AudioOutputDeviceTest, CreateStream) { |
| 287 StartAudioDevice(); | 283 StartAudioDevice(); |
| 288 ExpectRenderCallback(); | 284 ExpectRenderCallback(); |
| 289 CreateStream(); | 285 CreateStream(); |
| 290 WaitUntilRenderCallback(); | 286 WaitUntilRenderCallback(); |
| 291 StopAudioDevice(); | 287 StopAudioDevice(); |
| 292 } | 288 } |
| 293 | 289 |
| 294 INSTANTIATE_TEST_CASE_P(Render, AudioOutputDeviceTest, Values(false)); | 290 INSTANTIATE_TEST_CASE_P(Render, AudioOutputDeviceTest, Values(false)); |
| 295 INSTANTIATE_TEST_CASE_P(RenderIO, AudioOutputDeviceTest, Values(true)); | 291 INSTANTIATE_TEST_CASE_P(RenderIO, AudioOutputDeviceTest, Values(true)); |
| 296 | 292 |
| 297 } // namespace media. | 293 } // namespace media. |
| OLD | NEW |