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