2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
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
31 #include <avahi-client/client.h>
32 #include <avahi-client/lookup.h>
33 #include <avahi-common/alternative.h>
34 #include <avahi-common/error.h>
35 #include <avahi-common/domain.h>
36 #include <avahi-common/malloc.h>
38 #include <pulse/xmalloc.h>
39 #include <pulse/util.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/hashmap.h>
48 #include <pulsecore/modargs.h>
49 #include <pulsecore/namereg.h>
50 #include <pulsecore/avahi-wrap.h>
52 #include "module-zeroconf-discover-symdef.h"
54 PA_MODULE_AUTHOR("Lennart Poettering");
55 PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Discovery");
56 PA_MODULE_VERSION(PACKAGE_VERSION
);
57 PA_MODULE_LOAD_ONCE(TRUE
);
59 #define SERVICE_TYPE_SINK "_pulse-sink._tcp"
60 #define SERVICE_TYPE_SOURCE "_non-monitor._sub._pulse-source._tcp"
62 static const char* const valid_modargs
[] = {
67 AvahiIfIndex interface
;
68 AvahiProtocol protocol
;
69 char *name
, *type
, *domain
;
70 uint32_t module_index
;
76 AvahiPoll
*avahi_poll
;
78 AvahiServiceBrowser
*source_browser
, *sink_browser
;
83 static unsigned tunnel_hash(const void *p
) {
84 const struct tunnel
*t
= p
;
87 (unsigned) t
->interface
+
88 (unsigned) t
->protocol
+
89 pa_idxset_string_hash_func(t
->name
) +
90 pa_idxset_string_hash_func(t
->type
) +
91 pa_idxset_string_hash_func(t
->domain
);
94 static int tunnel_compare(const void *a
, const void *b
) {
95 const struct tunnel
*ta
= a
, *tb
= b
;
98 if (ta
->interface
!= tb
->interface
)
100 if (ta
->protocol
!= tb
->protocol
)
102 if ((r
= strcmp(ta
->name
, tb
->name
)))
104 if ((r
= strcmp(ta
->type
, tb
->type
)))
106 if ((r
= strcmp(ta
->domain
, tb
->domain
)))
112 static struct tunnel
*tunnel_new(
113 AvahiIfIndex interface
, AvahiProtocol protocol
,
114 const char *name
, const char *type
, const char *domain
) {
117 t
= pa_xnew(struct tunnel
, 1);
118 t
->interface
= interface
;
119 t
->protocol
= protocol
;
120 t
->name
= pa_xstrdup(name
);
121 t
->type
= pa_xstrdup(type
);
122 t
->domain
= pa_xstrdup(domain
);
123 t
->module_index
= PA_IDXSET_INVALID
;
127 static void tunnel_free(struct tunnel
*t
) {
135 static void resolver_cb(
136 AvahiServiceResolver
*r
,
137 AvahiIfIndex interface
, AvahiProtocol protocol
,
138 AvahiResolverEvent event
,
139 const char *name
, const char *type
, const char *domain
,
140 const char *host_name
, const AvahiAddress
*a
, uint16_t port
,
141 AvahiStringList
*txt
,
142 AvahiLookupResultFlags flags
,
145 struct userdata
*u
= userdata
;
150 tnl
= tunnel_new(interface
, protocol
, name
, type
, domain
);
152 if (event
!= AVAHI_RESOLVER_FOUND
)
153 pa_log("Resolving of '%s' failed: %s", name
, avahi_strerror(avahi_client_errno(u
->client
)));
155 char *device
= NULL
, *dname
, *module_name
, *args
;
157 char at
[AVAHI_ADDRESS_STR_MAX
], cmt
[PA_CHANNEL_MAP_SNPRINT_MAX
];
161 pa_bool_t channel_map_set
= FALSE
;
164 ss
= u
->core
->default_sample_spec
;
165 pa_channel_map_init_extend(&cm
, ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
167 for (l
= txt
; l
; l
= l
->next
) {
169 pa_assert_se(avahi_string_list_get_pair(l
, &key
, &value
, NULL
) == 0);
171 if (strcmp(key
, "device") == 0) {
175 } else if (strcmp(key
, "rate") == 0)
176 ss
.rate
= (uint32_t) atoi(value
);
177 else if (strcmp(key
, "channels") == 0)
178 ss
.channels
= (uint8_t) atoi(value
);
179 else if (strcmp(key
, "format") == 0)
180 ss
.format
= pa_parse_sample_format(value
);
181 else if (strcmp(key
, "channel_map") == 0) {
182 pa_channel_map_parse(&cm
, value
);
183 channel_map_set
= TRUE
;
190 if (!channel_map_set
&& cm
.channels
!= ss
.channels
)
191 pa_channel_map_init_extend(&cm
, ss
.channels
, PA_CHANNEL_MAP_DEFAULT
);
193 if (!pa_sample_spec_valid(&ss
)) {
194 pa_log("Service '%s' contains an invalid sample specification.", name
);
199 if (!pa_channel_map_valid(&cm
) || cm
.channels
!= ss
.channels
) {
200 pa_log("Service '%s' contains an invalid channel map.", name
);
206 dname
= pa_sprintf_malloc("tunnel.%s.%s", host_name
, device
);
208 dname
= pa_sprintf_malloc("tunnel.%s", host_name
);
210 if (!pa_namereg_is_valid_name(dname
)) {
211 pa_log("Cannot construct valid device name from credentials of service '%s'.", dname
);
217 t
= strstr(type
, "sink") ? "sink" : "source";
219 module_name
= pa_sprintf_malloc("module-tunnel-%s", t
);
220 args
= pa_sprintf_malloc("server=[%s]:%u "
227 avahi_address_snprint(at
, sizeof(at
), a
), port
,
229 pa_sample_format_to_string(ss
.format
),
233 pa_channel_map_snprint(cmt
, sizeof(cmt
), &cm
));
235 pa_log_debug("Loading %s with arguments '%s'", module_name
, args
);
237 if ((m
= pa_module_load(u
->core
, module_name
, args
))) {
238 tnl
->module_index
= m
->index
;
239 pa_hashmap_put(u
->tunnels
, tnl
, tnl
);
243 pa_xfree(module_name
);
251 avahi_service_resolver_free(r
);
257 static void browser_cb(
258 AvahiServiceBrowser
*b
,
259 AvahiIfIndex interface
, AvahiProtocol protocol
,
260 AvahiBrowserEvent event
,
261 const char *name
, const char *type
, const char *domain
,
262 AvahiLookupResultFlags flags
,
265 struct userdata
*u
= userdata
;
270 if (flags
& AVAHI_LOOKUP_RESULT_LOCAL
)
273 t
= tunnel_new(interface
, protocol
, name
, type
, domain
);
275 if (event
== AVAHI_BROWSER_NEW
) {
277 if (!pa_hashmap_get(u
->tunnels
, t
))
278 if (!(avahi_service_resolver_new(u
->client
, interface
, protocol
, name
, type
, domain
, AVAHI_PROTO_UNSPEC
, 0, resolver_cb
, u
)))
279 pa_log("avahi_service_resolver_new() failed: %s", avahi_strerror(avahi_client_errno(u
->client
)));
281 /* We ignore the returned resolver object here, since the we don't
282 * need to attach any special data to it, and we can still destory
283 * it from the callback */
285 } else if (event
== AVAHI_BROWSER_REMOVE
) {
288 if ((t2
= pa_hashmap_get(u
->tunnels
, t
))) {
289 pa_module_unload_by_index(u
->core
, t2
->module_index
, TRUE
);
290 pa_hashmap_remove(u
->tunnels
, t2
);
298 static void client_callback(AvahiClient
*c
, AvahiClientState state
, void *userdata
) {
299 struct userdata
*u
= userdata
;
307 case AVAHI_CLIENT_S_REGISTERING
:
308 case AVAHI_CLIENT_S_RUNNING
:
309 case AVAHI_CLIENT_S_COLLISION
:
311 if (!u
->sink_browser
) {
313 if (!(u
->sink_browser
= avahi_service_browser_new(
315 AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
,
321 pa_log("avahi_service_browser_new() failed: %s", avahi_strerror(avahi_client_errno(c
)));
322 pa_module_unload_request(u
->module
, TRUE
);
326 if (!u
->source_browser
) {
328 if (!(u
->source_browser
= avahi_service_browser_new(
330 AVAHI_IF_UNSPEC
, AVAHI_PROTO_UNSPEC
,
336 pa_log("avahi_service_browser_new() failed: %s", avahi_strerror(avahi_client_errno(c
)));
337 pa_module_unload_request(u
->module
, TRUE
);
343 case AVAHI_CLIENT_FAILURE
:
344 if (avahi_client_errno(c
) == AVAHI_ERR_DISCONNECTED
) {
347 pa_log_debug("Avahi daemon disconnected.");
349 if (!(u
->client
= avahi_client_new(u
->avahi_poll
, AVAHI_CLIENT_NO_FAIL
, client_callback
, u
, &error
))) {
350 pa_log("avahi_client_new() failed: %s", avahi_strerror(error
));
351 pa_module_unload_request(u
->module
, TRUE
);
357 case AVAHI_CLIENT_CONNECTING
:
359 if (u
->sink_browser
) {
360 avahi_service_browser_free(u
->sink_browser
);
361 u
->sink_browser
= NULL
;
364 if (u
->source_browser
) {
365 avahi_service_browser_free(u
->source_browser
);
366 u
->source_browser
= NULL
;
375 int pa__init(pa_module
*m
) {
378 pa_modargs
*ma
= NULL
;
381 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
382 pa_log("Failed to parse module arguments.");
386 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
389 u
->sink_browser
= u
->source_browser
= NULL
;
391 u
->tunnels
= pa_hashmap_new(tunnel_hash
, tunnel_compare
);
393 u
->avahi_poll
= pa_avahi_poll_new(m
->core
->mainloop
);
395 if (!(u
->client
= avahi_client_new(u
->avahi_poll
, AVAHI_CLIENT_NO_FAIL
, client_callback
, u
, &error
))) {
396 pa_log("pa_avahi_client_new() failed: %s", avahi_strerror(error
));
413 void pa__done(pa_module
*m
) {
417 if (!(u
= m
->userdata
))
421 avahi_client_free(u
->client
);
424 pa_avahi_poll_free(u
->avahi_poll
);
429 while ((t
= pa_hashmap_steal_first(u
->tunnels
))) {
430 pa_module_unload_by_index(u
->core
, t
->module_index
, TRUE
);
434 pa_hashmap_free(u
->tunnels
, NULL
, NULL
);