| Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 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 |  | 
| Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 20 | #include "common/CameraDeviceBase.h" | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 21 | #include "camera/CaptureResult.h" | 
| Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 22 | #include "utils/CameraServiceProxyWrapper.h" | 
| Jayant Chowdhary | 48162a7 | 2022-05-27 05:37:14 +0000 | [diff] [blame] | 23 | #include "CameraServiceWatchdog.h" | 
| Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 24 |  | 
|  | 25 | namespace android { | 
|  | 26 |  | 
|  | 27 | class IMemory; | 
|  | 28 |  | 
| Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 29 | class CameraService; | 
|  | 30 |  | 
| Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 31 | template <typename TClientBase> | 
|  | 32 | class Camera2ClientBase : | 
|  | 33 | public TClientBase, | 
| Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 34 | public NotificationListener | 
| Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 35 | { | 
|  | 36 | public: | 
|  | 37 | typedef typename TClientBase::TCamCallbacks TCamCallbacks; | 
|  | 38 |  | 
|  | 39 | /** | 
| Ruben Brunk | 9efdf95 | 2015-03-18 23:11:57 -0700 | [diff] [blame] | 40 | * Base binder interface (see ICamera/ICameraDeviceUser for details) | 
| Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 41 | */ | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 42 | virtual status_t       connect(const sp<TCamCallbacks>& callbacks); | 
|  | 43 | virtual binder::Status disconnect(); | 
| Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 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, | 
| Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 52 | std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper, | 
| Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 53 | const std::string& clientPackageName, | 
| Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 54 | bool systemNativeClient, | 
| Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 55 | const std::optional<std::string>& clientFeatureId, | 
|  | 56 | const std::string& cameraId, | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 57 | int api1CameraId, | 
| Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 58 | int cameraFacing, | 
| Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 59 | int sensorOrientation, | 
| Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 60 | int clientPid, | 
|  | 61 | uid_t clientUid, | 
| Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 62 | int servicePid, | 
| Emilian Peev | 5104fe9 | 2021-10-21 14:27:09 -0700 | [diff] [blame] | 63 | bool overrideForPerfClass, | 
| Austin Borger | 3560b7e | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 64 | bool overrideToPortrait, | 
| Emilian Peev | 5104fe9 | 2021-10-21 14:27:09 -0700 | [diff] [blame] | 65 | bool legacyClient = false); | 
| Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 66 | virtual ~Camera2ClientBase(); | 
|  | 67 |  | 
| Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 68 | virtual status_t      initialize(sp<CameraProviderManager> manager, | 
|  | 69 | const std::string& monitorTags) override; | 
|  | 70 | virtual status_t      dumpClient(int fd, const Vector<String16>& args) override; | 
|  | 71 | virtual status_t      startWatchingTags(const std::string &tags, int out) override; | 
|  | 72 | virtual status_t      stopWatchingTags(int out) override; | 
|  | 73 | virtual status_t      dumpWatchedEventsToVector(std::vector<std::string> &out) override; | 
| Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 74 |  | 
|  | 75 | /** | 
| Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 76 | * NotificationListener implementation | 
| Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 77 | */ | 
|  | 78 |  | 
| Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 79 | virtual void          notifyError(int32_t errorCode, | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 80 | const CaptureResultExtras& resultExtras); | 
| Austin Borger | c809976 | 2023-01-12 17:08:46 -0800 | [diff] [blame] | 81 | virtual void          notifyPhysicalCameraChange(const std::string &physicalId) override; | 
| Austin Borger | 4a870a3 | 2022-02-25 01:48:41 +0000 | [diff] [blame] | 82 | // Returns errors on app ops permission failures | 
|  | 83 | virtual status_t      notifyActive(float maxPreviewFps); | 
| Shuzhen Wang | d26b186 | 2022-03-07 12:05:05 -0800 | [diff] [blame] | 84 | virtual void          notifyIdle(int64_t /*requestCount*/, int64_t /*resultErrorCount*/, | 
|  | 85 | bool /*deviceError*/, | 
|  | 86 | const std::vector<hardware::CameraStreamStats>&) {} | 
| Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 87 | virtual void          notifyShutter(const CaptureResultExtras& resultExtras, | 
|  | 88 | nsecs_t timestamp); | 
| Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 89 | virtual void          notifyAutoFocus(uint8_t newState, int triggerId); | 
|  | 90 | virtual void          notifyAutoExposure(uint8_t newState, int triggerId); | 
|  | 91 | virtual void          notifyAutoWhitebalance(uint8_t newState, | 
|  | 92 | int triggerId); | 
| Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 93 | virtual void          notifyPrepared(int streamId); | 
| Shuzhen Wang | 9d06601 | 2016-09-30 11:30:20 -0700 | [diff] [blame] | 94 | virtual void          notifyRequestQueueEmpty(); | 
| Chien-Yu Chen | e8c535e | 2016-04-14 12:18:26 -0700 | [diff] [blame] | 95 | virtual void          notifyRepeatingRequestError(long lastFrameNumber); | 
| Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 96 |  | 
| Shuzhen Wang | d26b186 | 2022-03-07 12:05:05 -0800 | [diff] [blame] | 97 | void                  notifyIdleWithUserTag(int64_t requestCount, int64_t resultErrorCount, | 
|  | 98 | bool deviceError, | 
|  | 99 | const std::vector<hardware::CameraStreamStats>& streamStats, | 
| Shuzhen Wang | 9372b0b | 2022-05-11 18:55:19 -0700 | [diff] [blame] | 100 | const std::string& userTag, int videoStabilizationMode); | 
| Shuzhen Wang | d26b186 | 2022-03-07 12:05:05 -0800 | [diff] [blame] | 101 |  | 
| Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 102 | int                   getCameraId() const; | 
|  | 103 | const sp<CameraDeviceBase>& | 
|  | 104 | getCameraDevice(); | 
| Yin-Chia Yeh | cd8fce8 | 2014-06-18 10:51:34 -0700 | [diff] [blame] | 105 | int                   getCameraDeviceVersion() const; | 
| Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 106 | const sp<CameraService>& | 
|  | 107 | getCameraService(); | 
|  | 108 |  | 
|  | 109 | /** | 
|  | 110 | * Interface used by independent components of CameraClient2Base. | 
|  | 111 | */ | 
|  | 112 |  | 
|  | 113 | // Simple class to ensure that access to TCamCallbacks is serialized | 
|  | 114 | // by requiring mRemoteCallbackLock to be locked before access to | 
|  | 115 | // mRemoteCallback is possible. | 
|  | 116 | class SharedCameraCallbacks { | 
|  | 117 | public: | 
|  | 118 | class Lock { | 
|  | 119 | public: | 
| Chih-Hung Hsieh | 8b0b971 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 120 | explicit Lock(SharedCameraCallbacks &client); | 
| Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 121 | ~Lock(); | 
|  | 122 | sp<TCamCallbacks> &mRemoteCallback; | 
|  | 123 | private: | 
|  | 124 | SharedCameraCallbacks &mSharedClient; | 
|  | 125 | }; | 
| Chih-Hung Hsieh | 8b0b971 | 2016-08-09 14:25:53 -0700 | [diff] [blame] | 126 | explicit SharedCameraCallbacks(const sp<TCamCallbacks>& client); | 
| Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 127 | SharedCameraCallbacks& operator=(const sp<TCamCallbacks>& client); | 
|  | 128 | void clear(); | 
|  | 129 | private: | 
|  | 130 | sp<TCamCallbacks> mRemoteCallback; | 
|  | 131 | mutable Mutex mRemoteCallbackLock; | 
|  | 132 | } mSharedCameraCallbacks; | 
|  | 133 |  | 
| Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 134 | status_t      injectCamera(const std::string& injectedCamId, | 
| Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 135 | sp<CameraProviderManager> manager) override; | 
|  | 136 | status_t      stopInjection() override; | 
|  | 137 |  | 
| Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 138 | protected: | 
|  | 139 |  | 
| Ravneet Dhanjal | 72c9665 | 2022-06-29 01:34:01 +0000 | [diff] [blame] | 140 | // Used for watchdog timeout to monitor disconnect | 
|  | 141 | static const nsecs_t kBufferTimeDisconnectNs = 3000000000; // 3 sec. | 
|  | 142 |  | 
| Eino-Ville Talvala | b9d2f33 | 2014-09-18 17:24:22 -0700 | [diff] [blame] | 143 | // The PID provided in the constructor call | 
|  | 144 | pid_t mInitialClientPid; | 
| Jayant Chowdhary | 22441f3 | 2021-12-26 18:35:41 -0800 | [diff] [blame] | 145 | bool mOverrideForPerfClass = false; | 
|  | 146 | bool mLegacyClient = false; | 
| Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 147 | std::shared_ptr<CameraServiceProxyWrapper> mCameraServiceProxyWrapper; | 
| Eino-Ville Talvala | b9d2f33 | 2014-09-18 17:24:22 -0700 | [diff] [blame] | 148 |  | 
| Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 149 | virtual sp<IBinder> asBinderWrapper() { | 
| Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 150 | return IInterface::asBinder(this); | 
| Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 151 | } | 
|  | 152 |  | 
| Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 153 | virtual status_t      dumpDevice(int fd, const Vector<String16>& args); | 
|  | 154 |  | 
|  | 155 | /** Binder client interface-related private members */ | 
|  | 156 |  | 
|  | 157 | // Mutex that must be locked by methods implementing the binder client | 
|  | 158 | // interface. Ensures serialization between incoming client calls. | 
|  | 159 | // All methods in this class hierarchy that append 'L' to the name assume | 
|  | 160 | // that mBinderSerializationLock is locked when they're called | 
|  | 161 | mutable Mutex         mBinderSerializationLock; | 
|  | 162 |  | 
| Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 163 | /** CameraDeviceBase instance wrapping HAL3+ entry */ | 
| Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 164 |  | 
| Jayant Chowdhary | 22441f3 | 2021-12-26 18:35:41 -0800 | [diff] [blame] | 165 | // Note: This was previously set to const to avoid mDevice being updated - | 
|  | 166 | // b/112639939 (update of sp<> is racy) during dumpDevice (which is important to be lock free | 
|  | 167 | // for debugging purpose). The const has been removed since CameraDeviceBase | 
|  | 168 | // needs to be set during initializeImpl(). This must not be set / cleared | 
|  | 169 | // anywhere else. | 
|  | 170 | sp<CameraDeviceBase>  mDevice; | 
| Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 171 |  | 
|  | 172 | /** Utility members */ | 
|  | 173 |  | 
|  | 174 | // Verify that caller is the owner of the camera | 
|  | 175 | status_t              checkPid(const char *checkLocation) const; | 
|  | 176 |  | 
|  | 177 | virtual void          detachDevice(); | 
| Eino-Ville Talvala | 412fe56 | 2015-08-20 17:08:32 -0700 | [diff] [blame] | 178 |  | 
|  | 179 | bool                  mDeviceActive; | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 180 |  | 
| Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 181 | const int             mApi1CameraId; // -1 if client is API2 | 
|  | 182 |  | 
| Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 183 | private: | 
|  | 184 | template<typename TProviderPtr> | 
| Austin Borger | 0fb3ad9 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 185 | status_t              initializeImpl(TProviderPtr providerPtr, const std::string& monitorTags); | 
| Jayant Chowdhary | 48162a7 | 2022-05-27 05:37:14 +0000 | [diff] [blame] | 186 |  | 
|  | 187 | binder::Status disconnectImpl(); | 
|  | 188 |  | 
|  | 189 | // Watchdog thread | 
|  | 190 | sp<CameraServiceWatchdog> mCameraServiceWatchdog; | 
|  | 191 |  | 
| Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 192 | }; | 
|  | 193 |  | 
|  | 194 | }; // namespace android | 
|  | 195 |  | 
|  | 196 | #endif |