CameraSourceTimeLapse.h revision 98a668f6ea51e4d894d2ebb61a0e18287fb14008
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> 25ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala#include <utils/String16.h> 2665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 2765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatranamespace android { 2865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 2965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatraclass ICamera; 3065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatraclass IMemory; 3165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatraclass Camera; 3265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 3365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatraclass CameraSourceTimeLapse : public CameraSource { 3465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatrapublic: 3554ff19ac69ace7c05ea90d225e26dab3b133f487James Dong static CameraSourceTimeLapse *CreateFromCamera( 3654ff19ac69ace7c05ea90d225e26dab3b133f487James Dong const sp<ICamera> &camera, 374ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li const sp<ICameraRecordingProxy> &proxy, 3854ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t cameraId, 39ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala const String16& clientName, 40ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala uid_t clientUid, 4198a668f6ea51e4d894d2ebb61a0e18287fb14008Chien-Yu Chen pid_t clientPid, 4254ff19ac69ace7c05ea90d225e26dab3b133f487James Dong Size videoSize, 4354ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t videoFrameRate, 4499617adda9bc46c43f511f0940bc735c73de61deMathias Agopian const sp<IGraphicBufferProducer>& surface, 459c7f845780d62ea09469a8dac7117cc01adfea5cLajos Molnar int64_t timeBetweenTimeLapseFrameCaptureUs, 469c7f845780d62ea09469a8dac7117cc01adfea5cLajos Molnar bool storeMetaDataInVideoBuffers = true); 4765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 4865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra virtual ~CameraSourceTimeLapse(); 4965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 5078eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // If the frame capture interval is large, read will block for a long time. 5178eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Due to the way the mediaRecorder framework works, a stop() call from 5278eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // mediaRecorder waits until the read returns, causing a long wait for 5378eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // stop() to return. To avoid this, we can make read() return a copy of the 5478eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // last read frame with the same time stamp frequently. This keeps the 5578eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // read() call from blocking too long. Calling this function quickly 5678eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // captures another frame, keeps its copy, and enables this mode of read() 5778eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // returning quickly. 5878eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra void startQuickReadReturns(); 5978eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 6065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatraprivate: 61c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra // size of the encoded video. 62c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra int32_t mVideoWidth; 63c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra int32_t mVideoHeight; 64c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra 6565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Time between two frames in final video (1/frameRate) 6665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra int64_t mTimeBetweenTimeLapseVideoFramesUs; 6765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 6865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Real timestamp of the last encoded time lapse frame 6965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra int64_t mLastTimeLapseFrameRealTimestampUs; 7065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 7165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Variable set in dataCallbackTimestamp() to help skipCurrentFrame() 7265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // to know if current frame needs to be skipped. 7365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra bool mSkipCurrentFrame; 7465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 7578eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Lock for accessing mCameraIdle 7678eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra Mutex mCameraIdleLock; 7778eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 7878eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Condition variable to wait on if camera is is not yet idle. Once the 7978eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // camera gets idle, this variable will be signalled. 8078eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra Condition mCameraIdleCondition; 8178eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 820aacf105eea098a0b47761a4a9a5c4d820611f2dNipun Kwatra // True if camera is in preview mode and ready for takePicture(). 8378eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // False after a call to takePicture() but before the final compressed 8478eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // data callback has been called and preview has been restarted. 8578eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra volatile bool mCameraIdle; 8678eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 8778eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // True if stop() is waiting for camera to get idle, i.e. for the last 8878eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // takePicture() to complete. This is needed so that dataCallbackTimestamp() 8978eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // can return immediately. 9078eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra volatile bool mStopWaitingForIdleCamera; 9178eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 9278eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Lock for accessing quick stop variables. 9378eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra Mutex mQuickStopLock; 9478eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 9578eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // mQuickStop is set to true if we use quick read() returns, otherwise it is set 9678eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // to false. Once in this mode read() return a copy of the last read frame 9778eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // with the same time stamp. See startQuickReadReturns(). 9878eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra volatile bool mQuickStop; 9978eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 10078eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Forces the next frame passed to dataCallbackTimestamp() to be read 10178eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // as a time lapse frame. Used by startQuickReadReturns() so that the next 10278eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // frame wakes up any blocking read. 10378eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra volatile bool mForceRead; 10478eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 10578eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Stores a copy of the MediaBuffer read in the last read() call after 10678eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // mQuickStop was true. 10778eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra MediaBuffer* mLastReadBufferCopy; 10878eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 10978eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Status code for last read. 11078eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra status_t mLastReadStatus; 1110aacf105eea098a0b47761a4a9a5c4d820611f2dNipun Kwatra 11254ff19ac69ace7c05ea90d225e26dab3b133f487James Dong CameraSourceTimeLapse( 11354ff19ac69ace7c05ea90d225e26dab3b133f487James Dong const sp<ICamera> &camera, 1144ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li const sp<ICameraRecordingProxy> &proxy, 11554ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t cameraId, 116ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala const String16& clientName, 117ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala uid_t clientUid, 11898a668f6ea51e4d894d2ebb61a0e18287fb14008Chien-Yu Chen pid_t clientPid, 11954ff19ac69ace7c05ea90d225e26dab3b133f487James Dong Size videoSize, 12054ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t videoFrameRate, 12199617adda9bc46c43f511f0940bc735c73de61deMathias Agopian const sp<IGraphicBufferProducer>& surface, 1229c7f845780d62ea09469a8dac7117cc01adfea5cLajos Molnar int64_t timeBetweenTimeLapseFrameCaptureUs, 1239c7f845780d62ea09469a8dac7117cc01adfea5cLajos Molnar bool storeMetaDataInVideoBuffers = true); 12465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 12578eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Wrapper over CameraSource::signalBufferReturned() to implement quick stop. 12678eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // It only handles the case when mLastReadBufferCopy is signalled. Otherwise 12778eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // it calls the base class' function. 12878eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra virtual void signalBufferReturned(MediaBuffer* buffer); 12978eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 13078eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Wrapper over CameraSource::read() to implement quick stop. 13178eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra virtual status_t read(MediaBuffer **buffer, const ReadOptions *options = NULL); 13278eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 13365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // mSkipCurrentFrame is set to true in dataCallbackTimestamp() if the current 13465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // frame needs to be skipped and this function just returns the value of mSkipCurrentFrame. 13565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra virtual bool skipCurrentFrame(int64_t timestampUs); 13665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 13765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // In the video camera case calls skipFrameAndModifyTimeStamp() to modify 13865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // timestamp and set mSkipCurrentFrame. 13965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Then it calls the base CameraSource::dataCallbackTimestamp() 14065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType, 14165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra const sp<IMemory> &data); 14265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 14378eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Convenience function to fill mLastReadBufferCopy from the just read 14478eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // buffer. 14578eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra void fillLastReadBufferCopy(MediaBuffer& sourceBuffer); 14678eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 14728934a90e168291f6c77c56e8a05f272e5151bbdJames Dong // If the passed in size (width x height) is a supported video/preview size, 14828934a90e168291f6c77c56e8a05f272e5151bbdJames Dong // the function sets the camera's video/preview size to it and returns true. 149155e833a7a5fc3e193691324cf9326da1bc3289aNipun Kwatra // Otherwise returns false. 15028934a90e168291f6c77c56e8a05f272e5151bbdJames Dong bool trySettingVideoSize(int32_t width, int32_t height); 151155e833a7a5fc3e193691324cf9326da1bc3289aNipun Kwatra 15265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // When video camera is used for time lapse capture, returns true 15365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // until enough time has passed for the next time lapse frame. When 15465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // the frame needs to be encoded, it returns false and also modifies 15565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // the time stamp to be one frame time ahead of the last encoded 15665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // frame's time stamp. 15765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra bool skipFrameAndModifyTimeStamp(int64_t *timestampUs); 15865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 15965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Wrapper to enter threadTimeLapseEntry() 16065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra static void *ThreadTimeLapseWrapper(void *me); 16165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 16265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Creates a copy of source_data into a new memory of final type MemoryBase. 16365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra sp<IMemory> createIMemoryCopy(const sp<IMemory> &source_data); 16465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 16565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra CameraSourceTimeLapse(const CameraSourceTimeLapse &); 16665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra CameraSourceTimeLapse &operator=(const CameraSourceTimeLapse &); 16765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra}; 16865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 16965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra} // namespace android 17065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 17165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra#endif // CAMERA_SOURCE_TIME_LAPSE_H_ 172