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 { LiveVideo, VideoStreamingPlaylistType } from '@shared/models'
7 import { ObjectStorageCommand, PeerTubeServer } from '@shared/server-commands'
8 import { checkLiveSegmentHash, checkResolutionsInMasterPlaylist } from './streaming-playlists'
10 async function checkLiveCleanup (options: {
11 server: PeerTubeServer
14 savedResolutions?: number[]
16 const { server, videoUUID, permanent, savedResolutions = [] } = options
18 const basePath = server.servers.buildDirectory('streaming-playlists')
19 const hlsPath = join(basePath, 'hls', videoUUID)
22 if (!await pathExists(hlsPath)) return
24 const files = await readdir(hlsPath)
25 expect(files).to.have.lengthOf(0)
29 if (savedResolutions.length === 0) {
30 return checkUnsavedLiveCleanup(server, videoUUID, hlsPath)
33 return checkSavedLiveCleanup(hlsPath, savedResolutions)
36 // ---------------------------------------------------------------------------
38 async function testVideoResolutions (options: {
39 originServer: PeerTubeServer
40 servers: PeerTubeServer[]
44 objectStorage: boolean
46 const { originServer, servers, liveVideoId, resolutions, transcoded, objectStorage } = options
48 for (const server of servers) {
49 const { data } = await server.videos.list()
50 expect(data.find(v => v.uuid === liveVideoId)).to.exist
52 const video = await server.videos.get({ id: liveVideoId })
53 expect(video.streamingPlaylists).to.have.lengthOf(1)
55 const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS)
56 expect(hlsPlaylist).to.exist
57 expect(hlsPlaylist.files).to.have.lengthOf(0) // Only fragmented mp4 files are displayed
59 await checkResolutionsInMasterPlaylist({
61 playlistUrl: hlsPlaylist.playlistUrl,
64 withRetry: objectStorage
68 expect(hlsPlaylist.playlistUrl).to.contain(ObjectStorageCommand.getMockPlaylistBaseUrl())
71 for (let i = 0; i < resolutions.length; i++) {
73 const segmentName = `${i}-00000${segmentNum}.ts`
74 await originServer.live.waitUntilSegmentGeneration({
76 videoUUID: video.uuid,
82 const baseUrl = objectStorage
83 ? ObjectStorageCommand.getMockPlaylistBaseUrl() + 'hls'
84 : originServer.url + '/static/streaming-playlists/hls'
87 expect(hlsPlaylist.segmentsSha256Url).to.contain(ObjectStorageCommand.getMockPlaylistBaseUrl())
90 const subPlaylist = await originServer.streamingPlaylists.get({
91 url: `${baseUrl}/${video.uuid}/${i}.m3u8`,
92 withRetry: objectStorage // With object storage, the request may fail because of inconsistent data in S3
95 expect(subPlaylist).to.contain(segmentName)
97 await checkLiveSegmentHash({
99 baseUrlSegment: baseUrl,
100 videoUUID: video.uuid,
108 // ---------------------------------------------------------------------------
115 // ---------------------------------------------------------------------------
117 async function checkSavedLiveCleanup (hlsPath: string, savedResolutions: number[] = []) {
118 const files = await readdir(hlsPath)
120 // fragmented file and playlist per resolution + master playlist + segments sha256 json file
121 expect(files).to.have.lengthOf(savedResolutions.length * 2 + 2)
123 for (const resolution of savedResolutions) {
124 const fragmentedFile = files.find(f => f.endsWith(`-${resolution}-fragmented.mp4`))
125 expect(fragmentedFile).to.exist
127 const playlistFile = files.find(f => f.endsWith(`${resolution}.m3u8`))
128 expect(playlistFile).to.exist
131 const masterPlaylistFile = files.find(f => f.endsWith('-master.m3u8'))
132 expect(masterPlaylistFile).to.exist
134 const shaFile = files.find(f => f.endsWith('-segments-sha256.json'))
135 expect(shaFile).to.exist
138 async function checkUnsavedLiveCleanup (server: PeerTubeServer, videoUUID: string, hlsPath: string) {
142 live = await server.live.get({ videoId: videoUUID })
145 if (live?.permanentLive) {
146 expect(await pathExists(hlsPath)).to.be.true
148 const hlsFiles = await readdir(hlsPath)
149 expect(hlsFiles).to.have.lengthOf(1) // Only replays directory
151 const replayDir = join(hlsPath, 'replay')
152 expect(await pathExists(replayDir)).to.be.true
154 const replayFiles = await readdir(join(hlsPath, 'replay'))
155 expect(replayFiles).to.have.lengthOf(0)
160 expect(await pathExists(hlsPath)).to.be.false