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