1 import ffmpeg, { FfmpegCommand } from 'fluent-ffmpeg'
2 import { buildAbsoluteFixturePath, wait } from '@shared/core-utils'
3 import { PeerTubeServer } from '../server/server'
5 function sendRTMPStream (options: {
8 fixtureName?: string // default video_short.mp4
9 copyCodecs?: boolean // default false
11 const { rtmpBaseUrl, streamKey, fixtureName = 'video_short.mp4', copyCodecs = false } = options
13 const fixture = buildAbsoluteFixturePath(fixtureName)
15 const command = ffmpeg(fixture)
16 command.inputOption('-stream_loop -1')
17 command.inputOption('-re')
20 command.outputOption('-c copy')
22 command.outputOption('-c:v libx264')
23 command.outputOption('-g 50')
24 command.outputOption('-keyint_min 2')
25 command.outputOption('-r 60')
28 command.outputOption('-f flv')
30 const rtmpUrl = rtmpBaseUrl + '/' + streamKey
31 command.output(rtmpUrl)
33 command.on('error', err => {
34 if (err?.message?.includes('Exiting normally')) return
36 if (process.env.DEBUG) console.error(err)
39 if (process.env.DEBUG) {
40 command.on('stderr', data => console.log(data))
48 function waitFfmpegUntilError (command: FfmpegCommand, successAfterMS = 10000) {
49 return new Promise<void>((res, rej) => {
50 command.on('error', err => {
60 async function testFfmpegStreamError (command: FfmpegCommand, shouldHaveError: boolean) {
64 await waitFfmpegUntilError(command, 35000)
69 await stopFfmpeg(command)
71 if (shouldHaveError && !error) throw new Error('Ffmpeg did not have an error')
72 if (!shouldHaveError && error) throw error
75 async function stopFfmpeg (command: FfmpegCommand) {
76 command.kill('SIGINT')
81 async function waitUntilLivePublishedOnAllServers (servers: PeerTubeServer[], videoId: string) {
82 for (const server of servers) {
83 await server.live.waitUntilPublished({ videoId })
87 async function waitUntilLiveSavedOnAllServers (servers: PeerTubeServer[], videoId: string) {
88 for (const server of servers) {
89 await server.live.waitUntilSaved({ videoId })
96 testFfmpegStreamError,
98 waitUntilLivePublishedOnAllServers,
99 waitUntilLiveSavedOnAllServers