1c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// found in the LICENSE file.
4c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/renderer/pepper/pepper_video_destination_host.h"
6c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
7eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "content/public/renderer/renderer_ppapi_host.h"
9bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch#include "content/renderer/pepper/ppb_image_data_impl.h"
10c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ppapi/c/pp_errors.h"
11c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ppapi/host/dispatch_host_message.h"
12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ppapi/host/host_message_context.h"
13c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ppapi/host/ppapi_host.h"
14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ppapi/proxy/ppapi_messages.h"
15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ppapi/thunk/enter.h"
16c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ppapi/thunk/ppb_image_data_api.h"
17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
18c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)using ppapi::host::HostMessageContext;
19c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)using ppapi::host::ReplyMessageContext;
20c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
21c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace content {
22c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
23a02191e04bc25c4935f804f2c080ae28663d096dBen MurdochPepperVideoDestinationHost::PepperVideoDestinationHost(RendererPpapiHost* host,
24a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                                                       PP_Instance instance,
25a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch                                                       PP_Resource resource)
26c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    : ResourceHost(host->GetPpapiHost(), instance, resource),
27c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      renderer_ppapi_host_(host),
28a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      weak_factory_(this) {}
29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
30a02191e04bc25c4935f804f2c080ae28663d096dBen MurdochPepperVideoDestinationHost::~PepperVideoDestinationHost() {}
31c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
32c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)int32_t PepperVideoDestinationHost::OnResourceMessageReceived(
33c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const IPC::Message& msg,
34c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    HostMessageContext* context) {
35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  PPAPI_BEGIN_MESSAGE_MAP(PepperVideoDestinationHost, msg)
36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoDestination_Open,
37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                      OnHostMsgOpen)
38cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoDestination_PutFrame,
39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                      OnHostMsgPutFrame)
40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoDestination_Close,
41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                        OnHostMsgClose)
42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  PPAPI_END_MESSAGE_MAP()
43c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return PP_ERROR_FAILED;
44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
46c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)int32_t PepperVideoDestinationHost::OnHostMsgOpen(
47c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    HostMessageContext* context,
48c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const std::string& stream_url) {
49c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  GURL gurl(stream_url);
50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!gurl.is_valid())
51c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return PP_ERROR_BADARGUMENT;
52b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
5358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  FrameWriterInterface* frame_writer = NULL;
54a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  if (!VideoDestinationHandler::Open(
550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch          NULL /* registry */, gurl.spec(), &frame_writer))
56b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    return PP_ERROR_FAILED;
57b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  frame_writer_.reset(frame_writer);
58b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
59c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ReplyMessageContext reply_context = context->MakeReplyMessageContext();
60c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  reply_context.params.set_result(PP_OK);
61a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  host()->SendReply(reply_context, PpapiPluginMsg_VideoDestination_OpenReply());
62c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return PP_OK_COMPLETIONPENDING;
63c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
64c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
65c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)int32_t PepperVideoDestinationHost::OnHostMsgPutFrame(
66c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    HostMessageContext* context,
67b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    const ppapi::HostResource& image_data_resource,
68c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    PP_TimeTicks timestamp) {
69c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ppapi::thunk::EnterResourceNoLock<ppapi::thunk::PPB_ImageData_API> enter(
70b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      image_data_resource.host_resource(), true);
71c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (enter.failed())
72c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return PP_ERROR_BADRESOURCE;
7358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  PPB_ImageData_Impl* image_data_impl =
7458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      static_cast<PPB_ImageData_Impl*>(enter.object());
75c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
7658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  if (!PPB_ImageData_Impl::IsImageDataFormatSupported(
77b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)          image_data_impl->format()))
78c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return PP_ERROR_BADARGUMENT;
79c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
80b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  if (!frame_writer_.get())
81b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    return PP_ERROR_FAILED;
82b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
83b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // Convert PP_TimeTicks (a double, in seconds) to a TimeDelta (int64,
84b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // microseconds) and then to a video timestamp (int64, nanoseconds). All times
85b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // are relative to the Unix Epoch so don't subtract it to get a delta.
86b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  base::TimeDelta time_delta =
87b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      base::Time::FromDoubleT(timestamp) - base::Time();
88b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  int64_t timestamp_ns =
89b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      time_delta.InMicroseconds() * base::Time::kNanosecondsPerMicrosecond;
90b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  frame_writer_->PutFrame(image_data_impl, timestamp_ns);
91b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
92c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return PP_OK;
93c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
94c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
95c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)int32_t PepperVideoDestinationHost::OnHostMsgClose(
96c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    HostMessageContext* context) {
97b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  frame_writer_.reset(NULL);
98c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return PP_OK;
99c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
100c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
101c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}  // namespace content
102