CameraSourceTimeLapse.h revision 40e2f3f9b41f44bdb59f7708a421b87f169a6ede
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 *Create(bool useStillCameraForTimeLapse, 35 int64_t timeBetweenTimeLapseFrameCaptureUs, 36 int32_t width, int32_t height, 37 int32_t videoFrameRate); 38 39 static CameraSourceTimeLapse *CreateFromCamera(const sp<Camera> &camera, 40 bool useStillCameraForTimeLapse, 41 int64_t timeBetweenTimeLapseFrameCaptureUs, 42 int32_t width, int32_t height, 43 int32_t videoFrameRate); 44 45 virtual ~CameraSourceTimeLapse(); 46 47private: 48 // If true, will use still camera takePicture() for time lapse frames 49 // If false, will use the videocamera frames instead. 50 bool mUseStillCameraForTimeLapse; 51 52 // Size of picture taken from still camera. This may be larger than the size 53 // of the video, as still camera may not support the exact video resolution 54 // demanded. See setPictureSizeToClosestSupported(). 55 int32_t mPictureWidth; 56 int32_t mPictureHeight; 57 58 // size of the encoded video. 59 int32_t mVideoWidth; 60 int32_t mVideoHeight; 61 62 // True if we need to crop the still camera image to get the video frame. 63 bool mNeedCropping; 64 65 // Start location of the cropping rectangle. 66 int32_t mCropRectStartX; 67 int32_t mCropRectStartY; 68 69 // Time between capture of two frames during time lapse recording 70 // Negative value indicates that timelapse is disabled. 71 int64_t mTimeBetweenTimeLapseFrameCaptureUs; 72 73 // Time between two frames in final video (1/frameRate) 74 int64_t mTimeBetweenTimeLapseVideoFramesUs; 75 76 // Real timestamp of the last encoded time lapse frame 77 int64_t mLastTimeLapseFrameRealTimestampUs; 78 79 // Thread id of thread which takes still picture and sleeps in a loop. 80 pthread_t mThreadTimeLapse; 81 82 // Variable set in dataCallbackTimestamp() to help skipCurrentFrame() 83 // to know if current frame needs to be skipped. 84 bool mSkipCurrentFrame; 85 86 // True if camera is in preview mode and ready for takePicture(). 87 bool mCameraIdle; 88 89 CameraSourceTimeLapse(const sp<Camera> &camera, 90 bool useStillCameraForTimeLapse, 91 int64_t timeBetweenTimeLapseFrameCaptureUs, 92 int32_t width, int32_t height, 93 int32_t videoFrameRate); 94 95 // For still camera case starts a thread which calls camera's takePicture() 96 // in a loop. For video camera case, just starts the camera's video recording. 97 virtual void startCameraRecording(); 98 99 // For still camera case joins the thread created in startCameraRecording(). 100 // For video camera case, just stops the camera's video recording. 101 virtual void stopCameraRecording(); 102 103 // For still camera case don't need to do anything as memory is locally 104 // allocated with refcounting. 105 // For video camera case just tell the camera to release the frame. 106 virtual void releaseRecordingFrame(const sp<IMemory>& frame); 107 108 // mSkipCurrentFrame is set to true in dataCallbackTimestamp() if the current 109 // frame needs to be skipped and this function just returns the value of mSkipCurrentFrame. 110 virtual bool skipCurrentFrame(int64_t timestampUs); 111 112 // Handles the callback to handle raw frame data from the still camera. 113 // Creates a copy of the frame data as the camera can reuse the frame memory 114 // once this callback returns. The function also sets a new timstamp corresponding 115 // to one frame time ahead of the last encoded frame's time stamp. It then 116 // calls dataCallbackTimestamp() of the base class with the copied data and the 117 // modified timestamp, which will think that it recieved the frame from a video 118 // camera and proceed as usual. 119 virtual void dataCallback(int32_t msgType, const sp<IMemory> &data); 120 121 // In the video camera case calls skipFrameAndModifyTimeStamp() to modify 122 // timestamp and set mSkipCurrentFrame. 123 // Then it calls the base CameraSource::dataCallbackTimestamp() 124 virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType, 125 const sp<IMemory> &data); 126 127 // The still camera may not support the demanded video width and height. 128 // We look for the supported picture sizes from the still camera and 129 // choose the smallest one with either dimensions higher than the corresponding 130 // video dimensions. The still picture will be cropped to get the video frame. 131 // The function returns true if the camera supports picture sizes greater than 132 // or equal to the passed in width and height, and false otherwise. 133 bool setPictureSizeToClosestSupported(int32_t width, int32_t height); 134 135 // Computes the offset of the rectangle from where to start cropping the 136 // still image into the video frame. We choose the center of the image to be 137 // cropped. The offset is stored in (mCropRectStartX, mCropRectStartY). 138 bool computeCropRectangleOffset(); 139 140 // Crops the source data into a smaller image starting at 141 // (mCropRectStartX, mCropRectStartY) and of the size of the video frame. 142 // The data is returned into a newly allocated IMemory. 143 sp<IMemory> cropYUVImage(const sp<IMemory> &source_data); 144 145 // When video camera is used for time lapse capture, returns true 146 // until enough time has passed for the next time lapse frame. When 147 // the frame needs to be encoded, it returns false and also modifies 148 // the time stamp to be one frame time ahead of the last encoded 149 // frame's time stamp. 150 bool skipFrameAndModifyTimeStamp(int64_t *timestampUs); 151 152 // Wrapper to enter threadTimeLapseEntry() 153 static void *ThreadTimeLapseWrapper(void *me); 154 155 // Runs a loop which sleeps until a still picture is required 156 // and then calls mCamera->takePicture() to take the still picture. 157 // Used only in the case mUseStillCameraForTimeLapse = true. 158 void threadTimeLapseEntry(); 159 160 // Wrapper to enter threadStartPreview() 161 static void *ThreadStartPreviewWrapper(void *me); 162 163 // Starts the camera's preview. 164 void threadStartPreview(); 165 166 // Starts thread ThreadStartPreviewWrapper() for restarting preview. 167 // Needs to be done in a thread so that dataCallback() which calls this function 168 // can return, and the camera can know that takePicture() is done. 169 void restartPreview(); 170 171 // Creates a copy of source_data into a new memory of final type MemoryBase. 172 sp<IMemory> createIMemoryCopy(const sp<IMemory> &source_data); 173 174 CameraSourceTimeLapse(const CameraSourceTimeLapse &); 175 CameraSourceTimeLapse &operator=(const CameraSourceTimeLapse &); 176}; 177 178} // namespace android 179 180#endif // CAMERA_SOURCE_TIME_LAPSE_H_ 181