audio_recorder.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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. 4 5#include "components/copresence/mediums/audio/audio_recorder.h" 6 7#include <algorithm> 8#include <vector> 9 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" 20 21namespace copresence { 22 23namespace { 24 25const float kProcessIntervalMs = 500.0f; // milliseconds. 26 27void 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)); 30 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]; 35 } 36} 37 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. 41void ProcessSamples(scoped_ptr<media::AudioBus> bus, 42 const AudioRecorder::DecodeSamplesCallback& callback) { 43 std::string samples; 44 AudioBusToString(bus.Pass(), &samples); 45 content::BrowserThread::PostTask( 46 content::BrowserThread::UI, FROM_HERE, base::Bind(callback, samples)); 47} 48 49} // namespace 50 51// Public methods. 52 53AudioRecorder::AudioRecorder(const DecodeSamplesCallback& decode_callback) 54 : stream_(NULL), 55 is_recording_(false), 56 decode_callback_(decode_callback), 57 total_buffer_frames_(0), 58 buffer_frame_index_(0) { 59} 60 61void AudioRecorder::Initialize() { 62 media::AudioManager::Get()->GetTaskRunner()->PostTask( 63 FROM_HERE, 64 base::Bind(&AudioRecorder::InitializeOnAudioThread, 65 base::Unretained(this))); 66} 67 68AudioRecorder::~AudioRecorder() { 69} 70 71void AudioRecorder::Record() { 72 media::AudioManager::Get()->GetTaskRunner()->PostTask( 73 FROM_HERE, 74 base::Bind(&AudioRecorder::RecordOnAudioThread, base::Unretained(this))); 75} 76 77void AudioRecorder::Stop() { 78 media::AudioManager::Get()->GetTaskRunner()->PostTask( 79 FROM_HERE, 80 base::Bind(&AudioRecorder::StopOnAudioThread, base::Unretained(this))); 81} 82 83void AudioRecorder::Finalize() { 84 media::AudioManager::Get()->GetTaskRunner()->PostTask( 85 FROM_HERE, 86 base::Bind(&AudioRecorder::FinalizeOnAudioThread, 87 base::Unretained(this))); 88} 89 90// Private methods. 91 92void AudioRecorder::InitializeOnAudioThread() { 93 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()); 94 95 media::AudioParameters params = 96 params_for_testing_ 97 ? *params_for_testing_ 98 : media::AudioManager::Get()->GetInputStreamParameters( 99 media::AudioManagerBase::kDefaultDeviceId); 100 101 const media::AudioParameters dest_params(params.format(), 102 kDefaultChannelLayout, 103 kDefaultChannels, 104 params.input_channels(), 105 kDefaultSampleRate, 106 kDefaultBitsPerSample, 107 params.frames_per_buffer(), 108 media::AudioParameters::NO_EFFECTS); 109 110 converter_.reset(new media::AudioConverter( 111 params, dest_params, params.sample_rate() == dest_params.sample_rate())); 112 converter_->AddInput(this); 113 114 total_buffer_frames_ = kProcessIntervalMs * dest_params.sample_rate() / 1000; 115 buffer_ = 116 media::AudioBus::Create(dest_params.channels(), total_buffer_frames_); 117 buffer_frame_index_ = 0; 118 119 stream_ = input_stream_for_testing_ 120 ? input_stream_for_testing_.get() 121 : media::AudioManager::Get()->MakeAudioInputStream( 122 params, media::AudioManagerBase::kDefaultDeviceId); 123 124 if (!stream_ || !stream_->Open()) { 125 LOG(ERROR) << "Failed to open an input stream."; 126 if (stream_) { 127 stream_->Close(); 128 stream_ = NULL; 129 } 130 return; 131 } 132 stream_->SetVolume(stream_->GetMaxVolume()); 133} 134 135void AudioRecorder::RecordOnAudioThread() { 136 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()); 137 if (!stream_ || is_recording_) 138 return; 139 140 DVLOG(2) << "Recording Audio."; 141 converter_->Reset(); 142 stream_->Start(this); 143 is_recording_ = true; 144} 145 146void AudioRecorder::StopOnAudioThread() { 147 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()); 148 if (!stream_ || !is_recording_) 149 return; 150 151 stream_->Stop(); 152 is_recording_ = false; 153} 154 155void AudioRecorder::StopAndCloseOnAudioThread() { 156 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()); 157 if (!stream_) 158 return; 159 160 StopOnAudioThread(); 161 stream_->Close(); 162 stream_ = NULL; 163} 164 165void AudioRecorder::FinalizeOnAudioThread() { 166 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()); 167 StopAndCloseOnAudioThread(); 168 delete this; 169} 170 171void AudioRecorder::OnData(media::AudioInputStream* stream, 172 const media::AudioBus* source, 173 uint32 /* hardware_delay_bytes */, 174 double /* volume */) { 175 temp_conversion_buffer_ = source; 176 while (temp_conversion_buffer_) { 177 // source->frames() == source_params.frames_per_buffer(), so we only have 178 // one chunk of data in the source; correspondingly set the destination 179 // size to one chunk. 180 // TODO(rkc): Optimize this to directly write into buffer_ so we can avoid 181 // the copy into this buffer and then the copy back into buffer_. 182 scoped_ptr<media::AudioBus> converted_source = 183 media::AudioBus::Create(kDefaultChannels, converter_->ChunkSize()); 184 185 // Convert accumulated samples into converted_source. Note: One call may not 186 // be enough to consume the samples from |source|. The converter may have 187 // accumulated samples over time due to a fractional input:output sample 188 // rate ratio. Since |source| is ephemeral, Convert() must be called until 189 // |source| is at least buffered into the converter. Once |source| is 190 // consumed during ProvideInput(), |temp_conversion_buffer_| will be set to 191 // NULL, which will break the conversion loop. 192 converter_->Convert(converted_source.get()); 193 194 int remaining_buffer_frames = buffer_->frames() - buffer_frame_index_; 195 int frames_to_copy = 196 std::min(remaining_buffer_frames, converted_source->frames()); 197 converted_source->CopyPartialFramesTo( 198 0, frames_to_copy, buffer_frame_index_, buffer_.get()); 199 buffer_frame_index_ += frames_to_copy; 200 201 // Buffer full, send it for processing. 202 if (buffer_->frames() == buffer_frame_index_) { 203 ProcessSamples(buffer_.Pass(), decode_callback_); 204 buffer_ = media::AudioBus::Create(kDefaultChannels, total_buffer_frames_); 205 buffer_frame_index_ = 0; 206 207 // Copy any remaining frames in the source to our buffer. 208 int remaining_source_frames = converted_source->frames() - frames_to_copy; 209 converted_source->CopyPartialFramesTo(frames_to_copy, 210 remaining_source_frames, 211 buffer_frame_index_, 212 buffer_.get()); 213 buffer_frame_index_ += remaining_source_frames; 214 } 215 } 216} 217 218void AudioRecorder::OnError(media::AudioInputStream* /* stream */) { 219 LOG(ERROR) << "Error during sound recording."; 220 media::AudioManager::Get()->GetTaskRunner()->PostTask( 221 FROM_HERE, 222 base::Bind(&AudioRecorder::StopAndCloseOnAudioThread, 223 base::Unretained(this))); 224} 225 226double AudioRecorder::ProvideInput(media::AudioBus* dest, 227 base::TimeDelta /* buffer_delay */) { 228 DCHECK(temp_conversion_buffer_); 229 DCHECK_LE(temp_conversion_buffer_->frames(), dest->frames()); 230 temp_conversion_buffer_->CopyTo(dest); 231 temp_conversion_buffer_ = NULL; 232 return 1.0; 233} 234 235void AudioRecorder::FlushAudioLoopForTesting() { 236 if (media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread()) 237 return; 238 239 // Queue task on the audio thread, when it is executed, that means we've 240 // successfully executed all the tasks before us. 241 base::RunLoop rl; 242 media::AudioManager::Get()->GetTaskRunner()->PostTaskAndReply( 243 FROM_HERE, 244 base::Bind(base::IgnoreResult(&AudioRecorder::FlushAudioLoopForTesting), 245 base::Unretained(this)), 246 rl.QuitClosure()); 247 rl.Run(); 248} 249 250} // namespace copresence 251