The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright (C) 2008, The Android Open Source Project |
| 4 | ** Copyright (C) 2008 HTC Inc. |
| 5 | ** |
| 6 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | ** you may not use this file except in compliance with the License. |
| 8 | ** You may obtain a copy of the License at |
| 9 | ** |
| 10 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | ** |
| 12 | ** Unless required by applicable law or agreed to in writing, software |
| 13 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | ** See the License for the specific language governing permissions and |
| 16 | ** limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | #define LOG_TAG "CameraService" |
| 21 | #include <utils/Log.h> |
| 22 | |
| 23 | #include <utils/IServiceManager.h> |
| 24 | #include <utils/IPCThreadState.h> |
| 25 | #include <utils/String16.h> |
| 26 | #include <utils/Errors.h> |
| 27 | #include <utils/MemoryBase.h> |
| 28 | #include <utils/MemoryHeapBase.h> |
| 29 | #include <ui/ICameraService.h> |
| 30 | |
Jason Sams | 78b877e | 2009-03-24 20:21:36 -0700 | [diff] [blame] | 31 | #include <media/mediaplayer.h> |
| 32 | #include <media/AudioSystem.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | #include "CameraService.h" |
| 34 | |
Chih-Chung Chang | fa89f9f | 2009-06-24 13:44:37 +0800 | [diff] [blame] | 35 | #include <cutils/atomic.h> |
Eric Laurent | cbcb00e | 2009-03-27 16:27:16 -0700 | [diff] [blame] | 36 | #include <cutils/properties.h> |
| 37 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | namespace android { |
| 39 | |
| 40 | extern "C" { |
| 41 | #include <stdio.h> |
| 42 | #include <sys/types.h> |
| 43 | #include <sys/stat.h> |
| 44 | #include <fcntl.h> |
| 45 | #include <pthread.h> |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 46 | #include <signal.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // When you enable this, as well as DEBUG_REFS=1 and |
| 50 | // DEBUG_REFS_ENABLED_BY_DEFAULT=0 in libutils/RefBase.cpp, this will track all |
| 51 | // references to the CameraService::Client in order to catch the case where the |
| 52 | // client is being destroyed while a callback from the CameraHardwareInterface |
| 53 | // is outstanding. This is a serious bug because if we make another call into |
| 54 | // CameraHardwreInterface that itself triggers a callback, we will deadlock. |
| 55 | |
| 56 | #define DEBUG_CLIENT_REFERENCES 0 |
| 57 | |
| 58 | #define PICTURE_TIMEOUT seconds(5) |
| 59 | |
| 60 | #define DEBUG_DUMP_PREVIEW_FRAME_TO_FILE 0 /* n-th frame to write */ |
| 61 | #define DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE 0 |
| 62 | #define DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE 0 |
| 63 | |
| 64 | #if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE |
| 65 | static int debug_frame_cnt; |
| 66 | #endif |
| 67 | |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 68 | static int getCallingPid() { |
| 69 | return IPCThreadState::self()->getCallingPid(); |
| 70 | } |
| 71 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | // ---------------------------------------------------------------------------- |
| 73 | |
| 74 | void CameraService::instantiate() { |
| 75 | defaultServiceManager()->addService( |
| 76 | String16("media.camera"), new CameraService()); |
| 77 | } |
| 78 | |
| 79 | // ---------------------------------------------------------------------------- |
| 80 | |
| 81 | CameraService::CameraService() : |
| 82 | BnCameraService() |
| 83 | { |
| 84 | LOGI("CameraService started: pid=%d", getpid()); |
Chih-Chung Chang | fa89f9f | 2009-06-24 13:44:37 +0800 | [diff] [blame] | 85 | mUsers = 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | CameraService::~CameraService() |
| 89 | { |
| 90 | if (mClient != 0) { |
| 91 | LOGE("mClient was still connected in destructor!"); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | sp<ICamera> CameraService::connect(const sp<ICameraClient>& cameraClient) |
| 96 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 97 | int callingPid = getCallingPid(); |
| 98 | LOGD("CameraService::connect E (pid %d, client %p)", callingPid, |
| 99 | cameraClient->asBinder().get()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | |
| 101 | Mutex::Autolock lock(mLock); |
| 102 | sp<Client> client; |
| 103 | if (mClient != 0) { |
| 104 | sp<Client> currentClient = mClient.promote(); |
| 105 | if (currentClient != 0) { |
| 106 | sp<ICameraClient> currentCameraClient(currentClient->getCameraClient()); |
| 107 | if (cameraClient->asBinder() == currentCameraClient->asBinder()) { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 108 | // This is the same client reconnecting... |
| 109 | LOGD("CameraService::connect X (pid %d, same client %p) is reconnecting...", |
| 110 | callingPid, cameraClient->asBinder().get()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | return currentClient; |
| 112 | } else { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 113 | // It's another client... reject it |
| 114 | LOGD("CameraService::connect X (pid %d, new client %p) rejected. " |
| 115 | "(old pid %d, old client %p)", |
| 116 | callingPid, cameraClient->asBinder().get(), |
| 117 | currentClient->mClientPid, currentCameraClient->asBinder().get()); |
Chih-Chung Chang | fa89f9f | 2009-06-24 13:44:37 +0800 | [diff] [blame] | 118 | if (kill(currentClient->mClientPid, 0) == -1 && errno == ESRCH) { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 119 | LOGD("The old client is dead!"); |
| 120 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | return client; |
| 122 | } |
| 123 | } else { |
| 124 | // can't promote, the previous client has died... |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 125 | LOGD("New client (pid %d) connecting, old reference was dangling...", |
| 126 | callingPid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | mClient.clear(); |
Chih-Chung Chang | fa89f9f | 2009-06-24 13:44:37 +0800 | [diff] [blame] | 128 | if (mUsers > 0) { |
| 129 | LOGD("Still have client, rejected"); |
| 130 | return client; |
| 131 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
| 135 | // create a new Client object |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 136 | client = new Client(this, cameraClient, callingPid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | mClient = client; |
| 138 | #if DEBUG_CLIENT_REFERENCES |
| 139 | // Enable tracking for this object, and track increments and decrements of |
| 140 | // the refcount. |
| 141 | client->trackMe(true, true); |
| 142 | #endif |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 143 | LOGD("CameraService::connect X"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | return client; |
| 145 | } |
| 146 | |
| 147 | void CameraService::removeClient(const sp<ICameraClient>& cameraClient) |
| 148 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 149 | int callingPid = getCallingPid(); |
| 150 | |
| 151 | // Declare this outside the lock to make absolutely sure the |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | // destructor won't be called with the lock held. |
| 153 | sp<Client> client; |
| 154 | |
| 155 | Mutex::Autolock lock(mLock); |
| 156 | |
| 157 | if (mClient == 0) { |
| 158 | // This happens when we have already disconnected. |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 159 | LOGD("removeClient (pid %d): already disconnected", callingPid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | return; |
| 161 | } |
| 162 | |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 163 | // Promote mClient. It can fail if we are called from this path: |
| 164 | // Client::~Client() -> disconnect() -> removeClient(). |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | client = mClient.promote(); |
| 166 | if (client == 0) { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 167 | LOGD("removeClient (pid %d): no more strong reference", callingPid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | mClient.clear(); |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | if (cameraClient->asBinder() != client->getCameraClient()->asBinder()) { |
| 173 | // ugh! that's not our client!! |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 174 | LOGW("removeClient (pid %d): mClient doesn't match!", callingPid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | } else { |
| 176 | // okay, good, forget about mClient |
| 177 | mClient.clear(); |
| 178 | } |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 179 | |
| 180 | LOGD("removeClient (pid %d) done", callingPid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Chih-Chung Chang | fa89f9f | 2009-06-24 13:44:37 +0800 | [diff] [blame] | 183 | // The reason we need this count is a new CameraService::connect() request may |
| 184 | // come in while the previous Client's destructor has not been run or is still |
| 185 | // running. If the last strong reference of the previous Client is gone but |
| 186 | // destructor has not been run, we should not allow the new Client to be created |
| 187 | // because we need to wait for the previous Client to tear down the hardware |
| 188 | // first. |
| 189 | void CameraService::incUsers() { |
| 190 | android_atomic_inc(&mUsers); |
| 191 | } |
| 192 | |
| 193 | void CameraService::decUsers() { |
| 194 | android_atomic_dec(&mUsers); |
| 195 | } |
| 196 | |
Jason Sams | 78b877e | 2009-03-24 20:21:36 -0700 | [diff] [blame] | 197 | static sp<MediaPlayer> newMediaPlayer(const char *file) |
| 198 | { |
| 199 | sp<MediaPlayer> mp = new MediaPlayer(); |
| 200 | if (mp->setDataSource(file) == NO_ERROR) { |
Eric Laurent | cbcb00e | 2009-03-27 16:27:16 -0700 | [diff] [blame] | 201 | char value[PROPERTY_VALUE_MAX]; |
| 202 | property_get("ro.camera.sound.forced", value, "0"); |
| 203 | if (atoi(value)) { |
| 204 | mp->setAudioStreamType(AudioSystem::ENFORCED_AUDIBLE); |
| 205 | } else { |
| 206 | mp->setAudioStreamType(AudioSystem::SYSTEM); |
| 207 | } |
Jason Sams | 78b877e | 2009-03-24 20:21:36 -0700 | [diff] [blame] | 208 | mp->prepare(); |
| 209 | } else { |
| 210 | mp.clear(); |
| 211 | LOGE("Failed to load CameraService sounds."); |
| 212 | } |
| 213 | return mp; |
| 214 | } |
| 215 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | CameraService::Client::Client(const sp<CameraService>& cameraService, |
| 217 | const sp<ICameraClient>& cameraClient, pid_t clientPid) |
| 218 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 219 | int callingPid = getCallingPid(); |
| 220 | LOGD("Client::Client E (pid %d)", callingPid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | mCameraService = cameraService; |
| 222 | mCameraClient = cameraClient; |
| 223 | mClientPid = clientPid; |
| 224 | mHardware = openCameraHardware(); |
| 225 | mUseOverlay = mHardware->useOverlay(); |
| 226 | |
Jason Sams | 78b877e | 2009-03-24 20:21:36 -0700 | [diff] [blame] | 227 | mMediaPlayerClick = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); |
| 228 | mMediaPlayerBeep = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); |
| 229 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | // Callback is disabled by default |
| 231 | mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP; |
Chih-Chung Chang | fa89f9f | 2009-06-24 13:44:37 +0800 | [diff] [blame] | 232 | cameraService->incUsers(); |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 233 | LOGD("Client::Client X (pid %d)", callingPid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | status_t CameraService::Client::checkPid() |
| 237 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 238 | int callingPid = getCallingPid(); |
| 239 | if (mClientPid == callingPid) return NO_ERROR; |
| 240 | LOGW("Attempt to use locked camera (client %p) from different process " |
| 241 | " (old pid %d, new pid %d)", |
| 242 | getCameraClient()->asBinder().get(), mClientPid, callingPid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | return -EBUSY; |
| 244 | } |
| 245 | |
| 246 | status_t CameraService::Client::lock() |
| 247 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 248 | int callingPid = getCallingPid(); |
| 249 | LOGD("lock from pid %d (mClientPid %d)", callingPid, mClientPid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | Mutex::Autolock _l(mLock); |
| 251 | // lock camera to this client if the the camera is unlocked |
| 252 | if (mClientPid == 0) { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 253 | mClientPid = callingPid; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | return NO_ERROR; |
| 255 | } |
| 256 | // returns NO_ERROR if the client already owns the camera, -EBUSY otherwise |
| 257 | return checkPid(); |
| 258 | } |
| 259 | |
| 260 | status_t CameraService::Client::unlock() |
| 261 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 262 | int callingPid = getCallingPid(); |
| 263 | LOGD("unlock from pid %d (mClientPid %d)", callingPid, mClientPid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | Mutex::Autolock _l(mLock); |
| 265 | // allow anyone to use camera |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | status_t result = checkPid(); |
James Dong | 0ae13e3 | 2009-04-20 19:35:28 -0700 | [diff] [blame] | 267 | if (result == NO_ERROR) { |
| 268 | mClientPid = 0; |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 269 | LOGD("clear mCameraClient (pid %d)", callingPid); |
James Dong | 0ae13e3 | 2009-04-20 19:35:28 -0700 | [diff] [blame] | 270 | // we need to remove the reference so that when app goes |
| 271 | // away, the reference count goes to 0. |
| 272 | mCameraClient.clear(); |
| 273 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | return result; |
| 275 | } |
| 276 | |
| 277 | status_t CameraService::Client::connect(const sp<ICameraClient>& client) |
| 278 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 279 | int callingPid = getCallingPid(); |
| 280 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | // connect a new process to the camera |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 282 | LOGD("Client::connect E (pid %d, client %p)", callingPid, client->asBinder().get()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | |
| 284 | // I hate this hack, but things get really ugly when the media recorder |
| 285 | // service is handing back the camera to the app. The ICameraClient |
| 286 | // destructor will be called during the same IPC, making it look like |
| 287 | // the remote client is trying to disconnect. This hack temporarily |
| 288 | // sets the mClientPid to an invalid pid to prevent the hardware from |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 289 | // being torn down. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | { |
| 291 | |
| 292 | // hold a reference to the old client or we will deadlock if the client is |
| 293 | // in the same process and we hold the lock when we remove the reference |
| 294 | sp<ICameraClient> oldClient; |
| 295 | { |
| 296 | Mutex::Autolock _l(mLock); |
Chih-Chung Chang | 34e5a15 | 2009-06-09 13:56:44 +0800 | [diff] [blame] | 297 | if (mClientPid != 0 && checkPid() != NO_ERROR) { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 298 | LOGW("Tried to connect to locked camera (old pid %d, new pid %d)", |
| 299 | mClientPid, callingPid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | return -EBUSY; |
| 301 | } |
| 302 | oldClient = mCameraClient; |
| 303 | |
| 304 | // did the client actually change? |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 305 | if (client->asBinder() == mCameraClient->asBinder()) { |
| 306 | LOGD("Connect to the same client"); |
| 307 | return NO_ERROR; |
| 308 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | |
| 310 | mCameraClient = client; |
| 311 | mClientPid = -1; |
| 312 | mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP; |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 313 | LOGD("Connect to the new client (pid %d, client %p)", |
| 314 | callingPid, mCameraClient->asBinder().get()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | } |
| 318 | // the old client destructor is called when oldClient goes out of scope |
| 319 | // now we set the new PID to lock the interface again |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 320 | mClientPid = callingPid; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 321 | |
| 322 | return NO_ERROR; |
| 323 | } |
| 324 | |
| 325 | #if HAVE_ANDROID_OS |
| 326 | static void *unregister_surface(void *arg) |
| 327 | { |
| 328 | ISurface *surface = (ISurface *)arg; |
| 329 | surface->unregisterBuffers(); |
| 330 | IPCThreadState::self()->flushCommands(); |
| 331 | return NULL; |
| 332 | } |
| 333 | #endif |
| 334 | |
| 335 | CameraService::Client::~Client() |
| 336 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 337 | int callingPid = getCallingPid(); |
| 338 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | // tear down client |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 340 | LOGD("Client::~Client E (pid %d, client %p)", |
| 341 | callingPid, getCameraClient()->asBinder().get()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | if (mSurface != 0 && !mUseOverlay) { |
| 343 | #if HAVE_ANDROID_OS |
| 344 | pthread_t thr; |
| 345 | // We unregister the buffers in a different thread because binder does |
| 346 | // not let us make sychronous transactions in a binder destructor (that |
| 347 | // is, upon our reaching a refcount of zero.) |
| 348 | pthread_create(&thr, NULL, |
| 349 | unregister_surface, |
| 350 | mSurface.get()); |
| 351 | pthread_join(thr, NULL); |
| 352 | #else |
| 353 | mSurface->unregisterBuffers(); |
| 354 | #endif |
| 355 | } |
| 356 | |
Jason Sams | 9e431ac | 2009-03-24 20:36:57 -0700 | [diff] [blame] | 357 | if (mMediaPlayerBeep.get() != NULL) { |
| 358 | mMediaPlayerBeep->disconnect(); |
| 359 | mMediaPlayerBeep.clear(); |
| 360 | } |
| 361 | if (mMediaPlayerClick.get() != NULL) { |
| 362 | mMediaPlayerClick->disconnect(); |
| 363 | mMediaPlayerClick.clear(); |
| 364 | } |
Jason Sams | 78b877e | 2009-03-24 20:21:36 -0700 | [diff] [blame] | 365 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | // make sure we tear down the hardware |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 367 | mClientPid = callingPid; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | disconnect(); |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 369 | LOGD("Client::~Client X (pid %d)", mClientPid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | void CameraService::Client::disconnect() |
| 373 | { |
Chih-Chung Chang | fa89f9f | 2009-06-24 13:44:37 +0800 | [diff] [blame] | 374 | int callingPid = getCallingPid(); |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 375 | |
| 376 | LOGD("Client::disconnect() E (pid %d client %p)", |
| 377 | callingPid, getCameraClient()->asBinder().get()); |
| 378 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | Mutex::Autolock lock(mLock); |
| 380 | if (mClientPid <= 0) { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 381 | LOGD("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | return; |
| 383 | } |
| 384 | if (checkPid() != NO_ERROR) { |
Chih-Chung Chang | 6a5297d | 2009-06-16 17:15:04 +0800 | [diff] [blame] | 385 | LOGD("Different client - don't disconnect"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | return; |
| 387 | } |
| 388 | |
Chih-Chung Chang | fa89f9f | 2009-06-24 13:44:37 +0800 | [diff] [blame] | 389 | // Make sure disconnect() is done once and once only, whether it is called |
| 390 | // from the user directly, or called by the destructor. |
| 391 | if (mHardware == 0) return; |
| 392 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | mCameraService->removeClient(mCameraClient); |
Chih-Chung Chang | fa89f9f | 2009-06-24 13:44:37 +0800 | [diff] [blame] | 394 | |
| 395 | LOGD("hardware teardown"); |
| 396 | // Before destroying mHardware, we must make sure it's in the |
| 397 | // idle state. |
| 398 | mHardware->stopPreview(); |
| 399 | // Cancel all picture callbacks. |
| 400 | mHardware->cancelPicture(true, true, true); |
| 401 | // Release the hardware resources. |
| 402 | mHardware->release(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | mHardware.clear(); |
Chih-Chung Chang | fa89f9f | 2009-06-24 13:44:37 +0800 | [diff] [blame] | 404 | |
| 405 | mCameraService->decUsers(); |
| 406 | |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 407 | LOGD("Client::disconnect() X (pid %d)", callingPid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | // pass the buffered ISurface to the camera service |
| 411 | status_t CameraService::Client::setPreviewDisplay(const sp<ISurface>& surface) |
| 412 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 413 | LOGD("setPreviewDisplay(%p) (pid %d)", surface.get(), getCallingPid()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | Mutex::Autolock lock(mLock); |
| 415 | status_t result = checkPid(); |
| 416 | if (result != NO_ERROR) return result; |
| 417 | Mutex::Autolock surfaceLock(mSurfaceLock); |
| 418 | // asBinder() is safe on NULL (returns NULL) |
| 419 | if (surface->asBinder() != mSurface->asBinder()) { |
| 420 | if (mSurface != 0 && !mUseOverlay) { |
| 421 | LOGD("clearing old preview surface %p", mSurface.get()); |
| 422 | mSurface->unregisterBuffers(); |
| 423 | } |
| 424 | mSurface = surface; |
| 425 | } |
| 426 | return NO_ERROR; |
| 427 | } |
| 428 | |
| 429 | // set the preview callback flag to affect how the received frames from |
| 430 | // preview are handled. |
| 431 | void CameraService::Client::setPreviewCallbackFlag(int callback_flag) |
| 432 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 433 | LOGV("setPreviewCallbackFlag (pid %d)", getCallingPid()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | Mutex::Autolock lock(mLock); |
| 435 | if (checkPid() != NO_ERROR) return; |
| 436 | mPreviewCallbackFlag = callback_flag; |
| 437 | } |
| 438 | |
| 439 | // start preview mode, must call setPreviewDisplay first |
| 440 | status_t CameraService::Client::startCameraMode(camera_mode mode) |
| 441 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 442 | int callingPid = getCallingPid(); |
| 443 | |
| 444 | LOGD("startCameraMode(%d) (pid %d)", mode, callingPid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | |
| 446 | /* we cannot call into mHardware with mLock held because |
| 447 | * mHardware has callbacks onto us which acquire this lock |
| 448 | */ |
| 449 | |
| 450 | Mutex::Autolock lock(mLock); |
| 451 | status_t result = checkPid(); |
| 452 | if (result != NO_ERROR) return result; |
| 453 | |
| 454 | if (mHardware == 0) { |
| 455 | LOGE("mHardware is NULL, returning."); |
| 456 | return INVALID_OPERATION; |
| 457 | } |
| 458 | |
| 459 | if (mSurface == 0) { |
| 460 | LOGE("setPreviewDisplay must be called before startCameraMode!"); |
| 461 | return INVALID_OPERATION; |
| 462 | } |
| 463 | |
| 464 | switch(mode) { |
| 465 | case CAMERA_RECORDING_MODE: |
| 466 | return startRecordingMode(); |
| 467 | |
| 468 | default: // CAMERA_PREVIEW_MODE |
| 469 | return startPreviewMode(); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | status_t CameraService::Client::startRecordingMode() |
| 474 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 475 | LOGD("startRecordingMode (pid %d)", getCallingPid()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | |
| 477 | status_t ret = UNKNOWN_ERROR; |
| 478 | |
| 479 | // if preview has not been started, start preview first |
| 480 | if (!mHardware->previewEnabled()) { |
| 481 | ret = startPreviewMode(); |
| 482 | if (ret != NO_ERROR) { |
| 483 | return ret; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | // if recording has been enabled, nothing needs to be done |
| 488 | if (mHardware->recordingEnabled()) { |
| 489 | return NO_ERROR; |
| 490 | } |
| 491 | |
| 492 | // start recording mode |
| 493 | ret = mHardware->startRecording(recordingCallback, |
| 494 | mCameraService.get()); |
| 495 | if (ret != NO_ERROR) { |
| 496 | LOGE("mHardware->startRecording() failed with status %d", ret); |
| 497 | } |
| 498 | return ret; |
| 499 | } |
| 500 | |
| 501 | status_t CameraService::Client::startPreviewMode() |
| 502 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 503 | LOGD("startPreviewMode (pid %d)", getCallingPid()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | |
| 505 | // if preview has been enabled, nothing needs to be done |
| 506 | if (mHardware->previewEnabled()) { |
| 507 | return NO_ERROR; |
| 508 | } |
| 509 | |
| 510 | // start preview mode |
| 511 | #if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE |
| 512 | debug_frame_cnt = 0; |
| 513 | #endif |
| 514 | status_t ret = UNKNOWN_ERROR; |
| 515 | int w, h; |
| 516 | CameraParameters params(mHardware->getParameters()); |
| 517 | params.getPreviewSize(&w, &h); |
| 518 | |
| 519 | if (mUseOverlay) { |
| 520 | const char *format = params.getPreviewFormat(); |
| 521 | int fmt; |
| 522 | LOGD("Use Overlays"); |
| 523 | if (!strcmp(format, "yuv422i")) |
| 524 | fmt = OVERLAY_FORMAT_YCbCr_422_I; |
| 525 | else if (!strcmp(format, "rgb565")) |
| 526 | fmt = OVERLAY_FORMAT_RGB_565; |
| 527 | else { |
| 528 | LOGE("Invalid preview format for overlays"); |
| 529 | return -EINVAL; |
| 530 | } |
| 531 | sp<OverlayRef> ref = mSurface->createOverlay(w, h, fmt); |
| 532 | ret = mHardware->setOverlay(new Overlay(ref)); |
| 533 | if (ret != NO_ERROR) { |
| 534 | LOGE("mHardware->setOverlay() failed with status %d\n", ret); |
| 535 | return ret; |
| 536 | } |
| 537 | ret = mHardware->startPreview(NULL, mCameraService.get()); |
| 538 | if (ret != NO_ERROR) |
| 539 | LOGE("mHardware->startPreview() failed with status %d\n", ret); |
| 540 | |
| 541 | } else { |
| 542 | ret = mHardware->startPreview(previewCallback, |
| 543 | mCameraService.get()); |
| 544 | if (ret == NO_ERROR) { |
| 545 | |
| 546 | mSurface->unregisterBuffers(); |
| 547 | |
| 548 | uint32_t transform = 0; |
| 549 | if (params.getOrientation() == |
| 550 | CameraParameters::CAMERA_ORIENTATION_PORTRAIT) { |
| 551 | LOGV("portrait mode"); |
| 552 | transform = ISurface::BufferHeap::ROT_90; |
| 553 | } |
| 554 | ISurface::BufferHeap buffers(w, h, w, h, |
| 555 | PIXEL_FORMAT_YCbCr_420_SP, |
| 556 | transform, |
| 557 | 0, |
| 558 | mHardware->getPreviewHeap()); |
| 559 | |
| 560 | mSurface->registerBuffers(buffers); |
| 561 | } else { |
| 562 | LOGE("mHardware->startPreview() failed with status %d", ret); |
| 563 | } |
| 564 | } |
| 565 | return ret; |
| 566 | } |
| 567 | |
| 568 | status_t CameraService::Client::startPreview() |
| 569 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 570 | LOGD("startPreview (pid %d)", getCallingPid()); |
| 571 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 572 | return startCameraMode(CAMERA_PREVIEW_MODE); |
| 573 | } |
| 574 | |
| 575 | status_t CameraService::Client::startRecording() |
| 576 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 577 | LOGD("startRecording (pid %d)", getCallingPid()); |
| 578 | |
Jason Sams | 78b877e | 2009-03-24 20:21:36 -0700 | [diff] [blame] | 579 | if (mMediaPlayerBeep.get() != NULL) { |
Andreas Huber | a26e606 | 2009-03-24 20:47:19 -0700 | [diff] [blame] | 580 | mMediaPlayerBeep->seekTo(0); |
Jason Sams | 78b877e | 2009-03-24 20:21:36 -0700 | [diff] [blame] | 581 | mMediaPlayerBeep->start(); |
| 582 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 583 | return startCameraMode(CAMERA_RECORDING_MODE); |
| 584 | } |
| 585 | |
| 586 | // stop preview mode |
| 587 | void CameraService::Client::stopPreview() |
| 588 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 589 | LOGD("stopPreview (pid %d)", getCallingPid()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | |
| 591 | Mutex::Autolock lock(mLock); |
| 592 | if (checkPid() != NO_ERROR) return; |
| 593 | |
| 594 | if (mHardware == 0) { |
| 595 | LOGE("mHardware is NULL, returning."); |
| 596 | return; |
| 597 | } |
| 598 | |
| 599 | mHardware->stopPreview(); |
| 600 | LOGD("stopPreview(), hardware stopped OK"); |
| 601 | |
| 602 | if (mSurface != 0 && !mUseOverlay) { |
| 603 | mSurface->unregisterBuffers(); |
| 604 | } |
| 605 | mPreviewBuffer.clear(); |
| 606 | } |
| 607 | |
| 608 | // stop recording mode |
| 609 | void CameraService::Client::stopRecording() |
| 610 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 611 | LOGD("stopRecording (pid %d)", getCallingPid()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 612 | |
| 613 | Mutex::Autolock lock(mLock); |
| 614 | if (checkPid() != NO_ERROR) return; |
| 615 | |
| 616 | if (mHardware == 0) { |
| 617 | LOGE("mHardware is NULL, returning."); |
| 618 | return; |
| 619 | } |
| 620 | |
Jason Sams | 78b877e | 2009-03-24 20:21:36 -0700 | [diff] [blame] | 621 | if (mMediaPlayerBeep.get() != NULL) { |
Andreas Huber | a26e606 | 2009-03-24 20:47:19 -0700 | [diff] [blame] | 622 | mMediaPlayerBeep->seekTo(0); |
Jason Sams | 78b877e | 2009-03-24 20:21:36 -0700 | [diff] [blame] | 623 | mMediaPlayerBeep->start(); |
| 624 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 625 | mHardware->stopRecording(); |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 626 | LOGD("stopRecording(), hardware stopped OK"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 627 | mPreviewBuffer.clear(); |
| 628 | } |
| 629 | |
| 630 | // release a recording frame |
| 631 | void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem) |
| 632 | { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | Mutex::Autolock lock(mLock); |
| 634 | if (checkPid() != NO_ERROR) return; |
| 635 | |
| 636 | if (mHardware == 0) { |
| 637 | LOGE("mHardware is NULL, returning."); |
| 638 | return; |
| 639 | } |
| 640 | |
| 641 | mHardware->releaseRecordingFrame(mem); |
| 642 | } |
| 643 | |
| 644 | bool CameraService::Client::previewEnabled() |
| 645 | { |
| 646 | Mutex::Autolock lock(mLock); |
| 647 | if (mHardware == 0) return false; |
| 648 | return mHardware->previewEnabled(); |
| 649 | } |
| 650 | |
| 651 | bool CameraService::Client::recordingEnabled() |
| 652 | { |
| 653 | Mutex::Autolock lock(mLock); |
| 654 | if (mHardware == 0) return false; |
| 655 | return mHardware->recordingEnabled(); |
| 656 | } |
| 657 | |
| 658 | // Safely retrieves a strong pointer to the client during a hardware callback. |
| 659 | sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user) |
| 660 | { |
| 661 | sp<Client> client = 0; |
| 662 | CameraService *service = static_cast<CameraService*>(user); |
| 663 | if (service != NULL) { |
| 664 | Mutex::Autolock ourLock(service->mLock); |
| 665 | if (service->mClient != 0) { |
| 666 | client = service->mClient.promote(); |
| 667 | if (client == 0) { |
| 668 | LOGE("getClientFromCookie: client appears to have died"); |
| 669 | service->mClient.clear(); |
| 670 | } |
| 671 | } else { |
| 672 | LOGE("getClientFromCookie: got callback but client was NULL"); |
| 673 | } |
| 674 | } |
| 675 | return client; |
| 676 | } |
| 677 | |
| 678 | |
| 679 | #if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE || \ |
| 680 | DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE || \ |
| 681 | DEBUG_DUMP_PREVIEW_FRAME_TO_FILE |
| 682 | static void dump_to_file(const char *fname, |
| 683 | uint8_t *buf, uint32_t size) |
| 684 | { |
| 685 | int nw, cnt = 0; |
| 686 | uint32_t written = 0; |
| 687 | |
| 688 | LOGD("opening file [%s]\n", fname); |
| 689 | int fd = open(fname, O_RDWR | O_CREAT); |
| 690 | if (fd < 0) { |
| 691 | LOGE("failed to create file [%s]: %s", fname, strerror(errno)); |
| 692 | return; |
| 693 | } |
| 694 | |
| 695 | LOGD("writing %d bytes to file [%s]\n", size, fname); |
| 696 | while (written < size) { |
| 697 | nw = ::write(fd, |
| 698 | buf + written, |
| 699 | size - written); |
| 700 | if (nw < 0) { |
| 701 | LOGE("failed to write to file [%s]: %s", |
| 702 | fname, strerror(errno)); |
| 703 | break; |
| 704 | } |
| 705 | written += nw; |
| 706 | cnt++; |
| 707 | } |
| 708 | LOGD("done writing %d bytes to file [%s] in %d passes\n", |
| 709 | size, fname, cnt); |
| 710 | ::close(fd); |
| 711 | } |
| 712 | #endif |
| 713 | |
| 714 | // preview callback - frame buffer update |
| 715 | void CameraService::Client::previewCallback(const sp<IMemory>& mem, void* user) |
| 716 | { |
| 717 | LOGV("previewCallback()"); |
| 718 | sp<Client> client = getClientFromCookie(user); |
| 719 | if (client == 0) { |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | #if DEBUG_HEAP_LEAKS && 0 // debugging |
| 724 | if (gWeakHeap == NULL) { |
| 725 | ssize_t offset; |
| 726 | size_t size; |
| 727 | sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); |
| 728 | if (gWeakHeap != heap) { |
| 729 | LOGD("SETTING PREVIEW HEAP"); |
| 730 | heap->trackMe(true, true); |
| 731 | gWeakHeap = heap; |
| 732 | } |
| 733 | } |
| 734 | #endif |
| 735 | |
| 736 | #if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE |
| 737 | { |
| 738 | if (debug_frame_cnt++ == DEBUG_DUMP_PREVIEW_FRAME_TO_FILE) { |
| 739 | ssize_t offset; |
| 740 | size_t size; |
| 741 | sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); |
| 742 | dump_to_file("/data/preview.yuv", |
| 743 | (uint8_t *)heap->base() + offset, size); |
| 744 | } |
| 745 | } |
| 746 | #endif |
| 747 | |
| 748 | // The strong pointer guarantees the client will exist, but no lock is held. |
| 749 | client->postPreviewFrame(mem); |
| 750 | |
| 751 | #if DEBUG_CLIENT_REFERENCES |
| 752 | //**** if the client's refcount is 1, then we are about to destroy it here, |
| 753 | // which is bad--print all refcounts. |
| 754 | if (client->getStrongCount() == 1) { |
| 755 | LOGE("++++++++++++++++ (PREVIEW) THIS WILL CAUSE A LOCKUP!"); |
| 756 | client->printRefs(); |
| 757 | } |
| 758 | #endif |
| 759 | } |
| 760 | |
| 761 | // recording callback |
| 762 | void CameraService::Client::recordingCallback(const sp<IMemory>& mem, void* user) |
| 763 | { |
| 764 | LOGV("recordingCallback"); |
| 765 | sp<Client> client = getClientFromCookie(user); |
| 766 | if (client == 0) { |
| 767 | return; |
| 768 | } |
| 769 | // The strong pointer guarantees the client will exist, but no lock is held. |
| 770 | client->postRecordingFrame(mem); |
| 771 | } |
| 772 | |
| 773 | // take a picture - image is returned in callback |
| 774 | status_t CameraService::Client::autoFocus() |
| 775 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 776 | LOGD("autoFocus (pid %d)", getCallingPid()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 777 | |
| 778 | Mutex::Autolock lock(mLock); |
| 779 | status_t result = checkPid(); |
| 780 | if (result != NO_ERROR) return result; |
| 781 | |
| 782 | if (mHardware == 0) { |
| 783 | LOGE("mHardware is NULL, returning."); |
| 784 | return INVALID_OPERATION; |
| 785 | } |
| 786 | |
| 787 | return mHardware->autoFocus(autoFocusCallback, |
| 788 | mCameraService.get()); |
| 789 | } |
| 790 | |
| 791 | // take a picture - image is returned in callback |
| 792 | status_t CameraService::Client::takePicture() |
| 793 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 794 | LOGD("takePicture (pid %d)", getCallingPid()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 795 | |
| 796 | Mutex::Autolock lock(mLock); |
| 797 | status_t result = checkPid(); |
| 798 | if (result != NO_ERROR) return result; |
| 799 | |
| 800 | if (mHardware == 0) { |
| 801 | LOGE("mHardware is NULL, returning."); |
| 802 | return INVALID_OPERATION; |
| 803 | } |
| 804 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 805 | return mHardware->takePicture(shutterCallback, |
| 806 | yuvPictureCallback, |
| 807 | jpegPictureCallback, |
| 808 | mCameraService.get()); |
| 809 | } |
| 810 | |
| 811 | // picture callback - snapshot taken |
| 812 | void CameraService::Client::shutterCallback(void *user) |
| 813 | { |
| 814 | sp<Client> client = getClientFromCookie(user); |
| 815 | if (client == 0) { |
| 816 | return; |
| 817 | } |
| 818 | |
Wu-cheng Li | aab44b8 | 2009-03-24 20:39:09 -0700 | [diff] [blame] | 819 | // Play shutter sound. |
| 820 | if (client->mMediaPlayerClick.get() != NULL) { |
Andreas Huber | a26e606 | 2009-03-24 20:47:19 -0700 | [diff] [blame] | 821 | client->mMediaPlayerClick->seekTo(0); |
Wu-cheng Li | aab44b8 | 2009-03-24 20:39:09 -0700 | [diff] [blame] | 822 | client->mMediaPlayerClick->start(); |
| 823 | } |
| 824 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 825 | // Screen goes black after the buffer is unregistered. |
| 826 | if (client->mSurface != 0 && !client->mUseOverlay) { |
| 827 | client->mSurface->unregisterBuffers(); |
| 828 | } |
| 829 | |
| 830 | client->postShutter(); |
| 831 | |
| 832 | // It takes some time before yuvPicture callback to be called. |
| 833 | // Register the buffer for raw image here to reduce latency. |
| 834 | if (client->mSurface != 0 && !client->mUseOverlay) { |
| 835 | int w, h; |
| 836 | CameraParameters params(client->mHardware->getParameters()); |
| 837 | params.getPictureSize(&w, &h); |
| 838 | uint32_t transform = 0; |
| 839 | if (params.getOrientation() == CameraParameters::CAMERA_ORIENTATION_PORTRAIT) { |
| 840 | LOGV("portrait mode"); |
| 841 | transform = ISurface::BufferHeap::ROT_90; |
| 842 | } |
| 843 | ISurface::BufferHeap buffers(w, h, w, h, |
| 844 | PIXEL_FORMAT_YCbCr_420_SP, transform, 0, client->mHardware->getRawHeap()); |
| 845 | |
| 846 | client->mSurface->registerBuffers(buffers); |
| 847 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | // picture callback - raw image ready |
| 851 | void CameraService::Client::yuvPictureCallback(const sp<IMemory>& mem, |
| 852 | void *user) |
| 853 | { |
| 854 | sp<Client> client = getClientFromCookie(user); |
| 855 | if (client == 0) { |
| 856 | return; |
| 857 | } |
| 858 | if (mem == NULL) { |
| 859 | client->postRaw(NULL); |
| 860 | client->postError(UNKNOWN_ERROR); |
| 861 | return; |
| 862 | } |
| 863 | |
| 864 | ssize_t offset; |
| 865 | size_t size; |
| 866 | sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); |
| 867 | #if DEBUG_HEAP_LEAKS && 0 // debugging |
| 868 | gWeakHeap = heap; // debugging |
| 869 | #endif |
| 870 | |
| 871 | //LOGV("yuvPictureCallback(%d, %d, %p)", offset, size, user); |
| 872 | #if DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE // for testing pursposes only |
| 873 | dump_to_file("/data/photo.yuv", |
| 874 | (uint8_t *)heap->base() + offset, size); |
| 875 | #endif |
| 876 | |
| 877 | // Put the YUV version of the snapshot in the preview display. |
| 878 | if (client->mSurface != 0 && !client->mUseOverlay) { |
| 879 | client->mSurface->postBuffer(offset); |
| 880 | } |
| 881 | |
| 882 | client->postRaw(mem); |
| 883 | |
| 884 | #if DEBUG_CLIENT_REFERENCES |
| 885 | //**** if the client's refcount is 1, then we are about to destroy it here, |
| 886 | // which is bad--print all refcounts. |
| 887 | if (client->getStrongCount() == 1) { |
| 888 | LOGE("++++++++++++++++ (RAW) THIS WILL CAUSE A LOCKUP!"); |
| 889 | client->printRefs(); |
| 890 | } |
| 891 | #endif |
| 892 | } |
| 893 | |
| 894 | // picture callback - jpeg ready |
| 895 | void CameraService::Client::jpegPictureCallback(const sp<IMemory>& mem, void *user) |
| 896 | { |
| 897 | sp<Client> client = getClientFromCookie(user); |
| 898 | if (client == 0) { |
| 899 | return; |
| 900 | } |
| 901 | if (mem == NULL) { |
| 902 | client->postJpeg(NULL); |
| 903 | client->postError(UNKNOWN_ERROR); |
| 904 | return; |
| 905 | } |
| 906 | |
| 907 | /** We absolutely CANNOT call into user code with a lock held **/ |
| 908 | |
| 909 | #if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE // for testing pursposes only |
| 910 | { |
| 911 | ssize_t offset; |
| 912 | size_t size; |
| 913 | sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); |
| 914 | dump_to_file("/data/photo.jpg", |
| 915 | (uint8_t *)heap->base() + offset, size); |
| 916 | } |
| 917 | #endif |
| 918 | |
| 919 | client->postJpeg(mem); |
| 920 | |
| 921 | #if DEBUG_CLIENT_REFERENCES |
| 922 | //**** if the client's refcount is 1, then we are about to destroy it here, |
| 923 | // which is bad--print all refcounts. |
| 924 | if (client->getStrongCount() == 1) { |
| 925 | LOGE("++++++++++++++++ (JPEG) THIS WILL CAUSE A LOCKUP!"); |
| 926 | client->printRefs(); |
| 927 | } |
| 928 | #endif |
| 929 | } |
| 930 | |
| 931 | void CameraService::Client::autoFocusCallback(bool focused, void *user) |
| 932 | { |
| 933 | LOGV("autoFocusCallback"); |
| 934 | |
| 935 | sp<Client> client = getClientFromCookie(user); |
| 936 | if (client == 0) { |
| 937 | return; |
| 938 | } |
| 939 | |
| 940 | client->postAutoFocus(focused); |
| 941 | |
| 942 | #if DEBUG_CLIENT_REFERENCES |
| 943 | if (client->getStrongCount() == 1) { |
| 944 | LOGE("++++++++++++++++ (AUTOFOCUS) THIS WILL CAUSE A LOCKUP!"); |
| 945 | client->printRefs(); |
| 946 | } |
| 947 | #endif |
| 948 | } |
| 949 | |
| 950 | // set preview/capture parameters - key/value pairs |
| 951 | status_t CameraService::Client::setParameters(const String8& params) |
| 952 | { |
| 953 | LOGD("setParameters(%s)", params.string()); |
| 954 | |
| 955 | Mutex::Autolock lock(mLock); |
| 956 | status_t result = checkPid(); |
| 957 | if (result != NO_ERROR) return result; |
| 958 | |
| 959 | if (mHardware == 0) { |
| 960 | LOGE("mHardware is NULL, returning."); |
| 961 | return INVALID_OPERATION; |
| 962 | } |
| 963 | |
| 964 | CameraParameters p(params); |
| 965 | mHardware->setParameters(p); |
| 966 | return NO_ERROR; |
| 967 | } |
| 968 | |
| 969 | // get preview/capture parameters - key/value pairs |
| 970 | String8 CameraService::Client::getParameters() const |
| 971 | { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 972 | Mutex::Autolock lock(mLock); |
| 973 | |
| 974 | if (mHardware == 0) { |
| 975 | LOGE("mHardware is NULL, returning."); |
| 976 | return String8(); |
| 977 | } |
| 978 | |
Wu-cheng Li | 81d763f | 2009-04-22 16:21:26 +0800 | [diff] [blame] | 979 | String8 params(mHardware->getParameters().flatten()); |
| 980 | LOGD("getParameters(%s)", params.string()); |
| 981 | return params; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | void CameraService::Client::postAutoFocus(bool focused) |
| 985 | { |
| 986 | LOGV("postAutoFocus"); |
Dave Sparks | 93b9458 | 2009-05-07 19:27:32 -0700 | [diff] [blame] | 987 | mCameraClient->notifyCallback(CAMERA_MSG_FOCUS, (int32_t)focused, 0); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | void CameraService::Client::postShutter() |
| 991 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 992 | LOGD("postShutter"); |
Dave Sparks | 93b9458 | 2009-05-07 19:27:32 -0700 | [diff] [blame] | 993 | mCameraClient->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | void CameraService::Client::postRaw(const sp<IMemory>& mem) |
| 997 | { |
| 998 | LOGD("postRaw"); |
Dave Sparks | 93b9458 | 2009-05-07 19:27:32 -0700 | [diff] [blame] | 999 | mCameraClient->dataCallback(CAMERA_MSG_RAW_IMAGE, mem); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | void CameraService::Client::postJpeg(const sp<IMemory>& mem) |
| 1003 | { |
| 1004 | LOGD("postJpeg"); |
Dave Sparks | 93b9458 | 2009-05-07 19:27:32 -0700 | [diff] [blame] | 1005 | mCameraClient->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | void CameraService::Client::copyFrameAndPostCopiedFrame(sp<IMemoryHeap> heap, size_t offset, size_t size) |
| 1009 | { |
| 1010 | LOGV("copyFrameAndPostCopiedFrame"); |
| 1011 | // It is necessary to copy out of pmem before sending this to |
| 1012 | // the callback. For efficiency, reuse the same MemoryHeapBase |
| 1013 | // provided it's big enough. Don't allocate the memory or |
| 1014 | // perform the copy if there's no callback. |
| 1015 | if (mPreviewBuffer == 0) { |
| 1016 | mPreviewBuffer = new MemoryHeapBase(size, 0, NULL); |
| 1017 | } else if (size > mPreviewBuffer->virtualSize()) { |
| 1018 | mPreviewBuffer.clear(); |
| 1019 | mPreviewBuffer = new MemoryHeapBase(size, 0, NULL); |
| 1020 | if (mPreviewBuffer == 0) { |
| 1021 | LOGE("failed to allocate space for preview buffer"); |
| 1022 | return; |
| 1023 | } |
| 1024 | } |
| 1025 | memcpy(mPreviewBuffer->base(), |
| 1026 | (uint8_t *)heap->base() + offset, size); |
| 1027 | |
| 1028 | sp<MemoryBase> frame = new MemoryBase(mPreviewBuffer, 0, size); |
| 1029 | if (frame == 0) { |
| 1030 | LOGE("failed to allocate space for frame callback"); |
| 1031 | return; |
| 1032 | } |
Dave Sparks | 93b9458 | 2009-05-07 19:27:32 -0700 | [diff] [blame] | 1033 | mCameraClient->dataCallback(CAMERA_MSG_PREVIEW_FRAME, frame); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | void CameraService::Client::postRecordingFrame(const sp<IMemory>& frame) |
| 1037 | { |
| 1038 | LOGV("postRecordingFrame"); |
| 1039 | if (frame == 0) { |
| 1040 | LOGW("frame is a null pointer"); |
| 1041 | return; |
| 1042 | } |
Dave Sparks | 93b9458 | 2009-05-07 19:27:32 -0700 | [diff] [blame] | 1043 | mCameraClient->dataCallback(CAMERA_MSG_VIDEO_FRAME, frame); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | void CameraService::Client::postPreviewFrame(const sp<IMemory>& mem) |
| 1047 | { |
| 1048 | LOGV("postPreviewFrame"); |
| 1049 | if (mem == 0) { |
| 1050 | LOGW("mem is a null pointer"); |
| 1051 | return; |
| 1052 | } |
| 1053 | |
| 1054 | ssize_t offset; |
| 1055 | size_t size; |
| 1056 | sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); |
| 1057 | { |
| 1058 | Mutex::Autolock surfaceLock(mSurfaceLock); |
| 1059 | if (mSurface != NULL) { |
| 1060 | mSurface->postBuffer(offset); |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | // Is the callback enabled or not? |
| 1065 | if (!(mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ENABLE_MASK)) { |
| 1066 | // If the enable bit is off, the copy-out and one-shot bits are ignored |
| 1067 | LOGV("frame callback is diabled"); |
| 1068 | return; |
| 1069 | } |
| 1070 | |
| 1071 | // Is the received frame copied out or not? |
| 1072 | if (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_COPY_OUT_MASK) { |
| 1073 | LOGV("frame is copied out"); |
| 1074 | copyFrameAndPostCopiedFrame(heap, offset, size); |
| 1075 | } else { |
| 1076 | LOGV("frame is directly sent out without copying"); |
Dave Sparks | 93b9458 | 2009-05-07 19:27:32 -0700 | [diff] [blame] | 1077 | mCameraClient->dataCallback(CAMERA_MSG_PREVIEW_FRAME, mem); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | // Is this is one-shot only? |
| 1081 | if (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ONE_SHOT_MASK) { |
| 1082 | LOGV("One-shot only, thus clear the bits and disable frame callback"); |
| 1083 | mPreviewCallbackFlag &= ~(FRAME_CALLBACK_FLAG_ONE_SHOT_MASK | |
| 1084 | FRAME_CALLBACK_FLAG_COPY_OUT_MASK | |
| 1085 | FRAME_CALLBACK_FLAG_ENABLE_MASK); |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | void CameraService::Client::postError(status_t error) |
| 1090 | { |
Dave Sparks | 93b9458 | 2009-05-07 19:27:32 -0700 | [diff] [blame] | 1091 | mCameraClient->notifyCallback(CAMERA_MSG_ERROR, error, 0); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | status_t CameraService::dump(int fd, const Vector<String16>& args) |
| 1095 | { |
| 1096 | const size_t SIZE = 256; |
| 1097 | char buffer[SIZE]; |
| 1098 | String8 result; |
| 1099 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 1100 | snprintf(buffer, SIZE, "Permission Denial: " |
| 1101 | "can't dump CameraService from pid=%d, uid=%d\n", |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 1102 | getCallingPid(), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1103 | IPCThreadState::self()->getCallingUid()); |
| 1104 | result.append(buffer); |
| 1105 | write(fd, result.string(), result.size()); |
| 1106 | } else { |
| 1107 | AutoMutex lock(&mLock); |
| 1108 | if (mClient != 0) { |
| 1109 | sp<Client> currentClient = mClient.promote(); |
| 1110 | sprintf(buffer, "Client (%p) PID: %d\n", |
| 1111 | currentClient->getCameraClient()->asBinder().get(), |
| 1112 | currentClient->mClientPid); |
| 1113 | result.append(buffer); |
| 1114 | write(fd, result.string(), result.size()); |
| 1115 | currentClient->mHardware->dump(fd, args); |
| 1116 | } else { |
| 1117 | result.append("No camera client yet.\n"); |
| 1118 | write(fd, result.string(), result.size()); |
| 1119 | } |
| 1120 | } |
| 1121 | return NO_ERROR; |
| 1122 | } |
| 1123 | |
| 1124 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1125 | #define CHECK_INTERFACE(interface, data, reply) \ |
| 1126 | do { if (!data.enforceInterface(interface::getInterfaceDescriptor())) { \ |
| 1127 | LOGW("Call incorrectly routed to " #interface); \ |
| 1128 | return PERMISSION_DENIED; \ |
| 1129 | } } while (0) |
| 1130 | |
| 1131 | status_t CameraService::onTransact( |
| 1132 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 1133 | { |
| 1134 | // permission checks... |
| 1135 | switch (code) { |
| 1136 | case BnCameraService::CONNECT: |
| 1137 | IPCThreadState* ipc = IPCThreadState::self(); |
| 1138 | const int pid = ipc->getCallingPid(); |
| 1139 | const int self_pid = getpid(); |
| 1140 | if (pid != self_pid) { |
| 1141 | // we're called from a different process, do the real check |
| 1142 | if (!checkCallingPermission( |
| 1143 | String16("android.permission.CAMERA"))) |
| 1144 | { |
| 1145 | const int uid = ipc->getCallingUid(); |
| 1146 | LOGE("Permission Denial: " |
| 1147 | "can't use the camera pid=%d, uid=%d", pid, uid); |
| 1148 | return PERMISSION_DENIED; |
| 1149 | } |
| 1150 | } |
| 1151 | break; |
| 1152 | } |
| 1153 | |
| 1154 | status_t err = BnCameraService::onTransact(code, data, reply, flags); |
| 1155 | |
Dave Sparks | 998b329 | 2009-05-20 20:02:59 -0700 | [diff] [blame] | 1156 | #if DEBUG_HEAP_LEAKS |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1157 | LOGD("+++ onTransact err %d code %d", err, code); |
| 1158 | |
| 1159 | if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) { |
| 1160 | // the 'service' command interrogates this binder for its name, and then supplies it |
| 1161 | // even for the debugging commands. that means we need to check for it here, using |
| 1162 | // ISurfaceComposer (since we delegated the INTERFACE_TRANSACTION handling to |
| 1163 | // BnSurfaceComposer before falling through to this code). |
| 1164 | |
| 1165 | LOGD("+++ onTransact code %d", code); |
| 1166 | |
| 1167 | CHECK_INTERFACE(ICameraService, data, reply); |
| 1168 | |
| 1169 | switch(code) { |
| 1170 | case 1000: |
| 1171 | { |
| 1172 | if (gWeakHeap != 0) { |
| 1173 | sp<IMemoryHeap> h = gWeakHeap.promote(); |
| 1174 | IMemoryHeap *p = gWeakHeap.unsafe_get(); |
| 1175 | LOGD("CHECKING WEAK REFERENCE %p (%p)", h.get(), p); |
| 1176 | if (h != 0) |
| 1177 | h->printRefs(); |
| 1178 | bool attempt_to_delete = data.readInt32() == 1; |
| 1179 | if (attempt_to_delete) { |
| 1180 | // NOT SAFE! |
| 1181 | LOGD("DELETING WEAK REFERENCE %p (%p)", h.get(), p); |
| 1182 | if (p) delete p; |
| 1183 | } |
| 1184 | return NO_ERROR; |
| 1185 | } |
| 1186 | } |
| 1187 | break; |
| 1188 | default: |
| 1189 | break; |
| 1190 | } |
| 1191 | } |
Dave Sparks | 998b329 | 2009-05-20 20:02:59 -0700 | [diff] [blame] | 1192 | #endif // DEBUG_HEAP_LEAKS |
Dave Sparks | fec880d | 2009-05-21 09:18:18 -0700 | [diff] [blame] | 1193 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1194 | return err; |
| 1195 | } |
| 1196 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1197 | }; // namespace android |