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 | |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 23 | #include <binder/IServiceManager.h> |
| 24 | #include <binder/IPCThreadState.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | #include <utils/String16.h> |
| 26 | #include <utils/Errors.h> |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 27 | #include <binder/MemoryBase.h> |
| 28 | #include <binder/MemoryHeapBase.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 |
Dave Sparks | f72d640 | 2009-07-08 15:56:53 -0700 | [diff] [blame] | 507 | ret = mHardware->startRecording(recordingCallback, mCameraService.get()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 | if (ret != NO_ERROR) { |
| 509 | LOGE("mHardware->startRecording() failed with status %d", ret); |
| 510 | } |
| 511 | return ret; |
| 512 | } |
| 513 | |
Wu-cheng Li | 988fb62 | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 514 | status_t CameraService::Client::setOverlay() |
| 515 | { |
| 516 | LOGD("setOverlay"); |
| 517 | int w, h; |
| 518 | CameraParameters params(mHardware->getParameters()); |
| 519 | params.getPreviewSize(&w, &h); |
| 520 | |
| 521 | const char *format = params.getPreviewFormat(); |
| 522 | int fmt; |
| 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 | |
| 532 | status_t ret = NO_ERROR; |
| 533 | if (mSurface != 0) { |
| 534 | sp<OverlayRef> ref = mSurface->createOverlay(w, h, fmt); |
| 535 | ret = mHardware->setOverlay(new Overlay(ref)); |
| 536 | } else { |
| 537 | ret = mHardware->setOverlay(NULL); |
| 538 | } |
| 539 | if (ret != NO_ERROR) { |
| 540 | LOGE("mHardware->setOverlay() failed with status %d\n", ret); |
| 541 | } |
| 542 | return ret; |
| 543 | } |
| 544 | |
| 545 | status_t CameraService::Client::registerPreviewBuffers() |
| 546 | { |
| 547 | int w, h; |
| 548 | CameraParameters params(mHardware->getParameters()); |
| 549 | params.getPreviewSize(&w, &h); |
| 550 | |
| 551 | uint32_t transform = 0; |
| 552 | if (params.getOrientation() == |
| 553 | CameraParameters::CAMERA_ORIENTATION_PORTRAIT) { |
| 554 | LOGV("portrait mode"); |
| 555 | transform = ISurface::BufferHeap::ROT_90; |
| 556 | } |
| 557 | ISurface::BufferHeap buffers(w, h, w, h, |
| 558 | PIXEL_FORMAT_YCbCr_420_SP, |
| 559 | transform, |
| 560 | 0, |
| 561 | mHardware->getPreviewHeap()); |
| 562 | |
| 563 | status_t ret = mSurface->registerBuffers(buffers); |
| 564 | if (ret != NO_ERROR) { |
| 565 | LOGE("registerBuffers failed with status %d", ret); |
| 566 | } |
| 567 | return ret; |
| 568 | } |
| 569 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 570 | status_t CameraService::Client::startPreviewMode() |
| 571 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 572 | LOGD("startPreviewMode (pid %d)", getCallingPid()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 573 | |
| 574 | // if preview has been enabled, nothing needs to be done |
| 575 | if (mHardware->previewEnabled()) { |
| 576 | return NO_ERROR; |
| 577 | } |
| 578 | |
| 579 | // start preview mode |
| 580 | #if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE |
| 581 | debug_frame_cnt = 0; |
| 582 | #endif |
Wu-cheng Li | 988fb62 | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 583 | status_t ret = NO_ERROR; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 584 | |
| 585 | if (mUseOverlay) { |
Wu-cheng Li | 988fb62 | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 586 | // If preview display has been set, set overlay now. |
| 587 | if (mSurface != 0) { |
| 588 | ret = setOverlay(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 589 | } |
Wu-cheng Li | 988fb62 | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 590 | if (ret != NO_ERROR) return ret; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 591 | ret = mHardware->startPreview(NULL, mCameraService.get()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 592 | } else { |
| 593 | ret = mHardware->startPreview(previewCallback, |
| 594 | mCameraService.get()); |
Wu-cheng Li | 988fb62 | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 595 | if (ret != NO_ERROR) return ret; |
| 596 | // If preview display has been set, register preview buffers now. |
| 597 | if (mSurface != 0) { |
| 598 | // Unregister here because the surface registered with raw heap. |
| 599 | mSurface->unregisterBuffers(); |
| 600 | ret = registerPreviewBuffers(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | return ret; |
| 604 | } |
| 605 | |
| 606 | status_t CameraService::Client::startPreview() |
| 607 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 608 | LOGD("startPreview (pid %d)", getCallingPid()); |
| 609 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 610 | return startCameraMode(CAMERA_PREVIEW_MODE); |
| 611 | } |
| 612 | |
| 613 | status_t CameraService::Client::startRecording() |
| 614 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 615 | LOGD("startRecording (pid %d)", getCallingPid()); |
| 616 | |
Jason Sams | 78b877e | 2009-03-24 20:21:36 -0700 | [diff] [blame] | 617 | if (mMediaPlayerBeep.get() != NULL) { |
Andreas Huber | a26e606 | 2009-03-24 20:47:19 -0700 | [diff] [blame] | 618 | mMediaPlayerBeep->seekTo(0); |
Jason Sams | 78b877e | 2009-03-24 20:21:36 -0700 | [diff] [blame] | 619 | mMediaPlayerBeep->start(); |
| 620 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 621 | return startCameraMode(CAMERA_RECORDING_MODE); |
| 622 | } |
| 623 | |
| 624 | // stop preview mode |
| 625 | void CameraService::Client::stopPreview() |
| 626 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 627 | LOGD("stopPreview (pid %d)", getCallingPid()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 628 | |
| 629 | Mutex::Autolock lock(mLock); |
| 630 | if (checkPid() != NO_ERROR) return; |
| 631 | |
| 632 | if (mHardware == 0) { |
| 633 | LOGE("mHardware is NULL, returning."); |
| 634 | return; |
| 635 | } |
| 636 | |
| 637 | mHardware->stopPreview(); |
| 638 | LOGD("stopPreview(), hardware stopped OK"); |
| 639 | |
| 640 | if (mSurface != 0 && !mUseOverlay) { |
| 641 | mSurface->unregisterBuffers(); |
| 642 | } |
| 643 | mPreviewBuffer.clear(); |
| 644 | } |
| 645 | |
| 646 | // stop recording mode |
| 647 | void CameraService::Client::stopRecording() |
| 648 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 649 | LOGD("stopRecording (pid %d)", getCallingPid()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 650 | |
| 651 | Mutex::Autolock lock(mLock); |
| 652 | if (checkPid() != NO_ERROR) return; |
| 653 | |
| 654 | if (mHardware == 0) { |
| 655 | LOGE("mHardware is NULL, returning."); |
| 656 | return; |
| 657 | } |
| 658 | |
Jason Sams | 78b877e | 2009-03-24 20:21:36 -0700 | [diff] [blame] | 659 | if (mMediaPlayerBeep.get() != NULL) { |
Andreas Huber | a26e606 | 2009-03-24 20:47:19 -0700 | [diff] [blame] | 660 | mMediaPlayerBeep->seekTo(0); |
Jason Sams | 78b877e | 2009-03-24 20:21:36 -0700 | [diff] [blame] | 661 | mMediaPlayerBeep->start(); |
| 662 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 663 | mHardware->stopRecording(); |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 664 | LOGD("stopRecording(), hardware stopped OK"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 665 | mPreviewBuffer.clear(); |
| 666 | } |
| 667 | |
| 668 | // release a recording frame |
| 669 | void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem) |
| 670 | { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 671 | Mutex::Autolock lock(mLock); |
| 672 | if (checkPid() != NO_ERROR) return; |
| 673 | |
| 674 | if (mHardware == 0) { |
| 675 | LOGE("mHardware is NULL, returning."); |
| 676 | return; |
| 677 | } |
| 678 | |
| 679 | mHardware->releaseRecordingFrame(mem); |
| 680 | } |
| 681 | |
| 682 | bool CameraService::Client::previewEnabled() |
| 683 | { |
| 684 | Mutex::Autolock lock(mLock); |
| 685 | if (mHardware == 0) return false; |
| 686 | return mHardware->previewEnabled(); |
| 687 | } |
| 688 | |
| 689 | bool CameraService::Client::recordingEnabled() |
| 690 | { |
| 691 | Mutex::Autolock lock(mLock); |
| 692 | if (mHardware == 0) return false; |
| 693 | return mHardware->recordingEnabled(); |
| 694 | } |
| 695 | |
| 696 | // Safely retrieves a strong pointer to the client during a hardware callback. |
| 697 | sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user) |
| 698 | { |
| 699 | sp<Client> client = 0; |
| 700 | CameraService *service = static_cast<CameraService*>(user); |
| 701 | if (service != NULL) { |
Chih-Chung Chang | d2d6bc7 | 2009-06-24 19:59:31 +0800 | [diff] [blame] | 702 | Mutex::Autolock ourLock(service->mServiceLock); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 703 | if (service->mClient != 0) { |
| 704 | client = service->mClient.promote(); |
| 705 | if (client == 0) { |
| 706 | LOGE("getClientFromCookie: client appears to have died"); |
| 707 | service->mClient.clear(); |
| 708 | } |
| 709 | } else { |
| 710 | LOGE("getClientFromCookie: got callback but client was NULL"); |
| 711 | } |
| 712 | } |
| 713 | return client; |
| 714 | } |
| 715 | |
| 716 | |
| 717 | #if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE || \ |
| 718 | DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE || \ |
| 719 | DEBUG_DUMP_PREVIEW_FRAME_TO_FILE |
| 720 | static void dump_to_file(const char *fname, |
| 721 | uint8_t *buf, uint32_t size) |
| 722 | { |
| 723 | int nw, cnt = 0; |
| 724 | uint32_t written = 0; |
| 725 | |
| 726 | LOGD("opening file [%s]\n", fname); |
| 727 | int fd = open(fname, O_RDWR | O_CREAT); |
| 728 | if (fd < 0) { |
| 729 | LOGE("failed to create file [%s]: %s", fname, strerror(errno)); |
| 730 | return; |
| 731 | } |
| 732 | |
| 733 | LOGD("writing %d bytes to file [%s]\n", size, fname); |
| 734 | while (written < size) { |
| 735 | nw = ::write(fd, |
| 736 | buf + written, |
| 737 | size - written); |
| 738 | if (nw < 0) { |
| 739 | LOGE("failed to write to file [%s]: %s", |
| 740 | fname, strerror(errno)); |
| 741 | break; |
| 742 | } |
| 743 | written += nw; |
| 744 | cnt++; |
| 745 | } |
| 746 | LOGD("done writing %d bytes to file [%s] in %d passes\n", |
| 747 | size, fname, cnt); |
| 748 | ::close(fd); |
| 749 | } |
| 750 | #endif |
| 751 | |
| 752 | // preview callback - frame buffer update |
| 753 | void CameraService::Client::previewCallback(const sp<IMemory>& mem, void* user) |
| 754 | { |
| 755 | LOGV("previewCallback()"); |
| 756 | sp<Client> client = getClientFromCookie(user); |
| 757 | if (client == 0) { |
| 758 | return; |
| 759 | } |
| 760 | |
| 761 | #if DEBUG_HEAP_LEAKS && 0 // debugging |
| 762 | if (gWeakHeap == NULL) { |
| 763 | ssize_t offset; |
| 764 | size_t size; |
| 765 | sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); |
| 766 | if (gWeakHeap != heap) { |
| 767 | LOGD("SETTING PREVIEW HEAP"); |
| 768 | heap->trackMe(true, true); |
| 769 | gWeakHeap = heap; |
| 770 | } |
| 771 | } |
| 772 | #endif |
| 773 | |
| 774 | #if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE |
| 775 | { |
| 776 | if (debug_frame_cnt++ == DEBUG_DUMP_PREVIEW_FRAME_TO_FILE) { |
| 777 | ssize_t offset; |
| 778 | size_t size; |
| 779 | sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); |
| 780 | dump_to_file("/data/preview.yuv", |
| 781 | (uint8_t *)heap->base() + offset, size); |
| 782 | } |
| 783 | } |
| 784 | #endif |
| 785 | |
| 786 | // The strong pointer guarantees the client will exist, but no lock is held. |
| 787 | client->postPreviewFrame(mem); |
| 788 | |
| 789 | #if DEBUG_CLIENT_REFERENCES |
| 790 | //**** if the client's refcount is 1, then we are about to destroy it here, |
| 791 | // which is bad--print all refcounts. |
| 792 | if (client->getStrongCount() == 1) { |
| 793 | LOGE("++++++++++++++++ (PREVIEW) THIS WILL CAUSE A LOCKUP!"); |
| 794 | client->printRefs(); |
| 795 | } |
| 796 | #endif |
| 797 | } |
| 798 | |
| 799 | // recording callback |
Dave Sparks | f72d640 | 2009-07-08 15:56:53 -0700 | [diff] [blame] | 800 | void CameraService::Client::recordingCallback(nsecs_t timestamp, const sp<IMemory>& mem, void* user) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 801 | { |
| 802 | LOGV("recordingCallback"); |
| 803 | sp<Client> client = getClientFromCookie(user); |
| 804 | if (client == 0) { |
| 805 | return; |
| 806 | } |
| 807 | // The strong pointer guarantees the client will exist, but no lock is held. |
Dave Sparks | f72d640 | 2009-07-08 15:56:53 -0700 | [diff] [blame] | 808 | client->postRecordingFrame(timestamp, mem); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | // take a picture - image is returned in callback |
| 812 | status_t CameraService::Client::autoFocus() |
| 813 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 814 | LOGD("autoFocus (pid %d)", getCallingPid()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 815 | |
| 816 | Mutex::Autolock lock(mLock); |
| 817 | status_t result = checkPid(); |
| 818 | if (result != NO_ERROR) return result; |
| 819 | |
| 820 | if (mHardware == 0) { |
| 821 | LOGE("mHardware is NULL, returning."); |
| 822 | return INVALID_OPERATION; |
| 823 | } |
| 824 | |
| 825 | return mHardware->autoFocus(autoFocusCallback, |
| 826 | mCameraService.get()); |
| 827 | } |
| 828 | |
| 829 | // take a picture - image is returned in callback |
| 830 | status_t CameraService::Client::takePicture() |
| 831 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 832 | LOGD("takePicture (pid %d)", getCallingPid()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 833 | |
| 834 | Mutex::Autolock lock(mLock); |
| 835 | status_t result = checkPid(); |
| 836 | if (result != NO_ERROR) return result; |
| 837 | |
| 838 | if (mHardware == 0) { |
| 839 | LOGE("mHardware is NULL, returning."); |
| 840 | return INVALID_OPERATION; |
| 841 | } |
| 842 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 843 | return mHardware->takePicture(shutterCallback, |
| 844 | yuvPictureCallback, |
| 845 | jpegPictureCallback, |
| 846 | mCameraService.get()); |
| 847 | } |
| 848 | |
| 849 | // picture callback - snapshot taken |
| 850 | void CameraService::Client::shutterCallback(void *user) |
| 851 | { |
| 852 | sp<Client> client = getClientFromCookie(user); |
| 853 | if (client == 0) { |
| 854 | return; |
| 855 | } |
| 856 | |
Wu-cheng Li | aab44b8 | 2009-03-24 20:39:09 -0700 | [diff] [blame] | 857 | // Play shutter sound. |
| 858 | if (client->mMediaPlayerClick.get() != NULL) { |
Andreas Huber | a26e606 | 2009-03-24 20:47:19 -0700 | [diff] [blame] | 859 | client->mMediaPlayerClick->seekTo(0); |
Wu-cheng Li | aab44b8 | 2009-03-24 20:39:09 -0700 | [diff] [blame] | 860 | client->mMediaPlayerClick->start(); |
| 861 | } |
| 862 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 863 | // Screen goes black after the buffer is unregistered. |
| 864 | if (client->mSurface != 0 && !client->mUseOverlay) { |
| 865 | client->mSurface->unregisterBuffers(); |
| 866 | } |
| 867 | |
| 868 | client->postShutter(); |
| 869 | |
| 870 | // It takes some time before yuvPicture callback to be called. |
| 871 | // Register the buffer for raw image here to reduce latency. |
| 872 | if (client->mSurface != 0 && !client->mUseOverlay) { |
| 873 | int w, h; |
| 874 | CameraParameters params(client->mHardware->getParameters()); |
| 875 | params.getPictureSize(&w, &h); |
| 876 | uint32_t transform = 0; |
| 877 | if (params.getOrientation() == CameraParameters::CAMERA_ORIENTATION_PORTRAIT) { |
| 878 | LOGV("portrait mode"); |
| 879 | transform = ISurface::BufferHeap::ROT_90; |
| 880 | } |
| 881 | ISurface::BufferHeap buffers(w, h, w, h, |
| 882 | PIXEL_FORMAT_YCbCr_420_SP, transform, 0, client->mHardware->getRawHeap()); |
| 883 | |
| 884 | client->mSurface->registerBuffers(buffers); |
| 885 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | // picture callback - raw image ready |
| 889 | void CameraService::Client::yuvPictureCallback(const sp<IMemory>& mem, |
| 890 | void *user) |
| 891 | { |
| 892 | sp<Client> client = getClientFromCookie(user); |
| 893 | if (client == 0) { |
| 894 | return; |
| 895 | } |
| 896 | if (mem == NULL) { |
| 897 | client->postRaw(NULL); |
| 898 | client->postError(UNKNOWN_ERROR); |
| 899 | return; |
| 900 | } |
| 901 | |
| 902 | ssize_t offset; |
| 903 | size_t size; |
| 904 | sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); |
| 905 | #if DEBUG_HEAP_LEAKS && 0 // debugging |
| 906 | gWeakHeap = heap; // debugging |
| 907 | #endif |
| 908 | |
| 909 | //LOGV("yuvPictureCallback(%d, %d, %p)", offset, size, user); |
| 910 | #if DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE // for testing pursposes only |
| 911 | dump_to_file("/data/photo.yuv", |
| 912 | (uint8_t *)heap->base() + offset, size); |
| 913 | #endif |
| 914 | |
| 915 | // Put the YUV version of the snapshot in the preview display. |
| 916 | if (client->mSurface != 0 && !client->mUseOverlay) { |
| 917 | client->mSurface->postBuffer(offset); |
| 918 | } |
| 919 | |
| 920 | client->postRaw(mem); |
| 921 | |
| 922 | #if DEBUG_CLIENT_REFERENCES |
| 923 | //**** if the client's refcount is 1, then we are about to destroy it here, |
| 924 | // which is bad--print all refcounts. |
| 925 | if (client->getStrongCount() == 1) { |
| 926 | LOGE("++++++++++++++++ (RAW) THIS WILL CAUSE A LOCKUP!"); |
| 927 | client->printRefs(); |
| 928 | } |
| 929 | #endif |
| 930 | } |
| 931 | |
| 932 | // picture callback - jpeg ready |
| 933 | void CameraService::Client::jpegPictureCallback(const sp<IMemory>& mem, void *user) |
| 934 | { |
| 935 | sp<Client> client = getClientFromCookie(user); |
| 936 | if (client == 0) { |
| 937 | return; |
| 938 | } |
| 939 | if (mem == NULL) { |
| 940 | client->postJpeg(NULL); |
| 941 | client->postError(UNKNOWN_ERROR); |
| 942 | return; |
| 943 | } |
| 944 | |
| 945 | /** We absolutely CANNOT call into user code with a lock held **/ |
| 946 | |
| 947 | #if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE // for testing pursposes only |
| 948 | { |
| 949 | ssize_t offset; |
| 950 | size_t size; |
| 951 | sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); |
| 952 | dump_to_file("/data/photo.jpg", |
| 953 | (uint8_t *)heap->base() + offset, size); |
| 954 | } |
| 955 | #endif |
| 956 | |
| 957 | client->postJpeg(mem); |
| 958 | |
| 959 | #if DEBUG_CLIENT_REFERENCES |
| 960 | //**** if the client's refcount is 1, then we are about to destroy it here, |
| 961 | // which is bad--print all refcounts. |
| 962 | if (client->getStrongCount() == 1) { |
| 963 | LOGE("++++++++++++++++ (JPEG) THIS WILL CAUSE A LOCKUP!"); |
| 964 | client->printRefs(); |
| 965 | } |
| 966 | #endif |
| 967 | } |
| 968 | |
| 969 | void CameraService::Client::autoFocusCallback(bool focused, void *user) |
| 970 | { |
| 971 | LOGV("autoFocusCallback"); |
| 972 | |
| 973 | sp<Client> client = getClientFromCookie(user); |
| 974 | if (client == 0) { |
| 975 | return; |
| 976 | } |
| 977 | |
| 978 | client->postAutoFocus(focused); |
| 979 | |
| 980 | #if DEBUG_CLIENT_REFERENCES |
| 981 | if (client->getStrongCount() == 1) { |
| 982 | LOGE("++++++++++++++++ (AUTOFOCUS) THIS WILL CAUSE A LOCKUP!"); |
| 983 | client->printRefs(); |
| 984 | } |
| 985 | #endif |
| 986 | } |
| 987 | |
| 988 | // set preview/capture parameters - key/value pairs |
| 989 | status_t CameraService::Client::setParameters(const String8& params) |
| 990 | { |
| 991 | LOGD("setParameters(%s)", params.string()); |
| 992 | |
| 993 | Mutex::Autolock lock(mLock); |
| 994 | status_t result = checkPid(); |
| 995 | if (result != NO_ERROR) return result; |
| 996 | |
| 997 | if (mHardware == 0) { |
| 998 | LOGE("mHardware is NULL, returning."); |
| 999 | return INVALID_OPERATION; |
| 1000 | } |
| 1001 | |
| 1002 | CameraParameters p(params); |
| 1003 | mHardware->setParameters(p); |
| 1004 | return NO_ERROR; |
| 1005 | } |
| 1006 | |
| 1007 | // get preview/capture parameters - key/value pairs |
| 1008 | String8 CameraService::Client::getParameters() const |
| 1009 | { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1010 | Mutex::Autolock lock(mLock); |
| 1011 | |
| 1012 | if (mHardware == 0) { |
| 1013 | LOGE("mHardware is NULL, returning."); |
| 1014 | return String8(); |
| 1015 | } |
| 1016 | |
Wu-cheng Li | 81d763f | 2009-04-22 16:21:26 +0800 | [diff] [blame] | 1017 | String8 params(mHardware->getParameters().flatten()); |
| 1018 | LOGD("getParameters(%s)", params.string()); |
| 1019 | return params; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | void CameraService::Client::postAutoFocus(bool focused) |
| 1023 | { |
| 1024 | LOGV("postAutoFocus"); |
Dave Sparks | 93b9458 | 2009-05-07 19:27:32 -0700 | [diff] [blame] | 1025 | mCameraClient->notifyCallback(CAMERA_MSG_FOCUS, (int32_t)focused, 0); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | void CameraService::Client::postShutter() |
| 1029 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 1030 | LOGD("postShutter"); |
Dave Sparks | 93b9458 | 2009-05-07 19:27:32 -0700 | [diff] [blame] | 1031 | mCameraClient->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | void CameraService::Client::postRaw(const sp<IMemory>& mem) |
| 1035 | { |
| 1036 | LOGD("postRaw"); |
Dave Sparks | 93b9458 | 2009-05-07 19:27:32 -0700 | [diff] [blame] | 1037 | mCameraClient->dataCallback(CAMERA_MSG_RAW_IMAGE, mem); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | void CameraService::Client::postJpeg(const sp<IMemory>& mem) |
| 1041 | { |
| 1042 | LOGD("postJpeg"); |
Dave Sparks | 93b9458 | 2009-05-07 19:27:32 -0700 | [diff] [blame] | 1043 | mCameraClient->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | void CameraService::Client::copyFrameAndPostCopiedFrame(sp<IMemoryHeap> heap, size_t offset, size_t size) |
| 1047 | { |
| 1048 | LOGV("copyFrameAndPostCopiedFrame"); |
| 1049 | // It is necessary to copy out of pmem before sending this to |
| 1050 | // the callback. For efficiency, reuse the same MemoryHeapBase |
| 1051 | // provided it's big enough. Don't allocate the memory or |
| 1052 | // perform the copy if there's no callback. |
| 1053 | if (mPreviewBuffer == 0) { |
| 1054 | mPreviewBuffer = new MemoryHeapBase(size, 0, NULL); |
| 1055 | } else if (size > mPreviewBuffer->virtualSize()) { |
| 1056 | mPreviewBuffer.clear(); |
| 1057 | mPreviewBuffer = new MemoryHeapBase(size, 0, NULL); |
| 1058 | if (mPreviewBuffer == 0) { |
| 1059 | LOGE("failed to allocate space for preview buffer"); |
| 1060 | return; |
| 1061 | } |
| 1062 | } |
| 1063 | memcpy(mPreviewBuffer->base(), |
| 1064 | (uint8_t *)heap->base() + offset, size); |
| 1065 | |
| 1066 | sp<MemoryBase> frame = new MemoryBase(mPreviewBuffer, 0, size); |
| 1067 | if (frame == 0) { |
| 1068 | LOGE("failed to allocate space for frame callback"); |
| 1069 | return; |
| 1070 | } |
Dave Sparks | 93b9458 | 2009-05-07 19:27:32 -0700 | [diff] [blame] | 1071 | mCameraClient->dataCallback(CAMERA_MSG_PREVIEW_FRAME, frame); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1072 | } |
| 1073 | |
Dave Sparks | f72d640 | 2009-07-08 15:56:53 -0700 | [diff] [blame] | 1074 | void CameraService::Client::postRecordingFrame(nsecs_t timestamp, const sp<IMemory>& frame) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1075 | { |
| 1076 | LOGV("postRecordingFrame"); |
| 1077 | if (frame == 0) { |
| 1078 | LOGW("frame is a null pointer"); |
| 1079 | return; |
| 1080 | } |
Dave Sparks | f72d640 | 2009-07-08 15:56:53 -0700 | [diff] [blame] | 1081 | mCameraClient->dataCallbackTimestamp(timestamp, CAMERA_MSG_VIDEO_FRAME, frame); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | void CameraService::Client::postPreviewFrame(const sp<IMemory>& mem) |
| 1085 | { |
| 1086 | LOGV("postPreviewFrame"); |
| 1087 | if (mem == 0) { |
| 1088 | LOGW("mem is a null pointer"); |
| 1089 | return; |
| 1090 | } |
| 1091 | |
| 1092 | ssize_t offset; |
| 1093 | size_t size; |
| 1094 | sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); |
| 1095 | { |
| 1096 | Mutex::Autolock surfaceLock(mSurfaceLock); |
| 1097 | if (mSurface != NULL) { |
| 1098 | mSurface->postBuffer(offset); |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | // Is the callback enabled or not? |
| 1103 | if (!(mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ENABLE_MASK)) { |
| 1104 | // If the enable bit is off, the copy-out and one-shot bits are ignored |
| 1105 | LOGV("frame callback is diabled"); |
| 1106 | return; |
| 1107 | } |
| 1108 | |
| 1109 | // Is the received frame copied out or not? |
| 1110 | if (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_COPY_OUT_MASK) { |
| 1111 | LOGV("frame is copied out"); |
| 1112 | copyFrameAndPostCopiedFrame(heap, offset, size); |
| 1113 | } else { |
| 1114 | LOGV("frame is directly sent out without copying"); |
Dave Sparks | 93b9458 | 2009-05-07 19:27:32 -0700 | [diff] [blame] | 1115 | mCameraClient->dataCallback(CAMERA_MSG_PREVIEW_FRAME, mem); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | // Is this is one-shot only? |
| 1119 | if (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ONE_SHOT_MASK) { |
| 1120 | LOGV("One-shot only, thus clear the bits and disable frame callback"); |
| 1121 | mPreviewCallbackFlag &= ~(FRAME_CALLBACK_FLAG_ONE_SHOT_MASK | |
| 1122 | FRAME_CALLBACK_FLAG_COPY_OUT_MASK | |
| 1123 | FRAME_CALLBACK_FLAG_ENABLE_MASK); |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | void CameraService::Client::postError(status_t error) |
| 1128 | { |
Dave Sparks | 93b9458 | 2009-05-07 19:27:32 -0700 | [diff] [blame] | 1129 | mCameraClient->notifyCallback(CAMERA_MSG_ERROR, error, 0); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | status_t CameraService::dump(int fd, const Vector<String16>& args) |
| 1133 | { |
| 1134 | const size_t SIZE = 256; |
| 1135 | char buffer[SIZE]; |
| 1136 | String8 result; |
| 1137 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 1138 | snprintf(buffer, SIZE, "Permission Denial: " |
| 1139 | "can't dump CameraService from pid=%d, uid=%d\n", |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 1140 | getCallingPid(), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1141 | IPCThreadState::self()->getCallingUid()); |
| 1142 | result.append(buffer); |
| 1143 | write(fd, result.string(), result.size()); |
| 1144 | } else { |
Chih-Chung Chang | d2d6bc7 | 2009-06-24 19:59:31 +0800 | [diff] [blame] | 1145 | AutoMutex lock(&mServiceLock); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1146 | if (mClient != 0) { |
| 1147 | sp<Client> currentClient = mClient.promote(); |
| 1148 | sprintf(buffer, "Client (%p) PID: %d\n", |
| 1149 | currentClient->getCameraClient()->asBinder().get(), |
| 1150 | currentClient->mClientPid); |
| 1151 | result.append(buffer); |
| 1152 | write(fd, result.string(), result.size()); |
| 1153 | currentClient->mHardware->dump(fd, args); |
| 1154 | } else { |
| 1155 | result.append("No camera client yet.\n"); |
| 1156 | write(fd, result.string(), result.size()); |
| 1157 | } |
| 1158 | } |
| 1159 | return NO_ERROR; |
| 1160 | } |
| 1161 | |
| 1162 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1163 | status_t CameraService::onTransact( |
| 1164 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 1165 | { |
| 1166 | // permission checks... |
| 1167 | switch (code) { |
| 1168 | case BnCameraService::CONNECT: |
| 1169 | IPCThreadState* ipc = IPCThreadState::self(); |
| 1170 | const int pid = ipc->getCallingPid(); |
| 1171 | const int self_pid = getpid(); |
| 1172 | if (pid != self_pid) { |
| 1173 | // we're called from a different process, do the real check |
| 1174 | if (!checkCallingPermission( |
| 1175 | String16("android.permission.CAMERA"))) |
| 1176 | { |
| 1177 | const int uid = ipc->getCallingUid(); |
| 1178 | LOGE("Permission Denial: " |
| 1179 | "can't use the camera pid=%d, uid=%d", pid, uid); |
| 1180 | return PERMISSION_DENIED; |
| 1181 | } |
| 1182 | } |
| 1183 | break; |
| 1184 | } |
| 1185 | |
| 1186 | status_t err = BnCameraService::onTransact(code, data, reply, flags); |
| 1187 | |
Dave Sparks | 998b329 | 2009-05-20 20:02:59 -0700 | [diff] [blame] | 1188 | #if DEBUG_HEAP_LEAKS |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1189 | LOGD("+++ onTransact err %d code %d", err, code); |
| 1190 | |
| 1191 | if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) { |
| 1192 | // the 'service' command interrogates this binder for its name, and then supplies it |
| 1193 | // even for the debugging commands. that means we need to check for it here, using |
| 1194 | // ISurfaceComposer (since we delegated the INTERFACE_TRANSACTION handling to |
| 1195 | // BnSurfaceComposer before falling through to this code). |
| 1196 | |
| 1197 | LOGD("+++ onTransact code %d", code); |
| 1198 | |
| 1199 | CHECK_INTERFACE(ICameraService, data, reply); |
| 1200 | |
| 1201 | switch(code) { |
| 1202 | case 1000: |
| 1203 | { |
| 1204 | if (gWeakHeap != 0) { |
| 1205 | sp<IMemoryHeap> h = gWeakHeap.promote(); |
| 1206 | IMemoryHeap *p = gWeakHeap.unsafe_get(); |
| 1207 | LOGD("CHECKING WEAK REFERENCE %p (%p)", h.get(), p); |
| 1208 | if (h != 0) |
| 1209 | h->printRefs(); |
| 1210 | bool attempt_to_delete = data.readInt32() == 1; |
| 1211 | if (attempt_to_delete) { |
| 1212 | // NOT SAFE! |
| 1213 | LOGD("DELETING WEAK REFERENCE %p (%p)", h.get(), p); |
| 1214 | if (p) delete p; |
| 1215 | } |
| 1216 | return NO_ERROR; |
| 1217 | } |
| 1218 | } |
| 1219 | break; |
| 1220 | default: |
| 1221 | break; |
| 1222 | } |
| 1223 | } |
Dave Sparks | 998b329 | 2009-05-20 20:02:59 -0700 | [diff] [blame] | 1224 | #endif // DEBUG_HEAP_LEAKS |
Dave Sparks | fec880d | 2009-05-21 09:18:18 -0700 | [diff] [blame] | 1225 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1226 | return err; |
| 1227 | } |
| 1228 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1229 | }; // namespace android |