From 25b7c847249c2b7f7b309b14eecb03dd024db649 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 10 Nov 2020 14:21:26 +0100 Subject: [PATCH] Try to optimize sha segments fetching --- .../app/+videos/+video-watch/video-watch.component.ts | 2 ++ .../assets/player/p2p-media-loader/segment-validator.ts | 9 ++++++--- client/src/assets/player/peertube-player-manager.ts | 4 +++- client/src/standalone/videos/embed.ts | 2 ++ 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/client/src/app/+videos/+video-watch/video-watch.component.ts b/client/src/app/+videos/+video-watch/video-watch.component.ts index 48623cae0..66af9709d 100644 --- a/client/src/app/+videos/+video-watch/video-watch.component.ts +++ b/client/src/app/+videos/+video-watch/video-watch.component.ts @@ -771,6 +771,8 @@ export class VideoWatchComponent implements OnInit, OnDestroy { : null, embedUrl: video.embedUrl, + isLive: video.isLive, + language: this.localeId, userWatching: user && user.videosHistoryEnabled === true ? { 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 9add35184..844956d6a 100644 --- a/client/src/assets/player/p2p-media-loader/segment-validator.ts +++ b/client/src/assets/player/p2p-media-loader/segment-validator.ts @@ -6,11 +6,14 @@ type SegmentsJSON = { [filename: string]: string | { [byterange: string]: string const maxRetries = 3 -function segmentValidatorFactory (segmentsSha256Url: string) { +function segmentValidatorFactory (segmentsSha256Url: string, isLive: boolean) { let segmentsJSON = fetchSha256Segments(segmentsSha256Url) const regex = /bytes=(\d+)-(\d+)/ return async function segmentValidator (segment: Segment, retry = 1) { + // Wait for hash generation from the server + if (isLive) await wait(1000) + const filename = basename(segment.url) const segmentValue = (await segmentsJSON)[filename] @@ -20,10 +23,10 @@ function segmentValidatorFactory (segmentsSha256Url: string) { } if (!segmentValue) { - await wait(1000) - console.log('Refetching sha segments for %s.', filename) + await wait(1000) + segmentsJSON = fetchSha256Segments(segmentsSha256Url) await segmentValidator(segment, retry + 1) diff --git a/client/src/assets/player/peertube-player-manager.ts b/client/src/assets/player/peertube-player-manager.ts index c1953043e..da23c59a7 100644 --- a/client/src/assets/player/peertube-player-manager.ts +++ b/client/src/assets/player/peertube-player-manager.ts @@ -98,6 +98,8 @@ export interface CommonOptions extends CustomizationOptions { videoViewUrl: string embedUrl: string + isLive: boolean + language?: string videoCaptions: VideoJSCaption[] @@ -323,7 +325,7 @@ export class PeertubePlayerManager { const p2pMediaLoaderConfig = { loader: { trackerAnnounce, - segmentValidator: segmentValidatorFactory(options.p2pMediaLoader.segmentsSha256Url), + segmentValidator: segmentValidatorFactory(options.p2pMediaLoader.segmentsSha256Url, options.common.isLive), rtcConfig: getRtcConfig(), requiredSegmentsPriority: 1, segmentUrlBuilder: segmentUrlBuilderFactory(redundancyUrlManager), diff --git a/client/src/standalone/videos/embed.ts b/client/src/standalone/videos/embed.ts index 9b07ef5e6..66033158b 100644 --- a/client/src/standalone/videos/embed.ts +++ b/client/src/standalone/videos/embed.ts @@ -532,6 +532,8 @@ export class PeerTubeEmbed { inactivityTimeout: 2500, videoViewUrl: this.getVideoUrl(videoInfo.uuid) + '/views', + isLive: videoInfo.isLive, + playerElement: this.playerElement, onPlayerElementChange: (element: HTMLVideoElement) => this.playerElement = element,