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
+2 -32
View File
@@ -1,8 +1,6 @@
project(gvfg_preview LANGUAGES CXX)
add_library(gvfg_preview SHARED
gvfg_preview.def
add_library(gvfg_preview STATIC
include/gvfg_preview.h
src/preview_output_internal.h
../../sdk/gvfg/src/gpu/d3d_conversion_pipeline.cpp
../../sdk/gvfg/src/gpu/d3d_conversion_pipeline.h
src/gvfg_preview.cpp
@@ -23,7 +21,6 @@ target_include_directories(gvfg_preview
)
target_compile_definitions(gvfg_preview PRIVATE
GVFG_PREVIEW_BUILD
GVFG_INTERNAL_DIAGNOSTICS=$<IF:$<CONFIG:Debug>,1,0>
)
@@ -38,30 +35,3 @@ target_link_libraries(gvfg_preview
if (MSVC)
target_compile_options(gvfg_preview PRIVATE /utf-8 /EHsc)
endif()
if (MSVC AND GVFG_MINGW_DLLTOOL)
add_custom_command(TARGET gvfg_preview POST_BUILD
COMMAND "${GVFG_MINGW_DLLTOOL}"
-d "${CMAKE_CURRENT_SOURCE_DIR}/gvfg_preview.def"
-D gvfg_preview.dll
-l libgvfg_preview.dll.a
WORKING_DIRECTORY "${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}"
COMMENT "Generating MinGW import library for gvfg_preview.dll"
VERBATIM
)
endif()
install(TARGETS gvfg_preview
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
install(FILES include/gvfg_preview.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
if (MSVC AND GVFG_MINGW_DLLTOOL)
install(FILES "${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/libgvfg_preview.dll.a"
DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
endif()
@@ -1,9 +1,9 @@
# Preview GPU conversion paths
`gvfg_preview.dll` remains independent of `gvfg.dll`. Both targets compile
one shared private conversion source, `d3d_conversion_pipeline.cpp`, into
their own DLLs. There is no cross-DLL bridge, new public texture API, or
runtime SDK dependency in the preview helper.
The static `gvfg_preview` backend is linked into `gvfg_playback.dll` and remains
independent of `gvfg.dll`. Both playback and capture compile the shared private
conversion source, `d3d_conversion_pipeline.cpp`, into their own DLLs. Preview
functions are internal; applications use `gvfg_playback_*`.
CPU-buffer conversion uses dedicated outputs. YUY2/Y210 to NV12 preserves
native luma and averages chroma vertically; Y210 codes are reduced to 8 bits.
@@ -1,15 +0,0 @@
LIBRARY gvfg_preview
EXPORTS
gvfg_preview_get_delivery_stats
gvfg_preview_wait_idle
gvfg_preview_create
gvfg_preview_destroy
gvfg_preview_attach_window
gvfg_preview_prepare
gvfg_preview_render_frame
gvfg_preview_clear
gvfg_preview_get_info
gvfg_preview_get_stats
gvfg_preview_shutdown
gvfg_preview_strerror
@@ -1,40 +1,8 @@
#pragma once
/*
* Optional GVFG preview helper API.
*
* Standalone preview helper. Applications provide the frame buffer, pixel
* format, and row stride explicitly; gvfg_preview.dll does not depend on
* gvfg.dll.
*/
/* Public image and presentation types used by gvfg_playback.h. */
#include <stdint.h>
#ifdef _WIN32
#ifdef GVFG_PREVIEW_BUILD
#define GVFG_PREVIEW_API __declspec(dllexport)
#else
#define GVFG_PREVIEW_API __declspec(dllimport)
#endif
#else
#define GVFG_PREVIEW_API
#endif
#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;
typedef enum
{
GVFG_PREVIEW_PIXFMT_YUY2 = 1,
@@ -105,69 +73,3 @@ typedef struct
uint64_t slots_busy;
uint64_t last_busy_id;
} gvfg_preview_delivery_stats_t;
GVFG_PREVIEW_API 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_API gvfg_preview_status_t gvfg_preview_wait_idle(
gvfg_preview_handle handle, uint32_t timeout_ms);
GVFG_PREVIEW_API gvfg_preview_status_t gvfg_preview_create(
gvfg_preview_handle *out_handle);
GVFG_PREVIEW_API 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_API 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_API 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_API 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_API gvfg_preview_status_t gvfg_preview_clear(
gvfg_preview_handle handle);
GVFG_PREVIEW_API gvfg_preview_status_t gvfg_preview_get_info(
gvfg_preview_handle handle,
gvfg_preview_info_t *out_info);
GVFG_PREVIEW_API gvfg_preview_status_t gvfg_preview_get_stats(
gvfg_preview_handle handle,
gvfg_preview_stats_t *out_stats);
GVFG_PREVIEW_API gvfg_preview_status_t gvfg_preview_shutdown(
gvfg_preview_handle handle);
GVFG_PREVIEW_API const char *gvfg_preview_strerror(
gvfg_preview_status_t status);
#ifdef __cplusplus
}
#endif
@@ -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