diff options
author | Chocobozzz <me@florianbigard.com> | 2020-09-17 09:20:52 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2020-11-09 15:33:04 +0100 |
commit | c6c0fa6cd8fe8f752463d8982c3dbcd448739c4e (patch) | |
tree | 79304b0152b0a38d33b26e65d4acdad0da4032a7 /client/src/assets/player | |
parent | 110d463fece85e87a26aca48a6048ae0017a27b3 (diff) | |
download | PeerTube-c6c0fa6cd8fe8f752463d8982c3dbcd448739c4e.tar.gz PeerTube-c6c0fa6cd8fe8f752463d8982c3dbcd448739c4e.tar.zst PeerTube-c6c0fa6cd8fe8f752463d8982c3dbcd448739c4e.zip |
Live streaming implementation first step
Diffstat (limited to 'client/src/assets/player')
-rw-r--r-- | client/src/assets/player/p2p-media-loader/segment-validator.ts | 37 | ||||
-rw-r--r-- | client/src/assets/player/peertube-player-manager.ts | 4 |
2 files changed, 33 insertions, 8 deletions
diff --git a/client/src/assets/player/p2p-media-loader/segment-validator.ts b/client/src/assets/player/p2p-media-loader/segment-validator.ts index 72c32f9e0..0614f73d2 100644 --- a/client/src/assets/player/p2p-media-loader/segment-validator.ts +++ b/client/src/assets/player/p2p-media-loader/segment-validator.ts | |||
@@ -1,17 +1,42 @@ | |||
1 | import { Segment } from 'p2p-media-loader-core' | 1 | import { Segment } from 'p2p-media-loader-core' |
2 | import { basename } from 'path' | 2 | import { basename } from 'path' |
3 | 3 | ||
4 | type SegmentsJSON = { [filename: string]: string | { [byterange: string]: string } } | ||
5 | |||
4 | function segmentValidatorFactory (segmentsSha256Url: string) { | 6 | function segmentValidatorFactory (segmentsSha256Url: string) { |
5 | const segmentsJSON = fetchSha256Segments(segmentsSha256Url) | 7 | let segmentsJSON = fetchSha256Segments(segmentsSha256Url) |
6 | const regex = /bytes=(\d+)-(\d+)/ | 8 | const regex = /bytes=(\d+)-(\d+)/ |
7 | 9 | ||
8 | return async function segmentValidator (segment: Segment) { | 10 | return async function segmentValidator (segment: Segment, canRefetchSegmentHashes = true) { |
9 | const filename = basename(segment.url) | 11 | const filename = basename(segment.url) |
10 | const captured = regex.exec(segment.range) | ||
11 | 12 | ||
12 | const range = captured[1] + '-' + captured[2] | 13 | const segmentValue = (await segmentsJSON)[filename] |
14 | |||
15 | if (!segmentValue && !canRefetchSegmentHashes) { | ||
16 | throw new Error(`Unknown segment name ${filename} in segment validator`) | ||
17 | } | ||
18 | |||
19 | if (!segmentValue) { | ||
20 | console.log('Refetching sha segments.') | ||
21 | |||
22 | // Refetch | ||
23 | segmentsJSON = fetchSha256Segments(segmentsSha256Url) | ||
24 | segmentValidator(segment, false) | ||
25 | return | ||
26 | } | ||
27 | |||
28 | let hashShouldBe: string | ||
29 | let range = '' | ||
30 | |||
31 | if (typeof segmentValue === 'string') { | ||
32 | hashShouldBe = segmentValue | ||
33 | } else { | ||
34 | const captured = regex.exec(segment.range) | ||
35 | range = captured[1] + '-' + captured[2] | ||
36 | |||
37 | hashShouldBe = segmentValue[range] | ||
38 | } | ||
13 | 39 | ||
14 | const hashShouldBe = (await segmentsJSON)[filename][range] | ||
15 | if (hashShouldBe === undefined) { | 40 | if (hashShouldBe === undefined) { |
16 | throw new Error(`Unknown segment name ${filename}/${range} in segment validator`) | 41 | throw new Error(`Unknown segment name ${filename}/${range} in segment validator`) |
17 | } | 42 | } |
@@ -36,7 +61,7 @@ export { | |||
36 | 61 | ||
37 | function fetchSha256Segments (url: string) { | 62 | function fetchSha256Segments (url: string) { |
38 | return fetch(url) | 63 | return fetch(url) |
39 | .then(res => res.json()) | 64 | .then(res => res.json() as Promise<SegmentsJSON>) |
40 | .catch(err => { | 65 | .catch(err => { |
41 | console.error('Cannot get sha256 segments', err) | 66 | console.error('Cannot get sha256 segments', err) |
42 | return {} | 67 | return {} |
diff --git a/client/src/assets/player/peertube-player-manager.ts b/client/src/assets/player/peertube-player-manager.ts index af044c864..3d72d4609 100644 --- a/client/src/assets/player/peertube-player-manager.ts +++ b/client/src/assets/player/peertube-player-manager.ts | |||
@@ -325,7 +325,7 @@ export class PeertubePlayerManager { | |||
325 | trackerAnnounce, | 325 | trackerAnnounce, |
326 | segmentValidator: segmentValidatorFactory(options.p2pMediaLoader.segmentsSha256Url), | 326 | segmentValidator: segmentValidatorFactory(options.p2pMediaLoader.segmentsSha256Url), |
327 | rtcConfig: getRtcConfig(), | 327 | rtcConfig: getRtcConfig(), |
328 | requiredSegmentsPriority: 5, | 328 | requiredSegmentsPriority: 1, |
329 | segmentUrlBuilder: segmentUrlBuilderFactory(redundancyUrlManager), | 329 | segmentUrlBuilder: segmentUrlBuilderFactory(redundancyUrlManager), |
330 | useP2P: getStoredP2PEnabled(), | 330 | useP2P: getStoredP2PEnabled(), |
331 | consumeOnly | 331 | consumeOnly |
@@ -353,7 +353,7 @@ export class PeertubePlayerManager { | |||
353 | hlsjsConfig: { | 353 | hlsjsConfig: { |
354 | capLevelToPlayerSize: true, | 354 | capLevelToPlayerSize: true, |
355 | autoStartLoad: false, | 355 | autoStartLoad: false, |
356 | liveSyncDurationCount: 7, | 356 | liveSyncDurationCount: 5, |
357 | loader: new p2pMediaLoaderModule.Engine(p2pMediaLoaderConfig).createLoaderClass() | 357 | loader: new p2pMediaLoaderModule.Engine(p2pMediaLoaderConfig).createLoaderClass() |
358 | } | 358 | } |
359 | } | 359 | } |