1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { FfmpegCommand } from 'fluent-ffmpeg'
6 import { LiveVideoCreate, VideoDetails, VideoPrivacy, VideoState } from '@shared/models'
7 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
15 flushAndRunMultipleServers,
19 sendRTMPStreamInVideo,
21 setAccessTokensToServers,
22 setDefaultVideoChannel,
24 testFfmpegStreamError,
29 waitUntilLivePublished,
31 } from '../../../../shared/extra-utils'
33 const expect = chai.expect
35 describe('Save replay setting', function () {
36 let servers: ServerInfo[] = []
37 let liveVideoUUID: string
38 let ffmpegCommand: FfmpegCommand
40 async function createLiveWrapper (saveReplay: boolean) {
43 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
44 await waitJobs(servers)
48 const attributes: LiveVideoCreate = {
49 channelId: servers[0].videoChannel.id,
50 privacy: VideoPrivacy.PUBLIC,
51 name: 'my super live',
55 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
56 return res.body.video.uuid
59 async function checkVideosExist (videoId: string, existsInList: boolean, getStatus?: number) {
60 for (const server of servers) {
61 const length = existsInList ? 1 : 0
63 const resVideos = await getVideosList(server.url)
64 expect(resVideos.body.data).to.have.lengthOf(length)
65 expect(resVideos.body.total).to.equal(length)
68 await getVideo(server.url, videoId, getStatus)
73 async function checkVideoState (videoId: string, state: VideoState) {
74 for (const server of servers) {
75 const res = await getVideo(server.url, videoId)
76 expect((res.body as VideoDetails).state.id).to.equal(state)
80 async function waitUntilLivePublishedOnAllServers (videoId: string) {
81 for (const server of servers) {
82 await waitUntilLivePublished(server.url, server.accessToken, videoId)
86 async function waitUntilLiveSavedOnAllServers (videoId: string) {
87 for (const server of servers) {
88 await waitUntilLiveSaved(server.url, server.accessToken, videoId)
92 before(async function () {
95 servers = await flushAndRunMultipleServers(2)
97 // Get the access tokens
98 await setAccessTokensToServers(servers)
99 await setDefaultVideoChannel(servers)
101 // Server 1 and server 2 follow each other
102 await doubleFollow(servers[0], servers[1])
104 await servers[0].configCommand.updateCustomSubConfig({
112 resolutions: ConfigCommand.getCustomConfigResolutions(true)
119 describe('With save replay disabled', function () {
121 before(async function () {
125 it('Should correctly create and federate the "waiting for stream" live', async function () {
128 liveVideoUUID = await createLiveWrapper(false)
130 await waitJobs(servers)
132 await checkVideosExist(liveVideoUUID, false, HttpStatusCode.OK_200)
133 await checkVideoState(liveVideoUUID, VideoState.WAITING_FOR_LIVE)
136 it('Should correctly have updated the live and federated it when streaming in the live', async function () {
139 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
141 await waitUntilLivePublishedOnAllServers(liveVideoUUID)
143 await waitJobs(servers)
145 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
146 await checkVideoState(liveVideoUUID, VideoState.PUBLISHED)
149 it('Should correctly delete the video files after the stream ended', async function () {
152 await stopFfmpeg(ffmpegCommand)
154 for (const server of servers) {
155 await waitUntilLiveEnded(server.url, server.accessToken, liveVideoUUID)
157 await waitJobs(servers)
159 // Live still exist, but cannot be played anymore
160 await checkVideosExist(liveVideoUUID, false, HttpStatusCode.OK_200)
161 await checkVideoState(liveVideoUUID, VideoState.LIVE_ENDED)
163 // No resolutions saved since we did not save replay
164 await checkLiveCleanup(servers[0], liveVideoUUID, [])
167 it('Should correctly terminate the stream on blacklist and delete the live', async function () {
170 liveVideoUUID = await createLiveWrapper(false)
172 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
174 await waitUntilLivePublishedOnAllServers(liveVideoUUID)
176 await waitJobs(servers)
177 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
180 addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideoUUID, 'bad live', true),
181 testFfmpegStreamError(ffmpegCommand, true)
184 await waitJobs(servers)
186 await checkVideosExist(liveVideoUUID, false)
188 await getVideo(servers[0].url, liveVideoUUID, HttpStatusCode.UNAUTHORIZED_401)
189 await getVideo(servers[1].url, liveVideoUUID, HttpStatusCode.NOT_FOUND_404)
192 await waitJobs(servers)
193 await checkLiveCleanup(servers[0], liveVideoUUID, [])
196 it('Should correctly terminate the stream on delete and delete the video', async function () {
199 liveVideoUUID = await createLiveWrapper(false)
201 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
203 await waitUntilLivePublishedOnAllServers(liveVideoUUID)
205 await waitJobs(servers)
206 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
209 testFfmpegStreamError(ffmpegCommand, true),
210 removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
214 await waitJobs(servers)
216 await checkVideosExist(liveVideoUUID, false, HttpStatusCode.NOT_FOUND_404)
217 await checkLiveCleanup(servers[0], liveVideoUUID, [])
221 describe('With save replay enabled', function () {
223 it('Should correctly create and federate the "waiting for stream" live', async function () {
226 liveVideoUUID = await createLiveWrapper(true)
228 await waitJobs(servers)
230 await checkVideosExist(liveVideoUUID, false, HttpStatusCode.OK_200)
231 await checkVideoState(liveVideoUUID, VideoState.WAITING_FOR_LIVE)
234 it('Should correctly have updated the live and federated it when streaming in the live', async function () {
237 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
238 await waitUntilLivePublishedOnAllServers(liveVideoUUID)
240 await waitJobs(servers)
242 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
243 await checkVideoState(liveVideoUUID, VideoState.PUBLISHED)
246 it('Should correctly have saved the live and federated it after the streaming', async function () {
249 await stopFfmpeg(ffmpegCommand)
251 await waitUntilLiveSavedOnAllServers(liveVideoUUID)
252 await waitJobs(servers)
254 // Live has been transcoded
255 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
256 await checkVideoState(liveVideoUUID, VideoState.PUBLISHED)
259 it('Should update the saved live and correctly federate the updated attributes', async function () {
262 await updateVideo(servers[0].url, servers[0].accessToken, liveVideoUUID, { name: 'video updated' })
263 await waitJobs(servers)
265 for (const server of servers) {
266 const res = await getVideo(server.url, liveVideoUUID)
267 expect(res.body.name).to.equal('video updated')
268 expect(res.body.isLive).to.be.false
272 it('Should have cleaned up the live files', async function () {
273 await checkLiveCleanup(servers[0], liveVideoUUID, [ 720 ])
276 it('Should correctly terminate the stream on blacklist and blacklist the saved replay video', async function () {
279 liveVideoUUID = await createLiveWrapper(true)
281 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
282 await waitUntilLivePublishedOnAllServers(liveVideoUUID)
284 await waitJobs(servers)
285 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
288 addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideoUUID, 'bad live', true),
289 testFfmpegStreamError(ffmpegCommand, true)
292 await waitJobs(servers)
294 await checkVideosExist(liveVideoUUID, false)
296 await getVideo(servers[0].url, liveVideoUUID, HttpStatusCode.UNAUTHORIZED_401)
297 await getVideo(servers[1].url, liveVideoUUID, HttpStatusCode.NOT_FOUND_404)
300 await waitJobs(servers)
301 await checkLiveCleanup(servers[0], liveVideoUUID, [ 720 ])
304 it('Should correctly terminate the stream on delete and delete the video', async function () {
307 liveVideoUUID = await createLiveWrapper(true)
309 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
310 await waitUntilLivePublishedOnAllServers(liveVideoUUID)
312 await waitJobs(servers)
313 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
316 removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID),
317 testFfmpegStreamError(ffmpegCommand, true)
321 await waitJobs(servers)
323 await checkVideosExist(liveVideoUUID, false, HttpStatusCode.NOT_FOUND_404)
324 await checkLiveCleanup(servers[0], liveVideoUUID, [])
328 after(async function () {
329 await cleanupTests(servers)