Add HDMI audio input queries and split OSD into a helper in GVFG SDK 3.2
- Expose HDMI audio input information through the capture API and refresh the bundled Gigabyte library. - Move OSD rendering into gvfg_osd.dll, connect channel OSD through the capture frame processor, and remove the experimental in-place zero-copy OSD path. - Update playback integration, the customer API documentation, and the internal build guide. Validation: git diff --cached --check passed. The MSVC Release SDK build passed. Capture hardware and runtime behavior were not tested.
This commit is contained in:
@@ -1,28 +1,27 @@
|
||||
project(gvfg_osd_core LANGUAGES CXX)
|
||||
|
||||
add_library(gvfg_osd_core STATIC
|
||||
include/gvfg_osd.h
|
||||
src/gvfg_osd.cpp
|
||||
)
|
||||
|
||||
set_target_properties(gvfg_osd_core PROPERTIES
|
||||
AUTOMOC OFF
|
||||
AUTOUIC OFF
|
||||
AUTORCC OFF
|
||||
)
|
||||
|
||||
target_include_directories(gvfg_osd_core
|
||||
PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||
)
|
||||
|
||||
target_link_libraries(gvfg_osd_core PUBLIC
|
||||
d2d1
|
||||
dwrite
|
||||
windowscodecs
|
||||
ole32
|
||||
)
|
||||
|
||||
if (MSVC)
|
||||
target_compile_options(gvfg_osd_core PRIVATE /utf-8 /EHsc)
|
||||
add_library(gvfg_osd SHARED gvfg_osd.def include/gvfg_osd.h
|
||||
src/gvfg_osd_api.cpp src/gvfg_osd.cpp src/gvfg_osd_core.h
|
||||
src/gvfg_gpu_osd.cpp src/gvfg_gpu_osd.h)
|
||||
set_target_properties(gvfg_osd PROPERTIES AUTOMOC OFF AUTOUIC OFF AUTORCC OFF)
|
||||
target_include_directories(gvfg_osd PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/../../sdk/gvfg/include
|
||||
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../sdk/gvfg/src)
|
||||
target_compile_definitions(gvfg_osd PRIVATE GVFG_OSD_BUILD)
|
||||
target_link_libraries(gvfg_osd PRIVATE gvfg d2d1 dwrite windowscodecs ole32 d3d11 D3DCompiler)
|
||||
if(MSVC)
|
||||
target_compile_options(gvfg_osd PRIVATE /utf-8 /EHsc)
|
||||
endif()
|
||||
if(MSVC AND GVFG_MINGW_DLLTOOL)
|
||||
add_custom_command(TARGET gvfg_osd POST_BUILD
|
||||
COMMAND "${GVFG_MINGW_DLLTOOL}"
|
||||
-d "${CMAKE_CURRENT_SOURCE_DIR}/gvfg_osd.def"
|
||||
-D gvfg_osd.dll -l libgvfg_osd.dll.a
|
||||
WORKING_DIRECTORY "${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}"
|
||||
COMMENT "Generating MinGW import library for gvfg_osd.dll"
|
||||
VERBATIM
|
||||
)
|
||||
install(FILES "${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/libgvfg_osd.dll.a"
|
||||
DESTINATION ${CMAKE_INSTALL_LIBDIR})
|
||||
endif()
|
||||
install(TARGETS gvfg_osd
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
|
||||
install(FILES include/gvfg_osd.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
|
||||
|
||||
@@ -77,38 +77,3 @@ to application receipt is acquisition plus OSD/delivery cost. Waiting for the
|
||||
next frame is excluded. The public frame timestamp and old timed-read ABI are
|
||||
unchanged; this uses a separate internal result type and export.
|
||||
Do not ship the internal debug header in customer packages.
|
||||
|
||||
### Unsafe local zero-copy experiment (default OFF)
|
||||
|
||||
Set `GVFG_EXPERIMENTAL_ZERO_COPY_OSD_INPLACE=ON` in the standalone SDK's
|
||||
CMake configuration, rebuild `gvfg.dll` and its import library, then rebuild
|
||||
the srcroot viewer against the matching SDK. Enable zero-copy and apply OSD.
|
||||
This is an SDK build option, not the viewer console option.
|
||||
|
||||
The experimental backend supplies the original mutable vendor pointer, without
|
||||
casting away const on the public API. Before processing, `VirtualQuery` checks
|
||||
the entire destination ROI span for committed `PAGE_READWRITE` pages; readonly,
|
||||
guard, copy-on-write, and failed queries fall back to independent output and a
|
||||
full-frame copy. Permissions are checked again immediately before ROI writes;
|
||||
failure at that point returns `GVFG_EIO` without writing. No `VirtualProtect`
|
||||
or vendor release contract change is used. Successful in-place processing
|
||||
retains the backend hold until normal application frame release.
|
||||
|
||||
WARNING: these checks do not prevent concurrent DMA, buffer reuse, or a mapping
|
||||
change after the check. Corruption and access violations remain possible; C++
|
||||
exception handling is not an access-violation safety guarantee. Do not ship this
|
||||
configuration. A successful capture does not establish driver ownership safety.
|
||||
|
||||
The viewer warns at startup and labels each 300-frame diagnostic batch as actual
|
||||
driver-buffer writes, fallback copies, or no in-place processing. Flags use the
|
||||
separate `gvfg_debug_get_channel_osd_experiment_flags` export; existing timing
|
||||
struct layouts stay unchanged. Revert by setting the option OFF and rebuilding
|
||||
the SDK. The client-facing frame pointer remains read-only in all modes.
|
||||
|
||||
Fallback batches also report the final frame's first rejected region: a readable
|
||||
reason, native Protect/State/Type, VirtualQuery return byte count, and Win32 error
|
||||
(captured immediately only when VirtualQuery returns zero). Other frames may
|
||||
have different reasons. This uses the separate internal
|
||||
`gvfg_debug_get_channel_osd_page_check` export, queried while the frame is held;
|
||||
rebuild both SDK and viewer together. No per-frame logging or permission changes
|
||||
are introduced. Zero Win32 error does not imply a writable page.
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
LIBRARY gvfg_osd
|
||||
|
||||
EXPORTS
|
||||
gvfg_get_osd_fonts
|
||||
gvfg_measure_osd_text
|
||||
gvfg_render_osd_frame_bgra
|
||||
gvfg_set_channel_osd
|
||||
gvfg_set_channel_osd_visible
|
||||
gvfg_clear_channel_osd
|
||||
@@ -1,104 +1,113 @@
|
||||
#ifndef GVFG_OSD_H
|
||||
#define GVFG_OSD_H
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include "gvfg_capture.h"
|
||||
|
||||
#if defined(_WIN32)
|
||||
# if defined(GVFG_OSD_BUILD)
|
||||
# define GVFG_OSD_API __declspec(dllexport)
|
||||
# else
|
||||
# define GVFG_OSD_API __declspec(dllimport)
|
||||
# endif
|
||||
#else
|
||||
# define GVFG_OSD_API
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Strings are copied synchronously by gvfg_set_channel_osd(). */
|
||||
typedef struct gvfg_channel_osd_config_t
|
||||
{
|
||||
const char *text; /* NUL-terminated printable ASCII, 1..32 characters. */
|
||||
const char *font_family; /* NUL-terminated UTF-8 installed font family name. */
|
||||
uint32_t font_size_px; /* Non-zero em size; 96-DPI rendering, not ink height. */
|
||||
uint32_t color_rgb; /* 0x00RRGGBB. */
|
||||
int32_t x; /* Text-box left in frame pixels; clamped to fit. */
|
||||
int32_t y; /* Text-box top in frame pixels; clamped to fit. */
|
||||
} gvfg_channel_osd_config_t;
|
||||
|
||||
typedef struct gvfg_osd_font_list_t
|
||||
{
|
||||
uint32_t count;
|
||||
const char *const *names; /* SDK-owned UTF-8 family names; do not modify/free. */
|
||||
} gvfg_osd_font_list_t;
|
||||
|
||||
typedef struct gvfg_osd_bitmap_t
|
||||
{
|
||||
const uint8_t *data; /* SDK-owned; do not modify/free. See the render function's lifetime. */
|
||||
uint64_t data_size; /* stride * height bytes. */
|
||||
uint32_t width;
|
||||
uint32_t height;
|
||||
uint32_t stride; /* Byte distance between rows; width * 4. */
|
||||
} gvfg_osd_bitmap_t;
|
||||
|
||||
#define GVFG_OSD_SLOT_COUNT 2u
|
||||
#define GVFG_OSD_MAX_TEXT_CHARACTERS 32u
|
||||
|
||||
typedef void *gvfg_osd_handle;
|
||||
/* Refresh installed fonts and return an SDK-owned snapshot. No device needed.
|
||||
* Pointers expire at the next call (including a failed call), or DLL unload.
|
||||
* Copy names before then. Serialize calls and use of this borrowed list.
|
||||
* Returns an empty list on failure; no callback or caller-side free is needed.
|
||||
*/
|
||||
GVFG_OSD_API gvfg_status_t gvfg_get_osd_fonts(
|
||||
GVFG_PARAM_OUT gvfg_osd_font_list_t *out_list);
|
||||
|
||||
typedef enum gvfg_osd_status_t
|
||||
{
|
||||
GVFG_OSD_OK = 0,
|
||||
GVFG_OSD_EINVAL = -1,
|
||||
GVFG_OSD_ENOMEM = -2,
|
||||
GVFG_OSD_ERENDER = -3
|
||||
} gvfg_osd_status_t;
|
||||
/* Measure the same conservative text box used for rendering, including
|
||||
* overhang and a one-pixel guard per edge. Ignores x/y/color; no device needed.
|
||||
* Outputs are zeroed on failure. Missing font/invalid input: GVFG_EINVAL.
|
||||
*/
|
||||
GVFG_OSD_API gvfg_status_t gvfg_measure_osd_text(
|
||||
GVFG_PARAM_IN const gvfg_channel_osd_config_t *config,
|
||||
GVFG_PARAM_OUT uint32_t *out_width,
|
||||
GVFG_PARAM_OUT uint32_t *out_height);
|
||||
|
||||
typedef enum gvfg_osd_pixel_format_t
|
||||
{
|
||||
GVFG_OSD_PIXFMT_YUY2 = 1,
|
||||
GVFG_OSD_PIXFMT_Y210 = 2
|
||||
} gvfg_osd_pixel_format_t;
|
||||
/* Prepare one transparent video-sized overlay for FPGA transfer. No capture
|
||||
* handle needed; pass the target video width/height in pixels. Output is
|
||||
* top-down BGRA8888, premultiplied alpha, stride = width * 4.
|
||||
* NULL slots are omitted (hidden/unused); both NULL produce a blank frame.
|
||||
* x/y are frame coordinates, clamped to fit; oversized text is skipped.
|
||||
* Slot 1 is composited over slot 0. No video pixels are included.
|
||||
* Data is valid until the next call to this function on the same thread
|
||||
* (including failure), thread exit or DLL unload. Copy for async transfer.
|
||||
* Invalid inputs/dimensions (including byte size above UINT32_MAX) return
|
||||
* GVFG_EINVAL; allocation/render failures return GVFG_EIO. On failure the
|
||||
* output is empty. Does not affect GPU capture OSD.
|
||||
*/
|
||||
GVFG_OSD_API gvfg_status_t gvfg_render_osd_frame_bgra(
|
||||
GVFG_PARAM_IN uint32_t video_width,
|
||||
GVFG_PARAM_IN uint32_t video_height,
|
||||
GVFG_PARAM_IN const gvfg_channel_osd_config_t *slot0,
|
||||
GVFG_PARAM_IN const gvfg_channel_osd_config_t *slot1,
|
||||
GVFG_PARAM_OUT gvfg_osd_bitmap_t *out_bitmap);
|
||||
|
||||
/*
|
||||
* Generates an off-line OSD surface using the requested installed system font. Text is
|
||||
* restricted to printable ASCII and at most GVFG_OSD_MAX_TEXT_CHARACTERS
|
||||
* characters on one line without wrapping. color_rgb is 0xRRGGBB.
|
||||
* x/y identify the top-left of the conservative measured text box (including
|
||||
* glyph overhang and a one-pixel guard). They are clamped to keep that box
|
||||
* inside the frame. Rendering returns EINVAL if the box cannot fit the frame.
|
||||
* Output YUV uses BT.709 limited range. The mask is one coverage byte per
|
||||
* pixel: 0 is transparent and 255 is fully covered.
|
||||
*/
|
||||
typedef struct gvfg_osd_config_t
|
||||
{
|
||||
uint32_t struct_size;
|
||||
uint32_t width;
|
||||
uint32_t height;
|
||||
uint32_t pixel_format;
|
||||
const char *text;
|
||||
const char *font_family; /* UTF-8, NUL-terminated. */
|
||||
uint32_t text_length;
|
||||
int32_t x;
|
||||
int32_t y;
|
||||
uint32_t font_size_px;
|
||||
uint32_t color_rgb;
|
||||
} gvfg_osd_config_t;
|
||||
/* Set and show slot 0 or 1 on an opened channel. Strings are copied before
|
||||
* return; slot 1 is blended over slot 0. Applies to subsequent YUY2/Y210
|
||||
* frames using BT.709 limited range. X/Y are clamped to fit the text box.
|
||||
* An oversized slot is skipped until settings/frame size allow it to fit;
|
||||
* capture and the other slot continue. No wrapping/shrinking/truncation.
|
||||
*/
|
||||
GVFG_OSD_API gvfg_status_t gvfg_set_channel_osd(
|
||||
GVFG_PARAM_IN gvfg_handle handle,
|
||||
GVFG_PARAM_IN int channel_index,
|
||||
GVFG_PARAM_IN uint32_t osd_index,
|
||||
GVFG_PARAM_IN const gvfg_channel_osd_config_t *config);
|
||||
|
||||
/* All buffers are caller-owned and are used only for the duration of render. */
|
||||
typedef struct gvfg_osd_output_t
|
||||
{
|
||||
uint32_t struct_size;
|
||||
void *yuv_data;
|
||||
uint64_t yuv_size;
|
||||
uint32_t yuv_stride;
|
||||
void *mask_data;
|
||||
uint64_t mask_size;
|
||||
uint32_t mask_stride;
|
||||
} gvfg_osd_output_t;
|
||||
/* visible must be 0 or 1. Retains settings and cached glyph surfaces.
|
||||
* Showing an unconfigured/cleared slot returns GVFG_ESTATE.
|
||||
*/
|
||||
GVFG_OSD_API gvfg_status_t gvfg_set_channel_osd_visible(
|
||||
GVFG_PARAM_IN gvfg_handle handle,
|
||||
GVFG_PARAM_IN int channel_index,
|
||||
GVFG_PARAM_IN uint32_t osd_index,
|
||||
GVFG_PARAM_IN int visible);
|
||||
|
||||
gvfg_osd_status_t gvfg_osd_create(
|
||||
gvfg_osd_handle *out_handle);
|
||||
|
||||
void gvfg_osd_destroy(
|
||||
gvfg_osd_handle handle);
|
||||
|
||||
/* Conservative whole-text bounds in pixels, not pixel-tight raster bounds. */
|
||||
gvfg_osd_status_t gvfg_osd_measure(
|
||||
gvfg_osd_handle handle,
|
||||
const char *text,
|
||||
uint32_t text_length,
|
||||
uint32_t font_size_px,
|
||||
const char *font_family,
|
||||
uint32_t *out_width,
|
||||
uint32_t *out_height);
|
||||
|
||||
/* SDK-owned snapshot, invalidated by the next call. Serialize call/read access. */
|
||||
gvfg_osd_status_t gvfg_osd_get_fonts(uint32_t *out_count, const char *const **out_names);
|
||||
|
||||
gvfg_osd_status_t gvfg_osd_render(
|
||||
gvfg_osd_handle handle,
|
||||
const gvfg_osd_config_t *config,
|
||||
const gvfg_osd_output_t *output);
|
||||
|
||||
/* Caller-owned, top-down BGRA8888 with premultiplied alpha. Ignores pixel_format.
|
||||
* Unlike YUV output, width may be odd. No RGB/YUV conversion is performed.
|
||||
*/
|
||||
gvfg_osd_status_t gvfg_osd_render_bgra(
|
||||
gvfg_osd_handle handle,
|
||||
const gvfg_osd_config_t *config,
|
||||
void *data, uint64_t data_size, uint32_t stride);
|
||||
|
||||
const char *gvfg_osd_strerror(
|
||||
gvfg_osd_status_t status);
|
||||
/* Disable one slot and discard its settings and cached glyph surfaces. */
|
||||
GVFG_OSD_API gvfg_status_t gvfg_clear_channel_osd(
|
||||
GVFG_PARAM_IN gvfg_handle handle,
|
||||
GVFG_PARAM_IN int channel_index,
|
||||
GVFG_PARAM_IN uint32_t osd_index);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,656 @@
|
||||
#include "gvfg_gpu_osd.h"
|
||||
|
||||
#include "gvfg_osd_core.h"
|
||||
|
||||
#include <d3d11_4.h>
|
||||
#include <d3dcompiler.h>
|
||||
#include <wrl/client.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
#include <mutex>
|
||||
#include <utility>
|
||||
|
||||
using Microsoft::WRL::ComPtr;
|
||||
|
||||
namespace gvfg::internal
|
||||
{
|
||||
namespace
|
||||
{
|
||||
constexpr char kOsdBlendShader[] = R"(
|
||||
Texture2D<uint4> sourceTexture : register(t0);
|
||||
Texture2D<uint4> osdTexture : register(t1);
|
||||
Texture2D<uint2> maskTexture : register(t2);
|
||||
Texture2D<uint4> osdTexture1 : register(t3);
|
||||
Texture2D<uint2> maskTexture1 : register(t4);
|
||||
RWTexture2D<uint4> outputTexture : register(u0);
|
||||
|
||||
cbuffer BlendParameters : register(b0)
|
||||
{
|
||||
uint outputValueMask;
|
||||
uint visibleSlots;
|
||||
uint2 padding;
|
||||
};
|
||||
|
||||
uint blendComponent(uint sourceValue, uint osdValue, uint alpha)
|
||||
{
|
||||
return ((sourceValue * (255u - alpha) + osdValue * alpha + 127u) / 255u) &
|
||||
outputValueMask;
|
||||
}
|
||||
|
||||
uint4 blendLayer(uint4 sourceValue, uint4 osdValue, uint2 maskValue)
|
||||
{
|
||||
uint pairAlpha = max(maskValue.x, maskValue.y);
|
||||
uint4 result;
|
||||
result.x = blendComponent(sourceValue.x, osdValue.x, maskValue.x);
|
||||
result.y = blendComponent(sourceValue.y, osdValue.y, pairAlpha);
|
||||
result.z = blendComponent(sourceValue.z, osdValue.z, maskValue.y);
|
||||
result.w = blendComponent(sourceValue.w, osdValue.w, pairAlpha);
|
||||
return result;
|
||||
}
|
||||
|
||||
[numthreads(16, 16, 1)]
|
||||
void main(uint3 id : SV_DispatchThreadID)
|
||||
{
|
||||
uint width = 0;
|
||||
uint height = 0;
|
||||
outputTexture.GetDimensions(width, height);
|
||||
if (id.x >= width || id.y >= height)
|
||||
return;
|
||||
const uint3 location = uint3(id.xy, 0);
|
||||
uint4 result = sourceTexture.Load(location);
|
||||
if ((visibleSlots & 1u) != 0u)
|
||||
result = blendLayer(result, osdTexture.Load(location), maskTexture.Load(location));
|
||||
if ((visibleSlots & 2u) != 0u)
|
||||
result = blendLayer(result, osdTexture1.Load(location), maskTexture1.Load(location));
|
||||
outputTexture[id.xy] = result;
|
||||
}
|
||||
)";
|
||||
|
||||
bool copyRowsToMapped(const uint8_t *source, int sourceStride,
|
||||
uint32_t rowBytes, uint32_t height,
|
||||
const D3D11_MAPPED_SUBRESOURCE &mapped)
|
||||
{
|
||||
if (!source || sourceStride < 0 || static_cast<uint32_t>(sourceStride) < rowBytes ||
|
||||
mapped.RowPitch < rowBytes)
|
||||
return false;
|
||||
if (static_cast<uint32_t>(sourceStride) == rowBytes && mapped.RowPitch == rowBytes)
|
||||
{
|
||||
std::memcpy(mapped.pData, source, static_cast<size_t>(rowBytes) * height);
|
||||
return true;
|
||||
}
|
||||
for (uint32_t y = 0; y < height; ++y)
|
||||
{
|
||||
std::memcpy(static_cast<uint8_t *>(mapped.pData) + static_cast<size_t>(y) * mapped.RowPitch,
|
||||
source + static_cast<size_t>(y) * static_cast<size_t>(sourceStride),
|
||||
rowBytes);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
struct OsdRegion
|
||||
{
|
||||
uint32_t left = 0;
|
||||
uint32_t top = 0;
|
||||
uint32_t width = 0;
|
||||
uint32_t height = 0;
|
||||
|
||||
bool empty() const { return width == 0 || height == 0; }
|
||||
};
|
||||
|
||||
OsdRegion findCoveredRegion(const std::vector<uint8_t> &mask,
|
||||
uint32_t frameWidth, uint32_t frameHeight)
|
||||
{
|
||||
uint32_t left = frameWidth;
|
||||
uint32_t top = frameHeight;
|
||||
uint32_t right = 0;
|
||||
uint32_t bottom = 0;
|
||||
for (uint32_t row = 0; row < frameHeight; ++row)
|
||||
{
|
||||
const uint8_t *maskRow = mask.data() + static_cast<size_t>(row) * frameWidth;
|
||||
for (uint32_t column = 0; column < frameWidth; column += 2u)
|
||||
{
|
||||
// Keep both luma samples and their shared U/V inside the region.
|
||||
if (maskRow[column] == 0 && maskRow[column + 1u] == 0)
|
||||
continue;
|
||||
left = (std::min)(left, column);
|
||||
right = (std::max)(right, column + 2u);
|
||||
top = (std::min)(top, row);
|
||||
bottom = row + 1u;
|
||||
}
|
||||
}
|
||||
return right > left ? OsdRegion{left, top, right - left, bottom - top}
|
||||
: OsdRegion{};
|
||||
}
|
||||
|
||||
void copyFrameRows(const gvfg_frame_t &source, uint8_t *destination,
|
||||
uint32_t rowBytes)
|
||||
{
|
||||
const auto *sourceData = static_cast<const uint8_t *>(source.data);
|
||||
if (static_cast<uint32_t>(source.row_stride_bytes) == rowBytes)
|
||||
{
|
||||
std::memcpy(destination, sourceData,
|
||||
static_cast<size_t>(rowBytes) * static_cast<size_t>(source.height));
|
||||
return;
|
||||
}
|
||||
for (int row = 0; row < source.height; ++row)
|
||||
{
|
||||
std::memcpy(destination + static_cast<size_t>(row) * rowBytes,
|
||||
sourceData + static_cast<size_t>(row) * source.row_stride_bytes,
|
||||
rowBytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct GpuOsdProcessor::Impl
|
||||
{
|
||||
struct Slot
|
||||
{
|
||||
bool configured = false;
|
||||
bool visible = false;
|
||||
bool dirty = true;
|
||||
std::string text;
|
||||
std::string fontFamily;
|
||||
int32_t x = 0, y = 0;
|
||||
uint32_t fontSize = 0, colorRgb = 0xffffffu;
|
||||
uint32_t width = 0, height = 0;
|
||||
int format = GVFG_PIXFMT_UNKNOWN;
|
||||
OsdRegion region;
|
||||
std::vector<uint8_t> yuv, mask;
|
||||
ComPtr<ID3D11Texture2D> texture, maskTexture;
|
||||
ComPtr<ID3D11ShaderResourceView> srv, maskSrv;
|
||||
};
|
||||
|
||||
gvfg_status_t configure(uint32_t index, const gvfg_channel_osd_config_t &value)
|
||||
{
|
||||
if (index >= slots.size())
|
||||
return GVFG_EINVAL;
|
||||
uint32_t width = 0, height = 0;
|
||||
const auto status = gvfg_measure_osd_text(&value, &width, &height);
|
||||
if (status != GVFG_OK)
|
||||
return status;
|
||||
// Allocate copies before changing state so failures retain the old slot.
|
||||
std::string text(value.text), family(value.font_family);
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
auto &slot = slots[index];
|
||||
const bool changed = !slot.configured || slot.text != text || slot.fontFamily != family ||
|
||||
slot.x != value.x || slot.y != value.y || slot.fontSize != value.font_size_px ||
|
||||
slot.colorRgb != (value.color_rgb & 0x00ffffffu);
|
||||
slot.text.swap(text);
|
||||
slot.fontFamily.swap(family);
|
||||
slot.x = value.x;
|
||||
slot.y = value.y;
|
||||
slot.fontSize = value.font_size_px;
|
||||
slot.colorRgb = value.color_rgb & 0x00ffffffu;
|
||||
slot.configured = slot.visible = true;
|
||||
slot.dirty = slot.dirty || changed;
|
||||
surfaceDirty = surfaceDirty || changed;
|
||||
return GVFG_OK;
|
||||
}
|
||||
|
||||
gvfg_status_t clear(uint32_t index)
|
||||
{
|
||||
if (index >= slots.size())
|
||||
return GVFG_EINVAL;
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
slots[index] = Slot{};
|
||||
surfaceDirty = true;
|
||||
return GVFG_OK;
|
||||
}
|
||||
|
||||
gvfg_status_t setVisible(uint32_t index, int visible)
|
||||
{
|
||||
if (index >= slots.size() || (visible != 0 && visible != 1))
|
||||
return GVFG_EINVAL;
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
if (!slots[index].configured && visible)
|
||||
return GVFG_ESTATE;
|
||||
slots[index].visible = visible != 0;
|
||||
// Keep rasterized surfaces and GPU textures intact.
|
||||
return GVFG_OK;
|
||||
}
|
||||
|
||||
bool ensureDevice()
|
||||
{
|
||||
if (device && context && shader && blendParameters)
|
||||
return true;
|
||||
|
||||
UINT flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
|
||||
#ifdef _DEBUG
|
||||
flags |= D3D11_CREATE_DEVICE_DEBUG;
|
||||
#endif
|
||||
const D3D_FEATURE_LEVEL levels[] = {
|
||||
D3D_FEATURE_LEVEL_11_1,
|
||||
D3D_FEATURE_LEVEL_11_0,
|
||||
};
|
||||
D3D_FEATURE_LEVEL actual{};
|
||||
HRESULT hr = D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr,
|
||||
flags, levels, static_cast<UINT>(std::size(levels)),
|
||||
D3D11_SDK_VERSION, &device, &actual, &context);
|
||||
#ifdef _DEBUG
|
||||
if (FAILED(hr))
|
||||
{
|
||||
flags &= ~D3D11_CREATE_DEVICE_DEBUG;
|
||||
hr = D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr,
|
||||
flags, levels, static_cast<UINT>(std::size(levels)),
|
||||
D3D11_SDK_VERSION, &device, &actual, &context);
|
||||
}
|
||||
#endif
|
||||
if (FAILED(hr) || !device || !context)
|
||||
return false;
|
||||
|
||||
ComPtr<ID3DBlob> bytecode;
|
||||
ComPtr<ID3DBlob> errors;
|
||||
hr = D3DCompile(kOsdBlendShader, sizeof(kOsdBlendShader) - 1,
|
||||
"gvfg_osd_blend", nullptr, nullptr, "main", "cs_5_0",
|
||||
D3DCOMPILE_ENABLE_STRICTNESS, 0, &bytecode, &errors);
|
||||
if (FAILED(hr) || !bytecode)
|
||||
return false;
|
||||
if (FAILED(device->CreateComputeShader(bytecode->GetBufferPointer(),
|
||||
bytecode->GetBufferSize(),
|
||||
nullptr, &shader)))
|
||||
return false;
|
||||
|
||||
D3D11_BUFFER_DESC parameterDesc{};
|
||||
parameterDesc.ByteWidth = 16;
|
||||
parameterDesc.Usage = D3D11_USAGE_DEFAULT;
|
||||
parameterDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
return SUCCEEDED(device->CreateBuffer(¶meterDesc, nullptr, &blendParameters));
|
||||
}
|
||||
|
||||
bool ensureTexture(ComPtr<ID3D11Texture2D> &texture,
|
||||
uint32_t textureWidth, uint32_t textureHeight,
|
||||
DXGI_FORMAT format, D3D11_USAGE usage,
|
||||
UINT bindFlags, UINT cpuAccess)
|
||||
{
|
||||
if (texture)
|
||||
{
|
||||
D3D11_TEXTURE2D_DESC current{};
|
||||
texture->GetDesc(¤t);
|
||||
if (current.Width == textureWidth && current.Height == textureHeight &&
|
||||
current.Format == format && current.Usage == usage &&
|
||||
current.BindFlags == bindFlags && current.CPUAccessFlags == cpuAccess)
|
||||
return true;
|
||||
texture.Reset();
|
||||
}
|
||||
D3D11_TEXTURE2D_DESC desc{};
|
||||
desc.Width = textureWidth;
|
||||
desc.Height = textureHeight;
|
||||
desc.MipLevels = 1;
|
||||
desc.ArraySize = 1;
|
||||
desc.Format = format;
|
||||
desc.SampleDesc.Count = 1;
|
||||
desc.Usage = usage;
|
||||
desc.BindFlags = bindFlags;
|
||||
desc.CPUAccessFlags = cpuAccess;
|
||||
return SUCCEEDED(device->CreateTexture2D(&desc, nullptr, &texture));
|
||||
}
|
||||
|
||||
bool ensureResources(uint32_t regionWidth, uint32_t regionHeight, int pixelFormat)
|
||||
{
|
||||
const DXGI_FORMAT packedFormat = pixelFormat == GVFG_PIXFMT_YUY2
|
||||
? DXGI_FORMAT_R8G8B8A8_UINT : DXGI_FORMAT_R16G16B16A16_UINT;
|
||||
const bool changed = resourceWidth != regionWidth || resourceHeight != regionHeight ||
|
||||
resourceFormat != pixelFormat;
|
||||
if (changed)
|
||||
{
|
||||
sourceSrv.Reset();
|
||||
outputUav.Reset();
|
||||
for (auto &slot : slots) { slot.srv.Reset(); slot.maskSrv.Reset(); }
|
||||
resourceWidth = resourceHeight = 0;
|
||||
resourceFormat = GVFG_PIXFMT_UNKNOWN;
|
||||
}
|
||||
const uint32_t pairWidth = regionWidth / 2u;
|
||||
if (!ensureTexture(sourceTexture, pairWidth, regionHeight, packedFormat,
|
||||
D3D11_USAGE_DYNAMIC, D3D11_BIND_SHADER_RESOURCE, D3D11_CPU_ACCESS_WRITE) ||
|
||||
!ensureTexture(outputTexture, pairWidth, regionHeight, packedFormat,
|
||||
D3D11_USAGE_DEFAULT, D3D11_BIND_UNORDERED_ACCESS, 0) ||
|
||||
!ensureTexture(readbackTexture, pairWidth, regionHeight, packedFormat,
|
||||
D3D11_USAGE_STAGING, 0, D3D11_CPU_ACCESS_READ))
|
||||
return false;
|
||||
for (auto &slot : slots)
|
||||
{
|
||||
if (!ensureTexture(slot.texture, pairWidth, regionHeight, packedFormat,
|
||||
D3D11_USAGE_DYNAMIC, D3D11_BIND_SHADER_RESOURCE, D3D11_CPU_ACCESS_WRITE) ||
|
||||
!ensureTexture(slot.maskTexture, pairWidth, regionHeight, DXGI_FORMAT_R8G8_UINT,
|
||||
D3D11_USAGE_DYNAMIC, D3D11_BIND_SHADER_RESOURCE, D3D11_CPU_ACCESS_WRITE))
|
||||
return false;
|
||||
if ((!slot.srv && FAILED(device->CreateShaderResourceView(slot.texture.Get(), nullptr, &slot.srv))) ||
|
||||
(!slot.maskSrv && FAILED(device->CreateShaderResourceView(slot.maskTexture.Get(), nullptr, &slot.maskSrv))))
|
||||
return false;
|
||||
}
|
||||
if ((!sourceSrv && FAILED(device->CreateShaderResourceView(sourceTexture.Get(), nullptr, &sourceSrv))) ||
|
||||
(!outputUav && FAILED(device->CreateUnorderedAccessView(outputTexture.Get(), nullptr, &outputUav))))
|
||||
return false;
|
||||
resourceWidth = regionWidth;
|
||||
resourceHeight = regionHeight;
|
||||
resourceFormat = pixelFormat;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool rebuildSlot(Slot &slot, uint32_t width, uint32_t height, int format)
|
||||
{
|
||||
if (!slot.dirty && slot.width == width && slot.height == height && slot.format == format)
|
||||
return true;
|
||||
const uint32_t stride = width * (format == GVFG_PIXFMT_YUY2 ? 2u : 4u);
|
||||
slot.yuv.assign(static_cast<size_t>(stride) * height, 0);
|
||||
slot.mask.assign(static_cast<size_t>(width) * height, 0);
|
||||
slot.region = {};
|
||||
if (slot.configured)
|
||||
{
|
||||
gvfg_osd_handle raw = nullptr;
|
||||
if (gvfg_osd_create(&raw) != GVFG_OSD_OK)
|
||||
return false;
|
||||
std::unique_ptr<void, decltype(&gvfg_osd_destroy)> helper(raw, &gvfg_osd_destroy);
|
||||
uint32_t textWidth = 0, textHeight = 0;
|
||||
const auto measured = gvfg_osd_measure(raw, slot.text.c_str(),
|
||||
static_cast<uint32_t>(slot.text.size()), slot.fontSize, slot.fontFamily.c_str(),
|
||||
&textWidth, &textHeight);
|
||||
if (measured != GVFG_OSD_OK)
|
||||
return false;
|
||||
// Oversized text skips only this slot, preserving capture and the other slot.
|
||||
if (textWidth <= width && textHeight <= height)
|
||||
{
|
||||
gvfg_osd_config_t config{};
|
||||
config.struct_size = sizeof(config);
|
||||
config.width = width;
|
||||
config.height = height;
|
||||
config.pixel_format = format == GVFG_PIXFMT_YUY2 ? GVFG_OSD_PIXFMT_YUY2 : GVFG_OSD_PIXFMT_Y210;
|
||||
config.text = slot.text.c_str();
|
||||
config.text_length = static_cast<uint32_t>(slot.text.size());
|
||||
config.font_family = slot.fontFamily.c_str();
|
||||
config.font_size_px = slot.fontSize;
|
||||
config.color_rgb = slot.colorRgb;
|
||||
config.x = slot.x;
|
||||
config.y = slot.y;
|
||||
gvfg_osd_output_t output{};
|
||||
output.struct_size = sizeof(output);
|
||||
output.yuv_data = slot.yuv.data();
|
||||
output.yuv_size = slot.yuv.size();
|
||||
output.yuv_stride = stride;
|
||||
output.mask_data = slot.mask.data();
|
||||
output.mask_size = slot.mask.size();
|
||||
output.mask_stride = width;
|
||||
if (gvfg_osd_render(raw, &config, &output) != GVFG_OSD_OK)
|
||||
return false;
|
||||
slot.region = findCoveredRegion(slot.mask, width, height);
|
||||
}
|
||||
}
|
||||
slot.width = width;
|
||||
slot.height = height;
|
||||
slot.format = format;
|
||||
slot.dirty = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool rebuildSurface(uint32_t width, uint32_t height, int format)
|
||||
{
|
||||
surfaceDirty = true; // Retry partial failures, including upload failures.
|
||||
region = {};
|
||||
for (auto &slot : slots)
|
||||
{
|
||||
if (!rebuildSlot(slot, width, height, format))
|
||||
return false;
|
||||
if (slot.region.empty())
|
||||
continue;
|
||||
if (region.empty())
|
||||
region = slot.region;
|
||||
else
|
||||
{
|
||||
const uint32_t right = (std::max)(region.left + region.width, slot.region.left + slot.region.width);
|
||||
const uint32_t bottom = (std::max)(region.top + region.height, slot.region.top + slot.region.height);
|
||||
region.left = (std::min)(region.left, slot.region.left);
|
||||
region.top = (std::min)(region.top, slot.region.top);
|
||||
region.width = right - region.left;
|
||||
region.height = bottom - region.top;
|
||||
}
|
||||
}
|
||||
// Include hidden slots in the shared ROI so show/hide never rerasterizes
|
||||
// glyphs or recreates textures. Visibility is a shader constant.
|
||||
if (!region.empty())
|
||||
{
|
||||
if (!ensureDevice() || !ensureResources(region.width, region.height, format))
|
||||
return false;
|
||||
const uint32_t bpp = format == GVFG_PIXFMT_YUY2 ? 2u : 4u;
|
||||
const size_t yuvOffset = (static_cast<size_t>(region.top) * width + region.left) * bpp;
|
||||
const size_t maskOffset = static_cast<size_t>(region.top) * width + region.left;
|
||||
for (auto &slot : slots)
|
||||
{
|
||||
D3D11_MAPPED_SUBRESOURCE mapped{};
|
||||
if (FAILED(context->Map(slot.texture.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)))
|
||||
return false;
|
||||
const bool yuvOk = copyRowsToMapped(slot.yuv.data() + yuvOffset,
|
||||
static_cast<int>(width * bpp), region.width * bpp, region.height, mapped);
|
||||
context->Unmap(slot.texture.Get(), 0);
|
||||
if (!yuvOk || FAILED(context->Map(slot.maskTexture.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)))
|
||||
return false;
|
||||
const bool maskOk = copyRowsToMapped(slot.mask.data() + maskOffset,
|
||||
static_cast<int>(width), region.width, region.height, mapped);
|
||||
context->Unmap(slot.maskTexture.Get(), 0);
|
||||
if (!maskOk)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
surfaceWidth = width;
|
||||
surfaceHeight = height;
|
||||
surfaceFormat = format;
|
||||
surfaceDirty = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
gvfg_status_t process(const gvfg_frame_t &source,
|
||||
std::vector<uint8_t> &destination,
|
||||
int &destinationStride,
|
||||
bool &applied,
|
||||
gvfg_debug_osd_frame_timing_t *timing,
|
||||
uint8_t *writableSource,
|
||||
bool *inPlaceApplied)
|
||||
{
|
||||
if (inPlaceApplied)
|
||||
*inPlaceApplied = false;
|
||||
using Clock = std::chrono::steady_clock;
|
||||
auto stageStart = timing ? Clock::now() : Clock::time_point{};
|
||||
const auto finishStage = [timing, &stageStart](gvfg_debug_osd_stage_t stage) {
|
||||
if (timing)
|
||||
{
|
||||
const auto end = Clock::now();
|
||||
timing->stage_ms[stage] = std::chrono::duration<double, std::milli>(end - stageStart).count();
|
||||
stageStart = end;
|
||||
}
|
||||
};
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
finishStage(GVFG_DEBUG_OSD_LOCK_WAIT);
|
||||
applied = false;
|
||||
uint32_t visibleSlots = 0;
|
||||
for (uint32_t i = 0; i < slots.size(); ++i)
|
||||
if (slots[i].configured && slots[i].visible)
|
||||
visibleSlots |= 1u << i;
|
||||
if (!visibleSlots)
|
||||
return GVFG_OK;
|
||||
if (!source.data || source.width <= 0 || source.height <= 0 ||
|
||||
(source.width & 1) != 0 || source.row_stride_bytes <= 0 ||
|
||||
(source.pixel_format != GVFG_PIXFMT_YUY2 &&
|
||||
source.pixel_format != GVFG_PIXFMT_Y210))
|
||||
return GVFG_EINVAL;
|
||||
const uint64_t rowBytes64 = static_cast<uint64_t>(source.width) *
|
||||
(source.pixel_format == GVFG_PIXFMT_YUY2 ? 2u : 4u);
|
||||
if (rowBytes64 > static_cast<uint64_t>((std::numeric_limits<int>::max)()) ||
|
||||
static_cast<uint64_t>(source.row_stride_bytes) < rowBytes64)
|
||||
return GVFG_EINVAL;
|
||||
const uint64_t requiredSource =
|
||||
static_cast<uint64_t>(source.row_stride_bytes) *
|
||||
static_cast<uint64_t>(source.height - 1) +
|
||||
rowBytes64;
|
||||
if (source.data_size < requiredSource)
|
||||
return GVFG_EINVAL;
|
||||
if (writableSource && static_cast<const void *>(writableSource) != source.data)
|
||||
return GVFG_EINVAL;
|
||||
|
||||
const bool rebuild = surfaceDirty || surfaceWidth != static_cast<uint32_t>(source.width) ||
|
||||
surfaceHeight != static_cast<uint32_t>(source.height) ||
|
||||
surfaceFormat != source.pixel_format;
|
||||
if (timing)
|
||||
timing->surface_rebuilt = rebuild ? 1 : 0;
|
||||
if (rebuild &&
|
||||
!rebuildSurface(static_cast<uint32_t>(source.width),
|
||||
static_cast<uint32_t>(source.height), source.pixel_format))
|
||||
return GVFG_EIO;
|
||||
finishStage(GVFG_DEBUG_OSD_SURFACE);
|
||||
// Invisible text needs no processed buffer or GPU work.
|
||||
bool hasVisibleInk = false;
|
||||
for (const auto &slot : slots)
|
||||
hasVisibleInk = hasVisibleInk || (slot.visible && !slot.region.empty());
|
||||
if (region.empty() || !hasVisibleInk)
|
||||
return GVFG_OK;
|
||||
|
||||
const uint32_t rowBytes = static_cast<uint32_t>(rowBytes64);
|
||||
const uint32_t bytesPerPixel = source.pixel_format == GVFG_PIXFMT_YUY2 ? 2u : 4u;
|
||||
const uint32_t regionRowBytes = region.width * bytesPerPixel;
|
||||
const uint64_t sourceOffset64 = static_cast<uint64_t>(region.top) * source.row_stride_bytes +
|
||||
static_cast<uint64_t>(region.left) * bytesPerPixel;
|
||||
if (sourceOffset64 > (std::numeric_limits<size_t>::max)())
|
||||
return GVFG_EINVAL;
|
||||
const size_t sourceOffset = static_cast<size_t>(sourceOffset64);
|
||||
if (!writableSource)
|
||||
destination.resize(static_cast<size_t>(rowBytes) * static_cast<size_t>(source.height));
|
||||
uint8_t *outputData = writableSource ? writableSource : destination.data();
|
||||
const uint32_t outputStride = writableSource ? static_cast<uint32_t>(source.row_stride_bytes) : rowBytes;
|
||||
if (timing)
|
||||
{
|
||||
timing->roi_width = region.width;
|
||||
timing->roi_height = region.height;
|
||||
}
|
||||
finishStage(GVFG_DEBUG_OSD_PREPARE);
|
||||
D3D11_MAPPED_SUBRESOURCE mapped{};
|
||||
if (FAILED(context->Map(sourceTexture.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)))
|
||||
return GVFG_EIO;
|
||||
const bool copiedSource = copyRowsToMapped(
|
||||
static_cast<const uint8_t *>(source.data) + sourceOffset,
|
||||
source.row_stride_bytes, regionRowBytes, region.height, mapped);
|
||||
context->Unmap(sourceTexture.Get(), 0);
|
||||
if (!copiedSource)
|
||||
return GVFG_EIO;
|
||||
finishStage(GVFG_DEBUG_OSD_UPLOAD);
|
||||
|
||||
const uint32_t blendValues[4] = {
|
||||
source.pixel_format == GVFG_PIXFMT_Y210 ? 0xffc0u : 0x00ffu, visibleSlots, 0u, 0u};
|
||||
context->UpdateSubresource(blendParameters.Get(), 0, nullptr, blendValues, 0, 0);
|
||||
ID3D11ShaderResourceView *srvs[] = {sourceSrv.Get(), slots[0].srv.Get(), slots[0].maskSrv.Get(),
|
||||
slots[1].srv.Get(), slots[1].maskSrv.Get()};
|
||||
ID3D11UnorderedAccessView *uavs[] = {outputUav.Get()};
|
||||
context->CSSetShader(shader.Get(), nullptr, 0);
|
||||
ID3D11Buffer *constantBuffers[] = {blendParameters.Get()};
|
||||
context->CSSetConstantBuffers(0, 1, constantBuffers);
|
||||
context->CSSetShaderResources(0, 5, srvs);
|
||||
context->CSSetUnorderedAccessViews(0, 1, uavs, nullptr);
|
||||
context->Dispatch((region.width / 2u + 15u) / 16u,
|
||||
(region.height + 15u) / 16u, 1);
|
||||
ID3D11ShaderResourceView *nullSrvs[] = {nullptr, nullptr, nullptr, nullptr, nullptr};
|
||||
ID3D11UnorderedAccessView *nullUav = nullptr;
|
||||
context->CSSetShaderResources(0, 5, nullSrvs);
|
||||
context->CSSetUnorderedAccessViews(0, 1, &nullUav, nullptr);
|
||||
ID3D11Buffer *nullBuffer = nullptr;
|
||||
context->CSSetConstantBuffers(0, 1, &nullBuffer);
|
||||
context->CSSetShader(nullptr, nullptr, 0);
|
||||
|
||||
context->CopyResource(readbackTexture.Get(), outputTexture.Get());
|
||||
finishStage(GVFG_DEBUG_OSD_SUBMIT);
|
||||
// Driver zero-copy input requires an independent frame by default.
|
||||
// SDK-owned copy input already contains the untouched full frame, so
|
||||
// only its ROI is overwritten after GPU readback; no full copy/resize.
|
||||
if (!writableSource)
|
||||
{
|
||||
copyFrameRows(source, outputData, rowBytes);
|
||||
finishStage(GVFG_DEBUG_OSD_FRAME_COPY);
|
||||
}
|
||||
if (FAILED(context->Map(readbackTexture.Get(), 0, D3D11_MAP_READ, 0, &mapped)))
|
||||
return GVFG_EIO;
|
||||
finishStage(GVFG_DEBUG_OSD_READBACK_WAIT);
|
||||
if (mapped.RowPitch < regionRowBytes)
|
||||
{
|
||||
context->Unmap(readbackTexture.Get(), 0);
|
||||
return GVFG_EIO;
|
||||
}
|
||||
const size_t destinationOffset = static_cast<size_t>(region.top) * outputStride +
|
||||
static_cast<size_t>(region.left) * bytesPerPixel;
|
||||
for (uint32_t row = 0; row < region.height; ++row)
|
||||
{
|
||||
std::memcpy(outputData + destinationOffset + static_cast<size_t>(row) * outputStride,
|
||||
static_cast<const uint8_t *>(mapped.pData) +
|
||||
static_cast<size_t>(row) * mapped.RowPitch,
|
||||
regionRowBytes);
|
||||
}
|
||||
context->Unmap(readbackTexture.Get(), 0);
|
||||
finishStage(GVFG_DEBUG_OSD_ROI_COPY);
|
||||
destinationStride = static_cast<int>(outputStride);
|
||||
applied = true;
|
||||
if (inPlaceApplied)
|
||||
*inPlaceApplied = writableSource != nullptr;
|
||||
if (timing)
|
||||
timing->osd_applied = 1;
|
||||
return GVFG_OK;
|
||||
}
|
||||
|
||||
mutable std::mutex mutex;
|
||||
std::array<Slot, GVFG_OSD_SLOT_COUNT> slots;
|
||||
bool surfaceDirty = true;
|
||||
OsdRegion region;
|
||||
uint32_t surfaceWidth = 0;
|
||||
uint32_t surfaceHeight = 0;
|
||||
int surfaceFormat = GVFG_PIXFMT_UNKNOWN;
|
||||
uint32_t resourceWidth = 0;
|
||||
uint32_t resourceHeight = 0;
|
||||
int resourceFormat = GVFG_PIXFMT_UNKNOWN;
|
||||
ComPtr<ID3D11Device> device;
|
||||
ComPtr<ID3D11DeviceContext> context;
|
||||
ComPtr<ID3D11ComputeShader> shader;
|
||||
ComPtr<ID3D11Buffer> blendParameters;
|
||||
ComPtr<ID3D11Texture2D> sourceTexture;
|
||||
ComPtr<ID3D11Texture2D> outputTexture;
|
||||
ComPtr<ID3D11Texture2D> readbackTexture;
|
||||
ComPtr<ID3D11ShaderResourceView> sourceSrv;
|
||||
ComPtr<ID3D11UnorderedAccessView> outputUav;
|
||||
};
|
||||
|
||||
GpuOsdProcessor::GpuOsdProcessor() : impl_(std::make_unique<Impl>()) {}
|
||||
GpuOsdProcessor::~GpuOsdProcessor() = default;
|
||||
gvfg_status_t GpuOsdProcessor::configure(uint32_t index, const gvfg_channel_osd_config_t &config)
|
||||
{
|
||||
try
|
||||
{
|
||||
return impl_->configure(index, config);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return GVFG_EIO;
|
||||
}
|
||||
}
|
||||
gvfg_status_t GpuOsdProcessor::clear(uint32_t index) { return impl_->clear(index); }
|
||||
gvfg_status_t GpuOsdProcessor::setVisible(uint32_t index, int visible) { return impl_->setVisible(index, visible); }
|
||||
gvfg_status_t GpuOsdProcessor::process(const gvfg_frame_t &source,
|
||||
std::vector<uint8_t> &destination,
|
||||
int &destinationStride,
|
||||
bool &applied,
|
||||
gvfg_debug_osd_frame_timing_t *timing,
|
||||
uint8_t *writableSource,
|
||||
bool *inPlaceApplied)
|
||||
{
|
||||
try
|
||||
{
|
||||
return impl_->process(source, destination, destinationStride, applied, timing, writableSource, inPlaceApplied);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
applied = false;
|
||||
if (inPlaceApplied)
|
||||
*inPlaceApplied = false;
|
||||
destinationStride = 0;
|
||||
return GVFG_EIO;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
#include "gvfg_osd.h"
|
||||
#include "gvfg_debug.h"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace gvfg::internal
|
||||
{
|
||||
class GpuOsdProcessor
|
||||
{
|
||||
public:
|
||||
GpuOsdProcessor();
|
||||
~GpuOsdProcessor();
|
||||
|
||||
gvfg_status_t configure(uint32_t index, const gvfg_channel_osd_config_t &config);
|
||||
gvfg_status_t clear(uint32_t index);
|
||||
gvfg_status_t setVisible(uint32_t index, int visible);
|
||||
// writableSource must equal source.data. Only the backend can grant this
|
||||
// writable SDK-owned storage, held until the caller releases the frame.
|
||||
// nullptr preserves the separate-output path used for driver zero-copy.
|
||||
gvfg_status_t process(const gvfg_frame_t &source,
|
||||
std::vector<uint8_t> &destination,
|
||||
int &destinationStride,
|
||||
bool &applied,
|
||||
gvfg_debug_osd_frame_timing_t *timing = nullptr,
|
||||
uint8_t *writableSource = nullptr,
|
||||
bool *inPlaceApplied = nullptr);
|
||||
|
||||
private:
|
||||
struct Impl;
|
||||
std::unique_ptr<Impl> impl_;
|
||||
};
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "gvfg_osd.h"
|
||||
#include "gvfg_osd_core.h"
|
||||
|
||||
#include <d2d1.h>
|
||||
#include <dwrite.h>
|
||||
|
||||
@@ -0,0 +1,240 @@
|
||||
#include "gvfg_osd.h"
|
||||
#include "gvfg_osd_core.h"
|
||||
#include "gvfg_gpu_osd.h"
|
||||
#include "gvfg_frame_processor.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <climits>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <new>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
struct OsdFrameProcessor
|
||||
{
|
||||
gvfg::internal::GpuOsdProcessor gpu;
|
||||
std::vector<uint8_t> pixels;
|
||||
};
|
||||
|
||||
const gvfg_frame_processor processor{
|
||||
[](void **context) -> gvfg_status_t {
|
||||
try
|
||||
{
|
||||
*context = new OsdFrameProcessor;
|
||||
return GVFG_OK;
|
||||
}
|
||||
catch (const std::bad_alloc &)
|
||||
{
|
||||
return GVFG_EIO;
|
||||
}
|
||||
},
|
||||
[](void *context) { delete static_cast<OsdFrameProcessor *>(context); },
|
||||
[](void *context, const gvfg_frame_t &source, gvfg_frame_t &output,
|
||||
bool &applied, gvfg_debug_osd_frame_timing_t *timing, uint8_t *writable,
|
||||
bool *inPlace) {
|
||||
auto &state = *static_cast<OsdFrameProcessor *>(context);
|
||||
int stride = 0;
|
||||
const auto status = state.gpu.process(source, state.pixels, stride, applied, timing,
|
||||
writable, inPlace);
|
||||
if (status == GVFG_OK && applied && !*inPlace)
|
||||
{
|
||||
output.data = state.pixels.data();
|
||||
output.data_size = state.pixels.size();
|
||||
output.row_stride_bytes = stride;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
};
|
||||
|
||||
struct SlotCommand
|
||||
{
|
||||
uint32_t index;
|
||||
const gvfg_channel_osd_config_t *config;
|
||||
int visible;
|
||||
};
|
||||
}
|
||||
|
||||
extern "C"
|
||||
{
|
||||
gvfg_status_t gvfg_get_osd_fonts(gvfg_osd_font_list_t *out_list)
|
||||
{
|
||||
if (!out_list)
|
||||
return GVFG_EINVAL;
|
||||
*out_list = {};
|
||||
const auto status = gvfg_osd_get_fonts(&out_list->count, &out_list->names);
|
||||
return status == GVFG_OSD_OK ? GVFG_OK : GVFG_EIO;
|
||||
}
|
||||
|
||||
gvfg_status_t gvfg_measure_osd_text(const gvfg_channel_osd_config_t *config,
|
||||
uint32_t *out_width, uint32_t *out_height)
|
||||
{
|
||||
if (out_width) *out_width = 0;
|
||||
if (out_height) *out_height = 0;
|
||||
if (!config || !config->text || !out_width || !out_height)
|
||||
return GVFG_EINVAL;
|
||||
const size_t length = strnlen_s(config->text, GVFG_OSD_MAX_TEXT_CHARACTERS + 1u);
|
||||
if (length == 0 || length > GVFG_OSD_MAX_TEXT_CHARACTERS)
|
||||
return GVFG_EINVAL;
|
||||
try
|
||||
{
|
||||
gvfg_osd_handle rawHandle = nullptr;
|
||||
if (gvfg_osd_create(&rawHandle) != GVFG_OSD_OK)
|
||||
return GVFG_EIO;
|
||||
const std::unique_ptr<void, decltype(&gvfg_osd_destroy)> helper(rawHandle, &gvfg_osd_destroy);
|
||||
const auto status = gvfg_osd_measure(helper.get(), config->text,
|
||||
static_cast<uint32_t>(length), config->font_size_px, config->font_family,
|
||||
out_width, out_height);
|
||||
return status == GVFG_OSD_OK ? GVFG_OK :
|
||||
status == GVFG_OSD_EINVAL ? GVFG_EINVAL : GVFG_EIO;
|
||||
}
|
||||
catch (...) { return GVFG_EIO; }
|
||||
}
|
||||
|
||||
static gvfg_status_t renderOsdBgraTile(const gvfg_channel_osd_config_t *config,
|
||||
gvfg_osd_bitmap_t *out_bitmap,
|
||||
std::vector<uint8_t> &pixels)
|
||||
{
|
||||
if (!config || !config->text)
|
||||
return GVFG_EINVAL;
|
||||
const size_t length = strnlen_s(config->text, GVFG_OSD_MAX_TEXT_CHARACTERS + 1u);
|
||||
if (length == 0 || length > GVFG_OSD_MAX_TEXT_CHARACTERS)
|
||||
return GVFG_EINVAL;
|
||||
try
|
||||
{
|
||||
gvfg_osd_handle rawHandle = nullptr;
|
||||
if (gvfg_osd_create(&rawHandle) != GVFG_OSD_OK)
|
||||
return GVFG_EIO;
|
||||
const std::unique_ptr<void, decltype(&gvfg_osd_destroy)> helper(rawHandle, &gvfg_osd_destroy);
|
||||
uint32_t width = 0, height = 0;
|
||||
auto status = gvfg_osd_measure(helper.get(), config->text,
|
||||
static_cast<uint32_t>(length), config->font_size_px, config->font_family,
|
||||
&width, &height);
|
||||
if (status != GVFG_OSD_OK)
|
||||
return status == GVFG_OSD_EINVAL ? GVFG_EINVAL : GVFG_EIO;
|
||||
const uint64_t stride = static_cast<uint64_t>(width) * 4u;
|
||||
// WIC GetDataPointer reports its byte count as UINT.
|
||||
if (width > INT_MAX || height > INT_MAX || stride > UINT_MAX ||
|
||||
stride * height > UINT_MAX ||
|
||||
stride * height > (std::numeric_limits<size_t>::max)())
|
||||
return GVFG_EINVAL;
|
||||
pixels.resize(static_cast<size_t>(stride * height));
|
||||
gvfg_osd_config_t render{};
|
||||
render.struct_size = sizeof(render);
|
||||
render.width = width;
|
||||
render.height = height;
|
||||
render.text = config->text;
|
||||
render.font_family = config->font_family;
|
||||
render.text_length = static_cast<uint32_t>(length);
|
||||
render.font_size_px = config->font_size_px;
|
||||
render.color_rgb = config->color_rgb;
|
||||
status = gvfg_osd_render_bgra(helper.get(), &render, pixels.data(), pixels.size(),
|
||||
static_cast<uint32_t>(stride));
|
||||
if (status != GVFG_OSD_OK)
|
||||
return status == GVFG_OSD_EINVAL ? GVFG_EINVAL : GVFG_EIO;
|
||||
out_bitmap->data = pixels.data();
|
||||
out_bitmap->data_size = pixels.size();
|
||||
out_bitmap->width = width;
|
||||
out_bitmap->height = height;
|
||||
out_bitmap->stride = static_cast<uint32_t>(stride);
|
||||
return GVFG_OK;
|
||||
}
|
||||
catch (...) { return GVFG_EIO; }
|
||||
}
|
||||
|
||||
gvfg_status_t gvfg_render_osd_frame_bgra(uint32_t video_width, uint32_t video_height,
|
||||
const gvfg_channel_osd_config_t *slot0,
|
||||
const gvfg_channel_osd_config_t *slot1,
|
||||
gvfg_osd_bitmap_t *out_bitmap)
|
||||
{
|
||||
thread_local std::vector<uint8_t> frame;
|
||||
frame.clear();
|
||||
if (!out_bitmap)
|
||||
return GVFG_EINVAL;
|
||||
*out_bitmap = {};
|
||||
const uint64_t stride = static_cast<uint64_t>(video_width) * 4u;
|
||||
if (!video_width || !video_height || stride > UINT32_MAX ||
|
||||
stride * video_height > UINT32_MAX ||
|
||||
stride * video_height > (std::numeric_limits<size_t>::max)())
|
||||
return GVFG_EINVAL;
|
||||
|
||||
try
|
||||
{
|
||||
frame.assign(static_cast<size_t>(stride * video_height), 0);
|
||||
std::vector<uint8_t> tilePixels;
|
||||
for (const auto *slot : {slot0, slot1})
|
||||
{
|
||||
if (!slot)
|
||||
continue;
|
||||
gvfg_osd_bitmap_t tile{};
|
||||
const auto status = renderOsdBgraTile(slot, &tile, tilePixels);
|
||||
if (status != GVFG_OK)
|
||||
return status;
|
||||
if (tile.width > video_width || tile.height > video_height)
|
||||
continue;
|
||||
|
||||
const size_t x = std::clamp<int64_t>(slot->x, 0, video_width - tile.width);
|
||||
const size_t y = std::clamp<int64_t>(slot->y, 0, video_height - tile.height);
|
||||
for (uint32_t row = 0; row < tile.height; ++row)
|
||||
{
|
||||
const auto *src = tile.data + static_cast<size_t>(row) * tile.stride;
|
||||
auto *dst = frame.data() + (y + row) * static_cast<size_t>(stride) + x * 4u;
|
||||
for (uint32_t col = 0; col < tile.width; ++col, src += 4, dst += 4)
|
||||
{
|
||||
if (!src[3])
|
||||
continue;
|
||||
// Both layers are premultiplied, including at glyph edges.
|
||||
const unsigned remaining = 255u - src[3];
|
||||
for (unsigned c = 0; c < 4; ++c)
|
||||
dst[c] = static_cast<uint8_t>(src[c] + (dst[c] * remaining + 127u) / 255u);
|
||||
}
|
||||
}
|
||||
}
|
||||
*out_bitmap = {frame.data(), frame.size(), video_width, video_height,
|
||||
static_cast<uint32_t>(stride)};
|
||||
return GVFG_OK;
|
||||
}
|
||||
catch (...) { return GVFG_EIO; }
|
||||
}
|
||||
|
||||
gvfg_status_t gvfg_set_channel_osd(gvfg_handle handle, int channel_index,
|
||||
uint32_t index, const gvfg_channel_osd_config_t *config)
|
||||
{
|
||||
if (!config || index >= GVFG_OSD_SLOT_COUNT)
|
||||
return gvfg_control_frame_processor(handle, channel_index, &processor, false,
|
||||
nullptr, nullptr, "OSD config or index is invalid");
|
||||
const SlotCommand command{index, config, 0};
|
||||
return gvfg_control_frame_processor(handle, channel_index, &processor, true,
|
||||
[](void *context, const void *argument) {
|
||||
const auto &slot = *static_cast<const SlotCommand *>(argument);
|
||||
return static_cast<OsdFrameProcessor *>(context)->gpu.configure(slot.index, *slot.config);
|
||||
}, &command, "Could not configure OSD slot");
|
||||
}
|
||||
|
||||
gvfg_status_t gvfg_set_channel_osd_visible(gvfg_handle handle, int channel_index,
|
||||
uint32_t index, int visible)
|
||||
{
|
||||
if (index >= GVFG_OSD_SLOT_COUNT || (visible != 0 && visible != 1))
|
||||
return gvfg_control_frame_processor(handle, channel_index, &processor, false,
|
||||
nullptr, nullptr, "OSD index or visibility is invalid");
|
||||
const SlotCommand command{index, nullptr, visible};
|
||||
return gvfg_control_frame_processor(handle, channel_index, &processor, true,
|
||||
[](void *context, const void *argument) {
|
||||
const auto &slot = *static_cast<const SlotCommand *>(argument);
|
||||
return static_cast<OsdFrameProcessor *>(context)->gpu.setVisible(slot.index, slot.visible);
|
||||
}, &command, "Could not change OSD visibility");
|
||||
}
|
||||
|
||||
gvfg_status_t gvfg_clear_channel_osd(gvfg_handle handle, int channel_index, uint32_t index)
|
||||
{
|
||||
if (index >= GVFG_OSD_SLOT_COUNT)
|
||||
return gvfg_control_frame_processor(handle, channel_index, &processor, false,
|
||||
nullptr, nullptr, "OSD index is invalid");
|
||||
return gvfg_control_frame_processor(handle, channel_index, &processor, false,
|
||||
[](void *context, const void *argument) {
|
||||
return static_cast<OsdFrameProcessor *>(context)->gpu.clear(*static_cast<const uint32_t *>(argument));
|
||||
}, &index, "Could not clear OSD slot");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
#ifndef GVFG_OSD_H
|
||||
#define GVFG_OSD_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifndef GVFG_OSD_MAX_TEXT_CHARACTERS
|
||||
#define GVFG_OSD_MAX_TEXT_CHARACTERS 32u
|
||||
#endif
|
||||
|
||||
typedef void *gvfg_osd_handle;
|
||||
|
||||
typedef enum gvfg_osd_status_t
|
||||
{
|
||||
GVFG_OSD_OK = 0,
|
||||
GVFG_OSD_EINVAL = -1,
|
||||
GVFG_OSD_ENOMEM = -2,
|
||||
GVFG_OSD_ERENDER = -3
|
||||
} gvfg_osd_status_t;
|
||||
|
||||
typedef enum gvfg_osd_pixel_format_t
|
||||
{
|
||||
GVFG_OSD_PIXFMT_YUY2 = 1,
|
||||
GVFG_OSD_PIXFMT_Y210 = 2
|
||||
} gvfg_osd_pixel_format_t;
|
||||
|
||||
/*
|
||||
* Generates an off-line OSD surface using the requested installed system font. Text is
|
||||
* restricted to printable ASCII and at most GVFG_OSD_MAX_TEXT_CHARACTERS
|
||||
* characters on one line without wrapping. color_rgb is 0xRRGGBB.
|
||||
* x/y identify the top-left of the conservative measured text box (including
|
||||
* glyph overhang and a one-pixel guard). They are clamped to keep that box
|
||||
* inside the frame. Rendering returns EINVAL if the box cannot fit the frame.
|
||||
* Output YUV uses BT.709 limited range. The mask is one coverage byte per
|
||||
* pixel: 0 is transparent and 255 is fully covered.
|
||||
*/
|
||||
typedef struct gvfg_osd_config_t
|
||||
{
|
||||
uint32_t struct_size;
|
||||
uint32_t width;
|
||||
uint32_t height;
|
||||
uint32_t pixel_format;
|
||||
const char *text;
|
||||
const char *font_family; /* UTF-8, NUL-terminated. */
|
||||
uint32_t text_length;
|
||||
int32_t x;
|
||||
int32_t y;
|
||||
uint32_t font_size_px;
|
||||
uint32_t color_rgb;
|
||||
} gvfg_osd_config_t;
|
||||
|
||||
/* All buffers are caller-owned and are used only for the duration of render. */
|
||||
typedef struct gvfg_osd_output_t
|
||||
{
|
||||
uint32_t struct_size;
|
||||
void *yuv_data;
|
||||
uint64_t yuv_size;
|
||||
uint32_t yuv_stride;
|
||||
void *mask_data;
|
||||
uint64_t mask_size;
|
||||
uint32_t mask_stride;
|
||||
} gvfg_osd_output_t;
|
||||
|
||||
gvfg_osd_status_t gvfg_osd_create(
|
||||
gvfg_osd_handle *out_handle);
|
||||
|
||||
void gvfg_osd_destroy(
|
||||
gvfg_osd_handle handle);
|
||||
|
||||
/* Conservative whole-text bounds in pixels, not pixel-tight raster bounds. */
|
||||
gvfg_osd_status_t gvfg_osd_measure(
|
||||
gvfg_osd_handle handle,
|
||||
const char *text,
|
||||
uint32_t text_length,
|
||||
uint32_t font_size_px,
|
||||
const char *font_family,
|
||||
uint32_t *out_width,
|
||||
uint32_t *out_height);
|
||||
|
||||
/* SDK-owned snapshot, invalidated by the next call. Serialize call/read access. */
|
||||
gvfg_osd_status_t gvfg_osd_get_fonts(uint32_t *out_count, const char *const **out_names);
|
||||
|
||||
gvfg_osd_status_t gvfg_osd_render(
|
||||
gvfg_osd_handle handle,
|
||||
const gvfg_osd_config_t *config,
|
||||
const gvfg_osd_output_t *output);
|
||||
|
||||
/* Caller-owned, top-down BGRA8888 with premultiplied alpha. Ignores pixel_format.
|
||||
* Unlike YUV output, width may be odd. No RGB/YUV conversion is performed.
|
||||
*/
|
||||
gvfg_osd_status_t gvfg_osd_render_bgra(
|
||||
gvfg_osd_handle handle,
|
||||
const gvfg_osd_config_t *config,
|
||||
void *data, uint64_t data_size, uint32_t stride);
|
||||
|
||||
const char *gvfg_osd_strerror(
|
||||
gvfg_osd_status_t status);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -145,13 +145,15 @@ GVFG_PLAYBACK_API gvfg_playback_status_t gvfg_playback_submit_video(gvfg_playbac
|
||||
* Requires an attached window and a stopped handle. Caller data stays valid
|
||||
* through return. Used for RAW/DICOM image display; no audio is initialized. */
|
||||
GVFG_PLAYBACK_API gvfg_playback_status_t gvfg_playback_present_image(gvfg_playback p, const gvfg_preview_frame_t *image);
|
||||
/* In A/V mode an audio-output failure disables monitoring for this handle,
|
||||
* preserving video playback. Subsequent audio submissions return EAUDIO.
|
||||
* Keep capture/recording audio independent; do not retry output on this handle.
|
||||
* Recreate the handle to retry monitoring. Stop/reset does not clear EAUDIO.
|
||||
/* In A/V mode an unavailable audio output returns EAUDIO while video continues.
|
||||
* With reconnect enabled, keep submitting new PCM so monitoring can resume
|
||||
* when the default output becomes available. Otherwise recreate the handle
|
||||
* to retry monitoring. Keep capture/recording audio independent.
|
||||
* A/V start may succeed with monitoring disabled; audio-only start still fails
|
||||
* if its output is unavailable. Other errors retain their normal meaning. */
|
||||
GVFG_PLAYBACK_API gvfg_playback_status_t gvfg_playback_submit_audio(gvfg_playback p, const void *pcm, uint32_t bytes, uint64_t timestamp_ns);
|
||||
/* Opt in before start. Retries the default Windows output in A/V mode only. */
|
||||
GVFG_PLAYBACK_API gvfg_playback_status_t gvfg_playback_enable_audio_reconnect(gvfg_playback p);
|
||||
GVFG_PLAYBACK_API gvfg_playback_status_t gvfg_playback_set_volume(gvfg_playback p, float gain);
|
||||
/* Default enabled=1. Set 0 for independent best-effort playback with no
|
||||
* automatic return to synchronization. May be called before start or while
|
||||
|
||||
@@ -12,7 +12,9 @@
|
||||
#include <condition_variable>
|
||||
#include <cstdlib>
|
||||
#include <deque>
|
||||
#include <future>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <new>
|
||||
#include <thread>
|
||||
@@ -42,6 +44,8 @@ struct gvfg_playback_t {
|
||||
bool stopping = false, active = false;
|
||||
bool sync_enabled = true;
|
||||
bool audio_output_failed = false;
|
||||
bool audio_reconnect_enabled = false;
|
||||
uint64_t audio_retry_at = 0;
|
||||
float gain = 1.0f;
|
||||
void *hwnd = nullptr;
|
||||
SignalCard signal_card;
|
||||
@@ -94,14 +98,14 @@ struct gvfg_playback_t {
|
||||
if (!has_video()) { fail(GVFG_PLAYBACK_EOUTPUT); return; }
|
||||
if (audio_output_failed) return;
|
||||
audio_output_failed = true;
|
||||
audio_retry_at = now_ns() + 1'000'000'000;
|
||||
drop_audio();
|
||||
gvfg_audio_player_destroy(audio); audio = nullptr;
|
||||
audio_source_clock = {}; audio_timeline = {};
|
||||
first_audio = audio_end = 0;
|
||||
stats.audio_timestamp_ns = 0; stats.av_difference_ns = 0;
|
||||
stats.audio_rate_ratio = 1;
|
||||
// Retain video packets and move off a failed audio master. Monitoring
|
||||
// remains disabled across resets; a fresh handle may retry the device.
|
||||
// Retain video packets and move off a failed audio master.
|
||||
degrade(now_ns());
|
||||
}
|
||||
void fail(gvfg_playback_status_t error) {
|
||||
@@ -130,11 +134,46 @@ struct gvfg_playback_t {
|
||||
};
|
||||
|
||||
void gvfg_playback_t::loop() {
|
||||
using AudioPlayer = std::unique_ptr<gvfg_audio_player_t, decltype(&gvfg_audio_player_destroy)>;
|
||||
std::future<AudioPlayer> audio_retry;
|
||||
uint64_t last_control = 0;
|
||||
bool started = false, preview_ready = false;
|
||||
for (;;) {
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
if (stopping) break;
|
||||
if (audio_retry.valid() && audio_retry.wait_for(std::chrono::seconds(0)) == std::future_status::ready) {
|
||||
auto candidate = audio_retry.get();
|
||||
audio_retry_at = now_ns() + 1'000'000'000;
|
||||
if (candidate && audio_output_failed) {
|
||||
gvfg_audio_player_set_volume(candidate.get(), gain);
|
||||
audio = candidate.release();
|
||||
last_audio_delivery = last_audio_arrival = 0;
|
||||
recovery_rate = {};
|
||||
fallback_audio_primed = false;
|
||||
audio_wait_since = stable_since = 0;
|
||||
audio_output_failed = false;
|
||||
}
|
||||
}
|
||||
if (audio_reconnect_enabled && config.mode == GVFG_PLAYBACK_AUDIO_VIDEO &&
|
||||
audio_output_failed && !audio_retry.valid() && now_ns() >= audio_retry_at &&
|
||||
stats.last_error == GVFG_PLAYBACK_OK) {
|
||||
const auto format = config.audio_format;
|
||||
try {
|
||||
audio_retry = std::async(std::launch::async, [format] {
|
||||
AudioPlayer candidate(nullptr, gvfg_audio_player_destroy);
|
||||
try {
|
||||
candidate.reset(audio_output_create_managed());
|
||||
if (candidate && gvfg_audio_player_start(candidate.get(), &format) != GVFG_AUDIO_PLAYBACK_OK)
|
||||
candidate.reset();
|
||||
} catch (...) {
|
||||
candidate.reset();
|
||||
}
|
||||
return candidate;
|
||||
});
|
||||
} catch (...) {
|
||||
audio_retry_at = now_ns() + 1'000'000'000;
|
||||
}
|
||||
}
|
||||
if (stats.last_error != GVFG_PLAYBACK_OK) {
|
||||
changed.wait_for(lock, std::chrono::milliseconds(2)); continue;
|
||||
}
|
||||
@@ -555,6 +594,13 @@ gvfg_playback_status_t gvfg_playback_set_sync_enabled(gvfg_playback p, int enabl
|
||||
if (p->active && !p->sync_enabled) p->degrade(now_ns(), false);
|
||||
return GVFG_PLAYBACK_OK;
|
||||
}
|
||||
gvfg_playback_status_t gvfg_playback_enable_audio_reconnect(gvfg_playback p) {
|
||||
if (!p) return GVFG_PLAYBACK_EINVAL;
|
||||
std::lock_guard<std::mutex> lock(p->mutex);
|
||||
if (p->active) return GVFG_PLAYBACK_ESTATE;
|
||||
p->audio_reconnect_enabled = true;
|
||||
return GVFG_PLAYBACK_OK;
|
||||
}
|
||||
gvfg_playback_status_t gvfg_playback_stop(gvfg_playback p) {
|
||||
if (!p) return GVFG_PLAYBACK_EINVAL;
|
||||
{ std::lock_guard<std::mutex> lock(p->mutex); p->stopping = true; p->changed.notify_all(); }
|
||||
|
||||
Reference in New Issue
Block a user