]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/live/live-fast-restream.ts
Fix removed sha segments on fast restream
[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 'mocha'
4 import * as chai from 'chai'
5 import { wait } from '@shared/core-utils'
6 import { HttpStatusCode, LiveVideoCreate, VideoPrivacy } from '@shared/models'
7 import {
8 cleanupTests,
9 createSingleServer,
10 makeRawRequest,
11 PeerTubeServer,
12 setAccessTokensToServers,
13 setDefaultVideoChannel,
14 stopFfmpeg,
15 waitJobs
16 } from '@shared/server-commands'
17
18 const expect = chai.expect
19
20 describe('Fast restream in live', function () {
21 let server: PeerTubeServer
22
23 async function createLiveWrapper (options: { permanent: boolean, replay: boolean }) {
24 const attributes: LiveVideoCreate = {
25 channelId: server.store.channel.id,
26 privacy: VideoPrivacy.PUBLIC,
27 name: 'my super live',
28 saveReplay: options.replay,
29 permanentLive: options.permanent
30 }
31
32 const { uuid } = await server.live.create({ fields: attributes })
33 return uuid
34 }
35
36 async function fastRestreamWrapper ({ replay }: { replay: boolean }) {
37 const liveVideoUUID = await createLiveWrapper({ permanent: true, replay })
38 await waitJobs([ server ])
39
40 const rtmpOptions = {
41 videoId: liveVideoUUID,
42 copyCodecs: true,
43 fixtureName: 'video_short.mp4'
44 }
45
46 // Streaming session #1
47 let ffmpegCommand = await server.live.sendRTMPStreamInVideo(rtmpOptions)
48 await server.live.waitUntilPublished({ videoId: liveVideoUUID })
49 await stopFfmpeg(ffmpegCommand)
50 await server.live.waitUntilWaiting({ videoId: liveVideoUUID })
51
52 // Streaming session #2
53 ffmpegCommand = await server.live.sendRTMPStreamInVideo(rtmpOptions)
54 await server.live.waitUntilSegmentGeneration({ videoUUID: liveVideoUUID, segment: 0, playlistNumber: 0, totalSessions: 2 })
55
56 return { ffmpegCommand, liveVideoUUID }
57 }
58
59 async function ensureLastLiveWorks (liveId: string) {
60 // Equivalent to PEERTUBE_TEST_CONSTANTS_VIDEO_LIVE_CLEANUP_DELAY
61 for (let i = 0; i < 100; i++) {
62 const video = await server.videos.get({ id: liveId })
63 expect(video.streamingPlaylists).to.have.lengthOf(1)
64
65 await server.live.getSegment({ videoUUID: liveId, segment: 0, playlistNumber: 0 })
66 await makeRawRequest(video.streamingPlaylists[0].playlistUrl, HttpStatusCode.OK_200)
67 await makeRawRequest(video.streamingPlaylists[0].segmentsSha256Url, HttpStatusCode.OK_200)
68
69 await wait(100)
70 }
71 }
72
73 async function runTest (replay: boolean) {
74 const { ffmpegCommand, liveVideoUUID } = await fastRestreamWrapper({ replay })
75
76 await ensureLastLiveWorks(liveVideoUUID)
77
78 await stopFfmpeg(ffmpegCommand)
79 await server.live.waitUntilWaiting({ videoId: liveVideoUUID })
80
81 // Wait for replays
82 await waitJobs([ server ])
83
84 const { total, data: sessions } = await server.live.listSessions({ videoId: liveVideoUUID })
85
86 expect(total).to.equal(2)
87 expect(sessions).to.have.lengthOf(2)
88
89 for (const session of sessions) {
90 expect(session.error).to.be.null
91
92 if (replay) {
93 expect(session.replayVideo).to.exist
94
95 await server.videos.get({ id: session.replayVideo.uuid })
96 } else {
97 expect(session.replayVideo).to.not.exist
98 }
99 }
100 }
101
102 before(async function () {
103 this.timeout(120000)
104
105 const env = { 'PEERTUBE_TEST_CONSTANTS_VIDEO_LIVE_CLEANUP_DELAY': '10000' }
106 server = await createSingleServer(1, {}, { env })
107
108 // Get the access tokens
109 await setAccessTokensToServers([ server ])
110 await setDefaultVideoChannel([ server ])
111
112 await server.config.enableMinimumTranscoding(false, true)
113 await server.config.enableLive({ allowReplay: true, transcoding: true, resolutions: 'min' })
114 })
115
116 it('Should correctly fast reastream in a permanent live with and without save replay', async function () {
117 this.timeout(240000)
118
119 // A test can take a long time, so prefer to run them in parallel
120 await Promise.all([
121 runTest(true),
122 runTest(false)
123 ])
124 })
125
126 after(async function () {
127 await cleanupTests([ server ])
128 })
129 })