1 /*
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
17 #include <android-base/properties.h>
18
19 #include <hidl/AidlCameraDeviceCallbacks.h>
20 #include <hidl/AidlCameraServiceListener.h>
21 #include <hidl/HidlCameraService.h>
22 #include <hidl/HidlCameraDeviceUser.h>
23 #include <hidl/Utils.h>
24 #include <aidl/AidlUtils.h>
25
26 #include <hidl/HidlTransportSupport.h>
27
28 #include <camera/CameraUtils.h>
29 #include <utils/AttributionAndPermissionUtils.h>
30 #include <utils/Utils.h>
31
32 namespace android {
33 namespace frameworks {
34 namespace cameraservice {
35 namespace service {
36 namespace V2_0 {
37 namespace implementation {
38
39 using frameworks::cameraservice::service::V2_0::implementation::HidlCameraService;
40 using hardware::hidl_vec;
41 using hardware::BnCameraService::ROTATION_OVERRIDE_NONE;
42 using hardware::cameraservice::utils::conversion::convertToHidl;
43 using hardware::cameraservice::utils::conversion::B2HStatus;
44 using hardware::Void;
45 using hardware::cameraservice::utils::conversion::aidl::filterVndkKeys;
46
47 using device::V2_0::implementation::H2BCameraDeviceCallbacks;
48 using device::V2_1::implementation::HidlCameraDeviceUser;
49 using service::V2_0::implementation::H2BCameraServiceListener;
50 using HCameraMetadataType = frameworks::cameraservice::common::V2_0::CameraMetadataType;
51 using HVendorTag = frameworks::cameraservice::common::V2_0::VendorTag;
52 using HVendorTagSection = frameworks::cameraservice::common::V2_0::VendorTagSection;
53 using HProviderIdAndVendorTagSections =
54 frameworks::cameraservice::common::V2_0::ProviderIdAndVendorTagSections;
55
56 sp<HidlCameraService> gHidlCameraService;
57
getInstance(android::CameraService * cs)58 sp<HidlCameraService> HidlCameraService::getInstance(android::CameraService *cs) {
59 gHidlCameraService = new HidlCameraService(cs);
60 return gHidlCameraService;
61 }
62
HidlCameraService(android::CameraService * cs)63 HidlCameraService::HidlCameraService(android::CameraService *cs) : mAidlICameraService(cs) {
64 mVndkVersion = getVNDKVersion();
65 }
66
67 Return<void>
getCameraCharacteristics(const hidl_string & cameraId,getCameraCharacteristics_cb _hidl_cb)68 HidlCameraService::getCameraCharacteristics(const hidl_string& cameraId,
69 getCameraCharacteristics_cb _hidl_cb) {
70 android::CameraMetadata cameraMetadata;
71 HStatus status = HStatus::NO_ERROR;
72 AttributionSourceState clientAttribution =
73 AttributionAndPermissionUtils::buildAttributionSource(
74 hardware::ICameraService::USE_CALLING_PID,
75 hardware::ICameraService::USE_CALLING_UID,
76 kDefaultDeviceId);
77 binder::Status serviceRet =
78 mAidlICameraService->getCameraCharacteristics(cameraId,
79 /*targetSdkVersion*/__ANDROID_API_FUTURE__, ROTATION_OVERRIDE_NONE,
80 clientAttribution, 0, &cameraMetadata);
81 HCameraMetadata hidlMetadata;
82 if (!serviceRet.isOk()) {
83 switch(serviceRet.serviceSpecificErrorCode()) {
84 // No ERROR_CAMERA_DISCONNECTED since we're in the same process.
85 case hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT:
86 ALOGE("%s: Camera ID %s does not exist!", __FUNCTION__, cameraId.c_str());
87 status = HStatus::ILLEGAL_ARGUMENT;
88 break;
89 default:
90 ALOGE("Get camera characteristics from camera service failed: %s",
91 serviceRet.toString8().c_str());
92 status = B2HStatus(serviceRet);
93 }
94 _hidl_cb(status, hidlMetadata);
95 return Void();
96 }
97 if (filterVndkKeys(mVndkVersion, cameraMetadata) != OK) {
98 ALOGE("%s: Unable to filter vndk metadata keys for version %d", __FUNCTION__, mVndkVersion);
99 _hidl_cb(HStatus::UNKNOWN_ERROR, hidlMetadata);
100 return Void();
101 }
102 const camera_metadata_t *rawMetadata = cameraMetadata.getAndLock();
103 convertToHidl(rawMetadata, &hidlMetadata);
104 _hidl_cb(status, hidlMetadata);
105 cameraMetadata.unlock(rawMetadata);
106 return Void();
107 }
108
connectDevice(const sp<HCameraDeviceCallback> & hCallback,const hidl_string & cameraId,connectDevice_cb _hidl_cb)109 Return<void> HidlCameraService::connectDevice(const sp<HCameraDeviceCallback>& hCallback,
110 const hidl_string& cameraId,
111 connectDevice_cb _hidl_cb) {
112 // Here, we first get ICameraDeviceUser from mAidlICameraService, then save
113 // that interface in the newly created HidlCameraDeviceUser impl class.
114 if (mAidlICameraService == nullptr) {
115 _hidl_cb(HStatus::UNKNOWN_ERROR, nullptr);
116 return Void();
117 }
118 sp<hardware::camera2::ICameraDeviceUser> deviceRemote = nullptr;
119 // Create a hardware::camera2::ICameraDeviceCallback object which internally
120 // calls callback functions passed through hCallback.
121 sp<H2BCameraDeviceCallbacks> hybridCallbacks = new H2BCameraDeviceCallbacks(hCallback);
122 if (!hybridCallbacks->initializeLooper(mVndkVersion)) {
123 ALOGE("Unable to handle callbacks on device, cannot connect");
124 _hidl_cb(HStatus::UNKNOWN_ERROR, nullptr);
125 return Void();
126 }
127 sp<hardware::camera2::ICameraDeviceCallbacks> callbacks = hybridCallbacks;
128 AttributionSourceState clientAttribution =
129 AttributionAndPermissionUtils::buildAttributionSource(
130 hardware::ICameraService::USE_CALLING_PID,
131 hardware::ICameraService::USE_CALLING_UID,
132 kDefaultDeviceId);
133 clientAttribution.packageName = "";
134 clientAttribution.attributionTag = std::nullopt;
135 binder::Status serviceRet = mAidlICameraService->connectDeviceVendor(
136 callbacks, cameraId, 0/*oomScoreOffset*/,
137 /*targetSdkVersion*/__ANDROID_API_FUTURE__, ROTATION_OVERRIDE_NONE,
138 clientAttribution, /*devicePolicy*/0, /*sharedMode*/false, /*out*/&deviceRemote);
139 HStatus status = HStatus::NO_ERROR;
140 if (!serviceRet.isOk()) {
141 ALOGE("%s: Unable to connect to camera device", __FUNCTION__);
142 status = B2HStatus(serviceRet);
143 _hidl_cb(status, nullptr);
144 return Void();
145 }
146 // Now we create a HidlCameraDeviceUser class, store the deviceRemote in it,
147 // and return that back. All calls on that interface will be forwarded to
148 // the AIDL interface.
149 sp<HidlCameraDeviceUser> hDeviceRemote = new HidlCameraDeviceUser(deviceRemote);
150 if (!hDeviceRemote->initStatus()) {
151 ALOGE("%s: Unable to initialize camera device HIDL wrapper", __FUNCTION__);
152 _hidl_cb(HStatus::UNKNOWN_ERROR, nullptr);
153 return Void();
154 }
155 hybridCallbacks->setCaptureResultMetadataQueue(hDeviceRemote->getCaptureResultMetadataQueue());
156 _hidl_cb(status, hDeviceRemote);
157 return Void();
158 }
159
addToListenerCacheLocked(sp<HCameraServiceListener> hListener,sp<hardware::ICameraServiceListener> csListener)160 void HidlCameraService::addToListenerCacheLocked(sp<HCameraServiceListener> hListener,
161 sp<hardware::ICameraServiceListener> csListener) {
162 mListeners.emplace_back(std::make_pair(hListener, csListener));
163 }
164
165 sp<hardware::ICameraServiceListener>
searchListenerCacheLocked(sp<HCameraServiceListener> hListener,bool shouldRemove)166 HidlCameraService::searchListenerCacheLocked(sp<HCameraServiceListener> hListener,
167 bool shouldRemove) {
168 // Go through the mListeners list and compare the listener with the HIDL
169 // listener registered.
170 auto it = mListeners.begin();
171 sp<ICameraServiceListener> csListener = nullptr;
172 for (;it != mListeners.end(); it++) {
173 if (hardware::interfacesEqual(it->first, hListener)) {
174 break;
175 }
176 }
177 if (it != mListeners.end()) {
178 csListener = it->second;
179 if (shouldRemove) {
180 mListeners.erase(it);
181 }
182 }
183 return csListener;
184 }
185
addListener(const sp<HCameraServiceListener> & hCsListener,addListener_cb _hidl_cb)186 Return<void> HidlCameraService::addListener(const sp<HCameraServiceListener>& hCsListener,
187 addListener_cb _hidl_cb) {
188 std::vector<hardware::CameraStatus> cameraStatusAndIds{};
189 HStatus status = addListenerInternal<HCameraServiceListener>(
190 hCsListener, &cameraStatusAndIds);
191 if (status != HStatus::NO_ERROR) {
192 _hidl_cb(status, {});
193 return Void();
194 }
195
196 hidl_vec<HCameraStatusAndId> hCameraStatusAndIds;
197 //Convert cameraStatusAndIds to HIDL and call callback
198 convertToHidl(cameraStatusAndIds, &hCameraStatusAndIds);
199 _hidl_cb(status, hCameraStatusAndIds);
200
201 return Void();
202 }
203
addListener_2_1(const sp<HCameraServiceListener2_1> & hCsListener,addListener_2_1_cb _hidl_cb)204 Return<void> HidlCameraService::addListener_2_1(const sp<HCameraServiceListener2_1>& hCsListener,
205 addListener_2_1_cb _hidl_cb) {
206 std::vector<hardware::CameraStatus> cameraStatusAndIds{};
207 HStatus status = addListenerInternal<HCameraServiceListener2_1>(
208 hCsListener, &cameraStatusAndIds);
209 if (status != HStatus::NO_ERROR) {
210 _hidl_cb(status, {});
211 return Void();
212 }
213
214 hidl_vec<frameworks::cameraservice::service::V2_1::CameraStatusAndId> hCameraStatusAndIds;
215 //Convert cameraStatusAndIds to HIDL and call callback
216 convertToHidl(cameraStatusAndIds, &hCameraStatusAndIds);
217 _hidl_cb(status, hCameraStatusAndIds);
218
219 return Void();
220 }
221
222 template<class T>
addListenerInternal(const sp<T> & hCsListener,std::vector<hardware::CameraStatus> * cameraStatusAndIds)223 HStatus HidlCameraService::addListenerInternal(const sp<T>& hCsListener,
224 std::vector<hardware::CameraStatus>* cameraStatusAndIds) {
225 if (mAidlICameraService == nullptr) {
226 return HStatus::UNKNOWN_ERROR;
227 }
228 if (hCsListener == nullptr || cameraStatusAndIds == nullptr) {
229 ALOGE("%s listener and cameraStatusAndIds must not be NULL", __FUNCTION__);
230 return HStatus::ILLEGAL_ARGUMENT;
231 }
232 sp<hardware::ICameraServiceListener> csListener = nullptr;
233 // Check the cache for previously registered callbacks
234 {
235 Mutex::Autolock l(mListenerListLock);
236 csListener = searchListenerCacheLocked(hCsListener);
237 if (csListener == nullptr) {
238 // Wrap an hCsListener with AidlCameraServiceListener and pass it to
239 // CameraService.
240 csListener = new H2BCameraServiceListener(hCsListener);
241 // Add to cache
242 addToListenerCacheLocked(hCsListener, csListener);
243 } else {
244 ALOGE("%s: Trying to add a listener %p already registered",
245 __FUNCTION__, hCsListener.get());
246 return HStatus::ILLEGAL_ARGUMENT;
247 }
248 }
249 binder::Status serviceRet =
250 mAidlICameraService->addListenerHelper(csListener, cameraStatusAndIds, true);
251 HStatus status = HStatus::NO_ERROR;
252 if (!serviceRet.isOk()) {
253 ALOGE("%s: Unable to add camera device status listener", __FUNCTION__);
254 status = B2HStatus(serviceRet);
255 return status;
256 }
257 return HStatus::NO_ERROR;
258 }
259
removeListener(const sp<HCameraServiceListener> & hCsListener)260 Return<HStatus> HidlCameraService::removeListener(const sp<HCameraServiceListener>& hCsListener) {
261 if (hCsListener == nullptr) {
262 ALOGE("%s listener must not be NULL", __FUNCTION__);
263 return HStatus::ILLEGAL_ARGUMENT;
264 }
265 sp<ICameraServiceListener> csListener = nullptr;
266 {
267 Mutex::Autolock l(mListenerListLock);
268 csListener = searchListenerCacheLocked(hCsListener, /*removeIfFound*/true);
269 }
270 if (csListener != nullptr) {
271 mAidlICameraService->removeListener(csListener);
272 } else {
273 ALOGE("%s Removing unregistered listener %p", __FUNCTION__, hCsListener.get());
274 return HStatus::ILLEGAL_ARGUMENT;
275 }
276 return HStatus::NO_ERROR;
277 }
278
getCameraVendorTagSections(getCameraVendorTagSections_cb _hidl_cb)279 Return<void> HidlCameraService::getCameraVendorTagSections(getCameraVendorTagSections_cb _hidl_cb) {
280 sp<VendorTagDescriptorCache> gCache = VendorTagDescriptorCache::getGlobalVendorTagCache();
281 if (gCache == nullptr) {
282 _hidl_cb(HStatus::UNKNOWN_ERROR, {});
283 return Void();
284 }
285 const std::unordered_map<metadata_vendor_id_t, sp<android::VendorTagDescriptor>>
286 &vendorIdsAndTagDescs = gCache->getVendorIdsAndTagDescriptors();
287 if (vendorIdsAndTagDescs.size() == 0) {
288 _hidl_cb(HStatus::UNKNOWN_ERROR, {});
289 return Void();
290 }
291
292 hidl_vec<HProviderIdAndVendorTagSections> hTagIdsAndVendorTagSections;
293 hTagIdsAndVendorTagSections.resize(vendorIdsAndTagDescs.size());
294 size_t j = 0;
295 for (auto &vendorIdAndTagDescs : vendorIdsAndTagDescs) {
296 hidl_vec<HVendorTagSection> hVendorTagSections;
297 sp<VendorTagDescriptor> desc = vendorIdAndTagDescs.second;
298 const SortedVector<String8>* sectionNames = desc->getAllSectionNames();
299 size_t numSections = sectionNames->size();
300 std::vector<std::vector<HVendorTag>> 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 HVendorTag vt;
309 vt.tagId = tags[i];
310 vt.tagName = desc->getTagName(tags[i]);
311 vt.tagType = (HCameraMetadataType) desc->getTagType(tags[i]);
312 ssize_t sectionIdx = desc->getSectionIndex(tags[i]);
313 tagsBySection[sectionIdx].push_back(vt);
314 }
315 hVendorTagSections.resize(numSections);
316 for (size_t s = 0; s < numSections; s++) {
317 hVendorTagSections[s].sectionName = (*sectionNames)[s].c_str();
318 hVendorTagSections[s].tags = tagsBySection[s];
319 }
320 HProviderIdAndVendorTagSections &hProviderIdAndVendorTagSections =
321 hTagIdsAndVendorTagSections[j];
322 hProviderIdAndVendorTagSections.providerId = vendorIdAndTagDescs.first;
323 hProviderIdAndVendorTagSections.vendorTagSections = std::move(hVendorTagSections);
324 j++;
325 }
326 _hidl_cb(HStatus::NO_ERROR, hTagIdsAndVendorTagSections);
327 return Void();
328 }
329
330 } // implementation
331 } // V2_0
332 } // service
333 } // cameraservice
334 } // frameworks
335 } // android
336
337