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