blob: ce006a77ff88fd56c3a6e263e44ee364c924a1fb [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"
Igor Murashkin44cfcf02013-03-01 16:22:28 -080035
36namespace android {
37using namespace camera2;
38
39static int getCallingPid() {
40 return IPCThreadState::self()->getCallingPid();
41}
42
43// 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,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080050 const String8& cameraId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080051 int api1CameraId,
Igor Murashkin44cfcf02013-03-01 16:22:28 -080052 int cameraFacing,
53 int clientPid,
54 uid_t clientUid,
55 int servicePid):
56 TClientBase(cameraService, remoteCallback, clientPackageName,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080057 cameraId, api1CameraId, cameraFacing, clientPid, clientUid, servicePid),
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -070058 mSharedCameraCallbacks(remoteCallback),
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080059 mDeviceVersion(cameraService->getDeviceVersion(TClientBase::mCameraIdStr)),
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080060 mDeviceActive(false), mApi1CameraId(api1CameraId)
Igor Murashkin44cfcf02013-03-01 16:22:28 -080061{
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080062 ALOGI("Camera %s: Opened. Client: %s (PID %d, UID %d)", cameraId.string(),
Eino-Ville Talvalab9d2f332014-09-18 17:24:22 -070063 String8(clientPackageName).string(), clientPid, clientUid);
Igor Murashkin98e24722013-06-19 19:51:04 -070064
Eino-Ville Talvalab9d2f332014-09-18 17:24:22 -070065 mInitialClientPid = clientPid;
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -080066 mDevice = new Camera3Device(cameraId);
Igor Murashkin98e24722013-06-19 19:51:04 -070067 LOG_ALWAYS_FATAL_IF(mDevice == 0, "Device should never be NULL here.");
Igor Murashkin44cfcf02013-03-01 16:22:28 -080068}
69
70template <typename TClientBase>
71status_t Camera2ClientBase<TClientBase>::checkPid(const char* checkLocation)
72 const {
73
74 int callingPid = getCallingPid();
75 if (callingPid == TClientBase::mClientPid) return NO_ERROR;
76
77 ALOGE("%s: attempt to use a locked camera from a different process"
78 " (old pid %d, new pid %d)", checkLocation, TClientBase::mClientPid, callingPid);
79 return PERMISSION_DENIED;
80}
81
82template <typename TClientBase>
Emilian Peevbd8c5032018-02-14 23:05:40 +000083status_t Camera2ClientBase<TClientBase>::initialize(sp<CameraProviderManager> manager,
84 const String8& monitorTags) {
85 return initializeImpl(manager, monitorTags);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080086}
87
88template <typename TClientBase>
89template <typename TProviderPtr>
Emilian Peevbd8c5032018-02-14 23:05:40 +000090status_t Camera2ClientBase<TClientBase>::initializeImpl(TProviderPtr providerPtr,
91 const String8& monitorTags) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -080092 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080093 ALOGV("%s: Initializing client for camera %s", __FUNCTION__,
94 TClientBase::mCameraIdStr.string());
Igor Murashkin44cfcf02013-03-01 16:22:28 -080095 status_t res;
96
Igor Murashkine6800ce2013-03-04 17:25:57 -080097 // Verify ops permissions
98 res = TClientBase::startCameraOps();
99 if (res != OK) {
100 return res;
101 }
102
103 if (mDevice == NULL) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800104 ALOGE("%s: Camera %s: No device connected",
105 __FUNCTION__, TClientBase::mCameraIdStr.string());
Igor Murashkine6800ce2013-03-04 17:25:57 -0800106 return NO_INIT;
107 }
108
Emilian Peevbd8c5032018-02-14 23:05:40 +0000109 res = mDevice->initialize(providerPtr, monitorTags);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800110 if (res != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800111 ALOGE("%s: Camera %s: unable to initialize device: %s (%d)",
112 __FUNCTION__, TClientBase::mCameraIdStr.string(), strerror(-res), res);
Zhijun He66281c32013-09-13 17:59:59 -0700113 return res;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800114 }
115
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700116 wp<CameraDeviceBase::NotificationListener> weakThis(this);
117 res = mDevice->setNotifyCallback(weakThis);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800118
119 return OK;
120}
121
122template <typename TClientBase>
123Camera2ClientBase<TClientBase>::~Camera2ClientBase() {
124 ATRACE_CALL();
125
126 TClientBase::mDestructionStarted = true;
127
128 disconnect();
129
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800130 ALOGI("Closed Camera %s. Client was: %s (PID %d, UID %u)",
131 TClientBase::mCameraIdStr.string(),
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000132 String8(TClientBase::mClientPackageName).string(),
Eino-Ville Talvalab9d2f332014-09-18 17:24:22 -0700133 mInitialClientPid, TClientBase::mClientUid);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800134}
135
136template <typename TClientBase>
Eino-Ville Talvalac4003962016-01-13 10:07:04 -0800137status_t Camera2ClientBase<TClientBase>::dumpClient(int fd,
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800138 const Vector<String16>& args) {
139 String8 result;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800140 result.appendFormat("Camera2ClientBase[%s] (%p) PID: %d, dump:\n",
141 TClientBase::mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -0800142 (TClientBase::getRemoteCallback() != NULL ?
Marco Nelissen06b46062014-11-14 07:58:25 -0800143 IInterface::asBinder(TClientBase::getRemoteCallback()).get() : NULL),
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800144 TClientBase::mClientPid);
145 result.append(" State: ");
146
147 write(fd, result.string(), result.size());
148 // TODO: print dynamic/request section from most recent requests
149
150 return dumpDevice(fd, args);
151}
152
153template <typename TClientBase>
154status_t Camera2ClientBase<TClientBase>::dumpDevice(
155 int fd,
156 const Vector<String16>& args) {
157 String8 result;
158
159 result = " Device dump:\n";
160 write(fd, result.string(), result.size());
161
Yin-Chia Yehe5138f12017-11-10 14:10:05 -0800162 sp<CameraDeviceBase> device = mDevice;
163 if (!device.get()) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800164 result = " *** Device is detached\n";
165 write(fd, result.string(), result.size());
166 return NO_ERROR;
167 }
168
Yin-Chia Yehe5138f12017-11-10 14:10:05 -0800169 status_t res = device->dump(fd, args);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800170 if (res != OK) {
171 result = String8::format(" Error dumping device: %s (%d)",
172 strerror(-res), res);
173 write(fd, result.string(), result.size());
174 }
175
176 return NO_ERROR;
177}
178
179// ICameraClient2BaseUser interface
180
181
182template <typename TClientBase>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800183binder::Status Camera2ClientBase<TClientBase>::disconnect() {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800184 ATRACE_CALL();
185 Mutex::Autolock icl(mBinderSerializationLock);
186
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800187 binder::Status res = binder::Status::ok();
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800188 // Allow both client and the media server to disconnect at all times
189 int callingPid = getCallingPid();
190 if (callingPid != TClientBase::mClientPid &&
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800191 callingPid != TClientBase::mServicePid) return res;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800192
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800193 ALOGV("Camera %s: Shutting down", TClientBase::mCameraIdStr.string());
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800194
195 detachDevice();
196
Igor Murashkine6800ce2013-03-04 17:25:57 -0800197 CameraService::BasicClient::disconnect();
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800198
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800199 ALOGV("Camera %s: Shut down complete complete", TClientBase::mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800200
201 return res;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800202}
203
204template <typename TClientBase>
205void Camera2ClientBase<TClientBase>::detachDevice() {
206 if (mDevice == 0) return;
207 mDevice->disconnect();
208
209 mDevice.clear();
210
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800211 ALOGV("Camera %s: Detach complete", TClientBase::mCameraIdStr.string());
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800212}
213
214template <typename TClientBase>
215status_t Camera2ClientBase<TClientBase>::connect(
216 const sp<TCamCallbacks>& client) {
217 ATRACE_CALL();
218 ALOGV("%s: E", __FUNCTION__);
219 Mutex::Autolock icl(mBinderSerializationLock);
220
221 if (TClientBase::mClientPid != 0 &&
222 getCallingPid() != TClientBase::mClientPid) {
223
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800224 ALOGE("%s: Camera %s: Connection attempt from pid %d; "
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800225 "current locked to pid %d",
226 __FUNCTION__,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800227 TClientBase::mCameraIdStr.string(),
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800228 getCallingPid(),
229 TClientBase::mClientPid);
230 return BAD_VALUE;
231 }
232
233 TClientBase::mClientPid = getCallingPid();
234
235 TClientBase::mRemoteCallback = client;
236 mSharedCameraCallbacks = client;
237
238 return OK;
239}
240
241/** Device-related methods */
242
243template <typename TClientBase>
Jianing Weicb0652e2014-03-12 18:29:36 -0700244void Camera2ClientBase<TClientBase>::notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800245 int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -0700246 const CaptureResultExtras& resultExtras) {
247 ALOGE("Error condition %d reported by HAL, requestId %" PRId32, errorCode,
248 resultExtras.requestId);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800249}
250
251template <typename TClientBase>
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700252void Camera2ClientBase<TClientBase>::notifyIdle() {
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700253 if (mDeviceActive) {
254 getCameraService()->updateProxyDeviceState(
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -0700255 hardware::ICameraServiceProxy::CAMERA_STATE_IDLE, TClientBase::mCameraIdStr,
Emilian Peev573291c2018-02-10 02:10:56 +0000256 TClientBase::mCameraFacing, TClientBase::mClientPackageName,
257 ((mApi1CameraId < 0) ? hardware::ICameraServiceProxy::CAMERA_API_LEVEL_2 :
258 hardware::ICameraServiceProxy::CAMERA_API_LEVEL_1));
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700259 }
260 mDeviceActive = false;
261
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700262 ALOGV("Camera device is now idle");
263}
264
265template <typename TClientBase>
Jianing Weicb0652e2014-03-12 18:29:36 -0700266void Camera2ClientBase<TClientBase>::notifyShutter(const CaptureResultExtras& resultExtras,
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800267 nsecs_t timestamp) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700268 (void)resultExtras;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800269 (void)timestamp;
270
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700271 if (!mDeviceActive) {
272 getCameraService()->updateProxyDeviceState(
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -0700273 hardware::ICameraServiceProxy::CAMERA_STATE_ACTIVE, TClientBase::mCameraIdStr,
Emilian Peev573291c2018-02-10 02:10:56 +0000274 TClientBase::mCameraFacing, TClientBase::mClientPackageName,
275 ((mApi1CameraId < 0) ? hardware::ICameraServiceProxy::CAMERA_API_LEVEL_2 :
276 hardware::ICameraServiceProxy::CAMERA_API_LEVEL_1));
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700277 }
278 mDeviceActive = true;
279
Jianing Weicb0652e2014-03-12 18:29:36 -0700280 ALOGV("%s: Shutter notification for request id %" PRId32 " at time %" PRId64,
281 __FUNCTION__, resultExtras.requestId, timestamp);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800282}
283
284template <typename TClientBase>
285void Camera2ClientBase<TClientBase>::notifyAutoFocus(uint8_t newState,
286 int triggerId) {
287 (void)newState;
288 (void)triggerId;
289
290 ALOGV("%s: Autofocus state now %d, last trigger %d",
291 __FUNCTION__, newState, triggerId);
292
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800293}
294
295template <typename TClientBase>
296void Camera2ClientBase<TClientBase>::notifyAutoExposure(uint8_t newState,
297 int triggerId) {
298 (void)newState;
299 (void)triggerId;
300
301 ALOGV("%s: Autoexposure state now %d, last trigger %d",
302 __FUNCTION__, newState, triggerId);
303}
304
305template <typename TClientBase>
306void Camera2ClientBase<TClientBase>::notifyAutoWhitebalance(uint8_t newState,
307 int triggerId) {
308 (void)newState;
309 (void)triggerId;
310
311 ALOGV("%s: Auto-whitebalance state now %d, last trigger %d",
312 __FUNCTION__, newState, triggerId);
313}
314
315template <typename TClientBase>
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700316void Camera2ClientBase<TClientBase>::notifyPrepared(int streamId) {
317 (void)streamId;
318
319 ALOGV("%s: Stream %d now prepared",
320 __FUNCTION__, streamId);
321}
322
323template <typename TClientBase>
Shuzhen Wang9d066012016-09-30 11:30:20 -0700324void Camera2ClientBase<TClientBase>::notifyRequestQueueEmpty() {
325
326 ALOGV("%s: Request queue now empty", __FUNCTION__);
327}
328
329template <typename TClientBase>
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700330void Camera2ClientBase<TClientBase>::notifyRepeatingRequestError(long lastFrameNumber) {
331 (void)lastFrameNumber;
332
333 ALOGV("%s: Repeating request was stopped. Last frame number is %ld",
334 __FUNCTION__, lastFrameNumber);
335}
336
337template <typename TClientBase>
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800338int Camera2ClientBase<TClientBase>::getCameraId() const {
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800339 return mApi1CameraId;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800340}
341
342template <typename TClientBase>
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700343int Camera2ClientBase<TClientBase>::getCameraDeviceVersion() const {
344 return mDeviceVersion;
345}
346
347template <typename TClientBase>
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800348const sp<CameraDeviceBase>& Camera2ClientBase<TClientBase>::getCameraDevice() {
349 return mDevice;
350}
351
352template <typename TClientBase>
353const sp<CameraService>& Camera2ClientBase<TClientBase>::getCameraService() {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800354 return TClientBase::sCameraService;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800355}
356
357template <typename TClientBase>
358Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::Lock(
359 SharedCameraCallbacks &client) :
360
361 mRemoteCallback(client.mRemoteCallback),
362 mSharedClient(client) {
363
364 mSharedClient.mRemoteCallbackLock.lock();
365}
366
367template <typename TClientBase>
368Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::~Lock() {
369 mSharedClient.mRemoteCallbackLock.unlock();
370}
371
372template <typename TClientBase>
373Camera2ClientBase<TClientBase>::SharedCameraCallbacks::SharedCameraCallbacks(
374 const sp<TCamCallbacks>&client) :
375
376 mRemoteCallback(client) {
377}
378
379template <typename TClientBase>
380typename Camera2ClientBase<TClientBase>::SharedCameraCallbacks&
381Camera2ClientBase<TClientBase>::SharedCameraCallbacks::operator=(
382 const sp<TCamCallbacks>&client) {
383
384 Mutex::Autolock l(mRemoteCallbackLock);
385 mRemoteCallback = client;
386 return *this;
387}
388
389template <typename TClientBase>
390void Camera2ClientBase<TClientBase>::SharedCameraCallbacks::clear() {
391 Mutex::Autolock l(mRemoteCallbackLock);
392 mRemoteCallback.clear();
393}
394
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800395template class Camera2ClientBase<CameraService::Client>;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700396template class Camera2ClientBase<CameraDeviceClientBase>;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800397
398} // namespace android