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