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