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