1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { wait } from '@shared/core-utils'
5 import { HttpStatusCode, LiveVideoCreate, VideoPrivacy } from '@shared/models'
11 setAccessTokensToServers,
12 setDefaultVideoChannel,
15 } from '@shared/server-commands'
17 describe('Fast restream in live', function () {
18 let server: PeerTubeServer
20 async function createLiveWrapper (options: { permanent: boolean, replay: boolean }) {
21 const attributes: LiveVideoCreate = {
22 channelId: server.store.channel.id,
23 privacy: VideoPrivacy.PUBLIC,
24 name: 'my super live',
25 saveReplay: options.replay,
26 permanentLive: options.permanent
29 const { uuid } = await server.live.create({ fields: attributes })
33 async function fastRestreamWrapper ({ replay }: { replay: boolean }) {
34 const liveVideoUUID = await createLiveWrapper({ permanent: true, replay })
35 await waitJobs([ server ])
38 videoId: liveVideoUUID,
40 fixtureName: 'video_short.mp4'
43 // Streaming session #1
44 let ffmpegCommand = await server.live.sendRTMPStreamInVideo(rtmpOptions)
45 await server.live.waitUntilPublished({ videoId: liveVideoUUID })
47 const video = await server.videos.get({ id: liveVideoUUID })
48 const session1PlaylistId = video.streamingPlaylists[0].id
50 await stopFfmpeg(ffmpegCommand)
51 await server.live.waitUntilWaiting({ videoId: liveVideoUUID })
53 // Streaming session #2
54 ffmpegCommand = await server.live.sendRTMPStreamInVideo(rtmpOptions)
56 let hasNewPlaylist = false
58 const video = await server.videos.get({ id: liveVideoUUID })
59 hasNewPlaylist = video.streamingPlaylists.length === 1 && video.streamingPlaylists[0].id !== session1PlaylistId
62 } while (!hasNewPlaylist)
64 await server.live.waitUntilSegmentGeneration({
66 videoUUID: liveVideoUUID,
72 return { ffmpegCommand, liveVideoUUID }
75 async function ensureLastLiveWorks (liveId: string) {
76 // Equivalent to PEERTUBE_TEST_CONSTANTS_VIDEO_LIVE_CLEANUP_DELAY
77 for (let i = 0; i < 100; i++) {
78 const video = await server.videos.get({ id: liveId })
79 expect(video.streamingPlaylists).to.have.lengthOf(1)
81 await server.live.getSegmentFile({ videoUUID: liveId, segment: 0, playlistNumber: 0 })
82 await makeRawRequest({ url: video.streamingPlaylists[0].playlistUrl, expectedStatus: HttpStatusCode.OK_200 })
83 await makeRawRequest({ url: video.streamingPlaylists[0].segmentsSha256Url, expectedStatus: HttpStatusCode.OK_200 })
89 async function runTest (replay: boolean) {
90 const { ffmpegCommand, liveVideoUUID } = await fastRestreamWrapper({ replay })
92 await ensureLastLiveWorks(liveVideoUUID)
94 await stopFfmpeg(ffmpegCommand)
95 await server.live.waitUntilWaiting({ videoId: liveVideoUUID })
98 await waitJobs([ server ])
100 const { total, data: sessions } = await server.live.listSessions({ videoId: liveVideoUUID })
102 expect(total).to.equal(2)
103 expect(sessions).to.have.lengthOf(2)
105 for (const session of sessions) {
106 expect(session.error).to.be.null
109 expect(session.replayVideo).to.exist
111 await server.videos.get({ id: session.replayVideo.uuid })
113 expect(session.replayVideo).to.not.exist
118 before(async function () {
121 const env = { PEERTUBE_TEST_CONSTANTS_VIDEO_LIVE_CLEANUP_DELAY: '10000' }
122 server = await createSingleServer(1, {}, { env })
124 // Get the access tokens
125 await setAccessTokensToServers([ server ])
126 await setDefaultVideoChannel([ server ])
128 await server.config.enableMinimumTranscoding(false, true)
129 await server.config.enableLive({ allowReplay: true, transcoding: true, resolutions: 'min' })
132 it('Should correctly fast restream in a permanent live with and without save replay', async function () {
135 // A test can take a long time, so prefer to run them in parallel
142 after(async function () {
143 await cleanupTests([ server ])