blob: 19efd30f7c986d37bedf7086ed8f382e07e7c914 [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),
57 mSharedCameraCallbacks(remoteCallback)
58{
59 ALOGI("Camera %d: Opened", cameraId);
Igor Murashkin98e24722013-06-19 19:51:04 -070060
61 mDevice = CameraDeviceFactory::createDevice(cameraId);
62 LOG_ALWAYS_FATAL_IF(mDevice == 0, "Device should never be NULL here.");
Igor Murashkin44cfcf02013-03-01 16:22:28 -080063}
64
65template <typename TClientBase>
66status_t Camera2ClientBase<TClientBase>::checkPid(const char* checkLocation)
67 const {
68
69 int callingPid = getCallingPid();
70 if (callingPid == TClientBase::mClientPid) return NO_ERROR;
71
72 ALOGE("%s: attempt to use a locked camera from a different process"
73 " (old pid %d, new pid %d)", checkLocation, TClientBase::mClientPid, callingPid);
74 return PERMISSION_DENIED;
75}
76
77template <typename TClientBase>
78status_t Camera2ClientBase<TClientBase>::initialize(camera_module_t *module) {
79 ATRACE_CALL();
80 ALOGV("%s: Initializing client for camera %d", __FUNCTION__,
81 TClientBase::mCameraId);
82 status_t res;
83
Igor Murashkine6800ce2013-03-04 17:25:57 -080084 // Verify ops permissions
85 res = TClientBase::startCameraOps();
86 if (res != OK) {
87 return res;
88 }
89
90 if (mDevice == NULL) {
91 ALOGE("%s: Camera %d: No device connected",
92 __FUNCTION__, TClientBase::mCameraId);
93 return NO_INIT;
94 }
95
Igor Murashkin44cfcf02013-03-01 16:22:28 -080096 res = mDevice->initialize(module);
97 if (res != OK) {
98 ALOGE("%s: Camera %d: unable to initialize device: %s (%d)",
99 __FUNCTION__, TClientBase::mCameraId, strerror(-res), res);
Zhijun He66281c32013-09-13 17:59:59 -0700100 return res;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800101 }
102
103 res = mDevice->setNotifyCallback(this);
104
105 return OK;
106}
107
108template <typename TClientBase>
109Camera2ClientBase<TClientBase>::~Camera2ClientBase() {
110 ATRACE_CALL();
111
112 TClientBase::mDestructionStarted = true;
113
Igor Murashkine6800ce2013-03-04 17:25:57 -0800114 TClientBase::finishCameraOps();
115
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800116 disconnect();
117
118 ALOGI("Closed Camera %d", TClientBase::mCameraId);
119}
120
121template <typename TClientBase>
122status_t Camera2ClientBase<TClientBase>::dump(int fd,
123 const Vector<String16>& args) {
124 String8 result;
125 result.appendFormat("Camera2ClientBase[%d] (%p) PID: %d, dump:\n",
126 TClientBase::mCameraId,
127 TClientBase::getRemoteCallback()->asBinder().get(),
128 TClientBase::mClientPid);
129 result.append(" State: ");
130
131 write(fd, result.string(), result.size());
132 // TODO: print dynamic/request section from most recent requests
133
134 return dumpDevice(fd, args);
135}
136
137template <typename TClientBase>
138status_t Camera2ClientBase<TClientBase>::dumpDevice(
139 int fd,
140 const Vector<String16>& args) {
141 String8 result;
142
143 result = " Device dump:\n";
144 write(fd, result.string(), result.size());
145
146 if (!mDevice.get()) {
147 result = " *** Device is detached\n";
148 write(fd, result.string(), result.size());
149 return NO_ERROR;
150 }
151
152 status_t res = mDevice->dump(fd, args);
153 if (res != OK) {
154 result = String8::format(" Error dumping device: %s (%d)",
155 strerror(-res), res);
156 write(fd, result.string(), result.size());
157 }
158
159 return NO_ERROR;
160}
161
162// ICameraClient2BaseUser interface
163
164
165template <typename TClientBase>
166void Camera2ClientBase<TClientBase>::disconnect() {
167 ATRACE_CALL();
168 Mutex::Autolock icl(mBinderSerializationLock);
169
170 // Allow both client and the media server to disconnect at all times
171 int callingPid = getCallingPid();
172 if (callingPid != TClientBase::mClientPid &&
173 callingPid != TClientBase::mServicePid) return;
174
175 ALOGV("Camera %d: Shutting down", TClientBase::mCameraId);
176
177 detachDevice();
178
Igor Murashkine6800ce2013-03-04 17:25:57 -0800179 CameraService::BasicClient::disconnect();
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800180
181 ALOGV("Camera %d: Shut down complete complete", TClientBase::mCameraId);
182}
183
184template <typename TClientBase>
185void Camera2ClientBase<TClientBase>::detachDevice() {
186 if (mDevice == 0) return;
187 mDevice->disconnect();
188
189 mDevice.clear();
190
191 ALOGV("Camera %d: Detach complete", TClientBase::mCameraId);
192}
193
194template <typename TClientBase>
195status_t Camera2ClientBase<TClientBase>::connect(
196 const sp<TCamCallbacks>& client) {
197 ATRACE_CALL();
198 ALOGV("%s: E", __FUNCTION__);
199 Mutex::Autolock icl(mBinderSerializationLock);
200
201 if (TClientBase::mClientPid != 0 &&
202 getCallingPid() != TClientBase::mClientPid) {
203
204 ALOGE("%s: Camera %d: Connection attempt from pid %d; "
205 "current locked to pid %d",
206 __FUNCTION__,
207 TClientBase::mCameraId,
208 getCallingPid(),
209 TClientBase::mClientPid);
210 return BAD_VALUE;
211 }
212
213 TClientBase::mClientPid = getCallingPid();
214
215 TClientBase::mRemoteCallback = client;
216 mSharedCameraCallbacks = client;
217
218 return OK;
219}
220
221/** Device-related methods */
222
223template <typename TClientBase>
Jianing Weicb0652e2014-03-12 18:29:36 -0700224void Camera2ClientBase<TClientBase>::notifyError(
225 ICameraDeviceCallbacks::CameraErrorCode errorCode,
226 const CaptureResultExtras& resultExtras) {
227 ALOGE("Error condition %d reported by HAL, requestId %" PRId32, errorCode,
228 resultExtras.requestId);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800229}
230
231template <typename TClientBase>
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700232void Camera2ClientBase<TClientBase>::notifyIdle() {
233 ALOGV("Camera device is now idle");
234}
235
236template <typename TClientBase>
Jianing Weicb0652e2014-03-12 18:29:36 -0700237void Camera2ClientBase<TClientBase>::notifyShutter(const CaptureResultExtras& resultExtras,
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800238 nsecs_t timestamp) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700239 (void)resultExtras;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800240 (void)timestamp;
241
Jianing Weicb0652e2014-03-12 18:29:36 -0700242 ALOGV("%s: Shutter notification for request id %" PRId32 " at time %" PRId64,
243 __FUNCTION__, resultExtras.requestId, timestamp);
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800244}
245
246template <typename TClientBase>
247void Camera2ClientBase<TClientBase>::notifyAutoFocus(uint8_t newState,
248 int triggerId) {
249 (void)newState;
250 (void)triggerId;
251
252 ALOGV("%s: Autofocus state now %d, last trigger %d",
253 __FUNCTION__, newState, triggerId);
254
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800255}
256
257template <typename TClientBase>
258void Camera2ClientBase<TClientBase>::notifyAutoExposure(uint8_t newState,
259 int triggerId) {
260 (void)newState;
261 (void)triggerId;
262
263 ALOGV("%s: Autoexposure state now %d, last trigger %d",
264 __FUNCTION__, newState, triggerId);
265}
266
267template <typename TClientBase>
268void Camera2ClientBase<TClientBase>::notifyAutoWhitebalance(uint8_t newState,
269 int triggerId) {
270 (void)newState;
271 (void)triggerId;
272
273 ALOGV("%s: Auto-whitebalance state now %d, last trigger %d",
274 __FUNCTION__, newState, triggerId);
275}
276
277template <typename TClientBase>
278int Camera2ClientBase<TClientBase>::getCameraId() const {
279 return TClientBase::mCameraId;
280}
281
282template <typename TClientBase>
283const sp<CameraDeviceBase>& Camera2ClientBase<TClientBase>::getCameraDevice() {
284 return mDevice;
285}
286
287template <typename TClientBase>
288const sp<CameraService>& Camera2ClientBase<TClientBase>::getCameraService() {
289 return TClientBase::mCameraService;
290}
291
292template <typename TClientBase>
293Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::Lock(
294 SharedCameraCallbacks &client) :
295
296 mRemoteCallback(client.mRemoteCallback),
297 mSharedClient(client) {
298
299 mSharedClient.mRemoteCallbackLock.lock();
300}
301
302template <typename TClientBase>
303Camera2ClientBase<TClientBase>::SharedCameraCallbacks::Lock::~Lock() {
304 mSharedClient.mRemoteCallbackLock.unlock();
305}
306
307template <typename TClientBase>
308Camera2ClientBase<TClientBase>::SharedCameraCallbacks::SharedCameraCallbacks(
309 const sp<TCamCallbacks>&client) :
310
311 mRemoteCallback(client) {
312}
313
314template <typename TClientBase>
315typename Camera2ClientBase<TClientBase>::SharedCameraCallbacks&
316Camera2ClientBase<TClientBase>::SharedCameraCallbacks::operator=(
317 const sp<TCamCallbacks>&client) {
318
319 Mutex::Autolock l(mRemoteCallbackLock);
320 mRemoteCallback = client;
321 return *this;
322}
323
324template <typename TClientBase>
325void Camera2ClientBase<TClientBase>::SharedCameraCallbacks::clear() {
326 Mutex::Autolock l(mRemoteCallbackLock);
327 mRemoteCallback.clear();
328}
329
330template class Camera2ClientBase<CameraService::ProClient>;
331template class Camera2ClientBase<CameraService::Client>;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700332template class Camera2ClientBase<CameraDeviceClientBase>;
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800333
334} // namespace android