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