]> code.delx.au - pulseaudio/blob - src/modules/module-zeroconf-publish.c
don't set the sink/source descriptions manually, use the new functions pa_{sink,sourc...
[pulseaudio] / src / modules / module-zeroconf-publish.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
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 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 <assert.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 #include <avahi-client/client.h>
33 #include <avahi-client/publish.h>
34 #include <avahi-common/alternative.h>
35 #include <avahi-common/error.h>
36
37 #include <pulse/xmalloc.h>
38 #include <pulse/util.h>
39
40 #include <pulsecore/autoload.h>
41 #include <pulsecore/sink.h>
42 #include <pulsecore/source.h>
43 #include <pulsecore/native-common.h>
44 #include <pulsecore/core-util.h>
45 #include <pulsecore/log.h>
46 #include <pulsecore/core-subscribe.h>
47 #include <pulsecore/dynarray.h>
48 #include <pulsecore/modargs.h>
49 #include <pulsecore/avahi-wrap.h>
50 #include <pulsecore/endianmacros.h>
51
52 #include "module-zeroconf-publish-symdef.h"
53
54 PA_MODULE_AUTHOR("Lennart Poettering")
55 PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Publisher")
56 PA_MODULE_VERSION(PACKAGE_VERSION)
57 PA_MODULE_USAGE("port=<IP port number>")
58
59 #define SERVICE_TYPE_SINK "_pulse-sink._tcp"
60 #define SERVICE_TYPE_SOURCE "_pulse-source._tcp"
61 #define SERVICE_TYPE_SERVER "_pulse-server._tcp"
62
63 static const char* const valid_modargs[] = {
64 "port",
65 NULL
66 };
67
68 struct service {
69 struct userdata *userdata;
70 AvahiEntryGroup *entry_group;
71 char *service_name;
72 char *name;
73 enum { UNPUBLISHED, PUBLISHED_REAL, PUBLISHED_AUTOLOAD } published ;
74
75 struct {
76 int valid;
77 pa_namereg_type_t type;
78 uint32_t index;
79 } loaded;
80
81 struct {
82 int valid;
83 pa_namereg_type_t type;
84 uint32_t index;
85 } autoload;
86 };
87
88 struct userdata {
89 pa_core *core;
90 AvahiPoll *avahi_poll;
91 AvahiClient *client;
92 pa_hashmap *services;
93 pa_dynarray *sink_dynarray, *source_dynarray, *autoload_dynarray;
94 pa_subscription *subscription;
95 char *service_name;
96
97 AvahiEntryGroup *main_entry_group;
98
99 uint16_t port;
100 };
101
102 static void get_service_data(struct userdata *u, struct service *s, pa_sample_spec *ret_ss, char **ret_description) {
103 assert(u && s && s->loaded.valid && ret_ss && ret_description);
104
105 if (s->loaded.type == PA_NAMEREG_SINK) {
106 pa_sink *sink = pa_idxset_get_by_index(u->core->sinks, s->loaded.index);
107 assert(sink);
108 *ret_ss = sink->sample_spec;
109 *ret_description = sink->description;
110 } else if (s->loaded.type == PA_NAMEREG_SOURCE) {
111 pa_source *source = pa_idxset_get_by_index(u->core->sources, s->loaded.index);
112 assert(source);
113 *ret_ss = source->sample_spec;
114 *ret_description = source->description;
115 } else
116 assert(0);
117 }
118
119 static AvahiStringList* txt_record_server_data(pa_core *c, AvahiStringList *l) {
120 char s[128];
121 assert(c);
122
123 l = avahi_string_list_add_pair(l, "server-version", PACKAGE_NAME" "PACKAGE_VERSION);
124 l = avahi_string_list_add_pair(l, "user-name", pa_get_user_name(s, sizeof(s)));
125 l = avahi_string_list_add_pair(l, "fqdn", pa_get_fqdn(s, sizeof(s)));
126 l = avahi_string_list_add_printf(l, "cookie=0x%08x", c->cookie);
127
128 return l;
129 }
130
131 static int publish_service(struct userdata *u, struct service *s);
132
133 static void service_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
134 struct service *s = userdata;
135
136 if (state == AVAHI_ENTRY_GROUP_COLLISION) {
137 char *t;
138
139 t = avahi_alternative_service_name(s->service_name);
140 pa_xfree(s->service_name);
141 s->service_name = t;
142
143 publish_service(s->userdata, s);
144 }
145 }
146
147 static int publish_service(struct userdata *u, struct service *s) {
148 int r = -1;
149 AvahiStringList *txt = NULL;
150
151 assert(u);
152 assert(s);
153
154 if (!u->client || avahi_client_get_state(u->client) != AVAHI_CLIENT_S_RUNNING)
155 return 0;
156
157 if ((s->published == PUBLISHED_REAL && s->loaded.valid) ||
158 (s->published == PUBLISHED_AUTOLOAD && s->autoload.valid && !s->loaded.valid))
159 return 0;
160
161 if (s->published != UNPUBLISHED) {
162 avahi_entry_group_reset(s->entry_group);
163 s->published = UNPUBLISHED;
164 }
165
166 if (s->loaded.valid || s->autoload.valid) {
167 pa_namereg_type_t type;
168
169 if (!s->entry_group) {
170 if (!(s->entry_group = avahi_entry_group_new(u->client, service_entry_group_callback, s))) {
171 pa_log("avahi_entry_group_new(): %s", avahi_strerror(avahi_client_errno(u->client)));
172 goto finish;
173 }
174 }
175
176 txt = avahi_string_list_add_pair(txt, "device", s->name);
177 txt = txt_record_server_data(u->core, txt);
178
179 if (s->loaded.valid) {
180 char *description;
181 pa_sample_spec ss;
182
183 get_service_data(u, s, &ss, &description);
184
185 txt = avahi_string_list_add_printf(txt, "rate=%u", ss.rate);
186 txt = avahi_string_list_add_printf(txt, "channels=%u", ss.channels);
187 txt = avahi_string_list_add_pair(txt, "format", pa_sample_format_to_string(ss.format));
188 if (description)
189 txt = avahi_string_list_add_pair(txt, "description", description);
190
191 type = s->loaded.type;
192 } else if (s->autoload.valid)
193 type = s->autoload.type;
194
195 if (avahi_entry_group_add_service_strlst(
196 s->entry_group,
197 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
198 0,
199 s->service_name,
200 type == PA_NAMEREG_SINK ? SERVICE_TYPE_SINK : SERVICE_TYPE_SOURCE,
201 NULL,
202 NULL,
203 u->port,
204 txt) < 0) {
205
206 pa_log(__FILE__": avahi_entry_group_add_service_strlst(): %s", avahi_strerror(avahi_client_errno(u->client)));
207 goto finish;
208 }
209
210 if (avahi_entry_group_commit(s->entry_group) < 0) {
211 pa_log(__FILE__": avahi_entry_group_commit(): %s", avahi_strerror(avahi_client_errno(u->client)));
212 goto finish;
213 }
214
215 if (s->loaded.valid)
216 s->published = PUBLISHED_REAL;
217 else if (s->autoload.valid)
218 s->published = PUBLISHED_AUTOLOAD;
219 }
220
221 r = 0;
222
223 finish:
224
225 if (s->published == UNPUBLISHED) {
226 /* Remove this service */
227
228 if (s->entry_group) {
229 avahi_entry_group_free(s->entry_group);
230 }
231
232 pa_hashmap_remove(u->services, s->name);
233 pa_xfree(s->name);
234 pa_xfree(s->service_name);
235 pa_xfree(s);
236 }
237
238 if (txt)
239 avahi_string_list_free(txt);
240
241 return r;
242 }
243
244 static struct service *get_service(struct userdata *u, const char *name) {
245 struct service *s;
246 char hn[64];
247
248 if ((s = pa_hashmap_get(u->services, name)))
249 return s;
250
251 s = pa_xnew(struct service, 1);
252 s->userdata = u;
253 s->entry_group = NULL;
254 s->published = UNPUBLISHED;
255 s->name = pa_xstrdup(name);
256 s->loaded.valid = s->autoload.valid = 0;
257 s->service_name = pa_sprintf_malloc("%s on %s", s->name, pa_get_host_name(hn, sizeof(hn)));
258
259 pa_hashmap_put(u->services, s->name, s);
260
261 return s;
262 }
263
264 static int publish_sink(struct userdata *u, pa_sink *s) {
265 struct service *svc;
266 assert(u && s);
267
268 svc = get_service(u, s->name);
269 if (svc->loaded.valid)
270 return publish_service(u, svc);
271
272 svc->loaded.valid = 1;
273 svc->loaded.type = PA_NAMEREG_SINK;
274 svc->loaded.index = s->index;
275
276 pa_dynarray_put(u->sink_dynarray, s->index, svc);
277
278 return publish_service(u, svc);
279 }
280
281 static int publish_source(struct userdata *u, pa_source *s) {
282 struct service *svc;
283 assert(u && s);
284
285 svc = get_service(u, s->name);
286 if (svc->loaded.valid)
287 return publish_service(u, svc);
288
289 svc->loaded.valid = 1;
290 svc->loaded.type = PA_NAMEREG_SOURCE;
291 svc->loaded.index = s->index;
292
293 pa_dynarray_put(u->source_dynarray, s->index, svc);
294
295 return publish_service(u, svc);
296 }
297
298 static int publish_autoload(struct userdata *u, pa_autoload_entry *s) {
299 struct service *svc;
300 assert(u && s);
301
302 svc = get_service(u, s->name);
303 if (svc->autoload.valid)
304 return publish_service(u, svc);
305
306 svc->autoload.valid = 1;
307 svc->autoload.type = s->type;
308 svc->autoload.index = s->index;
309
310 pa_dynarray_put(u->autoload_dynarray, s->index, svc);
311
312 return publish_service(u, svc);
313 }
314
315 static int remove_sink(struct userdata *u, uint32_t idx) {
316 struct service *svc;
317 assert(u && idx != PA_INVALID_INDEX);
318
319 if (!(svc = pa_dynarray_get(u->sink_dynarray, idx)))
320 return 0;
321
322 if (!svc->loaded.valid || svc->loaded.type != PA_NAMEREG_SINK)
323 return 0;
324
325 svc->loaded.valid = 0;
326 pa_dynarray_put(u->sink_dynarray, idx, NULL);
327
328 return publish_service(u, svc);
329 }
330
331 static int remove_source(struct userdata *u, uint32_t idx) {
332 struct service *svc;
333 assert(u && idx != PA_INVALID_INDEX);
334
335 if (!(svc = pa_dynarray_get(u->source_dynarray, idx)))
336 return 0;
337
338 if (!svc->loaded.valid || svc->loaded.type != PA_NAMEREG_SOURCE)
339 return 0;
340
341 svc->loaded.valid = 0;
342 pa_dynarray_put(u->source_dynarray, idx, NULL);
343
344 return publish_service(u, svc);
345 }
346
347 static int remove_autoload(struct userdata *u, uint32_t idx) {
348 struct service *svc;
349 assert(u && idx != PA_INVALID_INDEX);
350
351 if (!(svc = pa_dynarray_get(u->autoload_dynarray, idx)))
352 return 0;
353
354 if (!svc->autoload.valid)
355 return 0;
356
357 svc->autoload.valid = 0;
358 pa_dynarray_put(u->autoload_dynarray, idx, NULL);
359
360 return publish_service(u, svc);
361 }
362
363 static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
364 struct userdata *u = userdata;
365 assert(u && c);
366
367 switch (t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK)
368 case PA_SUBSCRIPTION_EVENT_SINK: {
369 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
370 pa_sink *sink;
371
372 if ((sink = pa_idxset_get_by_index(c->sinks, idx))) {
373 if (publish_sink(u, sink) < 0)
374 goto fail;
375 }
376 } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
377 if (remove_sink(u, idx) < 0)
378 goto fail;
379 }
380
381 break;
382
383 case PA_SUBSCRIPTION_EVENT_SOURCE:
384
385 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
386 pa_source *source;
387
388 if ((source = pa_idxset_get_by_index(c->sources, idx))) {
389 if (publish_source(u, source) < 0)
390 goto fail;
391 }
392 } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
393 if (remove_source(u, idx) < 0)
394 goto fail;
395 }
396
397 break;
398
399 case PA_SUBSCRIPTION_EVENT_AUTOLOAD:
400 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
401 pa_autoload_entry *autoload;
402
403 if ((autoload = pa_idxset_get_by_index(c->autoload_idxset, idx))) {
404 if (publish_autoload(u, autoload) < 0)
405 goto fail;
406 }
407 } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
408 if (remove_autoload(u, idx) < 0)
409 goto fail;
410 }
411
412 break;
413 }
414
415 return;
416
417 fail:
418 if (u->subscription) {
419 pa_subscription_free(u->subscription);
420 u->subscription = NULL;
421 }
422 }
423
424 static int publish_main_service(struct userdata *u);
425
426 static void main_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
427 struct userdata *u = userdata;
428 assert(u);
429
430 if (state == AVAHI_ENTRY_GROUP_COLLISION) {
431 char *t;
432
433 t = avahi_alternative_service_name(u->service_name);
434 pa_xfree(u->service_name);
435 u->service_name = t;
436
437 publish_main_service(u);
438 }
439 }
440
441 static int publish_main_service(struct userdata *u) {
442 AvahiStringList *txt = NULL;
443 int r = -1;
444
445 if (!u->main_entry_group) {
446 if (!(u->main_entry_group = avahi_entry_group_new(u->client, main_entry_group_callback, u))) {
447 pa_log(__FILE__": avahi_entry_group_new() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
448 goto fail;
449 }
450 } else
451 avahi_entry_group_reset(u->main_entry_group);
452
453 txt = txt_record_server_data(u->core, NULL);
454
455 if (avahi_entry_group_add_service_strlst(
456 u->main_entry_group,
457 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
458 0,
459 u->service_name,
460 SERVICE_TYPE_SERVER,
461 NULL,
462 NULL,
463 u->port,
464 txt) < 0) {
465
466 pa_log(__FILE__": avahi_entry_group_add_service_strlst() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
467 goto fail;
468 }
469
470 if (avahi_entry_group_commit(u->main_entry_group) < 0) {
471 pa_log(__FILE__": avahi_entry_group_commit() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
472 goto fail;
473 }
474
475 r = 0;
476
477 fail:
478 avahi_string_list_free(txt);
479
480 return r;
481 }
482
483 static int publish_all_services(struct userdata *u) {
484 pa_sink *sink;
485 pa_source *source;
486 pa_autoload_entry *autoload;
487 int r = -1;
488 uint32_t idx;
489
490 assert(u);
491
492 pa_log_debug(__FILE__": Publishing services in Zeroconf");
493
494 for (sink = pa_idxset_first(u->core->sinks, &idx); sink; sink = pa_idxset_next(u->core->sinks, &idx))
495 if (publish_sink(u, sink) < 0)
496 goto fail;
497
498 for (source = pa_idxset_first(u->core->sources, &idx); source; source = pa_idxset_next(u->core->sources, &idx))
499 if (publish_source(u, source) < 0)
500 goto fail;
501
502 if (u->core->autoload_idxset)
503 for (autoload = pa_idxset_first(u->core->autoload_idxset, &idx); autoload; autoload = pa_idxset_next(u->core->autoload_idxset, &idx))
504 if (publish_autoload(u, autoload) < 0)
505 goto fail;
506
507 if (publish_main_service(u) < 0)
508 goto fail;
509
510 r = 0;
511
512 fail:
513 return r;
514 }
515
516 static void unpublish_all_services(struct userdata *u, int rem) {
517 void *state = NULL;
518 struct service *s;
519
520 assert(u);
521
522 pa_log_debug(__FILE__": Unpublishing services in Zeroconf");
523
524 while ((s = pa_hashmap_iterate(u->services, &state, NULL))) {
525 if (s->entry_group) {
526 if (rem) {
527 avahi_entry_group_free(s->entry_group);
528 s->entry_group = NULL;
529 } else
530 avahi_entry_group_reset(s->entry_group);
531 }
532
533 s->published = UNPUBLISHED;
534 }
535
536 if (u->main_entry_group) {
537 if (rem) {
538 avahi_entry_group_free(u->main_entry_group);
539 u->main_entry_group = NULL;
540 } else
541 avahi_entry_group_reset(u->main_entry_group);
542 }
543 }
544
545 static void client_callback(AvahiClient *c, AvahiClientState state, void *userdata) {
546 struct userdata *u = userdata;
547 assert(c);
548
549 u->client = c;
550
551 switch (state) {
552 case AVAHI_CLIENT_S_RUNNING:
553 publish_all_services(u);
554 break;
555
556 case AVAHI_CLIENT_S_COLLISION:
557 unpublish_all_services(u, 0);
558 break;
559
560 case AVAHI_CLIENT_FAILURE:
561 if (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
562 int error;
563 unpublish_all_services(u, 1);
564 avahi_client_free(u->client);
565
566 if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error)))
567 pa_log(__FILE__": pa_avahi_client_new() failed: %s", avahi_strerror(error));
568 }
569
570 break;
571
572 default: ;
573 }
574 }
575
576 int pa__init(pa_core *c, pa_module*m) {
577 struct userdata *u;
578 uint32_t port = PA_NATIVE_DEFAULT_PORT;
579 pa_modargs *ma = NULL;
580 char hn[256];
581 int error;
582
583 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
584 pa_log(__FILE__": failed to parse module arguments.");
585 goto fail;
586 }
587
588 if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port == 0 || port >= 0xFFFF) {
589 pa_log(__FILE__": invalid port specified.");
590 goto fail;
591 }
592
593 m->userdata = u = pa_xnew(struct userdata, 1);
594 u->core = c;
595 u->port = (uint16_t) port;
596
597 u->avahi_poll = pa_avahi_poll_new(c->mainloop);
598
599 u->services = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
600 u->sink_dynarray = pa_dynarray_new();
601 u->source_dynarray = pa_dynarray_new();
602 u->autoload_dynarray = pa_dynarray_new();
603
604 u->subscription = pa_subscription_new(c,
605 PA_SUBSCRIPTION_MASK_SINK|
606 PA_SUBSCRIPTION_MASK_SOURCE|
607 PA_SUBSCRIPTION_MASK_AUTOLOAD, subscribe_callback, u);
608
609 u->main_entry_group = NULL;
610
611 u->service_name = pa_xstrdup(pa_get_host_name(hn, sizeof(hn)));
612
613 if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
614 pa_log(__FILE__": pa_avahi_client_new() failed: %s", avahi_strerror(error));
615 goto fail;
616 }
617
618 pa_modargs_free(ma);
619
620 return 0;
621
622 fail:
623 pa__done(c, m);
624
625 if (ma)
626 pa_modargs_free(ma);
627
628 return -1;
629 }
630
631 static void service_free(void *p, void *userdata) {
632 struct service *s = p;
633 struct userdata *u = userdata;
634
635 assert(s);
636 assert(u);
637
638 if (s->entry_group)
639 avahi_entry_group_free(s->entry_group);
640
641 pa_xfree(s->service_name);
642 pa_xfree(s->name);
643 pa_xfree(s);
644 }
645
646 void pa__done(pa_core *c, pa_module*m) {
647 struct userdata*u;
648 assert(c && m);
649
650 if (!(u = m->userdata))
651 return;
652
653 if (u->services)
654 pa_hashmap_free(u->services, service_free, u);
655
656 if (u->sink_dynarray)
657 pa_dynarray_free(u->sink_dynarray, NULL, NULL);
658 if (u->source_dynarray)
659 pa_dynarray_free(u->source_dynarray, NULL, NULL);
660 if (u->autoload_dynarray)
661 pa_dynarray_free(u->autoload_dynarray, NULL, NULL);
662
663 if (u->subscription)
664 pa_subscription_free(u->subscription);
665
666 if (u->main_entry_group)
667 avahi_entry_group_free(u->main_entry_group);
668
669 if (u->client)
670 avahi_client_free(u->client);
671
672 if (u->avahi_poll)
673 pa_avahi_poll_free(u->avahi_poll);
674
675 pa_xfree(u->service_name);
676 pa_xfree(u);
677 }
678