1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { basename, dirname, join } from 'path'
5 import { removeFragmentedMP4Ext, uuidRegex } from '@shared/core-utils'
6 import { sha256 } from '@shared/extra-utils'
7 import { HttpStatusCode, VideoStreamingPlaylist, VideoStreamingPlaylistType } from '@shared/models'
8 import { makeRawRequest, PeerTubeServer, webtorrentAdd } from '@shared/server-commands'
9 import { expectStartWith } from './checks'
10 import { hlsInfohashExist } from './tracker'
12 async function checkSegmentHash (options: {
13 server: PeerTubeServer
14 baseUrlPlaylist: string
15 baseUrlSegment: string
17 hlsPlaylist: VideoStreamingPlaylist
19 const { server, baseUrlPlaylist, baseUrlSegment, resolution, hlsPlaylist } = options
20 const command = server.streamingPlaylists
22 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
23 const videoName = basename(file.fileUrl)
25 const playlist = await command.get({ url: `${baseUrlPlaylist}/${removeFragmentedMP4Ext(videoName)}.m3u8` })
27 const matches = /#EXT-X-BYTERANGE:(\d+)@(\d+)/.exec(playlist)
29 const length = parseInt(matches[1], 10)
30 const offset = parseInt(matches[2], 10)
31 const range = `${offset}-${offset + length - 1}`
33 const segmentBody = await command.getFragmentedSegment({
34 url: `${baseUrlSegment}/${videoName}`,
35 expectedStatus: HttpStatusCode.PARTIAL_CONTENT_206,
36 range: `bytes=${range}`
39 const shaBody = await command.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url })
40 expect(sha256(segmentBody)).to.equal(shaBody[videoName][range])
43 // ---------------------------------------------------------------------------
45 async function checkLiveSegmentHash (options: {
46 server: PeerTubeServer
47 baseUrlSegment: string
50 hlsPlaylist: VideoStreamingPlaylist
52 const { server, baseUrlSegment, videoUUID, segmentName, hlsPlaylist } = options
53 const command = server.streamingPlaylists
55 const segmentBody = await command.getFragmentedSegment({ url: `${baseUrlSegment}/${videoUUID}/${segmentName}` })
56 const shaBody = await command.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url })
58 expect(sha256(segmentBody)).to.equal(shaBody[segmentName])
61 // ---------------------------------------------------------------------------
63 async function checkResolutionsInMasterPlaylist (options: {
64 server: PeerTubeServer
67 transcoded?: boolean // default true
68 withRetry?: boolean // default false
70 const { server, playlistUrl, resolutions, withRetry = false, transcoded = true } = options
72 const masterPlaylist = await server.streamingPlaylists.get({ url: playlistUrl, withRetry })
74 for (const resolution of resolutions) {
75 const reg = transcoded
76 ? new RegExp('#EXT-X-STREAM-INF:BANDWIDTH=\\d+,RESOLUTION=\\d+x' + resolution + ',(FRAME-RATE=\\d+,)?CODECS="avc1.64001f,mp4a.40.2"')
77 : new RegExp('#EXT-X-STREAM-INF:BANDWIDTH=\\d+,RESOLUTION=\\d+x' + resolution + '')
79 expect(masterPlaylist).to.match(reg)
82 const playlistsLength = masterPlaylist.split('\n').filter(line => line.startsWith('#EXT-X-STREAM-INF:BANDWIDTH='))
83 expect(playlistsLength).to.have.lengthOf(resolutions.length)
86 async function completeCheckHlsPlaylist (options: {
87 servers: PeerTubeServer[]
91 resolutions?: number[]
92 objectStorageBaseUrl: string
94 const { videoUUID, hlsOnly, objectStorageBaseUrl } = options
96 const resolutions = options.resolutions ?? [ 240, 360, 480, 720 ]
98 for (const server of options.servers) {
99 const videoDetails = await server.videos.get({ id: videoUUID })
100 const baseUrl = `http://${videoDetails.account.host}`
102 expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
104 const hlsPlaylist = videoDetails.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
105 expect(hlsPlaylist).to.not.be.undefined
107 const hlsFiles = hlsPlaylist.files
108 expect(hlsFiles).to.have.lengthOf(resolutions.length)
110 if (hlsOnly) expect(videoDetails.files).to.have.lengthOf(0)
111 else expect(videoDetails.files).to.have.lengthOf(resolutions.length)
114 for (const resolution of resolutions) {
115 const file = hlsFiles.find(f => f.resolution.id === resolution)
116 expect(file).to.not.be.undefined
118 expect(file.magnetUri).to.have.lengthOf.above(2)
119 expect(file.torrentUrl).to.match(
120 new RegExp(`${server.url}/lazy-static/torrents/${uuidRegex}-${file.resolution.id}-hls.torrent`)
123 if (objectStorageBaseUrl) {
124 expectStartWith(file.fileUrl, objectStorageBaseUrl)
126 expect(file.fileUrl).to.match(
127 new RegExp(`${baseUrl}/static/streaming-playlists/hls/${videoDetails.uuid}/${uuidRegex}-${file.resolution.id}-fragmented.mp4`)
131 expect(file.resolution.label).to.equal(resolution + 'p')
133 await makeRawRequest({ url: file.torrentUrl, expectedStatus: HttpStatusCode.OK_200 })
134 await makeRawRequest({ url: file.fileUrl, expectedStatus: HttpStatusCode.OK_200 })
136 const torrent = await webtorrentAdd(file.magnetUri, true)
137 expect(torrent.files).to.be.an('array')
138 expect(torrent.files.length).to.equal(1)
139 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
142 // Check master playlist
144 await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
146 const masterPlaylist = await server.streamingPlaylists.get({ url: hlsPlaylist.playlistUrl })
149 for (const resolution of resolutions) {
150 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
151 expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
153 const url = 'http://' + videoDetails.account.host
154 await hlsInfohashExist(url, hlsPlaylist.playlistUrl, i)
160 // Check resolution playlists
162 for (const resolution of resolutions) {
163 const file = hlsFiles.find(f => f.resolution.id === resolution)
164 const playlistName = removeFragmentedMP4Ext(basename(file.fileUrl)) + '.m3u8'
166 const url = objectStorageBaseUrl
167 ? `${objectStorageBaseUrl}hls/${videoUUID}/${playlistName}`
168 : `${baseUrl}/static/streaming-playlists/hls/${videoUUID}/${playlistName}`
170 const subPlaylist = await server.streamingPlaylists.get({ url })
172 expect(subPlaylist).to.match(new RegExp(`${uuidRegex}-${resolution}-fragmented.mp4`))
173 expect(subPlaylist).to.contain(basename(file.fileUrl))
178 const baseUrlAndPath = objectStorageBaseUrl
179 ? objectStorageBaseUrl + 'hls/' + videoUUID
180 : baseUrl + '/static/streaming-playlists/hls/' + videoUUID
182 for (const resolution of resolutions) {
183 await checkSegmentHash({
185 baseUrlPlaylist: baseUrlAndPath,
186 baseUrlSegment: baseUrlAndPath,
195 async function checkVideoFileTokenReinjection (options: {
196 server: PeerTubeServer
198 videoFileToken: string
199 resolutions: number[]
202 const { server, resolutions, videoFileToken, videoUUID, isLive } = options
204 const video = await server.videos.getWithToken({ id: videoUUID })
205 const hls = video.streamingPlaylists[0]
207 const query = { videoFileToken, reinjectVideoFileToken: 'true' }
208 const { text } = await makeRawRequest({ url: hls.playlistUrl, query, expectedStatus: HttpStatusCode.OK_200 })
210 for (let i = 0; i < resolutions.length; i++) {
211 const resolution = resolutions[i]
213 const suffix = isLive
217 expect(text).to.contain(`${suffix}.m3u8?videoFileToken=${videoFileToken}`)
220 const resolutionPlaylists = extractResolutionPlaylistUrls(hls.playlistUrl, text)
221 expect(resolutionPlaylists).to.have.lengthOf(resolutions.length)
223 for (const url of resolutionPlaylists) {
224 const { text } = await makeRawRequest({ url, query, expectedStatus: HttpStatusCode.OK_200 })
226 const extension = isLive
230 expect(text).to.contain(`${extension}?videoFileToken=${videoFileToken}`)
234 function extractResolutionPlaylistUrls (masterPath: string, masterContent: string) {
235 return masterContent.match(/^([^.]+\.m3u8.*)/mg)
236 .map(filename => join(dirname(masterPath), filename))
241 checkLiveSegmentHash,
242 checkResolutionsInMasterPlaylist,
243 completeCheckHlsPlaylist,
244 extractResolutionPlaylistUrls,
245 checkVideoFileTokenReinjection