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