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 replaySettings: options.replay ? { privacy: VideoPrivacy.PUBLIC } : undefined,
27 permanentLive: options.permanent
30 const { uuid } = await server.live.create({ fields: attributes })
34 async function fastRestreamWrapper ({ replay }: { replay: boolean }) {
35 const liveVideoUUID = await createLiveWrapper({ permanent: true, replay })
36 await waitJobs([ server ])
39 videoId: liveVideoUUID,
41 fixtureName: 'video_short.mp4'
44 // Streaming session #1
45 let ffmpegCommand = await server.live.sendRTMPStreamInVideo(rtmpOptions)
46 await server.live.waitUntilPublished({ videoId: liveVideoUUID })
48 const video = await server.videos.get({ id: liveVideoUUID })
49 const session1PlaylistId = video.streamingPlaylists[0].id
51 await stopFfmpeg(ffmpegCommand)
52 await server.live.waitUntilWaiting({ videoId: liveVideoUUID })
54 // Streaming session #2
55 ffmpegCommand = await server.live.sendRTMPStreamInVideo(rtmpOptions)
57 let hasNewPlaylist = false
59 const video = await server.videos.get({ id: liveVideoUUID })
60 hasNewPlaylist = video.streamingPlaylists.length === 1 && video.streamingPlaylists[0].id !== session1PlaylistId
63 } while (!hasNewPlaylist)
65 await server.live.waitUntilSegmentGeneration({
67 videoUUID: liveVideoUUID,
73 return { ffmpegCommand, liveVideoUUID }
76 async function ensureLastLiveWorks (liveId: string) {
77 // Equivalent to PEERTUBE_TEST_CONSTANTS_VIDEO_LIVE_CLEANUP_DELAY
78 for (let i = 0; i < 100; i++) {
79 const video = await server.videos.get({ id: liveId })
80 expect(video.streamingPlaylists).to.have.lengthOf(1)
83 await server.live.getSegmentFile({ videoUUID: liveId, segment: 0, playlistNumber: 0 })
84 await makeRawRequest({ url: video.streamingPlaylists[0].playlistUrl, expectedStatus: HttpStatusCode.OK_200 })
85 await makeRawRequest({ url: video.streamingPlaylists[0].segmentsSha256Url, expectedStatus: HttpStatusCode.OK_200 })
87 // FIXME: try to debug error in CI "Unexpected end of JSON input"
96 async function runTest (replay: boolean) {
97 const { ffmpegCommand, liveVideoUUID } = await fastRestreamWrapper({ replay })
99 // TODO: remove, we try to debug a test timeout failure here
100 console.log('Ensuring last live works')
102 await ensureLastLiveWorks(liveVideoUUID)
104 await stopFfmpeg(ffmpegCommand)
105 await server.live.waitUntilWaiting({ videoId: liveVideoUUID })
108 await waitJobs([ server ])
110 const { total, data: sessions } = await server.live.listSessions({ videoId: liveVideoUUID })
112 expect(total).to.equal(2)
113 expect(sessions).to.have.lengthOf(2)
115 for (const session of sessions) {
116 expect(session.error).to.be.null
119 expect(session.replayVideo).to.exist
121 await server.videos.get({ id: session.replayVideo.uuid })
123 expect(session.replayVideo).to.not.exist
128 before(async function () {
131 const env = { PEERTUBE_TEST_CONSTANTS_VIDEO_LIVE_CLEANUP_DELAY: '10000' }
132 server = await createSingleServer(1, {}, { env })
134 // Get the access tokens
135 await setAccessTokensToServers([ server ])
136 await setDefaultVideoChannel([ server ])
138 await server.config.enableMinimumTranscoding(false, true)
139 await server.config.enableLive({ allowReplay: true, transcoding: true, resolutions: 'min' })
142 it('Should correctly fast restream in a permanent live with and without save replay', async function () {
145 // A test can take a long time, so prefer to run them in parallel
152 after(async function () {
153 await cleanupTests([ server ])