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

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 and 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 {
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.
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 const 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.
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(
151 int32_t result,
152 arc::mojom::Camera3DeviceOpsPtr device_ops);
153 // Creates the Mojo connection to the camera device.
154 void OnOpenedDevice(
155 int32_t result,
156 mojo::InterfacePtrInfo<arc::mojom::Camera3DeviceOps> device_ops_info);
157
158 // Initializes the camera HAL. Initialize sets up the Camera3CallbackOps with
159 // the camera HAL. OnInitialized continues to ConfigureStreams if the
160 // Initialize call succeeds.
161 void Initialize();
162 void OnInitialized(int32_t result);
163
164 // ConfigureStreams sets up stream context in |streams_| and configure the
165 // streams with the camera HAL. OnConfiguredStreams updates |streams_| with
166 // the stream config returned, and allocates buffers as per |updated_config|
167 // indicates. If there's no error OnConfiguredStreams notifies
168 // |client_| the capture has started by calling OnStarted, and proceeds to
169 // ConstructDefaultRequestSettings.
170 void ConfigureStreams();
171 void OnConfiguredStreams(
172 int32_t result,
173 arc::mojom::Camera3StreamConfigurationPtr updated_config);
174
175 // ConstructDefaultRequestSettings asks the camera HAL for the default request
176 // settings of the stream in |stream_context_|.
177 // OnConstructedDefaultRequestSettings sets the request settings in
178 // |streams_context_|. If there's no error
179 // OnConstructedDefaultRequestSettings calls StartCapture to start the video
180 // capture loop.
181 void ConstructDefaultRequestSettings();
182 void OnConstructedDefaultRequestSettings(
183 arc::mojom::CameraMetadataPtr settings);
184
185 // StartCapture is the entry point to starting the video capture. The way the
186 // video capture loop works is:
187 //
188 // (1) If there is a free buffer, RegisterBuffer registers the buffer with
189 // the camera HAL.
190 // (2) Once the free buffer is registered, ProcessCaptureRequest is called to
191 // issue a capture request which will eventually fill the registered
192 // buffer. Goto (1) to register the remaining free buffers.
193 // (3) The camera HAL returns shutter time of a capture request through
194 // Notify, and the filled buffer through ProcessCaptureResult.
195 // (4) Once all the result metadata are collected, SubmitCaptureResult is
196 // called to deliver the filled buffer to Chrome. After the buffer is
197 // consumed by Chrome it is enqueued back to the free buffer queue.
198 // Goto (1) to start another capture loop.
199 void StartCapture();
200
201 // Registers a free buffer, if any, to the camera HAL.
202 void RegisterBuffer();
203
204 // Calls ProcessCaptureRequest if the buffer indicated by |buffer_id| is
205 // successfully registered.
206 void OnRegisteredBuffer(size_t buffer_id, int32_t result);
207
208 // The capture request contains the buffer handle indicated by |buffer_id|.
209 void ProcessCaptureRequest(size_t buffer_id);
210 // Calls RegisterBuffer to attempt to register any remaining free buffers.
211 void OnProcessedCaptureRequest(int32_t result);
212
213 // Camera3CallbackOps implementations.
214
215 // ProcessCaptureResult receives the result metadata as well as the filled
216 // buffer from camera HAL. The result metadata may be divided and delivered
217 // in several stages. Before all the result metadata is received the partial
218 // results are kept in |partial_results_|.
219 void ProcessCaptureResult(arc::mojom::Camera3CaptureResultPtr result) final;
220
221 // Notify receives the shutter time of capture requests and various errors
222 // from camera HAL. The shutter time is used as the timestamp in the video
223 // frame delivered to Chrome.
224 void Notify(arc::mojom::Camera3NotifyMsgPtr message) final;
225 void HandleNotifyError(uint32_t frame_number,
226 uint64_t error_stream_id,
227 arc::mojom::Camera3ErrorMsgCode error_code);
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