@ -113,6 +113,7 @@ kernelModuleEmbedVideo::kernelModuleEmbedVideo(uint32_t moduleID) :
m_bDetached = true ;
m_tLiveRetry = 0 ;
m_tClipsScan = 0 ;
m_nClipWaitUs = 0 ;
// The MediaMtx library logs into the la_mojo log (tag MTX)
mtx : : Logger : : instance ( ) . setDestinations ( false , std : : string ( ) ) ;
mtx : : Logger : : instance ( ) . setSink ( [ ] ( mtx : : LogLevel level , const char * line ) {
@ -2491,6 +2492,10 @@ void kernelModuleEmbedVideo::applyLiveSettings()
m_sLiveConf . recordSegmentDurationMs = ( int64_t ) ( m_sEmbedVideoData . live_record_segment ? m_sEmbedVideoData . live_record_segment : 60 ) * 1000 ;
m_sLiveConf . recordPartDurationMs = 1000 ;
m_sLiveConf . onSegmentDone = [ this ] ( const mtx : : SegmentInfo & info ) { onLiveSegment ( info ) ; } ;
{
std : : lock_guard < std : : mutex > lock ( m_mClips ) ;
m_nClipWaitUs = m_sLiveConf . recordSegmentDurationMs * 1000LL + 60000000LL ; // + the post-alarm time
}
m_sLiveConf . webrtcICEServers . clear ( ) ;
std : : string tIce ( m_sEmbedVideoData . live_ice_servers ) ;
size_t tPos = 0 ;
@ -2554,6 +2559,7 @@ void kernelModuleEmbedVideo::stopLive()
{
p . second - > sourceSetNotReady ( ) ; // closes the recording
} ;
purgeClips ( ) ; // nothing records now: no segment is wanted any more
}
std : : shared_ptr < mtx : : MediaPath > kernelModuleEmbedVideo : : livePath ( uint32_t nodeID )
@ -2586,13 +2592,18 @@ std::shared_ptr<mtx::MediaPath> kernelModuleEmbedVideo::findPath(const std::stri
When the recorder writes segments ( live_record ) , the alarm clips come from them and the old encoder
( MS_MODE_ENCODE_FRAMES ) is not started , so the video is no longer encoded and written twice .
- every closed segment is kept for EVE_CLIP_KEEP_MS ( < scratch > / stream_ < node > / . . . mp4 ) , then deleted ;
The scratch is in shared memory ( / dev / shm ) , so it holds as little as possible :
- an alarm ( saveStreamPicture - > clipAlarm ) wants the segments within EVE_CLIP_PRE_ALARM_MS before
and EVE_CLIP_POST_ALARM_MS after it . Those segments are handed to the kernel like the old encoder ' s
clips : a hard link < scratch > / < md5 > . mp4 in a VIDEO_SEGMENT_INFO with EVT_CAPTURE_DONE . The alarm is
bound ( eventIDS / eventFrames ) to the segment it happened in ;
- mojoKernel : : handleVideo copies the clip into the report and deletes the link ( or the remote server
keeps it ) ; the recorder ' s own file is deleted by sweepClips ( ) as usual .
clips : moved ( renamed ) to < scratch > / < md5 > . mp4 in a VIDEO_SEGMENT_INFO with EVT_CAPTURE_DONE . The
alarm is bound ( eventIDS / eventFrames ) to the segment it happened in ;
- mojoKernel : : handleVideo copies the clip into the report folder and deletes it from the scratch
( or the remote server keeps it until sent ) ;
- a closed segment no alarm wants is deleted once its end is EVE_CLIP_PRE_ALARM_MS ( + EVE_CLIP_LAG_MS )
in the past : a later alarm cannot want it any more . So only the segment being recorded and the
last closed one ( for a few seconds ) are in < scratch > / stream_ < node > ;
- left - overs ( a crash , a previous run ) are deleted by the scan in sweepClips ( ) , and everything
when the recording stops ( purgeClips ( ) ) .
*/
# ifndef EVE_CLIP_PRE_ALARM_MS
# define EVE_CLIP_PRE_ALARM_MS 5000
@ -2600,8 +2611,11 @@ std::shared_ptr<mtx::MediaPath> kernelModuleEmbedVideo::findPath(const std::stri
# ifndef EVE_CLIP_POST_ALARM_MS
# define EVE_CLIP_POST_ALARM_MS 5000
# endif
# ifndef EVE_CLIP_KEEP_MS
# define EVE_CLIP_KEEP_MS 180000 // recorded segments kept for clips of later alarms (pre-alarm part)
# ifndef EVE_CLIP_LAG_MS
# define EVE_CLIP_LAG_MS 2000 // an alarm reaches clipAlarm() at most this late (event dispatch)
# endif
# ifndef EVE_CLIP_LEFTOVER_S
# define EVE_CLIP_LEFTOVER_S 30 // a file in <scratch>/stream_* not written for this long is not recorded any more
# endif
bool kernelModuleEmbedVideo : : liveClipsActive ( void ) const
@ -2641,8 +2655,31 @@ void kernelModuleEmbedVideo::onLiveSegment(const mtx::SegmentInfo & info)
{
std : : lock_guard < std : : mutex > lock ( m_mClips ) ;
EveClipStream & tS = m_mapClips [ info . path ] ;
int64_t tNow = mtx : : nowUs ( ) ;
tS . segments . push_back ( { info . file , info . startUs , info . startUs + info . durationUs , info . width , info . height , false } ) ;
matchClipsLocked ( tS , mtx : : nowUs ( ) ) ;
matchClipsLocked ( tS , tNow ) ;
pruneClipsLocked ( tS , tNow ) ;
}
// Drops the segments that are handed over (the file is moved) or that no alarm can want any more (deleted)
void kernelModuleEmbedVideo : : pruneClipsLocked ( EveClipStream & stream , int64_t nowUs )
{
for ( auto it = stream . segments . begin ( ) ; it ! = stream . segments . end ( ) ; )
{
if ( it - > handed )
{
it = stream . segments . erase ( it ) ;
}
else if ( nowUs - it - > endUs > ( EVE_CLIP_PRE_ALARM_MS + EVE_CLIP_LAG_MS ) * 1000LL )
{
unlink ( it - > file . c_str ( ) ) ;
it = stream . segments . erase ( it ) ;
}
else
{
+ + it ;
} ;
} ;
}
void kernelModuleEmbedVideo : : matchClipsLocked ( EveClipStream & stream , int64_t nowUs )
@ -2668,18 +2705,19 @@ void kernelModuleEmbedVideo::matchClipsLocked(EveClipStream & stream, int64_t no
} ;
// An alarm is done when its segment is handed over and the post-alarm time is recorded
int64_t tRecordedUntil = stream . segments . empty ( ) ? 0 : stream . segments . back ( ) . endUs ;
int64_t tWaitUs = ( m_nClipWaitUs > 0 ? m_nClipWaitUs : 120000000LL ) + EVE_CLIP_POST_ALARM_MS * 1000LL ;
for ( auto it = stream . alarms . begin ( ) ; it ! = stream . alarms . end ( ) ; )
{
bool tDone = it - > bound & & tRecordedUntil > = it - > timeUs + EVE_CLIP_POST_ALARM_MS * 1000LL ;
// nothing recorded for it (stream down): give up after a while
bool tLost = ! it - > bound & & nowUs - it - > timeUs > EVE_CLIP_KEEP_MS * 1000LL ;
bool tLost = ! it - > bound & & nowUs - it - > timeUs > tWaitUs ;
if ( tLost )
{
char tB [ 128 ] ;
snprintf ( tB , sizeof ( tB ) , " Alarm clip: nothing recorded for event %u - no clip " , it - > eventID ) ;
LOG_ERROR ( tB ) ;
} ;
if ( tDone | | tLost | | ( it - > bound & & nowUs - it - > timeUs > EVE_CLIP_KEEP_MS * 1000LL ) )
if ( tDone | | tLost | | ( it - > bound & & nowUs - it - > timeUs > tWaitUs ) )
{
it = stream . alarms . erase ( it ) ;
}
@ -2708,12 +2746,13 @@ bool kernelModuleEmbedVideo::handOffClipLocked(EveClipStream & stream, EveClipSe
strncpy ( tSI - > recordMD5 , tMD5 , 32 ) ;
tSI - > recordMD5 [ 32 ] = ( char ) 0 ;
free ( tMD5 ) ;
// a hard link: the recorder's file stays until sweepClips(), the kernel deletes its link
if ( link ( seg . file . c_str ( ) , tSI - > scratchPath ) ! = 0 & & fileCopy ( seg . file . c_str ( ) , tSI - > scratchPath ) ! = 0 )
// moved: the kernel copies it into the report folder and deletes it (no copy left in the scratch)
if ( rename ( seg . file . c_str ( ) , tSI - > scratchPath ) ! = 0 & & ( fileCopy ( seg . file . c_str ( ) , tSI - > scratchPath ) ! = 0 | | unlink ( seg . file . c_str ( ) ) ! = 0 ) )
{
char tB [ PATH_MAX + 64 ] ;
snprintf ( tB , sizeof ( tB ) , " Alarm clip: cannot link %s (%s) " , seg . file . c_str ( ) , strerror ( errno ) ) ;
snprintf ( tB , sizeof ( tB ) , " Alarm clip: cannot move %s (%s) " , seg . file . c_str ( ) , strerror ( errno ) ) ;
LOG_ERROR ( tB ) ;
unlink ( seg . file . c_str ( ) ) ;
delete tSI ;
return false ;
} ;
@ -2754,7 +2793,7 @@ bool kernelModuleEmbedVideo::handOffClipLocked(EveClipStream & stream, EveClipSe
return true ;
}
// validate(): deletes old segments, checks the alarms; every minute also left-overs (a previous run)
// validate(): deletes segments no alarm wa nts, checks the alarms; every minute also left-overs (a previous run)
void kernelModuleEmbedVideo : : sweepClips ( void )
{
int64_t tNow = mtx : : nowUs ( ) ;
@ -2762,13 +2801,8 @@ void kernelModuleEmbedVideo::sweepClips(void)
std : : lock_guard < std : : mutex > lock ( m_mClips ) ;
for ( auto & c : m_mapClips )
{
std : : deque < EveClipSegment > & tSegs = c . second . segments ;
while ( ! tSegs . empty ( ) & & tNow - tSegs . front ( ) . endUs > EVE_CLIP_KEEP_MS * 1000LL )
{
unlink ( tSegs . front ( ) . file . c_str ( ) ) ;
tSegs . pop_front ( ) ;
} ;
matchClipsLocked ( c . second , tNow ) ;
pruneClipsLocked ( c . second , tNow ) ;
} ;
}
time_t tSec = time ( NULL ) ;
@ -2777,7 +2811,7 @@ void kernelModuleEmbedVideo::sweepClips(void)
return ;
} ;
m_tClipsScan = tSec ;
// <scratch>/stream_*/*.mp4 older than the keep time (+1 min): not tracked any more (restart, path gone )
// <scratch>/stream_*/*.mp4 not written for a while and not tracked: left-overs (crash, previous run )
DIR * tDir = opendir ( m_pVideoScratchPath ) ;
if ( tDir = = NULL )
{
@ -2806,9 +2840,21 @@ void kernelModuleEmbedVideo::sweepClips(void)
} ;
std : : string tFile = tSub + " / " + tF - > d_name ;
struct stat tSt ;
if ( stat ( tFile . c_str ( ) , & tSt ) = = 0 & & S_ISREG ( tSt . st_mode ) & & ( tSec - tSt . st_mtime ) * 1000LL > EVE_CLIP_KEEP_MS + 60000 )
if ( stat ( tFile . c_str ( ) , & tSt ) = = 0 & & S_ISREG ( tSt . st_mode ) & & tSec - tSt . st_mtime > EVE_CLIP_LEFTOVER_S )
{
unlink ( tFile . c_str ( ) ) ;
std : : lock_guard < std : : mutex > lock ( m_mClips ) ;
bool tTracked = false ;
for ( auto & c : m_mapClips )
{
for ( const EveClipSegment & tSeg : c . second . segments )
{
tTracked = tTracked | | tSeg . file = = tFile ;
} ;
} ;
if ( ! tTracked )
{
unlink ( tFile . c_str ( ) ) ;
} ;
} ;
} ;
closedir ( tD ) ;
@ -2816,6 +2862,35 @@ void kernelModuleEmbedVideo::sweepClips(void)
closedir ( tDir ) ;
}
// stopLive(): the recording is closed (its last segment was handed over if an alarm wanted it),
// so the remaining segments and the stream folders go; alarms still waiting get no clip
void kernelModuleEmbedVideo : : purgeClips ( void )
{
std : : lock_guard < std : : mutex > lock ( m_mClips ) ;
for ( auto & c : m_mapClips )
{
for ( const EveClipSegment & tSeg : c . second . segments )
{
if ( ! tSeg . handed )
{
unlink ( tSeg . file . c_str ( ) ) ;
} ;
} ;
c . second . segments . clear ( ) ;
for ( const EveClipAlarm & a : c . second . alarms )
{
if ( ! a . bound )
{
char tB [ 128 ] ;
snprintf ( tB , sizeof ( tB ) , " Alarm clip: recording stopped - no clip for event %u " , a . eventID ) ;
LOG_ERROR ( tB ) ;
} ;
} ;
c . second . alarms . clear ( ) ;
rmdir ( ( std : : string ( m_pVideoScratchPath ) + " / " + c . first ) . c_str ( ) ) ; // only if empty
} ;
}
# undef TLOG
# undef TLOG_ERROR