| 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 "base/macros.h" |
| 9 #include "base/synchronization/waitable_event.h" |
| 10 #include "base/threading/thread.h" |
| 11 #include "media/capture/video/chromeos/mojo/arc_camera3.mojom.h" |
| 12 #include "media/capture/video/video_capture_device_factory.h" |
| 13 #include "media/capture/video_capture_types.h" |
| 14 #include "mojo/public/cpp/bindings/binding.h" |
| 15 |
| 16 namespace media { |
| 17 |
| 18 // VideoCaptureDeviceFactoryChromeOS delegates all its VCD factory interface |
| 19 // function calls to CameraHalDelegate. |
| 20 |
| 21 class CAPTURE_EXPORT CameraHalDelegate final |
| 22 : public base::RefCountedThreadSafe<CameraHalDelegate>, |
| 23 public arc::mojom::CameraModuleCallbacks { |
| 24 public: |
| 25 CameraHalDelegate( |
| 26 const scoped_refptr<base::SingleThreadTaskRunner> module_task_runner); |
| 27 |
| 28 // Establishes the Mojo IPC channel to the camera HAL adapter. This method |
| 29 // should be called before any other methods of CameraHalDelegate is called. |
| 30 // Called by VideoCaptureDeviceFactoryChromeOS::Init on the browser IO thread. |
| 31 bool StartCameraModuleIpc(); |
| 32 |
| 33 // Delegation methods for the VideoCaptureDeviceFactory interface. These |
| 34 // methods are called by VideoCaptureDeviceFactoryChromeOS directly. They |
| 35 // operate on the same thread that the VideoCaptureDeviceFactoryChromeOS runs |
| 36 // on. |
| 37 std::unique_ptr<VideoCaptureDevice> CreateDevice( |
| 38 const scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, |
| 39 const VideoCaptureDeviceDescriptor& device_descriptor); |
| 40 void GetSupportedFormats( |
| 41 const VideoCaptureDeviceDescriptor& device_descriptor, |
| 42 VideoCaptureFormats* supported_formats); |
| 43 void GetDeviceDescriptors(VideoCaptureDeviceDescriptors* device_descriptors); |
| 44 |
| 45 // Synchronous method to get the camera info of |camera_id|. This method may |
| 46 // be called on any thread. |
| 47 using GetCameraInfoCallback = |
| 48 base::Callback<void(int32_t, arc::mojom::CameraInfoPtr)>; |
| 49 void GetCameraInfo(int32_t camera_id, const GetCameraInfoCallback& callback); |
| 50 |
| 51 // Asynchronous method to open the camera device desginated by |camera_id|. |
| 52 // This method may be called on any thread; |callback| will run on |
| 53 // |module_task_runner_|. |
| 54 using OpenDeviceCallback = |
| 55 base::Callback<void(int32_t, arc::mojom::Camera3DeviceOpsPtr)>; |
| 56 void OpenDevice(int32_t camera_id, const OpenDeviceCallback& callback); |
| 57 |
| 58 private: |
| 59 friend class base::RefCountedThreadSafe<CameraHalDelegate>; |
| 60 |
| 61 ~CameraHalDelegate(); |
| 62 |
| 63 // Resets the Mojo interface and bindings. |
| 64 void ResetMojoInterfaceOnModuleThread(base::WaitableEvent* interface_closed); |
| 65 |
| 66 // Internal method to attempt to update the camera info for all cameras. |
| 67 // Runs on the same thread as CreateDevice, GetSupportedFormats, and |
| 68 // GetDeviceDescriptors. This is the only entry point to update |
| 69 // |num_cameras_| and |camera_info_|. |
| 70 bool UpdateCameraInfo(); |
| 71 |
| 72 // The actual methods that update the camera info. These methods run on |
| 73 // |module_task_runner_|. |
| 74 void UpdateCameraInfoOnModuleThread(); |
| 75 void OnGotNumberOfCamerasOnModuleThread(int32_t num_cameras); |
| 76 void GetCameraInfoOnModuleThread(int32_t camera_id, |
| 77 const GetCameraInfoCallback& callback); |
| 78 void OnGotCameraInfoOnModuleThread(int32_t camera_id, |
| 79 int32_t result, |
| 80 arc::mojom::CameraInfoPtr camera_info); |
| 81 |
| 82 // Signaled when |num_cameras_| and |camera_info_| are updated. Queried and |
| 83 // waited by UpdateCameraInfo, signaled by OnGotCameraInfoOnModuleThread, and |
| 84 // reset by CameraDeviceStatusChange. |
| 85 base::WaitableEvent camera_info_updated_; |
| 86 |
| 87 // Indicates if a camera info update is in flight, to prevent issuing |
| 88 // duplicated GetCameraInfo calls to |camera_module_delegate_|. |
| 89 bool updating_camera_info_; |
| 90 |
| 91 // |num_cameras_| stores the number of camera devices reported by the camera |
| 92 // HAL, and |camera_info_| stores the camera info of each camera device. |
| 93 // They are modified only on |module_task_runner_|. They are also read in |
| 94 // GetSupportedFormats and GetDeviceDescriptors, in which the access is |
| 95 // sequenced through UpdateCameraInfo and |camera_info_updated_| to avoid race |
| 96 // conditions. |
| 97 uint32_t num_cameras_; |
| 98 std::unordered_map<int32_t, arc::mojom::CameraInfoPtr> camera_info_; |
| 99 |
| 100 // Called by OpenDevice to actually open the device specified by |camera_id|. |
| 101 // This method runs on |module_task_runner_|. |
| 102 void OpenDeviceOnModuleThread(int32_t camera_id, |
| 103 const OpenDeviceCallback& callback); |
| 104 |
| 105 // Registers camera HAL module callback functions. SetCallbacks is called |
| 106 // after GetNumberOfCameras is called for the first time, and before any other |
| 107 // calls to |camera_module_|. These methods run on |module_task_runner_|. |
| 108 void SetCallbacksOnModuleThread(); |
| 109 void OnSetCallbacksOnModuleThread(int32_t result); |
| 110 // Indicates if the module callbacks are successfully registered. |
| 111 bool module_callbacks_registered_; |
| 112 |
| 113 // CameraModuleCallbacks implementation. Operates on |module_task_runner_|. |
| 114 void CameraDeviceStatusChange( |
| 115 int32_t camera_id, |
| 116 arc::mojom::CameraDeviceStatus new_status) final; |
| 117 |
| 118 base::ThreadChecker thread_checker_; |
| 119 |
| 120 // The task runner where all the camera module Mojo communication takes place. |
| 121 const scoped_refptr<base::SingleThreadTaskRunner> module_task_runner_; |
| 122 |
| 123 // The Mojo proxy to access the camera module at the remote camera HAL. Bound |
| 124 // to |module_task_runner_|. |
| 125 arc::mojom::CameraModulePtr camera_module_; |
| 126 |
| 127 // The Mojo binding serving the camera module callbacks. Bound to |
| 128 // |module_task_runner_|. |
| 129 mojo::Binding<arc::mojom::CameraModuleCallbacks> camera_module_callbacks_; |
| 130 |
| 131 DISALLOW_COPY_AND_ASSIGN(CameraHalDelegate); |
| 132 }; |
| 133 |
| 134 } // namespace media |
| 135 |
| 136 #endif // MEDIA_CAPTURE_VIDEO_CHROMEOS_CAMERA_HAL_DELEGATE_H_ |
| OLD | NEW |