Remove the redundant Gigabyte type layer

Compared with 9bc38c4:

- delete the duplicate gigabyte_types.h status, frame, signal, format, event, and debug model

- let GigabyteCaptureSession use the stable public gvfg types and status codes directly

- replace the one-field shared connection wrapper with the selected device interface path

- keep frame-token validation in the public session while the Gigabyte session owns only the vendor frame hold

- fill the existing debug statistics structure directly instead of translating duplicate structures

- preserve all capture, audio, event, zero-copy, GPU conversion, format-switch, and register-access APIs
This commit is contained in:
Mark.lin
2026-09-14 10:49:42 +08:00
parent 9bc38c4862
commit aac4eddaf7
5 changed files with 296 additions and 504 deletions
@@ -19,19 +19,19 @@ uint64_t monotonic_ns()
return static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(now).count());
}
gigabyte_pixel_format_t decode_fourcc(uint32_t fourcc)
gvfg_pixel_format_t decode_fourcc(uint32_t fourcc)
{
if (fourcc == kFourccY210)
return GIGABYTE_PIXFMT_Y210;
return GVFG_PIXFMT_Y210;
if (fourcc == kFourccYuy2 || fourcc == kFourccYuyv || fourcc == kFourccUyvy)
return GIGABYTE_PIXFMT_YUY2;
return GIGABYTE_PIXFMT_UNKNOWN;
return GVFG_PIXFMT_YUY2;
return GVFG_PIXFMT_UNKNOWN;
}
uint32_t bit_depth(gigabyte_pixel_format_t format)
uint32_t bit_depth(int format)
{
return format == GIGABYTE_PIXFMT_Y210 ? 10u :
format == GIGABYTE_PIXFMT_YUY2 ? 8u : 0u;
return format == GVFG_PIXFMT_Y210 ? 10u :
format == GVFG_PIXFMT_YUY2 ? 8u : 0u;
}
}
@@ -47,52 +47,50 @@ GigabyteCaptureSession::~GigabyteCaptureSession()
close();
}
gigabyte_status_t GigabyteCaptureSession::open_device_index(size_t deviceIndex)
gvfg_status_t GigabyteCaptureSession::open_device_index(size_t deviceIndex)
{
const auto devices = enumerate_gigabyte_devices();
if (deviceIndex >= devices.size())
return reject(GIGABYTE_ENODEV, "GigabyteLib open rejected: device index is unavailable");
return reject(GVFG_ENODEV, "GigabyteLib open rejected: device index is unavailable");
return open_device(devices[deviceIndex]);
}
gigabyte_status_t GigabyteCaptureSession::open_device_connection(
const std::shared_ptr<GigabyteDeviceConnection> &connection)
gvfg_status_t GigabyteCaptureSession::open_device_path(const std::wstring &devicePath)
{
if (!connection || connection->interface_path.empty())
return reject(GIGABYTE_EINVAL, "GigabyteLib open rejected: device selection is invalid");
if (devicePath.empty())
return reject(GVFG_EINVAL, "GigabyteLib open rejected: device selection is invalid");
close();
device_connection_ = connection;
return GIGABYTE_OK;
device_path_ = devicePath;
return GVFG_OK;
}
gigabyte_status_t GigabyteCaptureSession::open_device(const GigabyteDevice &device)
gvfg_status_t GigabyteCaptureSession::open_device(const GigabyteDevice &device)
{
close();
device_connection_ = std::make_shared<GigabyteDeviceConnection>();
device_connection_->interface_path = device.interface_path;
return GIGABYTE_OK;
device_path_ = device.interface_path;
return GVFG_OK;
}
gigabyte_status_t GigabyteCaptureSession::ensure_vendor_channel_open() const
gvfg_status_t GigabyteCaptureSession::ensure_vendor_channel_open() const
{
if (channel_open_)
return GIGABYTE_OK;
if (!device_connection_ || device_connection_->interface_path.empty())
return reject(GIGABYTE_ESTATE, "GigabyteLib channel open rejected: no device is selected");
return GVFG_OK;
if (device_path_.empty())
return reject(GVFG_ESTATE, "GigabyteLib channel open rejected: no device is selected");
device_handle_ = CreateFileW(device_connection_->interface_path.c_str(),
device_handle_ = CreateFileW(device_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(GIGABYTE_ENODEV, "CreateFile for GigabyteLib failed");
return reject(GVFG_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;
gigabyte_status_t status = from_vendor(
gvfg_status_t status = from_vendor(
GvfgOpenDev(device_handle_, &context_, memoryMode), "GvfgOpenDev");
if (status != GIGABYTE_OK)
if (status != GVFG_OK)
{
CloseHandle(device_handle_);
device_handle_ = INVALID_HANDLE_VALUE;
@@ -102,7 +100,7 @@ gigabyte_status_t GigabyteCaptureSession::ensure_vendor_channel_open() const
status = from_vendor(GvfgCreateEvents(&events_, audio_enabled_ ? FALSE : TRUE),
"GvfgCreateEvents");
if (status != GIGABYTE_OK)
if (status != GVFG_OK)
{
GvfgCloseDev(&context_);
CloseHandle(device_handle_);
@@ -115,12 +113,12 @@ gigabyte_status_t GigabyteCaptureSession::ensure_vendor_channel_open() const
if (!stop_event_)
{
close_vendor_channel();
return reject(GIGABYTE_EIO, "CreateEvent for GigabyteLib cancellation failed");
return reject(GVFG_EIO, "CreateEvent for GigabyteLib cancellation failed");
}
status = from_vendor(GvfgOpenVideoChn(context_, channel_, &events_),
"GvfgOpenVideoChn");
if (status != GIGABYTE_OK)
if (status != GVFG_OK)
{
close_vendor_channel();
return status;
@@ -148,71 +146,71 @@ void GigabyteCaptureSession::close_vendor_channel() const
device_handle_ = INVALID_HANDLE_VALUE;
}
gigabyte_status_t GigabyteCaptureSession::close()
gvfg_status_t GigabyteCaptureSession::close()
{
stop_stream();
close_vendor_channel();
device_connection_.reset();
device_path_.clear();
configured_ = false;
return GIGABYTE_OK;
return GVFG_OK;
}
gigabyte_status_t GigabyteCaptureSession::set_channel(uint32_t channel)
gvfg_status_t GigabyteCaptureSession::set_channel(uint32_t channel)
{
if (channel > 1)
return reject(GIGABYTE_EINVAL, "GigabyteLib channel index is invalid");
return reject(GVFG_EINVAL, "GigabyteLib channel index is invalid");
if (channel_open_)
return reject(GIGABYTE_ESTATE, "GigabyteLib channel is already open");
return reject(GVFG_ESTATE, "GigabyteLib channel is already open");
channel_ = channel;
return GIGABYTE_OK;
return GVFG_OK;
}
gigabyte_status_t GigabyteCaptureSession::set_zero_copy_enabled(bool enabled)
gvfg_status_t GigabyteCaptureSession::set_zero_copy_enabled(bool enabled)
{
if (channel_open_ || running_)
return reject(GIGABYTE_ESTATE, "GigabyteLib frame memory mode is already fixed");
return reject(GVFG_ESTATE, "GigabyteLib frame memory mode is already fixed");
zero_copy_enabled_ = enabled;
return GIGABYTE_OK;
return GVFG_OK;
}
gigabyte_status_t GigabyteCaptureSession::set_audio_enabled(bool enabled)
gvfg_status_t GigabyteCaptureSession::set_audio_enabled(bool enabled)
{
if (running_)
return reject(GIGABYTE_ESTATE, "GigabyteLib audio mode cannot change while running");
return reject(GVFG_ESTATE, "GigabyteLib audio mode cannot change while running");
if (channel_open_ && audio_enabled_ != enabled)
close_vendor_channel();
audio_enabled_ = enabled;
return GIGABYTE_OK;
return GVFG_OK;
}
gigabyte_status_t GigabyteCaptureSession::set_video_format(gigabyte_pixel_format_t format)
gvfg_status_t GigabyteCaptureSession::set_video_format(gvfg_pixel_format_t format)
{
if (format != GIGABYTE_PIXFMT_YUY2 && format != GIGABYTE_PIXFMT_Y210)
return reject(GIGABYTE_EINVAL, "video format is invalid");
if (format != GVFG_PIXFMT_YUY2 && format != GVFG_PIXFMT_Y210)
return reject(GVFG_EINVAL, "video format is invalid");
if (running_)
return reject(GIGABYTE_ESTATE, "video format cannot change while capture is running");
const gigabyte_status_t openStatus = ensure_vendor_channel_open();
if (openStatus != GIGABYTE_OK)
return reject(GVFG_ESTATE, "video format cannot change while capture is running");
const gvfg_status_t openStatus = ensure_vendor_channel_open();
if (openStatus != GVFG_OK)
return openStatus;
constexpr uint32_t kVideoOutputFormatRegister = 0x080;
if (!gigabyte_write_register(device_handle_, kVideoOutputFormatRegister,
format == GIGABYTE_PIXFMT_Y210 ? 1u : 0u))
return reject(GIGABYTE_EIO,
format == GVFG_PIXFMT_Y210 ? 1u : 0u))
return reject(GVFG_EIO,
"GigabyteLib gap: driver extension failed to set the output format register");
return GIGABYTE_OK;
return GVFG_OK;
}
gigabyte_status_t GigabyteCaptureSession::refresh_video_info() const
gvfg_status_t GigabyteCaptureSession::refresh_video_info() const
{
if (!context_)
return reject(GIGABYTE_ESTATE, "GigabyteLib video info requested before open");
return reject(GVFG_ESTATE, "GigabyteLib video info requested before open");
GVFG_VIDEO_INFO info{};
const gigabyte_status_t status = from_vendor(
const gvfg_status_t status = from_vendor(
GvfgGetVideoInfo(context_, channel_, &info), "GvfgGetVideoInfo");
if (status != GIGABYTE_OK)
if (status != GVFG_OK)
return status;
gigabyte_signal_status_t signal{};
gvfg_signal_status_t signal{};
signal.connected = info.VideoSignalLock ? 1 : 0;
signal.channel = channel_;
signal.width = info.Width;
@@ -223,16 +221,16 @@ gigabyte_status_t GigabyteCaptureSession::refresh_video_info() const
std::lock_guard<std::mutex> lock(state_mutex_);
cached_signal_ = signal;
}
return GIGABYTE_OK;
return GVFG_OK;
}
gigabyte_status_t GigabyteCaptureSession::get_signal_status(gigabyte_signal_status_t &out) const
gvfg_status_t GigabyteCaptureSession::get_signal_status(gvfg_signal_status_t &out) const
{
const gigabyte_status_t openStatus = ensure_vendor_channel_open();
if (openStatus != GIGABYTE_OK)
const gvfg_status_t openStatus = ensure_vendor_channel_open();
if (openStatus != GVFG_OK)
return openStatus;
const gigabyte_status_t status = refresh_video_info();
if (status == GIGABYTE_OK)
const gvfg_status_t status = refresh_video_info();
if (status == GVFG_OK)
{
std::lock_guard<std::mutex> lock(state_mutex_);
out = cached_signal_;
@@ -240,14 +238,14 @@ gigabyte_status_t GigabyteCaptureSession::get_signal_status(gigabyte_signal_stat
return status;
}
gigabyte_status_t GigabyteCaptureSession::get_audio_format(gigabyte_audio_format_t &out) const
gvfg_status_t GigabyteCaptureSession::get_audio_format(GigabyteAudioInfo &out) const
{
const gigabyte_status_t openStatus = ensure_vendor_channel_open();
if (openStatus != GIGABYTE_OK)
const gvfg_status_t openStatus = ensure_vendor_channel_open();
if (openStatus != GVFG_OK)
return openStatus;
const gigabyte_status_t status = from_vendor(
const gvfg_status_t status = from_vendor(
GvfgGetAudioInfo(context_, channel_, &audio_info_), "GvfgGetAudioInfo");
if (status != GIGABYTE_OK)
if (status != GVFG_OK)
return status;
out = {};
out.sample_rate = audio_info_.SamplesPerSec;
@@ -255,127 +253,123 @@ gigabyte_status_t GigabyteCaptureSession::get_audio_format(gigabyte_audio_format
out.bits_per_sample = audio_info_.BitsPerSample;
out.frames_per_second = audio_info_.FramesPerSec;
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 GIGABYTE_OK;
return GVFG_OK;
}
gigabyte_status_t GigabyteCaptureSession::set_event_callback(
gigabyte_event_callback_t callback, void *user, uint32_t eventMask)
gvfg_status_t GigabyteCaptureSession::set_event_callback(
GigabyteEventCallback 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 GIGABYTE_OK;
return GVFG_OK;
}
gigabyte_status_t GigabyteCaptureSession::configure_stream()
gvfg_status_t GigabyteCaptureSession::configure_stream()
{
if (running_)
return reject(GIGABYTE_ESTATE, "GigabyteLib stream is already running");
const gigabyte_status_t openStatus = ensure_vendor_channel_open();
if (openStatus != GIGABYTE_OK)
return reject(GVFG_ESTATE, "GigabyteLib stream is already running");
const gvfg_status_t openStatus = ensure_vendor_channel_open();
if (openStatus != GVFG_OK)
return openStatus;
configured_ = true;
return GIGABYTE_OK;
return GVFG_OK;
}
gigabyte_status_t GigabyteCaptureSession::start_stream()
gvfg_status_t GigabyteCaptureSession::start_stream()
{
if (running_)
return GIGABYTE_OK;
return GVFG_OK;
if (!configured_)
return reject(GIGABYTE_ESTATE, "GigabyteLib stream has not been configured");
return reject(GVFG_ESTATE, "GigabyteLib stream has not been configured");
ResetEvent(stop_event_);
const gigabyte_status_t status = from_vendor(
const gvfg_status_t status = from_vendor(
GvfgStartCapture(context_, channel_), "GvfgStartCapture");
if (status != GIGABYTE_OK)
if (status != GVFG_OK)
return status;
{
std::lock_guard<std::mutex> lock(state_mutex_);
held_frame_ = {};
frame_held_ = false;
frame_id_ = 0;
}
running_ = true;
start_event_monitoring();
return GIGABYTE_OK;
return GVFG_OK;
}
gigabyte_status_t GigabyteCaptureSession::stop_stream()
gvfg_status_t GigabyteCaptureSession::stop_stream()
{
const bool wasRunning = running_.exchange(false);
if (stop_event_)
SetEvent(stop_event_);
stop_event_monitoring();
gigabyte_status_t result = GIGABYTE_OK;
gvfg_status_t result = GVFG_OK;
{
std::lock_guard<std::mutex> lock(state_mutex_);
if (frame_held_ && zero_copy_enabled_ && context_)
result = from_vendor(GvfgReleaseVideoFrameZeroCopy(context_, channel_),
"GvfgReleaseVideoFrameZeroCopy during stop");
held_frame_ = {};
frame_held_ = false;
}
if (wasRunning && channel_open_ && context_)
{
const gigabyte_status_t stopStatus = from_vendor(
const gvfg_status_t stopStatus = from_vendor(
GvfgStopCapture(context_, channel_), "GvfgStopCapture");
if (result == GIGABYTE_OK)
if (result == GVFG_OK)
result = stopStatus;
}
return result;
}
gigabyte_status_t GigabyteCaptureSession::wait_frame(uint32_t timeoutMs, gigabyte_frame_t &out)
gvfg_status_t GigabyteCaptureSession::wait_frame(uint32_t timeoutMs, gvfg_frame_t &out)
{
out = {};
if (!running_ || !context_)
return reject(GIGABYTE_ESTATE, "GigabyteLib frame read rejected: stream is not running");
return reject(GVFG_ESTATE, "GigabyteLib frame read rejected: stream is not running");
{
std::lock_guard<std::mutex> lock(state_mutex_);
if (frame_held_)
return reject(GIGABYTE_ESTATE, "GigabyteLib frame read rejected: previous frame is held");
return reject(GVFG_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(GIGABYTE_EIO, "GigabyteLib did not provide a video-frame event");
return reject(GVFG_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 GIGABYTE_ETIMEOUT;
return GVFG_ETIMEOUT;
}
if (wait == WAIT_OBJECT_0)
return GIGABYTE_ESTATE;
return GVFG_ESTATE;
if (wait != WAIT_OBJECT_0 + 1 && wait != WAIT_OBJECT_0 + 2)
return reject(GIGABYTE_EIO, "GigabyteLib video event wait failed");
return reject(GVFG_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 gigabyte_status_t infoStatus = refresh_video_info();
if (infoStatus != GIGABYTE_OK)
const gvfg_status_t infoStatus = refresh_video_info();
if (infoStatus != GVFG_OK)
return infoStatus;
gigabyte_signal_status_t signal{};
gvfg_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 GIGABYTE_ETIMEOUT;
return GVFG_ETIMEOUT;
const size_t bytes = frame_size_bytes();
const void *data = nullptr;
gigabyte_status_t status = GIGABYTE_OK;
gvfg_status_t status = GVFG_OK;
if (zero_copy_enabled_)
{
void *buffer = nullptr;
@@ -394,59 +388,55 @@ gigabyte_status_t GigabyteCaptureSession::wait_frame(uint32_t timeoutMs, gigabyt
}
const auto end = std::chrono::steady_clock::now();
record_get_frame_timing(std::chrono::duration<double, std::micro>(end - begin).count());
if (status != GIGABYTE_OK)
if (status != GVFG_OK)
return status;
if (!data || bytes == 0)
return reject(GIGABYTE_EIO, "GigabyteLib returned an invalid video frame");
return reject(GVFG_EIO, "GigabyteLib returned an invalid video frame");
std::lock_guard<std::mutex> lock(state_mutex_);
out.data = data;
out.data_size_bytes = bytes;
out.data_size = bytes;
out.frame_id = ++frame_id_;
out.timestamp_ns = monotonic_ns();
out.width = signal.width;
out.height = signal.height;
out.row_stride_bytes = static_cast<int>(bytes / signal.height);
out.pixel_format = signal.pixel_format;
out.bit_depth = signal.bit_depth;
held_frame_ = out;
frame_held_ = true;
return GIGABYTE_OK;
return GVFG_OK;
}
gigabyte_status_t GigabyteCaptureSession::release_frame(const gigabyte_frame_t &frame)
gvfg_status_t GigabyteCaptureSession::release_frame()
{
std::lock_guard<std::mutex> lock(state_mutex_);
if (!frame_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(GIGABYTE_EINVAL, "GigabyteLib frame release token does not match");
return reject(GVFG_ESTATE, "GigabyteLib frame release rejected: no frame is held");
if (zero_copy_enabled_)
{
const gigabyte_status_t status = from_vendor(
const gvfg_status_t status = from_vendor(
GvfgReleaseVideoFrameZeroCopy(context_, channel_), "GvfgReleaseVideoFrameZeroCopy");
if (status != GIGABYTE_OK)
if (status != GVFG_OK)
return status;
}
held_frame_ = {};
frame_held_ = false;
return GIGABYTE_OK;
return GVFG_OK;
}
gigabyte_status_t GigabyteCaptureSession::wait_audio(
gvfg_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(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 reject(GVFG_ESTATE, "GigabyteLib audio read rejected");
GigabyteAudioInfo format{};
const gvfg_status_t formatStatus = get_audio_format(format);
if (formatStatus != GVFG_OK)
return formatStatus;
if (format.frame_bytes == 0 || format.frame_bytes > destinationCapacity)
return reject(GIGABYTE_EINVAL, "GigabyteLib audio destination is too small");
return reject(GVFG_EINVAL, "GigabyteLib audio destination is too small");
if (!events_.hAudioFrameInEvent)
return reject(GIGABYTE_EIO, "GigabyteLib did not provide an audio-frame event");
return reject(GVFG_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;
@@ -456,12 +446,12 @@ gigabyte_status_t GigabyteCaptureSession::wait_audio(
{
std::lock_guard<std::mutex> lock(state_mutex_);
++wait_timeout_count_;
return GIGABYTE_ETIMEOUT;
return GVFG_ETIMEOUT;
}
if (wait == WAIT_OBJECT_0)
return GIGABYTE_ESTATE;
return GVFG_ESTATE;
if (wait != WAIT_OBJECT_0 + 1 && wait != WAIT_OBJECT_0 + 2)
return reject(GIGABYTE_EIO, "GigabyteLib audio event wait failed");
return reject(GVFG_EIO, "GigabyteLib audio event wait failed");
{
std::lock_guard<std::mutex> lock(state_mutex_);
if (wait == WAIT_OBJECT_0 + 1)
@@ -469,9 +459,9 @@ gigabyte_status_t GigabyteCaptureSession::wait_audio(
else
++extra_audio_event_wakes_;
}
const gigabyte_status_t status = from_vendor(
const gvfg_status_t status = from_vendor(
GvfgGetAudioFrame(context_, channel_, destination, format.frame_bytes), "GvfgGetAudioFrame");
if (status != GIGABYTE_OK)
if (status != GVFG_OK)
return status;
outBytes = format.frame_bytes;
{
@@ -479,7 +469,7 @@ gigabyte_status_t GigabyteCaptureSession::wait_audio(
++audio_frames_from_driver_;
audio_bytes_from_driver_ += outBytes;
}
return GIGABYTE_OK;
return GVFG_OK;
}
void GigabyteCaptureSession::start_event_monitoring()
@@ -527,15 +517,15 @@ void GigabyteCaptureSession::event_thread_proc()
return;
if (kind == 1)
{
emit_event(GIGABYTE_EVENT_FORMAT_CHANGE_BEGIN);
emit_event(GVFG_EVENT_FORMAT_CHANGE_BEGIN);
refresh_video_info();
emit_event(GIGABYTE_EVENT_STREAM_READY);
emit_event(GVFG_EVENT_STREAM_READY);
}
else if (kind == 2)
{
refresh_video_info();
emit_event(GIGABYTE_EVENT_PLUG_IN);
emit_event(GIGABYTE_EVENT_STREAM_READY);
emit_event(GVFG_EVENT_SIGNAL_CONNECTED);
emit_event(GVFG_EVENT_STREAM_READY);
}
else if (kind == 3)
{
@@ -544,85 +534,80 @@ void GigabyteCaptureSession::event_thread_proc()
cached_signal_ = {};
cached_signal_.channel = channel_;
}
emit_event(GIGABYTE_EVENT_PLUG_OUT);
emit_event(GVFG_EVENT_SIGNAL_DISCONNECTED);
}
}
}
void GigabyteCaptureSession::emit_event(gigabyte_event_type_t type) const
void GigabyteCaptureSession::emit_event(gvfg_event_type_t type) const
{
std::lock_guard<std::mutex> lock(callback_mutex_);
uint32_t bit = 0;
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 (type == GVFG_EVENT_SIGNAL_CONNECTED) bit = GVFG_EVENT_MASK_SIGNAL_CONNECTED;
if (type == GVFG_EVENT_SIGNAL_DISCONNECTED) bit = GVFG_EVENT_MASK_SIGNAL_DISCONNECTED;
if (type == GVFG_EVENT_STREAM_READY) bit = GVFG_EVENT_MASK_STREAM_READY;
if (type == GVFG_EVENT_FORMAT_CHANGE_BEGIN) bit = GVFG_EVENT_MASK_FORMAT_CHANGE_BEGIN;
if (event_callback_ && (event_mask_filter_ & bit) != 0)
event_callback_(type, event_callback_user_);
}
void GigabyteCaptureSession::get_debug_stats(
gigabyte_stream_stats_t &outStats, uint64_t &outWaitTimeouts,
gigabyte_debug_state_t &outDebugState) const
void GigabyteCaptureSession::fill_debug_stats(gvfg_debug_backend_stats_t &out) const
{
std::lock_guard<std::mutex> lock(state_mutex_);
outStats = {};
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_;
outWaitTimeouts = wait_timeout_count_;
outDebugState = {};
outDebugState.running = running_ ? 1 : 0;
outDebugState.capture_active = running_ ? 1 : 0;
outDebugState.latest_sequence = video_event_wakes_;
outDebugState.delivered_sequence = frame_id_;
outDebugState.audio_dma_event_wakes = audio_event_wakes_;
outDebugState.extra_audio_event_wakes = extra_audio_event_wakes_;
outDebugState.audio_frames_from_driver = audio_frames_from_driver_;
outDebugState.audio_bytes_from_driver = audio_bytes_from_driver_;
outDebugState.get_frame_zero_copy = zero_copy_enabled_ ? 1 : 0;
outDebugState.get_frame_timing_samples = get_frame_timing_samples_;
outDebugState.get_frame_timing_average_us = get_frame_timing_samples_
out.backend_state = running_ ? 2 : configured_ ? 1 : 0;
out.backend_frames_captured = video_event_wakes_;
out.backend_frames_delivered = frame_id_;
out.backend_interrupt_count = video_event_wakes_;
out.backend_wait_timeouts = wait_timeout_count_;
out.backend_running = running_ ? 1 : 0;
out.backend_capture_active = running_ ? 1 : 0;
out.backend_latest_sequence = video_event_wakes_;
out.backend_delivered_sequence = frame_id_;
out.audio_dma_event_wakes = audio_event_wakes_;
out.extra_audio_event_wakes = extra_audio_event_wakes_;
out.audio_frames_from_driver = audio_frames_from_driver_;
out.audio_bytes_from_driver = audio_bytes_from_driver_;
out.get_frame_zero_copy = zero_copy_enabled_ ? 1 : 0;
out.get_frame_timing_samples = get_frame_timing_samples_;
out.get_frame_timing_average_us = get_frame_timing_samples_
? get_frame_timing_total_us_ / static_cast<double>(get_frame_timing_samples_) : 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_;
out.get_frame_timing_max300_us = get_frame_timing_last_max300_us_;
out.get_frame_timing_max_us = get_frame_timing_lifetime_max_us_;
}
gigabyte_status_t GigabyteCaptureSession::debug_read_register(uint32_t offset, uint32_t &outValue) const
gvfg_status_t GigabyteCaptureSession::debug_read_register(uint32_t offset, uint32_t &outValue) const
{
const gigabyte_status_t openStatus = ensure_vendor_channel_open();
if (openStatus != GIGABYTE_OK)
const gvfg_status_t openStatus = ensure_vendor_channel_open();
if (openStatus != GVFG_OK)
return openStatus;
if (!gigabyte_read_register(device_handle_, offset, outValue))
return reject(GIGABYTE_EIO,
return reject(GVFG_EIO,
"GigabyteLib gap: driver extension register read failed");
return GIGABYTE_OK;
return GVFG_OK;
}
gigabyte_status_t GigabyteCaptureSession::debug_write_register(uint32_t offset, uint32_t value) const
gvfg_status_t GigabyteCaptureSession::debug_write_register(uint32_t offset, uint32_t value) const
{
const gigabyte_status_t openStatus = ensure_vendor_channel_open();
if (openStatus != GIGABYTE_OK)
const gvfg_status_t openStatus = ensure_vendor_channel_open();
if (openStatus != GVFG_OK)
return openStatus;
if (!gigabyte_write_register(device_handle_, offset, value))
return reject(GIGABYTE_EIO,
return reject(GVFG_EIO,
"GigabyteLib gap: driver extension register write failed");
return GIGABYTE_OK;
return GVFG_OK;
}
gigabyte_status_t GigabyteCaptureSession::from_vendor(GVFG_HRESULT result, const char *operation) const
gvfg_status_t GigabyteCaptureSession::from_vendor(GVFG_HRESULT result, const char *operation) const
{
if (result == GVFG_HRESULT_OK)
return GIGABYTE_OK;
gigabyte_status_t status = GIGABYTE_EIO;
return GVFG_OK;
gvfg_status_t status = GVFG_EIO;
if (result == GVFG_HRESULT_DEV_BUSY || result == GVFG_HRESULT_CONTEXT_ERROR)
status = GIGABYTE_ESTATE;
status = GVFG_ESTATE;
else if (result == GVFG_HRESULT_VIDEO_CHN_INVALID)
status = GIGABYTE_EINVAL;
status = GVFG_EINVAL;
else if (result == GVFG_HRESULT_DEV_ERROR)
status = GIGABYTE_ENODEV;
status = GVFG_ENODEV;
std::ostringstream stream;
stream << (operation ? operation : "GigabyteLib")
<< " failed with GVFG_HRESULT 0x" << std::hex << std::uppercase
@@ -631,7 +616,7 @@ gigabyte_status_t GigabyteCaptureSession::from_vendor(GVFG_HRESULT result, const
return status;
}
gigabyte_status_t GigabyteCaptureSession::reject(gigabyte_status_t status, const char *message) const
gvfg_status_t GigabyteCaptureSession::reject(gvfg_status_t status, const char *message) const
{
error_state_.set(message ? message : "GigabyteLib operation rejected");
return status;
@@ -642,8 +627,8 @@ size_t GigabyteCaptureSession::frame_size_bytes() const
std::lock_guard<std::mutex> lock(state_mutex_);
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;
const int format = cached_signal_.pixel_format;
const size_t bytesPerPixel = format == GVFG_PIXFMT_Y210 ? 4u : 2u;
return static_cast<size_t>(width) * height * bytesPerPixel;
}
@@ -1,8 +1,9 @@
#pragma once
#include "gigabyte_types.h"
#include "gigabyte_device.h"
#include "gvfg_error_state.h"
#include "gvfg_capture.h"
#include "gvfg_debug.h"
#include "gvfgsdkapi.h"
#include <windows.h>
@@ -10,7 +11,6 @@
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
@@ -18,11 +18,17 @@
namespace gvfg::internal
{
struct GigabyteDeviceConnection
struct GigabyteAudioInfo
{
std::wstring interface_path;
uint32_t sample_rate = 0;
uint32_t channels = 0;
uint32_t bits_per_sample = 0;
uint32_t frames_per_second = 0;
uint32_t frame_bytes = 0;
};
using GigabyteEventCallback = void (*)(gvfg_event_type_t event, void *user);
class GigabyteCaptureSession
{
public:
@@ -31,53 +37,52 @@ namespace gvfg::internal
GigabyteCaptureSession &operator=(const GigabyteCaptureSession &) = delete;
~GigabyteCaptureSession();
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);
gvfg_status_t open_device_index(size_t deviceIndex);
gvfg_status_t open_device_path(const std::wstring &devicePath);
const std::wstring &device_path() const { return device_path_; }
gvfg_status_t close();
gvfg_status_t set_channel(uint32_t channel);
gvfg_status_t set_zero_copy_enabled(bool enabled);
bool zero_copy_enabled() const { return zero_copy_enabled_; }
gigabyte_status_t set_audio_enabled(bool enabled);
gigabyte_status_t set_video_format(gigabyte_pixel_format_t format);
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,
gvfg_status_t set_audio_enabled(bool enabled);
gvfg_status_t set_video_format(gvfg_pixel_format_t format);
gvfg_status_t get_signal_status(gvfg_signal_status_t &out) const;
gvfg_status_t get_audio_format(GigabyteAudioInfo &out) const;
gvfg_status_t set_event_callback(GigabyteEventCallback callback, void *user, uint32_t eventMask);
gvfg_status_t configure_stream();
gvfg_status_t start_stream();
gvfg_status_t stop_stream();
gvfg_status_t wait_frame(uint32_t timeoutMs, gvfg_frame_t &out);
gvfg_status_t wait_audio(uint32_t timeoutMs, void *destination,
uint32_t destinationCapacity, uint32_t &outBytes);
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;
gigabyte_status_t debug_read_register(uint32_t offset, uint32_t &outValue) const;
gigabyte_status_t debug_write_register(uint32_t offset, uint32_t value) const;
gvfg_status_t release_frame();
void fill_debug_stats(gvfg_debug_backend_stats_t &out) const;
gvfg_status_t debug_read_register(uint32_t offset, uint32_t &outValue) const;
gvfg_status_t debug_write_register(uint32_t offset, uint32_t value) const;
private:
gigabyte_status_t open_device(const GigabyteDevice &device);
gigabyte_status_t ensure_vendor_channel_open() const;
gvfg_status_t open_device(const GigabyteDevice &device);
gvfg_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(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 emit_event(gvfg_event_type_t type) const;
gvfg_status_t refresh_video_info() const;
gvfg_status_t from_vendor(GVFG_HRESULT result, const char *operation) const;
gvfg_status_t reject(gvfg_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<GigabyteDeviceConnection> device_connection_;
std::wstring device_path_;
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;
mutable gigabyte_signal_status_t cached_signal_{};
mutable gvfg_signal_status_t cached_signal_{};
mutable GVFG_AUDIO_INFO audio_info_{};
uint32_t channel_ = 0;
bool configured_ = false;
@@ -87,12 +92,11 @@ namespace gvfg::internal
std::atomic<bool> monitoring_{false};
std::thread event_thread_;
mutable std::mutex callback_mutex_;
gigabyte_event_callback_t event_callback_ = nullptr;
GigabyteEventCallback event_callback_ = nullptr;
void *event_callback_user_ = nullptr;
uint32_t event_mask_filter_ = GIGABYTE_EVENT_MASK_DEFAULT;
uint32_t event_mask_filter_ = GVFG_EVENT_MASK_ALL;
mutable std::mutex state_mutex_;
std::vector<uint8_t> copy_buffer_;
gigabyte_frame_t held_frame_{};
bool frame_held_ = false;
uint64_t frame_id_ = 0;
uint64_t audio_frames_from_driver_ = 0;
@@ -1,109 +0,0 @@
#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);