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