Roll src/third_party/WebKit d10c917:a1123a1 (svn 198729:198730)
[chromium-blink-merge.git] / content / renderer / media / webaudio_capturer_source.cc
blob465e7bbdf570c5b224d1a6cc2c9443d152e1761f
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;
20 namespace content {
22 WebAudioCapturerSource::WebAudioCapturerSource(
23 const blink::WebMediaStreamSource& blink_source)
24 : track_(NULL),
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.";
42 return;
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
51 // running on.
52 params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
53 channel_layout, number_of_channels, sample_rate, 16,
54 sample_rate / 100);
55 audio_format_changed_ = true;
57 wrapper_bus_ = AudioBus::CreateWrapper(params_.channels());
58 capture_bus_ = AudioBus::Create(params_);
59 fifo_.reset(new AudioFifo(
60 params_.channels(),
61 kMaxNumberOfBuffersInFifo * params_.frames_per_buffer()));
64 void WebAudioCapturerSource::Start(WebRtcLocalAudioTrack* track) {
65 DCHECK(thread_checker_.CalledOnValidThread());
66 DCHECK(track);
67 base::AutoLock auto_lock(lock_);
68 track_ = track;
71 void WebAudioCapturerSource::Stop() {
72 DCHECK(thread_checker_.CalledOnValidThread());
73 base::AutoLock auto_lock(lock_);
74 track_ = NULL;
75 removeFromBlinkSource();
78 void WebAudioCapturerSource::consumeAudio(
79 const blink::WebVector<const float*>& audio_data,
80 size_t number_of_frames) {
81 base::AutoLock auto_lock(lock_);
82 if (!track_)
83 return;
85 // Update the downstream client if the audio format has been changed.
86 if (audio_format_changed_) {
87 track_->OnSetFormat(params_);
88 audio_format_changed_ = false;
91 wrapper_bus_->set_frames(number_of_frames);
93 // Make sure WebKit is honoring what it told us up front
94 // about the channels.
95 DCHECK_EQ(params_.channels(), static_cast<int>(audio_data.size()));
97 for (size_t i = 0; i < audio_data.size(); ++i)
98 wrapper_bus_->SetChannelData(i, const_cast<float*>(audio_data[i]));
100 // Handle mismatch between WebAudio buffer-size and WebRTC.
101 int available = fifo_->max_frames() - fifo_->frames();
102 if (available < static_cast<int>(number_of_frames)) {
103 NOTREACHED() << "WebAudioCapturerSource::Consume() : FIFO overrun.";
104 return;
107 // Compute the estimated capture time of the first sample frame of audio that
108 // will be consumed from the FIFO in the loop below.
109 base::TimeTicks estimated_capture_time = base::TimeTicks::Now() -
110 fifo_->frames() * base::TimeDelta::FromSeconds(1) / params_.sample_rate();
112 fifo_->Push(wrapper_bus_.get());
113 while (fifo_->frames() >= capture_bus_->frames()) {
114 fifo_->Consume(capture_bus_.get(), 0, capture_bus_->frames());
115 track_->Capture(*capture_bus_, estimated_capture_time, false);
117 // Advance the estimated capture time for the next FIFO consume operation.
118 estimated_capture_time +=
119 capture_bus_->frames() * base::TimeDelta::FromSeconds(1) /
120 params_.sample_rate();
124 // If registered as audio consumer in |blink_source_|, deregister from
125 // |blink_source_| and stop keeping a reference to |blink_source_|.
126 // Failure to call this method when stopping the track might leave an invalid
127 // WebAudioCapturerSource reference still registered as an audio consumer on
128 // the blink side.
129 void WebAudioCapturerSource::removeFromBlinkSource() {
130 if (!blink_source_.isNull()) {
131 blink_source_.removeAudioConsumer(this);
132 blink_source_.reset();
136 } // namespace content