Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ppapi/proxy/video_capture_resource.h" | |
| 6 | |
| 7 #include "ppapi/c/dev/ppp_video_capture_dev.h" | |
| 8 #include "ppapi/proxy/dispatch_reply_message.h" | |
| 9 #include "ppapi/proxy/plugin_dispatcher.h" | |
| 10 #include "ppapi/proxy/plugin_globals.h" | |
| 11 #include "ppapi/proxy/plugin_resource_tracker.h" | |
| 12 #include "ppapi/proxy/ppapi_messages.h" | |
| 13 #include "ppapi/proxy/ppb_buffer_proxy.h" | |
| 14 #include "ppapi/proxy/resource_message_params.h" | |
| 15 #include "ppapi/shared_impl/array_writer.h" | |
| 16 #include "ppapi/shared_impl/proxy_lock.h" | |
| 17 #include "ppapi/shared_impl/tracked_callback.h" | |
| 18 | |
| 19 namespace ppapi { | |
| 20 namespace proxy { | |
| 21 | |
| 22 VideoCaptureResource::VideoCaptureResource( | |
| 23 Connection connection, | |
| 24 PP_Instance instance, | |
| 25 PluginDispatcher* dispatcher) | |
| 26 : PluginResource(connection, instance), | |
| 27 open_state_(BEFORE_OPEN), | |
| 28 has_pending_enum_devices_callback_(false) { | |
| 29 SendCreate(RENDERER, PpapiHostMsg_VideoCapture_Create()); | |
| 30 | |
| 31 ppp_video_capture_impl_ = static_cast<const PPP_VideoCapture_Dev*>( | |
| 32 dispatcher->local_get_interface()(PPP_VIDEO_CAPTURE_DEV_INTERFACE)); | |
| 33 } | |
| 34 | |
| 35 VideoCaptureResource::~VideoCaptureResource() { | |
| 36 } | |
| 37 | |
| 38 void VideoCaptureResource::OnReplyReceived( | |
| 39 const ResourceMessageReplyParams& params, | |
| 40 const IPC::Message& msg) { | |
| 41 if (params.sequence()) { | |
| 42 PluginResource::OnReplyReceived(params, msg); | |
| 43 return; | |
| 44 } | |
| 45 | |
| 46 IPC_BEGIN_MESSAGE_MAP(VideoCaptureResource, msg) | |
| 47 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL( | |
| 48 PpapiPluginMsg_VideoCapture_OnDeviceInfo, | |
| 49 OnPluginMsgOnDeviceInfo) | |
| 50 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL( | |
| 51 PpapiPluginMsg_VideoCapture_OnStatus, | |
| 52 OnPluginMsgOnStatus) | |
| 53 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL( | |
| 54 PpapiPluginMsg_VideoCapture_OnError, | |
| 55 OnPluginMsgOnError) | |
| 56 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL( | |
| 57 PpapiPluginMsg_VideoCapture_OnBufferReady, | |
| 58 OnPluginMsgOnBufferReady) | |
| 59 PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_UNHANDLED(NOTREACHED()) | |
| 60 IPC_END_MESSAGE_MAP() | |
| 61 } | |
| 62 | |
| 63 int32_t VideoCaptureResource::EnumerateDevices( | |
| 64 PP_Resource* devices, | |
| 65 scoped_refptr<TrackedCallback> callback) { | |
| 66 if (has_pending_enum_devices_callback_) | |
| 67 return PP_ERROR_INPROGRESS; | |
| 68 | |
| 69 has_pending_enum_devices_callback_ = true; | |
| 70 | |
| 71 Call<PpapiPluginMsg_VideoCapture_EnumerateDevicesReply>( | |
| 72 RENDERER, | |
| 73 PpapiHostMsg_VideoCapture_EnumerateDevices(), | |
| 74 base::Bind(&VideoCaptureResource::OnPluginMsgEnumerateDevicesReply, | |
| 75 this, | |
| 76 devices, | |
| 77 callback)); | |
| 78 return PP_OK_COMPLETIONPENDING; | |
| 79 } | |
| 80 | |
| 81 int32_t VideoCaptureResource::Open( | |
| 82 const std::string& device_id, | |
| 83 const PP_VideoCaptureDeviceInfo_Dev& requested_info, | |
| 84 uint32_t buffer_count, | |
| 85 scoped_refptr<TrackedCallback> callback) { | |
| 86 if (open_state_ != BEFORE_OPEN) | |
| 87 return PP_ERROR_FAILED; | |
| 88 | |
| 89 if (TrackedCallback::IsPending(open_callback_)) | |
| 90 return PP_ERROR_INPROGRESS; | |
| 91 | |
| 92 open_callback_ = callback; | |
| 93 | |
| 94 Call<PpapiPluginMsg_VideoCapture_OpenReply>( | |
| 95 RENDERER, | |
| 96 PpapiHostMsg_VideoCapture_Open(device_id, requested_info, buffer_count), | |
| 97 base::Bind(&VideoCaptureResource::OnPluginMsgOpenReply, this)); | |
| 98 return PP_OK_COMPLETIONPENDING; | |
| 99 } | |
| 100 | |
| 101 int32_t VideoCaptureResource::StartCapture() { | |
| 102 if (open_state_ != OPENED) | |
| 103 return PP_ERROR_FAILED; | |
| 104 | |
| 105 buffer_in_use_.clear(); | |
| 106 Post(RENDERER, PpapiHostMsg_VideoCapture_StartCapture()); | |
| 107 return PP_OK; | |
| 108 } | |
| 109 | |
| 110 int32_t VideoCaptureResource::ReuseBuffer(uint32_t buffer) { | |
| 111 if (buffer >= buffer_in_use_.size() || !buffer_in_use_[buffer]) | |
| 112 return PP_ERROR_BADARGUMENT; | |
| 113 Post(RENDERER, PpapiHostMsg_VideoCapture_ReuseBuffer(buffer)); | |
| 114 return PP_OK; | |
| 115 } | |
| 116 | |
| 117 int32_t VideoCaptureResource::StopCapture() { | |
| 118 if (open_state_ != OPENED) | |
| 119 return PP_ERROR_FAILED; | |
| 120 | |
| 121 Post(RENDERER, PpapiHostMsg_VideoCapture_StopCapture()); | |
| 122 return PP_OK; | |
| 123 } | |
| 124 | |
| 125 void VideoCaptureResource::Close() { | |
| 126 if (open_state_ == CLOSED) | |
| 127 return; | |
| 128 | |
| 129 Post(RENDERER, PpapiHostMsg_VideoCapture_Close()); | |
| 130 | |
| 131 open_state_ = CLOSED; | |
| 132 | |
| 133 if (TrackedCallback::IsPending(open_callback_)) | |
| 134 open_callback_->PostAbort(); | |
| 135 } | |
| 136 | |
| 137 int32_t VideoCaptureResource::EnumerateDevicesSync( | |
| 138 PP_ArrayOutput* devices) { | |
| 139 if (!devices) | |
| 140 return PP_ERROR_BADARGUMENT; | |
| 141 | |
| 142 ArrayWriter output; | |
| 143 output.set_pp_array_output(*devices); | |
| 144 if (!output.is_valid()) | |
| 145 return PP_ERROR_BADARGUMENT; | |
| 146 | |
| 147 std::vector<ppapi::DeviceRefData> device_ref_data; | |
| 148 int result = SyncCall<PpapiPluginMsg_VideoCapture_EnumerateDevicesReply>( | |
|
yzshen1
2012/11/14 06:54:16
Please use int32_t.
victorhsieh
2012/11/14 08:22:27
Done.
| |
| 149 RENDERER, | |
| 150 PpapiHostMsg_VideoCapture_EnumerateDevices(), | |
| 151 &device_ref_data); | |
| 152 | |
| 153 std::vector<scoped_refptr<Resource> > device_resources; | |
| 154 for (size_t i = 0; i < device_ref_data.size(); ++i) { | |
| 155 scoped_refptr<Resource> resource(new PPB_DeviceRef_Shared( | |
| 156 OBJECT_IS_PROXY, pp_instance(), device_ref_data[i])); | |
| 157 device_resources.push_back(resource); | |
| 158 } | |
| 159 | |
| 160 if (!output.StoreResourceVector(device_resources)) | |
| 161 return PP_ERROR_FAILED; | |
| 162 | |
| 163 return result; | |
| 164 } | |
| 165 | |
| 166 void VideoCaptureResource::OnPluginMsgOnDeviceInfo( | |
| 167 const ResourceMessageReplyParams& params, | |
| 168 const struct PP_VideoCaptureDeviceInfo_Dev& info, | |
| 169 const std::vector<HostResource>& buffers, | |
| 170 uint32_t buffer_size) { | |
| 171 if (!ppp_video_capture_impl_) | |
| 172 return; | |
| 173 | |
| 174 std::vector<base::SharedMemoryHandle> handles; | |
| 175 params.TakeAllSharedMemoryHandles(&handles); | |
| 176 CHECK(handles.size() == buffers.size()); | |
| 177 | |
| 178 PluginResourceTracker* tracker = | |
| 179 PluginGlobals::Get()->plugin_resource_tracker(); | |
| 180 scoped_array<PP_Resource> resources(new PP_Resource[buffers.size()]); | |
| 181 for (size_t i = 0; i < buffers.size(); ++i) { | |
| 182 // We assume that the browser created a new set of resources. | |
| 183 DCHECK(!tracker->PluginResourceForHostResource(buffers[i])); | |
| 184 resources[i] = ppapi::proxy::PPB_Buffer_Proxy::AddProxyResource( | |
| 185 buffers[i], handles[i], buffer_size); | |
| 186 } | |
| 187 | |
| 188 buffer_in_use_ = std::vector<bool>(buffers.size()); | |
| 189 | |
| 190 CallWhileUnlocked(ppp_video_capture_impl_->OnDeviceInfo, | |
| 191 pp_instance(), | |
| 192 pp_resource(), | |
| 193 &info, | |
| 194 static_cast<uint32_t>(buffers.size()), | |
| 195 const_cast<const PP_Resource*>(resources.get())); | |
| 196 | |
| 197 for (size_t i = 0; i < buffers.size(); ++i) | |
| 198 tracker->ReleaseResource(resources[i]); | |
| 199 } | |
| 200 | |
| 201 void VideoCaptureResource::OnPluginMsgOnStatus( | |
| 202 const ResourceMessageReplyParams& params, | |
| 203 uint32_t status) { | |
| 204 switch (status) { | |
| 205 case PP_VIDEO_CAPTURE_STATUS_STARTING: | |
| 206 case PP_VIDEO_CAPTURE_STATUS_STOPPING: | |
| 207 // Those states are not sent by the browser. | |
| 208 NOTREACHED(); | |
| 209 break; | |
| 210 } | |
| 211 if (ppp_video_capture_impl_) { | |
| 212 CallWhileUnlocked(ppp_video_capture_impl_->OnStatus, | |
| 213 pp_instance(), | |
| 214 pp_resource(), | |
| 215 status); | |
| 216 } | |
| 217 } | |
| 218 | |
| 219 void VideoCaptureResource::OnPluginMsgOnError( | |
| 220 const ResourceMessageReplyParams& params, | |
| 221 uint32_t error_code) { | |
| 222 open_state_ = CLOSED; | |
| 223 if (ppp_video_capture_impl_) { | |
| 224 CallWhileUnlocked(ppp_video_capture_impl_->OnError, | |
| 225 pp_instance(), | |
| 226 pp_resource(), | |
| 227 error_code); | |
| 228 } | |
| 229 } | |
| 230 | |
| 231 void VideoCaptureResource::OnPluginMsgOnBufferReady( | |
| 232 const ResourceMessageReplyParams& params, | |
| 233 uint32_t buffer) { | |
| 234 SetBufferInUse(buffer); | |
| 235 if (ppp_video_capture_impl_) { | |
| 236 CallWhileUnlocked(ppp_video_capture_impl_->OnBufferReady, | |
| 237 pp_instance(), | |
| 238 pp_resource(), | |
| 239 buffer); | |
| 240 } | |
| 241 } | |
| 242 | |
| 243 void VideoCaptureResource::OnPluginMsgOpenReply( | |
| 244 const ResourceMessageReplyParams& params) { | |
| 245 if (open_state_ == BEFORE_OPEN && params.result() == PP_OK) | |
| 246 open_state_ = OPENED; | |
| 247 | |
| 248 // The callback may have been aborted by Close(). | |
| 249 if (TrackedCallback::IsPending(open_callback_)) | |
| 250 open_callback_->Run(params.result()); | |
| 251 } | |
| 252 | |
| 253 void VideoCaptureResource::OnPluginMsgEnumerateDevicesReply( | |
| 254 PP_Resource* devices_output, | |
| 255 scoped_refptr<TrackedCallback> callback, | |
| 256 const ResourceMessageReplyParams& params, | |
| 257 const std::vector<DeviceRefData>& devices) { | |
| 258 if (!TrackedCallback::IsPending(callback)) | |
| 259 return; | |
| 260 | |
| 261 DCHECK(has_pending_enum_devices_callback_); | |
| 262 has_pending_enum_devices_callback_ = false; | |
| 263 | |
| 264 if (params.result() == PP_OK && devices_output) { | |
| 265 // devices_output points to the resource array of PP_ArrayOutput. In C++, | |
| 266 // it's typically allocated in VideoCapture_Dev. | |
| 267 *devices_output = PPB_DeviceRef_Shared::CreateResourceArray( | |
| 268 OBJECT_IS_PROXY, pp_instance(), devices); | |
| 269 } | |
| 270 | |
| 271 callback->Run(params.result()); | |
| 272 } | |
| 273 | |
| 274 void VideoCaptureResource::SetBufferInUse(uint32_t buffer_index) { | |
| 275 DCHECK(buffer_index < buffer_in_use_.size()); | |
| 276 buffer_in_use_[buffer_index] = true; | |
| 277 } | |
| 278 | |
| 279 } // namespace proxy | |
| 280 } // namespace ppapi | |
| OLD | NEW |