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'
23 const expect = chai.expect
25 async function checkHlsPlaylist (servers: PeerTubeServer[], videoUUID: string, hlsOnly: boolean, resolutions = [ 240, 360, 480, 720 ]) {
26 for (const server of servers) {
27 const videoDetails = await server.videos.get({ id: videoUUID })
28 const baseUrl = `http://${videoDetails.account.host}`
30 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
32 const hlsPlaylist = videoDetails.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
33 expect(hlsPlaylist).to.not.be.undefined
35 const hlsFiles = hlsPlaylist.files
36 expect(hlsFiles).to.have.lengthOf(resolutions.length)
38 if (hlsOnly) expect(videoDetails.files).to.have.lengthOf(0)
39 else expect(videoDetails.files).to.have.lengthOf(resolutions.length)
41 for (const resolution of resolutions) {
42 const file = hlsFiles.find(f => f.resolution.id === resolution)
43 expect(file).to.not.be.undefined
45 expect(file.magnetUri).to.have.lengthOf.above(2)
46 expect(file.torrentUrl).to.equal(`http://${server.host}/lazy-static/torrents/${videoDetails.uuid}-${file.resolution.id}-hls.torrent`)
47 expect(file.fileUrl).to.equal(
48 `${baseUrl}/static/streaming-playlists/hls/${videoDetails.uuid}/${videoDetails.uuid}-${file.resolution.id}-fragmented.mp4`
50 expect(file.resolution.label).to.equal(resolution + 'p')
52 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
53 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
55 const torrent = await webtorrentAdd(file.magnetUri, true)
56 expect(torrent.files).to.be.an('array')
57 expect(torrent.files.length).to.equal(1)
58 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
62 await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
64 const masterPlaylist = await server.streamingPlaylists.get({ url: hlsPlaylist.playlistUrl })
66 for (const resolution of resolutions) {
67 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
68 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
73 for (const resolution of resolutions) {
74 const subPlaylist = await server.streamingPlaylists.get({
75 url: `${baseUrl}/static/streaming-playlists/hls/${videoUUID}/${resolution}.m3u8`
78 expect(subPlaylist).to.contain(`${videoUUID}-${resolution}-fragmented.mp4`)
83 const baseUrlAndPath = baseUrl + '/static/streaming-playlists/hls'
85 for (const resolution of resolutions) {
86 await checkSegmentHash({
88 baseUrlPlaylist: baseUrlAndPath,
89 baseUrlSegment: baseUrlAndPath,
99 describe('Test HLS videos', function () {
100 let servers: PeerTubeServer[] = []
102 let videoAudioUUID = ''
104 function runTestSuite (hlsOnly: boolean) {
106 it('Should upload a video and transcode it to HLS', async function () {
109 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video 1', fixture: 'video_short.webm' } })
112 await waitJobs(servers)
114 await checkHlsPlaylist(servers, videoUUID, hlsOnly)
117 it('Should upload an audio file and transcode it to HLS', async function () {
120 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video audio', fixture: 'sample.ogg' } })
121 videoAudioUUID = uuid
123 await waitJobs(servers)
125 await checkHlsPlaylist(servers, videoAudioUUID, hlsOnly, [ DEFAULT_AUDIO_RESOLUTION, 360, 240 ])
128 it('Should update the video', async function () {
131 await servers[0].videos.update({ id: videoUUID, attributes: { name: 'video 1 updated' } })
133 await waitJobs(servers)
135 await checkHlsPlaylist(servers, videoUUID, hlsOnly)
138 it('Should delete videos', async function () {
141 await servers[0].videos.remove({ id: videoUUID })
142 await servers[0].videos.remove({ id: videoAudioUUID })
144 await waitJobs(servers)
146 for (const server of servers) {
147 await server.videos.get({ id: videoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
148 await server.videos.get({ id: videoAudioUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
152 it('Should have the playlists/segment deleted from the disk', async function () {
153 for (const server of servers) {
154 await checkDirectoryIsEmpty(server, 'videos')
155 await checkDirectoryIsEmpty(server, join('streaming-playlists', 'hls'))
159 it('Should have an empty tmp directory', async function () {
160 for (const server of servers) {
161 await checkTmpIsEmpty(server)
166 before(async function () {
169 const configOverride = {
172 allow_audio_files: true,
178 servers = await createMultipleServers(2, configOverride)
180 // Get the access tokens
181 await setAccessTokensToServers(servers)
183 // Server 1 and server 2 follow each other
184 await doubleFollow(servers[0], servers[1])
187 describe('With WebTorrent & HLS enabled', function () {
191 describe('With only HLS enabled', function () {
193 before(async function () {
194 await servers[0].config.updateCustomSubConfig({
198 allowAudioFiles: true,
222 after(async function () {
223 await cleanupTests(servers)