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_source_host.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/libyuv/include/libyuv/scale.h"
23 #include "third_party/skia/include/core/SkBitmap.h"
25 using ppapi::host::HostMessageContext
;
26 using ppapi::host::ReplyMessageContext
;
30 PepperVideoSourceHost::FrameReceiver::FrameReceiver(
31 const base::WeakPtr
<PepperVideoSourceHost
>& host
)
34 PepperVideoSourceHost::FrameReceiver::~FrameReceiver() {}
36 void PepperVideoSourceHost::FrameReceiver::GotFrame(
37 const scoped_refptr
<media::VideoFrame
>& frame
) {
38 DCHECK(thread_checker_
.CalledOnValidThread());
40 // Hold a reference to the new frame and release the previous.
41 host_
->last_frame_
= frame
;
43 if (host_
->get_frame_pending_
)
44 host_
->SendGetFrameReply();
48 PepperVideoSourceHost::PepperVideoSourceHost(RendererPpapiHost
* host
,
51 : ResourceHost(host
->GetPpapiHost(), instance
, resource
),
52 source_handler_(new VideoSourceHandler(NULL
)),
53 get_frame_pending_(false),
55 frame_receiver_
= new FrameReceiver(weak_factory_
.GetWeakPtr());
56 memset(&shared_image_desc_
, 0, sizeof(shared_image_desc_
));
59 PepperVideoSourceHost::~PepperVideoSourceHost() { Close(); }
61 int32_t PepperVideoSourceHost::OnResourceMessageReceived(
62 const IPC::Message
& msg
,
63 HostMessageContext
* context
) {
64 PPAPI_BEGIN_MESSAGE_MAP(PepperVideoSourceHost
, msg
)
65 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoSource_Open
,
67 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoSource_GetFrame
,
69 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoSource_Close
,
71 PPAPI_END_MESSAGE_MAP()
72 return PP_ERROR_FAILED
;
75 int32_t PepperVideoSourceHost::OnHostMsgOpen(HostMessageContext
* context
,
76 const std::string
& stream_url
) {
77 GURL
gurl(stream_url
);
79 return PP_ERROR_BADARGUMENT
;
81 if (!source_handler_
->Open(gurl
.spec(), frame_receiver_
.get()))
82 return PP_ERROR_BADARGUMENT
;
84 stream_url_
= gurl
.spec();
86 ReplyMessageContext reply_context
= context
->MakeReplyMessageContext();
87 reply_context
.params
.set_result(PP_OK
);
88 host()->SendReply(reply_context
, PpapiPluginMsg_VideoSource_OpenReply());
89 return PP_OK_COMPLETIONPENDING
;
92 int32_t PepperVideoSourceHost::OnHostMsgGetFrame(HostMessageContext
* context
) {
93 if (!source_handler_
.get())
94 return PP_ERROR_FAILED
;
95 if (get_frame_pending_
)
96 return PP_ERROR_INPROGRESS
;
98 reply_context_
= context
->MakeReplyMessageContext();
99 get_frame_pending_
= true;
101 // If a frame is ready, try to convert it and send the reply.
102 if (last_frame_
.get())
105 return PP_OK_COMPLETIONPENDING
;
108 int32_t PepperVideoSourceHost::OnHostMsgClose(HostMessageContext
* context
) {
113 void PepperVideoSourceHost::SendGetFrameReply() {
114 DCHECK(get_frame_pending_
);
115 get_frame_pending_
= false;
117 DCHECK(last_frame_
.get());
118 const gfx::Size dst_size
= last_frame_
->natural_size();
120 // Note: We try to reuse the shared memory for the previous frame here. This
121 // means that the previous frame may be overwritten and is no longer valid
122 // after calling this function again.
123 IPC::PlatformFileForTransit image_handle
;
125 if (shared_image_
.get() && dst_size
.width() == shared_image_
->width() &&
126 dst_size
.height() == shared_image_
->height()) {
127 // We have already allocated the correct size in shared memory. We need to
128 // duplicate the handle for IPC however, which will close down the
129 // duplicated handle when it's done.
131 if (shared_image_
->GetSharedMemory(&local_fd
, &byte_count
) != PP_OK
) {
132 SendGetFrameErrorReply(PP_ERROR_FAILED
);
136 ppapi::proxy::HostDispatcher
* dispatcher
=
137 ppapi::proxy::HostDispatcher::GetForInstance(pp_instance());
139 SendGetFrameErrorReply(PP_ERROR_FAILED
);
144 image_handle
= dispatcher
->ShareHandleWithRemote(
145 reinterpret_cast<HANDLE
>(static_cast<intptr_t>(local_fd
)), false);
146 #elif defined(OS_POSIX)
147 image_handle
= dispatcher
->ShareHandleWithRemote(local_fd
, false);
149 #error Not implemented.
152 // We need to allocate new shared memory.
153 shared_image_
= NULL
; // Release any previous image.
155 ppapi::ScopedPPResource
resource(
156 ppapi::ScopedPPResource::PassRef(),
157 ppapi::proxy::PPB_ImageData_Proxy::CreateImageData(
159 ppapi::PPB_ImageData_Shared::SIMPLE
,
160 PP_IMAGEDATAFORMAT_BGRA_PREMUL
,
161 PP_MakeSize(dst_size
.width(), dst_size
.height()),
162 false /* init_to_zero */,
167 SendGetFrameErrorReply(PP_ERROR_FAILED
);
171 ppapi::thunk::EnterResourceNoLock
<ppapi::thunk::PPB_ImageData_API
>
172 enter_resource(resource
, false);
173 if (enter_resource
.failed()) {
174 SendGetFrameErrorReply(PP_ERROR_FAILED
);
178 shared_image_
= static_cast<PPB_ImageData_Impl
*>(enter_resource
.object());
179 if (!shared_image_
.get()) {
180 SendGetFrameErrorReply(PP_ERROR_FAILED
);
184 DCHECK(!shared_image_
->IsMapped()); // New memory should not be mapped.
185 if (!shared_image_
->Map() || !shared_image_
->GetMappedBitmap() ||
186 !shared_image_
->GetMappedBitmap()->getPixels()) {
187 shared_image_
= NULL
;
188 SendGetFrameErrorReply(PP_ERROR_FAILED
);
193 const SkBitmap
* bitmap
= shared_image_
->GetMappedBitmap();
195 SendGetFrameErrorReply(PP_ERROR_FAILED
);
199 uint8_t* bitmap_pixels
= static_cast<uint8_t*>(bitmap
->getPixels());
200 if (!bitmap_pixels
) {
201 SendGetFrameErrorReply(PP_ERROR_FAILED
);
205 // Calculate the portion of the |last_frame_| that should be copied into
206 // |bitmap|. If |last_frame_| is lazily scaled, then
207 // last_frame_->visible_rect()._size() != last_frame_.natural_size().
208 scoped_refptr
<media::VideoFrame
> frame
;
209 if (dst_size
== last_frame_
->visible_rect().size()) {
210 // No scaling is needed, convert directly from last_frame_.
212 // Frame resolution doesn't change frequently, so don't keep any unnecessary
214 scaled_frame_
= NULL
;
216 // We need to create an intermediate scaled frame. Make sure we have
217 // allocated one of correct size.
218 if (!scaled_frame_
.get() || scaled_frame_
->coded_size() != dst_size
) {
219 scaled_frame_
= media::VideoFrame::CreateFrame(
220 media::VideoFrame::I420
, dst_size
, gfx::Rect(dst_size
), dst_size
,
221 last_frame_
->timestamp());
222 if (!scaled_frame_
.get()) {
223 LOG(ERROR
) << "Failed to allocate a media::VideoFrame";
224 SendGetFrameErrorReply(PP_ERROR_FAILED
);
228 scaled_frame_
->set_timestamp(last_frame_
->timestamp());
229 libyuv::I420Scale(last_frame_
->visible_data(media::VideoFrame::kYPlane
),
230 last_frame_
->stride(media::VideoFrame::kYPlane
),
231 last_frame_
->visible_data(media::VideoFrame::kUPlane
),
232 last_frame_
->stride(media::VideoFrame::kUPlane
),
233 last_frame_
->visible_data(media::VideoFrame::kVPlane
),
234 last_frame_
->stride(media::VideoFrame::kVPlane
),
235 last_frame_
->visible_rect().width(),
236 last_frame_
->visible_rect().height(),
237 scaled_frame_
->data(media::VideoFrame::kYPlane
),
238 scaled_frame_
->stride(media::VideoFrame::kYPlane
),
239 scaled_frame_
->data(media::VideoFrame::kUPlane
),
240 scaled_frame_
->stride(media::VideoFrame::kUPlane
),
241 scaled_frame_
->data(media::VideoFrame::kVPlane
),
242 scaled_frame_
->stride(media::VideoFrame::kVPlane
),
245 libyuv::kFilterBilinear
);
246 frame
= scaled_frame_
;
250 libyuv::I420ToARGB(frame
->visible_data(media::VideoFrame::kYPlane
),
251 frame
->stride(media::VideoFrame::kYPlane
),
252 frame
->visible_data(media::VideoFrame::kUPlane
),
253 frame
->stride(media::VideoFrame::kUPlane
),
254 frame
->visible_data(media::VideoFrame::kVPlane
),
255 frame
->stride(media::VideoFrame::kVPlane
),
261 ppapi::HostResource host_resource
;
262 host_resource
.SetHostResource(pp_instance(), shared_image_
->GetReference());
264 // Convert a video timestamp to a PP_TimeTicks (a double, in seconds).
265 const PP_TimeTicks timestamp
= frame
->timestamp().InSecondsF();
267 ppapi::proxy::SerializedHandle serialized_handle
;
268 serialized_handle
.set_shmem(image_handle
, byte_count
);
269 reply_context_
.params
.AppendHandle(serialized_handle
);
271 host()->SendReply(reply_context_
,
272 PpapiPluginMsg_VideoSource_GetFrameReply(
273 host_resource
, shared_image_desc_
, timestamp
));
275 reply_context_
= ppapi::host::ReplyMessageContext();
278 void PepperVideoSourceHost::SendGetFrameErrorReply(int32_t error
) {
279 reply_context_
.params
.set_result(error
);
282 PpapiPluginMsg_VideoSource_GetFrameReply(
283 ppapi::HostResource(), PP_ImageDataDesc(), 0.0 /* timestamp */));
284 reply_context_
= ppapi::host::ReplyMessageContext();
287 void PepperVideoSourceHost::Close() {
288 if (source_handler_
.get() && !stream_url_
.empty())
289 source_handler_
->Close(frame_receiver_
.get());
291 source_handler_
.reset(NULL
);
294 shared_image_
= NULL
;
297 } // namespace content