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