blob: 8f20685c593969c0c8836e4011950a5ff1bf5716 [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
200 if (res == AppOpsManager::MODE_IGNORED) {
201 ALOGI("Offline Camera %s: Access for \"%s\" has been restricted",
202 mCameraIdStr.string(), String8(mClientPackageName).string());
203 // Return the same error as for device policy manager rejection
204 return -EACCES;
205 }
206 }
207
208 mOpsActive = true;
209
210 // Transition device state to OPEN
211 sCameraService->mUidPolicy->registerMonitorUid(mClientUid);
212
213 return OK;
214}
215
216status_t CameraOfflineSessionClient::finishCameraOps() {
217 ATRACE_CALL();
218
219 // Check if startCameraOps succeeded, and if so, finish the camera op
220 if (mOpsActive) {
221 // Notify app ops that the camera is available again
222 if (mAppOpsManager != nullptr) {
223 // TODO : possibly change this to OP_OFFLINE_CAMERA_SESSION
224 mAppOpsManager->finishOp(AppOpsManager::OP_CAMERA, mClientUid,
225 mClientPackageName);
226 mOpsActive = false;
227 }
228 }
229 // Always stop watching, even if no camera op is active
230 if (mOpsCallback != nullptr && mAppOpsManager != nullptr) {
231 mAppOpsManager->stopWatchingMode(mOpsCallback);
232 }
233 mOpsCallback.clear();
234
235 sCameraService->mUidPolicy->unregisterMonitorUid(mClientUid);
236
237 return OK;
238}
239
Emilian Peev4697b642019-11-19 17:11:14 -0800240void CameraOfflineSessionClient::onResultAvailable(const CaptureResult& result) {
241 ATRACE_CALL();
242 ALOGV("%s", __FUNCTION__);
243
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800244 if (mRemoteCallback.get() != NULL) {
245 mRemoteCallback->onResultReceived(result.mMetadata, result.mResultExtras,
Emilian Peev4697b642019-11-19 17:11:14 -0800246 result.mPhysicalMetadatas);
247 }
248
249 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
250 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
251 }
252}
253
254void CameraOfflineSessionClient::notifyShutter(const CaptureResultExtras& resultExtras,
255 nsecs_t timestamp) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800256
257 if (mRemoteCallback.get() != nullptr) {
258 mRemoteCallback->onCaptureStarted(resultExtras, timestamp);
Emilian Peev4697b642019-11-19 17:11:14 -0800259 }
260
261 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
262 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
263 }
264}
265
Emilian Peevd99c8ae2019-11-26 13:19:13 -0800266void CameraOfflineSessionClient::notifyIdle() {
267 if (mRemoteCallback.get() != nullptr) {
268 mRemoteCallback->onDeviceIdle();
269 }
270}
271
272void CameraOfflineSessionClient::notifyAutoFocus(uint8_t newState, int triggerId) {
273 (void)newState;
274 (void)triggerId;
275
276 ALOGV("%s: Autofocus state now %d, last trigger %d",
277 __FUNCTION__, newState, triggerId);
278}
279
280void CameraOfflineSessionClient::notifyAutoExposure(uint8_t newState, int triggerId) {
281 (void)newState;
282 (void)triggerId;
283
284 ALOGV("%s: Autoexposure state now %d, last trigger %d",
285 __FUNCTION__, newState, triggerId);
286}
287
288void CameraOfflineSessionClient::notifyAutoWhitebalance(uint8_t newState, int triggerId) {
289 (void)newState;
290 (void)triggerId;
291
292 ALOGV("%s: Auto-whitebalance state now %d, last trigger %d", __FUNCTION__, newState,
293 triggerId);
294}
295
296void CameraOfflineSessionClient::notifyPrepared(int /*streamId*/) {
297 ALOGE("%s: Unexpected stream prepare notification in offline mode!", __FUNCTION__);
298 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
299 CaptureResultExtras());
300}
301
302void CameraOfflineSessionClient::notifyRequestQueueEmpty() {
303 if (mRemoteCallback.get() != nullptr) {
304 mRemoteCallback->onRequestQueueEmpty();
305 }
306}
307
308void CameraOfflineSessionClient::notifyRepeatingRequestError(long /*lastFrameNumber*/) {
309 ALOGE("%s: Unexpected repeating request error in offline mode!", __FUNCTION__);
310 notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
311 CaptureResultExtras());
312}
313
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800314// ----------------------------------------------------------------------------
315}; // namespace android