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