Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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_NDEBUG 0 |
| 18 | #define LOG_TAG "CameraSourceTimeLapse" |
| 19 | |
| 20 | #include <binder/IPCThreadState.h> |
| 21 | #include <binder/MemoryBase.h> |
| 22 | #include <binder/MemoryHeapBase.h> |
| 23 | #include <media/stagefright/CameraSource.h> |
| 24 | #include <media/stagefright/CameraSourceTimeLapse.h> |
| 25 | #include <media/stagefright/MediaDebug.h> |
| 26 | #include <media/stagefright/MetaData.h> |
Nipun Kwatra | dce4beb | 2010-07-27 22:21:44 -0700 | [diff] [blame] | 27 | #include <media/stagefright/YUVImage.h> |
| 28 | #include <media/stagefright/YUVCanvas.h> |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 29 | #include <camera/Camera.h> |
| 30 | #include <camera/CameraParameters.h> |
Nipun Kwatra | dce4beb | 2010-07-27 22:21:44 -0700 | [diff] [blame] | 31 | #include <ui/Rect.h> |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 32 | #include <utils/String8.h> |
Nipun Kwatra | b1fb607 | 2010-07-30 18:30:55 -0700 | [diff] [blame] | 33 | #include <utils/Vector.h> |
Nipun Kwatra | dce4beb | 2010-07-27 22:21:44 -0700 | [diff] [blame] | 34 | #include "OMX_Video.h" |
Nipun Kwatra | 7d435c5 | 2010-08-02 11:30:06 -0700 | [diff] [blame] | 35 | #include <limits.h> |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 36 | |
| 37 | namespace android { |
| 38 | |
| 39 | // static |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 40 | CameraSourceTimeLapse *CameraSourceTimeLapse::CreateFromCamera( |
| 41 | const sp<ICamera> &camera, |
| 42 | int32_t cameraId, |
| 43 | Size videoSize, |
| 44 | int32_t videoFrameRate, |
| 45 | const sp<Surface>& surface, |
| 46 | int64_t timeBetweenTimeLapseFrameCaptureUs) { |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 47 | |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 48 | CameraSourceTimeLapse *source = new |
| 49 | CameraSourceTimeLapse(camera, cameraId, |
| 50 | videoSize, videoFrameRate, surface, |
| 51 | timeBetweenTimeLapseFrameCaptureUs); |
| 52 | |
| 53 | if (source != NULL) { |
| 54 | if (source->initCheck() != OK) { |
| 55 | delete source; |
| 56 | return NULL; |
| 57 | } |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 58 | } |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 59 | return source; |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 60 | } |
| 61 | |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 62 | CameraSourceTimeLapse::CameraSourceTimeLapse( |
| 63 | const sp<ICamera>& camera, |
| 64 | int32_t cameraId, |
| 65 | Size videoSize, |
| 66 | int32_t videoFrameRate, |
| 67 | const sp<Surface>& surface, |
| 68 | int64_t timeBetweenTimeLapseFrameCaptureUs) |
James Dong | ab79d1f | 2010-10-18 21:42:27 -0700 | [diff] [blame] | 69 | : CameraSource(camera, cameraId, videoSize, videoFrameRate, surface, false), |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 70 | mTimeBetweenTimeLapseFrameCaptureUs(timeBetweenTimeLapseFrameCaptureUs), |
| 71 | mTimeBetweenTimeLapseVideoFramesUs(1E6/videoFrameRate), |
| 72 | mLastTimeLapseFrameRealTimestampUs(0), |
| 73 | mSkipCurrentFrame(false) { |
| 74 | |
James Dong | 08800f3 | 2010-12-09 15:04:33 -0800 | [diff] [blame] | 75 | LOGD("starting time lapse mode: %lld us", mTimeBetweenTimeLapseFrameCaptureUs); |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 76 | mVideoWidth = videoSize.width; |
| 77 | mVideoHeight = videoSize.height; |
Nipun Kwatra | 4a857e6 | 2010-09-02 11:43:15 -0700 | [diff] [blame] | 78 | |
James Dong | a1d2d8f | 2011-01-04 16:09:07 -0800 | [diff] [blame^] | 79 | if (trySettingVideoSize(videoSize.width, videoSize.height)) { |
Nipun Kwatra | 4a857e6 | 2010-09-02 11:43:15 -0700 | [diff] [blame] | 80 | mUseStillCameraForTimeLapse = false; |
| 81 | } else { |
| 82 | // TODO: Add a check to see that mTimeBetweenTimeLapseFrameCaptureUs is greater |
| 83 | // than the fastest rate at which the still camera can take pictures. |
| 84 | mUseStillCameraForTimeLapse = true; |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 85 | CHECK(setPictureSizeToClosestSupported(videoSize.width, videoSize.height)); |
Nipun Kwatra | dce4beb | 2010-07-27 22:21:44 -0700 | [diff] [blame] | 86 | mNeedCropping = computeCropRectangleOffset(); |
James Dong | 0c128b6 | 2010-10-08 11:59:32 -0700 | [diff] [blame] | 87 | mMeta->setInt32(kKeyWidth, videoSize.width); |
| 88 | mMeta->setInt32(kKeyHeight, videoSize.height); |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 89 | } |
Nipun Kwatra | 7553cf7 | 2010-09-15 15:08:49 -0700 | [diff] [blame] | 90 | |
| 91 | // Initialize quick stop variables. |
| 92 | mQuickStop = false; |
| 93 | mForceRead = false; |
| 94 | mLastReadBufferCopy = NULL; |
| 95 | mStopWaitingForIdleCamera = false; |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | CameraSourceTimeLapse::~CameraSourceTimeLapse() { |
| 99 | } |
| 100 | |
Nipun Kwatra | 7553cf7 | 2010-09-15 15:08:49 -0700 | [diff] [blame] | 101 | void CameraSourceTimeLapse::startQuickReadReturns() { |
| 102 | Mutex::Autolock autoLock(mQuickStopLock); |
| 103 | LOGV("Enabling quick read returns"); |
| 104 | |
| 105 | // Enable quick stop mode. |
| 106 | mQuickStop = true; |
| 107 | |
| 108 | if (mUseStillCameraForTimeLapse) { |
| 109 | // wake up the thread right away. |
| 110 | mTakePictureCondition.signal(); |
| 111 | } else { |
| 112 | // Force dataCallbackTimestamp() coming from the video camera to not skip the |
| 113 | // next frame as we want read() to get a get a frame right away. |
| 114 | mForceRead = true; |
| 115 | } |
| 116 | } |
| 117 | |
James Dong | a1d2d8f | 2011-01-04 16:09:07 -0800 | [diff] [blame^] | 118 | bool CameraSourceTimeLapse::trySettingVideoSize(int32_t width, int32_t height) { |
| 119 | LOGV("trySettingVideoSize: %dx%d", width, height); |
Nipun Kwatra | 4a857e6 | 2010-09-02 11:43:15 -0700 | [diff] [blame] | 120 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
| 121 | String8 s = mCamera->getParameters(); |
Nipun Kwatra | 4a857e6 | 2010-09-02 11:43:15 -0700 | [diff] [blame] | 122 | |
| 123 | CameraParameters params(s); |
| 124 | Vector<Size> supportedSizes; |
James Dong | a1d2d8f | 2011-01-04 16:09:07 -0800 | [diff] [blame^] | 125 | params.getSupportedVideoSizes(supportedSizes); |
| 126 | bool videoOutputSupported = false; |
| 127 | if (supportedSizes.size() == 0) { |
| 128 | params.getSupportedPreviewSizes(supportedSizes); |
| 129 | } else { |
| 130 | videoOutputSupported = true; |
| 131 | } |
Nipun Kwatra | 4a857e6 | 2010-09-02 11:43:15 -0700 | [diff] [blame] | 132 | |
James Dong | a1d2d8f | 2011-01-04 16:09:07 -0800 | [diff] [blame^] | 133 | bool videoSizeSupported = false; |
Nipun Kwatra | 4a857e6 | 2010-09-02 11:43:15 -0700 | [diff] [blame] | 134 | for (uint32_t i = 0; i < supportedSizes.size(); ++i) { |
| 135 | int32_t pictureWidth = supportedSizes[i].width; |
| 136 | int32_t pictureHeight = supportedSizes[i].height; |
| 137 | |
| 138 | if ((pictureWidth == width) && (pictureHeight == height)) { |
James Dong | a1d2d8f | 2011-01-04 16:09:07 -0800 | [diff] [blame^] | 139 | videoSizeSupported = true; |
Nipun Kwatra | 4a857e6 | 2010-09-02 11:43:15 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
James Dong | 08800f3 | 2010-12-09 15:04:33 -0800 | [diff] [blame] | 143 | bool isSuccessful = false; |
James Dong | a1d2d8f | 2011-01-04 16:09:07 -0800 | [diff] [blame^] | 144 | if (videoSizeSupported) { |
| 145 | LOGV("Video size (%d, %d) is supported", width, height); |
| 146 | if (videoOutputSupported) { |
| 147 | params.setVideoSize(width, height); |
| 148 | } else { |
| 149 | params.setPreviewSize(width, height); |
| 150 | } |
James Dong | 08800f3 | 2010-12-09 15:04:33 -0800 | [diff] [blame] | 151 | if (mCamera->setParameters(params.flatten()) == OK) { |
| 152 | isSuccessful = true; |
| 153 | } else { |
| 154 | LOGE("Failed to set preview size to %dx%d", width, height); |
| 155 | isSuccessful = false; |
| 156 | } |
Nipun Kwatra | 4a857e6 | 2010-09-02 11:43:15 -0700 | [diff] [blame] | 157 | } |
| 158 | |
James Dong | 08800f3 | 2010-12-09 15:04:33 -0800 | [diff] [blame] | 159 | IPCThreadState::self()->restoreCallingIdentity(token); |
| 160 | return isSuccessful; |
Nipun Kwatra | 4a857e6 | 2010-09-02 11:43:15 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Nipun Kwatra | b1fb607 | 2010-07-30 18:30:55 -0700 | [diff] [blame] | 163 | bool CameraSourceTimeLapse::setPictureSizeToClosestSupported(int32_t width, int32_t height) { |
James Dong | 08800f3 | 2010-12-09 15:04:33 -0800 | [diff] [blame] | 164 | LOGV("setPictureSizeToClosestSupported: %dx%d", width, height); |
Nipun Kwatra | b1fb607 | 2010-07-30 18:30:55 -0700 | [diff] [blame] | 165 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
| 166 | String8 s = mCamera->getParameters(); |
| 167 | IPCThreadState::self()->restoreCallingIdentity(token); |
| 168 | |
| 169 | CameraParameters params(s); |
| 170 | Vector<Size> supportedSizes; |
| 171 | params.getSupportedPictureSizes(supportedSizes); |
| 172 | |
| 173 | int32_t minPictureSize = INT_MAX; |
| 174 | for (uint32_t i = 0; i < supportedSizes.size(); ++i) { |
| 175 | int32_t pictureWidth = supportedSizes[i].width; |
| 176 | int32_t pictureHeight = supportedSizes[i].height; |
| 177 | |
| 178 | if ((pictureWidth >= width) && (pictureHeight >= height)) { |
| 179 | int32_t pictureSize = pictureWidth*pictureHeight; |
| 180 | if (pictureSize < minPictureSize) { |
| 181 | minPictureSize = pictureSize; |
| 182 | mPictureWidth = pictureWidth; |
| 183 | mPictureHeight = pictureHeight; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | LOGV("Picture size = (%d, %d)", mPictureWidth, mPictureHeight); |
| 188 | return (minPictureSize != INT_MAX); |
Nipun Kwatra | dce4beb | 2010-07-27 22:21:44 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | bool CameraSourceTimeLapse::computeCropRectangleOffset() { |
| 192 | if ((mPictureWidth == mVideoWidth) && (mPictureHeight == mVideoHeight)) { |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | CHECK((mPictureWidth > mVideoWidth) && (mPictureHeight > mVideoHeight)); |
| 197 | |
| 198 | int32_t widthDifference = mPictureWidth - mVideoWidth; |
| 199 | int32_t heightDifference = mPictureHeight - mVideoHeight; |
| 200 | |
| 201 | mCropRectStartX = widthDifference/2; |
| 202 | mCropRectStartY = heightDifference/2; |
| 203 | |
| 204 | LOGV("setting crop rectangle offset to (%d, %d)", mCropRectStartX, mCropRectStartY); |
| 205 | |
| 206 | return true; |
| 207 | } |
| 208 | |
Nipun Kwatra | 7553cf7 | 2010-09-15 15:08:49 -0700 | [diff] [blame] | 209 | void CameraSourceTimeLapse::signalBufferReturned(MediaBuffer* buffer) { |
| 210 | Mutex::Autolock autoLock(mQuickStopLock); |
| 211 | if (mQuickStop && (buffer == mLastReadBufferCopy)) { |
| 212 | buffer->setObserver(NULL); |
| 213 | buffer->release(); |
| 214 | } else { |
| 215 | return CameraSource::signalBufferReturned(buffer); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | void createMediaBufferCopy(const MediaBuffer& sourceBuffer, int64_t frameTime, MediaBuffer **newBuffer) { |
| 220 | size_t sourceSize = sourceBuffer.size(); |
| 221 | void* sourcePointer = sourceBuffer.data(); |
| 222 | |
| 223 | (*newBuffer) = new MediaBuffer(sourceSize); |
| 224 | memcpy((*newBuffer)->data(), sourcePointer, sourceSize); |
| 225 | |
| 226 | (*newBuffer)->meta_data()->setInt64(kKeyTime, frameTime); |
| 227 | } |
| 228 | |
| 229 | void CameraSourceTimeLapse::fillLastReadBufferCopy(MediaBuffer& sourceBuffer) { |
| 230 | int64_t frameTime; |
| 231 | CHECK(sourceBuffer.meta_data()->findInt64(kKeyTime, &frameTime)); |
| 232 | createMediaBufferCopy(sourceBuffer, frameTime, &mLastReadBufferCopy); |
| 233 | mLastReadBufferCopy->add_ref(); |
| 234 | mLastReadBufferCopy->setObserver(this); |
| 235 | } |
| 236 | |
| 237 | status_t CameraSourceTimeLapse::read( |
| 238 | MediaBuffer **buffer, const ReadOptions *options) { |
| 239 | if (mLastReadBufferCopy == NULL) { |
| 240 | mLastReadStatus = CameraSource::read(buffer, options); |
| 241 | |
| 242 | // mQuickStop may have turned to true while read was blocked. Make a copy of |
| 243 | // the buffer in that case. |
| 244 | Mutex::Autolock autoLock(mQuickStopLock); |
| 245 | if (mQuickStop && *buffer) { |
| 246 | fillLastReadBufferCopy(**buffer); |
| 247 | } |
| 248 | return mLastReadStatus; |
| 249 | } else { |
| 250 | (*buffer) = mLastReadBufferCopy; |
| 251 | (*buffer)->add_ref(); |
| 252 | return mLastReadStatus; |
| 253 | } |
| 254 | } |
| 255 | |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 256 | // static |
| 257 | void *CameraSourceTimeLapse::ThreadTimeLapseWrapper(void *me) { |
| 258 | CameraSourceTimeLapse *source = static_cast<CameraSourceTimeLapse *>(me); |
| 259 | source->threadTimeLapseEntry(); |
| 260 | return NULL; |
| 261 | } |
| 262 | |
| 263 | void CameraSourceTimeLapse::threadTimeLapseEntry() { |
Nipun Kwatra | 7553cf7 | 2010-09-15 15:08:49 -0700 | [diff] [blame] | 264 | while (mStarted) { |
| 265 | { |
| 266 | Mutex::Autolock autoLock(mCameraIdleLock); |
| 267 | if (!mCameraIdle) { |
| 268 | mCameraIdleCondition.wait(mCameraIdleLock); |
| 269 | } |
| 270 | CHECK(mCameraIdle); |
Nipun Kwatra | 4cd8672 | 2010-07-18 15:52:02 -0700 | [diff] [blame] | 271 | mCameraIdle = false; |
Nipun Kwatra | 4cd8672 | 2010-07-18 15:52:02 -0700 | [diff] [blame] | 272 | } |
Nipun Kwatra | 7553cf7 | 2010-09-15 15:08:49 -0700 | [diff] [blame] | 273 | |
| 274 | // Even if mQuickStop == true we need to take one more picture |
| 275 | // as a read() may be blocked, waiting for a frame to get available. |
| 276 | // After this takePicture, if mQuickStop == true, we can safely exit |
| 277 | // this thread as read() will make a copy of this last frame and keep |
| 278 | // returning it in the quick stop mode. |
| 279 | Mutex::Autolock autoLock(mQuickStopLock); |
| 280 | CHECK_EQ(OK, mCamera->takePicture()); |
| 281 | if (mQuickStop) { |
| 282 | LOGV("threadTimeLapseEntry: Exiting due to mQuickStop = true"); |
| 283 | return; |
| 284 | } |
| 285 | mTakePictureCondition.waitRelative(mQuickStopLock, |
| 286 | mTimeBetweenTimeLapseFrameCaptureUs * 1000); |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 287 | } |
Nipun Kwatra | 7553cf7 | 2010-09-15 15:08:49 -0700 | [diff] [blame] | 288 | LOGV("threadTimeLapseEntry: Exiting due to mStarted = false"); |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | void CameraSourceTimeLapse::startCameraRecording() { |
Nipun Kwatra | dce4beb | 2010-07-27 22:21:44 -0700 | [diff] [blame] | 292 | if (mUseStillCameraForTimeLapse) { |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 293 | LOGV("start time lapse recording using still camera"); |
| 294 | |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 295 | int64_t token = IPCThreadState::self()->clearCallingIdentity(); |
| 296 | String8 s = mCamera->getParameters(); |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 297 | |
| 298 | CameraParameters params(s); |
Nipun Kwatra | dce4beb | 2010-07-27 22:21:44 -0700 | [diff] [blame] | 299 | params.setPictureSize(mPictureWidth, mPictureHeight); |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 300 | mCamera->setParameters(params.flatten()); |
Nipun Kwatra | 4cd8672 | 2010-07-18 15:52:02 -0700 | [diff] [blame] | 301 | mCameraIdle = true; |
Nipun Kwatra | 7553cf7 | 2010-09-15 15:08:49 -0700 | [diff] [blame] | 302 | mStopWaitingForIdleCamera = false; |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 303 | |
Nipun Kwatra | 8e02ca7 | 2010-09-14 21:22:59 -0700 | [diff] [blame] | 304 | // disable shutter sound and play the recording sound. |
| 305 | mCamera->sendCommand(CAMERA_CMD_ENABLE_SHUTTER_SOUND, 0, 0); |
| 306 | mCamera->sendCommand(CAMERA_CMD_PLAY_RECORDING_SOUND, 0, 0); |
James Dong | 08800f3 | 2010-12-09 15:04:33 -0800 | [diff] [blame] | 307 | IPCThreadState::self()->restoreCallingIdentity(token); |
Nipun Kwatra | 8e02ca7 | 2010-09-14 21:22:59 -0700 | [diff] [blame] | 308 | |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 309 | // create a thread which takes pictures in a loop |
| 310 | pthread_attr_t attr; |
| 311 | pthread_attr_init(&attr); |
| 312 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); |
| 313 | |
| 314 | pthread_create(&mThreadTimeLapse, &attr, ThreadTimeLapseWrapper, this); |
| 315 | pthread_attr_destroy(&attr); |
| 316 | } else { |
| 317 | LOGV("start time lapse recording using video camera"); |
| 318 | CHECK_EQ(OK, mCamera->startRecording()); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | void CameraSourceTimeLapse::stopCameraRecording() { |
Nipun Kwatra | dce4beb | 2010-07-27 22:21:44 -0700 | [diff] [blame] | 323 | if (mUseStillCameraForTimeLapse) { |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 324 | void *dummy; |
| 325 | pthread_join(mThreadTimeLapse, &dummy); |
Nipun Kwatra | 8e02ca7 | 2010-09-14 21:22:59 -0700 | [diff] [blame] | 326 | |
Nipun Kwatra | 7553cf7 | 2010-09-15 15:08:49 -0700 | [diff] [blame] | 327 | // Last takePicture may still be underway. Wait for the camera to get |
| 328 | // idle. |
| 329 | Mutex::Autolock autoLock(mCameraIdleLock); |
| 330 | mStopWaitingForIdleCamera = true; |
| 331 | if (!mCameraIdle) { |
| 332 | mCameraIdleCondition.wait(mCameraIdleLock); |
| 333 | } |
| 334 | CHECK(mCameraIdle); |
| 335 | mCamera->setListener(NULL); |
| 336 | |
| 337 | // play the recording sound. |
Nipun Kwatra | 8e02ca7 | 2010-09-14 21:22:59 -0700 | [diff] [blame] | 338 | mCamera->sendCommand(CAMERA_CMD_PLAY_RECORDING_SOUND, 0, 0); |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 339 | } else { |
Nipun Kwatra | 7553cf7 | 2010-09-15 15:08:49 -0700 | [diff] [blame] | 340 | mCamera->setListener(NULL); |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 341 | mCamera->stopRecording(); |
| 342 | } |
Nipun Kwatra | 7553cf7 | 2010-09-15 15:08:49 -0700 | [diff] [blame] | 343 | if (mLastReadBufferCopy) { |
| 344 | mLastReadBufferCopy->release(); |
| 345 | mLastReadBufferCopy = NULL; |
| 346 | } |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | void CameraSourceTimeLapse::releaseRecordingFrame(const sp<IMemory>& frame) { |
Nipun Kwatra | dce4beb | 2010-07-27 22:21:44 -0700 | [diff] [blame] | 350 | if (!mUseStillCameraForTimeLapse) { |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 351 | mCamera->releaseRecordingFrame(frame); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | sp<IMemory> CameraSourceTimeLapse::createIMemoryCopy(const sp<IMemory> &source_data) { |
| 356 | size_t source_size = source_data->size(); |
| 357 | void* source_pointer = source_data->pointer(); |
| 358 | |
| 359 | sp<MemoryHeapBase> newMemoryHeap = new MemoryHeapBase(source_size); |
| 360 | sp<MemoryBase> newMemory = new MemoryBase(newMemoryHeap, 0, source_size); |
| 361 | memcpy(newMemory->pointer(), source_pointer, source_size); |
| 362 | return newMemory; |
| 363 | } |
| 364 | |
Nipun Kwatra | dce4beb | 2010-07-27 22:21:44 -0700 | [diff] [blame] | 365 | // Allocates IMemory of final type MemoryBase with the given size. |
| 366 | sp<IMemory> allocateIMemory(size_t size) { |
| 367 | sp<MemoryHeapBase> newMemoryHeap = new MemoryHeapBase(size); |
| 368 | sp<MemoryBase> newMemory = new MemoryBase(newMemoryHeap, 0, size); |
| 369 | return newMemory; |
| 370 | } |
| 371 | |
Nipun Kwatra | 4cd8672 | 2010-07-18 15:52:02 -0700 | [diff] [blame] | 372 | // static |
| 373 | void *CameraSourceTimeLapse::ThreadStartPreviewWrapper(void *me) { |
| 374 | CameraSourceTimeLapse *source = static_cast<CameraSourceTimeLapse *>(me); |
| 375 | source->threadStartPreview(); |
| 376 | return NULL; |
| 377 | } |
| 378 | |
| 379 | void CameraSourceTimeLapse::threadStartPreview() { |
| 380 | CHECK_EQ(OK, mCamera->startPreview()); |
Nipun Kwatra | 7553cf7 | 2010-09-15 15:08:49 -0700 | [diff] [blame] | 381 | Mutex::Autolock autoLock(mCameraIdleLock); |
Nipun Kwatra | 4cd8672 | 2010-07-18 15:52:02 -0700 | [diff] [blame] | 382 | mCameraIdle = true; |
Nipun Kwatra | 7553cf7 | 2010-09-15 15:08:49 -0700 | [diff] [blame] | 383 | mCameraIdleCondition.signal(); |
Nipun Kwatra | 4cd8672 | 2010-07-18 15:52:02 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | void CameraSourceTimeLapse::restartPreview() { |
| 387 | // Start this in a different thread, so that the dataCallback can return |
| 388 | LOGV("restartPreview"); |
| 389 | pthread_attr_t attr; |
| 390 | pthread_attr_init(&attr); |
| 391 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 392 | |
| 393 | pthread_t threadPreview; |
| 394 | pthread_create(&threadPreview, &attr, ThreadStartPreviewWrapper, this); |
| 395 | pthread_attr_destroy(&attr); |
| 396 | } |
| 397 | |
Nipun Kwatra | dce4beb | 2010-07-27 22:21:44 -0700 | [diff] [blame] | 398 | sp<IMemory> CameraSourceTimeLapse::cropYUVImage(const sp<IMemory> &source_data) { |
| 399 | // find the YUV format |
| 400 | int32_t srcFormat; |
| 401 | CHECK(mMeta->findInt32(kKeyColorFormat, &srcFormat)); |
| 402 | YUVImage::YUVFormat yuvFormat; |
| 403 | if (srcFormat == OMX_COLOR_FormatYUV420SemiPlanar) { |
| 404 | yuvFormat = YUVImage::YUV420SemiPlanar; |
James Dong | 5d1d920 | 2010-09-15 16:58:44 -0700 | [diff] [blame] | 405 | } else { |
| 406 | CHECK_EQ(srcFormat, OMX_COLOR_FormatYUV420Planar); |
Nipun Kwatra | dce4beb | 2010-07-27 22:21:44 -0700 | [diff] [blame] | 407 | yuvFormat = YUVImage::YUV420Planar; |
| 408 | } |
| 409 | |
| 410 | // allocate memory for cropped image and setup a canvas using it. |
| 411 | sp<IMemory> croppedImageMemory = allocateIMemory( |
| 412 | YUVImage::bufferSize(yuvFormat, mVideoWidth, mVideoHeight)); |
| 413 | YUVImage yuvImageCropped(yuvFormat, |
| 414 | mVideoWidth, mVideoHeight, |
| 415 | (uint8_t *)croppedImageMemory->pointer()); |
| 416 | YUVCanvas yuvCanvasCrop(yuvImageCropped); |
| 417 | |
| 418 | YUVImage yuvImageSource(yuvFormat, |
| 419 | mPictureWidth, mPictureHeight, |
| 420 | (uint8_t *)source_data->pointer()); |
| 421 | yuvCanvasCrop.CopyImageRect( |
| 422 | Rect(mCropRectStartX, mCropRectStartY, |
| 423 | mCropRectStartX + mVideoWidth, |
| 424 | mCropRectStartY + mVideoHeight), |
| 425 | 0, 0, |
| 426 | yuvImageSource); |
| 427 | |
| 428 | return croppedImageMemory; |
| 429 | } |
| 430 | |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 431 | void CameraSourceTimeLapse::dataCallback(int32_t msgType, const sp<IMemory> &data) { |
Nipun Kwatra | dce4beb | 2010-07-27 22:21:44 -0700 | [diff] [blame] | 432 | if (msgType == CAMERA_MSG_COMPRESSED_IMAGE) { |
Nipun Kwatra | 4cd8672 | 2010-07-18 15:52:02 -0700 | [diff] [blame] | 433 | // takePicture will complete after this callback, so restart preview. |
| 434 | restartPreview(); |
Nipun Kwatra | dce4beb | 2010-07-27 22:21:44 -0700 | [diff] [blame] | 435 | return; |
Nipun Kwatra | 4cd8672 | 2010-07-18 15:52:02 -0700 | [diff] [blame] | 436 | } |
Nipun Kwatra | dce4beb | 2010-07-27 22:21:44 -0700 | [diff] [blame] | 437 | if (msgType != CAMERA_MSG_RAW_IMAGE) { |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 438 | return; |
| 439 | } |
| 440 | |
| 441 | LOGV("dataCallback for timelapse still frame"); |
| 442 | CHECK_EQ(true, mUseStillCameraForTimeLapse); |
| 443 | |
| 444 | int64_t timestampUs; |
| 445 | if (mNumFramesReceived == 0) { |
| 446 | timestampUs = mStartTimeUs; |
| 447 | } else { |
| 448 | timestampUs = mLastFrameTimestampUs + mTimeBetweenTimeLapseVideoFramesUs; |
| 449 | } |
Nipun Kwatra | dce4beb | 2010-07-27 22:21:44 -0700 | [diff] [blame] | 450 | |
| 451 | if (mNeedCropping) { |
| 452 | sp<IMemory> croppedImageData = cropYUVImage(data); |
| 453 | dataCallbackTimestamp(timestampUs, msgType, croppedImageData); |
| 454 | } else { |
| 455 | sp<IMemory> dataCopy = createIMemoryCopy(data); |
| 456 | dataCallbackTimestamp(timestampUs, msgType, dataCopy); |
| 457 | } |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | bool CameraSourceTimeLapse::skipCurrentFrame(int64_t timestampUs) { |
Nipun Kwatra | dce4beb | 2010-07-27 22:21:44 -0700 | [diff] [blame] | 461 | if (mSkipCurrentFrame) { |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 462 | mSkipCurrentFrame = false; |
| 463 | return true; |
| 464 | } else { |
| 465 | return false; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | bool CameraSourceTimeLapse::skipFrameAndModifyTimeStamp(int64_t *timestampUs) { |
Nipun Kwatra | dce4beb | 2010-07-27 22:21:44 -0700 | [diff] [blame] | 470 | if (!mUseStillCameraForTimeLapse) { |
| 471 | if (mLastTimeLapseFrameRealTimestampUs == 0) { |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 472 | // First time lapse frame. Initialize mLastTimeLapseFrameRealTimestampUs |
| 473 | // to current time (timestampUs) and save frame data. |
| 474 | LOGV("dataCallbackTimestamp timelapse: initial frame"); |
| 475 | |
| 476 | mLastTimeLapseFrameRealTimestampUs = *timestampUs; |
Nipun Kwatra | 7553cf7 | 2010-09-15 15:08:49 -0700 | [diff] [blame] | 477 | return false; |
| 478 | } |
| 479 | |
| 480 | { |
| 481 | Mutex::Autolock autoLock(mQuickStopLock); |
| 482 | |
| 483 | // mForceRead may be set to true by startQuickReadReturns(). In that |
| 484 | // case don't skip this frame. |
| 485 | if (mForceRead) { |
| 486 | LOGV("dataCallbackTimestamp timelapse: forced read"); |
| 487 | mForceRead = false; |
| 488 | *timestampUs = mLastFrameTimestampUs; |
| 489 | return false; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | if (*timestampUs < |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 494 | (mLastTimeLapseFrameRealTimestampUs + mTimeBetweenTimeLapseFrameCaptureUs)) { |
| 495 | // Skip all frames from last encoded frame until |
| 496 | // sufficient time (mTimeBetweenTimeLapseFrameCaptureUs) has passed. |
| 497 | // Tell the camera to release its recording frame and return. |
| 498 | LOGV("dataCallbackTimestamp timelapse: skipping intermediate frame"); |
| 499 | return true; |
| 500 | } else { |
| 501 | // Desired frame has arrived after mTimeBetweenTimeLapseFrameCaptureUs time: |
| 502 | // - Reset mLastTimeLapseFrameRealTimestampUs to current time. |
| 503 | // - Artificially modify timestampUs to be one frame time (1/framerate) ahead |
| 504 | // of the last encoded frame's time stamp. |
| 505 | LOGV("dataCallbackTimestamp timelapse: got timelapse frame"); |
| 506 | |
| 507 | mLastTimeLapseFrameRealTimestampUs = *timestampUs; |
| 508 | *timestampUs = mLastFrameTimestampUs + mTimeBetweenTimeLapseVideoFramesUs; |
Nipun Kwatra | 7553cf7 | 2010-09-15 15:08:49 -0700 | [diff] [blame] | 509 | return false; |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | return false; |
| 513 | } |
| 514 | |
| 515 | void CameraSourceTimeLapse::dataCallbackTimestamp(int64_t timestampUs, int32_t msgType, |
| 516 | const sp<IMemory> &data) { |
Nipun Kwatra | dce4beb | 2010-07-27 22:21:44 -0700 | [diff] [blame] | 517 | if (!mUseStillCameraForTimeLapse) { |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 518 | mSkipCurrentFrame = skipFrameAndModifyTimeStamp(×tampUs); |
Nipun Kwatra | 7553cf7 | 2010-09-15 15:08:49 -0700 | [diff] [blame] | 519 | } else { |
| 520 | Mutex::Autolock autoLock(mCameraIdleLock); |
| 521 | // If we are using the still camera and stop() has been called, it may |
| 522 | // be waiting for the camera to get idle. In that case return |
| 523 | // immediately. Calling CameraSource::dataCallbackTimestamp() will lead |
| 524 | // to a deadlock since it tries to access CameraSource::mLock which in |
| 525 | // this case is held by CameraSource::stop() currently waiting for the |
| 526 | // camera to get idle. And camera will not get idle until this call |
| 527 | // returns. |
| 528 | if (mStopWaitingForIdleCamera) { |
| 529 | return; |
| 530 | } |
Nipun Kwatra | f9b8018 | 2010-07-12 09:17:14 -0700 | [diff] [blame] | 531 | } |
| 532 | CameraSource::dataCallbackTimestamp(timestampUs, msgType, data); |
| 533 | } |
| 534 | |
| 535 | } // namespace android |