]>
Commit | Line | Data |
---|---|---|
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 { HttpStatusCode, LiveVideoCreate, VideoPrivacy } from '@shared/models' | |
6 | import { | |
7 | cleanupTests, | |
8 | createSingleServer, | |
9 | makeRawRequest, | |
10 | PeerTubeServer, | |
11 | setAccessTokensToServers, | |
12 | setDefaultVideoChannel, | |
13 | stopFfmpeg, | |
14 | waitJobs | |
15 | } from '@shared/server-commands' | |
16 | ||
17 | describe('Fast restream in live', function () { | |
18 | let server: PeerTubeServer | |
19 | ||
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 | |
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 | objectStorage: false | |
70 | }) | |
71 | ||
72 | return { ffmpegCommand, liveVideoUUID } | |
73 | } | |
74 | ||
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) | |
80 | ||
81 | try { | |
82 | await server.live.getSegmentFile({ videoUUID: liveId, segment: 0, playlistNumber: 0 }) | |
83 | await makeRawRequest({ url: video.streamingPlaylists[0].playlistUrl, expectedStatus: HttpStatusCode.OK_200 }) | |
84 | await makeRawRequest({ url: video.streamingPlaylists[0].segmentsSha256Url, expectedStatus: HttpStatusCode.OK_200 }) | |
85 | } catch (err) { | |
86 | // FIXME: try to debug error in CI "Unexpected end of JSON input" | |
87 | console.error(err) | |
88 | throw err | |
89 | } | |
90 | ||
91 | await wait(100) | |
92 | } | |
93 | } | |
94 | ||
95 | async function runTest (replay: boolean) { | |
96 | const { ffmpegCommand, liveVideoUUID } = await fastRestreamWrapper({ replay }) | |
97 | ||
98 | // TODO: remove, we try to debug a test timeout failure here | |
99 | console.log('Ensuring last live works') | |
100 | ||
101 | await ensureLastLiveWorks(liveVideoUUID) | |
102 | ||
103 | await stopFfmpeg(ffmpegCommand) | |
104 | await server.live.waitUntilWaiting({ videoId: liveVideoUUID }) | |
105 | ||
106 | // Wait for replays | |
107 | await waitJobs([ server ]) | |
108 | ||
109 | const { total, data: sessions } = await server.live.listSessions({ videoId: liveVideoUUID }) | |
110 | ||
111 | expect(total).to.equal(2) | |
112 | expect(sessions).to.have.lengthOf(2) | |
113 | ||
114 | for (const session of sessions) { | |
115 | expect(session.error).to.be.null | |
116 | ||
117 | if (replay) { | |
118 | expect(session.replayVideo).to.exist | |
119 | ||
120 | await server.videos.get({ id: session.replayVideo.uuid }) | |
121 | } else { | |
122 | expect(session.replayVideo).to.not.exist | |
123 | } | |
124 | } | |
125 | } | |
126 | ||
127 | before(async function () { | |
128 | this.timeout(120000) | |
129 | ||
130 | const env = { PEERTUBE_TEST_CONSTANTS_VIDEO_LIVE_CLEANUP_DELAY: '10000' } | |
131 | server = await createSingleServer(1, {}, { env }) | |
132 | ||
133 | // Get the access tokens | |
134 | await setAccessTokensToServers([ server ]) | |
135 | await setDefaultVideoChannel([ server ]) | |
136 | ||
137 | await server.config.enableMinimumTranscoding(false, true) | |
138 | await server.config.enableLive({ allowReplay: true, transcoding: true, resolutions: 'min' }) | |
139 | }) | |
140 | ||
141 | it('Should correctly fast restream in a permanent live with and without save replay', async function () { | |
142 | this.timeout(480000) | |
143 | ||
144 | // A test can take a long time, so prefer to run them in parallel | |
145 | await Promise.all([ | |
146 | runTest(true), | |
147 | runTest(false) | |
148 | ]) | |
149 | }) | |
150 | ||
151 | after(async function () { | |
152 | await cleanupTests([ server ]) | |
153 | }) | |
154 | }) |