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 std::string& clientPackageName,
57                       bool systemNativeClient,
58                       const std::optional<std::string>& clientFeatureId,
59                       const std::string& cameraId,
60                       int api1CameraId,
61                       int cameraFacing,
62                       int sensorOrientation,
63                       int clientPid,
64                       uid_t clientUid,
65                       int servicePid,
66                       bool overrideForPerfClass,
67                       int rotationOverride,
68                       bool legacyClient = false);
69     virtual ~Camera2ClientBase();
70 
71     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;
77 
78     /**
79      * NotificationListener implementation
80      */
81 
82     virtual void          notifyError(int32_t errorCode,
83                                       const CaptureResultExtras& resultExtras);
84     virtual void          notifyPhysicalCameraChange(const std::string &physicalId) override;
85     // Returns errors on app ops permission failures
86     virtual status_t      notifyActive(float maxPreviewFps);
notifyIdle(int64_t,int64_t,bool,std::pair<int32_t,int32_t>,const std::vector<hardware::CameraStreamStats> &)87     virtual void          notifyIdle(int64_t /*requestCount*/, int64_t /*resultErrorCount*/,
88                                      bool /*deviceError*/,
89                                      std::pair<int32_t, int32_t> /*mostRequestedFpsRange*/,
90                                      const std::vector<hardware::CameraStreamStats>&) {}
91     virtual void          notifyShutter(const CaptureResultExtras& resultExtras,
92                                         nsecs_t timestamp);
93     virtual void          notifyAutoFocus(uint8_t newState, int triggerId);
94     virtual void          notifyAutoExposure(uint8_t newState, int triggerId);
95     virtual void          notifyAutoWhitebalance(uint8_t newState,
96                                                  int triggerId);
97     virtual void          notifyPrepared(int streamId);
98     virtual void          notifyRequestQueueEmpty();
99     virtual void          notifyRepeatingRequestError(long lastFrameNumber);
100 
101     void                  notifyIdleWithUserTag(int64_t requestCount, int64_t resultErrorCount,
102                                      bool deviceError,
103                                      std::pair<int32_t, int32_t> mostRequestedFpsRange,
104                                      const std::vector<hardware::CameraStreamStats>& streamStats,
105                                      const std::string& userTag, int videoStabilizationMode,
106                                      bool usedUltraWide, bool usedZoomOverride);
107 
108     int                   getCameraId() const;
109     const sp<CameraDeviceBase>&
110                           getCameraDevice();
111     int                   getCameraDeviceVersion() const;
112     const sp<CameraService>&
113                           getCameraService();
114 
115     /**
116      * Interface used by independent components of CameraClient2Base.
117      */
118 
119     // Simple class to ensure that access to TCamCallbacks is serialized
120     // by requiring mRemoteCallbackLock to be locked before access to
121     // mRemoteCallback is possible.
122     class SharedCameraCallbacks {
123       public:
124         class Lock {
125           public:
126             explicit Lock(SharedCameraCallbacks &client);
127             ~Lock();
128             sp<TCamCallbacks> &mRemoteCallback;
129           private:
130             SharedCameraCallbacks &mSharedClient;
131         };
132         explicit SharedCameraCallbacks(const sp<TCamCallbacks>& client);
133         SharedCameraCallbacks& operator=(const sp<TCamCallbacks>& client);
134         void clear();
135       private:
136         sp<TCamCallbacks> mRemoteCallback;
137         mutable Mutex mRemoteCallbackLock;
138     } mSharedCameraCallbacks;
139 
140     status_t      injectCamera(const std::string& injectedCamId,
141                                sp<CameraProviderManager> manager) override;
142     status_t      stopInjection() override;
143 
144     status_t      injectSessionParams(const CameraMetadata& sessionParams) override;
145 
146 protected:
147 
148     // The PID provided in the constructor call
149     pid_t mInitialClientPid;
150     bool mOverrideForPerfClass = false;
151     bool mLegacyClient = false;
152     std::shared_ptr<CameraServiceProxyWrapper> mCameraServiceProxyWrapper;
153 
asBinderWrapper()154     virtual sp<IBinder> asBinderWrapper() {
155         return IInterface::asBinder(this);
156     }
157 
158     virtual status_t      dumpDevice(int fd, const Vector<String16>& args);
159 
160     /** Binder client interface-related private members */
161 
162     // Mutex that must be locked by methods implementing the binder client
163     // interface. Ensures serialization between incoming client calls.
164     // All methods in this class hierarchy that append 'L' to the name assume
165     // that mBinderSerializationLock is locked when they're called
166     mutable Mutex         mBinderSerializationLock;
167 
168     /** CameraDeviceBase instance wrapping HAL3+ entry */
169 
170     // Note: This was previously set to const to avoid mDevice being updated -
171     // b/112639939 (update of sp<> is racy) during dumpDevice (which is important to be lock free
172     // for debugging purpose). The const has been removed since CameraDeviceBase
173     // needs to be set during initializeImpl(). This must not be set / cleared
174     // anywhere else.
175     sp<CameraDeviceBase>  mDevice;
176 
177     /** Utility members */
178 
179     // Verify that caller is the owner of the camera
180     status_t              checkPid(const char *checkLocation) const;
181 
182     virtual void          detachDevice();
183 
184     bool                  mDeviceActive;
185 
186     const int             mApi1CameraId; // -1 if client is API2
187 
188 private:
189     template<typename TProviderPtr>
190     status_t              initializeImpl(TProviderPtr providerPtr, const std::string& monitorTags);
191 
192     binder::Status disconnectImpl();
193 
194 };
195 
196 }; // namespace android
197 
198 #endif
199