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>
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
) {
105 assert(u
&& s
&& s
->loaded
.valid
&& ret_ss
&& ret_description
);
107 if (s
->loaded
.type
== PA_NAMEREG_SINK
) {
108 pa_sink
*sink
= pa_idxset_get_by_index(u
->core
->sinks
, s
->loaded
.index
);
110 *ret_ss
= sink
->sample_spec
;
111 *ret_description
= sink
->description
;
112 } else if (s
->loaded
.type
== PA_NAMEREG_SOURCE
) {
113 pa_source
*source
= pa_idxset_get_by_index(u
->core
->sources
, s
->loaded
.index
);
115 *ret_ss
= source
->sample_spec
;
116 *ret_description
= source
->description
;
121 static AvahiStringList
* txt_record_server_data(pa_core
*c
, AvahiStringList
*l
) {
125 l
= avahi_string_list_add_pair(l
, "server-version", PACKAGE_NAME
" "PACKAGE_VERSION
);
126 l
= avahi_string_list_add_pair(l
, "user-name", pa_get_user_name(s
, sizeof(s
)));
127 l
= avahi_string_list_add_pair(l
, "fqdn", pa_get_fqdn(s
, sizeof(s
)));
128 l
= avahi_string_list_add_printf(l
, "cookie=0x%08x", c
->cookie
);
133 static int publish_service(struct userdata
*u
, struct service
*s
);
135 static void service_entry_group_callback(AvahiEntryGroup
*g
, AvahiEntryGroupState state
, void *userdata
) {
136 struct service
*s
= userdata
;
138 if (state
== AVAHI_ENTRY_GROUP_COLLISION
) {
141 t
= avahi_alternative_service_name(s
->service_name
);
142 pa_xfree(s
->service_name
);
145 publish_service(s
->userdata
, s
);
149 static int publish_service(struct userdata
*u
, struct service
*s
) {
151 AvahiStringList
*txt
= NULL
;
156 if (!u
->client
|| avahi_client_get_state(u
->client
) != AVAHI_CLIENT_S_RUNNING
)
159 if ((s
->published
== PUBLISHED_REAL
&& s
->loaded
.valid
) ||
160 (s
->published
== PUBLISHED_AUTOLOAD
&& s
->autoload
.valid
&& !s
->loaded
.valid
))
163 if (s
->published
!= UNPUBLISHED
) {
164 avahi_entry_group_reset(s
->entry_group
);
165 s
->published
= UNPUBLISHED
;
168 if (s
->loaded
.valid
|| s
->autoload
.valid
) {
169 pa_namereg_type_t type
;
171 if (!s
->entry_group
) {
172 if (!(s
->entry_group
= avahi_entry_group_new(u
->client
, service_entry_group_callback
, s
))) {
173 pa_log("avahi_entry_group_new(): %s", avahi_strerror(avahi_client_errno(u
->client
)));
178 txt
= avahi_string_list_add_pair(txt
, "device", s
->name
);
179 txt
= txt_record_server_data(u
->core
, txt
);
181 if (s
->loaded
.valid
) {
185 get_service_data(u
, s
, &ss
, &description
);
187 txt
= avahi_string_list_add_printf(txt
, "rate=%u", ss
.rate
);
188 txt
= avahi_string_list_add_printf(txt
, "channels=%u", ss
.channels
);
189 txt
= avahi_string_list_add_pair(txt
, "format", pa_sample_format_to_string(ss
.format
));
191 txt
= avahi_string_list_add_pair(txt
, "description", description
);
193 type
= s
->loaded
.type
;
194 } else if (s
->autoload
.valid
)
195 type
= s
->autoload
.type
;
197 if (avahi_entry_group_add_service_strlst(
199 AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
,
202 type
== PA_NAMEREG_SINK
? SERVICE_TYPE_SINK
: SERVICE_TYPE_SOURCE
,
208 pa_log("avahi_entry_group_add_service_strlst(): %s", avahi_strerror(avahi_client_errno(u
->client
)));
212 if (avahi_entry_group_commit(s
->entry_group
) < 0) {
213 pa_log("avahi_entry_group_commit(): %s", avahi_strerror(avahi_client_errno(u
->client
)));
218 s
->published
= PUBLISHED_REAL
;
219 else if (s
->autoload
.valid
)
220 s
->published
= PUBLISHED_AUTOLOAD
;
227 if (s
->published
== UNPUBLISHED
) {
228 /* Remove this service */
231 avahi_entry_group_free(s
->entry_group
);
233 pa_hashmap_remove(u
->services
, s
->name
);
235 pa_xfree(s
->service_name
);
240 avahi_string_list_free(txt
);
245 static struct service
*get_service(struct userdata
*u
, const char *name
, const char *description
) {
249 if ((s
= pa_hashmap_get(u
->services
, name
)))
252 s
= pa_xnew(struct service
, 1);
254 s
->entry_group
= NULL
;
255 s
->published
= UNPUBLISHED
;
256 s
->name
= pa_xstrdup(name
);
257 s
->loaded
.valid
= s
->autoload
.valid
= 0;
258 s
->service_name
= pa_sprintf_malloc("%s on %s", description
? description
: s
->name
, pa_get_host_name(hn
, sizeof(hn
)));
260 pa_hashmap_put(u
->services
, s
->name
, s
);
265 static int publish_sink(struct userdata
*u
, pa_sink
*s
) {
270 svc
= get_service(u
, s
->name
, s
->description
);
271 if (svc
->loaded
.valid
)
272 return publish_service(u
, svc
);
274 svc
->loaded
.valid
= 1;
275 svc
->loaded
.type
= PA_NAMEREG_SINK
;
276 svc
->loaded
.index
= s
->index
;
278 if ((ret
= publish_service(u
, svc
)) < 0)
281 pa_dynarray_put(u
->sink_dynarray
, s
->index
, svc
);
285 static int publish_source(struct userdata
*u
, pa_source
*s
) {
291 svc
= get_service(u
, s
->name
, s
->description
);
292 if (svc
->loaded
.valid
)
293 return publish_service(u
, svc
);
295 svc
->loaded
.valid
= 1;
296 svc
->loaded
.type
= PA_NAMEREG_SOURCE
;
297 svc
->loaded
.index
= s
->index
;
299 pa_dynarray_put(u
->source_dynarray
, s
->index
, svc
);
301 if ((ret
= publish_service(u
, svc
)) < 0)
304 pa_dynarray_put(u
->sink_dynarray
, s
->index
, svc
);
308 static int publish_autoload(struct userdata
*u
, pa_autoload_entry
*s
) {
314 svc
= get_service(u
, s
->name
, NULL
);
315 if (svc
->autoload
.valid
)
316 return publish_service(u
, svc
);
318 svc
->autoload
.valid
= 1;
319 svc
->autoload
.type
= s
->type
;
320 svc
->autoload
.index
= s
->index
;
322 if ((ret
= publish_service(u
, svc
)) < 0)
325 pa_dynarray_put(u
->autoload_dynarray
, s
->index
, svc
);
329 static int remove_sink(struct userdata
*u
, uint32_t idx
) {
331 assert(u
&& idx
!= PA_INVALID_INDEX
);
333 if (!(svc
= pa_dynarray_get(u
->sink_dynarray
, idx
)))
336 if (!svc
->loaded
.valid
|| svc
->loaded
.type
!= PA_NAMEREG_SINK
)
339 svc
->loaded
.valid
= 0;
340 pa_dynarray_put(u
->sink_dynarray
, idx
, NULL
);
342 return publish_service(u
, svc
);
345 static int remove_source(struct userdata
*u
, uint32_t idx
) {
347 assert(u
&& idx
!= PA_INVALID_INDEX
);
349 if (!(svc
= pa_dynarray_get(u
->source_dynarray
, idx
)))
352 if (!svc
->loaded
.valid
|| svc
->loaded
.type
!= PA_NAMEREG_SOURCE
)
355 svc
->loaded
.valid
= 0;
356 pa_dynarray_put(u
->source_dynarray
, idx
, NULL
);
358 return publish_service(u
, svc
);
361 static int remove_autoload(struct userdata
*u
, uint32_t idx
) {
363 assert(u
&& idx
!= PA_INVALID_INDEX
);
365 if (!(svc
= pa_dynarray_get(u
->autoload_dynarray
, idx
)))
368 if (!svc
->autoload
.valid
)
371 svc
->autoload
.valid
= 0;
372 pa_dynarray_put(u
->autoload_dynarray
, idx
, NULL
);
374 return publish_service(u
, svc
);
377 static void subscribe_callback(pa_core
*c
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
378 struct userdata
*u
= userdata
;
381 switch (t
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
)
382 case PA_SUBSCRIPTION_EVENT_SINK
: {
383 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_NEW
) {
386 if ((sink
= pa_idxset_get_by_index(c
->sinks
, idx
))) {
387 if (publish_sink(u
, sink
) < 0)
390 } else if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_REMOVE
) {
391 if (remove_sink(u
, idx
) < 0)
397 case PA_SUBSCRIPTION_EVENT_SOURCE
:
399 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_NEW
) {
402 if ((source
= pa_idxset_get_by_index(c
->sources
, idx
))) {
403 if (publish_source(u
, source
) < 0)
406 } else if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_REMOVE
) {
407 if (remove_source(u
, idx
) < 0)
413 case PA_SUBSCRIPTION_EVENT_AUTOLOAD
:
414 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_NEW
) {
415 pa_autoload_entry
*autoload
;
417 if ((autoload
= pa_idxset_get_by_index(c
->autoload_idxset
, idx
))) {
418 if (publish_autoload(u
, autoload
) < 0)
421 } else if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_REMOVE
) {
422 if (remove_autoload(u
, idx
) < 0)
432 if (u
->subscription
) {
433 pa_subscription_free(u
->subscription
);
434 u
->subscription
= NULL
;
438 static int publish_main_service(struct userdata
*u
);
440 static void main_entry_group_callback(AvahiEntryGroup
*g
, AvahiEntryGroupState state
, void *userdata
) {
441 struct userdata
*u
= userdata
;
444 if (state
== AVAHI_ENTRY_GROUP_COLLISION
) {
447 t
= avahi_alternative_service_name(u
->service_name
);
448 pa_xfree(u
->service_name
);
451 publish_main_service(u
);
455 static int publish_main_service(struct userdata
*u
) {
456 AvahiStringList
*txt
= NULL
;
459 if (!u
->main_entry_group
) {
460 if (!(u
->main_entry_group
= avahi_entry_group_new(u
->client
, main_entry_group_callback
, u
))) {
461 pa_log("avahi_entry_group_new() failed: %s", avahi_strerror(avahi_client_errno(u
->client
)));
465 avahi_entry_group_reset(u
->main_entry_group
);
467 txt
= txt_record_server_data(u
->core
, NULL
);
469 if (avahi_entry_group_add_service_strlst(
471 AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
,
480 pa_log("avahi_entry_group_add_service_strlst() failed: %s", avahi_strerror(avahi_client_errno(u
->client
)));
484 if (avahi_entry_group_commit(u
->main_entry_group
) < 0) {
485 pa_log("avahi_entry_group_commit() failed: %s", avahi_strerror(avahi_client_errno(u
->client
)));
492 avahi_string_list_free(txt
);
497 static int publish_all_services(struct userdata
*u
) {
500 pa_autoload_entry
*autoload
;
506 pa_log_debug("Publishing services in Zeroconf");
508 for (sink
= pa_idxset_first(u
->core
->sinks
, &idx
); sink
; sink
= pa_idxset_next(u
->core
->sinks
, &idx
))
509 if (publish_sink(u
, sink
) < 0)
512 for (source
= pa_idxset_first(u
->core
->sources
, &idx
); source
; source
= pa_idxset_next(u
->core
->sources
, &idx
))
513 if (publish_source(u
, source
) < 0)
516 if (u
->core
->autoload_idxset
)
517 for (autoload
= pa_idxset_first(u
->core
->autoload_idxset
, &idx
); autoload
; autoload
= pa_idxset_next(u
->core
->autoload_idxset
, &idx
))
518 if (publish_autoload(u
, autoload
) < 0)
521 if (publish_main_service(u
) < 0)
530 static void unpublish_all_services(struct userdata
*u
, int rem
) {
536 pa_log_debug("Unpublishing services in Zeroconf");
538 while ((s
= pa_hashmap_iterate(u
->services
, &state
, NULL
))) {
539 if (s
->entry_group
) {
541 avahi_entry_group_free(s
->entry_group
);
542 s
->entry_group
= NULL
;
544 avahi_entry_group_reset(s
->entry_group
);
547 s
->published
= UNPUBLISHED
;
550 if (u
->main_entry_group
) {
552 avahi_entry_group_free(u
->main_entry_group
);
553 u
->main_entry_group
= NULL
;
555 avahi_entry_group_reset(u
->main_entry_group
);
559 static void client_callback(AvahiClient
*c
, AvahiClientState state
, void *userdata
) {
560 struct userdata
*u
= userdata
;
566 case AVAHI_CLIENT_S_RUNNING
:
567 publish_all_services(u
);
570 case AVAHI_CLIENT_S_COLLISION
:
571 unpublish_all_services(u
, 0);
574 case AVAHI_CLIENT_FAILURE
:
575 if (avahi_client_errno(c
) == AVAHI_ERR_DISCONNECTED
) {
577 unpublish_all_services(u
, 1);
578 avahi_client_free(u
->client
);
580 if (!(u
->client
= avahi_client_new(u
->avahi_poll
, AVAHI_CLIENT_NO_FAIL
, client_callback
, u
, &error
)))
581 pa_log("pa_avahi_client_new() failed: %s", avahi_strerror(error
));
590 int pa__init(pa_core
*c
, pa_module
*m
) {
592 uint32_t port
= PA_NATIVE_DEFAULT_PORT
;
593 pa_modargs
*ma
= NULL
;
597 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
598 pa_log("failed to parse module arguments.");
602 if (pa_modargs_get_value_u32(ma
, "port", &port
) < 0 || port
== 0 || port
>= 0xFFFF) {
603 pa_log("invalid port specified.");
607 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
609 u
->port
= (uint16_t) port
;
611 u
->avahi_poll
= pa_avahi_poll_new(c
->mainloop
);
613 u
->services
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
614 u
->sink_dynarray
= pa_dynarray_new();
615 u
->source_dynarray
= pa_dynarray_new();
616 u
->autoload_dynarray
= pa_dynarray_new();
618 u
->subscription
= pa_subscription_new(c
,
619 PA_SUBSCRIPTION_MASK_SINK
|
620 PA_SUBSCRIPTION_MASK_SOURCE
|
621 PA_SUBSCRIPTION_MASK_AUTOLOAD
, subscribe_callback
, u
);
623 u
->main_entry_group
= NULL
;
625 u
->service_name
= pa_xstrdup(pa_get_host_name(hn
, sizeof(hn
)));
627 if (!(u
->client
= avahi_client_new(u
->avahi_poll
, AVAHI_CLIENT_NO_FAIL
, client_callback
, u
, &error
))) {
628 pa_log("pa_avahi_client_new() failed: %s", avahi_strerror(error
));
645 static void service_free(void *p
, void *userdata
) {
646 struct service
*s
= p
;
647 struct userdata
*u
= userdata
;
653 avahi_entry_group_free(s
->entry_group
);
655 pa_xfree(s
->service_name
);
660 void pa__done(pa_core
*c
, pa_module
*m
) {
664 if (!(u
= m
->userdata
))
668 pa_hashmap_free(u
->services
, service_free
, u
);
671 pa_subscription_free(u
->subscription
);
673 if (u
->sink_dynarray
)
674 pa_dynarray_free(u
->sink_dynarray
, NULL
, NULL
);
675 if (u
->source_dynarray
)
676 pa_dynarray_free(u
->source_dynarray
, NULL
, NULL
);
677 if (u
->autoload_dynarray
)
678 pa_dynarray_free(u
->autoload_dynarray
, NULL
, NULL
);
681 if (u
->main_entry_group
)
682 avahi_entry_group_free(u
->main_entry_group
);
685 avahi_client_free(u
->client
);
688 pa_avahi_poll_free(u
->avahi_poll
);
690 pa_xfree(u
->service_name
);