]>
Commit | Line | Data |
---|---|---|
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | |
2 | ||
3 | import 'mocha' | |
4 | import * as chai from 'chai' | |
5 | import { basename, join } from 'path' | |
6 | import { removeFragmentedMP4Ext, uuidRegex } from '@shared/core-utils' | |
7 | import { | |
8 | areObjectStorageTestsDisabled, | |
9 | checkDirectoryIsEmpty, | |
10 | checkResolutionsInMasterPlaylist, | |
11 | checkSegmentHash, | |
12 | checkTmpIsEmpty, | |
13 | cleanupTests, | |
14 | createMultipleServers, | |
15 | doubleFollow, | |
16 | expectStartWith, | |
17 | hlsInfohashExist, | |
18 | makeRawRequest, | |
19 | ObjectStorageCommand, | |
20 | PeerTubeServer, | |
21 | setAccessTokensToServers, | |
22 | waitJobs, | |
23 | webtorrentAdd | |
24 | } from '@shared/extra-utils' | |
25 | import { HttpStatusCode, VideoStreamingPlaylistType } from '@shared/models' | |
26 | import { DEFAULT_AUDIO_RESOLUTION } from '../../../initializers/constants' | |
27 | ||
28 | const expect = chai.expect | |
29 | ||
30 | async 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) { | |
43 | const videoDetails = await server.videos.get({ id: videoUUID }) | |
44 | const baseUrl = `http://${videoDetails.account.host}` | |
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 | ||
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 | ||
57 | // Check JSON files | |
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) | |
63 | expect(file.torrentUrl).to.match( | |
64 | new RegExp(`http://${server.host}/lazy-static/torrents/${uuidRegex}-${file.resolution.id}-hls.torrent`) | |
65 | ) | |
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 | ||
75 | expect(file.resolution.label).to.equal(resolution + 'p') | |
76 | ||
77 | await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200) | |
78 | await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200) | |
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 | ||
86 | // Check master playlist | |
87 | { | |
88 | await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions }) | |
89 | ||
90 | const masterPlaylist = await server.streamingPlaylists.get({ url: hlsPlaylist.playlistUrl }) | |
91 | ||
92 | let i = 0 | |
93 | for (const resolution of resolutions) { | |
94 | expect(masterPlaylist).to.contain(`${resolution}.m3u8`) | |
95 | expect(masterPlaylist).to.contain(`${resolution}.m3u8`) | |
96 | ||
97 | const url = 'http://' + videoDetails.account.host | |
98 | await hlsInfohashExist(url, hlsPlaylist.playlistUrl, i) | |
99 | ||
100 | i++ | |
101 | } | |
102 | } | |
103 | ||
104 | // Check resolution playlists | |
105 | { | |
106 | for (const resolution of resolutions) { | |
107 | const file = hlsFiles.find(f => f.resolution.id === resolution) | |
108 | const playlistName = removeFragmentedMP4Ext(basename(file.fileUrl)) + '.m3u8' | |
109 | ||
110 | const url = objectStorageBaseUrl | |
111 | ? `${objectStorageBaseUrl}hls/${videoUUID}/${playlistName}` | |
112 | : `${baseUrl}/static/streaming-playlists/hls/${videoUUID}/${playlistName}` | |
113 | ||
114 | const subPlaylist = await server.streamingPlaylists.get({ url }) | |
115 | ||
116 | expect(subPlaylist).to.match(new RegExp(`${uuidRegex}-${resolution}-fragmented.mp4`)) | |
117 | expect(subPlaylist).to.contain(basename(file.fileUrl)) | |
118 | } | |
119 | } | |
120 | ||
121 | { | |
122 | const baseUrlAndPath = objectStorageBaseUrl | |
123 | ? objectStorageBaseUrl + 'hls/' + videoUUID | |
124 | : baseUrl + '/static/streaming-playlists/hls/' + videoUUID | |
125 | ||
126 | for (const resolution of resolutions) { | |
127 | await checkSegmentHash({ | |
128 | server, | |
129 | baseUrlPlaylist: baseUrlAndPath, | |
130 | baseUrlSegment: baseUrlAndPath, | |
131 | resolution, | |
132 | hlsPlaylist | |
133 | }) | |
134 | } | |
135 | } | |
136 | } | |
137 | } | |
138 | ||
139 | describe('Test HLS videos', function () { | |
140 | let servers: PeerTubeServer[] = [] | |
141 | let videoUUID = '' | |
142 | let videoAudioUUID = '' | |
143 | ||
144 | function runTestSuite (hlsOnly: boolean, objectStorageBaseUrl?: string) { | |
145 | ||
146 | it('Should upload a video and transcode it to HLS', async function () { | |
147 | this.timeout(120000) | |
148 | ||
149 | const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video 1', fixture: 'video_short.webm' } }) | |
150 | videoUUID = uuid | |
151 | ||
152 | await waitJobs(servers) | |
153 | ||
154 | await checkHlsPlaylist({ servers, videoUUID, hlsOnly, objectStorageBaseUrl }) | |
155 | }) | |
156 | ||
157 | it('Should upload an audio file and transcode it to HLS', async function () { | |
158 | this.timeout(120000) | |
159 | ||
160 | const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video audio', fixture: 'sample.ogg' } }) | |
161 | videoAudioUUID = uuid | |
162 | ||
163 | await waitJobs(servers) | |
164 | ||
165 | await checkHlsPlaylist({ | |
166 | servers, | |
167 | videoUUID: videoAudioUUID, | |
168 | hlsOnly, | |
169 | resolutions: [ DEFAULT_AUDIO_RESOLUTION, 360, 240 ], | |
170 | objectStorageBaseUrl | |
171 | }) | |
172 | }) | |
173 | ||
174 | it('Should update the video', async function () { | |
175 | this.timeout(30000) | |
176 | ||
177 | await servers[0].videos.update({ id: videoUUID, attributes: { name: 'video 1 updated' } }) | |
178 | ||
179 | await waitJobs(servers) | |
180 | ||
181 | await checkHlsPlaylist({ servers, videoUUID, hlsOnly, objectStorageBaseUrl }) | |
182 | }) | |
183 | ||
184 | it('Should delete videos', async function () { | |
185 | this.timeout(10000) | |
186 | ||
187 | await servers[0].videos.remove({ id: videoUUID }) | |
188 | await servers[0].videos.remove({ id: videoAudioUUID }) | |
189 | ||
190 | await waitJobs(servers) | |
191 | ||
192 | for (const server of servers) { | |
193 | await server.videos.get({ id: videoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) | |
194 | await server.videos.get({ id: videoAudioUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) | |
195 | } | |
196 | }) | |
197 | ||
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 | }) | |
204 | ||
205 | it('Should have an empty tmp directory', async function () { | |
206 | for (const server of servers) { | |
207 | await checkTmpIsEmpty(server) | |
208 | } | |
209 | }) | |
210 | } | |
211 | ||
212 | before(async function () { | |
213 | this.timeout(120000) | |
214 | ||
215 | const configOverride = { | |
216 | transcoding: { | |
217 | enabled: true, | |
218 | allow_audio_files: true, | |
219 | hls: { | |
220 | enabled: true | |
221 | } | |
222 | } | |
223 | } | |
224 | servers = await createMultipleServers(2, configOverride) | |
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]) | |
231 | }) | |
232 | ||
233 | describe('With WebTorrent & HLS enabled', function () { | |
234 | runTestSuite(false) | |
235 | }) | |
236 | ||
237 | describe('With only HLS enabled', function () { | |
238 | ||
239 | before(async function () { | |
240 | await servers[0].config.updateCustomSubConfig({ | |
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 | } | |
260 | } | |
261 | } | |
262 | }) | |
263 | }) | |
264 | ||
265 | runTestSuite(true) | |
266 | }) | |
267 | ||
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 | ||
284 | after(async function () { | |
285 | await cleanupTests(servers) | |
286 | }) | |
287 | }) |