blob: 51ef1609a7274696d2dfe96871c5167b0b4c4d55 [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,
Igor Murashkin44cfcf02013-03-01 16:22:28 -080051 int cameraFacing,
52 int clientPid,
53 uid_t clientUid,
54 int servicePid):
55 TClientBase(cameraService, remoteCallback, clientPackageName,
56 cameraId, cameraFacing, clientPid, clientUid, servicePid),
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -070057 mSharedCameraCallbacks(remoteCallback),
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080058 mDeviceVersion(cameraService->getDeviceVersion(TClientBase::mCameraIdStr)),
Eino-Ville Talvala412fe562015-08-20 17:08:32 -070059 mDeviceActive(false)
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;
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -080065 mDevice = new Camera3Device(cameraId);
Igor Murashkin98e24722013-06-19 19:51:04 -070066 LOG_ALWAYS_FATAL_IF(mDevice == 0, "Device should never be NULL here.");
Igor Murashkin44cfcf02013-03-01 16:22:28 -080067}
68
69template <typename TClientBase>
70status_t Camera2ClientBase<TClientBase>::checkPid(const char* checkLocation)
71 const {
72
73 int callingPid = getCallingPid();
74 if (callingPid == TClientBase::mClientPid) return NO_ERROR;
75
76 ALOGE("%s: attempt to use a locked camera from a different process"
77 " (old pid %d, new pid %d)", checkLocation, TClientBase::mClientPid, callingPid);
78 return PERMISSION_DENIED;
79}
80
81template <typename TClientBase>
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080082status_t Camera2ClientBase<TClientBase>::initialize(sp<CameraProviderManager> manager) {
83 return initializeImpl(manager);
84}
85
86template <typename TClientBase>
87template <typename TProviderPtr>
88status_t Camera2ClientBase<TClientBase>::initializeImpl(TProviderPtr providerPtr) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -080089 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080090 ALOGV("%s: Initializing client for camera %s", __FUNCTION__,
91 TClientBase::mCameraIdStr.string());
Igor Murashkin44cfcf02013-03-01 16:22:28 -080092 status_t res;
93
Igor Murashkine6800ce2013-03-04 17:25:57 -080094 // Verify ops permissions
95 res = TClientBase::startCameraOps();
96 if (res != OK) {
97 return res;
98 }
99
100 if (mDevice == NULL) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800101 ALOGE("%s: Camera %s: No device connected",
102 __FUNCTION__, TClientBase::mCameraIdStr.string());
Igor Murashkine6800ce2013-03-04 17:25:57 -0800103 return NO_INIT;
104 }
105
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800106 res = mDevice->initialize(providerPtr);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800107 if (res != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800108 ALOGE("%s: Camera %s: unable to initialize device: %s (%d)",
109 __FUNCTION__, TClientBase::mCameraIdStr.string(), strerror(-res), res);
Zhijun He66281c32013-09-13 17:59:59 -0700110 return res;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800111 }
112
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700113 wp<CameraDeviceBase::NotificationListener> weakThis(this);
114 res = mDevice->setNotifyCallback(weakThis);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800115
116 return OK;
117}
118
119template <typename TClientBase>
120Camera2ClientBase<TClientBase>::~Camera2ClientBase() {
121 ATRACE_CALL();
122
123 TClientBase::mDestructionStarted = true;
124
125 disconnect();
126
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800127 ALOGI("Closed Camera %s. Client was: %s (PID %d, UID %u)",
128 TClientBase::mCameraIdStr.string(),
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000129 String8(TClientBase::mClientPackageName).string(),
Eino-Ville Talvalab9d2f332014-09-18 17:24:22 -0700130 mInitialClientPid, TClientBase::mClientUid);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800131}
132
133template <typename TClientBase>
Eino-Ville Talvalac4003962016-01-13 10:07:04 -0800134status_t Camera2ClientBase<TClientBase>::dumpClient(int fd,
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800135 const Vector<String16>& args) {
136 String8 result;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800137 result.appendFormat("Camera2ClientBase[%s] (%p) PID: %d, dump:\n",
138 TClientBase::mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -0800139 (TClientBase::getRemoteCallback() != NULL ?
Marco Nelissen06b46062014-11-14 07:58:25 -0800140 IInterface::asBinder(TClientBase::getRemoteCallback()).get() : NULL),
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800141 TClientBase::mClientPid);
142 result.append(" State: ");
143
144 write(fd, result.string(), result.size());
145 // TODO: print dynamic/request section from most recent requests
146
147 return dumpDevice(fd, args);
148}
149
150template <typename TClientBase>
151status_t Camera2ClientBase<TClientBase>::dumpDevice(
152 int fd,
153 const Vector<String16>& args) {
154 String8 result;
155
156 result = " Device dump:\n";
157 write(fd, result.string(), result.size());
158
159 if (!mDevice.get()) {
160 result = " *** Device is detached\n";
161 write(fd, result.string(), result.size());
162 return NO_ERROR;
163 }
164
165 status_t res = mDevice->dump(fd, args);
166 if (res != OK) {
167 result = String8::format(" Error dumping device: %s (%d)",
168 strerror(-res), res);
169 write(fd, result.string(), result.size());
170 }
171
172 return NO_ERROR;
173}
174
175// ICameraClient2BaseUser interface
176
177
178template <typename TClientBase>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800179binder::Status Camera2ClientBase<TClientBase>::disconnect() {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800180 ATRACE_CALL();
181 Mutex::Autolock icl(mBinderSerializationLock);
182
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800183 binder::Status res = binder::Status::ok();
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800184 // Allow both client and the media server to disconnect at all times
185 int callingPid = getCallingPid();
186 if (callingPid != TClientBase::mClientPid &&
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800187 callingPid != TClientBase::mServicePid) return res;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800188
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800189 ALOGV("Camera %s: Shutting down", TClientBase::mCameraIdStr.string());
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800190
191 detachDevice();
192
Igor Murashkine6800ce2013-03-04 17:25:57 -0800193 CameraService::BasicClient::disconnect();
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800194
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800195 ALOGV("Camera %s: Shut down complete complete", TClientBase::mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800196
197 return res;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800198}
199
200template <typename TClientBase>
201void Camera2ClientBase<TClientBase>::detachDevice() {
202 if (mDevice == 0) return;
203 mDevice->disconnect();
204
205 mDevice.clear();
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 &&
218 getCallingPid() != TClientBase::mClientPid) {
219
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(),
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800224 getCallingPid(),
225 TClientBase::mClientPid);
226 return BAD_VALUE;
227 }
228
229 TClientBase::mClientPid = getCallingPid();
230
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,
252 TClientBase::mCameraFacing, TClientBase::mClientPackageName);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700253 }
254 mDeviceActive = false;
255
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700256 ALOGV("Camera device is now idle");
257}
258
259template <typename TClientBase>
Jianing Weicb0652e2014-03-12 18:29:36 -0700260void Camera2ClientBase<TClientBase>::notifyShutter(const CaptureResultExtras& resultExtras,
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800261 nsecs_t timestamp) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700262 (void)resultExtras;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800263 (void)timestamp;
264
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700265 if (!mDeviceActive) {
266 getCameraService()->updateProxyDeviceState(
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -0700267 hardware::ICameraServiceProxy::CAMERA_STATE_ACTIVE, TClientBase::mCameraIdStr,
268 TClientBase::mCameraFacing, TClientBase::mClientPackageName);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700269 }
270 mDeviceActive = true;
271
Jianing Weicb0652e2014-03-12 18:29:36 -0700272 ALOGV("%s: Shutter notification for request id %" PRId32 " at time %" PRId64,
273 __FUNCTION__, resultExtras.requestId, timestamp);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800274}
275
276template <typename TClientBase>
277void Camera2ClientBase<TClientBase>::notifyAutoFocus(uint8_t newState,
278 int triggerId) {
279 (void)newState;
280 (void)triggerId;
281
282 ALOGV("%s: Autofocus state now %d, last trigger %d",
283 __FUNCTION__, newState, triggerId);
284
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800285}
286
287template <typename TClientBase>
288void Camera2ClientBase<TClientBase>::notifyAutoExposure(uint8_t newState,
289 int triggerId) {
290 (void)newState;
291 (void)triggerId;
292
293 ALOGV("%s: Autoexposure state now %d, last trigger %d",
294 __FUNCTION__, newState, triggerId);
295}
296
297template <typename TClientBase>
298void Camera2ClientBase<TClientBase>::notifyAutoWhitebalance(uint8_t newState,
299 int triggerId) {
300 (void)newState;
301 (void)triggerId;
302
303 ALOGV("%s: Auto-whitebalance state now %d, last trigger %d",
304 __FUNCTION__, newState, triggerId);
305}
306
307template <typename TClientBase>
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700308void Camera2ClientBase<TClientBase>::notifyPrepared(int streamId) {
309 (void)streamId;
310
311 ALOGV("%s: Stream %d now prepared",
312 __FUNCTION__, streamId);
313}
314
315template <typename TClientBase>
Shuzhen Wang9d066012016-09-30 11:30:20 -0700316void Camera2ClientBase<TClientBase>::notifyRequestQueueEmpty() {
317
318 ALOGV("%s: Request queue now empty", __FUNCTION__);
319}
320
321template <typename TClientBase>
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700322void Camera2ClientBase<TClientBase>::notifyRepeatingRequestError(long lastFrameNumber) {
323 (void)lastFrameNumber;
324
325 ALOGV("%s: Repeating request was stopped. Last frame number is %ld",
326 __FUNCTION__, lastFrameNumber);
327}
328
329template <typename TClientBase>
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800330int Camera2ClientBase<TClientBase>::getCameraId() const {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800331 return std::stoi(TClientBase::mCameraIdStr.string());
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800332}
333
334template <typename TClientBase>
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700335int Camera2ClientBase<TClientBase>::getCameraDeviceVersion() const {
336 return mDeviceVersion;
337}
338
339template <typename TClientBase>
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800340const sp<CameraDeviceBase>& Camera2ClientBase<TClientBase>::getCameraDevice() {
341 return mDevice;
342}
343
344template <typename TClientBase>
345const sp<CameraService>& Camera2ClientBase<TClientBase>::getCameraService() {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800346 return TClientBase::sCameraService;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800347}
348
349template <typename TClientBase>
350Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::Lock(
351 SharedCameraCallbacks &client) :
352
353 mRemoteCallback(client.mRemoteCallback),
354 mSharedClient(client) {
355
356 mSharedClient.mRemoteCallbackLock.lock();
357}
358
359template <typename TClientBase>
360Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::~Lock() {
361 mSharedClient.mRemoteCallbackLock.unlock();
362}
363
364template <typename TClientBase>
365Camera2ClientBase<TClientBase>::SharedCameraCallbacks::SharedCameraCallbacks(
366 const sp<TCamCallbacks>&client) :
367
368 mRemoteCallback(client) {
369}
370
371template <typename TClientBase>
372typename Camera2ClientBase<TClientBase>::SharedCameraCallbacks&
373Camera2ClientBase<TClientBase>::SharedCameraCallbacks::operator=(
374 const sp<TCamCallbacks>&client) {
375
376 Mutex::Autolock l(mRemoteCallbackLock);
377 mRemoteCallback = client;
378 return *this;
379}
380
381template <typename TClientBase>
382void Camera2ClientBase<TClientBase>::SharedCameraCallbacks::clear() {
383 Mutex::Autolock l(mRemoteCallbackLock);
384 mRemoteCallback.clear();
385}
386
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800387template class Camera2ClientBase<CameraService::Client>;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700388template class Camera2ClientBase<CameraDeviceClientBase>;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800389
390} // namespace android