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