]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/shared/live.ts
Fix live with base url object storage
[github/Chocobozzz/PeerTube.git] / server / tests / shared / live.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import { expect } from 'chai'
4 import { pathExists, readdir } from 'fs-extra'
5 import { join } from 'path'
6 import { sha1 } from '@shared/extra-utils'
7 import { LiveVideo, VideoStreamingPlaylistType } from '@shared/models'
8 import { ObjectStorageCommand, PeerTubeServer } from '@shared/server-commands'
9 import { checkLiveSegmentHash, checkResolutionsInMasterPlaylist } from './streaming-playlists'
10
11 async function checkLiveCleanup (options: {
12 server: PeerTubeServer
13 videoUUID: string
14 permanent: boolean
15 savedResolutions?: number[]
16 }) {
17 const { server, videoUUID, permanent, savedResolutions = [] } = options
18
19 const basePath = server.servers.buildDirectory('streaming-playlists')
20 const hlsPath = join(basePath, 'hls', videoUUID)
21
22 if (permanent) {
23 if (!await pathExists(hlsPath)) return
24
25 const files = await readdir(hlsPath)
26 expect(files).to.have.lengthOf(0)
27 return
28 }
29
30 if (savedResolutions.length === 0) {
31 return checkUnsavedLiveCleanup(server, videoUUID, hlsPath)
32 }
33
34 return checkSavedLiveCleanup(hlsPath, savedResolutions)
35 }
36
37 // ---------------------------------------------------------------------------
38
39 async function testVideoResolutions (options: {
40 originServer: PeerTubeServer
41 servers: PeerTubeServer[]
42 liveVideoId: string
43 resolutions: number[]
44 transcoded: boolean
45
46 objectStorage: boolean
47 objectStorageBaseUrl?: string
48 }) {
49 const {
50 originServer,
51 servers,
52 liveVideoId,
53 resolutions,
54 transcoded,
55 objectStorage,
56 objectStorageBaseUrl = ObjectStorageCommand.getMockPlaylistBaseUrl()
57 } = options
58
59 for (const server of servers) {
60 const { data } = await server.videos.list()
61 expect(data.find(v => v.uuid === liveVideoId)).to.exist
62
63 const video = await server.videos.get({ id: liveVideoId })
64 expect(video.streamingPlaylists).to.have.lengthOf(1)
65
66 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
67 expect(hlsPlaylist).to.exist
68 expect(hlsPlaylist.files).to.have.lengthOf(0) // Only fragmented mp4 files are displayed
69
70 await checkResolutionsInMasterPlaylist({
71 server,
72 playlistUrl: hlsPlaylist.playlistUrl,
73 resolutions,
74 transcoded,
75 withRetry: objectStorage
76 })
77
78 if (objectStorage) {
79 expect(hlsPlaylist.playlistUrl).to.contain(objectStorageBaseUrl)
80 }
81
82 for (let i = 0; i < resolutions.length; i++) {
83 const segmentNum = 3
84 const segmentName = `${i}-00000${segmentNum}.ts`
85 await originServer.live.waitUntilSegmentGeneration({
86 server: originServer,
87 videoUUID: video.uuid,
88 playlistNumber: i,
89 segment: segmentNum,
90 objectStorage,
91 objectStorageBaseUrl
92 })
93
94 const baseUrl = objectStorage
95 ? join(objectStorageBaseUrl, 'hls')
96 : originServer.url + '/static/streaming-playlists/hls'
97
98 if (objectStorage) {
99 expect(hlsPlaylist.segmentsSha256Url).to.contain(objectStorageBaseUrl)
100 }
101
102 const subPlaylist = await originServer.streamingPlaylists.get({
103 url: `${baseUrl}/${video.uuid}/${i}.m3u8`,
104 withRetry: objectStorage // With object storage, the request may fail because of inconsistent data in S3
105 })
106
107 expect(subPlaylist).to.contain(segmentName)
108
109 await checkLiveSegmentHash({
110 server,
111 baseUrlSegment: baseUrl,
112 videoUUID: video.uuid,
113 segmentName,
114 hlsPlaylist
115 })
116
117 if (originServer.internalServerNumber === server.internalServerNumber) {
118 const infohash = sha1(`${2 + hlsPlaylist.playlistUrl}+V${i}`)
119 const dbInfohashes = await originServer.sql.getPlaylistInfohash(hlsPlaylist.id)
120
121 expect(dbInfohashes).to.include(infohash)
122 }
123 }
124 }
125 }
126
127 // ---------------------------------------------------------------------------
128
129 export {
130 checkLiveCleanup,
131 testVideoResolutions
132 }
133
134 // ---------------------------------------------------------------------------
135
136 async function checkSavedLiveCleanup (hlsPath: string, savedResolutions: number[] = []) {
137 const files = await readdir(hlsPath)
138
139 // fragmented file and playlist per resolution + master playlist + segments sha256 json file
140 expect(files).to.have.lengthOf(savedResolutions.length * 2 + 2)
141
142 for (const resolution of savedResolutions) {
143 const fragmentedFile = files.find(f => f.endsWith(`-${resolution}-fragmented.mp4`))
144 expect(fragmentedFile).to.exist
145
146 const playlistFile = files.find(f => f.endsWith(`${resolution}.m3u8`))
147 expect(playlistFile).to.exist
148 }
149
150 const masterPlaylistFile = files.find(f => f.endsWith('-master.m3u8'))
151 expect(masterPlaylistFile).to.exist
152
153 const shaFile = files.find(f => f.endsWith('-segments-sha256.json'))
154 expect(shaFile).to.exist
155 }
156
157 async function checkUnsavedLiveCleanup (server: PeerTubeServer, videoUUID: string, hlsPath: string) {
158 let live: LiveVideo
159
160 try {
161 live = await server.live.get({ videoId: videoUUID })
162 } catch {}
163
164 if (live?.permanentLive) {
165 expect(await pathExists(hlsPath)).to.be.true
166
167 const hlsFiles = await readdir(hlsPath)
168 expect(hlsFiles).to.have.lengthOf(1) // Only replays directory
169
170 const replayDir = join(hlsPath, 'replay')
171 expect(await pathExists(replayDir)).to.be.true
172
173 const replayFiles = await readdir(join(hlsPath, 'replay'))
174 expect(replayFiles).to.have.lengthOf(0)
175
176 return
177 }
178
179 expect(await pathExists(hlsPath)).to.be.false
180 }