| Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2012 The Android Open Source Project | 
 | 3 |  * | 
 | 4 |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 5 |  * you may not use this file except in compliance with the License. | 
 | 6 |  * You may obtain a copy of the License at | 
 | 7 |  * | 
 | 8 |  *      http://www.apache.org/licenses/LICENSE-2.0 | 
 | 9 |  * | 
 | 10 |  * Unless required by applicable law or agreed to in writing, software | 
 | 11 |  * distributed under the License is distributed on an "AS IS" BASIS, | 
 | 12 |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 13 |  * See the License for the specific language governing permissions and | 
 | 14 |  * limitations under the License. | 
 | 15 |  */ | 
 | 16 |  | 
 | 17 | #define LOG_TAG "CameraClient" | 
 | 18 | //#define LOG_NDEBUG 0 | 
 | 19 |  | 
 | 20 | #include <cutils/properties.h> | 
 | 21 | #include <gui/SurfaceTextureClient.h> | 
 | 22 | #include <gui/Surface.h> | 
 | 23 |  | 
 | 24 | #include "CameraClient.h" | 
 | 25 | #include "CameraHardwareInterface.h" | 
 | 26 | #include "CameraService.h" | 
 | 27 |  | 
 | 28 | namespace android { | 
 | 29 |  | 
 | 30 | #define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__); | 
 | 31 | #define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__); | 
 | 32 |  | 
 | 33 | static int getCallingPid() { | 
 | 34 |     return IPCThreadState::self()->getCallingPid(); | 
 | 35 | } | 
 | 36 |  | 
| Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 37 | CameraClient::CameraClient(const sp<CameraService>& cameraService, | 
 | 38 |         const sp<ICameraClient>& cameraClient, | 
| Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 39 |         int cameraId, int cameraFacing, int clientPid, int servicePid): | 
| Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 40 |         Client(cameraService, cameraClient, | 
| Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 41 |                 cameraId, cameraFacing, clientPid, servicePid) | 
| Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 42 | { | 
 | 43 |     int callingPid = getCallingPid(); | 
 | 44 |     LOG1("CameraClient::CameraClient E (pid %d, id %d)", callingPid, cameraId); | 
 | 45 |  | 
| Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 46 |     mHardware = NULL; | 
| Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 47 |     mMsgEnabled = 0; | 
 | 48 |     mSurface = 0; | 
 | 49 |     mPreviewWindow = 0; | 
 | 50 |     mDestructionStarted = false; | 
| Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 51 |  | 
 | 52 |     // Callback is disabled by default | 
 | 53 |     mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP; | 
 | 54 |     mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT); | 
 | 55 |     mPlayShutterSound = true; | 
 | 56 |     LOG1("CameraClient::CameraClient X (pid %d, id %d)", callingPid, cameraId); | 
 | 57 | } | 
 | 58 |  | 
| Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 59 | status_t CameraClient::initialize(camera_module_t *module) { | 
 | 60 |     int callingPid = getCallingPid(); | 
 | 61 |     LOG1("CameraClient::initialize E (pid %d, id %d)", callingPid, mCameraId); | 
 | 62 |  | 
 | 63 |     char camera_device_name[10]; | 
 | 64 |     status_t res; | 
 | 65 |     snprintf(camera_device_name, sizeof(camera_device_name), "%d", mCameraId); | 
 | 66 |  | 
 | 67 |     mHardware = new CameraHardwareInterface(camera_device_name); | 
 | 68 |     res = mHardware->initialize(&module->common); | 
 | 69 |     if (res != OK) { | 
 | 70 |         ALOGE("%s: Camera %d: unable to initialize device: %s (%d)", | 
 | 71 |                 __FUNCTION__, mCameraId, strerror(-res), res); | 
| Igor Murashkin | 44f120f | 2012-10-09 14:45:37 -0700 | [diff] [blame] | 72 |         mHardware.clear(); | 
| Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 73 |         return NO_INIT; | 
 | 74 |     } | 
 | 75 |  | 
 | 76 |     mHardware->setCallbacks(notifyCallback, | 
 | 77 |             dataCallback, | 
 | 78 |             dataCallbackTimestamp, | 
 | 79 |             (void *)mCameraId); | 
 | 80 |  | 
 | 81 |     // Enable zoom, error, focus, and metadata messages by default | 
 | 82 |     enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS | | 
 | 83 |                   CAMERA_MSG_PREVIEW_METADATA | CAMERA_MSG_FOCUS_MOVE); | 
 | 84 |  | 
 | 85 |     LOG1("CameraClient::initialize X (pid %d, id %d)", callingPid, mCameraId); | 
 | 86 |     return OK; | 
 | 87 | } | 
 | 88 |  | 
 | 89 |  | 
| Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 90 | // tear down the client | 
 | 91 | CameraClient::~CameraClient() { | 
 | 92 |     // this lock should never be NULL | 
 | 93 |     Mutex* lock = mCameraService->getClientLockById(mCameraId); | 
 | 94 |     lock->lock(); | 
 | 95 |     mDestructionStarted = true; | 
 | 96 |     // client will not be accessed from callback. should unlock to prevent dead-lock in disconnect | 
 | 97 |     lock->unlock(); | 
 | 98 |     int callingPid = getCallingPid(); | 
 | 99 |     LOG1("CameraClient::~CameraClient E (pid %d, this %p)", callingPid, this); | 
 | 100 |  | 
| Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 101 |     disconnect(); | 
| Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 102 |     LOG1("CameraClient::~CameraClient X (pid %d, this %p)", callingPid, this); | 
 | 103 | } | 
 | 104 |  | 
 | 105 | status_t CameraClient::dump(int fd, const Vector<String16>& args) { | 
 | 106 |     const size_t SIZE = 256; | 
 | 107 |     char buffer[SIZE]; | 
 | 108 |  | 
 | 109 |     size_t len = snprintf(buffer, SIZE, "Client[%d] (%p) PID: %d\n", | 
 | 110 |             mCameraId, | 
 | 111 |             getCameraClient()->asBinder().get(), | 
 | 112 |             mClientPid); | 
 | 113 |     len = (len > SIZE - 1) ? SIZE - 1 : len; | 
 | 114 |     write(fd, buffer, len); | 
 | 115 |     return mHardware->dump(fd, args); | 
 | 116 | } | 
 | 117 |  | 
 | 118 | // ---------------------------------------------------------------------------- | 
 | 119 |  | 
 | 120 | status_t CameraClient::checkPid() const { | 
 | 121 |     int callingPid = getCallingPid(); | 
| Eino-Ville Talvala | c037920 | 2012-10-09 22:16:58 -0700 | [diff] [blame] | 122 |     if (callingPid == mClientPid) return NO_ERROR; | 
| Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 123 |  | 
 | 124 |     ALOGW("attempt to use a locked camera from a different process" | 
 | 125 |          " (old pid %d, new pid %d)", mClientPid, callingPid); | 
 | 126 |     return EBUSY; | 
 | 127 | } | 
 | 128 |  | 
 | 129 | status_t CameraClient::checkPidAndHardware() const { | 
 | 130 |     status_t result = checkPid(); | 
 | 131 |     if (result != NO_ERROR) return result; | 
 | 132 |     if (mHardware == 0) { | 
 | 133 |         ALOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid()); | 
 | 134 |         return INVALID_OPERATION; | 
 | 135 |     } | 
 | 136 |     return NO_ERROR; | 
 | 137 | } | 
 | 138 |  | 
 | 139 | status_t CameraClient::lock() { | 
 | 140 |     int callingPid = getCallingPid(); | 
 | 141 |     LOG1("lock (pid %d)", callingPid); | 
 | 142 |     Mutex::Autolock lock(mLock); | 
 | 143 |  | 
 | 144 |     // lock camera to this client if the the camera is unlocked | 
 | 145 |     if (mClientPid == 0) { | 
 | 146 |         mClientPid = callingPid; | 
 | 147 |         return NO_ERROR; | 
 | 148 |     } | 
 | 149 |  | 
 | 150 |     // returns NO_ERROR if the client already owns the camera, EBUSY otherwise | 
 | 151 |     return checkPid(); | 
 | 152 | } | 
 | 153 |  | 
 | 154 | status_t CameraClient::unlock() { | 
 | 155 |     int callingPid = getCallingPid(); | 
 | 156 |     LOG1("unlock (pid %d)", callingPid); | 
 | 157 |     Mutex::Autolock lock(mLock); | 
 | 158 |  | 
 | 159 |     // allow anyone to use camera (after they lock the camera) | 
 | 160 |     status_t result = checkPid(); | 
 | 161 |     if (result == NO_ERROR) { | 
 | 162 |         if (mHardware->recordingEnabled()) { | 
 | 163 |             ALOGE("Not allowed to unlock camera during recording."); | 
 | 164 |             return INVALID_OPERATION; | 
 | 165 |         } | 
 | 166 |         mClientPid = 0; | 
 | 167 |         LOG1("clear mCameraClient (pid %d)", callingPid); | 
 | 168 |         // we need to remove the reference to ICameraClient so that when the app | 
 | 169 |         // goes away, the reference count goes to 0. | 
 | 170 |         mCameraClient.clear(); | 
 | 171 |     } | 
 | 172 |     return result; | 
 | 173 | } | 
 | 174 |  | 
 | 175 | // connect a new client to the camera | 
 | 176 | status_t CameraClient::connect(const sp<ICameraClient>& client) { | 
 | 177 |     int callingPid = getCallingPid(); | 
 | 178 |     LOG1("connect E (pid %d)", callingPid); | 
 | 179 |     Mutex::Autolock lock(mLock); | 
 | 180 |  | 
 | 181 |     if (mClientPid != 0 && checkPid() != NO_ERROR) { | 
 | 182 |         ALOGW("Tried to connect to a locked camera (old pid %d, new pid %d)", | 
 | 183 |                 mClientPid, callingPid); | 
 | 184 |         return EBUSY; | 
 | 185 |     } | 
 | 186 |  | 
 | 187 |     if (mCameraClient != 0 && (client->asBinder() == mCameraClient->asBinder())) { | 
 | 188 |         LOG1("Connect to the same client"); | 
 | 189 |         return NO_ERROR; | 
 | 190 |     } | 
 | 191 |  | 
 | 192 |     mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP; | 
 | 193 |     mClientPid = callingPid; | 
 | 194 |     mCameraClient = client; | 
 | 195 |  | 
 | 196 |     LOG1("connect X (pid %d)", callingPid); | 
 | 197 |     return NO_ERROR; | 
 | 198 | } | 
 | 199 |  | 
 | 200 | static void disconnectWindow(const sp<ANativeWindow>& window) { | 
 | 201 |     if (window != 0) { | 
 | 202 |         status_t result = native_window_api_disconnect(window.get(), | 
 | 203 |                 NATIVE_WINDOW_API_CAMERA); | 
 | 204 |         if (result != NO_ERROR) { | 
 | 205 |             ALOGW("native_window_api_disconnect failed: %s (%d)", strerror(-result), | 
 | 206 |                     result); | 
 | 207 |         } | 
 | 208 |     } | 
 | 209 | } | 
 | 210 |  | 
 | 211 | void CameraClient::disconnect() { | 
 | 212 |     int callingPid = getCallingPid(); | 
 | 213 |     LOG1("disconnect E (pid %d)", callingPid); | 
 | 214 |     Mutex::Autolock lock(mLock); | 
 | 215 |  | 
| Eino-Ville Talvala | c037920 | 2012-10-09 22:16:58 -0700 | [diff] [blame] | 216 |     // Allow both client and the media server to disconnect at all times | 
 | 217 |     if (callingPid != mClientPid && callingPid != mServicePid) { | 
| Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 218 |         ALOGW("different client - don't disconnect"); | 
 | 219 |         return; | 
 | 220 |     } | 
 | 221 |  | 
 | 222 |     if (mClientPid <= 0) { | 
 | 223 |         LOG1("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid); | 
 | 224 |         return; | 
 | 225 |     } | 
 | 226 |  | 
 | 227 |     // Make sure disconnect() is done once and once only, whether it is called | 
 | 228 |     // from the user directly, or called by the destructor. | 
 | 229 |     if (mHardware == 0) return; | 
 | 230 |  | 
 | 231 |     LOG1("hardware teardown"); | 
 | 232 |     // Before destroying mHardware, we must make sure it's in the | 
 | 233 |     // idle state. | 
 | 234 |     // Turn off all messages. | 
 | 235 |     disableMsgType(CAMERA_MSG_ALL_MSGS); | 
 | 236 |     mHardware->stopPreview(); | 
 | 237 |     mHardware->cancelPicture(); | 
 | 238 |     // Release the hardware resources. | 
 | 239 |     mHardware->release(); | 
 | 240 |  | 
 | 241 |     // Release the held ANativeWindow resources. | 
 | 242 |     if (mPreviewWindow != 0) { | 
 | 243 |         disconnectWindow(mPreviewWindow); | 
 | 244 |         mPreviewWindow = 0; | 
 | 245 |         mHardware->setPreviewWindow(mPreviewWindow); | 
 | 246 |     } | 
 | 247 |     mHardware.clear(); | 
 | 248 |  | 
 | 249 |     CameraService::Client::disconnect(); | 
 | 250 |  | 
 | 251 |     LOG1("disconnect X (pid %d)", callingPid); | 
 | 252 | } | 
 | 253 |  | 
 | 254 | // ---------------------------------------------------------------------------- | 
 | 255 |  | 
 | 256 | status_t CameraClient::setPreviewWindow(const sp<IBinder>& binder, | 
 | 257 |         const sp<ANativeWindow>& window) { | 
 | 258 |     Mutex::Autolock lock(mLock); | 
 | 259 |     status_t result = checkPidAndHardware(); | 
 | 260 |     if (result != NO_ERROR) return result; | 
 | 261 |  | 
 | 262 |     // return if no change in surface. | 
 | 263 |     if (binder == mSurface) { | 
 | 264 |         return NO_ERROR; | 
 | 265 |     } | 
 | 266 |  | 
 | 267 |     if (window != 0) { | 
 | 268 |         result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA); | 
 | 269 |         if (result != NO_ERROR) { | 
 | 270 |             ALOGE("native_window_api_connect failed: %s (%d)", strerror(-result), | 
 | 271 |                     result); | 
 | 272 |             return result; | 
 | 273 |         } | 
 | 274 |     } | 
 | 275 |  | 
 | 276 |     // If preview has been already started, register preview buffers now. | 
 | 277 |     if (mHardware->previewEnabled()) { | 
 | 278 |         if (window != 0) { | 
 | 279 |             native_window_set_scaling_mode(window.get(), | 
 | 280 |                     NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); | 
 | 281 |             native_window_set_buffers_transform(window.get(), mOrientation); | 
 | 282 |             result = mHardware->setPreviewWindow(window); | 
 | 283 |         } | 
 | 284 |     } | 
 | 285 |  | 
 | 286 |     if (result == NO_ERROR) { | 
 | 287 |         // Everything has succeeded.  Disconnect the old window and remember the | 
 | 288 |         // new window. | 
 | 289 |         disconnectWindow(mPreviewWindow); | 
 | 290 |         mSurface = binder; | 
 | 291 |         mPreviewWindow = window; | 
 | 292 |     } else { | 
 | 293 |         // Something went wrong after we connected to the new window, so | 
 | 294 |         // disconnect here. | 
 | 295 |         disconnectWindow(window); | 
 | 296 |     } | 
 | 297 |  | 
 | 298 |     return result; | 
 | 299 | } | 
 | 300 |  | 
 | 301 | // set the Surface that the preview will use | 
 | 302 | status_t CameraClient::setPreviewDisplay(const sp<Surface>& surface) { | 
 | 303 |     LOG1("setPreviewDisplay(%p) (pid %d)", surface.get(), getCallingPid()); | 
 | 304 |  | 
 | 305 |     sp<IBinder> binder(surface != 0 ? surface->asBinder() : 0); | 
 | 306 |     sp<ANativeWindow> window(surface); | 
 | 307 |     return setPreviewWindow(binder, window); | 
 | 308 | } | 
 | 309 |  | 
 | 310 | // set the SurfaceTexture that the preview will use | 
 | 311 | status_t CameraClient::setPreviewTexture( | 
 | 312 |         const sp<ISurfaceTexture>& surfaceTexture) { | 
 | 313 |     LOG1("setPreviewTexture(%p) (pid %d)", surfaceTexture.get(), | 
 | 314 |             getCallingPid()); | 
 | 315 |  | 
 | 316 |     sp<IBinder> binder; | 
 | 317 |     sp<ANativeWindow> window; | 
 | 318 |     if (surfaceTexture != 0) { | 
 | 319 |         binder = surfaceTexture->asBinder(); | 
 | 320 |         window = new SurfaceTextureClient(surfaceTexture); | 
 | 321 |     } | 
 | 322 |     return setPreviewWindow(binder, window); | 
 | 323 | } | 
 | 324 |  | 
 | 325 | // set the preview callback flag to affect how the received frames from | 
 | 326 | // preview are handled. | 
 | 327 | void CameraClient::setPreviewCallbackFlag(int callback_flag) { | 
 | 328 |     LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid()); | 
 | 329 |     Mutex::Autolock lock(mLock); | 
 | 330 |     if (checkPidAndHardware() != NO_ERROR) return; | 
 | 331 |  | 
 | 332 |     mPreviewCallbackFlag = callback_flag; | 
 | 333 |     if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) { | 
 | 334 |         enableMsgType(CAMERA_MSG_PREVIEW_FRAME); | 
 | 335 |     } else { | 
 | 336 |         disableMsgType(CAMERA_MSG_PREVIEW_FRAME); | 
 | 337 |     } | 
 | 338 | } | 
 | 339 |  | 
 | 340 | // start preview mode | 
 | 341 | status_t CameraClient::startPreview() { | 
 | 342 |     LOG1("startPreview (pid %d)", getCallingPid()); | 
 | 343 |     return startCameraMode(CAMERA_PREVIEW_MODE); | 
 | 344 | } | 
 | 345 |  | 
 | 346 | // start recording mode | 
 | 347 | status_t CameraClient::startRecording() { | 
 | 348 |     LOG1("startRecording (pid %d)", getCallingPid()); | 
 | 349 |     return startCameraMode(CAMERA_RECORDING_MODE); | 
 | 350 | } | 
 | 351 |  | 
 | 352 | // start preview or recording | 
 | 353 | status_t CameraClient::startCameraMode(camera_mode mode) { | 
 | 354 |     LOG1("startCameraMode(%d)", mode); | 
 | 355 |     Mutex::Autolock lock(mLock); | 
 | 356 |     status_t result = checkPidAndHardware(); | 
 | 357 |     if (result != NO_ERROR) return result; | 
 | 358 |  | 
 | 359 |     switch(mode) { | 
 | 360 |         case CAMERA_PREVIEW_MODE: | 
 | 361 |             if (mSurface == 0 && mPreviewWindow == 0) { | 
 | 362 |                 LOG1("mSurface is not set yet."); | 
 | 363 |                 // still able to start preview in this case. | 
 | 364 |             } | 
 | 365 |             return startPreviewMode(); | 
 | 366 |         case CAMERA_RECORDING_MODE: | 
 | 367 |             if (mSurface == 0 && mPreviewWindow == 0) { | 
 | 368 |                 ALOGE("mSurface or mPreviewWindow must be set before startRecordingMode."); | 
 | 369 |                 return INVALID_OPERATION; | 
 | 370 |             } | 
 | 371 |             return startRecordingMode(); | 
 | 372 |         default: | 
 | 373 |             return UNKNOWN_ERROR; | 
 | 374 |     } | 
 | 375 | } | 
 | 376 |  | 
 | 377 | status_t CameraClient::startPreviewMode() { | 
 | 378 |     LOG1("startPreviewMode"); | 
 | 379 |     status_t result = NO_ERROR; | 
 | 380 |  | 
 | 381 |     // if preview has been enabled, nothing needs to be done | 
 | 382 |     if (mHardware->previewEnabled()) { | 
 | 383 |         return NO_ERROR; | 
 | 384 |     } | 
 | 385 |  | 
 | 386 |     if (mPreviewWindow != 0) { | 
 | 387 |         native_window_set_scaling_mode(mPreviewWindow.get(), | 
 | 388 |                 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW); | 
 | 389 |         native_window_set_buffers_transform(mPreviewWindow.get(), | 
 | 390 |                 mOrientation); | 
 | 391 |     } | 
 | 392 |     mHardware->setPreviewWindow(mPreviewWindow); | 
 | 393 |     result = mHardware->startPreview(); | 
 | 394 |  | 
 | 395 |     return result; | 
 | 396 | } | 
 | 397 |  | 
 | 398 | status_t CameraClient::startRecordingMode() { | 
 | 399 |     LOG1("startRecordingMode"); | 
 | 400 |     status_t result = NO_ERROR; | 
 | 401 |  | 
 | 402 |     // if recording has been enabled, nothing needs to be done | 
 | 403 |     if (mHardware->recordingEnabled()) { | 
 | 404 |         return NO_ERROR; | 
 | 405 |     } | 
 | 406 |  | 
 | 407 |     // if preview has not been started, start preview first | 
 | 408 |     if (!mHardware->previewEnabled()) { | 
 | 409 |         result = startPreviewMode(); | 
 | 410 |         if (result != NO_ERROR) { | 
 | 411 |             return result; | 
 | 412 |         } | 
 | 413 |     } | 
 | 414 |  | 
 | 415 |     // start recording mode | 
 | 416 |     enableMsgType(CAMERA_MSG_VIDEO_FRAME); | 
 | 417 |     mCameraService->playSound(CameraService::SOUND_RECORDING); | 
 | 418 |     result = mHardware->startRecording(); | 
 | 419 |     if (result != NO_ERROR) { | 
 | 420 |         ALOGE("mHardware->startRecording() failed with status %d", result); | 
 | 421 |     } | 
 | 422 |     return result; | 
 | 423 | } | 
 | 424 |  | 
 | 425 | // stop preview mode | 
 | 426 | void CameraClient::stopPreview() { | 
 | 427 |     LOG1("stopPreview (pid %d)", getCallingPid()); | 
 | 428 |     Mutex::Autolock lock(mLock); | 
 | 429 |     if (checkPidAndHardware() != NO_ERROR) return; | 
 | 430 |  | 
 | 431 |  | 
 | 432 |     disableMsgType(CAMERA_MSG_PREVIEW_FRAME); | 
 | 433 |     mHardware->stopPreview(); | 
 | 434 |  | 
 | 435 |     mPreviewBuffer.clear(); | 
 | 436 | } | 
 | 437 |  | 
 | 438 | // stop recording mode | 
 | 439 | void CameraClient::stopRecording() { | 
 | 440 |     LOG1("stopRecording (pid %d)", getCallingPid()); | 
 | 441 |     Mutex::Autolock lock(mLock); | 
 | 442 |     if (checkPidAndHardware() != NO_ERROR) return; | 
 | 443 |  | 
| Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 444 |     disableMsgType(CAMERA_MSG_VIDEO_FRAME); | 
 | 445 |     mHardware->stopRecording(); | 
| Patric Frederiksen | 35f859b | 2012-07-10 13:38:35 +0200 | [diff] [blame] | 446 |     mCameraService->playSound(CameraService::SOUND_RECORDING); | 
| Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 447 |  | 
 | 448 |     mPreviewBuffer.clear(); | 
 | 449 | } | 
 | 450 |  | 
 | 451 | // release a recording frame | 
 | 452 | void CameraClient::releaseRecordingFrame(const sp<IMemory>& mem) { | 
 | 453 |     Mutex::Autolock lock(mLock); | 
 | 454 |     if (checkPidAndHardware() != NO_ERROR) return; | 
 | 455 |     mHardware->releaseRecordingFrame(mem); | 
 | 456 | } | 
 | 457 |  | 
 | 458 | status_t CameraClient::storeMetaDataInBuffers(bool enabled) | 
 | 459 | { | 
 | 460 |     LOG1("storeMetaDataInBuffers: %s", enabled? "true": "false"); | 
 | 461 |     Mutex::Autolock lock(mLock); | 
 | 462 |     if (checkPidAndHardware() != NO_ERROR) { | 
 | 463 |         return UNKNOWN_ERROR; | 
 | 464 |     } | 
 | 465 |     return mHardware->storeMetaDataInBuffers(enabled); | 
 | 466 | } | 
 | 467 |  | 
 | 468 | bool CameraClient::previewEnabled() { | 
 | 469 |     LOG1("previewEnabled (pid %d)", getCallingPid()); | 
 | 470 |  | 
 | 471 |     Mutex::Autolock lock(mLock); | 
 | 472 |     if (checkPidAndHardware() != NO_ERROR) return false; | 
 | 473 |     return mHardware->previewEnabled(); | 
 | 474 | } | 
 | 475 |  | 
 | 476 | bool CameraClient::recordingEnabled() { | 
 | 477 |     LOG1("recordingEnabled (pid %d)", getCallingPid()); | 
 | 478 |  | 
 | 479 |     Mutex::Autolock lock(mLock); | 
 | 480 |     if (checkPidAndHardware() != NO_ERROR) return false; | 
 | 481 |     return mHardware->recordingEnabled(); | 
 | 482 | } | 
 | 483 |  | 
 | 484 | status_t CameraClient::autoFocus() { | 
 | 485 |     LOG1("autoFocus (pid %d)", getCallingPid()); | 
 | 486 |  | 
 | 487 |     Mutex::Autolock lock(mLock); | 
 | 488 |     status_t result = checkPidAndHardware(); | 
 | 489 |     if (result != NO_ERROR) return result; | 
 | 490 |  | 
 | 491 |     return mHardware->autoFocus(); | 
 | 492 | } | 
 | 493 |  | 
 | 494 | status_t CameraClient::cancelAutoFocus() { | 
 | 495 |     LOG1("cancelAutoFocus (pid %d)", getCallingPid()); | 
 | 496 |  | 
 | 497 |     Mutex::Autolock lock(mLock); | 
 | 498 |     status_t result = checkPidAndHardware(); | 
 | 499 |     if (result != NO_ERROR) return result; | 
 | 500 |  | 
 | 501 |     return mHardware->cancelAutoFocus(); | 
 | 502 | } | 
 | 503 |  | 
 | 504 | // take a picture - image is returned in callback | 
 | 505 | status_t CameraClient::takePicture(int msgType) { | 
 | 506 |     LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType); | 
 | 507 |  | 
 | 508 |     Mutex::Autolock lock(mLock); | 
 | 509 |     status_t result = checkPidAndHardware(); | 
 | 510 |     if (result != NO_ERROR) return result; | 
 | 511 |  | 
 | 512 |     if ((msgType & CAMERA_MSG_RAW_IMAGE) && | 
 | 513 |         (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) { | 
 | 514 |         ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY" | 
 | 515 |                 " cannot be both enabled"); | 
 | 516 |         return BAD_VALUE; | 
 | 517 |     } | 
 | 518 |  | 
 | 519 |     // We only accept picture related message types | 
 | 520 |     // and ignore other types of messages for takePicture(). | 
 | 521 |     int picMsgType = msgType | 
 | 522 |                         & (CAMERA_MSG_SHUTTER | | 
 | 523 |                            CAMERA_MSG_POSTVIEW_FRAME | | 
 | 524 |                            CAMERA_MSG_RAW_IMAGE | | 
 | 525 |                            CAMERA_MSG_RAW_IMAGE_NOTIFY | | 
 | 526 |                            CAMERA_MSG_COMPRESSED_IMAGE); | 
 | 527 |  | 
 | 528 |     enableMsgType(picMsgType); | 
 | 529 |  | 
 | 530 |     return mHardware->takePicture(); | 
 | 531 | } | 
 | 532 |  | 
 | 533 | // set preview/capture parameters - key/value pairs | 
 | 534 | status_t CameraClient::setParameters(const String8& params) { | 
 | 535 |     LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string()); | 
 | 536 |  | 
 | 537 |     Mutex::Autolock lock(mLock); | 
 | 538 |     status_t result = checkPidAndHardware(); | 
 | 539 |     if (result != NO_ERROR) return result; | 
 | 540 |  | 
 | 541 |     CameraParameters p(params); | 
 | 542 |     return mHardware->setParameters(p); | 
 | 543 | } | 
 | 544 |  | 
 | 545 | // get preview/capture parameters - key/value pairs | 
 | 546 | String8 CameraClient::getParameters() const { | 
 | 547 |     Mutex::Autolock lock(mLock); | 
 | 548 |     if (checkPidAndHardware() != NO_ERROR) return String8(); | 
 | 549 |  | 
 | 550 |     String8 params(mHardware->getParameters().flatten()); | 
 | 551 |     LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string()); | 
 | 552 |     return params; | 
 | 553 | } | 
 | 554 |  | 
 | 555 | // enable shutter sound | 
 | 556 | status_t CameraClient::enableShutterSound(bool enable) { | 
 | 557 |     LOG1("enableShutterSound (pid %d)", getCallingPid()); | 
 | 558 |  | 
 | 559 |     status_t result = checkPidAndHardware(); | 
 | 560 |     if (result != NO_ERROR) return result; | 
 | 561 |  | 
 | 562 |     if (enable) { | 
 | 563 |         mPlayShutterSound = true; | 
 | 564 |         return OK; | 
 | 565 |     } | 
 | 566 |  | 
 | 567 |     // Disabling shutter sound may not be allowed. In that case only | 
 | 568 |     // allow the mediaserver process to disable the sound. | 
 | 569 |     char value[PROPERTY_VALUE_MAX]; | 
 | 570 |     property_get("ro.camera.sound.forced", value, "0"); | 
 | 571 |     if (strcmp(value, "0") != 0) { | 
 | 572 |         // Disabling shutter sound is not allowed. Deny if the current | 
 | 573 |         // process is not mediaserver. | 
 | 574 |         if (getCallingPid() != getpid()) { | 
 | 575 |             ALOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid()); | 
 | 576 |             return PERMISSION_DENIED; | 
 | 577 |         } | 
 | 578 |     } | 
 | 579 |  | 
 | 580 |     mPlayShutterSound = false; | 
 | 581 |     return OK; | 
 | 582 | } | 
 | 583 |  | 
 | 584 | status_t CameraClient::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) { | 
 | 585 |     LOG1("sendCommand (pid %d)", getCallingPid()); | 
 | 586 |     int orientation; | 
 | 587 |     Mutex::Autolock lock(mLock); | 
 | 588 |     status_t result = checkPidAndHardware(); | 
 | 589 |     if (result != NO_ERROR) return result; | 
 | 590 |  | 
 | 591 |     if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) { | 
 | 592 |         // Mirror the preview if the camera is front-facing. | 
 | 593 |         orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT); | 
 | 594 |         if (orientation == -1) return BAD_VALUE; | 
 | 595 |  | 
 | 596 |         if (mOrientation != orientation) { | 
 | 597 |             mOrientation = orientation; | 
 | 598 |             if (mPreviewWindow != 0) { | 
 | 599 |                 native_window_set_buffers_transform(mPreviewWindow.get(), | 
 | 600 |                         mOrientation); | 
 | 601 |             } | 
 | 602 |         } | 
 | 603 |         return OK; | 
 | 604 |     } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) { | 
 | 605 |         switch (arg1) { | 
 | 606 |             case 0: | 
| Eino-Ville Talvala | c5268e8 | 2012-09-11 11:01:18 -0700 | [diff] [blame] | 607 |                 return enableShutterSound(false); | 
| Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 608 |             case 1: | 
| Eino-Ville Talvala | c5268e8 | 2012-09-11 11:01:18 -0700 | [diff] [blame] | 609 |                 return enableShutterSound(true); | 
| Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 610 |             default: | 
 | 611 |                 return BAD_VALUE; | 
 | 612 |         } | 
 | 613 |         return OK; | 
 | 614 |     } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) { | 
 | 615 |         mCameraService->playSound(CameraService::SOUND_RECORDING); | 
| James Dong | 983cf23 | 2012-08-01 16:39:55 -0700 | [diff] [blame] | 616 |     } else if (cmd == CAMERA_CMD_SET_VIDEO_BUFFER_COUNT) { | 
 | 617 |         // Silently ignore this command | 
 | 618 |         return INVALID_OPERATION; | 
| Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 619 |     } else if (cmd == CAMERA_CMD_PING) { | 
 | 620 |         // If mHardware is 0, checkPidAndHardware will return error. | 
 | 621 |         return OK; | 
 | 622 |     } | 
 | 623 |  | 
 | 624 |     return mHardware->sendCommand(cmd, arg1, arg2); | 
 | 625 | } | 
 | 626 |  | 
 | 627 | // ---------------------------------------------------------------------------- | 
 | 628 |  | 
 | 629 | void CameraClient::enableMsgType(int32_t msgType) { | 
 | 630 |     android_atomic_or(msgType, &mMsgEnabled); | 
 | 631 |     mHardware->enableMsgType(msgType); | 
 | 632 | } | 
 | 633 |  | 
 | 634 | void CameraClient::disableMsgType(int32_t msgType) { | 
 | 635 |     android_atomic_and(~msgType, &mMsgEnabled); | 
 | 636 |     mHardware->disableMsgType(msgType); | 
 | 637 | } | 
 | 638 |  | 
 | 639 | #define CHECK_MESSAGE_INTERVAL 10 // 10ms | 
 | 640 | bool CameraClient::lockIfMessageWanted(int32_t msgType) { | 
 | 641 |     int sleepCount = 0; | 
 | 642 |     while (mMsgEnabled & msgType) { | 
 | 643 |         if (mLock.tryLock() == NO_ERROR) { | 
 | 644 |             if (sleepCount > 0) { | 
 | 645 |                 LOG1("lockIfMessageWanted(%d): waited for %d ms", | 
 | 646 |                     msgType, sleepCount * CHECK_MESSAGE_INTERVAL); | 
 | 647 |             } | 
 | 648 |             return true; | 
 | 649 |         } | 
 | 650 |         if (sleepCount++ == 0) { | 
 | 651 |             LOG1("lockIfMessageWanted(%d): enter sleep", msgType); | 
 | 652 |         } | 
 | 653 |         usleep(CHECK_MESSAGE_INTERVAL * 1000); | 
 | 654 |     } | 
 | 655 |     ALOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType); | 
 | 656 |     return false; | 
 | 657 | } | 
 | 658 |  | 
 | 659 | // Callback messages can be dispatched to internal handlers or pass to our | 
 | 660 | // client's callback functions, depending on the message type. | 
 | 661 | // | 
 | 662 | // notifyCallback: | 
 | 663 | //      CAMERA_MSG_SHUTTER              handleShutter | 
 | 664 | //      (others)                        c->notifyCallback | 
 | 665 | // dataCallback: | 
 | 666 | //      CAMERA_MSG_PREVIEW_FRAME        handlePreviewData | 
 | 667 | //      CAMERA_MSG_POSTVIEW_FRAME       handlePostview | 
 | 668 | //      CAMERA_MSG_RAW_IMAGE            handleRawPicture | 
 | 669 | //      CAMERA_MSG_COMPRESSED_IMAGE     handleCompressedPicture | 
 | 670 | //      (others)                        c->dataCallback | 
 | 671 | // dataCallbackTimestamp | 
 | 672 | //      (others)                        c->dataCallbackTimestamp | 
 | 673 | // | 
 | 674 | // NOTE: the *Callback functions grab mLock of the client before passing | 
 | 675 | // control to handle* functions. So the handle* functions must release the | 
 | 676 | // lock before calling the ICameraClient's callbacks, so those callbacks can | 
 | 677 | // invoke methods in the Client class again (For example, the preview frame | 
 | 678 | // callback may want to releaseRecordingFrame). The handle* functions must | 
 | 679 | // release the lock after all accesses to member variables, so it must be | 
 | 680 | // handled very carefully. | 
 | 681 |  | 
 | 682 | void CameraClient::notifyCallback(int32_t msgType, int32_t ext1, | 
 | 683 |         int32_t ext2, void* user) { | 
 | 684 |     LOG2("notifyCallback(%d)", msgType); | 
 | 685 |  | 
 | 686 |     Mutex* lock = getClientLockFromCookie(user); | 
 | 687 |     if (lock == NULL) return; | 
 | 688 |     Mutex::Autolock alock(*lock); | 
 | 689 |  | 
 | 690 |     CameraClient* client = | 
 | 691 |             static_cast<CameraClient*>(getClientFromCookie(user)); | 
 | 692 |     if (client == NULL) return; | 
 | 693 |  | 
 | 694 |     if (!client->lockIfMessageWanted(msgType)) return; | 
 | 695 |  | 
 | 696 |     switch (msgType) { | 
 | 697 |         case CAMERA_MSG_SHUTTER: | 
 | 698 |             // ext1 is the dimension of the yuv picture. | 
 | 699 |             client->handleShutter(); | 
 | 700 |             break; | 
 | 701 |         default: | 
 | 702 |             client->handleGenericNotify(msgType, ext1, ext2); | 
 | 703 |             break; | 
 | 704 |     } | 
 | 705 | } | 
 | 706 |  | 
 | 707 | void CameraClient::dataCallback(int32_t msgType, | 
 | 708 |         const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) { | 
 | 709 |     LOG2("dataCallback(%d)", msgType); | 
 | 710 |  | 
 | 711 |     Mutex* lock = getClientLockFromCookie(user); | 
 | 712 |     if (lock == NULL) return; | 
 | 713 |     Mutex::Autolock alock(*lock); | 
 | 714 |  | 
 | 715 |     CameraClient* client = | 
 | 716 |             static_cast<CameraClient*>(getClientFromCookie(user)); | 
 | 717 |     if (client == NULL) return; | 
 | 718 |  | 
 | 719 |     if (!client->lockIfMessageWanted(msgType)) return; | 
 | 720 |     if (dataPtr == 0 && metadata == NULL) { | 
 | 721 |         ALOGE("Null data returned in data callback"); | 
 | 722 |         client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0); | 
 | 723 |         return; | 
 | 724 |     } | 
 | 725 |  | 
 | 726 |     switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) { | 
 | 727 |         case CAMERA_MSG_PREVIEW_FRAME: | 
 | 728 |             client->handlePreviewData(msgType, dataPtr, metadata); | 
 | 729 |             break; | 
 | 730 |         case CAMERA_MSG_POSTVIEW_FRAME: | 
 | 731 |             client->handlePostview(dataPtr); | 
 | 732 |             break; | 
 | 733 |         case CAMERA_MSG_RAW_IMAGE: | 
 | 734 |             client->handleRawPicture(dataPtr); | 
 | 735 |             break; | 
 | 736 |         case CAMERA_MSG_COMPRESSED_IMAGE: | 
 | 737 |             client->handleCompressedPicture(dataPtr); | 
 | 738 |             break; | 
 | 739 |         default: | 
 | 740 |             client->handleGenericData(msgType, dataPtr, metadata); | 
 | 741 |             break; | 
 | 742 |     } | 
 | 743 | } | 
 | 744 |  | 
 | 745 | void CameraClient::dataCallbackTimestamp(nsecs_t timestamp, | 
 | 746 |         int32_t msgType, const sp<IMemory>& dataPtr, void* user) { | 
 | 747 |     LOG2("dataCallbackTimestamp(%d)", msgType); | 
 | 748 |  | 
 | 749 |     Mutex* lock = getClientLockFromCookie(user); | 
 | 750 |     if (lock == NULL) return; | 
 | 751 |     Mutex::Autolock alock(*lock); | 
 | 752 |  | 
 | 753 |     CameraClient* client = | 
 | 754 |             static_cast<CameraClient*>(getClientFromCookie(user)); | 
 | 755 |     if (client == NULL) return; | 
 | 756 |  | 
 | 757 |     if (!client->lockIfMessageWanted(msgType)) return; | 
 | 758 |  | 
 | 759 |     if (dataPtr == 0) { | 
 | 760 |         ALOGE("Null data returned in data with timestamp callback"); | 
 | 761 |         client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0); | 
 | 762 |         return; | 
 | 763 |     } | 
 | 764 |  | 
 | 765 |     client->handleGenericDataTimestamp(timestamp, msgType, dataPtr); | 
 | 766 | } | 
 | 767 |  | 
 | 768 | // snapshot taken callback | 
 | 769 | void CameraClient::handleShutter(void) { | 
 | 770 |     if (mPlayShutterSound) { | 
 | 771 |         mCameraService->playSound(CameraService::SOUND_SHUTTER); | 
 | 772 |     } | 
 | 773 |  | 
 | 774 |     sp<ICameraClient> c = mCameraClient; | 
 | 775 |     if (c != 0) { | 
 | 776 |         mLock.unlock(); | 
 | 777 |         c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0); | 
 | 778 |         if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return; | 
 | 779 |     } | 
 | 780 |     disableMsgType(CAMERA_MSG_SHUTTER); | 
 | 781 |  | 
 | 782 |     mLock.unlock(); | 
 | 783 | } | 
 | 784 |  | 
 | 785 | // preview callback - frame buffer update | 
 | 786 | void CameraClient::handlePreviewData(int32_t msgType, | 
 | 787 |                                               const sp<IMemory>& mem, | 
 | 788 |                                               camera_frame_metadata_t *metadata) { | 
 | 789 |     ssize_t offset; | 
 | 790 |     size_t size; | 
 | 791 |     sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); | 
 | 792 |  | 
 | 793 |     // local copy of the callback flags | 
 | 794 |     int flags = mPreviewCallbackFlag; | 
 | 795 |  | 
 | 796 |     // is callback enabled? | 
 | 797 |     if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) { | 
 | 798 |         // If the enable bit is off, the copy-out and one-shot bits are ignored | 
 | 799 |         LOG2("frame callback is disabled"); | 
 | 800 |         mLock.unlock(); | 
 | 801 |         return; | 
 | 802 |     } | 
 | 803 |  | 
 | 804 |     // hold a strong pointer to the client | 
 | 805 |     sp<ICameraClient> c = mCameraClient; | 
 | 806 |  | 
 | 807 |     // clear callback flags if no client or one-shot mode | 
 | 808 |     if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) { | 
 | 809 |         LOG2("Disable preview callback"); | 
 | 810 |         mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK | | 
 | 811 |                                   CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK | | 
 | 812 |                                   CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK); | 
 | 813 |         disableMsgType(CAMERA_MSG_PREVIEW_FRAME); | 
 | 814 |     } | 
 | 815 |  | 
 | 816 |     if (c != 0) { | 
 | 817 |         // Is the received frame copied out or not? | 
 | 818 |         if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) { | 
 | 819 |             LOG2("frame is copied"); | 
 | 820 |             copyFrameAndPostCopiedFrame(msgType, c, heap, offset, size, metadata); | 
 | 821 |         } else { | 
 | 822 |             LOG2("frame is forwarded"); | 
 | 823 |             mLock.unlock(); | 
 | 824 |             c->dataCallback(msgType, mem, metadata); | 
 | 825 |         } | 
 | 826 |     } else { | 
 | 827 |         mLock.unlock(); | 
 | 828 |     } | 
 | 829 | } | 
 | 830 |  | 
 | 831 | // picture callback - postview image ready | 
 | 832 | void CameraClient::handlePostview(const sp<IMemory>& mem) { | 
 | 833 |     disableMsgType(CAMERA_MSG_POSTVIEW_FRAME); | 
 | 834 |  | 
 | 835 |     sp<ICameraClient> c = mCameraClient; | 
 | 836 |     mLock.unlock(); | 
 | 837 |     if (c != 0) { | 
 | 838 |         c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL); | 
 | 839 |     } | 
 | 840 | } | 
 | 841 |  | 
 | 842 | // picture callback - raw image ready | 
 | 843 | void CameraClient::handleRawPicture(const sp<IMemory>& mem) { | 
 | 844 |     disableMsgType(CAMERA_MSG_RAW_IMAGE); | 
 | 845 |  | 
 | 846 |     ssize_t offset; | 
 | 847 |     size_t size; | 
 | 848 |     sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); | 
 | 849 |  | 
 | 850 |     sp<ICameraClient> c = mCameraClient; | 
 | 851 |     mLock.unlock(); | 
 | 852 |     if (c != 0) { | 
 | 853 |         c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem, NULL); | 
 | 854 |     } | 
 | 855 | } | 
 | 856 |  | 
 | 857 | // picture callback - compressed picture ready | 
 | 858 | void CameraClient::handleCompressedPicture(const sp<IMemory>& mem) { | 
 | 859 |     disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE); | 
 | 860 |  | 
 | 861 |     sp<ICameraClient> c = mCameraClient; | 
 | 862 |     mLock.unlock(); | 
 | 863 |     if (c != 0) { | 
 | 864 |         c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL); | 
 | 865 |     } | 
 | 866 | } | 
 | 867 |  | 
 | 868 |  | 
 | 869 | void CameraClient::handleGenericNotify(int32_t msgType, | 
 | 870 |     int32_t ext1, int32_t ext2) { | 
 | 871 |     sp<ICameraClient> c = mCameraClient; | 
 | 872 |     mLock.unlock(); | 
 | 873 |     if (c != 0) { | 
 | 874 |         c->notifyCallback(msgType, ext1, ext2); | 
 | 875 |     } | 
 | 876 | } | 
 | 877 |  | 
 | 878 | void CameraClient::handleGenericData(int32_t msgType, | 
 | 879 |     const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) { | 
 | 880 |     sp<ICameraClient> c = mCameraClient; | 
 | 881 |     mLock.unlock(); | 
 | 882 |     if (c != 0) { | 
 | 883 |         c->dataCallback(msgType, dataPtr, metadata); | 
 | 884 |     } | 
 | 885 | } | 
 | 886 |  | 
 | 887 | void CameraClient::handleGenericDataTimestamp(nsecs_t timestamp, | 
 | 888 |     int32_t msgType, const sp<IMemory>& dataPtr) { | 
 | 889 |     sp<ICameraClient> c = mCameraClient; | 
 | 890 |     mLock.unlock(); | 
 | 891 |     if (c != 0) { | 
 | 892 |         c->dataCallbackTimestamp(timestamp, msgType, dataPtr); | 
 | 893 |     } | 
 | 894 | } | 
 | 895 |  | 
 | 896 | void CameraClient::copyFrameAndPostCopiedFrame( | 
 | 897 |         int32_t msgType, const sp<ICameraClient>& client, | 
 | 898 |         const sp<IMemoryHeap>& heap, size_t offset, size_t size, | 
 | 899 |         camera_frame_metadata_t *metadata) { | 
 | 900 |     LOG2("copyFrameAndPostCopiedFrame"); | 
 | 901 |     // It is necessary to copy out of pmem before sending this to | 
 | 902 |     // the callback. For efficiency, reuse the same MemoryHeapBase | 
 | 903 |     // provided it's big enough. Don't allocate the memory or | 
 | 904 |     // perform the copy if there's no callback. | 
 | 905 |     // hold the preview lock while we grab a reference to the preview buffer | 
 | 906 |     sp<MemoryHeapBase> previewBuffer; | 
 | 907 |  | 
 | 908 |     if (mPreviewBuffer == 0) { | 
 | 909 |         mPreviewBuffer = new MemoryHeapBase(size, 0, NULL); | 
 | 910 |     } else if (size > mPreviewBuffer->virtualSize()) { | 
 | 911 |         mPreviewBuffer.clear(); | 
 | 912 |         mPreviewBuffer = new MemoryHeapBase(size, 0, NULL); | 
 | 913 |     } | 
 | 914 |     if (mPreviewBuffer == 0) { | 
 | 915 |         ALOGE("failed to allocate space for preview buffer"); | 
 | 916 |         mLock.unlock(); | 
 | 917 |         return; | 
 | 918 |     } | 
 | 919 |     previewBuffer = mPreviewBuffer; | 
 | 920 |  | 
 | 921 |     memcpy(previewBuffer->base(), (uint8_t *)heap->base() + offset, size); | 
 | 922 |  | 
 | 923 |     sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size); | 
 | 924 |     if (frame == 0) { | 
 | 925 |         ALOGE("failed to allocate space for frame callback"); | 
 | 926 |         mLock.unlock(); | 
 | 927 |         return; | 
 | 928 |     } | 
 | 929 |  | 
 | 930 |     mLock.unlock(); | 
 | 931 |     client->dataCallback(msgType, frame, metadata); | 
 | 932 | } | 
 | 933 |  | 
 | 934 | int CameraClient::getOrientation(int degrees, bool mirror) { | 
 | 935 |     if (!mirror) { | 
 | 936 |         if (degrees == 0) return 0; | 
 | 937 |         else if (degrees == 90) return HAL_TRANSFORM_ROT_90; | 
 | 938 |         else if (degrees == 180) return HAL_TRANSFORM_ROT_180; | 
 | 939 |         else if (degrees == 270) return HAL_TRANSFORM_ROT_270; | 
 | 940 |     } else {  // Do mirror (horizontal flip) | 
 | 941 |         if (degrees == 0) {           // FLIP_H and ROT_0 | 
 | 942 |             return HAL_TRANSFORM_FLIP_H; | 
 | 943 |         } else if (degrees == 90) {   // FLIP_H and ROT_90 | 
 | 944 |             return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90; | 
 | 945 |         } else if (degrees == 180) {  // FLIP_H and ROT_180 | 
 | 946 |             return HAL_TRANSFORM_FLIP_V; | 
 | 947 |         } else if (degrees == 270) {  // FLIP_H and ROT_270 | 
 | 948 |             return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90; | 
 | 949 |         } | 
 | 950 |     } | 
 | 951 |     ALOGE("Invalid setDisplayOrientation degrees=%d", degrees); | 
 | 952 |     return -1; | 
 | 953 | } | 
 | 954 |  | 
 | 955 | }; // namespace android |