Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(213)

Side by Side Diff: ppapi/cpp/dev/video_capture_dev.cc

Issue 9965080: Change the cpp wrappers of audio input/video capture to use CompletionCallbackWithOutput. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: More changes in response to Brett's comments. Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ppapi/cpp/dev/video_capture_dev.h ('k') | ppapi/cpp/output_traits.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ppapi/cpp/dev/video_capture_dev.h" 5 #include "ppapi/cpp/dev/video_capture_dev.h"
6 6
7 #include "ppapi/c/dev/ppb_video_capture_dev.h" 7 #include "ppapi/c/dev/ppb_video_capture_dev.h"
8 #include "ppapi/c/pp_errors.h" 8 #include "ppapi/c/pp_errors.h"
9 #include "ppapi/cpp/completion_callback.h"
10 #include "ppapi/cpp/dev/device_ref_dev.h"
11 #include "ppapi/cpp/dev/resource_array_dev.h" 9 #include "ppapi/cpp/dev/resource_array_dev.h"
12 #include "ppapi/cpp/instance_handle.h" 10 #include "ppapi/cpp/instance_handle.h"
13 #include "ppapi/cpp/module.h"
14 #include "ppapi/cpp/module_impl.h" 11 #include "ppapi/cpp/module_impl.h"
15 12
16 namespace pp { 13 namespace pp {
17 14
18 namespace { 15 namespace {
19 16
20 template <> const char* interface_name<PPB_VideoCapture_Dev_0_2>() { 17 template <> const char* interface_name<PPB_VideoCapture_Dev_0_2>() {
21 return PPB_VIDEOCAPTURE_DEV_INTERFACE_0_2; 18 return PPB_VIDEOCAPTURE_DEV_INTERFACE_0_2;
22 } 19 }
23 20
24 template <> const char* interface_name<PPB_VideoCapture_Dev_0_1>() { 21 template <> const char* interface_name<PPB_VideoCapture_Dev_0_1>() {
25 return PPB_VIDEOCAPTURE_DEV_INTERFACE_0_1; 22 return PPB_VIDEOCAPTURE_DEV_INTERFACE_0_1;
26 } 23 }
27 24
28 } // namespace 25 } // namespace
29 26
30 struct VideoCapture_Dev::EnumerateDevicesState {
31 EnumerateDevicesState(std::vector<DeviceRef_Dev>* in_devices,
32 const CompletionCallback& in_callback,
33 VideoCapture_Dev* in_video_capture)
34 : devices_resource(0),
35 devices(in_devices),
36 callback(in_callback),
37 video_capture(in_video_capture) {
38 }
39
40 PP_Resource devices_resource;
41 std::vector<DeviceRef_Dev>* devices;
42 CompletionCallback callback;
43 VideoCapture_Dev* video_capture;
44 };
45
46 VideoCapture_Dev::VideoCapture_Dev(const InstanceHandle& instance) 27 VideoCapture_Dev::VideoCapture_Dev(const InstanceHandle& instance)
47 : enum_state_(NULL), 28 : requested_info_(),
48 requested_info_(),
49 buffer_count_(0) { 29 buffer_count_(0) {
50 if (has_interface<PPB_VideoCapture_Dev_0_2>()) { 30 if (has_interface<PPB_VideoCapture_Dev_0_2>()) {
51 PassRefFromConstructor(get_interface<PPB_VideoCapture_Dev_0_2>()->Create( 31 PassRefFromConstructor(get_interface<PPB_VideoCapture_Dev_0_2>()->Create(
52 instance.pp_instance())); 32 instance.pp_instance()));
53 } else if (has_interface<PPB_VideoCapture_Dev_0_1>()) { 33 } else if (has_interface<PPB_VideoCapture_Dev_0_1>()) {
54 PassRefFromConstructor(get_interface<PPB_VideoCapture_Dev_0_1>()->Create( 34 PassRefFromConstructor(get_interface<PPB_VideoCapture_Dev_0_1>()->Create(
55 instance.pp_instance())); 35 instance.pp_instance()));
56 } 36 }
57 } 37 }
58 38
59 VideoCapture_Dev::VideoCapture_Dev(PP_Resource resource) 39 VideoCapture_Dev::VideoCapture_Dev(PP_Resource resource)
60 : Resource(resource), 40 : Resource(resource),
61 enum_state_(NULL),
62 requested_info_(), 41 requested_info_(),
63 buffer_count_(0) { 42 buffer_count_(0) {
64 } 43 }
65 44
66 VideoCapture_Dev::VideoCapture_Dev(const VideoCapture_Dev& other)
67 : Resource(other),
68 enum_state_(NULL),
69 requested_info_(other.requested_info_),
70 buffer_count_(other.buffer_count_) {
71 }
72
73 VideoCapture_Dev::~VideoCapture_Dev() { 45 VideoCapture_Dev::~VideoCapture_Dev() {
74 AbortEnumerateDevices();
75 }
76
77 VideoCapture_Dev& VideoCapture_Dev::operator=(
78 const VideoCapture_Dev& other) {
79 AbortEnumerateDevices();
80
81 Resource::operator=(other);
82 requested_info_ = other.requested_info_;
83 buffer_count_ = other.buffer_count_;
84 return *this;
85 } 46 }
86 47
87 // static 48 // static
88 bool VideoCapture_Dev::IsAvailable() { 49 bool VideoCapture_Dev::IsAvailable() {
89 return has_interface<PPB_VideoCapture_Dev_0_2>() || 50 return has_interface<PPB_VideoCapture_Dev_0_2>() ||
90 has_interface<PPB_VideoCapture_Dev_0_1>(); 51 has_interface<PPB_VideoCapture_Dev_0_1>();
91 } 52 }
92 53
93 int32_t VideoCapture_Dev::EnumerateDevices(std::vector<DeviceRef_Dev>* devices, 54 int32_t VideoCapture_Dev::EnumerateDevices(
94 const CompletionCallback& callback) { 55 const CompletionCallbackWithOutput<std::vector<DeviceRef_Dev> >& callback) {
95 if (!has_interface<PPB_VideoCapture_Dev_0_2>()) 56 if (!has_interface<PPB_VideoCapture_Dev_0_2>())
96 return callback.MayForce(PP_ERROR_NOINTERFACE); 57 return callback.MayForce(PP_ERROR_NOINTERFACE);
97 if (!devices)
98 return callback.MayForce(PP_ERROR_BADARGUMENT);
99 if (!callback.pp_completion_callback().func) 58 if (!callback.pp_completion_callback().func)
100 return callback.MayForce(PP_ERROR_BLOCKS_MAIN_THREAD); 59 return callback.MayForce(PP_ERROR_BLOCKS_MAIN_THREAD);
101 if (enum_state_)
102 return callback.MayForce(PP_ERROR_INPROGRESS);
103 60
104 // It will be deleted in OnEnumerateDevicesComplete(). 61 // ArrayOutputCallbackConverter is responsible to delete it.
105 enum_state_ = new EnumerateDevicesState(devices, callback, this); 62 ResourceArray_Dev::ArrayOutputCallbackData* data =
63 new ResourceArray_Dev::ArrayOutputCallbackData(
64 callback.output(), callback.pp_completion_callback());
106 return get_interface<PPB_VideoCapture_Dev_0_2>()->EnumerateDevices( 65 return get_interface<PPB_VideoCapture_Dev_0_2>()->EnumerateDevices(
107 pp_resource(), &enum_state_->devices_resource, 66 pp_resource(), &data->resource_array_output,
108 PP_MakeCompletionCallback(&VideoCapture_Dev::OnEnumerateDevicesComplete, 67 PP_MakeCompletionCallback(
109 enum_state_)); 68 &ResourceArray_Dev::ArrayOutputCallbackConverter, data));
110 } 69 }
111 70
112 int32_t VideoCapture_Dev::Open( 71 int32_t VideoCapture_Dev::Open(
113 const DeviceRef_Dev& device_ref, 72 const DeviceRef_Dev& device_ref,
114 const PP_VideoCaptureDeviceInfo_Dev& requested_info, 73 const PP_VideoCaptureDeviceInfo_Dev& requested_info,
115 uint32_t buffer_count, 74 uint32_t buffer_count,
116 const CompletionCallback& callback) { 75 const CompletionCallback& callback) {
117 if (has_interface<PPB_VideoCapture_Dev_0_2>()) { 76 if (has_interface<PPB_VideoCapture_Dev_0_2>()) {
118 return get_interface<PPB_VideoCapture_Dev_0_2>()->Open( 77 return get_interface<PPB_VideoCapture_Dev_0_2>()->Open(
119 pp_resource(), device_ref.pp_resource(), &requested_info, buffer_count, 78 pp_resource(), device_ref.pp_resource(), &requested_info, buffer_count,
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 } 131 }
173 132
174 return PP_ERROR_NOINTERFACE; 133 return PP_ERROR_NOINTERFACE;
175 } 134 }
176 135
177 void VideoCapture_Dev::Close() { 136 void VideoCapture_Dev::Close() {
178 if (has_interface<PPB_VideoCapture_Dev_0_2>()) 137 if (has_interface<PPB_VideoCapture_Dev_0_2>())
179 get_interface<PPB_VideoCapture_Dev_0_2>()->Close(pp_resource()); 138 get_interface<PPB_VideoCapture_Dev_0_2>()->Close(pp_resource());
180 } 139 }
181 140
182 void VideoCapture_Dev::AbortEnumerateDevices() {
183 if (enum_state_) {
184 enum_state_->devices = NULL;
185 Module::Get()->core()->CallOnMainThread(0, enum_state_->callback,
186 PP_ERROR_ABORTED);
187 enum_state_->video_capture = NULL;
188 enum_state_ = NULL;
189 }
190 }
191
192 // static
193 void VideoCapture_Dev::OnEnumerateDevicesComplete(void* user_data,
194 int32_t result) {
195 EnumerateDevicesState* enum_state =
196 static_cast<EnumerateDevicesState*>(user_data);
197
198 bool need_to_callback = !!enum_state->video_capture;
199
200 if (result == PP_OK) {
201 // It will take care of releasing the reference.
202 ResourceArray_Dev resources(pp::PASS_REF, enum_state->devices_resource);
203
204 if (need_to_callback) {
205 enum_state->devices->clear();
206 for (uint32_t index = 0; index < resources.size(); ++index) {
207 DeviceRef_Dev device(resources[index]);
208 enum_state->devices->push_back(device);
209 }
210 }
211 }
212
213 if (need_to_callback) {
214 enum_state->video_capture->enum_state_ = NULL;
215 enum_state->callback.Run(result);
216 }
217
218 delete enum_state;
219 }
220
221 } // namespace pp 141 } // namespace pp
OLDNEW
« no previous file with comments | « ppapi/cpp/dev/video_capture_dev.h ('k') | ppapi/cpp/output_traits.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698