Unify playback and harden preview/audio recovery for SDK 2.1.1

- expose one gvfg_playback DLL backed by static audio and preview libraries, retain public backend data types, and remove obsolete standalone backend exports and unused wrappers

- centralize playback clocks, timestamp scheduling, optional synchronization, audio rate correction, bounded queues and fallback while preserving legacy statistics buffer sizes

- provide still-image presentation and a helper-owned NO SIGNAL card with explicit SDK connection state, native window ownership and UI-thread checks

- report monitoring failures through EAUDIO without stopping video, retain the failed output state across timeline resets, and reject audio-only startup when output is unavailable

- make clearing an unconfigured preview a successful no-op so resetting before the first frame cannot leave playback stopped

- trace actual format-change events before event resets in Debug builds and refresh video metadata after plugin stream recovery while retaining the existing temporary HDMI lock bypass

- update SDK 2.1.1 public documentation, the playback guide and rendered API reference to describe the unified lifecycle, error handling and delivery boundaries

Validation: Full SDK Release build and git diff --cached --check passed. Native tests passed for video/reset lifecycle with synchronization enabled and disabled, signal-card lifecycle, bounded queue pressure and legacy statistics guards. Process-local WASAPI failure injection verified continued video, persistent monitoring failure and audio-only rejection; real WASAPI silence playback advanced the output clock in both synchronization modes. Public exports and rendered PDF pages/portable links were checked. No hardware capture, unplug/replug or long-running acoustic A/V synchronization test was performed.
This commit is contained in:
Mark.lin
2026-10-22 14:02:43 +08:00
parent da2ad24459
commit bfa9397f6b
36 changed files with 2389 additions and 2089 deletions
@@ -1,4 +1,4 @@
#include "gvfg_preview.h"
#include "preview_output_internal.h"
#include "d3d_conversion_pipeline.h"
@@ -124,7 +124,7 @@ public:
D3D11_MAPPED_SUBRESOURCE mapped{};
if (FAILED(deferred->Map(texture, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)))
{
lock.lock(); slot.state = SlotState::Free; return false;
lock.lock(); slot.state = SlotState::Free; idleCv_.notify_all(); return false;
}
const size_t rowBytes = frame.pixel_format == GVFG_PREVIEW_PIXFMT_Y210
? static_cast<size_t>(frame.width) * 4u
@@ -150,7 +150,7 @@ public:
ComPtr<ID3D11CommandList> commands;
if (FAILED(deferred->FinishCommandList(FALSE, &commands)))
{
lock.lock(); slot.state = SlotState::Free; return false;
lock.lock(); slot.state = SlotState::Free; idleCv_.notify_all(); return false;
}
lock.lock();
@@ -183,14 +183,20 @@ public:
{
auto lock = lockWithWindowMessages(mutex_);
if (!configured_ || !pipeline_)
return false;
{
// Before the first frame there is no surface to clear.
// Treat an already-empty preview as successfully cleared so
// playback reset can restart its timeline.
clearActiveInfo();
return true;
}
for (UploadSlot &slot : slots_)
{
if (slot.state == SlotState::Pending)
{
++delivery_.cancelled;
slot.commands.Reset();
slot.state = SlotState::Free;
slot.state = SlotState::Free; idleCv_.notify_all();
}
}
}
@@ -233,6 +239,7 @@ public:
clearActiveInfo();
resetPresentStats();
delivery_ = {};
idleCv_.notify_all();
}
bool active() const
@@ -291,6 +298,20 @@ public:
bool waitIdle(uint32_t timeoutMs)
{
const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeoutMs);
auto isIdle = [this] {
for (const auto &slot : slots_)
if (slot.state != SlotState::Free) return false;
return true;
};
{
auto lock = lockWithWindowMessages(mutex_);
const bool ownsWindow = hwnd_ && GetWindowThreadProcessId(static_cast<HWND>(hwnd_), nullptr) == GetCurrentThreadId();
// Playback workers do not own the HWND. Wake them directly when
// the GPU job retires, rather than polling a 1 ms Windows timer
// whose effective wait can consume most of a 60 Hz frame period.
if (!ownsWindow) return idleCv_.wait_until(lock, deadline, isIdle);
}
// The HWND owner must keep synchronous DXGI messages flowing.
for (;;)
{
std::unique_lock<std::mutex> lock(mutex_, std::try_to_lock);
@@ -338,7 +359,7 @@ private:
if (slot.state == SlotState::Pending && now - slot.queuedAt > kMaxQueueAge)
{
slot.commands.Reset();
slot.state = SlotState::Free;
slot.state = SlotState::Free; idleCv_.notify_all();
++skippedSubmits_;
++delivery_.replaced; // Retired queued frame: expired, not latest-frame replacement.
}
@@ -579,6 +600,7 @@ private:
if (slot.state == SlotState::Pending)
++delivery_.cancelled;
slots_ = std::move(replacement);
idleCv_.notify_all();
uploadWidth_ = width;
uploadHeight_ = height;
uploadPixelFormat_ = pixelFormat;
@@ -687,6 +709,7 @@ private:
}
slots_[selected].commands.Reset();
slots_[selected].state = SlotState::Free;
idleCv_.notify_all();
workerCv_.notify_all();
}
}
@@ -733,6 +756,7 @@ private:
std::array<UploadSlot, kUploadSlotCount> slots_{};
std::thread worker_;
std::condition_variable workerCv_;
std::condition_variable idleCv_;
bool workerStarted_ = false;
bool workerStopping_ = false;
int uploadWidth_ = 0;
@@ -901,30 +925,4 @@ extern "C"
return handle->renderer.waitIdle(timeout_ms) ? GVFG_PREVIEW_OK : GVFG_PREVIEW_ESTATE;
}
gvfg_preview_status_t gvfg_preview_shutdown(gvfg_preview_handle handle)
{
if (!handle)
return GVFG_PREVIEW_EINVAL;
handle->renderer.shutdown();
return GVFG_PREVIEW_OK;
}
const char *gvfg_preview_strerror(gvfg_preview_status_t status)
{
switch (status)
{
case GVFG_PREVIEW_OK:
return "OK";
case GVFG_PREVIEW_EINVAL:
return "Invalid argument";
case GVFG_PREVIEW_ESTATE:
return "Invalid state";
case GVFG_PREVIEW_ENOTSUP:
return "Unsupported frame format";
case GVFG_PREVIEW_ERENDER:
return "Render failed";
default:
return "Unknown";
}
}
}
@@ -0,0 +1,79 @@
#pragma once
/* Private static backend; not part of the customer API. */
#include "gvfg_preview.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef enum
{
GVFG_PREVIEW_OK = 0,
GVFG_PREVIEW_EINVAL = -1,
GVFG_PREVIEW_ESTATE = -2,
GVFG_PREVIEW_ENOTSUP = -3,
GVFG_PREVIEW_ERENDER = -4
} gvfg_preview_status_t;
typedef struct gvfg_preview_handle_t *gvfg_preview_handle;
gvfg_preview_status_t gvfg_preview_get_delivery_stats(
gvfg_preview_handle handle, gvfg_preview_delivery_stats_t *out_stats);
/* Call after stopping submissions. Wait at most timeout_ms for queued work.
* Returns ESTATE if work remains; does not discard or reset counters. */
gvfg_preview_status_t gvfg_preview_wait_idle(
gvfg_preview_handle handle, uint32_t timeout_ms);
gvfg_preview_status_t gvfg_preview_create(
gvfg_preview_handle *out_handle);
gvfg_preview_status_t gvfg_preview_destroy(
gvfg_preview_handle handle);
/*
* Attach a native window handle for preview output.
*
* On Windows, native_window_handle is an HWND.
*/
gvfg_preview_status_t gvfg_preview_attach_window(
gvfg_preview_handle handle,
void *native_window_handle);
/* Pre-create GPU resources before the first captured frame is held. */
gvfg_preview_status_t gvfg_preview_prepare(
gvfg_preview_handle handle,
int width,
int height,
int bit_depth);
/*
* Upload one frame to an internal GPU slot and queue it for preview.
*
* The frame memory remains owned by the caller and must remain valid until
* this function returns. GPU conversion and Present continue on the internal
* preview worker. Up to three pending frames are retained in FIFO order.
* Frames waiting longer than 50 ms are discarded; if the queue is full,
* the incoming frame is skipped. No SDK frame is retained for queueing.
* The age limit excludes time spent rendering or waiting inside Present.
*/
gvfg_preview_status_t gvfg_preview_render_frame(
gvfg_preview_handle handle,
const gvfg_preview_frame_t *frame);
/* Replace the last presented frame with a solid black frame. */
gvfg_preview_status_t gvfg_preview_clear(
gvfg_preview_handle handle);
gvfg_preview_status_t gvfg_preview_get_info(
gvfg_preview_handle handle,
gvfg_preview_info_t *out_info);
gvfg_preview_status_t gvfg_preview_get_stats(
gvfg_preview_handle handle,
gvfg_preview_stats_t *out_stats);
#ifdef __cplusplus
}
#endif