CameraSourceTimeLapse.h revision 3cecf640c4daf2df616b278bd9986018c8182908
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 // size of the encoded video. 57 int32_t mVideoWidth; 58 int32_t mVideoHeight; 59 60 // Time between capture of two frames during time lapse recording 61 // Negative value indicates that timelapse is disabled. 62 int64_t mTimeBetweenTimeLapseFrameCaptureUs; 63 64 // Time between two frames in final video (1/frameRate) 65 int64_t mTimeBetweenTimeLapseVideoFramesUs; 66 67 // Real timestamp of the last encoded time lapse frame 68 int64_t mLastTimeLapseFrameRealTimestampUs; 69 70 // Variable set in dataCallbackTimestamp() to help skipCurrentFrame() 71 // to know if current frame needs to be skipped. 72 bool mSkipCurrentFrame; 73 74 // Lock for accessing mCameraIdle 75 Mutex mCameraIdleLock; 76 77 // Condition variable to wait on if camera is is not yet idle. Once the 78 // camera gets idle, this variable will be signalled. 79 Condition mCameraIdleCondition; 80 81 // True if camera is in preview mode and ready for takePicture(). 82 // False after a call to takePicture() but before the final compressed 83 // data callback has been called and preview has been restarted. 84 volatile bool mCameraIdle; 85 86 // True if stop() is waiting for camera to get idle, i.e. for the last 87 // takePicture() to complete. This is needed so that dataCallbackTimestamp() 88 // can return immediately. 89 volatile bool mStopWaitingForIdleCamera; 90 91 // Lock for accessing quick stop variables. 92 Mutex mQuickStopLock; 93 94 // mQuickStop is set to true if we use quick read() returns, otherwise it is set 95 // to false. Once in this mode read() return a copy of the last read frame 96 // with the same time stamp. See startQuickReadReturns(). 97 volatile bool mQuickStop; 98 99 // Forces the next frame passed to dataCallbackTimestamp() to be read 100 // as a time lapse frame. Used by startQuickReadReturns() so that the next 101 // frame wakes up any blocking read. 102 volatile bool mForceRead; 103 104 // Stores a copy of the MediaBuffer read in the last read() call after 105 // mQuickStop was true. 106 MediaBuffer* mLastReadBufferCopy; 107 108 // Status code for last read. 109 status_t mLastReadStatus; 110 111 CameraSourceTimeLapse( 112 const sp<ICamera> &camera, 113 const sp<ICameraRecordingProxy> &proxy, 114 int32_t cameraId, 115 Size videoSize, 116 int32_t videoFrameRate, 117 const sp<Surface>& surface, 118 int64_t timeBetweenTimeLapseFrameCaptureUs); 119 120 // Wrapper over CameraSource::signalBufferReturned() to implement quick stop. 121 // It only handles the case when mLastReadBufferCopy is signalled. Otherwise 122 // it calls the base class' function. 123 virtual void signalBufferReturned(MediaBuffer* buffer); 124 125 // Wrapper over CameraSource::read() to implement quick stop. 126 virtual status_t read(MediaBuffer **buffer, const ReadOptions *options = NULL); 127 128 // For video camera case, just stops the camera's video recording. 129 virtual void stopCameraRecording(); 130 131 // mSkipCurrentFrame is set to true in dataCallbackTimestamp() if the current 132 // frame needs to be skipped and this function just returns the value of mSkipCurrentFrame. 133 virtual bool skipCurrentFrame(int64_t timestampUs); 134 135 // In the video camera case calls skipFrameAndModifyTimeStamp() to modify 136 // timestamp and set mSkipCurrentFrame. 137 // Then it calls the base CameraSource::dataCallbackTimestamp() 138 virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType, 139 const sp<IMemory> &data); 140 141 // Convenience function to fill mLastReadBufferCopy from the just read 142 // buffer. 143 void fillLastReadBufferCopy(MediaBuffer& sourceBuffer); 144 145 // If the passed in size (width x height) is a supported video/preview size, 146 // the function sets the camera's video/preview size to it and returns true. 147 // Otherwise returns false. 148 bool trySettingVideoSize(int32_t width, int32_t height); 149 150 // When video camera is used for time lapse capture, returns true 151 // until enough time has passed for the next time lapse frame. When 152 // the frame needs to be encoded, it returns false and also modifies 153 // the time stamp to be one frame time ahead of the last encoded 154 // frame's time stamp. 155 bool skipFrameAndModifyTimeStamp(int64_t *timestampUs); 156 157 // Wrapper to enter threadTimeLapseEntry() 158 static void *ThreadTimeLapseWrapper(void *me); 159 160 // Creates a copy of source_data into a new memory of final type MemoryBase. 161 sp<IMemory> createIMemoryCopy(const sp<IMemory> &source_data); 162 163 CameraSourceTimeLapse(const CameraSourceTimeLapse &); 164 CameraSourceTimeLapse &operator=(const CameraSourceTimeLapse &); 165}; 166 167} // namespace android 168 169#endif // CAMERA_SOURCE_TIME_LAPSE_H_ 170