1 /*
2  * Copyright (C) 2018 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/frameworks/cameraservice/service/2.0/ICameraService.h>
24 
25 #include <CameraMetadata.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 
36 namespace android {
37 namespace acam {
38 
39 using ICameraService = frameworks::cameraservice::service::V2_0::ICameraService;
40 using CameraDeviceStatus = frameworks::cameraservice::service::V2_0::CameraDeviceStatus;
41 using ICameraServiceListener = frameworks::cameraservice::service::V2_0::ICameraServiceListener;
42 using CameraStatusAndId = frameworks::cameraservice::service::V2_0::CameraStatusAndId;
43 using Status = frameworks::cameraservice::common::V2_0::Status;
44 using VendorTagSection = frameworks::cameraservice::common::V2_0::VendorTagSection;
45 using VendorTag = frameworks::cameraservice::common::V2_0::VendorTag;
46 using IBase = android::hidl::base::V1_0::IBase;
47 using android::hardware::hidl_string;
48 using hardware::Void;
49 
50 /**
51  * Per-process singleton instance of CameraManger. Shared by all ACameraManager
52  * instances. Created when first ACameraManager is created and destroyed when
53  * all ACameraManager instances are deleted.
54  *
55  * TODO: maybe CameraManagerGlobal is better suited in libcameraclient?
56  */
57 class CameraManagerGlobal final : public RefBase {
58   public:
59     static CameraManagerGlobal& getInstance();
60     sp<ICameraService> getCameraService();
61 
62     void registerAvailabilityCallback(
63             const ACameraManager_AvailabilityCallbacks *callback);
64     void unregisterAvailabilityCallback(
65             const ACameraManager_AvailabilityCallbacks *callback);
66 
registerExtendedAvailabilityCallback(const ACameraManager_ExtendedAvailabilityCallbacks *)67     void registerExtendedAvailabilityCallback(
68             const ACameraManager_ExtendedAvailabilityCallbacks* /*callback*/) {}
unregisterExtendedAvailabilityCallback(const ACameraManager_ExtendedAvailabilityCallbacks *)69     void unregisterExtendedAvailabilityCallback(
70             const ACameraManager_ExtendedAvailabilityCallbacks* /*callback*/) {}
71 
72     /**
73      * Return camera IDs that support camera2
74      */
75     void getCameraIdList(std::vector<hidl_string> *cameraIds);
76 
77   private:
78     sp<ICameraService> mCameraService;
79     const int          kCameraServicePollDelay = 500000; // 0.5s
80     Mutex              mLock;
81     class DeathNotifier : public android::hardware::hidl_death_recipient {
82       public:
DeathNotifier(CameraManagerGlobal * cm)83         explicit DeathNotifier(CameraManagerGlobal* cm) : mCameraManager(cm) {}
84       protected:
85         // IBinder::DeathRecipient implementation
86         virtual void serviceDied(uint64_t cookie, const wp<IBase> &who);
87       private:
88         const wp<CameraManagerGlobal> mCameraManager;
89     };
90     sp<DeathNotifier> mDeathNotifier;
91 
92     class CameraServiceListener final : public ICameraServiceListener {
93       public:
CameraServiceListener(CameraManagerGlobal * cm)94         explicit CameraServiceListener(CameraManagerGlobal* cm) : mCameraManager(cm) {}
95         android::hardware::Return<void> onStatusChanged(
96             const CameraStatusAndId &statusAndId) override;
97 
98       private:
99         const wp<CameraManagerGlobal> mCameraManager;
100     };
101     sp<CameraServiceListener> mCameraServiceListener;
102 
103     // Wrapper of ACameraManager_AvailabilityCallbacks so we can store it in std::set
104     struct Callback {
CallbackCallback105         explicit Callback(const ACameraManager_AvailabilityCallbacks *callback) :
106             mAvailable(callback->onCameraAvailable),
107             mUnavailable(callback->onCameraUnavailable),
108             mContext(callback->context) {}
109 
110         bool operator == (const Callback& other) const {
111             return (mAvailable == other.mAvailable &&
112                     mUnavailable == other.mUnavailable &&
113                     mContext == other.mContext);
114         }
115         bool operator != (const Callback& other) const {
116             return !(*this == other);
117         }
118         bool operator < (const Callback& other) const {
119             if (*this == other) return false;
120             if (mContext != other.mContext) return mContext < other.mContext;
121             if (mAvailable != other.mAvailable) return mAvailable < other.mAvailable;
122             return mUnavailable < other.mUnavailable;
123         }
124         bool operator > (const Callback& other) const {
125             return (*this != other && !(*this < other));
126         }
127         ACameraManager_AvailabilityCallback mAvailable;
128         ACameraManager_AvailabilityCallback mUnavailable;
129         void*                               mContext;
130     };
131     std::set<Callback> mCallbacks;
132 
133     // definition of handler and message
134     enum {
135         kWhatSendSingleCallback
136     };
137     static const char* kCameraIdKey;
138     static const char* kCallbackFpKey;
139     static const char* kContextKey;
140     class CallbackHandler : public AHandler {
141       public:
CallbackHandler()142         CallbackHandler() {}
143         void onMessageReceived(const sp<AMessage> &msg) override;
144     };
145     sp<CallbackHandler> mHandler;
146     sp<ALooper>         mCbLooper; // Looper thread where callbacks actually happen on
147 
148     void onStatusChanged(const CameraStatusAndId &statusAndId);
149     void onStatusChangedLocked(const CameraStatusAndId &statusAndId);
150     bool setupVendorTags();
151 
152     // Utils for status
153     static bool validStatus(CameraDeviceStatus status);
154     static bool isStatusAvailable(CameraDeviceStatus status);
155 
156     // The sort logic must match the logic in
157     // libcameraservice/common/CameraProviderManager.cpp::getAPI1CompatibleCameraDeviceIds
158     struct CameraIdComparator {
operatorCameraIdComparator159         bool operator()(const hidl_string& a, const hidl_string& b) const {
160             uint32_t aUint = 0, bUint = 0;
161             bool aIsUint = base::ParseUint(a.c_str(), &aUint);
162             bool bIsUint = base::ParseUint(b.c_str(), &bUint);
163 
164             // Uint device IDs first
165             if (aIsUint && bIsUint) {
166                 return aUint < bUint;
167             } else if (aIsUint) {
168                 return true;
169             } else if (bIsUint) {
170                 return false;
171             }
172             // Simple string compare if both id are not uint
173             return a < b;
174         }
175     };
176 
177     // Map camera_id -> status
178     std::map<hidl_string, CameraDeviceStatus, CameraIdComparator> mDeviceStatusMap;
179 
180     // For the singleton instance
181     static Mutex sLock;
182     static CameraManagerGlobal* sInstance;
CameraManagerGlobal()183     CameraManagerGlobal() {};
184     ~CameraManagerGlobal();
185 };
186 
187 } // namespace acam;
188 } // namespace android;
189 
190 /**
191  * ACameraManager opaque struct definition
192  * Leave outside of android namespace because it's NDK struct
193  */
194 struct ACameraManager {
ACameraManagerACameraManager195     ACameraManager() :
196             mGlobalManager(&(android::acam::CameraManagerGlobal::getInstance())) {}
197     ~ACameraManager();
198     camera_status_t getCameraIdList(ACameraIdList** cameraIdList);
199     static void     deleteCameraIdList(ACameraIdList* cameraIdList);
200 
201     camera_status_t getCameraCharacteristics(
202             const char* cameraId, android::sp<ACameraMetadata>* characteristics);
203 
204     camera_status_t openCamera(const char* cameraId,
205                                ACameraDevice_StateCallbacks* callback,
206                                /*out*/ACameraDevice** device);
207     camera_status_t getTagFromName(const char *cameraId, const char *name, uint32_t *tag);
208 
209   private:
210     enum {
211         kCameraIdListNotInit = -1
212     };
213     android::Mutex         mLock;
214     android::sp<android::acam::CameraManagerGlobal> mGlobalManager;
215 };
216 
217 #endif //_ACAMERA_MANAGER_H
218