]> code.delx.au - pulseaudio/blob - src/modules/module-zeroconf-publish.c
don't announce monitor sources
[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 #define SERVICE_SUBTYPE_SINK_HARDWARE "_hardware._sub."SERVICE_TYPE_SINK
64 #define SERVICE_SUBTYPE_SINK_VIRTUAL "_virtual._sub."SERVICE_TYPE_SINK
65 #define SERVICE_SUBTYPE_SOURCE_HARDWARE "_hardware._sub."SERVICE_TYPE_SOURCE
66 #define SERVICE_SUBTYPE_SOURCE_VIRTUAL "_virtual._sub."SERVICE_TYPE_SOURCE
67 #define SERVICE_SUBTYPE_SOURCE_MONITOR "_monitor._sub."SERVICE_TYPE_SOURCE
68 #define SERVICE_SUBTYPE_SOURCE_NON_MONITOR "_non-monitor._sub."SERVICE_TYPE_SOURCE
69
70 static const char* const valid_modargs[] = {
71 "port",
72 NULL
73 };
74
75 enum service_subtype {
76 SUBTYPE_HARDWARE,
77 SUBTYPE_VIRTUAL,
78 SUBTYPE_MONITOR
79 };
80
81 struct service {
82 struct userdata *userdata;
83 AvahiEntryGroup *entry_group;
84 char *service_name;
85 pa_object *device;
86 enum service_subtype subtype;
87 };
88
89 struct userdata {
90 pa_core *core;
91 pa_module *module;
92 AvahiPoll *avahi_poll;
93 AvahiClient *client;
94
95 pa_hashmap *services;
96 char *service_name;
97
98 AvahiEntryGroup *main_entry_group;
99
100 uint16_t port;
101
102 pa_hook_slot *sink_new_slot, *source_new_slot, *sink_unlink_slot, *source_unlink_slot, *sink_changed_slot, *source_changed_slot;
103 };
104
105 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, enum service_subtype *ret_subtype) {
106 pa_assert(s);
107 pa_assert(ret_ss);
108 pa_assert(ret_description);
109 pa_assert(ret_subtype);
110
111 if (pa_sink_isinstance(s->device)) {
112 pa_sink *sink = PA_SINK(s->device);
113
114 *ret_ss = sink->sample_spec;
115 *ret_map = sink->channel_map;
116 *ret_name = sink->name;
117 *ret_description = sink->description;
118 *ret_subtype = sink->flags & PA_SINK_HARDWARE ? SUBTYPE_HARDWARE : SUBTYPE_VIRTUAL;
119
120 } else if (pa_source_isinstance(s->device)) {
121 pa_source *source = PA_SOURCE(s->device);
122
123 *ret_ss = source->sample_spec;
124 *ret_map = source->channel_map;
125 *ret_name = source->name;
126 *ret_description = source->description;
127 *ret_subtype = source->monitor_of ? SUBTYPE_MONITOR : (source->flags & PA_SOURCE_HARDWARE ? SUBTYPE_HARDWARE : SUBTYPE_VIRTUAL);
128
129 } else
130 pa_assert_not_reached();
131 }
132
133 static AvahiStringList* txt_record_server_data(pa_core *c, AvahiStringList *l) {
134 char s[128];
135
136 pa_assert(c);
137
138 l = avahi_string_list_add_pair(l, "server-version", PACKAGE_NAME" "PACKAGE_VERSION);
139 l = avahi_string_list_add_pair(l, "user-name", pa_get_user_name(s, sizeof(s)));
140 l = avahi_string_list_add_pair(l, "fqdn", pa_get_fqdn(s, sizeof(s)));
141 l = avahi_string_list_add_printf(l, "cookie=0x%08x", c->cookie);
142
143 return l;
144 }
145
146 static int publish_service(struct service *s);
147
148 static void service_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
149 struct service *s = userdata;
150
151 pa_assert(s);
152
153 switch (state) {
154
155 case AVAHI_ENTRY_GROUP_ESTABLISHED:
156 pa_log_info("Successfully established service %s.", s->service_name);
157 break;
158
159 case AVAHI_ENTRY_GROUP_COLLISION: {
160 char *t;
161
162 t = avahi_alternative_service_name(s->service_name);
163 pa_log_info("Name collision, renaming %s to %s.", s->service_name, t);
164 pa_xfree(s->service_name);
165 s->service_name = t;
166
167 publish_service(s);
168 break;
169 }
170
171 case AVAHI_ENTRY_GROUP_FAILURE: {
172 pa_log("Failed to register service: %s", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
173
174 avahi_entry_group_free(g);
175 s->entry_group = NULL;
176
177 break;
178 }
179
180 case AVAHI_ENTRY_GROUP_UNCOMMITED:
181 case AVAHI_ENTRY_GROUP_REGISTERING:
182 ;
183 }
184 }
185
186 static void service_free(struct service *s);
187
188 static int publish_service(struct service *s) {
189 int r = -1;
190 AvahiStringList *txt = NULL;
191 const char *description = NULL, *name = NULL;
192 pa_sample_spec ss;
193 pa_channel_map map;
194 char cm[PA_CHANNEL_MAP_SNPRINT_MAX];
195 enum service_subtype subtype;
196
197 const char * const subtype_text[] = {
198 [SUBTYPE_HARDWARE] = "hardware",
199 [SUBTYPE_VIRTUAL] = "virtual",
200 [SUBTYPE_MONITOR] = "monitor"
201 };
202
203 pa_assert(s);
204
205 if (!s->userdata->client || avahi_client_get_state(s->userdata->client) != AVAHI_CLIENT_S_RUNNING)
206 return 0;
207
208 if (!s->entry_group) {
209 if (!(s->entry_group = avahi_entry_group_new(s->userdata->client, service_entry_group_callback, s))) {
210 pa_log("avahi_entry_group_new(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
211 goto finish;
212 }
213 } else
214 avahi_entry_group_reset(s->entry_group);
215
216 txt = txt_record_server_data(s->userdata->core, txt);
217
218 get_service_data(s, &ss, &map, &name, &description, &subtype);
219 txt = avahi_string_list_add_pair(txt, "device", name);
220 txt = avahi_string_list_add_printf(txt, "rate=%u", ss.rate);
221 txt = avahi_string_list_add_printf(txt, "channels=%u", ss.channels);
222 txt = avahi_string_list_add_pair(txt, "format", pa_sample_format_to_string(ss.format));
223 txt = avahi_string_list_add_pair(txt, "channel_map", pa_channel_map_snprint(cm, sizeof(cm), &map));
224 txt = avahi_string_list_add_pair(txt, "subtype", subtype_text[subtype]);
225
226 if (avahi_entry_group_add_service_strlst(
227 s->entry_group,
228 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
229 0,
230 s->service_name,
231 pa_sink_isinstance(s->device) ? SERVICE_TYPE_SINK : SERVICE_TYPE_SOURCE,
232 NULL,
233 NULL,
234 s->userdata->port,
235 txt) < 0) {
236
237 pa_log("avahi_entry_group_add_service_strlst(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
238 goto finish;
239 }
240
241 if (avahi_entry_group_add_service_subtype(
242 s->entry_group,
243 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
244 0,
245 s->service_name,
246 pa_sink_isinstance(s->device) ? SERVICE_TYPE_SINK : SERVICE_TYPE_SOURCE,
247 NULL,
248 pa_sink_isinstance(s->device) ? (subtype == SUBTYPE_HARDWARE ? SERVICE_SUBTYPE_SINK_HARDWARE : SERVICE_SUBTYPE_SINK_VIRTUAL) :
249 (subtype == SUBTYPE_HARDWARE ? SERVICE_SUBTYPE_SOURCE_HARDWARE : (subtype == SUBTYPE_VIRTUAL ? SERVICE_SUBTYPE_SOURCE_VIRTUAL : SERVICE_SUBTYPE_SOURCE_MONITOR))) < 0) {
250
251 pa_log("avahi_entry_group_add_service_subtype(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
252 goto finish;
253 }
254
255 if (pa_source_isinstance(s->device) && subtype != SUBTYPE_MONITOR) {
256 if (avahi_entry_group_add_service_subtype(
257 s->entry_group,
258 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
259 0,
260 s->service_name,
261 SERVICE_TYPE_SOURCE,
262 NULL,
263 SERVICE_SUBTYPE_SOURCE_NON_MONITOR) < 0) {
264
265 pa_log("avahi_entry_group_add_service_subtype(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
266 goto finish;
267 }
268 }
269
270 if (avahi_entry_group_commit(s->entry_group) < 0) {
271 pa_log("avahi_entry_group_commit(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
272 goto finish;
273 }
274
275 r = 0;
276 pa_log_debug("Successfully created entry group for %s.", s->service_name);
277
278 finish:
279
280 /* Remove this service */
281 if (r < 0)
282 service_free(s);
283
284 avahi_string_list_free(txt);
285
286 return r;
287 }
288
289 static struct service *get_service(struct userdata *u, pa_object *device) {
290 struct service *s;
291 char hn[64], un[64];
292 const char *n;
293
294 pa_assert(u);
295 pa_object_assert_ref(device);
296
297 if ((s = pa_hashmap_get(u->services, device)))
298 return s;
299
300 s = pa_xnew(struct service, 1);
301 s->userdata = u;
302 s->entry_group = NULL;
303 s->device = device;
304
305 if (pa_sink_isinstance(device)) {
306 if (!(n = PA_SINK(device)->description))
307 n = PA_SINK(device)->name;
308 } else {
309 if (!(n = PA_SOURCE(device)->description))
310 n = PA_SOURCE(device)->name;
311 }
312
313 s->service_name = pa_truncate_utf8(pa_sprintf_malloc("%s@%s: %s",
314 pa_get_user_name(un, sizeof(un)),
315 pa_get_host_name(hn, sizeof(hn)),
316 n),
317 AVAHI_LABEL_MAX-1);
318
319 pa_hashmap_put(u->services, s->device, s);
320
321 return s;
322 }
323
324 static void service_free(struct service *s) {
325 pa_assert(s);
326
327 pa_hashmap_remove(s->userdata->services, s->device);
328
329 if (s->entry_group) {
330 pa_log_debug("Removing entry group for %s.", s->service_name);
331 avahi_entry_group_free(s->entry_group);
332 }
333
334 pa_xfree(s->service_name);
335 pa_xfree(s);
336 }
337
338 static pa_bool_t shall_ignore(pa_object *o) {
339 pa_object_assert_ref(o);
340
341 if (pa_sink_isinstance(o))
342 return !!(PA_SINK(o)->flags & PA_SINK_NETWORK);
343
344 if (pa_source_isinstance(o))
345 return PA_SOURCE(o)->monitor_of || (PA_SOURCE(o)->flags & PA_SOURCE_NETWORK);
346
347 pa_assert_not_reached();
348 }
349
350 static pa_hook_result_t device_new_or_changed_cb(pa_core *c, pa_object *o, struct userdata *u) {
351 pa_assert(c);
352 pa_object_assert_ref(o);
353
354 if (!shall_ignore(o))
355 publish_service(get_service(u, o));
356
357 return PA_HOOK_OK;
358 }
359
360 static pa_hook_result_t device_unlink_cb(pa_core *c, pa_object *o, struct userdata *u) {
361 struct service *s;
362
363 pa_assert(c);
364 pa_object_assert_ref(o);
365
366 if ((s = pa_hashmap_get(u->services, o)))
367 service_free(s);
368
369 return PA_HOOK_OK;
370 }
371
372 static int publish_main_service(struct userdata *u);
373
374 static void main_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
375 struct userdata *u = userdata;
376 pa_assert(u);
377
378 switch (state) {
379
380 case AVAHI_ENTRY_GROUP_ESTABLISHED:
381 pa_log_info("Successfully established main service.");
382 break;
383
384 case AVAHI_ENTRY_GROUP_COLLISION: {
385 char *t;
386
387 t = avahi_alternative_service_name(u->service_name);
388 pa_log_info("Name collision: renaming main service %s to %s.", u->service_name, t);
389 pa_xfree(u->service_name);
390 u->service_name = t;
391
392 publish_main_service(u);
393 break;
394 }
395
396 case AVAHI_ENTRY_GROUP_FAILURE: {
397 pa_log("Failed to register main service: %s", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
398
399 avahi_entry_group_free(g);
400 u->main_entry_group = NULL;
401 break;
402 }
403
404 case AVAHI_ENTRY_GROUP_UNCOMMITED:
405 case AVAHI_ENTRY_GROUP_REGISTERING:
406 break;
407 }
408 }
409
410 static int publish_main_service(struct userdata *u) {
411 AvahiStringList *txt = NULL;
412 int r = -1;
413
414 pa_assert(u);
415
416 if (!u->main_entry_group) {
417 if (!(u->main_entry_group = avahi_entry_group_new(u->client, main_entry_group_callback, u))) {
418 pa_log("avahi_entry_group_new() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
419 goto fail;
420 }
421 } else
422 avahi_entry_group_reset(u->main_entry_group);
423
424 txt = txt_record_server_data(u->core, txt);
425
426 if (avahi_entry_group_add_service_strlst(
427 u->main_entry_group,
428 AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
429 0,
430 u->service_name,
431 SERVICE_TYPE_SERVER,
432 NULL,
433 NULL,
434 u->port,
435 txt) < 0) {
436
437 pa_log("avahi_entry_group_add_service_strlst() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
438 goto fail;
439 }
440
441 if (avahi_entry_group_commit(u->main_entry_group) < 0) {
442 pa_log("avahi_entry_group_commit() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
443 goto fail;
444 }
445
446 r = 0;
447
448 fail:
449 avahi_string_list_free(txt);
450
451 return r;
452 }
453
454 static int publish_all_services(struct userdata *u) {
455 pa_sink *sink;
456 pa_source *source;
457 int r = -1;
458 uint32_t idx;
459
460 pa_assert(u);
461
462 pa_log_debug("Publishing services in Zeroconf");
463
464 for (sink = PA_SINK(pa_idxset_first(u->core->sinks, &idx)); sink; sink = PA_SINK(pa_idxset_next(u->core->sinks, &idx)))
465 if (!shall_ignore(PA_OBJECT(sink)))
466 publish_service(get_service(u, PA_OBJECT(sink)));
467
468 for (source = PA_SOURCE(pa_idxset_first(u->core->sources, &idx)); source; source = PA_SOURCE(pa_idxset_next(u->core->sources, &idx)))
469 if (!shall_ignore(PA_OBJECT(source)))
470 publish_service(get_service(u, PA_OBJECT(source)));
471
472 if (publish_main_service(u) < 0)
473 goto fail;
474
475 r = 0;
476
477 fail:
478 return r;
479 }
480
481 static void unpublish_all_services(struct userdata *u, pa_bool_t rem) {
482 void *state = NULL;
483 struct service *s;
484
485 pa_assert(u);
486
487 pa_log_debug("Unpublishing services in Zeroconf");
488
489 while ((s = pa_hashmap_iterate(u->services, &state, NULL))) {
490 if (s->entry_group) {
491 if (rem) {
492 pa_log_debug("Removing entry group for %s.", s->service_name);
493 avahi_entry_group_free(s->entry_group);
494 s->entry_group = NULL;
495 } else {
496 avahi_entry_group_reset(s->entry_group);
497 pa_log_debug("Resetting entry group for %s.", s->service_name);
498 }
499 }
500 }
501
502 if (u->main_entry_group) {
503 if (rem) {
504 pa_log_debug("Removing main entry group.");
505 avahi_entry_group_free(u->main_entry_group);
506 u->main_entry_group = NULL;
507 } else {
508 avahi_entry_group_reset(u->main_entry_group);
509 pa_log_debug("Resetting main entry group.");
510 }
511 }
512 }
513
514 static void client_callback(AvahiClient *c, AvahiClientState state, void *userdata) {
515 struct userdata *u = userdata;
516
517 pa_assert(c);
518 pa_assert(u);
519
520 u->client = c;
521
522 switch (state) {
523 case AVAHI_CLIENT_S_RUNNING:
524 publish_all_services(u);
525 break;
526
527 case AVAHI_CLIENT_S_COLLISION:
528 pa_log_debug("Host name collision");
529 unpublish_all_services(u, FALSE);
530 break;
531
532 case AVAHI_CLIENT_FAILURE:
533 if (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
534 int error;
535
536 pa_log_debug("Avahi daemon disconnected.");
537
538 unpublish_all_services(u, TRUE);
539 avahi_client_free(u->client);
540
541 if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
542 pa_log("avahi_client_new() failed: %s", avahi_strerror(error));
543 pa_module_unload_request(u->module);
544 }
545 }
546
547 break;
548
549 default: ;
550 }
551 }
552
553 int pa__init(pa_module*m) {
554
555 struct userdata *u;
556 uint32_t port = PA_NATIVE_DEFAULT_PORT;
557 pa_modargs *ma = NULL;
558 char hn[256], un[256];
559 int error;
560
561 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
562 pa_log("Failed to parse module arguments.");
563 goto fail;
564 }
565
566 if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port <= 0 || port > 0xFFFF) {
567 pa_log("Invalid port specified.");
568 goto fail;
569 }
570
571 m->userdata = u = pa_xnew(struct userdata, 1);
572 u->core = m->core;
573 u->module = m;
574 u->port = (uint16_t) port;
575
576 u->avahi_poll = pa_avahi_poll_new(m->core->mainloop);
577
578 u->services = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
579
580 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);
581 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);
582 u->sink_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], (pa_hook_cb_t) device_unlink_cb, u);
583 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);
584 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);
585 u->source_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], (pa_hook_cb_t) device_unlink_cb, u);
586
587 u->main_entry_group = NULL;
588
589 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);
590
591 if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
592 pa_log("avahi_client_new() failed: %s", avahi_strerror(error));
593 goto fail;
594 }
595
596 pa_modargs_free(ma);
597
598 return 0;
599
600 fail:
601 pa__done(m);
602
603 if (ma)
604 pa_modargs_free(ma);
605
606 return -1;
607 }
608
609 void pa__done(pa_module*m) {
610 struct userdata*u;
611 pa_assert(m);
612
613 if (!(u = m->userdata))
614 return;
615
616 if (u->services) {
617 struct service *s;
618
619 while ((s = pa_hashmap_get_first(u->services)))
620 service_free(s);
621
622 pa_hashmap_free(u->services, NULL, NULL);
623 }
624
625 if (u->sink_new_slot)
626 pa_hook_slot_free(u->sink_new_slot);
627 if (u->source_new_slot)
628 pa_hook_slot_free(u->source_new_slot);
629 if (u->sink_changed_slot)
630 pa_hook_slot_free(u->sink_changed_slot);
631 if (u->source_changed_slot)
632 pa_hook_slot_free(u->source_changed_slot);
633 if (u->sink_unlink_slot)
634 pa_hook_slot_free(u->sink_unlink_slot);
635 if (u->source_unlink_slot)
636 pa_hook_slot_free(u->source_unlink_slot);
637
638 if (u->main_entry_group)
639 avahi_entry_group_free(u->main_entry_group);
640
641 if (u->client)
642 avahi_client_free(u->client);
643
644 if (u->avahi_poll)
645 pa_avahi_poll_free(u->avahi_poll);
646
647 pa_xfree(u->service_name);
648 pa_xfree(u);
649 }