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 "base/time/time.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(
23 const blink::WebMediaStreamSource
& blink_source
)
25 audio_format_changed_(false),
26 blink_source_(blink_source
) {
29 WebAudioCapturerSource::~WebAudioCapturerSource() {
30 DCHECK(thread_checker_
.CalledOnValidThread());
31 removeFromBlinkSource();
34 void WebAudioCapturerSource::setFormat(
35 size_t number_of_channels
, float sample_rate
) {
36 DCHECK(thread_checker_
.CalledOnValidThread());
37 DVLOG(1) << "WebAudioCapturerSource::setFormat(sample_rate="
38 << sample_rate
<< ")";
39 if (number_of_channels
> 2) {
40 // TODO(xians): Handle more than just the mono and stereo cases.
41 LOG(WARNING
) << "WebAudioCapturerSource::setFormat() : unhandled format.";
45 ChannelLayout channel_layout
=
46 number_of_channels
== 1 ? CHANNEL_LAYOUT_MONO
: CHANNEL_LAYOUT_STEREO
;
48 base::AutoLock
auto_lock(lock_
);
49 // Set the format used by this WebAudioCapturerSource. We are using 10ms data
50 // as buffer size since that is the native buffer size of WebRtc packet
52 params_
.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY
, channel_layout
,
53 sample_rate
, 16, sample_rate
/ 100);
54 audio_format_changed_
= true;
56 wrapper_bus_
= AudioBus::CreateWrapper(params_
.channels());
57 capture_bus_
= AudioBus::Create(params_
);
58 fifo_
.reset(new AudioFifo(
60 kMaxNumberOfBuffersInFifo
* params_
.frames_per_buffer()));
63 void WebAudioCapturerSource::Start(WebRtcLocalAudioTrack
* track
) {
64 DCHECK(thread_checker_
.CalledOnValidThread());
66 base::AutoLock
auto_lock(lock_
);
70 void WebAudioCapturerSource::Stop() {
71 DCHECK(thread_checker_
.CalledOnValidThread());
73 base::AutoLock
auto_lock(lock_
);
76 // removeFromBlinkSource() should not be called while |lock_| is acquired,
77 // as it could result in a deadlock.
78 removeFromBlinkSource();
81 void WebAudioCapturerSource::consumeAudio(
82 const blink::WebVector
<const float*>& audio_data
,
83 size_t number_of_frames
) {
84 base::AutoLock
auto_lock(lock_
);
88 // Update the downstream client if the audio format has been changed.
89 if (audio_format_changed_
) {
90 track_
->OnSetFormat(params_
);
91 audio_format_changed_
= false;
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 // Compute the estimated capture time of the first sample frame of audio that
111 // will be consumed from the FIFO in the loop below.
112 base::TimeTicks estimated_capture_time
= base::TimeTicks::Now() -
113 fifo_
->frames() * base::TimeDelta::FromSeconds(1) / params_
.sample_rate();
115 fifo_
->Push(wrapper_bus_
.get());
116 while (fifo_
->frames() >= capture_bus_
->frames()) {
117 fifo_
->Consume(capture_bus_
.get(), 0, capture_bus_
->frames());
118 track_
->Capture(*capture_bus_
, estimated_capture_time
, false);
120 // Advance the estimated capture time for the next FIFO consume operation.
121 estimated_capture_time
+=
122 capture_bus_
->frames() * base::TimeDelta::FromSeconds(1) /
123 params_
.sample_rate();
127 // If registered as audio consumer in |blink_source_|, deregister from
128 // |blink_source_| and stop keeping a reference to |blink_source_|.
129 // Failure to call this method when stopping the track might leave an invalid
130 // WebAudioCapturerSource reference still registered as an audio consumer on
132 void WebAudioCapturerSource::removeFromBlinkSource() {
133 if (!blink_source_
.isNull()) {
134 blink_source_
.removeAudioConsumer(this);
135 blink_source_
.reset();
139 } // namespace content