• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <camera/NdkCameraManager.h>
21 
22 #include <android-base/parseint.h>
23 #include <android/hardware/ICameraService.h>
24 #include <android/hardware/BnCameraServiceListener.h>
25 #include <camera/CameraMetadata.h>
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 
37 namespace android {
38 namespace acam {
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  *
45  * TODO: maybe CameraManagerGlobal is better suited in libcameraclient?
46  */
47 class CameraManagerGlobal final : public RefBase {
48   public:
49     static CameraManagerGlobal& getInstance();
50     sp<hardware::ICameraService> getCameraService();
51 
52     void registerAvailabilityCallback(
53             const ACameraManager_AvailabilityCallbacks *callback);
54     void unregisterAvailabilityCallback(
55             const ACameraManager_AvailabilityCallbacks *callback);
56 
57     void registerExtendedAvailabilityCallback(
58             const ACameraManager_ExtendedAvailabilityCallbacks* callback);
59     void unregisterExtendedAvailabilityCallback(
60             const ACameraManager_ExtendedAvailabilityCallbacks* callback);
61 
62     /**
63      * Return camera IDs that support camera2
64      */
65     void getCameraIdList(std::vector<String8> *cameraIds);
66 
67   private:
68     sp<hardware::ICameraService> mCameraService;
69     const int          kCameraServicePollDelay = 500000; // 0.5s
70     const char*        kCameraServiceName      = "media.camera";
71     Mutex              mLock;
72 
73     class DeathNotifier : public IBinder::DeathRecipient {
74       public:
DeathNotifier(CameraManagerGlobal * cm)75         explicit DeathNotifier(CameraManagerGlobal* cm) : mCameraManager(cm) {}
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 
84     class CameraServiceListener final : public hardware::BnCameraServiceListener {
85       public:
CameraServiceListener(CameraManagerGlobal * cm)86         explicit CameraServiceListener(CameraManagerGlobal* cm) : mCameraManager(cm) {}
87         virtual binder::Status onStatusChanged(int32_t status, const String16& cameraId);
88 
89         // Torch API not implemented yet
onTorchStatusChanged(int32_t,const String16 &)90         virtual binder::Status onTorchStatusChanged(int32_t, const String16&) {
91             return binder::Status::ok();
92         }
93 
94         virtual binder::Status onCameraAccessPrioritiesChanged();
95 
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 {
CallbackCallback103         explicit Callback(const ACameraManager_AvailabilityCallbacks *callback) :
104             mAvailable(callback->onCameraAvailable),
105             mUnavailable(callback->onCameraUnavailable),
106             mAccessPriorityChanged(nullptr),
107             mContext(callback->context) {}
108 
CallbackCallback109         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 
115         bool operator == (const Callback& other) const {
116             return (mAvailable == other.mAvailable &&
117                     mUnavailable == other.mUnavailable &&
118                     mAccessPriorityChanged == other.mAccessPriorityChanged &&
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;
127             if (mAccessPriorityChanged != other.mAccessPriorityChanged) {
128                 return mAccessPriorityChanged < other.mAccessPriorityChanged;
129             }
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;
138         ACameraManager_AccessPrioritiesChangedCallback mAccessPriorityChanged;
139         void*                               mContext;
140     };
141     std::set<Callback> mCallbacks;
142 
143     // definition of handler and message
144     enum {
145         kWhatSendSingleCallback,
146         kWhatSendSingleAccessCallback,
147     };
148     static const char* kCameraIdKey;
149     static const char* kCallbackFpKey;
150     static const char* kContextKey;
151     class CallbackHandler : public AHandler {
152       public:
CallbackHandler()153         CallbackHandler() {}
154         void onMessageReceived(const sp<AMessage> &msg) override;
155     };
156     sp<CallbackHandler> mHandler;
157     sp<ALooper>         mCbLooper; // Looper thread where callbacks actually happen on
158 
159     void onCameraAccessPrioritiesChanged();
160     void onStatusChanged(int32_t status, const String8& cameraId);
161     void onStatusChangedLocked(int32_t status, const String8& cameraId);
162     // Utils for status
163     static bool validStatus(int32_t status);
164     static bool isStatusAvailable(int32_t status);
165 
166     // The sort logic must match the logic in
167     // libcameraservice/common/CameraProviderManager.cpp::getAPI1CompatibleCameraDeviceIds
168     struct CameraIdComparator {
operatorCameraIdComparator169         bool operator()(const String8& a, const String8& b) const {
170             uint32_t aUint = 0, bUint = 0;
171             bool aIsUint = base::ParseUint(a.c_str(), &aUint);
172             bool bIsUint = base::ParseUint(b.c_str(), &bUint);
173 
174             // Uint device IDs first
175             if (aIsUint && bIsUint) {
176                 return aUint < bUint;
177             } else if (aIsUint) {
178                 return true;
179             } else if (bIsUint) {
180                 return false;
181             }
182             // Simple string compare if both id are not uint
183             return a < b;
184         }
185     };
186 
187     // Map camera_id -> status
188     std::map<String8, int32_t, CameraIdComparator> mDeviceStatusMap;
189 
190     // For the singleton instance
191     static Mutex sLock;
192     static CameraManagerGlobal* sInstance;
CameraManagerGlobal()193     CameraManagerGlobal() {};
194     ~CameraManagerGlobal();
195 };
196 
197 } // namespace acam;
198 } // namespace android;
199 
200 /**
201  * ACameraManager opaque struct definition
202  * Leave outside of android namespace because it's NDK struct
203  */
204 struct ACameraManager {
ACameraManagerACameraManager205     ACameraManager() :
206             mGlobalManager(&(android::acam::CameraManagerGlobal::getInstance())) {}
207     ~ACameraManager();
208     camera_status_t getCameraIdList(ACameraIdList** cameraIdList);
209     static void     deleteCameraIdList(ACameraIdList* cameraIdList);
210 
211     camera_status_t getCameraCharacteristics(
212             const char* cameraId, android::sp<ACameraMetadata>* characteristics);
213     camera_status_t openCamera(const char* cameraId,
214                                ACameraDevice_StateCallbacks* callback,
215                                /*out*/ACameraDevice** device);
216 
217   private:
218     enum {
219         kCameraIdListNotInit = -1
220     };
221     android::Mutex         mLock;
222     android::sp<android::acam::CameraManagerGlobal> mGlobalManager;
223 };
224 
225 #endif //_ACAMERA_MANAGER_H
226