Simplify GVFG API and frame diagnostics
This commit is contained in:
@@ -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_;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user