]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import ffmpeg, { FfmpegCommand } from 'fluent-ffmpeg' |
c55e3d72 | 2 | import { buildAbsoluteFixturePath, wait } from '@shared/core-utils' |
254d3579 | 3 | import { PeerTubeServer } from '../server/server' |
97969c4e | 4 | |
c826f34a C |
5 | function sendRTMPStream (options: { |
6 | rtmpBaseUrl: string | |
7 | streamKey: string | |
8 | fixtureName?: string // default video_short.mp4 | |
9 | copyCodecs?: boolean // default false | |
10 | }) { | |
11 | const { rtmpBaseUrl, streamKey, fixtureName = 'video_short.mp4', copyCodecs = false } = options | |
12 | ||
ca5c612b | 13 | const fixture = buildAbsoluteFixturePath(fixtureName) |
77e9f859 C |
14 | |
15 | const command = ffmpeg(fixture) | |
16 | command.inputOption('-stream_loop -1') | |
17 | command.inputOption('-re') | |
c826f34a C |
18 | |
19 | if (copyCodecs) { | |
a1c63fe1 C |
20 | command.outputOption('-c copy') |
21 | } else { | |
c826f34a C |
22 | command.outputOption('-c:v libx264') |
23 | command.outputOption('-g 50') | |
24 | command.outputOption('-keyint_min 2') | |
25 | command.outputOption('-r 60') | |
c826f34a C |
26 | } |
27 | ||
77e9f859 C |
28 | command.outputOption('-f flv') |
29 | ||
30 | const rtmpUrl = rtmpBaseUrl + '/' + streamKey | |
31 | command.output(rtmpUrl) | |
32 | ||
33 | command.on('error', err => { | |
34 | if (err?.message?.includes('Exiting normally')) return | |
35 | ||
68e70a74 | 36 | if (process.env.DEBUG) console.error(err) |
77e9f859 C |
37 | }) |
38 | ||
39 | if (process.env.DEBUG) { | |
40 | command.on('stderr', data => console.log(data)) | |
41 | } | |
42 | ||
43 | command.run() | |
44 | ||
45 | return command | |
46 | } | |
47 | ||
41fb13c3 | 48 | function waitFfmpegUntilError (command: FfmpegCommand, successAfterMS = 10000) { |
ba5a8d89 | 49 | return new Promise<void>((res, rej) => { |
97969c4e C |
50 | command.on('error', err => { |
51 | return rej(err) | |
52 | }) | |
53 | ||
54 | setTimeout(() => { | |
55 | res() | |
56 | }, successAfterMS) | |
57 | }) | |
58 | } | |
59 | ||
41fb13c3 | 60 | async function testFfmpegStreamError (command: FfmpegCommand, shouldHaveError: boolean) { |
97969c4e C |
61 | let error: Error |
62 | ||
63 | try { | |
5b23d4e0 | 64 | await waitFfmpegUntilError(command, 35000) |
97969c4e C |
65 | } catch (err) { |
66 | error = err | |
67 | } | |
68 | ||
69 | await stopFfmpeg(command) | |
70 | ||
71 | if (shouldHaveError && !error) throw new Error('Ffmpeg did not have an error') | |
72 | if (!shouldHaveError && error) throw error | |
73 | } | |
74 | ||
41fb13c3 | 75 | async function stopFfmpeg (command: FfmpegCommand) { |
77e9f859 C |
76 | command.kill('SIGINT') |
77 | ||
78 | await wait(500) | |
79 | } | |
80 | ||
254d3579 | 81 | async function waitUntilLivePublishedOnAllServers (servers: PeerTubeServer[], videoId: string) { |
8ebf2a5d | 82 | for (const server of servers) { |
89d241a7 | 83 | await server.live.waitUntilPublished({ videoId }) |
8ebf2a5d C |
84 | } |
85 | } | |
86 | ||
0305db28 JB |
87 | async function waitUntilLiveSavedOnAllServers (servers: PeerTubeServer[], videoId: string) { |
88 | for (const server of servers) { | |
89 | await server.live.waitUntilSaved({ videoId }) | |
90 | } | |
91 | } | |
92 | ||
77e9f859 | 93 | export { |
4f219914 | 94 | sendRTMPStream, |
97969c4e | 95 | waitFfmpegUntilError, |
4f219914 C |
96 | testFfmpegStreamError, |
97 | stopFfmpeg, | |
8ebf2a5d | 98 | waitUntilLivePublishedOnAllServers, |
c55e3d72 | 99 | waitUntilLiveSavedOnAllServers |
77e9f859 | 100 | } |