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_audio_capturer.h"
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
;
17 static const int kFifoSize
= 2048;
21 WebAudioCapturerSource::WebAudioCapturerSource(WebRtcAudioCapturer
* capturer
)
22 : capturer_(capturer
),
23 set_format_channels_(0),
28 WebAudioCapturerSource::~WebAudioCapturerSource() {
31 void WebAudioCapturerSource::setFormat(
32 size_t number_of_channels
, float sample_rate
) {
33 DVLOG(1) << "WebAudioCapturerSource::setFormat(sample_rate="
34 << sample_rate
<< ")";
35 if (number_of_channels
<= 2) {
36 set_format_channels_
= number_of_channels
;
37 ChannelLayout channel_layout
=
38 number_of_channels
== 1 ? CHANNEL_LAYOUT_MONO
: CHANNEL_LAYOUT_STEREO
;
39 capturer_
->SetCapturerSource(this, channel_layout
, sample_rate
);
42 // TODO(crogers): Handle more than just the mono and stereo cases.
43 LOG(WARNING
) << "WebAudioCapturerSource::setFormat() : unhandled format.";
47 void WebAudioCapturerSource::Initialize(
48 const media::AudioParameters
& params
,
49 media::AudioCapturerSource::CaptureCallback
* callback
,
51 // The downstream client should be configured the same as what WebKit
53 DCHECK_EQ(set_format_channels_
, params
.channels());
55 base::AutoLock
auto_lock(lock_
);
58 wrapper_bus_
= AudioBus::CreateWrapper(params
.channels());
59 capture_bus_
= AudioBus::Create(params
);
60 fifo_
.reset(new AudioFifo(params
.channels(), kFifoSize
));
63 void WebAudioCapturerSource::Start() {
67 void WebAudioCapturerSource::Stop() {
71 void WebAudioCapturerSource::consumeAudio(
72 const WebKit::WebVector
<const float*>& audio_data
,
73 size_t number_of_frames
) {
74 base::AutoLock
auto_lock(lock_
);
79 wrapper_bus_
->set_frames(number_of_frames
);
81 // Make sure WebKit is honoring what it told us up front
82 // about the channels.
83 DCHECK_EQ(set_format_channels_
, static_cast<int>(audio_data
.size()));
84 DCHECK_EQ(set_format_channels_
, wrapper_bus_
->channels());
86 for (size_t i
= 0; i
< audio_data
.size(); ++i
)
87 wrapper_bus_
->SetChannelData(i
, const_cast<float*>(audio_data
[i
]));
89 // Handle mismatch between WebAudio buffer-size and WebRTC.
90 int available
= fifo_
->max_frames() - fifo_
->frames();
91 if (available
< static_cast<int>(number_of_frames
)) {
92 LOG(ERROR
) << "WebAudioCapturerSource::Consume() : FIFO overrun.";
96 fifo_
->Push(wrapper_bus_
.get());
97 int capture_frames
= params_
.frames_per_buffer();
98 while (fifo_
->frames() >= capture_frames
) {
99 fifo_
->Consume(capture_bus_
.get(), 0, capture_frames
);
100 callback_
->Capture(capture_bus_
.get(), 0, 1.0);
104 } // namespace content