yolobs-studio/plugins/mac-syphon/syphon.m

1248 lines
31 KiB
Mathematica
Raw Normal View History

2016-02-23 23:16:51 +00:00
#import <Cocoa/Cocoa.h>
#import <ScriptingBridge/ScriptingBridge.h>
#import "syphon-framework/Syphon.h"
#include <obs-module.h>
2019-09-22 21:19:10 +00:00
#define LOG(level, message, ...) \
blog(level, "%s: " message, obs_source_get_name(s->source), \
##__VA_ARGS__)
2016-02-23 23:16:51 +00:00
struct syphon {
SYPHON_CLIENT_UNIQUE_CLASS_NAME *client;
IOSurfaceRef ref;
gs_samplerstate_t *sampler;
2019-09-22 21:19:10 +00:00
gs_effect_t *effect;
gs_vertbuffer_t *vertbuffer;
gs_texture_t *tex;
uint32_t width, height;
bool crop;
CGRect crop_rect;
bool allow_transparency;
2016-02-23 23:16:51 +00:00
obs_source_t *source;
bool active;
bool uuid_changed;
2019-09-22 21:19:10 +00:00
id new_server_listener;
id retire_listener;
2016-02-23 23:16:51 +00:00
NSString *app_name;
NSString *name;
NSString *uuid;
obs_data_t *inject_info;
2019-09-22 21:19:10 +00:00
NSString *inject_app;
NSString *inject_uuid;
bool inject_active;
id launch_listener;
bool inject_server_found;
float inject_wait_time;
2016-02-23 23:16:51 +00:00
};
typedef struct syphon *syphon_t;
static inline void update_properties(syphon_t s)
{
obs_source_update_properties(s->source);
}
static inline void find_and_inject_target(syphon_t s, NSArray *arr, bool retry);
@interface OBSSyphonKVObserver : NSObject
- (void)observeValueForKeyPath:(NSString *)keyPath
2019-09-22 21:19:10 +00:00
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context;
2016-02-23 23:16:51 +00:00
@end
static inline void handle_application_launch(syphon_t s, NSArray *new)
{
if (!s->inject_active)
return;
if (!new)
return;
find_and_inject_target(s, new, false);
}
@implementation OBSSyphonKVObserver
- (void)observeValueForKeyPath:(NSString *)keyPath
2019-09-22 21:19:10 +00:00
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
2016-02-23 23:16:51 +00:00
{
UNUSED_PARAMETER(keyPath);
UNUSED_PARAMETER(object);
syphon_t s = context;
if (!s)
return;
handle_application_launch(s, change[NSKeyValueChangeNewKey]);
update_properties(s);
}
@end
static const char *syphon_get_name(void *unused)
{
UNUSED_PARAMETER(unused);
return obs_module_text("Syphon");
}
static void stop_client(syphon_t s)
{
obs_enter_graphics();
if (s->client) {
2020-05-27 20:57:19 +00:00
[s->client stop];
2016-02-23 23:16:51 +00:00
}
if (s->tex) {
gs_texture_destroy(s->tex);
s->tex = NULL;
}
if (s->ref) {
IOSurfaceDecrementUseCount(s->ref);
CFRelease(s->ref);
s->ref = NULL;
}
2019-09-22 21:19:10 +00:00
s->width = 0;
2016-02-23 23:16:51 +00:00
s->height = 0;
obs_leave_graphics();
}
static inline NSDictionary *find_by_uuid(NSArray *arr, NSString *uuid)
{
for (NSDictionary *dict in arr) {
if ([dict[SyphonServerDescriptionUUIDKey] isEqual:uuid])
return dict;
}
return nil;
}
static inline void check_version(syphon_t s, NSDictionary *desc)
{
extern const NSString *SyphonServerDescriptionDictionaryVersionKey;
NSNumber *version = desc[SyphonServerDescriptionDictionaryVersionKey];
if (!version)
return LOG(LOG_WARNING, "Server description does not contain "
"VersionKey");
if (version.unsignedIntValue > 0)
2019-09-22 21:19:10 +00:00
LOG(LOG_WARNING,
"Got server description version %d, "
"expected 0",
version.unsignedIntValue);
2016-02-23 23:16:51 +00:00
}
static inline void check_description(syphon_t s, NSDictionary *desc)
{
extern const NSString *SyphonSurfaceType;
extern const NSString *SyphonSurfaceTypeIOSurface;
extern const NSString *SyphonServerDescriptionSurfacesKey;
NSArray *surfaces = desc[SyphonServerDescriptionSurfacesKey];
if (!surfaces)
return LOG(LOG_WARNING, "Server description does not contain "
"SyphonServerDescriptionSurfacesKey");
if (!surfaces.count)
return LOG(LOG_WARNING, "Server description contains empty "
"SyphonServerDescriptionSurfacesKey");
for (NSDictionary *surface in surfaces) {
NSString *type = surface[SyphonSurfaceType];
if (type && [type isEqual:SyphonSurfaceTypeIOSurface])
return;
}
NSString *surfaces_string = [NSString stringWithFormat:@"%@", surfaces];
2019-09-22 21:19:10 +00:00
LOG(LOG_WARNING,
"SyphonSurfaces does not contain"
"'SyphonSurfaceTypeIOSurface': %s",
surfaces_string.UTF8String);
2016-02-23 23:16:51 +00:00
}
static inline void handle_new_frame(syphon_t s,
2019-09-22 21:19:10 +00:00
SYPHON_CLIENT_UNIQUE_CLASS_NAME *client)
2016-02-23 23:16:51 +00:00
{
IOSurfaceRef ref = [client IOSurface];
if (!ref)
return;
if (ref == s->ref) {
CFRelease(ref);
return;
}
IOSurfaceIncrementUseCount(ref);
obs_enter_graphics();
if (s->ref) {
gs_texture_destroy(s->tex);
IOSurfaceDecrementUseCount(s->ref);
CFRelease(s->ref);
}
2019-09-22 21:19:10 +00:00
s->ref = ref;
s->tex = gs_texture_create_from_iosurface(s->ref);
s->width = gs_texture_get_width(s->tex);
2016-02-23 23:16:51 +00:00
s->height = gs_texture_get_height(s->tex);
obs_leave_graphics();
}
static void create_client(syphon_t s)
{
stop_client(s);
if (!s->app_name.length && !s->name.length && !s->uuid.length)
return;
SyphonServerDirectory *ssd = [SyphonServerDirectory sharedDirectory];
NSArray *servers = [ssd serversMatchingName:s->name
appName:s->app_name];
if (!servers.count)
return;
NSDictionary *desc = find_by_uuid(servers, s->uuid);
if (!desc) {
desc = servers[0];
2020-05-27 20:57:19 +00:00
if (![s->uuid isEqualToString:
desc[SyphonServerDescriptionUUIDKey]]) {
2016-02-23 23:16:51 +00:00
s->uuid_changed = true;
2020-05-27 20:57:19 +00:00
}
2016-02-23 23:16:51 +00:00
}
check_version(s, desc);
check_description(s, desc);
2020-05-27 20:57:19 +00:00
s->client = [[SYPHON_CLIENT_UNIQUE_CLASS_NAME alloc]
initWithServerDescription:desc
options:nil
newFrameHandler:^(
SYPHON_CLIENT_UNIQUE_CLASS_NAME *client) {
handle_new_frame(s, client);
}];
2016-02-23 23:16:51 +00:00
s->active = true;
}
static inline bool load_syphon_settings(syphon_t s, obs_data_t *settings)
{
NSString *app_name = @(obs_data_get_string(settings, "app_name"));
2019-09-22 21:19:10 +00:00
NSString *name = @(obs_data_get_string(settings, "name"));
bool equal_names = [app_name isEqual:s->app_name] &&
[name isEqual:s->name];
2016-02-23 23:16:51 +00:00
if (s->uuid_changed && equal_names)
return false;
2019-09-22 21:19:10 +00:00
NSString *uuid = @(obs_data_get_string(settings, "uuid"));
2016-02-23 23:16:51 +00:00
if ([uuid isEqual:s->uuid] && equal_names)
return false;
2020-05-27 20:57:19 +00:00
s->app_name = app_name;
s->name = name;
s->uuid = uuid;
2016-02-23 23:16:51 +00:00
s->uuid_changed = false;
return true;
}
static inline void update_from_announce(syphon_t s, NSDictionary *info)
{
if (s->active)
return;
if (!info)
return;
NSString *app_name = info[SyphonServerDescriptionAppNameKey];
2019-09-22 21:19:10 +00:00
NSString *name = info[SyphonServerDescriptionNameKey];
NSString *uuid = info[SyphonServerDescriptionUUIDKey];
2016-02-23 23:16:51 +00:00
if (![uuid isEqual:s->uuid] &&
2019-09-22 21:19:10 +00:00
!([app_name isEqual:s->app_name] && [name isEqual:s->name]))
2016-02-23 23:16:51 +00:00
return;
2020-05-27 20:57:19 +00:00
s->app_name = app_name;
s->name = name;
if (![s->uuid isEqualToString:uuid]) {
s->uuid = uuid;
2016-02-23 23:16:51 +00:00
s->uuid_changed = true;
2020-05-27 20:57:19 +00:00
}
2016-02-23 23:16:51 +00:00
create_client(s);
}
static inline void update_inject_state(syphon_t s, NSDictionary *info,
2019-09-22 21:19:10 +00:00
bool announce)
2016-02-23 23:16:51 +00:00
{
if (!info)
return;
NSString *app_name = info[SyphonServerDescriptionAppNameKey];
2019-09-22 21:19:10 +00:00
NSString *name = info[SyphonServerDescriptionNameKey];
NSString *uuid = info[SyphonServerDescriptionUUIDKey];
2016-02-23 23:16:51 +00:00
if (![uuid isEqual:s->inject_uuid] &&
2019-09-22 21:19:10 +00:00
(![app_name isEqual:s->inject_app] ||
![name isEqual:@"InjectedSyphon"]))
2016-02-23 23:16:51 +00:00
return;
if (!(s->inject_server_found = announce)) {
s->inject_wait_time = 0.f;
2019-09-22 21:19:10 +00:00
LOG(LOG_INFO,
"Injected server retired: "
"[%s] InjectedSyphon (%s)",
s->inject_app.UTF8String, uuid.UTF8String);
2016-02-23 23:16:51 +00:00
return;
}
if (s->inject_uuid) //TODO: track multiple injected instances?
return;
2020-05-27 20:57:19 +00:00
s->inject_uuid = uuid;
2016-02-23 23:16:51 +00:00
LOG(LOG_INFO, "Injected server found: [%s] %s (%s)",
2019-09-22 21:19:10 +00:00
app_name.UTF8String, name.UTF8String, uuid.UTF8String);
2016-02-23 23:16:51 +00:00
}
static inline void handle_announce(syphon_t s, NSNotification *note)
{
if (!note)
return;
update_from_announce(s, note.object);
update_inject_state(s, note.object, true);
update_properties(s);
}
static inline void update_from_retire(syphon_t s, NSDictionary *info)
{
if (!info)
return;
NSString *uuid = info[SyphonServerDescriptionUUIDKey];
if (!uuid)
return;
if (![uuid isEqual:s->uuid])
return;
s->active = false;
}
static inline void handle_retire(syphon_t s, NSNotification *note)
{
if (!note)
return;
update_from_retire(s, note.object);
update_inject_state(s, note.object, false);
update_properties(s);
}
static inline gs_vertbuffer_t *create_vertbuffer()
{
struct gs_vb_data *vb_data = gs_vbdata_create();
vb_data->num = 4;
vb_data->points = bzalloc(sizeof(struct vec3) * 4);
if (!vb_data->points)
return NULL;
vb_data->num_tex = 1;
vb_data->tvarray = bzalloc(sizeof(struct gs_tvertarray));
if (!vb_data->tvarray)
goto fail_tvarray;
vb_data->tvarray[0].width = 2;
vb_data->tvarray[0].array = bzalloc(sizeof(struct vec2) * 4);
if (!vb_data->tvarray[0].array)
goto fail_array;
gs_vertbuffer_t *vbuff = gs_vertexbuffer_create(vb_data, GS_DYNAMIC);
if (vbuff)
return vbuff;
bfree(vb_data->tvarray[0].array);
fail_array:
bfree(vb_data->tvarray);
fail_tvarray:
bfree(vb_data->points);
return NULL;
}
static inline bool init_obs_graphics_objects(syphon_t s)
{
struct gs_sampler_info info = {
2019-09-22 21:19:10 +00:00
.filter = GS_FILTER_LINEAR,
.address_u = GS_ADDRESS_CLAMP,
.address_v = GS_ADDRESS_CLAMP,
.address_w = GS_ADDRESS_CLAMP,
2016-02-23 23:16:51 +00:00
.max_anisotropy = 1,
};
obs_enter_graphics();
2019-09-22 21:19:10 +00:00
s->sampler = gs_samplerstate_create(&info);
2016-02-23 23:16:51 +00:00
s->vertbuffer = create_vertbuffer();
obs_leave_graphics();
s->effect = obs_get_base_effect(OBS_EFFECT_DEFAULT_RECT);
return s->sampler != NULL && s->vertbuffer != NULL && s->effect != NULL;
}
static inline bool create_syphon_listeners(syphon_t s)
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
2019-09-22 21:19:10 +00:00
s->new_server_listener = [nc
2016-02-23 23:16:51 +00:00
addObserverForName:SyphonServerAnnounceNotification
object:nil
queue:[NSOperationQueue mainQueue]
2019-09-22 21:19:10 +00:00
usingBlock:^(NSNotification *note) {
handle_announce(s, note);
}];
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
s->retire_listener = [nc
2016-02-23 23:16:51 +00:00
addObserverForName:SyphonServerRetireNotification
object:nil
queue:[NSOperationQueue mainQueue]
2019-09-22 21:19:10 +00:00
usingBlock:^(NSNotification *note) {
handle_retire(s, note);
}];
2016-02-23 23:16:51 +00:00
return s->new_server_listener != nil && s->retire_listener != nil;
}
static inline bool create_applications_observer(syphon_t s, NSWorkspace *ws)
{
s->launch_listener = [[OBSSyphonKVObserver alloc] init];
if (!s->launch_listener)
return false;
[ws addObserver:s->launch_listener
2019-09-22 21:19:10 +00:00
forKeyPath:NSStringFromSelector(@selector(runningApplications))
options:NSKeyValueObservingOptionNew
context:s];
2016-02-23 23:16:51 +00:00
return true;
}
static inline void load_crop(syphon_t s, obs_data_t *settings)
{
s->crop = obs_data_get_bool(settings, "crop");
2019-09-22 21:19:10 +00:00
#define LOAD_CROP(x) s->crop_rect.x = obs_data_get_double(settings, "crop." #x)
2016-02-23 23:16:51 +00:00
LOAD_CROP(origin.x);
LOAD_CROP(origin.y);
LOAD_CROP(size.width);
LOAD_CROP(size.height);
#undef LOAD_CROP
}
static inline void syphon_destroy_internal(syphon_t s);
static void *syphon_create_internal(obs_data_t *settings, obs_source_t *source)
{
UNUSED_PARAMETER(source);
syphon_t s = bzalloc(sizeof(struct syphon));
if (!s)
return s;
2019-09-22 21:19:10 +00:00
s->source = source;
2016-02-23 23:16:51 +00:00
2020-05-27 20:57:19 +00:00
if (!init_obs_graphics_objects(s)) {
syphon_destroy_internal(s);
return NULL;
}
2016-02-23 23:16:51 +00:00
2020-05-27 20:57:19 +00:00
if (!load_syphon_settings(s, settings)) {
syphon_destroy_internal(s);
return NULL;
}
2016-02-23 23:16:51 +00:00
const char *inject_info = obs_data_get_string(settings, "application");
2019-09-22 21:19:10 +00:00
s->inject_info = obs_data_create_from_json(inject_info);
s->inject_active = obs_data_get_bool(settings, "inject");
s->inject_app = @(obs_data_get_string(s->inject_info, "name"));
2016-02-23 23:16:51 +00:00
2020-05-27 20:57:19 +00:00
if (!create_syphon_listeners(s)) {
syphon_destroy_internal(s);
return NULL;
}
2016-02-23 23:16:51 +00:00
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
2020-05-27 20:57:19 +00:00
if (!create_applications_observer(s, ws)) {
syphon_destroy_internal(s);
return NULL;
}
2016-02-23 23:16:51 +00:00
if (s->inject_active)
find_and_inject_target(s, ws.runningApplications, false);
create_client(s);
load_crop(s, settings);
2019-09-22 21:19:10 +00:00
s->allow_transparency =
obs_data_get_bool(settings, "allow_transparency");
2016-02-23 23:16:51 +00:00
return s;
}
static void *syphon_create(obs_data_t *settings, obs_source_t *source)
{
@autoreleasepool {
return syphon_create_internal(settings, source);
}
}
static inline void stop_listener(id listener)
{
if (!listener)
return;
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:listener];
}
static inline void syphon_destroy_internal(syphon_t s)
{
stop_listener(s->new_server_listener);
stop_listener(s->retire_listener);
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
[ws removeObserver:s->launch_listener
2019-09-22 21:19:10 +00:00
forKeyPath:NSStringFromSelector(@selector
(runningApplications))];
2016-02-23 23:16:51 +00:00
obs_data_release(s->inject_info);
obs_enter_graphics();
stop_client(s);
2019-09-22 21:19:10 +00:00
2016-02-23 23:16:51 +00:00
if (s->sampler)
gs_samplerstate_destroy(s->sampler);
if (s->vertbuffer)
gs_vertexbuffer_destroy(s->vertbuffer);
obs_leave_graphics();
bfree(s);
}
static void syphon_destroy(void *data)
{
@autoreleasepool {
syphon_destroy_internal(data);
}
}
static inline NSString *get_string(obs_data_t *settings, const char *name)
{
if (!settings)
return nil;
return @(obs_data_get_string(settings, name));
}
static inline void update_strings_from_context(syphon_t s, obs_data_t *settings,
2019-09-22 21:19:10 +00:00
NSString **app, NSString **name,
NSString **uuid)
2016-02-23 23:16:51 +00:00
{
if (!s || !s->uuid_changed)
return;
s->uuid_changed = false;
2019-09-22 21:19:10 +00:00
*app = s->app_name;
2016-02-23 23:16:51 +00:00
*name = s->name;
*uuid = s->uuid;
obs_data_set_string(settings, "app_name", s->app_name.UTF8String);
2019-09-22 21:19:10 +00:00
obs_data_set_string(settings, "name", s->name.UTF8String);
obs_data_set_string(settings, "uuid", s->uuid.UTF8String);
2016-02-23 23:16:51 +00:00
}
static inline void add_servers(syphon_t s, obs_property_t *list,
2019-09-22 21:19:10 +00:00
obs_data_t *settings)
2016-02-23 23:16:51 +00:00
{
bool found_current = settings == NULL;
2019-09-22 21:19:10 +00:00
NSString *set_app = get_string(settings, "app_name");
2016-02-23 23:16:51 +00:00
NSString *set_name = get_string(settings, "name");
NSString *set_uuid = get_string(settings, "uuid");
2019-09-22 21:19:10 +00:00
update_strings_from_context(s, settings, &set_app, &set_name,
&set_uuid);
2016-02-23 23:16:51 +00:00
obs_property_list_add_string(list, "", "");
NSArray *arr = [[SyphonServerDirectory sharedDirectory] servers];
for (NSDictionary *server in arr) {
2019-09-22 21:19:10 +00:00
NSString *app = server[SyphonServerDescriptionAppNameKey];
2016-02-23 23:16:51 +00:00
NSString *name = server[SyphonServerDescriptionNameKey];
NSString *uuid = server[SyphonServerDescriptionUUIDKey];
2019-09-22 21:19:10 +00:00
NSString *serv =
[NSString stringWithFormat:@"[%@] %@", app, name];
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
obs_property_list_add_string(list, serv.UTF8String,
uuid.UTF8String);
2016-02-23 23:16:51 +00:00
if (!found_current)
found_current = [uuid isEqual:set_uuid];
}
if (found_current || !set_uuid.length || !set_app.length)
return;
2019-09-22 21:19:10 +00:00
NSString *serv =
[NSString stringWithFormat:@"[%@] %@", set_app, set_name];
size_t idx = obs_property_list_add_string(list, serv.UTF8String,
set_uuid.UTF8String);
2016-02-23 23:16:51 +00:00
obs_property_list_item_disable(list, idx, true);
}
static bool servers_changed(obs_properties_t *props, obs_property_t *list,
2019-09-22 21:19:10 +00:00
obs_data_t *settings)
2016-02-23 23:16:51 +00:00
{
@autoreleasepool {
obs_property_list_clear(list);
add_servers(obs_properties_get_param(props), list, settings);
return true;
}
}
static inline NSString *get_inject_application_path()
{
static NSString *ident = @"zakk.lol.SyphonInject";
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
return [ws absolutePathForAppBundleWithIdentifier:ident];
}
static inline bool is_inject_available_in_lib_dir(NSFileManager *fm, NSURL *url)
{
if (!url.isFileURL)
return false;
2019-09-22 21:19:10 +00:00
for (NSString *path in [fm contentsOfDirectoryAtPath:url.path
error:nil]) {
2016-02-23 23:16:51 +00:00
NSURL *bundle_url = [url URLByAppendingPathComponent:path];
2019-09-22 21:19:10 +00:00
NSBundle *bundle = [NSBundle bundleWithURL:bundle_url];
2016-02-23 23:16:51 +00:00
if (!bundle)
continue;
if ([bundle.bundleIdentifier
2019-09-22 21:19:10 +00:00
isEqual:@"zakk.lol.SASyphonInjector"])
2016-02-23 23:16:51 +00:00
return true;
}
return false;
}
static inline bool is_inject_available()
{
if (get_inject_application_path())
return true;
NSFileManager *fm = [NSFileManager defaultManager];
for (NSURL *url in [fm URLsForDirectory:NSLibraryDirectory
inDomains:NSAllDomainsMask]) {
NSURL *scripting = [url
URLByAppendingPathComponent:@"ScriptingAdditions"
isDirectory:true];
if (is_inject_available_in_lib_dir(fm, scripting))
return true;
}
return false;
}
static inline void launch_syphon_inject_internal()
{
NSString *path = get_inject_application_path();
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
if (path)
[ws launchApplication:path];
}
static bool launch_syphon_inject(obs_properties_t *props, obs_property_t *prop,
2019-09-22 21:19:10 +00:00
void *data)
2016-02-23 23:16:51 +00:00
{
UNUSED_PARAMETER(props);
UNUSED_PARAMETER(prop);
UNUSED_PARAMETER(data);
@autoreleasepool {
launch_syphon_inject_internal();
return false;
}
}
static int describes_app(obs_data_t *info, NSRunningApplication *app)
{
int score = 0;
2019-09-22 21:19:10 +00:00
if ([app.localizedName isEqual:get_string(info, "name")])
2016-02-23 23:16:51 +00:00
score += 2;
if ([app.bundleIdentifier isEqual:get_string(info, "bundle")])
score += 2;
2019-09-22 21:19:10 +00:00
if ([app.executableURL isEqual:get_string(info, "executable")])
2016-02-23 23:16:51 +00:00
score += 2;
if (score && app.processIdentifier == obs_data_get_int(info, "pid"))
score += 1;
return score;
}
static inline void app_to_data(NSRunningApplication *app, obs_data_t *app_data)
{
obs_data_set_string(app_data, "name", app.localizedName.UTF8String);
obs_data_set_string(app_data, "bundle",
2019-09-22 21:19:10 +00:00
app.bundleIdentifier.UTF8String);
2016-02-23 23:16:51 +00:00
// Until we drop 10.8, use path.fileSystemRepsentation
obs_data_set_string(app_data, "executable",
2019-09-22 21:19:10 +00:00
app.executableURL.path.fileSystemRepresentation);
2016-02-23 23:16:51 +00:00
obs_data_set_int(app_data, "pid", app.processIdentifier);
}
static inline NSDictionary *get_duplicate_names(NSArray *apps)
{
NSMutableDictionary *result =
[NSMutableDictionary dictionaryWithCapacity:apps.count];
for (NSRunningApplication *app in apps) {
if (result[app.localizedName])
result[app.localizedName] = @(true);
else
result[app.localizedName] = @(false);
}
return result;
}
static inline size_t add_app(obs_property_t *prop, NSDictionary *duplicates,
2019-09-22 21:19:10 +00:00
NSString *name, const char *bundle,
const char *json_data, bool is_duplicate,
pid_t pid)
2016-02-23 23:16:51 +00:00
{
if (!is_duplicate) {
NSNumber *val = duplicates[name];
is_duplicate = val && val.boolValue;
}
if (is_duplicate)
2019-09-22 21:19:10 +00:00
name = [NSString
stringWithFormat:@"%@ (%s: %d)", name, bundle, pid];
2016-02-23 23:16:51 +00:00
return obs_property_list_add_string(prop, name.UTF8String, json_data);
}
static void update_inject_list_internal(obs_properties_t *props,
2019-09-22 21:19:10 +00:00
obs_property_t *prop,
obs_data_t *settings)
2016-02-23 23:16:51 +00:00
{
UNUSED_PARAMETER(props);
2019-09-22 21:19:10 +00:00
const char *current_str = obs_data_get_string(settings, "application");
obs_data_t *current = obs_data_create_from_json(current_str);
NSString *current_name = @(obs_data_get_string(current, "name"));
2016-02-23 23:16:51 +00:00
bool current_found = !obs_data_has_user_value(current, "name");
obs_property_list_clear(prop);
obs_property_list_add_string(prop, "", "");
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
2019-09-22 21:19:10 +00:00
NSArray *apps = ws.runningApplications;
2016-02-23 23:16:51 +00:00
NSDictionary *duplicates = get_duplicate_names(apps);
2019-09-22 21:19:10 +00:00
NSMapTable *candidates = [NSMapTable weakToStrongObjectsMapTable];
2016-02-23 23:16:51 +00:00
obs_data_t *app_data = obs_data_create();
for (NSRunningApplication *app in apps) {
app_to_data(app, app_data);
int score = describes_app(current, app);
NSString *name = app.localizedName;
2019-09-22 21:19:10 +00:00
add_app(prop, duplicates, name, app.bundleIdentifier.UTF8String,
obs_data_get_json(app_data),
[name isEqual:current_name] && score < 4,
app.processIdentifier);
2016-02-23 23:16:51 +00:00
if (score >= 4) {
[candidates setObject:@(score) forKey:app];
current_found = true;
}
}
obs_data_release(app_data);
if (!current_found) {
size_t idx = add_app(prop, duplicates, current_name,
2019-09-22 21:19:10 +00:00
obs_data_get_string(current, "bundle"),
current_str,
duplicates[current_name] != nil,
obs_data_get_int(current, "pid"));
2016-02-23 23:16:51 +00:00
obs_property_list_item_disable(prop, idx, true);
} else if (candidates.count > 0) {
NSRunningApplication *best_match = nil;
NSNumber *best_match_score = @(0);
for (NSRunningApplication *app in candidates.keyEnumerator) {
NSNumber *score = [candidates objectForKey:app];
if ([score compare:best_match_score] ==
2019-09-22 21:19:10 +00:00
NSOrderedDescending) {
2016-02-23 23:16:51 +00:00
best_match = app;
best_match_score = score;
}
}
// Update settings in case of PID/executable updates
if (best_match_score.intValue >= 4) {
app_to_data(best_match, current);
obs_data_set_string(settings, "application",
2019-09-22 21:19:10 +00:00
obs_data_get_json(current));
2016-02-23 23:16:51 +00:00
}
}
obs_data_release(current);
}
static void toggle_inject_internal(obs_properties_t *props,
2019-09-22 21:19:10 +00:00
obs_property_t *prop, obs_data_t *settings)
2016-02-23 23:16:51 +00:00
{
bool enabled = obs_data_get_bool(settings, "inject");
obs_property_t *inject_list = obs_properties_get(props, "application");
bool inject_enabled = obs_property_enabled(prop);
obs_property_set_enabled(inject_list, enabled && inject_enabled);
}
static bool toggle_inject(obs_properties_t *props, obs_property_t *prop,
2019-09-22 21:19:10 +00:00
obs_data_t *settings)
2016-02-23 23:16:51 +00:00
{
@autoreleasepool {
toggle_inject_internal(props, prop, settings);
return true;
}
}
static bool update_inject_list(obs_properties_t *props, obs_property_t *prop,
2019-09-22 21:19:10 +00:00
obs_data_t *settings)
2016-02-23 23:16:51 +00:00
{
@autoreleasepool {
update_inject_list_internal(props, prop, settings);
return true;
}
}
static bool update_crop(obs_properties_t *props, obs_property_t *prop,
2019-09-22 21:19:10 +00:00
obs_data_t *settings)
2016-02-23 23:16:51 +00:00
{
bool enabled = obs_data_get_bool(settings, "crop");
2019-09-22 21:19:10 +00:00
#define LOAD_CROP(x) \
2016-02-23 23:16:51 +00:00
prop = obs_properties_get(props, "crop." #x); \
obs_property_set_enabled(prop, enabled);
LOAD_CROP(origin.x);
LOAD_CROP(origin.y);
LOAD_CROP(size.width);
LOAD_CROP(size.height);
#undef LOAD_CROP
return true;
}
static void show_syphon_license_internal(void)
{
char *path = obs_module_file("syphon_license.txt");
if (!path)
return;
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
[ws openFile:@(path)];
bfree(path);
}
static bool show_syphon_license(obs_properties_t *props, obs_property_t *prop,
2019-09-22 21:19:10 +00:00
void *data)
2016-02-23 23:16:51 +00:00
{
UNUSED_PARAMETER(props);
UNUSED_PARAMETER(prop);
UNUSED_PARAMETER(data);
@autoreleasepool {
show_syphon_license_internal();
return false;
}
}
static void syphon_release(void *param)
{
if (!param)
return;
obs_source_release(((syphon_t)param)->source);
}
static inline obs_properties_t *syphon_properties_internal(syphon_t s)
{
if (s)
obs_source_addref(s->source);
2019-09-22 21:19:10 +00:00
obs_properties_t *props =
obs_properties_create_param(s, syphon_release);
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
obs_property_t *list = obs_properties_add_list(
props, "uuid", obs_module_text("Source"), OBS_COMBO_TYPE_LIST,
OBS_COMBO_FORMAT_STRING);
2016-02-23 23:16:51 +00:00
obs_property_set_modified_callback(list, servers_changed);
obs_properties_add_bool(props, "allow_transparency",
2019-09-22 21:19:10 +00:00
obs_module_text("AllowTransparency"));
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
obs_property_t *launch = obs_properties_add_button(
props, "launch inject", obs_module_text("LaunchSyphonInject"),
launch_syphon_inject);
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
obs_property_t *inject = obs_properties_add_bool(
props, "inject", obs_module_text("Inject"));
2016-02-23 23:16:51 +00:00
obs_property_set_modified_callback(inject, toggle_inject);
2019-09-22 21:19:10 +00:00
obs_property_t *inject_list = obs_properties_add_list(
props, "application", obs_module_text("Application"),
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
2016-02-23 23:16:51 +00:00
obs_property_set_modified_callback(inject_list, update_inject_list);
if (!get_inject_application_path())
obs_property_set_enabled(launch, false);
if (!is_inject_available()) {
obs_property_set_enabled(inject, false);
obs_property_set_enabled(inject_list, false);
}
2019-09-22 21:19:10 +00:00
obs_property_t *crop =
obs_properties_add_bool(props, "crop", obs_module_text("Crop"));
2016-02-23 23:16:51 +00:00
obs_property_set_modified_callback(crop, update_crop);
2019-09-22 21:19:10 +00:00
#define LOAD_CROP(x) \
obs_properties_add_float(props, "crop." #x, \
obs_module_text("Crop." #x), 0., 4096.f, \
.5f);
2016-02-23 23:16:51 +00:00
LOAD_CROP(origin.x);
LOAD_CROP(origin.y);
LOAD_CROP(size.width);
LOAD_CROP(size.height);
#undef LOAD_CROP
obs_properties_add_button(props, "syphon license",
2019-09-22 21:19:10 +00:00
obs_module_text("SyphonLicense"),
show_syphon_license);
2016-02-23 23:16:51 +00:00
return props;
}
static obs_properties_t *syphon_properties(void *data)
{
@autoreleasepool {
return syphon_properties_internal(data);
}
}
static inline void syphon_save_internal(syphon_t s, obs_data_t *settings)
{
if (!s->uuid_changed)
return;
obs_data_set_string(settings, "app_name", s->app_name.UTF8String);
2019-09-22 21:19:10 +00:00
obs_data_set_string(settings, "name", s->name.UTF8String);
obs_data_set_string(settings, "uuid", s->uuid.UTF8String);
2016-02-23 23:16:51 +00:00
}
static void syphon_save(void *data, obs_data_t *settings)
{
@autoreleasepool {
syphon_save_internal(data, settings);
}
}
static inline void build_sprite(struct gs_vb_data *data, float fcx, float fcy,
2019-09-22 21:19:10 +00:00
float start_u, float end_u, float start_v,
float end_v)
2016-02-23 23:16:51 +00:00
{
struct vec2 *tvarray = data->tvarray[0].array;
2019-09-22 21:19:10 +00:00
vec3_set(data->points + 1, fcx, 0.0f, 0.0f);
vec3_set(data->points + 2, 0.0f, fcy, 0.0f);
vec3_set(data->points + 3, fcx, fcy, 0.0f);
vec2_set(tvarray, start_u, start_v);
vec2_set(tvarray + 1, end_u, start_v);
vec2_set(tvarray + 2, start_u, end_v);
vec2_set(tvarray + 3, end_u, end_v);
2016-02-23 23:16:51 +00:00
}
2019-09-22 21:19:10 +00:00
static inline void build_sprite_rect(struct gs_vb_data *data, float origin_x,
float origin_y, float end_x, float end_y)
2016-02-23 23:16:51 +00:00
{
build_sprite(data, fabs(end_x - origin_x), fabs(end_y - origin_y),
2019-09-22 21:19:10 +00:00
origin_x, end_x, origin_y, end_y);
2016-02-23 23:16:51 +00:00
}
static inline void tick_inject_state(syphon_t s, float seconds)
{
s->inject_wait_time -= seconds;
if (s->inject_wait_time > 0.f)
return;
s->inject_wait_time = 1.f;
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
find_and_inject_target(s, ws.runningApplications, true);
}
static void syphon_video_tick(void *data, float seconds)
{
UNUSED_PARAMETER(seconds);
syphon_t s = data;
if (s->inject_active && !s->inject_server_found)
tick_inject_state(s, seconds);
if (!s->tex)
return;
2019-09-22 21:19:10 +00:00
static const CGRect null_crop = {{0.f}};
2016-02-23 23:16:51 +00:00
const CGRect *crop = &null_crop;
if (s->crop)
crop = &s->crop_rect;
obs_enter_graphics();
build_sprite_rect(gs_vertexbuffer_get_data(s->vertbuffer),
2019-09-22 21:19:10 +00:00
crop->origin.x, s->height - crop->origin.y,
s->width - crop->size.width, crop->size.height);
2016-02-23 23:16:51 +00:00
obs_leave_graphics();
}
static void syphon_video_render(void *data, gs_effect_t *effect)
{
UNUSED_PARAMETER(effect);
syphon_t s = data;
if (!s->tex)
return;
bool disable_blending = !s->allow_transparency;
if (disable_blending) {
gs_enable_blending(false);
gs_enable_color(true, true, true, false);
}
gs_vertexbuffer_flush(s->vertbuffer);
gs_load_vertexbuffer(s->vertbuffer);
gs_load_indexbuffer(NULL);
gs_load_samplerstate(s->sampler, 0);
gs_technique_t *tech = gs_effect_get_technique(s->effect, "Draw");
gs_effect_set_texture(gs_effect_get_param_by_name(s->effect, "image"),
2019-09-22 21:19:10 +00:00
s->tex);
2016-02-23 23:16:51 +00:00
gs_technique_begin(tech);
gs_technique_begin_pass(tech, 0);
gs_draw(GS_TRISTRIP, 0, 4);
gs_technique_end_pass(tech);
gs_technique_end(tech);
if (disable_blending) {
gs_enable_color(true, true, true, true);
gs_enable_blending(true);
}
}
static uint32_t syphon_get_width(void *data)
{
syphon_t s = (syphon_t)data;
if (!s->crop)
return s->width;
2019-09-22 21:19:10 +00:00
int32_t width =
s->width - s->crop_rect.origin.x - s->crop_rect.size.width;
2016-02-23 23:16:51 +00:00
return MAX(0, width);
}
static uint32_t syphon_get_height(void *data)
{
syphon_t s = (syphon_t)data;
if (!s->crop)
return s->height;
2019-09-22 21:19:10 +00:00
int32_t height =
s->height - s->crop_rect.origin.y - s->crop_rect.size.height;
2016-02-23 23:16:51 +00:00
return MAX(0, height);
}
static inline void inject_app(syphon_t s, NSRunningApplication *app, bool retry)
{
SBApplication *sbapp = nil;
if (app.processIdentifier != -1)
sbapp = [SBApplication
applicationWithProcessIdentifier:app.processIdentifier];
else if (app.bundleIdentifier)
sbapp = [SBApplication
applicationWithBundleIdentifier:app.bundleIdentifier];
if (!sbapp)
return LOG(LOG_ERROR, "Could not inject %s",
2019-09-22 21:19:10 +00:00
app.localizedName.UTF8String);
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
sbapp.timeout = 10 * 60;
2016-02-23 23:16:51 +00:00
sbapp.sendMode = kAEWaitReply;
[sbapp sendEvent:'ascr' id:'gdut' parameters:0];
sbapp.sendMode = kAENoReply;
[sbapp sendEvent:'SASI' id:'injc' parameters:0];
if (retry)
return;
2019-09-22 21:19:10 +00:00
LOG(LOG_INFO, "Injected '%s' (%d, '%s')", app.localizedName.UTF8String,
app.processIdentifier, app.bundleIdentifier.UTF8String);
2016-02-23 23:16:51 +00:00
}
static inline void find_and_inject_target(syphon_t s, NSArray *arr, bool retry)
{
NSMutableArray *best_matches = [NSMutableArray arrayWithCapacity:1];
int best_score = 0;
for (NSRunningApplication *app in arr) {
int score = describes_app(s->inject_info, app);
if (!score)
continue;
if (score > best_score) {
best_score = score;
[best_matches removeAllObjects];
}
if (score >= best_score)
[best_matches addObject:app];
}
for (NSRunningApplication *app in best_matches)
inject_app(s, app, retry);
}
static inline bool inject_info_equal(obs_data_t *prev, obs_data_t *new)
{
2019-09-22 21:19:10 +00:00
if (![get_string(prev, "name") isEqual:get_string(new, "name")])
2016-02-23 23:16:51 +00:00
return false;
2019-09-22 21:19:10 +00:00
if (![get_string(prev, "bundle") isEqual:get_string(new, "bundle")])
2016-02-23 23:16:51 +00:00
return false;
if (![get_string(prev, "executable")
2019-09-22 21:19:10 +00:00
isEqual:get_string(new, "executable")])
2016-02-23 23:16:51 +00:00
return false;
2019-09-22 21:19:10 +00:00
if (![get_string(prev, "pid") isEqual:get_string(new, "pid")])
2016-02-23 23:16:51 +00:00
return false;
return true;
}
static inline void update_inject(syphon_t s, obs_data_t *settings)
{
2019-09-22 21:19:10 +00:00
bool try_injecting = s->inject_active;
s->inject_active = obs_data_get_bool(settings, "inject");
2016-02-23 23:16:51 +00:00
const char *inject_str = obs_data_get_string(settings, "application");
try_injecting = !try_injecting && s->inject_active;
obs_data_t *prev = s->inject_info;
s->inject_info = obs_data_create_from_json(inject_str);
2019-09-22 21:19:10 +00:00
2020-05-27 20:57:19 +00:00
s->inject_app = @(obs_data_get_string(s->inject_info, "name"));
2016-02-23 23:16:51 +00:00
SyphonServerDirectory *ssd = [SyphonServerDirectory sharedDirectory];
NSArray *servers = [ssd serversMatchingName:@"InjectedSyphon"
appName:s->inject_app];
s->inject_server_found = false;
for (NSDictionary *server in servers)
update_inject_state(s, server, true);
2019-09-22 21:19:10 +00:00
2016-02-23 23:16:51 +00:00
if (!try_injecting)
try_injecting = s->inject_active &&
2019-09-22 21:19:10 +00:00
!inject_info_equal(prev, s->inject_info);
2016-02-23 23:16:51 +00:00
obs_data_release(prev);
if (!try_injecting)
return;
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
find_and_inject_target(s, ws.runningApplications, false);
}
static inline bool update_syphon(syphon_t s, obs_data_t *settings)
{
NSArray *arr = [[SyphonServerDirectory sharedDirectory] servers];
if (!load_syphon_settings(s, settings))
return false;
NSDictionary *dict = find_by_uuid(arr, s->uuid);
if (dict) {
2019-09-22 21:19:10 +00:00
NSString *app = dict[SyphonServerDescriptionAppNameKey];
2016-02-23 23:16:51 +00:00
NSString *name = dict[SyphonServerDescriptionNameKey];
obs_data_set_string(settings, "app_name", app.UTF8String);
2019-09-22 21:19:10 +00:00
obs_data_set_string(settings, "name", name.UTF8String);
2016-02-23 23:16:51 +00:00
load_syphon_settings(s, settings);
} else if (!dict && !s->uuid.length) {
obs_data_set_string(settings, "app_name", "");
2019-09-22 21:19:10 +00:00
obs_data_set_string(settings, "name", "");
2016-02-23 23:16:51 +00:00
load_syphon_settings(s, settings);
}
return true;
}
static void syphon_update_internal(syphon_t s, obs_data_t *settings)
{
2019-09-22 21:19:10 +00:00
s->allow_transparency =
obs_data_get_bool(settings, "allow_transparency");
2016-02-23 23:16:51 +00:00
load_crop(s, settings);
update_inject(s, settings);
if (update_syphon(s, settings))
create_client(s);
}
static void syphon_update(void *data, obs_data_t *settings)
{
@autoreleasepool {
syphon_update_internal(data, settings);
}
}
struct obs_source_info syphon_info = {
2019-09-22 21:19:10 +00:00
.id = "syphon-input",
.type = OBS_SOURCE_TYPE_INPUT,
.output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
OBS_SOURCE_DO_NOT_DUPLICATE,
.get_name = syphon_get_name,
.create = syphon_create,
.destroy = syphon_destroy,
.video_render = syphon_video_render,
.video_tick = syphon_video_tick,
2016-02-23 23:16:51 +00:00
.get_properties = syphon_properties,
2019-09-22 21:19:10 +00:00
.get_width = syphon_get_width,
.get_height = syphon_get_height,
.update = syphon_update,
.save = syphon_save,
2020-03-25 08:07:22 +00:00
.icon_type = OBS_ICON_TYPE_GAME_CAPTURE,
2016-02-23 23:16:51 +00:00
};