• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/CaptureResult.h"
22 #include "utils/CameraServiceProxyWrapper.h"
23 #include "CameraServiceWatchdog.h"
24 
25 namespace android {
26 
27 class IMemory;
28 
29 class CameraService;
30 
31 template <typename TClientBase>
32 class Camera2ClientBase :
33         public TClientBase,
34         public NotificationListener
35 {
36 public:
37     typedef typename TClientBase::TCamCallbacks TCamCallbacks;
38 
39     /**
40      * Base binder interface (see ICamera/ICameraDeviceUser for details)
41      */
42     virtual status_t       connect(const sp<TCamCallbacks>& callbacks);
43     virtual binder::Status disconnect();
44 
45     /**
46      * Interface used by CameraService
47      */
48 
49     // TODO: too many params, move into a ClientArgs<T>
50     Camera2ClientBase(const sp<CameraService>& cameraService,
51                       const sp<TCamCallbacks>& remoteCallback,
52                       std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper,
53                       const String16& clientPackageName,
54                       bool systemNativeClient,
55                       const std::optional<String16>& clientFeatureId,
56                       const String8& cameraId,
57                       int api1CameraId,
58                       int cameraFacing,
59                       int sensorOrientation,
60                       int clientPid,
61                       uid_t clientUid,
62                       int servicePid,
63                       bool overrideForPerfClass,
64                       bool overrideToPortrait,
65                       bool legacyClient = false);
66     virtual ~Camera2ClientBase();
67 
68     virtual status_t      initialize(sp<CameraProviderManager> manager, const String8& monitorTags);
69     virtual status_t      dumpClient(int fd, const Vector<String16>& args);
70     virtual status_t      startWatchingTags(const String8 &tags, int out);
71     virtual status_t      stopWatchingTags(int out);
72     virtual status_t      dumpWatchedEventsToVector(std::vector<std::string> &out);
73 
74     /**
75      * NotificationListener implementation
76      */
77 
78     virtual void          notifyError(int32_t errorCode,
79                                       const CaptureResultExtras& resultExtras);
80     virtual void          notifyPhysicalCameraChange(const std::string &physicalId) override;
81     // Returns errors on app ops permission failures
82     virtual status_t      notifyActive(float maxPreviewFps);
notifyIdle(int64_t,int64_t,bool,const std::vector<hardware::CameraStreamStats> &)83     virtual void          notifyIdle(int64_t /*requestCount*/, int64_t /*resultErrorCount*/,
84                                      bool /*deviceError*/,
85                                      const std::vector<hardware::CameraStreamStats>&) {}
86     virtual void          notifyShutter(const CaptureResultExtras& resultExtras,
87                                         nsecs_t timestamp);
88     virtual void          notifyAutoFocus(uint8_t newState, int triggerId);
89     virtual void          notifyAutoExposure(uint8_t newState, int triggerId);
90     virtual void          notifyAutoWhitebalance(uint8_t newState,
91                                                  int triggerId);
92     virtual void          notifyPrepared(int streamId);
93     virtual void          notifyRequestQueueEmpty();
94     virtual void          notifyRepeatingRequestError(long lastFrameNumber);
95 
96     void                  notifyIdleWithUserTag(int64_t requestCount, int64_t resultErrorCount,
97                                      bool deviceError,
98                                      const std::vector<hardware::CameraStreamStats>& streamStats,
99                                      const std::string& userTag, int videoStabilizationMode);
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 String8& injectedCamId,
134                                sp<CameraProviderManager> manager) override;
135     status_t      stopInjection() override;
136 
137 protected:
138 
139     // Used for watchdog timeout to monitor disconnect
140     static const nsecs_t kBufferTimeDisconnectNs = 3000000000; // 3 sec.
141 
142     // The PID provided in the constructor call
143     pid_t mInitialClientPid;
144     bool mOverrideForPerfClass = false;
145     bool mLegacyClient = false;
146     std::shared_ptr<CameraServiceProxyWrapper> mCameraServiceProxyWrapper;
147 
asBinderWrapper()148     virtual sp<IBinder> asBinderWrapper() {
149         return IInterface::asBinder(this);
150     }
151 
152     virtual status_t      dumpDevice(int fd, const Vector<String16>& args);
153 
154     /** Binder client interface-related private members */
155 
156     // Mutex that must be locked by methods implementing the binder client
157     // interface. Ensures serialization between incoming client calls.
158     // All methods in this class hierarchy that append 'L' to the name assume
159     // that mBinderSerializationLock is locked when they're called
160     mutable Mutex         mBinderSerializationLock;
161 
162     /** CameraDeviceBase instance wrapping HAL3+ entry */
163 
164     // Note: This was previously set to const to avoid mDevice being updated -
165     // b/112639939 (update of sp<> is racy) during dumpDevice (which is important to be lock free
166     // for debugging purpose). The const has been removed since CameraDeviceBase
167     // needs to be set during initializeImpl(). This must not be set / cleared
168     // anywhere else.
169     sp<CameraDeviceBase>  mDevice;
170 
171     /** Utility members */
172 
173     // Verify that caller is the owner of the camera
174     status_t              checkPid(const char *checkLocation) const;
175 
176     virtual void          detachDevice();
177 
178     bool                  mDeviceActive;
179 
180     const int             mApi1CameraId; // -1 if client is API2
181 
182 private:
183     template<typename TProviderPtr>
184     status_t              initializeImpl(TProviderPtr providerPtr, const String8& monitorTags);
185 
186     binder::Status disconnectImpl();
187 
188     // Watchdog thread
189     sp<CameraServiceWatchdog> mCameraServiceWatchdog;
190 
191 };
192 
193 }; // namespace android
194 
195 #endif
196