4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #include <avahi-client/client.h>
33 #include <avahi-client/publish.h>
34 #include <avahi-common/alternative.h>
35 #include <avahi-common/error.h>
37 #include <pulse/xmalloc.h>
38 #include <pulse/util.h>
40 #include <pulsecore/autoload.h>
41 #include <pulsecore/sink.h>
42 #include <pulsecore/source.h>
43 #include <pulsecore/native-common.h>
44 #include <pulsecore/core-util.h>
45 #include <pulsecore/log.h>
46 #include <pulsecore/core-subscribe.h>
47 #include <pulsecore/dynarray.h>
48 #include <pulsecore/modargs.h>
49 #include <pulsecore/avahi-wrap.h>
50 #include <pulsecore/endianmacros.h>
52 #include "module-zeroconf-publish-symdef.h"
54 PA_MODULE_AUTHOR("Lennart Poettering")
55 PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Publisher")
56 PA_MODULE_VERSION(PACKAGE_VERSION)
57 PA_MODULE_USAGE("port=<IP port number>")
59 #define SERVICE_TYPE_SINK "_pulse-sink._tcp"
60 #define SERVICE_TYPE_SOURCE "_pulse-source._tcp"
61 #define SERVICE_TYPE_SERVER "_pulse-server._tcp"
63 static const char* const valid_modargs[] = {
69 struct userdata *userdata;
70 AvahiEntryGroup *entry_group;
73 enum { UNPUBLISHED, PUBLISHED_REAL, PUBLISHED_AUTOLOAD } published ;
77 pa_namereg_type_t type;
83 pa_namereg_type_t type;
90 AvahiPoll *avahi_poll;
93 pa_dynarray *sink_dynarray, *source_dynarray, *autoload_dynarray;
94 pa_subscription *subscription;
97 AvahiEntryGroup *main_entry_group;
102 static void get_service_data(struct userdata *u, struct service *s, pa_sample_spec *ret_ss, char **ret_description) {
103 assert(u && s && s->loaded.valid && ret_ss && ret_description);
105 if (s->loaded.type == PA_NAMEREG_SINK) {
106 pa_sink *sink = pa_idxset_get_by_index(u->core->sinks, s->loaded.index);
108 *ret_ss = sink->sample_spec;
109 *ret_description = sink->description;
110 } else if (s->loaded.type == PA_NAMEREG_SOURCE) {
111 pa_source *source = pa_idxset_get_by_index(u->core->sources, s->loaded.index);
113 *ret_ss = source->sample_spec;
114 *ret_description = source->description;
119 static AvahiStringList* txt_record_server_data(pa_core *c, AvahiStringList *l) {
123 l = avahi_string_list_add_pair(l, "server-version", PACKAGE_NAME" "PACKAGE_VERSION);
124 l = avahi_string_list_add_pair(l, "user-name", pa_get_user_name(s, sizeof(s)));
125 l = avahi_string_list_add_pair(l, "fqdn", pa_get_fqdn(s, sizeof(s)));
126 l = avahi_string_list_add_printf(l, "cookie=0x%08x", c->cookie);
131 static int publish_service(struct userdata *u, struct service *s);
133 static void service_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
134 struct service *s = userdata;
136 if (state == AVAHI_ENTRY_GROUP_COLLISION) {
139 t = avahi_alternative_service_name(s->service_name);
140 pa_xfree(s->service_name);
143 publish_service(s->userdata, s);
147 static int publish_service(struct userdata *u, struct service *s) {
149 AvahiStringList *txt = NULL;
154 if (!u->client || avahi_client_get_state(u->client) != AVAHI_CLIENT_S_RUNNING)
157 if ((s->published == PUBLISHED_REAL && s->loaded.valid) ||
158 (s->published == PUBLISHED_AUTOLOAD && s->autoload.valid && !s->loaded.valid))
161 if (s->published != UNPUBLISHED) {
162 avahi_entry_group_reset(s->entry_group);
163 s->published = UNPUBLISHED;
166 if (s->loaded.valid || s->autoload.valid) {
167 pa_namereg_type_t type;
169 if (!s->entry_group) {
170 if (!(s->entry_group = avahi_entry_group_new(u->client, service_entry_group_callback, s))) {
171 pa_log("avahi_entry_group_new(): %s", avahi_strerror(avahi_client_errno(u->client)));
176 txt = avahi_string_list_add_pair(txt, "device", s->name);
177 txt = txt_record_server_data(u->core, txt);
179 if (s->loaded.valid) {
183 get_service_data(u, s, &ss, &description);
185 txt = avahi_string_list_add_printf(txt, "rate=%u", ss.rate);
186 txt = avahi_string_list_add_printf(txt, "channels=%u", ss.channels);
187 txt = avahi_string_list_add_pair(txt, "format", pa_sample_format_to_string(ss.format));
189 txt = avahi_string_list_add_pair(txt, "description", description);
191 type = s->loaded.type;
192 } else if (s->autoload.valid)
193 type = s->autoload.type;
195 if (avahi_entry_group_add_service_strlst(
197 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
200 type == PA_NAMEREG_SINK ? SERVICE_TYPE_SINK : SERVICE_TYPE_SOURCE,
206 pa_log("avahi_entry_group_add_service_strlst(): %s", avahi_strerror(avahi_client_errno(u->client)));
210 if (avahi_entry_group_commit(s->entry_group) < 0) {
211 pa_log("avahi_entry_group_commit(): %s", avahi_strerror(avahi_client_errno(u->client)));
216 s->published = PUBLISHED_REAL;
217 else if (s->autoload.valid)
218 s->published = PUBLISHED_AUTOLOAD;
225 if (s->published == UNPUBLISHED) {
226 /* Remove this service */
229 avahi_entry_group_free(s->entry_group);
231 pa_hashmap_remove(u->services, s->name);
233 pa_xfree(s->service_name);
238 avahi_string_list_free(txt);
243 static struct service *get_service(struct userdata *u, const char *name, const char *description) {
247 if ((s = pa_hashmap_get(u->services, name)))
250 s = pa_xnew(struct service, 1);
252 s->entry_group = NULL;
253 s->published = UNPUBLISHED;
254 s->name = pa_xstrdup(name);
255 s->loaded.valid = s->autoload.valid = 0;
256 s->service_name = pa_sprintf_malloc("%s on %s", description ? description : s->name, pa_get_host_name(hn, sizeof(hn)));
258 pa_hashmap_put(u->services, s->name, s);
263 static int publish_sink(struct userdata *u, pa_sink *s) {
268 svc = get_service(u, s->name, s->description);
269 if (svc->loaded.valid)
270 return publish_service(u, svc);
272 svc->loaded.valid = 1;
273 svc->loaded.type = PA_NAMEREG_SINK;
274 svc->loaded.index = s->index;
276 if ((ret = publish_service(u, svc)) < 0)
279 pa_dynarray_put(u->sink_dynarray, s->index, svc);
283 static int publish_source(struct userdata *u, pa_source *s) {
289 svc = get_service(u, s->name, s->description);
290 if (svc->loaded.valid)
291 return publish_service(u, svc);
293 svc->loaded.valid = 1;
294 svc->loaded.type = PA_NAMEREG_SOURCE;
295 svc->loaded.index = s->index;
297 pa_dynarray_put(u->source_dynarray, s->index, svc);
299 if ((ret = publish_service(u, svc)) < 0)
302 pa_dynarray_put(u->sink_dynarray, s->index, svc);
306 static int publish_autoload(struct userdata *u, pa_autoload_entry *s) {
312 svc = get_service(u, s->name, NULL);
313 if (svc->autoload.valid)
314 return publish_service(u, svc);
316 svc->autoload.valid = 1;
317 svc->autoload.type = s->type;
318 svc->autoload.index = s->index;
320 if ((ret = publish_service(u, svc)) < 0)
323 pa_dynarray_put(u->autoload_dynarray, s->index, svc);
327 static int remove_sink(struct userdata *u, uint32_t idx) {
329 assert(u && idx != PA_INVALID_INDEX);
331 if (!(svc = pa_dynarray_get(u->sink_dynarray, idx)))
334 if (!svc->loaded.valid || svc->loaded.type != PA_NAMEREG_SINK)
337 svc->loaded.valid = 0;
338 pa_dynarray_put(u->sink_dynarray, idx, NULL);
340 return publish_service(u, svc);
343 static int remove_source(struct userdata *u, uint32_t idx) {
345 assert(u && idx != PA_INVALID_INDEX);
347 if (!(svc = pa_dynarray_get(u->source_dynarray, idx)))
350 if (!svc->loaded.valid || svc->loaded.type != PA_NAMEREG_SOURCE)
353 svc->loaded.valid = 0;
354 pa_dynarray_put(u->source_dynarray, idx, NULL);
356 return publish_service(u, svc);
359 static int remove_autoload(struct userdata *u, uint32_t idx) {
361 assert(u && idx != PA_INVALID_INDEX);
363 if (!(svc = pa_dynarray_get(u->autoload_dynarray, idx)))
366 if (!svc->autoload.valid)
369 svc->autoload.valid = 0;
370 pa_dynarray_put(u->autoload_dynarray, idx, NULL);
372 return publish_service(u, svc);
375 static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
376 struct userdata *u = userdata;
379 switch (t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK)
380 case PA_SUBSCRIPTION_EVENT_SINK: {
381 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
384 if ((sink = pa_idxset_get_by_index(c->sinks, idx))) {
385 if (publish_sink(u, sink) < 0)
388 } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
389 if (remove_sink(u, idx) < 0)
395 case PA_SUBSCRIPTION_EVENT_SOURCE:
397 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
400 if ((source = pa_idxset_get_by_index(c->sources, idx))) {
401 if (publish_source(u, source) < 0)
404 } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
405 if (remove_source(u, idx) < 0)
411 case PA_SUBSCRIPTION_EVENT_AUTOLOAD:
412 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
413 pa_autoload_entry *autoload;
415 if ((autoload = pa_idxset_get_by_index(c->autoload_idxset, idx))) {
416 if (publish_autoload(u, autoload) < 0)
419 } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
420 if (remove_autoload(u, idx) < 0)
430 if (u->subscription) {
431 pa_subscription_free(u->subscription);
432 u->subscription = NULL;
436 static int publish_main_service(struct userdata *u);
438 static void main_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
439 struct userdata *u = userdata;
442 if (state == AVAHI_ENTRY_GROUP_COLLISION) {
445 t = avahi_alternative_service_name(u->service_name);
446 pa_xfree(u->service_name);
449 publish_main_service(u);
453 static int publish_main_service(struct userdata *u) {
454 AvahiStringList *txt = NULL;
457 if (!u->main_entry_group) {
458 if (!(u->main_entry_group = avahi_entry_group_new(u->client, main_entry_group_callback, u))) {
459 pa_log("avahi_entry_group_new() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
463 avahi_entry_group_reset(u->main_entry_group);
465 txt = txt_record_server_data(u->core, NULL);
467 if (avahi_entry_group_add_service_strlst(
469 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
478 pa_log("avahi_entry_group_add_service_strlst() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
482 if (avahi_entry_group_commit(u->main_entry_group) < 0) {
483 pa_log("avahi_entry_group_commit() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
490 avahi_string_list_free(txt);
495 static int publish_all_services(struct userdata *u) {
498 pa_autoload_entry *autoload;
504 pa_log_debug("Publishing services in Zeroconf");
506 for (sink = pa_idxset_first(u->core->sinks, &idx); sink; sink = pa_idxset_next(u->core->sinks, &idx))
507 if (publish_sink(u, sink) < 0)
510 for (source = pa_idxset_first(u->core->sources, &idx); source; source = pa_idxset_next(u->core->sources, &idx))
511 if (publish_source(u, source) < 0)
514 if (u->core->autoload_idxset)
515 for (autoload = pa_idxset_first(u->core->autoload_idxset, &idx); autoload; autoload = pa_idxset_next(u->core->autoload_idxset, &idx))
516 if (publish_autoload(u, autoload) < 0)
519 if (publish_main_service(u) < 0)
528 static void unpublish_all_services(struct userdata *u, int rem) {
534 pa_log_debug("Unpublishing services in Zeroconf");
536 while ((s = pa_hashmap_iterate(u->services, &state, NULL))) {
537 if (s->entry_group) {
539 avahi_entry_group_free(s->entry_group);
540 s->entry_group = NULL;
542 avahi_entry_group_reset(s->entry_group);
545 s->published = UNPUBLISHED;
548 if (u->main_entry_group) {
550 avahi_entry_group_free(u->main_entry_group);
551 u->main_entry_group = NULL;
553 avahi_entry_group_reset(u->main_entry_group);
557 static void client_callback(AvahiClient *c, AvahiClientState state, void *userdata) {
558 struct userdata *u = userdata;
564 case AVAHI_CLIENT_S_RUNNING:
565 publish_all_services(u);
568 case AVAHI_CLIENT_S_COLLISION:
569 unpublish_all_services(u, 0);
572 case AVAHI_CLIENT_FAILURE:
573 if (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
575 unpublish_all_services(u, 1);
576 avahi_client_free(u->client);
578 if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error)))
579 pa_log("pa_avahi_client_new() failed: %s", avahi_strerror(error));
588 int pa__init(pa_core *c, pa_module*m) {
590 uint32_t port = PA_NATIVE_DEFAULT_PORT;
591 pa_modargs *ma = NULL;
595 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
596 pa_log("failed to parse module arguments.");
600 if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port == 0 || port >= 0xFFFF) {
601 pa_log("invalid port specified.");
605 m->userdata = u = pa_xnew(struct userdata, 1);
607 u->port = (uint16_t) port;
609 u->avahi_poll = pa_avahi_poll_new(c->mainloop);
611 u->services = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
612 u->sink_dynarray = pa_dynarray_new();
613 u->source_dynarray = pa_dynarray_new();
614 u->autoload_dynarray = pa_dynarray_new();
616 u->subscription = pa_subscription_new(c,
617 PA_SUBSCRIPTION_MASK_SINK|
618 PA_SUBSCRIPTION_MASK_SOURCE|
619 PA_SUBSCRIPTION_MASK_AUTOLOAD, subscribe_callback, u);
621 u->main_entry_group = NULL;
623 u->service_name = pa_xstrdup(pa_get_host_name(hn, sizeof(hn)));
625 if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
626 pa_log("pa_avahi_client_new() failed: %s", avahi_strerror(error));
643 static void service_free(void *p, void *userdata) {
644 struct service *s = p;
645 struct userdata *u = userdata;
651 avahi_entry_group_free(s->entry_group);
653 pa_xfree(s->service_name);
658 void pa__done(pa_core *c, pa_module*m) {
662 if (!(u = m->userdata))
666 pa_hashmap_free(u->services, service_free, u);
669 pa_subscription_free(u->subscription);
671 if (u->sink_dynarray)
672 pa_dynarray_free(u->sink_dynarray, NULL, NULL);
673 if (u->source_dynarray)
674 pa_dynarray_free(u->source_dynarray, NULL, NULL);
675 if (u->autoload_dynarray)
676 pa_dynarray_free(u->autoload_dynarray, NULL, NULL);
679 if (u->main_entry_group)
680 avahi_entry_group_free(u->main_entry_group);
683 avahi_client_free(u->client);
686 pa_avahi_poll_free(u->avahi_poll);
688 pa_xfree(u->service_name);