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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 } // namespace. | 93 } // namespace. |
94 | 94 |
95 class AudioOutputDeviceTest | 95 class AudioOutputDeviceTest |
96 : public testing::Test, | 96 : public testing::Test, |
97 public testing::WithParamInterface<bool> { | 97 public testing::WithParamInterface<bool> { |
98 public: | 98 public: |
99 AudioOutputDeviceTest() | 99 AudioOutputDeviceTest() |
100 : default_audio_parameters_(AudioParameters::AUDIO_PCM_LINEAR, | 100 : default_audio_parameters_(AudioParameters::AUDIO_PCM_LINEAR, |
101 CHANNEL_LAYOUT_STEREO, | 101 CHANNEL_LAYOUT_STEREO, |
102 48000, 16, 1024), | 102 48000, 16, 1024), |
103 audio_device_(new AudioOutputDevice( | |
104 &audio_output_ipc_, io_loop_.message_loop_proxy())), | |
105 synchronized_io_(GetParam()), | 103 synchronized_io_(GetParam()), |
106 input_channels_(synchronized_io_ ? 2 : 0) { | 104 input_channels_(synchronized_io_ ? 2 : 0) { |
107 } | 105 } |
108 | 106 |
109 ~AudioOutputDeviceTest() {} | 107 ~AudioOutputDeviceTest() {} |
110 | 108 |
111 void Initialize(); | 109 void Initialize(); |
112 void StartAudioDevice(); | 110 void StartAudioDevice(); |
113 void CreateStream(); | 111 void CreateStream(); |
114 void ExpectRenderCallback(); | 112 void ExpectRenderCallback(); |
115 void WaitUntilRenderCallback(); | 113 void WaitUntilRenderCallback(); |
116 void StopAudioDevice(); | 114 void StopAudioDevice(); |
117 | 115 |
118 protected: | 116 protected: |
119 // Used to clean up TLS pointers that the test(s) will initialize. | 117 // Used to clean up TLS pointers that the test(s) will initialize. |
120 // Must remain the first member of this class. | 118 // Must remain the first member of this class. |
121 base::ShadowingAtExitManager at_exit_manager_; | 119 base::ShadowingAtExitManager at_exit_manager_; |
122 MessageLoopForIO io_loop_; | 120 MessageLoopForIO io_loop_; |
123 const AudioParameters default_audio_parameters_; | 121 const AudioParameters default_audio_parameters_; |
124 StrictMock<MockRenderCallback> callback_; | 122 StrictMock<MockRenderCallback> callback_; |
125 StrictMock<MockAudioOutputIPC> audio_output_ipc_; | 123 StrictMock<MockAudioOutputIPC>* audio_output_ipc_; // Owned by audio_device_. |
126 scoped_refptr<AudioOutputDevice> audio_device_; | 124 scoped_refptr<AudioOutputDevice> audio_device_; |
127 | 125 |
128 private: | 126 private: |
129 int CalculateMemorySize(); | 127 int CalculateMemorySize(); |
130 | 128 |
131 const bool synchronized_io_; | 129 const bool synchronized_io_; |
132 const int input_channels_; | 130 const int input_channels_; |
133 SharedMemory shared_memory_; | 131 SharedMemory shared_memory_; |
134 CancelableSyncSocket browser_socket_; | 132 CancelableSyncSocket browser_socket_; |
135 CancelableSyncSocket renderer_socket_; | 133 CancelableSyncSocket renderer_socket_; |
(...skipping 15 matching lines...) Expand all Loading... |
151 int io_buffer_size = output_memory_size + input_memory_size; | 149 int io_buffer_size = output_memory_size + input_memory_size; |
152 | 150 |
153 // This is where it gets a bit hacky. The shared memory contract between | 151 // This is where it gets a bit hacky. The shared memory contract between |
154 // AudioOutputDevice and its browser side counter part includes a bit more | 152 // AudioOutputDevice and its browser side counter part includes a bit more |
155 // than just the audio data, so we must call TotalSharedMemorySizeInBytes() | 153 // than just the audio data, so we must call TotalSharedMemorySizeInBytes() |
156 // to get the actual size needed to fit the audio data plus the extra data. | 154 // to get the actual size needed to fit the audio data plus the extra data. |
157 return TotalSharedMemorySizeInBytes(io_buffer_size); | 155 return TotalSharedMemorySizeInBytes(io_buffer_size); |
158 } | 156 } |
159 | 157 |
160 void AudioOutputDeviceTest::Initialize() { | 158 void AudioOutputDeviceTest::Initialize() { |
| 159 audio_output_ipc_ = new StrictMock<MockAudioOutputIPC>; |
| 160 audio_device_ = new AudioOutputDevice( |
| 161 scoped_ptr<AudioOutputIPC>(audio_output_ipc_), |
| 162 io_loop_.message_loop_proxy()); |
| 163 |
161 if (synchronized_io_) { | 164 if (synchronized_io_) { |
162 audio_device_->InitializeIO(default_audio_parameters_, | 165 audio_device_->InitializeIO(default_audio_parameters_, |
163 input_channels_, | 166 input_channels_, |
164 &callback_); | 167 &callback_); |
165 } else { | 168 } else { |
166 audio_device_->Initialize(default_audio_parameters_, | 169 audio_device_->Initialize(default_audio_parameters_, |
167 &callback_); | 170 &callback_); |
168 } | 171 } |
169 io_loop_.RunAllPending(); | 172 io_loop_.RunAllPending(); |
170 } | 173 } |
171 | 174 |
172 void AudioOutputDeviceTest::StartAudioDevice() { | 175 void AudioOutputDeviceTest::StartAudioDevice() { |
173 audio_device_->Start(); | 176 audio_device_->Start(); |
174 | 177 |
175 EXPECT_CALL(audio_output_ipc_, AddDelegate(audio_device_.get())) | 178 EXPECT_CALL(*audio_output_ipc_, AddDelegate(audio_device_.get())) |
176 .WillOnce(Return(kStreamId)); | 179 .WillOnce(Return(kStreamId)); |
177 EXPECT_CALL(audio_output_ipc_, CreateStream(kStreamId, _, _)); | 180 EXPECT_CALL(*audio_output_ipc_, CreateStream(kStreamId, _, _)); |
178 | 181 |
179 io_loop_.RunAllPending(); | 182 io_loop_.RunAllPending(); |
180 } | 183 } |
181 | 184 |
182 void AudioOutputDeviceTest::CreateStream() { | 185 void AudioOutputDeviceTest::CreateStream() { |
183 const int kMemorySize = CalculateMemorySize(); | 186 const int kMemorySize = CalculateMemorySize(); |
184 | 187 |
185 ASSERT_TRUE(shared_memory_.CreateAndMapAnonymous(kMemorySize)); | 188 ASSERT_TRUE(shared_memory_.CreateAndMapAnonymous(kMemorySize)); |
186 memset(shared_memory_.memory(), 0xff, kMemorySize); | 189 memset(shared_memory_.memory(), 0xff, kMemorySize); |
187 | 190 |
(...skipping 15 matching lines...) Expand all Loading... |
203 io_loop_.RunAllPending(); | 206 io_loop_.RunAllPending(); |
204 } | 207 } |
205 | 208 |
206 void AudioOutputDeviceTest::ExpectRenderCallback() { | 209 void AudioOutputDeviceTest::ExpectRenderCallback() { |
207 // We should get a 'play' notification when we call OnStreamCreated(). | 210 // We should get a 'play' notification when we call OnStreamCreated(). |
208 // Respond by asking for some audio data. This should ask our callback | 211 // Respond by asking for some audio data. This should ask our callback |
209 // to provide some audio data that AudioOutputDevice then writes into the | 212 // to provide some audio data that AudioOutputDevice then writes into the |
210 // shared memory section. | 213 // shared memory section. |
211 const int kMemorySize = CalculateMemorySize(); | 214 const int kMemorySize = CalculateMemorySize(); |
212 | 215 |
213 EXPECT_CALL(audio_output_ipc_, PlayStream(kStreamId)) | 216 EXPECT_CALL(*audio_output_ipc_, PlayStream(kStreamId)) |
214 .WillOnce(SendPendingBytes(&browser_socket_, kMemorySize)); | 217 .WillOnce(SendPendingBytes(&browser_socket_, kMemorySize)); |
215 | 218 |
216 // We expect calls to our audio renderer callback, which returns the number | 219 // We expect calls to our audio renderer callback, which returns the number |
217 // of frames written to the memory section. | 220 // of frames written to the memory section. |
218 // Here's the second place where it gets hacky: There's no way for us to | 221 // Here's the second place where it gets hacky: There's no way for us to |
219 // know (without using a sleep loop!) when the AudioOutputDevice has finished | 222 // know (without using a sleep loop!) when the AudioOutputDevice has finished |
220 // writing the interleaved audio data into the shared memory section. | 223 // writing the interleaved audio data into the shared memory section. |
221 // So, for the sake of this test, we consider the call to Render a sign | 224 // So, for the sake of this test, we consider the call to Render a sign |
222 // of success and quit the loop. | 225 // of success and quit the loop. |
223 if (synchronized_io_) { | 226 if (synchronized_io_) { |
(...skipping 13 matching lines...) Expand all Loading... |
237 void AudioOutputDeviceTest::WaitUntilRenderCallback() { | 240 void AudioOutputDeviceTest::WaitUntilRenderCallback() { |
238 // Don't hang the test if we never get the Render() callback. | 241 // Don't hang the test if we never get the Render() callback. |
239 io_loop_.PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(), | 242 io_loop_.PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(), |
240 TestTimeouts::action_timeout()); | 243 TestTimeouts::action_timeout()); |
241 io_loop_.Run(); | 244 io_loop_.Run(); |
242 } | 245 } |
243 | 246 |
244 void AudioOutputDeviceTest::StopAudioDevice() { | 247 void AudioOutputDeviceTest::StopAudioDevice() { |
245 audio_device_->Stop(); | 248 audio_device_->Stop(); |
246 | 249 |
247 EXPECT_CALL(audio_output_ipc_, CloseStream(kStreamId)); | 250 EXPECT_CALL(*audio_output_ipc_, CloseStream(kStreamId)); |
248 EXPECT_CALL(audio_output_ipc_, RemoveDelegate(kStreamId)); | 251 EXPECT_CALL(*audio_output_ipc_, RemoveDelegate(kStreamId)); |
249 | 252 |
250 io_loop_.RunAllPending(); | 253 io_loop_.RunAllPending(); |
251 } | 254 } |
252 | 255 |
253 TEST_P(AudioOutputDeviceTest, Initialize) { | 256 TEST_P(AudioOutputDeviceTest, Initialize) { |
254 Initialize(); | 257 Initialize(); |
255 } | 258 } |
256 | 259 |
257 // Calls Start() followed by an immediate Stop() and check for the basic message | 260 // Calls Start() followed by an immediate Stop() and check for the basic message |
258 // filter messages being sent in that case. | 261 // filter messages being sent in that case. |
(...skipping 16 matching lines...) Expand all Loading... |
275 // on the IO loop. | 278 // on the IO loop. |
276 TEST_P(AudioOutputDeviceTest, StopBeforeRender) { | 279 TEST_P(AudioOutputDeviceTest, StopBeforeRender) { |
277 Initialize(); | 280 Initialize(); |
278 StartAudioDevice(); | 281 StartAudioDevice(); |
279 | 282 |
280 // Call Stop() but don't run the IO loop yet. | 283 // Call Stop() but don't run the IO loop yet. |
281 audio_device_->Stop(); | 284 audio_device_->Stop(); |
282 | 285 |
283 // Expect us to shutdown IPC but not to render anything despite the stream | 286 // Expect us to shutdown IPC but not to render anything despite the stream |
284 // getting created. | 287 // getting created. |
285 EXPECT_CALL(audio_output_ipc_, CloseStream(kStreamId)); | 288 EXPECT_CALL(*audio_output_ipc_, CloseStream(kStreamId)); |
286 EXPECT_CALL(audio_output_ipc_, RemoveDelegate(kStreamId)); | 289 EXPECT_CALL(*audio_output_ipc_, RemoveDelegate(kStreamId)); |
287 CreateStream(); | 290 CreateStream(); |
288 } | 291 } |
289 | 292 |
290 // Full test with output only. | 293 // Full test with output only. |
291 TEST_P(AudioOutputDeviceTest, CreateStream) { | 294 TEST_P(AudioOutputDeviceTest, CreateStream) { |
292 Initialize(); | 295 Initialize(); |
293 StartAudioDevice(); | 296 StartAudioDevice(); |
294 ExpectRenderCallback(); | 297 ExpectRenderCallback(); |
295 CreateStream(); | 298 CreateStream(); |
296 WaitUntilRenderCallback(); | 299 WaitUntilRenderCallback(); |
297 StopAudioDevice(); | 300 StopAudioDevice(); |
298 } | 301 } |
299 | 302 |
300 INSTANTIATE_TEST_CASE_P(Render, AudioOutputDeviceTest, Values(false)); | 303 INSTANTIATE_TEST_CASE_P(Render, AudioOutputDeviceTest, Values(false)); |
301 INSTANTIATE_TEST_CASE_P(RenderIO, AudioOutputDeviceTest, Values(true)); | 304 INSTANTIATE_TEST_CASE_P(RenderIO, AudioOutputDeviceTest, Values(true)); |
302 | 305 |
303 } // namespace media. | 306 } // namespace media. |
OLD | NEW |