Add "Rear Mic" to alsa mixer paths.
[profile/ivi/pulseaudio.git] / src / modules / module-zeroconf-publish.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/publish.h>
33 #include <avahi-common/alternative.h>
34 #include <avahi-common/error.h>
35 #include <avahi-common/domain.h>
36
37 #include <pulse/xmalloc.h>
38 #include <pulse/util.h>
39
40 #include <pulsecore/parseaddr.h>
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/dynarray.h>
48 #include <pulsecore/modargs.h>
49 #include <pulsecore/avahi-wrap.h>
50 #include <pulsecore/endianmacros.h>
51 #include <pulsecore/protocol-native.h>
52
53 #include "module-zeroconf-publish-symdef.h"
54
55 PA_MODULE_AUTHOR("Lennart Poettering");
56 PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Publisher");
57 PA_MODULE_VERSION(PACKAGE_VERSION);
58 PA_MODULE_LOAD_ONCE(TRUE);
59
60 #define SERVICE_TYPE_SINK "_pulse-sink._tcp"
61 #define SERVICE_TYPE_SOURCE "_pulse-source._tcp"
62 #define SERVICE_TYPE_SERVER "_pulse-server._tcp"
63 #define SERVICE_SUBTYPE_SINK_HARDWARE "_hardware._sub."SERVICE_TYPE_SINK
64 #define SERVICE_SUBTYPE_SINK_VIRTUAL "_virtual._sub."SERVICE_TYPE_SINK
65 #define SERVICE_SUBTYPE_SOURCE_HARDWARE "_hardware._sub."SERVICE_TYPE_SOURCE
66 #define SERVICE_SUBTYPE_SOURCE_VIRTUAL "_virtual._sub."SERVICE_TYPE_SOURCE
67 #define SERVICE_SUBTYPE_SOURCE_MONITOR "_monitor._sub."SERVICE_TYPE_SOURCE
68 #define SERVICE_SUBTYPE_SOURCE_NON_MONITOR "_non-monitor._sub."SERVICE_TYPE_SOURCE
69
70 static const char* const valid_modargs[] = {
71     NULL
72 };
73
74 enum service_subtype {
75     SUBTYPE_HARDWARE,
76     SUBTYPE_VIRTUAL,
77     SUBTYPE_MONITOR
78 };
79
80 struct service {
81     struct userdata *userdata;
82     AvahiEntryGroup *entry_group;
83     char *service_name;
84     pa_object *device;
85     enum service_subtype subtype;
86 };
87
88 struct userdata {
89     pa_core *core;
90     pa_module *module;
91
92     AvahiPoll *avahi_poll;
93     AvahiClient *client;
94
95     pa_hashmap *services;
96     char *service_name;
97
98     AvahiEntryGroup *main_entry_group;
99
100     pa_hook_slot *sink_new_slot, *source_new_slot, *sink_unlink_slot, *source_unlink_slot, *sink_changed_slot, *source_changed_slot;
101
102     pa_native_protocol *native;
103 };
104
105 static void get_service_data(struct service *s, pa_sample_spec *ret_ss, pa_channel_map *ret_map, const char **ret_name, pa_proplist **ret_proplist, enum service_subtype *ret_subtype) {
106     pa_assert(s);
107     pa_assert(ret_ss);
108     pa_assert(ret_proplist);
109     pa_assert(ret_subtype);
110
111     if (pa_sink_isinstance(s->device)) {
112         pa_sink *sink = PA_SINK(s->device);
113
114         *ret_ss = sink->sample_spec;
115         *ret_map = sink->channel_map;
116         *ret_name = sink->name;
117         *ret_proplist = sink->proplist;
118         *ret_subtype = sink->flags & PA_SINK_HARDWARE ? SUBTYPE_HARDWARE : SUBTYPE_VIRTUAL;
119
120     } else if (pa_source_isinstance(s->device)) {
121         pa_source *source = PA_SOURCE(s->device);
122
123         *ret_ss = source->sample_spec;
124         *ret_map = source->channel_map;
125         *ret_name = source->name;
126         *ret_proplist = source->proplist;
127         *ret_subtype = source->monitor_of ? SUBTYPE_MONITOR : (source->flags & PA_SOURCE_HARDWARE ? SUBTYPE_HARDWARE : SUBTYPE_VIRTUAL);
128
129     } else
130         pa_assert_not_reached();
131 }
132
133 static AvahiStringList* txt_record_server_data(pa_core *c, AvahiStringList *l) {
134     char s[128];
135     char *t;
136
137     pa_assert(c);
138
139     l = avahi_string_list_add_pair(l, "server-version", PACKAGE_NAME" "PACKAGE_VERSION);
140
141     t = pa_get_user_name_malloc();
142     l = avahi_string_list_add_pair(l, "user-name", t);
143     pa_xfree(t);
144
145     t = pa_machine_id();
146     l = avahi_string_list_add_pair(l, "machine-id", t);
147     pa_xfree(t);
148
149     t = pa_uname_string();
150     l = avahi_string_list_add_pair(l, "uname", t);
151     pa_xfree(t);
152
153     l = avahi_string_list_add_pair(l, "fqdn", pa_get_fqdn(s, sizeof(s)));
154     l = avahi_string_list_add_printf(l, "cookie=0x%08x", c->cookie);
155
156     return l;
157 }
158
159 static int publish_service(struct service *s);
160
161 static void service_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
162     struct service *s = userdata;
163
164     pa_assert(s);
165
166     switch (state) {
167
168         case AVAHI_ENTRY_GROUP_ESTABLISHED:
169             pa_log_info("Successfully established service %s.", s->service_name);
170             break;
171
172         case AVAHI_ENTRY_GROUP_COLLISION: {
173             char *t;
174
175             t = avahi_alternative_service_name(s->service_name);
176             pa_log_info("Name collision, renaming %s to %s.", s->service_name, t);
177             pa_xfree(s->service_name);
178             s->service_name = t;
179
180             publish_service(s);
181             break;
182         }
183
184         case AVAHI_ENTRY_GROUP_FAILURE: {
185             pa_log("Failed to register service: %s", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
186
187             avahi_entry_group_free(g);
188             s->entry_group = NULL;
189
190             break;
191         }
192
193         case AVAHI_ENTRY_GROUP_UNCOMMITED:
194         case AVAHI_ENTRY_GROUP_REGISTERING:
195             ;
196     }
197 }
198
199 static void service_free(struct service *s);
200
201 static uint16_t compute_port(struct userdata *u) {
202     pa_strlist *i;
203
204     pa_assert(u);
205
206     for (i = pa_native_protocol_servers(u->native); i; i = pa_strlist_next(i)) {
207         pa_parsed_address a;
208
209         if (pa_parse_address(pa_strlist_data(i), &a) >= 0 &&
210             (a.type == PA_PARSED_ADDRESS_TCP4 ||
211              a.type == PA_PARSED_ADDRESS_TCP6 ||
212              a.type == PA_PARSED_ADDRESS_TCP_AUTO) &&
213             a.port > 0) {
214
215             pa_xfree(a.path_or_host);
216             return a.port;
217         }
218
219         pa_xfree(a.path_or_host);
220     }
221
222     return PA_NATIVE_DEFAULT_PORT;
223 }
224
225 static int publish_service(struct service *s) {
226     int r = -1;
227     AvahiStringList *txt = NULL;
228     const char *name = NULL, *t;
229     pa_proplist *proplist = NULL;
230     pa_sample_spec ss;
231     pa_channel_map map;
232     char cm[PA_CHANNEL_MAP_SNPRINT_MAX];
233     enum service_subtype subtype;
234
235     const char * const subtype_text[] = {
236         [SUBTYPE_HARDWARE] = "hardware",
237         [SUBTYPE_VIRTUAL] = "virtual",
238         [SUBTYPE_MONITOR] = "monitor"
239     };
240
241     pa_assert(s);
242
243     if (!s->userdata->client || avahi_client_get_state(s->userdata->client) != AVAHI_CLIENT_S_RUNNING)
244         return 0;
245
246     if (!s->entry_group) {
247         if (!(s->entry_group = avahi_entry_group_new(s->userdata->client, service_entry_group_callback, s))) {
248             pa_log("avahi_entry_group_new(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
249             goto finish;
250         }
251     } else
252         avahi_entry_group_reset(s->entry_group);
253
254     txt = txt_record_server_data(s->userdata->core, txt);
255
256     get_service_data(s, &ss, &map, &name, &proplist, &subtype);
257     txt = avahi_string_list_add_pair(txt, "device", name);
258     txt = avahi_string_list_add_printf(txt, "rate=%u", ss.rate);
259     txt = avahi_string_list_add_printf(txt, "channels=%u", ss.channels);
260     txt = avahi_string_list_add_pair(txt, "format", pa_sample_format_to_string(ss.format));
261     txt = avahi_string_list_add_pair(txt, "channel_map", pa_channel_map_snprint(cm, sizeof(cm), &map));
262     txt = avahi_string_list_add_pair(txt, "subtype", subtype_text[subtype]);
263
264     if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_DESCRIPTION)))
265         txt = avahi_string_list_add_pair(txt, "description", t);
266     if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_ICON_NAME)))
267         txt = avahi_string_list_add_pair(txt, "icon-name", t);
268     if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_VENDOR_NAME)))
269         txt = avahi_string_list_add_pair(txt, "vendor-name", t);
270     if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_PRODUCT_NAME)))
271         txt = avahi_string_list_add_pair(txt, "product-name", t);
272     if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_CLASS)))
273         txt = avahi_string_list_add_pair(txt, "class", t);
274     if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_FORM_FACTOR)))
275         txt = avahi_string_list_add_pair(txt, "form-factor", t);
276
277     if (avahi_entry_group_add_service_strlst(
278                 s->entry_group,
279                 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
280                 0,
281                 s->service_name,
282                 pa_sink_isinstance(s->device) ? SERVICE_TYPE_SINK : SERVICE_TYPE_SOURCE,
283                 NULL,
284                 NULL,
285                 compute_port(s->userdata),
286                 txt) < 0) {
287
288         pa_log("avahi_entry_group_add_service_strlst(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
289         goto finish;
290     }
291
292     if (avahi_entry_group_add_service_subtype(
293                 s->entry_group,
294                 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
295                 0,
296                 s->service_name,
297                 pa_sink_isinstance(s->device) ? SERVICE_TYPE_SINK : SERVICE_TYPE_SOURCE,
298                 NULL,
299                 pa_sink_isinstance(s->device) ? (subtype == SUBTYPE_HARDWARE ? SERVICE_SUBTYPE_SINK_HARDWARE : SERVICE_SUBTYPE_SINK_VIRTUAL) :
300                 (subtype == SUBTYPE_HARDWARE ? SERVICE_SUBTYPE_SOURCE_HARDWARE : (subtype == SUBTYPE_VIRTUAL ? SERVICE_SUBTYPE_SOURCE_VIRTUAL : SERVICE_SUBTYPE_SOURCE_MONITOR))) < 0) {
301
302         pa_log("avahi_entry_group_add_service_subtype(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
303         goto finish;
304     }
305
306     if (pa_source_isinstance(s->device) && subtype != SUBTYPE_MONITOR) {
307         if (avahi_entry_group_add_service_subtype(
308                     s->entry_group,
309                     AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
310                     0,
311                     s->service_name,
312                     SERVICE_TYPE_SOURCE,
313                     NULL,
314                     SERVICE_SUBTYPE_SOURCE_NON_MONITOR) < 0) {
315
316             pa_log("avahi_entry_group_add_service_subtype(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
317             goto finish;
318         }
319     }
320
321     if (avahi_entry_group_commit(s->entry_group) < 0) {
322         pa_log("avahi_entry_group_commit(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
323         goto finish;
324     }
325
326     r = 0;
327     pa_log_debug("Successfully created entry group for %s.", s->service_name);
328
329 finish:
330
331     /* Remove this service */
332     if (r < 0)
333         service_free(s);
334
335     avahi_string_list_free(txt);
336
337     return r;
338 }
339
340 static struct service *get_service(struct userdata *u, pa_object *device) {
341     struct service *s;
342     char *hn, *un;
343     const char *n;
344
345     pa_assert(u);
346     pa_object_assert_ref(device);
347
348     if ((s = pa_hashmap_get(u->services, device)))
349         return s;
350
351     s = pa_xnew(struct service, 1);
352     s->userdata = u;
353     s->entry_group = NULL;
354     s->device = device;
355
356     if (pa_sink_isinstance(device)) {
357         if (!(n = pa_proplist_gets(PA_SINK(device)->proplist, PA_PROP_DEVICE_DESCRIPTION)))
358             n = PA_SINK(device)->name;
359     } else {
360         if (!(n = pa_proplist_gets(PA_SOURCE(device)->proplist, PA_PROP_DEVICE_DESCRIPTION)))
361             n = PA_SOURCE(device)->name;
362     }
363
364     hn = pa_get_host_name_malloc();
365     un = pa_get_user_name_malloc();
366
367     s->service_name = pa_truncate_utf8(pa_sprintf_malloc("%s@%s: %s", un, hn, n), AVAHI_LABEL_MAX-1);
368
369     pa_xfree(un);
370     pa_xfree(hn);
371
372     pa_hashmap_put(u->services, s->device, s);
373
374     return s;
375 }
376
377 static void service_free(struct service *s) {
378     pa_assert(s);
379
380     pa_hashmap_remove(s->userdata->services, s->device);
381
382     if (s->entry_group) {
383         pa_log_debug("Removing entry group for %s.", s->service_name);
384         avahi_entry_group_free(s->entry_group);
385     }
386
387     pa_xfree(s->service_name);
388     pa_xfree(s);
389 }
390
391 static pa_bool_t shall_ignore(pa_object *o) {
392     pa_object_assert_ref(o);
393
394     if (pa_sink_isinstance(o))
395         return !!(PA_SINK(o)->flags & PA_SINK_NETWORK);
396
397     if (pa_source_isinstance(o))
398         return PA_SOURCE(o)->monitor_of || (PA_SOURCE(o)->flags & PA_SOURCE_NETWORK);
399
400     pa_assert_not_reached();
401 }
402
403 static pa_hook_result_t device_new_or_changed_cb(pa_core *c, pa_object *o, struct userdata *u) {
404     pa_assert(c);
405     pa_object_assert_ref(o);
406
407     if (!shall_ignore(o))
408         publish_service(get_service(u, o));
409
410     return PA_HOOK_OK;
411 }
412
413 static pa_hook_result_t device_unlink_cb(pa_core *c, pa_object *o, struct userdata *u) {
414     struct service *s;
415
416     pa_assert(c);
417     pa_object_assert_ref(o);
418
419     if ((s = pa_hashmap_get(u->services, o)))
420         service_free(s);
421
422     return PA_HOOK_OK;
423 }
424
425 static int publish_main_service(struct userdata *u);
426
427 static void main_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
428     struct userdata *u = userdata;
429     pa_assert(u);
430
431     switch (state) {
432
433         case AVAHI_ENTRY_GROUP_ESTABLISHED:
434             pa_log_info("Successfully established main service.");
435             break;
436
437         case AVAHI_ENTRY_GROUP_COLLISION: {
438             char *t;
439
440             t = avahi_alternative_service_name(u->service_name);
441             pa_log_info("Name collision: renaming main service %s to %s.", u->service_name, t);
442             pa_xfree(u->service_name);
443             u->service_name = t;
444
445             publish_main_service(u);
446             break;
447         }
448
449         case AVAHI_ENTRY_GROUP_FAILURE: {
450             pa_log("Failed to register main service: %s", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
451
452             avahi_entry_group_free(g);
453             u->main_entry_group = NULL;
454             break;
455         }
456
457         case AVAHI_ENTRY_GROUP_UNCOMMITED:
458         case AVAHI_ENTRY_GROUP_REGISTERING:
459             break;
460     }
461 }
462
463 static int publish_main_service(struct userdata *u) {
464     AvahiStringList *txt = NULL;
465     int r = -1;
466
467     pa_assert(u);
468
469     if (!u->main_entry_group) {
470         if (!(u->main_entry_group = avahi_entry_group_new(u->client, main_entry_group_callback, u))) {
471             pa_log("avahi_entry_group_new() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
472             goto fail;
473         }
474     } else
475         avahi_entry_group_reset(u->main_entry_group);
476
477     txt = txt_record_server_data(u->core, txt);
478
479     if (avahi_entry_group_add_service_strlst(
480                 u->main_entry_group,
481                 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
482                 0,
483                 u->service_name,
484                 SERVICE_TYPE_SERVER,
485                 NULL,
486                 NULL,
487                 compute_port(u),
488                 txt) < 0) {
489
490         pa_log("avahi_entry_group_add_service_strlst() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
491         goto fail;
492     }
493
494     if (avahi_entry_group_commit(u->main_entry_group) < 0) {
495         pa_log("avahi_entry_group_commit() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
496         goto fail;
497     }
498
499     r = 0;
500
501 fail:
502     avahi_string_list_free(txt);
503
504     return r;
505 }
506
507 static int publish_all_services(struct userdata *u) {
508     pa_sink *sink;
509     pa_source *source;
510     int r = -1;
511     uint32_t idx;
512
513     pa_assert(u);
514
515     pa_log_debug("Publishing services in Zeroconf");
516
517     for (sink = PA_SINK(pa_idxset_first(u->core->sinks, &idx)); sink; sink = PA_SINK(pa_idxset_next(u->core->sinks, &idx)))
518         if (!shall_ignore(PA_OBJECT(sink)))
519             publish_service(get_service(u, PA_OBJECT(sink)));
520
521     for (source = PA_SOURCE(pa_idxset_first(u->core->sources, &idx)); source; source = PA_SOURCE(pa_idxset_next(u->core->sources, &idx)))
522         if (!shall_ignore(PA_OBJECT(source)))
523             publish_service(get_service(u, PA_OBJECT(source)));
524
525     if (publish_main_service(u) < 0)
526         goto fail;
527
528     r = 0;
529
530 fail:
531     return r;
532 }
533
534 static void unpublish_all_services(struct userdata *u, pa_bool_t rem) {
535     void *state = NULL;
536     struct service *s;
537
538     pa_assert(u);
539
540     pa_log_debug("Unpublishing services in Zeroconf");
541
542     while ((s = pa_hashmap_iterate(u->services, &state, NULL))) {
543         if (s->entry_group) {
544             if (rem) {
545                 pa_log_debug("Removing entry group for %s.", s->service_name);
546                 avahi_entry_group_free(s->entry_group);
547                 s->entry_group = NULL;
548             } else {
549                 avahi_entry_group_reset(s->entry_group);
550                 pa_log_debug("Resetting entry group for %s.", s->service_name);
551             }
552         }
553     }
554
555     if (u->main_entry_group) {
556         if (rem) {
557             pa_log_debug("Removing main entry group.");
558             avahi_entry_group_free(u->main_entry_group);
559             u->main_entry_group = NULL;
560         } else {
561             avahi_entry_group_reset(u->main_entry_group);
562             pa_log_debug("Resetting main entry group.");
563         }
564     }
565 }
566
567 static void client_callback(AvahiClient *c, AvahiClientState state, void *userdata) {
568     struct userdata *u = userdata;
569
570     pa_assert(c);
571     pa_assert(u);
572
573     u->client = c;
574
575     switch (state) {
576         case AVAHI_CLIENT_S_RUNNING:
577             publish_all_services(u);
578             break;
579
580         case AVAHI_CLIENT_S_COLLISION:
581             pa_log_debug("Host name collision");
582             unpublish_all_services(u, FALSE);
583             break;
584
585         case AVAHI_CLIENT_FAILURE:
586             if (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
587                 int error;
588
589                 pa_log_debug("Avahi daemon disconnected.");
590
591                 unpublish_all_services(u, TRUE);
592                 avahi_client_free(u->client);
593
594                 if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
595                     pa_log("avahi_client_new() failed: %s", avahi_strerror(error));
596                     pa_module_unload_request(u->module, TRUE);
597                 }
598             }
599
600             break;
601
602         default: ;
603     }
604 }
605
606 int pa__init(pa_module*m) {
607
608     struct userdata *u;
609     pa_modargs *ma = NULL;
610     char *hn, *un;
611     int error;
612
613     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
614         pa_log("Failed to parse module arguments.");
615         goto fail;
616     }
617
618     m->userdata = u = pa_xnew(struct userdata, 1);
619     u->core = m->core;
620     u->module = m;
621     u->native = pa_native_protocol_get(u->core);
622
623     u->avahi_poll = pa_avahi_poll_new(m->core->mainloop);
624
625     u->services = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
626
627     u->sink_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_LATE, (pa_hook_cb_t) device_new_or_changed_cb, u);
628     u->sink_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PROPLIST_CHANGED], PA_HOOK_LATE, (pa_hook_cb_t) device_new_or_changed_cb, u);
629     u->sink_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) device_unlink_cb, u);
630     u->source_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_LATE, (pa_hook_cb_t) device_new_or_changed_cb, u);
631     u->source_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PROPLIST_CHANGED], PA_HOOK_LATE, (pa_hook_cb_t) device_new_or_changed_cb, u);
632     u->source_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) device_unlink_cb, u);
633
634     u->main_entry_group = NULL;
635
636     un = pa_get_user_name_malloc();
637     hn = pa_get_host_name_malloc();
638     u->service_name = pa_truncate_utf8(pa_sprintf_malloc("%s@%s", un, hn), AVAHI_LABEL_MAX-1);
639     pa_xfree(un);
640     pa_xfree(hn);
641
642     if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
643         pa_log("avahi_client_new() failed: %s", avahi_strerror(error));
644         goto fail;
645     }
646
647     pa_modargs_free(ma);
648
649     return 0;
650
651 fail:
652     pa__done(m);
653
654     if (ma)
655         pa_modargs_free(ma);
656
657     return -1;
658 }
659
660 void pa__done(pa_module*m) {
661     struct userdata*u;
662     pa_assert(m);
663
664     if (!(u = m->userdata))
665         return;
666
667     if (u->services) {
668         struct service *s;
669
670         while ((s = pa_hashmap_first(u->services)))
671             service_free(s);
672
673         pa_hashmap_free(u->services, NULL, NULL);
674     }
675
676     if (u->sink_new_slot)
677         pa_hook_slot_free(u->sink_new_slot);
678     if (u->source_new_slot)
679         pa_hook_slot_free(u->source_new_slot);
680     if (u->sink_changed_slot)
681         pa_hook_slot_free(u->sink_changed_slot);
682     if (u->source_changed_slot)
683         pa_hook_slot_free(u->source_changed_slot);
684     if (u->sink_unlink_slot)
685         pa_hook_slot_free(u->sink_unlink_slot);
686     if (u->source_unlink_slot)
687         pa_hook_slot_free(u->source_unlink_slot);
688
689     if (u->main_entry_group)
690         avahi_entry_group_free(u->main_entry_group);
691
692     if (u->client)
693         avahi_client_free(u->client);
694
695     if (u->avahi_poll)
696         pa_avahi_poll_free(u->avahi_poll);
697
698     if (u->native)
699         pa_native_protocol_unref(u->native);
700
701     pa_xfree(u->service_name);
702     pa_xfree(u);
703 }