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