]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - 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
1 import { expect } from 'chai'
2 import { basename } from 'path'
3 import { sha256 } from '@server/helpers/core-utils'
4 import { removeFragmentedMP4Ext } from '@shared/core-utils'
5 import { HttpStatusCode, VideoStreamingPlaylist } from '@shared/models'
6 import { PeerTubeServer } from '../server'
7
8 async function checkSegmentHash (options: {
9 server: PeerTubeServer
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
17 const command = server.streamingPlaylists
18
19 const file = hlsPlaylist.files.find(f => f.resolution.id === resolution)
20 const videoName = basename(file.fileUrl)
21
22 const playlist = await command.get({ url: `${baseUrlPlaylist}/${videoUUID}/${removeFragmentedMP4Ext(videoName)}.m3u8` })
23
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
40 async function checkLiveSegmentHash (options: {
41 server: PeerTubeServer
42 baseUrlSegment: string
43 videoUUID: string
44 segmentName: string
45 hlsPlaylist: VideoStreamingPlaylist
46 }) {
47 const { server, baseUrlSegment, videoUUID, segmentName, hlsPlaylist } = options
48 const command = server.streamingPlaylists
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
56 async function checkResolutionsInMasterPlaylist (options: {
57 server: PeerTubeServer
58 playlistUrl: string
59 resolutions: number[]
60 }) {
61 const { server, playlistUrl, resolutions } = options
62
63 const masterPlaylist = await server.streamingPlaylists.get({ url: playlistUrl })
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
74 export {
75 checkSegmentHash,
76 checkLiveSegmentHash,
77 checkResolutionsInMasterPlaylist
78 }