]> code.delx.au - pulseaudio/blob - src/modules/module-zeroconf-publish.c
zeroconf: Make Avahi usage in m-z-publish async
[pulseaudio] / 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 <unistd.h>
29
30 #include <avahi-client/client.h>
31 #include <avahi-client/publish.h>
32 #include <avahi-common/alternative.h>
33 #include <avahi-common/error.h>
34 #include <avahi-common/domain.h>
35
36 #include <pulse/xmalloc.h>
37 #include <pulse/util.h>
38 #include <pulse/thread-mainloop.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/dynarray.h>
47 #include <pulsecore/modargs.h>
48 #include <pulsecore/avahi-wrap.h>
49 #include <pulsecore/protocol-native.h>
50
51 #include "module-zeroconf-publish-symdef.h"
52
53 PA_MODULE_AUTHOR("Lennart Poettering");
54 PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Publisher");
55 PA_MODULE_VERSION(PACKAGE_VERSION);
56 PA_MODULE_LOAD_ONCE(TRUE);
57
58 #define SERVICE_TYPE_SINK "_pulse-sink._tcp"
59 #define SERVICE_TYPE_SOURCE "_pulse-source._tcp"
60 #define SERVICE_TYPE_SERVER "_pulse-server._tcp"
61 #define SERVICE_SUBTYPE_SINK_HARDWARE "_hardware._sub."SERVICE_TYPE_SINK
62 #define SERVICE_SUBTYPE_SINK_VIRTUAL "_virtual._sub."SERVICE_TYPE_SINK
63 #define SERVICE_SUBTYPE_SOURCE_HARDWARE "_hardware._sub."SERVICE_TYPE_SOURCE
64 #define SERVICE_SUBTYPE_SOURCE_VIRTUAL "_virtual._sub."SERVICE_TYPE_SOURCE
65 #define SERVICE_SUBTYPE_SOURCE_MONITOR "_monitor._sub."SERVICE_TYPE_SOURCE
66 #define SERVICE_SUBTYPE_SOURCE_NON_MONITOR "_non-monitor._sub."SERVICE_TYPE_SOURCE
67
68 /*
69 * Note: Because the core avahi-client calls result in synchronous D-Bus
70 * communication, calling any of those functions in the PA mainloop context
71 * could lead to the mainloop being blocked for long periods.
72 *
73 * To avoid this, we create a threaded-mainloop for Avahi calls, and push all
74 * D-Bus communication into that thread. The thumb-rule for the split is:
75 *
76 * 1. If access to PA data structures is needed, use the PA mainloop context
77 *
78 * 2. If a (blocking) avahi-client call is needed, use the Avahi mainloop
79 *
80 * We do have message queue to pass messages from the Avahi mainloop to the PA
81 * mainloop.
82 */
83
84 static const char* const valid_modargs[] = {
85 NULL
86 };
87
88 struct avahi_msg {
89 pa_msgobject parent;
90 };
91
92 typedef struct avahi_msg avahi_msg;
93 PA_DEFINE_PRIVATE_CLASS(avahi_msg, pa_msgobject);
94
95 enum {
96 AVAHI_MESSAGE_PUBLISH_ALL,
97 AVAHI_MESSAGE_SHUTDOWN_START,
98 AVAHI_MESSAGE_SHUTDOWN_COMPLETE,
99 };
100
101 enum service_subtype {
102 SUBTYPE_HARDWARE,
103 SUBTYPE_VIRTUAL,
104 SUBTYPE_MONITOR
105 };
106
107 struct service {
108 void *key;
109
110 struct userdata *userdata;
111 AvahiEntryGroup *entry_group;
112 char *service_name;
113 const char *service_type;
114 enum service_subtype subtype;
115
116 char *name;
117 bool is_sink;
118 pa_sample_spec ss;
119 pa_channel_map cm;
120 pa_proplist *proplist;
121 };
122
123 struct userdata {
124 pa_thread_mq thread_mq;
125 pa_rtpoll *rtpoll;
126 avahi_msg *msg;
127
128 pa_core *core;
129 pa_module *module;
130 pa_mainloop_api *api;
131 pa_threaded_mainloop *mainloop;
132
133 AvahiPoll *avahi_poll;
134 AvahiClient *client;
135
136 pa_hashmap *services; /* protect with mainloop lock */
137 char *service_name;
138
139 AvahiEntryGroup *main_entry_group;
140
141 pa_hook_slot *sink_new_slot, *source_new_slot, *sink_unlink_slot, *source_unlink_slot, *sink_changed_slot, *source_changed_slot;
142
143 pa_native_protocol *native;
144 };
145
146 /* Runs in PA mainloop context */
147 static void get_service_data(struct service *s, pa_object *device) {
148 pa_assert(s);
149
150 if (pa_sink_isinstance(device)) {
151 pa_sink *sink = PA_SINK(device);
152
153 s->is_sink = TRUE;
154 s->service_type = SERVICE_TYPE_SINK;
155 s->ss = sink->sample_spec;
156 s->cm = sink->channel_map;
157 s->name = pa_xstrdup(sink->name);
158 s->proplist = pa_proplist_copy(sink->proplist);
159 s->subtype = sink->flags & PA_SINK_HARDWARE ? SUBTYPE_HARDWARE : SUBTYPE_VIRTUAL;
160
161 } else if (pa_source_isinstance(device)) {
162 pa_source *source = PA_SOURCE(device);
163
164 s->is_sink = FALSE;
165 s->service_type = SERVICE_TYPE_SOURCE;
166 s->ss = source->sample_spec;
167 s->cm = source->channel_map;
168 s->name = pa_xstrdup(source->name);
169 s->proplist = pa_proplist_copy(source->proplist);
170 s->subtype = source->monitor_of ? SUBTYPE_MONITOR : (source->flags & PA_SOURCE_HARDWARE ? SUBTYPE_HARDWARE : SUBTYPE_VIRTUAL);
171
172 } else
173 pa_assert_not_reached();
174 }
175
176 /* Can be used in either PA or Avahi mainloop context since the bits of u->core
177 * that we access don't change after startup. */
178 static AvahiStringList* txt_record_server_data(pa_core *c, AvahiStringList *l) {
179 char s[128];
180 char *t;
181
182 pa_assert(c);
183
184 l = avahi_string_list_add_pair(l, "server-version", PACKAGE_NAME" "PACKAGE_VERSION);
185
186 t = pa_get_user_name_malloc();
187 l = avahi_string_list_add_pair(l, "user-name", t);
188 pa_xfree(t);
189
190 t = pa_machine_id();
191 l = avahi_string_list_add_pair(l, "machine-id", t);
192 pa_xfree(t);
193
194 t = pa_uname_string();
195 l = avahi_string_list_add_pair(l, "uname", t);
196 pa_xfree(t);
197
198 l = avahi_string_list_add_pair(l, "fqdn", pa_get_fqdn(s, sizeof(s)));
199 l = avahi_string_list_add_printf(l, "cookie=0x%08x", c->cookie);
200
201 return l;
202 }
203
204 static void publish_service(pa_mainloop_api *api, void *service);
205
206 /* Runs in Avahi mainloop context */
207 static void service_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
208 struct service *s = userdata;
209
210 pa_assert(s);
211
212 switch (state) {
213
214 case AVAHI_ENTRY_GROUP_ESTABLISHED:
215 pa_log_info("Successfully established service %s.", s->service_name);
216 break;
217
218 case AVAHI_ENTRY_GROUP_COLLISION: {
219 char *t;
220
221 t = avahi_alternative_service_name(s->service_name);
222 pa_log_info("Name collision, renaming %s to %s.", s->service_name, t);
223 pa_xfree(s->service_name);
224 s->service_name = t;
225
226 publish_service(NULL, s);
227 break;
228 }
229
230 case AVAHI_ENTRY_GROUP_FAILURE: {
231 pa_log("Failed to register service: %s", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
232
233 avahi_entry_group_free(g);
234 s->entry_group = NULL;
235
236 break;
237 }
238
239 case AVAHI_ENTRY_GROUP_UNCOMMITED:
240 case AVAHI_ENTRY_GROUP_REGISTERING:
241 ;
242 }
243 }
244
245 static void service_free(struct service *s);
246
247 /* Can run in either context */
248 static uint16_t compute_port(struct userdata *u) {
249 pa_strlist *i;
250
251 pa_assert(u);
252
253 for (i = pa_native_protocol_servers(u->native); i; i = pa_strlist_next(i)) {
254 pa_parsed_address a;
255
256 if (pa_parse_address(pa_strlist_data(i), &a) >= 0 &&
257 (a.type == PA_PARSED_ADDRESS_TCP4 ||
258 a.type == PA_PARSED_ADDRESS_TCP6 ||
259 a.type == PA_PARSED_ADDRESS_TCP_AUTO) &&
260 a.port > 0) {
261
262 pa_xfree(a.path_or_host);
263 return a.port;
264 }
265
266 pa_xfree(a.path_or_host);
267 }
268
269 return PA_NATIVE_DEFAULT_PORT;
270 }
271
272 /* Runs in Avahi mainloop context */
273 static void publish_service(pa_mainloop_api *api PA_GCC_UNUSED, void *service) {
274 struct service *s = (struct service *) service;
275 int r = -1;
276 AvahiStringList *txt = NULL;
277 char cm[PA_CHANNEL_MAP_SNPRINT_MAX];
278 const char *t;
279
280 const char * const subtype_text[] = {
281 [SUBTYPE_HARDWARE] = "hardware",
282 [SUBTYPE_VIRTUAL] = "virtual",
283 [SUBTYPE_MONITOR] = "monitor"
284 };
285
286 pa_assert(s);
287
288 if (!s->userdata->client || avahi_client_get_state(s->userdata->client) != AVAHI_CLIENT_S_RUNNING)
289 return;
290
291 if (!s->entry_group) {
292 if (!(s->entry_group = avahi_entry_group_new(s->userdata->client, service_entry_group_callback, s))) {
293 pa_log("avahi_entry_group_new(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
294 goto finish;
295 }
296 } else
297 avahi_entry_group_reset(s->entry_group);
298
299 txt = txt_record_server_data(s->userdata->core, txt);
300
301 txt = avahi_string_list_add_pair(txt, "device", s->name);
302 txt = avahi_string_list_add_printf(txt, "rate=%u", s->ss.rate);
303 txt = avahi_string_list_add_printf(txt, "channels=%u", s->ss.channels);
304 txt = avahi_string_list_add_pair(txt, "format", pa_sample_format_to_string(s->ss.format));
305 txt = avahi_string_list_add_pair(txt, "channel_map", pa_channel_map_snprint(cm, sizeof(cm), &s->cm));
306 txt = avahi_string_list_add_pair(txt, "subtype", subtype_text[s->subtype]);
307
308 if ((t = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION)))
309 txt = avahi_string_list_add_pair(txt, "description", t);
310 if ((t = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_ICON_NAME)))
311 txt = avahi_string_list_add_pair(txt, "icon-name", t);
312 if ((t = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_VENDOR_NAME)))
313 txt = avahi_string_list_add_pair(txt, "vendor-name", t);
314 if ((t = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_PRODUCT_NAME)))
315 txt = avahi_string_list_add_pair(txt, "product-name", t);
316 if ((t = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_CLASS)))
317 txt = avahi_string_list_add_pair(txt, "class", t);
318 if ((t = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_FORM_FACTOR)))
319 txt = avahi_string_list_add_pair(txt, "form-factor", t);
320
321 if (avahi_entry_group_add_service_strlst(
322 s->entry_group,
323 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
324 0,
325 s->service_name,
326 s->service_type,
327 NULL,
328 NULL,
329 compute_port(s->userdata),
330 txt) < 0) {
331
332 pa_log("avahi_entry_group_add_service_strlst(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
333 goto finish;
334 }
335
336 if (avahi_entry_group_add_service_subtype(
337 s->entry_group,
338 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
339 0,
340 s->service_name,
341 s->service_type,
342 NULL,
343 s->is_sink ? (s->subtype == SUBTYPE_HARDWARE ? SERVICE_SUBTYPE_SINK_HARDWARE : SERVICE_SUBTYPE_SINK_VIRTUAL) :
344 (s->subtype == SUBTYPE_HARDWARE ? SERVICE_SUBTYPE_SOURCE_HARDWARE : (s->subtype == SUBTYPE_VIRTUAL ? SERVICE_SUBTYPE_SOURCE_VIRTUAL : SERVICE_SUBTYPE_SOURCE_MONITOR))) < 0) {
345
346 pa_log("avahi_entry_group_add_service_subtype(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
347 goto finish;
348 }
349
350 if (!s->is_sink && s->subtype != SUBTYPE_MONITOR) {
351 if (avahi_entry_group_add_service_subtype(
352 s->entry_group,
353 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
354 0,
355 s->service_name,
356 SERVICE_TYPE_SOURCE,
357 NULL,
358 SERVICE_SUBTYPE_SOURCE_NON_MONITOR) < 0) {
359
360 pa_log("avahi_entry_group_add_service_subtype(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
361 goto finish;
362 }
363 }
364
365 if (avahi_entry_group_commit(s->entry_group) < 0) {
366 pa_log("avahi_entry_group_commit(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
367 goto finish;
368 }
369
370 r = 0;
371 pa_log_debug("Successfully created entry group for %s.", s->service_name);
372
373 finish:
374
375 /* Remove this service */
376 if (r < 0) {
377 pa_hashmap_remove(s->userdata->services, s->key);
378 service_free(s);
379 }
380
381 avahi_string_list_free(txt);
382 }
383
384 /* Runs in PA mainloop context */
385 static struct service *get_service(struct userdata *u, pa_object *device) {
386 struct service *s;
387 char *hn, *un;
388 const char *n;
389
390 pa_assert(u);
391 pa_object_assert_ref(device);
392
393 pa_threaded_mainloop_lock(u->mainloop);
394
395 if ((s = pa_hashmap_get(u->services, device)))
396 goto out;
397
398 s = pa_xnew(struct service, 1);
399 s->key = device;
400 s->userdata = u;
401 s->entry_group = NULL;
402
403 get_service_data(s, device);
404
405 if (!(n = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION)))
406 n = s->name;
407
408 hn = pa_get_host_name_malloc();
409 un = pa_get_user_name_malloc();
410
411 s->service_name = pa_truncate_utf8(pa_sprintf_malloc("%s@%s: %s", un, hn, n), AVAHI_LABEL_MAX-1);
412
413 pa_xfree(un);
414 pa_xfree(hn);
415
416 pa_hashmap_put(u->services, device, s);
417
418 out:
419 pa_threaded_mainloop_unlock(u->mainloop);
420
421 return s;
422 }
423
424 /* Run from Avahi mainloop context */
425 static void service_free(struct service *s) {
426 pa_assert(s);
427
428 if (s->entry_group) {
429 pa_log_debug("Removing entry group for %s.", s->service_name);
430 avahi_entry_group_free(s->entry_group);
431 }
432
433 pa_xfree(s->service_name);
434
435 pa_xfree(s->name);
436 pa_proplist_free(s->proplist);
437
438 pa_xfree(s);
439 }
440
441 /* Runs in PA mainloop context */
442 static pa_bool_t shall_ignore(pa_object *o) {
443 pa_object_assert_ref(o);
444
445 if (pa_sink_isinstance(o))
446 return !!(PA_SINK(o)->flags & PA_SINK_NETWORK);
447
448 if (pa_source_isinstance(o))
449 return PA_SOURCE(o)->monitor_of || (PA_SOURCE(o)->flags & PA_SOURCE_NETWORK);
450
451 pa_assert_not_reached();
452 }
453
454 /* Runs in PA mainloop context */
455 static pa_hook_result_t device_new_or_changed_cb(pa_core *c, pa_object *o, struct userdata *u) {
456 pa_assert(c);
457 pa_object_assert_ref(o);
458
459 if (!shall_ignore(o))
460 pa_mainloop_api_once(u->api, publish_service, get_service(u, o));
461
462 return PA_HOOK_OK;
463 }
464
465 /* Runs in PA mainloop context */
466 static pa_hook_result_t device_unlink_cb(pa_core *c, pa_object *o, struct userdata *u) {
467 struct service *s;
468
469 pa_assert(c);
470 pa_object_assert_ref(o);
471
472 pa_threaded_mainloop_lock(u->mainloop);
473
474 if ((s = pa_hashmap_remove(u->services, o)))
475 service_free(s);
476
477 pa_threaded_mainloop_unlock(u->mainloop);
478
479 return PA_HOOK_OK;
480 }
481
482 static int publish_main_service(struct userdata *u);
483
484 /* Runs in Avahi mainloop context */
485 static void main_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
486 struct userdata *u = userdata;
487 pa_assert(u);
488
489 switch (state) {
490
491 case AVAHI_ENTRY_GROUP_ESTABLISHED:
492 pa_log_info("Successfully established main service.");
493 break;
494
495 case AVAHI_ENTRY_GROUP_COLLISION: {
496 char *t;
497
498 t = avahi_alternative_service_name(u->service_name);
499 pa_log_info("Name collision: renaming main service %s to %s.", u->service_name, t);
500 pa_xfree(u->service_name);
501 u->service_name = t;
502
503 publish_main_service(u);
504 break;
505 }
506
507 case AVAHI_ENTRY_GROUP_FAILURE: {
508 pa_log("Failed to register main service: %s", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
509
510 avahi_entry_group_free(g);
511 u->main_entry_group = NULL;
512 break;
513 }
514
515 case AVAHI_ENTRY_GROUP_UNCOMMITED:
516 case AVAHI_ENTRY_GROUP_REGISTERING:
517 break;
518 }
519 }
520
521 /* Runs in Avahi mainloop context */
522 static int publish_main_service(struct userdata *u) {
523 AvahiStringList *txt = NULL;
524 int r = -1;
525
526 pa_assert(u);
527
528 if (!u->main_entry_group) {
529 if (!(u->main_entry_group = avahi_entry_group_new(u->client, main_entry_group_callback, u))) {
530 pa_log("avahi_entry_group_new() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
531 goto fail;
532 }
533 } else
534 avahi_entry_group_reset(u->main_entry_group);
535
536 txt = txt_record_server_data(u->core, txt);
537
538 if (avahi_entry_group_add_service_strlst(
539 u->main_entry_group,
540 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
541 0,
542 u->service_name,
543 SERVICE_TYPE_SERVER,
544 NULL,
545 NULL,
546 compute_port(u),
547 txt) < 0) {
548
549 pa_log("avahi_entry_group_add_service_strlst() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
550 goto fail;
551 }
552
553 if (avahi_entry_group_commit(u->main_entry_group) < 0) {
554 pa_log("avahi_entry_group_commit() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
555 goto fail;
556 }
557
558 r = 0;
559
560 fail:
561 avahi_string_list_free(txt);
562
563 return r;
564 }
565
566 /* Runs in PA mainloop context */
567 static int publish_all_services(struct userdata *u) {
568 pa_sink *sink;
569 pa_source *source;
570 int r = -1;
571 uint32_t idx;
572
573 pa_assert(u);
574
575 pa_log_debug("Publishing services in Zeroconf");
576
577 for (sink = PA_SINK(pa_idxset_first(u->core->sinks, &idx)); sink; sink = PA_SINK(pa_idxset_next(u->core->sinks, &idx)))
578 if (!shall_ignore(PA_OBJECT(sink)))
579 pa_mainloop_api_once(u->api, publish_service, get_service(u, PA_OBJECT(sink)));
580
581 for (source = PA_SOURCE(pa_idxset_first(u->core->sources, &idx)); source; source = PA_SOURCE(pa_idxset_next(u->core->sources, &idx)))
582 if (!shall_ignore(PA_OBJECT(source)))
583 pa_mainloop_api_once(u->api, publish_service, get_service(u, PA_OBJECT(source)));
584
585 if (publish_main_service(u) < 0)
586 goto fail;
587
588 r = 0;
589
590 fail:
591 return r;
592 }
593
594 /* Runs in Avahi mainloop context */
595 static void unpublish_all_services(struct userdata *u, pa_bool_t rem) {
596 void *state = NULL;
597 struct service *s;
598
599 pa_assert(u);
600
601 pa_log_debug("Unpublishing services in Zeroconf");
602
603 while ((s = pa_hashmap_iterate(u->services, &state, NULL))) {
604 if (s->entry_group) {
605 if (rem) {
606 pa_log_debug("Removing entry group for %s.", s->service_name);
607 avahi_entry_group_free(s->entry_group);
608 s->entry_group = NULL;
609 } else {
610 avahi_entry_group_reset(s->entry_group);
611 pa_log_debug("Resetting entry group for %s.", s->service_name);
612 }
613 }
614 }
615
616 if (u->main_entry_group) {
617 if (rem) {
618 pa_log_debug("Removing main entry group.");
619 avahi_entry_group_free(u->main_entry_group);
620 u->main_entry_group = NULL;
621 } else {
622 avahi_entry_group_reset(u->main_entry_group);
623 pa_log_debug("Resetting main entry group.");
624 }
625 }
626 }
627
628 /* Runs in PA mainloop context */
629 static int avahi_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
630 struct userdata *u = (struct userdata *) data;
631
632 switch (code) {
633 case AVAHI_MESSAGE_PUBLISH_ALL:
634 publish_all_services(u);
635 break;
636
637 case AVAHI_MESSAGE_SHUTDOWN_START:
638 pa_module_unload(u->core, u->module, true);
639 break;
640
641 case AVAHI_MESSAGE_SHUTDOWN_COMPLETE:
642 /* pa__done() is waiting for this */
643 break;
644
645 default:
646 pa_assert_not_reached();
647 }
648
649 return 0;
650 }
651
652 /* Runs in Avahi mainloop context */
653 static void client_callback(AvahiClient *c, AvahiClientState state, void *userdata) {
654 struct userdata *u = userdata;
655
656 pa_assert(c);
657 pa_assert(u);
658
659 u->client = c;
660
661 switch (state) {
662 case AVAHI_CLIENT_S_RUNNING:
663 /* Collect all sinks/sources, and publish them */
664 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->msg), AVAHI_MESSAGE_PUBLISH_ALL, u, 0, NULL, NULL);
665 break;
666
667 case AVAHI_CLIENT_S_COLLISION:
668 pa_log_debug("Host name collision");
669 unpublish_all_services(u, FALSE);
670 break;
671
672 case AVAHI_CLIENT_FAILURE:
673 if (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
674 int error;
675
676 pa_log_debug("Avahi daemon disconnected.");
677
678 unpublish_all_services(u, TRUE);
679 avahi_client_free(u->client);
680
681 if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
682 pa_log("avahi_client_new() failed: %s", avahi_strerror(error));
683 pa_module_unload_request(u->module, TRUE);
684 }
685 }
686
687 break;
688
689 default: ;
690 }
691 }
692
693 /* Runs in Avahi mainloop context */
694 static void create_client(pa_mainloop_api *api PA_GCC_UNUSED, void *userdata) {
695 struct userdata *u = (struct userdata *) userdata;
696 int error;
697
698 pa_thread_mq_install(&u->thread_mq);
699
700 if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
701 pa_log("avahi_client_new() failed: %s", avahi_strerror(error));
702 goto fail;
703 }
704
705 pa_log_debug("Started Avahi threaded mainloop");
706
707 return;
708
709 fail:
710 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->msg), AVAHI_MESSAGE_SHUTDOWN_START, u, 0, NULL, NULL);
711 }
712
713 int pa__init(pa_module*m) {
714
715 struct userdata *u;
716 pa_modargs *ma = NULL;
717 char *hn, *un;
718
719 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
720 pa_log("Failed to parse module arguments.");
721 goto fail;
722 }
723
724 m->userdata = u = pa_xnew(struct userdata, 1);
725 u->core = m->core;
726 u->module = m;
727 u->native = pa_native_protocol_get(u->core);
728
729 u->rtpoll = pa_rtpoll_new();
730 u->mainloop = pa_threaded_mainloop_new();
731 u->api = pa_threaded_mainloop_get_api(u->mainloop);
732
733 pa_thread_mq_init(&u->thread_mq, u->core->mainloop, u->rtpoll);
734 u->msg = pa_msgobject_new(avahi_msg);
735 u->msg->parent.process_msg = avahi_process_msg;
736
737 u->avahi_poll = pa_avahi_poll_new(u->api);
738
739 u->services = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
740
741 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);
742 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);
743 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);
744 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);
745 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);
746 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);
747
748 u->main_entry_group = NULL;
749
750 un = pa_get_user_name_malloc();
751 hn = pa_get_host_name_malloc();
752 u->service_name = pa_truncate_utf8(pa_sprintf_malloc("%s@%s", un, hn), AVAHI_LABEL_MAX-1);
753 pa_xfree(un);
754 pa_xfree(hn);
755
756 pa_threaded_mainloop_set_name(u->mainloop, "avahi-ml");
757 pa_threaded_mainloop_start(u->mainloop);
758 pa_mainloop_api_once(u->api, create_client, u);
759
760 pa_modargs_free(ma);
761
762 return 0;
763
764 fail:
765 pa__done(m);
766
767 if (ma)
768 pa_modargs_free(ma);
769
770 return -1;
771 }
772
773 /* Runs in Avahi mainloop context */
774 static void client_free(pa_mainloop_api *api PA_GCC_UNUSED, void *userdata) {
775 struct userdata *u = (struct userdata *) userdata;
776
777 pa_hashmap_free(u->services, (pa_free_cb_t) service_free);
778
779 if (u->main_entry_group)
780 avahi_entry_group_free(u->main_entry_group);
781
782 if (u->client)
783 avahi_client_free(u->client);
784
785 if (u->avahi_poll)
786 pa_avahi_poll_free(u->avahi_poll);
787
788 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->msg), AVAHI_MESSAGE_SHUTDOWN_COMPLETE, NULL, 0, NULL, NULL);
789 }
790
791 void pa__done(pa_module*m) {
792 struct userdata*u;
793 pa_assert(m);
794
795 if (!(u = m->userdata))
796 return;
797
798 pa_mainloop_api_once(u->api, client_free, u);
799 pa_asyncmsgq_wait_for(u->thread_mq.outq, AVAHI_MESSAGE_SHUTDOWN_COMPLETE);
800
801 pa_threaded_mainloop_stop(u->mainloop);
802 pa_threaded_mainloop_free(u->mainloop);
803
804 pa_thread_mq_done(&u->thread_mq);
805 pa_rtpoll_free(u->rtpoll);
806
807 if (u->sink_new_slot)
808 pa_hook_slot_free(u->sink_new_slot);
809 if (u->source_new_slot)
810 pa_hook_slot_free(u->source_new_slot);
811 if (u->sink_changed_slot)
812 pa_hook_slot_free(u->sink_changed_slot);
813 if (u->source_changed_slot)
814 pa_hook_slot_free(u->source_changed_slot);
815 if (u->sink_unlink_slot)
816 pa_hook_slot_free(u->sink_unlink_slot);
817 if (u->source_unlink_slot)
818 pa_hook_slot_free(u->source_unlink_slot);
819
820 if (u->native)
821 pa_native_protocol_unref(u->native);
822
823 pa_xfree(u->msg);
824 pa_xfree(u->service_name);
825 pa_xfree(u);
826 }