OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "content/renderer/pepper/pepper_video_capture_host.h" | |
6 | |
7 #include "content/public/renderer/renderer_ppapi_host.h" | |
8 #include "ppapi/host/dispatch_host_message.h" | |
9 #include "ppapi/host/host_message_context.h" | |
10 #include "ppapi/host/ppapi_host.h" | |
11 #include "ppapi/proxy/host_dispatcher.h" | |
12 #include "ppapi/proxy/ppapi_messages.h" | |
13 #include "ppapi/shared_impl/host_resource.h" | |
14 #include "ppapi/shared_impl/ppapi_globals.h" | |
15 #include "ppapi/shared_impl/ppb_device_ref_shared.h" | |
16 #include "ppapi/thunk/enter.h" | |
17 #include "ppapi/thunk/ppb_buffer_api.h" | |
18 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | |
19 #include "webkit/plugins/ppapi/ppb_buffer_impl.h" | |
20 #include "webkit/plugins/ppapi/resource_helper.h" | |
21 | |
22 using ppapi::DeviceRefData; | |
23 using ppapi::HostResource; | |
24 using ppapi::PpapiGlobals; | |
25 using ppapi::TrackedCallback; | |
26 using ppapi::thunk::EnterResourceNoLock; | |
27 using ppapi::thunk::PPB_Buffer_API; | |
28 using ppapi::thunk::PPB_BufferTrusted_API; | |
29 using webkit::ppapi::ResourceHelper; | |
30 using webkit::ppapi::PPB_Buffer_Impl; | |
31 using webkit::ppapi::PluginInstance; | |
32 | |
33 namespace { | |
34 | |
35 // Maximum number of buffers to actually allocate. | |
36 const uint32_t kMaxBuffers = 20; | |
37 | |
38 } // namespace | |
39 | |
40 namespace content { | |
41 | |
42 PepperVideoCaptureHost::PepperVideoCaptureHost(RendererPpapiHost* host, | |
43 PP_Instance instance, | |
44 PP_Resource resource) | |
45 : ResourceHost(host->GetPpapiHost(), instance, resource), | |
46 ppapi::PPB_VideoCapture_Shared() { | |
47 } | |
48 | |
49 PepperVideoCaptureHost::~PepperVideoCaptureHost() { | |
50 Close(); | |
51 } | |
52 | |
53 bool PepperVideoCaptureHost::Init() { | |
54 PluginInstance* instance = GetPluginInstance(); | |
55 return !!instance; | |
56 } | |
57 | |
58 int32_t PepperVideoCaptureHost::OnResourceMessageReceived( | |
59 const IPC::Message& msg, | |
60 ppapi::host::HostMessageContext* context) { | |
61 IPC_BEGIN_MESSAGE_MAP(PepperVideoCaptureHost, msg) | |
62 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0( | |
63 PpapiHostMsg_VideoCapture_EnumerateDevices, | |
64 OnEnumerateDevices) | |
65 PPAPI_DISPATCH_HOST_RESOURCE_CALL( | |
66 PpapiHostMsg_VideoCapture_Open, | |
67 OnOpen) | |
68 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0( | |
69 PpapiHostMsg_VideoCapture_StartCapture, | |
70 OnStartCapture) | |
71 PPAPI_DISPATCH_HOST_RESOURCE_CALL( | |
72 PpapiHostMsg_VideoCapture_ReuseBuffer, | |
73 OnReuseBuffer) | |
74 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0( | |
75 PpapiHostMsg_VideoCapture_StopCapture, | |
76 OnStopCapture) | |
77 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0( | |
78 PpapiHostMsg_VideoCapture_Close, | |
79 OnClose) | |
80 PPAPI_DISPATCH_HOST_RESOURCE_CALL( | |
81 PpapiHostMsg_VideoCapture_StartCapture0_1, | |
82 OnStartCapture0_1) | |
83 IPC_END_MESSAGE_MAP() | |
84 return PP_ERROR_FAILED; | |
85 } | |
86 | |
87 void PepperVideoCaptureHost::OnInitialized(media::VideoCapture* capture, | |
88 bool succeeded) { | |
89 DCHECK(capture == platform_video_capture_.get()); | |
90 | |
91 if (open_state_ == BEFORE_OPEN && succeeded) | |
92 open_state_ = OPENED; | |
93 | |
94 host()->SendReply(open_reply_context_, | |
95 PpapiPluginMsg_VideoCapture_OpenACK(PP_OK)); | |
96 } | |
97 | |
98 void PepperVideoCaptureHost::OnStarted(media::VideoCapture* capture) { | |
99 if (SetStatus(PP_VIDEO_CAPTURE_STATUS_STARTED, false)) | |
100 SendStatus(); | |
101 } | |
102 | |
103 void PepperVideoCaptureHost::OnStopped(media::VideoCapture* capture) { | |
104 if (SetStatus(PP_VIDEO_CAPTURE_STATUS_STOPPED, false)) | |
105 SendStatus(); | |
106 } | |
107 | |
108 void PepperVideoCaptureHost::OnPaused(media::VideoCapture* capture) { | |
109 if (SetStatus(PP_VIDEO_CAPTURE_STATUS_PAUSED, false)) | |
110 SendStatus(); | |
111 } | |
112 | |
113 void PepperVideoCaptureHost::OnError(media::VideoCapture* capture, | |
114 int error_code) { | |
115 // Today, the media layer only sends "1" as an error. | |
116 DCHECK(error_code == 1); | |
117 // It either comes because some error was detected while starting (e.g. 2 | |
118 // conflicting "master" resolution), or because the browser failed to start | |
119 // the capture. | |
120 SetStatus(PP_VIDEO_CAPTURE_STATUS_STOPPED, true); | |
121 host()->SendUnsolicitedReply(pp_resource(), | |
122 PpapiPluginMsg_VideoCapture_OnError(PP_ERROR_FAILED)); | |
123 } | |
124 | |
125 void PepperVideoCaptureHost::OnRemoved(media::VideoCapture* capture) { | |
126 } | |
127 | |
128 void PepperVideoCaptureHost::OnBufferReady( | |
129 media::VideoCapture* capture, | |
130 scoped_refptr<media::VideoCapture::VideoFrameBuffer> buffer) { | |
131 DCHECK(buffer.get()); | |
132 for (uint32_t i = 0; i < buffers_.size(); ++i) { | |
133 if (!buffers_[i].in_use) { | |
134 // TODO(ihf): Switch to a size calculation based on stride. | |
135 // Stride is filled out now but not more meaningful than size | |
136 // until wjia unifies VideoFrameBuffer and media::VideoFrame. | |
137 size_t size = std::min(static_cast<size_t>(buffers_[i].buffer->size()), | |
138 buffer->buffer_size); | |
139 memcpy(buffers_[i].data, buffer->memory_pointer, size); | |
140 buffers_[i].in_use = true; | |
141 platform_video_capture_->FeedBuffer(buffer); | |
142 host()->SendUnsolicitedReply(pp_resource(), | |
143 PpapiPluginMsg_VideoCapture_OnBufferReady(i)); | |
144 return; | |
145 } | |
146 } | |
147 | |
148 // No free slot, just discard the frame and tell the media layer it can | |
149 // re-use the buffer. | |
150 platform_video_capture_->FeedBuffer(buffer); | |
151 } | |
152 | |
153 void PepperVideoCaptureHost::OnDeviceInfoReceived( | |
154 media::VideoCapture* capture, | |
155 const media::VideoCaptureParams& device_info) { | |
156 PP_VideoCaptureDeviceInfo_Dev info = { | |
157 static_cast<uint32_t>(device_info.width), | |
158 static_cast<uint32_t>(device_info.height), | |
159 static_cast<uint32_t>(device_info.frame_per_second) | |
160 }; | |
161 ReleaseBuffers(); | |
162 | |
163 // YUV 4:2:0 | |
164 int uv_width = info.width / 2; | |
165 int uv_height = info.height / 2; | |
166 size_t size = info.width * info.height + 2 * uv_width * uv_height; | |
167 | |
168 ppapi::proxy::ResourceMessageReplyParams params(pp_resource(), 0); | |
169 | |
170 // Allocate buffers. We keep a reference to them, that is released in | |
171 // ReleaseBuffers. In the mean time, we prepare the resource and handle here | |
172 // for sending below. | |
173 std::vector<HostResource> buffer_host_resources; | |
174 buffers_.reserve(buffer_count_hint_); | |
175 ppapi::proxy::HostDispatcher* dispatcher = | |
176 ppapi::proxy::HostDispatcher::GetForInstance(pp_instance()); | |
177 for (size_t i = 0; i < buffer_count_hint_; ++i) { | |
178 PP_Resource res = PPB_Buffer_Impl::Create(pp_instance(), size); | |
179 if (!res) | |
180 break; | |
181 | |
182 EnterResourceNoLock<PPB_Buffer_API> enter(res, true); | |
183 DCHECK(enter.succeeded()); | |
184 | |
185 BufferInfo buf; | |
186 buf.buffer = static_cast<PPB_Buffer_Impl*>(enter.object()); | |
187 buf.data = buf.buffer->Map(); | |
188 if (!buf.data) { | |
189 PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource(res); | |
190 break; | |
191 } | |
192 buffers_.push_back(buf); | |
193 | |
194 // Add to HostResource array to be sent. | |
195 { | |
196 HostResource host_resource; | |
197 host_resource.SetHostResource(pp_instance(), res); | |
198 buffer_host_resources.push_back(host_resource); | |
199 } | |
200 | |
201 // Add the serialized shared memory handle to params. FileDescriptor is | |
202 // treated in special case. | |
203 { | |
204 EnterResourceNoLock<PPB_BufferTrusted_API> enter(res, true); | |
205 DCHECK(enter.succeeded()); | |
206 int handle; | |
207 int32_t result = enter.object()->GetSharedMemory(&handle); | |
208 DCHECK(result == PP_OK); | |
209 // TODO(piman/brettw): Change trusted interface to return a PP_FileHandle, | |
210 // those casts are ugly. | |
211 base::PlatformFile platform_file = | |
212 #if defined(OS_WIN) | |
213 reinterpret_cast<HANDLE>(static_cast<intptr_t>(handle)); | |
214 #elif defined(OS_POSIX) | |
215 handle; | |
216 #else | |
217 #error Not implemented. | |
218 #endif | |
219 params.AppendHandle( | |
220 ppapi::proxy::SerializedHandle( | |
221 dispatcher->ShareHandleWithRemote(platform_file, false), | |
222 size)); | |
223 } | |
224 } | |
225 | |
226 if (buffers_.empty()) { | |
227 // We couldn't allocate/map buffers at all. Send an error and stop the | |
228 // capture. | |
229 SetStatus(PP_VIDEO_CAPTURE_STATUS_STOPPING, true); | |
230 platform_video_capture_->StopCapture(this); | |
231 OnError(capture, PP_ERROR_NOMEMORY); | |
232 return; | |
233 } | |
234 | |
235 host()->Send(new PpapiPluginMsg_ResourceReply( | |
236 params, PpapiPluginMsg_VideoCapture_OnDeviceInfo( | |
237 info, buffer_host_resources, size))); | |
238 } | |
239 | |
240 PluginInstance* PepperVideoCaptureHost::GetPluginInstance() const { | |
241 return ResourceHelper::PPInstanceToPluginInstance(pp_instance()); | |
242 } | |
243 | |
244 int32_t PepperVideoCaptureHost::OnEnumerateDevices( | |
245 ppapi::host::HostMessageContext* context) { | |
246 PluginInstance* instance = GetPluginInstance(); | |
247 if (!instance) | |
248 return PP_ERROR_FAILED; | |
249 | |
250 enum_reply_context_ = context->MakeReplyMessageContext(); | |
251 instance->delegate()->EnumerateDevices( | |
yzshen1
2012/10/25 20:41:05
FYI: I don't think we want to use the plugin deleg
victorhsieh
2012/10/26 02:34:54
I see.
| |
252 PP_DEVICETYPE_DEV_VIDEOCAPTURE, | |
253 base::Bind(&PepperVideoCaptureHost::EnumerateDevicesCallbackFunc, | |
254 AsWeakPtr())); | |
255 return PP_OK_COMPLETIONPENDING; | |
256 } | |
257 | |
258 int32_t PepperVideoCaptureHost::OnOpen( | |
259 ppapi::host::HostMessageContext* context, | |
260 const std::string& device_id, | |
261 const PP_VideoCaptureDeviceInfo_Dev& requested_info, | |
262 uint32_t buffer_count) { | |
263 if (open_state_ != BEFORE_OPEN) | |
264 return PP_ERROR_FAILED; | |
265 | |
266 PluginInstance* instance = GetPluginInstance(); | |
267 if (!instance) | |
268 return PP_ERROR_FAILED; | |
269 | |
270 SetRequestedInfo(requested_info, buffer_count); | |
271 | |
272 DCHECK(!platform_video_capture_.get()); | |
273 platform_video_capture_ = | |
274 instance->delegate()->CreateVideoCapture(device_id, this); | |
275 | |
276 open_reply_context_ = context->MakeReplyMessageContext(); | |
277 | |
278 // It is able to complete synchronously if the default device is used. | |
279 bool sync_completion = device_id.empty(); | |
280 if (sync_completion) { | |
281 // Send OpenACK directly, but still need to return PP_OK_COMPLETIONPENDING | |
282 // to make PluginResource happy. | |
283 OnInitialized(platform_video_capture_.get(), true); | |
284 return PP_OK_COMPLETIONPENDING; | |
victorhsieh
2012/10/25 09:16:42
The orignal impl returns PP_OK, but that will fail
yzshen1
2012/10/25 20:41:05
Yes. The original proxy code uses EnterHostFromHos
| |
285 } | |
286 | |
287 return PP_OK_COMPLETIONPENDING; | |
288 } | |
289 | |
290 int32_t PepperVideoCaptureHost::OnStartCapture( | |
291 ppapi::host::HostMessageContext* context) { | |
292 return StartCapture(); | |
293 } | |
294 | |
295 int32_t PepperVideoCaptureHost::OnReuseBuffer( | |
296 ppapi::host::HostMessageContext* context, | |
297 uint32_t buffer) { | |
298 if (buffer >= buffers_.size() || !buffers_[buffer].in_use) | |
299 return PP_ERROR_BADARGUMENT; | |
300 buffers_[buffer].in_use = false; | |
301 return PP_OK; | |
302 } | |
303 | |
304 int32_t PepperVideoCaptureHost::OnStopCapture( | |
305 ppapi::host::HostMessageContext* context) { | |
306 return StopCapture(); | |
307 } | |
308 | |
309 int32_t PepperVideoCaptureHost::OnClose( | |
310 ppapi::host::HostMessageContext* context) { | |
311 return Close(); | |
312 } | |
313 | |
314 int32_t PepperVideoCaptureHost::OnStartCapture0_1( | |
315 ppapi::host::HostMessageContext* context, | |
316 const PP_VideoCaptureDeviceInfo_Dev& requested_info, | |
317 uint32_t buffer_count) { | |
318 return StartCapture0_1(requested_info, buffer_count); | |
319 } | |
320 | |
321 int32_t PepperVideoCaptureHost::Close() { | |
322 if (open_state_ == CLOSED) | |
323 return PP_OK; | |
324 | |
325 StopCapture(); | |
326 DCHECK(buffers_.empty()); | |
327 DetachPlatformVideoCapture(); | |
328 | |
329 open_state_ = CLOSED; | |
330 | |
331 return PP_OK; | |
332 } | |
333 | |
334 int32_t PepperVideoCaptureHost::InternalStartCapture() { | |
335 DCHECK(buffers_.empty()); | |
336 platform_video_capture_->StartCapture(this, capability_); | |
337 return PP_OK; | |
338 } | |
339 | |
340 int32_t PepperVideoCaptureHost::InternalStopCapture() { | |
341 ReleaseBuffers(); | |
342 platform_video_capture_->StopCapture(this); | |
343 return PP_OK; | |
344 } | |
345 | |
346 int32_t PepperVideoCaptureHost::InternalStartCapture0_1( | |
347 const PP_VideoCaptureDeviceInfo_Dev& requested_info, | |
348 uint32_t buffer_count) { | |
349 if (platform_video_capture_.get()) { | |
350 return PP_ERROR_FAILED; | |
351 } | |
352 | |
353 PluginInstance* instance = GetPluginInstance(); | |
354 if (!instance) { | |
355 SetStatus(PP_VIDEO_CAPTURE_STATUS_STOPPED, true); | |
356 return PP_ERROR_FAILED; | |
357 } | |
358 | |
359 DCHECK(buffers_.empty()); | |
360 | |
361 SetRequestedInfo(requested_info, buffer_count); | |
362 | |
363 DetachPlatformVideoCapture(); | |
364 platform_video_capture_ = | |
365 instance->delegate()->CreateVideoCapture("", this); | |
366 platform_video_capture_->StartCapture(this, capability_); | |
367 | |
368 return PP_OK; | |
369 } | |
370 | |
371 void PepperVideoCaptureHost::ReleaseBuffers() { | |
372 ::ppapi::ResourceTracker* tracker = PpapiGlobals::Get()->GetResourceTracker(); | |
373 for (size_t i = 0; i < buffers_.size(); ++i) { | |
374 buffers_[i].buffer->Unmap(); | |
375 tracker->ReleaseResource(buffers_[i].buffer->pp_resource()); | |
376 } | |
377 buffers_.clear(); | |
378 } | |
379 | |
380 void PepperVideoCaptureHost::SendStatus() { | |
381 host()->SendUnsolicitedReply(pp_resource(), | |
382 PpapiPluginMsg_VideoCapture_OnStatus(status_)); | |
383 } | |
384 | |
385 void PepperVideoCaptureHost::SetRequestedInfo( | |
386 const PP_VideoCaptureDeviceInfo_Dev& device_info, | |
387 uint32_t buffer_count) { | |
388 // Clamp the buffer count to between 1 and |kMaxBuffers|. | |
389 buffer_count_hint_ = std::min(std::max(buffer_count, 1U), kMaxBuffers); | |
390 | |
391 capability_.width = device_info.width; | |
392 capability_.height = device_info.height; | |
393 capability_.frame_rate = device_info.frames_per_second; | |
394 capability_.expected_capture_delay = 0; // Ignored. | |
395 capability_.color = media::VideoCaptureCapability::kI420; | |
396 capability_.interlaced = false; // Ignored. | |
397 } | |
398 | |
399 void PepperVideoCaptureHost::DetachPlatformVideoCapture() { | |
400 if (platform_video_capture_.get()) { | |
401 platform_video_capture_->DetachEventHandler(); | |
402 platform_video_capture_ = NULL; | |
403 } | |
404 } | |
405 | |
406 void PepperVideoCaptureHost::EnumerateDevicesCallbackFunc( | |
407 int request_id, | |
408 bool succeeded, | |
409 const std::vector<ppapi::DeviceRefData>& devices) { | |
410 PluginInstance* instance = GetPluginInstance(); | |
411 if (instance) | |
412 instance->delegate()->StopEnumerateDevices(request_id); | |
413 | |
414 if (succeeded) { | |
415 host()->SendReply(enum_reply_context_, | |
416 PpapiPluginMsg_VideoCapture_EnumerateDevicesResponse( | |
417 PP_OK, | |
418 devices)); | |
419 } else { | |
420 host()->SendReply(enum_reply_context_, | |
421 PpapiPluginMsg_VideoCapture_EnumerateDevicesResponse( | |
422 PP_ERROR_FAILED, | |
423 std::vector<DeviceRefData>())); | |
424 } | |
425 } | |
426 | |
427 PepperVideoCaptureHost::BufferInfo::BufferInfo() | |
428 : in_use(false), | |
429 data(NULL), | |
430 buffer() { | |
431 } | |
432 | |
433 PepperVideoCaptureHost::BufferInfo::~BufferInfo() { | |
434 } | |
435 | |
436 } // namespace content | |
OLD | NEW |