Chromium Code Reviews| 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 #ifndef MEDIA_BLINK_WEBAUDIOSOURCEPROVIDER_IMPL_H_ | 5 #ifndef MEDIA_BLINK_WEBAUDIOSOURCEPROVIDER_IMPL_H_ |
| 6 #define MEDIA_BLINK_WEBAUDIOSOURCEPROVIDER_IMPL_H_ | 6 #define MEDIA_BLINK_WEBAUDIOSOURCEPROVIDER_IMPL_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
| 16 #include "media/base/audio_renderer_sink.h" | 16 #include "media/base/audio_renderer_sink.h" |
| 17 #include "media/blink/media_blink_export.h" | 17 #include "media/blink/media_blink_export.h" |
| 18 #include "third_party/WebKit/public/platform/WebAudioSourceProvider.h" | 18 #include "third_party/WebKit/public/platform/WebAudioSourceProvider.h" |
| 19 #include "third_party/WebKit/public/platform/WebVector.h" | 19 #include "third_party/WebKit/public/platform/WebVector.h" |
| 20 | 20 |
| 21 namespace blink { | 21 namespace blink { |
| 22 class WebAudioSourceProviderClient; | 22 class WebAudioSourceProviderClient; |
| 23 } | 23 } |
| 24 | 24 |
| 25 namespace media { | 25 namespace media { |
| 26 | 26 |
| 27 // WebAudioSourceProviderImpl provides a bridge between classes: | 27 // WebAudioSourceProviderImpl is either one of two things (but not both): |
| 28 // blink::WebAudioSourceProvider <---> AudioRendererSink | 28 // - a connection between a RestartableAudioRendererSink (the |sink_|) passed in |
| 29 // constructor and an AudioRendererSink::RenderCallback passed on Initialize() | |
| 30 // by means of an internal AudioRendererSink::RenderCallback. | |
| 31 // - a connection between the said AudioRendererSink::RenderCallback and a | |
| 32 // blink::WebAudioSourceProviderClient passed via setClient() (the |client_|), | |
| 33 // again using the internal AudioRendererSink::RenderCallback. Blink calls | |
| 34 // provideInput() periodically to fetch the appropriate data. | |
| 29 // | 35 // |
| 30 // WebAudioSourceProviderImpl wraps an existing audio sink that is used unless | 36 // In either case, the internal RenderCallback allows for delivering a copy of |
| 31 // WebKit has set a client via setClient(). While a client is set WebKit will | 37 // the data if a listener is configured. WASPImpl is also a |
| 32 // periodically call provideInput() to render a certain number of audio | 38 // RestartableAudioRendererSink itself in order to be controlled (Play(), |
| 33 // sample-frames using the sink's RenderCallback to get the data. | 39 // Pause() etc). |
| 34 // | 40 // |
| 35 // All calls are protected by a lock. | 41 // All calls are protected by a lock. |
| 36 class MEDIA_BLINK_EXPORT WebAudioSourceProviderImpl | 42 class MEDIA_BLINK_EXPORT WebAudioSourceProviderImpl |
| 37 : NON_EXPORTED_BASE(public blink::WebAudioSourceProvider), | 43 : NON_EXPORTED_BASE(public blink::WebAudioSourceProvider), |
| 38 NON_EXPORTED_BASE(public RestartableAudioRendererSink) { | 44 NON_EXPORTED_BASE(public RestartableAudioRendererSink) { |
| 39 public: | 45 public: |
| 46 using CopyAudioCB = base::Callback< | |
| 47 void(scoped_ptr<AudioBus>, uint32_t delay_milliseconds, int sample_rate)>; | |
| 48 | |
| 40 explicit WebAudioSourceProviderImpl( | 49 explicit WebAudioSourceProviderImpl( |
| 41 const scoped_refptr<RestartableAudioRendererSink>& sink); | 50 const scoped_refptr<RestartableAudioRendererSink>& sink); |
| 42 | 51 |
| 43 // blink::WebAudioSourceProvider implementation. | 52 // blink::WebAudioSourceProvider implementation. |
| 44 void setClient(blink::WebAudioSourceProviderClient* client) override; | 53 void setClient(blink::WebAudioSourceProviderClient* client) override; |
| 45 void provideInput(const blink::WebVector<float*>& audio_data, | 54 void provideInput(const blink::WebVector<float*>& audio_data, |
| 46 size_t number_of_frames) override; | 55 size_t number_of_frames) override; |
| 47 | 56 |
| 48 // RestartableAudioRendererSink implementation. | 57 // RestartableAudioRendererSink implementation. |
| 49 void Start() override; | 58 void Start() override; |
| 50 void Stop() override; | 59 void Stop() override; |
| 51 void Play() override; | 60 void Play() override; |
| 52 void Pause() override; | 61 void Pause() override; |
| 53 bool SetVolume(double volume) override; | 62 bool SetVolume(double volume) override; |
| 54 OutputDevice* GetOutputDevice() override; | 63 OutputDevice* GetOutputDevice() override; |
| 55 void Initialize(const AudioParameters& params, | 64 void Initialize(const AudioParameters& params, |
| 56 RenderCallback* renderer) override; | 65 RenderCallback* renderer) override; |
| 57 | 66 |
| 58 protected: | 67 // These methods allow a client to get a copy of the rendered audio. |
| 68 void RegisterCopyAudioCallback(const CopyAudioCB& callback); | |
|
DaleCurtis
2016/03/12 03:24:42
s/Register/Set/ since there can only be one.
mcasas
2016/03/16 18:50:51
Done.
| |
| 69 void ClearCopyAudioCallback(); | |
| 70 | |
| 71 private: | |
| 72 friend class WebAudioSourceProviderImplTest; | |
| 59 ~WebAudioSourceProviderImpl() override; | 73 ~WebAudioSourceProviderImpl() override; |
| 60 | 74 |
| 61 private: | |
| 62 // Calls setFormat() on |client_| from the Blink renderer thread. | 75 // Calls setFormat() on |client_| from the Blink renderer thread. |
| 63 void OnSetFormat(); | 76 void OnSetFormat(); |
| 64 | 77 |
| 65 // Closure that posts a task to call OnSetFormat() on the renderer thread. | 78 int RenderForTesting(AudioBus* audio_bus); |
| 66 base::Closure set_format_cb_; | |
| 67 | 79 |
| 68 // Set to true when Initialize() is called. | 80 // Used to keep the volume across reconfigurations. |
| 69 int channels_; | |
| 70 int sample_rate_; | |
| 71 double volume_; | 81 double volume_; |
| 72 | 82 |
| 73 // Tracks the current playback state. | 83 // Tracks the current playback state. |
| 74 enum PlaybackState { kStopped, kStarted, kPlaying }; | 84 enum PlaybackState { kStopped, kStarted, kPlaying }; |
| 75 PlaybackState state_; | 85 PlaybackState state_; |
| 76 | 86 |
| 77 // Where audio comes from. | 87 // Closure that calls OnSetFormat() on |client_| on the renderer thread. |
| 78 AudioRendererSink::RenderCallback* renderer_; | 88 base::Closure set_format_cb_; |
| 79 | |
| 80 // When set via setClient() it overrides |sink_| for consuming audio. | 89 // When set via setClient() it overrides |sink_| for consuming audio. |
| 81 blink::WebAudioSourceProviderClient* client_; | 90 blink::WebAudioSourceProviderClient* client_; |
| 82 | 91 |
| 83 // Where audio ends up unless overridden by |client_|. | 92 // Where audio ends up unless overridden by |client_|. |
| 84 base::Lock sink_lock_; | 93 base::Lock sink_lock_; |
| 85 scoped_refptr<RestartableAudioRendererSink> sink_; | 94 const scoped_refptr<RestartableAudioRendererSink> sink_; |
| 86 scoped_ptr<AudioBus> bus_wrapper_; | 95 scoped_ptr<AudioBus> bus_wrapper_; |
| 87 | 96 |
| 97 // An inner class acting as a T filter where actual data can be tapped. | |
| 98 class TeeFilter; | |
| 99 scoped_ptr<TeeFilter> tee_filter_; | |
| 100 | |
| 88 // NOTE: Weak pointers must be invalidated before all other member variables. | 101 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 89 base::WeakPtrFactory<WebAudioSourceProviderImpl> weak_factory_; | 102 base::WeakPtrFactory<WebAudioSourceProviderImpl> weak_factory_; |
| 90 | 103 |
| 91 DISALLOW_IMPLICIT_CONSTRUCTORS(WebAudioSourceProviderImpl); | 104 DISALLOW_IMPLICIT_CONSTRUCTORS(WebAudioSourceProviderImpl); |
| 92 }; | 105 }; |
| 93 | 106 |
| 94 } // namespace media | 107 } // namespace media |
| 95 | 108 |
| 96 #endif // MEDIA_BLINK_WEBAUDIOSOURCEPROVIDER_IMPL_H_ | 109 #endif // MEDIA_BLINK_WEBAUDIOSOURCEPROVIDER_IMPL_H_ |
| OLD | NEW |