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

Side by Side Diff: media/capture/video/chromeos/camera_hal_delegate.cc

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 #include "media/capture/video/chromeos/camera_hal_delegate.h"
6
7 #include <fcntl.h>
8 #include <sys/uio.h>
9
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/strings/string_piece.h"
13 #include "media/capture/video/chromeos/camera_metadata_utils.h"
14 #include "media/capture/video/chromeos/video_capture_device_arc_chromeos.h"
15 #include "mojo/edk/embedder/embedder.h"
16 #include "mojo/edk/embedder/incoming_broker_client_invitation.h"
17 #include "mojo/edk/embedder/named_platform_handle.h"
18 #include "mojo/edk/embedder/named_platform_handle_utils.h"
19 #include "mojo/edk/embedder/platform_channel_pair.h"
20 #include "mojo/edk/embedder/platform_channel_utils_posix.h"
21 #include "mojo/edk/embedder/platform_handle_vector.h"
22
23 namespace media {
24
25 namespace {
26
27 const base::StringPiece kArcCamera3SocketPath("/var/run/camera/camera3.sock");
28
29 const base::TimeDelta kEventWaitTimeoutMs =
30 base::TimeDelta::FromMilliseconds(3000);
31
32 } // namespace
33
34 CameraHalDelegate::CameraHalDelegate(
35 const scoped_refptr<base::SingleThreadTaskRunner> module_task_runner)
36 : builtin_camera_info_updated_(
37 base::WaitableEvent::ResetPolicy::MANUAL,
38 base::WaitableEvent::InitialState::NOT_SIGNALED),
39 num_builtin_cameras_(0),
40 module_task_runner_(module_task_runner),
41 camera_module_callbacks_(this) {
42 // We need to detach |thread_checker_| here because constructor is called on
43 // UI thread while the VideoCaptureDeviceFactory methods are called on capture
44 // thread.
45 thread_checker_.DetachFromThread();
46 }
47
48 CameraHalDelegate::~CameraHalDelegate() {
49 base::WaitableEvent interface_closed(
50 base::WaitableEvent::ResetPolicy::MANUAL,
51 base::WaitableEvent::InitialState::NOT_SIGNALED);
52 module_task_runner_->PostTask(
53 FROM_HERE,
54 base::Bind(&CameraHalDelegate::ResetMojoInterfaceOnModuleThread,
55 base::Unretained(this), base::Unretained(&interface_closed)));
56 interface_closed.Wait();
57 }
58
59 bool CameraHalDelegate::StartCameraModuleIpc() {
60 // Non-blocking socket handle.
61 mojo::edk::ScopedPlatformHandle socket_handle = mojo::edk::CreateClientHandle(
62 mojo::edk::NamedPlatformHandle(kArcCamera3SocketPath));
63
64 // Set socket to blocking
65 int flags = HANDLE_EINTR(fcntl(socket_handle.get().handle, F_GETFL));
66 if (flags == -1) {
67 PLOG(ERROR) << "fcntl(F_GETFL) failed: ";
68 return false;
69 }
70 if (HANDLE_EINTR(fcntl(socket_handle.get().handle, F_SETFL,
71 flags & ~O_NONBLOCK)) == -1) {
72 PLOG(ERROR) << "fcntl(F_SETFL) failed: ";
73 return false;
74 }
75
76 const size_t kTokenSize = 32;
77 char token[kTokenSize] = {};
78 std::deque<mojo::edk::PlatformHandle> platform_handles;
79 ssize_t ret = mojo::edk::PlatformChannelRecvmsg(
80 socket_handle.get(), token, sizeof(token), &platform_handles, true);
81 if (ret == -1) {
82 PLOG(ERROR) << "PlatformChannelRecvmsg failed: ";
83 return false;
84 }
85 if (platform_handles.size() != 1) {
86 LOG(ERROR) << "Unexpected number of handles received, expected 1: "
87 << platform_handles.size();
88 return false;
89 }
90 mojo::edk::ScopedPlatformHandle parent_pipe(platform_handles.back());
91 platform_handles.pop_back();
92 if (!parent_pipe.is_valid()) {
93 LOG(ERROR) << "Invalid parent pipe";
94 return false;
95 }
96 std::unique_ptr<mojo::edk::IncomingBrokerClientInvitation> invitation =
97 mojo::edk::IncomingBrokerClientInvitation::Accept(
98 mojo::edk::ConnectionParams(mojo::edk::TransportProtocol::kLegacy,
99 std::move(parent_pipe)));
100 mojo::ScopedMessagePipeHandle child_pipe =
101 invitation->ExtractMessagePipe(token);
102 DCHECK(child_pipe.is_valid());
103
104 camera_module_ =
105 mojo::MakeProxy(mojo::InterfacePtrInfo<arc::mojom::CameraModule>(
106 std::move(child_pipe), 0u),
107 module_task_runner_);
108
109 VLOG(1) << "Camera module IPC connection established";
110
111 return true;
112 }
113
114 std::unique_ptr<VideoCaptureDevice> CameraHalDelegate::CreateDevice(
115 const scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
116 const VideoCaptureDeviceDescriptor& device_descriptor) {
117 DCHECK(thread_checker_.CalledOnValidThread());
118 std::unique_ptr<VideoCaptureDevice> capture_device;
119 if (!UpdateBuiltInCameraInfo()) {
120 return capture_device;
121 }
122 if (camera_info_.find(device_descriptor.device_id) == camera_info_.end()) {
123 LOG(ERROR) << "Invalid camera device: " << device_descriptor.device_id;
124 return capture_device;
125 }
126 capture_device.reset(new VideoCaptureDeviceArcChromeOS(
127 ui_task_runner, device_descriptor, this));
128 return capture_device;
129 }
130
131 void CameraHalDelegate::GetSupportedFormats(
132 const VideoCaptureDeviceDescriptor& device_descriptor,
133 VideoCaptureFormats* supported_formats) {
134 DCHECK(thread_checker_.CalledOnValidThread());
135
136 if (!UpdateBuiltInCameraInfo()) {
137 return;
138 }
139 std::string camera_id = device_descriptor.device_id;
140 if (camera_info_.find(camera_id) == camera_info_.end() ||
141 camera_info_[camera_id].is_null()) {
142 // The camera may be removed already or is not ready yet.
143 VLOG(1) << "Invalid camera_id: " << camera_id;
wuchengli 2017/05/25 08:24:12 We have only built-in cameras now. LOG(ERROR)
jcliang 2017/05/25 14:23:47 Done.
144 return;
145 }
146 const arc::mojom::CameraInfoPtr& camera_info = camera_info_[camera_id];
147
148 // FIXME(jcliang): Remove this after the camera HAL has set the min frame
149 // durations for all formats.
150 supported_formats->emplace_back(gfx::Size(1280, 720), 60.0,
wuchengli 2017/05/25 08:24:12 Remember to change this to 30 or remove the hack b
151 PIXEL_FORMAT_NV12);
152
153 const arc::mojom::CameraMetadataEntryPtr* min_frame_durations =
154 GetMetadataEntry(camera_info->static_camera_characteristics,
155 arc::mojom::CameraMetadataTag::
156 ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS);
157 if (!min_frame_durations) {
158 LOG(ERROR)
159 << "Failed to get available min frame durations from camera info";
160 return;
161 }
162 // The min frame durations are stored as tuples of four int64s:
163 // (hal_pixel_format, width, height, ns) x n
164 const size_t kStreamFormatOffset = 0;
165 const size_t kStreamWidthOffset = 1;
166 const size_t kStreamHeightOffset = 2;
167 const size_t kStreamDurationOffset = 3;
168 const size_t kStreamDurationSize = 4;
169 int64_t* iter =
170 reinterpret_cast<int64_t*>((*min_frame_durations)->data.data());
171 for (size_t i = 0; i < (*min_frame_durations)->count / sizeof(int64_t); ++i) {
172 int32_t format = static_cast<int32_t>(iter[kStreamFormatOffset]);
173 int32_t width = static_cast<int32_t>(iter[kStreamWidthOffset]);
174 int32_t height = static_cast<int32_t>(iter[kStreamHeightOffset]);
175 int64_t duration = iter[kStreamDurationOffset];
176 iter += kStreamDurationSize;
177
178 float max_fps = 1.0 * 1000000000LL / duration;
179
180 DVLOG(1) << "[" << std::hex << format << " " << std::dec << width << " "
181 << height << " " << duration << "]";
182 VideoPixelFormat cr_format =
183 VideoCaptureDeviceArcChromeOS::PixFormatHalToChromium(
184 static_cast<arc::mojom::HalPixelFormat>(format));
185 if (cr_format == PIXEL_FORMAT_UNKNOWN) {
186 continue;
187 }
188 VLOG(1) << "Supported format: " << width << "x" << height
189 << " fps=" << max_fps << " format=" << cr_format;
190 supported_formats->emplace_back(gfx::Size(width, height), max_fps,
191 cr_format);
192 }
193 }
194
195 void CameraHalDelegate::GetDeviceDescriptors(
196 VideoCaptureDeviceDescriptors* device_descriptors) {
197 DCHECK(thread_checker_.CalledOnValidThread());
198
199 if (!UpdateBuiltInCameraInfo()) {
200 return;
201 }
202 for (size_t id = 0; id < num_builtin_cameras_; ++id) {
203 VideoCaptureDeviceDescriptor desc;
204 std::string camera_id = std::to_string(id);
205 const arc::mojom::CameraInfoPtr& camera_info = camera_info_[camera_id];
206 if (!camera_info) {
207 continue;
208 }
209 desc.device_id = camera_id;
210 desc.capture_api = VideoCaptureApi::ANDROID_API2_LIMITED;
211 desc.transport_type = VideoCaptureTransportType::OTHER_TRANSPORT;
212 switch (camera_info->facing) {
213 case arc::mojom::CameraFacing::CAMERA_FACING_BACK:
214 desc.facing = VideoFacingMode::MEDIA_VIDEO_FACING_ENVIRONMENT;
215 desc.display_name = std::string("Back Camera");
216 break;
217 case arc::mojom::CameraFacing::CAMERA_FACING_FRONT:
218 desc.facing = VideoFacingMode::MEDIA_VIDEO_FACING_USER;
219 desc.display_name = std::string("Front Camera");
220 break;
221 case arc::mojom::CameraFacing::CAMERA_FACING_EXTERNAL:
222 desc.facing = VideoFacingMode::MEDIA_VIDEO_FACING_NONE;
223 desc.display_name = std::string("External Camera");
224 break;
225 // Mojo validates the input parameters for us so we don't need to worry
226 // about malformed values.
227 }
228 device_descriptors->push_back(desc);
229 }
wuchengli 2017/05/25 09:06:41 Let's sort and put the front camera first. It seem
jcliang 2017/05/25 14:23:47 Done.
230 }
231
232 void CameraHalDelegate::GetCameraInfo(int32_t camera_id,
233 const GetCameraInfoCallback& callback) {
234 // This method may be called on any thread. Currently this method is used by
235 // VideoCaptureDeviceArcChromeOS to query camera info.
wuchengli 2017/05/25 08:24:12 s/VideoCaptureDeviceArcChromeOS/CameraDeviceDelega
jcliang 2017/05/25 14:23:47 Done.
236 module_task_runner_->PostTask(
237 FROM_HERE, base::Bind(&CameraHalDelegate::GetCameraInfoOnModuleThread,
238 this, camera_id, callback));
239 }
240
241 void CameraHalDelegate::OpenDevice(int32_t camera_id,
242 const OpenDeviceCallback& callback) {
243 // This method may be called on any thread. Currently this method is used by
244 // VideoCaptureDeviceArcChromeOS to open a camera device.
wuchengli 2017/05/25 08:24:12 s/VideoCaptureDeviceArcChromeOS/CameraDeviceDelega
jcliang 2017/05/25 14:23:47 Done.
245 module_task_runner_->PostTask(
246 FROM_HERE, base::Bind(&CameraHalDelegate::OpenDeviceOnModuleThread, this,
247 camera_id, callback));
248 }
249
250 void CameraHalDelegate::StartForTesting(arc::mojom::CameraModulePtrInfo info) {
251 camera_module_.Bind(std::move(info), module_task_runner_);
252 }
253
254 void CameraHalDelegate::ResetMojoInterfaceOnModuleThread(
255 base::WaitableEvent* interface_closed) {
256 DCHECK(module_task_runner_->BelongsToCurrentThread());
257 camera_module_.reset();
258 if (camera_module_callbacks_.is_bound()) {
259 camera_module_callbacks_.Unbind();
260 }
261 interface_closed->Signal();
262 }
263
264 bool CameraHalDelegate::UpdateBuiltInCameraInfo() {
265 DCHECK(thread_checker_.CalledOnValidThread());
266 if (builtin_camera_info_updated_.IsSignaled()) {
267 return true;
268 }
269 // The built-in camera are static per specification of the Android camera HAL
270 // v3 specification. We only update the built-in camera info once.
271 module_task_runner_->PostTask(
272 FROM_HERE,
273 base::Bind(&CameraHalDelegate::UpdateBuiltInCameraInfoOnModuleThread,
274 this));
275 if (!builtin_camera_info_updated_.TimedWait(kEventWaitTimeoutMs)) {
276 LOG(ERROR) << "Timed out getting camera info";
277 return false;
278 }
279 return true;
280 }
281
282 void CameraHalDelegate::UpdateBuiltInCameraInfoOnModuleThread() {
283 DCHECK(module_task_runner_->BelongsToCurrentThread());
284 camera_module_->GetNumberOfCameras(
285 base::Bind(&CameraHalDelegate::OnGotNumberOfCamerasOnModuleThread, this));
286 }
287
288 void CameraHalDelegate::OnGotNumberOfCamerasOnModuleThread(
289 int32_t num_cameras) {
290 DCHECK(module_task_runner_->BelongsToCurrentThread());
291 if (num_cameras < 0) {
292 builtin_camera_info_updated_.Signal();
293 LOG(ERROR) << "Failed to get number of cameras";
294 return;
295 }
296 VLOG(1) << "Number of built-in cameras: " << num_cameras;
297 num_builtin_cameras_ = num_cameras;
298 // Per camera HAL v3 specification SetCallbacks() should be called after the
299 // first time GetNumberOfCameras() is called, and before other CameraModule
300 // functions are called.
301 camera_module_->SetCallbacks(
302 camera_module_callbacks_.CreateInterfacePtrAndBind(),
303 base::Bind(&CameraHalDelegate::OnSetCallbacksOnModuleThread, this));
304 }
305
306 void CameraHalDelegate::OnSetCallbacksOnModuleThread(int32_t result) {
307 DCHECK(module_task_runner_->BelongsToCurrentThread());
308 if (result) {
309 num_builtin_cameras_ = 0;
310 builtin_camera_info_updated_.Signal();
311 LOG(ERROR) << "Failed to set camera module callbacks: " << strerror(result);
312 return;
313 }
314 for (size_t camera_id = 0; camera_id < num_builtin_cameras_; ++camera_id) {
315 GetCameraInfoOnModuleThread(
316 camera_id, base::Bind(&CameraHalDelegate::OnGotCameraInfoOnModuleThread,
317 this, camera_id));
318 }
319 }
320
321 void CameraHalDelegate::GetCameraInfoOnModuleThread(
322 int32_t camera_id,
323 const GetCameraInfoCallback& callback) {
324 DCHECK(module_task_runner_->BelongsToCurrentThread());
325 camera_module_->GetCameraInfo(camera_id, callback);
326 }
327
328 void CameraHalDelegate::OnGotCameraInfoOnModuleThread(
329 int32_t camera_id,
330 int32_t result,
331 arc::mojom::CameraInfoPtr camera_info) {
332 DCHECK(module_task_runner_->BelongsToCurrentThread());
333 DVLOG(1) << "Got camera info of camera " << camera_id;
334 if (result) {
335 // The call to get_camera_info may fail if the device is still in the
336 // enumerating state or is no longer present.
wuchengli 2017/05/25 08:24:12 Update the comment because we only have built-in c
jcliang 2017/05/25 14:23:47 Done.
337 VLOG(1) << "Failed to get camera info. Camera id: " << camera_id;
338 }
339 // In case of failure |camera_info| is empty.
340 camera_info_[std::to_string(camera_id)] = std::move(camera_info);
341 if (!builtin_camera_info_updated_.IsSignaled()) {
342 // Make sure all the built-in camera info is set. |camera_info_| may hold
343 // info of external cameras as well so we need to check each id of the
wuchengli 2017/05/25 08:24:12 Update the comment because we only have built-in c
jcliang 2017/05/25 14:23:47 Done.
344 // built-in cameras here.
345 for (size_t id = 0; id < num_builtin_cameras_; ++id) {
346 if (camera_info_.find(std::to_string(id)) == camera_info_.end()) {
347 return;
348 }
349 }
350 builtin_camera_info_updated_.Signal();
351 }
352 }
353
354 void CameraHalDelegate::OpenDeviceOnModuleThread(
355 int32_t camera_id,
356 const OpenDeviceCallback& callback) {
357 DCHECK(module_task_runner_->BelongsToCurrentThread());
358 camera_module_->OpenDevice(camera_id, callback);
359 }
360
361 // CameraModuleCallbacks implementations.
362 void CameraHalDelegate::CameraDeviceStatusChange(
363 int32_t camera_id,
364 arc::mojom::CameraDeviceStatus new_status) {
365 DCHECK(module_task_runner_->BelongsToCurrentThread());
366 // TODO(jcliang): Handle status change for external cameras.
367 }
368
369 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698