Tear down zero-viewer streams immediately
Stop retaining stream sessions after the last viewer closes playback. When lease count reaches zero, the stream session now tears down immediately instead of waiting for in-flight request counters or idle timers. This clears stream output and subtitle caches as soon as viewers are gone, matching expected close behaviour.\n\nAlso harden request-finish handling so late response callbacks are safe after session teardown, and add regression coverage proving zero-viewer sessions are removed immediately even when an asset response is still finishing.
@@ -150,7 +150,7 @@
150
150
## server/video-stream.js
151
151
- 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, including same-name sidecar `.srt` subtitles converted to WebVTT.
152
152
- 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.
153
- releaseVideoStreamSession(sessionId) - Releases one viewer lease; only destroys the underlying transcode session when no active leases remain; cleanup only clears image subtitle frame/event caches once no other active stream session for the same source file remains.
153
- releaseVideoStreamSession(sessionId) - Releases one viewer lease and destroys the underlying transcode session immediately once lease count drops to zero (even if a response is still finishing) so zero-viewer streams are purged right away; cleanup only clears image subtitle frame/event caches once no other active stream session for the same source file remains.
154
154
- parseSegmentIndexFromAssetPath(assetPath) - Extracts the numeric segment index from an HLS segment filename; matches both numeric variant identifiers (stream_0_segment_000000.ts) and named variants (stream_video_segment_000000.ts, stream_Audio-1-jpn_segment_000000.ts) — ffmpeg substitutes the var_stream_map name: value for %v when named streams are used.
155
155
- runSegmentPrune(session) - Removes HLS segment files that fall behind the slowest active viewer and calls pruneImageSubtitleCachesForSession() using the slowest active viewer across all live sessions for the same source file, so file-wide subtitle buffers are not pruned ahead of a newly started second session; segment deletion now runs in bounded batches and records last prune duration/deleted-segment counts for diagnostics; segment filename regex also uses named-variant pattern to match production filenames correctly.
156
156
- hasInitialHlsAssets(outputDir) - Returns true once master.m3u8 or at least one variant playlist/segment exists in an HLS output directory; accepts named variant filenames (stream_video.m3u8) in addition to numeric ones.
@@ -150,7 +150,7 @@150
## server/video-stream.js
150
## server/video-stream.js
151
- 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, including same-name sidecar `.srt` subtitles converted to WebVTT.
151
- 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, including same-name sidecar `.srt` subtitles converted to WebVTT.
152
- 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.
152
- 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.
153
- releaseVideoStreamSession(sessionId) - Releases one viewer lease; only destroys the underlying transcode session when no active leases remain; cleanup only clears image subtitle frame/event caches once no other active stream session for the same source file remains.
153
- releaseVideoStreamSession(sessionId) - Releases one viewer lease and destroys the underlying transcode session immediately once lease count drops to zero (even if a response is still finishing) so zero-viewer streams are purged right away; cleanup only clears image subtitle frame/event caches once no other active stream session for the same source file remains.
154
- parseSegmentIndexFromAssetPath(assetPath) - Extracts the numeric segment index from an HLS segment filename; matches both numeric variant identifiers (stream_0_segment_000000.ts) and named variants (stream_video_segment_000000.ts, stream_Audio-1-jpn_segment_000000.ts) — ffmpeg substitutes the var_stream_map name: value for %v when named streams are used.
154
- parseSegmentIndexFromAssetPath(assetPath) - Extracts the numeric segment index from an HLS segment filename; matches both numeric variant identifiers (stream_0_segment_000000.ts) and named variants (stream_video_segment_000000.ts, stream_Audio-1-jpn_segment_000000.ts) — ffmpeg substitutes the var_stream_map name: value for %v when named streams are used.
155
- runSegmentPrune(session) - Removes HLS segment files that fall behind the slowest active viewer and calls pruneImageSubtitleCachesForSession() using the slowest active viewer across all live sessions for the same source file, so file-wide subtitle buffers are not pruned ahead of a newly started second session; segment deletion now runs in bounded batches and records last prune duration/deleted-segment counts for diagnostics; segment filename regex also uses named-variant pattern to match production filenames correctly.
155
- runSegmentPrune(session) - Removes HLS segment files that fall behind the slowest active viewer and calls pruneImageSubtitleCachesForSession() using the slowest active viewer across all live sessions for the same source file, so file-wide subtitle buffers are not pruned ahead of a newly started second session; segment deletion now runs in bounded batches and records last prune duration/deleted-segment counts for diagnostics; segment filename regex also uses named-variant pattern to match production filenames correctly.
156
- hasInitialHlsAssets(outputDir) - Returns true once master.m3u8 or at least one variant playlist/segment exists in an HLS output directory; accepts named variant filenames (stream_video.m3u8) in addition to numeric ones.
156
- hasInitialHlsAssets(outputDir) - Returns true once master.m3u8 or at least one variant playlist/segment exists in an HLS output directory; accepts named variant filenames (stream_video.m3u8) in addition to numeric ones.
@@ -409,8 +409,15 @@ function startAssetRequest (session, lease)
409
409
function finishAssetRequest (session, lease)
410
410
{
411
411
if (session.activeRequests > 0) session.activeRequests -= 1;
412
touchSessionActivity(session, lease);
413
if (session.activeRequests === 0 && session.leaseIds.size === 0) scheduleIdleStop(session);
412
413
const activeSession = sessions.get(session.id);
414
if (!activeSession) return;
415
416
touchSessionActivity(activeSession, lease);
417
if (activeSession.activeRequests === 0 && activeSession.leaseIds.size === 0)
418
{
419
destroySession(activeSession.id);
420
}
414
421
}
415
422
416
423
function parseSegmentIndexFromAssetPath (assetPath)
@@ -1181,7 +1188,7 @@ function releaseVideoStreamSession (sessionId)
1181
1188
leases.delete(leaseId);
1182
1189
session.leaseIds.delete(leaseId);
1183
1190
1184
if (session.leaseIds.size === 0 && session.activeRequests === 0)
1191
if (session.leaseIds.size === 0)
1185
1192
{
1186
1193
return destroySession(session.id);
1187
1194
}
@@ -409,8 +409,15 @@ function startAssetRequest (session, lease)409
function finishAssetRequest (session, lease)409
function finishAssetRequest (session, lease)410
{410
{411
if (session.activeRequests > 0) session.activeRequests -= 1;411
if (session.activeRequests > 0) session.activeRequests -= 1;412
touchSessionActivity(session, lease);412
413
if (session.activeRequests === 0 && session.leaseIds.size === 0) scheduleIdleStop(session);413
const activeSession = sessions.get(session.id);414
if (!activeSession) return;415
416
touchSessionActivity(activeSession, lease);417
if (activeSession.activeRequests === 0 && activeSession.leaseIds.size === 0)418
{419
destroySession(activeSession.id);420
}414
}421
}415
422
416
function parseSegmentIndexFromAssetPath (assetPath)423
function parseSegmentIndexFromAssetPath (assetPath)@@ -1181,7 +1188,7 @@ function releaseVideoStreamSession (sessionId)1181
leases.delete(leaseId);1188
leases.delete(leaseId);1182
session.leaseIds.delete(leaseId);1189
session.leaseIds.delete(leaseId);1183
1190
1184
if (session.leaseIds.size === 0 && session.activeRequests === 0)1191
if (session.leaseIds.size === 0)1185
{1192
{1186
return destroySession(session.id);1193
return destroySession(session.id);1187
}1194
}@@ -896,4 +896,70 @@ describe('video stream bootstrap', () =>
896
896
streamModule.releaseVideoStreamSession(secondViewer.sessionId);
897
897
expect(clearCachesForFile).toHaveBeenCalledWith(sourceFile);
898
898
});
899
900
test('destroys zero-viewer sessions immediately even with an in-flight asset response', async () =>
901
{
902
mockSpawn.mockImplementation((command, args) =>
903
{
904
const child = makeChildProcess();
905
const exeName = path.basename(String(command || '')).toLowerCase();
906
907
if (exeName.includes('ffprobe'))
908
{
909
process.nextTick(() =>
910
{
911
child.stdout.emit('data', Buffer.from(JSON.stringify({
912
streams: [
913
{ codec_type: 'video', codec_name: 'h264', tags: {}, disposition: {} },
914
],
915
})));
916
child.emit('close', 0);
917
});
918
return child;
919
}
920
921
const playlistPath = String(args[args.length - 1] || '');
922
const outputDir = path.dirname(playlistPath);
923
process.nextTick(() =>
924
{
925
fs.mkdirSync(outputDir, { recursive: true });
926
fs.writeFileSync(path.join(outputDir, 'master.m3u8'), '#EXTM3U\n');
927
});
928
return child;
929
});
930
931
const streamModule = require('../server/video-stream');
932
const { clearCachesForFile } = require('../server/image-subtitles');
933
const session = await streamModule.createVideoStreamSession(sourceFile);
934
clearCachesForFile.mockClear();
935
936
const req = new EventEmitter();
937
const res = new EventEmitter();
938
let finishSendFile;
939
940
res.set = () => res;
941
res.type = () => res;
942
res.sendFile = (targetPath, cb) =>
943
{
944
finishSendFile = () =>
945
{
946
res.emit('finish');
947
cb();
948
};
949
expect(fs.existsSync(targetPath)).toBe(true);
950
};
951
952
const assetPromise = streamModule.sendVideoStreamAsset(session.sessionId, 'master.m3u8', req, res);
953
await new Promise((resolve) => setImmediate(resolve));
954
955
streamModule.releaseVideoStreamSession(session.sessionId);
956
957
const debugAfterRelease = streamModule.getVideoStreamDebugSnapshot();
958
expect(debugAfterRelease.activeSessionCount).toBe(0);
959
expect(debugAfterRelease.activeLeaseCount).toBe(0);
960
expect(clearCachesForFile).toHaveBeenCalledWith(sourceFile);
961
962
finishSendFile();
963
await expect(assetPromise).resolves.toBeUndefined();
964
});
899
965
});
@@ -896,4 +896,70 @@ describe('video stream bootstrap', () =>896
streamModule.releaseVideoStreamSession(secondViewer.sessionId);896
streamModule.releaseVideoStreamSession(secondViewer.sessionId);897
expect(clearCachesForFile).toHaveBeenCalledWith(sourceFile);897
expect(clearCachesForFile).toHaveBeenCalledWith(sourceFile);898
});898
});899
900
test('destroys zero-viewer sessions immediately even with an in-flight asset response', async () =>901
{902
mockSpawn.mockImplementation((command, args) =>903
{904
const child = makeChildProcess();905
const exeName = path.basename(String(command || '')).toLowerCase();906
907
if (exeName.includes('ffprobe'))908
{909
process.nextTick(() =>910
{911
child.stdout.emit('data', Buffer.from(JSON.stringify({912
streams: [913
{ codec_type: 'video', codec_name: 'h264', tags: {}, disposition: {} },914
],915
})));916
child.emit('close', 0);917
});918
return child;919
}920
921
const playlistPath = String(args[args.length - 1] || '');922
const outputDir = path.dirname(playlistPath);923
process.nextTick(() =>924
{925
fs.mkdirSync(outputDir, { recursive: true });926
fs.writeFileSync(path.join(outputDir, 'master.m3u8'), '#EXTM3U\n');927
});928
return child;929
});930
931
const streamModule = require('../server/video-stream');932
const { clearCachesForFile } = require('../server/image-subtitles');933
const session = await streamModule.createVideoStreamSession(sourceFile);934
clearCachesForFile.mockClear();935
936
const req = new EventEmitter();937
const res = new EventEmitter();938
let finishSendFile;939
940
res.set = () => res;941
res.type = () => res;942
res.sendFile = (targetPath, cb) =>943
{944
finishSendFile = () =>945
{946
res.emit('finish');947
cb();948
};949
expect(fs.existsSync(targetPath)).toBe(true);950
};951
952
const assetPromise = streamModule.sendVideoStreamAsset(session.sessionId, 'master.m3u8', req, res);953
await new Promise((resolve) => setImmediate(resolve));954
955
streamModule.releaseVideoStreamSession(session.sessionId);956
957
const debugAfterRelease = streamModule.getVideoStreamDebugSnapshot();958
expect(debugAfterRelease.activeSessionCount).toBe(0);959
expect(debugAfterRelease.activeLeaseCount).toBe(0);960
expect(clearCachesForFile).toHaveBeenCalledWith(sourceFile);961
962
finishSendFile();963
await expect(assetPromise).resolves.toBeUndefined();964
});899
});965
});