blob: fb4d2f7142b4000c4faf80254cf07f46cbe0dfc1 [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
Eino-Ville Talvala305cec62020-11-12 14:18:17 -0800109
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800110status_t CameraOfflineSessionClient::dump(int fd, const Vector<String16>& args) {
111 return BasicClient::dump(fd, args);
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800112}
113
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800114status_t CameraOfflineSessionClient::dumpClient(int fd, const Vector<String16>& args) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800115 String8 result;
116
117 result = " Offline session dump:\n";
118 write(fd, result.string(), result.size());
119
120 if (mOfflineSession.get() == nullptr) {
121 result = " *** Offline session is detached\n";
122 write(fd, result.string(), result.size());
123 return NO_ERROR;
124 }
125
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800126 mFrameProcessor->dump(fd, args);
127
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800128 auto res = mOfflineSession->dump(fd);
129 if (res != OK) {
130 result = String8::format(" Error dumping offline session: %s (%d)",
131 strerror(-res), res);
132 write(fd, result.string(), result.size());
133 }
134
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800135 return OK;
136}
137
Avichal Rakesh7e53cad2021-10-05 13:46:30 -0700138status_t CameraOfflineSessionClient::startWatchingTags(const String8 &tags, int outFd) {
139 return BasicClient::startWatchingTags(tags, outFd);
140}
141
142status_t CameraOfflineSessionClient::stopWatchingTags(int outFd) {
143 return BasicClient::stopWatchingTags(outFd);
144}
145
146status_t CameraOfflineSessionClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
147 return BasicClient::dumpWatchedEventsToVector(out);
148}
149
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800150binder::Status CameraOfflineSessionClient::disconnect() {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800151 Mutex::Autolock icl(mBinderSerializationLock);
152
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800153 binder::Status res = Status::ok();
154 if (mDisconnected) {
155 return res;
156 }
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800157 // Allow both client and the media server to disconnect at all times
158 int callingPid = CameraThreadState::getCallingPid();
159 if (callingPid != mClientPid &&
160 callingPid != mServicePid) {
161 return res;
162 }
163
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800164 mDisconnected = true;
165
166 sCameraService->removeByClient(this);
167 sCameraService->logDisconnectedOffline(mCameraIdStr, mClientPid, String8(mClientPackageName));
168
169 sp<IBinder> remote = getRemote();
170 if (remote != nullptr) {
171 remote->unlinkToDeath(sCameraService);
172 }
173
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800174 mFrameProcessor->removeListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
175 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
176 /*listener*/this);
177 mFrameProcessor->requestExit();
178 mFrameProcessor->join();
179
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800180 finishCameraOps();
181 ALOGI("%s: Disconnected client for offline camera %s for PID %d", __FUNCTION__,
182 mCameraIdStr.string(), mClientPid);
183
184 // client shouldn't be able to call into us anymore
185 mClientPid = 0;
186
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800187 if (mOfflineSession.get() != nullptr) {
188 auto ret = mOfflineSession->disconnect();
189 if (ret != OK) {
190 ALOGE("%s: Failed disconnecting from offline session %s (%d)", __FUNCTION__,
191 strerror(-ret), ret);
192 }
193 mOfflineSession = nullptr;
194 }
195
Emilian Peev4697b642019-11-19 17:11:14 -0800196 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
197 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
198 if (ret != OK) {
199 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
200 strerror(-ret), ret);
201 }
202 }
203 mCompositeStreamMap.clear();
204
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800205 return res;
206}
207
208void CameraOfflineSessionClient::notifyError(int32_t errorCode,
209 const CaptureResultExtras& resultExtras) {
210 // Thread safe. Don't bother locking.
Emilian Peev4697b642019-11-19 17:11:14 -0800211 // Composites can have multiple internal streams. Error notifications coming from such internal
212 // streams may need to remain within camera service.
213 bool skipClientNotification = false;
214 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
215 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode, resultExtras);
216 }
217
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800218 if ((mRemoteCallback.get() != nullptr) && (!skipClientNotification)) {
219 mRemoteCallback->onDeviceError(errorCode, resultExtras);
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800220 }
221}
222
223status_t CameraOfflineSessionClient::startCameraOps() {
224 ATRACE_CALL();
225 {
226 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
227 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
228 }
229
230 if (mAppOpsManager != nullptr) {
231 // Notify app ops that the camera is not available
232 mOpsCallback = new OpsCallback(this);
233 int32_t res;
234 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
235 mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA,
236 mClientPackageName, mOpsCallback);
237 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
238 res = mAppOpsManager->startOpNoThrow(AppOpsManager::OP_CAMERA,
239 mClientUid, mClientPackageName, /*startIfModeDefault*/ false);
240
241 if (res == AppOpsManager::MODE_ERRORED) {
242 ALOGI("Offline Camera %s: Access for \"%s\" has been revoked",
243 mCameraIdStr.string(), String8(mClientPackageName).string());
244 return PERMISSION_DENIED;
245 }
246
Shuzhen Wang2c656792020-04-13 17:36:49 -0700247 // If the calling Uid is trusted (a native service), the AppOpsManager could
248 // return MODE_IGNORED. Do not treat such case as error.
249 if (!mUidIsTrusted && res == AppOpsManager::MODE_IGNORED) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800250 ALOGI("Offline Camera %s: Access for \"%s\" has been restricted",
251 mCameraIdStr.string(), String8(mClientPackageName).string());
252 // Return the same error as for device policy manager rejection
253 return -EACCES;
254 }
255 }
256
257 mOpsActive = true;
258
259 // Transition device state to OPEN
260 sCameraService->mUidPolicy->registerMonitorUid(mClientUid);
261
262 return OK;
263}
264
265status_t CameraOfflineSessionClient::finishCameraOps() {
266 ATRACE_CALL();
267
268 // Check if startCameraOps succeeded, and if so, finish the camera op
269 if (mOpsActive) {
270 // Notify app ops that the camera is available again
271 if (mAppOpsManager != nullptr) {
272 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
273 mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, mClientUid,
274 mClientPackageName);
275 mOpsActive = false;
276 }
277 }
278 // Always stop watching, even if no camera op is active
279 if (mOpsCallback != nullptr && mAppOpsManager != nullptr) {
280 mAppOpsManager->stopWatchingMode(mOpsCallback);
281 }
282 mOpsCallback.clear();
283
284 sCameraService->mUidPolicy->unregisterMonitorUid(mClientUid);
285
286 return OK;
287}
288
Emilian Peev4697b642019-11-19 17:11:14 -0800289void CameraOfflineSessionClient::onResultAvailable(const CaptureResult& result) {
290 ATRACE_CALL();
291 ALOGV("%s", __FUNCTION__);
292
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800293 if (mRemoteCallback.get() != NULL) {
294 mRemoteCallback->onResultReceived(result.mMetadata, result.mResultExtras,
Emilian Peev4697b642019-11-19 17:11:14 -0800295 result.mPhysicalMetadatas);
296 }
297
298 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
299 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
300 }
301}
302
303void CameraOfflineSessionClient::notifyShutter(const CaptureResultExtras& resultExtras,
304 nsecs_t timestamp) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800305
306 if (mRemoteCallback.get() != nullptr) {
307 mRemoteCallback->onCaptureStarted(resultExtras, timestamp);
Emilian Peev4697b642019-11-19 17:11:14 -0800308 }
309
310 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
311 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
312 }
313}
314
Austin Borger4a870a32022-02-25 01:48:41 +0000315status_t CameraOfflineSessionClient::notifyActive(float maxPreviewFps __unused) {
Eino-Ville Talvala178e8232021-04-16 18:41:39 -0700316 return startCameraStreamingOps();
317}
318
Shuzhen Wang316781a2020-08-18 18:11:01 -0700319void CameraOfflineSessionClient::notifyIdle(
320 int64_t /*requestCount*/, int64_t /*resultErrorCount*/, bool /*deviceError*/,
321 const std::vector<hardware::CameraStreamStats>& /*streamStats*/) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800322 if (mRemoteCallback.get() != nullptr) {
323 mRemoteCallback->onDeviceIdle();
324 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -0700325 finishCameraStreamingOps();
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800326}
327
328void CameraOfflineSessionClient::notifyAutoFocus(uint8_t newState, int triggerId) {
329 (void)newState;
330 (void)triggerId;
331
332 ALOGV("%s: Autofocus state now %d, last trigger %d",
333 __FUNCTION__, newState, triggerId);
334}
335
336void CameraOfflineSessionClient::notifyAutoExposure(uint8_t newState, int triggerId) {
337 (void)newState;
338 (void)triggerId;
339
340 ALOGV("%s: Autoexposure state now %d, last trigger %d",
341 __FUNCTION__, newState, triggerId);
342}
343
344void CameraOfflineSessionClient::notifyAutoWhitebalance(uint8_t newState, int triggerId) {
345 (void)newState;
346 (void)triggerId;
347
348 ALOGV("%s: Auto-whitebalance state now %d, last trigger %d", __FUNCTION__, newState,
349 triggerId);
350}
351
352void CameraOfflineSessionClient::notifyPrepared(int /*streamId*/) {
353 ALOGE("%s: Unexpected stream prepare notification in offline mode!", __FUNCTION__);
354 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
355 CaptureResultExtras());
356}
357
358void CameraOfflineSessionClient::notifyRequestQueueEmpty() {
359 if (mRemoteCallback.get() != nullptr) {
360 mRemoteCallback->onRequestQueueEmpty();
361 }
362}
363
364void CameraOfflineSessionClient::notifyRepeatingRequestError(long /*lastFrameNumber*/) {
365 ALOGE("%s: Unexpected repeating request error in offline mode!", __FUNCTION__);
366 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
367 CaptureResultExtras());
368}
369
Cliff Wud3a05312021-04-26 23:07:31 +0800370status_t CameraOfflineSessionClient::injectCamera(const String8& injectedCamId,
371 sp<CameraProviderManager> manager) {
372 ALOGV("%s: This client doesn't support the injection camera. injectedCamId: %s providerPtr: %p",
373 __FUNCTION__, injectedCamId.string(), manager.get());
374
375 return OK;
376}
377
378status_t CameraOfflineSessionClient::stopInjection() {
379 ALOGV("%s: This client doesn't support the injection camera.", __FUNCTION__);
380
381 return OK;
382}
383
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800384// ----------------------------------------------------------------------------
385}; // namespace android