| 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 #include "content/renderer/media/webrtc_audio_capturer.h" |
| 9 |
| 10 using media::AudioBus; |
| 11 using media::AudioFifo; |
| 12 using media::AudioParameters; |
| 13 using media::ChannelLayout; |
| 14 using media::CHANNEL_LAYOUT_MONO; |
| 15 using media::CHANNEL_LAYOUT_STEREO; |
| 16 |
| 17 static const int kFifoSize = 2048; |
| 18 |
| 19 namespace content { |
| 20 |
| 21 WebAudioCapturerSource::WebAudioCapturerSource(WebRtcAudioCapturer* capturer) |
| 22 : capturer_(capturer), |
| 23 set_format_channels_(0), |
| 24 callback_(0), |
| 25 started_(false) { |
| 26 } |
| 27 |
| 28 WebAudioCapturerSource::~WebAudioCapturerSource() { |
| 29 } |
| 30 |
| 31 void WebAudioCapturerSource::setFormat( |
| 32 size_t number_of_channels, float sample_rate) { |
| 33 if (number_of_channels <= 2) { |
| 34 set_format_channels_ = number_of_channels; |
| 35 ChannelLayout channel_layout = |
| 36 number_of_channels == 1 ? CHANNEL_LAYOUT_MONO : CHANNEL_LAYOUT_STEREO; |
| 37 capturer_->SetCapturerSource(this, channel_layout, sample_rate); |
| 38 capturer_->Start(); |
| 39 } else { |
| 40 // TODO(crogers): Handle more than just the mono and stereo cases. |
| 41 LOG(WARNING) << "WebAudioCapturerSource::setFormat() : unhandled format."; |
| 42 } |
| 43 } |
| 44 |
| 45 void WebAudioCapturerSource::Initialize( |
| 46 const media::AudioParameters& params, |
| 47 media::AudioCapturerSource::CaptureCallback* callback, |
| 48 media::AudioCapturerSource::CaptureEventHandler* event_handler) { |
| 49 // The downstream client should be configured the same as what WebKit |
| 50 // is feeding it. |
| 51 DCHECK_EQ(set_format_channels_, params.channels()); |
| 52 |
| 53 base::AutoLock auto_lock(lock_); |
| 54 params_ = params; |
| 55 callback_ = callback; |
| 56 wrapper_bus_ = AudioBus::CreateWrapper(params.channels()); |
| 57 capture_bus_ = AudioBus::Create(params); |
| 58 fifo_.reset(new AudioFifo(params.channels(), kFifoSize)); |
| 59 } |
| 60 |
| 61 void WebAudioCapturerSource::Start() { |
| 62 started_ = true; |
| 63 } |
| 64 |
| 65 void WebAudioCapturerSource::Stop() { |
| 66 started_ = false; |
| 67 } |
| 68 |
| 69 void WebAudioCapturerSource::consumeAudio( |
| 70 const WebKit::WebVector<const float*>& audio_data, |
| 71 size_t number_of_frames) { |
| 72 base::AutoLock auto_lock(lock_); |
| 73 |
| 74 if (!callback_) |
| 75 return; |
| 76 |
| 77 wrapper_bus_->set_frames(number_of_frames); |
| 78 |
| 79 // Make sure WebKit is honoring what it told us up front |
| 80 // about the channels. |
| 81 DCHECK_EQ(set_format_channels_, static_cast<int>(audio_data.size())); |
| 82 DCHECK_EQ(set_format_channels_, wrapper_bus_->channels()); |
| 83 |
| 84 for (size_t i = 0; i < audio_data.size(); ++i) |
| 85 wrapper_bus_->SetChannelData(i, const_cast<float*>(audio_data[i])); |
| 86 |
| 87 // Handle mismatch between WebAudio buffer-size and WebRTC. |
| 88 int available = fifo_->max_frames() - fifo_->frames(); |
| 89 if (available < static_cast<int>(number_of_frames)) { |
| 90 LOG(ERROR) << "WebAudioCapturerSource::Consume() : FIFO overrun."; |
| 91 return; |
| 92 } |
| 93 |
| 94 fifo_->Push(wrapper_bus_.get()); |
| 95 int capture_frames = params_.frames_per_buffer(); |
| 96 while (fifo_->frames() >= capture_frames) { |
| 97 fifo_->Consume(capture_bus_.get(), 0, capture_frames); |
| 98 callback_->Capture(capture_bus_.get(), 0, 1.0); |
| 99 } |
| 100 } |
| 101 |
| 102 } // namespace content |
| OLD | NEW |