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