1 import videojs from 'video.js'
2 import { PlaybackMetricCreate } from '../../../../../../shared/models'
3 import { MetricsPluginOptions, PlayerMode, PlayerNetworkInfo } from '../../types'
4 import { logger } from '@root-helpers/logger'
6 const Plugin = videojs.getPlugin('plugin')
8 class MetricsPlugin extends Plugin {
9 private readonly metricsUrl: string
10 private readonly videoUUID: string
11 private readonly mode: PlayerMode
13 private downloadedBytesP2P = 0
14 private downloadedBytesHTTP = 0
15 private uploadedBytesP2P = 0
17 private resolutionChanges = 0
20 private lastPlayerNetworkInfo: PlayerNetworkInfo
22 private metricsInterval: any
24 private readonly CONSTANTS = {
25 METRICS_INTERVAL: 15000
28 constructor (player: videojs.Player, options: MetricsPluginOptions) {
31 this.metricsUrl = options.metricsUrl
32 this.videoUUID = options.videoUUID
33 this.mode = options.mode
35 this.player.one('play', () => {
36 this.runMetricsInterval()
39 this.trackResolutionChange()
45 if (this.metricsInterval) clearInterval(this.metricsInterval)
48 private runMetricsInterval () {
49 this.metricsInterval = setInterval(() => {
50 let resolution: number
53 if (this.mode === 'p2p-media-loader') {
54 const level = this.player.p2pMediaLoader().getCurrentLevel()
57 resolution = Math.min(level.height || 0, level.width || 0)
59 const framerate = level?.attrs['FRAME-RATE']
61 ? parseInt(framerate, 10)
63 } else { // webtorrent
64 const videoFile = this.player.webtorrent().getCurrentVideoFile()
65 if (!videoFile) return
67 resolution = videoFile.resolution.id
68 fps = videoFile.fps && videoFile.fps !== -1
73 const body: PlaybackMetricCreate = {
77 playerMode: this.mode,
79 resolutionChanges: this.resolutionChanges,
83 downloadedBytesP2P: this.downloadedBytesP2P,
84 downloadedBytesHTTP: this.downloadedBytesHTTP,
86 uploadedBytesP2P: this.uploadedBytesP2P,
88 videoId: this.videoUUID
91 this.resolutionChanges = 0
93 this.downloadedBytesP2P = 0
94 this.downloadedBytesHTTP = 0
96 this.uploadedBytesP2P = 0
100 const headers = new Headers({ 'Content-type': 'application/json; charset=UTF-8' })
102 return fetch(this.metricsUrl, { method: 'POST', body: JSON.stringify(body), headers })
103 .catch(err => logger.error('Cannot send metrics to the server.', err))
104 }, this.CONSTANTS.METRICS_INTERVAL)
107 private trackBytes () {
108 this.player.on('p2pInfo', (_event, data: PlayerNetworkInfo) => {
111 this.downloadedBytesHTTP += data.http.downloaded - (this.lastPlayerNetworkInfo?.http.downloaded || 0)
112 this.downloadedBytesP2P += data.p2p.downloaded - (this.lastPlayerNetworkInfo?.p2p.downloaded || 0)
114 this.uploadedBytesP2P += data.p2p.uploaded - (this.lastPlayerNetworkInfo?.p2p.uploaded || 0)
116 this.lastPlayerNetworkInfo = data
120 private trackResolutionChange () {
121 this.player.on('engineResolutionChange', () => {
122 this.resolutionChanges++
126 private trackErrors () {
127 this.player.on('error', () => {
133 videojs.registerPlugin('metrics', MetricsPlugin)
134 export { MetricsPlugin }