Fix crash on app list start page contents not existing.
[chromium-blink-merge.git] / content / renderer / media / webaudio_capturer_source.cc
blob3447cc9519965b83b9f5a2ec45634454273038e0
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 : track_(NULL),
24 audio_format_changed_(false) {
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.";
38 return;
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
47 // running on.
48 params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
49 channel_layout, number_of_channels, sample_rate, 16,
50 sample_rate / 100);
51 audio_format_changed_ = true;
53 wrapper_bus_ = AudioBus::CreateWrapper(params_.channels());
54 capture_bus_ = AudioBus::Create(params_);
55 fifo_.reset(new AudioFifo(
56 params_.channels(),
57 kMaxNumberOfBuffersInFifo * params_.frames_per_buffer()));
60 void WebAudioCapturerSource::Start(WebRtcLocalAudioTrack* track) {
61 DCHECK(thread_checker_.CalledOnValidThread());
62 DCHECK(track);
63 base::AutoLock auto_lock(lock_);
64 track_ = track;
67 void WebAudioCapturerSource::Stop() {
68 DCHECK(thread_checker_.CalledOnValidThread());
69 base::AutoLock auto_lock(lock_);
70 track_ = NULL;
73 void WebAudioCapturerSource::consumeAudio(
74 const blink::WebVector<const float*>& audio_data,
75 size_t number_of_frames) {
76 base::AutoLock auto_lock(lock_);
77 if (!track_)
78 return;
80 // Update the downstream client if the audio format has been changed.
81 if (audio_format_changed_) {
82 track_->OnSetFormat(params_);
83 audio_format_changed_ = false;
86 wrapper_bus_->set_frames(number_of_frames);
88 // Make sure WebKit is honoring what it told us up front
89 // about the channels.
90 DCHECK_EQ(params_.channels(), static_cast<int>(audio_data.size()));
92 for (size_t i = 0; i < audio_data.size(); ++i)
93 wrapper_bus_->SetChannelData(i, const_cast<float*>(audio_data[i]));
95 // Handle mismatch between WebAudio buffer-size and WebRTC.
96 int available = fifo_->max_frames() - fifo_->frames();
97 if (available < static_cast<int>(number_of_frames)) {
98 NOTREACHED() << "WebAudioCapturerSource::Consume() : FIFO overrun.";
99 return;
102 // Compute the estimated capture time of the first sample frame of audio that
103 // will be consumed from the FIFO in the loop below.
104 base::TimeTicks estimated_capture_time = base::TimeTicks::Now() -
105 fifo_->frames() * base::TimeDelta::FromSeconds(1) / params_.sample_rate();
107 fifo_->Push(wrapper_bus_.get());
108 while (fifo_->frames() >= capture_bus_->frames()) {
109 fifo_->Consume(capture_bus_.get(), 0, capture_bus_->frames());
110 track_->Capture(*capture_bus_, estimated_capture_time, false);
112 // Advance the estimated capture time for the next FIFO consume operation.
113 estimated_capture_time +=
114 capture_bus_->frames() * base::TimeDelta::FromSeconds(1) /
115 params_.sample_rate();
119 } // namespace content