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
|
// 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 "ppapi/cpp/dev/video_capture_dev.h"
#include "ppapi/c/dev/ppb_video_capture_dev.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/cpp/completion_callback.h"
#include "ppapi/cpp/dev/device_ref_dev.h"
#include "ppapi/cpp/dev/resource_array_dev.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/module_impl.h"
namespace pp {
namespace {
template <> const char* interface_name<PPB_VideoCapture_Dev>() {
return PPB_VIDEOCAPTURE_DEV_INTERFACE;
}
} // namespace
struct VideoCapture_Dev::EnumerateDevicesState {
EnumerateDevicesState(std::vector<DeviceRef_Dev>* in_devices,
const CompletionCallback& in_callback,
VideoCapture_Dev* in_video_capture)
: devices_resource(0),
devices(in_devices),
callback(in_callback),
video_capture(in_video_capture) {
}
PP_Resource devices_resource;
std::vector<DeviceRef_Dev>* devices;
CompletionCallback callback;
VideoCapture_Dev* video_capture;
};
VideoCapture_Dev::VideoCapture_Dev(const Instance& instance)
: enum_state_(NULL) {
if (!has_interface<PPB_VideoCapture_Dev>())
return;
PassRefFromConstructor(get_interface<PPB_VideoCapture_Dev>()->Create(
instance.pp_instance()));
}
VideoCapture_Dev::VideoCapture_Dev(PP_Resource resource)
: Resource(resource),
enum_state_(NULL) {
}
VideoCapture_Dev::VideoCapture_Dev(const VideoCapture_Dev& other)
: Resource(other),
enum_state_(NULL) {
}
VideoCapture_Dev::~VideoCapture_Dev() {
AbortEnumerateDevices();
}
VideoCapture_Dev& VideoCapture_Dev::operator=(
const VideoCapture_Dev& other) {
AbortEnumerateDevices();
Resource::operator=(other);
return *this;
}
// static
bool VideoCapture_Dev::IsAvailable() {
return has_interface<PPB_VideoCapture_Dev>();
}
int32_t VideoCapture_Dev::EnumerateDevices(std::vector<DeviceRef_Dev>* devices,
const CompletionCallback& callback) {
if (!has_interface<PPB_VideoCapture_Dev>())
return callback.MayForce(PP_ERROR_NOINTERFACE);
if (!devices)
return callback.MayForce(PP_ERROR_BADARGUMENT);
if (!callback.pp_completion_callback().func)
return callback.MayForce(PP_ERROR_BLOCKS_MAIN_THREAD);
if (enum_state_)
return callback.MayForce(PP_ERROR_INPROGRESS);
// It will be deleted in OnEnumerateDevicesComplete().
enum_state_ = new EnumerateDevicesState(devices, callback, this);
return get_interface<PPB_VideoCapture_Dev>()->EnumerateDevices(
pp_resource(), &enum_state_->devices_resource,
PP_MakeCompletionCallback(&VideoCapture_Dev::OnEnumerateDevicesComplete,
enum_state_));
}
int32_t VideoCapture_Dev::Open(
const DeviceRef_Dev& device_ref,
const PP_VideoCaptureDeviceInfo_Dev& requested_info,
uint32_t buffer_count,
const CompletionCallback& callback) {
if (!has_interface<PPB_VideoCapture_Dev>())
return callback.MayForce(PP_ERROR_NOINTERFACE);
return get_interface<PPB_VideoCapture_Dev>()->Open(
pp_resource(), device_ref.pp_resource(), &requested_info, buffer_count,
callback.pp_completion_callback());
}
int32_t VideoCapture_Dev::StartCapture() {
if (!has_interface<PPB_VideoCapture_Dev>())
return PP_ERROR_NOINTERFACE;
return get_interface<PPB_VideoCapture_Dev>()->StartCapture(pp_resource());
}
int32_t VideoCapture_Dev::ReuseBuffer(uint32_t buffer) {
if (!has_interface<PPB_VideoCapture_Dev>())
return PP_ERROR_NOINTERFACE;
return get_interface<PPB_VideoCapture_Dev>()->ReuseBuffer(
pp_resource(), buffer);
}
int32_t VideoCapture_Dev::StopCapture() {
if (!has_interface<PPB_VideoCapture_Dev>())
return PP_ERROR_NOINTERFACE;
return get_interface<PPB_VideoCapture_Dev>()->StopCapture(pp_resource());
}
void VideoCapture_Dev::Close() {
if (has_interface<PPB_VideoCapture_Dev>())
get_interface<PPB_VideoCapture_Dev>()->Close(pp_resource());
}
void VideoCapture_Dev::AbortEnumerateDevices() {
if (enum_state_) {
enum_state_->devices = NULL;
Module::Get()->core()->CallOnMainThread(0, enum_state_->callback,
PP_ERROR_ABORTED);
enum_state_->video_capture = NULL;
enum_state_ = NULL;
}
}
// static
void VideoCapture_Dev::OnEnumerateDevicesComplete(void* user_data,
int32_t result) {
EnumerateDevicesState* enum_state =
static_cast<EnumerateDevicesState*>(user_data);
bool need_to_callback = !!enum_state->video_capture;
if (result == PP_OK) {
// It will take care of releasing the reference.
ResourceArray_Dev resources(ResourceArray_Dev::PassRef(),
enum_state->devices_resource);
if (need_to_callback) {
enum_state->devices->clear();
for (uint32_t index = 0; index < resources.size(); ++index) {
DeviceRef_Dev device(resources[index]);
enum_state->devices->push_back(device);
}
}
}
if (need_to_callback) {
enum_state->video_capture->enum_state_ = NULL;
enum_state->callback.Run(result);
}
delete enum_state;
}
} // namespace pp
|