1 import { makeRawRequest } from '../requests/requests'
2 import { sha256 } from '../../../server/helpers/core-utils'
3 import { VideoStreamingPlaylist } from '../../models/videos/video-streaming-playlist.model'
4 import { expect } from 'chai'
5 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
7 function getPlaylist (url: string, statusCodeExpected = HttpStatusCode.OK_200) {
8 return makeRawRequest(url, statusCodeExpected)
11 function getSegment (url: string, statusCodeExpected = HttpStatusCode.OK_200, range?: string) {
12 return makeRawRequest(url, statusCodeExpected, range)
15 function getSegmentSha256 (url: string, statusCodeExpected = HttpStatusCode.OK_200) {
16 return makeRawRequest(url, statusCodeExpected)
19 async function checkSegmentHash (
20 baseUrlPlaylist: string,
21 baseUrlSegment: string,
24 hlsPlaylist: VideoStreamingPlaylist
26 const res = await getPlaylist(`${baseUrlPlaylist}/${videoUUID}/${resolution}.m3u8`)
27 const playlist = res.text
29 const videoName = `${videoUUID}-${resolution}-fragmented.mp4`
31 const matches = /#EXT-X-BYTERANGE:(\d+)@(\d+)/.exec(playlist)
33 const length = parseInt(matches[1], 10)
34 const offset = parseInt(matches[2], 10)
35 const range = `${offset}-${offset + length - 1}`
37 const res2 = await getSegment(`${baseUrlSegment}/${videoUUID}/${videoName}`, HttpStatusCode.PARTIAL_CONTENT_206, `bytes=${range}`)
39 const resSha = await getSegmentSha256(hlsPlaylist.segmentsSha256Url)
41 const sha256Server = resSha.body[videoName][range]
42 expect(sha256(res2.body)).to.equal(sha256Server)
45 async function checkLiveSegmentHash (
46 baseUrlSegment: string,
49 hlsPlaylist: VideoStreamingPlaylist
51 const res2 = await getSegment(`${baseUrlSegment}/${videoUUID}/${segmentName}`)
53 const resSha = await getSegmentSha256(hlsPlaylist.segmentsSha256Url)
55 const sha256Server = resSha.body[segmentName]
56 expect(sha256(res2.body)).to.equal(sha256Server)
59 async function checkResolutionsInMasterPlaylist (playlistUrl: string, resolutions: number[]) {
60 const res = await getPlaylist(playlistUrl)
62 const masterPlaylist = res.text
64 for (const resolution of resolutions) {
65 const reg = new RegExp(
66 '#EXT-X-STREAM-INF:BANDWIDTH=\\d+,RESOLUTION=\\d+x' + resolution + ',(FRAME-RATE=\\d+,)?CODECS="avc1.64001f,mp4a.40.2"'
69 expect(masterPlaylist).to.match(reg)
73 // ---------------------------------------------------------------------------
78 checkResolutionsInMasterPlaylist,