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