Refine GVFG preview pacing and error reporting

- Replace non-blocking Present retries with Present(0, 0), allowing DXGI to
  wait for presentation dependencies on the dedicated preview worker.
- Change preview admission from newest-frame replacement to bounded FIFO
  delivery with three pending/upload reservations and one presenting slot.
- Expire queued preview frames after 50 ms before GPU processing and skip new
  preview submissions only when the bounded slot capacity is full.
- Track queue order and queue age without retaining SDK-owned frame memory;
  capture continues releasing each SDK frame after the synchronous upload.
- Service only synchronous Windows sent messages while waiting for preview
  mutexes, worker shutdown, idle drain, and capture-thread join to avoid an
  HWND-owner/DXGI deadlock without dispatching queued application callbacks.
- Preserve internal preview pacing and audio queue counters while removing
  expected busy, expired, and audio-eviction reports from the normal UI log.
- Reserve delivery error logs for preview/render failures, video or audio
  frame-ID anomalies, audio write failures, and final accounting mismatches.
- Remove the normal stop summary and the audio queue-drop field from the
  status display so expected pacing adjustments are not presented as faults.
- Update the public preview comments and diagnostic documentation for the FIFO
  capacity, queue-age boundary, blocking Present behavior, and log policy.
- Keep the capture SDK, driver interfaces, frame ownership, GPU conversion
  precision, and public delivery-statistics structure unchanged.

Validation:
- User testing reported approximately 18% GPU utilization for one 1920x1080
  Y210 10-bit preview channel. This was not independently benchmarked.
- A user-provided long-run log contained 13,715,695 received video frames with
  no preview render failures; periodic preview skips matched the confirmed
  60.000 FPS versus 59.94 Hz pacing difference.
- The same log showed periodic audio playback-queue eviction without audio
  frame-ID gaps or write failures; this is retained as internal diagnostic
  accounting rather than reported as a source-delivery error.
- Static source review and git diff --check passed. No application, shader, or
  driver build was performed by the agent, and runtime deadlock behavior has
  not been independently reproduced or validated.
This commit is contained in:
Mark.lin
2026-09-07 10:28:03 +08:00
parent 29b449fc6e
commit d03f064b4f
7 changed files with 161 additions and 169 deletions
@@ -1,21 +1,20 @@
# 關鍵交付 Log
畫面保留原本狀態,不顯示完整交付統計,也不增加設定控制。正常運行不定期列印診斷快照。
畫面保留原本狀態,不顯示完整交付統計,也不增加設定控制。正常的預覽與音訊佇列取捨不輸出 Log。
- `Video issue`:預覽累計丟棄與原因。`replaced` 是舊影像被替換,`busy` 是無空槽/DXGI 忙碌跳過,`failed` 是提交或渲染失敗。
- busy 發生時附上 `present`/`slots` 分類累計數與 `last_frame_id`,可確認忙碌來源。
- `Audio issue`:累計丟棄區塊/毫秒數、最長寫入停滯、最長收音間隔,以及最近丟棄 ID/時間。
- 預覽排隊逾時或容量不足是顯示節拍取捨,不輸出 Log。只有提交/渲染失敗或序號異常增加時,才記錄 `ERROR preview delivery`。
- Audio 播放佇列滿而淘汰舊 PCM 區塊不輸出 Log。只有序號異常或寫入失敗時,才記錄 `ERROR audio delivery`。
- 序號缺口、倒退/重複、音訊失敗位元組僅在有異常時附上。
- 異常最多每 5 秒彙整一次;累計數不漏計,最近 ID 不代表列出每個遺失區塊。Stop 會補記尚未輸出的異常。
- Stop 留一行 `Summary`。資料去向核對正常時不另外印訊息,只有不一致才印 `ERROR delivery accounting mismatch`。正常停止清掉的待處理資料不算運行丟失。
- 錯誤最多每 5 秒彙整一次;Stop 會補記尚未輸出的錯誤。
- Stop 不列印正常統計摘要。只有資料去向核對不一致時才印 `ERROR delivery accounting mismatch`。正常停止清掉的待處理資料不算運行錯誤。
## 拿到 Log 後怎麼處理
Video 若以 replaced/busy 為主,查預覽/GPU 負載;failed 則查提交和渲染錯誤。APP 持續讀取並釋放每張影像,沒有新增預覽降頻行為。
`ERROR preview delivery` 表示提交/渲染失敗或 frame ID 異常,需查 Preview API、GPU 與來源序號。APP 持續讀取並釋放每張影像,沒有新增預覽降頻行為。
啟動/重新 attach 預覽後,第一次呈現若 DXGI 回報忙碌,預覽執行緒會保留同一張已上傳的影像,每次等待 2 ms 再試,最多 8 次,且到達 16 ms 重試期限後不再發起重試(排程或呼叫本身可能使實際時間超過期限)。只有最終仍忙碌才計一次 preview drop;成功不算丟棄。平常呈現不重試,不清掉既有計數。等待不持有 SDK frame,但仍可能讓後續預覽影像被替換,替換照常計數。
預覽使用 Present(0, 0),讓 DXGI 等待呈現相依條件,不做外部重試。SyncInterval 仍為 0,並非每張都保證顯示。預覽改成 FIFO,最多保留 3 張待處理影像(包含正在上傳的容量保留),另有 1 個處理中槽位,共 4 個 GPU 槽。60 FPS 下 3 張約 50 ms;這不是刻意等滿 50 ms 才顯示。從上傳完成入列開始計時,等待超過 50 ms 的影像才丟棄(expired);容量滿時跳過新影像(busy),不替換仍在期限內的舊影像。50 ms 是開始處理前的等待期限,不含 GPU 執行/DXGI Present 等待,不保證端到端延遲或實體螢幕逐張呈現。Stop/關閉/鎖等待只處理 Windows 同步送達的視窗訊息,避免 DXGI 等待 UI 而 UI 又等待 worker;不處理一般排隊輸入或 Qt callbacks。
Audio 保留原本最多 10 個 PCM 區塊的播放佇列(`kMaxQueuedAudioFrames`);滿時丟掉最舊區塊,再加入新區塊。毫秒數只用來表示實際丟失的音訊長度,不控制容量。若偶發丟棄,先根據 Log 的寫入停滯/收音間隔查原因,再決定是否需要調整;最長收音間隔包含正常區塊間隔,不能直接當成丟失時間。
Audio 保留原本最多 10 個 PCM 區塊的播放佇列(`kMaxQueuedAudioFrames`);滿時丟掉最舊區塊,再加入新區塊。丟棄區塊、停滯與收音間隔仍保留為內部診斷計數,但不顯示在一般 Log;audio frame 不保證與 video frame 等長。
播放 write 失敗,或持續回傳 0、兩秒沒有進展,會記錄原因並停止通道;檢查音效裝置後重新 Start。此檢查無法中斷本身永不返回的 write 呼叫。
+25 -39
View File
@@ -26,6 +26,11 @@
#include <utility>
#include <vector>
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
namespace
{
constexpr bool kChannel1UiVisible = false;
@@ -791,12 +796,11 @@ void MainWindow::updateSignalStatus(bool queryHardware)
.arg(zeroCopy ? QStringLiteral("Zero-copy") : QStringLiteral("Copy"));
if (channel.audioEnabled)
{
statusLines << QStringLiteral("CH%1 Audio | %2 Hz %3 ch %4-bit | queue_drops=%5")
statusLines << QStringLiteral("CH%1 Audio | %2 Hz %3 ch %4-bit")
.arg(channelIndex)
.arg(channel.audioFormat.sample_rate)
.arg(channel.audioFormat.channels)
.arg(channel.audioFormat.bits_per_sample)
.arg(static_cast<qulonglong>(channel.audioQueueDrops.load(std::memory_order_relaxed)));
.arg(channel.audioFormat.bits_per_sample);
}
statusLines << QStringLiteral("CH%1 Preview | %2 FPS | %3")
.arg(channelIndex)
@@ -1041,38 +1045,22 @@ void MainWindow::logDeliveryStatus(int channelIndex, bool finalSnapshot)
const bool previewOk = c.previewHandle &&
gvfg_preview_get_delivery_stats(c.previewHandle, &p) == GVFG_PREVIEW_OK;
const auto &b = c.previewBaseline;
const uint64_t replaced = previewOk ? p.replaced - b.replaced : 0;
const uint64_t busy = previewOk ? p.busy - b.busy : 0;
const uint64_t failed = (previewOk ? p.failed - b.failed : 0) + c.videoFailed.load();
const uint64_t videoDrops = replaced + busy + failed;
const uint64_t videoIssues = videoDrops + c.videoIdGaps.load() + c.videoIdResets.load();
const uint64_t videoIssues = failed + c.videoIdGaps.load() + c.videoIdResets.load();
uint64_t audioIssues, audioDrops, audioGaps, audioResets, audioFailed, lastDropId;
double droppedMs, writeStallMs, readGapMs;
uint64_t audioIssues, audioGaps, audioResets, audioFailed;
bool audioAccounted;
qint64 lastDropTime;
{
std::lock_guard<std::mutex> lock(c.audioQueueMutex);
const double bytesPerSecond = static_cast<double>(c.audioFormat.sample_rate) *
c.audioFormat.channels * (c.audioFormat.bits_per_sample / 8);
droppedMs = bytesPerSecond > 0 ? c.audioDroppedBytes * 1000.0 / bytesPerSecond : 0;
audioDrops = c.audioQueueDrops.load();
audioGaps = c.audioIdGaps;
audioResets = c.audioIdResets;
audioFailed = c.audioFailedBytes;
audioIssues = audioDrops + audioGaps + audioResets + c.audioFailedBytes;
writeStallMs = c.audioMaxWriteStallMs;
readGapMs = c.audioMaxReadGapMs;
lastDropId = c.audioLastDropId;
lastDropTime = c.audioLastDropTimeMs;
audioIssues = audioGaps + audioResets + audioFailed;
audioAccounted = c.audioQueuedBytes == 0 && c.audioReceivedBytes ==
c.audioAcceptedBytes + c.audioDroppedBytes + c.audioCancelledBytes + c.audioFailedBytes;
}
if (finalSnapshot)
{
appendLog(QStringLiteral("CH%1 Summary | video_received=%2 preview_drops=%3 | audio_drops=%4 (%5 ms)")
.arg(channelIndex).arg(count(c.videoReceived.load())).arg(count(videoDrops))
.arg(count(audioDrops)).arg(droppedMs, 0, 'f', 1));
const bool videoAccounted = previewOk && c.videoReceived.load() ==
c.videoSubmitted.load() + c.videoFailed.load() &&
c.videoSubmitted.load() == p.submitted - b.submitted;
@@ -1084,27 +1072,15 @@ void MainWindow::logDeliveryStatus(int channelIndex, bool finalSnapshot)
// Also flush any changes since the last aggregate when stopping.
if (videoIssues != c.lastLoggedVideoIssues)
{
QString message = QStringLiteral("CH%1 Video issue +%2 | preview_drops=%3 (replaced=%4 busy=%5 failed=%6)")
.arg(channelIndex).arg(count(videoIssues - c.lastLoggedVideoIssues)).arg(count(videoDrops))
.arg(count(replaced)).arg(count(busy)).arg(count(failed));
if (busy)
message += QStringLiteral(" | busy_reason: present=%1 slots=%2 last_frame_id=%3")
.arg(count(p.present_busy - b.present_busy)).arg(count(p.slots_busy - b.slots_busy))
.arg(count(p.last_busy_id));
if (c.videoIdGaps.load() || c.videoIdResets.load())
message += QStringLiteral(" | id_gaps=%1 reset_or_duplicate=%2")
.arg(count(c.videoIdGaps.load())).arg(count(c.videoIdResets.load()));
QString message = QStringLiteral("CH%1 ERROR preview delivery | failed=%2 id_gaps=%3 reset_or_duplicate=%4")
.arg(channelIndex).arg(count(failed)).arg(count(c.videoIdGaps.load()))
.arg(count(c.videoIdResets.load()));
appendLog(message);
}
if (audioIssues != c.lastLoggedAudioIssues)
{
QString message = QStringLiteral("CH%1 Audio issue | drops=%2 (%3 ms) max_write_stall=%4 ms max_read_gap=%5 ms last_drop=%6@%7")
.arg(channelIndex).arg(count(audioDrops)).arg(droppedMs, 0, 'f', 1)
.arg(writeStallMs, 0, 'f', 1).arg(readGapMs, 0, 'f', 1).arg(count(lastDropId))
.arg(lastDropTime ? QDateTime::fromMSecsSinceEpoch(lastDropTime).toString(QStringLiteral("HH:mm:ss.zzz")) : QStringLiteral("--"));
if (audioGaps || audioResets || audioFailed)
message += QStringLiteral(" | id_gaps=%1 reset_or_duplicate=%2 failed_bytes=%3")
.arg(count(audioGaps)).arg(count(audioResets)).arg(count(audioFailed));
QString message = QStringLiteral("CH%1 ERROR audio delivery | id_gaps=%2 reset_or_duplicate=%3 failed_bytes=%4")
.arg(channelIndex).arg(count(audioGaps)).arg(count(audioResets)).arg(count(audioFailed));
appendLog(message);
}
if (finalSnapshot || videoIssues != c.lastLoggedVideoIssues || audioIssues != c.lastLoggedAudioIssues)
@@ -1327,7 +1303,17 @@ void MainWindow::captureReadLoop(int channelIndex)
void MainWindow::joinCaptureThread(int channel)
{
if (channels_[channel].captureThread.joinable())
{
// The capture worker can wait behind preview resource updates. Keep
// DXGI's synchronous HWND messages flowing while joining it.
while (WaitForSingleObject(channels_[channel].captureThread.native_handle(), 0) == WAIT_TIMEOUT)
{
MsgWaitForMultipleObjectsEx(0, nullptr, 1, QS_SENDMESSAGE, MWMO_INPUTAVAILABLE);
MSG message{};
PeekMessageW(&message, nullptr, 0, 0, PM_NOREMOVE | PM_QS_SENDMESSAGE);
}
channels_[channel].captureThread.join();
}
}
void MainWindow::audioReadLoop(int channelIndex)