|
| 1 | +#include "SessionTelemetrySampler.h" |
| 2 | + |
| 3 | +#include <QJsonArray> |
| 4 | +#include <QJsonDocument> |
| 5 | +#include <QJsonObject> |
| 6 | + |
| 7 | +#include "streaming/session.h" |
| 8 | +#include "streaming/video/ffmpeg.h" |
| 9 | + |
| 10 | +SessionTelemetrySampler::SessionTelemetrySampler(QObject* parent) |
| 11 | + : QObject(parent) |
| 12 | +{ |
| 13 | + m_SampleTimer.setInterval(1000); |
| 14 | + m_SampleTimer.setSingleShot(false); |
| 15 | + connect(&m_SampleTimer, &QTimer::timeout, this, &SessionTelemetrySampler::onSampleTimer); |
| 16 | + |
| 17 | + m_BatchTimer.setInterval(10000); |
| 18 | + m_BatchTimer.setSingleShot(false); |
| 19 | + connect(&m_BatchTimer, &QTimer::timeout, this, &SessionTelemetrySampler::onBatchTimer); |
| 20 | +} |
| 21 | + |
| 22 | +void SessionTelemetrySampler::start(const QString& hostAddress, int targetFps) |
| 23 | +{ |
| 24 | + m_HostAddress = hostAddress; |
| 25 | + m_TargetFps = targetFps; |
| 26 | + |
| 27 | + // Start sampling immediately — no session ID negotiation needed. |
| 28 | + // StreamTweak accepts batches whenever a session is active, regardless |
| 29 | + // of NIC throttle mode. Telemetry is fully independent of streaming settings. |
| 30 | + m_SampleTimer.start(); |
| 31 | + m_BatchTimer.start(); |
| 32 | +} |
| 33 | + |
| 34 | +void SessionTelemetrySampler::onSampleTimer() |
| 35 | +{ |
| 36 | + Session* session = Session::get(); |
| 37 | + if (!session) return; |
| 38 | + |
| 39 | + // Retrieve last-window stats via the thread-safe accessor |
| 40 | + SDL_LockMutex(session->decoderLock()); |
| 41 | + IVideoDecoder* dec = session->videoDecoder(); |
| 42 | + TelemetryWindowStats ws = {}; |
| 43 | + if (dec) { |
| 44 | + auto* ffDec = dynamic_cast<FFmpegVideoDecoder*>(dec); |
| 45 | + if (ffDec) |
| 46 | + ws = ffDec->getLastWindowStats(); |
| 47 | + } |
| 48 | + SDL_UnlockMutex(session->decoderLock()); |
| 49 | + |
| 50 | + // Skip the sample if the decoder window hasn't been filled yet (first second). |
| 51 | + // fpsAvg == 0 means no frames have been rendered; including it would corrupt |
| 52 | + // the batch average and force fps_min to 0 for the entire batch. |
| 53 | + if (ws.fpsAvg == 0.0) return; |
| 54 | + |
| 55 | + TelemetrySample s; |
| 56 | + s.fpsAvg = (float)ws.fpsAvg; |
| 57 | + s.fpsMin = s.fpsAvg; // will be refined across batch as running min |
| 58 | + s.drops = ws.drops; |
| 59 | + s.rttAvg = (float)ws.rttAvgMs; |
| 60 | + s.rttMax = s.rttAvg; // will be refined across batch as running max |
| 61 | + s.decodeMs = ws.decodeAvgMs; |
| 62 | + s.bitrateMbps = ws.bitrateMbps; |
| 63 | + |
| 64 | + // Track running min/max within the current batch |
| 65 | + if (s.fpsAvg < m_BatchFpsMin) m_BatchFpsMin = s.fpsAvg; |
| 66 | + if (s.rttAvg > m_BatchRttMax) m_BatchRttMax = s.rttAvg; |
| 67 | + |
| 68 | + m_Samples.append(s); |
| 69 | +} |
| 70 | + |
| 71 | +void SessionTelemetrySampler::onBatchTimer() |
| 72 | +{ |
| 73 | + sendBatch(); |
| 74 | +} |
| 75 | + |
| 76 | +void SessionTelemetrySampler::flushAndStop() |
| 77 | +{ |
| 78 | + m_SampleTimer.stop(); |
| 79 | + m_BatchTimer.stop(); |
| 80 | + |
| 81 | + if (m_Samples.isEmpty()) return; |
| 82 | + |
| 83 | + // Apply batch-level min/max before the final send |
| 84 | + for (auto& s : m_Samples) { |
| 85 | + s.fpsMin = m_BatchFpsMin; |
| 86 | + s.rttMax = m_BatchRttMax; |
| 87 | + } |
| 88 | + |
| 89 | + // Use synchronous send: exec() has returned and the Qt event loop is no |
| 90 | + // longer pumping, so the async socket in sendSessionData() would never |
| 91 | + // complete. sendSessionDataSync() blocks until the data is written. |
| 92 | + m_Bridge.sendSessionDataSync(m_HostAddress, buildBatchJson()); |
| 93 | + m_Samples.clear(); |
| 94 | +} |
| 95 | + |
| 96 | +void SessionTelemetrySampler::sendBatch() |
| 97 | +{ |
| 98 | + if (m_Samples.isEmpty()) return; |
| 99 | + |
| 100 | + // Apply batch-level min/max to each sample's fpsMin and rttMax fields |
| 101 | + for (auto& s : m_Samples) { |
| 102 | + s.fpsMin = m_BatchFpsMin; |
| 103 | + s.rttMax = m_BatchRttMax; |
| 104 | + } |
| 105 | + |
| 106 | + QString json = buildBatchJson(); |
| 107 | + m_Bridge.sendSessionData(m_HostAddress, json); |
| 108 | + |
| 109 | + m_Samples.clear(); |
| 110 | + m_BatchFpsMin = 9999.0f; |
| 111 | + m_BatchRttMax = -1.0f; |
| 112 | +} |
| 113 | + |
| 114 | +QString SessionTelemetrySampler::buildBatchJson() const |
| 115 | +{ |
| 116 | + QJsonArray samplesArray; |
| 117 | + for (const auto& s : m_Samples) { |
| 118 | + QJsonObject obj; |
| 119 | + obj[QStringLiteral("fps_avg")] = qRound(s.fpsAvg * 10) / 10.0; |
| 120 | + obj[QStringLiteral("fps_min")] = (int)s.fpsMin; |
| 121 | + obj[QStringLiteral("drops")] = s.drops; |
| 122 | + obj[QStringLiteral("rtt_avg")] = qRound(s.rttAvg * 10) / 10.0; |
| 123 | + obj[QStringLiteral("rtt_max")] = qRound(s.rttMax * 10) / 10.0; |
| 124 | + obj[QStringLiteral("decode_ms")] = qRound(s.decodeMs * 10) / 10.0; |
| 125 | + obj[QStringLiteral("bitrate_mbps")] = qRound(s.bitrateMbps * 10) / 10.0; |
| 126 | + samplesArray.append(obj); |
| 127 | + } |
| 128 | + |
| 129 | + QJsonObject root; |
| 130 | + root[QStringLiteral("target_fps")] = m_TargetFps; |
| 131 | + root[QStringLiteral("samples")] = samplesArray; |
| 132 | + |
| 133 | + // Compact serialization — no embedded newlines (required by bridge protocol) |
| 134 | + return QString::fromUtf8( |
| 135 | + QJsonDocument(root).toJson(QJsonDocument::Compact)); |
| 136 | +} |
0 commit comments