Merge pull request #5218 from jpark37/win-audio-per-process

win-wasapi: Add support for capturing a process
This commit is contained in:
Jim 2022-07-23 18:19:44 -07:00 committed by GitHub
commit 4e1ba08701
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
31 changed files with 821 additions and 284 deletions

View File

@ -273,6 +273,26 @@ void WindowCaptureToolbar::Init()
ComboSelectToolbar::Init();
}
ApplicationAudioCaptureToolbar::ApplicationAudioCaptureToolbar(QWidget *parent,
OBSSource source)
: ComboSelectToolbar(parent, source)
{
}
void ApplicationAudioCaptureToolbar::Init()
{
delete ui->activateButton;
ui->activateButton = nullptr;
obs_module_t *mod = obs_get_module("win-wasapi");
const char *device_str = obs_module_get_locale_text(mod, "Window");
ui->deviceLabel->setText(device_str);
prop_name = "window";
ComboSelectToolbar::Init();
}
DisplayCaptureToolbar::DisplayCaptureToolbar(QWidget *parent, OBSSource source)
: ComboSelectToolbar(parent, source)
{

View File

@ -82,6 +82,14 @@ public:
void Init() override;
};
class ApplicationAudioCaptureToolbar : public ComboSelectToolbar {
Q_OBJECT
public:
ApplicationAudioCaptureToolbar(QWidget *parent, OBSSource source);
void Init() override;
};
class DisplayCaptureToolbar : public ComboSelectToolbar {
Q_OBJECT

View File

@ -1103,6 +1103,7 @@ OBSBasic {
qproperty-groupIcon: url(./Dark/sources/group.svg);
qproperty-sceneIcon: url(./Dark/sources/scene.svg);
qproperty-defaultIcon: url(./Dark/sources/default.svg);
qproperty-audioProcessOutputIcon: url(./Dark/sources/windowaudio.svg);
}
/* Scene Tree */

View File

@ -817,6 +817,7 @@ OBSBasic {
qproperty-groupIcon: url(./Dark/sources/group.svg);
qproperty-sceneIcon: url(./Dark/sources/scene.svg);
qproperty-defaultIcon: url(./Dark/sources/default.svg);
qproperty-audioProcessOutputIcon: url(./Dark/sources/windowaudio.svg);
}
/* Scene Tree */

View File

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="#d2d2d2" d="M1.5 1.5h1.5v1.5H1.5V1.5zM4.5 1.5h1.5v1.5H4.5V1.5zM7.5 1.5h1.5v1.5H7.5V1.5zM22.5 3H10.5V1.5h12v1.5zM22.5 22.5H1.5V4.5h21v18zM24 1.5c0-.55-.45-1.5-1.5-1.5H1.5c-.55 0-1.5 .45-1.5 1.5v21c0 .55 .45 1.5 1.5 1.5h21c.55 0 1.5-.45 1.5-1.5z"/><g fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" stroke="#d2d2d2"><polygon points="10 6.5 7 10.5 4 10.5 4 16.5 7 16.5 10 20.5" fill="#d2d2d2" stroke="#d2d2d2"/><path d="M17 6.5a10 10 0 0 1 0 14M14 10a5 5 0 0 1 0 7"/></g></svg>

After

Width:  |  Height:  |  Size: 575 B

View File

@ -1392,6 +1392,7 @@ OBSBasic {
qproperty-groupIcon: url(./Dark/sources/group.svg);
qproperty-sceneIcon: url(./Dark/sources/scene.svg);
qproperty-defaultIcon: url(./Dark/sources/default.svg);
qproperty-audioProcessOutputIcon: url(./Dark/sources/windowaudio.svg);
}
/* Scene Tree */

View File

@ -242,6 +242,7 @@ OBSBasic {
qproperty-groupIcon: url(:res/images/sources/group.svg);
qproperty-sceneIcon: url(:res/images/sources/scene.svg);
qproperty-defaultIcon: url(:res/images/sources/default.svg);
qproperty-audioProcessOutputIcon: url(:res/images/sources/windowaudio.svg);
}
/* Scene Tree */

View File

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M1.5 1.5h1.5v1.5H1.5V1.5zM4.5 1.5h1.5v1.5H4.5V1.5zM7.5 1.5h1.5v1.5H7.5V1.5zM22.5 3H10.5V1.5h12v1.5zM22.5 22.5H1.5V4.5h21v18zM24 1.5c0-.55-.45-1.5-1.5-1.5H1.5c-.55 0-1.5 .45-1.5 1.5v21c0 .55 .45 1.5 1.5 1.5h21c.55 0 1.5-.45 1.5-1.5z"/><g fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" stroke="#000000"><polygon points="10 6.5 7 10.5 4 10.5 4 16.5 7 16.5 10 20.5" fill="#000000" stroke="#000000"/><path d="M17 6.5a10 10 0 0 1 0 14M14 10a5 5 0 0 1 0 7"/></g></svg>

After

Width:  |  Height:  |  Size: 560 B

View File

@ -43,6 +43,7 @@
<file>images/sources/text.svg</file>
<file>images/sources/window.svg</file>
<file>images/sources/default.svg</file>
<file>images/sources/windowaudio.svg</file>
<file>images/recording-active.svg</file>
<file>images/recording-inactive.svg</file>
<file>images/recording-pause.svg</file>

View File

@ -32,6 +32,8 @@ QIcon OBSBasic::GetSourceIcon(const char *id) const
case OBS_ICON_TYPE_CUSTOM:
//TODO: Add ability for sources to define custom icons
return GetDefaultIcon();
case OBS_ICON_TYPE_PROCESS_AUDIO_OUTPUT:
return GetAudioProcessOutputIcon();
default:
return GetDefaultIcon();
}
@ -112,6 +114,11 @@ void OBSBasic::SetDefaultIcon(const QIcon &icon)
defaultIcon = icon;
}
void OBSBasic::SetAudioProcessOutputIcon(const QIcon &icon)
{
audioProcessOutputIcon = icon;
}
QIcon OBSBasic::GetImageIcon() const
{
return imageIcon;
@ -186,3 +193,8 @@ QIcon OBSBasic::GetDefaultIcon() const
{
return defaultIcon;
}
QIcon OBSBasic::GetAudioProcessOutputIcon() const
{
return audioProcessOutputIcon;
}

View File

@ -3110,6 +3110,15 @@ void OBSBasic::UpdateContextBar(bool force)
c->Init();
ui->emptySpace->layout()->addWidget(c);
} else if (strcmp(id,
"wasapi_process_output_capture") ==
0) {
ApplicationAudioCaptureToolbar *c =
new ApplicationAudioCaptureToolbar(
ui->emptySpace, source);
c->Init();
ui->emptySpace->layout()->addWidget(c);
} else if (strcmp(id, "window_capture") == 0 ||
strcmp(id, "xcomposite_input") == 0) {
WindowCaptureToolbar *c =

View File

@ -165,6 +165,8 @@ class OBSBasic : public OBSMainWindow {
DESIGNABLE true)
Q_PROPERTY(QIcon defaultIcon READ GetDefaultIcon WRITE SetDefaultIcon
DESIGNABLE true)
Q_PROPERTY(QIcon audioProcessOutputIcon READ GetAudioProcessOutputIcon
WRITE SetAudioProcessOutputIcon DESIGNABLE true)
friend class OBSAbout;
friend class OBSBasicPreview;
@ -560,6 +562,7 @@ private:
QIcon groupIcon;
QIcon sceneIcon;
QIcon defaultIcon;
QIcon audioProcessOutputIcon;
QIcon GetImageIcon() const;
QIcon GetColorIcon() const;
@ -574,6 +577,7 @@ private:
QIcon GetMediaIcon() const;
QIcon GetBrowserIcon() const;
QIcon GetDefaultIcon() const;
QIcon GetAudioProcessOutputIcon() const;
QSlider *tBar;
bool tBarActive = false;
@ -773,6 +777,7 @@ private slots:
void SetGroupIcon(const QIcon &icon);
void SetSceneIcon(const QIcon &icon);
void SetDefaultIcon(const QIcon &icon);
void SetAudioProcessOutputIcon(const QIcon &icon);
void TBarChanged(int value);
void TBarReleased();

View File

@ -284,8 +284,12 @@ if(OS_WINDOWS)
util/threading-windows.h
util/pipe-windows.c
util/platform-windows.c
util/windows/obfuscate.c
util/windows/obfuscate.h
util/windows/win-registry.h
util/windows/win-version.h
util/windows/window-helpers.c
util/windows/window-helpers.h
util/windows/ComPtr.hpp
util/windows/CoTaskMemPtr.hpp
util/windows/HRError.hpp
@ -300,7 +304,7 @@ if(OS_WINDOWS)
libobs PRIVATE UNICODE _UNICODE _CRT_SECURE_NO_WARNINGS
_CRT_NONSTDC_NO_WARNINGS)
target_link_libraries(libobs PRIVATE Avrt winmm)
target_link_libraries(libobs PRIVATE Avrt Dwmapi winmm)
if(MSVC)
target_link_libraries(libobs PRIVATE OBS::w32-pthreads)

View File

@ -58,6 +58,7 @@ enum obs_icon_type {
OBS_ICON_TYPE_MEDIA,
OBS_ICON_TYPE_BROWSER,
OBS_ICON_TYPE_CUSTOM,
OBS_ICON_TYPE_PROCESS_AUDIO_OUTPUT,
};
enum obs_media_state {

View File

@ -49,3 +49,34 @@ public:
return handle && handle != INVALID_HANDLE_VALUE;
}
};
class WinModule {
HMODULE handle = NULL;
inline void Clear()
{
if (handle)
FreeLibrary(handle);
}
public:
inline WinModule() {}
inline WinModule(HMODULE handle_) : handle(handle_) {}
inline ~WinModule() { Clear(); }
inline operator HMODULE() const { return handle; }
inline WinModule &operator=(HMODULE handle_)
{
if (handle_ != handle) {
Clear();
handle = handle_;
}
return *this;
}
inline HMODULE *operator&() { return &handle; }
inline bool Valid() const { return handle != NULL; }
};

View File

@ -30,7 +30,7 @@ static void deobfuscate_str(char *str, uint64_t val)
}
}
void *get_obfuscated_func(HMODULE module, const char *str, uint64_t val)
void *ms_get_obfuscated_func(HMODULE module, const char *str, uint64_t val)
{
char new_name[128];
strcpy(new_name, str);

View File

@ -1,6 +1,7 @@
#pragma once
#include <stdint.h>
#include "../c99defs.h"
#include <Windows.h>
#ifdef __cplusplus
extern "C" {
@ -8,7 +9,8 @@ extern "C" {
/* this is a workaround to A/Vs going crazy whenever certain functions (such as
* OpenProcess) are used */
extern void *get_obfuscated_func(HMODULE module, const char *str, uint64_t val);
EXPORT void *ms_get_obfuscated_func(HMODULE module, const char *str,
uint64_t val);
#ifdef __cplusplus
}

View File

@ -1,11 +1,10 @@
#include <obs.h>
#include "window-helpers.h"
#include <util/dstr.h>
#include <util/windows/obfuscate.h>
#include <dwmapi.h>
#include <psapi.h>
#include <windows.h>
#include "window-helpers.h"
#include "obfuscate.h"
static inline void encode_dstr(struct dstr *str)
{
@ -22,8 +21,8 @@ static inline char *decode_str(const char *src)
return str.array;
}
extern void build_window_strings(const char *str, char **class, char **title,
char **exe)
void ms_build_window_strings(const char *str, char **class, char **title,
char **exe)
{
char **strlist;
@ -46,6 +45,58 @@ extern void build_window_strings(const char *str, char **class, char **title,
strlist_free(strlist);
}
static void insert_preserved_val(obs_property_t *p, const char *val, size_t idx)
{
char *window_class = NULL;
char *title = NULL;
char *executable = NULL;
struct dstr desc = {0};
ms_build_window_strings(val, &window_class, &title, &executable);
dstr_printf(&desc, "[%s]: %s", executable, title);
obs_property_list_insert_string(p, idx, desc.array, val);
obs_property_list_item_disable(p, idx, true);
dstr_free(&desc);
bfree(window_class);
bfree(title);
bfree(executable);
}
bool ms_check_window_property_setting(obs_properties_t *ppts, obs_property_t *p,
obs_data_t *settings, const char *val,
size_t idx)
{
const char *cur_val;
bool match = false;
size_t i = 0;
cur_val = obs_data_get_string(settings, val);
if (!cur_val) {
return false;
}
for (;;) {
const char *val = obs_property_list_item_string(p, i++);
if (!val)
break;
if (strcmp(val, cur_val) == 0) {
match = true;
break;
}
}
if (cur_val && *cur_val && !match) {
insert_preserved_val(p, cur_val, idx);
return true;
}
UNUSED_PARAMETER(ppts);
return false;
}
static HMODULE kernel32(void)
{
static HMODULE kernel32_handle = NULL;
@ -60,13 +111,13 @@ static inline HANDLE open_process(DWORD desired_access, bool inherit_handle,
typedef HANDLE(WINAPI * PFN_OpenProcess)(DWORD, BOOL, DWORD);
static PFN_OpenProcess open_process_proc = NULL;
if (!open_process_proc)
open_process_proc = (PFN_OpenProcess)get_obfuscated_func(
open_process_proc = (PFN_OpenProcess)ms_get_obfuscated_func(
kernel32(), "B}caZyah`~q", 0x2D5BEBAF6DDULL);
return open_process_proc(desired_access, inherit_handle, process_id);
}
bool get_window_exe(struct dstr *name, HWND window)
bool ms_get_window_exe(struct dstr *name, HWND window)
{
wchar_t wname[MAX_PATH];
struct dstr temp = {0};
@ -103,7 +154,7 @@ fail:
return true;
}
void get_window_title(struct dstr *name, HWND hwnd)
void ms_get_window_title(struct dstr *name, HWND hwnd)
{
int len;
@ -130,7 +181,7 @@ void get_window_title(struct dstr *name, HWND hwnd)
}
}
void get_window_class(struct dstr *class, HWND hwnd)
void ms_get_window_class(struct dstr *class, HWND hwnd)
{
wchar_t temp[256];
@ -192,21 +243,21 @@ static void add_window(obs_property_t *p, HWND hwnd, add_window_cb callback)
struct dstr encoded = {0};
struct dstr desc = {0};
if (!get_window_exe(&exe, hwnd))
if (!ms_get_window_exe(&exe, hwnd))
return;
if (is_microsoft_internal_window_exe(exe.array)) {
dstr_free(&exe);
return;
}
get_window_title(&title, hwnd);
ms_get_window_title(&title, hwnd);
if (dstr_cmp(&exe, "explorer.exe") == 0 && dstr_is_empty(&title)) {
dstr_free(&exe);
dstr_free(&title);
return;
}
get_window_class(&class, hwnd);
ms_get_window_class(&class, hwnd);
if (callback && !callback(title.array, class.array, exe.array)) {
dstr_free(&title);
@ -268,7 +319,7 @@ static bool check_window_valid(HWND window, enum window_search_mode mode)
return true;
}
bool is_uwp_window(HWND hwnd)
bool ms_is_uwp_window(HWND hwnd)
{
wchar_t name[256];
@ -279,7 +330,7 @@ bool is_uwp_window(HWND hwnd)
return wcscmp(name, L"ApplicationFrameWindow") == 0;
}
HWND get_uwp_actual_window(HWND parent)
HWND ms_get_uwp_actual_window(HWND parent)
{
DWORD parent_id = 0;
HWND child;
@ -319,8 +370,8 @@ static HWND next_window(HWND window, enum window_search_mode mode, HWND *parent,
break;
}
if (is_uwp_window(window)) {
HWND child = get_uwp_actual_window(window);
if (ms_is_uwp_window(window)) {
HWND child = ms_get_uwp_actual_window(window);
if (child) {
*parent = window;
return child;
@ -357,8 +408,8 @@ static HWND first_window(enum window_search_mode mode, HWND *parent,
}
}
if (is_uwp_window(window)) {
HWND child = get_uwp_actual_window(window);
if (ms_is_uwp_window(window)) {
HWND child = ms_get_uwp_actual_window(window);
if (child) {
*parent = window;
return child;
@ -368,8 +419,8 @@ static HWND first_window(enum window_search_mode mode, HWND *parent,
return window;
}
void fill_window_list(obs_property_t *p, enum window_search_mode mode,
add_window_cb callback)
void ms_fill_window_list(obs_property_t *p, enum window_search_mode mode,
add_window_cb callback)
{
HWND parent;
bool use_findwindowex = false;
@ -384,24 +435,27 @@ void fill_window_list(obs_property_t *p, enum window_search_mode mode,
static int window_rating(HWND window, enum window_priority priority,
const char *class, const char *title, const char *exe,
bool generic_class)
bool uwp_window, bool generic_class)
{
struct dstr cur_class = {0};
struct dstr cur_title = {0};
struct dstr cur_exe = {0};
int val = 0x7FFFFFFF;
if (!get_window_exe(&cur_exe, window))
if (!ms_get_window_exe(&cur_exe, window))
return 0x7FFFFFFF;
get_window_title(&cur_title, window);
get_window_class(&cur_class, window);
ms_get_window_title(&cur_title, window);
ms_get_window_class(&cur_class, window);
bool class_matches = dstr_cmpi(&cur_class, class) == 0;
bool exe_matches = dstr_cmpi(&cur_exe, exe) == 0;
int title_val = abs(dstr_cmpi(&cur_title, title));
/* always match by name if class is generic */
if (generic_class) {
if (generic_class && (priority == WINDOW_PRIORITY_CLASS))
priority = WINDOW_PRIORITY_TITLE;
/* always match by name with UWP windows */
if (uwp_window) {
if (priority == WINDOW_PRIORITY_EXE && !exe_matches)
val = 0x7FFFFFFF;
else
@ -431,11 +485,6 @@ static const char *generic_class_substrings[] = {
NULL,
};
static const char *generic_classes[] = {
"Windows.UI.Core.CoreWindow",
NULL,
};
static bool is_generic_class(const char *current_class)
{
const char **class = generic_class_substrings;
@ -446,19 +495,16 @@ static bool is_generic_class(const char *current_class)
class ++;
}
class = generic_classes;
while (*class) {
if (astrcmpi(current_class, *class) == 0) {
return true;
}
class ++;
}
return false;
}
HWND find_window(enum window_search_mode mode, enum window_priority priority,
const char *class, const char *title, const char *exe)
static bool is_uwp_class(const char *window_class)
{
return strcmp(window_class, "Windows.UI.Core.CoreWindow") == 0;
}
HWND ms_find_window(enum window_search_mode mode, enum window_priority priority,
const char *class, const char *title, const char *exe)
{
HWND parent;
bool use_findwindowex = false;
@ -470,11 +516,12 @@ HWND find_window(enum window_search_mode mode, enum window_priority priority,
if (!class)
return NULL;
bool generic_class = is_generic_class(class);
const bool uwp_window = is_uwp_class(class);
const bool generic_class = is_generic_class(class);
while (window) {
int rating = window_rating(window, priority, class, title, exe,
generic_class);
uwp_window, generic_class);
if (rating < best_rating) {
best_rating = rating;
best_window = window;
@ -494,6 +541,7 @@ struct top_level_enum_data {
const char *class;
const char *title;
const char *exe;
bool uwp_window;
bool generic_class;
HWND best_window;
int best_rating;
@ -511,7 +559,7 @@ BOOL CALLBACK enum_windows_proc(HWND window, LPARAM lParam)
const int rating = window_rating(window, data->priority, data->class,
data->title, data->exe,
data->generic_class);
data->uwp_window, data->generic_class);
if (rating < data->best_rating) {
data->best_rating = rating;
data->best_window = window;
@ -520,9 +568,9 @@ BOOL CALLBACK enum_windows_proc(HWND window, LPARAM lParam)
return rating > 0;
}
HWND find_window_top_level(enum window_search_mode mode,
enum window_priority priority, const char *class,
const char *title, const char *exe)
HWND ms_find_window_top_level(enum window_search_mode mode,
enum window_priority priority, const char *class,
const char *title, const char *exe)
{
if (!class)
return NULL;
@ -533,6 +581,7 @@ HWND find_window_top_level(enum window_search_mode mode,
data.class = class;
data.title = title;
data.exe = exe;
data.uwp_window = is_uwp_class(class);
data.generic_class = is_generic_class(class);
data.best_window = NULL;
data.best_rating = 0x7FFFFFFF;

View File

@ -0,0 +1,56 @@
#pragma once
#include <obs-properties.h>
#include <util/c99defs.h>
#include <Windows.h>
#ifdef __cplusplus
extern "C" {
#endif
enum window_priority {
WINDOW_PRIORITY_CLASS,
WINDOW_PRIORITY_TITLE,
WINDOW_PRIORITY_EXE,
};
enum window_search_mode {
INCLUDE_MINIMIZED,
EXCLUDE_MINIMIZED,
};
EXPORT bool ms_get_window_exe(struct dstr *name, HWND window);
EXPORT void ms_get_window_title(struct dstr *name, HWND hwnd);
EXPORT void ms_get_window_class(struct dstr *window_class, HWND hwnd);
EXPORT bool ms_is_uwp_window(HWND hwnd);
EXPORT HWND ms_get_uwp_actual_window(HWND parent);
typedef bool (*add_window_cb)(const char *title, const char *window_class,
const char *exe);
EXPORT void ms_fill_window_list(obs_property_t *p, enum window_search_mode mode,
add_window_cb callback);
EXPORT void ms_build_window_strings(const char *str, char **window_class,
char **title, char **exe);
EXPORT bool ms_check_window_property_setting(obs_properties_t *ppts,
obs_property_t *p,
obs_data_t *settings,
const char *val, size_t idx);
EXPORT void ms_build_window_strings(const char *str, char **window_class,
char **title, char **exe);
EXPORT HWND ms_find_window(enum window_search_mode mode,
enum window_priority priority,
const char *window_class, const char *title,
const char *exe);
EXPORT HWND ms_find_window_top_level(enum window_search_mode mode,
enum window_priority priority,
const char *window_class,
const char *title, const char *exe);
#ifdef __cplusplus
}
#endif

View File

@ -24,13 +24,9 @@ target_sources(
monitor-capture.c
nt-stuff.c
nt-stuff.h
obfuscate.c
obfuscate.h
window-capture.c
window-helpers.c
window-helpers.h)
window-capture.c)
target_link_libraries(win-capture PRIVATE OBS::libobs OBS::ipc-util Dwmapi)
target_link_libraries(win-capture PRIVATE OBS::libobs OBS::ipc-util)
set_target_properties(win-capture PROPERTIES FOLDER "plugins/win-capture")

View File

@ -1,18 +1,19 @@
#include <inttypes.h>
#include <obs-module.h>
#include <obs-hotkey.h>
#include <util/dstr.h>
#include <util/platform.h>
#include <util/threading.h>
#include <util/windows/window-helpers.h>
#include <windows.h>
#include <dxgi.h>
#include <util/sse-intrin.h>
#include <util/util_uint64.h>
#include <ipc-util/pipe.h>
#include "obfuscate.h"
#include <util/windows/obfuscate.h>
#include "inject-library.h"
#include "graphics-hook-info.h"
#include "graphics-hook-ver.h"
#include "window-helpers.h"
#include "cursor-capture.h"
#include "app-helpers.h"
#include "nt-stuff.h"
@ -297,7 +298,7 @@ static inline HANDLE open_process(DWORD desired_access, bool inherit_handle,
typedef HANDLE(WINAPI * PFN_OpenProcess)(DWORD, BOOL, DWORD);
static PFN_OpenProcess open_process_proc = NULL;
if (!open_process_proc)
open_process_proc = (PFN_OpenProcess)get_obfuscated_func(
open_process_proc = (PFN_OpenProcess)ms_get_obfuscated_func(
kernel32(), "NuagUykjcxr", 0x1B694B59451ULL);
return open_process_proc(desired_access, inherit_handle, process_id);
@ -411,8 +412,8 @@ static inline void get_config(struct game_capture_config *cfg,
{
const char *mode_str = NULL;
build_window_strings(window, &cfg->class, &cfg->title,
&cfg->executable);
ms_build_window_strings(window, &cfg->class, &cfg->title,
&cfg->executable);
if (using_older_non_mode_format(settings)) {
bool any = obs_data_get_bool(settings, SETTING_ANY_FULLSCREEN);
@ -1021,12 +1022,12 @@ static bool init_hook(struct game_capture *gc)
bool blacklisted_process = false;
if (gc->config.mode == CAPTURE_MODE_ANY) {
if (get_window_exe(&exe, gc->next_window)) {
if (ms_get_window_exe(&exe, gc->next_window)) {
info("attempting to hook fullscreen process: %s",
exe.array);
}
} else {
if (get_window_exe(&exe, gc->next_window)) {
if (ms_get_window_exe(&exe, gc->next_window)) {
info("attempting to hook process: %s", exe.array);
}
}
@ -1166,9 +1167,9 @@ static void get_selected_window(struct game_capture *gc)
os_utf8_to_wcs(gc->class.array, 0, class_w, 512);
window = FindWindowW(class_w, NULL);
} else {
window = find_window(INCLUDE_MINIMIZED, gc->priority,
gc->class.array, gc->title.array,
gc->executable.array);
window = ms_find_window(INCLUDE_MINIMIZED, gc->priority,
gc->class.array, gc->title.array,
gc->executable.array);
}
if (window) {
@ -1780,12 +1781,12 @@ static void game_capture_tick(void *data, float seconds)
HWND hwnd = (HWND)(uintptr_t)os_atomic_load_long(
&gc->hotkey_window);
if (is_uwp_window(hwnd))
hwnd = get_uwp_actual_window(hwnd);
if (ms_is_uwp_window(hwnd))
hwnd = ms_get_uwp_actual_window(hwnd);
if (get_window_exe(&gc->executable, hwnd)) {
get_window_title(&gc->title, hwnd);
get_window_class(&gc->class, hwnd);
if (ms_get_window_exe(&gc->executable, hwnd)) {
ms_get_window_title(&gc->title, hwnd);
ms_get_window_class(&gc->class, hwnd);
gc->priority = WINDOW_PRIORITY_CLASS;
gc->retry_time = 10.0f * hook_rate_to_float(
@ -2247,63 +2248,11 @@ static bool mode_callback(obs_properties_t *ppts, obs_property_t *p,
return true;
}
static void insert_preserved_val(obs_property_t *p, const char *val, size_t idx)
{
char *class = NULL;
char *title = NULL;
char *executable = NULL;
struct dstr desc = {0};
build_window_strings(val, &class, &title, &executable);
dstr_printf(&desc, "[%s]: %s", executable, title);
obs_property_list_insert_string(p, idx, desc.array, val);
obs_property_list_item_disable(p, idx, true);
dstr_free(&desc);
bfree(class);
bfree(title);
bfree(executable);
}
bool check_window_property_setting(obs_properties_t *ppts, obs_property_t *p,
obs_data_t *settings, const char *val,
size_t idx)
{
const char *cur_val;
bool match = false;
size_t i = 0;
cur_val = obs_data_get_string(settings, val);
if (!cur_val) {
return false;
}
for (;;) {
const char *val = obs_property_list_item_string(p, i++);
if (!val)
break;
if (strcmp(val, cur_val) == 0) {
match = true;
break;
}
}
if (cur_val && *cur_val && !match) {
insert_preserved_val(p, cur_val, idx);
return true;
}
UNUSED_PARAMETER(ppts);
return false;
}
static bool window_changed_callback(obs_properties_t *ppts, obs_property_t *p,
obs_data_t *settings)
{
return check_window_property_setting(ppts, p, settings,
SETTING_CAPTURE_WINDOW, 1);
return ms_check_window_property_setting(ppts, p, settings,
SETTING_CAPTURE_WINDOW, 1);
}
static BOOL CALLBACK EnumFirstMonitor(HMONITOR monitor, HDC hdc, LPRECT rc,
@ -2377,7 +2326,7 @@ static obs_properties_t *game_capture_properties(void *data)
OBS_COMBO_TYPE_LIST,
OBS_COMBO_FORMAT_STRING);
obs_property_list_add_string(p, "", "");
fill_window_list(p, INCLUDE_MINIMIZED, window_not_blacklisted);
ms_fill_window_list(p, INCLUDE_MINIMIZED, window_not_blacklisted);
obs_property_set_modified_callback(p, window_changed_callback);

View File

@ -19,8 +19,8 @@ target_sources(
d3d10-capture.cpp
d3d11-capture.cpp
d3d12-capture.cpp
../obfuscate.c
../obfuscate.h
../../../libobs/util/windows/obfuscate.c
../../../libobs/util/windows/obfuscate.h
../graphics-hook-ver.h
../graphics-hook-info.h
../hook-helpers.h

View File

@ -3,7 +3,7 @@
#include <inttypes.h>
#include "graphics-hook.h"
#include "../graphics-hook-ver.h"
#include "../obfuscate.h"
#include "../../libobs/util/windows/obfuscate.h"
#define DEBUG_OUTPUT
@ -931,7 +931,7 @@ __declspec(dllexport) LRESULT CALLBACK
HMODULE user32 = GetModuleHandleW(L"USER32");
BOOL(WINAPI * unhook_windows_hook_ex)(HHOOK) = NULL;
unhook_windows_hook_ex = get_obfuscated_func(
unhook_windows_hook_ex = ms_get_obfuscated_func(
user32, "VojeleY`bdgxvM`hhDz", 0x7F55F80C9EE3A213ULL);
if (unhook_windows_hook_ex)

View File

@ -3,8 +3,10 @@ project(inject-helper)
add_executable(inject-helper)
target_sources(
inject-helper PRIVATE inject-helper.c ../inject-library.c ../inject-library.h
../obfuscate.c ../obfuscate.h)
inject-helper
PRIVATE inject-helper.c ../inject-library.c ../inject-library.h
../../../libobs/util/windows/obfuscate.c
../../../libobs/util/windows/obfuscate.h)
target_include_directories(inject-helper PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/..)

View File

@ -4,7 +4,7 @@
#include <windows.h>
#include <shellapi.h>
#include <stdbool.h>
#include "../obfuscate.h"
#include "../../../libobs/util/windows/obfuscate.h"
#include "../inject-library.h"
#if defined(_MSC_VER) && !defined(inline)
@ -38,9 +38,9 @@ static inline HANDLE open_process(DWORD desired_access, bool inherit_handle,
DWORD process_id)
{
HANDLE(WINAPI * open_process_proc)(DWORD, BOOL, DWORD);
open_process_proc = get_obfuscated_func(GetModuleHandleW(L"KERNEL32"),
"HxjcQrmkb|~",
0xc82efdf78201df87);
open_process_proc =
ms_get_obfuscated_func(GetModuleHandleW(L"KERNEL32"),
"HxjcQrmkb|~", 0xc82efdf78201df87);
return open_process_proc(desired_access, inherit_handle, process_id);
}

View File

@ -1,6 +1,6 @@
#include <windows.h>
#include <stdbool.h>
#include "obfuscate.h"
#include "../../libobs/util/windows/obfuscate.h"
#include "inject-library.h"
typedef HANDLE(WINAPI *create_remote_thread_t)(HANDLE, LPSECURITY_ATTRIBUTES,
@ -37,16 +37,16 @@ int inject_library_obf(HANDLE process, const wchar_t *dll,
virtual_free_ex_t virtual_free_ex;
FARPROC load_library_w;
create_remote_thread = (create_remote_thread_t)get_obfuscated_func(
create_remote_thread = (create_remote_thread_t)ms_get_obfuscated_func(
kernel32, create_remote_thread_obf, obf1);
write_process_memory = (write_process_memory_t)get_obfuscated_func(
write_process_memory = (write_process_memory_t)ms_get_obfuscated_func(
kernel32, write_process_memory_obf, obf2);
virtual_alloc_ex = (virtual_alloc_ex_t)get_obfuscated_func(
virtual_alloc_ex = (virtual_alloc_ex_t)ms_get_obfuscated_func(
kernel32, virtual_alloc_ex_obf, obf3);
virtual_free_ex = (virtual_free_ex_t)get_obfuscated_func(
virtual_free_ex = (virtual_free_ex_t)ms_get_obfuscated_func(
kernel32, virtual_free_ex_obf, obf4);
load_library_w = (FARPROC)get_obfuscated_func(kernel32,
load_library_w_obf, obf5);
load_library_w = (FARPROC)ms_get_obfuscated_func(
kernel32, load_library_w_obf, obf5);
/* -------------------------------- */
@ -126,7 +126,7 @@ int inject_library_safe_obf(DWORD thread_id, const wchar_t *dll,
return INJECT_ERROR_UNLIKELY_FAIL;
}
set_windows_hook_ex = (set_windows_hook_ex_t)get_obfuscated_func(
set_windows_hook_ex = (set_windows_hook_ex_t)ms_get_obfuscated_func(
user32, set_windows_hook_ex_obf, obf1);
hook = set_windows_hook_ex(WH_GETMESSAGE, proc, lib, thread_id);

View File

@ -1,8 +1,8 @@
#include <stdlib.h>
#include <util/dstr.h>
#include <util/threading.h>
#include <util/windows/window-helpers.h>
#include "dc-capture.h"
#include "window-helpers.h"
#include "../../libobs/util/platform.h"
#include "../../libobs-winrt/winrt-capture.h"
@ -198,7 +198,8 @@ static void update_settings(struct window_capture *wc, obs_data_t *s)
bfree(wc->class);
bfree(wc->executable);
build_window_strings(window, &wc->class, &wc->title, &wc->executable);
ms_build_window_strings(window, &wc->class, &wc->title,
&wc->executable);
wc->method = choose_method(method, wgc_supported, wc->class);
wc->priority = (enum window_priority)priority;
@ -414,11 +415,6 @@ static bool wc_capture_method_changed(obs_properties_t *props,
return true;
}
extern bool check_window_property_setting(obs_properties_t *ppts,
obs_property_t *p,
obs_data_t *settings, const char *val,
size_t idx);
static bool wc_window_changed(obs_properties_t *props, obs_property_t *p,
obs_data_t *settings)
{
@ -430,7 +426,7 @@ static bool wc_window_changed(obs_properties_t *props, obs_property_t *p,
update_settings_visibility(props, wc);
check_window_property_setting(props, p, settings, "window", 0);
ms_check_window_property_setting(props, p, settings, "window", 0);
return true;
}
@ -446,7 +442,7 @@ static obs_properties_t *wc_properties(void *data)
p = obs_properties_add_list(ppts, "window", TEXT_WINDOW,
OBS_COMBO_TYPE_LIST,
OBS_COMBO_FORMAT_STRING);
fill_window_list(p, EXCLUDE_MINIMIZED, NULL);
ms_fill_window_list(p, EXCLUDE_MINIMIZED, NULL);
obs_property_set_modified_callback(p, wc_window_changed);
p = obs_properties_add_list(ppts, "method", TEXT_METHOD,
@ -515,15 +511,15 @@ static void wc_tick(void *data, float seconds)
wc->check_window_timer = 0.0f;
wc->window = (wc->method == METHOD_WGC)
? find_window_top_level(INCLUDE_MINIMIZED,
wc->priority,
wc->class,
wc->title,
wc->executable)
: find_window(INCLUDE_MINIMIZED,
wc->priority, wc->class,
wc->title, wc->executable);
wc->window =
(wc->method == METHOD_WGC)
? ms_find_window_top_level(INCLUDE_MINIMIZED,
wc->priority,
wc->class, wc->title,
wc->executable)
: ms_find_window(INCLUDE_MINIMIZED,
wc->priority, wc->class,
wc->title, wc->executable);
if (!wc->window) {
if (wc->capture.valid)
dc_capture_free(&wc->capture);

View File

@ -1,37 +0,0 @@
#pragma once
#include <util/dstr.h>
enum window_priority {
WINDOW_PRIORITY_CLASS,
WINDOW_PRIORITY_TITLE,
WINDOW_PRIORITY_EXE,
};
enum window_search_mode {
INCLUDE_MINIMIZED,
EXCLUDE_MINIMIZED,
};
extern bool get_window_exe(struct dstr *name, HWND window);
extern void get_window_title(struct dstr *name, HWND hwnd);
extern void get_window_class(struct dstr *class, HWND hwnd);
extern bool is_uwp_window(HWND hwnd);
extern HWND get_uwp_actual_window(HWND parent);
typedef bool (*add_window_cb)(const char *title, const char *class,
const char *exe);
extern void fill_window_list(obs_property_t *p, enum window_search_mode mode,
add_window_cb callback);
extern void build_window_strings(const char *str, char **class, char **title,
char **exe);
extern HWND find_window(enum window_search_mode mode,
enum window_priority priority, const char *class,
const char *title, const char *exe);
extern HWND find_window_top_level(enum window_search_mode mode,
enum window_priority priority,
const char *class, const char *title,
const char *exe);

View File

@ -1,5 +1,10 @@
AudioInput="Audio Input Capture"
AudioOutput="Audio Output Capture"
ApplicationAudioCapture="Application Audio Capture (BETA)"
Device="Device"
Default="Default"
UseDeviceTiming="Use Device Timestamps"
Priority="Window Match Priority"
Priority.Title="Window title must match"
Priority.Class="Match title, otherwise find window of same type"
Priority.Exe="Match title, otherwise find window of same executable"

View File

@ -1,5 +1,7 @@
#include <obs-module.h>
#include <util/windows/win-version.h>
OBS_DECLARE_MODULE()
OBS_MODULE_USE_DEFAULT_LOCALE("win-wasapi", "en-US")
MODULE_EXPORT const char *obs_module_description(void)
@ -8,11 +10,25 @@ MODULE_EXPORT const char *obs_module_description(void)
}
void RegisterWASAPIInput();
void RegisterWASAPIOutput();
void RegisterWASAPIDeviceOutput();
void RegisterWASAPIProcessOutput();
bool obs_module_load(void)
{
/* MS says 20348, but process filtering seems to work earlier */
struct win_version_info ver;
get_win_ver(&ver);
struct win_version_info minimum;
minimum.major = 10;
minimum.minor = 0;
minimum.build = 19041;
minimum.revis = 0;
const bool process_filter_supported =
win_version_compare(&ver, &minimum) >= 0;
RegisterWASAPIInput();
RegisterWASAPIOutput();
RegisterWASAPIDeviceOutput();
if (process_filter_supported)
RegisterWASAPIProcessOutput();
return true;
}

View File

@ -2,31 +2,42 @@
#include <obs-module.h>
#include <obs.h>
#include <util/dstr.h>
#include <util/platform.h>
#include <util/windows/HRError.hpp>
#include <util/windows/ComPtr.hpp>
#include <util/windows/WinHandle.hpp>
#include <util/windows/CoTaskMemPtr.hpp>
#include <util/windows/win-version.h>
#include <util/windows/window-helpers.h>
#include <util/threading.h>
#include <util/util_uint64.h>
#include <atomic>
#include <cinttypes>
#include <audioclientactivationparams.h>
#include <avrt.h>
#include <RTWorkQ.h>
#include <wrl/implements.h>
using namespace std;
#define OPT_DEVICE_ID "device_id"
#define OPT_USE_DEVICE_TIMING "use_device_timing"
#define OPT_WINDOW "window"
#define OPT_PRIORITY "priority"
static void GetWASAPIDefaults(obs_data_t *settings);
#define OBS_KSAUDIO_SPEAKER_4POINT1 \
(KSAUDIO_SPEAKER_SURROUND | SPEAKER_LOW_FREQUENCY)
typedef HRESULT(STDAPICALLTYPE *PFN_ActivateAudioInterfaceAsync)(
LPCWSTR, REFIID, PROPVARIANT *,
IActivateAudioInterfaceCompletionHandler *,
IActivateAudioInterfaceAsyncOperation **);
typedef HRESULT(STDAPICALLTYPE *PFN_RtwqUnlockWorkQueue)(DWORD);
typedef HRESULT(STDAPICALLTYPE *PFN_RtwqLockSharedWorkQueue)(PCWSTR usageClass,
LONG basePriority,
@ -42,6 +53,61 @@ typedef HRESULT(STDAPICALLTYPE *PFN_RtwqPutWaitingWorkItem)(HANDLE, LONG,
IRtwqAsyncResult *,
RTWQWORKITEM_KEY *);
class WASAPIActivateAudioInterfaceCompletionHandler
: public Microsoft::WRL::RuntimeClass<
Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::ClassicCom>,
Microsoft::WRL::FtmBase,
IActivateAudioInterfaceCompletionHandler> {
IUnknown *unknown;
HRESULT activationResult;
WinHandle activationSignal;
public:
WASAPIActivateAudioInterfaceCompletionHandler();
HRESULT GetActivateResult(IAudioClient **client);
private:
virtual HRESULT STDMETHODCALLTYPE ActivateCompleted(
IActivateAudioInterfaceAsyncOperation *activateOperation)
override final;
};
WASAPIActivateAudioInterfaceCompletionHandler::
WASAPIActivateAudioInterfaceCompletionHandler()
{
activationSignal = CreateEvent(nullptr, false, false, nullptr);
if (!activationSignal.Valid())
throw "Could not create receive signal";
}
HRESULT
WASAPIActivateAudioInterfaceCompletionHandler::GetActivateResult(
IAudioClient **client)
{
WaitForSingleObject(activationSignal, INFINITE);
*client = static_cast<IAudioClient *>(unknown);
return activationResult;
}
HRESULT
WASAPIActivateAudioInterfaceCompletionHandler::ActivateCompleted(
IActivateAudioInterfaceAsyncOperation *activateOperation)
{
HRESULT hr, hr_activate;
hr = activateOperation->GetActivateResult(&hr_activate, &unknown);
hr = SUCCEEDED(hr) ? hr_activate : hr;
activationResult = hr;
SetEvent(activationSignal);
return hr;
}
enum class SourceType {
Input,
DeviceOutput,
ProcessOutput,
};
class ARtwqAsyncCallback : public IRtwqAsyncCallback {
protected:
ARtwqAsyncCallback(void *source) : source(source) {}
@ -97,13 +163,21 @@ class WASAPISource {
wstring default_id;
string device_id;
string device_name;
WinModule mmdevapi_module;
PFN_ActivateAudioInterfaceAsync activate_audio_interface_async = NULL;
PFN_RtwqUnlockWorkQueue rtwq_unlock_work_queue = NULL;
PFN_RtwqLockSharedWorkQueue rtwq_lock_shared_work_queue = NULL;
PFN_RtwqCreateAsyncResult rtwq_create_async_result = NULL;
PFN_RtwqPutWorkItem rtwq_put_work_item = NULL;
PFN_RtwqPutWaitingWorkItem rtwq_put_waiting_work_item = NULL;
bool rtwq_supported = false;
const bool isInputDevice;
window_priority priority;
string window_class;
string title;
string executable;
HWND hwnd = NULL;
DWORD process_id = 0;
const SourceType sourceType;
std::atomic<bool> useDeviceTiming = false;
std::atomic<bool> isDefaultDevice = false;
@ -173,6 +247,8 @@ class WASAPISource {
audio_format format;
uint32_t sampleRate;
uint64_t framesProcessed = 0;
static DWORD WINAPI ReconnectThread(LPVOID param);
static DWORD WINAPI CaptureThread(LPVOID param);
@ -183,13 +259,13 @@ class WASAPISource {
static ComPtr<IMMDevice> InitDevice(IMMDeviceEnumerator *enumerator,
bool isDefaultDevice,
bool isInputDevice,
SourceType type,
const string device_id);
static ComPtr<IAudioClient> InitClient(IMMDevice *device,
bool isInputDevice,
enum speaker_layout &speakers,
enum audio_format &format,
uint32_t &sampleRate);
static ComPtr<IAudioClient> InitClient(
IMMDevice *device, SourceType type, DWORD process_id,
PFN_ActivateAudioInterfaceAsync activate_audio_interface_async,
speaker_layout &speakers, audio_format &format,
uint32_t &sampleRate);
static void InitFormat(const WAVEFORMATEX *wfex,
enum speaker_layout &speakers,
enum audio_format &format, uint32_t &sampleRate);
@ -200,13 +276,27 @@ class WASAPISource {
bool TryInitialize();
void UpdateSettings(obs_data_t *settings);
struct UpdateParams {
string device_id;
bool useDeviceTiming;
bool isDefaultDevice;
window_priority priority;
string window_class;
string title;
string executable;
};
UpdateParams BuildUpdateParams(obs_data_t *settings);
void UpdateSettings(UpdateParams &&params);
void LogSettings();
public:
WASAPISource(obs_data_t *settings, obs_source_t *source_, bool input);
WASAPISource(obs_data_t *settings, obs_source_t *source_,
SourceType type);
~WASAPISource();
void Update(obs_data_t *settings);
void OnWindowChanged(obs_data_t *settings);
void SetDefaultDevice(EDataFlow flow, ERole role, LPCWSTR id);
@ -267,14 +357,22 @@ public:
};
WASAPISource::WASAPISource(obs_data_t *settings, obs_source_t *source_,
bool input)
SourceType type)
: source(source_),
isInputDevice(input),
sourceType(type),
startCapture(this),
sampleReady(this),
restart(this)
{
UpdateSettings(settings);
mmdevapi_module = LoadLibrary(L"Mmdevapi");
if (mmdevapi_module) {
activate_audio_interface_async =
(PFN_ActivateAudioInterfaceAsync)GetProcAddress(
mmdevapi_module, "ActivateAudioInterfaceAsync");
}
UpdateSettings(BuildUpdateParams(settings));
LogSettings();
idleSignal = CreateEvent(nullptr, true, false, nullptr);
if (!idleSignal.Valid())
@ -452,26 +550,108 @@ WASAPISource::~WASAPISource()
Stop();
}
void WASAPISource::UpdateSettings(obs_data_t *settings)
WASAPISource::UpdateParams WASAPISource::BuildUpdateParams(obs_data_t *settings)
{
device_id = obs_data_get_string(settings, OPT_DEVICE_ID);
useDeviceTiming = obs_data_get_bool(settings, OPT_USE_DEVICE_TIMING);
isDefaultDevice = _strcmpi(device_id.c_str(), "default") == 0;
WASAPISource::UpdateParams params;
params.device_id = obs_data_get_string(settings, OPT_DEVICE_ID);
params.useDeviceTiming =
obs_data_get_bool(settings, OPT_USE_DEVICE_TIMING);
params.isDefaultDevice =
_strcmpi(params.device_id.c_str(), "default") == 0;
params.priority =
(window_priority)obs_data_get_int(settings, "priority");
params.window_class.clear();
params.title.clear();
params.executable.clear();
if (sourceType != SourceType::Input) {
const char *const window =
obs_data_get_string(settings, OPT_WINDOW);
char *window_class = nullptr;
char *title = nullptr;
char *executable = nullptr;
ms_build_window_strings(window, &window_class, &title,
&executable);
if (window_class) {
params.window_class = window_class;
bfree(window_class);
}
if (title) {
params.title = title;
bfree(title);
}
if (executable) {
params.executable = executable;
bfree(executable);
}
}
blog(LOG_INFO,
"[win-wasapi: '%s'] update settings:\n"
"\tdevice id: %s\n"
"\tuse device timing: %d",
obs_source_get_name(source), device_id.c_str(),
(int)useDeviceTiming);
return params;
}
void WASAPISource::UpdateSettings(UpdateParams &&params)
{
device_id = std::move(params.device_id);
useDeviceTiming = params.useDeviceTiming;
isDefaultDevice = params.isDefaultDevice;
priority = params.priority;
window_class = std::move(params.window_class);
title = std::move(params.title);
executable = std::move(params.executable);
}
void WASAPISource::LogSettings()
{
if (sourceType == SourceType::ProcessOutput) {
blog(LOG_INFO,
"[win-wasapi: '%s'] update settings:\n"
"\texecutable: %s\n"
"\ttitle: %s\n"
"\tclass: %s\n"
"\tpriority: %d",
obs_source_get_name(source), executable.c_str(),
title.c_str(), window_class.c_str(), (int)priority);
} else {
blog(LOG_INFO,
"[win-wasapi: '%s'] update settings:\n"
"\tdevice id: %s\n"
"\tuse device timing: %d",
obs_source_get_name(source), device_id.c_str(),
(int)useDeviceTiming);
}
}
void WASAPISource::Update(obs_data_t *settings)
{
const string newDevice = obs_data_get_string(settings, OPT_DEVICE_ID);
const bool restart = newDevice.compare(device_id) != 0;
UpdateParams params = BuildUpdateParams(settings);
UpdateSettings(settings);
const bool restart =
(sourceType == SourceType::ProcessOutput)
? ((priority != params.priority) ||
(window_class != params.window_class) ||
(title != params.title) ||
(executable != params.executable))
: (device_id.compare(params.device_id) != 0);
UpdateSettings(std::move(params));
LogSettings();
if (restart)
SetEvent(restartSignal);
}
void WASAPISource::OnWindowChanged(obs_data_t *settings)
{
UpdateParams params = BuildUpdateParams(settings);
const bool restart =
(sourceType == SourceType::ProcessOutput)
? ((priority != params.priority) ||
(window_class != params.window_class) ||
(title != params.title) ||
(executable != params.executable))
: (device_id.compare(params.device_id) != 0);
UpdateSettings(std::move(params));
if (restart)
SetEvent(restartSignal);
@ -479,16 +659,16 @@ void WASAPISource::Update(obs_data_t *settings)
ComPtr<IMMDevice> WASAPISource::InitDevice(IMMDeviceEnumerator *enumerator,
bool isDefaultDevice,
bool isInputDevice,
SourceType type,
const string device_id)
{
ComPtr<IMMDevice> device;
if (isDefaultDevice) {
const bool input = type == SourceType::Input;
HRESULT res = enumerator->GetDefaultAudioEndpoint(
isInputDevice ? eCapture : eRender,
isInputDevice ? eCommunications : eConsole,
device.Assign());
input ? eCapture : eRender,
input ? eCommunications : eConsole, device.Assign());
if (FAILED(res))
throw HRError("Failed GetDefaultAudioEndpoint", res);
} else {
@ -511,31 +691,119 @@ ComPtr<IMMDevice> WASAPISource::InitDevice(IMMDeviceEnumerator *enumerator,
#define BUFFER_TIME_100NS (5 * 10000000)
ComPtr<IAudioClient> WASAPISource::InitClient(IMMDevice *device,
bool isInputDevice,
enum speaker_layout &speakers,
enum audio_format &format,
uint32_t &sampleRate)
static DWORD GetSpeakerChannelMask(speaker_layout layout)
{
ComPtr<IAudioClient> client;
HRESULT res = device->Activate(__uuidof(IAudioClient), CLSCTX_ALL,
nullptr, (void **)client.Assign());
if (FAILED(res))
throw HRError("Failed to activate client context", res);
switch (layout) {
case SPEAKERS_STEREO:
return KSAUDIO_SPEAKER_STEREO;
case SPEAKERS_2POINT1:
return KSAUDIO_SPEAKER_2POINT1;
case SPEAKERS_4POINT0:
return KSAUDIO_SPEAKER_SURROUND;
case SPEAKERS_4POINT1:
return OBS_KSAUDIO_SPEAKER_4POINT1;
case SPEAKERS_5POINT1:
return KSAUDIO_SPEAKER_5POINT1_SURROUND;
case SPEAKERS_7POINT1:
return KSAUDIO_SPEAKER_7POINT1_SURROUND;
}
return (DWORD)layout;
}
ComPtr<IAudioClient> WASAPISource::InitClient(
IMMDevice *device, SourceType type, DWORD process_id,
PFN_ActivateAudioInterfaceAsync activate_audio_interface_async,
speaker_layout &speakers, audio_format &format,
uint32_t &samples_per_sec)
{
WAVEFORMATEXTENSIBLE wfextensible;
CoTaskMemPtr<WAVEFORMATEX> wfex;
res = client->GetMixFormat(&wfex);
if (FAILED(res))
throw HRError("Failed to get mix format", res);
const WAVEFORMATEX *pFormat;
HRESULT res;
ComPtr<IAudioClient> client;
InitFormat(wfex, speakers, format, sampleRate);
if (type == SourceType::ProcessOutput) {
if (activate_audio_interface_async == NULL)
throw "ActivateAudioInterfaceAsync is not available";
struct obs_audio_info oai;
obs_get_audio_info(&oai);
const WORD nChannels = (WORD)get_audio_channels(oai.speakers);
const DWORD nSamplesPerSec = oai.samples_per_sec;
constexpr WORD wBitsPerSample = 32;
const WORD nBlockAlign = nChannels * wBitsPerSample / 8;
WAVEFORMATEX &wf = wfextensible.Format;
wf.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
wf.nChannels = nChannels;
wf.nSamplesPerSec = nSamplesPerSec;
wf.nAvgBytesPerSec = nSamplesPerSec * nBlockAlign;
wf.nBlockAlign = nBlockAlign;
wf.wBitsPerSample = wBitsPerSample;
wf.cbSize = sizeof(wfextensible) - sizeof(format);
wfextensible.Samples.wValidBitsPerSample = wBitsPerSample;
wfextensible.dwChannelMask =
GetSpeakerChannelMask(oai.speakers);
wfextensible.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
AUDIOCLIENT_ACTIVATION_PARAMS audioclientActivationParams;
audioclientActivationParams.ActivationType =
AUDIOCLIENT_ACTIVATION_TYPE_PROCESS_LOOPBACK;
audioclientActivationParams.ProcessLoopbackParams
.TargetProcessId = process_id;
audioclientActivationParams.ProcessLoopbackParams
.ProcessLoopbackMode =
PROCESS_LOOPBACK_MODE_INCLUDE_TARGET_PROCESS_TREE;
PROPVARIANT activateParams{};
activateParams.vt = VT_BLOB;
activateParams.blob.cbSize =
sizeof(audioclientActivationParams);
activateParams.blob.pBlobData =
reinterpret_cast<BYTE *>(&audioclientActivationParams);
{
Microsoft::WRL::ComPtr<
WASAPIActivateAudioInterfaceCompletionHandler>
handler = Microsoft::WRL::Make<
WASAPIActivateAudioInterfaceCompletionHandler>();
ComPtr<IActivateAudioInterfaceAsyncOperation> asyncOp;
res = activate_audio_interface_async(
VIRTUAL_AUDIO_DEVICE_PROCESS_LOOPBACK,
__uuidof(IAudioClient), &activateParams,
handler.Get(), &asyncOp);
if (FAILED(res))
throw HRError(
"Failed to get activate audio client",
res);
res = handler->GetActivateResult(client.Assign());
if (FAILED(res))
throw HRError("Async activation failed", res);
}
pFormat = &wf;
} else {
res = device->Activate(__uuidof(IAudioClient), CLSCTX_ALL,
nullptr, (void **)client.Assign());
if (FAILED(res))
throw HRError("Failed to activate client context", res);
res = client->GetMixFormat(&wfex);
if (FAILED(res))
throw HRError("Failed to get mix format", res);
pFormat = wfex.Get();
}
InitFormat(pFormat, speakers, format, samples_per_sec);
DWORD flags = AUDCLNT_STREAMFLAGS_EVENTCALLBACK;
if (!isInputDevice)
if (type != SourceType::Input)
flags |= AUDCLNT_STREAMFLAGS_LOOPBACK;
res = client->Initialize(AUDCLNT_SHAREMODE_SHARED, flags,
BUFFER_TIME_100NS, 0, wfex, nullptr);
BUFFER_TIME_100NS, 0, pFormat, nullptr);
if (FAILED(res))
throw HRError("Failed to initialize audio client", res);
@ -642,16 +910,36 @@ ComPtr<IAudioCaptureClient> WASAPISource::InitCapture(IAudioClient *client,
void WASAPISource::Initialize()
{
ComPtr<IMMDevice> device = InitDevice(enumerator, isDefaultDevice,
isInputDevice, device_id);
ComPtr<IMMDevice> device;
if (sourceType == SourceType::ProcessOutput) {
device_name = "[VIRTUAL_AUDIO_DEVICE_PROCESS_LOOPBACK]";
device_name = GetDeviceName(device);
hwnd = ms_find_window(INCLUDE_MINIMIZED, priority,
window_class.c_str(), title.c_str(),
executable.c_str());
if (!hwnd)
throw "Failed to find window";
DWORD dwProcessId = 0;
if (!GetWindowThreadProcessId(hwnd, &dwProcessId)) {
hwnd = NULL;
throw "Failed to get process id of window";
}
process_id = dwProcessId;
} else {
device = InitDevice(enumerator, isDefaultDevice, sourceType,
device_id);
device_name = GetDeviceName(device);
}
ResetEvent(receiveSignal);
ComPtr<IAudioClient> temp_client =
InitClient(device, isInputDevice, speakers, format, sampleRate);
if (!isInputDevice)
ComPtr<IAudioClient> temp_client = InitClient(
device, sourceType, process_id, activate_audio_interface_async,
speakers, format, sampleRate);
if (sourceType == SourceType::DeviceOutput)
ClearBuffer(device);
ComPtr<IAudioCaptureClient> temp_capture =
InitCapture(temp_client, receiveSignal);
@ -752,6 +1040,13 @@ bool WASAPISource::ProcessCaptureData()
UINT captureSize = 0;
while (true) {
if ((sourceType == SourceType::ProcessOutput) &&
!IsWindow(hwnd)) {
blog(LOG_WARNING,
"[WASAPISource::ProcessCaptureData] window disappeared");
return false;
}
res = capture->GetNextPacketSize(&captureSize);
if (FAILED(res)) {
if (res != AUDCLNT_E_DEVICE_INVALIDATED)
@ -783,11 +1078,20 @@ bool WASAPISource::ProcessCaptureData()
data.speakers = speakers;
data.samples_per_sec = sampleRate;
data.format = format;
data.timestamp = useDeviceTiming ? ts * 100 : os_gettime_ns();
if (sourceType == SourceType::ProcessOutput) {
data.timestamp = util_mul_div64(framesProcessed,
UINT64_C(1000000000),
sampleRate);
framesProcessed += frames;
} else {
data.timestamp = useDeviceTiming ? ts * 100
: os_gettime_ns();
if (!useDeviceTiming)
data.timestamp -= util_mul_div64(frames, 1000000000ULL,
sampleRate);
if (!useDeviceTiming)
data.timestamp -= util_mul_div64(
frames, UINT64_C(1000000000),
sampleRate);
}
obs_source_output_audio(source, &data);
@ -840,10 +1144,11 @@ DWORD WINAPI WASAPISource::CaptureThread(LPVOID param)
bool reconnect = false;
do {
/* Windows 7 does not seem to wake up for LOOPBACK */
const DWORD dwMilliseconds = ((sigs == active_sigs) &&
!source->isInputDevice)
? 10
: INFINITE;
const DWORD dwMilliseconds =
((sigs == active_sigs) &&
(source->sourceType != SourceType::Input))
? 10
: INFINITE;
const DWORD ret = WaitForMultipleObjects(
sig_count, sigs, false, dwMilliseconds);
@ -941,8 +1246,9 @@ void WASAPISource::SetDefaultDevice(EDataFlow flow, ERole role, LPCWSTR id)
if (!isDefaultDevice)
return;
const EDataFlow expectedFlow = isInputDevice ? eCapture : eRender;
const ERole expectedRole = isInputDevice ? eCommunications : eConsole;
const bool input = sourceType == SourceType::Input;
const EDataFlow expectedFlow = input ? eCapture : eRender;
const ERole expectedRole = input ? eCommunications : eConsole;
if (flow != expectedFlow || role != expectedRole)
return;
@ -957,7 +1263,7 @@ void WASAPISource::SetDefaultDevice(EDataFlow flow, ERole role, LPCWSTR id)
}
blog(LOG_INFO, "WASAPI: Default %s device changed",
isInputDevice ? "input" : "output");
input ? "input" : "output");
SetEvent(restartSignal);
}
@ -1050,28 +1356,35 @@ static const char *GetWASAPIInputName(void *)
return obs_module_text("AudioInput");
}
static const char *GetWASAPIOutputName(void *)
static const char *GetWASAPIDeviceOutputName(void *)
{
return obs_module_text("AudioOutput");
}
static const char *GetWASAPIProcessOutputName(void *)
{
return obs_module_text("ApplicationAudioCapture");
}
static void GetWASAPIDefaultsInput(obs_data_t *settings)
{
obs_data_set_default_string(settings, OPT_DEVICE_ID, "default");
obs_data_set_default_bool(settings, OPT_USE_DEVICE_TIMING, false);
}
static void GetWASAPIDefaultsOutput(obs_data_t *settings)
static void GetWASAPIDefaultsDeviceOutput(obs_data_t *settings)
{
obs_data_set_default_string(settings, OPT_DEVICE_ID, "default");
obs_data_set_default_bool(settings, OPT_USE_DEVICE_TIMING, true);
}
static void GetWASAPIDefaultsProcessOutput(obs_data_t *) {}
static void *CreateWASAPISource(obs_data_t *settings, obs_source_t *source,
bool input)
SourceType type)
{
try {
return new WASAPISource(settings, source, input);
return new WASAPISource(settings, source, type);
} catch (const char *error) {
blog(LOG_ERROR, "[CreateWASAPISource] %s", error);
}
@ -1081,12 +1394,19 @@ static void *CreateWASAPISource(obs_data_t *settings, obs_source_t *source,
static void *CreateWASAPIInput(obs_data_t *settings, obs_source_t *source)
{
return CreateWASAPISource(settings, source, true);
return CreateWASAPISource(settings, source, SourceType::Input);
}
static void *CreateWASAPIOutput(obs_data_t *settings, obs_source_t *source)
static void *CreateWASAPIDeviceOutput(obs_data_t *settings,
obs_source_t *source)
{
return CreateWASAPISource(settings, source, false);
return CreateWASAPISource(settings, source, SourceType::DeviceOutput);
}
static void *CreateWASAPIProcessOutput(obs_data_t *settings,
obs_source_t *source)
{
return CreateWASAPISource(settings, source, SourceType::ProcessOutput);
}
static void DestroyWASAPISource(void *obj)
@ -1099,7 +1419,19 @@ static void UpdateWASAPISource(void *obj, obs_data_t *settings)
static_cast<WASAPISource *>(obj)->Update(settings);
}
static obs_properties_t *GetWASAPIProperties(bool input)
static bool UpdateWASAPIMethod(obs_properties_t *props, obs_property_t *,
obs_data_t *settings)
{
WASAPISource *source = (WASAPISource *)obs_properties_get_param(props);
if (!source)
return false;
source->Update(settings);
return true;
}
static obs_properties_t *GetWASAPIPropertiesInput(void *)
{
obs_properties_t *props = obs_properties_create();
vector<AudioDeviceInfo> devices;
@ -1108,7 +1440,7 @@ static obs_properties_t *GetWASAPIProperties(bool input)
props, OPT_DEVICE_ID, obs_module_text("Device"),
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
GetWASAPIAudioDevices(devices, input);
GetWASAPIAudioDevices(devices, true);
if (devices.size())
obs_property_list_add_string(
@ -1126,14 +1458,71 @@ static obs_properties_t *GetWASAPIProperties(bool input)
return props;
}
static obs_properties_t *GetWASAPIPropertiesInput(void *)
static obs_properties_t *GetWASAPIPropertiesDeviceOutput(void *)
{
return GetWASAPIProperties(true);
obs_properties_t *props = obs_properties_create();
vector<AudioDeviceInfo> devices;
obs_property_t *device_prop = obs_properties_add_list(
props, OPT_DEVICE_ID, obs_module_text("Device"),
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
GetWASAPIAudioDevices(devices, false);
if (devices.size())
obs_property_list_add_string(
device_prop, obs_module_text("Default"), "default");
for (size_t i = 0; i < devices.size(); i++) {
AudioDeviceInfo &device = devices[i];
obs_property_list_add_string(device_prop, device.name.c_str(),
device.id.c_str());
}
obs_properties_add_bool(props, OPT_USE_DEVICE_TIMING,
obs_module_text("UseDeviceTiming"));
return props;
}
static obs_properties_t *GetWASAPIPropertiesOutput(void *)
static bool wasapi_window_changed(obs_properties_t *props, obs_property_t *p,
obs_data_t *settings)
{
return GetWASAPIProperties(false);
WASAPISource *source = (WASAPISource *)obs_properties_get_param(props);
if (!source)
return false;
source->OnWindowChanged(settings);
ms_check_window_property_setting(props, p, settings, "window", 0);
return true;
}
static obs_properties_t *GetWASAPIPropertiesProcessOutput(void *data)
{
obs_properties_t *props = obs_properties_create();
obs_properties_set_param(props, data, NULL);
obs_property_t *const window_prop = obs_properties_add_list(
props, OPT_WINDOW, obs_module_text("Window"),
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
ms_fill_window_list(window_prop, INCLUDE_MINIMIZED, nullptr);
obs_property_set_modified_callback(window_prop, wasapi_window_changed);
obs_property_t *const priority_prop = obs_properties_add_list(
props, OPT_PRIORITY, obs_module_text("Priority"),
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
obs_property_list_add_int(priority_prop,
obs_module_text("Priority.Title"),
WINDOW_PRIORITY_TITLE);
obs_property_list_add_int(priority_prop,
obs_module_text("Priority.Class"),
WINDOW_PRIORITY_CLASS);
obs_property_list_add_int(priority_prop,
obs_module_text("Priority.Exe"),
WINDOW_PRIORITY_EXE);
return props;
}
void RegisterWASAPIInput()
@ -1152,19 +1541,36 @@ void RegisterWASAPIInput()
obs_register_source(&info);
}
void RegisterWASAPIOutput()
void RegisterWASAPIDeviceOutput()
{
obs_source_info info = {};
info.id = "wasapi_output_capture";
info.type = OBS_SOURCE_TYPE_INPUT;
info.output_flags = OBS_SOURCE_AUDIO | OBS_SOURCE_DO_NOT_DUPLICATE |
OBS_SOURCE_DO_NOT_SELF_MONITOR;
info.get_name = GetWASAPIOutputName;
info.create = CreateWASAPIOutput;
info.get_name = GetWASAPIDeviceOutputName;
info.create = CreateWASAPIDeviceOutput;
info.destroy = DestroyWASAPISource;
info.update = UpdateWASAPISource;
info.get_defaults = GetWASAPIDefaultsOutput;
info.get_properties = GetWASAPIPropertiesOutput;
info.get_defaults = GetWASAPIDefaultsDeviceOutput;
info.get_properties = GetWASAPIPropertiesDeviceOutput;
info.icon_type = OBS_ICON_TYPE_AUDIO_OUTPUT;
obs_register_source(&info);
}
void RegisterWASAPIProcessOutput()
{
obs_source_info info = {};
info.id = "wasapi_process_output_capture";
info.type = OBS_SOURCE_TYPE_INPUT;
info.output_flags = OBS_SOURCE_AUDIO | OBS_SOURCE_DO_NOT_DUPLICATE |
OBS_SOURCE_DO_NOT_SELF_MONITOR;
info.get_name = GetWASAPIProcessOutputName;
info.create = CreateWASAPIProcessOutput;
info.destroy = DestroyWASAPISource;
info.update = UpdateWASAPISource;
info.get_defaults = GetWASAPIDefaultsProcessOutput;
info.get_properties = GetWASAPIPropertiesProcessOutput;
info.icon_type = OBS_ICON_TYPE_PROCESS_AUDIO_OUTPUT;
obs_register_source(&info);
}