2021-07-09 10:21:10 +02:00
|
|
|
import { expect } from 'chai'
|
2021-07-22 14:28:03 +02:00
|
|
|
import { basename } from 'path'
|
2021-07-23 11:20:00 +02:00
|
|
|
import { removeFragmentedMP4Ext } from '@shared/core-utils'
|
2021-12-17 13:58:07 +01:00
|
|
|
import { sha256 } from '@shared/extra-utils'
|
2021-07-16 14:27:30 +02:00
|
|
|
import { HttpStatusCode, VideoStreamingPlaylist } from '@shared/models'
|
2021-12-17 11:58:15 +01:00
|
|
|
import { PeerTubeServer } from '@shared/server-commands'
|
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}`
|
|
|
|
|
|
|
|
const segmentBody = await command.getSegment({
|
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
|
|
|
|
|
|
|
const segmentBody = await command.getSegment({ url: `${baseUrlSegment}/${videoUUID}/${segmentName}` })
|
|
|
|
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[]
|
|
|
|
}) {
|
|
|
|
const { server, playlistUrl, resolutions } = options
|
|
|
|
|
2021-07-16 09:04:35 +02:00
|
|
|
const masterPlaylist = await server.streamingPlaylists.get({ url: playlistUrl })
|
2021-07-09 10:21:10 +02:00
|
|
|
|
|
|
|
for (const resolution of resolutions) {
|
|
|
|
const reg = new RegExp(
|
|
|
|
'#EXT-X-STREAM-INF:BANDWIDTH=\\d+,RESOLUTION=\\d+x' + resolution + ',(FRAME-RATE=\\d+,)?CODECS="avc1.64001f,mp4a.40.2"'
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
checkSegmentHash,
|
|
|
|
checkLiveSegmentHash,
|
|
|
|
checkResolutionsInMasterPlaylist
|
|
|
|
}
|