Chromium Code Reviews| Index: content/renderer/media/webaudio_capturer_source.cc |
| =================================================================== |
| --- content/renderer/media/webaudio_capturer_source.cc (revision 0) |
| +++ content/renderer/media/webaudio_capturer_source.cc (revision 0) |
| @@ -0,0 +1,82 @@ |
| +// 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::AudioParameters; |
| +using media::AudioBus; |
| +using media::AudioFifo; |
| + |
| +static const int kFifoSize = 2048; |
| + |
| +namespace content { |
| + |
| +WebAudioCapturerSource::WebAudioCapturerSource() |
| + : callback_(0), |
| + started_(false) { |
| +} |
| + |
| +WebAudioCapturerSource::~WebAudioCapturerSource() { |
| +} |
| + |
| +void WebAudioCapturerSource::Initialize( |
| + const media::AudioParameters& params, |
| + media::AudioCapturerSource::CaptureCallback* callback, |
| + media::AudioCapturerSource::CaptureEventHandler* event_handler) { |
| + 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() { |
| + // TODO(crogers): check that our client actually is calling Start() |
| + // and do something with this information. |
| + started_ = true; |
| +} |
| + |
| +void WebAudioCapturerSource::Stop() { |
| + started_ = false; |
| +} |
| + |
| +void WebAudioCapturerSource::HandleCapture( |
| + const WebKit::WebVector<const float*>& audio_data, |
| + size_t number_of_frames) { |
| + base::AutoLock auto_lock(lock_); |
| + if (!callback_) |
| + return; |
| + |
| + if (wrapper_bus_->channels() != static_cast<int>(audio_data.size())) { |
| + LOG(ERROR) << "WebAudioCapturerSource::Consume() : Channel mismatch."; |
| + return; |
| + } |
| + |
| + // TODO(crogers): Handle possibly sample-rate mismatch. |
| + // Currently we don't even receive this information from WebKit. |
| + |
| + // Wrap. |
|
tommi (sloooow) - chröme
2012/11/12 09:46:18
comment doesn't add much :)
|
| + wrapper_bus_->set_frames(number_of_frames); |
| + for (unsigned i = 0; i < audio_data.size(); ++i) |
|
tommi (sloooow) - chröme
2012/11/12 09:46:18
s/unsigned/size_t or int?
|
| + wrapper_bus_->SetChannelData(i, const_cast<float*>(audio_data[i])); |
| + |
| + // 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 |