- Read Windows PCI location paths for capture devices. - Remove per-query HDMI video debug output and simplify frame descriptor transfer. - Document repository workflow and validation in AGENTS.md. Validation: git diff --cached --check passed. Build and runtime tests were not run for this commit.
1568 lines
58 KiB
C++
1568 lines
58 KiB
C++
#include "gvfg_capture.h"
|
|
|
|
#ifdef GVFG_CAPTURE_TEST_RUNTIME
|
|
#include "gvfg_test_device.h"
|
|
#endif
|
|
|
|
#include "gvfg_debug.h"
|
|
#include "gigabyte_capture_session.h"
|
|
#include "gvfg_frame_processor.h"
|
|
#include "gvfgframework.h"
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <climits>
|
|
#include <condition_variable>
|
|
#include <cstddef>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <deque>
|
|
#include <limits>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <string>
|
|
#include <type_traits>
|
|
#include <vector>
|
|
|
|
#ifndef GVFG_VERSION_STRING
|
|
#define GVFG_VERSION_STRING "0.0.0"
|
|
#endif
|
|
|
|
using namespace gvfg::internal;
|
|
|
|
namespace
|
|
{
|
|
#if INTPTR_MAX == INT64_MAX
|
|
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(sizeof(gvfg_audio_frame_t) == 48, "gvfg_audio_frame_t x64 ABI must remain frozen");
|
|
static_assert(offsetof(gvfg_audio_frame_t, data) == 0);
|
|
static_assert(offsetof(gvfg_audio_frame_t, data_size) == 8);
|
|
static_assert(offsetof(gvfg_audio_frame_t, sample_rate) == 16);
|
|
static_assert(offsetof(gvfg_audio_frame_t, frame_id) == 32);
|
|
static_assert(offsetof(gvfg_audio_frame_t, timestamp_ns) == 40);
|
|
static_assert(sizeof(gvfg_runtime_info_t) == 16, "gvfg_runtime_info_t x64 ABI must remain frozen");
|
|
static_assert(std::is_standard_layout_v<gvfg_frame_t>);
|
|
static_assert(sizeof(gvfg_frame_t) == 56, "gvfg_frame_t x64 ABI must remain frozen");
|
|
static_assert(offsetof(gvfg_frame_t, data) == 0);
|
|
static_assert(offsetof(gvfg_frame_t, data_size) == 8);
|
|
static_assert(offsetof(gvfg_frame_t, width) == 16);
|
|
static_assert(offsetof(gvfg_frame_t, height) == 20);
|
|
static_assert(offsetof(gvfg_frame_t, row_stride_bytes) == 24);
|
|
static_assert(offsetof(gvfg_frame_t, pixel_format) == 28);
|
|
static_assert(offsetof(gvfg_frame_t, bit_depth) == 32);
|
|
static_assert(offsetof(gvfg_frame_t, frame_id) == 40);
|
|
static_assert(offsetof(gvfg_frame_t, timestamp_ns) == 48);
|
|
#endif
|
|
|
|
void copy_cstr(char *dst, size_t dstSize, const char *src)
|
|
{
|
|
if (!dst || dstSize == 0)
|
|
return;
|
|
dst[0] = 0;
|
|
if (src)
|
|
strncpy_s(dst, dstSize, src, _TRUNCATE);
|
|
}
|
|
|
|
uint64_t now_ns()
|
|
{
|
|
const auto now = std::chrono::steady_clock::now().time_since_epoch();
|
|
return static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(now).count());
|
|
}
|
|
|
|
void copy_wide_to_utf8(const std::wstring &src, char *dst, size_t dstSize)
|
|
{
|
|
if (!dst || dstSize == 0)
|
|
return;
|
|
dst[0] = '\0';
|
|
if (src.empty())
|
|
return;
|
|
WideCharToMultiByte(CP_UTF8, 0, src.c_str(), -1, dst, static_cast<int>(dstSize), nullptr, nullptr);
|
|
dst[dstSize - 1] = '\0';
|
|
}
|
|
|
|
const char *pixel_format_name(int fmt)
|
|
{
|
|
switch (fmt)
|
|
{
|
|
case GVFG_PIXFMT_YUY2:
|
|
return "YUY2";
|
|
case GVFG_PIXFMT_Y210:
|
|
return "Y210";
|
|
default:
|
|
return "UNKNOWN";
|
|
}
|
|
}
|
|
|
|
bool checked_mul_u64(uint64_t a, uint64_t b, uint64_t &out)
|
|
{
|
|
if (a != 0 && b > UINT64_MAX / a)
|
|
return false;
|
|
out = a * b;
|
|
return true;
|
|
}
|
|
|
|
gvfg_status_t native_row_stride_bytes(uint32_t width,
|
|
int pixelFormat,
|
|
int &outStride)
|
|
{
|
|
uint64_t bytesPerPixel = 0;
|
|
if (pixelFormat == GVFG_PIXFMT_YUY2)
|
|
bytesPerPixel = 2;
|
|
else if (pixelFormat == GVFG_PIXFMT_Y210)
|
|
bytesPerPixel = 4;
|
|
else
|
|
return GVFG_ENOTSUP;
|
|
|
|
uint64_t row = 0;
|
|
if (!checked_mul_u64(static_cast<uint64_t>(width), bytesPerPixel, row) ||
|
|
row > static_cast<uint64_t>(INT_MAX))
|
|
return GVFG_EINVAL;
|
|
|
|
outStride = static_cast<int>(row);
|
|
return GVFG_OK;
|
|
}
|
|
|
|
}
|
|
|
|
struct gvfg_channel_session_t
|
|
{
|
|
explicit gvfg_channel_session_t(ChannelErrorState &errorState)
|
|
: errorState(errorState)
|
|
{
|
|
}
|
|
|
|
~gvfg_channel_session_t()
|
|
{
|
|
close();
|
|
}
|
|
|
|
gvfg_status_t open(int index,
|
|
int channelIndex = GVFG_CHANNEL_0,
|
|
const std::wstring *devicePath = nullptr)
|
|
{
|
|
if (index < 0 || (channelIndex != GVFG_CHANNEL_0 && channelIndex != GVFG_CHANNEL_1))
|
|
return reject(GVFG_EINVAL, "gvfg_open_channel rejected: device or channel index is invalid");
|
|
close();
|
|
|
|
session = std::make_unique<gvfg::internal::GigabyteCaptureSession>(errorState);
|
|
const gvfg_status_t stOpen = devicePath
|
|
? session->open_device_path(*devicePath)
|
|
: session->open_device_index(static_cast<size_t>(index));
|
|
if (stOpen != GVFG_OK)
|
|
{
|
|
session.reset();
|
|
return stOpen;
|
|
}
|
|
|
|
syncSessionEventCallback();
|
|
selectedChannel = static_cast<uint32_t>(channelIndex);
|
|
resetRuntimeCounters();
|
|
const gvfg_status_t stChannel = session->set_channel(selectedChannel);
|
|
if (stChannel != GVFG_OK)
|
|
{
|
|
close();
|
|
return stChannel;
|
|
}
|
|
|
|
if (zeroCopyRequested)
|
|
{
|
|
const gvfg_status_t stZeroCopy = session->set_zero_copy_enabled(true);
|
|
if (stZeroCopy != GVFG_OK)
|
|
{
|
|
close();
|
|
return stZeroCopy;
|
|
}
|
|
}
|
|
|
|
// querySignal();
|
|
return GVFG_OK;
|
|
}
|
|
|
|
gvfg_status_t start()
|
|
{
|
|
if (!session)
|
|
return reject(GVFG_ESTATE, "gvfg_start_channel rejected: channel is not open");
|
|
|
|
// A new start request starts a new diagnostic lifetime. Any failure
|
|
// below will replace this with the detail for the failed operation.
|
|
errorState.clear();
|
|
if (running)
|
|
return GVFG_OK;
|
|
|
|
const gvfg_status_t cfg = configureStream();
|
|
if (cfg != GVFG_OK)
|
|
return cfg;
|
|
if (audioEnabled)
|
|
{
|
|
GigabyteAudioInfo format{};
|
|
const gvfg_status_t audioStatus = session->get_audio_format(format);
|
|
if (audioStatus != GVFG_OK)
|
|
return audioStatus;
|
|
}
|
|
|
|
resetRuntimeCounters();
|
|
{
|
|
std::lock_guard<std::mutex> lock(audioMutex);
|
|
heldAudioFrame = {};
|
|
audioFrameHeld = false;
|
|
}
|
|
{
|
|
std::lock_guard<std::mutex> lock(eventMutex);
|
|
eventQueue.clear();
|
|
}
|
|
|
|
const gvfg_status_t st = session->start_stream();
|
|
if (st != GVFG_OK)
|
|
return st;
|
|
|
|
running = true;
|
|
return GVFG_OK;
|
|
}
|
|
|
|
gvfg_status_t stop()
|
|
{
|
|
running = false;
|
|
releaseHeldFrameForStop();
|
|
releaseHeldAudioFrameForStop();
|
|
|
|
gvfg_status_t st = GVFG_OK;
|
|
if (session)
|
|
{
|
|
// stop_stream() joins the session event thread, so no new event can
|
|
// be queued after it returns.
|
|
st = session->stop_stream();
|
|
}
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lock(eventMutex);
|
|
eventQueue.clear();
|
|
}
|
|
eventCv.notify_all();
|
|
return st;
|
|
}
|
|
|
|
void close()
|
|
{
|
|
stop();
|
|
if (session)
|
|
{
|
|
session->set_event_callback(nullptr, nullptr);
|
|
session->close();
|
|
session.reset();
|
|
}
|
|
}
|
|
|
|
gvfg_status_t getSignalStatus(gvfg_signal_status_t &out)
|
|
{
|
|
if (!session)
|
|
return reject(GVFG_ESTATE, "gvfg_get_channel_signal_status rejected: channel is not open");
|
|
std::memset(&out, 0, sizeof(out));
|
|
const gvfg_status_t status = running ? syncCachedSignal() : querySignal();
|
|
{
|
|
std::lock_guard<std::mutex> lock(stateMutex);
|
|
out.connected = signalConnected ? 1 : 0;
|
|
out.channel = static_cast<int>(selectedChannel);
|
|
out.width = static_cast<int>(width);
|
|
out.height = static_cast<int>(height);
|
|
out.pixel_format = static_cast<int>(pixelFormat);
|
|
out.bit_depth = static_cast<int>(bitDepth);
|
|
out.video_interface = videoInterface;
|
|
}
|
|
return status;
|
|
}
|
|
|
|
gvfg_status_t getRuntimeInfo(gvfg_runtime_info_t &out)
|
|
{
|
|
if (!session)
|
|
return reject(GVFG_ESTATE, "gvfg_get_channel_runtime_info rejected: channel is not open");
|
|
|
|
std::memset(&out, 0, sizeof(out));
|
|
out.capture_fps = runtimeFps.load(std::memory_order_relaxed);
|
|
out.delivered_frames = deliveredFrames.load(std::memory_order_relaxed);
|
|
return GVFG_OK;
|
|
}
|
|
|
|
void syncSessionEventCallback()
|
|
{
|
|
if (!session)
|
|
return;
|
|
session->set_event_callback(&gvfg_channel_session_t::onSessionEvent,
|
|
this);
|
|
}
|
|
|
|
static void onSessionEvent(gvfg_event_type_t event, void *user)
|
|
{
|
|
auto *self = static_cast<gvfg_channel_session_t *>(user);
|
|
if (!self)
|
|
return;
|
|
self->syncCachedSignal();
|
|
self->emitEvent(event);
|
|
}
|
|
|
|
void emitEvent(gvfg_event_type_t event)
|
|
{
|
|
{
|
|
std::lock_guard<std::mutex> lock(eventMutex);
|
|
if (eventQueue.size() >= 64)
|
|
eventQueue.pop_front();
|
|
eventQueue.push_back(event);
|
|
}
|
|
eventCv.notify_one();
|
|
}
|
|
|
|
gvfg_status_t querySignal(bool verifyRequestedFormat = false)
|
|
{
|
|
if (!session)
|
|
return GVFG_ESTATE;
|
|
|
|
gvfg_signal_status_t sig{};
|
|
const gvfg_status_t status = session->get_signal_status(sig, verifyRequestedFormat);
|
|
|
|
std::lock_guard<std::mutex> lock(stateMutex);
|
|
width = status == GVFG_OK ? sig.width : 0;
|
|
height = status == GVFG_OK ? sig.height : 0;
|
|
bitDepth = status == GVFG_OK ? sig.bit_depth : 0;
|
|
pixelFormat = status == GVFG_OK
|
|
? static_cast<gvfg_pixel_format_t>(sig.pixel_format)
|
|
: GVFG_PIXFMT_UNKNOWN;
|
|
videoInterface = status == GVFG_OK ? sig.video_interface : 0;
|
|
signalConnected = status == GVFG_OK && sig.connected != 0;
|
|
return status;
|
|
}
|
|
|
|
gvfg_status_t syncCachedSignal()
|
|
{
|
|
if (!session)
|
|
return GVFG_ESTATE;
|
|
|
|
gvfg_signal_status_t sig{};
|
|
const gvfg_status_t status = session->get_cached_signal_status(sig);
|
|
|
|
std::lock_guard<std::mutex> lock(stateMutex);
|
|
width = sig.width > 0 ? static_cast<uint32_t>(sig.width) : 0;
|
|
height = sig.height > 0 ? static_cast<uint32_t>(sig.height) : 0;
|
|
bitDepth = sig.bit_depth > 0 ? static_cast<uint32_t>(sig.bit_depth) : 0;
|
|
pixelFormat = static_cast<gvfg_pixel_format_t>(sig.pixel_format);
|
|
videoInterface = sig.video_interface;
|
|
signalConnected = sig.connected != 0;
|
|
return status;
|
|
}
|
|
|
|
gvfg_status_t configureStream()
|
|
{
|
|
if (!session)
|
|
return reject(GVFG_ESTATE, "configure stream rejected: channel is not open");
|
|
|
|
const gvfg_status_t signalStatus = querySignal(true);
|
|
if (signalStatus != GVFG_OK)
|
|
return signalStatus;
|
|
{
|
|
std::lock_guard<std::mutex> lock(stateMutex);
|
|
if (!signalConnected)
|
|
return reject(GVFG_ETIMEOUT,
|
|
"gvfg_start_channel rejected: no input signal");
|
|
}
|
|
return session->configure_stream();
|
|
}
|
|
|
|
gvfg_status_t readFrame(gvfg_frame_t &out, uint32_t timeoutMs,
|
|
gvfg_debug_osd_frame_timing_t *timing = nullptr,
|
|
uint64_t *acquisitionBeginNs = nullptr)
|
|
{
|
|
std::memset(&out, 0, sizeof(out));
|
|
if (timing)
|
|
*timing = {};
|
|
if (acquisitionBeginNs)
|
|
*acquisitionBeginNs = 0;
|
|
if (!session)
|
|
return reject(GVFG_ESTATE, "gvfg_read_channel_frame rejected: channel is not open");
|
|
if (!running)
|
|
return reject(GVFG_ESTATE, "gvfg_read_channel_frame rejected: channel is not running");
|
|
|
|
const char *stateError = nullptr;
|
|
{
|
|
std::lock_guard<std::mutex> lock(frameMutex);
|
|
if (frameHeld)
|
|
stateError = "gvfg_read_channel_frame rejected: previous frame has not been released";
|
|
else if (readInProgress)
|
|
stateError = "gvfg_read_channel_frame rejected: another read is already in progress";
|
|
else
|
|
readInProgress = true;
|
|
}
|
|
if (stateError)
|
|
return reject(GVFG_ESTATE, stateError);
|
|
|
|
gvfg_frame_t frame{};
|
|
const gvfg_status_t st = session->wait_frame(timeoutMs, frame, acquisitionBeginNs);
|
|
if (st != GVFG_OK)
|
|
{
|
|
{
|
|
std::lock_guard<std::mutex> lock(frameMutex);
|
|
readInProgress = false;
|
|
}
|
|
return st;
|
|
}
|
|
|
|
if (!frame.data || frame.width <= 0 || frame.height <= 0)
|
|
{
|
|
session->release_frame();
|
|
std::lock_guard<std::mutex> lock(frameMutex);
|
|
readInProgress = false;
|
|
return reject(GVFG_EIO, "gvfg_read_channel_frame received an invalid frame descriptor");
|
|
}
|
|
|
|
int nativeStride = 0;
|
|
const gvfg_status_t strideStatus =
|
|
native_row_stride_bytes(static_cast<uint32_t>(frame.width),
|
|
frame.pixel_format, nativeStride);
|
|
if (strideStatus != GVFG_OK || frame.row_stride_bytes < nativeStride)
|
|
{
|
|
session->release_frame();
|
|
std::lock_guard<std::mutex> lock(frameMutex);
|
|
readInProgress = false;
|
|
return reject(strideStatus == GVFG_OK ? GVFG_EIO : strideStatus,
|
|
"gvfg_read_channel_frame received an invalid row stride");
|
|
}
|
|
|
|
bool osdApplied = false;
|
|
gvfg_frame_t processedFrame{};
|
|
const gvfg_frame_processor *processor = nullptr;
|
|
void *processorContext = nullptr;
|
|
{
|
|
std::lock_guard<std::mutex> lock(processorMutex);
|
|
processor = frameProcessor.ops;
|
|
processorContext = frameProcessor.context;
|
|
}
|
|
// Only SDK-owned copy-mode buffers are available for in-place writes.
|
|
uint8_t *writableSource = processor ? session->writable_copy_frame_data(frame) : nullptr;
|
|
bool inPlaceApplied = false;
|
|
const uint64_t processBeginNs = timing ? now_ns() : 0;
|
|
const gvfg_status_t osdStatus = processor
|
|
? processor->process(processorContext, frame, processedFrame, osdApplied, timing,
|
|
writableSource, &inPlaceApplied)
|
|
: GVFG_OK;
|
|
if (timing && processor)
|
|
timing->stage_ms[GVFG_DEBUG_OSD_PROCESS_TOTAL] = static_cast<double>(now_ns() - processBeginNs) / 1e6;
|
|
if (osdStatus != GVFG_OK)
|
|
{
|
|
session->release_frame();
|
|
std::lock_guard<std::mutex> lock(frameMutex);
|
|
readInProgress = false;
|
|
return reject(osdStatus, "gvfg_read_channel_frame failed to apply OSD");
|
|
}
|
|
|
|
bool backendFrameHeld = true;
|
|
// An in-place copy-mode frame must keep the backend hold until the
|
|
// user's release; otherwise recovery/next capture may reuse its data.
|
|
if (osdApplied && !inPlaceApplied)
|
|
{
|
|
const uint64_t releaseBeginNs = timing ? now_ns() : 0;
|
|
const gvfg_status_t releaseStatus = session->release_frame();
|
|
if (timing)
|
|
timing->stage_ms[GVFG_DEBUG_OSD_BACKEND_RELEASE] = static_cast<double>(now_ns() - releaseBeginNs) / 1e6;
|
|
if (releaseStatus != GVFG_OK)
|
|
{
|
|
std::lock_guard<std::mutex> lock(frameMutex);
|
|
readInProgress = false;
|
|
return releaseStatus;
|
|
}
|
|
backendFrameHeld = false;
|
|
frame.data = processedFrame.data;
|
|
frame.data_size = processedFrame.data_size;
|
|
frame.row_stride_bytes = processedFrame.row_stride_bytes;
|
|
}
|
|
|
|
bool stoppedWhileWaiting = false;
|
|
{
|
|
std::lock_guard<std::mutex> lock(frameMutex);
|
|
if (!running.load(std::memory_order_acquire))
|
|
{
|
|
readInProgress = false;
|
|
stoppedWhileWaiting = true;
|
|
}
|
|
else
|
|
{
|
|
frame.frame_id = ++videoFrameId;
|
|
heldSessionFrame = frame;
|
|
heldFrameNeedsBackendRelease = backendFrameHeld;
|
|
readInProgress = false;
|
|
frameHeld = true;
|
|
}
|
|
}
|
|
if (stoppedWhileWaiting)
|
|
{
|
|
if (backendFrameHeld)
|
|
session->release_frame();
|
|
return GVFG_ESTATE;
|
|
}
|
|
|
|
updateRuntimeFps(now_ns());
|
|
|
|
out = frame;
|
|
if (timing)
|
|
timing->frame_id = out.frame_id;
|
|
noteDeliveredFrame(out.width, out.height, out.bit_depth, out.pixel_format);
|
|
|
|
return GVFG_OK;
|
|
}
|
|
|
|
gvfg_status_t releaseFrame(const gvfg_frame_t &frameToken)
|
|
{
|
|
if (!session)
|
|
return reject(GVFG_ESTATE, "gvfg_release_channel_frame rejected: channel is not open");
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lock(frameMutex);
|
|
if (!frameHeld)
|
|
return reject(GVFG_ESTATE, "gvfg_release_channel_frame rejected: no frame is currently held");
|
|
|
|
if (frameToken.data != heldSessionFrame.data ||
|
|
frameToken.frame_id != heldSessionFrame.frame_id)
|
|
return reject(GVFG_EINVAL, "gvfg_release_channel_frame rejected: frame token does not match the held frame");
|
|
|
|
if (heldFrameNeedsBackendRelease)
|
|
{
|
|
const gvfg_status_t st = session->release_frame();
|
|
if (st != GVFG_OK)
|
|
return st;
|
|
}
|
|
|
|
heldSessionFrame = {};
|
|
heldFrameNeedsBackendRelease = false;
|
|
frameHeld = false;
|
|
}
|
|
|
|
return GVFG_OK;
|
|
}
|
|
|
|
void releaseHeldFrameForStop()
|
|
{
|
|
if (!session)
|
|
return;
|
|
|
|
bool shouldReleaseBackend = false;
|
|
{
|
|
std::lock_guard<std::mutex> lock(frameMutex);
|
|
if (frameHeld)
|
|
{
|
|
heldSessionFrame = {};
|
|
frameHeld = false;
|
|
shouldReleaseBackend = heldFrameNeedsBackendRelease;
|
|
heldFrameNeedsBackendRelease = false;
|
|
}
|
|
}
|
|
|
|
if (shouldReleaseBackend)
|
|
session->release_frame();
|
|
}
|
|
|
|
gvfg_status_t pollEvent(gvfg_event_type_t &out, uint32_t timeoutMs)
|
|
{
|
|
if (!session)
|
|
return reject(GVFG_ESTATE, "gvfg_poll_channel_event rejected: channel is not open");
|
|
|
|
std::unique_lock<std::mutex> lock(eventMutex);
|
|
const auto eventAvailableOrStopped = [this]()
|
|
{
|
|
return !eventQueue.empty() ||
|
|
!running.load(std::memory_order_acquire);
|
|
};
|
|
|
|
bool waitConditionMet = false;
|
|
if (timeoutMs == 0)
|
|
{
|
|
// Non-blocking: inspect the current state without sleeping.
|
|
waitConditionMet = eventAvailableOrStopped();
|
|
}
|
|
else if (timeoutMs == GVFG_TIMEOUT_INFINITE)
|
|
{
|
|
// Wake when an event arrives or capture is stopped.
|
|
eventCv.wait(lock, eventAvailableOrStopped);
|
|
waitConditionMet = true;
|
|
}
|
|
else
|
|
{
|
|
waitConditionMet = eventCv.wait_for(lock,
|
|
std::chrono::milliseconds(timeoutMs),
|
|
eventAvailableOrStopped);
|
|
}
|
|
|
|
if (!waitConditionMet)
|
|
return GVFG_ETIMEOUT;
|
|
|
|
if (eventQueue.empty())
|
|
return running.load(std::memory_order_acquire)
|
|
? GVFG_ETIMEOUT
|
|
: GVFG_ESTATE;
|
|
|
|
out = eventQueue.front();
|
|
eventQueue.pop_front();
|
|
return GVFG_OK;
|
|
}
|
|
|
|
gvfg_status_t getDebugStats(gvfg_debug_backend_stats_t &out)
|
|
{
|
|
std::memset(&out, 0, sizeof(out));
|
|
out.running = running.load(std::memory_order_relaxed) ? 1 : 0;
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lock(frameMutex);
|
|
out.frame_held = frameHeld ? 1 : 0;
|
|
}
|
|
{
|
|
std::lock_guard<std::mutex> lock(eventMutex);
|
|
out.event_queue_depth = static_cast<uint32_t>(eventQueue.size());
|
|
}
|
|
|
|
if (session)
|
|
{
|
|
session->fill_debug_stats(out);
|
|
}
|
|
|
|
return GVFG_OK;
|
|
}
|
|
|
|
gvfg_status_t debugReadRegister(uint32_t offset, uint32_t &outValue)
|
|
{
|
|
if (!session)
|
|
return reject(GVFG_ESTATE, "debug register read rejected: channel is not open");
|
|
return session->debug_read_register(offset, outValue);
|
|
}
|
|
|
|
gvfg_status_t setVideoFormat(gvfg_pixel_format_t format)
|
|
{
|
|
if (!session)
|
|
return reject(GVFG_ESTATE, "gvfg_set_channel_video_format rejected: channel is not open");
|
|
|
|
if (format != GVFG_PIXFMT_YUY2 && format != GVFG_PIXFMT_Y210)
|
|
return reject(GVFG_EINVAL, "gvfg_set_channel_video_format rejected: pixel format is invalid");
|
|
return session->set_video_format(format);
|
|
}
|
|
|
|
gvfg_status_t controlFrameProcessor(const gvfg_frame_processor *processor, bool create,
|
|
gvfg_frame_processor_control control, const void *argument,
|
|
const char *error)
|
|
{
|
|
if (!session)
|
|
return reject(GVFG_ESTATE, "Frame processor channel is not open");
|
|
|
|
std::lock_guard<std::mutex> lock(processorMutex);
|
|
if (!frameProcessor.ops)
|
|
{
|
|
if (!create)
|
|
return GVFG_OK;
|
|
const auto status = processor->create(&frameProcessor.context);
|
|
if (status != GVFG_OK)
|
|
return reject(status, error);
|
|
frameProcessor.ops = processor;
|
|
}
|
|
if (frameProcessor.ops != processor)
|
|
return reject(GVFG_ESTATE, "A different frame processor is already attached");
|
|
|
|
const auto status = control(frameProcessor.context, argument);
|
|
return status == GVFG_OK ? status : reject(status, error);
|
|
}
|
|
|
|
gvfg_status_t setAudioEnabled(bool enabled)
|
|
{
|
|
if (!session)
|
|
return reject(GVFG_ESTATE, "gvfg_set_channel_audio_enabled rejected: channel is not open");
|
|
if (running)
|
|
return reject(GVFG_ESTATE, "gvfg_set_channel_audio_enabled rejected: channel is running");
|
|
const gvfg_status_t status = session->set_audio_enabled(enabled);
|
|
if (status != GVFG_OK)
|
|
return status;
|
|
audioEnabled = enabled;
|
|
return GVFG_OK;
|
|
}
|
|
|
|
gvfg_status_t debugWriteRegister(uint32_t offset, uint32_t value)
|
|
{
|
|
if (!session)
|
|
return reject(GVFG_ESTATE, "debug register write rejected: channel is not open");
|
|
return session->debug_write_register(offset, value);
|
|
}
|
|
|
|
gvfg_status_t getAudioFormat(gvfg_audio_format_t &out)
|
|
{
|
|
std::memset(&out, 0, sizeof(out));
|
|
if (!session)
|
|
return reject(GVFG_ESTATE, "gvfg_get_channel_audio_format rejected: channel is not open");
|
|
GigabyteAudioInfo format{};
|
|
const gvfg_status_t status = session->get_audio_format(format);
|
|
if (status != GVFG_OK)
|
|
return status;
|
|
out.sample_rate = format.sample_rate;
|
|
out.channels = format.channels;
|
|
out.bits_per_sample = format.bits_per_sample;
|
|
return GVFG_OK;
|
|
}
|
|
|
|
gvfg_status_t readAudioFrame(gvfg_audio_frame_t &out, uint32_t timeoutMs)
|
|
{
|
|
std::memset(&out, 0, sizeof(out));
|
|
if (!session)
|
|
return reject(GVFG_ESTATE, "gvfg_read_channel_audio_frame rejected: channel is not open");
|
|
if (!running || !audioEnabled)
|
|
return reject(GVFG_ESTATE, "gvfg_read_channel_audio_frame rejected: audio capture is not running");
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lock(audioMutex);
|
|
if (audioFrameHeld)
|
|
return reject(GVFG_ESTATE, "gvfg_read_channel_audio_frame rejected: previous audio frame has not been released");
|
|
if (audioReadInProgress)
|
|
return reject(GVFG_ESTATE, "gvfg_read_channel_audio_frame rejected: another audio read is already in progress");
|
|
audioReadInProgress = true;
|
|
}
|
|
|
|
GigabyteAudioInfo format{};
|
|
const gvfg_status_t status = session->wait_audio(timeoutMs, audioBuffer, format);
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lock(audioMutex);
|
|
audioReadInProgress = false;
|
|
if (status != GVFG_OK)
|
|
return status;
|
|
if (!running.load(std::memory_order_acquire))
|
|
return reject(GVFG_ESTATE, "gvfg_read_channel_audio_frame completed after capture stopped or returned invalid data");
|
|
audioFrameHeld = true;
|
|
out.data = audioBuffer.data();
|
|
out.data_size = format.frame_bytes;
|
|
out.sample_rate = format.sample_rate;
|
|
out.channels = format.channels;
|
|
out.bits_per_sample = format.bits_per_sample;
|
|
out.frame_id = ++audioFrameId;
|
|
out.timestamp_ns = now_ns();
|
|
heldAudioFrame = out;
|
|
}
|
|
return GVFG_OK;
|
|
}
|
|
|
|
gvfg_status_t releaseAudioFrame(const gvfg_audio_frame_t &token)
|
|
{
|
|
std::lock_guard<std::mutex> lock(audioMutex);
|
|
if (!audioFrameHeld)
|
|
return reject(GVFG_ESTATE, "gvfg_release_channel_audio_frame rejected: no audio frame is currently held");
|
|
if (token.data != heldAudioFrame.data || token.frame_id != heldAudioFrame.frame_id)
|
|
return reject(GVFG_EINVAL, "gvfg_release_channel_audio_frame rejected: frame token does not match the held audio frame");
|
|
heldAudioFrame = {};
|
|
audioFrameHeld = false;
|
|
return GVFG_OK;
|
|
}
|
|
|
|
void releaseHeldAudioFrameForStop()
|
|
{
|
|
std::lock_guard<std::mutex> lock(audioMutex);
|
|
heldAudioFrame = {};
|
|
audioFrameHeld = false;
|
|
}
|
|
|
|
gvfg_status_t reject(gvfg_status_t status, const char *message)
|
|
{
|
|
errorState.set(message ? message : "GVFG operation rejected");
|
|
return status;
|
|
}
|
|
|
|
void updateRuntimeFps(uint64_t ptsNs)
|
|
{
|
|
constexpr uint64_t kFpsWindowNs = 1000000000ULL;
|
|
const uint64_t windowStart = fpsWindowStartNs.load(std::memory_order_relaxed);
|
|
if (windowStart == 0 || ptsNs <= windowStart)
|
|
{
|
|
fpsWindowStartNs.store(ptsNs, std::memory_order_relaxed);
|
|
fpsWindowFrameCount.store(0, std::memory_order_relaxed);
|
|
return;
|
|
}
|
|
|
|
const uint64_t frameCount = fpsWindowFrameCount.fetch_add(1, std::memory_order_relaxed) + 1;
|
|
const uint64_t elapsedNs = ptsNs - windowStart;
|
|
if (elapsedNs < kFpsWindowNs)
|
|
return;
|
|
|
|
runtimeFps.store(static_cast<double>(frameCount) * 1e9 /
|
|
static_cast<double>(elapsedNs),
|
|
std::memory_order_relaxed);
|
|
fpsWindowStartNs.store(ptsNs, std::memory_order_relaxed);
|
|
fpsWindowFrameCount.store(0, std::memory_order_relaxed);
|
|
}
|
|
|
|
void resetRuntimeCounters()
|
|
{
|
|
fpsWindowStartNs.store(0, std::memory_order_relaxed);
|
|
fpsWindowFrameCount.store(0, std::memory_order_relaxed);
|
|
deliveredFrames.store(0, std::memory_order_relaxed);
|
|
deliveredWidth.store(0, std::memory_order_relaxed);
|
|
deliveredHeight.store(0, std::memory_order_relaxed);
|
|
deliveredBitDepth.store(0, std::memory_order_relaxed);
|
|
deliveredPixelFormat.store(GVFG_PIXFMT_UNKNOWN, std::memory_order_relaxed);
|
|
runtimeFps.store(0.0, std::memory_order_relaxed);
|
|
videoFrameId = 0;
|
|
audioFrameId = 0;
|
|
}
|
|
|
|
void noteDeliveredFrame(int frameWidth, int frameHeight, int bitDepth, int pixelFormat)
|
|
{
|
|
deliveredWidth.store(frameWidth > 0 ? static_cast<uint32_t>(frameWidth) : 0, std::memory_order_relaxed);
|
|
deliveredHeight.store(frameHeight > 0 ? static_cast<uint32_t>(frameHeight) : 0, std::memory_order_relaxed);
|
|
deliveredBitDepth.store(bitDepth > 0 ? static_cast<uint32_t>(bitDepth) : 0, std::memory_order_relaxed);
|
|
deliveredPixelFormat.store(pixelFormat, std::memory_order_relaxed);
|
|
deliveredFrames.fetch_add(1, std::memory_order_relaxed);
|
|
}
|
|
|
|
ChannelErrorState &errorState;
|
|
std::unique_ptr<gvfg::internal::GigabyteCaptureSession> session;
|
|
uint32_t selectedChannel = GVFG_CHANNEL_0;
|
|
bool zeroCopyRequested = false;
|
|
bool audioEnabled = false;
|
|
|
|
uint32_t width = 0;
|
|
uint32_t height = 0;
|
|
uint32_t bitDepth = 0;
|
|
gvfg_pixel_format_t pixelFormat = GVFG_PIXFMT_UNKNOWN;
|
|
int videoInterface = 0;
|
|
bool signalConnected = false;
|
|
mutable std::mutex stateMutex;
|
|
std::atomic<uint64_t> fpsWindowStartNs{0};
|
|
std::atomic<uint64_t> fpsWindowFrameCount{0};
|
|
std::atomic<uint64_t> deliveredFrames{0};
|
|
std::atomic<uint32_t> deliveredWidth{0};
|
|
std::atomic<uint32_t> deliveredHeight{0};
|
|
std::atomic<uint32_t> deliveredBitDepth{0};
|
|
std::atomic<int> deliveredPixelFormat{GVFG_PIXFMT_UNKNOWN};
|
|
std::atomic<double> runtimeFps{0.0};
|
|
|
|
std::atomic<bool> running{false};
|
|
std::mutex frameMutex;
|
|
bool readInProgress = false;
|
|
bool frameHeld = false;
|
|
bool heldFrameNeedsBackendRelease = false;
|
|
uint64_t videoFrameId = 0;
|
|
gvfg_frame_t heldSessionFrame{};
|
|
std::mutex processorMutex;
|
|
struct FrameProcessorState
|
|
{
|
|
const gvfg_frame_processor *ops = nullptr;
|
|
void *context = nullptr;
|
|
|
|
~FrameProcessorState()
|
|
{
|
|
if (ops)
|
|
ops->destroy(context);
|
|
}
|
|
} frameProcessor;
|
|
std::mutex audioMutex;
|
|
std::vector<uint8_t> audioBuffer;
|
|
bool audioReadInProgress = false;
|
|
bool audioFrameHeld = false;
|
|
uint64_t audioFrameId = 0;
|
|
gvfg_audio_frame_t heldAudioFrame{};
|
|
std::mutex eventMutex;
|
|
std::condition_variable eventCv;
|
|
std::deque<gvfg_event_type_t> eventQueue;
|
|
};
|
|
|
|
struct gvfg_handle_t
|
|
{
|
|
gvfg_status_t rejectChannel(int channelIndex,
|
|
gvfg_status_t status,
|
|
const char *message)
|
|
{
|
|
if (channelIndex == GVFG_CHANNEL_0 || channelIndex == GVFG_CHANNEL_1)
|
|
channelErrors[static_cast<size_t>(channelIndex)].set(message ? message : "GVFG operation rejected");
|
|
return status;
|
|
}
|
|
|
|
gvfg_status_t rejectAllChannels(gvfg_status_t status, const char *message)
|
|
{
|
|
for (ChannelErrorState &error : channelErrors)
|
|
error.set(message ? message : "GVFG operation rejected");
|
|
return status;
|
|
}
|
|
|
|
gvfg_status_t copyChannelError(int channelIndex,
|
|
char *outMessage,
|
|
uint32_t outMessageSize) const
|
|
{
|
|
if ((channelIndex != GVFG_CHANNEL_0 && channelIndex != GVFG_CHANNEL_1) ||
|
|
!outMessage || outMessageSize == 0)
|
|
return GVFG_EINVAL;
|
|
|
|
const std::string detail = channelErrors[static_cast<size_t>(channelIndex)].message();
|
|
copy_cstr(outMessage, outMessageSize, detail.c_str());
|
|
return GVFG_OK;
|
|
}
|
|
|
|
gvfg_channel_session_t *findChannel(int channelIndex)
|
|
{
|
|
if (channelIndex != GVFG_CHANNEL_0 && channelIndex != GVFG_CHANNEL_1)
|
|
return nullptr;
|
|
return channels[static_cast<size_t>(channelIndex)].get();
|
|
}
|
|
|
|
gvfg_channel_session_t *findOpenChannel()
|
|
{
|
|
if (channels[GVFG_CHANNEL_0])
|
|
return channels[GVFG_CHANNEL_0].get();
|
|
return channels[GVFG_CHANNEL_1].get();
|
|
}
|
|
|
|
gvfg_status_t openChannel(int index, int channelIndex, const std::wstring *requestedPath = nullptr)
|
|
{
|
|
if (channelIndex != GVFG_CHANNEL_0 && channelIndex != GVFG_CHANNEL_1)
|
|
return GVFG_EINVAL;
|
|
const size_t slot = static_cast<size_t>(channelIndex);
|
|
if (index < 0)
|
|
{
|
|
channelErrors[slot].set("gvfg_open_channel rejected: device index is invalid");
|
|
return GVFG_EINVAL;
|
|
}
|
|
if (currentIndex >= 0 && (currentIndex != index ||
|
|
(requestedPath && devicePath != *requestedPath)))
|
|
{
|
|
channelErrors[slot].set("gvfg_open_channel rejected: handle is already bound to another device");
|
|
return GVFG_ESTATE;
|
|
}
|
|
|
|
if (channels[slot])
|
|
return GVFG_OK;
|
|
|
|
auto channel = std::make_unique<gvfg_channel_session_t>(channelErrors[slot]);
|
|
channel->zeroCopyRequested = zeroCopyRequested[slot];
|
|
|
|
const gvfg_status_t status = channel->open(index,
|
|
channelIndex,
|
|
devicePath.empty() ? requestedPath : &devicePath);
|
|
if (status != GVFG_OK)
|
|
return status;
|
|
|
|
if (devicePath.empty())
|
|
devicePath = channel->session->device_path();
|
|
channels[slot] = std::move(channel);
|
|
currentIndex = index;
|
|
return GVFG_OK;
|
|
}
|
|
|
|
gvfg_status_t setChannelZeroCopyEnabled(int channelIndex, bool enabled)
|
|
{
|
|
if (channelIndex != GVFG_CHANNEL_0 && channelIndex != GVFG_CHANNEL_1)
|
|
return GVFG_EINVAL;
|
|
const size_t slot = static_cast<size_t>(channelIndex);
|
|
if (channels[slot])
|
|
{
|
|
channelErrors[slot].set(
|
|
"gvfg_set_channel_zero_copy_enabled rejected: channel is already open");
|
|
return GVFG_ESTATE;
|
|
}
|
|
zeroCopyRequested[slot] = enabled;
|
|
return GVFG_OK;
|
|
}
|
|
|
|
gvfg_status_t setChannelVideoFormat(int channelIndex, gvfg_pixel_format_t format)
|
|
{
|
|
gvfg_channel_session_t *channel = findChannel(channelIndex);
|
|
if (!channel)
|
|
return rejectChannel(channelIndex, GVFG_ESTATE,
|
|
"gvfg_set_channel_video_format rejected: channel is not open");
|
|
if (format != GVFG_PIXFMT_YUY2 && format != GVFG_PIXFMT_Y210)
|
|
return rejectChannel(channelIndex, GVFG_EINVAL,
|
|
"gvfg_set_channel_video_format rejected: pixel format is invalid");
|
|
|
|
const size_t slot = static_cast<size_t>(channelIndex);
|
|
const size_t otherSlot = static_cast<size_t>(
|
|
channelIndex == GVFG_CHANNEL_0 ? GVFG_CHANNEL_1 : GVFG_CHANNEL_0);
|
|
std::lock_guard<std::mutex> lock(formatMutex);
|
|
if (format == GVFG_PIXFMT_Y210 &&
|
|
requestedVideoFormats[otherSlot] == GVFG_PIXFMT_Y210)
|
|
{
|
|
return rejectChannel(
|
|
channelIndex,
|
|
GVFG_EBUSY,
|
|
"gvfg_set_channel_video_format rejected: Y210 is already selected by the other channel");
|
|
}
|
|
|
|
const gvfg_status_t status = channel->setVideoFormat(format);
|
|
if (status == GVFG_OK)
|
|
requestedVideoFormats[slot] = format;
|
|
return status;
|
|
}
|
|
|
|
gvfg_status_t stopAll()
|
|
{
|
|
gvfg_status_t result = GVFG_OK;
|
|
for (const auto &channel : channels)
|
|
{
|
|
if (!channel)
|
|
continue;
|
|
const gvfg_status_t status = channel->stop();
|
|
if (result == GVFG_OK)
|
|
result = status;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
gvfg_status_t debugReadRegister(uint32_t offset, uint32_t &outValue)
|
|
{
|
|
gvfg_channel_session_t *channel = findOpenChannel();
|
|
return channel ? channel->debugReadRegister(offset, outValue)
|
|
: rejectAllChannels(GVFG_ESTATE,
|
|
"debug register read rejected: no channel is open");
|
|
}
|
|
|
|
gvfg_status_t debugWriteRegister(uint32_t offset, uint32_t value)
|
|
{
|
|
gvfg_channel_session_t *channel = findOpenChannel();
|
|
return channel ? channel->debugWriteRegister(offset, value)
|
|
: rejectAllChannels(GVFG_ESTATE,
|
|
"debug register write rejected: no channel is open");
|
|
}
|
|
|
|
std::array<ChannelErrorState, 2> channelErrors;
|
|
std::wstring devicePath;
|
|
std::array<std::unique_ptr<gvfg_channel_session_t>, 2> channels;
|
|
int currentIndex = -1;
|
|
std::array<bool, 2> zeroCopyRequested{false, false};
|
|
std::mutex formatMutex;
|
|
std::array<gvfg_pixel_format_t, 2> requestedVideoFormats{
|
|
GVFG_PIXFMT_UNKNOWN, GVFG_PIXFMT_UNKNOWN};
|
|
};
|
|
|
|
extern "C"
|
|
{
|
|
#ifdef GVFG_CAPTURE_TEST_RUNTIME
|
|
int gvfg_test_enumerate_devices(gvfg_test_device_t *devices, int capacity)
|
|
{
|
|
if (!devices || capacity <= 0)
|
|
return 0;
|
|
int written = 0;
|
|
for (const auto &device : enumerate_gigabyte_devices())
|
|
{
|
|
if (written == capacity)
|
|
break;
|
|
gvfg_test_device_t entry{};
|
|
if (device.interface_path.empty() ||
|
|
wcscpy_s(entry.interface_path, device.interface_path.c_str()) != 0)
|
|
continue;
|
|
wcsncpy_s(entry.name, device.friendly_name.c_str(), _TRUNCATE);
|
|
devices[written++] = entry;
|
|
}
|
|
return written;
|
|
}
|
|
|
|
gvfg_status_t gvfg_test_open_channel(gvfg_handle handle, const wchar_t *interface_path, int channel)
|
|
{
|
|
if (!handle || !interface_path || !*interface_path)
|
|
return GVFG_EINVAL;
|
|
const std::wstring path(interface_path);
|
|
return handle->openChannel(0, channel, &path);
|
|
}
|
|
#endif
|
|
|
|
int gvfg_enumerate_devices(gvfg_device_info_t *out_devices, int max_devices)
|
|
{
|
|
const std::vector<gvfg::internal::GigabyteDevice> devices =
|
|
gvfg::internal::enumerate_gigabyte_devices();
|
|
const int n = static_cast<int>(devices.size());
|
|
if (n <= 0)
|
|
return n;
|
|
|
|
if (!out_devices || max_devices <= 0)
|
|
return n;
|
|
|
|
const int written = (std::min)((std::min)(n, max_devices),
|
|
static_cast<int>(GVFG_MAX_DEVICES));
|
|
for (int i = 0; i < written; ++i)
|
|
{
|
|
out_devices[i] = {};
|
|
copy_cstr(out_devices[i].name, sizeof(out_devices[i].name),
|
|
"GVFG Capture");
|
|
copy_wide_to_utf8(devices[static_cast<size_t>(i)].friendly_name,
|
|
out_devices[i].name,
|
|
sizeof(out_devices[i].name));
|
|
if (!out_devices[i].name[0])
|
|
copy_cstr(out_devices[i].name, sizeof(out_devices[i].name), "GVFG Capture");
|
|
|
|
const std::string suffix = " #" + std::to_string(i + 1);
|
|
std::string displayName = out_devices[i].name;
|
|
const size_t maxNameLength = sizeof(out_devices[i].name) - 1;
|
|
if (displayName.size() + suffix.size() > maxNameLength)
|
|
displayName.resize(maxNameLength - suffix.size());
|
|
displayName += suffix;
|
|
copy_cstr(out_devices[i].name, sizeof(out_devices[i].name), displayName.c_str());
|
|
}
|
|
return written;
|
|
}
|
|
|
|
gvfg_status_t gvfg_create(gvfg_handle *out_handle)
|
|
{
|
|
if (!out_handle)
|
|
return GVFG_EINVAL;
|
|
|
|
auto h = std::make_unique<gvfg_handle_t>();
|
|
*out_handle = h.release();
|
|
return GVFG_OK;
|
|
}
|
|
|
|
gvfg_status_t gvfg_destroy(gvfg_handle handle)
|
|
{
|
|
delete handle;
|
|
return GVFG_OK;
|
|
}
|
|
|
|
gvfg_status_t gvfg_open_channel(gvfg_handle handle, int device_index, int channel_index)
|
|
{
|
|
if (!handle)
|
|
return GVFG_EINVAL;
|
|
return handle->openChannel(device_index, channel_index);
|
|
}
|
|
|
|
gvfg_status_t gvfg_set_channel_zero_copy_enabled(gvfg_handle handle,
|
|
int channel_index,
|
|
int enabled)
|
|
{
|
|
if (!handle ||
|
|
(channel_index != GVFG_CHANNEL_0 && channel_index != GVFG_CHANNEL_1))
|
|
return GVFG_EINVAL;
|
|
if (enabled != 0 && enabled != 1)
|
|
{
|
|
handle->channelErrors[static_cast<size_t>(channel_index)].set(
|
|
"gvfg_set_channel_zero_copy_enabled rejected: enabled must be 0 or 1");
|
|
return GVFG_EINVAL;
|
|
}
|
|
return handle->setChannelZeroCopyEnabled(channel_index, enabled != 0);
|
|
}
|
|
|
|
gvfg_status_t gvfg_get_channel_zero_copy_enabled(gvfg_handle handle,
|
|
int channel_index,
|
|
int *out_enabled)
|
|
{
|
|
if (!handle || !out_enabled ||
|
|
(channel_index != GVFG_CHANNEL_0 && channel_index != GVFG_CHANNEL_1))
|
|
return GVFG_EINVAL;
|
|
*out_enabled = handle->zeroCopyRequested[static_cast<size_t>(channel_index)] ? 1 : 0;
|
|
return GVFG_OK;
|
|
}
|
|
|
|
gvfg_status_t gvfg_start_channel(gvfg_handle handle, int channel_index)
|
|
{
|
|
if (!handle)
|
|
return GVFG_EINVAL;
|
|
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
|
|
return channel ? channel->start()
|
|
: handle->rejectChannel(channel_index,
|
|
GVFG_ESTATE,
|
|
"gvfg_start_channel rejected: channel is not open");
|
|
}
|
|
|
|
gvfg_status_t gvfg_set_channel_video_format(gvfg_handle handle,
|
|
int channel_index,
|
|
gvfg_pixel_format_t format)
|
|
{
|
|
if (!handle)
|
|
return GVFG_EINVAL;
|
|
return handle->setChannelVideoFormat(channel_index, format);
|
|
}
|
|
|
|
gvfg_status_t gvfg_control_frame_processor(
|
|
gvfg_handle handle, int channel_index, const gvfg_frame_processor *processor,
|
|
bool create, gvfg_frame_processor_control control, const void *argument,
|
|
const char *error)
|
|
{
|
|
if (!handle)
|
|
return GVFG_EINVAL;
|
|
if (!control)
|
|
return handle->rejectChannel(channel_index, GVFG_EINVAL, error);
|
|
auto *channel = handle->findChannel(channel_index);
|
|
return channel ? channel->controlFrameProcessor(processor, create, control, argument, error)
|
|
: handle->rejectChannel(channel_index, GVFG_ESTATE, "Frame processor channel is not open");
|
|
}
|
|
|
|
gvfg_status_t gvfg_set_channel_audio_enabled(gvfg_handle handle,
|
|
int channel_index,
|
|
int enabled)
|
|
{
|
|
if (!handle)
|
|
return GVFG_EINVAL;
|
|
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
|
|
return channel ? channel->setAudioEnabled(enabled != 0)
|
|
: handle->rejectChannel(channel_index, GVFG_ESTATE,
|
|
"gvfg_set_channel_audio_enabled rejected: channel is not open");
|
|
}
|
|
|
|
gvfg_status_t gvfg_get_channel_audio_format(gvfg_handle handle,
|
|
int channel_index,
|
|
gvfg_audio_format_t *out_format)
|
|
{
|
|
if (!handle)
|
|
return GVFG_EINVAL;
|
|
if (!out_format)
|
|
return handle->rejectChannel(channel_index,
|
|
GVFG_EINVAL,
|
|
"gvfg_get_channel_audio_format rejected: out_format is null");
|
|
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
|
|
return channel ? channel->getAudioFormat(*out_format)
|
|
: handle->rejectChannel(channel_index,
|
|
GVFG_ESTATE,
|
|
"gvfg_get_channel_audio_format rejected: channel is not open");
|
|
}
|
|
|
|
gvfg_status_t gvfg_read_channel_frame(gvfg_handle handle,
|
|
int channel_index,
|
|
gvfg_frame_t *out_frame,
|
|
uint32_t timeout_ms)
|
|
{
|
|
if (!handle)
|
|
return GVFG_EINVAL;
|
|
if (!out_frame)
|
|
return handle->rejectChannel(channel_index,
|
|
GVFG_EINVAL,
|
|
"gvfg_read_channel_frame rejected: out_frame is null");
|
|
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
|
|
return channel ? channel->readFrame(*out_frame, timeout_ms)
|
|
: handle->rejectChannel(channel_index,
|
|
GVFG_ESTATE,
|
|
"gvfg_read_channel_frame rejected: channel is not open");
|
|
}
|
|
|
|
gvfg_status_t gvfg_debug_read_channel_frame_timed(gvfg_handle handle,
|
|
int channel_index,
|
|
gvfg_frame_t *out_frame,
|
|
gvfg_debug_osd_frame_timing_t *out_timing,
|
|
uint32_t timeout_ms)
|
|
{
|
|
if (out_timing)
|
|
*out_timing = {};
|
|
if (!handle || !out_frame || !out_timing)
|
|
return GVFG_EINVAL;
|
|
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
|
|
return channel ? channel->readFrame(*out_frame, timeout_ms, out_timing)
|
|
: handle->rejectChannel(channel_index, GVFG_ESTATE,
|
|
"gvfg_debug_read_channel_frame_timed rejected: channel is not open");
|
|
}
|
|
|
|
gvfg_status_t gvfg_debug_read_channel_frame_profiled(gvfg_handle handle,
|
|
int channel_index,
|
|
gvfg_frame_t *out_frame,
|
|
gvfg_debug_frame_read_timing_t *out_timing,
|
|
uint32_t timeout_ms)
|
|
{
|
|
if (out_timing)
|
|
*out_timing = {};
|
|
if (!handle || !out_frame || !out_timing)
|
|
return GVFG_EINVAL;
|
|
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
|
|
return channel ? channel->readFrame(*out_frame, timeout_ms, &out_timing->osd,
|
|
&out_timing->acquisition_begin_ns)
|
|
: handle->rejectChannel(channel_index, GVFG_ESTATE,
|
|
"gvfg_debug_read_channel_frame_profiled rejected: channel is not open");
|
|
}
|
|
|
|
gvfg_status_t gvfg_release_channel_frame(gvfg_handle handle,
|
|
int channel_index,
|
|
const gvfg_frame_t *frame)
|
|
{
|
|
if (!handle)
|
|
return GVFG_EINVAL;
|
|
if (!frame)
|
|
return handle->rejectChannel(channel_index,
|
|
GVFG_EINVAL,
|
|
"gvfg_release_channel_frame rejected: frame is null");
|
|
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
|
|
return channel ? channel->releaseFrame(*frame)
|
|
: handle->rejectChannel(channel_index,
|
|
GVFG_ESTATE,
|
|
"gvfg_release_channel_frame rejected: channel is not open");
|
|
}
|
|
|
|
gvfg_status_t gvfg_read_channel_audio_frame(gvfg_handle handle,
|
|
int channel_index,
|
|
gvfg_audio_frame_t *out_frame,
|
|
uint32_t timeout_ms)
|
|
{
|
|
if (!handle)
|
|
return GVFG_EINVAL;
|
|
if (!out_frame)
|
|
return handle->rejectChannel(channel_index,
|
|
GVFG_EINVAL,
|
|
"gvfg_read_channel_audio_frame rejected: out_frame is null");
|
|
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
|
|
return channel ? channel->readAudioFrame(*out_frame, timeout_ms)
|
|
: handle->rejectChannel(channel_index,
|
|
GVFG_ESTATE,
|
|
"gvfg_read_channel_audio_frame rejected: channel is not open");
|
|
}
|
|
|
|
gvfg_status_t gvfg_release_channel_audio_frame(gvfg_handle handle,
|
|
int channel_index,
|
|
const gvfg_audio_frame_t *frame)
|
|
{
|
|
if (!handle)
|
|
return GVFG_EINVAL;
|
|
if (!frame)
|
|
return handle->rejectChannel(channel_index, GVFG_EINVAL,
|
|
"gvfg_release_channel_audio_frame rejected: frame is null");
|
|
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
|
|
return channel ? channel->releaseAudioFrame(*frame)
|
|
: handle->rejectChannel(channel_index, GVFG_ESTATE,
|
|
"gvfg_release_channel_audio_frame rejected: channel is not open");
|
|
}
|
|
|
|
gvfg_status_t gvfg_poll_channel_event(gvfg_handle handle,
|
|
int channel_index,
|
|
gvfg_event_type_t *out_event_type,
|
|
uint32_t timeout_ms)
|
|
{
|
|
if (!handle)
|
|
return GVFG_EINVAL;
|
|
if (!out_event_type)
|
|
return handle->rejectChannel(channel_index,
|
|
GVFG_EINVAL,
|
|
"gvfg_poll_channel_event rejected: output event is invalid");
|
|
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
|
|
if (!channel)
|
|
return handle->rejectChannel(channel_index,
|
|
GVFG_ESTATE,
|
|
"gvfg_poll_channel_event rejected: channel is not open");
|
|
gvfg_event_type_t event = GVFG_EVENT_UNKNOWN;
|
|
const gvfg_status_t status = channel->pollEvent(event, timeout_ms);
|
|
if (status == GVFG_OK)
|
|
*out_event_type = event;
|
|
return status;
|
|
}
|
|
|
|
gvfg_status_t gvfg_stop(gvfg_handle handle)
|
|
{
|
|
if (!handle)
|
|
return GVFG_EINVAL;
|
|
return handle->stopAll();
|
|
}
|
|
|
|
gvfg_status_t gvfg_stop_channel(gvfg_handle handle, int channel_index)
|
|
{
|
|
if (!handle)
|
|
return GVFG_EINVAL;
|
|
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
|
|
return channel ? channel->stop()
|
|
: handle->rejectChannel(channel_index,
|
|
GVFG_ESTATE,
|
|
"gvfg_stop_channel rejected: channel is not open");
|
|
}
|
|
|
|
gvfg_status_t gvfg_get_channel_signal_status(gvfg_handle handle,
|
|
int channel_index,
|
|
gvfg_signal_status_t *out_status)
|
|
{
|
|
if (!handle)
|
|
return GVFG_EINVAL;
|
|
if (!out_status)
|
|
return handle->rejectChannel(channel_index,
|
|
GVFG_EINVAL,
|
|
"gvfg_get_channel_signal_status rejected: out_status is null");
|
|
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
|
|
if (!channel)
|
|
return handle->rejectChannel(channel_index,
|
|
GVFG_ESTATE,
|
|
"gvfg_get_channel_signal_status rejected: channel is not open");
|
|
return channel->getSignalStatus(*out_status);
|
|
}
|
|
|
|
gvfg_status_t gvfg_get_channel_runtime_info(gvfg_handle handle,
|
|
int channel_index,
|
|
gvfg_runtime_info_t *out_info)
|
|
{
|
|
if (!handle)
|
|
return GVFG_EINVAL;
|
|
if (!out_info)
|
|
return handle->rejectChannel(channel_index,
|
|
GVFG_EINVAL,
|
|
"gvfg_get_channel_runtime_info rejected: out_info is null");
|
|
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
|
|
return channel ? channel->getRuntimeInfo(*out_info)
|
|
: handle->rejectChannel(channel_index,
|
|
GVFG_ESTATE,
|
|
"gvfg_get_channel_runtime_info rejected: channel is not open");
|
|
}
|
|
|
|
gvfg_status_t gvfg_get_channel_sdi_info(
|
|
gvfg_handle handle, int channel_index, gvfg_sdi_info_t *out_info)
|
|
{
|
|
if (!handle)
|
|
return GVFG_EINVAL;
|
|
if (!out_info)
|
|
return handle->rejectChannel(
|
|
channel_index, GVFG_EINVAL,
|
|
"gvfg_get_channel_sdi_info rejected: out_info is null");
|
|
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
|
|
if (!channel || !channel->session)
|
|
return handle->rejectChannel(
|
|
channel_index, GVFG_ESTATE,
|
|
"gvfg_get_channel_sdi_info rejected: channel is not open");
|
|
return channel->session->get_sdi_info(*out_info);
|
|
}
|
|
|
|
gvfg_status_t gvfg_get_channel_hdmi_info(
|
|
gvfg_handle handle, int channel_index, gvfg_hdmi_info_t *out_info)
|
|
{
|
|
if (!handle || (channel_index != GVFG_CHANNEL_0 && channel_index != GVFG_CHANNEL_1))
|
|
return GVFG_EINVAL;
|
|
if (!out_info)
|
|
return handle->rejectChannel(
|
|
channel_index, GVFG_EINVAL,
|
|
"gvfg_get_channel_hdmi_info rejected: out_info is null");
|
|
*out_info = {};
|
|
auto *channel = handle->findChannel(channel_index);
|
|
if (!channel || !channel->session)
|
|
return handle->rejectChannel(
|
|
channel_index, GVFG_ESTATE,
|
|
"gvfg_get_channel_hdmi_info rejected: channel is not open");
|
|
return channel->session->get_hdmi_info(*out_info);
|
|
}
|
|
|
|
gvfg_status_t gvfg_get_channel_hdmi_audio_info(
|
|
gvfg_handle handle, int channel_index, gvfg_hdmi_audio_info_t *out_info)
|
|
{
|
|
if (!handle || (channel_index != GVFG_CHANNEL_0 && channel_index != GVFG_CHANNEL_1))
|
|
return GVFG_EINVAL;
|
|
if (!out_info)
|
|
return handle->rejectChannel(
|
|
channel_index, GVFG_EINVAL,
|
|
"gvfg_get_channel_hdmi_audio_info rejected: out_info is null");
|
|
*out_info = {};
|
|
auto *channel = handle->findChannel(channel_index);
|
|
if (!channel || !channel->session)
|
|
return handle->rejectChannel(
|
|
channel_index, GVFG_ESTATE,
|
|
"gvfg_get_channel_hdmi_audio_info rejected: channel is not open");
|
|
return channel->session->get_hdmi_audio_info(*out_info);
|
|
}
|
|
|
|
gvfg_status_t gvfg_get_chip_info(gvfg_handle handle, gvfg_chip_info_t *out_info)
|
|
{
|
|
if (!handle)
|
|
return GVFG_EINVAL;
|
|
if (!out_info)
|
|
return handle->rejectAllChannels(GVFG_EINVAL,
|
|
"gvfg_get_chip_info rejected: out_info is null");
|
|
*out_info = {};
|
|
auto *channel = handle->findOpenChannel();
|
|
if (!channel || !channel->session)
|
|
return handle->rejectAllChannels(GVFG_ESTATE,
|
|
"gvfg_get_chip_info rejected: device is not open");
|
|
return channel->session->get_chip_info(*out_info);
|
|
}
|
|
|
|
const char *gvfg_get_version(void)
|
|
{
|
|
return GVFG_VERSION_STRING;
|
|
}
|
|
|
|
const char *gvfg_get_gigabyte_lib_version(void)
|
|
{
|
|
return GVFG_SDK_VER;
|
|
}
|
|
|
|
const char *gvfg_pixel_format_name(int pixel_format)
|
|
{
|
|
return pixel_format_name(pixel_format);
|
|
}
|
|
|
|
const char *gvfg_strerror(gvfg_status_t status)
|
|
{
|
|
switch (status)
|
|
{
|
|
case GVFG_OK:
|
|
return "OK";
|
|
case GVFG_EINVAL:
|
|
return "Invalid argument";
|
|
case GVFG_ENODEV:
|
|
return "No GVFG device";
|
|
case GVFG_ESTATE:
|
|
return "Invalid state";
|
|
case GVFG_EIO:
|
|
return "I/O error";
|
|
case GVFG_ENOTSUP:
|
|
return "Not supported";
|
|
case GVFG_ETIMEOUT:
|
|
return "Timeout";
|
|
case GVFG_EBUSY:
|
|
return "Device busy";
|
|
default:
|
|
return "Unknown";
|
|
}
|
|
}
|
|
|
|
gvfg_status_t gvfg_get_channel_last_sdk_error_detail(gvfg_handle handle,
|
|
int channel_index,
|
|
char *out_message,
|
|
uint32_t out_message_size)
|
|
{
|
|
if (!handle)
|
|
return GVFG_EINVAL;
|
|
return handle->copyChannelError(channel_index, out_message, out_message_size);
|
|
}
|
|
|
|
gvfg_status_t gvfg_debug_get_channel_backend_stats(gvfg_handle handle,
|
|
int channel_index,
|
|
gvfg_debug_backend_stats_t *out_stats)
|
|
{
|
|
if (!handle || !out_stats)
|
|
return GVFG_EINVAL;
|
|
|
|
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
|
|
if (!channel)
|
|
return GVFG_ESTATE;
|
|
gvfg_debug_backend_stats_t stats{};
|
|
const gvfg_status_t status = channel->getDebugStats(stats);
|
|
if (status != GVFG_OK)
|
|
return status;
|
|
|
|
*out_stats = stats;
|
|
return GVFG_OK;
|
|
}
|
|
|
|
gvfg_status_t gvfg_debug_set_user_led(gvfg_handle handle, int on)
|
|
{
|
|
if (!handle || (on != 0 && on != 1))
|
|
return GVFG_EINVAL;
|
|
auto *channel = handle->findOpenChannel();
|
|
if (!channel || !channel->session)
|
|
return handle->rejectAllChannels(GVFG_ESTATE,
|
|
"gvfg_debug_set_user_led rejected: device is not open");
|
|
return channel->session->set_user_led(on != 0);
|
|
}
|
|
|
|
gvfg_status_t gvfg_debug_get_device_capabilities(
|
|
gvfg_handle handle, gvfg_debug_device_capabilities_t *out_capabilities)
|
|
{
|
|
if (!handle || !out_capabilities)
|
|
return GVFG_EINVAL;
|
|
gvfg_channel_session_t *channel = handle->findOpenChannel();
|
|
if (!channel || !channel->session)
|
|
return handle->rejectAllChannels(
|
|
GVFG_ESTATE,
|
|
"gvfg_debug_get_device_capabilities rejected: no channel is open");
|
|
return channel->session->get_device_capabilities(*out_capabilities);
|
|
}
|
|
|
|
gvfg_status_t gvfg_debug_read_register(gvfg_handle handle,
|
|
uint32_t offset,
|
|
uint32_t *out_value)
|
|
{
|
|
if (!handle || !out_value || (offset & 0x3u) != 0)
|
|
return GVFG_EINVAL;
|
|
return handle->debugReadRegister(offset, *out_value);
|
|
}
|
|
|
|
gvfg_status_t gvfg_debug_write_register(gvfg_handle handle,
|
|
uint32_t offset,
|
|
uint32_t value)
|
|
{
|
|
if (!handle || (offset & 0x3u) != 0)
|
|
return GVFG_EINVAL;
|
|
return handle->debugWriteRegister(offset, value);
|
|
}
|
|
|
|
}
|