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 3 // found in the LICENSE file. 4 5 #ifndef THIRD_PARTY_BLINK_PUBLIC_PLATFORM_WEBAUDIOSOURCEPROVIDER_IMPL_H_ 6 #define THIRD_PARTY_BLINK_PUBLIC_PLATFORM_WEBAUDIOSOURCEPROVIDER_IMPL_H_ 7 8 #include <stddef.h> 9 10 #include <memory> 11 #include <string> 12 13 #include "base/callback.h" 14 #include "base/callback_forward.h" 15 #include "base/macros.h" 16 #include "base/memory/weak_ptr.h" 17 #include "base/synchronization/lock.h" 18 #include "media/base/audio_renderer_sink.h" 19 #include "third_party/blink/public/platform/platform.h" 20 #include "third_party/blink/public/platform/web_audio_source_provider.h" 21 #include "third_party/blink/public/platform/web_vector.h" 22 23 namespace media { 24 class MediaLog; 25 } 26 27 namespace blink { 28 29 class WebAudioSourceProviderClient; 30 31 // WebAudioSourceProviderImpl is either one of two things (but not both): 32 // - a connection between a RestartableAudioRendererSink (the |sink_|) passed in 33 // constructor and an AudioRendererSink::RenderCallback passed on Initialize() 34 // by means of an internal AudioRendererSink::RenderCallback. 35 // - a connection between the said AudioRendererSink::RenderCallback and a 36 // WebAudioSourceProviderClient passed via setClient() (the |client_|), 37 // again using the internal AudioRendererSink::RenderCallback. Blink calls 38 // provideInput() periodically to fetch the appropriate data. 39 // 40 // In either case, the internal RenderCallback allows for delivering a copy of 41 // the data if a listener is configured. WASPImpl is also a 42 // RestartableAudioRendererSink itself in order to be controlled (Play(), 43 // Pause() etc). 44 // 45 // All calls are protected by a lock. 46 class BLINK_PLATFORM_EXPORT WebAudioSourceProviderImpl 47 : public WebAudioSourceProvider, 48 public media::SwitchableAudioRendererSink { 49 public: 50 using CopyAudioCB = 51 base::RepeatingCallback<void(std::unique_ptr<media::AudioBus>, 52 uint32_t frames_delayed, 53 int sample_rate)>; 54 55 // Optionally provide a callback to be run the first time a 56 // WebAudioSourceProviderClient is attached via SetClient(). Note that the 57 // callback will be run once at most, however SetClient may be called any 58 // number of times. 59 WebAudioSourceProviderImpl( 60 scoped_refptr<media::SwitchableAudioRendererSink> sink, 61 media::MediaLog* media_log, 62 base::OnceClosure on_set_client_callback = base::OnceClosure()); 63 64 // WebAudioSourceProvider implementation. 65 void SetClient(WebAudioSourceProviderClient* client) override; 66 void ProvideInput(const WebVector<float*>& audio_data, 67 size_t number_of_frames) override; 68 69 // RestartableAudioRendererSink implementation. 70 void Initialize(const media::AudioParameters& params, 71 RenderCallback* renderer) override; 72 void Start() override; 73 void Stop() override; 74 void Play() override; 75 void Pause() override; 76 void Flush() override; 77 bool SetVolume(double volume) override; 78 media::OutputDeviceInfo GetOutputDeviceInfo() override; 79 void GetOutputDeviceInfoAsync(OutputDeviceInfoCB info_cb) override; 80 bool IsOptimizedForHardwareParameters() override; 81 bool CurrentThreadIsRenderingThread() override; 82 void SwitchOutputDevice(const std::string& device_id, 83 media::OutputDeviceStatusCB callback) override; 84 void TaintOrigin(); 85 86 // These methods allow a client to get a copy of the rendered audio. 87 void SetCopyAudioCallback(CopyAudioCB callback); 88 void ClearCopyAudioCallback(); 89 90 int RenderForTesting(media::AudioBus* audio_bus); 91 92 protected: 93 ~WebAudioSourceProviderImpl() override; 94 95 private: 96 friend class WebAudioSourceProviderImplTest; 97 98 // Calls setFormat() on |client_| from the Blink renderer thread. 99 void OnSetFormat(); 100 101 // Used to keep the volume across reconfigurations. 102 double volume_; 103 104 // Tracks the current playback state. 105 enum PlaybackState { kStopped, kStarted, kPlaying }; 106 PlaybackState state_; 107 108 // Closure that calls OnSetFormat() on |client_| on the renderer thread. 109 base::RepeatingClosure set_format_cb_; 110 111 // When set via setClient() it overrides |sink_| for consuming audio. 112 WebAudioSourceProviderClient* client_; 113 114 // Where audio ends up unless overridden by |client_|. 115 base::Lock sink_lock_; 116 scoped_refptr<media::SwitchableAudioRendererSink> sink_ 117 GUARDED_BY(sink_lock_); 118 std::unique_ptr<media::AudioBus> bus_wrapper_; 119 120 // An inner class acting as a T filter where actual data can be tapped. 121 class TeeFilter; 122 const std::unique_ptr<TeeFilter> tee_filter_; 123 124 media::MediaLog* const media_log_; 125 126 base::OnceClosure on_set_client_callback_; 127 128 // NOTE: Weak pointers must be invalidated before all other member variables. 129 base::WeakPtrFactory<WebAudioSourceProviderImpl> weak_factory_{this}; 130 131 DISALLOW_IMPLICIT_CONSTRUCTORS(WebAudioSourceProviderImpl); 132 }; 133 134 } // namespace blink 135 136 #endif // THIRD_PARTY_BLINK_PUBLIC_PLATFORM_WEBAUDIOSOURCEPROVIDER_IMPL_H_ 137