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 | |
| 25 | #include <binder/IPCThreadState.h> |
| 26 | #include <binder/IServiceManager.h> |
| 27 | #include <binder/MemoryBase.h> |
| 28 | #include <binder/MemoryHeapBase.h> |
| 29 | #include <cutils/atomic.h> |
Nipun Kwatra | b5ca461 | 2010-09-11 19:31:10 -0700 | [diff] [blame] | 30 | #include <cutils/properties.h> |
Jamie Gennis | bfa33aa | 2010-12-20 11:51:31 -0800 | [diff] [blame] | 31 | #include <gui/SurfaceTextureClient.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" |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 42 | #include "CameraHardwareInterface.h" |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 43 | #include "Camera2Client.h" |
| 44 | #include "Camera2Device.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 45 | |
| 46 | namespace android { |
| 47 | |
| 48 | // ---------------------------------------------------------------------------- |
| 49 | // Logging support -- this is for debugging only |
| 50 | // Use "adb shell dumpsys media.camera -v 1" to change it. |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 51 | volatile int32_t gLogLevel = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 52 | |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 53 | #define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__); |
| 54 | #define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 55 | |
| 56 | static void setLogLevel(int level) { |
| 57 | android_atomic_write(level, &gLogLevel); |
| 58 | } |
| 59 | |
| 60 | // ---------------------------------------------------------------------------- |
| 61 | |
| 62 | static int getCallingPid() { |
| 63 | return IPCThreadState::self()->getCallingPid(); |
| 64 | } |
| 65 | |
| 66 | static int getCallingUid() { |
| 67 | return IPCThreadState::self()->getCallingUid(); |
| 68 | } |
| 69 | |
| 70 | // ---------------------------------------------------------------------------- |
| 71 | |
| 72 | // This is ugly and only safe if we never re-create the CameraService, but |
| 73 | // should be ok for now. |
| 74 | static CameraService *gCameraService; |
| 75 | |
| 76 | CameraService::CameraService() |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 77 | :mSoundRef(0), mModule(0) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 78 | { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 79 | ALOGI("CameraService started (pid=%d)", getpid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 80 | gCameraService = this; |
| 81 | } |
| 82 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 83 | void CameraService::onFirstRef() |
| 84 | { |
| 85 | BnCameraService::onFirstRef(); |
| 86 | |
| 87 | if (hw_get_module(CAMERA_HARDWARE_MODULE_ID, |
| 88 | (const hw_module_t **)&mModule) < 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 89 | ALOGE("Could not load camera HAL module"); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 90 | mNumberOfCameras = 0; |
| 91 | } |
| 92 | else { |
| 93 | mNumberOfCameras = mModule->get_number_of_cameras(); |
| 94 | if (mNumberOfCameras > MAX_CAMERAS) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 95 | ALOGE("Number of cameras(%d) > MAX_CAMERAS(%d).", |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 96 | mNumberOfCameras, MAX_CAMERAS); |
| 97 | mNumberOfCameras = MAX_CAMERAS; |
| 98 | } |
| 99 | for (int i = 0; i < mNumberOfCameras; i++) { |
| 100 | setCameraFree(i); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 105 | CameraService::~CameraService() { |
| 106 | for (int i = 0; i < mNumberOfCameras; i++) { |
| 107 | if (mBusy[i]) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 108 | ALOGE("camera %d is still in use in destructor!", i); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | gCameraService = NULL; |
| 113 | } |
| 114 | |
| 115 | int32_t CameraService::getNumberOfCameras() { |
| 116 | return mNumberOfCameras; |
| 117 | } |
| 118 | |
| 119 | status_t CameraService::getCameraInfo(int cameraId, |
| 120 | struct CameraInfo* cameraInfo) { |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 121 | if (!mModule) { |
| 122 | return NO_INIT; |
| 123 | } |
| 124 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 125 | if (cameraId < 0 || cameraId >= mNumberOfCameras) { |
| 126 | return BAD_VALUE; |
| 127 | } |
| 128 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 129 | struct camera_info info; |
| 130 | status_t rc = mModule->get_camera_info(cameraId, &info); |
| 131 | cameraInfo->facing = info.facing; |
| 132 | cameraInfo->orientation = info.orientation; |
| 133 | return rc; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | sp<ICamera> CameraService::connect( |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 137 | const sp<ICameraClient>& cameraClient, int cameraId) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 138 | int callingPid = getCallingPid(); |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 139 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 140 | LOG1("CameraService::connect E (pid %d, id %d)", callingPid, cameraId); |
| 141 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 142 | if (!mModule) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 143 | ALOGE("Camera HAL module not loaded"); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 144 | return NULL; |
| 145 | } |
| 146 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 147 | sp<Client> client; |
| 148 | if (cameraId < 0 || cameraId >= mNumberOfCameras) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 149 | ALOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 150 | callingPid, cameraId); |
| 151 | return NULL; |
| 152 | } |
| 153 | |
Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 154 | char value[PROPERTY_VALUE_MAX]; |
| 155 | property_get("sys.secpolicy.camera.disabled", value, "0"); |
| 156 | if (strcmp(value, "1") == 0) { |
| 157 | // Camera is disabled by DevicePolicyManager. |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 158 | ALOGI("Camera is disabled. connect X (pid %d) rejected", callingPid); |
Wu-cheng Li | a335543 | 2011-05-20 14:54:25 +0800 | [diff] [blame] | 159 | return NULL; |
| 160 | } |
| 161 | |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 162 | Mutex::Autolock lock(mServiceLock); |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 163 | if (mClient[cameraId] != 0) { |
| 164 | client = mClient[cameraId].promote(); |
| 165 | if (client != 0) { |
| 166 | if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) { |
| 167 | LOG1("CameraService::connect X (pid %d) (the same client)", |
| 168 | callingPid); |
| 169 | return client; |
| 170 | } else { |
| 171 | ALOGW("CameraService::connect X (pid %d) rejected (existing client).", |
| 172 | callingPid); |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 173 | return NULL; |
| 174 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 175 | } |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 176 | mClient[cameraId].clear(); |
| 177 | } |
| 178 | |
| 179 | if (mBusy[cameraId]) { |
| 180 | ALOGW("CameraService::connect X (pid %d) rejected" |
| 181 | " (camera %d is still busy).", callingPid, cameraId); |
| 182 | return NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 185 | struct camera_info info; |
| 186 | if (mModule->get_camera_info(cameraId, &info) != OK) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 187 | ALOGE("Invalid camera id %d", cameraId); |
Wu-cheng Li | b7a6794 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 188 | return NULL; |
| 189 | } |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 190 | |
| 191 | char camera_device_name[10]; |
| 192 | snprintf(camera_device_name, sizeof(camera_device_name), "%d", cameraId); |
| 193 | |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 194 | int deviceVersion; |
| 195 | if (mModule->common.module_api_version == CAMERA_MODULE_API_VERSION_2_0) { |
| 196 | deviceVersion = info.device_version; |
| 197 | } else { |
| 198 | deviceVersion = CAMERA_DEVICE_API_VERSION_1_0; |
| 199 | } |
| 200 | |
| 201 | switch(deviceVersion) { |
| 202 | case CAMERA_DEVICE_API_VERSION_1_0: { |
| 203 | sp<CameraHardwareInterface> hardware = |
| 204 | new CameraHardwareInterface(camera_device_name); |
| 205 | if (hardware->initialize(&mModule->common) != OK) { |
| 206 | return NULL; |
| 207 | } |
| 208 | |
| 209 | client = new CameraClient(this, cameraClient, hardware, cameraId, |
| 210 | info.facing, callingPid); |
| 211 | break; |
| 212 | } |
| 213 | case CAMERA_DEVICE_API_VERSION_2_0: { |
| 214 | sp<Camera2Device> hardware = |
| 215 | new Camera2Device(camera_device_name); |
| 216 | if (hardware->initialize(&mModule->common) != OK) { |
| 217 | return NULL; |
| 218 | } |
| 219 | |
| 220 | client = new Camera2Client(this, cameraClient, hardware, cameraId, |
| 221 | info.facing, callingPid); |
| 222 | break; |
| 223 | } |
| 224 | default: |
| 225 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 226 | return NULL; |
| 227 | } |
| 228 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 229 | mClient[cameraId] = client; |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 230 | LOG1("CameraService::connect X (id %d)", cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 231 | return client; |
| 232 | } |
| 233 | |
| 234 | void CameraService::removeClient(const sp<ICameraClient>& cameraClient) { |
| 235 | int callingPid = getCallingPid(); |
| 236 | LOG1("CameraService::removeClient E (pid %d)", callingPid); |
| 237 | |
| 238 | for (int i = 0; i < mNumberOfCameras; i++) { |
| 239 | // Declare this before the lock to make absolutely sure the |
| 240 | // destructor won't be called with the lock held. |
| 241 | sp<Client> client; |
| 242 | |
| 243 | Mutex::Autolock lock(mServiceLock); |
| 244 | |
| 245 | // This happens when we have already disconnected (or this is |
| 246 | // just another unused camera). |
| 247 | if (mClient[i] == 0) continue; |
| 248 | |
| 249 | // Promote mClient. It can fail if we are called from this path: |
| 250 | // Client::~Client() -> disconnect() -> removeClient(). |
| 251 | client = mClient[i].promote(); |
| 252 | |
| 253 | if (client == 0) { |
| 254 | mClient[i].clear(); |
| 255 | continue; |
| 256 | } |
| 257 | |
| 258 | if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) { |
| 259 | // Found our camera, clear and leave. |
| 260 | LOG1("removeClient: clear camera %d", i); |
| 261 | mClient[i].clear(); |
| 262 | break; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | LOG1("CameraService::removeClient X (pid %d)", callingPid); |
| 267 | } |
| 268 | |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 269 | CameraService::Client* CameraService::getClientByIdUnsafe(int cameraId) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 270 | if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL; |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 271 | return mClient[cameraId].unsafe_get(); |
| 272 | } |
| 273 | |
| 274 | Mutex* CameraService::getClientLockById(int cameraId) { |
| 275 | if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL; |
| 276 | return &mClientLock[cameraId]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 279 | status_t CameraService::onTransact( |
| 280 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { |
| 281 | // Permission checks |
| 282 | switch (code) { |
| 283 | case BnCameraService::CONNECT: |
| 284 | const int pid = getCallingPid(); |
| 285 | const int self_pid = getpid(); |
| 286 | if (pid != self_pid) { |
| 287 | // we're called from a different process, do the real check |
| 288 | if (!checkCallingPermission( |
| 289 | String16("android.permission.CAMERA"))) { |
| 290 | const int uid = getCallingUid(); |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 291 | ALOGE("Permission Denial: " |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 292 | "can't use the camera pid=%d, uid=%d", pid, uid); |
| 293 | return PERMISSION_DENIED; |
| 294 | } |
| 295 | } |
| 296 | break; |
| 297 | } |
| 298 | |
| 299 | return BnCameraService::onTransact(code, data, reply, flags); |
| 300 | } |
| 301 | |
| 302 | // The reason we need this busy bit is a new CameraService::connect() request |
| 303 | // may come in while the previous Client's destructor has not been run or is |
| 304 | // still running. If the last strong reference of the previous Client is gone |
| 305 | // but the destructor has not been finished, we should not allow the new Client |
| 306 | // to be created because we need to wait for the previous Client to tear down |
| 307 | // the hardware first. |
| 308 | void CameraService::setCameraBusy(int cameraId) { |
| 309 | android_atomic_write(1, &mBusy[cameraId]); |
| 310 | } |
| 311 | |
| 312 | void CameraService::setCameraFree(int cameraId) { |
| 313 | android_atomic_write(0, &mBusy[cameraId]); |
| 314 | } |
| 315 | |
| 316 | // We share the media players for shutter and recording sound for all clients. |
| 317 | // A reference count is kept to determine when we will actually release the |
| 318 | // media players. |
| 319 | |
Chih-Chung Chang | ff4f55c | 2011-10-17 19:03:12 +0800 | [diff] [blame] | 320 | MediaPlayer* CameraService::newMediaPlayer(const char *file) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 321 | MediaPlayer* mp = new MediaPlayer(); |
| 322 | if (mp->setDataSource(file, NULL) == NO_ERROR) { |
Eino-Ville Talvala | 60a78ac | 2012-01-05 15:34:53 -0800 | [diff] [blame] | 323 | mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 324 | mp->prepare(); |
| 325 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 326 | ALOGE("Failed to load CameraService sounds: %s", file); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 327 | return NULL; |
| 328 | } |
| 329 | return mp; |
| 330 | } |
| 331 | |
| 332 | void CameraService::loadSound() { |
| 333 | Mutex::Autolock lock(mSoundLock); |
| 334 | LOG1("CameraService::loadSound ref=%d", mSoundRef); |
| 335 | if (mSoundRef++) return; |
| 336 | |
| 337 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); |
| 338 | mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); |
| 339 | } |
| 340 | |
| 341 | void CameraService::releaseSound() { |
| 342 | Mutex::Autolock lock(mSoundLock); |
| 343 | LOG1("CameraService::releaseSound ref=%d", mSoundRef); |
| 344 | if (--mSoundRef) return; |
| 345 | |
| 346 | for (int i = 0; i < NUM_SOUNDS; i++) { |
| 347 | if (mSoundPlayer[i] != 0) { |
| 348 | mSoundPlayer[i]->disconnect(); |
| 349 | mSoundPlayer[i].clear(); |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | void CameraService::playSound(sound_kind kind) { |
| 355 | LOG1("playSound(%d)", kind); |
| 356 | Mutex::Autolock lock(mSoundLock); |
| 357 | sp<MediaPlayer> player = mSoundPlayer[kind]; |
| 358 | if (player != 0) { |
Chih-Chung Chang | 8888a75 | 2011-10-20 10:47:26 +0800 | [diff] [blame] | 359 | player->seekTo(0); |
| 360 | player->start(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
| 364 | // ---------------------------------------------------------------------------- |
| 365 | |
| 366 | CameraService::Client::Client(const sp<CameraService>& cameraService, |
Wu-cheng Li | b7a6794 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 367 | const sp<ICameraClient>& cameraClient, |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 368 | int cameraId, int cameraFacing, int clientPid) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 369 | int callingPid = getCallingPid(); |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 370 | LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 371 | |
| 372 | mCameraService = cameraService; |
| 373 | mCameraClient = cameraClient; |
| 374 | mCameraId = cameraId; |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 375 | mCameraFacing = cameraFacing; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 376 | mClientPid = clientPid; |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 377 | mDestructionStarted = false; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 378 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 379 | cameraService->setCameraBusy(cameraId); |
| 380 | cameraService->loadSound(); |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 381 | LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 384 | // tear down the client |
| 385 | CameraService::Client::~Client() { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | // ---------------------------------------------------------------------------- |
| 389 | |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 390 | Mutex* CameraService::Client::getClientLockFromCookie(void* user) { |
| 391 | return gCameraService->getClientLockById((int) user); |
| 392 | } |
| 393 | |
| 394 | // Provide client pointer for callbacks. Client lock returned from getClientLockFromCookie should |
| 395 | // be acquired for this to be safe |
| 396 | CameraService::Client* CameraService::Client::getClientFromCookie(void* user) { |
| 397 | Client* client = gCameraService->getClientByIdUnsafe((int) user); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 398 | |
| 399 | // This could happen if the Client is in the process of shutting down (the |
| 400 | // last strong reference is gone, but the destructor hasn't finished |
| 401 | // stopping the hardware). |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 402 | if (client == NULL) return NULL; |
| 403 | |
| 404 | // destruction already started, so should not be accessed |
| 405 | if (client->mDestructionStarted) return NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 406 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 407 | return client; |
| 408 | } |
| 409 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 410 | void CameraService::Client::disconnect() { |
| 411 | mCameraService->removeClient(mCameraClient); |
| 412 | mCameraService->setCameraFree(mCameraId); |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 413 | } |
| 414 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 415 | // ---------------------------------------------------------------------------- |
| 416 | |
| 417 | static const int kDumpLockRetries = 50; |
| 418 | static const int kDumpLockSleep = 60000; |
| 419 | |
| 420 | static bool tryLock(Mutex& mutex) |
| 421 | { |
| 422 | bool locked = false; |
| 423 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 424 | if (mutex.tryLock() == NO_ERROR) { |
| 425 | locked = true; |
| 426 | break; |
| 427 | } |
| 428 | usleep(kDumpLockSleep); |
| 429 | } |
| 430 | return locked; |
| 431 | } |
| 432 | |
| 433 | status_t CameraService::dump(int fd, const Vector<String16>& args) { |
| 434 | static const char* kDeadlockedString = "CameraService may be deadlocked\n"; |
| 435 | |
| 436 | const size_t SIZE = 256; |
| 437 | char buffer[SIZE]; |
| 438 | String8 result; |
| 439 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 440 | snprintf(buffer, SIZE, "Permission Denial: " |
| 441 | "can't dump CameraService from pid=%d, uid=%d\n", |
| 442 | getCallingPid(), |
| 443 | getCallingUid()); |
| 444 | result.append(buffer); |
| 445 | write(fd, result.string(), result.size()); |
| 446 | } else { |
| 447 | bool locked = tryLock(mServiceLock); |
| 448 | // failed to lock - CameraService is probably deadlocked |
| 449 | if (!locked) { |
| 450 | String8 result(kDeadlockedString); |
| 451 | write(fd, result.string(), result.size()); |
| 452 | } |
| 453 | |
| 454 | bool hasClient = false; |
| 455 | for (int i = 0; i < mNumberOfCameras; i++) { |
| 456 | sp<Client> client = mClient[i].promote(); |
| 457 | if (client == 0) continue; |
| 458 | hasClient = true; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 459 | client->dump(fd, args); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 460 | } |
| 461 | if (!hasClient) { |
| 462 | result.append("No camera client yet.\n"); |
| 463 | write(fd, result.string(), result.size()); |
| 464 | } |
| 465 | |
| 466 | if (locked) mServiceLock.unlock(); |
| 467 | |
| 468 | // change logging level |
| 469 | int n = args.size(); |
| 470 | for (int i = 0; i + 1 < n; i++) { |
| 471 | if (args[i] == String16("-v")) { |
| 472 | String8 levelStr(args[i+1]); |
| 473 | int level = atoi(levelStr.string()); |
| 474 | sprintf(buffer, "Set Log Level to %d", level); |
| 475 | result.append(buffer); |
| 476 | setLogLevel(level); |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | return NO_ERROR; |
| 481 | } |
| 482 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 483 | }; // namespace android |