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/sink.h>
43 #include <pulsecore/source.h>
44 #include <pulsecore/native-common.h>
45 #include <pulsecore/core-util.h>
46 #include <pulsecore/log.h>
47 #include <pulsecore/core-subscribe.h>
48 #include <pulsecore/dynarray.h>
49 #include <pulsecore/modargs.h>
50 #include <pulsecore/avahi-wrap.h>
51 #include <pulsecore/endianmacros.h>
53 #include "module-zeroconf-publish-symdef.h"
55 PA_MODULE_AUTHOR("Lennart Poettering")
56 PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Publisher")
57 PA_MODULE_VERSION(PACKAGE_VERSION
)
58 PA_MODULE_USAGE("port=<IP port number>")
60 #define SERVICE_TYPE_SINK "_pulse-sink._tcp"
61 #define SERVICE_TYPE_SOURCE "_pulse-source._tcp"
62 #define SERVICE_TYPE_SERVER "_pulse-server._tcp"
64 static const char* const valid_modargs
[] = {
70 struct userdata
*userdata
;
71 AvahiEntryGroup
*entry_group
;
78 AvahiPoll
*avahi_poll
;
84 AvahiEntryGroup
*main_entry_group
;
88 pa_hook_slot
*sink_new_slot
, *source_new_slot
, *sink_unlink_slot
, *source_unlink_slot
, *sink_changed_slot
, *source_changed_slot
;
91 static void get_service_data(struct service
*s
, pa_sample_spec
*ret_ss
, pa_channel_map
*ret_map
, const char **ret_name
, const char **ret_description
) {
94 pa_assert(ret_description
);
96 if (pa_sink_isinstance(s
->device
)) {
97 pa_sink
*sink
= PA_SINK(s
->device
);
99 *ret_ss
= sink
->sample_spec
;
100 *ret_map
= sink
->channel_map
;
101 *ret_name
= sink
->name
;
102 *ret_description
= sink
->description
;
104 } else if (pa_source_isinstance(s
->device
)) {
105 pa_source
*source
= PA_SOURCE(s
->device
);
107 *ret_ss
= source
->sample_spec
;
108 *ret_map
= source
->channel_map
;
109 *ret_name
= source
->name
;
110 *ret_description
= source
->description
;
112 pa_assert_not_reached();
115 static AvahiStringList
* txt_record_server_data(pa_core
*c
, AvahiStringList
*l
) {
120 l
= avahi_string_list_add_pair(l
, "server-version", PACKAGE_NAME
" "PACKAGE_VERSION
);
121 l
= avahi_string_list_add_pair(l
, "user-name", pa_get_user_name(s
, sizeof(s
)));
122 l
= avahi_string_list_add_pair(l
, "fqdn", pa_get_fqdn(s
, sizeof(s
)));
123 l
= avahi_string_list_add_printf(l
, "cookie=0x%08x", c
->cookie
);
128 static int publish_service(struct service
*s
);
130 static void service_entry_group_callback(AvahiEntryGroup
*g
, AvahiEntryGroupState state
, void *userdata
) {
131 struct service
*s
= userdata
;
137 case AVAHI_ENTRY_GROUP_ESTABLISHED
:
138 pa_log_info("Successfully established service %s.", s
->service_name
);
141 case AVAHI_ENTRY_GROUP_COLLISION
: {
144 t
= avahi_alternative_service_name(s
->service_name
);
145 pa_log_info("Name collision, renaming %s to %s.", s
->service_name
, t
);
146 pa_xfree(s
->service_name
);
153 case AVAHI_ENTRY_GROUP_FAILURE
: {
154 pa_log("Failed to register service: %s", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g
))));
156 avahi_entry_group_free(g
);
157 s
->entry_group
= NULL
;
162 case AVAHI_ENTRY_GROUP_UNCOMMITED
:
163 case AVAHI_ENTRY_GROUP_REGISTERING
:
168 static void service_free(struct service
*s
);
170 static int publish_service(struct service
*s
) {
172 AvahiStringList
*txt
= NULL
;
173 const char *description
= NULL
, *name
= NULL
;
176 char cm
[PA_CHANNEL_MAP_SNPRINT_MAX
];
180 if (!s
->userdata
->client
|| avahi_client_get_state(s
->userdata
->client
) != AVAHI_CLIENT_S_RUNNING
)
183 if (!s
->entry_group
) {
184 if (!(s
->entry_group
= avahi_entry_group_new(s
->userdata
->client
, service_entry_group_callback
, s
))) {
185 pa_log("avahi_entry_group_new(): %s", avahi_strerror(avahi_client_errno(s
->userdata
->client
)));
189 avahi_entry_group_reset(s
->entry_group
);
191 txt
= txt_record_server_data(s
->userdata
->core
, txt
);
193 get_service_data(s
, &ss
, &map
, &name
, &description
);
194 txt
= avahi_string_list_add_pair(txt
, "device", name
);
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
));
198 txt
= avahi_string_list_add_pair(txt
, "channel_map", pa_channel_map_snprint(cm
, sizeof(cm
), &map
));
200 if (avahi_entry_group_add_service_strlst(
202 AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
,
205 pa_sink_isinstance(s
->device
) ? SERVICE_TYPE_SINK
: SERVICE_TYPE_SOURCE
,
211 pa_log("avahi_entry_group_add_service_strlst(): %s", avahi_strerror(avahi_client_errno(s
->userdata
->client
)));
215 if (avahi_entry_group_commit(s
->entry_group
) < 0) {
216 pa_log("avahi_entry_group_commit(): %s", avahi_strerror(avahi_client_errno(s
->userdata
->client
)));
221 pa_log_debug("Successfully created entry group for %s.", s
->service_name
);
225 /* Remove this service */
229 avahi_string_list_free(txt
);
234 static struct service
*get_service(struct userdata
*u
, pa_object
*device
) {
240 pa_object_assert_ref(device
);
242 if ((s
= pa_hashmap_get(u
->services
, device
)))
245 s
= pa_xnew(struct service
, 1);
247 s
->entry_group
= NULL
;
250 if (pa_sink_isinstance(device
)) {
251 if (!(n
= PA_SINK(device
)->description
))
252 n
= PA_SINK(device
)->name
;
254 if (!(n
= PA_SOURCE(device
)->description
))
255 n
= PA_SOURCE(device
)->name
;
258 s
->service_name
= pa_truncate_utf8(pa_sprintf_malloc("%s@%s: %s",
259 pa_get_user_name(un
, sizeof(un
)),
260 pa_get_host_name(hn
, sizeof(hn
)),
264 pa_hashmap_put(u
->services
, s
->device
, s
);
269 static void service_free(struct service
*s
) {
272 pa_hashmap_remove(s
->userdata
->services
, s
->device
);
274 if (s
->entry_group
) {
275 pa_log_debug("Removing entry group for %s.", s
->service_name
);
276 avahi_entry_group_free(s
->entry_group
);
279 pa_xfree(s
->service_name
);
283 static pa_hook_result_t
device_new_or_changed_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
285 pa_object_assert_ref(o
);
287 publish_service(get_service(u
, o
));
292 static pa_hook_result_t
device_unlink_cb(pa_core
*c
, pa_object
*o
, struct userdata
*u
) {
296 pa_object_assert_ref(o
);
298 if ((s
= pa_hashmap_get(u
->services
, o
)))
304 static int publish_main_service(struct userdata
*u
);
306 static void main_entry_group_callback(AvahiEntryGroup
*g
, AvahiEntryGroupState state
, void *userdata
) {
307 struct userdata
*u
= userdata
;
312 case AVAHI_ENTRY_GROUP_ESTABLISHED
:
313 pa_log_info("Successfully established main service.");
316 case AVAHI_ENTRY_GROUP_COLLISION
: {
319 t
= avahi_alternative_service_name(u
->service_name
);
320 pa_log_info("Name collision: renaming main service %s to %s.", u
->service_name
, t
);
321 pa_xfree(u
->service_name
);
324 publish_main_service(u
);
328 case AVAHI_ENTRY_GROUP_FAILURE
: {
329 pa_log("Failed to register main service: %s", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g
))));
331 avahi_entry_group_free(g
);
332 u
->main_entry_group
= NULL
;
336 case AVAHI_ENTRY_GROUP_UNCOMMITED
:
337 case AVAHI_ENTRY_GROUP_REGISTERING
:
342 static int publish_main_service(struct userdata
*u
) {
343 AvahiStringList
*txt
= NULL
;
348 if (!u
->main_entry_group
) {
349 if (!(u
->main_entry_group
= avahi_entry_group_new(u
->client
, main_entry_group_callback
, u
))) {
350 pa_log("avahi_entry_group_new() failed: %s", avahi_strerror(avahi_client_errno(u
->client
)));
354 avahi_entry_group_reset(u
->main_entry_group
);
356 txt
= txt_record_server_data(u
->core
, txt
);
358 if (avahi_entry_group_add_service_strlst(
360 AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
,
369 pa_log("avahi_entry_group_add_service_strlst() failed: %s", avahi_strerror(avahi_client_errno(u
->client
)));
373 if (avahi_entry_group_commit(u
->main_entry_group
) < 0) {
374 pa_log("avahi_entry_group_commit() failed: %s", avahi_strerror(avahi_client_errno(u
->client
)));
381 avahi_string_list_free(txt
);
386 static int publish_all_services(struct userdata
*u
) {
394 pa_log_debug("Publishing services in Zeroconf");
396 for (sink
= PA_SINK(pa_idxset_first(u
->core
->sinks
, &idx
)); sink
; sink
= PA_SINK(pa_idxset_next(u
->core
->sinks
, &idx
)))
397 publish_service(get_service(u
, PA_OBJECT(sink
)));
399 for (source
= PA_SOURCE(pa_idxset_first(u
->core
->sources
, &idx
)); source
; source
= PA_SOURCE(pa_idxset_next(u
->core
->sources
, &idx
)))
400 publish_service(get_service(u
, PA_OBJECT(source
)));
402 if (publish_main_service(u
) < 0)
411 static void unpublish_all_services(struct userdata
*u
, pa_bool_t rem
) {
417 pa_log_debug("Unpublishing services in Zeroconf");
419 while ((s
= pa_hashmap_iterate(u
->services
, &state
, NULL
))) {
420 if (s
->entry_group
) {
422 pa_log_debug("Removing entry group for %s.", s
->service_name
);
423 avahi_entry_group_free(s
->entry_group
);
424 s
->entry_group
= NULL
;
426 avahi_entry_group_reset(s
->entry_group
);
427 pa_log_debug("Resetting entry group for %s.", s
->service_name
);
432 if (u
->main_entry_group
) {
434 pa_log_debug("Removing main entry group.");
435 avahi_entry_group_free(u
->main_entry_group
);
436 u
->main_entry_group
= NULL
;
438 avahi_entry_group_reset(u
->main_entry_group
);
439 pa_log_debug("Resetting main entry group.");
444 static void client_callback(AvahiClient
*c
, AvahiClientState state
, void *userdata
) {
445 struct userdata
*u
= userdata
;
453 case AVAHI_CLIENT_S_RUNNING
:
454 publish_all_services(u
);
457 case AVAHI_CLIENT_S_COLLISION
:
458 pa_log_debug("Host name collision");
459 unpublish_all_services(u
, FALSE
);
462 case AVAHI_CLIENT_FAILURE
:
463 if (avahi_client_errno(c
) == AVAHI_ERR_DISCONNECTED
) {
466 pa_log_debug("Avahi daemon disconnected.");
468 unpublish_all_services(u
, TRUE
);
469 avahi_client_free(u
->client
);
471 if (!(u
->client
= avahi_client_new(u
->avahi_poll
, AVAHI_CLIENT_NO_FAIL
, client_callback
, u
, &error
)))
472 pa_log("pa_avahi_client_new() failed: %s", avahi_strerror(error
));
481 int pa__init(pa_module
*m
) {
484 uint32_t port
= PA_NATIVE_DEFAULT_PORT
;
485 pa_modargs
*ma
= NULL
;
486 char hn
[256], un
[256];
489 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
490 pa_log("failed to parse module arguments.");
494 if (pa_modargs_get_value_u32(ma
, "port", &port
) < 0 || port
<= 0 || port
> 0xFFFF) {
495 pa_log("invalid port specified.");
499 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
501 u
->port
= (uint16_t) port
;
503 u
->avahi_poll
= pa_avahi_poll_new(m
->core
->mainloop
);
505 u
->services
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
507 u
->sink_new_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_NEW_POST
], (pa_hook_cb_t
) device_new_or_changed_cb
, u
);
508 u
->sink_changed_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_DESCRIPTION_CHANGED
], (pa_hook_cb_t
) device_new_or_changed_cb
, u
);
509 u
->sink_unlink_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK
], (pa_hook_cb_t
) device_unlink_cb
, u
);
510 u
->source_new_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_NEW_POST
], (pa_hook_cb_t
) device_new_or_changed_cb
, u
);
511 u
->source_changed_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_DESCRIPTION_CHANGED
], (pa_hook_cb_t
) device_new_or_changed_cb
, u
);
512 u
->source_unlink_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_UNLINK
], (pa_hook_cb_t
) device_unlink_cb
, u
);
514 u
->main_entry_group
= NULL
;
516 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
);
518 if (!(u
->client
= avahi_client_new(u
->avahi_poll
, AVAHI_CLIENT_NO_FAIL
, client_callback
, u
, &error
))) {
519 pa_log("pa_avahi_client_new() failed: %s", avahi_strerror(error
));
536 void pa__done(pa_module
*m
) {
540 if (!(u
= m
->userdata
))
546 while ((s
= pa_hashmap_get_first(u
->services
)))
549 pa_hashmap_free(u
->services
, NULL
, NULL
);
552 if (u
->sink_new_slot
)
553 pa_hook_slot_free(u
->sink_new_slot
);
554 if (u
->source_new_slot
)
555 pa_hook_slot_free(u
->source_new_slot
);
556 if (u
->sink_changed_slot
)
557 pa_hook_slot_free(u
->sink_changed_slot
);
558 if (u
->source_changed_slot
)
559 pa_hook_slot_free(u
->source_changed_slot
);
560 if (u
->sink_unlink_slot
)
561 pa_hook_slot_free(u
->sink_unlink_slot
);
562 if (u
->source_unlink_slot
)
563 pa_hook_slot_free(u
->source_unlink_slot
);
565 if (u
->main_entry_group
)
566 avahi_entry_group_free(u
->main_entry_group
);
569 avahi_client_free(u
->client
);
572 pa_avahi_poll_free(u
->avahi_poll
);
574 pa_xfree(u
->service_name
);