blob: 55bfa7ea5434b9f462919a04132426ceb193096a [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
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080089 private:
90 const wp<CameraManagerGlobal> mCameraManager;
91 };
92 sp<CameraServiceListener> mCameraServiceListener;
93
94 // Wrapper of ACameraManager_AvailabilityCallbacks so we can store it in std::set
95 struct Callback {
Chih-Hung Hsiehd19d9942016-08-29 14:21:14 -070096 explicit Callback(const ACameraManager_AvailabilityCallbacks *callback) :
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080097 mAvailable(callback->onCameraAvailable),
98 mUnavailable(callback->onCameraUnavailable),
99 mContext(callback->context) {}
100
101 bool operator == (const Callback& other) const {
102 return (mAvailable == other.mAvailable &&
103 mUnavailable == other.mUnavailable &&
104 mContext == other.mContext);
105 }
106 bool operator != (const Callback& other) const {
107 return !(*this == other);
108 }
109 bool operator < (const Callback& other) const {
110 if (*this == other) return false;
111 if (mContext != other.mContext) return mContext < other.mContext;
112 if (mAvailable != other.mAvailable) return mAvailable < other.mAvailable;
113 return mUnavailable < other.mUnavailable;
114 }
115 bool operator > (const Callback& other) const {
116 return (*this != other && !(*this < other));
117 }
118 ACameraManager_AvailabilityCallback mAvailable;
119 ACameraManager_AvailabilityCallback mUnavailable;
120 void* mContext;
121 };
122 std::set<Callback> mCallbacks;
123
124 // definition of handler and message
125 enum {
126 kWhatSendSingleCallback
127 };
128 static const char* kCameraIdKey;
129 static const char* kCallbackFpKey;
130 static const char* kContextKey;
131 class CallbackHandler : public AHandler {
132 public:
133 CallbackHandler() {}
134 void onMessageReceived(const sp<AMessage> &msg) override;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800135 };
136 sp<CallbackHandler> mHandler;
137 sp<ALooper> mCbLooper; // Looper thread where callbacks actually happen on
138
Eino-Ville Talvalaf51fca22016-12-13 11:25:55 -0800139 void onStatusChanged(int32_t status, const String8& cameraId);
140 void onStatusChangedLocked(int32_t status, const String8& cameraId);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800141 // Utils for status
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800142 static bool validStatus(int32_t status);
143 static bool isStatusAvailable(int32_t status);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800144
Yin-Chia Yeh03f55752018-03-14 15:28:02 -0700145 // The sort logic must match the logic in
146 // libcameraservice/common/CameraProviderManager.cpp::getAPI1CompatibleCameraDeviceIds
147 struct CameraIdComparator {
148 bool operator()(const String8& a, const String8& b) const {
149 uint32_t aUint = 0, bUint = 0;
150 bool aIsUint = base::ParseUint(a.c_str(), &aUint);
151 bool bIsUint = base::ParseUint(b.c_str(), &bUint);
152
153 // Uint device IDs first
154 if (aIsUint && bIsUint) {
155 return aUint < bUint;
156 } else if (aIsUint) {
157 return true;
158 } else if (bIsUint) {
159 return false;
160 }
161 // Simple string compare if both id are not uint
162 return a < b;
163 }
164 };
165
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800166 // Map camera_id -> status
Yin-Chia Yeh03f55752018-03-14 15:28:02 -0700167 std::map<String8, int32_t, CameraIdComparator> mDeviceStatusMap;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800168
169 // For the singleton instance
170 static Mutex sLock;
171 static CameraManagerGlobal* sInstance;
172 CameraManagerGlobal() {};
173 ~CameraManagerGlobal();
174};
175
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800176} // namespace acam;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800177} // namespace android;
178
179/**
180 * ACameraManager opaque struct definition
181 * Leave outside of android namespace because it's NDK struct
182 */
183struct ACameraManager {
184 ACameraManager() :
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800185 mGlobalManager(&(android::acam::CameraManagerGlobal::getInstance())) {}
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800186 ~ACameraManager();
187 camera_status_t getCameraIdList(ACameraIdList** cameraIdList);
188 static void deleteCameraIdList(ACameraIdList* cameraIdList);
189
190 camera_status_t getCameraCharacteristics(
Yin-Chia Yehdd045bf2018-08-20 12:39:19 -0700191 const char* cameraId, android::sp<ACameraMetadata>* characteristics);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800192 camera_status_t openCamera(const char* cameraId,
193 ACameraDevice_StateCallbacks* callback,
194 /*out*/ACameraDevice** device);
195
196 private:
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800197 enum {
198 kCameraIdListNotInit = -1
199 };
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800200 android::Mutex mLock;
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800201 android::sp<android::acam::CameraManagerGlobal> mGlobalManager;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800202};
203
204#endif //_ACAMERA_MANAGER_H