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 expect(masterPlaylist).to.match(new RegExp('#EXT-X-STREAM-INF:BANDWIDTH=\\d+,RESOLUTION=\\d+x' + resolution + ',FRAME-RATE=\\d+'))
70 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
75 for (const resolution of resolutions) {
76 const res = await getPlaylist(`${baseUrl}/static/streaming-playlists/hls/${videoUUID}/${resolution}.m3u8`)
78 const subPlaylist = res.text
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(baseUrlAndPath, baseUrlAndPath, videoUUID, resolution, hlsPlaylist)
93 describe('Test HLS videos', function () {
94 let servers: ServerInfo[] = []
96 let videoAudioUUID = ''
98 function runTestSuite (hlsOnly: boolean) {
99 it('Should upload a video and transcode it to HLS', async function () {
102 const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, { name: 'video 1', fixture: 'video_short.webm' })
103 videoUUID = res.body.video.uuid
105 await waitJobs(servers)
107 await checkHlsPlaylist(servers, videoUUID, hlsOnly)
110 it('Should upload an audio file and transcode it to HLS', async function () {
113 const res = await uploadVideo(servers[ 0 ].url, servers[ 0 ].accessToken, { name: 'video audio', fixture: 'sample.ogg' })
114 videoAudioUUID = res.body.video.uuid
116 await waitJobs(servers)
118 await checkHlsPlaylist(servers, videoAudioUUID, hlsOnly, [ DEFAULT_AUDIO_RESOLUTION ])
121 it('Should update the video', async function () {
124 await updateVideo(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID, { name: 'video 1 updated' })
126 await waitJobs(servers)
128 await checkHlsPlaylist(servers, videoUUID, hlsOnly)
131 it('Should delete videos', async function () {
134 await removeVideo(servers[ 0 ].url, servers[ 0 ].accessToken, videoUUID)
135 await removeVideo(servers[ 0 ].url, servers[ 0 ].accessToken, videoAudioUUID)
137 await waitJobs(servers)
139 for (const server of servers) {
140 await getVideo(server.url, videoUUID, 404)
141 await getVideo(server.url, videoAudioUUID, 404)
145 it('Should have the playlists/segment deleted from the disk', async function () {
146 for (const server of servers) {
147 await checkDirectoryIsEmpty(server, 'videos')
148 await checkDirectoryIsEmpty(server, join('streaming-playlists', 'hls'))
152 it('Should have an empty tmp directory', async function () {
153 for (const server of servers) {
154 await checkTmpIsEmpty(server)
159 before(async function () {
162 const configOverride = {
165 allow_audio_files: true,
171 servers = await flushAndRunMultipleServers(2, configOverride)
173 // Get the access tokens
174 await setAccessTokensToServers(servers)
176 // Server 1 and server 2 follow each other
177 await doubleFollow(servers[0], servers[1])
180 describe('With WebTorrent & HLS enabled', function () {
184 describe('With only HLS enabled', function () {
186 before(async function () {
187 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
190 allowAudioFiles: true,
212 after(async function () {
213 await cleanupTests(servers)