1 import { expect } from 'chai'
2 import { sha256 } from '@server/helpers/core-utils'
3 import { HttpStatusCode } from '@shared/core-utils'
4 import { VideoStreamingPlaylist } from '@shared/models'
5 import { ServerInfo } from '../server'
7 async function checkSegmentHash (options: {
9 baseUrlPlaylist: string
10 baseUrlSegment: string
13 hlsPlaylist: VideoStreamingPlaylist
15 const { server, baseUrlPlaylist, baseUrlSegment, videoUUID, resolution, hlsPlaylist } = options
16 const command = server.streamingPlaylistsCommand
18 const playlist = await command.get({ url: `${baseUrlPlaylist}/${videoUUID}/${resolution}.m3u8` })
20 const videoName = `${videoUUID}-${resolution}-fragmented.mp4`
22 const matches = /#EXT-X-BYTERANGE:(\d+)@(\d+)/.exec(playlist)
24 const length = parseInt(matches[1], 10)
25 const offset = parseInt(matches[2], 10)
26 const range = `${offset}-${offset + length - 1}`
28 const segmentBody = await command.getSegment({
29 url: `${baseUrlSegment}/${videoUUID}/${videoName}`,
30 expectedStatus: HttpStatusCode.PARTIAL_CONTENT_206,
31 range: `bytes=${range}`
34 const shaBody = await command.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url })
35 expect(sha256(segmentBody)).to.equal(shaBody[videoName][range])
38 async function checkLiveSegmentHash (options: {
40 baseUrlSegment: string
43 hlsPlaylist: VideoStreamingPlaylist
45 const { server, baseUrlSegment, videoUUID, segmentName, hlsPlaylist } = options
46 const command = server.streamingPlaylistsCommand
48 const segmentBody = await command.getSegment({ url: `${baseUrlSegment}/${videoUUID}/${segmentName}` })
49 const shaBody = await command.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url })
51 expect(sha256(segmentBody)).to.equal(shaBody[segmentName])
54 async function checkResolutionsInMasterPlaylist (options: {
59 const { server, playlistUrl, resolutions } = options
61 const masterPlaylist = await server.streamingPlaylistsCommand.get({ url: playlistUrl })
63 for (const resolution of resolutions) {
64 const reg = new RegExp(
65 '#EXT-X-STREAM-INF:BANDWIDTH=\\d+,RESOLUTION=\\d+x' + resolution + ',(FRAME-RATE=\\d+,)?CODECS="avc1.64001f,mp4a.40.2"'
68 expect(masterPlaylist).to.match(reg)
75 checkResolutionsInMasterPlaylist