Skip to content

Commit

Permalink
Check and update target of the source periodically
Browse files Browse the repository at this point in the history
Fix #1
  • Loading branch information
norihiro committed Mar 15, 2022
1 parent b6140a9 commit e92818e
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/async-source-duplication-source.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <obs-module.h>
#include <util/threading.h>
#include "plugin-macros.generated.h"

struct source_s
Expand All @@ -8,7 +9,9 @@ struct source_s
// properties
char *target_source_name;

pthread_mutex_t target_update_mutex;
obs_weak_source_t *target_weak;
float target_check;
};

static const char *get_name(void *type_data)
Expand Down Expand Up @@ -66,6 +69,7 @@ static void set_weak_target(struct source_s *s, obs_source_t *target)
if (s->target_weak)
release_weak_target(s);
s->target_weak = obs_source_get_weak_source(target);
s->target_check = 3.0f;

signal_handler_t *sh = obs_source_get_signal_handler(target);
signal_handler_connect(sh, "output_video", output_video, s);
Expand Down Expand Up @@ -96,36 +100,64 @@ static inline obs_source_t *source_to_filter(obs_source_t *src)
return target;
}

static inline void set_weak_target_by_name(struct source_s *s, const char *target_source_name)
static obs_source_t *get_filter_by_target_source_name(const char *target_source_name)
{
obs_source_t *src = obs_get_source_by_name(target_source_name);
if (!src)
return;
return NULL;
obs_source_t *target = source_to_filter(src);
obs_source_release(src);
return target;
}

static inline void set_weak_target_by_name(struct source_s *s, const char *target_source_name)
{
obs_source_t *target = get_filter_by_target_source_name(target_source_name);
if (target) {
set_weak_target(s, target);
obs_source_release(target);
}
obs_source_release(src);
}

static void update(void *data, obs_data_t *settings)
{
struct source_s *s = data;

const char *target_source_name = obs_data_get_string(settings, "target_source_name");
pthread_mutex_lock(&s->target_update_mutex);
if (target_source_name && (!s->target_source_name || strcmp(target_source_name, s->target_source_name))) {
bfree(s->target_source_name);
s->target_source_name = bstrdup(target_source_name);
release_weak_target(s);
set_weak_target_by_name(s, target_source_name);
}
pthread_mutex_unlock(&s->target_update_mutex);
}

static void tick(void *data, float seconds)
{
struct source_s *s = data;

pthread_mutex_lock(&s->target_update_mutex);
if ((s->target_check -= seconds) < 0.0f) {
obs_source_t *target_by_name = get_filter_by_target_source_name(s->target_source_name);
obs_source_t *target_by_weak = obs_weak_source_get_source(s->target_weak);
if (target_by_name != target_by_weak) {
blog(LOG_INFO, "updating target from %p to %p", target_by_weak, target_by_name);
set_weak_target(s, target_by_name);
}
obs_source_release(target_by_weak);
obs_source_release(target_by_name);
s->target_check = 3.0f;
}
pthread_mutex_unlock(&s->target_update_mutex);
}

static void *create(obs_data_t *settings, obs_source_t *source)
{
struct source_s *s = bzalloc(sizeof(struct source_s));
s->context = source;
pthread_mutex_init(&s->target_update_mutex, NULL);

update(s, settings);

Expand All @@ -137,6 +169,7 @@ static void destroy(void *data)
struct source_s *s = data;

release_weak_target(s);
pthread_mutex_destroy(&s->target_update_mutex);
bfree(s->target_source_name);

bfree(s);
Expand All @@ -150,5 +183,6 @@ const struct obs_source_info async_srcdup_source = {
.create = create,
.destroy = destroy,
.update = update,
.video_tick = tick,
.get_properties = get_properties,
};

0 comments on commit e92818e

Please sign in to comment.