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