blob: c3407f03957853e9b4cd661d64bc6657085ce562 [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
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -080057 /**
58 * Return camera IDs that support camera2
59 */
60 void getCameraIdList(std::vector<String8> *cameraIds);
61
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080062 private:
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080063 sp<hardware::ICameraService> mCameraService;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080064 const int kCameraServicePollDelay = 500000; // 0.5s
65 const char* kCameraServiceName = "media.camera";
66 Mutex mLock;
67
68 class DeathNotifier : public IBinder::DeathRecipient {
69 public:
Chih-Hung Hsiehd19d9942016-08-29 14:21:14 -070070 explicit DeathNotifier(CameraManagerGlobal* cm) : mCameraManager(cm) {}
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080071 protected:
72 // IBinder::DeathRecipient implementation
73 virtual void binderDied(const wp<IBinder>& who);
74 private:
75 const wp<CameraManagerGlobal> mCameraManager;
76 };
77 sp<DeathNotifier> mDeathNotifier;
78
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080079 class CameraServiceListener final : public hardware::BnCameraServiceListener {
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080080 public:
Chih-Hung Hsiehd19d9942016-08-29 14:21:14 -070081 explicit CameraServiceListener(CameraManagerGlobal* cm) : mCameraManager(cm) {}
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -080082 virtual binder::Status onStatusChanged(int32_t status, const String16& cameraId);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080083
84 // Torch API not implemented yet
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080085 virtual binder::Status onTorchStatusChanged(int32_t, const String16&) {
86 return binder::Status::ok();
87 }
88
Emilian Peev53722fa2019-02-22 17:47:20 -080089 // Access priority API not implemented yet
90 virtual binder::Status onCameraAccessPrioritiesChanged() {
91 return binder::Status::ok();
92 }
93
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080094 private:
95 const wp<CameraManagerGlobal> mCameraManager;
96 };
97 sp<CameraServiceListener> mCameraServiceListener;
98
99 // Wrapper of ACameraManager_AvailabilityCallbacks so we can store it in std::set
100 struct Callback {
Chih-Hung Hsiehd19d9942016-08-29 14:21:14 -0700101 explicit Callback(const ACameraManager_AvailabilityCallbacks *callback) :
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800102 mAvailable(callback->onCameraAvailable),
103 mUnavailable(callback->onCameraUnavailable),
104 mContext(callback->context) {}
105
106 bool operator == (const Callback& other) const {
107 return (mAvailable == other.mAvailable &&
108 mUnavailable == other.mUnavailable &&
109 mContext == other.mContext);
110 }
111 bool operator != (const Callback& other) const {
112 return !(*this == other);
113 }
114 bool operator < (const Callback& other) const {
115 if (*this == other) return false;
116 if (mContext != other.mContext) return mContext < other.mContext;
117 if (mAvailable != other.mAvailable) return mAvailable < other.mAvailable;
118 return mUnavailable < other.mUnavailable;
119 }
120 bool operator > (const Callback& other) const {
121 return (*this != other && !(*this < other));
122 }
123 ACameraManager_AvailabilityCallback mAvailable;
124 ACameraManager_AvailabilityCallback mUnavailable;
125 void* mContext;
126 };
127 std::set<Callback> mCallbacks;
128
129 // definition of handler and message
130 enum {
131 kWhatSendSingleCallback
132 };
133 static const char* kCameraIdKey;
134 static const char* kCallbackFpKey;
135 static const char* kContextKey;
136 class CallbackHandler : public AHandler {
137 public:
138 CallbackHandler() {}
139 void onMessageReceived(const sp<AMessage> &msg) override;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800140 };
141 sp<CallbackHandler> mHandler;
142 sp<ALooper> mCbLooper; // Looper thread where callbacks actually happen on
143
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800144 void onStatusChanged(int32_t status, const String8& cameraId);
145 void onStatusChangedLocked(int32_t status, const String8& cameraId);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800146 // Utils for status
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800147 static bool validStatus(int32_t status);
148 static bool isStatusAvailable(int32_t status);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800149
Yin-Chia Yeh03f55752018-03-14 15:28:02 -0700150 // The sort logic must match the logic in
151 // libcameraservice/common/CameraProviderManager.cpp::getAPI1CompatibleCameraDeviceIds
152 struct CameraIdComparator {
153 bool operator()(const String8& a, const String8& b) const {
154 uint32_t aUint = 0, bUint = 0;
155 bool aIsUint = base::ParseUint(a.c_str(), &aUint);
156 bool bIsUint = base::ParseUint(b.c_str(), &bUint);
157
158 // Uint device IDs first
159 if (aIsUint && bIsUint) {
160 return aUint < bUint;
161 } else if (aIsUint) {
162 return true;
163 } else if (bIsUint) {
164 return false;
165 }
166 // Simple string compare if both id are not uint
167 return a < b;
168 }
169 };
170
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800171 // Map camera_id -> status
Yin-Chia Yeh03f55752018-03-14 15:28:02 -0700172 std::map<String8, int32_t, CameraIdComparator> mDeviceStatusMap;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800173
174 // For the singleton instance
175 static Mutex sLock;
176 static CameraManagerGlobal* sInstance;
177 CameraManagerGlobal() {};
178 ~CameraManagerGlobal();
179};
180
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800181} // namespace acam;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800182} // namespace android;
183
184/**
185 * ACameraManager opaque struct definition
186 * Leave outside of android namespace because it's NDK struct
187 */
188struct ACameraManager {
189 ACameraManager() :
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800190 mGlobalManager(&(android::acam::CameraManagerGlobal::getInstance())) {}
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800191 ~ACameraManager();
192 camera_status_t getCameraIdList(ACameraIdList** cameraIdList);
193 static void deleteCameraIdList(ACameraIdList* cameraIdList);
194
195 camera_status_t getCameraCharacteristics(
Yin-Chia Yehdd045bf2018-08-20 12:39:19 -0700196 const char* cameraId, android::sp<ACameraMetadata>* characteristics);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800197 camera_status_t openCamera(const char* cameraId,
198 ACameraDevice_StateCallbacks* callback,
199 /*out*/ACameraDevice** device);
200
201 private:
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800202 enum {
203 kCameraIdListNotInit = -1
204 };
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800205 android::Mutex mLock;
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800206 android::sp<android::acam::CameraManagerGlobal> mGlobalManager;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800207};
208
209#endif //_ACAMERA_MANAGER_H