]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/videos/streaming-playlists.ts
Use random names for VOD HLS playlists
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / videos / streaming-playlists.ts
CommitLineData
57f879a5 1import { expect } from 'chai'
83903cb6 2import { basename } from 'path'
57f879a5 3import { sha256 } from '@server/helpers/core-utils'
764b1a14 4import { removeFragmentedMP4Ext } from '@shared/core-utils'
4c7e60bc 5import { HttpStatusCode, VideoStreamingPlaylist } from '@shared/models'
254d3579 6import { PeerTubeServer } from '../server'
57f879a5
C
7
8async function checkSegmentHash (options: {
254d3579 9 server: PeerTubeServer
57f879a5
C
10 baseUrlPlaylist: string
11 baseUrlSegment: string
12 videoUUID: string
13 resolution: number
14 hlsPlaylist: VideoStreamingPlaylist
15}) {
16 const { server, baseUrlPlaylist, baseUrlSegment, videoUUID, resolution, hlsPlaylist } = options
89d241a7 17 const command = server.streamingPlaylists
57f879a5 18
83903cb6
C
19 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
20 const videoName = basename(file.fileUrl)
57f879a5 21
764b1a14
C
22 const playlist = await command.get({ url: `${baseUrlPlaylist}/${videoUUID}/${removeFragmentedMP4Ext(videoName)}.m3u8` })
23
57f879a5
C
24 const matches = /#EXT-X-BYTERANGE:(\d+)@(\d+)/.exec(playlist)
25
26 const length = parseInt(matches[1], 10)
27 const offset = parseInt(matches[2], 10)
28 const range = `${offset}-${offset + length - 1}`
29
30 const segmentBody = await command.getSegment({
31 url: `${baseUrlSegment}/${videoUUID}/${videoName}`,
32 expectedStatus: HttpStatusCode.PARTIAL_CONTENT_206,
33 range: `bytes=${range}`
34 })
35
36 const shaBody = await command.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url })
37 expect(sha256(segmentBody)).to.equal(shaBody[videoName][range])
38}
39
40async function checkLiveSegmentHash (options: {
254d3579 41 server: PeerTubeServer
57f879a5
C
42 baseUrlSegment: string
43 videoUUID: string
44 segmentName: string
45 hlsPlaylist: VideoStreamingPlaylist
46}) {
47 const { server, baseUrlSegment, videoUUID, segmentName, hlsPlaylist } = options
89d241a7 48 const command = server.streamingPlaylists
57f879a5
C
49
50 const segmentBody = await command.getSegment({ url: `${baseUrlSegment}/${videoUUID}/${segmentName}` })
51 const shaBody = await command.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url })
52
53 expect(sha256(segmentBody)).to.equal(shaBody[segmentName])
54}
55
56async function checkResolutionsInMasterPlaylist (options: {
254d3579 57 server: PeerTubeServer
57f879a5
C
58 playlistUrl: string
59 resolutions: number[]
60}) {
61 const { server, playlistUrl, resolutions } = options
62
89d241a7 63 const masterPlaylist = await server.streamingPlaylists.get({ url: playlistUrl })
57f879a5
C
64
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"'
68 )
69
70 expect(masterPlaylist).to.match(reg)
71 }
72}
73
74export {
75 checkSegmentHash,
76 checkLiveSegmentHash,
77 checkResolutionsInMasterPlaylist
78}