blob: 79dbfed14c489a7119b2a323dc4ab91c22411ff5 [file] [log] [blame]
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -07001/*
2 * Copyright (C) 2022 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 "AidlCameraService"
18
19#include "AidlCameraService.h"
20#include <aidl/AidlCameraDeviceCallbacks.h>
21#include <aidl/AidlCameraDeviceUser.h>
22#include <aidl/AidlCameraServiceListener.h>
23#include <aidl/AidlUtils.h>
24#include <aidl/android/frameworks/cameraservice/common/CameraMetadataType.h>
25#include <android-base/properties.h>
26#include <android/binder_ibinder.h>
27#include <android/binder_manager.h>
28#include <binder/Status.h>
29#include <hidl/HidlTransportSupport.h>
Avichal Rakesh74b5ae72023-12-27 16:56:45 -080030#include <utils/Utils.h>
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -070031
32namespace android::frameworks::cameraservice::service::implementation {
33
34using ::android::frameworks::cameraservice::device::implementation::AidlCameraDeviceCallbacks;
35using ::android::frameworks::cameraservice::device::implementation::AidlCameraDeviceUser;
36using ::android::hardware::cameraservice::utils::conversion::aidl::areBindersEqual;
37using ::android::hardware::cameraservice::utils::conversion::aidl::cloneToAidl;
38using ::android::hardware::cameraservice::utils::conversion::aidl::convertToAidl;
39using ::android::hardware::cameraservice::utils::conversion::aidl::filterVndkKeys;
40using ::ndk::ScopedAStatus;
41
42// VNDK classes
43using SCameraMetadataType = ::aidl::android::frameworks::cameraservice::common::CameraMetadataType;
44using SVendorTag = ::aidl::android::frameworks::cameraservice::common::VendorTag;
45using SVendorTagSection = ::aidl::android::frameworks::cameraservice::common::VendorTagSection;
46// NDK classes
47using UICameraService = ::android::hardware::ICameraService;
48using UStatus = ::android::binder::Status;
49
50namespace {
51inline ScopedAStatus fromSStatus(const SStatus& s) {
52 return s == SStatus::NO_ERROR ? ScopedAStatus::ok()
53 : ScopedAStatus::fromServiceSpecificError(
54 static_cast<int32_t>(s));
55}
56inline ScopedAStatus fromUStatus(const UStatus& s) {
57 return s.isOk() ? ScopedAStatus::ok() : fromSStatus(convertToAidl(s));
58}
59} // anonymous namespace
60
61std::shared_ptr<AidlCameraService> kCameraService;
62
63bool AidlCameraService::registerService(::android::CameraService* cameraService) {
64 kCameraService = SharedRefBase::make<AidlCameraService>(cameraService);
65 std::string serviceName = SBnCameraService::descriptor;
66 serviceName += "/default";
67 bool isDeclared = AServiceManager_isDeclared(serviceName.c_str());
68 if (!isDeclared) {
69 ALOGI("%s: AIDL vndk not declared.", __FUNCTION__);
70 return false;
71 }
72
73 binder_exception_t registered = AServiceManager_addService(
74 kCameraService->asBinder().get(), serviceName.c_str());
75 ALOGE_IF(registered != EX_NONE,
76 "%s: AIDL VNDK declared, but failed to register service: %d",
77 __FUNCTION__, registered);
78 return registered == EX_NONE;
79}
80
81AidlCameraService::AidlCameraService(::android::CameraService* cameraService):
82 mCameraService(cameraService) {
Avichal Rakesh74b5ae72023-12-27 16:56:45 -080083 mVndkVersion = getVNDKVersionFromProp(__ANDROID_API_FUTURE__);
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -070084}
85ScopedAStatus AidlCameraService::getCameraCharacteristics(const std::string& in_cameraId,
86 SCameraMetadata* _aidl_return) {
87 if (_aidl_return == nullptr) { return fromSStatus(SStatus::ILLEGAL_ARGUMENT); }
88
89 ::android::CameraMetadata cameraMetadata;
Austin Borger71d8f672023-06-01 16:51:35 -070090 UStatus ret = mCameraService->getCameraCharacteristics(in_cameraId,
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -070091 mVndkVersion,
92 /* overrideToPortrait= */ false,
93 &cameraMetadata);
94 if (!ret.isOk()) {
95 if (ret.exceptionCode() != EX_SERVICE_SPECIFIC) {
96 ALOGE("%s: Transaction error when getting camera characteristics"
97 " from camera service: %d.",
98 __FUNCTION__ , ret.exceptionCode());
99 return fromUStatus(ret);
100 }
101 switch (ret.serviceSpecificErrorCode()) {
102 case UICameraService::ERROR_ILLEGAL_ARGUMENT:
103 ALOGE("%s: Camera ID %s does not exist!", __FUNCTION__, in_cameraId.c_str());
104 return fromSStatus(SStatus::ILLEGAL_ARGUMENT);
105 default:
106 ALOGE("Get camera characteristics from camera service failed: %s",
Tomasz Wasilczyk67b5ebc2023-08-30 17:45:57 +0000107 ret.toString8().c_str());
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -0700108 return fromUStatus(ret);
109 }
110 }
111
112 if (filterVndkKeys(mVndkVersion, cameraMetadata) != OK) {
113 ALOGE("%s: Unable to filter vndk metadata keys for version %d",
114 __FUNCTION__, mVndkVersion);
115 return fromSStatus(SStatus::UNKNOWN_ERROR);
116 }
117
118 const camera_metadata_t* rawMetadata = cameraMetadata.getAndLock();
119 cloneToAidl(rawMetadata, _aidl_return);
120 cameraMetadata.unlock(rawMetadata);
121
122 return ScopedAStatus::ok();
123}
124ndk::ScopedAStatus AidlCameraService::connectDevice(
125 const std::shared_ptr<SICameraDeviceCallback>& in_callback,
126 const std::string& in_cameraId,
127 std::shared_ptr<SICameraDeviceUser>* _aidl_return) {
128 // Here, we first get NDK ICameraDeviceUser from mCameraService, then save
129 // that interface in the newly created AidlCameraDeviceUser impl class.
130 if (mCameraService == nullptr) {
131 return fromSStatus(SStatus::UNKNOWN_ERROR);
132 }
133 sp<hardware::camera2::ICameraDeviceUser> unstableDevice = nullptr;
134 // Create a hardware::camera2::ICameraDeviceCallback object which internally
135 // calls callback functions passed through hCallback.
136 sp<AidlCameraDeviceCallbacks> hybridCallbacks = new AidlCameraDeviceCallbacks(in_callback);
137 if (!hybridCallbacks->initializeLooper(mVndkVersion)) {
138 ALOGE("Unable to handle callbacks on device, cannot connect");
139 return fromSStatus(SStatus::UNKNOWN_ERROR);
140 }
141 sp<hardware::camera2::ICameraDeviceCallbacks> callbacks = hybridCallbacks;
142 binder::Status serviceRet = mCameraService->connectDevice(
143 callbacks,
Austin Borger71d8f672023-06-01 16:51:35 -0700144 in_cameraId,
145 std::string(),
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -0700146 /* clientFeatureId= */{},
147 hardware::ICameraService::USE_CALLING_UID,
148 /* scoreOffset= */ 0,
149 /* targetSdkVersion= */ __ANDROID_API_FUTURE__,
150 /* overrideToPortrait= */ false,
151 &unstableDevice);
152 if (!serviceRet.isOk()) {
153 ALOGE("%s: Unable to connect to camera device: %s", __FUNCTION__,
154 serviceRet.toString8().c_str());
155 return fromUStatus(serviceRet);
156 }
157
158 // Now we create a AidlCameraDeviceUser class, store the unstableDevice in it,
159 // and return that back. All calls on that interface will be forwarded to
160 // the NDK AIDL interface.
161 std::shared_ptr<AidlCameraDeviceUser> stableDevice =
162 ndk::SharedRefBase::make<AidlCameraDeviceUser>(unstableDevice);
163 if (!stableDevice->initStatus()) {
164 ALOGE("%s: Unable to initialize camera device AIDL wrapper", __FUNCTION__);
165 return fromSStatus(SStatus::UNKNOWN_ERROR);
166 }
167 hybridCallbacks->setCaptureResultMetadataQueue(
168 stableDevice->getCaptureResultMetadataQueue());
169 *_aidl_return = stableDevice;
170 return ScopedAStatus::ok();
171}
172void AidlCameraService::addToListenerCacheLocked(
173 std::shared_ptr<SICameraServiceListener> stableCsListener,
174 sp<UICameraServiceListener> csListener) {
175 mListeners.emplace_back(std::make_pair(stableCsListener, csListener));
176}
177sp<UICameraServiceListener> AidlCameraService::searchListenerCacheLocked(
178 const std::shared_ptr<SICameraServiceListener>& listener, bool removeIfFound) {
179 // Go through the mListeners list and compare the listener with the VNDK AIDL
180 // listener registered.
181 if (listener == nullptr) {
182 return nullptr;
183 }
184
185 auto it = mListeners.begin();
186 sp<UICameraServiceListener> csListener = nullptr;
187 for (;it != mListeners.end(); it++) {
188 if (areBindersEqual(listener->asBinder(), it->first->asBinder())) {
189 break;
190 }
191 }
192 if (it != mListeners.end()) {
193 csListener = it->second;
194 if (removeIfFound) {
195 mListeners.erase(it);
196 }
197 }
198 return csListener;
199}
200ndk::ScopedAStatus AidlCameraService::addListener(
201 const std::shared_ptr<SICameraServiceListener>& in_listener,
202 std::vector<SCameraStatusAndId>* _aidl_return) {
203 std::vector<hardware::CameraStatus> cameraStatusAndIds{};
204 SStatus status = addListenerInternal(
205 in_listener, &cameraStatusAndIds);
206 if (status != SStatus::NO_ERROR) {
207 return fromSStatus(status);
208 }
209
210 // Convert cameraStatusAndIds to VNDK AIDL
211 convertToAidl(cameraStatusAndIds, _aidl_return);
212 return ScopedAStatus::ok();
213}
214SStatus AidlCameraService::addListenerInternal(
215 const std::shared_ptr<SICameraServiceListener>& listener,
216 std::vector<hardware::CameraStatus>* cameraStatusAndIds) {
217 if (mCameraService == nullptr) {
218 return SStatus::UNKNOWN_ERROR;
219 }
220 if (listener == nullptr || cameraStatusAndIds == nullptr) {
221 ALOGE("%s listener and cameraStatusAndIds must not be NULL", __FUNCTION__);
222 return SStatus::ILLEGAL_ARGUMENT;
223 }
224 sp<UICameraServiceListener> csListener = nullptr;
225 // Check the cache for previously registered callbacks
226 {
227 Mutex::Autolock l(mListenerListLock);
228 csListener = searchListenerCacheLocked(listener);
229 if (csListener == nullptr) {
230 // Wrap a listener with AidlCameraServiceListener and pass it to
231 // CameraService.
232 csListener = sp<AidlCameraServiceListener>::make(listener);
233 // Add to cache
234 addToListenerCacheLocked(listener, csListener);
235 } else {
236 ALOGE("%s: Trying to add a listener %p already registered",
237 __FUNCTION__, listener.get());
238 return SStatus::ILLEGAL_ARGUMENT;
239 }
240 }
241 binder::Status serviceRet =
242 mCameraService->addListenerHelper(csListener, cameraStatusAndIds, true);
243 if (!serviceRet.isOk()) {
244 ALOGE("%s: Unable to add camera device status listener", __FUNCTION__);
245 return convertToAidl(serviceRet);
246 }
247
248 cameraStatusAndIds->erase(std::remove_if(cameraStatusAndIds->begin(),
249 cameraStatusAndIds->end(),
250 [this](const hardware::CameraStatus& s) {
251 bool supportsHAL3 = false;
252 binder::Status sRet =
Austin Borger71d8f672023-06-01 16:51:35 -0700253 mCameraService->supportsCameraApi(s.cameraId,
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -0700254 UICameraService::API_VERSION_2, &supportsHAL3);
255 return !sRet.isOk() || !supportsHAL3;
256 }), cameraStatusAndIds->end());
257
258 return SStatus::NO_ERROR;
259}
260ndk::ScopedAStatus AidlCameraService::removeListener(
261 const std::shared_ptr<SICameraServiceListener>& in_listener) {
262 if (in_listener == nullptr) {
263 ALOGE("%s listener must not be NULL", __FUNCTION__);
264 return fromSStatus(SStatus::ILLEGAL_ARGUMENT);
265 }
266 sp<UICameraServiceListener> csListener = nullptr;
267 {
268 Mutex::Autolock l(mListenerListLock);
269 csListener = searchListenerCacheLocked(in_listener, /*removeIfFound*/true);
270 }
271 if (csListener != nullptr) {
272 mCameraService->removeListener(csListener);
273 } else {
274 ALOGE("%s Removing unregistered listener %p", __FUNCTION__, in_listener.get());
275 return fromSStatus(SStatus::ILLEGAL_ARGUMENT);
276 }
277 return ScopedAStatus::ok();
278}
279ndk::ScopedAStatus AidlCameraService::getCameraVendorTagSections(
280 std::vector<SProviderIdAndVendorTagSections>* _aidl_return) {
281 sp<VendorTagDescriptorCache> gCache = VendorTagDescriptorCache::getGlobalVendorTagCache();
282 if (gCache == nullptr) {
283 return fromSStatus(SStatus::UNKNOWN_ERROR);
284 }
285
286 const std::unordered_map<metadata_vendor_id_t, sp<android::VendorTagDescriptor>>
287 &vendorIdsAndTagDescs = gCache->getVendorIdsAndTagDescriptors();
288 if (vendorIdsAndTagDescs.empty()) {
289 return fromSStatus(SStatus::UNKNOWN_ERROR);
290 }
291
292 std::vector<SProviderIdAndVendorTagSections>& tagIdAndVendorTagSections = *_aidl_return;
293 tagIdAndVendorTagSections.resize(vendorIdsAndTagDescs.size());
294 size_t j = 0;
295 for (auto &vendorIdAndTagDescs : vendorIdsAndTagDescs) {
296 std::vector<SVendorTagSection> vendorTagSections;
297 sp<VendorTagDescriptor> desc = vendorIdAndTagDescs.second;
298 const SortedVector<String8>* sectionNames = desc->getAllSectionNames();
299 size_t numSections = sectionNames->size();
300 std::vector<std::vector<SVendorTag>> tagsBySection(numSections);
301 int tagCount = desc->getTagCount();
302 if (tagCount <= 0) {
303 continue;
304 }
305 std::vector<uint32_t> tags(tagCount);
306 desc->getTagArray(tags.data());
307 for (int i = 0; i < tagCount; i++) {
308 SVendorTag vt;
309 vt.tagId = tags[i];
310 vt.tagName = desc->getTagName(tags[i]);
311 vt.tagType = (SCameraMetadataType) desc->getTagType(tags[i]);
312 ssize_t sectionIdx = desc->getSectionIndex(tags[i]);
313 tagsBySection[sectionIdx].push_back(vt);
314 }
315 vendorTagSections.resize(numSections);
316 for (size_t s = 0; s < numSections; s++) {
Tomasz Wasilczyk67b5ebc2023-08-30 17:45:57 +0000317 vendorTagSections[s].sectionName = (*sectionNames)[s].c_str();
Avichal Rakeshfcb78cb2022-10-27 15:45:54 -0700318 vendorTagSections[s].tags = tagsBySection[s];
319 }
320 SProviderIdAndVendorTagSections & prvdrIdAndVendorTagSection =
321 tagIdAndVendorTagSections[j];
322 prvdrIdAndVendorTagSection.providerId = vendorIdAndTagDescs.first;
323 prvdrIdAndVendorTagSection.vendorTagSections = std::move(vendorTagSections);
324 j++;
325 }
326 return ScopedAStatus::ok();
327}
328
329} // namespace android::frameworks::cameraservice::service::implementation