-// FIXME: something weird with our path definition in tsconfig and typings
-// @ts-ignore
-import * as videojs from 'video.js'
-import { P2PMediaLoaderPluginOptions, PlayerNetworkInfo, VideoJSComponentInterface } from '../peertube-videojs-typings'
-import { Engine, initHlsJsPlayer, initVideoJsContribHlsJsPlayer } from 'p2p-media-loader-hlsjs'
-import { Events } from 'p2p-media-loader-core'
-
-// videojs-hlsjs-plugin needs videojs in window
-window['videojs'] = videojs
-require('@streamroot/videojs-hlsjs-plugin')
-
-const Plugin: VideoJSComponentInterface = videojs.getPlugin('plugin')
+import Hlsjs from 'hls.js'
+import videojs from 'video.js'
+import { Events, Segment } from '@peertube/p2p-media-loader-core'
+import { Engine, initHlsJsPlayer, initVideoJsContribHlsJsPlayer } from '@peertube/p2p-media-loader-hlsjs'
+import { timeToInt } from '@shared/core-utils'
+import { P2PMediaLoaderPluginOptions, PlayerNetworkInfo } from '../peertube-videojs-typings'
+import { registerConfigPlugin, registerSourceHandler } from './hls-plugin'
+
+registerConfigPlugin(videojs)
+registerSourceHandler(videojs)
+
+const Plugin = videojs.getPlugin('plugin')
class P2pMediaLoaderPlugin extends Plugin {
private readonly CONSTANTS = {
}
private readonly options: P2PMediaLoaderPluginOptions
- private hlsjs: any // Don't type hlsjs to not bundle the module
+ private hlsjs: Hlsjs
private p2pEngine: Engine
private statsP2PBytes = {
pendingDownload: [] as number[],
totalDownload: 0,
totalUpload: 0
}
+ private startTime: number
private networkInfoInterval: any
- constructor (player: videojs.Player, options: P2PMediaLoaderPluginOptions) {
- super(player, options)
+ constructor (player: videojs.Player, options?: P2PMediaLoaderPluginOptions) {
+ super(player)
this.options = options
- if (!videojs.Html5Hlsjs) {
- const message = 'HLS.js does not seem to be supported.'
- console.warn(message)
+ // FIXME: typings https://github.com/Microsoft/TypeScript/issues/14080
+ if (!(videojs as any).Html5Hlsjs) {
+ console.warn('HLS.js does not seem to be supported. Try to fallback to built in HLS.')
- player.ready(() => player.trigger('error', new Error(message)))
- return
- }
+ if (!player.canPlayType('application/vnd.apple.mpegurl')) {
+ const message = 'Cannot fallback to built-in HLS'
+ console.warn(message)
- videojs.Html5Hlsjs.addHook('beforeinitialize', (videojsPlayer: any, hlsjs: any) => {
- this.hlsjs = hlsjs
- })
+ 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
+ })
- initVideoJsContribHlsJsPlayer(player)
+ initVideoJsContribHlsJsPlayer(player)
+ }
+
+ this.startTime = timeToInt(options.startTime)
player.src({
type: options.type,
src: options.src
})
- player.on('play', () => {
- player.addClass('vjs-has-big-play-button-clicked')
- })
+ player.ready(() => {
+ this.initializeCore()
- player.ready(() => this.initialize())
+ if ((videojs as any).Html5Hlsjs) {
+ this.initializePlugin()
+ }
+ })
}
dispose () {
clearInterval(this.networkInfoInterval)
}
- private initialize () {
- initHlsJsPlayer(this.hlsjs)
+ getCurrentLevel () {
+ return this.hlsjs.levels[this.hlsjs.currentLevel]
+ }
- const tech = this.player.tech_
- this.p2pEngine = tech.options_.hlsjsConfig.loader.getEngine()
+ getLiveLatency () {
+ // FIXME: typings
+ return Math.round((this.hlsjs as any).latency)
+ }
- // Avoid using constants to not import hls.hs
- // https://github.com/video-dev/hls.js/blob/master/src/events.js#L37
- this.hlsjs.on('hlsLevelSwitching', (_: any, data: any) => {
- this.trigger('resolutionChange', { auto: this.hlsjs.autoLevelEnabled, resolutionId: data.height })
+ getHLSJS () {
+ return this.hlsjs
+ }
+
+ private initializeCore () {
+ this.player.one('play', () => {
+ this.player.addClass('vjs-has-big-play-button-clicked')
})
- this.p2pEngine.on(Events.SegmentError, (segment, err) => {
+ this.player.one('canplay', () => {
+ if (this.startTime) {
+ this.player.currentTime(this.startTime)
+ }
+ })
+ }
+
+ private initializePlugin () {
+ initHlsJsPlayer(this.hlsjs)
+
+ // FIXME: typings
+ const options = this.player.tech(true).options_ as any
+ this.p2pEngine = options.hlsjsConfig.loader.getEngine()
+
+ this.p2pEngine.on(Events.SegmentError, (segment: Segment, err) => {
console.error('Segment error.', segment, err)
+
+ this.options.redundancyUrlManager.removeBySegmentUrl(segment.requestUrl)
})
- this.statsP2PBytes.numPeers = 1 + this.options.redundancyBaseUrls.length
+ this.statsP2PBytes.numPeers = 1 + this.options.redundancyUrlManager.countBaseUrls()
this.runStats()
}
private runStats () {
- this.p2pEngine.on(Events.PieceBytesDownloaded, (method: string, size: number) => {
+ this.p2pEngine.on(Events.PieceBytesDownloaded, (method: string, _segment, bytes: number) => {
const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes
- elem.pendingDownload.push(size)
- elem.totalDownload += size
+ elem.pendingDownload.push(bytes)
+ elem.totalDownload += bytes
})
- this.p2pEngine.on(Events.PieceBytesUploaded, (method: string, size: number) => {
+ this.p2pEngine.on(Events.PieceBytesUploaded, (method: string, _segment, bytes: number) => {
const elem = method === 'p2p' ? this.statsP2PBytes : this.statsHTTPBytes
- elem.pendingUpload.push(size)
- elem.totalUpload += size
+ elem.pendingUpload.push(bytes)
+ elem.totalUpload += bytes
})
this.p2pEngine.on(Events.PeerConnect, () => this.statsP2PBytes.numPeers++)
this.statsHTTPBytes.pendingUpload = []
return this.player.trigger('p2pInfo', {
+ source: 'p2p-media-loader',
http: {
downloadSpeed: httpDownloadSpeed,
uploadSpeed: httpUploadSpeed,
numPeers: this.statsP2PBytes.numPeers,
downloaded: this.statsP2PBytes.totalDownload,
uploaded: this.statsP2PBytes.totalUpload
- }
+ },
+ bandwidthEstimate: (this.hlsjs as any).bandwidthEstimate / 8
} as PlayerNetworkInfo)
}, this.CONSTANTS.INFO_SCHEDULER)
}