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