]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/live/live-fast-restream.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / tests / api / live / live-fast-restream.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import { expect } from 'chai'
4 import { wait } from '@shared/core-utils'
5 import { LiveVideoCreate, VideoPrivacy } from '@shared/models'
6 import {
7 cleanupTests,
8 createSingleServer,
9 PeerTubeServer,
10 setAccessTokensToServers,
11 setDefaultVideoChannel,
12 stopFfmpeg,
13 waitJobs
14 } from '@shared/server-commands'
15
16 describe('Fast restream in live', function () {
17 let server: PeerTubeServer
18
19 async function createLiveWrapper (options: { permanent: boolean, replay: boolean }) {
20 const attributes: LiveVideoCreate = {
21 channelId: server.store.channel.id,
22 privacy: VideoPrivacy.PUBLIC,
23 name: 'my super live',
24 saveReplay: options.replay,
25 replaySettings: options.replay ? { privacy: VideoPrivacy.PUBLIC } : undefined,
26 permanentLive: options.permanent
27 }
28
29 const { uuid } = await server.live.create({ fields: attributes })
30 return uuid
31 }
32
33 async function fastRestreamWrapper ({ replay }: { replay: boolean }) {
34 const liveVideoUUID = await createLiveWrapper({ permanent: true, replay })
35 await waitJobs([ server ])
36
37 const rtmpOptions = {
38 videoId: liveVideoUUID,
39 copyCodecs: true,
40 fixtureName: 'video_short.mp4'
41 }
42
43 // Streaming session #1
44 let ffmpegCommand = await server.live.sendRTMPStreamInVideo(rtmpOptions)
45 await server.live.waitUntilPublished({ videoId: liveVideoUUID })
46
47 const video = await server.videos.get({ id: liveVideoUUID })
48 const session1PlaylistId = video.streamingPlaylists[0].id
49
50 await stopFfmpeg(ffmpegCommand)
51 await server.live.waitUntilWaiting({ videoId: liveVideoUUID })
52
53 // Streaming session #2
54 ffmpegCommand = await server.live.sendRTMPStreamInVideo(rtmpOptions)
55
56 let hasNewPlaylist = false
57 do {
58 const video = await server.videos.get({ id: liveVideoUUID })
59 hasNewPlaylist = video.streamingPlaylists.length === 1 && video.streamingPlaylists[0].id !== session1PlaylistId
60
61 await wait(100)
62 } while (!hasNewPlaylist)
63
64 await server.live.waitUntilSegmentGeneration({
65 server,
66 videoUUID: liveVideoUUID,
67 segment: 1,
68 playlistNumber: 0
69 })
70
71 return { ffmpegCommand, liveVideoUUID }
72 }
73
74 async function ensureLastLiveWorks (liveId: string) {
75 // Equivalent to PEERTUBE_TEST_CONSTANTS_VIDEO_LIVE_CLEANUP_DELAY
76 for (let i = 0; i < 100; i++) {
77 const video = await server.videos.get({ id: liveId })
78 expect(video.streamingPlaylists).to.have.lengthOf(1)
79
80 try {
81 await server.live.getSegmentFile({ videoUUID: liveId, segment: 0, playlistNumber: 0 })
82 await server.streamingPlaylists.get({ url: video.streamingPlaylists[0].playlistUrl })
83 await server.streamingPlaylists.getSegmentSha256({ url: video.streamingPlaylists[0].segmentsSha256Url })
84 } catch (err) {
85 // FIXME: try to debug error in CI "Unexpected end of JSON input"
86 console.error(err)
87 throw err
88 }
89
90 await wait(100)
91 }
92 }
93
94 async function runTest (replay: boolean) {
95 const { ffmpegCommand, liveVideoUUID } = await fastRestreamWrapper({ replay })
96
97 // TODO: remove, we try to debug a test timeout failure here
98 console.log('Ensuring last live works')
99
100 await ensureLastLiveWorks(liveVideoUUID)
101
102 await stopFfmpeg(ffmpegCommand)
103 await server.live.waitUntilWaiting({ videoId: liveVideoUUID })
104
105 // Wait for replays
106 await waitJobs([ server ])
107
108 const { total, data: sessions } = await server.live.listSessions({ videoId: liveVideoUUID })
109
110 expect(total).to.equal(2)
111 expect(sessions).to.have.lengthOf(2)
112
113 for (const session of sessions) {
114 expect(session.error).to.be.null
115
116 if (replay) {
117 expect(session.replayVideo).to.exist
118
119 await server.videos.get({ id: session.replayVideo.uuid })
120 } else {
121 expect(session.replayVideo).to.not.exist
122 }
123 }
124 }
125
126 before(async function () {
127 this.timeout(120000)
128
129 const env = { PEERTUBE_TEST_CONSTANTS_VIDEO_LIVE_CLEANUP_DELAY: '10000' }
130 server = await createSingleServer(1, {}, { env })
131
132 // Get the access tokens
133 await setAccessTokensToServers([ server ])
134 await setDefaultVideoChannel([ server ])
135
136 await server.config.enableMinimumTranscoding(false, true)
137 await server.config.enableLive({ allowReplay: true, transcoding: true, resolutions: 'min' })
138 })
139
140 it('Should correctly fast restream in a permanent live with and without save replay', async function () {
141 this.timeout(480000)
142
143 // A test can take a long time, so prefer to run them in parallel
144 await Promise.all([
145 runTest(true),
146 runTest(false)
147 ])
148 })
149
150 after(async function () {
151 await cleanupTests([ server ])
152 })
153 })