blob: 1edfbf9b11908d9cd6119fcdb7fd37eacc2558cf [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 Peevb2bc5a42019-11-20 16:02:14 -080062 return OK;
63}
64
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -080065status_t CameraOfflineSessionClient::setRotateAndCropOverride(uint8_t /*rotateAndCrop*/) {
66 // Since we're not submitting more capture requests, changes to rotateAndCrop override
67 // make no difference.
68 return OK;
69}
70
Emilian Peevd99c8ae2019-11-26 13:19:13 -080071status_t CameraOfflineSessionClient::dump(int fd, const Vector<String16>& args) {
72 return BasicClient::dump(fd, args);
Emilian Peevb2bc5a42019-11-20 16:02:14 -080073}
74
Emilian Peevfaa4bde2020-01-23 12:19:37 -080075status_t CameraOfflineSessionClient::dumpClient(int fd, const Vector<String16>& args) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -080076 String8 result;
77
78 result = " Offline session dump:\n";
79 write(fd, result.string(), result.size());
80
81 if (mOfflineSession.get() == nullptr) {
82 result = " *** Offline session is detached\n";
83 write(fd, result.string(), result.size());
84 return NO_ERROR;
85 }
86
Emilian Peevfaa4bde2020-01-23 12:19:37 -080087 mFrameProcessor->dump(fd, args);
88
Emilian Peevd99c8ae2019-11-26 13:19:13 -080089 auto res = mOfflineSession->dump(fd);
90 if (res != OK) {
91 result = String8::format(" Error dumping offline session: %s (%d)",
92 strerror(-res), res);
93 write(fd, result.string(), result.size());
94 }
95
Emilian Peevb2bc5a42019-11-20 16:02:14 -080096 return OK;
97}
98
99binder::Status CameraOfflineSessionClient::disconnect() {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800100 Mutex::Autolock icl(mBinderSerializationLock);
101
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800102 binder::Status res = Status::ok();
103 if (mDisconnected) {
104 return res;
105 }
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800106 // Allow both client and the media server to disconnect at all times
107 int callingPid = CameraThreadState::getCallingPid();
108 if (callingPid != mClientPid &&
109 callingPid != mServicePid) {
110 return res;
111 }
112
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800113 mDisconnected = true;
114
115 sCameraService->removeByClient(this);
116 sCameraService->logDisconnectedOffline(mCameraIdStr, mClientPid, String8(mClientPackageName));
117
118 sp<IBinder> remote = getRemote();
119 if (remote != nullptr) {
120 remote->unlinkToDeath(sCameraService);
121 }
122
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800123 mFrameProcessor->removeListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
124 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
125 /*listener*/this);
126 mFrameProcessor->requestExit();
127 mFrameProcessor->join();
128
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800129 finishCameraOps();
130 ALOGI("%s: Disconnected client for offline camera %s for PID %d", __FUNCTION__,
131 mCameraIdStr.string(), mClientPid);
132
133 // client shouldn't be able to call into us anymore
134 mClientPid = 0;
135
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800136 if (mOfflineSession.get() != nullptr) {
137 auto ret = mOfflineSession->disconnect();
138 if (ret != OK) {
139 ALOGE("%s: Failed disconnecting from offline session %s (%d)", __FUNCTION__,
140 strerror(-ret), ret);
141 }
142 mOfflineSession = nullptr;
143 }
144
Emilian Peev4697b642019-11-19 17:11:14 -0800145 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
146 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
147 if (ret != OK) {
148 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
149 strerror(-ret), ret);
150 }
151 }
152 mCompositeStreamMap.clear();
153
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800154 return res;
155}
156
157void CameraOfflineSessionClient::notifyError(int32_t errorCode,
158 const CaptureResultExtras& resultExtras) {
159 // Thread safe. Don't bother locking.
Emilian Peev4697b642019-11-19 17:11:14 -0800160 // Composites can have multiple internal streams. Error notifications coming from such internal
161 // streams may need to remain within camera service.
162 bool skipClientNotification = false;
163 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
164 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode, resultExtras);
165 }
166
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800167 if ((mRemoteCallback.get() != nullptr) && (!skipClientNotification)) {
168 mRemoteCallback->onDeviceError(errorCode, resultExtras);
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800169 }
170}
171
172status_t CameraOfflineSessionClient::startCameraOps() {
173 ATRACE_CALL();
174 {
175 ALOGV("%s: Start camera ops, package name = %s, client UID = %d",
176 __FUNCTION__, String8(mClientPackageName).string(), mClientUid);
177 }
178
179 if (mAppOpsManager != nullptr) {
180 // Notify app ops that the camera is not available
181 mOpsCallback = new OpsCallback(this);
182 int32_t res;
183 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
184 mAppOpsManager->startWatchingMode(AppOpsManager::OP_CAMERA,
185 mClientPackageName, mOpsCallback);
186 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
187 res = mAppOpsManager->startOpNoThrow(AppOpsManager::OP_CAMERA,
188 mClientUid, mClientPackageName, /*startIfModeDefault*/ false);
189
190 if (res == AppOpsManager::MODE_ERRORED) {
191 ALOGI("Offline Camera %s: Access for \"%s\" has been revoked",
192 mCameraIdStr.string(), String8(mClientPackageName).string());
193 return PERMISSION_DENIED;
194 }
195
196 if (res == AppOpsManager::MODE_IGNORED) {
197 ALOGI("Offline Camera %s: Access for \"%s\" has been restricted",
198 mCameraIdStr.string(), String8(mClientPackageName).string());
199 // Return the same error as for device policy manager rejection
200 return -EACCES;
201 }
202 }
203
204 mOpsActive = true;
205
206 // Transition device state to OPEN
207 sCameraService->mUidPolicy->registerMonitorUid(mClientUid);
208
209 return OK;
210}
211
212status_t CameraOfflineSessionClient::finishCameraOps() {
213 ATRACE_CALL();
214
215 // Check if startCameraOps succeeded, and if so, finish the camera op
216 if (mOpsActive) {
217 // Notify app ops that the camera is available again
218 if (mAppOpsManager != nullptr) {
219 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
220 mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, mClientUid,
221 mClientPackageName);
222 mOpsActive = false;
223 }
224 }
225 // Always stop watching, even if no camera op is active
226 if (mOpsCallback != nullptr && mAppOpsManager != nullptr) {
227 mAppOpsManager->stopWatchingMode(mOpsCallback);
228 }
229 mOpsCallback.clear();
230
231 sCameraService->mUidPolicy->unregisterMonitorUid(mClientUid);
232
233 return OK;
234}
235
Emilian Peev4697b642019-11-19 17:11:14 -0800236void CameraOfflineSessionClient::onResultAvailable(const CaptureResult& result) {
237 ATRACE_CALL();
238 ALOGV("%s", __FUNCTION__);
239
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800240 if (mRemoteCallback.get() != NULL) {
241 mRemoteCallback->onResultReceived(result.mMetadata, result.mResultExtras,
Emilian Peev4697b642019-11-19 17:11:14 -0800242 result.mPhysicalMetadatas);
243 }
244
245 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
246 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
247 }
248}
249
250void CameraOfflineSessionClient::notifyShutter(const CaptureResultExtras& resultExtras,
251 nsecs_t timestamp) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800252
253 if (mRemoteCallback.get() != nullptr) {
254 mRemoteCallback->onCaptureStarted(resultExtras, timestamp);
Emilian Peev4697b642019-11-19 17:11:14 -0800255 }
256
257 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
258 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
259 }
260}
261
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800262void CameraOfflineSessionClient::notifyIdle() {
263 if (mRemoteCallback.get() != nullptr) {
264 mRemoteCallback->onDeviceIdle();
265 }
266}
267
268void CameraOfflineSessionClient::notifyAutoFocus(uint8_t newState, int triggerId) {
269 (void)newState;
270 (void)triggerId;
271
272 ALOGV("%s: Autofocus state now %d, last trigger %d",
273 __FUNCTION__, newState, triggerId);
274}
275
276void CameraOfflineSessionClient::notifyAutoExposure(uint8_t newState, int triggerId) {
277 (void)newState;
278 (void)triggerId;
279
280 ALOGV("%s: Autoexposure state now %d, last trigger %d",
281 __FUNCTION__, newState, triggerId);
282}
283
284void CameraOfflineSessionClient::notifyAutoWhitebalance(uint8_t newState, int triggerId) {
285 (void)newState;
286 (void)triggerId;
287
288 ALOGV("%s: Auto-whitebalance state now %d, last trigger %d", __FUNCTION__, newState,
289 triggerId);
290}
291
292void CameraOfflineSessionClient::notifyPrepared(int /*streamId*/) {
293 ALOGE("%s: Unexpected stream prepare notification in offline mode!", __FUNCTION__);
294 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
295 CaptureResultExtras());
296}
297
298void CameraOfflineSessionClient::notifyRequestQueueEmpty() {
299 if (mRemoteCallback.get() != nullptr) {
300 mRemoteCallback->onRequestQueueEmpty();
301 }
302}
303
304void CameraOfflineSessionClient::notifyRepeatingRequestError(long /*lastFrameNumber*/) {
305 ALOGE("%s: Unexpected repeating request error in offline mode!", __FUNCTION__);
306 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
307 CaptureResultExtras());
308}
309
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800310// ----------------------------------------------------------------------------
311}; // namespace android