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 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 base::WeakPtrFactory<WebAudioSourceProviderImpl> weak_this_; 60 61 // Closure that posts a task to call OnSetFormat() on the renderer thread. 62 base::Closure set_format_cb_; 63 64 // Set to true when Initialize() is called. 65 int channels_; 66 int sample_rate_; 67 double volume_; 68 69 // Tracks the current playback state. 70 enum PlaybackState { kStopped, kStarted, kPlaying }; 71 PlaybackState state_; 72 73 // Where audio comes from. 74 media::AudioRendererSink::RenderCallback* renderer_; 75 76 // When set via setClient() it overrides |sink_| for consuming audio. 77 blink::WebAudioSourceProviderClient* client_; 78 79 // Where audio ends up unless overridden by |client_|. 80 base::Lock sink_lock_; 81 scoped_refptr<media::AudioRendererSink> sink_; 82 scoped_ptr<media::AudioBus> bus_wrapper_; 83 84 DISALLOW_IMPLICIT_CONSTRUCTORS(WebAudioSourceProviderImpl); 85 }; 86 87 } // namespace content 88 89 #endif // CONTENT_RENDERER_MEDIA_WEBAUDIOSOURCEPROVIDER_IMPL_H_ 90