Browse Source

Alarm clips: keep /dev/shm clean, move only the clips bound to an alarm

- A closed recorder segment that no alarm wants is deleted as soon as a
  later alarm cannot want it (pre-alarm time + 2 s), not kept 3 minutes.
  Only the segment being recorded and, for a few seconds, the last closed
  one are in <scratch>/stream_<node>.
- A segment around an alarm is moved (renamed) to <scratch>/<md5>.mp4 for
  the kernel, which copies it into the report folder and deletes it; no
  hard link to the recorder's file stays behind.
- Left-overs (crash, previous run): files in <scratch>/stream_* not
  written for 30 s and not tracked are deleted at start-up and every minute.
- Stopping the recording (settings change, shutdown) deletes the remaining
  segments and the empty stream folders.
- An alarm waits for its segments up to the segment length + 1 min (was a
  fixed 3 min, too short for long segments).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
pull/28/head
Claude 1 hour ago
parent
commit
2183c3a4d6
Failed to extract signature
  1. 121
      lib/App/common/kernelModuleEmbedVideo.cpp
  2. 8
      lib/App/common/kernelModuleEmbedVideo.h

121
lib/App/common/kernelModuleEmbedVideo.cpp

@ -113,6 +113,7 @@ kernelModuleEmbedVideo::kernelModuleEmbedVideo(uint32_t moduleID) :
m_bDetached = true; m_bDetached = true;
m_tLiveRetry = 0; m_tLiveRetry = 0;
m_tClipsScan = 0; m_tClipsScan = 0;
m_nClipWaitUs = 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) {
@ -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.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.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(); 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;
@ -2554,6 +2559,7 @@ void kernelModuleEmbedVideo::stopLive()
{ {
p.second->sourceSetNotReady(); // closes the recording 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) 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 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. (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 - 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 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 #ifndef EVE_CLIP_PRE_ALARM_MS
#define EVE_CLIP_PRE_ALARM_MS 5000 #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 #ifndef EVE_CLIP_POST_ALARM_MS
#define EVE_CLIP_POST_ALARM_MS 5000 #define EVE_CLIP_POST_ALARM_MS 5000
#endif #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 #endif
bool kernelModuleEmbedVideo::liveClipsActive(void) const bool kernelModuleEmbedVideo::liveClipsActive(void) const
@ -2641,8 +2655,31 @@ void kernelModuleEmbedVideo::onLiveSegment(const mtx::SegmentInfo & info)
{ {
std::lock_guard<std::mutex> lock(m_mClips); std::lock_guard<std::mutex> lock(m_mClips);
EveClipStream & tS = m_mapClips[info.path]; 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}); 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) 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 // 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 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();) for (auto it = stream.alarms.begin(); it != stream.alarms.end();)
{ {
bool tDone = it->bound && tRecordedUntil >= it->timeUs + EVE_CLIP_POST_ALARM_MS * 1000LL; bool tDone = it->bound && tRecordedUntil >= it->timeUs + EVE_CLIP_POST_ALARM_MS * 1000LL;
// nothing recorded for it (stream down): give up after a while // 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) if (tLost)
{ {
char tB[128]; char tB[128];
snprintf(tB, sizeof(tB), "Alarm clip: nothing recorded for event %u - no clip", it->eventID); snprintf(tB, sizeof(tB), "Alarm clip: nothing recorded for event %u - no clip", it->eventID);
LOG_ERROR(tB); 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); it = stream.alarms.erase(it);
} }
@ -2708,12 +2746,13 @@ bool kernelModuleEmbedVideo::handOffClipLocked(EveClipStream & stream, EveClipSe
strncpy(tSI->recordMD5, tMD5, 32); strncpy(tSI->recordMD5, tMD5, 32);
tSI->recordMD5[32] = (char) 0; tSI->recordMD5[32] = (char) 0;
free(tMD5); 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]; 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); LOG_ERROR(tB);
unlink(seg.file.c_str());
delete tSI; delete tSI;
return false; return false;
}; };
@ -2754,7 +2793,7 @@ bool kernelModuleEmbedVideo::handOffClipLocked(EveClipStream & stream, EveClipSe
return true; return true;
} }
// validate(): deletes old segments, checks the alarms; every minute also left-overs (a previous run)
// validate(): deletes segments no alarm wants, checks the alarms; every minute also left-overs (a previous run)
void kernelModuleEmbedVideo::sweepClips(void) void kernelModuleEmbedVideo::sweepClips(void)
{ {
int64_t tNow = mtx::nowUs(); int64_t tNow = mtx::nowUs();
@ -2762,13 +2801,8 @@ void kernelModuleEmbedVideo::sweepClips(void)
std::lock_guard<std::mutex> lock(m_mClips); std::lock_guard<std::mutex> lock(m_mClips);
for (auto & c : m_mapClips) 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); matchClipsLocked(c.second, tNow);
pruneClipsLocked(c.second, tNow);
}; };
} }
time_t tSec = time(NULL); time_t tSec = time(NULL);
@ -2777,7 +2811,7 @@ void kernelModuleEmbedVideo::sweepClips(void)
return; return;
}; };
m_tClipsScan = tSec; 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); DIR * tDir = opendir(m_pVideoScratchPath);
if (tDir == NULL) if (tDir == NULL)
{ {
@ -2806,9 +2840,21 @@ void kernelModuleEmbedVideo::sweepClips(void)
}; };
std::string tFile = tSub + "/" + tF->d_name; std::string tFile = tSub + "/" + tF->d_name;
struct stat tSt; 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); closedir(tD);
@ -2816,6 +2862,35 @@ void kernelModuleEmbedVideo::sweepClips(void)
closedir(tDir); 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
#undef TLOG_ERROR #undef TLOG_ERROR

8
lib/App/common/kernelModuleEmbedVideo.h

@ -148,8 +148,9 @@ class kernelModuleEmbedVideo : public kernelModule, public mtx::PathProvider
void stopLive(); void stopLive();
// Alarm clips from the recorded segments (live_record), replacing the old encoder's clips: // Alarm clips from the recorded segments (live_record), replacing the old encoder's clips:
// recent segments are kept for a while, a segment around an alarm is handed to the kernel
// (EVT_CAPTURE_DONE -> Reports/History) like the old encoder's clips.
// a closed segment stays in the scratch (/dev/shm) only as long as a new alarm may still want
// it (pre-alarm time); a segment around an alarm is moved to the kernel (EVT_CAPTURE_DONE ->
// Reports/History) like the old encoder's clips, the others are deleted.
struct EveClipSegment struct EveClipSegment
{ {
std::string file; std::string file;
@ -176,11 +177,14 @@ class kernelModuleEmbedVideo : public kernelModule, public mtx::PathProvider
std::mutex m_mClips; std::mutex m_mClips;
std::map<std::string, EveClipStream> m_mapClips; // path name -> stream std::map<std::string, EveClipStream> m_mapClips; // path name -> stream
time_t m_tClipsScan; // last scan of the scratch folders for left-over segments time_t m_tClipsScan; // last scan of the scratch folders for left-over segments
int64_t m_nClipWaitUs; // an alarm waits this long for its segments (segment length + 1 min)
bool liveClipsActive(void) const; bool liveClipsActive(void) const;
void onLiveSegment(const mtx::SegmentInfo & info); void onLiveSegment(const mtx::SegmentInfo & info);
void matchClipsLocked(EveClipStream & stream, int64_t nowUs); void matchClipsLocked(EveClipStream & stream, int64_t nowUs);
bool handOffClipLocked(EveClipStream & stream, EveClipSegment & seg); bool handOffClipLocked(EveClipStream & stream, EveClipSegment & seg);
void pruneClipsLocked(EveClipStream & stream, int64_t nowUs);
void sweepClips(void); void sweepClips(void);
void purgeClips(void);
#ifndef EVE_WORK_WITH_MEDIAMTX #ifndef EVE_WORK_WITH_MEDIAMTX
protected: protected:

Loading…
Cancel
Save