From 16e9a63eb6628b01bf0f71297c1643c46ad80c9e Mon Sep 17 00:00:00 2001 From: wiiiii123 Date: Sun, 19 Apr 2026 20:20:06 +0700 Subject: [PATCH] fix(windows): normalize WGC sample timestamps --- electron/native/wgc-capture/src/main.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/electron/native/wgc-capture/src/main.cpp b/electron/native/wgc-capture/src/main.cpp index c949e8a2..ae0a50a0 100644 --- a/electron/native/wgc-capture/src/main.cpp +++ b/electron/native/wgc-capture/src/main.cpp @@ -19,6 +19,7 @@ static std::atomic g_stopRequested{false}; static std::atomic g_pauseRequested{false}; static std::atomic g_resumePending{false}; static std::atomic g_lastFrameTimestampHns{0}; +static std::atomic g_firstFrameTimestampHns{-1}; static std::atomic g_pauseStartTimestampHns{0}; static std::atomic g_accumulatedPausedHns{0}; static std::mutex g_stopMutex; @@ -257,6 +258,16 @@ int main(int argc, char* argv[]) { if (g_pauseRequested) return; + int64_t firstFrameTimestampHns = g_firstFrameTimestampHns.load(); + if (firstFrameTimestampHns < 0) { + int64_t expected = -1; + if (g_firstFrameTimestampHns.compare_exchange_strong(expected, timestampHns)) { + firstFrameTimestampHns = timestampHns; + } else { + firstFrameTimestampHns = g_firstFrameTimestampHns.load(); + } + } + int64_t adjustedTimestampHns = timestampHns; if (g_resumePending.exchange(false)) { const int64_t pauseStart = g_pauseStartTimestampHns.load(); @@ -265,6 +276,7 @@ int main(int argc, char* argv[]) { } } + adjustedTimestampHns -= firstFrameTimestampHns; adjustedTimestampHns -= g_accumulatedPausedHns.load(); if (adjustedTimestampHns < 0) { adjustedTimestampHns = 0;