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

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: 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 #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 using namespace arc::mojom;
26
27 namespace {
28
29 const base::StringPiece kArcCamera3SocketPath("/var/run/camera/camera3.sock");
30
31 const base::TimeDelta kEventWaitTimeoutMs =
32 base::TimeDelta::FromMilliseconds(3000);
33
34 } // namespace
35
36 CameraHalDelegate::CameraHalDelegate(
37 const scoped_refptr<base::SingleThreadTaskRunner> module_task_runner)
38 : camera_info_updated_(base::WaitableEvent::ResetPolicy::MANUAL,
39 base::WaitableEvent::InitialState::NOT_SIGNALED),
40 updating_camera_info_(false),
41 num_cameras_(0),
42 module_callbacks_registered_(false),
43 module_task_runner_(module_task_runner),
44 camera_module_callbacks_(this) {
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 LOG(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 LOG(ERROR) << "fcntl(F_SETFL) failed";
73 return false;
74 }
75
76 char token[32] = {};
77 std::deque<mojo::edk::PlatformHandle> platform_handles;
78 ssize_t ret = mojo::edk::PlatformChannelRecvmsg(
79 socket_handle.get(), token, sizeof(token), &platform_handles, true);
80 if (ret == -1) {
81 LOG(ERROR) << "PlatformChannelRecvmsg failed: " << strerror(errno);
82 }
83 if (platform_handles.size() != 1) {
84 LOG(ERROR) << "Unexpected number of handles received, expected 1: "
85 << platform_handles.size();
86 return false;
87 }
88 mojo::edk::ScopedPlatformHandle parent_pipe(platform_handles.back());
89 platform_handles.pop_back();
90 if (!parent_pipe.is_valid()) {
91 LOG(ERROR) << "Invalid parent pipe";
92 return false;
93 }
94 mojo::edk::SetParentPipeHandle(std::move(parent_pipe));
95
96 mojo::ScopedMessagePipeHandle child_pipe =
97 mojo::edk::CreateChildMessagePipe(std::string(token, 32));
98
99 camera_module_ = mojo::MakeProxy(
100 mojo::InterfacePtrInfo<CameraModule>(std::move(child_pipe), 0u),
101 module_task_runner_);
102
103 VLOG(1) << "Camera module IPC connection established";
104
105 return true;
106 }
107
108 std::unique_ptr<VideoCaptureDevice> CameraHalDelegate::CreateDevice(
109 const scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
110 const VideoCaptureDeviceDescriptor& device_descriptor) {
111 DCHECK(thread_checker_.CalledOnValidThread());
112 std::unique_ptr<VideoCaptureDevice> capture_device(
113 new VideoCaptureDeviceArcChromeOS(ui_task_runner, device_descriptor,
114 this));
115 return capture_device;
116 }
117
118 void CameraHalDelegate::GetSupportedFormats(
119 const VideoCaptureDeviceDescriptor& device_descriptor,
120 VideoCaptureFormats* supported_formats) {
121 DCHECK(thread_checker_.CalledOnValidThread());
122 if (!UpdateCameraInfo()) {
123 return;
124 }
125 int camera_id = std::stoi(device_descriptor.device_id);
126 if (camera_info_.find(camera_id) == camera_info_.end() ||
127 camera_info_[camera_id].is_null()) {
128 // The camera may be removed already or is not ready yet.
129 VLOG(1) << "Invalid camera_id: " << camera_id;
130 return;
131 }
132 const CameraInfoPtr& camera_info = camera_info_[camera_id];
133
134 const CameraMetadataEntryPtr* fps_settings = GetMetadataEntry(
135 camera_info->static_camera_characteristics,
136 CameraMetadataTag::ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES);
137 if (!fps_settings) {
138 LOG(ERROR) << "Failed to get available target FPS ranges from camera info";
139 return;
140 }
141 // The available target FPS ranges are stored as pairs of two int32s:
142 // (min_fps, max_fps).
143 float max_fps = 0.0;
144 int32_t* iter = reinterpret_cast<int32_t*>((*fps_settings)->data.data());
145 // There may be multiple FPS ranges. We simply use the maximum FPS of all the
146 // FPS ranges here.
147 for (size_t i = 0; i < (*fps_settings)->count / sizeof(int32_t); ++i) {
148 float fps = static_cast<float>(*(iter + 1));
149 if (fps > max_fps) {
150 max_fps = fps;
151 }
152 iter += 2;
153 }
154
155 const CameraMetadataEntryPtr* configurations = GetMetadataEntry(
156 camera_info->static_camera_characteristics,
157 CameraMetadataTag::ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
158 if (!configurations) {
159 LOG(ERROR)
160 << "Failed to get available stream configurations from camera info";
161 return;
162 }
163 // The available stream configurations are stored as tuples of four int32s:
164 // (hal_pixel_format, width, height, input_or_output_type).
165 iter = reinterpret_cast<int32_t*>((*configurations)->data.data());
166 for (size_t i = 0; i < (*configurations)->count / sizeof(int32_t); ++i) {
167 HalPixelFormat format = static_cast<HalPixelFormat>(*iter);
168 int32_t width = *(iter + 1);
169 int32_t height = *(iter + 2);
170 int32_t type = *(iter + 3);
171 iter += 4;
172 VLOG(1) << "[" << std::hex << format << " " << std::dec << width << " "
173 << height << " " << type << "]";
174 VideoPixelFormat cr_format =
175 VideoCaptureDeviceArcChromeOS::PixFormatHalToChromium(format);
176 if (cr_format == PIXEL_FORMAT_UNKNOWN) {
177 continue;
178 }
179 VLOG(1) << "Supported format: " << width << "x" << height
180 << " fps=" << max_fps << " format=" << cr_format;
181 supported_formats->emplace_back(gfx::Size(width, height), max_fps,
182 cr_format);
183 }
184 }
185
186 void CameraHalDelegate::GetDeviceDescriptors(
187 VideoCaptureDeviceDescriptors* device_descriptors) {
188 DCHECK(thread_checker_.CalledOnValidThread());
189
190 if (!UpdateCameraInfo()) {
191 return;
192 }
193 for (size_t camera_id = 0; camera_id < num_cameras_; ++camera_id) {
194 VideoCaptureDeviceDescriptor desc;
195 CameraInfoPtr& camera_info = camera_info_[camera_id];
196 if (!camera_info) {
197 continue;
198 }
199 desc.device_id = std::to_string(camera_id);
200 desc.capture_api = VideoCaptureApi::ANDROID_API2_LIMITED;
201 desc.transport_type = VideoCaptureTransportType::OTHER_TRANSPORT;
202 switch (camera_info->facing) {
203 case CameraFacing::CAMERA_FACING_BACK:
204 desc.facing = VideoFacingMode::MEDIA_VIDEO_FACING_ENVIRONMENT;
205 desc.display_name = std::string("Back Camera");
206 break;
207 case CameraFacing::CAMERA_FACING_FRONT:
208 desc.facing = VideoFacingMode::MEDIA_VIDEO_FACING_USER;
209 desc.display_name = std::string("Front Camera");
210 break;
211 case CameraFacing::CAMERA_FACING_EXTERNAL:
212 desc.facing = VideoFacingMode::MEDIA_VIDEO_FACING_NONE;
213 desc.display_name = std::string("External Camera");
214 break;
215 }
216 device_descriptors->push_back(desc);
217 }
218 }
219
220 void CameraHalDelegate::GetCameraInfo(int32_t camera_id,
221 const GetCameraInfoCallback& callback) {
222 module_task_runner_->PostTask(
223 FROM_HERE, base::Bind(&CameraHalDelegate::GetCameraInfoOnModuleThread,
224 this, camera_id, callback));
225 }
226
227 void CameraHalDelegate::OpenDevice(int32_t camera_id,
228 const OpenDeviceCallback& callback) {
229 module_task_runner_->PostTask(
230 FROM_HERE, base::Bind(&CameraHalDelegate::OpenDeviceOnModuleThread, this,
231 camera_id, callback));
232 }
233
234 void CameraHalDelegate::ResetMojoInterfaceOnModuleThread(
235 base::WaitableEvent* interface_closed) {
236 DCHECK(module_task_runner_->BelongsToCurrentThread());
237 camera_module_.reset();
238 if (camera_module_callbacks_.is_bound()) {
239 camera_module_callbacks_.Unbind();
240 }
241 interface_closed->Signal();
242 }
243
244 bool CameraHalDelegate::UpdateCameraInfo() {
245 DCHECK(thread_checker_.CalledOnValidThread());
246 if (camera_info_updated_.IsSignaled()) {
247 return true;
248 }
249 module_task_runner_->PostTask(
250 FROM_HERE,
251 base::Bind(&CameraHalDelegate::UpdateCameraInfoOnModuleThread, this));
252 if (!camera_info_updated_.TimedWait(kEventWaitTimeoutMs)) {
253 LOG(ERROR) << "Timed out getting camera info";
254 return false;
255 }
256 return true;
257 }
258
259 void CameraHalDelegate::UpdateCameraInfoOnModuleThread() {
260 DCHECK(module_task_runner_->BelongsToCurrentThread());
261 if (updating_camera_info_) {
262 return;
263 }
264 updating_camera_info_ = true;
265 // We clear all existing camera info here and ask again. In theory the camera
266 // info should remain unchanged for internal cameras, but for external cameras
267 // the info may change.
268 camera_info_.clear();
269 camera_module_->GetNumberOfCameras(
270 base::Bind(&CameraHalDelegate::OnGotNumberOfCamerasOnModuleThread, this));
271 }
272
273 void CameraHalDelegate::OnGotNumberOfCamerasOnModuleThread(
274 int32_t num_cameras) {
275 DCHECK(module_task_runner_->BelongsToCurrentThread());
276 num_cameras_ = num_cameras;
277 if (!module_callbacks_registered_) {
278 SetCallbacksOnModuleThread();
279 } else {
280 for (ssize_t camera_id = 0; camera_id < num_cameras; ++camera_id) {
281 GetCameraInfoOnModuleThread(
282 camera_id,
283 base::Bind(&CameraHalDelegate::OnGotCameraInfoOnModuleThread, this,
284 camera_id));
285 }
286 }
287 }
288
289 void CameraHalDelegate::GetCameraInfoOnModuleThread(
290 int32_t camera_id,
291 const GetCameraInfoCallback& callback) {
292 DCHECK(module_task_runner_->BelongsToCurrentThread());
293 camera_module_->GetCameraInfo(camera_id, callback);
294 }
295
296 void CameraHalDelegate::OnGotCameraInfoOnModuleThread(
297 int32_t camera_id,
298 int32_t result,
299 CameraInfoPtr camera_info) {
300 DCHECK(module_task_runner_->BelongsToCurrentThread());
301 DCHECK(!camera_info_updated_.IsSignaled());
302 if (result) {
303 // The call to get_camera_info may fail if the device is still in the
304 // enumerating state or is no longer present.
305 VLOG(1) << "Failed to get camera info. Device id: " << camera_id;
306 }
307 // In case of failure |camera_info| is empty.
308 camera_info_[camera_id] = std::move(camera_info);
309 if (camera_info_.size() == num_cameras_) {
310 updating_camera_info_ = false;
311 camera_info_updated_.Signal();
312 }
313 }
314
315 void CameraHalDelegate::OpenDeviceOnModuleThread(
316 int32_t camera_id,
317 const OpenDeviceCallback& callback) {
318 DCHECK(module_task_runner_->BelongsToCurrentThread());
319 camera_module_->OpenDevice(camera_id, callback);
320 }
321
322 void CameraHalDelegate::SetCallbacksOnModuleThread() {
323 DCHECK(module_task_runner_->BelongsToCurrentThread());
324 camera_module_->SetCallbacks(
325 camera_module_callbacks_.CreateInterfacePtrAndBind(),
326 base::Bind(&CameraHalDelegate::OnSetCallbacksOnModuleThread, this));
327 }
328
329 void CameraHalDelegate::OnSetCallbacksOnModuleThread(int32_t result) {
330 if (result) {
331 LOG(ERROR) << "Failed to set camera module callbacks";
332 return;
333 }
334 module_callbacks_registered_ = true;
335 for (size_t camera_id = 0; camera_id < num_cameras_; ++camera_id) {
336 GetCameraInfoOnModuleThread(
337 camera_id, base::Bind(&CameraHalDelegate::OnGotCameraInfoOnModuleThread,
338 this, camera_id));
339 }
340 }
341
342 // CameraModuleCallbacks implementations.
343 void CameraHalDelegate::CameraDeviceStatusChange(
344 int32_t camera_id,
345 arc::mojom::CameraDeviceStatus new_status) {
346 DCHECK(module_task_runner_->BelongsToCurrentThread());
347 // Here we just reset |camera_info_updated_| instead of trigger an update of
348 // camera info. This is to make sure that changes to |num_cameras_| and
349 // |camera_info_| are only triggered by UpdateCameraInfo.
350 if (new_status == CameraDeviceStatus::CAMERA_DEVICE_STATUS_NOT_PRESENT) {
351 if (camera_info_.find(camera_id) != camera_info_.end()) {
352 camera_info_updated_.Reset();
353 }
354 } else if (new_status == CameraDeviceStatus::CAMERA_DEVICE_STATUS_PRESENT) {
355 if (camera_info_.find(camera_id) == camera_info_.end() ||
356 camera_info_[camera_id].is_null()) {
357 camera_info_updated_.Reset();
358 }
359 }
360 }
361
362 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698