| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/blink/webaudiosourceprovider_impl.h" | 5 #include "media/blink/webaudiosourceprovider_impl.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 client_(nullptr), | 98 client_(nullptr), |
| 99 sink_(sink), | 99 sink_(sink), |
| 100 tee_filter_(new TeeFilter()), | 100 tee_filter_(new TeeFilter()), |
| 101 weak_factory_(this) {} | 101 weak_factory_(this) {} |
| 102 | 102 |
| 103 WebAudioSourceProviderImpl::~WebAudioSourceProviderImpl() { | 103 WebAudioSourceProviderImpl::~WebAudioSourceProviderImpl() { |
| 104 } | 104 } |
| 105 | 105 |
| 106 void WebAudioSourceProviderImpl::setClient( | 106 void WebAudioSourceProviderImpl::setClient( |
| 107 blink::WebAudioSourceProviderClient* client) { | 107 blink::WebAudioSourceProviderClient* client) { |
| 108 // Skip taking the lock if unnecessary. This function is the only setter for |
| 109 // |client_| so it's safe to check |client_| outside of the lock. |
| 110 if (client_ == client) |
| 111 return; |
| 112 |
| 108 base::AutoLock auto_lock(sink_lock_); | 113 base::AutoLock auto_lock(sink_lock_); |
| 109 if (client && client != client_) { | 114 if (client) { |
| 110 // Detach the audio renderer from normal playback. | 115 // Detach the audio renderer from normal playback. |
| 111 sink_->Stop(); | 116 sink_->Stop(); |
| 112 | 117 |
| 113 // The client will now take control by calling provideInput() periodically. | 118 // The client will now take control by calling provideInput() periodically. |
| 114 client_ = client; | 119 client_ = client; |
| 115 | 120 |
| 116 set_format_cb_ = BindToCurrentLoop(base::Bind( | 121 set_format_cb_ = BindToCurrentLoop(base::Bind( |
| 117 &WebAudioSourceProviderImpl::OnSetFormat, weak_factory_.GetWeakPtr())); | 122 &WebAudioSourceProviderImpl::OnSetFormat, weak_factory_.GetWeakPtr())); |
| 118 | 123 |
| 119 // If |tee_filter_| is Initialize()d - then run |set_format_cb_| to send | 124 // If |tee_filter_| is Initialize()d - then run |set_format_cb_| to send |
| 120 // |client_| the current format info. Otherwise |set_format_cb_| will get | 125 // |client_| the current format info. Otherwise |set_format_cb_| will get |
| 121 // called when Initialize() is called. Note: Always using |set_format_cb_| | 126 // called when Initialize() is called. Note: Always using |set_format_cb_| |
| 122 // ensures we have the same locking order when calling into |client_|. | 127 // ensures we have the same locking order when calling into |client_|. |
| 123 if (tee_filter_->IsInitialized()) | 128 if (tee_filter_->IsInitialized()) |
| 124 base::ResetAndReturn(&set_format_cb_).Run(); | 129 base::ResetAndReturn(&set_format_cb_).Run(); |
| 125 } else if (!client && client_) { | 130 return; |
| 126 // Restore normal playback. | |
| 127 client_ = nullptr; | |
| 128 sink_->SetVolume(volume_); | |
| 129 if (state_ >= kStarted) | |
| 130 sink_->Start(); | |
| 131 if (state_ >= kPlaying) | |
| 132 sink_->Play(); | |
| 133 } | 131 } |
| 132 |
| 133 // Restore normal playback. |
| 134 client_ = nullptr; |
| 135 sink_->SetVolume(volume_); |
| 136 if (state_ >= kStarted) |
| 137 sink_->Start(); |
| 138 if (state_ >= kPlaying) |
| 139 sink_->Play(); |
| 134 } | 140 } |
| 135 | 141 |
| 136 void WebAudioSourceProviderImpl::provideInput( | 142 void WebAudioSourceProviderImpl::provideInput( |
| 137 const WebVector<float*>& audio_data, size_t number_of_frames) { | 143 const WebVector<float*>& audio_data, size_t number_of_frames) { |
| 138 if (!bus_wrapper_ || | 144 if (!bus_wrapper_ || |
| 139 static_cast<size_t>(bus_wrapper_->channels()) != audio_data.size()) { | 145 static_cast<size_t>(bus_wrapper_->channels()) != audio_data.size()) { |
| 140 bus_wrapper_ = AudioBus::CreateWrapper(static_cast<int>(audio_data.size())); | 146 bus_wrapper_ = AudioBus::CreateWrapper(static_cast<int>(audio_data.size())); |
| 141 } | 147 } |
| 142 | 148 |
| 143 const int incoming_number_of_frames = static_cast<int>(number_of_frames); | 149 const int incoming_number_of_frames = static_cast<int>(number_of_frames); |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 | 290 |
| 285 return num_rendered_frames; | 291 return num_rendered_frames; |
| 286 } | 292 } |
| 287 | 293 |
| 288 void WebAudioSourceProviderImpl::TeeFilter::OnRenderError() { | 294 void WebAudioSourceProviderImpl::TeeFilter::OnRenderError() { |
| 289 DCHECK(IsInitialized()); | 295 DCHECK(IsInitialized()); |
| 290 renderer_->OnRenderError(); | 296 renderer_->OnRenderError(); |
| 291 } | 297 } |
| 292 | 298 |
| 293 } // namespace media | 299 } // namespace media |
| OLD | NEW |