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