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