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