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

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

Powered by Google App Engine
This is Rietveld 408576698