| 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" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "media/base/audio_timestamp_helper.h" | |
| 15 #include "media/base/bind_to_current_loop.h" | 14 #include "media/base/bind_to_current_loop.h" |
| 16 #include "third_party/WebKit/public/platform/WebAudioSourceProviderClient.h" | 15 #include "third_party/WebKit/public/platform/WebAudioSourceProviderClient.h" |
| 17 | 16 |
| 18 using blink::WebVector; | 17 using blink::WebVector; |
| 19 | 18 |
| 20 namespace media { | 19 namespace media { |
| 21 | 20 |
| 22 namespace { | 21 namespace { |
| 23 | 22 |
| 24 // Simple helper class for Try() locks. Lock is Try()'d on construction and | 23 // Simple helper class for Try() locks. Lock is Try()'d on construction and |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 int sample_rate) { | 62 int sample_rate) { |
| 64 DCHECK(renderer); | 63 DCHECK(renderer); |
| 65 renderer_ = renderer; | 64 renderer_ = renderer; |
| 66 channels_ = channels; | 65 channels_ = channels; |
| 67 sample_rate_ = sample_rate; | 66 sample_rate_ = sample_rate; |
| 68 } | 67 } |
| 69 | 68 |
| 70 // AudioRendererSink::RenderCallback implementation. | 69 // AudioRendererSink::RenderCallback implementation. |
| 71 // These are forwarders to |renderer_| and are here to allow for a client to | 70 // These are forwarders to |renderer_| and are here to allow for a client to |
| 72 // get a copy of the rendered audio by SetCopyAudioCallback(). | 71 // get a copy of the rendered audio by SetCopyAudioCallback(). |
| 73 int Render(base::TimeDelta delay, | 72 int Render(AudioBus* audio_bus, |
| 74 base::TimeTicks delay_timestamp, | 73 uint32_t frames_delayed, |
| 75 int prior_frames_skipped, | 74 uint32_t frames_skipped) override; |
| 76 AudioBus* dest) override; | |
| 77 void OnRenderError() override; | 75 void OnRenderError() override; |
| 78 | 76 |
| 79 bool IsInitialized() const { return !!renderer_; } | 77 bool IsInitialized() const { return !!renderer_; } |
| 80 int channels() const { return channels_; } | 78 int channels() const { return channels_; } |
| 81 int sample_rate() const { return sample_rate_; } | 79 int sample_rate() const { return sample_rate_; } |
| 82 void set_copy_audio_bus_callback(const CopyAudioCB& callback) { | 80 void set_copy_audio_bus_callback(const CopyAudioCB& callback) { |
| 83 copy_audio_bus_callback_ = callback; | 81 copy_audio_bus_callback_ = callback; |
| 84 } | 82 } |
| 85 | 83 |
| 86 private: | 84 private: |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 AutoTryLock auto_try_lock(sink_lock_); | 149 AutoTryLock auto_try_lock(sink_lock_); |
| 152 if (!auto_try_lock.locked() || state_ != kPlaying) { | 150 if (!auto_try_lock.locked() || state_ != kPlaying) { |
| 153 // Provide silence if we failed to acquire the lock or the source is not | 151 // Provide silence if we failed to acquire the lock or the source is not |
| 154 // running. | 152 // running. |
| 155 bus_wrapper_->Zero(); | 153 bus_wrapper_->Zero(); |
| 156 return; | 154 return; |
| 157 } | 155 } |
| 158 | 156 |
| 159 DCHECK(client_); | 157 DCHECK(client_); |
| 160 DCHECK_EQ(tee_filter_->channels(), bus_wrapper_->channels()); | 158 DCHECK_EQ(tee_filter_->channels(), bus_wrapper_->channels()); |
| 161 const int frames = tee_filter_->Render( | 159 const int frames = tee_filter_->Render(bus_wrapper_.get(), 0, 0); |
| 162 base::TimeDelta(), base::TimeTicks::Now(), 0, bus_wrapper_.get()); | |
| 163 if (frames < incoming_number_of_frames) | 160 if (frames < incoming_number_of_frames) |
| 164 bus_wrapper_->ZeroFramesPartial(frames, incoming_number_of_frames - frames); | 161 bus_wrapper_->ZeroFramesPartial(frames, incoming_number_of_frames - frames); |
| 165 | 162 |
| 166 bus_wrapper_->Scale(volume_); | 163 bus_wrapper_->Scale(volume_); |
| 167 } | 164 } |
| 168 | 165 |
| 169 void WebAudioSourceProviderImpl::Initialize(const AudioParameters& params, | 166 void WebAudioSourceProviderImpl::Initialize(const AudioParameters& params, |
| 170 RenderCallback* renderer) { | 167 RenderCallback* renderer) { |
| 171 base::AutoLock auto_lock(sink_lock_); | 168 base::AutoLock auto_lock(sink_lock_); |
| 172 DCHECK_EQ(state_, kStopped); | 169 DCHECK_EQ(state_, kStopped); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 DCHECK(tee_filter_); | 247 DCHECK(tee_filter_); |
| 251 tee_filter_->set_copy_audio_bus_callback(callback); | 248 tee_filter_->set_copy_audio_bus_callback(callback); |
| 252 } | 249 } |
| 253 | 250 |
| 254 void WebAudioSourceProviderImpl::ClearCopyAudioCallback() { | 251 void WebAudioSourceProviderImpl::ClearCopyAudioCallback() { |
| 255 DCHECK(tee_filter_); | 252 DCHECK(tee_filter_); |
| 256 tee_filter_->set_copy_audio_bus_callback(CopyAudioCB()); | 253 tee_filter_->set_copy_audio_bus_callback(CopyAudioCB()); |
| 257 } | 254 } |
| 258 | 255 |
| 259 int WebAudioSourceProviderImpl::RenderForTesting(AudioBus* audio_bus) { | 256 int WebAudioSourceProviderImpl::RenderForTesting(AudioBus* audio_bus) { |
| 260 return tee_filter_->Render(base::TimeDelta(), base::TimeTicks::Now(), 0, | 257 return tee_filter_->Render(audio_bus, 0, 0); |
| 261 audio_bus); | |
| 262 } | 258 } |
| 263 | 259 |
| 264 void WebAudioSourceProviderImpl::OnSetFormat() { | 260 void WebAudioSourceProviderImpl::OnSetFormat() { |
| 265 base::AutoLock auto_lock(sink_lock_); | 261 base::AutoLock auto_lock(sink_lock_); |
| 266 if (!client_) | 262 if (!client_) |
| 267 return; | 263 return; |
| 268 | 264 |
| 269 // Inform Blink about the audio stream format. | 265 // Inform Blink about the audio stream format. |
| 270 client_->setFormat(tee_filter_->channels(), tee_filter_->sample_rate()); | 266 client_->setFormat(tee_filter_->channels(), tee_filter_->sample_rate()); |
| 271 } | 267 } |
| 272 | 268 |
| 273 int WebAudioSourceProviderImpl::TeeFilter::Render( | 269 int WebAudioSourceProviderImpl::TeeFilter::Render(AudioBus* audio_bus, |
| 274 base::TimeDelta delay, | 270 uint32_t frames_delayed, |
| 275 base::TimeTicks delay_timestamp, | 271 uint32_t frames_skipped) { |
| 276 int prior_frames_skipped, | |
| 277 AudioBus* audio_bus) { | |
| 278 DCHECK(IsInitialized()); | 272 DCHECK(IsInitialized()); |
| 279 | 273 |
| 280 const int num_rendered_frames = renderer_->Render( | 274 const int num_rendered_frames = |
| 281 delay, delay_timestamp, prior_frames_skipped, audio_bus); | 275 renderer_->Render(audio_bus, frames_delayed, frames_skipped); |
| 282 | 276 |
| 283 if (!copy_audio_bus_callback_.is_null()) { | 277 if (!copy_audio_bus_callback_.is_null()) { |
| 284 const int64_t frames_delayed = | |
| 285 AudioTimestampHelper::TimeToFrames(delay, sample_rate_); | |
| 286 std::unique_ptr<AudioBus> bus_copy = | 278 std::unique_ptr<AudioBus> bus_copy = |
| 287 AudioBus::Create(audio_bus->channels(), audio_bus->frames()); | 279 AudioBus::Create(audio_bus->channels(), audio_bus->frames()); |
| 288 audio_bus->CopyTo(bus_copy.get()); | 280 audio_bus->CopyTo(bus_copy.get()); |
| 289 copy_audio_bus_callback_.Run(std::move(bus_copy), frames_delayed, | 281 copy_audio_bus_callback_.Run(std::move(bus_copy), frames_delayed, |
| 290 sample_rate_); | 282 sample_rate_); |
| 291 } | 283 } |
| 292 | 284 |
| 293 return num_rendered_frames; | 285 return num_rendered_frames; |
| 294 } | 286 } |
| 295 | 287 |
| 296 void WebAudioSourceProviderImpl::TeeFilter::OnRenderError() { | 288 void WebAudioSourceProviderImpl::TeeFilter::OnRenderError() { |
| 297 DCHECK(IsInitialized()); | 289 DCHECK(IsInitialized()); |
| 298 renderer_->OnRenderError(); | 290 renderer_->OnRenderError(); |
| 299 } | 291 } |
| 300 | 292 |
| 301 } // namespace media | 293 } // namespace media |
| OLD | NEW |