blob: 0dad50f62b0e612743e3a43ce9b1d70a436d1127 [file] [log] [blame]
Igor Murashkin44cfcf02013-03-01 16:22:28 -08001/*
2 * Copyright (C) 2013 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 ANDROID_SERVERS_CAMERA_CAMERA2CLIENT_BASE_H
18#define ANDROID_SERVERS_CAMERA_CAMERA2CLIENT_BASE_H
19
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070020#include "common/CameraDeviceBase.h"
Jianing Weicb0652e2014-03-12 18:29:36 -070021#include "camera/CaptureResult.h"
Austin Borger74fca042022-05-23 12:41:21 -070022#include "utils/CameraServiceProxyWrapper.h"
Igor Murashkin44cfcf02013-03-01 16:22:28 -080023
24namespace android {
25
26class IMemory;
27
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070028class CameraService;
29
Igor Murashkin44cfcf02013-03-01 16:22:28 -080030template <typename TClientBase>
31class Camera2ClientBase :
32 public TClientBase,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080033 public NotificationListener
Igor Murashkin44cfcf02013-03-01 16:22:28 -080034{
35public:
36 typedef typename TClientBase::TCamCallbacks TCamCallbacks;
37
38 /**
Ruben Brunk9efdf952015-03-18 23:11:57 -070039 * Base binder interface (see ICamera/ICameraDeviceUser for details)
Igor Murashkin44cfcf02013-03-01 16:22:28 -080040 */
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080041 virtual status_t connect(const sp<TCamCallbacks>& callbacks);
42 virtual binder::Status disconnect();
Igor Murashkin44cfcf02013-03-01 16:22:28 -080043
44 /**
45 * Interface used by CameraService
46 */
47
48 // TODO: too many params, move into a ClientArgs<T>
49 Camera2ClientBase(const sp<CameraService>& cameraService,
50 const sp<TCamCallbacks>& remoteCallback,
Austin Borger74fca042022-05-23 12:41:21 -070051 std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper,
Igor Murashkin44cfcf02013-03-01 16:22:28 -080052 const String16& clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080053 bool systemNativeClient,
Jooyung Han3f9a3b42020-01-23 12:27:18 +090054 const std::optional<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080055 const String8& cameraId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080056 int api1CameraId,
Igor Murashkin44cfcf02013-03-01 16:22:28 -080057 int cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070058 int sensorOrientation,
Igor Murashkin44cfcf02013-03-01 16:22:28 -080059 int clientPid,
60 uid_t clientUid,
Shuzhen Wangd4abdf72021-05-28 11:22:50 -070061 int servicePid,
Emilian Peev5104fe92021-10-21 14:27:09 -070062 bool overrideForPerfClass,
63 bool legacyClient = false);
Igor Murashkin44cfcf02013-03-01 16:22:28 -080064 virtual ~Camera2ClientBase();
65
Emilian Peevbd8c5032018-02-14 23:05:40 +000066 virtual status_t initialize(sp<CameraProviderManager> manager, const String8& monitorTags);
Eino-Ville Talvalac4003962016-01-13 10:07:04 -080067 virtual status_t dumpClient(int fd, const Vector<String16>& args);
Avichal Rakesh7e53cad2021-10-05 13:46:30 -070068 virtual status_t startWatchingTags(const String8 &tags, int out);
69 virtual status_t stopWatchingTags(int out);
70 virtual status_t dumpWatchedEventsToVector(std::vector<std::string> &out);
Igor Murashkin44cfcf02013-03-01 16:22:28 -080071
72 /**
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080073 * NotificationListener implementation
Igor Murashkin44cfcf02013-03-01 16:22:28 -080074 */
75
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080076 virtual void notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -070077 const CaptureResultExtras& resultExtras);
Austin Borger4a870a32022-02-25 01:48:41 +000078 // Returns errors on app ops permission failures
79 virtual status_t notifyActive(float maxPreviewFps);
Shuzhen Wangd26b1862022-03-07 12:05:05 -080080 virtual void notifyIdle(int64_t /*requestCount*/, int64_t /*resultErrorCount*/,
81 bool /*deviceError*/,
82 const std::vector<hardware::CameraStreamStats>&) {}
Jianing Weicb0652e2014-03-12 18:29:36 -070083 virtual void notifyShutter(const CaptureResultExtras& resultExtras,
84 nsecs_t timestamp);
Igor Murashkin44cfcf02013-03-01 16:22:28 -080085 virtual void notifyAutoFocus(uint8_t newState, int triggerId);
86 virtual void notifyAutoExposure(uint8_t newState, int triggerId);
87 virtual void notifyAutoWhitebalance(uint8_t newState,
88 int triggerId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070089 virtual void notifyPrepared(int streamId);
Shuzhen Wang9d066012016-09-30 11:30:20 -070090 virtual void notifyRequestQueueEmpty();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -070091 virtual void notifyRepeatingRequestError(long lastFrameNumber);
Igor Murashkin44cfcf02013-03-01 16:22:28 -080092
Shuzhen Wangd26b1862022-03-07 12:05:05 -080093 void notifyIdleWithUserTag(int64_t requestCount, int64_t resultErrorCount,
94 bool deviceError,
95 const std::vector<hardware::CameraStreamStats>& streamStats,
Shuzhen Wang9372b0b2022-05-11 18:55:19 -070096 const std::string& userTag, int videoStabilizationMode);
Shuzhen Wangd26b1862022-03-07 12:05:05 -080097
Igor Murashkin44cfcf02013-03-01 16:22:28 -080098 int getCameraId() const;
99 const sp<CameraDeviceBase>&
100 getCameraDevice();
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700101 int getCameraDeviceVersion() const;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800102 const sp<CameraService>&
103 getCameraService();
104
105 /**
106 * Interface used by independent components of CameraClient2Base.
107 */
108
109 // Simple class to ensure that access to TCamCallbacks is serialized
110 // by requiring mRemoteCallbackLock to be locked before access to
111 // mRemoteCallback is possible.
112 class SharedCameraCallbacks {
113 public:
114 class Lock {
115 public:
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -0700116 explicit Lock(SharedCameraCallbacks &client);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800117 ~Lock();
118 sp<TCamCallbacks> &mRemoteCallback;
119 private:
120 SharedCameraCallbacks &mSharedClient;
121 };
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -0700122 explicit SharedCameraCallbacks(const sp<TCamCallbacks>& client);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800123 SharedCameraCallbacks& operator=(const sp<TCamCallbacks>& client);
124 void clear();
125 private:
126 sp<TCamCallbacks> mRemoteCallback;
127 mutable Mutex mRemoteCallbackLock;
128 } mSharedCameraCallbacks;
129
Cliff Wud3a05312021-04-26 23:07:31 +0800130 status_t injectCamera(const String8& injectedCamId,
131 sp<CameraProviderManager> manager) override;
132 status_t stopInjection() override;
133
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800134protected:
135
Eino-Ville Talvalab9d2f332014-09-18 17:24:22 -0700136 // The PID provided in the constructor call
137 pid_t mInitialClientPid;
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800138 bool mOverrideForPerfClass = false;
139 bool mLegacyClient = false;
Austin Borger74fca042022-05-23 12:41:21 -0700140 std::shared_ptr<CameraServiceProxyWrapper> mCameraServiceProxyWrapper;
Eino-Ville Talvalab9d2f332014-09-18 17:24:22 -0700141
Igor Murashkine7ee7632013-06-11 18:10:18 -0700142 virtual sp<IBinder> asBinderWrapper() {
Marco Nelissen06b46062014-11-14 07:58:25 -0800143 return IInterface::asBinder(this);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700144 }
145
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800146 virtual status_t dumpDevice(int fd, const Vector<String16>& args);
147
148 /** Binder client interface-related private members */
149
150 // Mutex that must be locked by methods implementing the binder client
151 // interface. Ensures serialization between incoming client calls.
152 // All methods in this class hierarchy that append 'L' to the name assume
153 // that mBinderSerializationLock is locked when they're called
154 mutable Mutex mBinderSerializationLock;
155
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -0800156 /** CameraDeviceBase instance wrapping HAL3+ entry */
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800157
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800158 // Note: This was previously set to const to avoid mDevice being updated -
159 // b/112639939 (update of sp<> is racy) during dumpDevice (which is important to be lock free
160 // for debugging purpose). The const has been removed since CameraDeviceBase
161 // needs to be set during initializeImpl(). This must not be set / cleared
162 // anywhere else.
163 sp<CameraDeviceBase> mDevice;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800164
165 /** Utility members */
166
167 // Verify that caller is the owner of the camera
168 status_t checkPid(const char *checkLocation) const;
169
170 virtual void detachDevice();
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700171
172 bool mDeviceActive;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800173
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800174 const int mApi1CameraId; // -1 if client is API2
175
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800176private:
177 template<typename TProviderPtr>
Emilian Peevbd8c5032018-02-14 23:05:40 +0000178 status_t initializeImpl(TProviderPtr providerPtr, const String8& monitorTags);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800179};
180
181}; // namespace android
182
183#endif