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