1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { join } from 'path'
6 import { HttpStatusCode } from '@shared/core-utils'
9 checkResolutionsInMasterPlaylist,
14 createMultipleServers,
17 setAccessTokensToServers,
20 } from '@shared/extra-utils'
21 import { 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)
42 for (const resolution of resolutions) {
43 const file = hlsFiles.find(f => f.resolution.id === resolution)
44 expect(file).to.not.be.undefined
46 expect(file.magnetUri).to.have.lengthOf.above(2)
47 expect(file.torrentUrl).to.equal(`http://${server.host}/lazy-static/torrents/${videoDetails.uuid}-${file.resolution.id}-hls.torrent`)
48 expect(file.fileUrl).to.equal(
49 `${baseUrl}/static/streaming-playlists/hls/${videoDetails.uuid}/${videoDetails.uuid}-${file.resolution.id}-fragmented.mp4`
51 expect(file.resolution.label).to.equal(resolution + 'p')
53 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
54 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
56 const torrent = await webtorrentAdd(file.magnetUri, true)
57 expect(torrent.files).to.be.an('array')
58 expect(torrent.files.length).to.equal(1)
59 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
63 await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
65 const masterPlaylist = await server.streamingPlaylists.get({ url: hlsPlaylist.playlistUrl })
67 for (const resolution of resolutions) {
68 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
69 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
74 for (const resolution of resolutions) {
75 const subPlaylist = await server.streamingPlaylists.get({
76 url: `${baseUrl}/static/streaming-playlists/hls/${videoUUID}/${resolution}.m3u8`
79 expect(subPlaylist).to.contain(`${videoUUID}-${resolution}-fragmented.mp4`)
84 const baseUrlAndPath = baseUrl + '/static/streaming-playlists/hls'
86 for (const resolution of resolutions) {
87 await checkSegmentHash({
89 baseUrlPlaylist: baseUrlAndPath,
90 baseUrlSegment: baseUrlAndPath,
100 describe('Test HLS videos', function () {
101 let servers: PeerTubeServer[] = []
103 let videoAudioUUID = ''
105 function runTestSuite (hlsOnly: boolean) {
107 it('Should upload a video and transcode it to HLS', async function () {
110 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video 1', fixture: 'video_short.webm' } })
113 await waitJobs(servers)
115 await checkHlsPlaylist(servers, videoUUID, hlsOnly)
118 it('Should upload an audio file and transcode it to HLS', async function () {
121 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video audio', fixture: 'sample.ogg' } })
122 videoAudioUUID = uuid
124 await waitJobs(servers)
126 await checkHlsPlaylist(servers, videoAudioUUID, hlsOnly, [ DEFAULT_AUDIO_RESOLUTION, 360, 240 ])
129 it('Should update the video', async function () {
132 await servers[0].videos.update({ id: videoUUID, attributes: { name: 'video 1 updated' } })
134 await waitJobs(servers)
136 await checkHlsPlaylist(servers, videoUUID, hlsOnly)
139 it('Should delete videos', async function () {
142 await servers[0].videos.remove({ id: videoUUID })
143 await servers[0].videos.remove({ id: videoAudioUUID })
145 await waitJobs(servers)
147 for (const server of servers) {
148 await server.videos.get({ id: videoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
149 await server.videos.get({ id: videoAudioUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
153 it('Should have the playlists/segment deleted from the disk', async function () {
154 for (const server of servers) {
155 await checkDirectoryIsEmpty(server, 'videos')
156 await checkDirectoryIsEmpty(server, join('streaming-playlists', 'hls'))
160 it('Should have an empty tmp directory', async function () {
161 for (const server of servers) {
162 await checkTmpIsEmpty(server)
167 before(async function () {
170 const configOverride = {
173 allow_audio_files: true,
179 servers = await createMultipleServers(2, configOverride)
181 // Get the access tokens
182 await setAccessTokensToServers(servers)
184 // Server 1 and server 2 follow each other
185 await doubleFollow(servers[0], servers[1])
188 describe('With WebTorrent & HLS enabled', function () {
192 describe('With only HLS enabled', function () {
194 before(async function () {
195 await servers[0].config.updateCustomSubConfig({
199 allowAudioFiles: true,
223 after(async function () {
224 await cleanupTests(servers)