diff options
author | Chocobozzz <me@florianbigard.com> | 2021-07-09 10:21:10 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-07-20 15:27:18 +0200 |
commit | 57f879a540551c3b958b0991c8e1e3657a4481d8 (patch) | |
tree | cd9283dec9ef0b7fee116c93c36650de188ad892 /shared/extra-utils/videos | |
parent | 6910f20f114b5bd020258a3a9a3f2117819a60c2 (diff) | |
download | PeerTube-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.ts | 3 | ||||
-rw-r--r-- | shared/extra-utils/videos/streaming-playlists-command.ts | 45 | ||||
-rw-r--r-- | shared/extra-utils/videos/streaming-playlists.ts | 76 | ||||
-rw-r--r-- | shared/extra-utils/videos/video-streaming-playlists.ts | 82 |
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' | |||
9 | export * from './playlists-command' | 9 | export * from './playlists-command' |
10 | export * from './playlists' | 10 | export * from './playlists' |
11 | export * from './services-command' | 11 | export * from './services-command' |
12 | export * from './streaming-playlists-command' | ||
13 | export * from './streaming-playlists' | ||
12 | export * from './video-channels' | 14 | export * from './video-channels' |
13 | export * from './video-comments' | 15 | export * from './video-comments' |
14 | export * from './video-streaming-playlists' | ||
15 | export * from './videos' | 16 | export * 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 | |||
2 | import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes' | ||
3 | import { unwrapBody, unwrapText } from '../requests' | ||
4 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
5 | |||
6 | export 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 @@ | |||
1 | import { expect } from 'chai' | ||
2 | import { sha256 } from '@server/helpers/core-utils' | ||
3 | import { HttpStatusCode } from '@shared/core-utils' | ||
4 | import { VideoStreamingPlaylist } from '@shared/models' | ||
5 | import { ServerInfo } from '../server' | ||
6 | |||
7 | async 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 | |||
38 | async 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 | |||
54 | async 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 | |||
72 | export { | ||
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 @@ | |||
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' | ||
5 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
6 | |||
7 | function getPlaylist (url: string, statusCodeExpected = HttpStatusCode.OK_200) { | ||
8 | return makeRawRequest(url, statusCodeExpected) | ||
9 | } | ||
10 | |||
11 | function getSegment (url: string, statusCodeExpected = HttpStatusCode.OK_200, range?: string) { | ||
12 | return makeRawRequest(url, statusCodeExpected, range) | ||
13 | } | ||
14 | |||
15 | function getSegmentSha256 (url: string, statusCodeExpected = HttpStatusCode.OK_200) { | ||
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 | |||
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 | |||
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 | |||
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 | |||
73 | // --------------------------------------------------------------------------- | ||
74 | |||
75 | export { | ||
76 | getPlaylist, | ||
77 | getSegment, | ||
78 | checkResolutionsInMasterPlaylist, | ||
79 | getSegmentSha256, | ||
80 | checkLiveSegmentHash, | ||
81 | checkSegmentHash | ||
82 | } | ||