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