1 import { HybridLoaderSettings } from '@peertube/p2p-media-loader-core'
2 import { HlsJsEngineSettings } from '@peertube/p2p-media-loader-hlsjs'
3 import { logger } from '@root-helpers/logger'
4 import { LiveVideoLatencyMode } from '@shared/models'
5 import { getAverageBandwidthInStore } from '../../peertube-player-local-storage'
6 import { P2PMediaLoader, P2PMediaLoaderPluginOptions } from '../../types'
7 import { PeertubePlayerManagerOptions } from '../../types/manager-options'
8 import { getRtcConfig, isSameOrigin } from '../common'
9 import { RedundancyUrlManager } from '../p2p-media-loader/redundancy-url-manager'
10 import { segmentUrlBuilderFactory } from '../p2p-media-loader/segment-url-builder'
11 import { segmentValidatorFactory } from '../p2p-media-loader/segment-validator'
13 export class HLSOptionsBuilder {
16 private options: PeertubePlayerManagerOptions,
17 private p2pMediaLoaderModule?: any
22 async getPluginOptions () {
23 const commonOptions = this.options.common
25 const redundancyUrlManager = new RedundancyUrlManager(this.options.p2pMediaLoader.redundancyBaseUrls)
27 const p2pMediaLoaderConfig = await this.options.pluginsManager.runHook(
28 'filter:internal.player.p2p-media-loader.options.result',
29 this.getP2PMediaLoaderOptions(redundancyUrlManager)
31 const loader = new this.p2pMediaLoaderModule.Engine(p2pMediaLoaderConfig).createLoaderClass() as P2PMediaLoader
33 const p2pMediaLoader: P2PMediaLoaderPluginOptions = {
35 type: 'application/x-mpegURL',
36 startTime: commonOptions.startTime,
37 src: this.options.p2pMediaLoader.playlistUrl,
42 levelLabelHandler: (level: { height: number, width: number }) => {
43 const resolution = Math.min(level.height || 0, level.width || 0)
45 const file = this.options.p2pMediaLoader.videoFiles.find(f => f.resolution.id === resolution)
46 // We don't have files for live videos
47 if (!file) return level.height
49 let label = file.resolution.label
50 if (file.fps >= 50) label += file.fps
57 hlsjsConfig: this.getHLSJSOptions(loader)
60 return { p2pMediaLoader, hlsjs, html5 }
63 // ---------------------------------------------------------------------------
65 private getP2PMediaLoaderOptions (redundancyUrlManager: RedundancyUrlManager): HlsJsEngineSettings {
66 let consumeOnly = false
67 if ((navigator as any)?.connection?.type === 'cellular') {
68 logger.info('We are on a cellular connection: disabling seeding.')
72 const trackerAnnounce = this.options.p2pMediaLoader.trackerAnnounce
73 .filter(t => t.startsWith('ws'))
75 const specificLiveOrVODOptions = this.options.common.isLive
76 ? this.getP2PMediaLoaderLiveOptions()
77 : this.getP2PMediaLoaderVODOptions()
82 rtcConfig: getRtcConfig(),
84 simultaneousHttpDownloads: 1,
85 httpFailedSegmentTimeout: 1000,
87 xhrSetup: (xhr, url) => {
88 if (!this.options.common.requiresAuth) return
89 if (!isSameOrigin(this.options.common.serverUrl, url)) return
91 xhr.setRequestHeader('Authorization', this.options.common.authorizationHeader())
94 segmentValidator: segmentValidatorFactory({
95 segmentsSha256Url: this.options.p2pMediaLoader.segmentsSha256Url,
96 isLive: this.options.common.isLive,
97 authorizationHeader: this.options.common.authorizationHeader,
98 requiresAuth: this.options.common.requiresAuth,
99 serverUrl: this.options.common.serverUrl
102 segmentUrlBuilder: segmentUrlBuilderFactory(redundancyUrlManager),
104 useP2P: this.options.common.p2pEnabled,
107 ...specificLiveOrVODOptions
110 swarmId: this.options.p2pMediaLoader.playlistUrl,
111 forwardSegmentCount: specificLiveOrVODOptions.p2pDownloadMaxPriority ?? 20
116 private getP2PMediaLoaderLiveOptions (): Partial<HybridLoaderSettings> {
118 requiredSegmentsPriority: 1
121 const latencyMode = this.options.common.liveOptions.latencyMode
123 switch (latencyMode) {
124 case LiveVideoLatencyMode.SMALL_LATENCY:
129 httpDownloadProbability: 1
132 case LiveVideoLatencyMode.HIGH_LATENCY:
140 private getP2PMediaLoaderVODOptions (): Partial<HybridLoaderSettings> {
142 requiredSegmentsPriority: 3,
143 skipSegmentBuilderPriority: 1,
145 cachedSegmentExpiration: 86400000,
146 cachedSegmentsCount: 100,
148 httpDownloadMaxPriority: 9,
149 httpDownloadProbability: 0.06,
150 httpDownloadProbabilitySkipIfNoPeers: true,
152 p2pDownloadMaxPriority: 50
156 // ---------------------------------------------------------------------------
158 private getHLSJSOptions (loader: P2PMediaLoader) {
159 const specificLiveOrVODOptions = this.options.common.isLive
160 ? this.getHLSLiveOptions()
161 : this.getHLSVODOptions()
164 capLevelToPlayerSize: true,
165 autoStartLoad: false,
169 ...specificLiveOrVODOptions
172 const averageBandwidth = getAverageBandwidthInStore()
173 if (!averageBandwidth) return base
178 abrEwmaDefaultEstimate: averageBandwidth * 8, // We want bit/s
179 backBufferLength: 90,
181 testBandwidth: false,
186 private getHLSLiveOptions () {
187 const latencyMode = this.options.common.liveOptions.latencyMode
189 switch (latencyMode) {
190 case LiveVideoLatencyMode.SMALL_LATENCY:
192 liveSyncDurationCount: 2
195 case LiveVideoLatencyMode.HIGH_LATENCY:
197 liveSyncDurationCount: 10
202 liveSyncDurationCount: 5
207 private getHLSVODOptions () {
209 liveSyncDurationCount: 5