module-raop: fix deduplication

Also check duplicate entries in the resolve callback because we might
not have created a tunnel earlier.
This commit is contained in:
Wim Taymans 2023-06-06 10:23:52 +02:00
parent 6b6f3432fe
commit 14fd7f7bf7

View file

@ -171,7 +171,14 @@ static struct tunnel *find_tunnel(struct impl *impl, const struct tunnel_info *i
static void free_tunnel(struct tunnel *t) static void free_tunnel(struct tunnel *t)
{ {
pw_impl_module_destroy(t->module); spa_list_remove(&t->link);
if (t->module)
pw_impl_module_destroy(t->module);
free((char *) t->info.name);
free((char *) t->info.host_name);
free((char *) t->info.ip);
free((char *) t->info.port);
free(t);
} }
static void impl_free(struct impl *impl) static void impl_free(struct impl *impl)
@ -284,16 +291,8 @@ static void pw_properties_from_avahi_string(const char *key, const char *value,
static void submodule_destroy(void *data) static void submodule_destroy(void *data)
{ {
struct tunnel *t = data; struct tunnel *t = data;
spa_list_remove(&t->link);
spa_hook_remove(&t->module_listener); spa_hook_remove(&t->module_listener);
t->module = NULL;
free((char *) t->info.name);
free((char *) t->info.host_name);
free((char *) t->info.ip);
free((char *) t->info.port);
free(t);
} }
static const struct pw_impl_module_events submodule_events = { static const struct pw_impl_module_events submodule_events = {
@ -304,19 +303,18 @@ static const struct pw_impl_module_events submodule_events = {
struct match_info { struct match_info {
struct impl *impl; struct impl *impl;
struct pw_properties *props; struct pw_properties *props;
struct tunnel_info *tinfo; struct tunnel *tunnel;
bool matched; bool matched;
}; };
static int create_stream(struct impl *impl, struct pw_properties *props, static int create_stream(struct impl *impl, struct pw_properties *props,
struct tunnel_info *tinfo) struct tunnel *t)
{ {
FILE *f; FILE *f;
char *args; char *args;
size_t size; size_t size;
int res = 0; int res = 0;
struct pw_impl_module *mod; struct pw_impl_module *mod;
struct tunnel *t;
if ((f = open_memstream(&args, &size)) == NULL) { if ((f = open_memstream(&args, &size)) == NULL) {
res = -errno; res = -errno;
@ -341,16 +339,7 @@ static int create_stream(struct impl *impl, struct pw_properties *props,
goto done; goto done;
} }
t = make_tunnel(impl, tinfo);
if (t == NULL) {
res = -errno;
pw_log_error("Can't make tunnel: %m");
pw_impl_module_destroy(mod);
goto done;
}
pw_impl_module_add_listener(mod, &t->module_listener, &submodule_events, t); pw_impl_module_add_listener(mod, &t->module_listener, &submodule_events, t);
t->module = mod; t->module = mod;
done: done:
return res; return res;
@ -365,7 +354,7 @@ static int rule_matched(void *data, const char *location, const char *action,
i->matched = true; i->matched = true;
if (spa_streq(action, "create-stream")) { if (spa_streq(action, "create-stream")) {
pw_properties_update_string(i->props, str, len); pw_properties_update_string(i->props, str, len);
create_stream(i->impl, i->props, i->tinfo); create_stream(i->impl, i->props, i->tunnel);
} }
return res; return res;
} }
@ -377,6 +366,7 @@ static void resolver_cb(AvahiServiceResolver *r, AvahiIfIndex interface, AvahiPr
{ {
struct impl *impl = userdata; struct impl *impl = userdata;
struct tunnel_info tinfo; struct tunnel_info tinfo;
struct tunnel *t;
const char *str, *port_str; const char *str, *port_str;
AvahiStringList *l; AvahiStringList *l;
struct pw_properties *props = NULL; struct pw_properties *props = NULL;
@ -419,6 +409,17 @@ static void resolver_cb(AvahiServiceResolver *r, AvahiIfIndex interface, AvahiPr
.host_name = host_name, .host_name = host_name,
.ip = at, .ip = at,
.port = port_str); .port = port_str);
t = find_tunnel(impl, &tinfo);
if (t == NULL)
t = make_tunnel(impl, &tinfo);
if (t == NULL) {
pw_log_error("Can't make tunnel: %m");
goto done;
}
if (t->module != NULL) {
pw_log_info("found duplicate mdns entry - skipping tunnel creation");
goto done;
}
if ((str = pw_properties_get(impl->properties, "stream.rules")) == NULL) if ((str = pw_properties_get(impl->properties, "stream.rules")) == NULL)
str = DEFAULT_CREATE_RULES; str = DEFAULT_CREATE_RULES;
@ -426,7 +427,7 @@ static void resolver_cb(AvahiServiceResolver *r, AvahiIfIndex interface, AvahiPr
struct match_info minfo = { struct match_info minfo = {
.impl = impl, .impl = impl,
.props = props, .props = props,
.tinfo = &tinfo, .tunnel = t,
}; };
pw_conf_match_rules(str, strlen(str), NAME, &props->dict, pw_conf_match_rules(str, strlen(str), NAME, &props->dict,
rule_matched, &minfo); rule_matched, &minfo);
@ -459,7 +460,7 @@ static void browser_cb(AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProt
switch (event) { switch (event) {
case AVAHI_BROWSER_NEW: case AVAHI_BROWSER_NEW:
if (t != NULL) { if (t != NULL) {
pw_log_debug("found duplicate mdns entry - skipping tunnel creation"); pw_log_info("found duplicate mdns entry - skipping tunnel creation");
return; return;
} }
if (!(avahi_service_resolver_new(impl->client, if (!(avahi_service_resolver_new(impl->client,