Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / renderer / pepper / pepper_video_capture_host.cc
blob22b0389a5ae33ea80db1874fe68d61d3cf1c5a31
1 // Copyright (c) 2012 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_capture_host.h"
7 #include "content/renderer/media/media_stream_video_source.h"
8 #include "content/renderer/pepper/host_globals.h"
9 #include "content/renderer/pepper/pepper_media_device_manager.h"
10 #include "content/renderer/pepper/pepper_platform_video_capture.h"
11 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
12 #include "content/renderer/pepper/renderer_ppapi_host_impl.h"
13 #include "content/renderer/render_frame_impl.h"
14 #include "media/base/limits.h"
15 #include "media/base/video_frame.h"
16 #include "ppapi/host/dispatch_host_message.h"
17 #include "ppapi/host/ppapi_host.h"
18 #include "ppapi/proxy/host_dispatcher.h"
19 #include "ppapi/proxy/ppapi_messages.h"
20 #include "ppapi/shared_impl/host_resource.h"
21 #include "ppapi/thunk/enter.h"
22 #include "ppapi/thunk/ppb_buffer_api.h"
24 using ppapi::HostResource;
25 using ppapi::TrackedCallback;
26 using ppapi::thunk::EnterResourceNoLock;
27 using ppapi::thunk::PPB_Buffer_API;
29 namespace {
31 // Maximum number of buffers to actually allocate.
32 const uint32_t kMaxBuffers = 20;
34 } // namespace
36 namespace content {
38 PepperVideoCaptureHost::PepperVideoCaptureHost(RendererPpapiHostImpl* host,
39 PP_Instance instance,
40 PP_Resource resource)
41 : ResourceHost(host->GetPpapiHost(), instance, resource),
42 renderer_ppapi_host_(host),
43 buffer_count_hint_(0),
44 status_(PP_VIDEO_CAPTURE_STATUS_STOPPED),
45 enumeration_helper_(this,
46 PepperMediaDeviceManager::GetForRenderFrame(
47 host->GetRenderFrameForInstance(pp_instance())),
48 PP_DEVICETYPE_DEV_VIDEOCAPTURE,
49 host->GetDocumentURL(instance)) {
52 PepperVideoCaptureHost::~PepperVideoCaptureHost() {
53 Close();
56 bool PepperVideoCaptureHost::Init() {
57 return !!renderer_ppapi_host_->GetPluginInstance(pp_instance());
60 int32_t PepperVideoCaptureHost::OnResourceMessageReceived(
61 const IPC::Message& msg,
62 ppapi::host::HostMessageContext* context) {
63 int32_t result = PP_ERROR_FAILED;
64 if (enumeration_helper_.HandleResourceMessage(msg, context, &result))
65 return result;
67 PPAPI_BEGIN_MESSAGE_MAP(PepperVideoCaptureHost, msg)
68 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoCapture_Open, OnOpen)
69 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoCapture_StartCapture,
70 OnStartCapture)
71 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_VideoCapture_ReuseBuffer,
72 OnReuseBuffer)
73 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoCapture_StopCapture,
74 OnStopCapture)
75 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0(PpapiHostMsg_VideoCapture_Close,
76 OnClose)
77 PPAPI_END_MESSAGE_MAP()
78 return PP_ERROR_FAILED;
81 void PepperVideoCaptureHost::OnInitialized(bool succeeded) {
82 if (succeeded) {
83 open_reply_context_.params.set_result(PP_OK);
84 } else {
85 DetachPlatformVideoCapture();
86 open_reply_context_.params.set_result(PP_ERROR_FAILED);
89 host()->SendReply(open_reply_context_,
90 PpapiPluginMsg_VideoCapture_OpenReply());
93 void PepperVideoCaptureHost::OnStarted() {
94 if (SetStatus(PP_VIDEO_CAPTURE_STATUS_STARTED, false))
95 SendStatus();
98 void PepperVideoCaptureHost::OnStopped() {
99 if (SetStatus(PP_VIDEO_CAPTURE_STATUS_STOPPED, false))
100 SendStatus();
103 void PepperVideoCaptureHost::OnPaused() {
104 if (SetStatus(PP_VIDEO_CAPTURE_STATUS_PAUSED, false))
105 SendStatus();
108 void PepperVideoCaptureHost::OnError() {
109 PostErrorReply();
112 void PepperVideoCaptureHost::PostErrorReply() {
113 // It either comes because some error was detected while starting (e.g. 2
114 // conflicting "master" resolution), or because the browser failed to start
115 // the capture.
116 SetStatus(PP_VIDEO_CAPTURE_STATUS_STOPPED, true);
117 host()->SendUnsolicitedReply(
118 pp_resource(),
119 PpapiPluginMsg_VideoCapture_OnError(
120 static_cast<uint32_t>(PP_ERROR_FAILED)));
123 void PepperVideoCaptureHost::OnFrameReady(
124 const scoped_refptr<media::VideoFrame>& frame) {
125 DCHECK(frame.get());
127 if (alloc_size_ != frame->visible_rect().size() || buffers_.empty()) {
128 alloc_size_ = frame->visible_rect().size();
129 double frame_rate;
130 int rounded_frame_rate;
131 if (frame->metadata()->GetDouble(media::VideoFrameMetadata::FRAME_RATE,
132 &frame_rate))
133 rounded_frame_rate = static_cast<int>(frame_rate + 0.5 /* round */);
134 else
135 rounded_frame_rate = MediaStreamVideoSource::kUnknownFrameRate;
136 AllocBuffers(alloc_size_, rounded_frame_rate);
139 for (uint32_t i = 0; i < buffers_.size(); ++i) {
140 if (!buffers_[i].in_use) {
141 DCHECK_EQ(frame->format(), media::PIXEL_FORMAT_I420);
142 if (buffers_[i].buffer->size() <
143 media::VideoFrame::AllocationSize(frame->format(), alloc_size_)) {
144 // TODO(ihf): handle size mismatches gracefully here.
145 return;
147 uint8* dst = reinterpret_cast<uint8*>(buffers_[i].data);
148 static_assert(media::VideoFrame::kYPlane == 0, "y plane should be 0");
149 static_assert(media::VideoFrame::kUPlane == 1, "u plane should be 1");
150 static_assert(media::VideoFrame::kVPlane == 2, "v plane should be 2");
151 for (size_t j = 0; j < media::VideoFrame::NumPlanes(frame->format());
152 ++j) {
153 const uint8* src = frame->visible_data(j);
154 const size_t row_bytes = frame->row_bytes(j);
155 const size_t src_stride = frame->stride(j);
156 for (int k = 0; k < frame->rows(j); ++k) {
157 memcpy(dst, src, row_bytes);
158 dst += row_bytes;
159 src += src_stride;
162 buffers_[i].in_use = true;
163 host()->SendUnsolicitedReply(
164 pp_resource(), PpapiPluginMsg_VideoCapture_OnBufferReady(i));
165 return;
170 void PepperVideoCaptureHost::AllocBuffers(const gfx::Size& resolution,
171 int frame_rate) {
172 PP_VideoCaptureDeviceInfo_Dev info = {
173 static_cast<uint32_t>(resolution.width()),
174 static_cast<uint32_t>(resolution.height()),
175 static_cast<uint32_t>(frame_rate)};
176 ReleaseBuffers();
178 const size_t size = media::VideoFrame::AllocationSize(
179 media::PIXEL_FORMAT_I420, gfx::Size(info.width, info.height));
181 ppapi::proxy::ResourceMessageReplyParams params(pp_resource(), 0);
183 // Allocate buffers. We keep a reference to them, that is released in
184 // ReleaseBuffers. In the mean time, we prepare the resource and handle here
185 // for sending below.
186 std::vector<HostResource> buffer_host_resources;
187 buffers_.reserve(buffer_count_hint_);
188 ppapi::ResourceTracker* tracker = HostGlobals::Get()->GetResourceTracker();
189 ppapi::proxy::HostDispatcher* dispatcher =
190 ppapi::proxy::HostDispatcher::GetForInstance(pp_instance());
191 for (size_t i = 0; i < buffer_count_hint_; ++i) {
192 PP_Resource res = PPB_Buffer_Impl::Create(pp_instance(), size);
193 if (!res)
194 break;
196 EnterResourceNoLock<PPB_Buffer_API> enter(res, true);
197 DCHECK(enter.succeeded());
199 BufferInfo buf;
200 buf.buffer = static_cast<PPB_Buffer_Impl*>(enter.object());
201 buf.data = buf.buffer->Map();
202 if (!buf.data) {
203 tracker->ReleaseResource(res);
204 break;
206 buffers_.push_back(buf);
208 // Add to HostResource array to be sent.
210 HostResource host_resource;
211 host_resource.SetHostResource(pp_instance(), res);
212 buffer_host_resources.push_back(host_resource);
214 // Add a reference for the plugin, which is resposible for releasing it.
215 tracker->AddRefResource(res);
218 // Add the serialized shared memory handle to params. FileDescriptor is
219 // treated in special case.
221 EnterResourceNoLock<PPB_Buffer_API> enter(res, true);
222 DCHECK(enter.succeeded());
223 base::SharedMemory* shm;
224 int32_t result = enter.object()->GetSharedMemory(&shm);
225 DCHECK(result == PP_OK);
226 params.AppendHandle(ppapi::proxy::SerializedHandle(
227 dispatcher->ShareSharedMemoryHandleWithRemote(shm->handle()), size));
231 if (buffers_.empty()) {
232 // We couldn't allocate/map buffers at all. Send an error and stop the
233 // capture.
234 SetStatus(PP_VIDEO_CAPTURE_STATUS_STOPPING, true);
235 platform_video_capture_->StopCapture();
236 PostErrorReply();
237 return;
240 host()->Send(
241 new PpapiPluginMsg_ResourceReply(params,
242 PpapiPluginMsg_VideoCapture_OnDeviceInfo(
243 info, buffer_host_resources, size)));
246 int32_t PepperVideoCaptureHost::OnOpen(
247 ppapi::host::HostMessageContext* context,
248 const std::string& device_id,
249 const PP_VideoCaptureDeviceInfo_Dev& requested_info,
250 uint32_t buffer_count) {
251 if (platform_video_capture_.get())
252 return PP_ERROR_FAILED;
254 SetRequestedInfo(requested_info, buffer_count);
256 GURL document_url = renderer_ppapi_host_->GetDocumentURL(pp_instance());
257 if (!document_url.is_valid())
258 return PP_ERROR_FAILED;
260 platform_video_capture_.reset(new PepperPlatformVideoCapture(
261 renderer_ppapi_host_->GetRenderFrameForInstance(pp_instance())->
262 GetRoutingID(),
263 device_id,
264 document_url,
265 this));
267 open_reply_context_ = context->MakeReplyMessageContext();
269 return PP_OK_COMPLETIONPENDING;
272 int32_t PepperVideoCaptureHost::OnStartCapture(
273 ppapi::host::HostMessageContext* context) {
274 if (!SetStatus(PP_VIDEO_CAPTURE_STATUS_STARTING, false) ||
275 !platform_video_capture_.get())
276 return PP_ERROR_FAILED;
278 DCHECK(buffers_.empty());
280 // It's safe to call this regardless it's capturing or not, because
281 // PepperPlatformVideoCapture maintains the state.
282 platform_video_capture_->StartCapture(video_capture_params_);
283 return PP_OK;
286 int32_t PepperVideoCaptureHost::OnReuseBuffer(
287 ppapi::host::HostMessageContext* context,
288 uint32_t buffer) {
289 if (buffer >= buffers_.size() || !buffers_[buffer].in_use)
290 return PP_ERROR_BADARGUMENT;
291 buffers_[buffer].in_use = false;
292 return PP_OK;
295 int32_t PepperVideoCaptureHost::OnStopCapture(
296 ppapi::host::HostMessageContext* context) {
297 return StopCapture();
300 int32_t PepperVideoCaptureHost::OnClose(
301 ppapi::host::HostMessageContext* context) {
302 return Close();
305 int32_t PepperVideoCaptureHost::StopCapture() {
306 if (!SetStatus(PP_VIDEO_CAPTURE_STATUS_STOPPING, false))
307 return PP_ERROR_FAILED;
309 DCHECK(platform_video_capture_.get());
311 ReleaseBuffers();
312 // It's safe to call this regardless it's capturing or not, because
313 // PepperPlatformVideoCapture maintains the state.
314 platform_video_capture_->StopCapture();
315 return PP_OK;
318 int32_t PepperVideoCaptureHost::Close() {
319 if (!platform_video_capture_.get())
320 return PP_OK;
322 StopCapture();
323 DCHECK(buffers_.empty());
324 DetachPlatformVideoCapture();
325 return PP_OK;
328 void PepperVideoCaptureHost::ReleaseBuffers() {
329 ppapi::ResourceTracker* tracker = HostGlobals::Get()->GetResourceTracker();
330 for (size_t i = 0; i < buffers_.size(); ++i) {
331 buffers_[i].buffer->Unmap();
332 tracker->ReleaseResource(buffers_[i].buffer->pp_resource());
334 buffers_.clear();
337 void PepperVideoCaptureHost::SendStatus() {
338 host()->SendUnsolicitedReply(pp_resource(),
339 PpapiPluginMsg_VideoCapture_OnStatus(status_));
342 void PepperVideoCaptureHost::SetRequestedInfo(
343 const PP_VideoCaptureDeviceInfo_Dev& device_info,
344 uint32_t buffer_count) {
345 // Clamp the buffer count to between 1 and |kMaxBuffers|.
346 buffer_count_hint_ = std::min(std::max(buffer_count, 1U), kMaxBuffers);
347 // Clamp the frame rate to between 1 and |kMaxFramesPerSecond - 1|.
348 int frames_per_second =
349 std::min(std::max(device_info.frames_per_second, 1U),
350 static_cast<uint32_t>(media::limits::kMaxFramesPerSecond - 1));
352 video_capture_params_.requested_format = media::VideoCaptureFormat(
353 gfx::Size(device_info.width, device_info.height), frames_per_second,
354 media::PIXEL_FORMAT_I420);
357 void PepperVideoCaptureHost::DetachPlatformVideoCapture() {
358 if (platform_video_capture_) {
359 platform_video_capture_->DetachEventHandler();
360 platform_video_capture_.reset();
364 bool PepperVideoCaptureHost::SetStatus(PP_VideoCaptureStatus_Dev status,
365 bool forced) {
366 if (!forced) {
367 switch (status) {
368 case PP_VIDEO_CAPTURE_STATUS_STOPPED:
369 if (status_ != PP_VIDEO_CAPTURE_STATUS_STOPPING)
370 return false;
371 break;
372 case PP_VIDEO_CAPTURE_STATUS_STARTING:
373 if (status_ != PP_VIDEO_CAPTURE_STATUS_STOPPED)
374 return false;
375 break;
376 case PP_VIDEO_CAPTURE_STATUS_STARTED:
377 switch (status_) {
378 case PP_VIDEO_CAPTURE_STATUS_STARTING:
379 case PP_VIDEO_CAPTURE_STATUS_PAUSED:
380 break;
381 default:
382 return false;
384 break;
385 case PP_VIDEO_CAPTURE_STATUS_PAUSED:
386 switch (status_) {
387 case PP_VIDEO_CAPTURE_STATUS_STARTING:
388 case PP_VIDEO_CAPTURE_STATUS_STARTED:
389 break;
390 default:
391 return false;
393 break;
394 case PP_VIDEO_CAPTURE_STATUS_STOPPING:
395 switch (status_) {
396 case PP_VIDEO_CAPTURE_STATUS_STARTING:
397 case PP_VIDEO_CAPTURE_STATUS_STARTED:
398 case PP_VIDEO_CAPTURE_STATUS_PAUSED:
399 break;
400 default:
401 return false;
403 break;
407 status_ = status;
408 return true;
411 PepperVideoCaptureHost::BufferInfo::BufferInfo()
412 : in_use(false), data(NULL), buffer() {
415 PepperVideoCaptureHost::BufferInfo::~BufferInfo() {
418 } // namespace content