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