blob: 59e892fda75a02b4856534d3593033d31b352a67 [file] [log] [blame]
Jayant Chowdharybe543d42018-08-15 13:16:14 -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
Jayant Chowdhary2f686522022-05-09 14:52:16 +000017#include <android-base/properties.h>
Jayant Chowdharybe543d42018-08-15 13:16:14 -070018
Jayant Chowdhary0c947272018-08-15 14:42:04 -070019#include <hidl/AidlCameraDeviceCallbacks.h>
Jayant Chowdhary94f79a92018-08-15 13:57:17 -070020#include <hidl/AidlCameraServiceListener.h>
Jayant Chowdhary2f686522022-05-09 14:52:16 +000021#include <hidl/HidlCameraService.h>
22#include <hidl/HidlCameraDeviceUser.h>
23#include <hidl/Utils.h>
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -070024#include <aidl/AidlUtils.h>
Jayant Chowdharybe543d42018-08-15 13:16:14 -070025
26#include <hidl/HidlTransportSupport.h>
27
Biswarup Pal37a75182024-01-16 15:53:35 +000028#include <camera/CameraUtils.h>
Austin Borger65e64642024-06-11 15:58:23 -070029#include <utils/AttributionAndPermissionUtils.h>
Avichal Rakesh74b5ae72023-12-27 16:56:45 -080030#include <utils/Utils.h>
31
Jayant Chowdharybe543d42018-08-15 13:16:14 -070032namespace android {
33namespace frameworks {
34namespace cameraservice {
35namespace service {
36namespace V2_0 {
37namespace implementation {
38
39using frameworks::cameraservice::service::V2_0::implementation::HidlCameraService;
40using hardware::hidl_vec;
Jayant Chowdhary81d81b02024-02-15 19:13:39 +000041using hardware::BnCameraService::ROTATION_OVERRIDE_NONE;
Jayant Chowdharybe543d42018-08-15 13:16:14 -070042using hardware::cameraservice::utils::conversion::convertToHidl;
43using hardware::cameraservice::utils::conversion::B2HStatus;
44using hardware::Void;
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -070045using hardware::cameraservice::utils::conversion::aidl::filterVndkKeys;
Jayant Chowdharybe543d42018-08-15 13:16:14 -070046
Jayant Chowdhary0c947272018-08-15 14:42:04 -070047using device::V2_0::implementation::H2BCameraDeviceCallbacks;
Shuzhen Wang316781a2020-08-18 18:11:01 -070048using device::V2_1::implementation::HidlCameraDeviceUser;
Jayant Chowdhary94f79a92018-08-15 13:57:17 -070049using service::V2_0::implementation::H2BCameraServiceListener;
Jayant Chowdhary8cf92922018-11-19 15:45:17 -080050using HCameraMetadataType = frameworks::cameraservice::common::V2_0::CameraMetadataType;
51using HVendorTag = frameworks::cameraservice::common::V2_0::VendorTag;
52using HVendorTagSection = frameworks::cameraservice::common::V2_0::VendorTagSection;
53using HProviderIdAndVendorTagSections =
54 frameworks::cameraservice::common::V2_0::ProviderIdAndVendorTagSections;
Jayant Chowdharybe543d42018-08-15 13:16:14 -070055
56sp<HidlCameraService> gHidlCameraService;
57
58sp<HidlCameraService> HidlCameraService::getInstance(android::CameraService *cs) {
59 gHidlCameraService = new HidlCameraService(cs);
60 return gHidlCameraService;
61}
62
Jayant Chowdhary2f686522022-05-09 14:52:16 +000063HidlCameraService::HidlCameraService(android::CameraService *cs) : mAidlICameraService(cs) {
Avichal Rakesh74b5ae72023-12-27 16:56:45 -080064 mVndkVersion = getVNDKVersionFromProp(__ANDROID_API_FUTURE__);
65}
Jayant Chowdhary2f686522022-05-09 14:52:16 +000066
Jayant Chowdharybe543d42018-08-15 13:16:14 -070067Return<void>
68HidlCameraService::getCameraCharacteristics(const hidl_string& cameraId,
69 getCameraCharacteristics_cb _hidl_cb) {
70 android::CameraMetadata cameraMetadata;
71 HStatus status = HStatus::NO_ERROR;
Austin Borger65e64642024-06-11 15:58:23 -070072 AttributionSourceState clientAttribution =
73 AttributionAndPermissionUtils::buildAttributionSource(
74 hardware::ICameraService::USE_CALLING_PID,
75 hardware::ICameraService::USE_CALLING_UID,
76 kDefaultDeviceId);
Jayant Chowdharybe543d42018-08-15 13:16:14 -070077 binder::Status serviceRet =
Austin Borger71d8f672023-06-01 16:51:35 -070078 mAidlICameraService->getCameraCharacteristics(cameraId,
Jayant Chowdhary81d81b02024-02-15 19:13:39 +000079 /*targetSdkVersion*/__ANDROID_API_FUTURE__, ROTATION_OVERRIDE_NONE,
Austin Borger65e64642024-06-11 15:58:23 -070080 clientAttribution, 0, &cameraMetadata);
Jayant Chowdharybe543d42018-08-15 13:16:14 -070081 HCameraMetadata hidlMetadata;
82 if (!serviceRet.isOk()) {
83 switch(serviceRet.serviceSpecificErrorCode()) {
84 // No ERROR_CAMERA_DISCONNECTED since we're in the same process.
85 case hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT:
86 ALOGE("%s: Camera ID %s does not exist!", __FUNCTION__, cameraId.c_str());
87 status = HStatus::ILLEGAL_ARGUMENT;
88 break;
89 default:
90 ALOGE("Get camera characteristics from camera service failed: %s",
Tomasz Wasilczyk12b04a52023-08-11 15:52:22 +000091 serviceRet.toString8().c_str());
Jayant Chowdharybe543d42018-08-15 13:16:14 -070092 status = B2HStatus(serviceRet);
93 }
94 _hidl_cb(status, hidlMetadata);
95 return Void();
96 }
Jayant Chowdhary2f686522022-05-09 14:52:16 +000097 if (filterVndkKeys(mVndkVersion, cameraMetadata) != OK) {
98 ALOGE("%s: Unable to filter vndk metadata keys for version %d", __FUNCTION__, mVndkVersion);
99 _hidl_cb(HStatus::UNKNOWN_ERROR, hidlMetadata);
100 return Void();
101 }
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700102 const camera_metadata_t *rawMetadata = cameraMetadata.getAndLock();
103 convertToHidl(rawMetadata, &hidlMetadata);
104 _hidl_cb(status, hidlMetadata);
105 cameraMetadata.unlock(rawMetadata);
106 return Void();
107}
108
109Return<void> HidlCameraService::connectDevice(const sp<HCameraDeviceCallback>& hCallback,
110 const hidl_string& cameraId,
111 connectDevice_cb _hidl_cb) {
Jayant Chowdhary0c947272018-08-15 14:42:04 -0700112 // Here, we first get ICameraDeviceUser from mAidlICameraService, then save
113 // that interface in the newly created HidlCameraDeviceUser impl class.
114 if (mAidlICameraService == nullptr) {
115 _hidl_cb(HStatus::UNKNOWN_ERROR, nullptr);
116 return Void();
117 }
118 sp<hardware::camera2::ICameraDeviceUser> deviceRemote = nullptr;
119 // Create a hardware::camera2::ICameraDeviceCallback object which internally
120 // calls callback functions passed through hCallback.
121 sp<H2BCameraDeviceCallbacks> hybridCallbacks = new H2BCameraDeviceCallbacks(hCallback);
Jayant Chowdhary2f686522022-05-09 14:52:16 +0000122 if (!hybridCallbacks->initializeLooper(mVndkVersion)) {
Jayant Chowdhary0c947272018-08-15 14:42:04 -0700123 ALOGE("Unable to handle callbacks on device, cannot connect");
124 _hidl_cb(HStatus::UNKNOWN_ERROR, nullptr);
125 return Void();
126 }
127 sp<hardware::camera2::ICameraDeviceCallbacks> callbacks = hybridCallbacks;
Austin Borger65e64642024-06-11 15:58:23 -0700128 AttributionSourceState clientAttribution =
129 AttributionAndPermissionUtils::buildAttributionSource(
130 hardware::ICameraService::USE_CALLING_PID,
131 hardware::ICameraService::USE_CALLING_UID,
132 kDefaultDeviceId);
Austin Borgerd1ad6c62024-07-01 11:28:31 -0700133 clientAttribution.packageName = "";
134 clientAttribution.attributionTag = std::nullopt;
Jayant Chowdhary0c947272018-08-15 14:42:04 -0700135 binder::Status serviceRet = mAidlICameraService->connectDevice(
Austin Borgerd1ad6c62024-07-01 11:28:31 -0700136 callbacks, cameraId, 0/*oomScoreOffset*/,
Jayant Chowdhary81d81b02024-02-15 19:13:39 +0000137 /*targetSdkVersion*/__ANDROID_API_FUTURE__, ROTATION_OVERRIDE_NONE,
Austin Borger65e64642024-06-11 15:58:23 -0700138 clientAttribution, /*devicePolicy*/0, /*out*/&deviceRemote);
Jayant Chowdhary0c947272018-08-15 14:42:04 -0700139 HStatus status = HStatus::NO_ERROR;
140 if (!serviceRet.isOk()) {
141 ALOGE("%s: Unable to connect to camera device", __FUNCTION__);
142 status = B2HStatus(serviceRet);
143 _hidl_cb(status, nullptr);
144 return Void();
145 }
146 // Now we create a HidlCameraDeviceUser class, store the deviceRemote in it,
147 // and return that back. All calls on that interface will be forwarded to
148 // the AIDL interface.
149 sp<HidlCameraDeviceUser> hDeviceRemote = new HidlCameraDeviceUser(deviceRemote);
150 if (!hDeviceRemote->initStatus()) {
151 ALOGE("%s: Unable to initialize camera device HIDL wrapper", __FUNCTION__);
152 _hidl_cb(HStatus::UNKNOWN_ERROR, nullptr);
153 return Void();
154 }
155 hybridCallbacks->setCaptureResultMetadataQueue(hDeviceRemote->getCaptureResultMetadataQueue());
156 _hidl_cb(status, hDeviceRemote);
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700157 return Void();
158}
159
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700160void HidlCameraService::addToListenerCacheLocked(sp<HCameraServiceListener> hListener,
161 sp<hardware::ICameraServiceListener> csListener) {
162 mListeners.emplace_back(std::make_pair(hListener, csListener));
163}
164
165sp<hardware::ICameraServiceListener>
166HidlCameraService::searchListenerCacheLocked(sp<HCameraServiceListener> hListener,
167 bool shouldRemove) {
168 // Go through the mListeners list and compare the listener with the HIDL
169 // listener registered.
170 auto it = mListeners.begin();
171 sp<ICameraServiceListener> csListener = nullptr;
172 for (;it != mListeners.end(); it++) {
173 if (hardware::interfacesEqual(it->first, hListener)) {
174 break;
175 }
176 }
177 if (it != mListeners.end()) {
178 csListener = it->second;
179 if (shouldRemove) {
180 mListeners.erase(it);
181 }
182 }
183 return csListener;
184}
185
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700186Return<void> HidlCameraService::addListener(const sp<HCameraServiceListener>& hCsListener,
187 addListener_cb _hidl_cb) {
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800188 std::vector<hardware::CameraStatus> cameraStatusAndIds{};
189 HStatus status = addListenerInternal<HCameraServiceListener>(
190 hCsListener, &cameraStatusAndIds);
191 if (status != HStatus::NO_ERROR) {
192 _hidl_cb(status, {});
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700193 return Void();
194 }
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800195
196 hidl_vec<HCameraStatusAndId> hCameraStatusAndIds;
197 //Convert cameraStatusAndIds to HIDL and call callback
198 convertToHidl(cameraStatusAndIds, &hCameraStatusAndIds);
199 _hidl_cb(status, hCameraStatusAndIds);
200
201 return Void();
202}
203
204Return<void> HidlCameraService::addListener_2_1(const sp<HCameraServiceListener2_1>& hCsListener,
205 addListener_2_1_cb _hidl_cb) {
206 std::vector<hardware::CameraStatus> cameraStatusAndIds{};
207 HStatus status = addListenerInternal<HCameraServiceListener2_1>(
208 hCsListener, &cameraStatusAndIds);
209 if (status != HStatus::NO_ERROR) {
210 _hidl_cb(status, {});
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700211 return Void();
212 }
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800213
214 hidl_vec<frameworks::cameraservice::service::V2_1::CameraStatusAndId> hCameraStatusAndIds;
215 //Convert cameraStatusAndIds to HIDL and call callback
216 convertToHidl(cameraStatusAndIds, &hCameraStatusAndIds);
217 _hidl_cb(status, hCameraStatusAndIds);
218
219 return Void();
220}
221
222template<class T>
223HStatus HidlCameraService::addListenerInternal(const sp<T>& hCsListener,
224 std::vector<hardware::CameraStatus>* cameraStatusAndIds) {
225 if (mAidlICameraService == nullptr) {
226 return HStatus::UNKNOWN_ERROR;
227 }
228 if (hCsListener == nullptr || cameraStatusAndIds == nullptr) {
229 ALOGE("%s listener and cameraStatusAndIds must not be NULL", __FUNCTION__);
230 return HStatus::ILLEGAL_ARGUMENT;
231 }
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700232 sp<hardware::ICameraServiceListener> csListener = nullptr;
233 // Check the cache for previously registered callbacks
234 {
235 Mutex::Autolock l(mListenerListLock);
236 csListener = searchListenerCacheLocked(hCsListener);
237 if (csListener == nullptr) {
238 // Wrap an hCsListener with AidlCameraServiceListener and pass it to
239 // CameraService.
240 csListener = new H2BCameraServiceListener(hCsListener);
241 // Add to cache
242 addToListenerCacheLocked(hCsListener, csListener);
243 } else {
244 ALOGE("%s: Trying to add a listener %p already registered",
245 __FUNCTION__, hCsListener.get());
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800246 return HStatus::ILLEGAL_ARGUMENT;
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700247 }
248 }
Jayant Chowdharyf949ddd2019-01-29 14:34:11 -0800249 binder::Status serviceRet =
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800250 mAidlICameraService->addListenerHelper(csListener, cameraStatusAndIds, true);
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700251 HStatus status = HStatus::NO_ERROR;
252 if (!serviceRet.isOk()) {
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800253 ALOGE("%s: Unable to add camera device status listener", __FUNCTION__);
254 status = B2HStatus(serviceRet);
255 return status;
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700256 }
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800257 cameraStatusAndIds->erase(std::remove_if(cameraStatusAndIds->begin(), cameraStatusAndIds->end(),
Jayant Chowdhary90e63692019-10-25 14:13:01 -0700258 [this](const hardware::CameraStatus& s) {
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800259 bool supportsHAL3 = false;
260 binder::Status sRet =
Austin Borger71d8f672023-06-01 16:51:35 -0700261 mAidlICameraService->supportsCameraApi(s.cameraId,
Jayant Chowdhary90e63692019-10-25 14:13:01 -0700262 hardware::ICameraService::API_VERSION_2, &supportsHAL3);
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800263 return !sRet.isOk() || !supportsHAL3;
264 }), cameraStatusAndIds->end());
265
266 return HStatus::NO_ERROR;
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700267}
268
269Return<HStatus> HidlCameraService::removeListener(const sp<HCameraServiceListener>& hCsListener) {
270 if (hCsListener == nullptr) {
271 ALOGE("%s listener must not be NULL", __FUNCTION__);
272 return HStatus::ILLEGAL_ARGUMENT;
273 }
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700274 sp<ICameraServiceListener> csListener = nullptr;
275 {
276 Mutex::Autolock l(mListenerListLock);
277 csListener = searchListenerCacheLocked(hCsListener, /*removeIfFound*/true);
278 }
279 if (csListener != nullptr) {
280 mAidlICameraService->removeListener(csListener);
281 } else {
282 ALOGE("%s Removing unregistered listener %p", __FUNCTION__, hCsListener.get());
283 return HStatus::ILLEGAL_ARGUMENT;
284 }
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700285 return HStatus::NO_ERROR;
286}
287
288Return<void> HidlCameraService::getCameraVendorTagSections(getCameraVendorTagSections_cb _hidl_cb) {
Jayant Chowdhary8cf92922018-11-19 15:45:17 -0800289 sp<VendorTagDescriptorCache> gCache = VendorTagDescriptorCache::getGlobalVendorTagCache();
290 if (gCache == nullptr) {
291 _hidl_cb(HStatus::UNKNOWN_ERROR, {});
292 return Void();
293 }
294 const std::unordered_map<metadata_vendor_id_t, sp<android::VendorTagDescriptor>>
295 &vendorIdsAndTagDescs = gCache->getVendorIdsAndTagDescriptors();
296 if (vendorIdsAndTagDescs.size() == 0) {
297 _hidl_cb(HStatus::UNKNOWN_ERROR, {});
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700298 return Void();
299 }
300
Jayant Chowdhary8cf92922018-11-19 15:45:17 -0800301 hidl_vec<HProviderIdAndVendorTagSections> hTagIdsAndVendorTagSections;
302 hTagIdsAndVendorTagSections.resize(vendorIdsAndTagDescs.size());
303 size_t j = 0;
304 for (auto &vendorIdAndTagDescs : vendorIdsAndTagDescs) {
305 hidl_vec<HVendorTagSection> hVendorTagSections;
306 sp<VendorTagDescriptor> desc = vendorIdAndTagDescs.second;
307 const SortedVector<String8>* sectionNames = desc->getAllSectionNames();
308 size_t numSections = sectionNames->size();
309 std::vector<std::vector<HVendorTag>> tagsBySection(numSections);
310 int tagCount = desc->getTagCount();
Jayant Chowdhary925a27f2021-09-13 16:25:07 -0700311 if (tagCount <= 0) {
312 continue;
313 }
Jayant Chowdhary8cf92922018-11-19 15:45:17 -0800314 std::vector<uint32_t> tags(tagCount);
315 desc->getTagArray(tags.data());
316 for (int i = 0; i < tagCount; i++) {
317 HVendorTag vt;
318 vt.tagId = tags[i];
319 vt.tagName = desc->getTagName(tags[i]);
320 vt.tagType = (HCameraMetadataType) desc->getTagType(tags[i]);
321 ssize_t sectionIdx = desc->getSectionIndex(tags[i]);
322 tagsBySection[sectionIdx].push_back(vt);
323 }
324 hVendorTagSections.resize(numSections);
325 for (size_t s = 0; s < numSections; s++) {
Tomasz Wasilczyk12b04a52023-08-11 15:52:22 +0000326 hVendorTagSections[s].sectionName = (*sectionNames)[s].c_str();
Jayant Chowdhary8cf92922018-11-19 15:45:17 -0800327 hVendorTagSections[s].tags = tagsBySection[s];
328 }
329 HProviderIdAndVendorTagSections &hProviderIdAndVendorTagSections =
330 hTagIdsAndVendorTagSections[j];
331 hProviderIdAndVendorTagSections.providerId = vendorIdAndTagDescs.first;
332 hProviderIdAndVendorTagSections.vendorTagSections = std::move(hVendorTagSections);
333 j++;
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700334 }
Jayant Chowdhary8cf92922018-11-19 15:45:17 -0800335 _hidl_cb(HStatus::NO_ERROR, hTagIdsAndVendorTagSections);
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700336 return Void();
337}
338
339} // implementation
340} // V2_0
341} // service
342} // cameraservice
343} // frameworks
344} // android
345