blob: 10fa33f830e4bffc9c8d6f2a2b9b8f75922d1277 [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
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -080069status_t CameraOfflineSessionClient::setRotateAndCropOverride(uint8_t /*rotateAndCrop*/) {
70 // Since we're not submitting more capture requests, changes to rotateAndCrop override
71 // make no difference.
72 return OK;
73}
74
Eino-Ville Talvala305cec62020-11-12 14:18:17 -080075bool CameraOfflineSessionClient::supportsCameraMute() {
76 // Offline mode doesn't support muting
77 return false;
78}
79
80status_t CameraOfflineSessionClient::setCameraMute(bool) {
81 return INVALID_OPERATION;
82}
83
84
Emilian Peevd99c8ae2019-11-26 13:19:13 -080085status_t CameraOfflineSessionClient::dump(int fd, const Vector<String16>& args) {
86 return BasicClient::dump(fd, args);
Emilian Peevb2bc5a42019-11-20 16:02:14 -080087}
88
Emilian Peevfaa4bde2020-01-23 12:19:37 -080089status_t CameraOfflineSessionClient::dumpClient(int fd, const Vector<String16>& args) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -080090 String8 result;
91
92 result = " Offline session dump:\n";
93 write(fd, result.string(), result.size());
94
95 if (mOfflineSession.get() == nullptr) {
96 result = " *** Offline session is detached\n";
97 write(fd, result.string(), result.size());
98 return NO_ERROR;
99 }
100
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800101 mFrameProcessor->dump(fd, args);
102
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800103 auto res = mOfflineSession->dump(fd);
104 if (res != OK) {
105 result = String8::format(" Error dumping offline session: %s (%d)",
106 strerror(-res), res);
107 write(fd, result.string(), result.size());
108 }
109
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800110 return OK;
111}
112
Avichal Rakesh7e53cad2021-10-05 13:46:30 -0700113status_t CameraOfflineSessionClient::startWatchingTags(const String8 &tags, int outFd) {
114 return BasicClient::startWatchingTags(tags, outFd);
115}
116
117status_t CameraOfflineSessionClient::stopWatchingTags(int outFd) {
118 return BasicClient::stopWatchingTags(outFd);
119}
120
121status_t CameraOfflineSessionClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
122 return BasicClient::dumpWatchedEventsToVector(out);
123}
124
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800125binder::Status CameraOfflineSessionClient::disconnect() {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800126 Mutex::Autolock icl(mBinderSerializationLock);
127
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800128 binder::Status res = Status::ok();
129 if (mDisconnected) {
130 return res;
131 }
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800132 // Allow both client and the media server to disconnect at all times
133 int callingPid = CameraThreadState::getCallingPid();
134 if (callingPid != mClientPid &&
135 callingPid != mServicePid) {
136 return res;
137 }
138
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800139 mDisconnected = true;
140
141 sCameraService->removeByClient(this);
142 sCameraService->logDisconnectedOffline(mCameraIdStr, mClientPid, String8(mClientPackageName));
143
144 sp<IBinder> remote = getRemote();
145 if (remote != nullptr) {
146 remote->unlinkToDeath(sCameraService);
147 }
148
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800149 mFrameProcessor->removeListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
150 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
151 /*listener*/this);
152 mFrameProcessor->requestExit();
153 mFrameProcessor->join();
154
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800155 finishCameraOps();
156 ALOGI("%s: Disconnected client for offline camera %s for PID %d", __FUNCTION__,
157 mCameraIdStr.string(), mClientPid);
158
159 // client shouldn't be able to call into us anymore
160 mClientPid = 0;
161
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800162 if (mOfflineSession.get() != nullptr) {
163 auto ret = mOfflineSession->disconnect();
164 if (ret != OK) {
165 ALOGE("%s: Failed disconnecting from offline session %s (%d)", __FUNCTION__,
166 strerror(-ret), ret);
167 }
168 mOfflineSession = nullptr;
169 }
170
Emilian Peev4697b642019-11-19 17:11:14 -0800171 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
172 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
173 if (ret != OK) {
174 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
175 strerror(-ret), ret);
176 }
177 }
178 mCompositeStreamMap.clear();
179
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800180 return res;
181}
182
183void CameraOfflineSessionClient::notifyError(int32_t errorCode,
184 const CaptureResultExtras& resultExtras) {
185 // Thread safe. Don't bother locking.
Emilian Peev4697b642019-11-19 17:11:14 -0800186 // Composites can have multiple internal streams. Error notifications coming from such internal
187 // streams may need to remain within camera service.
188 bool skipClientNotification = false;
189 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
190 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode, resultExtras);
191 }
192
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800193 if ((mRemoteCallback.get() != nullptr) && (!skipClientNotification)) {
194 mRemoteCallback->onDeviceError(errorCode, resultExtras);
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800195 }
196}
197
198status_t CameraOfflineSessionClient::startCameraOps() {
199 ATRACE_CALL();
200 {
201 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
202 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
203 }
204
205 if (mAppOpsManager != nullptr) {
206 // Notify app ops that the camera is not available
207 mOpsCallback = new OpsCallback(this);
208 int32_t res;
209 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
210 mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA,
211 mClientPackageName, mOpsCallback);
212 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
213 res = mAppOpsManager->startOpNoThrow(AppOpsManager::OP_CAMERA,
214 mClientUid, mClientPackageName, /*startIfModeDefault*/ false);
215
216 if (res == AppOpsManager::MODE_ERRORED) {
217 ALOGI("Offline Camera %s: Access for \"%s\" has been revoked",
218 mCameraIdStr.string(), String8(mClientPackageName).string());
219 return PERMISSION_DENIED;
220 }
221
Shuzhen Wang2c656792020-04-13 17:36:49 -0700222 // If the calling Uid is trusted (a native service), the AppOpsManager could
223 // return MODE_IGNORED. Do not treat such case as error.
224 if (!mUidIsTrusted && res == AppOpsManager::MODE_IGNORED) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800225 ALOGI("Offline Camera %s: Access for \"%s\" has been restricted",
226 mCameraIdStr.string(), String8(mClientPackageName).string());
227 // Return the same error as for device policy manager rejection
228 return -EACCES;
229 }
230 }
231
232 mOpsActive = true;
233
234 // Transition device state to OPEN
235 sCameraService->mUidPolicy->registerMonitorUid(mClientUid);
236
237 return OK;
238}
239
240status_t CameraOfflineSessionClient::finishCameraOps() {
241 ATRACE_CALL();
242
243 // Check if startCameraOps succeeded, and if so, finish the camera op
244 if (mOpsActive) {
245 // Notify app ops that the camera is available again
246 if (mAppOpsManager != nullptr) {
247 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
248 mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, mClientUid,
249 mClientPackageName);
250 mOpsActive = false;
251 }
252 }
253 // Always stop watching, even if no camera op is active
254 if (mOpsCallback != nullptr && mAppOpsManager != nullptr) {
255 mAppOpsManager->stopWatchingMode(mOpsCallback);
256 }
257 mOpsCallback.clear();
258
259 sCameraService->mUidPolicy->unregisterMonitorUid(mClientUid);
260
261 return OK;
262}
263
Emilian Peev4697b642019-11-19 17:11:14 -0800264void CameraOfflineSessionClient::onResultAvailable(const CaptureResult& result) {
265 ATRACE_CALL();
266 ALOGV("%s", __FUNCTION__);
267
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800268 if (mRemoteCallback.get() != NULL) {
269 mRemoteCallback->onResultReceived(result.mMetadata, result.mResultExtras,
Emilian Peev4697b642019-11-19 17:11:14 -0800270 result.mPhysicalMetadatas);
271 }
272
273 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
274 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
275 }
276}
277
278void CameraOfflineSessionClient::notifyShutter(const CaptureResultExtras& resultExtras,
279 nsecs_t timestamp) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800280
281 if (mRemoteCallback.get() != nullptr) {
282 mRemoteCallback->onCaptureStarted(resultExtras, timestamp);
Emilian Peev4697b642019-11-19 17:11:14 -0800283 }
284
285 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
286 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
287 }
288}
289
Eino-Ville Talvala178e8232021-04-16 18:41:39 -0700290status_t CameraOfflineSessionClient::notifyActive() {
291 return startCameraStreamingOps();
292}
293
Shuzhen Wang316781a2020-08-18 18:11:01 -0700294void CameraOfflineSessionClient::notifyIdle(
295 int64_t /*requestCount*/, int64_t /*resultErrorCount*/, bool /*deviceError*/,
296 const std::vector<hardware::CameraStreamStats>& /*streamStats*/) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800297 if (mRemoteCallback.get() != nullptr) {
298 mRemoteCallback->onDeviceIdle();
299 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -0700300 finishCameraStreamingOps();
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800301}
302
303void CameraOfflineSessionClient::notifyAutoFocus(uint8_t newState, int triggerId) {
304 (void)newState;
305 (void)triggerId;
306
307 ALOGV("%s: Autofocus state now %d, last trigger %d",
308 __FUNCTION__, newState, triggerId);
309}
310
311void CameraOfflineSessionClient::notifyAutoExposure(uint8_t newState, int triggerId) {
312 (void)newState;
313 (void)triggerId;
314
315 ALOGV("%s: Autoexposure state now %d, last trigger %d",
316 __FUNCTION__, newState, triggerId);
317}
318
319void CameraOfflineSessionClient::notifyAutoWhitebalance(uint8_t newState, int triggerId) {
320 (void)newState;
321 (void)triggerId;
322
323 ALOGV("%s: Auto-whitebalance state now %d, last trigger %d", __FUNCTION__, newState,
324 triggerId);
325}
326
327void CameraOfflineSessionClient::notifyPrepared(int /*streamId*/) {
328 ALOGE("%s: Unexpected stream prepare notification in offline mode!", __FUNCTION__);
329 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
330 CaptureResultExtras());
331}
332
333void CameraOfflineSessionClient::notifyRequestQueueEmpty() {
334 if (mRemoteCallback.get() != nullptr) {
335 mRemoteCallback->onRequestQueueEmpty();
336 }
337}
338
339void CameraOfflineSessionClient::notifyRepeatingRequestError(long /*lastFrameNumber*/) {
340 ALOGE("%s: Unexpected repeating request error in offline mode!", __FUNCTION__);
341 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
342 CaptureResultExtras());
343}
344
Cliff Wud3a05312021-04-26 23:07:31 +0800345status_t CameraOfflineSessionClient::injectCamera(const String8& injectedCamId,
346 sp<CameraProviderManager> manager) {
347 ALOGV("%s: This client doesn't support the injection camera. injectedCamId: %s providerPtr: %p",
348 __FUNCTION__, injectedCamId.string(), manager.get());
349
350 return OK;
351}
352
353status_t CameraOfflineSessionClient::stopInjection() {
354 ALOGV("%s: This client doesn't support the injection camera.", __FUNCTION__);
355
356 return OK;
357}
358
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800359// ----------------------------------------------------------------------------
360}; // namespace android