PeerTube/shared/extra-utils/videos/streaming-playlists.ts

77 lines
2.4 KiB
TypeScript
Raw Normal View History

2021-07-09 03:21:10 -05:00
import { expect } from 'chai'
import { sha256 } from '@server/helpers/core-utils'
2021-07-16 03:42:24 -05:00
import { HttpStatusCode } from '@shared/models'
2021-07-09 03:21:10 -05:00
import { VideoStreamingPlaylist } from '@shared/models'
2021-07-16 02:47:51 -05:00
import { PeerTubeServer } from '../server'
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
videoUUID: string
resolution: number
hlsPlaylist: VideoStreamingPlaylist
}) {
const { server, baseUrlPlaylist, baseUrlSegment, videoUUID, resolution, hlsPlaylist } = options
2021-07-16 02:04:35 -05:00
const command = server.streamingPlaylists
2021-07-09 03:21:10 -05:00
const playlist = await command.get({ url: `${baseUrlPlaylist}/${videoUUID}/${resolution}.m3u8` })
const videoName = `${videoUUID}-${resolution}-fragmented.mp4`
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({
url: `${baseUrlSegment}/${videoUUID}/${videoName}`,
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 02:47:51 -05:00
server: PeerTubeServer
2021-07-09 03:21:10 -05:00
baseUrlSegment: string
videoUUID: string
segmentName: string
hlsPlaylist: VideoStreamingPlaylist
}) {
const { server, baseUrlSegment, videoUUID, segmentName, hlsPlaylist } = options
2021-07-16 02:04:35 -05:00
const command = server.streamingPlaylists
2021-07-09 03:21:10 -05: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 02:47:51 -05:00
server: PeerTubeServer
2021-07-09 03:21:10 -05:00
playlistUrl: string
resolutions: number[]
}) {
const { server, playlistUrl, resolutions } = options
2021-07-16 02:04:35 -05:00
const masterPlaylist = await server.streamingPlaylists.get({ url: playlistUrl })
2021-07-09 03:21:10 -05: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)
}
}
export {
checkSegmentHash,
checkLiveSegmentHash,
checkResolutionsInMasterPlaylist
}