aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/videos/streaming-playlists.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-07-09 10:21:10 +0200
committerChocobozzz <me@florianbigard.com>2021-07-20 15:27:18 +0200
commit57f879a540551c3b958b0991c8e1e3657a4481d8 (patch)
treecd9283dec9ef0b7fee116c93c36650de188ad892 /shared/extra-utils/videos/streaming-playlists.ts
parent6910f20f114b5bd020258a3a9a3f2117819a60c2 (diff)
downloadPeerTube-57f879a540551c3b958b0991c8e1e3657a4481d8.tar.gz
PeerTube-57f879a540551c3b958b0991c8e1e3657a4481d8.tar.zst
PeerTube-57f879a540551c3b958b0991c8e1e3657a4481d8.zip
Introduce streaming playlists command
Diffstat (limited to 'shared/extra-utils/videos/streaming-playlists.ts')
-rw-r--r--shared/extra-utils/videos/streaming-playlists.ts76
1 files changed, 76 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..0324c739a
--- /dev/null
+++ b/shared/extra-utils/videos/streaming-playlists.ts
@@ -0,0 +1,76 @@
1import { expect } from 'chai'
2import { sha256 } from '@server/helpers/core-utils'
3import { HttpStatusCode } from '@shared/core-utils'
4import { VideoStreamingPlaylist } from '@shared/models'
5import { ServerInfo } from '../server'
6
7async function checkSegmentHash (options: {
8 server: ServerInfo
9 baseUrlPlaylist: string
10 baseUrlSegment: string
11 videoUUID: string
12 resolution: number
13 hlsPlaylist: VideoStreamingPlaylist
14}) {
15 const { server, baseUrlPlaylist, baseUrlSegment, videoUUID, resolution, hlsPlaylist } = options
16 const command = server.streamingPlaylistsCommand
17
18 const playlist = await command.get({ url: `${baseUrlPlaylist}/${videoUUID}/${resolution}.m3u8` })
19
20 const videoName = `${videoUUID}-${resolution}-fragmented.mp4`
21
22 const matches = /#EXT-X-BYTERANGE:(\d+)@(\d+)/.exec(playlist)
23
24 const length = parseInt(matches[1], 10)
25 const offset = parseInt(matches[2], 10)
26 const range = `${offset}-${offset + length - 1}`
27
28 const segmentBody = await command.getSegment({
29 url: `${baseUrlSegment}/${videoUUID}/${videoName}`,
30 expectedStatus: HttpStatusCode.PARTIAL_CONTENT_206,
31 range: `bytes=${range}`
32 })
33
34 const shaBody = await command.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url })
35 expect(sha256(segmentBody)).to.equal(shaBody[videoName][range])
36}
37
38async function checkLiveSegmentHash (options: {
39 server: ServerInfo
40 baseUrlSegment: string
41 videoUUID: string
42 segmentName: string
43 hlsPlaylist: VideoStreamingPlaylist
44}) {
45 const { server, baseUrlSegment, videoUUID, segmentName, hlsPlaylist } = options
46 const command = server.streamingPlaylistsCommand
47
48 const segmentBody = await command.getSegment({ url: `${baseUrlSegment}/${videoUUID}/${segmentName}` })
49 const shaBody = await command.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url })
50
51 expect(sha256(segmentBody)).to.equal(shaBody[segmentName])
52}
53
54async function checkResolutionsInMasterPlaylist (options: {
55 server: ServerInfo
56 playlistUrl: string
57 resolutions: number[]
58}) {
59 const { server, playlistUrl, resolutions } = options
60
61 const masterPlaylist = await server.streamingPlaylistsCommand.get({ url: playlistUrl })
62
63 for (const resolution of resolutions) {
64 const reg = new RegExp(
65 '#EXT-X-STREAM-INF:BANDWIDTH=\\d+,RESOLUTION=\\d+x' + resolution + ',(FRAME-RATE=\\d+,)?CODECS="avc1.64001f,mp4a.40.2"'
66 )
67
68 expect(masterPlaylist).to.match(reg)
69 }
70}
71
72export {
73 checkSegmentHash,
74 checkLiveSegmentHash,
75 checkResolutionsInMasterPlaylist
76}