]>
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 | await stopFfmpeg(ffmpegCommand) | |
47 | await server.live.waitUntilWaiting({ videoId: liveVideoUUID }) | |
48 | ||
49 | // Streaming session #2 | |
50 | ffmpegCommand = await server.live.sendRTMPStreamInVideo(rtmpOptions) | |
51 | await server.live.waitUntilSegmentGeneration({ videoUUID: liveVideoUUID, segment: 0, playlistNumber: 0, totalSessions: 2 }) | |
52 | ||
53 | return { ffmpegCommand, liveVideoUUID } | |
54 | } | |
55 | ||
56 | async function ensureLastLiveWorks (liveId: string) { | |
57 | // Equivalent to PEERTUBE_TEST_CONSTANTS_VIDEO_LIVE_CLEANUP_DELAY | |
58 | for (let i = 0; i < 100; i++) { | |
59 | const video = await server.videos.get({ id: liveId }) | |
60 | expect(video.streamingPlaylists).to.have.lengthOf(1) | |
61 | ||
62 | await server.live.getSegment({ videoUUID: liveId, segment: 0, playlistNumber: 0 }) | |
63 | await makeRawRequest(video.streamingPlaylists[0].playlistUrl, HttpStatusCode.OK_200) | |
64 | await makeRawRequest(video.streamingPlaylists[0].segmentsSha256Url, HttpStatusCode.OK_200) | |
65 | ||
66 | await wait(100) | |
67 | } | |
68 | } | |
69 | ||
70 | async function runTest (replay: boolean) { | |
71 | const { ffmpegCommand, liveVideoUUID } = await fastRestreamWrapper({ replay }) | |
72 | ||
73 | await ensureLastLiveWorks(liveVideoUUID) | |
74 | ||
75 | await stopFfmpeg(ffmpegCommand) | |
76 | await server.live.waitUntilWaiting({ videoId: liveVideoUUID }) | |
77 | ||
78 | // Wait for replays | |
79 | await waitJobs([ server ]) | |
80 | ||
81 | const { total, data: sessions } = await server.live.listSessions({ videoId: liveVideoUUID }) | |
82 | ||
83 | expect(total).to.equal(2) | |
84 | expect(sessions).to.have.lengthOf(2) | |
85 | ||
86 | for (const session of sessions) { | |
87 | expect(session.error).to.be.null | |
88 | ||
89 | if (replay) { | |
90 | expect(session.replayVideo).to.exist | |
91 | ||
92 | await server.videos.get({ id: session.replayVideo.uuid }) | |
93 | } else { | |
94 | expect(session.replayVideo).to.not.exist | |
95 | } | |
96 | } | |
97 | } | |
98 | ||
99 | before(async function () { | |
100 | this.timeout(120000) | |
101 | ||
102 | const env = { PEERTUBE_TEST_CONSTANTS_VIDEO_LIVE_CLEANUP_DELAY: '10000' } | |
103 | server = await createSingleServer(1, {}, { env }) | |
104 | ||
105 | // Get the access tokens | |
106 | await setAccessTokensToServers([ server ]) | |
107 | await setDefaultVideoChannel([ server ]) | |
108 | ||
109 | await server.config.enableMinimumTranscoding(false, true) | |
110 | await server.config.enableLive({ allowReplay: true, transcoding: true, resolutions: 'min' }) | |
111 | }) | |
112 | ||
113 | it('Should correctly fast reastream in a permanent live with and without save replay', async function () { | |
114 | this.timeout(240000) | |
115 | ||
116 | // A test can take a long time, so prefer to run them in parallel | |
117 | await Promise.all([ | |
118 | runTest(true), | |
119 | runTest(false) | |
120 | ]) | |
121 | }) | |
122 | ||
123 | after(async function () { | |
124 | await cleanupTests([ server ]) | |
125 | }) | |
126 | }) |