blob: aeea473090d23f3eabb6c2835c9d84816d693ffb [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 Yehc5248132018-08-15 12:19:20 -070060 mDevice(new Camera3Device(cameraId)),
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080061 mDeviceActive(false), mApi1CameraId(api1CameraId)
Igor Murashkin44cfcf02013-03-01 16:22:28 -080062{
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080063 ALOGI("Camera %s: Opened. Client: %s (PID %d, UID %d)", cameraId.string(),
Eino-Ville Talvalab9d2f332014-09-18 17:24:22 -070064 String8(clientPackageName).string(), clientPid, clientUid);
Igor Murashkin98e24722013-06-19 19:51:04 -070065
Eino-Ville Talvalab9d2f332014-09-18 17:24:22 -070066 mInitialClientPid = clientPid;
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
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800209 ALOGV("Camera %s: Detach complete", TClientBase::mCameraIdStr.string());
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800210}
211
212template <typename TClientBase>
213status_t Camera2ClientBase<TClientBase>::connect(
214 const sp<TCamCallbacks>& client) {
215 ATRACE_CALL();
216 ALOGV("%s: E", __FUNCTION__);
217 Mutex::Autolock icl(mBinderSerializationLock);
218
219 if (TClientBase::mClientPid != 0 &&
220 getCallingPid() != TClientBase::mClientPid) {
221
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800222 ALOGE("%s: Camera %s: Connection attempt from pid %d; "
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800223 "current locked to pid %d",
224 __FUNCTION__,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800225 TClientBase::mCameraIdStr.string(),
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800226 getCallingPid(),
227 TClientBase::mClientPid);
228 return BAD_VALUE;
229 }
230
231 TClientBase::mClientPid = getCallingPid();
232
233 TClientBase::mRemoteCallback = client;
234 mSharedCameraCallbacks = client;
235
236 return OK;
237}
238
239/** Device-related methods */
240
241template <typename TClientBase>
Jianing Weicb0652e2014-03-12 18:29:36 -0700242void Camera2ClientBase<TClientBase>::notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800243 int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -0700244 const CaptureResultExtras& resultExtras) {
245 ALOGE("Error condition %d reported by HAL, requestId %" PRId32, errorCode,
246 resultExtras.requestId);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800247}
248
249template <typename TClientBase>
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700250void Camera2ClientBase<TClientBase>::notifyIdle() {
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700251 if (mDeviceActive) {
252 getCameraService()->updateProxyDeviceState(
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -0700253 hardware::ICameraServiceProxy::CAMERA_STATE_IDLE, TClientBase::mCameraIdStr,
Emilian Peev573291c2018-02-10 02:10:56 +0000254 TClientBase::mCameraFacing, TClientBase::mClientPackageName,
255 ((mApi1CameraId < 0) ? hardware::ICameraServiceProxy::CAMERA_API_LEVEL_2 :
256 hardware::ICameraServiceProxy::CAMERA_API_LEVEL_1));
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700257 }
258 mDeviceActive = false;
259
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700260 ALOGV("Camera device is now idle");
261}
262
263template <typename TClientBase>
Jianing Weicb0652e2014-03-12 18:29:36 -0700264void Camera2ClientBase<TClientBase>::notifyShutter(const CaptureResultExtras& resultExtras,
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800265 nsecs_t timestamp) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700266 (void)resultExtras;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800267 (void)timestamp;
268
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700269 if (!mDeviceActive) {
270 getCameraService()->updateProxyDeviceState(
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -0700271 hardware::ICameraServiceProxy::CAMERA_STATE_ACTIVE, TClientBase::mCameraIdStr,
Emilian Peev573291c2018-02-10 02:10:56 +0000272 TClientBase::mCameraFacing, TClientBase::mClientPackageName,
273 ((mApi1CameraId < 0) ? hardware::ICameraServiceProxy::CAMERA_API_LEVEL_2 :
274 hardware::ICameraServiceProxy::CAMERA_API_LEVEL_1));
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700275 }
276 mDeviceActive = true;
277
Jianing Weicb0652e2014-03-12 18:29:36 -0700278 ALOGV("%s: Shutter notification for request id %" PRId32 " at time %" PRId64,
279 __FUNCTION__, resultExtras.requestId, timestamp);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800280}
281
282template <typename TClientBase>
283void Camera2ClientBase<TClientBase>::notifyAutoFocus(uint8_t newState,
284 int triggerId) {
285 (void)newState;
286 (void)triggerId;
287
288 ALOGV("%s: Autofocus state now %d, last trigger %d",
289 __FUNCTION__, newState, triggerId);
290
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800291}
292
293template <typename TClientBase>
294void Camera2ClientBase<TClientBase>::notifyAutoExposure(uint8_t newState,
295 int triggerId) {
296 (void)newState;
297 (void)triggerId;
298
299 ALOGV("%s: Autoexposure state now %d, last trigger %d",
300 __FUNCTION__, newState, triggerId);
301}
302
303template <typename TClientBase>
304void Camera2ClientBase<TClientBase>::notifyAutoWhitebalance(uint8_t newState,
305 int triggerId) {
306 (void)newState;
307 (void)triggerId;
308
309 ALOGV("%s: Auto-whitebalance state now %d, last trigger %d",
310 __FUNCTION__, newState, triggerId);
311}
312
313template <typename TClientBase>
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700314void Camera2ClientBase<TClientBase>::notifyPrepared(int streamId) {
315 (void)streamId;
316
317 ALOGV("%s: Stream %d now prepared",
318 __FUNCTION__, streamId);
319}
320
321template <typename TClientBase>
Shuzhen Wang9d066012016-09-30 11:30:20 -0700322void Camera2ClientBase<TClientBase>::notifyRequestQueueEmpty() {
323
324 ALOGV("%s: Request queue now empty", __FUNCTION__);
325}
326
327template <typename TClientBase>
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700328void Camera2ClientBase<TClientBase>::notifyRepeatingRequestError(long lastFrameNumber) {
329 (void)lastFrameNumber;
330
331 ALOGV("%s: Repeating request was stopped. Last frame number is %ld",
332 __FUNCTION__, lastFrameNumber);
333}
334
335template <typename TClientBase>
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800336int Camera2ClientBase<TClientBase>::getCameraId() const {
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -0800337 return mApi1CameraId;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800338}
339
340template <typename TClientBase>
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700341int Camera2ClientBase<TClientBase>::getCameraDeviceVersion() const {
342 return mDeviceVersion;
343}
344
345template <typename TClientBase>
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800346const sp<CameraDeviceBase>& Camera2ClientBase<TClientBase>::getCameraDevice() {
347 return mDevice;
348}
349
350template <typename TClientBase>
351const sp<CameraService>& Camera2ClientBase<TClientBase>::getCameraService() {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800352 return TClientBase::sCameraService;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800353}
354
355template <typename TClientBase>
356Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::Lock(
357 SharedCameraCallbacks &client) :
358
359 mRemoteCallback(client.mRemoteCallback),
360 mSharedClient(client) {
361
362 mSharedClient.mRemoteCallbackLock.lock();
363}
364
365template <typename TClientBase>
366Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::~Lock() {
367 mSharedClient.mRemoteCallbackLock.unlock();
368}
369
370template <typename TClientBase>
371Camera2ClientBase<TClientBase>::SharedCameraCallbacks::SharedCameraCallbacks(
372 const sp<TCamCallbacks>&client) :
373
374 mRemoteCallback(client) {
375}
376
377template <typename TClientBase>
378typename Camera2ClientBase<TClientBase>::SharedCameraCallbacks&
379Camera2ClientBase<TClientBase>::SharedCameraCallbacks::operator=(
380 const sp<TCamCallbacks>&client) {
381
382 Mutex::Autolock l(mRemoteCallbackLock);
383 mRemoteCallback = client;
384 return *this;
385}
386
387template <typename TClientBase>
388void Camera2ClientBase<TClientBase>::SharedCameraCallbacks::clear() {
389 Mutex::Autolock l(mRemoteCallbackLock);
390 mRemoteCallback.clear();
391}
392
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800393template class Camera2ClientBase<CameraService::Client>;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700394template class Camera2ClientBase<CameraDeviceClientBase>;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800395
396} // namespace android