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