]>
Commit | Line | Data |
---|---|---|
68e70a74 C |
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
2 | ||
3 | import { expect } from 'chai' | |
41fb13c3 | 4 | import ffmpeg, { FfmpegCommand } from 'fluent-ffmpeg' |
68e70a74 | 5 | import { pathExists, readdir } from 'fs-extra' |
68e70a74 | 6 | import { join } from 'path' |
6c5065a0 | 7 | import { buildAbsoluteFixturePath, wait } from '../miscs' |
254d3579 | 8 | import { PeerTubeServer } from '../server/server' |
97969c4e | 9 | |
c826f34a C |
10 | function sendRTMPStream (options: { |
11 | rtmpBaseUrl: string | |
12 | streamKey: string | |
13 | fixtureName?: string // default video_short.mp4 | |
14 | copyCodecs?: boolean // default false | |
15 | }) { | |
16 | const { rtmpBaseUrl, streamKey, fixtureName = 'video_short.mp4', copyCodecs = false } = options | |
17 | ||
ca5c612b | 18 | const fixture = buildAbsoluteFixturePath(fixtureName) |
77e9f859 C |
19 | |
20 | const command = ffmpeg(fixture) | |
21 | command.inputOption('-stream_loop -1') | |
22 | command.inputOption('-re') | |
c826f34a C |
23 | |
24 | if (copyCodecs) { | |
a1c63fe1 C |
25 | command.outputOption('-c copy') |
26 | } else { | |
c826f34a C |
27 | command.outputOption('-c:v libx264') |
28 | command.outputOption('-g 50') | |
29 | command.outputOption('-keyint_min 2') | |
30 | command.outputOption('-r 60') | |
c826f34a C |
31 | } |
32 | ||
77e9f859 C |
33 | command.outputOption('-f flv') |
34 | ||
35 | const rtmpUrl = rtmpBaseUrl + '/' + streamKey | |
36 | command.output(rtmpUrl) | |
37 | ||
38 | command.on('error', err => { | |
39 | if (err?.message?.includes('Exiting normally')) return | |
40 | ||
68e70a74 | 41 | if (process.env.DEBUG) console.error(err) |
77e9f859 C |
42 | }) |
43 | ||
44 | if (process.env.DEBUG) { | |
45 | command.on('stderr', data => console.log(data)) | |
46 | } | |
47 | ||
48 | command.run() | |
49 | ||
50 | return command | |
51 | } | |
52 | ||
41fb13c3 | 53 | function waitFfmpegUntilError (command: FfmpegCommand, successAfterMS = 10000) { |
ba5a8d89 | 54 | return new Promise<void>((res, rej) => { |
97969c4e C |
55 | command.on('error', err => { |
56 | return rej(err) | |
57 | }) | |
58 | ||
59 | setTimeout(() => { | |
60 | res() | |
61 | }, successAfterMS) | |
62 | }) | |
63 | } | |
64 | ||
41fb13c3 | 65 | async function testFfmpegStreamError (command: FfmpegCommand, shouldHaveError: boolean) { |
97969c4e C |
66 | let error: Error |
67 | ||
68 | try { | |
5b23d4e0 | 69 | await waitFfmpegUntilError(command, 35000) |
97969c4e C |
70 | } catch (err) { |
71 | error = err | |
72 | } | |
73 | ||
74 | await stopFfmpeg(command) | |
75 | ||
76 | if (shouldHaveError && !error) throw new Error('Ffmpeg did not have an error') | |
77 | if (!shouldHaveError && error) throw error | |
78 | } | |
79 | ||
41fb13c3 | 80 | async function stopFfmpeg (command: FfmpegCommand) { |
77e9f859 C |
81 | command.kill('SIGINT') |
82 | ||
83 | await wait(500) | |
84 | } | |
85 | ||
254d3579 | 86 | async function waitUntilLivePublishedOnAllServers (servers: PeerTubeServer[], videoId: string) { |
8ebf2a5d | 87 | for (const server of servers) { |
89d241a7 | 88 | await server.live.waitUntilPublished({ videoId }) |
8ebf2a5d C |
89 | } |
90 | } | |
91 | ||
0305db28 JB |
92 | async function waitUntilLiveSavedOnAllServers (servers: PeerTubeServer[], videoId: string) { |
93 | for (const server of servers) { | |
94 | await server.live.waitUntilSaved({ videoId }) | |
95 | } | |
96 | } | |
97 | ||
764b1a14 | 98 | async function checkLiveCleanupAfterSave (server: PeerTubeServer, videoUUID: string, resolutions: number[] = []) { |
89d241a7 | 99 | const basePath = server.servers.buildDirectory('streaming-playlists') |
68e70a74 C |
100 | const hlsPath = join(basePath, 'hls', videoUUID) |
101 | ||
102 | if (resolutions.length === 0) { | |
103 | const result = await pathExists(hlsPath) | |
104 | expect(result).to.be.false | |
105 | ||
106 | return | |
107 | } | |
108 | ||
109 | const files = await readdir(hlsPath) | |
110 | ||
111 | // fragmented file and playlist per resolution + master playlist + segments sha256 json file | |
112 | expect(files).to.have.lengthOf(resolutions.length * 2 + 2) | |
113 | ||
114 | for (const resolution of resolutions) { | |
764b1a14 C |
115 | const fragmentedFile = files.find(f => f.endsWith(`-${resolution}-fragmented.mp4`)) |
116 | expect(fragmentedFile).to.exist | |
117 | ||
118 | const playlistFile = files.find(f => f.endsWith(`${resolution}.m3u8`)) | |
119 | expect(playlistFile).to.exist | |
68e70a74 C |
120 | } |
121 | ||
764b1a14 C |
122 | const masterPlaylistFile = files.find(f => f.endsWith('-master.m3u8')) |
123 | expect(masterPlaylistFile).to.exist | |
124 | ||
125 | const shaFile = files.find(f => f.endsWith('-segments-sha256.json')) | |
126 | expect(shaFile).to.exist | |
68e70a74 C |
127 | } |
128 | ||
77e9f859 | 129 | export { |
4f219914 | 130 | sendRTMPStream, |
97969c4e | 131 | waitFfmpegUntilError, |
4f219914 C |
132 | testFfmpegStreamError, |
133 | stopFfmpeg, | |
8ebf2a5d | 134 | waitUntilLivePublishedOnAllServers, |
0305db28 | 135 | waitUntilLiveSavedOnAllServers, |
764b1a14 | 136 | checkLiveCleanupAfterSave |
77e9f859 | 137 | } |