| 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::AudioBus; |
| 10 using media::AudioFifo; |
| 11 using media::AudioParameters; |
| 12 |
| 13 static const int kFifoSize = 2048; |
| 14 |
| 15 namespace content { |
| 16 |
| 17 WebAudioCapturerSource::WebAudioCapturerSource() |
| 18 : callback_(0), |
| 19 started_(false) { |
| 20 DVLOG(1) << "WebAudioCapturerSource::WebAudioCapturerSource()"; |
| 21 } |
| 22 |
| 23 WebAudioCapturerSource::~WebAudioCapturerSource() { |
| 24 DVLOG(1) << "WebAudioCapturerSource::~WebAudioCapturerSource()"; |
| 25 } |
| 26 |
| 27 void WebAudioCapturerSource::Initialize( |
| 28 const media::AudioParameters& params, |
| 29 media::AudioCapturerSource::CaptureCallback* callback, |
| 30 media::AudioCapturerSource::CaptureEventHandler* event_handler) { |
| 31 DVLOG(1) << "WebAudioCapturerSource::Initialize()"; |
| 32 base::AutoLock auto_lock(lock_); |
| 33 params_ = params; |
| 34 callback_ = callback; |
| 35 wrapper_bus_ = AudioBus::CreateWrapper(params.channels()); |
| 36 capture_bus_ = AudioBus::Create(params); |
| 37 fifo_.reset(new AudioFifo(params.channels(), kFifoSize)); |
| 38 } |
| 39 |
| 40 void WebAudioCapturerSource::Start() { |
| 41 DVLOG(1) << "WebAudioCapturerSource::Start()"; |
| 42 // TODO(crogers): check that our client actually is calling Start() |
| 43 // and do something with this information. |
| 44 started_ = true; |
| 45 } |
| 46 |
| 47 void WebAudioCapturerSource::Stop() { |
| 48 DVLOG(1) << "WebAudioCapturerSource::Stop()"; |
| 49 started_ = false; |
| 50 } |
| 51 |
| 52 void WebAudioCapturerSource::consumeAudio( |
| 53 const WebKit::WebVector<const float*>& audio_data, |
| 54 size_t number_of_frames) { |
| 55 base::AutoLock auto_lock(lock_); |
| 56 if (!callback_) |
| 57 return; |
| 58 |
| 59 // TODO(crogers): Handle possible sample-rate mismatch. |
| 60 // Currently we don't even receive this information from WebKit. |
| 61 |
| 62 wrapper_bus_->set_frames(number_of_frames); |
| 63 |
| 64 // TODO(henrika,crogers): improve how we deal with channels depending on |
| 65 // the source. Today, the native capture channel config is utilized even |
| 66 // for cases where the source is not a microphone. |
| 67 if (wrapper_bus_->channels() == static_cast<int>(audio_data.size())) { |
| 68 for (size_t i = 0; i < audio_data.size(); ++i) |
| 69 wrapper_bus_->SetChannelData(i, const_cast<float*>(audio_data[0])); |
| 70 } else if (wrapper_bus_->channels() == 2 && |
| 71 static_cast<int>(audio_data.size()) == 1) { |
| 72 // Mono -> Stereo channel up-mixing. |
| 73 wrapper_bus_->SetChannelData(0, const_cast<float*>(audio_data[0])); |
| 74 wrapper_bus_->SetChannelData(1, const_cast<float*>(audio_data[0])); |
| 75 } else { |
| 76 LOG(ERROR) << "WebAudioCapturerSource::Consume() : Channel mismatch."; |
| 77 return; |
| 78 } |
| 79 |
| 80 // Handle mismatch between WebAudio buffer-size and WebRTC. |
| 81 int available = fifo_->max_frames() - fifo_->frames(); |
| 82 if (available < static_cast<int>(number_of_frames)) { |
| 83 LOG(ERROR) << "WebAudioCapturerSource::Consume() : FIFO overrun."; |
| 84 return; |
| 85 } |
| 86 |
| 87 fifo_->Push(wrapper_bus_.get()); |
| 88 int capture_frames = params_.frames_per_buffer(); |
| 89 while (fifo_->frames() >= capture_frames) { |
| 90 fifo_->Consume(capture_bus_.get(), 0, capture_frames); |
| 91 callback_->Capture(capture_bus_.get(), 0, 1.0); |
| 92 } |
| 93 } |
| 94 |
| 95 } // namespace content |
| OLD | NEW |