mirror of https://github.com/Chocobozzz/PeerTube
				
				
				
			
		
			
				
	
	
		
			83 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			TypeScript
		
	
	
			
		
		
	
	
			83 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			TypeScript
		
	
	
import { expect } from 'chai'
 | 
						|
import { basename } from 'path'
 | 
						|
import { removeFragmentedMP4Ext } from '@shared/core-utils'
 | 
						|
import { sha256 } from '@shared/extra-utils'
 | 
						|
import { HttpStatusCode, VideoStreamingPlaylist } from '@shared/models'
 | 
						|
import { PeerTubeServer } from '@shared/server-commands'
 | 
						|
 | 
						|
async function checkSegmentHash (options: {
 | 
						|
  server: PeerTubeServer
 | 
						|
  baseUrlPlaylist: string
 | 
						|
  baseUrlSegment: string
 | 
						|
  resolution: number
 | 
						|
  hlsPlaylist: VideoStreamingPlaylist
 | 
						|
}) {
 | 
						|
  const { server, baseUrlPlaylist, baseUrlSegment, resolution, hlsPlaylist } = options
 | 
						|
  const command = server.streamingPlaylists
 | 
						|
 | 
						|
  const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
 | 
						|
  const videoName = basename(file.fileUrl)
 | 
						|
 | 
						|
  const playlist = await command.get({ url: `${baseUrlPlaylist}/${removeFragmentedMP4Ext(videoName)}.m3u8` })
 | 
						|
 | 
						|
  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.getFragmentedSegment({
 | 
						|
    url: `${baseUrlSegment}/${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: {
 | 
						|
  server: PeerTubeServer
 | 
						|
  baseUrlSegment: string
 | 
						|
  videoUUID: string
 | 
						|
  segmentName: string
 | 
						|
  hlsPlaylist: VideoStreamingPlaylist
 | 
						|
}) {
 | 
						|
  const { server, baseUrlSegment, videoUUID, segmentName, hlsPlaylist } = options
 | 
						|
  const command = server.streamingPlaylists
 | 
						|
 | 
						|
  const segmentBody = await command.getFragmentedSegment({ url: `${baseUrlSegment}/${videoUUID}/${segmentName}` })
 | 
						|
  const shaBody = await command.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url })
 | 
						|
 | 
						|
  expect(sha256(segmentBody)).to.equal(shaBody[segmentName])
 | 
						|
}
 | 
						|
 | 
						|
async function checkResolutionsInMasterPlaylist (options: {
 | 
						|
  server: PeerTubeServer
 | 
						|
  playlistUrl: string
 | 
						|
  resolutions: number[]
 | 
						|
  transcoded?: boolean // default true
 | 
						|
  withRetry?: boolean // default false
 | 
						|
}) {
 | 
						|
  const { server, playlistUrl, resolutions, withRetry = false, transcoded = true } = options
 | 
						|
 | 
						|
  const masterPlaylist = await server.streamingPlaylists.get({ url: playlistUrl, withRetry })
 | 
						|
 | 
						|
  for (const resolution of resolutions) {
 | 
						|
    const reg = transcoded
 | 
						|
      ? new RegExp('#EXT-X-STREAM-INF:BANDWIDTH=\\d+,RESOLUTION=\\d+x' + resolution + ',(FRAME-RATE=\\d+,)?CODECS="avc1.64001f,mp4a.40.2"')
 | 
						|
      : new RegExp('#EXT-X-STREAM-INF:BANDWIDTH=\\d+,RESOLUTION=\\d+x' + resolution + '')
 | 
						|
 | 
						|
    expect(masterPlaylist).to.match(reg)
 | 
						|
  }
 | 
						|
 | 
						|
  const playlistsLength = masterPlaylist.split('\n').filter(line => line.startsWith('#EXT-X-STREAM-INF:BANDWIDTH='))
 | 
						|
  expect(playlistsLength).to.have.lengthOf(resolutions.length)
 | 
						|
}
 | 
						|
 | 
						|
export {
 | 
						|
  checkSegmentHash,
 | 
						|
  checkLiveSegmentHash,
 | 
						|
  checkResolutionsInMasterPlaylist
 | 
						|
}
 |