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