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 | |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 30 | #include <android/hardware/ICameraService.h> |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 31 | #include <camera/CameraSessionStats.h> |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 32 | #include <camera/StringUtils.h> |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 33 | #include <com_android_window_flags.h> |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 34 | |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 35 | #include "common/Camera2ClientBase.h" |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 36 | |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 37 | #include "api2/CameraDeviceClient.h" |
| 38 | |
Eino-Ville Talvala | d309fb9 | 2015-11-25 12:12:45 -0800 | [diff] [blame] | 39 | #include "device3/Camera3Device.h" |
Jayant Chowdhary | 35642f2 | 2022-01-08 00:39:39 +0000 | [diff] [blame] | 40 | #include "device3/aidl/AidlCamera3Device.h" |
Jayant Chowdhary | 22441f3 | 2021-12-26 18:35:41 -0800 | [diff] [blame] | 41 | #include "device3/hidl/HidlCamera3Device.h" |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 42 | |
| 43 | namespace android { |
Jayant Chowdhary | 620763f | 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 | |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 47 | namespace wm_flags = com::android::window::flags; |
| 48 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 49 | // Interface used by CameraService |
| 50 | |
| 51 | template <typename TClientBase> |
| 52 | Camera2ClientBase<TClientBase>::Camera2ClientBase( |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 53 | const sp<CameraService>& cameraService, const sp<TCamCallbacks>& remoteCallback, |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 54 | std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper, |
Austin Borger | 249e659 | 2024-03-10 22:28:11 -0700 | [diff] [blame] | 55 | std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils, |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 56 | const AttributionSourceState& clientAttribution, int callingPid, bool systemNativeClient, |
| 57 | const std::string& cameraId, int api1CameraId, int cameraFacing, int sensorOrientation, |
| 58 | int servicePid, bool overrideForPerfClass, int rotationOverride, bool legacyClient) |
| 59 | : TClientBase(cameraService, remoteCallback, attributionAndPermissionUtils, clientAttribution, |
| 60 | callingPid, systemNativeClient, cameraId, api1CameraId, cameraFacing, |
| 61 | sensorOrientation, servicePid, rotationOverride), |
| 62 | mSharedCameraCallbacks(remoteCallback), |
| 63 | mCameraServiceProxyWrapper(cameraServiceProxyWrapper), |
| 64 | mDeviceActive(false), |
| 65 | mApi1CameraId(api1CameraId) { |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 66 | ALOGI("Camera %s: Opened. Client: %s (PID %d, UID %d)", cameraId.c_str(), |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 67 | TClientBase::getPackageName().c_str(), TClientBase::mCallingPid, |
| 68 | TClientBase::getClientUid()); |
Igor Murashkin | 98e2472 | 2013-06-19 19:51:04 -0700 | [diff] [blame] | 69 | |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 70 | mInitialClientPid = TClientBase::mCallingPid; |
Jayant Chowdhary | 22441f3 | 2021-12-26 18:35:41 -0800 | [diff] [blame] | 71 | mOverrideForPerfClass = overrideForPerfClass; |
| 72 | mLegacyClient = legacyClient; |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | template <typename TClientBase> |
| 76 | status_t Camera2ClientBase<TClientBase>::checkPid(const char* checkLocation) |
| 77 | const { |
| 78 | |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 79 | int callingPid = TClientBase::getCallingPid(); |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 80 | if (callingPid == TClientBase::mCallingPid) return NO_ERROR; |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 81 | |
| 82 | ALOGE("%s: attempt to use a locked camera from a different process" |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 83 | " (old pid %d, new pid %d)", checkLocation, TClientBase::mCallingPid, callingPid); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 84 | return PERMISSION_DENIED; |
| 85 | } |
| 86 | |
| 87 | template <typename TClientBase> |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 88 | status_t Camera2ClientBase<TClientBase>::initialize(sp<CameraProviderManager> manager, |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 89 | const std::string& monitorTags) { |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 90 | return initializeImpl(manager, monitorTags); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | template <typename TClientBase> |
| 94 | template <typename TProviderPtr> |
Emilian Peev | bd8c503 | 2018-02-14 23:05:40 +0000 | [diff] [blame] | 95 | status_t Camera2ClientBase<TClientBase>::initializeImpl(TProviderPtr providerPtr, |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 96 | const std::string& monitorTags) { |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 97 | ATRACE_CALL(); |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 98 | ALOGV("%s: Initializing client for camera %s", __FUNCTION__, |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 99 | TClientBase::mCameraIdStr.c_str()); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 100 | status_t res; |
| 101 | |
Jayant Chowdhary | 22441f3 | 2021-12-26 18:35:41 -0800 | [diff] [blame] | 102 | IPCTransport providerTransport = IPCTransport::INVALID; |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 103 | res = providerPtr->getCameraIdIPCTransport(TClientBase::mCameraIdStr, |
Jayant Chowdhary | 22441f3 | 2021-12-26 18:35:41 -0800 | [diff] [blame] | 104 | &providerTransport); |
| 105 | if (res != OK) { |
| 106 | return res; |
| 107 | } |
| 108 | switch (providerTransport) { |
| 109 | case IPCTransport::HIDL: |
| 110 | mDevice = |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 111 | new HidlCamera3Device(mCameraServiceProxyWrapper, |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 112 | TClientBase::mAttributionAndPermissionUtils, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 113 | TClientBase::mCameraIdStr, mOverrideForPerfClass, |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 114 | TClientBase::mRotationOverride, mLegacyClient); |
Jayant Chowdhary | 22441f3 | 2021-12-26 18:35:41 -0800 | [diff] [blame] | 115 | break; |
| 116 | case IPCTransport::AIDL: |
Jayant Chowdhary | 35642f2 | 2022-01-08 00:39:39 +0000 | [diff] [blame] | 117 | mDevice = |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 118 | new AidlCamera3Device(mCameraServiceProxyWrapper, |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 119 | TClientBase::mAttributionAndPermissionUtils, |
Austin Borger | 18b30a7 | 2022-10-27 12:20:29 -0700 | [diff] [blame] | 120 | TClientBase::mCameraIdStr, mOverrideForPerfClass, |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 121 | TClientBase::mRotationOverride, mLegacyClient); |
Jayant Chowdhary | 35642f2 | 2022-01-08 00:39:39 +0000 | [diff] [blame] | 122 | break; |
Jayant Chowdhary | 22441f3 | 2021-12-26 18:35:41 -0800 | [diff] [blame] | 123 | default: |
| 124 | ALOGE("%s Invalid transport for camera id %s", __FUNCTION__, |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 125 | TClientBase::mCameraIdStr.c_str()); |
Jayant Chowdhary | 22441f3 | 2021-12-26 18:35:41 -0800 | [diff] [blame] | 126 | return NO_INIT; |
| 127 | } |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 128 | if (mDevice == NULL) { |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 129 | ALOGE("%s: Camera %s: No device connected", |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 130 | __FUNCTION__, TClientBase::mCameraIdStr.c_str()); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 131 | return NO_INIT; |
| 132 | } |
| 133 | |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame^] | 134 | // Notify camera opening (check op if check_full_attribution_source_chain flag is off). |
| 135 | res = TClientBase::notifyCameraOpening(); |
Emilian Peev | 510546a | 2024-03-07 01:21:28 +0000 | [diff] [blame] | 136 | if (res != OK) { |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame^] | 137 | TClientBase::notifyCameraClosing(); |
Emilian Peev | 510546a | 2024-03-07 01:21:28 +0000 | [diff] [blame] | 138 | return res; |
| 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)", |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 144 | __FUNCTION__, TClientBase::mCameraIdStr.c_str(), strerror(-res), res); |
Austin Borger | a0c61f1 | 2024-10-11 13:38:35 -0700 | [diff] [blame^] | 145 | TClientBase::notifyCameraClosing(); |
Emilian Peev | fa17f09 | 2022-07-29 13:28:55 -0700 | [diff] [blame] | 146 | return res; |
| 147 | } |
| 148 | |
Yin-Chia Yeh | 5fd603e | 2019-11-20 11:22:27 -0800 | [diff] [blame] | 149 | wp<NotificationListener> weakThis(this); |
Yin-Chia Yeh | e1c8063 | 2016-08-08 14:48:05 -0700 | [diff] [blame] | 150 | res = mDevice->setNotifyCallback(weakThis); |
Austin Borger | 7b12954 | 2022-06-09 13:23:06 -0700 | [diff] [blame] | 151 | if (res != OK) { |
| 152 | ALOGE("%s: Camera %s: Unable to set notify callback: %s (%d)", |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 153 | __FUNCTION__, TClientBase::mCameraIdStr.c_str(), strerror(-res), res); |
Austin Borger | 7b12954 | 2022-06-09 13:23:06 -0700 | [diff] [blame] | 154 | return res; |
| 155 | } |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 156 | |
| 157 | return OK; |
| 158 | } |
| 159 | |
| 160 | template <typename TClientBase> |
| 161 | Camera2ClientBase<TClientBase>::~Camera2ClientBase() { |
| 162 | ATRACE_CALL(); |
| 163 | |
| 164 | TClientBase::mDestructionStarted = true; |
| 165 | |
| 166 | disconnect(); |
| 167 | |
Jayant Chowdhary | 3ab499d | 2022-06-15 15:43:33 -0700 | [diff] [blame] | 168 | ALOGI("%s: Client object's dtor for Camera Id %s completed. Client was: %s (PID %d, UID %u)", |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 169 | __FUNCTION__, TClientBase::mCameraIdStr.c_str(), TClientBase::getPackageName().c_str(), |
| 170 | mInitialClientPid, TClientBase::getClientUid()); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | template <typename TClientBase> |
Eino-Ville Talvala | c400396 | 2016-01-13 10:07:04 -0800 | [diff] [blame] | 174 | status_t Camera2ClientBase<TClientBase>::dumpClient(int fd, |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 175 | const Vector<String16>& args) { |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 176 | std::string result; |
| 177 | result += fmt::sprintf("Camera2ClientBase[%s] (%p) PID: %d, dump:\n", |
| 178 | TClientBase::mCameraIdStr.c_str(), |
Eino-Ville Talvala | e992e75 | 2014-11-07 16:17:48 -0800 | [diff] [blame] | 179 | (TClientBase::getRemoteCallback() != NULL ? |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 180 | (void *)IInterface::asBinder(TClientBase::getRemoteCallback()).get() : NULL), |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 181 | TClientBase::mCallingPid); |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 182 | result += " State: "; |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 183 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 184 | write(fd, result.c_str(), result.size()); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 185 | // TODO: print dynamic/request section from most recent requests |
| 186 | |
| 187 | return dumpDevice(fd, args); |
| 188 | } |
| 189 | |
| 190 | template <typename TClientBase> |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 191 | status_t Camera2ClientBase<TClientBase>::startWatchingTags(const std::string &tags, int out) { |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 192 | sp<CameraDeviceBase> device = mDevice; |
| 193 | if (!device) { |
| 194 | dprintf(out, " Device is detached"); |
| 195 | return OK; |
| 196 | } |
| 197 | |
| 198 | return device->startWatchingTags(tags); |
| 199 | } |
| 200 | |
| 201 | template <typename TClientBase> |
| 202 | status_t Camera2ClientBase<TClientBase>::stopWatchingTags(int out) { |
| 203 | sp<CameraDeviceBase> device = mDevice; |
| 204 | if (!device) { |
| 205 | dprintf(out, " Device is detached"); |
| 206 | return OK; |
| 207 | } |
| 208 | |
| 209 | return device->stopWatchingTags(); |
| 210 | } |
| 211 | |
| 212 | template <typename TClientBase> |
| 213 | status_t Camera2ClientBase<TClientBase>::dumpWatchedEventsToVector(std::vector<std::string> &out) { |
| 214 | sp<CameraDeviceBase> device = mDevice; |
| 215 | if (!device) { |
| 216 | // Nothing to dump if the device is detached |
| 217 | return OK; |
| 218 | } |
| 219 | return device->dumpWatchedEventsToVector(out); |
| 220 | } |
| 221 | |
| 222 | template <typename TClientBase> |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 223 | status_t Camera2ClientBase<TClientBase>::dumpDevice( |
| 224 | int fd, |
| 225 | const Vector<String16>& args) { |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 226 | std::string result; |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 227 | |
| 228 | result = " Device dump:\n"; |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 229 | write(fd, result.c_str(), result.size()); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 230 | |
Yin-Chia Yeh | e5138f1 | 2017-11-10 14:10:05 -0800 | [diff] [blame] | 231 | sp<CameraDeviceBase> device = mDevice; |
| 232 | if (!device.get()) { |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 233 | result = " *** Device is detached\n"; |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 234 | write(fd, result.c_str(), result.size()); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 235 | return NO_ERROR; |
| 236 | } |
| 237 | |
Yin-Chia Yeh | e5138f1 | 2017-11-10 14:10:05 -0800 | [diff] [blame] | 238 | status_t res = device->dump(fd, args); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 239 | if (res != OK) { |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 240 | result = fmt::sprintf(" Error dumping device: %s (%d)", |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 241 | strerror(-res), res); |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 242 | write(fd, result.c_str(), result.size()); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | return NO_ERROR; |
| 246 | } |
| 247 | |
| 248 | // ICameraClient2BaseUser interface |
| 249 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 250 | template <typename TClientBase> |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 251 | binder::Status Camera2ClientBase<TClientBase>::disconnect() { |
Ravneet Dhanjal | baa324b | 2023-10-11 01:19:18 +0000 | [diff] [blame] | 252 | |
Jayant Chowdhary | 3f20d19 | 2022-05-31 17:24:39 +0000 | [diff] [blame] | 253 | return disconnectImpl(); |
Jayant Chowdhary | 620763f | 2022-05-27 05:37:14 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | template <typename TClientBase> |
| 257 | binder::Status Camera2ClientBase<TClientBase>::disconnectImpl() { |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 258 | ATRACE_CALL(); |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 259 | ALOGD("Camera %s: start to disconnect", TClientBase::mCameraIdStr.c_str()); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 260 | Mutex::Autolock icl(mBinderSerializationLock); |
| 261 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 262 | ALOGD("Camera %s: serializationLock acquired", TClientBase::mCameraIdStr.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 263 | binder::Status res = binder::Status::ok(); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 264 | // Allow both client and the media server to disconnect at all times |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 265 | int callingPid = TClientBase::getCallingPid(); |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 266 | if (callingPid != TClientBase::mCallingPid && |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 267 | callingPid != TClientBase::mServicePid) return res; |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 268 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 269 | ALOGD("Camera %s: Shutting down", TClientBase::mCameraIdStr.c_str()); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 270 | |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 271 | // Before detaching the device, cache the info from current open session. |
| 272 | // The disconnected check avoids duplication of info and also prevents |
| 273 | // deadlock while acquiring service lock in cacheDump. |
| 274 | if (!TClientBase::mDisconnected) { |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 275 | ALOGD("Camera %s: start to cacheDump", TClientBase::mCameraIdStr.c_str()); |
Rucha Katakwar | df22307 | 2021-06-15 10:21:00 -0700 | [diff] [blame] | 276 | Camera2ClientBase::getCameraService()->cacheDump(); |
| 277 | } |
| 278 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 279 | detachDevice(); |
| 280 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 281 | CameraService::BasicClient::disconnect(); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 282 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 283 | ALOGV("Camera %s: Shut down complete", TClientBase::mCameraIdStr.c_str()); |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 284 | |
| 285 | return res; |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | template <typename TClientBase> |
| 289 | void Camera2ClientBase<TClientBase>::detachDevice() { |
| 290 | if (mDevice == 0) return; |
| 291 | mDevice->disconnect(); |
| 292 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 293 | ALOGV("Camera %s: Detach complete", TClientBase::mCameraIdStr.c_str()); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | template <typename TClientBase> |
| 297 | status_t Camera2ClientBase<TClientBase>::connect( |
| 298 | const sp<TCamCallbacks>& client) { |
| 299 | ATRACE_CALL(); |
| 300 | ALOGV("%s: E", __FUNCTION__); |
| 301 | Mutex::Autolock icl(mBinderSerializationLock); |
| 302 | |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 303 | if (TClientBase::mCallingPid != 0 && |
| 304 | TClientBase::getCallingPid() != TClientBase::mCallingPid) { |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 305 | |
Eino-Ville Talvala | 2f09bac | 2016-12-13 11:29:54 -0800 | [diff] [blame] | 306 | ALOGE("%s: Camera %s: Connection attempt from pid %d; " |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 307 | "current locked to pid %d", |
| 308 | __FUNCTION__, |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 309 | TClientBase::mCameraIdStr.c_str(), |
Austin Borger | 22c5c85 | 2024-03-08 13:31:36 -0800 | [diff] [blame] | 310 | TClientBase::getCallingPid(), |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 311 | TClientBase::mCallingPid); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 312 | return BAD_VALUE; |
| 313 | } |
| 314 | |
Austin Borger | 2e772b8 | 2024-10-11 16:09:57 -0700 | [diff] [blame] | 315 | TClientBase::mCallingPid = TClientBase::getCallingPid(); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 316 | |
| 317 | TClientBase::mRemoteCallback = client; |
| 318 | mSharedCameraCallbacks = client; |
| 319 | |
| 320 | return OK; |
| 321 | } |
| 322 | |
| 323 | /** Device-related methods */ |
| 324 | |
| 325 | template <typename TClientBase> |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 326 | void Camera2ClientBase<TClientBase>::notifyError( |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 327 | int32_t errorCode, |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 328 | const CaptureResultExtras& resultExtras) { |
| 329 | ALOGE("Error condition %d reported by HAL, requestId %" PRId32, errorCode, |
| 330 | resultExtras.requestId); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | template <typename TClientBase> |
Austin Borger | c809976 | 2023-01-12 17:08:46 -0800 | [diff] [blame] | 334 | void Camera2ClientBase<TClientBase>::notifyPhysicalCameraChange(const std::string &physicalId) { |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 335 | using android::hardware::ICameraService; |
| 336 | // We're only interested in this notification if rotationOverride is turned on. |
| 337 | if (TClientBase::mRotationOverride == ICameraService::ROTATION_OVERRIDE_NONE) { |
Austin Borger | c809976 | 2023-01-12 17:08:46 -0800 | [diff] [blame] | 338 | return; |
| 339 | } |
| 340 | |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 341 | auto physicalCameraMetadata = mDevice->infoPhysical(physicalId); |
Austin Borger | c809976 | 2023-01-12 17:08:46 -0800 | [diff] [blame] | 342 | auto orientationEntry = physicalCameraMetadata.find(ANDROID_SENSOR_ORIENTATION); |
| 343 | |
| 344 | if (orientationEntry.count == 1) { |
| 345 | int orientation = orientationEntry.data.i32[0]; |
| 346 | int rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_NONE; |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 347 | bool landscapeSensor = (orientation == 0 || orientation == 180); |
| 348 | if (((TClientBase::mRotationOverride == |
| 349 | ICameraService::ROTATION_OVERRIDE_OVERRIDE_TO_PORTRAIT) && landscapeSensor) || |
Mina Granic | d19814a | 2024-06-18 10:08:58 +0000 | [diff] [blame] | 350 | ((wm_flags::enable_camera_compat_for_desktop_windowing() && |
Jayant Chowdhary | 81d81b0 | 2024-02-15 19:13:39 +0000 | [diff] [blame] | 351 | TClientBase::mRotationOverride == |
| 352 | ICameraService::ROTATION_OVERRIDE_ROTATION_ONLY) |
| 353 | && !landscapeSensor)) { |
Austin Borger | c809976 | 2023-01-12 17:08:46 -0800 | [diff] [blame] | 354 | rotateAndCropMode = ANDROID_SCALER_ROTATE_AND_CROP_90; |
| 355 | } |
| 356 | |
Jayant Chowdhary | 44d5f62 | 2023-09-20 03:11:41 +0000 | [diff] [blame] | 357 | static_cast<TClientBase *>(this)->setRotateAndCropOverride(rotateAndCropMode, |
| 358 | /*fromHal*/ true); |
Austin Borger | c809976 | 2023-01-12 17:08:46 -0800 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
| 362 | template <typename TClientBase> |
Austin Borger | 4a870a3 | 2022-02-25 01:48:41 +0000 | [diff] [blame] | 363 | status_t Camera2ClientBase<TClientBase>::notifyActive(float maxPreviewFps) { |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 364 | if (!mDeviceActive) { |
| 365 | status_t res = TClientBase::startCameraStreamingOps(); |
| 366 | if (res != OK) { |
| 367 | ALOGE("%s: Camera %s: Error starting camera streaming ops: %d", __FUNCTION__, |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 368 | TClientBase::mCameraIdStr.c_str(), res); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 369 | return res; |
| 370 | } |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 371 | mCameraServiceProxyWrapper->logActive(TClientBase::mCameraIdStr, maxPreviewFps); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 372 | } |
| 373 | mDeviceActive = true; |
| 374 | |
| 375 | ALOGV("Camera device is now active"); |
| 376 | return OK; |
| 377 | } |
| 378 | |
| 379 | template <typename TClientBase> |
Shuzhen Wang | d26b186 | 2022-03-07 12:05:05 -0800 | [diff] [blame] | 380 | void Camera2ClientBase<TClientBase>::notifyIdleWithUserTag( |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 381 | int64_t requestCount, int64_t resultErrorCount, bool deviceError, |
Eino-Ville Talvala | ffc186b | 2024-04-09 18:10:47 -0700 | [diff] [blame] | 382 | std::pair<int32_t, int32_t> mostRequestedFpsRange, |
Shuzhen Wang | d26b186 | 2022-03-07 12:05:05 -0800 | [diff] [blame] | 383 | const std::vector<hardware::CameraStreamStats>& streamStats, |
Shuzhen Wang | 6e08d20 | 2023-10-24 20:27:14 +0000 | [diff] [blame] | 384 | const std::string& userTag, int videoStabilizationMode, bool usedUltraWide, |
| 385 | bool usedZoomOverride) { |
Eino-Ville Talvala | 412fe56 | 2015-08-20 17:08:32 -0700 | [diff] [blame] | 386 | if (mDeviceActive) { |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 387 | status_t res = TClientBase::finishCameraStreamingOps(); |
| 388 | if (res != OK) { |
| 389 | ALOGE("%s: Camera %s: Error finishing streaming ops: %d", __FUNCTION__, |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 390 | TClientBase::mCameraIdStr.c_str(), res); |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 391 | } |
Austin Borger | 74fca04 | 2022-05-23 12:41:21 -0700 | [diff] [blame] | 392 | mCameraServiceProxyWrapper->logIdle(TClientBase::mCameraIdStr, |
Shuzhen Wang | 9372b0b | 2022-05-11 18:55:19 -0700 | [diff] [blame] | 393 | requestCount, resultErrorCount, deviceError, userTag, videoStabilizationMode, |
Eino-Ville Talvala | ffc186b | 2024-04-09 18:10:47 -0700 | [diff] [blame] | 394 | usedUltraWide, usedZoomOverride, mostRequestedFpsRange, streamStats); |
Eino-Ville Talvala | 412fe56 | 2015-08-20 17:08:32 -0700 | [diff] [blame] | 395 | } |
| 396 | mDeviceActive = false; |
| 397 | |
Eino-Ville Talvala | f1e98d8 | 2013-09-06 09:32:43 -0700 | [diff] [blame] | 398 | ALOGV("Camera device is now idle"); |
| 399 | } |
| 400 | |
| 401 | template <typename TClientBase> |
Jing Mike | c7f9b13 | 2023-03-12 11:12:04 +0800 | [diff] [blame] | 402 | void Camera2ClientBase<TClientBase>::notifyShutter( |
| 403 | [[maybe_unused]] const CaptureResultExtras& resultExtras, |
| 404 | [[maybe_unused]] nsecs_t timestamp) { |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 405 | ALOGV("%s: Shutter notification for request id %" PRId32 " at time %" PRId64, |
| 406 | __FUNCTION__, resultExtras.requestId, timestamp); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | template <typename TClientBase> |
Jing Mike | c7f9b13 | 2023-03-12 11:12:04 +0800 | [diff] [blame] | 410 | void Camera2ClientBase<TClientBase>::notifyAutoFocus([[maybe_unused]] uint8_t newState, |
| 411 | [[maybe_unused]] int triggerId) { |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 412 | ALOGV("%s: Autofocus state now %d, last trigger %d", |
| 413 | __FUNCTION__, newState, triggerId); |
| 414 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | template <typename TClientBase> |
Jing Mike | c7f9b13 | 2023-03-12 11:12:04 +0800 | [diff] [blame] | 418 | void Camera2ClientBase<TClientBase>::notifyAutoExposure([[maybe_unused]] uint8_t newState, |
| 419 | [[maybe_unused]] int triggerId) { |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 420 | ALOGV("%s: Autoexposure state now %d, last trigger %d", |
| 421 | __FUNCTION__, newState, triggerId); |
| 422 | } |
| 423 | |
| 424 | template <typename TClientBase> |
Jing Mike | c7f9b13 | 2023-03-12 11:12:04 +0800 | [diff] [blame] | 425 | void Camera2ClientBase<TClientBase>::notifyAutoWhitebalance( |
| 426 | [[maybe_unused]] uint8_t newState, |
| 427 | [[maybe_unused]] int triggerId) { |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 428 | ALOGV("%s: Auto-whitebalance state now %d, last trigger %d", |
| 429 | __FUNCTION__, newState, triggerId); |
| 430 | } |
| 431 | |
| 432 | template <typename TClientBase> |
Jing Mike | c7f9b13 | 2023-03-12 11:12:04 +0800 | [diff] [blame] | 433 | void Camera2ClientBase<TClientBase>::notifyPrepared([[maybe_unused]] int streamId) { |
Eino-Ville Talvala | 4d44cad | 2015-04-11 13:15:45 -0700 | [diff] [blame] | 434 | ALOGV("%s: Stream %d now prepared", |
| 435 | __FUNCTION__, streamId); |
| 436 | } |
| 437 | |
| 438 | template <typename TClientBase> |
Shuzhen Wang | 9d06601 | 2016-09-30 11:30:20 -0700 | [diff] [blame] | 439 | void Camera2ClientBase<TClientBase>::notifyRequestQueueEmpty() { |
| 440 | |
| 441 | ALOGV("%s: Request queue now empty", __FUNCTION__); |
| 442 | } |
| 443 | |
| 444 | template <typename TClientBase> |
Jing Mike | c7f9b13 | 2023-03-12 11:12:04 +0800 | [diff] [blame] | 445 | void Camera2ClientBase<TClientBase>::notifyRepeatingRequestError( |
| 446 | [[maybe_unused]] long lastFrameNumber) { |
Chien-Yu Chen | e8c535e | 2016-04-14 12:18:26 -0700 | [diff] [blame] | 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> |
Austin Borger | 1c1bee0 | 2023-06-01 16:51:35 -0700 | [diff] [blame] | 505 | status_t Camera2ClientBase<TClientBase>::injectCamera(const std::string& injectedCamId, |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 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 | |
malikakash | 22af94c | 2023-12-04 18:13:14 +0000 | [diff] [blame] | 515 | template <typename TClientBase> |
| 516 | status_t Camera2ClientBase<TClientBase>::injectSessionParams( |
| 517 | const CameraMetadata& sessionParams) { |
| 518 | return mDevice->injectSessionParams(sessionParams); |
| 519 | } |
| 520 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 521 | template class Camera2ClientBase<CameraService::Client>; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 522 | template class Camera2ClientBase<CameraDeviceClientBase>; |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 523 | |
| 524 | } // namespace android |