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