Home | History | Annotate | Download | only in pepper
      1 // Copyright (c) 2012 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_PEPPER_PEPPER_PLATFORM_AUDIO_OUTPUT_IMPL_H_
      6 #define CONTENT_RENDERER_PEPPER_PEPPER_PLATFORM_AUDIO_OUTPUT_IMPL_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/memory/ref_counted.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "media/audio/audio_output_ipc.h"
     12 
     13 namespace media {
     14 class AudioParameters;
     15 }
     16 
     17 namespace base {
     18 class MessageLoopProxy;
     19 }
     20 
     21 namespace content {
     22 class AudioHelper;
     23 
     24 class PepperPlatformAudioOutput
     25     : public media::AudioOutputIPCDelegate,
     26       public base::RefCountedThreadSafe<PepperPlatformAudioOutput> {
     27  public:
     28   // Factory function, returns NULL on failure. StreamCreated() will be called
     29   // when the stream is created.
     30   static PepperPlatformAudioOutput* Create(int sample_rate,
     31                                            int frames_per_buffer,
     32                                            int source_render_view_id,
     33                                            AudioHelper* client);
     34 
     35   // The following three methods are all called on main thread.
     36 
     37   // Starts the playback. Returns false on error or if called before the
     38   // stream is created or after the stream is closed.
     39   bool StartPlayback();
     40 
     41   // Stops the playback. Returns false on error or if called before the stream
     42   // is created or after the stream is closed.
     43   bool StopPlayback();
     44 
     45   // Closes the stream. Make sure to call this before the object is
     46   // destructed.
     47   void ShutDown();
     48 
     49   // media::AudioOutputIPCDelegate implementation.
     50   virtual void OnStateChanged(
     51       media::AudioOutputIPCDelegate::State state) OVERRIDE;
     52   virtual void OnStreamCreated(base::SharedMemoryHandle handle,
     53                                base::SyncSocket::Handle socket_handle,
     54                                int length) OVERRIDE;
     55   virtual void OnIPCClosed() OVERRIDE;
     56 
     57  protected:
     58   virtual ~PepperPlatformAudioOutput();
     59 
     60  private:
     61   friend class base::RefCountedThreadSafe<PepperPlatformAudioOutput>;
     62 
     63   PepperPlatformAudioOutput();
     64 
     65   bool Initialize(int sample_rate,
     66                   int frames_per_buffer,
     67                   int source_render_view_id,
     68                   AudioHelper* client);
     69 
     70   // I/O thread backends to above functions.
     71   void InitializeOnIOThread(const media::AudioParameters& params);
     72   void StartPlaybackOnIOThread();
     73   void StopPlaybackOnIOThread();
     74   void ShutDownOnIOThread();
     75 
     76   // The client to notify when the stream is created. THIS MUST ONLY BE
     77   // ACCESSED ON THE MAIN THREAD.
     78   AudioHelper* client_;
     79 
     80   // Used to send/receive IPC. THIS MUST ONLY BE ACCESSED ON THE
     81   // I/O thread except to send messages and get the message loop.
     82   scoped_ptr<media::AudioOutputIPC> ipc_;
     83 
     84   scoped_refptr<base::MessageLoopProxy> main_message_loop_proxy_;
     85   scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
     86 
     87   DISALLOW_COPY_AND_ASSIGN(PepperPlatformAudioOutput);
     88 };
     89 
     90 }  // namespace content
     91 
     92 #endif  // CONTENT_RENDERER_PEPPER_PEPPER_PLATFORM_AUDIO_OUTPUT_IMPL_H_
     93