]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import ffmpeg, { FfmpegCommand } from 'fluent-ffmpeg' |
c55e3d72 | 2 | import { buildAbsoluteFixturePath, wait } from '@shared/core-utils' |
3545e72c | 3 | import { VideoDetails, VideoInclude, VideoPrivacy } from '@shared/models' |
8bd6aa04 | 4 | import { PeerTubeServer } from '../server/server' |
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)) | |
2732eeff | 42 | command.on('stdout', data => console.log(data)) |
77e9f859 C |
43 | } |
44 | ||
45 | command.run() | |
46 | ||
47 | return command | |
48 | } | |
49 | ||
41fb13c3 | 50 | function waitFfmpegUntilError (command: FfmpegCommand, successAfterMS = 10000) { |
ba5a8d89 | 51 | return new Promise<void>((res, rej) => { |
97969c4e C |
52 | command.on('error', err => { |
53 | return rej(err) | |
54 | }) | |
55 | ||
56 | setTimeout(() => { | |
57 | res() | |
58 | }, successAfterMS) | |
59 | }) | |
60 | } | |
61 | ||
41fb13c3 | 62 | async function testFfmpegStreamError (command: FfmpegCommand, shouldHaveError: boolean) { |
97969c4e C |
63 | let error: Error |
64 | ||
65 | try { | |
5b23d4e0 | 66 | await waitFfmpegUntilError(command, 35000) |
97969c4e C |
67 | } catch (err) { |
68 | error = err | |
69 | } | |
70 | ||
71 | await stopFfmpeg(command) | |
72 | ||
73 | if (shouldHaveError && !error) throw new Error('Ffmpeg did not have an error') | |
74 | if (!shouldHaveError && error) throw error | |
75 | } | |
76 | ||
41fb13c3 | 77 | async function stopFfmpeg (command: FfmpegCommand) { |
77e9f859 C |
78 | command.kill('SIGINT') |
79 | ||
80 | await wait(500) | |
81 | } | |
82 | ||
254d3579 | 83 | async function waitUntilLivePublishedOnAllServers (servers: PeerTubeServer[], videoId: string) { |
8ebf2a5d | 84 | for (const server of servers) { |
89d241a7 | 85 | await server.live.waitUntilPublished({ videoId }) |
8ebf2a5d C |
86 | } |
87 | } | |
88 | ||
4ec52d04 | 89 | async function waitUntilLiveWaitingOnAllServers (servers: PeerTubeServer[], videoId: string) { |
0305db28 | 90 | for (const server of servers) { |
4ec52d04 | 91 | await server.live.waitUntilWaiting({ videoId }) |
0305db28 JB |
92 | } |
93 | } | |
94 | ||
4ec52d04 C |
95 | async function waitUntilLiveReplacedByReplayOnAllServers (servers: PeerTubeServer[], videoId: string) { |
96 | for (const server of servers) { | |
97 | await server.live.waitUntilReplacedByReplay({ videoId }) | |
98 | } | |
99 | } | |
100 | ||
101 | async function findExternalSavedVideo (server: PeerTubeServer, liveDetails: VideoDetails) { | |
3545e72c C |
102 | const include = VideoInclude.BLACKLISTED |
103 | const privacyOneOf = [ VideoPrivacy.INTERNAL, VideoPrivacy.PRIVATE, VideoPrivacy.PUBLIC, VideoPrivacy.UNLISTED ] | |
104 | ||
105 | const { data } = await server.videos.list({ token: server.accessToken, sort: '-publishedAt', include, privacyOneOf }) | |
4ec52d04 C |
106 | |
107 | return data.find(v => v.name === liveDetails.name + ' - ' + new Date(liveDetails.publishedAt).toLocaleString()) | |
108 | } | |
109 | ||
77e9f859 | 110 | export { |
4f219914 | 111 | sendRTMPStream, |
97969c4e | 112 | waitFfmpegUntilError, |
4f219914 C |
113 | testFfmpegStreamError, |
114 | stopFfmpeg, | |
4ec52d04 | 115 | |
8ebf2a5d | 116 | waitUntilLivePublishedOnAllServers, |
4ec52d04 C |
117 | waitUntilLiveReplacedByReplayOnAllServers, |
118 | waitUntilLiveWaitingOnAllServers, | |
119 | ||
120 | findExternalSavedVideo | |
77e9f859 | 121 | } |