BlueGit

timfa filemanager

Merge fix/image-subtitle-prune-eviction into master

timfa Tim Falken · pushed by timfa

Authored 2026-05-16 · 6 months ago

Hash
70f66df37c6fc7e436a71eff7be74ba6768ea2c9
4 files changed +16 −0
.github/technical-reference.md +1 −0
@@ -106,6 +106,7 @@
106 106 - createVideoStreamSession(sourcePath) - Probes input metadata and reuses a compatible active transcode session per source file when possible; returns a per-viewer lease id as sessionId plus duration, videoWidth, videoHeight, subtitleTracks, imageSubtitleTracks, and subtitleSummary data.
107 107 - sendVideoStreamAsset(sessionId, assetPath, req, res) - Resolves viewer leases to a shared stream session, serves HLS assets, tracks lease activity, and prunes segments using the slowest active viewer position.
108 108 - releaseVideoStreamSession(sessionId) - Releases one viewer lease; only destroys the underlying transcode session when no active leases remain; cleanup also clears image subtitle frame/event caches to prevent memory accumulation.
109 - runSegmentPrune(session) - Removes HLS segment files that fall behind the slowest active viewer and clears image subtitle caches whenever the prune cutoff advances, so subtitle buffers follow the same retention rules as video chunks.
109 110 - STREAM_ROOT_DIR - Temporary root directory for active ffmpeg-backed stream sessions.
110 111 - STREAM_SESSION_TTL_MS - Idle lifetime for a stream session before cleanup.
111 112 - STREAM_IDLE_STOP_MS - Maximum client inactivity window before an active stream session is terminated (5 minutes).
@@ -106,6 +106,7 @@
106- createVideoStreamSession(sourcePath) - Probes input metadata and reuses a compatible active transcode session per source file when possible; returns a per-viewer lease id as sessionId plus duration, videoWidth, videoHeight, subtitleTracks, imageSubtitleTracks, and subtitleSummary data.
106- createVideoStreamSession(sourcePath) - Probes input metadata and reuses a compatible active transcode session per source file when possible; returns a per-viewer lease id as sessionId plus duration, videoWidth, videoHeight, subtitleTracks, imageSubtitleTracks, and subtitleSummary data.
107- sendVideoStreamAsset(sessionId, assetPath, req, res) - Resolves viewer leases to a shared stream session, serves HLS assets, tracks lease activity, and prunes segments using the slowest active viewer position.
107- sendVideoStreamAsset(sessionId, assetPath, req, res) - Resolves viewer leases to a shared stream session, serves HLS assets, tracks lease activity, and prunes segments using the slowest active viewer position.
108- releaseVideoStreamSession(sessionId) - Releases one viewer lease; only destroys the underlying transcode session when no active leases remain; cleanup also clears image subtitle frame/event caches to prevent memory accumulation.
108- releaseVideoStreamSession(sessionId) - Releases one viewer lease; only destroys the underlying transcode session when no active leases remain; cleanup also clears image subtitle frame/event caches to prevent memory accumulation.
109- runSegmentPrune(session) - Removes HLS segment files that fall behind the slowest active viewer and clears image subtitle caches whenever the prune cutoff advances, so subtitle buffers follow the same retention rules as video chunks.
109- STREAM_ROOT_DIR - Temporary root directory for active ffmpeg-backed stream sessions.
110- STREAM_ROOT_DIR - Temporary root directory for active ffmpeg-backed stream sessions.
110- STREAM_SESSION_TTL_MS - Idle lifetime for a stream session before cleanup.
111- STREAM_SESSION_TTL_MS - Idle lifetime for a stream session before cleanup.
111- STREAM_IDLE_STOP_MS - Maximum client inactivity window before an active stream session is terminated (5 minutes).
112- STREAM_IDLE_STOP_MS - Maximum client inactivity window before an active stream session is terminated (5 minutes).
.github/tests.md +2 −0
@@ -83,5 +83,7 @@
83 83
84 84 ## `test/video-stream.test.js`
85 85 - `video stream bootstrap`
86 - `sendVideoStreamAsset prunes old segment files after playback moves far ahead`
87 - `createVideoStreamSession reuses one transcode for concurrent viewers of the same file`
86 88
87 89 ---
@@ -83,5 +83,7 @@
83
83
84## `test/video-stream.test.js`
84## `test/video-stream.test.js`
85- `video stream bootstrap`
85- `video stream bootstrap`
86- `sendVideoStreamAsset prunes old segment files after playback moves far ahead`
87- `createVideoStreamSession reuses one transcode for concurrent viewers of the same file`
86
88
87---
89---
server/video-stream.js +7 −0
@@ -214,6 +214,12 @@ function destroySession (sessionId)
214 214 return true;
215 215 }
216 216
217 function clearImageSubtitleCachesForSession (session)
218 {
219 if (!session || !session.sourcePath) return;
220 clearCachesForFile(session.sourcePath);
221 }
222
217 223 function getSessionForLease (leaseId)
218 224 {
219 225 const lease = leases.get(leaseId);
@@ -338,6 +344,7 @@ function runSegmentPrune (session)
338 344 .then(() =>
339 345 {
340 346 session.segmentPrunedUntil = Math.max(session.segmentPrunedUntil, cutoff);
347 clearImageSubtitleCachesForSession(session);
341 348 if (session.segmentPrunedUntil >= 0 && reusableSessionIdBySource.get(session.sourcePath) === session.id)
342 349 {
343 350 reusableSessionIdBySource.delete(session.sourcePath);
@@ -214,6 +214,12 @@ function destroySession (sessionId)
214 return true;
214 return true;
215}
215}
216
216
217function clearImageSubtitleCachesForSession (session)
218{
219 if (!session || !session.sourcePath) return;
220 clearCachesForFile(session.sourcePath);
221}
222
217function getSessionForLease (leaseId)
223function getSessionForLease (leaseId)
218{
224{
219 const lease = leases.get(leaseId);
225 const lease = leases.get(leaseId);
@@ -338,6 +344,7 @@ function runSegmentPrune (session)
338 .then(() =>
344 .then(() =>
339 {
345 {
340 session.segmentPrunedUntil = Math.max(session.segmentPrunedUntil, cutoff);
346 session.segmentPrunedUntil = Math.max(session.segmentPrunedUntil, cutoff);
347 clearImageSubtitleCachesForSession(session);
341 if (session.segmentPrunedUntil >= 0 && reusableSessionIdBySource.get(session.sourcePath) === session.id)
348 if (session.segmentPrunedUntil >= 0 && reusableSessionIdBySource.get(session.sourcePath) === session.id)
342 {
349 {
343 reusableSessionIdBySource.delete(session.sourcePath);
350 reusableSessionIdBySource.delete(session.sourcePath);
tests/video-stream.test.js +6 −0
@@ -9,6 +9,10 @@ jest.mock('child_process', () => ({
9 9 spawn: jest.fn(),
10 10 }));
11 11
12 jest.mock('../server/image-subtitles', () => ({
13 clearCachesForFile: jest.fn(),
14 }));
15
12 16 function makeChildProcess ()
13 17 {
14 18 const child = new EventEmitter();
@@ -402,6 +406,7 @@ describe('video stream bootstrap', () =>
402 406 });
403 407
404 408 const streamModule = require('../server/video-stream');
409 const { clearCachesForFile } = require('../server/image-subtitles');
405 410 const beforeDirs = new Set(
406 411 fs.existsSync(streamModule.STREAM_ROOT_DIR)
407 412 ? fs.readdirSync(streamModule.STREAM_ROOT_DIR)
@@ -433,6 +438,7 @@ describe('video stream bootstrap', () =>
433 438
434 439 expect(fs.existsSync(path.join(sessionDir, 'stream_0_segment_000040.ts'))).toBe(false);
435 440 expect(fs.existsSync(path.join(sessionDir, 'stream_0_segment_000041.ts'))).toBe(true);
441 expect(clearCachesForFile).toHaveBeenCalledWith(sourceFile);
436 442
437 443 streamModule.releaseVideoStreamSession(session.sessionId);
438 444 });
@@ -9,6 +9,10 @@ jest.mock('child_process', () => ({
9 spawn: jest.fn(),
9 spawn: jest.fn(),
10}));
10}));
11
11
12jest.mock('../server/image-subtitles', () => ({
13 clearCachesForFile: jest.fn(),
14}));
15
12function makeChildProcess ()
16function makeChildProcess ()
13{
17{
14 const child = new EventEmitter();
18 const child = new EventEmitter();
@@ -402,6 +406,7 @@ describe('video stream bootstrap', () =>
402 });
406 });
403
407
404 const streamModule = require('../server/video-stream');
408 const streamModule = require('../server/video-stream');
409 const { clearCachesForFile } = require('../server/image-subtitles');
405 const beforeDirs = new Set(
410 const beforeDirs = new Set(
406 fs.existsSync(streamModule.STREAM_ROOT_DIR)
411 fs.existsSync(streamModule.STREAM_ROOT_DIR)
407 ? fs.readdirSync(streamModule.STREAM_ROOT_DIR)
412 ? fs.readdirSync(streamModule.STREAM_ROOT_DIR)
@@ -433,6 +438,7 @@ describe('video stream bootstrap', () =>
433
438
434 expect(fs.existsSync(path.join(sessionDir, 'stream_0_segment_000040.ts'))).toBe(false);
439 expect(fs.existsSync(path.join(sessionDir, 'stream_0_segment_000040.ts'))).toBe(false);
435 expect(fs.existsSync(path.join(sessionDir, 'stream_0_segment_000041.ts'))).toBe(true);
440 expect(fs.existsSync(path.join(sessionDir, 'stream_0_segment_000041.ts'))).toBe(true);
441 expect(clearCachesForFile).toHaveBeenCalledWith(sourceFile);
436
442
437 streamModule.releaseVideoStreamSession(session.sessionId);
443 streamModule.releaseVideoStreamSession(session.sessionId);
438 });
444 });