webrtc stats: computed fps, fixes #10.

This commit is contained in:
Miroslav Šedivý 2021-03-18 12:45:58 +01:00
parent 0dbab610aa
commit a73a38b736

View File

@ -311,6 +311,7 @@ export class NekoWebRTC extends EventEmitter<NekoWebRTCEvents> {
private statsEmitter(ms: number = 2000) { private statsEmitter(ms: number = 2000) {
let bytesReceived: number let bytesReceived: number
let timestamp: number let timestamp: number
let framesDecoded: number
let packetsLost: number let packetsLost: number
let packetsReceived: number let packetsReceived: number
@ -342,13 +343,14 @@ export class NekoWebRTC extends EventEmitter<NekoWebRTCEvents> {
if (timestamp) { if (timestamp) {
const bytesDiff = (report.bytesReceived - bytesReceived) * 8 const bytesDiff = (report.bytesReceived - bytesReceived) * 8
const tsDiff = report.timestamp - timestamp const tsDiff = report.timestamp - timestamp
const framesDecodedDiff = report.framesDecoded - framesDecoded
const packetsLostDiff = report.packetsLost - packetsLost const packetsLostDiff = report.packetsLost - packetsLost
const packetsReceivedDiff = report.packetsReceived - packetsReceived const packetsReceivedDiff = report.packetsReceived - packetsReceived
this.emit('stats', { this.emit('stats', {
bitrate: (bytesDiff / tsDiff) * 1000, bitrate: (bytesDiff / tsDiff) * 1000,
packetLoss: (packetsLostDiff / (packetsLostDiff + packetsReceivedDiff)) * 100, packetLoss: (packetsLostDiff / (packetsLostDiff + packetsReceivedDiff)) * 100,
fps: Number(report.framesPerSecond || report.framerateMean), fps: Number(report.framesPerSecond || (framesDecodedDiff / (tsDiff / 1000))),
width: report.frameWidth || NaN, width: report.frameWidth || NaN,
height: report.frameHeight || NaN, height: report.frameHeight || NaN,
}) })
@ -356,6 +358,7 @@ export class NekoWebRTC extends EventEmitter<NekoWebRTCEvents> {
bytesReceived = report.bytesReceived bytesReceived = report.bytesReceived
timestamp = report.timestamp timestamp = report.timestamp
framesDecoded = report.framesDecoded
packetsLost = report.packetsLost packetsLost = report.packetsLost
packetsReceived = report.packetsReceived packetsReceived = report.packetsReceived
}, ms) }, ms)