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