FFmpeg
Loading...
Searching...
No Matches
vsrc_gfxcapture_winrt.cpp File Reference
#include "vsrc_gfxcapture_winrt.hpp"
#include "vsrc_gfxcapture_shader.h"
#include <dwmapi.h>
#include <d3d11.h>
#include <d3dcompiler.h>
#include <dispatcherqueue.h>
#include <windows.foundation.h>
#include <windows.graphics.capture.h>
#include <windows.graphics.capture.interop.h>
#include <windows.graphics.directx.direct3d11.h>
#include "libavutil/avassert.h"
#include "libavutil/internal.h"
#include "libavutil/mem.h"
#include "libavutil/opt.h"
#include "libavutil/time.h"
#include "libavutil/pixdesc.h"
#include "libavutil/hwcontext.h"
#include "libavutil/hwcontext_d3d11va.h"
#include "avfilter.h"
#include "filters.h"
#include "video.h"
#include "vsrc_gfxcapture.h"
#include <cinttypes>
#include <condition_variable>
#include <cwchar>
#include <memory>
#include <mutex>
#include <regex>
#include <string>
#include <system_error>
#include <thread>
#include <type_traits>

Go to the source code of this file.

Data Structures

struct  GfxCaptureFunctions
 
struct  GfxCaptureContextWgc
 
struct  GfxCaptureContextD3D
 
struct  GfxCaptureContextCpp
 

Macros

#define TIMESPAN_RES   10000000
 
#define TIMESPAN_RES64   INT64_C(10000000)
 
#define CAPTURE_POOL_SIZE   2
 
#define CCTX(ctx)
 
#define CHECK_HR(fcall, action)
 
#define CHECK_HR_RET(...)
 
#define CHECK_HR_FAIL(...)
 
#define CHECK_HR_LOG(...)
 
#define LOAD_DLL(handle, name)
 
#define LOAD_FUNC(handle, name)
 

Enumerations

enum  { WM_WGC_THREAD_SHUTDOWN = WM_APP + 1 }
 

Functions

template<typename T>
static HRESULT get_activation_factory (GfxCaptureContextCpp *ctx, PCWSTR clsid, T **factory)
 
static void wgc_frame_arrived_handler (const std::unique_ptr< GfxCaptureContextWgc > &wgctx)
 
static void wgc_closed_handler (const std::unique_ptr< GfxCaptureContextWgc > &wgctx)
 
static void wgc_stop_capture_session (AVFilterContext *avctx) noexcept
 
static int wgc_calculate_client_area (AVFilterContext *avctx)
 
static int wgc_setup_gfxcapture_session (AVFilterContext *avctx)
 
static int wgc_setup_gfxcapture_capture (AVFilterContext *avctx)
 
static int wgc_try_get_next_frame (AVFilterContext *avctx, ComPtr< IDirect3D11CaptureFrame > &capture_frame)
 
static int wgc_setup_winrt (AVFilterContext *avctx)
 
static void wgc_thread_uninit (AVFilterContext *avctx) noexcept
 
static int wgc_thread_init (AVFilterContext *avctx)
 
static int wgc_thread_worker (AVFilterContext *avctx)
 
static void wgc_thread_entry (AVFilterContext *avctx) noexcept
 
static int stop_wgc_thread (AVFilterContext *avctx)
 
static int start_wgc_thread (AVFilterContext *avctx)
 
template<typename F>
static int run_on_wgc_thread (AVFilterContext *avctx, F &&cb)
 
static int build_regex (AVFilterContext *avctx, const char *pattern, std::regex *out)
 
static int wstring_to_utf8 (const wchar_t *in, std::string *out)
 
static int get_window_exe_name (HWND hwnd, std::string *out)
 
static int find_capture_source (AVFilterContext *avctx)
 
static av_cold void gfxcapture_uninit (AVFilterContext *avctx)
 
template<typename T>
static av_cold void GetProcAddressTyped (const hmodule_ptr_t &hModule, LPCSTR lpProcName, T *out)
 
static av_cold int load_functions (AVFilterContext *avctx)
 
static av_cold int gfxcapture_init (AVFilterContext *avctx)
 
static int init_hwframes_ctx (AVFilterContext *avctx)
 
static int setup_gfxcapture_capture (AVFilterContext *avctx)
 
static int prepare_render_resources (AVFilterContext *avctx)
 
static int gfxcapture_config_props (AVFilterLink *outlink)
 
static int render_capture_to_frame (AVFilterContext *avctx, AVFrame *frame, const ComPtr< ID3D11Texture2D > &src_tex)
 
static int process_frame_if_exists (AVFilterLink *outlink)
 
static int gfxcapture_activate (AVFilterContext *avctx)
 
av_cold void ff_gfxcapture_uninit (AVFilterContext *avctx) noexcept
 
av_cold int ff_gfxcapture_init (AVFilterContext *avctx) noexcept
 
int ff_gfxcapture_activate (AVFilterContext *avctx) noexcept
 
int ff_gfxcapture_config_props (AVFilterLink *outlink) noexcept
 

Macro Definition Documentation

◆ TIMESPAN_RES

#define TIMESPAN_RES   10000000

Definition at line 71 of file vsrc_gfxcapture_winrt.cpp.

Referenced by gfxcapture_config_props().

◆ TIMESPAN_RES64

#define TIMESPAN_RES64   INT64_C(10000000)

Definition at line 72 of file vsrc_gfxcapture_winrt.cpp.

◆ CAPTURE_POOL_SIZE

#define CAPTURE_POOL_SIZE   2

◆ CCTX

◆ CHECK_HR

#define CHECK_HR ( fcall,
action )
Value:
do { \
HRESULT fhr = fcall; \
if (FAILED(fhr)) { \
av_log(avctx, AV_LOG_ERROR, #fcall " failed: 0x%08lX\n", fhr); \
action; \
} \
} while (0)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210

Definition at line 182 of file vsrc_gfxcapture_winrt.cpp.

◆ CHECK_HR_RET

#define CHECK_HR_RET ( ...)
Value:
CHECK_HR((__VA_ARGS__), return AVERROR_EXTERNAL)
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition error.h:59
#define CHECK_HR(fcall, action)

Definition at line 190 of file vsrc_gfxcapture_winrt.cpp.

Referenced by prepare_render_resources(), process_frame_if_exists(), render_capture_to_frame(), run_on_wgc_thread(), wgc_setup_gfxcapture_capture(), wgc_setup_gfxcapture_session(), wgc_setup_winrt(), and wgc_try_get_next_frame().

◆ CHECK_HR_FAIL

#define CHECK_HR_FAIL ( ...)
Value:
CHECK_HR((__VA_ARGS__), ret = AVERROR_EXTERNAL; goto fail)
#define fail
Definition test.h:479

Definition at line 191 of file vsrc_gfxcapture_winrt.cpp.

◆ CHECK_HR_LOG

#define CHECK_HR_LOG ( ...)
Value:
CHECK_HR((__VA_ARGS__), (void)0)

Definition at line 192 of file vsrc_gfxcapture_winrt.cpp.

Referenced by wgc_stop_capture_session().

◆ LOAD_DLL

#define LOAD_DLL ( handle,
name )
Value:
handle = hmodule_ptr_t(LoadLibraryExW(L##name, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32)); \
if (!handle) { \
av_log(avctx, AV_LOG_ERROR, "Failed opening " #name "\n"); \
return AVERROR(ENOSYS); \
}
#define L(x)
Definition vpx_arith.h:36
#define NULL
Definition coverity.c:32
#define AVERROR(e)
Definition error.h:45
const char * name
Definition qsvenc.c:142
std::unique_ptr< HMODULE, HMODULEDeleter > hmodule_ptr_t

Referenced by load_functions().

◆ LOAD_FUNC

#define LOAD_FUNC ( handle,
name )
Value:
GetProcAddressTyped(handle, #name, &ctx->fn.name); \
if (!ctx->fn.name) { \
av_log(avctx, AV_LOG_ERROR, "Failed loading " #name "\n"); \
return AVERROR(ENOSYS); \
}
static av_cold void GetProcAddressTyped(const hmodule_ptr_t &hModule, LPCSTR lpProcName, T *out)

Referenced by load_functions().

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
WM_WGC_THREAD_SHUTDOWN 

Definition at line 76 of file vsrc_gfxcapture_winrt.cpp.

Function Documentation

◆ get_activation_factory()

template<typename T>
static HRESULT get_activation_factory ( GfxCaptureContextCpp * ctx,
PCWSTR clsid,
T ** factory )
static

◆ wgc_frame_arrived_handler()

static void wgc_frame_arrived_handler ( const std::unique_ptr< GfxCaptureContextWgc > & wgctx)
static

Definition at line 199 of file vsrc_gfxcapture_winrt.cpp.

◆ wgc_closed_handler()

static void wgc_closed_handler ( const std::unique_ptr< GfxCaptureContextWgc > & wgctx)
static

Definition at line 207 of file vsrc_gfxcapture_winrt.cpp.

◆ wgc_stop_capture_session()

static void wgc_stop_capture_session ( AVFilterContext * avctx)
staticnoexcept

Definition at line 215 of file vsrc_gfxcapture_winrt.cpp.

Referenced by wgc_thread_uninit(), and wgc_thread_worker().

◆ wgc_calculate_client_area()

static int wgc_calculate_client_area ( AVFilterContext * avctx)
static

◆ wgc_setup_gfxcapture_session()

static int wgc_setup_gfxcapture_session ( AVFilterContext * avctx)
static

Definition at line 325 of file vsrc_gfxcapture_winrt.cpp.

Referenced by wgc_setup_gfxcapture_capture().

◆ wgc_setup_gfxcapture_capture()

static int wgc_setup_gfxcapture_capture ( AVFilterContext * avctx)
static

Definition at line 406 of file vsrc_gfxcapture_winrt.cpp.

Referenced by wgc_thread_init().

◆ wgc_try_get_next_frame()

static int wgc_try_get_next_frame ( AVFilterContext * avctx,
ComPtr< IDirect3D11CaptureFrame > & capture_frame )
static

Definition at line 446 of file vsrc_gfxcapture_winrt.cpp.

Referenced by process_frame_if_exists().

◆ wgc_setup_winrt()

static int wgc_setup_winrt ( AVFilterContext * avctx)
static

Definition at line 480 of file vsrc_gfxcapture_winrt.cpp.

Referenced by wgc_thread_init().

◆ wgc_thread_uninit()

static void wgc_thread_uninit ( AVFilterContext * avctx)
staticnoexcept

Definition at line 501 of file vsrc_gfxcapture_winrt.cpp.

Referenced by wgc_thread_entry(), and wgc_thread_init().

◆ wgc_thread_init()

static int wgc_thread_init ( AVFilterContext * avctx)
static

Definition at line 512 of file vsrc_gfxcapture_winrt.cpp.

Referenced by wgc_thread_entry().

◆ wgc_thread_worker()

static int wgc_thread_worker ( AVFilterContext * avctx)
static

Definition at line 549 of file vsrc_gfxcapture_winrt.cpp.

Referenced by wgc_thread_entry().

◆ wgc_thread_entry()

static void wgc_thread_entry ( AVFilterContext * avctx)
staticnoexcept

Definition at line 599 of file vsrc_gfxcapture_winrt.cpp.

Referenced by start_wgc_thread().

◆ stop_wgc_thread()

static int stop_wgc_thread ( AVFilterContext * avctx)
static

Definition at line 656 of file vsrc_gfxcapture_winrt.cpp.

Referenced by gfxcapture_uninit(), and setup_gfxcapture_capture().

◆ start_wgc_thread()

static int start_wgc_thread ( AVFilterContext * avctx)
static

Definition at line 675 of file vsrc_gfxcapture_winrt.cpp.

Referenced by setup_gfxcapture_capture().

◆ run_on_wgc_thread()

template<typename F>
static int run_on_wgc_thread ( AVFilterContext * avctx,
F && cb )
static

Definition at line 706 of file vsrc_gfxcapture_winrt.cpp.

Referenced by process_frame_if_exists().

◆ build_regex()

static int build_regex ( AVFilterContext * avctx,
const char * pattern,
std::regex * out )
static

Definition at line 775 of file vsrc_gfxcapture_winrt.cpp.

Referenced by find_capture_source().

◆ wstring_to_utf8()

static int wstring_to_utf8 ( const wchar_t * in,
std::string * out )
static

Definition at line 802 of file vsrc_gfxcapture_winrt.cpp.

Referenced by find_capture_source(), and get_window_exe_name().

◆ get_window_exe_name()

static int get_window_exe_name ( HWND hwnd,
std::string * out )
static

Definition at line 817 of file vsrc_gfxcapture_winrt.cpp.

Referenced by find_capture_source().

◆ find_capture_source()

static int find_capture_source ( AVFilterContext * avctx)
static

Definition at line 857 of file vsrc_gfxcapture_winrt.cpp.

Referenced by setup_gfxcapture_capture().

◆ gfxcapture_uninit()

static av_cold void gfxcapture_uninit ( AVFilterContext * avctx)
static

Definition at line 971 of file vsrc_gfxcapture_winrt.cpp.

Referenced by ff_gfxcapture_uninit(), and gfxcapture_init().

◆ GetProcAddressTyped()

template<typename T>
static av_cold void GetProcAddressTyped ( const hmodule_ptr_t & hModule,
LPCSTR lpProcName,
T * out )
static

Definition at line 991 of file vsrc_gfxcapture_winrt.cpp.

◆ load_functions()

static av_cold int load_functions ( AVFilterContext * avctx)
static

Definition at line 995 of file vsrc_gfxcapture_winrt.cpp.

Referenced by d3d11va_device_create().

◆ gfxcapture_init()

static av_cold int gfxcapture_init ( AVFilterContext * avctx)
static

Definition at line 1047 of file vsrc_gfxcapture_winrt.cpp.

Referenced by ff_gfxcapture_init().

◆ init_hwframes_ctx()

static int init_hwframes_ctx ( AVFilterContext * avctx)
static

Definition at line 1068 of file vsrc_gfxcapture_winrt.cpp.

Referenced by gfxcapture_config_props().

◆ setup_gfxcapture_capture()

static int setup_gfxcapture_capture ( AVFilterContext * avctx)
static

Definition at line 1101 of file vsrc_gfxcapture_winrt.cpp.

Referenced by gfxcapture_config_props().

◆ prepare_render_resources()

static int prepare_render_resources ( AVFilterContext * avctx)
static

Definition at line 1143 of file vsrc_gfxcapture_winrt.cpp.

Referenced by gfxcapture_config_props().

◆ gfxcapture_config_props()

static int gfxcapture_config_props ( AVFilterLink * outlink)
static

Definition at line 1198 of file vsrc_gfxcapture_winrt.cpp.

Referenced by ff_gfxcapture_config_props().

◆ render_capture_to_frame()

static int render_capture_to_frame ( AVFilterContext * avctx,
AVFrame * frame,
const ComPtr< ID3D11Texture2D > & src_tex )
static

Definition at line 1271 of file vsrc_gfxcapture_winrt.cpp.

Referenced by process_frame_if_exists().

◆ process_frame_if_exists()

static int process_frame_if_exists ( AVFilterLink * outlink)
static

Definition at line 1397 of file vsrc_gfxcapture_winrt.cpp.

Referenced by gfxcapture_activate().

◆ gfxcapture_activate()

static int gfxcapture_activate ( AVFilterContext * avctx)
static

Definition at line 1462 of file vsrc_gfxcapture_winrt.cpp.

Referenced by ff_gfxcapture_activate().

◆ ff_gfxcapture_uninit()

av_cold void ff_gfxcapture_uninit ( AVFilterContext * avctx)
noexcept

Definition at line 1501 of file vsrc_gfxcapture_winrt.cpp.

◆ ff_gfxcapture_init()

av_cold int ff_gfxcapture_init ( AVFilterContext * avctx)
noexcept

Definition at line 1512 of file vsrc_gfxcapture_winrt.cpp.

◆ ff_gfxcapture_activate()

int ff_gfxcapture_activate ( AVFilterContext * avctx)
noexcept

Definition at line 1527 of file vsrc_gfxcapture_winrt.cpp.

◆ ff_gfxcapture_config_props()

int ff_gfxcapture_config_props ( AVFilterLink * outlink)
noexcept

Definition at line 1542 of file vsrc_gfxcapture_winrt.cpp.