| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef PPAPI_SHARED_IMPL_PPB_AUDIO_INPUT_SHARED_H_ | 5 #ifndef PPAPI_SHARED_IMPL_PPB_AUDIO_INPUT_SHARED_H_ |
| 6 #define PPAPI_SHARED_IMPL_PPB_AUDIO_INPUT_SHARED_H_ | 6 #define PPAPI_SHARED_IMPL_PPB_AUDIO_INPUT_SHARED_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/compiler_specific.h" |
| 10 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/shared_memory.h" | 12 #include "base/shared_memory.h" |
| 11 #include "base/sync_socket.h" | 13 #include "base/sync_socket.h" |
| 12 #include "base/threading/simple_thread.h" | 14 #include "base/threading/simple_thread.h" |
| 13 #include "ppapi/c/dev/ppb_audio_input_dev.h" | 15 #include "ppapi/c/dev/ppb_audio_input_dev.h" |
| 16 #include "ppapi/c/ppb_audio_config.h" |
| 14 #include "ppapi/shared_impl/resource.h" | 17 #include "ppapi/shared_impl/resource.h" |
| 18 #include "ppapi/shared_impl/scoped_pp_resource.h" |
| 19 #include "ppapi/shared_impl/tracked_callback.h" |
| 15 #include "ppapi/thunk/ppb_audio_input_api.h" | 20 #include "ppapi/thunk/ppb_audio_input_api.h" |
| 16 | 21 |
| 17 namespace ppapi { | 22 namespace ppapi { |
| 18 | 23 |
| 19 // Implements the logic to map shared memory and run the audio thread signaled | 24 // Implements the logic to map shared memory and run the audio thread signaled |
| 20 // from the sync socket. Both the proxy and the renderer implementation use | 25 // from the sync socket. Both the proxy and the renderer implementation use |
| 21 // this code. | 26 // this code. |
| 22 class PPAPI_SHARED_EXPORT PPB_AudioInput_Shared | 27 class PPAPI_SHARED_EXPORT PPB_AudioInput_Shared |
| 23 : public thunk::PPB_AudioInput_API, | 28 : public Resource, |
| 29 public thunk::PPB_AudioInput_API, |
| 24 public base::DelegateSimpleThread::Delegate { | 30 public base::DelegateSimpleThread::Delegate { |
| 25 public: | 31 public: |
| 26 PPB_AudioInput_Shared(); | 32 // Used by the proxy. |
| 33 explicit PPB_AudioInput_Shared(const HostResource& audio_input); |
| 34 // Used by the impl. |
| 35 explicit PPB_AudioInput_Shared(PP_Instance instance); |
| 27 virtual ~PPB_AudioInput_Shared(); | 36 virtual ~PPB_AudioInput_Shared(); |
| 28 | 37 |
| 29 bool capturing() const { return capturing_; } | 38 // Resource overrides. |
| 39 virtual thunk::PPB_AudioInput_API* AsPPB_AudioInput_API() OVERRIDE; |
| 30 | 40 |
| 31 // Sets the callback information that the background thread will use. This | 41 // Implementation of PPB_AudioInput_API non-trusted methods. |
| 32 // is optional. Without a callback, the thread will not be run. This | 42 virtual int32_t EnumerateDevices(PP_Resource* devices, |
| 33 // non-callback mode is used in the renderer with the proxy, since the proxy | 43 PP_CompletionCallback callback) OVERRIDE; |
| 34 // handles the callback entirely within the plugin process. | 44 virtual int32_t Open(const std::string& device_id, |
| 35 void SetCallback(PPB_AudioInput_Callback callback, void* user_data); | 45 PP_Resource config, |
| 46 PPB_AudioInput_Callback audio_input_callback, |
| 47 void* user_data, |
| 48 PP_CompletionCallback callback) OVERRIDE; |
| 49 virtual PP_Resource GetCurrentConfig() OVERRIDE; |
| 50 virtual PP_Bool StartCapture() OVERRIDE; |
| 51 virtual PP_Bool StopCapture() OVERRIDE; |
| 52 virtual void Close() OVERRIDE; |
| 36 | 53 |
| 37 // Configures the current state to be playing or not. The caller is | 54 void OnEnumerateDevicesComplete(int32_t result, |
| 55 const std::vector<DeviceRefData>& devices); |
| 56 void OnOpenComplete(int32_t result, |
| 57 base::SharedMemoryHandle shared_memory_handle, |
| 58 size_t shared_memory_size, |
| 59 base::SyncSocket::Handle socket_handle); |
| 60 |
| 61 static PP_CompletionCallback MakeIgnoredCompletionCallback(); |
| 62 |
| 63 protected: |
| 64 enum OpenState { |
| 65 BEFORE_OPEN, |
| 66 OPENED, |
| 67 CLOSED |
| 68 }; |
| 69 |
| 70 // Subclasses should implement these methods to do impl- and proxy-specific |
| 71 // work. |
| 72 virtual int32_t InternalEnumerateDevices(PP_Resource* devices, |
| 73 PP_CompletionCallback callback) = 0; |
| 74 virtual int32_t InternalOpen(const std::string& device_id, |
| 75 PP_AudioSampleRate sample_rate, |
| 76 uint32_t sample_frame_count, |
| 77 PP_CompletionCallback callback) = 0; |
| 78 virtual PP_Bool InternalStartCapture() = 0; |
| 79 virtual PP_Bool InternalStopCapture() = 0; |
| 80 virtual void InternalClose() = 0; |
| 81 |
| 82 // Configures the current state to be capturing or not. The caller is |
| 38 // responsible for ensuring the new state is the opposite of the current one. | 83 // responsible for ensuring the new state is the opposite of the current one. |
| 39 // | 84 // |
| 40 // This is the implementation for PPB_AudioInput.Start/StopCapture, except | 85 // This is the implementation for PPB_AudioInput.Start/StopCapture, except |
| 41 // that it does not actually notify the audio system to stop capture, it just | 86 // that it does not actually notify the audio system to stop capture, it just |
| 42 // configures our object to stop generating callbacks. The actual stop | 87 // configures our object to stop generating callbacks. The actual stop |
| 43 // playback request will be done in the derived classes and will be different | 88 // capture request will be done in the derived classes and will be different |
| 44 // from the proxy and the renderer. | 89 // from the proxy and the renderer. |
| 45 void SetStartCaptureState(); | 90 void SetStartCaptureState(); |
| 46 void SetStopCaptureState(); | 91 void SetStopCaptureState(); |
| 47 | 92 |
| 48 // Sets the shared memory and socket handles. This will automatically start | 93 // Sets the shared memory and socket handles. This will automatically start |
| 49 // playback if we're currently set to play. | 94 // capture if we're currently set to capture. |
| 50 void SetStreamInfo(base::SharedMemoryHandle shared_memory_handle, | 95 void SetStreamInfo(base::SharedMemoryHandle shared_memory_handle, |
| 51 size_t shared_memory_size, | 96 size_t shared_memory_size, |
| 52 base::SyncSocket::Handle socket_handle); | 97 base::SyncSocket::Handle socket_handle); |
| 53 | 98 |
| 54 private: | |
| 55 // Starts execution of the audio input thread. | 99 // Starts execution of the audio input thread. |
| 56 void StartThread(); | 100 void StartThread(); |
| 57 | 101 |
| 58 // DelegateSimpleThread::Delegate implementation. | 102 // DelegateSimpleThread::Delegate implementation. |
| 59 // Run on the audio input thread. | 103 // Run on the audio input thread. |
| 60 virtual void Run(); | 104 virtual void Run(); |
| 61 | 105 |
| 106 // The common implementation of OpenTrusted() and Open(). It will call |
| 107 // InternalOpen() to do impl- and proxy-specific work. |
| 108 // OpenTrusted() will call this methods with a NULL |audio_input_callback|, |
| 109 // in this case, the thread will not be run. This non-callback mode is used in |
| 110 // the renderer with the proxy, since the proxy handles the callback entirely |
| 111 // within the plugin process. |
| 112 int32_t CommonOpen(const std::string& device_id, |
| 113 PP_Resource config, |
| 114 PPB_AudioInput_Callback audio_input_callback, |
| 115 void* user_data, |
| 116 PP_CompletionCallback callback); |
| 117 |
| 118 OpenState open_state_; |
| 119 |
| 62 // True if capturing the stream. | 120 // True if capturing the stream. |
| 63 bool capturing_; | 121 bool capturing_; |
| 64 | 122 |
| 65 // Socket used to notify us when audio is ready to accept new samples. This | 123 // Socket used to notify us when new samples are available. This pointer is |
| 66 // pointer is created in StreamCreated(). | 124 // created in SetStreamInfo(). |
| 67 scoped_ptr<base::SyncSocket> socket_; | 125 scoped_ptr<base::SyncSocket> socket_; |
| 68 | 126 |
| 69 // Sample buffer in shared memory. This pointer is created in | 127 // Sample buffer in shared memory. This pointer is created in |
| 70 // StreamCreated(). The memory is only mapped when the audio thread is | 128 // SetStreamInfo(). The memory is only mapped when the audio thread is |
| 71 // created. | 129 // created. |
| 72 scoped_ptr<base::SharedMemory> shared_memory_; | 130 scoped_ptr<base::SharedMemory> shared_memory_; |
| 73 | 131 |
| 74 // The size of the sample buffer in bytes. | 132 // The size of the sample buffer in bytes. |
| 75 size_t shared_memory_size_; | 133 size_t shared_memory_size_; |
| 76 | 134 |
| 77 // When the callback is set, this thread is spawned for calling it. | 135 // When the callback is set, this thread is spawned for calling it. |
| 78 scoped_ptr<base::DelegateSimpleThread> audio_input_thread_; | 136 scoped_ptr<base::DelegateSimpleThread> audio_input_thread_; |
| 79 | 137 |
| 80 // Callback to call when audio is ready to produce new samples. | 138 // Callback to call when new samples are available. |
| 81 PPB_AudioInput_Callback callback_; | 139 PPB_AudioInput_Callback audio_input_callback_; |
| 82 | 140 |
| 83 // User data pointer passed verbatim to the callback function. | 141 // User data pointer passed verbatim to the callback function. |
| 84 void* user_data_; | 142 void* user_data_; |
| 85 | 143 |
| 144 scoped_refptr<TrackedCallback> enumerate_devices_callback_; |
| 145 scoped_refptr<TrackedCallback> open_callback_; |
| 146 |
| 147 // Owning reference to the current config object. This isn't actually used, |
| 148 // we just dish it out as requested by the plugin. |
| 149 ScopedPPResource config_; |
| 150 |
| 151 // Output parameter of EnumerateDevices(). It should not be accessed after |
| 152 // |enumerate_devices_callback_| is run. |
| 153 PP_Resource* devices_; |
| 154 |
| 155 ResourceObjectType resource_object_type_; |
| 156 |
| 86 DISALLOW_COPY_AND_ASSIGN(PPB_AudioInput_Shared); | 157 DISALLOW_COPY_AND_ASSIGN(PPB_AudioInput_Shared); |
| 87 }; | 158 }; |
| 88 | 159 |
| 89 } // namespace ppapi | 160 } // namespace ppapi |
| 90 | 161 |
| 91 #endif // PPAPI_SHARED_IMPL_PPB_AUDIO_INPUT_SHARED_H_ | 162 #endif // PPAPI_SHARED_IMPL_PPB_AUDIO_INPUT_SHARED_H_ |
| OLD | NEW |