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/miscs/http-error-codes'
9 checkResolutionsInMasterPlaylist,
14 flushAndRunMultipleServers,
19 setAccessTokensToServers,
24 } from '../../../../shared/extra-utils'
25 import { VideoDetails } from '../../../../shared/models/videos'
26 import { VideoStreamingPlaylistType } from '../../../../shared/models/videos/video-streaming-playlist.type'
27 import { DEFAULT_AUDIO_RESOLUTION } from '../../../initializers/constants'
29 const expect = chai.expect
31 async function checkHlsPlaylist (servers: ServerInfo[], videoUUID: string, hlsOnly: boolean, resolutions = [ 240, 360, 480, 720 ]) {
32 for (const server of servers) {
33 const resVideoDetails = await getVideo(server.url, videoUUID)
34 const videoDetails: VideoDetails = resVideoDetails.body
35 const baseUrl = `http://${videoDetails.account.host}`
37 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
39 const hlsPlaylist = videoDetails.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
40 expect(hlsPlaylist).to.not.be.undefined
42 const hlsFiles = hlsPlaylist.files
43 expect(hlsFiles).to.have.lengthOf(resolutions.length)
45 if (hlsOnly) expect(videoDetails.files).to.have.lengthOf(0)
46 else expect(videoDetails.files).to.have.lengthOf(resolutions.length)
48 for (const resolution of resolutions) {
49 const file = hlsFiles.find(f => f.resolution.id === resolution)
50 expect(file).to.not.be.undefined
52 expect(file.magnetUri).to.have.lengthOf.above(2)
53 expect(file.torrentUrl).to.equal(`http://${server.host}/lazy-static/torrents/${videoDetails.uuid}-${file.resolution.id}-hls.torrent`)
54 expect(file.fileUrl).to.equal(
55 `${baseUrl}/static/streaming-playlists/hls/${videoDetails.uuid}/${videoDetails.uuid}-${file.resolution.id}-fragmented.mp4`
57 expect(file.resolution.label).to.equal(resolution + 'p')
59 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
60 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
62 const torrent = await webtorrentAdd(file.magnetUri, true)
63 expect(torrent.files).to.be.an('array')
64 expect(torrent.files.length).to.equal(1)
65 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
69 await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
71 const masterPlaylist = await server.streamingPlaylistsCommand.get({ url: hlsPlaylist.playlistUrl })
73 for (const resolution of resolutions) {
74 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
75 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
80 for (const resolution of resolutions) {
81 const subPlaylist = await server.streamingPlaylistsCommand.get({
82 url: `${baseUrl}/static/streaming-playlists/hls/${videoUUID}/${resolution}.m3u8`
85 expect(subPlaylist).to.contain(`${videoUUID}-${resolution}-fragmented.mp4`)
90 const baseUrlAndPath = baseUrl + '/static/streaming-playlists/hls'
92 for (const resolution of resolutions) {
93 await checkSegmentHash({
95 baseUrlPlaylist: baseUrlAndPath,
96 baseUrlSegment: baseUrlAndPath,
106 describe('Test HLS videos', function () {
107 let servers: ServerInfo[] = []
109 let videoAudioUUID = ''
111 function runTestSuite (hlsOnly: boolean) {
113 it('Should upload a video and transcode it to HLS', async function () {
116 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video 1', fixture: 'video_short.webm' })
117 videoUUID = res.body.video.uuid
119 await waitJobs(servers)
121 await checkHlsPlaylist(servers, videoUUID, hlsOnly)
124 it('Should upload an audio file and transcode it to HLS', async function () {
127 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video audio', fixture: 'sample.ogg' })
128 videoAudioUUID = res.body.video.uuid
130 await waitJobs(servers)
132 await checkHlsPlaylist(servers, videoAudioUUID, hlsOnly, [ DEFAULT_AUDIO_RESOLUTION, 360, 240 ])
135 it('Should update the video', async function () {
138 await updateVideo(servers[0].url, servers[0].accessToken, videoUUID, { name: 'video 1 updated' })
140 await waitJobs(servers)
142 await checkHlsPlaylist(servers, videoUUID, hlsOnly)
145 it('Should delete videos', async function () {
148 await removeVideo(servers[0].url, servers[0].accessToken, videoUUID)
149 await removeVideo(servers[0].url, servers[0].accessToken, videoAudioUUID)
151 await waitJobs(servers)
153 for (const server of servers) {
154 await getVideo(server.url, videoUUID, HttpStatusCode.NOT_FOUND_404)
155 await getVideo(server.url, videoAudioUUID, HttpStatusCode.NOT_FOUND_404)
159 it('Should have the playlists/segment deleted from the disk', async function () {
160 for (const server of servers) {
161 await checkDirectoryIsEmpty(server, 'videos')
162 await checkDirectoryIsEmpty(server, join('streaming-playlists', 'hls'))
166 it('Should have an empty tmp directory', async function () {
167 for (const server of servers) {
168 await checkTmpIsEmpty(server)
173 before(async function () {
176 const configOverride = {
179 allow_audio_files: true,
185 servers = await flushAndRunMultipleServers(2, configOverride)
187 // Get the access tokens
188 await setAccessTokensToServers(servers)
190 // Server 1 and server 2 follow each other
191 await doubleFollow(servers[0], servers[1])
194 describe('With WebTorrent & HLS enabled', function () {
198 describe('With only HLS enabled', function () {
200 before(async function () {
201 await servers[0].configCommand.updateCustomSubConfig({
205 allowAudioFiles: true,
229 after(async function () {
230 await cleanupTests(servers)