4 This file is part of polypaudio.
6 polypaudio 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.
11 polypaudio 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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #include <polypcore/howl-wrap.h>
33 #include <polypcore/xmalloc.h>
34 #include <polypcore/autoload.h>
35 #include <polypcore/sink.h>
36 #include <polypcore/source.h>
37 #include <polypcore/native-common.h>
38 #include <polypcore/util.h>
39 #include <polypcore/log.h>
40 #include <polypcore/subscribe.h>
41 #include <polypcore/dynarray.h>
42 #include <polypcore/endianmacros.h>
43 #include <polypcore/modargs.h>
45 #include "module-zeroconf-publish-symdef.h"
47 PA_MODULE_AUTHOR("Lennart Poettering")
48 PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Publisher")
49 PA_MODULE_VERSION(PACKAGE_VERSION)
50 PA_MODULE_USAGE("port=<IP port number>")
52 #define SERVICE_NAME_SINK "_polypaudio-sink._tcp"
53 #define SERVICE_NAME_SOURCE "_polypaudio-source._tcp"
54 #define SERVICE_NAME_SERVER "_polypaudio-server._tcp"
56 static const char* const valid_modargs[] = {
64 int published; /* 0 -> not yet registered, 1 -> registered with data from real device, 2 -> registered with data from autoload device */
81 pa_howl_wrapper *howl_wrapper;
83 pa_dynarray *sink_dynarray, *source_dynarray, *autoload_dynarray;
84 pa_subscription *subscription;
87 sw_discovery_oid server_oid;
90 static sw_result publish_reply(sw_discovery discovery, sw_discovery_publish_status status, sw_discovery_oid oid, sw_opaque extra) {
94 static void get_service_data(struct userdata *u, struct service *s, pa_sample_spec *ret_ss, char **ret_description, pa_typeid_t *ret_typeid) {
95 assert(u && s && s->loaded.valid && ret_ss && ret_description && ret_typeid);
97 if (s->loaded.type == PA_NAMEREG_SINK) {
98 pa_sink *sink = pa_idxset_get_by_index(u->core->sinks, s->loaded.index);
100 *ret_ss = sink->sample_spec;
101 *ret_description = sink->description;
102 *ret_typeid = sink->typeid;
103 } else if (s->loaded.type == PA_NAMEREG_SOURCE) {
104 pa_source *source = pa_idxset_get_by_index(u->core->sources, s->loaded.index);
106 *ret_ss = source->sample_spec;
107 *ret_description = source->description;
108 *ret_typeid = source->typeid;
113 static void txt_record_server_data(pa_core *c, sw_text_record t) {
117 sw_text_record_add_key_and_string_value(t, "server-version", PACKAGE_NAME" "PACKAGE_VERSION);
118 sw_text_record_add_key_and_string_value(t, "user-name", pa_get_user_name(s, sizeof(s)));
119 sw_text_record_add_key_and_string_value(t, "fqdn", pa_get_fqdn(s, sizeof(s)));
120 snprintf(s, sizeof(s), "0x%08x", c->cookie);
121 sw_text_record_add_key_and_string_value(t, "cookie", s);
124 static int publish_service(struct userdata *u, struct service *s) {
132 if ((s->published == 1 && s->loaded.valid) ||
133 (s->published == 2 && s->autoload.valid && !s->loaded.valid))
137 sw_discovery_cancel(pa_howl_wrapper_get_discovery(u->howl_wrapper), s->oid);
141 snprintf(t, sizeof(t), "Networked Audio Device %s on %s", s->name, pa_get_host_name(hn, sizeof(hn)));
143 if (sw_text_record_init(&txt) != SW_OKAY) {
144 pa_log(__FILE__": sw_text_record_init() failed\n");
149 sw_text_record_add_key_and_string_value(txt, "device", s->name);
151 txt_record_server_data(u->core, txt);
153 if (s->loaded.valid) {
154 char z[64], *description;
158 get_service_data(u, s, &ss, &description, &typeid);
160 snprintf(z, sizeof(z), "%u", ss.rate);
161 sw_text_record_add_key_and_string_value(txt, "rate", z);
162 snprintf(z, sizeof(z), "%u", ss.channels);
163 sw_text_record_add_key_and_string_value(txt, "channels", z);
164 sw_text_record_add_key_and_string_value(txt, "format", pa_sample_format_to_string(ss.format));
166 sw_text_record_add_key_and_string_value(txt, "description", description);
168 snprintf(z, sizeof(z), "0x%8x", typeid);
169 sw_text_record_add_key_and_string_value(txt, "typeid", z);
172 if (sw_discovery_publish(pa_howl_wrapper_get_discovery(u->howl_wrapper), 0, t,
173 s->loaded.type == PA_NAMEREG_SINK ? SERVICE_NAME_SINK : SERVICE_NAME_SOURCE,
174 NULL, NULL, u->port, sw_text_record_bytes(txt), sw_text_record_len(txt),
175 publish_reply, s, &s->oid) != SW_OKAY) {
176 pa_log(__FILE__": failed to register sink on zeroconf.\n");
181 } else if (s->autoload.valid) {
183 if (sw_discovery_publish(pa_howl_wrapper_get_discovery(u->howl_wrapper), 0, t,
184 s->autoload.type == PA_NAMEREG_SINK ? SERVICE_NAME_SINK : SERVICE_NAME_SOURCE,
185 NULL, NULL, u->port, sw_text_record_bytes(txt), sw_text_record_len(txt),
186 publish_reply, s, &s->oid) != SW_OKAY) {
187 pa_log(__FILE__": failed to register sink on zeroconf.\n");
199 /* Remove this service */
200 pa_hashmap_remove(u->services, s->name);
206 sw_text_record_fina(txt);
211 struct service *get_service(struct userdata *u, const char *name) {
214 if ((s = pa_hashmap_get(u->services, name)))
217 s = pa_xmalloc(sizeof(struct service));
219 s->name = pa_xstrdup(name);
220 s->loaded.valid = s->autoload.valid = 0;
222 pa_hashmap_put(u->services, s->name, s);
227 static int publish_sink(struct userdata *u, pa_sink *s) {
231 svc = get_service(u, s->name);
232 if (svc->loaded.valid)
235 svc->loaded.valid = 1;
236 svc->loaded.type = PA_NAMEREG_SINK;
237 svc->loaded.index = s->index;
239 pa_dynarray_put(u->sink_dynarray, s->index, svc);
241 return publish_service(u, svc);
244 static int publish_source(struct userdata *u, pa_source *s) {
248 svc = get_service(u, s->name);
249 if (svc->loaded.valid)
252 svc->loaded.valid = 1;
253 svc->loaded.type = PA_NAMEREG_SOURCE;
254 svc->loaded.index = s->index;
256 pa_dynarray_put(u->source_dynarray, s->index, svc);
258 return publish_service(u, svc);
261 static int publish_autoload(struct userdata *u, pa_autoload_entry *s) {
265 svc = get_service(u, s->name);
266 if (svc->autoload.valid)
269 svc->autoload.valid = 1;
270 svc->autoload.type = s->type;
271 svc->autoload.index = s->index;
273 pa_dynarray_put(u->autoload_dynarray, s->index, svc);
275 return publish_service(u, svc);
278 static int remove_sink(struct userdata *u, uint32_t index) {
280 assert(u && index != PA_INVALID_INDEX);
282 if (!(svc = pa_dynarray_get(u->sink_dynarray, index)))
285 if (!svc->loaded.valid || svc->loaded.type != PA_NAMEREG_SINK)
288 svc->loaded.valid = 0;
289 pa_dynarray_put(u->sink_dynarray, index, NULL);
291 return publish_service(u, svc);
294 static int remove_source(struct userdata *u, uint32_t index) {
296 assert(u && index != PA_INVALID_INDEX);
298 if (!(svc = pa_dynarray_get(u->source_dynarray, index)))
301 if (!svc->loaded.valid || svc->loaded.type != PA_NAMEREG_SOURCE)
304 svc->loaded.valid = 0;
305 pa_dynarray_put(u->source_dynarray, index, NULL);
307 return publish_service(u, svc);
310 static int remove_autoload(struct userdata *u, uint32_t index) {
312 assert(u && index != PA_INVALID_INDEX);
314 if (!(svc = pa_dynarray_get(u->autoload_dynarray, index)))
317 if (!svc->autoload.valid)
320 svc->autoload.valid = 0;
321 pa_dynarray_put(u->autoload_dynarray, index, NULL);
323 return publish_service(u, svc);
326 static void subscribe_callback(pa_core *c, pa_subscription_event_type t, uint32_t index, void *userdata) {
327 struct userdata *u = userdata;
330 switch (t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK)
331 case PA_SUBSCRIPTION_EVENT_SINK: {
332 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
335 if ((sink = pa_idxset_get_by_index(c->sinks, index))) {
336 if (publish_sink(u, sink) < 0)
339 } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
340 if (remove_sink(u, index) < 0)
346 case PA_SUBSCRIPTION_EVENT_SOURCE:
348 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
351 if ((source = pa_idxset_get_by_index(c->sources, index))) {
352 if (publish_source(u, source) < 0)
355 } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
356 if (remove_source(u, index) < 0)
362 case PA_SUBSCRIPTION_EVENT_AUTOLOAD:
363 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
364 pa_autoload_entry *autoload;
366 if ((autoload = pa_idxset_get_by_index(c->autoload_idxset, index))) {
367 if (publish_autoload(u, autoload) < 0)
370 } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
371 if (remove_autoload(u, index) < 0)
381 if (u->subscription) {
382 pa_subscription_free(u->subscription);
383 u->subscription = NULL;
387 int pa__init(pa_core *c, pa_module*m) {
389 uint32_t index, port = PA_NATIVE_DEFAULT_PORT;
392 pa_autoload_entry *autoload;
393 pa_modargs *ma = NULL;
394 char t[256], hn[256];
398 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
399 pa_log(__FILE__": failed to parse module arguments.\n");
403 if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port == 0 || port >= 0xFFFF) {
404 pa_log(__FILE__": invalid port specified.\n");
408 m->userdata = u = pa_xmalloc(sizeof(struct userdata));
410 u->port = (uint16_t) port;
412 if (!(u->howl_wrapper = pa_howl_wrapper_get(c)))
415 u->services = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
416 u->sink_dynarray = pa_dynarray_new();
417 u->source_dynarray = pa_dynarray_new();
418 u->autoload_dynarray = pa_dynarray_new();
420 u->subscription = pa_subscription_new(c,
421 PA_SUBSCRIPTION_MASK_SINK|
422 PA_SUBSCRIPTION_MASK_SOURCE|
423 PA_SUBSCRIPTION_MASK_AUTOLOAD, subscribe_callback, u);
425 for (sink = pa_idxset_first(c->sinks, &index); sink; sink = pa_idxset_next(c->sinks, &index))
426 if (publish_sink(u, sink) < 0)
429 for (source = pa_idxset_first(c->sources, &index); source; source = pa_idxset_next(c->sources, &index))
430 if (publish_source(u, source) < 0)
433 if (c->autoload_idxset)
434 for (autoload = pa_idxset_first(c->autoload_idxset, &index); autoload; autoload = pa_idxset_next(c->autoload_idxset, &index))
435 if (publish_autoload(u, autoload) < 0)
438 snprintf(t, sizeof(t), "Networked Audio Server on %s", pa_get_host_name(hn, sizeof(hn)));
440 if (sw_text_record_init(&txt) != SW_OKAY) {
441 pa_log(__FILE__": sw_text_record_init() failed\n");
446 txt_record_server_data(u->core, txt);
448 if (sw_discovery_publish(pa_howl_wrapper_get_discovery(u->howl_wrapper), 0, t,
450 NULL, NULL, u->port, sw_text_record_bytes(txt), sw_text_record_len(txt),
451 publish_reply, u, &u->server_oid) != SW_OKAY) {
452 pa_log(__FILE__": failed to register server on zeroconf.\n");
456 sw_text_record_fina(txt);
468 sw_text_record_fina(txt);
473 static void service_free(void *p, void *userdata) {
474 struct service *s = p;
475 struct userdata *u = userdata;
477 sw_discovery_cancel(pa_howl_wrapper_get_discovery(u->howl_wrapper), s->oid);
482 void pa__done(pa_core *c, pa_module*m) {
486 if (!(u = m->userdata))
490 pa_hashmap_free(u->services, service_free, u);
492 if (u->sink_dynarray)
493 pa_dynarray_free(u->sink_dynarray, NULL, NULL);
494 if (u->source_dynarray)
495 pa_dynarray_free(u->source_dynarray, NULL, NULL);
496 if (u->autoload_dynarray)
497 pa_dynarray_free(u->autoload_dynarray, NULL, NULL);
500 pa_subscription_free(u->subscription);
503 pa_howl_wrapper_unref(u->howl_wrapper);