Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 1 | /* |
Emilian Peev | d99c8ae | 2019-11-26 13:19:13 -0800 | [diff] [blame] | 2 | * Copyright (C) 2019 The Android Open Source Project |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 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 "CameraOfflineClient" |
| 18 | #define ATRACE_TAG ATRACE_TAG_CAMERA |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
| 21 | #include "CameraOfflineSessionClient.h" |
Emilian Peev | d99c8ae | 2019-11-26 13:19:13 -0800 | [diff] [blame] | 22 | #include "utils/CameraThreadState.h" |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 23 | #include <utils/Trace.h> |
| 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | using binder::Status; |
| 28 | |
| 29 | status_t CameraOfflineSessionClient::initialize(sp<CameraProviderManager>, const String8&) { |
Emilian Peev | d99c8ae | 2019-11-26 13:19:13 -0800 | [diff] [blame] | 30 | ATRACE_CALL(); |
| 31 | |
Emilian Peev | 886ac21 | 2023-02-07 14:10:24 -0800 | [diff] [blame] | 32 | if (mFrameProcessor.get() != nullptr) { |
| 33 | // Already initialized |
| 34 | return OK; |
| 35 | } |
| 36 | |
Emilian Peev | d99c8ae | 2019-11-26 13:19:13 -0800 | [diff] [blame] | 37 | // Verify ops permissions |
| 38 | auto res = startCameraOps(); |
| 39 | if (res != OK) { |
| 40 | return res; |
| 41 | } |
| 42 | |
| 43 | if (mOfflineSession.get() == nullptr) { |
| 44 | ALOGE("%s: Camera %s: No valid offline session", |
| 45 | __FUNCTION__, mCameraIdStr.string()); |
| 46 | return NO_INIT; |
| 47 | } |
| 48 | |
Emilian Peev | faa4bde | 2020-01-23 12:19:37 -0800 | [diff] [blame] | 49 | String8 threadName; |
| 50 | mFrameProcessor = new camera2::FrameProcessorBase(mOfflineSession); |
| 51 | threadName = String8::format("Offline-%s-FrameProc", mCameraIdStr.string()); |
Austin Borger | 7b12954 | 2022-06-09 13:23:06 -0700 | [diff] [blame] | 52 | res = mFrameProcessor->run(threadName.string()); |
| 53 | if (res != OK) { |
| 54 | ALOGE("%s: Unable to start frame processor thread: %s (%d)", |
| 55 | __FUNCTION__, strerror(-res), res); |
| 56 | return res; |
| 57 | } |
Emilian Peev | faa4bde | 2020-01-23 12:19:37 -0800 | [diff] [blame] | 58 | |
| 59 | mFrameProcessor->registerListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID, |
| 60 | camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID, |
| 61 | /*listener*/this, |
| 62 | /*sendPartials*/true); |
| 63 | |
Emilian Peev | d99c8ae | 2019-11-26 13:19:13 -0800 | [diff] [blame] | 64 | wp<NotificationListener> weakThis(this); |
| 65 | res = mOfflineSession->initialize(weakThis); |
| 66 | if (res != OK) { |
| 67 | ALOGE("%s: Camera %s: unable to initialize device: %s (%d)", |
| 68 | __FUNCTION__, mCameraIdStr.string(), strerror(-res), res); |
| 69 | return res; |
| 70 | } |
| 71 | |
Emilian Peev | c0fe54c | 2020-03-11 14:05:07 -0700 | [diff] [blame] | 72 | for (size_t i = 0; i < mCompositeStreamMap.size(); i++) { |
| 73 | mCompositeStreamMap.valueAt(i)->switchToOffline(); |
| 74 | } |
| 75 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 76 | return OK; |
| 77 | } |
| 78 | |
Ravneet | aeb20dc | 2022-03-30 05:33:03 +0000 | [diff] [blame] | 79 | status_t CameraOfflineSessionClient::setCameraServiceWatchdog(bool) { |
| 80 | return OK; |
| 81 | } |
| 82 | |
Eino-Ville Talvala | f2e3709 | 2020-01-07 15:32:32 -0800 | [diff] [blame] | 83 | status_t CameraOfflineSessionClient::setRotateAndCropOverride(uint8_t /*rotateAndCrop*/) { |
| 84 | // Since we're not submitting more capture requests, changes to rotateAndCrop override |
| 85 | // make no difference. |
| 86 | return OK; |
| 87 | } |
| 88 | |
Bharatt Kukreja | 7146ced | 2022-10-25 15:45:29 +0000 | [diff] [blame] | 89 | status_t CameraOfflineSessionClient::setAutoframingOverride(uint8_t) { |
| 90 | return OK; |
| 91 | } |
| 92 | |
Eino-Ville Talvala | 305cec6 | 2020-11-12 14:18:17 -0800 | [diff] [blame] | 93 | bool CameraOfflineSessionClient::supportsCameraMute() { |
| 94 | // Offline mode doesn't support muting |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | status_t CameraOfflineSessionClient::setCameraMute(bool) { |
| 99 | return INVALID_OPERATION; |
| 100 | } |
| 101 | |
Shuzhen Wang | 16610a6 | 2022-12-15 22:38:07 -0800 | [diff] [blame] | 102 | void CameraOfflineSessionClient::setStreamUseCaseOverrides( |
| 103 | const std::vector<int64_t>& /*useCaseOverrides*/) { |
| 104 | } |
| 105 | |
| 106 | void CameraOfflineSessionClient::clearStreamUseCaseOverrides() { |
| 107 | } |
| 108 | |
Emilian Peev | d99c8ae | 2019-11-26 13:19:13 -0800 | [diff] [blame] | 109 | status_t CameraOfflineSessionClient::dump(int fd, const Vector<String16>& args) { |
| 110 | return BasicClient::dump(fd, args); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Emilian Peev | faa4bde | 2020-01-23 12:19:37 -0800 | [diff] [blame] | 113 | status_t CameraOfflineSessionClient::dumpClient(int fd, const Vector<String16>& args) { |
Emilian Peev | d99c8ae | 2019-11-26 13:19:13 -0800 | [diff] [blame] | 114 | String8 result; |
| 115 | |
| 116 | result = " Offline session dump:\n"; |
| 117 | write(fd, result.string(), result.size()); |
| 118 | |
| 119 | if (mOfflineSession.get() == nullptr) { |
| 120 | result = " *** Offline session is detached\n"; |
| 121 | write(fd, result.string(), result.size()); |
| 122 | return NO_ERROR; |
| 123 | } |
| 124 | |
Emilian Peev | faa4bde | 2020-01-23 12:19:37 -0800 | [diff] [blame] | 125 | mFrameProcessor->dump(fd, args); |
| 126 | |
Emilian Peev | d99c8ae | 2019-11-26 13:19:13 -0800 | [diff] [blame] | 127 | auto res = mOfflineSession->dump(fd); |
| 128 | if (res != OK) { |
| 129 | result = String8::format(" Error dumping offline session: %s (%d)", |
| 130 | strerror(-res), res); |
| 131 | write(fd, result.string(), result.size()); |
| 132 | } |
| 133 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 134 | return OK; |
| 135 | } |
| 136 | |
Avichal Rakesh | 7e53cad | 2021-10-05 13:46:30 -0700 | [diff] [blame] | 137 | status_t CameraOfflineSessionClient::startWatchingTags(const String8 &tags, int outFd) { |
| 138 | return BasicClient::startWatchingTags(tags, outFd); |
| 139 | } |
| 140 | |
| 141 | status_t CameraOfflineSessionClient::stopWatchingTags(int outFd) { |
| 142 | return BasicClient::stopWatchingTags(outFd); |
| 143 | } |
| 144 | |
| 145 | status_t CameraOfflineSessionClient::dumpWatchedEventsToVector(std::vector<std::string> &out) { |
| 146 | return BasicClient::dumpWatchedEventsToVector(out); |
| 147 | } |
| 148 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 149 | binder::Status CameraOfflineSessionClient::disconnect() { |
Emilian Peev | d99c8ae | 2019-11-26 13:19:13 -0800 | [diff] [blame] | 150 | Mutex::Autolock icl(mBinderSerializationLock); |
| 151 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 152 | binder::Status res = Status::ok(); |
| 153 | if (mDisconnected) { |
| 154 | return res; |
| 155 | } |
Emilian Peev | d99c8ae | 2019-11-26 13:19:13 -0800 | [diff] [blame] | 156 | // Allow both client and the media server to disconnect at all times |
| 157 | int callingPid = CameraThreadState::getCallingPid(); |
| 158 | if (callingPid != mClientPid && |
| 159 | callingPid != mServicePid) { |
| 160 | return res; |
| 161 | } |
| 162 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 163 | mDisconnected = true; |
| 164 | |
| 165 | sCameraService->removeByClient(this); |
| 166 | sCameraService->logDisconnectedOffline(mCameraIdStr, mClientPid, String8(mClientPackageName)); |
| 167 | |
| 168 | sp<IBinder> remote = getRemote(); |
| 169 | if (remote != nullptr) { |
| 170 | remote->unlinkToDeath(sCameraService); |
| 171 | } |
| 172 | |
Emilian Peev | faa4bde | 2020-01-23 12:19:37 -0800 | [diff] [blame] | 173 | mFrameProcessor->removeListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID, |
| 174 | camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID, |
| 175 | /*listener*/this); |
| 176 | mFrameProcessor->requestExit(); |
| 177 | mFrameProcessor->join(); |
| 178 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 179 | finishCameraOps(); |
| 180 | ALOGI("%s: Disconnected client for offline camera %s for PID %d", __FUNCTION__, |
| 181 | mCameraIdStr.string(), mClientPid); |
| 182 | |
| 183 | // client shouldn't be able to call into us anymore |
| 184 | mClientPid = 0; |
| 185 | |
Emilian Peev | d99c8ae | 2019-11-26 13:19:13 -0800 | [diff] [blame] | 186 | if (mOfflineSession.get() != nullptr) { |
| 187 | auto ret = mOfflineSession->disconnect(); |
| 188 | if (ret != OK) { |
| 189 | ALOGE("%s: Failed disconnecting from offline session %s (%d)", __FUNCTION__, |
| 190 | strerror(-ret), ret); |
| 191 | } |
| 192 | mOfflineSession = nullptr; |
| 193 | } |
| 194 | |
Emilian Peev | 4697b64 | 2019-11-19 17:11:14 -0800 | [diff] [blame] | 195 | for (size_t i = 0; i < mCompositeStreamMap.size(); i++) { |
| 196 | auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams(); |
| 197 | if (ret != OK) { |
| 198 | ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__, |
| 199 | strerror(-ret), ret); |
| 200 | } |
| 201 | } |
| 202 | mCompositeStreamMap.clear(); |
| 203 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 204 | return res; |
| 205 | } |
| 206 | |
| 207 | void CameraOfflineSessionClient::notifyError(int32_t errorCode, |
| 208 | const CaptureResultExtras& resultExtras) { |
| 209 | // Thread safe. Don't bother locking. |
Emilian Peev | 4697b64 | 2019-11-19 17:11:14 -0800 | [diff] [blame] | 210 | // Composites can have multiple internal streams. Error notifications coming from such internal |
| 211 | // streams may need to remain within camera service. |
| 212 | bool skipClientNotification = false; |
| 213 | for (size_t i = 0; i < mCompositeStreamMap.size(); i++) { |
| 214 | skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode, resultExtras); |
| 215 | } |
| 216 | |
Emilian Peev | d99c8ae | 2019-11-26 13:19:13 -0800 | [diff] [blame] | 217 | if ((mRemoteCallback.get() != nullptr) && (!skipClientNotification)) { |
| 218 | mRemoteCallback->onDeviceError(errorCode, resultExtras); |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | status_t CameraOfflineSessionClient::startCameraOps() { |
| 223 | ATRACE_CALL(); |
| 224 | { |
| 225 | ALOGV("%s: Start camera ops, package name = %s, client UID = %d", |
| 226 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
| 227 | } |
| 228 | |
| 229 | if (mAppOpsManager != nullptr) { |
| 230 | // Notify app ops that the camera is not available |
| 231 | mOpsCallback = new OpsCallback(this); |
| 232 | int32_t res; |
| 233 | // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION |
| 234 | mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA, |
| 235 | mClientPackageName, mOpsCallback); |
| 236 | // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION |
| 237 | res = mAppOpsManager->startOpNoThrow(AppOpsManager::OP_CAMERA, |
| 238 | mClientUid, mClientPackageName, /*startIfModeDefault*/ false); |
| 239 | |
| 240 | if (res == AppOpsManager::MODE_ERRORED) { |
| 241 | ALOGI("Offline Camera %s: Access for \"%s\" has been revoked", |
| 242 | mCameraIdStr.string(), String8(mClientPackageName).string()); |
| 243 | return PERMISSION_DENIED; |
| 244 | } |
| 245 | |
Shuzhen Wang | 2c65679 | 2020-04-13 17:36:49 -0700 | [diff] [blame] | 246 | // If the calling Uid is trusted (a native service), the AppOpsManager could |
| 247 | // return MODE_IGNORED. Do not treat such case as error. |
| 248 | if (!mUidIsTrusted && res == AppOpsManager::MODE_IGNORED) { |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 249 | ALOGI("Offline Camera %s: Access for \"%s\" has been restricted", |
| 250 | mCameraIdStr.string(), String8(mClientPackageName).string()); |
| 251 | // Return the same error as for device policy manager rejection |
| 252 | return -EACCES; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | mOpsActive = true; |
| 257 | |
| 258 | // Transition device state to OPEN |
| 259 | sCameraService->mUidPolicy->registerMonitorUid(mClientUid); |
| 260 | |
| 261 | return OK; |
| 262 | } |
| 263 | |
| 264 | status_t CameraOfflineSessionClient::finishCameraOps() { |
| 265 | ATRACE_CALL(); |
| 266 | |
| 267 | // Check if startCameraOps succeeded, and if so, finish the camera op |
| 268 | if (mOpsActive) { |
| 269 | // Notify app ops that the camera is available again |
| 270 | if (mAppOpsManager != nullptr) { |
| 271 | // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION |
| 272 | mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 273 | mClientPackageName); |
| 274 | mOpsActive = false; |
| 275 | } |
| 276 | } |
| 277 | // Always stop watching, even if no camera op is active |
| 278 | if (mOpsCallback != nullptr && mAppOpsManager != nullptr) { |
| 279 | mAppOpsManager->stopWatchingMode(mOpsCallback); |
| 280 | } |
| 281 | mOpsCallback.clear(); |
| 282 | |
| 283 | sCameraService->mUidPolicy->unregisterMonitorUid(mClientUid); |
| 284 | |
| 285 | return OK; |
| 286 | } |
| 287 | |
Emilian Peev | 4697b64 | 2019-11-19 17:11:14 -0800 | [diff] [blame] | 288 | void CameraOfflineSessionClient::onResultAvailable(const CaptureResult& result) { |
| 289 | ATRACE_CALL(); |
| 290 | ALOGV("%s", __FUNCTION__); |
| 291 | |
Emilian Peev | d99c8ae | 2019-11-26 13:19:13 -0800 | [diff] [blame] | 292 | if (mRemoteCallback.get() != NULL) { |
| 293 | mRemoteCallback->onResultReceived(result.mMetadata, result.mResultExtras, |
Emilian Peev | 4697b64 | 2019-11-19 17:11:14 -0800 | [diff] [blame] | 294 | result.mPhysicalMetadatas); |
| 295 | } |
| 296 | |
| 297 | for (size_t i = 0; i < mCompositeStreamMap.size(); i++) { |
| 298 | mCompositeStreamMap.valueAt(i)->onResultAvailable(result); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | void CameraOfflineSessionClient::notifyShutter(const CaptureResultExtras& resultExtras, |
| 303 | nsecs_t timestamp) { |
Emilian Peev | d99c8ae | 2019-11-26 13:19:13 -0800 | [diff] [blame] | 304 | |
| 305 | if (mRemoteCallback.get() != nullptr) { |
| 306 | mRemoteCallback->onCaptureStarted(resultExtras, timestamp); |
Emilian Peev | 4697b64 | 2019-11-19 17:11:14 -0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | for (size_t i = 0; i < mCompositeStreamMap.size(); i++) { |
| 310 | mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp); |
| 311 | } |
| 312 | } |
| 313 | |
Austin Borger | 4a870a3 | 2022-02-25 01:48:41 +0000 | [diff] [blame] | 314 | status_t CameraOfflineSessionClient::notifyActive(float maxPreviewFps __unused) { |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 315 | return startCameraStreamingOps(); |
| 316 | } |
| 317 | |
Shuzhen Wang | 316781a | 2020-08-18 18:11:01 -0700 | [diff] [blame] | 318 | void CameraOfflineSessionClient::notifyIdle( |
| 319 | int64_t /*requestCount*/, int64_t /*resultErrorCount*/, bool /*deviceError*/, |
| 320 | const std::vector<hardware::CameraStreamStats>& /*streamStats*/) { |
Emilian Peev | d99c8ae | 2019-11-26 13:19:13 -0800 | [diff] [blame] | 321 | if (mRemoteCallback.get() != nullptr) { |
| 322 | mRemoteCallback->onDeviceIdle(); |
| 323 | } |
Eino-Ville Talvala | 178e823 | 2021-04-16 18:41:39 -0700 | [diff] [blame] | 324 | finishCameraStreamingOps(); |
Emilian Peev | d99c8ae | 2019-11-26 13:19:13 -0800 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | void CameraOfflineSessionClient::notifyAutoFocus(uint8_t newState, int triggerId) { |
| 328 | (void)newState; |
| 329 | (void)triggerId; |
| 330 | |
| 331 | ALOGV("%s: Autofocus state now %d, last trigger %d", |
| 332 | __FUNCTION__, newState, triggerId); |
| 333 | } |
| 334 | |
| 335 | void CameraOfflineSessionClient::notifyAutoExposure(uint8_t newState, int triggerId) { |
| 336 | (void)newState; |
| 337 | (void)triggerId; |
| 338 | |
| 339 | ALOGV("%s: Autoexposure state now %d, last trigger %d", |
| 340 | __FUNCTION__, newState, triggerId); |
| 341 | } |
| 342 | |
| 343 | void CameraOfflineSessionClient::notifyAutoWhitebalance(uint8_t newState, int triggerId) { |
| 344 | (void)newState; |
| 345 | (void)triggerId; |
| 346 | |
| 347 | ALOGV("%s: Auto-whitebalance state now %d, last trigger %d", __FUNCTION__, newState, |
| 348 | triggerId); |
| 349 | } |
| 350 | |
| 351 | void CameraOfflineSessionClient::notifyPrepared(int /*streamId*/) { |
| 352 | ALOGE("%s: Unexpected stream prepare notification in offline mode!", __FUNCTION__); |
| 353 | notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE, |
| 354 | CaptureResultExtras()); |
| 355 | } |
| 356 | |
| 357 | void CameraOfflineSessionClient::notifyRequestQueueEmpty() { |
| 358 | if (mRemoteCallback.get() != nullptr) { |
| 359 | mRemoteCallback->onRequestQueueEmpty(); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | void CameraOfflineSessionClient::notifyRepeatingRequestError(long /*lastFrameNumber*/) { |
| 364 | ALOGE("%s: Unexpected repeating request error in offline mode!", __FUNCTION__); |
| 365 | notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE, |
| 366 | CaptureResultExtras()); |
| 367 | } |
| 368 | |
Cliff Wu | d3a0531 | 2021-04-26 23:07:31 +0800 | [diff] [blame] | 369 | status_t CameraOfflineSessionClient::injectCamera(const String8& injectedCamId, |
| 370 | sp<CameraProviderManager> manager) { |
| 371 | ALOGV("%s: This client doesn't support the injection camera. injectedCamId: %s providerPtr: %p", |
| 372 | __FUNCTION__, injectedCamId.string(), manager.get()); |
| 373 | |
| 374 | return OK; |
| 375 | } |
| 376 | |
| 377 | status_t CameraOfflineSessionClient::stopInjection() { |
| 378 | ALOGV("%s: This client doesn't support the injection camera.", __FUNCTION__); |
| 379 | |
| 380 | return OK; |
| 381 | } |
| 382 | |
Emilian Peev | b2bc5a4 | 2019-11-20 16:02:14 -0800 | [diff] [blame] | 383 | // ---------------------------------------------------------------------------- |
| 384 | }; // namespace android |