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/lookup.h>
35 #include <avahi-common/alternative.h>
36 #include <avahi-common/error.h>
37 #include <avahi-common/domain.h>
38 #include <avahi-common/malloc.h>
40 #include <pulse/xmalloc.h>
41 #include <pulse/util.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/hashmap.h>
50 #include <pulsecore/modargs.h>
51 #include <pulsecore/namereg.h>
52 #include <pulsecore/avahi-wrap.h>
54 #include "module-zeroconf-discover-symdef.h"
56 PA_MODULE_AUTHOR("Lennart Poettering");
57 PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Discovery");
58 PA_MODULE_VERSION(PACKAGE_VERSION
);
59 PA_MODULE_LOAD_ONCE(TRUE
);
61 #define SERVICE_TYPE_SINK "_pulse-sink._tcp"
62 #define SERVICE_TYPE_SOURCE "_non-monitor._sub._pulse-source._tcp"
64 static const char* const valid_modargs
[] = {
69 AvahiIfIndex interface
;
70 AvahiProtocol protocol
;
71 char *name
, *type
, *domain
;
72 uint32_t module_index
;
78 AvahiPoll
*avahi_poll
;
80 AvahiServiceBrowser
*source_browser
, *sink_browser
;
85 static unsigned tunnel_hash(const void *p
) {
86 const struct tunnel
*t
= p
;
89 (unsigned) t
->interface
+
90 (unsigned) t
->protocol
+
91 pa_idxset_string_hash_func(t
->name
) +
92 pa_idxset_string_hash_func(t
->type
) +
93 pa_idxset_string_hash_func(t
->domain
);
96 static int tunnel_compare(const void *a
, const void *b
) {
97 const struct tunnel
*ta
= a
, *tb
= b
;
100 if (ta
->interface
!= tb
->interface
)
102 if (ta
->protocol
!= tb
->protocol
)
104 if ((r
= strcmp(ta
->name
, tb
->name
)))
106 if ((r
= strcmp(ta
->type
, tb
->type
)))
108 if ((r
= strcmp(ta
->domain
, tb
->domain
)))
114 static struct tunnel
*tunnel_new(
115 AvahiIfIndex interface
, AvahiProtocol protocol
,
116 const char *name
, const char *type
, const char *domain
) {
119 t
= pa_xnew(struct tunnel
, 1);
120 t
->interface
= interface
;
121 t
->protocol
= protocol
;
122 t
->name
= pa_xstrdup(name
);
123 t
->type
= pa_xstrdup(type
);
124 t
->domain
= pa_xstrdup(domain
);
125 t
->module_index
= PA_IDXSET_INVALID
;
129 static void tunnel_free(struct tunnel
*t
) {
137 static void resolver_cb(
138 AvahiServiceResolver
*r
,
139 AvahiIfIndex interface
, AvahiProtocol protocol
,
140 AvahiResolverEvent event
,
141 const char *name
, const char *type
, const char *domain
,
142 const char *host_name
, const AvahiAddress
*a
, uint16_t port
,
143 AvahiStringList
*txt
,
144 AvahiLookupResultFlags flags
,
147 struct userdata
*u
= userdata
;
152 tnl
= tunnel_new(interface
, protocol
, name
, type
, domain
);
154 if (event
!= AVAHI_RESOLVER_FOUND
)
155 pa_log("Resolving of '%s' failed: %s", name
, avahi_strerror(avahi_client_errno(u
->client
)));
157 char *device
= NULL
, *dname
, *module_name
, *args
;
159 char at
[AVAHI_ADDRESS_STR_MAX
], cmt
[PA_CHANNEL_MAP_SNPRINT_MAX
];
163 pa_bool_t channel_map_set
= FALSE
;
166 ss
= u
->core
->default_sample_spec
;
167 pa_assert_se(pa_channel_map_init_auto(&cm
, ss
.channels
, PA_CHANNEL_MAP_AUX
));
168 pa_channel_map_init_auto(&cm
, ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
170 for (l
= txt
; l
; l
= l
->next
) {
172 pa_assert_se(avahi_string_list_get_pair(l
, &key
, &value
, NULL
) == 0);
174 if (strcmp(key
, "device") == 0) {
178 } else if (strcmp(key
, "rate") == 0)
179 ss
.rate
= atoi(value
);
180 else if (strcmp(key
, "channels") == 0)
181 ss
.channels
= atoi(value
);
182 else if (strcmp(key
, "format") == 0)
183 ss
.format
= pa_parse_sample_format(value
);
184 else if (strcmp(key
, "channel_map") == 0) {
185 pa_channel_map_parse(&cm
, value
);
186 channel_map_set
= TRUE
;
193 if (!channel_map_set
&& cm
.channels
!= ss
.channels
) {
194 pa_assert_se(pa_channel_map_init_auto(&cm
, ss
.channels
, PA_CHANNEL_MAP_AUX
));
195 pa_channel_map_init_auto(&cm
, ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
198 if (!pa_sample_spec_valid(&ss
)) {
199 pa_log("Service '%s' contains an invalid sample specification.", name
);
204 if (!pa_channel_map_valid(&cm
) || cm
.channels
!= ss
.channels
) {
205 pa_log("Service '%s' contains an invalid channel map.", name
);
211 dname
= pa_sprintf_malloc("tunnel.%s.%s", host_name
, device
);
213 dname
= pa_sprintf_malloc("tunnel.%s", host_name
);
215 if (!pa_namereg_is_valid_name(dname
)) {
216 pa_log("Cannot construct valid device name from credentials of service '%s'.", dname
);
222 t
= strstr(type
, "sink") ? "sink" : "source";
224 module_name
= pa_sprintf_malloc("module-tunnel-%s", t
);
225 args
= pa_sprintf_malloc("server=[%s]:%u "
232 avahi_address_snprint(at
, sizeof(at
), a
), port
,
234 pa_sample_format_to_string(ss
.format
),
238 pa_channel_map_snprint(cmt
, sizeof(cmt
), &cm
));
240 pa_log_debug("Loading %s with arguments '%s'", module_name
, args
);
242 if ((m
= pa_module_load(u
->core
, module_name
, args
))) {
243 tnl
->module_index
= m
->index
;
244 pa_hashmap_put(u
->tunnels
, tnl
, tnl
);
248 pa_xfree(module_name
);
256 avahi_service_resolver_free(r
);
262 static void browser_cb(
263 AvahiServiceBrowser
*b
,
264 AvahiIfIndex interface
, AvahiProtocol protocol
,
265 AvahiBrowserEvent event
,
266 const char *name
, const char *type
, const char *domain
,
267 AvahiLookupResultFlags flags
,
270 struct userdata
*u
= userdata
;
275 if (flags
& AVAHI_LOOKUP_RESULT_LOCAL
)
278 t
= tunnel_new(interface
, protocol
, name
, type
, domain
);
280 if (event
== AVAHI_BROWSER_NEW
) {
282 if (!pa_hashmap_get(u
->tunnels
, t
))
283 if (!(avahi_service_resolver_new(u
->client
, interface
, protocol
, name
, type
, domain
, AVAHI_PROTO_UNSPEC
, 0, resolver_cb
, u
)))
284 pa_log("avahi_service_resolver_new() failed: %s", avahi_strerror(avahi_client_errno(u
->client
)));
286 /* We ignore the returned resolver object here, since the we don't
287 * need to attach any special data to it, and we can still destory
288 * it from the callback */
290 } else if (event
== AVAHI_BROWSER_REMOVE
) {
293 if ((t2
= pa_hashmap_get(u
->tunnels
, t
))) {
294 pa_module_unload_by_index(u
->core
, t2
->module_index
);
295 pa_hashmap_remove(u
->tunnels
, t2
);
303 static void client_callback(AvahiClient
*c
, AvahiClientState state
, void *userdata
) {
304 struct userdata
*u
= userdata
;
312 case AVAHI_CLIENT_S_REGISTERING
:
313 case AVAHI_CLIENT_S_RUNNING
:
314 case AVAHI_CLIENT_S_COLLISION
:
316 if (!u
->sink_browser
) {
318 if (!(u
->sink_browser
= avahi_service_browser_new(
320 AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
,
326 pa_log("avahi_service_browser_new() failed: %s", avahi_strerror(avahi_client_errno(c
)));
327 pa_module_unload_request(u
->module
);
331 if (!u
->source_browser
) {
333 if (!(u
->source_browser
= avahi_service_browser_new(
335 AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
,
341 pa_log("avahi_service_browser_new() failed: %s", avahi_strerror(avahi_client_errno(c
)));
342 pa_module_unload_request(u
->module
);
348 case AVAHI_CLIENT_FAILURE
:
349 if (avahi_client_errno(c
) == AVAHI_ERR_DISCONNECTED
) {
352 pa_log_debug("Avahi daemon disconnected.");
354 if (!(u
->client
= avahi_client_new(u
->avahi_poll
, AVAHI_CLIENT_NO_FAIL
, client_callback
, u
, &error
))) {
355 pa_log("avahi_client_new() failed: %s", avahi_strerror(error
));
356 pa_module_unload_request(u
->module
);
362 case AVAHI_CLIENT_CONNECTING
:
364 if (u
->sink_browser
) {
365 avahi_service_browser_free(u
->sink_browser
);
366 u
->sink_browser
= NULL
;
369 if (u
->source_browser
) {
370 avahi_service_browser_free(u
->source_browser
);
371 u
->source_browser
= NULL
;
380 int pa__init(pa_module
*m
) {
383 pa_modargs
*ma
= NULL
;
386 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
387 pa_log("Failed to parse module arguments.");
391 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
394 u
->sink_browser
= u
->source_browser
= NULL
;
396 u
->tunnels
= pa_hashmap_new(tunnel_hash
, tunnel_compare
);
398 u
->avahi_poll
= pa_avahi_poll_new(m
->core
->mainloop
);
400 if (!(u
->client
= avahi_client_new(u
->avahi_poll
, AVAHI_CLIENT_NO_FAIL
, client_callback
, u
, &error
))) {
401 pa_log("pa_avahi_client_new() failed: %s", avahi_strerror(error
));
418 void pa__done(pa_module
*m
) {
422 if (!(u
= m
->userdata
))
426 avahi_client_free(u
->client
);
429 pa_avahi_poll_free(u
->avahi_poll
);
434 while ((t
= pa_hashmap_steal_first(u
->tunnels
))) {
435 pa_module_unload_by_index(u
->core
, t
->module_index
);
439 pa_hashmap_free(u
->tunnels
, NULL
, NULL
);