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

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: handle error_msg in Notify 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/named_platform_handle.h"
17 #include "mojo/edk/embedder/named_platform_handle_utils.h"
18 #include "mojo/edk/embedder/pending_process_connection.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 mojo::edk::SetParentPipeHandle(std::move(parent_pipe));
97
98 mojo::ScopedMessagePipeHandle child_pipe =
99 mojo::edk::CreateChildMessagePipe(std::string(token, kTokenSize));
100
101 camera_module_ =
102 mojo::MakeProxy(mojo::InterfacePtrInfo<arc::mojom::CameraModule>(
103 std::move(child_pipe), 0u),
104 module_task_runner_);
105
106 VLOG(1) << "Camera module IPC connection established";
107
108 return true;
109 }
110
111 std::unique_ptr<VideoCaptureDevice> CameraHalDelegate::CreateDevice(
112 const scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
113 const VideoCaptureDeviceDescriptor& device_descriptor) {
114 DCHECK(thread_checker_.CalledOnValidThread());
115 std::unique_ptr<VideoCaptureDevice> capture_device(
116 new VideoCaptureDeviceArcChromeOS(ui_task_runner, device_descriptor,
117 this));
118 return capture_device;
119 }
120
121 void CameraHalDelegate::GetSupportedFormats(
122 const VideoCaptureDeviceDescriptor& device_descriptor,
123 VideoCaptureFormats* supported_formats) {
124 DCHECK(thread_checker_.CalledOnValidThread());
125
126 if (!UpdateBuiltInCameraInfo()) {
127 return;
128 }
129 int camera_id = std::stoi(device_descriptor.device_id);
130 if (camera_info_.find(camera_id) == camera_info_.end() ||
131 camera_info_[camera_id].is_null()) {
132 // The camera may be removed already or is not ready yet.
133 VLOG(1) << "Invalid camera_id: " << camera_id;
134 return;
135 }
136 const arc::mojom::CameraInfoPtr& camera_info = camera_info_[camera_id];
137
138 const arc::mojom::CameraMetadataEntryPtr* fps_settings =
139 GetMetadataEntry(camera_info->static_camera_characteristics,
140 arc::mojom::CameraMetadataTag::
141 ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES);
142 if (!fps_settings) {
143 LOG(ERROR) << "Failed to get available target FPS ranges from camera info";
144 return;
145 }
146 // The available target FPS ranges are stored as pairs of two int32s:
147 // (min_fps, max_fps).
148 float max_fps = 0.0;
149 int32_t* iter = reinterpret_cast<int32_t*>((*fps_settings)->data.data());
150 // There may be multiple FPS ranges. We simply use the maximum FPS of all the
151 // FPS ranges here.
152 for (size_t i = 0; i < (*fps_settings)->count / sizeof(int32_t); ++i) {
153 float fps = static_cast<float>(*(iter + 1));
154 if (fps > max_fps) {
155 max_fps = fps;
156 }
157 iter += 2;
158 }
159
160 const arc::mojom::CameraMetadataEntryPtr* configurations =
161 GetMetadataEntry(camera_info->static_camera_characteristics,
162 arc::mojom::CameraMetadataTag::
163 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
164 if (!configurations) {
165 LOG(ERROR)
166 << "Failed to get available stream configurations from camera info";
167 return;
168 }
169 // The available stream configurations are stored as tuples of four int32s:
170 // (hal_pixel_format, width, height, input_or_output_type).
171 iter = reinterpret_cast<int32_t*>((*configurations)->data.data());
172 for (size_t i = 0; i < (*configurations)->count / sizeof(int32_t); ++i) {
173 arc::mojom::HalPixelFormat format =
174 static_cast<arc::mojom::HalPixelFormat>(*iter);
175 int32_t width = *(iter + 1);
176 int32_t height = *(iter + 2);
177 int32_t type = *(iter + 3);
178 iter += 4;
179 DVLOG(1) << "[" << std::hex << format << " " << std::dec << width << " "
180 << height << " " << type << "]";
181 VideoPixelFormat cr_format =
182 VideoCaptureDeviceArcChromeOS::PixFormatHalToChromium(format);
183 if (cr_format == PIXEL_FORMAT_UNKNOWN) {
184 continue;
185 }
186 VLOG(1) << "Supported format: " << width << "x" << height
187 << " fps=" << max_fps << " format=" << cr_format;
188 supported_formats->emplace_back(gfx::Size(width, height), max_fps,
189 cr_format);
190 }
191 }
192
193 void CameraHalDelegate::GetDeviceDescriptors(
194 VideoCaptureDeviceDescriptors* device_descriptors) {
195 DCHECK(thread_checker_.CalledOnValidThread());
196
197 if (!UpdateBuiltInCameraInfo()) {
198 return;
199 }
200 for (size_t camera_id = 0; camera_id < num_builtin_cameras_; ++camera_id) {
201 VideoCaptureDeviceDescriptor desc;
202 arc::mojom::CameraInfoPtr& camera_info = camera_info_[camera_id];
203 if (!camera_info) {
204 continue;
205 }
206 desc.device_id = std::to_string(camera_id);
207 desc.capture_api = VideoCaptureApi::ANDROID_API2_LIMITED;
208 desc.transport_type = VideoCaptureTransportType::OTHER_TRANSPORT;
209 switch (camera_info->facing) {
210 case arc::mojom::CameraFacing::CAMERA_FACING_BACK:
211 desc.facing = VideoFacingMode::MEDIA_VIDEO_FACING_ENVIRONMENT;
212 desc.display_name = std::string("Back Camera");
213 break;
214 case arc::mojom::CameraFacing::CAMERA_FACING_FRONT:
215 desc.facing = VideoFacingMode::MEDIA_VIDEO_FACING_USER;
216 desc.display_name = std::string("Front Camera");
217 break;
218 case arc::mojom::CameraFacing::CAMERA_FACING_EXTERNAL:
219 desc.facing = VideoFacingMode::MEDIA_VIDEO_FACING_NONE;
220 desc.display_name = std::string("External Camera");
221 break;
222 // Mojo validates the input parameters for us so we don't need to worry
223 // about malformed values.
224 }
225 device_descriptors->push_back(desc);
226 }
227 }
228
229 void CameraHalDelegate::GetCameraInfo(int32_t camera_id,
230 const GetCameraInfoCallback& callback) {
231 // This method may be called on any thread. Currently this method is used by
232 // VideoCaptureDeviceArcChromeOS to query camera info.
233 module_task_runner_->PostTask(
234 FROM_HERE, base::Bind(&CameraHalDelegate::GetCameraInfoOnModuleThread,
235 this, camera_id, callback));
236 }
237
238 void CameraHalDelegate::OpenDevice(int32_t camera_id,
239 const OpenDeviceCallback& callback) {
240 // This method may be called on any thread. Currently this method is used by
241 // VideoCaptureDeviceArcChromeOS to open a camera device.
242 module_task_runner_->PostTask(
243 FROM_HERE, base::Bind(&CameraHalDelegate::OpenDeviceOnModuleThread, this,
244 camera_id, callback));
245 }
246
247 void CameraHalDelegate::ResetMojoInterfaceOnModuleThread(
248 base::WaitableEvent* interface_closed) {
249 DCHECK(module_task_runner_->BelongsToCurrentThread());
250 camera_module_.reset();
251 if (camera_module_callbacks_.is_bound()) {
252 camera_module_callbacks_.Unbind();
253 }
254 interface_closed->Signal();
255 }
256
257 bool CameraHalDelegate::UpdateBuiltInCameraInfo() {
258 DCHECK(thread_checker_.CalledOnValidThread());
259 if (builtin_camera_info_updated_.IsSignaled()) {
260 return true;
261 }
262 // The built-in camera are static per specification of the Android camera HAL
263 // v3 specification. We only update the built-in camera info once.
264 module_task_runner_->PostTask(
265 FROM_HERE,
266 base::Bind(&CameraHalDelegate::UpdateBuiltInCameraInfoOnModuleThread,
267 this));
268 if (!builtin_camera_info_updated_.TimedWait(kEventWaitTimeoutMs)) {
269 LOG(ERROR) << "Timed out getting camera info";
270 return false;
271 }
272 return true;
273 }
274
275 void CameraHalDelegate::UpdateBuiltInCameraInfoOnModuleThread() {
276 DCHECK(module_task_runner_->BelongsToCurrentThread());
277 camera_module_->GetNumberOfCameras(
278 base::Bind(&CameraHalDelegate::OnGotNumberOfCamerasOnModuleThread, this));
279 }
280
281 void CameraHalDelegate::OnGotNumberOfCamerasOnModuleThread(
282 int32_t num_cameras) {
283 DCHECK(module_task_runner_->BelongsToCurrentThread());
284 if (num_cameras < 0) {
285 builtin_camera_info_updated_.Signal();
286 LOG(ERROR) << "Failed to get number of cameras";
287 return;
288 }
289 VLOG(1) << "Number of built-in cameras: " << num_cameras;
290 num_builtin_cameras_ = num_cameras;
291 // Per camera HAL v3 specification SetCallbacks() should be called after the
292 // first time GetNumberOfCameras() is called, and before other CameraModule
293 // functions are called.
294 camera_module_->SetCallbacks(
295 camera_module_callbacks_.CreateInterfacePtrAndBind(),
296 base::Bind(&CameraHalDelegate::OnSetCallbacksOnModuleThread, this));
297 }
298
299 void CameraHalDelegate::OnSetCallbacksOnModuleThread(int32_t result) {
300 DCHECK(module_task_runner_->BelongsToCurrentThread());
301 if (result) {
302 num_builtin_cameras_ = 0;
303 builtin_camera_info_updated_.Signal();
304 LOG(ERROR) << "Failed to set camera module callbacks: " << strerror(result);
305 return;
306 }
307 for (size_t camera_id = 0; camera_id < num_builtin_cameras_; ++camera_id) {
308 GetCameraInfoOnModuleThread(
309 camera_id, base::Bind(&CameraHalDelegate::OnGotCameraInfoOnModuleThread,
310 this, camera_id));
311 }
312 }
313
314 void CameraHalDelegate::GetCameraInfoOnModuleThread(
315 int32_t camera_id,
316 const GetCameraInfoCallback& callback) {
317 DCHECK(module_task_runner_->BelongsToCurrentThread());
318 camera_module_->GetCameraInfo(camera_id, callback);
319 }
320
321 void CameraHalDelegate::OnGotCameraInfoOnModuleThread(
322 int32_t camera_id,
323 int32_t result,
324 arc::mojom::CameraInfoPtr camera_info) {
325 DCHECK(module_task_runner_->BelongsToCurrentThread());
326 DVLOG(1) << "Got camera info of camera " << camera_id;
327 if (result) {
328 // The call to get_camera_info may fail if the device is still in the
329 // enumerating state or is no longer present.
330 VLOG(1) << "Failed to get camera info. Camera id: " << camera_id;
331 }
332 // In case of failure |camera_info| is empty.
333 camera_info_[camera_id] = std::move(camera_info);
334 if (!builtin_camera_info_updated_.IsSignaled()) {
335 // Make sure all the built-in camera info is set. |camera_info_| may hold
336 // info of external cameras as well so we need to check each id of the
337 // built-in cameras here.
338 for (size_t id = 0; id < num_builtin_cameras_; ++id) {
339 if (camera_info_.find(id) == camera_info_.end()) {
340 return;
341 }
342 }
343 builtin_camera_info_updated_.Signal();
344 }
345 }
346
347 void CameraHalDelegate::OpenDeviceOnModuleThread(
348 int32_t camera_id,
349 const OpenDeviceCallback& callback) {
350 DCHECK(module_task_runner_->BelongsToCurrentThread());
351 camera_module_->OpenDevice(camera_id, callback);
352 }
353
354 // CameraModuleCallbacks implementations.
355 void CameraHalDelegate::CameraDeviceStatusChange(
356 int32_t camera_id,
357 arc::mojom::CameraDeviceStatus new_status) {
358 DCHECK(module_task_runner_->BelongsToCurrentThread());
359 // TODO(jcliang): Handle status change for external cameras.
360 }
361
362 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698