]> code.delx.au - pulseaudio/blob - src/modules/module-zeroconf-discover.c
keep track of configured tunnels and make sure to unload them when they disappear...
[pulseaudio] / src / modules / module-zeroconf-discover.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
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.
12
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.
17
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
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32
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>
39
40 #include <pulse/xmalloc.h>
41 #include <pulse/util.h>
42
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>
53
54 #include "module-zeroconf-discover-symdef.h"
55
56 PA_MODULE_AUTHOR("Lennart Poettering")
57 PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Discovery")
58 PA_MODULE_VERSION(PACKAGE_VERSION)
59
60 #define SERVICE_TYPE_SINK "_pulse-sink._tcp"
61 #define SERVICE_TYPE_SOURCE "_non-monitor._sub._pulse-source._tcp"
62
63 static const char* const valid_modargs[] = {
64 NULL
65 };
66
67 struct tunnel {
68 AvahiIfIndex interface;
69 AvahiProtocol protocol;
70 char *name, *type, *domain;
71 uint32_t module_index;
72 };
73
74 struct userdata {
75 pa_core *core;
76 pa_module *module;
77 AvahiPoll *avahi_poll;
78 AvahiClient *client;
79 AvahiServiceBrowser *source_browser, *sink_browser;
80
81 pa_hashmap *tunnels;
82 };
83
84 static unsigned tunnel_hash(const void *p) {
85 const struct tunnel *t = p;
86
87 return
88 (unsigned) t->interface +
89 (unsigned) t->protocol +
90 pa_idxset_string_hash_func(t->name) +
91 pa_idxset_string_hash_func(t->type) +
92 pa_idxset_string_hash_func(t->domain);
93 }
94
95 static int tunnel_compare(const void *a, const void *b) {
96 const struct tunnel *ta = a, *tb = b;
97 int r;
98
99 if (ta->interface != tb->interface)
100 return 1;
101 if (ta->protocol != tb->protocol)
102 return 1;
103 if ((r = strcmp(ta->name, tb->name)))
104 return r;
105 if ((r = strcmp(ta->type, tb->type)))
106 return r;
107 if ((r = strcmp(ta->domain, tb->domain)))
108 return r;
109
110 return 0;
111 }
112
113 static struct tunnel *tunnel_new(
114 AvahiIfIndex interface, AvahiProtocol protocol,
115 const char *name, const char *type, const char *domain) {
116
117 struct tunnel *t;
118 t = pa_xnew(struct tunnel, 1);
119 t->interface = interface;
120 t->protocol = protocol;
121 t->name = pa_xstrdup(name);
122 t->type = pa_xstrdup(type);
123 t->domain = pa_xstrdup(domain);
124 t->module_index = PA_IDXSET_INVALID;
125 return t;
126 }
127
128 static void tunnel_free(struct tunnel *t) {
129 pa_assert(t);
130 pa_xfree(t->name);
131 pa_xfree(t->type);
132 pa_xfree(t->domain);
133 pa_xfree(t);
134 }
135
136 static void resolver_cb(
137 AvahiServiceResolver *r,
138 AvahiIfIndex interface, AvahiProtocol protocol,
139 AvahiResolverEvent event,
140 const char *name, const char *type, const char *domain,
141 const char *host_name, const AvahiAddress *a, uint16_t port,
142 AvahiStringList *txt,
143 AvahiLookupResultFlags flags,
144 void *userdata) {
145
146 struct userdata *u = userdata;
147 struct tunnel *tnl;
148
149 pa_assert(u);
150
151 tnl = tunnel_new(interface, protocol, name, type, domain);
152
153 if (event != AVAHI_RESOLVER_FOUND)
154 pa_log("Resolving of '%s' failed: %s", name, avahi_strerror(avahi_client_errno(u->client)));
155 else {
156 char *device = NULL, *dname, *module_name, *args;
157 const char *t;
158 char at[AVAHI_ADDRESS_STR_MAX], cmt[PA_CHANNEL_MAP_SNPRINT_MAX];
159 pa_sample_spec ss;
160 pa_channel_map cm;
161 AvahiStringList *l;
162 pa_bool_t channel_map_set = FALSE;
163 pa_module *m;
164
165 ss = u->core->default_sample_spec;
166 pa_channel_map_init_auto(&cm, ss.channels, PA_CHANNEL_MAP_DEFAULT);
167
168 for (l = txt; l; l = l->next) {
169 char *key, *value;
170 pa_assert_se(avahi_string_list_get_pair(l, &key, &value, NULL) == 0);
171
172 if (strcmp(key, "device") == 0) {
173 pa_xfree(device);
174 device = value;
175 value = NULL;
176 } else if (strcmp(key, "rate") == 0)
177 ss.rate = atoi(value);
178 else if (strcmp(key, "channels") == 0)
179 ss.channels = atoi(value);
180 else if (strcmp(key, "format") == 0)
181 ss.format = pa_parse_sample_format(value);
182 else if (strcmp(key, "channel_map") == 0) {
183 pa_channel_map_parse(&cm, value);
184 channel_map_set = TRUE;
185 }
186
187 avahi_free(key);
188 avahi_free(value);
189 }
190
191 if (!channel_map_set && cm.channels != ss.channels)
192 pa_channel_map_init_auto(&cm, ss.channels, PA_CHANNEL_MAP_DEFAULT);
193
194 if (!pa_sample_spec_valid(&ss)) {
195 pa_log("Service '%s' contains an invalid sample specification.", name);
196 avahi_free(device);
197 goto finish;
198 }
199
200 if (!pa_channel_map_valid(&cm) || cm.channels != ss.channels) {
201 pa_log("Service '%s' contains an invalid channel map.", name);
202 avahi_free(device);
203 goto finish;
204 }
205
206 if (device)
207 dname = pa_sprintf_malloc("tunnel.%s.%s", host_name, device);
208 else
209 dname = pa_sprintf_malloc("tunnel.%s", host_name);
210
211 if (!pa_namereg_is_valid_name(dname)) {
212 pa_log("Cannot construct valid device name from credentials of service '%s'.", dname);
213 avahi_free(device);
214 pa_xfree(dname);
215 goto finish;
216 }
217
218 t = strstr(type, "sink") ? "sink" : "source";
219
220 module_name = pa_sprintf_malloc("module-tunnel-%s", t);
221 args = pa_sprintf_malloc("server=[%s]:%u "
222 "%s=%s "
223 "format=%s "
224 "channels=%u "
225 "rate=%u "
226 "%s_name=%s "
227 "channel_map=%s",
228 avahi_address_snprint(at, sizeof(at), a), port,
229 t, device,
230 pa_sample_format_to_string(ss.format),
231 ss.channels,
232 ss.rate,
233 t, dname,
234 pa_channel_map_snprint(cmt, sizeof(cmt), &cm));
235
236 pa_log_debug("Loading module-tunnel-%s with arguments '%s'", module_name, args);
237
238 if ((m = pa_module_load(u->core, module_name, args))) {
239 tnl->module_index = m->index;
240 pa_hashmap_put(u->tunnels, tnl, tnl);
241 tnl = NULL;
242 }
243
244 pa_xfree(module_name);
245 pa_xfree(dname);
246 pa_xfree(args);
247 avahi_free(device);
248 }
249
250 finish:
251
252 avahi_service_resolver_free(r);
253
254 if (tnl)
255 tunnel_free(tnl);
256 }
257
258 static void browser_cb(
259 AvahiServiceBrowser *b,
260 AvahiIfIndex interface, AvahiProtocol protocol,
261 AvahiBrowserEvent event,
262 const char *name, const char *type, const char *domain,
263 AvahiLookupResultFlags flags,
264 void *userdata) {
265
266 struct userdata *u = userdata;
267 struct tunnel *t;
268
269 pa_assert(u);
270
271 if (flags & AVAHI_LOOKUP_RESULT_LOCAL)
272 return;
273
274 t = tunnel_new(interface, protocol, name, type, domain);
275
276 if (event == AVAHI_BROWSER_NEW) {
277
278 if (!pa_hashmap_get(u->tunnels, t))
279 if (!(avahi_service_resolver_new(u->client, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, resolver_cb, u)))
280 pa_log("avahi_service_resolver_new() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
281
282 /* We ignore the returned resolver object here, since the we don't
283 * need to attach any special data to it, and we can still destory
284 * it from the callback */
285
286 } else if (event == AVAHI_BROWSER_REMOVE) {
287 struct tunnel *t2;
288
289 if ((t2 = pa_hashmap_get(u->tunnels, t))) {
290 pa_module_unload_by_index(u->core, t2->module_index);
291 pa_hashmap_remove(u->tunnels, t2);
292 tunnel_free(t2);
293 }
294 }
295
296 tunnel_free(t);
297 }
298
299 static void client_callback(AvahiClient *c, AvahiClientState state, void *userdata) {
300 struct userdata *u = userdata;
301
302 pa_assert(c);
303 pa_assert(u);
304
305 u->client = c;
306
307 switch (state) {
308 case AVAHI_CLIENT_S_REGISTERING:
309 case AVAHI_CLIENT_S_RUNNING:
310 case AVAHI_CLIENT_S_COLLISION:
311
312 if (!u->sink_browser) {
313
314 if (!(u->sink_browser = avahi_service_browser_new(
315 c,
316 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
317 SERVICE_TYPE_SINK,
318 NULL,
319 0,
320 browser_cb, u))) {
321
322 pa_log("avahi_service_browser_new() failed: %s", avahi_strerror(avahi_client_errno(c)));
323 pa_module_unload_request(u->module);
324 }
325 }
326
327 if (!u->source_browser) {
328
329 if (!(u->source_browser = avahi_service_browser_new(
330 c,
331 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
332 SERVICE_TYPE_SOURCE,
333 NULL,
334 0,
335 browser_cb, u))) {
336
337 pa_log("avahi_service_browser_new() failed: %s", avahi_strerror(avahi_client_errno(c)));
338 pa_module_unload_request(u->module);
339 }
340 }
341
342 break;
343
344 case AVAHI_CLIENT_FAILURE:
345 if (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
346 int error;
347
348 pa_log_debug("Avahi daemon disconnected.");
349
350 if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
351 pa_log("avahi_client_new() failed: %s", avahi_strerror(error));
352 pa_module_unload_request(u->module);
353 }
354 }
355
356 /* Fall through */
357
358 case AVAHI_CLIENT_CONNECTING:
359
360 if (u->sink_browser) {
361 avahi_service_browser_free(u->sink_browser);
362 u->sink_browser = NULL;
363 }
364
365 if (u->source_browser) {
366 avahi_service_browser_free(u->source_browser);
367 u->source_browser = NULL;
368 }
369
370 break;
371
372 default: ;
373 }
374 }
375
376 int pa__init(pa_module*m) {
377
378 struct userdata *u;
379 pa_modargs *ma = NULL;
380 int error;
381
382 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
383 pa_log("Failed to parse module arguments.");
384 goto fail;
385 }
386
387 m->userdata = u = pa_xnew(struct userdata, 1);
388 u->core = m->core;
389 u->module = m;
390 u->sink_browser = u->source_browser = NULL;
391
392 u->tunnels = pa_hashmap_new(tunnel_hash, tunnel_compare);
393
394 u->avahi_poll = pa_avahi_poll_new(m->core->mainloop);
395
396 if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
397 pa_log("pa_avahi_client_new() failed: %s", avahi_strerror(error));
398 goto fail;
399 }
400
401 pa_modargs_free(ma);
402
403 return 0;
404
405 fail:
406 pa__done(m);
407
408 if (ma)
409 pa_modargs_free(ma);
410
411 return -1;
412 }
413
414 void pa__done(pa_module*m) {
415 struct userdata*u;
416 pa_assert(m);
417
418 if (!(u = m->userdata))
419 return;
420
421 if (u->client)
422 avahi_client_free(u->client);
423
424 if (u->avahi_poll)
425 pa_avahi_poll_free(u->avahi_poll);
426
427 if (u->tunnels) {
428 struct tunnel *t;
429
430 while ((t = pa_hashmap_steal_first(u->tunnels))) {
431 pa_module_unload_by_index(u->core, t->module_index);
432 tunnel_free(t);
433 }
434
435 pa_hashmap_free(u->tunnels, NULL, NULL);
436 }
437
438 pa_xfree(u);
439 }