Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / audio_modem / audio_recorder_impl.cc
blob43b5d54f73ff8e1869b71b37a855c6953eb48059
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.set_effects(media::AudioParameters::NO_EFFECTS);
108 total_buffer_frames_ = kProcessIntervalMs * params.sample_rate() / 1000;
109 buffer_ = media::AudioBus::Create(params.channels(), total_buffer_frames_);
110 buffer_frame_index_ = 0;
112 stream_ = input_stream_for_testing_
113 ? input_stream_for_testing_.get()
114 : media::AudioManager::Get()->MakeAudioInputStream(
115 params, media::AudioManagerBase::kDefaultDeviceId);
117 if (!stream_ || !stream_->Open()) {
118 LOG(ERROR) << "Failed to open an input stream.";
119 if (stream_) {
120 stream_->Close();
121 stream_ = nullptr;
123 return;
125 stream_->SetVolume(stream_->GetMaxVolume());
128 void AudioRecorderImpl::RecordOnAudioThread() {
129 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
130 if (!stream_ || is_recording_)
131 return;
133 VLOG(3) << "Starting recording.";
134 stream_->Start(this);
135 is_recording_ = true;
138 void AudioRecorderImpl::StopOnAudioThread() {
139 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
140 if (!stream_ || !is_recording_)
141 return;
143 VLOG(3) << "Stopping recording.";
144 stream_->Stop();
145 is_recording_ = false;
148 void AudioRecorderImpl::StopAndCloseOnAudioThread() {
149 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
150 if (!stream_)
151 return;
153 StopOnAudioThread();
154 stream_->Close();
155 stream_ = nullptr;
158 void AudioRecorderImpl::FinalizeOnAudioThread() {
159 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
160 StopAndCloseOnAudioThread();
161 delete this;
164 void AudioRecorderImpl::OnData(media::AudioInputStream* stream,
165 const media::AudioBus* source,
166 uint32 /* hardware_delay_bytes */,
167 double /* volume */) {
168 // source->frames() == source_params.frames_per_buffer(), so we only have
169 // one chunk of data in the source; correspondingly set the destination
170 // size to one chunk.
172 int remaining_buffer_frames = buffer_->frames() - buffer_frame_index_;
173 int frames_to_copy = std::min(remaining_buffer_frames, source->frames());
174 source->CopyPartialFramesTo(0, frames_to_copy, buffer_frame_index_,
175 buffer_.get());
176 buffer_frame_index_ += frames_to_copy;
178 // Buffer full, send it for processing.
179 if (buffer_->frames() == buffer_frame_index_) {
180 ProcessSamples(buffer_.Pass(), decode_callback_);
181 buffer_ = media::AudioBus::Create(source->channels(), total_buffer_frames_);
182 buffer_frame_index_ = 0;
184 // Copy any remaining frames in the source to our buffer.
185 int remaining_source_frames = source->frames() - frames_to_copy;
186 source->CopyPartialFramesTo(frames_to_copy, remaining_source_frames,
187 buffer_frame_index_, buffer_.get());
188 buffer_frame_index_ += remaining_source_frames;
192 void AudioRecorderImpl::OnError(media::AudioInputStream* /* stream */) {
193 LOG(ERROR) << "Error during sound recording.";
194 media::AudioManager::Get()->GetTaskRunner()->PostTask(
195 FROM_HERE,
196 base::Bind(&AudioRecorderImpl::StopAndCloseOnAudioThread,
197 base::Unretained(this)));
200 void AudioRecorderImpl::FlushAudioLoopForTesting() {
201 if (media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread())
202 return;
204 // Queue task on the audio thread, when it is executed, that means we've
205 // successfully executed all the tasks before us.
206 base::RunLoop rl;
207 media::AudioManager::Get()->GetTaskRunner()->PostTaskAndReply(
208 FROM_HERE,
209 base::Bind(
210 base::IgnoreResult(&AudioRecorderImpl::FlushAudioLoopForTesting),
211 base::Unretained(this)),
212 rl.QuitClosure());
213 rl.Run();
216 } // namespace audio_modem