Home | History | Annotate | Download | only in proxy
      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 PPAPI_PROXY_AUDIO_INPUT_RESOURCE_H_
      6 #define PPAPI_PROXY_AUDIO_INPUT_RESOURCE_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/compiler_specific.h"
     10 #include "base/memory/ref_counted.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/memory/shared_memory.h"
     13 #include "base/sync_socket.h"
     14 #include "base/threading/simple_thread.h"
     15 #include "ppapi/proxy/device_enumeration_resource_helper.h"
     16 #include "ppapi/proxy/plugin_resource.h"
     17 #include "ppapi/shared_impl/scoped_pp_resource.h"
     18 #include "ppapi/thunk/ppb_audio_input_api.h"
     19 
     20 namespace ppapi {
     21 namespace proxy {
     22 
     23 class ResourceMessageReplyParams;
     24 
     25 class AudioInputResource : public PluginResource,
     26                            public thunk::PPB_AudioInput_API,
     27                            public base::DelegateSimpleThread::Delegate {
     28  public:
     29   AudioInputResource(Connection connection, PP_Instance instance);
     30   virtual ~AudioInputResource();
     31 
     32   // Resource overrides.
     33   virtual thunk::PPB_AudioInput_API* AsPPB_AudioInput_API() OVERRIDE;
     34   virtual void OnReplyReceived(const ResourceMessageReplyParams& params,
     35                                const IPC::Message& msg) OVERRIDE;
     36 
     37   // PPB_AudioInput_API implementation.
     38   virtual int32_t EnumerateDevices0_2(
     39       PP_Resource* devices,
     40       scoped_refptr<TrackedCallback> callback) OVERRIDE;
     41   virtual int32_t EnumerateDevices(
     42       const PP_ArrayOutput& output,
     43       scoped_refptr<TrackedCallback> callback) OVERRIDE;
     44   virtual int32_t MonitorDeviceChange(
     45       PP_MonitorDeviceChangeCallback callback,
     46       void* user_data) OVERRIDE;
     47   virtual int32_t Open0_2(PP_Resource device_ref,
     48                           PP_Resource config,
     49                           PPB_AudioInput_Callback_0_2 audio_input_callback_0_2,
     50                           void* user_data,
     51                           scoped_refptr<TrackedCallback> callback) OVERRIDE;
     52   virtual int32_t Open(PP_Resource device_ref,
     53                        PP_Resource config,
     54                        PPB_AudioInput_Callback audio_input_callback,
     55                        void* user_data,
     56                        scoped_refptr<TrackedCallback> callback) OVERRIDE;
     57   virtual PP_Resource GetCurrentConfig() OVERRIDE;
     58   virtual PP_Bool StartCapture() OVERRIDE;
     59   virtual PP_Bool StopCapture() OVERRIDE;
     60   virtual void Close() OVERRIDE;
     61 
     62  protected:
     63   // Resource override.
     64   virtual void LastPluginRefWasDeleted() OVERRIDE;
     65 
     66  private:
     67   enum OpenState {
     68     BEFORE_OPEN,
     69     OPENED,
     70     CLOSED
     71   };
     72 
     73   void OnPluginMsgOpenReply(const ResourceMessageReplyParams& params);
     74 
     75   // Sets the shared memory and socket handles. This will automatically start
     76   // capture if we're currently set to capture.
     77   void SetStreamInfo(base::SharedMemoryHandle shared_memory_handle,
     78                      size_t shared_memory_size,
     79                      base::SyncSocket::Handle socket_handle);
     80 
     81   // Starts execution of the audio input thread.
     82   void StartThread();
     83 
     84   // Stops execution of the audio input thread.
     85   void StopThread();
     86 
     87   // DelegateSimpleThread::Delegate implementation.
     88   // Run on the audio input thread.
     89   virtual void Run() OVERRIDE;
     90 
     91   int32_t CommonOpen(PP_Resource device_ref,
     92                      PP_Resource config,
     93                      PPB_AudioInput_Callback_0_2 audio_input_callback_0_2,
     94                      PPB_AudioInput_Callback audio_input_callback,
     95                      void* user_data,
     96                      scoped_refptr<TrackedCallback> callback);
     97 
     98   OpenState open_state_;
     99 
    100   // True if capturing the stream.
    101   bool capturing_;
    102 
    103   // Socket used to notify us when new samples are available. This pointer is
    104   // created in SetStreamInfo().
    105   scoped_ptr<base::CancelableSyncSocket> socket_;
    106 
    107   // Sample buffer in shared memory. This pointer is created in
    108   // SetStreamInfo(). The memory is only mapped when the audio thread is
    109   // created.
    110   scoped_ptr<base::SharedMemory> shared_memory_;
    111 
    112   // The size of the sample buffer in bytes.
    113   size_t shared_memory_size_;
    114 
    115   // When the callback is set, this thread is spawned for calling it.
    116   scoped_ptr<base::DelegateSimpleThread> audio_input_thread_;
    117 
    118   // Callback to call when new samples are available.
    119   PPB_AudioInput_Callback_0_2 audio_input_callback_0_2_;
    120   PPB_AudioInput_Callback audio_input_callback_;
    121 
    122   // User data pointer passed verbatim to the callback function.
    123   void* user_data_;
    124 
    125   // The callback is not directly passed to OnPluginMsgOpenReply() because we
    126   // would like to be able to cancel it early in Close().
    127   scoped_refptr<TrackedCallback> open_callback_;
    128 
    129   // Owning reference to the current config object. This isn't actually used,
    130   // we just dish it out as requested by the plugin.
    131   ScopedPPResource config_;
    132 
    133   DeviceEnumerationResourceHelper enumeration_helper_;
    134 
    135   // The data size (in bytes) of one second of audio input. Used to calculate
    136   // latency.
    137   size_t bytes_per_second_;
    138 
    139   DISALLOW_COPY_AND_ASSIGN(AudioInputResource);
    140 };
    141 
    142 }  // namespace proxy
    143 }  // namespace ppapi
    144 
    145 #endif  // PPAPI_PROXY_AUDIO_INPUT_RESOURCE_H_
    146