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