1 // Copyright 2013 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/webrtc_local_audio_source_provider.h"
7 #include "base/logging.h"
8 #include "content/renderer/render_thread_impl.h"
9 #include "media/audio/audio_parameters.h"
10 #include "media/base/audio_fifo.h"
11 #include "media/base/audio_hardware_config.h"
12 #include "third_party/WebKit/public/web/WebAudioSourceProviderClient.h"
14 using WebKit::WebVector;
18 static const size_t kMaxNumberOfBuffers = 10;
20 // Size of the buffer that WebAudio processes each time, it is the same value
21 // as AudioNode::ProcessingSizeInFrames in WebKit.
23 const size_t WebRtcLocalAudioSourceProvider::kWebAudioRenderBufferSize = 128;
25 WebRtcLocalAudioSourceProvider::WebRtcLocalAudioSourceProvider()
32 WebRtcLocalAudioSourceProvider::~WebRtcLocalAudioSourceProvider() {
33 if (audio_converter_.get())
34 audio_converter_->RemoveInput(this);
37 void WebRtcLocalAudioSourceProvider::Initialize(
38 const media::AudioParameters& source_params) {
39 DCHECK(thread_checker_.CalledOnValidThread());
41 // Use the native audio output hardware sample-rate for the sink.
42 if (RenderThreadImpl::current()) {
43 media::AudioHardwareConfig* hardware_config =
44 RenderThreadImpl::current()->GetAudioHardwareConfig();
45 int sample_rate = hardware_config->GetOutputSampleRate();
47 source_params.format(), media::CHANNEL_LAYOUT_STEREO, 2, 0,
48 sample_rate, source_params.bits_per_sample(),
49 kWebAudioRenderBufferSize);
51 // This happens on unittests which does not have a valid RenderThreadImpl,
52 // the unittests should have injected their own |sink_params_| for testing.
53 DCHECK(sink_params_.IsValid());
56 base::AutoLock auto_lock(lock_);
57 source_params_ = source_params;
58 // Create the audio converter with |disable_fifo| as false so that the
59 // converter will request source_params.frames_per_buffer() each time.
60 // This will not increase the complexity as there is only one client to
62 audio_converter_.reset(
63 new media::AudioConverter(source_params, sink_params_, false));
64 audio_converter_->AddInput(this);
65 fifo_.reset(new media::AudioFifo(
66 source_params.channels(),
67 kMaxNumberOfBuffers * source_params.frames_per_buffer()));
70 void WebRtcLocalAudioSourceProvider::DeliverData(
71 media::AudioBus* audio_source,
72 int audio_delay_milliseconds,
75 base::AutoLock auto_lock(lock_);
81 if (fifo_->frames() + audio_source->frames() <= fifo_->max_frames()) {
82 fifo_->Push(audio_source);
84 // This can happen if the data in FIFO is too slowed to be consumed or
85 // WebAudio stops consuming data.
86 DLOG(WARNING) << "Local source provicer FIFO is full" << fifo_->frames();
89 // Cache the values for GetAudioProcessingParams().
90 last_fill_ = base::TimeTicks::Now();
91 audio_delay_ms_ = audio_delay_milliseconds;
93 key_pressed_ = key_pressed;
96 void WebRtcLocalAudioSourceProvider::GetAudioProcessingParams(
97 int* delay_ms, int* volume, bool* key_pressed) {
99 if (!last_fill_.is_null()) {
100 elapsed_ms = static_cast<int>(
101 (base::TimeTicks::Now() - last_fill_).InMilliseconds());
103 *delay_ms = audio_delay_ms_ + elapsed_ms + static_cast<int>(
104 1000 * fifo_->frames() / source_params_.sample_rate() + 0.5);
106 *key_pressed = key_pressed_;
109 void WebRtcLocalAudioSourceProvider::setClient(
110 WebKit::WebAudioSourceProviderClient* client) {
114 void WebRtcLocalAudioSourceProvider::provideInput(
115 const WebVector<float*>& audio_data, size_t number_of_frames) {
116 DCHECK_EQ(number_of_frames, kWebAudioRenderBufferSize);
118 static_cast<size_t>(bus_wrapper_->channels()) != audio_data.size()) {
119 bus_wrapper_ = media::AudioBus::CreateWrapper(audio_data.size());
122 bus_wrapper_->set_frames(number_of_frames);
123 for (size_t i = 0; i < audio_data.size(); ++i)
124 bus_wrapper_->SetChannelData(i, audio_data[i]);
126 base::AutoLock auto_lock(lock_);
127 DCHECK(audio_converter_.get());
130 audio_converter_->Convert(bus_wrapper_.get());
133 double WebRtcLocalAudioSourceProvider::ProvideInput(
134 media::AudioBus* audio_bus, base::TimeDelta buffer_delay) {
135 if (fifo_->frames() >= audio_bus->frames()) {
136 fifo_->Consume(audio_bus, 0, audio_bus->frames());
139 if (!last_fill_.is_null()) {
140 DLOG(WARNING) << "Underrun, FIFO has data " << fifo_->frames()
141 << " samples but " << audio_bus->frames()
142 << " samples are needed";
149 void WebRtcLocalAudioSourceProvider::SetSinkParamsForTesting(
150 const media::AudioParameters& sink_params) {
151 DCHECK(thread_checker_.CalledOnValidThread());
152 sink_params_ = sink_params;
155 } // namespace content