Simplify capture to direct frame delivery

- support only the new vfg100_0821c driver; legacy driver behavior, done-index handling, and SDK frame-ring compatibility are intentionally unsupported
- replace the backend frame ring, slots, pending indices, and DMA acquisition worker with direct wait-event then GetFrame/Acquire delivery
- keep one copy buffer or one driver-owned zero-copy frame outstanding per channel and synchronize read completion during stop, format change, and teardown
- remove obsolete lost_frames, FRAME_LOSS events, dropped-frame counters, sample warnings, and related public/internal documentation
- remove periodic diagnostic register polling and let the event thread handle only format and plug state changes
- add separate GetFrame total, DMA event wait, driver IOCTL, and SDK processing timing statistics to the diagnostic status view
- clear stale DMA events before video start, preserve exact Win32 errors, and retry only documented transient not-ready/busy conditions within the caller timeout
- delete the obsolete DMA ring data-flow diagram and update architecture/API documentation for the direct frame path

Validation: user build exposed the Windows max macro conflict; corrected time_point::max with the macro-safe form. Final validation used static searches and git diff --check; no additional build was run.
This commit is contained in:
Mark.lin
2026-08-21 17:52:22 +08:00
parent 1574e90b49
commit ca19b4f276
14 changed files with 454 additions and 735 deletions
@@ -63,7 +63,6 @@ typedef struct pcies2mm_stream_stats_t
pcies2mm_stream_state_t state;
uint64_t frames_captured;
uint64_t frames_delivered;
uint64_t frames_dropped;
uint64_t dma_errors;
uint64_t interrupt_count;
} pcies2mm_stream_stats_t;
@@ -72,17 +71,21 @@ typedef struct pcies2mm_debug_state_t
{
int running;
int capture_active;
uint32_t pending_events;
uint64_t latest_sequence;
uint64_t delivered_sequence;
uint64_t active_delivery_slot;
uint64_t next_write_slot;
uint64_t ring_size;
int get_frame_zero_copy;
uint64_t get_frame_timing_samples;
double get_frame_timing_average_us;
double get_frame_timing_max300_us;
double get_frame_timing_max_us;
uint64_t event_wait_timing_samples;
double event_wait_timing_average_us;
double event_wait_timing_max300_us;
double event_wait_timing_max_us;
uint64_t sdk_processing_timing_samples;
double sdk_processing_timing_average_us;
double sdk_processing_timing_max300_us;
double sdk_processing_timing_max_us;
} pcies2mm_debug_state_t;
typedef enum pcies2mm_event_type_t
@@ -90,8 +93,7 @@ typedef enum pcies2mm_event_type_t
PCIES2MM_EVENT_PLUG_IN = 1,
PCIES2MM_EVENT_PLUG_OUT = 2,
PCIES2MM_EVENT_STREAM_READY = 3,
PCIES2MM_EVENT_FORMAT_CHANGE_BEGIN = 4,
PCIES2MM_EVENT_FRAME_LOSS = 5
PCIES2MM_EVENT_FORMAT_CHANGE_BEGIN = 4
} pcies2mm_event_type_t;
enum
@@ -100,12 +102,10 @@ enum
PCIES2MM_EVENT_MASK_PLUG_OUT = 1u << 1,
PCIES2MM_EVENT_MASK_STREAM_READY = 1u << 2,
PCIES2MM_EVENT_MASK_FORMAT_CHANGE_BEGIN = 1u << 3,
PCIES2MM_EVENT_MASK_FRAME_LOSS = 1u << 4,
PCIES2MM_EVENT_MASK_DEFAULT = PCIES2MM_EVENT_MASK_PLUG_IN |
PCIES2MM_EVENT_MASK_PLUG_OUT |
PCIES2MM_EVENT_MASK_STREAM_READY |
PCIES2MM_EVENT_MASK_FORMAT_CHANGE_BEGIN |
PCIES2MM_EVENT_MASK_FRAME_LOSS
PCIES2MM_EVENT_MASK_FORMAT_CHANGE_BEGIN
};
typedef void (*pcies2mm_event_callback_t)(pcies2mm_event_type_t event, void *user);
File diff suppressed because it is too large Load Diff
@@ -6,8 +6,8 @@
#include <windows.h>
#include <atomic>
#include <condition_variable>
#include <chrono>
#include <condition_variable>
#include <cstddef>
#include <cstdint>
#include <mutex>
@@ -78,13 +78,10 @@ namespace gvfg::internal
int get_frame(uint32_t channelIndex, uint32_t frameIndex, uint8_t *buffer, DWORD bufferSize) const;
void capture_thread_proc();
void handle_dma_event(uint32_t channel);
void handle_format_change_event(uint32_t channel);
void handle_plugin_event(uint32_t channel);
void handle_unplug_event(uint32_t channel);
bool resume_capture_from_signal(uint32_t channel);
bool refresh_stream_from_registers(bool resizeRing);
void publish_frame(size_t slotIndex, size_t bytes);
bool refresh_stream_from_registers(bool resizeBuffer);
void emit_event(pcies2mm_event_type_t type) const;
pcies2mm_status_t fail(pcies2mm_status_t status, const char *where, DWORD winerr = GetLastError()) const;
@@ -96,17 +93,9 @@ namespace gvfg::internal
uint32_t video_base() const;
uint32_t video_irq_mask_bit() const;
size_t frame_size_bytes() const;
struct FrameSlot
{
std::vector<uint8_t> data; // Frame byte storage for this ring slot.
const uint8_t *external_data = nullptr; // Driver-owned zero-copy frame.
bool zero_copy = false;
size_t bytes = 0; // Number of valid bytes read into data.
uint64_t sequence = 0; // Monotonic frame sequence assigned on publish.
bool ready = false; // True after the data thread publishes a frame.
bool in_use = false; // True while writing or while the caller holds the slot.
};
void record_get_frame_timing(double elapsedUs);
void record_event_wait_timing(double elapsedUs);
void record_sdk_processing_timing(double elapsedUs);
std::wstring base_path_;
std::wstring friendly_name_;
@@ -138,16 +127,11 @@ namespace gvfg::internal
std::thread capture_thread_;
mutable std::mutex mutex_;
std::condition_variable read_finished_cv_;
mutable std::mutex event_callback_mutex_;
std::condition_variable frame_cv_;
std::condition_variable data_cv_;
pcies2mm_event_callback_t event_callback_ = nullptr;
void *event_callback_user_ = nullptr;
uint32_t event_mask_filter_ = PCIES2MM_EVENT_MASK_DEFAULT;
uint32_t pending_events_ = 0;
std::vector<FrameSlot> frame_ring_;
size_t next_write_slot_ = 0;
size_t active_delivery_slot_ = static_cast<size_t>(-1);
uint64_t latest_sequence_ = 0;
uint64_t delivered_sequence_ = 0;
uint64_t wait_timeout_count_ = 0;
@@ -157,8 +141,23 @@ namespace gvfg::internal
double get_frame_timing_window_max_us_ = 0.0;
double get_frame_timing_last_max300_us_ = 0.0;
double get_frame_timing_lifetime_max_us_ = 0.0;
uint64_t event_wait_timing_samples_ = 0;
uint32_t event_wait_timing_window_samples_ = 0;
double event_wait_timing_total_us_ = 0.0;
double event_wait_timing_window_max_us_ = 0.0;
double event_wait_timing_last_max300_us_ = 0.0;
double event_wait_timing_lifetime_max_us_ = 0.0;
uint64_t sdk_processing_timing_samples_ = 0;
uint32_t sdk_processing_timing_window_samples_ = 0;
double sdk_processing_timing_total_us_ = 0.0;
double sdk_processing_timing_window_max_us_ = 0.0;
double sdk_processing_timing_last_max300_us_ = 0.0;
double sdk_processing_timing_lifetime_max_us_ = 0.0;
std::vector<uint8_t> copy_buffer_;
pcies2mm_frame_t held_frame_{};
bool frame_held_ = false;
bool read_in_progress_ = false;
std::chrono::steady_clock::time_point active_delivery_started_{};
std::chrono::steady_clock::time_point last_delivery_started_{};
bool stream_error_ = false;
pcies2mm_stream_stats_t stats_{};
mutable std::string last_error_;
+10 -19
View File
@@ -31,7 +31,6 @@ namespace
static_assert(GVFG_EVENT_MASK_SIGNAL_DISCONNECTED == PCIES2MM_EVENT_MASK_PLUG_OUT);
static_assert(GVFG_EVENT_MASK_STREAM_READY == PCIES2MM_EVENT_MASK_STREAM_READY);
static_assert(GVFG_EVENT_MASK_FORMAT_CHANGE_BEGIN == PCIES2MM_EVENT_MASK_FORMAT_CHANGE_BEGIN);
static_assert(GVFG_EVENT_MASK_FRAME_LOSS == PCIES2MM_EVENT_MASK_FRAME_LOSS);
#if INTPTR_MAX == INT64_MAX
static_assert(std::is_standard_layout_v<gvfg_frame_t>);
@@ -137,8 +136,6 @@ namespace
return GVFG_EVENT_STREAM_READY;
case PCIES2MM_EVENT_FORMAT_CHANGE_BEGIN:
return GVFG_EVENT_FORMAT_CHANGE_BEGIN;
case PCIES2MM_EVENT_FRAME_LOSS:
return GVFG_EVENT_FRAME_LOSS;
default:
return GVFG_EVENT_UNKNOWN;
}
@@ -354,7 +351,6 @@ struct gvfg_channel_session_t
pcies2mm_debug_state_t debugState{};
uint64_t waitTimeouts = 0;
backend->get_debug_stats(stats, waitTimeouts, debugState);
out.lost_frames = stats.frames_dropped;
return GVFG_OK;
}
@@ -380,19 +376,11 @@ struct gvfg_channel_session_t
const gvfg_event_type_t type = map_event_type(event);
{
std::lock_guard<std::mutex> lock(eventMutex);
if (type == GVFG_EVENT_FRAME_LOSS && !eventQueue.empty() &&
eventQueue.back().type == GVFG_EVENT_FRAME_LOSS)
{
++eventQueue.back().count;
eventCv.notify_one();
return;
}
if (eventQueue.size() >= 64)
eventQueue.pop_front();
gvfg_event_t out{};
out.struct_size = sizeof(out);
out.type = type;
out.count = type == GVFG_EVENT_FRAME_LOSS ? 1 : 0;
eventQueue.push_back(out);
}
eventCv.notify_one();
@@ -438,14 +426,14 @@ struct gvfg_channel_session_t
}
// configure_stream() is also used to enter event-monitoring mode. Keep
// its inactive placeholder ring minimal; the real signal descriptor
// its inactive placeholder buffer minimal; the real signal descriptor
// replaces it before DMA is enabled after reconnect.
pcies2mm_stream_desc_t desc{};
desc.width = configureWidth;
desc.height = configureHeight;
desc.pixel_format = configureFormat;
desc.buffer_count = 3;
desc.buffer_count = 1;
const pcies2mm_status_t st = backend->configure_stream(desc);
if (st != PCIES2MM_OK)
@@ -671,23 +659,26 @@ struct gvfg_channel_session_t
out.backend_state = static_cast<int>(stats.state);
out.backend_frames_captured = stats.frames_captured;
out.backend_frames_delivered = stats.frames_delivered;
out.backend_frames_dropped = stats.frames_dropped;
out.backend_dma_errors = stats.dma_errors;
out.backend_interrupt_count = stats.interrupt_count;
out.backend_wait_timeouts = waitTimeouts;
out.backend_running = debugState.running;
out.backend_capture_active = debugState.capture_active;
out.backend_pending_events = debugState.pending_events;
out.backend_latest_sequence = debugState.latest_sequence;
out.backend_delivered_sequence = debugState.delivered_sequence;
out.backend_active_delivery_slot = debugState.active_delivery_slot;
out.backend_next_write_slot = debugState.next_write_slot;
out.backend_ring_size = debugState.ring_size;
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;
out.get_frame_timing_max300_us = debugState.get_frame_timing_max300_us;
out.get_frame_timing_max_us = debugState.get_frame_timing_max_us;
out.event_wait_timing_samples = debugState.event_wait_timing_samples;
out.event_wait_timing_average_us = debugState.event_wait_timing_average_us;
out.event_wait_timing_max300_us = debugState.event_wait_timing_max300_us;
out.event_wait_timing_max_us = debugState.event_wait_timing_max_us;
out.sdk_processing_timing_samples = debugState.sdk_processing_timing_samples;
out.sdk_processing_timing_average_us = debugState.sdk_processing_timing_average_us;
out.sdk_processing_timing_max300_us = debugState.sdk_processing_timing_max300_us;
out.sdk_processing_timing_max_us = debugState.sdk_processing_timing_max_us;
}
return GVFG_OK;