1 /*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
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 "Camera2ClientBase"
18 #define ATRACE_TAG ATRACE_TAG_CAMERA
19 //#define LOG_NDEBUG 0
20
21 #include <inttypes.h>
22
23 #include <utils/Log.h>
24 #include <utils/Trace.h>
25
26 #include <cutils/properties.h>
27 #include <gui/Surface.h>
28 #include <gui/Surface.h>
29
30 #include <camera/CameraSessionStats.h>
31
32 #include "common/Camera2ClientBase.h"
33
34 #include "api2/CameraDeviceClient.h"
35
36 #include "device3/Camera3Device.h"
37 #include "utils/CameraThreadState.h"
38 #include "utils/CameraServiceProxyWrapper.h"
39
40 namespace android {
41 using namespace camera2;
42
43 // Interface used by CameraService
44
45 template <typename TClientBase>
Camera2ClientBase(const sp<CameraService> & cameraService,const sp<TCamCallbacks> & remoteCallback,const String16 & clientPackageName,const std::optional<String16> & clientFeatureId,const String8 & cameraId,int api1CameraId,int cameraFacing,int sensorOrientation,int clientPid,uid_t clientUid,int servicePid,bool overrideForPerfClass)46 Camera2ClientBase<TClientBase>::Camera2ClientBase(
47 const sp<CameraService>& cameraService,
48 const sp<TCamCallbacks>& remoteCallback,
49 const String16& clientPackageName,
50 const std::optional<String16>& clientFeatureId,
51 const String8& cameraId,
52 int api1CameraId,
53 int cameraFacing,
54 int sensorOrientation,
55 int clientPid,
56 uid_t clientUid,
57 int servicePid,
58 bool overrideForPerfClass):
59 TClientBase(cameraService, remoteCallback, clientPackageName, clientFeatureId,
60 cameraId, api1CameraId, cameraFacing, sensorOrientation, clientPid, clientUid,
61 servicePid),
62 mSharedCameraCallbacks(remoteCallback),
63 mDeviceVersion(cameraService->getDeviceVersion(TClientBase::mCameraIdStr)),
64 mDevice(new Camera3Device(cameraId, overrideForPerfClass)),
65 mDeviceActive(false), mApi1CameraId(api1CameraId)
66 {
67 ALOGI("Camera %s: Opened. Client: %s (PID %d, UID %d)", cameraId.string(),
68 String8(clientPackageName).string(), clientPid, clientUid);
69
70 mInitialClientPid = clientPid;
71 LOG_ALWAYS_FATAL_IF(mDevice == 0, "Device should never be NULL here.");
72 }
73
74 template <typename TClientBase>
checkPid(const char * checkLocation) const75 status_t Camera2ClientBase<TClientBase>::checkPid(const char* checkLocation)
76 const {
77
78 int callingPid = CameraThreadState::getCallingPid();
79 if (callingPid == TClientBase::mClientPid) return NO_ERROR;
80
81 ALOGE("%s: attempt to use a locked camera from a different process"
82 " (old pid %d, new pid %d)", checkLocation, TClientBase::mClientPid, callingPid);
83 return PERMISSION_DENIED;
84 }
85
86 template <typename TClientBase>
initialize(sp<CameraProviderManager> manager,const String8 & monitorTags)87 status_t Camera2ClientBase<TClientBase>::initialize(sp<CameraProviderManager> manager,
88 const String8& monitorTags) {
89 return initializeImpl(manager, monitorTags);
90 }
91
92 template <typename TClientBase>
93 template <typename TProviderPtr>
initializeImpl(TProviderPtr providerPtr,const String8 & monitorTags)94 status_t Camera2ClientBase<TClientBase>::initializeImpl(TProviderPtr providerPtr,
95 const String8& monitorTags) {
96 ATRACE_CALL();
97 ALOGV("%s: Initializing client for camera %s", __FUNCTION__,
98 TClientBase::mCameraIdStr.string());
99 status_t res;
100
101 // Verify ops permissions
102 res = TClientBase::startCameraOps();
103 if (res != OK) {
104 return res;
105 }
106
107 if (mDevice == NULL) {
108 ALOGE("%s: Camera %s: No device connected",
109 __FUNCTION__, TClientBase::mCameraIdStr.string());
110 return NO_INIT;
111 }
112
113 res = mDevice->initialize(providerPtr, monitorTags);
114 if (res != OK) {
115 ALOGE("%s: Camera %s: unable to initialize device: %s (%d)",
116 __FUNCTION__, TClientBase::mCameraIdStr.string(), strerror(-res), res);
117 return res;
118 }
119
120 wp<NotificationListener> weakThis(this);
121 res = mDevice->setNotifyCallback(weakThis);
122
123 return OK;
124 }
125
126 template <typename TClientBase>
~Camera2ClientBase()127 Camera2ClientBase<TClientBase>::~Camera2ClientBase() {
128 ATRACE_CALL();
129
130 TClientBase::mDestructionStarted = true;
131
132 disconnect();
133
134 ALOGI("Closed Camera %s. Client was: %s (PID %d, UID %u)",
135 TClientBase::mCameraIdStr.string(),
136 String8(TClientBase::mClientPackageName).string(),
137 mInitialClientPid, TClientBase::mClientUid);
138 }
139
140 template <typename TClientBase>
dumpClient(int fd,const Vector<String16> & args)141 status_t Camera2ClientBase<TClientBase>::dumpClient(int fd,
142 const Vector<String16>& args) {
143 String8 result;
144 result.appendFormat("Camera2ClientBase[%s] (%p) PID: %d, dump:\n",
145 TClientBase::mCameraIdStr.string(),
146 (TClientBase::getRemoteCallback() != NULL ?
147 IInterface::asBinder(TClientBase::getRemoteCallback()).get() : NULL),
148 TClientBase::mClientPid);
149 result.append(" State: ");
150
151 write(fd, result.string(), result.size());
152 // TODO: print dynamic/request section from most recent requests
153
154 return dumpDevice(fd, args);
155 }
156
157 template <typename TClientBase>
dumpDevice(int fd,const Vector<String16> & args)158 status_t Camera2ClientBase<TClientBase>::dumpDevice(
159 int fd,
160 const Vector<String16>& args) {
161 String8 result;
162
163 result = " Device dump:\n";
164 write(fd, result.string(), result.size());
165
166 sp<CameraDeviceBase> device = mDevice;
167 if (!device.get()) {
168 result = " *** Device is detached\n";
169 write(fd, result.string(), result.size());
170 return NO_ERROR;
171 }
172
173 status_t res = device->dump(fd, args);
174 if (res != OK) {
175 result = String8::format(" Error dumping device: %s (%d)",
176 strerror(-res), res);
177 write(fd, result.string(), result.size());
178 }
179
180 return NO_ERROR;
181 }
182
183 // ICameraClient2BaseUser interface
184
185
186 template <typename TClientBase>
disconnect()187 binder::Status Camera2ClientBase<TClientBase>::disconnect() {
188 ATRACE_CALL();
189 Mutex::Autolock icl(mBinderSerializationLock);
190
191 binder::Status res = binder::Status::ok();
192 // Allow both client and the media server to disconnect at all times
193 int callingPid = CameraThreadState::getCallingPid();
194 if (callingPid != TClientBase::mClientPid &&
195 callingPid != TClientBase::mServicePid) return res;
196
197 ALOGV("Camera %s: Shutting down", TClientBase::mCameraIdStr.string());
198
199 // Before detaching the device, cache the info from current open session.
200 // The disconnected check avoids duplication of info and also prevents
201 // deadlock while acquiring service lock in cacheDump.
202 if (!TClientBase::mDisconnected) {
203 Camera2ClientBase::getCameraService()->cacheDump();
204 }
205
206 detachDevice();
207
208 CameraService::BasicClient::disconnect();
209
210 ALOGV("Camera %s: Shut down complete", TClientBase::mCameraIdStr.string());
211
212 return res;
213 }
214
215 template <typename TClientBase>
detachDevice()216 void Camera2ClientBase<TClientBase>::detachDevice() {
217 if (mDevice == 0) return;
218 mDevice->disconnect();
219
220 ALOGV("Camera %s: Detach complete", TClientBase::mCameraIdStr.string());
221 }
222
223 template <typename TClientBase>
connect(const sp<TCamCallbacks> & client)224 status_t Camera2ClientBase<TClientBase>::connect(
225 const sp<TCamCallbacks>& client) {
226 ATRACE_CALL();
227 ALOGV("%s: E", __FUNCTION__);
228 Mutex::Autolock icl(mBinderSerializationLock);
229
230 if (TClientBase::mClientPid != 0 &&
231 CameraThreadState::getCallingPid() != TClientBase::mClientPid) {
232
233 ALOGE("%s: Camera %s: Connection attempt from pid %d; "
234 "current locked to pid %d",
235 __FUNCTION__,
236 TClientBase::mCameraIdStr.string(),
237 CameraThreadState::getCallingPid(),
238 TClientBase::mClientPid);
239 return BAD_VALUE;
240 }
241
242 TClientBase::mClientPid = CameraThreadState::getCallingPid();
243
244 TClientBase::mRemoteCallback = client;
245 mSharedCameraCallbacks = client;
246
247 return OK;
248 }
249
250 /** Device-related methods */
251
252 template <typename TClientBase>
notifyError(int32_t errorCode,const CaptureResultExtras & resultExtras)253 void Camera2ClientBase<TClientBase>::notifyError(
254 int32_t errorCode,
255 const CaptureResultExtras& resultExtras) {
256 ALOGE("Error condition %d reported by HAL, requestId %" PRId32, errorCode,
257 resultExtras.requestId);
258 }
259
260 template <typename TClientBase>
notifyActive()261 status_t Camera2ClientBase<TClientBase>::notifyActive() {
262 if (!mDeviceActive) {
263 status_t res = TClientBase::startCameraStreamingOps();
264 if (res != OK) {
265 ALOGE("%s: Camera %s: Error starting camera streaming ops: %d", __FUNCTION__,
266 TClientBase::mCameraIdStr.string(), res);
267 return res;
268 }
269 CameraServiceProxyWrapper::logActive(TClientBase::mCameraIdStr);
270 }
271 mDeviceActive = true;
272
273 ALOGV("Camera device is now active");
274 return OK;
275 }
276
277 template <typename TClientBase>
notifyIdle(int64_t requestCount,int64_t resultErrorCount,bool deviceError,const std::vector<hardware::CameraStreamStats> & streamStats)278 void Camera2ClientBase<TClientBase>::notifyIdle(
279 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
280 const std::vector<hardware::CameraStreamStats>& streamStats) {
281 if (mDeviceActive) {
282 status_t res = TClientBase::finishCameraStreamingOps();
283 if (res != OK) {
284 ALOGE("%s: Camera %s: Error finishing streaming ops: %d", __FUNCTION__,
285 TClientBase::mCameraIdStr.string(), res);
286 }
287 CameraServiceProxyWrapper::logIdle(TClientBase::mCameraIdStr,
288 requestCount, resultErrorCount, deviceError, streamStats);
289 }
290 mDeviceActive = false;
291
292 ALOGV("Camera device is now idle");
293 }
294
295 template <typename TClientBase>
notifyShutter(const CaptureResultExtras & resultExtras,nsecs_t timestamp)296 void Camera2ClientBase<TClientBase>::notifyShutter(const CaptureResultExtras& resultExtras,
297 nsecs_t timestamp) {
298 (void)resultExtras;
299 (void)timestamp;
300
301 ALOGV("%s: Shutter notification for request id %" PRId32 " at time %" PRId64,
302 __FUNCTION__, resultExtras.requestId, timestamp);
303 }
304
305 template <typename TClientBase>
notifyAutoFocus(uint8_t newState,int triggerId)306 void Camera2ClientBase<TClientBase>::notifyAutoFocus(uint8_t newState,
307 int triggerId) {
308 (void)newState;
309 (void)triggerId;
310
311 ALOGV("%s: Autofocus state now %d, last trigger %d",
312 __FUNCTION__, newState, triggerId);
313
314 }
315
316 template <typename TClientBase>
notifyAutoExposure(uint8_t newState,int triggerId)317 void Camera2ClientBase<TClientBase>::notifyAutoExposure(uint8_t newState,
318 int triggerId) {
319 (void)newState;
320 (void)triggerId;
321
322 ALOGV("%s: Autoexposure state now %d, last trigger %d",
323 __FUNCTION__, newState, triggerId);
324 }
325
326 template <typename TClientBase>
notifyAutoWhitebalance(uint8_t newState,int triggerId)327 void Camera2ClientBase<TClientBase>::notifyAutoWhitebalance(uint8_t newState,
328 int triggerId) {
329 (void)newState;
330 (void)triggerId;
331
332 ALOGV("%s: Auto-whitebalance state now %d, last trigger %d",
333 __FUNCTION__, newState, triggerId);
334 }
335
336 template <typename TClientBase>
notifyPrepared(int streamId)337 void Camera2ClientBase<TClientBase>::notifyPrepared(int streamId) {
338 (void)streamId;
339
340 ALOGV("%s: Stream %d now prepared",
341 __FUNCTION__, streamId);
342 }
343
344 template <typename TClientBase>
notifyRequestQueueEmpty()345 void Camera2ClientBase<TClientBase>::notifyRequestQueueEmpty() {
346
347 ALOGV("%s: Request queue now empty", __FUNCTION__);
348 }
349
350 template <typename TClientBase>
notifyRepeatingRequestError(long lastFrameNumber)351 void Camera2ClientBase<TClientBase>::notifyRepeatingRequestError(long lastFrameNumber) {
352 (void)lastFrameNumber;
353
354 ALOGV("%s: Repeating request was stopped. Last frame number is %ld",
355 __FUNCTION__, lastFrameNumber);
356 }
357
358 template <typename TClientBase>
getCameraId() const359 int Camera2ClientBase<TClientBase>::getCameraId() const {
360 return mApi1CameraId;
361 }
362
363 template <typename TClientBase>
getCameraDeviceVersion() const364 int Camera2ClientBase<TClientBase>::getCameraDeviceVersion() const {
365 return mDeviceVersion;
366 }
367
368 template <typename TClientBase>
getCameraDevice()369 const sp<CameraDeviceBase>& Camera2ClientBase<TClientBase>::getCameraDevice() {
370 return mDevice;
371 }
372
373 template <typename TClientBase>
getCameraService()374 const sp<CameraService>& Camera2ClientBase<TClientBase>::getCameraService() {
375 return TClientBase::sCameraService;
376 }
377
378 template <typename TClientBase>
Lock(SharedCameraCallbacks & client)379 Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::Lock(
380 SharedCameraCallbacks &client) :
381
382 mRemoteCallback(client.mRemoteCallback),
383 mSharedClient(client) {
384
385 mSharedClient.mRemoteCallbackLock.lock();
386 }
387
388 template <typename TClientBase>
~Lock()389 Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::~Lock() {
390 mSharedClient.mRemoteCallbackLock.unlock();
391 }
392
393 template <typename TClientBase>
SharedCameraCallbacks(const sp<TCamCallbacks> & client)394 Camera2ClientBase<TClientBase>::SharedCameraCallbacks::SharedCameraCallbacks(
395 const sp<TCamCallbacks>&client) :
396
397 mRemoteCallback(client) {
398 }
399
400 template <typename TClientBase>
401 typename Camera2ClientBase<TClientBase>::SharedCameraCallbacks&
operator =(const sp<TCamCallbacks> & client)402 Camera2ClientBase<TClientBase>::SharedCameraCallbacks::operator=(
403 const sp<TCamCallbacks>&client) {
404
405 Mutex::Autolock l(mRemoteCallbackLock);
406 mRemoteCallback = client;
407 return *this;
408 }
409
410 template <typename TClientBase>
clear()411 void Camera2ClientBase<TClientBase>::SharedCameraCallbacks::clear() {
412 Mutex::Autolock l(mRemoteCallbackLock);
413 mRemoteCallback.clear();
414 }
415
416 template class Camera2ClientBase<CameraService::Client>;
417 template class Camera2ClientBase<CameraDeviceClientBase>;
418
419 } // namespace android
420