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'
+import { Events, Segment } from 'p2p-media-loader-core'
+import { timeToInt } from '../utils'
// videojs-hlsjs-plugin needs videojs in window
window['videojs'] = videojs
totalDownload: 0,
totalUpload: 0
}
+ private startTime: number
private networkInfoInterval: any
this.options = options
+ if (!videojs.Html5Hlsjs) {
+ const message = 'HLS.js does not seem to be supported.'
+ console.warn(message)
+
+ player.ready(() => player.trigger('error', new Error(message)))
+ return
+ }
+
videojs.Html5Hlsjs.addHook('beforeinitialize', (videojsPlayer: any, hlsjs: any) => {
this.hlsjs = hlsjs
})
initVideoJsContribHlsJsPlayer(player)
+ this.startTime = timeToInt(options.startTime)
+
player.src({
type: options.type,
src: options.src
})
+ player.one('play', () => {
+ player.addClass('vjs-has-big-play-button-clicked')
+ })
+
player.ready(() => this.initialize())
}
dispose () {
+ if (this.hlsjs) this.hlsjs.destroy()
+ if (this.p2pEngine) this.p2pEngine.destroy()
+
clearInterval(this.networkInfoInterval)
}
private initialize () {
initHlsJsPlayer(this.hlsjs)
- this.p2pEngine = this.player.tech_.options_.hlsjsConfig.loader.getEngine()
+ const tech = this.player.tech_
+ this.p2pEngine = tech.options_.hlsjsConfig.loader.getEngine()
// Avoid using constants to not import hls.hs
// https://github.com/video-dev/hls.js/blob/master/src/events.js#L37
this.trigger('resolutionChange', { auto: this.hlsjs.autoLevelEnabled, resolutionId: data.height })
})
- this.p2pEngine.on(Events.SegmentError, (segment, err) => {
+ 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()
+
+ this.player.one('canplay', () => {
+ if (this.startTime) {
+ this.player.currentTime(this.startTime)
+ }
+ })
}
private runStats () {