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