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
@@ -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;