1 import { expect } from 'chai'
2 import { basename } from 'path'
3 import { sha256 } from '@server/helpers/core-utils'
4 import { removeFragmentedMP4Ext } from '@shared/core-utils'
5 import { HttpStatusCode, VideoStreamingPlaylist } from '@shared/models'
6 import { PeerTubeServer } from '../server'
8 async function checkSegmentHash (options: {
10 baseUrlPlaylist: string
11 baseUrlSegment: string
14 hlsPlaylist: VideoStreamingPlaylist
16 const { server, baseUrlPlaylist, baseUrlSegment, videoUUID, resolution, hlsPlaylist } = options
17 const command = server.streamingPlaylists
19 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
20 const videoName = basename(file.fileUrl)
22 const playlist = await command.get({ url: `${baseUrlPlaylist}/${videoUUID}/${removeFragmentedMP4Ext(videoName)}.m3u8` })
24 const matches = /#EXT-X-BYTERANGE:(\d+)@(\d+)/.exec(playlist)
26 const length = parseInt(matches[1], 10)
27 const offset = parseInt(matches[2], 10)
28 const range = `${offset}-${offset + length - 1}`
30 const segmentBody = await command.getSegment({
31 url: `${baseUrlSegment}/${videoUUID}/${videoName}`,
32 expectedStatus: HttpStatusCode.PARTIAL_CONTENT_206,
33 range: `bytes=${range}`
36 const shaBody = await command.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url })
37 expect(sha256(segmentBody)).to.equal(shaBody[videoName][range])
40 async function checkLiveSegmentHash (options: {
41 server: PeerTubeServer
42 baseUrlSegment: string
45 hlsPlaylist: VideoStreamingPlaylist
47 const { server, baseUrlSegment, videoUUID, segmentName, hlsPlaylist } = options
48 const command = server.streamingPlaylists
50 const segmentBody = await command.getSegment({ url: `${baseUrlSegment}/${videoUUID}/${segmentName}` })
51 const shaBody = await command.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url })
53 expect(sha256(segmentBody)).to.equal(shaBody[segmentName])
56 async function checkResolutionsInMasterPlaylist (options: {
57 server: PeerTubeServer
61 const { server, playlistUrl, resolutions } = options
63 const masterPlaylist = await server.streamingPlaylists.get({ url: playlistUrl })
65 for (const resolution of resolutions) {
66 const reg = new RegExp(
67 '#EXT-X-STREAM-INF:BANDWIDTH=\\d+,RESOLUTION=\\d+x' + resolution + ',(FRAME-RATE=\\d+,)?CODECS="avc1.64001f,mp4a.40.2"'
70 expect(masterPlaylist).to.match(reg)
77 checkResolutionsInMasterPlaylist