blob: 4a812b41449cc1f8394be3b5b9e119fcab3773d7 [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,
50 int cameraId,
51 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 Talvala412fe562015-08-20 17:08:32 -070058 mDeviceVersion(cameraService->getDeviceVersion(cameraId)),
59 mDeviceActive(false)
Igor Murashkin44cfcf02013-03-01 16:22:28 -080060{
Eino-Ville Talvalab9d2f332014-09-18 17:24:22 -070061 ALOGI("Camera %d: Opened. Client: %s (PID %d, UID %d)", cameraId,
62 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>
Yin-Chia Yehe074a932015-01-30 10:29:02 -080082status_t Camera2ClientBase<TClientBase>::initialize(CameraModule *module) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -080083 ATRACE_CALL();
84 ALOGV("%s: Initializing client for camera %d", __FUNCTION__,
85 TClientBase::mCameraId);
86 status_t res;
87
Igor Murashkine6800ce2013-03-04 17:25:57 -080088 // Verify ops permissions
89 res = TClientBase::startCameraOps();
90 if (res != OK) {
91 return res;
92 }
93
94 if (mDevice == NULL) {
95 ALOGE("%s: Camera %d: No device connected",
96 __FUNCTION__, TClientBase::mCameraId);
97 return NO_INIT;
98 }
99
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800100 res = mDevice->initialize(module);
101 if (res != OK) {
102 ALOGE("%s: Camera %d: unable to initialize device: %s (%d)",
103 __FUNCTION__, TClientBase::mCameraId, strerror(-res), res);
Zhijun He66281c32013-09-13 17:59:59 -0700104 return res;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800105 }
106
107 res = mDevice->setNotifyCallback(this);
108
109 return OK;
110}
111
112template <typename TClientBase>
113Camera2ClientBase<TClientBase>::~Camera2ClientBase() {
114 ATRACE_CALL();
115
116 TClientBase::mDestructionStarted = true;
117
118 disconnect();
119
Eino-Ville Talvalab9d2f332014-09-18 17:24:22 -0700120 ALOGI("Closed Camera %d. Client was: %s (PID %d, UID %u)",
121 TClientBase::mCameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000122 String8(TClientBase::mClientPackageName).string(),
Eino-Ville Talvalab9d2f332014-09-18 17:24:22 -0700123 mInitialClientPid, TClientBase::mClientUid);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800124}
125
126template <typename TClientBase>
Eino-Ville Talvalac4003962016-01-13 10:07:04 -0800127status_t Camera2ClientBase<TClientBase>::dumpClient(int fd,
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800128 const Vector<String16>& args) {
129 String8 result;
130 result.appendFormat("Camera2ClientBase[%d] (%p) PID: %d, dump:\n",
131 TClientBase::mCameraId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -0800132 (TClientBase::getRemoteCallback() != NULL ?
Marco Nelissen06b46062014-11-14 07:58:25 -0800133 IInterface::asBinder(TClientBase::getRemoteCallback()).get() : NULL),
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800134 TClientBase::mClientPid);
135 result.append(" State: ");
136
137 write(fd, result.string(), result.size());
138 // TODO: print dynamic/request section from most recent requests
139
140 return dumpDevice(fd, args);
141}
142
143template <typename TClientBase>
144status_t Camera2ClientBase<TClientBase>::dumpDevice(
145 int fd,
146 const Vector<String16>& args) {
147 String8 result;
148
149 result = " Device dump:\n";
150 write(fd, result.string(), result.size());
151
152 if (!mDevice.get()) {
153 result = " *** Device is detached\n";
154 write(fd, result.string(), result.size());
155 return NO_ERROR;
156 }
157
158 status_t res = mDevice->dump(fd, args);
159 if (res != OK) {
160 result = String8::format(" Error dumping device: %s (%d)",
161 strerror(-res), res);
162 write(fd, result.string(), result.size());
163 }
164
165 return NO_ERROR;
166}
167
168// ICameraClient2BaseUser interface
169
170
171template <typename TClientBase>
172void Camera2ClientBase<TClientBase>::disconnect() {
173 ATRACE_CALL();
174 Mutex::Autolock icl(mBinderSerializationLock);
175
176 // Allow both client and the media server to disconnect at all times
177 int callingPid = getCallingPid();
178 if (callingPid != TClientBase::mClientPid &&
179 callingPid != TClientBase::mServicePid) return;
180
181 ALOGV("Camera %d: Shutting down", TClientBase::mCameraId);
182
183 detachDevice();
184
Igor Murashkine6800ce2013-03-04 17:25:57 -0800185 CameraService::BasicClient::disconnect();
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800186
187 ALOGV("Camera %d: Shut down complete complete", TClientBase::mCameraId);
188}
189
190template <typename TClientBase>
191void Camera2ClientBase<TClientBase>::detachDevice() {
192 if (mDevice == 0) return;
193 mDevice->disconnect();
194
195 mDevice.clear();
196
197 ALOGV("Camera %d: Detach complete", TClientBase::mCameraId);
198}
199
200template <typename TClientBase>
201status_t Camera2ClientBase<TClientBase>::connect(
202 const sp<TCamCallbacks>& client) {
203 ATRACE_CALL();
204 ALOGV("%s: E", __FUNCTION__);
205 Mutex::Autolock icl(mBinderSerializationLock);
206
207 if (TClientBase::mClientPid != 0 &&
208 getCallingPid() != TClientBase::mClientPid) {
209
210 ALOGE("%s: Camera %d: Connection attempt from pid %d; "
211 "current locked to pid %d",
212 __FUNCTION__,
213 TClientBase::mCameraId,
214 getCallingPid(),
215 TClientBase::mClientPid);
216 return BAD_VALUE;
217 }
218
219 TClientBase::mClientPid = getCallingPid();
220
221 TClientBase::mRemoteCallback = client;
222 mSharedCameraCallbacks = client;
223
224 return OK;
225}
226
227/** Device-related methods */
228
229template <typename TClientBase>
Jianing Weicb0652e2014-03-12 18:29:36 -0700230void Camera2ClientBase<TClientBase>::notifyError(
231 ICameraDeviceCallbacks::CameraErrorCode errorCode,
232 const CaptureResultExtras& resultExtras) {
233 ALOGE("Error condition %d reported by HAL, requestId %" PRId32, errorCode,
234 resultExtras.requestId);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800235}
236
237template <typename TClientBase>
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700238void Camera2ClientBase<TClientBase>::notifyIdle() {
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700239 if (mDeviceActive) {
240 getCameraService()->updateProxyDeviceState(
241 ICameraServiceProxy::CAMERA_STATE_IDLE,
242 String8::format("%d", TClientBase::mCameraId));
243 }
244 mDeviceActive = false;
245
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700246 ALOGV("Camera device is now idle");
247}
248
249template <typename TClientBase>
Jianing Weicb0652e2014-03-12 18:29:36 -0700250void Camera2ClientBase<TClientBase>::notifyShutter(const CaptureResultExtras& resultExtras,
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800251 nsecs_t timestamp) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700252 (void)resultExtras;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800253 (void)timestamp;
254
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700255 if (!mDeviceActive) {
256 getCameraService()->updateProxyDeviceState(
257 ICameraServiceProxy::CAMERA_STATE_ACTIVE,
258 String8::format("%d", TClientBase::mCameraId));
259 }
260 mDeviceActive = true;
261
Jianing Weicb0652e2014-03-12 18:29:36 -0700262 ALOGV("%s: Shutter notification for request id %" PRId32 " at time %" PRId64,
263 __FUNCTION__, resultExtras.requestId, timestamp);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800264}
265
266template <typename TClientBase>
267void Camera2ClientBase<TClientBase>::notifyAutoFocus(uint8_t newState,
268 int triggerId) {
269 (void)newState;
270 (void)triggerId;
271
272 ALOGV("%s: Autofocus state now %d, last trigger %d",
273 __FUNCTION__, newState, triggerId);
274
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800275}
276
277template <typename TClientBase>
278void Camera2ClientBase<TClientBase>::notifyAutoExposure(uint8_t newState,
279 int triggerId) {
280 (void)newState;
281 (void)triggerId;
282
283 ALOGV("%s: Autoexposure state now %d, last trigger %d",
284 __FUNCTION__, newState, triggerId);
285}
286
287template <typename TClientBase>
288void Camera2ClientBase<TClientBase>::notifyAutoWhitebalance(uint8_t newState,
289 int triggerId) {
290 (void)newState;
291 (void)triggerId;
292
293 ALOGV("%s: Auto-whitebalance state now %d, last trigger %d",
294 __FUNCTION__, newState, triggerId);
295}
296
297template <typename TClientBase>
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700298void Camera2ClientBase<TClientBase>::notifyPrepared(int streamId) {
299 (void)streamId;
300
301 ALOGV("%s: Stream %d now prepared",
302 __FUNCTION__, streamId);
303}
304
305template <typename TClientBase>
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800306int Camera2ClientBase<TClientBase>::getCameraId() const {
307 return TClientBase::mCameraId;
308}
309
310template <typename TClientBase>
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700311int Camera2ClientBase<TClientBase>::getCameraDeviceVersion() const {
312 return mDeviceVersion;
313}
314
315template <typename TClientBase>
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800316const sp<CameraDeviceBase>& Camera2ClientBase<TClientBase>::getCameraDevice() {
317 return mDevice;
318}
319
320template <typename TClientBase>
321const sp<CameraService>& Camera2ClientBase<TClientBase>::getCameraService() {
322 return TClientBase::mCameraService;
323}
324
325template <typename TClientBase>
326Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::Lock(
327 SharedCameraCallbacks &client) :
328
329 mRemoteCallback(client.mRemoteCallback),
330 mSharedClient(client) {
331
332 mSharedClient.mRemoteCallbackLock.lock();
333}
334
335template <typename TClientBase>
336Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::~Lock() {
337 mSharedClient.mRemoteCallbackLock.unlock();
338}
339
340template <typename TClientBase>
341Camera2ClientBase<TClientBase>::SharedCameraCallbacks::SharedCameraCallbacks(
342 const sp<TCamCallbacks>&client) :
343
344 mRemoteCallback(client) {
345}
346
347template <typename TClientBase>
348typename Camera2ClientBase<TClientBase>::SharedCameraCallbacks&
349Camera2ClientBase<TClientBase>::SharedCameraCallbacks::operator=(
350 const sp<TCamCallbacks>&client) {
351
352 Mutex::Autolock l(mRemoteCallbackLock);
353 mRemoteCallback = client;
354 return *this;
355}
356
357template <typename TClientBase>
358void Camera2ClientBase<TClientBase>::SharedCameraCallbacks::clear() {
359 Mutex::Autolock l(mRemoteCallbackLock);
360 mRemoteCallback.clear();
361}
362
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800363template class Camera2ClientBase<CameraService::Client>;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700364template class Camera2ClientBase<CameraDeviceClientBase>;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800365
366} // namespace android