Home | History | Annotate | Download | only in pepper
      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 #include "content/renderer/pepper/audio_helper.h"
      6 
      7 #include "base/logging.h"
      8 #include "content/renderer/pepper/common.h"
      9 #include "ppapi/c/pp_completion_callback.h"
     10 #include "ppapi/c/pp_errors.h"
     11 
     12 using ppapi::TrackedCallback;
     13 
     14 namespace content {
     15 
     16 // AudioHelper -----------------------------------------------------------------
     17 
     18 AudioHelper::AudioHelper() : shared_memory_size_for_create_callback_(0) {}
     19 
     20 AudioHelper::~AudioHelper() {}
     21 
     22 int32_t AudioHelper::GetSyncSocketImpl(int* sync_socket) {
     23   if (socket_for_create_callback_) {
     24 #if defined(OS_POSIX)
     25     *sync_socket = socket_for_create_callback_->handle();
     26 #elif defined(OS_WIN)
     27     *sync_socket = reinterpret_cast<int>(socket_for_create_callback_->handle());
     28 #else
     29 #error "Platform not supported."
     30 #endif
     31     return PP_OK;
     32   }
     33   return PP_ERROR_FAILED;
     34 }
     35 
     36 int32_t AudioHelper::GetSharedMemoryImpl(int* shm_handle, uint32_t* shm_size) {
     37   if (shared_memory_for_create_callback_) {
     38 #if defined(OS_POSIX)
     39     *shm_handle = shared_memory_for_create_callback_->handle().fd;
     40 #elif defined(OS_WIN)
     41     *shm_handle =
     42         reinterpret_cast<int>(shared_memory_for_create_callback_->handle());
     43 #else
     44 #error "Platform not supported."
     45 #endif
     46     *shm_size = shared_memory_size_for_create_callback_;
     47     return PP_OK;
     48   }
     49   return PP_ERROR_FAILED;
     50 }
     51 
     52 void AudioHelper::StreamCreated(base::SharedMemoryHandle shared_memory_handle,
     53                                 size_t shared_memory_size,
     54                                 base::SyncSocket::Handle socket_handle) {
     55   if (TrackedCallback::IsPending(create_callback_)) {
     56     // Trusted side of proxy can specify a callback to receive handles. In
     57     // this case we don't need to map any data or start the thread since it
     58     // will be handled by the proxy.
     59     shared_memory_for_create_callback_.reset(
     60         new base::SharedMemory(shared_memory_handle, false));
     61     shared_memory_size_for_create_callback_ = shared_memory_size;
     62     socket_for_create_callback_.reset(new base::SyncSocket(socket_handle));
     63 
     64     create_callback_->Run(PP_OK);
     65 
     66     // It might be nice to close the handles here to free up some system
     67     // resources, but we can't since there's a race condition. The handles must
     68     // be valid until they're sent over IPC, which is done from the I/O thread
     69     // which will often get done after this code executes. We could do
     70     // something more elaborate like an ACK from the plugin or post a task to
     71     // the I/O thread and back, but this extra complexity doesn't seem worth it
     72     // just to clean up these handles faster.
     73   } else {
     74     OnSetStreamInfo(shared_memory_handle, shared_memory_size, socket_handle);
     75   }
     76 }
     77 
     78 void AudioHelper::SetCreateCallback(
     79     scoped_refptr<ppapi::TrackedCallback> create_callback) {
     80   DCHECK(!TrackedCallback::IsPending(create_callback_));
     81   create_callback_ = create_callback;
     82 }
     83 
     84 }  // namespace content
     85