Skip to content

Commit

Permalink
Cleanup stats for nerds
Browse files Browse the repository at this point in the history
  • Loading branch information
Chocobozzz committed Apr 28, 2021
1 parent ff56391 commit 4e11d8f
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 120 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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 { Engine, initHlsJsPlayer, initVideoJsContribHlsJsPlayer } from 'p2p-media-loader-hlsjs'
import { Events, Segment } from 'p2p-media-loader-core'
import { timeToInt } from '../utils'
import { registerConfigPlugin, registerSourceHandler } from './hls-plugin'
import * as Hlsjs from 'hls.js/dist/hls.light.js'

registerConfigPlugin(videojs)
registerSourceHandler(videojs)
Expand Down Expand Up @@ -36,6 +36,9 @@ class P2pMediaLoaderPlugin extends Plugin {

private networkInfoInterval: any

private hlsjsCurrentLevel: number
private hlsjsLevels: Hlsjs.Level[]

constructor (player: videojs.Player, options?: P2PMediaLoaderPluginOptions) {
super(player)

Expand Down Expand Up @@ -84,6 +87,16 @@ class P2pMediaLoaderPlugin extends Plugin {
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
}
Expand Down Expand Up @@ -140,6 +153,14 @@ class P2pMediaLoaderPlugin extends Plugin {
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)
Expand All @@ -166,7 +187,8 @@ class P2pMediaLoaderPlugin extends Plugin {
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)
}
Expand Down
6 changes: 5 additions & 1 deletion client/src/assets/player/peertube-videojs-typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { PlaylistPlugin } from './playlist/playlist-plugin'
import { EndCardOptions } from './upnext/end-card'
import { StatsCardOptions } from './stats/stats-card'
import { WebTorrentPlugin } from './webtorrent/webtorrent-plugin'
import { StatsForNerdsPlugin } from './stats/stats-plugin'

declare module 'video.js' {

Expand Down Expand Up @@ -37,7 +38,7 @@ declare module 'video.js' {

bezels (): void

stats (options?: Partial<StatsCardOptions>): any
stats (options?: StatsCardOptions): StatsForNerdsPlugin

qualityLevels (): QualityLevels

Expand Down Expand Up @@ -198,6 +199,9 @@ type PlayerNetworkInfo = {
uploaded: number
numPeers: number
}

// In bytes
bandwidthEstimate: number
}

type PlaylistItemOptions = {
Expand Down
Loading

0 comments on commit 4e11d8f

Please sign in to comment.