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/pepper_audio_input_host.h"
      6 
      7 #include "base/logging.h"
      8 #include "build/build_config.h"
      9 #include "content/renderer/pepper/pepper_media_device_manager.h"
     10 #include "content/renderer/pepper/pepper_platform_audio_input.h"
     11 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
     12 #include "content/renderer/pepper/renderer_ppapi_host_impl.h"
     13 #include "content/renderer/render_view_impl.h"
     14 #include "ipc/ipc_message.h"
     15 #include "ppapi/c/pp_errors.h"
     16 #include "ppapi/host/dispatch_host_message.h"
     17 #include "ppapi/host/ppapi_host.h"
     18 #include "ppapi/proxy/ppapi_messages.h"
     19 #include "ppapi/proxy/serialized_structs.h"
     20 
     21 namespace content {
     22 
     23 namespace {
     24 
     25 base::PlatformFile ConvertSyncSocketHandle(const base::SyncSocket& socket) {
     26   return socket.handle();
     27 }
     28 
     29 base::PlatformFile ConvertSharedMemoryHandle(
     30     const base::SharedMemory& shared_memory) {
     31 #if defined(OS_POSIX)
     32   return shared_memory.handle().fd;
     33 #elif defined(OS_WIN)
     34   return shared_memory.handle();
     35 #else
     36 #error "Platform not supported."
     37 #endif
     38 }
     39 
     40 }  // namespace
     41 
     42 PepperAudioInputHost::PepperAudioInputHost(RendererPpapiHostImpl* host,
     43                                            PP_Instance instance,
     44                                            PP_Resource resource)
     45     : ResourceHost(host->GetPpapiHost(), instance, resource),
     46       renderer_ppapi_host_(host),
     47       audio_input_(NULL),
     48       enumeration_helper_(this,
     49                           PepperMediaDeviceManager::GetForRenderView(
     50                               host->GetRenderViewForInstance(pp_instance())),
     51                           PP_DEVICETYPE_DEV_AUDIOCAPTURE,
     52                           host->GetDocumentURL(instance)) {}
     53 
     54 PepperAudioInputHost::~PepperAudioInputHost() { Close(); }
     55 
     56 int32_t PepperAudioInputHost::OnResourceMessageReceived(
     57     const IPC::Message& msg,
     58     ppapi::host::HostMessageContext* context) {
     59   int32_t result = PP_ERROR_FAILED;
     60   if (enumeration_helper_.HandleResourceMessage(msg, context, &result))
     61     return result;
     62 
     63   PPAPI_BEGIN_MESSAGE_MAP(PepperAudioInputHost, msg)
     64     PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_AudioInput_Open, OnOpen)
     65     PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_AudioInput_StartOrStop,
     66                                       OnStartOrStop)
     67     PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_AudioInput_Close, OnClose)
     68   PPAPI_END_MESSAGE_MAP()
     69   return PP_ERROR_FAILED;
     70 }
     71 
     72 void PepperAudioInputHost::StreamCreated(
     73     base::SharedMemoryHandle shared_memory_handle,
     74     size_t shared_memory_size,
     75     base::SyncSocket::Handle socket) {
     76   OnOpenComplete(PP_OK, shared_memory_handle, shared_memory_size, socket);
     77 }
     78 
     79 void PepperAudioInputHost::StreamCreationFailed() {
     80   OnOpenComplete(PP_ERROR_FAILED,
     81                  base::SharedMemory::NULLHandle(),
     82                  0,
     83                  base::SyncSocket::kInvalidHandle);
     84 }
     85 
     86 int32_t PepperAudioInputHost::OnOpen(ppapi::host::HostMessageContext* context,
     87                                      const std::string& device_id,
     88                                      PP_AudioSampleRate sample_rate,
     89                                      uint32_t sample_frame_count) {
     90   if (open_context_.is_valid())
     91     return PP_ERROR_INPROGRESS;
     92   if (audio_input_)
     93     return PP_ERROR_FAILED;
     94 
     95   GURL document_url = renderer_ppapi_host_->GetDocumentURL(pp_instance());
     96   if (!document_url.is_valid())
     97     return PP_ERROR_FAILED;
     98 
     99   // When it is done, we'll get called back on StreamCreated() or
    100   // StreamCreationFailed().
    101   RenderViewImpl* render_view = static_cast<RenderViewImpl*>(
    102       renderer_ppapi_host_->GetRenderViewForInstance(pp_instance()));
    103 
    104   audio_input_ =
    105       PepperPlatformAudioInput::Create(render_view->AsWeakPtr(),
    106                                        device_id,
    107                                        document_url,
    108                                        static_cast<int>(sample_rate),
    109                                        static_cast<int>(sample_frame_count),
    110                                        this);
    111   if (audio_input_) {
    112     open_context_ = context->MakeReplyMessageContext();
    113     return PP_OK_COMPLETIONPENDING;
    114   } else {
    115     return PP_ERROR_FAILED;
    116   }
    117 }
    118 
    119 int32_t PepperAudioInputHost::OnStartOrStop(
    120     ppapi::host::HostMessageContext* /* context */,
    121     bool capture) {
    122   if (!audio_input_)
    123     return PP_ERROR_FAILED;
    124   if (capture)
    125     audio_input_->StartCapture();
    126   else
    127     audio_input_->StopCapture();
    128   return PP_OK;
    129 }
    130 
    131 int32_t PepperAudioInputHost::OnClose(
    132     ppapi::host::HostMessageContext* /* context */) {
    133   Close();
    134   return PP_OK;
    135 }
    136 
    137 void PepperAudioInputHost::OnOpenComplete(
    138     int32_t result,
    139     base::SharedMemoryHandle shared_memory_handle,
    140     size_t shared_memory_size,
    141     base::SyncSocket::Handle socket_handle) {
    142   // Make sure the handles are cleaned up.
    143   base::SyncSocket scoped_socket(socket_handle);
    144   base::SharedMemory scoped_shared_memory(shared_memory_handle, false);
    145 
    146   if (!open_context_.is_valid()) {
    147     NOTREACHED();
    148     return;
    149   }
    150 
    151   ppapi::proxy::SerializedHandle serialized_socket_handle(
    152       ppapi::proxy::SerializedHandle::SOCKET);
    153   ppapi::proxy::SerializedHandle serialized_shared_memory_handle(
    154       ppapi::proxy::SerializedHandle::SHARED_MEMORY);
    155 
    156   if (result == PP_OK) {
    157     IPC::PlatformFileForTransit temp_socket =
    158         IPC::InvalidPlatformFileForTransit();
    159     base::SharedMemoryHandle temp_shmem = base::SharedMemory::NULLHandle();
    160     result = GetRemoteHandles(
    161         scoped_socket, scoped_shared_memory, &temp_socket, &temp_shmem);
    162 
    163     serialized_socket_handle.set_socket(temp_socket);
    164     serialized_shared_memory_handle.set_shmem(temp_shmem, shared_memory_size);
    165   }
    166 
    167   // Send all the values, even on error. This simplifies some of our cleanup
    168   // code since the handles will be in the other process and could be
    169   // inconvenient to clean up. Our IPC code will automatically handle this for
    170   // us, as long as the remote side always closes the handles it receives, even
    171   // in the failure case.
    172   open_context_.params.AppendHandle(serialized_socket_handle);
    173   open_context_.params.AppendHandle(serialized_shared_memory_handle);
    174   SendOpenReply(result);
    175 }
    176 
    177 int32_t PepperAudioInputHost::GetRemoteHandles(
    178     const base::SyncSocket& socket,
    179     const base::SharedMemory& shared_memory,
    180     IPC::PlatformFileForTransit* remote_socket_handle,
    181     base::SharedMemoryHandle* remote_shared_memory_handle) {
    182   *remote_socket_handle = renderer_ppapi_host_->ShareHandleWithRemote(
    183       ConvertSyncSocketHandle(socket), false);
    184   if (*remote_socket_handle == IPC::InvalidPlatformFileForTransit())
    185     return PP_ERROR_FAILED;
    186 
    187   *remote_shared_memory_handle = renderer_ppapi_host_->ShareHandleWithRemote(
    188       ConvertSharedMemoryHandle(shared_memory), false);
    189   if (*remote_shared_memory_handle == IPC::InvalidPlatformFileForTransit())
    190     return PP_ERROR_FAILED;
    191 
    192   return PP_OK;
    193 }
    194 
    195 void PepperAudioInputHost::Close() {
    196   if (!audio_input_)
    197     return;
    198 
    199   audio_input_->ShutDown();
    200   audio_input_ = NULL;
    201 
    202   if (open_context_.is_valid())
    203     SendOpenReply(PP_ERROR_ABORTED);
    204 }
    205 
    206 void PepperAudioInputHost::SendOpenReply(int32_t result) {
    207   open_context_.params.set_result(result);
    208   host()->SendReply(open_context_, PpapiPluginMsg_AudioInput_OpenReply());
    209   open_context_ = ppapi::host::ReplyMessageContext();
    210 }
    211 
    212 }  // namespace content
    213