| 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 class MediaLog; |
| 26 | 27 |
| 27 // WebAudioSourceProviderImpl is either one of two things (but not both): | 28 // WebAudioSourceProviderImpl is either one of two things (but not both): |
| 28 // - a connection between a RestartableAudioRendererSink (the |sink_|) passed in | 29 // - a connection between a RestartableAudioRendererSink (the |sink_|) passed in |
| 29 // constructor and an AudioRendererSink::RenderCallback passed on Initialize() | 30 // constructor and an AudioRendererSink::RenderCallback passed on Initialize() |
| 30 // by means of an internal AudioRendererSink::RenderCallback. | 31 // by means of an internal AudioRendererSink::RenderCallback. |
| 31 // - a connection between the said AudioRendererSink::RenderCallback and a | 32 // - a connection between the said AudioRendererSink::RenderCallback and a |
| 32 // blink::WebAudioSourceProviderClient passed via setClient() (the |client_|), | 33 // blink::WebAudioSourceProviderClient passed via setClient() (the |client_|), |
| 33 // again using the internal AudioRendererSink::RenderCallback. Blink calls | 34 // again using the internal AudioRendererSink::RenderCallback. Blink calls |
| 34 // provideInput() periodically to fetch the appropriate data. | 35 // provideInput() periodically to fetch the appropriate data. |
| 35 // | 36 // |
| 36 // In either case, the internal RenderCallback allows for delivering a copy of | 37 // In either case, the internal RenderCallback allows for delivering a copy of |
| 37 // the data if a listener is configured. WASPImpl is also a | 38 // the data if a listener is configured. WASPImpl is also a |
| 38 // RestartableAudioRendererSink itself in order to be controlled (Play(), | 39 // RestartableAudioRendererSink itself in order to be controlled (Play(), |
| 39 // Pause() etc). | 40 // Pause() etc). |
| 40 // | 41 // |
| 41 // All calls are protected by a lock. | 42 // All calls are protected by a lock. |
| 42 class MEDIA_BLINK_EXPORT WebAudioSourceProviderImpl | 43 class MEDIA_BLINK_EXPORT WebAudioSourceProviderImpl |
| 43 : NON_EXPORTED_BASE(public blink::WebAudioSourceProvider), | 44 : NON_EXPORTED_BASE(public blink::WebAudioSourceProvider), |
| 44 NON_EXPORTED_BASE(public SwitchableAudioRendererSink) { | 45 NON_EXPORTED_BASE(public SwitchableAudioRendererSink) { |
| 45 public: | 46 public: |
| 46 using CopyAudioCB = base::Callback<void(std::unique_ptr<AudioBus>, | 47 using CopyAudioCB = base::Callback<void(std::unique_ptr<AudioBus>, |
| 47 uint32_t frames_delayed, | 48 uint32_t frames_delayed, |
| 48 int sample_rate)>; | 49 int sample_rate)>; |
| 49 | 50 |
| 50 explicit WebAudioSourceProviderImpl( | 51 WebAudioSourceProviderImpl(scoped_refptr<SwitchableAudioRendererSink> sink, |
| 51 const scoped_refptr<SwitchableAudioRendererSink>& sink); | 52 scoped_refptr<MediaLog> media_log); |
| 52 | 53 |
| 53 // blink::WebAudioSourceProvider implementation. | 54 // blink::WebAudioSourceProvider implementation. |
| 54 void setClient(blink::WebAudioSourceProviderClient* client) override; | 55 void setClient(blink::WebAudioSourceProviderClient* client) override; |
| 55 void provideInput(const blink::WebVector<float*>& audio_data, | 56 void provideInput(const blink::WebVector<float*>& audio_data, |
| 56 size_t number_of_frames) override; | 57 size_t number_of_frames) override; |
| 57 | 58 |
| 58 // RestartableAudioRendererSink implementation. | 59 // RestartableAudioRendererSink implementation. |
| 59 void Initialize(const AudioParameters& params, | 60 void Initialize(const AudioParameters& params, |
| 60 RenderCallback* renderer) override; | 61 RenderCallback* renderer) override; |
| 61 void Start() override; | 62 void Start() override; |
| 62 void Stop() override; | 63 void Stop() override; |
| 63 void Play() override; | 64 void Play() override; |
| 64 void Pause() override; | 65 void Pause() override; |
| 65 bool SetVolume(double volume) override; | 66 bool SetVolume(double volume) override; |
| 66 OutputDeviceInfo GetOutputDeviceInfo() override; | 67 OutputDeviceInfo GetOutputDeviceInfo() override; |
| 67 bool CurrentThreadIsRenderingThread() override; | 68 bool CurrentThreadIsRenderingThread() override; |
| 68 void SwitchOutputDevice(const std::string& device_id, | 69 void SwitchOutputDevice(const std::string& device_id, |
| 69 const url::Origin& security_origin, | 70 const url::Origin& security_origin, |
| 70 const OutputDeviceStatusCB& callback) override; | 71 const OutputDeviceStatusCB& callback) override; |
| 71 | 72 |
| 72 // These methods allow a client to get a copy of the rendered audio. | 73 // These methods allow a client to get a copy of the rendered audio. |
| 73 void SetCopyAudioCallback(const CopyAudioCB& callback); | 74 void SetCopyAudioCallback(const CopyAudioCB& callback); |
| 74 void ClearCopyAudioCallback(); | 75 void ClearCopyAudioCallback(); |
| 75 | 76 |
| 76 int RenderForTesting(AudioBus* audio_bus); | 77 int RenderForTesting(AudioBus* audio_bus); |
| 77 | 78 |
| 79 protected: |
| 80 virtual scoped_refptr<SwitchableAudioRendererSink> CreateFallbackSink(); |
| 81 ~WebAudioSourceProviderImpl() override; |
| 82 |
| 78 private: | 83 private: |
| 79 friend class WebAudioSourceProviderImplTest; | 84 friend class WebAudioSourceProviderImplTest; |
| 80 ~WebAudioSourceProviderImpl() override; | |
| 81 | 85 |
| 82 // Calls setFormat() on |client_| from the Blink renderer thread. | 86 // Calls setFormat() on |client_| from the Blink renderer thread. |
| 83 void OnSetFormat(); | 87 void OnSetFormat(); |
| 84 | 88 |
| 85 // Used to keep the volume across reconfigurations. | 89 // Used to keep the volume across reconfigurations. |
| 86 double volume_; | 90 double volume_; |
| 87 | 91 |
| 88 // Tracks the current playback state. | 92 // Tracks the current playback state. |
| 89 enum PlaybackState { kStopped, kStarted, kPlaying }; | 93 enum PlaybackState { kStopped, kStarted, kPlaying }; |
| 90 PlaybackState state_; | 94 PlaybackState state_; |
| 91 | 95 |
| 92 // Closure that calls OnSetFormat() on |client_| on the renderer thread. | 96 // Closure that calls OnSetFormat() on |client_| on the renderer thread. |
| 93 base::Closure set_format_cb_; | 97 base::Closure set_format_cb_; |
| 94 // When set via setClient() it overrides |sink_| for consuming audio. | 98 // When set via setClient() it overrides |sink_| for consuming audio. |
| 95 blink::WebAudioSourceProviderClient* client_; | 99 blink::WebAudioSourceProviderClient* client_; |
| 96 | 100 |
| 97 // Where audio ends up unless overridden by |client_|. | 101 // Where audio ends up unless overridden by |client_|. |
| 98 base::Lock sink_lock_; | 102 base::Lock sink_lock_; |
| 99 const scoped_refptr<SwitchableAudioRendererSink> sink_; | 103 scoped_refptr<SwitchableAudioRendererSink> sink_; |
| 100 std::unique_ptr<AudioBus> bus_wrapper_; | 104 std::unique_ptr<AudioBus> bus_wrapper_; |
| 101 | 105 |
| 102 // An inner class acting as a T filter where actual data can be tapped. | 106 // An inner class acting as a T filter where actual data can be tapped. |
| 103 class TeeFilter; | 107 class TeeFilter; |
| 104 const std::unique_ptr<TeeFilter> tee_filter_; | 108 const std::unique_ptr<TeeFilter> tee_filter_; |
| 105 | 109 |
| 110 const scoped_refptr<MediaLog> media_log_; |
| 111 |
| 106 // NOTE: Weak pointers must be invalidated before all other member variables. | 112 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 107 base::WeakPtrFactory<WebAudioSourceProviderImpl> weak_factory_; | 113 base::WeakPtrFactory<WebAudioSourceProviderImpl> weak_factory_; |
| 108 | 114 |
| 109 DISALLOW_IMPLICIT_CONSTRUCTORS(WebAudioSourceProviderImpl); | 115 DISALLOW_IMPLICIT_CONSTRUCTORS(WebAudioSourceProviderImpl); |
| 110 }; | 116 }; |
| 111 | 117 |
| 112 } // namespace media | 118 } // namespace media |
| 113 | 119 |
| 114 #endif // MEDIA_BLINK_WEBAUDIOSOURCEPROVIDER_IMPL_H_ | 120 #endif // MEDIA_BLINK_WEBAUDIOSOURCEPROVIDER_IMPL_H_ |
| OLD | NEW |