Add a few more entries to CFI blacklist.
[chromium-blink-merge.git] / content / renderer / pepper / pepper_video_source_host.cc
blobab6a0f31c56c8a9aa25d1388eb5500d8c60ec85d
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"
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/libyuv/include/libyuv/scale.h"
23 #include "third_party/skia/include/core/SkBitmap.h"
25 using ppapi::host::HostMessageContext;
26 using ppapi::host::ReplyMessageContext;
28 namespace content {
30 PepperVideoSourceHost::FrameReceiver::FrameReceiver(
31 const base::WeakPtr<PepperVideoSourceHost>& host)
32 : host_(host) {}
34 PepperVideoSourceHost::FrameReceiver::~FrameReceiver() {}
36 void PepperVideoSourceHost::FrameReceiver::GotFrame(
37 const scoped_refptr<media::VideoFrame>& frame) {
38 DCHECK(thread_checker_.CalledOnValidThread());
39 if (host_.get()) {
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,
49 PP_Instance instance,
50 PP_Resource resource)
51 : ResourceHost(host->GetPpapiHost(), instance, resource),
52 source_handler_(new VideoSourceHandler(NULL)),
53 get_frame_pending_(false),
54 weak_factory_(this) {
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,
66 OnHostMsgOpen)
67 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoSource_GetFrame,
68 OnHostMsgGetFrame)
69 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoSource_Close,
70 OnHostMsgClose)
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);
78 if (!gurl.is_valid())
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())
103 SendGetFrameReply();
105 return PP_OK_COMPLETIONPENDING;
108 int32_t PepperVideoSourceHost::OnHostMsgClose(HostMessageContext* context) {
109 Close();
110 return PP_OK;
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 base::SharedMemoryHandle image_handle;
124 uint32_t byte_count;
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.
130 base::SharedMemory* local_shm;
131 if (shared_image_->GetSharedMemory(&local_shm, &byte_count) != PP_OK) {
132 SendGetFrameErrorReply(PP_ERROR_FAILED);
133 return;
136 ppapi::proxy::HostDispatcher* dispatcher =
137 ppapi::proxy::HostDispatcher::GetForInstance(pp_instance());
138 if (!dispatcher) {
139 SendGetFrameErrorReply(PP_ERROR_FAILED);
140 return;
143 image_handle =
144 dispatcher->ShareSharedMemoryHandleWithRemote(local_shm->handle());
145 } else {
146 // We need to allocate new shared memory.
147 shared_image_ = NULL; // Release any previous image.
149 ppapi::ScopedPPResource resource(
150 ppapi::ScopedPPResource::PassRef(),
151 ppapi::proxy::PPB_ImageData_Proxy::CreateImageData(
152 pp_instance(),
153 ppapi::PPB_ImageData_Shared::SIMPLE,
154 PP_IMAGEDATAFORMAT_BGRA_PREMUL,
155 PP_MakeSize(dst_size.width(), dst_size.height()),
156 false /* init_to_zero */,
157 &shared_image_desc_,
158 &image_handle,
159 &byte_count));
160 if (!resource) {
161 SendGetFrameErrorReply(PP_ERROR_FAILED);
162 return;
165 ppapi::thunk::EnterResourceNoLock<ppapi::thunk::PPB_ImageData_API>
166 enter_resource(resource, false);
167 if (enter_resource.failed()) {
168 SendGetFrameErrorReply(PP_ERROR_FAILED);
169 return;
172 shared_image_ = static_cast<PPB_ImageData_Impl*>(enter_resource.object());
173 if (!shared_image_.get()) {
174 SendGetFrameErrorReply(PP_ERROR_FAILED);
175 return;
178 DCHECK(!shared_image_->IsMapped()); // New memory should not be mapped.
179 if (!shared_image_->Map() || !shared_image_->GetMappedBitmap() ||
180 !shared_image_->GetMappedBitmap()->getPixels()) {
181 shared_image_ = NULL;
182 SendGetFrameErrorReply(PP_ERROR_FAILED);
183 return;
187 const SkBitmap* bitmap = shared_image_->GetMappedBitmap();
188 if (!bitmap) {
189 SendGetFrameErrorReply(PP_ERROR_FAILED);
190 return;
193 uint8_t* bitmap_pixels = static_cast<uint8_t*>(bitmap->getPixels());
194 if (!bitmap_pixels) {
195 SendGetFrameErrorReply(PP_ERROR_FAILED);
196 return;
199 // Calculate the portion of the |last_frame_| that should be copied into
200 // |bitmap|. If |last_frame_| is lazily scaled, then
201 // last_frame_->visible_rect()._size() != last_frame_.natural_size().
202 scoped_refptr<media::VideoFrame> frame;
203 if (dst_size == last_frame_->visible_rect().size()) {
204 // No scaling is needed, convert directly from last_frame_.
205 frame = last_frame_;
206 // Frame resolution doesn't change frequently, so don't keep any unnecessary
207 // buffers around.
208 scaled_frame_ = NULL;
209 } else {
210 // We need to create an intermediate scaled frame. Make sure we have
211 // allocated one of correct size.
212 if (!scaled_frame_.get() || scaled_frame_->coded_size() != dst_size) {
213 scaled_frame_ = media::VideoFrame::CreateFrame(
214 media::PIXEL_FORMAT_I420, dst_size, gfx::Rect(dst_size), dst_size,
215 last_frame_->timestamp());
216 if (!scaled_frame_.get()) {
217 LOG(ERROR) << "Failed to allocate a media::VideoFrame";
218 SendGetFrameErrorReply(PP_ERROR_FAILED);
219 return;
222 scaled_frame_->set_timestamp(last_frame_->timestamp());
223 libyuv::I420Scale(last_frame_->visible_data(media::VideoFrame::kYPlane),
224 last_frame_->stride(media::VideoFrame::kYPlane),
225 last_frame_->visible_data(media::VideoFrame::kUPlane),
226 last_frame_->stride(media::VideoFrame::kUPlane),
227 last_frame_->visible_data(media::VideoFrame::kVPlane),
228 last_frame_->stride(media::VideoFrame::kVPlane),
229 last_frame_->visible_rect().width(),
230 last_frame_->visible_rect().height(),
231 scaled_frame_->data(media::VideoFrame::kYPlane),
232 scaled_frame_->stride(media::VideoFrame::kYPlane),
233 scaled_frame_->data(media::VideoFrame::kUPlane),
234 scaled_frame_->stride(media::VideoFrame::kUPlane),
235 scaled_frame_->data(media::VideoFrame::kVPlane),
236 scaled_frame_->stride(media::VideoFrame::kVPlane),
237 dst_size.width(),
238 dst_size.height(),
239 libyuv::kFilterBilinear);
240 frame = scaled_frame_;
242 last_frame_ = NULL;
244 libyuv::I420ToARGB(frame->visible_data(media::VideoFrame::kYPlane),
245 frame->stride(media::VideoFrame::kYPlane),
246 frame->visible_data(media::VideoFrame::kUPlane),
247 frame->stride(media::VideoFrame::kUPlane),
248 frame->visible_data(media::VideoFrame::kVPlane),
249 frame->stride(media::VideoFrame::kVPlane),
250 bitmap_pixels,
251 bitmap->rowBytes(),
252 dst_size.width(),
253 dst_size.height());
255 ppapi::HostResource host_resource;
256 host_resource.SetHostResource(pp_instance(), shared_image_->GetReference());
258 // Convert a video timestamp to a PP_TimeTicks (a double, in seconds).
259 const PP_TimeTicks timestamp = frame->timestamp().InSecondsF();
261 ppapi::proxy::SerializedHandle serialized_handle;
262 serialized_handle.set_shmem(image_handle, byte_count);
263 reply_context_.params.AppendHandle(serialized_handle);
265 host()->SendReply(reply_context_,
266 PpapiPluginMsg_VideoSource_GetFrameReply(
267 host_resource, shared_image_desc_, timestamp));
269 reply_context_ = ppapi::host::ReplyMessageContext();
272 void PepperVideoSourceHost::SendGetFrameErrorReply(int32_t error) {
273 reply_context_.params.set_result(error);
274 host()->SendReply(
275 reply_context_,
276 PpapiPluginMsg_VideoSource_GetFrameReply(
277 ppapi::HostResource(), PP_ImageDataDesc(), 0.0 /* timestamp */));
278 reply_context_ = ppapi::host::ReplyMessageContext();
281 void PepperVideoSourceHost::Close() {
282 if (source_handler_.get() && !stream_url_.empty())
283 source_handler_->Close(frame_receiver_.get());
285 source_handler_.reset(NULL);
286 stream_url_.clear();
288 shared_image_ = NULL;
291 } // namespace content