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" | |
yzshen1
2012/11/06 21:56:23
you have included it in .h.
victorhsieh
2012/11/08 09:20:18
IWYU says we should include them.
http://code.goog
yzshen1
2012/11/10 01:14:40
I think that is not true:
--------------------
"In
victorhsieh
2012/11/13 03:10:53
I see. Done.
| |
8 #include "ppapi/host/dispatch_host_message.h" | |
9 #include "ppapi/host/host_message_context.h" | |
yzshen1
2012/11/06 21:56:23
ditto.
victorhsieh
2012/11/13 03:10:53
Done.
| |
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" | |
yzshen1
2012/11/06 21:56:23
ditto.
victorhsieh
2012/11/13 03:10:53
Done.
| |
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" | |
yzshen1
2012/11/06 21:56:23
ditto.
victorhsieh
2012/11/13 03:10:53
Done.
| |
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, | |
yzshen1
2012/11/06 21:56:23
wrong indent.
victorhsieh
2012/11/08 09:20:18
Done.
| |
44 PP_Resource resource) | |
45 : ResourceHost(host->GetPpapiHost(), instance, resource), | |
yzshen1
2012/11/06 21:56:23
wrong indent: four spaces.
victorhsieh
2012/11/08 09:20:18
Done.
| |
46 status_(PP_VIDEO_CAPTURE_STATUS_STOPPED) { | |
yzshen1
2012/11/06 21:56:23
please init buffer_count_hint_.
victorhsieh
2012/11/08 09:20:18
Done.
| |
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, | |
yzshen1
2012/11/06 21:56:23
wrong indent.
victorhsieh
2012/11/08 09:20:18
Done.
| |
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 IPC_END_MESSAGE_MAP() | |
81 return PP_ERROR_FAILED; | |
82 } | |
83 | |
84 void PepperVideoCaptureHost::OnInitialized(media::VideoCapture* capture, | |
85 bool succeeded) { | |
86 DCHECK(capture == platform_video_capture_.get()); | |
87 | |
88 if (succeeded) { | |
89 open_reply_context_.params.set_result(PP_OK); | |
90 } else { | |
91 DetachPlatformVideoCapture(); | |
92 open_reply_context_.params.set_result(PP_ERROR_FAILED); | |
93 } | |
94 | |
95 host()->SendReply(open_reply_context_, | |
96 PpapiPluginMsg_VideoCapture_OpenReply()); | |
97 } | |
98 | |
99 void PepperVideoCaptureHost::OnStarted(media::VideoCapture* capture) { | |
100 if (SetStatus(PP_VIDEO_CAPTURE_STATUS_STARTED, false)) | |
101 SendStatus(); | |
102 } | |
103 | |
104 void PepperVideoCaptureHost::OnStopped(media::VideoCapture* capture) { | |
105 if (SetStatus(PP_VIDEO_CAPTURE_STATUS_STOPPED, false)) | |
106 SendStatus(); | |
107 } | |
108 | |
109 void PepperVideoCaptureHost::OnPaused(media::VideoCapture* capture) { | |
110 if (SetStatus(PP_VIDEO_CAPTURE_STATUS_PAUSED, false)) | |
111 SendStatus(); | |
112 } | |
113 | |
114 void PepperVideoCaptureHost::OnError(media::VideoCapture* capture, | |
115 int error_code) { | |
116 // Today, the media layer only sends "1" as an error. | |
117 DCHECK(error_code == 1); | |
118 // It either comes because some error was detected while starting (e.g. 2 | |
119 // conflicting "master" resolution), or because the browser failed to start | |
120 // the capture. | |
121 SetStatus(PP_VIDEO_CAPTURE_STATUS_STOPPED, true); | |
122 host()->SendUnsolicitedReply(pp_resource(), | |
123 PpapiPluginMsg_VideoCapture_OnError(PP_ERROR_FAILED)); | |
124 } | |
125 | |
126 void PepperVideoCaptureHost::OnRemoved(media::VideoCapture* capture) { | |
127 } | |
128 | |
129 void PepperVideoCaptureHost::OnBufferReady( | |
130 media::VideoCapture* capture, | |
131 scoped_refptr<media::VideoCapture::VideoFrameBuffer> buffer) { | |
132 DCHECK(buffer.get()); | |
133 for (uint32_t i = 0; i < buffers_.size(); ++i) { | |
134 if (!buffers_[i].in_use) { | |
135 // TODO(ihf): Switch to a size calculation based on stride. | |
136 // Stride is filled out now but not more meaningful than size | |
137 // until wjia unifies VideoFrameBuffer and media::VideoFrame. | |
138 size_t size = std::min(static_cast<size_t>(buffers_[i].buffer->size()), | |
139 buffer->buffer_size); | |
140 memcpy(buffers_[i].data, buffer->memory_pointer, size); | |
141 buffers_[i].in_use = true; | |
142 platform_video_capture_->FeedBuffer(buffer); | |
143 host()->SendUnsolicitedReply(pp_resource(), | |
144 PpapiPluginMsg_VideoCapture_OnBufferReady(i)); | |
145 return; | |
146 } | |
147 } | |
148 | |
149 // No free slot, just discard the frame and tell the media layer it can | |
150 // re-use the buffer. | |
151 platform_video_capture_->FeedBuffer(buffer); | |
152 } | |
153 | |
154 void PepperVideoCaptureHost::OnDeviceInfoReceived( | |
155 media::VideoCapture* capture, | |
156 const media::VideoCaptureParams& device_info) { | |
157 PP_VideoCaptureDeviceInfo_Dev info = { | |
158 static_cast<uint32_t>(device_info.width), | |
159 static_cast<uint32_t>(device_info.height), | |
160 static_cast<uint32_t>(device_info.frame_per_second) | |
161 }; | |
162 ReleaseBuffers(); | |
163 | |
164 // YUV 4:2:0 | |
165 int uv_width = info.width / 2; | |
166 int uv_height = info.height / 2; | |
167 size_t size = info.width * info.height + 2 * uv_width * uv_height; | |
168 | |
169 ppapi::proxy::ResourceMessageReplyParams params(pp_resource(), 0); | |
170 | |
171 // Allocate buffers. We keep a reference to them, that is released in | |
172 // ReleaseBuffers. In the mean time, we prepare the resource and handle here | |
173 // for sending below. | |
174 std::vector<HostResource> buffer_host_resources; | |
175 buffers_.reserve(buffer_count_hint_); | |
176 ppapi::proxy::HostDispatcher* dispatcher = | |
177 ppapi::proxy::HostDispatcher::GetForInstance(pp_instance()); | |
178 for (size_t i = 0; i < buffer_count_hint_; ++i) { | |
179 PP_Resource res = PPB_Buffer_Impl::Create(pp_instance(), size); | |
180 if (!res) | |
181 break; | |
182 | |
183 EnterResourceNoLock<PPB_Buffer_API> enter(res, true); | |
184 DCHECK(enter.succeeded()); | |
185 | |
186 BufferInfo buf; | |
187 buf.buffer = static_cast<PPB_Buffer_Impl*>(enter.object()); | |
188 buf.data = buf.buffer->Map(); | |
189 if (!buf.data) { | |
190 PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource(res); | |
191 break; | |
192 } | |
193 buffers_.push_back(buf); | |
194 | |
195 // Add to HostResource array to be sent. | |
196 { | |
197 HostResource host_resource; | |
yzshen1
2012/11/06 21:56:23
You have to add one more ref for the plugin side.
victorhsieh
2012/11/08 09:20:18
Done. But how does that work when running out of
yzshen1
2012/11/10 01:14:40
What I said above is for out-of-process. And pleas
victorhsieh
2012/11/13 03:10:53
Done. I was thinking in different way and got con
yzshen1
2012/11/14 06:54:16
HostGlobals SGTM.
| |
198 host_resource.SetHostResource(pp_instance(), res); | |
199 buffer_host_resources.push_back(host_resource); | |
200 } | |
201 | |
202 // Add the serialized shared memory handle to params. FileDescriptor is | |
203 // treated in special case. | |
204 { | |
205 EnterResourceNoLock<PPB_BufferTrusted_API> enter(res, true); | |
206 DCHECK(enter.succeeded()); | |
207 int handle; | |
208 int32_t result = enter.object()->GetSharedMemory(&handle); | |
209 DCHECK(result == PP_OK); | |
210 // TODO(piman/brettw): Change trusted interface to return a PP_FileHandle, | |
211 // those casts are ugly. | |
212 base::PlatformFile platform_file = | |
213 #if defined(OS_WIN) | |
214 reinterpret_cast<HANDLE>(static_cast<intptr_t>(handle)); | |
215 #elif defined(OS_POSIX) | |
216 handle; | |
217 #else | |
218 #error Not implemented. | |
219 #endif | |
220 params.AppendHandle( | |
221 ppapi::proxy::SerializedHandle( | |
222 dispatcher->ShareHandleWithRemote(platform_file, false), | |
223 size)); | |
224 } | |
225 } | |
226 | |
227 if (buffers_.empty()) { | |
228 // We couldn't allocate/map buffers at all. Send an error and stop the | |
229 // capture. | |
230 SetStatus(PP_VIDEO_CAPTURE_STATUS_STOPPING, true); | |
231 platform_video_capture_->StopCapture(this); | |
232 OnError(capture, PP_ERROR_NOMEMORY); | |
233 return; | |
234 } | |
235 | |
236 host()->Send(new PpapiPluginMsg_ResourceReply( | |
237 params, PpapiPluginMsg_VideoCapture_OnDeviceInfo( | |
238 info, buffer_host_resources, size))); | |
239 } | |
240 | |
241 PluginInstance* PepperVideoCaptureHost::GetPluginInstance() const { | |
242 return ResourceHelper::PPInstanceToPluginInstance(pp_instance()); | |
yzshen1
2012/11/06 21:56:23
I thought this wouldn't link. Maybe you are using
victorhsieh
2012/11/08 09:20:18
Actually I just learned about component build this
yzshen1
2012/11/10 01:14:40
It has been checked in.
victorhsieh
2012/11/13 03:10:53
Done.
| |
243 } | |
244 | |
245 int32_t PepperVideoCaptureHost::OnEnumerateDevices( | |
246 ppapi::host::HostMessageContext* context) { | |
247 PluginInstance* instance = GetPluginInstance(); | |
248 if (!instance) | |
249 return PP_ERROR_FAILED; | |
250 | |
251 enum_reply_context_ = context->MakeReplyMessageContext(); | |
yzshen1
2012/11/06 21:56:23
If we receive a second enumerate devices request b
victorhsieh
2012/11/08 09:20:18
I don't see any risk here if we don't check? enum
yzshen1
2012/11/10 01:14:40
Okay.
On 2012/11/08 09:20:18, Victor Hsieh wrote:
| |
252 instance->delegate()->EnumerateDevices( | |
253 PP_DEVICETYPE_DEV_VIDEOCAPTURE, | |
254 base::Bind(&PepperVideoCaptureHost::EnumerateDevicesCallbackFunc, | |
255 AsWeakPtr())); | |
256 return PP_OK_COMPLETIONPENDING; | |
257 } | |
258 | |
259 int32_t PepperVideoCaptureHost::OnOpen( | |
260 ppapi::host::HostMessageContext* context, | |
261 const std::string& device_id, | |
262 const PP_VideoCaptureDeviceInfo_Dev& requested_info, | |
263 uint32_t buffer_count) { | |
264 if (platform_video_capture_.get()) | |
265 return PP_ERROR_FAILED; | |
266 | |
267 PluginInstance* instance = GetPluginInstance(); | |
268 if (!instance) | |
269 return PP_ERROR_FAILED; | |
270 | |
271 SetRequestedInfo(requested_info, buffer_count); | |
272 | |
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; | |
yzshen1
2012/11/06 21:56:23
You don't need this line.
victorhsieh
2012/11/08 09:20:18
Done.
| |
285 } | |
286 | |
287 return PP_OK_COMPLETIONPENDING; | |
288 } | |
289 | |
290 int32_t PepperVideoCaptureHost::OnStartCapture( | |
291 ppapi::host::HostMessageContext* context) { | |
292 if (!SetStatus(PP_VIDEO_CAPTURE_STATUS_STARTING, false)) | |
293 return PP_ERROR_FAILED; | |
294 | |
295 DCHECK(platform_video_capture_.get()); | |
yzshen1
2012/11/06 21:56:23
If we want this code to handle unexpected requests
victorhsieh
2012/11/08 09:20:18
Done.
| |
296 DCHECK(buffers_.empty()); | |
297 | |
298 // It's safe to call this regardless it's capturing or not, because | |
299 // PepperPlatformVideoCaptureImpl maintains the state. | |
300 platform_video_capture_->StartCapture(this, capability_); | |
301 return PP_OK; | |
302 } | |
303 | |
304 int32_t PepperVideoCaptureHost::OnReuseBuffer( | |
305 ppapi::host::HostMessageContext* context, | |
306 uint32_t buffer) { | |
307 if (buffer >= buffers_.size() || !buffers_[buffer].in_use) | |
308 return PP_ERROR_BADARGUMENT; | |
309 buffers_[buffer].in_use = false; | |
310 return PP_OK; | |
311 } | |
312 | |
313 int32_t PepperVideoCaptureHost::OnStopCapture( | |
314 ppapi::host::HostMessageContext* context) { | |
315 return StopCapture(); | |
316 } | |
317 | |
318 int32_t PepperVideoCaptureHost::OnClose( | |
319 ppapi::host::HostMessageContext* context) { | |
320 return Close(); | |
321 } | |
322 | |
323 int32_t PepperVideoCaptureHost::StopCapture() { | |
324 if (!SetStatus(PP_VIDEO_CAPTURE_STATUS_STOPPING, false)) | |
325 return PP_ERROR_FAILED; | |
326 | |
327 DCHECK(platform_video_capture_.get()); | |
328 | |
329 ReleaseBuffers(); | |
330 // It's safe to call this regardless it's capturing or not, because | |
331 // PepperPlatformVideoCaptureImpl maintains the state. | |
332 platform_video_capture_->StopCapture(this); | |
333 return PP_OK; | |
334 } | |
335 | |
336 int32_t PepperVideoCaptureHost::Close() { | |
337 if (!platform_video_capture_.get()) | |
338 return PP_OK; | |
339 | |
340 StopCapture(); | |
341 DCHECK(buffers_.empty()); | |
342 DetachPlatformVideoCapture(); | |
343 return PP_OK; | |
344 } | |
345 | |
346 void PepperVideoCaptureHost::ReleaseBuffers() { | |
347 ::ppapi::ResourceTracker* tracker = PpapiGlobals::Get()->GetResourceTracker(); | |
348 for (size_t i = 0; i < buffers_.size(); ++i) { | |
349 buffers_[i].buffer->Unmap(); | |
350 tracker->ReleaseResource(buffers_[i].buffer->pp_resource()); | |
351 } | |
352 buffers_.clear(); | |
353 } | |
354 | |
355 void PepperVideoCaptureHost::SendStatus() { | |
356 host()->SendUnsolicitedReply(pp_resource(), | |
357 PpapiPluginMsg_VideoCapture_OnStatus(status_)); | |
358 } | |
359 | |
360 void PepperVideoCaptureHost::SetRequestedInfo( | |
361 const PP_VideoCaptureDeviceInfo_Dev& device_info, | |
362 uint32_t buffer_count) { | |
363 // Clamp the buffer count to between 1 and |kMaxBuffers|. | |
364 buffer_count_hint_ = std::min(std::max(buffer_count, 1U), kMaxBuffers); | |
365 | |
366 capability_.width = device_info.width; | |
367 capability_.height = device_info.height; | |
368 capability_.frame_rate = device_info.frames_per_second; | |
369 capability_.expected_capture_delay = 0; // Ignored. | |
370 capability_.color = media::VideoCaptureCapability::kI420; | |
371 capability_.interlaced = false; // Ignored. | |
372 } | |
373 | |
374 void PepperVideoCaptureHost::DetachPlatformVideoCapture() { | |
375 if (platform_video_capture_.get()) { | |
376 platform_video_capture_->DetachEventHandler(); | |
377 platform_video_capture_ = NULL; | |
378 } | |
379 } | |
380 | |
381 void PepperVideoCaptureHost::EnumerateDevicesCallbackFunc( | |
382 int request_id, | |
383 bool succeeded, | |
384 const std::vector<ppapi::DeviceRefData>& devices) { | |
385 PluginInstance* instance = GetPluginInstance(); | |
386 if (instance) | |
387 instance->delegate()->StopEnumerateDevices(request_id); | |
388 | |
389 if (succeeded) { | |
390 enum_reply_context_.params.set_result(PP_OK); | |
391 host()->SendReply(enum_reply_context_, | |
392 PpapiPluginMsg_VideoCapture_EnumerateDevicesReply( | |
393 devices)); | |
394 } else { | |
395 enum_reply_context_.params.set_result(PP_ERROR_FAILED); | |
396 host()->SendReply(enum_reply_context_, | |
397 PpapiPluginMsg_VideoCapture_EnumerateDevicesReply( | |
398 std::vector<DeviceRefData>())); | |
399 } | |
400 } | |
401 | |
402 bool PepperVideoCaptureHost::SetStatus(PP_VideoCaptureStatus_Dev status, | |
403 bool forced) { | |
404 if (!forced) { | |
405 switch (status) { | |
406 case PP_VIDEO_CAPTURE_STATUS_STOPPED: | |
407 if (status_ != PP_VIDEO_CAPTURE_STATUS_STOPPING) | |
408 return false; | |
409 break; | |
410 case PP_VIDEO_CAPTURE_STATUS_STARTING: | |
411 if (status_ != PP_VIDEO_CAPTURE_STATUS_STOPPED) | |
412 return false; | |
413 break; | |
414 case PP_VIDEO_CAPTURE_STATUS_STARTED: | |
415 switch (status_) { | |
416 case PP_VIDEO_CAPTURE_STATUS_STARTING: | |
417 case PP_VIDEO_CAPTURE_STATUS_PAUSED: | |
418 break; | |
419 default: | |
420 return false; | |
421 } | |
422 break; | |
423 case PP_VIDEO_CAPTURE_STATUS_PAUSED: | |
424 switch (status_) { | |
425 case PP_VIDEO_CAPTURE_STATUS_STARTING: | |
426 case PP_VIDEO_CAPTURE_STATUS_STARTED: | |
427 break; | |
428 default: | |
429 return false; | |
430 } | |
431 break; | |
432 case PP_VIDEO_CAPTURE_STATUS_STOPPING: | |
433 switch (status_) { | |
434 case PP_VIDEO_CAPTURE_STATUS_STARTING: | |
435 case PP_VIDEO_CAPTURE_STATUS_STARTED: | |
436 case PP_VIDEO_CAPTURE_STATUS_PAUSED: | |
437 break; | |
438 default: | |
439 return false; | |
440 } | |
441 break; | |
442 } | |
443 } | |
444 | |
445 status_ = status; | |
446 return true; | |
447 } | |
448 | |
449 PepperVideoCaptureHost::BufferInfo::BufferInfo() | |
450 : in_use(false), | |
451 data(NULL), | |
452 buffer() { | |
453 } | |
454 | |
455 PepperVideoCaptureHost::BufferInfo::~BufferInfo() { | |
456 } | |
457 | |
458 } // namespace content | |
OLD | NEW |