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