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/webaudiosourceprovider_impl.h"
9 #include "base/logging.h"
10 #include "third_party/WebKit/public/web/WebAudioSourceProviderClient.h"
12 using WebKit::WebVector;
18 // Simple helper class for Try() locks. Lock is Try()'d on construction and
19 // must be checked via the locked() attribute. If acquisition was successful
20 // the lock will be released upon destruction.
21 // TODO(dalecurtis): This should probably move to base/ if others start using
25 explicit AutoTryLock(base::Lock& lock)
27 acquired_(lock_.Try()) {}
29 bool locked() const { return acquired_; }
33 lock_.AssertAcquired();
41 DISALLOW_COPY_AND_ASSIGN(AutoTryLock);
46 WebAudioSourceProviderImpl::WebAudioSourceProviderImpl(
47 const scoped_refptr<media::AudioRendererSink>& sink)
57 WebAudioSourceProviderImpl::~WebAudioSourceProviderImpl() {}
59 void WebAudioSourceProviderImpl::setClient(
60 WebKit::WebAudioSourceProviderClient* client) {
61 base::AutoLock auto_lock(sink_lock_);
62 if (client && client != client_) {
63 // Detach the audio renderer from normal playback.
66 // The client will now take control by calling provideInput() periodically.
70 // The client needs to be notified of the audio format, if available.
71 // If the format is not yet available, we'll be notified later
72 // when Initialize() is called.
74 // Inform WebKit about the audio stream format.
75 client->setFormat(channels_, sample_rate_);
77 } else if (!client && client_) {
78 // Restore normal playback.
80 sink_->SetVolume(volume_);
81 if (state_ >= kStarted)
83 if (state_ >= kPlaying)
88 void WebAudioSourceProviderImpl::provideInput(
89 const WebVector<float*>& audio_data, size_t number_of_frames) {
91 static_cast<size_t>(bus_wrapper_->channels()) != audio_data.size()) {
92 bus_wrapper_ = media::AudioBus::CreateWrapper(audio_data.size());
95 bus_wrapper_->set_frames(number_of_frames);
96 for (size_t i = 0; i < audio_data.size(); ++i)
97 bus_wrapper_->SetChannelData(i, audio_data[i]);
99 // Use a try lock to avoid contention in the real-time audio thread.
100 AutoTryLock auto_try_lock(sink_lock_);
101 if (!auto_try_lock.locked() || state_ != kPlaying) {
102 // Provide silence if we failed to acquire the lock or the source is not
104 bus_wrapper_->Zero();
110 DCHECK_EQ(channels_, bus_wrapper_->channels());
111 renderer_->Render(bus_wrapper_.get(), 0);
112 bus_wrapper_->Scale(volume_);
115 void WebAudioSourceProviderImpl::Start() {
116 base::AutoLock auto_lock(sink_lock_);
117 DCHECK_EQ(state_, kStopped);
123 void WebAudioSourceProviderImpl::Stop() {
124 base::AutoLock auto_lock(sink_lock_);
130 void WebAudioSourceProviderImpl::Play() {
131 base::AutoLock auto_lock(sink_lock_);
132 DCHECK_EQ(state_, kStarted);
138 void WebAudioSourceProviderImpl::Pause() {
139 base::AutoLock auto_lock(sink_lock_);
140 DCHECK(state_ == kPlaying || state_ == kStarted);
146 bool WebAudioSourceProviderImpl::SetVolume(double volume) {
147 base::AutoLock auto_lock(sink_lock_);
150 sink_->SetVolume(volume);
154 void WebAudioSourceProviderImpl::Initialize(
155 const media::AudioParameters& params,
156 RenderCallback* renderer) {
157 base::AutoLock auto_lock(sink_lock_);
159 renderer_ = renderer;
161 DCHECK_EQ(state_, kStopped);
162 sink_->Initialize(params, renderer);
164 // Keep track of the format in case the client hasn't yet been set.
165 channels_ = params.channels();
166 sample_rate_ = params.sample_rate();
169 // Inform WebKit about the audio stream format.
170 client_->setFormat(channels_, sample_rate_);
174 } // namespace content