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> |
Mathias Agopian | df712ea | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 31 | #include <gui/Surface.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 32 | #include <hardware/hardware.h> |
| 33 | #include <media/AudioSystem.h> |
| 34 | #include <media/mediaplayer.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 35 | #include <utils/Errors.h> |
| 36 | #include <utils/Log.h> |
| 37 | #include <utils/String16.h> |
| 38 | |
| 39 | #include "CameraService.h" |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 40 | #include "CameraClient.h" |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 41 | #include "Camera2Client.h" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 42 | |
| 43 | namespace android { |
| 44 | |
| 45 | // ---------------------------------------------------------------------------- |
| 46 | // Logging support -- this is for debugging only |
| 47 | // Use "adb shell dumpsys media.camera -v 1" to change it. |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 48 | volatile int32_t gLogLevel = 0; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 49 | |
Steve Block | b8a8052 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 50 | #define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__); |
| 51 | #define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 52 | |
| 53 | static void setLogLevel(int level) { |
| 54 | android_atomic_write(level, &gLogLevel); |
| 55 | } |
| 56 | |
| 57 | // ---------------------------------------------------------------------------- |
| 58 | |
| 59 | static int getCallingPid() { |
| 60 | return IPCThreadState::self()->getCallingPid(); |
| 61 | } |
| 62 | |
| 63 | static int getCallingUid() { |
| 64 | return IPCThreadState::self()->getCallingUid(); |
| 65 | } |
| 66 | |
| 67 | // ---------------------------------------------------------------------------- |
| 68 | |
| 69 | // This is ugly and only safe if we never re-create the CameraService, but |
| 70 | // should be ok for now. |
| 71 | static CameraService *gCameraService; |
| 72 | |
| 73 | CameraService::CameraService() |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 74 | :mSoundRef(0), mModule(0) |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 75 | { |
Steve Block | df64d15 | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 76 | ALOGI("CameraService started (pid=%d)", getpid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 77 | gCameraService = this; |
| 78 | } |
| 79 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 80 | void CameraService::onFirstRef() |
| 81 | { |
| 82 | BnCameraService::onFirstRef(); |
| 83 | |
| 84 | if (hw_get_module(CAMERA_HARDWARE_MODULE_ID, |
| 85 | (const hw_module_t **)&mModule) < 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 86 | ALOGE("Could not load camera HAL module"); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 87 | mNumberOfCameras = 0; |
| 88 | } |
| 89 | else { |
Alex Ray | c0dd54f | 2013-02-20 13:39:37 -0800 | [diff] [blame] | 90 | ALOGI("Loaded \"%s\" camera module", mModule->common.name); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 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 { |
Igor Murashkin | 1d88023 | 2013-02-20 16:50:13 -0800 | [diff] [blame^] | 169 | // TODOSC: need to support 1 regular client, multiple shared clients here |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 170 | ALOGW("CameraService::connect X (pid %d) rejected (existing client).", |
| 171 | callingPid); |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 172 | return NULL; |
| 173 | } |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 174 | } |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 175 | mClient[cameraId].clear(); |
| 176 | } |
| 177 | |
| 178 | if (mBusy[cameraId]) { |
| 179 | ALOGW("CameraService::connect X (pid %d) rejected" |
| 180 | " (camera %d is still busy).", callingPid, cameraId); |
| 181 | return NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 184 | struct camera_info info; |
| 185 | if (mModule->get_camera_info(cameraId, &info) != OK) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 186 | ALOGE("Invalid camera id %d", cameraId); |
Wu-cheng Li | b7a6794 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 187 | return NULL; |
| 188 | } |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 189 | |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 190 | int deviceVersion; |
| 191 | if (mModule->common.module_api_version == CAMERA_MODULE_API_VERSION_2_0) { |
| 192 | deviceVersion = info.device_version; |
| 193 | } else { |
| 194 | deviceVersion = CAMERA_DEVICE_API_VERSION_1_0; |
| 195 | } |
| 196 | |
| 197 | switch(deviceVersion) { |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 198 | case CAMERA_DEVICE_API_VERSION_1_0: |
| 199 | client = new CameraClient(this, cameraClient, cameraId, |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 200 | info.facing, callingPid, getpid()); |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 201 | break; |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 202 | case CAMERA_DEVICE_API_VERSION_2_0: |
| 203 | client = new Camera2Client(this, cameraClient, cameraId, |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 204 | info.facing, callingPid, getpid()); |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 205 | break; |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 206 | default: |
Eino-Ville Talvala | 61ab9f9 | 2012-05-17 10:30:54 -0700 | [diff] [blame] | 207 | ALOGE("Unknown camera device HAL version: %d", deviceVersion); |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 208 | return NULL; |
| 209 | } |
| 210 | |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 211 | if (client->initialize(mModule) != OK) { |
| 212 | return NULL; |
| 213 | } |
| 214 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 215 | cameraClient->asBinder()->linkToDeath(this); |
| 216 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 217 | mClient[cameraId] = client; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 218 | LOG1("CameraService::connect X (id %d, this pid is %d)", cameraId, getpid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 219 | return client; |
| 220 | } |
| 221 | |
| 222 | void CameraService::removeClient(const sp<ICameraClient>& cameraClient) { |
| 223 | int callingPid = getCallingPid(); |
| 224 | LOG1("CameraService::removeClient E (pid %d)", callingPid); |
| 225 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 226 | // Declare this before the lock to make absolutely sure the |
| 227 | // destructor won't be called with the lock held. |
| 228 | Mutex::Autolock lock(mServiceLock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 229 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 230 | int outIndex; |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 231 | sp<Client> client = findClientUnsafe(cameraClient->asBinder(), outIndex); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 232 | |
| 233 | if (client != 0) { |
| 234 | // Found our camera, clear and leave. |
| 235 | LOG1("removeClient: clear camera %d", outIndex); |
| 236 | mClient[outIndex].clear(); |
| 237 | |
| 238 | client->unlinkToDeath(this); |
| 239 | } |
| 240 | |
| 241 | LOG1("CameraService::removeClient X (pid %d)", callingPid); |
| 242 | } |
| 243 | |
| 244 | sp<CameraService::Client> CameraService::findClientUnsafe( |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 245 | const wp<IBinder>& cameraClient, int& outIndex) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 246 | sp<Client> client; |
| 247 | |
| 248 | for (int i = 0; i < mNumberOfCameras; i++) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 249 | |
| 250 | // This happens when we have already disconnected (or this is |
| 251 | // just another unused camera). |
| 252 | if (mClient[i] == 0) continue; |
| 253 | |
| 254 | // Promote mClient. It can fail if we are called from this path: |
| 255 | // Client::~Client() -> disconnect() -> removeClient(). |
| 256 | client = mClient[i].promote(); |
| 257 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 258 | // Clean up stale client entry |
| 259 | if (client == NULL) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 260 | mClient[i].clear(); |
| 261 | continue; |
| 262 | } |
| 263 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 264 | if (cameraClient == client->getCameraClient()->asBinder()) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 265 | // Found our camera |
| 266 | outIndex = i; |
| 267 | return client; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 271 | outIndex = -1; |
| 272 | return NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 275 | CameraService::Client* CameraService::getClientByIdUnsafe(int cameraId) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 276 | if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL; |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 277 | return mClient[cameraId].unsafe_get(); |
| 278 | } |
| 279 | |
| 280 | Mutex* CameraService::getClientLockById(int cameraId) { |
| 281 | if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL; |
| 282 | return &mClientLock[cameraId]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 285 | sp<CameraService::Client> CameraService::getClientByRemote( |
| 286 | const wp<IBinder>& cameraClient) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 287 | |
| 288 | // Declare this before the lock to make absolutely sure the |
| 289 | // destructor won't be called with the lock held. |
| 290 | sp<Client> client; |
| 291 | |
| 292 | Mutex::Autolock lock(mServiceLock); |
| 293 | |
| 294 | int outIndex; |
| 295 | client = findClientUnsafe(cameraClient, outIndex); |
| 296 | |
| 297 | return client; |
| 298 | } |
| 299 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 300 | status_t CameraService::onTransact( |
| 301 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { |
| 302 | // Permission checks |
| 303 | switch (code) { |
| 304 | case BnCameraService::CONNECT: |
| 305 | const int pid = getCallingPid(); |
| 306 | const int self_pid = getpid(); |
| 307 | if (pid != self_pid) { |
| 308 | // we're called from a different process, do the real check |
| 309 | if (!checkCallingPermission( |
| 310 | String16("android.permission.CAMERA"))) { |
| 311 | const int uid = getCallingUid(); |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 312 | ALOGE("Permission Denial: " |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 313 | "can't use the camera pid=%d, uid=%d", pid, uid); |
| 314 | return PERMISSION_DENIED; |
| 315 | } |
| 316 | } |
| 317 | break; |
| 318 | } |
| 319 | |
| 320 | return BnCameraService::onTransact(code, data, reply, flags); |
| 321 | } |
| 322 | |
| 323 | // The reason we need this busy bit is a new CameraService::connect() request |
| 324 | // may come in while the previous Client's destructor has not been run or is |
| 325 | // still running. If the last strong reference of the previous Client is gone |
| 326 | // but the destructor has not been finished, we should not allow the new Client |
| 327 | // to be created because we need to wait for the previous Client to tear down |
| 328 | // the hardware first. |
| 329 | void CameraService::setCameraBusy(int cameraId) { |
| 330 | android_atomic_write(1, &mBusy[cameraId]); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 331 | |
| 332 | ALOGV("setCameraBusy cameraId=%d", cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | void CameraService::setCameraFree(int cameraId) { |
| 336 | android_atomic_write(0, &mBusy[cameraId]); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 337 | |
| 338 | ALOGV("setCameraFree cameraId=%d", cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | // We share the media players for shutter and recording sound for all clients. |
| 342 | // A reference count is kept to determine when we will actually release the |
| 343 | // media players. |
| 344 | |
Chih-Chung Chang | ff4f55c | 2011-10-17 19:03:12 +0800 | [diff] [blame] | 345 | MediaPlayer* CameraService::newMediaPlayer(const char *file) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 346 | MediaPlayer* mp = new MediaPlayer(); |
| 347 | if (mp->setDataSource(file, NULL) == NO_ERROR) { |
Eino-Ville Talvala | 60a78ac | 2012-01-05 15:34:53 -0800 | [diff] [blame] | 348 | mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 349 | mp->prepare(); |
| 350 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 351 | ALOGE("Failed to load CameraService sounds: %s", file); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 352 | return NULL; |
| 353 | } |
| 354 | return mp; |
| 355 | } |
| 356 | |
| 357 | void CameraService::loadSound() { |
| 358 | Mutex::Autolock lock(mSoundLock); |
| 359 | LOG1("CameraService::loadSound ref=%d", mSoundRef); |
| 360 | if (mSoundRef++) return; |
| 361 | |
| 362 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); |
| 363 | mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); |
| 364 | } |
| 365 | |
| 366 | void CameraService::releaseSound() { |
| 367 | Mutex::Autolock lock(mSoundLock); |
| 368 | LOG1("CameraService::releaseSound ref=%d", mSoundRef); |
| 369 | if (--mSoundRef) return; |
| 370 | |
| 371 | for (int i = 0; i < NUM_SOUNDS; i++) { |
| 372 | if (mSoundPlayer[i] != 0) { |
| 373 | mSoundPlayer[i]->disconnect(); |
| 374 | mSoundPlayer[i].clear(); |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | void CameraService::playSound(sound_kind kind) { |
| 380 | LOG1("playSound(%d)", kind); |
| 381 | Mutex::Autolock lock(mSoundLock); |
| 382 | sp<MediaPlayer> player = mSoundPlayer[kind]; |
| 383 | if (player != 0) { |
Chih-Chung Chang | 8888a75 | 2011-10-20 10:47:26 +0800 | [diff] [blame] | 384 | player->seekTo(0); |
| 385 | player->start(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
| 389 | // ---------------------------------------------------------------------------- |
| 390 | |
| 391 | CameraService::Client::Client(const sp<CameraService>& cameraService, |
Wu-cheng Li | b7a6794 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 392 | const sp<ICameraClient>& cameraClient, |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 393 | int cameraId, int cameraFacing, int clientPid, int servicePid) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 394 | int callingPid = getCallingPid(); |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 395 | LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 396 | |
| 397 | mCameraService = cameraService; |
| 398 | mCameraClient = cameraClient; |
| 399 | mCameraId = cameraId; |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 400 | mCameraFacing = cameraFacing; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 401 | mClientPid = clientPid; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 402 | mServicePid = servicePid; |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 403 | mDestructionStarted = false; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 404 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 405 | cameraService->setCameraBusy(cameraId); |
| 406 | cameraService->loadSound(); |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 407 | LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 410 | // tear down the client |
| 411 | CameraService::Client::~Client() { |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 412 | mCameraService->releaseSound(); |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 413 | |
| 414 | // unconditionally disconnect. function is idempotent |
| 415 | Client::disconnect(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | // ---------------------------------------------------------------------------- |
| 419 | |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 420 | Mutex* CameraService::Client::getClientLockFromCookie(void* user) { |
| 421 | return gCameraService->getClientLockById((int) user); |
| 422 | } |
| 423 | |
| 424 | // Provide client pointer for callbacks. Client lock returned from getClientLockFromCookie should |
| 425 | // be acquired for this to be safe |
| 426 | CameraService::Client* CameraService::Client::getClientFromCookie(void* user) { |
| 427 | Client* client = gCameraService->getClientByIdUnsafe((int) user); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 428 | |
| 429 | // This could happen if the Client is in the process of shutting down (the |
| 430 | // last strong reference is gone, but the destructor hasn't finished |
| 431 | // stopping the hardware). |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 432 | if (client == NULL) return NULL; |
| 433 | |
| 434 | // destruction already started, so should not be accessed |
| 435 | if (client->mDestructionStarted) return NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 436 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 437 | return client; |
| 438 | } |
| 439 | |
Igor Murashkin | 036bc3e | 2012-10-08 15:09:46 -0700 | [diff] [blame] | 440 | // NOTE: function is idempotent |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 441 | void CameraService::Client::disconnect() { |
| 442 | mCameraService->removeClient(mCameraClient); |
| 443 | mCameraService->setCameraFree(mCameraId); |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 444 | } |
| 445 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 446 | // ---------------------------------------------------------------------------- |
| 447 | |
| 448 | static const int kDumpLockRetries = 50; |
| 449 | static const int kDumpLockSleep = 60000; |
| 450 | |
| 451 | static bool tryLock(Mutex& mutex) |
| 452 | { |
| 453 | bool locked = false; |
| 454 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 455 | if (mutex.tryLock() == NO_ERROR) { |
| 456 | locked = true; |
| 457 | break; |
| 458 | } |
| 459 | usleep(kDumpLockSleep); |
| 460 | } |
| 461 | return locked; |
| 462 | } |
| 463 | |
| 464 | status_t CameraService::dump(int fd, const Vector<String16>& args) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 465 | String8 result; |
| 466 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
Eino-Ville Talvala | 611f619 | 2012-05-31 12:28:23 -0700 | [diff] [blame] | 467 | result.appendFormat("Permission Denial: " |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 468 | "can't dump CameraService from pid=%d, uid=%d\n", |
| 469 | getCallingPid(), |
| 470 | getCallingUid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 471 | write(fd, result.string(), result.size()); |
| 472 | } else { |
| 473 | bool locked = tryLock(mServiceLock); |
| 474 | // failed to lock - CameraService is probably deadlocked |
| 475 | if (!locked) { |
Eino-Ville Talvala | 611f619 | 2012-05-31 12:28:23 -0700 | [diff] [blame] | 476 | result.append("CameraService may be deadlocked\n"); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 477 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | bool hasClient = false; |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 481 | if (!mModule) { |
| 482 | result = String8::format("No camera module available!\n"); |
| 483 | write(fd, result.string(), result.size()); |
| 484 | return NO_ERROR; |
| 485 | } |
| 486 | |
| 487 | result = String8::format("Camera module HAL API version: 0x%x\n", |
| 488 | mModule->common.hal_api_version); |
| 489 | result.appendFormat("Camera module API version: 0x%x\n", |
| 490 | mModule->common.module_api_version); |
| 491 | result.appendFormat("Camera module name: %s\n", |
| 492 | mModule->common.name); |
| 493 | result.appendFormat("Camera module author: %s\n", |
| 494 | mModule->common.author); |
| 495 | result.appendFormat("Number of camera devices: %d\n\n", mNumberOfCameras); |
| 496 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 497 | for (int i = 0; i < mNumberOfCameras; i++) { |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 498 | result = String8::format("Camera %d static information:\n", i); |
| 499 | camera_info info; |
| 500 | |
| 501 | status_t rc = mModule->get_camera_info(i, &info); |
| 502 | if (rc != OK) { |
| 503 | result.appendFormat(" Error reading static information!\n"); |
| 504 | write(fd, result.string(), result.size()); |
| 505 | } else { |
| 506 | result.appendFormat(" Facing: %s\n", |
| 507 | info.facing == CAMERA_FACING_BACK ? "BACK" : "FRONT"); |
| 508 | result.appendFormat(" Orientation: %d\n", info.orientation); |
| 509 | int deviceVersion; |
| 510 | if (mModule->common.module_api_version < |
| 511 | CAMERA_MODULE_API_VERSION_2_0) { |
| 512 | deviceVersion = CAMERA_DEVICE_API_VERSION_1_0; |
| 513 | } else { |
| 514 | deviceVersion = info.device_version; |
| 515 | } |
| 516 | result.appendFormat(" Device version: 0x%x\n", deviceVersion); |
| 517 | if (deviceVersion >= CAMERA_DEVICE_API_VERSION_2_0) { |
| 518 | result.appendFormat(" Device static metadata:\n"); |
| 519 | write(fd, result.string(), result.size()); |
Eino-Ville Talvala | 428b77a | 2012-07-30 09:55:30 -0700 | [diff] [blame] | 520 | dump_indented_camera_metadata(info.static_camera_characteristics, |
| 521 | fd, 2, 4); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 522 | } else { |
| 523 | write(fd, result.string(), result.size()); |
| 524 | } |
| 525 | } |
| 526 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 527 | sp<Client> client = mClient[i].promote(); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 528 | if (client == 0) { |
| 529 | result = String8::format(" Device is closed, no client instance\n"); |
| 530 | write(fd, result.string(), result.size()); |
| 531 | continue; |
| 532 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 533 | hasClient = true; |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 534 | result = String8::format(" Device is open. Client instance dump:\n"); |
| 535 | write(fd, result.string(), result.size()); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 536 | client->dump(fd, args); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 537 | } |
| 538 | if (!hasClient) { |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 539 | result = String8::format("\nNo active camera clients yet.\n"); |
| 540 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | if (locked) mServiceLock.unlock(); |
| 544 | |
| 545 | // change logging level |
| 546 | int n = args.size(); |
| 547 | for (int i = 0; i + 1 < n; i++) { |
Eino-Ville Talvala | 611f619 | 2012-05-31 12:28:23 -0700 | [diff] [blame] | 548 | String16 verboseOption("-v"); |
| 549 | if (args[i] == verboseOption) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 550 | String8 levelStr(args[i+1]); |
| 551 | int level = atoi(levelStr.string()); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 552 | result = String8::format("\nSetting log level to %d.\n", level); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 553 | setLogLevel(level); |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 554 | write(fd, result.string(), result.size()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 555 | } |
| 556 | } |
Eino-Ville Talvala | f592613 | 2012-07-17 13:54:20 -0700 | [diff] [blame] | 557 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 558 | } |
| 559 | return NO_ERROR; |
| 560 | } |
| 561 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 562 | /*virtual*/void CameraService::binderDied( |
| 563 | const wp<IBinder> &who) { |
| 564 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 565 | /** |
| 566 | * While tempting to promote the wp<IBinder> into a sp, |
| 567 | * it's actually not supported by the binder driver |
| 568 | */ |
| 569 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 570 | ALOGV("java clients' binder died"); |
| 571 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 572 | sp<Client> cameraClient = getClientByRemote(who); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 573 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 574 | if (cameraClient == 0) { |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 575 | ALOGV("java clients' binder death already cleaned up (normal case)"); |
| 576 | return; |
| 577 | } |
| 578 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 579 | ALOGW("Disconnecting camera client %p since the binder for it " |
| 580 | "died (this pid %d)", cameraClient.get(), getCallingPid()); |
| 581 | |
| 582 | cameraClient->disconnect(); |
| 583 | |
| 584 | } |
| 585 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 586 | }; // namespace android |