blob: 8f25ad65d1a8790d77e2e10133da63e647ca5617 [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;
40using hardware::cameraservice::utils::conversion::convertToHidl;
41using hardware::cameraservice::utils::conversion::B2HStatus;
42using hardware::Void;
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -070043using hardware::cameraservice::utils::conversion::aidl::filterVndkKeys;
Jayant Chowdharybe543d42018-08-15 13:16:14 -070044
Jayant Chowdhary0c947272018-08-15 14:42:04 -070045using device::V2_0::implementation::H2BCameraDeviceCallbacks;
Shuzhen Wang316781a2020-08-18 18:11:01 -070046using device::V2_1::implementation::HidlCameraDeviceUser;
Jayant Chowdhary94f79a92018-08-15 13:57:17 -070047using service::V2_0::implementation::H2BCameraServiceListener;
Jayant Chowdhary8cf92922018-11-19 15:45:17 -080048using HCameraMetadataType = frameworks::cameraservice::common::V2_0::CameraMetadataType;
49using HVendorTag = frameworks::cameraservice::common::V2_0::VendorTag;
50using HVendorTagSection = frameworks::cameraservice::common::V2_0::VendorTagSection;
51using HProviderIdAndVendorTagSections =
52 frameworks::cameraservice::common::V2_0::ProviderIdAndVendorTagSections;
Jayant Chowdharybe543d42018-08-15 13:16:14 -070053
54sp<HidlCameraService> gHidlCameraService;
55
56sp<HidlCameraService> HidlCameraService::getInstance(android::CameraService *cs) {
57 gHidlCameraService = new HidlCameraService(cs);
58 return gHidlCameraService;
59}
60
Jayant Chowdhary2f686522022-05-09 14:52:16 +000061HidlCameraService::HidlCameraService(android::CameraService *cs) : mAidlICameraService(cs) {
Avichal Rakesh74b5ae72023-12-27 16:56:45 -080062 mVndkVersion = getVNDKVersionFromProp(__ANDROID_API_FUTURE__);
63}
Jayant Chowdhary2f686522022-05-09 14:52:16 +000064
Jayant Chowdharybe543d42018-08-15 13:16:14 -070065Return<void>
66HidlCameraService::getCameraCharacteristics(const hidl_string& cameraId,
67 getCameraCharacteristics_cb _hidl_cb) {
68 android::CameraMetadata cameraMetadata;
69 HStatus status = HStatus::NO_ERROR;
70 binder::Status serviceRet =
Austin Borger71d8f672023-06-01 16:51:35 -070071 mAidlICameraService->getCameraCharacteristics(cameraId,
Austin Borger7b14d5e2023-03-16 10:29:20 -070072 /*targetSdkVersion*/__ANDROID_API_FUTURE__, /*overrideToPortrait*/false,
Biswarup Pal37a75182024-01-16 15:53:35 +000073 kDefaultDeviceId, 0, &cameraMetadata);
Jayant Chowdharybe543d42018-08-15 13:16:14 -070074 HCameraMetadata hidlMetadata;
75 if (!serviceRet.isOk()) {
76 switch(serviceRet.serviceSpecificErrorCode()) {
77 // No ERROR_CAMERA_DISCONNECTED since we're in the same process.
78 case hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT:
79 ALOGE("%s: Camera ID %s does not exist!", __FUNCTION__, cameraId.c_str());
80 status = HStatus::ILLEGAL_ARGUMENT;
81 break;
82 default:
83 ALOGE("Get camera characteristics from camera service failed: %s",
Tomasz Wasilczyk12b04a52023-08-11 15:52:22 +000084 serviceRet.toString8().c_str());
Jayant Chowdharybe543d42018-08-15 13:16:14 -070085 status = B2HStatus(serviceRet);
86 }
87 _hidl_cb(status, hidlMetadata);
88 return Void();
89 }
Jayant Chowdhary2f686522022-05-09 14:52:16 +000090 if (filterVndkKeys(mVndkVersion, cameraMetadata) != OK) {
91 ALOGE("%s: Unable to filter vndk metadata keys for version %d", __FUNCTION__, mVndkVersion);
92 _hidl_cb(HStatus::UNKNOWN_ERROR, hidlMetadata);
93 return Void();
94 }
Jayant Chowdharybe543d42018-08-15 13:16:14 -070095 const camera_metadata_t *rawMetadata = cameraMetadata.getAndLock();
96 convertToHidl(rawMetadata, &hidlMetadata);
97 _hidl_cb(status, hidlMetadata);
98 cameraMetadata.unlock(rawMetadata);
99 return Void();
100}
101
102Return<void> HidlCameraService::connectDevice(const sp<HCameraDeviceCallback>& hCallback,
103 const hidl_string& cameraId,
104 connectDevice_cb _hidl_cb) {
Jayant Chowdhary0c947272018-08-15 14:42:04 -0700105 // Here, we first get ICameraDeviceUser from mAidlICameraService, then save
106 // that interface in the newly created HidlCameraDeviceUser impl class.
107 if (mAidlICameraService == nullptr) {
108 _hidl_cb(HStatus::UNKNOWN_ERROR, nullptr);
109 return Void();
110 }
111 sp<hardware::camera2::ICameraDeviceUser> deviceRemote = nullptr;
112 // Create a hardware::camera2::ICameraDeviceCallback object which internally
113 // calls callback functions passed through hCallback.
114 sp<H2BCameraDeviceCallbacks> hybridCallbacks = new H2BCameraDeviceCallbacks(hCallback);
Jayant Chowdhary2f686522022-05-09 14:52:16 +0000115 if (!hybridCallbacks->initializeLooper(mVndkVersion)) {
Jayant Chowdhary0c947272018-08-15 14:42:04 -0700116 ALOGE("Unable to handle callbacks on device, cannot connect");
117 _hidl_cb(HStatus::UNKNOWN_ERROR, nullptr);
118 return Void();
119 }
120 sp<hardware::camera2::ICameraDeviceCallbacks> callbacks = hybridCallbacks;
121 binder::Status serviceRet = mAidlICameraService->connectDevice(
Austin Borger71d8f672023-06-01 16:51:35 -0700122 callbacks, cameraId, std::string(), {},
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700123 hardware::ICameraService::USE_CALLING_UID, 0/*oomScoreOffset*/,
Austin Borger7b14d5e2023-03-16 10:29:20 -0700124 /*targetSdkVersion*/__ANDROID_API_FUTURE__, /*overrideToPortrait*/false,
Biswarup Pal37a75182024-01-16 15:53:35 +0000125 kDefaultDeviceId, /*devicePolicy*/0, /*out*/&deviceRemote);
Jayant Chowdhary0c947272018-08-15 14:42:04 -0700126 HStatus status = HStatus::NO_ERROR;
127 if (!serviceRet.isOk()) {
128 ALOGE("%s: Unable to connect to camera device", __FUNCTION__);
129 status = B2HStatus(serviceRet);
130 _hidl_cb(status, nullptr);
131 return Void();
132 }
133 // Now we create a HidlCameraDeviceUser class, store the deviceRemote in it,
134 // and return that back. All calls on that interface will be forwarded to
135 // the AIDL interface.
136 sp<HidlCameraDeviceUser> hDeviceRemote = new HidlCameraDeviceUser(deviceRemote);
137 if (!hDeviceRemote->initStatus()) {
138 ALOGE("%s: Unable to initialize camera device HIDL wrapper", __FUNCTION__);
139 _hidl_cb(HStatus::UNKNOWN_ERROR, nullptr);
140 return Void();
141 }
142 hybridCallbacks->setCaptureResultMetadataQueue(hDeviceRemote->getCaptureResultMetadataQueue());
143 _hidl_cb(status, hDeviceRemote);
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700144 return Void();
145}
146
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700147void HidlCameraService::addToListenerCacheLocked(sp<HCameraServiceListener> hListener,
148 sp<hardware::ICameraServiceListener> csListener) {
149 mListeners.emplace_back(std::make_pair(hListener, csListener));
150}
151
152sp<hardware::ICameraServiceListener>
153HidlCameraService::searchListenerCacheLocked(sp<HCameraServiceListener> hListener,
154 bool shouldRemove) {
155 // Go through the mListeners list and compare the listener with the HIDL
156 // listener registered.
157 auto it = mListeners.begin();
158 sp<ICameraServiceListener> csListener = nullptr;
159 for (;it != mListeners.end(); it++) {
160 if (hardware::interfacesEqual(it->first, hListener)) {
161 break;
162 }
163 }
164 if (it != mListeners.end()) {
165 csListener = it->second;
166 if (shouldRemove) {
167 mListeners.erase(it);
168 }
169 }
170 return csListener;
171}
172
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700173Return<void> HidlCameraService::addListener(const sp<HCameraServiceListener>& hCsListener,
174 addListener_cb _hidl_cb) {
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800175 std::vector<hardware::CameraStatus> cameraStatusAndIds{};
176 HStatus status = addListenerInternal<HCameraServiceListener>(
177 hCsListener, &cameraStatusAndIds);
178 if (status != HStatus::NO_ERROR) {
179 _hidl_cb(status, {});
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700180 return Void();
181 }
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800182
183 hidl_vec<HCameraStatusAndId> hCameraStatusAndIds;
184 //Convert cameraStatusAndIds to HIDL and call callback
185 convertToHidl(cameraStatusAndIds, &hCameraStatusAndIds);
186 _hidl_cb(status, hCameraStatusAndIds);
187
188 return Void();
189}
190
191Return<void> HidlCameraService::addListener_2_1(const sp<HCameraServiceListener2_1>& hCsListener,
192 addListener_2_1_cb _hidl_cb) {
193 std::vector<hardware::CameraStatus> cameraStatusAndIds{};
194 HStatus status = addListenerInternal<HCameraServiceListener2_1>(
195 hCsListener, &cameraStatusAndIds);
196 if (status != HStatus::NO_ERROR) {
197 _hidl_cb(status, {});
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700198 return Void();
199 }
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800200
201 hidl_vec<frameworks::cameraservice::service::V2_1::CameraStatusAndId> hCameraStatusAndIds;
202 //Convert cameraStatusAndIds to HIDL and call callback
203 convertToHidl(cameraStatusAndIds, &hCameraStatusAndIds);
204 _hidl_cb(status, hCameraStatusAndIds);
205
206 return Void();
207}
208
209template<class T>
210HStatus HidlCameraService::addListenerInternal(const sp<T>& hCsListener,
211 std::vector<hardware::CameraStatus>* cameraStatusAndIds) {
212 if (mAidlICameraService == nullptr) {
213 return HStatus::UNKNOWN_ERROR;
214 }
215 if (hCsListener == nullptr || cameraStatusAndIds == nullptr) {
216 ALOGE("%s listener and cameraStatusAndIds must not be NULL", __FUNCTION__);
217 return HStatus::ILLEGAL_ARGUMENT;
218 }
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700219 sp<hardware::ICameraServiceListener> csListener = nullptr;
220 // Check the cache for previously registered callbacks
221 {
222 Mutex::Autolock l(mListenerListLock);
223 csListener = searchListenerCacheLocked(hCsListener);
224 if (csListener == nullptr) {
225 // Wrap an hCsListener with AidlCameraServiceListener and pass it to
226 // CameraService.
227 csListener = new H2BCameraServiceListener(hCsListener);
228 // Add to cache
229 addToListenerCacheLocked(hCsListener, csListener);
230 } else {
231 ALOGE("%s: Trying to add a listener %p already registered",
232 __FUNCTION__, hCsListener.get());
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800233 return HStatus::ILLEGAL_ARGUMENT;
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700234 }
235 }
Jayant Chowdharyf949ddd2019-01-29 14:34:11 -0800236 binder::Status serviceRet =
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800237 mAidlICameraService->addListenerHelper(csListener, cameraStatusAndIds, true);
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700238 HStatus status = HStatus::NO_ERROR;
239 if (!serviceRet.isOk()) {
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800240 ALOGE("%s: Unable to add camera device status listener", __FUNCTION__);
241 status = B2HStatus(serviceRet);
242 return status;
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700243 }
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800244 cameraStatusAndIds->erase(std::remove_if(cameraStatusAndIds->begin(), cameraStatusAndIds->end(),
Jayant Chowdhary90e63692019-10-25 14:13:01 -0700245 [this](const hardware::CameraStatus& s) {
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800246 bool supportsHAL3 = false;
247 binder::Status sRet =
Austin Borger71d8f672023-06-01 16:51:35 -0700248 mAidlICameraService->supportsCameraApi(s.cameraId,
Jayant Chowdhary90e63692019-10-25 14:13:01 -0700249 hardware::ICameraService::API_VERSION_2, &supportsHAL3);
Shuzhen Wang4fa28d22020-01-23 15:57:25 -0800250 return !sRet.isOk() || !supportsHAL3;
251 }), cameraStatusAndIds->end());
252
253 return HStatus::NO_ERROR;
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700254}
255
256Return<HStatus> HidlCameraService::removeListener(const sp<HCameraServiceListener>& hCsListener) {
257 if (hCsListener == nullptr) {
258 ALOGE("%s listener must not be NULL", __FUNCTION__);
259 return HStatus::ILLEGAL_ARGUMENT;
260 }
Jayant Chowdhary94f79a92018-08-15 13:57:17 -0700261 sp<ICameraServiceListener> csListener = nullptr;
262 {
263 Mutex::Autolock l(mListenerListLock);
264 csListener = searchListenerCacheLocked(hCsListener, /*removeIfFound*/true);
265 }
266 if (csListener != nullptr) {
267 mAidlICameraService->removeListener(csListener);
268 } else {
269 ALOGE("%s Removing unregistered listener %p", __FUNCTION__, hCsListener.get());
270 return HStatus::ILLEGAL_ARGUMENT;
271 }
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700272 return HStatus::NO_ERROR;
273}
274
275Return<void> HidlCameraService::getCameraVendorTagSections(getCameraVendorTagSections_cb _hidl_cb) {
Jayant Chowdhary8cf92922018-11-19 15:45:17 -0800276 sp<VendorTagDescriptorCache> gCache = VendorTagDescriptorCache::getGlobalVendorTagCache();
277 if (gCache == nullptr) {
278 _hidl_cb(HStatus::UNKNOWN_ERROR, {});
279 return Void();
280 }
281 const std::unordered_map<metadata_vendor_id_t, sp<android::VendorTagDescriptor>>
282 &vendorIdsAndTagDescs = gCache->getVendorIdsAndTagDescriptors();
283 if (vendorIdsAndTagDescs.size() == 0) {
284 _hidl_cb(HStatus::UNKNOWN_ERROR, {});
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700285 return Void();
286 }
287
Jayant Chowdhary8cf92922018-11-19 15:45:17 -0800288 hidl_vec<HProviderIdAndVendorTagSections> hTagIdsAndVendorTagSections;
289 hTagIdsAndVendorTagSections.resize(vendorIdsAndTagDescs.size());
290 size_t j = 0;
291 for (auto &vendorIdAndTagDescs : vendorIdsAndTagDescs) {
292 hidl_vec<HVendorTagSection> hVendorTagSections;
293 sp<VendorTagDescriptor> desc = vendorIdAndTagDescs.second;
294 const SortedVector<String8>* sectionNames = desc->getAllSectionNames();
295 size_t numSections = sectionNames->size();
296 std::vector<std::vector<HVendorTag>> tagsBySection(numSections);
297 int tagCount = desc->getTagCount();
Jayant Chowdhary925a27f2021-09-13 16:25:07 -0700298 if (tagCount <= 0) {
299 continue;
300 }
Jayant Chowdhary8cf92922018-11-19 15:45:17 -0800301 std::vector<uint32_t> tags(tagCount);
302 desc->getTagArray(tags.data());
303 for (int i = 0; i < tagCount; i++) {
304 HVendorTag vt;
305 vt.tagId = tags[i];
306 vt.tagName = desc->getTagName(tags[i]);
307 vt.tagType = (HCameraMetadataType) desc->getTagType(tags[i]);
308 ssize_t sectionIdx = desc->getSectionIndex(tags[i]);
309 tagsBySection[sectionIdx].push_back(vt);
310 }
311 hVendorTagSections.resize(numSections);
312 for (size_t s = 0; s < numSections; s++) {
Tomasz Wasilczyk12b04a52023-08-11 15:52:22 +0000313 hVendorTagSections[s].sectionName = (*sectionNames)[s].c_str();
Jayant Chowdhary8cf92922018-11-19 15:45:17 -0800314 hVendorTagSections[s].tags = tagsBySection[s];
315 }
316 HProviderIdAndVendorTagSections &hProviderIdAndVendorTagSections =
317 hTagIdsAndVendorTagSections[j];
318 hProviderIdAndVendorTagSections.providerId = vendorIdAndTagDescs.first;
319 hProviderIdAndVendorTagSections.vendorTagSections = std::move(hVendorTagSections);
320 j++;
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700321 }
Jayant Chowdhary8cf92922018-11-19 15:45:17 -0800322 _hidl_cb(HStatus::NO_ERROR, hTagIdsAndVendorTagSections);
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700323 return Void();
324}
325
326} // implementation
327} // V2_0
328} // service
329} // cameraservice
330} // frameworks
331} // android
332