Compared with 350e8e5:
- perform one fresh GigabyteLib signal query for each explicit channel Start and reject a no-lock request with GVFG_ETIMEOUT instead of entering a placeholder monitoring stream
- keep running signal-status reads on the SDK cache and synchronize format-change, input plug-in, and input unplug state before publishing the corresponding event
- invalidate cached video information when the output depth changes so format selection does not cause an extra immediate hardware read
- make the Qt Sample refresh SDI information after a successful Start and on signal events without polling it from the periodic status timer
- close the Sample device session after the final running channel stops or an idle Start path fails, while preserving the selected device for the next Start
- document the updated Start result, event/cache ordering, query boundaries, and Sample ownership policy across the public API and internal flow guides
Validation: git diff --check passed. The SDK and Qt Sample builds completed successfully during this change. Hardware no-signal, plug/unplug, dual-process handoff, and recording tests were not rerun for this commit.
1333 lines
48 KiB
C++
1333 lines
48 KiB
C++
#include "gvfg_capture.h"
|
|
|
|
#include "gvfg_debug.h"
|
|
#include "gigabyte_capture_session.h"
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <climits>
|
|
#include <condition_variable>
|
|
#include <cstddef>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <deque>
|
|
#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;
|
|
}
|
|
|
|
currentIndex = index;
|
|
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)
|
|
{
|
|
const gvfg_status_t audioStatus = session->get_audio_format(cachedAudioFormat);
|
|
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();
|
|
}
|
|
currentIndex = -1;
|
|
}
|
|
|
|
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();
|
|
gvfg_event_t out{};
|
|
out.struct_size = sizeof(out);
|
|
out.type = event;
|
|
eventQueue.push_back(out);
|
|
}
|
|
eventCv.notify_one();
|
|
}
|
|
|
|
gvfg_status_t querySignal()
|
|
{
|
|
if (!session)
|
|
return GVFG_ESTATE;
|
|
|
|
gvfg_signal_status_t sig{};
|
|
const gvfg_status_t status = session->get_signal_status(sig);
|
|
|
|
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{};
|
|
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 GVFG_OK;
|
|
}
|
|
|
|
gvfg_status_t configureStream()
|
|
{
|
|
if (!session)
|
|
return reject(GVFG_ESTATE, "configure stream rejected: channel is not open");
|
|
|
|
const gvfg_status_t st = session->configure_stream();
|
|
if (st != GVFG_OK)
|
|
return st;
|
|
const gvfg_status_t signalStatus = querySignal();
|
|
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 GVFG_OK;
|
|
}
|
|
|
|
gvfg_status_t readFrame(gvfg_frame_t &out, uint32_t timeoutMs)
|
|
{
|
|
std::memset(&out, 0, sizeof(out));
|
|
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);
|
|
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");
|
|
}
|
|
|
|
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;
|
|
readInProgress = false;
|
|
frameHeld = true;
|
|
}
|
|
}
|
|
if (stoppedWhileWaiting)
|
|
{
|
|
session->release_frame();
|
|
return GVFG_ESTATE;
|
|
}
|
|
|
|
updateRuntimeFps(now_ns());
|
|
|
|
out.data = frame.data;
|
|
out.data_size = static_cast<uint64_t>(frame.data_size);
|
|
out.width = static_cast<int>(frame.width);
|
|
out.height = static_cast<int>(frame.height);
|
|
const gvfg_status_t strideStatus =
|
|
native_row_stride_bytes(frame.width, frame.pixel_format, out.row_stride_bytes);
|
|
if (strideStatus != GVFG_OK)
|
|
{
|
|
session->release_frame();
|
|
std::lock_guard<std::mutex> lock(frameMutex);
|
|
heldSessionFrame = {};
|
|
frameHeld = false;
|
|
return reject(strideStatus, "gvfg_read_channel_frame received an invalid row stride");
|
|
}
|
|
out.pixel_format = frame.pixel_format;
|
|
out.bit_depth = static_cast<int>(frame.bit_depth);
|
|
out.frame_id = frame.frame_id;
|
|
out.timestamp_ns = frame.timestamp_ns;
|
|
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");
|
|
|
|
const gvfg_status_t st = session->release_frame();
|
|
if (st != GVFG_OK)
|
|
return st;
|
|
|
|
heldSessionFrame = {};
|
|
frameHeld = false;
|
|
}
|
|
|
|
return GVFG_OK;
|
|
}
|
|
|
|
void releaseHeldFrameForStop()
|
|
{
|
|
if (!session)
|
|
return;
|
|
|
|
gvfg_frame_t frame{};
|
|
bool shouldRelease = false;
|
|
{
|
|
std::lock_guard<std::mutex> lock(frameMutex);
|
|
if (frameHeld)
|
|
{
|
|
frame = heldSessionFrame;
|
|
heldSessionFrame = {};
|
|
frameHeld = false;
|
|
shouldRelease = true;
|
|
}
|
|
}
|
|
|
|
if (shouldRelease)
|
|
session->release_frame();
|
|
}
|
|
|
|
gvfg_status_t pollEvent(gvfg_event_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 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;
|
|
cachedAudioFormat = format;
|
|
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;
|
|
}
|
|
|
|
const GigabyteAudioInfo format = cachedAudioFormat;
|
|
gvfg_status_t status = format.frame_bytes != 0 ? GVFG_OK : GVFG_ESTATE;
|
|
uint32_t bytes = 0;
|
|
if (status == GVFG_OK)
|
|
{
|
|
audioBuffer.resize(format.frame_bytes);
|
|
status = session->wait_audio(timeoutMs, audioBuffer.data(),
|
|
static_cast<uint32_t>(audioBuffer.size()), bytes);
|
|
}
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lock(audioMutex);
|
|
audioReadInProgress = false;
|
|
if (status != GVFG_OK)
|
|
return status;
|
|
if (!running.load(std::memory_order_acquire) || bytes == 0 || bytes > audioBuffer.size())
|
|
return reject(GVFG_ESTATE, "gvfg_read_channel_audio_frame completed after capture stopped or returned invalid data");
|
|
audioFrameHeld = true;
|
|
out.data = audioBuffer.data();
|
|
out.data_size = 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;
|
|
int currentIndex = -1;
|
|
uint32_t selectedChannel = GVFG_CHANNEL_0;
|
|
bool zeroCopyRequested = false;
|
|
bool audioEnabled = false;
|
|
GigabyteAudioInfo cachedAudioFormat{};
|
|
|
|
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;
|
|
uint64_t videoFrameId = 0;
|
|
gvfg_frame_t heldSessionFrame{};
|
|
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_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)
|
|
{
|
|
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)
|
|
{
|
|
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() ? nullptr : &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");
|
|
|
|
return channel->setVideoFormat(format);
|
|
}
|
|
|
|
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};
|
|
};
|
|
|
|
extern "C"
|
|
{
|
|
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_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_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_t *out_event,
|
|
uint32_t timeout_ms)
|
|
{
|
|
if (!handle)
|
|
return GVFG_EINVAL;
|
|
if (!out_event || out_event->struct_size < sizeof(gvfg_event_t))
|
|
return handle->rejectChannel(channel_index,
|
|
GVFG_EINVAL,
|
|
"gvfg_poll_channel_event rejected: output event is invalid");
|
|
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
|
|
if (!channel)
|
|
return handle->rejectChannel(channel_index,
|
|
GVFG_ESTATE,
|
|
"gvfg_poll_channel_event rejected: channel is not open");
|
|
gvfg_event_t event{};
|
|
event.struct_size = sizeof(event);
|
|
const gvfg_status_t status = channel->pollEvent(event, timeout_ms);
|
|
if (status == GVFG_OK)
|
|
*out_event = 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_device_capabilities(
|
|
gvfg_handle handle, gvfg_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_get_device_capabilities rejected: no channel is open");
|
|
return channel->session->get_device_capabilities(*out_capabilities);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
const char *gvfg_get_version(void)
|
|
{
|
|
return GVFG_VERSION_STRING;
|
|
}
|
|
|
|
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_HRESULT_ERROR:
|
|
return "GVFG_HRESULT_ERROR";
|
|
case GVFG_HRESULT_DEV_ERROR:
|
|
return "GVFG_HRESULT_DEV_ERROR";
|
|
case GVFG_HRESULT_DEV_BUSY:
|
|
return "GVFG_HRESULT_DEV_BUSY";
|
|
case GVFG_HRESULT_VIDEO_CHN_ERROR:
|
|
return "GVFG_HRESULT_VIDEO_CHN_ERROR";
|
|
case GVFG_HRESULT_CONTEXT_ERROR:
|
|
return "GVFG_HRESULT_CONTEXT_ERROR";
|
|
case GVFG_HRESULT_VIDEO_CHN_INVALID:
|
|
return "GVFG_HRESULT_VIDEO_CHN_INVALID";
|
|
case GVFG_HRESULT_API_ERROR:
|
|
return "GVFG_HRESULT_API_ERROR";
|
|
default:
|
|
return status > 0 ? "GVFG_HRESULT_UNKNOWN" : "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_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);
|
|
}
|
|
|
|
}
|