module-zeroconf: Fix deduplication

Ports recent fixes and improvements regarding the deduplication of sink
creation from module-raop to module-zeroconf.

- 0bb0b524c7
- 14fd7f7bf7
This commit is contained in:
Christian Glombek 2023-06-11 20:28:56 +02:00
parent 389cbe7aef
commit d18db904b9

View file

@ -85,11 +85,7 @@ struct impl {
}; };
struct tunnel_info { struct tunnel_info {
AvahiIfIndex interface;
AvahiProtocol protocol;
const char *name; const char *name;
const char *type;
const char *domain;
}; };
#define TUNNEL_INFO(...) ((struct tunnel_info){ __VA_ARGS__ }) #define TUNNEL_INFO(...) ((struct tunnel_info){ __VA_ARGS__ })
@ -111,11 +107,7 @@ static struct tunnel *make_tunnel(struct impl *impl, const struct tunnel_info *i
if (t == NULL) if (t == NULL)
return NULL; return NULL;
t->info.interface = info->interface;
t->info.protocol = info->protocol;
t->info.name = strdup(info->name); t->info.name = strdup(info->name);
t->info.type = strdup(info->type);
t->info.domain = strdup(info->domain);
spa_list_append(&impl->tunnel_list, &t->link); spa_list_append(&impl->tunnel_list, &t->link);
return t; return t;
@ -125,11 +117,7 @@ static struct tunnel *find_tunnel(struct impl *impl, const struct tunnel_info *i
{ {
struct tunnel *t; struct tunnel *t;
spa_list_for_each(t, &impl->tunnel_list, link) { spa_list_for_each(t, &impl->tunnel_list, link) {
if (t->info.interface == info->interface && if (spa_streq(t->info.name, info->name))
t->info.protocol == info->protocol &&
spa_streq(t->info.name, info->name) &&
spa_streq(t->info.type, info->type) &&
spa_streq(t->info.domain, info->domain))
return t; return t;
} }
return NULL; return NULL;
@ -137,7 +125,12 @@ 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(t);
} }
static void impl_free(struct impl *impl) static void impl_free(struct impl *impl)
@ -226,14 +219,8 @@ 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.type);
free((char *) t->info.domain);
free(t);
} }
static const struct pw_impl_module_events submodule_events = { static const struct pw_impl_module_events submodule_events = {
@ -264,11 +251,19 @@ static void resolver_cb(AvahiServiceResolver *r, AvahiIfIndex interface, AvahiPr
avahi_strerror(avahi_client_errno(impl->client))); avahi_strerror(avahi_client_errno(impl->client)));
goto done; goto done;
} }
tinfo = TUNNEL_INFO(.interface = interface,
.protocol = protocol, tinfo = TUNNEL_INFO(.name = name);
.name = name,
.type = type, t = find_tunnel(impl, &tinfo);
.domain = domain); 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;
props = pw_properties_new(NULL, NULL); props = pw_properties_new(NULL, NULL);
if (props == NULL) { if (props == NULL) {
@ -346,8 +341,6 @@ static void resolver_cb(AvahiServiceResolver *r, AvahiIfIndex interface, AvahiPr
fprintf(f, "}"); fprintf(f, "}");
fclose(f); fclose(f);
pw_properties_free(props);
pw_log_info("loading module args:'%s'", args); pw_log_info("loading module args:'%s'", args);
mod = pw_context_load_module(impl->context, mod = pw_context_load_module(impl->context,
"libpipewire-module-pulse-tunnel", "libpipewire-module-pulse-tunnel",
@ -359,19 +352,13 @@ static void resolver_cb(AvahiServiceResolver *r, AvahiIfIndex interface, AvahiPr
goto done; goto done;
} }
t = make_tunnel(impl, &tinfo);
if (t == NULL) {
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:
avahi_service_resolver_free(r); avahi_service_resolver_free(r);
pw_properties_free(props);
} }
@ -386,18 +373,16 @@ static void browser_cb(AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProt
if (flags & AVAHI_LOOKUP_RESULT_LOCAL) if (flags & AVAHI_LOOKUP_RESULT_LOCAL)
return; return;
info = TUNNEL_INFO(.interface = interface, info = TUNNEL_INFO(.name = name);
.protocol = protocol,
.name = name,
.type = type,
.domain = domain);
t = find_tunnel(impl, &info); t = find_tunnel(impl, &info);
switch (event) { switch (event) {
case AVAHI_BROWSER_NEW: case AVAHI_BROWSER_NEW:
if (t != NULL) if (t != NULL) {
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,
interface, protocol, interface, protocol,
name, type, domain, name, type, domain,