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