blob: ccd1e4dd12fd148bdd5d664271eeef869ca61b59 [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
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700107 wp<CameraDeviceBase::NotificationListener> weakThis(this);
108 res = mDevice->setNotifyCallback(weakThis);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800109
110 return OK;
111}
112
113template <typename TClientBase>
114Camera2ClientBase<TClientBase>::~Camera2ClientBase() {
115 ATRACE_CALL();
116
117 TClientBase::mDestructionStarted = true;
118
119 disconnect();
120
Eino-Ville Talvalab9d2f332014-09-18 17:24:22 -0700121 ALOGI("Closed Camera %d. Client was: %s (PID %d, UID %u)",
122 TClientBase::mCameraId,
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000123 String8(TClientBase::mClientPackageName).string(),
Eino-Ville Talvalab9d2f332014-09-18 17:24:22 -0700124 mInitialClientPid, TClientBase::mClientUid);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800125}
126
127template <typename TClientBase>
Eino-Ville Talvalac4003962016-01-13 10:07:04 -0800128status_t Camera2ClientBase<TClientBase>::dumpClient(int fd,
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800129 const Vector<String16>& args) {
130 String8 result;
131 result.appendFormat("Camera2ClientBase[%d] (%p) PID: %d, dump:\n",
132 TClientBase::mCameraId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -0800133 (TClientBase::getRemoteCallback() != NULL ?
Marco Nelissen06b46062014-11-14 07:58:25 -0800134 IInterface::asBinder(TClientBase::getRemoteCallback()).get() : NULL),
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800135 TClientBase::mClientPid);
136 result.append(" State: ");
137
138 write(fd, result.string(), result.size());
139 // TODO: print dynamic/request section from most recent requests
140
141 return dumpDevice(fd, args);
142}
143
144template <typename TClientBase>
145status_t Camera2ClientBase<TClientBase>::dumpDevice(
146 int fd,
147 const Vector<String16>& args) {
148 String8 result;
149
150 result = " Device dump:\n";
151 write(fd, result.string(), result.size());
152
153 if (!mDevice.get()) {
154 result = " *** Device is detached\n";
155 write(fd, result.string(), result.size());
156 return NO_ERROR;
157 }
158
159 status_t res = mDevice->dump(fd, args);
160 if (res != OK) {
161 result = String8::format(" Error dumping device: %s (%d)",
162 strerror(-res), res);
163 write(fd, result.string(), result.size());
164 }
165
166 return NO_ERROR;
167}
168
169// ICameraClient2BaseUser interface
170
171
172template <typename TClientBase>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800173binder::Status Camera2ClientBase<TClientBase>::disconnect() {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800174 ATRACE_CALL();
175 Mutex::Autolock icl(mBinderSerializationLock);
176
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800177 binder::Status res = binder::Status::ok();
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800178 // Allow both client and the media server to disconnect at all times
179 int callingPid = getCallingPid();
180 if (callingPid != TClientBase::mClientPid &&
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800181 callingPid != TClientBase::mServicePid) return res;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800182
183 ALOGV("Camera %d: Shutting down", TClientBase::mCameraId);
184
185 detachDevice();
186
Igor Murashkine6800ce2013-03-04 17:25:57 -0800187 CameraService::BasicClient::disconnect();
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800188
189 ALOGV("Camera %d: Shut down complete complete", TClientBase::mCameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800190
191 return res;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800192}
193
194template <typename TClientBase>
195void Camera2ClientBase<TClientBase>::detachDevice() {
196 if (mDevice == 0) return;
197 mDevice->disconnect();
198
199 mDevice.clear();
200
201 ALOGV("Camera %d: Detach complete", TClientBase::mCameraId);
202}
203
204template <typename TClientBase>
205status_t Camera2ClientBase<TClientBase>::connect(
206 const sp<TCamCallbacks>& client) {
207 ATRACE_CALL();
208 ALOGV("%s: E", __FUNCTION__);
209 Mutex::Autolock icl(mBinderSerializationLock);
210
211 if (TClientBase::mClientPid != 0 &&
212 getCallingPid() != TClientBase::mClientPid) {
213
214 ALOGE("%s: Camera %d: Connection attempt from pid %d; "
215 "current locked to pid %d",
216 __FUNCTION__,
217 TClientBase::mCameraId,
218 getCallingPid(),
219 TClientBase::mClientPid);
220 return BAD_VALUE;
221 }
222
223 TClientBase::mClientPid = getCallingPid();
224
225 TClientBase::mRemoteCallback = client;
226 mSharedCameraCallbacks = client;
227
228 return OK;
229}
230
231/** Device-related methods */
232
233template <typename TClientBase>
Jianing Weicb0652e2014-03-12 18:29:36 -0700234void Camera2ClientBase<TClientBase>::notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800235 int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -0700236 const CaptureResultExtras& resultExtras) {
237 ALOGE("Error condition %d reported by HAL, requestId %" PRId32, errorCode,
238 resultExtras.requestId);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800239}
240
241template <typename TClientBase>
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700242void Camera2ClientBase<TClientBase>::notifyIdle() {
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700243 if (mDeviceActive) {
244 getCameraService()->updateProxyDeviceState(
245 ICameraServiceProxy::CAMERA_STATE_IDLE,
246 String8::format("%d", TClientBase::mCameraId));
247 }
248 mDeviceActive = false;
249
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700250 ALOGV("Camera device is now idle");
251}
252
253template <typename TClientBase>
Jianing Weicb0652e2014-03-12 18:29:36 -0700254void Camera2ClientBase<TClientBase>::notifyShutter(const CaptureResultExtras& resultExtras,
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800255 nsecs_t timestamp) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700256 (void)resultExtras;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800257 (void)timestamp;
258
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700259 if (!mDeviceActive) {
260 getCameraService()->updateProxyDeviceState(
261 ICameraServiceProxy::CAMERA_STATE_ACTIVE,
262 String8::format("%d", TClientBase::mCameraId));
263 }
264 mDeviceActive = true;
265
Jianing Weicb0652e2014-03-12 18:29:36 -0700266 ALOGV("%s: Shutter notification for request id %" PRId32 " at time %" PRId64,
267 __FUNCTION__, resultExtras.requestId, timestamp);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800268}
269
270template <typename TClientBase>
271void Camera2ClientBase<TClientBase>::notifyAutoFocus(uint8_t newState,
272 int triggerId) {
273 (void)newState;
274 (void)triggerId;
275
276 ALOGV("%s: Autofocus state now %d, last trigger %d",
277 __FUNCTION__, newState, triggerId);
278
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800279}
280
281template <typename TClientBase>
282void Camera2ClientBase<TClientBase>::notifyAutoExposure(uint8_t newState,
283 int triggerId) {
284 (void)newState;
285 (void)triggerId;
286
287 ALOGV("%s: Autoexposure state now %d, last trigger %d",
288 __FUNCTION__, newState, triggerId);
289}
290
291template <typename TClientBase>
292void Camera2ClientBase<TClientBase>::notifyAutoWhitebalance(uint8_t newState,
293 int triggerId) {
294 (void)newState;
295 (void)triggerId;
296
297 ALOGV("%s: Auto-whitebalance state now %d, last trigger %d",
298 __FUNCTION__, newState, triggerId);
299}
300
301template <typename TClientBase>
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700302void Camera2ClientBase<TClientBase>::notifyPrepared(int streamId) {
303 (void)streamId;
304
305 ALOGV("%s: Stream %d now prepared",
306 __FUNCTION__, streamId);
307}
308
309template <typename TClientBase>
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700310void Camera2ClientBase<TClientBase>::notifyRepeatingRequestError(long lastFrameNumber) {
311 (void)lastFrameNumber;
312
313 ALOGV("%s: Repeating request was stopped. Last frame number is %ld",
314 __FUNCTION__, lastFrameNumber);
315}
316
317template <typename TClientBase>
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800318int Camera2ClientBase<TClientBase>::getCameraId() const {
319 return TClientBase::mCameraId;
320}
321
322template <typename TClientBase>
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700323int Camera2ClientBase<TClientBase>::getCameraDeviceVersion() const {
324 return mDeviceVersion;
325}
326
327template <typename TClientBase>
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800328const sp<CameraDeviceBase>& Camera2ClientBase<TClientBase>::getCameraDevice() {
329 return mDevice;
330}
331
332template <typename TClientBase>
333const sp<CameraService>& Camera2ClientBase<TClientBase>::getCameraService() {
334 return TClientBase::mCameraService;
335}
336
337template <typename TClientBase>
338Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::Lock(
339 SharedCameraCallbacks &client) :
340
341 mRemoteCallback(client.mRemoteCallback),
342 mSharedClient(client) {
343
344 mSharedClient.mRemoteCallbackLock.lock();
345}
346
347template <typename TClientBase>
348Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::~Lock() {
349 mSharedClient.mRemoteCallbackLock.unlock();
350}
351
352template <typename TClientBase>
353Camera2ClientBase<TClientBase>::SharedCameraCallbacks::SharedCameraCallbacks(
354 const sp<TCamCallbacks>&client) :
355
356 mRemoteCallback(client) {
357}
358
359template <typename TClientBase>
360typename Camera2ClientBase<TClientBase>::SharedCameraCallbacks&
361Camera2ClientBase<TClientBase>::SharedCameraCallbacks::operator=(
362 const sp<TCamCallbacks>&client) {
363
364 Mutex::Autolock l(mRemoteCallbackLock);
365 mRemoteCallback = client;
366 return *this;
367}
368
369template <typename TClientBase>
370void Camera2ClientBase<TClientBase>::SharedCameraCallbacks::clear() {
371 Mutex::Autolock l(mRemoteCallbackLock);
372 mRemoteCallback.clear();
373}
374
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800375template class Camera2ClientBase<CameraService::Client>;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700376template class Camera2ClientBase<CameraDeviceClientBase>;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800377
378} // namespace android