1 /*
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 <camera/CameraUtils.h>
30 #include <hidl/HidlTransportSupport.h>
31 #include <utils/Utils.h>
32
33 namespace android::frameworks::cameraservice::service::implementation {
34
35 using ::android::frameworks::cameraservice::device::implementation::AidlCameraDeviceCallbacks;
36 using ::android::frameworks::cameraservice::device::implementation::AidlCameraDeviceUser;
37 using ::android::hardware::cameraservice::utils::conversion::aidl::areBindersEqual;
38 using ::android::hardware::cameraservice::utils::conversion::aidl::cloneToAidl;
39 using ::android::hardware::cameraservice::utils::conversion::aidl::convertToAidl;
40 using ::android::hardware::cameraservice::utils::conversion::aidl::filterVndkKeys;
41 using hardware::BnCameraService::ROTATION_OVERRIDE_NONE;
42 using ::ndk::ScopedAStatus;
43
44 // VNDK classes
45 using SCameraMetadataType = ::aidl::android::frameworks::cameraservice::common::CameraMetadataType;
46 using SVendorTag = ::aidl::android::frameworks::cameraservice::common::VendorTag;
47 using SVendorTagSection = ::aidl::android::frameworks::cameraservice::common::VendorTagSection;
48 // NDK classes
49 using UICameraService = ::android::hardware::ICameraService;
50 using UStatus = ::android::binder::Status;
51
52 namespace {
fromSStatus(const SStatus & s)53 inline ScopedAStatus fromSStatus(const SStatus& s) {
54 return s == SStatus::NO_ERROR ? ScopedAStatus::ok()
55 : ScopedAStatus::fromServiceSpecificError(
56 static_cast<int32_t>(s));
57 }
fromUStatus(const UStatus & s)58 inline ScopedAStatus fromUStatus(const UStatus& s) {
59 return s.isOk() ? ScopedAStatus::ok() : fromSStatus(convertToAidl(s));
60 }
61 } // anonymous namespace
62
63 std::shared_ptr<AidlCameraService> kCameraService;
64
registerService(::android::CameraService * cameraService)65 bool 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
AidlCameraService(::android::CameraService * cameraService)83 AidlCameraService::AidlCameraService(::android::CameraService* cameraService):
84 mCameraService(cameraService) {
85 mVndkVersion = getVNDKVersionFromProp(__ANDROID_API_FUTURE__);
86 }
getCameraCharacteristics(const std::string & in_cameraId,SCameraMetadata * _aidl_return)87 ScopedAStatus 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;
92 UStatus ret = mCameraService->getCameraCharacteristics(in_cameraId,
93 mVndkVersion,
94 ROTATION_OVERRIDE_NONE,
95 kDefaultDeviceId,
96 /* devicePolicy= */ 0,
97 &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",
111 ret.toString8().c_str());
112 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 }
connectDevice(const std::shared_ptr<SICameraDeviceCallback> & in_callback,const std::string & in_cameraId,std::shared_ptr<SICameraDeviceUser> * _aidl_return)128 ndk::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,
148 in_cameraId,
149 std::string(),
150 /* clientFeatureId= */{},
151 hardware::ICameraService::USE_CALLING_UID,
152 /* scoreOffset= */ 0,
153 /* targetSdkVersion= */ __ANDROID_API_FUTURE__,
154 ROTATION_OVERRIDE_NONE,
155 kDefaultDeviceId,
156 /* devicePolicy= */ 0,
157 &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 }
addToListenerCacheLocked(std::shared_ptr<SICameraServiceListener> stableCsListener,sp<UICameraServiceListener> csListener)178 void AidlCameraService::addToListenerCacheLocked(
179 std::shared_ptr<SICameraServiceListener> stableCsListener,
180 sp<UICameraServiceListener> csListener) {
181 mListeners.emplace_back(std::make_pair(stableCsListener, csListener));
182 }
searchListenerCacheLocked(const std::shared_ptr<SICameraServiceListener> & listener,bool removeIfFound)183 sp<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 }
addListener(const std::shared_ptr<SICameraServiceListener> & in_listener,std::vector<SCameraStatusAndId> * _aidl_return)206 ndk::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 }
addListenerInternal(const std::shared_ptr<SICameraServiceListener> & listener,std::vector<hardware::CameraStatus> * cameraStatusAndIds)220 SStatus 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 =
259 mCameraService->supportsCameraApi(s.cameraId,
260 UICameraService::API_VERSION_2, &supportsHAL3);
261 return !sRet.isOk() || !supportsHAL3;
262 }), cameraStatusAndIds->end());
263
264 return SStatus::NO_ERROR;
265 }
removeListener(const std::shared_ptr<SICameraServiceListener> & in_listener)266 ndk::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 }
getCameraVendorTagSections(std::vector<SProviderIdAndVendorTagSections> * _aidl_return)285 ndk::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++) {
323 vendorTagSections[s].sectionName = (*sectionNames)[s].c_str();
324 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
336