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,
20 setAccessTokensToServers,
23 } from '@shared/extra-utils'
24 import { HttpStatusCode, VideoStreamingPlaylistType } from '@shared/models'
25 import { DEFAULT_AUDIO_RESOLUTION } from '../../../initializers/constants'
27 const expect = chai.expect
29 async function checkHlsPlaylist (options: {
30 servers: PeerTubeServer[]
34 resolutions?: number[]
35 objectStorageBaseUrl: string
37 const { videoUUID, hlsOnly, objectStorageBaseUrl } = options
39 const resolutions = options.resolutions ?? [ 240, 360, 480, 720 ]
41 for (const server of options.servers) {
42 const videoDetails = await server.videos.get({ id: videoUUID })
43 const baseUrl = `http://${videoDetails.account.host}`
45 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
47 const hlsPlaylist = videoDetails.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
48 expect(hlsPlaylist).to.not.be.undefined
50 const hlsFiles = hlsPlaylist.files
51 expect(hlsFiles).to.have.lengthOf(resolutions.length)
53 if (hlsOnly) expect(videoDetails.files).to.have.lengthOf(0)
54 else expect(videoDetails.files).to.have.lengthOf(resolutions.length)
57 for (const resolution of resolutions) {
58 const file = hlsFiles.find(f => f.resolution.id === resolution)
59 expect(file).to.not.be.undefined
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`)
66 if (objectStorageBaseUrl) {
67 expectStartWith(file.fileUrl, objectStorageBaseUrl)
69 expect(file.fileUrl).to.match(
70 new RegExp(`${baseUrl}/static/streaming-playlists/hls/${videoDetails.uuid}/${uuidRegex}-${file.resolution.id}-fragmented.mp4`)
74 expect(file.resolution.label).to.equal(resolution + 'p')
76 await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200)
77 await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200)
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('')
85 // Check master playlist
87 await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
89 const masterPlaylist = await server.streamingPlaylists.get({ url: hlsPlaylist.playlistUrl })
91 for (const resolution of resolutions) {
92 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
93 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
97 // Check resolution playlists
99 for (const resolution of resolutions) {
100 const file = hlsFiles.find(f => f.resolution.id === resolution)
101 const playlistName = removeFragmentedMP4Ext(basename(file.fileUrl)) + '.m3u8'
103 const url = objectStorageBaseUrl
104 ? `${objectStorageBaseUrl}hls_${videoUUID}/${playlistName}`
105 : `${baseUrl}/static/streaming-playlists/hls/${videoUUID}/${playlistName}`
107 const subPlaylist = await server.streamingPlaylists.get({ url })
109 expect(subPlaylist).to.match(new RegExp(`${uuidRegex}-${resolution}-fragmented.mp4`))
110 expect(subPlaylist).to.contain(basename(file.fileUrl))
115 const baseUrlAndPath = objectStorageBaseUrl
116 ? objectStorageBaseUrl + 'hls_' + videoUUID
117 : baseUrl + '/static/streaming-playlists/hls/' + videoUUID
119 for (const resolution of resolutions) {
120 await checkSegmentHash({
122 baseUrlPlaylist: baseUrlAndPath,
123 baseUrlSegment: baseUrlAndPath,
132 describe('Test HLS videos', function () {
133 let servers: PeerTubeServer[] = []
135 let videoAudioUUID = ''
137 function runTestSuite (hlsOnly: boolean, objectStorageBaseUrl?: string) {
139 it('Should upload a video and transcode it to HLS', async function () {
142 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video 1', fixture: 'video_short.webm' } })
145 await waitJobs(servers)
147 await checkHlsPlaylist({ servers, videoUUID, hlsOnly, objectStorageBaseUrl })
150 it('Should upload an audio file and transcode it to HLS', async function () {
153 const { uuid } = await servers[0].videos.upload({ attributes: { name: 'video audio', fixture: 'sample.ogg' } })
154 videoAudioUUID = uuid
156 await waitJobs(servers)
158 await checkHlsPlaylist({
160 videoUUID: videoAudioUUID,
162 resolutions: [ DEFAULT_AUDIO_RESOLUTION, 360, 240 ],
167 it('Should update the video', async function () {
170 await servers[0].videos.update({ id: videoUUID, attributes: { name: 'video 1 updated' } })
172 await waitJobs(servers)
174 await checkHlsPlaylist({ servers, videoUUID, hlsOnly, objectStorageBaseUrl })
177 it('Should delete videos', async function () {
180 await servers[0].videos.remove({ id: videoUUID })
181 await servers[0].videos.remove({ id: videoAudioUUID })
183 await waitJobs(servers)
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 })
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'))
198 it('Should have an empty tmp directory', async function () {
199 for (const server of servers) {
200 await checkTmpIsEmpty(server)
205 before(async function () {
208 const configOverride = {
211 allow_audio_files: true,
217 servers = await createMultipleServers(2, configOverride)
219 // Get the access tokens
220 await setAccessTokensToServers(servers)
222 // Server 1 and server 2 follow each other
223 await doubleFollow(servers[0], servers[1])
226 describe('With WebTorrent & HLS enabled', function () {
230 describe('With only HLS enabled', function () {
232 before(async function () {
233 await servers[0].config.updateCustomSubConfig({
237 allowAudioFiles: true,
261 describe('With object storage enabled', function () {
262 if (areObjectStorageTestsDisabled()) return
264 before(async function () {
267 const configOverride = ObjectStorageCommand.getDefaultConfig()
268 await ObjectStorageCommand.prepareDefaultBuckets()
270 await servers[0].kill()
271 await servers[0].run(configOverride)
274 runTestSuite(true, ObjectStorageCommand.getPlaylistBaseUrl())
277 after(async function () {
278 await cleanupTests(servers)