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 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | #define LOG_TAG "CameraService" |
| 20 | #include <utils/Log.h> |
| 21 | |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 22 | #include <binder/IServiceManager.h> |
| 23 | #include <binder/IPCThreadState.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | #include <utils/String16.h> |
| 25 | #include <utils/Errors.h> |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 26 | #include <binder/MemoryBase.h> |
| 27 | #include <binder/MemoryHeapBase.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | #include <ui/ICameraService.h> |
| 29 | |
Jason Sams | 78b877e | 2009-03-24 20:21:36 -0700 | [diff] [blame] | 30 | #include <media/mediaplayer.h> |
| 31 | #include <media/AudioSystem.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | #include "CameraService.h" |
| 33 | |
Chih-Chung Chang | fa89f9f | 2009-06-24 13:44:37 +0800 | [diff] [blame] | 34 | #include <cutils/atomic.h> |
Eric Laurent | cbcb00e | 2009-03-27 16:27:16 -0700 | [diff] [blame] | 35 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | namespace android { |
| 37 | |
| 38 | extern "C" { |
| 39 | #include <stdio.h> |
| 40 | #include <sys/types.h> |
| 41 | #include <sys/stat.h> |
| 42 | #include <fcntl.h> |
| 43 | #include <pthread.h> |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 44 | #include <signal.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | // When you enable this, as well as DEBUG_REFS=1 and |
| 48 | // DEBUG_REFS_ENABLED_BY_DEFAULT=0 in libutils/RefBase.cpp, this will track all |
| 49 | // references to the CameraService::Client in order to catch the case where the |
| 50 | // client is being destroyed while a callback from the CameraHardwareInterface |
| 51 | // is outstanding. This is a serious bug because if we make another call into |
| 52 | // CameraHardwreInterface that itself triggers a callback, we will deadlock. |
| 53 | |
| 54 | #define DEBUG_CLIENT_REFERENCES 0 |
| 55 | |
| 56 | #define PICTURE_TIMEOUT seconds(5) |
| 57 | |
| 58 | #define DEBUG_DUMP_PREVIEW_FRAME_TO_FILE 0 /* n-th frame to write */ |
| 59 | #define DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE 0 |
| 60 | #define DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE 0 |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 61 | #define DEBUG_DUMP_POSTVIEW_SNAPSHOT_TO_FILE 0 |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | |
| 63 | #if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE |
| 64 | static int debug_frame_cnt; |
| 65 | #endif |
| 66 | |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 67 | static int getCallingPid() { |
| 68 | return IPCThreadState::self()->getCallingPid(); |
| 69 | } |
| 70 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | // ---------------------------------------------------------------------------- |
| 72 | |
| 73 | void CameraService::instantiate() { |
| 74 | defaultServiceManager()->addService( |
| 75 | String16("media.camera"), new CameraService()); |
| 76 | } |
| 77 | |
| 78 | // ---------------------------------------------------------------------------- |
| 79 | |
| 80 | CameraService::CameraService() : |
| 81 | BnCameraService() |
| 82 | { |
| 83 | LOGI("CameraService started: pid=%d", getpid()); |
Chih-Chung Chang | fa89f9f | 2009-06-24 13:44:37 +0800 | [diff] [blame] | 84 | mUsers = 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | CameraService::~CameraService() |
| 88 | { |
| 89 | if (mClient != 0) { |
| 90 | LOGE("mClient was still connected in destructor!"); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | sp<ICamera> CameraService::connect(const sp<ICameraClient>& cameraClient) |
| 95 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 96 | int callingPid = getCallingPid(); |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 97 | LOGV("CameraService::connect E (pid %d, client %p)", callingPid, |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 98 | cameraClient->asBinder().get()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | |
Chih-Chung Chang | d2d6bc7 | 2009-06-24 19:59:31 +0800 | [diff] [blame] | 100 | Mutex::Autolock lock(mServiceLock); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | sp<Client> client; |
| 102 | if (mClient != 0) { |
| 103 | sp<Client> currentClient = mClient.promote(); |
| 104 | if (currentClient != 0) { |
| 105 | sp<ICameraClient> currentCameraClient(currentClient->getCameraClient()); |
| 106 | if (cameraClient->asBinder() == currentCameraClient->asBinder()) { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 107 | // This is the same client reconnecting... |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 108 | LOGV("CameraService::connect X (pid %d, same client %p) is reconnecting...", |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 109 | callingPid, cameraClient->asBinder().get()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | return currentClient; |
| 111 | } else { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 112 | // It's another client... reject it |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 113 | LOGV("CameraService::connect X (pid %d, new client %p) rejected. " |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 114 | "(old pid %d, old client %p)", |
| 115 | callingPid, cameraClient->asBinder().get(), |
| 116 | currentClient->mClientPid, currentCameraClient->asBinder().get()); |
Chih-Chung Chang | fa89f9f | 2009-06-24 13:44:37 +0800 | [diff] [blame] | 117 | if (kill(currentClient->mClientPid, 0) == -1 && errno == ESRCH) { |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 118 | LOGV("The old client is dead!"); |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 119 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | return client; |
| 121 | } |
| 122 | } else { |
| 123 | // can't promote, the previous client has died... |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 124 | LOGV("New client (pid %d) connecting, old reference was dangling...", |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 125 | callingPid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | mClient.clear(); |
| 127 | } |
| 128 | } |
| 129 | |
Chih-Chung Chang | d2d6bc7 | 2009-06-24 19:59:31 +0800 | [diff] [blame] | 130 | if (mUsers > 0) { |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 131 | LOGV("Still have client, rejected"); |
Chih-Chung Chang | d2d6bc7 | 2009-06-24 19:59:31 +0800 | [diff] [blame] | 132 | return client; |
| 133 | } |
| 134 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | // create a new Client object |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 136 | client = new Client(this, cameraClient, callingPid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | mClient = client; |
| 138 | #if DEBUG_CLIENT_REFERENCES |
| 139 | // Enable tracking for this object, and track increments and decrements of |
| 140 | // the refcount. |
| 141 | client->trackMe(true, true); |
| 142 | #endif |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 143 | LOGV("CameraService::connect X"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | return client; |
| 145 | } |
| 146 | |
| 147 | void CameraService::removeClient(const sp<ICameraClient>& cameraClient) |
| 148 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 149 | int callingPid = getCallingPid(); |
| 150 | |
| 151 | // Declare this outside the lock to make absolutely sure the |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | // destructor won't be called with the lock held. |
| 153 | sp<Client> client; |
| 154 | |
Chih-Chung Chang | d2d6bc7 | 2009-06-24 19:59:31 +0800 | [diff] [blame] | 155 | Mutex::Autolock lock(mServiceLock); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | |
| 157 | if (mClient == 0) { |
| 158 | // This happens when we have already disconnected. |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 159 | LOGV("removeClient (pid %d): already disconnected", callingPid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | return; |
| 161 | } |
| 162 | |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 163 | // Promote mClient. It can fail if we are called from this path: |
| 164 | // Client::~Client() -> disconnect() -> removeClient(). |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | client = mClient.promote(); |
| 166 | if (client == 0) { |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 167 | LOGV("removeClient (pid %d): no more strong reference", callingPid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | mClient.clear(); |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | if (cameraClient->asBinder() != client->getCameraClient()->asBinder()) { |
| 173 | // ugh! that's not our client!! |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 174 | LOGW("removeClient (pid %d): mClient doesn't match!", callingPid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | } else { |
| 176 | // okay, good, forget about mClient |
| 177 | mClient.clear(); |
| 178 | } |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 179 | |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 180 | LOGV("removeClient (pid %d) done", callingPid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Chih-Chung Chang | fa89f9f | 2009-06-24 13:44:37 +0800 | [diff] [blame] | 183 | // The reason we need this count is a new CameraService::connect() request may |
| 184 | // come in while the previous Client's destructor has not been run or is still |
| 185 | // running. If the last strong reference of the previous Client is gone but |
| 186 | // destructor has not been run, we should not allow the new Client to be created |
| 187 | // because we need to wait for the previous Client to tear down the hardware |
| 188 | // first. |
| 189 | void CameraService::incUsers() { |
| 190 | android_atomic_inc(&mUsers); |
| 191 | } |
| 192 | |
| 193 | void CameraService::decUsers() { |
| 194 | android_atomic_dec(&mUsers); |
| 195 | } |
| 196 | |
Wu-cheng Li | e6a550d | 2009-09-28 16:14:58 -0700 | [diff] [blame] | 197 | static sp<MediaPlayer> newMediaPlayer(const char *file) |
Jason Sams | 78b877e | 2009-03-24 20:21:36 -0700 | [diff] [blame] | 198 | { |
| 199 | sp<MediaPlayer> mp = new MediaPlayer(); |
Andreas Huber | d4461e7 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 200 | if (mp->setDataSource(file, NULL /* headers */) == NO_ERROR) { |
Eric Laurent | 9d91ad5 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 201 | mp->setAudioStreamType(AudioSystem::ENFORCED_AUDIBLE); |
Jason Sams | 78b877e | 2009-03-24 20:21:36 -0700 | [diff] [blame] | 202 | mp->prepare(); |
| 203 | } else { |
| 204 | mp.clear(); |
| 205 | LOGE("Failed to load CameraService sounds."); |
| 206 | } |
| 207 | return mp; |
| 208 | } |
| 209 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | CameraService::Client::Client(const sp<CameraService>& cameraService, |
| 211 | const sp<ICameraClient>& cameraClient, pid_t clientPid) |
| 212 | { |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 213 | int callingPid = getCallingPid(); |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 214 | LOGV("Client::Client E (pid %d)", callingPid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | mCameraService = cameraService; |
| 216 | mCameraClient = cameraClient; |
| 217 | mClientPid = clientPid; |
| 218 | mHardware = openCameraHardware(); |
| 219 | mUseOverlay = mHardware->useOverlay(); |
| 220 | |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 221 | mHardware->setCallbacks(notifyCallback, |
| 222 | dataCallback, |
| 223 | dataCallbackTimestamp, |
| 224 | mCameraService.get()); |
| 225 | |
| 226 | // Enable zoom, error, and focus messages by default |
| 227 | mHardware->enableMsgType(CAMERA_MSG_ERROR | |
| 228 | CAMERA_MSG_ZOOM | |
| 229 | CAMERA_MSG_FOCUS); |
| 230 | |
Jason Sams | 78b877e | 2009-03-24 20:21:36 -0700 | [diff] [blame] | 231 | mMediaPlayerClick = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); |
| 232 | mMediaPlayerBeep = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); |
Benny Wong | 71f7715 | 2009-07-15 18:44:27 -0500 | [diff] [blame] | 233 | mOverlayW = 0; |
| 234 | mOverlayH = 0; |
Jason Sams | 78b877e | 2009-03-24 20:21:36 -0700 | [diff] [blame] | 235 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | // Callback is disabled by default |
| 237 | mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP; |
Chih-Chung Chang | 52e7200 | 2010-01-21 17:31:06 -0800 | [diff] [blame] | 238 | mOrientation = 0; |
Chih-Chung Chang | fa89f9f | 2009-06-24 13:44:37 +0800 | [diff] [blame] | 239 | cameraService->incUsers(); |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 240 | LOGV("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(); |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 256 | LOGV("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(); |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 270 | LOGV("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; |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 276 | LOGV("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 |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 289 | LOGV("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? |
Dave Sparks | 393eb79 | 2009-10-15 10:02:22 -0700 | [diff] [blame] | 312 | if ((mCameraClient != NULL) && (client->asBinder() == mCameraClient->asBinder())) { |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 313 | LOGV("Connect to the same client"); |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 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; |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 320 | LOGV("Connect to the new client (pid %d, client %p)", |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 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 |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 347 | LOGV("Client::~Client E (pid %d, client %p)", |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 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(); |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 376 | LOGV("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 | |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 383 | LOGV("Client::disconnect() E (pid %d client %p)", |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 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) { |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 388 | LOGV("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) { |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 392 | LOGV("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 | |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 400 | LOGV("hardware teardown"); |
Chih-Chung Chang | fa89f9f | 2009-06-24 13:44:37 +0800 | [diff] [blame] | 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 | |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 424 | LOGV("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 | { |
Dave Sparks | e7e93f9 | 2010-01-04 08:55:04 -0800 | [diff] [blame] | 430 | LOGV("setPreviewDisplay(%p) (pid %d)", |
Wu-cheng Li | 988fb62 | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 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) { |
Dave Sparks | e7e93f9 | 2010-01-04 08:55:04 -0800 | [diff] [blame] | 441 | LOGV("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 | |
Dave Sparks | e7e93f9 | 2010-01-04 08:55:04 -0800 | [diff] [blame] | 490 | LOGV("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) { |
Dave Sparks | e7e93f9 | 2010-01-04 08:55:04 -0800 | [diff] [blame] | 515 | LOGV("mSurface is not set yet."); |
Wu-cheng Li | 988fb62 | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 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 | { |
Dave Sparks | e7e93f9 | 2010-01-04 08:55:04 -0800 | [diff] [blame] | 523 | LOGV("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 | { |
Dave Sparks | e7e93f9 | 2010-01-04 08:55:04 -0800 | [diff] [blame] | 550 | LOGV("setOverlay"); |
Wu-cheng Li | 988fb62 | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 551 | int w, h; |
| 552 | CameraParameters params(mHardware->getParameters()); |
| 553 | params.getPreviewSize(&w, &h); |
| 554 | |
Benny Wong | 71f7715 | 2009-07-15 18:44:27 -0500 | [diff] [blame] | 555 | if ( w != mOverlayW || h != mOverlayH ) |
| 556 | { |
| 557 | // Force the destruction of any previous overlay |
| 558 | sp<Overlay> dummy; |
| 559 | mHardware->setOverlay( dummy ); |
| 560 | mOverlayRef = 0; |
| 561 | } |
| 562 | |
Wu-cheng Li | 988fb62 | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 563 | status_t ret = NO_ERROR; |
| 564 | if (mSurface != 0) { |
Benny Wong | 71f7715 | 2009-07-15 18:44:27 -0500 | [diff] [blame] | 565 | if (mOverlayRef.get() == NULL) { |
Dave Sparks | 587f783 | 2009-10-07 19:18:20 -0700 | [diff] [blame] | 566 | |
| 567 | // FIXME: |
| 568 | // Surfaceflinger may hold onto the previous overlay reference for some |
| 569 | // time after we try to destroy it. retry a few times. In the future, we |
| 570 | // should make the destroy call block, or possibly specify that we can |
| 571 | // wait in the createOverlay call if the previous overlay is in the |
| 572 | // process of being destroyed. |
| 573 | for (int retry = 0; retry < 50; ++retry) { |
Chih-Chung Chang | 52e7200 | 2010-01-21 17:31:06 -0800 | [diff] [blame] | 574 | mOverlayRef = mSurface->createOverlay(w, h, OVERLAY_FORMAT_DEFAULT, |
| 575 | mOrientation); |
Dave Sparks | 587f783 | 2009-10-07 19:18:20 -0700 | [diff] [blame] | 576 | if (mOverlayRef != NULL) break; |
Dave Sparks | e7e93f9 | 2010-01-04 08:55:04 -0800 | [diff] [blame] | 577 | LOGW("Overlay create failed - retrying"); |
Dave Sparks | 587f783 | 2009-10-07 19:18:20 -0700 | [diff] [blame] | 578 | usleep(20000); |
| 579 | } |
Benny Wong | 71f7715 | 2009-07-15 18:44:27 -0500 | [diff] [blame] | 580 | if ( mOverlayRef.get() == NULL ) |
| 581 | { |
| 582 | LOGE("Overlay Creation Failed!"); |
| 583 | return -EINVAL; |
| 584 | } |
| 585 | ret = mHardware->setOverlay(new Overlay(mOverlayRef)); |
| 586 | } |
Wu-cheng Li | 988fb62 | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 587 | } else { |
| 588 | ret = mHardware->setOverlay(NULL); |
| 589 | } |
| 590 | if (ret != NO_ERROR) { |
| 591 | LOGE("mHardware->setOverlay() failed with status %d\n", ret); |
| 592 | } |
Benny Wong | 71f7715 | 2009-07-15 18:44:27 -0500 | [diff] [blame] | 593 | |
| 594 | mOverlayW = w; |
| 595 | mOverlayH = h; |
| 596 | |
Wu-cheng Li | 988fb62 | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 597 | return ret; |
| 598 | } |
| 599 | |
| 600 | status_t CameraService::Client::registerPreviewBuffers() |
| 601 | { |
| 602 | int w, h; |
| 603 | CameraParameters params(mHardware->getParameters()); |
| 604 | params.getPreviewSize(&w, &h); |
| 605 | |
Wu-cheng Li | 988fb62 | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 606 | ISurface::BufferHeap buffers(w, h, w, h, |
| 607 | PIXEL_FORMAT_YCbCr_420_SP, |
Chih-Chung Chang | 52e7200 | 2010-01-21 17:31:06 -0800 | [diff] [blame] | 608 | mOrientation, |
Wu-cheng Li | 988fb62 | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 609 | 0, |
| 610 | mHardware->getPreviewHeap()); |
| 611 | |
| 612 | status_t ret = mSurface->registerBuffers(buffers); |
| 613 | if (ret != NO_ERROR) { |
| 614 | LOGE("registerBuffers failed with status %d", ret); |
| 615 | } |
| 616 | return ret; |
| 617 | } |
| 618 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | status_t CameraService::Client::startPreviewMode() |
| 620 | { |
Dave Sparks | e7e93f9 | 2010-01-04 08:55:04 -0800 | [diff] [blame] | 621 | LOGV("startPreviewMode (pid %d)", getCallingPid()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 622 | |
| 623 | // if preview has been enabled, nothing needs to be done |
| 624 | if (mHardware->previewEnabled()) { |
| 625 | return NO_ERROR; |
| 626 | } |
| 627 | |
| 628 | // start preview mode |
| 629 | #if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE |
| 630 | debug_frame_cnt = 0; |
| 631 | #endif |
Wu-cheng Li | 988fb62 | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 632 | status_t ret = NO_ERROR; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | |
| 634 | if (mUseOverlay) { |
Wu-cheng Li | 988fb62 | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 635 | // If preview display has been set, set overlay now. |
| 636 | if (mSurface != 0) { |
| 637 | ret = setOverlay(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | } |
Wu-cheng Li | 988fb62 | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 639 | if (ret != NO_ERROR) return ret; |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 640 | ret = mHardware->startPreview(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 641 | } else { |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 642 | mHardware->enableMsgType(CAMERA_MSG_PREVIEW_FRAME); |
| 643 | ret = mHardware->startPreview(); |
Wu-cheng Li | 988fb62 | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 644 | if (ret != NO_ERROR) return ret; |
| 645 | // If preview display has been set, register preview buffers now. |
| 646 | if (mSurface != 0) { |
| 647 | // Unregister here because the surface registered with raw heap. |
| 648 | mSurface->unregisterBuffers(); |
| 649 | ret = registerPreviewBuffers(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | return ret; |
| 653 | } |
| 654 | |
| 655 | status_t CameraService::Client::startPreview() |
| 656 | { |
Dave Sparks | e7e93f9 | 2010-01-04 08:55:04 -0800 | [diff] [blame] | 657 | LOGV("startPreview (pid %d)", getCallingPid()); |
Wu-cheng Li | e6a550d | 2009-09-28 16:14:58 -0700 | [diff] [blame] | 658 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 659 | return startCameraMode(CAMERA_PREVIEW_MODE); |
| 660 | } |
| 661 | |
| 662 | status_t CameraService::Client::startRecording() |
| 663 | { |
Dave Sparks | e7e93f9 | 2010-01-04 08:55:04 -0800 | [diff] [blame] | 664 | LOGV("startRecording (pid %d)", getCallingPid()); |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 665 | |
Jason Sams | 78b877e | 2009-03-24 20:21:36 -0700 | [diff] [blame] | 666 | if (mMediaPlayerBeep.get() != NULL) { |
Eric Laurent | 059b413 | 2009-11-27 05:07:55 -0800 | [diff] [blame] | 667 | // do not play record jingle if stream volume is 0 |
| 668 | // (typically because ringer mode is silent). |
| 669 | int index; |
| 670 | AudioSystem::getStreamVolumeIndex(AudioSystem::ENFORCED_AUDIBLE, &index); |
| 671 | if (index != 0) { |
| 672 | mMediaPlayerBeep->seekTo(0); |
| 673 | mMediaPlayerBeep->start(); |
| 674 | } |
Jason Sams | 78b877e | 2009-03-24 20:21:36 -0700 | [diff] [blame] | 675 | } |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 676 | |
| 677 | mHardware->enableMsgType(CAMERA_MSG_VIDEO_FRAME); |
| 678 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 679 | return startCameraMode(CAMERA_RECORDING_MODE); |
| 680 | } |
| 681 | |
| 682 | // stop preview mode |
| 683 | void CameraService::Client::stopPreview() |
| 684 | { |
Dave Sparks | e7e93f9 | 2010-01-04 08:55:04 -0800 | [diff] [blame] | 685 | LOGV("stopPreview (pid %d)", getCallingPid()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 686 | |
Dave Sparks | ff0f38e | 2009-11-10 17:08:08 -0800 | [diff] [blame] | 687 | // hold main lock during state transition |
| 688 | { |
| 689 | Mutex::Autolock lock(mLock); |
| 690 | if (checkPid() != NO_ERROR) return; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 691 | |
Dave Sparks | ff0f38e | 2009-11-10 17:08:08 -0800 | [diff] [blame] | 692 | if (mHardware == 0) { |
| 693 | LOGE("mHardware is NULL, returning."); |
| 694 | return; |
| 695 | } |
| 696 | |
| 697 | mHardware->stopPreview(); |
| 698 | mHardware->disableMsgType(CAMERA_MSG_PREVIEW_FRAME); |
Dave Sparks | e7e93f9 | 2010-01-04 08:55:04 -0800 | [diff] [blame] | 699 | LOGV("stopPreview(), hardware stopped OK"); |
Dave Sparks | ff0f38e | 2009-11-10 17:08:08 -0800 | [diff] [blame] | 700 | |
| 701 | if (mSurface != 0 && !mUseOverlay) { |
| 702 | mSurface->unregisterBuffers(); |
| 703 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 704 | } |
| 705 | |
Dave Sparks | ff0f38e | 2009-11-10 17:08:08 -0800 | [diff] [blame] | 706 | // hold preview buffer lock |
| 707 | { |
| 708 | Mutex::Autolock lock(mPreviewLock); |
| 709 | mPreviewBuffer.clear(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 710 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | // stop recording mode |
| 714 | void CameraService::Client::stopRecording() |
| 715 | { |
Dave Sparks | e7e93f9 | 2010-01-04 08:55:04 -0800 | [diff] [blame] | 716 | LOGV("stopRecording (pid %d)", getCallingPid()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 717 | |
Dave Sparks | ff0f38e | 2009-11-10 17:08:08 -0800 | [diff] [blame] | 718 | // hold main lock during state transition |
| 719 | { |
| 720 | Mutex::Autolock lock(mLock); |
| 721 | if (checkPid() != NO_ERROR) return; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 722 | |
Dave Sparks | ff0f38e | 2009-11-10 17:08:08 -0800 | [diff] [blame] | 723 | if (mHardware == 0) { |
| 724 | LOGE("mHardware is NULL, returning."); |
| 725 | return; |
| 726 | } |
| 727 | |
| 728 | if (mMediaPlayerBeep.get() != NULL) { |
| 729 | mMediaPlayerBeep->seekTo(0); |
| 730 | mMediaPlayerBeep->start(); |
| 731 | } |
| 732 | |
| 733 | mHardware->stopRecording(); |
| 734 | mHardware->disableMsgType(CAMERA_MSG_VIDEO_FRAME); |
Dave Sparks | e7e93f9 | 2010-01-04 08:55:04 -0800 | [diff] [blame] | 735 | LOGV("stopRecording(), hardware stopped OK"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 736 | } |
| 737 | |
Dave Sparks | ff0f38e | 2009-11-10 17:08:08 -0800 | [diff] [blame] | 738 | // hold preview buffer lock |
| 739 | { |
| 740 | Mutex::Autolock lock(mPreviewLock); |
| 741 | mPreviewBuffer.clear(); |
Jason Sams | 78b877e | 2009-03-24 20:21:36 -0700 | [diff] [blame] | 742 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | // release a recording frame |
| 746 | void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem) |
| 747 | { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 748 | Mutex::Autolock lock(mLock); |
| 749 | if (checkPid() != NO_ERROR) return; |
| 750 | |
| 751 | if (mHardware == 0) { |
| 752 | LOGE("mHardware is NULL, returning."); |
| 753 | return; |
| 754 | } |
| 755 | |
| 756 | mHardware->releaseRecordingFrame(mem); |
| 757 | } |
| 758 | |
| 759 | bool CameraService::Client::previewEnabled() |
| 760 | { |
| 761 | Mutex::Autolock lock(mLock); |
| 762 | if (mHardware == 0) return false; |
| 763 | return mHardware->previewEnabled(); |
| 764 | } |
| 765 | |
| 766 | bool CameraService::Client::recordingEnabled() |
| 767 | { |
| 768 | Mutex::Autolock lock(mLock); |
| 769 | if (mHardware == 0) return false; |
| 770 | return mHardware->recordingEnabled(); |
| 771 | } |
| 772 | |
| 773 | // Safely retrieves a strong pointer to the client during a hardware callback. |
| 774 | sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user) |
| 775 | { |
| 776 | sp<Client> client = 0; |
| 777 | CameraService *service = static_cast<CameraService*>(user); |
| 778 | if (service != NULL) { |
Chih-Chung Chang | d2d6bc7 | 2009-06-24 19:59:31 +0800 | [diff] [blame] | 779 | Mutex::Autolock ourLock(service->mServiceLock); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 780 | if (service->mClient != 0) { |
| 781 | client = service->mClient.promote(); |
| 782 | if (client == 0) { |
| 783 | LOGE("getClientFromCookie: client appears to have died"); |
| 784 | service->mClient.clear(); |
| 785 | } |
| 786 | } else { |
| 787 | LOGE("getClientFromCookie: got callback but client was NULL"); |
| 788 | } |
| 789 | } |
| 790 | return client; |
| 791 | } |
| 792 | |
| 793 | |
| 794 | #if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE || \ |
| 795 | DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE || \ |
| 796 | DEBUG_DUMP_PREVIEW_FRAME_TO_FILE |
| 797 | static void dump_to_file(const char *fname, |
| 798 | uint8_t *buf, uint32_t size) |
| 799 | { |
| 800 | int nw, cnt = 0; |
| 801 | uint32_t written = 0; |
| 802 | |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 803 | LOGV("opening file [%s]\n", fname); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 804 | int fd = open(fname, O_RDWR | O_CREAT); |
| 805 | if (fd < 0) { |
| 806 | LOGE("failed to create file [%s]: %s", fname, strerror(errno)); |
| 807 | return; |
| 808 | } |
| 809 | |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 810 | LOGV("writing %d bytes to file [%s]\n", size, fname); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 811 | while (written < size) { |
| 812 | nw = ::write(fd, |
| 813 | buf + written, |
| 814 | size - written); |
| 815 | if (nw < 0) { |
| 816 | LOGE("failed to write to file [%s]: %s", |
| 817 | fname, strerror(errno)); |
| 818 | break; |
| 819 | } |
| 820 | written += nw; |
| 821 | cnt++; |
| 822 | } |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 823 | LOGV("done writing %d bytes to file [%s] in %d passes\n", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 824 | size, fname, cnt); |
| 825 | ::close(fd); |
| 826 | } |
| 827 | #endif |
| 828 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 829 | status_t CameraService::Client::autoFocus() |
| 830 | { |
Dave Sparks | e7e93f9 | 2010-01-04 08:55:04 -0800 | [diff] [blame] | 831 | LOGV("autoFocus (pid %d)", getCallingPid()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 832 | |
| 833 | Mutex::Autolock lock(mLock); |
| 834 | status_t result = checkPid(); |
| 835 | if (result != NO_ERROR) return result; |
| 836 | |
| 837 | if (mHardware == 0) { |
| 838 | LOGE("mHardware is NULL, returning."); |
| 839 | return INVALID_OPERATION; |
| 840 | } |
| 841 | |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 842 | return mHardware->autoFocus(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 843 | } |
| 844 | |
Chih-Chung Chang | 00900eb | 2009-09-15 14:51:56 +0800 | [diff] [blame] | 845 | status_t CameraService::Client::cancelAutoFocus() |
| 846 | { |
Dave Sparks | e7e93f9 | 2010-01-04 08:55:04 -0800 | [diff] [blame] | 847 | LOGV("cancelAutoFocus (pid %d)", getCallingPid()); |
Chih-Chung Chang | 00900eb | 2009-09-15 14:51:56 +0800 | [diff] [blame] | 848 | |
| 849 | Mutex::Autolock lock(mLock); |
| 850 | status_t result = checkPid(); |
| 851 | if (result != NO_ERROR) return result; |
| 852 | |
| 853 | if (mHardware == 0) { |
| 854 | LOGE("mHardware is NULL, returning."); |
| 855 | return INVALID_OPERATION; |
| 856 | } |
| 857 | |
| 858 | return mHardware->cancelAutoFocus(); |
| 859 | } |
| 860 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 861 | // take a picture - image is returned in callback |
| 862 | status_t CameraService::Client::takePicture() |
| 863 | { |
Dave Sparks | e7e93f9 | 2010-01-04 08:55:04 -0800 | [diff] [blame] | 864 | LOGV("takePicture (pid %d)", getCallingPid()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 865 | |
| 866 | Mutex::Autolock lock(mLock); |
| 867 | status_t result = checkPid(); |
| 868 | if (result != NO_ERROR) return result; |
| 869 | |
| 870 | if (mHardware == 0) { |
| 871 | LOGE("mHardware is NULL, returning."); |
| 872 | return INVALID_OPERATION; |
| 873 | } |
| 874 | |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 875 | mHardware->enableMsgType(CAMERA_MSG_SHUTTER | |
| 876 | CAMERA_MSG_POSTVIEW_FRAME | |
| 877 | CAMERA_MSG_RAW_IMAGE | |
| 878 | CAMERA_MSG_COMPRESSED_IMAGE); |
| 879 | |
| 880 | return mHardware->takePicture(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 881 | } |
| 882 | |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 883 | // snapshot taken |
Wu-cheng Li | 986e0dc | 2009-10-23 17:39:46 +0800 | [diff] [blame] | 884 | void CameraService::Client::handleShutter( |
| 885 | image_rect_type *size // The width and height of yuv picture for |
| 886 | // registerBuffer. If this is NULL, use the picture |
| 887 | // size from parameters. |
| 888 | ) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 889 | { |
Wu-cheng Li | aab44b8 | 2009-03-24 20:39:09 -0700 | [diff] [blame] | 890 | // Play shutter sound. |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 891 | if (mMediaPlayerClick.get() != NULL) { |
Eric Laurent | 059b413 | 2009-11-27 05:07:55 -0800 | [diff] [blame] | 892 | // do not play shutter sound if stream volume is 0 |
| 893 | // (typically because ringer mode is silent). |
| 894 | int index; |
| 895 | AudioSystem::getStreamVolumeIndex(AudioSystem::ENFORCED_AUDIBLE, &index); |
| 896 | if (index != 0) { |
| 897 | mMediaPlayerClick->seekTo(0); |
| 898 | mMediaPlayerClick->start(); |
| 899 | } |
Wu-cheng Li | aab44b8 | 2009-03-24 20:39:09 -0700 | [diff] [blame] | 900 | } |
| 901 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 902 | // Screen goes black after the buffer is unregistered. |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 903 | if (mSurface != 0 && !mUseOverlay) { |
| 904 | mSurface->unregisterBuffers(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 905 | } |
| 906 | |
Dave Sparks | 393eb79 | 2009-10-15 10:02:22 -0700 | [diff] [blame] | 907 | sp<ICameraClient> c = mCameraClient; |
| 908 | if (c != NULL) { |
| 909 | c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0); |
| 910 | } |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 911 | mHardware->disableMsgType(CAMERA_MSG_SHUTTER); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 912 | |
| 913 | // It takes some time before yuvPicture callback to be called. |
| 914 | // Register the buffer for raw image here to reduce latency. |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 915 | if (mSurface != 0 && !mUseOverlay) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 916 | int w, h; |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 917 | CameraParameters params(mHardware->getParameters()); |
Wu-cheng Li | 986e0dc | 2009-10-23 17:39:46 +0800 | [diff] [blame] | 918 | if (size == NULL) { |
| 919 | params.getPictureSize(&w, &h); |
| 920 | } else { |
| 921 | w = size->width; |
| 922 | h = size->height; |
| 923 | w &= ~1; |
| 924 | h &= ~1; |
Dave Sparks | e7e93f9 | 2010-01-04 08:55:04 -0800 | [diff] [blame] | 925 | LOGV("Snapshot image width=%d, height=%d", w, h); |
Wu-cheng Li | 986e0dc | 2009-10-23 17:39:46 +0800 | [diff] [blame] | 926 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 927 | ISurface::BufferHeap buffers(w, h, w, h, |
Chih-Chung Chang | 52e7200 | 2010-01-21 17:31:06 -0800 | [diff] [blame] | 928 | PIXEL_FORMAT_YCbCr_420_SP, mOrientation, 0, mHardware->getRawHeap()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 929 | |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 930 | mSurface->registerBuffers(buffers); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 931 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 932 | } |
| 933 | |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 934 | // preview callback - frame buffer update |
| 935 | void CameraService::Client::handlePreviewData(const sp<IMemory>& mem) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 936 | { |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 937 | ssize_t offset; |
| 938 | size_t size; |
| 939 | sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); |
| 940 | |
| 941 | #if DEBUG_HEAP_LEAKS && 0 // debugging |
| 942 | if (gWeakHeap == NULL) { |
| 943 | if (gWeakHeap != heap) { |
Dave Sparks | e7e93f9 | 2010-01-04 08:55:04 -0800 | [diff] [blame] | 944 | LOGV("SETTING PREVIEW HEAP"); |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 945 | heap->trackMe(true, true); |
| 946 | gWeakHeap = heap; |
| 947 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 948 | } |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 949 | #endif |
| 950 | #if DEBUG_DUMP_PREVIEW_FRAME_TO_FILE |
| 951 | { |
| 952 | if (debug_frame_cnt++ == DEBUG_DUMP_PREVIEW_FRAME_TO_FILE) { |
| 953 | dump_to_file("/data/preview.yuv", |
| 954 | (uint8_t *)heap->base() + offset, size); |
| 955 | } |
| 956 | } |
| 957 | #endif |
| 958 | |
| 959 | if (!mUseOverlay) |
| 960 | { |
| 961 | Mutex::Autolock surfaceLock(mSurfaceLock); |
| 962 | if (mSurface != NULL) { |
| 963 | mSurface->postBuffer(offset); |
| 964 | } |
| 965 | } |
| 966 | |
Dave Sparks | 393eb79 | 2009-10-15 10:02:22 -0700 | [diff] [blame] | 967 | // local copy of the callback flags |
| 968 | int flags = mPreviewCallbackFlag; |
| 969 | |
| 970 | // is callback enabled? |
| 971 | if (!(flags & FRAME_CALLBACK_FLAG_ENABLE_MASK)) { |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 972 | // If the enable bit is off, the copy-out and one-shot bits are ignored |
| 973 | LOGV("frame callback is diabled"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 974 | return; |
| 975 | } |
| 976 | |
Dave Sparks | 393eb79 | 2009-10-15 10:02:22 -0700 | [diff] [blame] | 977 | // hold a strong pointer to the client |
| 978 | sp<ICameraClient> c = mCameraClient; |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 979 | |
Dave Sparks | 393eb79 | 2009-10-15 10:02:22 -0700 | [diff] [blame] | 980 | // clear callback flags if no client or one-shot mode |
| 981 | if ((c == NULL) || (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) { |
| 982 | LOGV("Disable preview callback"); |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 983 | mPreviewCallbackFlag &= ~(FRAME_CALLBACK_FLAG_ONE_SHOT_MASK | |
| 984 | FRAME_CALLBACK_FLAG_COPY_OUT_MASK | |
| 985 | FRAME_CALLBACK_FLAG_ENABLE_MASK); |
Dave Sparks | 393eb79 | 2009-10-15 10:02:22 -0700 | [diff] [blame] | 986 | // TODO: Shouldn't we use this API for non-overlay hardware as well? |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 987 | if (mUseOverlay) |
| 988 | mHardware->disableMsgType(CAMERA_MSG_PREVIEW_FRAME); |
| 989 | } |
Dave Sparks | 393eb79 | 2009-10-15 10:02:22 -0700 | [diff] [blame] | 990 | |
| 991 | // Is the received frame copied out or not? |
| 992 | if (flags & FRAME_CALLBACK_FLAG_COPY_OUT_MASK) { |
| 993 | LOGV("frame is copied"); |
| 994 | copyFrameAndPostCopiedFrame(c, heap, offset, size); |
| 995 | } else { |
| 996 | LOGV("frame is forwarded"); |
| 997 | c->dataCallback(CAMERA_MSG_PREVIEW_FRAME, mem); |
| 998 | } |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | // picture callback - postview image ready |
| 1002 | void CameraService::Client::handlePostview(const sp<IMemory>& mem) |
| 1003 | { |
| 1004 | #if DEBUG_DUMP_POSTVIEW_SNAPSHOT_TO_FILE // for testing pursposes only |
| 1005 | { |
| 1006 | ssize_t offset; |
| 1007 | size_t size; |
| 1008 | sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); |
| 1009 | dump_to_file("/data/postview.yuv", |
| 1010 | (uint8_t *)heap->base() + offset, size); |
| 1011 | } |
| 1012 | #endif |
| 1013 | |
Dave Sparks | 393eb79 | 2009-10-15 10:02:22 -0700 | [diff] [blame] | 1014 | sp<ICameraClient> c = mCameraClient; |
| 1015 | if (c != NULL) { |
| 1016 | c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem); |
| 1017 | } |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 1018 | mHardware->disableMsgType(CAMERA_MSG_POSTVIEW_FRAME); |
| 1019 | } |
| 1020 | |
| 1021 | // picture callback - raw image ready |
| 1022 | void CameraService::Client::handleRawPicture(const sp<IMemory>& mem) |
| 1023 | { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1024 | ssize_t offset; |
| 1025 | size_t size; |
| 1026 | sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); |
| 1027 | #if DEBUG_HEAP_LEAKS && 0 // debugging |
| 1028 | gWeakHeap = heap; // debugging |
| 1029 | #endif |
| 1030 | |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 1031 | //LOGV("handleRawPicture(%d, %d)", offset, size); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1032 | #if DEBUG_DUMP_YUV_SNAPSHOT_TO_FILE // for testing pursposes only |
| 1033 | dump_to_file("/data/photo.yuv", |
| 1034 | (uint8_t *)heap->base() + offset, size); |
| 1035 | #endif |
| 1036 | |
| 1037 | // Put the YUV version of the snapshot in the preview display. |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 1038 | if (mSurface != 0 && !mUseOverlay) { |
| 1039 | mSurface->postBuffer(offset); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1040 | } |
| 1041 | |
Dave Sparks | 393eb79 | 2009-10-15 10:02:22 -0700 | [diff] [blame] | 1042 | sp<ICameraClient> c = mCameraClient; |
| 1043 | if (c != NULL) { |
| 1044 | c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem); |
| 1045 | } |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 1046 | mHardware->disableMsgType(CAMERA_MSG_RAW_IMAGE); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1047 | } |
| 1048 | |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 1049 | // picture callback - compressed picture ready |
| 1050 | void CameraService::Client::handleCompressedPicture(const sp<IMemory>& mem) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1051 | { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1052 | #if DEBUG_DUMP_JPEG_SNAPSHOT_TO_FILE // for testing pursposes only |
| 1053 | { |
| 1054 | ssize_t offset; |
| 1055 | size_t size; |
| 1056 | sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); |
| 1057 | dump_to_file("/data/photo.jpg", |
| 1058 | (uint8_t *)heap->base() + offset, size); |
| 1059 | } |
| 1060 | #endif |
| 1061 | |
Dave Sparks | 393eb79 | 2009-10-15 10:02:22 -0700 | [diff] [blame] | 1062 | sp<ICameraClient> c = mCameraClient; |
| 1063 | if (c != NULL) { |
| 1064 | c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem); |
| 1065 | } |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 1066 | mHardware->disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1067 | } |
| 1068 | |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 1069 | 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] | 1070 | { |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 1071 | LOGV("notifyCallback(%d)", msgType); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1072 | |
| 1073 | sp<Client> client = getClientFromCookie(user); |
| 1074 | if (client == 0) { |
| 1075 | return; |
| 1076 | } |
| 1077 | |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 1078 | switch (msgType) { |
| 1079 | case CAMERA_MSG_SHUTTER: |
Wu-cheng Li | 986e0dc | 2009-10-23 17:39:46 +0800 | [diff] [blame] | 1080 | // ext1 is the dimension of the yuv picture. |
| 1081 | client->handleShutter((image_rect_type *)ext1); |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 1082 | break; |
| 1083 | default: |
Dave Sparks | 393eb79 | 2009-10-15 10:02:22 -0700 | [diff] [blame] | 1084 | sp<ICameraClient> c = client->mCameraClient; |
| 1085 | if (c != NULL) { |
| 1086 | c->notifyCallback(msgType, ext1, ext2); |
| 1087 | } |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 1088 | break; |
| 1089 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1090 | |
| 1091 | #if DEBUG_CLIENT_REFERENCES |
| 1092 | if (client->getStrongCount() == 1) { |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 1093 | LOGE("++++++++++++++++ (NOTIFY CALLBACK) THIS WILL CAUSE A LOCKUP!"); |
| 1094 | client->printRefs(); |
| 1095 | } |
| 1096 | #endif |
| 1097 | } |
| 1098 | |
| 1099 | void CameraService::Client::dataCallback(int32_t msgType, const sp<IMemory>& dataPtr, void* user) |
| 1100 | { |
| 1101 | LOGV("dataCallback(%d)", msgType); |
| 1102 | |
| 1103 | sp<Client> client = getClientFromCookie(user); |
| 1104 | if (client == 0) { |
| 1105 | return; |
| 1106 | } |
| 1107 | |
Dave Sparks | 393eb79 | 2009-10-15 10:02:22 -0700 | [diff] [blame] | 1108 | sp<ICameraClient> c = client->mCameraClient; |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 1109 | if (dataPtr == NULL) { |
| 1110 | LOGE("Null data returned in data callback"); |
Dave Sparks | 393eb79 | 2009-10-15 10:02:22 -0700 | [diff] [blame] | 1111 | if (c != NULL) { |
| 1112 | c->notifyCallback(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0); |
| 1113 | c->dataCallback(msgType, NULL); |
| 1114 | } |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 1115 | return; |
| 1116 | } |
| 1117 | |
| 1118 | switch (msgType) { |
| 1119 | case CAMERA_MSG_PREVIEW_FRAME: |
| 1120 | client->handlePreviewData(dataPtr); |
| 1121 | break; |
| 1122 | case CAMERA_MSG_POSTVIEW_FRAME: |
| 1123 | client->handlePostview(dataPtr); |
| 1124 | break; |
| 1125 | case CAMERA_MSG_RAW_IMAGE: |
| 1126 | client->handleRawPicture(dataPtr); |
| 1127 | break; |
| 1128 | case CAMERA_MSG_COMPRESSED_IMAGE: |
| 1129 | client->handleCompressedPicture(dataPtr); |
| 1130 | break; |
| 1131 | default: |
Dave Sparks | 393eb79 | 2009-10-15 10:02:22 -0700 | [diff] [blame] | 1132 | if (c != NULL) { |
| 1133 | c->dataCallback(msgType, dataPtr); |
| 1134 | } |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 1135 | break; |
| 1136 | } |
| 1137 | |
| 1138 | #if DEBUG_CLIENT_REFERENCES |
| 1139 | if (client->getStrongCount() == 1) { |
| 1140 | LOGE("++++++++++++++++ (DATA CALLBACK) THIS WILL CAUSE A LOCKUP!"); |
| 1141 | client->printRefs(); |
| 1142 | } |
| 1143 | #endif |
| 1144 | } |
| 1145 | |
| 1146 | void CameraService::Client::dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, |
| 1147 | const sp<IMemory>& dataPtr, void* user) |
| 1148 | { |
| 1149 | LOGV("dataCallbackTimestamp(%d)", msgType); |
| 1150 | |
| 1151 | sp<Client> client = getClientFromCookie(user); |
| 1152 | if (client == 0) { |
| 1153 | return; |
| 1154 | } |
Dave Sparks | 393eb79 | 2009-10-15 10:02:22 -0700 | [diff] [blame] | 1155 | sp<ICameraClient> c = client->mCameraClient; |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 1156 | |
| 1157 | if (dataPtr == NULL) { |
| 1158 | LOGE("Null data returned in data with timestamp callback"); |
Dave Sparks | 393eb79 | 2009-10-15 10:02:22 -0700 | [diff] [blame] | 1159 | if (c != NULL) { |
| 1160 | c->notifyCallback(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0); |
| 1161 | c->dataCallbackTimestamp(0, msgType, NULL); |
| 1162 | } |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 1163 | return; |
| 1164 | } |
| 1165 | |
Dave Sparks | 393eb79 | 2009-10-15 10:02:22 -0700 | [diff] [blame] | 1166 | if (c != NULL) { |
| 1167 | c->dataCallbackTimestamp(timestamp, msgType, dataPtr); |
| 1168 | } |
Benny Wong | 4c8fb0a | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 1169 | |
| 1170 | #if DEBUG_CLIENT_REFERENCES |
| 1171 | if (client->getStrongCount() == 1) { |
| 1172 | LOGE("++++++++++++++++ (DATA CALLBACK TIMESTAMP) THIS WILL CAUSE A LOCKUP!"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1173 | client->printRefs(); |
| 1174 | } |
| 1175 | #endif |
| 1176 | } |
| 1177 | |
| 1178 | // set preview/capture parameters - key/value pairs |
| 1179 | status_t CameraService::Client::setParameters(const String8& params) |
| 1180 | { |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 1181 | LOGV("setParameters(%s)", params.string()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1182 | |
| 1183 | Mutex::Autolock lock(mLock); |
| 1184 | status_t result = checkPid(); |
| 1185 | if (result != NO_ERROR) return result; |
| 1186 | |
| 1187 | if (mHardware == 0) { |
| 1188 | LOGE("mHardware is NULL, returning."); |
| 1189 | return INVALID_OPERATION; |
| 1190 | } |
| 1191 | |
| 1192 | CameraParameters p(params); |
Chih-Chung Chang | 52e7200 | 2010-01-21 17:31:06 -0800 | [diff] [blame] | 1193 | |
James Dong | 102f777 | 2009-09-13 17:10:24 -0700 | [diff] [blame] | 1194 | return mHardware->setParameters(p); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | // get preview/capture parameters - key/value pairs |
| 1198 | String8 CameraService::Client::getParameters() const |
| 1199 | { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1200 | Mutex::Autolock lock(mLock); |
| 1201 | |
| 1202 | if (mHardware == 0) { |
| 1203 | LOGE("mHardware is NULL, returning."); |
| 1204 | return String8(); |
| 1205 | } |
| 1206 | |
Wu-cheng Li | 81d763f | 2009-04-22 16:21:26 +0800 | [diff] [blame] | 1207 | String8 params(mHardware->getParameters().flatten()); |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 1208 | LOGV("getParameters(%s)", params.string()); |
Wu-cheng Li | 81d763f | 2009-04-22 16:21:26 +0800 | [diff] [blame] | 1209 | return params; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1210 | } |
| 1211 | |
Wu-cheng Li | e6a550d | 2009-09-28 16:14:58 -0700 | [diff] [blame] | 1212 | status_t CameraService::Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) |
| 1213 | { |
Dave Sparks | e7e93f9 | 2010-01-04 08:55:04 -0800 | [diff] [blame] | 1214 | LOGV("sendCommand (pid %d)", getCallingPid()); |
Wu-cheng Li | e6a550d | 2009-09-28 16:14:58 -0700 | [diff] [blame] | 1215 | Mutex::Autolock lock(mLock); |
| 1216 | status_t result = checkPid(); |
| 1217 | if (result != NO_ERROR) return result; |
| 1218 | |
Chih-Chung Chang | f091e83 | 2010-01-22 17:49:48 -0800 | [diff] [blame] | 1219 | if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) { |
| 1220 | // The orientation cannot be set during preview. |
| 1221 | if (mHardware->previewEnabled()) { |
| 1222 | return INVALID_OPERATION; |
| 1223 | } |
| 1224 | switch (arg1) { |
| 1225 | case 0: |
| 1226 | mOrientation = ISurface::BufferHeap::ROT_0; |
| 1227 | break; |
| 1228 | case 90: |
| 1229 | mOrientation = ISurface::BufferHeap::ROT_90; |
| 1230 | break; |
| 1231 | case 180: |
| 1232 | mOrientation = ISurface::BufferHeap::ROT_180; |
| 1233 | break; |
| 1234 | case 270: |
| 1235 | mOrientation = ISurface::BufferHeap::ROT_270; |
| 1236 | break; |
| 1237 | default: |
| 1238 | return BAD_VALUE; |
| 1239 | } |
| 1240 | return OK; |
| 1241 | } |
| 1242 | |
Wu-cheng Li | e6a550d | 2009-09-28 16:14:58 -0700 | [diff] [blame] | 1243 | if (mHardware == 0) { |
| 1244 | LOGE("mHardware is NULL, returning."); |
| 1245 | return INVALID_OPERATION; |
| 1246 | } |
| 1247 | |
| 1248 | return mHardware->sendCommand(cmd, arg1, arg2); |
| 1249 | } |
| 1250 | |
Dave Sparks | 393eb79 | 2009-10-15 10:02:22 -0700 | [diff] [blame] | 1251 | void CameraService::Client::copyFrameAndPostCopiedFrame(const sp<ICameraClient>& client, |
| 1252 | const sp<IMemoryHeap>& heap, size_t offset, size_t size) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1253 | { |
| 1254 | LOGV("copyFrameAndPostCopiedFrame"); |
| 1255 | // It is necessary to copy out of pmem before sending this to |
| 1256 | // the callback. For efficiency, reuse the same MemoryHeapBase |
| 1257 | // provided it's big enough. Don't allocate the memory or |
| 1258 | // perform the copy if there's no callback. |
Dave Sparks | 23c21ba | 2009-11-06 11:47:13 -0800 | [diff] [blame] | 1259 | |
Dave Sparks | ff0f38e | 2009-11-10 17:08:08 -0800 | [diff] [blame] | 1260 | // hold the preview lock while we grab a reference to the preview buffer |
Dave Sparks | 23c21ba | 2009-11-06 11:47:13 -0800 | [diff] [blame] | 1261 | sp<MemoryHeapBase> previewBuffer; |
| 1262 | { |
Dave Sparks | ff0f38e | 2009-11-10 17:08:08 -0800 | [diff] [blame] | 1263 | Mutex::Autolock lock(mPreviewLock); |
Dave Sparks | 23c21ba | 2009-11-06 11:47:13 -0800 | [diff] [blame] | 1264 | if (mPreviewBuffer == 0) { |
| 1265 | mPreviewBuffer = new MemoryHeapBase(size, 0, NULL); |
| 1266 | } else if (size > mPreviewBuffer->virtualSize()) { |
| 1267 | mPreviewBuffer.clear(); |
| 1268 | mPreviewBuffer = new MemoryHeapBase(size, 0, NULL); |
| 1269 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1270 | if (mPreviewBuffer == 0) { |
| 1271 | LOGE("failed to allocate space for preview buffer"); |
| 1272 | return; |
| 1273 | } |
Dave Sparks | 23c21ba | 2009-11-06 11:47:13 -0800 | [diff] [blame] | 1274 | previewBuffer = mPreviewBuffer; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1275 | } |
Dave Sparks | 23c21ba | 2009-11-06 11:47:13 -0800 | [diff] [blame] | 1276 | memcpy(previewBuffer->base(), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1277 | (uint8_t *)heap->base() + offset, size); |
| 1278 | |
Dave Sparks | 23c21ba | 2009-11-06 11:47:13 -0800 | [diff] [blame] | 1279 | sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1280 | if (frame == 0) { |
| 1281 | LOGE("failed to allocate space for frame callback"); |
| 1282 | return; |
| 1283 | } |
Dave Sparks | 393eb79 | 2009-10-15 10:02:22 -0700 | [diff] [blame] | 1284 | client->dataCallback(CAMERA_MSG_PREVIEW_FRAME, frame); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1285 | } |
| 1286 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1287 | status_t CameraService::dump(int fd, const Vector<String16>& args) |
| 1288 | { |
| 1289 | const size_t SIZE = 256; |
| 1290 | char buffer[SIZE]; |
| 1291 | String8 result; |
| 1292 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 1293 | snprintf(buffer, SIZE, "Permission Denial: " |
| 1294 | "can't dump CameraService from pid=%d, uid=%d\n", |
Chih-Chung Chang | d98c516 | 2009-06-22 16:03:41 +0800 | [diff] [blame] | 1295 | getCallingPid(), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1296 | IPCThreadState::self()->getCallingUid()); |
| 1297 | result.append(buffer); |
| 1298 | write(fd, result.string(), result.size()); |
| 1299 | } else { |
Chih-Chung Chang | d2d6bc7 | 2009-06-24 19:59:31 +0800 | [diff] [blame] | 1300 | AutoMutex lock(&mServiceLock); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1301 | if (mClient != 0) { |
| 1302 | sp<Client> currentClient = mClient.promote(); |
| 1303 | sprintf(buffer, "Client (%p) PID: %d\n", |
| 1304 | currentClient->getCameraClient()->asBinder().get(), |
| 1305 | currentClient->mClientPid); |
| 1306 | result.append(buffer); |
| 1307 | write(fd, result.string(), result.size()); |
| 1308 | currentClient->mHardware->dump(fd, args); |
| 1309 | } else { |
| 1310 | result.append("No camera client yet.\n"); |
| 1311 | write(fd, result.string(), result.size()); |
| 1312 | } |
| 1313 | } |
| 1314 | return NO_ERROR; |
| 1315 | } |
| 1316 | |
| 1317 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1318 | status_t CameraService::onTransact( |
| 1319 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 1320 | { |
| 1321 | // permission checks... |
| 1322 | switch (code) { |
| 1323 | case BnCameraService::CONNECT: |
| 1324 | IPCThreadState* ipc = IPCThreadState::self(); |
| 1325 | const int pid = ipc->getCallingPid(); |
| 1326 | const int self_pid = getpid(); |
| 1327 | if (pid != self_pid) { |
| 1328 | // we're called from a different process, do the real check |
| 1329 | if (!checkCallingPermission( |
| 1330 | String16("android.permission.CAMERA"))) |
| 1331 | { |
| 1332 | const int uid = ipc->getCallingUid(); |
| 1333 | LOGE("Permission Denial: " |
| 1334 | "can't use the camera pid=%d, uid=%d", pid, uid); |
| 1335 | return PERMISSION_DENIED; |
| 1336 | } |
| 1337 | } |
| 1338 | break; |
| 1339 | } |
| 1340 | |
| 1341 | status_t err = BnCameraService::onTransact(code, data, reply, flags); |
| 1342 | |
Dave Sparks | 998b329 | 2009-05-20 20:02:59 -0700 | [diff] [blame] | 1343 | #if DEBUG_HEAP_LEAKS |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 1344 | LOGV("+++ onTransact err %d code %d", err, code); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1345 | |
| 1346 | if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) { |
| 1347 | // the 'service' command interrogates this binder for its name, and then supplies it |
| 1348 | // even for the debugging commands. that means we need to check for it here, using |
| 1349 | // ISurfaceComposer (since we delegated the INTERFACE_TRANSACTION handling to |
| 1350 | // BnSurfaceComposer before falling through to this code). |
| 1351 | |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 1352 | LOGV("+++ onTransact code %d", code); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1353 | |
| 1354 | CHECK_INTERFACE(ICameraService, data, reply); |
| 1355 | |
| 1356 | switch(code) { |
| 1357 | case 1000: |
| 1358 | { |
| 1359 | if (gWeakHeap != 0) { |
| 1360 | sp<IMemoryHeap> h = gWeakHeap.promote(); |
| 1361 | IMemoryHeap *p = gWeakHeap.unsafe_get(); |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 1362 | LOGV("CHECKING WEAK REFERENCE %p (%p)", h.get(), p); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1363 | if (h != 0) |
| 1364 | h->printRefs(); |
| 1365 | bool attempt_to_delete = data.readInt32() == 1; |
| 1366 | if (attempt_to_delete) { |
| 1367 | // NOT SAFE! |
Joe Onorato | 51632e8 | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 1368 | LOGV("DELETING WEAK REFERENCE %p (%p)", h.get(), p); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1369 | if (p) delete p; |
| 1370 | } |
| 1371 | return NO_ERROR; |
| 1372 | } |
| 1373 | } |
| 1374 | break; |
| 1375 | default: |
| 1376 | break; |
| 1377 | } |
| 1378 | } |
Dave Sparks | 998b329 | 2009-05-20 20:02:59 -0700 | [diff] [blame] | 1379 | #endif // DEBUG_HEAP_LEAKS |
Dave Sparks | fec880d | 2009-05-21 09:18:18 -0700 | [diff] [blame] | 1380 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1381 | return err; |
| 1382 | } |
| 1383 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1384 | }; // namespace android |