]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/server-commands/videos/live.ts
Put private videos under a specific subdirectory
[github/Chocobozzz/PeerTube.git] / shared / server-commands / videos / live.ts
CommitLineData
41fb13c3 1import ffmpeg, { FfmpegCommand } from 'fluent-ffmpeg'
c55e3d72 2import { buildAbsoluteFixturePath, wait } from '@shared/core-utils'
3545e72c 3import { VideoDetails, VideoInclude, VideoPrivacy } from '@shared/models'
8bd6aa04 4import { PeerTubeServer } from '../server/server'
97969c4e 5
c826f34a
C
6function 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 49function 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 61async 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 76async function stopFfmpeg (command: FfmpegCommand) {
77e9f859
C
77 command.kill('SIGINT')
78
79 await wait(500)
80}
81
254d3579 82async 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 88async 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
94async function waitUntilLiveReplacedByReplayOnAllServers (servers: PeerTubeServer[], videoId: string) {
95 for (const server of servers) {
96 await server.live.waitUntilReplacedByReplay({ videoId })
97 }
98}
99
100async function findExternalSavedVideo (server: PeerTubeServer, liveDetails: VideoDetails) {
3545e72c
C
101 const include = VideoInclude.BLACKLISTED
102 const privacyOneOf = [ VideoPrivacy.INTERNAL, VideoPrivacy.PRIVATE, VideoPrivacy.PUBLIC, VideoPrivacy.UNLISTED ]
103
104 const { data } = await server.videos.list({ token: server.accessToken, sort: '-publishedAt', include, privacyOneOf })
4ec52d04
C
105
106 return data.find(v => v.name === liveDetails.name + ' - ' + new Date(liveDetails.publishedAt).toLocaleString())
107}
108
77e9f859 109export {
4f219914 110 sendRTMPStream,
97969c4e 111 waitFfmpegUntilError,
4f219914
C
112 testFfmpegStreamError,
113 stopFfmpeg,
4ec52d04 114
8ebf2a5d 115 waitUntilLivePublishedOnAllServers,
4ec52d04
C
116 waitUntilLiveReplacedByReplayOnAllServers,
117 waitUntilLiveWaitingOnAllServers,
118
119 findExternalSavedVideo
77e9f859 120}