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