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" |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 41 | #include "CameraHardwareInterface.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. |
| 48 | static volatile int32_t gLogLevel = 0; |
| 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 { |
| 90 | mNumberOfCameras = mModule->get_number_of_cameras(); |
| 91 | if (mNumberOfCameras > MAX_CAMERAS) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 92 | ALOGE("Number of cameras(%d) > MAX_CAMERAS(%d).", |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 93 | mNumberOfCameras, MAX_CAMERAS); |
| 94 | mNumberOfCameras = MAX_CAMERAS; |
| 95 | } |
| 96 | for (int i = 0; i < mNumberOfCameras; i++) { |
| 97 | setCameraFree(i); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 102 | CameraService::~CameraService() { |
| 103 | for (int i = 0; i < mNumberOfCameras; i++) { |
| 104 | if (mBusy[i]) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 105 | ALOGE("camera %d is still in use in destructor!", i); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | gCameraService = NULL; |
| 110 | } |
| 111 | |
| 112 | int32_t CameraService::getNumberOfCameras() { |
| 113 | return mNumberOfCameras; |
| 114 | } |
| 115 | |
| 116 | status_t CameraService::getCameraInfo(int cameraId, |
| 117 | struct CameraInfo* cameraInfo) { |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 118 | if (!mModule) { |
| 119 | return NO_INIT; |
| 120 | } |
| 121 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 122 | if (cameraId < 0 || cameraId >= mNumberOfCameras) { |
| 123 | return BAD_VALUE; |
| 124 | } |
| 125 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 126 | struct camera_info info; |
| 127 | status_t rc = mModule->get_camera_info(cameraId, &info); |
| 128 | cameraInfo->facing = info.facing; |
| 129 | cameraInfo->orientation = info.orientation; |
| 130 | return rc; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | sp<ICamera> CameraService::connect( |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 134 | const sp<ICameraClient>& cameraClient, int cameraId) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 135 | int callingPid = getCallingPid(); |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 136 | sp<CameraHardwareInterface> hardware = NULL; |
| 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 | |
| 189 | char camera_device_name[10]; |
| 190 | snprintf(camera_device_name, sizeof(camera_device_name), "%d", cameraId); |
| 191 | |
Tyler Luu | 5861a9a | 2011-10-06 00:00:03 -0500 | [diff] [blame] | 192 | hardware = new CameraHardwareInterface(camera_device_name); |
| 193 | if (hardware->initialize(&mModule->common) != OK) { |
| 194 | hardware.clear(); |
| 195 | return NULL; |
| 196 | } |
| 197 | |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 198 | client = new Client(this, cameraClient, hardware, cameraId, info.facing, callingPid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 199 | mClient[cameraId] = client; |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 200 | LOG1("CameraService::connect X (id %d)", cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 201 | return client; |
| 202 | } |
| 203 | |
| 204 | void CameraService::removeClient(const sp<ICameraClient>& cameraClient) { |
| 205 | int callingPid = getCallingPid(); |
| 206 | LOG1("CameraService::removeClient E (pid %d)", callingPid); |
| 207 | |
| 208 | for (int i = 0; i < mNumberOfCameras; i++) { |
| 209 | // Declare this before the lock to make absolutely sure the |
| 210 | // destructor won't be called with the lock held. |
| 211 | sp<Client> client; |
| 212 | |
| 213 | Mutex::Autolock lock(mServiceLock); |
| 214 | |
| 215 | // This happens when we have already disconnected (or this is |
| 216 | // just another unused camera). |
| 217 | if (mClient[i] == 0) continue; |
| 218 | |
| 219 | // Promote mClient. It can fail if we are called from this path: |
| 220 | // Client::~Client() -> disconnect() -> removeClient(). |
| 221 | client = mClient[i].promote(); |
| 222 | |
| 223 | if (client == 0) { |
| 224 | mClient[i].clear(); |
| 225 | continue; |
| 226 | } |
| 227 | |
| 228 | if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) { |
| 229 | // Found our camera, clear and leave. |
| 230 | LOG1("removeClient: clear camera %d", i); |
| 231 | mClient[i].clear(); |
| 232 | break; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | LOG1("CameraService::removeClient X (pid %d)", callingPid); |
| 237 | } |
| 238 | |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 239 | CameraService::Client* CameraService::getClientByIdUnsafe(int cameraId) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 240 | if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL; |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 241 | return mClient[cameraId].unsafe_get(); |
| 242 | } |
| 243 | |
| 244 | Mutex* CameraService::getClientLockById(int cameraId) { |
| 245 | if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL; |
| 246 | return &mClientLock[cameraId]; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 249 | status_t CameraService::onTransact( |
| 250 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { |
| 251 | // Permission checks |
| 252 | switch (code) { |
| 253 | case BnCameraService::CONNECT: |
| 254 | const int pid = getCallingPid(); |
| 255 | const int self_pid = getpid(); |
| 256 | if (pid != self_pid) { |
| 257 | // we're called from a different process, do the real check |
| 258 | if (!checkCallingPermission( |
| 259 | String16("android.permission.CAMERA"))) { |
| 260 | const int uid = getCallingUid(); |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 261 | ALOGE("Permission Denial: " |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 262 | "can't use the camera pid=%d, uid=%d", pid, uid); |
| 263 | return PERMISSION_DENIED; |
| 264 | } |
| 265 | } |
| 266 | break; |
| 267 | } |
| 268 | |
| 269 | return BnCameraService::onTransact(code, data, reply, flags); |
| 270 | } |
| 271 | |
| 272 | // The reason we need this busy bit is a new CameraService::connect() request |
| 273 | // may come in while the previous Client's destructor has not been run or is |
| 274 | // still running. If the last strong reference of the previous Client is gone |
| 275 | // but the destructor has not been finished, we should not allow the new Client |
| 276 | // to be created because we need to wait for the previous Client to tear down |
| 277 | // the hardware first. |
| 278 | void CameraService::setCameraBusy(int cameraId) { |
| 279 | android_atomic_write(1, &mBusy[cameraId]); |
| 280 | } |
| 281 | |
| 282 | void CameraService::setCameraFree(int cameraId) { |
| 283 | android_atomic_write(0, &mBusy[cameraId]); |
| 284 | } |
| 285 | |
| 286 | // We share the media players for shutter and recording sound for all clients. |
| 287 | // A reference count is kept to determine when we will actually release the |
| 288 | // media players. |
| 289 | |
Chih-Chung Chang | ff4f55c | 2011-10-17 19:03:12 +0800 | [diff] [blame] | 290 | MediaPlayer* CameraService::newMediaPlayer(const char *file) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 291 | MediaPlayer* mp = new MediaPlayer(); |
| 292 | if (mp->setDataSource(file, NULL) == NO_ERROR) { |
Eino-Ville Talvala | 60a78ac | 2012-01-05 15:34:53 -0800 | [diff] [blame] | 293 | mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 294 | mp->prepare(); |
| 295 | } else { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 296 | ALOGE("Failed to load CameraService sounds: %s", file); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 297 | return NULL; |
| 298 | } |
| 299 | return mp; |
| 300 | } |
| 301 | |
| 302 | void CameraService::loadSound() { |
| 303 | Mutex::Autolock lock(mSoundLock); |
| 304 | LOG1("CameraService::loadSound ref=%d", mSoundRef); |
| 305 | if (mSoundRef++) return; |
| 306 | |
| 307 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); |
| 308 | mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); |
| 309 | } |
| 310 | |
| 311 | void CameraService::releaseSound() { |
| 312 | Mutex::Autolock lock(mSoundLock); |
| 313 | LOG1("CameraService::releaseSound ref=%d", mSoundRef); |
| 314 | if (--mSoundRef) return; |
| 315 | |
| 316 | for (int i = 0; i < NUM_SOUNDS; i++) { |
| 317 | if (mSoundPlayer[i] != 0) { |
| 318 | mSoundPlayer[i]->disconnect(); |
| 319 | mSoundPlayer[i].clear(); |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | void CameraService::playSound(sound_kind kind) { |
| 325 | LOG1("playSound(%d)", kind); |
| 326 | Mutex::Autolock lock(mSoundLock); |
| 327 | sp<MediaPlayer> player = mSoundPlayer[kind]; |
| 328 | if (player != 0) { |
Chih-Chung Chang | 8888a75 | 2011-10-20 10:47:26 +0800 | [diff] [blame] | 329 | player->seekTo(0); |
| 330 | player->start(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
| 334 | // ---------------------------------------------------------------------------- |
| 335 | |
| 336 | CameraService::Client::Client(const sp<CameraService>& cameraService, |
Wu-cheng Li | b7a6794 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 337 | const sp<ICameraClient>& cameraClient, |
| 338 | const sp<CameraHardwareInterface>& hardware, |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 339 | int cameraId, int cameraFacing, int clientPid) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 340 | int callingPid = getCallingPid(); |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 341 | LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 342 | |
| 343 | mCameraService = cameraService; |
| 344 | mCameraClient = cameraClient; |
Wu-cheng Li | b7a6794 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 345 | mHardware = hardware; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 346 | mCameraId = cameraId; |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 347 | mCameraFacing = cameraFacing; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 348 | mClientPid = clientPid; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 349 | mMsgEnabled = 0; |
Jamie Gennis | bfa33aa | 2010-12-20 11:51:31 -0800 | [diff] [blame] | 350 | mSurface = 0; |
| 351 | mPreviewWindow = 0; |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 352 | mDestructionStarted = false; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 353 | mHardware->setCallbacks(notifyCallback, |
| 354 | dataCallback, |
| 355 | dataCallbackTimestamp, |
| 356 | (void *)cameraId); |
| 357 | |
Wu-cheng Li | 57c8618 | 2011-07-30 05:00:37 +0800 | [diff] [blame] | 358 | // Enable zoom, error, focus, and metadata messages by default |
| 359 | enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS | |
Wu-cheng Li | d620506 | 2011-11-14 20:30:14 +0800 | [diff] [blame] | 360 | CAMERA_MSG_PREVIEW_METADATA | CAMERA_MSG_FOCUS_MOVE); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 361 | |
| 362 | // Callback is disabled by default |
Iliyan Malchev | 9e62652 | 2011-04-14 16:51:21 -0700 | [diff] [blame] | 363 | mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP; |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 364 | mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT); |
Nipun Kwatra | b5ca461 | 2010-09-11 19:31:10 -0700 | [diff] [blame] | 365 | mPlayShutterSound = true; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 366 | cameraService->setCameraBusy(cameraId); |
| 367 | cameraService->loadSound(); |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 368 | LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 371 | // tear down the client |
| 372 | CameraService::Client::~Client() { |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 373 | // this lock should never be NULL |
| 374 | Mutex* lock = mCameraService->getClientLockById(mCameraId); |
| 375 | lock->lock(); |
| 376 | mDestructionStarted = true; |
| 377 | // client will not be accessed from callback. should unlock to prevent dead-lock in disconnect |
| 378 | lock->unlock(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 379 | int callingPid = getCallingPid(); |
| 380 | LOG1("Client::~Client E (pid %d, this %p)", callingPid, this); |
| 381 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 382 | // set mClientPid to let disconnet() tear down the hardware |
| 383 | mClientPid = callingPid; |
| 384 | disconnect(); |
| 385 | mCameraService->releaseSound(); |
| 386 | LOG1("Client::~Client X (pid %d, this %p)", callingPid, this); |
| 387 | } |
| 388 | |
| 389 | // ---------------------------------------------------------------------------- |
| 390 | |
| 391 | status_t CameraService::Client::checkPid() const { |
| 392 | int callingPid = getCallingPid(); |
| 393 | if (callingPid == mClientPid) return NO_ERROR; |
| 394 | |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 395 | ALOGW("attempt to use a locked camera from a different process" |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 396 | " (old pid %d, new pid %d)", mClientPid, callingPid); |
| 397 | return EBUSY; |
| 398 | } |
| 399 | |
| 400 | status_t CameraService::Client::checkPidAndHardware() const { |
| 401 | status_t result = checkPid(); |
| 402 | if (result != NO_ERROR) return result; |
| 403 | if (mHardware == 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 404 | ALOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid()); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 405 | return INVALID_OPERATION; |
| 406 | } |
| 407 | return NO_ERROR; |
| 408 | } |
| 409 | |
| 410 | status_t CameraService::Client::lock() { |
| 411 | int callingPid = getCallingPid(); |
| 412 | LOG1("lock (pid %d)", callingPid); |
| 413 | Mutex::Autolock lock(mLock); |
| 414 | |
| 415 | // lock camera to this client if the the camera is unlocked |
| 416 | if (mClientPid == 0) { |
| 417 | mClientPid = callingPid; |
| 418 | return NO_ERROR; |
| 419 | } |
| 420 | |
| 421 | // returns NO_ERROR if the client already owns the camera, EBUSY otherwise |
| 422 | return checkPid(); |
| 423 | } |
| 424 | |
| 425 | status_t CameraService::Client::unlock() { |
| 426 | int callingPid = getCallingPid(); |
| 427 | LOG1("unlock (pid %d)", callingPid); |
| 428 | Mutex::Autolock lock(mLock); |
| 429 | |
| 430 | // allow anyone to use camera (after they lock the camera) |
| 431 | status_t result = checkPid(); |
| 432 | if (result == NO_ERROR) { |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 433 | if (mHardware->recordingEnabled()) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 434 | ALOGE("Not allowed to unlock camera during recording."); |
Wu-cheng Li | 4ca2c7c | 2011-06-01 17:22:24 +0800 | [diff] [blame] | 435 | return INVALID_OPERATION; |
| 436 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 437 | mClientPid = 0; |
| 438 | LOG1("clear mCameraClient (pid %d)", callingPid); |
| 439 | // we need to remove the reference to ICameraClient so that when the app |
| 440 | // goes away, the reference count goes to 0. |
| 441 | mCameraClient.clear(); |
| 442 | } |
| 443 | return result; |
| 444 | } |
| 445 | |
| 446 | // connect a new client to the camera |
| 447 | status_t CameraService::Client::connect(const sp<ICameraClient>& client) { |
| 448 | int callingPid = getCallingPid(); |
| 449 | LOG1("connect E (pid %d)", callingPid); |
| 450 | Mutex::Autolock lock(mLock); |
| 451 | |
| 452 | if (mClientPid != 0 && checkPid() != NO_ERROR) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 453 | ALOGW("Tried to connect to a locked camera (old pid %d, new pid %d)", |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 454 | mClientPid, callingPid); |
| 455 | return EBUSY; |
| 456 | } |
| 457 | |
| 458 | if (mCameraClient != 0 && (client->asBinder() == mCameraClient->asBinder())) { |
| 459 | LOG1("Connect to the same client"); |
| 460 | return NO_ERROR; |
| 461 | } |
| 462 | |
Iliyan Malchev | 9e62652 | 2011-04-14 16:51:21 -0700 | [diff] [blame] | 463 | mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 464 | mClientPid = callingPid; |
| 465 | mCameraClient = client; |
| 466 | |
| 467 | LOG1("connect X (pid %d)", callingPid); |
| 468 | return NO_ERROR; |
| 469 | } |
| 470 | |
Wu-cheng Li | 7574da5 | 2011-07-20 05:35:02 +0800 | [diff] [blame] | 471 | static void disconnectWindow(const sp<ANativeWindow>& window) { |
| 472 | if (window != 0) { |
Mathias Agopian | c3da343 | 2011-07-29 17:55:48 -0700 | [diff] [blame] | 473 | status_t result = native_window_api_disconnect(window.get(), |
Wu-cheng Li | 7574da5 | 2011-07-20 05:35:02 +0800 | [diff] [blame] | 474 | NATIVE_WINDOW_API_CAMERA); |
| 475 | if (result != NO_ERROR) { |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 476 | ALOGW("native_window_api_disconnect failed: %s (%d)", strerror(-result), |
Wu-cheng Li | 7574da5 | 2011-07-20 05:35:02 +0800 | [diff] [blame] | 477 | result); |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 482 | void CameraService::Client::disconnect() { |
| 483 | int callingPid = getCallingPid(); |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 484 | LOG1("disconnect E (pid %d)", callingPid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 485 | Mutex::Autolock lock(mLock); |
| 486 | |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 487 | if (checkPid() != NO_ERROR) { |
| 488 | ALOGW("different client - don't disconnect"); |
| 489 | return; |
| 490 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 491 | |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 492 | if (mClientPid <= 0) { |
| 493 | LOG1("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid); |
| 494 | return; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | // Make sure disconnect() is done once and once only, whether it is called |
| 498 | // from the user directly, or called by the destructor. |
| 499 | if (mHardware == 0) return; |
| 500 | |
| 501 | LOG1("hardware teardown"); |
| 502 | // Before destroying mHardware, we must make sure it's in the |
| 503 | // idle state. |
| 504 | // Turn off all messages. |
| 505 | disableMsgType(CAMERA_MSG_ALL_MSGS); |
| 506 | mHardware->stopPreview(); |
| 507 | mHardware->cancelPicture(); |
| 508 | // Release the hardware resources. |
| 509 | mHardware->release(); |
Mathias Agopian | 03dfce9 | 2010-12-07 19:38:17 -0800 | [diff] [blame] | 510 | |
Jamie Gennis | 4b79168 | 2010-08-10 16:37:53 -0700 | [diff] [blame] | 511 | // Release the held ANativeWindow resources. |
| 512 | if (mPreviewWindow != 0) { |
Wu-cheng Li | 7574da5 | 2011-07-20 05:35:02 +0800 | [diff] [blame] | 513 | disconnectWindow(mPreviewWindow); |
Jamie Gennis | 4b79168 | 2010-08-10 16:37:53 -0700 | [diff] [blame] | 514 | mPreviewWindow = 0; |
| 515 | mHardware->setPreviewWindow(mPreviewWindow); |
| 516 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 517 | mHardware.clear(); |
| 518 | |
| 519 | mCameraService->removeClient(mCameraClient); |
| 520 | mCameraService->setCameraFree(mCameraId); |
| 521 | |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 522 | LOG1("disconnect X (pid %d)", callingPid); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | // ---------------------------------------------------------------------------- |
| 526 | |
Jamie Gennis | 0ed3ec0 | 2011-07-13 15:13:14 -0700 | [diff] [blame] | 527 | status_t CameraService::Client::setPreviewWindow(const sp<IBinder>& binder, |
| 528 | const sp<ANativeWindow>& window) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 529 | Mutex::Autolock lock(mLock); |
| 530 | status_t result = checkPidAndHardware(); |
| 531 | if (result != NO_ERROR) return result; |
| 532 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 533 | // return if no change in surface. |
Mathias Agopian | 8b1027d | 2011-04-05 15:44:20 -0700 | [diff] [blame] | 534 | if (binder == mSurface) { |
Jamie Gennis | 0ed3ec0 | 2011-07-13 15:13:14 -0700 | [diff] [blame] | 535 | return NO_ERROR; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Jamie Gennis | 0ed3ec0 | 2011-07-13 15:13:14 -0700 | [diff] [blame] | 538 | if (window != 0) { |
Mathias Agopian | c3da343 | 2011-07-29 17:55:48 -0700 | [diff] [blame] | 539 | result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA); |
Jamie Gennis | 0ed3ec0 | 2011-07-13 15:13:14 -0700 | [diff] [blame] | 540 | if (result != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 541 | ALOGE("native_window_api_connect failed: %s (%d)", strerror(-result), |
Jamie Gennis | 0ed3ec0 | 2011-07-13 15:13:14 -0700 | [diff] [blame] | 542 | result); |
| 543 | return result; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
Jamie Gennis | 0ed3ec0 | 2011-07-13 15:13:14 -0700 | [diff] [blame] | 547 | // If preview has been already started, register preview buffers now. |
| 548 | if (mHardware->previewEnabled()) { |
| 549 | if (window != 0) { |
Mathias Agopian | 9bc7af1 | 2011-07-18 16:15:08 -0700 | [diff] [blame] | 550 | native_window_set_scaling_mode(window.get(), |
| 551 | NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); |
Jamie Gennis | 0ed3ec0 | 2011-07-13 15:13:14 -0700 | [diff] [blame] | 552 | native_window_set_buffers_transform(window.get(), mOrientation); |
| 553 | result = mHardware->setPreviewWindow(window); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | if (result == NO_ERROR) { |
| 558 | // Everything has succeeded. Disconnect the old window and remember the |
| 559 | // new window. |
| 560 | disconnectWindow(mPreviewWindow); |
| 561 | mSurface = binder; |
| 562 | mPreviewWindow = window; |
| 563 | } else { |
| 564 | // Something went wrong after we connected to the new window, so |
| 565 | // disconnect here. |
| 566 | disconnectWindow(window); |
| 567 | } |
| 568 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 569 | return result; |
| 570 | } |
| 571 | |
Jamie Gennis | 0ed3ec0 | 2011-07-13 15:13:14 -0700 | [diff] [blame] | 572 | // set the Surface that the preview will use |
| 573 | status_t CameraService::Client::setPreviewDisplay(const sp<Surface>& surface) { |
| 574 | LOG1("setPreviewDisplay(%p) (pid %d)", surface.get(), getCallingPid()); |
| 575 | |
| 576 | sp<IBinder> binder(surface != 0 ? surface->asBinder() : 0); |
| 577 | sp<ANativeWindow> window(surface); |
| 578 | return setPreviewWindow(binder, window); |
| 579 | } |
| 580 | |
Jamie Gennis | bfa33aa | 2010-12-20 11:51:31 -0800 | [diff] [blame] | 581 | // set the SurfaceTexture that the preview will use |
| 582 | status_t CameraService::Client::setPreviewTexture( |
| 583 | const sp<ISurfaceTexture>& surfaceTexture) { |
| 584 | LOG1("setPreviewTexture(%p) (pid %d)", surfaceTexture.get(), |
| 585 | getCallingPid()); |
Jamie Gennis | bfa33aa | 2010-12-20 11:51:31 -0800 | [diff] [blame] | 586 | |
Jamie Gennis | 0ed3ec0 | 2011-07-13 15:13:14 -0700 | [diff] [blame] | 587 | sp<IBinder> binder; |
| 588 | sp<ANativeWindow> window; |
Jamie Gennis | bfa33aa | 2010-12-20 11:51:31 -0800 | [diff] [blame] | 589 | if (surfaceTexture != 0) { |
Jamie Gennis | 0ed3ec0 | 2011-07-13 15:13:14 -0700 | [diff] [blame] | 590 | binder = surfaceTexture->asBinder(); |
| 591 | window = new SurfaceTextureClient(surfaceTexture); |
Jamie Gennis | bfa33aa | 2010-12-20 11:51:31 -0800 | [diff] [blame] | 592 | } |
Jamie Gennis | 0ed3ec0 | 2011-07-13 15:13:14 -0700 | [diff] [blame] | 593 | return setPreviewWindow(binder, window); |
Jamie Gennis | bfa33aa | 2010-12-20 11:51:31 -0800 | [diff] [blame] | 594 | } |
| 595 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 596 | // set the preview callback flag to affect how the received frames from |
| 597 | // preview are handled. |
| 598 | void CameraService::Client::setPreviewCallbackFlag(int callback_flag) { |
| 599 | LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid()); |
| 600 | Mutex::Autolock lock(mLock); |
| 601 | if (checkPidAndHardware() != NO_ERROR) return; |
| 602 | |
| 603 | mPreviewCallbackFlag = callback_flag; |
Iliyan Malchev | 9e62652 | 2011-04-14 16:51:21 -0700 | [diff] [blame] | 604 | if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) { |
Wu-cheng Li | 0667de7 | 2010-09-03 16:40:32 -0700 | [diff] [blame] | 605 | enableMsgType(CAMERA_MSG_PREVIEW_FRAME); |
| 606 | } else { |
| 607 | disableMsgType(CAMERA_MSG_PREVIEW_FRAME); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
| 611 | // start preview mode |
| 612 | status_t CameraService::Client::startPreview() { |
| 613 | LOG1("startPreview (pid %d)", getCallingPid()); |
| 614 | return startCameraMode(CAMERA_PREVIEW_MODE); |
| 615 | } |
| 616 | |
| 617 | // start recording mode |
| 618 | status_t CameraService::Client::startRecording() { |
| 619 | LOG1("startRecording (pid %d)", getCallingPid()); |
| 620 | return startCameraMode(CAMERA_RECORDING_MODE); |
| 621 | } |
| 622 | |
| 623 | // start preview or recording |
| 624 | status_t CameraService::Client::startCameraMode(camera_mode mode) { |
| 625 | LOG1("startCameraMode(%d)", mode); |
| 626 | Mutex::Autolock lock(mLock); |
| 627 | status_t result = checkPidAndHardware(); |
| 628 | if (result != NO_ERROR) return result; |
| 629 | |
| 630 | switch(mode) { |
| 631 | case CAMERA_PREVIEW_MODE: |
Jamie Gennis | 4b79168 | 2010-08-10 16:37:53 -0700 | [diff] [blame] | 632 | if (mSurface == 0 && mPreviewWindow == 0) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 633 | LOG1("mSurface is not set yet."); |
| 634 | // still able to start preview in this case. |
| 635 | } |
| 636 | return startPreviewMode(); |
| 637 | case CAMERA_RECORDING_MODE: |
Jamie Gennis | 4b79168 | 2010-08-10 16:37:53 -0700 | [diff] [blame] | 638 | if (mSurface == 0 && mPreviewWindow == 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 639 | ALOGE("mSurface or mPreviewWindow must be set before startRecordingMode."); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 640 | return INVALID_OPERATION; |
| 641 | } |
| 642 | return startRecordingMode(); |
| 643 | default: |
| 644 | return UNKNOWN_ERROR; |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | status_t CameraService::Client::startPreviewMode() { |
| 649 | LOG1("startPreviewMode"); |
| 650 | status_t result = NO_ERROR; |
| 651 | |
| 652 | // if preview has been enabled, nothing needs to be done |
| 653 | if (mHardware->previewEnabled()) { |
| 654 | return NO_ERROR; |
| 655 | } |
| 656 | |
Mathias Agopian | 03dfce9 | 2010-12-07 19:38:17 -0800 | [diff] [blame] | 657 | if (mPreviewWindow != 0) { |
Mathias Agopian | 9bc7af1 | 2011-07-18 16:15:08 -0700 | [diff] [blame] | 658 | native_window_set_scaling_mode(mPreviewWindow.get(), |
| 659 | NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); |
Mathias Agopian | 03dfce9 | 2010-12-07 19:38:17 -0800 | [diff] [blame] | 660 | native_window_set_buffers_transform(mPreviewWindow.get(), |
| 661 | mOrientation); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 662 | } |
Mathias Agopian | 03dfce9 | 2010-12-07 19:38:17 -0800 | [diff] [blame] | 663 | mHardware->setPreviewWindow(mPreviewWindow); |
| 664 | result = mHardware->startPreview(); |
| 665 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 666 | return result; |
| 667 | } |
| 668 | |
| 669 | status_t CameraService::Client::startRecordingMode() { |
| 670 | LOG1("startRecordingMode"); |
| 671 | status_t result = NO_ERROR; |
| 672 | |
| 673 | // if recording has been enabled, nothing needs to be done |
| 674 | if (mHardware->recordingEnabled()) { |
| 675 | return NO_ERROR; |
| 676 | } |
| 677 | |
| 678 | // if preview has not been started, start preview first |
| 679 | if (!mHardware->previewEnabled()) { |
| 680 | result = startPreviewMode(); |
| 681 | if (result != NO_ERROR) { |
| 682 | return result; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | // start recording mode |
| 687 | enableMsgType(CAMERA_MSG_VIDEO_FRAME); |
| 688 | mCameraService->playSound(SOUND_RECORDING); |
| 689 | result = mHardware->startRecording(); |
| 690 | if (result != NO_ERROR) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 691 | ALOGE("mHardware->startRecording() failed with status %d", result); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 692 | } |
| 693 | return result; |
| 694 | } |
| 695 | |
| 696 | // stop preview mode |
| 697 | void CameraService::Client::stopPreview() { |
| 698 | LOG1("stopPreview (pid %d)", getCallingPid()); |
| 699 | Mutex::Autolock lock(mLock); |
| 700 | if (checkPidAndHardware() != NO_ERROR) return; |
| 701 | |
Jamie Gennis | 4b79168 | 2010-08-10 16:37:53 -0700 | [diff] [blame] | 702 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 703 | disableMsgType(CAMERA_MSG_PREVIEW_FRAME); |
| 704 | mHardware->stopPreview(); |
| 705 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 706 | mPreviewBuffer.clear(); |
| 707 | } |
| 708 | |
| 709 | // stop recording mode |
| 710 | void CameraService::Client::stopRecording() { |
| 711 | LOG1("stopRecording (pid %d)", getCallingPid()); |
| 712 | Mutex::Autolock lock(mLock); |
| 713 | if (checkPidAndHardware() != NO_ERROR) return; |
| 714 | |
| 715 | mCameraService->playSound(SOUND_RECORDING); |
| 716 | disableMsgType(CAMERA_MSG_VIDEO_FRAME); |
| 717 | mHardware->stopRecording(); |
| 718 | |
| 719 | mPreviewBuffer.clear(); |
| 720 | } |
| 721 | |
| 722 | // release a recording frame |
| 723 | void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem) { |
| 724 | Mutex::Autolock lock(mLock); |
| 725 | if (checkPidAndHardware() != NO_ERROR) return; |
| 726 | mHardware->releaseRecordingFrame(mem); |
| 727 | } |
| 728 | |
James Dong | e2ad673 | 2010-10-18 20:42:51 -0700 | [diff] [blame] | 729 | status_t CameraService::Client::storeMetaDataInBuffers(bool enabled) |
| 730 | { |
| 731 | LOG1("storeMetaDataInBuffers: %s", enabled? "true": "false"); |
| 732 | Mutex::Autolock lock(mLock); |
| 733 | if (checkPidAndHardware() != NO_ERROR) { |
| 734 | return UNKNOWN_ERROR; |
| 735 | } |
| 736 | return mHardware->storeMetaDataInBuffers(enabled); |
| 737 | } |
| 738 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 739 | bool CameraService::Client::previewEnabled() { |
| 740 | LOG1("previewEnabled (pid %d)", getCallingPid()); |
| 741 | |
| 742 | Mutex::Autolock lock(mLock); |
| 743 | if (checkPidAndHardware() != NO_ERROR) return false; |
| 744 | return mHardware->previewEnabled(); |
| 745 | } |
| 746 | |
| 747 | bool CameraService::Client::recordingEnabled() { |
| 748 | LOG1("recordingEnabled (pid %d)", getCallingPid()); |
| 749 | |
| 750 | Mutex::Autolock lock(mLock); |
| 751 | if (checkPidAndHardware() != NO_ERROR) return false; |
| 752 | return mHardware->recordingEnabled(); |
| 753 | } |
| 754 | |
| 755 | status_t CameraService::Client::autoFocus() { |
| 756 | LOG1("autoFocus (pid %d)", getCallingPid()); |
| 757 | |
| 758 | Mutex::Autolock lock(mLock); |
| 759 | status_t result = checkPidAndHardware(); |
| 760 | if (result != NO_ERROR) return result; |
| 761 | |
| 762 | return mHardware->autoFocus(); |
| 763 | } |
| 764 | |
| 765 | status_t CameraService::Client::cancelAutoFocus() { |
| 766 | LOG1("cancelAutoFocus (pid %d)", getCallingPid()); |
| 767 | |
| 768 | Mutex::Autolock lock(mLock); |
| 769 | status_t result = checkPidAndHardware(); |
| 770 | if (result != NO_ERROR) return result; |
| 771 | |
| 772 | return mHardware->cancelAutoFocus(); |
| 773 | } |
| 774 | |
| 775 | // take a picture - image is returned in callback |
James Dong | e468ac5 | 2011-02-17 16:38:06 -0800 | [diff] [blame] | 776 | status_t CameraService::Client::takePicture(int msgType) { |
| 777 | LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 778 | |
| 779 | Mutex::Autolock lock(mLock); |
| 780 | status_t result = checkPidAndHardware(); |
| 781 | if (result != NO_ERROR) return result; |
| 782 | |
James Dong | e468ac5 | 2011-02-17 16:38:06 -0800 | [diff] [blame] | 783 | if ((msgType & CAMERA_MSG_RAW_IMAGE) && |
| 784 | (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 785 | ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY" |
James Dong | e468ac5 | 2011-02-17 16:38:06 -0800 | [diff] [blame] | 786 | " cannot be both enabled"); |
| 787 | return BAD_VALUE; |
| 788 | } |
| 789 | |
| 790 | // We only accept picture related message types |
| 791 | // and ignore other types of messages for takePicture(). |
| 792 | int picMsgType = msgType |
| 793 | & (CAMERA_MSG_SHUTTER | |
| 794 | CAMERA_MSG_POSTVIEW_FRAME | |
| 795 | CAMERA_MSG_RAW_IMAGE | |
| 796 | CAMERA_MSG_RAW_IMAGE_NOTIFY | |
| 797 | CAMERA_MSG_COMPRESSED_IMAGE); |
| 798 | |
| 799 | enableMsgType(picMsgType); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 800 | |
| 801 | return mHardware->takePicture(); |
| 802 | } |
| 803 | |
| 804 | // set preview/capture parameters - key/value pairs |
| 805 | status_t CameraService::Client::setParameters(const String8& params) { |
| 806 | LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string()); |
| 807 | |
| 808 | Mutex::Autolock lock(mLock); |
| 809 | status_t result = checkPidAndHardware(); |
| 810 | if (result != NO_ERROR) return result; |
| 811 | |
| 812 | CameraParameters p(params); |
| 813 | return mHardware->setParameters(p); |
| 814 | } |
| 815 | |
| 816 | // get preview/capture parameters - key/value pairs |
| 817 | String8 CameraService::Client::getParameters() const { |
| 818 | Mutex::Autolock lock(mLock); |
| 819 | if (checkPidAndHardware() != NO_ERROR) return String8(); |
| 820 | |
| 821 | String8 params(mHardware->getParameters().flatten()); |
| 822 | LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string()); |
| 823 | return params; |
| 824 | } |
| 825 | |
Nipun Kwatra | b5ca461 | 2010-09-11 19:31:10 -0700 | [diff] [blame] | 826 | // enable shutter sound |
| 827 | status_t CameraService::Client::enableShutterSound(bool enable) { |
| 828 | LOG1("enableShutterSound (pid %d)", getCallingPid()); |
| 829 | |
| 830 | status_t result = checkPidAndHardware(); |
| 831 | if (result != NO_ERROR) return result; |
| 832 | |
| 833 | if (enable) { |
| 834 | mPlayShutterSound = true; |
| 835 | return OK; |
| 836 | } |
| 837 | |
| 838 | // Disabling shutter sound may not be allowed. In that case only |
| 839 | // allow the mediaserver process to disable the sound. |
| 840 | char value[PROPERTY_VALUE_MAX]; |
| 841 | property_get("ro.camera.sound.forced", value, "0"); |
| 842 | if (strcmp(value, "0") != 0) { |
| 843 | // Disabling shutter sound is not allowed. Deny if the current |
| 844 | // process is not mediaserver. |
| 845 | if (getCallingPid() != getpid()) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 846 | ALOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid()); |
Nipun Kwatra | b5ca461 | 2010-09-11 19:31:10 -0700 | [diff] [blame] | 847 | return PERMISSION_DENIED; |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | mPlayShutterSound = false; |
| 852 | return OK; |
| 853 | } |
| 854 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 855 | status_t CameraService::Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) { |
| 856 | LOG1("sendCommand (pid %d)", getCallingPid()); |
Wu-cheng Li | 4a73f3d | 2010-09-23 17:17:43 -0700 | [diff] [blame] | 857 | int orientation; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 858 | Mutex::Autolock lock(mLock); |
| 859 | status_t result = checkPidAndHardware(); |
| 860 | if (result != NO_ERROR) return result; |
| 861 | |
| 862 | if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) { |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 863 | // Mirror the preview if the camera is front-facing. |
| 864 | orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT); |
| 865 | if (orientation == -1) return BAD_VALUE; |
| 866 | |
Wu-cheng Li | 4a73f3d | 2010-09-23 17:17:43 -0700 | [diff] [blame] | 867 | if (mOrientation != orientation) { |
| 868 | mOrientation = orientation; |
Wu-cheng Li | b9f5886 | 2011-10-07 13:13:54 +0800 | [diff] [blame] | 869 | if (mPreviewWindow != 0) { |
| 870 | native_window_set_buffers_transform(mPreviewWindow.get(), |
| 871 | mOrientation); |
| 872 | } |
Wu-cheng Li | 4a73f3d | 2010-09-23 17:17:43 -0700 | [diff] [blame] | 873 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 874 | return OK; |
Nipun Kwatra | b5ca461 | 2010-09-11 19:31:10 -0700 | [diff] [blame] | 875 | } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) { |
| 876 | switch (arg1) { |
| 877 | case 0: |
| 878 | enableShutterSound(false); |
| 879 | break; |
| 880 | case 1: |
| 881 | enableShutterSound(true); |
| 882 | break; |
| 883 | default: |
| 884 | return BAD_VALUE; |
| 885 | } |
| 886 | return OK; |
Nipun Kwatra | 3b7b358 | 2010-09-14 16:49:08 -0700 | [diff] [blame] | 887 | } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) { |
| 888 | mCameraService->playSound(SOUND_RECORDING); |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 889 | } else if (cmd == CAMERA_CMD_PING) { |
| 890 | // If mHardware is 0, checkPidAndHardware will return error. |
| 891 | return OK; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | return mHardware->sendCommand(cmd, arg1, arg2); |
| 895 | } |
| 896 | |
| 897 | // ---------------------------------------------------------------------------- |
| 898 | |
| 899 | void CameraService::Client::enableMsgType(int32_t msgType) { |
| 900 | android_atomic_or(msgType, &mMsgEnabled); |
| 901 | mHardware->enableMsgType(msgType); |
| 902 | } |
| 903 | |
| 904 | void CameraService::Client::disableMsgType(int32_t msgType) { |
| 905 | android_atomic_and(~msgType, &mMsgEnabled); |
| 906 | mHardware->disableMsgType(msgType); |
| 907 | } |
| 908 | |
| 909 | #define CHECK_MESSAGE_INTERVAL 10 // 10ms |
| 910 | bool CameraService::Client::lockIfMessageWanted(int32_t msgType) { |
| 911 | int sleepCount = 0; |
| 912 | while (mMsgEnabled & msgType) { |
| 913 | if (mLock.tryLock() == NO_ERROR) { |
| 914 | if (sleepCount > 0) { |
| 915 | LOG1("lockIfMessageWanted(%d): waited for %d ms", |
| 916 | msgType, sleepCount * CHECK_MESSAGE_INTERVAL); |
| 917 | } |
| 918 | return true; |
| 919 | } |
| 920 | if (sleepCount++ == 0) { |
| 921 | LOG1("lockIfMessageWanted(%d): enter sleep", msgType); |
| 922 | } |
| 923 | usleep(CHECK_MESSAGE_INTERVAL * 1000); |
| 924 | } |
Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 925 | ALOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 926 | return false; |
| 927 | } |
| 928 | |
| 929 | // ---------------------------------------------------------------------------- |
| 930 | |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 931 | Mutex* CameraService::Client::getClientLockFromCookie(void* user) { |
| 932 | return gCameraService->getClientLockById((int) user); |
| 933 | } |
| 934 | |
| 935 | // Provide client pointer for callbacks. Client lock returned from getClientLockFromCookie should |
| 936 | // be acquired for this to be safe |
| 937 | CameraService::Client* CameraService::Client::getClientFromCookie(void* user) { |
| 938 | Client* client = gCameraService->getClientByIdUnsafe((int) user); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 939 | |
| 940 | // This could happen if the Client is in the process of shutting down (the |
| 941 | // last strong reference is gone, but the destructor hasn't finished |
| 942 | // stopping the hardware). |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 943 | if (client == NULL) return NULL; |
| 944 | |
| 945 | // destruction already started, so should not be accessed |
| 946 | if (client->mDestructionStarted) return NULL; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 947 | |
| 948 | // The checks below are not necessary and are for debugging only. |
| 949 | if (client->mCameraService.get() != gCameraService) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 950 | ALOGE("mismatch service!"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 951 | return NULL; |
| 952 | } |
| 953 | |
| 954 | if (client->mHardware == 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 955 | ALOGE("mHardware == 0: callback after disconnect()?"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 956 | return NULL; |
| 957 | } |
| 958 | |
| 959 | return client; |
| 960 | } |
| 961 | |
| 962 | // Callback messages can be dispatched to internal handlers or pass to our |
| 963 | // client's callback functions, depending on the message type. |
| 964 | // |
| 965 | // notifyCallback: |
| 966 | // CAMERA_MSG_SHUTTER handleShutter |
| 967 | // (others) c->notifyCallback |
| 968 | // dataCallback: |
| 969 | // CAMERA_MSG_PREVIEW_FRAME handlePreviewData |
| 970 | // CAMERA_MSG_POSTVIEW_FRAME handlePostview |
| 971 | // CAMERA_MSG_RAW_IMAGE handleRawPicture |
| 972 | // CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture |
| 973 | // (others) c->dataCallback |
| 974 | // dataCallbackTimestamp |
| 975 | // (others) c->dataCallbackTimestamp |
| 976 | // |
| 977 | // NOTE: the *Callback functions grab mLock of the client before passing |
| 978 | // control to handle* functions. So the handle* functions must release the |
| 979 | // lock before calling the ICameraClient's callbacks, so those callbacks can |
| 980 | // invoke methods in the Client class again (For example, the preview frame |
| 981 | // callback may want to releaseRecordingFrame). The handle* functions must |
| 982 | // release the lock after all accesses to member variables, so it must be |
| 983 | // handled very carefully. |
| 984 | |
| 985 | void CameraService::Client::notifyCallback(int32_t msgType, int32_t ext1, |
| 986 | int32_t ext2, void* user) { |
| 987 | LOG2("notifyCallback(%d)", msgType); |
| 988 | |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 989 | Mutex* lock = getClientLockFromCookie(user); |
| 990 | if (lock == NULL) return; |
| 991 | Mutex::Autolock alock(*lock); |
| 992 | |
| 993 | Client* client = getClientFromCookie(user); |
| 994 | if (client == NULL) return; |
| 995 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 996 | if (!client->lockIfMessageWanted(msgType)) return; |
| 997 | |
| 998 | switch (msgType) { |
| 999 | case CAMERA_MSG_SHUTTER: |
| 1000 | // ext1 is the dimension of the yuv picture. |
Iliyan Malchev | 108dddf | 2011-03-28 16:10:12 -0700 | [diff] [blame] | 1001 | client->handleShutter(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1002 | break; |
| 1003 | default: |
| 1004 | client->handleGenericNotify(msgType, ext1, ext2); |
| 1005 | break; |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | void CameraService::Client::dataCallback(int32_t msgType, |
Wu-cheng Li | ff09ef8 | 2011-07-28 05:30:59 +0800 | [diff] [blame] | 1010 | const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1011 | LOG2("dataCallback(%d)", msgType); |
| 1012 | |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 1013 | Mutex* lock = getClientLockFromCookie(user); |
| 1014 | if (lock == NULL) return; |
| 1015 | Mutex::Autolock alock(*lock); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1016 | |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 1017 | Client* client = getClientFromCookie(user); |
| 1018 | if (client == NULL) return; |
| 1019 | |
| 1020 | if (!client->lockIfMessageWanted(msgType)) return; |
Wu-cheng Li | 57c8618 | 2011-07-30 05:00:37 +0800 | [diff] [blame] | 1021 | if (dataPtr == 0 && metadata == NULL) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1022 | ALOGE("Null data returned in data callback"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1023 | client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0); |
| 1024 | return; |
| 1025 | } |
| 1026 | |
Wu-cheng Li | 57c8618 | 2011-07-30 05:00:37 +0800 | [diff] [blame] | 1027 | switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1028 | case CAMERA_MSG_PREVIEW_FRAME: |
Wu-cheng Li | 57c8618 | 2011-07-30 05:00:37 +0800 | [diff] [blame] | 1029 | client->handlePreviewData(msgType, dataPtr, metadata); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1030 | break; |
| 1031 | case CAMERA_MSG_POSTVIEW_FRAME: |
| 1032 | client->handlePostview(dataPtr); |
| 1033 | break; |
| 1034 | case CAMERA_MSG_RAW_IMAGE: |
| 1035 | client->handleRawPicture(dataPtr); |
| 1036 | break; |
| 1037 | case CAMERA_MSG_COMPRESSED_IMAGE: |
| 1038 | client->handleCompressedPicture(dataPtr); |
| 1039 | break; |
| 1040 | default: |
Wu-cheng Li | 57c8618 | 2011-07-30 05:00:37 +0800 | [diff] [blame] | 1041 | client->handleGenericData(msgType, dataPtr, metadata); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1042 | break; |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | void CameraService::Client::dataCallbackTimestamp(nsecs_t timestamp, |
| 1047 | int32_t msgType, const sp<IMemory>& dataPtr, void* user) { |
| 1048 | LOG2("dataCallbackTimestamp(%d)", msgType); |
| 1049 | |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 1050 | Mutex* lock = getClientLockFromCookie(user); |
| 1051 | if (lock == NULL) return; |
| 1052 | Mutex::Autolock alock(*lock); |
| 1053 | |
| 1054 | Client* client = getClientFromCookie(user); |
| 1055 | if (client == NULL) return; |
| 1056 | |
James Dong | 986ef2a | 2010-12-09 11:08:14 -0800 | [diff] [blame] | 1057 | if (!client->lockIfMessageWanted(msgType)) return; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1058 | |
| 1059 | if (dataPtr == 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1060 | ALOGE("Null data returned in data with timestamp callback"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1061 | client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0); |
| 1062 | return; |
| 1063 | } |
| 1064 | |
| 1065 | client->handleGenericDataTimestamp(timestamp, msgType, dataPtr); |
| 1066 | } |
| 1067 | |
| 1068 | // snapshot taken callback |
Iliyan Malchev | 108dddf | 2011-03-28 16:10:12 -0700 | [diff] [blame] | 1069 | void CameraService::Client::handleShutter(void) { |
Nipun Kwatra | b5ca461 | 2010-09-11 19:31:10 -0700 | [diff] [blame] | 1070 | if (mPlayShutterSound) { |
| 1071 | mCameraService->playSound(SOUND_SHUTTER); |
| 1072 | } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1073 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1074 | sp<ICameraClient> c = mCameraClient; |
| 1075 | if (c != 0) { |
| 1076 | mLock.unlock(); |
| 1077 | c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0); |
| 1078 | if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return; |
| 1079 | } |
| 1080 | disableMsgType(CAMERA_MSG_SHUTTER); |
| 1081 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1082 | mLock.unlock(); |
| 1083 | } |
| 1084 | |
| 1085 | // preview callback - frame buffer update |
Wu-cheng Li | 57c8618 | 2011-07-30 05:00:37 +0800 | [diff] [blame] | 1086 | void CameraService::Client::handlePreviewData(int32_t msgType, |
| 1087 | const sp<IMemory>& mem, |
| 1088 | camera_frame_metadata_t *metadata) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1089 | ssize_t offset; |
| 1090 | size_t size; |
| 1091 | sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); |
| 1092 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1093 | // local copy of the callback flags |
| 1094 | int flags = mPreviewCallbackFlag; |
| 1095 | |
| 1096 | // is callback enabled? |
Iliyan Malchev | 9e62652 | 2011-04-14 16:51:21 -0700 | [diff] [blame] | 1097 | if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1098 | // If the enable bit is off, the copy-out and one-shot bits are ignored |
| 1099 | LOG2("frame callback is disabled"); |
| 1100 | mLock.unlock(); |
| 1101 | return; |
| 1102 | } |
| 1103 | |
| 1104 | // hold a strong pointer to the client |
| 1105 | sp<ICameraClient> c = mCameraClient; |
| 1106 | |
| 1107 | // clear callback flags if no client or one-shot mode |
Iliyan Malchev | 9e62652 | 2011-04-14 16:51:21 -0700 | [diff] [blame] | 1108 | if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1109 | LOG2("Disable preview callback"); |
Iliyan Malchev | 9e62652 | 2011-04-14 16:51:21 -0700 | [diff] [blame] | 1110 | mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK | |
| 1111 | CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK | |
| 1112 | CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK); |
Wu-cheng Li | 0667de7 | 2010-09-03 16:40:32 -0700 | [diff] [blame] | 1113 | disableMsgType(CAMERA_MSG_PREVIEW_FRAME); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | if (c != 0) { |
| 1117 | // Is the received frame copied out or not? |
Iliyan Malchev | 9e62652 | 2011-04-14 16:51:21 -0700 | [diff] [blame] | 1118 | if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1119 | LOG2("frame is copied"); |
Wu-cheng Li | 57c8618 | 2011-07-30 05:00:37 +0800 | [diff] [blame] | 1120 | copyFrameAndPostCopiedFrame(msgType, c, heap, offset, size, metadata); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1121 | } else { |
| 1122 | LOG2("frame is forwarded"); |
| 1123 | mLock.unlock(); |
Wu-cheng Li | 57c8618 | 2011-07-30 05:00:37 +0800 | [diff] [blame] | 1124 | c->dataCallback(msgType, mem, metadata); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1125 | } |
| 1126 | } else { |
| 1127 | mLock.unlock(); |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | // picture callback - postview image ready |
| 1132 | void CameraService::Client::handlePostview(const sp<IMemory>& mem) { |
| 1133 | disableMsgType(CAMERA_MSG_POSTVIEW_FRAME); |
| 1134 | |
| 1135 | sp<ICameraClient> c = mCameraClient; |
| 1136 | mLock.unlock(); |
| 1137 | if (c != 0) { |
Wu-cheng Li | 57c8618 | 2011-07-30 05:00:37 +0800 | [diff] [blame] | 1138 | c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | // picture callback - raw image ready |
| 1143 | void CameraService::Client::handleRawPicture(const sp<IMemory>& mem) { |
| 1144 | disableMsgType(CAMERA_MSG_RAW_IMAGE); |
| 1145 | |
| 1146 | ssize_t offset; |
| 1147 | size_t size; |
| 1148 | sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); |
| 1149 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1150 | sp<ICameraClient> c = mCameraClient; |
| 1151 | mLock.unlock(); |
| 1152 | if (c != 0) { |
Wu-cheng Li | 57c8618 | 2011-07-30 05:00:37 +0800 | [diff] [blame] | 1153 | c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem, NULL); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | // picture callback - compressed picture ready |
| 1158 | void CameraService::Client::handleCompressedPicture(const sp<IMemory>& mem) { |
| 1159 | disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE); |
| 1160 | |
| 1161 | sp<ICameraClient> c = mCameraClient; |
| 1162 | mLock.unlock(); |
| 1163 | if (c != 0) { |
Wu-cheng Li | 57c8618 | 2011-07-30 05:00:37 +0800 | [diff] [blame] | 1164 | c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | |
| 1169 | void CameraService::Client::handleGenericNotify(int32_t msgType, |
| 1170 | int32_t ext1, int32_t ext2) { |
| 1171 | sp<ICameraClient> c = mCameraClient; |
| 1172 | mLock.unlock(); |
| 1173 | if (c != 0) { |
| 1174 | c->notifyCallback(msgType, ext1, ext2); |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | void CameraService::Client::handleGenericData(int32_t msgType, |
Wu-cheng Li | 57c8618 | 2011-07-30 05:00:37 +0800 | [diff] [blame] | 1179 | const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1180 | sp<ICameraClient> c = mCameraClient; |
| 1181 | mLock.unlock(); |
| 1182 | if (c != 0) { |
Wu-cheng Li | 57c8618 | 2011-07-30 05:00:37 +0800 | [diff] [blame] | 1183 | c->dataCallback(msgType, dataPtr, metadata); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | void CameraService::Client::handleGenericDataTimestamp(nsecs_t timestamp, |
| 1188 | int32_t msgType, const sp<IMemory>& dataPtr) { |
| 1189 | sp<ICameraClient> c = mCameraClient; |
| 1190 | mLock.unlock(); |
| 1191 | if (c != 0) { |
| 1192 | c->dataCallbackTimestamp(timestamp, msgType, dataPtr); |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | void CameraService::Client::copyFrameAndPostCopiedFrame( |
Wu-cheng Li | 57c8618 | 2011-07-30 05:00:37 +0800 | [diff] [blame] | 1197 | int32_t msgType, const sp<ICameraClient>& client, |
| 1198 | const sp<IMemoryHeap>& heap, size_t offset, size_t size, |
| 1199 | camera_frame_metadata_t *metadata) { |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1200 | LOG2("copyFrameAndPostCopiedFrame"); |
| 1201 | // It is necessary to copy out of pmem before sending this to |
| 1202 | // the callback. For efficiency, reuse the same MemoryHeapBase |
| 1203 | // provided it's big enough. Don't allocate the memory or |
| 1204 | // perform the copy if there's no callback. |
| 1205 | // hold the preview lock while we grab a reference to the preview buffer |
| 1206 | sp<MemoryHeapBase> previewBuffer; |
| 1207 | |
| 1208 | if (mPreviewBuffer == 0) { |
| 1209 | mPreviewBuffer = new MemoryHeapBase(size, 0, NULL); |
| 1210 | } else if (size > mPreviewBuffer->virtualSize()) { |
| 1211 | mPreviewBuffer.clear(); |
| 1212 | mPreviewBuffer = new MemoryHeapBase(size, 0, NULL); |
| 1213 | } |
| 1214 | if (mPreviewBuffer == 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1215 | ALOGE("failed to allocate space for preview buffer"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1216 | mLock.unlock(); |
| 1217 | return; |
| 1218 | } |
| 1219 | previewBuffer = mPreviewBuffer; |
| 1220 | |
| 1221 | memcpy(previewBuffer->base(), (uint8_t *)heap->base() + offset, size); |
| 1222 | |
| 1223 | sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size); |
| 1224 | if (frame == 0) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1225 | ALOGE("failed to allocate space for frame callback"); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1226 | mLock.unlock(); |
| 1227 | return; |
| 1228 | } |
| 1229 | |
| 1230 | mLock.unlock(); |
Wu-cheng Li | 57c8618 | 2011-07-30 05:00:37 +0800 | [diff] [blame] | 1231 | client->dataCallback(msgType, frame, metadata); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1232 | } |
| 1233 | |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 1234 | int CameraService::Client::getOrientation(int degrees, bool mirror) { |
| 1235 | if (!mirror) { |
| 1236 | if (degrees == 0) return 0; |
| 1237 | else if (degrees == 90) return HAL_TRANSFORM_ROT_90; |
| 1238 | else if (degrees == 180) return HAL_TRANSFORM_ROT_180; |
| 1239 | else if (degrees == 270) return HAL_TRANSFORM_ROT_270; |
| 1240 | } else { // Do mirror (horizontal flip) |
| 1241 | if (degrees == 0) { // FLIP_H and ROT_0 |
| 1242 | return HAL_TRANSFORM_FLIP_H; |
| 1243 | } else if (degrees == 90) { // FLIP_H and ROT_90 |
| 1244 | return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90; |
| 1245 | } else if (degrees == 180) { // FLIP_H and ROT_180 |
| 1246 | return HAL_TRANSFORM_FLIP_V; |
| 1247 | } else if (degrees == 270) { // FLIP_H and ROT_270 |
| 1248 | return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90; |
| 1249 | } |
| 1250 | } |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1251 | ALOGE("Invalid setDisplayOrientation degrees=%d", degrees); |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 1252 | return -1; |
| 1253 | } |
| 1254 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1255 | // ---------------------------------------------------------------------------- |
| 1256 | |
| 1257 | static const int kDumpLockRetries = 50; |
| 1258 | static const int kDumpLockSleep = 60000; |
| 1259 | |
| 1260 | static bool tryLock(Mutex& mutex) |
| 1261 | { |
| 1262 | bool locked = false; |
| 1263 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 1264 | if (mutex.tryLock() == NO_ERROR) { |
| 1265 | locked = true; |
| 1266 | break; |
| 1267 | } |
| 1268 | usleep(kDumpLockSleep); |
| 1269 | } |
| 1270 | return locked; |
| 1271 | } |
| 1272 | |
| 1273 | status_t CameraService::dump(int fd, const Vector<String16>& args) { |
| 1274 | static const char* kDeadlockedString = "CameraService may be deadlocked\n"; |
| 1275 | |
| 1276 | const size_t SIZE = 256; |
| 1277 | char buffer[SIZE]; |
| 1278 | String8 result; |
| 1279 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 1280 | snprintf(buffer, SIZE, "Permission Denial: " |
| 1281 | "can't dump CameraService from pid=%d, uid=%d\n", |
| 1282 | getCallingPid(), |
| 1283 | getCallingUid()); |
| 1284 | result.append(buffer); |
| 1285 | write(fd, result.string(), result.size()); |
| 1286 | } else { |
| 1287 | bool locked = tryLock(mServiceLock); |
| 1288 | // failed to lock - CameraService is probably deadlocked |
| 1289 | if (!locked) { |
| 1290 | String8 result(kDeadlockedString); |
| 1291 | write(fd, result.string(), result.size()); |
| 1292 | } |
| 1293 | |
| 1294 | bool hasClient = false; |
| 1295 | for (int i = 0; i < mNumberOfCameras; i++) { |
| 1296 | sp<Client> client = mClient[i].promote(); |
| 1297 | if (client == 0) continue; |
| 1298 | hasClient = true; |
| 1299 | sprintf(buffer, "Client[%d] (%p) PID: %d\n", |
| 1300 | i, |
| 1301 | client->getCameraClient()->asBinder().get(), |
| 1302 | client->mClientPid); |
| 1303 | result.append(buffer); |
| 1304 | write(fd, result.string(), result.size()); |
| 1305 | client->mHardware->dump(fd, args); |
| 1306 | } |
| 1307 | if (!hasClient) { |
| 1308 | result.append("No camera client yet.\n"); |
| 1309 | write(fd, result.string(), result.size()); |
| 1310 | } |
| 1311 | |
| 1312 | if (locked) mServiceLock.unlock(); |
| 1313 | |
| 1314 | // change logging level |
| 1315 | int n = args.size(); |
| 1316 | for (int i = 0; i + 1 < n; i++) { |
| 1317 | if (args[i] == String16("-v")) { |
| 1318 | String8 levelStr(args[i+1]); |
| 1319 | int level = atoi(levelStr.string()); |
| 1320 | sprintf(buffer, "Set Log Level to %d", level); |
| 1321 | result.append(buffer); |
| 1322 | setLogLevel(level); |
| 1323 | } |
| 1324 | } |
| 1325 | } |
| 1326 | return NO_ERROR; |
| 1327 | } |
| 1328 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1329 | }; // namespace android |