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