]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/video-hls.ts
Merge branch 'release/2.1.0' into develop
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / video-hls.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import {
6 checkDirectoryIsEmpty,
7 checkSegmentHash,
8 checkTmpIsEmpty,
9 cleanupTests,
10 doubleFollow,
11 flushAndRunMultipleServers,
12 getPlaylist,
13 getVideo,
14 makeRawRequest,
15 removeVideo,
16 ServerInfo,
17 setAccessTokensToServers,
18 updateCustomSubConfig,
19 updateVideo,
20 uploadVideo,
21 waitJobs,
22 webtorrentAdd
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'
28
29 const expect = chai.expect
30
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}`
36
37 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
38
39 const hlsPlaylist = videoDetails.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
40 expect(hlsPlaylist).to.not.be.undefined
41
42 const hlsFiles = hlsPlaylist.files
43 expect(hlsFiles).to.have.lengthOf(resolutions.length)
44
45 if (hlsOnly) expect(videoDetails.files).to.have.lengthOf(0)
46 else expect(videoDetails.files).to.have.lengthOf(resolutions.length)
47
48 for (const resolution of resolutions) {
49 const file = hlsFiles.find(f => f.resolution.id === resolution)
50 expect(file).to.not.be.undefined
51
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`
56 )
57 expect(file.resolution.label).to.equal(resolution + 'p')
58
59 await makeRawRequest(file.torrentUrl, 200)
60 await makeRawRequest(file.fileUrl, 200)
61
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('')
66 }
67
68 {
69 const res = await getPlaylist(hlsPlaylist.playlistUrl)
70
71 const masterPlaylist = res.text
72
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"'
76 )
77
78 expect(masterPlaylist).to.match(reg)
79 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
80 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
81 }
82 }
83
84 {
85 for (const resolution of resolutions) {
86 const res = await getPlaylist(`${baseUrl}/static/streaming-playlists/hls/${videoUUID}/${resolution}.m3u8`)
87
88 const subPlaylist = res.text
89 expect(subPlaylist).to.contain(`${videoUUID}-${resolution}-fragmented.mp4`)
90 }
91 }
92
93 {
94 const baseUrlAndPath = baseUrl + '/static/streaming-playlists/hls'
95
96 for (const resolution of resolutions) {
97 await checkSegmentHash(baseUrlAndPath, baseUrlAndPath, videoUUID, resolution, hlsPlaylist)
98 }
99 }
100 }
101 }
102
103 describe('Test HLS videos', function () {
104 let servers: ServerInfo[] = []
105 let videoUUID = ''
106 let videoAudioUUID = ''
107
108 function runTestSuite (hlsOnly: boolean) {
109 it('Should upload a video and transcode it to HLS', async function () {
110 this.timeout(120000)
111
112 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video 1', fixture: 'video_short.webm' })
113 videoUUID = res.body.video.uuid
114
115 await waitJobs(servers)
116
117 await checkHlsPlaylist(servers, videoUUID, hlsOnly)
118 })
119
120 it('Should upload an audio file and transcode it to HLS', async function () {
121 this.timeout(120000)
122
123 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video audio', fixture: 'sample.ogg' })
124 videoAudioUUID = res.body.video.uuid
125
126 await waitJobs(servers)
127
128 await checkHlsPlaylist(servers, videoAudioUUID, hlsOnly, [ DEFAULT_AUDIO_RESOLUTION ])
129 })
130
131 it('Should update the video', async function () {
132 this.timeout(10000)
133
134 await updateVideo(servers[0].url, servers[0].accessToken, videoUUID, { name: 'video 1 updated' })
135
136 await waitJobs(servers)
137
138 await checkHlsPlaylist(servers, videoUUID, hlsOnly)
139 })
140
141 it('Should delete videos', async function () {
142 this.timeout(10000)
143
144 await removeVideo(servers[0].url, servers[0].accessToken, videoUUID)
145 await removeVideo(servers[0].url, servers[0].accessToken, videoAudioUUID)
146
147 await waitJobs(servers)
148
149 for (const server of servers) {
150 await getVideo(server.url, videoUUID, 404)
151 await getVideo(server.url, videoAudioUUID, 404)
152 }
153 })
154
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'))
159 }
160 })
161
162 it('Should have an empty tmp directory', async function () {
163 for (const server of servers) {
164 await checkTmpIsEmpty(server)
165 }
166 })
167 }
168
169 before(async function () {
170 this.timeout(120000)
171
172 const configOverride = {
173 transcoding: {
174 enabled: true,
175 allow_audio_files: true,
176 hls: {
177 enabled: true
178 }
179 }
180 }
181 servers = await flushAndRunMultipleServers(2, configOverride)
182
183 // Get the access tokens
184 await setAccessTokensToServers(servers)
185
186 // Server 1 and server 2 follow each other
187 await doubleFollow(servers[0], servers[1])
188 })
189
190 describe('With WebTorrent & HLS enabled', function () {
191 runTestSuite(false)
192 })
193
194 describe('With only HLS enabled', function () {
195
196 before(async function () {
197 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
198 transcoding: {
199 enabled: true,
200 allowAudioFiles: true,
201 resolutions: {
202 '240p': true,
203 '360p': true,
204 '480p': true,
205 '720p': true,
206 '1080p': true,
207 '2160p': true
208 },
209 hls: {
210 enabled: true
211 },
212 webtorrent: {
213 enabled: false
214 }
215 }
216 })
217 })
218
219 runTestSuite(true)
220 })
221
222 after(async function () {
223 await cleanupTests(servers)
224 })
225 })