2019-02-26 10:55:40 +01:00
|
|
|
import { makeRawRequest } from '../requests/requests'
|
|
|
|
import { sha256 } from '../../../server/helpers/core-utils'
|
|
|
|
import { VideoStreamingPlaylist } from '../../models/videos/video-streaming-playlist.model'
|
|
|
|
import { expect } from 'chai'
|
2020-12-07 14:32:36 +01:00
|
|
|
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
|
2019-02-26 10:55:40 +01:00
|
|
|
|
2020-12-07 14:32:36 +01:00
|
|
|
function getPlaylist (url: string, statusCodeExpected = HttpStatusCode.OK_200) {
|
2019-02-26 10:55:40 +01:00
|
|
|
return makeRawRequest(url, statusCodeExpected)
|
|
|
|
}
|
|
|
|
|
2020-12-07 14:32:36 +01:00
|
|
|
function getSegment (url: string, statusCodeExpected = HttpStatusCode.OK_200, range?: string) {
|
2019-02-26 10:55:40 +01:00
|
|
|
return makeRawRequest(url, statusCodeExpected, range)
|
|
|
|
}
|
|
|
|
|
2020-12-07 14:32:36 +01:00
|
|
|
function getSegmentSha256 (url: string, statusCodeExpected = HttpStatusCode.OK_200) {
|
2019-02-26 10:55:40 +01:00
|
|
|
return makeRawRequest(url, statusCodeExpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function checkSegmentHash (
|
|
|
|
baseUrlPlaylist: string,
|
|
|
|
baseUrlSegment: string,
|
|
|
|
videoUUID: string,
|
|
|
|
resolution: number,
|
|
|
|
hlsPlaylist: VideoStreamingPlaylist
|
|
|
|
) {
|
|
|
|
const res = await getPlaylist(`${baseUrlPlaylist}/${videoUUID}/${resolution}.m3u8`)
|
|
|
|
const playlist = res.text
|
|
|
|
|
|
|
|
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}`
|
|
|
|
|
2020-12-07 14:32:36 +01:00
|
|
|
const res2 = await getSegment(`${baseUrlSegment}/${videoUUID}/${videoName}`, HttpStatusCode.PARTIAL_CONTENT_206, `bytes=${range}`)
|
2019-02-26 10:55:40 +01:00
|
|
|
|
|
|
|
const resSha = await getSegmentSha256(hlsPlaylist.segmentsSha256Url)
|
|
|
|
|
2020-01-31 16:56:52 +01:00
|
|
|
const sha256Server = resSha.body[videoName][range]
|
2019-02-26 10:55:40 +01:00
|
|
|
expect(sha256(res2.body)).to.equal(sha256Server)
|
|
|
|
}
|
|
|
|
|
2020-11-13 14:36:30 +01:00
|
|
|
async function checkLiveSegmentHash (
|
|
|
|
baseUrlSegment: string,
|
|
|
|
videoUUID: string,
|
|
|
|
segmentName: string,
|
|
|
|
hlsPlaylist: VideoStreamingPlaylist
|
|
|
|
) {
|
|
|
|
const res2 = await getSegment(`${baseUrlSegment}/${videoUUID}/${segmentName}`)
|
|
|
|
|
|
|
|
const resSha = await getSegmentSha256(hlsPlaylist.segmentsSha256Url)
|
|
|
|
|
|
|
|
const sha256Server = resSha.body[segmentName]
|
|
|
|
expect(sha256(res2.body)).to.equal(sha256Server)
|
|
|
|
}
|
|
|
|
|
2020-11-04 15:31:32 +01:00
|
|
|
async function checkResolutionsInMasterPlaylist (playlistUrl: string, resolutions: number[]) {
|
|
|
|
const res = await getPlaylist(playlistUrl)
|
|
|
|
|
|
|
|
const masterPlaylist = res.text
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 10:55:40 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
getPlaylist,
|
|
|
|
getSegment,
|
2020-11-04 15:31:32 +01:00
|
|
|
checkResolutionsInMasterPlaylist,
|
2019-02-26 10:55:40 +01:00
|
|
|
getSegmentSha256,
|
2020-11-13 14:36:30 +01:00
|
|
|
checkLiveSegmentHash,
|
2019-02-26 10:55:40 +01:00
|
|
|
checkSegmentHash
|
|
|
|
}
|