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'
14 flushAndRunMultipleServers,
18 sendRTMPStreamInVideo,
20 setAccessTokensToServers,
21 setDefaultVideoChannel,
23 testFfmpegStreamError,
24 updateCustomSubConfig,
28 waitUntilLivePublished
29 } from '../../../../shared/extra-utils'
31 const expect = chai.expect
33 describe('Save replay setting', function () {
34 let servers: ServerInfo[] = []
35 let liveVideoUUID: string
36 let ffmpegCommand: FfmpegCommand
38 async function createLiveWrapper (saveReplay: boolean) {
41 await removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
42 await waitJobs(servers)
46 const attributes: LiveVideoCreate = {
47 channelId: servers[0].videoChannel.id,
48 privacy: VideoPrivacy.PUBLIC,
49 name: 'my super live',
53 const res = await createLive(servers[0].url, servers[0].accessToken, attributes)
54 return res.body.video.uuid
57 async function checkVideosExist (videoId: string, existsInList: boolean, getStatus?: number) {
58 for (const server of servers) {
59 const length = existsInList ? 1 : 0
61 const resVideos = await getVideosList(server.url)
62 expect(resVideos.body.data).to.have.lengthOf(length)
63 expect(resVideos.body.total).to.equal(length)
66 await getVideo(server.url, videoId, getStatus)
71 async function checkVideoState (videoId: string, state: VideoState) {
72 for (const server of servers) {
73 const res = await getVideo(server.url, videoId)
74 expect((res.body as VideoDetails).state.id).to.equal(state)
78 async function waitUntilLivePublishedOnAllServers (videoId: string) {
79 for (const server of servers) {
80 await waitUntilLivePublished(server.url, server.accessToken, videoId)
84 before(async function () {
87 servers = await flushAndRunMultipleServers(2)
89 // Get the access tokens
90 await setAccessTokensToServers(servers)
91 await setDefaultVideoChannel(servers)
93 // Server 1 and server 2 follow each other
94 await doubleFollow(servers[0], servers[1])
96 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
117 describe('With save replay disabled', function () {
119 before(async function () {
123 it('Should correctly create and federate the "waiting for stream" live', async function () {
126 liveVideoUUID = await createLiveWrapper(false)
128 await waitJobs(servers)
130 await checkVideosExist(liveVideoUUID, false, HttpStatusCode.OK_200)
131 await checkVideoState(liveVideoUUID, VideoState.WAITING_FOR_LIVE)
134 it('Should correctly have updated the live and federated it when streaming in the live', async function () {
137 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
139 await waitUntilLivePublishedOnAllServers(liveVideoUUID)
141 await waitJobs(servers)
143 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
144 await checkVideoState(liveVideoUUID, VideoState.PUBLISHED)
147 it('Should correctly delete the video files after the stream ended', async function () {
150 await stopFfmpeg(ffmpegCommand)
152 for (const server of servers) {
153 await waitUntilLiveEnded(server.url, server.accessToken, liveVideoUUID)
155 await waitJobs(servers)
157 // Live still exist, but cannot be played anymore
158 await checkVideosExist(liveVideoUUID, false, HttpStatusCode.OK_200)
159 await checkVideoState(liveVideoUUID, VideoState.LIVE_ENDED)
161 await waitJobs(servers)
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)
191 await checkLiveCleanup(servers[0], liveVideoUUID, [])
194 it('Should correctly terminate the stream on delete and delete the video', async function () {
197 liveVideoUUID = await createLiveWrapper(false)
199 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
201 await waitUntilLivePublishedOnAllServers(liveVideoUUID)
203 await waitJobs(servers)
204 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
207 testFfmpegStreamError(ffmpegCommand, true),
208 removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
211 await waitJobs(servers)
213 await checkVideosExist(liveVideoUUID, false, HttpStatusCode.NOT_FOUND_404)
214 await checkLiveCleanup(servers[0], liveVideoUUID, [])
218 describe('With save replay enabled', function () {
220 it('Should correctly create and federate the "waiting for stream" live', async function () {
223 liveVideoUUID = await createLiveWrapper(true)
225 await waitJobs(servers)
227 await checkVideosExist(liveVideoUUID, false, HttpStatusCode.OK_200)
228 await checkVideoState(liveVideoUUID, VideoState.WAITING_FOR_LIVE)
231 it('Should correctly have updated the live and federated it when streaming in the live', async function () {
234 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
235 await waitUntilLivePublishedOnAllServers(liveVideoUUID)
237 await waitJobs(servers)
239 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
240 await checkVideoState(liveVideoUUID, VideoState.PUBLISHED)
243 it('Should correctly have saved the live and federated it after the streaming', async function () {
246 await stopFfmpeg(ffmpegCommand)
248 await waitJobs(servers)
250 // Live has been transcoded
251 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
252 await checkVideoState(liveVideoUUID, VideoState.PUBLISHED)
255 it('Should update the saved live and correctly federate the updated attributes', async function () {
258 await updateVideo(servers[0].url, servers[0].accessToken, liveVideoUUID, { name: 'video updated' })
259 await waitJobs(servers)
261 for (const server of servers) {
262 const res = await getVideo(server.url, liveVideoUUID)
263 expect(res.body.name).to.equal('video updated')
264 expect(res.body.isLive).to.be.false
268 it('Should have cleaned up the live files', async function () {
269 await checkLiveCleanup(servers[0], liveVideoUUID, [ 720 ])
272 it('Should correctly terminate the stream on blacklist and blacklist the saved replay video', async function () {
275 liveVideoUUID = await createLiveWrapper(true)
277 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
278 await waitUntilLivePublishedOnAllServers(liveVideoUUID)
280 await waitJobs(servers)
281 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
284 addVideoToBlacklist(servers[0].url, servers[0].accessToken, liveVideoUUID, 'bad live', true),
285 testFfmpegStreamError(ffmpegCommand, true)
288 await waitJobs(servers)
290 await checkVideosExist(liveVideoUUID, false)
292 await getVideo(servers[0].url, liveVideoUUID, HttpStatusCode.UNAUTHORIZED_401)
293 await getVideo(servers[1].url, liveVideoUUID, HttpStatusCode.NOT_FOUND_404)
295 await checkLiveCleanup(servers[0], liveVideoUUID, [ 720 ])
298 it('Should correctly terminate the stream on delete and delete the video', async function () {
301 liveVideoUUID = await createLiveWrapper(true)
303 ffmpegCommand = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID)
304 await waitUntilLivePublishedOnAllServers(liveVideoUUID)
306 await waitJobs(servers)
307 await checkVideosExist(liveVideoUUID, true, HttpStatusCode.OK_200)
310 removeVideo(servers[0].url, servers[0].accessToken, liveVideoUUID),
311 testFfmpegStreamError(ffmpegCommand, true)
314 await waitJobs(servers)
316 await checkVideosExist(liveVideoUUID, false, HttpStatusCode.NOT_FOUND_404)
317 await checkLiveCleanup(servers[0], liveVideoUUID, [])
321 after(async function () {
322 await cleanupTests(servers)