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

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

Issue 9234064: Implement device enumeration for PPB_VideoCapture_Dev. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 10 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/examples/video_capture/video_capture.cc » ('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) 2011 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/instance.h" 12 #include "ppapi/cpp/instance.h"
10 #include "ppapi/cpp/module.h" 13 #include "ppapi/cpp/module.h"
11 #include "ppapi/cpp/module_impl.h" 14 #include "ppapi/cpp/module_impl.h"
12 15
13 namespace pp { 16 namespace pp {
14 17
15 namespace { 18 namespace {
16 19
17 template <> const char* interface_name<PPB_VideoCapture_Dev>() { 20 template <> const char* interface_name<PPB_VideoCapture_Dev>() {
18 return PPB_VIDEOCAPTURE_DEV_INTERFACE; 21 return PPB_VIDEOCAPTURE_DEV_INTERFACE;
19 } 22 }
20 23
21 } // namespace 24 } // namespace
22 25
23 VideoCapture_Dev::VideoCapture_Dev(const Instance& instance) { 26 struct VideoCapture_Dev::EnumerateDevicesState {
27 EnumerateDevicesState(std::vector<DeviceRef_Dev>* in_devices,
28 const CompletionCallback& in_callback,
29 VideoCapture_Dev* in_video_capture)
30 : devices_resource(0),
31 devices(in_devices),
32 callback(in_callback),
33 video_capture(in_video_capture) {
34 }
35
36 PP_Resource devices_resource;
37 std::vector<DeviceRef_Dev>* devices;
38 CompletionCallback callback;
39 VideoCapture_Dev* video_capture;
40 };
41
42 VideoCapture_Dev::VideoCapture_Dev(const Instance& instance)
43 : enum_state_(NULL) {
24 if (!has_interface<PPB_VideoCapture_Dev>()) 44 if (!has_interface<PPB_VideoCapture_Dev>())
25 return; 45 return;
26 PassRefFromConstructor(get_interface<PPB_VideoCapture_Dev>()->Create( 46 PassRefFromConstructor(get_interface<PPB_VideoCapture_Dev>()->Create(
27 instance.pp_instance())); 47 instance.pp_instance()));
28 } 48 }
29 49
30 VideoCapture_Dev::VideoCapture_Dev(PP_Resource resource) : Resource(resource) { 50 VideoCapture_Dev::VideoCapture_Dev(PP_Resource resource)
51 : Resource(resource),
52 enum_state_(NULL) {
31 } 53 }
32 54
33 VideoCapture_Dev::VideoCapture_Dev(const VideoCapture_Dev& other) 55 VideoCapture_Dev::VideoCapture_Dev(const VideoCapture_Dev& other)
34 : Resource(other) { 56 : Resource(other),
57 enum_state_(NULL) {
58 }
59
60 VideoCapture_Dev::~VideoCapture_Dev() {
61 AbortEnumerateDevices();
62 }
63
64 VideoCapture_Dev& VideoCapture_Dev::operator=(
65 const VideoCapture_Dev& other) {
66 AbortEnumerateDevices();
67
68 Resource::operator=(other);
69 return *this;
35 } 70 }
36 71
37 // static 72 // static
38 bool VideoCapture_Dev::IsAvailable() { 73 bool VideoCapture_Dev::IsAvailable() {
39 return has_interface<PPB_VideoCapture_Dev>(); 74 return has_interface<PPB_VideoCapture_Dev>();
40 } 75 }
41 76
42 int32_t VideoCapture_Dev::StartCapture( 77 int32_t VideoCapture_Dev::EnumerateDevices(std::vector<DeviceRef_Dev>* devices,
78 const CompletionCallback& callback) {
79 if (!has_interface<PPB_VideoCapture_Dev>())
80 return callback.MayForce(PP_ERROR_NOINTERFACE);
81 if (!devices)
82 return callback.MayForce(PP_ERROR_BADARGUMENT);
83 if (!callback.pp_completion_callback().func)
84 return callback.MayForce(PP_ERROR_BLOCKS_MAIN_THREAD);
85 if (enum_state_)
86 return callback.MayForce(PP_ERROR_INPROGRESS);
87
88 // It will be deleted in OnEnumerateDevicesComplete().
89 enum_state_ = new EnumerateDevicesState(devices, callback, this);
90 return get_interface<PPB_VideoCapture_Dev>()->EnumerateDevices(
91 pp_resource(), &enum_state_->devices_resource,
92 PP_MakeCompletionCallback(&VideoCapture_Dev::OnEnumerateDevicesComplete,
93 enum_state_));
94 }
95
96 int32_t VideoCapture_Dev::Open(
97 const DeviceRef_Dev& device_ref,
43 const PP_VideoCaptureDeviceInfo_Dev& requested_info, 98 const PP_VideoCaptureDeviceInfo_Dev& requested_info,
44 uint32_t buffer_count) { 99 uint32_t buffer_count,
100 const CompletionCallback& callback) {
45 if (!has_interface<PPB_VideoCapture_Dev>()) 101 if (!has_interface<PPB_VideoCapture_Dev>())
46 return PP_ERROR_FAILED; 102 return callback.MayForce(PP_ERROR_NOINTERFACE);
47 return get_interface<PPB_VideoCapture_Dev>()->StartCapture( 103 return get_interface<PPB_VideoCapture_Dev>()->Open(
48 pp_resource(), &requested_info, buffer_count); 104 pp_resource(), device_ref.pp_resource(), &requested_info, buffer_count,
105 callback.pp_completion_callback());
106 }
107
108 int32_t VideoCapture_Dev::StartCapture() {
109 if (!has_interface<PPB_VideoCapture_Dev>())
110 return PP_ERROR_NOINTERFACE;
111 return get_interface<PPB_VideoCapture_Dev>()->StartCapture(pp_resource());
49 } 112 }
50 113
51 int32_t VideoCapture_Dev::ReuseBuffer(uint32_t buffer) { 114 int32_t VideoCapture_Dev::ReuseBuffer(uint32_t buffer) {
52 if (!has_interface<PPB_VideoCapture_Dev>()) 115 if (!has_interface<PPB_VideoCapture_Dev>())
53 return PP_ERROR_FAILED; 116 return PP_ERROR_NOINTERFACE;
54 return get_interface<PPB_VideoCapture_Dev>()->ReuseBuffer( 117 return get_interface<PPB_VideoCapture_Dev>()->ReuseBuffer(
55 pp_resource(), buffer); 118 pp_resource(), buffer);
56 } 119 }
57 120
58 int32_t VideoCapture_Dev::StopCapture(){ 121 int32_t VideoCapture_Dev::StopCapture() {
59 if (!has_interface<PPB_VideoCapture_Dev>()) 122 if (!has_interface<PPB_VideoCapture_Dev>())
60 return PP_ERROR_FAILED; 123 return PP_ERROR_NOINTERFACE;
61 return get_interface<PPB_VideoCapture_Dev>()->StopCapture(pp_resource()); 124 return get_interface<PPB_VideoCapture_Dev>()->StopCapture(pp_resource());
62 } 125 }
63 126
127 void VideoCapture_Dev::Close() {
128 if (has_interface<PPB_VideoCapture_Dev>())
129 get_interface<PPB_VideoCapture_Dev>()->Close(pp_resource());
130 }
131
132 void VideoCapture_Dev::AbortEnumerateDevices() {
133 if (enum_state_) {
134 enum_state_->devices = NULL;
135 Module::Get()->core()->CallOnMainThread(0, enum_state_->callback,
136 PP_ERROR_ABORTED);
137 enum_state_->video_capture = NULL;
138 enum_state_ = NULL;
139 }
140 }
141
142 // static
143 void VideoCapture_Dev::OnEnumerateDevicesComplete(void* user_data,
144 int32_t result) {
145 EnumerateDevicesState* enum_state =
146 static_cast<EnumerateDevicesState*>(user_data);
147
148 bool need_to_callback = !!enum_state->video_capture;
149
150 if (result == PP_OK) {
151 // It will take care of releasing the reference.
152 ResourceArray_Dev resources(ResourceArray_Dev::PassRef(),
153 enum_state->devices_resource);
154
155 if (need_to_callback) {
156 enum_state->devices->clear();
157 for (uint32_t index = 0; index < resources.size(); ++index) {
158 DeviceRef_Dev device(resources[index]);
159 enum_state->devices->push_back(device);
160 }
161 }
162 }
163
164 if (need_to_callback) {
165 enum_state->video_capture->enum_state_ = NULL;
166 enum_state->callback.Run(result);
167 }
168
169 delete enum_state;
170 }
171
64 } // namespace pp 172 } // namespace pp
OLDNEW
« no previous file with comments | « ppapi/cpp/dev/video_capture_dev.h ('k') | ppapi/examples/video_capture/video_capture.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698