blob: 8e37b267f9e5cb052f33cd821aba235bfb1562d1 [file] [log] [blame]
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -08001/*
2 * Copyright (C) 2016 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 "CamProvider@2.4-impl"
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -070018//#define LOG_NDEBUG 0
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -080019#include <android/log.h>
20
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -080021#include "CameraProvider.h"
Yin-Chia Yeh19030592017-10-19 17:30:11 -070022#include "ExternalCameraProvider.h"
Yin-Chia Yeh248ed702017-01-23 17:27:26 -080023#include "CameraDevice_1_0.h"
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -070024#include "CameraDevice_3_3.h"
Emilian Peeve18057b2017-11-13 16:03:44 +000025#include "CameraDevice_3_4.h"
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -070026#include <cutils/properties.h>
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -080027#include <string.h>
28#include <utils/Trace.h>
29
30
31namespace android {
32namespace hardware {
33namespace camera {
34namespace provider {
35namespace V2_4 {
36namespace implementation {
37
38namespace {
39const char *kLegacyProviderName = "legacy/0";
Yin-Chia Yeh19030592017-10-19 17:30:11 -070040const char *kExternalProviderName = "external/0";
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -080041// "device@<version>/legacy/<id>"
42const std::regex kDeviceNameRE("device@([0-9]+\\.[0-9]+)/legacy/(.+)");
43const char *kHAL3_2 = "3.2";
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -070044const char *kHAL3_3 = "3.3";
Emilian Peeve18057b2017-11-13 16:03:44 +000045const char *kHAL3_4 = "3.4";
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -080046const char *kHAL1_0 = "1.0";
47const int kMaxCameraDeviceNameLen = 128;
48const int kMaxCameraIdLen = 16;
49
Andreas Gampe0b171f12017-04-04 20:02:25 -070050bool matchDeviceName(const hidl_string& deviceName, std::string* deviceVersion,
51 std::string* cameraId) {
52 std::string deviceNameStd(deviceName.c_str());
53 std::smatch sm;
54 if (std::regex_match(deviceNameStd, sm, kDeviceNameRE)) {
55 if (deviceVersion != nullptr) {
56 *deviceVersion = sm[1];
57 }
58 if (cameraId != nullptr) {
59 *cameraId = sm[2];
60 }
61 return true;
62 }
63 return false;
64}
65
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -080066} // anonymous namespace
67
68using ::android::hardware::camera::common::V1_0::CameraMetadataType;
69using ::android::hardware::camera::common::V1_0::Status;
70
Guennadi Liakhovetski7b7ede72017-11-28 09:28:56 +010071void CameraProvider::addDeviceNames(int camera_id, CameraDeviceStatus status, bool cam_new)
72{
73 char cameraId[kMaxCameraIdLen];
74 snprintf(cameraId, sizeof(cameraId), "%d", camera_id);
75 std::string cameraIdStr(cameraId);
76
77 mCameraIds.add(cameraIdStr);
78
79 // initialize mCameraDeviceNames and mOpenLegacySupported
80 mOpenLegacySupported[cameraIdStr] = false;
81 int deviceVersion = mModule->getDeviceVersion(camera_id);
82 auto deviceNamePair = std::make_pair(cameraIdStr,
83 getHidlDeviceName(cameraIdStr, deviceVersion));
84 mCameraDeviceNames.add(deviceNamePair);
85 if (cam_new) {
86 mCallbacks->cameraDeviceStatusChange(deviceNamePair.second, status);
87 }
88 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
89 mModule->isOpenLegacyDefined()) {
90 // try open_legacy to see if it actually works
91 struct hw_device_t* halDev = nullptr;
92 int ret = mModule->openLegacy(cameraId, CAMERA_DEVICE_API_VERSION_1_0, &halDev);
93 if (ret == 0) {
94 mOpenLegacySupported[cameraIdStr] = true;
95 halDev->close(halDev);
96 deviceNamePair = std::make_pair(cameraIdStr,
97 getHidlDeviceName(cameraIdStr, CAMERA_DEVICE_API_VERSION_1_0));
98 mCameraDeviceNames.add(deviceNamePair);
99 if (cam_new) {
100 mCallbacks->cameraDeviceStatusChange(deviceNamePair.second, status);
101 }
102 } else if (ret == -EBUSY || ret == -EUSERS) {
103 // Looks like this provider instance is not initialized during
104 // system startup and there are other camera users already.
105 // Not a good sign but not fatal.
106 ALOGW("%s: open_legacy try failed!", __FUNCTION__);
107 }
108 }
109}
110
Guennadi Liakhovetskieca1d452017-12-07 10:59:35 +0100111void CameraProvider::removeDeviceNames(int camera_id)
112{
113 std::string cameraIdStr = std::to_string(camera_id);
114
115 mCameraIds.remove(cameraIdStr);
116
117 int deviceVersion = mModule->getDeviceVersion(camera_id);
118 auto deviceNamePair = std::make_pair(cameraIdStr,
119 getHidlDeviceName(cameraIdStr, deviceVersion));
120 mCameraDeviceNames.remove(deviceNamePair);
121 mCallbacks->cameraDeviceStatusChange(deviceNamePair.second, CameraDeviceStatus::NOT_PRESENT);
122 if (deviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
123 mModule->isOpenLegacyDefined() && mOpenLegacySupported[cameraIdStr]) {
124
125 deviceNamePair = std::make_pair(cameraIdStr,
126 getHidlDeviceName(cameraIdStr, CAMERA_DEVICE_API_VERSION_1_0));
127 mCameraDeviceNames.remove(deviceNamePair);
128 mCallbacks->cameraDeviceStatusChange(deviceNamePair.second,
129 CameraDeviceStatus::NOT_PRESENT);
130 }
131
132 mModule->removeCamera(camera_id);
133}
134
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800135/**
136 * static callback forwarding methods from HAL to instance
137 */
138void CameraProvider::sCameraDeviceStatusChange(
139 const struct camera_module_callbacks* callbacks,
140 int camera_id,
141 int new_status) {
Yin-Chia Yeh6dc9b532017-02-09 18:43:35 -0800142 CameraProvider* cp = const_cast<CameraProvider*>(
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800143 static_cast<const CameraProvider*>(callbacks));
Guennadi Liakhovetski7b7ede72017-11-28 09:28:56 +0100144 bool found = false;
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800145
146 if (cp == nullptr) {
147 ALOGE("%s: callback ops is null", __FUNCTION__);
148 return;
149 }
150
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800151 Mutex::Autolock _l(cp->mCbLock);
152 char cameraId[kMaxCameraIdLen];
153 snprintf(cameraId, sizeof(cameraId), "%d", camera_id);
154 std::string cameraIdStr(cameraId);
155 cp->mCameraStatusMap[cameraIdStr] = (camera_device_status_t) new_status;
156 if (cp->mCallbacks != nullptr) {
157 CameraDeviceStatus status = (CameraDeviceStatus) new_status;
158 for (auto const& deviceNamePair : cp->mCameraDeviceNames) {
159 if (cameraIdStr.compare(deviceNamePair.first) == 0) {
160 cp->mCallbacks->cameraDeviceStatusChange(
161 deviceNamePair.second, status);
Guennadi Liakhovetski7b7ede72017-11-28 09:28:56 +0100162 found = true;
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800163 }
164 }
Guennadi Liakhovetski7b7ede72017-11-28 09:28:56 +0100165
Guennadi Liakhovetskieca1d452017-12-07 10:59:35 +0100166 switch (status) {
167 case CameraDeviceStatus::PRESENT:
168 case CameraDeviceStatus::ENUMERATING:
169 if (!found) {
170 cp->addDeviceNames(camera_id, status, true);
171 }
172 break;
173 case CameraDeviceStatus::NOT_PRESENT:
174 if (found) {
175 cp->removeDeviceNames(camera_id);
176 }
Guennadi Liakhovetski7b7ede72017-11-28 09:28:56 +0100177 }
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800178 }
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800179}
180
181void CameraProvider::sTorchModeStatusChange(
182 const struct camera_module_callbacks* callbacks,
183 const char* camera_id,
184 int new_status) {
Yin-Chia Yeh6dc9b532017-02-09 18:43:35 -0800185 CameraProvider* cp = const_cast<CameraProvider*>(
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800186 static_cast<const CameraProvider*>(callbacks));
187
188 if (cp == nullptr) {
189 ALOGE("%s: callback ops is null", __FUNCTION__);
190 return;
191 }
192
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800193 Mutex::Autolock _l(cp->mCbLock);
194 if (cp->mCallbacks != nullptr) {
195 std::string cameraIdStr(camera_id);
196 TorchModeStatus status = (TorchModeStatus) new_status;
197 for (auto const& deviceNamePair : cp->mCameraDeviceNames) {
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800198 if (cameraIdStr.compare(deviceNamePair.first) == 0) {
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800199 cp->mCallbacks->torchModeStatusChange(
200 deviceNamePair.second, status);
201 }
202 }
203 }
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800204}
205
206Status CameraProvider::getHidlStatus(int status) {
207 switch (status) {
208 case 0: return Status::OK;
209 case -ENODEV: return Status::INTERNAL_ERROR;
210 case -EINVAL: return Status::ILLEGAL_ARGUMENT;
211 default:
212 ALOGE("%s: unknown HAL status code %d", __FUNCTION__, status);
213 return Status::INTERNAL_ERROR;
214 }
215}
216
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800217std::string CameraProvider::getLegacyCameraId(const hidl_string& deviceName) {
Andreas Gampe0b171f12017-04-04 20:02:25 -0700218 std::string cameraId;
219 matchDeviceName(deviceName, nullptr, &cameraId);
220 return cameraId;
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800221}
222
223int CameraProvider::getCameraDeviceVersion(const hidl_string& deviceName) {
Andreas Gampe0b171f12017-04-04 20:02:25 -0700224 std::string deviceVersion;
225 bool match = matchDeviceName(deviceName, &deviceVersion, nullptr);
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800226 if (!match) {
227 return -1;
228 }
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -0700229 if (deviceVersion == kHAL3_3) {
230 return CAMERA_DEVICE_API_VERSION_3_3;
231 } else if (deviceVersion == kHAL3_2) {
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800232 return CAMERA_DEVICE_API_VERSION_3_2;
Andreas Gampe0b171f12017-04-04 20:02:25 -0700233 } else if (deviceVersion == kHAL1_0) {
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800234 return CAMERA_DEVICE_API_VERSION_1_0;
235 }
236 return 0;
237}
238
239std::string CameraProvider::getHidlDeviceName(
240 std::string cameraId, int deviceVersion) {
241 // Maybe consider create a version check method and SortedVec to speed up?
242 if (deviceVersion != CAMERA_DEVICE_API_VERSION_1_0 &&
243 deviceVersion != CAMERA_DEVICE_API_VERSION_3_2 &&
244 deviceVersion != CAMERA_DEVICE_API_VERSION_3_3 &&
Emilian Peeve18057b2017-11-13 16:03:44 +0000245 deviceVersion != CAMERA_DEVICE_API_VERSION_3_4 &&
246 deviceVersion != CAMERA_DEVICE_API_VERSION_3_5) {
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800247 return hidl_string("");
248 }
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -0700249 bool isV1 = deviceVersion == CAMERA_DEVICE_API_VERSION_1_0;
250 int versionMajor = isV1 ? 1 : 3;
251 int versionMinor = isV1 ? 0 : mPreferredHal3MinorVersion;
Emilian Peeve18057b2017-11-13 16:03:44 +0000252 if (deviceVersion == CAMERA_DEVICE_API_VERSION_3_5) {
253 versionMinor = 4;
254 }
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800255 char deviceName[kMaxCameraDeviceNameLen];
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -0700256 snprintf(deviceName, sizeof(deviceName), "device@%d.%d/legacy/%s",
257 versionMajor, versionMinor, cameraId.c_str());
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800258 return deviceName;
259}
260
261CameraProvider::CameraProvider() :
262 camera_module_callbacks_t({sCameraDeviceStatusChange,
263 sTorchModeStatusChange}) {
264 mInitFailed = initialize();
265}
266
267CameraProvider::~CameraProvider() {}
268
269bool CameraProvider::initialize() {
270 camera_module_t *rawModule;
271 int err = hw_get_module(CAMERA_HARDWARE_MODULE_ID,
272 (const hw_module_t **)&rawModule);
273 if (err < 0) {
274 ALOGE("Could not load camera HAL module: %d (%s)", err, strerror(-err));
275 return true;
276 }
277
278 mModule = new CameraModule(rawModule);
279 err = mModule->init();
280 if (err != OK) {
Yin-Chia Yehfca2e742017-01-31 16:00:20 -0800281 ALOGE("Could not initialize camera HAL module: %d (%s)", err, strerror(-err));
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800282 mModule.clear();
283 return true;
284 }
285 ALOGI("Loaded \"%s\" camera module", mModule->getModuleName());
286
Shuzhen Wangefb7bfa2017-03-15 18:26:39 -0700287 // Setup vendor tags here so HAL can setup vendor keys in camera characteristics
288 VendorTagDescriptor::clearGlobalVendorTagDescriptor();
289 if (!setUpVendorTags()) {
290 ALOGE("%s: Vendor tag setup failed, will not be available.", __FUNCTION__);
291 }
292
Yin-Chia Yehfca2e742017-01-31 16:00:20 -0800293 // Setup callback now because we are going to try openLegacy next
294 err = mModule->setCallbacks(this);
295 if (err != OK) {
296 ALOGE("Could not set camera module callback: %d (%s)", err, strerror(-err));
297 mModule.clear();
298 return true;
299 }
300
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -0700301 mPreferredHal3MinorVersion = property_get_int32("ro.camera.wrapper.hal3TrebleMinorVersion", 3);
302 ALOGV("Preferred HAL 3 minor version is %d", mPreferredHal3MinorVersion);
303 switch(mPreferredHal3MinorVersion) {
304 case 2:
305 case 3:
306 // OK
307 break;
308 default:
309 ALOGW("Unknown minor camera device HAL version %d in property "
Emilian Peeve18057b2017-11-13 16:03:44 +0000310 "'camera.wrapper.hal3TrebleMinorVersion', defaulting to 3",
311 mPreferredHal3MinorVersion);
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -0700312 mPreferredHal3MinorVersion = 3;
313 }
314
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800315 mNumberOfLegacyCameras = mModule->getNumberOfCameras();
316 for (int i = 0; i < mNumberOfLegacyCameras; i++) {
Emilian Peevc9ded512017-04-10 16:12:55 +0100317 struct camera_info info;
318 auto rc = mModule->getCameraInfo(i, &info);
319 if (rc != NO_ERROR) {
320 ALOGE("%s: Camera info query failed!", __func__);
321 mModule.clear();
322 return true;
323 }
324
325 if (checkCameraVersion(i, info) != OK) {
326 ALOGE("%s: Camera version check failed!", __func__);
327 mModule.clear();
328 return true;
329 }
330
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800331 char cameraId[kMaxCameraIdLen];
332 snprintf(cameraId, sizeof(cameraId), "%d", i);
333 std::string cameraIdStr(cameraId);
334 mCameraStatusMap[cameraIdStr] = CAMERA_DEVICE_STATUS_PRESENT;
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800335
Guennadi Liakhovetski7b7ede72017-11-28 09:28:56 +0100336 addDeviceNames(i);
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800337 }
338
Eino-Ville Talvala0f5eb832017-02-09 19:45:31 -0800339 return false; // mInitFailed
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800340}
341
Emilian Peevc9ded512017-04-10 16:12:55 +0100342/**
343 * Check that the device HAL version is still in supported.
344 */
345int CameraProvider::checkCameraVersion(int id, camera_info info) {
346 if (mModule == nullptr) {
347 return NO_INIT;
348 }
349
350 // device_version undefined in CAMERA_MODULE_API_VERSION_1_0,
351 // All CAMERA_MODULE_API_VERSION_1_0 devices are backward-compatible
352 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_0) {
353 // Verify the device version is in the supported range
354 switch (info.device_version) {
355 case CAMERA_DEVICE_API_VERSION_1_0:
356 case CAMERA_DEVICE_API_VERSION_3_2:
357 case CAMERA_DEVICE_API_VERSION_3_3:
358 case CAMERA_DEVICE_API_VERSION_3_4:
Emilian Peeve18057b2017-11-13 16:03:44 +0000359 case CAMERA_DEVICE_API_VERSION_3_5:
Emilian Peevc9ded512017-04-10 16:12:55 +0100360 // in support
361 break;
362 case CAMERA_DEVICE_API_VERSION_2_0:
363 case CAMERA_DEVICE_API_VERSION_2_1:
364 case CAMERA_DEVICE_API_VERSION_3_0:
365 case CAMERA_DEVICE_API_VERSION_3_1:
366 // no longer supported
367 default:
368 ALOGE("%s: Device %d has HAL version %x, which is not supported",
369 __FUNCTION__, id, info.device_version);
370 return NO_INIT;
371 }
372 }
373
374 return OK;
375}
376
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800377bool CameraProvider::setUpVendorTags() {
378 ATRACE_CALL();
379 vendor_tag_ops_t vOps = vendor_tag_ops_t();
380
381 // Check if vendor operations have been implemented
382 if (!mModule->isVendorTagDefined()) {
383 ALOGI("%s: No vendor tags defined for this device.", __FUNCTION__);
Eino-Ville Talvala0f5eb832017-02-09 19:45:31 -0800384 return true;
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800385 }
386
387 mModule->getVendorTagOps(&vOps);
388
389 // Ensure all vendor operations are present
390 if (vOps.get_tag_count == nullptr || vOps.get_all_tags == nullptr ||
391 vOps.get_section_name == nullptr || vOps.get_tag_name == nullptr ||
392 vOps.get_tag_type == nullptr) {
393 ALOGE("%s: Vendor tag operations not fully defined. Ignoring definitions."
394 , __FUNCTION__);
395 return false;
396 }
397
398 // Read all vendor tag definitions into a descriptor
399 sp<VendorTagDescriptor> desc;
400 status_t res;
401 if ((res = VendorTagDescriptor::createDescriptorFromOps(&vOps, /*out*/desc))
402 != OK) {
403 ALOGE("%s: Could not generate descriptor from vendor tag operations,"
404 "received error %s (%d). Camera clients will not be able to use"
405 "vendor tags", __FUNCTION__, strerror(res), res);
406 return false;
407 }
408
409 // Set the global descriptor to use with camera metadata
410 VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc);
411 const SortedVector<String8>* sectionNames = desc->getAllSectionNames();
412 size_t numSections = sectionNames->size();
413 std::vector<std::vector<VendorTag>> tagsBySection(numSections);
414 int tagCount = desc->getTagCount();
415 std::vector<uint32_t> tags(tagCount);
416 desc->getTagArray(tags.data());
417 for (int i = 0; i < tagCount; i++) {
418 VendorTag vt;
419 vt.tagId = tags[i];
420 vt.tagName = desc->getTagName(tags[i]);
421 vt.tagType = (CameraMetadataType) desc->getTagType(tags[i]);
422 ssize_t sectionIdx = desc->getSectionIndex(tags[i]);
423 tagsBySection[sectionIdx].push_back(vt);
424 }
425 mVendorTagSections.resize(numSections);
426 for (size_t s = 0; s < numSections; s++) {
427 mVendorTagSections[s].sectionName = (*sectionNames)[s].string();
428 mVendorTagSections[s].tags = tagsBySection[s];
429 }
430 return true;
431}
432
433// Methods from ::android::hardware::camera::provider::V2_4::ICameraProvider follow.
434Return<Status> CameraProvider::setCallback(const sp<ICameraProviderCallback>& callback) {
Yin-Chia Yehfca2e742017-01-31 16:00:20 -0800435 Mutex::Autolock _l(mCbLock);
436 mCallbacks = callback;
437 return Status::OK;
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800438}
439
440Return<void> CameraProvider::getVendorTags(getVendorTags_cb _hidl_cb) {
441 _hidl_cb(Status::OK, mVendorTagSections);
442 return Void();
443}
444
445Return<void> CameraProvider::getCameraIdList(getCameraIdList_cb _hidl_cb) {
446 std::vector<hidl_string> deviceNameList;
447 for (auto const& deviceNamePair : mCameraDeviceNames) {
448 if (mCameraStatusMap[deviceNamePair.first] == CAMERA_DEVICE_STATUS_PRESENT) {
449 deviceNameList.push_back(deviceNamePair.second);
450 }
451 }
452 hidl_vec<hidl_string> hidlDeviceNameList(deviceNameList);
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800453 _hidl_cb(Status::OK, hidlDeviceNameList);
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800454 return Void();
455}
456
457Return<void> CameraProvider::isSetTorchModeSupported(isSetTorchModeSupported_cb _hidl_cb) {
458 bool support = mModule->isSetTorchModeSupported();
459 _hidl_cb (Status::OK, support);
460 return Void();
461}
462
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800463Return<void> CameraProvider::getCameraDeviceInterface_V1_x(
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800464 const hidl_string& cameraDeviceName, getCameraDeviceInterface_V1_x_cb _hidl_cb) {
Andreas Gampe0b171f12017-04-04 20:02:25 -0700465 std::string cameraId, deviceVersion;
466 bool match = matchDeviceName(cameraDeviceName, &deviceVersion, &cameraId);
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800467 if (!match) {
468 _hidl_cb(Status::ILLEGAL_ARGUMENT, nullptr);
469 return Void();
470 }
471
Yin-Chia Yeh248ed702017-01-23 17:27:26 -0800472 std::string deviceName(cameraDeviceName.c_str());
473 ssize_t index = mCameraDeviceNames.indexOf(std::make_pair(cameraId, deviceName));
474 if (index == NAME_NOT_FOUND) { // Either an illegal name or a device version mismatch
475 Status status = Status::OK;
476 ssize_t idx = mCameraIds.indexOf(cameraId);
477 if (idx == NAME_NOT_FOUND) {
478 ALOGE("%s: cannot find camera %s!", __FUNCTION__, cameraId.c_str());
479 status = Status::ILLEGAL_ARGUMENT;
480 } else { // invalid version
481 ALOGE("%s: camera device %s does not support version %s!",
482 __FUNCTION__, cameraId.c_str(), deviceVersion.c_str());
483 status = Status::OPERATION_NOT_SUPPORTED;
484 }
485 _hidl_cb(status, nullptr);
486 return Void();
487 }
488
489 if (mCameraStatusMap.count(cameraId) == 0 ||
490 mCameraStatusMap[cameraId] != CAMERA_DEVICE_STATUS_PRESENT) {
491 _hidl_cb(Status::ILLEGAL_ARGUMENT, nullptr);
492 return Void();
493 }
494
495 sp<android::hardware::camera::device::V1_0::implementation::CameraDevice> device =
496 new android::hardware::camera::device::V1_0::implementation::CameraDevice(
497 mModule, cameraId, mCameraDeviceNames);
498
499 if (device == nullptr) {
500 ALOGE("%s: cannot allocate camera device for id %s", __FUNCTION__, cameraId.c_str());
501 _hidl_cb(Status::INTERNAL_ERROR, nullptr);
502 return Void();
503 }
504
505 if (device->isInitFailed()) {
506 ALOGE("%s: camera device %s init failed!", __FUNCTION__, cameraId.c_str());
507 device = nullptr;
508 _hidl_cb(Status::INTERNAL_ERROR, nullptr);
509 return Void();
510 }
511
512 _hidl_cb (Status::OK, device);
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800513 return Void();
514}
515
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800516Return<void> CameraProvider::getCameraDeviceInterface_V3_x(
517 const hidl_string& cameraDeviceName, getCameraDeviceInterface_V3_x_cb _hidl_cb) {
Andreas Gampe0b171f12017-04-04 20:02:25 -0700518 std::string cameraId, deviceVersion;
519 bool match = matchDeviceName(cameraDeviceName, &deviceVersion, &cameraId);
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800520 if (!match) {
521 _hidl_cb(Status::ILLEGAL_ARGUMENT, nullptr);
522 return Void();
523 }
524
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800525 std::string deviceName(cameraDeviceName.c_str());
526 ssize_t index = mCameraDeviceNames.indexOf(std::make_pair(cameraId, deviceName));
527 if (index == NAME_NOT_FOUND) { // Either an illegal name or a device version mismatch
528 Status status = Status::OK;
529 ssize_t idx = mCameraIds.indexOf(cameraId);
530 if (idx == NAME_NOT_FOUND) {
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800531 ALOGE("%s: cannot find camera %s!", __FUNCTION__, cameraId.c_str());
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800532 status = Status::ILLEGAL_ARGUMENT;
533 } else { // invalid version
Yin-Chia Yeh9c6dbd52016-12-22 14:55:02 -0800534 ALOGE("%s: camera device %s does not support version %s!",
535 __FUNCTION__, cameraId.c_str(), deviceVersion.c_str());
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800536 status = Status::OPERATION_NOT_SUPPORTED;
537 }
538 _hidl_cb(status, nullptr);
539 return Void();
540 }
541
542 if (mCameraStatusMap.count(cameraId) == 0 ||
543 mCameraStatusMap[cameraId] != CAMERA_DEVICE_STATUS_PRESENT) {
544 _hidl_cb(Status::ILLEGAL_ARGUMENT, nullptr);
545 return Void();
546 }
547
Emilian Peeve18057b2017-11-13 16:03:44 +0000548 sp<android::hardware::camera::device::V3_2::ICameraDevice> device;
549 if (deviceVersion == kHAL3_4) {
550 ALOGV("Constructing v3.4 camera device");
551 sp<android::hardware::camera::device::V3_2::implementation::CameraDevice> deviceImpl =
552 new android::hardware::camera::device::V3_4::implementation::CameraDevice(
553 mModule, cameraId, mCameraDeviceNames);
554 if (deviceImpl == nullptr || deviceImpl->isInitFailed()) {
555 ALOGE("%s: camera device %s init failed!", __FUNCTION__, cameraId.c_str());
556 device = nullptr;
557 _hidl_cb(Status::INTERNAL_ERROR, nullptr);
558 return Void();
559 }
560
561 device = deviceImpl;
562 _hidl_cb (Status::OK, device);
563 return Void();
564 }
565
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -0700566 // Since some Treble HAL revisions can map to the same legacy HAL version(s), we default
567 // to the newest possible Treble HAL revision, but allow for override if needed via
568 // system property.
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -0700569 switch (mPreferredHal3MinorVersion) {
570 case 2: { // Map legacy camera device v3 HAL to Treble camera device HAL v3.2
571 ALOGV("Constructing v3.2 camera device");
572 sp<android::hardware::camera::device::V3_2::implementation::CameraDevice> deviceImpl =
573 new android::hardware::camera::device::V3_2::implementation::CameraDevice(
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800574 mModule, cameraId, mCameraDeviceNames);
Eino-Ville Talvala50fe4302017-08-22 16:15:09 -0700575 if (deviceImpl == nullptr || deviceImpl->isInitFailed()) {
576 ALOGE("%s: camera device %s init failed!", __FUNCTION__, cameraId.c_str());
577 device = nullptr;
578 _hidl_cb(Status::INTERNAL_ERROR, nullptr);
579 return Void();
580 }
581 device = deviceImpl;
582 break;
583 }
584 case 3: { // Map legacy camera device v3 HAL to Treble camera device HAL v3.3
585 ALOGV("Constructing v3.3 camera device");
586 sp<android::hardware::camera::device::V3_2::implementation::CameraDevice> deviceImpl =
587 new android::hardware::camera::device::V3_3::implementation::CameraDevice(
588 mModule, cameraId, mCameraDeviceNames);
589 if (deviceImpl == nullptr || deviceImpl->isInitFailed()) {
590 ALOGE("%s: camera device %s init failed!", __FUNCTION__, cameraId.c_str());
591 device = nullptr;
592 _hidl_cb(Status::INTERNAL_ERROR, nullptr);
593 return Void();
594 }
595 device = deviceImpl;
596 break;
597 }
598 default:
599 ALOGE("%s: Unknown HAL minor version %d!", __FUNCTION__, mPreferredHal3MinorVersion);
600 device = nullptr;
601 _hidl_cb(Status::INTERNAL_ERROR, nullptr);
602 return Void();
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800603 }
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800604 _hidl_cb (Status::OK, device);
605 return Void();
606}
607
608ICameraProvider* HIDL_FETCH_ICameraProvider(const char* name) {
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700609 if (strcmp(name, kLegacyProviderName) == 0) {
610 CameraProvider* provider = new CameraProvider();
611 if (provider == nullptr) {
612 ALOGE("%s: cannot allocate camera provider!", __FUNCTION__);
613 return nullptr;
614 }
615 if (provider->isInitFailed()) {
616 ALOGE("%s: camera provider init failed!", __FUNCTION__);
617 delete provider;
618 return nullptr;
619 }
620 return provider;
621 } else if (strcmp(name, kExternalProviderName) == 0) {
622 ExternalCameraProvider* provider = new ExternalCameraProvider();
623 return provider;
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800624 }
Yin-Chia Yeh19030592017-10-19 17:30:11 -0700625 ALOGE("%s: unknown instance name: %s", __FUNCTION__, name);
626 return nullptr;
Yin-Chia Yehf906b3b2016-12-14 19:13:15 -0800627}
628
629} // namespace implementation
630} // namespace V2_4
631} // namespace provider
632} // namespace camera
633} // namespace hardware
634} // namespace android