@ -1359,20 +1359,20 @@ servaVideoComposer::servaVideoComposer( uint64_t session_id ){
m_pDBC = servaMySQLDBC : : getInstance ( ) ;
m_uLModuleID = 0 ;
m_uRModuleID = 0 ;
m_uComposerRecordId = 0 ;
m_nSessionID = session_id ;
if ( m_pDBC ) {
DBC_MEDIA_COMPOSER_DATA * tData = m_pDBC - > getVideoComposerData ( ) ;
if ( tData ) {
LOG ( " Settings OK " ) ;
memcpy ( & m_sComposerSettings , tData , sizeof ( DBC_MEDIA_COMPOSER_DATA ) ) ;
free ( tData ) ;
getUniqID_UUID ( m_cUUID ) ;
} else {
LOG_ERROR ( " Settings Load Failure! " ) ;
memset ( & m_sComposerSettings , 0 , sizeof ( DBC_MEDIA_COMPOSER_DATA ) ) ;
m_bComposerStatus = VIDEO_COMPOSER_ERR ;
} ;
} ;
LOG ( " Con Done! " ) ;
} ;
servaVideoComposer : : ~ servaVideoComposer ( ) {
@ -1380,19 +1380,16 @@ servaVideoComposer::~servaVideoComposer(){
} ;
bool servaVideoComposer : : compose ( ) {
// If settings are OK
LOG ( " Compose " ) ;
if ( m_bComposerStatus ! = VIDEO_COMPOSER_ERR ) {
if ( m_sComposerSettings . out_width & & m_sComposerSettings . out_height ) {
if ( m_pDBC - > checkMediaComposerItem ( m_nSessionID ) ) return false ; // Already Composed that session
if ( m_bComposerStatus ! = VIDEO_COMPOSER_ERR ) {
if ( m_sComposerSettings . out_width & & m_sComposerSettings . out_height ) {
// create supplimentary data
LOG ( " Decode No Frame Image " ) ;
cv : : Mat rawBufferMat ( 1 , no_frame_data_size , CV_8UC1 , const_cast < uchar * > ( no_frame_png_1600x900 ) ) ;
//m_oNoVideoMat = cv::imdecode( cv::InputArray( no_frame_png_1600x900 ), cv::IMREAD_COLOR );
m_oNoVideoMat = cv : : imdecode ( rawBufferMat , cv : : IMREAD_UNCHANGED ) ;
//Check if need to resize no pic mat
if ( ( m_oNoVideoMat . cols ! = m_sComposerSettings . out_width ) | | ( m_oNoVideoMat . rows ! = m_sComposerSettings . out_height ) ) {
LOG ( " Resize No Frame Image " ) ;
cv : : resize (
m_oNoVideoMat ,
m_oNoVideoMat ,
@ -1401,13 +1398,11 @@ bool servaVideoComposer::compose(){
) ; //cv::INTER_NEAREST cv::INTER_LINEAR cv::INTER_CUBIC cv::INTER_AREA
} ;
//Load Session Data
LOG ( " Get Session Data " ) ;
m_pSessionData = m_pDBC - > getSessionData ( m_nSessionID ) ;
if ( m_pSessionData ) {
servaSessionManager * tSM = servaSessionManager : : getInstance ( ) ;
CSTR tPath = tSM - > getReportDir ( m_pSessionData - > track_id , m_pSessionData - > sts ) ;
if ( tPath ) {
LOG ( " Have Report Dir " ) ;
strncpy ( m_pSessionData - > reportVaultPath , tPath , MAX_PATH ) ;
free ( tPath ) ;
char tName [ MAX_PATH ] ;
@ -1422,7 +1417,6 @@ bool servaVideoComposer::compose(){
free ( m_pSessionData ) ;
m_pSessionData = NULL ;
} else {
LOG ( " Connected to Report " ) ;
DBC_IS_TRACK_DATA * tTD = m_pDBC - > getTrackData ( m_pSessionData - > track_id ) ;
if ( tTD ) {
if ( tTD - > side = = ' U ' ) {
@ -1434,7 +1428,6 @@ bool servaVideoComposer::compose(){
} ;
free ( tTD ) ;
} ;
LOG ( " Loading Videos! " ) ;
if ( loadLeftQueue ( ) ) m_bComposerStatus | = VIDEO_COMPOSER_LOK ;
if ( loadRightQueue ( ) ) m_bComposerStatus | = VIDEO_COMPOSER_ROK ;
} ;
@ -1458,17 +1451,22 @@ bool servaVideoComposer::compose(){
} ;
} ;
if ( m_pSessionData = = NULL ) {
LOG ( " Error Init Composer! " ) ;
LOG_ERROR ( " Error Init Composer! " ) ;
m_bComposerStatus = VIDEO_COMPOSER_ERR ;
} else {
if ( m_bComposerStatus ) { // Have items in Video Queue
# ifdef DEBUG_EXTRA
LOG ( " Have To Compose Videos! " ) ;
if ( init ( ) ) {
# endif
m_uComposerRecordId = m_pDBC - > addMediaComposerItem ( m_nSessionID , m_cUUID ) ;
if ( init ( ) ) {
m_bComposerStatus | = VIDEO_COMPOSER_INIT ;
return true ;
} ;
} else LOG_ERROR ( " Cannot Init Thread! " ) ;
} else {
# ifdef DEBUG_EXTRA
LOG ( " No Need to Compose Videos! " ) ;
# endif
m_bComposerStatus | = VIDEO_COMPOSER_DONE ;
} ;
} ;
@ -1485,16 +1483,21 @@ void servaVideoComposer::finishupCompose(){
uint8_t tChVal = ( ( ( m_bComposerStatus & VIDEO_COMPOSER_LOK ) = = VIDEO_COMPOSER_LOK ) ? VIDEO_COMPOSER_L_DONE : 0 ) |
( ( ( m_bComposerStatus & VIDEO_COMPOSER_ROK ) = = VIDEO_COMPOSER_ROK ) ? VIDEO_COMPOSER_R_DONE : 0 ) ;
if ( ( m_bComposerStatus & tChVal ) ! = tChVal ) waitThreadEnd ( true ) ;
char status = ' L ' ; // Two rightmost bits are zeroes
//update session Data to have new video records instead of the partial videos
if ( ( m_bComposerStatus & VIDEO_COMPOSER_L_DONE ) = = VIDEO_COMPOSER_L_DONE ) {
status = ( char ) ( ( ( uint8_t ) status ) | 0x02 ) ;
} ;
if ( ( m_bComposerStatus & VIDEO_COMPOSER_R_DONE ) = = VIDEO_COMPOSER_R_DONE ) {
status = ( char ) ( ( ( uint8_t ) status ) | 0x01 ) ;
} ;
m_pDBC - > updateMediaComposerItem ( m_uComposerRecordId , status , 255 ) ;
} else {
// Not finished -> Delete unfinished Files and abort the mission!
waitThreadEnd ( true ) ;
unlink ( m_cLOutPath ) ;
unlink ( m_cROutPath ) ;
m_pDBC - > removeMediaComposerItem ( m_uComposerRecordId ) ;
} ;
} ;
} ;
@ -1616,8 +1619,7 @@ bool servaVideoComposer::loadRightQueue(){
} ;
void servaVideoComposer : : m_fThreadProc ( void ) {
uint8_t operc = 0 ;
//m_bThreadStatus = THREAD_INIT;
uint8_t operc = 0 ;
pthread_setname_np ( pthread_self ( ) , " VideoComposer " ) ;
if ( ( m_bComposerStatus & VIDEO_COMPOSER_LOK ) | | ( m_bComposerStatus & VIDEO_COMPOSER_ROK ) ) {
@ -1639,27 +1641,34 @@ void servaVideoComposer::m_fThreadProc(void){
cv : : VideoCapture R_cap ;
size_t R_NOF = 0 ;
uint32_t R_idx = 0 ;
uint64_t R_global_sts = 0 ;
uint32_t R_global_NOF = 0 ;
if ( ( m_bComposerStatus & VIDEO_COMPOSER_LOK ) = = VIDEO_COMPOSER_LOK ) {
LOG ( " Open Let Output " ) ;
char tUUID [ 36 ] ;
getUniqID_UUID ( tUUID ) ;
sprintf ( m_cLOutPath , " %s/%s.mp4 " , m_pSessionData - > reportVaultPath , tUUID ) ;
LOG ( " Open Lef t Output " ) ;
//char tUUID[36];
//getUniqID_UUID( tUUID );
sprintf ( m_cLOutPath , " data/composed_videos/%s_L.mp4 " , m_cUUID ) ; //m_pSessionData->reportVaultPath, tUUID );
LOG ( m_cLOutPath ) ;
L_out . open ( m_cLOutPath , cv : : VideoWriter : : fourcc ( ' a ' , ' v ' , ' c ' , ' 1 ' ) , fps , cv : : Size ( m_sComposerSettings . out_width , m_sComposerSettings . out_height ) ) ;
}
if ( ( m_bComposerStatus & VIDEO_COMPOSER_ROK ) = = VIDEO_COMPOSER_ROK ) {
LOG ( " Open Right Output " ) ;
char tUUID [ 36 ] ;
getUniqID_UUID ( tUUID ) ;
sprintf ( m_cROutPath , " %s/%s.mp4 " , m_pSessionData - > reportVaultPath , tUUID ) ;
//char tUUID[36];
//getUniqID_UUID( tUUID );
//sprintf( m_cROutPath, "%s/%s.mp4", m_pSessionData->reportVaultPath, tUUID );
sprintf ( m_cROutPath , " data/composed_videos/%s_R.mp4 " , m_cUUID ) ; //m_pSessionData->reportVaultPath, tUUID );
LOG ( m_cROutPath ) ;
R_out . open ( m_cROutPath , cv : : VideoWriter : : fourcc ( ' a ' , ' v ' , ' c ' , ' 1 ' ) , fps , cv : : Size ( m_sComposerSettings . out_width , m_sComposerSettings . out_height ) ) ;
}
char tB [ 256 ] ;
m_bThreadStatus = TH_WORKING ;
for ( uint64_t CurrentTS = BeginnhingTS ; CurrentTS < EndTS & & m_bThreadStatus < TH_ENDED ; CurrentTS + = FramePT ) {
uint8_t perc = ( uint8_t ) ( ( CurrentTS - BeginnhingTS ) * 100 ) / ( EndTS - BeginnhingTS ) ;
m_pDBC - > updateMediaComposerItem ( m_uComposerRecordId , ' L ' , perc ) ;
if ( ( m_bComposerStatus & VIDEO_COMPOSER_LOK ) = = VIDEO_COMPOSER_LOK ) {
uint64_t csts = ( ( uint64_t ) m_vLVideoQueue [ L_idx ] - > sts ) * 1000 + m_vLVideoQueue [ L_idx ] - > sms ;
if ( L_global_sts = = 0 ) L_global_sts = csts ;
@ -1762,67 +1771,135 @@ void servaVideoComposer::m_fThreadProc(void){
L_out . write ( mat ) ;
} else {
// No Next Video -> Finishing
LOG ( " Finished! " ) ;
//LOG("L Finished!");
L_out . release ( ) ;
m_bComposerStatus | = VIDEO_COMPOSER_L_DONE ;
//test
m_bThreadStatus = TH_ENDED ;
//m_bThreadStatus=TH_ENDED;
} ;
} ;
} ;
} ;
} ;
/*
if ( ( m_bComposerStatus & VIDEO_COMPOSER_LOK ) = = VIDEO_COMPOSER_LOK ) {
//Right Videos
if ( ( m_bComposerStatus & VIDEO_COMPOSER_ROK ) = = VIDEO_COMPOSER_ROK ) {
uint64_t csts = ( ( uint64_t ) m_vRVideoQueue [ R_idx ] - > sts ) * 1000 + m_vRVideoQueue [ R_idx ] - > sms ;
if ( R_global_sts = = 0 ) R_global_sts = csts ;
bool have_next_segment = R_idx < ( m_vRVideoQueue . size ( ) - 1 ) ;
if ( have_next_segment ) {
uint64_t nsts = ( ( uint64_t ) m_vRVideoQueue [ R_idx + 1 ] - > sts ) * 1000 + m_vRVideoQueue [ R_idx + 1 ] - > sms ;
if ( CurrentTS > csts & & CurrentTS < nsts ) {
// at beginning
if ( R_NOF = = 0 ) {
sprintf ( tRSRC , " %s/%s.%s " , m_pSessionData - > reportVaultPath , m_vRVideoQueue [ R_idx ] - > uuid , m_vRVideoQueue [ R_idx ] - > ext ) ;
LOG ( tRSRC ) ;
R_cap . open ( tRSRC ) ;
if ( R_cap . isOpened ( ) ) {
R_cap . set ( cv : : VideoCaptureProperties : : CAP_PROP_POS_FRAMES , 0.0 ) ;
} ;
} ;
} else {
if ( CurrentTS > nsts ) {
// Have to switch to the Next Video
R_cap . release ( ) ;
R_NOF = 0 ;
R_idx + + ;
} else {
if ( m_sComposerSettings . prependEmptyFrames ) {
R_global_NOF + + ;
// Prepend Video with No Video Frames ??
R_out . write ( m_oNoVideoMat ) ;
} ;
} ;
} ;
if ( R_cap . isOpened ( ) ) {
cv : : Mat mat ;
R_cap > > mat ;
if ( ! mat . empty ( ) ) {
R_NOF + + ;
R_global_NOF + + ;
if ( m_sComposerSettings . debugMode ) {
char tsb [ 64 ] ;
sprintf ( tsb , " #%d " , R_idx + 1 ) ;
cv : : putText ( mat , tsb , cv : : Point ( 100 , 100 ) , cv : : FONT_HERSHEY_DUPLEX , 3.0 , cv : : Scalar ( 0 , 255 , 255 ) , 5 , cv : : LINE_8 ) ;
for ( V_VP_INFO : : iterator idx = m_vLVideoQueue . begin ( ) ; idx ! = m_vLVideoQueue . end ( ) ; idx + + ) {
sprintf ( tSRC , " %s/%s.%s " , m_pSessionData - > reportVaultPath , ( * idx ) - > uuid , ( * idx ) - > ext ) ;
# ifdef DEBUG
char tB [ 1024 ] ;
sprintf ( tB , " Composing %s to %s " , tSRC , m_cLOutPath ) ;
LOG ( tB ) ;
# endif
cv : : VideoCapture cap ( tSRC ) ;
if ( cap . isOpened ( ) ) {
cv : : Mat mat ;
cap > > mat ;
if ( ! mat . empty ( ) ) {
cap . set ( cv : : VideoCaptureProperties : : CAP_PROP_POS_FRAMES , 0.0 ) ;
m_bThreadStatus = TH_WORKING ;
while ( cap . isOpened ( ) & & out . isOpened ( ) & & m_bThreadStatus < TH_ENDED ) {
cap > > mat ;
m_bComposerStatus = ENCODER_ENCODING ;
if ( mat . empty ( ) ) {
m_bThreadStatus = TH_ENDED ;
} else {
number_of_frames + + ;
out . write ( mat ) ;
uint32_t tTS = ( CurrentTS / 1000 ) ;
time_t t = ( time_t ) tTS ;
struct tm * tmp = localtime ( & t ) ;
if ( tmp ) {
if ( strftime ( tsb , sizeof ( tsb ) , " %F %T " , tmp ) ! = 0 ) {
int baseline = 0 ;
cv : : Size textSize = cv : : getTextSize ( tsb , cv : : FONT_HERSHEY_DUPLEX , 1 , 1 , & baseline ) ;
int x = ( m_sComposerSettings . out_width - textSize . width ) > > 1 ;
int y = ( m_sComposerSettings . out_height - textSize . height ) > > 1 ;
cv : : rectangle ( mat , cv : : Rect ( x , y - textSize . height - 4 , textSize . width , textSize . height + baseline + 4 ) , cv : : Scalar ( 0 , 0 , 0 ) , cv : : FILLED ) ;
cv : : putText ( mat , tsb , cv : : Point ( x , y ) , cv : : FONT_HERSHEY_DUPLEX , 1 , cv : : Scalar ( 255 , 255 , 0 ) , 1 , cv : : LINE_8 ) ;
} ;
} ;
} ;
uint8_t perc = ( 100 * number_of_frames ) / 900 ;
if ( operc ! = perc ) {
if ( perc > 100 ) perc = 100 ;
operc = perc ;
m_pMaster - > updateEncodeStatus ( m_nRefID , MEDIA_ENCODE_PROGRESS , perc ) ;
R_out . write ( mat ) ;
} else {
// End of Video but not reached Next Video Start -> Place No Video Frame ?
R_global_NOF + + ;
R_out . write ( m_oNoVideoMat ) ;
} ;
} ;
} else {
if ( CurrentTS > csts ) {
// Reading From Current Segment
if ( R_NOF = = 0 ) {
//Open Source File
sprintf ( tRSRC , " %s/%s.%s " , m_pSessionData - > reportVaultPath , m_vRVideoQueue [ R_idx ] - > uuid , m_vRVideoQueue [ R_idx ] - > ext ) ;
R_cap . open ( tRSRC ) ;
if ( R_cap . isOpened ( ) ) {
R_cap . set ( cv : : VideoCaptureProperties : : CAP_PROP_POS_FRAMES , 0.0 ) ;
} ;
} ;
cap . release ( ) ;
// Does it look reasonable? If so, then we can go ahead and delete the original file.
if ( number_of_frames > 100 ) {
unlink ( m_sDecoderVS . m_pFileName ) ;
} ;
if ( R_cap . isOpened ( ) ) {
cv : : Mat mat ;
R_cap > > mat ;
R_NOF + + ;
R_global_NOF + + ;
if ( ! mat . empty ( ) ) {
if ( m_sComposerSettings . debugMode ) {
char tsb [ 64 ] ;
sprintf ( tsb , " #%d " , R_idx + 1 ) ;
cv : : putText ( mat , tsb , cv : : Point ( 100 , 100 ) , cv : : FONT_HERSHEY_DUPLEX , 3.0 , cv : : Scalar ( 0 , 255 , 255 ) , 5 , cv : : LINE_8 ) ;
uint32_t tTS = ( CurrentTS / 1000 ) ;
time_t t = ( time_t ) tTS ;
struct tm * tmp = localtime ( & t ) ;
if ( tmp ) {
if ( strftime ( tsb , sizeof ( tsb ) , " %F %T " , tmp ) ! = 0 ) {
int baseline = 0 ;
cv : : Size textSize = cv : : getTextSize ( tsb , cv : : FONT_HERSHEY_DUPLEX , 1 , 1 , & baseline ) ;
int x = ( m_sComposerSettings . out_width - textSize . width ) > > 1 ;
int y = ( m_sComposerSettings . out_height - textSize . height ) > > 1 ;
cv : : rectangle ( mat , cv : : Rect ( x , y - textSize . height - 4 , textSize . width , textSize . height + baseline + 4 ) , cv : : Scalar ( 0 , 0 , 0 ) , cv : : FILLED ) ;
cv : : putText ( mat , tsb , cv : : Point ( x , y ) , cv : : FONT_HERSHEY_DUPLEX , 1 , cv : : Scalar ( 255 , 255 , 0 ) , 1 , cv : : LINE_8 ) ;
} ;
} ;
} ;
R_out . write ( mat ) ;
} else {
// No Next Video -> Finishing
// LOG("R Finished!");
R_out . release ( ) ;
m_bComposerStatus | = VIDEO_COMPOSER_R_DONE ;
//test
//m_bThreadStatus=TH_ENDED;
} ;
} ;
} ;
} ;
} ;
} ;
m_bStatus = TH_ENDED ;
*/
} ;
m_bComposerStatus | = VIDEO_COMPOSER_DONE ;
}
bool servaVideoComposer : : isFinished ( ) {
return ( ( m_bComposerStatus & VIDEO_COMPOSER_DONE ) = = VIDEO_COMPOSER_DONE ) ;
}
# undef LOG
# undef LOG_ERROR