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