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