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