aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/videos/streaming-playlists.ts
blob: a224b8f5f67a78fd2595c1ba8255e1889af58d6f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { expect } from 'chai'
import { basename } from 'path'
import { sha256 } from '@server/helpers/core-utils'
import { removeFragmentedMP4Ext } from '@shared/core-utils'
import { HttpStatusCode, VideoStreamingPlaylist } from '@shared/models'
import { PeerTubeServer } from '../server'

async function checkSegmentHash (options: {
  server: PeerTubeServer
  baseUrlPlaylist: string
  baseUrlSegment: string
  videoUUID: string
  resolution: number
  hlsPlaylist: VideoStreamingPlaylist
}) {
  const { server, baseUrlPlaylist, baseUrlSegment, videoUUID, 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}/${videoUUID}/${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.getSegment({
    url: `${baseUrlSegment}/${videoUUID}/${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.getSegment({ 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[]
}) {
  const { server, playlistUrl, resolutions } = options

  const masterPlaylist = await server.streamingPlaylists.get({ url: playlistUrl })

  for (const resolution of resolutions) {
    const reg = new RegExp(
      '#EXT-X-STREAM-INF:BANDWIDTH=\\d+,RESOLUTION=\\d+x' + resolution + ',(FRAME-RATE=\\d+,)?CODECS="avc1.64001f,mp4a.40.2"'
    )

    expect(masterPlaylist).to.match(reg)
  }
}

export {
  checkSegmentHash,
  checkLiveSegmentHash,
  checkResolutionsInMasterPlaylist
}