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/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/test/test_timeouts.h" | 9 #include "base/test/test_timeouts.h" |
10 #include "base/threading/platform_thread.h" | 10 #include "base/threading/platform_thread.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 // Allocate space for ~10 seconds of data @ 48kHz in stereo: | 49 // Allocate space for ~10 seconds of data @ 48kHz in stereo: |
50 // 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. |
51 static const int kMaxBufferSize = 2 * 2 * 480 * 100 * 10; | 51 static const int kMaxBufferSize = 2 * 2 * 480 * 100 * 10; |
52 | 52 |
53 explicit WriteToFileAudioSink(const char* file_name) | 53 explicit WriteToFileAudioSink(const char* file_name) |
54 : buffer_(0, kMaxBufferSize), | 54 : buffer_(0, kMaxBufferSize), |
55 file_(fopen(file_name, "wb")), | 55 file_(fopen(file_name, "wb")), |
56 bytes_to_write_(0) { | 56 bytes_to_write_(0) { |
57 } | 57 } |
58 | 58 |
59 virtual ~WriteToFileAudioSink() { | 59 ~WriteToFileAudioSink() override { |
60 int bytes_written = 0; | 60 int bytes_written = 0; |
61 while (bytes_written < bytes_to_write_) { | 61 while (bytes_written < bytes_to_write_) { |
62 const uint8* chunk; | 62 const uint8* chunk; |
63 int chunk_size; | 63 int chunk_size; |
64 | 64 |
65 // Stop writing if no more data is available. | 65 // Stop writing if no more data is available. |
66 if (!buffer_.GetCurrentChunk(&chunk, &chunk_size)) | 66 if (!buffer_.GetCurrentChunk(&chunk, &chunk_size)) |
67 break; | 67 break; |
68 | 68 |
69 // 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. |
70 fwrite(chunk, 1, chunk_size, file_); | 70 fwrite(chunk, 1, chunk_size, file_); |
71 buffer_.Seek(chunk_size); | 71 buffer_.Seek(chunk_size); |
72 bytes_written += chunk_size; | 72 bytes_written += chunk_size; |
73 } | 73 } |
74 fclose(file_); | 74 fclose(file_); |
75 } | 75 } |
76 | 76 |
77 // AudioInputStream::AudioInputCallback implementation. | 77 // AudioInputStream::AudioInputCallback implementation. |
78 virtual void OnData(AudioInputStream* stream, | 78 void OnData(AudioInputStream* stream, |
79 const AudioBus* src, | 79 const AudioBus* src, |
80 uint32 hardware_delay_bytes, | 80 uint32 hardware_delay_bytes, |
81 double volume) override { | 81 double volume) override { |
82 const int num_samples = src->frames() * src->channels(); | 82 const int num_samples = src->frames() * src->channels(); |
83 scoped_ptr<int16> interleaved(new int16[num_samples]); | 83 scoped_ptr<int16> interleaved(new int16[num_samples]); |
84 const int bytes_per_sample = sizeof(*interleaved); | 84 const int bytes_per_sample = sizeof(*interleaved); |
85 src->ToInterleaved(src->frames(), bytes_per_sample, interleaved.get()); | 85 src->ToInterleaved(src->frames(), bytes_per_sample, interleaved.get()); |
86 | 86 |
87 // Store data data in a temporary buffer to avoid making blocking | 87 // Store data data in a temporary buffer to avoid making blocking |
88 // fwrite() calls in the audio callback. The complete buffer will be | 88 // fwrite() calls in the audio callback. The complete buffer will be |
89 // written to file in the destructor. | 89 // written to file in the destructor. |
90 const int size = bytes_per_sample * num_samples; | 90 const int size = bytes_per_sample * num_samples; |
91 if (buffer_.Append((const uint8*)interleaved.get(), size)) { | 91 if (buffer_.Append((const uint8*)interleaved.get(), size)) { |
92 bytes_to_write_ += size; | 92 bytes_to_write_ += size; |
93 } | 93 } |
94 } | 94 } |
95 | 95 |
96 virtual void OnError(AudioInputStream* stream) override {} | 96 void OnError(AudioInputStream* stream) override {} |
97 | 97 |
98 private: | 98 private: |
99 media::SeekableBuffer buffer_; | 99 media::SeekableBuffer buffer_; |
100 FILE* file_; | 100 FILE* file_; |
101 int bytes_to_write_; | 101 int bytes_to_write_; |
102 }; | 102 }; |
103 | 103 |
104 class MacAudioInputTest : public testing::Test { | 104 class MacAudioInputTest : public testing::Test { |
105 protected: | 105 protected: |
106 MacAudioInputTest() | 106 MacAudioInputTest() |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 WriteToFileAudioSink file_sink(file_name); | 303 WriteToFileAudioSink file_sink(file_name); |
304 fprintf(stderr, " >> Speak into the mic while recording...\n"); | 304 fprintf(stderr, " >> Speak into the mic while recording...\n"); |
305 ais->Start(&file_sink); | 305 ais->Start(&file_sink); |
306 base::PlatformThread::Sleep(TestTimeouts::action_timeout()); | 306 base::PlatformThread::Sleep(TestTimeouts::action_timeout()); |
307 ais->Stop(); | 307 ais->Stop(); |
308 fprintf(stderr, " >> Recording has stopped.\n"); | 308 fprintf(stderr, " >> Recording has stopped.\n"); |
309 ais->Close(); | 309 ais->Close(); |
310 } | 310 } |
311 | 311 |
312 } // namespace media | 312 } // namespace media |
OLD | NEW |