blob: 52d0020925ab444e8214b7e71060b1610c8e31dd [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());
52 mFrameProcessor->run(threadName.string());
53
54 mFrameProcessor->registerListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
55 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
56 /*listener*/this,
57 /*sendPartials*/true);
58
Emilian Peevd99c8ae2019-11-26 13:19:13 -080059 wp<NotificationListener> weakThis(this);
60 res = mOfflineSession->initialize(weakThis);
61 if (res != OK) {
62 ALOGE("%s: Camera %s: unable to initialize device: %s (%d)",
63 __FUNCTION__, mCameraIdStr.string(), strerror(-res), res);
64 return res;
65 }
66
Emilian Peevc0fe54c2020-03-11 14:05:07 -070067 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
68 mCompositeStreamMap.valueAt(i)->switchToOffline();
69 }
70
Emilian Peevb2bc5a42019-11-20 16:02:14 -080071 return OK;
72}
73
Ravneet74cd3732022-03-30 05:33:03 +000074status_t CameraOfflineSessionClient::setCameraServiceWatchdog(bool) {
75 return OK;
76}
77
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -080078status_t CameraOfflineSessionClient::setRotateAndCropOverride(uint8_t /*rotateAndCrop*/) {
79 // Since we're not submitting more capture requests, changes to rotateAndCrop override
80 // make no difference.
81 return OK;
82}
83
Eino-Ville Talvala305cec62020-11-12 14:18:17 -080084bool CameraOfflineSessionClient::supportsCameraMute() {
85 // Offline mode doesn't support muting
86 return false;
87}
88
89status_t CameraOfflineSessionClient::setCameraMute(bool) {
90 return INVALID_OPERATION;
91}
92
Shuzhen Wangabe5ea12022-12-15 22:38:07 -080093void CameraOfflineSessionClient::setStreamUseCaseOverrides(
94 const std::vector<int64_t>& /*useCaseOverrides*/) {
95}
96
97void CameraOfflineSessionClient::clearStreamUseCaseOverrides() {
98}
99
Eino-Ville Talvala305cec62020-11-12 14:18:17 -0800100
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800101status_t CameraOfflineSessionClient::dump(int fd, const Vector<String16>& args) {
102 return BasicClient::dump(fd, args);
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800103}
104
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800105status_t CameraOfflineSessionClient::dumpClient(int fd, const Vector<String16>& args) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800106 String8 result;
107
108 result = " Offline session dump:\n";
109 write(fd, result.string(), result.size());
110
111 if (mOfflineSession.get() == nullptr) {
112 result = " *** Offline session is detached\n";
113 write(fd, result.string(), result.size());
114 return NO_ERROR;
115 }
116
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800117 mFrameProcessor->dump(fd, args);
118
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800119 auto res = mOfflineSession->dump(fd);
120 if (res != OK) {
121 result = String8::format(" Error dumping offline session: %s (%d)",
122 strerror(-res), res);
123 write(fd, result.string(), result.size());
124 }
125
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800126 return OK;
127}
128
Avichal Rakesh7e53cad2021-10-05 13:46:30 -0700129status_t CameraOfflineSessionClient::startWatchingTags(const String8 &tags, int outFd) {
130 return BasicClient::startWatchingTags(tags, outFd);
131}
132
133status_t CameraOfflineSessionClient::stopWatchingTags(int outFd) {
134 return BasicClient::stopWatchingTags(outFd);
135}
136
137status_t CameraOfflineSessionClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
138 return BasicClient::dumpWatchedEventsToVector(out);
139}
140
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800141binder::Status CameraOfflineSessionClient::disconnect() {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800142 Mutex::Autolock icl(mBinderSerializationLock);
143
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800144 binder::Status res = Status::ok();
145 if (mDisconnected) {
146 return res;
147 }
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800148 // Allow both client and the media server to disconnect at all times
149 int callingPid = CameraThreadState::getCallingPid();
150 if (callingPid != mClientPid &&
151 callingPid != mServicePid) {
152 return res;
153 }
154
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800155 mDisconnected = true;
156
157 sCameraService->removeByClient(this);
158 sCameraService->logDisconnectedOffline(mCameraIdStr, mClientPid, String8(mClientPackageName));
159
160 sp<IBinder> remote = getRemote();
161 if (remote != nullptr) {
162 remote->unlinkToDeath(sCameraService);
163 }
164
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800165 mFrameProcessor->removeListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
166 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
167 /*listener*/this);
168 mFrameProcessor->requestExit();
169 mFrameProcessor->join();
170
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800171 finishCameraOps();
172 ALOGI("%s: Disconnected client for offline camera %s for PID %d", __FUNCTION__,
173 mCameraIdStr.string(), mClientPid);
174
175 // client shouldn't be able to call into us anymore
176 mClientPid = 0;
177
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800178 if (mOfflineSession.get() != nullptr) {
179 auto ret = mOfflineSession->disconnect();
180 if (ret != OK) {
181 ALOGE("%s: Failed disconnecting from offline session %s (%d)", __FUNCTION__,
182 strerror(-ret), ret);
183 }
184 mOfflineSession = nullptr;
185 }
186
Emilian Peev4697b642019-11-19 17:11:14 -0800187 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
188 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
189 if (ret != OK) {
190 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
191 strerror(-ret), ret);
192 }
193 }
194 mCompositeStreamMap.clear();
195
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800196 return res;
197}
198
199void CameraOfflineSessionClient::notifyError(int32_t errorCode,
200 const CaptureResultExtras& resultExtras) {
201 // Thread safe. Don't bother locking.
Emilian Peev4697b642019-11-19 17:11:14 -0800202 // Composites can have multiple internal streams. Error notifications coming from such internal
203 // streams may need to remain within camera service.
204 bool skipClientNotification = false;
205 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
206 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode, resultExtras);
207 }
208
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800209 if ((mRemoteCallback.get() != nullptr) && (!skipClientNotification)) {
210 mRemoteCallback->onDeviceError(errorCode, resultExtras);
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800211 }
212}
213
214status_t CameraOfflineSessionClient::startCameraOps() {
215 ATRACE_CALL();
216 {
217 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
218 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
219 }
220
221 if (mAppOpsManager != nullptr) {
222 // Notify app ops that the camera is not available
223 mOpsCallback = new OpsCallback(this);
224 int32_t res;
225 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
226 mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA,
227 mClientPackageName, mOpsCallback);
228 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
229 res = mAppOpsManager->startOpNoThrow(AppOpsManager::OP_CAMERA,
230 mClientUid, mClientPackageName, /*startIfModeDefault*/ false);
231
232 if (res == AppOpsManager::MODE_ERRORED) {
233 ALOGI("Offline Camera %s: Access for \"%s\" has been revoked",
234 mCameraIdStr.string(), String8(mClientPackageName).string());
235 return PERMISSION_DENIED;
236 }
237
Shuzhen Wang2c656792020-04-13 17:36:49 -0700238 // If the calling Uid is trusted (a native service), the AppOpsManager could
239 // return MODE_IGNORED. Do not treat such case as error.
240 if (!mUidIsTrusted && res == AppOpsManager::MODE_IGNORED) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800241 ALOGI("Offline Camera %s: Access for \"%s\" has been restricted",
242 mCameraIdStr.string(), String8(mClientPackageName).string());
243 // Return the same error as for device policy manager rejection
244 return -EACCES;
245 }
246 }
247
248 mOpsActive = true;
249
250 // Transition device state to OPEN
251 sCameraService->mUidPolicy->registerMonitorUid(mClientUid);
252
253 return OK;
254}
255
256status_t CameraOfflineSessionClient::finishCameraOps() {
257 ATRACE_CALL();
258
259 // Check if startCameraOps succeeded, and if so, finish the camera op
260 if (mOpsActive) {
261 // Notify app ops that the camera is available again
262 if (mAppOpsManager != nullptr) {
263 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
264 mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, mClientUid,
265 mClientPackageName);
266 mOpsActive = false;
267 }
268 }
269 // Always stop watching, even if no camera op is active
270 if (mOpsCallback != nullptr && mAppOpsManager != nullptr) {
271 mAppOpsManager->stopWatchingMode(mOpsCallback);
272 }
273 mOpsCallback.clear();
274
275 sCameraService->mUidPolicy->unregisterMonitorUid(mClientUid);
276
277 return OK;
278}
279
Emilian Peev4697b642019-11-19 17:11:14 -0800280void CameraOfflineSessionClient::onResultAvailable(const CaptureResult& result) {
281 ATRACE_CALL();
282 ALOGV("%s", __FUNCTION__);
283
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800284 if (mRemoteCallback.get() != NULL) {
285 mRemoteCallback->onResultReceived(result.mMetadata, result.mResultExtras,
Emilian Peev4697b642019-11-19 17:11:14 -0800286 result.mPhysicalMetadatas);
287 }
288
289 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
290 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
291 }
292}
293
294void CameraOfflineSessionClient::notifyShutter(const CaptureResultExtras& resultExtras,
295 nsecs_t timestamp) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800296
297 if (mRemoteCallback.get() != nullptr) {
298 mRemoteCallback->onCaptureStarted(resultExtras, timestamp);
Emilian Peev4697b642019-11-19 17:11:14 -0800299 }
300
301 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
302 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
303 }
304}
305
Austin Borger4a870a32022-02-25 01:48:41 +0000306status_t CameraOfflineSessionClient::notifyActive(float maxPreviewFps __unused) {
Eino-Ville Talvala178e8232021-04-16 18:41:39 -0700307 return startCameraStreamingOps();
308}
309
Shuzhen Wang316781a2020-08-18 18:11:01 -0700310void CameraOfflineSessionClient::notifyIdle(
311 int64_t /*requestCount*/, int64_t /*resultErrorCount*/, bool /*deviceError*/,
312 const std::vector<hardware::CameraStreamStats>& /*streamStats*/) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800313 if (mRemoteCallback.get() != nullptr) {
314 mRemoteCallback->onDeviceIdle();
315 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -0700316 finishCameraStreamingOps();
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800317}
318
319void CameraOfflineSessionClient::notifyAutoFocus(uint8_t newState, int triggerId) {
320 (void)newState;
321 (void)triggerId;
322
323 ALOGV("%s: Autofocus state now %d, last trigger %d",
324 __FUNCTION__, newState, triggerId);
325}
326
327void CameraOfflineSessionClient::notifyAutoExposure(uint8_t newState, int triggerId) {
328 (void)newState;
329 (void)triggerId;
330
331 ALOGV("%s: Autoexposure state now %d, last trigger %d",
332 __FUNCTION__, newState, triggerId);
333}
334
335void CameraOfflineSessionClient::notifyAutoWhitebalance(uint8_t newState, int triggerId) {
336 (void)newState;
337 (void)triggerId;
338
339 ALOGV("%s: Auto-whitebalance state now %d, last trigger %d", __FUNCTION__, newState,
340 triggerId);
341}
342
343void CameraOfflineSessionClient::notifyPrepared(int /*streamId*/) {
344 ALOGE("%s: Unexpected stream prepare notification in offline mode!", __FUNCTION__);
345 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
346 CaptureResultExtras());
347}
348
349void CameraOfflineSessionClient::notifyRequestQueueEmpty() {
350 if (mRemoteCallback.get() != nullptr) {
351 mRemoteCallback->onRequestQueueEmpty();
352 }
353}
354
355void CameraOfflineSessionClient::notifyRepeatingRequestError(long /*lastFrameNumber*/) {
356 ALOGE("%s: Unexpected repeating request error in offline mode!", __FUNCTION__);
357 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
358 CaptureResultExtras());
359}
360
Cliff Wud3a05312021-04-26 23:07:31 +0800361status_t CameraOfflineSessionClient::injectCamera(const String8& injectedCamId,
362 sp<CameraProviderManager> manager) {
363 ALOGV("%s: This client doesn't support the injection camera. injectedCamId: %s providerPtr: %p",
364 __FUNCTION__, injectedCamId.string(), manager.get());
365
366 return OK;
367}
368
369status_t CameraOfflineSessionClient::stopInjection() {
370 ALOGV("%s: This client doesn't support the injection camera.", __FUNCTION__);
371
372 return OK;
373}
374
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800375// ----------------------------------------------------------------------------
376}; // namespace android