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

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: remove external camera handling in camera_hal_delegate.* 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 thread_checker_.DetachFromThread();
wuchengli 2017/05/03 06:34:00 Document we need to detach because constructor run
jcliang 2017/05/08 01:48:59 Done.
43 }
44
45 CameraHalDelegate::~CameraHalDelegate() {
46 base::WaitableEvent interface_closed(
47 base::WaitableEvent::ResetPolicy::MANUAL,
48 base::WaitableEvent::InitialState::NOT_SIGNALED);
49 module_task_runner_->PostTask(
50 FROM_HERE,
51 base::Bind(&CameraHalDelegate::ResetMojoInterfaceOnModuleThread,
52 base::Unretained(this), base::Unretained(&interface_closed)));
53 interface_closed.Wait();
54 }
55
56 bool CameraHalDelegate::StartCameraModuleIpc() {
57 // Non-blocking socket handle.
58 mojo::edk::ScopedPlatformHandle socket_handle = mojo::edk::CreateClientHandle(
59 mojo::edk::NamedPlatformHandle(kArcCamera3SocketPath));
60
61 // Set socket to blocking
62 int flags = HANDLE_EINTR(fcntl(socket_handle.get().handle, F_GETFL));
63 if (flags == -1) {
64 PLOG(ERROR) << "fcntl(F_GETFL) failed: ";
65 return false;
66 }
67 if (HANDLE_EINTR(fcntl(socket_handle.get().handle, F_SETFL,
68 flags & ~O_NONBLOCK)) == -1) {
69 PLOG(ERROR) << "fcntl(F_SETFL) failed: ";
70 return false;
71 }
72
73 char token[32] = {};
74 std::deque<mojo::edk::PlatformHandle> platform_handles;
75 ssize_t ret = mojo::edk::PlatformChannelRecvmsg(
76 socket_handle.get(), token, sizeof(token), &platform_handles, true);
77 if (ret == -1) {
78 PLOG(ERROR) << "PlatformChannelRecvmsg failed: ";
wuchengli 2017/05/03 06:33:59 return false;
jcliang 2017/05/08 01:48:59 Done.
79 }
80 if (platform_handles.size() != 1) {
81 LOG(ERROR) << "Unexpected number of handles received, expected 1: "
82 << platform_handles.size();
83 return false;
84 }
85 mojo::edk::ScopedPlatformHandle parent_pipe(platform_handles.back());
86 platform_handles.pop_back();
87 if (!parent_pipe.is_valid()) {
88 LOG(ERROR) << "Invalid parent pipe";
89 return false;
90 }
91 mojo::edk::SetParentPipeHandle(std::move(parent_pipe));
92
93 mojo::ScopedMessagePipeHandle child_pipe =
94 mojo::edk::CreateChildMessagePipe(std::string(token, 32));
wuchengli 2017/05/03 06:33:59 Use constant for 32 (also line 73).
jcliang 2017/05/08 01:48:59 Done.
95
96 camera_module_ =
97 mojo::MakeProxy(mojo::InterfacePtrInfo<arc::mojom::CameraModule>(
98 std::move(child_pipe), 0u),
99 module_task_runner_);
100
101 VLOG(1) << "Camera module IPC connection established";
102
103 return true;
104 }
105
106 std::unique_ptr<VideoCaptureDevice> CameraHalDelegate::CreateDevice(
107 const scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
108 const VideoCaptureDeviceDescriptor& device_descriptor) {
109 DCHECK(thread_checker_.CalledOnValidThread());
110 std::unique_ptr<VideoCaptureDevice> capture_device(
111 new VideoCaptureDeviceArcChromeOS(ui_task_runner, device_descriptor,
112 this));
113 return capture_device;
114 }
115
116 void CameraHalDelegate::GetSupportedFormats(
117 const VideoCaptureDeviceDescriptor& device_descriptor,
118 VideoCaptureFormats* supported_formats) {
119 DCHECK(thread_checker_.CalledOnValidThread());
120
121 if (!UpdateBuiltInCameraInfo()) {
122 return;
123 }
124 int camera_id = std::stoi(device_descriptor.device_id);
125 if (camera_info_.find(camera_id) == camera_info_.end() ||
126 camera_info_[camera_id].is_null()) {
127 // The camera may be removed already or is not ready yet.
128 VLOG(1) << "Invalid camera_id: " << camera_id;
129 return;
130 }
131 const arc::mojom::CameraInfoPtr& camera_info = camera_info_[camera_id];
132
133 const arc::mojom::CameraMetadataEntryPtr* fps_settings =
134 GetMetadataEntry(camera_info->static_camera_characteristics,
135 arc::mojom::CameraMetadataTag::
136 ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES);
wuchengli 2017/05/03 06:33:59 Should we use SCALER_AVAILABLE_MIN_FRAME_DURATIONS
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 arc::mojom::CameraMetadataEntryPtr* configurations =
156 GetMetadataEntry(camera_info->static_camera_characteristics,
157 arc::mojom::CameraMetadataTag::
158 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
159 if (!configurations) {
160 LOG(ERROR)
161 << "Failed to get available stream configurations from camera info";
162 return;
163 }
164 // The available stream configurations are stored as tuples of four int32s:
165 // (hal_pixel_format, width, height, input_or_output_type).
166 iter = reinterpret_cast<int32_t*>((*configurations)->data.data());
167 for (size_t i = 0; i < (*configurations)->count / sizeof(int32_t); ++i) {
168 arc::mojom::HalPixelFormat format =
169 static_cast<arc::mojom::HalPixelFormat>(*iter);
170 int32_t width = *(iter + 1);
171 int32_t height = *(iter + 2);
172 int32_t type = *(iter + 3);
173 iter += 4;
174 VLOG(1) << "[" << std::hex << format << " " << std::dec << width << " "
wuchengli 2017/05/03 06:34:00 DVLOG
jcliang 2017/05/08 01:48:59 Done.
175 << height << " " << type << "]";
176 VideoPixelFormat cr_format =
177 VideoCaptureDeviceArcChromeOS::PixFormatHalToChromium(format);
178 if (cr_format == PIXEL_FORMAT_UNKNOWN) {
179 continue;
180 }
181 VLOG(1) << "Supported format: " << width << "x" << height
182 << " fps=" << max_fps << " format=" << cr_format;
183 supported_formats->emplace_back(gfx::Size(width, height), max_fps,
wuchengli 2017/05/03 06:33:59 From video_capture_device_factory_linux.cc GetFram
184 cr_format);
185 }
186 }
187
188 void CameraHalDelegate::GetDeviceDescriptors(
189 VideoCaptureDeviceDescriptors* device_descriptors) {
190 DCHECK(thread_checker_.CalledOnValidThread());
191
192 if (!UpdateBuiltInCameraInfo()) {
193 return;
194 }
195 for (size_t camera_id = 0; camera_id < num_builtin_cameras_; ++camera_id) {
196 VideoCaptureDeviceDescriptor desc;
197 arc::mojom::CameraInfoPtr& camera_info = camera_info_[camera_id];
198 if (!camera_info) {
199 continue;
200 }
201 desc.device_id = std::to_string(camera_id);
202 desc.capture_api = VideoCaptureApi::ANDROID_API2_LIMITED;
wuchengli 2017/05/03 06:34:00 Chrome media team may know what |capture_api| is f
jcliang 2017/05/08 01:48:58 I checked with chfremer@ and we can either declare
203 desc.transport_type = VideoCaptureTransportType::OTHER_TRANSPORT;
204 switch (camera_info->facing) {
205 case arc::mojom::CameraFacing::CAMERA_FACING_BACK:
206 desc.facing = VideoFacingMode::MEDIA_VIDEO_FACING_ENVIRONMENT;
207 desc.display_name = std::string("Back Camera");
wuchengli 2017/05/03 06:34:00 What's display_name is used for? Should we follow
208 break;
209 case arc::mojom::CameraFacing::CAMERA_FACING_FRONT:
210 desc.facing = VideoFacingMode::MEDIA_VIDEO_FACING_USER;
211 desc.display_name = std::string("Front Camera");
212 break;
213 case arc::mojom::CameraFacing::CAMERA_FACING_EXTERNAL:
214 desc.facing = VideoFacingMode::MEDIA_VIDEO_FACING_NONE;
215 desc.display_name = std::string("External Camera");
216 break;
wuchengli 2017/05/03 06:33:59 Document mojo makes sure |facing| is valid.
jcliang 2017/05/08 01:48:59 Done.
217 }
218 device_descriptors->push_back(desc);
219 }
220 }
221
222 void CameraHalDelegate::GetCameraInfo(int32_t camera_id,
223 const GetCameraInfoCallback& callback) {
224 // This method may be called on any thread. Currently this method is used by
225 // VideoCaptureDeviceArcChromeOS to query camera info.
226 module_task_runner_->PostTask(
227 FROM_HERE, base::Bind(&CameraHalDelegate::GetCameraInfoOnModuleThread,
228 this, camera_id, callback));
229 }
230
231 void CameraHalDelegate::OpenDevice(int32_t camera_id,
232 const OpenDeviceCallback& callback) {
233 // This method may be called on any thread. Currently this method is used by
234 // VideoCaptureDeviceArcChromeOS to open a camera device.
235 module_task_runner_->PostTask(
236 FROM_HERE, base::Bind(&CameraHalDelegate::OpenDeviceOnModuleThread, this,
237 camera_id, callback));
238 }
239
240 void CameraHalDelegate::ResetMojoInterfaceOnModuleThread(
241 base::WaitableEvent* interface_closed) {
242 DCHECK(module_task_runner_->BelongsToCurrentThread());
243 camera_module_.reset();
244 if (camera_module_callbacks_.is_bound()) {
245 camera_module_callbacks_.Unbind();
246 }
247 interface_closed->Signal();
248 }
249
250 bool CameraHalDelegate::UpdateBuiltInCameraInfo() {
251 DCHECK(thread_checker_.CalledOnValidThread());
252 if (builtin_camera_info_updated_.IsSignaled()) {
253 return true;
254 }
255 // The built-in camera are static per specification of the Android camera HAL
256 // v3 specification. We only update the built-in camera info once.
257 DCHECK(!builtin_camera_info_updated_.IsSignaled());
wuchengli 2017/05/03 06:33:59 remove because line 252 just checks it.
jcliang 2017/05/08 01:48:59 Done.
258 module_task_runner_->PostTask(
259 FROM_HERE,
260 base::Bind(&CameraHalDelegate::UpdateBuiltInCameraInfoOnModuleThread,
261 this));
262 if (!builtin_camera_info_updated_.TimedWait(kEventWaitTimeoutMs)) {
263 LOG(ERROR) << "Timed out getting camera info";
264 return false;
265 }
266 return true;
267 }
268
269 void CameraHalDelegate::UpdateBuiltInCameraInfoOnModuleThread() {
270 DCHECK(module_task_runner_->BelongsToCurrentThread());
271 camera_module_->GetNumberOfCameras(
272 base::Bind(&CameraHalDelegate::OnGotNumberOfCamerasOnModuleThread, this));
273 }
274
275 void CameraHalDelegate::OnGotNumberOfCamerasOnModuleThread(
276 int32_t num_cameras) {
277 DCHECK(module_task_runner_->BelongsToCurrentThread());
wuchengli 2017/05/03 06:33:59 Do not set num_builtin_cameras_ if num_cameras < 0
jcliang 2017/05/08 01:48:59 Done.
278 num_builtin_cameras_ = num_cameras;
wuchengli 2017/05/03 06:34:00 Add VLOG to print the number of cameras here so it
jcliang 2017/05/08 01:48:58 Done.
279 // Per camera HAL v3 specification SetCallbacks() should be called after the
280 // first time GetNumberOfCameras() is called, and before other CameraModule
281 // functions are called.
282 camera_module_->SetCallbacks(
283 camera_module_callbacks_.CreateInterfacePtrAndBind(),
284 base::Bind(&CameraHalDelegate::OnSetCallbacksOnModuleThread, this));
285 }
286
287 void CameraHalDelegate::OnSetCallbacksOnModuleThread(int32_t result) {
288 DCHECK(module_task_runner_->BelongsToCurrentThread());
289 if (result) {
290 num_builtin_cameras_ = 0;
291 builtin_camera_info_updated_.Signal();
292 LOG(ERROR) << "Failed to set camera module callbacks";
wuchengli 2017/05/03 06:34:00 Print |result| if it contains an error code.
jcliang 2017/05/08 01:48:59 Done.
293 return;
294 }
295 for (size_t camera_id = 0; camera_id < num_builtin_cameras_; ++camera_id) {
296 GetCameraInfoOnModuleThread(
297 camera_id, base::Bind(&CameraHalDelegate::OnGotCameraInfoOnModuleThread,
298 this, camera_id));
299 }
300 }
301
302 void CameraHalDelegate::GetCameraInfoOnModuleThread(
303 int32_t camera_id,
304 const GetCameraInfoCallback& callback) {
305 DCHECK(module_task_runner_->BelongsToCurrentThread());
306 camera_module_->GetCameraInfo(camera_id, callback);
307 }
308
309 void CameraHalDelegate::OnGotCameraInfoOnModuleThread(
310 int32_t camera_id,
311 int32_t result,
312 arc::mojom::CameraInfoPtr camera_info) {
313 DCHECK(module_task_runner_->BelongsToCurrentThread());
wuchengli 2017/05/03 06:33:59 Add DVLOG to print camera_id so it's easier to deb
jcliang 2017/05/08 01:48:58 Done.
314 if (result) {
315 // The call to get_camera_info may fail if the device is still in the
316 // enumerating state or is no longer present.
317 VLOG(1) << "Failed to get camera info. Device id: " << camera_id;
wuchengli 2017/05/03 06:33:59 s/Device/Camera/ Print |result| if it contains an
jcliang 2017/05/08 01:48:58 Done.
318 }
319 // In case of failure |camera_info| is empty.
320 camera_info_[camera_id] = std::move(camera_info);
321 if (!builtin_camera_info_updated_.IsSignaled()) {
322 // Make sure all the built-in camera info is set. |camera_info_| may hold
323 // info of external cameras as well so we need to check each id of the
324 // built-in cameras here.
325 for (size_t id = 0; id < num_builtin_cameras_; ++id) {
326 if (camera_info_.find(id) == camera_info_.end()) {
327 return;
328 }
329 }
330 builtin_camera_info_updated_.Signal();
331 }
332 }
333
334 void CameraHalDelegate::OpenDeviceOnModuleThread(
335 int32_t camera_id,
336 const OpenDeviceCallback& callback) {
337 DCHECK(module_task_runner_->BelongsToCurrentThread());
338 camera_module_->OpenDevice(camera_id, callback);
339 }
340
341 // CameraModuleCallbacks implementations.
342 void CameraHalDelegate::CameraDeviceStatusChange(
343 int32_t camera_id,
344 arc::mojom::CameraDeviceStatus new_status) {
345 DCHECK(module_task_runner_->BelongsToCurrentThread());
346 // TODO(jcliang): Handle status change for external cameras.
347 }
348
349 } // namespace media
OLDNEW
« no previous file with comments | « media/capture/video/chromeos/camera_hal_delegate.h ('k') | media/capture/video/chromeos/camera_metadata_utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698