]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
09209296 | 2 | |
09209296 | 3 | import 'mocha' |
bd54ad19 | 4 | import * as chai from 'chai' |
764b1a14 C |
5 | import { basename, join } from 'path' |
6 | import { removeFragmentedMP4Ext, uuidRegex } from '@shared/core-utils' | |
09209296 C |
7 | import { |
8 | checkDirectoryIsEmpty, | |
bd54ad19 | 9 | checkResolutionsInMasterPlaylist, |
4c280004 | 10 | checkSegmentHash, |
7243f84d C |
11 | checkTmpIsEmpty, |
12 | cleanupTests, | |
254d3579 | 13 | createMultipleServers, |
4c7e60bc | 14 | doubleFollow, |
a1587156 | 15 | makeRawRequest, |
254d3579 | 16 | PeerTubeServer, |
a1587156 | 17 | setAccessTokensToServers, |
a1587156 C |
18 | waitJobs, |
19 | webtorrentAdd | |
d23dd9fb | 20 | } from '@shared/extra-utils' |
4c7e60bc | 21 | import { HttpStatusCode, VideoStreamingPlaylistType } from '@shared/models' |
b345a804 | 22 | import { DEFAULT_AUDIO_RESOLUTION } from '../../../initializers/constants' |
09209296 C |
23 | |
24 | const expect = chai.expect | |
25 | ||
254d3579 | 26 | async function checkHlsPlaylist (servers: PeerTubeServer[], videoUUID: string, hlsOnly: boolean, resolutions = [ 240, 360, 480, 720 ]) { |
09209296 | 27 | for (const server of servers) { |
89d241a7 | 28 | const videoDetails = await server.videos.get({ id: videoUUID }) |
d7a25329 | 29 | const baseUrl = `http://${videoDetails.account.host}` |
09209296 C |
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 | ||
d7a25329 C |
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 | ||
83903cb6 | 42 | // Check JSON files |
d7a25329 C |
43 | for (const resolution of resolutions) { |
44 | const file = hlsFiles.find(f => f.resolution.id === resolution) | |
45 | expect(file).to.not.be.undefined | |
46 | ||
47 | expect(file.magnetUri).to.have.lengthOf.above(2) | |
83903cb6 C |
48 | expect(file.torrentUrl).to.match( |
49 | new RegExp(`http://${server.host}/lazy-static/torrents/${uuidRegex}-${file.resolution.id}-hls.torrent`) | |
50 | ) | |
51 | expect(file.fileUrl).to.match( | |
52 | new RegExp(`${baseUrl}/static/streaming-playlists/hls/${videoDetails.uuid}/${uuidRegex}-${file.resolution.id}-fragmented.mp4`) | |
a1587156 | 53 | ) |
d7a25329 C |
54 | expect(file.resolution.label).to.equal(resolution + 'p') |
55 | ||
f2eb23cd RK |
56 | await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200) |
57 | await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200) | |
d7a25329 C |
58 | |
59 | const torrent = await webtorrentAdd(file.magnetUri, true) | |
60 | expect(torrent.files).to.be.an('array') | |
61 | expect(torrent.files.length).to.equal(1) | |
62 | expect(torrent.files[0].path).to.exist.and.to.not.equal('') | |
63 | } | |
64 | ||
83903cb6 | 65 | // Check master playlist |
09209296 | 66 | { |
57f879a5 | 67 | await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions }) |
09209296 | 68 | |
89d241a7 | 69 | const masterPlaylist = await server.streamingPlaylists.get({ url: hlsPlaylist.playlistUrl }) |
09209296 | 70 | |
09209296 | 71 | for (const resolution of resolutions) { |
52201311 | 72 | expect(masterPlaylist).to.contain(`${resolution}.m3u8`) |
09209296 C |
73 | expect(masterPlaylist).to.contain(`${resolution}.m3u8`) |
74 | } | |
75 | } | |
76 | ||
83903cb6 | 77 | // Check resolution playlists |
09209296 C |
78 | { |
79 | for (const resolution of resolutions) { | |
764b1a14 C |
80 | const file = hlsFiles.find(f => f.resolution.id === resolution) |
81 | const playlistName = removeFragmentedMP4Ext(basename(file.fileUrl)) + '.m3u8' | |
82 | ||
89d241a7 | 83 | const subPlaylist = await server.streamingPlaylists.get({ |
764b1a14 | 84 | url: `${baseUrl}/static/streaming-playlists/hls/${videoUUID}/${playlistName}` |
57f879a5 | 85 | }) |
09209296 | 86 | |
83903cb6 C |
87 | expect(subPlaylist).to.match(new RegExp(`${uuidRegex}-${resolution}-fragmented.mp4`)) |
88 | expect(subPlaylist).to.contain(basename(file.fileUrl)) | |
09209296 C |
89 | } |
90 | } | |
91 | ||
92 | { | |
d7a25329 | 93 | const baseUrlAndPath = baseUrl + '/static/streaming-playlists/hls' |
09209296 | 94 | |
4c280004 | 95 | for (const resolution of resolutions) { |
57f879a5 C |
96 | await checkSegmentHash({ |
97 | server, | |
98 | baseUrlPlaylist: baseUrlAndPath, | |
99 | baseUrlSegment: baseUrlAndPath, | |
100 | videoUUID, | |
101 | resolution, | |
102 | hlsPlaylist | |
103 | }) | |
09209296 C |
104 | } |
105 | } | |
106 | } | |
107 | } | |
108 | ||
109 | describe('Test HLS videos', function () { | |
254d3579 | 110 | let servers: PeerTubeServer[] = [] |
09209296 | 111 | let videoUUID = '' |
b345a804 | 112 | let videoAudioUUID = '' |
09209296 | 113 | |
d7a25329 | 114 | function runTestSuite (hlsOnly: boolean) { |
dd0ebb71 | 115 | |
d7a25329 C |
116 | it('Should upload a video and transcode it to HLS', async function () { |
117 | this.timeout(120000) | |
09209296 | 118 | |
89d241a7 | 119 | const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video 1', fixture: 'video_short.webm' } }) |
d23dd9fb | 120 | videoUUID = uuid |
09209296 | 121 | |
d7a25329 | 122 | await waitJobs(servers) |
09209296 | 123 | |
d7a25329 C |
124 | await checkHlsPlaylist(servers, videoUUID, hlsOnly) |
125 | }) | |
09209296 | 126 | |
d7a25329 C |
127 | it('Should upload an audio file and transcode it to HLS', async function () { |
128 | this.timeout(120000) | |
09209296 | 129 | |
89d241a7 | 130 | const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video audio', fixture: 'sample.ogg' } }) |
d23dd9fb | 131 | videoAudioUUID = uuid |
09209296 | 132 | |
d7a25329 | 133 | await waitJobs(servers) |
09209296 | 134 | |
657bba2b | 135 | await checkHlsPlaylist(servers, videoAudioUUID, hlsOnly, [ DEFAULT_AUDIO_RESOLUTION, 360, 240 ]) |
d7a25329 | 136 | }) |
09209296 | 137 | |
d7a25329 C |
138 | it('Should update the video', async function () { |
139 | this.timeout(10000) | |
b345a804 | 140 | |
89d241a7 | 141 | await servers[0].videos.update({ id: videoUUID, attributes: { name: 'video 1 updated' } }) |
b345a804 | 142 | |
d7a25329 | 143 | await waitJobs(servers) |
b345a804 | 144 | |
d7a25329 C |
145 | await checkHlsPlaylist(servers, videoUUID, hlsOnly) |
146 | }) | |
b345a804 | 147 | |
d7a25329 C |
148 | it('Should delete videos', async function () { |
149 | this.timeout(10000) | |
80b8ad2a | 150 | |
89d241a7 C |
151 | await servers[0].videos.remove({ id: videoUUID }) |
152 | await servers[0].videos.remove({ id: videoAudioUUID }) | |
09209296 | 153 | |
d7a25329 | 154 | await waitJobs(servers) |
09209296 | 155 | |
d7a25329 | 156 | for (const server of servers) { |
89d241a7 C |
157 | await server.videos.get({ id: videoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) |
158 | await server.videos.get({ id: videoAudioUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 }) | |
d7a25329 C |
159 | } |
160 | }) | |
09209296 | 161 | |
d7a25329 C |
162 | it('Should have the playlists/segment deleted from the disk', async function () { |
163 | for (const server of servers) { | |
164 | await checkDirectoryIsEmpty(server, 'videos') | |
165 | await checkDirectoryIsEmpty(server, join('streaming-playlists', 'hls')) | |
166 | } | |
167 | }) | |
80b8ad2a | 168 | |
d7a25329 C |
169 | it('Should have an empty tmp directory', async function () { |
170 | for (const server of servers) { | |
171 | await checkTmpIsEmpty(server) | |
172 | } | |
173 | }) | |
174 | } | |
09209296 | 175 | |
d7a25329 C |
176 | before(async function () { |
177 | this.timeout(120000) | |
09209296 | 178 | |
d7a25329 C |
179 | const configOverride = { |
180 | transcoding: { | |
181 | enabled: true, | |
182 | allow_audio_files: true, | |
183 | hls: { | |
184 | enabled: true | |
185 | } | |
186 | } | |
09209296 | 187 | } |
254d3579 | 188 | servers = await createMultipleServers(2, configOverride) |
d7a25329 C |
189 | |
190 | // Get the access tokens | |
191 | await setAccessTokensToServers(servers) | |
192 | ||
193 | // Server 1 and server 2 follow each other | |
194 | await doubleFollow(servers[0], servers[1]) | |
09209296 C |
195 | }) |
196 | ||
d7a25329 C |
197 | describe('With WebTorrent & HLS enabled', function () { |
198 | runTestSuite(false) | |
09209296 C |
199 | }) |
200 | ||
d7a25329 C |
201 | describe('With only HLS enabled', function () { |
202 | ||
203 | before(async function () { | |
89d241a7 | 204 | await servers[0].config.updateCustomSubConfig({ |
65e6e260 C |
205 | newConfig: { |
206 | transcoding: { | |
207 | enabled: true, | |
208 | allowAudioFiles: true, | |
209 | resolutions: { | |
210 | '240p': true, | |
211 | '360p': true, | |
212 | '480p': true, | |
213 | '720p': true, | |
214 | '1080p': true, | |
215 | '1440p': true, | |
216 | '2160p': true | |
217 | }, | |
218 | hls: { | |
219 | enabled: true | |
220 | }, | |
221 | webtorrent: { | |
222 | enabled: false | |
223 | } | |
d7a25329 C |
224 | } |
225 | } | |
226 | }) | |
227 | }) | |
228 | ||
229 | runTestSuite(true) | |
09209296 C |
230 | }) |
231 | ||
7c3b7976 C |
232 | after(async function () { |
233 | await cleanupTests(servers) | |
09209296 C |
234 | }) |
235 | }) |