Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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 "content/renderer/media/webaudio_capturer_source.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 | |
| 9 using media::AudioParameters; | |
| 10 using media::AudioBus; | |
| 11 using media::AudioFifo; | |
| 12 | |
| 13 static const int kFifoSize = 2048; | |
| 14 | |
| 15 namespace content { | |
| 16 | |
| 17 WebAudioCapturerSource::WebAudioCapturerSource() | |
| 18 : callback_(0), | |
| 19 started_(false) { | |
| 20 } | |
| 21 | |
| 22 WebAudioCapturerSource::~WebAudioCapturerSource() { | |
| 23 } | |
| 24 | |
| 25 void WebAudioCapturerSource::Initialize( | |
| 26 const media::AudioParameters& params, | |
| 27 media::AudioCapturerSource::CaptureCallback* callback, | |
| 28 media::AudioCapturerSource::CaptureEventHandler* event_handler) { | |
| 29 base::AutoLock auto_lock(lock_); | |
| 30 params_ = params; | |
| 31 callback_ = callback; | |
| 32 wrapper_bus_ = AudioBus::CreateWrapper(params.channels()); | |
| 33 capture_bus_ = AudioBus::Create(params); | |
| 34 fifo_.reset(new AudioFifo(params.channels(), kFifoSize)); | |
| 35 } | |
| 36 | |
| 37 void WebAudioCapturerSource::Start() { | |
| 38 // TODO(crogers): check that our client actually is calling Start() | |
| 39 // and do something with this information. | |
| 40 started_ = true; | |
| 41 } | |
| 42 | |
| 43 void WebAudioCapturerSource::Stop() { | |
| 44 started_ = false; | |
| 45 } | |
| 46 | |
| 47 void WebAudioCapturerSource::HandleCapture( | |
| 48 const WebKit::WebVector<const float*>& audio_data, | |
| 49 size_t number_of_frames) { | |
| 50 base::AutoLock auto_lock(lock_); | |
| 51 if (!callback_) | |
| 52 return; | |
| 53 | |
| 54 if (wrapper_bus_->channels() != static_cast<int>(audio_data.size())) { | |
| 55 LOG(ERROR) << "WebAudioCapturerSource::Consume() : Channel mismatch."; | |
| 56 return; | |
| 57 } | |
| 58 | |
| 59 // TODO(crogers): Handle possibly sample-rate mismatch. | |
| 60 // Currently we don't even receive this information from WebKit. | |
| 61 | |
| 62 // Wrap. | |
|
tommi (sloooow) - chröme
2012/11/12 09:46:18
comment doesn't add much :)
| |
| 63 wrapper_bus_->set_frames(number_of_frames); | |
| 64 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?
| |
| 65 wrapper_bus_->SetChannelData(i, const_cast<float*>(audio_data[i])); | |
| 66 | |
| 67 // Handle mismatch between WebAudio buffer-size and WebRTC. | |
| 68 int available = fifo_->max_frames() - fifo_->frames(); | |
| 69 if (available < static_cast<int>(number_of_frames)) { | |
| 70 LOG(ERROR) << "WebAudioCapturerSource::Consume() : FIFO overrun."; | |
| 71 return; | |
| 72 } | |
| 73 | |
| 74 fifo_->Push(wrapper_bus_.get()); | |
| 75 int capture_frames = params_.frames_per_buffer(); | |
| 76 while (fifo_->frames() >= capture_frames) { | |
| 77 fifo_->Consume(capture_bus_.get(), 0, capture_frames); | |
| 78 callback_->Capture(capture_bus_.get(), 0, 1.0); | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 } // namespace content | |
| OLD | NEW |