]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/videos/live.ts
Add check constraints live tests
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / videos / live.ts
CommitLineData
77e9f859 1import * as ffmpeg from 'fluent-ffmpeg'
97969c4e
C
2import { omit } from 'lodash'
3import { LiveVideo, LiveVideoCreate, LiveVideoUpdate, VideoDetails, VideoState } from '@shared/models'
77e9f859
C
4import { buildAbsoluteFixturePath, wait } from '../miscs/miscs'
5import { makeGetRequest, makePutBodyRequest, makeUploadRequest } from '../requests/requests'
af4ae64f 6import { getVideoWithToken } from './videos'
77e9f859
C
7
8function getLive (url: string, token: string, videoId: number | string, statusCodeExpected = 200) {
9 const path = '/api/v1/videos/live'
10
11 return makeGetRequest({
12 url,
13 token,
14 path: path + '/' + videoId,
15 statusCodeExpected
16 })
17}
18
19function updateLive (url: string, token: string, videoId: number | string, fields: LiveVideoUpdate, statusCodeExpected = 204) {
20 const path = '/api/v1/videos/live'
21
22 return makePutBodyRequest({
23 url,
24 token,
25 path: path + '/' + videoId,
26 fields,
27 statusCodeExpected
28 })
29}
30
31function createLive (url: string, token: string, fields: LiveVideoCreate, statusCodeExpected = 200) {
32 const path = '/api/v1/videos/live'
33
af4ae64f
C
34 const attaches: any = {}
35 if (fields.thumbnailfile) attaches.thumbnailfile = fields.thumbnailfile
36 if (fields.previewfile) attaches.previewfile = fields.previewfile
37
38 const updatedFields = omit(fields, 'thumbnailfile', 'previewfile')
77e9f859
C
39
40 return makeUploadRequest({
41 url,
42 path,
43 token,
44 attaches,
af4ae64f 45 fields: updatedFields,
77e9f859
C
46 statusCodeExpected
47 })
48}
49
97969c4e
C
50async function sendRTMPStreamInVideo (url: string, token: string, videoId: number | string, onErrorCb?: Function) {
51 const res = await getLive(url, token, videoId)
52 const videoLive = res.body as LiveVideo
53
54 return sendRTMPStream(videoLive.rtmpUrl, videoLive.streamKey, onErrorCb)
55}
56
57function sendRTMPStream (rtmpBaseUrl: string, streamKey: string, onErrorCb?: Function) {
77e9f859
C
58 const fixture = buildAbsoluteFixturePath('video_short.mp4')
59
60 const command = ffmpeg(fixture)
61 command.inputOption('-stream_loop -1')
62 command.inputOption('-re')
63
64 command.outputOption('-c copy')
65 command.outputOption('-f flv')
66
67 const rtmpUrl = rtmpBaseUrl + '/' + streamKey
68 command.output(rtmpUrl)
69
70 command.on('error', err => {
71 if (err?.message?.includes('Exiting normally')) return
72
97969c4e 73 if (onErrorCb) onErrorCb(err)
77e9f859
C
74 })
75
76 if (process.env.DEBUG) {
77 command.on('stderr', data => console.log(data))
78 }
79
80 command.run()
81
82 return command
83}
84
97969c4e
C
85function waitFfmpegUntilError (command: ffmpeg.FfmpegCommand, successAfterMS = 10000) {
86 return new Promise((res, rej) => {
87 command.on('error', err => {
88 return rej(err)
89 })
90
91 setTimeout(() => {
92 res()
93 }, successAfterMS)
94 })
95}
96
97async function testFfmpegStreamError (url: string, token: string, videoId: number | string, shouldHaveError: boolean) {
98 const command = await sendRTMPStreamInVideo(url, token, videoId)
99 let error: Error
100
101 try {
102 await waitFfmpegUntilError(command, 10000)
103 } catch (err) {
104 error = err
105 }
106
107 await stopFfmpeg(command)
108
109 if (shouldHaveError && !error) throw new Error('Ffmpeg did not have an error')
110 if (!shouldHaveError && error) throw error
111}
112
77e9f859
C
113async function stopFfmpeg (command: ffmpeg.FfmpegCommand) {
114 command.kill('SIGINT')
115
116 await wait(500)
117}
118
119async function waitUntilLiveStarts (url: string, token: string, videoId: number | string) {
120 let video: VideoDetails
121
122 do {
123 const res = await getVideoWithToken(url, token, videoId)
124 video = res.body
125
126 await wait(500)
127 } while (video.state.id === VideoState.WAITING_FOR_LIVE)
128}
129
130// ---------------------------------------------------------------------------
131
132export {
133 getLive,
134 updateLive,
135 waitUntilLiveStarts,
136 createLive,
97969c4e 137 testFfmpegStreamError,
77e9f859 138 stopFfmpeg,
97969c4e
C
139 sendRTMPStreamInVideo,
140 waitFfmpegUntilError,
77e9f859
C
141 sendRTMPStream
142}