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 27 namespace android { 28 29 class ICamera; 30 class IMemory; 31 class Camera; 32 33 class CameraSourceTimeLapse : public CameraSource { 34 public: 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 bool storeMetaDataInVideoBuffers = true); 46 47 virtual ~CameraSourceTimeLapse(); 48 49 // If the frame capture interval is large, read will block for a long time. 50 // Due to the way the mediaRecorder framework works, a stop() call from 51 // mediaRecorder waits until the read returns, causing a long wait for 52 // stop() to return. To avoid this, we can make read() return a copy of the 53 // last read frame with the same time stamp frequently. This keeps the 54 // read() call from blocking too long. Calling this function quickly 55 // captures another frame, keeps its copy, and enables this mode of read() 56 // returning quickly. 57 void startQuickReadReturns(); 58 59 private: 60 // size of the encoded video. 61 int32_t mVideoWidth; 62 int32_t mVideoHeight; 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 const String16& clientName, 116 uid_t clientUid, 117 Size videoSize, 118 int32_t videoFrameRate, 119 const sp<IGraphicBufferProducer>& surface, 120 int64_t timeBetweenTimeLapseFrameCaptureUs, 121 bool storeMetaDataInVideoBuffers = true); 122 123 // Wrapper over CameraSource::signalBufferReturned() to implement quick stop. 124 // It only handles the case when mLastReadBufferCopy is signalled. Otherwise 125 // it calls the base class' function. 126 virtual void signalBufferReturned(MediaBuffer* buffer); 127 128 // Wrapper over CameraSource::read() to implement quick stop. 129 virtual status_t read(MediaBuffer **buffer, const ReadOptions *options = NULL); 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