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 | #define LOG_TAG "Camera2ClientBase" |
| 18 | #define ATRACE_TAG ATRACE_TAG_CAMERA |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
Colin Cross | e5729fa | 2014-03-21 15:04:25 -0700 | [diff] [blame] | 21 | #include <inttypes.h> |
| 22 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 23 | #include <utils/Log.h> |
| 24 | #include <utils/Trace.h> |
| 25 | |
| 26 | #include <cutils/properties.h> |
| 27 | #include <gui/Surface.h> |
| 28 | #include <gui/Surface.h> |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 29 | |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 30 | #include <camera/CameraSessionStats.h> |
| 31 | |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 32 | #include "common/Camera2ClientBase.h" |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 33 | |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 34 | #include "api2/CameraDeviceClient.h" |
| 35 | |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 36 | #include "device3/Camera3Device.h" |
Jayant Chowdhary | 35642f2 | 2022-01-08 00:39:39 +0000 | [diff] [blame] | 37 | #include "device3/aidl/AidlCamera3Device.h" |
Jayant Chowdhary | 22441f3 | 2021-12-26 18:35:41 -0800 | [diff] [blame] | 38 | #include "device3/hidl/HidlCamera3Device.h" |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 39 | #include "utils/CameraThreadState.h" |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 40 | |
| 41 | namespace android { |
Jayant Chowdhary | 620763f | 2022-05-27 05:37:14 +0000 | [diff] [blame] | 42 | |
| 43 | const static size_t kDisconnectTimeoutMs = 2500; |
| 44 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 45 | using namespace camera2; |
| 46 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 47 | // Interface used by CameraService |
| 48 | |
| 49 | template <typename TClientBase> |
| 50 | Camera2ClientBase<TClientBase>::Camera2ClientBase( |
| 51 | const sp<CameraService>& cameraService, |
| 52 | const sp<TCamCallbacks>& remoteCallback, |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 53 | std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper, |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 54 | const String16& clientPackageName, |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 55 | bool systemNativeClient, |
Jooyung Han | 3f9a3b4 | 2020-01-23 12:27:18 +0900 | [diff] [blame] | 56 | const std::optional<String16>& clientFeatureId, |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 57 | const String8& cameraId, |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 58 | int api1CameraId, |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 59 | int cameraFacing, |
Emilian Peev | 8b64f28 | 2021-03-25 16:49:57 -0700 | [diff] [blame] | 60 | int sensorOrientation, |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 61 | int clientPid, |
| 62 | uid_t clientUid, |
Shuzhen Wang | d4abdf7 | 2021-05-28 11:22:50 -0700 | [diff] [blame] | 63 | int servicePid, |
Emilian Peev | 5104fe9 | 2021-10-21 14:27:09 -0700 | [diff] [blame] | 64 | bool overrideForPerfClass, |
| 65 | bool legacyClient): |
Jayant Chowdhary | eb0169f | 2022-01-31 00:00:02 -0800 | [diff] [blame] | 66 | TClientBase(cameraService, remoteCallback, clientPackageName, systemNativeClient, |
| 67 | clientFeatureId, cameraId, api1CameraId, cameraFacing, sensorOrientation, clientPid, |
| 68 | clientUid, servicePid), |
Yin-Chia Yeh | cd8fce8 | 2014-06-18 10:51:34 -0700 | [diff] [blame] | 69 | mSharedCameraCallbacks(remoteCallback), |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 70 | mCameraServiceProxyWrapper(cameraServiceProxyWrapper), |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 71 | mDeviceActive(false), mApi1CameraId(api1CameraId) |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 72 | { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 73 | ALOGI("Camera %s: Opened. Client: %s (PID %d, UID %d)", cameraId.string(), |
Eino-Ville Talvala | b9d2f33 | 2014-09-18 17:24:22 -0700 | [diff] [blame] | 74 | String8(clientPackageName).string(), clientPid, clientUid); |
Igor Murashkin | 98e2472 | 2013-06-19 19:51:04 -0700 | [diff] [blame] | 75 | |
Eino-Ville Talvala | b9d2f33 | 2014-09-18 17:24:22 -0700 | [diff] [blame] | 76 | mInitialClientPid = clientPid; |
Jayant Chowdhary | 22441f3 | 2021-12-26 18:35:41 -0800 | [diff] [blame] | 77 | mOverrideForPerfClass = overrideForPerfClass; |
| 78 | mLegacyClient = legacyClient; |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | template <typename TClientBase> |
| 82 | status_t Camera2ClientBase<TClientBase>::checkPid(const char* checkLocation) |
| 83 | const { |
| 84 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 85 | int callingPid = CameraThreadState::getCallingPid(); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 86 | if (callingPid == TClientBase::mClientPid) return NO_ERROR; |
| 87 | |
| 88 | ALOGE("%s: attempt to use a locked camera from a different process" |
| 89 | " (old pid %d, new pid %d)", checkLocation, TClientBase::mClientPid, callingPid); |
| 90 | return PERMISSION_DENIED; |
| 91 | } |
| 92 | |
| 93 | template <typename TClientBase> |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 94 | status_t Camera2ClientBase<TClientBase>::initialize(sp<CameraProviderManager> manager, |
| 95 | const String8& monitorTags) { |
| 96 | return initializeImpl(manager, monitorTags); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | template <typename TClientBase> |
| 100 | template <typename TProviderPtr> |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 101 | status_t Camera2ClientBase<TClientBase>::initializeImpl(TProviderPtr providerPtr, |
| 102 | const String8& monitorTags) { |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 103 | ATRACE_CALL(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 104 | ALOGV("%s: Initializing client for camera %s", __FUNCTION__, |
| 105 | TClientBase::mCameraIdStr.string()); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 106 | status_t res; |
| 107 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 108 | // Verify ops permissions |
| 109 | res = TClientBase::startCameraOps(); |
| 110 | if (res != OK) { |
| 111 | return res; |
| 112 | } |
Jayant Chowdhary | 22441f3 | 2021-12-26 18:35:41 -0800 | [diff] [blame] | 113 | IPCTransport providerTransport = IPCTransport::INVALID; |
| 114 | res = providerPtr->getCameraIdIPCTransport(TClientBase::mCameraIdStr.string(), |
| 115 | &providerTransport); |
| 116 | if (res != OK) { |
| 117 | return res; |
| 118 | } |
| 119 | switch (providerTransport) { |
| 120 | case IPCTransport::HIDL: |
| 121 | mDevice = |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 122 | new HidlCamera3Device(mCameraServiceProxyWrapper, |
| 123 | TClientBase::mCameraIdStr, mOverrideForPerfClass, mLegacyClient); |
Jayant Chowdhary | 22441f3 | 2021-12-26 18:35:41 -0800 | [diff] [blame] | 124 | break; |
| 125 | case IPCTransport::AIDL: |
Jayant Chowdhary | 35642f2 | 2022-01-08 00:39:39 +0000 | [diff] [blame] | 126 | mDevice = |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 127 | new AidlCamera3Device(mCameraServiceProxyWrapper, |
| 128 | TClientBase::mCameraIdStr, mOverrideForPerfClass, mLegacyClient); |
Jayant Chowdhary | 35642f2 | 2022-01-08 00:39:39 +0000 | [diff] [blame] | 129 | break; |
Jayant Chowdhary | 22441f3 | 2021-12-26 18:35:41 -0800 | [diff] [blame] | 130 | default: |
| 131 | ALOGE("%s Invalid transport for camera id %s", __FUNCTION__, |
| 132 | TClientBase::mCameraIdStr.string()); |
| 133 | return NO_INIT; |
| 134 | } |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 135 | if (mDevice == NULL) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 136 | ALOGE("%s: Camera %s: No device connected", |
| 137 | __FUNCTION__, TClientBase::mCameraIdStr.string()); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 138 | return NO_INIT; |
| 139 | } |
| 140 | |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 141 | res = mDevice->initialize(providerPtr, monitorTags); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 142 | if (res != OK) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 143 | ALOGE("%s: Camera %s: unable to initialize device: %s (%d)", |
| 144 | __FUNCTION__, TClientBase::mCameraIdStr.string(), strerror(-res), res); |
Zhijun He | 66281c3 | 2013-09-13 17:59:59 -0700 | [diff] [blame] | 145 | return res; |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 148 | wp<NotificationListener> weakThis(this); |
Yin-Chia Yeh | e1c8063 | 2016-08-08 14:48:05 -0700 | [diff] [blame] | 149 | res = mDevice->setNotifyCallback(weakThis); |
Austin Borger | 7b12954 | 2022-06-09 13:23:06 -0700 | [diff] [blame^] | 150 | if (res != OK) { |
| 151 | ALOGE("%s: Camera %s: Unable to set notify callback: %s (%d)", |
| 152 | __FUNCTION__, TClientBase::mCameraIdStr.string(), strerror(-res), res); |
| 153 | return res; |
| 154 | } |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 155 | |
Jayant Chowdhary | 620763f | 2022-05-27 05:37:14 +0000 | [diff] [blame] | 156 | /** Start watchdog thread */ |
| 157 | mCameraServiceWatchdog = new CameraServiceWatchdog(); |
Austin Borger | 7b12954 | 2022-06-09 13:23:06 -0700 | [diff] [blame^] | 158 | res = mCameraServiceWatchdog->run("Camera2ClientBaseWatchdog"); |
| 159 | if (res != OK) { |
| 160 | ALOGE("%s: Unable to start camera service watchdog thread: %s (%d)", |
| 161 | __FUNCTION__, strerror(-res), res); |
| 162 | return res; |
| 163 | } |
Jayant Chowdhary | 620763f | 2022-05-27 05:37:14 +0000 | [diff] [blame] | 164 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 165 | return OK; |
| 166 | } |
| 167 | |
| 168 | template <typename TClientBase> |
| 169 | Camera2ClientBase<TClientBase>::~Camera2ClientBase() { |
| 170 | ATRACE_CALL(); |
| 171 | |
| 172 | TClientBase::mDestructionStarted = true; |
| 173 | |
| 174 | disconnect(); |
| 175 | |
Ravneet | c83d7c4 | 2022-06-03 20:51:58 +0000 | [diff] [blame] | 176 | if (mCameraServiceWatchdog != NULL) { |
| 177 | mCameraServiceWatchdog->requestExit(); |
| 178 | mCameraServiceWatchdog.clear(); |
| 179 | } |
| 180 | |
Jayant Chowdhary | 3ab499d | 2022-06-15 15:43:33 -0700 | [diff] [blame] | 181 | ALOGI("%s: Client object's dtor for Camera Id %s completed. Client was: %s (PID %d, UID %u)", |
| 182 | __FUNCTION__, TClientBase::mCameraIdStr.string(), |
Svetoslav Ganov | 280405a | 2015-05-12 02:19:27 +0000 | [diff] [blame] | 183 | String8(TClientBase::mClientPackageName).string(), |
Eino-Ville Talvala | b9d2f33 | 2014-09-18 17:24:22 -0700 | [diff] [blame] | 184 | mInitialClientPid, TClientBase::mClientUid); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | template <typename TClientBase> |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 188 | status_t Camera2ClientBase<TClientBase>::dumpClient(int fd, |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 189 | const Vector<String16>& args) { |
| 190 | String8 result; |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 191 | result.appendFormat("Camera2ClientBase[%s] (%p) PID: %d, dump:\n", |
| 192 | TClientBase::mCameraIdStr.string(), |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 193 | (TClientBase::getRemoteCallback() != NULL ? |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 194 | IInterface::asBinder(TClientBase::getRemoteCallback()).get() : NULL), |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 195 | TClientBase::mClientPid); |
| 196 | result.append(" State: "); |
| 197 | |
| 198 | write(fd, result.string(), result.size()); |
| 199 | // TODO: print dynamic/request section from most recent requests |
| 200 | |
| 201 | return dumpDevice(fd, args); |
| 202 | } |
| 203 | |
| 204 | template <typename TClientBase> |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 205 | status_t Camera2ClientBase<TClientBase>::startWatchingTags(const String8 &tags, int out) { |
| 206 | sp<CameraDeviceBase> device = mDevice; |
| 207 | if (!device) { |
| 208 | dprintf(out, " Device is detached"); |
| 209 | return OK; |
| 210 | } |
| 211 | |
| 212 | return device->startWatchingTags(tags); |
| 213 | } |
| 214 | |
| 215 | template <typename TClientBase> |
| 216 | status_t Camera2ClientBase<TClientBase>::stopWatchingTags(int out) { |
| 217 | sp<CameraDeviceBase> device = mDevice; |
| 218 | if (!device) { |
| 219 | dprintf(out, " Device is detached"); |
| 220 | return OK; |
| 221 | } |
| 222 | |
| 223 | return device->stopWatchingTags(); |
| 224 | } |
| 225 | |
| 226 | template <typename TClientBase> |
| 227 | status_t Camera2ClientBase<TClientBase>::dumpWatchedEventsToVector(std::vector<std::string> &out) { |
| 228 | sp<CameraDeviceBase> device = mDevice; |
| 229 | if (!device) { |
| 230 | // Nothing to dump if the device is detached |
| 231 | return OK; |
| 232 | } |
| 233 | return device->dumpWatchedEventsToVector(out); |
| 234 | } |
| 235 | |
| 236 | template <typename TClientBase> |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 237 | status_t Camera2ClientBase<TClientBase>::dumpDevice( |
| 238 | int fd, |
| 239 | const Vector<String16>& args) { |
| 240 | String8 result; |
| 241 | |
| 242 | result = " Device dump:\n"; |
| 243 | write(fd, result.string(), result.size()); |
| 244 | |
Yin-Chia Yeh | e5138f1 | 2017-11-10 14:10:05 -0800 | [diff] [blame] | 245 | sp<CameraDeviceBase> device = mDevice; |
| 246 | if (!device.get()) { |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 247 | result = " *** Device is detached\n"; |
| 248 | write(fd, result.string(), result.size()); |
| 249 | return NO_ERROR; |
| 250 | } |
| 251 | |
Yin-Chia Yeh | e5138f1 | 2017-11-10 14:10:05 -0800 | [diff] [blame] | 252 | status_t res = device->dump(fd, args); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 253 | if (res != OK) { |
| 254 | result = String8::format(" Error dumping device: %s (%d)", |
| 255 | strerror(-res), res); |
| 256 | write(fd, result.string(), result.size()); |
| 257 | } |
| 258 | |
| 259 | return NO_ERROR; |
| 260 | } |
| 261 | |
| 262 | // ICameraClient2BaseUser interface |
| 263 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 264 | template <typename TClientBase> |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 265 | binder::Status Camera2ClientBase<TClientBase>::disconnect() { |
Jayant Chowdhary | 3f20d19 | 2022-05-31 17:24:39 +0000 | [diff] [blame] | 266 | if (mCameraServiceWatchdog != nullptr) { |
| 267 | // Initialization from hal succeeded, time disconnect. |
| 268 | return mCameraServiceWatchdog->WATCH_CUSTOM_TIMER(disconnectImpl(), |
| 269 | kDisconnectTimeoutMs / kCycleLengthMs, kCycleLengthMs); |
| 270 | } |
| 271 | return disconnectImpl(); |
Jayant Chowdhary | 620763f | 2022-05-27 05:37:14 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | template <typename TClientBase> |
| 275 | binder::Status Camera2ClientBase<TClientBase>::disconnectImpl() { |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 276 | ATRACE_CALL(); |
Shuzhen Wang | 046f4a8 | 2022-05-05 17:15:24 -0700 | [diff] [blame] | 277 | ALOGD("Camera %s: start to disconnect", TClientBase::mCameraIdStr.string()); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 278 | Mutex::Autolock icl(mBinderSerializationLock); |
| 279 | |
Shuzhen Wang | 046f4a8 | 2022-05-05 17:15:24 -0700 | [diff] [blame] | 280 | ALOGD("Camera %s: serializationLock acquired", TClientBase::mCameraIdStr.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 281 | binder::Status res = binder::Status::ok(); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 282 | // Allow both client and the media server to disconnect at all times |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 283 | int callingPid = CameraThreadState::getCallingPid(); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 284 | if (callingPid != TClientBase::mClientPid && |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 285 | callingPid != TClientBase::mServicePid) return res; |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 286 | |
Shuzhen Wang | 046f4a8 | 2022-05-05 17:15:24 -0700 | [diff] [blame] | 287 | ALOGD("Camera %s: Shutting down", TClientBase::mCameraIdStr.string()); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 288 | |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 289 | // Before detaching the device, cache the info from current open session. |
| 290 | // The disconnected check avoids duplication of info and also prevents |
| 291 | // deadlock while acquiring service lock in cacheDump. |
| 292 | if (!TClientBase::mDisconnected) { |
Shuzhen Wang | 046f4a8 | 2022-05-05 17:15:24 -0700 | [diff] [blame] | 293 | ALOGD("Camera %s: start to cacheDump", TClientBase::mCameraIdStr.string()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 294 | Camera2ClientBase::getCameraService()->cacheDump(); |
| 295 | } |
| 296 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 297 | detachDevice(); |
| 298 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 299 | CameraService::BasicClient::disconnect(); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 300 | |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 301 | ALOGV("Camera %s: Shut down complete", TClientBase::mCameraIdStr.string()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 302 | |
| 303 | return res; |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | template <typename TClientBase> |
| 307 | void Camera2ClientBase<TClientBase>::detachDevice() { |
| 308 | if (mDevice == 0) return; |
| 309 | mDevice->disconnect(); |
| 310 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 311 | ALOGV("Camera %s: Detach complete", TClientBase::mCameraIdStr.string()); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | template <typename TClientBase> |
| 315 | status_t Camera2ClientBase<TClientBase>::connect( |
| 316 | const sp<TCamCallbacks>& client) { |
| 317 | ATRACE_CALL(); |
| 318 | ALOGV("%s: E", __FUNCTION__); |
| 319 | Mutex::Autolock icl(mBinderSerializationLock); |
| 320 | |
| 321 | if (TClientBase::mClientPid != 0 && |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 322 | CameraThreadState::getCallingPid() != TClientBase::mClientPid) { |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 323 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 324 | ALOGE("%s: Camera %s: Connection attempt from pid %d; " |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 325 | "current locked to pid %d", |
| 326 | __FUNCTION__, |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 327 | TClientBase::mCameraIdStr.string(), |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 328 | CameraThreadState::getCallingPid(), |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 329 | TClientBase::mClientPid); |
| 330 | return BAD_VALUE; |
| 331 | } |
| 332 | |
Jayant Chowdhary | 1236193 | 2018-08-27 14:46:13 -0700 | [diff] [blame] | 333 | TClientBase::mClientPid = CameraThreadState::getCallingPid(); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 334 | |
| 335 | TClientBase::mRemoteCallback = client; |
| 336 | mSharedCameraCallbacks = client; |
| 337 | |
| 338 | return OK; |
| 339 | } |
| 340 | |
| 341 | /** Device-related methods */ |
| 342 | |
| 343 | template <typename TClientBase> |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 344 | void Camera2ClientBase<TClientBase>::notifyError( |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 345 | int32_t errorCode, |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 346 | const CaptureResultExtras& resultExtras) { |
| 347 | ALOGE("Error condition %d reported by HAL, requestId %" PRId32, errorCode, |
| 348 | resultExtras.requestId); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | template <typename TClientBase> |
Austin Borger | 4a870a3 | 2022-02-25 01:48:41 +0000 | [diff] [blame] | 352 | status_t Camera2ClientBase<TClientBase>::notifyActive(float maxPreviewFps) { |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 353 | if (!mDeviceActive) { |
| 354 | status_t res = TClientBase::startCameraStreamingOps(); |
| 355 | if (res != OK) { |
| 356 | ALOGE("%s: Camera %s: Error starting camera streaming ops: %d", __FUNCTION__, |
| 357 | TClientBase::mCameraIdStr.string(), res); |
| 358 | return res; |
| 359 | } |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 360 | mCameraServiceProxyWrapper->logActive(TClientBase::mCameraIdStr, maxPreviewFps); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 361 | } |
| 362 | mDeviceActive = true; |
| 363 | |
| 364 | ALOGV("Camera device is now active"); |
| 365 | return OK; |
| 366 | } |
| 367 | |
| 368 | template <typename TClientBase> |
Shuzhen Wang | d26b186 | 2022-03-07 12:05:05 -0800 | [diff] [blame] | 369 | void Camera2ClientBase<TClientBase>::notifyIdleWithUserTag( |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 370 | int64_t requestCount, int64_t resultErrorCount, bool deviceError, |
Shuzhen Wang | d26b186 | 2022-03-07 12:05:05 -0800 | [diff] [blame] | 371 | const std::vector<hardware::CameraStreamStats>& streamStats, |
Shuzhen Wang | 9372b0b | 2022-05-11 18:55:19 -0700 | [diff] [blame] | 372 | const std::string& userTag, int videoStabilizationMode) { |
Eino-Ville Talvala | 412fe56 | 2015-08-20 17:08:32 -0700 | [diff] [blame] | 373 | if (mDeviceActive) { |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 374 | status_t res = TClientBase::finishCameraStreamingOps(); |
| 375 | if (res != OK) { |
| 376 | ALOGE("%s: Camera %s: Error finishing streaming ops: %d", __FUNCTION__, |
| 377 | TClientBase::mCameraIdStr.string(), res); |
| 378 | } |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 379 | mCameraServiceProxyWrapper->logIdle(TClientBase::mCameraIdStr, |
Shuzhen Wang | 9372b0b | 2022-05-11 18:55:19 -0700 | [diff] [blame] | 380 | requestCount, resultErrorCount, deviceError, userTag, videoStabilizationMode, |
| 381 | streamStats); |
Eino-Ville Talvala | 412fe56 | 2015-08-20 17:08:32 -0700 | [diff] [blame] | 382 | } |
| 383 | mDeviceActive = false; |
| 384 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 385 | ALOGV("Camera device is now idle"); |
| 386 | } |
| 387 | |
| 388 | template <typename TClientBase> |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 389 | void Camera2ClientBase<TClientBase>::notifyShutter(const CaptureResultExtras& resultExtras, |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 390 | nsecs_t timestamp) { |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 391 | (void)resultExtras; |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 392 | (void)timestamp; |
| 393 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 394 | ALOGV("%s: Shutter notification for request id %" PRId32 " at time %" PRId64, |
| 395 | __FUNCTION__, resultExtras.requestId, timestamp); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | template <typename TClientBase> |
| 399 | void Camera2ClientBase<TClientBase>::notifyAutoFocus(uint8_t newState, |
| 400 | int triggerId) { |
| 401 | (void)newState; |
| 402 | (void)triggerId; |
| 403 | |
| 404 | ALOGV("%s: Autofocus state now %d, last trigger %d", |
| 405 | __FUNCTION__, newState, triggerId); |
| 406 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | template <typename TClientBase> |
| 410 | void Camera2ClientBase<TClientBase>::notifyAutoExposure(uint8_t newState, |
| 411 | int triggerId) { |
| 412 | (void)newState; |
| 413 | (void)triggerId; |
| 414 | |
| 415 | ALOGV("%s: Autoexposure state now %d, last trigger %d", |
| 416 | __FUNCTION__, newState, triggerId); |
| 417 | } |
| 418 | |
| 419 | template <typename TClientBase> |
| 420 | void Camera2ClientBase<TClientBase>::notifyAutoWhitebalance(uint8_t newState, |
| 421 | int triggerId) { |
| 422 | (void)newState; |
| 423 | (void)triggerId; |
| 424 | |
| 425 | ALOGV("%s: Auto-whitebalance state now %d, last trigger %d", |
| 426 | __FUNCTION__, newState, triggerId); |
| 427 | } |
| 428 | |
| 429 | template <typename TClientBase> |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 430 | void Camera2ClientBase<TClientBase>::notifyPrepared(int streamId) { |
| 431 | (void)streamId; |
| 432 | |
| 433 | ALOGV("%s: Stream %d now prepared", |
| 434 | __FUNCTION__, streamId); |
| 435 | } |
| 436 | |
| 437 | template <typename TClientBase> |
Shuzhen Wang | 9d06601 | 2016-09-30 11:30:20 -0700 | [diff] [blame] | 438 | void Camera2ClientBase<TClientBase>::notifyRequestQueueEmpty() { |
| 439 | |
| 440 | ALOGV("%s: Request queue now empty", __FUNCTION__); |
| 441 | } |
| 442 | |
| 443 | template <typename TClientBase> |
Chien-Yu Chen | e8c535e | 2016-04-14 12:18:26 -0700 | [diff] [blame] | 444 | void Camera2ClientBase<TClientBase>::notifyRepeatingRequestError(long lastFrameNumber) { |
| 445 | (void)lastFrameNumber; |
| 446 | |
| 447 | ALOGV("%s: Repeating request was stopped. Last frame number is %ld", |
| 448 | __FUNCTION__, lastFrameNumber); |
| 449 | } |
| 450 | |
| 451 | template <typename TClientBase> |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 452 | int Camera2ClientBase<TClientBase>::getCameraId() const { |
Yin-Chia Yeh | c3e9d6f | 2018-02-06 10:56:32 -0800 | [diff] [blame] | 453 | return mApi1CameraId; |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | template <typename TClientBase> |
| 457 | const sp<CameraDeviceBase>& Camera2ClientBase<TClientBase>::getCameraDevice() { |
| 458 | return mDevice; |
| 459 | } |
| 460 | |
| 461 | template <typename TClientBase> |
| 462 | const sp<CameraService>& Camera2ClientBase<TClientBase>::getCameraService() { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 463 | return TClientBase::sCameraService; |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | template <typename TClientBase> |
| 467 | Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::Lock( |
| 468 | SharedCameraCallbacks &client) : |
| 469 | |
| 470 | mRemoteCallback(client.mRemoteCallback), |
| 471 | mSharedClient(client) { |
| 472 | |
| 473 | mSharedClient.mRemoteCallbackLock.lock(); |
| 474 | } |
| 475 | |
| 476 | template <typename TClientBase> |
| 477 | Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::~Lock() { |
| 478 | mSharedClient.mRemoteCallbackLock.unlock(); |
| 479 | } |
| 480 | |
| 481 | template <typename TClientBase> |
| 482 | Camera2ClientBase<TClientBase>::SharedCameraCallbacks::SharedCameraCallbacks( |
| 483 | const sp<TCamCallbacks>&client) : |
| 484 | |
| 485 | mRemoteCallback(client) { |
| 486 | } |
| 487 | |
| 488 | template <typename TClientBase> |
| 489 | typename Camera2ClientBase<TClientBase>::SharedCameraCallbacks& |
| 490 | Camera2ClientBase<TClientBase>::SharedCameraCallbacks::operator=( |
| 491 | const sp<TCamCallbacks>&client) { |
| 492 | |
| 493 | Mutex::Autolock l(mRemoteCallbackLock); |
| 494 | mRemoteCallback = client; |
| 495 | return *this; |
| 496 | } |
| 497 | |
| 498 | template <typename TClientBase> |
| 499 | void Camera2ClientBase<TClientBase>::SharedCameraCallbacks::clear() { |
| 500 | Mutex::Autolock l(mRemoteCallbackLock); |
| 501 | mRemoteCallback.clear(); |
| 502 | } |
| 503 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 504 | template <typename TClientBase> |
| 505 | status_t Camera2ClientBase<TClientBase>::injectCamera(const String8& injectedCamId, |
| 506 | sp<CameraProviderManager> manager) { |
| 507 | return mDevice->injectCamera(injectedCamId, manager); |
| 508 | } |
| 509 | |
| 510 | template <typename TClientBase> |
| 511 | status_t Camera2ClientBase<TClientBase>::stopInjection() { |
| 512 | return mDevice->stopInjection(); |
| 513 | } |
| 514 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 515 | template class Camera2ClientBase<CameraService::Client>; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 516 | template class Camera2ClientBase<CameraDeviceClientBase>; |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 517 | |
| 518 | } // namespace android |