video-streaming-playlists.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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'
  6. function getPlaylist (url: string, statusCodeExpected = HttpStatusCode.OK_200) {
  7. return makeRawRequest(url, statusCodeExpected)
  8. }
  9. function getSegment (url: string, statusCodeExpected = HttpStatusCode.OK_200, range?: string) {
  10. return makeRawRequest(url, statusCodeExpected, range)
  11. }
  12. function getSegmentSha256 (url: string, statusCodeExpected = HttpStatusCode.OK_200) {
  13. return makeRawRequest(url, statusCodeExpected)
  14. }
  15. async function checkSegmentHash (
  16. baseUrlPlaylist: string,
  17. baseUrlSegment: string,
  18. videoUUID: string,
  19. resolution: number,
  20. hlsPlaylist: VideoStreamingPlaylist
  21. ) {
  22. const res = await getPlaylist(`${baseUrlPlaylist}/${videoUUID}/${resolution}.m3u8`)
  23. const playlist = res.text
  24. const videoName = `${videoUUID}-${resolution}-fragmented.mp4`
  25. 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}`
  29. const res2 = await getSegment(`${baseUrlSegment}/${videoUUID}/${videoName}`, HttpStatusCode.PARTIAL_CONTENT_206, `bytes=${range}`)
  30. const resSha = await getSegmentSha256(hlsPlaylist.segmentsSha256Url)
  31. const sha256Server = resSha.body[videoName][range]
  32. expect(sha256(res2.body)).to.equal(sha256Server)
  33. }
  34. async function checkLiveSegmentHash (
  35. baseUrlSegment: string,
  36. videoUUID: string,
  37. segmentName: string,
  38. hlsPlaylist: VideoStreamingPlaylist
  39. ) {
  40. const res2 = await getSegment(`${baseUrlSegment}/${videoUUID}/${segmentName}`)
  41. const resSha = await getSegmentSha256(hlsPlaylist.segmentsSha256Url)
  42. const sha256Server = resSha.body[segmentName]
  43. expect(sha256(res2.body)).to.equal(sha256Server)
  44. }
  45. async function checkResolutionsInMasterPlaylist (playlistUrl: string, resolutions: number[]) {
  46. const res = await getPlaylist(playlistUrl)
  47. const masterPlaylist = res.text
  48. for (const resolution of resolutions) {
  49. const reg = new RegExp(
  50. '#EXT-X-STREAM-INF:BANDWIDTH=\\d+,RESOLUTION=\\d+x' + resolution + ',(FRAME-RATE=\\d+,)?CODECS="avc1.64001f,mp4a.40.2"'
  51. )
  52. expect(masterPlaylist).to.match(reg)
  53. }
  54. }
  55. // ---------------------------------------------------------------------------
  56. export {
  57. getPlaylist,
  58. getSegment,
  59. checkResolutionsInMasterPlaylist,
  60. getSegmentSha256,
  61. checkLiveSegmentHash,
  62. checkSegmentHash
  63. }