]> code.delx.au - pulseaudio/blob - src/modules/module-zeroconf-publish.c
make sure when can shutdown PA cleanly without segfault
[pulseaudio] / src / modules / module-zeroconf-publish.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32
33 #include <avahi-client/client.h>
34 #include <avahi-client/publish.h>
35 #include <avahi-common/alternative.h>
36 #include <avahi-common/error.h>
37 #include <avahi-common/domain.h>
38
39 #include <pulse/xmalloc.h>
40 #include <pulse/util.h>
41
42 #include <pulsecore/sink.h>
43 #include <pulsecore/source.h>
44 #include <pulsecore/native-common.h>
45 #include <pulsecore/core-util.h>
46 #include <pulsecore/log.h>
47 #include <pulsecore/core-subscribe.h>
48 #include <pulsecore/dynarray.h>
49 #include <pulsecore/modargs.h>
50 #include <pulsecore/avahi-wrap.h>
51 #include <pulsecore/endianmacros.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_USAGE("port=<IP port number>")
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
64 static const char* const valid_modargs[] = {
65 "port",
66 NULL
67 };
68
69 struct service {
70 struct userdata *userdata;
71 AvahiEntryGroup *entry_group;
72 char *service_name;
73 pa_object *device;
74 };
75
76 struct userdata {
77 pa_core *core;
78 AvahiPoll *avahi_poll;
79 AvahiClient *client;
80
81 pa_hashmap *services;
82 char *service_name;
83
84 AvahiEntryGroup *main_entry_group;
85
86 uint16_t port;
87
88 pa_hook_slot *sink_new_slot, *source_new_slot, *sink_unlink_slot, *source_unlink_slot, *sink_changed_slot, *source_changed_slot;
89 };
90
91 static void get_service_data(struct service *s, pa_sample_spec *ret_ss, pa_channel_map *ret_map, const char **ret_name, const char **ret_description) {
92 pa_assert(s);
93 pa_assert(ret_ss);
94 pa_assert(ret_description);
95
96 if (pa_sink_isinstance(s->device)) {
97 pa_sink *sink = PA_SINK(s->device);
98
99 *ret_ss = sink->sample_spec;
100 *ret_map = sink->channel_map;
101 *ret_name = sink->name;
102 *ret_description = sink->description;
103
104 } else if (pa_source_isinstance(s->device)) {
105 pa_source *source = PA_SOURCE(s->device);
106
107 *ret_ss = source->sample_spec;
108 *ret_map = source->channel_map;
109 *ret_name = source->name;
110 *ret_description = source->description;
111 } else
112 pa_assert_not_reached();
113 }
114
115 static AvahiStringList* txt_record_server_data(pa_core *c, AvahiStringList *l) {
116 char s[128];
117
118 pa_assert(c);
119
120 l = avahi_string_list_add_pair(l, "server-version", PACKAGE_NAME" "PACKAGE_VERSION);
121 l = avahi_string_list_add_pair(l, "user-name", pa_get_user_name(s, sizeof(s)));
122 l = avahi_string_list_add_pair(l, "fqdn", pa_get_fqdn(s, sizeof(s)));
123 l = avahi_string_list_add_printf(l, "cookie=0x%08x", c->cookie);
124
125 return l;
126 }
127
128 static int publish_service(struct service *s);
129
130 static void service_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
131 struct service *s = userdata;
132
133 pa_assert(s);
134
135 switch (state) {
136
137 case AVAHI_ENTRY_GROUP_ESTABLISHED:
138 pa_log_info("Successfully established service %s.", s->service_name);
139 break;
140
141 case AVAHI_ENTRY_GROUP_COLLISION: {
142 char *t;
143
144 t = avahi_alternative_service_name(s->service_name);
145 pa_log_info("Name collision, renaming %s to %s.", s->service_name, t);
146 pa_xfree(s->service_name);
147 s->service_name = t;
148
149 publish_service(s);
150 break;
151 }
152
153 case AVAHI_ENTRY_GROUP_FAILURE: {
154 pa_log("Failed to register service: %s", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
155
156 avahi_entry_group_free(g);
157 s->entry_group = NULL;
158
159 break;
160 }
161
162 case AVAHI_ENTRY_GROUP_UNCOMMITED:
163 case AVAHI_ENTRY_GROUP_REGISTERING:
164 ;
165 }
166 }
167
168 static void service_free(struct service *s);
169
170 static int publish_service(struct service *s) {
171 int r = -1;
172 AvahiStringList *txt = NULL;
173 const char *description = NULL, *name = NULL;
174 pa_sample_spec ss;
175 pa_channel_map map;
176 char cm[PA_CHANNEL_MAP_SNPRINT_MAX];
177
178 pa_assert(s);
179
180 if (!s->userdata->client || avahi_client_get_state(s->userdata->client) != AVAHI_CLIENT_S_RUNNING)
181 return 0;
182
183 if (!s->entry_group) {
184 if (!(s->entry_group = avahi_entry_group_new(s->userdata->client, service_entry_group_callback, s))) {
185 pa_log("avahi_entry_group_new(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
186 goto finish;
187 }
188 } else
189 avahi_entry_group_reset(s->entry_group);
190
191 txt = txt_record_server_data(s->userdata->core, txt);
192
193 get_service_data(s, &ss, &map, &name, &description);
194 txt = avahi_string_list_add_pair(txt, "device", name);
195 txt = avahi_string_list_add_printf(txt, "rate=%u", ss.rate);
196 txt = avahi_string_list_add_printf(txt, "channels=%u", ss.channels);
197 txt = avahi_string_list_add_pair(txt, "format", pa_sample_format_to_string(ss.format));
198 txt = avahi_string_list_add_pair(txt, "channel_map", pa_channel_map_snprint(cm, sizeof(cm), &map));
199
200 if (avahi_entry_group_add_service_strlst(
201 s->entry_group,
202 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
203 0,
204 s->service_name,
205 pa_sink_isinstance(s->device) ? SERVICE_TYPE_SINK : SERVICE_TYPE_SOURCE,
206 NULL,
207 NULL,
208 s->userdata->port,
209 txt) < 0) {
210
211 pa_log("avahi_entry_group_add_service_strlst(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
212 goto finish;
213 }
214
215 if (avahi_entry_group_commit(s->entry_group) < 0) {
216 pa_log("avahi_entry_group_commit(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
217 goto finish;
218 }
219
220 r = 0;
221 pa_log_debug("Successfully created entry group for %s.", s->service_name);
222
223 finish:
224
225 /* Remove this service */
226 if (r < 0)
227 service_free(s);
228
229 avahi_string_list_free(txt);
230
231 return r;
232 }
233
234 static struct service *get_service(struct userdata *u, pa_object *device) {
235 struct service *s;
236 char hn[64], un[64];
237 const char *n;
238
239 pa_assert(u);
240 pa_object_assert_ref(device);
241
242 if ((s = pa_hashmap_get(u->services, device)))
243 return s;
244
245 s = pa_xnew(struct service, 1);
246 s->userdata = u;
247 s->entry_group = NULL;
248 s->device = device;
249
250 if (pa_sink_isinstance(device)) {
251 if (!(n = PA_SINK(device)->description))
252 n = PA_SINK(device)->name;
253 } else {
254 if (!(n = PA_SOURCE(device)->description))
255 n = PA_SOURCE(device)->name;
256 }
257
258 s->service_name = pa_truncate_utf8(pa_sprintf_malloc("%s@%s: %s",
259 pa_get_user_name(un, sizeof(un)),
260 pa_get_host_name(hn, sizeof(hn)),
261 n),
262 AVAHI_LABEL_MAX-1);
263
264 pa_hashmap_put(u->services, s->device, s);
265
266 return s;
267 }
268
269 static void service_free(struct service *s) {
270 pa_assert(s);
271
272 pa_hashmap_remove(s->userdata->services, s->device);
273
274 if (s->entry_group) {
275 pa_log_debug("Removing entry group for %s.", s->service_name);
276 avahi_entry_group_free(s->entry_group);
277 }
278
279 pa_xfree(s->service_name);
280 pa_xfree(s);
281 }
282
283 static pa_hook_result_t device_new_or_changed_cb(pa_core *c, pa_object *o, struct userdata *u) {
284 pa_assert(c);
285 pa_object_assert_ref(o);
286
287 publish_service(get_service(u, o));
288
289 return PA_HOOK_OK;
290 }
291
292 static pa_hook_result_t device_unlink_cb(pa_core *c, pa_object *o, struct userdata *u) {
293 struct service *s;
294
295 pa_assert(c);
296 pa_object_assert_ref(o);
297
298 if ((s = pa_hashmap_get(u->services, o)))
299 service_free(s);
300
301 return PA_HOOK_OK;
302 }
303
304 static int publish_main_service(struct userdata *u);
305
306 static void main_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
307 struct userdata *u = userdata;
308 pa_assert(u);
309
310 switch (state) {
311
312 case AVAHI_ENTRY_GROUP_ESTABLISHED:
313 pa_log_info("Successfully established main service.");
314 break;
315
316 case AVAHI_ENTRY_GROUP_COLLISION: {
317 char *t;
318
319 t = avahi_alternative_service_name(u->service_name);
320 pa_log_info("Name collision: renaming main service %s to %s.", u->service_name, t);
321 pa_xfree(u->service_name);
322 u->service_name = t;
323
324 publish_main_service(u);
325 break;
326 }
327
328 case AVAHI_ENTRY_GROUP_FAILURE: {
329 pa_log("Failed to register main service: %s", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
330
331 avahi_entry_group_free(g);
332 u->main_entry_group = NULL;
333 break;
334 }
335
336 case AVAHI_ENTRY_GROUP_UNCOMMITED:
337 case AVAHI_ENTRY_GROUP_REGISTERING:
338 break;
339 }
340 }
341
342 static int publish_main_service(struct userdata *u) {
343 AvahiStringList *txt = NULL;
344 int r = -1;
345
346 pa_assert(u);
347
348 if (!u->main_entry_group) {
349 if (!(u->main_entry_group = avahi_entry_group_new(u->client, main_entry_group_callback, u))) {
350 pa_log("avahi_entry_group_new() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
351 goto fail;
352 }
353 } else
354 avahi_entry_group_reset(u->main_entry_group);
355
356 txt = txt_record_server_data(u->core, txt);
357
358 if (avahi_entry_group_add_service_strlst(
359 u->main_entry_group,
360 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
361 0,
362 u->service_name,
363 SERVICE_TYPE_SERVER,
364 NULL,
365 NULL,
366 u->port,
367 txt) < 0) {
368
369 pa_log("avahi_entry_group_add_service_strlst() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
370 goto fail;
371 }
372
373 if (avahi_entry_group_commit(u->main_entry_group) < 0) {
374 pa_log("avahi_entry_group_commit() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
375 goto fail;
376 }
377
378 r = 0;
379
380 fail:
381 avahi_string_list_free(txt);
382
383 return r;
384 }
385
386 static int publish_all_services(struct userdata *u) {
387 pa_sink *sink;
388 pa_source *source;
389 int r = -1;
390 uint32_t idx;
391
392 pa_assert(u);
393
394 pa_log_debug("Publishing services in Zeroconf");
395
396 for (sink = PA_SINK(pa_idxset_first(u->core->sinks, &idx)); sink; sink = PA_SINK(pa_idxset_next(u->core->sinks, &idx)))
397 publish_service(get_service(u, PA_OBJECT(sink)));
398
399 for (source = PA_SOURCE(pa_idxset_first(u->core->sources, &idx)); source; source = PA_SOURCE(pa_idxset_next(u->core->sources, &idx)))
400 publish_service(get_service(u, PA_OBJECT(source)));
401
402 if (publish_main_service(u) < 0)
403 goto fail;
404
405 r = 0;
406
407 fail:
408 return r;
409 }
410
411 static void unpublish_all_services(struct userdata *u, pa_bool_t rem) {
412 void *state = NULL;
413 struct service *s;
414
415 pa_assert(u);
416
417 pa_log_debug("Unpublishing services in Zeroconf");
418
419 while ((s = pa_hashmap_iterate(u->services, &state, NULL))) {
420 if (s->entry_group) {
421 if (rem) {
422 pa_log_debug("Removing entry group for %s.", s->service_name);
423 avahi_entry_group_free(s->entry_group);
424 s->entry_group = NULL;
425 } else {
426 avahi_entry_group_reset(s->entry_group);
427 pa_log_debug("Resetting entry group for %s.", s->service_name);
428 }
429 }
430 }
431
432 if (u->main_entry_group) {
433 if (rem) {
434 pa_log_debug("Removing main entry group.");
435 avahi_entry_group_free(u->main_entry_group);
436 u->main_entry_group = NULL;
437 } else {
438 avahi_entry_group_reset(u->main_entry_group);
439 pa_log_debug("Resetting main entry group.");
440 }
441 }
442 }
443
444 static void client_callback(AvahiClient *c, AvahiClientState state, void *userdata) {
445 struct userdata *u = userdata;
446
447 pa_assert(c);
448 pa_assert(u);
449
450 u->client = c;
451
452 switch (state) {
453 case AVAHI_CLIENT_S_RUNNING:
454 publish_all_services(u);
455 break;
456
457 case AVAHI_CLIENT_S_COLLISION:
458 pa_log_debug("Host name collision");
459 unpublish_all_services(u, FALSE);
460 break;
461
462 case AVAHI_CLIENT_FAILURE:
463 if (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
464 int error;
465
466 pa_log_debug("Avahi daemon disconnected.");
467
468 unpublish_all_services(u, TRUE);
469 avahi_client_free(u->client);
470
471 if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error)))
472 pa_log("pa_avahi_client_new() failed: %s", avahi_strerror(error));
473 }
474
475 break;
476
477 default: ;
478 }
479 }
480
481 int pa__init(pa_module*m) {
482
483 struct userdata *u;
484 uint32_t port = PA_NATIVE_DEFAULT_PORT;
485 pa_modargs *ma = NULL;
486 char hn[256], un[256];
487 int error;
488
489 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
490 pa_log("failed to parse module arguments.");
491 goto fail;
492 }
493
494 if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port <= 0 || port > 0xFFFF) {
495 pa_log("invalid port specified.");
496 goto fail;
497 }
498
499 m->userdata = u = pa_xnew(struct userdata, 1);
500 u->core = m->core;
501 u->port = (uint16_t) port;
502
503 u->avahi_poll = pa_avahi_poll_new(m->core->mainloop);
504
505 u->services = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
506
507 u->sink_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_NEW_POST], (pa_hook_cb_t) device_new_or_changed_cb, u);
508 u->sink_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_DESCRIPTION_CHANGED], (pa_hook_cb_t) device_new_or_changed_cb, u);
509 u->sink_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], (pa_hook_cb_t) device_unlink_cb, u);
510 u->source_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_NEW_POST], (pa_hook_cb_t) device_new_or_changed_cb, u);
511 u->source_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_DESCRIPTION_CHANGED], (pa_hook_cb_t) device_new_or_changed_cb, u);
512 u->source_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], (pa_hook_cb_t) device_unlink_cb, u);
513
514 u->main_entry_group = NULL;
515
516 u->service_name = pa_truncate_utf8(pa_sprintf_malloc("%s@%s", pa_get_user_name(un, sizeof(un)), pa_get_host_name(hn, sizeof(hn))), AVAHI_LABEL_MAX);
517
518 if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
519 pa_log("pa_avahi_client_new() failed: %s", avahi_strerror(error));
520 goto fail;
521 }
522
523 pa_modargs_free(ma);
524
525 return 0;
526
527 fail:
528 pa__done(m);
529
530 if (ma)
531 pa_modargs_free(ma);
532
533 return -1;
534 }
535
536 void pa__done(pa_module*m) {
537 struct userdata*u;
538 pa_assert(m);
539
540 if (!(u = m->userdata))
541 return;
542
543 if (u->services) {
544 struct service *s;
545
546 while ((s = pa_hashmap_get_first(u->services)))
547 service_free(s);
548
549 pa_hashmap_free(u->services, NULL, NULL);
550 }
551
552 if (u->sink_new_slot)
553 pa_hook_slot_free(u->sink_new_slot);
554 if (u->source_new_slot)
555 pa_hook_slot_free(u->source_new_slot);
556 if (u->sink_changed_slot)
557 pa_hook_slot_free(u->sink_changed_slot);
558 if (u->source_changed_slot)
559 pa_hook_slot_free(u->source_changed_slot);
560 if (u->sink_unlink_slot)
561 pa_hook_slot_free(u->sink_unlink_slot);
562 if (u->source_unlink_slot)
563 pa_hook_slot_free(u->source_unlink_slot);
564
565 if (u->main_entry_group)
566 avahi_entry_group_free(u->main_entry_group);
567
568 if (u->client)
569 avahi_client_free(u->client);
570
571 if (u->avahi_poll)
572 pa_avahi_poll_free(u->avahi_poll);
573
574 pa_xfree(u->service_name);
575 pa_xfree(u);
576 }