blob: beb655bfdff2fd10ff0029fdcdf8711eafa8d5bf [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
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 Peevfaa4bde2020-01-23 12:19:37 -080044 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 Peevd99c8ae2019-11-26 13:19:13 -080054 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 Peevc0fe54c2020-03-11 14:05:07 -070062 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
63 mCompositeStreamMap.valueAt(i)->switchToOffline();
64 }
65
Emilian Peevb2bc5a42019-11-20 16:02:14 -080066 return OK;
67}
68
Ravneetaeb20dc2022-03-30 05:33:03 +000069status_t CameraOfflineSessionClient::setCameraServiceWatchdog(bool) {
70 return OK;
71}
72
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -080073status_t CameraOfflineSessionClient::setRotateAndCropOverride(uint8_t /*rotateAndCrop*/) {
74 // Since we're not submitting more capture requests, changes to rotateAndCrop override
75 // make no difference.
76 return OK;
77}
78
Eino-Ville Talvala305cec62020-11-12 14:18:17 -080079bool CameraOfflineSessionClient::supportsCameraMute() {
80 // Offline mode doesn't support muting
81 return false;
82}
83
84status_t CameraOfflineSessionClient::setCameraMute(bool) {
85 return INVALID_OPERATION;
86}
87
88
Emilian Peevd99c8ae2019-11-26 13:19:13 -080089status_t CameraOfflineSessionClient::dump(int fd, const Vector<String16>& args) {
90 return BasicClient::dump(fd, args);
Emilian Peevb2bc5a42019-11-20 16:02:14 -080091}
92
Emilian Peevfaa4bde2020-01-23 12:19:37 -080093status_t CameraOfflineSessionClient::dumpClient(int fd, const Vector<String16>& args) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -080094 String8 result;
95
96 result = " Offline session dump:\n";
97 write(fd, result.string(), result.size());
98
99 if (mOfflineSession.get() == nullptr) {
100 result = " *** Offline session is detached\n";
101 write(fd, result.string(), result.size());
102 return NO_ERROR;
103 }
104
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800105 mFrameProcessor->dump(fd, args);
106
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800107 auto res = mOfflineSession->dump(fd);
108 if (res != OK) {
109 result = String8::format(" Error dumping offline session: %s (%d)",
110 strerror(-res), res);
111 write(fd, result.string(), result.size());
112 }
113
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800114 return OK;
115}
116
Avichal Rakesh7e53cad2021-10-05 13:46:30 -0700117status_t CameraOfflineSessionClient::startWatchingTags(const String8 &tags, int outFd) {
118 return BasicClient::startWatchingTags(tags, outFd);
119}
120
121status_t CameraOfflineSessionClient::stopWatchingTags(int outFd) {
122 return BasicClient::stopWatchingTags(outFd);
123}
124
125status_t CameraOfflineSessionClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
126 return BasicClient::dumpWatchedEventsToVector(out);
127}
128
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800129binder::Status CameraOfflineSessionClient::disconnect() {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800130 Mutex::Autolock icl(mBinderSerializationLock);
131
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800132 binder::Status res = Status::ok();
133 if (mDisconnected) {
134 return res;
135 }
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800136 // Allow both client and the media server to disconnect at all times
137 int callingPid = CameraThreadState::getCallingPid();
138 if (callingPid != mClientPid &&
139 callingPid != mServicePid) {
140 return res;
141 }
142
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800143 mDisconnected = true;
144
145 sCameraService->removeByClient(this);
146 sCameraService->logDisconnectedOffline(mCameraIdStr, mClientPid, String8(mClientPackageName));
147
148 sp<IBinder> remote = getRemote();
149 if (remote != nullptr) {
150 remote->unlinkToDeath(sCameraService);
151 }
152
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800153 mFrameProcessor->removeListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
154 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
155 /*listener*/this);
156 mFrameProcessor->requestExit();
157 mFrameProcessor->join();
158
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800159 finishCameraOps();
160 ALOGI("%s: Disconnected client for offline camera %s for PID %d", __FUNCTION__,
161 mCameraIdStr.string(), mClientPid);
162
163 // client shouldn't be able to call into us anymore
164 mClientPid = 0;
165
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800166 if (mOfflineSession.get() != nullptr) {
167 auto ret = mOfflineSession->disconnect();
168 if (ret != OK) {
169 ALOGE("%s: Failed disconnecting from offline session %s (%d)", __FUNCTION__,
170 strerror(-ret), ret);
171 }
172 mOfflineSession = nullptr;
173 }
174
Emilian Peev4697b642019-11-19 17:11:14 -0800175 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
176 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
177 if (ret != OK) {
178 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
179 strerror(-ret), ret);
180 }
181 }
182 mCompositeStreamMap.clear();
183
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800184 return res;
185}
186
187void CameraOfflineSessionClient::notifyError(int32_t errorCode,
188 const CaptureResultExtras& resultExtras) {
189 // Thread safe. Don't bother locking.
Emilian Peev4697b642019-11-19 17:11:14 -0800190 // Composites can have multiple internal streams. Error notifications coming from such internal
191 // streams may need to remain within camera service.
192 bool skipClientNotification = false;
193 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
194 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode, resultExtras);
195 }
196
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800197 if ((mRemoteCallback.get() != nullptr) && (!skipClientNotification)) {
198 mRemoteCallback->onDeviceError(errorCode, resultExtras);
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800199 }
200}
201
202status_t CameraOfflineSessionClient::startCameraOps() {
203 ATRACE_CALL();
204 {
205 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
206 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
207 }
208
209 if (mAppOpsManager != nullptr) {
210 // Notify app ops that the camera is not available
211 mOpsCallback = new OpsCallback(this);
212 int32_t res;
213 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
214 mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA,
215 mClientPackageName, mOpsCallback);
216 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
217 res = mAppOpsManager->startOpNoThrow(AppOpsManager::OP_CAMERA,
218 mClientUid, mClientPackageName, /*startIfModeDefault*/ false);
219
220 if (res == AppOpsManager::MODE_ERRORED) {
221 ALOGI("Offline Camera %s: Access for \"%s\" has been revoked",
222 mCameraIdStr.string(), String8(mClientPackageName).string());
223 return PERMISSION_DENIED;
224 }
225
Shuzhen Wang2c656792020-04-13 17:36:49 -0700226 // If the calling Uid is trusted (a native service), the AppOpsManager could
227 // return MODE_IGNORED. Do not treat such case as error.
228 if (!mUidIsTrusted && res == AppOpsManager::MODE_IGNORED) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800229 ALOGI("Offline Camera %s: Access for \"%s\" has been restricted",
230 mCameraIdStr.string(), String8(mClientPackageName).string());
231 // Return the same error as for device policy manager rejection
232 return -EACCES;
233 }
234 }
235
236 mOpsActive = true;
237
238 // Transition device state to OPEN
239 sCameraService->mUidPolicy->registerMonitorUid(mClientUid);
240
241 return OK;
242}
243
244status_t CameraOfflineSessionClient::finishCameraOps() {
245 ATRACE_CALL();
246
247 // Check if startCameraOps succeeded, and if so, finish the camera op
248 if (mOpsActive) {
249 // Notify app ops that the camera is available again
250 if (mAppOpsManager != nullptr) {
251 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
252 mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, mClientUid,
253 mClientPackageName);
254 mOpsActive = false;
255 }
256 }
257 // Always stop watching, even if no camera op is active
258 if (mOpsCallback != nullptr && mAppOpsManager != nullptr) {
259 mAppOpsManager->stopWatchingMode(mOpsCallback);
260 }
261 mOpsCallback.clear();
262
263 sCameraService->mUidPolicy->unregisterMonitorUid(mClientUid);
264
265 return OK;
266}
267
Emilian Peev4697b642019-11-19 17:11:14 -0800268void CameraOfflineSessionClient::onResultAvailable(const CaptureResult& result) {
269 ATRACE_CALL();
270 ALOGV("%s", __FUNCTION__);
271
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800272 if (mRemoteCallback.get() != NULL) {
273 mRemoteCallback->onResultReceived(result.mMetadata, result.mResultExtras,
Emilian Peev4697b642019-11-19 17:11:14 -0800274 result.mPhysicalMetadatas);
275 }
276
277 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
278 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
279 }
280}
281
282void CameraOfflineSessionClient::notifyShutter(const CaptureResultExtras& resultExtras,
283 nsecs_t timestamp) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800284
285 if (mRemoteCallback.get() != nullptr) {
286 mRemoteCallback->onCaptureStarted(resultExtras, timestamp);
Emilian Peev4697b642019-11-19 17:11:14 -0800287 }
288
289 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
290 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
291 }
292}
293
Austin Borger4a870a32022-02-25 01:48:41 +0000294status_t CameraOfflineSessionClient::notifyActive(float maxPreviewFps __unused) {
Eino-Ville Talvala178e8232021-04-16 18:41:39 -0700295 return startCameraStreamingOps();
296}
297
Shuzhen Wang316781a2020-08-18 18:11:01 -0700298void CameraOfflineSessionClient::notifyIdle(
299 int64_t /*requestCount*/, int64_t /*resultErrorCount*/, bool /*deviceError*/,
300 const std::vector<hardware::CameraStreamStats>& /*streamStats*/) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800301 if (mRemoteCallback.get() != nullptr) {
302 mRemoteCallback->onDeviceIdle();
303 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -0700304 finishCameraStreamingOps();
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800305}
306
307void CameraOfflineSessionClient::notifyAutoFocus(uint8_t newState, int triggerId) {
308 (void)newState;
309 (void)triggerId;
310
311 ALOGV("%s: Autofocus state now %d, last trigger %d",
312 __FUNCTION__, newState, triggerId);
313}
314
315void CameraOfflineSessionClient::notifyAutoExposure(uint8_t newState, int triggerId) {
316 (void)newState;
317 (void)triggerId;
318
319 ALOGV("%s: Autoexposure state now %d, last trigger %d",
320 __FUNCTION__, newState, triggerId);
321}
322
323void CameraOfflineSessionClient::notifyAutoWhitebalance(uint8_t newState, int triggerId) {
324 (void)newState;
325 (void)triggerId;
326
327 ALOGV("%s: Auto-whitebalance state now %d, last trigger %d", __FUNCTION__, newState,
328 triggerId);
329}
330
331void CameraOfflineSessionClient::notifyPrepared(int /*streamId*/) {
332 ALOGE("%s: Unexpected stream prepare notification in offline mode!", __FUNCTION__);
333 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
334 CaptureResultExtras());
335}
336
337void CameraOfflineSessionClient::notifyRequestQueueEmpty() {
338 if (mRemoteCallback.get() != nullptr) {
339 mRemoteCallback->onRequestQueueEmpty();
340 }
341}
342
343void CameraOfflineSessionClient::notifyRepeatingRequestError(long /*lastFrameNumber*/) {
344 ALOGE("%s: Unexpected repeating request error in offline mode!", __FUNCTION__);
345 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
346 CaptureResultExtras());
347}
348
Cliff Wud3a05312021-04-26 23:07:31 +0800349status_t CameraOfflineSessionClient::injectCamera(const String8& injectedCamId,
350 sp<CameraProviderManager> manager) {
351 ALOGV("%s: This client doesn't support the injection camera. injectedCamId: %s providerPtr: %p",
352 __FUNCTION__, injectedCamId.string(), manager.get());
353
354 return OK;
355}
356
357status_t CameraOfflineSessionClient::stopInjection() {
358 ALOGV("%s: This client doesn't support the injection camera.", __FUNCTION__);
359
360 return OK;
361}
362
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800363// ----------------------------------------------------------------------------
364}; // namespace android