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(__FILE__
": 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(__FILE__
": 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 */
228 if (s
->entry_group
) {
229 avahi_entry_group_free(s
->entry_group
);
232 pa_hashmap_remove(u
->services
, s
->name
);
234 pa_xfree(s
->service_name
);
239 avahi_string_list_free(txt
);
244 static struct service
*get_service(struct userdata
*u
, const char *name
) {
248 if ((s
= pa_hashmap_get(u
->services
, name
)))
251 s
= pa_xnew(struct service
, 1);
253 s
->entry_group
= NULL
;
254 s
->published
= UNPUBLISHED
;
255 s
->name
= pa_xstrdup(name
);
256 s
->loaded
.valid
= s
->autoload
.valid
= 0;
257 s
->service_name
= pa_sprintf_malloc("%s on %s", s
->name
, pa_get_host_name(hn
, sizeof(hn
)));
259 pa_hashmap_put(u
->services
, s
->name
, s
);
264 static int publish_sink(struct userdata
*u
, pa_sink
*s
) {
268 svc
= get_service(u
, s
->name
);
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 pa_dynarray_put(u
->sink_dynarray
, s
->index
, svc
);
278 return publish_service(u
, svc
);
281 static int publish_source(struct userdata
*u
, pa_source
*s
) {
285 svc
= get_service(u
, s
->name
);
286 if (svc
->loaded
.valid
)
287 return publish_service(u
, svc
);
289 svc
->loaded
.valid
= 1;
290 svc
->loaded
.type
= PA_NAMEREG_SOURCE
;
291 svc
->loaded
.index
= s
->index
;
293 pa_dynarray_put(u
->source_dynarray
, s
->index
, svc
);
295 return publish_service(u
, svc
);
298 static int publish_autoload(struct userdata
*u
, pa_autoload_entry
*s
) {
302 svc
= get_service(u
, s
->name
);
303 if (svc
->autoload
.valid
)
304 return publish_service(u
, svc
);
306 svc
->autoload
.valid
= 1;
307 svc
->autoload
.type
= s
->type
;
308 svc
->autoload
.index
= s
->index
;
310 pa_dynarray_put(u
->autoload_dynarray
, s
->index
, svc
);
312 return publish_service(u
, svc
);
315 static int remove_sink(struct userdata
*u
, uint32_t idx
) {
317 assert(u
&& idx
!= PA_INVALID_INDEX
);
319 if (!(svc
= pa_dynarray_get(u
->sink_dynarray
, idx
)))
322 if (!svc
->loaded
.valid
|| svc
->loaded
.type
!= PA_NAMEREG_SINK
)
325 svc
->loaded
.valid
= 0;
326 pa_dynarray_put(u
->sink_dynarray
, idx
, NULL
);
328 return publish_service(u
, svc
);
331 static int remove_source(struct userdata
*u
, uint32_t idx
) {
333 assert(u
&& idx
!= PA_INVALID_INDEX
);
335 if (!(svc
= pa_dynarray_get(u
->source_dynarray
, idx
)))
338 if (!svc
->loaded
.valid
|| svc
->loaded
.type
!= PA_NAMEREG_SOURCE
)
341 svc
->loaded
.valid
= 0;
342 pa_dynarray_put(u
->source_dynarray
, idx
, NULL
);
344 return publish_service(u
, svc
);
347 static int remove_autoload(struct userdata
*u
, uint32_t idx
) {
349 assert(u
&& idx
!= PA_INVALID_INDEX
);
351 if (!(svc
= pa_dynarray_get(u
->autoload_dynarray
, idx
)))
354 if (!svc
->autoload
.valid
)
357 svc
->autoload
.valid
= 0;
358 pa_dynarray_put(u
->autoload_dynarray
, idx
, NULL
);
360 return publish_service(u
, svc
);
363 static void subscribe_callback(pa_core
*c
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
364 struct userdata
*u
= userdata
;
367 switch (t
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
)
368 case PA_SUBSCRIPTION_EVENT_SINK
: {
369 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_NEW
) {
372 if ((sink
= pa_idxset_get_by_index(c
->sinks
, idx
))) {
373 if (publish_sink(u
, sink
) < 0)
376 } else if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_REMOVE
) {
377 if (remove_sink(u
, idx
) < 0)
383 case PA_SUBSCRIPTION_EVENT_SOURCE
:
385 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_NEW
) {
388 if ((source
= pa_idxset_get_by_index(c
->sources
, idx
))) {
389 if (publish_source(u
, source
) < 0)
392 } else if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_REMOVE
) {
393 if (remove_source(u
, idx
) < 0)
399 case PA_SUBSCRIPTION_EVENT_AUTOLOAD
:
400 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_NEW
) {
401 pa_autoload_entry
*autoload
;
403 if ((autoload
= pa_idxset_get_by_index(c
->autoload_idxset
, idx
))) {
404 if (publish_autoload(u
, autoload
) < 0)
407 } else if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_REMOVE
) {
408 if (remove_autoload(u
, idx
) < 0)
418 if (u
->subscription
) {
419 pa_subscription_free(u
->subscription
);
420 u
->subscription
= NULL
;
424 static int publish_main_service(struct userdata
*u
);
426 static void main_entry_group_callback(AvahiEntryGroup
*g
, AvahiEntryGroupState state
, void *userdata
) {
427 struct userdata
*u
= userdata
;
430 if (state
== AVAHI_ENTRY_GROUP_COLLISION
) {
433 t
= avahi_alternative_service_name(u
->service_name
);
434 pa_xfree(u
->service_name
);
437 publish_main_service(u
);
441 static int publish_main_service(struct userdata
*u
) {
442 AvahiStringList
*txt
= NULL
;
445 if (!u
->main_entry_group
) {
446 if (!(u
->main_entry_group
= avahi_entry_group_new(u
->client
, main_entry_group_callback
, u
))) {
447 pa_log(__FILE__
": avahi_entry_group_new() failed: %s", avahi_strerror(avahi_client_errno(u
->client
)));
451 avahi_entry_group_reset(u
->main_entry_group
);
453 txt
= txt_record_server_data(u
->core
, NULL
);
455 if (avahi_entry_group_add_service_strlst(
457 AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
,
466 pa_log(__FILE__
": avahi_entry_group_add_service_strlst() failed: %s", avahi_strerror(avahi_client_errno(u
->client
)));
470 if (avahi_entry_group_commit(u
->main_entry_group
) < 0) {
471 pa_log(__FILE__
": avahi_entry_group_commit() failed: %s", avahi_strerror(avahi_client_errno(u
->client
)));
478 avahi_string_list_free(txt
);
483 static int publish_all_services(struct userdata
*u
) {
486 pa_autoload_entry
*autoload
;
492 pa_log_debug(__FILE__
": Publishing services in Zeroconf");
494 for (sink
= pa_idxset_first(u
->core
->sinks
, &idx
); sink
; sink
= pa_idxset_next(u
->core
->sinks
, &idx
))
495 if (publish_sink(u
, sink
) < 0)
498 for (source
= pa_idxset_first(u
->core
->sources
, &idx
); source
; source
= pa_idxset_next(u
->core
->sources
, &idx
))
499 if (publish_source(u
, source
) < 0)
502 if (u
->core
->autoload_idxset
)
503 for (autoload
= pa_idxset_first(u
->core
->autoload_idxset
, &idx
); autoload
; autoload
= pa_idxset_next(u
->core
->autoload_idxset
, &idx
))
504 if (publish_autoload(u
, autoload
) < 0)
507 if (publish_main_service(u
) < 0)
516 static void unpublish_all_services(struct userdata
*u
, int rem
) {
522 pa_log_debug(__FILE__
": Unpublishing services in Zeroconf");
524 while ((s
= pa_hashmap_iterate(u
->services
, &state
, NULL
))) {
525 if (s
->entry_group
) {
527 avahi_entry_group_free(s
->entry_group
);
528 s
->entry_group
= NULL
;
530 avahi_entry_group_reset(s
->entry_group
);
533 s
->published
= UNPUBLISHED
;
536 if (u
->main_entry_group
) {
538 avahi_entry_group_free(u
->main_entry_group
);
539 u
->main_entry_group
= NULL
;
541 avahi_entry_group_reset(u
->main_entry_group
);
545 static void client_callback(AvahiClient
*c
, AvahiClientState state
, void *userdata
) {
546 struct userdata
*u
= userdata
;
552 case AVAHI_CLIENT_S_RUNNING
:
553 publish_all_services(u
);
556 case AVAHI_CLIENT_S_COLLISION
:
557 unpublish_all_services(u
, 0);
560 case AVAHI_CLIENT_FAILURE
:
561 if (avahi_client_errno(c
) == AVAHI_ERR_DISCONNECTED
) {
563 unpublish_all_services(u
, 1);
564 avahi_client_free(u
->client
);
566 if (!(u
->client
= avahi_client_new(u
->avahi_poll
, AVAHI_CLIENT_NO_FAIL
, client_callback
, u
, &error
)))
567 pa_log(__FILE__
": pa_avahi_client_new() failed: %s", avahi_strerror(error
));
576 int pa__init(pa_core
*c
, pa_module
*m
) {
578 uint32_t port
= PA_NATIVE_DEFAULT_PORT
;
579 pa_modargs
*ma
= NULL
;
583 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
584 pa_log(__FILE__
": failed to parse module arguments.");
588 if (pa_modargs_get_value_u32(ma
, "port", &port
) < 0 || port
== 0 || port
>= 0xFFFF) {
589 pa_log(__FILE__
": invalid port specified.");
593 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
595 u
->port
= (uint16_t) port
;
597 u
->avahi_poll
= pa_avahi_poll_new(c
->mainloop
);
599 u
->services
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
600 u
->sink_dynarray
= pa_dynarray_new();
601 u
->source_dynarray
= pa_dynarray_new();
602 u
->autoload_dynarray
= pa_dynarray_new();
604 u
->subscription
= pa_subscription_new(c
,
605 PA_SUBSCRIPTION_MASK_SINK
|
606 PA_SUBSCRIPTION_MASK_SOURCE
|
607 PA_SUBSCRIPTION_MASK_AUTOLOAD
, subscribe_callback
, u
);
609 u
->main_entry_group
= NULL
;
611 u
->service_name
= pa_xstrdup(pa_get_host_name(hn
, sizeof(hn
)));
613 if (!(u
->client
= avahi_client_new(u
->avahi_poll
, AVAHI_CLIENT_NO_FAIL
, client_callback
, u
, &error
))) {
614 pa_log(__FILE__
": pa_avahi_client_new() failed: %s", avahi_strerror(error
));
631 static void service_free(void *p
, void *userdata
) {
632 struct service
*s
= p
;
633 struct userdata
*u
= userdata
;
639 avahi_entry_group_free(s
->entry_group
);
641 pa_xfree(s
->service_name
);
646 void pa__done(pa_core
*c
, pa_module
*m
) {
650 if (!(u
= m
->userdata
))
654 pa_hashmap_free(u
->services
, service_free
, u
);
656 if (u
->sink_dynarray
)
657 pa_dynarray_free(u
->sink_dynarray
, NULL
, NULL
);
658 if (u
->source_dynarray
)
659 pa_dynarray_free(u
->source_dynarray
, NULL
, NULL
);
660 if (u
->autoload_dynarray
)
661 pa_dynarray_free(u
->autoload_dynarray
, NULL
, NULL
);
664 pa_subscription_free(u
->subscription
);
666 if (u
->main_entry_group
)
667 avahi_entry_group_free(u
->main_entry_group
);
670 avahi_client_free(u
->client
);
673 pa_avahi_poll_free(u
->avahi_poll
);
675 pa_xfree(u
->service_name
);