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