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