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