1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { basename, join } from 'path'
8 checkResolutionsInMasterPlaylist,
13 } from '@server/tests/shared'
14 import { areObjectStorageTestsDisabled, removeFragmentedMP4Ext, uuidRegex } from '@shared/core-utils'
15 import { HttpStatusCode, VideoStreamingPlaylistType } from '@shared/models'
18 createMultipleServers,
23 setAccessTokensToServers,
26 } from '@shared/server-commands'
27 import { DEFAULT_AUDIO_RESOLUTION } from '../../../initializers/constants'
29 const expect = chai.expect
31 async function checkHlsPlaylist (options: {
32 servers: PeerTubeServer[]
36 resolutions?: number[]
37 objectStorageBaseUrl: string
39 const { videoUUID, hlsOnly, objectStorageBaseUrl } = options
41 const resolutions = options.resolutions ?? [ 240, 360, 480, 720 ]
43 for (const server of options.servers) {
44 const videoDetails = await server.videos.get({ id: videoUUID })
45 const baseUrl = `http://${videoDetails.account.host}`
47 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
49 const hlsPlaylist = videoDetails.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
50 expect(hlsPlaylist).to.not.be.undefined
52 const hlsFiles = hlsPlaylist.files
53 expect(hlsFiles).to.have.lengthOf(resolutions.length)
55 if (hlsOnly) expect(videoDetails.files).to.have.lengthOf(0)
56 else expect(videoDetails.files).to.have.lengthOf(resolutions.length)
59 for (const resolution of resolutions) {
60 const file = hlsFiles.find(f => f.resolution.id === resolution)
61 expect(file).to.not.be.undefined
63 expect(file.magnetUri).to.have.lengthOf.above(2)
64 expect(file.torrentUrl).to.match(
65 new RegExp(`http://${server.host}/lazy-static/torrents/${uuidRegex}-${file.resolution.id}-hls.torrent`)
68 if (objectStorageBaseUrl) {
69 expectStartWith(file.fileUrl, objectStorageBaseUrl)
71 expect(file.fileUrl).to.match(
72 new RegExp(`${baseUrl}/static/streaming-playlists/hls/${videoDetails.uuid}/${uuidRegex}-${file.resolution.id}-fragmented.mp4`)
76 expect(file.resolution.label).to.equal(resolution + 'p')
78 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
79 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
81 const torrent = await webtorrentAdd(file.magnetUri, true)
82 expect(torrent.files).to.be.an('array')
83 expect(torrent.files.length).to.equal(1)
84 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
87 // Check master playlist
89 await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
91 const masterPlaylist = await server.streamingPlaylists.get({ url: hlsPlaylist.playlistUrl })
94 for (const resolution of resolutions) {
95 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
96 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
98 const url = 'http://' + videoDetails.account.host
99 await hlsInfohashExist(url, hlsPlaylist.playlistUrl, i)
105 // Check resolution playlists
107 for (const resolution of resolutions) {
108 const file = hlsFiles.find(f => f.resolution.id === resolution)
109 const playlistName = removeFragmentedMP4Ext(basename(file.fileUrl)) + '.m3u8'
111 const url = objectStorageBaseUrl
112 ? `${objectStorageBaseUrl}hls/${videoUUID}/${playlistName}`
113 : `${baseUrl}/static/streaming-playlists/hls/${videoUUID}/${playlistName}`
115 const subPlaylist = await server.streamingPlaylists.get({ url })
117 expect(subPlaylist).to.match(new RegExp(`${uuidRegex}-${resolution}-fragmented.mp4`))
118 expect(subPlaylist).to.contain(basename(file.fileUrl))
123 const baseUrlAndPath = objectStorageBaseUrl
124 ? objectStorageBaseUrl + 'hls/' + videoUUID
125 : baseUrl + '/static/streaming-playlists/hls/' + videoUUID
127 for (const resolution of resolutions) {
128 await checkSegmentHash({
130 baseUrlPlaylist: baseUrlAndPath,
131 baseUrlSegment: baseUrlAndPath,
140 describe('Test HLS videos', function () {
141 let servers: PeerTubeServer[] = []
143 let videoAudioUUID = ''
145 function runTestSuite (hlsOnly: boolean, objectStorageBaseUrl?: string) {
147 it('Should upload a video and transcode it to HLS', async function () {
150 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video 1', fixture: 'video_short.webm' } })
153 await waitJobs(servers)
155 await checkHlsPlaylist({ servers, videoUUID, hlsOnly, objectStorageBaseUrl })
158 it('Should upload an audio file and transcode it to HLS', async function () {
161 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video audio', fixture: 'sample.ogg' } })
162 videoAudioUUID = uuid
164 await waitJobs(servers)
166 await checkHlsPlaylist({
168 videoUUID: videoAudioUUID,
170 resolutions: [ DEFAULT_AUDIO_RESOLUTION, 360, 240 ],
175 it('Should update the video', async function () {
178 await servers[0].videos.update({ id: videoUUID, attributes: { name: 'video 1 updated' } })
180 await waitJobs(servers)
182 await checkHlsPlaylist({ servers, videoUUID, hlsOnly, objectStorageBaseUrl })
185 it('Should delete videos', async function () {
188 await servers[0].videos.remove({ id: videoUUID })
189 await servers[0].videos.remove({ id: videoAudioUUID })
191 await waitJobs(servers)
193 for (const server of servers) {
194 await server.videos.get({ id: videoUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
195 await server.videos.get({ id: videoAudioUUID, expectedStatus: HttpStatusCode.NOT_FOUND_404 })
199 it('Should have the playlists/segment deleted from the disk', async function () {
200 for (const server of servers) {
201 await checkDirectoryIsEmpty(server, 'videos')
202 await checkDirectoryIsEmpty(server, join('streaming-playlists', 'hls'))
206 it('Should have an empty tmp directory', async function () {
207 for (const server of servers) {
208 await checkTmpIsEmpty(server)
213 before(async function () {
216 const configOverride = {
219 allow_audio_files: true,
225 servers = await createMultipleServers(2, configOverride)
227 // Get the access tokens
228 await setAccessTokensToServers(servers)
230 // Server 1 and server 2 follow each other
231 await doubleFollow(servers[0], servers[1])
234 describe('With WebTorrent & HLS enabled', function () {
238 describe('With only HLS enabled', function () {
240 before(async function () {
241 await servers[0].config.updateCustomSubConfig({
245 allowAudioFiles: true,
270 describe('With object storage enabled', function () {
271 if (areObjectStorageTestsDisabled()) return
273 before(async function () {
276 const configOverride = ObjectStorageCommand.getDefaultConfig()
277 await ObjectStorageCommand.prepareDefaultBuckets()
279 await servers[0].kill()
280 await servers[0].run(configOverride)
283 runTestSuite(true, ObjectStorageCommand.getPlaylistBaseUrl())
286 after(async function () {
287 await cleanupTests(servers)