1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/copresence/mediums/audio/audio_recorder.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/logging.h"
13 #include "base/run_loop.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "components/copresence/public/copresence_constants.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "media/audio/audio_manager.h"
18 #include "media/audio/audio_manager_base.h"
19 #include "media/base/audio_bus.h"
21 namespace copresence {
25 const float kProcessIntervalMs = 500.0f; // milliseconds.
27 void AudioBusToString(scoped_ptr<media::AudioBus> source, std::string* buffer) {
28 buffer->resize(source->frames() * source->channels() * sizeof(float));
29 float* buffer_view = reinterpret_cast<float*>(string_as_array(buffer));
31 const int channels = source->channels();
32 for (int ch = 0; ch < channels; ++ch) {
33 for (int si = 0, di = ch; si < source->frames(); ++si, di += channels)
34 buffer_view[di] = source->channel(ch)[si];
38 // Called every kProcessIntervalMs to process the recorded audio. This
39 // converts our samples to the required sample rate, interleaves the samples
40 // and sends them to the whispernet decoder to process.
41 void ProcessSamples(scoped_ptr<media::AudioBus> bus,
42 const AudioRecorder::DecodeSamplesCallback& callback) {
44 AudioBusToString(bus.Pass(), &samples);
45 content::BrowserThread::PostTask(
46 content::BrowserThread::UI, FROM_HERE, base::Bind(callback, samples));
53 AudioRecorder::AudioRecorder(const DecodeSamplesCallback& decode_callback)
54 : is_recording_(false),
56 decode_callback_(decode_callback),
57 total_buffer_frames_(0),
58 buffer_frame_index_(0) {
61 void AudioRecorder::Initialize() {
62 media::AudioManager::Get()->GetTaskRunner()->PostTask(
64 base::Bind(&AudioRecorder::InitializeOnAudioThread,
65 base::Unretained(this)));
68 AudioRecorder::~AudioRecorder() {
71 void AudioRecorder::Record() {
72 media::AudioManager::Get()->GetTaskRunner()->PostTask(
74 base::Bind(&AudioRecorder::RecordOnAudioThread, base::Unretained(this)));
77 void AudioRecorder::Stop() {
78 media::AudioManager::Get()->GetTaskRunner()->PostTask(
80 base::Bind(&AudioRecorder::StopOnAudioThread, base::Unretained(this)));
83 bool AudioRecorder::IsRecording() {
87 void AudioRecorder::Finalize() {
88 media::AudioManager::Get()->GetTaskRunner()->PostTask(
90 base::Bind(&AudioRecorder::FinalizeOnAudioThread,
91 base::Unretained(this)));
96 void AudioRecorder::InitializeOnAudioThread() {
97 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
99 media::AudioParameters params =
101 ? *params_for_testing_
102 : media::AudioManager::Get()->GetInputStreamParameters(
103 media::AudioManagerBase::kDefaultDeviceId);
105 const media::AudioParameters dest_params(params.format(),
106 kDefaultChannelLayout,
108 kDefaultBitsPerSample,
109 params.frames_per_buffer(),
110 media::AudioParameters::NO_EFFECTS);
112 converter_.reset(new media::AudioConverter(
113 params, dest_params, params.sample_rate() == dest_params.sample_rate()));
114 converter_->AddInput(this);
116 total_buffer_frames_ = kProcessIntervalMs * dest_params.sample_rate() / 1000;
118 media::AudioBus::Create(dest_params.channels(), total_buffer_frames_);
119 buffer_frame_index_ = 0;
121 stream_ = input_stream_for_testing_
122 ? input_stream_for_testing_.get()
123 : media::AudioManager::Get()->MakeAudioInputStream(
124 params, media::AudioManagerBase::kDefaultDeviceId);
126 if (!stream_ || !stream_->Open()) {
127 LOG(ERROR) << "Failed to open an input stream.";
134 stream_->SetVolume(stream_->GetMaxVolume());
137 void AudioRecorder::RecordOnAudioThread() {
138 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
139 if (!stream_ || is_recording_)
143 stream_->Start(this);
144 is_recording_ = true;
147 void AudioRecorder::StopOnAudioThread() {
148 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
149 if (!stream_ || !is_recording_)
153 is_recording_ = false;
156 void AudioRecorder::StopAndCloseOnAudioThread() {
157 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
166 void AudioRecorder::FinalizeOnAudioThread() {
167 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
168 StopAndCloseOnAudioThread();
172 void AudioRecorder::OnData(media::AudioInputStream* stream,
173 const media::AudioBus* source,
174 uint32 /* hardware_delay_bytes */,
175 double /* volume */) {
176 temp_conversion_buffer_ = source;
177 while (temp_conversion_buffer_) {
178 // source->frames() == source_params.frames_per_buffer(), so we only have
179 // one chunk of data in the source; correspondingly set the destination
180 // size to one chunk.
181 // TODO(rkc): Optimize this to directly write into buffer_ so we can avoid
182 // the copy into this buffer and then the copy back into buffer_.
183 scoped_ptr<media::AudioBus> converted_source =
184 media::AudioBus::Create(kDefaultChannels, converter_->ChunkSize());
186 // Convert accumulated samples into converted_source. Note: One call may not
187 // be enough to consume the samples from |source|. The converter may have
188 // accumulated samples over time due to a fractional input:output sample
189 // rate ratio. Since |source| is ephemeral, Convert() must be called until
190 // |source| is at least buffered into the converter. Once |source| is
191 // consumed during ProvideInput(), |temp_conversion_buffer_| will be set to
192 // NULL, which will break the conversion loop.
193 converter_->Convert(converted_source.get());
195 int remaining_buffer_frames = buffer_->frames() - buffer_frame_index_;
197 std::min(remaining_buffer_frames, converted_source->frames());
198 converted_source->CopyPartialFramesTo(
199 0, frames_to_copy, buffer_frame_index_, buffer_.get());
200 buffer_frame_index_ += frames_to_copy;
202 // Buffer full, send it for processing.
203 if (buffer_->frames() == buffer_frame_index_) {
204 ProcessSamples(buffer_.Pass(), decode_callback_);
205 buffer_ = media::AudioBus::Create(kDefaultChannels, total_buffer_frames_);
206 buffer_frame_index_ = 0;
208 // Copy any remaining frames in the source to our buffer.
209 int remaining_source_frames = converted_source->frames() - frames_to_copy;
210 converted_source->CopyPartialFramesTo(frames_to_copy,
211 remaining_source_frames,
214 buffer_frame_index_ += remaining_source_frames;
219 void AudioRecorder::OnError(media::AudioInputStream* /* stream */) {
220 LOG(ERROR) << "Error during sound recording.";
221 media::AudioManager::Get()->GetTaskRunner()->PostTask(
223 base::Bind(&AudioRecorder::StopAndCloseOnAudioThread,
224 base::Unretained(this)));
227 double AudioRecorder::ProvideInput(media::AudioBus* dest,
228 base::TimeDelta /* buffer_delay */) {
229 DCHECK(temp_conversion_buffer_);
230 DCHECK_LE(temp_conversion_buffer_->frames(), dest->frames());
231 temp_conversion_buffer_->CopyTo(dest);
232 temp_conversion_buffer_ = NULL;
236 void AudioRecorder::FlushAudioLoopForTesting() {
237 if (media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread())
240 // Queue task on the audio thread, when it is executed, that means we've
241 // successfully executed all the tasks before us.
243 media::AudioManager::Get()->GetTaskRunner()->PostTaskAndReply(
245 base::Bind(base::IgnoreResult(&AudioRecorder::FlushAudioLoopForTesting),
246 base::Unretained(this)),
251 } // namespace copresence