blob: 237c24bb0097c70069844dee8a5515f8694a4e48 [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
Emilian Peevd99c8ae2019-11-26 13:19:13 -080075status_t CameraOfflineSessionClient::dump(int fd, const Vector<String16>& args) {
76 return BasicClient::dump(fd, args);
Emilian Peevb2bc5a42019-11-20 16:02:14 -080077}
78
Emilian Peevfaa4bde2020-01-23 12:19:37 -080079status_t CameraOfflineSessionClient::dumpClient(int fd, const Vector<String16>& args) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -080080 String8 result;
81
82 result = " Offline session dump:\n";
83 write(fd, result.string(), result.size());
84
85 if (mOfflineSession.get() == nullptr) {
86 result = " *** Offline session is detached\n";
87 write(fd, result.string(), result.size());
88 return NO_ERROR;
89 }
90
Emilian Peevfaa4bde2020-01-23 12:19:37 -080091 mFrameProcessor->dump(fd, args);
92
Emilian Peevd99c8ae2019-11-26 13:19:13 -080093 auto res = mOfflineSession->dump(fd);
94 if (res != OK) {
95 result = String8::format(" Error dumping offline session: %s (%d)",
96 strerror(-res), res);
97 write(fd, result.string(), result.size());
98 }
99
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800100 return OK;
101}
102
103binder::Status CameraOfflineSessionClient::disconnect() {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800104 Mutex::Autolock icl(mBinderSerializationLock);
105
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800106 binder::Status res = Status::ok();
107 if (mDisconnected) {
108 return res;
109 }
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800110 // Allow both client and the media server to disconnect at all times
111 int callingPid = CameraThreadState::getCallingPid();
112 if (callingPid != mClientPid &&
113 callingPid != mServicePid) {
114 return res;
115 }
116
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800117 mDisconnected = true;
118
119 sCameraService->removeByClient(this);
120 sCameraService->logDisconnectedOffline(mCameraIdStr, mClientPid, String8(mClientPackageName));
121
122 sp<IBinder> remote = getRemote();
123 if (remote != nullptr) {
124 remote->unlinkToDeath(sCameraService);
125 }
126
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800127 mFrameProcessor->removeListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
128 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
129 /*listener*/this);
130 mFrameProcessor->requestExit();
131 mFrameProcessor->join();
132
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800133 finishCameraOps();
134 ALOGI("%s: Disconnected client for offline camera %s for PID %d", __FUNCTION__,
135 mCameraIdStr.string(), mClientPid);
136
137 // client shouldn't be able to call into us anymore
138 mClientPid = 0;
139
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800140 if (mOfflineSession.get() != nullptr) {
141 auto ret = mOfflineSession->disconnect();
142 if (ret != OK) {
143 ALOGE("%s: Failed disconnecting from offline session %s (%d)", __FUNCTION__,
144 strerror(-ret), ret);
145 }
146 mOfflineSession = nullptr;
147 }
148
Emilian Peev4697b642019-11-19 17:11:14 -0800149 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
150 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
151 if (ret != OK) {
152 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
153 strerror(-ret), ret);
154 }
155 }
156 mCompositeStreamMap.clear();
157
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800158 return res;
159}
160
161void CameraOfflineSessionClient::notifyError(int32_t errorCode,
162 const CaptureResultExtras& resultExtras) {
163 // Thread safe. Don't bother locking.
Emilian Peev4697b642019-11-19 17:11:14 -0800164 // Composites can have multiple internal streams. Error notifications coming from such internal
165 // streams may need to remain within camera service.
166 bool skipClientNotification = false;
167 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
168 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode, resultExtras);
169 }
170
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800171 if ((mRemoteCallback.get() != nullptr) && (!skipClientNotification)) {
172 mRemoteCallback->onDeviceError(errorCode, resultExtras);
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800173 }
174}
175
176status_t CameraOfflineSessionClient::startCameraOps() {
177 ATRACE_CALL();
178 {
179 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
180 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
181 }
182
183 if (mAppOpsManager != nullptr) {
184 // Notify app ops that the camera is not available
185 mOpsCallback = new OpsCallback(this);
186 int32_t res;
187 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
188 mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA,
189 mClientPackageName, mOpsCallback);
190 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
191 res = mAppOpsManager->startOpNoThrow(AppOpsManager::OP_CAMERA,
192 mClientUid, mClientPackageName, /*startIfModeDefault*/ false);
193
194 if (res == AppOpsManager::MODE_ERRORED) {
195 ALOGI("Offline Camera %s: Access for \"%s\" has been revoked",
196 mCameraIdStr.string(), String8(mClientPackageName).string());
197 return PERMISSION_DENIED;
198 }
199
Shuzhen Wang2c656792020-04-13 17:36:49 -0700200 // If the calling Uid is trusted (a native service), the AppOpsManager could
201 // return MODE_IGNORED. Do not treat such case as error.
202 if (!mUidIsTrusted && res == AppOpsManager::MODE_IGNORED) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800203 ALOGI("Offline Camera %s: Access for \"%s\" has been restricted",
204 mCameraIdStr.string(), String8(mClientPackageName).string());
205 // Return the same error as for device policy manager rejection
206 return -EACCES;
207 }
208 }
209
210 mOpsActive = true;
211
212 // Transition device state to OPEN
213 sCameraService->mUidPolicy->registerMonitorUid(mClientUid);
214
215 return OK;
216}
217
218status_t CameraOfflineSessionClient::finishCameraOps() {
219 ATRACE_CALL();
220
221 // Check if startCameraOps succeeded, and if so, finish the camera op
222 if (mOpsActive) {
223 // Notify app ops that the camera is available again
224 if (mAppOpsManager != nullptr) {
225 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
226 mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, mClientUid,
227 mClientPackageName);
228 mOpsActive = false;
229 }
230 }
231 // Always stop watching, even if no camera op is active
232 if (mOpsCallback != nullptr && mAppOpsManager != nullptr) {
233 mAppOpsManager->stopWatchingMode(mOpsCallback);
234 }
235 mOpsCallback.clear();
236
237 sCameraService->mUidPolicy->unregisterMonitorUid(mClientUid);
238
239 return OK;
240}
241
Emilian Peev4697b642019-11-19 17:11:14 -0800242void CameraOfflineSessionClient::onResultAvailable(const CaptureResult& result) {
243 ATRACE_CALL();
244 ALOGV("%s", __FUNCTION__);
245
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800246 if (mRemoteCallback.get() != NULL) {
247 mRemoteCallback->onResultReceived(result.mMetadata, result.mResultExtras,
Emilian Peev4697b642019-11-19 17:11:14 -0800248 result.mPhysicalMetadatas);
249 }
250
251 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
252 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
253 }
254}
255
256void CameraOfflineSessionClient::notifyShutter(const CaptureResultExtras& resultExtras,
257 nsecs_t timestamp) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800258
259 if (mRemoteCallback.get() != nullptr) {
260 mRemoteCallback->onCaptureStarted(resultExtras, timestamp);
Emilian Peev4697b642019-11-19 17:11:14 -0800261 }
262
263 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
264 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
265 }
266}
267
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800268void CameraOfflineSessionClient::notifyIdle() {
269 if (mRemoteCallback.get() != nullptr) {
270 mRemoteCallback->onDeviceIdle();
271 }
272}
273
274void CameraOfflineSessionClient::notifyAutoFocus(uint8_t newState, int triggerId) {
275 (void)newState;
276 (void)triggerId;
277
278 ALOGV("%s: Autofocus state now %d, last trigger %d",
279 __FUNCTION__, newState, triggerId);
280}
281
282void CameraOfflineSessionClient::notifyAutoExposure(uint8_t newState, int triggerId) {
283 (void)newState;
284 (void)triggerId;
285
286 ALOGV("%s: Autoexposure state now %d, last trigger %d",
287 __FUNCTION__, newState, triggerId);
288}
289
290void CameraOfflineSessionClient::notifyAutoWhitebalance(uint8_t newState, int triggerId) {
291 (void)newState;
292 (void)triggerId;
293
294 ALOGV("%s: Auto-whitebalance state now %d, last trigger %d", __FUNCTION__, newState,
295 triggerId);
296}
297
298void CameraOfflineSessionClient::notifyPrepared(int /*streamId*/) {
299 ALOGE("%s: Unexpected stream prepare notification in offline mode!", __FUNCTION__);
300 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
301 CaptureResultExtras());
302}
303
304void CameraOfflineSessionClient::notifyRequestQueueEmpty() {
305 if (mRemoteCallback.get() != nullptr) {
306 mRemoteCallback->onRequestQueueEmpty();
307 }
308}
309
310void CameraOfflineSessionClient::notifyRepeatingRequestError(long /*lastFrameNumber*/) {
311 ALOGE("%s: Unexpected repeating request error in offline mode!", __FUNCTION__);
312 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
313 CaptureResultExtras());
314}
315
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800316// ----------------------------------------------------------------------------
317}; // namespace android