aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/assets/player/p2p-media-loader
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/assets/player/p2p-media-loader')
-rw-r--r--client/src/assets/player/p2p-media-loader/hls-plugin.ts419
-rw-r--r--client/src/assets/player/p2p-media-loader/p2p-media-loader-plugin.ts183
-rw-r--r--client/src/assets/player/p2p-media-loader/redundancy-url-manager.ts42
-rw-r--r--client/src/assets/player/p2p-media-loader/segment-url-builder.ts17
-rw-r--r--client/src/assets/player/p2p-media-loader/segment-validator.ts106
5 files changed, 0 insertions, 767 deletions
diff --git a/client/src/assets/player/p2p-media-loader/hls-plugin.ts b/client/src/assets/player/p2p-media-loader/hls-plugin.ts
deleted file mode 100644
index ccee2d90f..000000000
--- a/client/src/assets/player/p2p-media-loader/hls-plugin.ts
+++ /dev/null
@@ -1,419 +0,0 @@
1// Thanks https://github.com/streamroot/videojs-hlsjs-plugin
2// We duplicated this plugin to choose the hls.js version we want, because streamroot only provide a bundled file
3
4import Hlsjs, { ErrorData, HlsConfig, Level, LevelSwitchingData, ManifestParsedData } from 'hls.js'
5import videojs from 'video.js'
6import { HlsjsConfigHandlerOptions, PeerTubeResolution, VideoJSTechHLS } from '../peertube-videojs-typings'
7
8type ErrorCounts = {
9 [ type: string ]: number
10}
11
12type Metadata = {
13 levels: Level[]
14}
15
16type HookFn = (player: videojs.Player, hljs: Hlsjs) => void
17
18const registerSourceHandler = function (vjs: typeof videojs) {
19 if (!Hlsjs.isSupported()) {
20 console.warn('Hls.js is not supported in this browser!')
21 return
22 }
23
24 const html5 = vjs.getTech('Html5')
25
26 if (!html5) {
27 console.error('No Hml5 tech found in videojs')
28 return
29 }
30
31 // FIXME: typings
32 (html5 as any).registerSourceHandler({
33 canHandleSource: function (source: videojs.Tech.SourceObject) {
34 const hlsTypeRE = /^application\/x-mpegURL|application\/vnd\.apple\.mpegurl$/i
35 const hlsExtRE = /\.m3u8/i
36
37 if (hlsTypeRE.test(source.type)) return 'probably'
38 if (hlsExtRE.test(source.src)) return 'maybe'
39
40 return ''
41 },
42
43 handleSource: function (source: videojs.Tech.SourceObject, tech: VideoJSTechHLS) {
44 if (tech.hlsProvider) {
45 tech.hlsProvider.dispose()
46 }
47
48 tech.hlsProvider = new Html5Hlsjs(vjs, source, tech)
49
50 return tech.hlsProvider
51 }
52 }, 0);
53
54 // FIXME: typings
55 (vjs as any).Html5Hlsjs = Html5Hlsjs
56}
57
58function hlsjsConfigHandler (this: videojs.Player, options: HlsjsConfigHandlerOptions) {
59 const player = this
60
61 if (!options) return
62
63 if (!player.srOptions_) {
64 player.srOptions_ = {}
65 }
66
67 if (!player.srOptions_.hlsjsConfig) {
68 player.srOptions_.hlsjsConfig = options.hlsjsConfig
69 }
70
71 if (options.levelLabelHandler && !player.srOptions_.levelLabelHandler) {
72 player.srOptions_.levelLabelHandler = options.levelLabelHandler
73 }
74}
75
76const registerConfigPlugin = function (vjs: typeof videojs) {
77 // Used in Brightcove since we don't pass options directly there
78 const registerVjsPlugin = vjs.registerPlugin || vjs.plugin
79 registerVjsPlugin('hlsjs', hlsjsConfigHandler)
80}
81
82class Html5Hlsjs {
83 private static readonly hooks: { [id: string]: HookFn[] } = {}
84
85 private readonly videoElement: HTMLVideoElement
86 private readonly errorCounts: ErrorCounts = {}
87 private readonly player: videojs.Player
88 private readonly tech: videojs.Tech
89 private readonly source: videojs.Tech.SourceObject
90 private readonly vjs: typeof videojs
91
92 private maxNetworkErrorRecovery = 5
93
94 private hls: Hlsjs
95 private hlsjsConfig: Partial<HlsConfig & { cueHandler: any }> = null
96
97 private _duration: number = null
98 private metadata: Metadata = null
99 private isLive: boolean = null
100 private dvrDuration: number = null
101 private edgeMargin: number = null
102
103 private handlers: { [ id in 'play' ]: EventListener } = {
104 play: null
105 }
106
107 constructor (vjs: typeof videojs, source: videojs.Tech.SourceObject, tech: videojs.Tech) {
108 this.vjs = vjs
109 this.source = source
110
111 this.tech = tech;
112 (this.tech as any).name_ = 'Hlsjs'
113
114 this.videoElement = tech.el() as HTMLVideoElement
115 this.player = vjs((tech.options_ as any).playerId)
116
117 this.videoElement.addEventListener('error', event => {
118 let errorTxt: string
119 const mediaError = ((event.currentTarget || event.target) as HTMLVideoElement).error
120
121 if (!mediaError) return
122
123 console.log(mediaError)
124 switch (mediaError.code) {
125 case mediaError.MEDIA_ERR_ABORTED:
126 errorTxt = 'You aborted the video playback'
127 break
128 case mediaError.MEDIA_ERR_DECODE:
129 errorTxt = 'The video playback was aborted due to a corruption problem or because the video used features ' +
130 'your browser did not support'
131 this._handleMediaError(mediaError)
132 break
133 case mediaError.MEDIA_ERR_NETWORK:
134 errorTxt = 'A network error caused the video download to fail part-way'
135 break
136 case mediaError.MEDIA_ERR_SRC_NOT_SUPPORTED:
137 errorTxt = 'The video could not be loaded, either because the server or network failed or because the format is not supported'
138 break
139
140 default:
141 errorTxt = mediaError.message
142 }
143
144 console.error('MEDIA_ERROR: ', errorTxt)
145 })
146
147 this.initialize()
148 }
149
150 duration () {
151 if (this._duration === Infinity) return Infinity
152 if (!isNaN(this.videoElement.duration)) return this.videoElement.duration
153
154 return this._duration || 0
155 }
156
157 seekable () {
158 if (this.hls.media) {
159 if (!this.isLive) {
160 return this.vjs.createTimeRanges(0, this.hls.media.duration)
161 }
162
163 // Video.js doesn't seem to like floating point timeranges
164 const startTime = Math.round(this.hls.media.duration - this.dvrDuration)
165 const endTime = Math.round(this.hls.media.duration - this.edgeMargin)
166
167 return this.vjs.createTimeRanges(startTime, endTime)
168 }
169
170 return this.vjs.createTimeRanges()
171 }
172
173 // See comment for `initialize` method.
174 dispose () {
175 this.videoElement.removeEventListener('play', this.handlers.play)
176
177 // FIXME: https://github.com/video-dev/hls.js/issues/4092
178 const untypedHLS = this.hls as any
179 untypedHLS.log = untypedHLS.warn = () => {
180 // empty
181 }
182
183 this.hls.destroy()
184 }
185
186 static addHook (type: string, callback: HookFn) {
187 Html5Hlsjs.hooks[type] = this.hooks[type] || []
188 Html5Hlsjs.hooks[type].push(callback)
189 }
190
191 static removeHook (type: string, callback: HookFn) {
192 if (Html5Hlsjs.hooks[type] === undefined) return false
193
194 const index = Html5Hlsjs.hooks[type].indexOf(callback)
195 if (index === -1) return false
196
197 Html5Hlsjs.hooks[type].splice(index, 1)
198
199 return true
200 }
201
202 private _executeHooksFor (type: string) {
203 if (Html5Hlsjs.hooks[type] === undefined) {
204 return
205 }
206
207 // ES3 and IE < 9
208 for (let i = 0; i < Html5Hlsjs.hooks[type].length; i++) {
209 Html5Hlsjs.hooks[type][i](this.player, this.hls)
210 }
211 }
212
213 private _handleMediaError (error: any) {
214 if (this.errorCounts[Hlsjs.ErrorTypes.MEDIA_ERROR] === 1) {
215 console.info('trying to recover media error')
216 this.hls.recoverMediaError()
217 return
218 }
219
220 if (this.errorCounts[Hlsjs.ErrorTypes.MEDIA_ERROR] === 2) {
221 console.info('2nd try to recover media error (by swapping audio codec')
222 this.hls.swapAudioCodec()
223 this.hls.recoverMediaError()
224 return
225 }
226
227 if (this.errorCounts[Hlsjs.ErrorTypes.MEDIA_ERROR] > 2) {
228 console.info('bubbling media error up to VIDEOJS')
229 this.hls.destroy()
230 this.tech.error = () => error
231 this.tech.trigger('error')
232 }
233 }
234
235 private _handleNetworkError (error: any) {
236 if (this.errorCounts[Hlsjs.ErrorTypes.NETWORK_ERROR] <= this.maxNetworkErrorRecovery) {
237 console.info('trying to recover network error')
238
239 // Wait 1 second and retry
240 setTimeout(() => this.hls.startLoad(), 1000)
241
242 // Reset error count on success
243 this.hls.once(Hlsjs.Events.FRAG_LOADED, () => {
244 this.errorCounts[Hlsjs.ErrorTypes.NETWORK_ERROR] = 0
245 })
246
247 return
248 }
249
250 console.info('bubbling network error up to VIDEOJS')
251 this.hls.destroy()
252 this.tech.error = () => error
253 this.tech.trigger('error')
254 }
255
256 private _onError (_event: any, data: ErrorData) {
257 const error: { message: string, code?: number } = {
258 message: `HLS.js error: ${data.type} - fatal: ${data.fatal} - ${data.details}`
259 }
260
261 // increment/set error count
262 if (this.errorCounts[data.type]) this.errorCounts[data.type] += 1
263 else this.errorCounts[data.type] = 1
264
265 if (data.fatal) console.warn(error.message)
266 else console.error(error.message, data)
267
268 if (data.type === Hlsjs.ErrorTypes.NETWORK_ERROR) {
269 error.code = 2
270 this._handleNetworkError(error)
271 } else if (data.fatal && data.type === Hlsjs.ErrorTypes.MEDIA_ERROR && data.details !== 'manifestIncompatibleCodecsError') {
272 error.code = 3
273 this._handleMediaError(error)
274 } else if (data.fatal) {
275 this.hls.destroy()
276 console.info('bubbling error up to VIDEOJS')
277 this.tech.error = () => error as any
278 this.tech.trigger('error')
279 }
280 }
281
282 private buildLevelLabel (level: Level) {
283 if (this.player.srOptions_.levelLabelHandler) {
284 return this.player.srOptions_.levelLabelHandler(level as any)
285 }
286
287 if (level.height) return level.height + 'p'
288 if (level.width) return Math.round(level.width * 9 / 16) + 'p'
289 if (level.bitrate) return (level.bitrate / 1000) + 'kbps'
290
291 return '0'
292 }
293
294 private _notifyVideoQualities () {
295 if (!this.metadata) return
296
297 const resolutions: PeerTubeResolution[] = []
298
299 this.metadata.levels.forEach((level, index) => {
300 resolutions.push({
301 id: index,
302 height: level.height,
303 width: level.width,
304 bitrate: level.bitrate,
305 label: this.buildLevelLabel(level),
306 selected: level.id === this.hls.manualLevel,
307
308 selectCallback: () => {
309 this.hls.currentLevel = index
310 }
311 })
312 })
313
314 resolutions.push({
315 id: -1,
316 label: this.player.localize('Auto'),
317 selected: true,
318 selectCallback: () => this.hls.currentLevel = -1
319 })
320
321 this.player.peertubeResolutions().add(resolutions)
322 }
323
324 private _startLoad () {
325 this.hls.startLoad(-1)
326 this.videoElement.removeEventListener('play', this.handlers.play)
327 }
328
329 private _oneLevelObjClone (obj: { [ id: string ]: any }) {
330 const result = {}
331 const objKeys = Object.keys(obj)
332 for (let i = 0; i < objKeys.length; i++) {
333 result[objKeys[i]] = obj[objKeys[i]]
334 }
335
336 return result
337 }
338
339 private _onMetaData (_event: any, data: ManifestParsedData) {
340 // This could arrive before 'loadedqualitydata' handlers is registered, remember it so we can raise it later
341 this.metadata = data
342 this._notifyVideoQualities()
343 }
344
345 private _initHlsjs () {
346 const techOptions = this.tech.options_ as HlsjsConfigHandlerOptions
347 const srOptions_ = this.player.srOptions_
348
349 const hlsjsConfigRef = srOptions_?.hlsjsConfig || techOptions.hlsjsConfig
350 // Hls.js will write to the reference thus change the object for later streams
351 this.hlsjsConfig = hlsjsConfigRef ? this._oneLevelObjClone(hlsjsConfigRef) : {}
352
353 if ([ '', 'auto' ].includes(this.videoElement.preload) && !this.videoElement.autoplay && this.hlsjsConfig.autoStartLoad === undefined) {
354 this.hlsjsConfig.autoStartLoad = false
355 }
356
357 // If the user explicitly sets autoStartLoad to false, we're not going to enter the if block above
358 // That's why we have a separate if block here to set the 'play' listener
359 if (this.hlsjsConfig.autoStartLoad === false) {
360 this.handlers.play = this._startLoad.bind(this)
361 this.videoElement.addEventListener('play', this.handlers.play)
362 }
363
364 this.hls = new Hlsjs(this.hlsjsConfig)
365
366 this._executeHooksFor('beforeinitialize')
367
368 this.hls.on(Hlsjs.Events.ERROR, (event, data) => this._onError(event, data))
369 this.hls.on(Hlsjs.Events.MANIFEST_PARSED, (event, data) => this._onMetaData(event, data))
370 this.hls.on(Hlsjs.Events.LEVEL_LOADED, (event, data) => {
371 // The DVR plugin will auto seek to "live edge" on start up
372 if (this.hlsjsConfig.liveSyncDuration) {
373 this.edgeMargin = this.hlsjsConfig.liveSyncDuration
374 } else if (this.hlsjsConfig.liveSyncDurationCount) {
375 this.edgeMargin = this.hlsjsConfig.liveSyncDurationCount * data.details.targetduration
376 }
377
378 this.isLive = data.details.live
379 this.dvrDuration = data.details.totalduration
380
381 this._duration = this.isLive ? Infinity : data.details.totalduration
382
383 // Increase network error recovery for lives since they can be broken (server restart, stream interruption etc)
384 if (this.isLive) this.maxNetworkErrorRecovery = 300
385 })
386
387 this.hls.once(Hlsjs.Events.FRAG_LOADED, () => {
388 // Emit custom 'loadedmetadata' event for parity with `videojs-contrib-hls`
389 // Ref: https://github.com/videojs/videojs-contrib-hls#loadedmetadata
390 this.tech.trigger('loadedmetadata')
391 })
392
393 this.hls.on(Hlsjs.Events.LEVEL_SWITCHING, (_e, data: LevelSwitchingData) => {
394 const resolutionId = this.hls.autoLevelEnabled
395 ? -1
396 : data.level
397
398 const autoResolutionChosenId = this.hls.autoLevelEnabled
399 ? data.level
400 : -1
401
402 this.player.peertubeResolutions().select({ id: resolutionId, autoResolutionChosenId, byEngine: true })
403 })
404
405 this.hls.attachMedia(this.videoElement)
406
407 this.hls.loadSource(this.source.src)
408 }
409
410 private initialize () {
411 this._initHlsjs()
412 }
413}
414
415export {
416 Html5Hlsjs,
417 registerSourceHandler,
418 registerConfigPlugin
419}
diff --git a/client/src/assets/player/p2p-media-loader/p2p-media-loader-plugin.ts b/client/src/assets/player/p2p-media-loader/p2p-media-loader-plugin.ts
deleted file mode 100644
index 1d7a39b4e..000000000
--- a/client/src/assets/player/p2p-media-loader/p2p-media-loader-plugin.ts
+++ /dev/null
@@ -1,183 +0,0 @@
1import Hlsjs from 'hls.js'
2import videojs from 'video.js'
3import { Events, Segment } from '@peertube/p2p-media-loader-core'
4import { Engine, initHlsJsPlayer, initVideoJsContribHlsJsPlayer } from '@peertube/p2p-media-loader-hlsjs'
5import { timeToInt } from '@shared/core-utils'
6import { P2PMediaLoaderPluginOptions, PlayerNetworkInfo } from '../peertube-videojs-typings'
7import { registerConfigPlugin, registerSourceHandler } from './hls-plugin'
8
9registerConfigPlugin(videojs)
10registerSourceHandler(videojs)
11
12const Plugin = videojs.getPlugin('plugin')
13class P2pMediaLoaderPlugin extends Plugin {
14
15 private readonly CONSTANTS = {
16 INFO_SCHEDULER: 1000 // Don't change this
17 }
18 private readonly options: P2PMediaLoaderPluginOptions
19
20 private hlsjs: Hlsjs
21 private p2pEngine: Engine
22 private statsP2PBytes = {
23 pendingDownload: [] as number[],
24 pendingUpload: [] as number[],
25 numPeers: 0,
26 totalDownload: 0,
27 totalUpload: 0
28 }
29 private statsHTTPBytes = {
30 pendingDownload: [] as number[],
31 pendingUpload: [] as number[],
32 totalDownload: 0,
33 totalUpload: 0
34 }
35 private startTime: number
36
37 private networkInfoInterval: any
38
39 constructor (player: videojs.Player, options?: P2PMediaLoaderPluginOptions) {
40 super(player)
41
42 this.options = options
43
44 // FIXME: typings https://github.com/Microsoft/TypeScript/issues/14080
45 if (!(videojs as any).Html5Hlsjs) {
46 console.warn('HLS.js does not seem to be supported. Try to fallback to built in HLS.')
47
48 if (!player.canPlayType('application/vnd.apple.mpegurl')) {
49 const message = 'Cannot fallback to built-in HLS'
50 console.warn(message)
51
52 player.ready(() => player.trigger('error', new Error(message)))
53 return
54 }
55 } else {
56 // FIXME: typings https://github.com/Microsoft/TypeScript/issues/14080
57 (videojs as any).Html5Hlsjs.addHook('beforeinitialize', (videojsPlayer: any, hlsjs: any) => {
58 this.hlsjs = hlsjs
59 })
60
61 initVideoJsContribHlsJsPlayer(player)
62 }
63
64 this.startTime = timeToInt(options.startTime)
65
66 player.src({
67 type: options.type,
68 src: options.src
69 })
70
71 player.ready(() => {
72 this.initializeCore()
73
74 if ((videojs as any).Html5Hlsjs) {
75 this.initializePlugin()
76 }
77 })
78 }
79
80 dispose () {
81 if (this.hlsjs) this.hlsjs.destroy()
82 if (this.p2pEngine) this.p2pEngine.destroy()
83
84 clearInterval(this.networkInfoInterval)
85 }
86
87 getCurrentLevel () {
88 return this.hlsjs.levels[this.hlsjs.currentLevel]
89 }
90
91 getLiveLatency () {
92 return Math.round(this.hlsjs.latency)
93 }
94
95 getHLSJS () {
96 return this.hlsjs
97 }
98
99 private initializeCore () {
100 this.player.one('play', () => {
101 this.player.addClass('vjs-has-big-play-button-clicked')
102 })
103
104 this.player.one('canplay', () => {
105 if (this.startTime) {
106 this.player.currentTime(this.startTime)
107 }
108 })
109 }
110
111 private initializePlugin () {
112 initHlsJsPlayer(this.hlsjs)
113
114 this.p2pEngine = this.options.loader.getEngine()
115
116 this.p2pEngine.on(Events.SegmentError, (segment: Segment, err) => {
117 console.error('Segment error.', segment, err)
118
119 this.options.redundancyUrlManager.removeBySegmentUrl(segment.requestUrl)
120 })
121
122 this.statsP2PBytes.numPeers = 1 + this.options.redundancyUrlManager.countBaseUrls()
123
124 this.runStats()
125 }
126
127 private runStats () {
128 this.p2pEngine.on(Events.PieceBytesDownloaded, (method: string, _segment, bytes: number) => {
129 const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes
130
131 elem.pendingDownload.push(bytes)
132 elem.totalDownload += bytes
133 })
134
135 this.p2pEngine.on(Events.PieceBytesUploaded, (method: string, _segment, bytes: number) => {
136 const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes
137
138 elem.pendingUpload.push(bytes)
139 elem.totalUpload += bytes
140 })
141
142 this.p2pEngine.on(Events.PeerConnect, () => this.statsP2PBytes.numPeers++)
143 this.p2pEngine.on(Events.PeerClose, () => this.statsP2PBytes.numPeers--)
144
145 this.networkInfoInterval = setInterval(() => {
146 const p2pDownloadSpeed = this.arraySum(this.statsP2PBytes.pendingDownload)
147 const p2pUploadSpeed = this.arraySum(this.statsP2PBytes.pendingUpload)
148
149 const httpDownloadSpeed = this.arraySum(this.statsHTTPBytes.pendingDownload)
150 const httpUploadSpeed = this.arraySum(this.statsHTTPBytes.pendingUpload)
151
152 this.statsP2PBytes.pendingDownload = []
153 this.statsP2PBytes.pendingUpload = []
154 this.statsHTTPBytes.pendingDownload = []
155 this.statsHTTPBytes.pendingUpload = []
156
157 return this.player.trigger('p2pInfo', {
158 source: 'p2p-media-loader',
159 http: {
160 downloadSpeed: httpDownloadSpeed,
161 uploadSpeed: httpUploadSpeed,
162 downloaded: this.statsHTTPBytes.totalDownload,
163 uploaded: this.statsHTTPBytes.totalUpload
164 },
165 p2p: {
166 downloadSpeed: p2pDownloadSpeed,
167 uploadSpeed: p2pUploadSpeed,
168 numPeers: this.statsP2PBytes.numPeers,
169 downloaded: this.statsP2PBytes.totalDownload,
170 uploaded: this.statsP2PBytes.totalUpload
171 },
172 bandwidthEstimate: (this.hlsjs as any).bandwidthEstimate / 8
173 } as PlayerNetworkInfo)
174 }, this.CONSTANTS.INFO_SCHEDULER)
175 }
176
177 private arraySum (data: number[]) {
178 return data.reduce((a: number, b: number) => a + b, 0)
179 }
180}
181
182videojs.registerPlugin('p2pMediaLoader', P2pMediaLoaderPlugin)
183export { P2pMediaLoaderPlugin }
diff --git a/client/src/assets/player/p2p-media-loader/redundancy-url-manager.ts b/client/src/assets/player/p2p-media-loader/redundancy-url-manager.ts
deleted file mode 100644
index abab8aa99..000000000
--- a/client/src/assets/player/p2p-media-loader/redundancy-url-manager.ts
+++ /dev/null
@@ -1,42 +0,0 @@
1import { basename, dirname } from 'path'
2
3class RedundancyUrlManager {
4
5 constructor (private baseUrls: string[] = []) {
6 // empty
7 }
8
9 removeBySegmentUrl (segmentUrl: string) {
10 console.log('Removing redundancy of segment URL %s.', segmentUrl)
11
12 const baseUrl = dirname(segmentUrl)
13
14 this.baseUrls = this.baseUrls.filter(u => u !== baseUrl && u !== baseUrl + '/')
15 }
16
17 buildUrl (url: string) {
18 const max = this.baseUrls.length + 1
19 const i = this.getRandomInt(max)
20
21 if (i === max - 1) return url
22
23 const newBaseUrl = this.baseUrls[i]
24 const slashPart = newBaseUrl.endsWith('/') ? '' : '/'
25
26 return newBaseUrl + slashPart + basename(url)
27 }
28
29 countBaseUrls () {
30 return this.baseUrls.length
31 }
32
33 private getRandomInt (max: number) {
34 return Math.floor(Math.random() * Math.floor(max))
35 }
36}
37
38// ---------------------------------------------------------------------------
39
40export {
41 RedundancyUrlManager
42}
diff --git a/client/src/assets/player/p2p-media-loader/segment-url-builder.ts b/client/src/assets/player/p2p-media-loader/segment-url-builder.ts
deleted file mode 100644
index 9d324078a..000000000
--- a/client/src/assets/player/p2p-media-loader/segment-url-builder.ts
+++ /dev/null
@@ -1,17 +0,0 @@
1import { Segment } from '@peertube/p2p-media-loader-core'
2import { RedundancyUrlManager } from './redundancy-url-manager'
3
4function segmentUrlBuilderFactory (redundancyUrlManager: RedundancyUrlManager, useOriginPriority: number) {
5 return function segmentBuilder (segment: Segment) {
6 // Don't use redundancy for high priority segments
7 if (segment.priority <= useOriginPriority) return segment.url
8
9 return redundancyUrlManager.buildUrl(segment.url)
10 }
11}
12
13// ---------------------------------------------------------------------------
14
15export {
16 segmentUrlBuilderFactory
17}
diff --git a/client/src/assets/player/p2p-media-loader/segment-validator.ts b/client/src/assets/player/p2p-media-loader/segment-validator.ts
deleted file mode 100644
index f7f83a8a4..000000000
--- a/client/src/assets/player/p2p-media-loader/segment-validator.ts
+++ /dev/null
@@ -1,106 +0,0 @@
1import { wait } from '@root-helpers/utils'
2import { Segment } from '@peertube/p2p-media-loader-core'
3import { basename } from 'path'
4
5type SegmentsJSON = { [filename: string]: string | { [byterange: string]: string } }
6
7const maxRetries = 3
8
9function segmentValidatorFactory (segmentsSha256Url: string, isLive: boolean) {
10 let segmentsJSON = fetchSha256Segments(segmentsSha256Url)
11 const regex = /bytes=(\d+)-(\d+)/
12
13 return async function segmentValidator (segment: Segment, _method: string, _peerId: string, retry = 1) {
14 // Wait for hash generation from the server
15 if (isLive) await wait(1000)
16
17 const filename = basename(segment.url)
18
19 const segmentValue = (await segmentsJSON)[filename]
20
21 if (!segmentValue && retry > maxRetries) {
22 throw new Error(`Unknown segment name ${filename} in segment validator`)
23 }
24
25 if (!segmentValue) {
26 console.log('Refetching sha segments for %s.', filename)
27
28 await wait(1000)
29
30 segmentsJSON = fetchSha256Segments(segmentsSha256Url)
31 await segmentValidator(segment, _method, _peerId, retry + 1)
32
33 return
34 }
35
36 let hashShouldBe: string
37 let range = ''
38
39 if (typeof segmentValue === 'string') {
40 hashShouldBe = segmentValue
41 } else {
42 const captured = regex.exec(segment.range)
43 range = captured[1] + '-' + captured[2]
44
45 hashShouldBe = segmentValue[range]
46 }
47
48 if (hashShouldBe === undefined) {
49 throw new Error(`Unknown segment name ${filename}/${range} in segment validator`)
50 }
51
52 const calculatedSha = await sha256Hex(segment.data)
53 if (calculatedSha !== hashShouldBe) {
54 throw new Error(
55 `Hashes does not correspond for segment ${filename}/${range}` +
56 `(expected: ${hashShouldBe} instead of ${calculatedSha})`
57 )
58 }
59 }
60}
61
62// ---------------------------------------------------------------------------
63
64export {
65 segmentValidatorFactory
66}
67
68// ---------------------------------------------------------------------------
69
70function fetchSha256Segments (url: string) {
71 return fetch(url)
72 .then(res => res.json() as Promise<SegmentsJSON>)
73 .catch(err => {
74 console.error('Cannot get sha256 segments', err)
75 return {}
76 })
77}
78
79async function sha256Hex (data?: ArrayBuffer) {
80 if (!data) return undefined
81
82 if (window.crypto.subtle) {
83 return window.crypto.subtle.digest('SHA-256', data)
84 .then(data => bufferToHex(data))
85 }
86
87 // Fallback for non HTTPS context
88 const shaModule = (await import('sha.js') as any).default
89 // eslint-disable-next-line new-cap
90 return new shaModule.sha256().update(Buffer.from(data)).digest('hex')
91}
92
93// Thanks: https://stackoverflow.com/a/53307879
94function bufferToHex (buffer?: ArrayBuffer) {
95 if (!buffer) return ''
96
97 let s = ''
98 const h = '0123456789abcdef'
99 const o = new Uint8Array(buffer)
100
101 o.forEach((v: any) => {
102 s += h[v >> 4] + h[v & 15]
103 })
104
105 return s
106}