xref: /aosp_15_r20/frameworks/av/services/camera/libcameraservice/common/Camera2ClientBase.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1 /*
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 
20 #include "common/CameraDeviceBase.h"
21 #include "camera/CameraMetadata.h"
22 #include "camera/CaptureResult.h"
23 #include "utils/CameraServiceProxyWrapper.h"
24 #include "utils/AttributionAndPermissionUtils.h"
25 #include "CameraServiceWatchdog.h"
26 
27 namespace android {
28 
29 class IMemory;
30 
31 class CameraService;
32 
33 template <typename TClientBase>
34 class Camera2ClientBase :
35         public TClientBase,
36         public NotificationListener
37 {
38 public:
39     typedef typename TClientBase::TCamCallbacks TCamCallbacks;
40 
41     /**
42      * Base binder interface (see ICamera/ICameraDeviceUser for details)
43      */
44     virtual status_t       connect(const sp<TCamCallbacks>& callbacks);
45     virtual binder::Status disconnect();
46 
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,
54                       std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper,
55                       std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils,
56                       const AttributionSourceState& clientAttribution, int callingPid,
57                       bool systemNativeClient, const std::string& cameraId, int api1CameraId,
58                       int cameraFacing, int sensorOrientation, int servicePid,
59                       bool overrideForPerfClass, int rotationOverride,  bool sharedMode,
60                       bool legacyClient = false);
61     virtual ~Camera2ClientBase();
62 
63     virtual status_t      initialize(sp<CameraProviderManager> manager,
64             const std::string& monitorTags) override;
65     virtual status_t      dumpClient(int fd, const Vector<String16>& args) override;
66     virtual status_t      startWatchingTags(const std::string &tags, int out) override;
67     virtual status_t      stopWatchingTags(int out) override;
68     virtual status_t      dumpWatchedEventsToVector(std::vector<std::string> &out) override;
69 
70     /**
71      * NotificationListener implementation
72      */
73 
74     virtual void          notifyError(int32_t errorCode,
75                                       const CaptureResultExtras& resultExtras);
76     virtual void          notifyPhysicalCameraChange(const std::string &physicalId) override;
77     // Returns errors on app ops permission failures
78     virtual status_t      notifyActive(float maxPreviewFps);
notifyIdle(int64_t,int64_t,bool,std::pair<int32_t,int32_t>,const std::vector<hardware::CameraStreamStats> &)79     virtual void          notifyIdle(int64_t /*requestCount*/, int64_t /*resultErrorCount*/,
80                                      bool /*deviceError*/,
81                                      std::pair<int32_t, int32_t> /*mostRequestedFpsRange*/,
82                                      const std::vector<hardware::CameraStreamStats>&) {}
83     virtual void          notifyShutter(const CaptureResultExtras& resultExtras,
84                                         nsecs_t timestamp);
85     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);
89     virtual void          notifyPrepared(int streamId);
90     virtual void          notifyRequestQueueEmpty();
91     virtual void          notifyRepeatingRequestError(long lastFrameNumber);
92     virtual void          notifyClientSharedAccessPriorityChanged(bool primaryClient) override;
93 
94     void                  notifyIdleWithUserTag(int64_t requestCount, int64_t resultErrorCount,
95                                      bool deviceError,
96                                      std::pair<int32_t, int32_t> mostRequestedFpsRange,
97                                      const std::vector<hardware::CameraStreamStats>& streamStats,
98                                      const std::string& userTag, int videoStabilizationMode,
99                                      bool usedUltraWide, bool usedZoomOverride);
100 
101     int                   getCameraId() const;
102     const sp<CameraDeviceBase>&
103                           getCameraDevice();
104     int                   getCameraDeviceVersion() const;
105     const sp<CameraService>&
106                           getCameraService();
107 
108     /**
109      * Interface used by independent components of CameraClient2Base.
110      */
111 
112     // Simple class to ensure that access to TCamCallbacks is serialized
113     // by requiring mRemoteCallbackLock to be locked before access to
114     // mRemoteCallback is possible.
115     class SharedCameraCallbacks {
116       public:
117         class Lock {
118           public:
119             explicit Lock(SharedCameraCallbacks &client);
120             ~Lock();
121             sp<TCamCallbacks> &mRemoteCallback;
122           private:
123             SharedCameraCallbacks &mSharedClient;
124         };
125         explicit SharedCameraCallbacks(const sp<TCamCallbacks>& client);
126         SharedCameraCallbacks& operator=(const sp<TCamCallbacks>& client);
127         void clear();
128       private:
129         sp<TCamCallbacks> mRemoteCallback;
130         mutable Mutex mRemoteCallbackLock;
131     } mSharedCameraCallbacks;
132 
133     status_t      injectCamera(const std::string& injectedCamId,
134                                sp<CameraProviderManager> manager) override;
135     status_t      stopInjection() override;
136 
137     status_t      injectSessionParams(const CameraMetadata& sessionParams) override;
138 
139 protected:
140 
141     // The PID provided in the constructor call
142     pid_t mInitialClientPid;
143     bool mOverrideForPerfClass = false;
144     bool mLegacyClient = false;
145     std::shared_ptr<CameraServiceProxyWrapper> mCameraServiceProxyWrapper;
146 
asBinderWrapper()147     virtual sp<IBinder> asBinderWrapper() {
148         return IInterface::asBinder(this);
149     }
150 
151     virtual status_t      dumpDevice(int fd, const Vector<String16>& args);
152 
153     /** Binder client interface-related private members */
154 
155     // Mutex that must be locked by methods implementing the binder client
156     // interface. Ensures serialization between incoming client calls.
157     // All methods in this class hierarchy that append 'L' to the name assume
158     // that mBinderSerializationLock is locked when they're called
159     mutable Mutex         mBinderSerializationLock;
160 
161     /** CameraDeviceBase instance wrapping HAL3+ entry */
162 
163     // Note: This was previously set to const to avoid mDevice being updated -
164     // b/112639939 (update of sp<> is racy) during dumpDevice (which is important to be lock free
165     // for debugging purpose). The const has been removed since CameraDeviceBase
166     // needs to be set during initializeImpl(). This must not be set / cleared
167     // anywhere else.
168     sp<CameraDeviceBase>  mDevice;
169 
170     /** Utility members */
171 
172     // Verify that caller is the owner of the camera
173     status_t              checkPid(const char *checkLocation) const;
174 
175     virtual void          detachDevice();
176 
177     bool                  mDeviceActive;
178 
179     const int             mApi1CameraId; // -1 if client is API2
180 
181 private:
182     template<typename TProviderPtr>
183     status_t              initializeImpl(TProviderPtr providerPtr, const std::string& monitorTags);
184 
185     binder::Status disconnectImpl();
186 
187 };
188 
189 }; // namespace android
190 
191 #endif
192