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
33 #include <avahi-client/client.h>
34 #include <avahi-client/publish.h>
35 #include <avahi-common/alternative.h>
36 #include <avahi-common/error.h>
37 #include <avahi-common/domain.h>
39 #include <pulse/xmalloc.h>
40 #include <pulse/util.h>
42 #include <pulsecore/autoload.h>
43 #include <pulsecore/sink.h>
44 #include <pulsecore/source.h>
45 #include <pulsecore/native-common.h>
46 #include <pulsecore/core-util.h>
47 #include <pulsecore/log.h>
48 #include <pulsecore/core-subscribe.h>
49 #include <pulsecore/dynarray.h>
50 #include <pulsecore/modargs.h>
51 #include <pulsecore/avahi-wrap.h>
52 #include <pulsecore/endianmacros.h>
54 #include "module-zeroconf-publish-symdef.h"
56 PA_MODULE_AUTHOR("Lennart Poettering")
57 PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Publisher")
58 PA_MODULE_VERSION(PACKAGE_VERSION
)
59 PA_MODULE_USAGE("port=<IP port number>")
61 #define SERVICE_TYPE_SINK "_pulse-sink._tcp"
62 #define SERVICE_TYPE_SOURCE "_pulse-source._tcp"
63 #define SERVICE_TYPE_SERVER "_pulse-server._tcp"
65 static const char* const valid_modargs
[] = {
71 struct userdata
*userdata
;
72 AvahiEntryGroup
*entry_group
;
75 enum { UNPUBLISHED
, PUBLISHED_REAL
, PUBLISHED_AUTOLOAD
} published
;
79 pa_namereg_type_t type
;
85 pa_namereg_type_t type
;
92 AvahiPoll
*avahi_poll
;
95 pa_dynarray
*sink_dynarray
, *source_dynarray
, *autoload_dynarray
;
96 pa_subscription
*subscription
;
99 AvahiEntryGroup
*main_entry_group
;
104 static void get_service_data(struct userdata
*u
, struct service
*s
, pa_sample_spec
*ret_ss
, char **ret_description
) {
107 pa_assert(s
->loaded
.valid
);
109 pa_assert(ret_description
);
111 if (s
->loaded
.type
== PA_NAMEREG_SINK
) {
112 pa_sink
*sink
= PA_SINK(pa_idxset_get_by_index(u
->core
->sinks
, s
->loaded
.index
));
113 pa_sink_assert_ref(sink
);
114 *ret_ss
= sink
->sample_spec
;
115 *ret_description
= sink
->description
;
117 } else if (s
->loaded
.type
== PA_NAMEREG_SOURCE
) {
118 pa_source
*source
= PA_SOURCE(pa_idxset_get_by_index(u
->core
->sources
, s
->loaded
.index
));
119 pa_source_assert_ref(source
);
120 *ret_ss
= source
->sample_spec
;
121 *ret_description
= source
->description
;
123 pa_assert_not_reached();
126 static AvahiStringList
* txt_record_server_data(pa_core
*c
, AvahiStringList
*l
) {
129 pa_core_assert_ref(c
);
131 l
= avahi_string_list_add_pair(l
, "server-version", PACKAGE_NAME
" "PACKAGE_VERSION
);
132 l
= avahi_string_list_add_pair(l
, "user-name", pa_get_user_name(s
, sizeof(s
)));
133 l
= avahi_string_list_add_pair(l
, "fqdn", pa_get_fqdn(s
, sizeof(s
)));
134 l
= avahi_string_list_add_printf(l
, "cookie=0x%08x", c
->cookie
);
139 static int publish_service(struct userdata
*u
, struct service
*s
);
141 static void service_entry_group_callback(AvahiEntryGroup
*g
, AvahiEntryGroupState state
, void *userdata
) {
142 struct service
*s
= userdata
;
146 if (state
== AVAHI_ENTRY_GROUP_COLLISION
) {
149 t
= avahi_alternative_service_name(s
->service_name
);
150 pa_xfree(s
->service_name
);
153 publish_service(s
->userdata
, s
);
157 static int publish_service(struct userdata
*u
, struct service
*s
) {
159 AvahiStringList
*txt
= NULL
;
164 if (!u
->client
|| avahi_client_get_state(u
->client
) != AVAHI_CLIENT_S_RUNNING
)
167 if ((s
->published
== PUBLISHED_REAL
&& s
->loaded
.valid
) ||
168 (s
->published
== PUBLISHED_AUTOLOAD
&& s
->autoload
.valid
&& !s
->loaded
.valid
))
171 if (s
->published
!= UNPUBLISHED
) {
172 avahi_entry_group_reset(s
->entry_group
);
173 s
->published
= UNPUBLISHED
;
176 if (s
->loaded
.valid
|| s
->autoload
.valid
) {
177 pa_namereg_type_t type
;
179 if (!s
->entry_group
) {
180 if (!(s
->entry_group
= avahi_entry_group_new(u
->client
, service_entry_group_callback
, s
))) {
181 pa_log("avahi_entry_group_new(): %s", avahi_strerror(avahi_client_errno(u
->client
)));
186 txt
= avahi_string_list_add_pair(txt
, "device", s
->name
);
187 txt
= txt_record_server_data(u
->core
, txt
);
189 if (s
->loaded
.valid
) {
193 get_service_data(u
, s
, &ss
, &description
);
195 txt
= avahi_string_list_add_printf(txt
, "rate=%u", ss
.rate
);
196 txt
= avahi_string_list_add_printf(txt
, "channels=%u", ss
.channels
);
197 txt
= avahi_string_list_add_pair(txt
, "format", pa_sample_format_to_string(ss
.format
));
199 txt
= avahi_string_list_add_pair(txt
, "description", description
);
201 type
= s
->loaded
.type
;
202 } else if (s
->autoload
.valid
)
203 type
= s
->autoload
.type
;
205 if (avahi_entry_group_add_service_strlst(
207 AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
,
210 type
== PA_NAMEREG_SINK
? SERVICE_TYPE_SINK
: SERVICE_TYPE_SOURCE
,
216 pa_log("avahi_entry_group_add_service_strlst(): %s", avahi_strerror(avahi_client_errno(u
->client
)));
220 if (avahi_entry_group_commit(s
->entry_group
) < 0) {
221 pa_log("avahi_entry_group_commit(): %s", avahi_strerror(avahi_client_errno(u
->client
)));
226 s
->published
= PUBLISHED_REAL
;
227 else if (s
->autoload
.valid
)
228 s
->published
= PUBLISHED_AUTOLOAD
;
235 if (s
->published
== UNPUBLISHED
) {
236 /* Remove this service */
239 avahi_entry_group_free(s
->entry_group
);
241 pa_hashmap_remove(u
->services
, s
->name
);
243 pa_xfree(s
->service_name
);
248 avahi_string_list_free(txt
);
253 static struct service
*get_service(struct userdata
*u
, const char *name
, const char *description
) {
257 if ((s
= pa_hashmap_get(u
->services
, name
)))
260 s
= pa_xnew(struct service
, 1);
262 s
->entry_group
= NULL
;
263 s
->published
= UNPUBLISHED
;
264 s
->name
= pa_xstrdup(name
);
265 s
->loaded
.valid
= s
->autoload
.valid
= 0;
266 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);
268 pa_hashmap_put(u
->services
, s
->name
, s
);
273 static int publish_sink(struct userdata
*u
, pa_sink
*s
) {
278 pa_sink_assert_ref(s
);
280 svc
= get_service(u
, s
->name
, s
->description
);
281 if (svc
->loaded
.valid
)
282 return publish_service(u
, svc
);
284 svc
->loaded
.valid
= 1;
285 svc
->loaded
.type
= PA_NAMEREG_SINK
;
286 svc
->loaded
.index
= s
->index
;
288 if ((ret
= publish_service(u
, svc
)) < 0)
291 pa_dynarray_put(u
->sink_dynarray
, s
->index
, svc
);
295 static int publish_source(struct userdata
*u
, pa_source
*s
) {
300 pa_source_assert_ref(s
);
302 svc
= get_service(u
, s
->name
, s
->description
);
303 if (svc
->loaded
.valid
)
304 return publish_service(u
, svc
);
306 svc
->loaded
.valid
= 1;
307 svc
->loaded
.type
= PA_NAMEREG_SOURCE
;
308 svc
->loaded
.index
= s
->index
;
310 pa_dynarray_put(u
->source_dynarray
, s
->index
, svc
);
312 if ((ret
= publish_service(u
, svc
)) < 0)
315 pa_dynarray_put(u
->sink_dynarray
, s
->index
, svc
);
319 static int publish_autoload(struct userdata
*u
, pa_autoload_entry
*s
) {
326 svc
= get_service(u
, s
->name
, NULL
);
327 if (svc
->autoload
.valid
)
328 return publish_service(u
, svc
);
330 svc
->autoload
.valid
= 1;
331 svc
->autoload
.type
= s
->type
;
332 svc
->autoload
.index
= s
->index
;
334 if ((ret
= publish_service(u
, svc
)) < 0)
337 pa_dynarray_put(u
->autoload_dynarray
, s
->index
, svc
);
341 static int remove_sink(struct userdata
*u
, uint32_t idx
) {
345 pa_assert(idx
!= PA_INVALID_INDEX
);
347 if (!(svc
= pa_dynarray_get(u
->sink_dynarray
, idx
)))
350 if (!svc
->loaded
.valid
|| svc
->loaded
.type
!= PA_NAMEREG_SINK
)
353 svc
->loaded
.valid
= 0;
354 pa_dynarray_put(u
->sink_dynarray
, idx
, NULL
);
356 return publish_service(u
, svc
);
359 static int remove_source(struct userdata
*u
, uint32_t idx
) {
363 pa_assert(idx
!= PA_INVALID_INDEX
);
365 if (!(svc
= pa_dynarray_get(u
->source_dynarray
, idx
)))
368 if (!svc
->loaded
.valid
|| svc
->loaded
.type
!= PA_NAMEREG_SOURCE
)
371 svc
->loaded
.valid
= 0;
372 pa_dynarray_put(u
->source_dynarray
, idx
, NULL
);
374 return publish_service(u
, svc
);
377 static int remove_autoload(struct userdata
*u
, uint32_t idx
) {
381 pa_assert(idx
!= PA_INVALID_INDEX
);
383 if (!(svc
= pa_dynarray_get(u
->autoload_dynarray
, idx
)))
386 if (!svc
->autoload
.valid
)
389 svc
->autoload
.valid
= 0;
390 pa_dynarray_put(u
->autoload_dynarray
, idx
, NULL
);
392 return publish_service(u
, svc
);
395 static void subscribe_callback(pa_core
*c
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
396 struct userdata
*u
= userdata
;
399 pa_core_assert_ref(c
);
401 switch (t
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
)
403 case PA_SUBSCRIPTION_EVENT_SINK
: {
404 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_NEW
) {
407 if ((sink
= PA_SINK(pa_idxset_get_by_index(c
->sinks
, idx
)))) {
408 if (publish_sink(u
, sink
) < 0)
411 } else if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_REMOVE
) {
412 if (remove_sink(u
, idx
) < 0)
418 case PA_SUBSCRIPTION_EVENT_SOURCE
:
420 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_NEW
) {
423 if ((source
= PA_SOURCE(pa_idxset_get_by_index(c
->sources
, idx
)))) {
424 if (publish_source(u
, source
) < 0)
427 } else if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_REMOVE
) {
428 if (remove_source(u
, idx
) < 0)
434 case PA_SUBSCRIPTION_EVENT_AUTOLOAD
:
435 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_NEW
) {
436 pa_autoload_entry
*autoload
;
438 if ((autoload
= pa_idxset_get_by_index(c
->autoload_idxset
, idx
))) {
439 if (publish_autoload(u
, autoload
) < 0)
442 } else if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_REMOVE
) {
443 if (remove_autoload(u
, idx
) < 0)
453 if (u
->subscription
) {
454 pa_subscription_free(u
->subscription
);
455 u
->subscription
= NULL
;
459 static int publish_main_service(struct userdata
*u
);
461 static void main_entry_group_callback(AvahiEntryGroup
*g
, AvahiEntryGroupState state
, void *userdata
) {
462 struct userdata
*u
= userdata
;
465 if (state
== AVAHI_ENTRY_GROUP_COLLISION
) {
468 t
= avahi_alternative_service_name(u
->service_name
);
469 pa_xfree(u
->service_name
);
472 publish_main_service(u
);
476 static int publish_main_service(struct userdata
*u
) {
477 AvahiStringList
*txt
= NULL
;
482 if (!u
->main_entry_group
) {
483 if (!(u
->main_entry_group
= avahi_entry_group_new(u
->client
, main_entry_group_callback
, u
))) {
484 pa_log("avahi_entry_group_new() failed: %s", avahi_strerror(avahi_client_errno(u
->client
)));
488 avahi_entry_group_reset(u
->main_entry_group
);
490 txt
= txt_record_server_data(u
->core
, NULL
);
492 if (avahi_entry_group_add_service_strlst(
494 AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
,
503 pa_log("avahi_entry_group_add_service_strlst() failed: %s", avahi_strerror(avahi_client_errno(u
->client
)));
507 if (avahi_entry_group_commit(u
->main_entry_group
) < 0) {
508 pa_log("avahi_entry_group_commit() failed: %s", avahi_strerror(avahi_client_errno(u
->client
)));
515 avahi_string_list_free(txt
);
520 static int publish_all_services(struct userdata
*u
) {
523 pa_autoload_entry
*autoload
;
529 pa_log_debug("Publishing services in Zeroconf");
531 for (sink
= PA_SINK(pa_idxset_first(u
->core
->sinks
, &idx
)); sink
; sink
= PA_SINK(pa_idxset_next(u
->core
->sinks
, &idx
)))
532 if (publish_sink(u
, sink
) < 0)
535 for (source
= PA_SOURCE(pa_idxset_first(u
->core
->sources
, &idx
)); source
; source
= PA_SOURCE(pa_idxset_next(u
->core
->sources
, &idx
)))
536 if (publish_source(u
, source
) < 0)
539 if (u
->core
->autoload_idxset
)
540 for (autoload
= pa_idxset_first(u
->core
->autoload_idxset
, &idx
); autoload
; autoload
= pa_idxset_next(u
->core
->autoload_idxset
, &idx
))
541 if (publish_autoload(u
, autoload
) < 0)
544 if (publish_main_service(u
) < 0)
553 static void unpublish_all_services(struct userdata
*u
, int rem
) {
559 pa_log_debug("Unpublishing services in Zeroconf");
561 while ((s
= pa_hashmap_iterate(u
->services
, &state
, NULL
))) {
562 if (s
->entry_group
) {
564 avahi_entry_group_free(s
->entry_group
);
565 s
->entry_group
= NULL
;
567 avahi_entry_group_reset(s
->entry_group
);
570 s
->published
= UNPUBLISHED
;
573 if (u
->main_entry_group
) {
575 avahi_entry_group_free(u
->main_entry_group
);
576 u
->main_entry_group
= NULL
;
578 avahi_entry_group_reset(u
->main_entry_group
);
582 static void client_callback(AvahiClient
*c
, AvahiClientState state
, void *userdata
) {
583 struct userdata
*u
= userdata
;
591 case AVAHI_CLIENT_S_RUNNING
:
592 publish_all_services(u
);
595 case AVAHI_CLIENT_S_COLLISION
:
596 unpublish_all_services(u
, 0);
599 case AVAHI_CLIENT_FAILURE
:
600 if (avahi_client_errno(c
) == AVAHI_ERR_DISCONNECTED
) {
602 unpublish_all_services(u
, 1);
603 avahi_client_free(u
->client
);
605 if (!(u
->client
= avahi_client_new(u
->avahi_poll
, AVAHI_CLIENT_NO_FAIL
, client_callback
, u
, &error
)))
606 pa_log("pa_avahi_client_new() failed: %s", avahi_strerror(error
));
615 int pa__init(pa_module
*m
) {
618 uint32_t port
= PA_NATIVE_DEFAULT_PORT
;
619 pa_modargs
*ma
= NULL
;
620 char hn
[256], un
[256];
623 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
624 pa_log("failed to parse module arguments.");
628 if (pa_modargs_get_value_u32(ma
, "port", &port
) < 0 || port
<= 0 || port
> 0xFFFF) {
629 pa_log("invalid port specified.");
633 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
635 u
->port
= (uint16_t) port
;
637 u
->avahi_poll
= pa_avahi_poll_new(m
->core
->mainloop
);
639 u
->services
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
640 u
->sink_dynarray
= pa_dynarray_new();
641 u
->source_dynarray
= pa_dynarray_new();
642 u
->autoload_dynarray
= pa_dynarray_new();
644 u
->subscription
= pa_subscription_new(m
->core
,
645 PA_SUBSCRIPTION_MASK_SINK
|
646 PA_SUBSCRIPTION_MASK_SOURCE
|
647 PA_SUBSCRIPTION_MASK_AUTOLOAD
, subscribe_callback
, u
);
649 u
->main_entry_group
= NULL
;
651 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
);
653 if (!(u
->client
= avahi_client_new(u
->avahi_poll
, AVAHI_CLIENT_NO_FAIL
, client_callback
, u
, &error
))) {
654 pa_log("pa_avahi_client_new() failed: %s", avahi_strerror(error
));
671 static void service_free(void *p
, void *userdata
) {
672 struct service
*s
= p
;
673 struct userdata
*u
= userdata
;
679 avahi_entry_group_free(s
->entry_group
);
681 pa_xfree(s
->service_name
);
686 void pa__done(pa_module
*m
) {
690 if (!(u
= m
->userdata
))
694 pa_hashmap_free(u
->services
, service_free
, u
);
697 pa_subscription_free(u
->subscription
);
699 if (u
->sink_dynarray
)
700 pa_dynarray_free(u
->sink_dynarray
, NULL
, NULL
);
701 if (u
->source_dynarray
)
702 pa_dynarray_free(u
->source_dynarray
, NULL
, NULL
);
703 if (u
->autoload_dynarray
)
704 pa_dynarray_free(u
->autoload_dynarray
, NULL
, NULL
);
706 if (u
->main_entry_group
)
707 avahi_entry_group_free(u
->main_entry_group
);
710 avahi_client_free(u
->client
);
713 pa_avahi_poll_free(u
->avahi_poll
);
715 pa_xfree(u
->service_name
);