Simplify GVFG API and frame diagnostics

This commit is contained in:
Mark.lin
2026-07-30 16:43:30 +08:00
parent 9124930e98
commit 2b5e75ade9
20 changed files with 245 additions and 257 deletions
+15 -35
View File
@@ -118,7 +118,6 @@ extern "C"
typedef struct
{
uint32_t struct_size; /* Set to sizeof(gvfg_signal_status_t) before calling. */
int connected; /* Non-zero while the selected channel has a valid input signal. */
int channel; /* gvfg_channel_t selected when the device was opened. */
int width; /* Signal width in pixels when connected. */
@@ -129,19 +128,8 @@ extern "C"
typedef struct
{
int width; /* Width of the most recent frame returned by gvfg_read_frame(). */
int height; /* Height of the most recent frame returned by gvfg_read_frame(). */
int bit_depth; /* Bits per color channel of the frame buffer. */
int pixel_format; /* gvfg_pixel_format_t value: YUY2 or Y210. */
int valid; /* Non-zero while capture is running after at least one frame read. */
} gvfg_last_frame_info_t;
typedef struct
{
uint32_t struct_size; /* Set to sizeof(gvfg_runtime_info_t) before calling. */
gvfg_last_frame_info_t last_frame; /* Last frame returned by gvfg_read_frame(). */
double capture_fps; /* Runtime FPS measured from frames returned by gvfg_read_frame(). */
uint64_t delivered_frames; /* Number of frames returned by gvfg_read_frame(). */
double capture_fps; /* Runtime FPS measured from frames returned by gvfg_read_frame(). */
uint64_t delivered_frames; /* Number of frames returned by gvfg_read_frame(). */
} gvfg_runtime_info_t;
typedef struct
@@ -157,7 +145,6 @@ extern "C"
typedef struct
{
uint32_t struct_size; /* Set to sizeof(gvfg_frame_layout_t) before calling. */
int plane_count; /* Number of valid entries in plane_data/plane_stride/plane_size. */
const void *plane_data[GVFG_MAX_PLANES]; /* Plane pointers inside frame.data, valid until gvfg_release_frame(). */
int plane_stride[GVFG_MAX_PLANES]; /* Bytes from one row to the next for each plane. */
@@ -168,15 +155,13 @@ extern "C"
{
GVFG_EVENT_UNKNOWN = 0,
GVFG_EVENT_SIGNAL_CONNECTED = 1,
GVFG_EVENT_SIGNAL_DISCONNECTED = 2
GVFG_EVENT_SIGNAL_DISCONNECTED = 2,
/* First complete frame after stream start, plug-in, or format recovery is ready. */
GVFG_EVENT_STREAM_READY = 3,
/* Input format is changing; pause use of resources created for the old format. */
GVFG_EVENT_FORMAT_CHANGE_BEGIN = 4
} gvfg_event_type_t;
typedef struct
{
gvfg_event_type_t type;
uint64_t timestamp_ns; /* Monotonic timestamp for ordering; not Unix/wall-clock time. */
} gvfg_event_t;
/* Opaque session handle created by gvfg_create() and released by gvfg_destroy(). */
typedef struct gvfg_handle_t *gvfg_handle;
@@ -299,13 +284,11 @@ extern "C"
*
* Parameters:
* - frame: Frame descriptor returned by gvfg_read_frame(). Must not be NULL.
* - out_layout: Receives plane pointers, strides, and sizes. Must not
* be NULL. Set out_layout->struct_size to sizeof(gvfg_frame_layout_t) before
* calling so future SDKs can safely extend this struct.
* - out_layout: Receives plane pointers, strides, and sizes. Must not be NULL.
*
* Returns:
* - GVFG_OK on success.
* - GVFG_EINVAL if frame/out_layout is NULL or struct_size is too small.
* - GVFG_EINVAL if frame or out_layout is NULL.
* - GVFG_ENOTSUP if the SDK cannot describe the frame layout.
*
* The returned plane_data pointers are owned by the SDK and remain valid only
@@ -313,7 +296,7 @@ extern "C"
*/
GVFG_API gvfg_status_t gvfg_get_frame_layout(
_In_ const gvfg_frame_t *frame,
_Inout_ gvfg_frame_layout_t *out_layout);
_Out_ gvfg_frame_layout_t *out_layout);
/*
* Release a frame returned by gvfg_read_frame().
@@ -349,7 +332,7 @@ extern "C"
*/
GVFG_API gvfg_status_t gvfg_poll_event(
_In_ gvfg_handle handle,
_Out_ gvfg_event_t *out_event,
_Out_ gvfg_event_type_t *out_event,
_In_ uint32_t timeout_ms);
/*
@@ -377,15 +360,14 @@ extern "C"
* Returns:
* - GVFG_OK on success.
* - GVFG_EINVAL if handle or out_status is NULL.
* - GVFG_EINVAL if out_status->struct_size is too small.
* - GVFG_ESTATE if no capture device is open.
*
* No input signal is a normal state: the function returns GVFG_OK with
* out_status->connected set to 0. Set out_status->struct_size before calling.
* out_status->connected set to 0.
*/
GVFG_API gvfg_status_t gvfg_get_signal_status(
_In_ gvfg_handle handle,
_Inout_ gvfg_signal_status_t *out_status);
_Out_ gvfg_signal_status_t *out_status);
/*
* Query runtime frame-delivery diagnostics.
@@ -397,16 +379,14 @@ extern "C"
* Returns:
* - GVFG_OK on success.
* - GVFG_EINVAL if handle or out_info is NULL.
* - GVFG_EINVAL if out_info->struct_size is too small.
*
* The result includes the latest delivered frame, SDK-measured capture FPS,
* and the number of frames delivered by the SDK. Query current input signal
* metadata separately with gvfg_get_signal_status(). Set out_info->struct_size
* before calling.
* metadata separately with gvfg_get_signal_status().
*/
GVFG_API gvfg_status_t gvfg_get_runtime_info(
_In_ gvfg_handle handle,
_Inout_ gvfg_runtime_info_t *out_info);
_Out_ gvfg_runtime_info_t *out_info);
/* Convert a gvfg_pixel_format_t value to a static English format name. */
GVFG_API const char *gvfg_pixel_format_name(
+10 -5
View File
@@ -17,17 +17,22 @@ extern "C" {
typedef struct
{
uint32_t struct_size; /* Set to sizeof(gvfg_debug_backend_stats_t) before calling. */
int width; /* Width of the most recent frame returned by gvfg_read_frame(). */
int height; /* Height of the most recent frame returned by gvfg_read_frame(). */
int bit_depth; /* Bits per color channel of the frame buffer. */
int pixel_format; /* gvfg_pixel_format_t value: YUY2 or Y210. */
int valid; /* Non-zero after a frame is returned during the current running session. */
} gvfg_debug_last_frame_info_t;
typedef struct
{
int sdk_running;
int frame_held;
uint32_t event_queue_depth;
double runtime_fps;
uint64_t frames_returned;
int last_frame_width;
int last_frame_height;
int last_frame_pixel_format;
int last_frame_bit_depth;
gvfg_debug_last_frame_info_t last_frame;
int backend_state;
uint64_t backend_frames_captured;
@@ -84,21 +84,21 @@ typedef struct pcies2mm_debug_state_t
typedef enum pcies2mm_event_type_t
{
PCIES2MM_EVENT_PLUG_IN = 1,
PCIES2MM_EVENT_PLUG_OUT = 2
PCIES2MM_EVENT_PLUG_OUT = 2,
PCIES2MM_EVENT_STREAM_READY = 3,
PCIES2MM_EVENT_FORMAT_CHANGE_BEGIN = 4
} pcies2mm_event_type_t;
enum
{
PCIES2MM_EVENT_MASK_PLUG_IN = 1u << 0,
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_DEFAULT = PCIES2MM_EVENT_MASK_PLUG_IN |
PCIES2MM_EVENT_MASK_PLUG_OUT
PCIES2MM_EVENT_MASK_PLUG_OUT |
PCIES2MM_EVENT_MASK_STREAM_READY |
PCIES2MM_EVENT_MASK_FORMAT_CHANGE_BEGIN
};
typedef struct pcies2mm_event_t
{
pcies2mm_event_type_t type;
uint64_t timestamp_ns;
} pcies2mm_event_t;
typedef void (*pcies2mm_event_callback_t)(const pcies2mm_event_t *event, void *user);
typedef void (*pcies2mm_event_callback_t)(pcies2mm_event_type_t event, void *user);
@@ -20,12 +20,6 @@ namespace
constexpr uint32_t kDefaultRingBufferCount = 3;
constexpr uint32_t kMaxRingBufferCount = 16;
static uint64_t steady_now_ns()
{
const auto now = std::chrono::steady_clock::now().time_since_epoch();
return static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(now).count());
}
static bool should_log_counter(uint64_t count)
{
return count <= 5 || (count % 60) == 0;
@@ -109,6 +103,10 @@ namespace
return PCIES2MM_EVENT_MASK_PLUG_IN;
case PCIES2MM_EVENT_PLUG_OUT:
return PCIES2MM_EVENT_MASK_PLUG_OUT;
case PCIES2MM_EVENT_STREAM_READY:
return PCIES2MM_EVENT_MASK_STREAM_READY;
case PCIES2MM_EVENT_FORMAT_CHANGE_BEGIN:
return PCIES2MM_EVENT_MASK_FORMAT_CHANGE_BEGIN;
default:
return 0;
}
@@ -354,6 +352,7 @@ namespace gvfg::internal
running_ = true;
capture_active_ = startProbeNow;
signal_probe_active_ = startProbeNow;
stream_ready_pending_ = startProbeNow;
try
{
capture_thread_ = std::thread(&PcieS2mmCaptureSession::capture_thread_proc, this);
@@ -363,6 +362,7 @@ namespace gvfg::internal
running_ = false;
capture_active_ = false;
signal_probe_active_ = false;
stream_ready_pending_ = false;
if (startProbeNow)
{
write_reg(video_base() + VIDEO_DMA_EN_OFFSET, 0);
@@ -390,6 +390,7 @@ namespace gvfg::internal
running_ = false;
capture_active_ = false;
signal_probe_active_ = false;
stream_ready_pending_ = false;
if (device_ != INVALID_HANDLE_VALUE)
{
@@ -731,6 +732,7 @@ namespace gvfg::internal
if (dmaEnableOk && videoEnableOk)
{
signal_probe_active_.store(true, std::memory_order_release);
stream_ready_pending_.store(true, std::memory_order_release);
capture_active_.store(true, std::memory_order_release);
}
else
@@ -861,7 +863,6 @@ namespace gvfg::internal
{
signal_present_.store(true, std::memory_order_release);
signal_presence_known_.store(true, std::memory_order_release);
emit_event(PCIES2MM_EVENT_PLUG_IN);
}
{
@@ -870,10 +871,14 @@ namespace gvfg::internal
--pending_events_;
publish_frame(slotIndex, static_cast<size_t>(ret));
}
if (stream_ready_pending_.exchange(false, std::memory_order_acq_rel))
emit_event(PCIES2MM_EVENT_STREAM_READY);
}
void PcieS2mmCaptureSession::handle_format_change_event(uint32_t channel)
{
emit_event(PCIES2MM_EVENT_FORMAT_CHANGE_BEGIN);
write_reg(video_base() + VIDEO_DMA_EN_OFFSET, 0);
write_reg(video_base() + VIDEO_EN_OFFSET, 0);
capture_active_ = false;
@@ -906,6 +911,7 @@ namespace gvfg::internal
void PcieS2mmCaptureSession::handle_unplug_event(uint32_t channel)
{
signal_probe_active_.store(false, std::memory_order_release);
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);
write_reg(video_base() + VIDEO_DMA_EN_OFFSET, 0);
@@ -942,6 +948,7 @@ namespace gvfg::internal
write_reg(video_base() + VIDEO_DMA_EN_OFFSET, 0);
write_reg(video_base() + VIDEO_EN_OFFSET, 0);
capture_active_.store(false, std::memory_order_release);
stream_ready_pending_.store(false, std::memory_order_release);
{
std::lock_guard<std::mutex> lock(mutex_);
stream_error_ = false;
@@ -956,6 +963,7 @@ namespace gvfg::internal
capture_active_.store(true, std::memory_order_release);
signal_probe_active_.store(false, std::memory_order_release);
stream_ready_pending_.store(true, std::memory_order_release);
{
std::lock_guard<std::mutex> lock(mutex_);
stream_error_ = false;
@@ -1055,10 +1063,7 @@ namespace gvfg::internal
user = event_callback_user_;
}
pcies2mm_event_t event{};
event.type = type;
event.timestamp_ns = steady_now_ns();
callback(&event, user);
callback(type, user);
}
pcies2mm_status_t PcieS2mmCaptureSession::fail(pcies2mm_status_t status, const char *where, DWORD winerr) const
@@ -103,6 +103,7 @@ namespace gvfg::internal
std::atomic<bool> running_{false};
std::atomic<bool> capture_active_{false};
std::atomic<bool> signal_probe_active_{false};
std::atomic<bool> stream_ready_pending_{false};
mutable std::atomic<bool> signal_presence_known_{false};
mutable std::atomic<bool> signal_present_{false};
std::thread capture_thread_;
+42 -71
View File
@@ -36,9 +36,8 @@ namespace
static_assert(offsetof(gvfg_frame_t, frame_id) == 32);
static_assert(std::is_standard_layout_v<gvfg_frame_layout_t>);
static_assert(sizeof(gvfg_frame_layout_t) == 32, "gvfg_frame_layout_t x64 ABI must remain stable");
static_assert(offsetof(gvfg_frame_layout_t, struct_size) == 0);
static_assert(offsetof(gvfg_frame_layout_t, plane_count) == 4);
static_assert(sizeof(gvfg_frame_layout_t) == 32, "gvfg_frame_layout_t x64 ABI must remain frozen");
static_assert(offsetof(gvfg_frame_layout_t, plane_count) == 0);
static_assert(offsetof(gvfg_frame_layout_t, plane_data) == 8);
static_assert(offsetof(gvfg_frame_layout_t, plane_stride) == 16);
static_assert(offsetof(gvfg_frame_layout_t, plane_size) == 24);
@@ -131,6 +130,10 @@ namespace
return GVFG_EVENT_SIGNAL_CONNECTED;
case PCIES2MM_EVENT_PLUG_OUT:
return GVFG_EVENT_SIGNAL_DISCONNECTED;
case PCIES2MM_EVENT_STREAM_READY:
return GVFG_EVENT_STREAM_READY;
case PCIES2MM_EVENT_FORMAT_CHANGE_BEGIN:
return GVFG_EVENT_FORMAT_CHANGE_BEGIN;
default:
return GVFG_EVENT_UNKNOWN;
}
@@ -297,7 +300,6 @@ struct gvfg_handle_t
gvfg_status_t getSignalStatus(gvfg_signal_status_t &out)
{
std::memset(&out, 0, sizeof(out));
out.struct_size = sizeof(out);
const pcies2mm_status_t status = querySignal();
{
std::lock_guard<std::mutex> lock(stateMutex);
@@ -319,20 +321,8 @@ struct gvfg_handle_t
return GVFG_ESTATE;
std::memset(&out, 0, sizeof(out));
out.struct_size = sizeof(out);
const uint64_t frames = deliveredFrames.load(std::memory_order_relaxed);
const bool deliveredValid = running.load(std::memory_order_relaxed) && frames > 0;
out.last_frame.valid = deliveredValid ? 1 : 0;
if (deliveredValid)
{
out.last_frame.width = static_cast<int>(deliveredWidth.load(std::memory_order_relaxed));
out.last_frame.height = static_cast<int>(deliveredHeight.load(std::memory_order_relaxed));
out.last_frame.bit_depth = static_cast<int>(deliveredBitDepth.load(std::memory_order_relaxed));
out.last_frame.pixel_format = deliveredPixelFormat.load(std::memory_order_relaxed);
}
out.capture_fps = runtimeFps.load(std::memory_order_relaxed);
out.delivered_frames = frames;
out.delivered_frames = deliveredFrames.load(std::memory_order_relaxed);
return GVFG_OK;
}
@@ -345,19 +335,17 @@ struct gvfg_handle_t
PCIES2MM_EVENT_MASK_DEFAULT);
}
static void onBackendEvent(const pcies2mm_event_t *event, void *user)
static void onBackendEvent(pcies2mm_event_type_t event, void *user)
{
auto *self = static_cast<gvfg_handle_t *>(user);
if (!self || !event)
if (!self)
return;
self->emitEvent(*event);
self->emitEvent(event);
}
void emitEvent(const pcies2mm_event_t &event)
void emitEvent(pcies2mm_event_type_t event)
{
gvfg_event_t out{};
out.type = map_event_type(event.type);
out.timestamp_ns = event.timestamp_ns;
const gvfg_event_type_t out = map_event_type(event);
{
std::lock_guard<std::mutex> lock(eventMutex);
if (eventQueue.size() >= 64)
@@ -541,31 +529,40 @@ struct gvfg_handle_t
backend->release_frame(frame);
}
gvfg_status_t pollEvent(gvfg_event_t &out, uint32_t timeoutMs)
gvfg_status_t pollEvent(gvfg_event_type_t &out, uint32_t timeoutMs)
{
if (!backend || !running.load(std::memory_order_acquire))
return GVFG_ESTATE;
std::unique_lock<std::mutex> lock(eventMutex);
const auto hasEvent = [this]()
const auto eventAvailableOrStopped = [this]()
{
return !eventQueue.empty() || !running.load(std::memory_order_acquire);
};
bool waitConditionMet = false;
if (timeoutMs == 0)
{
if (!hasEvent())
return GVFG_ETIMEOUT;
// Non-blocking: inspect the current state without sleeping.
waitConditionMet = eventAvailableOrStopped();
}
else if (timeoutMs == GVFG_TIMEOUT_INFINITE)
{
eventCv.wait(lock, hasEvent);
// Wake when an event arrives or capture is stopped.
eventCv.wait(lock, eventAvailableOrStopped);
waitConditionMet = true;
}
else if (!eventCv.wait_for(lock, std::chrono::milliseconds(timeoutMs), hasEvent))
else
{
return GVFG_ETIMEOUT;
waitConditionMet = eventCv.wait_for(lock,
std::chrono::milliseconds(timeoutMs),
eventAvailableOrStopped);
}
if (!waitConditionMet)
return GVFG_ETIMEOUT;
// A wake-up without an event means capture was stopped while waiting.
if (eventQueue.empty())
return GVFG_ESTATE;
@@ -581,10 +578,14 @@ struct gvfg_handle_t
out.sdk_running = running.load(std::memory_order_relaxed) ? 1 : 0;
out.runtime_fps = runtimeFps.load(std::memory_order_relaxed);
out.frames_returned = deliveredFrames.load(std::memory_order_relaxed);
out.last_frame_width = static_cast<int>(deliveredWidth.load(std::memory_order_relaxed));
out.last_frame_height = static_cast<int>(deliveredHeight.load(std::memory_order_relaxed));
out.last_frame_pixel_format = deliveredPixelFormat.load(std::memory_order_relaxed);
out.last_frame_bit_depth = static_cast<int>(deliveredBitDepth.load(std::memory_order_relaxed));
out.last_frame.valid = out.sdk_running && out.frames_returned > 0 ? 1 : 0;
if (out.last_frame.valid)
{
out.last_frame.width = static_cast<int>(deliveredWidth.load(std::memory_order_relaxed));
out.last_frame.height = static_cast<int>(deliveredHeight.load(std::memory_order_relaxed));
out.last_frame.bit_depth = static_cast<int>(deliveredBitDepth.load(std::memory_order_relaxed));
out.last_frame.pixel_format = deliveredPixelFormat.load(std::memory_order_relaxed);
}
{
std::lock_guard<std::mutex> lock(frameMutex);
@@ -701,7 +702,7 @@ struct gvfg_handle_t
pcies2mm_frame_t heldBackendFrame{};
std::mutex eventMutex;
std::condition_variable eventCv;
std::deque<gvfg_event_t> eventQueue;
std::deque<gvfg_event_type_t> eventQueue;
};
extern "C"
@@ -776,20 +777,12 @@ extern "C"
{
if (!frame || !out_layout)
return GVFG_EINVAL;
constexpr uint32_t kFrameLayoutV1Size =
static_cast<uint32_t>(offsetof(gvfg_frame_layout_t, plane_size) + sizeof(out_layout->plane_size));
const uint32_t callerSize = out_layout->struct_size;
if (callerSize < kFrameLayoutV1Size)
return GVFG_EINVAL;
gvfg_frame_layout_t layout{};
layout.struct_size = sizeof(layout);
const gvfg_status_t status = populate_frame_layout(*frame, layout);
if (status != GVFG_OK)
return status;
std::memcpy(out_layout, &layout, std::min<size_t>(callerSize, sizeof(layout)));
out_layout->struct_size = callerSize;
*out_layout = layout;
return GVFG_OK;
}
@@ -800,7 +793,7 @@ extern "C"
return handle->releaseFrame(*frame);
}
gvfg_status_t gvfg_poll_event(gvfg_handle handle, gvfg_event_t *out_event, uint32_t timeout_ms)
gvfg_status_t gvfg_poll_event(gvfg_handle handle, gvfg_event_type_t *out_event, uint32_t timeout_ms)
{
if (!handle || !out_event)
return GVFG_EINVAL;
@@ -818,20 +811,12 @@ extern "C"
{
if (!handle || !out_status)
return GVFG_EINVAL;
constexpr uint32_t kSignalStatusV1Size =
static_cast<uint32_t>(offsetof(gvfg_signal_status_t, bit_depth) + sizeof(out_status->bit_depth));
const uint32_t callerSize = out_status->struct_size;
if (callerSize < kSignalStatusV1Size)
return GVFG_EINVAL;
gvfg_signal_status_t statusInfo{};
statusInfo.struct_size = sizeof(statusInfo);
const gvfg_status_t status = handle->getSignalStatus(statusInfo);
if (status != GVFG_OK)
return status;
std::memcpy(out_status, &statusInfo, std::min<size_t>(callerSize, sizeof(statusInfo)));
out_status->struct_size = callerSize;
*out_status = statusInfo;
return GVFG_OK;
}
@@ -839,20 +824,12 @@ extern "C"
{
if (!handle || !out_info)
return GVFG_EINVAL;
constexpr uint32_t kRuntimeInfoV1Size =
static_cast<uint32_t>(offsetof(gvfg_runtime_info_t, delivered_frames) + sizeof(out_info->delivered_frames));
const uint32_t callerSize = out_info->struct_size;
if (callerSize < kRuntimeInfoV1Size)
return GVFG_EINVAL;
gvfg_runtime_info_t runtimeInfo{};
runtimeInfo.struct_size = sizeof(runtimeInfo);
const gvfg_status_t status = handle->getRuntimeInfo(runtimeInfo);
if (status != GVFG_OK)
return status;
std::memcpy(out_info, &runtimeInfo, std::min<size_t>(callerSize, sizeof(runtimeInfo)));
out_info->struct_size = callerSize;
*out_info = runtimeInfo;
return GVFG_OK;
}
@@ -891,18 +868,12 @@ extern "C"
if (!handle || !out_stats)
return GVFG_EINVAL;
const uint32_t callerSize = out_stats->struct_size;
if (callerSize < sizeof(uint32_t) || callerSize > sizeof(gvfg_debug_backend_stats_t))
return GVFG_EINVAL;
gvfg_debug_backend_stats_t stats{};
const gvfg_status_t status = handle->getDebugBackendStats(stats);
if (status != GVFG_OK)
return status;
stats.struct_size = sizeof(stats);
std::memcpy(out_stats, &stats, callerSize);
out_stats->struct_size = callerSize;
*out_stats = stats;
return GVFG_OK;
}