blob: b8a6d8b95c125a228589a739d94e67674bc79326 [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"
malikakash22af94c2023-12-04 18:13:14 +000021#include "camera/CameraMetadata.h"
Jianing Weicb0652e2014-03-12 18:29:36 -070022#include "camera/CaptureResult.h"
Austin Borger74fca042022-05-23 12:41:21 -070023#include "utils/CameraServiceProxyWrapper.h"
Austin Borger17e3ebe2024-02-14 15:02:11 -080024#include "utils/AttributionAndPermissionUtils.h"
Jayant Chowdhary620763f2022-05-27 05:37:14 +000025#include "CameraServiceWatchdog.h"
Igor Murashkin44cfcf02013-03-01 16:22:28 -080026
27namespace android {
28
29class IMemory;
30
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070031class CameraService;
32
Igor Murashkin44cfcf02013-03-01 16:22:28 -080033template <typename TClientBase>
34class Camera2ClientBase :
35 public TClientBase,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080036 public NotificationListener
Igor Murashkin44cfcf02013-03-01 16:22:28 -080037{
38public:
39 typedef typename TClientBase::TCamCallbacks TCamCallbacks;
40
41 /**
Ruben Brunk9efdf952015-03-18 23:11:57 -070042 * Base binder interface (see ICamera/ICameraDeviceUser for details)
Igor Murashkin44cfcf02013-03-01 16:22:28 -080043 */
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080044 virtual status_t connect(const sp<TCamCallbacks>& callbacks);
45 virtual binder::Status disconnect();
Igor Murashkin44cfcf02013-03-01 16:22:28 -080046
47 /**
48 * Interface used by CameraService
49 */
50
51 // TODO: too many params, move into a ClientArgs<T>
52 Camera2ClientBase(const sp<CameraService>& cameraService,
53 const sp<TCamCallbacks>& remoteCallback,
Austin Borger74fca042022-05-23 12:41:21 -070054 std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper,
Austin Borger17e3ebe2024-02-14 15:02:11 -080055 std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils,
Austin Borger1c1bee02023-06-01 16:51:35 -070056 const std::string& clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080057 bool systemNativeClient,
Austin Borger1c1bee02023-06-01 16:51:35 -070058 const std::optional<std::string>& clientFeatureId,
59 const std::string& cameraId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080060 int api1CameraId,
Igor Murashkin44cfcf02013-03-01 16:22:28 -080061 int cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070062 int sensorOrientation,
Igor Murashkin44cfcf02013-03-01 16:22:28 -080063 int clientPid,
64 uid_t clientUid,
Shuzhen Wangd4abdf72021-05-28 11:22:50 -070065 int servicePid,
Emilian Peev5104fe92021-10-21 14:27:09 -070066 bool overrideForPerfClass,
Austin Borger18b30a72022-10-27 12:20:29 -070067 bool overrideToPortrait,
Emilian Peev5104fe92021-10-21 14:27:09 -070068 bool legacyClient = false);
Igor Murashkin44cfcf02013-03-01 16:22:28 -080069 virtual ~Camera2ClientBase();
70
Austin Borger1c1bee02023-06-01 16:51:35 -070071 virtual status_t initialize(sp<CameraProviderManager> manager,
72 const std::string& monitorTags) override;
73 virtual status_t dumpClient(int fd, const Vector<String16>& args) override;
74 virtual status_t startWatchingTags(const std::string &tags, int out) override;
75 virtual status_t stopWatchingTags(int out) override;
76 virtual status_t dumpWatchedEventsToVector(std::vector<std::string> &out) override;
Igor Murashkin44cfcf02013-03-01 16:22:28 -080077
78 /**
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080079 * NotificationListener implementation
Igor Murashkin44cfcf02013-03-01 16:22:28 -080080 */
81
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080082 virtual void notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -070083 const CaptureResultExtras& resultExtras);
Austin Borgerc8099762023-01-12 17:08:46 -080084 virtual void notifyPhysicalCameraChange(const std::string &physicalId) override;
Austin Borger4a870a32022-02-25 01:48:41 +000085 // Returns errors on app ops permission failures
86 virtual status_t notifyActive(float maxPreviewFps);
Shuzhen Wangd26b1862022-03-07 12:05:05 -080087 virtual void notifyIdle(int64_t /*requestCount*/, int64_t /*resultErrorCount*/,
88 bool /*deviceError*/,
89 const std::vector<hardware::CameraStreamStats>&) {}
Jianing Weicb0652e2014-03-12 18:29:36 -070090 virtual void notifyShutter(const CaptureResultExtras& resultExtras,
91 nsecs_t timestamp);
Igor Murashkin44cfcf02013-03-01 16:22:28 -080092 virtual void notifyAutoFocus(uint8_t newState, int triggerId);
93 virtual void notifyAutoExposure(uint8_t newState, int triggerId);
94 virtual void notifyAutoWhitebalance(uint8_t newState,
95 int triggerId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -070096 virtual void notifyPrepared(int streamId);
Shuzhen Wang9d066012016-09-30 11:30:20 -070097 virtual void notifyRequestQueueEmpty();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -070098 virtual void notifyRepeatingRequestError(long lastFrameNumber);
Igor Murashkin44cfcf02013-03-01 16:22:28 -080099
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800100 void notifyIdleWithUserTag(int64_t requestCount, int64_t resultErrorCount,
101 bool deviceError,
102 const std::vector<hardware::CameraStreamStats>& streamStats,
Eino-Ville Talvala6f1a9c12023-09-14 17:26:28 -0700103 const std::string& userTag, int videoStabilizationMode,
Shuzhen Wang6e08d202023-10-24 20:27:14 +0000104 bool usedUltraWide, bool usedZoomOverride);
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800105
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800106 int getCameraId() const;
107 const sp<CameraDeviceBase>&
108 getCameraDevice();
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700109 int getCameraDeviceVersion() const;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800110 const sp<CameraService>&
111 getCameraService();
112
113 /**
114 * Interface used by independent components of CameraClient2Base.
115 */
116
117 // Simple class to ensure that access to TCamCallbacks is serialized
118 // by requiring mRemoteCallbackLock to be locked before access to
119 // mRemoteCallback is possible.
120 class SharedCameraCallbacks {
121 public:
122 class Lock {
123 public:
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -0700124 explicit Lock(SharedCameraCallbacks &client);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800125 ~Lock();
126 sp<TCamCallbacks> &mRemoteCallback;
127 private:
128 SharedCameraCallbacks &mSharedClient;
129 };
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -0700130 explicit SharedCameraCallbacks(const sp<TCamCallbacks>& client);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800131 SharedCameraCallbacks& operator=(const sp<TCamCallbacks>& client);
132 void clear();
133 private:
134 sp<TCamCallbacks> mRemoteCallback;
135 mutable Mutex mRemoteCallbackLock;
136 } mSharedCameraCallbacks;
137
Austin Borger1c1bee02023-06-01 16:51:35 -0700138 status_t injectCamera(const std::string& injectedCamId,
Cliff Wud3a05312021-04-26 23:07:31 +0800139 sp<CameraProviderManager> manager) override;
140 status_t stopInjection() override;
141
malikakash22af94c2023-12-04 18:13:14 +0000142 status_t injectSessionParams(const CameraMetadata& sessionParams) override;
143
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800144protected:
145
Eino-Ville Talvalab9d2f332014-09-18 17:24:22 -0700146 // The PID provided in the constructor call
147 pid_t mInitialClientPid;
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800148 bool mOverrideForPerfClass = false;
149 bool mLegacyClient = false;
Austin Borger74fca042022-05-23 12:41:21 -0700150 std::shared_ptr<CameraServiceProxyWrapper> mCameraServiceProxyWrapper;
Eino-Ville Talvalab9d2f332014-09-18 17:24:22 -0700151
Igor Murashkine7ee7632013-06-11 18:10:18 -0700152 virtual sp<IBinder> asBinderWrapper() {
Marco Nelissen06b46062014-11-14 07:58:25 -0800153 return IInterface::asBinder(this);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700154 }
155
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800156 virtual status_t dumpDevice(int fd, const Vector<String16>& args);
157
158 /** Binder client interface-related private members */
159
160 // Mutex that must be locked by methods implementing the binder client
161 // interface. Ensures serialization between incoming client calls.
162 // All methods in this class hierarchy that append 'L' to the name assume
163 // that mBinderSerializationLock is locked when they're called
164 mutable Mutex mBinderSerializationLock;
165
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -0800166 /** CameraDeviceBase instance wrapping HAL3+ entry */
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800167
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800168 // Note: This was previously set to const to avoid mDevice being updated -
169 // b/112639939 (update of sp<> is racy) during dumpDevice (which is important to be lock free
170 // for debugging purpose). The const has been removed since CameraDeviceBase
171 // needs to be set during initializeImpl(). This must not be set / cleared
172 // anywhere else.
173 sp<CameraDeviceBase> mDevice;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800174
175 /** Utility members */
176
177 // Verify that caller is the owner of the camera
178 status_t checkPid(const char *checkLocation) const;
179
180 virtual void detachDevice();
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700181
182 bool mDeviceActive;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800183
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800184 const int mApi1CameraId; // -1 if client is API2
185
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800186private:
187 template<typename TProviderPtr>
Austin Borger1c1bee02023-06-01 16:51:35 -0700188 status_t initializeImpl(TProviderPtr providerPtr, const std::string& monitorTags);
Jayant Chowdhary620763f2022-05-27 05:37:14 +0000189
190 binder::Status disconnectImpl();
191
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800192};
193
194}; // namespace android
195
196#endif