1
0
Fork 0
peertube/client/src/assets/player/p2p-media-loader/p2p-media-loader-plugin.ts

207 lines
6.2 KiB
TypeScript
Raw Normal View History

2021-04-27 13:50:29 +00:00
import * as Hlsjs from 'hls.js/dist/hls.light.js'
import { Events, Segment } from 'p2p-media-loader-core'
import { Engine, initHlsJsPlayer, initVideoJsContribHlsJsPlayer } from 'p2p-media-loader-hlsjs'
2020-04-21 09:02:28 +00:00
import videojs from 'video.js'
2021-07-26 13:04:37 +00:00
import { timeToInt } from '@shared/core-utils'
2020-01-28 16:29:50 +00:00
import { P2PMediaLoaderPluginOptions, PlayerNetworkInfo } from '../peertube-videojs-typings'
import { registerConfigPlugin, registerSourceHandler } from './hls-plugin'
registerConfigPlugin(videojs)
registerSourceHandler(videojs)
2020-01-28 16:29:50 +00:00
const Plugin = videojs.getPlugin('plugin')
class P2pMediaLoaderPlugin extends Plugin {
2019-01-24 09:16:30 +00:00
private readonly CONSTANTS = {
INFO_SCHEDULER: 1000 // Don't change this
}
2019-01-29 07:37:25 +00:00
private readonly options: P2PMediaLoaderPluginOptions
2019-01-24 09:16:30 +00:00
private hlsjs: Hlsjs
2019-01-24 09:16:30 +00:00
private p2pEngine: Engine
private statsP2PBytes = {
pendingDownload: [] as number[],
pendingUpload: [] as number[],
numPeers: 0,
totalDownload: 0,
totalUpload: 0
}
2019-01-29 07:37:25 +00:00
private statsHTTPBytes = {
pendingDownload: [] as number[],
pendingUpload: [] as number[],
totalDownload: 0,
totalUpload: 0
}
2019-03-13 13:18:58 +00:00
private startTime: number
2019-01-24 09:16:30 +00:00
private networkInfoInterval: any
2021-04-27 13:50:29 +00:00
private hlsjsCurrentLevel: number
private hlsjsLevels: Hlsjs.Level[]
2020-04-17 09:20:12 +00:00
constructor (player: videojs.Player, options?: P2PMediaLoaderPluginOptions) {
2020-01-28 16:29:50 +00:00
super(player)
2019-01-29 07:37:25 +00:00
this.options = options
2020-01-28 16:29:50 +00:00
// FIXME: typings https://github.com/Microsoft/TypeScript/issues/14080
if (!(videojs as any).Html5Hlsjs) {
2020-07-20 08:13:56 +00:00
console.warn('HLS.js does not seem to be supported. Try to fallback to built in HLS.')
2019-02-20 10:26:14 +00:00
2020-07-20 08:13:56 +00:00
if (!player.canPlayType('application/vnd.apple.mpegurl')) {
const message = 'Cannot fallback to built-in HLS'
console.warn(message)
2019-02-20 10:26:14 +00:00
2020-07-20 08:13:56 +00:00
player.ready(() => player.trigger('error', new Error(message)))
return
}
} else {
// FIXME: typings https://github.com/Microsoft/TypeScript/issues/14080
(videojs as any).Html5Hlsjs.addHook('beforeinitialize', (videojsPlayer: any, hlsjs: any) => {
this.hlsjs = hlsjs
})
2019-01-24 09:16:30 +00:00
2020-07-20 08:13:56 +00:00
initVideoJsContribHlsJsPlayer(player)
}
2019-03-13 13:18:58 +00:00
this.startTime = timeToInt(options.startTime)
player.src({
type: options.type,
src: options.src
})
2019-01-29 07:37:25 +00:00
2020-07-20 08:13:56 +00:00
player.ready(() => {
this.initializeCore()
2019-02-06 09:39:50 +00:00
2020-07-20 08:13:56 +00:00
if ((videojs as any).Html5Hlsjs) {
this.initializePlugin()
}
})
}
2019-01-24 09:16:30 +00:00
dispose () {
2019-02-06 09:39:50 +00:00
if (this.hlsjs) this.hlsjs.destroy()
if (this.p2pEngine) this.p2pEngine.destroy()
2019-01-24 09:16:30 +00:00
clearInterval(this.networkInfoInterval)
}
2021-04-27 13:50:29 +00:00
getCurrentLevel () {
return this.hlsjsLevels.find(l => l.level === this.hlsjsCurrentLevel)
}
getLiveLatency () {
return undefined as number
// FIXME: Use latency when hls >= V1
// return this.hlsjs.latency
}
2019-12-17 13:20:43 +00:00
getHLSJS () {
return this.hlsjs
}
2020-07-20 08:13:56 +00:00
private initializeCore () {
this.player.one('play', () => {
this.player.addClass('vjs-has-big-play-button-clicked')
})
this.player.one('canplay', () => {
if (this.startTime) {
this.player.currentTime(this.startTime)
}
})
}
private initializePlugin () {
2019-01-29 07:37:25 +00:00
initHlsJsPlayer(this.hlsjs)
2020-01-28 16:29:50 +00:00
// FIXME: typings
const options = this.player.tech(true).options_ as any
this.p2pEngine = options.hlsjsConfig.loader.getEngine()
2019-01-24 09:16:30 +00:00
this.hlsjs.on(Hlsjs.Events.LEVEL_SWITCHING, (_: any, data: any) => {
2019-01-24 09:16:30 +00:00
this.trigger('resolutionChange', { auto: this.hlsjs.autoLevelEnabled, resolutionId: data.height })
})
2021-06-08 08:17:47 +00:00
this.hlsjs.on(Hlsjs.Events.MANIFEST_LOADED, (_: any, data: any) => {
this.trigger('resolutionsLoaded')
})
2019-08-23 08:19:44 +00:00
this.p2pEngine.on(Events.SegmentError, (segment: Segment, err) => {
2019-01-29 07:37:25 +00:00
console.error('Segment error.', segment, err)
2019-08-23 08:19:44 +00:00
this.options.redundancyUrlManager.removeBySegmentUrl(segment.requestUrl)
2019-01-29 07:37:25 +00:00
})
2019-08-23 08:19:44 +00:00
this.statsP2PBytes.numPeers = 1 + this.options.redundancyUrlManager.countBaseUrls()
2019-01-29 07:37:25 +00:00
2019-01-24 09:16:30 +00:00
this.runStats()
}
private runStats () {
this.p2pEngine.on(Events.PieceBytesDownloaded, (method: string, size: number) => {
2019-01-29 07:37:25 +00:00
const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes
elem.pendingDownload.push(size)
elem.totalDownload += size
2019-01-24 09:16:30 +00:00
})
this.p2pEngine.on(Events.PieceBytesUploaded, (method: string, size: number) => {
2019-01-29 07:37:25 +00:00
const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes
elem.pendingUpload.push(size)
elem.totalUpload += size
2019-01-24 09:16:30 +00:00
})
this.p2pEngine.on(Events.PeerConnect, () => this.statsP2PBytes.numPeers++)
this.p2pEngine.on(Events.PeerClose, () => this.statsP2PBytes.numPeers--)
2021-04-27 13:50:29 +00:00
this.hlsjs.on(Hlsjs.Events.MANIFEST_PARSED, (_e, manifest) => {
this.hlsjsCurrentLevel = manifest.firstLevel
this.hlsjsLevels = manifest.levels
})
this.hlsjs.on(Hlsjs.Events.LEVEL_LOADED, (_e, level) => {
this.hlsjsCurrentLevel = level.levelId || (level as any).id
})
2019-01-24 09:16:30 +00:00
this.networkInfoInterval = setInterval(() => {
2019-01-29 07:37:25 +00:00
const p2pDownloadSpeed = this.arraySum(this.statsP2PBytes.pendingDownload)
const p2pUploadSpeed = this.arraySum(this.statsP2PBytes.pendingUpload)
const httpDownloadSpeed = this.arraySum(this.statsHTTPBytes.pendingDownload)
const httpUploadSpeed = this.arraySum(this.statsHTTPBytes.pendingUpload)
2019-01-24 09:16:30 +00:00
this.statsP2PBytes.pendingDownload = []
this.statsP2PBytes.pendingUpload = []
2019-01-29 07:37:25 +00:00
this.statsHTTPBytes.pendingDownload = []
this.statsHTTPBytes.pendingUpload = []
2019-01-24 09:16:30 +00:00
return this.player.trigger('p2pInfo', {
source: 'p2p-media-loader',
2019-01-29 07:37:25 +00:00
http: {
downloadSpeed: httpDownloadSpeed,
uploadSpeed: httpUploadSpeed,
downloaded: this.statsHTTPBytes.totalDownload,
uploaded: this.statsHTTPBytes.totalUpload
},
2019-01-24 09:16:30 +00:00
p2p: {
2019-01-29 07:37:25 +00:00
downloadSpeed: p2pDownloadSpeed,
uploadSpeed: p2pUploadSpeed,
2019-01-24 09:16:30 +00:00
numPeers: this.statsP2PBytes.numPeers,
downloaded: this.statsP2PBytes.totalDownload,
uploaded: this.statsP2PBytes.totalUpload
2021-04-27 13:50:29 +00:00
},
bandwidthEstimate: (this.hlsjs as any).bandwidthEstimate / 8
2019-01-24 09:16:30 +00:00
} as PlayerNetworkInfo)
}, this.CONSTANTS.INFO_SCHEDULER)
}
2019-01-29 07:37:25 +00:00
private arraySum (data: number[]) {
return data.reduce((a: number, b: number) => a + b, 0)
}
}
videojs.registerPlugin('p2pMediaLoader', P2pMediaLoaderPlugin)
export { P2pMediaLoaderPlugin }