1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai from 'chai'
11 flushAndRunMultipleServers,
17 setAccessTokensToServers,
18 updateCustomSubConfig,
23 } from '../../../../shared/extra-utils'
24 import { VideoDetails } from '../../../../shared/models/videos'
25 import { VideoStreamingPlaylistType } from '../../../../shared/models/videos/video-streaming-playlist.type'
26 import { join } from 'path'
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(`${baseUrl}/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, 200)
60 await makeRawRequest(file.fileUrl, 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 const res = await getPlaylist(hlsPlaylist.playlistUrl)
71 const masterPlaylist = res.text
73 for (const resolution of resolutions) {
74 const reg = new RegExp(
75 '#EXT-X-STREAM-INF:BANDWIDTH=\\d+,RESOLUTION=\\d+x' + resolution + ',FRAME-RATE=\\d+,CODECS="avc1.64001f,mp4a.40.2"'
78 expect(masterPlaylist).to.match(reg)
79 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
80 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
85 for (const resolution of resolutions) {
86 const res = await getPlaylist(`${baseUrl}/static/streaming-playlists/hls/${videoUUID}/${resolution}.m3u8`)
88 const subPlaylist = res.text
89 expect(subPlaylist).to.contain(`${videoUUID}-${resolution}-fragmented.mp4`)
94 const baseUrlAndPath = baseUrl + '/static/streaming-playlists/hls'
96 for (const resolution of resolutions) {
97 await checkSegmentHash(baseUrlAndPath, baseUrlAndPath, videoUUID, resolution, hlsPlaylist)
103 describe('Test HLS videos', function () {
104 let servers: ServerInfo[] = []
106 let videoAudioUUID = ''
108 function runTestSuite (hlsOnly: boolean) {
109 it('Should upload a video and transcode it to HLS', async function () {
112 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video 1', fixture: 'video_short.webm' })
113 videoUUID = res.body.video.uuid
115 await waitJobs(servers)
117 await checkHlsPlaylist(servers, videoUUID, hlsOnly)
120 it('Should upload an audio file and transcode it to HLS', async function () {
123 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video audio', fixture: 'sample.ogg' })
124 videoAudioUUID = res.body.video.uuid
126 await waitJobs(servers)
128 await checkHlsPlaylist(servers, videoAudioUUID, hlsOnly, [ DEFAULT_AUDIO_RESOLUTION ])
131 it('Should update the video', async function () {
134 await updateVideo(servers[0].url, servers[0].accessToken, videoUUID, { name: 'video 1 updated' })
136 await waitJobs(servers)
138 await checkHlsPlaylist(servers, videoUUID, hlsOnly)
141 it('Should delete videos', async function () {
144 await removeVideo(servers[0].url, servers[0].accessToken, videoUUID)
145 await removeVideo(servers[0].url, servers[0].accessToken, videoAudioUUID)
147 await waitJobs(servers)
149 for (const server of servers) {
150 await getVideo(server.url, videoUUID, 404)
151 await getVideo(server.url, videoAudioUUID, 404)
155 it('Should have the playlists/segment deleted from the disk', async function () {
156 for (const server of servers) {
157 await checkDirectoryIsEmpty(server, 'videos')
158 await checkDirectoryIsEmpty(server, join('streaming-playlists', 'hls'))
162 it('Should have an empty tmp directory', async function () {
163 for (const server of servers) {
164 await checkTmpIsEmpty(server)
169 before(async function () {
172 const configOverride = {
175 allow_audio_files: true,
181 servers = await flushAndRunMultipleServers(2, configOverride)
183 // Get the access tokens
184 await setAccessTokensToServers(servers)
186 // Server 1 and server 2 follow each other
187 await doubleFollow(servers[0], servers[1])
190 describe('With WebTorrent & HLS enabled', function () {
194 describe('With only HLS enabled', function () {
196 before(async function () {
197 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
200 allowAudioFiles: true,
222 after(async function () {
223 await cleanupTests(servers)