]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/live/live-fast-restream.ts
Feature/Add replay privacy (#5692)
[github/Chocobozzz/PeerTube.git] / server / tests / api / live / live-fast-restream.ts
CommitLineData
53023be3
C
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
86347717 3import { expect } from 'chai'
53023be3
C
4import { wait } from '@shared/core-utils'
5import { HttpStatusCode, LiveVideoCreate, VideoPrivacy } from '@shared/models'
6import {
7 cleanupTests,
8 createSingleServer,
9 makeRawRequest,
10 PeerTubeServer,
11 setAccessTokensToServers,
12 setDefaultVideoChannel,
13 stopFfmpeg,
14 waitJobs
15} from '@shared/server-commands'
16
53023be3
C
17describe('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,
05a60d85 26 replaySettings: options.replay ? { privacy: VideoPrivacy.PUBLIC } : undefined,
53023be3
C
27 permanentLive: options.permanent
28 }
29
30 const { uuid } = await server.live.create({ fields: attributes })
31 return uuid
32 }
33
34 async function fastRestreamWrapper ({ replay }: { replay: boolean }) {
35 const liveVideoUUID = await createLiveWrapper({ permanent: true, replay })
36 await waitJobs([ server ])
37
38 const rtmpOptions = {
39 videoId: liveVideoUUID,
40 copyCodecs: true,
41 fixtureName: 'video_short.mp4'
42 }
43
44 // Streaming session #1
45 let ffmpegCommand = await server.live.sendRTMPStreamInVideo(rtmpOptions)
46 await server.live.waitUntilPublished({ videoId: liveVideoUUID })
bbae45c3
C
47
48 const video = await server.videos.get({ id: liveVideoUUID })
49 const session1PlaylistId = video.streamingPlaylists[0].id
50
53023be3
C
51 await stopFfmpeg(ffmpegCommand)
52 await server.live.waitUntilWaiting({ videoId: liveVideoUUID })
53
54 // Streaming session #2
55 ffmpegCommand = await server.live.sendRTMPStreamInVideo(rtmpOptions)
bbae45c3
C
56
57 let hasNewPlaylist = false
58 do {
59 const video = await server.videos.get({ id: liveVideoUUID })
60 hasNewPlaylist = video.streamingPlaylists.length === 1 && video.streamingPlaylists[0].id !== session1PlaylistId
61
62 await wait(100)
63 } while (!hasNewPlaylist)
64
65 await server.live.waitUntilSegmentGeneration({
66 server,
67 videoUUID: liveVideoUUID,
68 segment: 1,
69 playlistNumber: 0,
70 objectStorage: false
71 })
53023be3
C
72
73 return { ffmpegCommand, liveVideoUUID }
74 }
75
76 async function ensureLastLiveWorks (liveId: string) {
b6e2b5df 77 // Equivalent to PEERTUBE_TEST_CONSTANTS_VIDEO_LIVE_CLEANUP_DELAY
53023be3
C
78 for (let i = 0; i < 100; i++) {
79 const video = await server.videos.get({ id: liveId })
80 expect(video.streamingPlaylists).to.have.lengthOf(1)
81
518c5cc6
C
82 try {
83 await server.live.getSegmentFile({ videoUUID: liveId, segment: 0, playlistNumber: 0 })
84 await makeRawRequest({ url: video.streamingPlaylists[0].playlistUrl, expectedStatus: HttpStatusCode.OK_200 })
85 await makeRawRequest({ url: video.streamingPlaylists[0].segmentsSha256Url, expectedStatus: HttpStatusCode.OK_200 })
86 } catch (err) {
87 // FIXME: try to debug error in CI "Unexpected end of JSON input"
88 console.error(err)
89 throw err
90 }
53023be3
C
91
92 await wait(100)
93 }
94 }
95
96 async function runTest (replay: boolean) {
97 const { ffmpegCommand, liveVideoUUID } = await fastRestreamWrapper({ replay })
98
2a720a0f
C
99 // TODO: remove, we try to debug a test timeout failure here
100 console.log('Ensuring last live works')
101
53023be3
C
102 await ensureLastLiveWorks(liveVideoUUID)
103
104 await stopFfmpeg(ffmpegCommand)
105 await server.live.waitUntilWaiting({ videoId: liveVideoUUID })
106
107 // Wait for replays
108 await waitJobs([ server ])
109
110 const { total, data: sessions } = await server.live.listSessions({ videoId: liveVideoUUID })
111
112 expect(total).to.equal(2)
113 expect(sessions).to.have.lengthOf(2)
114
115 for (const session of sessions) {
116 expect(session.error).to.be.null
117
118 if (replay) {
119 expect(session.replayVideo).to.exist
120
121 await server.videos.get({ id: session.replayVideo.uuid })
122 } else {
123 expect(session.replayVideo).to.not.exist
124 }
125 }
126 }
127
128 before(async function () {
129 this.timeout(120000)
130
c3fb12b3 131 const env = { PEERTUBE_TEST_CONSTANTS_VIDEO_LIVE_CLEANUP_DELAY: '10000' }
53023be3
C
132 server = await createSingleServer(1, {}, { env })
133
134 // Get the access tokens
135 await setAccessTokensToServers([ server ])
136 await setDefaultVideoChannel([ server ])
137
138 await server.config.enableMinimumTranscoding(false, true)
139 await server.config.enableLive({ allowReplay: true, transcoding: true, resolutions: 'min' })
140 })
141
25691c99 142 it('Should correctly fast restream in a permanent live with and without save replay', async function () {
99b75748 143 this.timeout(480000)
53023be3
C
144
145 // A test can take a long time, so prefer to run them in parallel
146 await Promise.all([
147 runTest(true),
148 runTest(false)
149 ])
150 })
151
152 after(async function () {
153 await cleanupTests([ server ])
154 })
155})