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