Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / pepper / pepper_video_destination_host.cc
blob8773046fd2c2e79de22f858d34620d02a0483a66
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;
21 namespace content {
23 PepperVideoDestinationHost::PepperVideoDestinationHost(RendererPpapiHost* host,
24 PP_Instance instance,
25 PP_Resource resource)
26 : ResourceHost(host->GetPpapiHost(), instance, resource),
27 #if DCHECK_IS_ON()
28 has_received_frame_(false),
29 #endif
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,
39 OnHostMsgOpen)
40 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoDestination_PutFrame,
41 OnHostMsgPutFrame)
42 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoDestination_Close,
43 OnHostMsgClose)
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);
52 if (!gurl.is_valid())
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);
73 if (enter.failed())
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,
86 // nanoseconds).
87 const int64_t timestamp_ns =
88 static_cast<int64_t>(timestamp * base::Time::kNanosecondsPerSecond);
89 // Check that timestamps are strictly increasing.
90 #if DCHECK_IS_ON()
91 if (has_received_frame_)
92 DCHECK_GT(timestamp_ns, previous_timestamp_ns_);
93 has_received_frame_ = true;
94 previous_timestamp_ns_ = timestamp_ns;
95 #endif
97 frame_writer_->PutFrame(image_data_impl, timestamp_ns);
99 return PP_OK;
102 int32_t PepperVideoDestinationHost::OnHostMsgClose(
103 HostMessageContext* context) {
104 frame_writer_.reset(NULL);
105 return PP_OK;
108 } // namespace content