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 } 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
23 const commonOptions = this.options.common
25 const redundancyUrlManager = new RedundancyUrlManager(this.options.p2pMediaLoader.redundancyBaseUrls)
27 const p2pMediaLoaderConfig = this.getP2PMediaLoaderOptions(redundancyUrlManager)
28 const loader = new this.p2pMediaLoaderModule.Engine(p2pMediaLoaderConfig).createLoaderClass() as P2PMediaLoader
30 const p2pMediaLoader: P2PMediaLoaderPluginOptions = {
32 type: 'application/x-mpegURL',
33 startTime: commonOptions.startTime,
34 src: this.options.p2pMediaLoader.playlistUrl,
39 levelLabelHandler: (level: { height: number, width: number }) => {
40 const resolution = Math.min(level.height || 0, level.width || 0)
42 const file = this.options.p2pMediaLoader.videoFiles.find(f => f.resolution.id === resolution)
43 // We don't have files for live videos
44 if (!file) return level.height
46 let label = file.resolution.label
47 if (file.fps >= 50) label += file.fps
54 hlsjsConfig: this.getHLSJSOptions(loader)
57 return { p2pMediaLoader, hlsjs, html5 }
60 // ---------------------------------------------------------------------------
62 private getP2PMediaLoaderOptions (redundancyUrlManager: RedundancyUrlManager): HlsJsEngineSettings {
63 let consumeOnly = false
64 if ((navigator as any)?.connection?.type === 'cellular') {
65 logger.info('We are on a cellular connection: disabling seeding.')
69 const trackerAnnounce = this.options.p2pMediaLoader.trackerAnnounce
70 .filter(t => t.startsWith('ws'))
72 const specificLiveOrVODOptions = this.options.common.isLive
73 ? this.getP2PMediaLoaderLiveOptions()
74 : this.getP2PMediaLoaderVODOptions()
79 rtcConfig: getRtcConfig(),
81 simultaneousHttpDownloads: 1,
82 httpFailedSegmentTimeout: 1000,
84 segmentValidator: segmentValidatorFactory(this.options.p2pMediaLoader.segmentsSha256Url, this.options.common.isLive),
85 segmentUrlBuilder: segmentUrlBuilderFactory(redundancyUrlManager),
87 useP2P: this.options.common.p2pEnabled,
90 ...specificLiveOrVODOptions
93 swarmId: this.options.p2pMediaLoader.playlistUrl,
94 forwardSegmentCount: specificLiveOrVODOptions.p2pDownloadMaxPriority ?? 20
99 private getP2PMediaLoaderLiveOptions (): Partial<HybridLoaderSettings> {
101 requiredSegmentsPriority: 1
104 const latencyMode = this.options.common.liveOptions.latencyMode
106 switch (latencyMode) {
107 case LiveVideoLatencyMode.SMALL_LATENCY:
112 httpDownloadProbability: 1
115 case LiveVideoLatencyMode.HIGH_LATENCY:
123 private getP2PMediaLoaderVODOptions (): Partial<HybridLoaderSettings> {
125 requiredSegmentsPriority: 3,
126 skipSegmentBuilderPriority: 1,
128 cachedSegmentExpiration: 86400000,
129 cachedSegmentsCount: 100,
131 httpDownloadMaxPriority: 9,
132 httpDownloadProbability: 0.06,
133 httpDownloadProbabilitySkipIfNoPeers: true,
135 p2pDownloadMaxPriority: 50
139 // ---------------------------------------------------------------------------
141 private getHLSJSOptions (loader: P2PMediaLoader) {
142 const specificLiveOrVODOptions = this.options.common.isLive
143 ? this.getHLSLiveOptions()
144 : this.getHLSVODOptions()
147 capLevelToPlayerSize: true,
148 autoStartLoad: false,
152 ...specificLiveOrVODOptions
155 const averageBandwidth = getAverageBandwidthInStore()
156 if (!averageBandwidth) return base
161 abrEwmaDefaultEstimate: averageBandwidth * 8, // We want bit/s
162 backBufferLength: 90,
164 testBandwidth: false,
169 private getHLSLiveOptions () {
170 const latencyMode = this.options.common.liveOptions.latencyMode
172 switch (latencyMode) {
173 case LiveVideoLatencyMode.SMALL_LATENCY:
175 liveSyncDurationCount: 2
178 case LiveVideoLatencyMode.HIGH_LATENCY:
180 liveSyncDurationCount: 10
185 liveSyncDurationCount: 5
190 private getHLSVODOptions () {
192 liveSyncDurationCount: 5