video-streaming-playlists.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. function getPlaylist (url: string, statusCodeExpected = 200) {
  6. return makeRawRequest(url, statusCodeExpected)
  7. }
  8. function getSegment (url: string, statusCodeExpected = 200, range?: string) {
  9. return makeRawRequest(url, statusCodeExpected, range)
  10. }
  11. function getSegmentSha256 (url: string, statusCodeExpected = 200) {
  12. return makeRawRequest(url, statusCodeExpected)
  13. }
  14. async function checkSegmentHash (
  15. baseUrlPlaylist: string,
  16. baseUrlSegment: string,
  17. videoUUID: string,
  18. resolution: number,
  19. hlsPlaylist: VideoStreamingPlaylist
  20. ) {
  21. const res = await getPlaylist(`${baseUrlPlaylist}/${videoUUID}/${resolution}.m3u8`)
  22. const playlist = res.text
  23. const videoName = `${videoUUID}-${resolution}-fragmented.mp4`
  24. const matches = /#EXT-X-BYTERANGE:(\d+)@(\d+)/.exec(playlist)
  25. const length = parseInt(matches[1], 10)
  26. const offset = parseInt(matches[2], 10)
  27. const range = `${offset}-${offset + length - 1}`
  28. const res2 = await getSegment(`${baseUrlSegment}/${videoUUID}/${videoName}`, 206, `bytes=${range}`)
  29. const resSha = await getSegmentSha256(hlsPlaylist.segmentsSha256Url)
  30. const sha256Server = resSha.body[ videoName ][range]
  31. expect(sha256(res2.body)).to.equal(sha256Server)
  32. }
  33. // ---------------------------------------------------------------------------
  34. export {
  35. getPlaylist,
  36. getSegment,
  37. getSegmentSha256,
  38. checkSegmentHash
  39. }