blob: 5c6711a25dcca96467ea16e46b9f8428161d0d72 [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>
Emilian Peevbd8c5032018-02-14 23:05:40 +000082status_t Camera2ClientBase<TClientBase>::initialize(sp<CameraProviderManager> manager,
83 const String8& monitorTags) {
84 return initializeImpl(manager, monitorTags);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080085}
86
87template <typename TClientBase>
88template <typename TProviderPtr>
Emilian Peevbd8c5032018-02-14 23:05:40 +000089status_t Camera2ClientBase<TClientBase>::initializeImpl(TProviderPtr providerPtr,
90 const String8& monitorTags) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -080091 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080092 ALOGV("%s: Initializing client for camera %s", __FUNCTION__,
93 TClientBase::mCameraIdStr.string());
Igor Murashkin44cfcf02013-03-01 16:22:28 -080094 status_t res;
95
Igor Murashkine6800ce2013-03-04 17:25:57 -080096 // Verify ops permissions
97 res = TClientBase::startCameraOps();
98 if (res != OK) {
99 return res;
100 }
101
102 if (mDevice == NULL) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800103 ALOGE("%s: Camera %s: No device connected",
104 __FUNCTION__, TClientBase::mCameraIdStr.string());
Igor Murashkine6800ce2013-03-04 17:25:57 -0800105 return NO_INIT;
106 }
107
Emilian Peevbd8c5032018-02-14 23:05:40 +0000108 res = mDevice->initialize(providerPtr, monitorTags);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800109 if (res != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800110 ALOGE("%s: Camera %s: unable to initialize device: %s (%d)",
111 __FUNCTION__, TClientBase::mCameraIdStr.string(), strerror(-res), res);
Zhijun He66281c32013-09-13 17:59:59 -0700112 return res;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800113 }
114
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700115 wp<CameraDeviceBase::NotificationListener> weakThis(this);
116 res = mDevice->setNotifyCallback(weakThis);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800117
118 return OK;
119}
120
121template <typename TClientBase>
122Camera2ClientBase<TClientBase>::~Camera2ClientBase() {
123 ATRACE_CALL();
124
125 TClientBase::mDestructionStarted = true;
126
127 disconnect();
128
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800129 ALOGI("Closed Camera %s. Client was: %s (PID %d, UID %u)",
130 TClientBase::mCameraIdStr.string(),
Svetoslav Ganov280405a2015-05-12 02:19:27 +0000131 String8(TClientBase::mClientPackageName).string(),
Eino-Ville Talvalab9d2f332014-09-18 17:24:22 -0700132 mInitialClientPid, TClientBase::mClientUid);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800133}
134
135template <typename TClientBase>
Eino-Ville Talvalac4003962016-01-13 10:07:04 -0800136status_t Camera2ClientBase<TClientBase>::dumpClient(int fd,
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800137 const Vector<String16>& args) {
138 String8 result;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800139 result.appendFormat("Camera2ClientBase[%s] (%p) PID: %d, dump:\n",
140 TClientBase::mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -0800141 (TClientBase::getRemoteCallback() != NULL ?
Marco Nelissen06b46062014-11-14 07:58:25 -0800142 IInterface::asBinder(TClientBase::getRemoteCallback()).get() : NULL),
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800143 TClientBase::mClientPid);
144 result.append(" State: ");
145
146 write(fd, result.string(), result.size());
147 // TODO: print dynamic/request section from most recent requests
148
149 return dumpDevice(fd, args);
150}
151
152template <typename TClientBase>
153status_t Camera2ClientBase<TClientBase>::dumpDevice(
154 int fd,
155 const Vector<String16>& args) {
156 String8 result;
157
158 result = " Device dump:\n";
159 write(fd, result.string(), result.size());
160
Yin-Chia Yehe5138f12017-11-10 14:10:05 -0800161 sp<CameraDeviceBase> device = mDevice;
162 if (!device.get()) {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800163 result = " *** Device is detached\n";
164 write(fd, result.string(), result.size());
165 return NO_ERROR;
166 }
167
Yin-Chia Yehe5138f12017-11-10 14:10:05 -0800168 status_t res = device->dump(fd, args);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800169 if (res != OK) {
170 result = String8::format(" Error dumping device: %s (%d)",
171 strerror(-res), res);
172 write(fd, result.string(), result.size());
173 }
174
175 return NO_ERROR;
176}
177
178// ICameraClient2BaseUser interface
179
180
181template <typename TClientBase>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800182binder::Status Camera2ClientBase<TClientBase>::disconnect() {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800183 ATRACE_CALL();
184 Mutex::Autolock icl(mBinderSerializationLock);
185
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800186 binder::Status res = binder::Status::ok();
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800187 // Allow both client and the media server to disconnect at all times
188 int callingPid = getCallingPid();
189 if (callingPid != TClientBase::mClientPid &&
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800190 callingPid != TClientBase::mServicePid) return res;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800191
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800192 ALOGV("Camera %s: Shutting down", TClientBase::mCameraIdStr.string());
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800193
194 detachDevice();
195
Igor Murashkine6800ce2013-03-04 17:25:57 -0800196 CameraService::BasicClient::disconnect();
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800197
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800198 ALOGV("Camera %s: Shut down complete complete", TClientBase::mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800199
200 return res;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800201}
202
203template <typename TClientBase>
204void Camera2ClientBase<TClientBase>::detachDevice() {
205 if (mDevice == 0) return;
206 mDevice->disconnect();
207
208 mDevice.clear();
209
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800210 ALOGV("Camera %s: Detach complete", TClientBase::mCameraIdStr.string());
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800211}
212
213template <typename TClientBase>
214status_t Camera2ClientBase<TClientBase>::connect(
215 const sp<TCamCallbacks>& client) {
216 ATRACE_CALL();
217 ALOGV("%s: E", __FUNCTION__);
218 Mutex::Autolock icl(mBinderSerializationLock);
219
220 if (TClientBase::mClientPid != 0 &&
221 getCallingPid() != TClientBase::mClientPid) {
222
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800223 ALOGE("%s: Camera %s: Connection attempt from pid %d; "
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800224 "current locked to pid %d",
225 __FUNCTION__,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800226 TClientBase::mCameraIdStr.string(),
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800227 getCallingPid(),
228 TClientBase::mClientPid);
229 return BAD_VALUE;
230 }
231
232 TClientBase::mClientPid = getCallingPid();
233
234 TClientBase::mRemoteCallback = client;
235 mSharedCameraCallbacks = client;
236
237 return OK;
238}
239
240/** Device-related methods */
241
242template <typename TClientBase>
Jianing Weicb0652e2014-03-12 18:29:36 -0700243void Camera2ClientBase<TClientBase>::notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800244 int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -0700245 const CaptureResultExtras& resultExtras) {
246 ALOGE("Error condition %d reported by HAL, requestId %" PRId32, errorCode,
247 resultExtras.requestId);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800248}
249
250template <typename TClientBase>
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700251void Camera2ClientBase<TClientBase>::notifyIdle() {
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700252 if (mDeviceActive) {
253 getCameraService()->updateProxyDeviceState(
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -0700254 hardware::ICameraServiceProxy::CAMERA_STATE_IDLE, TClientBase::mCameraIdStr,
255 TClientBase::mCameraFacing, TClientBase::mClientPackageName);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700256 }
257 mDeviceActive = false;
258
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700259 ALOGV("Camera device is now idle");
260}
261
262template <typename TClientBase>
Jianing Weicb0652e2014-03-12 18:29:36 -0700263void Camera2ClientBase<TClientBase>::notifyShutter(const CaptureResultExtras& resultExtras,
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800264 nsecs_t timestamp) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700265 (void)resultExtras;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800266 (void)timestamp;
267
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700268 if (!mDeviceActive) {
269 getCameraService()->updateProxyDeviceState(
Eino-Ville Talvalae8c96c72017-06-27 12:24:07 -0700270 hardware::ICameraServiceProxy::CAMERA_STATE_ACTIVE, TClientBase::mCameraIdStr,
271 TClientBase::mCameraFacing, TClientBase::mClientPackageName);
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700272 }
273 mDeviceActive = true;
274
Jianing Weicb0652e2014-03-12 18:29:36 -0700275 ALOGV("%s: Shutter notification for request id %" PRId32 " at time %" PRId64,
276 __FUNCTION__, resultExtras.requestId, timestamp);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800277}
278
279template <typename TClientBase>
280void Camera2ClientBase<TClientBase>::notifyAutoFocus(uint8_t newState,
281 int triggerId) {
282 (void)newState;
283 (void)triggerId;
284
285 ALOGV("%s: Autofocus state now %d, last trigger %d",
286 __FUNCTION__, newState, triggerId);
287
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800288}
289
290template <typename TClientBase>
291void Camera2ClientBase<TClientBase>::notifyAutoExposure(uint8_t newState,
292 int triggerId) {
293 (void)newState;
294 (void)triggerId;
295
296 ALOGV("%s: Autoexposure state now %d, last trigger %d",
297 __FUNCTION__, newState, triggerId);
298}
299
300template <typename TClientBase>
301void Camera2ClientBase<TClientBase>::notifyAutoWhitebalance(uint8_t newState,
302 int triggerId) {
303 (void)newState;
304 (void)triggerId;
305
306 ALOGV("%s: Auto-whitebalance state now %d, last trigger %d",
307 __FUNCTION__, newState, triggerId);
308}
309
310template <typename TClientBase>
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700311void Camera2ClientBase<TClientBase>::notifyPrepared(int streamId) {
312 (void)streamId;
313
314 ALOGV("%s: Stream %d now prepared",
315 __FUNCTION__, streamId);
316}
317
318template <typename TClientBase>
Shuzhen Wang9d066012016-09-30 11:30:20 -0700319void Camera2ClientBase<TClientBase>::notifyRequestQueueEmpty() {
320
321 ALOGV("%s: Request queue now empty", __FUNCTION__);
322}
323
324template <typename TClientBase>
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700325void Camera2ClientBase<TClientBase>::notifyRepeatingRequestError(long lastFrameNumber) {
326 (void)lastFrameNumber;
327
328 ALOGV("%s: Repeating request was stopped. Last frame number is %ld",
329 __FUNCTION__, lastFrameNumber);
330}
331
332template <typename TClientBase>
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800333int Camera2ClientBase<TClientBase>::getCameraId() const {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800334 return std::stoi(TClientBase::mCameraIdStr.string());
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800335}
336
337template <typename TClientBase>
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700338int Camera2ClientBase<TClientBase>::getCameraDeviceVersion() const {
339 return mDeviceVersion;
340}
341
342template <typename TClientBase>
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800343const sp<CameraDeviceBase>& Camera2ClientBase<TClientBase>::getCameraDevice() {
344 return mDevice;
345}
346
347template <typename TClientBase>
348const sp<CameraService>& Camera2ClientBase<TClientBase>::getCameraService() {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800349 return TClientBase::sCameraService;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800350}
351
352template <typename TClientBase>
353Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::Lock(
354 SharedCameraCallbacks &client) :
355
356 mRemoteCallback(client.mRemoteCallback),
357 mSharedClient(client) {
358
359 mSharedClient.mRemoteCallbackLock.lock();
360}
361
362template <typename TClientBase>
363Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::~Lock() {
364 mSharedClient.mRemoteCallbackLock.unlock();
365}
366
367template <typename TClientBase>
368Camera2ClientBase<TClientBase>::SharedCameraCallbacks::SharedCameraCallbacks(
369 const sp<TCamCallbacks>&client) :
370
371 mRemoteCallback(client) {
372}
373
374template <typename TClientBase>
375typename Camera2ClientBase<TClientBase>::SharedCameraCallbacks&
376Camera2ClientBase<TClientBase>::SharedCameraCallbacks::operator=(
377 const sp<TCamCallbacks>&client) {
378
379 Mutex::Autolock l(mRemoteCallbackLock);
380 mRemoteCallback = client;
381 return *this;
382}
383
384template <typename TClientBase>
385void Camera2ClientBase<TClientBase>::SharedCameraCallbacks::clear() {
386 Mutex::Autolock l(mRemoteCallbackLock);
387 mRemoteCallback.clear();
388}
389
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800390template class Camera2ClientBase<CameraService::Client>;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700391template class Camera2ClientBase<CameraDeviceClientBase>;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800392
393} // namespace android