]>
Commit | Line | Data |
---|---|---|
57f879a5 C |
1 | import { expect } from 'chai' |
2 | import { sha256 } from '@server/helpers/core-utils' | |
4c7e60bc | 3 | import { HttpStatusCode, VideoStreamingPlaylist } from '@shared/models' |
254d3579 | 4 | import { PeerTubeServer } from '../server' |
57f879a5 C |
5 | |
6 | async function checkSegmentHash (options: { | |
254d3579 | 7 | server: PeerTubeServer |
57f879a5 C |
8 | baseUrlPlaylist: string |
9 | baseUrlSegment: string | |
10 | videoUUID: string | |
11 | resolution: number | |
12 | hlsPlaylist: VideoStreamingPlaylist | |
13 | }) { | |
14 | const { server, baseUrlPlaylist, baseUrlSegment, videoUUID, resolution, hlsPlaylist } = options | |
89d241a7 | 15 | const command = server.streamingPlaylists |
57f879a5 C |
16 | |
17 | const playlist = await command.get({ url: `${baseUrlPlaylist}/${videoUUID}/${resolution}.m3u8` }) | |
18 | ||
19 | const videoName = `${videoUUID}-${resolution}-fragmented.mp4` | |
20 | ||
21 | const matches = /#EXT-X-BYTERANGE:(\d+)@(\d+)/.exec(playlist) | |
22 | ||
23 | const length = parseInt(matches[1], 10) | |
24 | const offset = parseInt(matches[2], 10) | |
25 | const range = `${offset}-${offset + length - 1}` | |
26 | ||
27 | const segmentBody = await command.getSegment({ | |
28 | url: `${baseUrlSegment}/${videoUUID}/${videoName}`, | |
29 | expectedStatus: HttpStatusCode.PARTIAL_CONTENT_206, | |
30 | range: `bytes=${range}` | |
31 | }) | |
32 | ||
33 | const shaBody = await command.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url }) | |
34 | expect(sha256(segmentBody)).to.equal(shaBody[videoName][range]) | |
35 | } | |
36 | ||
37 | async function checkLiveSegmentHash (options: { | |
254d3579 | 38 | server: PeerTubeServer |
57f879a5 C |
39 | baseUrlSegment: string |
40 | videoUUID: string | |
41 | segmentName: string | |
42 | hlsPlaylist: VideoStreamingPlaylist | |
43 | }) { | |
44 | const { server, baseUrlSegment, videoUUID, segmentName, hlsPlaylist } = options | |
89d241a7 | 45 | const command = server.streamingPlaylists |
57f879a5 C |
46 | |
47 | const segmentBody = await command.getSegment({ url: `${baseUrlSegment}/${videoUUID}/${segmentName}` }) | |
48 | const shaBody = await command.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url }) | |
49 | ||
50 | expect(sha256(segmentBody)).to.equal(shaBody[segmentName]) | |
51 | } | |
52 | ||
53 | async function checkResolutionsInMasterPlaylist (options: { | |
254d3579 | 54 | server: PeerTubeServer |
57f879a5 C |
55 | playlistUrl: string |
56 | resolutions: number[] | |
57 | }) { | |
58 | const { server, playlistUrl, resolutions } = options | |
59 | ||
89d241a7 | 60 | const masterPlaylist = await server.streamingPlaylists.get({ url: playlistUrl }) |
57f879a5 C |
61 | |
62 | for (const resolution of resolutions) { | |
63 | const reg = new RegExp( | |
64 | '#EXT-X-STREAM-INF:BANDWIDTH=\\d+,RESOLUTION=\\d+x' + resolution + ',(FRAME-RATE=\\d+,)?CODECS="avc1.64001f,mp4a.40.2"' | |
65 | ) | |
66 | ||
67 | expect(masterPlaylist).to.match(reg) | |
68 | } | |
69 | } | |
70 | ||
71 | export { | |
72 | checkSegmentHash, | |
73 | checkLiveSegmentHash, | |
74 | checkResolutionsInMasterPlaylist | |
75 | } |