blob: 0415d67f9d1f1abe99bf2a43a665a32c4efe6f45 [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
Igor Murashkin98e24722013-06-19 19:51:04 -070034#include "CameraDeviceFactory.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),
58 mDeviceVersion(cameraService->getDeviceVersion(cameraId))
Igor Murashkin44cfcf02013-03-01 16:22:28 -080059{
Eino-Ville Talvalab9d2f332014-09-18 17:24:22 -070060 ALOGI("Camera %d: Opened. Client: %s (PID %d, UID %d)", cameraId,
61 String8(clientPackageName).string(), clientPid, clientUid);
Igor Murashkin98e24722013-06-19 19:51:04 -070062
Eino-Ville Talvalab9d2f332014-09-18 17:24:22 -070063 mInitialClientPid = clientPid;
Igor Murashkin98e24722013-06-19 19:51:04 -070064 mDevice = CameraDeviceFactory::createDevice(cameraId);
65 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
72 int callingPid = getCallingPid();
73 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>
Yin-Chia Yehe074a932015-01-30 10:29:02 -080081status_t Camera2ClientBase<TClientBase>::initialize(CameraModule *module) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -080082 ATRACE_CALL();
83 ALOGV("%s: Initializing client for camera %d", __FUNCTION__,
84 TClientBase::mCameraId);
85 status_t res;
86
Igor Murashkine6800ce2013-03-04 17:25:57 -080087 // Verify ops permissions
88 res = TClientBase::startCameraOps();
89 if (res != OK) {
90 return res;
91 }
92
93 if (mDevice == NULL) {
94 ALOGE("%s: Camera %d: No device connected",
95 __FUNCTION__, TClientBase::mCameraId);
96 return NO_INIT;
97 }
98
Igor Murashkin44cfcf02013-03-01 16:22:28 -080099 res = mDevice->initialize(module);
100 if (res != OK) {
101 ALOGE("%s: Camera %d: unable to initialize device: %s (%d)",
102 __FUNCTION__, TClientBase::mCameraId, strerror(-res), res);
Zhijun He66281c32013-09-13 17:59:59 -0700103 return res;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800104 }
105
106 res = mDevice->setNotifyCallback(this);
107
108 return OK;
109}
110
111template <typename TClientBase>
112Camera2ClientBase<TClientBase>::~Camera2ClientBase() {
113 ATRACE_CALL();
114
115 TClientBase::mDestructionStarted = true;
116
117 disconnect();
118
Eino-Ville Talvalab9d2f332014-09-18 17:24:22 -0700119 ALOGI("Closed Camera %d. Client was: %s (PID %d, UID %u)",
120 TClientBase::mCameraId,
121 String8(TClientBase::mClientPackageName).string(),
122 mInitialClientPid, TClientBase::mClientUid);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800123}
124
125template <typename TClientBase>
126status_t Camera2ClientBase<TClientBase>::dump(int fd,
127 const Vector<String16>& args) {
128 String8 result;
129 result.appendFormat("Camera2ClientBase[%d] (%p) PID: %d, dump:\n",
130 TClientBase::mCameraId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -0800131 (TClientBase::getRemoteCallback() != NULL ?
Marco Nelissen06b46062014-11-14 07:58:25 -0800132 IInterface::asBinder(TClientBase::getRemoteCallback()).get() : NULL),
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800133 TClientBase::mClientPid);
134 result.append(" State: ");
135
136 write(fd, result.string(), result.size());
137 // TODO: print dynamic/request section from most recent requests
138
139 return dumpDevice(fd, args);
140}
141
142template <typename TClientBase>
143status_t Camera2ClientBase<TClientBase>::dumpDevice(
144 int fd,
145 const Vector<String16>& args) {
146 String8 result;
147
148 result = " Device dump:\n";
149 write(fd, result.string(), result.size());
150
151 if (!mDevice.get()) {
152 result = " *** Device is detached\n";
153 write(fd, result.string(), result.size());
154 return NO_ERROR;
155 }
156
157 status_t res = mDevice->dump(fd, args);
158 if (res != OK) {
159 result = String8::format(" Error dumping device: %s (%d)",
160 strerror(-res), res);
161 write(fd, result.string(), result.size());
162 }
163
164 return NO_ERROR;
165}
166
167// ICameraClient2BaseUser interface
168
169
170template <typename TClientBase>
171void Camera2ClientBase<TClientBase>::disconnect() {
172 ATRACE_CALL();
173 Mutex::Autolock icl(mBinderSerializationLock);
174
175 // Allow both client and the media server to disconnect at all times
176 int callingPid = getCallingPid();
177 if (callingPid != TClientBase::mClientPid &&
178 callingPid != TClientBase::mServicePid) return;
179
180 ALOGV("Camera %d: Shutting down", TClientBase::mCameraId);
181
182 detachDevice();
183
Igor Murashkine6800ce2013-03-04 17:25:57 -0800184 CameraService::BasicClient::disconnect();
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800185
186 ALOGV("Camera %d: Shut down complete complete", TClientBase::mCameraId);
187}
188
189template <typename TClientBase>
190void Camera2ClientBase<TClientBase>::detachDevice() {
191 if (mDevice == 0) return;
192 mDevice->disconnect();
193
194 mDevice.clear();
195
196 ALOGV("Camera %d: Detach complete", TClientBase::mCameraId);
197}
198
199template <typename TClientBase>
200status_t Camera2ClientBase<TClientBase>::connect(
201 const sp<TCamCallbacks>& client) {
202 ATRACE_CALL();
203 ALOGV("%s: E", __FUNCTION__);
204 Mutex::Autolock icl(mBinderSerializationLock);
205
206 if (TClientBase::mClientPid != 0 &&
207 getCallingPid() != TClientBase::mClientPid) {
208
209 ALOGE("%s: Camera %d: Connection attempt from pid %d; "
210 "current locked to pid %d",
211 __FUNCTION__,
212 TClientBase::mCameraId,
213 getCallingPid(),
214 TClientBase::mClientPid);
215 return BAD_VALUE;
216 }
217
218 TClientBase::mClientPid = getCallingPid();
219
220 TClientBase::mRemoteCallback = client;
221 mSharedCameraCallbacks = client;
222
223 return OK;
224}
225
226/** Device-related methods */
227
228template <typename TClientBase>
Jianing Weicb0652e2014-03-12 18:29:36 -0700229void Camera2ClientBase<TClientBase>::notifyError(
230 ICameraDeviceCallbacks::CameraErrorCode errorCode,
231 const CaptureResultExtras& resultExtras) {
232 ALOGE("Error condition %d reported by HAL, requestId %" PRId32, errorCode,
233 resultExtras.requestId);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800234}
235
236template <typename TClientBase>
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700237void Camera2ClientBase<TClientBase>::notifyIdle() {
238 ALOGV("Camera device is now idle");
239}
240
241template <typename TClientBase>
Jianing Weicb0652e2014-03-12 18:29:36 -0700242void Camera2ClientBase<TClientBase>::notifyShutter(const CaptureResultExtras& resultExtras,
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800243 nsecs_t timestamp) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700244 (void)resultExtras;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800245 (void)timestamp;
246
Jianing Weicb0652e2014-03-12 18:29:36 -0700247 ALOGV("%s: Shutter notification for request id %" PRId32 " at time %" PRId64,
248 __FUNCTION__, resultExtras.requestId, timestamp);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800249}
250
251template <typename TClientBase>
252void Camera2ClientBase<TClientBase>::notifyAutoFocus(uint8_t newState,
253 int triggerId) {
254 (void)newState;
255 (void)triggerId;
256
257 ALOGV("%s: Autofocus state now %d, last trigger %d",
258 __FUNCTION__, newState, triggerId);
259
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800260}
261
262template <typename TClientBase>
263void Camera2ClientBase<TClientBase>::notifyAutoExposure(uint8_t newState,
264 int triggerId) {
265 (void)newState;
266 (void)triggerId;
267
268 ALOGV("%s: Autoexposure state now %d, last trigger %d",
269 __FUNCTION__, newState, triggerId);
270}
271
272template <typename TClientBase>
273void Camera2ClientBase<TClientBase>::notifyAutoWhitebalance(uint8_t newState,
274 int triggerId) {
275 (void)newState;
276 (void)triggerId;
277
278 ALOGV("%s: Auto-whitebalance state now %d, last trigger %d",
279 __FUNCTION__, newState, triggerId);
280}
281
282template <typename TClientBase>
283int Camera2ClientBase<TClientBase>::getCameraId() const {
284 return TClientBase::mCameraId;
285}
286
287template <typename TClientBase>
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700288int Camera2ClientBase<TClientBase>::getCameraDeviceVersion() const {
289 return mDeviceVersion;
290}
291
292template <typename TClientBase>
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800293const sp<CameraDeviceBase>& Camera2ClientBase<TClientBase>::getCameraDevice() {
294 return mDevice;
295}
296
297template <typename TClientBase>
298const sp<CameraService>& Camera2ClientBase<TClientBase>::getCameraService() {
299 return TClientBase::mCameraService;
300}
301
302template <typename TClientBase>
303Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::Lock(
304 SharedCameraCallbacks &client) :
305
306 mRemoteCallback(client.mRemoteCallback),
307 mSharedClient(client) {
308
309 mSharedClient.mRemoteCallbackLock.lock();
310}
311
312template <typename TClientBase>
313Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::~Lock() {
314 mSharedClient.mRemoteCallbackLock.unlock();
315}
316
317template <typename TClientBase>
318Camera2ClientBase<TClientBase>::SharedCameraCallbacks::SharedCameraCallbacks(
319 const sp<TCamCallbacks>&client) :
320
321 mRemoteCallback(client) {
322}
323
324template <typename TClientBase>
325typename Camera2ClientBase<TClientBase>::SharedCameraCallbacks&
326Camera2ClientBase<TClientBase>::SharedCameraCallbacks::operator=(
327 const sp<TCamCallbacks>&client) {
328
329 Mutex::Autolock l(mRemoteCallbackLock);
330 mRemoteCallback = client;
331 return *this;
332}
333
334template <typename TClientBase>
335void Camera2ClientBase<TClientBase>::SharedCameraCallbacks::clear() {
336 Mutex::Autolock l(mRemoteCallbackLock);
337 mRemoteCallback.clear();
338}
339
340template class Camera2ClientBase<CameraService::ProClient>;
341template class Camera2ClientBase<CameraService::Client>;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700342template class Camera2ClientBase<CameraDeviceClientBase>;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800343
344} // namespace android