1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { expectStartWith, testVideoResolutions } from '@server/tests/shared'
5 import { areMockObjectStorageTestsDisabled } from '@shared/core-utils'
6 import { HttpStatusCode, LiveVideoCreate, VideoPrivacy } from '@shared/models'
10 findExternalSavedVideo,
15 setAccessTokensToServers,
16 setDefaultVideoChannel,
19 waitUntilLivePublishedOnAllServers,
20 waitUntilLiveReplacedByReplayOnAllServers,
21 waitUntilLiveWaitingOnAllServers
22 } from '@shared/server-commands'
24 async function createLive (server: PeerTubeServer, permanent: boolean) {
25 const attributes: LiveVideoCreate = {
26 channelId: server.store.channel.id,
27 privacy: VideoPrivacy.PUBLIC,
28 name: 'my super live',
30 permanentLive: permanent
33 const { uuid } = await server.live.create({ fields: attributes })
38 async function checkFilesExist (servers: PeerTubeServer[], videoUUID: string, numberOfFiles: number) {
39 for (const server of servers) {
40 const video = await server.videos.get({ id: videoUUID })
42 expect(video.files).to.have.lengthOf(0)
43 expect(video.streamingPlaylists).to.have.lengthOf(1)
45 const files = video.streamingPlaylists[0].files
46 expect(files).to.have.lengthOf(numberOfFiles)
48 for (const file of files) {
49 expectStartWith(file.fileUrl, ObjectStorageCommand.getMockPlaylistBaseUrl())
51 await makeRawRequest({ url: file.fileUrl, expectedStatus: HttpStatusCode.OK_200 })
56 async function checkFilesCleanup (server: PeerTubeServer, videoUUID: string, resolutions: number[]) {
57 const resolutionFiles = resolutions.map((_value, i) => `${i}.m3u8`)
59 for (const playlistName of [ 'master.m3u8' ].concat(resolutionFiles)) {
60 await server.live.getPlaylistFile({
63 expectedStatus: HttpStatusCode.NOT_FOUND_404,
68 await server.live.getSegmentFile({
73 expectedStatus: HttpStatusCode.NOT_FOUND_404
77 describe('Object storage for lives', function () {
78 if (areMockObjectStorageTestsDisabled()) return
80 let servers: PeerTubeServer[]
82 before(async function () {
85 await ObjectStorageCommand.prepareDefaultMockBuckets()
87 servers = await createMultipleServers(2, ObjectStorageCommand.getDefaultMockConfig())
89 await setAccessTokensToServers(servers)
90 await setDefaultVideoChannel(servers)
91 await doubleFollow(servers[0], servers[1])
93 await servers[0].config.enableTranscoding()
96 describe('Without live transcoding', async function () {
99 before(async function () {
100 await servers[0].config.enableLive({ transcoding: false })
102 videoUUID = await createLive(servers[0], false)
105 it('Should create a live and publish it on object storage', async function () {
108 const ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: videoUUID })
109 await waitUntilLivePublishedOnAllServers(servers, videoUUID)
111 await testVideoResolutions({
112 originServer: servers[0],
114 liveVideoId: videoUUID,
115 resolutions: [ 720 ],
120 await stopFfmpeg(ffmpegCommand)
123 it('Should have saved the replay on object storage', async function () {
126 await waitUntilLiveReplacedByReplayOnAllServers(servers, videoUUID)
127 await waitJobs(servers)
129 await checkFilesExist(servers, videoUUID, 1)
132 it('Should have cleaned up live files from object storage', async function () {
133 await checkFilesCleanup(servers[0], videoUUID, [ 720 ])
137 describe('With live transcoding', async function () {
138 const resolutions = [ 720, 480, 360, 240, 144 ]
140 before(async function () {
141 await servers[0].config.enableLive({ transcoding: true })
144 describe('Normal replay', function () {
145 let videoUUIDNonPermanent: string
147 before(async function () {
148 videoUUIDNonPermanent = await createLive(servers[0], false)
151 it('Should create a live and publish it on object storage', async function () {
154 const ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: videoUUIDNonPermanent })
155 await waitUntilLivePublishedOnAllServers(servers, videoUUIDNonPermanent)
157 await testVideoResolutions({
158 originServer: servers[0],
160 liveVideoId: videoUUIDNonPermanent,
166 await stopFfmpeg(ffmpegCommand)
169 it('Should have saved the replay on object storage', async function () {
172 await waitUntilLiveReplacedByReplayOnAllServers(servers, videoUUIDNonPermanent)
173 await waitJobs(servers)
175 await checkFilesExist(servers, videoUUIDNonPermanent, 5)
178 it('Should have cleaned up live files from object storage', async function () {
179 await checkFilesCleanup(servers[0], videoUUIDNonPermanent, resolutions)
183 describe('Permanent replay', function () {
184 let videoUUIDPermanent: string
186 before(async function () {
187 videoUUIDPermanent = await createLive(servers[0], true)
190 it('Should create a live and publish it on object storage', async function () {
193 const ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: videoUUIDPermanent })
194 await waitUntilLivePublishedOnAllServers(servers, videoUUIDPermanent)
196 await testVideoResolutions({
197 originServer: servers[0],
199 liveVideoId: videoUUIDPermanent,
205 await stopFfmpeg(ffmpegCommand)
208 it('Should have saved the replay on object storage', async function () {
211 await waitUntilLiveWaitingOnAllServers(servers, videoUUIDPermanent)
212 await waitJobs(servers)
214 const videoLiveDetails = await servers[0].videos.get({ id: videoUUIDPermanent })
215 const replay = await findExternalSavedVideo(servers[0], videoLiveDetails)
217 await checkFilesExist(servers, replay.uuid, 5)
220 it('Should have cleaned up live files from object storage', async function () {
221 await checkFilesCleanup(servers[0], videoUUIDPermanent, resolutions)
226 after(async function () {
227 await killallServers(servers)