blob: 4ad176891011c92e7fc5d19c2bced9800ebbb7d9 [file] [log] [blame]
Yin-Chia Yeh19030592017-10-19 17:30:11 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "ExtCamDev@3.4"
18#define LOG_NDEBUG 0
19#include <log/log.h>
20
21#include <array>
22#include <linux/videodev2.h>
23#include "android-base/macros.h"
24#include "CameraMetadata.h"
25#include "../../3.2/default/include/convert.h"
26#include "ExternalCameraDevice_3_4.h"
27
28
29namespace android {
30namespace hardware {
31namespace camera {
32namespace device {
33namespace V3_4 {
34namespace implementation {
35
36namespace {
37// Only support MJPEG for now as it seems to be the one supports higher fps
38// Other formats to consider in the future:
39// * V4L2_PIX_FMT_YVU420 (== YV12)
40// * V4L2_PIX_FMT_YVYU (YVYU: can be converted to YV12 or other YUV420_888 formats)
41const std::array<uint32_t, /*size*/1> kSupportedFourCCs {{
42 V4L2_PIX_FMT_MJPEG
43}}; // double braces required in C++11
44
45// TODO: b/72261897
46// Define max size/fps this Android device can advertise (and streaming at reasonable speed)
47// Also make sure that can be done without editing source code
48
49// TODO: b/72261675: make it dynamic since this affects memory usage
50const int kMaxJpegSize = {13 * 1024 * 1024}; // 13MB
51} // anonymous namespace
52
53ExternalCameraDevice::ExternalCameraDevice(const std::string& cameraId) :
54 mCameraId(cameraId) {
55 status_t ret = initCameraCharacteristics();
56 if (ret != OK) {
57 ALOGE("%s: init camera characteristics failed: errorno %d", __FUNCTION__, ret);
58 mInitFailed = true;
59 }
60}
61
62ExternalCameraDevice::~ExternalCameraDevice() {}
63
64bool ExternalCameraDevice::isInitFailed() {
65 return mInitFailed;
66}
67
68Return<void> ExternalCameraDevice::getResourceCost(getResourceCost_cb _hidl_cb) {
69 CameraResourceCost resCost;
70 resCost.resourceCost = 100;
71 _hidl_cb(Status::OK, resCost);
72 return Void();
73}
74
75Return<void> ExternalCameraDevice::getCameraCharacteristics(
76 getCameraCharacteristics_cb _hidl_cb) {
77 Mutex::Autolock _l(mLock);
78 V3_2::CameraMetadata hidlChars;
79
80 if (isInitFailed()) {
81 _hidl_cb(Status::INTERNAL_ERROR, hidlChars);
82 return Void();
83 }
84
85 const camera_metadata_t* rawMetadata = mCameraCharacteristics.getAndLock();
86 V3_2::implementation::convertToHidl(rawMetadata, &hidlChars);
87 _hidl_cb(Status::OK, hidlChars);
88 mCameraCharacteristics.unlock(rawMetadata);
89 return Void();
90}
91
92Return<Status> ExternalCameraDevice::setTorchMode(TorchMode) {
93 return Status::METHOD_NOT_SUPPORTED;
94}
95
96Return<void> ExternalCameraDevice::open(
97 const sp<ICameraDeviceCallback>& callback, open_cb _hidl_cb) {
98 Status status = Status::OK;
99 sp<ExternalCameraDeviceSession> session = nullptr;
100
101 if (callback == nullptr) {
102 ALOGE("%s: cannot open camera %s. callback is null!",
103 __FUNCTION__, mCameraId.c_str());
104 _hidl_cb(Status::ILLEGAL_ARGUMENT, nullptr);
105 return Void();
106 }
107
108 if (isInitFailed()) {
109 ALOGE("%s: cannot open camera %s. camera init failed!",
110 __FUNCTION__, mCameraId.c_str());
111 _hidl_cb(Status::INTERNAL_ERROR, nullptr);
112 return Void();
113 }
114
115 mLock.lock();
116
117 ALOGV("%s: Initializing device for camera %s", __FUNCTION__, mCameraId.c_str());
118 session = mSession.promote();
119 if (session != nullptr && !session->isClosed()) {
120 ALOGE("%s: cannot open an already opened camera!", __FUNCTION__);
121 mLock.unlock();
122 _hidl_cb(Status::CAMERA_IN_USE, nullptr);
123 return Void();
124 }
125
126 unique_fd fd(::open(mCameraId.c_str(), O_RDWR));
127 if (fd.get() < 0) {
128 ALOGE("%s: v4l2 device open %s failed: %s",
129 __FUNCTION__, mCameraId.c_str(), strerror(errno));
130 mLock.unlock();
131 _hidl_cb(Status::INTERNAL_ERROR, nullptr);
132 return Void();
133 }
134
135 session = new ExternalCameraDeviceSession(
136 callback, mSupportedFormats, mCameraCharacteristics, std::move(fd));
137 if (session == nullptr) {
138 ALOGE("%s: camera device session allocation failed", __FUNCTION__);
139 mLock.unlock();
140 _hidl_cb(Status::INTERNAL_ERROR, nullptr);
141 return Void();
142 }
143 if (session->isInitFailed()) {
144 ALOGE("%s: camera device session init failed", __FUNCTION__);
145 session = nullptr;
146 mLock.unlock();
147 _hidl_cb(Status::INTERNAL_ERROR, nullptr);
148 return Void();
149 }
150 mSession = session;
151
152 mLock.unlock();
153
154 _hidl_cb(status, session->getInterface());
155 return Void();
156}
157
158Return<void> ExternalCameraDevice::dumpState(const ::android::hardware::hidl_handle& handle) {
159 Mutex::Autolock _l(mLock);
160 if (handle.getNativeHandle() == nullptr) {
161 ALOGE("%s: handle must not be null", __FUNCTION__);
162 return Void();
163 }
164 if (handle->numFds != 1 || handle->numInts != 0) {
165 ALOGE("%s: handle must contain 1 FD and 0 integers! Got %d FDs and %d ints",
166 __FUNCTION__, handle->numFds, handle->numInts);
167 return Void();
168 }
169 int fd = handle->data[0];
170 if (mSession == nullptr) {
171 dprintf(fd, "No active camera device session instance\n");
172 return Void();
173 }
174 auto session = mSession.promote();
175 if (session == nullptr) {
176 dprintf(fd, "No active camera device session instance\n");
177 return Void();
178 }
179 // Call into active session to dump states
180 session->dumpState(handle);
181 return Void();
182}
183
184
185status_t ExternalCameraDevice::initCameraCharacteristics() {
186 if (mCameraCharacteristics.isEmpty()) {
187 // init camera characteristics
188 unique_fd fd(::open(mCameraId.c_str(), O_RDWR));
189 if (fd.get() < 0) {
190 ALOGE("%s: v4l2 device open %s failed", __FUNCTION__, mCameraId.c_str());
191 return DEAD_OBJECT;
192 }
193
194 status_t ret;
195 ret = initDefaultCharsKeys(&mCameraCharacteristics);
196 if (ret != OK) {
197 ALOGE("%s: init default characteristics key failed: errorno %d", __FUNCTION__, ret);
198 mCameraCharacteristics.clear();
199 return ret;
200 }
201
202 ret = initCameraControlsCharsKeys(fd.get(), &mCameraCharacteristics);
203 if (ret != OK) {
204 ALOGE("%s: init camera control characteristics key failed: errorno %d", __FUNCTION__, ret);
205 mCameraCharacteristics.clear();
206 return ret;
207 }
208
209 ret = initOutputCharsKeys(fd.get(), &mCameraCharacteristics);
210 if (ret != OK) {
211 ALOGE("%s: init output characteristics key failed: errorno %d", __FUNCTION__, ret);
212 mCameraCharacteristics.clear();
213 return ret;
214 }
215 }
216 return OK;
217}
218
219#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
220#define UPDATE(tag, data, size) \
221do { \
222 if (metadata->update((tag), (data), (size))) { \
223 ALOGE("Update " #tag " failed!"); \
224 return -EINVAL; \
225 } \
226} while (0)
227
228status_t ExternalCameraDevice::initDefaultCharsKeys(
229 ::android::hardware::camera::common::V1_0::helper::CameraMetadata* metadata) {
230 // TODO: changed to HARDWARELEVEL_EXTERNAL later
231 const uint8_t hardware_level = ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED;
232 UPDATE(ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL, &hardware_level, 1);
233
234 // android.colorCorrection
235 const uint8_t availableAberrationModes[] = {
236 ANDROID_COLOR_CORRECTION_ABERRATION_MODE_OFF};
237 UPDATE(ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES,
238 availableAberrationModes, ARRAY_SIZE(availableAberrationModes));
239
240 // android.control
241 const uint8_t antibandingMode =
242 ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO;
243 UPDATE(ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES,
244 &antibandingMode, 1);
245
246 const int32_t controlMaxRegions[] = {/*AE*/ 0, /*AWB*/ 0, /*AF*/ 0};
247 UPDATE(ANDROID_CONTROL_MAX_REGIONS, controlMaxRegions,
248 ARRAY_SIZE(controlMaxRegions));
249
250 const uint8_t videoStabilizationMode =
251 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF;
252 UPDATE(ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES,
253 &videoStabilizationMode, 1);
254
255 const uint8_t awbAvailableMode = ANDROID_CONTROL_AWB_MODE_AUTO;
256 UPDATE(ANDROID_CONTROL_AWB_AVAILABLE_MODES, &awbAvailableMode, 1);
257
258 const uint8_t aeAvailableMode = ANDROID_CONTROL_AE_MODE_ON;
259 UPDATE(ANDROID_CONTROL_AE_AVAILABLE_MODES, &aeAvailableMode, 1);
260
261 const uint8_t availableFffect = ANDROID_CONTROL_EFFECT_MODE_OFF;
262 UPDATE(ANDROID_CONTROL_AVAILABLE_EFFECTS, &availableFffect, 1);
263
264 const uint8_t controlAvailableModes[] = {ANDROID_CONTROL_MODE_OFF,
265 ANDROID_CONTROL_MODE_AUTO};
266 UPDATE(ANDROID_CONTROL_AVAILABLE_MODES, controlAvailableModes,
267 ARRAY_SIZE(controlAvailableModes));
268
269 // android.edge
270 const uint8_t edgeMode = ANDROID_EDGE_MODE_OFF;
271 UPDATE(ANDROID_EDGE_AVAILABLE_EDGE_MODES, &edgeMode, 1);
272
273 // android.flash
274 const uint8_t flashInfo = ANDROID_FLASH_INFO_AVAILABLE_FALSE;
275 UPDATE(ANDROID_FLASH_INFO_AVAILABLE, &flashInfo, 1);
276
277 // android.hotPixel
278 const uint8_t hotPixelMode = ANDROID_HOT_PIXEL_MODE_OFF;
279 UPDATE(ANDROID_HOT_PIXEL_AVAILABLE_HOT_PIXEL_MODES, &hotPixelMode, 1);
280
281 // android.jpeg
282 // TODO: b/72261675 See if we can provide thumbnail size for all jpeg aspect ratios
283 const int32_t jpegAvailableThumbnailSizes[] = {0, 0, 240, 180};
284 UPDATE(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES, jpegAvailableThumbnailSizes,
285 ARRAY_SIZE(jpegAvailableThumbnailSizes));
286
287 const int32_t jpegMaxSize = kMaxJpegSize;
288 UPDATE(ANDROID_JPEG_MAX_SIZE, &jpegMaxSize, 1);
289
290 const uint8_t jpegQuality = 90;
291 UPDATE(ANDROID_JPEG_QUALITY, &jpegQuality, 1);
292 UPDATE(ANDROID_JPEG_THUMBNAIL_QUALITY, &jpegQuality, 1);
293
294 const int32_t jpegOrientation = 0;
295 UPDATE(ANDROID_JPEG_ORIENTATION, &jpegOrientation, 1);
296
297 // android.lens
298 const uint8_t focusDistanceCalibration =
299 ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION_UNCALIBRATED;
300 UPDATE(ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION, &focusDistanceCalibration, 1);
301
302 const uint8_t opticalStabilizationMode =
303 ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF;
304 UPDATE(ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION,
305 &opticalStabilizationMode, 1);
306
307 const uint8_t facing = ANDROID_LENS_FACING_EXTERNAL;
308 UPDATE(ANDROID_LENS_FACING, &facing, 1);
309
310 // android.noiseReduction
311 const uint8_t noiseReductionMode = ANDROID_NOISE_REDUCTION_MODE_OFF;
312 UPDATE(ANDROID_NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES,
313 &noiseReductionMode, 1);
314 UPDATE(ANDROID_NOISE_REDUCTION_MODE, &noiseReductionMode, 1);
315
316 // android.request
317 const uint8_t availableCapabilities[] = {
318 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE};
319 UPDATE(ANDROID_REQUEST_AVAILABLE_CAPABILITIES, availableCapabilities,
320 ARRAY_SIZE(availableCapabilities));
321
322 const int32_t partialResultCount = 1;
323 UPDATE(ANDROID_REQUEST_PARTIAL_RESULT_COUNT, &partialResultCount, 1);
324
325 // This means pipeline latency of X frame intervals. The maximum number is 4.
326 const uint8_t requestPipelineMaxDepth = 4;
327 UPDATE(ANDROID_REQUEST_PIPELINE_MAX_DEPTH, &requestPipelineMaxDepth, 1);
328 UPDATE(ANDROID_REQUEST_PIPELINE_DEPTH, &requestPipelineMaxDepth, 1);
329
330 // Three numbers represent the maximum numbers of different types of output
331 // streams simultaneously. The types are raw sensor, processed (but not
332 // stalling), and processed (but stalling). For usb limited mode, raw sensor
333 // is not supported. Stalling stream is JPEG. Non-stalling streams are
334 // YUV_420_888 or YV12.
335 const int32_t requestMaxNumOutputStreams[] = {
336 /*RAW*/0,
337 /*Processed*/ExternalCameraDeviceSession::kMaxProcessedStream,
338 /*Stall*/ExternalCameraDeviceSession::kMaxStallStream};
339 UPDATE(ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS, requestMaxNumOutputStreams,
340 ARRAY_SIZE(requestMaxNumOutputStreams));
341
342 // Limited mode doesn't support reprocessing.
343 const int32_t requestMaxNumInputStreams = 0;
344 UPDATE(ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS, &requestMaxNumInputStreams,
345 1);
346
347 // android.scaler
348 // TODO: b/72263447 V4L2_CID_ZOOM_*
349 const float scalerAvailableMaxDigitalZoom[] = {1};
350 UPDATE(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
351 scalerAvailableMaxDigitalZoom,
352 ARRAY_SIZE(scalerAvailableMaxDigitalZoom));
353
354 const uint8_t croppingType = ANDROID_SCALER_CROPPING_TYPE_CENTER_ONLY;
355 UPDATE(ANDROID_SCALER_CROPPING_TYPE, &croppingType, 1);
356
357 const int32_t testPatternModes[] = {
358 ANDROID_SENSOR_TEST_PATTERN_MODE_OFF};
359 UPDATE(ANDROID_SENSOR_AVAILABLE_TEST_PATTERN_MODES, testPatternModes,
360 ARRAY_SIZE(testPatternModes));
361 UPDATE(ANDROID_SENSOR_TEST_PATTERN_MODE, &testPatternModes[0], 1);
362
363 const uint8_t timestampSource = ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN;
364 UPDATE(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE, &timestampSource, 1);
365
366 // Orientation probably isn't useful for external facing camera?
367 const int32_t orientation = 0;
368 UPDATE(ANDROID_SENSOR_ORIENTATION, &orientation, 1);
369
370 // android.shading
371 const uint8_t availabeMode = ANDROID_SHADING_MODE_OFF;
372 UPDATE(ANDROID_SHADING_AVAILABLE_MODES, &availabeMode, 1);
373
374 // android.statistics
375 const uint8_t faceDetectMode = ANDROID_STATISTICS_FACE_DETECT_MODE_OFF;
376 UPDATE(ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES, &faceDetectMode,
377 1);
378
379 const int32_t maxFaceCount = 0;
380 UPDATE(ANDROID_STATISTICS_INFO_MAX_FACE_COUNT, &maxFaceCount, 1);
381
382 const uint8_t availableHotpixelMode =
383 ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF;
384 UPDATE(ANDROID_STATISTICS_INFO_AVAILABLE_HOT_PIXEL_MAP_MODES,
385 &availableHotpixelMode, 1);
386
387 const uint8_t lensShadingMapMode =
388 ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF;
389 UPDATE(ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES,
390 &lensShadingMapMode, 1);
391
392 // android.sync
393 const int32_t maxLatency = ANDROID_SYNC_MAX_LATENCY_UNKNOWN;
394 UPDATE(ANDROID_SYNC_MAX_LATENCY, &maxLatency, 1);
395
396 /* Other sensor/RAW realted keys:
397 * android.sensor.info.colorFilterArrangement -> no need if we don't do RAW
398 * android.sensor.info.physicalSize -> not available
399 * android.sensor.info.whiteLevel -> not available/not needed
400 * android.sensor.info.lensShadingApplied -> not needed
401 * android.sensor.info.preCorrectionActiveArraySize -> not available/not needed
402 * android.sensor.blackLevelPattern -> not available/not needed
403 */
404
405 const int32_t availableRequestKeys[] = {
406 ANDROID_COLOR_CORRECTION_ABERRATION_MODE,
407 ANDROID_CONTROL_AE_ANTIBANDING_MODE,
408 ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION,
409 ANDROID_CONTROL_AE_LOCK,
410 ANDROID_CONTROL_AE_MODE,
411 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
412 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
413 ANDROID_CONTROL_AF_MODE,
414 ANDROID_CONTROL_AF_TRIGGER,
415 ANDROID_CONTROL_AWB_LOCK,
416 ANDROID_CONTROL_AWB_MODE,
417 ANDROID_CONTROL_CAPTURE_INTENT,
418 ANDROID_CONTROL_EFFECT_MODE,
419 ANDROID_CONTROL_MODE,
420 ANDROID_CONTROL_SCENE_MODE,
421 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE,
422 ANDROID_FLASH_MODE,
423 ANDROID_JPEG_ORIENTATION,
424 ANDROID_JPEG_QUALITY,
425 ANDROID_JPEG_THUMBNAIL_QUALITY,
426 ANDROID_JPEG_THUMBNAIL_SIZE,
427 ANDROID_LENS_OPTICAL_STABILIZATION_MODE,
428 ANDROID_NOISE_REDUCTION_MODE,
429 ANDROID_SCALER_CROP_REGION,
430 ANDROID_SENSOR_TEST_PATTERN_MODE,
431 ANDROID_STATISTICS_FACE_DETECT_MODE,
432 ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE};
433 UPDATE(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS, availableRequestKeys,
434 ARRAY_SIZE(availableRequestKeys));
435
436 const int32_t availableResultKeys[] = {
437 ANDROID_COLOR_CORRECTION_ABERRATION_MODE,
438 ANDROID_CONTROL_AE_ANTIBANDING_MODE,
439 ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION,
440 ANDROID_CONTROL_AE_LOCK,
441 ANDROID_CONTROL_AE_MODE,
442 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
443 ANDROID_CONTROL_AE_STATE,
444 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
445 ANDROID_CONTROL_AF_MODE,
446 ANDROID_CONTROL_AF_STATE,
447 ANDROID_CONTROL_AF_TRIGGER,
448 ANDROID_CONTROL_AWB_LOCK,
449 ANDROID_CONTROL_AWB_MODE,
450 ANDROID_CONTROL_AWB_STATE,
451 ANDROID_CONTROL_CAPTURE_INTENT,
452 ANDROID_CONTROL_EFFECT_MODE,
453 ANDROID_CONTROL_MODE,
454 ANDROID_CONTROL_SCENE_MODE,
455 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE,
456 ANDROID_FLASH_MODE,
457 ANDROID_FLASH_STATE,
458 ANDROID_JPEG_ORIENTATION,
459 ANDROID_JPEG_QUALITY,
460 ANDROID_JPEG_THUMBNAIL_QUALITY,
461 ANDROID_JPEG_THUMBNAIL_SIZE,
462 ANDROID_LENS_OPTICAL_STABILIZATION_MODE,
463 ANDROID_NOISE_REDUCTION_MODE,
464 ANDROID_REQUEST_PIPELINE_DEPTH,
465 ANDROID_SCALER_CROP_REGION,
466 ANDROID_SENSOR_TIMESTAMP,
467 ANDROID_STATISTICS_FACE_DETECT_MODE,
468 ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE,
469 ANDROID_STATISTICS_LENS_SHADING_MAP_MODE,
470 ANDROID_STATISTICS_SCENE_FLICKER};
471 UPDATE(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS, availableResultKeys,
472 ARRAY_SIZE(availableResultKeys));
473
474 const int32_t availableCharacteristicsKeys[] = {
475 ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES,
476 ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES,
477 ANDROID_CONTROL_AE_AVAILABLE_MODES,
478 ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
479 ANDROID_CONTROL_AE_COMPENSATION_RANGE,
480 ANDROID_CONTROL_AE_COMPENSATION_STEP,
481 ANDROID_CONTROL_AE_LOCK_AVAILABLE,
482 ANDROID_CONTROL_AF_AVAILABLE_MODES,
483 ANDROID_CONTROL_AVAILABLE_EFFECTS,
484 ANDROID_CONTROL_AVAILABLE_MODES,
485 ANDROID_CONTROL_AVAILABLE_SCENE_MODES,
486 ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES,
487 ANDROID_CONTROL_AWB_AVAILABLE_MODES,
488 ANDROID_CONTROL_AWB_LOCK_AVAILABLE,
489 ANDROID_CONTROL_MAX_REGIONS,
490 ANDROID_FLASH_INFO_AVAILABLE,
491 ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL,
492 ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES,
493 ANDROID_LENS_FACING,
494 ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION,
495 ANDROID_LENS_INFO_FOCUS_DISTANCE_CALIBRATION,
496 ANDROID_NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES,
497 ANDROID_REQUEST_AVAILABLE_CAPABILITIES,
498 ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS,
499 ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS,
500 ANDROID_REQUEST_PARTIAL_RESULT_COUNT,
501 ANDROID_REQUEST_PIPELINE_MAX_DEPTH,
502 ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
503 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
504 ANDROID_SCALER_CROPPING_TYPE,
505 ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
506 ANDROID_SENSOR_INFO_MAX_FRAME_DURATION,
507 ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE,
508 ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE,
509 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE,
510 ANDROID_SENSOR_ORIENTATION,
511 ANDROID_SHADING_AVAILABLE_MODES,
512 ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES,
513 ANDROID_STATISTICS_INFO_AVAILABLE_HOT_PIXEL_MAP_MODES,
514 ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES,
515 ANDROID_STATISTICS_INFO_MAX_FACE_COUNT,
516 ANDROID_SYNC_MAX_LATENCY};
517 UPDATE(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS,
518 availableCharacteristicsKeys,
519 ARRAY_SIZE(availableCharacteristicsKeys));
520
521 return OK;
522}
523
524status_t ExternalCameraDevice::initCameraControlsCharsKeys(int,
525 ::android::hardware::camera::common::V1_0::helper::CameraMetadata* metadata) {
526 /**
527 * android.sensor.info.sensitivityRange -> V4L2_CID_ISO_SENSITIVITY
528 * android.sensor.info.exposureTimeRange -> V4L2_CID_EXPOSURE_ABSOLUTE
529 * android.sensor.info.maxFrameDuration -> TBD
530 * android.lens.info.minimumFocusDistance -> V4L2_CID_FOCUS_ABSOLUTE
531 * android.lens.info.hyperfocalDistance
532 * android.lens.info.availableFocalLengths -> not available?
533 */
534
535 // android.control
536 // No AE compensation support for now.
537 // TODO: V4L2_CID_EXPOSURE_BIAS
538 const int32_t controlAeCompensationRange[] = {0, 0};
539 UPDATE(ANDROID_CONTROL_AE_COMPENSATION_RANGE, controlAeCompensationRange,
540 ARRAY_SIZE(controlAeCompensationRange));
541 const camera_metadata_rational_t controlAeCompensationStep[] = {{0, 1}};
542 UPDATE(ANDROID_CONTROL_AE_COMPENSATION_STEP, controlAeCompensationStep,
543 ARRAY_SIZE(controlAeCompensationStep));
544
545
546 // TODO: Check V4L2_CID_AUTO_FOCUS_*.
547 const uint8_t afAvailableModes[] = {ANDROID_CONTROL_AF_MODE_AUTO,
548 ANDROID_CONTROL_AF_MODE_OFF};
549 UPDATE(ANDROID_CONTROL_AF_AVAILABLE_MODES, afAvailableModes,
550 ARRAY_SIZE(afAvailableModes));
551
552 // TODO: V4L2_CID_SCENE_MODE
553 const uint8_t availableSceneMode = ANDROID_CONTROL_SCENE_MODE_DISABLED;
554 UPDATE(ANDROID_CONTROL_AVAILABLE_SCENE_MODES, &availableSceneMode, 1);
555
556 // TODO: V4L2_CID_3A_LOCK
557 const uint8_t aeLockAvailable = ANDROID_CONTROL_AE_LOCK_AVAILABLE_FALSE;
558 UPDATE(ANDROID_CONTROL_AE_LOCK_AVAILABLE, &aeLockAvailable, 1);
559 const uint8_t awbLockAvailable = ANDROID_CONTROL_AWB_LOCK_AVAILABLE_FALSE;
560 UPDATE(ANDROID_CONTROL_AWB_LOCK_AVAILABLE, &awbLockAvailable, 1);
561
562 // TODO: V4L2_CID_ZOOM_*
563 const float scalerAvailableMaxDigitalZoom[] = {1};
564 UPDATE(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
565 scalerAvailableMaxDigitalZoom,
566 ARRAY_SIZE(scalerAvailableMaxDigitalZoom));
567
568 return OK;
569}
570
571status_t ExternalCameraDevice::initOutputCharsKeys(int fd,
572 ::android::hardware::camera::common::V1_0::helper::CameraMetadata* metadata) {
573 initSupportedFormatsLocked(fd);
574 if (mSupportedFormats.empty()) {
575 ALOGE("%s: Init supported format list failed", __FUNCTION__);
576 return UNKNOWN_ERROR;
577 }
578
579 std::vector<int32_t> streamConfigurations;
580 std::vector<int64_t> minFrameDurations;
581 std::vector<int64_t> stallDurations;
582 int64_t maxFrameDuration = 0;
583 int32_t maxFps = std::numeric_limits<int32_t>::min();
584 int32_t minFps = std::numeric_limits<int32_t>::max();
585 std::set<int32_t> framerates;
586
587 std::array<int, /*size*/3> halFormats{{
588 HAL_PIXEL_FORMAT_BLOB,
589 HAL_PIXEL_FORMAT_YCbCr_420_888,
590 HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED}};
591
592 for (const auto& supportedFormat : mSupportedFormats) {
593 for (const auto& format : halFormats) {
594 streamConfigurations.push_back(format);
595 streamConfigurations.push_back(supportedFormat.width);
596 streamConfigurations.push_back(supportedFormat.height);
597 streamConfigurations.push_back(
598 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT);
599 }
600
601 int64_t min_frame_duration = std::numeric_limits<int64_t>::max();
602 for (const auto& frameRate : supportedFormat.frameRates) {
603 int64_t frame_duration = 1000000000LL / frameRate;
604 if (frame_duration < min_frame_duration) {
605 min_frame_duration = frame_duration;
606 }
607 if (frame_duration > maxFrameDuration) {
608 maxFrameDuration = frame_duration;
609 }
610 int32_t frameRateInt = static_cast<int32_t>(frameRate);
611 if (minFps > frameRateInt) {
612 minFps = frameRateInt;
613 }
614 if (maxFps < frameRateInt) {
615 maxFps = frameRateInt;
616 }
617 framerates.insert(frameRateInt);
618 }
619
620 for (const auto& format : halFormats) {
621 minFrameDurations.push_back(format);
622 minFrameDurations.push_back(supportedFormat.width);
623 minFrameDurations.push_back(supportedFormat.height);
624 minFrameDurations.push_back(min_frame_duration);
625 }
626
627 // The stall duration is 0 for non-jpeg formats. For JPEG format, stall
628 // duration can be 0 if JPEG is small. Here we choose 1 sec for JPEG.
629 // TODO: b/72261675. Maybe set this dynamically
630 for (const auto& format : halFormats) {
631 const int64_t NS_TO_SECOND = 1000000000;
632 int64_t stall_duration =
633 (format == HAL_PIXEL_FORMAT_BLOB) ? NS_TO_SECOND : 0;
634 stallDurations.push_back(format);
635 stallDurations.push_back(supportedFormat.width);
636 stallDurations.push_back(supportedFormat.height);
637 stallDurations.push_back(stall_duration);
638 }
639 }
640
641 // The document in aeAvailableTargetFpsRanges section says the minFps should
642 // not be larger than 15.
643 // We cannot support fixed 30fps but Android requires (min, max) and
644 // (max, max) ranges.
645 // TODO: populate more, right now this does not support 30,30 if the device
646 // has higher than 30 fps modes
647 std::vector<int32_t> fpsRanges;
648 // Variable range
649 fpsRanges.push_back(minFps);
650 fpsRanges.push_back(maxFps);
651 // Fixed ranges
652 for (const auto& framerate : framerates) {
653 fpsRanges.push_back(framerate);
654 fpsRanges.push_back(framerate);
655 }
656 UPDATE(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES, fpsRanges.data(),
657 fpsRanges.size());
658
659 UPDATE(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS,
660 streamConfigurations.data(), streamConfigurations.size());
661
662 UPDATE(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS,
663 minFrameDurations.data(), minFrameDurations.size());
664
665 UPDATE(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS, stallDurations.data(),
666 stallDurations.size());
667
668 UPDATE(ANDROID_SENSOR_INFO_MAX_FRAME_DURATION, &maxFrameDuration, 1);
669
670 SupportedV4L2Format maximumFormat {.width = 0, .height = 0};
671 for (const auto& supportedFormat : mSupportedFormats) {
672 if (supportedFormat.width >= maximumFormat.width &&
673 supportedFormat.height >= maximumFormat.height) {
674 maximumFormat = supportedFormat;
675 }
676 }
677 int32_t activeArraySize[] = {0, 0,
678 static_cast<int32_t>(maximumFormat.width),
679 static_cast<int32_t>(maximumFormat.height)};
680 UPDATE(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE,
681 activeArraySize, ARRAY_SIZE(activeArraySize));
682 UPDATE(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE, activeArraySize,
683 ARRAY_SIZE(activeArraySize));
684
685 int32_t pixelArraySize[] = {static_cast<int32_t>(maximumFormat.width),
686 static_cast<int32_t>(maximumFormat.height)};
687 UPDATE(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE, pixelArraySize,
688 ARRAY_SIZE(pixelArraySize));
689 return OK;
690}
691
692#undef ARRAY_SIZE
693#undef UPDATE
694
695void ExternalCameraDevice::getFrameRateList(
696 int fd, SupportedV4L2Format* format) {
697 format->frameRates.clear();
698
699 v4l2_frmivalenum frameInterval {
700 .pixel_format = format->fourcc,
701 .width = format->width,
702 .height = format->height,
703 .index = 0
704 };
705
706 for (frameInterval.index = 0;
707 TEMP_FAILURE_RETRY(ioctl(fd, VIDIOC_ENUM_FRAMEINTERVALS, &frameInterval)) == 0;
708 ++frameInterval.index) {
709 if (frameInterval.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
710 if (frameInterval.discrete.numerator != 0) {
711 float framerate = frameInterval.discrete.denominator /
712 static_cast<float>(frameInterval.discrete.numerator);
713 ALOGV("index:%d, format:%c%c%c%c, w %d, h %d, framerate %f",
714 frameInterval.index,
715 frameInterval.pixel_format & 0xFF,
716 (frameInterval.pixel_format >> 8) & 0xFF,
717 (frameInterval.pixel_format >> 16) & 0xFF,
718 (frameInterval.pixel_format >> 24) & 0xFF,
719 frameInterval.width, frameInterval.height, framerate);
720 format->frameRates.push_back(framerate);
721 }
722 }
723 }
724
725 if (format->frameRates.empty()) {
726 ALOGE("%s: failed to get supported frame rates for format:%c%c%c%c w %d h %d",
727 __FUNCTION__,
728 frameInterval.pixel_format & 0xFF,
729 (frameInterval.pixel_format >> 8) & 0xFF,
730 (frameInterval.pixel_format >> 16) & 0xFF,
731 (frameInterval.pixel_format >> 24) & 0xFF,
732 frameInterval.width, frameInterval.height);
733 }
734}
735
736void ExternalCameraDevice::initSupportedFormatsLocked(int fd) {
737 struct v4l2_fmtdesc fmtdesc {
738 .index = 0,
739 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE};
740 int ret = 0;
741 while (ret == 0) {
742 ret = TEMP_FAILURE_RETRY(ioctl(fd, VIDIOC_ENUM_FMT, &fmtdesc));
743 ALOGD("index:%d,ret:%d, format:%c%c%c%c", fmtdesc.index, ret,
744 fmtdesc.pixelformat & 0xFF,
745 (fmtdesc.pixelformat >> 8) & 0xFF,
746 (fmtdesc.pixelformat >> 16) & 0xFF,
747 (fmtdesc.pixelformat >> 24) & 0xFF);
748 if (ret == 0 && !(fmtdesc.flags & V4L2_FMT_FLAG_EMULATED)) {
749 auto it = std::find (
750 kSupportedFourCCs.begin(), kSupportedFourCCs.end(), fmtdesc.pixelformat);
751 if (it != kSupportedFourCCs.end()) {
752 // Found supported format
753 v4l2_frmsizeenum frameSize {
754 .index = 0,
755 .pixel_format = fmtdesc.pixelformat};
756 for (; TEMP_FAILURE_RETRY(ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &frameSize)) == 0;
757 ++frameSize.index) {
758 if (frameSize.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
759 ALOGD("index:%d, format:%c%c%c%c, w %d, h %d", frameSize.index,
760 fmtdesc.pixelformat & 0xFF,
761 (fmtdesc.pixelformat >> 8) & 0xFF,
762 (fmtdesc.pixelformat >> 16) & 0xFF,
763 (fmtdesc.pixelformat >> 24) & 0xFF,
764 frameSize.discrete.width, frameSize.discrete.height);
765 // Disregard h > w formats so all aspect ratio (h/w) <= 1.0
766 // This will simplify the crop/scaling logic down the road
767 if (frameSize.discrete.height > frameSize.discrete.width) {
768 continue;
769 }
770 SupportedV4L2Format format {
771 .width = frameSize.discrete.width,
772 .height = frameSize.discrete.height,
773 .fourcc = fmtdesc.pixelformat
774 };
775 getFrameRateList(fd, &format);
776 if (!format.frameRates.empty()) {
777 mSupportedFormats.push_back(format);
778 }
779 }
780 }
781 }
782 }
783 fmtdesc.index++;
784 }
785}
786
787} // namespace implementation
788} // namespace V3_4
789} // namespace device
790} // namespace camera
791} // namespace hardware
792} // namespace android
793