Release SDK 0.2.1 with unplug-safe zero-copy handling

Compared with SDK 0.2.0 (3cd7fbbb):

- Handle the zero-copy release race where the driver clears frame ownership before the SDK receives the unplug event. Wait up to 100 ms only after ERROR_BAD_COMMAND, accept the release as revoked only after a confirmed disconnect, and preserve all other release failures.

- Apply the same confirmed-unplug handling to normal frame release and pending-frame teardown without adding latency to successful per-frame releases.

- Add Debug-only audio event, frame, and byte counters across the Driver-to-SDK, SDK-to-APP, and APP-to-output boundaries, and show the concise totals in the internal diagnostic sample.

- Rename Release and Debug customer packages to the requested GVFG_Sample labels, use minute-resolution timestamps, and derive the package suffix from the root CMake project version.

- Bump the standalone SDK version to 0.2.1 and synchronize the public API examples and internal build guide.

Validation: git diff --check and focused source/diff inspection passed. No build, package generation, runtime capture, unplug stress test, or hardware validation was run.
This commit is contained in:
Mark.lin
2026-09-10 11:36:17 +08:00
parent 3cd7fbbb3f
commit cdd2f8a75b
14 changed files with 111 additions and 19 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.16)
project(gvfg_standalone VERSION 0.2.0 LANGUAGES CXX)
project(gvfg_standalone VERSION 0.2.1 LANGUAGES CXX)
include(GNUInstallDirs)
+1 -1
View File
@@ -144,7 +144,7 @@ Audio 採用相同的 pull 與 ownership 模型:
可用 `gvfg_strerror()` 取得靜態英文說明字串;呼叫端不可釋放該字串。
可用 `gvfg_get_version()` 查詢目前實際載入的 `gvfg.dll` 版本。回傳值為
靜態字串,例如 `"0.2.0"`,呼叫端不可釋放。
靜態字串,例如 `"0.2.1"`,呼叫端不可釋放。
需要記錄最近一次失敗的詳細原因時,可在 API 失敗後立即呼叫
`gvfg_get_channel_last_error_detail()`;driver/register 等內部診斷仍保留在 debug API。
+1 -1
View File
@@ -496,7 +496,7 @@ gvfg_status_t gvfg_get_channel_runtime_info(
const char *gvfg_get_version(void);
```
- 回傳目前實際載入的 GVFG runtime DLL 版本,例如 `"0.2.0"`。
- 回傳目前實際載入的 GVFG runtime DLL 版本,例如 `"0.2.1"`。
- 回傳值是靜態 null-terminated 字串,caller 不可 free。
- 可用於 log、問題回報,以及確認 header、LIB、DLL 是否來自同一版本。
+3 -3
View File
@@ -41,13 +41,13 @@
版本只有一個來源:根目錄 `CMakeLists.txt` 的 `project()`:
```cmake
project(gvfg_standalone VERSION 0.2.0 LANGUAGES CXX)
project(gvfg_standalone VERSION 0.2.1 LANGUAGES CXX)
```
例如準備 `0.2.0` 時改成:
例如準備 `0.2.1` 時改成:
```cmake
project(gvfg_standalone VERSION 0.2.0 LANGUAGES CXX)
project(gvfg_standalone VERSION 0.2.1 LANGUAGES CXX)
```
`sdk/gvfg` 會直接繼承根 project version,並將它編入 `gvfg.dll`。不再存在 `GVFG_SDK_VERSION` cache option,也不應使用 `-DGVFG_SDK_VERSION=...` 覆寫。修改版本後要重新 configure,再重新建置 DLL。
+8 -2
View File
@@ -14,9 +14,15 @@ if "%~1"=="" (
set "OUTPUT_DIR=%~1"
)
for /f %%I in ('powershell -NoProfile -ExecutionPolicy Bypass -Command "Get-Date -Format yyyyMMdd_HHmmss"') do set "STAMP=%%I"
for /f %%I in ('powershell -NoProfile -ExecutionPolicy Bypass -Command "Get-Date -Format yyyyMMdd_HHmm"') do set "STAMP=%%I"
for /f "tokens=3" %%I in ('findstr /C:"project(gvfg_standalone VERSION" "%PROJECT_DIR%\CMakeLists.txt"') do set "PACKAGE_VERSION=%%I"
set "PACKAGE_NAME=GVFG_Qt_Preview_Debug_%STAMP%"
if not defined PACKAGE_VERSION (
echo [package] ERROR: Project version not found in CMakeLists.txt.
goto fail
)
set "PACKAGE_NAME=GVFG_Sample_Debug_%STAMP%_%PACKAGE_VERSION%"
set "ZIP_PATH=%OUTPUT_DIR%\%PACKAGE_NAME%.zip"
set "STAGE_ROOT=%TEMP%\%PACKAGE_NAME%_%RANDOM%"
set "STAGE_DIR=%STAGE_ROOT%\%PACKAGE_NAME%"
+8 -2
View File
@@ -14,9 +14,15 @@ if "%~1"=="" (
set "OUTPUT_DIR=%~1"
)
for /f %%I in ('powershell -NoProfile -ExecutionPolicy Bypass -Command "Get-Date -Format yyyyMMdd_HHmmss"') do set "STAMP=%%I"
for /f %%I in ('powershell -NoProfile -ExecutionPolicy Bypass -Command "Get-Date -Format yyyyMMdd_HHmm"') do set "STAMP=%%I"
for /f "tokens=3" %%I in ('findstr /C:"project(gvfg_standalone VERSION" "%PROJECT_DIR%\CMakeLists.txt"') do set "PACKAGE_VERSION=%%I"
set "PACKAGE_NAME=GVFG_Qt_Preview_Release_%STAMP%"
if not defined PACKAGE_VERSION (
echo [package] ERROR: Project version not found in CMakeLists.txt.
goto fail
)
set "PACKAGE_NAME=GVFG_Sample_Release_%STAMP%_%PACKAGE_VERSION%"
set "ZIP_PATH=%OUTPUT_DIR%\%PACKAGE_NAME%.zip"
set "STAGE_ROOT=%TEMP%\%PACKAGE_NAME%_%RANDOM%"
set "STAGE_DIR=%STAGE_ROOT%\%PACKAGE_NAME%"
+28
View File
@@ -612,6 +612,7 @@ void MainWindow::startCapture(int channelIndex)
channel.audioMaxReadGapMs = 0;
}
#if GVFG_INTERNAL_DIAGNOSTICS
channel.audioReceivedFrames = 0;
channel.haveDebugBaseline = false;
channel.lastDebugDmaErrors = 0;
#endif
@@ -828,6 +829,30 @@ void MainWindow::updateSignalStatus(bool queryHardware)
.arg(channel.audioFormat.sample_rate)
.arg(channel.audioFormat.channels)
.arg(channel.audioFormat.bits_per_sample);
#if GVFG_INTERNAL_DIAGNOSTICS
uint64_t sdkFrames = 0;
uint64_t sdkBytes = 0;
uint64_t appOutputBytes = 0;
{
std::lock_guard<std::mutex> lock(channel.audioQueueMutex);
sdkFrames = channel.audioReceivedFrames;
sdkBytes = channel.audioReceivedBytes;
appOutputBytes = channel.audioAcceptedBytes;
}
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")
.arg(channelIndex)
.arg(static_cast<qulonglong>(audioStats.audio_dma_event_wakes))
.arg(static_cast<qulonglong>(audioStats.extra_audio_event_wakes))
.arg(static_cast<qulonglong>(audioStats.audio_frames_from_driver))
.arg(static_cast<qulonglong>(audioStats.audio_bytes_from_driver))
.arg(static_cast<qulonglong>(sdkFrames))
.arg(static_cast<qulonglong>(sdkBytes))
.arg(static_cast<qulonglong>(appOutputBytes));
}
#endif
}
statusLines << QStringLiteral("CH%1 Preview | %2 FPS | %3")
.arg(channelIndex)
@@ -1430,6 +1455,9 @@ void MainWindow::audioReadLoop(int channelIndex)
}
channel.audioLastId = audioId;
channel.audioReceivedBytes += frame.data_size;
#if GVFG_INTERNAL_DIAGNOSTICS
++channel.audioReceivedFrames;
#endif
}
lastArrival = arrival;
std::vector<uint8_t> pcm(frame.data_size);
+1
View File
@@ -99,6 +99,7 @@ private:
bool haveCachedSignalStatus = false;
QString lastLoggedInputStatus;
#if GVFG_INTERNAL_DIAGNOSTICS
uint64_t audioReceivedFrames = 0;
uint64_t lastDebugDmaErrors = 0;
bool haveDebugBaseline = false;
#endif
+1 -1
View File
@@ -576,7 +576,7 @@ extern "C"
_In_ int channel_index,
_Out_ gvfg_runtime_info_t *out_info);
/* Return the loaded GVFG runtime DLL version, for example "0.2.0". */
/* Return the loaded GVFG runtime DLL version, for example "0.2.1". */
GVFG_API const char *gvfg_get_version(void);
/* Convert a gvfg_pixel_format_t value to a static English format name. */
+4
View File
@@ -45,6 +45,10 @@ typedef struct
int backend_capture_active;
uint64_t backend_latest_sequence;
uint64_t backend_delivered_sequence;
uint64_t audio_dma_event_wakes;
uint64_t extra_audio_event_wakes;
uint64_t audio_frames_from_driver;
uint64_t audio_bytes_from_driver;
int get_frame_zero_copy;
uint64_t get_frame_timing_samples;
@@ -84,6 +84,10 @@ typedef struct pcies2mm_debug_state_t
int capture_active;
uint64_t latest_sequence;
uint64_t delivered_sequence;
uint64_t audio_dma_event_wakes;
uint64_t extra_audio_event_wakes;
uint64_t audio_frames_from_driver;
uint64_t audio_bytes_from_driver;
int get_frame_zero_copy;
uint64_t get_frame_timing_samples;
double get_frame_timing_average_us;
@@ -16,6 +16,7 @@ namespace
constexpr uint32_t kMaxChannels = 2;
constexpr uint32_t kDefaultWidth = 1920;
constexpr uint32_t kDefaultHeight = 1080;
constexpr auto kZeroCopyUnplugGracePeriod = std::chrono::milliseconds(100);
static bool is_write_only_register(uint32_t offset)
{
@@ -510,6 +511,10 @@ namespace gvfg::internal
latest_sequence_ = 0;
delivered_sequence_ = 0;
wait_timeout_count_ = 0;
audio_dma_event_wakes_.store(0, std::memory_order_relaxed);
extra_audio_event_wakes_.store(0, std::memory_order_relaxed);
audio_frames_from_driver_.store(0, std::memory_order_relaxed);
audio_bytes_from_driver_.store(0, std::memory_order_relaxed);
get_frame_timing_samples_ = 0;
get_frame_timing_window_samples_ = 0;
get_frame_timing_total_us_ = 0.0;
@@ -854,6 +859,10 @@ namespace gvfg::internal
finishRead();
return fail(PCIES2MM_EIO, "wait audio event", err);
}
if (waitResult == WAIT_OBJECT_0)
audio_dma_event_wakes_.fetch_add(1, std::memory_order_relaxed);
else
extra_audio_event_wakes_.fetch_add(1, std::memory_order_relaxed);
if (!running_.load(std::memory_order_acquire))
{
finishRead();
@@ -880,6 +889,8 @@ namespace gvfg::internal
PCIES2MM_LOG("audio frame size mismatch: expected=%u returned=%d",
format.frame_bytes, ret);
outBytes = static_cast<uint32_t>(ret);
audio_frames_from_driver_.fetch_add(1, std::memory_order_relaxed);
audio_bytes_from_driver_.fetch_add(outBytes, std::memory_order_relaxed);
finishRead();
return PCIES2MM_OK;
}
@@ -916,10 +927,7 @@ namespace gvfg::internal
// it after the unplug interrupt. In that exact state, ownership has
// already been revoked by the driver; clear the matching SDK token
// so unplug handling and subsequent capture can make progress.
const bool revokedByUnplug =
err == ERROR_BAD_COMMAND &&
signal_presence_known_.load(std::memory_order_acquire) &&
!signal_present_.load(std::memory_order_acquire);
const bool revokedByUnplug = zero_copy_release_revoked_by_unplug(err, lock);
if (!revokedByUnplug)
{
lock.unlock();
@@ -954,6 +962,14 @@ namespace gvfg::internal
outDebugState.capture_active = capture_active_.load(std::memory_order_relaxed) ? 1 : 0;
outDebugState.latest_sequence = latest_sequence_;
outDebugState.delivered_sequence = delivered_sequence_;
outDebugState.audio_dma_event_wakes =
audio_dma_event_wakes_.load(std::memory_order_relaxed);
outDebugState.extra_audio_event_wakes =
extra_audio_event_wakes_.load(std::memory_order_relaxed);
outDebugState.audio_frames_from_driver =
audio_frames_from_driver_.load(std::memory_order_relaxed);
outDebugState.audio_bytes_from_driver =
audio_bytes_from_driver_.load(std::memory_order_relaxed);
outDebugState.get_frame_zero_copy = zero_copy_enabled_ ? 1 : 0;
outDebugState.get_frame_timing_samples = get_frame_timing_samples_;
outDebugState.get_frame_timing_average_us =
@@ -1025,6 +1041,24 @@ namespace gvfg::internal
return giga_ioctl_release_video_frame(device_handle(), channelIndex) != FALSE;
}
bool PcieS2mmCaptureSession::zero_copy_release_revoked_by_unplug(
DWORD error, std::unique_lock<std::mutex> &lock)
{
if (error != ERROR_BAD_COMMAND)
return false;
// The driver can reject release before its unplug event reaches this
// thread. Wait only on this error path and accept it only if the event
// monitor confirms that the signal disappeared within the grace period.
const auto signalDisconnected = [this] {
return signal_presence_known_.load(std::memory_order_acquire) &&
!signal_present_.load(std::memory_order_acquire);
};
return signalDisconnected() ||
signal_presence_cv_.wait_for(lock, kZeroCopyUnplugGracePeriod,
signalDisconnected);
}
pcies2mm_status_t PcieS2mmCaptureSession::release_all_zero_copy_frames(bool includeInUse)
{
std::unique_lock<std::mutex> lock(mutex_);
@@ -1033,10 +1067,7 @@ namespace gvfg::internal
if (zero_copy_enabled_ && held_frame_.data && !release_zero_copy_frame(active_channel()))
{
const DWORD err = GetLastError();
const bool revokedByUnplug =
err == ERROR_BAD_COMMAND &&
signal_presence_known_.load(std::memory_order_acquire) &&
!signal_present_.load(std::memory_order_acquire);
const bool revokedByUnplug = zero_copy_release_revoked_by_unplug(err, lock);
if (!revokedByUnplug)
{
lock.unlock();
@@ -1372,6 +1403,7 @@ namespace gvfg::internal
stream_ready_pending_.store(false, std::memory_order_release);
signal_present_.store(false, std::memory_order_release);
signal_presence_known_.store(true, std::memory_order_release);
signal_presence_cv_.notify_all();
capture_active_ = false;
if (dma_event_)
SetEvent(dma_event_);
@@ -74,6 +74,8 @@ namespace gvfg::internal
bool stop_capture(uint32_t channelIndex) const;
bool acquire_zero_copy_frame(uint32_t channelIndex, const uint8_t *&outData) const;
bool release_zero_copy_frame(uint32_t channelIndex) const;
bool zero_copy_release_revoked_by_unplug(
DWORD error, std::unique_lock<std::mutex> &lock);
pcies2mm_status_t release_all_zero_copy_frames(bool includeInUse);
bool register_event(uint32_t channelIndex, uint32_t eventType, HANDLE eventHandle);
void unregister_event(uint32_t channelIndex, uint32_t eventType);
@@ -137,6 +139,7 @@ namespace gvfg::internal
mutable std::mutex mutex_;
std::condition_variable read_finished_cv_;
std::condition_variable signal_presence_cv_;
mutable std::mutex event_callback_mutex_;
pcies2mm_event_callback_t event_callback_ = nullptr;
void *event_callback_user_ = nullptr;
@@ -144,6 +147,10 @@ namespace gvfg::internal
uint64_t latest_sequence_ = 0;
uint64_t delivered_sequence_ = 0;
uint64_t wait_timeout_count_ = 0;
std::atomic<uint64_t> audio_dma_event_wakes_{0};
std::atomic<uint64_t> extra_audio_event_wakes_{0};
std::atomic<uint64_t> audio_frames_from_driver_{0};
std::atomic<uint64_t> audio_bytes_from_driver_{0};
uint64_t get_frame_timing_samples_ = 0;
uint32_t get_frame_timing_window_samples_ = 0;
double get_frame_timing_total_us_ = 0.0;
+4
View File
@@ -656,6 +656,10 @@ struct gvfg_channel_session_t
out.backend_capture_active = debugState.capture_active;
out.backend_latest_sequence = debugState.latest_sequence;
out.backend_delivered_sequence = debugState.delivered_sequence;
out.audio_dma_event_wakes = debugState.audio_dma_event_wakes;
out.extra_audio_event_wakes = debugState.extra_audio_event_wakes;
out.audio_frames_from_driver = debugState.audio_frames_from_driver;
out.audio_bytes_from_driver = debugState.audio_bytes_from_driver;
out.get_frame_zero_copy = debugState.get_frame_zero_copy;
out.get_frame_timing_samples = debugState.get_frame_timing_samples;
out.get_frame_timing_average_us = debugState.get_frame_timing_average_us;