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