UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
bandwidth_widget.cpp
Go to the documentation of this file.
1#include <stdio.h>
4
5namespace {
6QString getBitrateStr(long long bitsPerSecond){
7 static constexpr std::string_view units = "kMG";
8
9 float num = bitsPerSecond;
10 char unit = ' ';
11
12 for(unsigned i = 0; num > 1000 && i < units.size(); i++){
13 unit = units[i];
14 num /= 1000;
15 }
16
17 return QString::number(num, 'f', 2) + " " + unit + "bps";
18}
19}
20
22 elapsedTimer.start();
23 reset();
24 QObject::connect(&timer, &QTimer::timeout, this, &BandwidthWidget::timeout);
25 timer.setInterval(timeout_msec);
26}
27
29 reports.remove_timed_out(timeout_msec, elapsedTimer.elapsed());
30
31 if(!isEnabled()){
32 setText("-- bps");
33 setToolTip("");
34 return;
35 }
36
37 long long total = 0;
38 QString tooltip;
39 for(const auto& i : reports.get()){
40 total += i.item.bitsPerSecond;
41 tooltip += QString::number(i.ssrc, 16)
42 + " (" + QString::fromStdString(i.item.type) + "): "
43 + getBitrateStr(i.item.bitsPerSecond) + "\n";
44 }
45
46 if(tooltip.endsWith('\n'))
47 tooltip.chop(1);
48
49 setToolTip(tooltip);
50
51 setText(getBitrateStr(total));
52
53 timer.start();
54}
55
56void BandwidthWidget::parseLine(std::string_view line){
57 static constexpr std::string_view prefix = "stats tx_send";
58 if(!sv_is_prefix(line, prefix))
59 return;
60
61 line.remove_prefix(prefix.size());
62
63 auto ssrc_sv = tokenize(line, ' ');
64 if(ssrc_sv.substr(0, 2) == "0x")
65 ssrc_sv.remove_prefix(2);
66 auto type = tokenize(line, ' ');
67 auto bytes_sv = tokenize(line, ' ');
68
69 long long bytes = 0;
70 uint32_t ssrc = 0;
71 if(!parse_num(bytes_sv, bytes)
72 || !parse_num(ssrc_sv, ssrc, 16))
73 {
74 return;
75 }
76
77 BandwidthReport report = {};
78 report.bitsPerSecond = 0;
79 report.type = type;
80
81 for(const auto& r : reports.get()){
82 if(r.ssrc != ssrc)
83 continue;
84
85 auto duration = std::max<qint64>(elapsedTimer.elapsed() - r.timestamp, 1);
86 report.bitsPerSecond = bytes * 8 * 1000 / duration;
87 }
88
89 reports.insert(ssrc, report, elapsedTimer.elapsed());
90 updateVal();
91}
92
94 reports.clear();
95 updateVal();
96 timer.stop();
97}
98
100 reset();
101}
QElapsedTimer elapsedTimer
void parseLine(std::string_view line)
BandwidthWidget(QWidget *parent=nullptr)
SSRC_container< BandwidthReport, decltype(elapsedTimer.elapsed())> reports
steady_clock::duration duration
Definition echo.cpp:67
const char * parent
Definition settings.cpp:198
Option::OptType type
Definition settings.cpp:194
std::string_view tokenize(std::string_view &str, char delim, char quot)
Tokenizer for string_view.
bool parse_num(std::string_view sv, T &res, int base=10)
bool sv_is_prefix(std::string_view haystack, std::string_view needle)