Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1 | /* |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 2 | * Copyright (C) 2008 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 | */ |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 16 | |
| 17 | #define LOG_TAG "CameraService" |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 19 | |
| 20 | #include <stdio.h> |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 21 | #include <string.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | #include <pthread.h> |
| 24 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 25 | #include <binder/AppOpsManager.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 26 | #include <binder/IPCThreadState.h> |
| 27 | #include <binder/IServiceManager.h> |
| 28 | #include <binder/MemoryBase.h> |
| 29 | #include <binder/MemoryHeapBase.h> |
| 30 | #include <cutils/atomic.h> |
Nipun Kwatra | b5ca461 | 2010-09-11 19:31:10 -0700 | [diff] [blame] | 31 | #include <cutils/properties.h> |
Mathias Agopian | df712ea | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 32 | #include <gui/Surface.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 33 | #include <hardware/hardware.h> |
| 34 | #include <media/AudioSystem.h> |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 35 | #include <media/IMediaHTTPService.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 36 | #include <media/mediaplayer.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 37 | #include <utils/Errors.h> |
| 38 | #include <utils/Log.h> |
| 39 | #include <utils/String16.h> |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 40 | #include <utils/Trace.h> |
| 41 | #include <system/camera_vendor_tags.h> |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 42 | #include <system/camera_metadata.h> |
| 43 | #include <system/camera.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 44 | |
| 45 | #include "CameraService.h" |
Eino-Ville Talvala | 7b82efe | 2013-07-25 17:12:35 -0700 | [diff] [blame] | 46 | #include "api1/CameraClient.h" |
| 47 | #include "api1/Camera2Client.h" |
| 48 | #include "api_pro/ProCamera2Client.h" |
| 49 | #include "api2/CameraDeviceClient.h" |
Igor Murashkin | ff3e31d | 2013-10-23 16:40:06 -0700 | [diff] [blame] | 50 | #include "utils/CameraTraces.h" |
Igor Murashkin | 98e2472 | 2013-06-19 19:51:04 -0700 | [diff] [blame] | 51 | #include "CameraDeviceFactory.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 52 | |
| 53 | namespace android { |
| 54 | |
| 55 | // ---------------------------------------------------------------------------- |
| 56 | // Logging support -- this is for debugging only |
| 57 | // Use "adb shell dumpsys media.camera -v 1" to change it. |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 58 | volatile int32_t gLogLevel = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 59 | |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 60 | #define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__); |
| 61 | #define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 62 | |
| 63 | static void setLogLevel(int level) { |
| 64 | android_atomic_write(level, &gLogLevel); |
| 65 | } |
| 66 | |
| 67 | // ---------------------------------------------------------------------------- |
| 68 | |
| 69 | static int getCallingPid() { |
| 70 | return IPCThreadState::self()->getCallingPid(); |
| 71 | } |
| 72 | |
| 73 | static int getCallingUid() { |
| 74 | return IPCThreadState::self()->getCallingUid(); |
| 75 | } |
| 76 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 77 | extern "C" { |
| 78 | static void camera_device_status_change( |
| 79 | const struct camera_module_callbacks* callbacks, |
| 80 | int camera_id, |
| 81 | int new_status) { |
| 82 | sp<CameraService> cs = const_cast<CameraService*>( |
| 83 | static_cast<const CameraService*>(callbacks)); |
| 84 | |
| 85 | cs->onDeviceStatusChanged( |
| 86 | camera_id, |
| 87 | new_status); |
| 88 | } |
| 89 | } // extern "C" |
| 90 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 91 | // ---------------------------------------------------------------------------- |
| 92 | |
| 93 | // This is ugly and only safe if we never re-create the CameraService, but |
| 94 | // should be ok for now. |
| 95 | static CameraService *gCameraService; |
| 96 | |
| 97 | CameraService::CameraService() |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 98 | :mSoundRef(0), mModule(0) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 99 | { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 100 | ALOGI("CameraService started (pid=%d)", getpid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 101 | gCameraService = this; |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 102 | |
| 103 | for (size_t i = 0; i < MAX_CAMERAS; ++i) { |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 104 | mStatusList[i] = ICameraServiceListener::STATUS_PRESENT; |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 105 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 106 | |
| 107 | this->camera_device_status_change = android::camera_device_status_change; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 110 | void CameraService::onFirstRef() |
| 111 | { |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 112 | LOG1("CameraService::onFirstRef"); |
| 113 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 114 | BnCameraService::onFirstRef(); |
| 115 | |
| 116 | if (hw_get_module(CAMERA_HARDWARE_MODULE_ID, |
| 117 | (const hw_module_t **)&mModule) < 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 118 | ALOGE("Could not load camera HAL module"); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 119 | mNumberOfCameras = 0; |
| 120 | } |
| 121 | else { |
Alex Ray | c0dd54f | 2013-02-20 13:39:37 -0800 | [diff] [blame] | 122 | ALOGI("Loaded \"%s\" camera module", mModule->common.name); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 123 | mNumberOfCameras = mModule->get_number_of_cameras(); |
| 124 | if (mNumberOfCameras > MAX_CAMERAS) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 125 | ALOGE("Number of cameras(%d) > MAX_CAMERAS(%d).", |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 126 | mNumberOfCameras, MAX_CAMERAS); |
| 127 | mNumberOfCameras = MAX_CAMERAS; |
| 128 | } |
| 129 | for (int i = 0; i < mNumberOfCameras; i++) { |
| 130 | setCameraFree(i); |
| 131 | } |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 132 | |
| 133 | if (mModule->common.module_api_version >= |
| 134 | CAMERA_MODULE_API_VERSION_2_1) { |
| 135 | mModule->set_callbacks(this); |
| 136 | } |
Igor Murashkin | 98e2472 | 2013-06-19 19:51:04 -0700 | [diff] [blame] | 137 | |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 138 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); |
| 139 | |
| 140 | if (mModule->common.module_api_version >= CAMERA_MODULE_API_VERSION_2_2) { |
| 141 | setUpVendorTags(); |
| 142 | } |
| 143 | |
Igor Murashkin | 98e2472 | 2013-06-19 19:51:04 -0700 | [diff] [blame] | 144 | CameraDeviceFactory::registerService(this); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 148 | CameraService::~CameraService() { |
| 149 | for (int i = 0; i < mNumberOfCameras; i++) { |
| 150 | if (mBusy[i]) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 151 | ALOGE("camera %d is still in use in destructor!", i); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 155 | VendorTagDescriptor::clearGlobalVendorTagDescriptor(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 156 | gCameraService = NULL; |
| 157 | } |
| 158 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 159 | void CameraService::onDeviceStatusChanged(int cameraId, |
| 160 | int newStatus) |
| 161 | { |
| 162 | ALOGI("%s: Status changed for cameraId=%d, newStatus=%d", __FUNCTION__, |
| 163 | cameraId, newStatus); |
| 164 | |
| 165 | if (cameraId < 0 || cameraId >= MAX_CAMERAS) { |
| 166 | ALOGE("%s: Bad camera ID %d", __FUNCTION__, cameraId); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | if ((int)getStatus(cameraId) == newStatus) { |
| 171 | ALOGE("%s: State transition to the same status 0x%x not allowed", |
| 172 | __FUNCTION__, (uint32_t)newStatus); |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | /* don't do this in updateStatus |
| 177 | since it is also called from connect and we could get into a deadlock */ |
| 178 | if (newStatus == CAMERA_DEVICE_STATUS_NOT_PRESENT) { |
| 179 | Vector<sp<BasicClient> > clientsToDisconnect; |
| 180 | { |
| 181 | Mutex::Autolock al(mServiceLock); |
| 182 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 183 | /* Remove cached parameters from shim cache */ |
| 184 | mShimParams.removeItem(cameraId); |
| 185 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 186 | /* Find all clients that we need to disconnect */ |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 187 | sp<BasicClient> client = mClient[cameraId].promote(); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 188 | if (client.get() != NULL) { |
| 189 | clientsToDisconnect.push_back(client); |
| 190 | } |
| 191 | |
| 192 | int i = cameraId; |
| 193 | for (size_t j = 0; j < mProClientList[i].size(); ++j) { |
| 194 | sp<ProClient> cl = mProClientList[i][j].promote(); |
| 195 | if (cl != NULL) { |
| 196 | clientsToDisconnect.push_back(cl); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /* now disconnect them. don't hold the lock |
| 202 | or we can get into a deadlock */ |
| 203 | |
| 204 | for (size_t i = 0; i < clientsToDisconnect.size(); ++i) { |
| 205 | sp<BasicClient> client = clientsToDisconnect[i]; |
| 206 | |
| 207 | client->disconnect(); |
| 208 | /** |
| 209 | * The remote app will no longer be able to call methods on the |
| 210 | * client since the client PID will be reset to 0 |
| 211 | */ |
| 212 | } |
| 213 | |
Colin Cross | e5729fa | 2014-03-21 15:04:25 -0700 | [diff] [blame] | 214 | ALOGV("%s: After unplug, disconnected %zu clients", |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 215 | __FUNCTION__, clientsToDisconnect.size()); |
| 216 | } |
| 217 | |
| 218 | updateStatus( |
| 219 | static_cast<ICameraServiceListener::Status>(newStatus), cameraId); |
| 220 | |
| 221 | } |
| 222 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 223 | int32_t CameraService::getNumberOfCameras() { |
| 224 | return mNumberOfCameras; |
| 225 | } |
| 226 | |
| 227 | status_t CameraService::getCameraInfo(int cameraId, |
| 228 | struct CameraInfo* cameraInfo) { |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 229 | if (!mModule) { |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 230 | return -ENODEV; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 233 | if (cameraId < 0 || cameraId >= mNumberOfCameras) { |
| 234 | return BAD_VALUE; |
| 235 | } |
| 236 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 237 | struct camera_info info; |
| 238 | status_t rc = mModule->get_camera_info(cameraId, &info); |
| 239 | cameraInfo->facing = info.facing; |
| 240 | cameraInfo->orientation = info.orientation; |
| 241 | return rc; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 244 | |
| 245 | status_t CameraService::generateShimMetadata(int cameraId, /*out*/CameraMetadata* cameraInfo) { |
| 246 | status_t ret = OK; |
| 247 | struct CameraInfo info; |
| 248 | if ((ret = getCameraInfo(cameraId, &info)) != OK) { |
| 249 | return ret; |
| 250 | } |
| 251 | |
| 252 | CameraMetadata shimInfo; |
| 253 | int32_t orientation = static_cast<int32_t>(info.orientation); |
| 254 | if ((ret = shimInfo.update(ANDROID_SENSOR_ORIENTATION, &orientation, 1)) != OK) { |
| 255 | return ret; |
| 256 | } |
| 257 | |
| 258 | uint8_t facing = (info.facing == CAMERA_FACING_FRONT) ? |
| 259 | ANDROID_LENS_FACING_FRONT : ANDROID_LENS_FACING_BACK; |
| 260 | if ((ret = shimInfo.update(ANDROID_LENS_FACING, &facing, 1)) != OK) { |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | ssize_t index = -1; |
| 265 | { // Scope for service lock |
| 266 | Mutex::Autolock lock(mServiceLock); |
| 267 | index = mShimParams.indexOfKey(cameraId); |
| 268 | // Release service lock so initializeShimMetadata can be called correctly. |
| 269 | } |
| 270 | |
| 271 | if (index < 0) { |
| 272 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
| 273 | ret = initializeShimMetadata(cameraId); |
| 274 | IPCThreadState::self()->restoreCallingIdentity(token); |
| 275 | if (ret != OK) { |
| 276 | return ret; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | Vector<Size> sizes; |
Ruben Brunk | 152dbcf | 2014-06-12 19:11:45 -0700 | [diff] [blame^] | 281 | Vector<Size> jpegSizes; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 282 | Vector<int32_t> formats; |
| 283 | const char* supportedPreviewFormats; |
| 284 | { // Scope for service lock |
| 285 | Mutex::Autolock lock(mServiceLock); |
| 286 | index = mShimParams.indexOfKey(cameraId); |
| 287 | |
| 288 | mShimParams[index].getSupportedPreviewSizes(/*out*/sizes); |
| 289 | |
| 290 | mShimParams[index].getSupportedPreviewFormats(/*out*/formats); |
Ruben Brunk | 152dbcf | 2014-06-12 19:11:45 -0700 | [diff] [blame^] | 291 | |
| 292 | mShimParams[index].getSupportedPictureSizes(/*out*/jpegSizes); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | // Always include IMPLEMENTATION_DEFINED |
| 296 | formats.add(HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED); |
| 297 | |
| 298 | const size_t INTS_PER_CONFIG = 4; |
| 299 | |
| 300 | // Build available stream configurations metadata |
Ruben Brunk | 152dbcf | 2014-06-12 19:11:45 -0700 | [diff] [blame^] | 301 | size_t streamConfigSize = (sizes.size() * formats.size() + jpegSizes.size()) * INTS_PER_CONFIG; |
| 302 | |
| 303 | Vector<int32_t> streamConfigs; |
| 304 | streamConfigs.setCapacity(streamConfigSize); |
| 305 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 306 | for (size_t i = 0; i < formats.size(); ++i) { |
| 307 | for (size_t j = 0; j < sizes.size(); ++j) { |
Ruben Brunk | 152dbcf | 2014-06-12 19:11:45 -0700 | [diff] [blame^] | 308 | streamConfigs.add(formats[i]); |
| 309 | streamConfigs.add(sizes[j].width); |
| 310 | streamConfigs.add(sizes[j].height); |
| 311 | streamConfigs.add(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT); |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
Ruben Brunk | 152dbcf | 2014-06-12 19:11:45 -0700 | [diff] [blame^] | 315 | for (size_t i = 0; i < jpegSizes.size(); ++i) { |
| 316 | streamConfigs.add(HAL_PIXEL_FORMAT_BLOB); |
| 317 | streamConfigs.add(jpegSizes[i].width); |
| 318 | streamConfigs.add(jpegSizes[i].height); |
| 319 | streamConfigs.add(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT); |
| 320 | } |
| 321 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 322 | if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS, |
Ruben Brunk | 152dbcf | 2014-06-12 19:11:45 -0700 | [diff] [blame^] | 323 | streamConfigs.array(), streamConfigSize)) != OK) { |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 324 | return ret; |
| 325 | } |
| 326 | |
| 327 | int64_t fakeMinFrames[0]; |
| 328 | // TODO: Fixme, don't fake min frame durations. |
| 329 | if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS, |
| 330 | fakeMinFrames, 0)) != OK) { |
| 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | int64_t fakeStalls[0]; |
| 335 | // TODO: Fixme, don't fake stall durations. |
| 336 | if ((ret = shimInfo.update(ANDROID_SCALER_AVAILABLE_STALL_DURATIONS, |
| 337 | fakeStalls, 0)) != OK) { |
| 338 | return ret; |
| 339 | } |
| 340 | |
| 341 | *cameraInfo = shimInfo; |
| 342 | return OK; |
| 343 | } |
| 344 | |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 345 | status_t CameraService::getCameraCharacteristics(int cameraId, |
| 346 | CameraMetadata* cameraInfo) { |
| 347 | if (!cameraInfo) { |
| 348 | ALOGE("%s: cameraInfo is NULL", __FUNCTION__); |
| 349 | return BAD_VALUE; |
| 350 | } |
| 351 | |
| 352 | if (!mModule) { |
| 353 | ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__); |
| 354 | return -ENODEV; |
| 355 | } |
| 356 | |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 357 | if (cameraId < 0 || cameraId >= mNumberOfCameras) { |
| 358 | ALOGE("%s: Invalid camera id: %d", __FUNCTION__, cameraId); |
| 359 | return BAD_VALUE; |
| 360 | } |
| 361 | |
| 362 | int facing; |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 363 | status_t ret = OK; |
| 364 | if (mModule->common.module_api_version < CAMERA_MODULE_API_VERSION_2_0 || |
| 365 | getDeviceVersion(cameraId, &facing) <= CAMERA_DEVICE_API_VERSION_2_1 ) { |
| 366 | /** |
| 367 | * Backwards compatibility mode for old HALs: |
| 368 | * - Convert CameraInfo into static CameraMetadata properties. |
| 369 | * - Retrieve cached CameraParameters for this camera. If none exist, |
| 370 | * attempt to open CameraClient and retrieve the CameraParameters. |
| 371 | * - Convert cached CameraParameters into static CameraMetadata |
| 372 | * properties. |
| 373 | */ |
| 374 | ALOGI("%s: Switching to HAL1 shim implementation...", __FUNCTION__); |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 375 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 376 | if ((ret = generateShimMetadata(cameraId, cameraInfo)) != OK) { |
| 377 | return ret; |
| 378 | } |
Zhijun He | f05e50e | 2013-10-01 11:05:33 -0700 | [diff] [blame] | 379 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 380 | } else { |
| 381 | /** |
| 382 | * Normal HAL 2.1+ codepath. |
| 383 | */ |
| 384 | struct camera_info info; |
| 385 | ret = mModule->get_camera_info(cameraId, &info); |
| 386 | *cameraInfo = info.static_camera_characteristics; |
| 387 | } |
Zhijun He | 2b59be8 | 2013-09-25 10:14:30 -0700 | [diff] [blame] | 388 | |
| 389 | return ret; |
| 390 | } |
| 391 | |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 392 | status_t CameraService::getCameraVendorTagDescriptor(/*out*/sp<VendorTagDescriptor>& desc) { |
| 393 | if (!mModule) { |
| 394 | ALOGE("%s: camera hardware module doesn't exist", __FUNCTION__); |
| 395 | return -ENODEV; |
| 396 | } |
| 397 | |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 398 | desc = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 399 | return OK; |
| 400 | } |
| 401 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 402 | int CameraService::getDeviceVersion(int cameraId, int* facing) { |
| 403 | struct camera_info info; |
| 404 | if (mModule->get_camera_info(cameraId, &info) != OK) { |
| 405 | return -1; |
| 406 | } |
| 407 | |
| 408 | int deviceVersion; |
| 409 | if (mModule->common.module_api_version >= CAMERA_MODULE_API_VERSION_2_0) { |
| 410 | deviceVersion = info.device_version; |
| 411 | } else { |
| 412 | deviceVersion = CAMERA_DEVICE_API_VERSION_1_0; |
| 413 | } |
| 414 | |
| 415 | if (facing) { |
| 416 | *facing = info.facing; |
| 417 | } |
| 418 | |
| 419 | return deviceVersion; |
| 420 | } |
| 421 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 422 | bool CameraService::isValidCameraId(int cameraId) { |
| 423 | int facing; |
| 424 | int deviceVersion = getDeviceVersion(cameraId, &facing); |
| 425 | |
| 426 | switch(deviceVersion) { |
| 427 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 428 | case CAMERA_DEVICE_API_VERSION_2_0: |
| 429 | case CAMERA_DEVICE_API_VERSION_2_1: |
| 430 | case CAMERA_DEVICE_API_VERSION_3_0: |
Zhijun He | 95dd5ba | 2014-03-26 18:18:00 -0700 | [diff] [blame] | 431 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 432 | case CAMERA_DEVICE_API_VERSION_3_2: |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 433 | return true; |
| 434 | default: |
| 435 | return false; |
| 436 | } |
| 437 | |
| 438 | return false; |
| 439 | } |
| 440 | |
Ruben Brunk | d1176ef | 2014-02-21 10:51:38 -0800 | [diff] [blame] | 441 | bool CameraService::setUpVendorTags() { |
| 442 | vendor_tag_ops_t vOps = vendor_tag_ops_t(); |
| 443 | |
| 444 | // Check if vendor operations have been implemented |
| 445 | if (mModule->get_vendor_tag_ops == NULL) { |
| 446 | ALOGI("%s: No vendor tags defined for this device.", __FUNCTION__); |
| 447 | return false; |
| 448 | } |
| 449 | |
| 450 | ATRACE_BEGIN("camera3->get_metadata_vendor_tag_ops"); |
| 451 | mModule->get_vendor_tag_ops(&vOps); |
| 452 | ATRACE_END(); |
| 453 | |
| 454 | // Ensure all vendor operations are present |
| 455 | if (vOps.get_tag_count == NULL || vOps.get_all_tags == NULL || |
| 456 | vOps.get_section_name == NULL || vOps.get_tag_name == NULL || |
| 457 | vOps.get_tag_type == NULL) { |
| 458 | ALOGE("%s: Vendor tag operations not fully defined. Ignoring definitions." |
| 459 | , __FUNCTION__); |
| 460 | return false; |
| 461 | } |
| 462 | |
| 463 | // Read all vendor tag definitions into a descriptor |
| 464 | sp<VendorTagDescriptor> desc; |
| 465 | status_t res; |
| 466 | if ((res = VendorTagDescriptor::createDescriptorFromOps(&vOps, /*out*/desc)) |
| 467 | != OK) { |
| 468 | ALOGE("%s: Could not generate descriptor from vendor tag operations," |
| 469 | "received error %s (%d). Camera clients will not be able to use" |
| 470 | "vendor tags", __FUNCTION__, strerror(res), res); |
| 471 | return false; |
| 472 | } |
| 473 | |
| 474 | // Set the global descriptor to use with camera metadata |
| 475 | VendorTagDescriptor::setAsGlobalVendorTagDescriptor(desc); |
| 476 | return true; |
| 477 | } |
| 478 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 479 | status_t CameraService::initializeShimMetadata(int cameraId) { |
| 480 | int pid = getCallingPid(); |
| 481 | int uid = getCallingUid(); |
| 482 | status_t ret = validateConnect(cameraId, uid); |
| 483 | if (ret != OK) { |
| 484 | return ret; |
| 485 | } |
| 486 | |
| 487 | bool needsNewClient = false; |
| 488 | sp<Client> client; |
| 489 | |
| 490 | String16 internalPackageName("media"); |
| 491 | { // Scope for service lock |
| 492 | Mutex::Autolock lock(mServiceLock); |
| 493 | if (mClient[cameraId] != NULL) { |
| 494 | client = static_cast<Client*>(mClient[cameraId].promote().get()); |
| 495 | } |
| 496 | if (client == NULL) { |
| 497 | needsNewClient = true; |
| 498 | ret = connectHelperLocked(/*cameraClient*/NULL, // Empty binder callbacks |
| 499 | cameraId, |
| 500 | internalPackageName, |
| 501 | uid, |
| 502 | pid, |
| 503 | client); |
| 504 | |
| 505 | if (ret != OK) { |
| 506 | return ret; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | if (client == NULL) { |
| 511 | ALOGE("%s: Could not connect to client camera device.", __FUNCTION__); |
| 512 | return BAD_VALUE; |
| 513 | } |
| 514 | |
| 515 | String8 rawParams = client->getParameters(); |
| 516 | CameraParameters params(rawParams); |
| 517 | mShimParams.add(cameraId, params); |
| 518 | } |
| 519 | |
| 520 | // Close client if one was opened solely for this call |
| 521 | if (needsNewClient) { |
| 522 | client->disconnect(); |
| 523 | } |
| 524 | return OK; |
| 525 | } |
| 526 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 527 | status_t CameraService::validateConnect(int cameraId, |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 528 | /*inout*/ |
| 529 | int& clientUid) const { |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 530 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 531 | int callingPid = getCallingPid(); |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 532 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 533 | if (clientUid == USE_CALLING_UID) { |
| 534 | clientUid = getCallingUid(); |
| 535 | } else { |
| 536 | // We only trust our own process to forward client UIDs |
| 537 | if (callingPid != getpid()) { |
| 538 | ALOGE("CameraService::connect X (pid %d) rejected (don't trust clientUid)", |
| 539 | callingPid); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 540 | return PERMISSION_DENIED; |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 541 | } |
| 542 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 543 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 544 | if (!mModule) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 545 | ALOGE("Camera HAL module not loaded"); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 546 | return -ENODEV; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 549 | if (cameraId < 0 || cameraId >= mNumberOfCameras) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 550 | ALOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 551 | callingPid, cameraId); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 552 | return -ENODEV; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 555 | char value[PROPERTY_VALUE_MAX]; |
| 556 | property_get("sys.secpolicy.camera.disabled", value, "0"); |
| 557 | if (strcmp(value, "1") == 0) { |
| 558 | // Camera is disabled by DevicePolicyManager. |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 559 | ALOGI("Camera is disabled. connect X (pid %d) rejected", callingPid); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 560 | return -EACCES; |
Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 561 | } |
| 562 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 563 | ICameraServiceListener::Status currentStatus = getStatus(cameraId); |
| 564 | if (currentStatus == ICameraServiceListener::STATUS_NOT_PRESENT) { |
| 565 | ALOGI("Camera is not plugged in," |
| 566 | " connect X (pid %d) rejected", callingPid); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 567 | return -ENODEV; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 568 | } else if (currentStatus == ICameraServiceListener::STATUS_ENUMERATING) { |
| 569 | ALOGI("Camera is enumerating," |
| 570 | " connect X (pid %d) rejected", callingPid); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 571 | return -EBUSY; |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 572 | } |
| 573 | // Else don't check for STATUS_NOT_AVAILABLE. |
| 574 | // -- It's done implicitly in canConnectUnsafe /w the mBusy array |
| 575 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 576 | return OK; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | bool CameraService::canConnectUnsafe(int cameraId, |
| 580 | const String16& clientPackageName, |
| 581 | const sp<IBinder>& remoteCallback, |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 582 | sp<BasicClient> &client) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 583 | String8 clientName8(clientPackageName); |
| 584 | int callingPid = getCallingPid(); |
| 585 | |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 586 | if (mClient[cameraId] != 0) { |
| 587 | client = mClient[cameraId].promote(); |
| 588 | if (client != 0) { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 589 | if (remoteCallback == client->getRemote()) { |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 590 | LOG1("CameraService::connect X (pid %d) (the same client)", |
| 591 | callingPid); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 592 | return true; |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 593 | } else { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 594 | // TODOSC: need to support 1 regular client, |
| 595 | // multiple shared clients here |
| 596 | ALOGW("CameraService::connect X (pid %d) rejected" |
| 597 | " (existing client).", callingPid); |
| 598 | return false; |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 599 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 600 | } |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 601 | mClient[cameraId].clear(); |
| 602 | } |
| 603 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 604 | /* |
| 605 | mBusy is set to false as the last step of the Client destructor, |
| 606 | after which it is guaranteed that the Client destructor has finished ( |
| 607 | including any inherited destructors) |
| 608 | |
| 609 | We only need this for a Client subclasses since we don't allow |
| 610 | multiple Clents to be opened concurrently, but multiple BasicClient |
| 611 | would be fine |
| 612 | */ |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 613 | if (mBusy[cameraId]) { |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 614 | ALOGW("CameraService::connect X (pid %d, \"%s\") rejected" |
| 615 | " (camera %d is still busy).", callingPid, |
| 616 | clientName8.string(), cameraId); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 617 | return false; |
| 618 | } |
| 619 | |
| 620 | return true; |
| 621 | } |
| 622 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 623 | status_t CameraService::connectHelperLocked(const sp<ICameraClient>& cameraClient, |
| 624 | int cameraId, |
| 625 | const String16& clientPackageName, |
| 626 | int clientUid, |
| 627 | int callingPid, |
| 628 | /*out*/ |
| 629 | sp<Client>& client) { |
| 630 | |
| 631 | int facing = -1; |
| 632 | int deviceVersion = getDeviceVersion(cameraId, &facing); |
| 633 | |
| 634 | // If there are other non-exclusive users of the camera, |
| 635 | // this will tear them down before we can reuse the camera |
| 636 | if (isValidCameraId(cameraId)) { |
| 637 | // transition from PRESENT -> NOT_AVAILABLE |
| 638 | updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE, |
| 639 | cameraId); |
| 640 | } |
| 641 | |
| 642 | switch(deviceVersion) { |
| 643 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 644 | client = new CameraClient(this, cameraClient, |
| 645 | clientPackageName, cameraId, |
| 646 | facing, callingPid, clientUid, getpid()); |
| 647 | break; |
| 648 | case CAMERA_DEVICE_API_VERSION_2_0: |
| 649 | case CAMERA_DEVICE_API_VERSION_2_1: |
| 650 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 651 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 652 | case CAMERA_DEVICE_API_VERSION_3_2: |
| 653 | client = new Camera2Client(this, cameraClient, |
| 654 | clientPackageName, cameraId, |
| 655 | facing, callingPid, clientUid, getpid(), |
| 656 | deviceVersion); |
| 657 | break; |
| 658 | case -1: |
| 659 | ALOGE("Invalid camera id %d", cameraId); |
| 660 | return BAD_VALUE; |
| 661 | default: |
| 662 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
| 663 | return INVALID_OPERATION; |
| 664 | } |
| 665 | |
| 666 | status_t status = connectFinishUnsafe(client, client->getRemote()); |
| 667 | if (status != OK) { |
| 668 | // this is probably not recoverable.. maybe the client can try again |
| 669 | // OK: we can only get here if we were originally in PRESENT state |
| 670 | updateStatus(ICameraServiceListener::STATUS_PRESENT, cameraId); |
| 671 | return status; |
| 672 | } |
| 673 | |
| 674 | mClient[cameraId] = client; |
| 675 | LOG1("CameraService::connect X (id %d, this pid is %d)", cameraId, |
| 676 | getpid()); |
| 677 | |
| 678 | return OK; |
| 679 | } |
| 680 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 681 | status_t CameraService::connect( |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 682 | const sp<ICameraClient>& cameraClient, |
| 683 | int cameraId, |
| 684 | const String16& clientPackageName, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 685 | int clientUid, |
| 686 | /*out*/ |
| 687 | sp<ICamera>& device) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 688 | |
| 689 | String8 clientName8(clientPackageName); |
| 690 | int callingPid = getCallingPid(); |
| 691 | |
| 692 | LOG1("CameraService::connect E (pid %d \"%s\", id %d)", callingPid, |
| 693 | clientName8.string(), cameraId); |
| 694 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 695 | status_t status = validateConnect(cameraId, /*inout*/clientUid); |
| 696 | if (status != OK) { |
| 697 | return status; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 698 | } |
| 699 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 700 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 701 | sp<Client> client; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 702 | { |
| 703 | Mutex::Autolock lock(mServiceLock); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 704 | sp<BasicClient> clientTmp; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 705 | if (!canConnectUnsafe(cameraId, clientPackageName, |
| 706 | cameraClient->asBinder(), |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 707 | /*out*/clientTmp)) { |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 708 | return -EBUSY; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 709 | } else if (client.get() != NULL) { |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 710 | device = static_cast<Client*>(clientTmp.get()); |
| 711 | return OK; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 714 | status = connectHelperLocked(cameraClient, |
| 715 | cameraId, |
| 716 | clientPackageName, |
| 717 | clientUid, |
| 718 | callingPid, |
| 719 | client); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 720 | if (status != OK) { |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 721 | return status; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 722 | } |
| 723 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 724 | } |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 725 | // important: release the mutex here so the client can call back |
| 726 | // into the service from its destructor (can be at the end of the call) |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 727 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 728 | device = client; |
| 729 | return OK; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 730 | } |
| 731 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 732 | status_t CameraService::connectFinishUnsafe(const sp<BasicClient>& client, |
| 733 | const sp<IBinder>& remoteCallback) { |
| 734 | status_t status = client->initialize(mModule); |
| 735 | if (status != OK) { |
| 736 | return status; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 737 | } |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 738 | if (remoteCallback != NULL) { |
| 739 | remoteCallback->linkToDeath(this); |
| 740 | } |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 741 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 742 | return OK; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 743 | } |
| 744 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 745 | status_t CameraService::connectPro( |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 746 | const sp<IProCameraCallbacks>& cameraCb, |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 747 | int cameraId, |
| 748 | const String16& clientPackageName, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 749 | int clientUid, |
| 750 | /*out*/ |
| 751 | sp<IProCameraUser>& device) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 752 | { |
Natalie Silvanovich | e1b55da | 2014-05-01 14:44:52 -0700 | [diff] [blame] | 753 | if (cameraCb == 0) { |
| 754 | ALOGE("%s: Callback must not be null", __FUNCTION__); |
| 755 | return BAD_VALUE; |
| 756 | } |
| 757 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 758 | String8 clientName8(clientPackageName); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 759 | int callingPid = getCallingPid(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 760 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 761 | LOG1("CameraService::connectPro E (pid %d \"%s\", id %d)", callingPid, |
| 762 | clientName8.string(), cameraId); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 763 | status_t status = validateConnect(cameraId, /*inout*/clientUid); |
| 764 | if (status != OK) { |
| 765 | return status; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 766 | } |
| 767 | |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 768 | sp<ProClient> client; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 769 | { |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 770 | Mutex::Autolock lock(mServiceLock); |
| 771 | { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 772 | sp<BasicClient> client; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 773 | if (!canConnectUnsafe(cameraId, clientPackageName, |
| 774 | cameraCb->asBinder(), |
| 775 | /*out*/client)) { |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 776 | return -EBUSY; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 777 | } |
| 778 | } |
| 779 | |
| 780 | int facing = -1; |
| 781 | int deviceVersion = getDeviceVersion(cameraId, &facing); |
| 782 | |
| 783 | switch(deviceVersion) { |
| 784 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 785 | ALOGE("Camera id %d uses HALv1, doesn't support ProCamera", |
| 786 | cameraId); |
Ruben Brunk | 17963d1 | 2013-08-19 15:21:19 -0700 | [diff] [blame] | 787 | return -EOPNOTSUPP; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 788 | break; |
| 789 | case CAMERA_DEVICE_API_VERSION_2_0: |
| 790 | case CAMERA_DEVICE_API_VERSION_2_1: |
Zhijun He | 4711005 | 2013-07-22 17:34:34 -0700 | [diff] [blame] | 791 | case CAMERA_DEVICE_API_VERSION_3_0: |
Zhijun He | 95dd5ba | 2014-03-26 18:18:00 -0700 | [diff] [blame] | 792 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 793 | case CAMERA_DEVICE_API_VERSION_3_2: |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 794 | client = new ProCamera2Client(this, cameraCb, String16(), |
| 795 | cameraId, facing, callingPid, USE_CALLING_UID, getpid()); |
| 796 | break; |
| 797 | case -1: |
| 798 | ALOGE("Invalid camera id %d", cameraId); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 799 | return BAD_VALUE; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 800 | default: |
| 801 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 802 | return INVALID_OPERATION; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 803 | } |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 804 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 805 | status_t status = connectFinishUnsafe(client, client->getRemote()); |
| 806 | if (status != OK) { |
| 807 | return status; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | mProClientList[cameraId].push(client); |
| 811 | |
| 812 | LOG1("CameraService::connectPro X (id %d, this pid is %d)", cameraId, |
| 813 | getpid()); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 814 | } |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 815 | // important: release the mutex here so the client can call back |
| 816 | // into the service from its destructor (can be at the end of the call) |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 817 | device = client; |
| 818 | return OK; |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 819 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 820 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 821 | status_t CameraService::connectDevice( |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 822 | const sp<ICameraDeviceCallbacks>& cameraCb, |
| 823 | int cameraId, |
| 824 | const String16& clientPackageName, |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 825 | int clientUid, |
| 826 | /*out*/ |
| 827 | sp<ICameraDeviceUser>& device) |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 828 | { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 829 | |
| 830 | String8 clientName8(clientPackageName); |
| 831 | int callingPid = getCallingPid(); |
| 832 | |
| 833 | LOG1("CameraService::connectDevice E (pid %d \"%s\", id %d)", callingPid, |
| 834 | clientName8.string(), cameraId); |
| 835 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 836 | status_t status = validateConnect(cameraId, /*inout*/clientUid); |
| 837 | if (status != OK) { |
| 838 | return status; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | sp<CameraDeviceClient> client; |
| 842 | { |
| 843 | Mutex::Autolock lock(mServiceLock); |
| 844 | { |
| 845 | sp<BasicClient> client; |
| 846 | if (!canConnectUnsafe(cameraId, clientPackageName, |
| 847 | cameraCb->asBinder(), |
| 848 | /*out*/client)) { |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 849 | return -EBUSY; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 850 | } |
| 851 | } |
| 852 | |
| 853 | int facing = -1; |
| 854 | int deviceVersion = getDeviceVersion(cameraId, &facing); |
| 855 | |
| 856 | // If there are other non-exclusive users of the camera, |
| 857 | // this will tear them down before we can reuse the camera |
| 858 | if (isValidCameraId(cameraId)) { |
| 859 | // transition from PRESENT -> NOT_AVAILABLE |
| 860 | updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE, |
| 861 | cameraId); |
| 862 | } |
| 863 | |
| 864 | switch(deviceVersion) { |
| 865 | case CAMERA_DEVICE_API_VERSION_1_0: |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 866 | ALOGW("Camera using old HAL version: %d", deviceVersion); |
Ruben Brunk | 17963d1 | 2013-08-19 15:21:19 -0700 | [diff] [blame] | 867 | return -EOPNOTSUPP; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 868 | // TODO: don't allow 2.0 Only allow 2.1 and higher |
| 869 | case CAMERA_DEVICE_API_VERSION_2_0: |
| 870 | case CAMERA_DEVICE_API_VERSION_2_1: |
| 871 | case CAMERA_DEVICE_API_VERSION_3_0: |
Zhijun He | 95dd5ba | 2014-03-26 18:18:00 -0700 | [diff] [blame] | 872 | case CAMERA_DEVICE_API_VERSION_3_1: |
| 873 | case CAMERA_DEVICE_API_VERSION_3_2: |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 874 | client = new CameraDeviceClient(this, cameraCb, String16(), |
| 875 | cameraId, facing, callingPid, USE_CALLING_UID, getpid()); |
| 876 | break; |
| 877 | case -1: |
| 878 | ALOGE("Invalid camera id %d", cameraId); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 879 | return BAD_VALUE; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 880 | default: |
| 881 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 882 | return INVALID_OPERATION; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 883 | } |
| 884 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 885 | status_t status = connectFinishUnsafe(client, client->getRemote()); |
| 886 | if (status != OK) { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 887 | // this is probably not recoverable.. maybe the client can try again |
| 888 | // OK: we can only get here if we were originally in PRESENT state |
| 889 | updateStatus(ICameraServiceListener::STATUS_PRESENT, cameraId); |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 890 | return status; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | LOG1("CameraService::connectDevice X (id %d, this pid is %d)", cameraId, |
| 894 | getpid()); |
| 895 | |
| 896 | mClient[cameraId] = client; |
| 897 | } |
| 898 | // important: release the mutex here so the client can call back |
| 899 | // into the service from its destructor (can be at the end of the call) |
| 900 | |
Ruben Brunk | 0f61d8f | 2013-08-08 13:07:18 -0700 | [diff] [blame] | 901 | device = client; |
| 902 | return OK; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 906 | status_t CameraService::addListener( |
| 907 | const sp<ICameraServiceListener>& listener) { |
| 908 | ALOGV("%s: Add listener %p", __FUNCTION__, listener.get()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 909 | |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 910 | if (listener == 0) { |
| 911 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
| 912 | return BAD_VALUE; |
| 913 | } |
| 914 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 915 | Mutex::Autolock lock(mServiceLock); |
| 916 | |
| 917 | Vector<sp<ICameraServiceListener> >::iterator it, end; |
| 918 | for (it = mListenerList.begin(); it != mListenerList.end(); ++it) { |
| 919 | if ((*it)->asBinder() == listener->asBinder()) { |
| 920 | ALOGW("%s: Tried to add listener %p which was already subscribed", |
| 921 | __FUNCTION__, listener.get()); |
| 922 | return ALREADY_EXISTS; |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | mListenerList.push_back(listener); |
| 927 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 928 | /* Immediately signal current status to this listener only */ |
| 929 | { |
| 930 | Mutex::Autolock m(mStatusMutex) ; |
| 931 | int numCams = getNumberOfCameras(); |
| 932 | for (int i = 0; i < numCams; ++i) { |
| 933 | listener->onStatusChanged(mStatusList[i], i); |
| 934 | } |
| 935 | } |
| 936 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 937 | return OK; |
| 938 | } |
| 939 | status_t CameraService::removeListener( |
| 940 | const sp<ICameraServiceListener>& listener) { |
| 941 | ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get()); |
| 942 | |
Igor Murashkin | bd3e2e0 | 2014-03-17 13:01:41 -0700 | [diff] [blame] | 943 | if (listener == 0) { |
| 944 | ALOGE("%s: Listener must not be null", __FUNCTION__); |
| 945 | return BAD_VALUE; |
| 946 | } |
| 947 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 948 | Mutex::Autolock lock(mServiceLock); |
| 949 | |
| 950 | Vector<sp<ICameraServiceListener> >::iterator it; |
| 951 | for (it = mListenerList.begin(); it != mListenerList.end(); ++it) { |
| 952 | if ((*it)->asBinder() == listener->asBinder()) { |
| 953 | mListenerList.erase(it); |
| 954 | return OK; |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | ALOGW("%s: Tried to remove a listener %p which was not subscribed", |
| 959 | __FUNCTION__, listener.get()); |
| 960 | |
| 961 | return BAD_VALUE; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | void CameraService::removeClientByRemote(const wp<IBinder>& remoteBinder) { |
| 965 | int callingPid = getCallingPid(); |
| 966 | LOG1("CameraService::removeClientByRemote E (pid %d)", callingPid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 967 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 968 | // Declare this before the lock to make absolutely sure the |
| 969 | // destructor won't be called with the lock held. |
| 970 | Mutex::Autolock lock(mServiceLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 971 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 972 | int outIndex; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 973 | sp<BasicClient> client = findClientUnsafe(remoteBinder, outIndex); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 974 | |
| 975 | if (client != 0) { |
| 976 | // Found our camera, clear and leave. |
| 977 | LOG1("removeClient: clear camera %d", outIndex); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 978 | |
Ruben Brunk | b2119af | 2014-05-09 19:57:56 -0700 | [diff] [blame] | 979 | sp<IBinder> remote = client->getRemote(); |
| 980 | if (remote != NULL) { |
| 981 | remote->unlinkToDeath(this); |
| 982 | } |
| 983 | |
| 984 | mClient[outIndex].clear(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 985 | } else { |
| 986 | |
| 987 | sp<ProClient> clientPro = findProClientUnsafe(remoteBinder); |
| 988 | |
| 989 | if (clientPro != NULL) { |
| 990 | // Found our camera, clear and leave. |
| 991 | LOG1("removeClient: clear pro %p", clientPro.get()); |
| 992 | |
| 993 | clientPro->getRemoteCallback()->asBinder()->unlinkToDeath(this); |
| 994 | } |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 995 | } |
| 996 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 997 | LOG1("CameraService::removeClientByRemote X (pid %d)", callingPid); |
| 998 | } |
| 999 | |
| 1000 | sp<CameraService::ProClient> CameraService::findProClientUnsafe( |
| 1001 | const wp<IBinder>& cameraCallbacksRemote) |
| 1002 | { |
| 1003 | sp<ProClient> clientPro; |
| 1004 | |
| 1005 | for (int i = 0; i < mNumberOfCameras; ++i) { |
| 1006 | Vector<size_t> removeIdx; |
| 1007 | |
| 1008 | for (size_t j = 0; j < mProClientList[i].size(); ++j) { |
| 1009 | wp<ProClient> cl = mProClientList[i][j]; |
| 1010 | |
| 1011 | sp<ProClient> clStrong = cl.promote(); |
| 1012 | if (clStrong != NULL && clStrong->getRemote() == cameraCallbacksRemote) { |
| 1013 | clientPro = clStrong; |
| 1014 | break; |
| 1015 | } else if (clStrong == NULL) { |
| 1016 | // mark to clean up dead ptr |
| 1017 | removeIdx.push(j); |
| 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | // remove stale ptrs (in reverse so the indices dont change) |
| 1022 | for (ssize_t j = (ssize_t)removeIdx.size() - 1; j >= 0; --j) { |
| 1023 | mProClientList[i].removeAt(removeIdx[j]); |
| 1024 | } |
| 1025 | |
| 1026 | } |
| 1027 | |
| 1028 | return clientPro; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1031 | sp<CameraService::BasicClient> CameraService::findClientUnsafe( |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 1032 | const wp<IBinder>& cameraClient, int& outIndex) { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1033 | sp<BasicClient> client; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1034 | |
| 1035 | for (int i = 0; i < mNumberOfCameras; i++) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1036 | |
| 1037 | // This happens when we have already disconnected (or this is |
| 1038 | // just another unused camera). |
| 1039 | if (mClient[i] == 0) continue; |
| 1040 | |
| 1041 | // Promote mClient. It can fail if we are called from this path: |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1042 | // Client::~Client() -> disconnect() -> removeClientByRemote(). |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1043 | client = mClient[i].promote(); |
| 1044 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1045 | // Clean up stale client entry |
| 1046 | if (client == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1047 | mClient[i].clear(); |
| 1048 | continue; |
| 1049 | } |
| 1050 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1051 | if (cameraClient == client->getRemote()) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1052 | // Found our camera |
| 1053 | outIndex = i; |
| 1054 | return client; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1055 | } |
| 1056 | } |
| 1057 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1058 | outIndex = -1; |
| 1059 | return NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1062 | CameraService::BasicClient* CameraService::getClientByIdUnsafe(int cameraId) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1063 | if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL; |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 1064 | return mClient[cameraId].unsafe_get(); |
| 1065 | } |
| 1066 | |
| 1067 | Mutex* CameraService::getClientLockById(int cameraId) { |
| 1068 | if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL; |
| 1069 | return &mClientLock[cameraId]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1072 | sp<CameraService::BasicClient> CameraService::getClientByRemote( |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 1073 | const wp<IBinder>& cameraClient) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1074 | |
| 1075 | // Declare this before the lock to make absolutely sure the |
| 1076 | // destructor won't be called with the lock held. |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1077 | sp<BasicClient> client; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1078 | |
| 1079 | Mutex::Autolock lock(mServiceLock); |
| 1080 | |
| 1081 | int outIndex; |
| 1082 | client = findClientUnsafe(cameraClient, outIndex); |
| 1083 | |
| 1084 | return client; |
| 1085 | } |
| 1086 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1087 | status_t CameraService::onTransact( |
| 1088 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { |
| 1089 | // Permission checks |
| 1090 | switch (code) { |
| 1091 | case BnCameraService::CONNECT: |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1092 | case BnCameraService::CONNECT_PRO: |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1093 | const int pid = getCallingPid(); |
| 1094 | const int self_pid = getpid(); |
| 1095 | if (pid != self_pid) { |
| 1096 | // we're called from a different process, do the real check |
| 1097 | if (!checkCallingPermission( |
| 1098 | String16("android.permission.CAMERA"))) { |
| 1099 | const int uid = getCallingUid(); |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1100 | ALOGE("Permission Denial: " |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1101 | "can't use the camera pid=%d, uid=%d", pid, uid); |
| 1102 | return PERMISSION_DENIED; |
| 1103 | } |
| 1104 | } |
| 1105 | break; |
| 1106 | } |
| 1107 | |
| 1108 | return BnCameraService::onTransact(code, data, reply, flags); |
| 1109 | } |
| 1110 | |
| 1111 | // The reason we need this busy bit is a new CameraService::connect() request |
| 1112 | // may come in while the previous Client's destructor has not been run or is |
| 1113 | // still running. If the last strong reference of the previous Client is gone |
| 1114 | // but the destructor has not been finished, we should not allow the new Client |
| 1115 | // to be created because we need to wait for the previous Client to tear down |
| 1116 | // the hardware first. |
| 1117 | void CameraService::setCameraBusy(int cameraId) { |
| 1118 | android_atomic_write(1, &mBusy[cameraId]); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1119 | |
| 1120 | ALOGV("setCameraBusy cameraId=%d", cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | void CameraService::setCameraFree(int cameraId) { |
| 1124 | android_atomic_write(0, &mBusy[cameraId]); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1125 | |
| 1126 | ALOGV("setCameraFree cameraId=%d", cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | // We share the media players for shutter and recording sound for all clients. |
| 1130 | // A reference count is kept to determine when we will actually release the |
| 1131 | // media players. |
| 1132 | |
Chih-Chung Chang | ff4f55c | 2011-10-17 19:03:12 +0800 | [diff] [blame] | 1133 | MediaPlayer* CameraService::newMediaPlayer(const char *file) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1134 | MediaPlayer* mp = new MediaPlayer(); |
Andreas Huber | 1b86fe0 | 2014-01-29 11:13:26 -0800 | [diff] [blame] | 1135 | if (mp->setDataSource(NULL /* httpService */, file, NULL) == NO_ERROR) { |
Eino-Ville Talvala | 60a78ac | 2012-01-05 15:34:53 -0800 | [diff] [blame] | 1136 | mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1137 | mp->prepare(); |
| 1138 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1139 | ALOGE("Failed to load CameraService sounds: %s", file); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1140 | return NULL; |
| 1141 | } |
| 1142 | return mp; |
| 1143 | } |
| 1144 | |
| 1145 | void CameraService::loadSound() { |
| 1146 | Mutex::Autolock lock(mSoundLock); |
| 1147 | LOG1("CameraService::loadSound ref=%d", mSoundRef); |
| 1148 | if (mSoundRef++) return; |
| 1149 | |
| 1150 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); |
| 1151 | mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); |
| 1152 | } |
| 1153 | |
| 1154 | void CameraService::releaseSound() { |
| 1155 | Mutex::Autolock lock(mSoundLock); |
| 1156 | LOG1("CameraService::releaseSound ref=%d", mSoundRef); |
| 1157 | if (--mSoundRef) return; |
| 1158 | |
| 1159 | for (int i = 0; i < NUM_SOUNDS; i++) { |
| 1160 | if (mSoundPlayer[i] != 0) { |
| 1161 | mSoundPlayer[i]->disconnect(); |
| 1162 | mSoundPlayer[i].clear(); |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | void CameraService::playSound(sound_kind kind) { |
| 1168 | LOG1("playSound(%d)", kind); |
| 1169 | Mutex::Autolock lock(mSoundLock); |
| 1170 | sp<MediaPlayer> player = mSoundPlayer[kind]; |
| 1171 | if (player != 0) { |
Chih-Chung Chang | 8888a75 | 2011-10-20 10:47:26 +0800 | [diff] [blame] | 1172 | player->seekTo(0); |
| 1173 | player->start(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | // ---------------------------------------------------------------------------- |
| 1178 | |
| 1179 | CameraService::Client::Client(const sp<CameraService>& cameraService, |
Wu-cheng Li | b7a6794 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 1180 | const sp<ICameraClient>& cameraClient, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1181 | const String16& clientPackageName, |
| 1182 | int cameraId, int cameraFacing, |
| 1183 | int clientPid, uid_t clientUid, |
| 1184 | int servicePid) : |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1185 | CameraService::BasicClient(cameraService, cameraClient->asBinder(), |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1186 | clientPackageName, |
| 1187 | cameraId, cameraFacing, |
| 1188 | clientPid, clientUid, |
| 1189 | servicePid) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1190 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1191 | int callingPid = getCallingPid(); |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1192 | LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1193 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 1194 | mRemoteCallback = cameraClient; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1195 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1196 | cameraService->setCameraBusy(cameraId); |
| 1197 | cameraService->loadSound(); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1198 | |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 1199 | LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1202 | // tear down the client |
| 1203 | CameraService::Client::~Client() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 1204 | ALOGV("~Client"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1205 | mDestructionStarted = true; |
| 1206 | |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 1207 | mCameraService->releaseSound(); |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 1208 | // unconditionally disconnect. function is idempotent |
| 1209 | Client::disconnect(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1210 | } |
| 1211 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1212 | CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1213 | const sp<IBinder>& remoteCallback, |
| 1214 | const String16& clientPackageName, |
| 1215 | int cameraId, int cameraFacing, |
| 1216 | int clientPid, uid_t clientUid, |
| 1217 | int servicePid): |
| 1218 | mClientPackageName(clientPackageName) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1219 | { |
| 1220 | mCameraService = cameraService; |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 1221 | mRemoteBinder = remoteCallback; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1222 | mCameraId = cameraId; |
| 1223 | mCameraFacing = cameraFacing; |
| 1224 | mClientPid = clientPid; |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1225 | mClientUid = clientUid; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1226 | mServicePid = servicePid; |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1227 | mOpsActive = false; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1228 | mDestructionStarted = false; |
| 1229 | } |
| 1230 | |
| 1231 | CameraService::BasicClient::~BasicClient() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 1232 | ALOGV("~BasicClient"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1233 | mDestructionStarted = true; |
| 1234 | } |
| 1235 | |
| 1236 | void CameraService::BasicClient::disconnect() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 1237 | ALOGV("BasicClient::disconnect"); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 1238 | mCameraService->removeClientByRemote(mRemoteBinder); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1239 | // client shouldn't be able to call into us anymore |
| 1240 | mClientPid = 0; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1241 | } |
| 1242 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1243 | status_t CameraService::BasicClient::startCameraOps() { |
| 1244 | int32_t res; |
| 1245 | |
| 1246 | mOpsCallback = new OpsCallback(this); |
| 1247 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1248 | { |
| 1249 | ALOGV("%s: Start camera ops, package name = %s, client UID = %d", |
| 1250 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
| 1251 | } |
| 1252 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1253 | mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA, |
| 1254 | mClientPackageName, mOpsCallback); |
| 1255 | res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA, |
| 1256 | mClientUid, mClientPackageName); |
| 1257 | |
| 1258 | if (res != AppOpsManager::MODE_ALLOWED) { |
| 1259 | ALOGI("Camera %d: Access for \"%s\" has been revoked", |
| 1260 | mCameraId, String8(mClientPackageName).string()); |
| 1261 | return PERMISSION_DENIED; |
| 1262 | } |
| 1263 | mOpsActive = true; |
| 1264 | return OK; |
| 1265 | } |
| 1266 | |
| 1267 | status_t CameraService::BasicClient::finishCameraOps() { |
| 1268 | if (mOpsActive) { |
| 1269 | mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 1270 | mClientPackageName); |
| 1271 | mOpsActive = false; |
| 1272 | } |
| 1273 | mAppOpsManager.stopWatchingMode(mOpsCallback); |
| 1274 | mOpsCallback.clear(); |
| 1275 | |
| 1276 | return OK; |
| 1277 | } |
| 1278 | |
| 1279 | void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) { |
| 1280 | String8 name(packageName); |
| 1281 | String8 myName(mClientPackageName); |
| 1282 | |
| 1283 | if (op != AppOpsManager::OP_CAMERA) { |
| 1284 | ALOGW("Unexpected app ops notification received: %d", op); |
| 1285 | return; |
| 1286 | } |
| 1287 | |
| 1288 | int32_t res; |
| 1289 | res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA, |
| 1290 | mClientUid, mClientPackageName); |
| 1291 | ALOGV("checkOp returns: %d, %s ", res, |
| 1292 | res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" : |
| 1293 | res == AppOpsManager::MODE_IGNORED ? "IGNORED" : |
| 1294 | res == AppOpsManager::MODE_ERRORED ? "ERRORED" : |
| 1295 | "UNKNOWN"); |
| 1296 | |
| 1297 | if (res != AppOpsManager::MODE_ALLOWED) { |
| 1298 | ALOGI("Camera %d: Access for \"%s\" revoked", mCameraId, |
| 1299 | myName.string()); |
| 1300 | // Reset the client PID to allow server-initiated disconnect, |
| 1301 | // and to prevent further calls by client. |
| 1302 | mClientPid = getCallingPid(); |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1303 | CaptureResultExtras resultExtras; // a dummy result (invalid) |
| 1304 | notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE, resultExtras); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1305 | disconnect(); |
| 1306 | } |
| 1307 | } |
| 1308 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1309 | // ---------------------------------------------------------------------------- |
| 1310 | |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 1311 | Mutex* CameraService::Client::getClientLockFromCookie(void* user) { |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1312 | return gCameraService->getClientLockById((int)(intptr_t) user); |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | // Provide client pointer for callbacks. Client lock returned from getClientLockFromCookie should |
| 1316 | // be acquired for this to be safe |
| 1317 | CameraService::Client* CameraService::Client::getClientFromCookie(void* user) { |
Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1318 | BasicClient *basicClient = gCameraService->getClientByIdUnsafe((int)(intptr_t) user); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1319 | // OK: only CameraClient calls this, and they already cast anyway. |
| 1320 | Client* client = static_cast<Client*>(basicClient); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1321 | |
| 1322 | // This could happen if the Client is in the process of shutting down (the |
| 1323 | // last strong reference is gone, but the destructor hasn't finished |
| 1324 | // stopping the hardware). |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 1325 | if (client == NULL) return NULL; |
| 1326 | |
| 1327 | // destruction already started, so should not be accessed |
| 1328 | if (client->mDestructionStarted) return NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1329 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1330 | return client; |
| 1331 | } |
| 1332 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1333 | void CameraService::Client::notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode, |
| 1334 | const CaptureResultExtras& resultExtras) { |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 1335 | mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1336 | } |
| 1337 | |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 1338 | // NOTE: function is idempotent |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 1339 | void CameraService::Client::disconnect() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 1340 | ALOGV("Client::disconnect"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1341 | BasicClient::disconnect(); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 1342 | mCameraService->setCameraFree(mCameraId); |
Igor Murashkin | 93747b9 | 2013-05-01 15:42:20 -0700 | [diff] [blame] | 1343 | |
| 1344 | StatusVector rejectSourceStates; |
| 1345 | rejectSourceStates.push_back(ICameraServiceListener::STATUS_NOT_PRESENT); |
| 1346 | rejectSourceStates.push_back(ICameraServiceListener::STATUS_ENUMERATING); |
| 1347 | |
| 1348 | // Transition to PRESENT if the camera is not in either of above 2 states |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1349 | mCameraService->updateStatus(ICameraServiceListener::STATUS_PRESENT, |
Igor Murashkin | 93747b9 | 2013-05-01 15:42:20 -0700 | [diff] [blame] | 1350 | mCameraId, |
| 1351 | &rejectSourceStates); |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 1352 | } |
| 1353 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1354 | CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client): |
| 1355 | mClient(client) { |
| 1356 | } |
| 1357 | |
| 1358 | void CameraService::Client::OpsCallback::opChanged(int32_t op, |
| 1359 | const String16& packageName) { |
| 1360 | sp<BasicClient> client = mClient.promote(); |
| 1361 | if (client != NULL) { |
| 1362 | client->opChanged(op, packageName); |
| 1363 | } |
| 1364 | } |
| 1365 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1366 | // ---------------------------------------------------------------------------- |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1367 | // IProCamera |
| 1368 | // ---------------------------------------------------------------------------- |
| 1369 | |
| 1370 | CameraService::ProClient::ProClient(const sp<CameraService>& cameraService, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1371 | const sp<IProCameraCallbacks>& remoteCallback, |
| 1372 | const String16& clientPackageName, |
| 1373 | int cameraId, |
| 1374 | int cameraFacing, |
| 1375 | int clientPid, |
| 1376 | uid_t clientUid, |
| 1377 | int servicePid) |
| 1378 | : CameraService::BasicClient(cameraService, remoteCallback->asBinder(), |
| 1379 | clientPackageName, cameraId, cameraFacing, |
| 1380 | clientPid, clientUid, servicePid) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1381 | { |
| 1382 | mRemoteCallback = remoteCallback; |
| 1383 | } |
| 1384 | |
| 1385 | CameraService::ProClient::~ProClient() { |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1386 | } |
| 1387 | |
Jianing Wei | cb0652e | 2014-03-12 18:29:36 -0700 | [diff] [blame] | 1388 | void CameraService::ProClient::notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode, |
| 1389 | const CaptureResultExtras& resultExtras) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1390 | mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1391 | } |
| 1392 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1393 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1394 | |
| 1395 | static const int kDumpLockRetries = 50; |
| 1396 | static const int kDumpLockSleep = 60000; |
| 1397 | |
| 1398 | static bool tryLock(Mutex& mutex) |
| 1399 | { |
| 1400 | bool locked = false; |
| 1401 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 1402 | if (mutex.tryLock() == NO_ERROR) { |
| 1403 | locked = true; |
| 1404 | break; |
| 1405 | } |
| 1406 | usleep(kDumpLockSleep); |
| 1407 | } |
| 1408 | return locked; |
| 1409 | } |
| 1410 | |
| 1411 | status_t CameraService::dump(int fd, const Vector<String16>& args) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1412 | String8 result; |
| 1413 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
Eino-Ville Talvala | 611f619 | 2012-05-31 12:28:23 -0700 | [diff] [blame] | 1414 | result.appendFormat("Permission Denial: " |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1415 | "can't dump CameraService from pid=%d, uid=%d\n", |
| 1416 | getCallingPid(), |
| 1417 | getCallingUid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1418 | write(fd, result.string(), result.size()); |
| 1419 | } else { |
| 1420 | bool locked = tryLock(mServiceLock); |
| 1421 | // failed to lock - CameraService is probably deadlocked |
| 1422 | if (!locked) { |
Eino-Ville Talvala | 611f619 | 2012-05-31 12:28:23 -0700 | [diff] [blame] | 1423 | result.append("CameraService may be deadlocked\n"); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1424 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | bool hasClient = false; |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1428 | if (!mModule) { |
| 1429 | result = String8::format("No camera module available!\n"); |
| 1430 | write(fd, result.string(), result.size()); |
Kalle Lampila | 6ec3a15 | 2013-04-30 15:27:19 +0300 | [diff] [blame] | 1431 | if (locked) mServiceLock.unlock(); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1432 | return NO_ERROR; |
| 1433 | } |
| 1434 | |
| 1435 | result = String8::format("Camera module HAL API version: 0x%x\n", |
| 1436 | mModule->common.hal_api_version); |
| 1437 | result.appendFormat("Camera module API version: 0x%x\n", |
| 1438 | mModule->common.module_api_version); |
| 1439 | result.appendFormat("Camera module name: %s\n", |
| 1440 | mModule->common.name); |
| 1441 | result.appendFormat("Camera module author: %s\n", |
| 1442 | mModule->common.author); |
| 1443 | result.appendFormat("Number of camera devices: %d\n\n", mNumberOfCameras); |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 1444 | |
| 1445 | sp<VendorTagDescriptor> desc = VendorTagDescriptor::getGlobalVendorTagDescriptor(); |
| 1446 | if (desc == NULL) { |
| 1447 | result.appendFormat("Vendor tags left unimplemented.\n"); |
| 1448 | } else { |
| 1449 | result.appendFormat("Vendor tag definitions:\n"); |
| 1450 | } |
| 1451 | |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1452 | write(fd, result.string(), result.size()); |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 1453 | |
| 1454 | if (desc != NULL) { |
| 1455 | desc->dump(fd, /*verbosity*/2, /*indentation*/4); |
| 1456 | } |
| 1457 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1458 | for (int i = 0; i < mNumberOfCameras; i++) { |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1459 | result = String8::format("Camera %d static information:\n", i); |
| 1460 | camera_info info; |
| 1461 | |
| 1462 | status_t rc = mModule->get_camera_info(i, &info); |
| 1463 | if (rc != OK) { |
| 1464 | result.appendFormat(" Error reading static information!\n"); |
| 1465 | write(fd, result.string(), result.size()); |
| 1466 | } else { |
| 1467 | result.appendFormat(" Facing: %s\n", |
| 1468 | info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT"); |
| 1469 | result.appendFormat(" Orientation: %d\n", info.orientation); |
| 1470 | int deviceVersion; |
| 1471 | if (mModule->common.module_api_version < |
| 1472 | CAMERA_MODULE_API_VERSION_2_0) { |
| 1473 | deviceVersion = CAMERA_DEVICE_API_VERSION_1_0; |
| 1474 | } else { |
| 1475 | deviceVersion = info.device_version; |
| 1476 | } |
| 1477 | result.appendFormat(" Device version: 0x%x\n", deviceVersion); |
| 1478 | if (deviceVersion >= CAMERA_DEVICE_API_VERSION_2_0) { |
| 1479 | result.appendFormat(" Device static metadata:\n"); |
| 1480 | write(fd, result.string(), result.size()); |
Eino-Ville Talvala | 428b77a | 2012-07-30 09:55:30 -0700 | [diff] [blame] | 1481 | dump_indented_camera_metadata(info.static_camera_characteristics, |
Ruben Brunk | f81648e | 2014-04-17 16:14:57 -0700 | [diff] [blame] | 1482 | fd, /*verbosity*/2, /*indentation*/4); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1483 | } else { |
| 1484 | write(fd, result.string(), result.size()); |
| 1485 | } |
| 1486 | } |
| 1487 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1488 | sp<BasicClient> client = mClient[i].promote(); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1489 | if (client == 0) { |
| 1490 | result = String8::format(" Device is closed, no client instance\n"); |
| 1491 | write(fd, result.string(), result.size()); |
| 1492 | continue; |
| 1493 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1494 | hasClient = true; |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1495 | result = String8::format(" Device is open. Client instance dump:\n"); |
| 1496 | write(fd, result.string(), result.size()); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 1497 | client->dump(fd, args); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1498 | } |
| 1499 | if (!hasClient) { |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1500 | result = String8::format("\nNo active camera clients yet.\n"); |
| 1501 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | if (locked) mServiceLock.unlock(); |
| 1505 | |
Igor Murashkin | ff3e31d | 2013-10-23 16:40:06 -0700 | [diff] [blame] | 1506 | // Dump camera traces if there were any |
| 1507 | write(fd, "\n", 1); |
| 1508 | camera3::CameraTraces::dump(fd, args); |
| 1509 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1510 | // change logging level |
| 1511 | int n = args.size(); |
| 1512 | for (int i = 0; i + 1 < n; i++) { |
Eino-Ville Talvala | 611f619 | 2012-05-31 12:28:23 -0700 | [diff] [blame] | 1513 | String16 verboseOption("-v"); |
| 1514 | if (args[i] == verboseOption) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1515 | String8 levelStr(args[i+1]); |
| 1516 | int level = atoi(levelStr.string()); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1517 | result = String8::format("\nSetting log level to %d.\n", level); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1518 | setLogLevel(level); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1519 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1520 | } |
| 1521 | } |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1522 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1523 | } |
| 1524 | return NO_ERROR; |
| 1525 | } |
| 1526 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1527 | /*virtual*/void CameraService::binderDied( |
| 1528 | const wp<IBinder> &who) { |
| 1529 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 1530 | /** |
| 1531 | * While tempting to promote the wp<IBinder> into a sp, |
| 1532 | * it's actually not supported by the binder driver |
| 1533 | */ |
| 1534 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1535 | ALOGV("java clients' binder died"); |
| 1536 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1537 | sp<BasicClient> cameraClient = getClientByRemote(who); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1538 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 1539 | if (cameraClient == 0) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1540 | ALOGV("java clients' binder death already cleaned up (normal case)"); |
| 1541 | return; |
| 1542 | } |
| 1543 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1544 | ALOGW("Disconnecting camera client %p since the binder for it " |
| 1545 | "died (this pid %d)", cameraClient.get(), getCallingPid()); |
| 1546 | |
| 1547 | cameraClient->disconnect(); |
| 1548 | |
| 1549 | } |
| 1550 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1551 | void CameraService::updateStatus(ICameraServiceListener::Status status, |
Igor Murashkin | 93747b9 | 2013-05-01 15:42:20 -0700 | [diff] [blame] | 1552 | int32_t cameraId, |
| 1553 | const StatusVector *rejectSourceStates) { |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1554 | // do not lock mServiceLock here or can get into a deadlock from |
| 1555 | // connect() -> ProClient::disconnect -> updateStatus |
| 1556 | Mutex::Autolock lock(mStatusMutex); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1557 | |
| 1558 | ICameraServiceListener::Status oldStatus = mStatusList[cameraId]; |
| 1559 | |
| 1560 | mStatusList[cameraId] = status; |
| 1561 | |
| 1562 | if (oldStatus != status) { |
| 1563 | ALOGV("%s: Status has changed for camera ID %d from 0x%x to 0x%x", |
| 1564 | __FUNCTION__, cameraId, (uint32_t)oldStatus, (uint32_t)status); |
| 1565 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1566 | if (oldStatus == ICameraServiceListener::STATUS_NOT_PRESENT && |
| 1567 | (status != ICameraServiceListener::STATUS_PRESENT && |
| 1568 | status != ICameraServiceListener::STATUS_ENUMERATING)) { |
| 1569 | |
| 1570 | ALOGW("%s: From NOT_PRESENT can only transition into PRESENT" |
| 1571 | " or ENUMERATING", __FUNCTION__); |
| 1572 | mStatusList[cameraId] = oldStatus; |
| 1573 | return; |
| 1574 | } |
| 1575 | |
Igor Murashkin | 93747b9 | 2013-05-01 15:42:20 -0700 | [diff] [blame] | 1576 | if (rejectSourceStates != NULL) { |
| 1577 | const StatusVector &rejectList = *rejectSourceStates; |
| 1578 | StatusVector::const_iterator it = rejectList.begin(); |
| 1579 | |
| 1580 | /** |
| 1581 | * Sometimes we want to conditionally do a transition. |
| 1582 | * For example if a client disconnects, we want to go to PRESENT |
| 1583 | * only if we weren't already in NOT_PRESENT or ENUMERATING. |
| 1584 | */ |
| 1585 | for (; it != rejectList.end(); ++it) { |
| 1586 | if (oldStatus == *it) { |
| 1587 | ALOGV("%s: Rejecting status transition for Camera ID %d, " |
| 1588 | " since the source state was was in one of the bad " |
| 1589 | " states.", __FUNCTION__, cameraId); |
| 1590 | mStatusList[cameraId] = oldStatus; |
| 1591 | return; |
| 1592 | } |
| 1593 | } |
| 1594 | } |
| 1595 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1596 | /** |
| 1597 | * ProClients lose their exclusive lock. |
| 1598 | * - Done before the CameraClient can initialize the HAL device, |
| 1599 | * since we want to be able to close it before they get to initialize |
| 1600 | */ |
| 1601 | if (status == ICameraServiceListener::STATUS_NOT_AVAILABLE) { |
| 1602 | Vector<wp<ProClient> > proClients(mProClientList[cameraId]); |
| 1603 | Vector<wp<ProClient> >::const_iterator it; |
| 1604 | |
| 1605 | for (it = proClients.begin(); it != proClients.end(); ++it) { |
| 1606 | sp<ProClient> proCl = it->promote(); |
| 1607 | if (proCl.get() != NULL) { |
| 1608 | proCl->onExclusiveLockStolen(); |
| 1609 | } |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | Vector<sp<ICameraServiceListener> >::const_iterator it; |
| 1614 | for (it = mListenerList.begin(); it != mListenerList.end(); ++it) { |
| 1615 | (*it)->onStatusChanged(status, cameraId); |
| 1616 | } |
| 1617 | } |
| 1618 | } |
| 1619 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1620 | ICameraServiceListener::Status CameraService::getStatus(int cameraId) const { |
| 1621 | if (cameraId < 0 || cameraId >= MAX_CAMERAS) { |
| 1622 | ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId); |
| 1623 | return ICameraServiceListener::STATUS_UNKNOWN; |
| 1624 | } |
| 1625 | |
| 1626 | Mutex::Autolock al(mStatusMutex); |
| 1627 | return mStatusList[cameraId]; |
| 1628 | } |
| 1629 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1630 | }; // namespace android |