| 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_DEVICE_DELEGATE_H_ | |
| 6 #define MEDIA_CAPTURE_VIDEO_CHROMEOS_CAMERA_DEVICE_DELEGATE_H_ | |
| 7 | |
| 8 #include <memory> | |
| 9 | |
| 10 #include "base/macros.h" | |
| 11 #include "base/memory/weak_ptr.h" | |
| 12 #include "media/capture/video/chromeos/mojo/arc_camera3.mojom.h" | |
| 13 #include "media/capture/video/video_capture_device.h" | |
| 14 #include "media/capture/video_capture_types.h" | |
| 15 | |
| 16 namespace media { | |
| 17 | |
| 18 class CameraHalDelegate; | |
| 19 class CameraDeviceContext; | |
| 20 class StreamBufferManager; | |
| 21 | |
| 22 // The interface to register buffer with and send capture request to the | |
| 23 // camera HAL. | |
| 24 class CAPTURE_EXPORT StreamCaptureInterface { | |
| 25 public: | |
| 26 struct Plane { | |
| 27 Plane(); | |
| 28 ~Plane(); | |
| 29 mojo::ScopedHandle fd; | |
| 30 uint32_t stride; | |
| 31 uint32_t offset; | |
| 32 }; | |
| 33 | |
| 34 virtual ~StreamCaptureInterface() {} | |
| 35 | |
| 36 // Registers a buffer to the camera HAL. | |
| 37 virtual void RegisterBuffer(uint64_t buffer_id, | |
| 38 arc::mojom::Camera3DeviceOps::BufferType type, | |
| 39 uint32_t drm_format, | |
| 40 arc::mojom::HalPixelFormat hal_pixel_format, | |
| 41 uint32_t width, | |
| 42 uint32_t height, | |
| 43 std::vector<Plane> planes, | |
| 44 base::OnceCallback<void(int32_t)> callback); | |
| 45 | |
| 46 // Sends a capture request to the camera HAL. | |
| 47 virtual void ProcessCaptureRequest( | |
| 48 arc::mojom::Camera3CaptureRequestPtr request, | |
| 49 base::OnceCallback<void(int32_t)> callback); | |
| 50 }; | |
| 51 | |
| 52 // CameraDeviceDelegate is instantiated on the capture thread where | |
| 53 // AllocateAndStart of VideoCaptureDeviceArcChromeOS runs on. All the methods | |
| 54 // in CameraDeviceDelegate run on |ipc_task_runner_| and hence all the | |
| 55 // access to member variables is sequenced. | |
| 56 class CAPTURE_EXPORT CameraDeviceDelegate final { | |
| 57 public: | |
| 58 CameraDeviceDelegate( | |
| 59 VideoCaptureDeviceDescriptor device_descriptor, | |
| 60 scoped_refptr<CameraHalDelegate> camera_hal_delegate, | |
| 61 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner); | |
| 62 | |
| 63 ~CameraDeviceDelegate(); | |
| 64 | |
| 65 // Delegation methods for the VideoCaptureDevice interface. | |
| 66 void AllocateAndStart(const VideoCaptureParams& params, | |
| 67 std::unique_ptr<VideoCaptureDevice::Client> client); | |
| 68 void StopAndDeAllocate(base::Closure device_close_callback); | |
| 69 void TakePhoto(VideoCaptureDevice::TakePhotoCallback callback); | |
| 70 void GetPhotoState(VideoCaptureDevice::GetPhotoStateCallback callback); | |
| 71 void SetPhotoOptions(mojom::PhotoSettingsPtr settings, | |
| 72 VideoCaptureDevice::SetPhotoOptionsCallback callback); | |
| 73 | |
| 74 // Sets the frame rotation angle in |rotation_|. |rotation_| is clockwise | |
| 75 // rotation in degrees, and is passed to |client_| along with the captured | |
| 76 // frames. | |
| 77 void SetRotation(int rotation); | |
| 78 | |
| 79 base::WeakPtr<CameraDeviceDelegate> GetWeakPtr(); | |
| 80 | |
| 81 private: | |
| 82 class StreamCaptureInterfaceImpl; | |
| 83 | |
| 84 friend class CameraDeviceDelegateTest; | |
| 85 | |
| 86 // Mojo connection error handler. | |
| 87 void OnMojoConnectionError(); | |
| 88 | |
| 89 // Callback method for the Close Mojo IPC call. This method resets the Mojo | |
| 90 // connection and closes the camera device. | |
| 91 void OnClosed(int32_t result); | |
| 92 | |
| 93 // Resets the Mojo interface and bindings. | |
| 94 void ResetMojoInterface(); | |
| 95 | |
| 96 // Sets |static_metadata_| from |camera_info|. | |
| 97 void OnGotCameraInfo(int32_t result, arc::mojom::CameraInfoPtr camera_info); | |
| 98 | |
| 99 // Creates the Mojo connection to the camera device. | |
| 100 void OnOpenedDevice(int32_t result); | |
| 101 | |
| 102 // Initializes the camera HAL. Initialize sets up the Camera3CallbackOps with | |
| 103 // the camera HAL. OnInitialized continues to ConfigureStreams if the | |
| 104 // Initialize call succeeds. | |
| 105 void Initialize(); | |
| 106 void OnInitialized(int32_t result); | |
| 107 | |
| 108 // ConfigureStreams sets up stream context in |streams_| and configure the | |
| 109 // streams with the camera HAL. OnConfiguredStreams updates |streams_| with | |
| 110 // the stream config returned, and allocates buffers as per |updated_config| | |
| 111 // indicates. If there's no error OnConfiguredStreams notifies | |
| 112 // |client_| the capture has started by calling OnStarted, and proceeds to | |
| 113 // ConstructDefaultRequestSettings. | |
| 114 void ConfigureStreams(); | |
| 115 void OnConfiguredStreams( | |
| 116 int32_t result, | |
| 117 arc::mojom::Camera3StreamConfigurationPtr updated_config); | |
| 118 | |
| 119 // ConstructDefaultRequestSettings asks the camera HAL for the default request | |
| 120 // settings of the stream in |stream_context_|. | |
| 121 // OnConstructedDefaultRequestSettings sets the request settings in | |
| 122 // |streams_context_|. If there's no error | |
| 123 // OnConstructedDefaultRequestSettings calls StartCapture to start the video | |
| 124 // capture loop. | |
| 125 void ConstructDefaultRequestSettings(); | |
| 126 void OnConstructedDefaultRequestSettings( | |
| 127 arc::mojom::CameraMetadataPtr settings); | |
| 128 | |
| 129 // StreamCaptureInterface implementations. These methods are called by | |
| 130 // |stream_buffer_manager_| on |ipc_task_runner_|. | |
| 131 void RegisterBuffer(uint64_t buffer_id, | |
| 132 arc::mojom::Camera3DeviceOps::BufferType type, | |
| 133 uint32_t drm_format, | |
| 134 arc::mojom::HalPixelFormat hal_pixel_format, | |
| 135 uint32_t width, | |
| 136 uint32_t height, | |
| 137 std::vector<StreamCaptureInterface::Plane> planes, | |
| 138 base::OnceCallback<void(int32_t)> callback); | |
| 139 void ProcessCaptureRequest(arc::mojom::Camera3CaptureRequestPtr request, | |
| 140 base::OnceCallback<void(int32_t)> callback); | |
| 141 | |
| 142 const VideoCaptureDeviceDescriptor device_descriptor_; | |
| 143 | |
| 144 int32_t camera_id_; | |
| 145 | |
| 146 const scoped_refptr<CameraHalDelegate> camera_hal_delegate_; | |
| 147 | |
| 148 VideoCaptureParams chrome_capture_params_; | |
| 149 | |
| 150 std::unique_ptr<CameraDeviceContext> device_context_; | |
| 151 | |
| 152 std::unique_ptr<StreamBufferManager> stream_buffer_manager_; | |
| 153 | |
| 154 // Stores the static camera characteristics of the camera device. E.g. the | |
| 155 // supported formats and resolution, various available exposure and apeture | |
| 156 // settings, etc. | |
| 157 arc::mojom::CameraMetadataPtr static_metadata_; | |
| 158 | |
| 159 arc::mojom::Camera3DeviceOpsPtr device_ops_; | |
| 160 | |
| 161 // Where all the Mojo IPC calls takes place. | |
| 162 const scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_; | |
| 163 | |
| 164 base::Closure device_close_callback_; | |
| 165 | |
| 166 base::WeakPtrFactory<CameraDeviceDelegate> weak_ptr_factory_; | |
| 167 | |
| 168 DISALLOW_IMPLICIT_CONSTRUCTORS(CameraDeviceDelegate); | |
| 169 }; | |
| 170 | |
| 171 } // namespace media | |
| 172 | |
| 173 #endif // MEDIA_CAPTURE_VIDEO_CHROMEOS_CAMERA_DEVICE_DELEGATE_H_ | |
| OLD | NEW |