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