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