blob: cba6c73bcb5ce4bb86dcdc401c436ab578651e55 [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;
Jayant Chowdhary80f128b2019-10-30 16:13:31 -070069 const int kCameraServicePollDelay = 500000; // 0.5s
70 const char* kCameraServiceName = "media.camera";
71 Mutex mLock;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080072
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();
Shuzhen Wang1bb30b22020-03-06 09:02:23 -080095 virtual binder::Status onCameraOpened(const String16&, const String16&) {
96 return binder::Status::ok();
97 }
98 virtual binder::Status onCameraClosed(const String16&) {
99 return binder::Status::ok();
100 }
Emilian Peev53722fa2019-02-22 17:47:20 -0800101
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800102 private:
103 const wp<CameraManagerGlobal> mCameraManager;
104 };
105 sp<CameraServiceListener> mCameraServiceListener;
106
107 // Wrapper of ACameraManager_AvailabilityCallbacks so we can store it in std::set
108 struct Callback {
Chih-Hung Hsiehd19d9942016-08-29 14:21:14 -0700109 explicit Callback(const ACameraManager_AvailabilityCallbacks *callback) :
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800110 mAvailable(callback->onCameraAvailable),
111 mUnavailable(callback->onCameraUnavailable),
Emilian Peevc6f2ab32019-03-04 11:18:59 -0800112 mAccessPriorityChanged(nullptr),
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800113 mContext(callback->context) {}
114
Emilian Peevc6f2ab32019-03-04 11:18:59 -0800115 explicit Callback(const ACameraManager_ExtendedAvailabilityCallbacks *callback) :
116 mAvailable(callback->availabilityCallbacks.onCameraAvailable),
117 mUnavailable(callback->availabilityCallbacks.onCameraUnavailable),
118 mAccessPriorityChanged(callback->onCameraAccessPrioritiesChanged),
119 mContext(callback->availabilityCallbacks.context) {}
120
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800121 bool operator == (const Callback& other) const {
122 return (mAvailable == other.mAvailable &&
123 mUnavailable == other.mUnavailable &&
Emilian Peevc6f2ab32019-03-04 11:18:59 -0800124 mAccessPriorityChanged == other.mAccessPriorityChanged &&
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800125 mContext == other.mContext);
126 }
127 bool operator != (const Callback& other) const {
128 return !(*this == other);
129 }
130 bool operator < (const Callback& other) const {
131 if (*this == other) return false;
132 if (mContext != other.mContext) return mContext < other.mContext;
Emilian Peevc6f2ab32019-03-04 11:18:59 -0800133 if (mAccessPriorityChanged != other.mAccessPriorityChanged) {
134 return mAccessPriorityChanged < other.mAccessPriorityChanged;
135 }
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800136 if (mAvailable != other.mAvailable) return mAvailable < other.mAvailable;
137 return mUnavailable < other.mUnavailable;
138 }
139 bool operator > (const Callback& other) const {
140 return (*this != other && !(*this < other));
141 }
142 ACameraManager_AvailabilityCallback mAvailable;
143 ACameraManager_AvailabilityCallback mUnavailable;
Emilian Peevc6f2ab32019-03-04 11:18:59 -0800144 ACameraManager_AccessPrioritiesChangedCallback mAccessPriorityChanged;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800145 void* mContext;
146 };
147 std::set<Callback> mCallbacks;
148
149 // definition of handler and message
150 enum {
Emilian Peevc6f2ab32019-03-04 11:18:59 -0800151 kWhatSendSingleCallback,
152 kWhatSendSingleAccessCallback,
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800153 };
154 static const char* kCameraIdKey;
155 static const char* kCallbackFpKey;
156 static const char* kContextKey;
157 class CallbackHandler : public AHandler {
158 public:
159 CallbackHandler() {}
160 void onMessageReceived(const sp<AMessage> &msg) override;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800161 };
162 sp<CallbackHandler> mHandler;
163 sp<ALooper> mCbLooper; // Looper thread where callbacks actually happen on
164
Jayant Chowdhary80f128b2019-10-30 16:13:31 -0700165 sp<hardware::ICameraService> getCameraServiceLocked();
Emilian Peevc6f2ab32019-03-04 11:18:59 -0800166 void onCameraAccessPrioritiesChanged();
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800167 void onStatusChanged(int32_t status, const String8& cameraId);
168 void onStatusChangedLocked(int32_t status, const String8& cameraId);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800169 // Utils for status
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800170 static bool validStatus(int32_t status);
171 static bool isStatusAvailable(int32_t status);
Jayant Chowdhary80f128b2019-10-30 16:13:31 -0700172 bool supportsCamera2ApiLocked(const String8 &cameraId);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800173
Yin-Chia Yeh03f55752018-03-14 15:28:02 -0700174 // The sort logic must match the logic in
175 // libcameraservice/common/CameraProviderManager.cpp::getAPI1CompatibleCameraDeviceIds
176 struct CameraIdComparator {
177 bool operator()(const String8& a, const String8& b) const {
178 uint32_t aUint = 0, bUint = 0;
179 bool aIsUint = base::ParseUint(a.c_str(), &aUint);
180 bool bIsUint = base::ParseUint(b.c_str(), &bUint);
181
182 // Uint device IDs first
183 if (aIsUint && bIsUint) {
184 return aUint < bUint;
185 } else if (aIsUint) {
186 return true;
187 } else if (bIsUint) {
188 return false;
189 }
190 // Simple string compare if both id are not uint
191 return a < b;
192 }
193 };
194
Jayant Chowdhary80f128b2019-10-30 16:13:31 -0700195 struct StatusAndHAL3Support {
196 int32_t status = hardware::ICameraServiceListener::STATUS_NOT_PRESENT;
197 bool supportsHAL3 = false;
198 StatusAndHAL3Support(int32_t st, bool HAL3support):
199 status(st), supportsHAL3(HAL3support) { };
200 StatusAndHAL3Support() = default;
201 };
202
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800203 // Map camera_id -> status
Jayant Chowdhary80f128b2019-10-30 16:13:31 -0700204 std::map<String8, StatusAndHAL3Support, CameraIdComparator> mDeviceStatusMap;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800205
206 // For the singleton instance
207 static Mutex sLock;
208 static CameraManagerGlobal* sInstance;
209 CameraManagerGlobal() {};
210 ~CameraManagerGlobal();
211};
212
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800213} // namespace acam;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800214} // namespace android;
215
216/**
217 * ACameraManager opaque struct definition
218 * Leave outside of android namespace because it's NDK struct
219 */
220struct ACameraManager {
221 ACameraManager() :
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800222 mGlobalManager(&(android::acam::CameraManagerGlobal::getInstance())) {}
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800223 ~ACameraManager();
224 camera_status_t getCameraIdList(ACameraIdList** cameraIdList);
225 static void deleteCameraIdList(ACameraIdList* cameraIdList);
226
227 camera_status_t getCameraCharacteristics(
Yin-Chia Yehdd045bf2018-08-20 12:39:19 -0700228 const char* cameraId, android::sp<ACameraMetadata>* characteristics);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800229 camera_status_t openCamera(const char* cameraId,
230 ACameraDevice_StateCallbacks* callback,
231 /*out*/ACameraDevice** device);
232
233 private:
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800234 enum {
235 kCameraIdListNotInit = -1
236 };
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800237 android::Mutex mLock;
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800238 android::sp<android::acam::CameraManagerGlobal> mGlobalManager;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800239};
240
241#endif //_ACAMERA_MANAGER_H