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'
13 flushAndRunMultipleServers,
17 sendRTMPStreamInVideo,
19 setAccessTokensToServers,
20 setDefaultVideoChannel,
22 testFfmpegStreamError,
23 updateCustomSubConfig,
26 waitUntilLivePublished
27 } from '../../../../shared/extra-utils'
28 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
30 const expect = chai.expect
32 describe('Save replay setting', function () {
33 let servers: ServerInfo[] = []
34 let liveVideoUUID: string
35 let ffmpegCommand: FfmpegCommand
37 async function createLiveWrapper (saveReplay: boolean) {
40 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
41 await waitJobs(servers)
45 const attributes: LiveVideoCreate = {
46 channelId: servers[0].videoChannel.id,
47 privacy: VideoPrivacy.PUBLIC,
48 name: 'my super live',
52 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
53 return res.body.video.uuid
56 async function checkVideosExist (videoId: string, existsInList: boolean, getStatus?: number) {
57 for (const server of servers) {
58 const length = existsInList ? 1 : 0
60 const resVideos = await getVideosList(server.url)
61 expect(resVideos.body.data).to.have.lengthOf(length)
62 expect(resVideos.body.total).to.equal(length)
65 await getVideo(server.url, videoId, getStatus)
70 async function checkVideoState (videoId: string, state: VideoState) {
71 for (const server of servers) {
72 const res = await getVideo(server.url, videoId)
73 expect((res.body as VideoDetails).state.id).to.equal(state)
77 before(async function () {
80 servers = await flushAndRunMultipleServers(2)
82 // Get the access tokens
83 await setAccessTokensToServers(servers)
84 await setDefaultVideoChannel(servers)
86 // Server 1 and server 2 follow each other
87 await doubleFollow(servers[0], servers[1])
89 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
109 describe('With save replay disabled', function () {
111 before(async function () {
115 it('Should correctly create and federate the "waiting for stream" live', async function () {
118 liveVideoUUID = await createLiveWrapper(false)
120 await waitJobs(servers)
122 await checkVideosExist(liveVideoUUID, false, HttpStatusCode.OK_200)
123 await checkVideoState(liveVideoUUID, VideoState.WAITING_FOR_LIVE)
126 it('Should correctly have updated the live and federated it when streaming in the live', async function () {
129 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
130 await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoUUID)
132 await waitJobs(servers)
134 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
135 await checkVideoState(liveVideoUUID, VideoState.PUBLISHED)
138 it('Should correctly delete the video files after the stream ended', async function () {
141 await stopFfmpeg(ffmpegCommand)
143 await waitJobs(servers)
145 // Live still exist, but cannot be played anymore
146 await checkVideosExist(liveVideoUUID, false, HttpStatusCode.OK_200)
147 await checkVideoState(liveVideoUUID, VideoState.LIVE_ENDED)
149 // No resolutions saved since we did not save replay
150 await checkLiveCleanup(servers[0], liveVideoUUID, [])
153 it('Should correctly terminate the stream on blacklist and delete the live', async function () {
156 liveVideoUUID = await createLiveWrapper(false)
158 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
159 await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoUUID)
161 await waitJobs(servers)
162 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
165 addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideoUUID, 'bad live', true),
166 testFfmpegStreamError(ffmpegCommand, true)
169 await waitJobs(servers)
171 await checkVideosExist(liveVideoUUID, false)
173 await getVideo(servers[0].url, liveVideoUUID, HttpStatusCode.UNAUTHORIZED_401)
174 await getVideo(servers[1].url, liveVideoUUID, HttpStatusCode.NOT_FOUND_404)
176 await checkLiveCleanup(servers[0], liveVideoUUID, [])
179 it('Should correctly terminate the stream on delete and delete the video', async function () {
182 liveVideoUUID = await createLiveWrapper(false)
184 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
185 await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoUUID)
187 await waitJobs(servers)
188 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
191 testFfmpegStreamError(ffmpegCommand, true),
192 removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
195 await waitJobs(servers)
197 await checkVideosExist(liveVideoUUID, false, HttpStatusCode.NOT_FOUND_404)
198 await checkLiveCleanup(servers[0], liveVideoUUID, [])
202 describe('With save replay enabled', function () {
204 it('Should correctly create and federate the "waiting for stream" live', async function () {
207 liveVideoUUID = await createLiveWrapper(true)
209 await waitJobs(servers)
211 await checkVideosExist(liveVideoUUID, false, HttpStatusCode.OK_200)
212 await checkVideoState(liveVideoUUID, VideoState.WAITING_FOR_LIVE)
215 it('Should correctly have updated the live and federated it when streaming in the live', async function () {
218 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
219 await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoUUID)
221 await waitJobs(servers)
223 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
224 await checkVideoState(liveVideoUUID, VideoState.PUBLISHED)
227 it('Should correctly have saved the live and federated it after the streaming', async function () {
230 await stopFfmpeg(ffmpegCommand)
232 await waitJobs(servers)
234 // Live has been transcoded
235 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
236 await checkVideoState(liveVideoUUID, VideoState.PUBLISHED)
239 it('Should update the saved live and correctly federate the updated attributes', async function () {
242 await updateVideo(servers[0].url, servers[0].accessToken, liveVideoUUID, { name: 'video updated' })
243 await waitJobs(servers)
245 for (const server of servers) {
246 const res = await getVideo(server.url, liveVideoUUID)
247 expect(res.body.name).to.equal('video updated')
248 expect(res.body.isLive).to.be.false
252 it('Should have cleaned up the live files', async function () {
253 await checkLiveCleanup(servers[0], liveVideoUUID, [ 720 ])
256 it('Should correctly terminate the stream on blacklist and blacklist the saved replay video', async function () {
259 liveVideoUUID = await createLiveWrapper(true)
261 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
262 await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoUUID)
264 await waitJobs(servers)
265 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
268 addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideoUUID, 'bad live', true),
269 testFfmpegStreamError(ffmpegCommand, true)
272 await waitJobs(servers)
274 await checkVideosExist(liveVideoUUID, false)
276 await getVideo(servers[0].url, liveVideoUUID, HttpStatusCode.UNAUTHORIZED_401)
277 await getVideo(servers[1].url, liveVideoUUID, HttpStatusCode.NOT_FOUND_404)
279 await checkLiveCleanup(servers[0], liveVideoUUID, [ 720 ])
282 it('Should correctly terminate the stream on delete and delete the video', async function () {
285 liveVideoUUID = await createLiveWrapper(true)
287 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
288 await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoUUID)
290 await waitJobs(servers)
291 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
294 removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID),
295 testFfmpegStreamError(ffmpegCommand, true)
298 await waitJobs(servers)
300 await checkVideosExist(liveVideoUUID, false, HttpStatusCode.NOT_FOUND_404)
301 await checkLiveCleanup(servers[0], liveVideoUUID, [])
305 after(async function () {
306 await cleanupTests(servers)