blob: 2cb33972067b093cf506aa5fb06cf97e810e5632 [file] [log] [blame]
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001/*
Emilian Peevd99c8ae2019-11-26 13:19:13 -08002 * Copyright (C) 2019 The Android Open Source Project
Emilian Peevb2bc5a42019-11-20 16:02:14 -08003 *
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 Peevd99c8ae2019-11-26 13:19:13 -080022#include "utils/CameraThreadState.h"
Emilian Peevb2bc5a42019-11-20 16:02:14 -080023#include <utils/Trace.h>
24
25namespace android {
26
27using binder::Status;
28
29status_t CameraOfflineSessionClient::initialize(sp<CameraProviderManager>, const String8&) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -080030 ATRACE_CALL();
31
Emilian Peev886ac212023-02-07 14:10:24 -080032 if (mFrameProcessor.get() != nullptr) {
33 // Already initialized
34 return OK;
35 }
36
Emilian Peevd99c8ae2019-11-26 13:19:13 -080037 // 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 Peevfaa4bde2020-01-23 12:19:37 -080049 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 Peevd99c8ae2019-11-26 13:19:13 -080059 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 Peevc0fe54c2020-03-11 14:05:07 -070067 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
68 mCompositeStreamMap.valueAt(i)->switchToOffline();
69 }
70
Emilian Peevb2bc5a42019-11-20 16:02:14 -080071 return OK;
72}
73
Ravneet74cd3732022-03-30 05:33:03 +000074status_t CameraOfflineSessionClient::setCameraServiceWatchdog(bool) {
75 return OK;
76}
77
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -080078status_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 Talvala305cec62020-11-12 14:18:17 -080084bool CameraOfflineSessionClient::supportsCameraMute() {
85 // Offline mode doesn't support muting
86 return false;
87}
88
89status_t CameraOfflineSessionClient::setCameraMute(bool) {
90 return INVALID_OPERATION;
91}
92
93
Emilian Peevd99c8ae2019-11-26 13:19:13 -080094status_t CameraOfflineSessionClient::dump(int fd, const Vector<String16>& args) {
95 return BasicClient::dump(fd, args);
Emilian Peevb2bc5a42019-11-20 16:02:14 -080096}
97
Emilian Peevfaa4bde2020-01-23 12:19:37 -080098status_t CameraOfflineSessionClient::dumpClient(int fd, const Vector<String16>& args) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -080099 String8 result;
100
101 result = " Offline session dump:\n";
102 write(fd, result.string(), result.size());
103
104 if (mOfflineSession.get() == nullptr) {
105 result = " *** Offline session is detached\n";
106 write(fd, result.string(), result.size());
107 return NO_ERROR;
108 }
109
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800110 mFrameProcessor->dump(fd, args);
111
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800112 auto res = mOfflineSession->dump(fd);
113 if (res != OK) {
114 result = String8::format(" Error dumping offline session: %s (%d)",
115 strerror(-res), res);
116 write(fd, result.string(), result.size());
117 }
118
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800119 return OK;
120}
121
Avichal Rakesh7e53cad2021-10-05 13:46:30 -0700122status_t CameraOfflineSessionClient::startWatchingTags(const String8 &tags, int outFd) {
123 return BasicClient::startWatchingTags(tags, outFd);
124}
125
126status_t CameraOfflineSessionClient::stopWatchingTags(int outFd) {
127 return BasicClient::stopWatchingTags(outFd);
128}
129
130status_t CameraOfflineSessionClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
131 return BasicClient::dumpWatchedEventsToVector(out);
132}
133
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800134binder::Status CameraOfflineSessionClient::disconnect() {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800135 Mutex::Autolock icl(mBinderSerializationLock);
136
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800137 binder::Status res = Status::ok();
138 if (mDisconnected) {
139 return res;
140 }
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800141 // Allow both client and the media server to disconnect at all times
142 int callingPid = CameraThreadState::getCallingPid();
143 if (callingPid != mClientPid &&
144 callingPid != mServicePid) {
145 return res;
146 }
147
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800148 mDisconnected = true;
149
150 sCameraService->removeByClient(this);
151 sCameraService->logDisconnectedOffline(mCameraIdStr, mClientPid, String8(mClientPackageName));
152
153 sp<IBinder> remote = getRemote();
154 if (remote != nullptr) {
155 remote->unlinkToDeath(sCameraService);
156 }
157
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800158 mFrameProcessor->removeListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
159 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
160 /*listener*/this);
161 mFrameProcessor->requestExit();
162 mFrameProcessor->join();
163
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800164 finishCameraOps();
165 ALOGI("%s: Disconnected client for offline camera %s for PID %d", __FUNCTION__,
166 mCameraIdStr.string(), mClientPid);
167
168 // client shouldn't be able to call into us anymore
169 mClientPid = 0;
170
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800171 if (mOfflineSession.get() != nullptr) {
172 auto ret = mOfflineSession->disconnect();
173 if (ret != OK) {
174 ALOGE("%s: Failed disconnecting from offline session %s (%d)", __FUNCTION__,
175 strerror(-ret), ret);
176 }
177 mOfflineSession = nullptr;
178 }
179
Emilian Peev4697b642019-11-19 17:11:14 -0800180 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
181 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
182 if (ret != OK) {
183 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
184 strerror(-ret), ret);
185 }
186 }
187 mCompositeStreamMap.clear();
188
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800189 return res;
190}
191
192void CameraOfflineSessionClient::notifyError(int32_t errorCode,
193 const CaptureResultExtras& resultExtras) {
194 // Thread safe. Don't bother locking.
Emilian Peev4697b642019-11-19 17:11:14 -0800195 // Composites can have multiple internal streams. Error notifications coming from such internal
196 // streams may need to remain within camera service.
197 bool skipClientNotification = false;
198 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
199 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode, resultExtras);
200 }
201
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800202 if ((mRemoteCallback.get() != nullptr) && (!skipClientNotification)) {
203 mRemoteCallback->onDeviceError(errorCode, resultExtras);
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800204 }
205}
206
207status_t CameraOfflineSessionClient::startCameraOps() {
208 ATRACE_CALL();
209 {
210 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
211 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
212 }
213
214 if (mAppOpsManager != nullptr) {
215 // Notify app ops that the camera is not available
216 mOpsCallback = new OpsCallback(this);
217 int32_t res;
218 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
219 mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA,
220 mClientPackageName, mOpsCallback);
221 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
222 res = mAppOpsManager->startOpNoThrow(AppOpsManager::OP_CAMERA,
223 mClientUid, mClientPackageName, /*startIfModeDefault*/ false);
224
225 if (res == AppOpsManager::MODE_ERRORED) {
226 ALOGI("Offline Camera %s: Access for \"%s\" has been revoked",
227 mCameraIdStr.string(), String8(mClientPackageName).string());
228 return PERMISSION_DENIED;
229 }
230
Shuzhen Wang2c656792020-04-13 17:36:49 -0700231 // If the calling Uid is trusted (a native service), the AppOpsManager could
232 // return MODE_IGNORED. Do not treat such case as error.
233 if (!mUidIsTrusted && res == AppOpsManager::MODE_IGNORED) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800234 ALOGI("Offline Camera %s: Access for \"%s\" has been restricted",
235 mCameraIdStr.string(), String8(mClientPackageName).string());
236 // Return the same error as for device policy manager rejection
237 return -EACCES;
238 }
239 }
240
241 mOpsActive = true;
242
243 // Transition device state to OPEN
244 sCameraService->mUidPolicy->registerMonitorUid(mClientUid);
245
246 return OK;
247}
248
249status_t CameraOfflineSessionClient::finishCameraOps() {
250 ATRACE_CALL();
251
252 // Check if startCameraOps succeeded, and if so, finish the camera op
253 if (mOpsActive) {
254 // Notify app ops that the camera is available again
255 if (mAppOpsManager != nullptr) {
256 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
257 mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, mClientUid,
258 mClientPackageName);
259 mOpsActive = false;
260 }
261 }
262 // Always stop watching, even if no camera op is active
263 if (mOpsCallback != nullptr && mAppOpsManager != nullptr) {
264 mAppOpsManager->stopWatchingMode(mOpsCallback);
265 }
266 mOpsCallback.clear();
267
268 sCameraService->mUidPolicy->unregisterMonitorUid(mClientUid);
269
270 return OK;
271}
272
Emilian Peev4697b642019-11-19 17:11:14 -0800273void CameraOfflineSessionClient::onResultAvailable(const CaptureResult& result) {
274 ATRACE_CALL();
275 ALOGV("%s", __FUNCTION__);
276
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800277 if (mRemoteCallback.get() != NULL) {
278 mRemoteCallback->onResultReceived(result.mMetadata, result.mResultExtras,
Emilian Peev4697b642019-11-19 17:11:14 -0800279 result.mPhysicalMetadatas);
280 }
281
282 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
283 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
284 }
285}
286
287void CameraOfflineSessionClient::notifyShutter(const CaptureResultExtras& resultExtras,
288 nsecs_t timestamp) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800289
290 if (mRemoteCallback.get() != nullptr) {
291 mRemoteCallback->onCaptureStarted(resultExtras, timestamp);
Emilian Peev4697b642019-11-19 17:11:14 -0800292 }
293
294 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
295 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
296 }
297}
298
Austin Borger4a870a32022-02-25 01:48:41 +0000299status_t CameraOfflineSessionClient::notifyActive(float maxPreviewFps __unused) {
Eino-Ville Talvala178e8232021-04-16 18:41:39 -0700300 return startCameraStreamingOps();
301}
302
Shuzhen Wang316781a2020-08-18 18:11:01 -0700303void CameraOfflineSessionClient::notifyIdle(
304 int64_t /*requestCount*/, int64_t /*resultErrorCount*/, bool /*deviceError*/,
305 const std::vector<hardware::CameraStreamStats>& /*streamStats*/) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800306 if (mRemoteCallback.get() != nullptr) {
307 mRemoteCallback->onDeviceIdle();
308 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -0700309 finishCameraStreamingOps();
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800310}
311
312void CameraOfflineSessionClient::notifyAutoFocus(uint8_t newState, int triggerId) {
313 (void)newState;
314 (void)triggerId;
315
316 ALOGV("%s: Autofocus state now %d, last trigger %d",
317 __FUNCTION__, newState, triggerId);
318}
319
320void CameraOfflineSessionClient::notifyAutoExposure(uint8_t newState, int triggerId) {
321 (void)newState;
322 (void)triggerId;
323
324 ALOGV("%s: Autoexposure state now %d, last trigger %d",
325 __FUNCTION__, newState, triggerId);
326}
327
328void CameraOfflineSessionClient::notifyAutoWhitebalance(uint8_t newState, int triggerId) {
329 (void)newState;
330 (void)triggerId;
331
332 ALOGV("%s: Auto-whitebalance state now %d, last trigger %d", __FUNCTION__, newState,
333 triggerId);
334}
335
336void CameraOfflineSessionClient::notifyPrepared(int /*streamId*/) {
337 ALOGE("%s: Unexpected stream prepare notification in offline mode!", __FUNCTION__);
338 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
339 CaptureResultExtras());
340}
341
342void CameraOfflineSessionClient::notifyRequestQueueEmpty() {
343 if (mRemoteCallback.get() != nullptr) {
344 mRemoteCallback->onRequestQueueEmpty();
345 }
346}
347
348void CameraOfflineSessionClient::notifyRepeatingRequestError(long /*lastFrameNumber*/) {
349 ALOGE("%s: Unexpected repeating request error in offline mode!", __FUNCTION__);
350 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
351 CaptureResultExtras());
352}
353
Cliff Wud3a05312021-04-26 23:07:31 +0800354status_t CameraOfflineSessionClient::injectCamera(const String8& injectedCamId,
355 sp<CameraProviderManager> manager) {
356 ALOGV("%s: This client doesn't support the injection camera. injectedCamId: %s providerPtr: %p",
357 __FUNCTION__, injectedCamId.string(), manager.get());
358
359 return OK;
360}
361
362status_t CameraOfflineSessionClient::stopInjection() {
363 ALOGV("%s: This client doesn't support the injection camera.", __FUNCTION__);
364
365 return OK;
366}
367
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800368// ----------------------------------------------------------------------------
369}; // namespace android