| 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_STREAM_BUFFER_MANAGER_H_ |
| 6 #define MEDIA_CAPTURE_VIDEO_CHROMEOS_STREAM_BUFFER_MANAGER_H_ |
| 7 |
| 8 #include "base/memory/shared_memory.h" |
| 9 #include "media/capture/video/chromeos/mojo/arc_camera3.mojom.h" |
| 10 #include "media/capture/video_capture_types.h" |
| 11 #include "mojo/public/cpp/bindings/binding.h" |
| 12 |
| 13 namespace media { |
| 14 |
| 15 class CameraDeviceContext; |
| 16 class CameraDeviceDelegate; |
| 17 |
| 18 // StreamBufferManager is responsible for managing the buffers of the |
| 19 // stream. StreamBufferManager allocates buffers according to the given |
| 20 // stream configuration, and circulates the buffers along with capture |
| 21 // requests and results between Chrome and the camera HAL process. |
| 22 class CAPTURE_EXPORT StreamBufferManager final |
| 23 : public base::RefCountedThreadSafe<StreamBufferManager>, |
| 24 public arc::mojom::Camera3CallbackOps { |
| 25 public: |
| 26 StreamBufferManager( |
| 27 arc::mojom::Camera3CallbackOpsRequest callback_ops_request, |
| 28 scoped_refptr<CameraDeviceDelegate> camera_device_delegate, |
| 29 CameraDeviceContext* device_context, |
| 30 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner); |
| 31 |
| 32 // Sets up the stream context and allocate buffers according to the |
| 33 // configuration specified in |stream|. |
| 34 void SetUpStreamAndBuffers(VideoCaptureFormat capture_format, |
| 35 uint32_t partial_result_count, |
| 36 arc::mojom::Camera3StreamPtr stream); |
| 37 |
| 38 // StartCapture is the entry point to starting the video capture. The way |
| 39 // the video capture loop works is: |
| 40 // |
| 41 // (1) If there is a free buffer, RegisterBuffer registers the buffer with |
| 42 // the camera HAL. |
| 43 // (2) Once the free buffer is registered, ProcessCaptureRequest is called |
| 44 // to issue a capture request which will eventually fill the registered |
| 45 // buffer. Goto (1) to register the remaining free buffers. |
| 46 // (3) The camera HAL returns the shutter time of a capture request through |
| 47 // Notify, and the filled buffer through ProcessCaptureResult. |
| 48 // (4) Once all the result metadata are collected, SubmitCaptureResult is |
| 49 // called to deliver the filled buffer to Chrome. After the buffer is |
| 50 // consumed by Chrome it is enqueued back to the free buffer queue. |
| 51 // Goto (1) to start another capture loop. |
| 52 void StartCapture(arc::mojom::CameraMetadataPtr settings); |
| 53 |
| 54 // Stops the capture loop. After StopCapture is called |callback_ops_| is |
| 55 // unbound, so no new capture request or result will be processed. |
| 56 void StopCapture(); |
| 57 |
| 58 private: |
| 59 friend class base::RefCountedThreadSafe<StreamBufferManager>; |
| 60 ~StreamBufferManager() = default; |
| 61 |
| 62 // Registers a free buffer, if any, to the camera HAL. |
| 63 void RegisterBuffer(); |
| 64 |
| 65 // Calls ProcessCaptureRequest if the buffer specified by |buffer_id| is |
| 66 // successfully registered. |
| 67 void OnRegisteredBuffer(size_t buffer_id, int32_t result); |
| 68 |
| 69 // The capture request contains the buffer handle specified by |buffer_id|. |
| 70 void ProcessCaptureRequest(size_t buffer_id); |
| 71 // Calls RegisterBuffer to attempt to register any remaining free buffers. |
| 72 void OnProcessedCaptureRequest(int32_t result); |
| 73 |
| 74 // Camera3CallbackOps implementations. |
| 75 |
| 76 // ProcessCaptureResult receives the result metadata as well as the filled |
| 77 // buffer from camera HAL. The result metadata may be divided and delivered |
| 78 // in several stages. Before all the result metadata is received the |
| 79 // partial results are kept in |partial_results_|. |
| 80 void ProcessCaptureResult(arc::mojom::Camera3CaptureResultPtr result) final; |
| 81 |
| 82 // Notify receives the shutter time of capture requests and various errors |
| 83 // from camera HAL. The shutter time is used as the timestamp in the video |
| 84 // frame delivered to Chrome. |
| 85 void Notify(arc::mojom::Camera3NotifyMsgPtr message) final; |
| 86 void HandleNotifyError(uint32_t frame_number, |
| 87 uint64_t error_stream_id, |
| 88 arc::mojom::Camera3ErrorMsgCode error_code); |
| 89 |
| 90 // Submits the captured buffer of frame |frame_number_| to Chrome, then |
| 91 // enqueues the buffer to free buffer queue for the next capture request. |
| 92 void SubmitCaptureResult(uint32_t frame_number); |
| 93 |
| 94 // Mojo proxy and binding for the camera HAL device ops and callback ops |
| 95 // interfaces. |
| 96 mojo::Binding<arc::mojom::Camera3CallbackOps> callback_ops_; |
| 97 |
| 98 // A reference to the CameraDeviceDelegate that owns this |
| 99 // StreamBufferManager instance. |
| 100 const scoped_refptr<CameraDeviceDelegate> camera_device_delegate_; |
| 101 |
| 102 CameraDeviceContext* device_context_; |
| 103 |
| 104 // Where all the Mojo IPC calls takes place. |
| 105 const scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_; |
| 106 |
| 107 // A flag indicating whether the capture loops is running. |
| 108 bool capturing_; |
| 109 |
| 110 // The frame number. Increased by one for each capture request sent; reset |
| 111 // to zero in AllocateAndStart. |
| 112 uint32_t frame_number_; |
| 113 |
| 114 struct StreamContext { |
| 115 // The actual pixel format used in the capture request. |
| 116 VideoCaptureFormat capture_format; |
| 117 // The camera HAL stream. |
| 118 arc::mojom::Camera3StreamPtr stream; |
| 119 // The request settings used in the capture request of this stream. |
| 120 arc::mojom::CameraMetadataPtr request_settings; |
| 121 // The allocated buffers of this stream. |
| 122 std::vector<std::unique_ptr<base::SharedMemory>> buffers; |
| 123 // The free buffers of this stream. The queue stores indices into the |
| 124 // |buffers| vector. |
| 125 std::queue<size_t> free_buffers; |
| 126 }; |
| 127 |
| 128 // The stream context of the preview stream. |
| 129 std::unique_ptr<StreamContext> stream_context_; |
| 130 |
| 131 // CaptureResult is used to hold the partial capture results for each frame. |
| 132 struct CaptureResult { |
| 133 CaptureResult() : metadata(arc::mojom::CameraMetadata::New()) {} |
| 134 // |reference_time| and |timestamp| are derived from the shutter time of |
| 135 // this frame. They are be passed to |client_->OnIncomingCapturedData| |
| 136 // along with the |buffers| when the captured frame is submitted. |
| 137 base::TimeTicks reference_time; |
| 138 base::TimeDelta timestamp; |
| 139 // The result metadata. Contains various information about the captured |
| 140 // frame. |
| 141 arc::mojom::CameraMetadataPtr metadata; |
| 142 // The buffer handle that hold the captured data of this frame. |
| 143 arc::mojom::Camera3StreamBufferPtr buffer; |
| 144 // The set of the partial metadata received. For each capture result, the |
| 145 // total number of partial metadata should equal to |
| 146 // |partial_result_count_|. |
| 147 std::set<uint32_t> partial_metadata_received; |
| 148 }; |
| 149 |
| 150 // The number of partial stages. |partial_result_count_| is learned by |
| 151 // querying |static_metadata_|. In case the result count is absent in |
| 152 // |static_metadata_|, it defaults to one which means all the result |
| 153 // metadata and captured buffer of a frame are returned together in one |
| 154 // shot. |
| 155 uint32_t partial_result_count_; |
| 156 |
| 157 // The shutter time of the first frame. We derive the |timestamp| of a |
| 158 // frame using the difference between the frame's shutter time and |
| 159 // |first_frame_shutter_time_|. |
| 160 base::TimeTicks first_frame_shutter_time_; |
| 161 |
| 162 // Stores the partial capture results of the current in-flight frames. |
| 163 std::map<uint32_t, CaptureResult> partial_results_; |
| 164 |
| 165 DISALLOW_IMPLICIT_CONSTRUCTORS(StreamBufferManager); |
| 166 }; |
| 167 |
| 168 } // namespace media |
| 169 |
| 170 #endif // MEDIA_CAPTURE_VIDEO_CHROMEOS_STREAM_BUFFER_MANAGER_H_ |
| OLD | NEW |