summaryrefslogtreecommitdiffstats
path: root/webkit/plugins/ppapi/ppb_audio_impl.cc
blob: 54de09ca418379aa40e4ef8d8329c75f8a6e0bc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "webkit/plugins/ppapi/ppb_audio_impl.h"

#include "base/logging.h"
#include "ppapi/c/pp_completion_callback.h"
#include "ppapi/c/ppb_audio.h"
#include "ppapi/c/ppb_audio_config.h"
#include "ppapi/c/trusted/ppb_audio_trusted.h"
#include "webkit/plugins/ppapi/common.h"

namespace webkit {
namespace ppapi {

namespace {

// PPB_AudioConfig -------------------------------------------------------------

uint32_t RecommendSampleFrameCount(PP_AudioSampleRate sample_rate,
                                   uint32_t requested_sample_frame_count);

PP_Resource CreateStereo16bit(PP_Instance instance_id,
                              PP_AudioSampleRate sample_rate,
                              uint32_t sample_frame_count) {
  PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id);
  if (!instance)
    return 0;

  // TODO(brettw): Currently we don't actually check what the hardware
  // supports, so just allow sample rates of the "guaranteed working" ones.
  if (sample_rate != PP_AUDIOSAMPLERATE_44100 &&
      sample_rate != PP_AUDIOSAMPLERATE_48000)
    return 0;

  // TODO(brettw): Currently we don't actually query to get a value from the
  // hardware, so just validate the range.
  if (RecommendSampleFrameCount(sample_rate, sample_frame_count) !=
      sample_frame_count)
    return 0;

  scoped_refptr<PPB_AudioConfig_Impl> config(
      new PPB_AudioConfig_Impl(instance, sample_rate, sample_frame_count));
  return config->GetReference();
}

uint32_t RecommendSampleFrameCount(PP_AudioSampleRate sample_rate,
                                   uint32_t requested_sample_frame_count) {
  // TODO(brettw) Currently we don't actually query to get a value from the
  // hardware, so we always return the input for in-range values.
  //
  // Danger: this code is duplicated in the audio config proxy.
  if (requested_sample_frame_count < PP_AUDIOMINSAMPLEFRAMECOUNT)
    return PP_AUDIOMINSAMPLEFRAMECOUNT;
  if (requested_sample_frame_count > PP_AUDIOMAXSAMPLEFRAMECOUNT)
    return PP_AUDIOMAXSAMPLEFRAMECOUNT;
  return requested_sample_frame_count;
}

PP_Bool IsAudioConfig(PP_Resource resource) {
  scoped_refptr<PPB_AudioConfig_Impl> config =
      Resource::GetAs<PPB_AudioConfig_Impl>(resource);
  return BoolToPPBool(!!config);
}

PP_AudioSampleRate GetSampleRate(PP_Resource config_id) {
  scoped_refptr<PPB_AudioConfig_Impl> config =
      Resource::GetAs<PPB_AudioConfig_Impl>(config_id);
  return config ? config->sample_rate() : PP_AUDIOSAMPLERATE_NONE;
}

uint32_t GetSampleFrameCount(PP_Resource config_id) {
  scoped_refptr<PPB_AudioConfig_Impl> config =
      Resource::GetAs<PPB_AudioConfig_Impl>(config_id);
  return config ? config->sample_frame_count() : 0;
}

const PPB_AudioConfig ppb_audioconfig = {
  &CreateStereo16bit,
  &RecommendSampleFrameCount,
  &IsAudioConfig,
  &GetSampleRate,
  &GetSampleFrameCount
};

// PPB_Audio -------------------------------------------------------------------

PP_Resource Create(PP_Instance instance_id, PP_Resource config_id,
                   PPB_Audio_Callback user_callback, void* user_data) {
  PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id);
  if (!instance)
    return 0;
  if (!user_callback)
    return 0;
  scoped_refptr<PPB_Audio_Impl> audio(new PPB_Audio_Impl(instance));
  if (!audio->Init(instance->delegate(), config_id,
                   user_callback, user_data))
    return 0;
  return audio->GetReference();
}

PP_Bool IsAudio(PP_Resource resource) {
  scoped_refptr<PPB_Audio_Impl> audio =
      Resource::GetAs<PPB_Audio_Impl>(resource);
  return BoolToPPBool(!!audio);
}

PP_Resource GetCurrentConfig(PP_Resource audio_id) {
  scoped_refptr<PPB_Audio_Impl> audio =
      Resource::GetAs<PPB_Audio_Impl>(audio_id);
  return audio ? audio->GetCurrentConfig() : 0;
}

PP_Bool StartPlayback(PP_Resource audio_id) {
  scoped_refptr<PPB_Audio_Impl> audio =
      Resource::GetAs<PPB_Audio_Impl>(audio_id);
  return audio ? BoolToPPBool(audio->StartPlayback()) : PP_FALSE;
}

PP_Bool StopPlayback(PP_Resource audio_id) {
  scoped_refptr<PPB_Audio_Impl> audio =
      Resource::GetAs<PPB_Audio_Impl>(audio_id);
  return audio ? BoolToPPBool(audio->StopPlayback()) : PP_FALSE;
}

const PPB_Audio ppb_audio = {
  &Create,
  &IsAudio,
  &GetCurrentConfig,
  &StartPlayback,
  &StopPlayback,
};

// PPB_AudioTrusted ------------------------------------------------------------

PP_Resource CreateTrusted(PP_Instance instance_id) {
  PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id);
  if (!instance)
    return 0;
  scoped_refptr<PPB_Audio_Impl> audio(new PPB_Audio_Impl(instance));
  return audio->GetReference();
}

int32_t Open(PP_Resource audio_id,
             PP_Resource config_id,
             PP_CompletionCallback created) {
  scoped_refptr<PPB_Audio_Impl> audio =
      Resource::GetAs<PPB_Audio_Impl>(audio_id);
  if (!audio)
    return PP_ERROR_BADRESOURCE;
  if (!created.func)
    return PP_ERROR_BADARGUMENT;
  return audio->Open(audio->instance()->delegate(), config_id, created);
}

int32_t GetSyncSocket(PP_Resource audio_id, int* sync_socket) {
  scoped_refptr<PPB_Audio_Impl> audio =
      Resource::GetAs<PPB_Audio_Impl>(audio_id);
  if (audio)
    return audio->GetSyncSocket(sync_socket);
  return PP_ERROR_BADRESOURCE;
}

int32_t GetSharedMemory(PP_Resource audio_id,
                        int* shm_handle,
                        uint32_t* shm_size) {
  scoped_refptr<PPB_Audio_Impl> audio =
      Resource::GetAs<PPB_Audio_Impl>(audio_id);
  if (audio)
    return audio->GetSharedMemory(shm_handle, shm_size);
  return PP_ERROR_BADRESOURCE;
}

const PPB_AudioTrusted ppb_audiotrusted = {
  &CreateTrusted,
  &Open,
  &GetSyncSocket,
  &GetSharedMemory,
};

}  // namespace

// PPB_AudioConfig_Impl --------------------------------------------------------

PPB_AudioConfig_Impl::PPB_AudioConfig_Impl(
    PluginInstance* instance,
    PP_AudioSampleRate sample_rate,
    uint32_t sample_frame_count)
    : Resource(instance),
      sample_rate_(sample_rate),
      sample_frame_count_(sample_frame_count) {
}

const PPB_AudioConfig* PPB_AudioConfig_Impl::GetInterface() {
  return &ppb_audioconfig;
}

size_t PPB_AudioConfig_Impl::BufferSize() {
  // TODO(audio): as more options become available, we'll need to
  // have additional code here to correctly calculate the size in
  // bytes of an audio buffer.  For now, only support two channel
  // int16_t sample buffers.
  const int kChannels = 2;
  const int kSizeOfSample = sizeof(int16_t);
  return static_cast<size_t>(sample_frame_count_ * kSizeOfSample * kChannels);
}

PPB_AudioConfig_Impl* PPB_AudioConfig_Impl::AsPPB_AudioConfig_Impl() {
  return this;
}

// PPB_Audio_Impl --------------------------------------------------------------

PPB_Audio_Impl::PPB_Audio_Impl(PluginInstance* instance)
    : Resource(instance),
      audio_(NULL),
      create_callback_pending_(false) {
  create_callback_ = PP_MakeCompletionCallback(NULL, NULL);
}

PPB_Audio_Impl::~PPB_Audio_Impl() {
  // Calling ShutDown() makes sure StreamCreated cannot be called anymore and
  // releases the audio data associated with the pointer. Note however, that
  // until ShutDown returns, StreamCreated may still be called. This will be
  // OK since we'll just immediately clean up the data it stored later in this
  // destructor.
  if (audio_) {
    audio_->ShutDown();
    audio_ = NULL;
  }

  // If the completion callback hasn't fired yet, do so here
  // with an error condition.
  if (create_callback_pending_) {
    PP_RunCompletionCallback(&create_callback_, PP_ERROR_ABORTED);
    create_callback_pending_ = false;
  }
}

const PPB_Audio* PPB_Audio_Impl::GetInterface() {
  return &ppb_audio;
}

const PPB_AudioTrusted* PPB_Audio_Impl::GetTrustedInterface() {
  return &ppb_audiotrusted;
}

PPB_Audio_Impl* PPB_Audio_Impl::AsPPB_Audio_Impl() {
  return this;
}

bool PPB_Audio_Impl::Init(PluginDelegate* plugin_delegate,
                          PP_Resource config_id,
                          PPB_Audio_Callback callback, void* user_data) {
  CHECK(!audio_);
  config_ = Resource::GetAs<PPB_AudioConfig_Impl>(config_id);
  if (!config_)
    return false;
  SetCallback(callback, user_data);

  // When the stream is created, we'll get called back on StreamCreated().
  audio_ = plugin_delegate->CreateAudio(config_->sample_rate(),
                                        config_->sample_frame_count(),
                                        this);
  return audio_ != NULL;
}

PP_Resource PPB_Audio_Impl::GetCurrentConfig() {
  return config_->GetReference();
}

bool PPB_Audio_Impl::StartPlayback() {
  if (!audio_)
    return false;
  if (playing())
    return true;
  SetStartPlaybackState();
  return audio_->StartPlayback();
}

bool PPB_Audio_Impl::StopPlayback() {
  if (!audio_)
    return false;
  if (!playing())
    return true;
  if (!audio_->StopPlayback())
    return false;
  SetStopPlaybackState();
  return true;
}

int32_t PPB_Audio_Impl::Open(PluginDelegate* plugin_delegate,
                             PP_Resource config_id,
                             PP_CompletionCallback create_callback) {
  DCHECK(!audio_);
  config_ = Resource::GetAs<PPB_AudioConfig_Impl>(config_id);
  if (!config_)
    return PP_ERROR_BADRESOURCE;

  // When the stream is created, we'll get called back on StreamCreated().
  audio_ = plugin_delegate->CreateAudio(config_->sample_rate(),
                                        config_->sample_frame_count(),
                                        this);
  if (!audio_)
    return PP_ERROR_FAILED;

  // At this point, we are guaranteeing ownership of the completion
  // callback.  Audio promises to fire the completion callback
  // once and only once.
  create_callback_ = create_callback;
  create_callback_pending_ = true;
  return PP_OK_COMPLETIONPENDING;
}

int32_t PPB_Audio_Impl::GetSyncSocket(int* sync_socket) {
  if (socket_for_create_callback_.get()) {
#if defined(OS_POSIX)
    *sync_socket = socket_for_create_callback_->handle();
#elif defined(OS_WIN)
    *sync_socket = reinterpret_cast<int>(socket_for_create_callback_->handle());
#else
    #error "Platform not supported."
#endif
    return PP_OK;
  }
  return PP_ERROR_FAILED;
}

int32_t PPB_Audio_Impl::GetSharedMemory(int* shm_handle, uint32_t* shm_size) {
  if (shared_memory_for_create_callback_.get()) {
#if defined(OS_POSIX)
    *shm_handle = shared_memory_for_create_callback_->handle().fd;
#elif defined(OS_WIN)
    *shm_handle = reinterpret_cast<int>(
        shared_memory_for_create_callback_->handle());
#else
    #error "Platform not supported."
#endif
    *shm_size = shared_memory_size_for_create_callback_;
    return PP_OK;
  }
  return PP_ERROR_FAILED;
}

void PPB_Audio_Impl::StreamCreated(
    base::SharedMemoryHandle shared_memory_handle,
    size_t shared_memory_size,
    base::SyncSocket::Handle socket_handle) {
  if (create_callback_pending_) {
    // Trusted side of proxy can specify a callback to recieve handles. In
    // this case we don't need to map any data or start the thread since it
    // will be handled by the proxy.
    shared_memory_for_create_callback_.reset(
        new base::SharedMemory(shared_memory_handle, false));
    shared_memory_size_for_create_callback_ = shared_memory_size;
    socket_for_create_callback_.reset(new base::SyncSocket(socket_handle));

    PP_RunCompletionCallback(&create_callback_, 0);
    create_callback_pending_ = false;

    // It might be nice to close the handles here to free up some system
    // resources, but we can't since there's a race condition. The handles must
    // be valid until they're sent over IPC, which is done from the I/O thread
    // which will often get done after this code executes. We could do
    // something more elaborate like an ACK from the plugin or post a task to
    // the I/O thread and back, but this extra complexity doesn't seem worth it
    // just to clean up these handles faster.
  } else {
    SetStreamInfo(shared_memory_handle, shared_memory_size, socket_handle);
  }
}

}  // namespace ppapi
}  // namespace webkit