2022-10-12 16:09:02 +02:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
|
|
|
|
|
2021-07-09 10:21:10 +02:00
|
|
|
import { expect } from 'chai'
|
2021-07-22 14:28:03 +02:00
|
|
|
import { basename } from 'path'
|
2022-10-12 16:09:02 +02:00
|
|
|
import { removeFragmentedMP4Ext, uuidRegex } from '@shared/core-utils'
|
2021-12-17 13:58:07 +01:00
|
|
|
import { sha256 } from '@shared/extra-utils'
|
2022-10-12 16:09:02 +02:00
|
|
|
import { HttpStatusCode, VideoStreamingPlaylist, VideoStreamingPlaylistType } from '@shared/models'
|
|
|
|
import { makeRawRequest, PeerTubeServer, webtorrentAdd } from '@shared/server-commands'
|
|
|
|
import { expectStartWith } from './checks'
|
|
|
|
import { hlsInfohashExist } from './tracker'
|
2021-07-09 10:21:10 +02:00
|
|
|
|
|
|
|
async function checkSegmentHash (options: {
|
2021-07-16 09:47:51 +02:00
|
|
|
server: PeerTubeServer
|
2021-07-09 10:21:10 +02:00
|
|
|
baseUrlPlaylist: string
|
|
|
|
baseUrlSegment: string
|
|
|
|
resolution: number
|
|
|
|
hlsPlaylist: VideoStreamingPlaylist
|
|
|
|
}) {
|
2021-08-17 08:26:20 +02:00
|
|
|
const { server, baseUrlPlaylist, baseUrlSegment, resolution, hlsPlaylist } = options
|
2021-07-16 09:04:35 +02:00
|
|
|
const command = server.streamingPlaylists
|
2021-07-09 10:21:10 +02:00
|
|
|
|
2021-07-22 14:28:03 +02:00
|
|
|
const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
|
|
|
|
const videoName = basename(file.fileUrl)
|
2021-07-09 10:21:10 +02:00
|
|
|
|
2021-08-17 08:26:20 +02:00
|
|
|
const playlist = await command.get({ url: `${baseUrlPlaylist}/${removeFragmentedMP4Ext(videoName)}.m3u8` })
|
2021-07-23 11:20:00 +02:00
|
|
|
|
2021-07-09 10:21:10 +02:00
|
|
|
const matches = /#EXT-X-BYTERANGE:(\d+)@(\d+)/.exec(playlist)
|
|
|
|
|
|
|
|
const length = parseInt(matches[1], 10)
|
|
|
|
const offset = parseInt(matches[2], 10)
|
|
|
|
const range = `${offset}-${offset + length - 1}`
|
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
const segmentBody = await command.getFragmentedSegment({
|
2021-08-17 08:26:20 +02:00
|
|
|
url: `${baseUrlSegment}/${videoName}`,
|
2021-07-09 10:21:10 +02:00
|
|
|
expectedStatus: HttpStatusCode.PARTIAL_CONTENT_206,
|
|
|
|
range: `bytes=${range}`
|
|
|
|
})
|
|
|
|
|
|
|
|
const shaBody = await command.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url })
|
|
|
|
expect(sha256(segmentBody)).to.equal(shaBody[videoName][range])
|
|
|
|
}
|
|
|
|
|
|
|
|
async function checkLiveSegmentHash (options: {
|
2021-07-16 09:47:51 +02:00
|
|
|
server: PeerTubeServer
|
2021-07-09 10:21:10 +02:00
|
|
|
baseUrlSegment: string
|
|
|
|
videoUUID: string
|
|
|
|
segmentName: string
|
|
|
|
hlsPlaylist: VideoStreamingPlaylist
|
|
|
|
}) {
|
|
|
|
const { server, baseUrlSegment, videoUUID, segmentName, hlsPlaylist } = options
|
2021-07-16 09:04:35 +02:00
|
|
|
const command = server.streamingPlaylists
|
2021-07-09 10:21:10 +02:00
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
const segmentBody = await command.getFragmentedSegment({ url: `${baseUrlSegment}/${videoUUID}/${segmentName}` })
|
2021-07-09 10:21:10 +02:00
|
|
|
const shaBody = await command.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url })
|
|
|
|
|
|
|
|
expect(sha256(segmentBody)).to.equal(shaBody[segmentName])
|
|
|
|
}
|
|
|
|
|
|
|
|
async function checkResolutionsInMasterPlaylist (options: {
|
2021-07-16 09:47:51 +02:00
|
|
|
server: PeerTubeServer
|
2021-07-09 10:21:10 +02:00
|
|
|
playlistUrl: string
|
|
|
|
resolutions: number[]
|
2022-10-04 10:03:17 +02:00
|
|
|
transcoded?: boolean // default true
|
2022-10-11 14:20:19 +02:00
|
|
|
withRetry?: boolean // default false
|
2021-07-09 10:21:10 +02:00
|
|
|
}) {
|
2022-10-11 14:20:19 +02:00
|
|
|
const { server, playlistUrl, resolutions, withRetry = false, transcoded = true } = options
|
2021-07-09 10:21:10 +02:00
|
|
|
|
2022-10-11 14:20:19 +02:00
|
|
|
const masterPlaylist = await server.streamingPlaylists.get({ url: playlistUrl, withRetry })
|
2021-07-09 10:21:10 +02:00
|
|
|
|
|
|
|
for (const resolution of resolutions) {
|
2022-10-04 10:03:17 +02:00
|
|
|
const reg = transcoded
|
|
|
|
? new RegExp('#EXT-X-STREAM-INF:BANDWIDTH=\\d+,RESOLUTION=\\d+x' + resolution + ',(FRAME-RATE=\\d+,)?CODECS="avc1.64001f,mp4a.40.2"')
|
|
|
|
: new RegExp('#EXT-X-STREAM-INF:BANDWIDTH=\\d+,RESOLUTION=\\d+x' + resolution + '')
|
2021-07-09 10:21:10 +02:00
|
|
|
|
|
|
|
expect(masterPlaylist).to.match(reg)
|
|
|
|
}
|
2022-08-05 10:36:19 +02:00
|
|
|
|
|
|
|
const playlistsLength = masterPlaylist.split('\n').filter(line => line.startsWith('#EXT-X-STREAM-INF:BANDWIDTH='))
|
|
|
|
expect(playlistsLength).to.have.lengthOf(resolutions.length)
|
2021-07-09 10:21:10 +02:00
|
|
|
}
|
|
|
|
|
2022-10-12 16:09:02 +02:00
|
|
|
async function completeCheckHlsPlaylist (options: {
|
|
|
|
servers: PeerTubeServer[]
|
|
|
|
videoUUID: string
|
|
|
|
hlsOnly: boolean
|
|
|
|
|
|
|
|
resolutions?: number[]
|
|
|
|
objectStorageBaseUrl: string
|
|
|
|
}) {
|
|
|
|
const { videoUUID, hlsOnly, objectStorageBaseUrl } = options
|
|
|
|
|
|
|
|
const resolutions = options.resolutions ?? [ 240, 360, 480, 720 ]
|
|
|
|
|
|
|
|
for (const server of options.servers) {
|
|
|
|
const videoDetails = await server.videos.get({ id: videoUUID })
|
|
|
|
const baseUrl = `http://${videoDetails.account.host}`
|
|
|
|
|
|
|
|
expect(videoDetails.streamingPlaylists).to.have.lengthOf(1)
|
|
|
|
|
|
|
|
const hlsPlaylist = videoDetails.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS)
|
|
|
|
expect(hlsPlaylist).to.not.be.undefined
|
|
|
|
|
|
|
|
const hlsFiles = hlsPlaylist.files
|
|
|
|
expect(hlsFiles).to.have.lengthOf(resolutions.length)
|
|
|
|
|
|
|
|
if (hlsOnly) expect(videoDetails.files).to.have.lengthOf(0)
|
|
|
|
else expect(videoDetails.files).to.have.lengthOf(resolutions.length)
|
|
|
|
|
|
|
|
// Check JSON files
|
|
|
|
for (const resolution of resolutions) {
|
|
|
|
const file = hlsFiles.find(f => f.resolution.id === resolution)
|
|
|
|
expect(file).to.not.be.undefined
|
|
|
|
|
|
|
|
expect(file.magnetUri).to.have.lengthOf.above(2)
|
|
|
|
expect(file.torrentUrl).to.match(
|
|
|
|
new RegExp(`http://${server.host}/lazy-static/torrents/${uuidRegex}-${file.resolution.id}-hls.torrent`)
|
|
|
|
)
|
|
|
|
|
|
|
|
if (objectStorageBaseUrl) {
|
|
|
|
expectStartWith(file.fileUrl, objectStorageBaseUrl)
|
|
|
|
} else {
|
|
|
|
expect(file.fileUrl).to.match(
|
|
|
|
new RegExp(`${baseUrl}/static/streaming-playlists/hls/${videoDetails.uuid}/${uuidRegex}-${file.resolution.id}-fragmented.mp4`)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(file.resolution.label).to.equal(resolution + 'p')
|
|
|
|
|
|
|
|
await makeRawRequest({ url: file.torrentUrl, expectedStatus: HttpStatusCode.OK_200 })
|
|
|
|
await makeRawRequest({ url: file.fileUrl, expectedStatus: HttpStatusCode.OK_200 })
|
|
|
|
|
|
|
|
const torrent = await webtorrentAdd(file.magnetUri, true)
|
|
|
|
expect(torrent.files).to.be.an('array')
|
|
|
|
expect(torrent.files.length).to.equal(1)
|
|
|
|
expect(torrent.files[0].path).to.exist.and.to.not.equal('')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check master playlist
|
|
|
|
{
|
|
|
|
await checkResolutionsInMasterPlaylist({ server, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
|
|
|
|
|
|
|
|
const masterPlaylist = await server.streamingPlaylists.get({ url: hlsPlaylist.playlistUrl })
|
|
|
|
|
|
|
|
let i = 0
|
|
|
|
for (const resolution of resolutions) {
|
|
|
|
expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
|
|
|
|
expect(masterPlaylist).to.contain(`${resolution}.m3u8`)
|
|
|
|
|
|
|
|
const url = 'http://' + videoDetails.account.host
|
|
|
|
await hlsInfohashExist(url, hlsPlaylist.playlistUrl, i)
|
|
|
|
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check resolution playlists
|
|
|
|
{
|
|
|
|
for (const resolution of resolutions) {
|
|
|
|
const file = hlsFiles.find(f => f.resolution.id === resolution)
|
|
|
|
const playlistName = removeFragmentedMP4Ext(basename(file.fileUrl)) + '.m3u8'
|
|
|
|
|
|
|
|
const url = objectStorageBaseUrl
|
|
|
|
? `${objectStorageBaseUrl}hls/${videoUUID}/${playlistName}`
|
|
|
|
: `${baseUrl}/static/streaming-playlists/hls/${videoUUID}/${playlistName}`
|
|
|
|
|
|
|
|
const subPlaylist = await server.streamingPlaylists.get({ url })
|
|
|
|
|
|
|
|
expect(subPlaylist).to.match(new RegExp(`${uuidRegex}-${resolution}-fragmented.mp4`))
|
|
|
|
expect(subPlaylist).to.contain(basename(file.fileUrl))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const baseUrlAndPath = objectStorageBaseUrl
|
|
|
|
? objectStorageBaseUrl + 'hls/' + videoUUID
|
|
|
|
: baseUrl + '/static/streaming-playlists/hls/' + videoUUID
|
|
|
|
|
|
|
|
for (const resolution of resolutions) {
|
|
|
|
await checkSegmentHash({
|
|
|
|
server,
|
|
|
|
baseUrlPlaylist: baseUrlAndPath,
|
|
|
|
baseUrlSegment: baseUrlAndPath,
|
|
|
|
resolution,
|
|
|
|
hlsPlaylist
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-09 10:21:10 +02:00
|
|
|
export {
|
|
|
|
checkSegmentHash,
|
|
|
|
checkLiveSegmentHash,
|
2022-10-12 16:09:02 +02:00
|
|
|
checkResolutionsInMasterPlaylist,
|
|
|
|
completeCheckHlsPlaylist
|
2021-07-09 10:21:10 +02:00
|
|
|
}
|