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, 1),
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,
127 cachedSegmentExpiration: 86400000,
128 cachedSegmentsCount: 100,
130 httpDownloadMaxPriority: 9,
131 httpDownloadProbability: 0.06,
132 httpDownloadProbabilitySkipIfNoPeers: true,
134 p2pDownloadMaxPriority: 50
138 // ---------------------------------------------------------------------------
140 private getHLSJSOptions (loader: P2PMediaLoader) {
141 const specificLiveOrVODOptions = this.options.common.isLive
142 ? this.getHLSLiveOptions()
143 : this.getHLSVODOptions()
146 capLevelToPlayerSize: true,
147 autoStartLoad: false,
151 ...specificLiveOrVODOptions
154 const averageBandwidth = getAverageBandwidthInStore()
155 if (!averageBandwidth) return base
160 abrEwmaDefaultEstimate: averageBandwidth * 8, // We want bit/s
162 testBandwidth: false,
167 private getHLSLiveOptions () {
168 const latencyMode = this.options.common.liveOptions.latencyMode
170 switch (latencyMode) {
171 case LiveVideoLatencyMode.SMALL_LATENCY:
173 liveSyncDurationCount: 2
176 case LiveVideoLatencyMode.HIGH_LATENCY:
178 liveSyncDurationCount: 10
183 liveSyncDurationCount: 5
188 private getHLSVODOptions () {
190 liveSyncDurationCount: 5