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.
5 #include "content/renderer/media/webaudio_capturer_source.h"
7 #include "base/logging.h"
8 #include "content/renderer/media/webrtc_local_audio_source_provider.h"
9 #include "content/renderer/media/webrtc_local_audio_track.h"
11 using media::AudioBus;
12 using media::AudioFifo;
13 using media::AudioParameters;
14 using media::ChannelLayout;
15 using media::CHANNEL_LAYOUT_MONO;
16 using media::CHANNEL_LAYOUT_STEREO;
18 static const int kMaxNumberOfBuffersInFifo = 5;
22 WebAudioCapturerSource::WebAudioCapturerSource()
24 source_provider_(NULL) {
27 WebAudioCapturerSource::~WebAudioCapturerSource() {
30 void WebAudioCapturerSource::setFormat(
31 size_t number_of_channels, float sample_rate) {
32 DCHECK(thread_checker_.CalledOnValidThread());
33 DVLOG(1) << "WebAudioCapturerSource::setFormat(sample_rate="
34 << sample_rate << ")";
35 if (number_of_channels > 2) {
36 // TODO(xians): Handle more than just the mono and stereo cases.
37 LOG(WARNING) << "WebAudioCapturerSource::setFormat() : unhandled format.";
41 ChannelLayout channel_layout =
42 number_of_channels == 1 ? CHANNEL_LAYOUT_MONO : CHANNEL_LAYOUT_STEREO;
44 base::AutoLock auto_lock(lock_);
45 // Set the format used by this WebAudioCapturerSource. We are using 10ms data
46 // as buffer size since that is the native buffer size of WebRtc packet
48 params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
49 channel_layout, number_of_channels, 0, sample_rate, 16,
52 // Update the downstream client to use the same format as what WebKit
55 track_->SetCaptureFormat(params_);
57 wrapper_bus_ = AudioBus::CreateWrapper(params_.channels());
58 capture_bus_ = AudioBus::Create(params_);
59 fifo_.reset(new AudioFifo(
61 kMaxNumberOfBuffersInFifo * params_.frames_per_buffer()));
64 void WebAudioCapturerSource::Start(
65 WebRtcLocalAudioTrack* track,
66 WebRtcLocalAudioSourceProvider* source_provider) {
67 DCHECK(thread_checker_.CalledOnValidThread());
69 // |source_provider| may be NULL if no getUserMedia has been called before
70 // calling CreateMediaStreamDestination.
71 // The downstream client should be configured the same as what WebKit
73 track->SetCaptureFormat(params_);
75 base::AutoLock auto_lock(lock_);
77 source_provider_ = source_provider;
80 void WebAudioCapturerSource::Stop() {
81 DCHECK(thread_checker_.CalledOnValidThread());
82 base::AutoLock auto_lock(lock_);
84 source_provider_ = NULL;
87 void WebAudioCapturerSource::consumeAudio(
88 const WebKit::WebVector<const float*>& audio_data,
89 size_t number_of_frames) {
90 base::AutoLock auto_lock(lock_);
94 wrapper_bus_->set_frames(number_of_frames);
96 // Make sure WebKit is honoring what it told us up front
97 // about the channels.
98 DCHECK_EQ(params_.channels(), static_cast<int>(audio_data.size()));
100 for (size_t i = 0; i < audio_data.size(); ++i)
101 wrapper_bus_->SetChannelData(i, const_cast<float*>(audio_data[i]));
103 // Handle mismatch between WebAudio buffer-size and WebRTC.
104 int available = fifo_->max_frames() - fifo_->frames();
105 if (available < static_cast<int>(number_of_frames)) {
106 NOTREACHED() << "WebAudioCapturerSource::Consume() : FIFO overrun.";
110 fifo_->Push(wrapper_bus_.get());
111 int capture_frames = params_.frames_per_buffer();
114 bool key_pressed = false;
115 while (fifo_->frames() >= capture_frames) {
116 if (source_provider_) {
117 source_provider_->GetAudioProcessingParams(
118 &delay_ms, &volume, &key_pressed);
120 fifo_->Consume(capture_bus_.get(), 0, capture_frames);
121 track_->Capture(capture_bus_.get(), delay_ms, volume, key_pressed);
125 } // namespace content