Skip to content

Commit

Permalink
Add RTT stats
Browse files Browse the repository at this point in the history
  • Loading branch information
sergystepanov committed Sep 2, 2021
1 parent 3c1ce04 commit b916b0b
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 32 deletions.
1 change: 0 additions & 1 deletion web/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,6 @@ body {
#stats-overlay > div > div {
display: inline-block;
font-weight: 500;
width: 4em;
}

#stats-overlay .graph {
Expand Down
2 changes: 1 addition & 1 deletion web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ <h1>Options</h1>
<script src="/static/js/gameList.js?v=3"></script>
<script src="/static/js/stream/stream.js?v=2"></script>
<script src="/static/js/room.js?v=3"></script>
<script src="/static/js/stats/stats.js?v=1"></script>
<script src="/static/js/network/ajax.js?v=3"></script>
<script src="/static/js/network/socket.js?v=4"></script>
<script src="/static/js/network/rtcp.js?v=3"></script>
<script src="/static/js/stats/stats.js?v=2"></script>
<script src="/static/js/controller.js?v=6"></script>
<script src="/static/js/input/keyboard.js?v=5"></script>
<script src="/static/js/input/touch.js?v=3"></script>
Expand Down
117 changes: 87 additions & 30 deletions web/js/stats/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @version 1
*/
const stats = (() => {
const modules = [];
const _modules = [];
let tempHide = false;

// internal rendering stuff
Expand Down Expand Up @@ -181,7 +181,7 @@ const stats = (() => {
let previous = 0;
const window = 5;

const ui = moduleUi('Ping', true);
const ui = moduleUi('Ping(c)', true);

const onPingRequest = (data) => previous = data.time;

Expand Down Expand Up @@ -262,6 +262,39 @@ const stats = (() => {
return {get, enable, disable, render}
})(moduleUi, performance, window);


const webRTCStats_ = (() => {
let interval = null

function getStats() {
if (!rtcp.isConnected()) return;
rtcp.getConnection().getStats(null).then(stats => {
let frameStatValue = '?';
stats.forEach(report => {
if (report["framesReceived"] !== undefined && report["framesDecoded"] !== undefined && report["framesDropped"] !== undefined) {
frameStatValue = report["framesReceived"] - report["framesDecoded"] - report["framesDropped"];
event.pub('STATS_WEBRTC_FRAME_STATS', frameStatValue)
} else if (report["framerateMean"] !== undefined) {
frameStatValue = Math.round(report["framerateMean"] * 100) / 100;
event.pub('STATS_WEBRTC_FRAME_STATS', frameStatValue)
}

if (report["nominated"] && report["currentRoundTripTime"] !== undefined) {
event.pub('STATS_WEBRTC_ICE_RTT', report["currentRoundTripTime"] * 1000);
}
});
});
}

const enable = () => {
interval = window.setInterval(getStats, 1000);
}

const disable = () => window.clearInterval(interval);

return {enable, disable, internal: true}
})(event, rtcp, window);

/**
* User agent frame stats.
*
Expand All @@ -273,49 +306,71 @@ const stats = (() => {
*
* @version 1
*/
const webRTCStats = (() => {
const webRTCFrameStats = (() => {
let value = 0;
let interval = null
let listener;

let browser = env.getBrowser();
let label = 'FrameDelay'
if (browser === 'firefox') {
label = 'FramerateMean'
}
const label = env.getBrowser() === 'firefox' ? 'FramerateMean' : 'FrameDelay';
const ui = moduleUi(label, false, () => '');

const get = () => ui.el;

const enable = () => {
interval = window.setInterval(getStats, 1000);
listener = event.sub('STATS_WEBRTC_FRAME_STATS', onStats);
}

const disable = () => {
window.clearInterval(interval);
value = 0;
if (listener) listener.unsub();
}

const render = () => ui.update(value);

function getStats() {
if (!active || !rtcp.isConnected()) return;
rtcp.getConnection().getStats(null).then(stats => {
stats.forEach(report => {
if (report["framesReceived"] !== undefined && report["framesDecoded"] !== undefined && report["framesDropped"] !== undefined) {
value = report["framesReceived"] - report["framesDecoded"] - report["framesDropped"];
} else if (report["framerateMean"] !== undefined) {
value = Math.round(report["framerateMean"]*100)/100;
}
});
});
function onStats(val) {
value = val;
}

return {get, enable, disable, render}
})(moduleUi, rtcp, window);

const webRTCRttStats = (() => {
let value = 0;
let listener;

const ui = moduleUi('RTT(w)', true, () => 'ms');

const get = () => ui.el;

const enable = () => {
listener = event.sub('STATS_WEBRTC_ICE_RTT', onStats);
}

const disable = () => {
value = 0;
if (listener) listener.unsub();
}

const render = () => ui.update(value);

function onStats(val) {
value = val;
}

return {get, enable, disable, render}
})(moduleUi, window);
})(moduleUi, rtcp, window);

const modules = (fn, force = true) => {
_modules.forEach(m => {
if (force || !m.internal) {
fn(m);
}
}
)
}

const enable = () => {
active = true;
modules.forEach(m => m.enable());
modules(m => m.enable())
render();
draw();
_show();
Expand All @@ -336,7 +391,7 @@ const stats = (() => {

const disable = () => {
active = false;
modules.forEach(m => m.disable());
modules(m => m.disable());
_hide();
}

Expand Down Expand Up @@ -366,18 +421,20 @@ const stats = (() => {
}
}

const render = () => modules.forEach(m => m.render());
const render = () => modules(m => m.render(), false);

// add submodules
modules.push(
_modules.push(
webRTCRttStats,
latency,
clientMemory,
webRTCStats
webRTCStats_,
webRTCFrameStats
);
modules.forEach(m => statsOverlayEl.append(m.get()));
modules(m => statsOverlayEl.append(m.get()), false);

event.sub(STATS_TOGGLE, onToggle);
event.sub(HELP_OVERLAY_TOGGLED, onHelpOverlayToggle)

return {enable, disable}
})(document, env, event, log, window);
})(document, env, event, log, rtcp, window);

0 comments on commit b916b0b

Please sign in to comment.