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