OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/environment.h" | 5 #include "base/environment.h" |
6 #include "base/basictypes.h" | 6 #include "base/basictypes.h" |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/task.h" | 8 #include "base/task.h" |
9 #include "base/waitable_event.h" | 9 #include "base/waitable_event.h" |
10 #include "media/audio/audio_output_controller.h" | 10 #include "media/audio/audio_output_controller.h" |
11 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 using ::testing::_; | 14 using ::testing::_; |
15 using ::testing::AtLeast; | 15 using ::testing::AtLeast; |
16 using ::testing::Exactly; | 16 using ::testing::Exactly; |
17 using ::testing::InvokeWithoutArgs; | 17 using ::testing::InvokeWithoutArgs; |
18 using ::testing::NotNull; | 18 using ::testing::NotNull; |
19 using ::testing::Return; | 19 using ::testing::Return; |
20 | 20 |
21 static const int kSampleRate = AudioParameters::kAudioCDSampleRate; | 21 static const int kSampleRate = AudioParameters::kAudioCDSampleRate; |
22 static const int kBitsPerSample = 16; | 22 static const int kBitsPerSample = 16; |
23 static const int kChannels = 2; | 23 static const int kChannels = 2; |
24 static const int kHardwareBufferSize = kSampleRate * kBitsPerSample * | 24 static const int kSamplesPerPacket = kSampleRate / 10; |
25 kChannels / 8; | 25 static const int kHardwareBufferSize = kSamplesPerPacket * kChannels * |
| 26 kBitsPerSample / 8; |
26 static const int kBufferCapacity = 3 * kHardwareBufferSize; | 27 static const int kBufferCapacity = 3 * kHardwareBufferSize; |
27 | 28 |
28 namespace media { | 29 namespace media { |
29 | 30 |
30 class MockAudioOutputControllerEventHandler | 31 class MockAudioOutputControllerEventHandler |
31 : public AudioOutputController::EventHandler { | 32 : public AudioOutputController::EventHandler { |
32 public: | 33 public: |
33 MockAudioOutputControllerEventHandler() {} | 34 MockAudioOutputControllerEventHandler() {} |
34 | 35 |
35 MOCK_METHOD1(OnCreated, void(AudioOutputController* controller)); | 36 MOCK_METHOD1(OnCreated, void(AudioOutputController* controller)); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 if (!HasAudioOutputDevices() || IsRunningHeadless()) | 91 if (!HasAudioOutputDevices() || IsRunningHeadless()) |
91 return; | 92 return; |
92 | 93 |
93 MockAudioOutputControllerEventHandler event_handler; | 94 MockAudioOutputControllerEventHandler event_handler; |
94 | 95 |
95 EXPECT_CALL(event_handler, OnCreated(NotNull())) | 96 EXPECT_CALL(event_handler, OnCreated(NotNull())) |
96 .Times(1); | 97 .Times(1); |
97 EXPECT_CALL(event_handler, OnMoreData(NotNull(), _)); | 98 EXPECT_CALL(event_handler, OnMoreData(NotNull(), _)); |
98 | 99 |
99 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannels, | 100 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannels, |
100 kSampleRate, kBitsPerSample); | 101 kSampleRate, kBitsPerSample, kSamplesPerPacket); |
101 scoped_refptr<AudioOutputController> controller = | 102 scoped_refptr<AudioOutputController> controller = |
102 AudioOutputController::Create(&event_handler, params, | 103 AudioOutputController::Create(&event_handler, params, kBufferCapacity); |
103 kHardwareBufferSize, kBufferCapacity); | |
104 ASSERT_TRUE(controller.get()); | 104 ASSERT_TRUE(controller.get()); |
105 | 105 |
106 // Close the controller immediately. | 106 // Close the controller immediately. |
107 CloseAudioController(controller); | 107 CloseAudioController(controller); |
108 } | 108 } |
109 | 109 |
110 TEST(AudioOutputControllerTest, PlayAndClose) { | 110 TEST(AudioOutputControllerTest, PlayAndClose) { |
111 if (!HasAudioOutputDevices() || IsRunningHeadless()) | 111 if (!HasAudioOutputDevices() || IsRunningHeadless()) |
112 return; | 112 return; |
113 | 113 |
114 MockAudioOutputControllerEventHandler event_handler; | 114 MockAudioOutputControllerEventHandler event_handler; |
115 base::WaitableEvent event(false, false); | 115 base::WaitableEvent event(false, false); |
116 | 116 |
117 // If OnCreated is called then signal the event. | 117 // If OnCreated is called then signal the event. |
118 EXPECT_CALL(event_handler, OnCreated(NotNull())) | 118 EXPECT_CALL(event_handler, OnCreated(NotNull())) |
119 .WillOnce(SignalEvent(&event)); | 119 .WillOnce(SignalEvent(&event)); |
120 | 120 |
121 // OnPlaying() will be called only once. | 121 // OnPlaying() will be called only once. |
122 EXPECT_CALL(event_handler, OnPlaying(NotNull())) | 122 EXPECT_CALL(event_handler, OnPlaying(NotNull())) |
123 .Times(Exactly(1)); | 123 .Times(Exactly(1)); |
124 | 124 |
125 // If OnMoreData is called enough then signal the event. | 125 // If OnMoreData is called enough then signal the event. |
126 EXPECT_CALL(event_handler, OnMoreData(NotNull(), _)) | 126 EXPECT_CALL(event_handler, OnMoreData(NotNull(), _)) |
127 .Times(AtLeast(10)) | 127 .Times(AtLeast(10)) |
128 .WillRepeatedly(SignalEvent(&event)); | 128 .WillRepeatedly(SignalEvent(&event)); |
129 | 129 |
130 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannels, | 130 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannels, |
131 kSampleRate, kBitsPerSample); | 131 kSampleRate, kBitsPerSample, kSamplesPerPacket); |
132 scoped_refptr<AudioOutputController> controller = | 132 scoped_refptr<AudioOutputController> controller = |
133 AudioOutputController::Create(&event_handler, params, | 133 AudioOutputController::Create(&event_handler, params, kBufferCapacity); |
134 kHardwareBufferSize, kBufferCapacity); | |
135 ASSERT_TRUE(controller.get()); | 134 ASSERT_TRUE(controller.get()); |
136 | 135 |
137 // Wait for OnCreated() to be called. | 136 // Wait for OnCreated() to be called. |
138 event.Wait(); | 137 event.Wait(); |
139 | 138 |
140 controller->Play(); | 139 controller->Play(); |
141 | 140 |
142 // Wait until the date is requested at least 10 times. | 141 // Wait until the date is requested at least 10 times. |
143 for (int i = 0; i < 10; i++) { | 142 for (int i = 0; i < 10; i++) { |
144 event.Wait(); | 143 event.Wait(); |
(...skipping 26 matching lines...) Expand all Loading... |
171 EXPECT_CALL(event_handler, OnMoreData(NotNull(), _)) | 170 EXPECT_CALL(event_handler, OnMoreData(NotNull(), _)) |
172 .Times(AtLeast(10)) | 171 .Times(AtLeast(10)) |
173 .WillRepeatedly(SignalEvent(&event)); | 172 .WillRepeatedly(SignalEvent(&event)); |
174 | 173 |
175 // And then OnPaused() will be called. | 174 // And then OnPaused() will be called. |
176 EXPECT_CALL(event_handler, OnPaused(NotNull())) | 175 EXPECT_CALL(event_handler, OnPaused(NotNull())) |
177 .Times(Exactly(1)) | 176 .Times(Exactly(1)) |
178 .WillOnce(InvokeWithoutArgs(&pause_event, &base::WaitableEvent::Signal)); | 177 .WillOnce(InvokeWithoutArgs(&pause_event, &base::WaitableEvent::Signal)); |
179 | 178 |
180 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannels, | 179 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannels, |
181 kSampleRate, kBitsPerSample); | 180 kSampleRate, kBitsPerSample, kSamplesPerPacket); |
182 scoped_refptr<AudioOutputController> controller = | 181 scoped_refptr<AudioOutputController> controller = |
183 AudioOutputController::Create(&event_handler, params, | 182 AudioOutputController::Create(&event_handler, params, kBufferCapacity); |
184 kHardwareBufferSize, kBufferCapacity); | |
185 ASSERT_TRUE(controller.get()); | 183 ASSERT_TRUE(controller.get()); |
186 | 184 |
187 // Wait for OnCreated() to be called. | 185 // Wait for OnCreated() to be called. |
188 event.Wait(); | 186 event.Wait(); |
189 | 187 |
190 controller->Play(); | 188 controller->Play(); |
191 | 189 |
192 // Wait until the date is requested at least 10 times. | 190 // Wait until the date is requested at least 10 times. |
193 for (int i = 0; i < 10; i++) { | 191 for (int i = 0; i < 10; i++) { |
194 event.Wait(); | 192 event.Wait(); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 EXPECT_CALL(event_handler, OnPaused(NotNull())) | 230 EXPECT_CALL(event_handler, OnPaused(NotNull())) |
233 .Times(Exactly(1)) | 231 .Times(Exactly(1)) |
234 .WillOnce(InvokeWithoutArgs(&pause_event, &base::WaitableEvent::Signal)); | 232 .WillOnce(InvokeWithoutArgs(&pause_event, &base::WaitableEvent::Signal)); |
235 | 233 |
236 // OnPlaying() will be called only once. | 234 // OnPlaying() will be called only once. |
237 EXPECT_CALL(event_handler, OnPlaying(NotNull())) | 235 EXPECT_CALL(event_handler, OnPlaying(NotNull())) |
238 .Times(Exactly(1)) | 236 .Times(Exactly(1)) |
239 .RetiresOnSaturation(); | 237 .RetiresOnSaturation(); |
240 | 238 |
241 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannels, | 239 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannels, |
242 kSampleRate, kBitsPerSample); | 240 kSampleRate, kBitsPerSample, kSamplesPerPacket); |
243 scoped_refptr<AudioOutputController> controller = | 241 scoped_refptr<AudioOutputController> controller = |
244 AudioOutputController::Create(&event_handler, params, | 242 AudioOutputController::Create(&event_handler, params, kBufferCapacity); |
245 kHardwareBufferSize, kBufferCapacity); | |
246 ASSERT_TRUE(controller.get()); | 243 ASSERT_TRUE(controller.get()); |
247 | 244 |
248 // Wait for OnCreated() to be called. | 245 // Wait for OnCreated() to be called. |
249 event.Wait(); | 246 event.Wait(); |
250 | 247 |
251 controller->Play(); | 248 controller->Play(); |
252 | 249 |
253 // Wait until the date is requested at least 10 times. | 250 // Wait until the date is requested at least 10 times. |
254 for (int i = 0; i < 10; i++) { | 251 for (int i = 0; i < 10; i++) { |
255 event.Wait(); | 252 event.Wait(); |
(...skipping 20 matching lines...) Expand all Loading... |
276 CloseAudioController(controller); | 273 CloseAudioController(controller); |
277 } | 274 } |
278 | 275 |
279 TEST(AudioOutputControllerTest, HardwareBufferTooLarge) { | 276 TEST(AudioOutputControllerTest, HardwareBufferTooLarge) { |
280 if (!HasAudioOutputDevices() || IsRunningHeadless()) | 277 if (!HasAudioOutputDevices() || IsRunningHeadless()) |
281 return; | 278 return; |
282 | 279 |
283 // Create an audio device with a very large hardware buffer size. | 280 // Create an audio device with a very large hardware buffer size. |
284 MockAudioOutputControllerEventHandler event_handler; | 281 MockAudioOutputControllerEventHandler event_handler; |
285 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannels, | 282 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannels, |
286 kSampleRate, kBitsPerSample); | 283 kSampleRate, kBitsPerSample, |
| 284 kSamplesPerPacket * 1000); |
287 scoped_refptr<AudioOutputController> controller = | 285 scoped_refptr<AudioOutputController> controller = |
288 AudioOutputController::Create(&event_handler, params, | 286 AudioOutputController::Create(&event_handler, params, |
289 kHardwareBufferSize * 1000, | |
290 kBufferCapacity); | 287 kBufferCapacity); |
291 | 288 |
292 // Use assert because we don't stop the device and assume we can't | 289 // Use assert because we don't stop the device and assume we can't |
293 // create one. | 290 // create one. |
294 ASSERT_FALSE(controller); | 291 ASSERT_FALSE(controller); |
295 } | 292 } |
296 | 293 |
297 TEST(AudioOutputControllerTest, CloseTwice) { | 294 TEST(AudioOutputControllerTest, CloseTwice) { |
298 if (!HasAudioOutputDevices() || IsRunningHeadless()) | 295 if (!HasAudioOutputDevices() || IsRunningHeadless()) |
299 return; | 296 return; |
300 | 297 |
301 MockAudioOutputControllerEventHandler event_handler; | 298 MockAudioOutputControllerEventHandler event_handler; |
302 base::WaitableEvent event(false, false); | 299 base::WaitableEvent event(false, false); |
303 | 300 |
304 // If OnCreated is called then signal the event. | 301 // If OnCreated is called then signal the event. |
305 EXPECT_CALL(event_handler, OnCreated(NotNull())) | 302 EXPECT_CALL(event_handler, OnCreated(NotNull())) |
306 .WillOnce(SignalEvent(&event)); | 303 .WillOnce(SignalEvent(&event)); |
307 | 304 |
308 // One OnMoreData() is expected. | 305 // One OnMoreData() is expected. |
309 EXPECT_CALL(event_handler, OnMoreData(NotNull(), _)) | 306 EXPECT_CALL(event_handler, OnMoreData(NotNull(), _)) |
310 .Times(AtLeast(1)) | 307 .Times(AtLeast(1)) |
311 .WillRepeatedly(SignalEvent(&event)); | 308 .WillRepeatedly(SignalEvent(&event)); |
312 | 309 |
313 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannels, | 310 AudioParameters params(AudioParameters::AUDIO_PCM_LINEAR, kChannels, |
314 kSampleRate, kBitsPerSample); | 311 kSampleRate, kBitsPerSample, kSamplesPerPacket); |
315 scoped_refptr<AudioOutputController> controller = | 312 scoped_refptr<AudioOutputController> controller = |
316 AudioOutputController::Create(&event_handler, params, | 313 AudioOutputController::Create(&event_handler, params, kBufferCapacity); |
317 kHardwareBufferSize, kBufferCapacity); | |
318 ASSERT_TRUE(controller.get()); | 314 ASSERT_TRUE(controller.get()); |
319 | 315 |
320 // Wait for OnCreated() to be called. | 316 // Wait for OnCreated() to be called. |
321 event.Wait(); | 317 event.Wait(); |
322 | 318 |
323 // Wait for OnMoreData() to be called. | 319 // Wait for OnMoreData() to be called. |
324 event.Wait(); | 320 event.Wait(); |
325 | 321 |
326 base::WaitableEvent closed_event_1(true, false); | 322 base::WaitableEvent closed_event_1(true, false); |
327 controller->Close(NewRunnableFunction(&SignalClosedEvent, &closed_event_1)); | 323 controller->Close(NewRunnableFunction(&SignalClosedEvent, &closed_event_1)); |
328 | 324 |
329 base::WaitableEvent closed_event_2(true, false); | 325 base::WaitableEvent closed_event_2(true, false); |
330 controller->Close(NewRunnableFunction(&SignalClosedEvent, &closed_event_2)); | 326 controller->Close(NewRunnableFunction(&SignalClosedEvent, &closed_event_2)); |
331 | 327 |
332 closed_event_1.Wait(); | 328 closed_event_1.Wait(); |
333 closed_event_2.Wait(); | 329 closed_event_2.Wait(); |
334 } | 330 } |
335 | 331 |
336 } // namespace media | 332 } // namespace media |
OLD | NEW |