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