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