]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/extra-utils/videos/video-streaming-playlists.ts
Increase tests waits
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / videos / video-streaming-playlists.ts
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
6 function getPlaylist (url: string, statusCodeExpected = 200) {
7 return makeRawRequest(url, statusCodeExpected)
8 }
9
10 function getSegment (url: string, statusCodeExpected = 200, range?: string) {
11 return makeRawRequest(url, statusCodeExpected, range)
12 }
13
14 function getSegmentSha256 (url: string, statusCodeExpected = 200) {
15 return makeRawRequest(url, statusCodeExpected)
16 }
17
18 async function checkSegmentHash (
19 baseUrlPlaylist: string,
20 baseUrlSegment: string,
21 videoUUID: string,
22 resolution: number,
23 hlsPlaylist: VideoStreamingPlaylist
24 ) {
25 const res = await getPlaylist(`${baseUrlPlaylist}/${videoUUID}/${resolution}.m3u8`)
26 const playlist = res.text
27
28 const videoName = `${videoUUID}-${resolution}-fragmented.mp4`
29
30 const matches = /#EXT-X-BYTERANGE:(\d+)@(\d+)/.exec(playlist)
31
32 const length = parseInt(matches[1], 10)
33 const offset = parseInt(matches[2], 10)
34 const range = `${offset}-${offset + length - 1}`
35
36 const res2 = await getSegment(`${baseUrlSegment}/${videoUUID}/${videoName}`, 206, `bytes=${range}`)
37
38 const resSha = await getSegmentSha256(hlsPlaylist.segmentsSha256Url)
39
40 const sha256Server = resSha.body[videoName][range]
41 expect(sha256(res2.body)).to.equal(sha256Server)
42 }
43
44 async function checkResolutionsInMasterPlaylist (playlistUrl: string, resolutions: number[]) {
45 const res = await getPlaylist(playlistUrl)
46
47 const masterPlaylist = res.text
48
49 for (const resolution of resolutions) {
50 const reg = new RegExp(
51 '#EXT-X-STREAM-INF:BANDWIDTH=\\d+,RESOLUTION=\\d+x' + resolution + ',(FRAME-RATE=\\d+,)?CODECS="avc1.64001f,mp4a.40.2"'
52 )
53
54 expect(masterPlaylist).to.match(reg)
55 }
56 }
57
58 // ---------------------------------------------------------------------------
59
60 export {
61 getPlaylist,
62 getSegment,
63 checkResolutionsInMasterPlaylist,
64 getSegmentSha256,
65 checkSegmentHash
66 }