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