You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
166 lines
5.1 KiB
166 lines
5.1 KiB
#include "lapsedVideoRecorder.h"
|
|
|
|
#define LOG( X ) m_pLog->Log( "LVR", this, X )
|
|
#define LOG_ERROR( X ) m_pLog->Log( "LVR", this, _LOG_ERROR, X )
|
|
|
|
lapsedVideoRecorder::lapsedVideoRecorder() :basicMediaDestination(), basicEventHandler(){
|
|
m_nTimeLapse = 0;
|
|
m_nFrameLapse = 0;
|
|
m_bLapseByTime = true;
|
|
m_nCurrentFrame = 0;
|
|
m_bOutputIsStarted = false;
|
|
m_pCurrentOutput = NULL;
|
|
m_pRealOutput = NULL;
|
|
m_pLog = basicLogCollection::getInstance();
|
|
};
|
|
|
|
lapsedVideoRecorder::~lapsedVideoRecorder(){
|
|
if( m_pRealOutput != NULL ) {
|
|
//m_pRealOutput->stopRecord();
|
|
delete( m_pRealOutput );
|
|
};
|
|
if( m_pCurrentOutput!=NULL ) {
|
|
free( m_pCurrentOutput );
|
|
};
|
|
};
|
|
|
|
|
|
void lapsedVideoRecorder::pushFrame(cv::Mat *Frame){
|
|
if( Frame != NULL ){
|
|
FRAME_QUEUE_ITEM *tItem;
|
|
cv::Mat *tMat;
|
|
tMat = new cv::Mat(Frame->rows, Frame->cols, Frame->type());
|
|
if( tMat != NULL ) {
|
|
memcpy( tMat->data, Frame->data, Frame->total() * Frame->elemSize() );
|
|
if( m_nFrameLapse > 0 || m_nTimeLapse > 0 ){
|
|
// Must have queue
|
|
tItem = (FRAME_QUEUE_ITEM *)malloc(sizeof(FRAME_QUEUE_ITEM));
|
|
if( tItem != NULL ){
|
|
long long tCTime;
|
|
tCTime = getMilliseconds();
|
|
tItem->TS = tCTime;
|
|
tItem->FRAME = tMat;
|
|
m_vTimeLapse.push(tItem);
|
|
if( m_bLapseByTime ) {
|
|
// Time
|
|
if( ( m_vTimeLapse.front()->TS + m_nTimeLapse ) < tCTime ){
|
|
if (m_vTimeLapse.front()->FRAME != NULL){
|
|
if( m_bOutputIsStarted ) {
|
|
// Encode Frame
|
|
m_pRealOutput->pushFrame( m_vTimeLapse.front()->FRAME );
|
|
m_nCurrentFrame++;
|
|
};
|
|
// Free mem
|
|
delete(m_vTimeLapse.front()->FRAME);
|
|
}
|
|
// free mem
|
|
free(m_vTimeLapse.front());
|
|
m_vTimeLapse.pop();
|
|
};
|
|
} else {
|
|
// Frame
|
|
if( m_vTimeLapse.size() > m_nFrameLapse ){
|
|
if( m_vTimeLapse.front()->FRAME != NULL ){
|
|
if( m_bOutputIsStarted ) {
|
|
m_pRealOutput->pushFrame( m_vTimeLapse.front()->FRAME );
|
|
// Check to finish
|
|
m_mProtecor.lock();
|
|
if( m_nFrameToSave ){
|
|
m_nFrameToSave--;
|
|
} else {
|
|
// Finish up record
|
|
m_pRealOutput->stopRecord();
|
|
m_bOutputIsStarted = false;
|
|
};
|
|
m_nCurrentFrame++;
|
|
m_mProtecor.unlock();
|
|
};
|
|
// Free mem
|
|
delete(m_vTimeLapse.front()->FRAME);
|
|
};
|
|
// Free mem
|
|
free(m_vTimeLapse.front());
|
|
m_vTimeLapse.pop();
|
|
};
|
|
};
|
|
} else {
|
|
// Handle allocation error - cleanup
|
|
LOG_ERROR("Cannot Alocate Queue Element!");
|
|
delete( tMat );
|
|
};
|
|
} else {
|
|
// Immidiate encode output (No TimeShift)
|
|
LOG("NO Shift Defined!");
|
|
};
|
|
};
|
|
};
|
|
};
|
|
|
|
void lapsedVideoRecorder::setTimeLapse( uint32_t milliseconds ){
|
|
m_nTimeLapse = milliseconds;
|
|
m_bLapseByTime = true;
|
|
};
|
|
|
|
void lapsedVideoRecorder::setFrameLapse( uint32_t frames ){
|
|
m_nFrameLapse = frames;
|
|
m_bLapseByTime = false;
|
|
};
|
|
|
|
bool lapsedVideoRecorder::continueOutput( void ){
|
|
if( m_pCurrentOutput == NULL ) return false;
|
|
if( m_pRealOutput == NULL ) return false;
|
|
if( !m_bOutputIsStarted ) return false;
|
|
m_mProtecor.lock();
|
|
if( m_bLapseByTime ) {
|
|
m_nTimeToSave = m_nTimeLapse << 1;
|
|
m_nFrameToSave = 0;
|
|
} else {
|
|
m_nTimeToSave = 0;
|
|
m_nFrameToSave = m_nFrameLapse << 1;
|
|
};
|
|
m_mProtecor.unlock();
|
|
return true;
|
|
};
|
|
|
|
bool lapsedVideoRecorder::beginOutput( CSTR FName ){
|
|
if( m_pRealOutput == NULL ){
|
|
LOG("Create AVLib Dest!");
|
|
m_pRealOutput = new videoAVLIBDestination();
|
|
if( m_pRealOutput == NULL ) return false;
|
|
LOG("OK!");
|
|
};
|
|
if( m_pCurrentOutput != NULL ){
|
|
free( m_pCurrentOutput );
|
|
};
|
|
m_pCurrentOutput = (CSTR) malloc( strlen(FName)+1 );
|
|
if( m_pCurrentOutput == NULL ) return false;
|
|
memcpy( m_pCurrentOutput, FName, strlen(FName)+1 );
|
|
LOG("StartRec AVLib Dest!");
|
|
m_pRealOutput->startRecord( m_pCurrentOutput );
|
|
LOG("Record is Started");
|
|
if( m_bLapseByTime ) {
|
|
m_nTimeToSave = m_nTimeLapse << 1;
|
|
m_nFrameToSave = 0;
|
|
} else {
|
|
m_nTimeToSave = 0;
|
|
m_nFrameToSave = m_nFrameLapse << 1;
|
|
};
|
|
m_bOutputIsStarted = true;
|
|
return m_bOutputIsStarted;
|
|
};
|
|
|
|
bool lapsedVideoRecorder::finishOutput( void ){
|
|
if( m_pCurrentOutput == NULL ) return true;
|
|
if( m_pRealOutput != NULL ) m_pRealOutput->stopRecord();
|
|
|
|
return true;
|
|
};
|
|
|
|
void lapsedVideoRecorder::dispatchEvent(basicEvent *Evt){
|
|
if (Evt == NULL)return;
|
|
|
|
};
|
|
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|