default: esd is obsolete, hence don't load it anymore by default
[pulseaudio-mirror.git] / src / modules / module-zeroconf-discover.c
blobcd076aab145268ae4019c2f039c5bce28728d5f5
1 /***
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
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
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[] = {
58 NULL
61 struct tunnel {
62 AvahiIfIndex interface;
63 AvahiProtocol protocol;
64 char *name, *type, *domain;
65 uint32_t module_index;
68 struct userdata {
69 pa_core *core;
70 pa_module *module;
71 AvahiPoll *avahi_poll;
72 AvahiClient *client;
73 AvahiServiceBrowser *source_browser, *sink_browser;
75 pa_hashmap *tunnels;
78 static unsigned tunnel_hash(const void *p) {
79 const struct tunnel *t = p;
81 return
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;
91 int r;
93 if (ta->interface != tb->interface)
94 return 1;
95 if (ta->protocol != tb->protocol)
96 return 1;
97 if ((r = strcmp(ta->name, tb->name)))
98 return r;
99 if ((r = strcmp(ta->type, tb->type)))
100 return r;
101 if ((r = strcmp(ta->domain, tb->domain)))
102 return r;
104 return 0;
107 static struct tunnel *tunnel_new(
108 AvahiIfIndex interface, AvahiProtocol protocol,
109 const char *name, const char *type, const char *domain) {
111 struct tunnel *t;
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;
119 return t;
122 static void tunnel_free(struct tunnel *t) {
123 pa_assert(t);
124 pa_xfree(t->name);
125 pa_xfree(t->type);
126 pa_xfree(t->domain);
127 pa_xfree(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,
138 void *userdata) {
140 struct userdata *u = userdata;
141 struct tunnel *tnl;
143 pa_assert(u);
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)));
149 else {
150 char *device = NULL, *dname, *module_name, *args;
151 const char *t;
152 char at[AVAHI_ADDRESS_STR_MAX], cmt[PA_CHANNEL_MAP_SNPRINT_MAX];
153 pa_sample_spec ss;
154 pa_channel_map cm;
155 AvahiStringList *l;
156 pa_bool_t channel_map_set = FALSE;
157 pa_module *m;
159 ss = u->core->default_sample_spec;
160 cm = u->core->default_channel_map;
162 for (l = txt; l; l = l->next) {
163 char *key, *value;
164 pa_assert_se(avahi_string_list_get_pair(l, &key, &value, NULL) == 0);
166 if (strcmp(key, "device") == 0) {
167 pa_xfree(device);
168 device = value;
169 value = NULL;
170 } else if (strcmp(key, "rate") == 0)
171 ss.rate = (uint32_t) atoi(value);
172 else if (strcmp(key, "channels") == 0)
173 ss.channels = (uint8_t) atoi(value);
174 else if (strcmp(key, "format") == 0)
175 ss.format = pa_parse_sample_format(value);
176 else if (strcmp(key, "channel_map") == 0) {
177 pa_channel_map_parse(&cm, value);
178 channel_map_set = TRUE;
181 avahi_free(key);
182 avahi_free(value);
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);
190 avahi_free(device);
191 goto finish;
194 if (!pa_channel_map_valid(&cm) || cm.channels != ss.channels) {
195 pa_log("Service '%s' contains an invalid channel map.", name);
196 avahi_free(device);
197 goto finish;
200 if (device)
201 dname = pa_sprintf_malloc("tunnel.%s.%s", host_name, device);
202 else
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);
207 avahi_free(device);
208 pa_xfree(dname);
209 goto finish;
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 "
216 "%s=%s "
217 "format=%s "
218 "channels=%u "
219 "rate=%u "
220 "%s_name=%s "
221 "channel_map=%s",
222 avahi_address_snprint(at, sizeof(at), a), port,
223 t, device,
224 pa_sample_format_to_string(ss.format),
225 ss.channels,
226 ss.rate,
227 t, dname,
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);
235 tnl = NULL;
238 pa_xfree(module_name);
239 pa_xfree(dname);
240 pa_xfree(args);
241 avahi_free(device);
244 finish:
246 avahi_service_resolver_free(r);
248 if (tnl)
249 tunnel_free(tnl);
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,
258 void *userdata) {
260 struct userdata *u = userdata;
261 struct tunnel *t;
263 pa_assert(u);
265 if (flags & AVAHI_LOOKUP_RESULT_LOCAL)
266 return;
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) {
281 struct tunnel *t2;
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);
286 tunnel_free(t2);
290 tunnel_free(t);
293 static void client_callback(AvahiClient *c, AvahiClientState state, void *userdata) {
294 struct userdata *u = userdata;
296 pa_assert(c);
297 pa_assert(u);
299 u->client = c;
301 switch (state) {
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,
311 SERVICE_TYPE_SINK,
312 NULL,
314 browser_cb, u))) {
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,
326 SERVICE_TYPE_SOURCE,
327 NULL,
329 browser_cb, u))) {
331 pa_log("avahi_service_browser_new() failed: %s", avahi_strerror(avahi_client_errno(c)));
332 pa_module_unload_request(u->module, TRUE);
336 break;
338 case AVAHI_CLIENT_FAILURE:
339 if (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
340 int error;
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);
350 /* Fall through */
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;
364 break;
366 default: ;
370 int pa__init(pa_module*m) {
372 struct userdata *u;
373 pa_modargs *ma = NULL;
374 int error;
376 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
377 pa_log("Failed to parse module arguments.");
378 goto fail;
381 m->userdata = u = pa_xnew(struct userdata, 1);
382 u->core = m->core;
383 u->module = m;
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));
392 goto fail;
395 pa_modargs_free(ma);
397 return 0;
399 fail:
400 pa__done(m);
402 if (ma)
403 pa_modargs_free(ma);
405 return -1;
408 void pa__done(pa_module*m) {
409 struct userdata*u;
410 pa_assert(m);
412 if (!(u = m->userdata))
413 return;
415 if (u->client)
416 avahi_client_free(u->client);
418 if (u->avahi_poll)
419 pa_avahi_poll_free(u->avahi_poll);
421 if (u->tunnels) {
422 struct tunnel *t;
424 while ((t = pa_hashmap_steal_first(u->tunnels))) {
425 pa_module_unload_request_by_index(u->core, t->module_index, TRUE);
426 tunnel_free(t);
429 pa_hashmap_free(u->tunnels, NULL, NULL);
432 pa_xfree(u);