| OLD | NEW |
| 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" |
| 11 #include "ppapi/cpp/logging.h" |
| 13 #include "ppapi/cpp/module.h" | 12 #include "ppapi/cpp/module.h" |
| 14 #include "ppapi/cpp/module_impl.h" | 13 #include "ppapi/cpp/module_impl.h" |
| 15 | 14 |
| 16 namespace pp { | 15 namespace pp { |
| 17 | 16 |
| 18 namespace { | 17 namespace { |
| 19 | 18 |
| 20 template <> const char* interface_name<PPB_VideoCapture_Dev_0_2>() { | 19 template <> const char* interface_name<PPB_VideoCapture_Dev_0_2>() { |
| 21 return PPB_VIDEOCAPTURE_DEV_INTERFACE_0_2; | 20 return PPB_VIDEOCAPTURE_DEV_INTERFACE_0_2; |
| 22 } | 21 } |
| 23 | 22 |
| 24 template <> const char* interface_name<PPB_VideoCapture_Dev_0_1>() { | 23 template <> const char* interface_name<PPB_VideoCapture_Dev_0_1>() { |
| 25 return PPB_VIDEOCAPTURE_DEV_INTERFACE_0_1; | 24 return PPB_VIDEOCAPTURE_DEV_INTERFACE_0_1; |
| 26 } | 25 } |
| 27 | 26 |
| 28 } // namespace | 27 } // namespace |
| 29 | 28 |
| 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) | 29 VideoCapture_Dev::VideoCapture_Dev(const InstanceHandle& instance) |
| 47 : enum_state_(NULL), | 30 : requested_info_(), |
| 48 requested_info_(), | |
| 49 buffer_count_(0) { | 31 buffer_count_(0) { |
| 50 if (has_interface<PPB_VideoCapture_Dev_0_2>()) { | 32 if (has_interface<PPB_VideoCapture_Dev_0_2>()) { |
| 51 PassRefFromConstructor(get_interface<PPB_VideoCapture_Dev_0_2>()->Create( | 33 PassRefFromConstructor(get_interface<PPB_VideoCapture_Dev_0_2>()->Create( |
| 52 instance.pp_instance())); | 34 instance.pp_instance())); |
| 53 } else if (has_interface<PPB_VideoCapture_Dev_0_1>()) { | 35 } else if (has_interface<PPB_VideoCapture_Dev_0_1>()) { |
| 54 PassRefFromConstructor(get_interface<PPB_VideoCapture_Dev_0_1>()->Create( | 36 PassRefFromConstructor(get_interface<PPB_VideoCapture_Dev_0_1>()->Create( |
| 55 instance.pp_instance())); | 37 instance.pp_instance())); |
| 56 } | 38 } |
| 57 } | 39 } |
| 58 | 40 |
| 59 VideoCapture_Dev::VideoCapture_Dev(PP_Resource resource) | 41 VideoCapture_Dev::VideoCapture_Dev(PP_Resource resource) |
| 60 : Resource(resource), | 42 : Resource(resource), |
| 61 enum_state_(NULL), | |
| 62 requested_info_(), | 43 requested_info_(), |
| 63 buffer_count_(0) { | 44 buffer_count_(0) { |
| 64 } | 45 } |
| 65 | 46 |
| 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() { | 47 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 } | 48 } |
| 86 | 49 |
| 87 // static | 50 // static |
| 88 bool VideoCapture_Dev::IsAvailable() { | 51 bool VideoCapture_Dev::IsAvailable() { |
| 89 return has_interface<PPB_VideoCapture_Dev_0_2>() || | 52 return has_interface<PPB_VideoCapture_Dev_0_2>() || |
| 90 has_interface<PPB_VideoCapture_Dev_0_1>(); | 53 has_interface<PPB_VideoCapture_Dev_0_1>(); |
| 91 } | 54 } |
| 92 | 55 |
| 93 int32_t VideoCapture_Dev::EnumerateDevices(std::vector<DeviceRef_Dev>* devices, | 56 int32_t VideoCapture_Dev::EnumerateDevices( |
| 94 const CompletionCallback& callback) { | 57 const CompletionCallbackWithOutput<std::vector<DeviceRef_Dev> >& callback) { |
| 95 if (!has_interface<PPB_VideoCapture_Dev_0_2>()) | 58 if (!has_interface<PPB_VideoCapture_Dev_0_2>()) |
| 96 return callback.MayForce(PP_ERROR_NOINTERFACE); | 59 return callback.MayForce(PP_ERROR_NOINTERFACE); |
| 97 if (!devices) | |
| 98 return callback.MayForce(PP_ERROR_BADARGUMENT); | |
| 99 if (!callback.pp_completion_callback().func) | 60 if (!callback.pp_completion_callback().func) |
| 100 return callback.MayForce(PP_ERROR_BLOCKS_MAIN_THREAD); | 61 return callback.MayForce(PP_ERROR_BLOCKS_MAIN_THREAD); |
| 101 if (enum_state_) | |
| 102 return callback.MayForce(PP_ERROR_INPROGRESS); | |
| 103 | 62 |
| 104 // It will be deleted in OnEnumerateDevicesComplete(). | 63 // CallbackConverter is responsible to delete it. |
| 105 enum_state_ = new EnumerateDevicesState(devices, callback, this); | 64 EnumerateCallbackData0_2* data = new EnumerateCallbackData0_2; |
| 65 data->devices = 0; |
| 66 data->output = callback.output(); |
| 67 data->original_callback = callback.pp_completion_callback(); |
| 68 |
| 106 return get_interface<PPB_VideoCapture_Dev_0_2>()->EnumerateDevices( | 69 return get_interface<PPB_VideoCapture_Dev_0_2>()->EnumerateDevices( |
| 107 pp_resource(), &enum_state_->devices_resource, | 70 pp_resource(), &data->devices, |
| 108 PP_MakeCompletionCallback(&VideoCapture_Dev::OnEnumerateDevicesComplete, | 71 PP_MakeCompletionCallback(&CallbackConverter, data)); |
| 109 enum_state_)); | |
| 110 } | 72 } |
| 111 | 73 |
| 112 int32_t VideoCapture_Dev::Open( | 74 int32_t VideoCapture_Dev::Open( |
| 113 const DeviceRef_Dev& device_ref, | 75 const DeviceRef_Dev& device_ref, |
| 114 const PP_VideoCaptureDeviceInfo_Dev& requested_info, | 76 const PP_VideoCaptureDeviceInfo_Dev& requested_info, |
| 115 uint32_t buffer_count, | 77 uint32_t buffer_count, |
| 116 const CompletionCallback& callback) { | 78 const CompletionCallback& callback) { |
| 117 if (has_interface<PPB_VideoCapture_Dev_0_2>()) { | 79 if (has_interface<PPB_VideoCapture_Dev_0_2>()) { |
| 118 return get_interface<PPB_VideoCapture_Dev_0_2>()->Open( | 80 return get_interface<PPB_VideoCapture_Dev_0_2>()->Open( |
| 119 pp_resource(), device_ref.pp_resource(), &requested_info, buffer_count, | 81 pp_resource(), device_ref.pp_resource(), &requested_info, buffer_count, |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 } | 134 } |
| 173 | 135 |
| 174 return PP_ERROR_NOINTERFACE; | 136 return PP_ERROR_NOINTERFACE; |
| 175 } | 137 } |
| 176 | 138 |
| 177 void VideoCapture_Dev::Close() { | 139 void VideoCapture_Dev::Close() { |
| 178 if (has_interface<PPB_VideoCapture_Dev_0_2>()) | 140 if (has_interface<PPB_VideoCapture_Dev_0_2>()) |
| 179 get_interface<PPB_VideoCapture_Dev_0_2>()->Close(pp_resource()); | 141 get_interface<PPB_VideoCapture_Dev_0_2>()->Close(pp_resource()); |
| 180 } | 142 } |
| 181 | 143 |
| 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 | 144 |
| 192 // static | 145 // static |
| 193 void VideoCapture_Dev::OnEnumerateDevicesComplete(void* user_data, | 146 void VideoCapture_Dev::CallbackConverter(void* user_data, int32_t result) { |
| 194 int32_t result) { | 147 EnumerateCallbackData0_2* data = |
| 195 EnumerateDevicesState* enum_state = | 148 static_cast<EnumerateCallbackData0_2*>(user_data); |
| 196 static_cast<EnumerateDevicesState*>(user_data); | |
| 197 | 149 |
| 198 bool need_to_callback = !!enum_state->video_capture; | 150 // data->devices should remain 0 if the call to EnumerateDevices failed. |
| 151 ResourceArray_Dev resources(PASS_REF, data->devices); |
| 152 PP_DCHECK(resources.is_null() || result == PP_OK); |
| 199 | 153 |
| 200 if (result == PP_OK) { | 154 // Need to issue the "GetDataBuffer" even for error cases and when the number |
| 201 // It will take care of releasing the reference. | 155 // of items is 0. |
| 202 ResourceArray_Dev resources(pp::PASS_REF, enum_state->devices_resource); | 156 PP_Resource* output_buf = static_cast<PP_Resource*>( |
| 203 | 157 data->output.GetDataBuffer( |
| 204 if (need_to_callback) { | 158 data->output.user_data, resources.is_null() ? 0 : resources.size(), |
| 205 enum_state->devices->clear(); | 159 sizeof(PP_Resource))); |
| 206 for (uint32_t index = 0; index < resources.size(); ++index) { | 160 if (output_buf) { |
| 207 DeviceRef_Dev device(resources[index]); | 161 for (uint32_t index = 0; index < resources.size(); ++index) { |
| 208 enum_state->devices->push_back(device); | 162 output_buf[index] = resources[index]; |
| 209 } | 163 Module::Get()->core()->AddRefResource(output_buf[index]); |
| 210 } | 164 } |
| 211 } | 165 } |
| 212 | 166 |
| 213 if (need_to_callback) { | 167 PP_RunCompletionCallback(&data->original_callback, result); |
| 214 enum_state->video_capture->enum_state_ = NULL; | 168 delete data; |
| 215 enum_state->callback.Run(result); | |
| 216 } | |
| 217 | |
| 218 delete enum_state; | |
| 219 } | 169 } |
| 220 | 170 |
| 221 } // namespace pp | 171 } // namespace pp |
| OLD | NEW |