From ce333551244f2e41ec466fb866b7cecdcf232090 Mon Sep 17 00:00:00 2001 From: evgeny Date: Fri, 30 Oct 2026 21:18:01 +0100 Subject: [PATCH] Add stacked desktop audio history, queue, throughput and histogram plots --- tools/chatgui/src/call_audio_graphs.cpp | 140 ++++++++++++++++++++++++ tools/chatgui/src/call_audio_graphs.h | 20 ++++ 2 files changed, 160 insertions(+) create mode 100644 tools/chatgui/src/call_audio_graphs.cpp create mode 100644 tools/chatgui/src/call_audio_graphs.h diff --git a/tools/chatgui/src/call_audio_graphs.cpp b/tools/chatgui/src/call_audio_graphs.cpp new file mode 100644 index 000000000..1cd57bef7 --- /dev/null +++ b/tools/chatgui/src/call_audio_graphs.cpp @@ -0,0 +1,140 @@ +#include "call_audio_graphs.h" +#include "../../../src/call/call_audio.h" +#include "../../../lib/debug_config.h" +#include +#include +#include +#include +#include +#include +#include + +static QString text(const char* value) { return QCoreApplication::translate("ChatGui", value); } +struct GraphPoint { double x, y; bool gap; }; +struct GraphLine { QColor color; std::vector points; }; + +CallAudioGraphs::CallAudioGraphs(quint64 id, QWidget* parent) : QWidget(parent, Qt::Window), m_callId(id) { + setObjectName("callAudioGraphs"); setAttribute(Qt::WA_QuitOnClose, false); + setWindowTitle(text("Audio graphs")); resize(760, 780); setMinimumSize(440, 620); + m_timer = new QTimer(this); m_timer->setInterval(250); + connect(m_timer, &QTimer::timeout, this, [this] { + call_audio_history snapshot; + bool available = call_audio_get_history(m_callId, &snapshot) == 0; + if (available) setSnapshot(snapshot); + else if (m_available) { + DEBUG_INFO(DEBUG_CATEGORY_CALL, "audio graphs frozen call=%llx RX=%u TX=%u", + (unsigned long long)m_callId, m_data.rx_count, m_data.tx_count); + m_available = false; update(); + } + }); +} +void CallAudioGraphs::setSnapshot(const call_audio_history& snapshot) { m_data = snapshot; m_available = true; update(); } +void CallAudioGraphs::showEvent(QShowEvent* event) { + QWidget::showEvent(event); m_timer->start(); + DEBUG_INFO(DEBUG_CATEGORY_CALL, "audio graphs opened call=%llx window_ms=30000 half_life_ms=10000", + (unsigned long long)m_callId); +} +void CallAudioGraphs::hideEvent(QHideEvent* event) { + m_timer->stop(); QWidget::hideEvent(event); + DEBUG_DEBUG(DEBUG_CATEGORY_CALL, "audio graphs hidden call=%llx", (unsigned long long)m_callId); +} + +void CallAudioGraphs::paintEvent(QPaintEvent*) { + QPainter p(this); p.fillRect(rect(), palette().window()); p.setRenderHint(QPainter::Antialiasing); + const QColor foreground = palette().text().color(), muted = palette().color(QPalette::Disabled, QPalette::Text); + const QColor blue("#3498db"), green("#27ae60"), orange("#e67e22"), violet("#9b59b6"), red("#e74c3c"); + p.setPen(foreground); p.drawText(QRectF(16, 8, width() - 32, 22), text("Audio history — last 30 seconds")); + p.setPen(muted); + QString status = m_available ? text("Live") : text("Waiting for audio / last snapshot"); + double missing = m_data.observed_ms > 0 ? 100 * m_data.missing_ms / m_data.observed_ms : 0; + p.drawText(QRectF(16, 30, width() - 32, 22), status + " · " + text("Missing audio: %1% (weighted)").arg(missing, 0, 'f', 1)); + double row = (height() - 68) / 4.0; + auto xValue = [this](uint64_t at) { return (double)CALL_HISTORY_MS - (double)m_data.now_ms + at; }; + auto graph = [&](int index, const QString& title, const QString& legend, const std::vector& lines, + double minimumSpan, const QString& smallUnit, const QString& largeUnit, bool threshold) { + double low = INFINITY, high = -INFINITY; + for (const auto& line : lines) for (const auto& v : line.points) { + low = std::min(low, v.y); high = std::max(high, v.y); + } + bool have = std::isfinite(low); if (!have) low = high = 0; + double factor = !largeUnit.isEmpty() && high >= 1024 ? 1024 : 1; + QString unit = factor == 1024 ? largeUnit : smallUnit; + double span = std::max(high - low, minimumSpan), bottom = std::max(0.0, (high + low - span) / 2 - span * .05); + double top = std::max(high, bottom + span) + span * .05; + double y = 58 + index * row; + p.setPen(foreground); p.drawText(QRectF(16, y, width() - 32, 20), title); + p.setPen(muted); p.drawText(QRectF(16, y + 20, width() - 32, 18), legend); + p.drawText(QRectF(16, y, width() - 32, 20), Qt::AlignRight, + have ? text("Min: %1 · Max: %2 %3").arg(low / factor, 0, 'f', factor == 1 ? 0 : 1) + .arg(high / factor, 0, 'f', factor == 1 ? 0 : 1).arg(unit) : text("No samples")); + QRectF box(62, y + 43, width() - 78, row - 66); + auto px = [&](double time) { return box.left() + time / CALL_HISTORY_MS * box.width(); }; + auto py = [&](double value) { return box.bottom() - (value - bottom) / (top - bottom) * box.height(); }; + p.setPen(muted); p.drawText(QRectF(0, box.top(), 57, 16), Qt::AlignRight, QString::number(top / factor, 'f', factor == 1 ? 0 : 1)); + p.drawText(QRectF(0, box.bottom() - 15, 57, 16), Qt::AlignRight, QString::number(bottom / factor, 'f', factor == 1 ? 0 : 1)); + for (int second = 0; second <= 30; second += 10) { + double x = px(second * 1000); p.setPen(QPen(muted, .4)); p.drawLine(QPointF(x, box.top()), QPointF(x, box.bottom())); + p.drawText(QRectF(x - 20, box.bottom() + 2, 40, 17), Qt::AlignCenter, QString::number(second - 30) + " s"); + } + p.save(); p.setClipRect(box); + if (threshold && bottom <= 3000 && top >= 3000) { + p.setPen(QPen(red, 1, Qt::DashLine)); p.drawLine(QPointF(box.left(), py(3000)), QPointF(box.right(), py(3000))); + } + for (const auto& line : lines) { + QPainterPath path; bool started = false; + for (const auto& v : line.points) { + QPointF pos(px(v.x), py(v.y)); + if (!started || v.gap) path.moveTo(pos); else path.lineTo(pos); + started = true; + } + p.setPen(QPen(line.color, 1.5)); p.drawPath(path); + } + p.restore(); + }; + std::vector receive{{blue, {}}, {green, {}}, {red, {}}}; + for (unsigned i = 0; i < m_data.rx_count; ++i) { + const auto& v = m_data.rx[i]; bool gap = !i || v.at_ms - m_data.rx[i - 1].at_ms > 150; + double x = xValue(v.at_ms); + receive[0].points.push_back({x, (double)v.low_ms, gap}); receive[0].points.push_back({x, (double)v.high_ms, false}); + receive[1].points.push_back({x, (double)v.reserve_ms, gap}); + if (v.missing_samples) { receive[2].points.push_back({x, (double)v.low_ms, true}); receive[2].points.push_back({x, (double)v.high_ms + 10, false}); } + } + graph(0, text("Receive buffer"), text("Blue: buffer · Green: target · Red: missing audio"), receive, 100, text("ms"), {}, true); + std::vector transmit{{blue, {}}, {orange, {}}, {violet, {}}}, rate{{green, {}}}; + for (unsigned i = 0; i < m_data.tx_count; ++i) { + const auto& v = m_data.tx[i]; bool gap = !i || v.at_ms - m_data.tx[i - 1].at_ms > 150; + double x = xValue(v.at_ms); + transmit[0].points.push_back({x, (double)v.queued_bytes + v.inflight_bytes, gap}); + transmit[1].points.push_back({x, (double)v.inflight_bytes, gap}); + transmit[2].points.push_back({x, (double)v.transport_bytes, gap}); + if (v.wire_bytes_per_sec >= 0) rate[0].points.push_back({x, v.wire_bytes_per_sec, + gap || (i && (v.peer_id != m_data.tx[i - 1].peer_id || v.tx_epoch != m_data.tx[i - 1].tx_epoch + || m_data.tx[i - 1].wire_bytes_per_sec < 0))}); + } + graph(1, text("All transmission queues"), text("Blue: Router + waiting audio · Orange: inflight · Purple: transport separately"), + transmit, 1024, text("B"), text("KiB"), false); + graph(2, text("ETCP send rate — call route"), text("All ETCP traffic including retries · Average over 1 second"), + rate, 256, text("B/s"), text("KiB/s"), false); + double y = 58 + 3 * row; p.setPen(foreground); + p.drawText(QRectF(16, y, width() - 32, 20), text("Receive buffer distribution")); + p.setPen(muted); p.drawText(QRectF(16, y + 20, width() - 32, 18), text("Time weighted · Older samples lose half their weight every 10 seconds")); + int first = 0, last = CALL_HISTORY_BINS - 1; + while (first < last && m_data.histogram[first] == 0) ++first; + while (last > first && m_data.histogram[last] == 0) --last; + if (!m_data.observed_ms) first = 0, last = 1; + if (first == last) { first = std::max(0, first - 1); last = std::min(CALL_HISTORY_BINS - 1, last + 1); } + double maximum = 0; for (double v : m_data.histogram) maximum = std::max(maximum, v); + p.drawText(QRectF(16, y, width() - 32, 20), Qt::AlignRight, + text("Min: %1 · Max: %2 %3").arg(0).arg(maximum * 100, 0, 'f', 1).arg("%")); + QRectF box(62, y + 43, width() - 78, row - 66); double scale = std::max(.05, maximum) * 1.05; + double barWidth = box.width() / (last - first + 1); p.setPen(Qt::NoPen); p.setBrush(blue); + for (int i = first; i <= last; ++i) { + double h = m_data.histogram[i] / scale * box.height(); + p.drawRect(QRectF(box.left() + (i - first) * barWidth, box.bottom() - h, std::max(1.0, barWidth - 1), h)); + } + p.setPen(muted); p.drawText(QRectF(0, box.top(), 57, 16), Qt::AlignRight, QString::number(scale * 100, 'f', 0) + "%"); + p.drawText(QRectF(0, box.bottom() - 15, 57, 16), Qt::AlignRight, "0%"); + p.drawText(QRectF(box.left(), box.bottom() + 2, box.width(), 17), Qt::AlignLeft, QString::number(first * CALL_HISTORY_BIN_MS) + " " + text("ms")); + p.drawText(QRectF(box.left(), box.bottom() + 2, box.width(), 17), Qt::AlignRight, + (last == 60 ? QStringLiteral("3000+") : QString::number((last + 1) * CALL_HISTORY_BIN_MS)) + " " + text("ms")); +} diff --git a/tools/chatgui/src/call_audio_graphs.h b/tools/chatgui/src/call_audio_graphs.h new file mode 100644 index 000000000..27b935ef9 --- /dev/null +++ b/tools/chatgui/src/call_audio_graphs.h @@ -0,0 +1,20 @@ +/* Отдельное окно истории звонка. Только GUI-поток; native отдаёт независимые копии. + * Таймер работает лишь при видимом окне. После завершения звонка сохраняется последний снимок. */ +#pragma once +#include +#include "../../../src/call/call_audio_history.h" +class QTimer; +class CallAudioGraphs : public QWidget { +public: + explicit CallAudioGraphs(quint64 callId, QWidget* parent = nullptr); + void setSnapshot(const call_audio_history& snapshot); // Копирует готовый native-снимок, не читает живые очереди. +protected: + void paintEvent(QPaintEvent*) override; + void showEvent(QShowEvent*) override; + void hideEvent(QHideEvent*) override; +private: + quint64 m_callId; + QTimer* m_timer; + call_audio_history m_data{}; + bool m_available = false; +};