From 2183c3a4d62c3135bbdc26fe1e48b62b70836f9d Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 3 Dec 2026 13:49:45 +0000 Subject: [PATCH] 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 /stream_. - A segment around an alarm is moved (renamed) to /.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 /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 Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16 --- lib/App/common/kernelModuleEmbedVideo.cpp | 121 ++++++++++++++++++---- lib/App/common/kernelModuleEmbedVideo.h | 8 +- 2 files changed, 104 insertions(+), 25 deletions(-) diff --git a/lib/App/common/kernelModuleEmbedVideo.cpp b/lib/App/common/kernelModuleEmbedVideo.cpp index a641644b..05c6aa18 100644 --- a/lib/App/common/kernelModuleEmbedVideo.cpp +++ b/lib/App/common/kernelModuleEmbedVideo.cpp @@ -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 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 kernelModuleEmbedVideo::livePath(uint32_t nodeID) @@ -2586,13 +2592,18 @@ std::shared_ptr 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 (/stream_/...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 /.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 /.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 /stream_; + - 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 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 /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 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 wants, 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 lock(m_mClips); for (auto & c : m_mapClips) { - std::deque & 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; - // /stream_*/*.mp4 older than the keep time (+1 min): not tracked any more (restart, path gone) + // /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 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 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 diff --git a/lib/App/common/kernelModuleEmbedVideo.h b/lib/App/common/kernelModuleEmbedVideo.h index 5b9fa162..b5d71794 100644 --- a/lib/App/common/kernelModuleEmbedVideo.h +++ b/lib/App/common/kernelModuleEmbedVideo.h @@ -148,8 +148,9 @@ class kernelModuleEmbedVideo : public kernelModule, public mtx::PathProvider void stopLive(); // 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 { std::string file; @@ -176,11 +177,14 @@ class kernelModuleEmbedVideo : public kernelModule, public mtx::PathProvider std::mutex m_mClips; std::map m_mapClips; // path name -> stream 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; void onLiveSegment(const mtx::SegmentInfo & info); void matchClipsLocked(EveClipStream & stream, int64_t nowUs); bool handOffClipLocked(EveClipStream & stream, EveClipSegment & seg); + void pruneClipsLocked(EveClipStream & stream, int64_t nowUs); void sweepClips(void); + void purgeClips(void); #ifndef EVE_WORK_WITH_MEDIAMTX protected: