blob: 8031f1c5d81d2f085cb77f7b988ffabf67eb267b [file] [log] [blame]
Igor Murashkin44cfcf02013-03-01 16:22:28 -08001/*
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
Colin Crosse5729fa2014-03-21 15:04:25 -070021#include <inttypes.h>
22
Igor Murashkin44cfcf02013-03-01 16:22:28 -080023#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>
Igor Murashkin44cfcf02013-03-01 16:22:28 -080029
Shuzhen Wang316781a2020-08-18 18:11:01 -070030#include <camera/CameraSessionStats.h>
31
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070032#include "common/Camera2ClientBase.h"
Igor Murashkine7ee7632013-06-11 18:10:18 -070033
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070034#include "api2/CameraDeviceClient.h"
35
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -080036#include "device3/Camera3Device.h"
Jayant Chowdhary12361932018-08-27 14:46:13 -070037#include "utils/CameraThreadState.h"
Shuzhen Wang316781a2020-08-18 18:11:01 -070038#include "utils/CameraServiceProxyWrapper.h"
Igor Murashkin44cfcf02013-03-01 16:22:28 -080039
40namespace android {
41using namespace camera2;
42
Igor Murashkin44cfcf02013-03-01 16:22:28 -080043// Interface used by CameraService
44
45template <typename TClientBase>
46Camera2ClientBase<TClientBase>::Camera2ClientBase(
47 const sp<CameraService>& cameraService,
48 const sp<TCamCallbacks>& remoteCallback,
49 const String16& clientPackageName,
Jooyung Han3f9a3b42020-01-23 12:27:18 +090050 const std::optional<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080051 const String8& cameraId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080052 int api1CameraId,
Igor Murashkin44cfcf02013-03-01 16:22:28 -080053 int cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070054 int sensorOrientation,
Igor Murashkin44cfcf02013-03-01 16:22:28 -080055 int clientPid,
56 uid_t clientUid,
57 int servicePid):
Philip P. Moltmann9e648f62019-11-04 12:52:45 -080058 TClientBase(cameraService, remoteCallback, clientPackageName, clientFeatureId,
Emilian Peev8b64f282021-03-25 16:49:57 -070059 cameraId, api1CameraId, cameraFacing, sensorOrientation, clientPid, clientUid,
60 servicePid),
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -070061 mSharedCameraCallbacks(remoteCallback),
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080062 mDeviceVersion(cameraService->getDeviceVersion(TClientBase::mCameraIdStr)),
Yin-Chia Yehc5248132018-08-15 12:19:20 -070063 mDevice(new Camera3Device(cameraId)),
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080064 mDeviceActive(false), mApi1CameraId(api1CameraId)
Igor Murashkin44cfcf02013-03-01 16:22:28 -080065{
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080066 ALOGI("Camera %s: Opened. Client: %s (PID %d, UID %d)", cameraId.string(),
Eino-Ville Talvalab9d2f332014-09-18 17:24:22 -070067 String8(clientPackageName).string(), clientPid, clientUid);
Igor Murashkin98e24722013-06-19 19:51:04 -070068
Eino-Ville Talvalab9d2f332014-09-18 17:24:22 -070069 mInitialClientPid = clientPid;
Igor Murashkin98e24722013-06-19 19:51:04 -070070 LOG_ALWAYS_FATAL_IF(mDevice == 0, "Device should never be NULL here.");
Igor Murashkin44cfcf02013-03-01 16:22:28 -080071}
72
73template <typename TClientBase>
74status_t Camera2ClientBase<TClientBase>::checkPid(const char* checkLocation)
75 const {
76
Jayant Chowdhary12361932018-08-27 14:46:13 -070077 int callingPid = CameraThreadState::getCallingPid();
Igor Murashkin44cfcf02013-03-01 16:22:28 -080078 if (callingPid == TClientBase::mClientPid) return NO_ERROR;
79
80 ALOGE("%s: attempt to use a locked camera from a different process"
81 " (old pid %d, new pid %d)", checkLocation, TClientBase::mClientPid, callingPid);
82 return PERMISSION_DENIED;
83}
84
85template <typename TClientBase>
Emilian Peevbd8c5032018-02-14 23:05:40 +000086status_t Camera2ClientBase<TClientBase>::initialize(sp<CameraProviderManager> manager,
87 const String8& monitorTags) {
88 return initializeImpl(manager, monitorTags);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080089}
90
91template <typename TClientBase>
92template <typename TProviderPtr>
Emilian Peevbd8c5032018-02-14 23:05:40 +000093status_t Camera2ClientBase<TClientBase>::initializeImpl(TProviderPtr providerPtr,
94 const String8& monitorTags) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -080095 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080096 ALOGV("%s: Initializing client for camera %s", __FUNCTION__,
97 TClientBase::mCameraIdStr.string());
Igor Murashkin44cfcf02013-03-01 16:22:28 -080098 status_t res;
99
Igor Murashkine6800ce2013-03-04 17:25:57 -0800100 // Verify ops permissions
101 res = TClientBase::startCameraOps();
102 if (res != OK) {
103 return res;
104 }
105
106 if (mDevice == NULL) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800107 ALOGE("%s: Camera %s: No device connected",
108 __FUNCTION__, TClientBase::mCameraIdStr.string());
Igor Murashkine6800ce2013-03-04 17:25:57 -0800109 return NO_INIT;
110 }
111
Emilian Peevbd8c5032018-02-14 23:05:40 +0000112 res = mDevice->initialize(providerPtr, monitorTags);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800113 if (res != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800114 ALOGE("%s: Camera %s: unable to initialize device: %s (%d)",
115 __FUNCTION__, TClientBase::mCameraIdStr.string(), strerror(-res), res);
Zhijun He66281c32013-09-13 17:59:59 -0700116 return res;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800117 }
118
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800119 wp<NotificationListener> weakThis(this);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700120 res = mDevice->setNotifyCallback(weakThis);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800121
122 return OK;
123}
124
125template <typename TClientBase>
126Camera2ClientBase<TClientBase>::~Camera2ClientBase() {
127 ATRACE_CALL();
128
129 TClientBase::mDestructionStarted = true;
130
131 disconnect();
132
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800133 ALOGI("Closed Camera %s. Client was: %s (PID %d, UID %u)",
134 TClientBase::mCameraIdStr.string(),
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000135 String8(TClientBase::mClientPackageName).string(),
Eino-Ville Talvalab9d2f332014-09-18 17:24:22 -0700136 mInitialClientPid, TClientBase::mClientUid);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800137}
138
139template <typename TClientBase>
Eino-Ville Talvalac4003962016-01-13 10:07:04 -0800140status_t Camera2ClientBase<TClientBase>::dumpClient(int fd,
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800141 const Vector<String16>& args) {
142 String8 result;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800143 result.appendFormat("Camera2ClientBase[%s] (%p) PID: %d, dump:\n",
144 TClientBase::mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -0800145 (TClientBase::getRemoteCallback() != NULL ?
Marco Nelissen06b46062014-11-14 07:58:25 -0800146 IInterface::asBinder(TClientBase::getRemoteCallback()).get() : NULL),
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800147 TClientBase::mClientPid);
148 result.append(" State: ");
149
150 write(fd, result.string(), result.size());
151 // TODO: print dynamic/request section from most recent requests
152
153 return dumpDevice(fd, args);
154}
155
156template <typename TClientBase>
157status_t Camera2ClientBase<TClientBase>::dumpDevice(
158 int fd,
159 const Vector<String16>& args) {
160 String8 result;
161
162 result = " Device dump:\n";
163 write(fd, result.string(), result.size());
164
Yin-Chia Yehe5138f12017-11-10 14:10:05 -0800165 sp<CameraDeviceBase> device = mDevice;
166 if (!device.get()) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800167 result = " *** Device is detached\n";
168 write(fd, result.string(), result.size());
169 return NO_ERROR;
170 }
171
Yin-Chia Yehe5138f12017-11-10 14:10:05 -0800172 status_t res = device->dump(fd, args);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800173 if (res != OK) {
174 result = String8::format(" Error dumping device: %s (%d)",
175 strerror(-res), res);
176 write(fd, result.string(), result.size());
177 }
178
179 return NO_ERROR;
180}
181
182// ICameraClient2BaseUser interface
183
184
185template <typename TClientBase>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800186binder::Status Camera2ClientBase<TClientBase>::disconnect() {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800187 ATRACE_CALL();
188 Mutex::Autolock icl(mBinderSerializationLock);
189
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800190 binder::Status res = binder::Status::ok();
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800191 // Allow both client and the media server to disconnect at all times
Jayant Chowdhary12361932018-08-27 14:46:13 -0700192 int callingPid = CameraThreadState::getCallingPid();
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800193 if (callingPid != TClientBase::mClientPid &&
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800194 callingPid != TClientBase::mServicePid) return res;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800195
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800196 ALOGV("Camera %s: Shutting down", TClientBase::mCameraIdStr.string());
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800197
Rucha Katakwardf223072021-06-15 10:21:00 -0700198 // Before detaching the device, cache the info from current open session.
199 // The disconnected check avoids duplication of info and also prevents
200 // deadlock while acquiring service lock in cacheDump.
201 if (!TClientBase::mDisconnected) {
202 Camera2ClientBase::getCameraService()->cacheDump();
203 }
204
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800205 detachDevice();
206
Igor Murashkine6800ce2013-03-04 17:25:57 -0800207 CameraService::BasicClient::disconnect();
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800208
Shuzhen Wang316781a2020-08-18 18:11:01 -0700209 ALOGV("Camera %s: Shut down complete", TClientBase::mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800210
211 return res;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800212}
213
214template <typename TClientBase>
215void Camera2ClientBase<TClientBase>::detachDevice() {
216 if (mDevice == 0) return;
217 mDevice->disconnect();
218
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800219 ALOGV("Camera %s: Detach complete", TClientBase::mCameraIdStr.string());
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800220}
221
222template <typename TClientBase>
223status_t Camera2ClientBase<TClientBase>::connect(
224 const sp<TCamCallbacks>& client) {
225 ATRACE_CALL();
226 ALOGV("%s: E", __FUNCTION__);
227 Mutex::Autolock icl(mBinderSerializationLock);
228
229 if (TClientBase::mClientPid != 0 &&
Jayant Chowdhary12361932018-08-27 14:46:13 -0700230 CameraThreadState::getCallingPid() != TClientBase::mClientPid) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800231
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800232 ALOGE("%s: Camera %s: Connection attempt from pid %d; "
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800233 "current locked to pid %d",
234 __FUNCTION__,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800235 TClientBase::mCameraIdStr.string(),
Jayant Chowdhary12361932018-08-27 14:46:13 -0700236 CameraThreadState::getCallingPid(),
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800237 TClientBase::mClientPid);
238 return BAD_VALUE;
239 }
240
Jayant Chowdhary12361932018-08-27 14:46:13 -0700241 TClientBase::mClientPid = CameraThreadState::getCallingPid();
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800242
243 TClientBase::mRemoteCallback = client;
244 mSharedCameraCallbacks = client;
245
246 return OK;
247}
248
249/** Device-related methods */
250
251template <typename TClientBase>
Jianing Weicb0652e2014-03-12 18:29:36 -0700252void Camera2ClientBase<TClientBase>::notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800253 int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -0700254 const CaptureResultExtras& resultExtras) {
255 ALOGE("Error condition %d reported by HAL, requestId %" PRId32, errorCode,
256 resultExtras.requestId);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800257}
258
259template <typename TClientBase>
Eino-Ville Talvala178e8232021-04-16 18:41:39 -0700260status_t Camera2ClientBase<TClientBase>::notifyActive() {
261 if (!mDeviceActive) {
262 status_t res = TClientBase::startCameraStreamingOps();
263 if (res != OK) {
264 ALOGE("%s: Camera %s: Error starting camera streaming ops: %d", __FUNCTION__,
265 TClientBase::mCameraIdStr.string(), res);
266 return res;
267 }
268 CameraServiceProxyWrapper::logActive(TClientBase::mCameraIdStr);
269 }
270 mDeviceActive = true;
271
272 ALOGV("Camera device is now active");
273 return OK;
274}
275
276template <typename TClientBase>
Shuzhen Wang316781a2020-08-18 18:11:01 -0700277void Camera2ClientBase<TClientBase>::notifyIdle(
278 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
279 const std::vector<hardware::CameraStreamStats>& streamStats) {
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700280 if (mDeviceActive) {
Eino-Ville Talvala178e8232021-04-16 18:41:39 -0700281 status_t res = TClientBase::finishCameraStreamingOps();
282 if (res != OK) {
283 ALOGE("%s: Camera %s: Error finishing streaming ops: %d", __FUNCTION__,
284 TClientBase::mCameraIdStr.string(), res);
285 }
Shuzhen Wang316781a2020-08-18 18:11:01 -0700286 CameraServiceProxyWrapper::logIdle(TClientBase::mCameraIdStr,
287 requestCount, resultErrorCount, deviceError, streamStats);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700288 }
289 mDeviceActive = false;
290
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700291 ALOGV("Camera device is now idle");
292}
293
294template <typename TClientBase>
Jianing Weicb0652e2014-03-12 18:29:36 -0700295void Camera2ClientBase<TClientBase>::notifyShutter(const CaptureResultExtras& resultExtras,
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800296 nsecs_t timestamp) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700297 (void)resultExtras;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800298 (void)timestamp;
299
Jianing Weicb0652e2014-03-12 18:29:36 -0700300 ALOGV("%s: Shutter notification for request id %" PRId32 " at time %" PRId64,
301 __FUNCTION__, resultExtras.requestId, timestamp);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800302}
303
304template <typename TClientBase>
305void Camera2ClientBase<TClientBase>::notifyAutoFocus(uint8_t newState,
306 int triggerId) {
307 (void)newState;
308 (void)triggerId;
309
310 ALOGV("%s: Autofocus state now %d, last trigger %d",
311 __FUNCTION__, newState, triggerId);
312
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800313}
314
315template <typename TClientBase>
316void Camera2ClientBase<TClientBase>::notifyAutoExposure(uint8_t newState,
317 int triggerId) {
318 (void)newState;
319 (void)triggerId;
320
321 ALOGV("%s: Autoexposure state now %d, last trigger %d",
322 __FUNCTION__, newState, triggerId);
323}
324
325template <typename TClientBase>
326void Camera2ClientBase<TClientBase>::notifyAutoWhitebalance(uint8_t newState,
327 int triggerId) {
328 (void)newState;
329 (void)triggerId;
330
331 ALOGV("%s: Auto-whitebalance state now %d, last trigger %d",
332 __FUNCTION__, newState, triggerId);
333}
334
335template <typename TClientBase>
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700336void Camera2ClientBase<TClientBase>::notifyPrepared(int streamId) {
337 (void)streamId;
338
339 ALOGV("%s: Stream %d now prepared",
340 __FUNCTION__, streamId);
341}
342
343template <typename TClientBase>
Shuzhen Wang9d066012016-09-30 11:30:20 -0700344void Camera2ClientBase<TClientBase>::notifyRequestQueueEmpty() {
345
346 ALOGV("%s: Request queue now empty", __FUNCTION__);
347}
348
349template <typename TClientBase>
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700350void Camera2ClientBase<TClientBase>::notifyRepeatingRequestError(long lastFrameNumber) {
351 (void)lastFrameNumber;
352
353 ALOGV("%s: Repeating request was stopped. Last frame number is %ld",
354 __FUNCTION__, lastFrameNumber);
355}
356
357template <typename TClientBase>
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800358int Camera2ClientBase<TClientBase>::getCameraId() const {
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800359 return mApi1CameraId;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800360}
361
362template <typename TClientBase>
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700363int Camera2ClientBase<TClientBase>::getCameraDeviceVersion() const {
364 return mDeviceVersion;
365}
366
367template <typename TClientBase>
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800368const sp<CameraDeviceBase>& Camera2ClientBase<TClientBase>::getCameraDevice() {
369 return mDevice;
370}
371
372template <typename TClientBase>
373const sp<CameraService>& Camera2ClientBase<TClientBase>::getCameraService() {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800374 return TClientBase::sCameraService;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800375}
376
377template <typename TClientBase>
378Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::Lock(
379 SharedCameraCallbacks &client) :
380
381 mRemoteCallback(client.mRemoteCallback),
382 mSharedClient(client) {
383
384 mSharedClient.mRemoteCallbackLock.lock();
385}
386
387template <typename TClientBase>
388Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::~Lock() {
389 mSharedClient.mRemoteCallbackLock.unlock();
390}
391
392template <typename TClientBase>
393Camera2ClientBase<TClientBase>::SharedCameraCallbacks::SharedCameraCallbacks(
394 const sp<TCamCallbacks>&client) :
395
396 mRemoteCallback(client) {
397}
398
399template <typename TClientBase>
400typename Camera2ClientBase<TClientBase>::SharedCameraCallbacks&
401Camera2ClientBase<TClientBase>::SharedCameraCallbacks::operator=(
402 const sp<TCamCallbacks>&client) {
403
404 Mutex::Autolock l(mRemoteCallbackLock);
405 mRemoteCallback = client;
406 return *this;
407}
408
409template <typename TClientBase>
410void Camera2ClientBase<TClientBase>::SharedCameraCallbacks::clear() {
411 Mutex::Autolock l(mRemoteCallbackLock);
412 mRemoteCallback.clear();
413}
414
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800415template class Camera2ClientBase<CameraService::Client>;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700416template class Camera2ClientBase<CameraDeviceClientBase>;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800417
418} // namespace android