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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/environment.h" | 6 #include "base/environment.h" |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/test/test_timeouts.h" | 8 #include "base/test/test_timeouts.h" |
9 #include "base/threading/platform_thread.h" | 9 #include "base/threading/platform_thread.h" |
10 #include "media/audio/audio_io.h" | 10 #include "media/audio/audio_io.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 using ::testing::NotNull; | 21 using ::testing::NotNull; |
22 | 22 |
23 ACTION_P3(CheckCountAndPostQuitTask, count, limit, loop) { | 23 ACTION_P3(CheckCountAndPostQuitTask, count, limit, loop) { |
24 if (++*count >= limit) { | 24 if (++*count >= limit) { |
25 loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 25 loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
26 } | 26 } |
27 } | 27 } |
28 | 28 |
29 class MockAudioInputCallback : public AudioInputStream::AudioInputCallback { | 29 class MockAudioInputCallback : public AudioInputStream::AudioInputCallback { |
30 public: | 30 public: |
31 MOCK_METHOD4(OnData, void(AudioInputStream* stream, | 31 MOCK_METHOD5(OnData, void(AudioInputStream* stream, |
32 const uint8* src, uint32 size, | 32 const uint8* src, uint32 size, |
33 uint32 hardware_delay_bytes)); | 33 uint32 hardware_delay_bytes, double volume)); |
34 MOCK_METHOD1(OnClose, void(AudioInputStream* stream)); | 34 MOCK_METHOD1(OnClose, void(AudioInputStream* stream)); |
35 MOCK_METHOD2(OnError, void(AudioInputStream* stream, int code)); | 35 MOCK_METHOD2(OnError, void(AudioInputStream* stream, int code)); |
36 }; | 36 }; |
37 | 37 |
38 // This audio sink implementation should be used for manual tests only since | 38 // This audio sink implementation should be used for manual tests only since |
39 // the recorded data is stored on a raw binary data file. | 39 // the recorded data is stored on a raw binary data file. |
40 // The last test (WriteToFileAudioSink) - which is disabled by default - | 40 // The last test (WriteToFileAudioSink) - which is disabled by default - |
41 // can use this audio sink to store the captured data on a file for offline | 41 // can use this audio sink to store the captured data on a file for offline |
42 // analysis. | 42 // analysis. |
43 class WriteToFileAudioSink : public AudioInputStream::AudioInputCallback { | 43 class WriteToFileAudioSink : public AudioInputStream::AudioInputCallback { |
(...skipping 22 matching lines...) Expand all Loading... |
66 fwrite(chunk, 1, chunk_size, file_); | 66 fwrite(chunk, 1, chunk_size, file_); |
67 buffer_.Seek(chunk_size); | 67 buffer_.Seek(chunk_size); |
68 bytes_written += chunk_size; | 68 bytes_written += chunk_size; |
69 } | 69 } |
70 fclose(file_); | 70 fclose(file_); |
71 } | 71 } |
72 | 72 |
73 // AudioInputStream::AudioInputCallback implementation. | 73 // AudioInputStream::AudioInputCallback implementation. |
74 virtual void OnData(AudioInputStream* stream, | 74 virtual void OnData(AudioInputStream* stream, |
75 const uint8* src, uint32 size, | 75 const uint8* src, uint32 size, |
76 uint32 hardware_delay_bytes) { | 76 uint32 hardware_delay_bytes, double volume) { |
77 // Store data data in a temporary buffer to avoid making blocking | 77 // Store data data in a temporary buffer to avoid making blocking |
78 // fwrite() calls in the audio callback. The complete buffer will be | 78 // fwrite() calls in the audio callback. The complete buffer will be |
79 // written to file in the destructor. | 79 // written to file in the destructor. |
80 if (buffer_.Append(src, size)) { | 80 if (buffer_.Append(src, size)) { |
81 bytes_to_write_ += size; | 81 bytes_to_write_ += size; |
82 } | 82 } |
83 } | 83 } |
84 | 84 |
85 virtual void OnClose(AudioInputStream* stream) {} | 85 virtual void OnClose(AudioInputStream* stream) {} |
86 virtual void OnError(AudioInputStream* stream, int code) {} | 86 virtual void OnError(AudioInputStream* stream, int code) {} |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 int samples_per_packet = fs / 100; | 225 int samples_per_packet = fs / 100; |
226 int bits_per_sample = 16; | 226 int bits_per_sample = 16; |
227 uint32 bytes_per_packet = samples_per_packet * (bits_per_sample / 8); | 227 uint32 bytes_per_packet = samples_per_packet * (bits_per_sample / 8); |
228 | 228 |
229 MockAudioInputCallback sink; | 229 MockAudioInputCallback sink; |
230 | 230 |
231 // We use 10ms packets and will run the test until ten packets are received. | 231 // We use 10ms packets and will run the test until ten packets are received. |
232 // All should contain valid packets of the same size and a valid delay | 232 // All should contain valid packets of the same size and a valid delay |
233 // estimate. | 233 // estimate. |
234 EXPECT_CALL(sink, OnData(ais, NotNull(), bytes_per_packet, | 234 EXPECT_CALL(sink, OnData(ais, NotNull(), bytes_per_packet, |
235 Ge(bytes_per_packet))) | 235 Ge(bytes_per_packet), _)) |
236 .Times(AtLeast(10)) | 236 .Times(AtLeast(10)) |
237 .WillRepeatedly(CheckCountAndPostQuitTask(&count, 10, &loop)); | 237 .WillRepeatedly(CheckCountAndPostQuitTask(&count, 10, &loop)); |
238 ais->Start(&sink); | 238 ais->Start(&sink); |
239 loop.Run(); | 239 loop.Run(); |
240 ais->Stop(); | 240 ais->Stop(); |
241 | 241 |
242 // Verify that the sink receieves OnClose() call when calling Close(). | 242 // Verify that the sink receieves OnClose() call when calling Close(). |
243 EXPECT_CALL(sink, OnClose(ais)) | 243 EXPECT_CALL(sink, OnClose(ais)) |
244 .Times(1); | 244 .Times(1); |
245 ais->Close(); | 245 ais->Close(); |
(...skipping 15 matching lines...) Expand all Loading... |
261 int samples_per_packet = fs / 100; | 261 int samples_per_packet = fs / 100; |
262 int bits_per_sample = 16; | 262 int bits_per_sample = 16; |
263 uint32 bytes_per_packet = 2 * samples_per_packet * (bits_per_sample / 8); | 263 uint32 bytes_per_packet = 2 * samples_per_packet * (bits_per_sample / 8); |
264 | 264 |
265 MockAudioInputCallback sink; | 265 MockAudioInputCallback sink; |
266 | 266 |
267 // We use 10ms packets and will run the test until ten packets are received. | 267 // We use 10ms packets and will run the test until ten packets are received. |
268 // All should contain valid packets of the same size and a valid delay | 268 // All should contain valid packets of the same size and a valid delay |
269 // estimate. | 269 // estimate. |
270 EXPECT_CALL(sink, OnData(ais, NotNull(), bytes_per_packet, | 270 EXPECT_CALL(sink, OnData(ais, NotNull(), bytes_per_packet, |
271 Ge(bytes_per_packet))) | 271 Ge(bytes_per_packet), _)) |
272 .Times(AtLeast(10)) | 272 .Times(AtLeast(10)) |
273 .WillRepeatedly(CheckCountAndPostQuitTask(&count, 10, &loop)); | 273 .WillRepeatedly(CheckCountAndPostQuitTask(&count, 10, &loop)); |
274 ais->Start(&sink); | 274 ais->Start(&sink); |
275 loop.Run(); | 275 loop.Run(); |
276 ais->Stop(); | 276 ais->Stop(); |
277 | 277 |
278 // Verify that the sink receieves OnClose() call when calling Close(). | 278 // Verify that the sink receieves OnClose() call when calling Close(). |
279 EXPECT_CALL(sink, OnClose(ais)) | 279 EXPECT_CALL(sink, OnClose(ais)) |
280 .Times(1); | 280 .Times(1); |
281 ais->Close(); | 281 ais->Close(); |
(...skipping 18 matching lines...) Expand all Loading... |
300 fprintf(stderr, " Sample rate: %d\n", fs); | 300 fprintf(stderr, " Sample rate: %d\n", fs); |
301 WriteToFileAudioSink file_sink(file_name); | 301 WriteToFileAudioSink file_sink(file_name); |
302 fprintf(stderr, " >> Speak into the mic while recording...\n"); | 302 fprintf(stderr, " >> Speak into the mic while recording...\n"); |
303 ais->Start(&file_sink); | 303 ais->Start(&file_sink); |
304 base::PlatformThread::Sleep(TestTimeouts::action_timeout()); | 304 base::PlatformThread::Sleep(TestTimeouts::action_timeout()); |
305 ais->Stop(); | 305 ais->Stop(); |
306 fprintf(stderr, " >> Recording has stopped.\n"); | 306 fprintf(stderr, " >> Recording has stopped.\n"); |
307 ais->Close(); | 307 ais->Close(); |
308 } | 308 } |
309 | 309 |
OLD | NEW |