]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/transcoding/hls.ts
Increase test timeout
[github/Chocobozzz/PeerTube.git] / server / tests / api / transcoding / hls.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
09209296 2
09209296 3import 'mocha'
bd54ad19 4import * as chai from 'chai'
764b1a14 5import { basename, join } from 'path'
09209296
C
6import {
7 checkDirectoryIsEmpty,
bd54ad19 8 checkResolutionsInMasterPlaylist,
4c280004 9 checkSegmentHash,
7243f84d 10 checkTmpIsEmpty,
c55e3d72
C
11 expectStartWith,
12 hlsInfohashExist
13} from '@server/tests/shared'
14import { areObjectStorageTestsDisabled, removeFragmentedMP4Ext, uuidRegex } from '@shared/core-utils'
15import { HttpStatusCode, VideoStreamingPlaylistType } from '@shared/models'
16import {
7243f84d 17 cleanupTests,
254d3579 18 createMultipleServers,
4c7e60bc 19 doubleFollow,
a1587156 20 makeRawRequest,
0305db28 21 ObjectStorageCommand,
254d3579 22 PeerTubeServer,
a1587156 23 setAccessTokensToServers,
a1587156
C
24 waitJobs,
25 webtorrentAdd
bf54587a 26} from '@shared/server-commands'
b345a804 27import { DEFAULT_AUDIO_RESOLUTION } from '../../../initializers/constants'
09209296
C
28
29const expect = chai.expect
30
0305db28
JB
31async function checkHlsPlaylist (options: {
32 servers: PeerTubeServer[]
33 videoUUID: string
34 hlsOnly: boolean
35
36 resolutions?: number[]
37 objectStorageBaseUrl: string
38}) {
39 const { videoUUID, hlsOnly, objectStorageBaseUrl } = options
40
41 const resolutions = options.resolutions ?? [ 240, 360, 480, 720 ]
42
43 for (const server of options.servers) {
89d241a7 44 const videoDetails = await server.videos.get({ id: videoUUID })
d7a25329 45 const baseUrl = `http://${videoDetails.account.host}`
09209296
C
46
47 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
48
49 const hlsPlaylist = videoDetails.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
50 expect(hlsPlaylist).to.not.be.undefined
51
d7a25329
C
52 const hlsFiles = hlsPlaylist.files
53 expect(hlsFiles).to.have.lengthOf(resolutions.length)
54
55 if (hlsOnly) expect(videoDetails.files).to.have.lengthOf(0)
56 else expect(videoDetails.files).to.have.lengthOf(resolutions.length)
57
83903cb6 58 // Check JSON files
d7a25329
C
59 for (const resolution of resolutions) {
60 const file = hlsFiles.find(f => f.resolution.id === resolution)
61 expect(file).to.not.be.undefined
62
63 expect(file.magnetUri).to.have.lengthOf.above(2)
83903cb6
C
64 expect(file.torrentUrl).to.match(
65 new RegExp(`http://${server.host}/lazy-static/torrents/${uuidRegex}-${file.resolution.id}-hls.torrent`)
66 )
0305db28
JB
67
68 if (objectStorageBaseUrl) {
69 expectStartWith(file.fileUrl, objectStorageBaseUrl)
70 } else {
71 expect(file.fileUrl).to.match(
72 new RegExp(`${baseUrl}/static/streaming-playlists/hls/${videoDetails.uuid}/${uuidRegex}-${file.resolution.id}-fragmented.mp4`)
73 )
74 }
75
d7a25329
C
76 expect(file.resolution.label).to.equal(resolution + 'p')
77
f2eb23cd
RK
78 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
79 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
d7a25329
C
80
81 const torrent = await webtorrentAdd(file.magnetUri, true)
82 expect(torrent.files).to.be.an('array')
83 expect(torrent.files.length).to.equal(1)
84 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
85 }
86
83903cb6 87 // Check master playlist
09209296 88 {
57f879a5 89 await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
09209296 90
89d241a7 91 const masterPlaylist = await server.streamingPlaylists.get({ url: hlsPlaylist.playlistUrl })
09209296 92
fb72d2e1 93 let i = 0
09209296 94 for (const resolution of resolutions) {
52201311 95 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
09209296 96 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
fb72d2e1
C
97
98 const url = 'http://' + videoDetails.account.host
99 await hlsInfohashExist(url, hlsPlaylist.playlistUrl, i)
100
101 i++
09209296
C
102 }
103 }
104
83903cb6 105 // Check resolution playlists
09209296
C
106 {
107 for (const resolution of resolutions) {
764b1a14
C
108 const file = hlsFiles.find(f => f.resolution.id === resolution)
109 const playlistName = removeFragmentedMP4Ext(basename(file.fileUrl)) + '.m3u8'
110
0305db28 111 const url = objectStorageBaseUrl
cbb1d46b 112 ? `${objectStorageBaseUrl}hls/${videoUUID}/${playlistName}`
0305db28
JB
113 : `${baseUrl}/static/streaming-playlists/hls/${videoUUID}/${playlistName}`
114
115 const subPlaylist = await server.streamingPlaylists.get({ url })
09209296 116
83903cb6
C
117 expect(subPlaylist).to.match(new RegExp(`${uuidRegex}-${resolution}-fragmented.mp4`))
118 expect(subPlaylist).to.contain(basename(file.fileUrl))
09209296
C
119 }
120 }
121
122 {
0305db28 123 const baseUrlAndPath = objectStorageBaseUrl
cbb1d46b 124 ? objectStorageBaseUrl + 'hls/' + videoUUID
0305db28 125 : baseUrl + '/static/streaming-playlists/hls/' + videoUUID
09209296 126
4c280004 127 for (const resolution of resolutions) {
57f879a5
C
128 await checkSegmentHash({
129 server,
130 baseUrlPlaylist: baseUrlAndPath,
131 baseUrlSegment: baseUrlAndPath,
57f879a5
C
132 resolution,
133 hlsPlaylist
134 })
09209296
C
135 }
136 }
137 }
138}
139
140describe('Test HLS videos', function () {
254d3579 141 let servers: PeerTubeServer[] = []
09209296 142 let videoUUID = ''
b345a804 143 let videoAudioUUID = ''
09209296 144
0305db28 145 function runTestSuite (hlsOnly: boolean, objectStorageBaseUrl?: string) {
dd0ebb71 146
d7a25329
C
147 it('Should upload a video and transcode it to HLS', async function () {
148 this.timeout(120000)
09209296 149
89d241a7 150 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video 1', fixture: 'video_short.webm' } })
d23dd9fb 151 videoUUID = uuid
09209296 152
d7a25329 153 await waitJobs(servers)
09209296 154
0305db28 155 await checkHlsPlaylist({ servers, videoUUID, hlsOnly, objectStorageBaseUrl })
d7a25329 156 })
09209296 157
d7a25329
C
158 it('Should upload an audio file and transcode it to HLS', async function () {
159 this.timeout(120000)
09209296 160
89d241a7 161 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video audio', fixture: 'sample.ogg' } })
d23dd9fb 162 videoAudioUUID = uuid
09209296 163
d7a25329 164 await waitJobs(servers)
09209296 165
0305db28
JB
166 await checkHlsPlaylist({
167 servers,
168 videoUUID: videoAudioUUID,
169 hlsOnly,
170 resolutions: [ DEFAULT_AUDIO_RESOLUTION, 360, 240 ],
171 objectStorageBaseUrl
172 })
d7a25329 173 })
09209296 174
d7a25329 175 it('Should update the video', async function () {
cbb1d46b 176 this.timeout(30000)
b345a804 177
89d241a7 178 await servers[0].videos.update({ id: videoUUID, attributes: { name: 'video 1 updated' } })
b345a804 179
d7a25329 180 await waitJobs(servers)
b345a804 181
0305db28 182 await checkHlsPlaylist({ servers, videoUUID, hlsOnly, objectStorageBaseUrl })
d7a25329 183 })
b345a804 184
d7a25329
C
185 it('Should delete videos', async function () {
186 this.timeout(10000)
80b8ad2a 187
89d241a7
C
188 await servers[0].videos.remove({ id: videoUUID })
189 await servers[0].videos.remove({ id: videoAudioUUID })
09209296 190
d7a25329 191 await waitJobs(servers)
09209296 192
d7a25329 193 for (const server of servers) {
89d241a7
C
194 await server.videos.get({ id: videoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
195 await server.videos.get({ id: videoAudioUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
d7a25329
C
196 }
197 })
09209296 198
d7a25329
C
199 it('Should have the playlists/segment deleted from the disk', async function () {
200 for (const server of servers) {
201 await checkDirectoryIsEmpty(server, 'videos')
202 await checkDirectoryIsEmpty(server, join('streaming-playlists', 'hls'))
203 }
204 })
80b8ad2a 205
d7a25329
C
206 it('Should have an empty tmp directory', async function () {
207 for (const server of servers) {
208 await checkTmpIsEmpty(server)
209 }
210 })
211 }
09209296 212
d7a25329
C
213 before(async function () {
214 this.timeout(120000)
09209296 215
d7a25329
C
216 const configOverride = {
217 transcoding: {
218 enabled: true,
219 allow_audio_files: true,
220 hls: {
221 enabled: true
222 }
223 }
09209296 224 }
254d3579 225 servers = await createMultipleServers(2, configOverride)
d7a25329
C
226
227 // Get the access tokens
228 await setAccessTokensToServers(servers)
229
230 // Server 1 and server 2 follow each other
231 await doubleFollow(servers[0], servers[1])
09209296
C
232 })
233
d7a25329
C
234 describe('With WebTorrent & HLS enabled', function () {
235 runTestSuite(false)
09209296
C
236 })
237
d7a25329
C
238 describe('With only HLS enabled', function () {
239
240 before(async function () {
89d241a7 241 await servers[0].config.updateCustomSubConfig({
65e6e260
C
242 newConfig: {
243 transcoding: {
244 enabled: true,
245 allowAudioFiles: true,
246 resolutions: {
8dd754c7 247 '144p': false,
65e6e260
C
248 '240p': true,
249 '360p': true,
250 '480p': true,
251 '720p': true,
252 '1080p': true,
253 '1440p': true,
254 '2160p': true
255 },
256 hls: {
257 enabled: true
258 },
259 webtorrent: {
260 enabled: false
261 }
d7a25329
C
262 }
263 }
264 })
265 })
266
267 runTestSuite(true)
09209296
C
268 })
269
0305db28
JB
270 describe('With object storage enabled', function () {
271 if (areObjectStorageTestsDisabled()) return
272
273 before(async function () {
274 this.timeout(120000)
275
276 const configOverride = ObjectStorageCommand.getDefaultConfig()
277 await ObjectStorageCommand.prepareDefaultBuckets()
278
279 await servers[0].kill()
280 await servers[0].run(configOverride)
281 })
282
283 runTestSuite(true, ObjectStorageCommand.getPlaylistBaseUrl())
284 })
285
7c3b7976
C
286 after(async function () {
287 await cleanupTests(servers)
09209296
C
288 })
289})