CameraSourceTimeLapse.h revision 99617adda9bc46c43f511f0940bc735c73de61de
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, 4154ff19ac69ace7c05ea90d225e26dab3b133f487James Dong Size videoSize, 4254ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t videoFrameRate, 4399617adda9bc46c43f511f0940bc735c73de61deMathias Agopian const sp<IGraphicBufferProducer>& surface, 4454ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int64_t timeBetweenTimeLapseFrameCaptureUs); 4565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 4665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra virtual ~CameraSourceTimeLapse(); 4765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 4878eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // If the frame capture interval is large, read will block for a long time. 4978eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Due to the way the mediaRecorder framework works, a stop() call from 5078eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // mediaRecorder waits until the read returns, causing a long wait for 5178eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // stop() to return. To avoid this, we can make read() return a copy of the 5278eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // last read frame with the same time stamp frequently. This keeps the 5378eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // read() call from blocking too long. Calling this function quickly 5478eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // captures another frame, keeps its copy, and enables this mode of read() 5578eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // returning quickly. 5678eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra void startQuickReadReturns(); 5778eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 5865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatraprivate: 59c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra // size of the encoded video. 60c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra int32_t mVideoWidth; 61c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra int32_t mVideoHeight; 62c4e47d1e81c4e4403663cb911e98dbf3ada9942cNipun Kwatra 6365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Time between two frames in final video (1/frameRate) 6465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra int64_t mTimeBetweenTimeLapseVideoFramesUs; 6565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 6665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Real timestamp of the last encoded time lapse frame 6765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra int64_t mLastTimeLapseFrameRealTimestampUs; 6865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 6965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Variable set in dataCallbackTimestamp() to help skipCurrentFrame() 7065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // to know if current frame needs to be skipped. 7165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra bool mSkipCurrentFrame; 7265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 7378eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Lock for accessing mCameraIdle 7478eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra Mutex mCameraIdleLock; 7578eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 7678eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Condition variable to wait on if camera is is not yet idle. Once the 7778eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // camera gets idle, this variable will be signalled. 7878eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra Condition mCameraIdleCondition; 7978eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 800aacf105eea098a0b47761a4a9a5c4d820611f2dNipun Kwatra // True if camera is in preview mode and ready for takePicture(). 8178eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // False after a call to takePicture() but before the final compressed 8278eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // data callback has been called and preview has been restarted. 8378eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra volatile bool mCameraIdle; 8478eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 8578eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // True if stop() is waiting for camera to get idle, i.e. for the last 8678eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // takePicture() to complete. This is needed so that dataCallbackTimestamp() 8778eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // can return immediately. 8878eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra volatile bool mStopWaitingForIdleCamera; 8978eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 9078eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Lock for accessing quick stop variables. 9178eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra Mutex mQuickStopLock; 9278eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 9378eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // mQuickStop is set to true if we use quick read() returns, otherwise it is set 9478eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // to false. Once in this mode read() return a copy of the last read frame 9578eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // with the same time stamp. See startQuickReadReturns(). 9678eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra volatile bool mQuickStop; 9778eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 9878eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Forces the next frame passed to dataCallbackTimestamp() to be read 9978eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // as a time lapse frame. Used by startQuickReadReturns() so that the next 10078eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // frame wakes up any blocking read. 10178eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra volatile bool mForceRead; 10278eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 10378eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Stores a copy of the MediaBuffer read in the last read() call after 10478eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // mQuickStop was true. 10578eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra MediaBuffer* mLastReadBufferCopy; 10678eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 10778eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Status code for last read. 10878eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra status_t mLastReadStatus; 1090aacf105eea098a0b47761a4a9a5c4d820611f2dNipun Kwatra 11054ff19ac69ace7c05ea90d225e26dab3b133f487James Dong CameraSourceTimeLapse( 11154ff19ac69ace7c05ea90d225e26dab3b133f487James Dong const sp<ICamera> &camera, 1124ca2c7c913f8bd4ada13aca56d36045d42d1e00fWu-cheng Li const sp<ICameraRecordingProxy> &proxy, 11354ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t cameraId, 114ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala const String16& clientName, 115ceb388d6c03c38b96dc41c0ea4804b749aa077c4Eino-Ville Talvala uid_t clientUid, 11654ff19ac69ace7c05ea90d225e26dab3b133f487James Dong Size videoSize, 11754ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int32_t videoFrameRate, 11899617adda9bc46c43f511f0940bc735c73de61deMathias Agopian const sp<IGraphicBufferProducer>& surface, 11954ff19ac69ace7c05ea90d225e26dab3b133f487James Dong int64_t timeBetweenTimeLapseFrameCaptureUs); 12065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 12178eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Wrapper over CameraSource::signalBufferReturned() to implement quick stop. 12278eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // It only handles the case when mLastReadBufferCopy is signalled. Otherwise 12378eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // it calls the base class' function. 12478eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra virtual void signalBufferReturned(MediaBuffer* buffer); 12578eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 12678eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Wrapper over CameraSource::read() to implement quick stop. 12778eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra virtual status_t read(MediaBuffer **buffer, const ReadOptions *options = NULL); 12878eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 12965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // mSkipCurrentFrame is set to true in dataCallbackTimestamp() if the current 13065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // frame needs to be skipped and this function just returns the value of mSkipCurrentFrame. 13165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra virtual bool skipCurrentFrame(int64_t timestampUs); 13265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 13365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // In the video camera case calls skipFrameAndModifyTimeStamp() to modify 13465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // timestamp and set mSkipCurrentFrame. 13565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Then it calls the base CameraSource::dataCallbackTimestamp() 13665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra virtual void dataCallbackTimestamp(int64_t timestampUs, int32_t msgType, 13765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra const sp<IMemory> &data); 13865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 13978eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // Convenience function to fill mLastReadBufferCopy from the just read 14078eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra // buffer. 14178eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra void fillLastReadBufferCopy(MediaBuffer& sourceBuffer); 14278eff720c86eb6d4e3d45a144df60b2ca464d2d4Nipun Kwatra 14328934a90e168291f6c77c56e8a05f272e5151bbdJames Dong // If the passed in size (width x height) is a supported video/preview size, 14428934a90e168291f6c77c56e8a05f272e5151bbdJames Dong // the function sets the camera's video/preview size to it and returns true. 145155e833a7a5fc3e193691324cf9326da1bc3289aNipun Kwatra // Otherwise returns false. 14628934a90e168291f6c77c56e8a05f272e5151bbdJames Dong bool trySettingVideoSize(int32_t width, int32_t height); 147155e833a7a5fc3e193691324cf9326da1bc3289aNipun Kwatra 14865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // When video camera is used for time lapse capture, returns true 14965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // until enough time has passed for the next time lapse frame. When 15065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // the frame needs to be encoded, it returns false and also modifies 15165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // the time stamp to be one frame time ahead of the last encoded 15265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // frame's time stamp. 15365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra bool skipFrameAndModifyTimeStamp(int64_t *timestampUs); 15465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 15565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Wrapper to enter threadTimeLapseEntry() 15665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra static void *ThreadTimeLapseWrapper(void *me); 15765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 15865e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra // Creates a copy of source_data into a new memory of final type MemoryBase. 15965e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra sp<IMemory> createIMemoryCopy(const sp<IMemory> &source_data); 16065e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 16165e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra CameraSourceTimeLapse(const CameraSourceTimeLapse &); 16265e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra CameraSourceTimeLapse &operator=(const CameraSourceTimeLapse &); 16365e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra}; 16465e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 16565e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra} // namespace android 16665e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra 16765e7e6facda89927cb26594b3b65ae81b3235ebcNipun Kwatra#endif // CAMERA_SOURCE_TIME_LAPSE_H_ 168