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
),
28 has_received_frame_(false),
30 weak_factory_(this) {}
32 PepperVideoDestinationHost::~PepperVideoDestinationHost() {}
34 int32_t PepperVideoDestinationHost::OnResourceMessageReceived(
35 const IPC::Message
& msg
,
36 HostMessageContext
* context
) {
37 PPAPI_BEGIN_MESSAGE_MAP(PepperVideoDestinationHost
, msg
)
38 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoDestination_Open
,
40 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoDestination_PutFrame
,
42 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoDestination_Close
,
44 PPAPI_END_MESSAGE_MAP()
45 return PP_ERROR_FAILED
;
48 int32_t PepperVideoDestinationHost::OnHostMsgOpen(
49 HostMessageContext
* context
,
50 const std::string
& stream_url
) {
51 GURL
gurl(stream_url
);
53 return PP_ERROR_BADARGUMENT
;
55 FrameWriterInterface
* frame_writer
= NULL
;
56 if (!VideoDestinationHandler::Open(
57 NULL
/* registry */, gurl
.spec(), &frame_writer
))
58 return PP_ERROR_FAILED
;
59 frame_writer_
.reset(frame_writer
);
61 ReplyMessageContext reply_context
= context
->MakeReplyMessageContext();
62 reply_context
.params
.set_result(PP_OK
);
63 host()->SendReply(reply_context
, PpapiPluginMsg_VideoDestination_OpenReply());
64 return PP_OK_COMPLETIONPENDING
;
67 int32_t PepperVideoDestinationHost::OnHostMsgPutFrame(
68 HostMessageContext
* context
,
69 const ppapi::HostResource
& image_data_resource
,
70 PP_TimeTicks timestamp
) {
71 ppapi::thunk::EnterResourceNoLock
<ppapi::thunk::PPB_ImageData_API
> enter(
72 image_data_resource
.host_resource(), true);
74 return PP_ERROR_BADRESOURCE
;
75 PPB_ImageData_Impl
* image_data_impl
=
76 static_cast<PPB_ImageData_Impl
*>(enter
.object());
78 if (!PPB_ImageData_Impl::IsImageDataFormatSupported(
79 image_data_impl
->format()))
80 return PP_ERROR_BADARGUMENT
;
82 if (!frame_writer_
.get())
83 return PP_ERROR_FAILED
;
85 // Convert PP_TimeTicks (a double, in seconds) to a video timestamp (int64,
87 const int64_t timestamp_ns
=
88 static_cast<int64_t>(timestamp
* base::Time::kNanosecondsPerSecond
);
89 // Check that timestamps are strictly increasing.
91 if (has_received_frame_
)
92 DCHECK_GT(timestamp_ns
, previous_timestamp_ns_
);
93 has_received_frame_
= true;
94 previous_timestamp_ns_
= timestamp_ns
;
97 frame_writer_
->PutFrame(image_data_impl
, timestamp_ns
);
102 int32_t PepperVideoDestinationHost::OnHostMsgClose(
103 HostMessageContext
* context
) {
104 frame_writer_
.reset(NULL
);
108 } // namespace content