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 "media/blink/webaudiosourceprovider_impl.h"
10 #include "base/callback_helpers.h"
11 #include "base/logging.h"
12 #include "media/base/bind_to_current_loop.h"
13 #include "third_party/WebKit/public/platform/WebAudioSourceProviderClient.h"
15 using blink::WebVector
;
21 // Simple helper class for Try() locks. Lock is Try()'d on construction and
22 // must be checked via the locked() attribute. If acquisition was successful
23 // the lock will be released upon destruction.
24 // TODO(dalecurtis): This should probably move to base/ if others start using
28 explicit AutoTryLock(base::Lock
& lock
)
30 acquired_(lock_
.Try()) {}
32 bool locked() const { return acquired_
; }
36 lock_
.AssertAcquired();
44 DISALLOW_COPY_AND_ASSIGN(AutoTryLock
);
49 WebAudioSourceProviderImpl::WebAudioSourceProviderImpl(
50 const scoped_refptr
<AudioRendererSink
>& sink
)
58 weak_factory_(this) {}
60 WebAudioSourceProviderImpl::~WebAudioSourceProviderImpl() {
63 void WebAudioSourceProviderImpl::setClient(
64 blink::WebAudioSourceProviderClient
* client
) {
65 base::AutoLock
auto_lock(sink_lock_
);
66 if (client
&& client
!= client_
) {
67 // Detach the audio renderer from normal playback.
70 // The client will now take control by calling provideInput() periodically.
73 set_format_cb_
= BindToCurrentLoop(base::Bind(
74 &WebAudioSourceProviderImpl::OnSetFormat
, weak_factory_
.GetWeakPtr()));
76 // If |renderer_| is set, then run |set_format_cb_| to send |client_|
77 // the current format info. If |renderer_| is not set, then |set_format_cb_|
78 // will get called when Initialize() is called.
79 // Note: Always using |set_format_cb_| ensures we have the same
80 // locking order when calling into |client_|.
82 base::ResetAndReturn(&set_format_cb_
).Run();
83 } else if (!client
&& client_
) {
84 // Restore normal playback.
86 sink_
->SetVolume(volume_
);
87 if (state_
>= kStarted
)
89 if (state_
>= kPlaying
)
94 void WebAudioSourceProviderImpl::provideInput(
95 const WebVector
<float*>& audio_data
, size_t number_of_frames
) {
97 static_cast<size_t>(bus_wrapper_
->channels()) != audio_data
.size()) {
98 bus_wrapper_
= AudioBus::CreateWrapper(static_cast<int>(audio_data
.size()));
101 bus_wrapper_
->set_frames(static_cast<int>(number_of_frames
));
102 for (size_t i
= 0; i
< audio_data
.size(); ++i
)
103 bus_wrapper_
->SetChannelData(static_cast<int>(i
), audio_data
[i
]);
105 // Use a try lock to avoid contention in the real-time audio thread.
106 AutoTryLock
auto_try_lock(sink_lock_
);
107 if (!auto_try_lock
.locked() || state_
!= kPlaying
) {
108 // Provide silence if we failed to acquire the lock or the source is not
110 bus_wrapper_
->Zero();
116 DCHECK_EQ(channels_
, bus_wrapper_
->channels());
117 const int frames
= renderer_
->Render(bus_wrapper_
.get(), 0);
118 if (frames
< static_cast<int>(number_of_frames
)) {
119 bus_wrapper_
->ZeroFramesPartial(
121 static_cast<int>(number_of_frames
- frames
));
124 bus_wrapper_
->Scale(volume_
);
127 void WebAudioSourceProviderImpl::Start() {
128 base::AutoLock
auto_lock(sink_lock_
);
129 DCHECK_EQ(state_
, kStopped
);
135 void WebAudioSourceProviderImpl::Stop() {
136 base::AutoLock
auto_lock(sink_lock_
);
142 void WebAudioSourceProviderImpl::Play() {
143 base::AutoLock
auto_lock(sink_lock_
);
144 DCHECK_EQ(state_
, kStarted
);
150 void WebAudioSourceProviderImpl::Pause() {
151 base::AutoLock
auto_lock(sink_lock_
);
152 DCHECK(state_
== kPlaying
|| state_
== kStarted
);
158 bool WebAudioSourceProviderImpl::SetVolume(double volume
) {
159 base::AutoLock
auto_lock(sink_lock_
);
162 sink_
->SetVolume(volume
);
166 void WebAudioSourceProviderImpl::SwitchOutputDevice(
167 const std::string
& device_id
,
168 const GURL
& security_origin
,
169 const SwitchOutputDeviceCB
& callback
) {
170 base::AutoLock
auto_lock(sink_lock_
);
172 sink_
->SwitchOutputDevice(device_id
, security_origin
, callback
);
174 callback
.Run(SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_SUPPORTED
);
178 void WebAudioSourceProviderImpl::Initialize(
179 const AudioParameters
& params
,
180 RenderCallback
* renderer
) {
181 base::AutoLock
auto_lock(sink_lock_
);
183 renderer_
= renderer
;
185 DCHECK_EQ(state_
, kStopped
);
186 sink_
->Initialize(params
, renderer
);
188 // Keep track of the format in case the client hasn't yet been set.
189 channels_
= params
.channels();
190 sample_rate_
= params
.sample_rate();
192 if (!set_format_cb_
.is_null())
193 base::ResetAndReturn(&set_format_cb_
).Run();
196 void WebAudioSourceProviderImpl::OnSetFormat() {
197 base::AutoLock
auto_lock(sink_lock_
);
201 // Inform Blink about the audio stream format.
202 client_
->setFormat(channels_
, sample_rate_
);