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