1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { basename, join } from 'path'
6 import { removeFragmentedMP4Ext, uuidRegex } from '@shared/core-utils'
9 checkResolutionsInMasterPlaylist,
13 createMultipleServers,
17 setAccessTokensToServers,
20 } from '@shared/extra-utils'
21 import { HttpStatusCode, VideoStreamingPlaylistType } from '@shared/models'
22 import { DEFAULT_AUDIO_RESOLUTION } from '../../../initializers/constants'
24 const expect = chai.expect
26 async function checkHlsPlaylist (servers: PeerTubeServer[], videoUUID: string, hlsOnly: boolean, resolutions = [ 240, 360, 480, 720 ]) {
27 for (const server of servers) {
28 const videoDetails = await server.videos.get({ id: videoUUID })
29 const baseUrl = `http://${videoDetails.account.host}`
31 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
33 const hlsPlaylist = videoDetails.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
34 expect(hlsPlaylist).to.not.be.undefined
36 const hlsFiles = hlsPlaylist.files
37 expect(hlsFiles).to.have.lengthOf(resolutions.length)
39 if (hlsOnly) expect(videoDetails.files).to.have.lengthOf(0)
40 else expect(videoDetails.files).to.have.lengthOf(resolutions.length)
43 for (const resolution of resolutions) {
44 const file = hlsFiles.find(f => f.resolution.id === resolution)
45 expect(file).to.not.be.undefined
47 expect(file.magnetUri).to.have.lengthOf.above(2)
48 expect(file.torrentUrl).to.match(
49 new RegExp(`http://${server.host}/lazy-static/torrents/${uuidRegex}-${file.resolution.id}-hls.torrent`)
51 expect(file.fileUrl).to.match(
52 new RegExp(`${baseUrl}/static/streaming-playlists/hls/${videoDetails.uuid}/${uuidRegex}-${file.resolution.id}-fragmented.mp4`)
54 expect(file.resolution.label).to.equal(resolution + 'p')
56 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
57 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
59 const torrent = await webtorrentAdd(file.magnetUri, true)
60 expect(torrent.files).to.be.an('array')
61 expect(torrent.files.length).to.equal(1)
62 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
65 // Check master playlist
67 await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
69 const masterPlaylist = await server.streamingPlaylists.get({ url: hlsPlaylist.playlistUrl })
71 for (const resolution of resolutions) {
72 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
73 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
77 // Check resolution playlists
79 for (const resolution of resolutions) {
80 const file = hlsFiles.find(f => f.resolution.id === resolution)
81 const playlistName = removeFragmentedMP4Ext(basename(file.fileUrl)) + '.m3u8'
83 const subPlaylist = await server.streamingPlaylists.get({
84 url: `${baseUrl}/static/streaming-playlists/hls/${videoUUID}/${playlistName}`
87 expect(subPlaylist).to.match(new RegExp(`${uuidRegex}-${resolution}-fragmented.mp4`))
88 expect(subPlaylist).to.contain(basename(file.fileUrl))
93 const baseUrlAndPath = baseUrl + '/static/streaming-playlists/hls'
95 for (const resolution of resolutions) {
96 await checkSegmentHash({
98 baseUrlPlaylist: baseUrlAndPath,
99 baseUrlSegment: baseUrlAndPath,
109 describe('Test HLS videos', function () {
110 let servers: PeerTubeServer[] = []
112 let videoAudioUUID = ''
114 function runTestSuite (hlsOnly: boolean) {
116 it('Should upload a video and transcode it to HLS', async function () {
119 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video 1', fixture: 'video_short.webm' } })
122 await waitJobs(servers)
124 await checkHlsPlaylist(servers, videoUUID, hlsOnly)
127 it('Should upload an audio file and transcode it to HLS', async function () {
130 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video audio', fixture: 'sample.ogg' } })
131 videoAudioUUID = uuid
133 await waitJobs(servers)
135 await checkHlsPlaylist(servers, videoAudioUUID, hlsOnly, [ DEFAULT_AUDIO_RESOLUTION, 360, 240 ])
138 it('Should update the video', async function () {
141 await servers[0].videos.update({ id: videoUUID, attributes: { name: 'video 1 updated' } })
143 await waitJobs(servers)
145 await checkHlsPlaylist(servers, videoUUID, hlsOnly)
148 it('Should delete videos', async function () {
151 await servers[0].videos.remove({ id: videoUUID })
152 await servers[0].videos.remove({ id: videoAudioUUID })
154 await waitJobs(servers)
156 for (const server of servers) {
157 await server.videos.get({ id: videoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
158 await server.videos.get({ id: videoAudioUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
162 it('Should have the playlists/segment deleted from the disk', async function () {
163 for (const server of servers) {
164 await checkDirectoryIsEmpty(server, 'videos')
165 await checkDirectoryIsEmpty(server, join('streaming-playlists', 'hls'))
169 it('Should have an empty tmp directory', async function () {
170 for (const server of servers) {
171 await checkTmpIsEmpty(server)
176 before(async function () {
179 const configOverride = {
182 allow_audio_files: true,
188 servers = await createMultipleServers(2, configOverride)
190 // Get the access tokens
191 await setAccessTokensToServers(servers)
193 // Server 1 and server 2 follow each other
194 await doubleFollow(servers[0], servers[1])
197 describe('With WebTorrent & HLS enabled', function () {
201 describe('With only HLS enabled', function () {
203 before(async function () {
204 await servers[0].config.updateCustomSubConfig({
208 allowAudioFiles: true,
232 after(async function () {
233 await cleanupTests(servers)