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