blob: 0d1d0195b821e0878f3de00663bc5add94644fc8 [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());
Austin Borger7b129542022-06-09 13:23:06 -070052 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 Peevfaa4bde2020-01-23 12:19:37 -080058
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 Peevd99c8ae2019-11-26 13:19:13 -080064 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 Peevc0fe54c2020-03-11 14:05:07 -070072 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
73 mCompositeStreamMap.valueAt(i)->switchToOffline();
74 }
75
Emilian Peevb2bc5a42019-11-20 16:02:14 -080076 return OK;
77}
78
Ravneetaeb20dc2022-03-30 05:33:03 +000079status_t CameraOfflineSessionClient::setCameraServiceWatchdog(bool) {
80 return OK;
81}
82
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -080083status_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 Kukreja7146ced2022-10-25 15:45:29 +000089status_t CameraOfflineSessionClient::setAutoframingOverride(uint8_t) {
90 return OK;
91}
92
Eino-Ville Talvala305cec62020-11-12 14:18:17 -080093bool CameraOfflineSessionClient::supportsCameraMute() {
94 // Offline mode doesn't support muting
95 return false;
96}
97
98status_t CameraOfflineSessionClient::setCameraMute(bool) {
99 return INVALID_OPERATION;
100}
101
Shuzhen Wang16610a62022-12-15 22:38:07 -0800102void CameraOfflineSessionClient::setStreamUseCaseOverrides(
103 const std::vector<int64_t>& /*useCaseOverrides*/) {
104}
105
106void CameraOfflineSessionClient::clearStreamUseCaseOverrides() {
107}
108
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800109status_t CameraOfflineSessionClient::dump(int fd, const Vector<String16>& args) {
110 return BasicClient::dump(fd, args);
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800111}
112
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800113status_t CameraOfflineSessionClient::dumpClient(int fd, const Vector<String16>& args) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800114 String8 result;
115
116 result = " Offline session dump:\n";
117 write(fd, result.string(), result.size());
118
119 if (mOfflineSession.get() == nullptr) {
120 result = " *** Offline session is detached\n";
121 write(fd, result.string(), result.size());
122 return NO_ERROR;
123 }
124
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800125 mFrameProcessor->dump(fd, args);
126
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800127 auto res = mOfflineSession->dump(fd);
128 if (res != OK) {
129 result = String8::format(" Error dumping offline session: %s (%d)",
130 strerror(-res), res);
131 write(fd, result.string(), result.size());
132 }
133
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800134 return OK;
135}
136
Avichal Rakesh7e53cad2021-10-05 13:46:30 -0700137status_t CameraOfflineSessionClient::startWatchingTags(const String8 &tags, int outFd) {
138 return BasicClient::startWatchingTags(tags, outFd);
139}
140
141status_t CameraOfflineSessionClient::stopWatchingTags(int outFd) {
142 return BasicClient::stopWatchingTags(outFd);
143}
144
145status_t CameraOfflineSessionClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
146 return BasicClient::dumpWatchedEventsToVector(out);
147}
148
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800149binder::Status CameraOfflineSessionClient::disconnect() {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800150 Mutex::Autolock icl(mBinderSerializationLock);
151
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800152 binder::Status res = Status::ok();
153 if (mDisconnected) {
154 return res;
155 }
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800156 // Allow both client and the media server to disconnect at all times
157 int callingPid = CameraThreadState::getCallingPid();
158 if (callingPid != mClientPid &&
159 callingPid != mServicePid) {
160 return res;
161 }
162
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800163 mDisconnected = true;
164
165 sCameraService->removeByClient(this);
166 sCameraService->logDisconnectedOffline(mCameraIdStr, mClientPid, String8(mClientPackageName));
167
168 sp<IBinder> remote = getRemote();
169 if (remote != nullptr) {
170 remote->unlinkToDeath(sCameraService);
171 }
172
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800173 mFrameProcessor->removeListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
174 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
175 /*listener*/this);
176 mFrameProcessor->requestExit();
177 mFrameProcessor->join();
178
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800179 finishCameraOps();
180 ALOGI("%s: Disconnected client for offline camera %s for PID %d", __FUNCTION__,
181 mCameraIdStr.string(), mClientPid);
182
183 // client shouldn't be able to call into us anymore
184 mClientPid = 0;
185
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800186 if (mOfflineSession.get() != nullptr) {
187 auto ret = mOfflineSession->disconnect();
188 if (ret != OK) {
189 ALOGE("%s: Failed disconnecting from offline session %s (%d)", __FUNCTION__,
190 strerror(-ret), ret);
191 }
192 mOfflineSession = nullptr;
193 }
194
Emilian Peev4697b642019-11-19 17:11:14 -0800195 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
196 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
197 if (ret != OK) {
198 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
199 strerror(-ret), ret);
200 }
201 }
202 mCompositeStreamMap.clear();
203
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800204 return res;
205}
206
207void CameraOfflineSessionClient::notifyError(int32_t errorCode,
208 const CaptureResultExtras& resultExtras) {
209 // Thread safe. Don't bother locking.
Emilian Peev4697b642019-11-19 17:11:14 -0800210 // Composites can have multiple internal streams. Error notifications coming from such internal
211 // streams may need to remain within camera service.
212 bool skipClientNotification = false;
213 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
214 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode, resultExtras);
215 }
216
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800217 if ((mRemoteCallback.get() != nullptr) && (!skipClientNotification)) {
218 mRemoteCallback->onDeviceError(errorCode, resultExtras);
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800219 }
220}
221
222status_t CameraOfflineSessionClient::startCameraOps() {
223 ATRACE_CALL();
224 {
225 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
226 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
227 }
228
229 if (mAppOpsManager != nullptr) {
230 // Notify app ops that the camera is not available
231 mOpsCallback = new OpsCallback(this);
232 int32_t res;
233 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
234 mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA,
235 mClientPackageName, mOpsCallback);
236 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
237 res = mAppOpsManager->startOpNoThrow(AppOpsManager::OP_CAMERA,
238 mClientUid, mClientPackageName, /*startIfModeDefault*/ false);
239
240 if (res == AppOpsManager::MODE_ERRORED) {
241 ALOGI("Offline Camera %s: Access for \"%s\" has been revoked",
242 mCameraIdStr.string(), String8(mClientPackageName).string());
243 return PERMISSION_DENIED;
244 }
245
Shuzhen Wang2c656792020-04-13 17:36:49 -0700246 // If the calling Uid is trusted (a native service), the AppOpsManager could
247 // return MODE_IGNORED. Do not treat such case as error.
248 if (!mUidIsTrusted && res == AppOpsManager::MODE_IGNORED) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800249 ALOGI("Offline Camera %s: Access for \"%s\" has been restricted",
250 mCameraIdStr.string(), String8(mClientPackageName).string());
251 // Return the same error as for device policy manager rejection
252 return -EACCES;
253 }
254 }
255
256 mOpsActive = true;
257
258 // Transition device state to OPEN
259 sCameraService->mUidPolicy->registerMonitorUid(mClientUid);
260
261 return OK;
262}
263
264status_t CameraOfflineSessionClient::finishCameraOps() {
265 ATRACE_CALL();
266
267 // Check if startCameraOps succeeded, and if so, finish the camera op
268 if (mOpsActive) {
269 // Notify app ops that the camera is available again
270 if (mAppOpsManager != nullptr) {
271 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
272 mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, mClientUid,
273 mClientPackageName);
274 mOpsActive = false;
275 }
276 }
277 // Always stop watching, even if no camera op is active
278 if (mOpsCallback != nullptr && mAppOpsManager != nullptr) {
279 mAppOpsManager->stopWatchingMode(mOpsCallback);
280 }
281 mOpsCallback.clear();
282
283 sCameraService->mUidPolicy->unregisterMonitorUid(mClientUid);
284
285 return OK;
286}
287
Emilian Peev4697b642019-11-19 17:11:14 -0800288void CameraOfflineSessionClient::onResultAvailable(const CaptureResult& result) {
289 ATRACE_CALL();
290 ALOGV("%s", __FUNCTION__);
291
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800292 if (mRemoteCallback.get() != NULL) {
293 mRemoteCallback->onResultReceived(result.mMetadata, result.mResultExtras,
Emilian Peev4697b642019-11-19 17:11:14 -0800294 result.mPhysicalMetadatas);
295 }
296
297 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
298 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
299 }
300}
301
302void CameraOfflineSessionClient::notifyShutter(const CaptureResultExtras& resultExtras,
303 nsecs_t timestamp) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800304
305 if (mRemoteCallback.get() != nullptr) {
306 mRemoteCallback->onCaptureStarted(resultExtras, timestamp);
Emilian Peev4697b642019-11-19 17:11:14 -0800307 }
308
309 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
310 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
311 }
312}
313
Austin Borger4a870a32022-02-25 01:48:41 +0000314status_t CameraOfflineSessionClient::notifyActive(float maxPreviewFps __unused) {
Eino-Ville Talvala178e8232021-04-16 18:41:39 -0700315 return startCameraStreamingOps();
316}
317
Shuzhen Wang316781a2020-08-18 18:11:01 -0700318void CameraOfflineSessionClient::notifyIdle(
319 int64_t /*requestCount*/, int64_t /*resultErrorCount*/, bool /*deviceError*/,
320 const std::vector<hardware::CameraStreamStats>& /*streamStats*/) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800321 if (mRemoteCallback.get() != nullptr) {
322 mRemoteCallback->onDeviceIdle();
323 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -0700324 finishCameraStreamingOps();
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800325}
326
327void CameraOfflineSessionClient::notifyAutoFocus(uint8_t newState, int triggerId) {
328 (void)newState;
329 (void)triggerId;
330
331 ALOGV("%s: Autofocus state now %d, last trigger %d",
332 __FUNCTION__, newState, triggerId);
333}
334
335void CameraOfflineSessionClient::notifyAutoExposure(uint8_t newState, int triggerId) {
336 (void)newState;
337 (void)triggerId;
338
339 ALOGV("%s: Autoexposure state now %d, last trigger %d",
340 __FUNCTION__, newState, triggerId);
341}
342
343void CameraOfflineSessionClient::notifyAutoWhitebalance(uint8_t newState, int triggerId) {
344 (void)newState;
345 (void)triggerId;
346
347 ALOGV("%s: Auto-whitebalance state now %d, last trigger %d", __FUNCTION__, newState,
348 triggerId);
349}
350
351void CameraOfflineSessionClient::notifyPrepared(int /*streamId*/) {
352 ALOGE("%s: Unexpected stream prepare notification in offline mode!", __FUNCTION__);
353 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
354 CaptureResultExtras());
355}
356
357void CameraOfflineSessionClient::notifyRequestQueueEmpty() {
358 if (mRemoteCallback.get() != nullptr) {
359 mRemoteCallback->onRequestQueueEmpty();
360 }
361}
362
363void CameraOfflineSessionClient::notifyRepeatingRequestError(long /*lastFrameNumber*/) {
364 ALOGE("%s: Unexpected repeating request error in offline mode!", __FUNCTION__);
365 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
366 CaptureResultExtras());
367}
368
Cliff Wud3a05312021-04-26 23:07:31 +0800369status_t CameraOfflineSessionClient::injectCamera(const String8& injectedCamId,
370 sp<CameraProviderManager> manager) {
371 ALOGV("%s: This client doesn't support the injection camera. injectedCamId: %s providerPtr: %p",
372 __FUNCTION__, injectedCamId.string(), manager.get());
373
374 return OK;
375}
376
377status_t CameraOfflineSessionClient::stopInjection() {
378 ALOGV("%s: This client doesn't support the injection camera.", __FUNCTION__);
379
380 return OK;
381}
382
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800383// ----------------------------------------------------------------------------
384}; // namespace android