]>
Commit | Line | Data |
---|---|---|
53023be3 C |
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
2 | ||
86347717 | 3 | import { expect } from 'chai' |
53023be3 | 4 | import { wait } from '@shared/core-utils' |
823c34c0 | 5 | import { LiveVideoCreate, VideoPrivacy } from '@shared/models' |
53023be3 C |
6 | import { |
7 | cleanupTests, | |
8 | createSingleServer, | |
53023be3 C |
9 | PeerTubeServer, |
10 | setAccessTokensToServers, | |
11 | setDefaultVideoChannel, | |
12 | stopFfmpeg, | |
13 | waitJobs | |
14 | } from '@shared/server-commands' | |
15 | ||
53023be3 C |
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, | |
05a60d85 | 25 | replaySettings: options.replay ? { privacy: VideoPrivacy.PUBLIC } : undefined, |
53023be3 C |
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 }) | |
bbae45c3 C |
46 | |
47 | const video = await server.videos.get({ id: liveVideoUUID }) | |
48 | const session1PlaylistId = video.streamingPlaylists[0].id | |
49 | ||
53023be3 C |
50 | await stopFfmpeg(ffmpegCommand) |
51 | await server.live.waitUntilWaiting({ videoId: liveVideoUUID }) | |
52 | ||
53 | // Streaming session #2 | |
54 | ffmpegCommand = await server.live.sendRTMPStreamInVideo(rtmpOptions) | |
bbae45c3 C |
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, | |
f8918990 | 68 | playlistNumber: 0 |
bbae45c3 | 69 | }) |
53023be3 C |
70 | |
71 | return { ffmpegCommand, liveVideoUUID } | |
72 | } | |
73 | ||
74 | async function ensureLastLiveWorks (liveId: string) { | |
b6e2b5df | 75 | // Equivalent to PEERTUBE_TEST_CONSTANTS_VIDEO_LIVE_CLEANUP_DELAY |
53023be3 C |
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 | ||
518c5cc6 C |
80 | try { |
81 | await server.live.getSegmentFile({ videoUUID: liveId, segment: 0, playlistNumber: 0 }) | |
823c34c0 C |
82 | await server.streamingPlaylists.get({ url: video.streamingPlaylists[0].playlistUrl }) |
83 | await server.streamingPlaylists.getSegmentSha256({ url: video.streamingPlaylists[0].segmentsSha256Url }) | |
518c5cc6 C |
84 | } catch (err) { |
85 | // FIXME: try to debug error in CI "Unexpected end of JSON input" | |
86 | console.error(err) | |
87 | throw err | |
88 | } | |
53023be3 C |
89 | |
90 | await wait(100) | |
91 | } | |
92 | } | |
93 | ||
94 | async function runTest (replay: boolean) { | |
95 | const { ffmpegCommand, liveVideoUUID } = await fastRestreamWrapper({ replay }) | |
96 | ||
2a720a0f C |
97 | // TODO: remove, we try to debug a test timeout failure here |
98 | console.log('Ensuring last live works') | |
99 | ||
53023be3 C |
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 | ||
c3fb12b3 | 129 | const env = { PEERTUBE_TEST_CONSTANTS_VIDEO_LIVE_CLEANUP_DELAY: '10000' } |
53023be3 C |
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 | ||
25691c99 | 140 | it('Should correctly fast restream in a permanent live with and without save replay', async function () { |
99b75748 | 141 | this.timeout(480000) |
53023be3 C |
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 | }) |