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

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: WIP: media: add video capture device for ARC++ camera HAL v3 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 <unordered_map>
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 using namespace arc::mojom;
chfremer 2017/04/27 22:41:41 As per Google C++ Style Guide, this may not be all
jcliang 2017/04/28 04:47:57 Removed.
23
24 // CameraDeviceDelegate is instantiated on the capture thread where
25 // AllocateAndStart of VideoCaptureDeviceArcChromeOS runs on. All the methods
26 // in CameraDeviceDelegate runs on |device_task_runner_| and hence all the
27 // access to member variables is sequenced.
28 class CAPTURE_EXPORT CameraDeviceDelegate final
chfremer 2017/04/27 22:41:41 This class is fairly large, which makes it difficu
jcliang 2017/04/28 04:47:57 I'll try if I can split the class, but it may not
29 : public base::RefCountedThreadSafe<CameraDeviceDelegate>,
30 public 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().
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 loops is
chfremer 2017/04/27 22:41:41 loops -> loop
jcliang 2017/04/28 04:47:57 Done.
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 arc::mojom::CameraMetadataPtr static_metadata,
97 mojo::InterfacePtrInfo<Camera3DeviceOps> device_ops_info,
98 const scoped_refptr<base::SingleThreadTaskRunner> device_task_runner);
99
100 // Converts the HAL pixel format |from| to Chromium pixel format. Returns
101 // PIXEL_FORMAT_UNKNOWN if |from| is not supported.
102 static VideoPixelFormat PixFormatHalToChromium(
103 arc::mojom::HalPixelFormat from);
104
105 // Converts the Chromium pixel format |from| to DRM pixel format. Returns 0
106 // if |from| is not supported.
107 static uint32_t PixFormatChromiumToDrm(VideoPixelFormat from);
108
109 // Delegation methods for the VideoCaptureDevice interface.
110 void AllocateAndStart(const VideoCaptureParams& params,
111 std::unique_ptr<VideoCaptureDevice::Client> client);
112 void StopAndDeAllocate(base::WaitableEvent* closed);
113 void TakePhoto(VideoCaptureDevice::TakePhotoCallback callback);
114 void GetPhotoCapabilities(
115 VideoCaptureDevice::GetPhotoCapabilitiesCallback callback);
116 void SetPhotoOptions(mojom::PhotoSettingsPtr settings,
117 VideoCaptureDevice::SetPhotoOptionsCallback callback);
118
119 // Sets the frame rotation angle in |rotation_|.
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 function asynchronously
chfremer 2017/04/27 22:41:41 Comment cut off?
jcliang 2017/04/28 04:47:57 Done.
142 void OnClosed(base::WaitableEvent* closed, int32_t result);
143
144 // Initializes the camera HAL. Initialize sets up the Camera3CallbackOps with
145 // the camera HAL. OnInitialized continues to ConfigureStreams if the
146 // Initialize call succeeds.
147 void Initialize();
148 void OnInitialized(int32_t result);
149
150 // ConfigureStreams sets up stream context in |streams_| and configure the
151 // streams with the camera HAL. OnConfiguredStreams updates |streams_| with
152 // the stream config returned, and allocates buffers as per |updated_config|
153 // indicates. If there's no error OnConfiguredStreams notifies
154 // |client_| the capture has started by calling OnStarted, and proceeds to
155 // ConstructDefaultRequestSettings.
156 void ConfigureStreams();
157 void OnConfiguredStreams(Camera3StreamConfigurationPtr updated_config);
158
159 // ConstructDefaultRequestSettings asks the camera HAL for the default request
160 // settings of each stream in |streams_|. OnConstructedDefaultRequestSettings
161 // sets the request settings for |stream_type| in |streams_|. If there's no
162 // error OnConstructedDefaultRequestSettings calls StartCapture to start the
163 // video capture loop.
164 void ConstructDefaultRequestSettings(Camera3RequestTemplate stream_type);
165 void OnConstructedDefaultRequestSettings(Camera3RequestTemplate stream_type,
166 CameraMetadataPtr settings);
167
168 // StartCapture is the entry point to starting the video capture of
169 // |stream_type|. The way the video capture loop works is:
170 //
171 // (1) If there is a free buffer, RegisterBuffer registers the buffer with
172 // the camera HAL.
173 // (2) Once the free buffer is registered, ProcessCaptureRequest is called to
174 // issue a capture request which will eventually fill the registered
175 // buffer. Goto (1) to register the remaining free buffers.
176 // (3) The camera HAL returns shutter time of a capture request through
177 // Notify, and the filled buffer through ProcessCaptureResult.
178 // (4) Once all the result metadata are collected, SubmitCaptureResult is
179 // called to deliver the filled buffer to Chrome. After the buffer is
180 // consumed by Chrome it is enqueued back to the free buffer queue.
181 // Goto (1) to start another capture loop.
182 //
183 // The capture loop runs asynchronously. (1), (2), (3) and (4) may be
184 // interleaved.
185 void StartCapture(Camera3RequestTemplate stream_type);
186
187 // Registers a free buffer, if any, of |stream_type| to the camera HAL.
188 void RegisterBuffer(Camera3RequestTemplate stream_type);
189
190 // Calls ProcessCaptureRequest if the buffer indicated by |buffer_index| is
191 // successfully registered.
192 void OnRegisteredBuffer(Camera3RequestTemplate stream_type,
193 size_t buffer_index,
194 int32_t result);
195
196 // Creates and sends out a capture request for |stream_type|. The capture
197 // request contains the buffer handle indicated by |buffer_index|.
198 void ProcessCaptureRequest(Camera3RequestTemplate stream_type,
199 size_t buffer_index);
200 // Calls RegisterBuffer to attempt to register any remaining free buffers of
201 // |stream_type|.
202 void OnProcessedCaptureRequest(Camera3RequestTemplate stream_type,
203 int32_t result);
204
205 // Camera3CallbackOps implementations.
206
207 // ProcessCaptureResult receives the result metadata as well as the filled
208 // buffer from camera HAL. The result metadata may be divided and delivered
209 // in several stages. Before all the result metadata is received the partial
210 // results are kept in |partial_results_|.
211 void ProcessCaptureResult(Camera3CaptureResultPtr result) final;
212
213 // Notify receives the shutter time of capture requests and various errors
214 // from camera HAL. The shutter time is used as the timestamp in the video
215 // frame delivered to Chrome.
216 void Notify(Camera3NotifyMsgPtr message) final;
217
218 // Submits the captured buffer of frame |frame_number_| to Chrome, then
219 // enqueues the buffer to free buffer queue for the next capture request.
220 void SubmitCaptureResult(uint32_t frame_number);
221
222 VideoCaptureDeviceDescriptor device_descriptor_;
223 std::unique_ptr<VideoCaptureDevice::Client> client_;
224
225 // Stores the static camera characteristics of the camera device.
226 arc::mojom::CameraMetadataPtr static_metadata_;
227
228 // The state the CameraDeviceDelegate currently is in.
229 State state_;
230
231 // Clockwise rotation in degrees. This value should be 0, 90, 180, or 270.
232 int rotation_;
233
234 // Mojo proxy and binding for the camera HAL device ops and callback ops API.
235 mojo::InterfacePtrInfo<Camera3DeviceOps> device_ops_info_;
236 Camera3DeviceOpsPtr device_ops_;
237 mojo::Binding<Camera3CallbackOps> callback_ops_;
238
239 // Where all the Mojo IPC calls takes place.
240 const scoped_refptr<base::SingleThreadTaskRunner> device_task_runner_;
chfremer 2017/04/27 22:41:41 Would "ipc_task_runner_" be a more revealing name
jcliang 2017/04/28 04:47:57 Done.
241
242 // The frame number. Increased by one for each capture request sent; reset to
243 // zero in AllocateAndStart.
244 uint32_t frame_number_;
245
246 struct StreamContext {
247 // Capture parameters provided by Chrome.
248 VideoCaptureParams params;
249 // The actual pixel format used in the capture request.
250 VideoCaptureFormat capture_format;
251 // The camera HAL stream.
252 arc::mojom::Camera3StreamPtr stream;
253 // The request settings used in the capture request of this stream.
254 arc::mojom::CameraMetadataPtr request_settings;
255 // The allocated buffers of this stream.
256 std::vector<std::unique_ptr<base::SharedMemory>> buffers;
257 // The free buffers of this stream.
258 std::queue<size_t> free_buffers;
chfremer 2017/04/27 22:41:41 Are these buffers or buffer ids? Or indices into t
jcliang 2017/04/28 04:47:57 It's indices into the |buffers| vector. I'll updat
259 };
260
261 StreamContext* GetStreamContext(Camera3RequestTemplate stream_type) {
262 auto it = streams_.find(stream_type);
263 if (it == streams_.end()) {
264 return nullptr;
265 }
266 return &(it->second);
267 }
268
269 // Stores the stream context of each active stream.
270 std::unordered_map<Camera3RequestTemplate, StreamContext> streams_;
271
272 // CaptureResult is used to hold the partial capture results for each frame.
273 struct CaptureResult {
274 CaptureResult()
275 : metadata(arc::mojom::CameraMetadata::New()), partial_stage(0) {}
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 handles that hold the captured data of this frame.
285 std::unordered_map<arc::mojom::Camera3RequestTemplate,
286 arc::mojom::Camera3StreamBufferPtr>
287 buffers;
288 // The current stage of this partial result.
289 uint32_t partial_stage;
290 };
291
292 // The number of partial stages. |partial_result_count_| is learned by
293 // querying |static_metadata_|. In case the result count is absent in
294 // |static_metadata_|, it defaults to one which means all the result metadata
295 // and captured buffer of a frame are returned together in one shot.
296 uint32_t partial_result_count_;
297
298 // The shutter time of the first frame. We derive the |timestamp| of a frame
299 // using the difference between the frame's shutter time and
300 // |first_frame_shutter_time_|.
301 base::TimeTicks first_frame_shutter_time_;
302
303 // Stores the partial capture results of the current in-flight frames.
304 std::map<uint32_t, CaptureResult> partial_results_;
305
306 DISALLOW_IMPLICIT_CONSTRUCTORS(CameraDeviceDelegate);
307 };
308
309 } // namespace media
310
311 #endif // MEDIA_CAPTURE_VIDEO_CHROMEOS_CAMERA_DEVICE_DELEGATE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698