The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright (C) 2008, The Android Open Source Project |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | #define LOG_TAG "CameraService" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <pthread.h> |
| 23 | |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 24 | #include <binder/IPCThreadState.h> |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 25 | #include <binder/IServiceManager.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 26 | #include <binder/MemoryBase.h> |
| 27 | #include <binder/MemoryHeapBase.h> |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 28 | #include <cutils/atomic.h> |
| 29 | #include <hardware/hardware.h> |
| 30 | #include <media/AudioSystem.h> |
| 31 | #include <media/mediaplayer.h> |
Mathias Agopian | 000479f | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 32 | #include <surfaceflinger/ISurface.h> |
| 33 | #include <ui/Overlay.h> |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 34 | #include <utils/Errors.h> |
| 35 | #include <utils/Log.h> |
| 36 | #include <utils/String16.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | |
| 38 | #include "CameraService.h" |
Eric Laurent | a7f1e5c | 2009-03-27 16:27:16 -0700 | [diff] [blame] | 39 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | namespace android { |
| 41 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 42 | // ---------------------------------------------------------------------------- |
| 43 | // Logging support -- this is for debugging only |
| 44 | // Use "adb shell dumpsys media.camera -v 1" to change it. |
| 45 | static volatile int32_t gLogLevel = 0; |
| 46 | |
| 47 | #define LOG1(...) LOGD_IF(gLogLevel >= 1, __VA_ARGS__); |
| 48 | #define LOG2(...) LOGD_IF(gLogLevel >= 2, __VA_ARGS__); |
| 49 | |
| 50 | static void setLogLevel(int level) { |
| 51 | android_atomic_write(level, &gLogLevel); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 54 | // ---------------------------------------------------------------------------- |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | |
Chih-Chung Chang | 1f25ec8 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 56 | static int getCallingPid() { |
| 57 | return IPCThreadState::self()->getCallingPid(); |
| 58 | } |
| 59 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 60 | static int getCallingUid() { |
| 61 | return IPCThreadState::self()->getCallingUid(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // ---------------------------------------------------------------------------- |
| 65 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 66 | // This is ugly and only safe if we never re-create the CameraService, but |
| 67 | // should be ok for now. |
| 68 | static CameraService *gCameraService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 70 | CameraService::CameraService() |
| 71 | :mSoundRef(0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | { |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 73 | LOGI("CameraService started (pid=%d)", getpid()); |
| 74 | |
Chih-Chung Chang | b8bb78f | 2010-06-10 13:32:16 +0800 | [diff] [blame] | 75 | mNumberOfCameras = HAL_getNumberOfCameras(); |
| 76 | if (mNumberOfCameras > MAX_CAMERAS) { |
| 77 | LOGE("Number of cameras(%d) > MAX_CAMERAS(%d).", |
| 78 | mNumberOfCameras, MAX_CAMERAS); |
| 79 | mNumberOfCameras = MAX_CAMERAS; |
| 80 | } |
| 81 | |
| 82 | for (int i = 0; i < mNumberOfCameras; i++) { |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 83 | setCameraFree(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | } |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 85 | |
| 86 | gCameraService = this; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 89 | CameraService::~CameraService() { |
Chih-Chung Chang | b8bb78f | 2010-06-10 13:32:16 +0800 | [diff] [blame] | 90 | for (int i = 0; i < mNumberOfCameras; i++) { |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 91 | if (mBusy[i]) { |
| 92 | LOGE("camera %d is still in use in destructor!", i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 96 | gCameraService = NULL; |
| 97 | } |
| 98 | |
| 99 | int32_t CameraService::getNumberOfCameras() { |
Chih-Chung Chang | b8bb78f | 2010-06-10 13:32:16 +0800 | [diff] [blame] | 100 | return mNumberOfCameras; |
| 101 | } |
| 102 | |
| 103 | status_t CameraService::getCameraInfo(int cameraId, |
| 104 | struct CameraInfo* cameraInfo) { |
| 105 | if (cameraId < 0 || cameraId >= mNumberOfCameras) { |
| 106 | return BAD_VALUE; |
| 107 | } |
| 108 | |
| 109 | HAL_getCameraInfo(cameraId, cameraInfo); |
| 110 | return OK; |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | sp<ICamera> CameraService::connect( |
| 114 | const sp<ICameraClient>& cameraClient, int cameraId) { |
| 115 | int callingPid = getCallingPid(); |
| 116 | LOG1("CameraService::connect E (pid %d, id %d)", callingPid, cameraId); |
| 117 | |
| 118 | sp<Client> client; |
Chih-Chung Chang | b8bb78f | 2010-06-10 13:32:16 +0800 | [diff] [blame] | 119 | if (cameraId < 0 || cameraId >= mNumberOfCameras) { |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 120 | LOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).", |
| 121 | callingPid, cameraId); |
| 122 | return NULL; |
Chih-Chung Chang | d5d1ebd | 2009-06-24 19:59:31 +0800 | [diff] [blame] | 123 | } |
| 124 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 125 | Mutex::Autolock lock(mServiceLock); |
| 126 | if (mClient[cameraId] != 0) { |
| 127 | client = mClient[cameraId].promote(); |
| 128 | if (client != 0) { |
| 129 | if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) { |
| 130 | LOG1("CameraService::connect X (pid %d) (the same client)", |
| 131 | callingPid); |
| 132 | return client; |
| 133 | } else { |
| 134 | LOGW("CameraService::connect X (pid %d) rejected (existing client).", |
| 135 | callingPid); |
| 136 | return NULL; |
| 137 | } |
| 138 | } |
| 139 | mClient[cameraId].clear(); |
| 140 | } |
| 141 | |
| 142 | if (mBusy[cameraId]) { |
| 143 | LOGW("CameraService::connect X (pid %d) rejected" |
| 144 | " (camera %d is still busy).", callingPid, cameraId); |
| 145 | return NULL; |
| 146 | } |
| 147 | |
Wu-cheng Li | e704438 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 148 | sp<CameraHardwareInterface> hardware = HAL_openCameraHardware(cameraId); |
| 149 | if (hardware == NULL) { |
| 150 | LOGE("Fail to open camera hardware (id=%d)", cameraId); |
| 151 | return NULL; |
| 152 | } |
Wu-cheng Li | b982fb4 | 2010-10-19 17:19:09 +0800 | [diff] [blame^] | 153 | CameraInfo info; |
| 154 | HAL_getCameraInfo(cameraId, &info); |
| 155 | client = new Client(this, cameraClient, hardware, cameraId, info.facing, |
| 156 | info.orientation, callingPid); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 157 | mClient[cameraId] = client; |
| 158 | LOG1("CameraService::connect X"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | return client; |
| 160 | } |
| 161 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 162 | void CameraService::removeClient(const sp<ICameraClient>& cameraClient) { |
Chih-Chung Chang | 1f25ec8 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 163 | int callingPid = getCallingPid(); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 164 | LOG1("CameraService::removeClient E (pid %d)", callingPid); |
Chih-Chung Chang | 1f25ec8 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 165 | |
Chih-Chung Chang | b8bb78f | 2010-06-10 13:32:16 +0800 | [diff] [blame] | 166 | for (int i = 0; i < mNumberOfCameras; i++) { |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 167 | // Declare this before the lock to make absolutely sure the |
| 168 | // destructor won't be called with the lock held. |
| 169 | sp<Client> client; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 171 | Mutex::Autolock lock(mServiceLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 173 | // This happens when we have already disconnected (or this is |
| 174 | // just another unused camera). |
| 175 | if (mClient[i] == 0) continue; |
| 176 | |
| 177 | // Promote mClient. It can fail if we are called from this path: |
| 178 | // Client::~Client() -> disconnect() -> removeClient(). |
| 179 | client = mClient[i].promote(); |
| 180 | |
| 181 | if (client == 0) { |
| 182 | mClient[i].clear(); |
| 183 | continue; |
| 184 | } |
| 185 | |
| 186 | if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) { |
| 187 | // Found our camera, clear and leave. |
| 188 | LOG1("removeClient: clear camera %d", i); |
| 189 | mClient[i].clear(); |
| 190 | break; |
| 191 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 194 | LOG1("CameraService::removeClient X (pid %d)", callingPid); |
| 195 | } |
| 196 | |
| 197 | sp<CameraService::Client> CameraService::getClientById(int cameraId) { |
Chih-Chung Chang | b8bb78f | 2010-06-10 13:32:16 +0800 | [diff] [blame] | 198 | if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL; |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 199 | return mClient[cameraId].promote(); |
| 200 | } |
| 201 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 202 | status_t CameraService::onTransact( |
| 203 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { |
| 204 | // Permission checks |
| 205 | switch (code) { |
| 206 | case BnCameraService::CONNECT: |
| 207 | const int pid = getCallingPid(); |
| 208 | const int self_pid = getpid(); |
| 209 | if (pid != self_pid) { |
| 210 | // we're called from a different process, do the real check |
| 211 | if (!checkCallingPermission( |
| 212 | String16("android.permission.CAMERA"))) { |
| 213 | const int uid = getCallingUid(); |
| 214 | LOGE("Permission Denial: " |
| 215 | "can't use the camera pid=%d, uid=%d", pid, uid); |
| 216 | return PERMISSION_DENIED; |
| 217 | } |
| 218 | } |
| 219 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 222 | return BnCameraService::onTransact(code, data, reply, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 225 | // The reason we need this busy bit is a new CameraService::connect() request |
| 226 | // may come in while the previous Client's destructor has not been run or is |
| 227 | // still running. If the last strong reference of the previous Client is gone |
| 228 | // but the destructor has not been finished, we should not allow the new Client |
| 229 | // to be created because we need to wait for the previous Client to tear down |
| 230 | // the hardware first. |
| 231 | void CameraService::setCameraBusy(int cameraId) { |
| 232 | android_atomic_write(1, &mBusy[cameraId]); |
Chih-Chung Chang | 6fcba31 | 2009-06-24 13:44:37 +0800 | [diff] [blame] | 233 | } |
| 234 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 235 | void CameraService::setCameraFree(int cameraId) { |
| 236 | android_atomic_write(0, &mBusy[cameraId]); |
Chih-Chung Chang | 6fcba31 | 2009-06-24 13:44:37 +0800 | [diff] [blame] | 237 | } |
| 238 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 239 | // We share the media players for shutter and recording sound for all clients. |
| 240 | // A reference count is kept to determine when we will actually release the |
| 241 | // media players. |
| 242 | |
| 243 | static MediaPlayer* newMediaPlayer(const char *file) { |
| 244 | MediaPlayer* mp = new MediaPlayer(); |
| 245 | if (mp->setDataSource(file, NULL) == NO_ERROR) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 246 | mp->setAudioStreamType(AudioSystem::ENFORCED_AUDIBLE); |
Jason Sams | b18b691 | 2009-03-24 20:21:36 -0700 | [diff] [blame] | 247 | mp->prepare(); |
| 248 | } else { |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 249 | LOGE("Failed to load CameraService sounds: %s", file); |
| 250 | return NULL; |
Jason Sams | b18b691 | 2009-03-24 20:21:36 -0700 | [diff] [blame] | 251 | } |
| 252 | return mp; |
| 253 | } |
| 254 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 255 | void CameraService::loadSound() { |
| 256 | Mutex::Autolock lock(mSoundLock); |
| 257 | LOG1("CameraService::loadSound ref=%d", mSoundRef); |
| 258 | if (mSoundRef++) return; |
| 259 | |
| 260 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); |
| 261 | mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); |
| 262 | } |
| 263 | |
| 264 | void CameraService::releaseSound() { |
| 265 | Mutex::Autolock lock(mSoundLock); |
| 266 | LOG1("CameraService::releaseSound ref=%d", mSoundRef); |
| 267 | if (--mSoundRef) return; |
| 268 | |
| 269 | for (int i = 0; i < NUM_SOUNDS; i++) { |
| 270 | if (mSoundPlayer[i] != 0) { |
| 271 | mSoundPlayer[i]->disconnect(); |
| 272 | mSoundPlayer[i].clear(); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | void CameraService::playSound(sound_kind kind) { |
| 278 | LOG1("playSound(%d)", kind); |
| 279 | Mutex::Autolock lock(mSoundLock); |
| 280 | sp<MediaPlayer> player = mSoundPlayer[kind]; |
| 281 | if (player != 0) { |
| 282 | // do not play the sound if stream volume is 0 |
| 283 | // (typically because ringer mode is silent). |
| 284 | int index; |
| 285 | AudioSystem::getStreamVolumeIndex(AudioSystem::ENFORCED_AUDIBLE, &index); |
| 286 | if (index != 0) { |
| 287 | player->seekTo(0); |
| 288 | player->start(); |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | // ---------------------------------------------------------------------------- |
| 294 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | CameraService::Client::Client(const sp<CameraService>& cameraService, |
Wu-cheng Li | e704438 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 296 | const sp<ICameraClient>& cameraClient, |
| 297 | const sp<CameraHardwareInterface>& hardware, |
Wu-cheng Li | b982fb4 | 2010-10-19 17:19:09 +0800 | [diff] [blame^] | 298 | int cameraId, int cameraFacing, int cameraOrientation, int clientPid) { |
Chih-Chung Chang | 1f25ec8 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 299 | int callingPid = getCallingPid(); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 300 | LOG1("Client::Client E (pid %d)", callingPid); |
| 301 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | mCameraService = cameraService; |
| 303 | mCameraClient = cameraClient; |
Wu-cheng Li | e704438 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 304 | mHardware = hardware; |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 305 | mCameraId = cameraId; |
Wu-cheng Li | b982fb4 | 2010-10-19 17:19:09 +0800 | [diff] [blame^] | 306 | mCameraFacing = cameraFacing; |
| 307 | mCameraOrientation = cameraOrientation; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | mClientPid = clientPid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | mUseOverlay = mHardware->useOverlay(); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 310 | mMsgEnabled = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | |
Benny Wong | da83f46 | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 312 | mHardware->setCallbacks(notifyCallback, |
| 313 | dataCallback, |
| 314 | dataCallbackTimestamp, |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 315 | (void *)cameraId); |
Benny Wong | da83f46 | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 316 | |
| 317 | // Enable zoom, error, and focus messages by default |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 318 | enableMsgType(CAMERA_MSG_ERROR | |
| 319 | CAMERA_MSG_ZOOM | |
| 320 | CAMERA_MSG_FOCUS); |
Benny Wong | 6d2090e | 2009-07-15 18:44:27 -0500 | [diff] [blame] | 321 | mOverlayW = 0; |
| 322 | mOverlayH = 0; |
Jason Sams | b18b691 | 2009-03-24 20:21:36 -0700 | [diff] [blame] | 323 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | // Callback is disabled by default |
| 325 | mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP; |
Wu-cheng Li | b982fb4 | 2010-10-19 17:19:09 +0800 | [diff] [blame^] | 326 | mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT); |
Wu-cheng Li | b3347bc | 2010-09-23 17:17:43 -0700 | [diff] [blame] | 327 | mOrientationChanged = false; |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 328 | cameraService->setCameraBusy(cameraId); |
| 329 | cameraService->loadSound(); |
| 330 | LOG1("Client::Client X (pid %d)", callingPid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | } |
| 332 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 333 | static void *unregister_surface(void *arg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | ISurface *surface = (ISurface *)arg; |
| 335 | surface->unregisterBuffers(); |
| 336 | IPCThreadState::self()->flushCommands(); |
| 337 | return NULL; |
| 338 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 340 | // tear down the client |
| 341 | CameraService::Client::~Client() { |
Chih-Chung Chang | 1f25ec8 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 342 | int callingPid = getCallingPid(); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 343 | LOG1("Client::~Client E (pid %d, this %p)", callingPid, this); |
Chih-Chung Chang | 1f25ec8 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 344 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | if (mSurface != 0 && !mUseOverlay) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | pthread_t thr; |
| 347 | // We unregister the buffers in a different thread because binder does |
| 348 | // not let us make sychronous transactions in a binder destructor (that |
| 349 | // is, upon our reaching a refcount of zero.) |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 350 | pthread_create(&thr, |
| 351 | NULL, // attr |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 352 | unregister_surface, |
| 353 | mSurface.get()); |
| 354 | pthread_join(thr, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | } |
| 356 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 357 | // set mClientPid to let disconnet() tear down the hardware |
Chih-Chung Chang | 1f25ec8 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 358 | mClientPid = callingPid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 359 | disconnect(); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 360 | mCameraService->releaseSound(); |
| 361 | LOG1("Client::~Client X (pid %d, this %p)", callingPid, this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | } |
| 363 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 364 | // ---------------------------------------------------------------------------- |
| 365 | |
| 366 | status_t CameraService::Client::checkPid() const { |
Chih-Chung Chang | 6fcba31 | 2009-06-24 13:44:37 +0800 | [diff] [blame] | 367 | int callingPid = getCallingPid(); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 368 | if (callingPid == mClientPid) return NO_ERROR; |
James Dong | 71d714c | 2010-06-09 15:57:48 -0700 | [diff] [blame] | 369 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 370 | LOGW("attempt to use a locked camera from a different process" |
| 371 | " (old pid %d, new pid %d)", mClientPid, callingPid); |
| 372 | return EBUSY; |
| 373 | } |
Chih-Chung Chang | 1f25ec8 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 374 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 375 | status_t CameraService::Client::checkPidAndHardware() const { |
| 376 | status_t result = checkPid(); |
| 377 | if (result != NO_ERROR) return result; |
| 378 | if (mHardware == 0) { |
| 379 | LOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid()); |
| 380 | return INVALID_OPERATION; |
| 381 | } |
| 382 | return NO_ERROR; |
| 383 | } |
Chih-Chung Chang | 1f25ec8 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 384 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 385 | status_t CameraService::Client::lock() { |
| 386 | int callingPid = getCallingPid(); |
| 387 | LOG1("lock (pid %d)", callingPid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | Mutex::Autolock lock(mLock); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 389 | |
| 390 | // lock camera to this client if the the camera is unlocked |
| 391 | if (mClientPid == 0) { |
| 392 | mClientPid = callingPid; |
| 393 | return NO_ERROR; |
| 394 | } |
| 395 | |
| 396 | // returns NO_ERROR if the client already owns the camera, EBUSY otherwise |
| 397 | return checkPid(); |
| 398 | } |
| 399 | |
| 400 | status_t CameraService::Client::unlock() { |
| 401 | int callingPid = getCallingPid(); |
| 402 | LOG1("unlock (pid %d)", callingPid); |
| 403 | Mutex::Autolock lock(mLock); |
| 404 | |
| 405 | // allow anyone to use camera (after they lock the camera) |
| 406 | status_t result = checkPid(); |
| 407 | if (result == NO_ERROR) { |
| 408 | mClientPid = 0; |
| 409 | LOG1("clear mCameraClient (pid %d)", callingPid); |
| 410 | // we need to remove the reference to ICameraClient so that when the app |
| 411 | // goes away, the reference count goes to 0. |
| 412 | mCameraClient.clear(); |
| 413 | } |
| 414 | return result; |
| 415 | } |
| 416 | |
| 417 | // connect a new client to the camera |
| 418 | status_t CameraService::Client::connect(const sp<ICameraClient>& client) { |
| 419 | int callingPid = getCallingPid(); |
| 420 | LOG1("connect E (pid %d)", callingPid); |
| 421 | Mutex::Autolock lock(mLock); |
| 422 | |
| 423 | if (mClientPid != 0 && checkPid() != NO_ERROR) { |
| 424 | LOGW("Tried to connect to a locked camera (old pid %d, new pid %d)", |
| 425 | mClientPid, callingPid); |
| 426 | return EBUSY; |
| 427 | } |
| 428 | |
| 429 | if (mCameraClient != 0 && (client->asBinder() == mCameraClient->asBinder())) { |
| 430 | LOG1("Connect to the same client"); |
| 431 | return NO_ERROR; |
| 432 | } |
| 433 | |
| 434 | mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP; |
| 435 | mClientPid = callingPid; |
| 436 | mCameraClient = client; |
| 437 | |
| 438 | LOG1("connect X (pid %d)", callingPid); |
| 439 | return NO_ERROR; |
| 440 | } |
| 441 | |
| 442 | void CameraService::Client::disconnect() { |
| 443 | int callingPid = getCallingPid(); |
| 444 | LOG1("disconnect E (pid %d)", callingPid); |
| 445 | Mutex::Autolock lock(mLock); |
| 446 | |
| 447 | if (checkPid() != NO_ERROR) { |
| 448 | LOGW("different client - don't disconnect"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 449 | return; |
| 450 | } |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 451 | |
| 452 | if (mClientPid <= 0) { |
| 453 | LOG1("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | return; |
| 455 | } |
| 456 | |
Chih-Chung Chang | 6fcba31 | 2009-06-24 13:44:37 +0800 | [diff] [blame] | 457 | // Make sure disconnect() is done once and once only, whether it is called |
| 458 | // from the user directly, or called by the destructor. |
| 459 | if (mHardware == 0) return; |
| 460 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 461 | LOG1("hardware teardown"); |
Chih-Chung Chang | 6fcba31 | 2009-06-24 13:44:37 +0800 | [diff] [blame] | 462 | // Before destroying mHardware, we must make sure it's in the |
| 463 | // idle state. |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 464 | // Turn off all messages. |
| 465 | disableMsgType(CAMERA_MSG_ALL_MSGS); |
Chih-Chung Chang | 6fcba31 | 2009-06-24 13:44:37 +0800 | [diff] [blame] | 466 | mHardware->stopPreview(); |
Benny Wong | da83f46 | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 467 | mHardware->cancelPicture(); |
Chih-Chung Chang | 6fcba31 | 2009-06-24 13:44:37 +0800 | [diff] [blame] | 468 | // Release the hardware resources. |
| 469 | mHardware->release(); |
Benny Wong | 6d2090e | 2009-07-15 18:44:27 -0500 | [diff] [blame] | 470 | // Release the held overlay resources. |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 471 | if (mUseOverlay) { |
Benny Wong | 6d2090e | 2009-07-15 18:44:27 -0500 | [diff] [blame] | 472 | mOverlayRef = 0; |
| 473 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | mHardware.clear(); |
Chih-Chung Chang | 6fcba31 | 2009-06-24 13:44:37 +0800 | [diff] [blame] | 475 | |
Chih-Chung Chang | d5d1ebd | 2009-06-24 19:59:31 +0800 | [diff] [blame] | 476 | mCameraService->removeClient(mCameraClient); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 477 | mCameraService->setCameraFree(mCameraId); |
Chih-Chung Chang | 6fcba31 | 2009-06-24 13:44:37 +0800 | [diff] [blame] | 478 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 479 | LOG1("disconnect X (pid %d)", callingPid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | } |
| 481 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 482 | // ---------------------------------------------------------------------------- |
| 483 | |
| 484 | // set the ISurface that the preview will use |
| 485 | status_t CameraService::Client::setPreviewDisplay(const sp<ISurface>& surface) { |
| 486 | LOG1("setPreviewDisplay(%p) (pid %d)", surface.get(), getCallingPid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | Mutex::Autolock lock(mLock); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 488 | status_t result = checkPidAndHardware(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | if (result != NO_ERROR) return result; |
Wu-cheng Li | b8a10fe | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 490 | |
Wu-cheng Li | b8a10fe | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 491 | result = NO_ERROR; |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 492 | |
| 493 | // return if no change in surface. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | // asBinder() is safe on NULL (returns NULL) |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 495 | if (surface->asBinder() == mSurface->asBinder()) { |
| 496 | return result; |
| 497 | } |
| 498 | |
| 499 | if (mSurface != 0) { |
| 500 | LOG1("clearing old preview surface %p", mSurface.get()); |
| 501 | if (mUseOverlay) { |
| 502 | // Force the destruction of any previous overlay |
| 503 | sp<Overlay> dummy; |
| 504 | mHardware->setOverlay(dummy); |
Wu-cheng Li | b3347bc | 2010-09-23 17:17:43 -0700 | [diff] [blame] | 505 | mOverlayRef = 0; |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 506 | } else { |
| 507 | mSurface->unregisterBuffers(); |
Wu-cheng Li | b8a10fe | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 508 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | } |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 510 | mSurface = surface; |
| 511 | mOverlayRef = 0; |
| 512 | // If preview has been already started, set overlay or register preview |
| 513 | // buffers now. |
| 514 | if (mHardware->previewEnabled()) { |
| 515 | if (mUseOverlay) { |
| 516 | result = setOverlay(); |
| 517 | } else if (mSurface != 0) { |
| 518 | result = registerPreviewBuffers(); |
| 519 | } |
| 520 | } |
| 521 | |
Wu-cheng Li | b8a10fe | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 522 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 523 | } |
| 524 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 525 | status_t CameraService::Client::registerPreviewBuffers() { |
Wu-cheng Li | b8a10fe | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 526 | int w, h; |
| 527 | CameraParameters params(mHardware->getParameters()); |
| 528 | params.getPreviewSize(&w, &h); |
| 529 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 530 | // FIXME: don't use a hardcoded format here. |
Wu-cheng Li | b8a10fe | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 531 | ISurface::BufferHeap buffers(w, h, w, h, |
Mathias Agopian | 5a48712 | 2010-02-16 19:42:32 -0800 | [diff] [blame] | 532 | HAL_PIXEL_FORMAT_YCrCb_420_SP, |
Chih-Chung Chang | e1ceec2 | 2010-01-21 17:31:06 -0800 | [diff] [blame] | 533 | mOrientation, |
Wu-cheng Li | b8a10fe | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 534 | 0, |
| 535 | mHardware->getPreviewHeap()); |
| 536 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 537 | status_t result = mSurface->registerBuffers(buffers); |
| 538 | if (result != NO_ERROR) { |
| 539 | LOGE("registerBuffers failed with status %d", result); |
Wu-cheng Li | b8a10fe | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 540 | } |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 541 | return result; |
Wu-cheng Li | b8a10fe | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 542 | } |
| 543 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 544 | status_t CameraService::Client::setOverlay() { |
| 545 | int w, h; |
| 546 | CameraParameters params(mHardware->getParameters()); |
| 547 | params.getPreviewSize(&w, &h); |
| 548 | |
Wu-cheng Li | b3347bc | 2010-09-23 17:17:43 -0700 | [diff] [blame] | 549 | if (w != mOverlayW || h != mOverlayH || mOrientationChanged) { |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 550 | // Force the destruction of any previous overlay |
| 551 | sp<Overlay> dummy; |
| 552 | mHardware->setOverlay(dummy); |
| 553 | mOverlayRef = 0; |
Wu-cheng Li | b3347bc | 2010-09-23 17:17:43 -0700 | [diff] [blame] | 554 | mOrientationChanged = false; |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | status_t result = NO_ERROR; |
| 558 | if (mSurface == 0) { |
| 559 | result = mHardware->setOverlay(NULL); |
| 560 | } else { |
| 561 | if (mOverlayRef == 0) { |
| 562 | // FIXME: |
| 563 | // Surfaceflinger may hold onto the previous overlay reference for some |
| 564 | // time after we try to destroy it. retry a few times. In the future, we |
| 565 | // should make the destroy call block, or possibly specify that we can |
| 566 | // wait in the createOverlay call if the previous overlay is in the |
| 567 | // process of being destroyed. |
| 568 | for (int retry = 0; retry < 50; ++retry) { |
| 569 | mOverlayRef = mSurface->createOverlay(w, h, OVERLAY_FORMAT_DEFAULT, |
| 570 | mOrientation); |
| 571 | if (mOverlayRef != 0) break; |
| 572 | LOGW("Overlay create failed - retrying"); |
| 573 | usleep(20000); |
| 574 | } |
| 575 | if (mOverlayRef == 0) { |
| 576 | LOGE("Overlay Creation Failed!"); |
| 577 | return -EINVAL; |
| 578 | } |
| 579 | result = mHardware->setOverlay(new Overlay(mOverlayRef)); |
| 580 | } |
| 581 | } |
| 582 | if (result != NO_ERROR) { |
| 583 | LOGE("mHardware->setOverlay() failed with status %d\n", result); |
| 584 | return result; |
| 585 | } |
| 586 | |
| 587 | mOverlayW = w; |
| 588 | mOverlayH = h; |
| 589 | |
| 590 | return result; |
| 591 | } |
| 592 | |
| 593 | // set the preview callback flag to affect how the received frames from |
| 594 | // preview are handled. |
| 595 | void CameraService::Client::setPreviewCallbackFlag(int callback_flag) { |
| 596 | LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid()); |
| 597 | Mutex::Autolock lock(mLock); |
| 598 | if (checkPidAndHardware() != NO_ERROR) return; |
| 599 | |
| 600 | mPreviewCallbackFlag = callback_flag; |
| 601 | |
| 602 | // If we don't use overlay, we always need the preview frame for display. |
| 603 | // If we do use overlay, we only need the preview frame if the user |
| 604 | // wants the data. |
| 605 | if (mUseOverlay) { |
| 606 | if(mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ENABLE_MASK) { |
| 607 | enableMsgType(CAMERA_MSG_PREVIEW_FRAME); |
| 608 | } else { |
| 609 | disableMsgType(CAMERA_MSG_PREVIEW_FRAME); |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | // start preview mode |
| 615 | status_t CameraService::Client::startPreview() { |
| 616 | LOG1("startPreview (pid %d)", getCallingPid()); |
| 617 | return startCameraMode(CAMERA_PREVIEW_MODE); |
| 618 | } |
| 619 | |
| 620 | // start recording mode |
| 621 | status_t CameraService::Client::startRecording() { |
| 622 | LOG1("startRecording (pid %d)", getCallingPid()); |
| 623 | return startCameraMode(CAMERA_RECORDING_MODE); |
| 624 | } |
| 625 | |
| 626 | // start preview or recording |
| 627 | status_t CameraService::Client::startCameraMode(camera_mode mode) { |
| 628 | LOG1("startCameraMode(%d)", mode); |
| 629 | Mutex::Autolock lock(mLock); |
| 630 | status_t result = checkPidAndHardware(); |
| 631 | if (result != NO_ERROR) return result; |
| 632 | |
| 633 | switch(mode) { |
| 634 | case CAMERA_PREVIEW_MODE: |
| 635 | if (mSurface == 0) { |
| 636 | LOG1("mSurface is not set yet."); |
| 637 | // still able to start preview in this case. |
| 638 | } |
| 639 | return startPreviewMode(); |
| 640 | case CAMERA_RECORDING_MODE: |
| 641 | if (mSurface == 0) { |
| 642 | LOGE("mSurface must be set before startRecordingMode."); |
| 643 | return INVALID_OPERATION; |
| 644 | } |
| 645 | return startRecordingMode(); |
| 646 | default: |
| 647 | return UNKNOWN_ERROR; |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | status_t CameraService::Client::startPreviewMode() { |
| 652 | LOG1("startPreviewMode"); |
| 653 | status_t result = NO_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 654 | |
| 655 | // if preview has been enabled, nothing needs to be done |
| 656 | if (mHardware->previewEnabled()) { |
| 657 | return NO_ERROR; |
| 658 | } |
| 659 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 660 | if (mUseOverlay) { |
Wu-cheng Li | b8a10fe | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 661 | // If preview display has been set, set overlay now. |
| 662 | if (mSurface != 0) { |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 663 | result = setOverlay(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 664 | } |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 665 | if (result != NO_ERROR) return result; |
| 666 | result = mHardware->startPreview(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 667 | } else { |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 668 | enableMsgType(CAMERA_MSG_PREVIEW_FRAME); |
| 669 | result = mHardware->startPreview(); |
| 670 | if (result != NO_ERROR) return result; |
Wu-cheng Li | b8a10fe | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 671 | // If preview display has been set, register preview buffers now. |
| 672 | if (mSurface != 0) { |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 673 | // Unregister here because the surface may be previously registered |
| 674 | // with the raw (snapshot) heap. |
Wu-cheng Li | b8a10fe | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 675 | mSurface->unregisterBuffers(); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 676 | result = registerPreviewBuffers(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 677 | } |
| 678 | } |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 679 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 680 | } |
| 681 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 682 | status_t CameraService::Client::startRecordingMode() { |
| 683 | LOG1("startRecordingMode"); |
| 684 | status_t result = NO_ERROR; |
Wu-cheng Li | 36f68b8 | 2009-09-28 16:14:58 -0700 | [diff] [blame] | 685 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 686 | // if recording has been enabled, nothing needs to be done |
| 687 | if (mHardware->recordingEnabled()) { |
| 688 | return NO_ERROR; |
| 689 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 690 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 691 | // if preview has not been started, start preview first |
| 692 | if (!mHardware->previewEnabled()) { |
| 693 | result = startPreviewMode(); |
| 694 | if (result != NO_ERROR) { |
| 695 | return result; |
Eric Laurent | 524dc04 | 2009-11-27 05:07:55 -0800 | [diff] [blame] | 696 | } |
Jason Sams | b18b691 | 2009-03-24 20:21:36 -0700 | [diff] [blame] | 697 | } |
Benny Wong | da83f46 | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 698 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 699 | // start recording mode |
| 700 | enableMsgType(CAMERA_MSG_VIDEO_FRAME); |
| 701 | mCameraService->playSound(SOUND_RECORDING); |
| 702 | result = mHardware->startRecording(); |
| 703 | if (result != NO_ERROR) { |
| 704 | LOGE("mHardware->startRecording() failed with status %d", result); |
| 705 | } |
| 706 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | // stop preview mode |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 710 | void CameraService::Client::stopPreview() { |
| 711 | LOG1("stopPreview (pid %d)", getCallingPid()); |
| 712 | Mutex::Autolock lock(mLock); |
| 713 | if (checkPidAndHardware() != NO_ERROR) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 714 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 715 | disableMsgType(CAMERA_MSG_PREVIEW_FRAME); |
| 716 | mHardware->stopPreview(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 717 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 718 | if (mSurface != 0 && !mUseOverlay) { |
| 719 | mSurface->unregisterBuffers(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 720 | } |
| 721 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 722 | mPreviewBuffer.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | // stop recording mode |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 726 | void CameraService::Client::stopRecording() { |
| 727 | LOG1("stopRecording (pid %d)", getCallingPid()); |
| 728 | Mutex::Autolock lock(mLock); |
| 729 | if (checkPidAndHardware() != NO_ERROR) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 730 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 731 | mCameraService->playSound(SOUND_RECORDING); |
| 732 | disableMsgType(CAMERA_MSG_VIDEO_FRAME); |
| 733 | mHardware->stopRecording(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 734 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 735 | mPreviewBuffer.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | // release a recording frame |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 739 | void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 740 | Mutex::Autolock lock(mLock); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 741 | if (checkPidAndHardware() != NO_ERROR) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 742 | mHardware->releaseRecordingFrame(mem); |
| 743 | } |
| 744 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 745 | bool CameraService::Client::previewEnabled() { |
| 746 | LOG1("previewEnabled (pid %d)", getCallingPid()); |
| 747 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 748 | Mutex::Autolock lock(mLock); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 749 | if (checkPidAndHardware() != NO_ERROR) return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 750 | return mHardware->previewEnabled(); |
| 751 | } |
| 752 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 753 | bool CameraService::Client::recordingEnabled() { |
| 754 | LOG1("recordingEnabled (pid %d)", getCallingPid()); |
| 755 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 756 | Mutex::Autolock lock(mLock); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 757 | if (checkPidAndHardware() != NO_ERROR) return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 758 | return mHardware->recordingEnabled(); |
| 759 | } |
| 760 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 761 | status_t CameraService::Client::autoFocus() { |
| 762 | LOG1("autoFocus (pid %d)", getCallingPid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 763 | |
| 764 | Mutex::Autolock lock(mLock); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 765 | status_t result = checkPidAndHardware(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 766 | if (result != NO_ERROR) return result; |
| 767 | |
Benny Wong | da83f46 | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 768 | return mHardware->autoFocus(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 769 | } |
| 770 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 771 | status_t CameraService::Client::cancelAutoFocus() { |
| 772 | LOG1("cancelAutoFocus (pid %d)", getCallingPid()); |
Chih-Chung Chang | 244f8c2 | 2009-09-15 14:51:56 +0800 | [diff] [blame] | 773 | |
| 774 | Mutex::Autolock lock(mLock); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 775 | status_t result = checkPidAndHardware(); |
Chih-Chung Chang | 244f8c2 | 2009-09-15 14:51:56 +0800 | [diff] [blame] | 776 | if (result != NO_ERROR) return result; |
| 777 | |
Chih-Chung Chang | 244f8c2 | 2009-09-15 14:51:56 +0800 | [diff] [blame] | 778 | return mHardware->cancelAutoFocus(); |
| 779 | } |
| 780 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 781 | // take a picture - image is returned in callback |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 782 | status_t CameraService::Client::takePicture() { |
| 783 | LOG1("takePicture (pid %d)", getCallingPid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 784 | |
| 785 | Mutex::Autolock lock(mLock); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 786 | status_t result = checkPidAndHardware(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 787 | if (result != NO_ERROR) return result; |
| 788 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 789 | enableMsgType(CAMERA_MSG_SHUTTER | |
| 790 | CAMERA_MSG_POSTVIEW_FRAME | |
| 791 | CAMERA_MSG_RAW_IMAGE | |
| 792 | CAMERA_MSG_COMPRESSED_IMAGE); |
Benny Wong | da83f46 | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 793 | |
| 794 | return mHardware->takePicture(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 795 | } |
| 796 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 797 | // set preview/capture parameters - key/value pairs |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 798 | status_t CameraService::Client::setParameters(const String8& params) { |
| 799 | LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 800 | |
| 801 | Mutex::Autolock lock(mLock); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 802 | status_t result = checkPidAndHardware(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 803 | if (result != NO_ERROR) return result; |
| 804 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 805 | CameraParameters p(params); |
James Dong | 6085b4e | 2009-09-13 17:10:24 -0700 | [diff] [blame] | 806 | return mHardware->setParameters(p); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | // get preview/capture parameters - key/value pairs |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 810 | String8 CameraService::Client::getParameters() const { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 811 | Mutex::Autolock lock(mLock); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 812 | if (checkPidAndHardware() != NO_ERROR) return String8(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 813 | |
Wu-cheng Li | ab5b424 | 2009-04-22 16:21:26 +0800 | [diff] [blame] | 814 | String8 params(mHardware->getParameters().flatten()); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 815 | LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string()); |
Wu-cheng Li | ab5b424 | 2009-04-22 16:21:26 +0800 | [diff] [blame] | 816 | return params; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 817 | } |
| 818 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 819 | status_t CameraService::Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) { |
| 820 | LOG1("sendCommand (pid %d)", getCallingPid()); |
Wu-cheng Li | b3347bc | 2010-09-23 17:17:43 -0700 | [diff] [blame] | 821 | int orientation; |
Wu-cheng Li | 36f68b8 | 2009-09-28 16:14:58 -0700 | [diff] [blame] | 822 | Mutex::Autolock lock(mLock); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 823 | status_t result = checkPidAndHardware(); |
Wu-cheng Li | 36f68b8 | 2009-09-28 16:14:58 -0700 | [diff] [blame] | 824 | if (result != NO_ERROR) return result; |
| 825 | |
Chih-Chung Chang | d1d7706 | 2010-01-22 17:49:48 -0800 | [diff] [blame] | 826 | if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) { |
| 827 | // The orientation cannot be set during preview. |
| 828 | if (mHardware->previewEnabled()) { |
| 829 | return INVALID_OPERATION; |
| 830 | } |
Wu-cheng Li | b982fb4 | 2010-10-19 17:19:09 +0800 | [diff] [blame^] | 831 | // Mirror the preview if the camera is front-facing. |
| 832 | orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT); |
| 833 | if (orientation == -1) return BAD_VALUE; |
| 834 | |
Wu-cheng Li | b3347bc | 2010-09-23 17:17:43 -0700 | [diff] [blame] | 835 | if (mOrientation != orientation) { |
| 836 | mOrientation = orientation; |
| 837 | if (mOverlayRef != 0) mOrientationChanged = true; |
| 838 | } |
Chih-Chung Chang | d1d7706 | 2010-01-22 17:49:48 -0800 | [diff] [blame] | 839 | return OK; |
| 840 | } |
| 841 | |
Wu-cheng Li | 36f68b8 | 2009-09-28 16:14:58 -0700 | [diff] [blame] | 842 | return mHardware->sendCommand(cmd, arg1, arg2); |
| 843 | } |
| 844 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 845 | // ---------------------------------------------------------------------------- |
| 846 | |
| 847 | void CameraService::Client::enableMsgType(int32_t msgType) { |
| 848 | android_atomic_or(msgType, &mMsgEnabled); |
| 849 | mHardware->enableMsgType(msgType); |
| 850 | } |
| 851 | |
| 852 | void CameraService::Client::disableMsgType(int32_t msgType) { |
| 853 | android_atomic_and(~msgType, &mMsgEnabled); |
| 854 | mHardware->disableMsgType(msgType); |
| 855 | } |
| 856 | |
| 857 | #define CHECK_MESSAGE_INTERVAL 10 // 10ms |
| 858 | bool CameraService::Client::lockIfMessageWanted(int32_t msgType) { |
| 859 | int sleepCount = 0; |
| 860 | while (mMsgEnabled & msgType) { |
| 861 | if (mLock.tryLock() == NO_ERROR) { |
| 862 | if (sleepCount > 0) { |
| 863 | LOG1("lockIfMessageWanted(%d): waited for %d ms", |
| 864 | msgType, sleepCount * CHECK_MESSAGE_INTERVAL); |
| 865 | } |
| 866 | return true; |
| 867 | } |
| 868 | if (sleepCount++ == 0) { |
| 869 | LOG1("lockIfMessageWanted(%d): enter sleep", msgType); |
| 870 | } |
| 871 | usleep(CHECK_MESSAGE_INTERVAL * 1000); |
| 872 | } |
| 873 | LOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType); |
| 874 | return false; |
| 875 | } |
| 876 | |
| 877 | // ---------------------------------------------------------------------------- |
| 878 | |
| 879 | // Converts from a raw pointer to the client to a strong pointer during a |
| 880 | // hardware callback. This requires the callbacks only happen when the client |
| 881 | // is still alive. |
| 882 | sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user) { |
| 883 | sp<Client> client = gCameraService->getClientById((int) user); |
| 884 | |
| 885 | // This could happen if the Client is in the process of shutting down (the |
| 886 | // last strong reference is gone, but the destructor hasn't finished |
| 887 | // stopping the hardware). |
| 888 | if (client == 0) return NULL; |
| 889 | |
| 890 | // The checks below are not necessary and are for debugging only. |
| 891 | if (client->mCameraService.get() != gCameraService) { |
| 892 | LOGE("mismatch service!"); |
| 893 | return NULL; |
| 894 | } |
| 895 | |
| 896 | if (client->mHardware == 0) { |
| 897 | LOGE("mHardware == 0: callback after disconnect()?"); |
| 898 | return NULL; |
| 899 | } |
| 900 | |
| 901 | return client; |
| 902 | } |
| 903 | |
| 904 | // Callback messages can be dispatched to internal handlers or pass to our |
| 905 | // client's callback functions, depending on the message type. |
| 906 | // |
| 907 | // notifyCallback: |
| 908 | // CAMERA_MSG_SHUTTER handleShutter |
| 909 | // (others) c->notifyCallback |
| 910 | // dataCallback: |
| 911 | // CAMERA_MSG_PREVIEW_FRAME handlePreviewData |
| 912 | // CAMERA_MSG_POSTVIEW_FRAME handlePostview |
| 913 | // CAMERA_MSG_RAW_IMAGE handleRawPicture |
| 914 | // CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture |
| 915 | // (others) c->dataCallback |
| 916 | // dataCallbackTimestamp |
| 917 | // (others) c->dataCallbackTimestamp |
| 918 | // |
| 919 | // NOTE: the *Callback functions grab mLock of the client before passing |
| 920 | // control to handle* functions. So the handle* functions must release the |
| 921 | // lock before calling the ICameraClient's callbacks, so those callbacks can |
| 922 | // invoke methods in the Client class again (For example, the preview frame |
| 923 | // callback may want to releaseRecordingFrame). The handle* functions must |
| 924 | // release the lock after all accesses to member variables, so it must be |
| 925 | // handled very carefully. |
| 926 | |
| 927 | void CameraService::Client::notifyCallback(int32_t msgType, int32_t ext1, |
| 928 | int32_t ext2, void* user) { |
| 929 | LOG2("notifyCallback(%d)", msgType); |
| 930 | |
| 931 | sp<Client> client = getClientFromCookie(user); |
| 932 | if (client == 0) return; |
| 933 | if (!client->lockIfMessageWanted(msgType)) return; |
| 934 | |
| 935 | switch (msgType) { |
| 936 | case CAMERA_MSG_SHUTTER: |
| 937 | // ext1 is the dimension of the yuv picture. |
| 938 | client->handleShutter((image_rect_type *)ext1); |
| 939 | break; |
| 940 | default: |
| 941 | client->handleGenericNotify(msgType, ext1, ext2); |
| 942 | break; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | void CameraService::Client::dataCallback(int32_t msgType, |
| 947 | const sp<IMemory>& dataPtr, void* user) { |
| 948 | LOG2("dataCallback(%d)", msgType); |
| 949 | |
| 950 | sp<Client> client = getClientFromCookie(user); |
| 951 | if (client == 0) return; |
| 952 | if (!client->lockIfMessageWanted(msgType)) return; |
| 953 | |
| 954 | if (dataPtr == 0) { |
| 955 | LOGE("Null data returned in data callback"); |
| 956 | client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0); |
| 957 | return; |
| 958 | } |
| 959 | |
| 960 | switch (msgType) { |
| 961 | case CAMERA_MSG_PREVIEW_FRAME: |
| 962 | client->handlePreviewData(dataPtr); |
| 963 | break; |
| 964 | case CAMERA_MSG_POSTVIEW_FRAME: |
| 965 | client->handlePostview(dataPtr); |
| 966 | break; |
| 967 | case CAMERA_MSG_RAW_IMAGE: |
| 968 | client->handleRawPicture(dataPtr); |
| 969 | break; |
| 970 | case CAMERA_MSG_COMPRESSED_IMAGE: |
| 971 | client->handleCompressedPicture(dataPtr); |
| 972 | break; |
| 973 | default: |
| 974 | client->handleGenericData(msgType, dataPtr); |
| 975 | break; |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | void CameraService::Client::dataCallbackTimestamp(nsecs_t timestamp, |
| 980 | int32_t msgType, const sp<IMemory>& dataPtr, void* user) { |
| 981 | LOG2("dataCallbackTimestamp(%d)", msgType); |
| 982 | |
| 983 | sp<Client> client = getClientFromCookie(user); |
| 984 | if (client == 0) return; |
| 985 | if (!client->lockIfMessageWanted(msgType)) return; |
| 986 | |
| 987 | if (dataPtr == 0) { |
| 988 | LOGE("Null data returned in data with timestamp callback"); |
| 989 | client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0); |
| 990 | return; |
| 991 | } |
| 992 | |
| 993 | client->handleGenericDataTimestamp(timestamp, msgType, dataPtr); |
| 994 | } |
| 995 | |
| 996 | // snapshot taken callback |
| 997 | // "size" is the width and height of yuv picture for registerBuffer. |
| 998 | // If it is NULL, use the picture size from parameters. |
| 999 | void CameraService::Client::handleShutter(image_rect_type *size) { |
| 1000 | mCameraService->playSound(SOUND_SHUTTER); |
| 1001 | |
| 1002 | // Screen goes black after the buffer is unregistered. |
| 1003 | if (mSurface != 0 && !mUseOverlay) { |
| 1004 | mSurface->unregisterBuffers(); |
| 1005 | } |
| 1006 | |
| 1007 | sp<ICameraClient> c = mCameraClient; |
| 1008 | if (c != 0) { |
| 1009 | mLock.unlock(); |
| 1010 | c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0); |
| 1011 | if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return; |
| 1012 | } |
| 1013 | disableMsgType(CAMERA_MSG_SHUTTER); |
| 1014 | |
| 1015 | // It takes some time before yuvPicture callback to be called. |
| 1016 | // Register the buffer for raw image here to reduce latency. |
| 1017 | if (mSurface != 0 && !mUseOverlay) { |
| 1018 | int w, h; |
| 1019 | CameraParameters params(mHardware->getParameters()); |
| 1020 | if (size == NULL) { |
| 1021 | params.getPictureSize(&w, &h); |
| 1022 | } else { |
| 1023 | w = size->width; |
| 1024 | h = size->height; |
| 1025 | w &= ~1; |
| 1026 | h &= ~1; |
| 1027 | LOG1("Snapshot image width=%d, height=%d", w, h); |
| 1028 | } |
| 1029 | // FIXME: don't use hardcoded format constants here |
| 1030 | ISurface::BufferHeap buffers(w, h, w, h, |
| 1031 | HAL_PIXEL_FORMAT_YCrCb_420_SP, mOrientation, 0, |
| 1032 | mHardware->getRawHeap()); |
| 1033 | |
| 1034 | mSurface->registerBuffers(buffers); |
Chih-Chung Chang | 3ef6ebe | 2010-07-02 07:48:03 -0700 | [diff] [blame] | 1035 | IPCThreadState::self()->flushCommands(); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | mLock.unlock(); |
| 1039 | } |
| 1040 | |
| 1041 | // preview callback - frame buffer update |
| 1042 | void CameraService::Client::handlePreviewData(const sp<IMemory>& mem) { |
| 1043 | ssize_t offset; |
| 1044 | size_t size; |
| 1045 | sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); |
| 1046 | |
| 1047 | if (!mUseOverlay) { |
| 1048 | if (mSurface != 0) { |
| 1049 | mSurface->postBuffer(offset); |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | // local copy of the callback flags |
| 1054 | int flags = mPreviewCallbackFlag; |
| 1055 | |
| 1056 | // is callback enabled? |
| 1057 | if (!(flags & FRAME_CALLBACK_FLAG_ENABLE_MASK)) { |
| 1058 | // If the enable bit is off, the copy-out and one-shot bits are ignored |
| 1059 | LOG2("frame callback is disabled"); |
| 1060 | mLock.unlock(); |
| 1061 | return; |
| 1062 | } |
| 1063 | |
| 1064 | // hold a strong pointer to the client |
| 1065 | sp<ICameraClient> c = mCameraClient; |
| 1066 | |
| 1067 | // clear callback flags if no client or one-shot mode |
| 1068 | if (c == 0 || (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) { |
| 1069 | LOG2("Disable preview callback"); |
| 1070 | mPreviewCallbackFlag &= ~(FRAME_CALLBACK_FLAG_ONE_SHOT_MASK | |
| 1071 | FRAME_CALLBACK_FLAG_COPY_OUT_MASK | |
| 1072 | FRAME_CALLBACK_FLAG_ENABLE_MASK); |
| 1073 | if (mUseOverlay) { |
| 1074 | disableMsgType(CAMERA_MSG_PREVIEW_FRAME); |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | if (c != 0) { |
| 1079 | // Is the received frame copied out or not? |
| 1080 | if (flags & FRAME_CALLBACK_FLAG_COPY_OUT_MASK) { |
| 1081 | LOG2("frame is copied"); |
| 1082 | copyFrameAndPostCopiedFrame(c, heap, offset, size); |
| 1083 | } else { |
| 1084 | LOG2("frame is forwarded"); |
| 1085 | mLock.unlock(); |
| 1086 | c->dataCallback(CAMERA_MSG_PREVIEW_FRAME, mem); |
| 1087 | } |
| 1088 | } else { |
| 1089 | mLock.unlock(); |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | // picture callback - postview image ready |
| 1094 | void CameraService::Client::handlePostview(const sp<IMemory>& mem) { |
| 1095 | disableMsgType(CAMERA_MSG_POSTVIEW_FRAME); |
| 1096 | |
| 1097 | sp<ICameraClient> c = mCameraClient; |
| 1098 | mLock.unlock(); |
| 1099 | if (c != 0) { |
| 1100 | c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem); |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | // picture callback - raw image ready |
| 1105 | void CameraService::Client::handleRawPicture(const sp<IMemory>& mem) { |
| 1106 | disableMsgType(CAMERA_MSG_RAW_IMAGE); |
| 1107 | |
| 1108 | ssize_t offset; |
| 1109 | size_t size; |
| 1110 | sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); |
| 1111 | |
| 1112 | // Put the YUV version of the snapshot in the preview display. |
| 1113 | if (mSurface != 0 && !mUseOverlay) { |
| 1114 | mSurface->postBuffer(offset); |
| 1115 | } |
| 1116 | |
| 1117 | sp<ICameraClient> c = mCameraClient; |
| 1118 | mLock.unlock(); |
| 1119 | if (c != 0) { |
| 1120 | c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem); |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | // picture callback - compressed picture ready |
| 1125 | void CameraService::Client::handleCompressedPicture(const sp<IMemory>& mem) { |
| 1126 | disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE); |
| 1127 | |
| 1128 | sp<ICameraClient> c = mCameraClient; |
| 1129 | mLock.unlock(); |
| 1130 | if (c != 0) { |
| 1131 | c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem); |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | |
| 1136 | void CameraService::Client::handleGenericNotify(int32_t msgType, |
| 1137 | int32_t ext1, int32_t ext2) { |
| 1138 | sp<ICameraClient> c = mCameraClient; |
| 1139 | mLock.unlock(); |
| 1140 | if (c != 0) { |
| 1141 | c->notifyCallback(msgType, ext1, ext2); |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | void CameraService::Client::handleGenericData(int32_t msgType, |
| 1146 | const sp<IMemory>& dataPtr) { |
| 1147 | sp<ICameraClient> c = mCameraClient; |
| 1148 | mLock.unlock(); |
| 1149 | if (c != 0) { |
| 1150 | c->dataCallback(msgType, dataPtr); |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | void CameraService::Client::handleGenericDataTimestamp(nsecs_t timestamp, |
| 1155 | int32_t msgType, const sp<IMemory>& dataPtr) { |
| 1156 | sp<ICameraClient> c = mCameraClient; |
| 1157 | mLock.unlock(); |
| 1158 | if (c != 0) { |
| 1159 | c->dataCallbackTimestamp(timestamp, msgType, dataPtr); |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | void CameraService::Client::copyFrameAndPostCopiedFrame( |
| 1164 | const sp<ICameraClient>& client, const sp<IMemoryHeap>& heap, |
| 1165 | size_t offset, size_t size) { |
| 1166 | LOG2("copyFrameAndPostCopiedFrame"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1167 | // It is necessary to copy out of pmem before sending this to |
| 1168 | // the callback. For efficiency, reuse the same MemoryHeapBase |
| 1169 | // provided it's big enough. Don't allocate the memory or |
| 1170 | // perform the copy if there's no callback. |
Dave Sparks | 05fd0df | 2009-11-10 17:08:08 -0800 | [diff] [blame] | 1171 | // hold the preview lock while we grab a reference to the preview buffer |
Dave Sparks | c8093c1 | 2009-11-06 11:47:13 -0800 | [diff] [blame] | 1172 | sp<MemoryHeapBase> previewBuffer; |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 1173 | |
| 1174 | if (mPreviewBuffer == 0) { |
| 1175 | mPreviewBuffer = new MemoryHeapBase(size, 0, NULL); |
| 1176 | } else if (size > mPreviewBuffer->virtualSize()) { |
| 1177 | mPreviewBuffer.clear(); |
| 1178 | mPreviewBuffer = new MemoryHeapBase(size, 0, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1179 | } |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 1180 | if (mPreviewBuffer == 0) { |
| 1181 | LOGE("failed to allocate space for preview buffer"); |
| 1182 | mLock.unlock(); |
| 1183 | return; |
| 1184 | } |
| 1185 | previewBuffer = mPreviewBuffer; |
| 1186 | |
| 1187 | memcpy(previewBuffer->base(), (uint8_t *)heap->base() + offset, size); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1188 | |
Dave Sparks | c8093c1 | 2009-11-06 11:47:13 -0800 | [diff] [blame] | 1189 | sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1190 | if (frame == 0) { |
| 1191 | LOGE("failed to allocate space for frame callback"); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 1192 | mLock.unlock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1193 | return; |
| 1194 | } |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 1195 | |
| 1196 | mLock.unlock(); |
Dave Sparks | dd158c9 | 2009-10-15 10:02:22 -0700 | [diff] [blame] | 1197 | client->dataCallback(CAMERA_MSG_PREVIEW_FRAME, frame); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1198 | } |
| 1199 | |
Wu-cheng Li | b982fb4 | 2010-10-19 17:19:09 +0800 | [diff] [blame^] | 1200 | int CameraService::Client::getOrientation(int degrees, bool mirror) { |
| 1201 | if (!mirror) { |
| 1202 | if (degrees == 0) return 0; |
| 1203 | else if (degrees == 90) return HAL_TRANSFORM_ROT_90; |
| 1204 | else if (degrees == 180) return HAL_TRANSFORM_ROT_180; |
| 1205 | else if (degrees == 270) return HAL_TRANSFORM_ROT_270; |
| 1206 | } else { // mirror (horizontal flip) |
| 1207 | // Now overlay does ROT_90 before FLIP_V or FLIP_H. It should be FLIP_V |
| 1208 | // or FLIP_H first. |
| 1209 | // TODO: change this after overlay is fixed. |
| 1210 | if (degrees == 0) { // FLIP_H and ROT_0 |
| 1211 | return HAL_TRANSFORM_FLIP_H; |
| 1212 | } else if (degrees == 90) { // FLIP_H and ROT_90 |
| 1213 | return HAL_TRANSFORM_ROT_90 | HAL_TRANSFORM_FLIP_V; |
| 1214 | } else if (degrees == 180) { // FLIP_H and ROT_180 |
| 1215 | return HAL_TRANSFORM_FLIP_V; |
| 1216 | } else if (degrees == 270) { // FLIP_H and ROT_270 |
| 1217 | return HAL_TRANSFORM_ROT_90 | HAL_TRANSFORM_FLIP_H; |
| 1218 | } |
| 1219 | } |
| 1220 | LOGE("Invalid setDisplayOrientation degrees=%d", degrees); |
| 1221 | return -1; |
| 1222 | } |
| 1223 | |
| 1224 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 1225 | // ---------------------------------------------------------------------------- |
| 1226 | |
Chih-Chung Chang | e86ae1b | 2010-03-05 11:33:03 -0800 | [diff] [blame] | 1227 | static const int kDumpLockRetries = 50; |
| 1228 | static const int kDumpLockSleep = 60000; |
| 1229 | |
| 1230 | static bool tryLock(Mutex& mutex) |
| 1231 | { |
| 1232 | bool locked = false; |
| 1233 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 1234 | if (mutex.tryLock() == NO_ERROR) { |
| 1235 | locked = true; |
| 1236 | break; |
| 1237 | } |
| 1238 | usleep(kDumpLockSleep); |
| 1239 | } |
| 1240 | return locked; |
| 1241 | } |
| 1242 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 1243 | status_t CameraService::dump(int fd, const Vector<String16>& args) { |
Chih-Chung Chang | e86ae1b | 2010-03-05 11:33:03 -0800 | [diff] [blame] | 1244 | static const char* kDeadlockedString = "CameraService may be deadlocked\n"; |
| 1245 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1246 | const size_t SIZE = 256; |
| 1247 | char buffer[SIZE]; |
| 1248 | String8 result; |
| 1249 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 1250 | snprintf(buffer, SIZE, "Permission Denial: " |
| 1251 | "can't dump CameraService from pid=%d, uid=%d\n", |
Chih-Chung Chang | 1f25ec8 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 1252 | getCallingPid(), |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 1253 | getCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1254 | result.append(buffer); |
| 1255 | write(fd, result.string(), result.size()); |
| 1256 | } else { |
Chih-Chung Chang | e86ae1b | 2010-03-05 11:33:03 -0800 | [diff] [blame] | 1257 | bool locked = tryLock(mServiceLock); |
| 1258 | // failed to lock - CameraService is probably deadlocked |
| 1259 | if (!locked) { |
| 1260 | String8 result(kDeadlockedString); |
| 1261 | write(fd, result.string(), result.size()); |
| 1262 | } |
| 1263 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 1264 | bool hasClient = false; |
Chih-Chung Chang | b8bb78f | 2010-06-10 13:32:16 +0800 | [diff] [blame] | 1265 | for (int i = 0; i < mNumberOfCameras; i++) { |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 1266 | sp<Client> client = mClient[i].promote(); |
| 1267 | if (client == 0) continue; |
| 1268 | hasClient = true; |
| 1269 | sprintf(buffer, "Client[%d] (%p) PID: %d\n", |
| 1270 | i, |
| 1271 | client->getCameraClient()->asBinder().get(), |
| 1272 | client->mClientPid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1273 | result.append(buffer); |
| 1274 | write(fd, result.string(), result.size()); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 1275 | client->mHardware->dump(fd, args); |
| 1276 | } |
| 1277 | if (!hasClient) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1278 | result.append("No camera client yet.\n"); |
| 1279 | write(fd, result.string(), result.size()); |
| 1280 | } |
Chih-Chung Chang | e86ae1b | 2010-03-05 11:33:03 -0800 | [diff] [blame] | 1281 | |
| 1282 | if (locked) mServiceLock.unlock(); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 1283 | |
| 1284 | // change logging level |
| 1285 | int n = args.size(); |
| 1286 | for (int i = 0; i + 1 < n; i++) { |
| 1287 | if (args[i] == String16("-v")) { |
| 1288 | String8 levelStr(args[i+1]); |
| 1289 | int level = atoi(levelStr.string()); |
| 1290 | sprintf(buffer, "Set Log Level to %d", level); |
| 1291 | result.append(buffer); |
| 1292 | setLogLevel(level); |
| 1293 | } |
| 1294 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1295 | } |
| 1296 | return NO_ERROR; |
| 1297 | } |
| 1298 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1299 | }; // namespace android |