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