1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { basename, join } from 'path'
6 import { removeFragmentedMP4Ext, uuidRegex } from '@shared/core-utils'
8 areObjectStorageTestsDisabled,
10 checkResolutionsInMasterPlaylist,
14 createMultipleServers,
21 setAccessTokensToServers,
24 } from '@shared/server-commands'
25 import { HttpStatusCode, VideoStreamingPlaylistType } from '@shared/models'
26 import { DEFAULT_AUDIO_RESOLUTION } from '../../../initializers/constants'
28 const expect = chai.expect
30 async function checkHlsPlaylist (options: {
31 servers: PeerTubeServer[]
35 resolutions?: number[]
36 objectStorageBaseUrl: string
38 const { videoUUID, hlsOnly, objectStorageBaseUrl } = options
40 const resolutions = options.resolutions ?? [ 240, 360, 480, 720 ]
42 for (const server of options.servers) {
43 const videoDetails = await server.videos.get({ id: videoUUID })
44 const baseUrl = `http://${videoDetails.account.host}`
46 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
48 const hlsPlaylist = videoDetails.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
49 expect(hlsPlaylist).to.not.be.undefined
51 const hlsFiles = hlsPlaylist.files
52 expect(hlsFiles).to.have.lengthOf(resolutions.length)
54 if (hlsOnly) expect(videoDetails.files).to.have.lengthOf(0)
55 else expect(videoDetails.files).to.have.lengthOf(resolutions.length)
58 for (const resolution of resolutions) {
59 const file = hlsFiles.find(f => f.resolution.id === resolution)
60 expect(file).to.not.be.undefined
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`)
67 if (objectStorageBaseUrl) {
68 expectStartWith(file.fileUrl, objectStorageBaseUrl)
70 expect(file.fileUrl).to.match(
71 new RegExp(`${baseUrl}/static/streaming-playlists/hls/${videoDetails.uuid}/${uuidRegex}-${file.resolution.id}-fragmented.mp4`)
75 expect(file.resolution.label).to.equal(resolution + 'p')
77 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
78 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
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('')
86 // Check master playlist
88 await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
90 const masterPlaylist = await server.streamingPlaylists.get({ url: hlsPlaylist.playlistUrl })
93 for (const resolution of resolutions) {
94 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
95 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
97 const url = 'http://' + videoDetails.account.host
98 await hlsInfohashExist(url, hlsPlaylist.playlistUrl, i)
104 // Check resolution playlists
106 for (const resolution of resolutions) {
107 const file = hlsFiles.find(f => f.resolution.id === resolution)
108 const playlistName = removeFragmentedMP4Ext(basename(file.fileUrl)) + '.m3u8'
110 const url = objectStorageBaseUrl
111 ? `${objectStorageBaseUrl}hls/${videoUUID}/${playlistName}`
112 : `${baseUrl}/static/streaming-playlists/hls/${videoUUID}/${playlistName}`
114 const subPlaylist = await server.streamingPlaylists.get({ url })
116 expect(subPlaylist).to.match(new RegExp(`${uuidRegex}-${resolution}-fragmented.mp4`))
117 expect(subPlaylist).to.contain(basename(file.fileUrl))
122 const baseUrlAndPath = objectStorageBaseUrl
123 ? objectStorageBaseUrl + 'hls/' + videoUUID
124 : baseUrl + '/static/streaming-playlists/hls/' + videoUUID
126 for (const resolution of resolutions) {
127 await checkSegmentHash({
129 baseUrlPlaylist: baseUrlAndPath,
130 baseUrlSegment: baseUrlAndPath,
139 describe('Test HLS videos', function () {
140 let servers: PeerTubeServer[] = []
142 let videoAudioUUID = ''
144 function runTestSuite (hlsOnly: boolean, objectStorageBaseUrl?: string) {
146 it('Should upload a video and transcode it to HLS', async function () {
149 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video 1', fixture: 'video_short.webm' } })
152 await waitJobs(servers)
154 await checkHlsPlaylist({ servers, videoUUID, hlsOnly, objectStorageBaseUrl })
157 it('Should upload an audio file and transcode it to HLS', async function () {
160 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video audio', fixture: 'sample.ogg' } })
161 videoAudioUUID = uuid
163 await waitJobs(servers)
165 await checkHlsPlaylist({
167 videoUUID: videoAudioUUID,
169 resolutions: [ DEFAULT_AUDIO_RESOLUTION, 360, 240 ],
174 it('Should update the video', async function () {
177 await servers[0].videos.update({ id: videoUUID, attributes: { name: 'video 1 updated' } })
179 await waitJobs(servers)
181 await checkHlsPlaylist({ servers, videoUUID, hlsOnly, objectStorageBaseUrl })
184 it('Should delete videos', async function () {
187 await servers[0].videos.remove({ id: videoUUID })
188 await servers[0].videos.remove({ id: videoAudioUUID })
190 await waitJobs(servers)
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 })
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'))
205 it('Should have an empty tmp directory', async function () {
206 for (const server of servers) {
207 await checkTmpIsEmpty(server)
212 before(async function () {
215 const configOverride = {
218 allow_audio_files: true,
224 servers = await createMultipleServers(2, configOverride)
226 // Get the access tokens
227 await setAccessTokensToServers(servers)
229 // Server 1 and server 2 follow each other
230 await doubleFollow(servers[0], servers[1])
233 describe('With WebTorrent & HLS enabled', function () {
237 describe('With only HLS enabled', function () {
239 before(async function () {
240 await servers[0].config.updateCustomSubConfig({
244 allowAudioFiles: true,
269 describe('With object storage enabled', function () {
270 if (areObjectStorageTestsDisabled()) return
272 before(async function () {
275 const configOverride = ObjectStorageCommand.getDefaultConfig()
276 await ObjectStorageCommand.prepareDefaultBuckets()
278 await servers[0].kill()
279 await servers[0].run(configOverride)
282 runTestSuite(true, ObjectStorageCommand.getPlaylistBaseUrl())
285 after(async function () {
286 await cleanupTests(servers)