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/media/video_track_to_pepper_adapter.h"
11 #include "content/renderer/pepper/ppb_image_data_impl.h"
12 #include "content/renderer/render_thread_impl.h"
13 #include "ppapi/c/pp_errors.h"
14 #include "ppapi/host/dispatch_host_message.h"
15 #include "ppapi/host/ppapi_host.h"
16 #include "ppapi/proxy/host_dispatcher.h"
17 #include "ppapi/proxy/ppapi_messages.h"
18 #include "ppapi/proxy/ppb_image_data_proxy.h"
19 #include "ppapi/shared_impl/scoped_pp_resource.h"
20 #include "ppapi/thunk/enter.h"
21 #include "ppapi/thunk/ppb_image_data_api.h"
22 #include "third_party/libyuv/include/libyuv/convert.h"
23 #include "third_party/libyuv/include/libyuv/scale.h"
24 #include "third_party/skia/include/core/SkBitmap.h"
26 using ppapi::host::HostMessageContext
;
27 using ppapi::host::ReplyMessageContext
;
31 class PepperVideoSourceHost::FrameReceiver
32 : public FrameReaderInterface
,
33 public base::RefCountedThreadSafe
<FrameReceiver
> {
35 explicit FrameReceiver(const base::WeakPtr
<PepperVideoSourceHost
>& host
);
37 // FrameReaderInterface implementation.
38 void GotFrame(const scoped_refptr
<media::VideoFrame
>& frame
) override
;
41 friend class base::RefCountedThreadSafe
<FrameReceiver
>;
42 ~FrameReceiver() override
;
44 base::WeakPtr
<PepperVideoSourceHost
> host_
;
45 // |thread_checker_| is bound to the main render thread.
46 base::ThreadChecker thread_checker_
;
49 PepperVideoSourceHost::FrameReceiver::FrameReceiver(
50 const base::WeakPtr
<PepperVideoSourceHost
>& host
)
53 PepperVideoSourceHost::FrameReceiver::~FrameReceiver() {}
55 void PepperVideoSourceHost::FrameReceiver::GotFrame(
56 const scoped_refptr
<media::VideoFrame
>& frame
) {
57 DCHECK(thread_checker_
.CalledOnValidThread());
60 // Hold a reference to the new frame and release the previous.
61 host_
->last_frame_
= frame
;
62 if (host_
->get_frame_pending_
)
63 host_
->SendGetFrameReply();
66 PepperVideoSourceHost::PepperVideoSourceHost(RendererPpapiHost
* host
,
69 : ResourceHost(host
->GetPpapiHost(), instance
, resource
),
70 frame_source_(new VideoTrackToPepperAdapter(nullptr)),
71 get_frame_pending_(false),
73 frame_receiver_
= new FrameReceiver(weak_factory_
.GetWeakPtr());
74 memset(&shared_image_desc_
, 0, sizeof(shared_image_desc_
));
77 PepperVideoSourceHost::~PepperVideoSourceHost() { Close(); }
79 int32_t PepperVideoSourceHost::OnResourceMessageReceived(
80 const IPC::Message
& msg
,
81 HostMessageContext
* context
) {
82 PPAPI_BEGIN_MESSAGE_MAP(PepperVideoSourceHost
, msg
)
83 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoSource_Open
,
85 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoSource_GetFrame
,
87 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoSource_Close
,
89 PPAPI_END_MESSAGE_MAP()
90 return PP_ERROR_FAILED
;
93 int32_t PepperVideoSourceHost::OnHostMsgOpen(HostMessageContext
* context
,
94 const std::string
& stream_url
) {
95 GURL
gurl(stream_url
);
97 return PP_ERROR_BADARGUMENT
;
99 if (!frame_source_
->Open(gurl
.spec(), frame_receiver_
.get()))
100 return PP_ERROR_BADARGUMENT
;
102 stream_url_
= gurl
.spec();
104 ReplyMessageContext reply_context
= context
->MakeReplyMessageContext();
105 reply_context
.params
.set_result(PP_OK
);
106 host()->SendReply(reply_context
, PpapiPluginMsg_VideoSource_OpenReply());
107 return PP_OK_COMPLETIONPENDING
;
110 int32_t PepperVideoSourceHost::OnHostMsgGetFrame(HostMessageContext
* context
) {
111 if (!frame_source_
.get())
112 return PP_ERROR_FAILED
;
113 if (get_frame_pending_
)
114 return PP_ERROR_INPROGRESS
;
116 reply_context_
= context
->MakeReplyMessageContext();
117 get_frame_pending_
= true;
119 // If a frame is ready, try to convert it and send the reply.
120 if (last_frame_
.get())
123 return PP_OK_COMPLETIONPENDING
;
126 int32_t PepperVideoSourceHost::OnHostMsgClose(HostMessageContext
* context
) {
131 void PepperVideoSourceHost::SendGetFrameReply() {
132 DCHECK(get_frame_pending_
);
133 get_frame_pending_
= false;
135 DCHECK(last_frame_
.get());
136 const gfx::Size dst_size
= last_frame_
->natural_size();
138 // Note: We try to reuse the shared memory for the previous frame here. This
139 // means that the previous frame may be overwritten and is no longer valid
140 // after calling this function again.
141 base::SharedMemoryHandle image_handle
;
143 if (shared_image_
.get() && dst_size
.width() == shared_image_
->width() &&
144 dst_size
.height() == shared_image_
->height()) {
145 // We have already allocated the correct size in shared memory. We need to
146 // duplicate the handle for IPC however, which will close down the
147 // duplicated handle when it's done.
148 base::SharedMemory
* local_shm
;
149 if (shared_image_
->GetSharedMemory(&local_shm
, &byte_count
) != PP_OK
) {
150 SendGetFrameErrorReply(PP_ERROR_FAILED
);
154 ppapi::proxy::HostDispatcher
* dispatcher
=
155 ppapi::proxy::HostDispatcher::GetForInstance(pp_instance());
157 SendGetFrameErrorReply(PP_ERROR_FAILED
);
162 dispatcher
->ShareSharedMemoryHandleWithRemote(local_shm
->handle());
164 // We need to allocate new shared memory.
165 shared_image_
= NULL
; // Release any previous image.
167 ppapi::ScopedPPResource
resource(
168 ppapi::ScopedPPResource::PassRef(),
169 ppapi::proxy::PPB_ImageData_Proxy::CreateImageData(
171 ppapi::PPB_ImageData_Shared::SIMPLE
,
172 PP_IMAGEDATAFORMAT_BGRA_PREMUL
,
173 PP_MakeSize(dst_size
.width(), dst_size
.height()),
174 false /* init_to_zero */,
179 SendGetFrameErrorReply(PP_ERROR_FAILED
);
183 ppapi::thunk::EnterResourceNoLock
<ppapi::thunk::PPB_ImageData_API
>
184 enter_resource(resource
, false);
185 if (enter_resource
.failed()) {
186 SendGetFrameErrorReply(PP_ERROR_FAILED
);
190 shared_image_
= static_cast<PPB_ImageData_Impl
*>(enter_resource
.object());
191 if (!shared_image_
.get()) {
192 SendGetFrameErrorReply(PP_ERROR_FAILED
);
196 DCHECK(!shared_image_
->IsMapped()); // New memory should not be mapped.
197 if (!shared_image_
->Map() || !shared_image_
->GetMappedBitmap() ||
198 !shared_image_
->GetMappedBitmap()->getPixels()) {
199 shared_image_
= NULL
;
200 SendGetFrameErrorReply(PP_ERROR_FAILED
);
205 const SkBitmap
* bitmap
= shared_image_
->GetMappedBitmap();
207 SendGetFrameErrorReply(PP_ERROR_FAILED
);
211 uint8_t* bitmap_pixels
= static_cast<uint8_t*>(bitmap
->getPixels());
212 if (!bitmap_pixels
) {
213 SendGetFrameErrorReply(PP_ERROR_FAILED
);
217 // Calculate the portion of the |last_frame_| that should be copied into
218 // |bitmap|. If |last_frame_| is lazily scaled, then
219 // last_frame_->visible_rect()._size() != last_frame_.natural_size().
220 scoped_refptr
<media::VideoFrame
> frame
;
221 if (dst_size
== last_frame_
->visible_rect().size()) {
222 // No scaling is needed, convert directly from last_frame_.
224 // Frame resolution doesn't change frequently, so don't keep any unnecessary
226 scaled_frame_
= NULL
;
228 // We need to create an intermediate scaled frame. Make sure we have
229 // allocated one of correct size.
230 if (!scaled_frame_
.get() || scaled_frame_
->coded_size() != dst_size
) {
231 scaled_frame_
= media::VideoFrame::CreateFrame(
232 media::PIXEL_FORMAT_I420
, dst_size
, gfx::Rect(dst_size
), dst_size
,
233 last_frame_
->timestamp());
234 if (!scaled_frame_
.get()) {
235 LOG(ERROR
) << "Failed to allocate a media::VideoFrame";
236 SendGetFrameErrorReply(PP_ERROR_FAILED
);
240 scaled_frame_
->set_timestamp(last_frame_
->timestamp());
241 libyuv::I420Scale(last_frame_
->visible_data(media::VideoFrame::kYPlane
),
242 last_frame_
->stride(media::VideoFrame::kYPlane
),
243 last_frame_
->visible_data(media::VideoFrame::kUPlane
),
244 last_frame_
->stride(media::VideoFrame::kUPlane
),
245 last_frame_
->visible_data(media::VideoFrame::kVPlane
),
246 last_frame_
->stride(media::VideoFrame::kVPlane
),
247 last_frame_
->visible_rect().width(),
248 last_frame_
->visible_rect().height(),
249 scaled_frame_
->data(media::VideoFrame::kYPlane
),
250 scaled_frame_
->stride(media::VideoFrame::kYPlane
),
251 scaled_frame_
->data(media::VideoFrame::kUPlane
),
252 scaled_frame_
->stride(media::VideoFrame::kUPlane
),
253 scaled_frame_
->data(media::VideoFrame::kVPlane
),
254 scaled_frame_
->stride(media::VideoFrame::kVPlane
),
257 libyuv::kFilterBilinear
);
258 frame
= scaled_frame_
;
262 libyuv::I420ToARGB(frame
->visible_data(media::VideoFrame::kYPlane
),
263 frame
->stride(media::VideoFrame::kYPlane
),
264 frame
->visible_data(media::VideoFrame::kUPlane
),
265 frame
->stride(media::VideoFrame::kUPlane
),
266 frame
->visible_data(media::VideoFrame::kVPlane
),
267 frame
->stride(media::VideoFrame::kVPlane
),
273 ppapi::HostResource host_resource
;
274 host_resource
.SetHostResource(pp_instance(), shared_image_
->GetReference());
276 // Convert a video timestamp to a PP_TimeTicks (a double, in seconds).
277 const PP_TimeTicks timestamp
= frame
->timestamp().InSecondsF();
279 ppapi::proxy::SerializedHandle serialized_handle
;
280 serialized_handle
.set_shmem(image_handle
, byte_count
);
281 reply_context_
.params
.AppendHandle(serialized_handle
);
283 host()->SendReply(reply_context_
,
284 PpapiPluginMsg_VideoSource_GetFrameReply(
285 host_resource
, shared_image_desc_
, timestamp
));
287 reply_context_
= ppapi::host::ReplyMessageContext();
290 void PepperVideoSourceHost::SendGetFrameErrorReply(int32_t error
) {
291 reply_context_
.params
.set_result(error
);
294 PpapiPluginMsg_VideoSource_GetFrameReply(
295 ppapi::HostResource(), PP_ImageDataDesc(), 0.0 /* timestamp */));
296 reply_context_
= ppapi::host::ReplyMessageContext();
299 void PepperVideoSourceHost::Close() {
300 if (frame_source_
.get() && !stream_url_
.empty())
301 frame_source_
->Close(frame_receiver_
.get());
303 frame_source_
.reset(NULL
);
306 shared_image_
= NULL
;
309 } // namespace content