echo-cancel-test: Enable debug log level
[platform/upstream/pulseaudio.git] / 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.1 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
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>
46
47 #include "module-zeroconf-discover-symdef.h"
48
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);
53
54 #define SERVICE_TYPE_SINK "_pulse-sink._tcp"
55 #define SERVICE_TYPE_SOURCE "_non-monitor._sub._pulse-source._tcp"
56
57 static const char* const valid_modargs[] = {
58     NULL
59 };
60
61 struct tunnel {
62     AvahiIfIndex interface;
63     AvahiProtocol protocol;
64     char *name, *type, *domain;
65     uint32_t module_index;
66 };
67
68 struct userdata {
69     pa_core *core;
70     pa_module *module;
71     AvahiPoll *avahi_poll;
72     AvahiClient *client;
73     AvahiServiceBrowser *source_browser, *sink_browser;
74
75     pa_hashmap *tunnels;
76 };
77
78 static unsigned tunnel_hash(const void *p) {
79     const struct tunnel *t = p;
80
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);
87 }
88
89 static int tunnel_compare(const void *a, const void *b) {
90     const struct tunnel *ta = a, *tb = b;
91     int r;
92
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;
103
104     return 0;
105 }
106
107 static struct tunnel *tunnel_new(
108         AvahiIfIndex interface, AvahiProtocol protocol,
109         const char *name, const char *type, const char *domain) {
110
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;
120 }
121
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);
128 }
129
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) {
139
140     struct userdata *u = userdata;
141     struct tunnel *tnl;
142
143     pa_assert(u);
144
145     tnl = tunnel_new(interface, protocol, name, type, domain);
146
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;
158
159         ss = u->core->default_sample_spec;
160         cm = u->core->default_channel_map;
161
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);
165
166             if (pa_streq(key, "device")) {
167                 pa_xfree(device);
168                 device = value;
169                 value = NULL;
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;
179             }
180
181             avahi_free(key);
182             avahi_free(value);
183         }
184
185         if (!channel_map_set && cm.channels != ss.channels)
186             pa_channel_map_init_extend(&cm, ss.channels, PA_CHANNEL_MAP_DEFAULT);
187
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;
192         }
193
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;
198         }
199
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);
204
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;
210         }
211
212         t = strstr(type, "sink") ? "sink" : "source";
213
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));
229
230         pa_log_debug("Loading %s with arguments '%s'", module_name, args);
231
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;
236         }
237
238         pa_xfree(module_name);
239         pa_xfree(dname);
240         pa_xfree(args);
241         avahi_free(device);
242     }
243
244 finish:
245
246     avahi_service_resolver_free(r);
247
248     if (tnl)
249         tunnel_free(tnl);
250 }
251
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) {
259
260     struct userdata *u = userdata;
261     struct tunnel *t;
262
263     pa_assert(u);
264
265     if (flags & AVAHI_LOOKUP_RESULT_LOCAL)
266         return;
267
268     t = tunnel_new(interface, protocol, name, type, domain);
269
270     if (event == AVAHI_BROWSER_NEW) {
271
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)));
275
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 */
279
280     } else if (event == AVAHI_BROWSER_REMOVE) {
281         struct tunnel *t2;
282
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);
287         }
288     }
289
290     tunnel_free(t);
291 }
292
293 static void client_callback(AvahiClient *c, AvahiClientState state, void *userdata) {
294     struct userdata *u = userdata;
295
296     pa_assert(c);
297     pa_assert(u);
298
299     u->client = c;
300
301     switch (state) {
302         case AVAHI_CLIENT_S_REGISTERING:
303         case AVAHI_CLIENT_S_RUNNING:
304         case AVAHI_CLIENT_S_COLLISION:
305
306             if (!u->sink_browser) {
307
308                 if (!(u->sink_browser = avahi_service_browser_new(
309                               c,
310                               AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
311                               SERVICE_TYPE_SINK,
312                               NULL,
313                               0,
314                               browser_cb, u))) {
315
316                     pa_log("avahi_service_browser_new() failed: %s", avahi_strerror(avahi_client_errno(c)));
317                     pa_module_unload_request(u->module, TRUE);
318                 }
319             }
320
321             if (!u->source_browser) {
322
323                 if (!(u->source_browser = avahi_service_browser_new(
324                               c,
325                               AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
326                               SERVICE_TYPE_SOURCE,
327                               NULL,
328                               0,
329                               browser_cb, u))) {
330
331                     pa_log("avahi_service_browser_new() failed: %s", avahi_strerror(avahi_client_errno(c)));
332                     pa_module_unload_request(u->module, TRUE);
333                 }
334             }
335
336             break;
337
338         case AVAHI_CLIENT_FAILURE:
339             if (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
340                 int error;
341
342                 pa_log_debug("Avahi daemon disconnected.");
343
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);
347                 }
348             }
349
350             /* Fall through */
351
352         case AVAHI_CLIENT_CONNECTING:
353
354             if (u->sink_browser) {
355                 avahi_service_browser_free(u->sink_browser);
356                 u->sink_browser = NULL;
357             }
358
359             if (u->source_browser) {
360                 avahi_service_browser_free(u->source_browser);
361                 u->source_browser = NULL;
362             }
363
364             break;
365
366         default: ;
367     }
368 }
369
370 int pa__init(pa_module*m) {
371
372     struct userdata *u;
373     pa_modargs *ma = NULL;
374     int error;
375
376     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
377         pa_log("Failed to parse module arguments.");
378         goto fail;
379     }
380
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;
385
386     u->tunnels = pa_hashmap_new(tunnel_hash, tunnel_compare);
387
388     u->avahi_poll = pa_avahi_poll_new(m->core->mainloop);
389
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;
393     }
394
395     pa_modargs_free(ma);
396
397     return 0;
398
399 fail:
400     pa__done(m);
401
402     if (ma)
403         pa_modargs_free(ma);
404
405     return -1;
406 }
407
408 void pa__done(pa_module*m) {
409     struct userdata*u;
410     pa_assert(m);
411
412     if (!(u = m->userdata))
413         return;
414
415     if (u->client)
416         avahi_client_free(u->client);
417
418     if (u->avahi_poll)
419         pa_avahi_poll_free(u->avahi_poll);
420
421     if (u->tunnels) {
422         struct tunnel *t;
423
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);
427         }
428
429         pa_hashmap_free(u->tunnels, NULL);
430     }
431
432     pa_xfree(u);
433 }