1 /*
2  * Copyright (C) 2015 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_NDEBUG 0
18 #define LOG_TAG "ACameraManager"
19 
20 #include <memory>
21 #include "ACameraManager.h"
22 #include "ACameraMetadata.h"
23 #include "ACameraDevice.h"
24 #include <utils/Vector.h>
25 #include <cutils/properties.h>
26 #include <stdlib.h>
27 #include <camera/VendorTagDescriptor.h>
28 
29 using namespace android::acam;
30 
31 namespace android {
32 namespace acam {
33 // Static member definitions
34 const char* CameraManagerGlobal::kCameraIdKey   = "CameraId";
35 const char* CameraManagerGlobal::kCallbackFpKey = "CallbackFp";
36 const char* CameraManagerGlobal::kContextKey    = "CallbackContext";
37 Mutex                CameraManagerGlobal::sLock;
38 CameraManagerGlobal* CameraManagerGlobal::sInstance = nullptr;
39 
40 CameraManagerGlobal&
getInstance()41 CameraManagerGlobal::getInstance() {
42     Mutex::Autolock _l(sLock);
43     CameraManagerGlobal* instance = sInstance;
44     if (instance == nullptr) {
45         instance = new CameraManagerGlobal();
46         sInstance = instance;
47     }
48     return *instance;
49 }
50 
~CameraManagerGlobal()51 CameraManagerGlobal::~CameraManagerGlobal() {
52     // clear sInstance so next getInstance call knows to create a new one
53     Mutex::Autolock _sl(sLock);
54     sInstance = nullptr;
55     Mutex::Autolock _l(mLock);
56     if (mCameraService != nullptr) {
57         IInterface::asBinder(mCameraService)->unlinkToDeath(mDeathNotifier);
58         mCameraService->removeListener(mCameraServiceListener);
59     }
60     mDeathNotifier.clear();
61     if (mCbLooper != nullptr) {
62         mCbLooper->unregisterHandler(mHandler->id());
63         mCbLooper->stop();
64     }
65     mCbLooper.clear();
66     mHandler.clear();
67     mCameraServiceListener.clear();
68     mCameraService.clear();
69 }
70 
isCameraServiceDisabled()71 static bool isCameraServiceDisabled() {
72     char value[PROPERTY_VALUE_MAX];
73     property_get("config.disable_cameraservice", value, "0");
74     return (strncmp(value, "0", 2) != 0 && strncasecmp(value, "false", 6) != 0);
75 }
76 
getCameraService()77 sp<hardware::ICameraService> CameraManagerGlobal::getCameraService() {
78     Mutex::Autolock _l(mLock);
79     if (mCameraService.get() == nullptr) {
80         if (isCameraServiceDisabled()) {
81             return mCameraService;
82         }
83 
84         sp<IServiceManager> sm = defaultServiceManager();
85         sp<IBinder> binder;
86         do {
87             binder = sm->getService(String16(kCameraServiceName));
88             if (binder != nullptr) {
89                 break;
90             }
91             ALOGW("CameraService not published, waiting...");
92             usleep(kCameraServicePollDelay);
93         } while(true);
94         if (mDeathNotifier == nullptr) {
95             mDeathNotifier = new DeathNotifier(this);
96         }
97         binder->linkToDeath(mDeathNotifier);
98         mCameraService = interface_cast<hardware::ICameraService>(binder);
99 
100         // Setup looper thread to perfrom availiability callbacks
101         if (mCbLooper == nullptr) {
102             mCbLooper = new ALooper;
103             mCbLooper->setName("C2N-mgr-looper");
104             status_t err = mCbLooper->start(
105                     /*runOnCallingThread*/false,
106                     /*canCallJava*/       true,
107                     PRIORITY_DEFAULT);
108             if (err != OK) {
109                 ALOGE("%s: Unable to start camera service listener looper: %s (%d)",
110                         __FUNCTION__, strerror(-err), err);
111                 mCbLooper.clear();
112                 return nullptr;
113             }
114             if (mHandler == nullptr) {
115                 mHandler = new CallbackHandler();
116             }
117             mCbLooper->registerHandler(mHandler);
118         }
119 
120         // register ICameraServiceListener
121         if (mCameraServiceListener == nullptr) {
122             mCameraServiceListener = new CameraServiceListener(this);
123         }
124         std::vector<hardware::CameraStatus> cameraStatuses{};
125         mCameraService->addListener(mCameraServiceListener, &cameraStatuses);
126         for (auto& c : cameraStatuses) {
127             onStatusChangedLocked(c.status, c.cameraId);
128         }
129 
130         // setup vendor tags
131         sp<VendorTagDescriptor> desc = new VendorTagDescriptor();
132         binder::Status ret = mCameraService->getCameraVendorTagDescriptor(/*out*/desc.get());
133 
134         if (ret.isOk()) {
135             if (0 < desc->getTagCount()) {
136                 status_t err = VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc);
137                 if (err != OK) {
138                     ALOGE("%s: Failed to set vendor tag descriptors, received error %s (%d)",
139                             __FUNCTION__, strerror(-err), err);
140                 }
141             } else {
142                 sp<VendorTagDescriptorCache> cache =
143                         new VendorTagDescriptorCache();
144                 binder::Status res =
145                         mCameraService->getCameraVendorTagCache(
146                                 /*out*/cache.get());
147                 if (res.serviceSpecificErrorCode() ==
148                         hardware::ICameraService::ERROR_DISCONNECTED) {
149                     // No camera module available, not an error on devices with no cameras
150                     VendorTagDescriptorCache::clearGlobalVendorTagCache();
151                 } else if (res.isOk()) {
152                     status_t err =
153                             VendorTagDescriptorCache::setAsGlobalVendorTagCache(
154                                     cache);
155                     if (err != OK) {
156                         ALOGE("%s: Failed to set vendor tag cache,"
157                                 "received error %s (%d)", __FUNCTION__,
158                                 strerror(-err), err);
159                     }
160                 } else {
161                     VendorTagDescriptorCache::clearGlobalVendorTagCache();
162                     ALOGE("%s: Failed to setup vendor tag cache: %s",
163                             __FUNCTION__, res.toString8().string());
164                 }
165             }
166         } else if (ret.serviceSpecificErrorCode() ==
167                 hardware::ICameraService::ERROR_DEPRECATED_HAL) {
168             ALOGW("%s: Camera HAL too old; does not support vendor tags",
169                     __FUNCTION__);
170             VendorTagDescriptor::clearGlobalVendorTagDescriptor();
171         } else {
172             ALOGE("%s: Failed to get vendor tag descriptors: %s",
173                     __FUNCTION__, ret.toString8().string());
174         }
175     }
176     ALOGE_IF(mCameraService == nullptr, "no CameraService!?");
177     return mCameraService;
178 }
179 
binderDied(const wp<IBinder> &)180 void CameraManagerGlobal::DeathNotifier::binderDied(const wp<IBinder>&)
181 {
182     ALOGE("Camera service binderDied!");
183     sp<CameraManagerGlobal> cm = mCameraManager.promote();
184     if (cm != nullptr) {
185         AutoMutex lock(cm->mLock);
186         for (auto& pair : cm->mDeviceStatusMap) {
187             const String8 &cameraId = pair.first;
188             cm->onStatusChangedLocked(
189                     CameraServiceListener::STATUS_NOT_PRESENT, cameraId);
190         }
191         cm->mCameraService.clear();
192         // TODO: consider adding re-connect call here?
193     }
194 }
195 
registerExtendedAvailabilityCallback(const ACameraManager_ExtendedAvailabilityCallbacks * callback)196 void CameraManagerGlobal::registerExtendedAvailabilityCallback(
197         const ACameraManager_ExtendedAvailabilityCallbacks *callback) {
198     Mutex::Autolock _l(mLock);
199     Callback cb(callback);
200     mCallbacks.insert(cb);
201 }
202 
unregisterExtendedAvailabilityCallback(const ACameraManager_ExtendedAvailabilityCallbacks * callback)203 void CameraManagerGlobal::unregisterExtendedAvailabilityCallback(
204         const ACameraManager_ExtendedAvailabilityCallbacks *callback) {
205     Mutex::Autolock _l(mLock);
206     Callback cb(callback);
207     mCallbacks.erase(cb);
208 }
209 
registerAvailabilityCallback(const ACameraManager_AvailabilityCallbacks * callback)210 void CameraManagerGlobal::registerAvailabilityCallback(
211         const ACameraManager_AvailabilityCallbacks *callback) {
212     Mutex::Autolock _l(mLock);
213     Callback cb(callback);
214     auto pair = mCallbacks.insert(cb);
215     // Send initial callbacks if callback is newly registered
216     if (pair.second) {
217         for (auto& pair : mDeviceStatusMap) {
218             const String8& cameraId = pair.first;
219             int32_t status = pair.second;
220 
221             sp<AMessage> msg = new AMessage(kWhatSendSingleCallback, mHandler);
222             ACameraManager_AvailabilityCallback cb = isStatusAvailable(status) ?
223                     callback->onCameraAvailable : callback->onCameraUnavailable;
224             msg->setPointer(kCallbackFpKey, (void *) cb);
225             msg->setPointer(kContextKey, callback->context);
226             msg->setString(kCameraIdKey, AString(cameraId));
227             msg->post();
228         }
229     }
230 }
231 
unregisterAvailabilityCallback(const ACameraManager_AvailabilityCallbacks * callback)232 void CameraManagerGlobal::unregisterAvailabilityCallback(
233         const ACameraManager_AvailabilityCallbacks *callback) {
234     Mutex::Autolock _l(mLock);
235     Callback cb(callback);
236     mCallbacks.erase(cb);
237 }
238 
getCameraIdList(std::vector<String8> * cameraIds)239 void CameraManagerGlobal::getCameraIdList(std::vector<String8>* cameraIds) {
240     // Ensure that we have initialized/refreshed the list of available devices
241     auto cs = getCameraService();
242     Mutex::Autolock _l(mLock);
243 
244     for(auto& deviceStatus : mDeviceStatusMap) {
245         if (deviceStatus.second == hardware::ICameraServiceListener::STATUS_NOT_PRESENT ||
246                 deviceStatus.second == hardware::ICameraServiceListener::STATUS_ENUMERATING) {
247             continue;
248         }
249         bool camera2Support = false;
250         binder::Status serviceRet = cs->supportsCameraApi(String16(deviceStatus.first),
251                 hardware::ICameraService::API_VERSION_2, &camera2Support);
252         if (!serviceRet.isOk() || !camera2Support) {
253             continue;
254         }
255         cameraIds->push_back(deviceStatus.first);
256     }
257 }
258 
validStatus(int32_t status)259 bool CameraManagerGlobal::validStatus(int32_t status) {
260     switch (status) {
261         case hardware::ICameraServiceListener::STATUS_NOT_PRESENT:
262         case hardware::ICameraServiceListener::STATUS_PRESENT:
263         case hardware::ICameraServiceListener::STATUS_ENUMERATING:
264         case hardware::ICameraServiceListener::STATUS_NOT_AVAILABLE:
265             return true;
266         default:
267             return false;
268     }
269 }
270 
isStatusAvailable(int32_t status)271 bool CameraManagerGlobal::isStatusAvailable(int32_t status) {
272     switch (status) {
273         case hardware::ICameraServiceListener::STATUS_PRESENT:
274             return true;
275         default:
276             return false;
277     }
278 }
279 
onMessageReceived(const sp<AMessage> & msg)280 void CameraManagerGlobal::CallbackHandler::onMessageReceived(
281         const sp<AMessage> &msg) {
282     switch (msg->what()) {
283         case kWhatSendSingleCallback:
284         {
285             ACameraManager_AvailabilityCallback cb;
286             void* context;
287             AString cameraId;
288             bool found = msg->findPointer(kCallbackFpKey, (void**) &cb);
289             if (!found) {
290                 ALOGE("%s: Cannot find camera callback fp!", __FUNCTION__);
291                 return;
292             }
293             found = msg->findPointer(kContextKey, &context);
294             if (!found) {
295                 ALOGE("%s: Cannot find callback context!", __FUNCTION__);
296                 return;
297             }
298             found = msg->findString(kCameraIdKey, &cameraId);
299             if (!found) {
300                 ALOGE("%s: Cannot find camera ID!", __FUNCTION__);
301                 return;
302             }
303             (*cb)(context, cameraId.c_str());
304             break;
305         }
306         case kWhatSendSingleAccessCallback:
307         {
308             ACameraManager_AccessPrioritiesChangedCallback cb;
309             void* context;
310             AString cameraId;
311             bool found = msg->findPointer(kCallbackFpKey, (void**) &cb);
312             if (!found) {
313                 ALOGE("%s: Cannot find camera callback fp!", __FUNCTION__);
314                 return;
315             }
316             found = msg->findPointer(kContextKey, &context);
317             if (!found) {
318                 ALOGE("%s: Cannot find callback context!", __FUNCTION__);
319                 return;
320             }
321             (*cb)(context);
322             break;
323         }
324         default:
325             ALOGE("%s: unknown message type %d", __FUNCTION__, msg->what());
326             break;
327     }
328 }
329 
onCameraAccessPrioritiesChanged()330 binder::Status CameraManagerGlobal::CameraServiceListener::onCameraAccessPrioritiesChanged() {
331     sp<CameraManagerGlobal> cm = mCameraManager.promote();
332     if (cm != nullptr) {
333         cm->onCameraAccessPrioritiesChanged();
334     } else {
335         ALOGE("Cannot deliver camera access priority callback. Global camera manager died");
336     }
337     return binder::Status::ok();
338 }
339 
onStatusChanged(int32_t status,const String16 & cameraId)340 binder::Status CameraManagerGlobal::CameraServiceListener::onStatusChanged(
341         int32_t status, const String16& cameraId) {
342     sp<CameraManagerGlobal> cm = mCameraManager.promote();
343     if (cm != nullptr) {
344         cm->onStatusChanged(status, String8(cameraId));
345     } else {
346         ALOGE("Cannot deliver status change. Global camera manager died");
347     }
348     return binder::Status::ok();
349 }
350 
onCameraAccessPrioritiesChanged()351 void CameraManagerGlobal::onCameraAccessPrioritiesChanged() {
352     Mutex::Autolock _l(mLock);
353     for (auto cb : mCallbacks) {
354         sp<AMessage> msg = new AMessage(kWhatSendSingleAccessCallback, mHandler);
355         ACameraManager_AccessPrioritiesChangedCallback cbFp = cb.mAccessPriorityChanged;
356         if (cbFp != nullptr) {
357             msg->setPointer(kCallbackFpKey, (void *) cbFp);
358             msg->setPointer(kContextKey, cb.mContext);
359             msg->post();
360         }
361     }
362 }
363 
onStatusChanged(int32_t status,const String8 & cameraId)364 void CameraManagerGlobal::onStatusChanged(
365         int32_t status, const String8& cameraId) {
366     Mutex::Autolock _l(mLock);
367     onStatusChangedLocked(status, cameraId);
368 }
369 
onStatusChangedLocked(int32_t status,const String8 & cameraId)370 void CameraManagerGlobal::onStatusChangedLocked(
371         int32_t status, const String8& cameraId) {
372     if (!validStatus(status)) {
373         ALOGE("%s: Invalid status %d", __FUNCTION__, status);
374         return;
375     }
376 
377     bool firstStatus = (mDeviceStatusMap.count(cameraId) == 0);
378     int32_t oldStatus = firstStatus ?
379             status : // first status
380             mDeviceStatusMap[cameraId];
381 
382     if (!firstStatus &&
383             isStatusAvailable(status) == isStatusAvailable(oldStatus)) {
384         // No status update. No need to send callback
385         return;
386     }
387 
388     // Iterate through all registered callbacks
389     mDeviceStatusMap[cameraId] = status;
390     for (auto cb : mCallbacks) {
391         sp<AMessage> msg = new AMessage(kWhatSendSingleCallback, mHandler);
392         ACameraManager_AvailabilityCallback cbFp = isStatusAvailable(status) ?
393                 cb.mAvailable : cb.mUnavailable;
394         msg->setPointer(kCallbackFpKey, (void *) cbFp);
395         msg->setPointer(kContextKey, cb.mContext);
396         msg->setString(kCameraIdKey, AString(cameraId));
397         msg->post();
398     }
399     if (status == hardware::ICameraServiceListener::STATUS_NOT_PRESENT) {
400         mDeviceStatusMap.erase(cameraId);
401     }
402 }
403 
404 } // namespace acam
405 } // namespace android
406 
407 /**
408  * ACameraManger Implementation
409  */
410 camera_status_t
getCameraIdList(ACameraIdList ** cameraIdList)411 ACameraManager::getCameraIdList(ACameraIdList** cameraIdList) {
412     Mutex::Autolock _l(mLock);
413 
414     std::vector<String8> idList;
415     CameraManagerGlobal::getInstance().getCameraIdList(&idList);
416 
417     int numCameras = idList.size();
418     ACameraIdList *out = new ACameraIdList;
419     if (!out) {
420         ALOGE("Allocate memory for ACameraIdList failed!");
421         return ACAMERA_ERROR_NOT_ENOUGH_MEMORY;
422     }
423     out->numCameras = numCameras;
424     out->cameraIds = new const char*[numCameras];
425     if (!out->cameraIds) {
426         ALOGE("Allocate memory for ACameraIdList failed!");
427         deleteCameraIdList(out);
428         return ACAMERA_ERROR_NOT_ENOUGH_MEMORY;
429     }
430     for (int i = 0; i < numCameras; i++) {
431         const char* src = idList[i].string();
432         size_t dstSize = strlen(src) + 1;
433         char* dst = new char[dstSize];
434         if (!dst) {
435             ALOGE("Allocate memory for ACameraIdList failed!");
436             deleteCameraIdList(out);
437             return ACAMERA_ERROR_NOT_ENOUGH_MEMORY;
438         }
439         strlcpy(dst, src, dstSize);
440         out->cameraIds[i] = dst;
441     }
442     *cameraIdList = out;
443     return ACAMERA_OK;
444 }
445 
446 void
deleteCameraIdList(ACameraIdList * cameraIdList)447 ACameraManager::deleteCameraIdList(ACameraIdList* cameraIdList) {
448     if (cameraIdList != nullptr) {
449         if (cameraIdList->cameraIds != nullptr) {
450             for (int i = 0; i < cameraIdList->numCameras; i ++) {
451                 if (cameraIdList->cameraIds[i] != nullptr) {
452                     delete[] cameraIdList->cameraIds[i];
453                 }
454             }
455             delete[] cameraIdList->cameraIds;
456         }
457         delete cameraIdList;
458     }
459 }
460 
getCameraCharacteristics(const char * cameraIdStr,sp<ACameraMetadata> * characteristics)461 camera_status_t ACameraManager::getCameraCharacteristics(
462         const char* cameraIdStr, sp<ACameraMetadata>* characteristics) {
463     Mutex::Autolock _l(mLock);
464 
465     sp<hardware::ICameraService> cs = CameraManagerGlobal::getInstance().getCameraService();
466     if (cs == nullptr) {
467         ALOGE("%s: Cannot reach camera service!", __FUNCTION__);
468         return ACAMERA_ERROR_CAMERA_DISCONNECTED;
469     }
470     CameraMetadata rawMetadata;
471     binder::Status serviceRet = cs->getCameraCharacteristics(String16(cameraIdStr), &rawMetadata);
472     if (!serviceRet.isOk()) {
473         switch(serviceRet.serviceSpecificErrorCode()) {
474             case hardware::ICameraService::ERROR_DISCONNECTED:
475                 ALOGE("%s: Camera %s has been disconnected", __FUNCTION__, cameraIdStr);
476                 return ACAMERA_ERROR_CAMERA_DISCONNECTED;
477             case hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT:
478                 ALOGE("%s: Camera ID %s does not exist!", __FUNCTION__, cameraIdStr);
479                 return ACAMERA_ERROR_INVALID_PARAMETER;
480             default:
481                 ALOGE("Get camera characteristics from camera service failed: %s",
482                         serviceRet.toString8().string());
483                 return ACAMERA_ERROR_UNKNOWN; // should not reach here
484         }
485     }
486 
487     *characteristics = new ACameraMetadata(
488             rawMetadata.release(), ACameraMetadata::ACM_CHARACTERISTICS);
489     return ACAMERA_OK;
490 }
491 
492 camera_status_t
openCamera(const char * cameraId,ACameraDevice_StateCallbacks * callback,ACameraDevice ** outDevice)493 ACameraManager::openCamera(
494         const char* cameraId,
495         ACameraDevice_StateCallbacks* callback,
496         /*out*/ACameraDevice** outDevice) {
497     sp<ACameraMetadata> chars;
498     camera_status_t ret = getCameraCharacteristics(cameraId, &chars);
499     Mutex::Autolock _l(mLock);
500     if (ret != ACAMERA_OK) {
501         ALOGE("%s: cannot get camera characteristics for camera %s. err %d",
502                 __FUNCTION__, cameraId, ret);
503         return ACAMERA_ERROR_INVALID_PARAMETER;
504     }
505 
506     ACameraDevice* device = new ACameraDevice(cameraId, callback, chars);
507 
508     sp<hardware::ICameraService> cs = CameraManagerGlobal::getInstance().getCameraService();
509     if (cs == nullptr) {
510         ALOGE("%s: Cannot reach camera service!", __FUNCTION__);
511         delete device;
512         return ACAMERA_ERROR_CAMERA_DISCONNECTED;
513     }
514 
515     sp<hardware::camera2::ICameraDeviceCallbacks> callbacks = device->getServiceCallback();
516     sp<hardware::camera2::ICameraDeviceUser> deviceRemote;
517     // No way to get package name from native.
518     // Send a zero length package name and let camera service figure it out from UID
519     binder::Status serviceRet = cs->connectDevice(
520             callbacks, String16(cameraId), String16(""),
521             hardware::ICameraService::USE_CALLING_UID, /*out*/&deviceRemote);
522 
523     if (!serviceRet.isOk()) {
524         ALOGE("%s: connect camera device failed: %s", __FUNCTION__, serviceRet.toString8().string());
525         // Convert serviceRet to camera_status_t
526         switch(serviceRet.serviceSpecificErrorCode()) {
527             case hardware::ICameraService::ERROR_DISCONNECTED:
528                 ret = ACAMERA_ERROR_CAMERA_DISCONNECTED;
529                 break;
530             case hardware::ICameraService::ERROR_CAMERA_IN_USE:
531                 ret = ACAMERA_ERROR_CAMERA_IN_USE;
532                 break;
533             case hardware::ICameraService::ERROR_MAX_CAMERAS_IN_USE:
534                 ret = ACAMERA_ERROR_MAX_CAMERA_IN_USE;
535                 break;
536             case hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT:
537                 ret = ACAMERA_ERROR_INVALID_PARAMETER;
538                 break;
539             case hardware::ICameraService::ERROR_DEPRECATED_HAL:
540                 // Should not reach here since we filtered legacy HALs earlier
541                 ret = ACAMERA_ERROR_INVALID_PARAMETER;
542                 break;
543             case hardware::ICameraService::ERROR_DISABLED:
544                 ret = ACAMERA_ERROR_CAMERA_DISABLED;
545                 break;
546             case hardware::ICameraService::ERROR_PERMISSION_DENIED:
547                 ret = ACAMERA_ERROR_PERMISSION_DENIED;
548                 break;
549             case hardware::ICameraService::ERROR_INVALID_OPERATION:
550             default:
551                 ret = ACAMERA_ERROR_UNKNOWN;
552                 break;
553         }
554 
555         delete device;
556         return ret;
557     }
558     if (deviceRemote == nullptr) {
559         ALOGE("%s: connect camera device failed! remote device is null", __FUNCTION__);
560         delete device;
561         return ACAMERA_ERROR_CAMERA_DISCONNECTED;
562     }
563     device->setRemoteDevice(deviceRemote);
564     *outDevice = device;
565     return ACAMERA_OK;
566 }
567 
~ACameraManager()568 ACameraManager::~ACameraManager() {
569 
570 }
571