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