Simplify dual-channel capture and error handling

Compared with b5b045a:

- Model CH0 and CH1 as independent channel sessions sharing one device connection.

- Keep one outstanding frame per channel and tighten read/release ownership validation.

- Unify facade and PCIES2MM diagnostic details in a per-channel ChannelErrorState.

- Treat polling timeouts and stop wakeups as expected control flow instead of persistent faults.

- Report detailed errors only when an API operation actually fails.

- Remove obsolete preview timing/error diagnostics and the redundant debug error API.

- Refresh the public and internal API references, architecture diagrams, and learning guides.
This commit is contained in:
Mark.lin
2026-08-27 11:10:51 +08:00
parent b5b045a41a
commit f380a8a143
21 changed files with 1658 additions and 545 deletions
@@ -78,14 +78,6 @@ typedef struct pcies2mm_debug_state_t
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
@@ -141,13 +141,14 @@ namespace
namespace gvfg::internal
{
PcieS2mmCaptureSession::SharedDevice::~SharedDevice()
PcieS2mmDeviceConnection::~PcieS2mmDeviceConnection()
{
if (handle != INVALID_HANDLE_VALUE)
CloseHandle(handle);
}
PcieS2mmCaptureSession::PcieS2mmCaptureSession()
PcieS2mmCaptureSession::PcieS2mmCaptureSession(ChannelErrorState &errorState)
: error_state_(errorState)
{
stream_desc_.width = kDefaultWidth;
stream_desc_.height = kDefaultHeight;
@@ -173,59 +174,47 @@ namespace gvfg::internal
pcies2mm_status_t PcieS2mmCaptureSession::open_device(const PcieS2mmDevice &device)
{
close();
base_path_ = device.interface_path;
friendly_name_ = device.friendly_name.empty() ? L"PcieS2mm Capture Device" : device.friendly_name;
auto sharedDevice = std::make_shared<SharedDevice>();
sharedDevice->handle = CreateFileW(base_path_.c_str(),
auto connection = std::make_shared<PcieS2mmDeviceConnection>();
connection->handle = CreateFileW(device.interface_path.c_str(),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
nullptr);
if (sharedDevice->handle == INVALID_HANDLE_VALUE)
if (connection->handle == INVALID_HANDLE_VALUE)
{
const DWORD err = GetLastError();
close_handles();
return fail(PCIES2MM_EIO, "CreateFile(PcieS2mm)", err);
}
shared_device_ = std::move(sharedDevice);
device_ = shared_device_->handle;
device_connection_ = std::move(connection);
opened_ = true;
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);
signal_metadata_valid_ = false;
clear_last_error();
PCIES2MM_LOG("open_device: %s", wide_to_utf8(base_path_).c_str());
error_state_.clear();
PCIES2MM_LOG("open_device: %s", wide_to_utf8(device.interface_path).c_str());
return PCIES2MM_OK;
}
pcies2mm_status_t PcieS2mmCaptureSession::open_shared_device(
const PcieS2mmCaptureSession &source,
uint32_t channel)
pcies2mm_status_t PcieS2mmCaptureSession::open_device_connection(
const std::shared_ptr<PcieS2mmDeviceConnection> &connection)
{
if (channel >= kMaxChannels)
return PCIES2MM_EINVAL;
if (!source.opened_ || !source.shared_device_ ||
source.shared_device_->handle == INVALID_HANDLE_VALUE)
return PCIES2MM_ESTATE;
if (!connection || connection->handle == INVALID_HANDLE_VALUE)
return reject(PCIES2MM_ESTATE, "open device connection rejected: connection is invalid");
close();
shared_device_ = source.shared_device_;
device_ = shared_device_->handle;
base_path_ = source.base_path_;
friendly_name_ = source.friendly_name_;
opened_ = true;
device_connection_ = connection;
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);
signal_metadata_valid_ = false;
clear_last_error();
error_state_.clear();
return PCIES2MM_OK;
}
@@ -233,22 +222,19 @@ namespace gvfg::internal
{
pcies2mm_status_t result = stop_stream();
stop_event_monitoring();
if (zero_copy_enabled_ && device_ != INVALID_HANDLE_VALUE)
if (zero_copy_enabled_ && device_handle() != INVALID_HANDLE_VALUE)
{
const pcies2mm_status_t disableStatus = set_zero_copy_enabled(false);
if (result == PCIES2MM_OK)
result = disableStatus;
}
close_handles();
opened_ = false;
configured_ = false;
zero_copy_enabled_ = 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);
signal_metadata_valid_ = false;
base_path_.clear();
friendly_name_.clear();
reset_stats(stats_, PCIES2MM_STREAM_STOPPED);
return result;
}
@@ -256,18 +242,22 @@ namespace gvfg::internal
void PcieS2mmCaptureSession::close_handles()
{
close_event_handles();
device_ = INVALID_HANDLE_VALUE;
shared_device_.reset();
device_connection_.reset();
}
HANDLE PcieS2mmCaptureSession::device_handle() const
{
return device_connection_ ? device_connection_->handle : INVALID_HANDLE_VALUE;
}
pcies2mm_status_t PcieS2mmCaptureSession::set_channel(uint32_t channel)
{
if (!opened_)
return PCIES2MM_ESTATE;
if (device_handle() == INVALID_HANDLE_VALUE)
return reject(PCIES2MM_ESTATE, "set channel rejected: device is not open");
if (running_)
return PCIES2MM_ESTATE;
return reject(PCIES2MM_ESTATE, "set channel rejected: stream is running");
if (channel >= kMaxChannels)
return PCIES2MM_EINVAL;
return reject(PCIES2MM_EINVAL, "set channel rejected: channel index is invalid");
channel_ = channel;
signal_metadata_valid_ = false;
if (!start_event_monitoring())
@@ -277,10 +267,10 @@ namespace gvfg::internal
pcies2mm_status_t PcieS2mmCaptureSession::set_zero_copy_enabled(bool enabled)
{
if (!opened_ || device_ == INVALID_HANDLE_VALUE)
return PCIES2MM_ESTATE;
if (device_handle() == INVALID_HANDLE_VALUE)
return reject(PCIES2MM_ESTATE, "set zero copy rejected: device is not open");
if (running_)
return PCIES2MM_ESTATE;
return reject(PCIES2MM_ESTATE, "set zero copy rejected: stream is running");
if (zero_copy_enabled_ == enabled)
return PCIES2MM_OK;
@@ -296,7 +286,7 @@ namespace gvfg::internal
stop_event_monitoring();
}
if (!giga_ioctl_set_frame_zerocopy(device_, active_channel(), enabled ? TRUE : FALSE))
if (!giga_ioctl_set_frame_zerocopy(device_handle(), active_channel(), enabled ? TRUE : FALSE))
return fail(PCIES2MM_EIO, enabled ? "enable zero copy" : "disable zero copy");
zero_copy_enabled_ = enabled;
return PCIES2MM_OK;
@@ -304,14 +294,14 @@ namespace gvfg::internal
pcies2mm_status_t PcieS2mmCaptureSession::set_video_format(pcies2mm_pixel_format_t format)
{
if (!opened_)
return PCIES2MM_ESTATE;
if (device_handle() == INVALID_HANDLE_VALUE)
return reject(PCIES2MM_ESTATE, "set video format rejected: device is not open");
uint32_t value = 0;
if (format == PCIES2MM_PIXFMT_Y210)
value = 1;
else if (format != PCIES2MM_PIXFMT_YUY2)
return PCIES2MM_EINVAL;
return reject(PCIES2MM_EINVAL, "set video format rejected: pixel format is invalid");
if (!write_reg(VIDEO_OUTPUT_FORMAT_REGISTER, value))
return fail(PCIES2MM_EIO, "set_video_format");
@@ -323,8 +313,8 @@ namespace gvfg::internal
pcies2mm_status_t PcieS2mmCaptureSession::get_signal_status(pcies2mm_signal_status_t &out) const
{
if (!opened_)
return PCIES2MM_ESTATE;
if (device_handle() == INVALID_HANDLE_VALUE)
return reject(PCIES2MM_ESTATE, "get signal status rejected: device is not open");
std::memset(&out, 0, sizeof(out));
out.channel = channel_;
@@ -392,12 +382,12 @@ namespace gvfg::internal
pcies2mm_status_t PcieS2mmCaptureSession::configure_stream(const pcies2mm_stream_desc_t &desc)
{
if (!opened_)
return PCIES2MM_ESTATE;
if (device_handle() == INVALID_HANDLE_VALUE)
return reject(PCIES2MM_ESTATE, "configure stream rejected: device is not open");
if (running_)
return PCIES2MM_ESTATE;
return reject(PCIES2MM_ESTATE, "configure stream rejected: stream is running");
if (desc.width == 0 || desc.height == 0)
return PCIES2MM_EINVAL;
return reject(PCIES2MM_EINVAL, "configure stream rejected: frame dimensions are invalid");
pcies2mm_pixel_format_t fmt = desc.pixel_format == PCIES2MM_PIXFMT_UNKNOWN ? PCIES2MM_PIXFMT_YUY2 : desc.pixel_format;
switch (fmt)
@@ -415,14 +405,13 @@ namespace gvfg::internal
stream_bit_depth_ = bit_depth_for_pixfmt(fmt);
configured_ = true;
reset_stats(stats_, PCIES2MM_STREAM_CONFIGURED);
clear_last_error();
return PCIES2MM_OK;
}
pcies2mm_status_t PcieS2mmCaptureSession::start_stream()
{
if (!opened_ || !configured_)
return PCIES2MM_ESTATE;
if (device_handle() == INVALID_HANDLE_VALUE || !configured_)
return reject(PCIES2MM_ESTATE, "start stream rejected: device is not configured");
if (running_)
return PCIES2MM_OK;
@@ -454,18 +443,6 @@ namespace gvfg::internal
get_frame_timing_window_max_us_ = 0.0;
get_frame_timing_last_max300_us_ = 0.0;
get_frame_timing_lifetime_max_us_ = 0.0;
event_wait_timing_samples_ = 0;
event_wait_timing_window_samples_ = 0;
event_wait_timing_total_us_ = 0.0;
event_wait_timing_window_max_us_ = 0.0;
event_wait_timing_last_max300_us_ = 0.0;
event_wait_timing_lifetime_max_us_ = 0.0;
sdk_processing_timing_samples_ = 0;
sdk_processing_timing_window_samples_ = 0;
sdk_processing_timing_total_us_ = 0.0;
sdk_processing_timing_window_max_us_ = 0.0;
sdk_processing_timing_last_max300_us_ = 0.0;
sdk_processing_timing_lifetime_max_us_ = 0.0;
stream_error_ = false;
reset_stats(stats_, PCIES2MM_STREAM_CONFIGURED);
}
@@ -512,7 +489,7 @@ namespace gvfg::internal
signal_probe_active_ = false;
stream_ready_pending_ = false;
if (device_ != INVALID_HANDLE_VALUE)
if (device_handle() != INVALID_HANDLE_VALUE)
stop_video(active_channel());
if (dma_event_)
@@ -536,12 +513,13 @@ namespace gvfg::internal
pcies2mm_status_t PcieS2mmCaptureSession::wait_frame(uint32_t timeoutMs, pcies2mm_frame_t &out)
{
constexpr uint64_t kTimingWarmupFrames = 30;
reader_ready_.store(true, std::memory_order_release);
std::memset(&out, 0, sizeof(out));
{
std::unique_lock<std::mutex> lock(mutex_);
if (!running_ || frame_held_ || read_in_progress_)
return PCIES2MM_ESTATE;
return reject(PCIES2MM_ESTATE, "wait frame rejected: stream state does not allow another read");
if (!capture_active_.load(std::memory_order_acquire))
{
if (!signal_presence_known_.load(std::memory_order_acquire) ||
@@ -574,13 +552,11 @@ namespace gvfg::internal
};
const bool infiniteWait = timeoutMs == UINT32_MAX;
const auto attemptStarted = std::chrono::steady_clock::now();
const auto deadline = infiniteWait
? (std::chrono::steady_clock::time_point::max)()
: attemptStarted + std::chrono::milliseconds(timeoutMs);
: std::chrono::steady_clock::now() + std::chrono::milliseconds(timeoutMs);
const DWORD bytes = static_cast<DWORD>(frame_size_bytes());
const uint8_t *data = nullptr;
double eventWaitUs = 0.0;
double getFrameUs = 0.0;
for (;;)
{
@@ -597,11 +573,7 @@ namespace gvfg::internal
}
}
const auto eventWaitStarted = std::chrono::steady_clock::now();
const DWORD waitResult = WaitForSingleObject(dma_event_, waitMs);
const auto eventWaitFinished = std::chrono::steady_clock::now();
eventWaitUs += std::chrono::duration<double, std::micro>(
eventWaitFinished - eventWaitStarted).count();
if (waitResult == WAIT_TIMEOUT)
{
{
@@ -679,8 +651,10 @@ namespace gvfg::internal
release_zero_copy_frame(active_channel());
return PCIES2MM_ESTATE;
}
record_get_frame_timing(getFrameUs);
++latest_sequence_;
const bool timingWarmupComplete = latest_sequence_ > kTimingWarmupFrames;
if (timingWarmupComplete)
record_get_frame_timing(getFrameUs);
delivered_sequence_ = latest_sequence_;
++stats_.interrupt_count;
++stats_.frames_captured;
@@ -698,10 +672,6 @@ namespace gvfg::internal
out.bit_depth = stream_bit_depth_;
held_frame_ = out;
frame_held_ = true;
const double backendTotalUs = std::chrono::duration<double, std::micro>(
std::chrono::steady_clock::now() - attemptStarted).count();
record_event_wait_timing(eventWaitUs);
record_sdk_processing_timing((std::max)(0.0, backendTotalUs - eventWaitUs - getFrameUs));
}
if (stream_ready_pending_.exchange(false, std::memory_order_acq_rel))
emit_event(PCIES2MM_EVENT_STREAM_READY);
@@ -712,7 +682,7 @@ namespace gvfg::internal
{
std::unique_lock<std::mutex> lock(mutex_);
if (!frame_held_)
return PCIES2MM_ESTATE;
return reject(PCIES2MM_ESTATE, "release frame rejected: no frame is currently held");
if (frame.data != held_frame_.data ||
frame.data_size_bytes != held_frame_.data_size_bytes ||
frame.frame_id != held_frame_.frame_id ||
@@ -720,7 +690,7 @@ namespace gvfg::internal
frame.height != held_frame_.height ||
frame.pixel_format != held_frame_.pixel_format ||
frame.bit_depth != held_frame_.bit_depth)
return PCIES2MM_EINVAL;
return reject(PCIES2MM_EINVAL, "release frame rejected: frame token does not match the held frame");
if (zero_copy_enabled_ && !release_zero_copy_frame(active_channel()))
{
@@ -742,12 +712,6 @@ namespace gvfg::internal
return PCIES2MM_OK;
}
const char *PcieS2mmCaptureSession::last_error() const
{
std::lock_guard<std::mutex> lock(mutex_);
return last_error_.c_str();
}
void PcieS2mmCaptureSession::get_debug_stats(pcies2mm_stream_stats_t &outStats,
uint64_t &outWaitTimeouts,
pcies2mm_debug_state_t &outDebugState) const
@@ -768,59 +732,45 @@ namespace gvfg::internal
: 0.0;
outDebugState.get_frame_timing_max300_us = get_frame_timing_last_max300_us_;
outDebugState.get_frame_timing_max_us = get_frame_timing_lifetime_max_us_;
outDebugState.event_wait_timing_samples = event_wait_timing_samples_;
outDebugState.event_wait_timing_average_us =
event_wait_timing_samples_ > 0
? event_wait_timing_total_us_ / static_cast<double>(event_wait_timing_samples_)
: 0.0;
outDebugState.event_wait_timing_max300_us = event_wait_timing_last_max300_us_;
outDebugState.event_wait_timing_max_us = event_wait_timing_lifetime_max_us_;
outDebugState.sdk_processing_timing_samples = sdk_processing_timing_samples_;
outDebugState.sdk_processing_timing_average_us =
sdk_processing_timing_samples_ > 0
? sdk_processing_timing_total_us_ / static_cast<double>(sdk_processing_timing_samples_)
: 0.0;
outDebugState.sdk_processing_timing_max300_us = sdk_processing_timing_last_max300_us_;
outDebugState.sdk_processing_timing_max_us = sdk_processing_timing_lifetime_max_us_;
}
bool PcieS2mmCaptureSession::read_reg(uint32_t offset, uint32_t &out) const
{
if (is_write_only_register(offset))
return false;
return giga_ioctl_read_register(device_, offset, &out) != FALSE;
return giga_ioctl_read_register(device_handle(), offset, &out) != FALSE;
}
pcies2mm_status_t PcieS2mmCaptureSession::debug_read_register(uint32_t offset,
uint32_t &outValue) const
{
if (!opened_ || device_ == INVALID_HANDLE_VALUE)
return PCIES2MM_ESTATE;
if (device_handle() == INVALID_HANDLE_VALUE)
return reject(PCIES2MM_ESTATE, "debug register read rejected: device is not open");
if ((offset & 0x3u) != 0 || is_write_only_register(offset))
return PCIES2MM_EINVAL;
return reject(PCIES2MM_EINVAL, "debug register read rejected: offset is invalid or write-only");
return read_reg(offset, outValue) ? PCIES2MM_OK : fail(PCIES2MM_EIO, "debug_read_register");
}
bool PcieS2mmCaptureSession::write_reg(uint32_t offset, uint32_t value) const
{
return giga_ioctl_write_register(device_, offset, value) != FALSE;
return giga_ioctl_write_register(device_handle(), offset, value) != FALSE;
}
bool PcieS2mmCaptureSession::start_video(uint32_t channelIndex) const
{
return giga_ioctl_video_start(device_, channelIndex) != FALSE;
return giga_ioctl_video_start(device_handle(), channelIndex) != FALSE;
}
bool PcieS2mmCaptureSession::stop_video(uint32_t channelIndex) const
{
return giga_ioctl_video_stop(device_, channelIndex) != FALSE;
return giga_ioctl_video_stop(device_handle(), channelIndex) != FALSE;
}
bool PcieS2mmCaptureSession::acquire_zero_copy_frame(uint32_t channelIndex,
const uint8_t *&outData) const
{
const void *buffer = nullptr;
if (!giga_ioctl_acquire_video_frame_zerocopy(device_, channelIndex, &buffer))
if (!giga_ioctl_acquire_video_frame_zerocopy(device_handle(), channelIndex, &buffer))
return false;
outData = static_cast<const uint8_t *>(buffer);
return true;
@@ -828,7 +778,7 @@ namespace gvfg::internal
bool PcieS2mmCaptureSession::release_zero_copy_frame(uint32_t channelIndex) const
{
return giga_ioctl_release_video_frame(device_, channelIndex) != FALSE;
return giga_ioctl_release_video_frame(device_handle(), channelIndex) != FALSE;
}
pcies2mm_status_t PcieS2mmCaptureSession::release_all_zero_copy_frames(bool includeInUse)
@@ -851,21 +801,21 @@ namespace gvfg::internal
pcies2mm_status_t PcieS2mmCaptureSession::debug_write_register(uint32_t offset,
uint32_t value) const
{
if (!opened_ || device_ == INVALID_HANDLE_VALUE)
return PCIES2MM_ESTATE;
if (device_handle() == INVALID_HANDLE_VALUE)
return reject(PCIES2MM_ESTATE, "debug register write rejected: device is not open");
if ((offset & 0x3u) != 0)
return PCIES2MM_EINVAL;
return reject(PCIES2MM_EINVAL, "debug register write rejected: offset is not aligned");
return write_reg(offset, value) ? PCIES2MM_OK : fail(PCIES2MM_EIO, "debug_write_register");
}
bool PcieS2mmCaptureSession::register_event(uint32_t channelIndex, uint32_t eventType, HANDLE eventHandle)
{
return giga_ioctl_register_event(device_, channelIndex, eventType, eventHandle) != FALSE;
return giga_ioctl_register_event(device_handle(), channelIndex, eventType, eventHandle) != FALSE;
}
void PcieS2mmCaptureSession::unregister_event(uint32_t channelIndex, uint32_t eventType)
{
giga_ioctl_unregister_event(device_, channelIndex, eventType);
giga_ioctl_unregister_event(device_handle(), channelIndex, eventType);
}
bool PcieS2mmCaptureSession::create_and_register_events(uint32_t channelIndex)
@@ -965,7 +915,7 @@ namespace gvfg::internal
if (capture_thread_.joinable())
capture_thread_.join();
if (device_ != INVALID_HANDLE_VALUE)
if (device_handle() != INVALID_HANDLE_VALUE)
unregister_events(active_channel());
close_event_handles();
}
@@ -973,7 +923,7 @@ namespace gvfg::internal
int PcieS2mmCaptureSession::get_frame(uint32_t channelIndex, uint32_t frameIndex, uint8_t *buffer, DWORD bufferSize) const
{
uint32_t bytesReturned = 0;
const BOOL ok = giga_ioctl_get_frame(device_, channelIndex, frameIndex,
const BOOL ok = giga_ioctl_get_frame(device_handle(), channelIndex, frameIndex,
buffer, bufferSize, &bytesReturned);
if (!ok)
return -1;
@@ -1198,21 +1148,16 @@ namespace gvfg::internal
oss << (where ? where : "PcieS2mm") << " failed";
if (winerr != NO_ERROR)
oss << ": " << win32_error(winerr) << " (" << winerr << ")";
set_last_error(oss.str());
error_state_.set(oss.str());
PCIES2MM_ERROR_LOG("%s", oss.str().c_str());
return status;
}
void PcieS2mmCaptureSession::set_last_error(const std::string &message) const
pcies2mm_status_t PcieS2mmCaptureSession::reject(pcies2mm_status_t status,
const char *message) const
{
std::lock_guard<std::mutex> lock(mutex_);
last_error_ = message;
}
void PcieS2mmCaptureSession::clear_last_error() const
{
std::lock_guard<std::mutex> lock(mutex_);
last_error_.clear();
error_state_.set(message ? message : "PCIES2MM operation rejected");
return status;
}
uint32_t PcieS2mmCaptureSession::active_channel() const
@@ -1255,33 +1200,4 @@ namespace gvfg::internal
}
}
void PcieS2mmCaptureSession::record_event_wait_timing(double elapsedUs)
{
++event_wait_timing_samples_;
++event_wait_timing_window_samples_;
event_wait_timing_total_us_ += elapsedUs;
event_wait_timing_window_max_us_ = (std::max)(event_wait_timing_window_max_us_, elapsedUs);
event_wait_timing_lifetime_max_us_ = (std::max)(event_wait_timing_lifetime_max_us_, elapsedUs);
if (event_wait_timing_window_samples_ >= 300)
{
event_wait_timing_last_max300_us_ = event_wait_timing_window_max_us_;
event_wait_timing_window_samples_ = 0;
event_wait_timing_window_max_us_ = 0.0;
}
}
void PcieS2mmCaptureSession::record_sdk_processing_timing(double elapsedUs)
{
++sdk_processing_timing_samples_;
++sdk_processing_timing_window_samples_;
sdk_processing_timing_total_us_ += elapsedUs;
sdk_processing_timing_window_max_us_ = (std::max)(sdk_processing_timing_window_max_us_, elapsedUs);
sdk_processing_timing_lifetime_max_us_ = (std::max)(sdk_processing_timing_lifetime_max_us_, elapsedUs);
if (sdk_processing_timing_window_samples_ >= 300)
{
sdk_processing_timing_last_max300_us_ = sdk_processing_timing_window_max_us_;
sdk_processing_timing_window_samples_ = 0;
sdk_processing_timing_window_max_us_ = 0.0;
}
}
}
@@ -2,6 +2,7 @@
#include "pcies2mm_backend_types.h"
#include "pcies2mm_device.h"
#include "gvfg_error_state.h"
#include <windows.h>
@@ -12,23 +13,29 @@
#include <cstdint>
#include <mutex>
#include <memory>
#include <string>
#include <thread>
#include <vector>
namespace gvfg::internal
{
struct PcieS2mmDeviceConnection
{
HANDLE handle = INVALID_HANDLE_VALUE;
~PcieS2mmDeviceConnection();
};
class PcieS2mmCaptureSession
{
public:
PcieS2mmCaptureSession();
explicit PcieS2mmCaptureSession(ChannelErrorState &errorState);
PcieS2mmCaptureSession(const PcieS2mmCaptureSession &) = delete;
PcieS2mmCaptureSession &operator=(const PcieS2mmCaptureSession &) = delete;
~PcieS2mmCaptureSession();
pcies2mm_status_t open_device_index(size_t deviceIndex);
pcies2mm_status_t open_shared_device(const PcieS2mmCaptureSession &source,
uint32_t channel);
pcies2mm_status_t open_device_connection(
const std::shared_ptr<PcieS2mmDeviceConnection> &connection);
std::shared_ptr<PcieS2mmDeviceConnection> device_connection() const { return device_connection_; }
pcies2mm_status_t close();
pcies2mm_status_t set_channel(uint32_t channel);
@@ -44,7 +51,6 @@ namespace gvfg::internal
pcies2mm_status_t wait_frame(uint32_t timeoutMs, pcies2mm_frame_t &out);
pcies2mm_status_t release_frame(const pcies2mm_frame_t &frame);
const char *last_error() const;
void get_debug_stats(pcies2mm_stream_stats_t &outStats,
uint64_t &outWaitTimeouts,
pcies2mm_debug_state_t &outDebugState) const;
@@ -52,14 +58,9 @@ namespace gvfg::internal
pcies2mm_status_t debug_write_register(uint32_t offset, uint32_t value) const;
private:
struct SharedDevice
{
HANDLE handle = INVALID_HANDLE_VALUE;
~SharedDevice();
};
pcies2mm_status_t open_device(const PcieS2mmDevice &device);
void close_handles();
HANDLE device_handle() const;
bool read_reg(uint32_t offset, uint32_t &out) const;
bool write_reg(uint32_t offset, uint32_t value) const;
@@ -85,8 +86,7 @@ namespace gvfg::internal
void emit_event(pcies2mm_event_type_t type) const;
pcies2mm_status_t fail(pcies2mm_status_t status, const char *where, DWORD winerr = GetLastError()) const;
void set_last_error(const std::string &message) const;
void clear_last_error() const;
pcies2mm_status_t reject(pcies2mm_status_t status, const char *message) const;
uint32_t active_channel() const;
uint32_t video_event_mask() const;
@@ -94,14 +94,9 @@ namespace gvfg::internal
uint32_t video_irq_mask_bit() const;
size_t frame_size_bytes() const;
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_;
HANDLE device_ = INVALID_HANDLE_VALUE;
std::shared_ptr<SharedDevice> shared_device_;
ChannelErrorState &error_state_;
std::shared_ptr<PcieS2mmDeviceConnection> device_connection_;
HANDLE dma_event_ = nullptr;
HANDLE format_change_event_ = nullptr;
HANDLE plug_in_event_ = nullptr;
@@ -110,7 +105,6 @@ namespace gvfg::internal
pcies2mm_stream_desc_t stream_desc_{};
uint32_t stream_bit_depth_ = 8;
uint32_t channel_ = 0;
bool opened_ = false;
bool configured_ = false;
bool zero_copy_enabled_ = false;
@@ -141,18 +135,6 @@ 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;
@@ -160,6 +142,5 @@ namespace gvfg::internal
std::chrono::steady_clock::time_point active_delivery_started_{};
bool stream_error_ = false;
pcies2mm_stream_stats_t stats_{};
mutable std::string last_error_;
};
}
+185 -144
View File
@@ -82,37 +82,6 @@ namespace
}
}
const char *pcies2mm_status_text(pcies2mm_status_t st)
{
switch (st)
{
case PCIES2MM_OK:
return "ok";
case PCIES2MM_EINVAL:
return "invalid argument";
case PCIES2MM_ENODEV:
return "device not found";
case PCIES2MM_ESTATE:
return "invalid state";
case PCIES2MM_ENOTSUP:
return "not supported";
case PCIES2MM_ETIMEOUT:
return "timeout";
case PCIES2MM_EIO:
return "i/o error";
default:
return "unknown";
}
}
const char *pcies2mm_error_text(pcies2mm_status_t st, const gvfg::internal::PcieS2mmCaptureSession *session)
{
const char *detail = session ? session->last_error() : nullptr;
if (detail && detail[0])
return detail;
return pcies2mm_status_text(st);
}
void copy_wide_to_utf8(const std::wstring &src, char *dst, size_t dstSize)
{
if (!dst || dstSize == 0)
@@ -200,6 +169,11 @@ namespace
struct gvfg_channel_session_t
{
explicit gvfg_channel_session_t(ChannelErrorState &errorState)
: errorState(errorState)
{
}
~gvfg_channel_session_t()
{
close();
@@ -207,20 +181,18 @@ struct gvfg_channel_session_t
gvfg_status_t open(int index,
int channelIndex = GVFG_CHANNEL_0,
gvfg::internal::PcieS2mmCaptureSession *sharedDeviceSource = nullptr)
const std::shared_ptr<gvfg::internal::PcieS2mmDeviceConnection> &deviceConnection = nullptr)
{
if (index < 0 || (channelIndex != GVFG_CHANNEL_0 && channelIndex != GVFG_CHANNEL_1))
return GVFG_EINVAL;
return reject(GVFG_EINVAL, "gvfg_open_channel rejected: device or channel index is invalid");
close();
backend = std::make_unique<gvfg::internal::PcieS2mmCaptureSession>();
const pcies2mm_status_t stOpen = sharedDeviceSource
? backend->open_shared_device(*sharedDeviceSource,
static_cast<uint32_t>(channelIndex))
backend = std::make_unique<gvfg::internal::PcieS2mmCaptureSession>(errorState);
const pcies2mm_status_t stOpen = deviceConnection
? backend->open_device_connection(deviceConnection)
: backend->open_device_index(static_cast<size_t>(index));
if (stOpen != PCIES2MM_OK)
{
recordError(pcies2mm_error_text(stOpen, backend.get()));
backend.reset();
return map_status(stOpen);
}
@@ -232,7 +204,6 @@ struct gvfg_channel_session_t
const pcies2mm_status_t stChannel = backend->set_channel(selectedChannel);
if (stChannel != PCIES2MM_OK)
{
recordError(pcies2mm_error_text(stChannel, backend.get()));
close();
return map_status(stChannel);
}
@@ -242,7 +213,6 @@ struct gvfg_channel_session_t
const pcies2mm_status_t stZeroCopy = backend->set_zero_copy_enabled(true);
if (stZeroCopy != PCIES2MM_OK)
{
recordError(pcies2mm_error_text(stZeroCopy, backend.get()));
close();
return map_status(stZeroCopy);
}
@@ -255,7 +225,7 @@ struct gvfg_channel_session_t
gvfg_status_t setZeroCopyEnabled(bool enabled)
{
if (backend)
return GVFG_ESTATE;
return reject(GVFG_ESTATE, "set zero copy rejected: channel is already open");
zeroCopyRequested = enabled;
return GVFG_OK;
}
@@ -263,7 +233,11 @@ struct gvfg_channel_session_t
gvfg_status_t start()
{
if (!backend)
return GVFG_ESTATE;
return reject(GVFG_ESTATE, "gvfg_start_channel rejected: channel is not open");
// A new start request starts a new diagnostic lifetime. Any failure
// below will replace this with the detail for the failed operation.
errorState.clear();
if (running)
return GVFG_OK;
@@ -279,10 +253,7 @@ struct gvfg_channel_session_t
const pcies2mm_status_t st = backend->start_stream();
if (st != PCIES2MM_OK)
{
recordError(pcies2mm_error_text(st, backend.get()));
return map_status(st);
}
running = true;
return GVFG_OK;
@@ -299,8 +270,6 @@ struct gvfg_channel_session_t
// stop_stream() joins the backend event thread, so no new event can
// be queued after it returns.
st = backend->stop_stream();
if (st != PCIES2MM_OK)
recordError(pcies2mm_error_text(st, backend.get()));
}
{
@@ -325,6 +294,8 @@ struct gvfg_channel_session_t
gvfg_status_t getSignalStatus(gvfg_signal_status_t &out)
{
if (!backend)
return reject(GVFG_ESTATE, "gvfg_get_channel_signal_status rejected: channel is not open");
std::memset(&out, 0, sizeof(out));
const pcies2mm_status_t status = querySignal();
{
@@ -342,7 +313,7 @@ struct gvfg_channel_session_t
gvfg_status_t getRuntimeInfo(gvfg_runtime_info_t &out)
{
if (!backend)
return GVFG_ESTATE;
return reject(GVFG_ESTATE, "gvfg_get_channel_runtime_info rejected: channel is not open");
std::memset(&out, 0, sizeof(out));
out.capture_fps = runtimeFps.load(std::memory_order_relaxed);
@@ -406,7 +377,7 @@ struct gvfg_channel_session_t
gvfg_status_t configureStream()
{
if (!backend)
return GVFG_ESTATE;
return reject(GVFG_ESTATE, "configure stream rejected: channel is not open");
const pcies2mm_status_t signalStatus = querySignal();
if (signalStatus != PCIES2MM_OK)
@@ -437,26 +408,30 @@ struct gvfg_channel_session_t
const pcies2mm_status_t st = backend->configure_stream(desc);
if (st != PCIES2MM_OK)
{
recordError(pcies2mm_error_text(st, backend.get()));
return map_status(st);
}
if (waitingForSignal)
recordError(nullptr);
return GVFG_OK;
}
gvfg_status_t readFrame(gvfg_frame_t &out, uint32_t timeoutMs)
{
std::memset(&out, 0, sizeof(out));
if (!backend || !running)
return GVFG_ESTATE;
if (!backend)
return reject(GVFG_ESTATE, "gvfg_read_channel_frame rejected: channel is not open");
if (!running)
return reject(GVFG_ESTATE, "gvfg_read_channel_frame rejected: channel is not running");
const char *stateError = nullptr;
{
std::lock_guard<std::mutex> lock(frameMutex);
if (readInProgress || frameHeld)
return GVFG_ESTATE;
readInProgress = true;
if (frameHeld)
stateError = "gvfg_read_channel_frame rejected: previous frame has not been released";
else if (readInProgress)
stateError = "gvfg_read_channel_frame rejected: another read is already in progress";
else
readInProgress = true;
}
if (stateError)
return reject(GVFG_ESTATE, stateError);
pcies2mm_frame_t frame{};
const pcies2mm_status_t st = backend->wait_frame(timeoutMs, frame);
@@ -466,8 +441,6 @@ struct gvfg_channel_session_t
std::lock_guard<std::mutex> lock(frameMutex);
readInProgress = false;
}
if (st != PCIES2MM_ETIMEOUT && st != PCIES2MM_ESTATE)
recordError(pcies2mm_error_text(st, backend.get()));
return map_status(st);
}
@@ -476,7 +449,7 @@ struct gvfg_channel_session_t
backend->release_frame(frame);
std::lock_guard<std::mutex> lock(frameMutex);
readInProgress = false;
return GVFG_EIO;
return reject(GVFG_EIO, "gvfg_read_channel_frame received an invalid frame descriptor");
}
bool stoppedWhileWaiting = false;
@@ -514,7 +487,7 @@ struct gvfg_channel_session_t
std::lock_guard<std::mutex> lock(frameMutex);
heldBackendFrame = {};
frameHeld = false;
return strideStatus;
return reject(strideStatus, "gvfg_read_channel_frame received an invalid row stride");
}
out.pixel_format = to_gvfg_pixel_format(frame.pixel_format);
out.bit_depth = static_cast<int>(frame.bit_depth);
@@ -527,18 +500,18 @@ struct gvfg_channel_session_t
gvfg_status_t releaseFrame(const gvfg_frame_t &frameToken)
{
if (!backend)
return GVFG_ESTATE;
return reject(GVFG_ESTATE, "gvfg_release_channel_frame rejected: channel is not open");
{
std::lock_guard<std::mutex> lock(frameMutex);
if (!frameHeld)
return GVFG_ESTATE;
return reject(GVFG_ESTATE, "gvfg_release_channel_frame rejected: no frame is currently held");
int expectedStride = 0;
if (native_row_stride_bytes(heldBackendFrame.width,
heldBackendFrame.pixel_format,
expectedStride) != GVFG_OK)
return GVFG_ESTATE;
return reject(GVFG_ESTATE, "gvfg_release_channel_frame rejected: held frame has an invalid row stride");
if (frameToken.data != heldBackendFrame.data ||
frameToken.data_size != static_cast<uint64_t>(heldBackendFrame.data_size_bytes) ||
@@ -548,7 +521,7 @@ struct gvfg_channel_session_t
frameToken.pixel_format != to_gvfg_pixel_format(heldBackendFrame.pixel_format) ||
frameToken.bit_depth != static_cast<int>(heldBackendFrame.bit_depth) ||
frameToken.frame_id != heldBackendFrame.frame_id)
return GVFG_EINVAL;
return reject(GVFG_EINVAL, "gvfg_release_channel_frame rejected: frame token does not match the held frame");
const pcies2mm_status_t st = backend->release_frame(heldBackendFrame);
if (st != PCIES2MM_OK)
@@ -586,7 +559,7 @@ struct gvfg_channel_session_t
gvfg_status_t pollEvent(gvfg_event_t &out, uint32_t timeoutMs)
{
if (!backend)
return GVFG_ESTATE;
return reject(GVFG_ESTATE, "gvfg_poll_channel_event rejected: channel is not open");
std::unique_lock<std::mutex> lock(eventMutex);
const auto eventAvailableOrStopped = [this]()
@@ -618,8 +591,9 @@ struct gvfg_channel_session_t
return GVFG_ETIMEOUT;
if (eventQueue.empty())
return running.load(std::memory_order_acquire) ? GVFG_ETIMEOUT
: GVFG_ESTATE;
return running.load(std::memory_order_acquire)
? GVFG_ETIMEOUT
: GVFG_ESTATE;
out = eventQueue.front();
eventQueue.pop_front();
@@ -671,14 +645,6 @@ struct gvfg_channel_session_t
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;
@@ -687,14 +653,14 @@ struct gvfg_channel_session_t
gvfg_status_t debugReadRegister(uint32_t offset, uint32_t &outValue)
{
if (!backend)
return GVFG_ESTATE;
return reject(GVFG_ESTATE, "debug register read rejected: channel is not open");
return map_status(backend->debug_read_register(offset, outValue));
}
gvfg_status_t setVideoFormat(gvfg_pixel_format_t format)
{
if (!backend)
return GVFG_ESTATE;
return reject(GVFG_ESTATE, "gvfg_set_channel_video_format rejected: channel is not open");
pcies2mm_pixel_format_t backendFormat = PCIES2MM_PIXFMT_UNKNOWN;
if (format == GVFG_PIXFMT_YUY2)
@@ -702,36 +668,23 @@ struct gvfg_channel_session_t
else if (format == GVFG_PIXFMT_Y210)
backendFormat = PCIES2MM_PIXFMT_Y210;
else
return GVFG_EINVAL;
return reject(GVFG_EINVAL, "gvfg_set_channel_video_format rejected: pixel format is invalid");
const pcies2mm_status_t status = backend->set_video_format(backendFormat);
if (status != PCIES2MM_OK)
recordError(pcies2mm_error_text(status, backend.get()));
return map_status(status);
}
gvfg_status_t debugWriteRegister(uint32_t offset, uint32_t value)
{
if (!backend)
return GVFG_ESTATE;
return reject(GVFG_ESTATE, "debug register write rejected: channel is not open");
return map_status(backend->debug_write_register(offset, value));
}
gvfg_status_t getLastErrorDetail(char *outMessage, uint32_t outMessageSize)
gvfg_status_t reject(gvfg_status_t status, const char *message)
{
if (!outMessage || outMessageSize == 0)
return GVFG_EINVAL;
outMessage[0] = '\0';
if (!lastError.empty())
copy_cstr(outMessage, outMessageSize, lastError.c_str());
else
copy_cstr(outMessage, outMessageSize, backend ? backend->last_error() : "");
return GVFG_OK;
}
void recordError(const char *msg)
{
lastError = msg ? msg : "";
errorState.set(message ? message : "GVFG operation rejected");
return status;
}
void updateRuntimeFps(uint64_t ptsNs)
@@ -778,6 +731,7 @@ struct gvfg_channel_session_t
deliveredFrames.fetch_add(1, std::memory_order_relaxed);
}
ChannelErrorState &errorState;
std::unique_ptr<gvfg::internal::PcieS2mmCaptureSession> backend;
int currentIndex = -1;
uint32_t selectedChannel = GVFG_CHANNEL_0;
@@ -799,8 +753,6 @@ struct gvfg_channel_session_t
std::atomic<int> deliveredPixelFormat{GVFG_PIXFMT_UNKNOWN};
std::atomic<double> runtimeFps{0.0};
std::string lastError;
std::atomic<bool> running{false};
std::mutex frameMutex;
bool readInProgress = false;
@@ -813,6 +765,35 @@ struct gvfg_channel_session_t
struct gvfg_handle_t
{
gvfg_status_t rejectChannel(int channelIndex,
gvfg_status_t status,
const char *message)
{
if (channelIndex == GVFG_CHANNEL_0 || channelIndex == GVFG_CHANNEL_1)
channelErrors[static_cast<size_t>(channelIndex)].set(message ? message : "GVFG operation rejected");
return status;
}
gvfg_status_t rejectAllChannels(gvfg_status_t status, const char *message)
{
for (ChannelErrorState &error : channelErrors)
error.set(message ? message : "GVFG operation rejected");
return status;
}
gvfg_status_t copyChannelError(int channelIndex,
char *outMessage,
uint32_t outMessageSize) const
{
if ((channelIndex != GVFG_CHANNEL_0 && channelIndex != GVFG_CHANNEL_1) ||
!outMessage || outMessageSize == 0)
return GVFG_EINVAL;
const std::string detail = channelErrors[static_cast<size_t>(channelIndex)].message();
copy_cstr(outMessage, outMessageSize, detail.c_str());
return GVFG_OK;
}
gvfg_channel_session_t *findChannel(int channelIndex)
{
if (channelIndex != GVFG_CHANNEL_0 && channelIndex != GVFG_CHANNEL_1)
@@ -820,7 +801,7 @@ struct gvfg_handle_t
return channels[static_cast<size_t>(channelIndex)].get();
}
gvfg_channel_session_t *firstOpenChannel()
gvfg_channel_session_t *findOpenChannel()
{
if (channels[GVFG_CHANNEL_0])
return channels[GVFG_CHANNEL_0].get();
@@ -829,27 +810,37 @@ struct gvfg_handle_t
gvfg_status_t openChannel(int index, int channelIndex)
{
if (index < 0 || (channelIndex != GVFG_CHANNEL_0 && channelIndex != GVFG_CHANNEL_1))
if (channelIndex != GVFG_CHANNEL_0 && channelIndex != GVFG_CHANNEL_1)
return GVFG_EINVAL;
if (currentIndex >= 0 && currentIndex != index)
return GVFG_ESTATE;
const size_t slot = static_cast<size_t>(channelIndex);
if (index < 0)
{
channelErrors[slot].set("gvfg_open_channel rejected: device index is invalid");
return GVFG_EINVAL;
}
if (currentIndex >= 0 && currentIndex != index)
{
channelErrors[slot].set("gvfg_open_channel rejected: handle is already bound to another device");
return GVFG_ESTATE;
}
if (channels[slot])
return GVFG_OK;
auto channel = std::make_unique<gvfg_channel_session_t>();
auto channel = std::make_unique<gvfg_channel_session_t>(channelErrors[slot]);
channel->zeroCopyRequested = zeroCopyRequested;
channel->eventMask = eventMasks[slot];
gvfg::internal::PcieS2mmCaptureSession *sharedSource = nullptr;
if (gvfg_channel_session_t *existing = firstOpenChannel())
sharedSource = existing->backend.get();
const gvfg_status_t status = channel->open(index, channelIndex, sharedSource);
const gvfg_status_t status = channel->open(index,
channelIndex,
deviceConnection);
if (status != GVFG_OK)
return status;
if (!deviceConnection)
{
deviceConnection = channel->backend->device_connection();
}
channels[slot] = std::move(channel);
currentIndex = index;
return GVFG_OK;
@@ -857,8 +848,10 @@ struct gvfg_handle_t
gvfg_status_t setZeroCopyEnabled(bool enabled)
{
if (firstOpenChannel())
return GVFG_ESTATE;
if (deviceConnection)
return rejectAllChannels(
GVFG_ESTATE,
"gvfg_set_zero_copy_enabled rejected: a device is already open");
zeroCopyRequested = enabled;
return GVFG_OK;
}
@@ -868,10 +861,18 @@ struct gvfg_handle_t
if (channelIndex != GVFG_CHANNEL_0 && channelIndex != GVFG_CHANNEL_1)
return GVFG_EINVAL;
if ((eventMask & ~static_cast<uint32_t>(GVFG_EVENT_MASK_ALL)) != 0)
{
channelErrors[static_cast<size_t>(channelIndex)].set(
"gvfg_set_channel_event_mask rejected: event mask contains unsupported bits");
return GVFG_EINVAL;
}
const size_t slot = static_cast<size_t>(channelIndex);
if (channels[slot])
{
channelErrors[slot].set(
"gvfg_set_channel_event_mask rejected: channel is already open");
return GVFG_ESTATE;
}
eventMasks[slot] = eventMask;
return GVFG_OK;
}
@@ -892,16 +893,22 @@ struct gvfg_handle_t
gvfg_status_t debugReadRegister(uint32_t offset, uint32_t &outValue)
{
gvfg_channel_session_t *channel = firstOpenChannel();
return channel ? channel->debugReadRegister(offset, outValue) : GVFG_ESTATE;
gvfg_channel_session_t *channel = findOpenChannel();
return channel ? channel->debugReadRegister(offset, outValue)
: rejectAllChannels(GVFG_ESTATE,
"debug register read rejected: no channel is open");
}
gvfg_status_t debugWriteRegister(uint32_t offset, uint32_t value)
{
gvfg_channel_session_t *channel = firstOpenChannel();
return channel ? channel->debugWriteRegister(offset, value) : GVFG_ESTATE;
gvfg_channel_session_t *channel = findOpenChannel();
return channel ? channel->debugWriteRegister(offset, value)
: rejectAllChannels(GVFG_ESTATE,
"debug register write rejected: no channel is open");
}
std::array<ChannelErrorState, 2> channelErrors;
std::shared_ptr<gvfg::internal::PcieS2mmDeviceConnection> deviceConnection;
std::array<std::unique_ptr<gvfg_channel_session_t>, 2> channels;
std::array<uint32_t, 2> eventMasks{GVFG_EVENT_MASK_ALL, GVFG_EVENT_MASK_ALL};
int currentIndex = -1;
@@ -982,8 +989,12 @@ extern "C"
gvfg_status_t gvfg_set_zero_copy_enabled(gvfg_handle handle, int enabled)
{
if (!handle || (enabled != 0 && enabled != 1))
if (!handle)
return GVFG_EINVAL;
if (enabled != 0 && enabled != 1)
return handle->rejectAllChannels(
GVFG_EINVAL,
"gvfg_set_zero_copy_enabled rejected: enabled must be 0 or 1");
return handle->setZeroCopyEnabled(enabled != 0);
}
@@ -1000,7 +1011,10 @@ extern "C"
if (!handle)
return GVFG_EINVAL;
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
return channel ? channel->start() : GVFG_ESTATE;
return channel ? channel->start()
: handle->rejectChannel(channel_index,
GVFG_ESTATE,
"gvfg_start_channel rejected: channel is not open");
}
gvfg_status_t gvfg_set_channel_video_format(gvfg_handle handle,
@@ -1010,7 +1024,10 @@ extern "C"
if (!handle)
return GVFG_EINVAL;
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
return channel ? channel->setVideoFormat(format) : GVFG_ESTATE;
return channel ? channel->setVideoFormat(format)
: handle->rejectChannel(channel_index,
GVFG_ESTATE,
"gvfg_set_channel_video_format rejected: channel is not open");
}
gvfg_status_t gvfg_read_channel_frame(gvfg_handle handle,
@@ -1018,20 +1035,34 @@ extern "C"
gvfg_frame_t *out_frame,
uint32_t timeout_ms)
{
if (!handle || !out_frame)
if (!handle)
return GVFG_EINVAL;
if (!out_frame)
return handle->rejectChannel(channel_index,
GVFG_EINVAL,
"gvfg_read_channel_frame rejected: out_frame is null");
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
return channel ? channel->readFrame(*out_frame, timeout_ms) : GVFG_ESTATE;
return channel ? channel->readFrame(*out_frame, timeout_ms)
: handle->rejectChannel(channel_index,
GVFG_ESTATE,
"gvfg_read_channel_frame rejected: channel is not open");
}
gvfg_status_t gvfg_release_channel_frame(gvfg_handle handle,
int channel_index,
const gvfg_frame_t *frame)
{
if (!handle || !frame)
if (!handle)
return GVFG_EINVAL;
if (!frame)
return handle->rejectChannel(channel_index,
GVFG_EINVAL,
"gvfg_release_channel_frame rejected: frame is null");
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
return channel ? channel->releaseFrame(*frame) : GVFG_ESTATE;
return channel ? channel->releaseFrame(*frame)
: handle->rejectChannel(channel_index,
GVFG_ESTATE,
"gvfg_release_channel_frame rejected: channel is not open");
}
gvfg_status_t gvfg_poll_channel_event(gvfg_handle handle,
@@ -1039,11 +1070,17 @@ extern "C"
gvfg_event_t *out_event,
uint32_t timeout_ms)
{
if (!handle || !out_event || out_event->struct_size < sizeof(gvfg_event_t))
if (!handle)
return GVFG_EINVAL;
if (!out_event || out_event->struct_size < sizeof(gvfg_event_t))
return handle->rejectChannel(channel_index,
GVFG_EINVAL,
"gvfg_poll_channel_event rejected: output event is invalid");
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
if (!channel)
return GVFG_ESTATE;
return handle->rejectChannel(channel_index,
GVFG_ESTATE,
"gvfg_poll_channel_event rejected: channel is not open");
gvfg_event_t event{};
event.struct_size = sizeof(event);
const gvfg_status_t status = channel->pollEvent(event, timeout_ms);
@@ -1064,18 +1101,27 @@ extern "C"
if (!handle)
return GVFG_EINVAL;
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
return channel ? channel->stop() : GVFG_ESTATE;
return channel ? channel->stop()
: handle->rejectChannel(channel_index,
GVFG_ESTATE,
"gvfg_stop_channel rejected: channel is not open");
}
gvfg_status_t gvfg_get_channel_signal_status(gvfg_handle handle,
int channel_index,
gvfg_signal_status_t *out_status)
{
if (!handle || !out_status)
if (!handle)
return GVFG_EINVAL;
if (!out_status)
return handle->rejectChannel(channel_index,
GVFG_EINVAL,
"gvfg_get_channel_signal_status rejected: out_status is null");
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
if (!channel)
return GVFG_ESTATE;
return handle->rejectChannel(channel_index,
GVFG_ESTATE,
"gvfg_get_channel_signal_status rejected: channel is not open");
return channel->getSignalStatus(*out_status);
}
@@ -1083,10 +1129,17 @@ extern "C"
int channel_index,
gvfg_runtime_info_t *out_info)
{
if (!handle || !out_info)
if (!handle)
return GVFG_EINVAL;
if (!out_info)
return handle->rejectChannel(channel_index,
GVFG_EINVAL,
"gvfg_get_channel_runtime_info rejected: out_info is null");
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
return channel ? channel->getRuntimeInfo(*out_info) : GVFG_ESTATE;
return channel ? channel->getRuntimeInfo(*out_info)
: handle->rejectChannel(channel_index,
GVFG_ESTATE,
"gvfg_get_channel_runtime_info rejected: channel is not open");
}
const char *gvfg_get_version(void)
@@ -1129,8 +1182,7 @@ extern "C"
{
if (!handle)
return GVFG_EINVAL;
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
return channel ? channel->getLastErrorDetail(out_message, out_message_size) : GVFG_ESTATE;
return handle->copyChannelError(channel_index, out_message, out_message_size);
}
gvfg_status_t gvfg_debug_get_channel_backend_stats(gvfg_handle handle,
@@ -1152,17 +1204,6 @@ extern "C"
return GVFG_OK;
}
gvfg_status_t gvfg_debug_get_channel_last_error_detail(gvfg_handle handle,
int channel_index,
char *out_message,
uint32_t out_message_size)
{
if (!handle)
return GVFG_EINVAL;
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
return channel ? channel->getLastErrorDetail(out_message, out_message_size) : GVFG_ESTATE;
}
gvfg_status_t gvfg_debug_read_register(gvfg_handle handle,
uint32_t offset,
uint32_t *out_value)
+34
View File
@@ -0,0 +1,34 @@
#pragma once
#include <mutex>
#include <string>
#include <utility>
namespace gvfg::internal
{
class ChannelErrorState final
{
public:
void set(std::string message)
{
std::lock_guard<std::mutex> lock(mutex_);
message_ = std::move(message);
}
void clear()
{
std::lock_guard<std::mutex> lock(mutex_);
message_.clear();
}
std::string message() const
{
std::lock_guard<std::mutex> lock(mutex_);
return message_;
}
private:
mutable std::mutex mutex_;
std::string message_;
};
}