Upstream version 11.39.250.0
[platform/framework/web/crosswalk.git] / src / content / renderer / pepper / pepper_video_source_host.cc
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_source_host.h"
6
7 #include "base/bind.h"
8 #include "base/numerics/safe_conversions.h"
9 #include "content/public/renderer/renderer_ppapi_host.h"
10 #include "content/renderer/pepper/ppb_image_data_impl.h"
11 #include "content/renderer/render_thread_impl.h"
12 #include "ppapi/c/pp_errors.h"
13 #include "ppapi/host/dispatch_host_message.h"
14 #include "ppapi/host/ppapi_host.h"
15 #include "ppapi/proxy/host_dispatcher.h"
16 #include "ppapi/proxy/ppapi_messages.h"
17 #include "ppapi/proxy/ppb_image_data_proxy.h"
18 #include "ppapi/shared_impl/scoped_pp_resource.h"
19 #include "ppapi/thunk/enter.h"
20 #include "ppapi/thunk/ppb_image_data_api.h"
21 #include "third_party/libyuv/include/libyuv/convert.h"
22 #include "third_party/skia/include/core/SkBitmap.h"
23
24 using ppapi::host::HostMessageContext;
25 using ppapi::host::ReplyMessageContext;
26
27 namespace content {
28
29 PepperVideoSourceHost::FrameReceiver::FrameReceiver(
30     const base::WeakPtr<PepperVideoSourceHost>& host)
31     : host_(host),
32       main_message_loop_proxy_(base::MessageLoopProxy::current()) {}
33
34 PepperVideoSourceHost::FrameReceiver::~FrameReceiver() {}
35
36 bool PepperVideoSourceHost::FrameReceiver::GotFrame(
37     const scoped_refptr<media::VideoFrame>& frame) {
38   // It's not safe to access the host from this thread, so post a task to our
39   // main thread to transfer the new frame.
40   main_message_loop_proxy_->PostTask(
41       FROM_HERE, base::Bind(&FrameReceiver::OnGotFrame, this, frame));
42
43   return true;
44 }
45
46 void PepperVideoSourceHost::FrameReceiver::OnGotFrame(
47     const scoped_refptr<media::VideoFrame>& frame) {
48   if (host_.get()) {
49     // Hold a reference to the new frame and release the previous.
50     host_->last_frame_ = frame;
51
52     if (host_->get_frame_pending_)
53       host_->SendGetFrameReply();
54   }
55 }
56
57 PepperVideoSourceHost::PepperVideoSourceHost(RendererPpapiHost* host,
58                                              PP_Instance instance,
59                                              PP_Resource resource)
60     : ResourceHost(host->GetPpapiHost(), instance, resource),
61       renderer_ppapi_host_(host),
62       source_handler_(new VideoSourceHandler(NULL)),
63       get_frame_pending_(false),
64       weak_factory_(this) {
65   frame_receiver_ = new FrameReceiver(weak_factory_.GetWeakPtr());
66   memset(&shared_image_desc_, 0, sizeof(shared_image_desc_));
67 }
68
69 PepperVideoSourceHost::~PepperVideoSourceHost() { Close(); }
70
71 int32_t PepperVideoSourceHost::OnResourceMessageReceived(
72     const IPC::Message& msg,
73     HostMessageContext* context) {
74   PPAPI_BEGIN_MESSAGE_MAP(PepperVideoSourceHost, msg)
75     PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoSource_Open,
76                                       OnHostMsgOpen)
77     PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoSource_GetFrame,
78                                         OnHostMsgGetFrame)
79     PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoSource_Close,
80                                         OnHostMsgClose)
81   PPAPI_END_MESSAGE_MAP()
82   return PP_ERROR_FAILED;
83 }
84
85 int32_t PepperVideoSourceHost::OnHostMsgOpen(HostMessageContext* context,
86                                              const std::string& stream_url) {
87   GURL gurl(stream_url);
88   if (!gurl.is_valid())
89     return PP_ERROR_BADARGUMENT;
90
91   if (!source_handler_->Open(gurl.spec(), frame_receiver_.get()))
92     return PP_ERROR_BADARGUMENT;
93
94   stream_url_ = gurl.spec();
95
96   ReplyMessageContext reply_context = context->MakeReplyMessageContext();
97   reply_context.params.set_result(PP_OK);
98   host()->SendReply(reply_context, PpapiPluginMsg_VideoSource_OpenReply());
99   return PP_OK_COMPLETIONPENDING;
100 }
101
102 int32_t PepperVideoSourceHost::OnHostMsgGetFrame(HostMessageContext* context) {
103   if (!source_handler_.get())
104     return PP_ERROR_FAILED;
105   if (get_frame_pending_)
106     return PP_ERROR_INPROGRESS;
107
108   reply_context_ = context->MakeReplyMessageContext();
109   get_frame_pending_ = true;
110
111   // If a frame is ready, try to convert it and send the reply.
112   if (last_frame_.get())
113     SendGetFrameReply();
114
115   return PP_OK_COMPLETIONPENDING;
116 }
117
118 int32_t PepperVideoSourceHost::OnHostMsgClose(HostMessageContext* context) {
119   Close();
120   return PP_OK;
121 }
122
123 void PepperVideoSourceHost::SendGetFrameReply() {
124   DCHECK(get_frame_pending_);
125   get_frame_pending_ = false;
126
127   DCHECK(last_frame_.get());
128   scoped_refptr<media::VideoFrame> frame(last_frame_);
129   last_frame_ = NULL;
130
131   const int dst_width = frame->visible_rect().width();
132   const int dst_height = frame->visible_rect().height();
133
134   // Note: We try to reuse the shared memory for the previous frame here. This
135   // means that the previous frame may be overwritten and is no longer valid
136   // after calling this function again.
137   IPC::PlatformFileForTransit image_handle;
138   uint32_t byte_count;
139   if (shared_image_.get() && dst_width == shared_image_->width() &&
140       dst_height == shared_image_->height()) {
141     // We have already allocated the correct size in shared memory. We need to
142     // duplicate the handle for IPC however, which will close down the
143     // duplicated handle when it's done.
144     int local_fd = 0;
145     if (shared_image_->GetSharedMemory(&local_fd, &byte_count) != PP_OK) {
146       SendGetFrameErrorReply(PP_ERROR_FAILED);
147       return;
148     }
149
150     ppapi::proxy::HostDispatcher* dispatcher =
151         ppapi::proxy::HostDispatcher::GetForInstance(pp_instance());
152     if (!dispatcher) {
153       SendGetFrameErrorReply(PP_ERROR_FAILED);
154       return;
155     }
156
157 #if defined(OS_WIN)
158     image_handle = dispatcher->ShareHandleWithRemote(
159         reinterpret_cast<HANDLE>(static_cast<intptr_t>(local_fd)), false);
160 #elif defined(OS_POSIX)
161     image_handle = dispatcher->ShareHandleWithRemote(local_fd, false);
162 #else
163 #error Not implemented.
164 #endif
165   } else {
166     // We need to allocate new shared memory.
167     shared_image_ = NULL;  // Release any previous image.
168
169     ppapi::ScopedPPResource resource(
170         ppapi::ScopedPPResource::PassRef(),
171         ppapi::proxy::PPB_ImageData_Proxy::CreateImageData(
172             pp_instance(),
173             ppapi::PPB_ImageData_Shared::SIMPLE,
174             PP_IMAGEDATAFORMAT_BGRA_PREMUL,
175             PP_MakeSize(dst_width, dst_height),
176             false /* init_to_zero */,
177             &shared_image_desc_,
178             &image_handle,
179             &byte_count));
180     if (!resource) {
181       SendGetFrameErrorReply(PP_ERROR_FAILED);
182       return;
183     }
184
185     ppapi::thunk::EnterResourceNoLock<ppapi::thunk::PPB_ImageData_API>
186         enter_resource(resource, false);
187     if (enter_resource.failed()) {
188       SendGetFrameErrorReply(PP_ERROR_FAILED);
189       return;
190     }
191
192     shared_image_ = static_cast<PPB_ImageData_Impl*>(enter_resource.object());
193     if (!shared_image_.get()) {
194       SendGetFrameErrorReply(PP_ERROR_FAILED);
195       return;
196     }
197
198     DCHECK(!shared_image_->IsMapped());  // New memory should not be mapped.
199     if (!shared_image_->Map() || !shared_image_->GetMappedBitmap() ||
200         !shared_image_->GetMappedBitmap()->getPixels()) {
201       shared_image_ = NULL;
202       SendGetFrameErrorReply(PP_ERROR_FAILED);
203       return;
204     }
205   }
206
207   const SkBitmap* bitmap = shared_image_->GetMappedBitmap();
208   if (!bitmap) {
209     SendGetFrameErrorReply(PP_ERROR_FAILED);
210     return;
211   }
212
213   uint8_t* bitmap_pixels = static_cast<uint8_t*>(bitmap->getPixels());
214   if (!bitmap_pixels) {
215     SendGetFrameErrorReply(PP_ERROR_FAILED);
216     return;
217   }
218
219   // Calculate that portion of the |frame| that should be copied into
220   // |bitmap|. If |frame| has been cropped,
221   // frame->coded_size() != frame->visible_rect().
222   const int src_width = frame->coded_size().width();
223   const int src_height = frame->coded_size().height();
224   DCHECK(src_width >= dst_width && src_height >= dst_height);
225
226   const int horiz_crop = frame->visible_rect().x();
227   const int vert_crop = frame->visible_rect().y();
228
229   const uint8* src_y = frame->data(media::VideoFrame::kYPlane) +
230                        (src_width * vert_crop + horiz_crop);
231   const int center = (src_width + 1) / 2;
232   const uint8* src_u = frame->data(media::VideoFrame::kUPlane) +
233                        (center * vert_crop + horiz_crop) / 2;
234   const uint8* src_v = frame->data(media::VideoFrame::kVPlane) +
235                        (center * vert_crop + horiz_crop) / 2;
236
237   libyuv::I420ToBGRA(src_y,
238                      frame->stride(media::VideoFrame::kYPlane),
239                      src_u,
240                      frame->stride(media::VideoFrame::kUPlane),
241                      src_v,
242                      frame->stride(media::VideoFrame::kVPlane),
243                      bitmap_pixels,
244                      bitmap->rowBytes(),
245                      dst_width,
246                      dst_height);
247
248   ppapi::HostResource host_resource;
249   host_resource.SetHostResource(pp_instance(), shared_image_->GetReference());
250
251   // Convert a video timestamp to a PP_TimeTicks (a double, in seconds).
252   const PP_TimeTicks timestamp = frame->timestamp().InSecondsF();
253
254   ppapi::proxy::SerializedHandle serialized_handle;
255   serialized_handle.set_shmem(image_handle, byte_count);
256   reply_context_.params.AppendHandle(serialized_handle);
257
258   host()->SendReply(reply_context_,
259                     PpapiPluginMsg_VideoSource_GetFrameReply(
260                         host_resource, shared_image_desc_, timestamp));
261
262   reply_context_ = ppapi::host::ReplyMessageContext();
263 }
264
265 void PepperVideoSourceHost::SendGetFrameErrorReply(int32_t error) {
266   reply_context_.params.set_result(error);
267   host()->SendReply(
268       reply_context_,
269       PpapiPluginMsg_VideoSource_GetFrameReply(
270           ppapi::HostResource(), PP_ImageDataDesc(), 0.0 /* timestamp */));
271   reply_context_ = ppapi::host::ReplyMessageContext();
272 }
273
274 void PepperVideoSourceHost::Close() {
275   if (source_handler_.get() && !stream_url_.empty())
276     source_handler_->Close(frame_receiver_.get());
277
278   source_handler_.reset(NULL);
279   stream_url_.clear();
280
281   shared_image_ = NULL;
282 }
283
284 }  // namespace content