aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/videos
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
parent6910f20f114b5bd020258a3a9a3f2117819a60c2 (diff)
downloadPeerTube-57f879a540551c3b958b0991c8e1e3657a4481d8.tar.gz
PeerTube-57f879a540551c3b958b0991c8e1e3657a4481d8.tar.zst
PeerTube-57f879a540551c3b958b0991c8e1e3657a4481d8.zip
Introduce streaming playlists command
Diffstat (limited to 'shared/extra-utils/videos')
-rw-r--r--shared/extra-utils/videos/index.ts3
-rw-r--r--shared/extra-utils/videos/streaming-playlists-command.ts45
-rw-r--r--shared/extra-utils/videos/streaming-playlists.ts76
-rw-r--r--shared/extra-utils/videos/video-streaming-playlists.ts82
4 files changed, 123 insertions, 83 deletions
diff --git a/shared/extra-utils/videos/index.ts b/shared/extra-utils/videos/index.ts
index 372cf7a90..f87ae8eea 100644
--- a/shared/extra-utils/videos/index.ts
+++ b/shared/extra-utils/videos/index.ts
@@ -9,7 +9,8 @@ export * from './live'
9export * from './playlists-command' 9export * from './playlists-command'
10export * from './playlists' 10export * from './playlists'
11export * from './services-command' 11export * from './services-command'
12export * from './streaming-playlists-command'
13export * from './streaming-playlists'
12export * from './video-channels' 14export * from './video-channels'
13export * from './video-comments' 15export * from './video-comments'
14export * from './video-streaming-playlists'
15export * from './videos' 16export * from './videos'
diff --git a/shared/extra-utils/videos/streaming-playlists-command.ts b/shared/extra-utils/videos/streaming-playlists-command.ts
new file mode 100644
index 000000000..4caec7137
--- /dev/null
+++ b/shared/extra-utils/videos/streaming-playlists-command.ts
@@ -0,0 +1,45 @@
1
2import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes'
3import { unwrapBody, unwrapText } from '../requests'
4import { AbstractCommand, OverrideCommandOptions } from '../shared'
5
6export class StreamingPlaylistsCommand extends AbstractCommand {
7
8 get (options: OverrideCommandOptions & {
9 url: string
10 }) {
11 return unwrapText(this.getRawRequest({
12 ...options,
13
14 url: options.url,
15 implicitToken: false,
16 defaultExpectedStatus: HttpStatusCode.OK_200
17 }))
18 }
19
20 getSegment (options: OverrideCommandOptions & {
21 url: string
22 range?: string
23 }) {
24 return unwrapText(this.getRawRequest({
25 ...options,
26
27 url: options.url,
28 range: options.range,
29 implicitToken: false,
30 defaultExpectedStatus: HttpStatusCode.OK_200,
31 }))
32 }
33
34 getSegmentSha256 (options: OverrideCommandOptions & {
35 url: string
36 }) {
37 return unwrapBody<{ [ id: string ]: string }>(this.getRawRequest({
38 ...options,
39
40 url: options.url,
41 implicitToken: false,
42 defaultExpectedStatus: HttpStatusCode.OK_200
43 }))
44 }
45}
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}
diff --git a/shared/extra-utils/videos/video-streaming-playlists.ts b/shared/extra-utils/videos/video-streaming-playlists.ts
deleted file mode 100644
index 99c2e1880..000000000
--- a/shared/extra-utils/videos/video-streaming-playlists.ts
+++ /dev/null
@@ -1,82 +0,0 @@
1import { makeRawRequest } from '../requests/requests'
2import { sha256 } from '../../../server/helpers/core-utils'
3import { VideoStreamingPlaylist } from '../../models/videos/video-streaming-playlist.model'
4import { expect } from 'chai'
5import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
6
7function getPlaylist (url: string, statusCodeExpected = HttpStatusCode.OK_200) {
8 return makeRawRequest(url, statusCodeExpected)
9}
10
11function getSegment (url: string, statusCodeExpected = HttpStatusCode.OK_200, range?: string) {
12 return makeRawRequest(url, statusCodeExpected, range)
13}
14
15function getSegmentSha256 (url: string, statusCodeExpected = HttpStatusCode.OK_200) {
16 return makeRawRequest(url, statusCodeExpected)
17}
18
19async 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
37 const res2 = await getSegment(`${baseUrlSegment}/${videoUUID}/${videoName}`, HttpStatusCode.PARTIAL_CONTENT_206, `bytes=${range}`)
38
39 const resSha = await getSegmentSha256(hlsPlaylist.segmentsSha256Url)
40
41 const sha256Server = resSha.body[videoName][range]
42 expect(sha256(res2.body)).to.equal(sha256Server)
43}
44
45async 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
59async 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
73// ---------------------------------------------------------------------------
74
75export {
76 getPlaylist,
77 getSegment,
78 checkResolutionsInMasterPlaylist,
79 getSegmentSha256,
80 checkLiveSegmentHash,
81 checkSegmentHash
82}