1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
import videojs from 'video.js'
import { logger } from '@root-helpers/logger'
import { secondsToTime } from '@shared/core-utils'
import { PlayerNetworkInfo as EventPlayerNetworkInfo } from '../../types'
import { bytes } from '../common'
interface StatsCardOptions extends videojs.ComponentOptions {
videoUUID: string
videoIsLive: boolean
mode: 'web-video' | 'p2p-media-loader'
p2pEnabled: boolean
}
interface PlayerNetworkInfo {
downloadSpeed?: string
uploadSpeed?: string
totalDownloaded?: string
totalUploaded?: string
numPeers?: number
averageBandwidth?: string
downloadedFromServer?: string
downloadedFromPeers?: string
}
interface InfoElement {
root: HTMLElement
value: HTMLElement
}
const Component = videojs.getComponent('Component')
class StatsCard extends Component {
options_: StatsCardOptions
updateInterval: any
mode: 'web-video' | 'p2p-media-loader'
metadataStore: any = {}
intervalMs = 300
playerNetworkInfo: PlayerNetworkInfo = {}
private containerEl: HTMLDivElement
private infoListEl: HTMLDivElement
private playerMode: InfoElement
private p2p: InfoElement
private uuid: InfoElement
private viewport: InfoElement
private resolution: InfoElement
private volume: InfoElement
private codecs: InfoElement
private color: InfoElement
private connection: InfoElement
private network: InfoElement
private transferred: InfoElement
private download: InfoElement
private bufferProgress: InfoElement
private bufferState: InfoElement
private liveLatency: InfoElement
private onNetworkInfoHandler: (_event: any, data: EventPlayerNetworkInfo) => void
createEl () {
this.containerEl = videojs.dom.createEl('div', {
className: 'vjs-stats-content'
}) as HTMLDivElement
this.containerEl.style.display = 'none'
this.infoListEl = videojs.dom.createEl('div', {
className: 'vjs-stats-list'
}) as HTMLDivElement
const closeButton = videojs.dom.createEl('button', {
className: 'vjs-stats-close',
tabindex: '0',
title: 'Close stats',
innerText: '[x]'
}, { 'aria-label': 'Close stats' }) as HTMLElement
closeButton.onclick = () => this.hide()
this.containerEl.appendChild(closeButton)
this.containerEl.appendChild(this.infoListEl)
this.populateInfoBlocks()
this.onNetworkInfoHandler = (_event, data) => {
this.mode = data.source
const p2pStats = data.p2p
const httpStats = data.http
this.playerNetworkInfo.downloadSpeed = bytes((p2pStats?.downloadSpeed || 0) + (httpStats.downloadSpeed || 0)).join(' ')
this.playerNetworkInfo.uploadSpeed = bytes(p2pStats?.uploadSpeed || 0).join(' ')
this.playerNetworkInfo.totalDownloaded = bytes((p2pStats?.downloaded || 0) + httpStats.downloaded).join(' ')
this.playerNetworkInfo.totalUploaded = bytes(p2pStats?.uploaded || 0).join(' ')
this.playerNetworkInfo.numPeers = p2pStats?.peersWithWebSeed
if (data.source === 'p2p-media-loader') {
this.playerNetworkInfo.averageBandwidth = bytes(data.bandwidthEstimate).join(' ') + '/s'
this.playerNetworkInfo.downloadedFromServer = bytes(httpStats.downloaded).join(' ')
this.playerNetworkInfo.downloadedFromPeers = bytes(p2pStats?.downloaded || 0).join(' ')
}
}
this.player().on('network-info', this.onNetworkInfoHandler)
return this.containerEl
}
dispose () {
if (this.updateInterval) clearInterval(this.updateInterval)
this.player().off('network-info', this.onNetworkInfoHandler)
super.dispose()
}
toggle () {
if (this.updateInterval) this.hide()
else this.show()
}
show () {
this.containerEl.style.display = 'block'
this.updateInterval = setInterval(async () => {
try {
const options = this.mode === 'p2p-media-loader'
? this.buildHLSOptions()
: await this.buildWebVideoOptions() // Default
this.populateInfoValues(options)
} catch (err) {
logger.error('Cannot update stats.', err)
clearInterval(this.updateInterval)
}
}, this.intervalMs)
}
hide () {
clearInterval(this.updateInterval)
this.containerEl.style.display = 'none'
}
private buildHLSOptions () {
const p2pMediaLoader = this.player_.p2pMediaLoader()
const level = p2pMediaLoader.getCurrentLevel()
const codecs = level?.videoCodec || level?.audioCodec
? `${level?.videoCodec || ''} / ${level?.audioCodec || ''}`
: undefined
const resolution = level?.height
? `${level.height}p${level?.attrs['FRAME-RATE'] || ''}`
: undefined
const buffer = this.timeRangesToString(this.player().buffered())
let progress: number
let latency: string
if (this.options_.videoIsLive) {
latency = secondsToTime(p2pMediaLoader.getLiveLatency())
} else {
progress = this.player().bufferedPercent()
}
return {
playerNetworkInfo: this.playerNetworkInfo,
resolution,
codecs,
buffer,
latency,
progress
}
}
private async buildWebVideoOptions () {
const videoFile = this.player_.webVideo().getCurrentVideoFile()
if (!this.metadataStore[videoFile.fileUrl]) {
this.metadataStore[videoFile.fileUrl] = await fetch(videoFile.metadataUrl).then(res => res.json())
}
const metadata = this.metadataStore[videoFile.fileUrl]
let colorSpace = 'unknown'
let codecs = 'unknown'
if (metadata?.streams?.[0]) {
const stream = metadata.streams[0]
colorSpace = stream['color_space'] !== 'unknown'
? stream['color_space']
: 'bt709'
codecs = stream['codec_name'] || 'avc1'
}
const resolution = videoFile?.resolution.label + videoFile?.fps
const buffer = this.timeRangesToString(this.player_.buffered())
const progress = this.player_.bufferedPercent()
return {
playerNetworkInfo: this.playerNetworkInfo,
progress,
colorSpace,
codecs,
resolution,
buffer
}
}
private populateInfoBlocks () {
this.playerMode = this.buildInfoRow(this.player().localize('Player mode'))
this.p2p = this.buildInfoRow(this.player().localize('P2P'))
this.uuid = this.buildInfoRow(this.player().localize('Video UUID'))
this.viewport = this.buildInfoRow(this.player().localize('Viewport / Frames'))
this.resolution = this.buildInfoRow(this.player().localize('Resolution'))
this.volume = this.buildInfoRow(this.player().localize('Volume'))
this.codecs = this.buildInfoRow(this.player().localize('Codecs'))
this.color = this.buildInfoRow(this.player().localize('Color'))
this.connection = this.buildInfoRow(this.player().localize('Connection Speed'))
this.network = this.buildInfoRow(this.player().localize('Network Activity'))
this.transferred = this.buildInfoRow(this.player().localize('Total Transfered'))
this.download = this.buildInfoRow(this.player().localize('Download Breakdown'))
this.bufferProgress = this.buildInfoRow(this.player().localize('Buffer Progress'))
this.bufferState = this.buildInfoRow(this.player().localize('Buffer State'))
this.liveLatency = this.buildInfoRow(this.player().localize('Live Latency'))
this.infoListEl.appendChild(this.playerMode.root)
this.infoListEl.appendChild(this.p2p.root)
this.infoListEl.appendChild(this.uuid.root)
this.infoListEl.appendChild(this.viewport.root)
this.infoListEl.appendChild(this.resolution.root)
this.infoListEl.appendChild(this.volume.root)
this.infoListEl.appendChild(this.codecs.root)
this.infoListEl.appendChild(this.color.root)
this.infoListEl.appendChild(this.connection.root)
this.infoListEl.appendChild(this.network.root)
this.infoListEl.appendChild(this.transferred.root)
this.infoListEl.appendChild(this.download.root)
this.infoListEl.appendChild(this.bufferProgress.root)
this.infoListEl.appendChild(this.bufferState.root)
this.infoListEl.appendChild(this.liveLatency.root)
}
private populateInfoValues (options: {
playerNetworkInfo: PlayerNetworkInfo
progress: number
codecs: string
resolution: string
buffer: string
latency?: string
colorSpace?: string
}) {
const { playerNetworkInfo, progress, colorSpace, codecs, resolution, buffer, latency } = options
const { downloadedFromServer, downloadedFromPeers } = playerNetworkInfo
const player = this.player()
const videoQuality: VideoPlaybackQuality = player.getVideoPlaybackQuality()
const vw = Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0)
const vh = Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0)
const pr = (window.devicePixelRatio || 1).toFixed(2)
const vp = `${vw}x${vh}*${pr}`
const { droppedVideoFrames, totalVideoFrames } = videoQuality
const frames = player.localize('{1} / {2} dropped of {3}', [ vp, droppedVideoFrames + '', totalVideoFrames + '' ])
const duration = player.duration()
let volume = `${Math.round(player.volume() * 100)}`
if (player.muted()) volume += player.localize(' (muted)')
const networkActivity = playerNetworkInfo.downloadSpeed
? `${playerNetworkInfo.downloadSpeed} ⇓ / ${playerNetworkInfo.uploadSpeed} ⇑`
: undefined
let totalTransferred = playerNetworkInfo.totalDownloaded
? `${playerNetworkInfo.totalDownloaded} ⇓`
: ''
if (playerNetworkInfo.totalUploaded) {
totalTransferred += `/ ${playerNetworkInfo.totalUploaded} ⇑`
}
const downloadBreakdown = playerNetworkInfo.downloadedFromServer
? player.localize('{1} from servers · {2} from peers', [ downloadedFromServer, downloadedFromPeers ])
: undefined
const bufferProgress = progress !== undefined
? `${(progress * 100).toFixed(1)}% (${(progress * duration).toFixed(1)}s)`
: undefined
const p2pEnabled = this.options_.p2pEnabled && this.mode === 'p2p-media-loader'
this.setInfoValue(this.playerMode, this.mode)
this.setInfoValue(this.p2p, player.localize(p2pEnabled ? 'enabled' : 'disabled'))
this.setInfoValue(this.uuid, this.options_.videoUUID)
this.setInfoValue(this.viewport, frames)
this.setInfoValue(this.resolution, resolution)
this.setInfoValue(this.volume, volume)
this.setInfoValue(this.codecs, codecs)
this.setInfoValue(this.color, colorSpace)
this.setInfoValue(this.connection, playerNetworkInfo.averageBandwidth)
this.setInfoValue(this.network, networkActivity)
this.setInfoValue(this.transferred, totalTransferred)
this.setInfoValue(this.download, downloadBreakdown)
this.setInfoValue(this.bufferProgress, bufferProgress)
this.setInfoValue(this.bufferState, buffer)
this.setInfoValue(this.liveLatency, latency)
}
private setInfoValue (el: InfoElement, value: string) {
if (!value) {
el.root.style.display = 'none'
return
}
el.root.style.display = 'block'
if (el.value.innerHTML === value) return
el.value.innerHTML = value
}
private buildInfoRow (labelText: string, valueHTML?: string) {
const root = videojs.dom.createEl('div') as HTMLElement
root.style.display = 'none'
const label = videojs.dom.createEl('div', { innerText: labelText }) as HTMLElement
const value = videojs.dom.createEl('span', { innerHTML: valueHTML }) as HTMLElement
root.appendChild(label)
root.appendChild(value)
return { root, value }
}
private timeRangesToString (r: videojs.TimeRange) {
let result = ''
for (let i = 0; i < r.length; i++) {
const start = Math.floor(r.start(i))
const end = Math.floor(r.end(i))
result += `[${secondsToTime(start)}, ${secondsToTime(end)}] `
}
return result
}
}
videojs.registerComponent('StatsCard', StatsCard)
export {
StatsCard,
StatsCardOptions
}
|