diff options
Diffstat (limited to 'shared/extra-utils/videos/streaming-playlists.ts')
-rw-r--r-- | shared/extra-utils/videos/streaming-playlists.ts | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/shared/extra-utils/videos/streaming-playlists.ts b/shared/extra-utils/videos/streaming-playlists.ts new file mode 100644 index 000000000..1ae3fefc1 --- /dev/null +++ b/shared/extra-utils/videos/streaming-playlists.ts | |||
@@ -0,0 +1,75 @@ | |||
1 | import { expect } from 'chai' | ||
2 | import { sha256 } from '@server/helpers/core-utils' | ||
3 | import { HttpStatusCode, VideoStreamingPlaylist } from '@shared/models' | ||
4 | import { PeerTubeServer } from '../server' | ||
5 | |||
6 | async function checkSegmentHash (options: { | ||
7 | server: PeerTubeServer | ||
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 | ||
15 | const command = server.streamingPlaylists | ||
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: { | ||
38 | server: PeerTubeServer | ||
39 | baseUrlSegment: string | ||
40 | videoUUID: string | ||
41 | segmentName: string | ||
42 | hlsPlaylist: VideoStreamingPlaylist | ||
43 | }) { | ||
44 | const { server, baseUrlSegment, videoUUID, segmentName, hlsPlaylist } = options | ||
45 | const command = server.streamingPlaylists | ||
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: { | ||
54 | server: PeerTubeServer | ||
55 | playlistUrl: string | ||
56 | resolutions: number[] | ||
57 | }) { | ||
58 | const { server, playlistUrl, resolutions } = options | ||
59 | |||
60 | const masterPlaylist = await server.streamingPlaylists.get({ url: playlistUrl }) | ||
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 | } | ||