| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 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 |  | 
| Colin Cross | 7e8d4ba | 2017-05-04 16:17:42 -0700 | [diff] [blame] | 20 | #include <camera/NdkCameraManager.h> | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 21 |  | 
| Yin-Chia Yeh | 03f5575 | 2018-03-14 15:28:02 -0700 | [diff] [blame] | 22 | #include <android-base/parseint.h> | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 23 | #include <android/hardware/ICameraService.h> | 
 | 24 | #include <android/hardware/BnCameraServiceListener.h> | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 25 | #include <camera/CameraMetadata.h> | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 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 |  | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 37 | namespace android { | 
| Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 38 | namespace acam { | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 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 |  * | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 45 |  * TODO: maybe CameraManagerGlobal is better suited in libcameraclient? | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 46 |  */ | 
 | 47 | class CameraManagerGlobal final : public RefBase { | 
 | 48 |   public: | 
 | 49 |     static CameraManagerGlobal& getInstance(); | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 50 |     sp<hardware::ICameraService> getCameraService(); | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 51 |  | 
 | 52 |     void registerAvailabilityCallback( | 
 | 53 |             const ACameraManager_AvailabilityCallbacks *callback); | 
 | 54 |     void unregisterAvailabilityCallback( | 
 | 55 |             const ACameraManager_AvailabilityCallbacks *callback); | 
 | 56 |  | 
| Emilian Peev | c6f2ab3 | 2019-03-04 11:18:59 -0800 | [diff] [blame] | 57 |     void registerExtendedAvailabilityCallback( | 
 | 58 |             const ACameraManager_ExtendedAvailabilityCallbacks* callback); | 
 | 59 |     void unregisterExtendedAvailabilityCallback( | 
 | 60 |             const ACameraManager_ExtendedAvailabilityCallbacks* callback); | 
 | 61 |  | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 62 |     /** | 
 | 63 |      * Return camera IDs that support camera2 | 
 | 64 |      */ | 
 | 65 |     void getCameraIdList(std::vector<String8> *cameraIds); | 
 | 66 |  | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 67 |   private: | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 68 |     sp<hardware::ICameraService> mCameraService; | 
| Jayant Chowdhary | 80f128b | 2019-10-30 16:13:31 -0700 | [diff] [blame] | 69 |     const int                    kCameraServicePollDelay = 500000; // 0.5s | 
 | 70 |     const char*                  kCameraServiceName      = "media.camera"; | 
 | 71 |     Mutex                        mLock; | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 72 |  | 
 | 73 |     class DeathNotifier : public IBinder::DeathRecipient { | 
 | 74 |       public: | 
| Chih-Hung Hsieh | d19d994 | 2016-08-29 14:21:14 -0700 | [diff] [blame] | 75 |         explicit DeathNotifier(CameraManagerGlobal* cm) : mCameraManager(cm) {} | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 76 |       protected: | 
 | 77 |         // IBinder::DeathRecipient implementation | 
 | 78 |         virtual void binderDied(const wp<IBinder>& who); | 
 | 79 |       private: | 
 | 80 |         const wp<CameraManagerGlobal> mCameraManager; | 
 | 81 |     }; | 
 | 82 |     sp<DeathNotifier> mDeathNotifier; | 
 | 83 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 84 |     class CameraServiceListener final : public hardware::BnCameraServiceListener { | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 85 |       public: | 
| Chih-Hung Hsieh | d19d994 | 2016-08-29 14:21:14 -0700 | [diff] [blame] | 86 |         explicit CameraServiceListener(CameraManagerGlobal* cm) : mCameraManager(cm) {} | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 87 |         virtual binder::Status onStatusChanged(int32_t status, const String16& cameraId); | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 88 |  | 
 | 89 |         // Torch API not implemented yet | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 90 |         virtual binder::Status onTorchStatusChanged(int32_t, const String16&) { | 
 | 91 |             return binder::Status::ok(); | 
 | 92 |         } | 
 | 93 |  | 
| Emilian Peev | c6f2ab3 | 2019-03-04 11:18:59 -0800 | [diff] [blame] | 94 |         virtual binder::Status onCameraAccessPrioritiesChanged(); | 
| Emilian Peev | 53722fa | 2019-02-22 17:47:20 -0800 | [diff] [blame] | 95 |  | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 96 |       private: | 
 | 97 |         const wp<CameraManagerGlobal> mCameraManager; | 
 | 98 |     }; | 
 | 99 |     sp<CameraServiceListener> mCameraServiceListener; | 
 | 100 |  | 
 | 101 |     // Wrapper of ACameraManager_AvailabilityCallbacks so we can store it in std::set | 
 | 102 |     struct Callback { | 
| Chih-Hung Hsieh | d19d994 | 2016-08-29 14:21:14 -0700 | [diff] [blame] | 103 |         explicit Callback(const ACameraManager_AvailabilityCallbacks *callback) : | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 104 |             mAvailable(callback->onCameraAvailable), | 
 | 105 |             mUnavailable(callback->onCameraUnavailable), | 
| Emilian Peev | c6f2ab3 | 2019-03-04 11:18:59 -0800 | [diff] [blame] | 106 |             mAccessPriorityChanged(nullptr), | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 107 |             mContext(callback->context) {} | 
 | 108 |  | 
| Emilian Peev | c6f2ab3 | 2019-03-04 11:18:59 -0800 | [diff] [blame] | 109 |         explicit Callback(const ACameraManager_ExtendedAvailabilityCallbacks *callback) : | 
 | 110 |             mAvailable(callback->availabilityCallbacks.onCameraAvailable), | 
 | 111 |             mUnavailable(callback->availabilityCallbacks.onCameraUnavailable), | 
 | 112 |             mAccessPriorityChanged(callback->onCameraAccessPrioritiesChanged), | 
 | 113 |             mContext(callback->availabilityCallbacks.context) {} | 
 | 114 |  | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 115 |         bool operator == (const Callback& other) const { | 
 | 116 |             return (mAvailable == other.mAvailable && | 
 | 117 |                     mUnavailable == other.mUnavailable && | 
| Emilian Peev | c6f2ab3 | 2019-03-04 11:18:59 -0800 | [diff] [blame] | 118 |                     mAccessPriorityChanged == other.mAccessPriorityChanged && | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 119 |                     mContext == other.mContext); | 
 | 120 |         } | 
 | 121 |         bool operator != (const Callback& other) const { | 
 | 122 |             return !(*this == other); | 
 | 123 |         } | 
 | 124 |         bool operator < (const Callback& other) const { | 
 | 125 |             if (*this == other) return false; | 
 | 126 |             if (mContext != other.mContext) return mContext < other.mContext; | 
| Emilian Peev | c6f2ab3 | 2019-03-04 11:18:59 -0800 | [diff] [blame] | 127 |             if (mAccessPriorityChanged != other.mAccessPriorityChanged) { | 
 | 128 |                 return mAccessPriorityChanged < other.mAccessPriorityChanged; | 
 | 129 |             } | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 130 |             if (mAvailable != other.mAvailable) return mAvailable < other.mAvailable; | 
 | 131 |             return mUnavailable < other.mUnavailable; | 
 | 132 |         } | 
 | 133 |         bool operator > (const Callback& other) const { | 
 | 134 |             return (*this != other && !(*this < other)); | 
 | 135 |         } | 
 | 136 |         ACameraManager_AvailabilityCallback mAvailable; | 
 | 137 |         ACameraManager_AvailabilityCallback mUnavailable; | 
| Emilian Peev | c6f2ab3 | 2019-03-04 11:18:59 -0800 | [diff] [blame] | 138 |         ACameraManager_AccessPrioritiesChangedCallback mAccessPriorityChanged; | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 139 |         void*                               mContext; | 
 | 140 |     }; | 
 | 141 |     std::set<Callback> mCallbacks; | 
 | 142 |  | 
 | 143 |     // definition of handler and message | 
 | 144 |     enum { | 
| Emilian Peev | c6f2ab3 | 2019-03-04 11:18:59 -0800 | [diff] [blame] | 145 |         kWhatSendSingleCallback, | 
 | 146 |         kWhatSendSingleAccessCallback, | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 147 |     }; | 
 | 148 |     static const char* kCameraIdKey; | 
 | 149 |     static const char* kCallbackFpKey; | 
 | 150 |     static const char* kContextKey; | 
 | 151 |     class CallbackHandler : public AHandler { | 
 | 152 |       public: | 
 | 153 |         CallbackHandler() {} | 
 | 154 |         void onMessageReceived(const sp<AMessage> &msg) override; | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 155 |     }; | 
 | 156 |     sp<CallbackHandler> mHandler; | 
 | 157 |     sp<ALooper>         mCbLooper; // Looper thread where callbacks actually happen on | 
 | 158 |  | 
| Jayant Chowdhary | 80f128b | 2019-10-30 16:13:31 -0700 | [diff] [blame] | 159 |     sp<hardware::ICameraService> getCameraServiceLocked(); | 
| Emilian Peev | c6f2ab3 | 2019-03-04 11:18:59 -0800 | [diff] [blame] | 160 |     void onCameraAccessPrioritiesChanged(); | 
| Eino-Ville Talvala | f51fca2 | 2016-12-13 11:25:55 -0800 | [diff] [blame] | 161 |     void onStatusChanged(int32_t status, const String8& cameraId); | 
 | 162 |     void onStatusChangedLocked(int32_t status, const String8& cameraId); | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 163 |     // Utils for status | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 164 |     static bool validStatus(int32_t status); | 
 | 165 |     static bool isStatusAvailable(int32_t status); | 
| Jayant Chowdhary | 80f128b | 2019-10-30 16:13:31 -0700 | [diff] [blame] | 166 |     bool supportsCamera2ApiLocked(const String8 &cameraId); | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 167 |  | 
| Yin-Chia Yeh | 03f5575 | 2018-03-14 15:28:02 -0700 | [diff] [blame] | 168 |     // The sort logic must match the logic in | 
 | 169 |     // libcameraservice/common/CameraProviderManager.cpp::getAPI1CompatibleCameraDeviceIds | 
 | 170 |     struct CameraIdComparator { | 
 | 171 |         bool operator()(const String8& a, const String8& b) const { | 
 | 172 |             uint32_t aUint = 0, bUint = 0; | 
 | 173 |             bool aIsUint = base::ParseUint(a.c_str(), &aUint); | 
 | 174 |             bool bIsUint = base::ParseUint(b.c_str(), &bUint); | 
 | 175 |  | 
 | 176 |             // Uint device IDs first | 
 | 177 |             if (aIsUint && bIsUint) { | 
 | 178 |                 return aUint < bUint; | 
 | 179 |             } else if (aIsUint) { | 
 | 180 |                 return true; | 
 | 181 |             } else if (bIsUint) { | 
 | 182 |                 return false; | 
 | 183 |             } | 
 | 184 |             // Simple string compare if both id are not uint | 
 | 185 |             return a < b; | 
 | 186 |         } | 
 | 187 |     }; | 
 | 188 |  | 
| Jayant Chowdhary | 80f128b | 2019-10-30 16:13:31 -0700 | [diff] [blame] | 189 |     struct StatusAndHAL3Support { | 
 | 190 |         int32_t status = hardware::ICameraServiceListener::STATUS_NOT_PRESENT; | 
 | 191 |         bool supportsHAL3 = false; | 
 | 192 |         StatusAndHAL3Support(int32_t st, bool HAL3support): | 
 | 193 |                 status(st), supportsHAL3(HAL3support) { }; | 
 | 194 |         StatusAndHAL3Support() = default; | 
 | 195 |     }; | 
 | 196 |  | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 197 |     // Map camera_id -> status | 
| Jayant Chowdhary | 80f128b | 2019-10-30 16:13:31 -0700 | [diff] [blame] | 198 |     std::map<String8, StatusAndHAL3Support, CameraIdComparator> mDeviceStatusMap; | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 199 |  | 
 | 200 |     // For the singleton instance | 
 | 201 |     static Mutex sLock; | 
 | 202 |     static CameraManagerGlobal* sInstance; | 
 | 203 |     CameraManagerGlobal() {}; | 
 | 204 |     ~CameraManagerGlobal(); | 
 | 205 | }; | 
 | 206 |  | 
| Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 207 | } // namespace acam; | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 208 | } // namespace android; | 
 | 209 |  | 
 | 210 | /** | 
 | 211 |  * ACameraManager opaque struct definition | 
 | 212 |  * Leave outside of android namespace because it's NDK struct | 
 | 213 |  */ | 
 | 214 | struct ACameraManager { | 
 | 215 |     ACameraManager() : | 
| Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 216 |             mGlobalManager(&(android::acam::CameraManagerGlobal::getInstance())) {} | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 217 |     ~ACameraManager(); | 
 | 218 |     camera_status_t getCameraIdList(ACameraIdList** cameraIdList); | 
 | 219 |     static void     deleteCameraIdList(ACameraIdList* cameraIdList); | 
 | 220 |  | 
 | 221 |     camera_status_t getCameraCharacteristics( | 
| Yin-Chia Yeh | dd045bf | 2018-08-20 12:39:19 -0700 | [diff] [blame] | 222 |             const char* cameraId, android::sp<ACameraMetadata>* characteristics); | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 223 |     camera_status_t openCamera(const char* cameraId, | 
 | 224 |                                ACameraDevice_StateCallbacks* callback, | 
 | 225 |                                /*out*/ACameraDevice** device); | 
 | 226 |  | 
 | 227 |   private: | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 228 |     enum { | 
 | 229 |         kCameraIdListNotInit = -1 | 
 | 230 |     }; | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 231 |     android::Mutex         mLock; | 
| Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 232 |     android::sp<android::acam::CameraManagerGlobal> mGlobalManager; | 
| Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 233 | }; | 
 | 234 |  | 
 | 235 | #endif //_ACAMERA_MANAGER_H |