aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/transcoding/hls.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/tests/api/transcoding/hls.ts')
-rw-r--r--server/tests/api/transcoding/hls.ts289
1 files changed, 289 insertions, 0 deletions
diff --git a/server/tests/api/transcoding/hls.ts b/server/tests/api/transcoding/hls.ts
new file mode 100644
index 000000000..218ec08ae
--- /dev/null
+++ b/server/tests/api/transcoding/hls.ts
@@ -0,0 +1,289 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3import 'mocha'
4import * as chai from 'chai'
5import { basename, join } from 'path'
6import {
7 checkDirectoryIsEmpty,
8 checkResolutionsInMasterPlaylist,
9 checkSegmentHash,
10 checkTmpIsEmpty,
11 expectStartWith,
12 hlsInfohashExist
13} from '@server/tests/shared'
14import { areObjectStorageTestsDisabled, removeFragmentedMP4Ext, uuidRegex } from '@shared/core-utils'
15import { HttpStatusCode, VideoStreamingPlaylistType } from '@shared/models'
16import {
17 cleanupTests,
18 createMultipleServers,
19 doubleFollow,
20 makeRawRequest,
21 ObjectStorageCommand,
22 PeerTubeServer,
23 setAccessTokensToServers,
24 waitJobs,
25 webtorrentAdd
26} from '@shared/server-commands'
27import { DEFAULT_AUDIO_RESOLUTION } from '../../../initializers/constants'
28
29const expect = chai.expect
30
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) {
44 const videoDetails = await server.videos.get({ id: videoUUID })
45 const baseUrl = `http://${videoDetails.account.host}`
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
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
58 // Check JSON files
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)
64 expect(file.torrentUrl).to.match(
65 new RegExp(`http://${server.host}/lazy-static/torrents/${uuidRegex}-${file.resolution.id}-hls.torrent`)
66 )
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
76 expect(file.resolution.label).to.equal(resolution + 'p')
77
78 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
79 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
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
87 // Check master playlist
88 {
89 await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
90
91 const masterPlaylist = await server.streamingPlaylists.get({ url: hlsPlaylist.playlistUrl })
92
93 let i = 0
94 for (const resolution of resolutions) {
95 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
96 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
97
98 const url = 'http://' + videoDetails.account.host
99 await hlsInfohashExist(url, hlsPlaylist.playlistUrl, i)
100
101 i++
102 }
103 }
104
105 // Check resolution playlists
106 {
107 for (const resolution of resolutions) {
108 const file = hlsFiles.find(f => f.resolution.id === resolution)
109 const playlistName = removeFragmentedMP4Ext(basename(file.fileUrl)) + '.m3u8'
110
111 const url = objectStorageBaseUrl
112 ? `${objectStorageBaseUrl}hls/${videoUUID}/${playlistName}`
113 : `${baseUrl}/static/streaming-playlists/hls/${videoUUID}/${playlistName}`
114
115 const subPlaylist = await server.streamingPlaylists.get({ url })
116
117 expect(subPlaylist).to.match(new RegExp(`${uuidRegex}-${resolution}-fragmented.mp4`))
118 expect(subPlaylist).to.contain(basename(file.fileUrl))
119 }
120 }
121
122 {
123 const baseUrlAndPath = objectStorageBaseUrl
124 ? objectStorageBaseUrl + 'hls/' + videoUUID
125 : baseUrl + '/static/streaming-playlists/hls/' + videoUUID
126
127 for (const resolution of resolutions) {
128 await checkSegmentHash({
129 server,
130 baseUrlPlaylist: baseUrlAndPath,
131 baseUrlSegment: baseUrlAndPath,
132 resolution,
133 hlsPlaylist
134 })
135 }
136 }
137 }
138}
139
140describe('Test HLS videos', function () {
141 let servers: PeerTubeServer[] = []
142 let videoUUID = ''
143 let videoAudioUUID = ''
144
145 function runTestSuite (hlsOnly: boolean, objectStorageBaseUrl?: string) {
146
147 it('Should upload a video and transcode it to HLS', async function () {
148 this.timeout(120000)
149
150 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video 1', fixture: 'video_short.webm' } })
151 videoUUID = uuid
152
153 await waitJobs(servers)
154
155 await checkHlsPlaylist({ servers, videoUUID, hlsOnly, objectStorageBaseUrl })
156 })
157
158 it('Should upload an audio file and transcode it to HLS', async function () {
159 this.timeout(120000)
160
161 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video audio', fixture: 'sample.ogg' } })
162 videoAudioUUID = uuid
163
164 await waitJobs(servers)
165
166 await checkHlsPlaylist({
167 servers,
168 videoUUID: videoAudioUUID,
169 hlsOnly,
170 resolutions: [ DEFAULT_AUDIO_RESOLUTION, 360, 240 ],
171 objectStorageBaseUrl
172 })
173 })
174
175 it('Should update the video', async function () {
176 this.timeout(30000)
177
178 await servers[0].videos.update({ id: videoUUID, attributes: { name: 'video 1 updated' } })
179
180 await waitJobs(servers)
181
182 await checkHlsPlaylist({ servers, videoUUID, hlsOnly, objectStorageBaseUrl })
183 })
184
185 it('Should delete videos', async function () {
186 this.timeout(10000)
187
188 await servers[0].videos.remove({ id: videoUUID })
189 await servers[0].videos.remove({ id: videoAudioUUID })
190
191 await waitJobs(servers)
192
193 for (const server of servers) {
194 await server.videos.get({ id: videoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
195 await server.videos.get({ id: videoAudioUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
196 }
197 })
198
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 })
205
206 it('Should have an empty tmp directory', async function () {
207 for (const server of servers) {
208 await checkTmpIsEmpty(server)
209 }
210 })
211 }
212
213 before(async function () {
214 this.timeout(120000)
215
216 const configOverride = {
217 transcoding: {
218 enabled: true,
219 allow_audio_files: true,
220 hls: {
221 enabled: true
222 }
223 }
224 }
225 servers = await createMultipleServers(2, configOverride)
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])
232 })
233
234 describe('With WebTorrent & HLS enabled', function () {
235 runTestSuite(false)
236 })
237
238 describe('With only HLS enabled', function () {
239
240 before(async function () {
241 await servers[0].config.updateCustomSubConfig({
242 newConfig: {
243 transcoding: {
244 enabled: true,
245 allowAudioFiles: true,
246 resolutions: {
247 '144p': false,
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 }
262 }
263 }
264 })
265 })
266
267 runTestSuite(true)
268 })
269
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
286 after(async function () {
287 await cleanupTests(servers)
288 })
289})