blob: 8c1da364185f87e33a39ae2db9e24da190cd5288 [file] [log] [blame]
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001/*
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 Cross7e8d4ba2017-05-04 16:17:42 -070020#include <camera/NdkCameraManager.h>
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080021
Yin-Chia Yeh03f55752018-03-14 15:28:02 -070022#include <android-base/parseint.h>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080023#include <android/hardware/ICameraService.h>
24#include <android/hardware/BnCameraServiceListener.h>
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080025#include <camera/CameraMetadata.h>
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080026#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 Yeh0dea57f2015-12-09 16:46:07 -080037namespace android {
Jayant Chowdhary6df26072018-11-06 23:55:12 -080038namespace acam {
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080039
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 *
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -080045 * TODO: maybe CameraManagerGlobal is better suited in libcameraclient?
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080046 */
47class CameraManagerGlobal final : public RefBase {
48 public:
49 static CameraManagerGlobal& getInstance();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080050 sp<hardware::ICameraService> getCameraService();
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080051
52 void registerAvailabilityCallback(
53 const ACameraManager_AvailabilityCallbacks *callback);
54 void unregisterAvailabilityCallback(
55 const ACameraManager_AvailabilityCallbacks *callback);
56
Emilian Peevc6f2ab32019-03-04 11:18:59 -080057 void registerExtendedAvailabilityCallback(
58 const ACameraManager_ExtendedAvailabilityCallbacks* callback);
59 void unregisterExtendedAvailabilityCallback(
60 const ACameraManager_ExtendedAvailabilityCallbacks* callback);
61
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -080062 /**
63 * Return camera IDs that support camera2
64 */
65 void getCameraIdList(std::vector<String8> *cameraIds);
66
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080067 private:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080068 sp<hardware::ICameraService> mCameraService;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080069 const int kCameraServicePollDelay = 500000; // 0.5s
70 const char* kCameraServiceName = "media.camera";
71 Mutex mLock;
72
73 class DeathNotifier : public IBinder::DeathRecipient {
74 public:
Chih-Hung Hsiehd19d9942016-08-29 14:21:14 -070075 explicit DeathNotifier(CameraManagerGlobal* cm) : mCameraManager(cm) {}
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080076 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
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080084 class CameraServiceListener final : public hardware::BnCameraServiceListener {
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080085 public:
Chih-Hung Hsiehd19d9942016-08-29 14:21:14 -070086 explicit CameraServiceListener(CameraManagerGlobal* cm) : mCameraManager(cm) {}
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -080087 virtual binder::Status onStatusChanged(int32_t status, const String16& cameraId);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080088
89 // Torch API not implemented yet
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080090 virtual binder::Status onTorchStatusChanged(int32_t, const String16&) {
91 return binder::Status::ok();
92 }
93
Emilian Peevc6f2ab32019-03-04 11:18:59 -080094 virtual binder::Status onCameraAccessPrioritiesChanged();
Emilian Peev53722fa2019-02-22 17:47:20 -080095
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080096 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 {
Chih-Hung Hsiehd19d9942016-08-29 14:21:14 -0700103 explicit Callback(const ACameraManager_AvailabilityCallbacks *callback) :
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800104 mAvailable(callback->onCameraAvailable),
105 mUnavailable(callback->onCameraUnavailable),
Emilian Peevc6f2ab32019-03-04 11:18:59 -0800106 mAccessPriorityChanged(nullptr),
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800107 mContext(callback->context) {}
108
Emilian Peevc6f2ab32019-03-04 11:18:59 -0800109 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
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800115 bool operator == (const Callback& other) const {
116 return (mAvailable == other.mAvailable &&
117 mUnavailable == other.mUnavailable &&
Emilian Peevc6f2ab32019-03-04 11:18:59 -0800118 mAccessPriorityChanged == other.mAccessPriorityChanged &&
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800119 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;
Emilian Peevc6f2ab32019-03-04 11:18:59 -0800127 if (mAccessPriorityChanged != other.mAccessPriorityChanged) {
128 return mAccessPriorityChanged < other.mAccessPriorityChanged;
129 }
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800130 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;
Emilian Peevc6f2ab32019-03-04 11:18:59 -0800138 ACameraManager_AccessPrioritiesChangedCallback mAccessPriorityChanged;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800139 void* mContext;
140 };
141 std::set<Callback> mCallbacks;
142
143 // definition of handler and message
144 enum {
Emilian Peevc6f2ab32019-03-04 11:18:59 -0800145 kWhatSendSingleCallback,
146 kWhatSendSingleAccessCallback,
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800147 };
148 static const char* kCameraIdKey;
149 static const char* kCallbackFpKey;
150 static const char* kContextKey;
151 class CallbackHandler : public AHandler {
152 public:
153 CallbackHandler() {}
154 void onMessageReceived(const sp<AMessage> &msg) override;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800155 };
156 sp<CallbackHandler> mHandler;
157 sp<ALooper> mCbLooper; // Looper thread where callbacks actually happen on
158
Emilian Peevc6f2ab32019-03-04 11:18:59 -0800159 void onCameraAccessPrioritiesChanged();
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800160 void onStatusChanged(int32_t status, const String8& cameraId);
161 void onStatusChangedLocked(int32_t status, const String8& cameraId);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800162 // Utils for status
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800163 static bool validStatus(int32_t status);
164 static bool isStatusAvailable(int32_t status);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800165
Yin-Chia Yeh03f55752018-03-14 15:28:02 -0700166 // The sort logic must match the logic in
167 // libcameraservice/common/CameraProviderManager.cpp::getAPI1CompatibleCameraDeviceIds
168 struct CameraIdComparator {
169 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
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800187 // Map camera_id -> status
Yin-Chia Yeh03f55752018-03-14 15:28:02 -0700188 std::map<String8, int32_t, CameraIdComparator> mDeviceStatusMap;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800189
190 // For the singleton instance
191 static Mutex sLock;
192 static CameraManagerGlobal* sInstance;
193 CameraManagerGlobal() {};
194 ~CameraManagerGlobal();
195};
196
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800197} // namespace acam;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800198} // namespace android;
199
200/**
201 * ACameraManager opaque struct definition
202 * Leave outside of android namespace because it's NDK struct
203 */
204struct ACameraManager {
205 ACameraManager() :
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800206 mGlobalManager(&(android::acam::CameraManagerGlobal::getInstance())) {}
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800207 ~ACameraManager();
208 camera_status_t getCameraIdList(ACameraIdList** cameraIdList);
209 static void deleteCameraIdList(ACameraIdList* cameraIdList);
210
211 camera_status_t getCameraCharacteristics(
Yin-Chia Yehdd045bf2018-08-20 12:39:19 -0700212 const char* cameraId, android::sp<ACameraMetadata>* characteristics);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800213 camera_status_t openCamera(const char* cameraId,
214 ACameraDevice_StateCallbacks* callback,
215 /*out*/ACameraDevice** device);
216
217 private:
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800218 enum {
219 kCameraIdListNotInit = -1
220 };
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800221 android::Mutex mLock;
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800222 android::sp<android::acam::CameraManagerGlobal> mGlobalManager;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800223};
224
225#endif //_ACAMERA_MANAGER_H