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.1 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>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/log.h>
42 #include <pulsecore/hashmap.h>
43 #include <pulsecore/modargs.h>
44 #include <pulsecore/namereg.h>
45 #include <pulsecore/avahi-wrap.h>
47 #include "module-zeroconf-discover-symdef.h"
49 PA_MODULE_AUTHOR("Lennart Poettering");
50 PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Discovery");
51 PA_MODULE_VERSION(PACKAGE_VERSION);
52 PA_MODULE_LOAD_ONCE(true);
54 #define SERVICE_TYPE_SINK "_pulse-sink._tcp"
55 #define SERVICE_TYPE_SOURCE "_non-monitor._sub._pulse-source._tcp"
57 static const char* const valid_modargs[] = {
62 AvahiIfIndex interface;
63 AvahiProtocol protocol;
64 char *name, *type, *domain;
65 uint32_t module_index;
71 AvahiPoll *avahi_poll;
73 AvahiServiceBrowser *source_browser, *sink_browser;
78 static unsigned tunnel_hash(const void *p) {
79 const struct tunnel *t = p;
82 (unsigned) t->interface +
83 (unsigned) t->protocol +
84 pa_idxset_string_hash_func(t->name) +
85 pa_idxset_string_hash_func(t->type) +
86 pa_idxset_string_hash_func(t->domain);
89 static int tunnel_compare(const void *a, const void *b) {
90 const struct tunnel *ta = a, *tb = b;
93 if (ta->interface != tb->interface)
95 if (ta->protocol != tb->protocol)
97 if ((r = strcmp(ta->name, tb->name)))
99 if ((r = strcmp(ta->type, tb->type)))
101 if ((r = strcmp(ta->domain, tb->domain)))
107 static struct tunnel *tunnel_new(
108 AvahiIfIndex interface, AvahiProtocol protocol,
109 const char *name, const char *type, const char *domain) {
112 t = pa_xnew(struct tunnel, 1);
113 t->interface = interface;
114 t->protocol = protocol;
115 t->name = pa_xstrdup(name);
116 t->type = pa_xstrdup(type);
117 t->domain = pa_xstrdup(domain);
118 t->module_index = PA_IDXSET_INVALID;
122 static void tunnel_free(struct tunnel *t) {
130 static void resolver_cb(
131 AvahiServiceResolver *r,
132 AvahiIfIndex interface, AvahiProtocol protocol,
133 AvahiResolverEvent event,
134 const char *name, const char *type, const char *domain,
135 const char *host_name, const AvahiAddress *a, uint16_t port,
136 AvahiStringList *txt,
137 AvahiLookupResultFlags flags,
140 struct userdata *u = userdata;
145 tnl = tunnel_new(interface, protocol, name, type, domain);
147 if (event != AVAHI_RESOLVER_FOUND)
148 pa_log("Resolving of '%s' failed: %s", name, avahi_strerror(avahi_client_errno(u->client)));
150 char *device = NULL, *dname, *module_name, *args;
152 char at[AVAHI_ADDRESS_STR_MAX], cmt[PA_CHANNEL_MAP_SNPRINT_MAX];
156 bool channel_map_set = false;
159 ss = u->core->default_sample_spec;
160 cm = u->core->default_channel_map;
162 for (l = txt; l; l = l->next) {
164 pa_assert_se(avahi_string_list_get_pair(l, &key, &value, NULL) == 0);
166 if (pa_streq(key, "device")) {
170 } else if (pa_streq(key, "rate"))
171 ss.rate = (uint32_t) atoi(value);
172 else if (pa_streq(key, "channels"))
173 ss.channels = (uint8_t) atoi(value);
174 else if (pa_streq(key, "format"))
175 ss.format = pa_parse_sample_format(value);
176 else if (pa_streq(key, "channel_map")) {
177 pa_channel_map_parse(&cm, value);
178 channel_map_set = true;
185 if (!channel_map_set && cm.channels != ss.channels)
186 pa_channel_map_init_extend(&cm, ss.channels, PA_CHANNEL_MAP_DEFAULT);
188 if (!pa_sample_spec_valid(&ss)) {
189 pa_log("Service '%s' contains an invalid sample specification.", name);
194 if (!pa_channel_map_valid(&cm) || cm.channels != ss.channels) {
195 pa_log("Service '%s' contains an invalid channel map.", name);
201 dname = pa_sprintf_malloc("tunnel.%s.%s", host_name, device);
203 dname = pa_sprintf_malloc("tunnel.%s", host_name);
205 if (!pa_namereg_is_valid_name(dname)) {
206 pa_log("Cannot construct valid device name from credentials of service '%s'.", dname);
212 t = strstr(type, "sink") ? "sink" : "source";
214 module_name = pa_sprintf_malloc("module-tunnel-%s", t);
215 args = pa_sprintf_malloc("server=[%s]:%u "
222 avahi_address_snprint(at, sizeof(at), a), port,
224 pa_sample_format_to_string(ss.format),
228 pa_channel_map_snprint(cmt, sizeof(cmt), &cm));
230 pa_log_debug("Loading %s with arguments '%s'", module_name, args);
232 if ((m = pa_module_load(u->core, module_name, args))) {
233 tnl->module_index = m->index;
234 pa_hashmap_put(u->tunnels, tnl, tnl);
238 pa_xfree(module_name);
246 avahi_service_resolver_free(r);
252 static void browser_cb(
253 AvahiServiceBrowser *b,
254 AvahiIfIndex interface, AvahiProtocol protocol,
255 AvahiBrowserEvent event,
256 const char *name, const char *type, const char *domain,
257 AvahiLookupResultFlags flags,
260 struct userdata *u = userdata;
265 if (flags & AVAHI_LOOKUP_RESULT_LOCAL)
268 t = tunnel_new(interface, protocol, name, type, domain);
270 if (event == AVAHI_BROWSER_NEW) {
272 if (!pa_hashmap_get(u->tunnels, t))
273 if (!(avahi_service_resolver_new(u->client, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, resolver_cb, u)))
274 pa_log("avahi_service_resolver_new() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
276 /* We ignore the returned resolver object here, since the we don't
277 * need to attach any special data to it, and we can still destroy
278 * it from the callback */
280 } else if (event == AVAHI_BROWSER_REMOVE) {
283 if ((t2 = pa_hashmap_get(u->tunnels, t))) {
284 pa_module_unload_request_by_index(u->core, t2->module_index, true);
285 pa_hashmap_remove(u->tunnels, t2);
293 static void client_callback(AvahiClient *c, AvahiClientState state, void *userdata) {
294 struct userdata *u = userdata;
302 case AVAHI_CLIENT_S_REGISTERING:
303 case AVAHI_CLIENT_S_RUNNING:
304 case AVAHI_CLIENT_S_COLLISION:
306 if (!u->sink_browser) {
308 if (!(u->sink_browser = avahi_service_browser_new(
310 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
316 pa_log("avahi_service_browser_new() failed: %s", avahi_strerror(avahi_client_errno(c)));
317 pa_module_unload_request(u->module, true);
321 if (!u->source_browser) {
323 if (!(u->source_browser = avahi_service_browser_new(
325 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
331 pa_log("avahi_service_browser_new() failed: %s", avahi_strerror(avahi_client_errno(c)));
332 pa_module_unload_request(u->module, true);
338 case AVAHI_CLIENT_FAILURE:
339 if (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
342 pa_log_debug("Avahi daemon disconnected.");
344 if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
345 pa_log("avahi_client_new() failed: %s", avahi_strerror(error));
346 pa_module_unload_request(u->module, true);
352 case AVAHI_CLIENT_CONNECTING:
354 if (u->sink_browser) {
355 avahi_service_browser_free(u->sink_browser);
356 u->sink_browser = NULL;
359 if (u->source_browser) {
360 avahi_service_browser_free(u->source_browser);
361 u->source_browser = NULL;
370 int pa__init(pa_module*m) {
373 pa_modargs *ma = NULL;
376 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
377 pa_log("Failed to parse module arguments.");
381 m->userdata = u = pa_xnew(struct userdata, 1);
384 u->sink_browser = u->source_browser = NULL;
386 u->tunnels = pa_hashmap_new(tunnel_hash, tunnel_compare);
388 u->avahi_poll = pa_avahi_poll_new(m->core->mainloop);
390 if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
391 pa_log("pa_avahi_client_new() failed: %s", avahi_strerror(error));
408 void pa__done(pa_module*m) {
412 if (!(u = m->userdata))
416 avahi_client_free(u->client);
419 pa_avahi_poll_free(u->avahi_poll);
424 while ((t = pa_hashmap_steal_first(u->tunnels))) {
425 pa_module_unload_request_by_index(u->core, t->module_index, true);
429 pa_hashmap_free(u->tunnels);