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