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