Chromium Code Reviews| 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/synchronization/waitable_event.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 #include "mojo/public/cpp/bindings/binding.h" | |
| 16 | |
| 17 namespace media { | |
| 18 | |
| 19 class CameraHalDelegate; | |
| 20 | |
| 21 // CameraDeviceDelegate is instantiated on the capture thread where | |
| 22 // AllocateAndStart of VideoCaptureDeviceArcChromeOS runs on. All the methods | |
| 23 // in CameraDeviceDelegate run on |ipc_task_runner_| and hence all the | |
| 24 // access to member variables is sequenced. | |
| 25 // | |
| 26 // CameraDeviceDelegate is refcounted because it is bound to closures that run | |
| 27 // on the module thread of |hal_delegate_| and the device thread which is | |
| 28 // referenced by |ipc_task_runner_|. | |
| 29 class CAPTURE_EXPORT CameraDeviceDelegate final | |
| 30 : public base::RefCountedThreadSafe<CameraDeviceDelegate> { | |
| 31 public: | |
| 32 enum State { | |
| 33 // The camera device is completely stopped. This is the initial state, and | |
| 34 // is also set in OnClosed(). | |
| 35 kStopped, | |
| 36 | |
| 37 // The camera device is starting and waiting to be initialized. | |
| 38 // | |
| 39 // The kStarting state is set in AllocateAndStart(). | |
| 40 kStarting, | |
| 41 | |
| 42 // The camera device is initialized and can accept stream configuration | |
| 43 // requests. | |
| 44 // | |
| 45 // The state is transitioned to kInitialized through: | |
| 46 // | |
| 47 // |hal_delegate_|->GetCameraInfo() -> OnGotCameraInfo() -> | |
| 48 // |hal_delegate_|->OpenDevice() -> OnOpenedDevice() -> | |
| 49 // Initialize() -> OnInitialized() | |
| 50 kInitialized, | |
| 51 | |
| 52 // The various capture streams are configured and the camera device is ready | |
| 53 // to process capture requests. | |
| 54 // | |
| 55 // The state is transitioned to kStreamConfigured through: | |
| 56 // | |
| 57 // ConfigureStreams() -> OnConfiguredStreams() | |
| 58 kStreamConfigured, | |
| 59 | |
| 60 // The camera device is capturing video streams. | |
| 61 // | |
| 62 // The state is transitioned to kCapturing through: | |
| 63 // | |
| 64 // ConstructDefaultRequestSettings() -> | |
| 65 // OnConstructedDefaultRequestSettings() -> | |
| 66 // |stream_buffer_manager_|->StartCapture() | |
| 67 // | |
| 68 // In the kCapturing state the |stream_buffer_manager_| runs the capture | |
| 69 // loop to send capture requests and process capture results. | |
| 70 kCapturing, | |
| 71 | |
| 72 // When the camera device is in the kCapturing state, a capture loop is | |
| 73 // constantly running in |stream_buffer_manager_|: | |
| 74 // | |
| 75 // On the StreamBufferManager side, we register and submit a capture | |
| 76 // request whenever a free buffer is available: | |
| 77 // | |
| 78 // RegisterBuffer() -> OnRegisteredBuffer() -> | |
| 79 // ProcessCaptureRequest() -> OnProcessedCaptureRequest() | |
| 80 // | |
| 81 // We get various capture metadata and error notifications from the camera | |
| 82 // HAL through the following callbacks: | |
| 83 // | |
| 84 // ProcessCaptureResult() | |
| 85 // Notify() | |
| 86 | |
| 87 // The camera device is going through the shut down process; in order to | |
| 88 // avoid race conditions, no new Mojo messages may be sent to camera HAL in | |
| 89 // the kStopping state. | |
| 90 // | |
| 91 // The kStopping state is set in StopAndDeAllocate(). | |
| 92 kStopping, | |
| 93 | |
| 94 // The camera device encountered an unrecoverable error and needs to be | |
| 95 // StopAndDeAllocate()'d. | |
| 96 // | |
| 97 // The kError state is set in SetErrorState(). | |
| 98 kError, | |
| 99 }; | |
| 100 | |
| 101 // StreamBufferManager is responsible for managing the buffers of the | |
| 102 // stream. StreamBufferManager allocates buffers according to the given | |
| 103 // stream configuration, and circulates the buffers along with capture | |
| 104 // requests and results between Chrome and the camera HAL process. | |
| 105 class CAPTURE_EXPORT StreamBufferManager final | |
|
chfremer
2017/06/06 17:09:25
I recommend moving this class to its own files. I
jcliang
2017/06/07 08:18:39
Done. It was kept here to access some private meth
| |
| 106 : public base::RefCountedThreadSafe<StreamBufferManager>, | |
| 107 public arc::mojom::Camera3CallbackOps { | |
| 108 public: | |
| 109 StreamBufferManager( | |
| 110 arc::mojom::Camera3CallbackOpsRequest callback_ops_request, | |
| 111 scoped_refptr<CameraDeviceDelegate> camera_device_delegate, | |
| 112 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner); | |
| 113 | |
| 114 // Sets up the stream context and allocate buffers according to the | |
| 115 // configuration specified in |stream|. | |
| 116 void SetUpStreamAndBuffers(VideoCaptureFormat capture_format, | |
| 117 uint32_t partial_result_count, | |
| 118 arc::mojom::Camera3StreamPtr stream); | |
| 119 | |
| 120 // StartCapture is the entry point to starting the video capture. The way | |
| 121 // the video capture loop works is: | |
| 122 // | |
| 123 // (1) If there is a free buffer, RegisterBuffer registers the buffer with | |
| 124 // the camera HAL. | |
| 125 // (2) Once the free buffer is registered, ProcessCaptureRequest is called | |
| 126 // to issue a capture request which will eventually fill the registered | |
| 127 // buffer. Goto (1) to register the remaining free buffers. | |
| 128 // (3) The camera HAL returns the shutter time of a capture request through | |
| 129 // Notify, and the filled buffer through ProcessCaptureResult. | |
| 130 // (4) Once all the result metadata are collected, SubmitCaptureResult is | |
| 131 // called to deliver the filled buffer to Chrome. After the buffer is | |
| 132 // consumed by Chrome it is enqueued back to the free buffer queue. | |
| 133 // Goto (1) to start another capture loop. | |
| 134 void StartCapture(arc::mojom::CameraMetadataPtr settings); | |
| 135 | |
| 136 // Stops the capture loop. After StopCapture is called |callback_ops_| is | |
| 137 // unbound, so no new capture request or result will be processed. | |
| 138 void StopCapture(); | |
| 139 | |
| 140 private: | |
| 141 friend class base::RefCountedThreadSafe<StreamBufferManager>; | |
| 142 ~StreamBufferManager() = default; | |
| 143 | |
| 144 // Registers a free buffer, if any, to the camera HAL. | |
| 145 void RegisterBuffer(); | |
| 146 | |
| 147 // Calls ProcessCaptureRequest if the buffer specified by |buffer_id| is | |
| 148 // successfully registered. | |
| 149 void OnRegisteredBuffer(size_t buffer_id, int32_t result); | |
| 150 | |
| 151 // The capture request contains the buffer handle specified by |buffer_id|. | |
| 152 void ProcessCaptureRequest(size_t buffer_id); | |
| 153 // Calls RegisterBuffer to attempt to register any remaining free buffers. | |
| 154 void OnProcessedCaptureRequest(int32_t result); | |
| 155 | |
| 156 // Camera3CallbackOps implementations. | |
| 157 | |
| 158 // ProcessCaptureResult receives the result metadata as well as the filled | |
| 159 // buffer from camera HAL. The result metadata may be divided and delivered | |
| 160 // in several stages. Before all the result metadata is received the | |
| 161 // partial results are kept in |partial_results_|. | |
| 162 void ProcessCaptureResult(arc::mojom::Camera3CaptureResultPtr result) final; | |
| 163 | |
| 164 // Notify receives the shutter time of capture requests and various errors | |
| 165 // from camera HAL. The shutter time is used as the timestamp in the video | |
| 166 // frame delivered to Chrome. | |
| 167 void Notify(arc::mojom::Camera3NotifyMsgPtr message) final; | |
| 168 void HandleNotifyError(uint32_t frame_number, | |
| 169 uint64_t error_stream_id, | |
| 170 arc::mojom::Camera3ErrorMsgCode error_code); | |
| 171 | |
| 172 // Submits the captured buffer of frame |frame_number_| to Chrome, then | |
| 173 // enqueues the buffer to free buffer queue for the next capture request. | |
| 174 void SubmitCaptureResult(uint32_t frame_number); | |
| 175 | |
| 176 // A flag indicating whether the capture loops is running. | |
| 177 bool capturing_; | |
| 178 | |
| 179 // A reference to the CameraDeviceDelegate that owns this | |
| 180 // StreamBufferManager instance. | |
| 181 const scoped_refptr<CameraDeviceDelegate> camera_device_delegate_; | |
| 182 | |
| 183 // Mojo proxy and binding for the camera HAL device ops and callback ops | |
| 184 // interfaces. | |
| 185 mojo::Binding<arc::mojom::Camera3CallbackOps> callback_ops_; | |
| 186 | |
| 187 // Where all the Mojo IPC calls takes place. | |
| 188 const scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_; | |
| 189 | |
| 190 // The frame number. Increased by one for each capture request sent; reset | |
| 191 // to zero in AllocateAndStart. | |
| 192 uint32_t frame_number_; | |
| 193 | |
| 194 struct StreamContext { | |
| 195 // The actual pixel format used in the capture request. | |
| 196 VideoCaptureFormat capture_format; | |
| 197 // The camera HAL stream. | |
| 198 arc::mojom::Camera3StreamPtr stream; | |
| 199 // The request settings used in the capture request of this stream. | |
| 200 arc::mojom::CameraMetadataPtr request_settings; | |
| 201 // The allocated buffers of this stream. | |
| 202 std::vector<std::unique_ptr<base::SharedMemory>> buffers; | |
| 203 // The free buffers of this stream. The queue stores indices into the | |
| 204 // |buffers| vector. | |
| 205 std::queue<size_t> free_buffers; | |
| 206 }; | |
| 207 | |
| 208 // The stream context of the preview stream. | |
| 209 std::unique_ptr<StreamContext> stream_context_; | |
| 210 | |
| 211 // CaptureResult is used to hold the partial capture results for each frame. | |
| 212 struct CaptureResult { | |
| 213 CaptureResult() : metadata(arc::mojom::CameraMetadata::New()) {} | |
| 214 // |reference_time| and |timestamp| are derived from the shutter time of | |
| 215 // this frame. They are be passed to |client_->OnIncomingCapturedData| | |
| 216 // along with the |buffers| when the captured frame is submitted. | |
| 217 base::TimeTicks reference_time; | |
| 218 base::TimeDelta timestamp; | |
| 219 // The result metadata. Contains various information about the captured | |
| 220 // frame. | |
| 221 arc::mojom::CameraMetadataPtr metadata; | |
| 222 // The buffer handle that hold the captured data of this frame. | |
| 223 arc::mojom::Camera3StreamBufferPtr buffer; | |
| 224 // The set of the partial metadata received. For each capture result, the | |
| 225 // total number of partial metadata should equal to | |
| 226 // |partial_result_count_|. | |
| 227 std::set<uint32_t> partial_metadata_received; | |
| 228 }; | |
| 229 | |
| 230 // The number of partial stages. |partial_result_count_| is learned by | |
| 231 // querying |static_metadata_|. In case the result count is absent in | |
| 232 // |static_metadata_|, it defaults to one which means all the result | |
| 233 // metadata and captured buffer of a frame are returned together in one | |
| 234 // shot. | |
| 235 uint32_t partial_result_count_; | |
| 236 | |
| 237 // The shutter time of the first frame. We derive the |timestamp| of a | |
| 238 // frame using the difference between the frame's shutter time and | |
| 239 // |first_frame_shutter_time_|. | |
| 240 base::TimeTicks first_frame_shutter_time_; | |
| 241 | |
| 242 // Stores the partial capture results of the current in-flight frames. | |
| 243 std::map<uint32_t, CaptureResult> partial_results_; | |
| 244 | |
| 245 DISALLOW_IMPLICIT_CONSTRUCTORS(StreamBufferManager); | |
| 246 }; | |
| 247 | |
| 248 CameraDeviceDelegate( | |
| 249 VideoCaptureDeviceDescriptor device_descriptor, | |
| 250 scoped_refptr<CameraHalDelegate> camera_hal_delegate, | |
| 251 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner); | |
| 252 | |
| 253 // Delegation methods for the VideoCaptureDevice interface. | |
| 254 void AllocateAndStart(const VideoCaptureParams& params, | |
| 255 std::unique_ptr<VideoCaptureDevice::Client> client); | |
| 256 void StopAndDeAllocate(); | |
| 257 void TakePhoto(VideoCaptureDevice::TakePhotoCallback callback); | |
| 258 void GetPhotoCapabilities( | |
| 259 VideoCaptureDevice::GetPhotoCapabilitiesCallback callback); | |
| 260 void SetPhotoOptions(mojom::PhotoSettingsPtr settings, | |
| 261 VideoCaptureDevice::SetPhotoOptionsCallback callback); | |
| 262 | |
| 263 // Sets the frame rotation angle in |rotation_|. |rotation_| is clockwise | |
| 264 // rotation in degrees, and is passed to |client_| along with the captured | |
| 265 // frames. | |
| 266 void SetRotation(int rotation); | |
| 267 | |
| 268 private: | |
| 269 friend class CameraDeviceDelegateTest; | |
| 270 friend class base::RefCountedThreadSafe<CameraDeviceDelegate>; | |
| 271 ~CameraDeviceDelegate() = default; | |
| 272 | |
| 273 void SetState(State state); | |
| 274 | |
| 275 // Sets state to kError and call |client_->OnError| to tear down the | |
| 276 // VideoCaptureDevice. | |
| 277 void SetErrorState(const tracked_objects::Location& from_here, | |
| 278 const std::string& reason); | |
| 279 | |
| 280 // Logs |message| to |client_|. | |
| 281 void LogToClient(std::string message); | |
| 282 | |
| 283 // Submits the capture data to |client_->OnIncomingCapturedData|. | |
| 284 void SubmitCapturedData(const uint8_t* data, | |
| 285 int length, | |
| 286 const VideoCaptureFormat& frame_format, | |
| 287 base::TimeTicks reference_time, | |
| 288 base::TimeDelta timestamp); | |
| 289 | |
| 290 // Mojo connection error handler. | |
| 291 void OnMojoConnectionError(); | |
| 292 | |
| 293 // Callback method for the Close Mojo IPC call. This method resets the Mojo | |
| 294 // connection and closes the camera device. | |
| 295 void OnClosed(int32_t result); | |
| 296 | |
| 297 // Resets the Mojo interface and bindings. | |
| 298 void ResetMojoInterface(); | |
| 299 | |
| 300 // Sets |static_metadata_| from |camera_info|. | |
| 301 void OnGotCameraInfo(int32_t result, arc::mojom::CameraInfoPtr camera_info); | |
| 302 | |
| 303 // Creates the Mojo connection to the camera device. | |
| 304 void OnOpenedDevice(int32_t result); | |
| 305 | |
| 306 // Initializes the camera HAL. Initialize sets up the Camera3CallbackOps with | |
| 307 // the camera HAL. OnInitialized continues to ConfigureStreams if the | |
| 308 // Initialize call succeeds. | |
| 309 void Initialize(); | |
| 310 void OnInitialized(int32_t result); | |
| 311 | |
| 312 // ConfigureStreams sets up stream context in |streams_| and configure the | |
| 313 // streams with the camera HAL. OnConfiguredStreams updates |streams_| with | |
| 314 // the stream config returned, and allocates buffers as per |updated_config| | |
| 315 // indicates. If there's no error OnConfiguredStreams notifies | |
| 316 // |client_| the capture has started by calling OnStarted, and proceeds to | |
| 317 // ConstructDefaultRequestSettings. | |
| 318 void ConfigureStreams(); | |
| 319 void OnConfiguredStreams( | |
| 320 int32_t result, | |
| 321 arc::mojom::Camera3StreamConfigurationPtr updated_config); | |
| 322 | |
| 323 // ConstructDefaultRequestSettings asks the camera HAL for the default request | |
| 324 // settings of the stream in |stream_context_|. | |
| 325 // OnConstructedDefaultRequestSettings sets the request settings in | |
| 326 // |streams_context_|. If there's no error | |
| 327 // OnConstructedDefaultRequestSettings calls StartCapture to start the video | |
| 328 // capture loop. | |
| 329 void ConstructDefaultRequestSettings(); | |
| 330 void OnConstructedDefaultRequestSettings( | |
| 331 arc::mojom::CameraMetadataPtr settings); | |
| 332 | |
| 333 // Registers a buffer to the camera HAL. This method is called by | |
| 334 // |stream_buffer_manager_| in the capture loop. | |
| 335 void RegisterBuffer(uint64_t buffer_id, | |
| 336 arc::mojom::Camera3DeviceOps::BufferType type, | |
| 337 std::vector<mojo::ScopedHandle> fds, | |
| 338 uint32_t drm_format, | |
| 339 arc::mojom::HalPixelFormat hal_pixel_format, | |
| 340 uint32_t width, | |
| 341 uint32_t height, | |
| 342 std::vector<uint32_t> strides, | |
| 343 std::vector<uint32_t> offsets, | |
| 344 base::OnceCallback<void(int32_t)> callback); | |
| 345 | |
| 346 // Sends a capture request to the camera HAL. This method is called by | |
| 347 // |stream_buffer_manager_| in the capture loop. | |
| 348 void ProcessCaptureRequest(arc::mojom::Camera3CaptureRequestPtr request, | |
| 349 base::OnceCallback<void(int32_t)> callback); | |
| 350 | |
| 351 scoped_refptr<StreamBufferManager> stream_buffer_manager_; | |
| 352 | |
| 353 const VideoCaptureDeviceDescriptor device_descriptor_; | |
| 354 const scoped_refptr<CameraHalDelegate> camera_hal_delegate_; | |
| 355 | |
| 356 VideoCaptureParams chrome_capture_params_; | |
| 357 std::unique_ptr<VideoCaptureDevice::Client> client_; | |
| 358 | |
| 359 // Stores the static camera characteristics of the camera device. | |
| 360 arc::mojom::CameraMetadataPtr static_metadata_; | |
| 361 | |
| 362 // Clockwise rotation in degrees. This value should be 0, 90, 180, or 270. | |
| 363 int rotation_; | |
| 364 | |
| 365 // The state the CameraDeviceDelegate currently is in. | |
| 366 State state_; | |
| 367 | |
| 368 // Mojo proxy and binding for the camera HAL device ops and callback ops | |
| 369 // interfaces. | |
| 370 arc::mojom::Camera3DeviceOpsPtr device_ops_; | |
| 371 | |
| 372 // Where all the Mojo IPC calls takes place. | |
| 373 const scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_; | |
| 374 | |
| 375 DISALLOW_IMPLICIT_CONSTRUCTORS(CameraDeviceDelegate); | |
| 376 }; | |
| 377 | |
| 378 } // namespace media | |
| 379 | |
| 380 #endif // MEDIA_CAPTURE_VIDEO_CHROMEOS_CAMERA_DEVICE_DELEGATE_H_ | |
| OLD | NEW |