Add ICU message format support
[chromium-blink-merge.git] / content / renderer / media / webaudio_capturer_source.cc
blobcf65d2f487af5ddd89679f5cf8de23c6ded96a92
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());
74 base::AutoLock auto_lock(lock_);
75 track_ = NULL;
77 // removeFromBlinkSource() should not be called while |lock_| is acquired,
78 // as it could result in a deadlock.
79 removeFromBlinkSource();
82 void WebAudioCapturerSource::consumeAudio(
83 const blink::WebVector<const float*>& audio_data,
84 size_t number_of_frames) {
85 base::AutoLock auto_lock(lock_);
86 if (!track_)
87 return;
89 // Update the downstream client if the audio format has been changed.
90 if (audio_format_changed_) {
91 track_->OnSetFormat(params_);
92 audio_format_changed_ = false;
95 wrapper_bus_->set_frames(number_of_frames);
97 // Make sure WebKit is honoring what it told us up front
98 // about the channels.
99 DCHECK_EQ(params_.channels(), static_cast<int>(audio_data.size()));
101 for (size_t i = 0; i < audio_data.size(); ++i)
102 wrapper_bus_->SetChannelData(i, const_cast<float*>(audio_data[i]));
104 // Handle mismatch between WebAudio buffer-size and WebRTC.
105 int available = fifo_->max_frames() - fifo_->frames();
106 if (available < static_cast<int>(number_of_frames)) {
107 NOTREACHED() << "WebAudioCapturerSource::Consume() : FIFO overrun.";
108 return;
111 // Compute the estimated capture time of the first sample frame of audio that
112 // will be consumed from the FIFO in the loop below.
113 base::TimeTicks estimated_capture_time = base::TimeTicks::Now() -
114 fifo_->frames() * base::TimeDelta::FromSeconds(1) / params_.sample_rate();
116 fifo_->Push(wrapper_bus_.get());
117 while (fifo_->frames() >= capture_bus_->frames()) {
118 fifo_->Consume(capture_bus_.get(), 0, capture_bus_->frames());
119 track_->Capture(*capture_bus_, estimated_capture_time, false);
121 // Advance the estimated capture time for the next FIFO consume operation.
122 estimated_capture_time +=
123 capture_bus_->frames() * base::TimeDelta::FromSeconds(1) /
124 params_.sample_rate();
128 // If registered as audio consumer in |blink_source_|, deregister from
129 // |blink_source_| and stop keeping a reference to |blink_source_|.
130 // Failure to call this method when stopping the track might leave an invalid
131 // WebAudioCapturerSource reference still registered as an audio consumer on
132 // the blink side.
133 void WebAudioCapturerSource::removeFromBlinkSource() {
134 if (!blink_source_.isNull()) {
135 blink_source_.removeAudioConsumer(this);
136 blink_source_.reset();
140 } // namespace content