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 CONTENT_RENDERER_MEDIA_WEBAUDIOSOURCEPROVIDER_IMPL_H_ 6 #define CONTENT_RENDERER_MEDIA_WEBAUDIOSOURCEPROVIDER_IMPL_H_ 7 8 #include "base/callback.h" 9 #include "base/memory/weak_ptr.h" 10 #include "base/synchronization/lock.h" 11 #include "content/common/content_export.h" 12 #include "media/base/audio_renderer_sink.h" 13 #include "third_party/WebKit/public/platform/WebAudioSourceProvider.h" 14 #include "third_party/WebKit/public/platform/WebVector.h" 15 16 namespace blink { 17 class WebAudioSourceProviderClient; 18 } 19 20 namespace content { 21 22 // WebAudioSourceProviderImpl provides a bridge between classes: 23 // blink::WebAudioSourceProvider <---> media::AudioRendererSink 24 // 25 // WebAudioSourceProviderImpl wraps an existing audio sink that is used unless 26 // WebKit has set a client via setClient(). While a client is set WebKit will 27 // periodically call provideInput() to render a certain number of audio 28 // sample-frames using the sink's RenderCallback to get the data. 29 // 30 // All calls are protected by a lock. 31 class CONTENT_EXPORT WebAudioSourceProviderImpl NON_EXPORTED_BASE(public blink::WebAudioSourceProvider)32 : NON_EXPORTED_BASE(public blink::WebAudioSourceProvider), 33 NON_EXPORTED_BASE(public media::AudioRendererSink) { 34 public: 35 explicit WebAudioSourceProviderImpl( 36 const scoped_refptr<media::AudioRendererSink>& sink); 37 38 // blink::WebAudioSourceProvider implementation. 39 virtual void setClient(blink::WebAudioSourceProviderClient* client); 40 virtual void provideInput(const blink::WebVector<float*>& audio_data, 41 size_t number_of_frames); 42 43 // media::AudioRendererSink implementation. 44 virtual void Start() OVERRIDE; 45 virtual void Stop() OVERRIDE; 46 virtual void Play() OVERRIDE; 47 virtual void Pause() OVERRIDE; 48 virtual bool SetVolume(double volume) OVERRIDE; 49 virtual void Initialize(const media::AudioParameters& params, 50 RenderCallback* renderer) OVERRIDE; 51 52 protected: 53 virtual ~WebAudioSourceProviderImpl(); 54 55 private: 56 // Calls setFormat() on |client_| from the Blink renderer thread. 57 void OnSetFormat(); 58 59 // Closure that posts a task to call OnSetFormat() on the renderer thread. 60 base::Closure set_format_cb_; 61 62 // Set to true when Initialize() is called. 63 int channels_; 64 int sample_rate_; 65 double volume_; 66 67 // Tracks the current playback state. 68 enum PlaybackState { kStopped, kStarted, kPlaying }; 69 PlaybackState state_; 70 71 // Where audio comes from. 72 media::AudioRendererSink::RenderCallback* renderer_; 73 74 // When set via setClient() it overrides |sink_| for consuming audio. 75 blink::WebAudioSourceProviderClient* client_; 76 77 // Where audio ends up unless overridden by |client_|. 78 base::Lock sink_lock_; 79 scoped_refptr<media::AudioRendererSink> sink_; 80 scoped_ptr<media::AudioBus> bus_wrapper_; 81 82 // NOTE: Weak pointers must be invalidated before all other member variables. 83 base::WeakPtrFactory<WebAudioSourceProviderImpl> weak_factory_; 84 85 DISALLOW_IMPLICIT_CONSTRUCTORS(WebAudioSourceProviderImpl); 86 }; 87 88 } // namespace content 89 90 #endif // CONTENT_RENDERER_MEDIA_WEBAUDIOSOURCEPROVIDER_IMPL_H_ 91