2022-10-12 09:09:02 -05:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
|
|
|
|
|
2021-07-09 03:21:10 -05:00
|
|
|
import { expect } from 'chai'
|
2022-12-02 07:47:21 -06:00
|
|
|
import { basename, dirname, join } from 'path'
|
2022-10-12 09:09:02 -05:00
|
|
|
import { removeFragmentedMP4Ext, uuidRegex } from '@shared/core-utils'
|
2021-12-17 06:58:07 -06:00
|
|
|
import { sha256 } from '@shared/extra-utils'
|
2023-04-21 08:00:01 -05:00
|
|
|
import { HttpStatusCode, VideoPrivacy, VideoResolution, VideoStreamingPlaylist, VideoStreamingPlaylistType } from '@shared/models'
|
|
|
|
import { makeRawRequest, PeerTubeServer } from '@shared/server-commands'
|
2022-10-12 09:09:02 -05:00
|
|
|
import { expectStartWith } from './checks'
|
|
|
|
import { hlsInfohashExist } from './tracker'
|
2023-04-21 08:00:01 -05:00
|
|
|
import { checkWebTorrentWorks } from './webtorrent'
|
2021-07-09 03:21:10 -05:00
|
|
|
|
|
|
|
async function checkSegmentHash (options: {
|
2021-07-16 02:47:51 -05:00
|
|
|
server: PeerTubeServer
|
2021-07-09 03:21:10 -05:00
|
|
|
baseUrlPlaylist: string
|
|
|
|
baseUrlSegment: string
|
|
|
|
resolution: number
|
|
|
|
hlsPlaylist: VideoStreamingPlaylist
|
2023-04-21 08:00:01 -05:00
|
|
|
token?: string
|
2021-07-09 03:21:10 -05:00
|
|
|
}) {
|
2023-04-21 08:00:01 -05:00
|
|
|
const { server, baseUrlPlaylist, baseUrlSegment, resolution, hlsPlaylist, token } = options
|
2021-07-16 02:04:35 -05:00
|
|
|
const command = server.streamingPlaylists
|
2021-07-09 03:21:10 -05:00
|
|
|
|
2021-07-22 07:28:03 -05:00
|
|
|
const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
|
|
|
|
const videoName = basename(file.fileUrl)
|
2021-07-09 03:21:10 -05:00
|
|
|
|
2023-04-21 08:00:01 -05:00
|
|
|
const playlist = await command.get({ url: `${baseUrlPlaylist}/${removeFragmentedMP4Ext(videoName)}.m3u8`, token })
|
2021-07-23 04:20:00 -05:00
|
|
|
|
2021-07-09 03:21:10 -05: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 03:03:17 -05:00
|
|
|
const segmentBody = await command.getFragmentedSegment({
|
2021-08-17 01:26:20 -05:00
|
|
|
url: `${baseUrlSegment}/${videoName}`,
|
2021-07-09 03:21:10 -05:00
|
|
|
expectedStatus: HttpStatusCode.PARTIAL_CONTENT_206,
|
2023-04-21 08:00:01 -05:00
|
|
|
range: `bytes=${range}`,
|
|
|
|
token
|
2021-07-09 03:21:10 -05:00
|
|
|
})
|
|
|
|
|
2023-04-21 08:00:01 -05:00
|
|
|
const shaBody = await command.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url, token })
|
|
|
|
expect(sha256(segmentBody)).to.equal(shaBody[videoName][range], `Invalid sha256 result for ${videoName} range ${range}`)
|
2021-07-09 03:21:10 -05:00
|
|
|
}
|
|
|
|
|
2022-12-30 08:05:14 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2021-07-09 03:21:10 -05:00
|
|
|
async function checkLiveSegmentHash (options: {
|
2021-07-16 02:47:51 -05:00
|
|
|
server: PeerTubeServer
|
2021-07-09 03:21:10 -05:00
|
|
|
baseUrlSegment: string
|
|
|
|
videoUUID: string
|
|
|
|
segmentName: string
|
|
|
|
hlsPlaylist: VideoStreamingPlaylist
|
2023-05-10 02:28:42 -05:00
|
|
|
withRetry?: boolean
|
2021-07-09 03:21:10 -05:00
|
|
|
}) {
|
2023-05-10 02:28:42 -05:00
|
|
|
const { server, baseUrlSegment, videoUUID, segmentName, hlsPlaylist, withRetry = false } = options
|
2021-07-16 02:04:35 -05:00
|
|
|
const command = server.streamingPlaylists
|
2021-07-09 03:21:10 -05:00
|
|
|
|
2023-05-10 02:28:42 -05:00
|
|
|
const segmentBody = await command.getFragmentedSegment({ url: `${baseUrlSegment}/${videoUUID}/${segmentName}`, withRetry })
|
|
|
|
const shaBody = await command.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url, withRetry })
|
2021-07-09 03:21:10 -05:00
|
|
|
|
|
|
|
expect(sha256(segmentBody)).to.equal(shaBody[segmentName])
|
|
|
|
}
|
|
|
|
|
2022-12-30 08:05:14 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2021-07-09 03:21:10 -05:00
|
|
|
async function checkResolutionsInMasterPlaylist (options: {
|
2021-07-16 02:47:51 -05:00
|
|
|
server: PeerTubeServer
|
2021-07-09 03:21:10 -05:00
|
|
|
playlistUrl: string
|
|
|
|
resolutions: number[]
|
2023-04-21 08:00:01 -05:00
|
|
|
token?: string
|
2022-10-04 03:03:17 -05:00
|
|
|
transcoded?: boolean // default true
|
2022-10-11 07:20:19 -05:00
|
|
|
withRetry?: boolean // default false
|
2021-07-09 03:21:10 -05:00
|
|
|
}) {
|
2023-04-21 08:00:01 -05:00
|
|
|
const { server, playlistUrl, resolutions, token, withRetry = false, transcoded = true } = options
|
2021-07-09 03:21:10 -05:00
|
|
|
|
2023-04-21 08:00:01 -05:00
|
|
|
const masterPlaylist = await server.streamingPlaylists.get({ url: playlistUrl, token, withRetry })
|
2021-07-09 03:21:10 -05:00
|
|
|
|
|
|
|
for (const resolution of resolutions) {
|
2023-04-21 08:00:01 -05:00
|
|
|
const base = '#EXT-X-STREAM-INF:BANDWIDTH=\\d+,RESOLUTION=\\d+x' + resolution
|
|
|
|
|
|
|
|
if (resolution === VideoResolution.H_NOVIDEO) {
|
|
|
|
expect(masterPlaylist).to.match(new RegExp(`${base},CODECS="mp4a.40.2"`))
|
|
|
|
} else if (transcoded) {
|
|
|
|
expect(masterPlaylist).to.match(new RegExp(`${base},(FRAME-RATE=\\d+,)?CODECS="avc1.64001f,mp4a.40.2"`))
|
|
|
|
} else {
|
|
|
|
expect(masterPlaylist).to.match(new RegExp(`${base}`))
|
|
|
|
}
|
2021-07-09 03:21:10 -05:00
|
|
|
}
|
2022-08-05 03:36:19 -05: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 03:21:10 -05:00
|
|
|
}
|
|
|
|
|
2022-10-12 09:09:02 -05:00
|
|
|
async function completeCheckHlsPlaylist (options: {
|
|
|
|
servers: PeerTubeServer[]
|
|
|
|
videoUUID: string
|
|
|
|
hlsOnly: boolean
|
|
|
|
|
|
|
|
resolutions?: number[]
|
2023-04-21 08:00:01 -05:00
|
|
|
objectStorageBaseUrl?: string
|
2022-10-12 09:09:02 -05:00
|
|
|
}) {
|
|
|
|
const { videoUUID, hlsOnly, objectStorageBaseUrl } = options
|
|
|
|
|
|
|
|
const resolutions = options.resolutions ?? [ 240, 360, 480, 720 ]
|
|
|
|
|
|
|
|
for (const server of options.servers) {
|
2023-04-21 08:00:01 -05:00
|
|
|
const videoDetails = await server.videos.getWithToken({ id: videoUUID })
|
|
|
|
const requiresAuth = videoDetails.privacy.id === VideoPrivacy.PRIVATE || videoDetails.privacy.id === VideoPrivacy.INTERNAL
|
|
|
|
|
|
|
|
const privatePath = requiresAuth
|
|
|
|
? 'private/'
|
|
|
|
: ''
|
|
|
|
const token = requiresAuth
|
|
|
|
? server.accessToken
|
|
|
|
: undefined
|
|
|
|
|
2022-10-12 09:09:02 -05:00
|
|
|
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
|
|
|
|
|
2023-04-21 08:00:01 -05:00
|
|
|
if (file.resolution.id === VideoResolution.H_NOVIDEO) {
|
|
|
|
expect(file.resolution.label).to.equal('Audio')
|
2022-10-12 09:09:02 -05:00
|
|
|
} else {
|
2023-04-21 08:00:01 -05:00
|
|
|
expect(file.resolution.label).to.equal(resolution + 'p')
|
2022-10-12 09:09:02 -05:00
|
|
|
}
|
|
|
|
|
2023-04-21 08:00:01 -05:00
|
|
|
expect(file.magnetUri).to.have.lengthOf.above(2)
|
|
|
|
await checkWebTorrentWorks(file.magnetUri)
|
|
|
|
|
|
|
|
{
|
|
|
|
const nameReg = `${uuidRegex}-${file.resolution.id}`
|
|
|
|
|
|
|
|
expect(file.torrentUrl).to.match(new RegExp(`${server.url}/lazy-static/torrents/${nameReg}-hls.torrent`))
|
|
|
|
|
|
|
|
if (objectStorageBaseUrl && requiresAuth) {
|
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
expect(file.fileUrl).to.match(new RegExp(`${server.url}/object-storage-proxy/streaming-playlists/hls/${privatePath}${videoDetails.uuid}/${nameReg}-fragmented.mp4`))
|
|
|
|
} else if (objectStorageBaseUrl) {
|
|
|
|
expectStartWith(file.fileUrl, objectStorageBaseUrl)
|
|
|
|
} else {
|
|
|
|
expect(file.fileUrl).to.match(
|
|
|
|
new RegExp(`${baseUrl}/static/streaming-playlists/hls/${privatePath}${videoDetails.uuid}/${nameReg}-fragmented.mp4`)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2022-10-12 09:09:02 -05:00
|
|
|
|
2023-04-21 08:00:01 -05:00
|
|
|
{
|
|
|
|
await Promise.all([
|
|
|
|
makeRawRequest({ url: file.torrentUrl, token, expectedStatus: HttpStatusCode.OK_200 }),
|
|
|
|
makeRawRequest({ url: file.torrentDownloadUrl, token, expectedStatus: HttpStatusCode.OK_200 }),
|
|
|
|
makeRawRequest({ url: file.metadataUrl, token, expectedStatus: HttpStatusCode.OK_200 }),
|
|
|
|
makeRawRequest({ url: file.fileUrl, token, expectedStatus: HttpStatusCode.OK_200 }),
|
|
|
|
|
|
|
|
makeRawRequest({
|
|
|
|
url: file.fileDownloadUrl,
|
|
|
|
token,
|
|
|
|
expectedStatus: objectStorageBaseUrl
|
|
|
|
? HttpStatusCode.FOUND_302
|
|
|
|
: HttpStatusCode.OK_200
|
|
|
|
})
|
|
|
|
])
|
|
|
|
}
|
2022-10-12 09:09:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check master playlist
|
|
|
|
{
|
2023-04-21 08:00:01 -05:00
|
|
|
await checkResolutionsInMasterPlaylist({ server, token, playlistUrl: hlsPlaylist.playlistUrl, resolutions })
|
2022-10-12 09:09:02 -05:00
|
|
|
|
2023-04-21 08:00:01 -05:00
|
|
|
const masterPlaylist = await server.streamingPlaylists.get({ url: hlsPlaylist.playlistUrl, token })
|
2022-10-12 09:09:02 -05:00
|
|
|
|
|
|
|
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'
|
|
|
|
|
2023-04-21 08:00:01 -05:00
|
|
|
let url: string
|
|
|
|
if (objectStorageBaseUrl && requiresAuth) {
|
|
|
|
url = `${baseUrl}/object-storage-proxy/streaming-playlists/hls/${privatePath}${videoUUID}/${playlistName}`
|
|
|
|
} else if (objectStorageBaseUrl) {
|
|
|
|
url = `${objectStorageBaseUrl}hls/${videoUUID}/${playlistName}`
|
|
|
|
} else {
|
|
|
|
url = `${baseUrl}/static/streaming-playlists/hls/${privatePath}${videoUUID}/${playlistName}`
|
|
|
|
}
|
2022-10-12 09:09:02 -05:00
|
|
|
|
2023-04-21 08:00:01 -05:00
|
|
|
const subPlaylist = await server.streamingPlaylists.get({ url, token })
|
2022-10-12 09:09:02 -05:00
|
|
|
|
|
|
|
expect(subPlaylist).to.match(new RegExp(`${uuidRegex}-${resolution}-fragmented.mp4`))
|
|
|
|
expect(subPlaylist).to.contain(basename(file.fileUrl))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2023-04-21 08:00:01 -05:00
|
|
|
let baseUrlAndPath: string
|
|
|
|
if (objectStorageBaseUrl && requiresAuth) {
|
|
|
|
baseUrlAndPath = `${baseUrl}/object-storage-proxy/streaming-playlists/hls/${privatePath}${videoUUID}`
|
|
|
|
} else if (objectStorageBaseUrl) {
|
|
|
|
baseUrlAndPath = `${objectStorageBaseUrl}hls/${videoUUID}`
|
|
|
|
} else {
|
|
|
|
baseUrlAndPath = `${baseUrl}/static/streaming-playlists/hls/${privatePath}${videoUUID}`
|
|
|
|
}
|
2022-10-12 09:09:02 -05:00
|
|
|
|
|
|
|
for (const resolution of resolutions) {
|
|
|
|
await checkSegmentHash({
|
|
|
|
server,
|
2023-04-21 08:00:01 -05:00
|
|
|
token,
|
2022-10-12 09:09:02 -05:00
|
|
|
baseUrlPlaylist: baseUrlAndPath,
|
|
|
|
baseUrlSegment: baseUrlAndPath,
|
|
|
|
resolution,
|
|
|
|
hlsPlaylist
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-02 07:47:21 -06:00
|
|
|
async function checkVideoFileTokenReinjection (options: {
|
|
|
|
server: PeerTubeServer
|
|
|
|
videoUUID: string
|
|
|
|
videoFileToken: string
|
|
|
|
resolutions: number[]
|
|
|
|
isLive: boolean
|
|
|
|
}) {
|
|
|
|
const { server, resolutions, videoFileToken, videoUUID, isLive } = options
|
|
|
|
|
|
|
|
const video = await server.videos.getWithToken({ id: videoUUID })
|
|
|
|
const hls = video.streamingPlaylists[0]
|
|
|
|
|
|
|
|
const query = { videoFileToken, reinjectVideoFileToken: 'true' }
|
|
|
|
const { text } = await makeRawRequest({ url: hls.playlistUrl, query, expectedStatus: HttpStatusCode.OK_200 })
|
|
|
|
|
|
|
|
for (let i = 0; i < resolutions.length; i++) {
|
|
|
|
const resolution = resolutions[i]
|
|
|
|
|
|
|
|
const suffix = isLive
|
|
|
|
? i
|
|
|
|
: `-${resolution}`
|
|
|
|
|
2023-03-07 03:52:20 -06:00
|
|
|
expect(text).to.contain(`${suffix}.m3u8?videoFileToken=${videoFileToken}&reinjectVideoFileToken=true`)
|
2022-12-02 07:47:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const resolutionPlaylists = extractResolutionPlaylistUrls(hls.playlistUrl, text)
|
|
|
|
expect(resolutionPlaylists).to.have.lengthOf(resolutions.length)
|
|
|
|
|
|
|
|
for (const url of resolutionPlaylists) {
|
|
|
|
const { text } = await makeRawRequest({ url, query, expectedStatus: HttpStatusCode.OK_200 })
|
|
|
|
|
|
|
|
const extension = isLive
|
|
|
|
? '.ts'
|
|
|
|
: '.mp4'
|
|
|
|
|
|
|
|
expect(text).to.contain(`${extension}?videoFileToken=${videoFileToken}`)
|
2023-03-07 03:52:20 -06:00
|
|
|
expect(text).not.to.contain(`reinjectVideoFileToken=true`)
|
2022-12-02 07:47:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function extractResolutionPlaylistUrls (masterPath: string, masterContent: string) {
|
|
|
|
return masterContent.match(/^([^.]+\.m3u8.*)/mg)
|
|
|
|
.map(filename => join(dirname(masterPath), filename))
|
|
|
|
}
|
|
|
|
|
2021-07-09 03:21:10 -05:00
|
|
|
export {
|
|
|
|
checkSegmentHash,
|
|
|
|
checkLiveSegmentHash,
|
2022-10-12 09:09:02 -05:00
|
|
|
checkResolutionsInMasterPlaylist,
|
2022-12-02 07:47:21 -06:00
|
|
|
completeCheckHlsPlaylist,
|
|
|
|
extractResolutionPlaylistUrls,
|
|
|
|
checkVideoFileTokenReinjection
|
2021-07-09 03:21:10 -05:00
|
|
|
}
|