feat: harden capture API and preview diagnostics
This commit is contained in:
@@ -9,12 +9,18 @@
|
||||
* Minimal capture flow:
|
||||
*
|
||||
* gvfg_device_info_t devices[GVFG_MAX_DEVICES] = {};
|
||||
* int count = gvfg_enumerate_devices(devices, GVFG_MAX_DEVICES);
|
||||
* const int count = gvfg_enumerate_devices(devices, GVFG_MAX_DEVICES);
|
||||
* if (count <= 0)
|
||||
* return;
|
||||
*
|
||||
* gvfg_handle h = NULL;
|
||||
* gvfg_create(&h);
|
||||
* gvfg_open(h, devices[0].index);
|
||||
* gvfg_start(h);
|
||||
* if (gvfg_create(&h) != GVFG_OK)
|
||||
* return;
|
||||
* if (gvfg_open(h, devices[0].index) != GVFG_OK ||
|
||||
* gvfg_start(h) != GVFG_OK) {
|
||||
* gvfg_destroy(h);
|
||||
* return;
|
||||
* }
|
||||
*
|
||||
* while (running) {
|
||||
* gvfg_frame_t frame = {};
|
||||
@@ -33,6 +39,18 @@
|
||||
* - The frame data pointer remains valid until gvfg_release_frame() is called.
|
||||
* Copy the data if it must outlive that call.
|
||||
* - At most one frame may be held by a handle at a time.
|
||||
* - Query functions and gvfg_poll_event() may run concurrently with the single
|
||||
* frame-reading thread. Lifecycle calls must be serialized by the application.
|
||||
* gvfg_stop() may cancel a blocked read/poll, but no thread may access a held
|
||||
* frame while stop/destroy invalidates it.
|
||||
*
|
||||
* ABI policy:
|
||||
* - gvfg_handle remains opaque.
|
||||
* - gvfg_frame_t contains only mandatory per-frame fields and is frozen after
|
||||
* the first stable SDK release.
|
||||
* - Extensible metadata is returned by dedicated query APIs whose output
|
||||
* structures begin with struct_size.
|
||||
* - Breaking existing ABI requires a new major SDK version.
|
||||
*/
|
||||
|
||||
#ifdef _WIN32
|
||||
@@ -69,6 +87,9 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* Consistent timeout value for an indefinite wait. A timeout of 0 never waits. */
|
||||
#define GVFG_TIMEOUT_INFINITE UINT32_MAX
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
@@ -83,7 +104,7 @@ typedef enum
|
||||
{
|
||||
GVFG_OK = 0,
|
||||
GVFG_EINVAL = -1, /* Invalid argument, such as NULL handle/output pointer. */
|
||||
GVFG_ENODEV = -2, /* No GVFG device, no signal, or device open failed. */
|
||||
GVFG_ENODEV = -2, /* No GVFG capture device, or the device cannot be opened. */
|
||||
GVFG_ESTATE = -3, /* API was called in the wrong state. */
|
||||
GVFG_EIO = -4, /* Driver/backend I/O failure. */
|
||||
GVFG_ENOTSUP = -5, /* Requested feature or format is not supported. */
|
||||
@@ -117,11 +138,12 @@ typedef struct
|
||||
|
||||
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. */
|
||||
int height; /* Signal height in pixels when connected. */
|
||||
char pixel_format[16]; /* Actual DMA payload format, for example Y210. */
|
||||
int pixel_format; /* gvfg_pixel_format_t value for the actual DMA payload. */
|
||||
int bit_depth; /* Signal bit depth derived from the payload format. */
|
||||
} gvfg_signal_status_t;
|
||||
|
||||
@@ -130,13 +152,13 @@ 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. */
|
||||
char pixel_format[32]; /* Native frame buffer format: YUY2 or Y210. */
|
||||
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
|
||||
{
|
||||
gvfg_signal_status_t input_signal; /* Decoded input signal metadata. */
|
||||
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(). */
|
||||
@@ -150,7 +172,7 @@ typedef struct
|
||||
int height; /* Frame height in pixels. */
|
||||
int pixel_format; /* gvfg_pixel_format_t value. */
|
||||
int bit_depth; /* Bits per color channel of the native frame. */
|
||||
uint64_t frame_id; /* Monotonic frame identifier from the backend. */
|
||||
uint64_t frame_id; /* Monotonic identifier within the current gvfg_start()/stop() run. */
|
||||
} gvfg_frame_t;
|
||||
|
||||
typedef struct
|
||||
@@ -163,7 +185,6 @@ typedef struct
|
||||
int plane_stride[GVFG_MAX_PLANES]; /* Bytes from one row to the next for each plane. */
|
||||
uint64_t plane_size[GVFG_MAX_PLANES]; /* Bytes available in each plane. */
|
||||
uint64_t plane_offset[GVFG_MAX_PLANES]; /* Byte offset from frame.data to each plane. */
|
||||
uint64_t reserved[8]; /* Reserved for future SDK/driver layout metadata. Must be ignored. */
|
||||
} gvfg_frame_layout_t;
|
||||
|
||||
typedef enum
|
||||
@@ -183,7 +204,7 @@ typedef enum
|
||||
typedef struct
|
||||
{
|
||||
gvfg_event_type_t type;
|
||||
uint64_t timestamp_ns;
|
||||
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(). */
|
||||
@@ -268,7 +289,7 @@ GVFG_API gvfg_status_t gvfg_open_channel(
|
||||
* Configure and start capture on an opened device.
|
||||
*
|
||||
* Parameters:
|
||||
* - handle: Opened session handle.
|
||||
* - handle: Running session handle.
|
||||
*
|
||||
* Returns:
|
||||
* - GVFG_OK on success, including when capture is already running or the SDK
|
||||
@@ -290,7 +311,8 @@ GVFG_API gvfg_status_t gvfg_start(
|
||||
* Parameters:
|
||||
* - handle: Running capture session.
|
||||
* - out_frame: Receives a frame descriptor. Must not be NULL.
|
||||
* - timeout_ms: Maximum time to wait. Use 0 to wait indefinitely.
|
||||
* - timeout_ms: Maximum time to wait. Use 0 for a non-blocking read or
|
||||
* GVFG_TIMEOUT_INFINITE to wait indefinitely.
|
||||
*
|
||||
* Returns:
|
||||
* - GVFG_OK on success.
|
||||
@@ -352,11 +374,13 @@ GVFG_API gvfg_status_t gvfg_release_frame(
|
||||
* Parameters:
|
||||
* - handle: Opened session handle.
|
||||
* - out_event: Receives the event. Must not be NULL.
|
||||
* - timeout_ms: Maximum time to wait. Use 0 to return immediately.
|
||||
* - timeout_ms: Maximum time to wait. Use 0 to return immediately or
|
||||
* GVFG_TIMEOUT_INFINITE to wait indefinitely.
|
||||
*
|
||||
* Returns:
|
||||
* - GVFG_OK on success.
|
||||
* - GVFG_EINVAL if handle or out_event is NULL.
|
||||
* - GVFG_ESTATE if capture is not running or is stopped while waiting.
|
||||
* - GVFG_ETIMEOUT if no event is available before timeout_ms expires.
|
||||
*/
|
||||
GVFG_API gvfg_status_t gvfg_poll_event(
|
||||
@@ -389,16 +413,18 @@ GVFG_API gvfg_status_t gvfg_stop(
|
||||
* Returns:
|
||||
* - GVFG_OK on success.
|
||||
* - GVFG_EINVAL if handle or out_status is NULL.
|
||||
* - GVFG_ENODEV if no valid signal information is available.
|
||||
* - GVFG_EINVAL if out_status->struct_size is too small.
|
||||
* - GVFG_ESTATE if no capture device is open.
|
||||
*
|
||||
* Use gvfg_runtime_info_t::last_frame for the frame buffer format.
|
||||
* 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.
|
||||
*/
|
||||
GVFG_API gvfg_status_t gvfg_get_signal_status(
|
||||
_In_ gvfg_handle handle,
|
||||
_Out_ gvfg_signal_status_t *out_status);
|
||||
_Inout_ gvfg_signal_status_t *out_status);
|
||||
|
||||
/*
|
||||
* Query runtime capture diagnostics.
|
||||
* Query runtime frame-delivery diagnostics.
|
||||
*
|
||||
* Parameters:
|
||||
* - handle: Opened or running session handle.
|
||||
@@ -407,13 +433,20 @@ GVFG_API gvfg_status_t gvfg_get_signal_status(
|
||||
* 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 current signal status, SDK-measured capture FPS, and the
|
||||
* number of frames delivered by the SDK.
|
||||
* 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.
|
||||
*/
|
||||
GVFG_API gvfg_status_t gvfg_get_runtime_info(
|
||||
_In_ gvfg_handle handle,
|
||||
_Out_ gvfg_runtime_info_t *out_info);
|
||||
_Inout_ 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(
|
||||
_In_ int pixel_format);
|
||||
|
||||
/*
|
||||
* Convert a GVFG status code to a static English error string.
|
||||
|
||||
@@ -177,6 +177,7 @@ namespace gvfg::internal
|
||||
configured_ = false;
|
||||
signal_presence_known_.store(false, std::memory_order_release);
|
||||
signal_present_.store(false, std::memory_order_release);
|
||||
signal_probe_active_.store(false, std::memory_order_release);
|
||||
clear_last_error();
|
||||
PCIES2MM_LOG("open_device: %s", wide_to_utf8(base_path_).c_str());
|
||||
return PCIES2MM_OK;
|
||||
@@ -190,6 +191,7 @@ namespace gvfg::internal
|
||||
configured_ = false;
|
||||
signal_presence_known_.store(false, std::memory_order_release);
|
||||
signal_present_.store(false, std::memory_order_release);
|
||||
signal_probe_active_.store(false, std::memory_order_release);
|
||||
base_path_.clear();
|
||||
friendly_name_.clear();
|
||||
reset_stats(stats_, PCIES2MM_STREAM_STOPPED);
|
||||
@@ -232,16 +234,10 @@ namespace gvfg::internal
|
||||
|
||||
const bool haveRawSize = widthOk && heightOk && rawWidth != 0 && rawHeight != 0;
|
||||
const bool presenceKnown = signal_presence_known_.load(std::memory_order_acquire);
|
||||
if (!presenceKnown)
|
||||
signal_present_.store(haveRawSize, std::memory_order_release);
|
||||
|
||||
// Before the driver reports an explicit plug/unplug event, raw H/V is
|
||||
// only a probe result, not a permanent disconnected state. This matters
|
||||
// when the cable was already connected before the SDK registered its
|
||||
// event handles and the first register read happened too early.
|
||||
const bool signalPresent = presenceKnown
|
||||
? signal_present_.load(std::memory_order_acquire)
|
||||
: haveRawSize;
|
||||
// H/V/format registers can retain their previous values after signal
|
||||
// loss. They describe a candidate DMA layout, never signal presence.
|
||||
const bool signalPresent = presenceKnown &&
|
||||
signal_present_.load(std::memory_order_acquire);
|
||||
pcies2mm_pixel_format_t fmt = formatOk ? decode_pixel_format(rawFormat) : PCIES2MM_PIXFMT_UNKNOWN;
|
||||
|
||||
const uint32_t width = signalPresent && haveRawSize ? rawWidth : 0;
|
||||
@@ -338,8 +334,13 @@ namespace gvfg::internal
|
||||
return fail(PCIES2MM_EIO, "REGISTER_EVENT", err);
|
||||
}
|
||||
|
||||
const bool startCaptureNow = signal_present_.load(std::memory_order_acquire);
|
||||
if (startCaptureNow)
|
||||
// Events are edge-triggered, so a source connected before registration
|
||||
// may not produce a plug-in event. Probe DMA using the register layout,
|
||||
// but do not report connected until a full frame is received.
|
||||
signal_presence_known_.store(false, std::memory_order_release);
|
||||
signal_present_.store(false, std::memory_order_release);
|
||||
const bool startProbeNow = refresh_stream_from_registers(true);
|
||||
if (startProbeNow)
|
||||
{
|
||||
// Match the proven CaptureDemo sequence: register events first,
|
||||
// enable DMA/video second, then start the single wait thread.
|
||||
@@ -357,7 +358,8 @@ namespace gvfg::internal
|
||||
}
|
||||
|
||||
running_ = true;
|
||||
capture_active_ = startCaptureNow;
|
||||
capture_active_ = startProbeNow;
|
||||
signal_probe_active_ = startProbeNow;
|
||||
try
|
||||
{
|
||||
capture_thread_ = std::thread(&PcieS2mmCaptureSession::capture_thread_proc, this);
|
||||
@@ -366,7 +368,8 @@ namespace gvfg::internal
|
||||
{
|
||||
running_ = false;
|
||||
capture_active_ = false;
|
||||
if (startCaptureNow)
|
||||
signal_probe_active_ = false;
|
||||
if (startProbeNow)
|
||||
{
|
||||
write_reg(video_base() + VIDEO_DMA_EN_OFFSET, 0);
|
||||
write_reg(video_base() + VIDEO_EN_OFFSET, 0);
|
||||
@@ -376,17 +379,11 @@ namespace gvfg::internal
|
||||
return fail(PCIES2MM_EIO, "capture_thread", ERROR_NOT_ENOUGH_MEMORY);
|
||||
}
|
||||
|
||||
if (startCaptureNow)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
stats_.state = PCIES2MM_STREAM_RUNNING;
|
||||
}
|
||||
|
||||
PCIES2MM_LOG("start: channel=%u base=0x%x bytes=%zu capture_active=%d",
|
||||
PCIES2MM_LOG("start: channel=%u base=0x%x bytes=%zu probe_active=%d",
|
||||
channel,
|
||||
video_base(),
|
||||
bytes,
|
||||
startCaptureNow ? 1 : 0);
|
||||
frame_size_bytes(),
|
||||
startProbeNow ? 1 : 0);
|
||||
return PCIES2MM_OK;
|
||||
}
|
||||
|
||||
@@ -398,6 +395,7 @@ namespace gvfg::internal
|
||||
const uint32_t channel = active_channel();
|
||||
running_ = false;
|
||||
capture_active_ = false;
|
||||
signal_probe_active_ = false;
|
||||
|
||||
if (device_ != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
@@ -450,6 +448,14 @@ namespace gvfg::internal
|
||||
};
|
||||
|
||||
if (timeoutMs == 0)
|
||||
{
|
||||
if (!hasFrame())
|
||||
{
|
||||
++wait_timeout_count_;
|
||||
return PCIES2MM_ETIMEOUT;
|
||||
}
|
||||
}
|
||||
else if (timeoutMs == UINT32_MAX)
|
||||
{
|
||||
frame_cv_.wait(lock, hasFrame);
|
||||
}
|
||||
@@ -722,10 +728,22 @@ namespace gvfg::internal
|
||||
break;
|
||||
if (waitResult == WAIT_TIMEOUT)
|
||||
{
|
||||
if (!signal_presence_known_.load(std::memory_order_acquire))
|
||||
if (!signal_presence_known_.load(std::memory_order_acquire) &&
|
||||
!capture_active_.load(std::memory_order_acquire) &&
|
||||
refresh_stream_from_registers(true))
|
||||
{
|
||||
pcies2mm_signal_status_t probe{};
|
||||
get_signal_status(probe);
|
||||
const bool dmaEnableOk = write_reg(video_base() + VIDEO_DMA_EN_OFFSET, 1);
|
||||
const bool videoEnableOk = write_reg(video_base() + VIDEO_EN_OFFSET, 1);
|
||||
if (dmaEnableOk && videoEnableOk)
|
||||
{
|
||||
signal_probe_active_.store(true, std::memory_order_release);
|
||||
capture_active_.store(true, std::memory_order_release);
|
||||
}
|
||||
else
|
||||
{
|
||||
write_reg(video_base() + VIDEO_DMA_EN_OFFSET, 0);
|
||||
write_reg(video_base() + VIDEO_EN_OFFSET, 0);
|
||||
}
|
||||
}
|
||||
if (signal_present_.load(std::memory_order_acquire) &&
|
||||
!capture_active_.load(std::memory_order_acquire))
|
||||
@@ -847,6 +865,14 @@ namespace gvfg::internal
|
||||
return;
|
||||
}
|
||||
|
||||
if (signal_probe_active_.exchange(false, std::memory_order_acq_rel))
|
||||
{
|
||||
signal_present_.store(true, std::memory_order_release);
|
||||
signal_presence_known_.store(true, std::memory_order_release);
|
||||
emit_event(PCIES2MM_EVENT_PLUG_IN, channel == 0 ? 0 : 4, video_irq_mask_bit());
|
||||
emit_event(PCIES2MM_EVENT_CAPTURE_RESUMED, channel == 0 ? 0 : 4, video_irq_mask_bit());
|
||||
}
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (pending_events_ > 0)
|
||||
@@ -870,6 +896,16 @@ namespace gvfg::internal
|
||||
|
||||
void PcieS2mmCaptureSession::handle_plugin_event(uint32_t channel)
|
||||
{
|
||||
if (signal_presence_known_.load(std::memory_order_acquire) &&
|
||||
signal_present_.load(std::memory_order_acquire))
|
||||
return;
|
||||
|
||||
if (capture_active_.exchange(false, std::memory_order_acq_rel))
|
||||
{
|
||||
write_reg(video_base() + VIDEO_DMA_EN_OFFSET, 0);
|
||||
write_reg(video_base() + VIDEO_EN_OFFSET, 0);
|
||||
}
|
||||
signal_probe_active_.store(false, std::memory_order_release);
|
||||
signal_present_.store(true, std::memory_order_release);
|
||||
signal_presence_known_.store(true, std::memory_order_release);
|
||||
emit_event(PCIES2MM_EVENT_PLUG_IN, channel == 0 ? 0 : 4, video_irq_mask_bit());
|
||||
@@ -880,6 +916,7 @@ namespace gvfg::internal
|
||||
|
||||
void PcieS2mmCaptureSession::handle_unplug_event(uint32_t channel)
|
||||
{
|
||||
signal_probe_active_.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);
|
||||
@@ -906,7 +943,7 @@ namespace gvfg::internal
|
||||
{
|
||||
if (!running_.load(std::memory_order_acquire) ||
|
||||
!signal_present_.load(std::memory_order_acquire) ||
|
||||
!refresh_stream_from_signal(true))
|
||||
!refresh_stream_from_registers(true))
|
||||
return false;
|
||||
|
||||
const bool dmaEnableOk = write_reg(video_base() + VIDEO_DMA_EN_OFFSET, 1);
|
||||
@@ -930,6 +967,7 @@ namespace gvfg::internal
|
||||
}
|
||||
|
||||
capture_active_.store(true, std::memory_order_release);
|
||||
signal_probe_active_.store(false, std::memory_order_release);
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
stream_error_ = false;
|
||||
@@ -941,31 +979,35 @@ namespace gvfg::internal
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PcieS2mmCaptureSession::refresh_stream_from_signal(bool resizeRing)
|
||||
bool PcieS2mmCaptureSession::refresh_stream_from_registers(bool resizeRing)
|
||||
{
|
||||
pcies2mm_signal_status_t signal{};
|
||||
if (get_signal_status(signal) != PCIES2MM_OK ||
|
||||
signal.width == 0 ||
|
||||
signal.height == 0 ||
|
||||
signal.pixel_format == PCIES2MM_PIXFMT_UNKNOWN)
|
||||
{
|
||||
PCIES2MM_ERROR_LOG("refresh_stream_from_signal failed");
|
||||
uint32_t width = 0;
|
||||
uint32_t height = 0;
|
||||
uint32_t rawFormat = 0;
|
||||
if (!read_reg(video_base() + VIDEO_HSIZE_OFFSET, width) ||
|
||||
!read_reg(video_base() + VIDEO_VSIZE_OFFSET, height) ||
|
||||
!read_reg(video_base() + VIDEO_FORMAT_OFFSET, rawFormat) ||
|
||||
width == 0 || height == 0)
|
||||
return false;
|
||||
}
|
||||
|
||||
const uint32_t bitDepth = bit_depth_for_pixfmt(signal.pixel_format);
|
||||
const size_t bytes = bytes_per_frame(signal.width, signal.height, signal.pixel_format);
|
||||
const pcies2mm_pixel_format_t pixelFormat = decode_pixel_format(rawFormat);
|
||||
if (pixelFormat == PCIES2MM_PIXFMT_UNKNOWN)
|
||||
return false;
|
||||
|
||||
const uint32_t bitDepth = bit_depth_for_pixfmt(pixelFormat);
|
||||
const size_t bytes = bytes_per_frame(width, height, pixelFormat);
|
||||
if (bytes == 0 || bytes > (std::numeric_limits<DWORD>::max)())
|
||||
{
|
||||
PCIES2MM_ERROR_LOG("refresh_stream_from_signal invalid frame size width=%u height=%u bytes=%zu",
|
||||
signal.width,
|
||||
signal.height,
|
||||
PCIES2MM_ERROR_LOG("refresh_stream_from_registers invalid frame size width=%u height=%u bytes=%zu",
|
||||
width,
|
||||
height,
|
||||
bytes);
|
||||
return false;
|
||||
}
|
||||
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
if (resizeRing)
|
||||
const bool wasRunning = running_.load(std::memory_order_acquire);
|
||||
if (resizeRing && wasRunning)
|
||||
{
|
||||
data_cv_.wait(lock, [this]()
|
||||
{ return !running_.load(std::memory_order_acquire) ||
|
||||
@@ -973,9 +1015,9 @@ namespace gvfg::internal
|
||||
if (!running_.load(std::memory_order_acquire))
|
||||
return false;
|
||||
}
|
||||
stream_desc_.width = signal.width;
|
||||
stream_desc_.height = signal.height;
|
||||
stream_desc_.pixel_format = signal.pixel_format;
|
||||
stream_desc_.width = width;
|
||||
stream_desc_.height = height;
|
||||
stream_desc_.pixel_format = pixelFormat;
|
||||
stream_bit_depth_ = bitDepth;
|
||||
if (resizeRing)
|
||||
{
|
||||
|
||||
@@ -62,7 +62,7 @@ namespace gvfg::internal
|
||||
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_signal(bool resizeRing);
|
||||
bool refresh_stream_from_registers(bool resizeRing);
|
||||
void publish_frame(size_t slotIndex, size_t bytes);
|
||||
void emit_event(pcies2mm_event_type_t type, uint32_t irqBit, uint32_t irqMask) const;
|
||||
|
||||
@@ -102,6 +102,7 @@ namespace gvfg::internal
|
||||
|
||||
std::atomic<bool> running_{false};
|
||||
std::atomic<bool> capture_active_{false};
|
||||
std::atomic<bool> signal_probe_active_{false};
|
||||
mutable std::atomic<bool> signal_presence_known_{false};
|
||||
mutable std::atomic<bool> signal_present_{false};
|
||||
std::thread capture_thread_;
|
||||
|
||||
@@ -8,18 +8,32 @@
|
||||
#include <chrono>
|
||||
#include <climits>
|
||||
#include <condition_variable>
|
||||
#include <cstddef>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <deque>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
using namespace gvfg::internal;
|
||||
|
||||
namespace
|
||||
{
|
||||
#if INTPTR_MAX == INT64_MAX
|
||||
static_assert(std::is_standard_layout_v<gvfg_frame_t>);
|
||||
static_assert(sizeof(gvfg_frame_t) == 40, "gvfg_frame_t x64 ABI must remain frozen");
|
||||
static_assert(offsetof(gvfg_frame_t, data) == 0);
|
||||
static_assert(offsetof(gvfg_frame_t, data_size) == 8);
|
||||
static_assert(offsetof(gvfg_frame_t, width) == 16);
|
||||
static_assert(offsetof(gvfg_frame_t, height) == 20);
|
||||
static_assert(offsetof(gvfg_frame_t, pixel_format) == 24);
|
||||
static_assert(offsetof(gvfg_frame_t, bit_depth) == 28);
|
||||
static_assert(offsetof(gvfg_frame_t, frame_id) == 32);
|
||||
#endif
|
||||
|
||||
void copy_cstr(char *dst, size_t dstSize, const char *src)
|
||||
{
|
||||
if (!dst || dstSize == 0)
|
||||
@@ -129,7 +143,7 @@ namespace
|
||||
}
|
||||
}
|
||||
|
||||
const char *gvfg_pixel_format_name(int fmt)
|
||||
const char *pixel_format_name(int fmt)
|
||||
{
|
||||
switch (fmt)
|
||||
{
|
||||
@@ -159,8 +173,6 @@ namespace
|
||||
std::memset(layout.plane_stride, 0, sizeof(layout.plane_stride));
|
||||
std::memset(layout.plane_size, 0, sizeof(layout.plane_size));
|
||||
std::memset(layout.plane_offset, 0, sizeof(layout.plane_offset));
|
||||
std::memset(layout.reserved, 0, sizeof(layout.reserved));
|
||||
|
||||
if (!frame.data || frame.data_size == 0 || frame.width <= 0 || frame.height <= 0)
|
||||
return GVFG_EINVAL;
|
||||
|
||||
@@ -263,6 +275,7 @@ struct gvfg_handle_t
|
||||
gvfg_status_t stop()
|
||||
{
|
||||
running = false;
|
||||
eventCv.notify_all();
|
||||
releaseHeldFrameForStop();
|
||||
if (backend)
|
||||
backend->stop_stream();
|
||||
@@ -284,6 +297,7 @@ 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);
|
||||
@@ -291,16 +305,18 @@ struct gvfg_handle_t
|
||||
out.channel = static_cast<int>(selectedChannel);
|
||||
out.width = static_cast<int>(width);
|
||||
out.height = static_cast<int>(height);
|
||||
copy_cstr(out.pixel_format, sizeof(out.pixel_format), gvfg_pixel_format_name(to_gvfg_pixel_format(pixelFormat)));
|
||||
out.pixel_format = to_gvfg_pixel_format(pixelFormat);
|
||||
out.bit_depth = static_cast<int>(bitDepth);
|
||||
}
|
||||
return map_status(status);
|
||||
// A disconnected input is a normal query result, not a missing-device
|
||||
// error. The opened device remains usable for plug-in monitoring.
|
||||
return status == PCIES2MM_ENODEV ? GVFG_OK : map_status(status);
|
||||
}
|
||||
|
||||
gvfg_status_t getRuntimeInfo(gvfg_runtime_info_t &out)
|
||||
{
|
||||
std::memset(&out, 0, sizeof(out));
|
||||
getSignalStatus(out.input_signal);
|
||||
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;
|
||||
|
||||
@@ -310,9 +326,7 @@ struct gvfg_handle_t
|
||||
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));
|
||||
copy_cstr(out.last_frame.pixel_format,
|
||||
sizeof(out.last_frame.pixel_format),
|
||||
gvfg_pixel_format_name(deliveredPixelFormat.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;
|
||||
@@ -509,10 +523,13 @@ struct gvfg_handle_t
|
||||
|
||||
gvfg_status_t pollEvent(gvfg_event_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]()
|
||||
{
|
||||
return !eventQueue.empty();
|
||||
return !eventQueue.empty() || !running.load(std::memory_order_acquire);
|
||||
};
|
||||
|
||||
if (timeoutMs == 0)
|
||||
@@ -520,11 +537,18 @@ struct gvfg_handle_t
|
||||
if (!hasEvent())
|
||||
return GVFG_ETIMEOUT;
|
||||
}
|
||||
else if (timeoutMs == GVFG_TIMEOUT_INFINITE)
|
||||
{
|
||||
eventCv.wait(lock, hasEvent);
|
||||
}
|
||||
else if (!eventCv.wait_for(lock, std::chrono::milliseconds(timeoutMs), hasEvent))
|
||||
{
|
||||
return GVFG_ETIMEOUT;
|
||||
}
|
||||
|
||||
if (eventQueue.empty())
|
||||
return GVFG_ESTATE;
|
||||
|
||||
out = eventQueue.front();
|
||||
eventQueue.pop_front();
|
||||
return GVFG_OK;
|
||||
@@ -734,13 +758,21 @@ extern "C"
|
||||
{
|
||||
if (!frame || !out_layout)
|
||||
return GVFG_EINVAL;
|
||||
if (out_layout->struct_size < sizeof(gvfg_frame_layout_t))
|
||||
constexpr uint32_t kFrameLayoutV1Size =
|
||||
static_cast<uint32_t>(offsetof(gvfg_frame_layout_t, plane_offset) + sizeof(out_layout->plane_offset));
|
||||
const uint32_t callerSize = out_layout->struct_size;
|
||||
if (callerSize < kFrameLayoutV1Size)
|
||||
return GVFG_EINVAL;
|
||||
|
||||
const uint32_t callerSize = out_layout->struct_size;
|
||||
std::memset(out_layout, 0, sizeof(*out_layout));
|
||||
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;
|
||||
return populate_frame_layout(*frame, *out_layout);
|
||||
return GVFG_OK;
|
||||
}
|
||||
|
||||
gvfg_status_t gvfg_release_frame(gvfg_handle handle, const gvfg_frame_t *frame)
|
||||
@@ -768,14 +800,47 @@ extern "C"
|
||||
{
|
||||
if (!handle || !out_status)
|
||||
return GVFG_EINVAL;
|
||||
return handle->getSignalStatus(*out_status);
|
||||
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;
|
||||
return GVFG_OK;
|
||||
}
|
||||
|
||||
gvfg_status_t gvfg_get_runtime_info(gvfg_handle handle, gvfg_runtime_info_t *out_info)
|
||||
{
|
||||
if (!handle || !out_info)
|
||||
return GVFG_EINVAL;
|
||||
return handle->getRuntimeInfo(*out_info);
|
||||
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;
|
||||
return GVFG_OK;
|
||||
}
|
||||
|
||||
const char *gvfg_pixel_format_name(int pixel_format)
|
||||
{
|
||||
return pixel_format_name(pixel_format);
|
||||
}
|
||||
|
||||
const char *gvfg_strerror(gvfg_status_t status)
|
||||
|
||||
Reference in New Issue
Block a user