1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
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'
11 async function checkLiveCleanup (options: {
12 server: PeerTubeServer
15 savedResolutions?: number[]
17 const { server, videoUUID, permanent, savedResolutions = [] } = options
19 const basePath = server.servers.buildDirectory('streaming-playlists')
20 const hlsPath = join(basePath, 'hls', videoUUID)
23 if (!await pathExists(hlsPath)) return
25 const files = await readdir(hlsPath)
26 expect(files).to.have.lengthOf(0)
30 if (savedResolutions.length === 0) {
31 return checkUnsavedLiveCleanup(server, videoUUID, hlsPath)
34 return checkSavedLiveCleanup(hlsPath, savedResolutions)
37 // ---------------------------------------------------------------------------
39 async function testVideoResolutions (options: {
40 originServer: PeerTubeServer
41 servers: PeerTubeServer[]
46 objectStorage: boolean
47 objectStorageBaseUrl?: string
56 objectStorageBaseUrl = ObjectStorageCommand.getMockPlaylistBaseUrl()
59 for (const server of servers) {
60 const { data } = await server.videos.list()
61 expect(data.find(v => v.uuid === liveVideoId)).to.exist
63 const video = await server.videos.get({ id: liveVideoId })
64 expect(video.streamingPlaylists).to.have.lengthOf(1)
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
70 await checkResolutionsInMasterPlaylist({
72 playlistUrl: hlsPlaylist.playlistUrl,
75 withRetry: objectStorage
79 expect(hlsPlaylist.playlistUrl).to.contain(objectStorageBaseUrl)
82 for (let i = 0; i < resolutions.length; i++) {
84 const segmentName = `${i}-00000${segmentNum}.ts`
85 await originServer.live.waitUntilSegmentGeneration({
87 videoUUID: video.uuid,
94 const baseUrl = objectStorage
95 ? join(objectStorageBaseUrl, 'hls')
96 : originServer.url + '/static/streaming-playlists/hls'
99 expect(hlsPlaylist.segmentsSha256Url).to.contain(objectStorageBaseUrl)
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
107 expect(subPlaylist).to.contain(segmentName)
109 await checkLiveSegmentHash({
111 baseUrlSegment: baseUrl,
112 videoUUID: video.uuid,
117 if (originServer.internalServerNumber === server.internalServerNumber) {
118 const infohash = sha1(`${2 + hlsPlaylist.playlistUrl}+V${i}`)
119 const dbInfohashes = await originServer.sql.getPlaylistInfohash(hlsPlaylist.id)
121 expect(dbInfohashes).to.include(infohash)
127 // ---------------------------------------------------------------------------
134 // ---------------------------------------------------------------------------
136 async function checkSavedLiveCleanup (hlsPath: string, savedResolutions: number[] = []) {
137 const files = await readdir(hlsPath)
139 // fragmented file and playlist per resolution + master playlist + segments sha256 json file
140 expect(files).to.have.lengthOf(savedResolutions.length * 2 + 2)
142 for (const resolution of savedResolutions) {
143 const fragmentedFile = files.find(f => f.endsWith(`-${resolution}-fragmented.mp4`))
144 expect(fragmentedFile).to.exist
146 const playlistFile = files.find(f => f.endsWith(`${resolution}.m3u8`))
147 expect(playlistFile).to.exist
150 const masterPlaylistFile = files.find(f => f.endsWith('-master.m3u8'))
151 expect(masterPlaylistFile).to.exist
153 const shaFile = files.find(f => f.endsWith('-segments-sha256.json'))
154 expect(shaFile).to.exist
157 async function checkUnsavedLiveCleanup (server: PeerTubeServer, videoUUID: string, hlsPath: string) {
161 live = await server.live.get({ videoId: videoUUID })
164 if (live?.permanentLive) {
165 expect(await pathExists(hlsPath)).to.be.true
167 const hlsFiles = await readdir(hlsPath)
168 expect(hlsFiles).to.have.lengthOf(1) // Only replays directory
170 const replayDir = join(hlsPath, 'replay')
171 expect(await pathExists(replayDir)).to.be.true
173 const replayFiles = await readdir(join(hlsPath, 'replay'))
174 expect(replayFiles).to.have.lengthOf(0)
179 expect(await pathExists(hlsPath)).to.be.false