CameraSourceTimeLapse.h revision 4ca2c7c913f8bd4ada13aca56d36045d42d1e00f
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#ifndef CAMERA_SOURCE_TIME_LAPSE_H_ 18 19#define CAMERA_SOURCE_TIME_LAPSE_H_ 20 21#include <pthread.h> 22 23#include <utils/RefBase.h> 24#include <utils/threads.h> 25 26namespace android { 27 28class ICamera; 29class IMemory; 30class Camera; 31 32class CameraSourceTimeLapse : public CameraSource { 33public: 34 static CameraSourceTimeLapse *CreateFromCamera( 35 const sp<ICamera> &camera, 36 const sp<ICameraRecordingProxy> &proxy, 37 int32_t cameraId, 38 Size videoSize, 39 int32_t videoFrameRate, 40 const sp<Surface>& surface, 41 int64_t timeBetweenTimeLapseFrameCaptureUs); 42 43 virtual ~CameraSourceTimeLapse(); 44 45 // If the frame capture interval is large, read will block for a long time. 46 // Due to the way the mediaRecorder framework works, a stop() call from 47 // mediaRecorder waits until the read returns, causing a long wait for 48 // stop() to return. To avoid this, we can make read() return a copy of the 49 // last read frame with the same time stamp frequently. This keeps the 50 // read() call from blocking too long. Calling this function quickly 51 // captures another frame, keeps its copy, and enables this mode of read() 52 // returning quickly. 53 void startQuickReadReturns(); 54 55private: 56 // If true, will use still camera takePicture() for time lapse frames 57 // If false, will use the videocamera frames instead. 58 bool mUseStillCameraForTimeLapse; 59 60 // Size of picture taken from still camera. This may be larger than the size 61 // of the video, as still camera may not support the exact video resolution 62 // demanded. See setPictureSizeToClosestSupported(). 63 int32_t mPictureWidth; 64 int32_t mPictureHeight; 65 66 // size of the encoded video. 67 int32_t mVideoWidth; 68 int32_t mVideoHeight; 69 70 // True if we need to crop the still camera image to get the video frame. 71 bool mNeedCropping; 72 73 // Start location of the cropping rectangle. 74 int32_t mCropRectStartX; 75 int32_t mCropRectStartY; 76 77 // Time between capture of two frames during time lapse recording 78 // Negative value indicates that timelapse is disabled. 79 int64_t mTimeBetweenTimeLapseFrameCaptureUs; 80 81 // Time between two frames in final video (1/frameRate) 82 int64_t mTimeBetweenTimeLapseVideoFramesUs; 83 84 // Real timestamp of the last encoded time lapse frame 85 int64_t mLastTimeLapseFrameRealTimestampUs; 86 87 // Thread id of thread which takes still picture and sleeps in a loop. 88 pthread_t mThreadTimeLapse; 89 90 // Variable set in dataCallbackTimestamp() to help skipCurrentFrame() 91 // to know if current frame needs to be skipped. 92 bool mSkipCurrentFrame; 93 94 // Lock for accessing mCameraIdle 95 Mutex mCameraIdleLock; 96 97 // Condition variable to wait on if camera is is not yet idle. Once the 98 // camera gets idle, this variable will be signalled. 99 Condition mCameraIdleCondition; 100 101 // True if camera is in preview mode and ready for takePicture(). 102 // False after a call to takePicture() but before the final compressed 103 // data callback has been called and preview has been restarted. 104 volatile bool mCameraIdle; 105 106 // True if stop() is waiting for camera to get idle, i.e. for the last 107 // takePicture() to complete. This is needed so that dataCallbackTimestamp() 108 // can return immediately. 109 volatile bool mStopWaitingForIdleCamera; 110 111 // Lock for accessing quick stop variables. 112 Mutex mQuickStopLock; 113 114 // Condition variable to wake up still picture thread. 115 Condition mTakePictureCondition; 116 117 // mQuickStop is set to true if we use quick read() returns, otherwise it is set 118 // to false. Once in this mode read() return a copy of the last read frame 119 // with the same time stamp. See startQuickReadReturns(). 120 volatile bool mQuickStop; 121 122 // Forces the next frame passed to dataCallbackTimestamp() to be read 123 // as a time lapse frame. Used by startQuickReadReturns() so that the next 124 // frame wakes up any blocking read. 125 volatile bool mForceRead; 126 127 // Stores a copy of the MediaBuffer read in the last read() call after 128 // mQuickStop was true. 129 MediaBuffer* mLastReadBufferCopy; 130 131 // Status code for last read. 132 status_t mLastReadStatus; 133 134 CameraSourceTimeLapse( 135 const sp<ICamera> &camera, 136 const sp<ICameraRecordingProxy> &proxy, 137 int32_t cameraId, 138 Size videoSize, 139 int32_t videoFrameRate, 140 const sp<Surface>& surface, 141 int64_t timeBetweenTimeLapseFrameCaptureUs); 142 143 // Wrapper over CameraSource::signalBufferReturned() to implement quick stop. 144 // It only handles the case when mLastReadBufferCopy is signalled. Otherwise 145 // it calls the base class' function. 146 virtual void signalBufferReturned(MediaBuffer* buffer); 147 148 // Wrapper over CameraSource::read() to implement quick stop. 149 virtual status_t read(MediaBuffer **buffer, const ReadOptions *options = NULL); 150 151 // For still camera case starts a thread which calls camera's takePicture() 152 // in a loop. For video camera case, just starts the camera's video recording. 153 virtual void startCameraRecording(); 154 155 // For still camera case joins the thread created in startCameraRecording(). 156 // For video camera case, just stops the camera's video recording. 157 virtual void stopCameraRecording(); 158 159 // For still camera case don't need to do anything as memory is locally 160 // allocated with refcounting. 161 // For video camera case just tell the camera to release the frame. 162 virtual void releaseRecordingFrame(const sp<IMemory>& frame); 163 164 // mSkipCurrentFrame is set to true in dataCallbackTimestamp() if the current 165 // frame needs to be skipped and this function just returns the value of mSkipCurrentFrame. 166 virtual bool skipCurrentFrame(int64_t timestampUs); 167 168 // Handles the callback to handle raw frame data from the still camera. 169 // Creates a copy of the frame data as the camera can reuse the frame memory 170 // once this callback returns. The function also sets a new timstamp corresponding 171 // to one frame time ahead of the last encoded frame's time stamp. It then 172 // calls dataCallbackTimestamp() of the base class with the copied data and the 173 // modified timestamp, which will think that it recieved the frame from a video 174 // camera and proceed as usual. 175 virtual void dataCallback(int32_t msgType, const sp<IMemory> &data); 176 177 // In the video camera case calls skipFrameAndModifyTimeStamp() to modify 178 // timestamp and set mSkipCurrentFrame. 179 // Then it calls the base CameraSource::dataCallbackTimestamp() 180 virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType, 181 const sp<IMemory> &data); 182 183 // Convenience function to fill mLastReadBufferCopy from the just read 184 // buffer. 185 void fillLastReadBufferCopy(MediaBuffer& sourceBuffer); 186 187 // If the passed in size (width x height) is a supported video/preview size, 188 // the function sets the camera's video/preview size to it and returns true. 189 // Otherwise returns false. 190 bool trySettingVideoSize(int32_t width, int32_t height); 191 192 // The still camera may not support the demanded video width and height. 193 // We look for the supported picture sizes from the still camera and 194 // choose the smallest one with either dimensions higher than the corresponding 195 // video dimensions. The still picture will be cropped to get the video frame. 196 // The function returns true if the camera supports picture sizes greater than 197 // or equal to the passed in width and height, and false otherwise. 198 bool setPictureSizeToClosestSupported(int32_t width, int32_t height); 199 200 // Computes the offset of the rectangle from where to start cropping the 201 // still image into the video frame. We choose the center of the image to be 202 // cropped. The offset is stored in (mCropRectStartX, mCropRectStartY). 203 bool computeCropRectangleOffset(); 204 205 // Crops the source data into a smaller image starting at 206 // (mCropRectStartX, mCropRectStartY) and of the size of the video frame. 207 // The data is returned into a newly allocated IMemory. 208 sp<IMemory> cropYUVImage(const sp<IMemory> &source_data); 209 210 // When video camera is used for time lapse capture, returns true 211 // until enough time has passed for the next time lapse frame. When 212 // the frame needs to be encoded, it returns false and also modifies 213 // the time stamp to be one frame time ahead of the last encoded 214 // frame's time stamp. 215 bool skipFrameAndModifyTimeStamp(int64_t *timestampUs); 216 217 // Wrapper to enter threadTimeLapseEntry() 218 static void *ThreadTimeLapseWrapper(void *me); 219 220 // Runs a loop which sleeps until a still picture is required 221 // and then calls mCamera->takePicture() to take the still picture. 222 // Used only in the case mUseStillCameraForTimeLapse = true. 223 void threadTimeLapseEntry(); 224 225 // Wrapper to enter threadStartPreview() 226 static void *ThreadStartPreviewWrapper(void *me); 227 228 // Starts the camera's preview. 229 void threadStartPreview(); 230 231 // Starts thread ThreadStartPreviewWrapper() for restarting preview. 232 // Needs to be done in a thread so that dataCallback() which calls this function 233 // can return, and the camera can know that takePicture() is done. 234 void restartPreview(); 235 236 // Creates a copy of source_data into a new memory of final type MemoryBase. 237 sp<IMemory> createIMemoryCopy(const sp<IMemory> &source_data); 238 239 CameraSourceTimeLapse(const CameraSourceTimeLapse &); 240 CameraSourceTimeLapse &operator=(const CameraSourceTimeLapse &); 241}; 242 243} // namespace android 244 245#endif // CAMERA_SOURCE_TIME_LAPSE_H_ 246