| 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 #ifndef MEDIA_CAPTURE_VIDEO_CHROMEOS_CAMERA_HAL_DELEGATE_H_ |
| 6 #define MEDIA_CAPTURE_VIDEO_CHROMEOS_CAMERA_HAL_DELEGATE_H_ |
| 7 |
| 8 #include <memory> |
| 9 #include <string> |
| 10 #include <unordered_map> |
| 11 |
| 12 #include "base/macros.h" |
| 13 #include "base/sequence_checker.h" |
| 14 #include "base/synchronization/waitable_event.h" |
| 15 #include "base/threading/thread.h" |
| 16 #include "media/capture/video/chromeos/mojo/arc_camera3.mojom.h" |
| 17 #include "media/capture/video/video_capture_device_factory.h" |
| 18 #include "media/capture/video_capture_types.h" |
| 19 #include "mojo/public/cpp/bindings/binding.h" |
| 20 |
| 21 namespace media { |
| 22 |
| 23 // CameraHalDelegate is the component which does Mojo IPCs to the camera HAL |
| 24 // process on Chrome OS to access the module-level camera functionalities such |
| 25 // as camera device info look-up and opening camera devices. |
| 26 // |
| 27 // CameraHalDelegate is refcounted because VideoCaptureDeviceFactoryChromeOS and |
| 28 // CameraDeviceDelegate both need to reference CameraHalDelegate, and |
| 29 // VideoCaptureDeviceFactoryChromeOS may be destroyed while CameraDeviceDelegate |
| 30 // is still alive. |
| 31 class CAPTURE_EXPORT CameraHalDelegate final |
| 32 : public base::RefCountedThreadSafe<CameraHalDelegate>, |
| 33 public arc::mojom::CameraModuleCallbacks { |
| 34 public: |
| 35 // All the Mojo IPC operations happen on |ipc_task_runner|. |
| 36 explicit CameraHalDelegate( |
| 37 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner); |
| 38 |
| 39 // Establishes the Mojo IPC channel to the camera HAL adapter. This method |
| 40 // should be called before any other methods of CameraHalDelegate is called. |
| 41 bool StartCameraModuleIpc(); |
| 42 |
| 43 // Resets |camera_module_| and |camera_module_callbacks_|. |
| 44 void Reset(); |
| 45 |
| 46 // Delegation methods for the VideoCaptureDeviceFactory interface. These |
| 47 // methods are called by VideoCaptureDeviceFactoryChromeOS directly. They |
| 48 // operate on the same thread that the VideoCaptureDeviceFactoryChromeOS runs |
| 49 // on. |
| 50 std::unique_ptr<VideoCaptureDevice> CreateDevice( |
| 51 scoped_refptr<base::SingleThreadTaskRunner> |
| 52 task_runner_for_screen_observer, |
| 53 const VideoCaptureDeviceDescriptor& device_descriptor); |
| 54 void GetSupportedFormats( |
| 55 const VideoCaptureDeviceDescriptor& device_descriptor, |
| 56 VideoCaptureFormats* supported_formats); |
| 57 void GetDeviceDescriptors(VideoCaptureDeviceDescriptors* device_descriptors); |
| 58 |
| 59 // Asynchronous method to get the camera info of |camera_id|. This method may |
| 60 // be called on any thread. |
| 61 using GetCameraInfoCallback = |
| 62 base::Callback<void(int32_t, arc::mojom::CameraInfoPtr)>; |
| 63 void GetCameraInfo(int32_t camera_id, const GetCameraInfoCallback& callback); |
| 64 |
| 65 // Asynchronous method to open the camera device designated by |camera_id|. |
| 66 // This method may be called on any thread; |callback| will run on |
| 67 // |ipc_task_runner_|. |
| 68 using OpenDeviceCallback = base::Callback<void(int32_t)>; |
| 69 void OpenDevice(int32_t camera_id, |
| 70 arc::mojom::Camera3DeviceOpsRequest device_ops_request, |
| 71 const OpenDeviceCallback& callback); |
| 72 |
| 73 private: |
| 74 friend class base::RefCountedThreadSafe<CameraHalDelegate>; |
| 75 |
| 76 ~CameraHalDelegate() final; |
| 77 |
| 78 friend class CameraHalDelegateTest; |
| 79 friend class CameraDeviceDelegateTest; |
| 80 void StartForTesting(arc::mojom::CameraModulePtrInfo info); |
| 81 |
| 82 // Resets the Mojo interface and bindings. |
| 83 void ResetMojoInterfaceOnIpcThread(); |
| 84 |
| 85 // Internal method to update the camera info for all built-in cameras. Runs on |
| 86 // the same thread as CreateDevice, GetSupportedFormats, and |
| 87 // GetDeviceDescriptors. |
| 88 bool UpdateBuiltInCameraInfo(); |
| 89 void UpdateBuiltInCameraInfoOnIpcThread(); |
| 90 // Callback for GetNumberOfCameras Mojo IPC function. GetNumberOfCameras |
| 91 // returns the number of built-in cameras on the device. |
| 92 void OnGotNumberOfCamerasOnIpcThread(int32_t num_cameras); |
| 93 // Callback for SetCallbacks Mojo IPC function. SetCallbacks is called after |
| 94 // GetNumberOfCameras is called for the first time, and before any other calls |
| 95 // to |camera_module_|. |
| 96 void OnSetCallbacksOnIpcThread(int32_t result); |
| 97 void GetCameraInfoOnIpcThread(int32_t camera_id, |
| 98 const GetCameraInfoCallback& callback); |
| 99 void OnGotCameraInfoOnIpcThread(int32_t camera_id, |
| 100 int32_t result, |
| 101 arc::mojom::CameraInfoPtr camera_info); |
| 102 |
| 103 // Called by OpenDevice to actually open the device specified by |camera_id|. |
| 104 // This method runs on |ipc_task_runner_|. |
| 105 void OpenDeviceOnIpcThread( |
| 106 int32_t camera_id, |
| 107 arc::mojom::Camera3DeviceOpsRequest device_ops_request, |
| 108 const OpenDeviceCallback& callback); |
| 109 |
| 110 // CameraModuleCallbacks implementation. Operates on |ipc_task_runner_|. |
| 111 void CameraDeviceStatusChange( |
| 112 int32_t camera_id, |
| 113 arc::mojom::CameraDeviceStatus new_status) final; |
| 114 |
| 115 // Signaled when |num_builtin_cameras_| and |camera_info_| are updated. |
| 116 // Queried and waited by UpdateBuiltInCameraInfo, signaled by |
| 117 // OnGotCameraInfoOnIpcThread. |
| 118 base::WaitableEvent builtin_camera_info_updated_; |
| 119 |
| 120 // |num_builtin_cameras_| stores the number of built-in camera devices |
| 121 // reported by the camera HAL, and |camera_info_| stores the camera info of |
| 122 // each camera device. They are modified only on |ipc_task_runner_|. They |
| 123 // are also read in GetSupportedFormats and GetDeviceDescriptors, in which the |
| 124 // access is sequenced through UpdateBuiltInCameraInfo and |
| 125 // |builtin_camera_info_updated_| to avoid race conditions. |
| 126 size_t num_builtin_cameras_; |
| 127 std::unordered_map<std::string, arc::mojom::CameraInfoPtr> camera_info_; |
| 128 |
| 129 SEQUENCE_CHECKER(sequence_checker_); |
| 130 |
| 131 // The task runner where all the camera module Mojo communication takes place. |
| 132 const scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_; |
| 133 |
| 134 // The Mojo proxy to access the camera module at the remote camera HAL. Bound |
| 135 // to |ipc_task_runner_|. |
| 136 arc::mojom::CameraModulePtr camera_module_; |
| 137 |
| 138 // The Mojo binding serving the camera module callbacks. Bound to |
| 139 // |ipc_task_runner_|. |
| 140 mojo::Binding<arc::mojom::CameraModuleCallbacks> camera_module_callbacks_; |
| 141 |
| 142 DISALLOW_COPY_AND_ASSIGN(CameraHalDelegate); |
| 143 }; |
| 144 |
| 145 } // namespace media |
| 146 |
| 147 #endif // MEDIA_CAPTURE_VIDEO_CHROMEOS_CAMERA_HAL_DELEGATE_H_ |
| OLD | NEW |