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 | |
| 20 | #include "NdkCameraManager.h" |
| 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 | * |
| 43 | * TODO: maybe CameraManagerGlobal is better sutied in libcameraclient? |
| 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 | |
| 55 | private: |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 56 | sp<hardware::ICameraService> mCameraService; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 57 | const int kCameraServicePollDelay = 500000; // 0.5s |
| 58 | const char* kCameraServiceName = "media.camera"; |
| 59 | Mutex mLock; |
| 60 | |
| 61 | class DeathNotifier : public IBinder::DeathRecipient { |
| 62 | public: |
| 63 | DeathNotifier(CameraManagerGlobal* cm) : mCameraManager(cm) {} |
| 64 | protected: |
| 65 | // IBinder::DeathRecipient implementation |
| 66 | virtual void binderDied(const wp<IBinder>& who); |
| 67 | private: |
| 68 | const wp<CameraManagerGlobal> mCameraManager; |
| 69 | }; |
| 70 | sp<DeathNotifier> mDeathNotifier; |
| 71 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 72 | class CameraServiceListener final : public hardware::BnCameraServiceListener { |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 73 | public: |
| 74 | CameraServiceListener(CameraManagerGlobal* cm) : mCameraManager(cm) {} |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 75 | virtual binder::Status onStatusChanged(int32_t status, int32_t cameraId); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 76 | |
| 77 | // Torch API not implemented yet |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 78 | virtual binder::Status onTorchStatusChanged(int32_t, const String16&) { |
| 79 | return binder::Status::ok(); |
| 80 | } |
| 81 | |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 82 | private: |
| 83 | const wp<CameraManagerGlobal> mCameraManager; |
| 84 | }; |
| 85 | sp<CameraServiceListener> mCameraServiceListener; |
| 86 | |
| 87 | // Wrapper of ACameraManager_AvailabilityCallbacks so we can store it in std::set |
| 88 | struct Callback { |
| 89 | Callback(const ACameraManager_AvailabilityCallbacks *callback) : |
| 90 | mAvailable(callback->onCameraAvailable), |
| 91 | mUnavailable(callback->onCameraUnavailable), |
| 92 | mContext(callback->context) {} |
| 93 | |
| 94 | bool operator == (const Callback& other) const { |
| 95 | return (mAvailable == other.mAvailable && |
| 96 | mUnavailable == other.mUnavailable && |
| 97 | mContext == other.mContext); |
| 98 | } |
| 99 | bool operator != (const Callback& other) const { |
| 100 | return !(*this == other); |
| 101 | } |
| 102 | bool operator < (const Callback& other) const { |
| 103 | if (*this == other) return false; |
| 104 | if (mContext != other.mContext) return mContext < other.mContext; |
| 105 | if (mAvailable != other.mAvailable) return mAvailable < other.mAvailable; |
| 106 | return mUnavailable < other.mUnavailable; |
| 107 | } |
| 108 | bool operator > (const Callback& other) const { |
| 109 | return (*this != other && !(*this < other)); |
| 110 | } |
| 111 | ACameraManager_AvailabilityCallback mAvailable; |
| 112 | ACameraManager_AvailabilityCallback mUnavailable; |
| 113 | void* mContext; |
| 114 | }; |
| 115 | std::set<Callback> mCallbacks; |
| 116 | |
| 117 | // definition of handler and message |
| 118 | enum { |
| 119 | kWhatSendSingleCallback |
| 120 | }; |
| 121 | static const char* kCameraIdKey; |
| 122 | static const char* kCallbackFpKey; |
| 123 | static const char* kContextKey; |
| 124 | class CallbackHandler : public AHandler { |
| 125 | public: |
| 126 | CallbackHandler() {} |
| 127 | void onMessageReceived(const sp<AMessage> &msg) override; |
| 128 | private: |
| 129 | inline void sendSingleCallback( |
| 130 | int32_t cameraId, void* context, |
| 131 | ACameraManager_AvailabilityCallback cb) const; |
| 132 | }; |
| 133 | sp<CallbackHandler> mHandler; |
| 134 | sp<ALooper> mCbLooper; // Looper thread where callbacks actually happen on |
| 135 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 136 | void onStatusChanged(int32_t status, int32_t cameraId); |
| 137 | void onStatusChangedLocked(int32_t status, int32_t cameraId); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 138 | // Utils for status |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 139 | static bool validStatus(int32_t status); |
| 140 | static bool isStatusAvailable(int32_t status); |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 141 | |
| 142 | // Map camera_id -> status |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 143 | std::map<int32_t, int32_t> mDeviceStatusMap; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 144 | |
| 145 | // For the singleton instance |
| 146 | static Mutex sLock; |
| 147 | static CameraManagerGlobal* sInstance; |
| 148 | CameraManagerGlobal() {}; |
| 149 | ~CameraManagerGlobal(); |
| 150 | }; |
| 151 | |
| 152 | } // namespace android; |
| 153 | |
| 154 | /** |
| 155 | * ACameraManager opaque struct definition |
| 156 | * Leave outside of android namespace because it's NDK struct |
| 157 | */ |
| 158 | struct ACameraManager { |
| 159 | ACameraManager() : |
| 160 | mCachedCameraIdList({kCameraIdListNotInit, nullptr}), |
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: |
| 173 | camera_status_t getOrCreateCameraIdListLocked(ACameraIdList** cameraIdList); |
| 174 | |
| 175 | enum { |
| 176 | kCameraIdListNotInit = -1 |
| 177 | }; |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 178 | android::Mutex mLock; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 179 | std::set<int> mCameraIds; // Init by getOrCreateCameraIdListLocked |
| 180 | ACameraIdList mCachedCameraIdList; // Init by getOrCreateCameraIdListLocked |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 181 | android::sp<android::CameraManagerGlobal> mGlobalManager; |
Yin-Chia Yeh | 0dea57f | 2015-12-09 16:46:07 -0800 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | #endif //_ACAMERA_MANAGER_H |