blob: 2810667cdef41f2c038a1b684d42e63a0dc4114b [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
Ravneet74cd3732022-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
Shuzhen Wangabe5ea12022-12-15 22:38:07 -080088void CameraOfflineSessionClient::setStreamUseCaseOverrides(
89 const std::vector<int64_t>& /*useCaseOverrides*/) {
90}
91
92void CameraOfflineSessionClient::clearStreamUseCaseOverrides() {
93}
94
Emilian Peevd99c8ae2019-11-26 13:19:13 -080095status_t CameraOfflineSessionClient::dump(int fd, const Vector<String16>& args) {
96 return BasicClient::dump(fd, args);
Emilian Peevb2bc5a42019-11-20 16:02:14 -080097}
98
Emilian Peevfaa4bde2020-01-23 12:19:37 -080099status_t CameraOfflineSessionClient::dumpClient(int fd, const Vector<String16>& args) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800100 String8 result;
101
102 result = " Offline session dump:\n";
103 write(fd, result.string(), result.size());
104
105 if (mOfflineSession.get() == nullptr) {
106 result = " *** Offline session is detached\n";
107 write(fd, result.string(), result.size());
108 return NO_ERROR;
109 }
110
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800111 mFrameProcessor->dump(fd, args);
112
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800113 auto res = mOfflineSession->dump(fd);
114 if (res != OK) {
115 result = String8::format(" Error dumping offline session: %s (%d)",
116 strerror(-res), res);
117 write(fd, result.string(), result.size());
118 }
119
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800120 return OK;
121}
122
Avichal Rakesh7e53cad2021-10-05 13:46:30 -0700123status_t CameraOfflineSessionClient::startWatchingTags(const String8 &tags, int outFd) {
124 return BasicClient::startWatchingTags(tags, outFd);
125}
126
127status_t CameraOfflineSessionClient::stopWatchingTags(int outFd) {
128 return BasicClient::stopWatchingTags(outFd);
129}
130
131status_t CameraOfflineSessionClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
132 return BasicClient::dumpWatchedEventsToVector(out);
133}
134
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800135binder::Status CameraOfflineSessionClient::disconnect() {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800136 Mutex::Autolock icl(mBinderSerializationLock);
137
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800138 binder::Status res = Status::ok();
139 if (mDisconnected) {
140 return res;
141 }
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800142 // Allow both client and the media server to disconnect at all times
143 int callingPid = CameraThreadState::getCallingPid();
144 if (callingPid != mClientPid &&
145 callingPid != mServicePid) {
146 return res;
147 }
148
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800149 mDisconnected = true;
150
151 sCameraService->removeByClient(this);
152 sCameraService->logDisconnectedOffline(mCameraIdStr, mClientPid, String8(mClientPackageName));
153
154 sp<IBinder> remote = getRemote();
155 if (remote != nullptr) {
156 remote->unlinkToDeath(sCameraService);
157 }
158
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800159 mFrameProcessor->removeListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
160 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
161 /*listener*/this);
162 mFrameProcessor->requestExit();
163 mFrameProcessor->join();
164
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800165 finishCameraOps();
166 ALOGI("%s: Disconnected client for offline camera %s for PID %d", __FUNCTION__,
167 mCameraIdStr.string(), mClientPid);
168
169 // client shouldn't be able to call into us anymore
170 mClientPid = 0;
171
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800172 if (mOfflineSession.get() != nullptr) {
173 auto ret = mOfflineSession->disconnect();
174 if (ret != OK) {
175 ALOGE("%s: Failed disconnecting from offline session %s (%d)", __FUNCTION__,
176 strerror(-ret), ret);
177 }
178 mOfflineSession = nullptr;
179 }
180
Emilian Peev4697b642019-11-19 17:11:14 -0800181 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
182 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
183 if (ret != OK) {
184 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
185 strerror(-ret), ret);
186 }
187 }
188 mCompositeStreamMap.clear();
189
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800190 return res;
191}
192
193void CameraOfflineSessionClient::notifyError(int32_t errorCode,
194 const CaptureResultExtras& resultExtras) {
195 // Thread safe. Don't bother locking.
Emilian Peev4697b642019-11-19 17:11:14 -0800196 // Composites can have multiple internal streams. Error notifications coming from such internal
197 // streams may need to remain within camera service.
198 bool skipClientNotification = false;
199 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
200 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode, resultExtras);
201 }
202
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800203 if ((mRemoteCallback.get() != nullptr) && (!skipClientNotification)) {
204 mRemoteCallback->onDeviceError(errorCode, resultExtras);
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800205 }
206}
207
208status_t CameraOfflineSessionClient::startCameraOps() {
209 ATRACE_CALL();
210 {
211 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
212 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
213 }
214
215 if (mAppOpsManager != nullptr) {
216 // Notify app ops that the camera is not available
217 mOpsCallback = new OpsCallback(this);
218 int32_t res;
219 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
220 mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA,
221 mClientPackageName, mOpsCallback);
222 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
223 res = mAppOpsManager->startOpNoThrow(AppOpsManager::OP_CAMERA,
224 mClientUid, mClientPackageName, /*startIfModeDefault*/ false);
225
226 if (res == AppOpsManager::MODE_ERRORED) {
227 ALOGI("Offline Camera %s: Access for \"%s\" has been revoked",
228 mCameraIdStr.string(), String8(mClientPackageName).string());
229 return PERMISSION_DENIED;
230 }
231
Shuzhen Wang2c656792020-04-13 17:36:49 -0700232 // If the calling Uid is trusted (a native service), the AppOpsManager could
233 // return MODE_IGNORED. Do not treat such case as error.
234 if (!mUidIsTrusted && res == AppOpsManager::MODE_IGNORED) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800235 ALOGI("Offline Camera %s: Access for \"%s\" has been restricted",
236 mCameraIdStr.string(), String8(mClientPackageName).string());
237 // Return the same error as for device policy manager rejection
238 return -EACCES;
239 }
240 }
241
242 mOpsActive = true;
243
244 // Transition device state to OPEN
245 sCameraService->mUidPolicy->registerMonitorUid(mClientUid);
246
247 return OK;
248}
249
250status_t CameraOfflineSessionClient::finishCameraOps() {
251 ATRACE_CALL();
252
253 // Check if startCameraOps succeeded, and if so, finish the camera op
254 if (mOpsActive) {
255 // Notify app ops that the camera is available again
256 if (mAppOpsManager != nullptr) {
257 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
258 mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, mClientUid,
259 mClientPackageName);
260 mOpsActive = false;
261 }
262 }
263 // Always stop watching, even if no camera op is active
264 if (mOpsCallback != nullptr && mAppOpsManager != nullptr) {
265 mAppOpsManager->stopWatchingMode(mOpsCallback);
266 }
267 mOpsCallback.clear();
268
269 sCameraService->mUidPolicy->unregisterMonitorUid(mClientUid);
270
271 return OK;
272}
273
Emilian Peev4697b642019-11-19 17:11:14 -0800274void CameraOfflineSessionClient::onResultAvailable(const CaptureResult& result) {
275 ATRACE_CALL();
276 ALOGV("%s", __FUNCTION__);
277
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800278 if (mRemoteCallback.get() != NULL) {
279 mRemoteCallback->onResultReceived(result.mMetadata, result.mResultExtras,
Emilian Peev4697b642019-11-19 17:11:14 -0800280 result.mPhysicalMetadatas);
281 }
282
283 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
284 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
285 }
286}
287
288void CameraOfflineSessionClient::notifyShutter(const CaptureResultExtras& resultExtras,
289 nsecs_t timestamp) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800290
291 if (mRemoteCallback.get() != nullptr) {
292 mRemoteCallback->onCaptureStarted(resultExtras, timestamp);
Emilian Peev4697b642019-11-19 17:11:14 -0800293 }
294
295 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
296 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
297 }
298}
299
Austin Borger4a870a32022-02-25 01:48:41 +0000300status_t CameraOfflineSessionClient::notifyActive(float maxPreviewFps __unused) {
Eino-Ville Talvala178e8232021-04-16 18:41:39 -0700301 return startCameraStreamingOps();
302}
303
Shuzhen Wang316781a2020-08-18 18:11:01 -0700304void CameraOfflineSessionClient::notifyIdle(
305 int64_t /*requestCount*/, int64_t /*resultErrorCount*/, bool /*deviceError*/,
306 const std::vector<hardware::CameraStreamStats>& /*streamStats*/) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800307 if (mRemoteCallback.get() != nullptr) {
308 mRemoteCallback->onDeviceIdle();
309 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -0700310 finishCameraStreamingOps();
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800311}
312
313void CameraOfflineSessionClient::notifyAutoFocus(uint8_t newState, int triggerId) {
314 (void)newState;
315 (void)triggerId;
316
317 ALOGV("%s: Autofocus state now %d, last trigger %d",
318 __FUNCTION__, newState, triggerId);
319}
320
321void CameraOfflineSessionClient::notifyAutoExposure(uint8_t newState, int triggerId) {
322 (void)newState;
323 (void)triggerId;
324
325 ALOGV("%s: Autoexposure state now %d, last trigger %d",
326 __FUNCTION__, newState, triggerId);
327}
328
329void CameraOfflineSessionClient::notifyAutoWhitebalance(uint8_t newState, int triggerId) {
330 (void)newState;
331 (void)triggerId;
332
333 ALOGV("%s: Auto-whitebalance state now %d, last trigger %d", __FUNCTION__, newState,
334 triggerId);
335}
336
337void CameraOfflineSessionClient::notifyPrepared(int /*streamId*/) {
338 ALOGE("%s: Unexpected stream prepare notification in offline mode!", __FUNCTION__);
339 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
340 CaptureResultExtras());
341}
342
343void CameraOfflineSessionClient::notifyRequestQueueEmpty() {
344 if (mRemoteCallback.get() != nullptr) {
345 mRemoteCallback->onRequestQueueEmpty();
346 }
347}
348
349void CameraOfflineSessionClient::notifyRepeatingRequestError(long /*lastFrameNumber*/) {
350 ALOGE("%s: Unexpected repeating request error in offline mode!", __FUNCTION__);
351 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
352 CaptureResultExtras());
353}
354
Cliff Wud3a05312021-04-26 23:07:31 +0800355status_t CameraOfflineSessionClient::injectCamera(const String8& injectedCamId,
356 sp<CameraProviderManager> manager) {
357 ALOGV("%s: This client doesn't support the injection camera. injectedCamId: %s providerPtr: %p",
358 __FUNCTION__, injectedCamId.string(), manager.get());
359
360 return OK;
361}
362
363status_t CameraOfflineSessionClient::stopInjection() {
364 ALOGV("%s: This client doesn't support the injection camera.", __FUNCTION__);
365
366 return OK;
367}
368
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800369// ----------------------------------------------------------------------------
370}; // namespace android