1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { join } from 'path'
8 checkResolutionsInMasterPlaylist,
12 createMultipleServers,
16 setAccessTokensToServers,
19 } from '@shared/extra-utils'
20 import { HttpStatusCode, VideoStreamingPlaylistType } from '@shared/models'
21 import { DEFAULT_AUDIO_RESOLUTION } from '../../../initializers/constants'
22 import { uuidRegex } from '@shared/core-utils'
23 import { basename } from 'path/posix'
25 const expect = chai.expect
27 async function checkHlsPlaylist (servers: PeerTubeServer[], videoUUID: string, hlsOnly: boolean, resolutions = [ 240, 360, 480, 720 ]) {
28 for (const server of servers) {
29 const videoDetails = await server.videos.get({ id: videoUUID })
30 const baseUrl = `http://${videoDetails.account.host}`
32 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
34 const hlsPlaylist = videoDetails.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
35 expect(hlsPlaylist).to.not.be.undefined
37 const hlsFiles = hlsPlaylist.files
38 expect(hlsFiles).to.have.lengthOf(resolutions.length)
40 if (hlsOnly) expect(videoDetails.files).to.have.lengthOf(0)
41 else expect(videoDetails.files).to.have.lengthOf(resolutions.length)
44 for (const resolution of resolutions) {
45 const file = hlsFiles.find(f => f.resolution.id === resolution)
46 expect(file).to.not.be.undefined
48 expect(file.magnetUri).to.have.lengthOf.above(2)
49 expect(file.torrentUrl).to.match(
50 new RegExp(`http://${server.host}/lazy-static/torrents/${uuidRegex}-${file.resolution.id}-hls.torrent`)
52 expect(file.fileUrl).to.match(
53 new RegExp(`${baseUrl}/static/streaming-playlists/hls/${videoDetails.uuid}/${uuidRegex}-${file.resolution.id}-fragmented.mp4`)
55 expect(file.resolution.label).to.equal(resolution + 'p')
57 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
58 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
60 const torrent = await webtorrentAdd(file.magnetUri, true)
61 expect(torrent.files).to.be.an('array')
62 expect(torrent.files.length).to.equal(1)
63 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
66 // Check master playlist
68 await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
70 const masterPlaylist = await server.streamingPlaylists.get({ url: hlsPlaylist.playlistUrl })
72 for (const resolution of resolutions) {
73 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
74 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
78 // Check resolution playlists
80 for (const resolution of resolutions) {
81 const subPlaylist = await server.streamingPlaylists.get({
82 url: `${baseUrl}/static/streaming-playlists/hls/${videoUUID}/${resolution}.m3u8`
85 const file = hlsFiles.find(f => f.resolution.id === resolution)
86 expect(subPlaylist).to.match(new RegExp(`${uuidRegex}-${resolution}-fragmented.mp4`))
87 expect(subPlaylist).to.contain(basename(file.fileUrl))
92 const baseUrlAndPath = baseUrl + '/static/streaming-playlists/hls'
94 for (const resolution of resolutions) {
95 await checkSegmentHash({
97 baseUrlPlaylist: baseUrlAndPath,
98 baseUrlSegment: baseUrlAndPath,
108 describe('Test HLS videos', function () {
109 let servers: PeerTubeServer[] = []
111 let videoAudioUUID = ''
113 function runTestSuite (hlsOnly: boolean) {
115 it('Should upload a video and transcode it to HLS', async function () {
118 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video 1', fixture: 'video_short.webm' } })
121 await waitJobs(servers)
123 await checkHlsPlaylist(servers, videoUUID, hlsOnly)
126 it('Should upload an audio file and transcode it to HLS', async function () {
129 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video audio', fixture: 'sample.ogg' } })
130 videoAudioUUID = uuid
132 await waitJobs(servers)
134 await checkHlsPlaylist(servers, videoAudioUUID, hlsOnly, [ DEFAULT_AUDIO_RESOLUTION, 360, 240 ])
137 it('Should update the video', async function () {
140 await servers[0].videos.update({ id: videoUUID, attributes: { name: 'video 1 updated' } })
142 await waitJobs(servers)
144 await checkHlsPlaylist(servers, videoUUID, hlsOnly)
147 it('Should delete videos', async function () {
150 await servers[0].videos.remove({ id: videoUUID })
151 await servers[0].videos.remove({ id: videoAudioUUID })
153 await waitJobs(servers)
155 for (const server of servers) {
156 await server.videos.get({ id: videoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
157 await server.videos.get({ id: videoAudioUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
161 it('Should have the playlists/segment deleted from the disk', async function () {
162 for (const server of servers) {
163 await checkDirectoryIsEmpty(server, 'videos')
164 await checkDirectoryIsEmpty(server, join('streaming-playlists', 'hls'))
168 it('Should have an empty tmp directory', async function () {
169 for (const server of servers) {
170 await checkTmpIsEmpty(server)
175 before(async function () {
178 const configOverride = {
181 allow_audio_files: true,
187 servers = await createMultipleServers(2, configOverride)
189 // Get the access tokens
190 await setAccessTokensToServers(servers)
192 // Server 1 and server 2 follow each other
193 await doubleFollow(servers[0], servers[1])
196 describe('With WebTorrent & HLS enabled', function () {
200 describe('With only HLS enabled', function () {
202 before(async function () {
203 await servers[0].config.updateCustomSubConfig({
207 allowAudioFiles: true,
231 after(async function () {
232 await cleanupTests(servers)