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

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 wuchengli@'s comments Created 3 years, 7 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.
wuchengli 2017/05/23 04:29:07 Document why we need this to be reference counted.
jcliang 2017/05/25 14:23:46 Done.
28 class CAPTURE_EXPORT CameraDeviceDelegate final
29 : public base::RefCountedThreadSafe<CameraDeviceDelegate>,
30 public arc::mojom::Camera3CallbackOps {
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().
wuchengli 2017/05/23 04:29:07 As discussed, add some documentation.
jcliang 2017/05/25 14:23:46 Done.
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 // Initialize() -> OnInitialized()
48 kInitialized,
49
50 // The various capture streams are configured and the camera device is ready
51 // to process capture requests.
52 //
53 // The state is transitioned to kStreamConfigured through:
54 //
55 // ConfigureStreams() -> OnConfiguredStreams() ->
56 // ConstructDefaultRequestSettings() ->
57 // OnConstructedDefaultRequestSettings()
58 kStreamConfigured,
59
60 // The camera device is capturing video streams.
61 //
62 // The kCapturing state is set in StartCapture().
63 kCapturing,
64
65 // When the camera device is in the kCapturing state, a capture loop is
66 // constantly running:
67 //
68 // On the CameraDeviceDelegate side, we register and submit a capture
69 // request whenever a free buffer is available:
70 //
71 // RegisterBuffer() -> OnRegisteredBuffer() ->
72 // ProcessCaptureRequest() -> OnProcessedCaptureRequest()
73 //
74 // We get various capture metadata and error notifications from the camera
75 // HAL through the following callbacks:
76 //
77 // ProcessCaptureResult()
78 // Notify()
79
80 // The camera device is going through the shut down process; in order to
81 // avoid race conditions, no new Mojo messages may be sent to camera HAL in
82 // the kStopping state.
83 //
84 // The kStopping state is set in StopAndDeAllocate().
85 kStopping,
86
87 // The camera device encountered an unrecoverable error and needs to be
88 // StopAndDeAllocate()'d.
89 //
90 // The kError state is set in SetErrorState().
91 kError,
92 };
93
94 CameraDeviceDelegate(
95 VideoCaptureDeviceDescriptor device_descriptor,
96 scoped_refptr<CameraHalDelegate> camera_hal_delegate,
97 const scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner);
98
99 // Converts the HAL pixel format |from| to Chromium pixel format. Returns
100 // PIXEL_FORMAT_UNKNOWN if |from| is not supported.
101 static VideoPixelFormat PixFormatHalToChromium(
102 arc::mojom::HalPixelFormat from);
103
104 // Converts the Chromium pixel format |from| to DRM pixel format. Returns 0
105 // if |from| is not supported.
106 static uint32_t PixFormatChromiumToDrm(VideoPixelFormat from);
107
108 // Delegation methods for the VideoCaptureDevice interface.
109 void AllocateAndStart(const VideoCaptureParams& params,
110 std::unique_ptr<VideoCaptureDevice::Client> client);
111 void StopAndDeAllocate();
112 void TakePhoto(VideoCaptureDevice::TakePhotoCallback callback);
113 void GetPhotoCapabilities(
114 VideoCaptureDevice::GetPhotoCapabilitiesCallback callback);
115 void SetPhotoOptions(mojom::PhotoSettingsPtr settings,
116 VideoCaptureDevice::SetPhotoOptionsCallback callback);
117
118 // Sets the frame rotation angle in |rotation_|. |rotation_| is clockwise
119 // rotation in degrees, and is passed to |client_| along with the captured
120 // frames.
121 void SetRotation(int rotation);
122
123 private:
124 friend class base::RefCountedThreadSafe<CameraDeviceDelegate>;
125
126 ~CameraDeviceDelegate() = default;
127
128 // Sets the state.
129 void SetState(State state);
130
131 // Sets state to kError and call |client_->OnError| to tear down the
132 // VideoCaptureDevice.
133 void SetErrorState(const tracked_objects::Location& from_here,
134 const std::string& reason);
135
136 // Resets the Mojo interface and bindings.
137 void ResetMojoInterface();
138
139 // Mojo connection error handler.
140 void OnMojoConnectionError();
141
142 // Callback method for the Close Mojo IPC call. This method resets the Mojo
143 // connection and closes the camera device.
144 void OnClosed(int32_t result);
145
146 // This method run on |module_task_runner_| of |camera_hal_delegate_|.
147 void OnGotCameraInfoOnModuleDelegate(int32_t result,
148 arc::mojom::CameraInfoPtr camera_info);
149 // Sets |static_metadata_| from |camera_info|.
150 void OnGotCameraInfo(int32_t result, arc::mojom::CameraInfoPtr camera_info);
151
152 // This method run on |module_task_runner_| of |camera_hal_delegate_|.
153 void OnOpenedDeviceOnModuleDelegate(
154 int32_t result,
155 arc::mojom::Camera3DeviceOpsPtr device_ops);
156 // Creates the Mojo connection to the camera device.
157 void OnOpenedDevice(
158 int32_t result,
159 mojo::InterfacePtrInfo<arc::mojom::Camera3DeviceOps> device_ops_info);
160
161 // Initializes the camera HAL. Initialize sets up the Camera3CallbackOps with
162 // the camera HAL. OnInitialized continues to ConfigureStreams if the
163 // Initialize call succeeds.
164 void Initialize();
165 void OnInitialized(int32_t result);
166
167 // ConfigureStreams sets up stream context in |streams_| and configure the
168 // streams with the camera HAL. OnConfiguredStreams updates |streams_| with
169 // the stream config returned, and allocates buffers as per |updated_config|
170 // indicates. If there's no error OnConfiguredStreams notifies
171 // |client_| the capture has started by calling OnStarted, and proceeds to
172 // ConstructDefaultRequestSettings.
173 void ConfigureStreams();
174 void OnConfiguredStreams(
175 int32_t result,
176 arc::mojom::Camera3StreamConfigurationPtr updated_config);
177
178 // ConstructDefaultRequestSettings asks the camera HAL for the default request
179 // settings of the stream in |stream_context_|.
180 // OnConstructedDefaultRequestSettings sets the request settings in
181 // |streams_context_|. If there's no error
182 // OnConstructedDefaultRequestSettings calls StartCapture to start the video
183 // capture loop.
184 void ConstructDefaultRequestSettings();
185 void OnConstructedDefaultRequestSettings(
186 arc::mojom::CameraMetadataPtr settings);
187
188 // StartCapture is the entry point to starting the video capture. The way the
189 // video capture loop works is:
190 //
191 // (1) If there is a free buffer, RegisterBuffer registers the buffer with
192 // the camera HAL.
193 // (2) Once the free buffer is registered, ProcessCaptureRequest is called to
194 // issue a capture request which will eventually fill the registered
195 // buffer. Goto (1) to register the remaining free buffers.
196 // (3) The camera HAL returns shutter time of a capture request through
197 // Notify, and the filled buffer through ProcessCaptureResult.
198 // (4) Once all the result metadata are collected, SubmitCaptureResult is
199 // called to deliver the filled buffer to Chrome. After the buffer is
200 // consumed by Chrome it is enqueued back to the free buffer queue.
201 // Goto (1) to start another capture loop.
202 void StartCapture();
203
204 // Registers a free buffer, if any, to the camera HAL.
205 void RegisterBuffer();
206
207 // Calls ProcessCaptureRequest if the buffer indicated by |buffer_id| is
208 // successfully registered.
209 void OnRegisteredBuffer(size_t buffer_id, int32_t result);
210
211 // The capture request contains the buffer handle indicated by |buffer_id|.
212 void ProcessCaptureRequest(size_t buffer_id);
213 // Calls RegisterBuffer to attempt to register any remaining free buffers.
214 void OnProcessedCaptureRequest(int32_t result);
215
216 // Camera3CallbackOps implementations.
217
218 // ProcessCaptureResult receives the result metadata as well as the filled
219 // buffer from camera HAL. The result metadata may be divided and delivered
220 // in several stages. Before all the result metadata is received the partial
221 // results are kept in |partial_results_|.
222 void ProcessCaptureResult(arc::mojom::Camera3CaptureResultPtr result) final;
223
224 // Notify receives the shutter time of capture requests and various errors
225 // from camera HAL. The shutter time is used as the timestamp in the video
226 // frame delivered to Chrome.
227 void Notify(arc::mojom::Camera3NotifyMsgPtr message) final;
228
229 // Submits the captured buffer of frame |frame_number_| to Chrome, then
230 // enqueues the buffer to free buffer queue for the next capture request.
231 void SubmitCaptureResult(uint32_t frame_number);
232
233 const VideoCaptureDeviceDescriptor device_descriptor_;
234 const scoped_refptr<CameraHalDelegate> camera_hal_delegate_;
235
236 VideoCaptureParams chrome_capture_params_;
237 std::unique_ptr<VideoCaptureDevice::Client> client_;
238
239 // Stores the static camera characteristics of the camera device.
240 arc::mojom::CameraMetadataPtr static_metadata_;
241
242 // The state the CameraDeviceDelegate currently is in.
243 State state_;
244
245 // Clockwise rotation in degrees. This value should be 0, 90, 180, or 270.
246 int rotation_;
247
248 // Mojo proxy and binding for the camera HAL device ops and callback ops
249 // interfaces.
250 arc::mojom::Camera3DeviceOpsPtr device_ops_;
251 mojo::Binding<arc::mojom::Camera3CallbackOps> callback_ops_;
252
253 // Where all the Mojo IPC calls takes place.
254 const scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_;
255
256 // The frame number. Increased by one for each capture request sent; reset to
257 // zero in AllocateAndStart.
258 uint32_t frame_number_;
259
260 struct StreamContext {
261 // The actual pixel format used in the capture request.
262 VideoCaptureFormat capture_format;
263 // The camera HAL stream.
264 arc::mojom::Camera3StreamPtr stream;
265 // The request settings used in the capture request of this stream.
266 arc::mojom::CameraMetadataPtr request_settings;
267 // The allocated buffers of this stream.
268 std::vector<std::unique_ptr<base::SharedMemory>> buffers;
269 // The free buffers of this stream. The queue stores indices into the
270 // |buffers| vector.
271 std::queue<size_t> free_buffers;
272 };
273
274 // The stream context of the preview stream.
275 std::unique_ptr<StreamContext> stream_context_;
276
277 // CaptureResult is used to hold the partial capture results for each frame.
278 struct CaptureResult {
279 CaptureResult() : metadata(arc::mojom::CameraMetadata::New()) {}
280 // |reference_time| and |timestamp| are derived from the shutter time of
281 // this frame. They are be passed to |client_->OnIncomingCapturedData|
282 // along with the |buffers| when the captured frame is submitted.
283 base::TimeTicks reference_time;
284 base::TimeDelta timestamp;
285 // The result metadata. Contains various information about the captured
286 // frame.
287 arc::mojom::CameraMetadataPtr metadata;
288 // The buffer handle that hold the captured data of this frame.
289 arc::mojom::Camera3StreamBufferPtr buffer;
290 // The set of the partial metadata received. For each capture result, the
291 // total number of partial metadata should equal to |partial_result_count_|.
292 std::set<uint32_t> partial_metadata_received;
293 };
294
295 // The number of partial stages. |partial_result_count_| is learned by
296 // querying |static_metadata_|. In case the result count is absent in
297 // |static_metadata_|, it defaults to one which means all the result metadata
298 // and captured buffer of a frame are returned together in one shot.
299 uint32_t partial_result_count_;
300
301 // The shutter time of the first frame. We derive the |timestamp| of a frame
302 // using the difference between the frame's shutter time and
303 // |first_frame_shutter_time_|.
304 base::TimeTicks first_frame_shutter_time_;
305
306 // Stores the partial capture results of the current in-flight frames.
307 std::map<uint32_t, CaptureResult> partial_results_;
308
309 DISALLOW_IMPLICIT_CONSTRUCTORS(CameraDeviceDelegate);
310 };
311
312 } // namespace media
313
314 #endif // MEDIA_CAPTURE_VIDEO_CHROMEOS_CAMERA_DEVICE_DELEGATE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698