blob: 3be63986ad7e875a608c70cef0be217149e19ff2 [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>
81status_t Camera2ClientBase<TClientBase>::initialize(camera_module_t *module) {
82 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>
Eino-Ville Talvalac9ab2b02016-01-13 10:07:04 -0800126status_t Camera2ClientBase<TClientBase>::dumpClient(int fd,
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800127 const Vector<String16>& args) {
128 String8 result;
129 result.appendFormat("Camera2ClientBase[%d] (%p) PID: %d, dump:\n",
130 TClientBase::mCameraId,
131 TClientBase::getRemoteCallback()->asBinder().get(),
132 TClientBase::mClientPid);
133 result.append(" State: ");
134
135 write(fd, result.string(), result.size());
136 // TODO: print dynamic/request section from most recent requests
137
138 return dumpDevice(fd, args);
139}
140
141template <typename TClientBase>
142status_t Camera2ClientBase<TClientBase>::dumpDevice(
143 int fd,
144 const Vector<String16>& args) {
145 String8 result;
146
147 result = " Device dump:\n";
148 write(fd, result.string(), result.size());
149
150 if (!mDevice.get()) {
151 result = " *** Device is detached\n";
152 write(fd, result.string(), result.size());
153 return NO_ERROR;
154 }
155
156 status_t res = mDevice->dump(fd, args);
157 if (res != OK) {
158 result = String8::format(" Error dumping device: %s (%d)",
159 strerror(-res), res);
160 write(fd, result.string(), result.size());
161 }
162
163 return NO_ERROR;
164}
165
166// ICameraClient2BaseUser interface
167
168
169template <typename TClientBase>
170void Camera2ClientBase<TClientBase>::disconnect() {
171 ATRACE_CALL();
172 Mutex::Autolock icl(mBinderSerializationLock);
173
174 // Allow both client and the media server to disconnect at all times
175 int callingPid = getCallingPid();
176 if (callingPid != TClientBase::mClientPid &&
177 callingPid != TClientBase::mServicePid) return;
178
179 ALOGV("Camera %d: Shutting down", TClientBase::mCameraId);
180
181 detachDevice();
182
Igor Murashkine6800ce2013-03-04 17:25:57 -0800183 CameraService::BasicClient::disconnect();
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800184
185 ALOGV("Camera %d: Shut down complete complete", TClientBase::mCameraId);
186}
187
188template <typename TClientBase>
189void Camera2ClientBase<TClientBase>::detachDevice() {
190 if (mDevice == 0) return;
191 mDevice->disconnect();
192
193 mDevice.clear();
194
195 ALOGV("Camera %d: Detach complete", TClientBase::mCameraId);
196}
197
198template <typename TClientBase>
199status_t Camera2ClientBase<TClientBase>::connect(
200 const sp<TCamCallbacks>& client) {
201 ATRACE_CALL();
202 ALOGV("%s: E", __FUNCTION__);
203 Mutex::Autolock icl(mBinderSerializationLock);
204
205 if (TClientBase::mClientPid != 0 &&
206 getCallingPid() != TClientBase::mClientPid) {
207
208 ALOGE("%s: Camera %d: Connection attempt from pid %d; "
209 "current locked to pid %d",
210 __FUNCTION__,
211 TClientBase::mCameraId,
212 getCallingPid(),
213 TClientBase::mClientPid);
214 return BAD_VALUE;
215 }
216
217 TClientBase::mClientPid = getCallingPid();
218
219 TClientBase::mRemoteCallback = client;
220 mSharedCameraCallbacks = client;
221
222 return OK;
223}
224
225/** Device-related methods */
226
227template <typename TClientBase>
Jianing Weicb0652e2014-03-12 18:29:36 -0700228void Camera2ClientBase<TClientBase>::notifyError(
229 ICameraDeviceCallbacks::CameraErrorCode errorCode,
230 const CaptureResultExtras& resultExtras) {
231 ALOGE("Error condition %d reported by HAL, requestId %" PRId32, errorCode,
232 resultExtras.requestId);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800233}
234
235template <typename TClientBase>
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700236void Camera2ClientBase<TClientBase>::notifyIdle() {
237 ALOGV("Camera device is now idle");
238}
239
240template <typename TClientBase>
Jianing Weicb0652e2014-03-12 18:29:36 -0700241void Camera2ClientBase<TClientBase>::notifyShutter(const CaptureResultExtras& resultExtras,
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800242 nsecs_t timestamp) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700243 (void)resultExtras;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800244 (void)timestamp;
245
Jianing Weicb0652e2014-03-12 18:29:36 -0700246 ALOGV("%s: Shutter notification for request id %" PRId32 " at time %" PRId64,
247 __FUNCTION__, resultExtras.requestId, timestamp);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800248}
249
250template <typename TClientBase>
251void Camera2ClientBase<TClientBase>::notifyAutoFocus(uint8_t newState,
252 int triggerId) {
253 (void)newState;
254 (void)triggerId;
255
256 ALOGV("%s: Autofocus state now %d, last trigger %d",
257 __FUNCTION__, newState, triggerId);
258
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800259}
260
261template <typename TClientBase>
262void Camera2ClientBase<TClientBase>::notifyAutoExposure(uint8_t newState,
263 int triggerId) {
264 (void)newState;
265 (void)triggerId;
266
267 ALOGV("%s: Autoexposure state now %d, last trigger %d",
268 __FUNCTION__, newState, triggerId);
269}
270
271template <typename TClientBase>
272void Camera2ClientBase<TClientBase>::notifyAutoWhitebalance(uint8_t newState,
273 int triggerId) {
274 (void)newState;
275 (void)triggerId;
276
277 ALOGV("%s: Auto-whitebalance state now %d, last trigger %d",
278 __FUNCTION__, newState, triggerId);
279}
280
281template <typename TClientBase>
282int Camera2ClientBase<TClientBase>::getCameraId() const {
283 return TClientBase::mCameraId;
284}
285
286template <typename TClientBase>
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700287int Camera2ClientBase<TClientBase>::getCameraDeviceVersion() const {
288 return mDeviceVersion;
289}
290
291template <typename TClientBase>
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800292const sp<CameraDeviceBase>& Camera2ClientBase<TClientBase>::getCameraDevice() {
293 return mDevice;
294}
295
296template <typename TClientBase>
297const sp<CameraService>& Camera2ClientBase<TClientBase>::getCameraService() {
298 return TClientBase::mCameraService;
299}
300
301template <typename TClientBase>
302Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::Lock(
303 SharedCameraCallbacks &client) :
304
305 mRemoteCallback(client.mRemoteCallback),
306 mSharedClient(client) {
307
308 mSharedClient.mRemoteCallbackLock.lock();
309}
310
311template <typename TClientBase>
312Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::~Lock() {
313 mSharedClient.mRemoteCallbackLock.unlock();
314}
315
316template <typename TClientBase>
317Camera2ClientBase<TClientBase>::SharedCameraCallbacks::SharedCameraCallbacks(
318 const sp<TCamCallbacks>&client) :
319
320 mRemoteCallback(client) {
321}
322
323template <typename TClientBase>
324typename Camera2ClientBase<TClientBase>::SharedCameraCallbacks&
325Camera2ClientBase<TClientBase>::SharedCameraCallbacks::operator=(
326 const sp<TCamCallbacks>&client) {
327
328 Mutex::Autolock l(mRemoteCallbackLock);
329 mRemoteCallback = client;
330 return *this;
331}
332
333template <typename TClientBase>
334void Camera2ClientBase<TClientBase>::SharedCameraCallbacks::clear() {
335 Mutex::Autolock l(mRemoteCallbackLock);
336 mRemoteCallback.clear();
337}
338
339template class Camera2ClientBase<CameraService::ProClient>;
340template class Camera2ClientBase<CameraService::Client>;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700341template class Camera2ClientBase<CameraDeviceClientBase>;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800342
343} // namespace android