Home | History | Annotate | Download | only in pepper
      1 // Copyright (c) 2013 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_video_destination_host.h"
      6 
      7 #include "base/time/time.h"
      8 #include "content/public/renderer/renderer_ppapi_host.h"
      9 #include "content/renderer/pepper/ppb_image_data_impl.h"
     10 #include "ppapi/c/pp_errors.h"
     11 #include "ppapi/host/dispatch_host_message.h"
     12 #include "ppapi/host/host_message_context.h"
     13 #include "ppapi/host/ppapi_host.h"
     14 #include "ppapi/proxy/ppapi_messages.h"
     15 #include "ppapi/thunk/enter.h"
     16 #include "ppapi/thunk/ppb_image_data_api.h"
     17 
     18 using ppapi::host::HostMessageContext;
     19 using ppapi::host::ReplyMessageContext;
     20 
     21 namespace content {
     22 
     23 PepperVideoDestinationHost::PepperVideoDestinationHost(
     24     RendererPpapiHost* host,
     25     PP_Instance instance,
     26     PP_Resource resource)
     27     : ResourceHost(host->GetPpapiHost(), instance, resource),
     28       renderer_ppapi_host_(host),
     29       weak_factory_(this) {
     30 }
     31 
     32 PepperVideoDestinationHost::~PepperVideoDestinationHost() {
     33 }
     34 
     35 int32_t PepperVideoDestinationHost::OnResourceMessageReceived(
     36     const IPC::Message& msg,
     37     HostMessageContext* context) {
     38   IPC_BEGIN_MESSAGE_MAP(PepperVideoDestinationHost, msg)
     39     PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoDestination_Open,
     40                                       OnHostMsgOpen)
     41     PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoDestination_PutFrame,
     42                                       OnHostMsgPutFrame)
     43     PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoDestination_Close,
     44                                         OnHostMsgClose)
     45   IPC_END_MESSAGE_MAP()
     46   return PP_ERROR_FAILED;
     47 }
     48 
     49 int32_t PepperVideoDestinationHost::OnHostMsgOpen(
     50     HostMessageContext* context,
     51     const std::string& stream_url) {
     52   GURL gurl(stream_url);
     53   if (!gurl.is_valid())
     54     return PP_ERROR_BADARGUMENT;
     55 
     56   FrameWriterInterface* frame_writer = NULL;
     57   if (!VideoDestinationHandler::Open(NULL /* factory */,
     58                                      NULL /* registry */,
     59                                      gurl.spec(),
     60                                      &frame_writer))
     61     return PP_ERROR_FAILED;
     62   frame_writer_.reset(frame_writer);
     63 
     64   ReplyMessageContext reply_context = context->MakeReplyMessageContext();
     65   reply_context.params.set_result(PP_OK);
     66   host()->SendReply(reply_context,
     67                     PpapiPluginMsg_VideoDestination_OpenReply());
     68   return PP_OK_COMPLETIONPENDING;
     69 }
     70 
     71 int32_t PepperVideoDestinationHost::OnHostMsgPutFrame(
     72     HostMessageContext* context,
     73     const ppapi::HostResource& image_data_resource,
     74     PP_TimeTicks timestamp) {
     75   ppapi::thunk::EnterResourceNoLock<ppapi::thunk::PPB_ImageData_API> enter(
     76       image_data_resource.host_resource(), true);
     77   if (enter.failed())
     78     return PP_ERROR_BADRESOURCE;
     79   PPB_ImageData_Impl* image_data_impl =
     80       static_cast<PPB_ImageData_Impl*>(enter.object());
     81 
     82   if (!PPB_ImageData_Impl::IsImageDataFormatSupported(
     83           image_data_impl->format()))
     84     return PP_ERROR_BADARGUMENT;
     85 
     86   if (!frame_writer_.get())
     87     return PP_ERROR_FAILED;
     88 
     89   // Convert PP_TimeTicks (a double, in seconds) to a TimeDelta (int64,
     90   // microseconds) and then to a video timestamp (int64, nanoseconds). All times
     91   // are relative to the Unix Epoch so don't subtract it to get a delta.
     92   base::TimeDelta time_delta =
     93       base::Time::FromDoubleT(timestamp) - base::Time();
     94   int64_t timestamp_ns =
     95       time_delta.InMicroseconds() * base::Time::kNanosecondsPerMicrosecond;
     96   frame_writer_->PutFrame(image_data_impl, timestamp_ns);
     97 
     98   return PP_OK;
     99 }
    100 
    101 int32_t PepperVideoDestinationHost::OnHostMsgClose(
    102     HostMessageContext* context) {
    103   frame_writer_.reset(NULL);
    104   return PP_OK;
    105 }
    106 
    107 }  // namespace content
    108