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 | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 41 | #include "CameraClient.h" |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 42 | #include "Camera2Client.h" |
Igor Murashkin | 985fd30 | 2013-02-20 18:24:43 -0800 | [diff] [blame] | 43 | #include "ProCamera2Client.h" |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 44 | #include "photography/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) { |
| 214 | return NO_INIT; |
| 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 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 265 | bool CameraService::validateConnect(int cameraId, |
| 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); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 278 | return false; |
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"); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 284 | return false; |
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); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 290 | return false; |
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); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 298 | return false; |
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); |
| 305 | return false; |
| 306 | } else if (currentStatus == ICameraServiceListener::STATUS_ENUMERATING) { |
| 307 | ALOGI("Camera is enumerating," |
| 308 | " connect X (pid %d) rejected", callingPid); |
| 309 | return false; |
| 310 | } |
| 311 | // Else don't check for STATUS_NOT_AVAILABLE. |
| 312 | // -- It's done implicitly in canConnectUnsafe /w the mBusy array |
| 313 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 314 | return true; |
| 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 | |
| 361 | sp<ICamera> CameraService::connect( |
| 362 | const sp<ICameraClient>& cameraClient, |
| 363 | int cameraId, |
| 364 | const String16& clientPackageName, |
| 365 | int clientUid) { |
| 366 | |
| 367 | String8 clientName8(clientPackageName); |
| 368 | int callingPid = getCallingPid(); |
| 369 | |
| 370 | LOG1("CameraService::connect E (pid %d \"%s\", id %d)", callingPid, |
| 371 | clientName8.string(), cameraId); |
| 372 | |
| 373 | if (!validateConnect(cameraId, /*inout*/clientUid)) { |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 374 | return NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 377 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 378 | sp<Client> client; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 379 | { |
| 380 | Mutex::Autolock lock(mServiceLock); |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 381 | sp<BasicClient> clientTmp; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 382 | if (!canConnectUnsafe(cameraId, clientPackageName, |
| 383 | cameraClient->asBinder(), |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 384 | /*out*/clientTmp)) { |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 385 | return NULL; |
| 386 | } else if (client.get() != NULL) { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 387 | return static_cast<Client*>(clientTmp.get()); |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | int facing = -1; |
| 391 | int deviceVersion = getDeviceVersion(cameraId, &facing); |
| 392 | |
| 393 | // If there are other non-exclusive users of the camera, |
| 394 | // this will tear them down before we can reuse the camera |
| 395 | if (isValidCameraId(cameraId)) { |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 396 | // transition from PRESENT -> NOT_AVAILABLE |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 397 | updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE, |
| 398 | cameraId); |
| 399 | } |
| 400 | |
| 401 | switch(deviceVersion) { |
| 402 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 403 | client = new CameraClient(this, cameraClient, |
| 404 | clientPackageName, cameraId, |
| 405 | facing, callingPid, clientUid, getpid()); |
| 406 | break; |
| 407 | case CAMERA_DEVICE_API_VERSION_2_0: |
| 408 | case CAMERA_DEVICE_API_VERSION_2_1: |
| 409 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 410 | client = new Camera2Client(this, cameraClient, |
| 411 | clientPackageName, cameraId, |
| 412 | facing, callingPid, clientUid, getpid(), |
| 413 | deviceVersion); |
| 414 | break; |
| 415 | case -1: |
| 416 | ALOGE("Invalid camera id %d", cameraId); |
| 417 | return NULL; |
| 418 | default: |
| 419 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
| 420 | return NULL; |
| 421 | } |
| 422 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 423 | if (!connectFinishUnsafe(client, |
| 424 | client->getRemote())) { |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 425 | // this is probably not recoverable.. maybe the client can try again |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 426 | // OK: we can only get here if we were originally in PRESENT state |
| 427 | updateStatus(ICameraServiceListener::STATUS_PRESENT, cameraId); |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 428 | |
| 429 | return NULL; |
| 430 | } |
| 431 | |
| 432 | mClient[cameraId] = client; |
| 433 | LOG1("CameraService::connect X (id %d, this pid is %d)", cameraId, |
| 434 | getpid()); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 435 | } |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 436 | // important: release the mutex here so the client can call back |
| 437 | // 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] | 438 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 439 | return client; |
| 440 | } |
| 441 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 442 | bool CameraService::connectFinishUnsafe(const sp<BasicClient>& client, |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 443 | const sp<IBinder>& remoteCallback) { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 444 | if (client->initialize(mModule) != OK) { |
| 445 | return false; |
| 446 | } |
| 447 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 448 | remoteCallback->linkToDeath(this); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 449 | |
| 450 | return true; |
| 451 | } |
| 452 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 453 | sp<IProCameraUser> CameraService::connect( |
| 454 | const sp<IProCameraCallbacks>& cameraCb, |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 455 | int cameraId, |
| 456 | const String16& clientPackageName, |
| 457 | int clientUid) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 458 | { |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 459 | String8 clientName8(clientPackageName); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 460 | int callingPid = getCallingPid(); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 461 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 462 | LOG1("CameraService::connectPro E (pid %d \"%s\", id %d)", callingPid, |
| 463 | clientName8.string(), cameraId); |
Igor Murashkin | c073ba5 | 2013-02-26 14:32:34 -0800 | [diff] [blame] | 464 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 465 | if (!validateConnect(cameraId, /*inout*/clientUid)) { |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 466 | return NULL; |
| 467 | } |
| 468 | |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 469 | sp<ProClient> client; |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 470 | { |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 471 | Mutex::Autolock lock(mServiceLock); |
| 472 | { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 473 | sp<BasicClient> client; |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 474 | if (!canConnectUnsafe(cameraId, clientPackageName, |
| 475 | cameraCb->asBinder(), |
| 476 | /*out*/client)) { |
| 477 | return NULL; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | int facing = -1; |
| 482 | int deviceVersion = getDeviceVersion(cameraId, &facing); |
| 483 | |
| 484 | switch(deviceVersion) { |
| 485 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 486 | ALOGE("Camera id %d uses HALv1, doesn't support ProCamera", |
| 487 | cameraId); |
| 488 | return NULL; |
| 489 | break; |
| 490 | case CAMERA_DEVICE_API_VERSION_2_0: |
| 491 | case CAMERA_DEVICE_API_VERSION_2_1: |
Zhijun He | 4711005 | 2013-07-22 17:34:34 -0700 | [diff] [blame^] | 492 | case CAMERA_DEVICE_API_VERSION_3_0: |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 493 | client = new ProCamera2Client(this, cameraCb, String16(), |
| 494 | cameraId, facing, callingPid, USE_CALLING_UID, getpid()); |
| 495 | break; |
| 496 | case -1: |
| 497 | ALOGE("Invalid camera id %d", cameraId); |
| 498 | return NULL; |
| 499 | default: |
| 500 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 501 | return NULL; |
| 502 | } |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 503 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 504 | if (!connectFinishUnsafe(client, client->getRemote())) { |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 505 | return NULL; |
| 506 | } |
| 507 | |
| 508 | mProClientList[cameraId].push(client); |
| 509 | |
| 510 | LOG1("CameraService::connectPro X (id %d, this pid is %d)", cameraId, |
| 511 | getpid()); |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 512 | } |
Igor Murashkin | acd695c | 2013-03-13 17:23:00 -0700 | [diff] [blame] | 513 | // important: release the mutex here so the client can call back |
| 514 | // into the service from its destructor (can be at the end of the call) |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 515 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 516 | return client; |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 517 | } |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 518 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 519 | sp<ICameraDeviceUser> CameraService::connect( |
| 520 | const sp<ICameraDeviceCallbacks>& cameraCb, |
| 521 | int cameraId, |
| 522 | const String16& clientPackageName, |
| 523 | int clientUid) |
| 524 | { |
| 525 | // TODO: this function needs to return status_t |
| 526 | // so that we have an error code when things go wrong and the client is NULL |
| 527 | |
| 528 | String8 clientName8(clientPackageName); |
| 529 | int callingPid = getCallingPid(); |
| 530 | |
| 531 | LOG1("CameraService::connectDevice E (pid %d \"%s\", id %d)", callingPid, |
| 532 | clientName8.string(), cameraId); |
| 533 | |
| 534 | if (!validateConnect(cameraId, /*inout*/clientUid)) { |
| 535 | return NULL; |
| 536 | } |
| 537 | |
| 538 | sp<CameraDeviceClient> client; |
| 539 | { |
| 540 | Mutex::Autolock lock(mServiceLock); |
| 541 | { |
| 542 | sp<BasicClient> client; |
| 543 | if (!canConnectUnsafe(cameraId, clientPackageName, |
| 544 | cameraCb->asBinder(), |
| 545 | /*out*/client)) { |
| 546 | return NULL; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | int facing = -1; |
| 551 | int deviceVersion = getDeviceVersion(cameraId, &facing); |
| 552 | |
| 553 | // If there are other non-exclusive users of the camera, |
| 554 | // this will tear them down before we can reuse the camera |
| 555 | if (isValidCameraId(cameraId)) { |
| 556 | // transition from PRESENT -> NOT_AVAILABLE |
| 557 | updateStatus(ICameraServiceListener::STATUS_NOT_AVAILABLE, |
| 558 | cameraId); |
| 559 | } |
| 560 | |
| 561 | switch(deviceVersion) { |
| 562 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 563 | ALOGE("Camera id %d uses old HAL, doesn't support CameraDevice", |
| 564 | cameraId); |
| 565 | return NULL; |
| 566 | break; |
| 567 | // TODO: don't allow 2.0 Only allow 2.1 and higher |
| 568 | case CAMERA_DEVICE_API_VERSION_2_0: |
| 569 | case CAMERA_DEVICE_API_VERSION_2_1: |
| 570 | case CAMERA_DEVICE_API_VERSION_3_0: |
| 571 | client = new CameraDeviceClient(this, cameraCb, String16(), |
| 572 | cameraId, facing, callingPid, USE_CALLING_UID, getpid()); |
| 573 | break; |
| 574 | case -1: |
| 575 | ALOGE("Invalid camera id %d", cameraId); |
| 576 | return NULL; |
| 577 | default: |
| 578 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
| 579 | return NULL; |
| 580 | } |
| 581 | |
| 582 | if (!connectFinishUnsafe(client, client->getRemote())) { |
| 583 | // this is probably not recoverable.. maybe the client can try again |
| 584 | // OK: we can only get here if we were originally in PRESENT state |
| 585 | updateStatus(ICameraServiceListener::STATUS_PRESENT, cameraId); |
| 586 | return NULL; |
| 587 | } |
| 588 | |
| 589 | LOG1("CameraService::connectDevice X (id %d, this pid is %d)", cameraId, |
| 590 | getpid()); |
| 591 | |
| 592 | mClient[cameraId] = client; |
| 593 | } |
| 594 | // important: release the mutex here so the client can call back |
| 595 | // into the service from its destructor (can be at the end of the call) |
| 596 | |
| 597 | return client; |
| 598 | } |
| 599 | |
| 600 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 601 | status_t CameraService::addListener( |
| 602 | const sp<ICameraServiceListener>& listener) { |
| 603 | ALOGV("%s: Add listener %p", __FUNCTION__, listener.get()); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 604 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 605 | Mutex::Autolock lock(mServiceLock); |
| 606 | |
| 607 | Vector<sp<ICameraServiceListener> >::iterator it, end; |
| 608 | for (it = mListenerList.begin(); it != mListenerList.end(); ++it) { |
| 609 | if ((*it)->asBinder() == listener->asBinder()) { |
| 610 | ALOGW("%s: Tried to add listener %p which was already subscribed", |
| 611 | __FUNCTION__, listener.get()); |
| 612 | return ALREADY_EXISTS; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | mListenerList.push_back(listener); |
| 617 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 618 | /* Immediately signal current status to this listener only */ |
| 619 | { |
| 620 | Mutex::Autolock m(mStatusMutex) ; |
| 621 | int numCams = getNumberOfCameras(); |
| 622 | for (int i = 0; i < numCams; ++i) { |
| 623 | listener->onStatusChanged(mStatusList[i], i); |
| 624 | } |
| 625 | } |
| 626 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 627 | return OK; |
| 628 | } |
| 629 | status_t CameraService::removeListener( |
| 630 | const sp<ICameraServiceListener>& listener) { |
| 631 | ALOGV("%s: Remove listener %p", __FUNCTION__, listener.get()); |
| 632 | |
| 633 | Mutex::Autolock lock(mServiceLock); |
| 634 | |
| 635 | Vector<sp<ICameraServiceListener> >::iterator it; |
| 636 | for (it = mListenerList.begin(); it != mListenerList.end(); ++it) { |
| 637 | if ((*it)->asBinder() == listener->asBinder()) { |
| 638 | mListenerList.erase(it); |
| 639 | return OK; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | ALOGW("%s: Tried to remove a listener %p which was not subscribed", |
| 644 | __FUNCTION__, listener.get()); |
| 645 | |
| 646 | return BAD_VALUE; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | void CameraService::removeClientByRemote(const wp<IBinder>& remoteBinder) { |
| 650 | int callingPid = getCallingPid(); |
| 651 | LOG1("CameraService::removeClientByRemote E (pid %d)", callingPid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 652 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 653 | // Declare this before the lock to make absolutely sure the |
| 654 | // destructor won't be called with the lock held. |
| 655 | Mutex::Autolock lock(mServiceLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 656 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 657 | int outIndex; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 658 | sp<BasicClient> client = findClientUnsafe(remoteBinder, outIndex); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 659 | |
| 660 | if (client != 0) { |
| 661 | // Found our camera, clear and leave. |
| 662 | LOG1("removeClient: clear camera %d", outIndex); |
| 663 | mClient[outIndex].clear(); |
| 664 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 665 | client->getRemote()->unlinkToDeath(this); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 666 | } else { |
| 667 | |
| 668 | sp<ProClient> clientPro = findProClientUnsafe(remoteBinder); |
| 669 | |
| 670 | if (clientPro != NULL) { |
| 671 | // Found our camera, clear and leave. |
| 672 | LOG1("removeClient: clear pro %p", clientPro.get()); |
| 673 | |
| 674 | clientPro->getRemoteCallback()->asBinder()->unlinkToDeath(this); |
| 675 | } |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 678 | LOG1("CameraService::removeClientByRemote X (pid %d)", callingPid); |
| 679 | } |
| 680 | |
| 681 | sp<CameraService::ProClient> CameraService::findProClientUnsafe( |
| 682 | const wp<IBinder>& cameraCallbacksRemote) |
| 683 | { |
| 684 | sp<ProClient> clientPro; |
| 685 | |
| 686 | for (int i = 0; i < mNumberOfCameras; ++i) { |
| 687 | Vector<size_t> removeIdx; |
| 688 | |
| 689 | for (size_t j = 0; j < mProClientList[i].size(); ++j) { |
| 690 | wp<ProClient> cl = mProClientList[i][j]; |
| 691 | |
| 692 | sp<ProClient> clStrong = cl.promote(); |
| 693 | if (clStrong != NULL && clStrong->getRemote() == cameraCallbacksRemote) { |
| 694 | clientPro = clStrong; |
| 695 | break; |
| 696 | } else if (clStrong == NULL) { |
| 697 | // mark to clean up dead ptr |
| 698 | removeIdx.push(j); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | // remove stale ptrs (in reverse so the indices dont change) |
| 703 | for (ssize_t j = (ssize_t)removeIdx.size() - 1; j >= 0; --j) { |
| 704 | mProClientList[i].removeAt(removeIdx[j]); |
| 705 | } |
| 706 | |
| 707 | } |
| 708 | |
| 709 | return clientPro; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 712 | sp<CameraService::BasicClient> CameraService::findClientUnsafe( |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 713 | const wp<IBinder>& cameraClient, int& outIndex) { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 714 | sp<BasicClient> client; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 715 | |
| 716 | for (int i = 0; i < mNumberOfCameras; i++) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 717 | |
| 718 | // This happens when we have already disconnected (or this is |
| 719 | // just another unused camera). |
| 720 | if (mClient[i] == 0) continue; |
| 721 | |
| 722 | // Promote mClient. It can fail if we are called from this path: |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 723 | // Client::~Client() -> disconnect() -> removeClientByRemote(). |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 724 | client = mClient[i].promote(); |
| 725 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 726 | // Clean up stale client entry |
| 727 | if (client == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 728 | mClient[i].clear(); |
| 729 | continue; |
| 730 | } |
| 731 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 732 | if (cameraClient == client->getRemote()) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 733 | // Found our camera |
| 734 | outIndex = i; |
| 735 | return client; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 739 | outIndex = -1; |
| 740 | return NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 741 | } |
| 742 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 743 | CameraService::BasicClient* CameraService::getClientByIdUnsafe(int cameraId) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 744 | if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL; |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 745 | return mClient[cameraId].unsafe_get(); |
| 746 | } |
| 747 | |
| 748 | Mutex* CameraService::getClientLockById(int cameraId) { |
| 749 | if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL; |
| 750 | return &mClientLock[cameraId]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 751 | } |
| 752 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 753 | sp<CameraService::BasicClient> CameraService::getClientByRemote( |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 754 | const wp<IBinder>& cameraClient) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 755 | |
| 756 | // Declare this before the lock to make absolutely sure the |
| 757 | // destructor won't be called with the lock held. |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 758 | sp<BasicClient> client; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 759 | |
| 760 | Mutex::Autolock lock(mServiceLock); |
| 761 | |
| 762 | int outIndex; |
| 763 | client = findClientUnsafe(cameraClient, outIndex); |
| 764 | |
| 765 | return client; |
| 766 | } |
| 767 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 768 | status_t CameraService::onTransact( |
| 769 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { |
| 770 | // Permission checks |
| 771 | switch (code) { |
| 772 | case BnCameraService::CONNECT: |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 773 | case BnCameraService::CONNECT_PRO: |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 774 | const int pid = getCallingPid(); |
| 775 | const int self_pid = getpid(); |
| 776 | if (pid != self_pid) { |
| 777 | // we're called from a different process, do the real check |
| 778 | if (!checkCallingPermission( |
| 779 | String16("android.permission.CAMERA"))) { |
| 780 | const int uid = getCallingUid(); |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 781 | ALOGE("Permission Denial: " |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 782 | "can't use the camera pid=%d, uid=%d", pid, uid); |
| 783 | return PERMISSION_DENIED; |
| 784 | } |
| 785 | } |
| 786 | break; |
| 787 | } |
| 788 | |
| 789 | return BnCameraService::onTransact(code, data, reply, flags); |
| 790 | } |
| 791 | |
| 792 | // The reason we need this busy bit is a new CameraService::connect() request |
| 793 | // may come in while the previous Client's destructor has not been run or is |
| 794 | // still running. If the last strong reference of the previous Client is gone |
| 795 | // but the destructor has not been finished, we should not allow the new Client |
| 796 | // to be created because we need to wait for the previous Client to tear down |
| 797 | // the hardware first. |
| 798 | void CameraService::setCameraBusy(int cameraId) { |
| 799 | android_atomic_write(1, &mBusy[cameraId]); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 800 | |
| 801 | ALOGV("setCameraBusy cameraId=%d", cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | void CameraService::setCameraFree(int cameraId) { |
| 805 | android_atomic_write(0, &mBusy[cameraId]); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 806 | |
| 807 | ALOGV("setCameraFree cameraId=%d", cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | // We share the media players for shutter and recording sound for all clients. |
| 811 | // A reference count is kept to determine when we will actually release the |
| 812 | // media players. |
| 813 | |
Chih-Chung Chang | ff4f55c | 2011-10-17 19:03:12 +0800 | [diff] [blame] | 814 | MediaPlayer* CameraService::newMediaPlayer(const char *file) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 815 | MediaPlayer* mp = new MediaPlayer(); |
| 816 | if (mp->setDataSource(file, NULL) == NO_ERROR) { |
Eino-Ville Talvala | 60a78ac | 2012-01-05 15:34:53 -0800 | [diff] [blame] | 817 | mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 818 | mp->prepare(); |
| 819 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 820 | ALOGE("Failed to load CameraService sounds: %s", file); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 821 | return NULL; |
| 822 | } |
| 823 | return mp; |
| 824 | } |
| 825 | |
| 826 | void CameraService::loadSound() { |
| 827 | Mutex::Autolock lock(mSoundLock); |
| 828 | LOG1("CameraService::loadSound ref=%d", mSoundRef); |
| 829 | if (mSoundRef++) return; |
| 830 | |
| 831 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); |
| 832 | mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); |
| 833 | } |
| 834 | |
| 835 | void CameraService::releaseSound() { |
| 836 | Mutex::Autolock lock(mSoundLock); |
| 837 | LOG1("CameraService::releaseSound ref=%d", mSoundRef); |
| 838 | if (--mSoundRef) return; |
| 839 | |
| 840 | for (int i = 0; i < NUM_SOUNDS; i++) { |
| 841 | if (mSoundPlayer[i] != 0) { |
| 842 | mSoundPlayer[i]->disconnect(); |
| 843 | mSoundPlayer[i].clear(); |
| 844 | } |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | void CameraService::playSound(sound_kind kind) { |
| 849 | LOG1("playSound(%d)", kind); |
| 850 | Mutex::Autolock lock(mSoundLock); |
| 851 | sp<MediaPlayer> player = mSoundPlayer[kind]; |
| 852 | if (player != 0) { |
Chih-Chung Chang | 8888a75 | 2011-10-20 10:47:26 +0800 | [diff] [blame] | 853 | player->seekTo(0); |
| 854 | player->start(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 855 | } |
| 856 | } |
| 857 | |
| 858 | // ---------------------------------------------------------------------------- |
| 859 | |
| 860 | CameraService::Client::Client(const sp<CameraService>& cameraService, |
Wu-cheng Li | b7a6794 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 861 | const sp<ICameraClient>& cameraClient, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 862 | const String16& clientPackageName, |
| 863 | int cameraId, int cameraFacing, |
| 864 | int clientPid, uid_t clientUid, |
| 865 | int servicePid) : |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 866 | CameraService::BasicClient(cameraService, cameraClient->asBinder(), |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 867 | clientPackageName, |
| 868 | cameraId, cameraFacing, |
| 869 | clientPid, clientUid, |
| 870 | servicePid) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 871 | { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 872 | int callingPid = getCallingPid(); |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 873 | LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 874 | |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 875 | mRemoteCallback = cameraClient; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 876 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 877 | cameraService->setCameraBusy(cameraId); |
| 878 | cameraService->loadSound(); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 879 | |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 880 | LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 881 | } |
| 882 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 883 | // tear down the client |
| 884 | CameraService::Client::~Client() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 885 | ALOGV("~Client"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 886 | mDestructionStarted = true; |
| 887 | |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 888 | mCameraService->releaseSound(); |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 889 | // unconditionally disconnect. function is idempotent |
| 890 | Client::disconnect(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 891 | } |
| 892 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 893 | CameraService::BasicClient::BasicClient(const sp<CameraService>& cameraService, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 894 | const sp<IBinder>& remoteCallback, |
| 895 | const String16& clientPackageName, |
| 896 | int cameraId, int cameraFacing, |
| 897 | int clientPid, uid_t clientUid, |
| 898 | int servicePid): |
| 899 | mClientPackageName(clientPackageName) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 900 | { |
| 901 | mCameraService = cameraService; |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 902 | mRemoteBinder = remoteCallback; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 903 | mCameraId = cameraId; |
| 904 | mCameraFacing = cameraFacing; |
| 905 | mClientPid = clientPid; |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 906 | mClientUid = clientUid; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 907 | mServicePid = servicePid; |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 908 | mOpsActive = false; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 909 | mDestructionStarted = false; |
| 910 | } |
| 911 | |
| 912 | CameraService::BasicClient::~BasicClient() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 913 | ALOGV("~BasicClient"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 914 | mDestructionStarted = true; |
| 915 | } |
| 916 | |
| 917 | void CameraService::BasicClient::disconnect() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 918 | ALOGV("BasicClient::disconnect"); |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 919 | mCameraService->removeClientByRemote(mRemoteBinder); |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 920 | // client shouldn't be able to call into us anymore |
| 921 | mClientPid = 0; |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 922 | } |
| 923 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 924 | status_t CameraService::BasicClient::startCameraOps() { |
| 925 | int32_t res; |
| 926 | |
| 927 | mOpsCallback = new OpsCallback(this); |
| 928 | |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 929 | { |
| 930 | ALOGV("%s: Start camera ops, package name = %s, client UID = %d", |
| 931 | __FUNCTION__, String8(mClientPackageName).string(), mClientUid); |
| 932 | } |
| 933 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 934 | mAppOpsManager.startWatchingMode(AppOpsManager::OP_CAMERA, |
| 935 | mClientPackageName, mOpsCallback); |
| 936 | res = mAppOpsManager.startOp(AppOpsManager::OP_CAMERA, |
| 937 | mClientUid, mClientPackageName); |
| 938 | |
| 939 | if (res != AppOpsManager::MODE_ALLOWED) { |
| 940 | ALOGI("Camera %d: Access for \"%s\" has been revoked", |
| 941 | mCameraId, String8(mClientPackageName).string()); |
| 942 | return PERMISSION_DENIED; |
| 943 | } |
| 944 | mOpsActive = true; |
| 945 | return OK; |
| 946 | } |
| 947 | |
| 948 | status_t CameraService::BasicClient::finishCameraOps() { |
| 949 | if (mOpsActive) { |
| 950 | mAppOpsManager.finishOp(AppOpsManager::OP_CAMERA, mClientUid, |
| 951 | mClientPackageName); |
| 952 | mOpsActive = false; |
| 953 | } |
| 954 | mAppOpsManager.stopWatchingMode(mOpsCallback); |
| 955 | mOpsCallback.clear(); |
| 956 | |
| 957 | return OK; |
| 958 | } |
| 959 | |
| 960 | void CameraService::BasicClient::opChanged(int32_t op, const String16& packageName) { |
| 961 | String8 name(packageName); |
| 962 | String8 myName(mClientPackageName); |
| 963 | |
| 964 | if (op != AppOpsManager::OP_CAMERA) { |
| 965 | ALOGW("Unexpected app ops notification received: %d", op); |
| 966 | return; |
| 967 | } |
| 968 | |
| 969 | int32_t res; |
| 970 | res = mAppOpsManager.checkOp(AppOpsManager::OP_CAMERA, |
| 971 | mClientUid, mClientPackageName); |
| 972 | ALOGV("checkOp returns: %d, %s ", res, |
| 973 | res == AppOpsManager::MODE_ALLOWED ? "ALLOWED" : |
| 974 | res == AppOpsManager::MODE_IGNORED ? "IGNORED" : |
| 975 | res == AppOpsManager::MODE_ERRORED ? "ERRORED" : |
| 976 | "UNKNOWN"); |
| 977 | |
| 978 | if (res != AppOpsManager::MODE_ALLOWED) { |
| 979 | ALOGI("Camera %d: Access for \"%s\" revoked", mCameraId, |
| 980 | myName.string()); |
| 981 | // Reset the client PID to allow server-initiated disconnect, |
| 982 | // and to prevent further calls by client. |
| 983 | mClientPid = getCallingPid(); |
| 984 | notifyError(); |
| 985 | disconnect(); |
| 986 | } |
| 987 | } |
| 988 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 989 | // ---------------------------------------------------------------------------- |
| 990 | |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 991 | Mutex* CameraService::Client::getClientLockFromCookie(void* user) { |
| 992 | return gCameraService->getClientLockById((int) user); |
| 993 | } |
| 994 | |
| 995 | // Provide client pointer for callbacks. Client lock returned from getClientLockFromCookie should |
| 996 | // be acquired for this to be safe |
| 997 | CameraService::Client* CameraService::Client::getClientFromCookie(void* user) { |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 998 | BasicClient *basicClient = gCameraService->getClientByIdUnsafe((int) user); |
| 999 | // OK: only CameraClient calls this, and they already cast anyway. |
| 1000 | Client* client = static_cast<Client*>(basicClient); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1001 | |
| 1002 | // This could happen if the Client is in the process of shutting down (the |
| 1003 | // last strong reference is gone, but the destructor hasn't finished |
| 1004 | // stopping the hardware). |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 1005 | if (client == NULL) return NULL; |
| 1006 | |
| 1007 | // destruction already started, so should not be accessed |
| 1008 | if (client->mDestructionStarted) return NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1009 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1010 | return client; |
| 1011 | } |
| 1012 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1013 | void CameraService::Client::notifyError() { |
Igor Murashkin | 44cfcf0 | 2013-03-01 16:22:28 -0800 | [diff] [blame] | 1014 | mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1015 | } |
| 1016 | |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 1017 | // NOTE: function is idempotent |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 1018 | void CameraService::Client::disconnect() { |
Eino-Ville Talvala | d09801b | 2013-04-23 15:16:57 -0700 | [diff] [blame] | 1019 | ALOGV("Client::disconnect"); |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1020 | BasicClient::disconnect(); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 1021 | mCameraService->setCameraFree(mCameraId); |
Igor Murashkin | 93747b9 | 2013-05-01 15:42:20 -0700 | [diff] [blame] | 1022 | |
| 1023 | StatusVector rejectSourceStates; |
| 1024 | rejectSourceStates.push_back(ICameraServiceListener::STATUS_NOT_PRESENT); |
| 1025 | rejectSourceStates.push_back(ICameraServiceListener::STATUS_ENUMERATING); |
| 1026 | |
| 1027 | // 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] | 1028 | mCameraService->updateStatus(ICameraServiceListener::STATUS_PRESENT, |
Igor Murashkin | 93747b9 | 2013-05-01 15:42:20 -0700 | [diff] [blame] | 1029 | mCameraId, |
| 1030 | &rejectSourceStates); |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 1031 | } |
| 1032 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1033 | CameraService::Client::OpsCallback::OpsCallback(wp<BasicClient> client): |
| 1034 | mClient(client) { |
| 1035 | } |
| 1036 | |
| 1037 | void CameraService::Client::OpsCallback::opChanged(int32_t op, |
| 1038 | const String16& packageName) { |
| 1039 | sp<BasicClient> client = mClient.promote(); |
| 1040 | if (client != NULL) { |
| 1041 | client->opChanged(op, packageName); |
| 1042 | } |
| 1043 | } |
| 1044 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1045 | // ---------------------------------------------------------------------------- |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1046 | // IProCamera |
| 1047 | // ---------------------------------------------------------------------------- |
| 1048 | |
| 1049 | CameraService::ProClient::ProClient(const sp<CameraService>& cameraService, |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1050 | const sp<IProCameraCallbacks>& remoteCallback, |
| 1051 | const String16& clientPackageName, |
| 1052 | int cameraId, |
| 1053 | int cameraFacing, |
| 1054 | int clientPid, |
| 1055 | uid_t clientUid, |
| 1056 | int servicePid) |
| 1057 | : CameraService::BasicClient(cameraService, remoteCallback->asBinder(), |
| 1058 | clientPackageName, cameraId, cameraFacing, |
| 1059 | clientPid, clientUid, servicePid) |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1060 | { |
| 1061 | mRemoteCallback = remoteCallback; |
| 1062 | } |
| 1063 | |
| 1064 | CameraService::ProClient::~ProClient() { |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1065 | } |
| 1066 | |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1067 | void CameraService::ProClient::notifyError() { |
Igor Murashkin | e6800ce | 2013-03-04 17:25:57 -0800 | [diff] [blame] | 1068 | mRemoteCallback->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0); |
Eino-Ville Talvala | ceb388d | 2013-02-19 10:40:14 -0800 | [diff] [blame] | 1069 | } |
| 1070 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1071 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1072 | |
| 1073 | static const int kDumpLockRetries = 50; |
| 1074 | static const int kDumpLockSleep = 60000; |
| 1075 | |
| 1076 | static bool tryLock(Mutex& mutex) |
| 1077 | { |
| 1078 | bool locked = false; |
| 1079 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 1080 | if (mutex.tryLock() == NO_ERROR) { |
| 1081 | locked = true; |
| 1082 | break; |
| 1083 | } |
| 1084 | usleep(kDumpLockSleep); |
| 1085 | } |
| 1086 | return locked; |
| 1087 | } |
| 1088 | |
| 1089 | status_t CameraService::dump(int fd, const Vector<String16>& args) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1090 | String8 result; |
| 1091 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
Eino-Ville Talvala | 611f619 | 2012-05-31 12:28:23 -0700 | [diff] [blame] | 1092 | result.appendFormat("Permission Denial: " |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1093 | "can't dump CameraService from pid=%d, uid=%d\n", |
| 1094 | getCallingPid(), |
| 1095 | getCallingUid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1096 | write(fd, result.string(), result.size()); |
| 1097 | } else { |
| 1098 | bool locked = tryLock(mServiceLock); |
| 1099 | // failed to lock - CameraService is probably deadlocked |
| 1100 | if (!locked) { |
Eino-Ville Talvala | 611f619 | 2012-05-31 12:28:23 -0700 | [diff] [blame] | 1101 | result.append("CameraService may be deadlocked\n"); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1102 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | bool hasClient = false; |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1106 | if (!mModule) { |
| 1107 | result = String8::format("No camera module available!\n"); |
| 1108 | write(fd, result.string(), result.size()); |
| 1109 | return NO_ERROR; |
| 1110 | } |
| 1111 | |
| 1112 | result = String8::format("Camera module HAL API version: 0x%x\n", |
| 1113 | mModule->common.hal_api_version); |
| 1114 | result.appendFormat("Camera module API version: 0x%x\n", |
| 1115 | mModule->common.module_api_version); |
| 1116 | result.appendFormat("Camera module name: %s\n", |
| 1117 | mModule->common.name); |
| 1118 | result.appendFormat("Camera module author: %s\n", |
| 1119 | mModule->common.author); |
| 1120 | result.appendFormat("Number of camera devices: %d\n\n", mNumberOfCameras); |
| 1121 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1122 | for (int i = 0; i < mNumberOfCameras; i++) { |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1123 | result = String8::format("Camera %d static information:\n", i); |
| 1124 | camera_info info; |
| 1125 | |
| 1126 | status_t rc = mModule->get_camera_info(i, &info); |
| 1127 | if (rc != OK) { |
| 1128 | result.appendFormat(" Error reading static information!\n"); |
| 1129 | write(fd, result.string(), result.size()); |
| 1130 | } else { |
| 1131 | result.appendFormat(" Facing: %s\n", |
| 1132 | info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT"); |
| 1133 | result.appendFormat(" Orientation: %d\n", info.orientation); |
| 1134 | int deviceVersion; |
| 1135 | if (mModule->common.module_api_version < |
| 1136 | CAMERA_MODULE_API_VERSION_2_0) { |
| 1137 | deviceVersion = CAMERA_DEVICE_API_VERSION_1_0; |
| 1138 | } else { |
| 1139 | deviceVersion = info.device_version; |
| 1140 | } |
| 1141 | result.appendFormat(" Device version: 0x%x\n", deviceVersion); |
| 1142 | if (deviceVersion >= CAMERA_DEVICE_API_VERSION_2_0) { |
| 1143 | result.appendFormat(" Device static metadata:\n"); |
| 1144 | write(fd, result.string(), result.size()); |
Eino-Ville Talvala | 428b77a | 2012-07-30 09:55:30 -0700 | [diff] [blame] | 1145 | dump_indented_camera_metadata(info.static_camera_characteristics, |
| 1146 | fd, 2, 4); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1147 | } else { |
| 1148 | write(fd, result.string(), result.size()); |
| 1149 | } |
| 1150 | } |
| 1151 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 1152 | sp<BasicClient> client = mClient[i].promote(); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1153 | if (client == 0) { |
| 1154 | result = String8::format(" Device is closed, no client instance\n"); |
| 1155 | write(fd, result.string(), result.size()); |
| 1156 | continue; |
| 1157 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1158 | hasClient = true; |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1159 | result = String8::format(" Device is open. Client instance dump:\n"); |
| 1160 | write(fd, result.string(), result.size()); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 1161 | client->dump(fd, args); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1162 | } |
| 1163 | if (!hasClient) { |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1164 | result = String8::format("\nNo active camera clients yet.\n"); |
| 1165 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | if (locked) mServiceLock.unlock(); |
| 1169 | |
| 1170 | // change logging level |
| 1171 | int n = args.size(); |
| 1172 | for (int i = 0; i + 1 < n; i++) { |
Eino-Ville Talvala | 611f619 | 2012-05-31 12:28:23 -0700 | [diff] [blame] | 1173 | String16 verboseOption("-v"); |
| 1174 | if (args[i] == verboseOption) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1175 | String8 levelStr(args[i+1]); |
| 1176 | int level = atoi(levelStr.string()); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1177 | result = String8::format("\nSetting log level to %d.\n", level); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1178 | setLogLevel(level); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1179 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1180 | } |
| 1181 | } |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 1182 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1183 | } |
| 1184 | return NO_ERROR; |
| 1185 | } |
| 1186 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1187 | /*virtual*/void CameraService::binderDied( |
| 1188 | const wp<IBinder> &who) { |
| 1189 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 1190 | /** |
| 1191 | * While tempting to promote the wp<IBinder> into a sp, |
| 1192 | * it's actually not supported by the binder driver |
| 1193 | */ |
| 1194 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1195 | ALOGV("java clients' binder died"); |
| 1196 | |
Igor Murashkin | 634a515 | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 1197 | sp<BasicClient> cameraClient = getClientByRemote(who); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1198 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 1199 | if (cameraClient == 0) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1200 | ALOGV("java clients' binder death already cleaned up (normal case)"); |
| 1201 | return; |
| 1202 | } |
| 1203 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 1204 | ALOGW("Disconnecting camera client %p since the binder for it " |
| 1205 | "died (this pid %d)", cameraClient.get(), getCallingPid()); |
| 1206 | |
| 1207 | cameraClient->disconnect(); |
| 1208 | |
| 1209 | } |
| 1210 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1211 | void CameraService::updateStatus(ICameraServiceListener::Status status, |
Igor Murashkin | 93747b9 | 2013-05-01 15:42:20 -0700 | [diff] [blame] | 1212 | int32_t cameraId, |
| 1213 | const StatusVector *rejectSourceStates) { |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1214 | // do not lock mServiceLock here or can get into a deadlock from |
| 1215 | // connect() -> ProClient::disconnect -> updateStatus |
| 1216 | Mutex::Autolock lock(mStatusMutex); |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1217 | |
| 1218 | ICameraServiceListener::Status oldStatus = mStatusList[cameraId]; |
| 1219 | |
| 1220 | mStatusList[cameraId] = status; |
| 1221 | |
| 1222 | if (oldStatus != status) { |
| 1223 | ALOGV("%s: Status has changed for camera ID %d from 0x%x to 0x%x", |
| 1224 | __FUNCTION__, cameraId, (uint32_t)oldStatus, (uint32_t)status); |
| 1225 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1226 | if (oldStatus == ICameraServiceListener::STATUS_NOT_PRESENT && |
| 1227 | (status != ICameraServiceListener::STATUS_PRESENT && |
| 1228 | status != ICameraServiceListener::STATUS_ENUMERATING)) { |
| 1229 | |
| 1230 | ALOGW("%s: From NOT_PRESENT can only transition into PRESENT" |
| 1231 | " or ENUMERATING", __FUNCTION__); |
| 1232 | mStatusList[cameraId] = oldStatus; |
| 1233 | return; |
| 1234 | } |
| 1235 | |
Igor Murashkin | 93747b9 | 2013-05-01 15:42:20 -0700 | [diff] [blame] | 1236 | if (rejectSourceStates != NULL) { |
| 1237 | const StatusVector &rejectList = *rejectSourceStates; |
| 1238 | StatusVector::const_iterator it = rejectList.begin(); |
| 1239 | |
| 1240 | /** |
| 1241 | * Sometimes we want to conditionally do a transition. |
| 1242 | * For example if a client disconnects, we want to go to PRESENT |
| 1243 | * only if we weren't already in NOT_PRESENT or ENUMERATING. |
| 1244 | */ |
| 1245 | for (; it != rejectList.end(); ++it) { |
| 1246 | if (oldStatus == *it) { |
| 1247 | ALOGV("%s: Rejecting status transition for Camera ID %d, " |
| 1248 | " since the source state was was in one of the bad " |
| 1249 | " states.", __FUNCTION__, cameraId); |
| 1250 | mStatusList[cameraId] = oldStatus; |
| 1251 | return; |
| 1252 | } |
| 1253 | } |
| 1254 | } |
| 1255 | |
Igor Murashkin | bfc9915 | 2013-02-27 12:55:20 -0800 | [diff] [blame] | 1256 | /** |
| 1257 | * ProClients lose their exclusive lock. |
| 1258 | * - Done before the CameraClient can initialize the HAL device, |
| 1259 | * since we want to be able to close it before they get to initialize |
| 1260 | */ |
| 1261 | if (status == ICameraServiceListener::STATUS_NOT_AVAILABLE) { |
| 1262 | Vector<wp<ProClient> > proClients(mProClientList[cameraId]); |
| 1263 | Vector<wp<ProClient> >::const_iterator it; |
| 1264 | |
| 1265 | for (it = proClients.begin(); it != proClients.end(); ++it) { |
| 1266 | sp<ProClient> proCl = it->promote(); |
| 1267 | if (proCl.get() != NULL) { |
| 1268 | proCl->onExclusiveLockStolen(); |
| 1269 | } |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | Vector<sp<ICameraServiceListener> >::const_iterator it; |
| 1274 | for (it = mListenerList.begin(); it != mListenerList.end(); ++it) { |
| 1275 | (*it)->onStatusChanged(status, cameraId); |
| 1276 | } |
| 1277 | } |
| 1278 | } |
| 1279 | |
Igor Murashkin | cba2c16 | 2013-03-20 15:56:31 -0700 | [diff] [blame] | 1280 | ICameraServiceListener::Status CameraService::getStatus(int cameraId) const { |
| 1281 | if (cameraId < 0 || cameraId >= MAX_CAMERAS) { |
| 1282 | ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId); |
| 1283 | return ICameraServiceListener::STATUS_UNKNOWN; |
| 1284 | } |
| 1285 | |
| 1286 | Mutex::Autolock al(mStatusMutex); |
| 1287 | return mStatusList[cameraId]; |
| 1288 | } |
| 1289 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1290 | }; // namespace android |