1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
11 flushAndRunMultipleServers,
13 getVideo, makeGetRequest, makeRawRequest,
16 setAccessTokensToServers, updateCustomSubConfig,
19 waitJobs, webtorrentAdd
20 } from '../../../../shared/extra-utils'
21 import { VideoDetails } from '../../../../shared/models/videos'
22 import { VideoStreamingPlaylistType } from '../../../../shared/models/videos/video-streaming-playlist.type'
23 import { join } from 'path'
24 import { DEFAULT_AUDIO_RESOLUTION } from '../../../initializers/constants'
26 const expect = chai.expect
28 async function checkHlsPlaylist (servers: ServerInfo[], videoUUID: string, hlsOnly: boolean, resolutions = [ 240, 360, 480, 720 ]) {
29 for (const server of servers) {
30 const resVideoDetails = await getVideo(server.url, videoUUID)
31 const videoDetails: VideoDetails = resVideoDetails.body
32 const baseUrl = `http://${videoDetails.account.host}`
34 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
36 const hlsPlaylist = videoDetails.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
37 expect(hlsPlaylist).to.not.be.undefined
39 const hlsFiles = hlsPlaylist.files
40 expect(hlsFiles).to.have.lengthOf(resolutions.length)
42 if (hlsOnly) expect(videoDetails.files).to.have.lengthOf(0)
43 else expect(videoDetails.files).to.have.lengthOf(resolutions.length)
45 for (const resolution of resolutions) {
46 const file = hlsFiles.find(f => f.resolution.id === resolution)
47 expect(file).to.not.be.undefined
49 expect(file.magnetUri).to.have.lengthOf.above(2)
50 expect(file.torrentUrl).to.equal(`${baseUrl}/static/torrents/${videoDetails.uuid}-${file.resolution.id}-hls.torrent`)
51 expect(file.fileUrl).to.equal(`${baseUrl}/static/streaming-playlists/hls/${videoDetails.uuid}/${videoDetails.uuid}-${file.resolution.id}-fragmented.mp4`)
52 expect(file.resolution.label).to.equal(resolution + 'p')
54 await makeRawRequest(file.torrentUrl, 200)
55 await makeRawRequest(file.fileUrl, 200)
57 const torrent = await webtorrentAdd(file.magnetUri, true)
58 expect(torrent.files).to.be.an('array')
59 expect(torrent.files.length).to.equal(1)
60 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
64 const res = await getPlaylist(hlsPlaylist.playlistUrl)
66 const masterPlaylist = res.text
68 for (const resolution of resolutions) {
69 const reg = new RegExp('#EXT-X-STREAM-INF:BANDWIDTH=\\d+,RESOLUTION=\\d+x' + resolution + ',FRAME-RATE=\\d+,CODECS="avc1.64001f,mp4a.40.2"')
71 expect(masterPlaylist).to.match(reg)
72 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
73 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
78 for (const resolution of resolutions) {
79 const res = await getPlaylist(`${baseUrl}/static/streaming-playlists/hls/${videoUUID}/${resolution}.m3u8`)
81 const subPlaylist = res.text
82 expect(subPlaylist).to.contain(`${videoUUID}-${resolution}-fragmented.mp4`)
87 const baseUrlAndPath = baseUrl + '/static/streaming-playlists/hls'
89 for (const resolution of resolutions) {
90 await checkSegmentHash(baseUrlAndPath, baseUrlAndPath, videoUUID, resolution, hlsPlaylist)
96 describe('Test HLS videos', function () {
97 let servers: ServerInfo[] = []
99 let videoAudioUUID = ''
101 function runTestSuite (hlsOnly: boolean) {
102 it('Should upload a video and transcode it to HLS', async function () {
105 const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, { name: 'video 1', fixture: 'video_short.webm' })
106 videoUUID = res.body.video.uuid
108 await waitJobs(servers)
110 await checkHlsPlaylist(servers, videoUUID, hlsOnly)
113 it('Should upload an audio file and transcode it to HLS', async function () {
116 const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, { name: 'video audio', fixture: 'sample.ogg' })
117 videoAudioUUID = res.body.video.uuid
119 await waitJobs(servers)
121 await checkHlsPlaylist(servers, videoAudioUUID, hlsOnly, [ DEFAULT_AUDIO_RESOLUTION ])
124 it('Should update the video', async function () {
127 await updateVideo(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID, { name: 'video 1 updated' })
129 await waitJobs(servers)
131 await checkHlsPlaylist(servers, videoUUID, hlsOnly)
134 it('Should delete videos', async function () {
137 await removeVideo(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID)
138 await removeVideo(servers[ 0 ].url, servers[ 0 ].accessToken, videoAudioUUID)
140 await waitJobs(servers)
142 for (const server of servers) {
143 await getVideo(server.url, videoUUID, 404)
144 await getVideo(server.url, videoAudioUUID, 404)
148 it('Should have the playlists/segment deleted from the disk', async function () {
149 for (const server of servers) {
150 await checkDirectoryIsEmpty(server, 'videos')
151 await checkDirectoryIsEmpty(server, join('streaming-playlists', 'hls'))
155 it('Should have an empty tmp directory', async function () {
156 for (const server of servers) {
157 await checkTmpIsEmpty(server)
162 before(async function () {
165 const configOverride = {
168 allow_audio_files: true,
174 servers = await flushAndRunMultipleServers(2, configOverride)
176 // Get the access tokens
177 await setAccessTokensToServers(servers)
179 // Server 1 and server 2 follow each other
180 await doubleFollow(servers[0], servers[1])
183 describe('With WebTorrent & HLS enabled', function () {
187 describe('With only HLS enabled', function () {
189 before(async function () {
190 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
193 allowAudioFiles: true,
215 after(async function () {
216 await cleanupTests(servers)