Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / renderer / media / webrtc_local_audio_source_provider.cc
bloba84151b10d420ff79d6672ae0451411cc6d24b58
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/platform/WebAudioSourceProviderClient.h"
14 using blink::WebVector;
16 namespace content {
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.
22 // static
23 const size_t WebRtcLocalAudioSourceProvider::kWebAudioRenderBufferSize = 128;
25 WebRtcLocalAudioSourceProvider::WebRtcLocalAudioSourceProvider(
26 const blink::WebMediaStreamTrack& track)
27 : is_enabled_(false),
28 track_(track),
29 track_stopped_(false) {
30 // Get the native audio output hardware sample-rate for the sink.
31 // We need to check if RenderThreadImpl is valid here since the unittests
32 // do not have one and they will inject their own |sink_params_| for testing.
33 if (RenderThreadImpl::current()) {
34 media::AudioHardwareConfig* hardware_config =
35 RenderThreadImpl::current()->GetAudioHardwareConfig();
36 int sample_rate = hardware_config->GetOutputSampleRate();
37 sink_params_.Reset(
38 media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
39 media::CHANNEL_LAYOUT_STEREO, 2, sample_rate, 16,
40 kWebAudioRenderBufferSize);
43 // Connect the source provider to the track as a sink.
44 MediaStreamAudioSink::AddToAudioTrack(this, track_);
47 WebRtcLocalAudioSourceProvider::~WebRtcLocalAudioSourceProvider() {
48 if (audio_converter_.get())
49 audio_converter_->RemoveInput(this);
51 // If the track is still active, it is necessary to notify the track before
52 // the source provider goes away.
53 if (!track_stopped_)
54 MediaStreamAudioSink::RemoveFromAudioTrack(this, track_);
57 void WebRtcLocalAudioSourceProvider::OnSetFormat(
58 const media::AudioParameters& params) {
59 // We need detach the thread here because it will be a new capture thread
60 // calling OnSetFormat() and OnData() if the source is restarted.
61 capture_thread_checker_.DetachFromThread();
62 DCHECK(capture_thread_checker_.CalledOnValidThread());
63 DCHECK(params.IsValid());
64 DCHECK(sink_params_.IsValid());
66 base::AutoLock auto_lock(lock_);
67 source_params_ = params;
68 // Create the audio converter with |disable_fifo| as false so that the
69 // converter will request source_params.frames_per_buffer() each time.
70 // This will not increase the complexity as there is only one client to
71 // the converter.
72 audio_converter_.reset(
73 new media::AudioConverter(params, sink_params_, false));
74 audio_converter_->AddInput(this);
75 fifo_.reset(new media::AudioFifo(
76 params.channels(),
77 kMaxNumberOfBuffers * params.frames_per_buffer()));
80 void WebRtcLocalAudioSourceProvider::OnReadyStateChanged(
81 blink::WebMediaStreamSource::ReadyState state) {
82 if (state == blink::WebMediaStreamSource::ReadyStateEnded)
83 track_stopped_ = true;
86 void WebRtcLocalAudioSourceProvider::OnData(
87 const media::AudioBus& audio_bus,
88 base::TimeTicks estimated_capture_time) {
89 DCHECK(capture_thread_checker_.CalledOnValidThread());
90 DCHECK_EQ(audio_bus.channels(), source_params_.channels());
91 DCHECK_EQ(audio_bus.frames(), source_params_.frames_per_buffer());
92 DCHECK(!estimated_capture_time.is_null());
94 base::AutoLock auto_lock(lock_);
95 if (!is_enabled_)
96 return;
98 DCHECK(fifo_.get());
100 if (fifo_->frames() + audio_bus.frames() <= fifo_->max_frames()) {
101 fifo_->Push(&audio_bus);
102 } else {
103 // This can happen if the data in FIFO is too slowly consumed or
104 // WebAudio stops consuming data.
105 DVLOG(3) << "Local source provicer FIFO is full" << fifo_->frames();
109 void WebRtcLocalAudioSourceProvider::setClient(
110 blink::WebAudioSourceProviderClient* client) {
111 NOTREACHED();
114 void WebRtcLocalAudioSourceProvider::provideInput(
115 const WebVector<float*>& audio_data, size_t number_of_frames) {
116 DCHECK_EQ(number_of_frames, kWebAudioRenderBufferSize);
117 if (!output_wrapper_ ||
118 static_cast<size_t>(output_wrapper_->channels()) != audio_data.size()) {
119 output_wrapper_ = media::AudioBus::CreateWrapper(audio_data.size());
122 output_wrapper_->set_frames(number_of_frames);
123 for (size_t i = 0; i < audio_data.size(); ++i)
124 output_wrapper_->SetChannelData(i, audio_data[i]);
126 base::AutoLock auto_lock(lock_);
127 if (!audio_converter_)
128 return;
130 is_enabled_ = true;
131 audio_converter_->Convert(output_wrapper_.get());
134 double WebRtcLocalAudioSourceProvider::ProvideInput(
135 media::AudioBus* audio_bus, base::TimeDelta buffer_delay) {
136 if (fifo_->frames() >= audio_bus->frames()) {
137 fifo_->Consume(audio_bus, 0, audio_bus->frames());
138 } else {
139 audio_bus->Zero();
140 DVLOG(1) << "WARNING: Underrun, FIFO has data " << fifo_->frames()
141 << " samples but " << audio_bus->frames()
142 << " samples are needed";
145 return 1.0;
148 void WebRtcLocalAudioSourceProvider::SetSinkParamsForTesting(
149 const media::AudioParameters& sink_params) {
150 sink_params_ = sink_params;
153 } // namespace content