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 #ifndef _ACAMERA_MANAGER_H 18 #define _ACAMERA_MANAGER_H 19 20 #include <camera/NdkCameraManager.h> 21 22 #include <android-base/parseint.h> 23 #include <android/hardware/ICameraService.h> 24 #include <android/hardware/BnCameraServiceListener.h> 25 #include <camera/CameraMetadata.h> 26 #include <binder/IServiceManager.h> 27 #include <utils/StrongPointer.h> 28 #include <utils/Mutex.h> 29 30 #include <media/stagefright/foundation/ALooper.h> 31 #include <media/stagefright/foundation/AHandler.h> 32 #include <media/stagefright/foundation/AMessage.h> 33 34 #include <set> 35 #include <map> 36 37 namespace android { 38 namespace acam { 39 40 /** 41 * Per-process singleton instance of CameraManger. Shared by all ACameraManager 42 * instances. Created when first ACameraManager is created and destroyed when 43 * all ACameraManager instances are deleted. 44 * 45 * TODO: maybe CameraManagerGlobal is better suited in libcameraclient? 46 */ 47 class CameraManagerGlobal final : public RefBase { 48 public: 49 static CameraManagerGlobal& getInstance(); 50 sp<hardware::ICameraService> getCameraService(); 51 52 void registerAvailabilityCallback( 53 const ACameraManager_AvailabilityCallbacks *callback); 54 void unregisterAvailabilityCallback( 55 const ACameraManager_AvailabilityCallbacks *callback); 56 57 void registerExtendedAvailabilityCallback( 58 const ACameraManager_ExtendedAvailabilityCallbacks* callback); 59 void unregisterExtendedAvailabilityCallback( 60 const ACameraManager_ExtendedAvailabilityCallbacks* callback); 61 62 /** 63 * Return camera IDs that support camera2 64 */ 65 void getCameraIdList(std::vector<String8> *cameraIds); 66 67 private: 68 sp<hardware::ICameraService> mCameraService; 69 const int kCameraServicePollDelay = 500000; // 0.5s 70 const char* kCameraServiceName = "media.camera"; 71 Mutex mLock; 72 73 template<class T> 74 void registerAvailCallback(const T *callback); 75 76 class DeathNotifier : public IBinder::DeathRecipient { 77 public: DeathNotifier(CameraManagerGlobal * cm)78 explicit DeathNotifier(CameraManagerGlobal* cm) : mCameraManager(cm) {} 79 protected: 80 // IBinder::DeathRecipient implementation 81 virtual void binderDied(const wp<IBinder>& who); 82 private: 83 const wp<CameraManagerGlobal> mCameraManager; 84 }; 85 sp<DeathNotifier> mDeathNotifier; 86 87 class CameraServiceListener final : public hardware::BnCameraServiceListener { 88 public: CameraServiceListener(CameraManagerGlobal * cm)89 explicit CameraServiceListener(CameraManagerGlobal* cm) : mCameraManager(cm) {} 90 virtual binder::Status onStatusChanged(int32_t status, const String16& cameraId); 91 virtual binder::Status onPhysicalCameraStatusChanged(int32_t status, 92 const String16& cameraId, const String16& physicalCameraId); 93 94 // Torch API not implemented yet onTorchStatusChanged(int32_t,const String16 &)95 virtual binder::Status onTorchStatusChanged(int32_t, const String16&) { 96 return binder::Status::ok(); 97 } 98 99 virtual binder::Status onCameraAccessPrioritiesChanged(); onCameraOpened(const String16 &,const String16 &)100 virtual binder::Status onCameraOpened(const String16&, const String16&) { 101 return binder::Status::ok(); 102 } onCameraClosed(const String16 &)103 virtual binder::Status onCameraClosed(const String16&) { 104 return binder::Status::ok(); 105 } 106 107 private: 108 const wp<CameraManagerGlobal> mCameraManager; 109 }; 110 sp<CameraServiceListener> mCameraServiceListener; 111 112 // Wrapper of ACameraManager_AvailabilityCallbacks so we can store it in std::set 113 struct Callback { CallbackCallback114 explicit Callback(const ACameraManager_AvailabilityCallbacks *callback) : 115 mAvailable(callback->onCameraAvailable), 116 mUnavailable(callback->onCameraUnavailable), 117 mAccessPriorityChanged(nullptr), 118 mPhysicalCamAvailable(nullptr), 119 mPhysicalCamUnavailable(nullptr), 120 mContext(callback->context) {} 121 CallbackCallback122 explicit Callback(const ACameraManager_ExtendedAvailabilityCallbacks *callback) : 123 mAvailable(callback->availabilityCallbacks.onCameraAvailable), 124 mUnavailable(callback->availabilityCallbacks.onCameraUnavailable), 125 mAccessPriorityChanged(callback->onCameraAccessPrioritiesChanged), 126 mPhysicalCamAvailable(callback->onPhysicalCameraAvailable), 127 mPhysicalCamUnavailable(callback->onPhysicalCameraUnavailable), 128 mContext(callback->availabilityCallbacks.context) {} 129 130 bool operator == (const Callback& other) const { 131 return (mAvailable == other.mAvailable && 132 mUnavailable == other.mUnavailable && 133 mAccessPriorityChanged == other.mAccessPriorityChanged && 134 mPhysicalCamAvailable == other.mPhysicalCamAvailable && 135 mPhysicalCamUnavailable == other.mPhysicalCamUnavailable && 136 mContext == other.mContext); 137 } 138 bool operator != (const Callback& other) const { 139 return !(*this == other); 140 } 141 bool operator < (const Callback& other) const { 142 if (*this == other) return false; 143 if (mContext != other.mContext) return mContext < other.mContext; 144 if (mPhysicalCamAvailable != other.mPhysicalCamAvailable) { 145 return mPhysicalCamAvailable < other.mPhysicalCamAvailable; 146 } 147 if (mPhysicalCamUnavailable != other.mPhysicalCamUnavailable) { 148 return mPhysicalCamUnavailable < other.mPhysicalCamUnavailable; 149 } 150 if (mAccessPriorityChanged != other.mAccessPriorityChanged) { 151 return mAccessPriorityChanged < other.mAccessPriorityChanged; 152 } 153 if (mAvailable != other.mAvailable) return mAvailable < other.mAvailable; 154 return mUnavailable < other.mUnavailable; 155 } 156 bool operator > (const Callback& other) const { 157 return (*this != other && !(*this < other)); 158 } 159 ACameraManager_AvailabilityCallback mAvailable; 160 ACameraManager_AvailabilityCallback mUnavailable; 161 ACameraManager_AccessPrioritiesChangedCallback mAccessPriorityChanged; 162 ACameraManager_PhysicalCameraAvailabilityCallback mPhysicalCamAvailable; 163 ACameraManager_PhysicalCameraAvailabilityCallback mPhysicalCamUnavailable; 164 void* mContext; 165 }; 166 167 android::Condition mCallbacksCond; 168 size_t mPendingCallbackCnt = 0; 169 void onCallbackCalled(); 170 void drainPendingCallbacksLocked(); 171 172 std::set<Callback> mCallbacks; 173 174 // definition of handler and message 175 enum { 176 kWhatSendSingleCallback, 177 kWhatSendSingleAccessCallback, 178 kWhatSendSinglePhysicalCameraCallback, 179 }; 180 static const char* kCameraIdKey; 181 static const char* kPhysicalCameraIdKey; 182 static const char* kCallbackFpKey; 183 static const char* kContextKey; 184 static const nsecs_t kCallbackDrainTimeout; 185 class CallbackHandler : public AHandler { 186 public: CallbackHandler(wp<CameraManagerGlobal> parent)187 CallbackHandler(wp<CameraManagerGlobal> parent) : mParent(parent) {} 188 void onMessageReceived(const sp<AMessage> &msg) override; 189 190 private: 191 wp<CameraManagerGlobal> mParent; 192 void notifyParent(); 193 void onMessageReceivedInternal(const sp<AMessage> &msg); 194 }; 195 sp<CallbackHandler> mHandler; 196 sp<ALooper> mCbLooper; // Looper thread where callbacks actually happen on 197 198 sp<hardware::ICameraService> getCameraServiceLocked(); 199 void onCameraAccessPrioritiesChanged(); 200 void onStatusChanged(int32_t status, const String8& cameraId); 201 void onStatusChangedLocked(int32_t status, const String8& cameraId); 202 void onStatusChanged(int32_t status, const String8& cameraId, const String8& physicalCameraId); 203 void onStatusChangedLocked(int32_t status, const String8& cameraId, 204 const String8& physicalCameraId); 205 // Utils for status 206 static bool validStatus(int32_t status); 207 static bool isStatusAvailable(int32_t status); 208 bool supportsCamera2ApiLocked(const String8 &cameraId); 209 210 // The sort logic must match the logic in 211 // libcameraservice/common/CameraProviderManager.cpp::getAPI1CompatibleCameraDeviceIds 212 struct CameraIdComparator { operatorCameraIdComparator213 bool operator()(const String8& a, const String8& b) const { 214 uint32_t aUint = 0, bUint = 0; 215 bool aIsUint = base::ParseUint(a.c_str(), &aUint); 216 bool bIsUint = base::ParseUint(b.c_str(), &bUint); 217 218 // Uint device IDs first 219 if (aIsUint && bIsUint) { 220 return aUint < bUint; 221 } else if (aIsUint) { 222 return true; 223 } else if (bIsUint) { 224 return false; 225 } 226 // Simple string compare if both id are not uint 227 return a < b; 228 } 229 }; 230 231 struct StatusAndHAL3Support { 232 private: 233 int32_t status = hardware::ICameraServiceListener::STATUS_NOT_PRESENT; 234 mutable std::mutex mLock; 235 std::set<String8> unavailablePhysicalIds; 236 public: 237 const bool supportsHAL3 = false; StatusAndHAL3SupportStatusAndHAL3Support238 StatusAndHAL3Support(int32_t st, bool HAL3support): 239 status(st), supportsHAL3(HAL3support) { }; 240 StatusAndHAL3Support() = default; 241 242 bool addUnavailablePhysicalId(const String8& physicalCameraId); 243 bool removeUnavailablePhysicalId(const String8& physicalCameraId); 244 int32_t getStatus(); 245 void updateStatus(int32_t newStatus); 246 std::set<String8> getUnavailablePhysicalIds(); 247 }; 248 249 // Map camera_id -> status 250 std::map<String8, StatusAndHAL3Support, CameraIdComparator> mDeviceStatusMap; 251 252 // For the singleton instance 253 static Mutex sLock; 254 static CameraManagerGlobal* sInstance; CameraManagerGlobal()255 CameraManagerGlobal() {}; 256 ~CameraManagerGlobal(); 257 }; 258 259 } // namespace acam; 260 } // namespace android; 261 262 /** 263 * ACameraManager opaque struct definition 264 * Leave outside of android namespace because it's NDK struct 265 */ 266 struct ACameraManager { ACameraManagerACameraManager267 ACameraManager() : 268 mGlobalManager(&(android::acam::CameraManagerGlobal::getInstance())) {} 269 ~ACameraManager(); 270 camera_status_t getCameraIdList(ACameraIdList** cameraIdList); 271 static void deleteCameraIdList(ACameraIdList* cameraIdList); 272 273 camera_status_t getCameraCharacteristics( 274 const char* cameraId, android::sp<ACameraMetadata>* characteristics); 275 camera_status_t openCamera(const char* cameraId, 276 ACameraDevice_StateCallbacks* callback, 277 /*out*/ACameraDevice** device); 278 279 private: 280 enum { 281 kCameraIdListNotInit = -1 282 }; 283 android::Mutex mLock; 284 android::sp<android::acam::CameraManagerGlobal> mGlobalManager; 285 }; 286 287 #endif //_ACAMERA_MANAGER_H 288