| 1 | 'use strict'; |
| 2 | |
| 3 | const { EventEmitter } = require('events'); |
| 4 | const fs = require('fs'); |
| 5 | const os = require('os'); |
| 6 | const path = require('path'); |
| 7 | |
| 8 | jest.mock('child_process', () => ({ |
| 9 | spawn: jest.fn(), |
| 10 | })); |
| 11 | |
| 12 | function 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 | |
| 21 | describe('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 | streams: [ |
| 50 | { codec_type: 'video', codec_name: 'h264', tags: {}, disposition: {} }, |
| 51 | { codec_type: 'audio', codec_name: 'aac', tags: {}, disposition: { default: 1 } }, |
| 52 | ], |
| 53 | }))); |
| 54 | child.emit('close', 0); |
| 55 | }); |
| 56 | return child; |
| 57 | } |
| 58 | |
| 59 | expect(exeName.includes('ffmpeg')).toBe(true); |
| 60 | |
| 61 | const outputPattern = String(args[args.length - 1] || ''); |
| 62 | const outputDir = path.dirname(outputPattern); |
| 63 | |
| 64 | process.nextTick(() => |
| 65 | { |
| 66 | fs.mkdirSync(outputDir, { recursive: true }); |
| 67 | setTimeout(() => |
| 68 | { |
| 69 | fs.writeFileSync(path.join(outputDir, 'stream_0.m3u8'), '#EXTM3U\n'); |
| 70 | }, 40); |
| 71 | }); |
| 72 | |
| 73 | return child; |
| 74 | }); |
| 75 | |
| 76 | const { createVideoStreamSession } = require('../server/video-stream'); |
| 77 | const session = await createVideoStreamSession(sourceFile); |
| 78 | |
| 79 | expect(session).toEqual({ |
| 80 | sessionId: expect.any(String), |
| 81 | subtitleTracks: [], |
| 82 | subtitleSummary: { |
| 83 | total: 0, |
| 84 | converted: 0, |
| 85 | skipped: 0, |
| 86 | }, |
| 87 | }); |
| 88 | }); |
| 89 | |
| 90 | test('createVideoStreamSession configures flat segment filenames that match playlist URLs', async () => |
| 91 | { |
| 92 | mockSpawn.mockImplementation((command, args) => |
| 93 | { |
| 94 | const child = makeChildProcess(); |
| 95 | const exeName = path.basename(String(command || '')).toLowerCase(); |
| 96 | |
| 97 | if (exeName.includes('ffprobe')) |
| 98 | { |
| 99 | process.nextTick(() => |
| 100 | { |
| 101 | child.stdout.emit('data', Buffer.from(JSON.stringify({ |
| 102 | streams: [ |
| 103 | { codec_type: 'video', codec_name: 'h264', tags: {}, disposition: {} }, |
| 104 | { codec_type: 'audio', codec_name: 'aac', tags: { handler_name: 'SoundHandler' }, disposition: { default: 1 } }, |
| 105 | ], |
| 106 | }))); |
| 107 | child.emit('close', 0); |
| 108 | }); |
| 109 | return child; |
| 110 | } |
| 111 | |
| 112 | const segmentArgIndex = args.indexOf('-hls_segment_filename'); |
| 113 | expect(segmentArgIndex).toBeGreaterThan(-1); |
| 114 | expect(String(args[segmentArgIndex + 1])).toContain('stream_%v_segment_%06d.ts'); |
| 115 | |
| 116 | const playlistPath = String(args[args.length - 1] || ''); |
| 117 | const outputDir = path.dirname(playlistPath); |
| 118 | process.nextTick(() => |
| 119 | { |
| 120 | fs.mkdirSync(outputDir, { recursive: true }); |
| 121 | fs.writeFileSync(path.join(outputDir, 'master.m3u8'), '#EXTM3U\n'); |
| 122 | }); |
| 123 | |
| 124 | return child; |
| 125 | }); |
| 126 | |
| 127 | const { createVideoStreamSession } = require('../server/video-stream'); |
| 128 | await expect(createVideoStreamSession(sourceFile)).resolves.toEqual({ |
| 129 | sessionId: expect.any(String), |
| 130 | subtitleTracks: [], |
| 131 | subtitleSummary: { |
| 132 | total: 0, |
| 133 | converted: 0, |
| 134 | skipped: 0, |
| 135 | }, |
| 136 | }); |
| 137 | }); |
| 138 | |
| 139 | test('createVideoStreamSession keeps playback available when subtitle conversion fails', async () => |
| 140 | { |
| 141 | mockSpawn.mockImplementation((command, args) => |
| 142 | { |
| 143 | const child = makeChildProcess(); |
| 144 | const exeName = path.basename(String(command || '')).toLowerCase(); |
| 145 | |
| 146 | if (exeName.includes('ffprobe')) |
| 147 | { |
| 148 | process.nextTick(() => |
| 149 | { |
| 150 | child.stdout.emit('data', Buffer.from(JSON.stringify({ |
| 151 | streams: [ |
| 152 | { codec_type: 'video', codec_name: 'h264', tags: {}, disposition: {} }, |
| 153 | { codec_type: 'audio', codec_name: 'aac', tags: {}, disposition: { default: 1 } }, |
| 154 | { codec_type: 'subtitle', codec_name: 'hdmv_pgs_subtitle', tags: { language: 'eng' }, disposition: {} }, |
| 155 | ], |
| 156 | }))); |
| 157 | child.emit('close', 0); |
| 158 | }); |
| 159 | return child; |
| 160 | } |
| 161 | |
| 162 | const mapIndex = args.indexOf('-map'); |
| 163 | const mappedValue = mapIndex > -1 ? String(args[mapIndex + 1] || '') : ''; |
| 164 | |
| 165 | // Fail subtitle extraction but keep HLS transcoding path successful. |
| 166 | if (mappedValue.startsWith('0:s:')) |
| 167 | { |
| 168 | process.nextTick(() => |
| 169 | { |
| 170 | child.stderr.emit('data', Buffer.from('Subtitle codec not supported')); |
| 171 | child.emit('close', 1); |
| 172 | }); |
| 173 | return child; |
| 174 | } |
| 175 | |
| 176 | const playlistPath = String(args[args.length - 1] || ''); |
| 177 | const outputDir = path.dirname(playlistPath); |
| 178 | process.nextTick(() => |
| 179 | { |
| 180 | fs.mkdirSync(outputDir, { recursive: true }); |
| 181 | fs.writeFileSync(path.join(outputDir, 'master.m3u8'), '#EXTM3U\n'); |
| 182 | }); |
| 183 | |
| 184 | return child; |
| 185 | }); |
| 186 | |
| 187 | const { createVideoStreamSession } = require('../server/video-stream'); |
| 188 | await expect(createVideoStreamSession(sourceFile)).resolves.toEqual({ |
| 189 | sessionId: expect.any(String), |
| 190 | subtitleTracks: [], |
| 191 | subtitleSummary: { |
| 192 | total: 1, |
| 193 | converted: 0, |
| 194 | skipped: 1, |
| 195 | }, |
| 196 | }); |
| 197 | }); |
| 198 | }); |