modules: use timer-queue in avahi-poll

Pass the pw_context to get to the shared queue and loop.
Patch up the users of avahi-poll.

Fixes #4913
This commit is contained in:
Wim Taymans 2025-09-26 13:39:49 +02:00
parent 678e571d80
commit fdc74df383
7 changed files with 49 additions and 41 deletions

View file

@ -637,7 +637,6 @@ static const struct impl_events impl_events = {
static int module_zeroconf_publish_load(struct module *module) static int module_zeroconf_publish_load(struct module *module)
{ {
struct module_zeroconf_publish_data *data = module->user_data; struct module_zeroconf_publish_data *data = module->user_data;
struct pw_loop *loop;
int error; int error;
data->core = pw_context_connect(module->impl->context, NULL, 0); data->core = pw_context_connect(module->impl->context, NULL, 0);
@ -650,8 +649,7 @@ static int module_zeroconf_publish_load(struct module *module)
&data->core_listener, &data->core_listener,
&core_events, data); &core_events, data);
loop = pw_context_get_main_loop(module->impl->context); data->avahi_poll = pw_avahi_poll_new(module->impl->context);
data->avahi_poll = pw_avahi_poll_new(loop);
data->client = avahi_client_new(data->avahi_poll, AVAHI_CLIENT_NO_FAIL, data->client = avahi_client_new(data->avahi_poll, AVAHI_CLIENT_NO_FAIL,
client_callback, data, &error); client_callback, data, &error);

View file

@ -561,10 +561,8 @@ static int start_client(struct impl *impl)
static int start_avahi(struct impl *impl) static int start_avahi(struct impl *impl)
{ {
struct pw_loop *loop;
loop = pw_context_get_main_loop(impl->context); impl->avahi_poll = pw_avahi_poll_new(impl->context);
impl->avahi_poll = pw_avahi_poll_new(loop);
return start_client(impl); return start_client(impl);
} }

View file

@ -1835,7 +1835,7 @@ int pipewire__module_init(struct pw_impl_module *module, const char *args)
if ((res = setup_apple_session(impl)) < 0) if ((res = setup_apple_session(impl)) < 0)
goto out; goto out;
impl->avahi_poll = pw_avahi_poll_new(impl->loop); impl->avahi_poll = pw_avahi_poll_new(impl->context);
if ((impl->client = avahi_client_new(impl->avahi_poll, if ((impl->client = avahi_client_new(impl->avahi_poll,
AVAHI_CLIENT_NO_FAIL, AVAHI_CLIENT_NO_FAIL,
client_callback, impl, client_callback, impl,

View file

@ -166,13 +166,13 @@ static const struct spa_dict_item module_props[] = {
struct impl { struct impl {
struct pw_context *context; struct pw_context *context;
struct pw_loop *loop;
struct pw_impl_module *module; struct pw_impl_module *module;
struct spa_hook module_listener; struct spa_hook module_listener;
struct pw_properties *properties; struct pw_properties *properties;
bool discover_local; bool discover_local;
struct pw_loop *loop;
AvahiPoll *avahi_poll; AvahiPoll *avahi_poll;
AvahiClient *client; AvahiClient *client;
@ -850,10 +850,8 @@ static int start_client(struct impl *impl)
static int start_avahi(struct impl *impl) static int start_avahi(struct impl *impl)
{ {
struct pw_loop *loop;
loop = pw_context_get_main_loop(impl->context); impl->avahi_poll = pw_avahi_poll_new(impl->context);
impl->avahi_poll = pw_avahi_poll_new(loop);
return start_client(impl); return start_client(impl);
} }

View file

@ -487,10 +487,7 @@ static int start_client(struct impl *impl)
static int start_avahi(struct impl *impl) static int start_avahi(struct impl *impl)
{ {
struct pw_loop *loop; impl->avahi_poll = pw_avahi_poll_new(impl->context);
loop = pw_context_get_main_loop(impl->context);
impl->avahi_poll = pw_avahi_poll_new(loop);
return start_client(impl); return start_client(impl);
} }

View file

@ -8,7 +8,9 @@
struct impl { struct impl {
AvahiPoll api; AvahiPoll api;
struct pw_context *context;
struct pw_loop *loop; struct pw_loop *loop;
struct pw_timer_queue *timer_queue;
}; };
struct AvahiWatch { struct AvahiWatch {
@ -22,7 +24,7 @@ struct AvahiWatch {
struct AvahiTimeout { struct AvahiTimeout {
struct impl *impl; struct impl *impl;
struct spa_source *source; struct pw_timer timer;
AvahiTimeoutCallback callback; AvahiTimeoutCallback callback;
void *userdata; void *userdata;
}; };
@ -99,17 +101,40 @@ static void watch_free(AvahiWatch *w)
free(w); free(w);
} }
static void timeout_callback(void *data, uint64_t expirations) static void timeout_callback(void *data)
{ {
AvahiTimeout *w = data; AvahiTimeout *w = data;
w->callback(w, w->userdata); w->callback(w, w->userdata);
} }
static int schedule_timeout(AvahiTimeout *t, const struct timeval *tv)
{
struct timeval now;
int64_t timeout_ns;
if (tv == NULL)
return 0;
/* Get current REALTIME (same clock domain as Avahi) */
if (gettimeofday(&now, NULL) < 0)
return -errno;
/* Calculate relative timeout: target - now */
timeout_ns = ((int64_t)tv->tv_sec - now.tv_sec) * SPA_NSEC_PER_SEC +
((int64_t)tv->tv_usec - now.tv_usec) * 1000UL;
/* Ensure minimum timeout */
if (timeout_ns <= 0)
timeout_ns = 1;
return pw_timer_queue_add(t->impl->timer_queue, &t->timer, NULL,
timeout_ns, timeout_callback, t);
}
static AvahiTimeout* timeout_new(const AvahiPoll *api, const struct timeval *tv, static AvahiTimeout* timeout_new(const AvahiPoll *api, const struct timeval *tv,
AvahiTimeoutCallback callback, void *userdata) AvahiTimeoutCallback callback, void *userdata)
{ {
struct impl *impl = api->userdata; struct impl *impl = api->userdata;
struct timespec value;
AvahiTimeout *w; AvahiTimeout *w;
w = calloc(1, sizeof(*w)); w = calloc(1, sizeof(*w));
@ -119,38 +144,27 @@ static AvahiTimeout* timeout_new(const AvahiPoll *api, const struct timeval *tv,
w->impl = impl; w->impl = impl;
w->callback = callback; w->callback = callback;
w->userdata = userdata; w->userdata = userdata;
w->source = pw_loop_add_timer(impl->loop, timeout_callback, w);
if (w->source == NULL) { if (schedule_timeout(w, tv) < 0) {
free(w); free(w);
return NULL; return NULL;
} }
if (tv != NULL) {
value.tv_sec = tv->tv_sec;
value.tv_nsec = tv->tv_usec * 1000UL;
pw_loop_update_timer(impl->loop, w->source, &value, NULL, true);
}
return w; return w;
} }
static void timeout_update(AvahiTimeout *t, const struct timeval *tv) static void timeout_update(AvahiTimeout *t, const struct timeval *tv)
{ {
struct impl *impl = t->impl; /* Cancel the existing timer */
struct timespec value, *v = NULL; pw_timer_queue_cancel(&t->timer);
if (tv != NULL) { /* Schedule new timeout if provided */
value.tv_sec = tv->tv_sec; schedule_timeout(t, tv);
value.tv_nsec = tv->tv_usec * 1000UL;
if (value.tv_sec == 0 && value.tv_nsec == 0)
value.tv_nsec = 1;
v = &value;
}
pw_loop_update_timer(impl->loop, t->source, v, NULL, true);
} }
static void timeout_free(AvahiTimeout *t) static void timeout_free(AvahiTimeout *t)
{ {
struct impl *impl = t->impl; pw_timer_queue_cancel(&t->timer);
pw_loop_destroy_source(impl->loop, t->source);
free(t); free(t);
} }
@ -164,7 +178,7 @@ static const AvahiPoll avahi_poll_api = {
.timeout_free = timeout_free, .timeout_free = timeout_free,
}; };
AvahiPoll* pw_avahi_poll_new(struct pw_loop *loop) AvahiPoll* pw_avahi_poll_new(struct pw_context *context)
{ {
struct impl *impl; struct impl *impl;
@ -172,7 +186,10 @@ AvahiPoll* pw_avahi_poll_new(struct pw_loop *loop)
if (impl == NULL) if (impl == NULL)
return NULL; return NULL;
impl->loop = loop; impl->context = context;
impl->loop = pw_context_get_main_loop(context);
impl->timer_queue = pw_context_get_timer_queue(context);
impl->api = avahi_poll_api; impl->api = avahi_poll_api;
impl->api.userdata = impl; impl->api.userdata = impl;

View file

@ -4,8 +4,8 @@
#include <avahi-client/client.h> #include <avahi-client/client.h>
#include <pipewire/loop.h> #include <pipewire/context.h>
AvahiPoll* pw_avahi_poll_new(struct pw_loop *loop); AvahiPoll* pw_avahi_poll_new(struct pw_context *context);
void pw_avahi_poll_free(AvahiPoll *p); void pw_avahi_poll_free(AvahiPoll *p);