]>
Commit | Line | Data |
---|---|---|
418d092a C |
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' | |
2d53be02 | 5 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' |
418d092a | 6 | |
2d53be02 | 7 | function getPlaylist (url: string, statusCodeExpected = HttpStatusCode.OK_200) { |
418d092a C |
8 | return makeRawRequest(url, statusCodeExpected) |
9 | } | |
10 | ||
2d53be02 | 11 | function getSegment (url: string, statusCodeExpected = HttpStatusCode.OK_200, range?: string) { |
418d092a C |
12 | return makeRawRequest(url, statusCodeExpected, range) |
13 | } | |
14 | ||
2d53be02 | 15 | function getSegmentSha256 (url: string, statusCodeExpected = HttpStatusCode.OK_200) { |
418d092a C |
16 | return makeRawRequest(url, statusCodeExpected) |
17 | } | |
18 | ||
19 | async function checkSegmentHash ( | |
20 | baseUrlPlaylist: string, | |
21 | baseUrlSegment: string, | |
22 | videoUUID: string, | |
23 | resolution: number, | |
24 | hlsPlaylist: VideoStreamingPlaylist | |
25 | ) { | |
26 | const res = await getPlaylist(`${baseUrlPlaylist}/${videoUUID}/${resolution}.m3u8`) | |
27 | const playlist = res.text | |
28 | ||
29 | const videoName = `${videoUUID}-${resolution}-fragmented.mp4` | |
30 | ||
31 | const matches = /#EXT-X-BYTERANGE:(\d+)@(\d+)/.exec(playlist) | |
32 | ||
33 | const length = parseInt(matches[1], 10) | |
34 | const offset = parseInt(matches[2], 10) | |
35 | const range = `${offset}-${offset + length - 1}` | |
36 | ||
2d53be02 | 37 | const res2 = await getSegment(`${baseUrlSegment}/${videoUUID}/${videoName}`, HttpStatusCode.PARTIAL_CONTENT_206, `bytes=${range}`) |
418d092a C |
38 | |
39 | const resSha = await getSegmentSha256(hlsPlaylist.segmentsSha256Url) | |
40 | ||
a1587156 | 41 | const sha256Server = resSha.body[videoName][range] |
418d092a C |
42 | expect(sha256(res2.body)).to.equal(sha256Server) |
43 | } | |
44 | ||
5c0904fc C |
45 | async function checkLiveSegmentHash ( |
46 | baseUrlSegment: string, | |
47 | videoUUID: string, | |
48 | segmentName: string, | |
49 | hlsPlaylist: VideoStreamingPlaylist | |
50 | ) { | |
51 | const res2 = await getSegment(`${baseUrlSegment}/${videoUUID}/${segmentName}`) | |
52 | ||
53 | const resSha = await getSegmentSha256(hlsPlaylist.segmentsSha256Url) | |
54 | ||
55 | const sha256Server = resSha.body[segmentName] | |
56 | expect(sha256(res2.body)).to.equal(sha256Server) | |
57 | } | |
58 | ||
bd54ad19 C |
59 | async function checkResolutionsInMasterPlaylist (playlistUrl: string, resolutions: number[]) { |
60 | const res = await getPlaylist(playlistUrl) | |
61 | ||
62 | const masterPlaylist = res.text | |
63 | ||
64 | for (const resolution of resolutions) { | |
65 | const reg = new RegExp( | |
66 | '#EXT-X-STREAM-INF:BANDWIDTH=\\d+,RESOLUTION=\\d+x' + resolution + ',(FRAME-RATE=\\d+,)?CODECS="avc1.64001f,mp4a.40.2"' | |
67 | ) | |
68 | ||
69 | expect(masterPlaylist).to.match(reg) | |
70 | } | |
71 | } | |
72 | ||
418d092a C |
73 | // --------------------------------------------------------------------------- |
74 | ||
75 | export { | |
76 | getPlaylist, | |
77 | getSegment, | |
bd54ad19 | 78 | checkResolutionsInMasterPlaylist, |
418d092a | 79 | getSegmentSha256, |
5c0904fc | 80 | checkLiveSegmentHash, |
418d092a C |
81 | checkSegmentHash |
82 | } |