Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(225)

Side by Side Diff: media/capture/video/chromeos/camera_device_delegate.h

Issue 2837273004: media: add video capture device for ARC++ camera HAL v3 (Closed)
Patch Set: address chfremer's comments Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 <map>
9 #include <memory>
10 #include <set>
11 #include <vector>
12
13 #include "base/macros.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "media/capture/video/chromeos/mojo/arc_camera3.mojom.h"
16 #include "media/capture/video/video_capture_device.h"
17 #include "media/capture/video_capture_types.h"
18 #include "mojo/public/cpp/bindings/binding.h"
19
20 namespace media {
21
22 class CameraHalDelegate;
23
24 // CameraDeviceDelegate is instantiated on the capture thread where
25 // AllocateAndStart of VideoCaptureDeviceArcChromeOS runs on. All the methods
26 // in CameraDeviceDelegate run on |ipc_task_runner_| and hence all the
27 // access to member variables is sequenced.
28 //
29 // CameraDeviceDelegate is refcounted because it is bound to closures that run
30 // on the module thread of |hal_delegate_| and the device thread which is
31 // referenced by |ipc_task_runner_|.
32 class CAPTURE_EXPORT CameraDeviceDelegate final
33 : public base::RefCountedThreadSafe<CameraDeviceDelegate>,
34 public arc::mojom::Camera3CallbackOps {
35 public:
36 enum State {
chfremer 2017/06/01 17:16:48 I don't see any public API of this class that invo
jcliang 2017/06/06 16:04:29 With the new test cases I added in camera_device_d
37 // The camera device is completely stopped. This is the initial state, and
38 // is also set in OnClosed().
39 kStopped,
40
41 // The camera device is starting and waiting to be initialized.
42 //
43 // The kStarting state is set in AllocateAndStart().
44 kStarting,
45
46 // The camera device is initialized and can accept stream configuration
47 // requests.
48 //
49 // The state is transitioned to kInitialized through:
50 //
51 // |hal_delegate_|->GetCameraInfo() -> OnGotCameraInfo() ->
52 // |hal_delegate_|->OpenDevice() -> OnOpenedDevice() ->
53 // Initialize() -> OnInitialized()
54 kInitialized,
55
56 // The various capture streams are configured and the camera device is ready
57 // to process capture requests.
chfremer 2017/06/01 17:16:48 For a reader unfamiliar with the larger context (i
jcliang 2017/06/06 16:04:29 A capture stream is a set of parameters (width, he
jcliang 2017/06/06 16:09:23 Oops, I should have deleted the last paragraph. Th
chfremer 2017/06/06 17:09:25 If I understand correctly, the situation is as fol
jcliang 2017/06/07 08:18:39 This is a great suggestion! I've extracted the sta
58 //
59 // The state is transitioned to kStreamConfigured through:
60 //
61 // ConfigureStreams() -> OnConfiguredStreams() ->
62 // ConstructDefaultRequestSettings() ->
63 // OnConstructedDefaultRequestSettings()
64 kStreamConfigured,
65
66 // The camera device is capturing video streams.
67 //
68 // The kCapturing state is set in StartCapture().
69 kCapturing,
70
71 // When the camera device is in the kCapturing state, a capture loop is
72 // constantly running:
73 //
74 // On the CameraDeviceDelegate side, we register and submit a capture
75 // request whenever a free buffer is available:
76 //
77 // RegisterBuffer() -> OnRegisteredBuffer() ->
78 // ProcessCaptureRequest() -> OnProcessedCaptureRequest()
79 //
80 // We get various capture metadata and error notifications from the camera
81 // HAL through the following callbacks:
82 //
83 // ProcessCaptureResult()
84 // Notify()
85
86 // The camera device is going through the shut down process; in order to
87 // avoid race conditions, no new Mojo messages may be sent to camera HAL in
88 // the kStopping state.
89 //
90 // The kStopping state is set in StopAndDeAllocate().
91 kStopping,
92
93 // The camera device encountered an unrecoverable error and needs to be
94 // StopAndDeAllocate()'d.
95 //
96 // The kError state is set in SetErrorState().
97 kError,
98 };
99
100 CameraDeviceDelegate(
101 VideoCaptureDeviceDescriptor device_descriptor,
102 scoped_refptr<CameraHalDelegate> camera_hal_delegate,
103 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner);
104
105 // Delegation methods for the VideoCaptureDevice interface.
106 void AllocateAndStart(const VideoCaptureParams& params,
107 std::unique_ptr<VideoCaptureDevice::Client> client);
108 void StopAndDeAllocate();
109 void TakePhoto(VideoCaptureDevice::TakePhotoCallback callback);
110 void GetPhotoCapabilities(
111 VideoCaptureDevice::GetPhotoCapabilitiesCallback callback);
112 void SetPhotoOptions(mojom::PhotoSettingsPtr settings,
113 VideoCaptureDevice::SetPhotoOptionsCallback callback);
114
115 // Sets the frame rotation angle in |rotation_|. |rotation_| is clockwise
116 // rotation in degrees, and is passed to |client_| along with the captured
117 // frames.
118 void SetRotation(int rotation);
119
120 private:
121 friend class base::RefCountedThreadSafe<CameraDeviceDelegate>;
122
123 ~CameraDeviceDelegate() = default;
124
125 // Sets the state.
chfremer 2017/06/01 17:16:49 This comment is redundant. I recommend only using
jcliang 2017/06/06 16:04:29 Done.
126 void SetState(State state);
127
128 // Sets state to kError and call |client_->OnError| to tear down the
129 // VideoCaptureDevice.
130 void SetErrorState(const tracked_objects::Location& from_here,
131 const std::string& reason);
132
133 // Resets the Mojo interface and bindings.
134 void ResetMojoInterface();
135
136 // Mojo connection error handler.
137 void OnMojoConnectionError();
138
139 // Callback method for the Close Mojo IPC call. This method resets the Mojo
140 // connection and closes the camera device.
141 void OnClosed(int32_t result);
142
143 // This method run on |module_task_runner_| of |camera_hal_delegate_|.
144 void OnGotCameraInfoOnModuleDelegate(int32_t result,
145 arc::mojom::CameraInfoPtr camera_info);
146 // Sets |static_metadata_| from |camera_info|.
147 void OnGotCameraInfo(int32_t result, arc::mojom::CameraInfoPtr camera_info);
148
149 // This method run on |module_task_runner_| of |camera_hal_delegate_|.
150 void OnOpenedDeviceOnModuleDelegate(int32_t result);
151 // Creates the Mojo connection to the camera device.
152 void OnOpenedDevice(int32_t result);
153
154 // Initializes the camera HAL. Initialize sets up the Camera3CallbackOps with
155 // the camera HAL. OnInitialized continues to ConfigureStreams if the
156 // Initialize call succeeds.
157 void Initialize();
158 void OnInitialized(int32_t result);
159
160 // ConfigureStreams sets up stream context in |streams_| and configure the
161 // streams with the camera HAL. OnConfiguredStreams updates |streams_| with
162 // the stream config returned, and allocates buffers as per |updated_config|
163 // indicates. If there's no error OnConfiguredStreams notifies
164 // |client_| the capture has started by calling OnStarted, and proceeds to
165 // ConstructDefaultRequestSettings.
166 void ConfigureStreams();
167 void OnConfiguredStreams(
168 int32_t result,
169 arc::mojom::Camera3StreamConfigurationPtr updated_config);
170
171 // ConstructDefaultRequestSettings asks the camera HAL for the default request
172 // settings of the stream in |stream_context_|.
173 // OnConstructedDefaultRequestSettings sets the request settings in
174 // |streams_context_|. If there's no error
175 // OnConstructedDefaultRequestSettings calls StartCapture to start the video
176 // capture loop.
177 void ConstructDefaultRequestSettings();
178 void OnConstructedDefaultRequestSettings(
179 arc::mojom::CameraMetadataPtr settings);
180
181 // StartCapture is the entry point to starting the video capture. The way the
182 // video capture loop works is:
183 //
184 // (1) If there is a free buffer, RegisterBuffer registers the buffer with
185 // the camera HAL.
186 // (2) Once the free buffer is registered, ProcessCaptureRequest is called to
187 // issue a capture request which will eventually fill the registered
188 // buffer. Goto (1) to register the remaining free buffers.
189 // (3) The camera HAL returns shutter time of a capture request through
190 // Notify, and the filled buffer through ProcessCaptureResult.
191 // (4) Once all the result metadata are collected, SubmitCaptureResult is
192 // called to deliver the filled buffer to Chrome. After the buffer is
193 // consumed by Chrome it is enqueued back to the free buffer queue.
194 // Goto (1) to start another capture loop.
195 void StartCapture();
196
197 // Registers a free buffer, if any, to the camera HAL.
198 void RegisterBuffer();
199
200 // Calls ProcessCaptureRequest if the buffer indicated by |buffer_id| is
201 // successfully registered.
202 void OnRegisteredBuffer(size_t buffer_id, int32_t result);
203
204 // The capture request contains the buffer handle indicated by |buffer_id|.
205 void ProcessCaptureRequest(size_t buffer_id);
206 // Calls RegisterBuffer to attempt to register any remaining free buffers.
207 void OnProcessedCaptureRequest(int32_t result);
208
209 // Camera3CallbackOps implementations.
210
211 // ProcessCaptureResult receives the result metadata as well as the filled
212 // buffer from camera HAL. The result metadata may be divided and delivered
213 // in several stages. Before all the result metadata is received the partial
214 // results are kept in |partial_results_|.
215 void ProcessCaptureResult(arc::mojom::Camera3CaptureResultPtr result) final;
216
217 // Notify receives the shutter time of capture requests and various errors
218 // from camera HAL. The shutter time is used as the timestamp in the video
219 // frame delivered to Chrome.
220 void Notify(arc::mojom::Camera3NotifyMsgPtr message) final;
221 void HandleNotifyError(uint32_t frame_number,
222 uint64_t error_stream_id,
223 arc::mojom::Camera3ErrorMsgCode error_code);
224
225 // Submits the captured buffer of frame |frame_number_| to Chrome, then
226 // enqueues the buffer to free buffer queue for the next capture request.
227 void SubmitCaptureResult(uint32_t frame_number);
228
chfremer 2017/06/01 00:16:27 This class is quite big and has a large number of
jcliang 2017/06/01 17:11:17 I've split the buffer allocation and circulation i
chfremer 2017/06/06 17:09:25 Thanks. That is definitely an improvement. From my
229 const VideoCaptureDeviceDescriptor device_descriptor_;
230 const scoped_refptr<CameraHalDelegate> camera_hal_delegate_;
231
232 VideoCaptureParams chrome_capture_params_;
233 std::unique_ptr<VideoCaptureDevice::Client> client_;
234
235 // Stores the static camera characteristics of the camera device.
236 arc::mojom::CameraMetadataPtr static_metadata_;
237
238 // The state the CameraDeviceDelegate currently is in.
239 State state_;
240
241 // Clockwise rotation in degrees. This value should be 0, 90, 180, or 270.
242 int rotation_;
243
244 // Mojo proxy and binding for the camera HAL device ops and callback ops
245 // interfaces.
246 arc::mojom::Camera3DeviceOpsPtr device_ops_;
247 mojo::Binding<arc::mojom::Camera3CallbackOps> callback_ops_;
248
249 // Where all the Mojo IPC calls takes place.
250 const scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_;
251
252 // The frame number. Increased by one for each capture request sent; reset to
253 // zero in AllocateAndStart.
254 uint32_t frame_number_;
255
256 struct StreamContext {
257 // The actual pixel format used in the capture request.
258 VideoCaptureFormat capture_format;
259 // The camera HAL stream.
260 arc::mojom::Camera3StreamPtr stream;
261 // The request settings used in the capture request of this stream.
262 arc::mojom::CameraMetadataPtr request_settings;
263 // The allocated buffers of this stream.
264 std::vector<std::unique_ptr<base::SharedMemory>> buffers;
265 // The free buffers of this stream. The queue stores indices into the
266 // |buffers| vector.
267 std::queue<size_t> free_buffers;
268 };
269
270 // The stream context of the preview stream.
271 std::unique_ptr<StreamContext> stream_context_;
272
273 // CaptureResult is used to hold the partial capture results for each frame.
274 struct CaptureResult {
275 CaptureResult() : metadata(arc::mojom::CameraMetadata::New()) {}
276 // |reference_time| and |timestamp| are derived from the shutter time of
277 // this frame. They are be passed to |client_->OnIncomingCapturedData|
278 // along with the |buffers| when the captured frame is submitted.
279 base::TimeTicks reference_time;
280 base::TimeDelta timestamp;
281 // The result metadata. Contains various information about the captured
282 // frame.
283 arc::mojom::CameraMetadataPtr metadata;
284 // The buffer handle that hold the captured data of this frame.
285 arc::mojom::Camera3StreamBufferPtr buffer;
286 // The set of the partial metadata received. For each capture result, the
287 // total number of partial metadata should equal to |partial_result_count_|.
288 std::set<uint32_t> partial_metadata_received;
289 };
290
291 // The number of partial stages. |partial_result_count_| is learned by
292 // querying |static_metadata_|. In case the result count is absent in
293 // |static_metadata_|, it defaults to one which means all the result metadata
294 // and captured buffer of a frame are returned together in one shot.
295 uint32_t partial_result_count_;
296
297 // The shutter time of the first frame. We derive the |timestamp| of a frame
298 // using the difference between the frame's shutter time and
299 // |first_frame_shutter_time_|.
300 base::TimeTicks first_frame_shutter_time_;
301
302 // Stores the partial capture results of the current in-flight frames.
303 std::map<uint32_t, CaptureResult> partial_results_;
304
305 DISALLOW_IMPLICIT_CONSTRUCTORS(CameraDeviceDelegate);
306 };
307
308 } // namespace media
309
310 #endif // MEDIA_CAPTURE_VIDEO_CHROMEOS_CAMERA_DEVICE_DELEGATE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698