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