blob: d3b2a5151738f762f03e4757755c978ca1f20bd6 [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>
Avichal Rakesh74b5ae72023-12-27 16:56:45 -080029#include <utils/Utils.h>
30
Jayant Chowdharybe543d42018-08-15 13:16:14 -070031namespace android {
32namespace frameworks {
33namespace cameraservice {
34namespace service {
35namespace V2_0 {
36namespace implementation {
37
38using frameworks::cameraservice::service::V2_0::implementation::HidlCameraService;
39using hardware::hidl_vec;
Jayant Chowdhary81d81b02024-02-15 19:13:39 +000040using hardware::BnCameraService::ROTATION_OVERRIDE_NONE;
Jayant Chowdharybe543d42018-08-15 13:16:14 -070041using hardware::cameraservice::utils::conversion::convertToHidl;
42using hardware::cameraservice::utils::conversion::B2HStatus;
43using hardware::Void;
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -070044using hardware::cameraservice::utils::conversion::aidl::filterVndkKeys;
Jayant Chowdharybe543d42018-08-15 13:16:14 -070045
Jayant Chowdhary0c947272018-08-15 14:42:04 -070046using device::V2_0::implementation::H2BCameraDeviceCallbacks;
Shuzhen Wang316781a2020-08-18 18:11:01 -070047using device::V2_1::implementation::HidlCameraDeviceUser;
Jayant Chowdhary94f79a92018-08-15 13:57:17 -070048using service::V2_0::implementation::H2BCameraServiceListener;
Jayant Chowdhary8cf92922018-11-19 15:45:17 -080049using HCameraMetadataType = frameworks::cameraservice::common::V2_0::CameraMetadataType;
50using HVendorTag = frameworks::cameraservice::common::V2_0::VendorTag;
51using HVendorTagSection = frameworks::cameraservice::common::V2_0::VendorTagSection;
52using HProviderIdAndVendorTagSections =
53 frameworks::cameraservice::common::V2_0::ProviderIdAndVendorTagSections;
Jayant Chowdharybe543d42018-08-15 13:16:14 -070054
55sp<HidlCameraService> gHidlCameraService;
56
57sp<HidlCameraService> HidlCameraService::getInstance(android::CameraService *cs) {
58 gHidlCameraService = new HidlCameraService(cs);
59 return gHidlCameraService;
60}
61
Jayant Chowdhary2f686522022-05-09 14:52:16 +000062HidlCameraService::HidlCameraService(android::CameraService *cs) : mAidlICameraService(cs) {
Avichal Rakesh74b5ae72023-12-27 16:56:45 -080063 mVndkVersion = getVNDKVersionFromProp(__ANDROID_API_FUTURE__);
64}
Jayant Chowdhary2f686522022-05-09 14:52:16 +000065
Jayant Chowdharybe543d42018-08-15 13:16:14 -070066Return<void>
67HidlCameraService::getCameraCharacteristics(const hidl_string& cameraId,
68 getCameraCharacteristics_cb _hidl_cb) {
69 android::CameraMetadata cameraMetadata;
70 HStatus status = HStatus::NO_ERROR;
71 binder::Status serviceRet =
Austin Borger71d8f672023-06-01 16:51:35 -070072 mAidlICameraService->getCameraCharacteristics(cameraId,
Jayant Chowdhary81d81b02024-02-15 19:13:39 +000073 /*targetSdkVersion*/__ANDROID_API_FUTURE__, ROTATION_OVERRIDE_NONE,
Biswarup Pal37a75182024-01-16 15:53:35 +000074 kDefaultDeviceId, 0, &cameraMetadata);
Jayant Chowdharybe543d42018-08-15 13:16:14 -070075 HCameraMetadata hidlMetadata;
76 if (!serviceRet.isOk()) {
77 switch(serviceRet.serviceSpecificErrorCode()) {
78 // No ERROR_CAMERA_DISCONNECTED since we're in the same process.
79 case hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT:
80 ALOGE("%s: Camera ID %s does not exist!", __FUNCTION__, cameraId.c_str());
81 status = HStatus::ILLEGAL_ARGUMENT;
82 break;
83 default:
84 ALOGE("Get camera characteristics from camera service failed: %s",
Tomasz Wasilczyk12b04a52023-08-11 15:52:22 +000085 serviceRet.toString8().c_str());
Jayant Chowdharybe543d42018-08-15 13:16:14 -070086 status = B2HStatus(serviceRet);
87 }
88 _hidl_cb(status, hidlMetadata);
89 return Void();
90 }
Jayant Chowdhary2f686522022-05-09 14:52:16 +000091 if (filterVndkKeys(mVndkVersion, cameraMetadata) != OK) {
92 ALOGE("%s: Unable to filter vndk metadata keys for version %d", __FUNCTION__, mVndkVersion);
93 _hidl_cb(HStatus::UNKNOWN_ERROR, hidlMetadata);
94 return Void();
95 }
Jayant Chowdharybe543d42018-08-15 13:16:14 -070096 const camera_metadata_t *rawMetadata = cameraMetadata.getAndLock();
97 convertToHidl(rawMetadata, &hidlMetadata);
98 _hidl_cb(status, hidlMetadata);
99 cameraMetadata.unlock(rawMetadata);
100 return Void();
101}
102
103Return<void> HidlCameraService::connectDevice(const sp<HCameraDeviceCallback>& hCallback,
104 const hidl_string& cameraId,
105 connectDevice_cb _hidl_cb) {
Jayant Chowdhary0c947272018-08-15 14:42:04 -0700106 // Here, we first get ICameraDeviceUser from mAidlICameraService, then save
107 // that interface in the newly created HidlCameraDeviceUser impl class.
108 if (mAidlICameraService == nullptr) {
109 _hidl_cb(HStatus::UNKNOWN_ERROR, nullptr);
110 return Void();
111 }
112 sp<hardware::camera2::ICameraDeviceUser> deviceRemote = nullptr;
113 // Create a hardware::camera2::ICameraDeviceCallback object which internally
114 // calls callback functions passed through hCallback.
115 sp<H2BCameraDeviceCallbacks> hybridCallbacks = new H2BCameraDeviceCallbacks(hCallback);
Jayant Chowdhary2f686522022-05-09 14:52:16 +0000116 if (!hybridCallbacks->initializeLooper(mVndkVersion)) {
Jayant Chowdhary0c947272018-08-15 14:42:04 -0700117 ALOGE("Unable to handle callbacks on device, cannot connect");
118 _hidl_cb(HStatus::UNKNOWN_ERROR, nullptr);
119 return Void();
120 }
121 sp<hardware::camera2::ICameraDeviceCallbacks> callbacks = hybridCallbacks;
122 binder::Status serviceRet = mAidlICameraService->connectDevice(
Austin Borger71d8f672023-06-01 16:51:35 -0700123 callbacks, cameraId, std::string(), {},
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700124 hardware::ICameraService::USE_CALLING_UID, 0/*oomScoreOffset*/,
Jayant Chowdhary81d81b02024-02-15 19:13:39 +0000125 /*targetSdkVersion*/__ANDROID_API_FUTURE__, ROTATION_OVERRIDE_NONE,
Biswarup Pal37a75182024-01-16 15:53:35 +0000126 kDefaultDeviceId, /*devicePolicy*/0, /*out*/&deviceRemote);
Jayant Chowdhary0c947272018-08-15 14:42:04 -0700127 HStatus status = HStatus::NO_ERROR;
128 if (!serviceRet.isOk()) {
129 ALOGE("%s: Unable to connect to camera device", __FUNCTION__);
130 status = B2HStatus(serviceRet);
131 _hidl_cb(status, nullptr);
132 return Void();
133 }
134 // Now we create a HidlCameraDeviceUser class, store the deviceRemote in it,
135 // and return that back. All calls on that interface will be forwarded to
136 // the AIDL interface.
137 sp<HidlCameraDeviceUser> hDeviceRemote = new HidlCameraDeviceUser(deviceRemote);
138 if (!hDeviceRemote->initStatus()) {
139 ALOGE("%s: Unable to initialize camera device HIDL wrapper", __FUNCTION__);
140 _hidl_cb(HStatus::UNKNOWN_ERROR, nullptr);
141 return Void();
142 }
143 hybridCallbacks->setCaptureResultMetadataQueue(hDeviceRemote->getCaptureResultMetadataQueue());
144 _hidl_cb(status, hDeviceRemote);
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700145 return Void();
146}
147
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700148void HidlCameraService::addToListenerCacheLocked(sp<HCameraServiceListener> hListener,
149 sp<hardware::ICameraServiceListener> csListener) {
150 mListeners.emplace_back(std::make_pair(hListener, csListener));
151}
152
153sp<hardware::ICameraServiceListener>
154HidlCameraService::searchListenerCacheLocked(sp<HCameraServiceListener> hListener,
155 bool shouldRemove) {
156 // Go through the mListeners list and compare the listener with the HIDL
157 // listener registered.
158 auto it = mListeners.begin();
159 sp<ICameraServiceListener> csListener = nullptr;
160 for (;it != mListeners.end(); it++) {
161 if (hardware::interfacesEqual(it->first, hListener)) {
162 break;
163 }
164 }
165 if (it != mListeners.end()) {
166 csListener = it->second;
167 if (shouldRemove) {
168 mListeners.erase(it);
169 }
170 }
171 return csListener;
172}
173
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700174Return<void> HidlCameraService::addListener(const sp<HCameraServiceListener>& hCsListener,
175 addListener_cb _hidl_cb) {
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800176 std::vector<hardware::CameraStatus> cameraStatusAndIds{};
177 HStatus status = addListenerInternal<HCameraServiceListener>(
178 hCsListener, &cameraStatusAndIds);
179 if (status != HStatus::NO_ERROR) {
180 _hidl_cb(status, {});
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700181 return Void();
182 }
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800183
184 hidl_vec<HCameraStatusAndId> hCameraStatusAndIds;
185 //Convert cameraStatusAndIds to HIDL and call callback
186 convertToHidl(cameraStatusAndIds, &hCameraStatusAndIds);
187 _hidl_cb(status, hCameraStatusAndIds);
188
189 return Void();
190}
191
192Return<void> HidlCameraService::addListener_2_1(const sp<HCameraServiceListener2_1>& hCsListener,
193 addListener_2_1_cb _hidl_cb) {
194 std::vector<hardware::CameraStatus> cameraStatusAndIds{};
195 HStatus status = addListenerInternal<HCameraServiceListener2_1>(
196 hCsListener, &cameraStatusAndIds);
197 if (status != HStatus::NO_ERROR) {
198 _hidl_cb(status, {});
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700199 return Void();
200 }
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800201
202 hidl_vec<frameworks::cameraservice::service::V2_1::CameraStatusAndId> hCameraStatusAndIds;
203 //Convert cameraStatusAndIds to HIDL and call callback
204 convertToHidl(cameraStatusAndIds, &hCameraStatusAndIds);
205 _hidl_cb(status, hCameraStatusAndIds);
206
207 return Void();
208}
209
210template<class T>
211HStatus HidlCameraService::addListenerInternal(const sp<T>& hCsListener,
212 std::vector<hardware::CameraStatus>* cameraStatusAndIds) {
213 if (mAidlICameraService == nullptr) {
214 return HStatus::UNKNOWN_ERROR;
215 }
216 if (hCsListener == nullptr || cameraStatusAndIds == nullptr) {
217 ALOGE("%s listener and cameraStatusAndIds must not be NULL", __FUNCTION__);
218 return HStatus::ILLEGAL_ARGUMENT;
219 }
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700220 sp<hardware::ICameraServiceListener> csListener = nullptr;
221 // Check the cache for previously registered callbacks
222 {
223 Mutex::Autolock l(mListenerListLock);
224 csListener = searchListenerCacheLocked(hCsListener);
225 if (csListener == nullptr) {
226 // Wrap an hCsListener with AidlCameraServiceListener and pass it to
227 // CameraService.
228 csListener = new H2BCameraServiceListener(hCsListener);
229 // Add to cache
230 addToListenerCacheLocked(hCsListener, csListener);
231 } else {
232 ALOGE("%s: Trying to add a listener %p already registered",
233 __FUNCTION__, hCsListener.get());
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800234 return HStatus::ILLEGAL_ARGUMENT;
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700235 }
236 }
Jayant Chowdharyf949ddd2019-01-29 14:34:11 -0800237 binder::Status serviceRet =
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800238 mAidlICameraService->addListenerHelper(csListener, cameraStatusAndIds, true);
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700239 HStatus status = HStatus::NO_ERROR;
240 if (!serviceRet.isOk()) {
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800241 ALOGE("%s: Unable to add camera device status listener", __FUNCTION__);
242 status = B2HStatus(serviceRet);
243 return status;
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700244 }
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800245 cameraStatusAndIds->erase(std::remove_if(cameraStatusAndIds->begin(), cameraStatusAndIds->end(),
Jayant Chowdhary90e63692019-10-25 14:13:01 -0700246 [this](const hardware::CameraStatus& s) {
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800247 bool supportsHAL3 = false;
248 binder::Status sRet =
Austin Borger71d8f672023-06-01 16:51:35 -0700249 mAidlICameraService->supportsCameraApi(s.cameraId,
Jayant Chowdhary90e63692019-10-25 14:13:01 -0700250 hardware::ICameraService::API_VERSION_2, &supportsHAL3);
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800251 return !sRet.isOk() || !supportsHAL3;
252 }), cameraStatusAndIds->end());
253
254 return HStatus::NO_ERROR;
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700255}
256
257Return<HStatus> HidlCameraService::removeListener(const sp<HCameraServiceListener>& hCsListener) {
258 if (hCsListener == nullptr) {
259 ALOGE("%s listener must not be NULL", __FUNCTION__);
260 return HStatus::ILLEGAL_ARGUMENT;
261 }
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700262 sp<ICameraServiceListener> csListener = nullptr;
263 {
264 Mutex::Autolock l(mListenerListLock);
265 csListener = searchListenerCacheLocked(hCsListener, /*removeIfFound*/true);
266 }
267 if (csListener != nullptr) {
268 mAidlICameraService->removeListener(csListener);
269 } else {
270 ALOGE("%s Removing unregistered listener %p", __FUNCTION__, hCsListener.get());
271 return HStatus::ILLEGAL_ARGUMENT;
272 }
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700273 return HStatus::NO_ERROR;
274}
275
276Return<void> HidlCameraService::getCameraVendorTagSections(getCameraVendorTagSections_cb _hidl_cb) {
Jayant Chowdhary8cf92922018-11-19 15:45:17 -0800277 sp<VendorTagDescriptorCache> gCache = VendorTagDescriptorCache::getGlobalVendorTagCache();
278 if (gCache == nullptr) {
279 _hidl_cb(HStatus::UNKNOWN_ERROR, {});
280 return Void();
281 }
282 const std::unordered_map<metadata_vendor_id_t, sp<android::VendorTagDescriptor>>
283 &vendorIdsAndTagDescs = gCache->getVendorIdsAndTagDescriptors();
284 if (vendorIdsAndTagDescs.size() == 0) {
285 _hidl_cb(HStatus::UNKNOWN_ERROR, {});
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700286 return Void();
287 }
288
Jayant Chowdhary8cf92922018-11-19 15:45:17 -0800289 hidl_vec<HProviderIdAndVendorTagSections> hTagIdsAndVendorTagSections;
290 hTagIdsAndVendorTagSections.resize(vendorIdsAndTagDescs.size());
291 size_t j = 0;
292 for (auto &vendorIdAndTagDescs : vendorIdsAndTagDescs) {
293 hidl_vec<HVendorTagSection> hVendorTagSections;
294 sp<VendorTagDescriptor> desc = vendorIdAndTagDescs.second;
295 const SortedVector<String8>* sectionNames = desc->getAllSectionNames();
296 size_t numSections = sectionNames->size();
297 std::vector<std::vector<HVendorTag>> tagsBySection(numSections);
298 int tagCount = desc->getTagCount();
Jayant Chowdhary925a27f2021-09-13 16:25:07 -0700299 if (tagCount <= 0) {
300 continue;
301 }
Jayant Chowdhary8cf92922018-11-19 15:45:17 -0800302 std::vector<uint32_t> tags(tagCount);
303 desc->getTagArray(tags.data());
304 for (int i = 0; i < tagCount; i++) {
305 HVendorTag vt;
306 vt.tagId = tags[i];
307 vt.tagName = desc->getTagName(tags[i]);
308 vt.tagType = (HCameraMetadataType) desc->getTagType(tags[i]);
309 ssize_t sectionIdx = desc->getSectionIndex(tags[i]);
310 tagsBySection[sectionIdx].push_back(vt);
311 }
312 hVendorTagSections.resize(numSections);
313 for (size_t s = 0; s < numSections; s++) {
Tomasz Wasilczyk12b04a52023-08-11 15:52:22 +0000314 hVendorTagSections[s].sectionName = (*sectionNames)[s].c_str();
Jayant Chowdhary8cf92922018-11-19 15:45:17 -0800315 hVendorTagSections[s].tags = tagsBySection[s];
316 }
317 HProviderIdAndVendorTagSections &hProviderIdAndVendorTagSections =
318 hTagIdsAndVendorTagSections[j];
319 hProviderIdAndVendorTagSections.providerId = vendorIdAndTagDescs.first;
320 hProviderIdAndVendorTagSections.vendorTagSections = std::move(hVendorTagSections);
321 j++;
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700322 }
Jayant Chowdhary8cf92922018-11-19 15:45:17 -0800323 _hidl_cb(HStatus::NO_ERROR, hTagIdsAndVendorTagSections);
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700324 return Void();
325}
326
327} // implementation
328} // V2_0
329} // service
330} // cameraservice
331} // frameworks
332} // android
333