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