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
-1
View File
@@ -7,7 +7,6 @@ add_library(gvfg SHARED
gvfg.def gvfg.def
include/gvfg_capture.h include/gvfg_capture.h
include/gvfg_debug.h include/gvfg_debug.h
src/gigabyte/gigabyte_types.h
src/gigabyte/gigabyte_capture_session.cpp src/gigabyte/gigabyte_capture_session.cpp
src/gigabyte/gigabyte_capture_session.h src/gigabyte/gigabyte_capture_session.h
src/gigabyte/gigabyte_driver_extensions.cpp src/gigabyte/gigabyte_driver_extensions.cpp
@@ -19,19 +19,19 @@ uint64_t monotonic_ns()
return static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(now).count()); 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) if (fourcc == kFourccY210)
return GIGABYTE_PIXFMT_Y210; return GVFG_PIXFMT_Y210;
if (fourcc == kFourccYuy2 || fourcc == kFourccYuyv || fourcc == kFourccUyvy) if (fourcc == kFourccYuy2 || fourcc == kFourccYuyv || fourcc == kFourccUyvy)
return GIGABYTE_PIXFMT_YUY2; return GVFG_PIXFMT_YUY2;
return GIGABYTE_PIXFMT_UNKNOWN; 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 : return format == GVFG_PIXFMT_Y210 ? 10u :
format == GIGABYTE_PIXFMT_YUY2 ? 8u : 0u; format == GVFG_PIXFMT_YUY2 ? 8u : 0u;
} }
} }
@@ -47,52 +47,50 @@ GigabyteCaptureSession::~GigabyteCaptureSession()
close(); 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(); const auto devices = enumerate_gigabyte_devices();
if (deviceIndex >= devices.size()) 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]); return open_device(devices[deviceIndex]);
} }
gigabyte_status_t GigabyteCaptureSession::open_device_connection( gvfg_status_t GigabyteCaptureSession::open_device_path(const std::wstring &devicePath)
const std::shared_ptr<GigabyteDeviceConnection> &connection)
{ {
if (!connection || connection->interface_path.empty()) if (devicePath.empty())
return reject(GIGABYTE_EINVAL, "GigabyteLib open rejected: device selection is invalid"); return reject(GVFG_EINVAL, "GigabyteLib open rejected: device selection is invalid");
close(); close();
device_connection_ = connection; device_path_ = devicePath;
return GIGABYTE_OK; return GVFG_OK;
} }
gigabyte_status_t GigabyteCaptureSession::open_device(const GigabyteDevice &device) gvfg_status_t GigabyteCaptureSession::open_device(const GigabyteDevice &device)
{ {
close(); close();
device_connection_ = std::make_shared<GigabyteDeviceConnection>(); device_path_ = device.interface_path;
device_connection_->interface_path = device.interface_path; return GVFG_OK;
return GIGABYTE_OK;
} }
gigabyte_status_t GigabyteCaptureSession::ensure_vendor_channel_open() const gvfg_status_t GigabyteCaptureSession::ensure_vendor_channel_open() const
{ {
if (channel_open_) if (channel_open_)
return GIGABYTE_OK; return GVFG_OK;
if (!device_connection_ || device_connection_->interface_path.empty()) if (device_path_.empty())
return reject(GIGABYTE_ESTATE, "GigabyteLib channel open rejected: no device is selected"); 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, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (device_handle_ == INVALID_HANDLE_VALUE) 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_ const ULONG memoryMode = zero_copy_enabled_
? GVFG_VIDEO_FRAME_ZERO_COPY | GVFG_AUDIO_FRAME_BUF_COPY ? GVFG_VIDEO_FRAME_ZERO_COPY | GVFG_AUDIO_FRAME_BUF_COPY
: GVFG_VIDEO_FRAME_BUF_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"); GvfgOpenDev(device_handle_, &context_, memoryMode), "GvfgOpenDev");
if (status != GIGABYTE_OK) if (status != GVFG_OK)
{ {
CloseHandle(device_handle_); CloseHandle(device_handle_);
device_handle_ = INVALID_HANDLE_VALUE; 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), status = from_vendor(GvfgCreateEvents(&events_, audio_enabled_ ? FALSE : TRUE),
"GvfgCreateEvents"); "GvfgCreateEvents");
if (status != GIGABYTE_OK) if (status != GVFG_OK)
{ {
GvfgCloseDev(&context_); GvfgCloseDev(&context_);
CloseHandle(device_handle_); CloseHandle(device_handle_);
@@ -115,12 +113,12 @@ gigabyte_status_t GigabyteCaptureSession::ensure_vendor_channel_open() const
if (!stop_event_) if (!stop_event_)
{ {
close_vendor_channel(); 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_), status = from_vendor(GvfgOpenVideoChn(context_, channel_, &events_),
"GvfgOpenVideoChn"); "GvfgOpenVideoChn");
if (status != GIGABYTE_OK) if (status != GVFG_OK)
{ {
close_vendor_channel(); close_vendor_channel();
return status; return status;
@@ -148,71 +146,71 @@ void GigabyteCaptureSession::close_vendor_channel() const
device_handle_ = INVALID_HANDLE_VALUE; device_handle_ = INVALID_HANDLE_VALUE;
} }
gigabyte_status_t GigabyteCaptureSession::close() gvfg_status_t GigabyteCaptureSession::close()
{ {
stop_stream(); stop_stream();
close_vendor_channel(); close_vendor_channel();
device_connection_.reset(); device_path_.clear();
configured_ = false; 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) if (channel > 1)
return reject(GIGABYTE_EINVAL, "GigabyteLib channel index is invalid"); return reject(GVFG_EINVAL, "GigabyteLib channel index is invalid");
if (channel_open_) if (channel_open_)
return reject(GIGABYTE_ESTATE, "GigabyteLib channel is already open"); return reject(GVFG_ESTATE, "GigabyteLib channel is already open");
channel_ = channel; 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_) 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; 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_) 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) if (channel_open_ && audio_enabled_ != enabled)
close_vendor_channel(); close_vendor_channel();
audio_enabled_ = enabled; 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) if (format != GVFG_PIXFMT_YUY2 && format != GVFG_PIXFMT_Y210)
return reject(GIGABYTE_EINVAL, "video format is invalid"); return reject(GVFG_EINVAL, "video format is invalid");
if (running_) if (running_)
return reject(GIGABYTE_ESTATE, "video format cannot change while capture is running"); return reject(GVFG_ESTATE, "video format cannot change while capture is running");
const gigabyte_status_t openStatus = ensure_vendor_channel_open(); const gvfg_status_t openStatus = ensure_vendor_channel_open();
if (openStatus != GIGABYTE_OK) if (openStatus != GVFG_OK)
return openStatus; return openStatus;
constexpr uint32_t kVideoOutputFormatRegister = 0x080; constexpr uint32_t kVideoOutputFormatRegister = 0x080;
if (!gigabyte_write_register(device_handle_, kVideoOutputFormatRegister, if (!gigabyte_write_register(device_handle_, kVideoOutputFormatRegister,
format == GIGABYTE_PIXFMT_Y210 ? 1u : 0u)) format == GVFG_PIXFMT_Y210 ? 1u : 0u))
return reject(GIGABYTE_EIO, return reject(GVFG_EIO,
"GigabyteLib gap: driver extension failed to set the output format register"); "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_) 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{}; GVFG_VIDEO_INFO info{};
const gigabyte_status_t status = from_vendor( const gvfg_status_t status = from_vendor(
GvfgGetVideoInfo(context_, channel_, &info), "GvfgGetVideoInfo"); GvfgGetVideoInfo(context_, channel_, &info), "GvfgGetVideoInfo");
if (status != GIGABYTE_OK) if (status != GVFG_OK)
return status; return status;
gigabyte_signal_status_t signal{}; gvfg_signal_status_t signal{};
signal.connected = info.VideoSignalLock ? 1 : 0; signal.connected = info.VideoSignalLock ? 1 : 0;
signal.channel = channel_; signal.channel = channel_;
signal.width = info.Width; signal.width = info.Width;
@@ -223,16 +221,16 @@ gigabyte_status_t GigabyteCaptureSession::refresh_video_info() const
std::lock_guard<std::mutex> lock(state_mutex_); std::lock_guard<std::mutex> lock(state_mutex_);
cached_signal_ = signal; 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(); const gvfg_status_t openStatus = ensure_vendor_channel_open();
if (openStatus != GIGABYTE_OK) if (openStatus != GVFG_OK)
return openStatus; return openStatus;
const gigabyte_status_t status = refresh_video_info(); const gvfg_status_t status = refresh_video_info();
if (status == GIGABYTE_OK) if (status == GVFG_OK)
{ {
std::lock_guard<std::mutex> lock(state_mutex_); std::lock_guard<std::mutex> lock(state_mutex_);
out = cached_signal_; out = cached_signal_;
@@ -240,14 +238,14 @@ gigabyte_status_t GigabyteCaptureSession::get_signal_status(gigabyte_signal_stat
return status; 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(); const gvfg_status_t openStatus = ensure_vendor_channel_open();
if (openStatus != GIGABYTE_OK) if (openStatus != GVFG_OK)
return openStatus; return openStatus;
const gigabyte_status_t status = from_vendor( const gvfg_status_t status = from_vendor(
GvfgGetAudioInfo(context_, channel_, &audio_info_), "GvfgGetAudioInfo"); GvfgGetAudioInfo(context_, channel_, &audio_info_), "GvfgGetAudioInfo");
if (status != GIGABYTE_OK) if (status != GVFG_OK)
return status; return status;
out = {}; out = {};
out.sample_rate = audio_info_.SamplesPerSec; 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.bits_per_sample = audio_info_.BitsPerSample;
out.frames_per_second = audio_info_.FramesPerSec; out.frames_per_second = audio_info_.FramesPerSec;
out.frame_bytes = audio_info_.cbBufSize; out.frame_bytes = audio_info_.cbBufSize;
out.block_align = static_cast<uint32_t>(audio_info_.Channels) * return GVFG_OK;
static_cast<uint32_t>(audio_info_.BitsPerSample) / 8u;
return GIGABYTE_OK;
} }
gigabyte_status_t GigabyteCaptureSession::set_event_callback( gvfg_status_t GigabyteCaptureSession::set_event_callback(
gigabyte_event_callback_t callback, void *user, uint32_t eventMask) GigabyteEventCallback callback, void *user, uint32_t eventMask)
{ {
std::lock_guard<std::mutex> lock(callback_mutex_); std::lock_guard<std::mutex> lock(callback_mutex_);
event_callback_ = callback; event_callback_ = callback;
event_callback_user_ = user; event_callback_user_ = user;
event_mask_filter_ = eventMask; event_mask_filter_ = eventMask;
return GIGABYTE_OK; return GVFG_OK;
} }
gigabyte_status_t GigabyteCaptureSession::configure_stream() gvfg_status_t GigabyteCaptureSession::configure_stream()
{ {
if (running_) if (running_)
return reject(GIGABYTE_ESTATE, "GigabyteLib stream is already running"); return reject(GVFG_ESTATE, "GigabyteLib stream is already running");
const gigabyte_status_t openStatus = ensure_vendor_channel_open(); const gvfg_status_t openStatus = ensure_vendor_channel_open();
if (openStatus != GIGABYTE_OK) if (openStatus != GVFG_OK)
return openStatus; return openStatus;
configured_ = true; configured_ = true;
return GIGABYTE_OK; return GVFG_OK;
} }
gigabyte_status_t GigabyteCaptureSession::start_stream() gvfg_status_t GigabyteCaptureSession::start_stream()
{ {
if (running_) if (running_)
return GIGABYTE_OK; return GVFG_OK;
if (!configured_) 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_); ResetEvent(stop_event_);
const gigabyte_status_t status = from_vendor( const gvfg_status_t status = from_vendor(
GvfgStartCapture(context_, channel_), "GvfgStartCapture"); GvfgStartCapture(context_, channel_), "GvfgStartCapture");
if (status != GIGABYTE_OK) if (status != GVFG_OK)
return status; return status;
{ {
std::lock_guard<std::mutex> lock(state_mutex_); std::lock_guard<std::mutex> lock(state_mutex_);
held_frame_ = {};
frame_held_ = false; frame_held_ = false;
frame_id_ = 0; frame_id_ = 0;
} }
running_ = true; running_ = true;
start_event_monitoring(); 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); const bool wasRunning = running_.exchange(false);
if (stop_event_) if (stop_event_)
SetEvent(stop_event_); SetEvent(stop_event_);
stop_event_monitoring(); stop_event_monitoring();
gigabyte_status_t result = GIGABYTE_OK; gvfg_status_t result = GVFG_OK;
{ {
std::lock_guard<std::mutex> lock(state_mutex_); std::lock_guard<std::mutex> lock(state_mutex_);
if (frame_held_ && zero_copy_enabled_ && context_) if (frame_held_ && zero_copy_enabled_ && context_)
result = from_vendor(GvfgReleaseVideoFrameZeroCopy(context_, channel_), result = from_vendor(GvfgReleaseVideoFrameZeroCopy(context_, channel_),
"GvfgReleaseVideoFrameZeroCopy during stop"); "GvfgReleaseVideoFrameZeroCopy during stop");
held_frame_ = {};
frame_held_ = false; frame_held_ = false;
} }
if (wasRunning && channel_open_ && context_) if (wasRunning && channel_open_ && context_)
{ {
const gigabyte_status_t stopStatus = from_vendor( const gvfg_status_t stopStatus = from_vendor(
GvfgStopCapture(context_, channel_), "GvfgStopCapture"); GvfgStopCapture(context_, channel_), "GvfgStopCapture");
if (result == GIGABYTE_OK) if (result == GVFG_OK)
result = stopStatus; result = stopStatus;
} }
return result; 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 = {}; out = {};
if (!running_ || !context_) 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_); std::lock_guard<std::mutex> lock(state_mutex_);
if (frame_held_) 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}; std::array<HANDLE, 3> handles{stop_event_, events_.hVideoFrameInEvent, events_.hVideoExtraFrame};
const DWORD count = handles[2] ? 3u : 2u; const DWORD count = handles[2] ? 3u : 2u;
if (!handles[1]) 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 waitMs = timeoutMs == UINT32_MAX ? INFINITE : timeoutMs;
const DWORD wait = WaitForMultipleObjects(count, handles.data(), FALSE, waitMs); const DWORD wait = WaitForMultipleObjects(count, handles.data(), FALSE, waitMs);
if (wait == WAIT_TIMEOUT) if (wait == WAIT_TIMEOUT)
{ {
std::lock_guard<std::mutex> lock(state_mutex_); std::lock_guard<std::mutex> lock(state_mutex_);
++wait_timeout_count_; ++wait_timeout_count_;
return GIGABYTE_ETIMEOUT; return GVFG_ETIMEOUT;
} }
if (wait == WAIT_OBJECT_0) if (wait == WAIT_OBJECT_0)
return GIGABYTE_ESTATE; return GVFG_ESTATE;
if (wait != WAIT_OBJECT_0 + 1 && wait != WAIT_OBJECT_0 + 2) 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_); std::lock_guard<std::mutex> lock(state_mutex_);
++video_event_wakes_; ++video_event_wakes_;
} }
const auto begin = std::chrono::steady_clock::now(); const auto begin = std::chrono::steady_clock::now();
const gigabyte_status_t infoStatus = refresh_video_info(); const gvfg_status_t infoStatus = refresh_video_info();
if (infoStatus != GIGABYTE_OK) if (infoStatus != GVFG_OK)
return infoStatus; return infoStatus;
gigabyte_signal_status_t signal{}; gvfg_signal_status_t signal{};
{ {
std::lock_guard<std::mutex> lock(state_mutex_); std::lock_guard<std::mutex> lock(state_mutex_);
signal = cached_signal_; signal = cached_signal_;
} }
if (!signal.connected || signal.width == 0 || signal.height == 0) if (!signal.connected || signal.width == 0 || signal.height == 0)
return GIGABYTE_ETIMEOUT; return GVFG_ETIMEOUT;
const size_t bytes = frame_size_bytes(); const size_t bytes = frame_size_bytes();
const void *data = nullptr; const void *data = nullptr;
gigabyte_status_t status = GIGABYTE_OK; gvfg_status_t status = GVFG_OK;
if (zero_copy_enabled_) if (zero_copy_enabled_)
{ {
void *buffer = nullptr; 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(); const auto end = std::chrono::steady_clock::now();
record_get_frame_timing(std::chrono::duration<double, std::micro>(end - begin).count()); record_get_frame_timing(std::chrono::duration<double, std::micro>(end - begin).count());
if (status != GIGABYTE_OK) if (status != GVFG_OK)
return status; return status;
if (!data || bytes == 0) 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_); std::lock_guard<std::mutex> lock(state_mutex_);
out.data = data; out.data = data;
out.data_size_bytes = bytes; out.data_size = bytes;
out.frame_id = ++frame_id_; out.frame_id = ++frame_id_;
out.timestamp_ns = monotonic_ns(); out.timestamp_ns = monotonic_ns();
out.width = signal.width; out.width = signal.width;
out.height = signal.height; out.height = signal.height;
out.row_stride_bytes = static_cast<int>(bytes / signal.height);
out.pixel_format = signal.pixel_format; out.pixel_format = signal.pixel_format;
out.bit_depth = signal.bit_depth; out.bit_depth = signal.bit_depth;
held_frame_ = out;
frame_held_ = true; 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_); std::lock_guard<std::mutex> lock(state_mutex_);
if (!frame_held_) if (!frame_held_)
return reject(GIGABYTE_ESTATE, "GigabyteLib frame release rejected: no frame is held"); return reject(GVFG_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");
if (zero_copy_enabled_) if (zero_copy_enabled_)
{ {
const gigabyte_status_t status = from_vendor( const gvfg_status_t status = from_vendor(
GvfgReleaseVideoFrameZeroCopy(context_, channel_), "GvfgReleaseVideoFrameZeroCopy"); GvfgReleaseVideoFrameZeroCopy(context_, channel_), "GvfgReleaseVideoFrameZeroCopy");
if (status != GIGABYTE_OK) if (status != GVFG_OK)
return status; return status;
} }
held_frame_ = {};
frame_held_ = false; 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) uint32_t timeoutMs, void *destination, uint32_t destinationCapacity, uint32_t &outBytes)
{ {
outBytes = 0; outBytes = 0;
if (!running_ || !audio_enabled_ || !destination) if (!running_ || !audio_enabled_ || !destination)
return reject(GIGABYTE_ESTATE, "GigabyteLib audio read rejected"); return reject(GVFG_ESTATE, "GigabyteLib audio read rejected");
gigabyte_audio_format_t format{}; GigabyteAudioInfo format{};
const gigabyte_status_t formatStatus = get_audio_format(format); const gvfg_status_t formatStatus = get_audio_format(format);
if (formatStatus != GIGABYTE_OK) if (formatStatus != GVFG_OK)
return formatStatus; return formatStatus;
if (format.frame_bytes == 0 || format.frame_bytes > destinationCapacity) 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) 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}; std::array<HANDLE, 3> handles{stop_event_, events_.hAudioFrameInEvent, events_.hAudioExtraFrame};
const DWORD count = handles[2] ? 3u : 2u; 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_); std::lock_guard<std::mutex> lock(state_mutex_);
++wait_timeout_count_; ++wait_timeout_count_;
return GIGABYTE_ETIMEOUT; return GVFG_ETIMEOUT;
} }
if (wait == WAIT_OBJECT_0) if (wait == WAIT_OBJECT_0)
return GIGABYTE_ESTATE; return GVFG_ESTATE;
if (wait != WAIT_OBJECT_0 + 1 && wait != WAIT_OBJECT_0 + 2) 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_); std::lock_guard<std::mutex> lock(state_mutex_);
if (wait == WAIT_OBJECT_0 + 1) if (wait == WAIT_OBJECT_0 + 1)
@@ -469,9 +459,9 @@ gigabyte_status_t GigabyteCaptureSession::wait_audio(
else else
++extra_audio_event_wakes_; ++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"); GvfgGetAudioFrame(context_, channel_, destination, format.frame_bytes), "GvfgGetAudioFrame");
if (status != GIGABYTE_OK) if (status != GVFG_OK)
return status; return status;
outBytes = format.frame_bytes; outBytes = format.frame_bytes;
{ {
@@ -479,7 +469,7 @@ gigabyte_status_t GigabyteCaptureSession::wait_audio(
++audio_frames_from_driver_; ++audio_frames_from_driver_;
audio_bytes_from_driver_ += outBytes; audio_bytes_from_driver_ += outBytes;
} }
return GIGABYTE_OK; return GVFG_OK;
} }
void GigabyteCaptureSession::start_event_monitoring() void GigabyteCaptureSession::start_event_monitoring()
@@ -527,15 +517,15 @@ void GigabyteCaptureSession::event_thread_proc()
return; return;
if (kind == 1) if (kind == 1)
{ {
emit_event(GIGABYTE_EVENT_FORMAT_CHANGE_BEGIN); emit_event(GVFG_EVENT_FORMAT_CHANGE_BEGIN);
refresh_video_info(); refresh_video_info();
emit_event(GIGABYTE_EVENT_STREAM_READY); emit_event(GVFG_EVENT_STREAM_READY);
} }
else if (kind == 2) else if (kind == 2)
{ {
refresh_video_info(); refresh_video_info();
emit_event(GIGABYTE_EVENT_PLUG_IN); emit_event(GVFG_EVENT_SIGNAL_CONNECTED);
emit_event(GIGABYTE_EVENT_STREAM_READY); emit_event(GVFG_EVENT_STREAM_READY);
} }
else if (kind == 3) else if (kind == 3)
{ {
@@ -544,85 +534,80 @@ void GigabyteCaptureSession::event_thread_proc()
cached_signal_ = {}; cached_signal_ = {};
cached_signal_.channel = channel_; 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_); std::lock_guard<std::mutex> lock(callback_mutex_);
uint32_t bit = 0; uint32_t bit = 0;
if (type == GIGABYTE_EVENT_PLUG_IN) bit = GIGABYTE_EVENT_MASK_PLUG_IN; if (type == GVFG_EVENT_SIGNAL_CONNECTED) bit = GVFG_EVENT_MASK_SIGNAL_CONNECTED;
if (type == GIGABYTE_EVENT_PLUG_OUT) bit = GIGABYTE_EVENT_MASK_PLUG_OUT; if (type == GVFG_EVENT_SIGNAL_DISCONNECTED) bit = GVFG_EVENT_MASK_SIGNAL_DISCONNECTED;
if (type == GIGABYTE_EVENT_STREAM_READY) bit = GIGABYTE_EVENT_MASK_STREAM_READY; if (type == GVFG_EVENT_STREAM_READY) bit = GVFG_EVENT_MASK_STREAM_READY;
if (type == GIGABYTE_EVENT_FORMAT_CHANGE_BEGIN) bit = GIGABYTE_EVENT_MASK_FORMAT_CHANGE_BEGIN; if (type == GVFG_EVENT_FORMAT_CHANGE_BEGIN) bit = GVFG_EVENT_MASK_FORMAT_CHANGE_BEGIN;
if (event_callback_ && (event_mask_filter_ & bit) != 0) if (event_callback_ && (event_mask_filter_ & bit) != 0)
event_callback_(type, event_callback_user_); event_callback_(type, event_callback_user_);
} }
void GigabyteCaptureSession::get_debug_stats( void GigabyteCaptureSession::fill_debug_stats(gvfg_debug_backend_stats_t &out) const
gigabyte_stream_stats_t &outStats, uint64_t &outWaitTimeouts,
gigabyte_debug_state_t &outDebugState) const
{ {
std::lock_guard<std::mutex> lock(state_mutex_); std::lock_guard<std::mutex> lock(state_mutex_);
outStats = {}; out.backend_state = running_ ? 2 : configured_ ? 1 : 0;
outStats.state = running_ ? GIGABYTE_STREAM_RUNNING : out.backend_frames_captured = video_event_wakes_;
configured_ ? GIGABYTE_STREAM_CONFIGURED : GIGABYTE_STREAM_STOPPED; out.backend_frames_delivered = frame_id_;
outStats.frames_captured = video_event_wakes_; out.backend_interrupt_count = video_event_wakes_;
outStats.frames_delivered = frame_id_; out.backend_wait_timeouts = wait_timeout_count_;
outStats.interrupt_count = video_event_wakes_; out.backend_running = running_ ? 1 : 0;
outWaitTimeouts = wait_timeout_count_; out.backend_capture_active = running_ ? 1 : 0;
outDebugState = {}; out.backend_latest_sequence = video_event_wakes_;
outDebugState.running = running_ ? 1 : 0; out.backend_delivered_sequence = frame_id_;
outDebugState.capture_active = running_ ? 1 : 0; out.audio_dma_event_wakes = audio_event_wakes_;
outDebugState.latest_sequence = video_event_wakes_; out.extra_audio_event_wakes = extra_audio_event_wakes_;
outDebugState.delivered_sequence = frame_id_; out.audio_frames_from_driver = audio_frames_from_driver_;
outDebugState.audio_dma_event_wakes = audio_event_wakes_; out.audio_bytes_from_driver = audio_bytes_from_driver_;
outDebugState.extra_audio_event_wakes = extra_audio_event_wakes_; out.get_frame_zero_copy = zero_copy_enabled_ ? 1 : 0;
outDebugState.audio_frames_from_driver = audio_frames_from_driver_; out.get_frame_timing_samples = get_frame_timing_samples_;
outDebugState.audio_bytes_from_driver = audio_bytes_from_driver_; out.get_frame_timing_average_us = get_frame_timing_samples_
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_
? get_frame_timing_total_us_ / static_cast<double>(get_frame_timing_samples_) : 0.0; ? 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_; out.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_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(); const gvfg_status_t openStatus = ensure_vendor_channel_open();
if (openStatus != GIGABYTE_OK) if (openStatus != GVFG_OK)
return openStatus; return openStatus;
if (!gigabyte_read_register(device_handle_, offset, outValue)) if (!gigabyte_read_register(device_handle_, offset, outValue))
return reject(GIGABYTE_EIO, return reject(GVFG_EIO,
"GigabyteLib gap: driver extension register read failed"); "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(); const gvfg_status_t openStatus = ensure_vendor_channel_open();
if (openStatus != GIGABYTE_OK) if (openStatus != GVFG_OK)
return openStatus; return openStatus;
if (!gigabyte_write_register(device_handle_, offset, value)) if (!gigabyte_write_register(device_handle_, offset, value))
return reject(GIGABYTE_EIO, return reject(GVFG_EIO,
"GigabyteLib gap: driver extension register write failed"); "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) if (result == GVFG_HRESULT_OK)
return GIGABYTE_OK; return GVFG_OK;
gigabyte_status_t status = GIGABYTE_EIO; gvfg_status_t status = GVFG_EIO;
if (result == GVFG_HRESULT_DEV_BUSY || result == GVFG_HRESULT_CONTEXT_ERROR) 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) else if (result == GVFG_HRESULT_VIDEO_CHN_INVALID)
status = GIGABYTE_EINVAL; status = GVFG_EINVAL;
else if (result == GVFG_HRESULT_DEV_ERROR) else if (result == GVFG_HRESULT_DEV_ERROR)
status = GIGABYTE_ENODEV; status = GVFG_ENODEV;
std::ostringstream stream; std::ostringstream stream;
stream << (operation ? operation : "GigabyteLib") stream << (operation ? operation : "GigabyteLib")
<< " failed with GVFG_HRESULT 0x" << std::hex << std::uppercase << " 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; 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"); error_state_.set(message ? message : "GigabyteLib operation rejected");
return status; return status;
@@ -642,8 +627,8 @@ size_t GigabyteCaptureSession::frame_size_bytes() const
std::lock_guard<std::mutex> lock(state_mutex_); std::lock_guard<std::mutex> lock(state_mutex_);
const uint32_t width = cached_signal_.width; const uint32_t width = cached_signal_.width;
const uint32_t height = cached_signal_.height; const uint32_t height = cached_signal_.height;
const gigabyte_pixel_format_t format = cached_signal_.pixel_format; const int format = cached_signal_.pixel_format;
const size_t bytesPerPixel = format == GIGABYTE_PIXFMT_Y210 ? 4u : 2u; const size_t bytesPerPixel = format == GVFG_PIXFMT_Y210 ? 4u : 2u;
return static_cast<size_t>(width) * height * bytesPerPixel; return static_cast<size_t>(width) * height * bytesPerPixel;
} }
@@ -1,8 +1,9 @@
#pragma once #pragma once
#include "gigabyte_types.h"
#include "gigabyte_device.h" #include "gigabyte_device.h"
#include "gvfg_error_state.h" #include "gvfg_error_state.h"
#include "gvfg_capture.h"
#include "gvfg_debug.h"
#include "gvfgsdkapi.h" #include "gvfgsdkapi.h"
#include <windows.h> #include <windows.h>
@@ -10,7 +11,6 @@
#include <chrono> #include <chrono>
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <memory>
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <thread> #include <thread>
@@ -18,11 +18,17 @@
namespace gvfg::internal 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 class GigabyteCaptureSession
{ {
public: public:
@@ -31,53 +37,52 @@ namespace gvfg::internal
GigabyteCaptureSession &operator=(const GigabyteCaptureSession &) = delete; GigabyteCaptureSession &operator=(const GigabyteCaptureSession &) = delete;
~GigabyteCaptureSession(); ~GigabyteCaptureSession();
gigabyte_status_t open_device_index(size_t deviceIndex); gvfg_status_t open_device_index(size_t deviceIndex);
gigabyte_status_t open_device_connection(const std::shared_ptr<GigabyteDeviceConnection> &connection); gvfg_status_t open_device_path(const std::wstring &devicePath);
std::shared_ptr<GigabyteDeviceConnection> device_connection() const { return device_connection_; } const std::wstring &device_path() const { return device_path_; }
gigabyte_status_t close(); gvfg_status_t close();
gigabyte_status_t set_channel(uint32_t channel); gvfg_status_t set_channel(uint32_t channel);
gigabyte_status_t set_zero_copy_enabled(bool enabled); gvfg_status_t set_zero_copy_enabled(bool enabled);
bool zero_copy_enabled() const { return zero_copy_enabled_; } bool zero_copy_enabled() const { return zero_copy_enabled_; }
gigabyte_status_t set_audio_enabled(bool enabled); gvfg_status_t set_audio_enabled(bool enabled);
gigabyte_status_t set_video_format(gigabyte_pixel_format_t format); gvfg_status_t set_video_format(gvfg_pixel_format_t format);
gigabyte_status_t get_signal_status(gigabyte_signal_status_t &out) const; gvfg_status_t get_signal_status(gvfg_signal_status_t &out) const;
gigabyte_status_t get_audio_format(gigabyte_audio_format_t &out) const; gvfg_status_t get_audio_format(GigabyteAudioInfo &out) const;
gigabyte_status_t set_event_callback(gigabyte_event_callback_t callback, void *user, uint32_t eventMask); gvfg_status_t set_event_callback(GigabyteEventCallback callback, void *user, uint32_t eventMask);
gigabyte_status_t configure_stream(); gvfg_status_t configure_stream();
gigabyte_status_t start_stream(); gvfg_status_t start_stream();
gigabyte_status_t stop_stream(); gvfg_status_t stop_stream();
gigabyte_status_t wait_frame(uint32_t timeoutMs, gigabyte_frame_t &out); gvfg_status_t wait_frame(uint32_t timeoutMs, gvfg_frame_t &out);
gigabyte_status_t wait_audio(uint32_t timeoutMs, void *destination, gvfg_status_t wait_audio(uint32_t timeoutMs, void *destination,
uint32_t destinationCapacity, uint32_t &outBytes); uint32_t destinationCapacity, uint32_t &outBytes);
gigabyte_status_t release_frame(const gigabyte_frame_t &frame); gvfg_status_t release_frame();
void get_debug_stats(gigabyte_stream_stats_t &outStats, uint64_t &outWaitTimeouts, void fill_debug_stats(gvfg_debug_backend_stats_t &out) const;
gigabyte_debug_state_t &outDebugState) const; gvfg_status_t debug_read_register(uint32_t offset, uint32_t &outValue) const;
gigabyte_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;
gigabyte_status_t debug_write_register(uint32_t offset, uint32_t value) const;
private: private:
gigabyte_status_t open_device(const GigabyteDevice &device); gvfg_status_t open_device(const GigabyteDevice &device);
gigabyte_status_t ensure_vendor_channel_open() const; gvfg_status_t ensure_vendor_channel_open() const;
void close_vendor_channel() const; void close_vendor_channel() const;
void start_event_monitoring(); void start_event_monitoring();
void stop_event_monitoring(); void stop_event_monitoring();
void event_thread_proc(); void event_thread_proc();
void emit_event(gigabyte_event_type_t type) const; void emit_event(gvfg_event_type_t type) const;
gigabyte_status_t refresh_video_info() const; gvfg_status_t refresh_video_info() const;
gigabyte_status_t from_vendor(GVFG_HRESULT result, const char *operation) const; gvfg_status_t from_vendor(GVFG_HRESULT result, const char *operation) const;
gigabyte_status_t reject(gigabyte_status_t status, const char *message) const; gvfg_status_t reject(gvfg_status_t status, const char *message) const;
void record_get_frame_timing(double elapsedUs); void record_get_frame_timing(double elapsedUs);
size_t frame_size_bytes() const; size_t frame_size_bytes() const;
ChannelErrorState &error_state_; ChannelErrorState &error_state_;
std::shared_ptr<GigabyteDeviceConnection> device_connection_; std::wstring device_path_;
mutable HANDLE device_handle_ = INVALID_HANDLE_VALUE; mutable HANDLE device_handle_ = INVALID_HANDLE_VALUE;
mutable PGVFG_CONTEXT context_ = nullptr; mutable PGVFG_CONTEXT context_ = nullptr;
mutable GVFG_VIDEO_CHN_EVENT events_{}; mutable GVFG_VIDEO_CHN_EVENT events_{};
mutable HANDLE stop_event_ = nullptr; mutable HANDLE stop_event_ = nullptr;
mutable bool events_created_ = false; mutable bool events_created_ = false;
mutable bool channel_open_ = 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_{}; mutable GVFG_AUDIO_INFO audio_info_{};
uint32_t channel_ = 0; uint32_t channel_ = 0;
bool configured_ = false; bool configured_ = false;
@@ -87,12 +92,11 @@ namespace gvfg::internal
std::atomic<bool> monitoring_{false}; std::atomic<bool> monitoring_{false};
std::thread event_thread_; std::thread event_thread_;
mutable std::mutex callback_mutex_; mutable std::mutex callback_mutex_;
gigabyte_event_callback_t event_callback_ = nullptr; GigabyteEventCallback event_callback_ = nullptr;
void *event_callback_user_ = 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_; mutable std::mutex state_mutex_;
std::vector<uint8_t> copy_buffer_; std::vector<uint8_t> copy_buffer_;
gigabyte_frame_t held_frame_{};
bool frame_held_ = false; bool frame_held_ = false;
uint64_t frame_id_ = 0; uint64_t frame_id_ = 0;
uint64_t audio_frames_from_driver_ = 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);
+88 -175
View File
@@ -27,11 +27,6 @@ using namespace gvfg::internal;
namespace namespace
{ {
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 #if INTPTR_MAX == INT64_MAX
static_assert(sizeof(gvfg_audio_format_t) == 12, "gvfg_audio_format_t ABI must remain frozen"); static_assert(sizeof(gvfg_audio_format_t) == 12, "gvfg_audio_format_t ABI must remain frozen");
static_assert(std::is_standard_layout_v<gvfg_audio_frame_t>); static_assert(std::is_standard_layout_v<gvfg_audio_frame_t>);
@@ -70,28 +65,6 @@ namespace
return static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(now).count()); return static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(now).count());
} }
gvfg_status_t map_status(gigabyte_status_t st)
{
switch (st)
{
case GIGABYTE_OK:
return GVFG_OK;
case GIGABYTE_EINVAL:
return GVFG_EINVAL;
case GIGABYTE_ENODEV:
return GVFG_ENODEV;
case GIGABYTE_ESTATE:
return GVFG_ESTATE;
case GIGABYTE_ETIMEOUT:
return GVFG_ETIMEOUT;
case GIGABYTE_ENOTSUP:
return GVFG_ENOTSUP;
case GIGABYTE_EIO:
default:
return GVFG_EIO;
}
}
void copy_wide_to_utf8(const std::wstring &src, char *dst, size_t dstSize) void copy_wide_to_utf8(const std::wstring &src, char *dst, size_t dstSize)
{ {
if (!dst || dstSize == 0) if (!dst || dstSize == 0)
@@ -103,36 +76,6 @@ namespace
dst[dstSize - 1] = '\0'; dst[dstSize - 1] = '\0';
} }
gvfg_event_type_t map_event_type(gigabyte_event_type_t type)
{
switch (type)
{
case GIGABYTE_EVENT_PLUG_IN:
return GVFG_EVENT_SIGNAL_CONNECTED;
case GIGABYTE_EVENT_PLUG_OUT:
return GVFG_EVENT_SIGNAL_DISCONNECTED;
case GIGABYTE_EVENT_STREAM_READY:
return GVFG_EVENT_STREAM_READY;
case GIGABYTE_EVENT_FORMAT_CHANGE_BEGIN:
return GVFG_EVENT_FORMAT_CHANGE_BEGIN;
default:
return GVFG_EVENT_UNKNOWN;
}
}
int to_gvfg_pixel_format(gigabyte_pixel_format_t fmt)
{
switch (fmt)
{
case GIGABYTE_PIXFMT_YUY2:
return GVFG_PIXFMT_YUY2;
case GIGABYTE_PIXFMT_Y210:
return GVFG_PIXFMT_Y210;
default:
return GVFG_PIXFMT_UNKNOWN;
}
}
const char *pixel_format_name(int fmt) const char *pixel_format_name(int fmt)
{ {
switch (fmt) switch (fmt)
@@ -155,13 +98,13 @@ namespace
} }
gvfg_status_t native_row_stride_bytes(uint32_t width, gvfg_status_t native_row_stride_bytes(uint32_t width,
gigabyte_pixel_format_t pixelFormat, int pixelFormat,
int &outStride) int &outStride)
{ {
uint64_t bytesPerPixel = 0; uint64_t bytesPerPixel = 0;
if (pixelFormat == GIGABYTE_PIXFMT_YUY2) if (pixelFormat == GVFG_PIXFMT_YUY2)
bytesPerPixel = 2; bytesPerPixel = 2;
else if (pixelFormat == GIGABYTE_PIXFMT_Y210) else if (pixelFormat == GVFG_PIXFMT_Y210)
bytesPerPixel = 4; bytesPerPixel = 4;
else else
return GVFG_ENOTSUP; return GVFG_ENOTSUP;
@@ -191,40 +134,40 @@ struct gvfg_channel_session_t
gvfg_status_t open(int index, gvfg_status_t open(int index,
int channelIndex = GVFG_CHANNEL_0, int channelIndex = GVFG_CHANNEL_0,
const std::shared_ptr<gvfg::internal::GigabyteDeviceConnection> &deviceConnection = nullptr) const std::wstring *devicePath = nullptr)
{ {
if (index < 0 || (channelIndex != GVFG_CHANNEL_0 && channelIndex != GVFG_CHANNEL_1)) 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"); return reject(GVFG_EINVAL, "gvfg_open_channel rejected: device or channel index is invalid");
close(); close();
session = std::make_unique<gvfg::internal::GigabyteCaptureSession>(errorState); session = std::make_unique<gvfg::internal::GigabyteCaptureSession>(errorState);
const gigabyte_status_t stOpen = deviceConnection const gvfg_status_t stOpen = devicePath
? session->open_device_connection(deviceConnection) ? session->open_device_path(*devicePath)
: session->open_device_index(static_cast<size_t>(index)); : session->open_device_index(static_cast<size_t>(index));
if (stOpen != GIGABYTE_OK) if (stOpen != GVFG_OK)
{ {
session.reset(); session.reset();
return map_status(stOpen); return stOpen;
} }
currentIndex = index; currentIndex = index;
syncSessionEventCallback(); syncSessionEventCallback();
selectedChannel = static_cast<uint32_t>(channelIndex); selectedChannel = static_cast<uint32_t>(channelIndex);
resetRuntimeCounters(); resetRuntimeCounters();
const gigabyte_status_t stChannel = session->set_channel(selectedChannel); const gvfg_status_t stChannel = session->set_channel(selectedChannel);
if (stChannel != GIGABYTE_OK) if (stChannel != GVFG_OK)
{ {
close(); close();
return map_status(stChannel); return stChannel;
} }
if (zeroCopyRequested) if (zeroCopyRequested)
{ {
const gigabyte_status_t stZeroCopy = session->set_zero_copy_enabled(true); const gvfg_status_t stZeroCopy = session->set_zero_copy_enabled(true);
if (stZeroCopy != GIGABYTE_OK) if (stZeroCopy != GVFG_OK)
{ {
close(); close();
return map_status(stZeroCopy); return stZeroCopy;
} }
} }
@@ -259,9 +202,9 @@ struct gvfg_channel_session_t
eventQueue.clear(); eventQueue.clear();
} }
const gigabyte_status_t st = session->start_stream(); const gvfg_status_t st = session->start_stream();
if (st != GIGABYTE_OK) if (st != GVFG_OK)
return map_status(st); return st;
running = true; running = true;
return GVFG_OK; return GVFG_OK;
@@ -273,7 +216,7 @@ struct gvfg_channel_session_t
releaseHeldFrameForStop(); releaseHeldFrameForStop();
releaseHeldAudioFrameForStop(); releaseHeldAudioFrameForStop();
gigabyte_status_t st = GIGABYTE_OK; gvfg_status_t st = GVFG_OK;
if (session) if (session)
{ {
// stop_stream() joins the session event thread, so no new event can // stop_stream() joins the session event thread, so no new event can
@@ -286,7 +229,7 @@ struct gvfg_channel_session_t
eventQueue.clear(); eventQueue.clear();
} }
eventCv.notify_all(); eventCv.notify_all();
return map_status(st); return st;
} }
void close() void close()
@@ -306,17 +249,17 @@ struct gvfg_channel_session_t
if (!session) if (!session)
return reject(GVFG_ESTATE, "gvfg_get_channel_signal_status rejected: channel is not open"); return reject(GVFG_ESTATE, "gvfg_get_channel_signal_status rejected: channel is not open");
std::memset(&out, 0, sizeof(out)); std::memset(&out, 0, sizeof(out));
const gigabyte_status_t status = querySignal(); const gvfg_status_t status = querySignal();
{ {
std::lock_guard<std::mutex> lock(stateMutex); std::lock_guard<std::mutex> lock(stateMutex);
out.connected = signalConnected ? 1 : 0; out.connected = signalConnected ? 1 : 0;
out.channel = static_cast<int>(selectedChannel); out.channel = static_cast<int>(selectedChannel);
out.width = static_cast<int>(width); out.width = static_cast<int>(width);
out.height = static_cast<int>(height); out.height = static_cast<int>(height);
out.pixel_format = to_gvfg_pixel_format(pixelFormat); out.pixel_format = static_cast<int>(pixelFormat);
out.bit_depth = static_cast<int>(bitDepth); out.bit_depth = static_cast<int>(bitDepth);
} }
return map_status(status); return status;
} }
gvfg_status_t getRuntimeInfo(gvfg_runtime_info_t &out) gvfg_status_t getRuntimeInfo(gvfg_runtime_info_t &out)
@@ -327,15 +270,13 @@ struct gvfg_channel_session_t
std::memset(&out, 0, sizeof(out)); std::memset(&out, 0, sizeof(out));
out.capture_fps = runtimeFps.load(std::memory_order_relaxed); out.capture_fps = runtimeFps.load(std::memory_order_relaxed);
out.delivered_frames = deliveredFrames.load(std::memory_order_relaxed); out.delivered_frames = deliveredFrames.load(std::memory_order_relaxed);
gigabyte_stream_stats_t stats{}; gvfg_debug_backend_stats_t stats{};
gigabyte_debug_state_t debugState{}; session->fill_debug_stats(stats);
uint64_t waitTimeouts = 0; out.zero_copy_enabled = stats.get_frame_zero_copy;
session->get_debug_stats(stats, waitTimeouts, debugState); out.driver_read_samples = stats.get_frame_timing_samples;
out.zero_copy_enabled = debugState.get_frame_zero_copy; out.driver_read_average_us = stats.get_frame_timing_average_us;
out.driver_read_samples = debugState.get_frame_timing_samples; out.driver_read_max300_us = stats.get_frame_timing_max300_us;
out.driver_read_average_us = debugState.get_frame_timing_average_us; out.driver_read_max_us = stats.get_frame_timing_max_us;
out.driver_read_max300_us = debugState.get_frame_timing_max300_us;
out.driver_read_max_us = debugState.get_frame_timing_max_us;
return GVFG_OK; return GVFG_OK;
} }
@@ -348,7 +289,7 @@ struct gvfg_channel_session_t
eventMask); eventMask);
} }
static void onSessionEvent(gigabyte_event_type_t event, void *user) static void onSessionEvent(gvfg_event_type_t event, void *user)
{ {
auto *self = static_cast<gvfg_channel_session_t *>(user); auto *self = static_cast<gvfg_channel_session_t *>(user);
if (!self) if (!self)
@@ -356,35 +297,36 @@ struct gvfg_channel_session_t
self->emitEvent(event); self->emitEvent(event);
} }
void emitEvent(gigabyte_event_type_t event) void emitEvent(gvfg_event_type_t event)
{ {
const gvfg_event_type_t type = map_event_type(event);
{ {
std::lock_guard<std::mutex> lock(eventMutex); std::lock_guard<std::mutex> lock(eventMutex);
if (eventQueue.size() >= 64) if (eventQueue.size() >= 64)
eventQueue.pop_front(); eventQueue.pop_front();
gvfg_event_t out{}; gvfg_event_t out{};
out.struct_size = sizeof(out); out.struct_size = sizeof(out);
out.type = type; out.type = event;
eventQueue.push_back(out); eventQueue.push_back(out);
} }
eventCv.notify_one(); eventCv.notify_one();
} }
gigabyte_status_t querySignal() gvfg_status_t querySignal()
{ {
if (!session) if (!session)
return GIGABYTE_ESTATE; return GVFG_ESTATE;
gigabyte_signal_status_t sig{}; gvfg_signal_status_t sig{};
const gigabyte_status_t status = session->get_signal_status(sig); const gvfg_status_t status = session->get_signal_status(sig);
std::lock_guard<std::mutex> lock(stateMutex); std::lock_guard<std::mutex> lock(stateMutex);
width = status == GIGABYTE_OK ? sig.width : 0; width = status == GVFG_OK ? sig.width : 0;
height = status == GIGABYTE_OK ? sig.height : 0; height = status == GVFG_OK ? sig.height : 0;
bitDepth = status == GIGABYTE_OK ? sig.bit_depth : 0; bitDepth = status == GVFG_OK ? sig.bit_depth : 0;
pixelFormat = status == GIGABYTE_OK ? sig.pixel_format : GIGABYTE_PIXFMT_UNKNOWN; pixelFormat = status == GVFG_OK
signalConnected = status == GIGABYTE_OK && sig.connected != 0; ? static_cast<gvfg_pixel_format_t>(sig.pixel_format)
: GVFG_PIXFMT_UNKNOWN;
signalConnected = status == GVFG_OK && sig.connected != 0;
return status; return status;
} }
@@ -393,13 +335,13 @@ struct gvfg_channel_session_t
if (!session) if (!session)
return reject(GVFG_ESTATE, "configure stream rejected: channel is not open"); return reject(GVFG_ESTATE, "configure stream rejected: channel is not open");
const gigabyte_status_t signalStatus = querySignal(); const gvfg_status_t signalStatus = querySignal();
if (signalStatus != GIGABYTE_OK) if (signalStatus != GVFG_OK)
return map_status(signalStatus); return signalStatus;
const gigabyte_status_t st = session->configure_stream(); const gvfg_status_t st = session->configure_stream();
if (st != GIGABYTE_OK) if (st != GVFG_OK)
return map_status(st); return st;
return GVFG_OK; return GVFG_OK;
} }
@@ -424,20 +366,20 @@ struct gvfg_channel_session_t
if (stateError) if (stateError)
return reject(GVFG_ESTATE, stateError); return reject(GVFG_ESTATE, stateError);
gigabyte_frame_t frame{}; gvfg_frame_t frame{};
const gigabyte_status_t st = session->wait_frame(timeoutMs, frame); const gvfg_status_t st = session->wait_frame(timeoutMs, frame);
if (st != GIGABYTE_OK) if (st != GVFG_OK)
{ {
{ {
std::lock_guard<std::mutex> lock(frameMutex); std::lock_guard<std::mutex> lock(frameMutex);
readInProgress = false; readInProgress = false;
} }
return map_status(st); return st;
} }
if (!frame.data || frame.width == 0 || frame.height == 0) if (!frame.data || frame.width == 0 || frame.height == 0)
{ {
session->release_frame(frame); session->release_frame();
std::lock_guard<std::mutex> lock(frameMutex); std::lock_guard<std::mutex> lock(frameMutex);
readInProgress = false; readInProgress = false;
return reject(GVFG_EIO, "gvfg_read_channel_frame received an invalid frame descriptor"); return reject(GVFG_EIO, "gvfg_read_channel_frame received an invalid frame descriptor");
@@ -460,27 +402,27 @@ struct gvfg_channel_session_t
} }
if (stoppedWhileWaiting) if (stoppedWhileWaiting)
{ {
session->release_frame(frame); session->release_frame();
return GVFG_ESTATE; return GVFG_ESTATE;
} }
updateRuntimeFps(now_ns()); updateRuntimeFps(now_ns());
out.data = frame.data; out.data = frame.data;
out.data_size = static_cast<uint64_t>(frame.data_size_bytes); out.data_size = static_cast<uint64_t>(frame.data_size);
out.width = static_cast<int>(frame.width); out.width = static_cast<int>(frame.width);
out.height = static_cast<int>(frame.height); out.height = static_cast<int>(frame.height);
const gvfg_status_t strideStatus = const gvfg_status_t strideStatus =
native_row_stride_bytes(frame.width, frame.pixel_format, out.row_stride_bytes); native_row_stride_bytes(frame.width, frame.pixel_format, out.row_stride_bytes);
if (strideStatus != GVFG_OK) if (strideStatus != GVFG_OK)
{ {
session->release_frame(frame); session->release_frame();
std::lock_guard<std::mutex> lock(frameMutex); std::lock_guard<std::mutex> lock(frameMutex);
heldSessionFrame = {}; heldSessionFrame = {};
frameHeld = false; frameHeld = false;
return reject(strideStatus, "gvfg_read_channel_frame received an invalid row stride"); return reject(strideStatus, "gvfg_read_channel_frame received an invalid row stride");
} }
out.pixel_format = to_gvfg_pixel_format(frame.pixel_format); out.pixel_format = frame.pixel_format;
out.bit_depth = static_cast<int>(frame.bit_depth); out.bit_depth = static_cast<int>(frame.bit_depth);
out.frame_id = frame.frame_id; out.frame_id = frame.frame_id;
out.timestamp_ns = frame.timestamp_ns; out.timestamp_ns = frame.timestamp_ns;
@@ -506,19 +448,19 @@ struct gvfg_channel_session_t
return reject(GVFG_ESTATE, "gvfg_release_channel_frame rejected: held frame has an invalid row stride"); return reject(GVFG_ESTATE, "gvfg_release_channel_frame rejected: held frame has an invalid row stride");
if (frameToken.data != heldSessionFrame.data || if (frameToken.data != heldSessionFrame.data ||
frameToken.data_size != static_cast<uint64_t>(heldSessionFrame.data_size_bytes) || frameToken.data_size != static_cast<uint64_t>(heldSessionFrame.data_size) ||
frameToken.width != static_cast<int>(heldSessionFrame.width) || frameToken.width != static_cast<int>(heldSessionFrame.width) ||
frameToken.height != static_cast<int>(heldSessionFrame.height) || frameToken.height != static_cast<int>(heldSessionFrame.height) ||
frameToken.row_stride_bytes != expectedStride || frameToken.row_stride_bytes != expectedStride ||
frameToken.pixel_format != to_gvfg_pixel_format(heldSessionFrame.pixel_format) || frameToken.pixel_format != heldSessionFrame.pixel_format ||
frameToken.bit_depth != static_cast<int>(heldSessionFrame.bit_depth) || frameToken.bit_depth != static_cast<int>(heldSessionFrame.bit_depth) ||
frameToken.frame_id != heldSessionFrame.frame_id || frameToken.frame_id != heldSessionFrame.frame_id ||
frameToken.timestamp_ns != heldSessionFrame.timestamp_ns) frameToken.timestamp_ns != heldSessionFrame.timestamp_ns)
return reject(GVFG_EINVAL, "gvfg_release_channel_frame rejected: frame token does not match the held frame"); return reject(GVFG_EINVAL, "gvfg_release_channel_frame rejected: frame token does not match the held frame");
const gigabyte_status_t st = session->release_frame(heldSessionFrame); const gvfg_status_t st = session->release_frame();
if (st != GIGABYTE_OK) if (st != GVFG_OK)
return map_status(st); return st;
heldSessionFrame = {}; heldSessionFrame = {};
frameHeld = false; frameHeld = false;
@@ -532,7 +474,7 @@ struct gvfg_channel_session_t
if (!session) if (!session)
return; return;
gigabyte_frame_t frame{}; gvfg_frame_t frame{};
bool shouldRelease = false; bool shouldRelease = false;
{ {
std::lock_guard<std::mutex> lock(frameMutex); std::lock_guard<std::mutex> lock(frameMutex);
@@ -546,7 +488,7 @@ struct gvfg_channel_session_t
} }
if (shouldRelease) if (shouldRelease)
session->release_frame(frame); session->release_frame();
} }
gvfg_status_t pollEvent(gvfg_event_t &out, uint32_t timeoutMs) gvfg_status_t pollEvent(gvfg_event_t &out, uint32_t timeoutMs)
@@ -619,29 +561,7 @@ struct gvfg_channel_session_t
if (session) if (session)
{ {
gigabyte_stream_stats_t stats{}; session->fill_debug_stats(out);
gigabyte_debug_state_t debugState{};
uint64_t waitTimeouts = 0;
session->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;
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;
out.audio_bytes_from_driver = debugState.audio_bytes_from_driver;
out.get_frame_zero_copy = debugState.get_frame_zero_copy;
out.get_frame_timing_samples = debugState.get_frame_timing_samples;
out.get_frame_timing_average_us = debugState.get_frame_timing_average_us;
out.get_frame_timing_max300_us = debugState.get_frame_timing_max300_us;
out.get_frame_timing_max_us = debugState.get_frame_timing_max_us;
} }
return GVFG_OK; return GVFG_OK;
@@ -651,7 +571,7 @@ struct gvfg_channel_session_t
{ {
if (!session) if (!session)
return reject(GVFG_ESTATE, "debug register read rejected: channel is not open"); return reject(GVFG_ESTATE, "debug register read rejected: channel is not open");
return map_status(session->debug_read_register(offset, outValue)); return session->debug_read_register(offset, outValue);
} }
gvfg_status_t setVideoFormat(gvfg_pixel_format_t format) gvfg_status_t setVideoFormat(gvfg_pixel_format_t format)
@@ -659,14 +579,9 @@ struct gvfg_channel_session_t
if (!session) if (!session)
return reject(GVFG_ESTATE, "gvfg_set_channel_video_format rejected: channel is not open"); return reject(GVFG_ESTATE, "gvfg_set_channel_video_format rejected: channel is not open");
gigabyte_pixel_format_t nativeFormat = GIGABYTE_PIXFMT_UNKNOWN; if (format != GVFG_PIXFMT_YUY2 && format != GVFG_PIXFMT_Y210)
if (format == GVFG_PIXFMT_YUY2)
nativeFormat = GIGABYTE_PIXFMT_YUY2;
else if (format == GVFG_PIXFMT_Y210)
nativeFormat = GIGABYTE_PIXFMT_Y210;
else
return reject(GVFG_EINVAL, "gvfg_set_channel_video_format rejected: pixel format is invalid"); return reject(GVFG_EINVAL, "gvfg_set_channel_video_format rejected: pixel format is invalid");
return map_status(session->set_video_format(nativeFormat)); return session->set_video_format(format);
} }
gvfg_status_t setAudioEnabled(bool enabled) gvfg_status_t setAudioEnabled(bool enabled)
@@ -675,9 +590,9 @@ struct gvfg_channel_session_t
return reject(GVFG_ESTATE, "gvfg_set_channel_audio_enabled rejected: channel is not open"); return reject(GVFG_ESTATE, "gvfg_set_channel_audio_enabled rejected: channel is not open");
if (running) if (running)
return reject(GVFG_ESTATE, "gvfg_set_channel_audio_enabled rejected: channel is running"); return reject(GVFG_ESTATE, "gvfg_set_channel_audio_enabled rejected: channel is running");
const gigabyte_status_t status = session->set_audio_enabled(enabled); const gvfg_status_t status = session->set_audio_enabled(enabled);
if (status != GIGABYTE_OK) if (status != GVFG_OK)
return map_status(status); return status;
audioEnabled = enabled; audioEnabled = enabled;
return GVFG_OK; return GVFG_OK;
} }
@@ -686,7 +601,7 @@ struct gvfg_channel_session_t
{ {
if (!session) if (!session)
return reject(GVFG_ESTATE, "debug register write rejected: channel is not open"); return reject(GVFG_ESTATE, "debug register write rejected: channel is not open");
return map_status(session->debug_write_register(offset, value)); return session->debug_write_register(offset, value);
} }
gvfg_status_t getAudioFormat(gvfg_audio_format_t &out) gvfg_status_t getAudioFormat(gvfg_audio_format_t &out)
@@ -694,10 +609,10 @@ struct gvfg_channel_session_t
std::memset(&out, 0, sizeof(out)); std::memset(&out, 0, sizeof(out));
if (!session) if (!session)
return reject(GVFG_ESTATE, "gvfg_get_channel_audio_format rejected: channel is not open"); return reject(GVFG_ESTATE, "gvfg_get_channel_audio_format rejected: channel is not open");
gigabyte_audio_format_t format{}; GigabyteAudioInfo format{};
const gigabyte_status_t status = session->get_audio_format(format); const gvfg_status_t status = session->get_audio_format(format);
if (status != GIGABYTE_OK) if (status != GVFG_OK)
return map_status(status); return status;
out.sample_rate = format.sample_rate; out.sample_rate = format.sample_rate;
out.channels = format.channels; out.channels = format.channels;
out.bits_per_sample = format.bits_per_sample; out.bits_per_sample = format.bits_per_sample;
@@ -721,10 +636,10 @@ struct gvfg_channel_session_t
audioReadInProgress = true; audioReadInProgress = true;
} }
gigabyte_audio_format_t format{}; GigabyteAudioInfo format{};
gigabyte_status_t status = session->get_audio_format(format); gvfg_status_t status = session->get_audio_format(format);
uint32_t bytes = 0; uint32_t bytes = 0;
if (status == GIGABYTE_OK) if (status == GVFG_OK)
{ {
audioBuffer.resize(format.frame_bytes); audioBuffer.resize(format.frame_bytes);
status = session->wait_audio(timeoutMs, audioBuffer.data(), status = session->wait_audio(timeoutMs, audioBuffer.data(),
@@ -734,8 +649,8 @@ struct gvfg_channel_session_t
{ {
std::lock_guard<std::mutex> lock(audioMutex); std::lock_guard<std::mutex> lock(audioMutex);
audioReadInProgress = false; audioReadInProgress = false;
if (status != GIGABYTE_OK) if (status != GVFG_OK)
return map_status(status); return status;
if (!running.load(std::memory_order_acquire) || bytes == 0 || bytes > audioBuffer.size()) 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"); return reject(GVFG_ESTATE, "gvfg_read_channel_audio_frame completed after capture stopped or returned invalid data");
audioFrameHeld = true; audioFrameHeld = true;
@@ -836,7 +751,7 @@ struct gvfg_channel_session_t
uint32_t width = 0; uint32_t width = 0;
uint32_t height = 0; uint32_t height = 0;
uint32_t bitDepth = 0; uint32_t bitDepth = 0;
gigabyte_pixel_format_t pixelFormat = GIGABYTE_PIXFMT_UNKNOWN; gvfg_pixel_format_t pixelFormat = GVFG_PIXFMT_UNKNOWN;
bool signalConnected = false; bool signalConnected = false;
mutable std::mutex stateMutex; mutable std::mutex stateMutex;
std::atomic<uint64_t> fpsWindowStartNs{0}; std::atomic<uint64_t> fpsWindowStartNs{0};
@@ -852,7 +767,7 @@ struct gvfg_channel_session_t
std::mutex frameMutex; std::mutex frameMutex;
bool readInProgress = false; bool readInProgress = false;
bool frameHeld = false; bool frameHeld = false;
gigabyte_frame_t heldSessionFrame{}; gvfg_frame_t heldSessionFrame{};
std::mutex audioMutex; std::mutex audioMutex;
std::vector<uint8_t> audioBuffer; std::vector<uint8_t> audioBuffer;
bool audioReadInProgress = false; bool audioReadInProgress = false;
@@ -934,14 +849,12 @@ struct gvfg_handle_t
const gvfg_status_t status = channel->open(index, const gvfg_status_t status = channel->open(index,
channelIndex, channelIndex,
deviceConnection); devicePath.empty() ? nullptr : &devicePath);
if (status != GVFG_OK) if (status != GVFG_OK)
return status; return status;
if (!deviceConnection) if (devicePath.empty())
{ devicePath = channel->session->device_path();
deviceConnection = channel->session->device_connection();
}
channels[slot] = std::move(channel); channels[slot] = std::move(channel);
currentIndex = index; currentIndex = index;
return GVFG_OK; return GVFG_OK;
@@ -1036,7 +949,7 @@ struct gvfg_handle_t
} }
std::array<ChannelErrorState, 2> channelErrors; std::array<ChannelErrorState, 2> channelErrors;
std::shared_ptr<gvfg::internal::GigabyteDeviceConnection> deviceConnection; std::wstring devicePath;
std::array<std::unique_ptr<gvfg_channel_session_t>, 2> channels; 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}; std::array<uint32_t, 2> eventMasks{GVFG_EVENT_MASK_ALL, GVFG_EVENT_MASK_ALL};
int currentIndex = -1; int currentIndex = -1;