From 6edcd456fe164e5cd3519da794021bda518de357 Mon Sep 17 00:00:00 2001 From: jp9000 Date: Thu, 26 Dec 2013 23:10:15 -0700 Subject: [PATCH] implement signal/procedure handling into libobs and individual sources --- libobs/callback/proc.c | 12 +++++------- libobs/callback/proc.h | 18 +++++++++++------- libobs/obs-data.h | 3 +++ libobs/obs-source.c | 26 ++++++++++++++++++++++++++ libobs/obs-source.h | 5 +++++ libobs/obs.c | 25 +++++++++++++++++++++++++ libobs/obs.h | 14 ++++++++++++++ 7 files changed, 89 insertions(+), 14 deletions(-) diff --git a/libobs/callback/proc.c b/libobs/callback/proc.c index 5156cc80d..44aad370f 100644 --- a/libobs/callback/proc.c +++ b/libobs/callback/proc.c @@ -20,6 +20,7 @@ struct proc_info { char *name; + void *data; void (*proc)(calldata_t, void*); }; @@ -29,16 +30,13 @@ static inline void proc_info_free(struct proc_info *pi) } struct proc_handler { - void *data; - /* TODO: replace with hash table lookup? */ DARRAY(struct proc_info) procs; }; -proc_handler_t proc_handler_create(void *data) +proc_handler_t proc_handler_create(void) { struct proc_handler *handler = bmalloc(sizeof(struct proc_handler)); - handler->data = data; da_init(handler->procs); return handler; } @@ -54,9 +52,9 @@ void proc_handler_destroy(proc_handler_t handler) } void proc_handler_add(proc_handler_t handler, const char *name, - void (*proc)(calldata_t, void*)) + void (*proc)(calldata_t, void*), void *data) { - struct proc_info pi = {bstrdup(name), proc}; + struct proc_info pi = {bstrdup(name), data, proc}; da_push_back(handler->procs, &pi); } @@ -67,7 +65,7 @@ bool proc_handler_call(proc_handler_t handler, const char *name, struct proc_info *info = handler->procs.array+i; if (strcmp(info->name, name) == 0) { - info->proc(params, handler->data); + info->proc(params, info->data); return true; } } diff --git a/libobs/callback/proc.h b/libobs/callback/proc.h index 793562bf3..1bf1581ac 100644 --- a/libobs/callback/proc.h +++ b/libobs/callback/proc.h @@ -24,18 +24,22 @@ * Dynamic procedure handler * * This handler is used to allow dynamic access to one or more procedures - * that can be called without having to have direct access to declarations - * or procedure callback pointers. + * that can be dynamically added and called without having to have direct + * access to declarations or procedure callback pointers. */ struct proc_handler; typedef struct proc_handler *proc_handler_t; -proc_handler_t proc_handler_create(void *data); -void proc_handler_destroy(proc_handler_t handler); +EXPORT proc_handler_t proc_handler_create(void); +EXPORT void proc_handler_destroy(proc_handler_t handler); -void proc_handler_add(proc_handler_t handler, const char *name, - void (*proc)(calldata_t, void*)); +EXPORT void proc_handler_add(proc_handler_t handler, const char *name, + void (*proc)(calldata_t, void*), void *data); -bool proc_handler_call(proc_handler_t handler, const char *name, +/** + * Calls a function in a procedure handler. Returns false if the named + * procedure is not found. + */ +EXPORT bool proc_handler_call(proc_handler_t handler, const char *name, calldata_t params); diff --git a/libobs/obs-data.h b/libobs/obs-data.h index 2fe3dd9ff..c38aab985 100644 --- a/libobs/obs-data.h +++ b/libobs/obs-data.h @@ -88,6 +88,9 @@ struct obs_subsystem { media_t media; + signal_handler_t signals; + proc_handler_t procs; + /* segmented into multiple sub-structures to keep things a bit more * clean and organized */ struct obs_video video; diff --git a/libobs/obs-source.c b/libobs/obs-source.c index 7634e45dc..228907c94 100644 --- a/libobs/obs-source.c +++ b/libobs/obs-source.c @@ -85,6 +85,16 @@ static inline const struct source_info *find_source(struct darray *list, return NULL; } +static inline bool obs_source_init_handlers(struct obs_source *source) +{ + source->signals = signal_handler_create(); + if (!source->signals) + return false; + + source->procs = proc_handler_create(); + return (source->procs != NULL); +} + /* internal initialization */ bool obs_source_init(struct obs_source *source, const char *settings, const struct source_info *info) @@ -143,6 +153,9 @@ obs_source_t obs_source_create(enum obs_source_type type, const char *id, source = bmalloc(sizeof(struct obs_source)); memset(source, 0, sizeof(struct obs_source)); + if (!obs_source_init_handlers(source)) + goto fail; + source->name = bstrdup(name); source->type = type; source->data = info->create(settings, source); @@ -186,6 +199,9 @@ static void obs_source_destroy(obs_source_t source) audio_line_destroy(source->audio_line); audio_resampler_destroy(source->resampler); + proc_handler_destroy(source->procs); + signal_handler_destroy(source->signals); + da_free(source->video_frames); da_free(source->audio_wait_buffer); da_free(source->filters); @@ -1004,3 +1020,13 @@ void obs_source_process_filter(obs_source_t filter, texrender_t texrender, render_filter_tex(texrender_gettexture(texrender), effect, width, height, yuv); } + +signal_handler_t obs_source_signalhandler(obs_source_t source) +{ + return source->signals; +} + +proc_handler_t obs_source_prochandler(obs_source_t source) +{ + return source->procs; +} diff --git a/libobs/obs-source.h b/libobs/obs-source.h index 684f9dc4a..96c4f8d3e 100644 --- a/libobs/obs-source.h +++ b/libobs/obs-source.h @@ -23,6 +23,8 @@ #include "util/threading.h" #include "media-io/media-io.h" #include "media-io/audio-resampler.h" +#include "callback/signal.h" +#include "callback/proc.h" /* * =========================================== @@ -210,6 +212,9 @@ struct obs_source { void *data; struct source_info callbacks; + signal_handler_t signals; + proc_handler_t procs; + /* used to indicate that the source has been removed and all * references to it should be released (not exactly how I would prefer * to handle things but it's the best option) */ diff --git a/libobs/obs.c b/libobs/obs.c index e9a6bf043..fe955bb9b 100644 --- a/libobs/obs.c +++ b/libobs/obs.c @@ -252,6 +252,16 @@ static void obs_free_data(void) pthread_mutex_unlock(&obs->data.sources_mutex); } +static inline bool obs_init_handlers(void) +{ + obs->signals = signal_handler_create(); + if (!obs->signals) + return false; + + obs->procs = proc_handler_create(); + return (obs->procs != NULL); +} + static bool obs_init(void) { obs = bmalloc(sizeof(struct obs_subsystem)); @@ -259,6 +269,9 @@ static bool obs_init(void) memset(obs, 0, sizeof(struct obs_subsystem)); obs_init_data(); + if (!obs_init_handlers()) + return false; + obs->media = media_open(); if (!obs->media) return false; @@ -300,6 +313,8 @@ void obs_shutdown(void) obs_free_graphics(); obs_free_audio(); media_close(obs->media); + proc_handler_destroy(obs->procs); + signal_handler_destroy(obs->signals); for (i = 0; i < obs->modules.num; i++) free_module(obs->modules.array+i); @@ -489,3 +504,13 @@ effect_t obs_get_default_effect(void) { return obs->video.default_effect; } + +signal_handler_t obs_signalhandler(void) +{ + return obs->signals; +} + +proc_handler_t obs_prochandler(void) +{ + return obs->procs; +} diff --git a/libobs/obs.h b/libobs/obs.h index 9c1439d0c..90249cce5 100644 --- a/libobs/obs.h +++ b/libobs/obs.h @@ -21,6 +21,8 @@ #include "graphics/graphics.h" #include "graphics/vec2.h" #include "media-io/media-io.h" +#include "callback/signal.h" +#include "callback/proc.h" #include "obs-defs.h" @@ -272,6 +274,12 @@ EXPORT char *obs_find_plugin_file(const char *file); /** Returns the default effect for generic RGB/YUV drawing */ EXPORT effect_t obs_get_default_effect(void); +/** Returns the primary obs signal handler */ +EXPORT signal_handler_t obs_signalhandler(void); + +/** Returns the primary obs procedure handler */ +EXPORT proc_handler_t obs_prochandler(void); + /* ------------------------------------------------------------------------- */ /* Display context */ @@ -393,6 +401,12 @@ EXPORT void obs_source_setname(obs_source_t source, const char *name); EXPORT void obs_source_getid(obs_source_t source, enum obs_source_type *type, const char **id); +/** Returns the signal handler for a source */ +EXPORT signal_handler_t obs_source_signalhandler(obs_source_t source); + +/** Returns the procedure handler for a source */ +EXPORT proc_handler_t obs_source_prochandler(obs_source_t source); + /* ------------------------------------------------------------------------- */ /* Functions used by sources */