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 
23 namespace android {
24 
25 class IMemory;
26 
27 class CameraService;
28 
29 template <typename TClientBase>
30 class Camera2ClientBase :
31         public TClientBase,
32         public NotificationListener
33 {
34 public:
35     typedef typename TClientBase::TCamCallbacks TCamCallbacks;
36 
37     /**
38      * Base binder interface (see ICamera/ICameraDeviceUser for details)
39      */
40     virtual status_t       connect(const sp<TCamCallbacks>& callbacks);
41     virtual binder::Status disconnect();
42 
43     /**
44      * Interface used by CameraService
45      */
46 
47     // TODO: too many params, move into a ClientArgs<T>
48     Camera2ClientBase(const sp<CameraService>& cameraService,
49                       const sp<TCamCallbacks>& remoteCallback,
50                       const String16& clientPackageName,
51                       const std::optional<String16>& clientFeatureId,
52                       const String8& cameraId,
53                       int api1CameraId,
54                       int cameraFacing,
55                       int sensorOrientation,
56                       int clientPid,
57                       uid_t clientUid,
58                       int servicePid,
59                       bool overrideForPerfClass,
60                       bool legacyClient = false);
61     virtual ~Camera2ClientBase();
62 
63     virtual status_t      initialize(sp<CameraProviderManager> manager, const String8& monitorTags);
64     virtual status_t      dumpClient(int fd, const Vector<String16>& args);
65 
66     /**
67      * NotificationListener implementation
68      */
69 
70     virtual void          notifyError(int32_t errorCode,
71                                       const CaptureResultExtras& resultExtras);
72     virtual status_t      notifyActive();  // Returns errors on app ops permission failures
73     virtual void          notifyIdle(int64_t requestCount, int64_t resultErrorCount,
74                                      bool deviceError,
75                                      const std::vector<hardware::CameraStreamStats>& streamStats);
76     virtual void          notifyShutter(const CaptureResultExtras& resultExtras,
77                                         nsecs_t timestamp);
78     virtual void          notifyAutoFocus(uint8_t newState, int triggerId);
79     virtual void          notifyAutoExposure(uint8_t newState, int triggerId);
80     virtual void          notifyAutoWhitebalance(uint8_t newState,
81                                                  int triggerId);
82     virtual void          notifyPrepared(int streamId);
83     virtual void          notifyRequestQueueEmpty();
84     virtual void          notifyRepeatingRequestError(long lastFrameNumber);
85 
86     int                   getCameraId() const;
87     const sp<CameraDeviceBase>&
88                           getCameraDevice();
89     int                   getCameraDeviceVersion() const;
90     const sp<CameraService>&
91                           getCameraService();
92 
93     /**
94      * Interface used by independent components of CameraClient2Base.
95      */
96 
97     // Simple class to ensure that access to TCamCallbacks is serialized
98     // by requiring mRemoteCallbackLock to be locked before access to
99     // mRemoteCallback is possible.
100     class SharedCameraCallbacks {
101       public:
102         class Lock {
103           public:
104             explicit Lock(SharedCameraCallbacks &client);
105             ~Lock();
106             sp<TCamCallbacks> &mRemoteCallback;
107           private:
108             SharedCameraCallbacks &mSharedClient;
109         };
110         explicit SharedCameraCallbacks(const sp<TCamCallbacks>& client);
111         SharedCameraCallbacks& operator=(const sp<TCamCallbacks>& client);
112         void clear();
113       private:
114         sp<TCamCallbacks> mRemoteCallback;
115         mutable Mutex mRemoteCallbackLock;
116     } mSharedCameraCallbacks;
117 
118 protected:
119 
120     // The PID provided in the constructor call
121     pid_t mInitialClientPid;
122 
asBinderWrapper()123     virtual sp<IBinder> asBinderWrapper() {
124         return IInterface::asBinder(this);
125     }
126 
127     virtual status_t      dumpDevice(int fd, const Vector<String16>& args);
128 
129     /** Binder client interface-related private members */
130 
131     // Mutex that must be locked by methods implementing the binder client
132     // interface. Ensures serialization between incoming client calls.
133     // All methods in this class hierarchy that append 'L' to the name assume
134     // that mBinderSerializationLock is locked when they're called
135     mutable Mutex         mBinderSerializationLock;
136 
137     /** CameraDeviceBase instance wrapping HAL3+ entry */
138 
139     const int mDeviceVersion;
140 
141     // Set to const to avoid mDevice being updated (update of sp<> is racy) during
142     // dumpDevice (which is important to be lock free for debugging purpose)
143     const sp<CameraDeviceBase>  mDevice;
144 
145     /** Utility members */
146 
147     // Verify that caller is the owner of the camera
148     status_t              checkPid(const char *checkLocation) const;
149 
150     virtual void          detachDevice();
151 
152     bool                  mDeviceActive;
153 
154     const int             mApi1CameraId; // -1 if client is API2
155 
156 private:
157     template<typename TProviderPtr>
158     status_t              initializeImpl(TProviderPtr providerPtr, const String8& monitorTags);
159 };
160 
161 }; // namespace android
162 
163 #endif
164