@ -24,6 +24,8 @@
# include <arpa/inet.h>
# include <arpa/inet.h>
# include <ctype.h>
# include <ctype.h>
# include <dirent.h>
# include <sys/stat.h>
# include <err.h>
# include <err.h>
# include <errno.h>
# include <errno.h>
# include <getopt.h>
# include <getopt.h>
@ -110,6 +112,7 @@ kernelModuleEmbedVideo::kernelModuleEmbedVideo(uint32_t moduleID) :
} ;
} ;
m_bDetached = true ;
m_bDetached = true ;
m_tLiveRetry = 0 ;
m_tLiveRetry = 0 ;
m_tClipsScan = 0 ;
// The MediaMtx library logs into the la_mojo log (tag MTX)
// The MediaMtx library logs into the la_mojo log (tag MTX)
mtx : : Logger : : instance ( ) . setDestinations ( false , std : : string ( ) ) ;
mtx : : Logger : : instance ( ) . setDestinations ( false , std : : string ( ) ) ;
mtx : : Logger : : instance ( ) . setSink ( [ ] ( mtx : : LogLevel level , const char * line ) {
mtx : : Logger : : instance ( ) . setSink ( [ ] ( mtx : : LogLevel level , const char * line ) {
@ -350,7 +353,20 @@ bool kernelModuleEmbedVideo::isProcessingVideoStream(){
} ;
} ;
} ;
} ;
m_mStreamHandlersProtector . unlock ( ) ;
m_mStreamHandlersProtector . unlock ( ) ;
return RV ; ( m_mapHANDLERS . size ( ) > 0 ) ;
if ( ! RV )
{
// recorder clips: an alarm whose clip is not handed over yet
std : : lock_guard < std : : mutex > lock ( m_mClips ) ;
for ( auto & c : m_mapClips )
{
if ( ! c . second . alarms . empty ( ) )
{
RV = true ;
break ;
} ;
} ;
} ;
return RV ;
} ;
} ;
uint8_t kernelModuleEmbedVideo : : getMediaMode ( void )
uint8_t kernelModuleEmbedVideo : : getMediaMode ( void )
{
{
@ -1717,6 +1733,7 @@ bool kernelModuleEmbedVideo::validate()
} ;
} ;
} ;
} ;
} ;
} ;
sweepClips ( ) ; // old recorded segments, alarms whose clip is overdue
// The WebRTC server could not start (e.g. the port was still used by the MediaMTX service): retry
// The WebRTC server could not start (e.g. the port was still used by the MediaMTX service): retry
if ( m_bEnabled & & m_sEmbedVideoData . live_mode ! = EVE_LIVE_OFF & & ! m_pWebrtc & & time ( NULL ) > = m_tLiveRetry )
if ( m_bEnabled & & m_sEmbedVideoData . live_mode ! = EVE_LIVE_OFF & & ! m_pWebrtc & & time ( NULL ) > = m_tLiveRetry )
{
{
@ -2162,7 +2179,14 @@ bool kernelModuleEmbedVideo::saveStreamPicture(DBC_SNAPSHOT_DATA * ptr)
# ifdef DEBUG
# ifdef DEBUG
LOG ( " Saving Video! " ) ;
LOG ( " Saving Video! " ) ;
# endif
# endif
if ( ptr - > assoc_event_ids . size ( ) )
if ( ptr - > assoc_event_ids . size ( ) & & liveClipsActive ( ) )
{
for ( DBC_INT_LIST : : iterator idx = ptr - > assoc_event_ids . begin ( ) ; idx < ptr - > assoc_event_ids . end ( ) ; idx + + )
{
clipAlarm ( ptr - > stream_id , * idx ) ;
} ;
}
else if ( ptr - > assoc_event_ids . size ( ) )
{
{
m_mStreamHandlersProtector . lock ( ) ;
m_mStreamHandlersProtector . lock ( ) ;
mapSTREAMHANDLERS : : iterator hnd = m_mapHANDLERS . find ( ptr - > stream_id ) ;
mapSTREAMHANDLERS : : iterator hnd = m_mapHANDLERS . find ( ptr - > stream_id ) ;
@ -2342,7 +2366,8 @@ bool kernelModuleEmbedVideo::registerStream(uint32_t streamID, uint8_t flip_mode
LOG ( " Create New Stream Handler! " ) ;
LOG ( " Create New Stream Handler! " ) ;
# endif
# endif
if ( ( m_sEmbedVideoData . output_mode & EVE_OUTPUT_VIDEO ) ! = EVE_OUTPUT_VIDEO )
// Alarm clips: from the recorder segments when it records (liveClipsActive), else the old encoder
if ( ( m_sEmbedVideoData . output_mode & EVE_OUTPUT_VIDEO ) ! = EVE_OUTPUT_VIDEO | | liveClipsActive ( ) )
{
{
tData = new streamHandler ( this , MS_MODE_DECODE_FRAMES , flip_mode ) ;
tData = new streamHandler ( this , MS_MODE_DECODE_FRAMES , flip_mode ) ;
}
}
@ -2378,7 +2403,7 @@ bool kernelModuleEmbedVideo::registerStream(uint32_t streamID, uint8_t flip_mode
# endif
# endif
streamHandler * tSH = NULL ;
streamHandler * tSH = NULL ;
// Create New One
// Create New One
if ( ( m_sEmbedVideoData . output_mode & EVE_OUTPUT_VIDEO ) ! = EVE_OUTPUT_VIDEO )
if ( ( m_sEmbedVideoData . output_mode & EVE_OUTPUT_VIDEO ) ! = EVE_OUTPUT_VIDEO | | liveClipsActive ( ) )
{
{
# ifdef DEBUG_EXTRA
# ifdef DEBUG_EXTRA
LOG ( " New 1 " ) ;
LOG ( " New 1 " ) ;
@ -2465,6 +2490,7 @@ void kernelModuleEmbedVideo::applyLiveSettings()
m_sLiveConf . recordPath = std : : string ( m_pVideoScratchPath ) + " /%path/%Y-%m-%d_%H-%M-%S-%f " ;
m_sLiveConf . recordPath = std : : string ( m_pVideoScratchPath ) + " /%path/%Y-%m-%d_%H-%M-%S-%f " ;
m_sLiveConf . recordSegmentDurationMs = ( int64_t ) ( m_sEmbedVideoData . live_record_segment ? m_sEmbedVideoData . live_record_segment : 60 ) * 1000 ;
m_sLiveConf . recordSegmentDurationMs = ( int64_t ) ( m_sEmbedVideoData . live_record_segment ? m_sEmbedVideoData . live_record_segment : 60 ) * 1000 ;
m_sLiveConf . recordPartDurationMs = 1000 ;
m_sLiveConf . recordPartDurationMs = 1000 ;
m_sLiveConf . onSegmentDone = [ this ] ( const mtx : : SegmentInfo & info ) { onLiveSegment ( info ) ; } ;
m_sLiveConf . webrtcICEServers . clear ( ) ;
m_sLiveConf . webrtcICEServers . clear ( ) ;
std : : string tIce ( m_sEmbedVideoData . live_ice_servers ) ;
std : : string tIce ( m_sEmbedVideoData . live_ice_servers ) ;
size_t tPos = 0 ;
size_t tPos = 0 ;
@ -2555,6 +2581,241 @@ std::shared_ptr<mtx::MediaPath> kernelModuleEmbedVideo::findPath(const std::stri
return ( it = = m_mapLivePaths . end ( ) ) ? nullptr : it - > second ;
return ( it = = m_mapLivePaths . end ( ) ) ? nullptr : it - > second ;
}
}
/* ---------------------------------------------------------------- alarm clips from the recorder
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 ;
- 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 .
*/
# ifndef EVE_CLIP_PRE_ALARM_MS
# define EVE_CLIP_PRE_ALARM_MS 5000
# endif
# 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)
# endif
bool kernelModuleEmbedVideo : : liveClipsActive ( void ) const
{
return m_sEmbedVideoData . live_mode ! = EVE_LIVE_OFF & & m_sEmbedVideoData . live_record ;
}
void kernelModuleEmbedVideo : : registerClipStream ( const std : : string & path , uint32_t streamID , uint64_t sourceID , uint8_t fps )
{
std : : lock_guard < std : : mutex > lock ( m_mClips ) ;
EveClipStream & tS = m_mapClips [ path ] ;
tS . streamID = streamID ;
tS . sourceID = sourceID ;
tS . fps = fps ? fps : 25 ;
}
void kernelModuleEmbedVideo : : clipAlarm ( uint32_t streamID , uint32_t eventID )
{
int64_t tNow = mtx : : nowUs ( ) ;
std : : lock_guard < std : : mutex > lock ( m_mClips ) ;
for ( auto & c : m_mapClips )
{
if ( c . second . streamID = = streamID )
{
c . second . alarms . push_back ( { eventID , tNow , false } ) ;
matchClipsLocked ( c . second , tNow ) ; // the pre-alarm part may be recorded already
return ;
} ;
} ;
char tB [ 128 ] ;
snprintf ( tB , sizeof ( tB ) , " Alarm clip: stream %u is not recorded - no clip for event %u " , streamID , eventID ) ;
LOG_ERROR ( tB ) ;
}
// Recorder thread: a segment is complete
void kernelModuleEmbedVideo : : onLiveSegment ( const mtx : : SegmentInfo & info )
{
std : : lock_guard < std : : mutex > lock ( m_mClips ) ;
EveClipStream & tS = m_mapClips [ info . path ] ;
tS . segments . push_back ( { info . file , info . startUs , info . startUs + info . durationUs , info . width , info . height , false } ) ;
matchClipsLocked ( tS , mtx : : nowUs ( ) ) ;
}
void kernelModuleEmbedVideo : : matchClipsLocked ( EveClipStream & stream , int64_t nowUs )
{
if ( stream . alarms . empty ( ) )
{
return ;
} ;
for ( EveClipSegment & tSeg : stream . segments )
{
if ( tSeg . handed )
{
continue ;
} ;
for ( const EveClipAlarm & a : stream . alarms )
{
if ( a . timeUs - EVE_CLIP_PRE_ALARM_MS * 1000LL < tSeg . endUs & & a . timeUs + EVE_CLIP_POST_ALARM_MS * 1000LL > tSeg . startUs )
{
handOffClipLocked ( stream , tSeg ) ;
break ;
} ;
} ;
} ;
// 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 ;
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 ;
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 ) )
{
it = stream . alarms . erase ( it ) ;
}
else
{
+ + it ;
} ;
} ;
}
bool kernelModuleEmbedVideo : : handOffClipLocked ( EveClipStream & stream , EveClipSegment & seg )
{
seg . handed = true ; // also on failure: do not retry every time
if ( stream . streamID = = 0 )
{
return false ; // a path no stream handler registered (cannot happen: initLive registers it)
} ;
CSTR tMD5 = getUniqID_MD5 ( ) ;
if ( tMD5 = = NULL )
{
LOG_ERROR ( " Alarm clip: no MD5 " ) ;
return false ;
} ;
VIDEO_SEGMENT_INFO * tSI = new VIDEO_SEGMENT_INFO ( ) ; // owned by the receiver (mojoKernel deletes it)
snprintf ( tSI - > scratchPath , sizeof ( tSI - > scratchPath ) , " %s/%s.mp4 " , m_pVideoScratchPath , tMD5 ) ;
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 )
{
char tB [ PATH_MAX + 64 ] ;
snprintf ( tB , sizeof ( tB ) , " Alarm clip: cannot link %s (%s) " , seg . file . c_str ( ) , strerror ( errno ) ) ;
LOG_ERROR ( tB ) ;
delete tSI ;
return false ;
} ;
strncpy ( tSI - > recordEXT , " mp4 " , sizeof ( tSI - > recordEXT ) ) ;
tSI - > stream_id = stream . streamID ;
tSI - > streamTSS = ( uint32_t ) ( seg . startUs / 1000000 ) ;
tSI - > streamTMS = ( uint32_t ) ( ( seg . startUs / 1000 ) % 1000 ) ;
tSI - > streamFPS = stream . fps ;
tSI - > streamWidth = ( uint16_t ) seg . width ;
tSI - > streamHeight = ( uint16_t ) seg . height ;
struct stat tSt ;
tSI - > dataSize = ( stat ( tSI - > scratchPath , & tSt ) = = 0 ) ? ( uint32_t ) tSt . st_size : 0 ;
for ( EveClipAlarm & a : stream . alarms )
{
// the segment the alarm happened in (or the first one after it: nothing recorded at its time)
if ( ! a . bound & & a . timeUs < seg . endUs )
{
a . bound = true ;
int64_t tOffset = a . timeUs > seg . startUs ? a . timeUs - seg . startUs : 0 ;
tSI - > eventIDS . push_back ( a . eventID ) ;
tSI - > eventTSS . push_back ( ( uint32_t ) ( a . timeUs / 1000000 ) ) ;
tSI - > eventTMS . push_back ( ( uint32_t ) ( ( a . timeUs / 1000 ) % 1000 ) ) ;
tSI - > eventFrames . push_back ( ( uint32_t ) ( tOffset * stream . fps / 1000000 ) ) ;
} ;
} ;
EVENT_CONTAINER * bEVT = createBasicEvent ( stream . sourceID , EVT_CLASS_VIDEO , EVT_CAPTURE_DONE , NULL , NULL , tSI ) ;
if ( bEVT = = NULL )
{
LOG_ERROR ( " Alarm clip: event creation failure! " ) ;
unlink ( tSI - > scratchPath ) ;
delete tSI ;
return false ;
} ;
char tB [ PATH_MAX + 96 ] ;
snprintf ( tB , sizeof ( tB ) , " Alarm clip: %s (%u event(s)) " , seg . file . c_str ( ) , ( unsigned ) tSI - > eventIDS . size ( ) ) ;
LOG ( tB ) ;
basicEventHeap : : getInstance ( ) - > broadcast ( bEVT ) ;
return true ;
}
// validate(): deletes old segments, checks the alarms; every minute also left-overs (a previous run)
void kernelModuleEmbedVideo : : sweepClips ( void )
{
int64_t tNow = mtx : : nowUs ( ) ;
{
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 ) ;
} ;
}
time_t tSec = time ( NULL ) ;
if ( tSec - m_tClipsScan < 60 )
{
return ;
} ;
m_tClipsScan = tSec ;
// <scratch>/stream_*/*.mp4 older than the keep time (+1 min): not tracked any more (restart, path gone)
DIR * tDir = opendir ( m_pVideoScratchPath ) ;
if ( tDir = = NULL )
{
return ;
} ;
struct dirent * tE ;
while ( ( tE = readdir ( tDir ) ) ! = NULL )
{
if ( strncmp ( tE - > d_name , " stream_ " , 7 ) ! = 0 )
{
continue ;
} ;
std : : string tSub = std : : string ( m_pVideoScratchPath ) + " / " + tE - > d_name ;
DIR * tD = opendir ( tSub . c_str ( ) ) ;
if ( tD = = NULL )
{
continue ;
} ;
struct dirent * tF ;
while ( ( tF = readdir ( tD ) ) ! = NULL )
{
size_t tLen = strlen ( tF - > d_name ) ;
if ( tLen < 4 | | strcmp ( tF - > d_name + tLen - 4 , " .mp4 " ) ! = 0 )
{
continue ;
} ;
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 )
{
unlink ( tFile . c_str ( ) ) ;
} ;
} ;
closedir ( tD ) ;
} ;
closedir ( tDir ) ;
}
# undef TLOG
# undef TLOG
# undef TLOG_ERROR
# undef TLOG_ERROR