]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/assets/player/shared/manager-options/hls-options-builder.ts
Prevent invalid end watch section warnings
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / shared / manager-options / hls-options-builder.ts
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'
12
13 export class HLSOptionsBuilder {
14
15 constructor (
16 private options: PeertubePlayerManagerOptions,
17 private p2pMediaLoaderModule?: any
18 ) {
19
20 }
21
22 async getPluginOptions () {
23 const commonOptions = this.options.common
24
25 const redundancyUrlManager = new RedundancyUrlManager(this.options.p2pMediaLoader.redundancyBaseUrls)
26
27 const p2pMediaLoaderConfig = await this.options.pluginsManager.runHook(
28 'filter:internal.player.p2p-media-loader.options.result',
29 this.getP2PMediaLoaderOptions(redundancyUrlManager)
30 )
31 const loader = new this.p2pMediaLoaderModule.Engine(p2pMediaLoaderConfig).createLoaderClass() as P2PMediaLoader
32
33 const p2pMediaLoader: P2PMediaLoaderPluginOptions = {
34 requiresAuth: commonOptions.requiresAuth,
35 videoFileToken: commonOptions.videoFileToken,
36
37 redundancyUrlManager,
38 type: 'application/x-mpegURL',
39 startTime: commonOptions.startTime,
40 src: this.options.p2pMediaLoader.playlistUrl,
41 loader
42 }
43
44 const hlsjs = {
45 levelLabelHandler: (level: { height: number, width: number }) => {
46 const resolution = Math.min(level.height || 0, level.width || 0)
47
48 const file = this.options.p2pMediaLoader.videoFiles.find(f => f.resolution.id === resolution)
49 // We don't have files for live videos
50 if (!file) return level.height
51
52 let label = file.resolution.label
53 if (file.fps >= 50) label += file.fps
54
55 return label
56 }
57 }
58
59 const html5 = {
60 hlsjsConfig: this.getHLSJSOptions(loader)
61 }
62
63 return { p2pMediaLoader, hlsjs, html5 }
64 }
65
66 // ---------------------------------------------------------------------------
67
68 private getP2PMediaLoaderOptions (redundancyUrlManager: RedundancyUrlManager): HlsJsEngineSettings {
69 let consumeOnly = false
70 if ((navigator as any)?.connection?.type === 'cellular') {
71 logger.info('We are on a cellular connection: disabling seeding.')
72 consumeOnly = true
73 }
74
75 const trackerAnnounce = this.options.p2pMediaLoader.trackerAnnounce
76 .filter(t => t.startsWith('ws'))
77
78 const specificLiveOrVODOptions = this.options.common.isLive
79 ? this.getP2PMediaLoaderLiveOptions()
80 : this.getP2PMediaLoaderVODOptions()
81
82 return {
83 loader: {
84 trackerAnnounce,
85 rtcConfig: getRtcConfig(),
86
87 simultaneousHttpDownloads: 1,
88 httpFailedSegmentTimeout: 1000,
89
90 xhrSetup: (xhr, url) => {
91 if (!this.options.common.requiresAuth) return
92 if (!isSameOrigin(this.options.common.serverUrl, url)) return
93
94 xhr.setRequestHeader('Authorization', this.options.common.authorizationHeader())
95 },
96
97 segmentValidator: segmentValidatorFactory({
98 segmentsSha256Url: this.options.p2pMediaLoader.segmentsSha256Url,
99 authorizationHeader: this.options.common.authorizationHeader,
100 requiresAuth: this.options.common.requiresAuth,
101 serverUrl: this.options.common.serverUrl
102 }),
103
104 segmentUrlBuilder: segmentUrlBuilderFactory(redundancyUrlManager),
105
106 useP2P: this.options.common.p2pEnabled,
107 consumeOnly,
108
109 ...specificLiveOrVODOptions
110 },
111 segments: {
112 swarmId: this.options.p2pMediaLoader.playlistUrl,
113 forwardSegmentCount: specificLiveOrVODOptions.p2pDownloadMaxPriority ?? 20
114 }
115 }
116 }
117
118 private getP2PMediaLoaderLiveOptions (): Partial<HybridLoaderSettings> {
119 const base = {
120 requiredSegmentsPriority: 1
121 }
122
123 const latencyMode = this.options.common.liveOptions.latencyMode
124
125 switch (latencyMode) {
126 case LiveVideoLatencyMode.SMALL_LATENCY:
127 return {
128 ...base,
129
130 useP2P: false,
131 requiredSegmentsPriority: 10
132 }
133
134 case LiveVideoLatencyMode.HIGH_LATENCY:
135 return base
136
137 default:
138 return base
139 }
140 }
141
142 private getP2PMediaLoaderVODOptions (): Partial<HybridLoaderSettings> {
143 return {
144 requiredSegmentsPriority: 3,
145 skipSegmentBuilderPriority: 1,
146
147 cachedSegmentExpiration: 86400000,
148 cachedSegmentsCount: 100,
149
150 httpDownloadMaxPriority: 9,
151 httpDownloadProbability: 0.06,
152 httpDownloadProbabilitySkipIfNoPeers: true,
153
154 p2pDownloadMaxPriority: 50
155 }
156 }
157
158 // ---------------------------------------------------------------------------
159
160 private getHLSJSOptions (loader: P2PMediaLoader) {
161 const specificLiveOrVODOptions = this.options.common.isLive
162 ? this.getHLSLiveOptions()
163 : this.getHLSVODOptions()
164
165 const base = {
166 capLevelToPlayerSize: true,
167 autoStartLoad: false,
168
169 loader,
170
171 ...specificLiveOrVODOptions
172 }
173
174 const averageBandwidth = getAverageBandwidthInStore()
175 if (!averageBandwidth) return base
176
177 return {
178 ...base,
179
180 abrEwmaDefaultEstimate: averageBandwidth * 8, // We want bit/s
181 backBufferLength: 90,
182 startLevel: -1,
183 testBandwidth: false,
184 debug: false
185 }
186 }
187
188 private getHLSLiveOptions () {
189 const latencyMode = this.options.common.liveOptions.latencyMode
190
191 switch (latencyMode) {
192 case LiveVideoLatencyMode.SMALL_LATENCY:
193 return {
194 liveSyncDurationCount: 2
195 }
196
197 case LiveVideoLatencyMode.HIGH_LATENCY:
198 return {
199 liveSyncDurationCount: 10
200 }
201
202 default:
203 return {
204 liveSyncDurationCount: 5
205 }
206 }
207 }
208
209 private getHLSVODOptions () {
210 return {
211 liveSyncDurationCount: 5
212 }
213 }
214 }