]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/videos/video-hls.ts
Introduce notifications command
[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
113 it('Should upload a video and transcode it to HLS', async function () {
114 this.timeout(120000)
115
116 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video 1', fixture: 'video_short.webm' })
117 videoUUID = res.body.video.uuid
118
119 await waitJobs(servers)
120
121 await checkHlsPlaylist(servers, videoUUID, hlsOnly)
122 })
123
124 it('Should upload an audio file and transcode it to HLS', async function () {
125 this.timeout(120000)
126
127 const res = await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video audio', fixture: 'sample.ogg' })
128 videoAudioUUID = res.body.video.uuid
129
130 await waitJobs(servers)
131
132 await checkHlsPlaylist(servers, videoAudioUUID, hlsOnly, [ DEFAULT_AUDIO_RESOLUTION, 360, 240 ])
133 })
134
135 it('Should update the video', async function () {
136 this.timeout(10000)
137
138 await updateVideo(servers[0].url, servers[0].accessToken, videoUUID, { name: 'video 1 updated' })
139
140 await waitJobs(servers)
141
142 await checkHlsPlaylist(servers, videoUUID, hlsOnly)
143 })
144
145 it('Should delete videos', async function () {
146 this.timeout(10000)
147
148 await removeVideo(servers[0].url, servers[0].accessToken, videoUUID)
149 await removeVideo(servers[0].url, servers[0].accessToken, videoAudioUUID)
150
151 await waitJobs(servers)
152
153 for (const server of servers) {
154 await getVideo(server.url, videoUUID, HttpStatusCode.NOT_FOUND_404)
155 await getVideo(server.url, videoAudioUUID, HttpStatusCode.NOT_FOUND_404)
156 }
157 })
158
159 it('Should have the playlists/segment deleted from the disk', async function () {
160 for (const server of servers) {
161 await checkDirectoryIsEmpty(server, 'videos')
162 await checkDirectoryIsEmpty(server, join('streaming-playlists', 'hls'))
163 }
164 })
165
166 it('Should have an empty tmp directory', async function () {
167 for (const server of servers) {
168 await checkTmpIsEmpty(server)
169 }
170 })
171 }
172
173 before(async function () {
174 this.timeout(120000)
175
176 const configOverride = {
177 transcoding: {
178 enabled: true,
179 allow_audio_files: true,
180 hls: {
181 enabled: true
182 }
183 }
184 }
185 servers = await flushAndRunMultipleServers(2, configOverride)
186
187 // Get the access tokens
188 await setAccessTokensToServers(servers)
189
190 // Server 1 and server 2 follow each other
191 await doubleFollow(servers[0], servers[1])
192 })
193
194 describe('With WebTorrent & HLS enabled', function () {
195 runTestSuite(false)
196 })
197
198 describe('With only HLS enabled', function () {
199
200 before(async function () {
201 await servers[0].configCommand.updateCustomSubConfig({
202 newConfig: {
203 transcoding: {
204 enabled: true,
205 allowAudioFiles: true,
206 resolutions: {
207 '240p': true,
208 '360p': true,
209 '480p': true,
210 '720p': true,
211 '1080p': true,
212 '1440p': true,
213 '2160p': true
214 },
215 hls: {
216 enabled: true
217 },
218 webtorrent: {
219 enabled: false
220 }
221 }
222 }
223 })
224 })
225
226 runTestSuite(true)
227 })
228
229 after(async function () {
230 await cleanupTests(servers)
231 })
232 })