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