]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/video-hls.ts
Use random names for VOD HLS playlists
[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 'mocha'
4 import * as chai from 'chai'
5 import { basename, join } from 'path'
6 import { removeFragmentedMP4Ext, uuidRegex } from '@shared/core-utils'
7 import {
8 checkDirectoryIsEmpty,
9 checkResolutionsInMasterPlaylist,
10 checkSegmentHash,
11 checkTmpIsEmpty,
12 cleanupTests,
13 createMultipleServers,
14 doubleFollow,
15 makeRawRequest,
16 PeerTubeServer,
17 setAccessTokensToServers,
18 waitJobs,
19 webtorrentAdd
20 } from '@shared/extra-utils'
21 import { HttpStatusCode, VideoStreamingPlaylistType } from '@shared/models'
22 import { DEFAULT_AUDIO_RESOLUTION } from '../../../initializers/constants'
23
24 const expect = chai.expect
25
26 async function checkHlsPlaylist (servers: PeerTubeServer[], videoUUID: string, hlsOnly: boolean, resolutions = [ 240, 360, 480, 720 ]) {
27 for (const server of servers) {
28 const videoDetails = await server.videos.get({ id: videoUUID })
29 const baseUrl = `http://${videoDetails.account.host}`
30
31 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
32
33 const hlsPlaylist = videoDetails.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
34 expect(hlsPlaylist).to.not.be.undefined
35
36 const hlsFiles = hlsPlaylist.files
37 expect(hlsFiles).to.have.lengthOf(resolutions.length)
38
39 if (hlsOnly) expect(videoDetails.files).to.have.lengthOf(0)
40 else expect(videoDetails.files).to.have.lengthOf(resolutions.length)
41
42 // Check JSON files
43 for (const resolution of resolutions) {
44 const file = hlsFiles.find(f => f.resolution.id === resolution)
45 expect(file).to.not.be.undefined
46
47 expect(file.magnetUri).to.have.lengthOf.above(2)
48 expect(file.torrentUrl).to.match(
49 new RegExp(`http://${server.host}/lazy-static/torrents/${uuidRegex}-${file.resolution.id}-hls.torrent`)
50 )
51 expect(file.fileUrl).to.match(
52 new RegExp(`${baseUrl}/static/streaming-playlists/hls/${videoDetails.uuid}/${uuidRegex}-${file.resolution.id}-fragmented.mp4`)
53 )
54 expect(file.resolution.label).to.equal(resolution + 'p')
55
56 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
57 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
58
59 const torrent = await webtorrentAdd(file.magnetUri, true)
60 expect(torrent.files).to.be.an('array')
61 expect(torrent.files.length).to.equal(1)
62 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
63 }
64
65 // Check master playlist
66 {
67 await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
68
69 const masterPlaylist = await server.streamingPlaylists.get({ url: hlsPlaylist.playlistUrl })
70
71 for (const resolution of resolutions) {
72 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
73 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
74 }
75 }
76
77 // Check resolution playlists
78 {
79 for (const resolution of resolutions) {
80 const file = hlsFiles.find(f => f.resolution.id === resolution)
81 const playlistName = removeFragmentedMP4Ext(basename(file.fileUrl)) + '.m3u8'
82
83 const subPlaylist = await server.streamingPlaylists.get({
84 url: `${baseUrl}/static/streaming-playlists/hls/${videoUUID}/${playlistName}`
85 })
86
87 expect(subPlaylist).to.match(new RegExp(`${uuidRegex}-${resolution}-fragmented.mp4`))
88 expect(subPlaylist).to.contain(basename(file.fileUrl))
89 }
90 }
91
92 {
93 const baseUrlAndPath = baseUrl + '/static/streaming-playlists/hls'
94
95 for (const resolution of resolutions) {
96 await checkSegmentHash({
97 server,
98 baseUrlPlaylist: baseUrlAndPath,
99 baseUrlSegment: baseUrlAndPath,
100 videoUUID,
101 resolution,
102 hlsPlaylist
103 })
104 }
105 }
106 }
107 }
108
109 describe('Test HLS videos', function () {
110 let servers: PeerTubeServer[] = []
111 let videoUUID = ''
112 let videoAudioUUID = ''
113
114 function runTestSuite (hlsOnly: boolean) {
115
116 it('Should upload a video and transcode it to HLS', async function () {
117 this.timeout(120000)
118
119 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video 1', fixture: 'video_short.webm' } })
120 videoUUID = uuid
121
122 await waitJobs(servers)
123
124 await checkHlsPlaylist(servers, videoUUID, hlsOnly)
125 })
126
127 it('Should upload an audio file and transcode it to HLS', async function () {
128 this.timeout(120000)
129
130 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video audio', fixture: 'sample.ogg' } })
131 videoAudioUUID = uuid
132
133 await waitJobs(servers)
134
135 await checkHlsPlaylist(servers, videoAudioUUID, hlsOnly, [ DEFAULT_AUDIO_RESOLUTION, 360, 240 ])
136 })
137
138 it('Should update the video', async function () {
139 this.timeout(10000)
140
141 await servers[0].videos.update({ id: videoUUID, attributes: { name: 'video 1 updated' } })
142
143 await waitJobs(servers)
144
145 await checkHlsPlaylist(servers, videoUUID, hlsOnly)
146 })
147
148 it('Should delete videos', async function () {
149 this.timeout(10000)
150
151 await servers[0].videos.remove({ id: videoUUID })
152 await servers[0].videos.remove({ id: videoAudioUUID })
153
154 await waitJobs(servers)
155
156 for (const server of servers) {
157 await server.videos.get({ id: videoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
158 await server.videos.get({ id: videoAudioUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
159 }
160 })
161
162 it('Should have the playlists/segment deleted from the disk', async function () {
163 for (const server of servers) {
164 await checkDirectoryIsEmpty(server, 'videos')
165 await checkDirectoryIsEmpty(server, join('streaming-playlists', 'hls'))
166 }
167 })
168
169 it('Should have an empty tmp directory', async function () {
170 for (const server of servers) {
171 await checkTmpIsEmpty(server)
172 }
173 })
174 }
175
176 before(async function () {
177 this.timeout(120000)
178
179 const configOverride = {
180 transcoding: {
181 enabled: true,
182 allow_audio_files: true,
183 hls: {
184 enabled: true
185 }
186 }
187 }
188 servers = await createMultipleServers(2, configOverride)
189
190 // Get the access tokens
191 await setAccessTokensToServers(servers)
192
193 // Server 1 and server 2 follow each other
194 await doubleFollow(servers[0], servers[1])
195 })
196
197 describe('With WebTorrent & HLS enabled', function () {
198 runTestSuite(false)
199 })
200
201 describe('With only HLS enabled', function () {
202
203 before(async function () {
204 await servers[0].config.updateCustomSubConfig({
205 newConfig: {
206 transcoding: {
207 enabled: true,
208 allowAudioFiles: true,
209 resolutions: {
210 '240p': true,
211 '360p': true,
212 '480p': true,
213 '720p': true,
214 '1080p': true,
215 '1440p': true,
216 '2160p': true
217 },
218 hls: {
219 enabled: true
220 },
221 webtorrent: {
222 enabled: false
223 }
224 }
225 }
226 })
227 })
228
229 runTestSuite(true)
230 })
231
232 after(async function () {
233 await cleanupTests(servers)
234 })
235 })