Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: webkit/glue/plugins/pepper_audio.cc

Issue 5828003: Move the Pepper implementation from webkit/glue/plugins/pepper_* to... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « webkit/glue/plugins/pepper_audio.h ('k') | webkit/glue/plugins/pepper_buffer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2010 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 #include "webkit/glue/plugins/pepper_audio.h"
6
7 #include "base/logging.h"
8 #include "ppapi/c/dev/ppb_audio_dev.h"
9 #include "ppapi/c/dev/ppb_audio_trusted_dev.h"
10 #include "ppapi/c/pp_completion_callback.h"
11 #include "webkit/glue/plugins/pepper_common.h"
12
13 namespace pepper {
14
15 namespace {
16
17 // PPB_AudioConfig -------------------------------------------------------------
18
19 uint32_t RecommendSampleFrameCount(uint32_t requested_sample_frame_count);
20
21 PP_Resource CreateStereo16bit(PP_Module module_id,
22 PP_AudioSampleRate_Dev sample_rate,
23 uint32_t sample_frame_count) {
24 PluginModule* module = ResourceTracker::Get()->GetModule(module_id);
25 if (!module)
26 return 0;
27
28 // TODO(brettw): Currently we don't actually check what the hardware
29 // supports, so just allow sample rates of the "guaranteed working" ones.
30 if (sample_rate != PP_AUDIOSAMPLERATE_44100 &&
31 sample_rate != PP_AUDIOSAMPLERATE_48000)
32 return 0;
33
34 // TODO(brettw): Currently we don't actually query to get a value from the
35 // hardware, so just validate the range.
36 if (RecommendSampleFrameCount(sample_frame_count) != sample_frame_count)
37 return 0;
38
39 scoped_refptr<AudioConfig> config(new AudioConfig(module,
40 sample_rate,
41 sample_frame_count));
42 return config->GetReference();
43 }
44
45 uint32_t RecommendSampleFrameCount(uint32_t requested_sample_frame_count) {
46 // TODO(brettw) Currently we don't actually query to get a value from the
47 // hardware, so we always return the input for in-range values.
48 if (requested_sample_frame_count < PP_AUDIOMINSAMPLEFRAMECOUNT)
49 return PP_AUDIOMINSAMPLEFRAMECOUNT;
50 if (requested_sample_frame_count > PP_AUDIOMAXSAMPLEFRAMECOUNT)
51 return PP_AUDIOMAXSAMPLEFRAMECOUNT;
52 return requested_sample_frame_count;
53 }
54
55 PP_Bool IsAudioConfig(PP_Resource resource) {
56 scoped_refptr<AudioConfig> config = Resource::GetAs<AudioConfig>(resource);
57 return BoolToPPBool(!!config);
58 }
59
60 PP_AudioSampleRate_Dev GetSampleRate(PP_Resource config_id) {
61 scoped_refptr<AudioConfig> config = Resource::GetAs<AudioConfig>(config_id);
62 return config ? config->sample_rate() : PP_AUDIOSAMPLERATE_NONE;
63 }
64
65 uint32_t GetSampleFrameCount(PP_Resource config_id) {
66 scoped_refptr<AudioConfig> config = Resource::GetAs<AudioConfig>(config_id);
67 return config ? config->sample_frame_count() : 0;
68 }
69
70 const PPB_AudioConfig_Dev ppb_audioconfig = {
71 &CreateStereo16bit,
72 &RecommendSampleFrameCount,
73 &IsAudioConfig,
74 &GetSampleRate,
75 &GetSampleFrameCount
76 };
77
78 // PPB_Audio -------------------------------------------------------------------
79
80 PP_Resource Create(PP_Instance instance_id, PP_Resource config_id,
81 PPB_Audio_Callback user_callback, void* user_data) {
82 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id);
83 if (!instance)
84 return 0;
85 if (!user_callback)
86 return 0;
87 scoped_refptr<Audio> audio(new Audio(instance->module(), instance_id));
88 if (!audio->Init(instance->delegate(), config_id,
89 user_callback, user_data))
90 return 0;
91 return audio->GetReference();
92 }
93
94 PP_Bool IsAudio(PP_Resource resource) {
95 scoped_refptr<Audio> audio = Resource::GetAs<Audio>(resource);
96 return BoolToPPBool(!!audio);
97 }
98
99 PP_Resource GetCurrentConfig(PP_Resource audio_id) {
100 scoped_refptr<Audio> audio = Resource::GetAs<Audio>(audio_id);
101 return audio ? audio->GetCurrentConfig() : 0;
102 }
103
104 PP_Bool StartPlayback(PP_Resource audio_id) {
105 scoped_refptr<Audio> audio = Resource::GetAs<Audio>(audio_id);
106 return audio ? BoolToPPBool(audio->StartPlayback()) : PP_FALSE;
107 }
108
109 PP_Bool StopPlayback(PP_Resource audio_id) {
110 scoped_refptr<Audio> audio = Resource::GetAs<Audio>(audio_id);
111 return audio ? BoolToPPBool(audio->StopPlayback()) : PP_FALSE;
112 }
113
114 const PPB_Audio_Dev ppb_audio = {
115 &Create,
116 &IsAudio,
117 &GetCurrentConfig,
118 &StartPlayback,
119 &StopPlayback,
120 };
121
122 // PPB_AudioTrusted ------------------------------------------------------------
123
124 PP_Resource CreateTrusted(PP_Instance instance_id) {
125 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id);
126 if (!instance)
127 return 0;
128 scoped_refptr<Audio> audio(new Audio(instance->module(), instance_id));
129 return audio->GetReference();
130 }
131
132 int32_t Open(PP_Resource audio_id,
133 PP_Resource config_id,
134 PP_CompletionCallback created) {
135 scoped_refptr<Audio> audio = Resource::GetAs<Audio>(audio_id);
136 if (!audio)
137 return PP_ERROR_BADRESOURCE;
138 if (!created.func)
139 return PP_ERROR_BADARGUMENT;
140 PP_Instance instance_id = audio->pp_instance();
141 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id);
142 if (!instance)
143 return PP_ERROR_FAILED;
144 return audio->Open(instance->delegate(), config_id, created);
145 }
146
147 int32_t GetSyncSocket(PP_Resource audio_id, int* sync_socket) {
148 scoped_refptr<Audio> audio = Resource::GetAs<Audio>(audio_id);
149 if (audio)
150 return audio->GetSyncSocket(sync_socket);
151 return PP_ERROR_BADRESOURCE;
152 }
153
154 int32_t GetSharedMemory(PP_Resource audio_id,
155 int* shm_handle,
156 uint32_t* shm_size) {
157 scoped_refptr<Audio> audio = Resource::GetAs<Audio>(audio_id);
158 if (audio)
159 return audio->GetSharedMemory(shm_handle, shm_size);
160 return PP_ERROR_BADRESOURCE;
161 }
162
163 const PPB_AudioTrusted_Dev ppb_audiotrusted = {
164 &CreateTrusted,
165 &Open,
166 &GetSyncSocket,
167 &GetSharedMemory,
168 };
169
170 } // namespace
171
172 // AudioConfig -----------------------------------------------------------------
173
174 AudioConfig::AudioConfig(PluginModule* module,
175 PP_AudioSampleRate_Dev sample_rate,
176 uint32_t sample_frame_count)
177 : Resource(module),
178 sample_rate_(sample_rate),
179 sample_frame_count_(sample_frame_count) {
180 }
181
182 const PPB_AudioConfig_Dev* AudioConfig::GetInterface() {
183 return &ppb_audioconfig;
184 }
185
186 size_t AudioConfig::BufferSize() {
187 // TODO(audio): as more options become available, we'll need to
188 // have additional code here to correctly calculate the size in
189 // bytes of an audio buffer. For now, only support two channel
190 // int16_t sample buffers.
191 const int kChannels = 2;
192 const int kSizeOfSample = sizeof(int16_t);
193 return static_cast<size_t>(sample_frame_count_ * kSizeOfSample * kChannels);
194 }
195
196 AudioConfig* AudioConfig::AsAudioConfig() {
197 return this;
198 }
199
200 // Audio -----------------------------------------------------------------------
201
202 Audio::Audio(PluginModule* module, PP_Instance instance_id)
203 : Resource(module),
204 pp_instance_(instance_id),
205 audio_(NULL),
206 create_callback_pending_(false) {
207 create_callback_ = PP_MakeCompletionCallback(NULL, NULL);
208 }
209
210 Audio::~Audio() {
211 // Calling ShutDown() makes sure StreamCreated cannot be called anymore.
212 audio_->ShutDown();
213 audio_ = NULL;
214
215 // If the completion callback hasn't fired yet, do so here
216 // with an error condition.
217 if (create_callback_pending_) {
218 PP_RunCompletionCallback(&create_callback_, PP_ERROR_ABORTED);
219 create_callback_pending_ = false;
220 }
221 }
222
223 const PPB_Audio_Dev* Audio::GetInterface() {
224 return &ppb_audio;
225 }
226
227 const PPB_AudioTrusted_Dev* Audio::GetTrustedInterface() {
228 return &ppb_audiotrusted;
229 }
230
231 Audio* Audio::AsAudio() {
232 return this;
233 }
234
235 bool Audio::Init(PluginDelegate* plugin_delegate,
236 PP_Resource config_id,
237 PPB_Audio_Callback callback, void* user_data) {
238 CHECK(!audio_);
239 config_ = Resource::GetAs<AudioConfig>(config_id);
240 if (!config_)
241 return false;
242 SetCallback(callback, user_data);
243
244 // When the stream is created, we'll get called back on StreamCreated().
245 audio_ = plugin_delegate->CreateAudio(config_->sample_rate(),
246 config_->sample_frame_count(),
247 this);
248 return audio_ != NULL;
249 }
250
251 PP_Resource Audio::GetCurrentConfig() {
252 return config_->GetReference();
253 }
254
255 bool Audio::StartPlayback() {
256 if (playing())
257 return true;
258 SetStartPlaybackState();
259 return audio_->StartPlayback();
260 }
261
262 bool Audio::StopPlayback() {
263 if (!playing())
264 return true;
265 if (!audio_->StopPlayback())
266 return false;
267 SetStopPlaybackState();
268 return true;
269 }
270
271 int32_t Audio::Open(PluginDelegate* plugin_delegate,
272 PP_Resource config_id,
273 PP_CompletionCallback create_callback) {
274 DCHECK(!audio_);
275 config_ = Resource::GetAs<AudioConfig>(config_id);
276 if (!config_)
277 return PP_ERROR_BADRESOURCE;
278
279 // When the stream is created, we'll get called back on StreamCreated().
280 audio_ = plugin_delegate->CreateAudio(config_->sample_rate(),
281 config_->sample_frame_count(),
282 this);
283 if (!audio_)
284 return PP_ERROR_FAILED;
285
286 // At this point, we are guaranteeing ownership of the completion
287 // callback. Audio promises to fire the completion callback
288 // once and only once.
289 create_callback_ = create_callback;
290 create_callback_pending_ = true;
291 return PP_ERROR_WOULDBLOCK;
292 }
293
294 int32_t Audio::GetSyncSocket(int* sync_socket) {
295 if (socket_for_create_callback_.get()) {
296 #if defined(OS_POSIX)
297 *sync_socket = socket_for_create_callback_->handle();
298 #elif defined(OS_WIN)
299 *sync_socket = reinterpret_cast<int>(socket_for_create_callback_->handle());
300 #else
301 #error "Platform not supported."
302 #endif
303 return PP_OK;
304 }
305 return PP_ERROR_FAILED;
306 }
307
308 int32_t Audio::GetSharedMemory(int* shm_handle, uint32_t* shm_size) {
309 if (shared_memory_for_create_callback_.get()) {
310 #if defined(OS_POSIX)
311 *shm_handle = shared_memory_for_create_callback_->handle().fd;
312 #elif defined(OS_WIN)
313 *shm_handle = reinterpret_cast<int>(
314 shared_memory_for_create_callback_->handle());
315 #else
316 #error "Platform not supported."
317 #endif
318 *shm_size = shared_memory_size_for_create_callback_;
319 return PP_OK;
320 }
321 return PP_ERROR_FAILED;
322 }
323
324 void Audio::StreamCreated(base::SharedMemoryHandle shared_memory_handle,
325 size_t shared_memory_size,
326 base::SyncSocket::Handle socket_handle) {
327 if (create_callback_pending_) {
328 // Trusted side of proxy can specify a callback to recieve handles. In
329 // this case we don't need to map any data or start the thread since it
330 // will be handled by the proxy.
331 shared_memory_for_create_callback_.reset(
332 new base::SharedMemory(shared_memory_handle, false));
333 shared_memory_size_for_create_callback_ = shared_memory_size;
334 socket_for_create_callback_.reset(new base::SyncSocket(socket_handle));
335
336 PP_RunCompletionCallback(&create_callback_, 0);
337 create_callback_pending_ = false;
338
339 // It might be nice to close the handles here to free up some system
340 // resources, but we can't since there's a race condition. The handles must
341 // be valid until they're sent over IPC, which is done from the I/O thread
342 // which will often get done after this code executes. We could do
343 // something more elaborate like an ACK from the plugin or post a task to
344 // the I/O thread and back, but this extra complexity doesn't seem worth it
345 // just to clean up these handles faster.
346 } else {
347 SetStreamInfo(shared_memory_handle, shared_memory_size, socket_handle);
348 }
349 }
350
351 } // namespace pepper
OLDNEW
« no previous file with comments | « webkit/glue/plugins/pepper_audio.h ('k') | webkit/glue/plugins/pepper_buffer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698