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