]>
Commit | Line | Data |
---|---|---|
9597920e C |
1 | import { HybridLoaderSettings } from '@peertube/p2p-media-loader-core' |
2 | import { HlsJsEngineSettings } from '@peertube/p2p-media-loader-hlsjs' | |
42b40636 | 3 | import { logger } from '@root-helpers/logger' |
9597920e | 4 | import { LiveVideoLatencyMode } from '@shared/models' |
57d65032 C |
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' | |
9597920e C |
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' | |
9597920e C |
12 | |
13 | export class HLSOptionsBuilder { | |
14 | ||
15 | constructor ( | |
16 | private options: PeertubePlayerManagerOptions, | |
17 | private p2pMediaLoaderModule?: any | |
18 | ) { | |
19 | ||
20 | } | |
21 | ||
22 | getPluginOptions () { | |
23 | const commonOptions = this.options.common | |
24 | ||
25 | const redundancyUrlManager = new RedundancyUrlManager(this.options.p2pMediaLoader.redundancyBaseUrls) | |
26 | ||
27 | const p2pMediaLoaderConfig = this.getP2PMediaLoaderOptions(redundancyUrlManager) | |
28 | const loader = new this.p2pMediaLoaderModule.Engine(p2pMediaLoaderConfig).createLoaderClass() as P2PMediaLoader | |
29 | ||
30 | const p2pMediaLoader: P2PMediaLoaderPluginOptions = { | |
31 | redundancyUrlManager, | |
32 | type: 'application/x-mpegURL', | |
33 | startTime: commonOptions.startTime, | |
34 | src: this.options.p2pMediaLoader.playlistUrl, | |
35 | loader | |
36 | } | |
37 | ||
38 | const hlsjs = { | |
39 | levelLabelHandler: (level: { height: number, width: number }) => { | |
40 | const resolution = Math.min(level.height || 0, level.width || 0) | |
41 | ||
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 | |
45 | ||
46 | let label = file.resolution.label | |
47 | if (file.fps >= 50) label += file.fps | |
48 | ||
49 | return label | |
9597920e C |
50 | } |
51 | } | |
52 | ||
e71e2d8a C |
53 | const html5 = { |
54 | hlsjsConfig: this.getHLSJSOptions(loader) | |
55 | } | |
56 | ||
57 | return { p2pMediaLoader, hlsjs, html5 } | |
9597920e C |
58 | } |
59 | ||
60 | // --------------------------------------------------------------------------- | |
61 | ||
62 | private getP2PMediaLoaderOptions (redundancyUrlManager: RedundancyUrlManager): HlsJsEngineSettings { | |
63 | let consumeOnly = false | |
64 | if ((navigator as any)?.connection?.type === 'cellular') { | |
42b40636 | 65 | logger.info('We are on a cellular connection: disabling seeding.') |
9597920e C |
66 | consumeOnly = true |
67 | } | |
68 | ||
69 | const trackerAnnounce = this.options.p2pMediaLoader.trackerAnnounce | |
70 | .filter(t => t.startsWith('ws')) | |
71 | ||
72 | const specificLiveOrVODOptions = this.options.common.isLive | |
73 | ? this.getP2PMediaLoaderLiveOptions() | |
74 | : this.getP2PMediaLoaderVODOptions() | |
75 | ||
76 | return { | |
77 | loader: { | |
9597920e C |
78 | trackerAnnounce, |
79 | rtcConfig: getRtcConfig(), | |
80 | ||
81 | simultaneousHttpDownloads: 1, | |
82 | httpFailedSegmentTimeout: 1000, | |
83 | ||
84 | segmentValidator: segmentValidatorFactory(this.options.p2pMediaLoader.segmentsSha256Url, this.options.common.isLive), | |
a77c5ff3 | 85 | segmentUrlBuilder: segmentUrlBuilderFactory(redundancyUrlManager), |
9597920e C |
86 | |
87 | useP2P: this.options.common.p2pEnabled, | |
88 | consumeOnly, | |
89 | ||
90 | ...specificLiveOrVODOptions | |
91 | }, | |
92 | segments: { | |
93 | swarmId: this.options.p2pMediaLoader.playlistUrl, | |
023c3ca2 | 94 | forwardSegmentCount: specificLiveOrVODOptions.p2pDownloadMaxPriority ?? 20 |
9597920e C |
95 | } |
96 | } | |
97 | } | |
98 | ||
99 | private getP2PMediaLoaderLiveOptions (): Partial<HybridLoaderSettings> { | |
100 | const base = { | |
101 | requiredSegmentsPriority: 1 | |
102 | } | |
103 | ||
104 | const latencyMode = this.options.common.liveOptions.latencyMode | |
105 | ||
106 | switch (latencyMode) { | |
107 | case LiveVideoLatencyMode.SMALL_LATENCY: | |
108 | return { | |
109 | ...base, | |
110 | ||
111 | useP2P: false, | |
112 | httpDownloadProbability: 1 | |
113 | } | |
114 | ||
115 | case LiveVideoLatencyMode.HIGH_LATENCY: | |
116 | return base | |
117 | ||
118 | default: | |
119 | return base | |
120 | } | |
121 | } | |
122 | ||
123 | private getP2PMediaLoaderVODOptions (): Partial<HybridLoaderSettings> { | |
124 | return { | |
125 | requiredSegmentsPriority: 3, | |
a77c5ff3 | 126 | skipSegmentBuilderPriority: 1, |
9597920e C |
127 | |
128 | cachedSegmentExpiration: 86400000, | |
129 | cachedSegmentsCount: 100, | |
130 | ||
131 | httpDownloadMaxPriority: 9, | |
132 | httpDownloadProbability: 0.06, | |
133 | httpDownloadProbabilitySkipIfNoPeers: true, | |
134 | ||
135 | p2pDownloadMaxPriority: 50 | |
136 | } | |
137 | } | |
138 | ||
139 | // --------------------------------------------------------------------------- | |
140 | ||
141 | private getHLSJSOptions (loader: P2PMediaLoader) { | |
142 | const specificLiveOrVODOptions = this.options.common.isLive | |
143 | ? this.getHLSLiveOptions() | |
144 | : this.getHLSVODOptions() | |
145 | ||
146 | const base = { | |
147 | capLevelToPlayerSize: true, | |
148 | autoStartLoad: false, | |
149 | ||
150 | loader, | |
151 | ||
152 | ...specificLiveOrVODOptions | |
153 | } | |
154 | ||
155 | const averageBandwidth = getAverageBandwidthInStore() | |
156 | if (!averageBandwidth) return base | |
157 | ||
158 | return { | |
159 | ...base, | |
160 | ||
161 | abrEwmaDefaultEstimate: averageBandwidth * 8, // We want bit/s | |
a77c5ff3 | 162 | backBufferLength: 90, |
9597920e C |
163 | startLevel: -1, |
164 | testBandwidth: false, | |
165 | debug: false | |
166 | } | |
167 | } | |
168 | ||
169 | private getHLSLiveOptions () { | |
170 | const latencyMode = this.options.common.liveOptions.latencyMode | |
171 | ||
172 | switch (latencyMode) { | |
173 | case LiveVideoLatencyMode.SMALL_LATENCY: | |
174 | return { | |
175 | liveSyncDurationCount: 2 | |
176 | } | |
177 | ||
178 | case LiveVideoLatencyMode.HIGH_LATENCY: | |
179 | return { | |
180 | liveSyncDurationCount: 10 | |
181 | } | |
182 | ||
183 | default: | |
184 | return { | |
185 | liveSyncDurationCount: 5 | |
186 | } | |
187 | } | |
188 | } | |
189 | ||
190 | private getHLSVODOptions () { | |
191 | return { | |
192 | liveSyncDurationCount: 5 | |
193 | } | |
194 | } | |
195 | } |