]> code.delx.au - pulseaudio/blob - src/modules/module-zeroconf-publish.c
be8806e32a72248e0f40e3c14e26a261b8dd20f5
[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 bool shutting_down; /* Used in the main thread. */
146 bool client_freed; /* Used in the Avahi thread. */
147 };
148
149 /* Runs in PA mainloop context */
150 static void get_service_data(struct service *s, pa_object *device) {
151 pa_assert(s);
152
153 if (pa_sink_isinstance(device)) {
154 pa_sink *sink = PA_SINK(device);
155
156 s->is_sink = true;
157 s->service_type = SERVICE_TYPE_SINK;
158 s->ss = sink->sample_spec;
159 s->cm = sink->channel_map;
160 s->name = pa_xstrdup(sink->name);
161 s->proplist = pa_proplist_copy(sink->proplist);
162 s->subtype = sink->flags & PA_SINK_HARDWARE ? SUBTYPE_HARDWARE : SUBTYPE_VIRTUAL;
163
164 } else if (pa_source_isinstance(device)) {
165 pa_source *source = PA_SOURCE(device);
166
167 s->is_sink = false;
168 s->service_type = SERVICE_TYPE_SOURCE;
169 s->ss = source->sample_spec;
170 s->cm = source->channel_map;
171 s->name = pa_xstrdup(source->name);
172 s->proplist = pa_proplist_copy(source->proplist);
173 s->subtype = source->monitor_of ? SUBTYPE_MONITOR : (source->flags & PA_SOURCE_HARDWARE ? SUBTYPE_HARDWARE : SUBTYPE_VIRTUAL);
174
175 } else
176 pa_assert_not_reached();
177 }
178
179 /* Can be used in either PA or Avahi mainloop context since the bits of u->core
180 * that we access don't change after startup. */
181 static AvahiStringList* txt_record_server_data(pa_core *c, AvahiStringList *l) {
182 char s[128];
183 char *t;
184
185 pa_assert(c);
186
187 l = avahi_string_list_add_pair(l, "server-version", PACKAGE_NAME" "PACKAGE_VERSION);
188
189 t = pa_get_user_name_malloc();
190 l = avahi_string_list_add_pair(l, "user-name", t);
191 pa_xfree(t);
192
193 t = pa_machine_id();
194 l = avahi_string_list_add_pair(l, "machine-id", t);
195 pa_xfree(t);
196
197 t = pa_uname_string();
198 l = avahi_string_list_add_pair(l, "uname", t);
199 pa_xfree(t);
200
201 l = avahi_string_list_add_pair(l, "fqdn", pa_get_fqdn(s, sizeof(s)));
202 l = avahi_string_list_add_printf(l, "cookie=0x%08x", c->cookie);
203
204 return l;
205 }
206
207 static void publish_service(pa_mainloop_api *api, void *service);
208
209 /* Runs in Avahi mainloop context */
210 static void service_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
211 struct service *s = userdata;
212
213 pa_assert(s);
214
215 switch (state) {
216
217 case AVAHI_ENTRY_GROUP_ESTABLISHED:
218 pa_log_info("Successfully established service %s.", s->service_name);
219 break;
220
221 case AVAHI_ENTRY_GROUP_COLLISION: {
222 char *t;
223
224 t = avahi_alternative_service_name(s->service_name);
225 pa_log_info("Name collision, renaming %s to %s.", s->service_name, t);
226 pa_xfree(s->service_name);
227 s->service_name = t;
228
229 publish_service(NULL, s);
230 break;
231 }
232
233 case AVAHI_ENTRY_GROUP_FAILURE: {
234 pa_log("Failed to register service: %s", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
235
236 avahi_entry_group_free(g);
237 s->entry_group = NULL;
238
239 break;
240 }
241
242 case AVAHI_ENTRY_GROUP_UNCOMMITED:
243 case AVAHI_ENTRY_GROUP_REGISTERING:
244 ;
245 }
246 }
247
248 static void service_free(struct service *s);
249
250 /* Can run in either context */
251 static uint16_t compute_port(struct userdata *u) {
252 pa_strlist *i;
253
254 pa_assert(u);
255
256 for (i = pa_native_protocol_servers(u->native); i; i = pa_strlist_next(i)) {
257 pa_parsed_address a;
258
259 if (pa_parse_address(pa_strlist_data(i), &a) >= 0 &&
260 (a.type == PA_PARSED_ADDRESS_TCP4 ||
261 a.type == PA_PARSED_ADDRESS_TCP6 ||
262 a.type == PA_PARSED_ADDRESS_TCP_AUTO) &&
263 a.port > 0) {
264
265 pa_xfree(a.path_or_host);
266 return a.port;
267 }
268
269 pa_xfree(a.path_or_host);
270 }
271
272 return PA_NATIVE_DEFAULT_PORT;
273 }
274
275 /* Runs in Avahi mainloop context */
276 static void publish_service(pa_mainloop_api *api PA_GCC_UNUSED, void *service) {
277 struct service *s = (struct service *) service;
278 int r = -1;
279 AvahiStringList *txt = NULL;
280 char cm[PA_CHANNEL_MAP_SNPRINT_MAX];
281 const char *t;
282
283 const char * const subtype_text[] = {
284 [SUBTYPE_HARDWARE] = "hardware",
285 [SUBTYPE_VIRTUAL] = "virtual",
286 [SUBTYPE_MONITOR] = "monitor"
287 };
288
289 pa_assert(s);
290
291 if (!s->userdata->client || avahi_client_get_state(s->userdata->client) != AVAHI_CLIENT_S_RUNNING)
292 return;
293
294 if (!s->entry_group) {
295 if (!(s->entry_group = avahi_entry_group_new(s->userdata->client, service_entry_group_callback, s))) {
296 pa_log("avahi_entry_group_new(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
297 goto finish;
298 }
299 } else
300 avahi_entry_group_reset(s->entry_group);
301
302 txt = txt_record_server_data(s->userdata->core, txt);
303
304 txt = avahi_string_list_add_pair(txt, "device", s->name);
305 txt = avahi_string_list_add_printf(txt, "rate=%u", s->ss.rate);
306 txt = avahi_string_list_add_printf(txt, "channels=%u", s->ss.channels);
307 txt = avahi_string_list_add_pair(txt, "format", pa_sample_format_to_string(s->ss.format));
308 txt = avahi_string_list_add_pair(txt, "channel_map", pa_channel_map_snprint(cm, sizeof(cm), &s->cm));
309 txt = avahi_string_list_add_pair(txt, "subtype", subtype_text[s->subtype]);
310
311 if ((t = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION)))
312 txt = avahi_string_list_add_pair(txt, "description", t);
313 if ((t = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_ICON_NAME)))
314 txt = avahi_string_list_add_pair(txt, "icon-name", t);
315 if ((t = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_VENDOR_NAME)))
316 txt = avahi_string_list_add_pair(txt, "vendor-name", t);
317 if ((t = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_PRODUCT_NAME)))
318 txt = avahi_string_list_add_pair(txt, "product-name", t);
319 if ((t = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_CLASS)))
320 txt = avahi_string_list_add_pair(txt, "class", t);
321 if ((t = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_FORM_FACTOR)))
322 txt = avahi_string_list_add_pair(txt, "form-factor", t);
323
324 if (avahi_entry_group_add_service_strlst(
325 s->entry_group,
326 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
327 0,
328 s->service_name,
329 s->service_type,
330 NULL,
331 NULL,
332 compute_port(s->userdata),
333 txt) < 0) {
334
335 pa_log("avahi_entry_group_add_service_strlst(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
336 goto finish;
337 }
338
339 if (avahi_entry_group_add_service_subtype(
340 s->entry_group,
341 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
342 0,
343 s->service_name,
344 s->service_type,
345 NULL,
346 s->is_sink ? (s->subtype == SUBTYPE_HARDWARE ? SERVICE_SUBTYPE_SINK_HARDWARE : SERVICE_SUBTYPE_SINK_VIRTUAL) :
347 (s->subtype == SUBTYPE_HARDWARE ? SERVICE_SUBTYPE_SOURCE_HARDWARE : (s->subtype == SUBTYPE_VIRTUAL ? SERVICE_SUBTYPE_SOURCE_VIRTUAL : SERVICE_SUBTYPE_SOURCE_MONITOR))) < 0) {
348
349 pa_log("avahi_entry_group_add_service_subtype(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
350 goto finish;
351 }
352
353 if (!s->is_sink && s->subtype != SUBTYPE_MONITOR) {
354 if (avahi_entry_group_add_service_subtype(
355 s->entry_group,
356 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
357 0,
358 s->service_name,
359 SERVICE_TYPE_SOURCE,
360 NULL,
361 SERVICE_SUBTYPE_SOURCE_NON_MONITOR) < 0) {
362
363 pa_log("avahi_entry_group_add_service_subtype(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
364 goto finish;
365 }
366 }
367
368 if (avahi_entry_group_commit(s->entry_group) < 0) {
369 pa_log("avahi_entry_group_commit(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
370 goto finish;
371 }
372
373 r = 0;
374 pa_log_debug("Successfully created entry group for %s.", s->service_name);
375
376 finish:
377
378 /* Remove this service */
379 if (r < 0) {
380 pa_hashmap_remove(s->userdata->services, s->key);
381 service_free(s);
382 }
383
384 avahi_string_list_free(txt);
385 }
386
387 /* Runs in PA mainloop context */
388 static struct service *get_service(struct userdata *u, pa_object *device) {
389 struct service *s;
390 char *hn, *un;
391 const char *n;
392
393 pa_assert(u);
394 pa_object_assert_ref(device);
395
396 pa_threaded_mainloop_lock(u->mainloop);
397
398 if ((s = pa_hashmap_get(u->services, device)))
399 goto out;
400
401 s = pa_xnew(struct service, 1);
402 s->key = device;
403 s->userdata = u;
404 s->entry_group = NULL;
405
406 get_service_data(s, device);
407
408 if (!(n = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION)))
409 n = s->name;
410
411 hn = pa_get_host_name_malloc();
412 un = pa_get_user_name_malloc();
413
414 s->service_name = pa_truncate_utf8(pa_sprintf_malloc("%s@%s: %s", un, hn, n), AVAHI_LABEL_MAX-1);
415
416 pa_xfree(un);
417 pa_xfree(hn);
418
419 pa_hashmap_put(u->services, device, s);
420
421 out:
422 pa_threaded_mainloop_unlock(u->mainloop);
423
424 return s;
425 }
426
427 /* Run from Avahi mainloop context */
428 static void service_free(struct service *s) {
429 pa_assert(s);
430
431 if (s->entry_group) {
432 pa_log_debug("Removing entry group for %s.", s->service_name);
433 avahi_entry_group_free(s->entry_group);
434 }
435
436 pa_xfree(s->service_name);
437
438 pa_xfree(s->name);
439 pa_proplist_free(s->proplist);
440
441 pa_xfree(s);
442 }
443
444 /* Runs in PA mainloop context */
445 static bool shall_ignore(pa_object *o) {
446 pa_object_assert_ref(o);
447
448 if (pa_sink_isinstance(o))
449 return !!(PA_SINK(o)->flags & PA_SINK_NETWORK);
450
451 if (pa_source_isinstance(o))
452 return PA_SOURCE(o)->monitor_of || (PA_SOURCE(o)->flags & PA_SOURCE_NETWORK);
453
454 pa_assert_not_reached();
455 }
456
457 /* Runs in PA mainloop context */
458 static pa_hook_result_t device_new_or_changed_cb(pa_core *c, pa_object *o, struct userdata *u) {
459 pa_assert(c);
460 pa_object_assert_ref(o);
461
462 if (!shall_ignore(o)) {
463 pa_threaded_mainloop_lock(u->mainloop);
464 pa_mainloop_api_once(u->api, publish_service, get_service(u, o));
465 pa_threaded_mainloop_unlock(u->mainloop);
466 }
467
468 return PA_HOOK_OK;
469 }
470
471 /* Runs in PA mainloop context */
472 static pa_hook_result_t device_unlink_cb(pa_core *c, pa_object *o, struct userdata *u) {
473 struct service *s;
474
475 pa_assert(c);
476 pa_object_assert_ref(o);
477
478 pa_threaded_mainloop_lock(u->mainloop);
479
480 if ((s = pa_hashmap_remove(u->services, o)))
481 service_free(s);
482
483 pa_threaded_mainloop_unlock(u->mainloop);
484
485 return PA_HOOK_OK;
486 }
487
488 static int publish_main_service(struct userdata *u);
489
490 /* Runs in Avahi mainloop context */
491 static void main_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
492 struct userdata *u = userdata;
493 pa_assert(u);
494
495 switch (state) {
496
497 case AVAHI_ENTRY_GROUP_ESTABLISHED:
498 pa_log_info("Successfully established main service.");
499 break;
500
501 case AVAHI_ENTRY_GROUP_COLLISION: {
502 char *t;
503
504 t = avahi_alternative_service_name(u->service_name);
505 pa_log_info("Name collision: renaming main service %s to %s.", u->service_name, t);
506 pa_xfree(u->service_name);
507 u->service_name = t;
508
509 publish_main_service(u);
510 break;
511 }
512
513 case AVAHI_ENTRY_GROUP_FAILURE: {
514 pa_log("Failed to register main service: %s", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
515
516 avahi_entry_group_free(g);
517 u->main_entry_group = NULL;
518 break;
519 }
520
521 case AVAHI_ENTRY_GROUP_UNCOMMITED:
522 case AVAHI_ENTRY_GROUP_REGISTERING:
523 break;
524 }
525 }
526
527 /* Runs in Avahi mainloop context */
528 static int publish_main_service(struct userdata *u) {
529 AvahiStringList *txt = NULL;
530 int r = -1;
531
532 pa_assert(u);
533
534 if (!u->main_entry_group) {
535 if (!(u->main_entry_group = avahi_entry_group_new(u->client, main_entry_group_callback, u))) {
536 pa_log("avahi_entry_group_new() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
537 goto fail;
538 }
539 } else
540 avahi_entry_group_reset(u->main_entry_group);
541
542 txt = txt_record_server_data(u->core, txt);
543
544 if (avahi_entry_group_add_service_strlst(
545 u->main_entry_group,
546 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
547 0,
548 u->service_name,
549 SERVICE_TYPE_SERVER,
550 NULL,
551 NULL,
552 compute_port(u),
553 txt) < 0) {
554
555 pa_log("avahi_entry_group_add_service_strlst() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
556 goto fail;
557 }
558
559 if (avahi_entry_group_commit(u->main_entry_group) < 0) {
560 pa_log("avahi_entry_group_commit() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
561 goto fail;
562 }
563
564 r = 0;
565
566 fail:
567 avahi_string_list_free(txt);
568
569 return r;
570 }
571
572 /* Runs in PA mainloop context */
573 static int publish_all_services(struct userdata *u) {
574 pa_sink *sink;
575 pa_source *source;
576 int r = -1;
577 uint32_t idx;
578
579 pa_assert(u);
580
581 pa_log_debug("Publishing services in Zeroconf");
582
583 for (sink = PA_SINK(pa_idxset_first(u->core->sinks, &idx)); sink; sink = PA_SINK(pa_idxset_next(u->core->sinks, &idx)))
584 if (!shall_ignore(PA_OBJECT(sink))) {
585 pa_threaded_mainloop_lock(u->mainloop);
586 pa_mainloop_api_once(u->api, publish_service, get_service(u, PA_OBJECT(sink)));
587 pa_threaded_mainloop_unlock(u->mainloop);
588 }
589
590 for (source = PA_SOURCE(pa_idxset_first(u->core->sources, &idx)); source; source = PA_SOURCE(pa_idxset_next(u->core->sources, &idx)))
591 if (!shall_ignore(PA_OBJECT(source))) {
592 pa_threaded_mainloop_lock(u->mainloop);
593 pa_mainloop_api_once(u->api, publish_service, get_service(u, PA_OBJECT(source)));
594 pa_threaded_mainloop_unlock(u->mainloop);
595 }
596
597 if (publish_main_service(u) < 0)
598 goto fail;
599
600 r = 0;
601
602 fail:
603 return r;
604 }
605
606 /* Runs in Avahi mainloop context */
607 static void unpublish_all_services(struct userdata *u, bool rem) {
608 void *state = NULL;
609 struct service *s;
610
611 pa_assert(u);
612
613 pa_log_debug("Unpublishing services in Zeroconf");
614
615 while ((s = pa_hashmap_iterate(u->services, &state, NULL))) {
616 if (s->entry_group) {
617 if (rem) {
618 pa_log_debug("Removing entry group for %s.", s->service_name);
619 avahi_entry_group_free(s->entry_group);
620 s->entry_group = NULL;
621 } else {
622 avahi_entry_group_reset(s->entry_group);
623 pa_log_debug("Resetting entry group for %s.", s->service_name);
624 }
625 }
626 }
627
628 if (u->main_entry_group) {
629 if (rem) {
630 pa_log_debug("Removing main entry group.");
631 avahi_entry_group_free(u->main_entry_group);
632 u->main_entry_group = NULL;
633 } else {
634 avahi_entry_group_reset(u->main_entry_group);
635 pa_log_debug("Resetting main entry group.");
636 }
637 }
638 }
639
640 /* Runs in PA mainloop context */
641 static int avahi_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
642 struct userdata *u = (struct userdata *) data;
643
644 pa_assert(u);
645
646 if (u->shutting_down)
647 return 0;
648
649 switch (code) {
650 case AVAHI_MESSAGE_PUBLISH_ALL:
651 publish_all_services(u);
652 break;
653
654 case AVAHI_MESSAGE_SHUTDOWN_START:
655 pa_module_unload(u->core, u->module, true);
656 break;
657
658 default:
659 pa_assert_not_reached();
660 }
661
662 return 0;
663 }
664
665 /* Runs in Avahi mainloop context */
666 static void client_callback(AvahiClient *c, AvahiClientState state, void *userdata) {
667 struct userdata *u = userdata;
668
669 pa_assert(c);
670 pa_assert(u);
671
672 u->client = c;
673
674 switch (state) {
675 case AVAHI_CLIENT_S_RUNNING:
676 /* Collect all sinks/sources, and publish them */
677 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->msg), AVAHI_MESSAGE_PUBLISH_ALL, u, 0, NULL, NULL);
678 break;
679
680 case AVAHI_CLIENT_S_COLLISION:
681 pa_log_debug("Host name collision");
682 unpublish_all_services(u, false);
683 break;
684
685 case AVAHI_CLIENT_FAILURE:
686 if (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
687 int error;
688
689 pa_log_debug("Avahi daemon disconnected.");
690
691 unpublish_all_services(u, true);
692 avahi_client_free(u->client);
693
694 if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
695 pa_log("avahi_client_new() failed: %s", avahi_strerror(error));
696 pa_module_unload_request(u->module, true);
697 }
698 }
699
700 break;
701
702 default: ;
703 }
704 }
705
706 /* Runs in Avahi mainloop context */
707 static void create_client(pa_mainloop_api *api PA_GCC_UNUSED, void *userdata) {
708 struct userdata *u = (struct userdata *) userdata;
709 int error;
710
711 /* create_client() and client_free() are called via defer events. If the
712 * two defer events are created very quickly one after another, we can't
713 * assume that the defer event that runs create_client() will be dispatched
714 * before the defer event that runs client_free() (at the time of writing,
715 * pa_mainloop actually always dispatches queued defer events in reverse
716 * creation order). For that reason we must be prepared for the case where
717 * client_free() has already been called. */
718 if (u->client_freed)
719 return;
720
721 pa_thread_mq_install(&u->thread_mq);
722
723 if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
724 pa_log("avahi_client_new() failed: %s", avahi_strerror(error));
725 goto fail;
726 }
727
728 pa_log_debug("Started Avahi threaded mainloop");
729
730 return;
731
732 fail:
733 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->msg), AVAHI_MESSAGE_SHUTDOWN_START, u, 0, NULL, NULL);
734 }
735
736 int pa__init(pa_module*m) {
737
738 struct userdata *u;
739 pa_modargs *ma = NULL;
740 char *hn, *un;
741
742 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
743 pa_log("Failed to parse module arguments.");
744 goto fail;
745 }
746
747 m->userdata = u = pa_xnew0(struct userdata, 1);
748 u->core = m->core;
749 u->module = m;
750 u->native = pa_native_protocol_get(u->core);
751
752 u->rtpoll = pa_rtpoll_new();
753 u->mainloop = pa_threaded_mainloop_new();
754 u->api = pa_threaded_mainloop_get_api(u->mainloop);
755
756 pa_thread_mq_init(&u->thread_mq, u->core->mainloop, u->rtpoll);
757 u->msg = pa_msgobject_new(avahi_msg);
758 u->msg->parent.process_msg = avahi_process_msg;
759
760 u->avahi_poll = pa_avahi_poll_new(u->api);
761
762 u->services = pa_hashmap_new_full(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func, NULL, (pa_free_cb_t) service_free);
763
764 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);
765 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);
766 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);
767 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);
768 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);
769 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);
770
771 un = pa_get_user_name_malloc();
772 hn = pa_get_host_name_malloc();
773 u->service_name = pa_truncate_utf8(pa_sprintf_malloc("%s@%s", un, hn), AVAHI_LABEL_MAX-1);
774 pa_xfree(un);
775 pa_xfree(hn);
776
777 pa_threaded_mainloop_set_name(u->mainloop, "avahi-ml");
778 pa_threaded_mainloop_start(u->mainloop);
779
780 pa_threaded_mainloop_lock(u->mainloop);
781 pa_mainloop_api_once(u->api, create_client, u);
782 pa_threaded_mainloop_unlock(u->mainloop);
783
784 pa_modargs_free(ma);
785
786 return 0;
787
788 fail:
789 pa__done(m);
790
791 if (ma)
792 pa_modargs_free(ma);
793
794 return -1;
795 }
796
797 /* Runs in Avahi mainloop context */
798 static void client_free(pa_mainloop_api *api PA_GCC_UNUSED, void *userdata) {
799 struct userdata *u = (struct userdata *) userdata;
800
801 pa_hashmap_free(u->services);
802
803 if (u->main_entry_group)
804 avahi_entry_group_free(u->main_entry_group);
805
806 if (u->client)
807 avahi_client_free(u->client);
808
809 if (u->avahi_poll)
810 pa_avahi_poll_free(u->avahi_poll);
811
812 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->msg), AVAHI_MESSAGE_SHUTDOWN_COMPLETE, u, 0, NULL, NULL);
813
814 u->client_freed = true;
815 }
816
817 void pa__done(pa_module*m) {
818 struct userdata*u;
819 pa_assert(m);
820
821 if (!(u = m->userdata))
822 return;
823
824 u->shutting_down = true;
825
826 pa_threaded_mainloop_lock(u->mainloop);
827 pa_mainloop_api_once(u->api, client_free, u);
828 pa_threaded_mainloop_unlock(u->mainloop);
829 pa_asyncmsgq_wait_for(u->thread_mq.outq, AVAHI_MESSAGE_SHUTDOWN_COMPLETE);
830
831 pa_threaded_mainloop_stop(u->mainloop);
832 pa_threaded_mainloop_free(u->mainloop);
833
834 pa_thread_mq_done(&u->thread_mq);
835 pa_rtpoll_free(u->rtpoll);
836
837 if (u->sink_new_slot)
838 pa_hook_slot_free(u->sink_new_slot);
839 if (u->source_new_slot)
840 pa_hook_slot_free(u->source_new_slot);
841 if (u->sink_changed_slot)
842 pa_hook_slot_free(u->sink_changed_slot);
843 if (u->source_changed_slot)
844 pa_hook_slot_free(u->source_changed_slot);
845 if (u->sink_unlink_slot)
846 pa_hook_slot_free(u->sink_unlink_slot);
847 if (u->source_unlink_slot)
848 pa_hook_slot_free(u->source_unlink_slot);
849
850 if (u->native)
851 pa_native_protocol_unref(u->native);
852
853 pa_xfree(u->msg);
854 pa_xfree(u->service_name);
855 pa_xfree(u);
856 }