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