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
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 segmentValidator: segmentValidatorFactory(this.options.p2pMediaLoader.segmentsSha256Url, this.options.common.isLive),
88 segmentUrlBuilder: segmentUrlBuilderFactory(redundancyUrlManager),
90 useP2P: this.options.common.p2pEnabled,
93 ...specificLiveOrVODOptions
96 swarmId: this.options.p2pMediaLoader.playlistUrl,
97 forwardSegmentCount: specificLiveOrVODOptions.p2pDownloadMaxPriority ?? 20
102 private getP2PMediaLoaderLiveOptions (): Partial<HybridLoaderSettings> {
104 requiredSegmentsPriority: 1
107 const latencyMode = this.options.common.liveOptions.latencyMode
109 switch (latencyMode) {
110 case LiveVideoLatencyMode.SMALL_LATENCY:
115 httpDownloadProbability: 1
118 case LiveVideoLatencyMode.HIGH_LATENCY:
126 private getP2PMediaLoaderVODOptions (): Partial<HybridLoaderSettings> {
128 requiredSegmentsPriority: 3,
129 skipSegmentBuilderPriority: 1,
131 cachedSegmentExpiration: 86400000,
132 cachedSegmentsCount: 100,
134 httpDownloadMaxPriority: 9,
135 httpDownloadProbability: 0.06,
136 httpDownloadProbabilitySkipIfNoPeers: true,
138 p2pDownloadMaxPriority: 50
142 // ---------------------------------------------------------------------------
144 private getHLSJSOptions (loader: P2PMediaLoader) {
145 const specificLiveOrVODOptions = this.options.common.isLive
146 ? this.getHLSLiveOptions()
147 : this.getHLSVODOptions()
150 capLevelToPlayerSize: true,
151 autoStartLoad: false,
155 ...specificLiveOrVODOptions
158 const averageBandwidth = getAverageBandwidthInStore()
159 if (!averageBandwidth) return base
164 abrEwmaDefaultEstimate: averageBandwidth * 8, // We want bit/s
165 backBufferLength: 90,
167 testBandwidth: false,
172 private getHLSLiveOptions () {
173 const latencyMode = this.options.common.liveOptions.latencyMode
175 switch (latencyMode) {
176 case LiveVideoLatencyMode.SMALL_LATENCY:
178 liveSyncDurationCount: 2
181 case LiveVideoLatencyMode.HIGH_LATENCY:
183 liveSyncDurationCount: 10
188 liveSyncDurationCount: 5
193 private getHLSVODOptions () {
195 liveSyncDurationCount: 5