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