Add CH0 audio capture and customer runtime diagnostics
Extend the internal driver ABI and public GVFG API for combined CH0 video/audio capture, PCM format discovery, blocking copy-out reads, extra-frame wake events, and coordinated teardown of video and audio readers. Update the Qt sample with optional CH0 audio playback on a dedicated thread, customer-visible copy/zero-copy timing telemetry, mutually exclusive CH0/CH1 Y210 selection, and a refresh-safe temporary hide for the unsupported CH1 panel. Harden Win32 error text conversion, document the new stream and ownership contracts, and package giga_ioctl plus the VC143 runtime in customer Debug/Release bundles. Validation: git diff --check passed before commit. No build, package execution, runtime, audio-device, or capture-hardware test was run.
This commit is contained in:
@@ -58,6 +58,16 @@ typedef struct pcies2mm_frame_t
|
||||
uint32_t bit_depth;
|
||||
} pcies2mm_frame_t;
|
||||
|
||||
typedef struct pcies2mm_audio_format_t
|
||||
{
|
||||
uint32_t sample_rate;
|
||||
uint32_t channels;
|
||||
uint32_t bits_per_sample;
|
||||
uint32_t frames_per_second;
|
||||
uint32_t frame_bytes;
|
||||
uint32_t block_align;
|
||||
} pcies2mm_audio_format_t;
|
||||
|
||||
typedef struct pcies2mm_stream_stats_t
|
||||
{
|
||||
pcies2mm_stream_state_t state;
|
||||
|
||||
@@ -61,22 +61,23 @@ namespace
|
||||
|
||||
static std::string win32_error(DWORD err)
|
||||
{
|
||||
char *msg = nullptr;
|
||||
const DWORD len = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||
wchar_t *msg = nullptr;
|
||||
const DWORD len = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||
FORMAT_MESSAGE_FROM_SYSTEM |
|
||||
FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||
nullptr,
|
||||
err,
|
||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
reinterpret_cast<LPSTR>(&msg),
|
||||
reinterpret_cast<LPWSTR>(&msg),
|
||||
0,
|
||||
nullptr);
|
||||
std::string out;
|
||||
if (len && msg)
|
||||
{
|
||||
out.assign(msg, msg + len);
|
||||
while (!out.empty() && (out.back() == '\r' || out.back() == '\n' || out.back() == ' '))
|
||||
out.pop_back();
|
||||
std::wstring wide(msg, msg + len);
|
||||
while (!wide.empty() && (wide.back() == L'\r' || wide.back() == L'\n' || wide.back() == L' '))
|
||||
wide.pop_back();
|
||||
out = wide_to_utf8(wide);
|
||||
}
|
||||
if (msg)
|
||||
LocalFree(msg);
|
||||
@@ -311,6 +312,55 @@ namespace gvfg::internal
|
||||
return PCIES2MM_OK;
|
||||
}
|
||||
|
||||
pcies2mm_status_t PcieS2mmCaptureSession::set_audio_enabled(bool enabled)
|
||||
{
|
||||
if (device_handle() == INVALID_HANDLE_VALUE)
|
||||
return reject(PCIES2MM_ESTATE, "set audio enabled rejected: device is not open");
|
||||
if (running_)
|
||||
return reject(PCIES2MM_ESTATE, "set audio enabled rejected: stream is running");
|
||||
if (enabled && active_channel() != 0)
|
||||
return reject(PCIES2MM_ENOTSUP, "audio capture is currently supported only on channel 0");
|
||||
|
||||
if (audio_enabled_ == enabled)
|
||||
return PCIES2MM_OK;
|
||||
|
||||
// Event handles are created according to audio_enabled_. Rebuild the
|
||||
// registrations when the stopped channel changes between video-only
|
||||
// and video+audio; otherwise the new audio handles remain null.
|
||||
const bool wasMonitoring = monitoring_.load(std::memory_order_acquire);
|
||||
if (wasMonitoring)
|
||||
stop_event_monitoring();
|
||||
audio_enabled_ = enabled;
|
||||
if (wasMonitoring && !start_event_monitoring())
|
||||
return fail(PCIES2MM_EIO, "restart event monitoring after audio stream change");
|
||||
return PCIES2MM_OK;
|
||||
}
|
||||
|
||||
pcies2mm_status_t PcieS2mmCaptureSession::get_audio_format(pcies2mm_audio_format_t &out) const
|
||||
{
|
||||
std::memset(&out, 0, sizeof(out));
|
||||
if (device_handle() == INVALID_HANDLE_VALUE)
|
||||
return reject(PCIES2MM_ESTATE, "get audio format rejected: device is not open");
|
||||
if (active_channel() != 0)
|
||||
return reject(PCIES2MM_ENOTSUP, "audio capture is currently supported only on channel 0");
|
||||
|
||||
giga_ioctl_audio_info info{};
|
||||
if (!giga_ioctl_get_audio_info(device_handle(), active_channel(), &info))
|
||||
return fail(PCIES2MM_EIO, "get audio info");
|
||||
if (info.channels == 0 || info.samples_per_second == 0 ||
|
||||
info.bits_per_sample == 0 || info.frames_per_second == 0 ||
|
||||
info.frame_buffer_size == 0 || (info.bits_per_sample % 8) != 0)
|
||||
return reject(PCIES2MM_EIO, "driver returned an invalid audio format");
|
||||
|
||||
out.sample_rate = info.samples_per_second;
|
||||
out.channels = info.channels;
|
||||
out.bits_per_sample = info.bits_per_sample;
|
||||
out.frames_per_second = info.frames_per_second;
|
||||
out.frame_bytes = info.frame_buffer_size;
|
||||
out.block_align = info.channels * (info.bits_per_sample / 8);
|
||||
return PCIES2MM_OK;
|
||||
}
|
||||
|
||||
pcies2mm_status_t PcieS2mmCaptureSession::get_signal_status(pcies2mm_signal_status_t &out) const
|
||||
{
|
||||
if (device_handle() == INVALID_HANDLE_VALUE)
|
||||
@@ -433,6 +483,7 @@ namespace gvfg::internal
|
||||
held_frame_ = {};
|
||||
frame_held_ = false;
|
||||
read_in_progress_ = false;
|
||||
audio_read_in_progress_ = false;
|
||||
active_delivery_started_ = {};
|
||||
latest_sequence_ = 0;
|
||||
delivered_sequence_ = 0;
|
||||
@@ -490,14 +541,20 @@ namespace gvfg::internal
|
||||
stream_ready_pending_ = false;
|
||||
|
||||
if (device_handle() != INVALID_HANDLE_VALUE)
|
||||
stop_video(active_channel());
|
||||
stop_capture(active_channel());
|
||||
|
||||
if (dma_event_)
|
||||
SetEvent(dma_event_);
|
||||
if (audio_event_)
|
||||
SetEvent(audio_event_);
|
||||
if (extra_video_event_)
|
||||
SetEvent(extra_video_event_);
|
||||
if (extra_audio_event_)
|
||||
SetEvent(extra_audio_event_);
|
||||
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
read_finished_cv_.wait(lock, [this] { return !read_in_progress_; });
|
||||
read_finished_cv_.wait(lock, [this] { return !read_in_progress_ && !audio_read_in_progress_; });
|
||||
}
|
||||
const pcies2mm_status_t releaseStatus = release_all_zero_copy_frames(true);
|
||||
stop_event_monitoring();
|
||||
@@ -506,11 +563,30 @@ namespace gvfg::internal
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
held_frame_ = {};
|
||||
frame_held_ = false;
|
||||
audio_read_in_progress_ = false;
|
||||
stats_.state = configured_ ? PCIES2MM_STREAM_CONFIGURED : PCIES2MM_STREAM_STOPPED;
|
||||
}
|
||||
return releaseStatus;
|
||||
}
|
||||
|
||||
pcies2mm_status_t PcieS2mmCaptureSession::ensure_capture_started()
|
||||
{
|
||||
if (capture_active_.load(std::memory_order_acquire))
|
||||
return PCIES2MM_OK;
|
||||
if (!signal_presence_known_.load(std::memory_order_acquire) ||
|
||||
!signal_present_.load(std::memory_order_acquire))
|
||||
return PCIES2MM_ETIMEOUT;
|
||||
if (!ResetEvent(dma_event_) || !ResetEvent(extra_video_event_) ||
|
||||
(audio_enabled_ &&
|
||||
(!ResetEvent(audio_event_) || !ResetEvent(extra_audio_event_))))
|
||||
return fail(PCIES2MM_EIO, "reset capture event");
|
||||
if (!start_capture(active_channel()))
|
||||
return fail(PCIES2MM_EIO, "enable reader capture");
|
||||
capture_active_.store(true, std::memory_order_release);
|
||||
stream_ready_pending_.store(true, std::memory_order_release);
|
||||
return PCIES2MM_OK;
|
||||
}
|
||||
|
||||
pcies2mm_status_t PcieS2mmCaptureSession::wait_frame(uint32_t timeoutMs, pcies2mm_frame_t &out)
|
||||
{
|
||||
constexpr uint64_t kTimingWarmupFrames = 30;
|
||||
@@ -520,28 +596,9 @@ namespace gvfg::internal
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
if (!running_ || frame_held_ || read_in_progress_)
|
||||
return reject(PCIES2MM_ESTATE, "wait frame rejected: stream state does not allow another read");
|
||||
if (!capture_active_.load(std::memory_order_acquire))
|
||||
{
|
||||
if (!signal_presence_known_.load(std::memory_order_acquire) ||
|
||||
!signal_present_.load(std::memory_order_acquire))
|
||||
return PCIES2MM_ETIMEOUT;
|
||||
if (!ResetEvent(dma_event_))
|
||||
{
|
||||
const DWORD err = GetLastError();
|
||||
++stats_.dma_errors;
|
||||
lock.unlock();
|
||||
return fail(PCIES2MM_EIO, "reset DMA event", err);
|
||||
}
|
||||
if (!start_video(active_channel()))
|
||||
{
|
||||
const DWORD err = GetLastError();
|
||||
++stats_.dma_errors;
|
||||
lock.unlock();
|
||||
return fail(PCIES2MM_EIO, "enable reader capture", err);
|
||||
}
|
||||
capture_active_.store(true, std::memory_order_release);
|
||||
stream_ready_pending_.store(true, std::memory_order_release);
|
||||
}
|
||||
const pcies2mm_status_t startStatus = ensure_capture_started();
|
||||
if (startStatus != PCIES2MM_OK)
|
||||
return startStatus;
|
||||
read_in_progress_ = true;
|
||||
}
|
||||
|
||||
@@ -573,7 +630,8 @@ namespace gvfg::internal
|
||||
}
|
||||
}
|
||||
|
||||
const DWORD waitResult = WaitForSingleObject(dma_event_, waitMs);
|
||||
HANDLE videoReadyEvents[] = {dma_event_, extra_video_event_};
|
||||
const DWORD waitResult = WaitForMultipleObjects(2, videoReadyEvents, FALSE, waitMs);
|
||||
if (waitResult == WAIT_TIMEOUT)
|
||||
{
|
||||
{
|
||||
@@ -583,7 +641,7 @@ namespace gvfg::internal
|
||||
finishRead();
|
||||
return PCIES2MM_ETIMEOUT;
|
||||
}
|
||||
if (waitResult != WAIT_OBJECT_0)
|
||||
if (waitResult != WAIT_OBJECT_0 && waitResult != WAIT_OBJECT_0 + 1)
|
||||
{
|
||||
const DWORD err = GetLastError();
|
||||
finishRead();
|
||||
@@ -678,6 +736,109 @@ namespace gvfg::internal
|
||||
return PCIES2MM_OK;
|
||||
}
|
||||
|
||||
pcies2mm_status_t PcieS2mmCaptureSession::wait_audio(uint32_t timeoutMs,
|
||||
void *destination,
|
||||
uint32_t destinationCapacity,
|
||||
uint32_t &outBytes)
|
||||
{
|
||||
outBytes = 0;
|
||||
if (!destination || destinationCapacity == 0)
|
||||
return reject(PCIES2MM_EINVAL, "wait audio rejected: destination is invalid");
|
||||
|
||||
pcies2mm_audio_format_t format{};
|
||||
const pcies2mm_status_t formatStatus = get_audio_format(format);
|
||||
if (formatStatus != PCIES2MM_OK)
|
||||
return formatStatus;
|
||||
if (destinationCapacity < format.frame_bytes)
|
||||
return reject(PCIES2MM_EINVAL, "wait audio rejected: destination is smaller than the driver frame");
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (!running_ || !audio_enabled_ || audio_read_in_progress_)
|
||||
return reject(PCIES2MM_ESTATE, "wait audio rejected: stream state does not allow another audio read");
|
||||
const pcies2mm_status_t startStatus = ensure_capture_started();
|
||||
if (startStatus != PCIES2MM_OK)
|
||||
return startStatus;
|
||||
audio_read_in_progress_ = true;
|
||||
}
|
||||
|
||||
const auto finishRead = [this]() {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
audio_read_in_progress_ = false;
|
||||
read_finished_cv_.notify_all();
|
||||
};
|
||||
const bool infiniteWait = timeoutMs == UINT32_MAX;
|
||||
const auto deadline = infiniteWait
|
||||
? (std::chrono::steady_clock::time_point::max)()
|
||||
: std::chrono::steady_clock::now() + std::chrono::milliseconds(timeoutMs);
|
||||
|
||||
for (;;)
|
||||
{
|
||||
DWORD waitMs = INFINITE;
|
||||
if (!infiniteWait)
|
||||
{
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
if (now >= deadline)
|
||||
waitMs = 0;
|
||||
else
|
||||
{
|
||||
const auto remainingUs = std::chrono::duration_cast<std::chrono::microseconds>(deadline - now).count();
|
||||
waitMs = static_cast<DWORD>((remainingUs + 999) / 1000);
|
||||
}
|
||||
}
|
||||
|
||||
HANDLE audioReadyEvents[] = {audio_event_, extra_audio_event_};
|
||||
const DWORD waitResult = WaitForMultipleObjects(2, audioReadyEvents, FALSE, waitMs);
|
||||
if (waitResult == WAIT_TIMEOUT)
|
||||
{
|
||||
finishRead();
|
||||
return PCIES2MM_ETIMEOUT;
|
||||
}
|
||||
if (waitResult != WAIT_OBJECT_0 && waitResult != WAIT_OBJECT_0 + 1)
|
||||
{
|
||||
const DWORD err = GetLastError();
|
||||
finishRead();
|
||||
return fail(PCIES2MM_EIO, "wait audio event", err);
|
||||
}
|
||||
if (!running_.load(std::memory_order_acquire))
|
||||
{
|
||||
finishRead();
|
||||
return PCIES2MM_ESTATE;
|
||||
}
|
||||
if (!capture_active_.load(std::memory_order_acquire))
|
||||
{
|
||||
finishRead();
|
||||
return PCIES2MM_ETIMEOUT;
|
||||
}
|
||||
|
||||
const int ret = get_audio_frame(active_channel(),
|
||||
(std::numeric_limits<uint32_t>::max)(),
|
||||
destination,
|
||||
destinationCapacity);
|
||||
if (ret >= 0)
|
||||
{
|
||||
if (ret == 0 || static_cast<uint32_t>(ret) > format.frame_bytes)
|
||||
{
|
||||
finishRead();
|
||||
return reject(PCIES2MM_EIO, "driver returned an invalid audio byte count");
|
||||
}
|
||||
if (static_cast<uint32_t>(ret) != format.frame_bytes)
|
||||
PCIES2MM_LOG("audio frame size mismatch: expected=%u returned=%d",
|
||||
format.frame_bytes, ret);
|
||||
outBytes = static_cast<uint32_t>(ret);
|
||||
finishRead();
|
||||
return PCIES2MM_OK;
|
||||
}
|
||||
|
||||
const DWORD frameError = GetLastError();
|
||||
if (!is_transient_frame_not_ready_error(frameError))
|
||||
{
|
||||
finishRead();
|
||||
return fail(PCIES2MM_EIO, "copy get audio frame", frameError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pcies2mm_status_t PcieS2mmCaptureSession::release_frame(const pcies2mm_frame_t &frame)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
@@ -766,6 +927,20 @@ namespace gvfg::internal
|
||||
return giga_ioctl_video_stop(device_handle(), channelIndex) != FALSE;
|
||||
}
|
||||
|
||||
bool PcieS2mmCaptureSession::start_capture(uint32_t channelIndex) const
|
||||
{
|
||||
return audio_enabled_
|
||||
? giga_ioctl_start_video_audio(device_handle(), channelIndex) != FALSE
|
||||
: start_video(channelIndex);
|
||||
}
|
||||
|
||||
bool PcieS2mmCaptureSession::stop_capture(uint32_t channelIndex) const
|
||||
{
|
||||
return audio_enabled_
|
||||
? giga_ioctl_stop_video_audio(device_handle(), channelIndex) != FALSE
|
||||
: stop_video(channelIndex);
|
||||
}
|
||||
|
||||
bool PcieS2mmCaptureSession::acquire_zero_copy_frame(uint32_t channelIndex,
|
||||
const uint8_t *&outData) const
|
||||
{
|
||||
@@ -821,14 +996,28 @@ namespace gvfg::internal
|
||||
bool PcieS2mmCaptureSession::create_and_register_events(uint32_t channelIndex)
|
||||
{
|
||||
dma_event_ = CreateEventW(nullptr, FALSE, FALSE, nullptr);
|
||||
extra_video_event_ = CreateEventW(nullptr, FALSE, FALSE, nullptr);
|
||||
if (audio_enabled_)
|
||||
{
|
||||
audio_event_ = CreateEventW(nullptr, FALSE, FALSE, nullptr);
|
||||
extra_audio_event_ = CreateEventW(nullptr, FALSE, FALSE, nullptr);
|
||||
}
|
||||
format_change_event_ = CreateEventW(nullptr, FALSE, FALSE, nullptr);
|
||||
plug_in_event_ = CreateEventW(nullptr, FALSE, FALSE, nullptr);
|
||||
plug_out_event_ = CreateEventW(nullptr, FALSE, FALSE, nullptr);
|
||||
if (!dma_event_ || !format_change_event_ || !plug_in_event_ || !plug_out_event_)
|
||||
if (!dma_event_ || !extra_video_event_ ||
|
||||
(audio_enabled_ && (!audio_event_ || !extra_audio_event_)) ||
|
||||
!format_change_event_ || !plug_in_event_ || !plug_out_event_)
|
||||
return false;
|
||||
|
||||
if (!register_event(channelIndex, GIGA_IOCTL_EVENT_VIDEO_DMA, dma_event_))
|
||||
return false;
|
||||
if (!register_event(channelIndex, GIGA_IOCTL_EVENT_EXTRA_VIDEO_FRAME, extra_video_event_))
|
||||
return false;
|
||||
if (audio_enabled_ &&
|
||||
(!register_event(channelIndex, GIGA_IOCTL_EVENT_AUDIO_DMA, audio_event_) ||
|
||||
!register_event(channelIndex, GIGA_IOCTL_EVENT_EXTRA_AUDIO_FRAME, extra_audio_event_)))
|
||||
return false;
|
||||
if ((event_mask_filter_ & PCIES2MM_EVENT_MASK_FORMAT_CHANGE_BEGIN) != 0 &&
|
||||
!register_event(channelIndex, GIGA_IOCTL_EVENT_VIDEO_FORMAT_CHANGE, format_change_event_))
|
||||
return false;
|
||||
@@ -844,6 +1033,12 @@ namespace gvfg::internal
|
||||
void PcieS2mmCaptureSession::unregister_events(uint32_t channelIndex)
|
||||
{
|
||||
unregister_event(channelIndex, GIGA_IOCTL_EVENT_VIDEO_DMA);
|
||||
unregister_event(channelIndex, GIGA_IOCTL_EVENT_EXTRA_VIDEO_FRAME);
|
||||
if (audio_enabled_)
|
||||
{
|
||||
unregister_event(channelIndex, GIGA_IOCTL_EVENT_AUDIO_DMA);
|
||||
unregister_event(channelIndex, GIGA_IOCTL_EVENT_EXTRA_AUDIO_FRAME);
|
||||
}
|
||||
if ((event_mask_filter_ & PCIES2MM_EVENT_MASK_FORMAT_CHANGE_BEGIN) != 0)
|
||||
unregister_event(channelIndex, GIGA_IOCTL_EVENT_VIDEO_FORMAT_CHANGE);
|
||||
if ((event_mask_filter_ & PCIES2MM_EVENT_MASK_PLUG_IN) != 0)
|
||||
@@ -859,6 +1054,21 @@ namespace gvfg::internal
|
||||
CloseHandle(dma_event_);
|
||||
dma_event_ = nullptr;
|
||||
}
|
||||
if (audio_event_)
|
||||
{
|
||||
CloseHandle(audio_event_);
|
||||
audio_event_ = nullptr;
|
||||
}
|
||||
if (extra_video_event_)
|
||||
{
|
||||
CloseHandle(extra_video_event_);
|
||||
extra_video_event_ = nullptr;
|
||||
}
|
||||
if (extra_audio_event_)
|
||||
{
|
||||
CloseHandle(extra_audio_event_);
|
||||
extra_audio_event_ = nullptr;
|
||||
}
|
||||
if (format_change_event_)
|
||||
{
|
||||
CloseHandle(format_change_event_);
|
||||
@@ -912,6 +1122,12 @@ namespace gvfg::internal
|
||||
|
||||
if (dma_event_)
|
||||
SetEvent(dma_event_);
|
||||
if (audio_event_)
|
||||
SetEvent(audio_event_);
|
||||
if (extra_video_event_)
|
||||
SetEvent(extra_video_event_);
|
||||
if (extra_audio_event_)
|
||||
SetEvent(extra_audio_event_);
|
||||
if (capture_thread_.joinable())
|
||||
capture_thread_.join();
|
||||
|
||||
@@ -930,6 +1146,19 @@ namespace gvfg::internal
|
||||
return static_cast<int>(bytesReturned);
|
||||
}
|
||||
|
||||
int PcieS2mmCaptureSession::get_audio_frame(uint32_t channelIndex,
|
||||
uint32_t frameIndex,
|
||||
void *buffer,
|
||||
DWORD bufferSize) const
|
||||
{
|
||||
uint32_t bytesReturned = 0;
|
||||
const BOOL ok = giga_ioctl_get_audio_frame(device_handle(), channelIndex, frameIndex,
|
||||
buffer, bufferSize, &bytesReturned);
|
||||
if (!ok)
|
||||
return -1;
|
||||
return static_cast<int>(bytesReturned);
|
||||
}
|
||||
|
||||
void PcieS2mmCaptureSession::capture_thread_proc()
|
||||
{
|
||||
const uint32_t channel = active_channel();
|
||||
@@ -985,14 +1214,18 @@ namespace gvfg::internal
|
||||
signal_metadata_valid_ = false;
|
||||
}
|
||||
emit_event(PCIES2MM_EVENT_FORMAT_CHANGE_BEGIN);
|
||||
stop_video(channel);
|
||||
stop_capture(channel);
|
||||
capture_active_ = false;
|
||||
stream_ready_pending_ = false;
|
||||
if (dma_event_)
|
||||
SetEvent(dma_event_);
|
||||
if (audio_event_)
|
||||
SetEvent(audio_event_);
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
read_finished_cv_.wait(lock, [this] { return !read_in_progress_ && !frame_held_; });
|
||||
read_finished_cv_.wait(lock, [this] {
|
||||
return !read_in_progress_ && !audio_read_in_progress_ && !frame_held_;
|
||||
});
|
||||
}
|
||||
if (running_.load(std::memory_order_acquire))
|
||||
refresh_stream_from_registers(true);
|
||||
@@ -1006,10 +1239,12 @@ namespace gvfg::internal
|
||||
|
||||
if (capture_active_.exchange(false, std::memory_order_acq_rel))
|
||||
{
|
||||
stop_video(channel);
|
||||
stop_capture(channel);
|
||||
}
|
||||
if (dma_event_)
|
||||
SetEvent(dma_event_);
|
||||
if (audio_event_)
|
||||
SetEvent(audio_event_);
|
||||
signal_probe_active_.store(false, std::memory_order_release);
|
||||
signal_present_.store(true, std::memory_order_release);
|
||||
signal_presence_known_.store(true, std::memory_order_release);
|
||||
@@ -1020,7 +1255,9 @@ namespace gvfg::internal
|
||||
emit_event(PCIES2MM_EVENT_PLUG_IN);
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
read_finished_cv_.wait(lock, [this] { return !read_in_progress_ && !frame_held_; });
|
||||
read_finished_cv_.wait(lock, [this] {
|
||||
return !read_in_progress_ && !audio_read_in_progress_ && !frame_held_;
|
||||
});
|
||||
}
|
||||
if (running_.load(std::memory_order_acquire))
|
||||
refresh_stream_from_registers(true);
|
||||
@@ -1032,10 +1269,12 @@ namespace gvfg::internal
|
||||
stream_ready_pending_.store(false, std::memory_order_release);
|
||||
signal_present_.store(false, std::memory_order_release);
|
||||
signal_presence_known_.store(true, std::memory_order_release);
|
||||
stop_video(channel);
|
||||
stop_capture(channel);
|
||||
capture_active_ = false;
|
||||
if (dma_event_)
|
||||
SetEvent(dma_event_);
|
||||
if (audio_event_)
|
||||
SetEvent(audio_event_);
|
||||
emit_event(PCIES2MM_EVENT_PLUG_OUT);
|
||||
|
||||
{
|
||||
|
||||
@@ -41,14 +41,18 @@ namespace gvfg::internal
|
||||
pcies2mm_status_t set_channel(uint32_t channel);
|
||||
pcies2mm_status_t set_zero_copy_enabled(bool enabled);
|
||||
bool zero_copy_enabled() const { return zero_copy_enabled_; }
|
||||
pcies2mm_status_t set_audio_enabled(bool enabled);
|
||||
pcies2mm_status_t set_video_format(pcies2mm_pixel_format_t format);
|
||||
pcies2mm_status_t get_signal_status(pcies2mm_signal_status_t &out) const;
|
||||
pcies2mm_status_t get_audio_format(pcies2mm_audio_format_t &out) const;
|
||||
|
||||
pcies2mm_status_t set_event_callback(pcies2mm_event_callback_t callback, void *user, uint32_t eventMask);
|
||||
pcies2mm_status_t configure_stream(const pcies2mm_stream_desc_t &desc);
|
||||
pcies2mm_status_t start_stream();
|
||||
pcies2mm_status_t stop_stream();
|
||||
pcies2mm_status_t wait_frame(uint32_t timeoutMs, pcies2mm_frame_t &out);
|
||||
pcies2mm_status_t wait_audio(uint32_t timeoutMs, void *destination,
|
||||
uint32_t destinationCapacity, uint32_t &outBytes);
|
||||
pcies2mm_status_t release_frame(const pcies2mm_frame_t &frame);
|
||||
|
||||
void get_debug_stats(pcies2mm_stream_stats_t &outStats,
|
||||
@@ -66,6 +70,8 @@ namespace gvfg::internal
|
||||
bool write_reg(uint32_t offset, uint32_t value) const;
|
||||
bool start_video(uint32_t channelIndex) const;
|
||||
bool stop_video(uint32_t channelIndex) const;
|
||||
bool start_capture(uint32_t channelIndex) const;
|
||||
bool stop_capture(uint32_t channelIndex) const;
|
||||
bool acquire_zero_copy_frame(uint32_t channelIndex, const uint8_t *&outData) const;
|
||||
bool release_zero_copy_frame(uint32_t channelIndex) const;
|
||||
pcies2mm_status_t release_all_zero_copy_frames(bool includeInUse);
|
||||
@@ -77,6 +83,9 @@ namespace gvfg::internal
|
||||
void unregister_events(uint32_t channelIndex);
|
||||
void close_event_handles();
|
||||
int get_frame(uint32_t channelIndex, uint32_t frameIndex, uint8_t *buffer, DWORD bufferSize) const;
|
||||
int get_audio_frame(uint32_t channelIndex, uint32_t frameIndex,
|
||||
void *buffer, DWORD bufferSize) const;
|
||||
pcies2mm_status_t ensure_capture_started();
|
||||
|
||||
void capture_thread_proc();
|
||||
void handle_format_change_event(uint32_t channel);
|
||||
@@ -98,6 +107,9 @@ namespace gvfg::internal
|
||||
ChannelErrorState &error_state_;
|
||||
std::shared_ptr<PcieS2mmDeviceConnection> device_connection_;
|
||||
HANDLE dma_event_ = nullptr;
|
||||
HANDLE audio_event_ = nullptr;
|
||||
HANDLE extra_video_event_ = nullptr;
|
||||
HANDLE extra_audio_event_ = nullptr;
|
||||
HANDLE format_change_event_ = nullptr;
|
||||
HANDLE plug_in_event_ = nullptr;
|
||||
HANDLE plug_out_event_ = nullptr;
|
||||
@@ -107,6 +119,7 @@ namespace gvfg::internal
|
||||
uint32_t channel_ = 0;
|
||||
bool configured_ = false;
|
||||
bool zero_copy_enabled_ = false;
|
||||
bool audio_enabled_ = false;
|
||||
|
||||
std::atomic<bool> running_{false};
|
||||
std::atomic<bool> monitoring_{false};
|
||||
@@ -139,6 +152,7 @@ namespace gvfg::internal
|
||||
pcies2mm_frame_t held_frame_{};
|
||||
bool frame_held_ = false;
|
||||
bool read_in_progress_ = false;
|
||||
bool audio_read_in_progress_ = false;
|
||||
std::chrono::steady_clock::time_point active_delivery_started_{};
|
||||
bool stream_error_ = false;
|
||||
pcies2mm_stream_stats_t stats_{};
|
||||
|
||||
@@ -33,6 +33,8 @@ namespace
|
||||
static_assert(GVFG_EVENT_MASK_FORMAT_CHANGE_BEGIN == PCIES2MM_EVENT_MASK_FORMAT_CHANGE_BEGIN);
|
||||
|
||||
#if INTPTR_MAX == INT64_MAX
|
||||
static_assert(sizeof(gvfg_audio_format_t) == 24, "gvfg_audio_format_t ABI must remain frozen");
|
||||
static_assert(sizeof(gvfg_runtime_info_t) == 56, "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) == 48, "gvfg_frame_t x64 ABI must remain frozen");
|
||||
static_assert(offsetof(gvfg_frame_t, data) == 0);
|
||||
@@ -314,6 +316,11 @@ struct gvfg_channel_session_t
|
||||
pcies2mm_debug_state_t debugState{};
|
||||
uint64_t waitTimeouts = 0;
|
||||
backend->get_debug_stats(stats, waitTimeouts, debugState);
|
||||
out.zero_copy_enabled = debugState.get_frame_zero_copy;
|
||||
out.driver_read_samples = debugState.get_frame_timing_samples;
|
||||
out.driver_read_average_us = debugState.get_frame_timing_average_us;
|
||||
out.driver_read_max300_us = debugState.get_frame_timing_max300_us;
|
||||
out.driver_read_max_us = debugState.get_frame_timing_max_us;
|
||||
return GVFG_OK;
|
||||
}
|
||||
|
||||
@@ -666,6 +673,53 @@ struct gvfg_channel_session_t
|
||||
return map_status(status);
|
||||
}
|
||||
|
||||
gvfg_status_t setStreams(uint32_t value)
|
||||
{
|
||||
if (!backend)
|
||||
return reject(GVFG_ESTATE, "gvfg_set_channel_streams rejected: channel is not open");
|
||||
if (running)
|
||||
return reject(GVFG_ESTATE, "gvfg_set_channel_streams rejected: channel is running");
|
||||
const uint32_t supported = GVFG_STREAM_VIDEO | GVFG_STREAM_AUDIO;
|
||||
if ((value & ~supported) != 0 || (value & GVFG_STREAM_VIDEO) == 0)
|
||||
return reject(GVFG_EINVAL, "gvfg_set_channel_streams rejected: use video or video plus audio");
|
||||
|
||||
const bool enableAudio = (value & GVFG_STREAM_AUDIO) != 0;
|
||||
const pcies2mm_status_t status = backend->set_audio_enabled(enableAudio);
|
||||
if (status != PCIES2MM_OK)
|
||||
return map_status(status);
|
||||
streams = value;
|
||||
return GVFG_OK;
|
||||
}
|
||||
|
||||
gvfg_status_t getAudioFormat(gvfg_audio_format_t &out)
|
||||
{
|
||||
std::memset(&out, 0, sizeof(out));
|
||||
if (!backend)
|
||||
return reject(GVFG_ESTATE, "gvfg_get_channel_audio_format rejected: channel is not open");
|
||||
pcies2mm_audio_format_t format{};
|
||||
const pcies2mm_status_t status = backend->get_audio_format(format);
|
||||
if (status != PCIES2MM_OK)
|
||||
return map_status(status);
|
||||
out.sample_rate = format.sample_rate;
|
||||
out.channels = format.channels;
|
||||
out.bits_per_sample = format.bits_per_sample;
|
||||
out.frames_per_second = format.frames_per_second;
|
||||
out.frame_bytes = format.frame_bytes;
|
||||
out.block_align = format.block_align;
|
||||
return GVFG_OK;
|
||||
}
|
||||
|
||||
gvfg_status_t readAudio(void *destination, uint32_t destinationCapacity,
|
||||
uint32_t &outBytes, uint32_t timeoutMs)
|
||||
{
|
||||
outBytes = 0;
|
||||
if (!backend)
|
||||
return reject(GVFG_ESTATE, "gvfg_read_channel_audio rejected: channel is not open");
|
||||
if (!running || (streams & GVFG_STREAM_AUDIO) == 0)
|
||||
return reject(GVFG_ESTATE, "gvfg_read_channel_audio rejected: audio capture is not running");
|
||||
return map_status(backend->wait_audio(timeoutMs, destination, destinationCapacity, outBytes));
|
||||
}
|
||||
|
||||
gvfg_status_t debugWriteRegister(uint32_t offset, uint32_t value)
|
||||
{
|
||||
if (!backend)
|
||||
@@ -728,6 +782,7 @@ struct gvfg_channel_session_t
|
||||
int currentIndex = -1;
|
||||
uint32_t selectedChannel = GVFG_CHANNEL_0;
|
||||
bool zeroCopyRequested = false;
|
||||
uint32_t streams = GVFG_STREAM_VIDEO;
|
||||
uint32_t eventMask = GVFG_EVENT_MASK_ALL;
|
||||
|
||||
uint32_t width = 0;
|
||||
@@ -874,6 +929,31 @@ struct gvfg_handle_t
|
||||
return GVFG_OK;
|
||||
}
|
||||
|
||||
gvfg_status_t setChannelVideoFormat(int channelIndex, gvfg_pixel_format_t format)
|
||||
{
|
||||
gvfg_channel_session_t *channel = findChannel(channelIndex);
|
||||
if (!channel)
|
||||
return rejectChannel(channelIndex,
|
||||
GVFG_ESTATE,
|
||||
"gvfg_set_channel_video_format rejected: channel is not open");
|
||||
if (format != GVFG_PIXFMT_YUY2 && format != GVFG_PIXFMT_Y210)
|
||||
return rejectChannel(channelIndex,
|
||||
GVFG_EINVAL,
|
||||
"gvfg_set_channel_video_format rejected: pixel format is invalid");
|
||||
|
||||
const int otherChannel = channelIndex == GVFG_CHANNEL_0 ? GVFG_CHANNEL_1 : GVFG_CHANNEL_0;
|
||||
if (format == GVFG_PIXFMT_Y210 &&
|
||||
requestedFormats[static_cast<size_t>(otherChannel)] == GVFG_PIXFMT_Y210)
|
||||
return rejectChannel(channelIndex,
|
||||
GVFG_ENOTSUP,
|
||||
"both channels cannot use Y210 at the same time");
|
||||
|
||||
const gvfg_status_t status = channel->setVideoFormat(format);
|
||||
if (status == GVFG_OK)
|
||||
requestedFormats[static_cast<size_t>(channelIndex)] = format;
|
||||
return status;
|
||||
}
|
||||
|
||||
gvfg_status_t stopAll()
|
||||
{
|
||||
gvfg_status_t result = GVFG_OK;
|
||||
@@ -910,6 +990,7 @@ struct gvfg_handle_t
|
||||
std::array<uint32_t, 2> eventMasks{GVFG_EVENT_MASK_ALL, GVFG_EVENT_MASK_ALL};
|
||||
int currentIndex = -1;
|
||||
std::array<bool, 2> zeroCopyRequested{false, false};
|
||||
std::array<gvfg_pixel_format_t, 2> requestedFormats{GVFG_PIXFMT_YUY2, GVFG_PIXFMT_YUY2};
|
||||
};
|
||||
|
||||
extern "C"
|
||||
@@ -1025,14 +1106,40 @@ extern "C"
|
||||
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_streams(gvfg_handle handle,
|
||||
int channel_index,
|
||||
uint32_t streams)
|
||||
{
|
||||
if (!handle)
|
||||
return GVFG_EINVAL;
|
||||
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
|
||||
return channel ? channel->setVideoFormat(format)
|
||||
return channel ? channel->setStreams(streams)
|
||||
: handle->rejectChannel(channel_index,
|
||||
GVFG_ESTATE,
|
||||
"gvfg_set_channel_video_format rejected: channel is not open");
|
||||
"gvfg_set_channel_streams 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,
|
||||
@@ -1070,6 +1177,31 @@ extern "C"
|
||||
"gvfg_release_channel_frame rejected: channel is not open");
|
||||
}
|
||||
|
||||
gvfg_status_t gvfg_read_channel_audio(gvfg_handle handle,
|
||||
int channel_index,
|
||||
void *destination,
|
||||
uint32_t destination_capacity,
|
||||
uint32_t *out_bytes,
|
||||
uint32_t timeout_ms)
|
||||
{
|
||||
if (!handle)
|
||||
return GVFG_EINVAL;
|
||||
if (out_bytes)
|
||||
*out_bytes = 0;
|
||||
if (!destination || destination_capacity == 0 || !out_bytes)
|
||||
return handle->rejectChannel(channel_index,
|
||||
GVFG_EINVAL,
|
||||
"gvfg_read_channel_audio rejected: output buffer is invalid");
|
||||
gvfg_channel_session_t *channel = handle->findChannel(channel_index);
|
||||
return channel ? channel->readAudio(destination,
|
||||
destination_capacity,
|
||||
*out_bytes,
|
||||
timeout_ms)
|
||||
: handle->rejectChannel(channel_index,
|
||||
GVFG_ESTATE,
|
||||
"gvfg_read_channel_audio rejected: channel is not open");
|
||||
}
|
||||
|
||||
gvfg_status_t gvfg_poll_channel_event(gvfg_handle handle,
|
||||
int channel_index,
|
||||
gvfg_event_t *out_event,
|
||||
|
||||
Reference in New Issue
Block a user