blob: 609698c27faf5e1465af59b0b4359694c1bde733 [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
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070030#include "common/Camera2ClientBase.h"
Igor Murashkine7ee7632013-06-11 18:10:18 -070031
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070032#include "api2/CameraDeviceClient.h"
33
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -080034#include "device3/Camera3Device.h"
Jayant Chowdhary12361932018-08-27 14:46:13 -070035#include "utils/CameraThreadState.h"
Igor Murashkin44cfcf02013-03-01 16:22:28 -080036
37namespace android {
38using namespace camera2;
39
Igor Murashkin44cfcf02013-03-01 16:22:28 -080040// Interface used by CameraService
41
42template <typename TClientBase>
43Camera2ClientBase<TClientBase>::Camera2ClientBase(
44 const sp<CameraService>& cameraService,
45 const sp<TCamCallbacks>& remoteCallback,
46 const String16& clientPackageName,
Jooyung Hanb3f7cd22020-01-23 12:27:18 +090047 const std::optional<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080048 const String8& cameraId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080049 int api1CameraId,
Igor Murashkin44cfcf02013-03-01 16:22:28 -080050 int cameraFacing,
51 int clientPid,
52 uid_t clientUid,
53 int servicePid):
Philip P. Moltmann9e648f62019-11-04 12:52:45 -080054 TClientBase(cameraService, remoteCallback, clientPackageName, clientFeatureId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080055 cameraId, api1CameraId, cameraFacing, clientPid, clientUid, servicePid),
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -070056 mSharedCameraCallbacks(remoteCallback),
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080057 mDeviceVersion(cameraService->getDeviceVersion(TClientBase::mCameraIdStr)),
Yin-Chia Yehc5248132018-08-15 12:19:20 -070058 mDevice(new Camera3Device(cameraId)),
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080059 mDeviceActive(false), mApi1CameraId(api1CameraId)
Igor Murashkin44cfcf02013-03-01 16:22:28 -080060{
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080061 ALOGI("Camera %s: Opened. Client: %s (PID %d, UID %d)", cameraId.string(),
Eino-Ville Talvalab9d2f332014-09-18 17:24:22 -070062 String8(clientPackageName).string(), clientPid, clientUid);
Igor Murashkin98e24722013-06-19 19:51:04 -070063
Eino-Ville Talvalab9d2f332014-09-18 17:24:22 -070064 mInitialClientPid = clientPid;
Igor Murashkin98e24722013-06-19 19:51:04 -070065 LOG_ALWAYS_FATAL_IF(mDevice == 0, "Device should never be NULL here.");
Igor Murashkin44cfcf02013-03-01 16:22:28 -080066}
67
68template <typename TClientBase>
69status_t Camera2ClientBase<TClientBase>::checkPid(const char* checkLocation)
70 const {
71
Jayant Chowdhary12361932018-08-27 14:46:13 -070072 int callingPid = CameraThreadState::getCallingPid();
Igor Murashkin44cfcf02013-03-01 16:22:28 -080073 if (callingPid == TClientBase::mClientPid) return NO_ERROR;
74
75 ALOGE("%s: attempt to use a locked camera from a different process"
76 " (old pid %d, new pid %d)", checkLocation, TClientBase::mClientPid, callingPid);
77 return PERMISSION_DENIED;
78}
79
80template <typename TClientBase>
Emilian Peevbd8c5032018-02-14 23:05:40 +000081status_t Camera2ClientBase<TClientBase>::initialize(sp<CameraProviderManager> manager,
82 const String8& monitorTags) {
83 return initializeImpl(manager, monitorTags);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080084}
85
86template <typename TClientBase>
87template <typename TProviderPtr>
Emilian Peevbd8c5032018-02-14 23:05:40 +000088status_t Camera2ClientBase<TClientBase>::initializeImpl(TProviderPtr providerPtr,
89 const String8& monitorTags) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -080090 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080091 ALOGV("%s: Initializing client for camera %s", __FUNCTION__,
92 TClientBase::mCameraIdStr.string());
Igor Murashkin44cfcf02013-03-01 16:22:28 -080093 status_t res;
94
Igor Murashkine6800ce2013-03-04 17:25:57 -080095 // Verify ops permissions
96 res = TClientBase::startCameraOps();
97 if (res != OK) {
98 return res;
99 }
100
101 if (mDevice == NULL) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800102 ALOGE("%s: Camera %s: No device connected",
103 __FUNCTION__, TClientBase::mCameraIdStr.string());
Igor Murashkine6800ce2013-03-04 17:25:57 -0800104 return NO_INIT;
105 }
106
Emilian Peevbd8c5032018-02-14 23:05:40 +0000107 res = mDevice->initialize(providerPtr, monitorTags);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800108 if (res != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800109 ALOGE("%s: Camera %s: unable to initialize device: %s (%d)",
110 __FUNCTION__, TClientBase::mCameraIdStr.string(), strerror(-res), res);
Zhijun He66281c32013-09-13 17:59:59 -0700111 return res;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800112 }
113
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -0800114 wp<NotificationListener> weakThis(this);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700115 res = mDevice->setNotifyCallback(weakThis);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800116
117 return OK;
118}
119
120template <typename TClientBase>
121Camera2ClientBase<TClientBase>::~Camera2ClientBase() {
122 ATRACE_CALL();
123
124 TClientBase::mDestructionStarted = true;
125
126 disconnect();
127
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800128 ALOGI("Closed Camera %s. Client was: %s (PID %d, UID %u)",
129 TClientBase::mCameraIdStr.string(),
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000130 String8(TClientBase::mClientPackageName).string(),
Eino-Ville Talvalab9d2f332014-09-18 17:24:22 -0700131 mInitialClientPid, TClientBase::mClientUid);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800132}
133
134template <typename TClientBase>
Eino-Ville Talvalac4003962016-01-13 10:07:04 -0800135status_t Camera2ClientBase<TClientBase>::dumpClient(int fd,
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800136 const Vector<String16>& args) {
137 String8 result;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800138 result.appendFormat("Camera2ClientBase[%s] (%p) PID: %d, dump:\n",
139 TClientBase::mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -0800140 (TClientBase::getRemoteCallback() != NULL ?
Marco Nelissen06b46062014-11-14 07:58:25 -0800141 IInterface::asBinder(TClientBase::getRemoteCallback()).get() : NULL),
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800142 TClientBase::mClientPid);
143 result.append(" State: ");
144
145 write(fd, result.string(), result.size());
146 // TODO: print dynamic/request section from most recent requests
147
148 return dumpDevice(fd, args);
149}
150
151template <typename TClientBase>
152status_t Camera2ClientBase<TClientBase>::dumpDevice(
153 int fd,
154 const Vector<String16>& args) {
155 String8 result;
156
157 result = " Device dump:\n";
158 write(fd, result.string(), result.size());
159
Yin-Chia Yehe5138f12017-11-10 14:10:05 -0800160 sp<CameraDeviceBase> device = mDevice;
161 if (!device.get()) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800162 result = " *** Device is detached\n";
163 write(fd, result.string(), result.size());
164 return NO_ERROR;
165 }
166
Yin-Chia Yehe5138f12017-11-10 14:10:05 -0800167 status_t res = device->dump(fd, args);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800168 if (res != OK) {
169 result = String8::format(" Error dumping device: %s (%d)",
170 strerror(-res), res);
171 write(fd, result.string(), result.size());
172 }
173
174 return NO_ERROR;
175}
176
177// ICameraClient2BaseUser interface
178
179
180template <typename TClientBase>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800181binder::Status Camera2ClientBase<TClientBase>::disconnect() {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800182 ATRACE_CALL();
183 Mutex::Autolock icl(mBinderSerializationLock);
184
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800185 binder::Status res = binder::Status::ok();
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800186 // Allow both client and the media server to disconnect at all times
Jayant Chowdhary12361932018-08-27 14:46:13 -0700187 int callingPid = CameraThreadState::getCallingPid();
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800188 if (callingPid != TClientBase::mClientPid &&
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800189 callingPid != TClientBase::mServicePid) return res;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800190
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800191 ALOGV("Camera %s: Shutting down", TClientBase::mCameraIdStr.string());
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800192
193 detachDevice();
194
Igor Murashkine6800ce2013-03-04 17:25:57 -0800195 CameraService::BasicClient::disconnect();
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800196
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800197 ALOGV("Camera %s: Shut down complete complete", TClientBase::mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800198
199 return res;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800200}
201
202template <typename TClientBase>
203void Camera2ClientBase<TClientBase>::detachDevice() {
204 if (mDevice == 0) return;
205 mDevice->disconnect();
206
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800207 ALOGV("Camera %s: Detach complete", TClientBase::mCameraIdStr.string());
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800208}
209
210template <typename TClientBase>
211status_t Camera2ClientBase<TClientBase>::connect(
212 const sp<TCamCallbacks>& client) {
213 ATRACE_CALL();
214 ALOGV("%s: E", __FUNCTION__);
215 Mutex::Autolock icl(mBinderSerializationLock);
216
217 if (TClientBase::mClientPid != 0 &&
Jayant Chowdhary12361932018-08-27 14:46:13 -0700218 CameraThreadState::getCallingPid() != TClientBase::mClientPid) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800219
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800220 ALOGE("%s: Camera %s: Connection attempt from pid %d; "
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800221 "current locked to pid %d",
222 __FUNCTION__,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800223 TClientBase::mCameraIdStr.string(),
Jayant Chowdhary12361932018-08-27 14:46:13 -0700224 CameraThreadState::getCallingPid(),
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800225 TClientBase::mClientPid);
226 return BAD_VALUE;
227 }
228
Jayant Chowdhary12361932018-08-27 14:46:13 -0700229 TClientBase::mClientPid = CameraThreadState::getCallingPid();
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800230
231 TClientBase::mRemoteCallback = client;
232 mSharedCameraCallbacks = client;
233
234 return OK;
235}
236
237/** Device-related methods */
238
239template <typename TClientBase>
Jianing Weicb0652e2014-03-12 18:29:36 -0700240void Camera2ClientBase<TClientBase>::notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800241 int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -0700242 const CaptureResultExtras& resultExtras) {
243 ALOGE("Error condition %d reported by HAL, requestId %" PRId32, errorCode,
244 resultExtras.requestId);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800245}
246
247template <typename TClientBase>
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700248void Camera2ClientBase<TClientBase>::notifyIdle() {
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700249 if (mDeviceActive) {
250 getCameraService()->updateProxyDeviceState(
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -0700251 hardware::ICameraServiceProxy::CAMERA_STATE_IDLE, TClientBase::mCameraIdStr,
Emilian Peev573291c2018-02-10 02:10:56 +0000252 TClientBase::mCameraFacing, TClientBase::mClientPackageName,
253 ((mApi1CameraId < 0) ? hardware::ICameraServiceProxy::CAMERA_API_LEVEL_2 :
254 hardware::ICameraServiceProxy::CAMERA_API_LEVEL_1));
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700255 }
256 mDeviceActive = false;
257
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700258 ALOGV("Camera device is now idle");
259}
260
261template <typename TClientBase>
Jianing Weicb0652e2014-03-12 18:29:36 -0700262void Camera2ClientBase<TClientBase>::notifyShutter(const CaptureResultExtras& resultExtras,
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800263 nsecs_t timestamp) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700264 (void)resultExtras;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800265 (void)timestamp;
266
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700267 if (!mDeviceActive) {
268 getCameraService()->updateProxyDeviceState(
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -0700269 hardware::ICameraServiceProxy::CAMERA_STATE_ACTIVE, TClientBase::mCameraIdStr,
Emilian Peev573291c2018-02-10 02:10:56 +0000270 TClientBase::mCameraFacing, TClientBase::mClientPackageName,
271 ((mApi1CameraId < 0) ? hardware::ICameraServiceProxy::CAMERA_API_LEVEL_2 :
272 hardware::ICameraServiceProxy::CAMERA_API_LEVEL_1));
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700273 }
274 mDeviceActive = true;
275
Jianing Weicb0652e2014-03-12 18:29:36 -0700276 ALOGV("%s: Shutter notification for request id %" PRId32 " at time %" PRId64,
277 __FUNCTION__, resultExtras.requestId, timestamp);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800278}
279
280template <typename TClientBase>
281void Camera2ClientBase<TClientBase>::notifyAutoFocus(uint8_t newState,
282 int triggerId) {
283 (void)newState;
284 (void)triggerId;
285
286 ALOGV("%s: Autofocus state now %d, last trigger %d",
287 __FUNCTION__, newState, triggerId);
288
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800289}
290
291template <typename TClientBase>
292void Camera2ClientBase<TClientBase>::notifyAutoExposure(uint8_t newState,
293 int triggerId) {
294 (void)newState;
295 (void)triggerId;
296
297 ALOGV("%s: Autoexposure state now %d, last trigger %d",
298 __FUNCTION__, newState, triggerId);
299}
300
301template <typename TClientBase>
302void Camera2ClientBase<TClientBase>::notifyAutoWhitebalance(uint8_t newState,
303 int triggerId) {
304 (void)newState;
305 (void)triggerId;
306
307 ALOGV("%s: Auto-whitebalance state now %d, last trigger %d",
308 __FUNCTION__, newState, triggerId);
309}
310
311template <typename TClientBase>
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700312void Camera2ClientBase<TClientBase>::notifyPrepared(int streamId) {
313 (void)streamId;
314
315 ALOGV("%s: Stream %d now prepared",
316 __FUNCTION__, streamId);
317}
318
319template <typename TClientBase>
Shuzhen Wang9d066012016-09-30 11:30:20 -0700320void Camera2ClientBase<TClientBase>::notifyRequestQueueEmpty() {
321
322 ALOGV("%s: Request queue now empty", __FUNCTION__);
323}
324
325template <typename TClientBase>
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700326void Camera2ClientBase<TClientBase>::notifyRepeatingRequestError(long lastFrameNumber) {
327 (void)lastFrameNumber;
328
329 ALOGV("%s: Repeating request was stopped. Last frame number is %ld",
330 __FUNCTION__, lastFrameNumber);
331}
332
333template <typename TClientBase>
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800334int Camera2ClientBase<TClientBase>::getCameraId() const {
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800335 return mApi1CameraId;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800336}
337
338template <typename TClientBase>
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700339int Camera2ClientBase<TClientBase>::getCameraDeviceVersion() const {
340 return mDeviceVersion;
341}
342
343template <typename TClientBase>
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800344const sp<CameraDeviceBase>& Camera2ClientBase<TClientBase>::getCameraDevice() {
345 return mDevice;
346}
347
348template <typename TClientBase>
349const sp<CameraService>& Camera2ClientBase<TClientBase>::getCameraService() {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800350 return TClientBase::sCameraService;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800351}
352
353template <typename TClientBase>
354Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::Lock(
355 SharedCameraCallbacks &client) :
356
357 mRemoteCallback(client.mRemoteCallback),
358 mSharedClient(client) {
359
360 mSharedClient.mRemoteCallbackLock.lock();
361}
362
363template <typename TClientBase>
364Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::~Lock() {
365 mSharedClient.mRemoteCallbackLock.unlock();
366}
367
368template <typename TClientBase>
369Camera2ClientBase<TClientBase>::SharedCameraCallbacks::SharedCameraCallbacks(
370 const sp<TCamCallbacks>&client) :
371
372 mRemoteCallback(client) {
373}
374
375template <typename TClientBase>
376typename Camera2ClientBase<TClientBase>::SharedCameraCallbacks&
377Camera2ClientBase<TClientBase>::SharedCameraCallbacks::operator=(
378 const sp<TCamCallbacks>&client) {
379
380 Mutex::Autolock l(mRemoteCallbackLock);
381 mRemoteCallback = client;
382 return *this;
383}
384
385template <typename TClientBase>
386void Camera2ClientBase<TClientBase>::SharedCameraCallbacks::clear() {
387 Mutex::Autolock l(mRemoteCallbackLock);
388 mRemoteCallback.clear();
389}
390
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800391template class Camera2ClientBase<CameraService::Client>;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700392template class Camera2ClientBase<CameraDeviceClientBase>;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800393
394} // namespace android