165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra/* 265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra * Copyright (C) 2010 The Android Open Source Project 365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra * 465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra * Licensed under the Apache License, Version 2.0 (the "License"); 565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra * you may not use this file except in compliance with the License. 665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra * You may obtain a copy of the License at 765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra * 865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra * http://www.apache.org/licenses/LICENSE-2.0 965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra * 1065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra * Unless required by applicable law or agreed to in writing, software 1165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra * distributed under the License is distributed on an "AS IS" BASIS, 1265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra * See the License for the specific language governing permissions and 1465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra * limitations under the License. 1565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra */ 1665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 1765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra#ifndef CAMERA_SOURCE_TIME_LAPSE_H_ 1865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 1965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra#define CAMERA_SOURCE_TIME_LAPSE_H_ 2065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 2165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra#include <pthread.h> 2265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 2365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra#include <utils/RefBase.h> 2465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra#include <utils/threads.h> 2565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 2665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatranamespace android { 2765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 2865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatraclass ICamera; 2965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatraclass IMemory; 3065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatraclass Camera; 3165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 3265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatraclass CameraSourceTimeLapse : public CameraSource { 3365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatrapublic: 3454ff19ac69ace7c05ea90d225e26dab3b133f487James Dong static CameraSourceTimeLapse *CreateFromCamera( 3554ff19ac69ace7c05ea90d225e26dab3b133f487James Dong const sp<ICamera> &camera, 364ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li const sp<ICameraRecordingProxy> &proxy, 3754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t cameraId, 3854ff19ac69ace7c05ea90d225e26dab3b133f487James Dong Size videoSize, 3954ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t videoFrameRate, 4054ff19ac69ace7c05ea90d225e26dab3b133f487James Dong const sp<Surface>& surface, 4154ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int64_t timeBetweenTimeLapseFrameCaptureUs); 4265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 4365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra virtual ~CameraSourceTimeLapse(); 4465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 4578eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // If the frame capture interval is large, read will block for a long time. 4678eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Due to the way the mediaRecorder framework works, a stop() call from 4778eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // mediaRecorder waits until the read returns, causing a long wait for 4878eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // stop() to return. To avoid this, we can make read() return a copy of the 4978eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // last read frame with the same time stamp frequently. This keeps the 5078eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // read() call from blocking too long. Calling this function quickly 5178eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // captures another frame, keeps its copy, and enables this mode of read() 5278eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // returning quickly. 5378eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra void startQuickReadReturns(); 5478eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 5565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatraprivate: 56c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra // size of the encoded video. 57c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra int32_t mVideoWidth; 58c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra int32_t mVideoHeight; 59c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra 6065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Time between two frames in final video (1/frameRate) 6165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra int64_t mTimeBetweenTimeLapseVideoFramesUs; 6265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 6365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Real timestamp of the last encoded time lapse frame 6465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra int64_t mLastTimeLapseFrameRealTimestampUs; 6565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 6665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Variable set in dataCallbackTimestamp() to help skipCurrentFrame() 6765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // to know if current frame needs to be skipped. 6865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra bool mSkipCurrentFrame; 6965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 7078eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Lock for accessing mCameraIdle 7178eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra Mutex mCameraIdleLock; 7278eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 7378eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Condition variable to wait on if camera is is not yet idle. Once the 7478eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // camera gets idle, this variable will be signalled. 7578eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra Condition mCameraIdleCondition; 7678eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 770aacf105eea098a0b47761a4a9a5c4d820611f2dNipun Kwatra // True if camera is in preview mode and ready for takePicture(). 7878eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // False after a call to takePicture() but before the final compressed 7978eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // data callback has been called and preview has been restarted. 8078eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra volatile bool mCameraIdle; 8178eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 8278eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // True if stop() is waiting for camera to get idle, i.e. for the last 8378eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // takePicture() to complete. This is needed so that dataCallbackTimestamp() 8478eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // can return immediately. 8578eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra volatile bool mStopWaitingForIdleCamera; 8678eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 8778eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Lock for accessing quick stop variables. 8878eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra Mutex mQuickStopLock; 8978eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 9078eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // mQuickStop is set to true if we use quick read() returns, otherwise it is set 9178eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // to false. Once in this mode read() return a copy of the last read frame 9278eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // with the same time stamp. See startQuickReadReturns(). 9378eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra volatile bool mQuickStop; 9478eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 9578eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Forces the next frame passed to dataCallbackTimestamp() to be read 9678eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // as a time lapse frame. Used by startQuickReadReturns() so that the next 9778eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // frame wakes up any blocking read. 9878eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra volatile bool mForceRead; 9978eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 10078eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Stores a copy of the MediaBuffer read in the last read() call after 10178eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // mQuickStop was true. 10278eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra MediaBuffer* mLastReadBufferCopy; 10378eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 10478eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Status code for last read. 10578eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra status_t mLastReadStatus; 1060aacf105eea098a0b47761a4a9a5c4d820611f2dNipun Kwatra 10754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong CameraSourceTimeLapse( 10854ff19ac69ace7c05ea90d225e26dab3b133f487James Dong const sp<ICamera> &camera, 1094ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li const sp<ICameraRecordingProxy> &proxy, 11054ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t cameraId, 11154ff19ac69ace7c05ea90d225e26dab3b133f487James Dong Size videoSize, 11254ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t videoFrameRate, 11354ff19ac69ace7c05ea90d225e26dab3b133f487James Dong const sp<Surface>& surface, 11454ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int64_t timeBetweenTimeLapseFrameCaptureUs); 11565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 11678eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Wrapper over CameraSource::signalBufferReturned() to implement quick stop. 11778eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // It only handles the case when mLastReadBufferCopy is signalled. Otherwise 11878eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // it calls the base class' function. 11978eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra virtual void signalBufferReturned(MediaBuffer* buffer); 12078eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 12178eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Wrapper over CameraSource::read() to implement quick stop. 12278eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra virtual status_t read(MediaBuffer **buffer, const ReadOptions *options = NULL); 12378eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 12465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // mSkipCurrentFrame is set to true in dataCallbackTimestamp() if the current 12565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // frame needs to be skipped and this function just returns the value of mSkipCurrentFrame. 12665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra virtual bool skipCurrentFrame(int64_t timestampUs); 12765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 12865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // In the video camera case calls skipFrameAndModifyTimeStamp() to modify 12965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // timestamp and set mSkipCurrentFrame. 13065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Then it calls the base CameraSource::dataCallbackTimestamp() 13165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType, 13265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra const sp<IMemory> &data); 13365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 13478eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Convenience function to fill mLastReadBufferCopy from the just read 13578eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // buffer. 13678eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra void fillLastReadBufferCopy(MediaBuffer& sourceBuffer); 13778eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 13828934a90e168291f6c77c56e8a05f272e5151bbdJames Dong // If the passed in size (width x height) is a supported video/preview size, 13928934a90e168291f6c77c56e8a05f272e5151bbdJames Dong // the function sets the camera's video/preview size to it and returns true. 140155e833a7a5fc3e193691324cf9326da1bc3289aNipun Kwatra // Otherwise returns false. 14128934a90e168291f6c77c56e8a05f272e5151bbdJames Dong bool trySettingVideoSize(int32_t width, int32_t height); 142155e833a7a5fc3e193691324cf9326da1bc3289aNipun Kwatra 14365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // When video camera is used for time lapse capture, returns true 14465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // until enough time has passed for the next time lapse frame. When 14565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // the frame needs to be encoded, it returns false and also modifies 14665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // the time stamp to be one frame time ahead of the last encoded 14765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // frame's time stamp. 14865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra bool skipFrameAndModifyTimeStamp(int64_t *timestampUs); 14965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 15065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Wrapper to enter threadTimeLapseEntry() 15165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra static void *ThreadTimeLapseWrapper(void *me); 15265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 15365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Creates a copy of source_data into a new memory of final type MemoryBase. 15465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra sp<IMemory> createIMemoryCopy(const sp<IMemory> &source_data); 15565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 15665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra CameraSourceTimeLapse(const CameraSourceTimeLapse &); 15765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra CameraSourceTimeLapse &operator=(const CameraSourceTimeLapse &); 15865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra}; 15965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 16065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra} // namespace android 16165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 16265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra#endif // CAMERA_SOURCE_TIME_LAPSE_H_ 163