| 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 <windows.h> | 5 #include <windows.h> |
| 6 #include <mmsystem.h> | 6 #include <mmsystem.h> |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/environment.h" | 9 #include "base/environment.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 bytes_to_write_(0) { | 97 bytes_to_write_(0) { |
| 98 base::FilePath file_path; | 98 base::FilePath file_path; |
| 99 EXPECT_TRUE(PathService::Get(base::DIR_EXE, &file_path)); | 99 EXPECT_TRUE(PathService::Get(base::DIR_EXE, &file_path)); |
| 100 file_path = file_path.AppendASCII(file_name); | 100 file_path = file_path.AppendASCII(file_name); |
| 101 binary_file_ = base::OpenFile(file_path, "wb"); | 101 binary_file_ = base::OpenFile(file_path, "wb"); |
| 102 DLOG_IF(ERROR, !binary_file_) << "Failed to open binary PCM data file."; | 102 DLOG_IF(ERROR, !binary_file_) << "Failed to open binary PCM data file."; |
| 103 VLOG(0) << ">> Output file: " << file_path.value() << " has been created."; | 103 VLOG(0) << ">> Output file: " << file_path.value() << " has been created."; |
| 104 VLOG(0) << "bits_per_sample_:" << bits_per_sample_; | 104 VLOG(0) << "bits_per_sample_:" << bits_per_sample_; |
| 105 } | 105 } |
| 106 | 106 |
| 107 virtual ~WriteToFileAudioSink() { | 107 ~WriteToFileAudioSink() override { |
| 108 size_t bytes_written = 0; | 108 size_t bytes_written = 0; |
| 109 while (bytes_written < bytes_to_write_) { | 109 while (bytes_written < bytes_to_write_) { |
| 110 const uint8* chunk; | 110 const uint8* chunk; |
| 111 int chunk_size; | 111 int chunk_size; |
| 112 | 112 |
| 113 // Stop writing if no more data is available. | 113 // Stop writing if no more data is available. |
| 114 if (!buffer_.GetCurrentChunk(&chunk, &chunk_size)) | 114 if (!buffer_.GetCurrentChunk(&chunk, &chunk_size)) |
| 115 break; | 115 break; |
| 116 | 116 |
| 117 // Write recorded data chunk to the file and prepare for next chunk. | 117 // Write recorded data chunk to the file and prepare for next chunk. |
| 118 fwrite(chunk, 1, chunk_size, binary_file_); | 118 fwrite(chunk, 1, chunk_size, binary_file_); |
| 119 buffer_.Seek(chunk_size); | 119 buffer_.Seek(chunk_size); |
| 120 bytes_written += chunk_size; | 120 bytes_written += chunk_size; |
| 121 } | 121 } |
| 122 base::CloseFile(binary_file_); | 122 base::CloseFile(binary_file_); |
| 123 } | 123 } |
| 124 | 124 |
| 125 // AudioInputStream::AudioInputCallback implementation. | 125 // AudioInputStream::AudioInputCallback implementation. |
| 126 virtual void OnData(AudioInputStream* stream, | 126 void OnData(AudioInputStream* stream, |
| 127 const AudioBus* src, | 127 const AudioBus* src, |
| 128 uint32 hardware_delay_bytes, | 128 uint32 hardware_delay_bytes, |
| 129 double volume) { | 129 double volume) override { |
| 130 EXPECT_EQ(bits_per_sample_, 16); | 130 EXPECT_EQ(bits_per_sample_, 16); |
| 131 const int num_samples = src->frames() * src->channels(); | 131 const int num_samples = src->frames() * src->channels(); |
| 132 scoped_ptr<int16> interleaved(new int16[num_samples]); | 132 scoped_ptr<int16> interleaved(new int16[num_samples]); |
| 133 const int bytes_per_sample = sizeof(*interleaved); | 133 const int bytes_per_sample = sizeof(*interleaved); |
| 134 src->ToInterleaved(src->frames(), bytes_per_sample, interleaved.get()); | 134 src->ToInterleaved(src->frames(), bytes_per_sample, interleaved.get()); |
| 135 | 135 |
| 136 // Store data data in a temporary buffer to avoid making blocking | 136 // Store data data in a temporary buffer to avoid making blocking |
| 137 // fwrite() calls in the audio callback. The complete buffer will be | 137 // fwrite() calls in the audio callback. The complete buffer will be |
| 138 // written to file in the destructor. | 138 // written to file in the destructor. |
| 139 const int size = bytes_per_sample * num_samples; | 139 const int size = bytes_per_sample * num_samples; |
| 140 if (buffer_.Append((const uint8*)interleaved.get(), size)) { | 140 if (buffer_.Append((const uint8*)interleaved.get(), size)) { |
| 141 bytes_to_write_ += size; | 141 bytes_to_write_ += size; |
| 142 } | 142 } |
| 143 } | 143 } |
| 144 | 144 |
| 145 virtual void OnError(AudioInputStream* stream) {} | 145 void OnError(AudioInputStream* stream) override {} |
| 146 | 146 |
| 147 private: | 147 private: |
| 148 int bits_per_sample_; | 148 int bits_per_sample_; |
| 149 media::SeekableBuffer buffer_; | 149 media::SeekableBuffer buffer_; |
| 150 FILE* binary_file_; | 150 FILE* binary_file_; |
| 151 size_t bytes_to_write_; | 151 size_t bytes_to_write_; |
| 152 }; | 152 }; |
| 153 | 153 |
| 154 static bool HasCoreAudioAndInputDevices(AudioManager* audio_man) { | 154 static bool HasCoreAudioAndInputDevices(AudioManager* audio_man) { |
| 155 // The low-latency (WASAPI-based) version requires Windows Vista or higher. | 155 // The low-latency (WASAPI-based) version requires Windows Vista or higher. |
| (...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 WriteToFileAudioSink file_sink(file_name, aisw.bits_per_sample()); | 470 WriteToFileAudioSink file_sink(file_name, aisw.bits_per_sample()); |
| 471 VLOG(0) << ">> Speak into the default microphone while recording."; | 471 VLOG(0) << ">> Speak into the default microphone while recording."; |
| 472 ais->Start(&file_sink); | 472 ais->Start(&file_sink); |
| 473 base::PlatformThread::Sleep(TestTimeouts::action_timeout()); | 473 base::PlatformThread::Sleep(TestTimeouts::action_timeout()); |
| 474 ais->Stop(); | 474 ais->Stop(); |
| 475 VLOG(0) << ">> Recording has stopped."; | 475 VLOG(0) << ">> Recording has stopped."; |
| 476 ais.Close(); | 476 ais.Close(); |
| 477 } | 477 } |
| 478 | 478 |
| 479 } // namespace media | 479 } // namespace media |
| OLD | NEW |