]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - shared/extra-utils/videos/live.ts
Do not display wait transcoding checkbox
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / videos / live.ts
... / ...
CommitLineData
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3import { expect } from 'chai'
4import * as ffmpeg from 'fluent-ffmpeg'
5import { pathExists, readdir } from 'fs-extra'
6import { omit } from 'lodash'
7import { join } from 'path'
8import { LiveVideo, LiveVideoCreate, LiveVideoUpdate, VideoDetails, VideoState } from '@shared/models'
9import { buildAbsoluteFixturePath, buildServerDirectory, wait } from '../miscs/miscs'
10import { makeGetRequest, makePutBodyRequest, makeUploadRequest } from '../requests/requests'
11import { ServerInfo } from '../server/servers'
12import { getVideoWithToken } from './videos'
13
14function getLive (url: string, token: string, videoId: number | string, statusCodeExpected = 200) {
15 const path = '/api/v1/videos/live'
16
17 return makeGetRequest({
18 url,
19 token,
20 path: path + '/' + videoId,
21 statusCodeExpected
22 })
23}
24
25function updateLive (url: string, token: string, videoId: number | string, fields: LiveVideoUpdate, statusCodeExpected = 204) {
26 const path = '/api/v1/videos/live'
27
28 return makePutBodyRequest({
29 url,
30 token,
31 path: path + '/' + videoId,
32 fields,
33 statusCodeExpected
34 })
35}
36
37function createLive (url: string, token: string, fields: LiveVideoCreate, statusCodeExpected = 200) {
38 const path = '/api/v1/videos/live'
39
40 const attaches: any = {}
41 if (fields.thumbnailfile) attaches.thumbnailfile = fields.thumbnailfile
42 if (fields.previewfile) attaches.previewfile = fields.previewfile
43
44 const updatedFields = omit(fields, 'thumbnailfile', 'previewfile')
45
46 return makeUploadRequest({
47 url,
48 path,
49 token,
50 attaches,
51 fields: updatedFields,
52 statusCodeExpected
53 })
54}
55
56async function sendRTMPStreamInVideo (url: string, token: string, videoId: number | string, fixtureName?: string) {
57 const res = await getLive(url, token, videoId)
58 const videoLive = res.body as LiveVideo
59
60 return sendRTMPStream(videoLive.rtmpUrl, videoLive.streamKey, fixtureName)
61}
62
63function sendRTMPStream (rtmpBaseUrl: string, streamKey: string, fixtureName = 'video_short.mp4') {
64 const fixture = buildAbsoluteFixturePath(fixtureName)
65
66 const command = ffmpeg(fixture)
67 command.inputOption('-stream_loop -1')
68 command.inputOption('-re')
69 command.outputOption('-c:v libx264')
70 command.outputOption('-g 50')
71 command.outputOption('-keyint_min 2')
72 command.outputOption('-f flv')
73
74 const rtmpUrl = rtmpBaseUrl + '/' + streamKey
75 command.output(rtmpUrl)
76
77 command.on('error', err => {
78 if (err?.message?.includes('Exiting normally')) return
79
80 if (process.env.DEBUG) console.error(err)
81 })
82
83 if (process.env.DEBUG) {
84 command.on('stderr', data => console.log(data))
85 }
86
87 command.run()
88
89 return command
90}
91
92function waitFfmpegUntilError (command: ffmpeg.FfmpegCommand, successAfterMS = 10000) {
93 return new Promise((res, rej) => {
94 command.on('error', err => {
95 return rej(err)
96 })
97
98 setTimeout(() => {
99 res()
100 }, successAfterMS)
101 })
102}
103
104async function runAndTestFfmpegStreamError (url: string, token: string, videoId: number | string, shouldHaveError: boolean) {
105 const command = await sendRTMPStreamInVideo(url, token, videoId)
106
107 return testFfmpegStreamError(command, shouldHaveError)
108}
109
110async function testFfmpegStreamError (command: ffmpeg.FfmpegCommand, shouldHaveError: boolean) {
111 let error: Error
112
113 try {
114 await waitFfmpegUntilError(command, 15000)
115 } catch (err) {
116 error = err
117 }
118
119 await stopFfmpeg(command)
120
121 if (shouldHaveError && !error) throw new Error('Ffmpeg did not have an error')
122 if (!shouldHaveError && error) throw error
123}
124
125async function stopFfmpeg (command: ffmpeg.FfmpegCommand) {
126 command.kill('SIGINT')
127
128 await wait(500)
129}
130
131function waitUntilLiveStarts (url: string, token: string, videoId: number | string) {
132 return waitWhileLiveState(url, token, videoId, VideoState.WAITING_FOR_LIVE)
133}
134
135function waitUntilLivePublished (url: string, token: string, videoId: number | string) {
136 return waitWhileLiveState(url, token, videoId, VideoState.PUBLISHED)
137}
138
139async function waitWhileLiveState (url: string, token: string, videoId: number | string, state: VideoState) {
140 let video: VideoDetails
141
142 do {
143 const res = await getVideoWithToken(url, token, videoId)
144 video = res.body
145
146 await wait(500)
147 } while (video.state.id === state)
148}
149
150async function checkLiveCleanup (server: ServerInfo, videoUUID: string, resolutions: number[] = []) {
151 const basePath = buildServerDirectory(server, 'streaming-playlists')
152 const hlsPath = join(basePath, 'hls', videoUUID)
153
154 if (resolutions.length === 0) {
155 const result = await pathExists(hlsPath)
156 expect(result).to.be.false
157
158 return
159 }
160
161 const files = await readdir(hlsPath)
162
163 // fragmented file and playlist per resolution + master playlist + segments sha256 json file
164 expect(files).to.have.lengthOf(resolutions.length * 2 + 2)
165
166 for (const resolution of resolutions) {
167 expect(files).to.contain(`${videoUUID}-${resolution}-fragmented.mp4`)
168 expect(files).to.contain(`${resolution}.m3u8`)
169 }
170
171 expect(files).to.contain('master.m3u8')
172 expect(files).to.contain('segments-sha256.json')
173}
174
175// ---------------------------------------------------------------------------
176
177export {
178 getLive,
179 waitUntilLivePublished,
180 updateLive,
181 waitUntilLiveStarts,
182 createLive,
183 runAndTestFfmpegStreamError,
184 checkLiveCleanup,
185 stopFfmpeg,
186 sendRTMPStreamInVideo,
187 waitFfmpegUntilError,
188 sendRTMPStream,
189 testFfmpegStreamError
190}