2014-08-24 00:58:35 +02:00
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
#import <ScriptingBridge/ScriptingBridge.h>
|
|
|
|
#import "syphon-framework/Syphon.h"
|
|
|
|
#include <obs-module.h>
|
2022-06-20 10:16:01 +02:00
|
|
|
#include <AvailabilityMacros.h>
|
2014-08-24 00:58:35 +02:00
|
|
|
|
2019-07-09 13:29:39 -05:00
|
|
|
#define LOG(level, message, ...) \
|
|
|
|
blog(level, "%s: " message, obs_source_get_name(s->source), \
|
|
|
|
##__VA_ARGS__)
|
2014-08-24 00:58:35 +02:00
|
|
|
|
|
|
|
struct syphon {
|
|
|
|
SYPHON_CLIENT_UNIQUE_CLASS_NAME *client;
|
|
|
|
IOSurfaceRef ref;
|
|
|
|
|
|
|
|
gs_samplerstate_t *sampler;
|
2019-07-09 13:29:39 -05: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;
|
2014-08-24 00:58:35 +02:00
|
|
|
|
|
|
|
obs_source_t *source;
|
|
|
|
|
|
|
|
bool active;
|
|
|
|
bool uuid_changed;
|
2019-07-09 13:29:39 -05:00
|
|
|
id new_server_listener;
|
|
|
|
id retire_listener;
|
2014-08-24 00:58:35 +02:00
|
|
|
|
|
|
|
NSString *app_name;
|
|
|
|
NSString *name;
|
|
|
|
NSString *uuid;
|
|
|
|
};
|
|
|
|
typedef struct syphon *syphon_t;
|
|
|
|
|
|
|
|
static inline void update_properties(syphon_t s)
|
|
|
|
{
|
|
|
|
obs_source_update_properties(s->source);
|
|
|
|
}
|
|
|
|
|
|
|
|
@interface OBSSyphonKVObserver : NSObject
|
|
|
|
- (void)observeValueForKeyPath:(NSString *)keyPath
|
2019-07-09 13:29:39 -05:00
|
|
|
ofObject:(id)object
|
|
|
|
change:(NSDictionary *)change
|
|
|
|
context:(void *)context;
|
2014-08-24 00:58:35 +02:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation OBSSyphonKVObserver
|
|
|
|
- (void)observeValueForKeyPath:(NSString *)keyPath
|
2019-07-09 13:29:39 -05:00
|
|
|
ofObject:(id)object
|
|
|
|
change:(NSDictionary *)change
|
|
|
|
context:(void *)context
|
2014-08-24 00:58:35 +02:00
|
|
|
{
|
|
|
|
syphon_t s = context;
|
|
|
|
if (!s)
|
|
|
|
return;
|
|
|
|
|
2022-07-28 19:04:16 +02:00
|
|
|
if (!change[NSKeyValueChangeNewKey])
|
|
|
|
return;
|
|
|
|
|
2014-08-24 00:58:35 +02:00
|
|
|
update_properties(s);
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2022-07-28 19:04:16 +02:00
|
|
|
static const char *syphon_get_name(void *unused __attribute((unused)))
|
2014-08-24 00:58:35 +02:00
|
|
|
{
|
2015-02-10 22:28:56 -08:00
|
|
|
return obs_module_text("Syphon");
|
2014-08-24 00:58:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void stop_client(syphon_t s)
|
|
|
|
{
|
|
|
|
obs_enter_graphics();
|
|
|
|
|
|
|
|
if (s->client) {
|
2020-04-10 20:09:07 -05:00
|
|
|
[s->client stop];
|
2014-08-24 00:58:35 +02: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-07-09 13:29:39 -05:00
|
|
|
s->width = 0;
|
2014-08-24 00:58:35 +02: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-07-09 13:29:39 -05:00
|
|
|
LOG(LOG_WARNING,
|
|
|
|
"Got server description version %d, "
|
|
|
|
"expected 0",
|
|
|
|
version.unsignedIntValue);
|
2014-08-24 00:58:35 +02: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-07-09 13:29:39 -05:00
|
|
|
LOG(LOG_WARNING,
|
|
|
|
"SyphonSurfaces does not contain"
|
|
|
|
"'SyphonSurfaceTypeIOSurface': %s",
|
|
|
|
surfaces_string.UTF8String);
|
2014-08-24 00:58:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void handle_new_frame(syphon_t s,
|
2019-07-09 13:29:39 -05:00
|
|
|
SYPHON_CLIENT_UNIQUE_CLASS_NAME *client)
|
2014-08-24 00:58:35 +02: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-07-09 13:29:39 -05:00
|
|
|
s->ref = ref;
|
|
|
|
s->tex = gs_texture_create_from_iosurface(s->ref);
|
|
|
|
s->width = gs_texture_get_width(s->tex);
|
2014-08-24 00:58:35 +02: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-04-10 20:09:07 -05:00
|
|
|
if (![s->uuid isEqualToString:
|
|
|
|
desc[SyphonServerDescriptionUUIDKey]]) {
|
2014-08-24 00:58:35 +02:00
|
|
|
s->uuid_changed = true;
|
2020-04-10 20:09:07 -05:00
|
|
|
}
|
2014-08-24 00:58:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
check_version(s, desc);
|
|
|
|
check_description(s, desc);
|
|
|
|
|
2020-04-10 20:09:07 -05: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);
|
|
|
|
}];
|
2014-08-24 00:58:35 +02: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-07-09 13:29:39 -05:00
|
|
|
NSString *name = @(obs_data_get_string(settings, "name"));
|
|
|
|
bool equal_names = [app_name isEqual:s->app_name] &&
|
|
|
|
[name isEqual:s->name];
|
2014-08-24 00:58:35 +02:00
|
|
|
if (s->uuid_changed && equal_names)
|
|
|
|
return false;
|
|
|
|
|
2019-07-09 13:29:39 -05:00
|
|
|
NSString *uuid = @(obs_data_get_string(settings, "uuid"));
|
2014-08-24 00:58:35 +02:00
|
|
|
if ([uuid isEqual:s->uuid] && equal_names)
|
|
|
|
return false;
|
|
|
|
|
2020-04-10 20:09:07 -05:00
|
|
|
s->app_name = app_name;
|
|
|
|
s->name = name;
|
|
|
|
s->uuid = uuid;
|
2014-08-24 00:58:35 +02: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-07-09 13:29:39 -05:00
|
|
|
NSString *name = info[SyphonServerDescriptionNameKey];
|
|
|
|
NSString *uuid = info[SyphonServerDescriptionUUIDKey];
|
2014-08-24 00:58:35 +02:00
|
|
|
|
|
|
|
if (![uuid isEqual:s->uuid] &&
|
2019-07-09 13:29:39 -05:00
|
|
|
!([app_name isEqual:s->app_name] && [name isEqual:s->name]))
|
2014-08-24 00:58:35 +02:00
|
|
|
return;
|
|
|
|
|
2020-04-10 20:09:07 -05:00
|
|
|
s->app_name = app_name;
|
|
|
|
s->name = name;
|
|
|
|
if (![s->uuid isEqualToString:uuid]) {
|
|
|
|
s->uuid = uuid;
|
2014-08-24 00:58:35 +02:00
|
|
|
s->uuid_changed = true;
|
2020-04-10 20:09:07 -05:00
|
|
|
}
|
2014-08-24 00:58:35 +02:00
|
|
|
|
|
|
|
create_client(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void handle_announce(syphon_t s, NSNotification *note)
|
|
|
|
{
|
|
|
|
if (!note)
|
|
|
|
return;
|
|
|
|
|
|
|
|
update_from_announce(s, note.object);
|
|
|
|
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_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-07-09 13:29:39 -05:00
|
|
|
.filter = GS_FILTER_LINEAR,
|
|
|
|
.address_u = GS_ADDRESS_CLAMP,
|
|
|
|
.address_v = GS_ADDRESS_CLAMP,
|
|
|
|
.address_w = GS_ADDRESS_CLAMP,
|
2014-08-24 00:58:35 +02:00
|
|
|
.max_anisotropy = 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
obs_enter_graphics();
|
2019-07-09 13:29:39 -05:00
|
|
|
s->sampler = gs_samplerstate_create(&info);
|
2014-08-24 00:58:35 +02:00
|
|
|
s->vertbuffer = create_vertbuffer();
|
|
|
|
obs_leave_graphics();
|
|
|
|
|
2015-10-16 07:31:52 -07:00
|
|
|
s->effect = obs_get_base_effect(OBS_EFFECT_DEFAULT_RECT);
|
2014-08-24 00:58:35 +02:00
|
|
|
|
|
|
|
return s->sampler != NULL && s->vertbuffer != NULL && s->effect != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool create_syphon_listeners(syphon_t s)
|
|
|
|
{
|
|
|
|
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
|
2019-07-09 13:29:39 -05:00
|
|
|
s->new_server_listener = [nc
|
2014-08-24 00:58:35 +02:00
|
|
|
addObserverForName:SyphonServerAnnounceNotification
|
|
|
|
object:nil
|
|
|
|
queue:[NSOperationQueue mainQueue]
|
2019-07-09 13:29:39 -05:00
|
|
|
usingBlock:^(NSNotification *note) {
|
|
|
|
handle_announce(s, note);
|
|
|
|
}];
|
2014-08-24 00:58:35 +02:00
|
|
|
|
2019-07-09 13:29:39 -05:00
|
|
|
s->retire_listener = [nc
|
2014-08-24 00:58:35 +02:00
|
|
|
addObserverForName:SyphonServerRetireNotification
|
|
|
|
object:nil
|
|
|
|
queue:[NSOperationQueue mainQueue]
|
2019-07-09 13:29:39 -05:00
|
|
|
usingBlock:^(NSNotification *note) {
|
|
|
|
handle_retire(s, note);
|
|
|
|
}];
|
2014-08-24 00:58:35 +02:00
|
|
|
|
|
|
|
return s->new_server_listener != nil && s->retire_listener != nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void load_crop(syphon_t s, obs_data_t *settings)
|
|
|
|
{
|
|
|
|
s->crop = obs_data_get_bool(settings, "crop");
|
|
|
|
|
2019-07-09 13:29:39 -05:00
|
|
|
#define LOAD_CROP(x) s->crop_rect.x = obs_data_get_double(settings, "crop." #x)
|
2014-08-24 00:58:35 +02: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)
|
|
|
|
{
|
|
|
|
syphon_t s = bzalloc(sizeof(struct syphon));
|
|
|
|
if (!s)
|
|
|
|
return s;
|
|
|
|
|
2019-07-09 13:29:39 -05:00
|
|
|
s->source = source;
|
2014-08-24 00:58:35 +02:00
|
|
|
|
2020-04-10 20:09:07 -05:00
|
|
|
if (!init_obs_graphics_objects(s)) {
|
|
|
|
syphon_destroy_internal(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-08-24 00:58:35 +02:00
|
|
|
|
2020-04-10 20:09:07 -05:00
|
|
|
if (!load_syphon_settings(s, settings)) {
|
|
|
|
syphon_destroy_internal(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-08-24 00:58:35 +02:00
|
|
|
|
2020-04-10 20:09:07 -05:00
|
|
|
if (!create_syphon_listeners(s)) {
|
|
|
|
syphon_destroy_internal(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-08-24 00:58:35 +02:00
|
|
|
|
|
|
|
create_client(s);
|
|
|
|
|
|
|
|
load_crop(s, settings);
|
|
|
|
|
2019-07-09 13:29:39 -05:00
|
|
|
s->allow_transparency =
|
|
|
|
obs_data_get_bool(settings, "allow_transparency");
|
2014-10-27 00:59:44 +01:00
|
|
|
|
2014-08-24 00:58:35 +02: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];
|
|
|
|
|
|
|
|
obs_enter_graphics();
|
|
|
|
stop_client(s);
|
2019-07-09 13:29:39 -05:00
|
|
|
|
2014-08-24 00:58:35 +02: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-07-09 13:29:39 -05:00
|
|
|
NSString **app, NSString **name,
|
|
|
|
NSString **uuid)
|
2014-08-24 00:58:35 +02:00
|
|
|
{
|
|
|
|
if (!s || !s->uuid_changed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
s->uuid_changed = false;
|
2019-07-09 13:29:39 -05:00
|
|
|
*app = s->app_name;
|
2014-08-24 00:58:35 +02:00
|
|
|
*name = s->name;
|
|
|
|
*uuid = s->uuid;
|
|
|
|
|
|
|
|
obs_data_set_string(settings, "app_name", s->app_name.UTF8String);
|
2019-07-09 13:29:39 -05:00
|
|
|
obs_data_set_string(settings, "name", s->name.UTF8String);
|
|
|
|
obs_data_set_string(settings, "uuid", s->uuid.UTF8String);
|
2014-08-24 00:58:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void add_servers(syphon_t s, obs_property_t *list,
|
2019-07-09 13:29:39 -05:00
|
|
|
obs_data_t *settings)
|
2014-08-24 00:58:35 +02:00
|
|
|
{
|
|
|
|
bool found_current = settings == NULL;
|
|
|
|
|
2019-07-09 13:29:39 -05:00
|
|
|
NSString *set_app = get_string(settings, "app_name");
|
2014-08-24 00:58:35 +02:00
|
|
|
NSString *set_name = get_string(settings, "name");
|
|
|
|
NSString *set_uuid = get_string(settings, "uuid");
|
|
|
|
|
2019-07-09 13:29:39 -05:00
|
|
|
update_strings_from_context(s, settings, &set_app, &set_name,
|
|
|
|
&set_uuid);
|
2014-08-24 00:58:35 +02:00
|
|
|
|
|
|
|
obs_property_list_add_string(list, "", "");
|
|
|
|
NSArray *arr = [[SyphonServerDirectory sharedDirectory] servers];
|
|
|
|
for (NSDictionary *server in arr) {
|
2019-07-09 13:29:39 -05:00
|
|
|
NSString *app = server[SyphonServerDescriptionAppNameKey];
|
2014-08-24 00:58:35 +02:00
|
|
|
NSString *name = server[SyphonServerDescriptionNameKey];
|
|
|
|
NSString *uuid = server[SyphonServerDescriptionUUIDKey];
|
2019-07-09 13:29:39 -05:00
|
|
|
NSString *serv =
|
|
|
|
[NSString stringWithFormat:@"[%@] %@", app, name];
|
2014-08-24 00:58:35 +02:00
|
|
|
|
2019-07-09 13:29:39 -05:00
|
|
|
obs_property_list_add_string(list, serv.UTF8String,
|
|
|
|
uuid.UTF8String);
|
2014-08-24 00:58:35 +02:00
|
|
|
|
|
|
|
if (!found_current)
|
|
|
|
found_current = [uuid isEqual:set_uuid];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found_current || !set_uuid.length || !set_app.length)
|
|
|
|
return;
|
|
|
|
|
2019-07-09 13:29:39 -05:00
|
|
|
NSString *serv =
|
|
|
|
[NSString stringWithFormat:@"[%@] %@", set_app, set_name];
|
|
|
|
size_t idx = obs_property_list_add_string(list, serv.UTF8String,
|
|
|
|
set_uuid.UTF8String);
|
2014-08-24 00:58:35 +02:00
|
|
|
obs_property_list_item_disable(list, idx, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool servers_changed(obs_properties_t *props, obs_property_t *list,
|
2019-07-09 13:29:39 -05:00
|
|
|
obs_data_t *settings)
|
2014-08-24 00:58:35 +02:00
|
|
|
{
|
|
|
|
@autoreleasepool {
|
|
|
|
obs_property_list_clear(list);
|
|
|
|
add_servers(obs_properties_get_param(props), list, settings);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool update_crop(obs_properties_t *props, obs_property_t *prop,
|
2019-07-09 13:29:39 -05:00
|
|
|
obs_data_t *settings)
|
2014-08-24 00:58:35 +02:00
|
|
|
{
|
|
|
|
bool enabled = obs_data_get_bool(settings, "crop");
|
|
|
|
|
2019-07-09 13:29:39 -05:00
|
|
|
#define LOAD_CROP(x) \
|
2014-08-24 00:58:35 +02: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];
|
2022-06-20 10:16:01 +02:00
|
|
|
|
2022-07-28 19:04:16 +02:00
|
|
|
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 110000
|
2022-06-20 10:16:01 +02:00
|
|
|
if (@available(macOS 11.0, *)) {
|
|
|
|
NSURL *url = [NSURL
|
|
|
|
URLWithString:
|
2022-07-23 16:47:08 +02:00
|
|
|
[@"file://"
|
|
|
|
stringByAppendingString:
|
|
|
|
[NSString
|
|
|
|
stringWithCString:path
|
|
|
|
encoding:NSUTF8StringEncoding]]];
|
2022-06-20 10:16:01 +02:00
|
|
|
[ws openURL:url];
|
|
|
|
} else {
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
|
|
|
[ws openFile:@(path)];
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
}
|
|
|
|
#else
|
2014-08-24 00:58:35 +02:00
|
|
|
[ws openFile:@(path)];
|
2022-06-20 10:16:01 +02:00
|
|
|
#endif
|
2014-08-24 00:58:35 +02:00
|
|
|
bfree(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool show_syphon_license(obs_properties_t *props, obs_property_t *prop,
|
2019-07-09 13:29:39 -05:00
|
|
|
void *data)
|
2014-08-24 00:58:35 +02: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)
|
|
|
|
{
|
2022-01-24 11:36:41 -08:00
|
|
|
if (s && obs_source_get_ref(s->source) == NULL) {
|
|
|
|
s = NULL;
|
|
|
|
}
|
2014-08-24 00:58:35 +02:00
|
|
|
|
2019-07-09 13:29:39 -05:00
|
|
|
obs_properties_t *props =
|
|
|
|
obs_properties_create_param(s, syphon_release);
|
2014-08-24 00:58:35 +02:00
|
|
|
|
2019-07-09 13:29:39 -05:00
|
|
|
obs_property_t *list = obs_properties_add_list(
|
|
|
|
props, "uuid", obs_module_text("Source"), OBS_COMBO_TYPE_LIST,
|
|
|
|
OBS_COMBO_FORMAT_STRING);
|
2014-08-24 00:58:35 +02:00
|
|
|
obs_property_set_modified_callback(list, servers_changed);
|
|
|
|
|
2014-10-27 00:59:44 +01:00
|
|
|
obs_properties_add_bool(props, "allow_transparency",
|
2019-07-09 13:29:39 -05:00
|
|
|
obs_module_text("AllowTransparency"));
|
2014-10-27 00:59:44 +01:00
|
|
|
|
2019-07-09 13:29:39 -05:00
|
|
|
obs_property_t *crop =
|
|
|
|
obs_properties_add_bool(props, "crop", obs_module_text("Crop"));
|
2014-08-24 00:58:35 +02:00
|
|
|
obs_property_set_modified_callback(crop, update_crop);
|
|
|
|
|
2019-07-09 13:29:39 -05:00
|
|
|
#define LOAD_CROP(x) \
|
|
|
|
obs_properties_add_float(props, "crop." #x, \
|
|
|
|
obs_module_text("Crop." #x), 0., 4096.f, \
|
|
|
|
.5f);
|
2014-08-24 00:58:35 +02: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-07-09 13:29:39 -05:00
|
|
|
obs_module_text("SyphonLicense"),
|
|
|
|
show_syphon_license);
|
2014-08-24 00:58:35 +02: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-07-09 13:29:39 -05:00
|
|
|
obs_data_set_string(settings, "name", s->name.UTF8String);
|
|
|
|
obs_data_set_string(settings, "uuid", s->uuid.UTF8String);
|
2014-08-24 00:58:35 +02: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-07-09 13:29:39 -05:00
|
|
|
float start_u, float end_u, float start_v,
|
|
|
|
float end_v)
|
2014-08-24 00:58:35 +02:00
|
|
|
{
|
|
|
|
struct vec2 *tvarray = data->tvarray[0].array;
|
|
|
|
|
2019-07-09 13:29:39 -05: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);
|
2014-08-24 00:58:35 +02:00
|
|
|
}
|
|
|
|
|
2019-07-09 13:29:39 -05:00
|
|
|
static inline void build_sprite_rect(struct gs_vb_data *data, float origin_x,
|
|
|
|
float origin_y, float end_x, float end_y)
|
2014-08-24 00:58:35 +02:00
|
|
|
{
|
|
|
|
build_sprite(data, fabs(end_x - origin_x), fabs(end_y - origin_y),
|
2019-07-09 13:29:39 -05:00
|
|
|
origin_x, end_x, origin_y, end_y);
|
2014-08-24 00:58:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void syphon_video_tick(void *data, float seconds)
|
|
|
|
{
|
|
|
|
syphon_t s = data;
|
2014-10-31 05:40:52 +01:00
|
|
|
|
2014-08-24 00:58:35 +02:00
|
|
|
if (!s->tex)
|
|
|
|
return;
|
|
|
|
|
2019-07-09 13:29:39 -05:00
|
|
|
static const CGRect null_crop = {{0.f}};
|
2014-08-24 00:58:35 +02: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-07-09 13:29:39 -05:00
|
|
|
crop->origin.x, s->height - crop->origin.y,
|
|
|
|
s->width - crop->size.width, crop->size.height);
|
2014-08-24 00:58:35 +02: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;
|
|
|
|
|
|
|
|
gs_vertexbuffer_flush(s->vertbuffer);
|
|
|
|
gs_load_vertexbuffer(s->vertbuffer);
|
|
|
|
gs_load_indexbuffer(NULL);
|
|
|
|
gs_load_samplerstate(s->sampler, 0);
|
2021-06-15 00:39:55 -07:00
|
|
|
const char *tech_name = s->allow_transparency ? "Draw" : "DrawOpaque";
|
|
|
|
gs_technique_t *tech = gs_effect_get_technique(s->effect, tech_name);
|
2014-08-24 00:58:35 +02:00
|
|
|
gs_effect_set_texture(gs_effect_get_param_by_name(s->effect, "image"),
|
2019-07-09 13:29:39 -05:00
|
|
|
s->tex);
|
2014-08-24 00:58:35 +02: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t syphon_get_width(void *data)
|
|
|
|
{
|
|
|
|
syphon_t s = (syphon_t)data;
|
|
|
|
if (!s->crop)
|
|
|
|
return s->width;
|
2019-07-09 13:29:39 -05:00
|
|
|
int32_t width =
|
|
|
|
s->width - s->crop_rect.origin.x - s->crop_rect.size.width;
|
2014-08-24 00:58:35 +02: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-07-09 13:29:39 -05:00
|
|
|
int32_t height =
|
|
|
|
s->height - s->crop_rect.origin.y - s->crop_rect.size.height;
|
2014-08-24 00:58:35 +02:00
|
|
|
return MAX(0, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-09 13:29:39 -05:00
|
|
|
NSString *app = dict[SyphonServerDescriptionAppNameKey];
|
2014-08-24 00:58:35 +02:00
|
|
|
NSString *name = dict[SyphonServerDescriptionNameKey];
|
|
|
|
obs_data_set_string(settings, "app_name", app.UTF8String);
|
2019-07-09 13:29:39 -05:00
|
|
|
obs_data_set_string(settings, "name", name.UTF8String);
|
2014-08-24 00:58:35 +02:00
|
|
|
load_syphon_settings(s, settings);
|
|
|
|
|
|
|
|
} else if (!dict && !s->uuid.length) {
|
|
|
|
obs_data_set_string(settings, "app_name", "");
|
2019-07-09 13:29:39 -05:00
|
|
|
obs_data_set_string(settings, "name", "");
|
2014-08-24 00:58:35 +02:00
|
|
|
load_syphon_settings(s, settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void syphon_update_internal(syphon_t s, obs_data_t *settings)
|
|
|
|
{
|
2019-07-09 13:29:39 -05:00
|
|
|
s->allow_transparency =
|
|
|
|
obs_data_get_bool(settings, "allow_transparency");
|
2014-10-27 00:59:44 +01:00
|
|
|
|
2014-08-24 00:58:35 +02:00
|
|
|
load_crop(s, settings);
|
2022-07-28 19:04:16 +02:00
|
|
|
|
2014-08-24 00:58:35 +02:00
|
|
|
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-07-09 13:29:39 -05: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,
|
2014-08-24 00:58:35 +02:00
|
|
|
.get_properties = syphon_properties,
|
2019-07-09 13:29:39 -05:00
|
|
|
.get_width = syphon_get_width,
|
|
|
|
.get_height = syphon_get_height,
|
|
|
|
.update = syphon_update,
|
|
|
|
.save = syphon_save,
|
2019-07-27 23:59:16 -05:00
|
|
|
.icon_type = OBS_ICON_TYPE_GAME_CAPTURE,
|
2014-08-24 00:58:35 +02:00
|
|
|
};
|