blob: 277a8cfc0fe9909b5f6a2df1bd5cf02bdffc45b5 [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"
22#include <utils/Trace.h>
Austin Borgered99f642023-06-01 16:51:35 -070023#include <camera/StringUtils.h>
Emilian Peevb2bc5a42019-11-20 16:02:14 -080024
25namespace android {
26
27using binder::Status;
28
Austin Borgered99f642023-06-01 16:51:35 -070029status_t CameraOfflineSessionClient::initialize(sp<CameraProviderManager>, const std::string&) {
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",
Austin Borgered99f642023-06-01 16:51:35 -070045 __FUNCTION__, mCameraIdStr.c_str());
Emilian Peevd99c8ae2019-11-26 13:19:13 -080046 return NO_INIT;
47 }
48
Emilian Peevfaa4bde2020-01-23 12:19:37 -080049 mFrameProcessor = new camera2::FrameProcessorBase(mOfflineSession);
Austin Borgered99f642023-06-01 16:51:35 -070050 std::string threadName = fmt::sprintf("Offline-%s-FrameProc", mCameraIdStr.c_str());
51 res = mFrameProcessor->run(threadName.c_str());
Austin Borger7b129542022-06-09 13:23:06 -070052 if (res != OK) {
53 ALOGE("%s: Unable to start frame processor thread: %s (%d)",
54 __FUNCTION__, strerror(-res), res);
55 return res;
56 }
Emilian Peevfaa4bde2020-01-23 12:19:37 -080057
58 mFrameProcessor->registerListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
59 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
60 /*listener*/this,
61 /*sendPartials*/true);
62
Emilian Peevd99c8ae2019-11-26 13:19:13 -080063 wp<NotificationListener> weakThis(this);
64 res = mOfflineSession->initialize(weakThis);
65 if (res != OK) {
66 ALOGE("%s: Camera %s: unable to initialize device: %s (%d)",
Austin Borgered99f642023-06-01 16:51:35 -070067 __FUNCTION__, mCameraIdStr.c_str(), strerror(-res), res);
Emilian Peevd99c8ae2019-11-26 13:19:13 -080068 return res;
69 }
70
Emilian Peevc0fe54c2020-03-11 14:05:07 -070071 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
72 mCompositeStreamMap.valueAt(i)->switchToOffline();
73 }
74
Emilian Peevb2bc5a42019-11-20 16:02:14 -080075 return OK;
76}
77
Ravneetaeb20dc2022-03-30 05:33:03 +000078status_t CameraOfflineSessionClient::setCameraServiceWatchdog(bool) {
79 return OK;
80}
81
Jayant Chowdhary44d5f622023-09-20 03:11:41 +000082status_t CameraOfflineSessionClient::setRotateAndCropOverride(uint8_t /*rotateAndCrop*/,
83 bool /*fromHal*/) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -080084 // 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
Shuzhen Wangaf22e912023-04-11 16:03:17 -0700109bool CameraOfflineSessionClient::supportsZoomOverride() {
110 return false;
111}
112
113status_t CameraOfflineSessionClient::setZoomOverride(int32_t /*zoomOverride*/) {
114 return INVALID_OPERATION;
115}
116
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800117status_t CameraOfflineSessionClient::dump(int fd, const Vector<String16>& args) {
118 return BasicClient::dump(fd, args);
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800119}
120
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800121status_t CameraOfflineSessionClient::dumpClient(int fd, const Vector<String16>& args) {
Austin Borgered99f642023-06-01 16:51:35 -0700122 std::string result;
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800123
124 result = " Offline session dump:\n";
Austin Borgered99f642023-06-01 16:51:35 -0700125 write(fd, result.c_str(), result.size());
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800126
127 if (mOfflineSession.get() == nullptr) {
128 result = " *** Offline session is detached\n";
Austin Borgered99f642023-06-01 16:51:35 -0700129 write(fd, result.c_str(), result.size());
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800130 return NO_ERROR;
131 }
132
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800133 mFrameProcessor->dump(fd, args);
134
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800135 auto res = mOfflineSession->dump(fd);
136 if (res != OK) {
Austin Borgered99f642023-06-01 16:51:35 -0700137 result = fmt::sprintf(" Error dumping offline session: %s (%d)",
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800138 strerror(-res), res);
Austin Borgered99f642023-06-01 16:51:35 -0700139 write(fd, result.c_str(), result.size());
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800140 }
141
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800142 return OK;
143}
144
Austin Borgered99f642023-06-01 16:51:35 -0700145status_t CameraOfflineSessionClient::startWatchingTags(const std::string &tags, int outFd) {
Avichal Rakesh7e53cad2021-10-05 13:46:30 -0700146 return BasicClient::startWatchingTags(tags, outFd);
147}
148
149status_t CameraOfflineSessionClient::stopWatchingTags(int outFd) {
150 return BasicClient::stopWatchingTags(outFd);
151}
152
153status_t CameraOfflineSessionClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
154 return BasicClient::dumpWatchedEventsToVector(out);
155}
156
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800157binder::Status CameraOfflineSessionClient::disconnect() {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800158 Mutex::Autolock icl(mBinderSerializationLock);
159
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800160 binder::Status res = Status::ok();
161 if (mDisconnected) {
162 return res;
163 }
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800164 // Allow both client and the media server to disconnect at all times
Austin Borger22c5c852024-03-08 13:31:36 -0800165 int callingPid = getCallingPid();
Austin Borger2e772b82024-10-11 16:09:57 -0700166 if (callingPid != mCallingPid &&
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800167 callingPid != mServicePid) {
168 return res;
169 }
170
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800171 mDisconnected = true;
172
173 sCameraService->removeByClient(this);
Austin Borger2e772b82024-10-11 16:09:57 -0700174 sCameraService->logDisconnectedOffline(mCameraIdStr, mCallingPid, getPackageName());
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800175
176 sp<IBinder> remote = getRemote();
177 if (remote != nullptr) {
178 remote->unlinkToDeath(sCameraService);
179 }
180
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800181 mFrameProcessor->removeListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
182 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
183 /*listener*/this);
184 mFrameProcessor->requestExit();
185 mFrameProcessor->join();
186
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800187 finishCameraOps();
188 ALOGI("%s: Disconnected client for offline camera %s for PID %d", __FUNCTION__,
Austin Borger2e772b82024-10-11 16:09:57 -0700189 mCameraIdStr.c_str(), mCallingPid);
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800190
191 // client shouldn't be able to call into us anymore
Austin Borger2e772b82024-10-11 16:09:57 -0700192 mCallingPid = 0;
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800193
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800194 if (mOfflineSession.get() != nullptr) {
195 auto ret = mOfflineSession->disconnect();
196 if (ret != OK) {
197 ALOGE("%s: Failed disconnecting from offline session %s (%d)", __FUNCTION__,
198 strerror(-ret), ret);
199 }
200 mOfflineSession = nullptr;
201 }
202
Emilian Peev4697b642019-11-19 17:11:14 -0800203 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
204 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
205 if (ret != OK) {
206 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
207 strerror(-ret), ret);
208 }
209 }
210 mCompositeStreamMap.clear();
211
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800212 return res;
213}
214
215void CameraOfflineSessionClient::notifyError(int32_t errorCode,
216 const CaptureResultExtras& resultExtras) {
217 // Thread safe. Don't bother locking.
Emilian Peev4697b642019-11-19 17:11:14 -0800218 // Composites can have multiple internal streams. Error notifications coming from such internal
219 // streams may need to remain within camera service.
220 bool skipClientNotification = false;
221 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
222 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode, resultExtras);
223 }
224
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800225 if ((mRemoteCallback.get() != nullptr) && (!skipClientNotification)) {
226 mRemoteCallback->onDeviceError(errorCode, resultExtras);
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800227 }
228}
229
230status_t CameraOfflineSessionClient::startCameraOps() {
231 ATRACE_CALL();
232 {
Austin Borger2e772b82024-10-11 16:09:57 -0700233 ALOGV("%s: Start camera ops, package name = %s, client UID = %d", __FUNCTION__,
234 getPackageName().c_str(), getClientUid());
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800235 }
236
237 if (mAppOpsManager != nullptr) {
238 // Notify app ops that the camera is not available
239 mOpsCallback = new OpsCallback(this);
240 int32_t res;
241 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
Austin Borger2e772b82024-10-11 16:09:57 -0700242 mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA, toString16(getPackageName()),
243 mOpsCallback);
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800244 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
Austin Borger2e772b82024-10-11 16:09:57 -0700245 res = mAppOpsManager->startOpNoThrow(AppOpsManager::OP_CAMERA, getClientUid(),
246 toString16(getPackageName()),
247 /*startIfModeDefault*/ false);
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800248
249 if (res == AppOpsManager::MODE_ERRORED) {
Austin Borger2e772b82024-10-11 16:09:57 -0700250 ALOGI("Offline Camera %s: Access for \"%s\" has been revoked", mCameraIdStr.c_str(),
251 getPackageName().c_str());
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800252 return PERMISSION_DENIED;
253 }
254
Shuzhen Wang2c656792020-04-13 17:36:49 -0700255 // If the calling Uid is trusted (a native service), the AppOpsManager could
256 // return MODE_IGNORED. Do not treat such case as error.
257 if (!mUidIsTrusted && res == AppOpsManager::MODE_IGNORED) {
Austin Borger2e772b82024-10-11 16:09:57 -0700258 ALOGI("Offline Camera %s: Access for \"%s\" has been restricted", mCameraIdStr.c_str(),
259 getPackageName().c_str());
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800260 // Return the same error as for device policy manager rejection
261 return -EACCES;
262 }
263 }
264
265 mOpsActive = true;
266
267 // Transition device state to OPEN
Austin Borger2e772b82024-10-11 16:09:57 -0700268 sCameraService->mUidPolicy->registerMonitorUid(getClientUid(), /*openCamera*/ true);
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800269
270 return OK;
271}
272
273status_t CameraOfflineSessionClient::finishCameraOps() {
274 ATRACE_CALL();
275
276 // Check if startCameraOps succeeded, and if so, finish the camera op
277 if (mOpsActive) {
278 // Notify app ops that the camera is available again
279 if (mAppOpsManager != nullptr) {
Austin Borger2e772b82024-10-11 16:09:57 -0700280 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
281 mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, getClientUid(),
282 toString16(getPackageName()));
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800283 mOpsActive = false;
284 }
285 }
286 // Always stop watching, even if no camera op is active
287 if (mOpsCallback != nullptr && mAppOpsManager != nullptr) {
288 mAppOpsManager->stopWatchingMode(mOpsCallback);
289 }
290 mOpsCallback.clear();
291
Austin Borger2e772b82024-10-11 16:09:57 -0700292 sCameraService->mUidPolicy->unregisterMonitorUid(getClientUid(), /*closeCamera*/ true);
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800293
294 return OK;
295}
296
Emilian Peev4697b642019-11-19 17:11:14 -0800297void CameraOfflineSessionClient::onResultAvailable(const CaptureResult& result) {
298 ATRACE_CALL();
299 ALOGV("%s", __FUNCTION__);
300
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800301 if (mRemoteCallback.get() != NULL) {
302 mRemoteCallback->onResultReceived(result.mMetadata, result.mResultExtras,
Emilian Peev4697b642019-11-19 17:11:14 -0800303 result.mPhysicalMetadatas);
304 }
305
306 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
307 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
308 }
309}
310
311void CameraOfflineSessionClient::notifyShutter(const CaptureResultExtras& resultExtras,
312 nsecs_t timestamp) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800313
314 if (mRemoteCallback.get() != nullptr) {
315 mRemoteCallback->onCaptureStarted(resultExtras, timestamp);
Emilian Peev4697b642019-11-19 17:11:14 -0800316 }
317
318 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
319 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
320 }
321}
322
Austin Borger4a870a32022-02-25 01:48:41 +0000323status_t CameraOfflineSessionClient::notifyActive(float maxPreviewFps __unused) {
Eino-Ville Talvala178e8232021-04-16 18:41:39 -0700324 return startCameraStreamingOps();
325}
326
Shuzhen Wang316781a2020-08-18 18:11:01 -0700327void CameraOfflineSessionClient::notifyIdle(
328 int64_t /*requestCount*/, int64_t /*resultErrorCount*/, bool /*deviceError*/,
Eino-Ville Talvalaffc186b2024-04-09 18:10:47 -0700329 std::pair<int32_t, int32_t> /*mostRequestedFpsRange*/,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700330 const std::vector<hardware::CameraStreamStats>& /*streamStats*/) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800331 if (mRemoteCallback.get() != nullptr) {
332 mRemoteCallback->onDeviceIdle();
333 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -0700334 finishCameraStreamingOps();
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800335}
336
Jing Mikec7f9b132023-03-12 11:12:04 +0800337void CameraOfflineSessionClient::notifyAutoFocus([[maybe_unused]] uint8_t newState,
338 [[maybe_unused]] int triggerId) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800339 ALOGV("%s: Autofocus state now %d, last trigger %d",
340 __FUNCTION__, newState, triggerId);
341}
342
Jing Mikec7f9b132023-03-12 11:12:04 +0800343void CameraOfflineSessionClient::notifyAutoExposure([[maybe_unused]] uint8_t newState,
344 [[maybe_unused]] int triggerId) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800345 ALOGV("%s: Autoexposure state now %d, last trigger %d",
346 __FUNCTION__, newState, triggerId);
347}
348
Jing Mikec7f9b132023-03-12 11:12:04 +0800349void CameraOfflineSessionClient::notifyAutoWhitebalance([[maybe_unused]] uint8_t newState,
350 [[maybe_unused]] int triggerId) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800351 ALOGV("%s: Auto-whitebalance state now %d, last trigger %d", __FUNCTION__, newState,
352 triggerId);
353}
354
355void CameraOfflineSessionClient::notifyPrepared(int /*streamId*/) {
356 ALOGE("%s: Unexpected stream prepare notification in offline mode!", __FUNCTION__);
357 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
358 CaptureResultExtras());
359}
360
361void CameraOfflineSessionClient::notifyRequestQueueEmpty() {
362 if (mRemoteCallback.get() != nullptr) {
363 mRemoteCallback->onRequestQueueEmpty();
364 }
365}
366
367void CameraOfflineSessionClient::notifyRepeatingRequestError(long /*lastFrameNumber*/) {
368 ALOGE("%s: Unexpected repeating request error in offline mode!", __FUNCTION__);
369 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
370 CaptureResultExtras());
371}
372
Austin Borgered99f642023-06-01 16:51:35 -0700373status_t CameraOfflineSessionClient::injectCamera(const std::string& injectedCamId,
Cliff Wud3a05312021-04-26 23:07:31 +0800374 sp<CameraProviderManager> manager) {
375 ALOGV("%s: This client doesn't support the injection camera. injectedCamId: %s providerPtr: %p",
Austin Borgered99f642023-06-01 16:51:35 -0700376 __FUNCTION__, injectedCamId.c_str(), manager.get());
Cliff Wud3a05312021-04-26 23:07:31 +0800377
378 return OK;
379}
380
381status_t CameraOfflineSessionClient::stopInjection() {
382 ALOGV("%s: This client doesn't support the injection camera.", __FUNCTION__);
383
384 return OK;
385}
386
malikakash22af94c2023-12-04 18:13:14 +0000387status_t CameraOfflineSessionClient::injectSessionParams(
388 const hardware::camera2::impl::CameraMetadataNative& sessionParams) {
389 ALOGV("%s: This client doesn't support the injecting session parameters camera.",
390 __FUNCTION__);
391 (void)sessionParams;
392 return OK;
393}
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800394// ----------------------------------------------------------------------------
395}; // namespace android