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.
5 #include "content/renderer/pepper/pepper_video_destination_host.h"
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"
18 using ppapi::host::HostMessageContext
;
19 using ppapi::host::ReplyMessageContext
;
23 PepperVideoDestinationHost::PepperVideoDestinationHost(RendererPpapiHost
* host
,
26 : ResourceHost(host
->GetPpapiHost(), instance
, resource
),
27 renderer_ppapi_host_(host
),
28 weak_factory_(this) {}
30 PepperVideoDestinationHost::~PepperVideoDestinationHost() {}
32 int32_t PepperVideoDestinationHost::OnResourceMessageReceived(
33 const IPC::Message
& msg
,
34 HostMessageContext
* context
) {
35 PPAPI_BEGIN_MESSAGE_MAP(PepperVideoDestinationHost
, msg
)
36 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoDestination_Open
,
38 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoDestination_PutFrame
,
40 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoDestination_Close
,
42 PPAPI_END_MESSAGE_MAP()
43 return PP_ERROR_FAILED
;
46 int32_t PepperVideoDestinationHost::OnHostMsgOpen(
47 HostMessageContext
* context
,
48 const std::string
& stream_url
) {
49 GURL
gurl(stream_url
);
51 return PP_ERROR_BADARGUMENT
;
53 FrameWriterInterface
* frame_writer
= NULL
;
54 if (!VideoDestinationHandler::Open(
55 NULL
/* registry */, gurl
.spec(), &frame_writer
))
56 return PP_ERROR_FAILED
;
57 frame_writer_
.reset(frame_writer
);
59 ReplyMessageContext reply_context
= context
->MakeReplyMessageContext();
60 reply_context
.params
.set_result(PP_OK
);
61 host()->SendReply(reply_context
, PpapiPluginMsg_VideoDestination_OpenReply());
62 return PP_OK_COMPLETIONPENDING
;
65 int32_t PepperVideoDestinationHost::OnHostMsgPutFrame(
66 HostMessageContext
* context
,
67 const ppapi::HostResource
& image_data_resource
,
68 PP_TimeTicks timestamp
) {
69 ppapi::thunk::EnterResourceNoLock
<ppapi::thunk::PPB_ImageData_API
> enter(
70 image_data_resource
.host_resource(), true);
72 return PP_ERROR_BADRESOURCE
;
73 PPB_ImageData_Impl
* image_data_impl
=
74 static_cast<PPB_ImageData_Impl
*>(enter
.object());
76 if (!PPB_ImageData_Impl::IsImageDataFormatSupported(
77 image_data_impl
->format()))
78 return PP_ERROR_BADARGUMENT
;
80 if (!frame_writer_
.get())
81 return PP_ERROR_FAILED
;
83 // Convert PP_TimeTicks (a double, in seconds) to a video timestamp (int64,
85 const int64_t timestamp_ns
=
86 static_cast<int64_t>(timestamp
* base::Time::kNanosecondsPerSecond
);
87 frame_writer_
->PutFrame(image_data_impl
, timestamp_ns
);
92 int32_t PepperVideoDestinationHost::OnHostMsgClose(
93 HostMessageContext
* context
) {
94 frame_writer_
.reset(NULL
);
98 } // namespace content