BlueGit

timfa filemanager

Files

filemanager / tests / video-stream.test.js

video-stream.test.js

fix jumping partially
timfa Tim Falken · pushed by timfa · 6 months ago
ec1bfab7 History
video-stream.test.js 9.5 KiB Download Raw
1'use strict';
2
3const { EventEmitter } = require('events');
4const fs = require('fs');
5const os = require('os');
6const path = require('path');
7
8jest.mock('child_process', () => ({
9 spawn: jest.fn(),
10}));
11
12function makeChildProcess ()
13{
14 const child = new EventEmitter();
15 child.stdout = new EventEmitter();
16 child.stderr = new EventEmitter();
17 child.kill = jest.fn();
18 return child;
19}
20
21describe('video stream bootstrap', () =>
22{
23 let mockSpawn;
24 let sourceFile;
25
26 beforeEach(() =>
27 {
28 jest.resetModules();
29 mockSpawn = require('child_process').spawn;
30 mockSpawn.mockReset();
31
32 const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'filemanager-video-stream-test-'));
33 sourceFile = path.join(tmpDir, 'clip.mkv');
34 fs.writeFileSync(sourceFile, 'video');
35 });
36
37 test('createVideoStreamSession resolves once ffmpeg starts writing stream assets', async () =>
38 {
39 mockSpawn.mockImplementation((command, args) =>
40 {
41 const child = makeChildProcess();
42 const exeName = path.basename(String(command || '')).toLowerCase();
43
44 if (exeName.includes('ffprobe'))
45 {
46 process.nextTick(() =>
47 {
48 child.stdout.emit('data', Buffer.from(JSON.stringify({
49 format: { duration: '12.5' },
50 streams: [
51 { codec_type: 'video', codec_name: 'h264', tags: {}, disposition: {} },
52 { codec_type: 'audio', codec_name: 'aac', tags: {}, disposition: { default: 1 } },
53 ],
54 })));
55 child.emit('close', 0);
56 });
57 return child;
58 }
59
60 expect(exeName.includes('ffmpeg')).toBe(true);
61
62 const outputPattern = String(args[args.length - 1] || '');
63 const outputDir = path.dirname(outputPattern);
64
65 process.nextTick(() =>
66 {
67 fs.mkdirSync(outputDir, { recursive: true });
68 setTimeout(() =>
69 {
70 fs.writeFileSync(path.join(outputDir, 'stream_0.m3u8'), '#EXTM3U\n');
71 }, 40);
72 });
73
74 return child;
75 });
76
77 const { createVideoStreamSession } = require('../server/video-stream');
78 const session = await createVideoStreamSession(sourceFile);
79
80 expect(session).toEqual({
81 sessionId: expect.any(String),
82 durationSeconds: 12.5,
83 subtitleTracks: [],
84 subtitleSummary: {
85 total: 0,
86 converted: 0,
87 skipped: 0,
88 },
89 });
90 });
91
92 test('createVideoStreamSession configures flat segment filenames that match playlist URLs', async () =>
93 {
94 mockSpawn.mockImplementation((command, args) =>
95 {
96 const child = makeChildProcess();
97 const exeName = path.basename(String(command || '')).toLowerCase();
98
99 if (exeName.includes('ffprobe'))
100 {
101 process.nextTick(() =>
102 {
103 child.stdout.emit('data', Buffer.from(JSON.stringify({
104 streams: [
105 { codec_type: 'video', codec_name: 'h264', tags: {}, disposition: {} },
106 { codec_type: 'audio', codec_name: 'aac', tags: { handler_name: 'SoundHandler' }, disposition: { default: 1 } },
107 ],
108 })));
109 child.emit('close', 0);
110 });
111 return child;
112 }
113
114 const segmentArgIndex = args.indexOf('-hls_segment_filename');
115 expect(segmentArgIndex).toBeGreaterThan(-1);
116 expect(String(args[segmentArgIndex + 1])).toContain('stream_%v_segment_%06d.ts');
117 expect(args).not.toContain('-hls_playlist_type');
118
119 const playlistPath = String(args[args.length - 1] || '');
120 const outputDir = path.dirname(playlistPath);
121 process.nextTick(() =>
122 {
123 fs.mkdirSync(outputDir, { recursive: true });
124 fs.writeFileSync(path.join(outputDir, 'master.m3u8'), '#EXTM3U\n');
125 });
126
127 return child;
128 });
129
130 const { createVideoStreamSession } = require('../server/video-stream');
131 await expect(createVideoStreamSession(sourceFile)).resolves.toEqual({
132 sessionId: expect.any(String),
133 durationSeconds: null,
134 subtitleTracks: [],
135 subtitleSummary: {
136 total: 0,
137 converted: 0,
138 skipped: 0,
139 },
140 });
141 });
142
143 test('createVideoStreamSession reads duration from ffprobe duration tags', async () =>
144 {
145 mockSpawn.mockImplementation((command, args) =>
146 {
147 const child = makeChildProcess();
148 const exeName = path.basename(String(command || '')).toLowerCase();
149
150 if (exeName.includes('ffprobe'))
151 {
152 process.nextTick(() =>
153 {
154 child.stdout.emit('data', Buffer.from(JSON.stringify({
155 streams: [
156 { codec_type: 'video', codec_name: 'h264', tags: { DURATION: '00:22:17.500000000' }, disposition: {} },
157 { codec_type: 'audio', codec_name: 'aac', tags: {}, disposition: { default: 1 } },
158 ],
159 })));
160 child.emit('close', 0);
161 });
162 return child;
163 }
164
165 const playlistPath = String(args[args.length - 1] || '');
166 const outputDir = path.dirname(playlistPath);
167 process.nextTick(() =>
168 {
169 fs.mkdirSync(outputDir, { recursive: true });
170 fs.writeFileSync(path.join(outputDir, 'master.m3u8'), '#EXTM3U\n');
171 });
172
173 return child;
174 });
175
176 const { createVideoStreamSession } = require('../server/video-stream');
177 await expect(createVideoStreamSession(sourceFile)).resolves.toMatchObject({
178 durationSeconds: 1337.5,
179 });
180 });
181
182 test('createVideoStreamSession does not become ready from subtitle files alone', async () =>
183 {
184 mockSpawn.mockImplementation((command, args) =>
185 {
186 const child = makeChildProcess();
187 const exeName = path.basename(String(command || '')).toLowerCase();
188
189 if (exeName.includes('ffprobe'))
190 {
191 process.nextTick(() =>
192 {
193 child.stdout.emit('data', Buffer.from(JSON.stringify({
194 streams: [
195 { codec_type: 'video', codec_name: 'h264', tags: {}, disposition: {} },
196 { codec_type: 'audio', codec_name: 'aac', tags: {}, disposition: { default: 1 } },
197 { codec_type: 'subtitle', codec_name: 'subrip', tags: { language: 'eng' }, disposition: {} },
198 ],
199 })));
200 child.emit('close', 0);
201 });
202 return child;
203 }
204
205 const mapIndex = args.indexOf('-map');
206 const mappedValue = mapIndex > -1 ? String(args[mapIndex + 1] || '') : '';
207
208 if (mappedValue.startsWith('0:s:'))
209 {
210 const subtitleTarget = String(args[args.length - 1] || '');
211 process.nextTick(() =>
212 {
213 fs.mkdirSync(path.dirname(subtitleTarget), { recursive: true });
214 fs.writeFileSync(subtitleTarget, 'WEBVTT\n\n');
215 child.emit('close', 0);
216 });
217 return child;
218 }
219
220 const playlistPath = String(args[args.length - 1] || '');
221 const outputDir = path.dirname(playlistPath);
222 process.nextTick(() =>
223 {
224 fs.mkdirSync(outputDir, { recursive: true });
225 setTimeout(() =>
226 {
227 fs.writeFileSync(path.join(outputDir, 'master.m3u8'), '#EXTM3U\n');
228 }, 80);
229 });
230
231 return child;
232 });
233
234 const { createVideoStreamSession } = require('../server/video-stream');
235 const startedAt = Date.now();
236 const session = await createVideoStreamSession(sourceFile);
237 const elapsedMs = Date.now() - startedAt;
238
239 expect(elapsedMs).toBeGreaterThanOrEqual(60);
240 expect(session).toEqual({
241 sessionId: expect.any(String),
242 durationSeconds: null,
243 subtitleTracks: [
244 expect.objectContaining({
245 fileName: 'subtitle-0.vtt',
246 language: 'eng',
247 }),
248 ],
249 subtitleSummary: {
250 total: 1,
251 converted: 1,
252 skipped: 0,
253 },
254 });
255 });
256
257 test('createVideoStreamSession keeps playback available when subtitle conversion fails', async () =>
258 {
259 mockSpawn.mockImplementation((command, args) =>
260 {
261 const child = makeChildProcess();
262 const exeName = path.basename(String(command || '')).toLowerCase();
263
264 if (exeName.includes('ffprobe'))
265 {
266 process.nextTick(() =>
267 {
268 child.stdout.emit('data', Buffer.from(JSON.stringify({
269 streams: [
270 { codec_type: 'video', codec_name: 'h264', tags: {}, disposition: {} },
271 { codec_type: 'audio', codec_name: 'aac', tags: {}, disposition: { default: 1 } },
272 { codec_type: 'subtitle', codec_name: 'hdmv_pgs_subtitle', tags: { language: 'eng' }, disposition: {} },
273 ],
274 })));
275 child.emit('close', 0);
276 });
277 return child;
278 }
279
280 const mapIndex = args.indexOf('-map');
281 const mappedValue = mapIndex > -1 ? String(args[mapIndex + 1] || '') : '';
282
283 // Fail subtitle extraction but keep HLS transcoding path successful.
284 if (mappedValue.startsWith('0:s:'))
285 {
286 process.nextTick(() =>
287 {
288 child.stderr.emit('data', Buffer.from('Subtitle codec not supported'));
289 child.emit('close', 1);
290 });
291 return child;
292 }
293
294 const playlistPath = String(args[args.length - 1] || '');
295 const outputDir = path.dirname(playlistPath);
296 process.nextTick(() =>
297 {
298 fs.mkdirSync(outputDir, { recursive: true });
299 fs.writeFileSync(path.join(outputDir, 'master.m3u8'), '#EXTM3U\n');
300 });
301
302 return child;
303 });
304
305 const { createVideoStreamSession } = require('../server/video-stream');
306 await expect(createVideoStreamSession(sourceFile)).resolves.toEqual({
307 sessionId: expect.any(String),
308 durationSeconds: null,
309 subtitleTracks: [],
310 subtitleSummary: {
311 total: 1,
312 converted: 0,
313 skipped: 1,
314 },
315 });
316 });
317});