Roll src/third_party/WebKit a2aeeb7:6373c4f (svn 198318:198336)
[chromium-blink-merge.git] / components / audio_modem / audio_recorder_impl.cc
blobe2826887b871368bd61623f7ffe8b621d63b51e8
1 // Copyright 2015 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/audio_modem/audio_recorder_impl.h"
7 #include <algorithm>
8 #include <vector>
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/audio_modem/public/audio_modem_types.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 audio_modem {
23 namespace {
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(
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));
50 } // namespace
52 // Public methods.
54 AudioRecorderImpl::AudioRecorderImpl()
55 : is_recording_(false),
56 stream_(nullptr),
57 total_buffer_frames_(0),
58 buffer_frame_index_(0) {
61 void AudioRecorderImpl::Initialize(
62 const RecordedSamplesCallback& decode_callback) {
63 decode_callback_ = decode_callback;
64 media::AudioManager::Get()->GetTaskRunner()->PostTask(
65 FROM_HERE,
66 base::Bind(&AudioRecorderImpl::InitializeOnAudioThread,
67 base::Unretained(this)));
70 AudioRecorderImpl::~AudioRecorderImpl() {
73 void AudioRecorderImpl::Record() {
74 media::AudioManager::Get()->GetTaskRunner()->PostTask(
75 FROM_HERE,
76 base::Bind(&AudioRecorderImpl::RecordOnAudioThread,
77 base::Unretained(this)));
80 void AudioRecorderImpl::Stop() {
81 media::AudioManager::Get()->GetTaskRunner()->PostTask(
82 FROM_HERE,
83 base::Bind(&AudioRecorderImpl::StopOnAudioThread,
84 base::Unretained(this)));
87 void AudioRecorderImpl::Finalize() {
88 media::AudioManager::Get()->GetTaskRunner()->PostTask(
89 FROM_HERE,
90 base::Bind(&AudioRecorderImpl::FinalizeOnAudioThread,
91 base::Unretained(this)));
94 // Private methods.
96 void AudioRecorderImpl::InitializeOnAudioThread() {
97 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
99 media::AudioParameters params;
100 if (params_for_testing_) {
101 params = *params_for_testing_;
102 } else {
103 params = media::AudioManager::Get()->GetInputStreamParameters(
104 media::AudioManagerBase::kDefaultDeviceId);
105 params = media::AudioParameters(params.format(),
106 params.channel_layout(),
107 params.sample_rate(),
108 params.bits_per_sample(),
109 params.frames_per_buffer(),
110 media::AudioParameters::NO_EFFECTS);
113 total_buffer_frames_ = kProcessIntervalMs * params.sample_rate() / 1000;
114 buffer_ = media::AudioBus::Create(params.channels(), total_buffer_frames_);
115 buffer_frame_index_ = 0;
117 stream_ = input_stream_for_testing_
118 ? input_stream_for_testing_.get()
119 : media::AudioManager::Get()->MakeAudioInputStream(
120 params, media::AudioManagerBase::kDefaultDeviceId);
122 if (!stream_ || !stream_->Open()) {
123 LOG(ERROR) << "Failed to open an input stream.";
124 if (stream_) {
125 stream_->Close();
126 stream_ = nullptr;
128 return;
130 stream_->SetVolume(stream_->GetMaxVolume());
133 void AudioRecorderImpl::RecordOnAudioThread() {
134 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
135 if (!stream_ || is_recording_)
136 return;
138 VLOG(3) << "Starting recording.";
139 stream_->Start(this);
140 is_recording_ = true;
143 void AudioRecorderImpl::StopOnAudioThread() {
144 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
145 if (!stream_ || !is_recording_)
146 return;
148 VLOG(3) << "Stopping recording.";
149 stream_->Stop();
150 is_recording_ = false;
153 void AudioRecorderImpl::StopAndCloseOnAudioThread() {
154 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
155 if (!stream_)
156 return;
158 StopOnAudioThread();
159 stream_->Close();
160 stream_ = nullptr;
163 void AudioRecorderImpl::FinalizeOnAudioThread() {
164 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
165 StopAndCloseOnAudioThread();
166 delete this;
169 void AudioRecorderImpl::OnData(media::AudioInputStream* stream,
170 const media::AudioBus* source,
171 uint32 /* hardware_delay_bytes */,
172 double /* volume */) {
173 // source->frames() == source_params.frames_per_buffer(), so we only have
174 // one chunk of data in the source; correspondingly set the destination
175 // size to one chunk.
177 int remaining_buffer_frames = buffer_->frames() - buffer_frame_index_;
178 int frames_to_copy = std::min(remaining_buffer_frames, source->frames());
179 source->CopyPartialFramesTo(0, frames_to_copy, buffer_frame_index_,
180 buffer_.get());
181 buffer_frame_index_ += frames_to_copy;
183 // Buffer full, send it for processing.
184 if (buffer_->frames() == buffer_frame_index_) {
185 ProcessSamples(buffer_.Pass(), decode_callback_);
186 buffer_ = media::AudioBus::Create(source->channels(), total_buffer_frames_);
187 buffer_frame_index_ = 0;
189 // Copy any remaining frames in the source to our buffer.
190 int remaining_source_frames = source->frames() - frames_to_copy;
191 source->CopyPartialFramesTo(frames_to_copy, remaining_source_frames,
192 buffer_frame_index_, buffer_.get());
193 buffer_frame_index_ += remaining_source_frames;
197 void AudioRecorderImpl::OnError(media::AudioInputStream* /* stream */) {
198 LOG(ERROR) << "Error during sound recording.";
199 media::AudioManager::Get()->GetTaskRunner()->PostTask(
200 FROM_HERE,
201 base::Bind(&AudioRecorderImpl::StopAndCloseOnAudioThread,
202 base::Unretained(this)));
205 void AudioRecorderImpl::FlushAudioLoopForTesting() {
206 if (media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread())
207 return;
209 // Queue task on the audio thread, when it is executed, that means we've
210 // successfully executed all the tasks before us.
211 base::RunLoop rl;
212 media::AudioManager::Get()->GetTaskRunner()->PostTaskAndReply(
213 FROM_HERE,
214 base::Bind(
215 base::IgnoreResult(&AudioRecorderImpl::FlushAudioLoopForTesting),
216 base::Unretained(this)),
217 rl.QuitClosure());
218 rl.Run();
221 } // namespace audio_modem