2019-04-12 12:10:40 -05:00
|
|
|
// 3rd party.
|
2019-04-13 17:51:49 -05:00
|
|
|
import classnames from 'classnames';
|
2019-04-12 12:10:40 -05:00
|
|
|
import React, {useEffect, useState} from 'react';
|
|
|
|
// 2nd party.
|
|
|
|
import {compose} from '@avocado/core';
|
2019-04-13 17:08:58 -05:00
|
|
|
import {Vector} from '@avocado/math';
|
2019-04-12 12:10:40 -05:00
|
|
|
import contempo from 'contempo';
|
|
|
|
|
|
|
|
const decorate = compose(
|
|
|
|
contempo(`
|
|
|
|
.throughput {
|
|
|
|
background-color: rgba(0, 0, 0, .3);
|
|
|
|
color: white;
|
|
|
|
line-height: 1em;
|
2019-04-14 17:08:19 -05:00
|
|
|
width: 9em;
|
2019-04-14 00:24:21 -05:00
|
|
|
padding: 0.125em;
|
2019-04-12 12:10:40 -05:00
|
|
|
font-family: monospace;
|
|
|
|
}
|
2019-04-13 17:08:58 -05:00
|
|
|
.throughput .kbps:before {
|
2019-04-13 17:51:49 -05:00
|
|
|
color: rgb(0, 255, 0);
|
2019-04-13 17:08:58 -05:00
|
|
|
content: 'kb/s ';
|
|
|
|
}
|
2019-04-13 17:51:49 -05:00
|
|
|
.throughput .mbps:before {
|
|
|
|
color: rgb(255, 180, 0);
|
|
|
|
content: 'mb/s ';
|
|
|
|
}
|
2019-04-12 12:10:40 -05:00
|
|
|
.throughput .input:before {
|
2019-04-13 17:08:58 -05:00
|
|
|
content: '\\a0in: ';
|
2019-04-12 12:10:40 -05:00
|
|
|
}
|
|
|
|
.throughput .output:before {
|
2019-04-13 17:08:58 -05:00
|
|
|
content: 'out: ';
|
2019-04-12 12:10:40 -05:00
|
|
|
}
|
|
|
|
.throughput p {
|
2019-04-14 17:08:19 -05:00
|
|
|
font-size: 0.8em;
|
2019-04-12 12:10:40 -05:00
|
|
|
margin: 0.25em;
|
|
|
|
}
|
|
|
|
`),
|
|
|
|
);
|
|
|
|
|
2019-04-13 17:51:49 -05:00
|
|
|
function formatKbps(bps) {
|
2019-04-12 12:10:40 -05:00
|
|
|
const kbps = bps / 1000;
|
|
|
|
return Math.floor(kbps * 100) / 100;
|
|
|
|
}
|
|
|
|
|
2019-04-13 17:51:49 -05:00
|
|
|
function formatMbps(bps) {
|
|
|
|
const mbps = bps / 1000000;
|
|
|
|
return Math.floor(mbps * 100) / 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
function format(bps) {
|
|
|
|
const unit = preferredUnit(bps);
|
|
|
|
switch (unit) {
|
|
|
|
case 'kbps':
|
|
|
|
return formatKbps(bps);
|
|
|
|
case 'mbps':
|
|
|
|
return formatMbps(bps);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function preferredUnit(bps) {
|
|
|
|
if (bps > 1000000) {
|
|
|
|
return 'mbps';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 'kbps';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 17:08:58 -05:00
|
|
|
const ThroughputComponent = ({Parser}) => {
|
2019-04-12 12:10:40 -05:00
|
|
|
const [throughput, setThroughput] = useState([0, 0]);
|
2019-04-13 17:51:49 -05:00
|
|
|
const [inUnit, setInUnit] = useState('kbps');
|
|
|
|
const [outUnit, setOutUnit] = useState('kbps');
|
2019-04-12 12:10:40 -05:00
|
|
|
useEffect(() => {
|
2019-04-13 17:08:58 -05:00
|
|
|
const {Decoder, Encoder} = Parser;
|
|
|
|
const throughputSampleTime = 0.5;
|
|
|
|
// Throughput ticker.
|
|
|
|
const throughputHandle = setInterval(() => {
|
|
|
|
const throughput = Vector.scale([
|
|
|
|
Decoder.throughput || 0,
|
|
|
|
Encoder.throughput || 0,
|
2019-04-13 17:51:49 -05:00
|
|
|
], 8 * (1 / throughputSampleTime));
|
2019-04-12 12:10:40 -05:00
|
|
|
setThroughput(throughput);
|
2019-04-13 17:51:49 -05:00
|
|
|
setInUnit(preferredUnit(throughput[0]));
|
|
|
|
setOutUnit(preferredUnit(throughput[1]));
|
2019-04-13 17:08:58 -05:00
|
|
|
Decoder.throughput = 0;
|
|
|
|
Encoder.throughput = 0;
|
|
|
|
}, throughputSampleTime * 1000);
|
2019-04-12 12:10:40 -05:00
|
|
|
return () => {
|
2019-04-13 17:08:58 -05:00
|
|
|
clearInterval(throughputHandle);
|
2019-04-12 12:10:40 -05:00
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
return <div className="throughput unselectable">
|
2019-04-13 17:51:49 -05:00
|
|
|
<p className={classnames(
|
|
|
|
'unit',
|
|
|
|
inUnit,
|
|
|
|
)}>
|
2019-04-13 17:08:58 -05:00
|
|
|
<span className="input">
|
2019-04-13 17:51:49 -05:00
|
|
|
{format(throughput[0])}
|
2019-04-13 17:08:58 -05:00
|
|
|
</span>
|
|
|
|
</p>
|
2019-04-13 17:51:49 -05:00
|
|
|
<p className={classnames(
|
|
|
|
'unit',
|
|
|
|
outUnit,
|
|
|
|
)}>
|
2019-04-13 17:08:58 -05:00
|
|
|
<span className="output">
|
2019-04-13 17:51:49 -05:00
|
|
|
{format(throughput[1])}
|
2019-04-13 17:08:58 -05:00
|
|
|
</span>
|
|
|
|
</p>
|
2019-04-12 12:10:40 -05:00
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default decorate(ThroughputComponent);
|