Simplify the SDK around GigabyteLib

Compared with 0af70a5:

- Remove the generic backend directory and legacy PCIE S2MM naming so the SDK now has a direct gvfg facade to GigabyteCaptureSession to GvfgSdk.lib structure.

- Delete the unsupported video-format setter and direct register read/write APIs instead of retaining compatibility methods that cannot be implemented by the supplied GigabyteLib interface.

- Remove output-format controls and requested-format state from the Qt sample; capture now follows the native YUY2 or Y210 format reported by GvfgGetVideoInfo.

- Eliminate the placeholder stream descriptor and allocate copy buffers from current GigabyteLib video metadata when a frame becomes available.

- Rename the internal debug stats API and fields from backend terminology to channel/session terminology, while retaining capture, audio, event, timeout, timing, and zero-copy diagnostics.

- Update source paths and documentation to describe GigabyteLib as the only lower layer and remove stale format-setting, register, and direct IOCTL guidance.

Validation: git diff --cached --check passed. MSVC x64 Release and Debug gvfg.dll builds passed, the Release Qt sample rebuilt and linked successfully, dumpbin verified the intentional 29-export API, and the Release package script produced an archive containing the sample and gvfg.dll without private GigabyteLib files or giga_ioctl.dll. No runtime capture, audio, zero-copy, hot-plug, dual-channel, shutdown, MinGW, or hardware validation was run.
This commit is contained in:
Mark.lin
2026-09-14 10:05:54 +08:00
parent 0af70a59ca
commit a04bd611f9
26 changed files with 532 additions and 920 deletions
@@ -1,118 +0,0 @@
#pragma once
#include <stdint.h>
#include <stddef.h>
typedef enum pcies2mm_status_t
{
PCIES2MM_OK = 0,
PCIES2MM_EINVAL,
PCIES2MM_ENODEV,
PCIES2MM_ESTATE,
PCIES2MM_ENOTSUP,
PCIES2MM_ETIMEOUT,
PCIES2MM_EIO
} pcies2mm_status_t;
typedef enum pcies2mm_pixel_format_t
{
PCIES2MM_PIXFMT_UNKNOWN = 0,
PCIES2MM_PIXFMT_Y210 = 2,
PCIES2MM_PIXFMT_YUY2 = 3
} pcies2mm_pixel_format_t;
typedef enum pcies2mm_stream_state_t
{
PCIES2MM_STREAM_STOPPED = 0,
PCIES2MM_STREAM_CONFIGURED = 1,
PCIES2MM_STREAM_RUNNING = 2
} pcies2mm_stream_state_t;
typedef struct pcies2mm_stream_desc_t
{
uint32_t width;
uint32_t height;
pcies2mm_pixel_format_t pixel_format;
uint32_t buffer_count;
uint32_t flags;
} pcies2mm_stream_desc_t;
typedef struct pcies2mm_signal_status_t
{
int connected;
uint32_t channel;
uint32_t width;
uint32_t height;
pcies2mm_pixel_format_t pixel_format;
uint32_t bit_depth;
} pcies2mm_signal_status_t;
typedef struct pcies2mm_frame_t
{
const void *data; /* Valid until the next PcieS2mmCaptureSession::wait_frame() or close(). */
size_t data_size_bytes;
uint64_t frame_id;
uint64_t timestamp_ns;
uint32_t width;
uint32_t height;
pcies2mm_pixel_format_t pixel_format;
uint32_t bit_depth;
} pcies2mm_frame_t;
typedef struct pcies2mm_audio_format_t
{
uint32_t sample_rate;
uint32_t channels;
uint32_t bits_per_sample;
uint32_t frames_per_second;
uint32_t frame_bytes;
uint32_t block_align;
} pcies2mm_audio_format_t;
typedef struct pcies2mm_stream_stats_t
{
pcies2mm_stream_state_t state;
uint64_t frames_captured;
uint64_t frames_delivered;
uint64_t dma_errors;
uint64_t interrupt_count;
} pcies2mm_stream_stats_t;
typedef struct pcies2mm_debug_state_t
{
int running;
int capture_active;
uint64_t latest_sequence;
uint64_t delivered_sequence;
uint64_t audio_dma_event_wakes;
uint64_t extra_audio_event_wakes;
uint64_t audio_frames_from_driver;
uint64_t audio_bytes_from_driver;
int get_frame_zero_copy;
uint64_t get_frame_timing_samples;
double get_frame_timing_average_us;
double get_frame_timing_max300_us;
double get_frame_timing_max_us;
} pcies2mm_debug_state_t;
typedef enum pcies2mm_event_type_t
{
PCIES2MM_EVENT_PLUG_IN = 1,
PCIES2MM_EVENT_PLUG_OUT = 2,
PCIES2MM_EVENT_STREAM_READY = 3,
PCIES2MM_EVENT_FORMAT_CHANGE_BEGIN = 4
} pcies2mm_event_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_STREAM_READY |
PCIES2MM_EVENT_MASK_FORMAT_CHANGE_BEGIN
};
typedef void (*pcies2mm_event_callback_t)(pcies2mm_event_type_t event, void *user);
@@ -18,80 +18,80 @@ uint64_t monotonic_ns()
return static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(now).count());
}
pcies2mm_pixel_format_t decode_fourcc(uint32_t fourcc)
gigabyte_pixel_format_t decode_fourcc(uint32_t fourcc)
{
if (fourcc == kFourccY210)
return PCIES2MM_PIXFMT_Y210;
return GIGABYTE_PIXFMT_Y210;
if (fourcc == kFourccYuy2 || fourcc == kFourccYuyv || fourcc == kFourccUyvy)
return PCIES2MM_PIXFMT_YUY2;
return PCIES2MM_PIXFMT_UNKNOWN;
return GIGABYTE_PIXFMT_YUY2;
return GIGABYTE_PIXFMT_UNKNOWN;
}
uint32_t bit_depth(pcies2mm_pixel_format_t format)
uint32_t bit_depth(gigabyte_pixel_format_t format)
{
return format == PCIES2MM_PIXFMT_Y210 ? 10u :
format == PCIES2MM_PIXFMT_YUY2 ? 8u : 0u;
return format == GIGABYTE_PIXFMT_Y210 ? 10u :
format == GIGABYTE_PIXFMT_YUY2 ? 8u : 0u;
}
}
namespace gvfg::internal
{
PcieS2mmCaptureSession::PcieS2mmCaptureSession(ChannelErrorState &errorState)
GigabyteCaptureSession::GigabyteCaptureSession(ChannelErrorState &errorState)
: error_state_(errorState)
{
}
PcieS2mmCaptureSession::~PcieS2mmCaptureSession()
GigabyteCaptureSession::~GigabyteCaptureSession()
{
close();
}
pcies2mm_status_t PcieS2mmCaptureSession::open_device_index(size_t deviceIndex)
gigabyte_status_t GigabyteCaptureSession::open_device_index(size_t deviceIndex)
{
const auto devices = enumerate_gigabyte_devices();
if (deviceIndex >= devices.size())
return reject(PCIES2MM_ENODEV, "GigabyteLib open rejected: device index is unavailable");
return reject(GIGABYTE_ENODEV, "GigabyteLib open rejected: device index is unavailable");
return open_device(devices[deviceIndex]);
}
pcies2mm_status_t PcieS2mmCaptureSession::open_device_connection(
const std::shared_ptr<PcieS2mmDeviceConnection> &connection)
gigabyte_status_t GigabyteCaptureSession::open_device_connection(
const std::shared_ptr<GigabyteDeviceConnection> &connection)
{
if (!connection || connection->interface_path.empty())
return reject(PCIES2MM_EINVAL, "GigabyteLib open rejected: device selection is invalid");
return reject(GIGABYTE_EINVAL, "GigabyteLib open rejected: device selection is invalid");
close();
device_connection_ = connection;
return PCIES2MM_OK;
return GIGABYTE_OK;
}
pcies2mm_status_t PcieS2mmCaptureSession::open_device(const GigabyteDevice &device)
gigabyte_status_t GigabyteCaptureSession::open_device(const GigabyteDevice &device)
{
close();
device_connection_ = std::make_shared<PcieS2mmDeviceConnection>();
device_connection_ = std::make_shared<GigabyteDeviceConnection>();
device_connection_->interface_path = device.interface_path;
return PCIES2MM_OK;
return GIGABYTE_OK;
}
pcies2mm_status_t PcieS2mmCaptureSession::ensure_vendor_channel_open() const
gigabyte_status_t GigabyteCaptureSession::ensure_vendor_channel_open() const
{
if (channel_open_)
return PCIES2MM_OK;
return GIGABYTE_OK;
if (!device_connection_ || device_connection_->interface_path.empty())
return reject(PCIES2MM_ESTATE, "GigabyteLib channel open rejected: no device is selected");
return reject(GIGABYTE_ESTATE, "GigabyteLib channel open rejected: no device is selected");
device_handle_ = CreateFileW(device_connection_->interface_path.c_str(),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (device_handle_ == INVALID_HANDLE_VALUE)
return reject(PCIES2MM_ENODEV, "CreateFile for GigabyteLib failed");
return reject(GIGABYTE_ENODEV, "CreateFile for GigabyteLib failed");
const ULONG memoryMode = zero_copy_enabled_
? GVFG_VIDEO_FRAME_ZERO_COPY | GVFG_AUDIO_FRAME_BUF_COPY
: GVFG_VIDEO_FRAME_BUF_COPY | GVFG_AUDIO_FRAME_BUF_COPY;
pcies2mm_status_t status = from_vendor(
gigabyte_status_t status = from_vendor(
GvfgOpenDev(device_handle_, &context_, memoryMode), "GvfgOpenDev");
if (status != PCIES2MM_OK)
if (status != GIGABYTE_OK)
{
CloseHandle(device_handle_);
device_handle_ = INVALID_HANDLE_VALUE;
@@ -101,7 +101,7 @@ pcies2mm_status_t PcieS2mmCaptureSession::ensure_vendor_channel_open() const
status = from_vendor(GvfgCreateEvents(&events_, audio_enabled_ ? FALSE : TRUE),
"GvfgCreateEvents");
if (status != PCIES2MM_OK)
if (status != GIGABYTE_OK)
{
GvfgCloseDev(&context_);
CloseHandle(device_handle_);
@@ -114,12 +114,12 @@ pcies2mm_status_t PcieS2mmCaptureSession::ensure_vendor_channel_open() const
if (!stop_event_)
{
close_vendor_channel();
return reject(PCIES2MM_EIO, "CreateEvent for GigabyteLib cancellation failed");
return reject(GIGABYTE_EIO, "CreateEvent for GigabyteLib cancellation failed");
}
status = from_vendor(GvfgOpenVideoChn(context_, channel_, &events_),
"GvfgOpenVideoChn");
if (status != PCIES2MM_OK)
if (status != GIGABYTE_OK)
{
close_vendor_channel();
return status;
@@ -128,7 +128,7 @@ pcies2mm_status_t PcieS2mmCaptureSession::ensure_vendor_channel_open() const
return refresh_video_info();
}
void PcieS2mmCaptureSession::close_vendor_channel() const
void GigabyteCaptureSession::close_vendor_channel() const
{
if (channel_open_ && context_)
GvfgCloseVideoChn(context_, channel_);
@@ -147,76 +147,53 @@ void PcieS2mmCaptureSession::close_vendor_channel() const
device_handle_ = INVALID_HANDLE_VALUE;
}
pcies2mm_status_t PcieS2mmCaptureSession::close()
gigabyte_status_t GigabyteCaptureSession::close()
{
stop_stream();
close_vendor_channel();
device_connection_.reset();
configured_ = false;
return PCIES2MM_OK;
return GIGABYTE_OK;
}
pcies2mm_status_t PcieS2mmCaptureSession::set_channel(uint32_t channel)
gigabyte_status_t GigabyteCaptureSession::set_channel(uint32_t channel)
{
if (channel > 1)
return reject(PCIES2MM_EINVAL, "GigabyteLib channel index is invalid");
return reject(GIGABYTE_EINVAL, "GigabyteLib channel index is invalid");
if (channel_open_)
return reject(PCIES2MM_ESTATE, "GigabyteLib channel is already open");
return reject(GIGABYTE_ESTATE, "GigabyteLib channel is already open");
channel_ = channel;
return PCIES2MM_OK;
return GIGABYTE_OK;
}
pcies2mm_status_t PcieS2mmCaptureSession::set_zero_copy_enabled(bool enabled)
gigabyte_status_t GigabyteCaptureSession::set_zero_copy_enabled(bool enabled)
{
if (channel_open_ || running_)
return reject(PCIES2MM_ESTATE, "GigabyteLib frame memory mode is already fixed");
return reject(GIGABYTE_ESTATE, "GigabyteLib frame memory mode is already fixed");
zero_copy_enabled_ = enabled;
return PCIES2MM_OK;
return GIGABYTE_OK;
}
pcies2mm_status_t PcieS2mmCaptureSession::set_audio_enabled(bool enabled)
gigabyte_status_t GigabyteCaptureSession::set_audio_enabled(bool enabled)
{
if (running_)
return reject(PCIES2MM_ESTATE, "GigabyteLib audio mode cannot change while running");
return reject(GIGABYTE_ESTATE, "GigabyteLib audio mode cannot change while running");
if (channel_open_ && audio_enabled_ != enabled)
close_vendor_channel();
audio_enabled_ = enabled;
return PCIES2MM_OK;
return GIGABYTE_OK;
}
pcies2mm_status_t PcieS2mmCaptureSession::set_video_format(pcies2mm_pixel_format_t format)
{
if (format != PCIES2MM_PIXFMT_YUY2 && format != PCIES2MM_PIXFMT_Y210)
return reject(PCIES2MM_EINVAL, "GigabyteLib pixel format is invalid");
if (running_)
return reject(PCIES2MM_ESTATE, "GigabyteLib pixel format cannot change while running");
const pcies2mm_status_t openStatus = ensure_vendor_channel_open();
if (openStatus != PCIES2MM_OK)
return openStatus;
const pcies2mm_status_t infoStatus = refresh_video_info();
if (infoStatus != PCIES2MM_OK)
return infoStatus;
{
std::lock_guard<std::mutex> lock(state_mutex_);
if (cached_signal_.connected && cached_signal_.pixel_format != PCIES2MM_PIXFMT_UNKNOWN &&
cached_signal_.pixel_format != format)
return reject(PCIES2MM_ENOTSUP,
"GigabyteLib does not expose output-format conversion; requested format differs from input");
}
stream_desc_.pixel_format = format;
return PCIES2MM_OK;
}
pcies2mm_status_t PcieS2mmCaptureSession::refresh_video_info() const
gigabyte_status_t GigabyteCaptureSession::refresh_video_info() const
{
if (!context_)
return reject(PCIES2MM_ESTATE, "GigabyteLib video info requested before open");
return reject(GIGABYTE_ESTATE, "GigabyteLib video info requested before open");
GVFG_VIDEO_INFO info{};
const pcies2mm_status_t status = from_vendor(
const gigabyte_status_t status = from_vendor(
GvfgGetVideoInfo(context_, channel_, &info), "GvfgGetVideoInfo");
if (status != PCIES2MM_OK)
if (status != GIGABYTE_OK)
return status;
pcies2mm_signal_status_t signal{};
gigabyte_signal_status_t signal{};
signal.connected = info.VideoSignalLock ? 1 : 0;
signal.channel = channel_;
signal.width = info.Width;
@@ -227,16 +204,16 @@ pcies2mm_status_t PcieS2mmCaptureSession::refresh_video_info() const
std::lock_guard<std::mutex> lock(state_mutex_);
cached_signal_ = signal;
}
return PCIES2MM_OK;
return GIGABYTE_OK;
}
pcies2mm_status_t PcieS2mmCaptureSession::get_signal_status(pcies2mm_signal_status_t &out) const
gigabyte_status_t GigabyteCaptureSession::get_signal_status(gigabyte_signal_status_t &out) const
{
const pcies2mm_status_t openStatus = ensure_vendor_channel_open();
if (openStatus != PCIES2MM_OK)
const gigabyte_status_t openStatus = ensure_vendor_channel_open();
if (openStatus != GIGABYTE_OK)
return openStatus;
const pcies2mm_status_t status = refresh_video_info();
if (status == PCIES2MM_OK)
const gigabyte_status_t status = refresh_video_info();
if (status == GIGABYTE_OK)
{
std::lock_guard<std::mutex> lock(state_mutex_);
out = cached_signal_;
@@ -244,14 +221,14 @@ pcies2mm_status_t PcieS2mmCaptureSession::get_signal_status(pcies2mm_signal_stat
return status;
}
pcies2mm_status_t PcieS2mmCaptureSession::get_audio_format(pcies2mm_audio_format_t &out) const
gigabyte_status_t GigabyteCaptureSession::get_audio_format(gigabyte_audio_format_t &out) const
{
const pcies2mm_status_t openStatus = ensure_vendor_channel_open();
if (openStatus != PCIES2MM_OK)
const gigabyte_status_t openStatus = ensure_vendor_channel_open();
if (openStatus != GIGABYTE_OK)
return openStatus;
const pcies2mm_status_t status = from_vendor(
const gigabyte_status_t status = from_vendor(
GvfgGetAudioInfo(context_, channel_, &audio_info_), "GvfgGetAudioInfo");
if (status != PCIES2MM_OK)
if (status != GIGABYTE_OK)
return status;
out = {};
out.sample_rate = audio_info_.SamplesPerSec;
@@ -261,52 +238,40 @@ pcies2mm_status_t PcieS2mmCaptureSession::get_audio_format(pcies2mm_audio_format
out.frame_bytes = audio_info_.cbBufSize;
out.block_align = static_cast<uint32_t>(audio_info_.Channels) *
static_cast<uint32_t>(audio_info_.BitsPerSample) / 8u;
return PCIES2MM_OK;
return GIGABYTE_OK;
}
pcies2mm_status_t PcieS2mmCaptureSession::set_event_callback(
pcies2mm_event_callback_t callback, void *user, uint32_t eventMask)
gigabyte_status_t GigabyteCaptureSession::set_event_callback(
gigabyte_event_callback_t callback, void *user, uint32_t eventMask)
{
std::lock_guard<std::mutex> lock(callback_mutex_);
event_callback_ = callback;
event_callback_user_ = user;
event_mask_filter_ = eventMask;
return PCIES2MM_OK;
return GIGABYTE_OK;
}
pcies2mm_status_t PcieS2mmCaptureSession::configure_stream(const pcies2mm_stream_desc_t &desc)
gigabyte_status_t GigabyteCaptureSession::configure_stream()
{
if (running_)
return reject(PCIES2MM_ESTATE, "GigabyteLib stream is already running");
const pcies2mm_status_t openStatus = ensure_vendor_channel_open();
if (openStatus != PCIES2MM_OK)
return reject(GIGABYTE_ESTATE, "GigabyteLib stream is already running");
const gigabyte_status_t openStatus = ensure_vendor_channel_open();
if (openStatus != GIGABYTE_OK)
return openStatus;
stream_desc_ = desc;
{
std::lock_guard<std::mutex> lock(state_mutex_);
if (cached_signal_.connected)
{
stream_desc_.width = cached_signal_.width;
stream_desc_.height = cached_signal_.height;
stream_desc_.pixel_format = cached_signal_.pixel_format;
}
}
if (!zero_copy_enabled_)
copy_buffer_.assign(frame_size_bytes(), 0);
configured_ = true;
return PCIES2MM_OK;
return GIGABYTE_OK;
}
pcies2mm_status_t PcieS2mmCaptureSession::start_stream()
gigabyte_status_t GigabyteCaptureSession::start_stream()
{
if (running_)
return PCIES2MM_OK;
return GIGABYTE_OK;
if (!configured_)
return reject(PCIES2MM_ESTATE, "GigabyteLib stream has not been configured");
return reject(GIGABYTE_ESTATE, "GigabyteLib stream has not been configured");
ResetEvent(stop_event_);
const pcies2mm_status_t status = from_vendor(
const gigabyte_status_t status = from_vendor(
GvfgStartCapture(context_, channel_), "GvfgStartCapture");
if (status != PCIES2MM_OK)
if (status != GIGABYTE_OK)
return status;
{
std::lock_guard<std::mutex> lock(state_mutex_);
@@ -316,17 +281,17 @@ pcies2mm_status_t PcieS2mmCaptureSession::start_stream()
}
running_ = true;
start_event_monitoring();
return PCIES2MM_OK;
return GIGABYTE_OK;
}
pcies2mm_status_t PcieS2mmCaptureSession::stop_stream()
gigabyte_status_t GigabyteCaptureSession::stop_stream()
{
const bool wasRunning = running_.exchange(false);
if (stop_event_)
SetEvent(stop_event_);
stop_event_monitoring();
pcies2mm_status_t result = PCIES2MM_OK;
gigabyte_status_t result = GIGABYTE_OK;
{
std::lock_guard<std::mutex> lock(state_mutex_);
if (frame_held_ && zero_copy_enabled_ && context_)
@@ -337,61 +302,61 @@ pcies2mm_status_t PcieS2mmCaptureSession::stop_stream()
}
if (wasRunning && channel_open_ && context_)
{
const pcies2mm_status_t stopStatus = from_vendor(
const gigabyte_status_t stopStatus = from_vendor(
GvfgStopCapture(context_, channel_), "GvfgStopCapture");
if (result == PCIES2MM_OK)
if (result == GIGABYTE_OK)
result = stopStatus;
}
return result;
}
pcies2mm_status_t PcieS2mmCaptureSession::wait_frame(uint32_t timeoutMs, pcies2mm_frame_t &out)
gigabyte_status_t GigabyteCaptureSession::wait_frame(uint32_t timeoutMs, gigabyte_frame_t &out)
{
out = {};
if (!running_ || !context_)
return reject(PCIES2MM_ESTATE, "GigabyteLib frame read rejected: stream is not running");
return reject(GIGABYTE_ESTATE, "GigabyteLib frame read rejected: stream is not running");
{
std::lock_guard<std::mutex> lock(state_mutex_);
if (frame_held_)
return reject(PCIES2MM_ESTATE, "GigabyteLib frame read rejected: previous frame is held");
return reject(GIGABYTE_ESTATE, "GigabyteLib frame read rejected: previous frame is held");
}
std::array<HANDLE, 3> handles{stop_event_, events_.hVideoFrameInEvent, events_.hVideoExtraFrame};
const DWORD count = handles[2] ? 3u : 2u;
if (!handles[1])
return reject(PCIES2MM_EIO, "GigabyteLib did not provide a video-frame event");
return reject(GIGABYTE_EIO, "GigabyteLib did not provide a video-frame event");
const DWORD waitMs = timeoutMs == UINT32_MAX ? INFINITE : timeoutMs;
const DWORD wait = WaitForMultipleObjects(count, handles.data(), FALSE, waitMs);
if (wait == WAIT_TIMEOUT)
{
std::lock_guard<std::mutex> lock(state_mutex_);
++wait_timeout_count_;
return PCIES2MM_ETIMEOUT;
return GIGABYTE_ETIMEOUT;
}
if (wait == WAIT_OBJECT_0)
return PCIES2MM_ESTATE;
return GIGABYTE_ESTATE;
if (wait != WAIT_OBJECT_0 + 1 && wait != WAIT_OBJECT_0 + 2)
return reject(PCIES2MM_EIO, "GigabyteLib video event wait failed");
return reject(GIGABYTE_EIO, "GigabyteLib video event wait failed");
{
std::lock_guard<std::mutex> lock(state_mutex_);
++video_event_wakes_;
}
const auto begin = std::chrono::steady_clock::now();
const pcies2mm_status_t infoStatus = refresh_video_info();
if (infoStatus != PCIES2MM_OK)
const gigabyte_status_t infoStatus = refresh_video_info();
if (infoStatus != GIGABYTE_OK)
return infoStatus;
pcies2mm_signal_status_t signal{};
gigabyte_signal_status_t signal{};
{
std::lock_guard<std::mutex> lock(state_mutex_);
signal = cached_signal_;
}
if (!signal.connected || signal.width == 0 || signal.height == 0)
return PCIES2MM_ETIMEOUT;
return GIGABYTE_ETIMEOUT;
const size_t bytes = frame_size_bytes();
const void *data = nullptr;
pcies2mm_status_t status = PCIES2MM_OK;
gigabyte_status_t status = GIGABYTE_OK;
if (zero_copy_enabled_)
{
void *buffer = nullptr;
@@ -410,10 +375,10 @@ pcies2mm_status_t PcieS2mmCaptureSession::wait_frame(uint32_t timeoutMs, pcies2m
}
const auto end = std::chrono::steady_clock::now();
record_get_frame_timing(std::chrono::duration<double, std::micro>(end - begin).count());
if (status != PCIES2MM_OK)
if (status != GIGABYTE_OK)
return status;
if (!data || bytes == 0)
return reject(PCIES2MM_EIO, "GigabyteLib returned an invalid video frame");
return reject(GIGABYTE_EIO, "GigabyteLib returned an invalid video frame");
std::lock_guard<std::mutex> lock(state_mutex_);
out.data = data;
@@ -426,43 +391,43 @@ pcies2mm_status_t PcieS2mmCaptureSession::wait_frame(uint32_t timeoutMs, pcies2m
out.bit_depth = signal.bit_depth;
held_frame_ = out;
frame_held_ = true;
return PCIES2MM_OK;
return GIGABYTE_OK;
}
pcies2mm_status_t PcieS2mmCaptureSession::release_frame(const pcies2mm_frame_t &frame)
gigabyte_status_t GigabyteCaptureSession::release_frame(const gigabyte_frame_t &frame)
{
std::lock_guard<std::mutex> lock(state_mutex_);
if (!frame_held_)
return reject(PCIES2MM_ESTATE, "GigabyteLib frame release rejected: no frame is held");
return reject(GIGABYTE_ESTATE, "GigabyteLib frame release rejected: no frame is held");
if (frame.data != held_frame_.data || frame.frame_id != held_frame_.frame_id ||
frame.data_size_bytes != held_frame_.data_size_bytes)
return reject(PCIES2MM_EINVAL, "GigabyteLib frame release token does not match");
return reject(GIGABYTE_EINVAL, "GigabyteLib frame release token does not match");
if (zero_copy_enabled_)
{
const pcies2mm_status_t status = from_vendor(
const gigabyte_status_t status = from_vendor(
GvfgReleaseVideoFrameZeroCopy(context_, channel_), "GvfgReleaseVideoFrameZeroCopy");
if (status != PCIES2MM_OK)
if (status != GIGABYTE_OK)
return status;
}
held_frame_ = {};
frame_held_ = false;
return PCIES2MM_OK;
return GIGABYTE_OK;
}
pcies2mm_status_t PcieS2mmCaptureSession::wait_audio(
gigabyte_status_t GigabyteCaptureSession::wait_audio(
uint32_t timeoutMs, void *destination, uint32_t destinationCapacity, uint32_t &outBytes)
{
outBytes = 0;
if (!running_ || !audio_enabled_ || !destination)
return reject(PCIES2MM_ESTATE, "GigabyteLib audio read rejected");
pcies2mm_audio_format_t format{};
const pcies2mm_status_t formatStatus = get_audio_format(format);
if (formatStatus != PCIES2MM_OK)
return reject(GIGABYTE_ESTATE, "GigabyteLib audio read rejected");
gigabyte_audio_format_t format{};
const gigabyte_status_t formatStatus = get_audio_format(format);
if (formatStatus != GIGABYTE_OK)
return formatStatus;
if (format.frame_bytes == 0 || format.frame_bytes > destinationCapacity)
return reject(PCIES2MM_EINVAL, "GigabyteLib audio destination is too small");
return reject(GIGABYTE_EINVAL, "GigabyteLib audio destination is too small");
if (!events_.hAudioFrameInEvent)
return reject(PCIES2MM_EIO, "GigabyteLib did not provide an audio-frame event");
return reject(GIGABYTE_EIO, "GigabyteLib did not provide an audio-frame event");
std::array<HANDLE, 3> handles{stop_event_, events_.hAudioFrameInEvent, events_.hAudioExtraFrame};
const DWORD count = handles[2] ? 3u : 2u;
@@ -472,12 +437,12 @@ pcies2mm_status_t PcieS2mmCaptureSession::wait_audio(
{
std::lock_guard<std::mutex> lock(state_mutex_);
++wait_timeout_count_;
return PCIES2MM_ETIMEOUT;
return GIGABYTE_ETIMEOUT;
}
if (wait == WAIT_OBJECT_0)
return PCIES2MM_ESTATE;
return GIGABYTE_ESTATE;
if (wait != WAIT_OBJECT_0 + 1 && wait != WAIT_OBJECT_0 + 2)
return reject(PCIES2MM_EIO, "GigabyteLib audio event wait failed");
return reject(GIGABYTE_EIO, "GigabyteLib audio event wait failed");
{
std::lock_guard<std::mutex> lock(state_mutex_);
if (wait == WAIT_OBJECT_0 + 1)
@@ -485,9 +450,9 @@ pcies2mm_status_t PcieS2mmCaptureSession::wait_audio(
else
++extra_audio_event_wakes_;
}
const pcies2mm_status_t status = from_vendor(
const gigabyte_status_t status = from_vendor(
GvfgGetAudioFrame(context_, channel_, destination, format.frame_bytes), "GvfgGetAudioFrame");
if (status != PCIES2MM_OK)
if (status != GIGABYTE_OK)
return status;
outBytes = format.frame_bytes;
{
@@ -495,18 +460,18 @@ pcies2mm_status_t PcieS2mmCaptureSession::wait_audio(
++audio_frames_from_driver_;
audio_bytes_from_driver_ += outBytes;
}
return PCIES2MM_OK;
return GIGABYTE_OK;
}
void PcieS2mmCaptureSession::start_event_monitoring()
void GigabyteCaptureSession::start_event_monitoring()
{
if (monitoring_)
return;
monitoring_ = true;
event_thread_ = std::thread(&PcieS2mmCaptureSession::event_thread_proc, this);
event_thread_ = std::thread(&GigabyteCaptureSession::event_thread_proc, this);
}
void PcieS2mmCaptureSession::stop_event_monitoring()
void GigabyteCaptureSession::stop_event_monitoring()
{
monitoring_ = false;
if (stop_event_)
@@ -515,7 +480,7 @@ void PcieS2mmCaptureSession::stop_event_monitoring()
event_thread_.join();
}
void PcieS2mmCaptureSession::event_thread_proc()
void GigabyteCaptureSession::event_thread_proc()
{
const std::array<HANDLE, 4> handles{stop_event_, events_.hVideoFormatChangedEvent,
events_.hVideoInputPluginEvent, events_.hVideoInputUnplugEvent};
@@ -543,15 +508,15 @@ void PcieS2mmCaptureSession::event_thread_proc()
return;
if (kind == 1)
{
emit_event(PCIES2MM_EVENT_FORMAT_CHANGE_BEGIN);
emit_event(GIGABYTE_EVENT_FORMAT_CHANGE_BEGIN);
refresh_video_info();
emit_event(PCIES2MM_EVENT_STREAM_READY);
emit_event(GIGABYTE_EVENT_STREAM_READY);
}
else if (kind == 2)
{
refresh_video_info();
emit_event(PCIES2MM_EVENT_PLUG_IN);
emit_event(PCIES2MM_EVENT_STREAM_READY);
emit_event(GIGABYTE_EVENT_PLUG_IN);
emit_event(GIGABYTE_EVENT_STREAM_READY);
}
else if (kind == 3)
{
@@ -560,31 +525,31 @@ void PcieS2mmCaptureSession::event_thread_proc()
cached_signal_ = {};
cached_signal_.channel = channel_;
}
emit_event(PCIES2MM_EVENT_PLUG_OUT);
emit_event(GIGABYTE_EVENT_PLUG_OUT);
}
}
}
void PcieS2mmCaptureSession::emit_event(pcies2mm_event_type_t type) const
void GigabyteCaptureSession::emit_event(gigabyte_event_type_t type) const
{
std::lock_guard<std::mutex> lock(callback_mutex_);
uint32_t bit = 0;
if (type == PCIES2MM_EVENT_PLUG_IN) bit = PCIES2MM_EVENT_MASK_PLUG_IN;
if (type == PCIES2MM_EVENT_PLUG_OUT) bit = PCIES2MM_EVENT_MASK_PLUG_OUT;
if (type == PCIES2MM_EVENT_STREAM_READY) bit = PCIES2MM_EVENT_MASK_STREAM_READY;
if (type == PCIES2MM_EVENT_FORMAT_CHANGE_BEGIN) bit = PCIES2MM_EVENT_MASK_FORMAT_CHANGE_BEGIN;
if (type == GIGABYTE_EVENT_PLUG_IN) bit = GIGABYTE_EVENT_MASK_PLUG_IN;
if (type == GIGABYTE_EVENT_PLUG_OUT) bit = GIGABYTE_EVENT_MASK_PLUG_OUT;
if (type == GIGABYTE_EVENT_STREAM_READY) bit = GIGABYTE_EVENT_MASK_STREAM_READY;
if (type == GIGABYTE_EVENT_FORMAT_CHANGE_BEGIN) bit = GIGABYTE_EVENT_MASK_FORMAT_CHANGE_BEGIN;
if (event_callback_ && (event_mask_filter_ & bit) != 0)
event_callback_(type, event_callback_user_);
}
void PcieS2mmCaptureSession::get_debug_stats(
pcies2mm_stream_stats_t &outStats, uint64_t &outWaitTimeouts,
pcies2mm_debug_state_t &outDebugState) const
void GigabyteCaptureSession::get_debug_stats(
gigabyte_stream_stats_t &outStats, uint64_t &outWaitTimeouts,
gigabyte_debug_state_t &outDebugState) const
{
std::lock_guard<std::mutex> lock(state_mutex_);
outStats = {};
outStats.state = running_ ? PCIES2MM_STREAM_RUNNING :
configured_ ? PCIES2MM_STREAM_CONFIGURED : PCIES2MM_STREAM_STOPPED;
outStats.state = running_ ? GIGABYTE_STREAM_RUNNING :
configured_ ? GIGABYTE_STREAM_CONFIGURED : GIGABYTE_STREAM_STOPPED;
outStats.frames_captured = video_event_wakes_;
outStats.frames_delivered = frame_id_;
outStats.interrupt_count = video_event_wakes_;
@@ -606,27 +571,17 @@ void PcieS2mmCaptureSession::get_debug_stats(
outDebugState.get_frame_timing_max_us = get_frame_timing_lifetime_max_us_;
}
pcies2mm_status_t PcieS2mmCaptureSession::debug_read_register(uint32_t, uint32_t &) const
{
return reject(PCIES2MM_ENOTSUP, "GigabyteLib does not expose direct register reads");
}
pcies2mm_status_t PcieS2mmCaptureSession::debug_write_register(uint32_t, uint32_t) const
{
return reject(PCIES2MM_ENOTSUP, "GigabyteLib does not expose direct register writes");
}
pcies2mm_status_t PcieS2mmCaptureSession::from_vendor(GVFG_HRESULT result, const char *operation) const
gigabyte_status_t GigabyteCaptureSession::from_vendor(GVFG_HRESULT result, const char *operation) const
{
if (result == GVFG_HRESULT_OK)
return PCIES2MM_OK;
pcies2mm_status_t status = PCIES2MM_EIO;
return GIGABYTE_OK;
gigabyte_status_t status = GIGABYTE_EIO;
if (result == GVFG_HRESULT_DEV_BUSY || result == GVFG_HRESULT_CONTEXT_ERROR)
status = PCIES2MM_ESTATE;
status = GIGABYTE_ESTATE;
else if (result == GVFG_HRESULT_VIDEO_CHN_INVALID)
status = PCIES2MM_EINVAL;
status = GIGABYTE_EINVAL;
else if (result == GVFG_HRESULT_DEV_ERROR)
status = PCIES2MM_ENODEV;
status = GIGABYTE_ENODEV;
std::ostringstream stream;
stream << (operation ? operation : "GigabyteLib")
<< " failed with GVFG_HRESULT 0x" << std::hex << std::uppercase
@@ -635,24 +590,23 @@ pcies2mm_status_t PcieS2mmCaptureSession::from_vendor(GVFG_HRESULT result, const
return status;
}
pcies2mm_status_t PcieS2mmCaptureSession::reject(pcies2mm_status_t status, const char *message) const
gigabyte_status_t GigabyteCaptureSession::reject(gigabyte_status_t status, const char *message) const
{
error_state_.set(message ? message : "GigabyteLib operation rejected");
return status;
}
size_t PcieS2mmCaptureSession::frame_size_bytes() const
size_t GigabyteCaptureSession::frame_size_bytes() const
{
std::lock_guard<std::mutex> lock(state_mutex_);
const uint32_t width = cached_signal_.width ? cached_signal_.width : stream_desc_.width;
const uint32_t height = cached_signal_.height ? cached_signal_.height : stream_desc_.height;
const pcies2mm_pixel_format_t format = cached_signal_.pixel_format != PCIES2MM_PIXFMT_UNKNOWN
? cached_signal_.pixel_format : stream_desc_.pixel_format;
const size_t bytesPerPixel = format == PCIES2MM_PIXFMT_Y210 ? 4u : 2u;
const uint32_t width = cached_signal_.width;
const uint32_t height = cached_signal_.height;
const gigabyte_pixel_format_t format = cached_signal_.pixel_format;
const size_t bytesPerPixel = format == GIGABYTE_PIXFMT_Y210 ? 4u : 2u;
return static_cast<size_t>(width) * height * bytesPerPixel;
}
void PcieS2mmCaptureSession::record_get_frame_timing(double elapsedUs)
void GigabyteCaptureSession::record_get_frame_timing(double elapsedUs)
{
std::lock_guard<std::mutex> lock(state_mutex_);
++get_frame_timing_samples_;
@@ -1,6 +1,6 @@
#pragma once
#include "gigabyte_backend_types.h"
#include "gigabyte_types.h"
#include "gigabyte_device.h"
#include "gvfg_error_state.h"
#include "gvfgsdkapi.h"
@@ -18,67 +18,63 @@
namespace gvfg::internal
{
struct PcieS2mmDeviceConnection
struct GigabyteDeviceConnection
{
std::wstring interface_path;
};
class PcieS2mmCaptureSession
class GigabyteCaptureSession
{
public:
explicit PcieS2mmCaptureSession(ChannelErrorState &errorState);
PcieS2mmCaptureSession(const PcieS2mmCaptureSession &) = delete;
PcieS2mmCaptureSession &operator=(const PcieS2mmCaptureSession &) = delete;
~PcieS2mmCaptureSession();
explicit GigabyteCaptureSession(ChannelErrorState &errorState);
GigabyteCaptureSession(const GigabyteCaptureSession &) = delete;
GigabyteCaptureSession &operator=(const GigabyteCaptureSession &) = delete;
~GigabyteCaptureSession();
pcies2mm_status_t open_device_index(size_t deviceIndex);
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);
pcies2mm_status_t set_zero_copy_enabled(bool enabled);
gigabyte_status_t open_device_index(size_t deviceIndex);
gigabyte_status_t open_device_connection(const std::shared_ptr<GigabyteDeviceConnection> &connection);
std::shared_ptr<GigabyteDeviceConnection> device_connection() const { return device_connection_; }
gigabyte_status_t close();
gigabyte_status_t set_channel(uint32_t channel);
gigabyte_status_t set_zero_copy_enabled(bool enabled);
bool zero_copy_enabled() const { return zero_copy_enabled_; }
pcies2mm_status_t set_audio_enabled(bool enabled);
pcies2mm_status_t set_video_format(pcies2mm_pixel_format_t format);
pcies2mm_status_t get_signal_status(pcies2mm_signal_status_t &out) const;
pcies2mm_status_t get_audio_format(pcies2mm_audio_format_t &out) const;
pcies2mm_status_t set_event_callback(pcies2mm_event_callback_t callback, void *user, uint32_t eventMask);
pcies2mm_status_t configure_stream(const pcies2mm_stream_desc_t &desc);
pcies2mm_status_t start_stream();
pcies2mm_status_t stop_stream();
pcies2mm_status_t wait_frame(uint32_t timeoutMs, pcies2mm_frame_t &out);
pcies2mm_status_t wait_audio(uint32_t timeoutMs, void *destination,
gigabyte_status_t set_audio_enabled(bool enabled);
gigabyte_status_t get_signal_status(gigabyte_signal_status_t &out) const;
gigabyte_status_t get_audio_format(gigabyte_audio_format_t &out) const;
gigabyte_status_t set_event_callback(gigabyte_event_callback_t callback, void *user, uint32_t eventMask);
gigabyte_status_t configure_stream();
gigabyte_status_t start_stream();
gigabyte_status_t stop_stream();
gigabyte_status_t wait_frame(uint32_t timeoutMs, gigabyte_frame_t &out);
gigabyte_status_t wait_audio(uint32_t timeoutMs, void *destination,
uint32_t destinationCapacity, uint32_t &outBytes);
pcies2mm_status_t release_frame(const pcies2mm_frame_t &frame);
void get_debug_stats(pcies2mm_stream_stats_t &outStats, uint64_t &outWaitTimeouts,
pcies2mm_debug_state_t &outDebugState) const;
pcies2mm_status_t debug_read_register(uint32_t offset, uint32_t &outValue) const;
pcies2mm_status_t debug_write_register(uint32_t offset, uint32_t value) const;
gigabyte_status_t release_frame(const gigabyte_frame_t &frame);
void get_debug_stats(gigabyte_stream_stats_t &outStats, uint64_t &outWaitTimeouts,
gigabyte_debug_state_t &outDebugState) const;
private:
pcies2mm_status_t open_device(const GigabyteDevice &device);
pcies2mm_status_t ensure_vendor_channel_open() const;
gigabyte_status_t open_device(const GigabyteDevice &device);
gigabyte_status_t ensure_vendor_channel_open() const;
void close_vendor_channel() const;
void start_event_monitoring();
void stop_event_monitoring();
void event_thread_proc();
void emit_event(pcies2mm_event_type_t type) const;
pcies2mm_status_t refresh_video_info() const;
pcies2mm_status_t from_vendor(GVFG_HRESULT result, const char *operation) const;
pcies2mm_status_t reject(pcies2mm_status_t status, const char *message) const;
void emit_event(gigabyte_event_type_t type) const;
gigabyte_status_t refresh_video_info() const;
gigabyte_status_t from_vendor(GVFG_HRESULT result, const char *operation) const;
gigabyte_status_t reject(gigabyte_status_t status, const char *message) const;
void record_get_frame_timing(double elapsedUs);
size_t frame_size_bytes() const;
ChannelErrorState &error_state_;
std::shared_ptr<PcieS2mmDeviceConnection> device_connection_;
std::shared_ptr<GigabyteDeviceConnection> device_connection_;
mutable HANDLE device_handle_ = INVALID_HANDLE_VALUE;
mutable PGVFG_CONTEXT context_ = nullptr;
mutable GVFG_VIDEO_CHN_EVENT events_{};
mutable HANDLE stop_event_ = nullptr;
mutable bool events_created_ = false;
mutable bool channel_open_ = false;
pcies2mm_stream_desc_t stream_desc_{};
mutable pcies2mm_signal_status_t cached_signal_{};
mutable gigabyte_signal_status_t cached_signal_{};
mutable GVFG_AUDIO_INFO audio_info_{};
uint32_t channel_ = 0;
bool configured_ = false;
@@ -88,12 +84,12 @@ namespace gvfg::internal
std::atomic<bool> monitoring_{false};
std::thread event_thread_;
mutable std::mutex callback_mutex_;
pcies2mm_event_callback_t event_callback_ = nullptr;
gigabyte_event_callback_t event_callback_ = nullptr;
void *event_callback_user_ = nullptr;
uint32_t event_mask_filter_ = PCIES2MM_EVENT_MASK_DEFAULT;
uint32_t event_mask_filter_ = GIGABYTE_EVENT_MASK_DEFAULT;
mutable std::mutex state_mutex_;
std::vector<uint8_t> copy_buffer_;
pcies2mm_frame_t held_frame_{};
gigabyte_frame_t held_frame_{};
bool frame_held_ = false;
uint64_t frame_id_ = 0;
uint64_t audio_frames_from_driver_ = 0;
@@ -10,7 +10,7 @@
namespace
{
// Device interface exported by pcie_s2mm_driver.
const GUID kPcieS2mmDeviceInterfaceGuid =
const GUID kGigabyteDeviceInterfaceGuid =
{0x8c47b9c3, 0x1faa, 0x4557, {0xbc, 0x1d, 0xf2, 0x25, 0xd2, 0x6c, 0x9e, 0x91}};
std::wstring device_property(HDEVINFO info, SP_DEVINFO_DATA &deviceInfo, DWORD property)
@@ -34,14 +34,14 @@ namespace gvfg::internal
std::vector<GigabyteDevice> enumerate_gigabyte_devices()
{
std::vector<GigabyteDevice> devices;
HDEVINFO info = SetupDiGetClassDevsW(&kPcieS2mmDeviceInterfaceGuid, nullptr, nullptr,
HDEVINFO info = SetupDiGetClassDevsW(&kGigabyteDeviceInterfaceGuid, nullptr, nullptr,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (info == INVALID_HANDLE_VALUE)
return devices;
SP_DEVICE_INTERFACE_DATA iface = {};
iface.cbSize = sizeof(iface);
for (DWORD index = 0; SetupDiEnumDeviceInterfaces(info, nullptr, &kPcieS2mmDeviceInterfaceGuid, index, &iface); ++index)
for (DWORD index = 0; SetupDiEnumDeviceInterfaces(info, nullptr, &kGigabyteDeviceInterfaceGuid, index, &iface); ++index)
{
DWORD required = 0;
SetupDiGetDeviceInterfaceDetailW(info, &iface, nullptr, 0, &required, nullptr);
@@ -0,0 +1,109 @@
#pragma once
#include <stdint.h>
#include <stddef.h>
typedef enum gigabyte_status_t
{
GIGABYTE_OK = 0,
GIGABYTE_EINVAL,
GIGABYTE_ENODEV,
GIGABYTE_ESTATE,
GIGABYTE_ENOTSUP,
GIGABYTE_ETIMEOUT,
GIGABYTE_EIO
} gigabyte_status_t;
typedef enum gigabyte_pixel_format_t
{
GIGABYTE_PIXFMT_UNKNOWN = 0,
GIGABYTE_PIXFMT_Y210 = 2,
GIGABYTE_PIXFMT_YUY2 = 3
} gigabyte_pixel_format_t;
typedef enum gigabyte_stream_state_t
{
GIGABYTE_STREAM_STOPPED = 0,
GIGABYTE_STREAM_CONFIGURED = 1,
GIGABYTE_STREAM_RUNNING = 2
} gigabyte_stream_state_t;
typedef struct gigabyte_signal_status_t
{
int connected;
uint32_t channel;
uint32_t width;
uint32_t height;
gigabyte_pixel_format_t pixel_format;
uint32_t bit_depth;
} gigabyte_signal_status_t;
typedef struct gigabyte_frame_t
{
const void *data; /* Valid until the next GigabyteCaptureSession::wait_frame() or close(). */
size_t data_size_bytes;
uint64_t frame_id;
uint64_t timestamp_ns;
uint32_t width;
uint32_t height;
gigabyte_pixel_format_t pixel_format;
uint32_t bit_depth;
} gigabyte_frame_t;
typedef struct gigabyte_audio_format_t
{
uint32_t sample_rate;
uint32_t channels;
uint32_t bits_per_sample;
uint32_t frames_per_second;
uint32_t frame_bytes;
uint32_t block_align;
} gigabyte_audio_format_t;
typedef struct gigabyte_stream_stats_t
{
gigabyte_stream_state_t state;
uint64_t frames_captured;
uint64_t frames_delivered;
uint64_t dma_errors;
uint64_t interrupt_count;
} gigabyte_stream_stats_t;
typedef struct gigabyte_debug_state_t
{
int running;
int capture_active;
uint64_t latest_sequence;
uint64_t delivered_sequence;
uint64_t audio_dma_event_wakes;
uint64_t extra_audio_event_wakes;
uint64_t audio_frames_from_driver;
uint64_t audio_bytes_from_driver;
int get_frame_zero_copy;
uint64_t get_frame_timing_samples;
double get_frame_timing_average_us;
double get_frame_timing_max300_us;
double get_frame_timing_max_us;
} gigabyte_debug_state_t;
typedef enum gigabyte_event_type_t
{
GIGABYTE_EVENT_PLUG_IN = 1,
GIGABYTE_EVENT_PLUG_OUT = 2,
GIGABYTE_EVENT_STREAM_READY = 3,
GIGABYTE_EVENT_FORMAT_CHANGE_BEGIN = 4
} gigabyte_event_type_t;
enum
{
GIGABYTE_EVENT_MASK_PLUG_IN = 1u << 0,
GIGABYTE_EVENT_MASK_PLUG_OUT = 1u << 1,
GIGABYTE_EVENT_MASK_STREAM_READY = 1u << 2,
GIGABYTE_EVENT_MASK_FORMAT_CHANGE_BEGIN = 1u << 3,
GIGABYTE_EVENT_MASK_DEFAULT = GIGABYTE_EVENT_MASK_PLUG_IN |
GIGABYTE_EVENT_MASK_PLUG_OUT |
GIGABYTE_EVENT_MASK_STREAM_READY |
GIGABYTE_EVENT_MASK_FORMAT_CHANGE_BEGIN
};
typedef void (*gigabyte_event_callback_t)(gigabyte_event_type_t event, void *user);
+138 -274
View File
@@ -27,10 +27,10 @@ using namespace gvfg::internal;
namespace
{
static_assert(GVFG_EVENT_MASK_SIGNAL_CONNECTED == PCIES2MM_EVENT_MASK_PLUG_IN);
static_assert(GVFG_EVENT_MASK_SIGNAL_DISCONNECTED == PCIES2MM_EVENT_MASK_PLUG_OUT);
static_assert(GVFG_EVENT_MASK_STREAM_READY == PCIES2MM_EVENT_MASK_STREAM_READY);
static_assert(GVFG_EVENT_MASK_FORMAT_CHANGE_BEGIN == PCIES2MM_EVENT_MASK_FORMAT_CHANGE_BEGIN);
static_assert(GVFG_EVENT_MASK_SIGNAL_CONNECTED == GIGABYTE_EVENT_MASK_PLUG_IN);
static_assert(GVFG_EVENT_MASK_SIGNAL_DISCONNECTED == GIGABYTE_EVENT_MASK_PLUG_OUT);
static_assert(GVFG_EVENT_MASK_STREAM_READY == GIGABYTE_EVENT_MASK_STREAM_READY);
static_assert(GVFG_EVENT_MASK_FORMAT_CHANGE_BEGIN == GIGABYTE_EVENT_MASK_FORMAT_CHANGE_BEGIN);
#if INTPTR_MAX == INT64_MAX
static_assert(sizeof(gvfg_audio_format_t) == 12, "gvfg_audio_format_t ABI must remain frozen");
@@ -70,23 +70,23 @@ namespace
return static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(now).count());
}
gvfg_status_t map_status(pcies2mm_status_t st)
gvfg_status_t map_status(gigabyte_status_t st)
{
switch (st)
{
case PCIES2MM_OK:
case GIGABYTE_OK:
return GVFG_OK;
case PCIES2MM_EINVAL:
case GIGABYTE_EINVAL:
return GVFG_EINVAL;
case PCIES2MM_ENODEV:
case GIGABYTE_ENODEV:
return GVFG_ENODEV;
case PCIES2MM_ESTATE:
case GIGABYTE_ESTATE:
return GVFG_ESTATE;
case PCIES2MM_ETIMEOUT:
case GIGABYTE_ETIMEOUT:
return GVFG_ETIMEOUT;
case PCIES2MM_ENOTSUP:
case GIGABYTE_ENOTSUP:
return GVFG_ENOTSUP;
case PCIES2MM_EIO:
case GIGABYTE_EIO:
default:
return GVFG_EIO;
}
@@ -103,30 +103,30 @@ namespace
dst[dstSize - 1] = '\0';
}
gvfg_event_type_t map_event_type(pcies2mm_event_type_t type)
gvfg_event_type_t map_event_type(gigabyte_event_type_t type)
{
switch (type)
{
case PCIES2MM_EVENT_PLUG_IN:
case GIGABYTE_EVENT_PLUG_IN:
return GVFG_EVENT_SIGNAL_CONNECTED;
case PCIES2MM_EVENT_PLUG_OUT:
case GIGABYTE_EVENT_PLUG_OUT:
return GVFG_EVENT_SIGNAL_DISCONNECTED;
case PCIES2MM_EVENT_STREAM_READY:
case GIGABYTE_EVENT_STREAM_READY:
return GVFG_EVENT_STREAM_READY;
case PCIES2MM_EVENT_FORMAT_CHANGE_BEGIN:
case GIGABYTE_EVENT_FORMAT_CHANGE_BEGIN:
return GVFG_EVENT_FORMAT_CHANGE_BEGIN;
default:
return GVFG_EVENT_UNKNOWN;
}
}
int to_gvfg_pixel_format(pcies2mm_pixel_format_t fmt)
int to_gvfg_pixel_format(gigabyte_pixel_format_t fmt)
{
switch (fmt)
{
case PCIES2MM_PIXFMT_YUY2:
case GIGABYTE_PIXFMT_YUY2:
return GVFG_PIXFMT_YUY2;
case PCIES2MM_PIXFMT_Y210:
case GIGABYTE_PIXFMT_Y210:
return GVFG_PIXFMT_Y210;
default:
return GVFG_PIXFMT_UNKNOWN;
@@ -155,13 +155,13 @@ namespace
}
gvfg_status_t native_row_stride_bytes(uint32_t width,
pcies2mm_pixel_format_t pixelFormat,
gigabyte_pixel_format_t pixelFormat,
int &outStride)
{
uint64_t bytesPerPixel = 0;
if (pixelFormat == PCIES2MM_PIXFMT_YUY2)
if (pixelFormat == GIGABYTE_PIXFMT_YUY2)
bytesPerPixel = 2;
else if (pixelFormat == PCIES2MM_PIXFMT_Y210)
else if (pixelFormat == GIGABYTE_PIXFMT_Y210)
bytesPerPixel = 4;
else
return GVFG_ENOTSUP;
@@ -191,28 +191,28 @@ struct gvfg_channel_session_t
gvfg_status_t open(int index,
int channelIndex = GVFG_CHANNEL_0,
const std::shared_ptr<gvfg::internal::PcieS2mmDeviceConnection> &deviceConnection = nullptr)
const std::shared_ptr<gvfg::internal::GigabyteDeviceConnection> &deviceConnection = nullptr)
{
if (index < 0 || (channelIndex != GVFG_CHANNEL_0 && channelIndex != GVFG_CHANNEL_1))
return reject(GVFG_EINVAL, "gvfg_open_channel rejected: device or channel index is invalid");
close();
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)
session = std::make_unique<gvfg::internal::GigabyteCaptureSession>(errorState);
const gigabyte_status_t stOpen = deviceConnection
? session->open_device_connection(deviceConnection)
: session->open_device_index(static_cast<size_t>(index));
if (stOpen != GIGABYTE_OK)
{
backend.reset();
session.reset();
return map_status(stOpen);
}
currentIndex = index;
syncBackendEventCallback();
syncSessionEventCallback();
selectedChannel = static_cast<uint32_t>(channelIndex);
resetRuntimeCounters();
const pcies2mm_status_t stChannel = backend->set_channel(selectedChannel);
if (stChannel != PCIES2MM_OK)
const gigabyte_status_t stChannel = session->set_channel(selectedChannel);
if (stChannel != GIGABYTE_OK)
{
close();
return map_status(stChannel);
@@ -220,8 +220,8 @@ struct gvfg_channel_session_t
if (zeroCopyRequested)
{
const pcies2mm_status_t stZeroCopy = backend->set_zero_copy_enabled(true);
if (stZeroCopy != PCIES2MM_OK)
const gigabyte_status_t stZeroCopy = session->set_zero_copy_enabled(true);
if (stZeroCopy != GIGABYTE_OK)
{
close();
return map_status(stZeroCopy);
@@ -234,7 +234,7 @@ struct gvfg_channel_session_t
gvfg_status_t start()
{
if (!backend)
if (!session)
return reject(GVFG_ESTATE, "gvfg_start_channel rejected: channel is not open");
// A new start request starts a new diagnostic lifetime. Any failure
@@ -259,8 +259,8 @@ struct gvfg_channel_session_t
eventQueue.clear();
}
const pcies2mm_status_t st = backend->start_stream();
if (st != PCIES2MM_OK)
const gigabyte_status_t st = session->start_stream();
if (st != GIGABYTE_OK)
return map_status(st);
running = true;
@@ -273,12 +273,12 @@ struct gvfg_channel_session_t
releaseHeldFrameForStop();
releaseHeldAudioFrameForStop();
pcies2mm_status_t st = PCIES2MM_OK;
if (backend)
gigabyte_status_t st = GIGABYTE_OK;
if (session)
{
// stop_stream() joins the backend event thread, so no new event can
// stop_stream() joins the session event thread, so no new event can
// be queued after it returns.
st = backend->stop_stream();
st = session->stop_stream();
}
{
@@ -292,21 +292,21 @@ struct gvfg_channel_session_t
void close()
{
stop();
if (backend)
if (session)
{
backend->set_event_callback(nullptr, nullptr, 0);
backend->close();
backend.reset();
session->set_event_callback(nullptr, nullptr, 0);
session->close();
session.reset();
}
currentIndex = -1;
}
gvfg_status_t getSignalStatus(gvfg_signal_status_t &out)
{
if (!backend)
if (!session)
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();
const gigabyte_status_t status = querySignal();
{
std::lock_guard<std::mutex> lock(stateMutex);
out.connected = signalConnected ? 1 : 0;
@@ -321,16 +321,16 @@ struct gvfg_channel_session_t
gvfg_status_t getRuntimeInfo(gvfg_runtime_info_t &out)
{
if (!backend)
if (!session)
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);
out.delivered_frames = deliveredFrames.load(std::memory_order_relaxed);
pcies2mm_stream_stats_t stats{};
pcies2mm_debug_state_t debugState{};
gigabyte_stream_stats_t stats{};
gigabyte_debug_state_t debugState{};
uint64_t waitTimeouts = 0;
backend->get_debug_stats(stats, waitTimeouts, debugState);
session->get_debug_stats(stats, waitTimeouts, debugState);
out.zero_copy_enabled = debugState.get_frame_zero_copy;
out.driver_read_samples = debugState.get_frame_timing_samples;
out.driver_read_average_us = debugState.get_frame_timing_average_us;
@@ -339,16 +339,16 @@ struct gvfg_channel_session_t
return GVFG_OK;
}
void syncBackendEventCallback()
void syncSessionEventCallback()
{
if (!backend)
if (!session)
return;
backend->set_event_callback(&gvfg_channel_session_t::onBackendEvent,
session->set_event_callback(&gvfg_channel_session_t::onSessionEvent,
this,
eventMask);
}
static void onBackendEvent(pcies2mm_event_type_t event, void *user)
static void onSessionEvent(gigabyte_event_type_t event, void *user)
{
auto *self = static_cast<gvfg_channel_session_t *>(user);
if (!self)
@@ -356,7 +356,7 @@ struct gvfg_channel_session_t
self->emitEvent(event);
}
void emitEvent(pcies2mm_event_type_t event)
void emitEvent(gigabyte_event_type_t event)
{
const gvfg_event_type_t type = map_event_type(event);
{
@@ -371,57 +371,34 @@ struct gvfg_channel_session_t
eventCv.notify_one();
}
pcies2mm_status_t querySignal()
gigabyte_status_t querySignal()
{
if (!backend)
return PCIES2MM_ESTATE;
if (!session)
return GIGABYTE_ESTATE;
pcies2mm_signal_status_t sig{};
const pcies2mm_status_t status = backend->get_signal_status(sig);
gigabyte_signal_status_t sig{};
const gigabyte_status_t status = session->get_signal_status(sig);
std::lock_guard<std::mutex> lock(stateMutex);
width = status == PCIES2MM_OK ? sig.width : 0;
height = status == PCIES2MM_OK ? sig.height : 0;
bitDepth = status == PCIES2MM_OK ? sig.bit_depth : 0;
pixelFormat = status == PCIES2MM_OK ? sig.pixel_format : PCIES2MM_PIXFMT_UNKNOWN;
signalConnected = status == PCIES2MM_OK && sig.connected != 0;
width = status == GIGABYTE_OK ? sig.width : 0;
height = status == GIGABYTE_OK ? sig.height : 0;
bitDepth = status == GIGABYTE_OK ? sig.bit_depth : 0;
pixelFormat = status == GIGABYTE_OK ? sig.pixel_format : GIGABYTE_PIXFMT_UNKNOWN;
signalConnected = status == GIGABYTE_OK && sig.connected != 0;
return status;
}
gvfg_status_t configureStream()
{
if (!backend)
if (!session)
return reject(GVFG_ESTATE, "configure stream rejected: channel is not open");
const pcies2mm_status_t signalStatus = querySignal();
if (signalStatus != PCIES2MM_OK)
const gigabyte_status_t signalStatus = querySignal();
if (signalStatus != GIGABYTE_OK)
return map_status(signalStatus);
bool waitingForSignal = false;
uint32_t configureWidth = 0;
uint32_t configureHeight = 0;
pcies2mm_pixel_format_t configureFormat = PCIES2MM_PIXFMT_UNKNOWN;
{
std::lock_guard<std::mutex> lock(stateMutex);
waitingForSignal = !signalConnected;
configureWidth = waitingForSignal ? 2 : width;
configureHeight = waitingForSignal ? 1 : height;
configureFormat =
waitingForSignal || pixelFormat == PCIES2MM_PIXFMT_UNKNOWN ? PCIES2MM_PIXFMT_YUY2 : pixelFormat;
}
// configure_stream() is also used to enter event-monitoring mode. Keep
// its inactive placeholder buffer minimal; the real signal descriptor
// replaces it before DMA is enabled after reconnect.
pcies2mm_stream_desc_t desc{};
desc.width = configureWidth;
desc.height = configureHeight;
desc.pixel_format = configureFormat;
desc.buffer_count = 1;
const pcies2mm_status_t st = backend->configure_stream(desc);
if (st != PCIES2MM_OK)
const gigabyte_status_t st = session->configure_stream();
if (st != GIGABYTE_OK)
return map_status(st);
return GVFG_OK;
}
@@ -429,7 +406,7 @@ struct gvfg_channel_session_t
gvfg_status_t readFrame(gvfg_frame_t &out, uint32_t timeoutMs)
{
std::memset(&out, 0, sizeof(out));
if (!backend)
if (!session)
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");
@@ -447,9 +424,9 @@ struct gvfg_channel_session_t
if (stateError)
return reject(GVFG_ESTATE, stateError);
pcies2mm_frame_t frame{};
const pcies2mm_status_t st = backend->wait_frame(timeoutMs, frame);
if (st != PCIES2MM_OK)
gigabyte_frame_t frame{};
const gigabyte_status_t st = session->wait_frame(timeoutMs, frame);
if (st != GIGABYTE_OK)
{
{
std::lock_guard<std::mutex> lock(frameMutex);
@@ -460,7 +437,7 @@ struct gvfg_channel_session_t
if (!frame.data || frame.width == 0 || frame.height == 0)
{
backend->release_frame(frame);
session->release_frame(frame);
std::lock_guard<std::mutex> lock(frameMutex);
readInProgress = false;
return reject(GVFG_EIO, "gvfg_read_channel_frame received an invalid frame descriptor");
@@ -476,14 +453,14 @@ struct gvfg_channel_session_t
}
else
{
heldBackendFrame = frame;
heldSessionFrame = frame;
readInProgress = false;
frameHeld = true;
}
}
if (stoppedWhileWaiting)
{
backend->release_frame(frame);
session->release_frame(frame);
return GVFG_ESTATE;
}
@@ -497,9 +474,9 @@ struct gvfg_channel_session_t
native_row_stride_bytes(frame.width, frame.pixel_format, out.row_stride_bytes);
if (strideStatus != GVFG_OK)
{
backend->release_frame(frame);
session->release_frame(frame);
std::lock_guard<std::mutex> lock(frameMutex);
heldBackendFrame = {};
heldSessionFrame = {};
frameHeld = false;
return reject(strideStatus, "gvfg_read_channel_frame received an invalid row stride");
}
@@ -514,7 +491,7 @@ struct gvfg_channel_session_t
gvfg_status_t releaseFrame(const gvfg_frame_t &frameToken)
{
if (!backend)
if (!session)
return reject(GVFG_ESTATE, "gvfg_release_channel_frame rejected: channel is not open");
{
@@ -523,27 +500,27 @@ struct gvfg_channel_session_t
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,
if (native_row_stride_bytes(heldSessionFrame.width,
heldSessionFrame.pixel_format,
expectedStride) != GVFG_OK)
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) ||
frameToken.width != static_cast<int>(heldBackendFrame.width) ||
frameToken.height != static_cast<int>(heldBackendFrame.height) ||
if (frameToken.data != heldSessionFrame.data ||
frameToken.data_size != static_cast<uint64_t>(heldSessionFrame.data_size_bytes) ||
frameToken.width != static_cast<int>(heldSessionFrame.width) ||
frameToken.height != static_cast<int>(heldSessionFrame.height) ||
frameToken.row_stride_bytes != expectedStride ||
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 ||
frameToken.timestamp_ns != heldBackendFrame.timestamp_ns)
frameToken.pixel_format != to_gvfg_pixel_format(heldSessionFrame.pixel_format) ||
frameToken.bit_depth != static_cast<int>(heldSessionFrame.bit_depth) ||
frameToken.frame_id != heldSessionFrame.frame_id ||
frameToken.timestamp_ns != heldSessionFrame.timestamp_ns)
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)
const gigabyte_status_t st = session->release_frame(heldSessionFrame);
if (st != GIGABYTE_OK)
return map_status(st);
heldBackendFrame = {};
heldSessionFrame = {};
frameHeld = false;
}
@@ -552,29 +529,29 @@ struct gvfg_channel_session_t
void releaseHeldFrameForStop()
{
if (!backend)
if (!session)
return;
pcies2mm_frame_t frame{};
gigabyte_frame_t frame{};
bool shouldRelease = false;
{
std::lock_guard<std::mutex> lock(frameMutex);
if (frameHeld)
{
frame = heldBackendFrame;
heldBackendFrame = {};
frame = heldSessionFrame;
heldSessionFrame = {};
frameHeld = false;
shouldRelease = true;
}
}
if (shouldRelease)
backend->release_frame(frame);
session->release_frame(frame);
}
gvfg_status_t pollEvent(gvfg_event_t &out, uint32_t timeoutMs)
{
if (!backend)
if (!session)
return reject(GVFG_ESTATE, "gvfg_poll_channel_event rejected: channel is not open");
std::unique_lock<std::mutex> lock(eventMutex);
@@ -616,7 +593,7 @@ struct gvfg_channel_session_t
return GVFG_OK;
}
gvfg_status_t getDebugBackendStats(gvfg_debug_backend_stats_t &out)
gvfg_status_t getDebugStats(gvfg_debug_channel_stats_t &out)
{
std::memset(&out, 0, sizeof(out));
out.sdk_running = running.load(std::memory_order_relaxed) ? 1 : 0;
@@ -640,22 +617,22 @@ struct gvfg_channel_session_t
out.event_queue_depth = static_cast<uint32_t>(eventQueue.size());
}
if (backend)
if (session)
{
pcies2mm_stream_stats_t stats{};
pcies2mm_debug_state_t debugState{};
gigabyte_stream_stats_t stats{};
gigabyte_debug_state_t debugState{};
uint64_t waitTimeouts = 0;
backend->get_debug_stats(stats, waitTimeouts, debugState);
out.backend_state = static_cast<int>(stats.state);
out.backend_frames_captured = stats.frames_captured;
out.backend_frames_delivered = stats.frames_delivered;
out.backend_dma_errors = stats.dma_errors;
out.backend_interrupt_count = stats.interrupt_count;
out.backend_wait_timeouts = waitTimeouts;
out.backend_running = debugState.running;
out.backend_capture_active = debugState.capture_active;
out.backend_latest_sequence = debugState.latest_sequence;
out.backend_delivered_sequence = debugState.delivered_sequence;
session->get_debug_stats(stats, waitTimeouts, debugState);
out.session_state = static_cast<int>(stats.state);
out.session_frames_captured = stats.frames_captured;
out.session_frames_delivered = stats.frames_delivered;
out.session_dma_errors = stats.dma_errors;
out.session_interrupt_count = stats.interrupt_count;
out.session_wait_timeouts = waitTimeouts;
out.session_running = debugState.running;
out.session_capture_active = debugState.capture_active;
out.session_latest_sequence = debugState.latest_sequence;
out.session_delivered_sequence = debugState.delivered_sequence;
out.audio_dma_event_wakes = debugState.audio_dma_event_wakes;
out.extra_audio_event_wakes = debugState.extra_audio_event_wakes;
out.audio_frames_from_driver = debugState.audio_frames_from_driver;
@@ -670,38 +647,14 @@ struct gvfg_channel_session_t
return GVFG_OK;
}
gvfg_status_t debugReadRegister(uint32_t offset, uint32_t &outValue)
{
if (!backend)
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 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)
backendFormat = PCIES2MM_PIXFMT_YUY2;
else if (format == GVFG_PIXFMT_Y210)
backendFormat = PCIES2MM_PIXFMT_Y210;
else
return reject(GVFG_EINVAL, "gvfg_set_channel_video_format rejected: pixel format is invalid");
const pcies2mm_status_t status = backend->set_video_format(backendFormat);
return map_status(status);
}
gvfg_status_t setAudioEnabled(bool enabled)
{
if (!backend)
if (!session)
return reject(GVFG_ESTATE, "gvfg_set_channel_audio_enabled rejected: channel is not open");
if (running)
return reject(GVFG_ESTATE, "gvfg_set_channel_audio_enabled rejected: channel is running");
const pcies2mm_status_t status = backend->set_audio_enabled(enabled);
if (status != PCIES2MM_OK)
const gigabyte_status_t status = session->set_audio_enabled(enabled);
if (status != GIGABYTE_OK)
return map_status(status);
audioEnabled = enabled;
return GVFG_OK;
@@ -710,11 +663,11 @@ struct gvfg_channel_session_t
gvfg_status_t getAudioFormat(gvfg_audio_format_t &out)
{
std::memset(&out, 0, sizeof(out));
if (!backend)
if (!session)
return reject(GVFG_ESTATE, "gvfg_get_channel_audio_format rejected: channel is not open");
pcies2mm_audio_format_t format{};
const pcies2mm_status_t status = backend->get_audio_format(format);
if (status != PCIES2MM_OK)
gigabyte_audio_format_t format{};
const gigabyte_status_t status = session->get_audio_format(format);
if (status != GIGABYTE_OK)
return map_status(status);
out.sample_rate = format.sample_rate;
out.channels = format.channels;
@@ -725,7 +678,7 @@ struct gvfg_channel_session_t
gvfg_status_t readAudioFrame(gvfg_audio_frame_t &out, uint32_t timeoutMs)
{
std::memset(&out, 0, sizeof(out));
if (!backend)
if (!session)
return reject(GVFG_ESTATE, "gvfg_read_channel_audio_frame rejected: channel is not open");
if (!running || !audioEnabled)
return reject(GVFG_ESTATE, "gvfg_read_channel_audio_frame rejected: audio capture is not running");
@@ -739,20 +692,20 @@ struct gvfg_channel_session_t
audioReadInProgress = true;
}
pcies2mm_audio_format_t format{};
pcies2mm_status_t status = backend->get_audio_format(format);
gigabyte_audio_format_t format{};
gigabyte_status_t status = session->get_audio_format(format);
uint32_t bytes = 0;
if (status == PCIES2MM_OK)
if (status == GIGABYTE_OK)
{
audioBuffer.resize(format.frame_bytes);
status = backend->wait_audio(timeoutMs, audioBuffer.data(),
status = session->wait_audio(timeoutMs, audioBuffer.data(),
static_cast<uint32_t>(audioBuffer.size()), bytes);
}
{
std::lock_guard<std::mutex> lock(audioMutex);
audioReadInProgress = false;
if (status != PCIES2MM_OK)
if (status != GIGABYTE_OK)
return map_status(status);
if (!running.load(std::memory_order_acquire) || bytes == 0 || bytes > audioBuffer.size())
return reject(GVFG_ESTATE, "gvfg_read_channel_audio_frame completed after capture stopped or returned invalid data");
@@ -793,13 +746,6 @@ struct gvfg_channel_session_t
audioFrameHeld = false;
}
gvfg_status_t debugWriteRegister(uint32_t offset, uint32_t value)
{
if (!backend)
return reject(GVFG_ESTATE, "debug register write rejected: channel is not open");
return map_status(backend->debug_write_register(offset, value));
}
gvfg_status_t reject(gvfg_status_t status, const char *message)
{
errorState.set(message ? message : "GVFG operation rejected");
@@ -851,7 +797,7 @@ struct gvfg_channel_session_t
}
ChannelErrorState &errorState;
std::unique_ptr<gvfg::internal::PcieS2mmCaptureSession> backend;
std::unique_ptr<gvfg::internal::GigabyteCaptureSession> session;
int currentIndex = -1;
uint32_t selectedChannel = GVFG_CHANNEL_0;
bool zeroCopyRequested = false;
@@ -861,7 +807,7 @@ struct gvfg_channel_session_t
uint32_t width = 0;
uint32_t height = 0;
uint32_t bitDepth = 0;
pcies2mm_pixel_format_t pixelFormat = PCIES2MM_PIXFMT_UNKNOWN;
gigabyte_pixel_format_t pixelFormat = GIGABYTE_PIXFMT_UNKNOWN;
bool signalConnected = false;
mutable std::mutex stateMutex;
std::atomic<uint64_t> fpsWindowStartNs{0};
@@ -877,7 +823,7 @@ struct gvfg_channel_session_t
std::mutex frameMutex;
bool readInProgress = false;
bool frameHeld = false;
pcies2mm_frame_t heldBackendFrame{};
gigabyte_frame_t heldSessionFrame{};
std::mutex audioMutex;
std::vector<uint8_t> audioBuffer;
bool audioReadInProgress = false;
@@ -900,13 +846,6 @@ struct gvfg_handle_t
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
@@ -927,13 +866,6 @@ struct gvfg_handle_t
return channels[static_cast<size_t>(channelIndex)].get();
}
gvfg_channel_session_t *findOpenChannel()
{
if (channels[GVFG_CHANNEL_0])
return channels[GVFG_CHANNEL_0].get();
return channels[GVFG_CHANNEL_1].get();
}
gvfg_status_t openChannel(int index, int channelIndex)
{
if (channelIndex != GVFG_CHANNEL_0 && channelIndex != GVFG_CHANNEL_1)
@@ -965,7 +897,7 @@ struct gvfg_handle_t
if (!deviceConnection)
{
deviceConnection = channel->backend->device_connection();
deviceConnection = channel->session->device_connection();
}
channels[slot] = std::move(channel);
currentIndex = index;
@@ -1008,31 +940,6 @@ struct gvfg_handle_t
return GVFG_OK;
}
gvfg_status_t setChannelVideoFormat(int channelIndex, gvfg_pixel_format_t format)
{
gvfg_channel_session_t *channel = findChannel(channelIndex);
if (!channel)
return rejectChannel(channelIndex,
GVFG_ESTATE,
"gvfg_set_channel_video_format rejected: channel is not open");
if (format != GVFG_PIXFMT_YUY2 && format != GVFG_PIXFMT_Y210)
return rejectChannel(channelIndex,
GVFG_EINVAL,
"gvfg_set_channel_video_format rejected: pixel format is invalid");
const int otherChannel = channelIndex == GVFG_CHANNEL_0 ? GVFG_CHANNEL_1 : GVFG_CHANNEL_0;
if (format == GVFG_PIXFMT_Y210 &&
requestedFormats[static_cast<size_t>(otherChannel)] == GVFG_PIXFMT_Y210)
return rejectChannel(channelIndex,
GVFG_ENOTSUP,
"both channels cannot use Y210 at the same time");
const gvfg_status_t status = channel->setVideoFormat(format);
if (status == GVFG_OK)
requestedFormats[static_cast<size_t>(channelIndex)] = format;
return status;
}
gvfg_status_t stopAll()
{
gvfg_status_t result = GVFG_OK;
@@ -1047,29 +954,12 @@ struct gvfg_handle_t
return result;
}
gvfg_status_t debugReadRegister(uint32_t offset, uint32_t &outValue)
{
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 = 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::shared_ptr<gvfg::internal::GigabyteDeviceConnection> 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;
std::array<bool, 2> zeroCopyRequested{false, false};
std::array<gvfg_pixel_format_t, 2> requestedFormats{GVFG_PIXFMT_YUY2, GVFG_PIXFMT_YUY2};
};
extern "C"
@@ -1190,15 +1080,6 @@ extern "C"
"gvfg_start_channel rejected: channel is not open");
}
gvfg_status_t gvfg_set_channel_video_format(gvfg_handle handle,
int channel_index,
gvfg_pixel_format_t format)
{
if (!handle)
return GVFG_EINVAL;
return handle->setChannelVideoFormat(channel_index, format);
}
gvfg_status_t gvfg_set_channel_audio_enabled(gvfg_handle handle,
int channel_index,
int enabled)
@@ -1416,9 +1297,9 @@ extern "C"
return handle->copyChannelError(channel_index, out_message, out_message_size);
}
gvfg_status_t gvfg_debug_get_channel_backend_stats(gvfg_handle handle,
gvfg_status_t gvfg_debug_get_channel_stats(gvfg_handle handle,
int channel_index,
gvfg_debug_backend_stats_t *out_stats)
gvfg_debug_channel_stats_t *out_stats)
{
if (!handle || !out_stats)
return GVFG_EINVAL;
@@ -1426,8 +1307,8 @@ extern "C"
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
if (!channel)
return GVFG_ESTATE;
gvfg_debug_backend_stats_t stats{};
const gvfg_status_t status = channel->getDebugBackendStats(stats);
gvfg_debug_channel_stats_t stats{};
const gvfg_status_t status = channel->getDebugStats(stats);
if (status != GVFG_OK)
return status;
@@ -1435,21 +1316,4 @@ extern "C"
return GVFG_OK;
}
gvfg_status_t gvfg_debug_read_register(gvfg_handle handle,
uint32_t offset,
uint32_t *out_value)
{
if (!handle || !out_value || (offset & 0x3u) != 0)
return GVFG_EINVAL;
return handle->debugReadRegister(offset, *out_value);
}
gvfg_status_t gvfg_debug_write_register(gvfg_handle handle,
uint32_t offset,
uint32_t value)
{
if (!handle || (offset & 0x3u) != 0)
return GVFG_EINVAL;
return handle->debugWriteRegister(offset, value);
}
}