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