-import videojs, { VideoJsPlayer } from 'video.js'
-import { P2PMediaLoaderPluginOptions, PlayerNetworkInfo } from '../peertube-videojs-typings'
-import { Engine, initHlsJsPlayer, initVideoJsContribHlsJsPlayer } from 'p2p-media-loader-hlsjs'
+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'
+import videojs from 'video.js'
+import { P2PMediaLoaderPluginOptions, PlayerNetworkInfo } from '../peertube-videojs-typings'
import { timeToInt } from '../utils'
import { registerConfigPlugin, registerSourceHandler } from './hls-plugin'
-import * as Hlsjs from 'hls.js'
registerConfigPlugin(videojs)
registerSourceHandler(videojs)
private networkInfoInterval: any
- constructor (player: VideoJsPlayer, options?: P2PMediaLoaderPluginOptions) {
+ private hlsjsCurrentLevel: number
+ private hlsjsLevels: Hlsjs.Level[]
+
+ constructor (player: videojs.Player, options?: P2PMediaLoaderPluginOptions) {
super(player)
this.options = options
// FIXME: typings https://github.com/Microsoft/TypeScript/issues/14080
if (!(videojs as any).Html5Hlsjs) {
- const message = 'HLS.js does not seem to be supported.'
- console.warn(message)
+ 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)
- // FIXME: typings https://github.com/Microsoft/TypeScript/issues/14080
- (videojs as any).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)
src: options.src
})
- player.one('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)
}
+ 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
+ }
+
getHLSJS () {
return this.hlsjs
}
- private initialize () {
+ 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 () {
initHlsJsPlayer(this.hlsjs)
// FIXME: typings
this.statsP2PBytes.numPeers = 1 + this.options.redundancyUrlManager.countBaseUrls()
this.runStats()
-
- this.player.one('canplay', () => {
- if (this.startTime) {
- this.player.currentTime(this.startTime)
- }
- })
}
private runStats () {
this.p2pEngine.on(Events.PeerConnect, () => this.statsP2PBytes.numPeers++)
this.p2pEngine.on(Events.PeerClose, () => this.statsP2PBytes.numPeers--)
+ 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
+ })
+
this.networkInfoInterval = setInterval(() => {
const p2pDownloadSpeed = this.arraySum(this.statsP2PBytes.pendingDownload)
const p2pUploadSpeed = this.arraySum(this.statsP2PBytes.pendingUpload)
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)
}