diff --git a/GVFG_Qt_Sample/DELIVERY_DIAGNOSTICS.md b/GVFG_Qt_Sample/DELIVERY_DIAGNOSTICS.md index a124747..f2b035a 100644 --- a/GVFG_Qt_Sample/DELIVERY_DIAGNOSTICS.md +++ b/GVFG_Qt_Sample/DELIVERY_DIAGNOSTICS.md @@ -2,15 +2,15 @@ 畫面保留原本狀態,不顯示完整交付統計,也不增加設定控制。正常的預覽與音訊佇列取捨不輸出 Log。 -- 預覽排隊逾時或容量不足是顯示節拍取捨,不輸出 Log。只有提交/渲染失敗或序號異常增加時,才記錄 `ERROR preview delivery`。 -- Audio 播放佇列滿而淘汰舊 PCM 區塊不輸出 Log。只有序號異常或寫入失敗時,才記錄 `ERROR audio delivery`。 -- 序號缺口、倒退/重複、音訊失敗位元組僅在有異常時附上。 -- 錯誤最多每 5 秒彙整一次;Stop 會補記尚未輸出的錯誤。 -- Stop 不列印正常統計摘要。只有資料去向核對不一致時才印 `ERROR delivery accounting mismatch`。正常停止清掉的待處理資料不算運行錯誤。 +- `[LIB]`:GigabyteLib 回傳的 signal 或 event 現象。 +- `[SDK]`/`[SDK API]`:SDK read 邊界或 API 回傳的現象。 +- `[APP]`:Preview、audio output 或 Sample 自身核對的現象。 +- 累計數字最多每 5 秒彙整一次;Stop 會補記尚未輸出的變化。 +- Stop 不列印正常統計摘要。只有資料去向核對不一致時才印 `[APP] ERROR accounting mismatch`。正常停止清掉的待處理資料不算運行錯誤。 ## 拿到 Log 後怎麼處理 -`ERROR preview delivery` 表示提交/渲染失敗或 frame ID 異常,需查 Preview API、GPU 與來源序號。APP 持續讀取並釋放每張影像,沒有新增預覽降頻行為。 +Log 只標示觀測層、現象與數值。Video/audio `frame_id` 是 SDK 每次 Start 從 1 開始的成功交付序號,不再使用 GigabyteLib `FrameCount`;`[APP] Preview ...` 是 Sample 顯示路徑問題。 預覽使用 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。 diff --git a/GVFG_Qt_Sample/capture_controller.cpp b/GVFG_Qt_Sample/capture_controller.cpp index 64ee867..b41a917 100644 --- a/GVFG_Qt_Sample/capture_controller.cpp +++ b/GVFG_Qt_Sample/capture_controller.cpp @@ -38,12 +38,6 @@ namespace #if GVFG_INTERNAL_DIAGNOSTICS QString boolText(int value) { return value ? QStringLiteral("yes") : QStringLiteral("no"); } - QString captureStatusText(const gvfg_debug_backend_stats_t &stats, bool signalConnected) - { - if (!stats.running) return QStringLiteral("stopped"); - if (!signalConnected) return QStringLiteral("waiting_signal"); - return QStringLiteral("streaming"); - } #endif QString frameText(bool valid, int width, int height, const char *pixelFormat, int bitDepth) @@ -193,7 +187,7 @@ void CaptureController::refreshDevices() if (deviceCount_ <= 0) { - appendLog(QStringLiteral("No GVFG device found")); + appendLog(QStringLiteral("[SDK] ERROR no GVFG device found")); } else { @@ -209,7 +203,7 @@ bool CaptureController::openDevice() if (selectedDeviceIndex_ < 0) { - appendLog(QStringLiteral("Open skipped: no device selected")); + appendLog(QStringLiteral("[APP] Open skipped | no device selected")); return false; } @@ -332,7 +326,7 @@ void CaptureController::startCapture(int channelIndex) processPendingEvents(); if (!channel.haveCachedSignalStatus || !channel.cachedSignalStatus.connected) { - appendLog(QStringLiteral("CH%1 Start skipped: no input signal").arg(channelIndex)); + appendLog(QStringLiteral("CH%1 [LIB] No input signal; start skipped").arg(channelIndex)); return; } @@ -357,7 +351,7 @@ void CaptureController::startCapture(int channelIndex) channel.audioFormat.bits_per_sample != 16 && channel.audioFormat.bits_per_sample != 32)) { - appendLog(QStringLiteral("CH%1 Audio format unsupported by Qt playback | %2 Hz %3 ch %4-bit") + appendLog(QStringLiteral("CH%1 [APP] ERROR audio format unsupported | %2 Hz %3 ch %4-bit") .arg(channelIndex) .arg(channel.audioFormat.sample_rate) .arg(channel.audioFormat.channels) @@ -381,7 +375,7 @@ void CaptureController::startCapture(int channelIndex) channel.cachedSignalStatus.height, channel.cachedSignalStatus.bit_depth) != GVFG_PREVIEW_OK) { - appendLog(QStringLiteral("CH%1 Preview prewarm failed; first frame may initialize GPU resources").arg(channelIndex)); + appendLog(QStringLiteral("CH%1 [APP] Preview prewarm failed").arg(channelIndex)); } #if GVFG_INTERNAL_DIAGNOSTICS @@ -390,7 +384,7 @@ void CaptureController::startCapture(int channelIndex) if (gvfg_preview_wait_idle(channel.previewHandle, 2000) != GVFG_PREVIEW_OK) { - appendLog(QStringLiteral("CH%1 Preview still busy from previous run; retry Start after it completes.").arg(channelIndex)); + appendLog(QStringLiteral("CH%1 [APP] WARNING preview busy from previous run; start skipped").arg(channelIndex)); return; } channel.startupStartTime = std::chrono::steady_clock::now(); @@ -417,20 +411,19 @@ void CaptureController::startCapture(int channelIndex) channel.audioEnabled = audioEnabled; channel.videoReceived = 0; channel.videoSubmitted = 0; channel.videoSkipped = 0; channel.videoFailed = 0; - channel.videoIdGaps = 0; channel.videoIdResets = 0; - channel.videoLastId = 0; - channel.lastLoggedVideoIssues = channel.lastLoggedAudioIssues = 0; + channel.lastLoggedPreviewFailures = 0; + channel.lastLoggedAudioReleaseFailures = channel.lastLoggedAudioOutputFailures = 0; channel.lastDeliveryLogMs = 0; channel.previewBaseline = {}; gvfg_preview_get_delivery_stats(channel.previewHandle, &channel.previewBaseline); + channel.backendBaseline = {}; + gvfg_debug_get_channel_backend_stats(handle_, channelIndex, &channel.backendBaseline); { std::lock_guard lock(channel.audioQueueMutex); channel.audioQueue.clear(); - channel.audioReceivedFrames = 0; - channel.audioReceivedBytes = channel.audioAcceptedBytes = channel.audioQueuedBytes = 0; - channel.audioDroppedBytes = channel.audioCancelledBytes = 0; - channel.audioFailedBytes = channel.audioIdGaps = channel.audioIdResets = 0; - channel.audioLastId = 0; + channel.audioReceivedFrames = channel.audioOutputFrames = 0; + channel.audioDroppedFrames = channel.audioCancelledFrames = 0; + channel.audioReleaseFailedFrames = channel.audioOutputFailedFrames = 0; } channel.signalConnected.store(channel.cachedSignalStatus.connected != 0, std::memory_order_release); @@ -468,7 +461,7 @@ void CaptureController::stopCapture(int channelIndex) joinCaptureThread(channelIndex); joinAudioThread(channelIndex); if (gvfg_preview_wait_idle(channel.previewHandle, 2000) != GVFG_PREVIEW_OK) - appendLog(QStringLiteral("CH%1 Preview drain timed out; remaining in_flight is not counted as lost.").arg(channelIndex)); + appendLog(QStringLiteral("CH%1 [APP] WARNING preview drain timeout | in-flight not classified as lost").arg(channelIndex)); logDeliveryStatus(channelIndex, true); gvfg_stop_channel(handle_, channelIndex); channel.running.store(false, std::memory_order_release); @@ -497,7 +490,7 @@ bool CaptureController::applyPreview(int channelIndex) const gvfg_preview_status_t st = gvfg_preview_create(&channel.previewHandle); if (st != GVFG_PREVIEW_OK || !channel.previewHandle) { - appendLog(QStringLiteral("CH%1 Preview setup failed: %2") + appendLog(QStringLiteral("CH%1 [APP] Preview setup failed | %2") .arg(channelIndex) .arg(QString::fromUtf8(gvfg_preview_strerror(st)))); channel.previewHandle = nullptr; @@ -507,14 +500,14 @@ bool CaptureController::applyPreview(int channelIndex) if (!channel.previewTarget) { - appendLog(QStringLiteral("CH%1 Preview setup failed: no native window target").arg(channelIndex)); + appendLog(QStringLiteral("CH%1 [APP] Preview setup failed | no native window target").arg(channelIndex)); return false; } const gvfg_preview_status_t st = gvfg_preview_attach_window( channel.previewHandle, channel.previewTarget); if (st != GVFG_PREVIEW_OK) { - appendLog(QStringLiteral("CH%1 Preview setup failed: %2") + appendLog(QStringLiteral("CH%1 [APP] Preview setup failed | %2") .arg(channelIndex) .arg(QString::fromUtf8(gvfg_preview_strerror(st)))); return false; @@ -537,7 +530,7 @@ void CaptureController::processPendingEvents() { const auto eventType = static_cast(event.type); if (eventType != GVFG_EVENT_STREAM_READY) - appendLog(QStringLiteral("CH%1 EVENT %2").arg(channel).arg(eventTypeText(eventType))); + appendLog(QStringLiteral("CH%1 [LIB] Event | %2").arg(channel).arg(eventTypeText(eventType))); if (eventType == GVFG_EVENT_SIGNAL_CONNECTED || eventType == GVFG_EVENT_SIGNAL_DISCONNECTED || @@ -604,7 +597,7 @@ void CaptureController::updateSignalStatus(bool queryHardware) : QStringLiteral("CH%1 No signal").arg(channelIndex); if (inputStatus != channel.lastLoggedInputStatus) { - appendLog(QStringLiteral("Input status | %1").arg(inputStatus)); + appendLog(QStringLiteral("[LIB] Input status | %1").arg(inputStatus)); channel.lastLoggedInputStatus = inputStatus; } if (channel.previewVisible.load(std::memory_order_acquire) && signal.width > 0 && signal.height > 0) @@ -617,6 +610,9 @@ void CaptureController::updateSignalStatus(bool queryHardware) gvfg_preview_stats_t previewStats{}; const bool previewStatsOk = channel.previewHandle && gvfg_preview_get_stats(channel.previewHandle, &previewStats) == GVFG_PREVIEW_OK; + gvfg_preview_delivery_stats_t previewDelivery{}; + const bool previewDeliveryOk = channel.previewHandle && + gvfg_preview_get_delivery_stats(channel.previewHandle, &previewDelivery) == GVFG_PREVIEW_OK; const QString previewFps = previewStatsOk && previewStats.present_fps > 0.0 ? QString::number(previewStats.present_fps, 'f', 2) : QStringLiteral("--"); @@ -645,25 +641,21 @@ void CaptureController::updateSignalStatus(bool queryHardware) .arg(channel.audioFormat.bits_per_sample) .arg(static_cast(receivedFrames)); #if GVFG_INTERNAL_DIAGNOSTICS - uint64_t sdkBytes = 0; - uint64_t appOutputBytes = 0; + uint64_t appOutputFrames = 0; { std::lock_guard lock(channel.audioQueueMutex); - sdkBytes = channel.audioReceivedBytes; - appOutputBytes = channel.audioAcceptedBytes; + appOutputFrames = channel.audioOutputFrames; } gvfg_debug_backend_stats_t audioStats{}; if (gvfg_debug_get_channel_backend_stats(handle_, channelIndex, &audioStats) == GVFG_OK) { - statusLines << QStringLiteral("CH%1 Audio Debug | Events DMA=%2 Extra=%3 | Driver->SDK %4 frames/%5 bytes | SDK->APP %6 frames/%7 bytes | APP->Output %8 bytes") + statusLines << QStringLiteral("CH%1 Audio Debug | LIB events DMA=%2 Extra=%3 | LIB->SDK %4 frames | SDK->APP %5 frames | APP->Output %6 frames") .arg(channelIndex) - .arg(static_cast(audioStats.audio_dma_event_wakes)) - .arg(static_cast(audioStats.extra_audio_event_wakes)) - .arg(static_cast(audioStats.audio_frames_from_driver)) - .arg(static_cast(audioStats.audio_bytes_from_driver)) + .arg(static_cast(audioStats.audio_dma_event_wakes - channel.backendBaseline.audio_dma_event_wakes)) + .arg(static_cast(audioStats.extra_audio_event_wakes - channel.backendBaseline.extra_audio_event_wakes)) + .arg(static_cast(audioStats.audio_frames_from_lib - channel.backendBaseline.audio_frames_from_lib)) .arg(static_cast(receivedFrames)) - .arg(static_cast(sdkBytes)) - .arg(static_cast(appOutputBytes)); + .arg(static_cast(appOutputFrames)); } #endif } @@ -689,18 +681,21 @@ void CaptureController::updateSignalStatus(bool queryHardware) ? QStringLiteral("CH%1 %2 | avg=%3 max300=%4 max=%5 us samples=%6") .arg(channelIndex) .arg(channel.zeroCopy - ? QStringLiteral("ZeroCopy Acquire") - : QStringLiteral("Copy GetFrame")) + ? QStringLiteral("[LIB] GvfgGetVideoFrameZeroCopy") + : QStringLiteral("[LIB] GvfgGetVideoFrame")) .arg(channelStats.get_frame_timing_average_us, 0, 'f', 3) .arg(channelStats.get_frame_timing_max300_us, 0, 'f', 3) .arg(channelStats.get_frame_timing_max_us, 0, 'f', 3) .arg(static_cast(channelStats.get_frame_timing_samples)) - : QStringLiteral("CH%1 Driver GetFrame | measuring").arg(channelIndex)); - statusLines << QStringLiteral("CH%1 Capture | status=%2 no_frame_waits=%3 video_wakes=%4") + : QStringLiteral("CH%1 [LIB] GetVideoFrame | measuring").arg(channelIndex)); + statusLines << QStringLiteral("CH%1 Video Debug | LIB events=%2 | LIB->SDK %3 frames | SDK->APP %4 frames | APP->Preview %5 frames | Preview->DXGI %6 frames") .arg(channelIndex) - .arg(captureStatusText(channelStats, signal.connected != 0)) - .arg(static_cast(channelStats.frame_wait_timeouts)) - .arg(static_cast(channelStats.video_event_wakes)); + .arg(static_cast(channelStats.video_event_wakes - channel.backendBaseline.video_event_wakes)) + .arg(static_cast(channelStats.video_frames_from_lib - channel.backendBaseline.video_frames_from_lib)) + .arg(static_cast(channel.videoReceived.load())) + .arg(static_cast(channel.videoSubmitted.load())) + .arg(static_cast(previewDeliveryOk + ? previewDelivery.presented - channel.previewBaseline.presented : 0)); } #endif } @@ -718,7 +713,7 @@ void CaptureController::updateSignalStatus(bool queryHardware) void CaptureController::reportError(const QString &apiName, gvfg_status_t status, int channel) { - QString message = QStringLiteral("%1 failed: %2") + QString message = QStringLiteral("[SDK API] %1 failed | %2") .arg(apiName, QString::fromUtf8(gvfg_strerror(status))); if (channel == GVFG_CHANNEL_0 || channel == GVFG_CHANNEL_1) message.prepend(QStringLiteral("CH%1 ").arg(channel)); @@ -864,18 +859,15 @@ void CaptureController::logDeliveryStatus(int channelIndex, bool finalSnapshot) gvfg_preview_get_delivery_stats(c.previewHandle, &p) == GVFG_PREVIEW_OK; const auto &b = c.previewBaseline; const uint64_t failed = (previewOk ? p.failed - b.failed : 0) + c.videoFailed.load(); - const uint64_t videoIssues = failed + c.videoIdGaps.load() + c.videoIdResets.load(); - - uint64_t audioIssues, audioGaps, audioResets, audioFailed; + uint64_t audioReleaseFailed, audioOutputFailed; bool audioAccounted; { std::lock_guard lock(c.audioQueueMutex); - audioGaps = c.audioIdGaps; - audioResets = c.audioIdResets; - audioFailed = c.audioFailedBytes; - audioIssues = audioGaps + audioResets + audioFailed; - audioAccounted = c.audioQueuedBytes == 0 && c.audioReceivedBytes == - c.audioAcceptedBytes + c.audioDroppedBytes + c.audioCancelledBytes + c.audioFailedBytes; + audioReleaseFailed = c.audioReleaseFailedFrames; + audioOutputFailed = c.audioOutputFailedFrames; + audioAccounted = c.audioQueue.empty() && c.audioReceivedFrames == + c.audioOutputFrames + c.audioDroppedFrames + c.audioCancelledFrames + + c.audioReleaseFailedFrames + c.audioOutputFailedFrames; } if (finalSnapshot) { @@ -883,29 +875,37 @@ void CaptureController::logDeliveryStatus(int channelIndex, bool finalSnapshot) c.videoSubmitted.load() + c.videoSkipped.load() + c.videoFailed.load() && c.videoSubmitted.load() == p.submitted - b.submitted; if (!videoAccounted || !audioAccounted) - appendLog(QStringLiteral("CH%1 ERROR delivery accounting mismatch | video=%2 audio=%3") + appendLog(QStringLiteral("CH%1 [APP] ERROR accounting mismatch | video=%2 audio=%3") .arg(QString::number(channelIndex), videoAccounted ? QStringLiteral("OK") : QStringLiteral("MISMATCH"), audioAccounted ? QStringLiteral("OK") : QStringLiteral("MISMATCH"))); } // Also flush any changes since the last aggregate when stopping. - if (videoIssues != c.lastLoggedVideoIssues) + if (failed != c.lastLoggedPreviewFailures) { - QString message = QStringLiteral("CH%1 ERROR preview delivery | failed=%2 id_gaps=%3 reset_or_duplicate=%4") - .arg(QString::number(channelIndex), count(failed), count(c.videoIdGaps.load()), - count(c.videoIdResets.load())); + QString message = QStringLiteral("CH%1 [APP] ERROR preview submission | failures=%2") + .arg(QString::number(channelIndex), count(failed)); appendLog(message); } - if (audioIssues != c.lastLoggedAudioIssues) + if (audioReleaseFailed != c.lastLoggedAudioReleaseFailures) { - QString message = QStringLiteral("CH%1 ERROR audio delivery | id_gaps=%2 reset_or_duplicate=%3 failed_bytes=%4") - .arg(QString::number(channelIndex), count(audioGaps), count(audioResets), count(audioFailed)); + QString message = QStringLiteral("CH%1 [SDK API] ERROR audio frame release | failed_frames=%2") + .arg(QString::number(channelIndex), count(audioReleaseFailed)); appendLog(message); } - if (finalSnapshot || videoIssues != c.lastLoggedVideoIssues || audioIssues != c.lastLoggedAudioIssues) + if (audioOutputFailed != c.lastLoggedAudioOutputFailures) + { + QString message = QStringLiteral("CH%1 [APP] ERROR audio output write | failed_frames=%2") + .arg(QString::number(channelIndex), count(audioOutputFailed)); + appendLog(message); + } + if (finalSnapshot || failed != c.lastLoggedPreviewFailures || + audioReleaseFailed != c.lastLoggedAudioReleaseFailures || + audioOutputFailed != c.lastLoggedAudioOutputFailures) c.lastDeliveryLogMs = now; - c.lastLoggedVideoIssues = videoIssues; - c.lastLoggedAudioIssues = audioIssues; + c.lastLoggedPreviewFailures = failed; + c.lastLoggedAudioReleaseFailures = audioReleaseFailed; + c.lastLoggedAudioOutputFailures = audioOutputFailed; } void CaptureController::captureReadLoop(int channelIndex) @@ -942,14 +942,7 @@ void CaptureController::captureReadLoop(int channelIndex) .count(); if (st == GVFG_OK) { - const uint64_t previousId = channel.videoLastId.exchange(frame.frame_id); - if (channel.videoReceived.fetch_add(1) != 0) - { - if (frame.frame_id > previousId && frame.frame_id - previousId > 1) - channel.videoIdGaps += frame.frame_id - previousId - 1; - else if (frame.frame_id <= previousId) - ++channel.videoIdResets; - } + channel.videoReceived.fetch_add(1); const bool timingWarmupComplete = ++successfulFrameCount > kTimingWarmupFrames; if (timingWarmupComplete) { @@ -986,7 +979,7 @@ void CaptureController::captureReadLoop(int channelIndex) { captureStalledLogged = false; QMetaObject::invokeMethod(this, [this, channelIndex]() - { appendLog(QStringLiteral("CH%1 RECOVERED capture resumed").arg(channelIndex)); }, Qt::QueuedConnection); + { appendLog(QStringLiteral("CH%1 [SDK] Video frame delivery resumed").arg(channelIndex)); }, Qt::QueuedConnection); } const bool attemptedPreview = channel.previewHandle && @@ -1028,7 +1021,7 @@ void CaptureController::captureReadLoop(int channelIndex) if (failures == 1) { QMetaObject::invokeMethod(this, [this, channelIndex, failures, previewStatus]() - { appendLog(QStringLiteral("CH%1 preview render failed #%2: %3") + { appendLog(QStringLiteral("CH%1 [APP] Preview render failed | consecutive=%2 error=%3") .arg(channelIndex) .arg(static_cast(failures)) .arg(QString::fromUtf8(gvfg_preview_strerror(previewStatus)))); }, Qt::QueuedConnection); @@ -1056,7 +1049,7 @@ void CaptureController::captureReadLoop(int channelIndex) appendLog(QStringLiteral("CH%1 Startup latency | Start -> start_channel return=%2 ms | " "first read_frame call -> return=%3 ms | " "first render_frame call -> return=%4 ms | " - "Start -> first render_frame return=%5 ms | frame_id=%6") + "Start -> first render_frame return=%5 ms | SDK_frame_id=%6") .arg(channelIndex) .arg(startCallMs, 0, 'f', 3) .arg(firstReadMs, 0, 'f', 3) @@ -1071,7 +1064,7 @@ void CaptureController::captureReadLoop(int channelIndex) const uint64_t failures = channel.previewFailureCount; channel.previewFailureCount = 0; QMetaObject::invokeMethod(this, [this, channelIndex, failures]() - { appendLog(QStringLiteral("CH%1 preview render recovered after %2 failure(s)") + { appendLog(QStringLiteral("CH%1 [APP] Preview render recovered | previous_failures=%2") .arg(channelIndex) .arg(static_cast(failures))); }, Qt::QueuedConnection); } @@ -1109,7 +1102,7 @@ void CaptureController::captureReadLoop(int channelIndex) nextNoFrameReport = now + std::chrono::seconds(10); QMetaObject::invokeMethod(this, [this, channelIndex, elapsedMs]() { - appendLog(QStringLiteral("CH%1 ERROR no capture frame for %2 ms") + appendLog(QStringLiteral("CH%1 [SDK] ERROR no video frame returned | duration_ms=%2") .arg(channelIndex) .arg(elapsedMs)); #if GVFG_INTERNAL_DIAGNOSTICS @@ -1176,18 +1169,8 @@ void CaptureController::audioReadLoop(int channelIndex) break; } - const uint64_t audioId = frame.frame_id; { std::lock_guard lock(channel.audioQueueMutex); - if (channel.audioReceivedBytes != 0) - { - if (audioId > channel.audioLastId && audioId - channel.audioLastId > 1) - channel.audioIdGaps += audioId - channel.audioLastId - 1; - else if (audioId <= channel.audioLastId) - ++channel.audioIdResets; - } - channel.audioLastId = audioId; - channel.audioReceivedBytes += frame.data_size; ++channel.audioReceivedFrames; } std::vector pcm(frame.data_size); @@ -1197,7 +1180,7 @@ void CaptureController::audioReadLoop(int channelIndex) if (releaseStatus != GVFG_OK) { std::lock_guard lock(channel.audioQueueMutex); - channel.audioFailedBytes += pcm.size(); + ++channel.audioReleaseFailedFrames; } if (releaseStatus != GVFG_OK && !channel.stopRequested.load(std::memory_order_acquire)) { @@ -1212,17 +1195,14 @@ void CaptureController::audioReadLoop(int channelIndex) std::lock_guard lock(channel.audioQueueMutex); if (channel.stopRequested.load(std::memory_order_acquire)) { - channel.audioCancelledBytes += pcm.size(); + ++channel.audioCancelledFrames; break; } if (channel.audioQueue.size() >= kMaxQueuedAudioFrames) { - const auto &oldest = channel.audioQueue.front(); - channel.audioDroppedBytes += oldest.pcm.size(); - channel.audioQueuedBytes -= oldest.pcm.size(); + ++channel.audioDroppedFrames; channel.audioQueue.pop_front(); } - channel.audioQueuedBytes += pcm.size(); channel.audioQueue.push_back({std::move(pcm)}); } channel.audioQueueReady.notify_one(); @@ -1262,14 +1242,13 @@ void CaptureController::audioPlaybackLoop(int channelIndex) { if (!recovering) QMetaObject::invokeMethod(this, [this, channelIndex]() { - appendLog(QStringLiteral("CH%1 Audio output unavailable; monitoring playback will retry").arg(channelIndex)); + appendLog(QStringLiteral("CH%1 [APP] WARNING audio output unavailable | retrying").arg(channelIndex)); }, Qt::QueuedConnection); recovering = true; std::unique_lock lock(channel.audioQueueMutex); while (!channel.audioQueue.empty()) { - channel.audioDroppedBytes += channel.audioQueue.front().pcm.size(); - channel.audioQueuedBytes -= channel.audioQueue.front().pcm.size(); + ++channel.audioDroppedFrames; channel.audioQueue.pop_front(); } channel.audioQueueReady.wait_for(lock, std::chrono::milliseconds(500), [&channel]() { @@ -1284,14 +1263,13 @@ void CaptureController::audioPlaybackLoop(int channelIndex) { if (!recovering) QMetaObject::invokeMethod(this, [this, channelIndex]() { - appendLog(QStringLiteral("CH%1 Audio output failed to start; monitoring playback will retry").arg(channelIndex)); + appendLog(QStringLiteral("CH%1 [APP] Audio output start failed | retrying").arg(channelIndex)); }, Qt::QueuedConnection); recovering = true; std::unique_lock lock(channel.audioQueueMutex); while (!channel.audioQueue.empty()) { - channel.audioDroppedBytes += channel.audioQueue.front().pcm.size(); - channel.audioQueuedBytes -= channel.audioQueue.front().pcm.size(); + ++channel.audioDroppedFrames; channel.audioQueue.pop_front(); } channel.audioQueueReady.wait_for(lock, std::chrono::milliseconds(500), [&channel]() { @@ -1302,7 +1280,7 @@ void CaptureController::audioPlaybackLoop(int channelIndex) if (recovering) QMetaObject::invokeMethod(this, [this, channelIndex]() { - appendLog(QStringLiteral("CH%1 Audio monitoring playback recovered").arg(channelIndex)); + appendLog(QStringLiteral("CH%1 [APP] Audio output recovered").arg(channelIndex)); }, Qt::QueuedConnection); recovering = false; bool restartPlayback = false; @@ -1319,7 +1297,6 @@ void CaptureController::audioPlaybackLoop(int channelIndex) break; pcm = std::move(channel.audioQueue.front().pcm); channel.audioQueue.pop_front(); - channel.audioQueuedBytes -= pcm.size(); } qint64 written = 0; @@ -1332,13 +1309,6 @@ void CaptureController::audioPlaybackLoop(int channelIndex) static_cast(pcm.size()) - written); const auto now = std::chrono::steady_clock::now(); const bool stalled = result == 0 && now - lastProgress >= std::chrono::seconds(2); - { - std::lock_guard lock(channel.audioQueueMutex); - if (result > 0) - channel.audioAcceptedBytes += static_cast(result); - else if (result < 0 || stalled) - channel.audioFailedBytes += pcm.size() - static_cast(written); - } if (result > 0) { written += result; @@ -1347,10 +1317,14 @@ void CaptureController::audioPlaybackLoop(int channelIndex) } if (result < 0 || stalled) { + { + std::lock_guard lock(channel.audioQueueMutex); + ++channel.audioOutputFailedFrames; + } QMetaObject::invokeMethod(this, [this, channelIndex, stalled]() { appendLog(stalled - ? QStringLiteral("CH%1 Audio output made no progress for 2 seconds; rebuilding monitoring playback").arg(channelIndex) - : QStringLiteral("CH%1 Audio output write failed; rebuilding monitoring playback").arg(channelIndex)); + ? QStringLiteral("CH%1 [APP] Audio output stalled | duration_ms=2000").arg(channelIndex) + : QStringLiteral("CH%1 [APP] Audio output write failed").arg(channelIndex)); }, Qt::QueuedConnection); restartPlayback = true; break; @@ -1360,7 +1334,12 @@ void CaptureController::audioPlaybackLoop(int channelIndex) if (written != static_cast(pcm.size()) && !restartPlayback) { std::lock_guard lock(channel.audioQueueMutex); - channel.audioCancelledBytes += pcm.size() - static_cast(written); + ++channel.audioCancelledFrames; + } + else if (written == static_cast(pcm.size())) + { + std::lock_guard lock(channel.audioQueueMutex); + ++channel.audioOutputFrames; } } audioSink.stop(); @@ -1370,8 +1349,7 @@ void CaptureController::audioPlaybackLoop(int channelIndex) std::unique_lock lock(channel.audioQueueMutex); while (!channel.audioQueue.empty()) { - channel.audioDroppedBytes += channel.audioQueue.front().pcm.size(); - channel.audioQueuedBytes -= channel.audioQueue.front().pcm.size(); + ++channel.audioDroppedFrames; channel.audioQueue.pop_front(); } channel.audioQueueReady.wait_for(lock, std::chrono::milliseconds(500), [&channel]() { @@ -1389,7 +1367,6 @@ void CaptureController::joinAudioThread(int channel) if (channels_[channel].audioPlaybackThread.joinable()) channels_[channel].audioPlaybackThread.join(); std::lock_guard lock(channels_[channel].audioQueueMutex); - channels_[channel].audioCancelledBytes += channels_[channel].audioQueuedBytes; - channels_[channel].audioQueuedBytes = 0; + channels_[channel].audioCancelledFrames += channels_[channel].audioQueue.size(); channels_[channel].audioQueue.clear(); } diff --git a/GVFG_Qt_Sample/capture_controller.h b/GVFG_Qt_Sample/capture_controller.h index 6f68011..a788fc1 100644 --- a/GVFG_Qt_Sample/capture_controller.h +++ b/GVFG_Qt_Sample/capture_controller.h @@ -84,15 +84,16 @@ private: std::deque audioQueue; bool audioEnabled = false; gvfg_audio_format_t audioFormat{}; - uint64_t audioReceivedFrames = 0, audioReceivedBytes = 0, audioAcceptedBytes = 0; - uint64_t audioQueuedBytes = 0, audioDroppedBytes = 0, audioCancelledBytes = 0; - uint64_t audioFailedBytes = 0, audioIdGaps = 0, audioIdResets = 0, audioLastId = 0; + uint64_t audioReceivedFrames = 0, audioOutputFrames = 0; + uint64_t audioDroppedFrames = 0, audioCancelledFrames = 0; + uint64_t audioReleaseFailedFrames = 0, audioOutputFailedFrames = 0; std::atomic videoReceived{0}, videoSubmitted{0}, videoSkipped{0}, videoFailed{0}; - std::atomic videoIdGaps{0}, videoIdResets{0}, videoLastId{0}; gvfg_preview_delivery_stats_t previewBaseline{}; + gvfg_debug_backend_stats_t backendBaseline{}; std::chrono::steady_clock::time_point startupStartTime{}; double startupStartCallMs = 0.0; - uint64_t lastLoggedVideoIssues = 0, lastLoggedAudioIssues = 0; + uint64_t lastLoggedPreviewFailures = 0; + uint64_t lastLoggedAudioReleaseFailures = 0, lastLoggedAudioOutputFailures = 0; qint64 lastDeliveryLogMs = 0; std::atomic getFrameAverageMs{0.0}, getFrameMaximumMs{0.0}, getFrameWindowMaximumMs{0.0}; std::atomic getFrameSamples{0}; diff --git a/GVFG_Qt_Sample/mainwindow.cpp b/GVFG_Qt_Sample/mainwindow.cpp index 803aa27..9fa5a45 100644 --- a/GVFG_Qt_Sample/mainwindow.cpp +++ b/GVFG_Qt_Sample/mainwindow.cpp @@ -25,6 +25,7 @@ protected: void highlightBlock(const QString &text) override { if (text.contains(QStringLiteral("SIGNAL_DISCONNECTED")) || + text.contains(QStringLiteral("No signal"), Qt::CaseInsensitive) || text.contains(QStringLiteral("failed"), Qt::CaseInsensitive) || text.contains(QStringLiteral("error"), Qt::CaseInsensitive)) setFormat(0, text.size(), error_); @@ -167,8 +168,8 @@ void MainWindow::showPreviewWindow(int channel, bool fullscreen) if (!controller_->applyPreview(channel)) controller_->logUiMessage( fullscreen - ? QStringLiteral("CH%1 Fullscreen Preview failed: unable to update preview window").arg(channel) - : QStringLiteral("CH%1 Show Preview failed: unable to update preview window").arg(channel)); + ? QStringLiteral("CH%1 [APP] Fullscreen preview failed | window update").arg(channel) + : QStringLiteral("CH%1 [APP] Show preview failed | window update").arg(channel)); } void MainWindow::updateOutputFormatOptions(int changedChannel) diff --git a/GVFG_SDK/sdk/gvfg/include/gvfg_capture.h b/GVFG_SDK/sdk/gvfg/include/gvfg_capture.h index ad0f5c0..7377eaa 100644 --- a/GVFG_SDK/sdk/gvfg/include/gvfg_capture.h +++ b/GVFG_SDK/sdk/gvfg/include/gvfg_capture.h @@ -205,7 +205,7 @@ extern "C" int row_stride_bytes; /* Byte distance between the starts of adjacent rows. */ int pixel_format; /* gvfg_pixel_format_t value. */ int bit_depth; /* Bits per color channel of the native frame. */ - uint64_t frame_id; /* GigabyteLib GVFG_VIDEO_INFO.FrameCount. */ + uint64_t frame_id; /* SDK delivery sequence; starts at 1 for each channel start. */ uint64_t timestamp_ns; /* Monotonic SDK delivery time; same clock domain as audio. */ } gvfg_frame_t; @@ -217,7 +217,7 @@ extern "C" uint32_t channels; /* Interleaved PCM channel count. */ uint32_t bits_per_sample; /* Bits in each native PCM sample. */ uint32_t reserved; - uint64_t frame_id; /* GigabyteLib GVFG_AUDIO_INFO.FrameCount. */ + uint64_t frame_id; /* SDK delivery sequence; starts at 1 for each channel start. */ uint64_t timestamp_ns; /* Monotonic SDK delivery time; same clock domain as video. */ } gvfg_audio_frame_t; @@ -625,9 +625,8 @@ extern "C" /* * Copy the most recent detailed fault or rejected API call for one - * channel. Event-poll timeouts are not stored; frame-read timeouts currently - * retain the backend timeout detail. A successful gvfg_start_channel() - * begins a new diagnostic lifetime. + * channel. Normal frame-read and event-poll timeouts are not stored. + * A successful gvfg_start_channel() begins a new diagnostic lifetime. */ GVFG_API gvfg_status_t gvfg_get_channel_last_error_detail( GVFG_PARAM_IN gvfg_handle handle, diff --git a/GVFG_SDK/sdk/gvfg/include/gvfg_debug.h b/GVFG_SDK/sdk/gvfg/include/gvfg_debug.h index aee28c6..2c552a0 100644 --- a/GVFG_SDK/sdk/gvfg/include/gvfg_debug.h +++ b/GVFG_SDK/sdk/gvfg/include/gvfg_debug.h @@ -20,12 +20,11 @@ typedef struct int running; int frame_held; uint32_t event_queue_depth; - uint64_t frame_wait_timeouts; uint64_t video_event_wakes; + uint64_t video_frames_from_lib; uint64_t audio_dma_event_wakes; uint64_t extra_audio_event_wakes; - uint64_t audio_frames_from_driver; - uint64_t audio_bytes_from_driver; + uint64_t audio_frames_from_lib; uint64_t get_frame_timing_samples; double get_frame_timing_average_us; double get_frame_timing_max300_us; diff --git a/GVFG_SDK/sdk/gvfg/src/gigabyte/gigabyte_capture_session.cpp b/GVFG_SDK/sdk/gvfg/src/gigabyte/gigabyte_capture_session.cpp index d79875b..38b0ab6 100644 --- a/GVFG_SDK/sdk/gvfg/src/gigabyte/gigabyte_capture_session.cpp +++ b/GVFG_SDK/sdk/gvfg/src/gigabyte/gigabyte_capture_session.cpp @@ -196,8 +196,9 @@ gvfg_status_t GigabyteCaptureSession::set_video_format(gvfg_pixel_format_t forma const ULONG colorDepth = format == GVFG_PIXFMT_Y210 ? GVFG_VIDEO_COLOR_DEPTH_10_BITS : GVFG_VIDEO_COLOR_DEPTH_8_BITS; - return from_vendor(GvfgSetVideoColorDepth(context_, channel_, colorDepth), - "GvfgSetVideoColorDepth"); + const gvfg_status_t status = from_vendor( + GvfgSetVideoColorDepth(context_, channel_, colorDepth), "GvfgSetVideoColorDepth"); + return status == GVFG_OK ? refresh_video_info() : status; } gvfg_status_t GigabyteCaptureSession::refresh_video_info() const @@ -230,13 +231,9 @@ gvfg_status_t GigabyteCaptureSession::get_signal_status(gvfg_signal_status_t &ou const gvfg_status_t openStatus = ensure_vendor_channel_open(); if (openStatus != GVFG_OK) return openStatus; - const gvfg_status_t status = refresh_video_info(); - if (status == GVFG_OK) - { - std::lock_guard lock(state_mutex_); - out = cached_signal_; - } - return status; + std::lock_guard lock(state_mutex_); + out = cached_signal_; + return GVFG_OK; } gvfg_status_t GigabyteCaptureSession::get_device_capabilities( @@ -296,17 +293,27 @@ gvfg_status_t GigabyteCaptureSession::get_audio_format(GigabyteAudioInfo &out) c const gvfg_status_t openStatus = ensure_vendor_channel_open(); if (openStatus != GVFG_OK) return openStatus; - const gvfg_status_t status = from_vendor( - GvfgGetAudioInfo(context_, channel_, &audio_info_), "GvfgGetAudioInfo"); + const gvfg_status_t status = refresh_audio_info(); if (status != GVFG_OK) return status; + std::lock_guard lock(state_mutex_); out = {}; out.sample_rate = audio_info_.SamplesPerSec; out.channels = audio_info_.Channels; out.bits_per_sample = audio_info_.BitsPerSample; - out.frames_per_second = audio_info_.FramesPerSec; out.frame_bytes = audio_info_.cbBufSize; - out.frame_count = audio_info_.FrameCount; + return GVFG_OK; +} + +gvfg_status_t GigabyteCaptureSession::refresh_audio_info() const +{ + GVFG_AUDIO_INFO info{}; + const gvfg_status_t status = from_vendor( + GvfgGetAudioInfo(context_, channel_, &info), "GvfgGetAudioInfo"); + if (status != GVFG_OK) + return status; + std::lock_guard lock(state_mutex_); + audio_info_ = info; return GVFG_OK; } @@ -326,6 +333,9 @@ gvfg_status_t GigabyteCaptureSession::configure_stream() const gvfg_status_t openStatus = ensure_vendor_channel_open(); if (openStatus != GVFG_OK) return openStatus; + const gvfg_status_t infoStatus = refresh_video_info(); + if (infoStatus != GVFG_OK) + return infoStatus; configured_ = true; return GVFG_OK; } @@ -393,11 +403,7 @@ gvfg_status_t GigabyteCaptureSession::wait_frame(uint32_t timeoutMs, gvfg_frame_ const DWORD waitMs = timeoutMs == UINT32_MAX ? INFINITE : timeoutMs; const DWORD wait = WaitForMultipleObjects(count, handles.data(), FALSE, waitMs); if (wait == WAIT_TIMEOUT) - { - std::lock_guard lock(state_mutex_); - ++wait_timeout_count_; return GVFG_ETIMEOUT; - } if (wait == WAIT_OBJECT_0) return GVFG_ESTATE; if (wait != WAIT_OBJECT_0 + 1 && wait != WAIT_OBJECT_0 + 2) @@ -407,10 +413,6 @@ gvfg_status_t GigabyteCaptureSession::wait_frame(uint32_t timeoutMs, gvfg_frame_ ++video_event_wakes_; } - const auto begin = std::chrono::steady_clock::now(); - const gvfg_status_t infoStatus = refresh_video_info(); - if (infoStatus != GVFG_OK) - return infoStatus; gvfg_signal_status_t signal{}; { std::lock_guard lock(state_mutex_); @@ -420,32 +422,34 @@ gvfg_status_t GigabyteCaptureSession::wait_frame(uint32_t timeoutMs, gvfg_frame_ return GVFG_ETIMEOUT; size_t bytes = 0; - uint64_t frameCount = 0; { std::lock_guard lock(state_mutex_); bytes = video_info_.cbBufSize; - frameCount = video_info_.FrameCount; } const void *data = nullptr; gvfg_status_t status = GVFG_OK; if (zero_copy_enabled_) { void *buffer = nullptr; + const auto begin = std::chrono::steady_clock::now(); status = from_vendor(GvfgGetVideoFrameZeroCopy(context_, channel_, &buffer), "GvfgGetVideoFrameZeroCopy"); + const auto end = std::chrono::steady_clock::now(); + record_get_frame_timing(std::chrono::duration(end - begin).count()); data = buffer; } else { if (copy_buffer_.size() != bytes) copy_buffer_.assign(bytes, 0); + const auto begin = std::chrono::steady_clock::now(); status = from_vendor(GvfgGetVideoFrame(context_, channel_, copy_buffer_.data(), static_cast(copy_buffer_.size())), "GvfgGetVideoFrame"); + const auto end = std::chrono::steady_clock::now(); + record_get_frame_timing(std::chrono::duration(end - begin).count()); data = copy_buffer_.data(); } - const auto end = std::chrono::steady_clock::now(); - record_get_frame_timing(std::chrono::duration(end - begin).count()); if (status != GVFG_OK) return status; if (!data || bytes == 0) @@ -454,13 +458,14 @@ gvfg_status_t GigabyteCaptureSession::wait_frame(uint32_t timeoutMs, gvfg_frame_ std::lock_guard lock(state_mutex_); out.data = data; out.data_size = bytes; - out.frame_id = frameCount; + out.frame_id = 0; out.timestamp_ns = monotonic_ns(); out.width = signal.width; out.height = signal.height; out.row_stride_bytes = static_cast(bytes / signal.height); out.pixel_format = signal.pixel_format; out.bit_depth = signal.bit_depth; + ++video_frames_from_lib_; frame_held_ = true; return GVFG_OK; } @@ -482,18 +487,17 @@ gvfg_status_t GigabyteCaptureSession::release_frame() } gvfg_status_t GigabyteCaptureSession::wait_audio( - uint32_t timeoutMs, void *destination, uint32_t destinationCapacity, uint32_t &outBytes, - uint64_t &outFrameCount) + uint32_t timeoutMs, void *destination, uint32_t destinationCapacity, uint32_t &outBytes) { outBytes = 0; - outFrameCount = 0; if (!running_ || !audio_enabled_ || !destination) return reject(GVFG_ESTATE, "GigabyteLib audio read rejected"); - GigabyteAudioInfo format{}; - const gvfg_status_t formatStatus = get_audio_format(format); - if (formatStatus != GVFG_OK) - return formatStatus; - if (format.frame_bytes == 0 || format.frame_bytes > destinationCapacity) + uint32_t frameBytes = 0; + { + std::lock_guard lock(state_mutex_); + frameBytes = audio_info_.cbBufSize; + } + if (frameBytes == 0 || frameBytes > destinationCapacity) return reject(GVFG_EINVAL, "GigabyteLib audio destination is too small"); if (!events_.hAudioFrameInEvent) return reject(GVFG_EIO, "GigabyteLib did not provide an audio-frame event"); @@ -503,11 +507,7 @@ gvfg_status_t GigabyteCaptureSession::wait_audio( const DWORD waitMs = timeoutMs == UINT32_MAX ? INFINITE : timeoutMs; const DWORD wait = WaitForMultipleObjects(count, handles.data(), FALSE, waitMs); if (wait == WAIT_TIMEOUT) - { - std::lock_guard lock(state_mutex_); - ++wait_timeout_count_; return GVFG_ETIMEOUT; - } if (wait == WAIT_OBJECT_0) return GVFG_ESTATE; if (wait != WAIT_OBJECT_0 + 1 && wait != WAIT_OBJECT_0 + 2) @@ -519,21 +519,14 @@ gvfg_status_t GigabyteCaptureSession::wait_audio( else ++extra_audio_event_wakes_; } - const uint32_t frameBytes = format.frame_bytes; const gvfg_status_t status = from_vendor( GvfgGetAudioFrame(context_, channel_, destination, frameBytes), "GvfgGetAudioFrame"); if (status != GVFG_OK) return status; - GigabyteAudioInfo latestInfo{}; - const gvfg_status_t infoStatus = get_audio_format(latestInfo); - if (infoStatus != GVFG_OK) - return infoStatus; outBytes = frameBytes; - outFrameCount = latestInfo.frame_count; { std::lock_guard lock(state_mutex_); - ++audio_frames_from_driver_; - audio_bytes_from_driver_ += outBytes; + ++audio_frames_from_lib_; } return GVFG_OK; } @@ -615,12 +608,11 @@ void GigabyteCaptureSession::emit_event(gvfg_event_type_t type) const void GigabyteCaptureSession::fill_debug_stats(gvfg_debug_backend_stats_t &out) const { std::lock_guard lock(state_mutex_); - out.frame_wait_timeouts = wait_timeout_count_; out.video_event_wakes = video_event_wakes_; + out.video_frames_from_lib = video_frames_from_lib_; out.audio_dma_event_wakes = audio_event_wakes_; out.extra_audio_event_wakes = extra_audio_event_wakes_; - out.audio_frames_from_driver = audio_frames_from_driver_; - out.audio_bytes_from_driver = audio_bytes_from_driver_; + out.audio_frames_from_lib = audio_frames_from_lib_; out.get_frame_timing_samples = get_frame_timing_samples_; out.get_frame_timing_average_us = get_frame_timing_samples_ ? get_frame_timing_total_us_ / static_cast(get_frame_timing_samples_) : 0.0; diff --git a/GVFG_SDK/sdk/gvfg/src/gigabyte/gigabyte_capture_session.h b/GVFG_SDK/sdk/gvfg/src/gigabyte/gigabyte_capture_session.h index 1454a7c..c8f2998 100644 --- a/GVFG_SDK/sdk/gvfg/src/gigabyte/gigabyte_capture_session.h +++ b/GVFG_SDK/sdk/gvfg/src/gigabyte/gigabyte_capture_session.h @@ -23,9 +23,7 @@ namespace gvfg::internal uint32_t sample_rate = 0; uint32_t channels = 0; uint32_t bits_per_sample = 0; - uint32_t frames_per_second = 0; uint32_t frame_bytes = 0; - uint64_t frame_count = 0; }; using GigabyteEventCallback = void (*)(gvfg_event_type_t event, void *user); @@ -57,8 +55,7 @@ namespace gvfg::internal gvfg_status_t stop_stream(); gvfg_status_t wait_frame(uint32_t timeoutMs, gvfg_frame_t &out); gvfg_status_t wait_audio(uint32_t timeoutMs, void *destination, - uint32_t destinationCapacity, uint32_t &outBytes, - uint64_t &outFrameCount); + uint32_t destinationCapacity, uint32_t &outBytes); gvfg_status_t release_frame(); void fill_debug_stats(gvfg_debug_backend_stats_t &out) const; gvfg_status_t debug_read_register(uint32_t offset, uint32_t &outValue) const; @@ -73,6 +70,7 @@ namespace gvfg::internal void event_thread_proc(); void emit_event(gvfg_event_type_t type) const; gvfg_status_t refresh_video_info() const; + gvfg_status_t refresh_audio_info() const; gvfg_status_t from_vendor(GVFG_HRESULT result, const char *operation) const; gvfg_status_t reject(gvfg_status_t status, const char *message) const; void record_get_frame_timing(double elapsedUs); @@ -101,12 +99,11 @@ namespace gvfg::internal mutable std::mutex state_mutex_; std::vector copy_buffer_; bool frame_held_ = false; - uint64_t audio_frames_from_driver_ = 0; - uint64_t audio_bytes_from_driver_ = 0; + uint64_t video_frames_from_lib_ = 0; + uint64_t audio_frames_from_lib_ = 0; uint64_t video_event_wakes_ = 0; uint64_t audio_event_wakes_ = 0; uint64_t extra_audio_event_wakes_ = 0; - uint64_t wait_timeout_count_ = 0; uint64_t get_frame_timing_samples_ = 0; uint32_t get_frame_timing_window_samples_ = 0; double get_frame_timing_total_us_ = 0.0; diff --git a/GVFG_SDK/sdk/gvfg/src/gvfg_capture.cpp b/GVFG_SDK/sdk/gvfg/src/gvfg_capture.cpp index b249be5..cb614d3 100644 --- a/GVFG_SDK/sdk/gvfg/src/gvfg_capture.cpp +++ b/GVFG_SDK/sdk/gvfg/src/gvfg_capture.cpp @@ -189,6 +189,12 @@ struct gvfg_channel_session_t const gvfg_status_t cfg = configureStream(); if (cfg != GVFG_OK) return cfg; + if (audioEnabled) + { + const gvfg_status_t audioStatus = session->get_audio_format(cachedAudioFormat); + if (audioStatus != GVFG_OK) + return audioStatus; + } resetRuntimeCounters(); { @@ -326,14 +332,10 @@ struct gvfg_channel_session_t if (!session) return reject(GVFG_ESTATE, "configure stream rejected: channel is not open"); - const gvfg_status_t signalStatus = querySignal(); - if (signalStatus != GVFG_OK) - return signalStatus; - const gvfg_status_t st = session->configure_stream(); if (st != GVFG_OK) return st; - return GVFG_OK; + return querySignal(); } gvfg_status_t readFrame(gvfg_frame_t &out, uint32_t timeoutMs) @@ -386,6 +388,7 @@ struct gvfg_channel_session_t } else { + frame.frame_id = ++videoFrameId; heldSessionFrame = frame; readInProgress = false; frameHeld = true; @@ -594,6 +597,7 @@ struct gvfg_channel_session_t const gvfg_status_t status = session->get_audio_format(format); if (status != GVFG_OK) return status; + cachedAudioFormat = format; out.sample_rate = format.sample_rate; out.channels = format.channels; out.bits_per_sample = format.bits_per_sample; @@ -617,16 +621,14 @@ struct gvfg_channel_session_t audioReadInProgress = true; } - GigabyteAudioInfo format{}; - gvfg_status_t status = session->get_audio_format(format); + const GigabyteAudioInfo format = cachedAudioFormat; + gvfg_status_t status = format.frame_bytes != 0 ? GVFG_OK : GVFG_ESTATE; uint32_t bytes = 0; - uint64_t frameCount = 0; if (status == GVFG_OK) { audioBuffer.resize(format.frame_bytes); status = session->wait_audio(timeoutMs, audioBuffer.data(), - static_cast(audioBuffer.size()), bytes, - frameCount); + static_cast(audioBuffer.size()), bytes); } { @@ -642,7 +644,7 @@ struct gvfg_channel_session_t out.sample_rate = format.sample_rate; out.channels = format.channels; out.bits_per_sample = format.bits_per_sample; - out.frame_id = frameCount; + out.frame_id = ++audioFrameId; out.timestamp_ns = now_ns(); heldAudioFrame = out; } @@ -711,6 +713,8 @@ struct gvfg_channel_session_t deliveredBitDepth.store(0, std::memory_order_relaxed); deliveredPixelFormat.store(GVFG_PIXFMT_UNKNOWN, std::memory_order_relaxed); runtimeFps.store(0.0, std::memory_order_relaxed); + videoFrameId = 0; + audioFrameId = 0; } void noteDeliveredFrame(int frameWidth, int frameHeight, int bitDepth, int pixelFormat) @@ -728,6 +732,7 @@ struct gvfg_channel_session_t uint32_t selectedChannel = GVFG_CHANNEL_0; bool zeroCopyRequested = false; bool audioEnabled = false; + GigabyteAudioInfo cachedAudioFormat{}; uint32_t width = 0; uint32_t height = 0; @@ -748,11 +753,13 @@ struct gvfg_channel_session_t std::mutex frameMutex; bool readInProgress = false; bool frameHeld = false; + uint64_t videoFrameId = 0; gvfg_frame_t heldSessionFrame{}; std::mutex audioMutex; std::vector audioBuffer; bool audioReadInProgress = false; bool audioFrameHeld = false; + uint64_t audioFrameId = 0; gvfg_audio_frame_t heldAudioFrame{}; std::mutex eventMutex; std::condition_variable eventCv; diff --git a/docs/GVFG_CUSTOMER_API.md b/docs/GVFG_CUSTOMER_API.md index 87b4582..ce03bbb 100644 --- a/docs/GVFG_CUSTOMER_API.md +++ b/docs/GVFG_CUSTOMER_API.md @@ -166,7 +166,7 @@ Audio 採用相同的 pull 與 ownership 模型: - `width`、`height`:影像尺寸。 - `row_stride_bytes`:相鄰兩列起點的 byte 距離。 - `pixel_format`、`bit_depth`:原生 payload 格式。 -- `frame_id`:GigabyteLib `GVFG_VIDEO_INFO.FrameCount`。 +- `frame_id`:SDK 成功交付序號;每次 channel Start 從 1 重新開始。 - `timestamp_ns`:SDK monotonic delivery timestamp,與 audio 使用相同 clock domain。 ### PCM audio @@ -179,7 +179,7 @@ Audio 採用相同的 pull 與 ownership 模型: - `data`、`data_size`:SDK-owned PCM 與本次有效 byte 數。 - `sample_rate`、`channels`、`bits_per_sample`:此 frame 的 PCM 格式。 -- `frame_id`:GigabyteLib `GVFG_AUDIO_INFO.FrameCount`。 +- `frame_id`:SDK 成功交付序號;每次 channel Start 從 1 重新開始。 - `timestamp_ns`:SDK monotonic delivery timestamp,與 video 使用相同 clock domain。 `timestamp_ns` 用於比較同一 process/session 內的 video/audio delivery timing;它 diff --git a/docs/GVFG_CUSTOMER_API_REFERENCE.md b/docs/GVFG_CUSTOMER_API_REFERENCE.md index b592f68..78c0bde 100644 --- a/docs/GVFG_CUSTOMER_API_REFERENCE.md +++ b/docs/GVFG_CUSTOMER_API_REFERENCE.md @@ -126,7 +126,7 @@ timing 屬於內部診斷資訊,不放入客戶 runtime 結構。 | `row_stride_bytes` | `int` | 相鄰兩列起點間距,單位 byte;處理每列時必須使用此值 | | `pixel_format` | `int` | `gvfg_pixel_format_t` 值 | | `bit_depth` | `int` | 原生 frame 每色彩 channel 的 bit depth | -| `frame_id` | `uint64_t` | GigabyteLib `GVFG_VIDEO_INFO.FrameCount` | +| `frame_id` | `uint64_t` | SDK 成功交付序號;每次 channel Start 從 1 重新開始 | | `timestamp_ns` | `uint64_t` | SDK 交付時間;與 audio 共用 monotonic clock,單位 ns | Caller 不得修改任何欄位再 release。SDK 會將完整 token 與目前 held frame 比對。 @@ -157,7 +157,7 @@ Driver 一次傳回多少 bytes、buffer capacity 與 block alignment 均由 SDK | `sample_rate` | `uint32_t` | 每秒 sample 數 | | `channels` | `uint32_t` | interleaved PCM channel 數 | | `bits_per_sample` | `uint32_t` | 每個 PCM sample 的 bit 數 | -| `frame_id` | `uint64_t` | GigabyteLib `GVFG_AUDIO_INFO.FrameCount` | +| `frame_id` | `uint64_t` | SDK 成功交付序號;每次 channel Start 從 1 重新開始 | | `timestamp_ns` | `uint64_t` | SDK 交付時間;與 video 共用 monotonic clock,單位 ns | 與 video 相同,每個 channel 同時只能持有一個 audio frame,且 caller 不得修改