blob: 463aefb2d4b47a8d69f247a58507560da61038ef [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);
Jayant Chowdhary0c947272018-08-15 14:42:04 -0700133 binder::Status serviceRet = mAidlICameraService->connectDevice(
Austin Borger65e64642024-06-11 15:58:23 -0700134 callbacks, cameraId, std::string(), {}, 0/*oomScoreOffset*/,
Jayant Chowdhary81d81b02024-02-15 19:13:39 +0000135 /*targetSdkVersion*/__ANDROID_API_FUTURE__, ROTATION_OVERRIDE_NONE,
Austin Borger65e64642024-06-11 15:58:23 -0700136 clientAttribution, /*devicePolicy*/0, /*out*/&deviceRemote);
Jayant Chowdhary0c947272018-08-15 14:42:04 -0700137 HStatus status = HStatus::NO_ERROR;
138 if (!serviceRet.isOk()) {
139 ALOGE("%s: Unable to connect to camera device", __FUNCTION__);
140 status = B2HStatus(serviceRet);
141 _hidl_cb(status, nullptr);
142 return Void();
143 }
144 // Now we create a HidlCameraDeviceUser class, store the deviceRemote in it,
145 // and return that back. All calls on that interface will be forwarded to
146 // the AIDL interface.
147 sp<HidlCameraDeviceUser> hDeviceRemote = new HidlCameraDeviceUser(deviceRemote);
148 if (!hDeviceRemote->initStatus()) {
149 ALOGE("%s: Unable to initialize camera device HIDL wrapper", __FUNCTION__);
150 _hidl_cb(HStatus::UNKNOWN_ERROR, nullptr);
151 return Void();
152 }
153 hybridCallbacks->setCaptureResultMetadataQueue(hDeviceRemote->getCaptureResultMetadataQueue());
154 _hidl_cb(status, hDeviceRemote);
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700155 return Void();
156}
157
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700158void HidlCameraService::addToListenerCacheLocked(sp<HCameraServiceListener> hListener,
159 sp<hardware::ICameraServiceListener> csListener) {
160 mListeners.emplace_back(std::make_pair(hListener, csListener));
161}
162
163sp<hardware::ICameraServiceListener>
164HidlCameraService::searchListenerCacheLocked(sp<HCameraServiceListener> hListener,
165 bool shouldRemove) {
166 // Go through the mListeners list and compare the listener with the HIDL
167 // listener registered.
168 auto it = mListeners.begin();
169 sp<ICameraServiceListener> csListener = nullptr;
170 for (;it != mListeners.end(); it++) {
171 if (hardware::interfacesEqual(it->first, hListener)) {
172 break;
173 }
174 }
175 if (it != mListeners.end()) {
176 csListener = it->second;
177 if (shouldRemove) {
178 mListeners.erase(it);
179 }
180 }
181 return csListener;
182}
183
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700184Return<void> HidlCameraService::addListener(const sp<HCameraServiceListener>& hCsListener,
185 addListener_cb _hidl_cb) {
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800186 std::vector<hardware::CameraStatus> cameraStatusAndIds{};
187 HStatus status = addListenerInternal<HCameraServiceListener>(
188 hCsListener, &cameraStatusAndIds);
189 if (status != HStatus::NO_ERROR) {
190 _hidl_cb(status, {});
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700191 return Void();
192 }
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800193
194 hidl_vec<HCameraStatusAndId> hCameraStatusAndIds;
195 //Convert cameraStatusAndIds to HIDL and call callback
196 convertToHidl(cameraStatusAndIds, &hCameraStatusAndIds);
197 _hidl_cb(status, hCameraStatusAndIds);
198
199 return Void();
200}
201
202Return<void> HidlCameraService::addListener_2_1(const sp<HCameraServiceListener2_1>& hCsListener,
203 addListener_2_1_cb _hidl_cb) {
204 std::vector<hardware::CameraStatus> cameraStatusAndIds{};
205 HStatus status = addListenerInternal<HCameraServiceListener2_1>(
206 hCsListener, &cameraStatusAndIds);
207 if (status != HStatus::NO_ERROR) {
208 _hidl_cb(status, {});
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700209 return Void();
210 }
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800211
212 hidl_vec<frameworks::cameraservice::service::V2_1::CameraStatusAndId> hCameraStatusAndIds;
213 //Convert cameraStatusAndIds to HIDL and call callback
214 convertToHidl(cameraStatusAndIds, &hCameraStatusAndIds);
215 _hidl_cb(status, hCameraStatusAndIds);
216
217 return Void();
218}
219
220template<class T>
221HStatus HidlCameraService::addListenerInternal(const sp<T>& hCsListener,
222 std::vector<hardware::CameraStatus>* cameraStatusAndIds) {
223 if (mAidlICameraService == nullptr) {
224 return HStatus::UNKNOWN_ERROR;
225 }
226 if (hCsListener == nullptr || cameraStatusAndIds == nullptr) {
227 ALOGE("%s listener and cameraStatusAndIds must not be NULL", __FUNCTION__);
228 return HStatus::ILLEGAL_ARGUMENT;
229 }
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700230 sp<hardware::ICameraServiceListener> csListener = nullptr;
231 // Check the cache for previously registered callbacks
232 {
233 Mutex::Autolock l(mListenerListLock);
234 csListener = searchListenerCacheLocked(hCsListener);
235 if (csListener == nullptr) {
236 // Wrap an hCsListener with AidlCameraServiceListener and pass it to
237 // CameraService.
238 csListener = new H2BCameraServiceListener(hCsListener);
239 // Add to cache
240 addToListenerCacheLocked(hCsListener, csListener);
241 } else {
242 ALOGE("%s: Trying to add a listener %p already registered",
243 __FUNCTION__, hCsListener.get());
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800244 return HStatus::ILLEGAL_ARGUMENT;
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700245 }
246 }
Jayant Chowdharyf949ddd2019-01-29 14:34:11 -0800247 binder::Status serviceRet =
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800248 mAidlICameraService->addListenerHelper(csListener, cameraStatusAndIds, true);
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700249 HStatus status = HStatus::NO_ERROR;
250 if (!serviceRet.isOk()) {
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800251 ALOGE("%s: Unable to add camera device status listener", __FUNCTION__);
252 status = B2HStatus(serviceRet);
253 return status;
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700254 }
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800255 cameraStatusAndIds->erase(std::remove_if(cameraStatusAndIds->begin(), cameraStatusAndIds->end(),
Jayant Chowdhary90e63692019-10-25 14:13:01 -0700256 [this](const hardware::CameraStatus& s) {
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800257 bool supportsHAL3 = false;
258 binder::Status sRet =
Austin Borger71d8f672023-06-01 16:51:35 -0700259 mAidlICameraService->supportsCameraApi(s.cameraId,
Jayant Chowdhary90e63692019-10-25 14:13:01 -0700260 hardware::ICameraService::API_VERSION_2, &supportsHAL3);
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800261 return !sRet.isOk() || !supportsHAL3;
262 }), cameraStatusAndIds->end());
263
264 return HStatus::NO_ERROR;
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700265}
266
267Return<HStatus> HidlCameraService::removeListener(const sp<HCameraServiceListener>& hCsListener) {
268 if (hCsListener == nullptr) {
269 ALOGE("%s listener must not be NULL", __FUNCTION__);
270 return HStatus::ILLEGAL_ARGUMENT;
271 }
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700272 sp<ICameraServiceListener> csListener = nullptr;
273 {
274 Mutex::Autolock l(mListenerListLock);
275 csListener = searchListenerCacheLocked(hCsListener, /*removeIfFound*/true);
276 }
277 if (csListener != nullptr) {
278 mAidlICameraService->removeListener(csListener);
279 } else {
280 ALOGE("%s Removing unregistered listener %p", __FUNCTION__, hCsListener.get());
281 return HStatus::ILLEGAL_ARGUMENT;
282 }
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700283 return HStatus::NO_ERROR;
284}
285
286Return<void> HidlCameraService::getCameraVendorTagSections(getCameraVendorTagSections_cb _hidl_cb) {
Jayant Chowdhary8cf92922018-11-19 15:45:17 -0800287 sp<VendorTagDescriptorCache> gCache = VendorTagDescriptorCache::getGlobalVendorTagCache();
288 if (gCache == nullptr) {
289 _hidl_cb(HStatus::UNKNOWN_ERROR, {});
290 return Void();
291 }
292 const std::unordered_map<metadata_vendor_id_t, sp<android::VendorTagDescriptor>>
293 &vendorIdsAndTagDescs = gCache->getVendorIdsAndTagDescriptors();
294 if (vendorIdsAndTagDescs.size() == 0) {
295 _hidl_cb(HStatus::UNKNOWN_ERROR, {});
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700296 return Void();
297 }
298
Jayant Chowdhary8cf92922018-11-19 15:45:17 -0800299 hidl_vec<HProviderIdAndVendorTagSections> hTagIdsAndVendorTagSections;
300 hTagIdsAndVendorTagSections.resize(vendorIdsAndTagDescs.size());
301 size_t j = 0;
302 for (auto &vendorIdAndTagDescs : vendorIdsAndTagDescs) {
303 hidl_vec<HVendorTagSection> hVendorTagSections;
304 sp<VendorTagDescriptor> desc = vendorIdAndTagDescs.second;
305 const SortedVector<String8>* sectionNames = desc->getAllSectionNames();
306 size_t numSections = sectionNames->size();
307 std::vector<std::vector<HVendorTag>> tagsBySection(numSections);
308 int tagCount = desc->getTagCount();
Jayant Chowdhary925a27f2021-09-13 16:25:07 -0700309 if (tagCount <= 0) {
310 continue;
311 }
Jayant Chowdhary8cf92922018-11-19 15:45:17 -0800312 std::vector<uint32_t> tags(tagCount);
313 desc->getTagArray(tags.data());
314 for (int i = 0; i < tagCount; i++) {
315 HVendorTag vt;
316 vt.tagId = tags[i];
317 vt.tagName = desc->getTagName(tags[i]);
318 vt.tagType = (HCameraMetadataType) desc->getTagType(tags[i]);
319 ssize_t sectionIdx = desc->getSectionIndex(tags[i]);
320 tagsBySection[sectionIdx].push_back(vt);
321 }
322 hVendorTagSections.resize(numSections);
323 for (size_t s = 0; s < numSections; s++) {
Tomasz Wasilczyk12b04a52023-08-11 15:52:22 +0000324 hVendorTagSections[s].sectionName = (*sectionNames)[s].c_str();
Jayant Chowdhary8cf92922018-11-19 15:45:17 -0800325 hVendorTagSections[s].tags = tagsBySection[s];
326 }
327 HProviderIdAndVendorTagSections &hProviderIdAndVendorTagSections =
328 hTagIdsAndVendorTagSections[j];
329 hProviderIdAndVendorTagSections.providerId = vendorIdAndTagDescs.first;
330 hProviderIdAndVendorTagSections.vendorTagSections = std::move(hVendorTagSections);
331 j++;
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700332 }
Jayant Chowdhary8cf92922018-11-19 15:45:17 -0800333 _hidl_cb(HStatus::NO_ERROR, hTagIdsAndVendorTagSections);
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700334 return Void();
335}
336
337} // implementation
338} // V2_0
339} // service
340} // cameraservice
341} // frameworks
342} // android
343