4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 #include <avahi-client/client.h>
35 #include <avahi-client/publish.h>
36 #include <avahi-common/alternative.h>
37 #include <avahi-common/error.h>
38 #include <avahi-common/domain.h>
40 #include <pulse/xmalloc.h>
41 #include <pulse/util.h>
43 #include <pulsecore/autoload.h>
44 #include <pulsecore/sink.h>
45 #include <pulsecore/source.h>
46 #include <pulsecore/native-common.h>
47 #include <pulsecore/core-util.h>
48 #include <pulsecore/log.h>
49 #include <pulsecore/core-subscribe.h>
50 #include <pulsecore/dynarray.h>
51 #include <pulsecore/modargs.h>
52 #include <pulsecore/avahi-wrap.h>
53 #include <pulsecore/endianmacros.h>
55 #include "module-zeroconf-publish-symdef.h"
57 PA_MODULE_AUTHOR("Lennart Poettering")
58 PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Publisher")
59 PA_MODULE_VERSION(PACKAGE_VERSION
)
60 PA_MODULE_USAGE("port=<IP port number>")
62 #define SERVICE_TYPE_SINK "_pulse-sink._tcp"
63 #define SERVICE_TYPE_SOURCE "_pulse-source._tcp"
64 #define SERVICE_TYPE_SERVER "_pulse-server._tcp"
66 static const char* const valid_modargs
[] = {
72 struct userdata
*userdata
;
73 AvahiEntryGroup
*entry_group
;
76 enum { UNPUBLISHED
, PUBLISHED_REAL
, PUBLISHED_AUTOLOAD
} published
;
80 pa_namereg_type_t type
;
86 pa_namereg_type_t type
;
93 AvahiPoll
*avahi_poll
;
96 pa_dynarray
*sink_dynarray
, *source_dynarray
, *autoload_dynarray
;
97 pa_subscription
*subscription
;
100 AvahiEntryGroup
*main_entry_group
;
105 static void get_service_data(struct userdata
*u
, struct service
*s
, pa_sample_spec
*ret_ss
, char **ret_description
) {
108 pa_assert(s
->loaded
.valid
);
110 pa_assert(ret_description
);
112 if (s
->loaded
.type
== PA_NAMEREG_SINK
) {
113 pa_sink
*sink
= PA_SINK(pa_idxset_get_by_index(u
->core
->sinks
, s
->loaded
.index
));
114 pa_sink_assert_ref(sink
);
115 *ret_ss
= sink
->sample_spec
;
116 *ret_description
= sink
->description
;
118 } else if (s
->loaded
.type
== PA_NAMEREG_SOURCE
) {
119 pa_source
*source
= PA_SOURCE(pa_idxset_get_by_index(u
->core
->sources
, s
->loaded
.index
));
120 pa_source_assert_ref(source
);
121 *ret_ss
= source
->sample_spec
;
122 *ret_description
= source
->description
;
124 pa_assert_not_reached();
127 static AvahiStringList
* txt_record_server_data(pa_core
*c
, AvahiStringList
*l
) {
130 pa_core_assert_ref(c
);
132 l
= avahi_string_list_add_pair(l
, "server-version", PACKAGE_NAME
" "PACKAGE_VERSION
);
133 l
= avahi_string_list_add_pair(l
, "user-name", pa_get_user_name(s
, sizeof(s
)));
134 l
= avahi_string_list_add_pair(l
, "fqdn", pa_get_fqdn(s
, sizeof(s
)));
135 l
= avahi_string_list_add_printf(l
, "cookie=0x%08x", c
->cookie
);
140 static int publish_service(struct userdata
*u
, struct service
*s
);
142 static void service_entry_group_callback(AvahiEntryGroup
*g
, AvahiEntryGroupState state
, void *userdata
) {
143 struct service
*s
= userdata
;
147 if (state
== AVAHI_ENTRY_GROUP_COLLISION
) {
150 t
= avahi_alternative_service_name(s
->service_name
);
151 pa_xfree(s
->service_name
);
154 publish_service(s
->userdata
, s
);
158 static int publish_service(struct userdata
*u
, struct service
*s
) {
160 AvahiStringList
*txt
= NULL
;
165 if (!u
->client
|| avahi_client_get_state(u
->client
) != AVAHI_CLIENT_S_RUNNING
)
168 if ((s
->published
== PUBLISHED_REAL
&& s
->loaded
.valid
) ||
169 (s
->published
== PUBLISHED_AUTOLOAD
&& s
->autoload
.valid
&& !s
->loaded
.valid
))
172 if (s
->published
!= UNPUBLISHED
) {
173 avahi_entry_group_reset(s
->entry_group
);
174 s
->published
= UNPUBLISHED
;
177 if (s
->loaded
.valid
|| s
->autoload
.valid
) {
178 pa_namereg_type_t type
;
180 if (!s
->entry_group
) {
181 if (!(s
->entry_group
= avahi_entry_group_new(u
->client
, service_entry_group_callback
, s
))) {
182 pa_log("avahi_entry_group_new(): %s", avahi_strerror(avahi_client_errno(u
->client
)));
187 txt
= avahi_string_list_add_pair(txt
, "device", s
->name
);
188 txt
= txt_record_server_data(u
->core
, txt
);
190 if (s
->loaded
.valid
) {
194 get_service_data(u
, s
, &ss
, &description
);
196 txt
= avahi_string_list_add_printf(txt
, "rate=%u", ss
.rate
);
197 txt
= avahi_string_list_add_printf(txt
, "channels=%u", ss
.channels
);
198 txt
= avahi_string_list_add_pair(txt
, "format", pa_sample_format_to_string(ss
.format
));
200 txt
= avahi_string_list_add_pair(txt
, "description", description
);
202 type
= s
->loaded
.type
;
203 } else if (s
->autoload
.valid
)
204 type
= s
->autoload
.type
;
206 if (avahi_entry_group_add_service_strlst(
208 AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
,
211 type
== PA_NAMEREG_SINK
? SERVICE_TYPE_SINK
: SERVICE_TYPE_SOURCE
,
217 pa_log("avahi_entry_group_add_service_strlst(): %s", avahi_strerror(avahi_client_errno(u
->client
)));
221 if (avahi_entry_group_commit(s
->entry_group
) < 0) {
222 pa_log("avahi_entry_group_commit(): %s", avahi_strerror(avahi_client_errno(u
->client
)));
227 s
->published
= PUBLISHED_REAL
;
228 else if (s
->autoload
.valid
)
229 s
->published
= PUBLISHED_AUTOLOAD
;
236 if (s
->published
== UNPUBLISHED
) {
237 /* Remove this service */
240 avahi_entry_group_free(s
->entry_group
);
242 pa_hashmap_remove(u
->services
, s
->name
);
244 pa_xfree(s
->service_name
);
249 avahi_string_list_free(txt
);
254 static struct service
*get_service(struct userdata
*u
, const char *name
, const char *description
) {
258 if ((s
= pa_hashmap_get(u
->services
, name
)))
261 s
= pa_xnew(struct service
, 1);
263 s
->entry_group
= NULL
;
264 s
->published
= UNPUBLISHED
;
265 s
->name
= pa_xstrdup(name
);
266 s
->loaded
.valid
= s
->autoload
.valid
= 0;
267 s
->service_name
= pa_truncate_utf8(pa_sprintf_malloc("%s@%s: %s", pa_get_user_name(un
, sizeof(un
)), pa_get_host_name(hn
, sizeof(hn
)), description
? description
: s
->name
), AVAHI_LABEL_MAX
-1);
269 pa_hashmap_put(u
->services
, s
->name
, s
);
274 static int publish_sink(struct userdata
*u
, pa_sink
*s
) {
279 pa_sink_assert_ref(s
);
281 svc
= get_service(u
, s
->name
, s
->description
);
282 if (svc
->loaded
.valid
)
283 return publish_service(u
, svc
);
285 svc
->loaded
.valid
= 1;
286 svc
->loaded
.type
= PA_NAMEREG_SINK
;
287 svc
->loaded
.index
= s
->index
;
289 if ((ret
= publish_service(u
, svc
)) < 0)
292 pa_dynarray_put(u
->sink_dynarray
, s
->index
, svc
);
296 static int publish_source(struct userdata
*u
, pa_source
*s
) {
301 pa_source_assert_ref(s
);
303 svc
= get_service(u
, s
->name
, s
->description
);
304 if (svc
->loaded
.valid
)
305 return publish_service(u
, svc
);
307 svc
->loaded
.valid
= 1;
308 svc
->loaded
.type
= PA_NAMEREG_SOURCE
;
309 svc
->loaded
.index
= s
->index
;
311 pa_dynarray_put(u
->source_dynarray
, s
->index
, svc
);
313 if ((ret
= publish_service(u
, svc
)) < 0)
316 pa_dynarray_put(u
->sink_dynarray
, s
->index
, svc
);
320 static int publish_autoload(struct userdata
*u
, pa_autoload_entry
*s
) {
327 svc
= get_service(u
, s
->name
, NULL
);
328 if (svc
->autoload
.valid
)
329 return publish_service(u
, svc
);
331 svc
->autoload
.valid
= 1;
332 svc
->autoload
.type
= s
->type
;
333 svc
->autoload
.index
= s
->index
;
335 if ((ret
= publish_service(u
, svc
)) < 0)
338 pa_dynarray_put(u
->autoload_dynarray
, s
->index
, svc
);
342 static int remove_sink(struct userdata
*u
, uint32_t idx
) {
346 pa_assert(idx
!= PA_INVALID_INDEX
);
348 if (!(svc
= pa_dynarray_get(u
->sink_dynarray
, idx
)))
351 if (!svc
->loaded
.valid
|| svc
->loaded
.type
!= PA_NAMEREG_SINK
)
354 svc
->loaded
.valid
= 0;
355 pa_dynarray_put(u
->sink_dynarray
, idx
, NULL
);
357 return publish_service(u
, svc
);
360 static int remove_source(struct userdata
*u
, uint32_t idx
) {
364 pa_assert(idx
!= PA_INVALID_INDEX
);
366 if (!(svc
= pa_dynarray_get(u
->source_dynarray
, idx
)))
369 if (!svc
->loaded
.valid
|| svc
->loaded
.type
!= PA_NAMEREG_SOURCE
)
372 svc
->loaded
.valid
= 0;
373 pa_dynarray_put(u
->source_dynarray
, idx
, NULL
);
375 return publish_service(u
, svc
);
378 static int remove_autoload(struct userdata
*u
, uint32_t idx
) {
382 pa_assert(idx
!= PA_INVALID_INDEX
);
384 if (!(svc
= pa_dynarray_get(u
->autoload_dynarray
, idx
)))
387 if (!svc
->autoload
.valid
)
390 svc
->autoload
.valid
= 0;
391 pa_dynarray_put(u
->autoload_dynarray
, idx
, NULL
);
393 return publish_service(u
, svc
);
396 static void subscribe_callback(pa_core
*c
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
397 struct userdata
*u
= userdata
;
400 pa_core_assert_ref(c
);
402 switch (t
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
)
404 case PA_SUBSCRIPTION_EVENT_SINK
: {
405 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_NEW
) {
408 if ((sink
= PA_SINK(pa_idxset_get_by_index(c
->sinks
, idx
)))) {
409 if (publish_sink(u
, sink
) < 0)
412 } else if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_REMOVE
) {
413 if (remove_sink(u
, idx
) < 0)
419 case PA_SUBSCRIPTION_EVENT_SOURCE
:
421 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_NEW
) {
424 if ((source
= PA_SOURCE(pa_idxset_get_by_index(c
->sources
, idx
)))) {
425 if (publish_source(u
, source
) < 0)
428 } else if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_REMOVE
) {
429 if (remove_source(u
, idx
) < 0)
435 case PA_SUBSCRIPTION_EVENT_AUTOLOAD
:
436 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_NEW
) {
437 pa_autoload_entry
*autoload
;
439 if ((autoload
= pa_idxset_get_by_index(c
->autoload_idxset
, idx
))) {
440 if (publish_autoload(u
, autoload
) < 0)
443 } else if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_REMOVE
) {
444 if (remove_autoload(u
, idx
) < 0)
454 if (u
->subscription
) {
455 pa_subscription_free(u
->subscription
);
456 u
->subscription
= NULL
;
460 static int publish_main_service(struct userdata
*u
);
462 static void main_entry_group_callback(AvahiEntryGroup
*g
, AvahiEntryGroupState state
, void *userdata
) {
463 struct userdata
*u
= userdata
;
466 if (state
== AVAHI_ENTRY_GROUP_COLLISION
) {
469 t
= avahi_alternative_service_name(u
->service_name
);
470 pa_xfree(u
->service_name
);
473 publish_main_service(u
);
477 static int publish_main_service(struct userdata
*u
) {
478 AvahiStringList
*txt
= NULL
;
483 if (!u
->main_entry_group
) {
484 if (!(u
->main_entry_group
= avahi_entry_group_new(u
->client
, main_entry_group_callback
, u
))) {
485 pa_log("avahi_entry_group_new() failed: %s", avahi_strerror(avahi_client_errno(u
->client
)));
489 avahi_entry_group_reset(u
->main_entry_group
);
491 txt
= txt_record_server_data(u
->core
, NULL
);
493 if (avahi_entry_group_add_service_strlst(
495 AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
,
504 pa_log("avahi_entry_group_add_service_strlst() failed: %s", avahi_strerror(avahi_client_errno(u
->client
)));
508 if (avahi_entry_group_commit(u
->main_entry_group
) < 0) {
509 pa_log("avahi_entry_group_commit() failed: %s", avahi_strerror(avahi_client_errno(u
->client
)));
516 avahi_string_list_free(txt
);
521 static int publish_all_services(struct userdata
*u
) {
524 pa_autoload_entry
*autoload
;
530 pa_log_debug("Publishing services in Zeroconf");
532 for (sink
= PA_SINK(pa_idxset_first(u
->core
->sinks
, &idx
)); sink
; sink
= PA_SINK(pa_idxset_next(u
->core
->sinks
, &idx
)))
533 if (publish_sink(u
, sink
) < 0)
536 for (source
= PA_SOURCE(pa_idxset_first(u
->core
->sources
, &idx
)); source
; source
= PA_SOURCE(pa_idxset_next(u
->core
->sources
, &idx
)))
537 if (publish_source(u
, source
) < 0)
540 if (u
->core
->autoload_idxset
)
541 for (autoload
= pa_idxset_first(u
->core
->autoload_idxset
, &idx
); autoload
; autoload
= pa_idxset_next(u
->core
->autoload_idxset
, &idx
))
542 if (publish_autoload(u
, autoload
) < 0)
545 if (publish_main_service(u
) < 0)
554 static void unpublish_all_services(struct userdata
*u
, int rem
) {
560 pa_log_debug("Unpublishing services in Zeroconf");
562 while ((s
= pa_hashmap_iterate(u
->services
, &state
, NULL
))) {
563 if (s
->entry_group
) {
565 avahi_entry_group_free(s
->entry_group
);
566 s
->entry_group
= NULL
;
568 avahi_entry_group_reset(s
->entry_group
);
571 s
->published
= UNPUBLISHED
;
574 if (u
->main_entry_group
) {
576 avahi_entry_group_free(u
->main_entry_group
);
577 u
->main_entry_group
= NULL
;
579 avahi_entry_group_reset(u
->main_entry_group
);
583 static void client_callback(AvahiClient
*c
, AvahiClientState state
, void *userdata
) {
584 struct userdata
*u
= userdata
;
592 case AVAHI_CLIENT_S_RUNNING
:
593 publish_all_services(u
);
596 case AVAHI_CLIENT_S_COLLISION
:
597 unpublish_all_services(u
, 0);
600 case AVAHI_CLIENT_FAILURE
:
601 if (avahi_client_errno(c
) == AVAHI_ERR_DISCONNECTED
) {
603 unpublish_all_services(u
, 1);
604 avahi_client_free(u
->client
);
606 if (!(u
->client
= avahi_client_new(u
->avahi_poll
, AVAHI_CLIENT_NO_FAIL
, client_callback
, u
, &error
)))
607 pa_log("pa_avahi_client_new() failed: %s", avahi_strerror(error
));
616 int pa__init(pa_module
*m
) {
619 uint32_t port
= PA_NATIVE_DEFAULT_PORT
;
620 pa_modargs
*ma
= NULL
;
621 char hn
[256], un
[256];
624 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
625 pa_log("failed to parse module arguments.");
629 if (pa_modargs_get_value_u32(ma
, "port", &port
) < 0 || port
<= 0 || port
> 0xFFFF) {
630 pa_log("invalid port specified.");
634 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
636 u
->port
= (uint16_t) port
;
638 u
->avahi_poll
= pa_avahi_poll_new(m
->core
->mainloop
);
640 u
->services
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
641 u
->sink_dynarray
= pa_dynarray_new();
642 u
->source_dynarray
= pa_dynarray_new();
643 u
->autoload_dynarray
= pa_dynarray_new();
645 u
->subscription
= pa_subscription_new(m
->core
,
646 PA_SUBSCRIPTION_MASK_SINK
|
647 PA_SUBSCRIPTION_MASK_SOURCE
|
648 PA_SUBSCRIPTION_MASK_AUTOLOAD
, subscribe_callback
, u
);
650 u
->main_entry_group
= NULL
;
652 u
->service_name
= pa_truncate_utf8(pa_sprintf_malloc("%s@%s", pa_get_user_name(un
, sizeof(un
)), pa_get_host_name(hn
, sizeof(hn
))), AVAHI_LABEL_MAX
);
654 if (!(u
->client
= avahi_client_new(u
->avahi_poll
, AVAHI_CLIENT_NO_FAIL
, client_callback
, u
, &error
))) {
655 pa_log("pa_avahi_client_new() failed: %s", avahi_strerror(error
));
672 static void service_free(void *p
, void *userdata
) {
673 struct service
*s
= p
;
674 struct userdata
*u
= userdata
;
680 avahi_entry_group_free(s
->entry_group
);
682 pa_xfree(s
->service_name
);
687 void pa__done(pa_module
*m
) {
691 if (!(u
= m
->userdata
))
695 pa_hashmap_free(u
->services
, service_free
, u
);
698 pa_subscription_free(u
->subscription
);
700 if (u
->sink_dynarray
)
701 pa_dynarray_free(u
->sink_dynarray
, NULL
, NULL
);
702 if (u
->source_dynarray
)
703 pa_dynarray_free(u
->source_dynarray
, NULL
, NULL
);
704 if (u
->autoload_dynarray
)
705 pa_dynarray_free(u
->autoload_dynarray
, NULL
, NULL
);
707 if (u
->main_entry_group
)
708 avahi_entry_group_free(u
->main_entry_group
);
711 avahi_client_free(u
->client
);
714 pa_avahi_poll_free(u
->avahi_poll
);
716 pa_xfree(u
->service_name
);