| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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 "media/capture/video/chromeos/camera_hal_delegate.h" |
| 6 |
| 7 #include <fcntl.h> |
| 8 #include <sys/uio.h> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" |
| 12 #include "base/strings/string_piece.h" |
| 13 #include "media/capture/video/chromeos/camera_metadata_utils.h" |
| 14 #include "media/capture/video/chromeos/video_capture_device_arc_chromeos.h" |
| 15 #include "mojo/edk/embedder/embedder.h" |
| 16 #include "mojo/edk/embedder/named_platform_handle.h" |
| 17 #include "mojo/edk/embedder/named_platform_handle_utils.h" |
| 18 #include "mojo/edk/embedder/pending_process_connection.h" |
| 19 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 20 #include "mojo/edk/embedder/platform_channel_utils_posix.h" |
| 21 #include "mojo/edk/embedder/platform_handle_vector.h" |
| 22 |
| 23 namespace media { |
| 24 |
| 25 namespace { |
| 26 |
| 27 const base::StringPiece kArcCamera3SocketPath("/var/run/camera/camera3.sock"); |
| 28 |
| 29 const base::TimeDelta kEventWaitTimeoutMs = |
| 30 base::TimeDelta::FromMilliseconds(3000); |
| 31 |
| 32 } // namespace |
| 33 |
| 34 CameraHalDelegate::CameraHalDelegate( |
| 35 const scoped_refptr<base::SingleThreadTaskRunner> module_task_runner) |
| 36 : builtin_camera_info_updated_( |
| 37 base::WaitableEvent::ResetPolicy::MANUAL, |
| 38 base::WaitableEvent::InitialState::NOT_SIGNALED), |
| 39 num_builtin_cameras_(0), |
| 40 module_task_runner_(module_task_runner), |
| 41 camera_module_callbacks_(this) { |
| 42 // We need to detach |thread_checker_| here because constructor is called on |
| 43 // UI thread while the VideoCaptureDeviceFactory methods are called on capture |
| 44 // thread. |
| 45 thread_checker_.DetachFromThread(); |
| 46 } |
| 47 |
| 48 CameraHalDelegate::~CameraHalDelegate() { |
| 49 base::WaitableEvent interface_closed( |
| 50 base::WaitableEvent::ResetPolicy::MANUAL, |
| 51 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 52 module_task_runner_->PostTask( |
| 53 FROM_HERE, |
| 54 base::Bind(&CameraHalDelegate::ResetMojoInterfaceOnModuleThread, |
| 55 base::Unretained(this), base::Unretained(&interface_closed))); |
| 56 interface_closed.Wait(); |
| 57 } |
| 58 |
| 59 bool CameraHalDelegate::StartCameraModuleIpc() { |
| 60 // Non-blocking socket handle. |
| 61 mojo::edk::ScopedPlatformHandle socket_handle = mojo::edk::CreateClientHandle( |
| 62 mojo::edk::NamedPlatformHandle(kArcCamera3SocketPath)); |
| 63 |
| 64 // Set socket to blocking |
| 65 int flags = HANDLE_EINTR(fcntl(socket_handle.get().handle, F_GETFL)); |
| 66 if (flags == -1) { |
| 67 PLOG(ERROR) << "fcntl(F_GETFL) failed: "; |
| 68 return false; |
| 69 } |
| 70 if (HANDLE_EINTR(fcntl(socket_handle.get().handle, F_SETFL, |
| 71 flags & ~O_NONBLOCK)) == -1) { |
| 72 PLOG(ERROR) << "fcntl(F_SETFL) failed: "; |
| 73 return false; |
| 74 } |
| 75 |
| 76 const size_t kTokenSize = 32; |
| 77 char token[kTokenSize] = {}; |
| 78 std::deque<mojo::edk::PlatformHandle> platform_handles; |
| 79 ssize_t ret = mojo::edk::PlatformChannelRecvmsg( |
| 80 socket_handle.get(), token, sizeof(token), &platform_handles, true); |
| 81 if (ret == -1) { |
| 82 PLOG(ERROR) << "PlatformChannelRecvmsg failed: "; |
| 83 return false; |
| 84 } |
| 85 if (platform_handles.size() != 1) { |
| 86 LOG(ERROR) << "Unexpected number of handles received, expected 1: " |
| 87 << platform_handles.size(); |
| 88 return false; |
| 89 } |
| 90 mojo::edk::ScopedPlatformHandle parent_pipe(platform_handles.back()); |
| 91 platform_handles.pop_back(); |
| 92 if (!parent_pipe.is_valid()) { |
| 93 LOG(ERROR) << "Invalid parent pipe"; |
| 94 return false; |
| 95 } |
| 96 mojo::edk::SetParentPipeHandle(std::move(parent_pipe)); |
| 97 |
| 98 mojo::ScopedMessagePipeHandle child_pipe = |
| 99 mojo::edk::CreateChildMessagePipe(std::string(token, kTokenSize)); |
| 100 |
| 101 camera_module_ = |
| 102 mojo::MakeProxy(mojo::InterfacePtrInfo<arc::mojom::CameraModule>( |
| 103 std::move(child_pipe), 0u), |
| 104 module_task_runner_); |
| 105 |
| 106 VLOG(1) << "Camera module IPC connection established"; |
| 107 |
| 108 return true; |
| 109 } |
| 110 |
| 111 std::unique_ptr<VideoCaptureDevice> CameraHalDelegate::CreateDevice( |
| 112 const scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, |
| 113 const VideoCaptureDeviceDescriptor& device_descriptor) { |
| 114 DCHECK(thread_checker_.CalledOnValidThread()); |
| 115 std::unique_ptr<VideoCaptureDevice> capture_device; |
| 116 if (!UpdateBuiltInCameraInfo()) { |
| 117 return capture_device; |
| 118 } |
| 119 if (camera_info_.find(device_descriptor.device_id) == camera_info_.end()) { |
| 120 LOG(ERROR) << "Invalid camera device: " << device_descriptor.device_id; |
| 121 return capture_device; |
| 122 } |
| 123 capture_device.reset(new VideoCaptureDeviceArcChromeOS( |
| 124 ui_task_runner, device_descriptor, this)); |
| 125 return capture_device; |
| 126 } |
| 127 |
| 128 void CameraHalDelegate::GetSupportedFormats( |
| 129 const VideoCaptureDeviceDescriptor& device_descriptor, |
| 130 VideoCaptureFormats* supported_formats) { |
| 131 DCHECK(thread_checker_.CalledOnValidThread()); |
| 132 |
| 133 if (!UpdateBuiltInCameraInfo()) { |
| 134 return; |
| 135 } |
| 136 std::string camera_id = device_descriptor.device_id; |
| 137 if (camera_info_.find(camera_id) == camera_info_.end() || |
| 138 camera_info_[camera_id].is_null()) { |
| 139 // The camera may be removed already or is not ready yet. |
| 140 VLOG(1) << "Invalid camera_id: " << camera_id; |
| 141 return; |
| 142 } |
| 143 const arc::mojom::CameraInfoPtr& camera_info = camera_info_[camera_id]; |
| 144 |
| 145 const arc::mojom::CameraMetadataEntryPtr* fps_settings = |
| 146 GetMetadataEntry(camera_info->static_camera_characteristics, |
| 147 arc::mojom::CameraMetadataTag:: |
| 148 ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES); |
| 149 if (!fps_settings) { |
| 150 LOG(ERROR) << "Failed to get available target FPS ranges from camera info"; |
| 151 return; |
| 152 } |
| 153 // The available target FPS ranges are stored as pairs of two int32s: |
| 154 // (min_fps, max_fps). |
| 155 float max_fps = 0.0; |
| 156 int32_t* iter = reinterpret_cast<int32_t*>((*fps_settings)->data.data()); |
| 157 // There may be multiple FPS ranges. We simply use the maximum FPS of all the |
| 158 // FPS ranges here. |
| 159 for (size_t i = 0; i < (*fps_settings)->count / sizeof(int32_t); ++i) { |
| 160 float fps = static_cast<float>(*(iter + 1)); |
| 161 if (fps > max_fps) { |
| 162 max_fps = fps; |
| 163 } |
| 164 iter += 2; |
| 165 } |
| 166 |
| 167 const arc::mojom::CameraMetadataEntryPtr* configurations = |
| 168 GetMetadataEntry(camera_info->static_camera_characteristics, |
| 169 arc::mojom::CameraMetadataTag:: |
| 170 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS); |
| 171 if (!configurations) { |
| 172 LOG(ERROR) |
| 173 << "Failed to get available stream configurations from camera info"; |
| 174 return; |
| 175 } |
| 176 // The available stream configurations are stored as tuples of four int32s: |
| 177 // (hal_pixel_format, width, height, input_or_output_type). |
| 178 iter = reinterpret_cast<int32_t*>((*configurations)->data.data()); |
| 179 for (size_t i = 0; i < (*configurations)->count / sizeof(int32_t); ++i) { |
| 180 arc::mojom::HalPixelFormat format = |
| 181 static_cast<arc::mojom::HalPixelFormat>(*iter); |
| 182 int32_t width = *(iter + 1); |
| 183 int32_t height = *(iter + 2); |
| 184 int32_t type = *(iter + 3); |
| 185 iter += 4; |
| 186 DVLOG(1) << "[" << std::hex << format << " " << std::dec << width << " " |
| 187 << height << " " << type << "]"; |
| 188 VideoPixelFormat cr_format = |
| 189 VideoCaptureDeviceArcChromeOS::PixFormatHalToChromium(format); |
| 190 if (cr_format == PIXEL_FORMAT_UNKNOWN) { |
| 191 continue; |
| 192 } |
| 193 VLOG(1) << "Supported format: " << width << "x" << height |
| 194 << " fps=" << max_fps << " format=" << cr_format; |
| 195 supported_formats->emplace_back(gfx::Size(width, height), max_fps, |
| 196 cr_format); |
| 197 } |
| 198 } |
| 199 |
| 200 void CameraHalDelegate::GetDeviceDescriptors( |
| 201 VideoCaptureDeviceDescriptors* device_descriptors) { |
| 202 DCHECK(thread_checker_.CalledOnValidThread()); |
| 203 |
| 204 if (!UpdateBuiltInCameraInfo()) { |
| 205 return; |
| 206 } |
| 207 for (size_t id = 0; id < num_builtin_cameras_; ++id) { |
| 208 VideoCaptureDeviceDescriptor desc; |
| 209 std::string camera_id = std::to_string(id); |
| 210 const arc::mojom::CameraInfoPtr& camera_info = camera_info_[camera_id]; |
| 211 if (!camera_info) { |
| 212 continue; |
| 213 } |
| 214 desc.device_id = camera_id; |
| 215 desc.capture_api = VideoCaptureApi::ANDROID_API2_LIMITED; |
| 216 desc.transport_type = VideoCaptureTransportType::OTHER_TRANSPORT; |
| 217 switch (camera_info->facing) { |
| 218 case arc::mojom::CameraFacing::CAMERA_FACING_BACK: |
| 219 desc.facing = VideoFacingMode::MEDIA_VIDEO_FACING_ENVIRONMENT; |
| 220 desc.display_name = std::string("Back Camera"); |
| 221 break; |
| 222 case arc::mojom::CameraFacing::CAMERA_FACING_FRONT: |
| 223 desc.facing = VideoFacingMode::MEDIA_VIDEO_FACING_USER; |
| 224 desc.display_name = std::string("Front Camera"); |
| 225 break; |
| 226 case arc::mojom::CameraFacing::CAMERA_FACING_EXTERNAL: |
| 227 desc.facing = VideoFacingMode::MEDIA_VIDEO_FACING_NONE; |
| 228 desc.display_name = std::string("External Camera"); |
| 229 break; |
| 230 // Mojo validates the input parameters for us so we don't need to worry |
| 231 // about malformed values. |
| 232 } |
| 233 device_descriptors->push_back(desc); |
| 234 } |
| 235 } |
| 236 |
| 237 void CameraHalDelegate::GetCameraInfo(int32_t camera_id, |
| 238 const GetCameraInfoCallback& callback) { |
| 239 // This method may be called on any thread. Currently this method is used by |
| 240 // VideoCaptureDeviceArcChromeOS to query camera info. |
| 241 module_task_runner_->PostTask( |
| 242 FROM_HERE, base::Bind(&CameraHalDelegate::GetCameraInfoOnModuleThread, |
| 243 this, camera_id, callback)); |
| 244 } |
| 245 |
| 246 void CameraHalDelegate::OpenDevice(int32_t camera_id, |
| 247 const OpenDeviceCallback& callback) { |
| 248 // This method may be called on any thread. Currently this method is used by |
| 249 // VideoCaptureDeviceArcChromeOS to open a camera device. |
| 250 module_task_runner_->PostTask( |
| 251 FROM_HERE, base::Bind(&CameraHalDelegate::OpenDeviceOnModuleThread, this, |
| 252 camera_id, callback)); |
| 253 } |
| 254 |
| 255 void CameraHalDelegate::ResetMojoInterfaceOnModuleThread( |
| 256 base::WaitableEvent* interface_closed) { |
| 257 DCHECK(module_task_runner_->BelongsToCurrentThread()); |
| 258 camera_module_.reset(); |
| 259 if (camera_module_callbacks_.is_bound()) { |
| 260 camera_module_callbacks_.Unbind(); |
| 261 } |
| 262 interface_closed->Signal(); |
| 263 } |
| 264 |
| 265 bool CameraHalDelegate::UpdateBuiltInCameraInfo() { |
| 266 DCHECK(thread_checker_.CalledOnValidThread()); |
| 267 if (builtin_camera_info_updated_.IsSignaled()) { |
| 268 return true; |
| 269 } |
| 270 // The built-in camera are static per specification of the Android camera HAL |
| 271 // v3 specification. We only update the built-in camera info once. |
| 272 module_task_runner_->PostTask( |
| 273 FROM_HERE, |
| 274 base::Bind(&CameraHalDelegate::UpdateBuiltInCameraInfoOnModuleThread, |
| 275 this)); |
| 276 if (!builtin_camera_info_updated_.TimedWait(kEventWaitTimeoutMs)) { |
| 277 LOG(ERROR) << "Timed out getting camera info"; |
| 278 return false; |
| 279 } |
| 280 return true; |
| 281 } |
| 282 |
| 283 void CameraHalDelegate::UpdateBuiltInCameraInfoOnModuleThread() { |
| 284 DCHECK(module_task_runner_->BelongsToCurrentThread()); |
| 285 camera_module_->GetNumberOfCameras( |
| 286 base::Bind(&CameraHalDelegate::OnGotNumberOfCamerasOnModuleThread, this)); |
| 287 } |
| 288 |
| 289 void CameraHalDelegate::OnGotNumberOfCamerasOnModuleThread( |
| 290 int32_t num_cameras) { |
| 291 DCHECK(module_task_runner_->BelongsToCurrentThread()); |
| 292 if (num_cameras < 0) { |
| 293 builtin_camera_info_updated_.Signal(); |
| 294 LOG(ERROR) << "Failed to get number of cameras"; |
| 295 return; |
| 296 } |
| 297 VLOG(1) << "Number of built-in cameras: " << num_cameras; |
| 298 num_builtin_cameras_ = num_cameras; |
| 299 // Per camera HAL v3 specification SetCallbacks() should be called after the |
| 300 // first time GetNumberOfCameras() is called, and before other CameraModule |
| 301 // functions are called. |
| 302 camera_module_->SetCallbacks( |
| 303 camera_module_callbacks_.CreateInterfacePtrAndBind(), |
| 304 base::Bind(&CameraHalDelegate::OnSetCallbacksOnModuleThread, this)); |
| 305 } |
| 306 |
| 307 void CameraHalDelegate::OnSetCallbacksOnModuleThread(int32_t result) { |
| 308 DCHECK(module_task_runner_->BelongsToCurrentThread()); |
| 309 if (result) { |
| 310 num_builtin_cameras_ = 0; |
| 311 builtin_camera_info_updated_.Signal(); |
| 312 LOG(ERROR) << "Failed to set camera module callbacks: " << strerror(result); |
| 313 return; |
| 314 } |
| 315 for (size_t camera_id = 0; camera_id < num_builtin_cameras_; ++camera_id) { |
| 316 GetCameraInfoOnModuleThread( |
| 317 camera_id, base::Bind(&CameraHalDelegate::OnGotCameraInfoOnModuleThread, |
| 318 this, camera_id)); |
| 319 } |
| 320 } |
| 321 |
| 322 void CameraHalDelegate::GetCameraInfoOnModuleThread( |
| 323 int32_t camera_id, |
| 324 const GetCameraInfoCallback& callback) { |
| 325 DCHECK(module_task_runner_->BelongsToCurrentThread()); |
| 326 camera_module_->GetCameraInfo(camera_id, callback); |
| 327 } |
| 328 |
| 329 void CameraHalDelegate::OnGotCameraInfoOnModuleThread( |
| 330 int32_t camera_id, |
| 331 int32_t result, |
| 332 arc::mojom::CameraInfoPtr camera_info) { |
| 333 DCHECK(module_task_runner_->BelongsToCurrentThread()); |
| 334 DVLOG(1) << "Got camera info of camera " << camera_id; |
| 335 if (result) { |
| 336 // The call to get_camera_info may fail if the device is still in the |
| 337 // enumerating state or is no longer present. |
| 338 VLOG(1) << "Failed to get camera info. Camera id: " << camera_id; |
| 339 } |
| 340 // In case of failure |camera_info| is empty. |
| 341 camera_info_[std::to_string(camera_id)] = std::move(camera_info); |
| 342 if (!builtin_camera_info_updated_.IsSignaled()) { |
| 343 // Make sure all the built-in camera info is set. |camera_info_| may hold |
| 344 // info of external cameras as well so we need to check each id of the |
| 345 // built-in cameras here. |
| 346 for (size_t id = 0; id < num_builtin_cameras_; ++id) { |
| 347 if (camera_info_.find(std::to_string(id)) == camera_info_.end()) { |
| 348 return; |
| 349 } |
| 350 } |
| 351 builtin_camera_info_updated_.Signal(); |
| 352 } |
| 353 } |
| 354 |
| 355 void CameraHalDelegate::OpenDeviceOnModuleThread( |
| 356 int32_t camera_id, |
| 357 const OpenDeviceCallback& callback) { |
| 358 DCHECK(module_task_runner_->BelongsToCurrentThread()); |
| 359 camera_module_->OpenDevice(camera_id, callback); |
| 360 } |
| 361 |
| 362 // CameraModuleCallbacks implementations. |
| 363 void CameraHalDelegate::CameraDeviceStatusChange( |
| 364 int32_t camera_id, |
| 365 arc::mojom::CameraDeviceStatus new_status) { |
| 366 DCHECK(module_task_runner_->BelongsToCurrentThread()); |
| 367 // TODO(jcliang): Handle status change for external cameras. |
| 368 } |
| 369 |
| 370 } // namespace media |
| OLD | NEW |