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" | |
6 #include "base/environment.h" | 5 #include "base/environment.h" |
7 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
8 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
9 #include "base/test/test_timeouts.h" | 8 #include "base/test/test_timeouts.h" |
10 #include "base/threading/platform_thread.h" | 9 #include "base/threading/platform_thread.h" |
11 #include "media/audio/audio_io.h" | 10 #include "media/audio/audio_io.h" |
12 #include "media/audio/audio_manager_base.h" | 11 #include "media/audio/audio_manager_base.h" |
13 #include "media/audio/audio_unittest_util.h" | 12 #include "media/audio/audio_unittest_util.h" |
14 #include "media/audio/mac/audio_low_latency_input_mac.h" | 13 #include "media/audio/mac/audio_low_latency_input_mac.h" |
15 #include "media/base/seekable_buffer.h" | 14 #include "media/base/seekable_buffer.h" |
(...skipping 12 matching lines...) Expand all Loading... |
28 if (++*count >= limit) { | 27 if (++*count >= limit) { |
29 loop->PostTask(FROM_HERE, closure); | 28 loop->PostTask(FROM_HERE, closure); |
30 } | 29 } |
31 } | 30 } |
32 | 31 |
33 class MockAudioInputCallback : public AudioInputStream::AudioInputCallback { | 32 class MockAudioInputCallback : public AudioInputStream::AudioInputCallback { |
34 public: | 33 public: |
35 MOCK_METHOD4(OnData, | 34 MOCK_METHOD4(OnData, |
36 void(AudioInputStream* stream, | 35 void(AudioInputStream* stream, |
37 const AudioBus* src, | 36 const AudioBus* src, |
38 uint32 hardware_delay_bytes, | 37 uint32_t hardware_delay_bytes, |
39 double volume)); | 38 double volume)); |
40 MOCK_METHOD1(OnError, void(AudioInputStream* stream)); | 39 MOCK_METHOD1(OnError, void(AudioInputStream* stream)); |
41 }; | 40 }; |
42 | 41 |
43 // This audio sink implementation should be used for manual tests only since | 42 // This audio sink implementation should be used for manual tests only since |
44 // the recorded data is stored on a raw binary data file. | 43 // the recorded data is stored on a raw binary data file. |
45 // The last test (WriteToFileAudioSink) - which is disabled by default - | 44 // The last test (WriteToFileAudioSink) - which is disabled by default - |
46 // can use this audio sink to store the captured data on a file for offline | 45 // can use this audio sink to store the captured data on a file for offline |
47 // analysis. | 46 // analysis. |
48 class WriteToFileAudioSink : public AudioInputStream::AudioInputCallback { | 47 class WriteToFileAudioSink : public AudioInputStream::AudioInputCallback { |
49 public: | 48 public: |
50 // Allocate space for ~10 seconds of data @ 48kHz in stereo: | 49 // Allocate space for ~10 seconds of data @ 48kHz in stereo: |
51 // 2 bytes per sample, 2 channels, 10ms @ 48kHz, 10 seconds <=> 1920000 bytes. | 50 // 2 bytes per sample, 2 channels, 10ms @ 48kHz, 10 seconds <=> 1920000 bytes. |
52 static const int kMaxBufferSize = 2 * 2 * 480 * 100 * 10; | 51 static const int kMaxBufferSize = 2 * 2 * 480 * 100 * 10; |
53 | 52 |
54 explicit WriteToFileAudioSink(const char* file_name) | 53 explicit WriteToFileAudioSink(const char* file_name) |
55 : buffer_(0, kMaxBufferSize), | 54 : buffer_(0, kMaxBufferSize), |
56 file_(fopen(file_name, "wb")), | 55 file_(fopen(file_name, "wb")), |
57 bytes_to_write_(0) { | 56 bytes_to_write_(0) { |
58 } | 57 } |
59 | 58 |
60 ~WriteToFileAudioSink() override { | 59 ~WriteToFileAudioSink() override { |
61 int bytes_written = 0; | 60 int bytes_written = 0; |
62 while (bytes_written < bytes_to_write_) { | 61 while (bytes_written < bytes_to_write_) { |
63 const uint8* chunk; | 62 const uint8_t* chunk; |
64 int chunk_size; | 63 int chunk_size; |
65 | 64 |
66 // Stop writing if no more data is available. | 65 // Stop writing if no more data is available. |
67 if (!buffer_.GetCurrentChunk(&chunk, &chunk_size)) | 66 if (!buffer_.GetCurrentChunk(&chunk, &chunk_size)) |
68 break; | 67 break; |
69 | 68 |
70 // Write recorded data chunk to the file and prepare for next chunk. | 69 // Write recorded data chunk to the file and prepare for next chunk. |
71 fwrite(chunk, 1, chunk_size, file_); | 70 fwrite(chunk, 1, chunk_size, file_); |
72 buffer_.Seek(chunk_size); | 71 buffer_.Seek(chunk_size); |
73 bytes_written += chunk_size; | 72 bytes_written += chunk_size; |
74 } | 73 } |
75 fclose(file_); | 74 fclose(file_); |
76 } | 75 } |
77 | 76 |
78 // AudioInputStream::AudioInputCallback implementation. | 77 // AudioInputStream::AudioInputCallback implementation. |
79 void OnData(AudioInputStream* stream, | 78 void OnData(AudioInputStream* stream, |
80 const AudioBus* src, | 79 const AudioBus* src, |
81 uint32 hardware_delay_bytes, | 80 uint32_t hardware_delay_bytes, |
82 double volume) override { | 81 double volume) override { |
83 const int num_samples = src->frames() * src->channels(); | 82 const int num_samples = src->frames() * src->channels(); |
84 scoped_ptr<int16> interleaved(new int16[num_samples]); | 83 scoped_ptr<int16_t> interleaved(new int16_t[num_samples]); |
85 const int bytes_per_sample = sizeof(*interleaved); | 84 const int bytes_per_sample = sizeof(*interleaved); |
86 src->ToInterleaved(src->frames(), bytes_per_sample, interleaved.get()); | 85 src->ToInterleaved(src->frames(), bytes_per_sample, interleaved.get()); |
87 | 86 |
88 // Store data data in a temporary buffer to avoid making blocking | 87 // Store data data in a temporary buffer to avoid making blocking |
89 // fwrite() calls in the audio callback. The complete buffer will be | 88 // fwrite() calls in the audio callback. The complete buffer will be |
90 // written to file in the destructor. | 89 // written to file in the destructor. |
91 const int size = bytes_per_sample * num_samples; | 90 const int size = bytes_per_sample * num_samples; |
92 if (buffer_.Append((const uint8*)interleaved.get(), size)) { | 91 if (buffer_.Append((const uint8_t*)interleaved.get(), size)) { |
93 bytes_to_write_ += size; | 92 bytes_to_write_ += size; |
94 } | 93 } |
95 } | 94 } |
96 | 95 |
97 void OnError(AudioInputStream* stream) override {} | 96 void OnError(AudioInputStream* stream) override {} |
98 | 97 |
99 private: | 98 private: |
100 media::SeekableBuffer buffer_; | 99 media::SeekableBuffer buffer_; |
101 FILE* file_; | 100 FILE* file_; |
102 int bytes_to_write_; | 101 int bytes_to_write_; |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 WriteToFileAudioSink file_sink(file_name); | 288 WriteToFileAudioSink file_sink(file_name); |
290 fprintf(stderr, " >> Speak into the mic while recording...\n"); | 289 fprintf(stderr, " >> Speak into the mic while recording...\n"); |
291 ais->Start(&file_sink); | 290 ais->Start(&file_sink); |
292 base::PlatformThread::Sleep(TestTimeouts::action_timeout()); | 291 base::PlatformThread::Sleep(TestTimeouts::action_timeout()); |
293 ais->Stop(); | 292 ais->Stop(); |
294 fprintf(stderr, " >> Recording has stopped.\n"); | 293 fprintf(stderr, " >> Recording has stopped.\n"); |
295 ais->Close(); | 294 ais->Close(); |
296 } | 295 } |
297 | 296 |
298 } // namespace media | 297 } // namespace media |
OLD | NEW |