| Index: content/renderer/media/webaudio_capturer_source.cc
|
| diff --git a/content/renderer/media/webaudio_capturer_source.cc b/content/renderer/media/webaudio_capturer_source.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..777bb90a84edff3b2593eb12615c114493ecee08
|
| --- /dev/null
|
| +++ b/content/renderer/media/webaudio_capturer_source.cc
|
| @@ -0,0 +1,95 @@
|
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "content/renderer/media/webaudio_capturer_source.h"
|
| +
|
| +#include "base/logging.h"
|
| +
|
| +using media::AudioBus;
|
| +using media::AudioFifo;
|
| +using media::AudioParameters;
|
| +
|
| +static const int kFifoSize = 2048;
|
| +
|
| +namespace content {
|
| +
|
| +WebAudioCapturerSource::WebAudioCapturerSource()
|
| + : callback_(0),
|
| + started_(false) {
|
| + DVLOG(1) << "WebAudioCapturerSource::WebAudioCapturerSource()";
|
| +}
|
| +
|
| +WebAudioCapturerSource::~WebAudioCapturerSource() {
|
| + DVLOG(1) << "WebAudioCapturerSource::~WebAudioCapturerSource()";
|
| +}
|
| +
|
| +void WebAudioCapturerSource::Initialize(
|
| + const media::AudioParameters& params,
|
| + media::AudioCapturerSource::CaptureCallback* callback,
|
| + media::AudioCapturerSource::CaptureEventHandler* event_handler) {
|
| + DVLOG(1) << "WebAudioCapturerSource::Initialize()";
|
| + base::AutoLock auto_lock(lock_);
|
| + params_ = params;
|
| + callback_ = callback;
|
| + wrapper_bus_ = AudioBus::CreateWrapper(params.channels());
|
| + capture_bus_ = AudioBus::Create(params);
|
| + fifo_.reset(new AudioFifo(params.channels(), kFifoSize));
|
| +}
|
| +
|
| +void WebAudioCapturerSource::Start() {
|
| + DVLOG(1) << "WebAudioCapturerSource::Start()";
|
| + // TODO(crogers): check that our client actually is calling Start()
|
| + // and do something with this information.
|
| + started_ = true;
|
| +}
|
| +
|
| +void WebAudioCapturerSource::Stop() {
|
| + DVLOG(1) << "WebAudioCapturerSource::Stop()";
|
| + started_ = false;
|
| +}
|
| +
|
| +void WebAudioCapturerSource::consumeAudio(
|
| + const WebKit::WebVector<const float*>& audio_data,
|
| + size_t number_of_frames) {
|
| + base::AutoLock auto_lock(lock_);
|
| + if (!callback_)
|
| + return;
|
| +
|
| + // TODO(crogers): Handle possible sample-rate mismatch.
|
| + // Currently we don't even receive this information from WebKit.
|
| +
|
| + wrapper_bus_->set_frames(number_of_frames);
|
| +
|
| + // TODO(henrika,crogers): improve how we deal with channels depending on
|
| + // the source. Today, the native capture channel config is utilized even
|
| + // for cases where the source is not a microphone.
|
| + if (wrapper_bus_->channels() == static_cast<int>(audio_data.size())) {
|
| + for (size_t i = 0; i < audio_data.size(); ++i)
|
| + wrapper_bus_->SetChannelData(i, const_cast<float*>(audio_data[0]));
|
| + } else if (wrapper_bus_->channels() == 2 &&
|
| + static_cast<int>(audio_data.size()) == 1) {
|
| + // Mono -> Stereo channel up-mixing.
|
| + wrapper_bus_->SetChannelData(0, const_cast<float*>(audio_data[0]));
|
| + wrapper_bus_->SetChannelData(1, const_cast<float*>(audio_data[0]));
|
| + } else {
|
| + LOG(ERROR) << "WebAudioCapturerSource::Consume() : Channel mismatch.";
|
| + return;
|
| + }
|
| +
|
| + // Handle mismatch between WebAudio buffer-size and WebRTC.
|
| + int available = fifo_->max_frames() - fifo_->frames();
|
| + if (available < static_cast<int>(number_of_frames)) {
|
| + LOG(ERROR) << "WebAudioCapturerSource::Consume() : FIFO overrun.";
|
| + return;
|
| + }
|
| +
|
| + fifo_->Push(wrapper_bus_.get());
|
| + int capture_frames = params_.frames_per_buffer();
|
| + while (fifo_->frames() >= capture_frames) {
|
| + fifo_->Consume(capture_bus_.get(), 0, capture_frames);
|
| + callback_->Capture(capture_bus_.get(), 0, 1.0);
|
| + }
|
| +}
|
| +
|
| +} // namespace content
|
|
|