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 <stdint.h> | 5 #include <stdint.h> |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/memory/shared_memory.h" | 14 #include "base/memory/shared_memory.h" |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
16 #include "base/process/process_handle.h" | 16 #include "base/process/process_handle.h" |
| 17 #include "base/run_loop.h" |
17 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
18 #include "base/sync_socket.h" | 19 #include "base/sync_socket.h" |
19 #include "base/task_runner.h" | 20 #include "base/task_runner.h" |
20 #include "base/test/test_timeouts.h" | 21 #include "base/test/test_timeouts.h" |
21 #include "base/threading/thread.h" | 22 #include "base/threading/thread.h" |
22 #include "base/threading/thread_task_runner_handle.h" | 23 #include "base/threading/thread_task_runner_handle.h" |
23 #include "media/audio/audio_output_device.h" | 24 #include "media/audio/audio_output_device.h" |
24 #include "media/audio/sample_rates.h" | 25 #include "media/audio/sample_rates.h" |
25 #include "media/base/test_helpers.h" | 26 #include "media/base/test_helpers.h" |
26 #include "testing/gmock/include/gmock/gmock.h" | 27 #include "testing/gmock/include/gmock/gmock.h" |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 base::WrapUnique(audio_output_ipc_), io_loop_.task_runner(), 0, device_id, | 152 base::WrapUnique(audio_output_ipc_), io_loop_.task_runner(), 0, device_id, |
152 url::Origin(), | 153 url::Origin(), |
153 base::TimeDelta::FromMilliseconds(kAuthTimeoutForTestingMs)); | 154 base::TimeDelta::FromMilliseconds(kAuthTimeoutForTestingMs)); |
154 } | 155 } |
155 | 156 |
156 void AudioOutputDeviceTest::SetDevice(const std::string& device_id) { | 157 void AudioOutputDeviceTest::SetDevice(const std::string& device_id) { |
157 CreateDevice(device_id); | 158 CreateDevice(device_id); |
158 EXPECT_CALL(*audio_output_ipc_, | 159 EXPECT_CALL(*audio_output_ipc_, |
159 RequestDeviceAuthorization(audio_device_.get(), 0, device_id, _)); | 160 RequestDeviceAuthorization(audio_device_.get(), 0, device_id, _)); |
160 audio_device_->RequestDeviceAuthorization(); | 161 audio_device_->RequestDeviceAuthorization(); |
161 io_loop_.RunUntilIdle(); | 162 base::RunLoop().RunUntilIdle(); |
162 | 163 |
163 // Simulate response from browser | 164 // Simulate response from browser |
164 OutputDeviceStatus device_status = | 165 OutputDeviceStatus device_status = |
165 (device_id == kUnauthorizedDeviceId) | 166 (device_id == kUnauthorizedDeviceId) |
166 ? OUTPUT_DEVICE_STATUS_ERROR_NOT_AUTHORIZED | 167 ? OUTPUT_DEVICE_STATUS_ERROR_NOT_AUTHORIZED |
167 : OUTPUT_DEVICE_STATUS_OK; | 168 : OUTPUT_DEVICE_STATUS_OK; |
168 ReceiveAuthorization(device_status); | 169 ReceiveAuthorization(device_status); |
169 | 170 |
170 audio_device_->Initialize(default_audio_parameters_, | 171 audio_device_->Initialize(default_audio_parameters_, |
171 &callback_); | 172 &callback_); |
172 } | 173 } |
173 | 174 |
174 void AudioOutputDeviceTest::CheckDeviceStatus(OutputDeviceStatus status) { | 175 void AudioOutputDeviceTest::CheckDeviceStatus(OutputDeviceStatus status) { |
175 DCHECK(!io_loop_.task_runner()->BelongsToCurrentThread()); | 176 DCHECK(!io_loop_.task_runner()->BelongsToCurrentThread()); |
176 EXPECT_EQ(status, audio_device_->GetOutputDeviceInfo().device_status()); | 177 EXPECT_EQ(status, audio_device_->GetOutputDeviceInfo().device_status()); |
177 } | 178 } |
178 | 179 |
179 void AudioOutputDeviceTest::ReceiveAuthorization(OutputDeviceStatus status) { | 180 void AudioOutputDeviceTest::ReceiveAuthorization(OutputDeviceStatus status) { |
180 device_status_ = status; | 181 device_status_ = status; |
181 if (device_status_ != OUTPUT_DEVICE_STATUS_OK) | 182 if (device_status_ != OUTPUT_DEVICE_STATUS_OK) |
182 EXPECT_CALL(*audio_output_ipc_, CloseStream()); | 183 EXPECT_CALL(*audio_output_ipc_, CloseStream()); |
183 | 184 |
184 audio_device_->OnDeviceAuthorized(device_status_, default_audio_parameters_, | 185 audio_device_->OnDeviceAuthorized(device_status_, default_audio_parameters_, |
185 kDefaultDeviceId); | 186 kDefaultDeviceId); |
186 io_loop_.RunUntilIdle(); | 187 base::RunLoop().RunUntilIdle(); |
187 } | 188 } |
188 | 189 |
189 void AudioOutputDeviceTest::StartAudioDevice() { | 190 void AudioOutputDeviceTest::StartAudioDevice() { |
190 if (device_status_ == OUTPUT_DEVICE_STATUS_OK) | 191 if (device_status_ == OUTPUT_DEVICE_STATUS_OK) |
191 EXPECT_CALL(*audio_output_ipc_, CreateStream(audio_device_.get(), _)); | 192 EXPECT_CALL(*audio_output_ipc_, CreateStream(audio_device_.get(), _)); |
192 else | 193 else |
193 EXPECT_CALL(callback_, OnRenderError()); | 194 EXPECT_CALL(callback_, OnRenderError()); |
194 | 195 |
195 audio_device_->Start(); | 196 audio_device_->Start(); |
196 io_loop_.RunUntilIdle(); | 197 base::RunLoop().RunUntilIdle(); |
197 } | 198 } |
198 | 199 |
199 void AudioOutputDeviceTest::CreateStream() { | 200 void AudioOutputDeviceTest::CreateStream() { |
200 const int kMemorySize = CalculateMemorySize(); | 201 const int kMemorySize = CalculateMemorySize(); |
201 | 202 |
202 ASSERT_TRUE(shared_memory_.CreateAndMapAnonymous(kMemorySize)); | 203 ASSERT_TRUE(shared_memory_.CreateAndMapAnonymous(kMemorySize)); |
203 memset(shared_memory_.memory(), 0xff, kMemorySize); | 204 memset(shared_memory_.memory(), 0xff, kMemorySize); |
204 | 205 |
205 ASSERT_TRUE(CancelableSyncSocket::CreatePair(&browser_socket_, | 206 ASSERT_TRUE(CancelableSyncSocket::CreatePair(&browser_socket_, |
206 &renderer_socket_)); | 207 &renderer_socket_)); |
207 | 208 |
208 // Create duplicates of the handles we pass to AudioOutputDevice since | 209 // Create duplicates of the handles we pass to AudioOutputDevice since |
209 // ownership will be transferred and AudioOutputDevice is responsible for | 210 // ownership will be transferred and AudioOutputDevice is responsible for |
210 // freeing. | 211 // freeing. |
211 SyncSocket::TransitDescriptor audio_device_socket_descriptor; | 212 SyncSocket::TransitDescriptor audio_device_socket_descriptor; |
212 ASSERT_TRUE(renderer_socket_.PrepareTransitDescriptor( | 213 ASSERT_TRUE(renderer_socket_.PrepareTransitDescriptor( |
213 base::GetCurrentProcessHandle(), &audio_device_socket_descriptor)); | 214 base::GetCurrentProcessHandle(), &audio_device_socket_descriptor)); |
214 base::SharedMemoryHandle duplicated_memory_handle; | 215 base::SharedMemoryHandle duplicated_memory_handle; |
215 ASSERT_TRUE(shared_memory_.ShareToProcess(base::GetCurrentProcessHandle(), | 216 ASSERT_TRUE(shared_memory_.ShareToProcess(base::GetCurrentProcessHandle(), |
216 &duplicated_memory_handle)); | 217 &duplicated_memory_handle)); |
217 | 218 |
218 audio_device_->OnStreamCreated( | 219 audio_device_->OnStreamCreated( |
219 duplicated_memory_handle, | 220 duplicated_memory_handle, |
220 SyncSocket::UnwrapHandle(audio_device_socket_descriptor), kMemorySize); | 221 SyncSocket::UnwrapHandle(audio_device_socket_descriptor), kMemorySize); |
221 io_loop_.RunUntilIdle(); | 222 base::RunLoop().RunUntilIdle(); |
222 } | 223 } |
223 | 224 |
224 void AudioOutputDeviceTest::ExpectRenderCallback() { | 225 void AudioOutputDeviceTest::ExpectRenderCallback() { |
225 // We should get a 'play' notification when we call OnStreamCreated(). | 226 // We should get a 'play' notification when we call OnStreamCreated(). |
226 // Respond by asking for some audio data. This should ask our callback | 227 // Respond by asking for some audio data. This should ask our callback |
227 // to provide some audio data that AudioOutputDevice then writes into the | 228 // to provide some audio data that AudioOutputDevice then writes into the |
228 // shared memory section. | 229 // shared memory section. |
229 const int kMemorySize = CalculateMemorySize(); | 230 const int kMemorySize = CalculateMemorySize(); |
230 | 231 |
231 EXPECT_CALL(*audio_output_ipc_, PlayStream()) | 232 EXPECT_CALL(*audio_output_ipc_, PlayStream()) |
(...skipping 10 matching lines...) Expand all Loading... |
242 EXPECT_CALL(callback_, Render(_, _, _)) | 243 EXPECT_CALL(callback_, Render(_, _, _)) |
243 .WillOnce(DoAll(QuitLoop(io_loop_.task_runner()), | 244 .WillOnce(DoAll(QuitLoop(io_loop_.task_runner()), |
244 Return(kNumberOfFramesToProcess))); | 245 Return(kNumberOfFramesToProcess))); |
245 } | 246 } |
246 | 247 |
247 void AudioOutputDeviceTest::WaitUntilRenderCallback() { | 248 void AudioOutputDeviceTest::WaitUntilRenderCallback() { |
248 // Don't hang the test if we never get the Render() callback. | 249 // Don't hang the test if we never get the Render() callback. |
249 io_loop_.task_runner()->PostDelayedTask( | 250 io_loop_.task_runner()->PostDelayedTask( |
250 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), | 251 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), |
251 TestTimeouts::action_timeout()); | 252 TestTimeouts::action_timeout()); |
252 io_loop_.Run(); | 253 base::RunLoop().Run(); |
253 } | 254 } |
254 | 255 |
255 void AudioOutputDeviceTest::StopAudioDevice() { | 256 void AudioOutputDeviceTest::StopAudioDevice() { |
256 if (device_status_ == OUTPUT_DEVICE_STATUS_OK) | 257 if (device_status_ == OUTPUT_DEVICE_STATUS_OK) |
257 EXPECT_CALL(*audio_output_ipc_, CloseStream()); | 258 EXPECT_CALL(*audio_output_ipc_, CloseStream()); |
258 | 259 |
259 audio_device_->Stop(); | 260 audio_device_->Stop(); |
260 io_loop_.RunUntilIdle(); | 261 base::RunLoop().RunUntilIdle(); |
261 } | 262 } |
262 | 263 |
263 TEST_P(AudioOutputDeviceTest, Initialize) { | 264 TEST_P(AudioOutputDeviceTest, Initialize) { |
264 // Tests that the object can be constructed, initialized and destructed | 265 // Tests that the object can be constructed, initialized and destructed |
265 // without having ever been started. | 266 // without having ever been started. |
266 StopAudioDevice(); | 267 StopAudioDevice(); |
267 } | 268 } |
268 | 269 |
269 // Calls Start() followed by an immediate Stop() and check for the basic message | 270 // Calls Start() followed by an immediate Stop() and check for the basic message |
270 // filter messages being sent in that case. | 271 // filter messages being sent in that case. |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 | 351 |
351 media::WaitableMessageLoopEvent event; | 352 media::WaitableMessageLoopEvent event; |
352 | 353 |
353 // Request device info on another thread. | 354 // Request device info on another thread. |
354 thread.task_runner()->PostTaskAndReply( | 355 thread.task_runner()->PostTaskAndReply( |
355 FROM_HERE, | 356 FROM_HERE, |
356 base::Bind(&AudioOutputDeviceTest::CheckDeviceStatus, | 357 base::Bind(&AudioOutputDeviceTest::CheckDeviceStatus, |
357 base::Unretained(this), OUTPUT_DEVICE_STATUS_ERROR_TIMED_OUT), | 358 base::Unretained(this), OUTPUT_DEVICE_STATUS_ERROR_TIMED_OUT), |
358 event.GetClosure()); | 359 event.GetClosure()); |
359 | 360 |
360 io_loop_.RunUntilIdle(); | 361 base::RunLoop().RunUntilIdle(); |
361 | 362 |
362 // Runs the loop and waits for |thread| to call event's closure. | 363 // Runs the loop and waits for |thread| to call event's closure. |
363 event.RunAndWait(); | 364 event.RunAndWait(); |
364 | 365 |
365 audio_device_->Stop(); | 366 audio_device_->Stop(); |
366 io_loop_.RunUntilIdle(); | 367 base::RunLoop().RunUntilIdle(); |
367 } | 368 } |
368 | 369 |
369 INSTANTIATE_TEST_CASE_P(Render, AudioOutputDeviceTest, Values(false)); | 370 INSTANTIATE_TEST_CASE_P(Render, AudioOutputDeviceTest, Values(false)); |
370 | 371 |
371 } // namespace media. | 372 } // namespace media. |
OLD | NEW |