]> code.delx.au - pulseaudio/blob - src/modules/module-zeroconf-publish.c
change pa_log() and friends to not require a trailing \n on all logged strings
[pulseaudio] / src / modules / module-zeroconf-publish.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
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 <polypcore/xmalloc.h>
33 #include <polypcore/autoload.h>
34 #include <polypcore/sink.h>
35 #include <polypcore/source.h>
36 #include <polypcore/native-common.h>
37 #include <polypcore/util.h>
38 #include <polypcore/log.h>
39 #include <polypcore/core-subscribe.h>
40 #include <polypcore/dynarray.h>
41 #include <polypcore/modargs.h>
42
43 #include "../polypcore/endianmacros.h"
44
45 #include "howl-wrap.h"
46
47 #include "module-zeroconf-publish-symdef.h"
48
49 PA_MODULE_AUTHOR("Lennart Poettering")
50 PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Publisher")
51 PA_MODULE_VERSION(PACKAGE_VERSION)
52 PA_MODULE_USAGE("port=<IP port number>")
53
54 #define SERVICE_NAME_SINK "_polypaudio-sink._tcp"
55 #define SERVICE_NAME_SOURCE "_polypaudio-source._tcp"
56 #define SERVICE_NAME_SERVER "_polypaudio-server._tcp"
57
58 static const char* const valid_modargs[] = {
59 "port",
60 NULL
61 };
62
63 struct service {
64 sw_discovery_oid oid;
65 char *name;
66 int published; /* 0 -> not yet registered, 1 -> registered with data from real device, 2 -> registered with data from autoload device */
67
68 struct {
69 int valid;
70 pa_namereg_type_t type;
71 uint32_t index;
72 } loaded;
73
74 struct {
75 int valid;
76 pa_namereg_type_t type;
77 uint32_t index;
78 } autoload;
79 };
80
81 struct userdata {
82 pa_core *core;
83 pa_howl_wrapper *howl_wrapper;
84 pa_hashmap *services;
85 pa_dynarray *sink_dynarray, *source_dynarray, *autoload_dynarray;
86 pa_subscription *subscription;
87
88 uint16_t port;
89 sw_discovery_oid server_oid;
90 };
91
92 static sw_result publish_reply(sw_discovery discovery, sw_discovery_publish_status status, sw_discovery_oid oid, sw_opaque extra) {
93 return SW_OKAY;
94 }
95
96 static void get_service_data(struct userdata *u, struct service *s, pa_sample_spec *ret_ss, char **ret_description) {
97 assert(u && s && s->loaded.valid && ret_ss && ret_description);
98
99 if (s->loaded.type == PA_NAMEREG_SINK) {
100 pa_sink *sink = pa_idxset_get_by_index(u->core->sinks, s->loaded.index);
101 assert(sink);
102 *ret_ss = sink->sample_spec;
103 *ret_description = sink->description;
104 } else if (s->loaded.type == PA_NAMEREG_SOURCE) {
105 pa_source *source = pa_idxset_get_by_index(u->core->sources, s->loaded.index);
106 assert(source);
107 *ret_ss = source->sample_spec;
108 *ret_description = source->description;
109 } else
110 assert(0);
111 }
112
113 static void txt_record_server_data(pa_core *c, sw_text_record t) {
114 char s[256];
115 assert(c);
116
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);
122 }
123
124 static int publish_service(struct userdata *u, struct service *s) {
125 char t[256];
126 char hn[256];
127 int r = -1;
128 sw_text_record txt;
129 int free_txt = 0;
130 assert(u && s);
131
132 if ((s->published == 1 && s->loaded.valid) ||
133 (s->published == 2 && s->autoload.valid && !s->loaded.valid))
134 return 0;
135
136 if (s->published) {
137 sw_discovery_cancel(pa_howl_wrapper_get_discovery(u->howl_wrapper), s->oid);
138 s->published = 0;
139 }
140
141 snprintf(t, sizeof(t), "Networked Audio Device %s on %s", s->name, pa_get_host_name(hn, sizeof(hn)));
142
143 if (sw_text_record_init(&txt) != SW_OKAY) {
144 pa_log(__FILE__": sw_text_record_init() failed");
145 goto finish;
146 }
147 free_txt = 1;
148
149 sw_text_record_add_key_and_string_value(txt, "device", s->name);
150
151 txt_record_server_data(u->core, txt);
152
153 if (s->loaded.valid) {
154 char z[64], *description;
155 pa_sample_spec ss;
156
157 get_service_data(u, s, &ss, &description);
158
159 snprintf(z, sizeof(z), "%u", ss.rate);
160 sw_text_record_add_key_and_string_value(txt, "rate", z);
161 snprintf(z, sizeof(z), "%u", ss.channels);
162 sw_text_record_add_key_and_string_value(txt, "channels", z);
163 sw_text_record_add_key_and_string_value(txt, "format", pa_sample_format_to_string(ss.format));
164
165 sw_text_record_add_key_and_string_value(txt, "description", description);
166
167 if (sw_discovery_publish(pa_howl_wrapper_get_discovery(u->howl_wrapper), 0, t,
168 s->loaded.type == PA_NAMEREG_SINK ? SERVICE_NAME_SINK : SERVICE_NAME_SOURCE,
169 NULL, NULL, u->port, sw_text_record_bytes(txt), sw_text_record_len(txt),
170 publish_reply, s, &s->oid) != SW_OKAY) {
171 pa_log(__FILE__": failed to register sink on zeroconf.");
172 goto finish;
173 }
174
175 s->published = 1;
176 } else if (s->autoload.valid) {
177
178 if (sw_discovery_publish(pa_howl_wrapper_get_discovery(u->howl_wrapper), 0, t,
179 s->autoload.type == PA_NAMEREG_SINK ? SERVICE_NAME_SINK : SERVICE_NAME_SOURCE,
180 NULL, NULL, u->port, sw_text_record_bytes(txt), sw_text_record_len(txt),
181 publish_reply, s, &s->oid) != SW_OKAY) {
182 pa_log(__FILE__": failed to register sink on zeroconf.");
183 goto finish;
184 }
185
186 s->published = 2;
187 }
188
189 r = 0;
190
191 finish:
192
193 if (!s->published) {
194 /* Remove this service */
195 pa_hashmap_remove(u->services, s->name);
196 pa_xfree(s->name);
197 pa_xfree(s);
198 }
199
200 if (free_txt)
201 sw_text_record_fina(txt);
202
203 return r;
204 }
205
206 static struct service *get_service(struct userdata *u, const char *name) {
207 struct service *s;
208
209 if ((s = pa_hashmap_get(u->services, name)))
210 return s;
211
212 s = pa_xmalloc(sizeof(struct service));
213 s->published = 0;
214 s->name = pa_xstrdup(name);
215 s->loaded.valid = s->autoload.valid = 0;
216
217 pa_hashmap_put(u->services, s->name, s);
218
219 return s;
220 }
221
222 static int publish_sink(struct userdata *u, pa_sink *s) {
223 struct service *svc;
224 assert(u && s);
225
226 svc = get_service(u, s->name);
227 if (svc->loaded.valid)
228 return 0;
229
230 svc->loaded.valid = 1;
231 svc->loaded.type = PA_NAMEREG_SINK;
232 svc->loaded.index = s->index;
233
234 pa_dynarray_put(u->sink_dynarray, s->index, svc);
235
236 return publish_service(u, svc);
237 }
238
239 static int publish_source(struct userdata *u, pa_source *s) {
240 struct service *svc;
241 assert(u && s);
242
243 svc = get_service(u, s->name);
244 if (svc->loaded.valid)
245 return 0;
246
247 svc->loaded.valid = 1;
248 svc->loaded.type = PA_NAMEREG_SOURCE;
249 svc->loaded.index = s->index;
250
251 pa_dynarray_put(u->source_dynarray, s->index, svc);
252
253 return publish_service(u, svc);
254 }
255
256 static int publish_autoload(struct userdata *u, pa_autoload_entry *s) {
257 struct service *svc;
258 assert(u && s);
259
260 svc = get_service(u, s->name);
261 if (svc->autoload.valid)
262 return 0;
263
264 svc->autoload.valid = 1;
265 svc->autoload.type = s->type;
266 svc->autoload.index = s->index;
267
268 pa_dynarray_put(u->autoload_dynarray, s->index, svc);
269
270 return publish_service(u, svc);
271 }
272
273 static int remove_sink(struct userdata *u, uint32_t idx) {
274 struct service *svc;
275 assert(u && idx != PA_INVALID_INDEX);
276
277 if (!(svc = pa_dynarray_get(u->sink_dynarray, idx)))
278 return 0;
279
280 if (!svc->loaded.valid || svc->loaded.type != PA_NAMEREG_SINK)
281 return 0;
282
283 svc->loaded.valid = 0;
284 pa_dynarray_put(u->sink_dynarray, idx, NULL);
285
286 return publish_service(u, svc);
287 }
288
289 static int remove_source(struct userdata *u, uint32_t idx) {
290 struct service *svc;
291 assert(u && idx != PA_INVALID_INDEX);
292
293 if (!(svc = pa_dynarray_get(u->source_dynarray, idx)))
294 return 0;
295
296 if (!svc->loaded.valid || svc->loaded.type != PA_NAMEREG_SOURCE)
297 return 0;
298
299 svc->loaded.valid = 0;
300 pa_dynarray_put(u->source_dynarray, idx, NULL);
301
302 return publish_service(u, svc);
303 }
304
305 static int remove_autoload(struct userdata *u, uint32_t idx) {
306 struct service *svc;
307 assert(u && idx != PA_INVALID_INDEX);
308
309 if (!(svc = pa_dynarray_get(u->autoload_dynarray, idx)))
310 return 0;
311
312 if (!svc->autoload.valid)
313 return 0;
314
315 svc->autoload.valid = 0;
316 pa_dynarray_put(u->autoload_dynarray, idx, NULL);
317
318 return publish_service(u, svc);
319 }
320
321 static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
322 struct userdata *u = userdata;
323 assert(u && c);
324
325 switch (t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK)
326 case PA_SUBSCRIPTION_EVENT_SINK: {
327 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
328 pa_sink *sink;
329
330 if ((sink = pa_idxset_get_by_index(c->sinks, idx))) {
331 if (publish_sink(u, sink) < 0)
332 goto fail;
333 }
334 } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
335 if (remove_sink(u, idx) < 0)
336 goto fail;
337 }
338
339 break;
340
341 case PA_SUBSCRIPTION_EVENT_SOURCE:
342
343 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
344 pa_source *source;
345
346 if ((source = pa_idxset_get_by_index(c->sources, idx))) {
347 if (publish_source(u, source) < 0)
348 goto fail;
349 }
350 } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
351 if (remove_source(u, idx) < 0)
352 goto fail;
353 }
354
355 break;
356
357 case PA_SUBSCRIPTION_EVENT_AUTOLOAD:
358 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
359 pa_autoload_entry *autoload;
360
361 if ((autoload = pa_idxset_get_by_index(c->autoload_idxset, idx))) {
362 if (publish_autoload(u, autoload) < 0)
363 goto fail;
364 }
365 } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
366 if (remove_autoload(u, idx) < 0)
367 goto fail;
368 }
369
370 break;
371 }
372
373 return;
374
375 fail:
376 if (u->subscription) {
377 pa_subscription_free(u->subscription);
378 u->subscription = NULL;
379 }
380 }
381
382 int pa__init(pa_core *c, pa_module*m) {
383 struct userdata *u;
384 uint32_t idx, port = PA_NATIVE_DEFAULT_PORT;
385 pa_sink *sink;
386 pa_source *source;
387 pa_autoload_entry *autoload;
388 pa_modargs *ma = NULL;
389 char t[256], hn[256];
390 int free_txt = 0;
391 sw_text_record txt;
392
393 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
394 pa_log(__FILE__": failed to parse module arguments.");
395 goto fail;
396 }
397
398 if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port == 0 || port >= 0xFFFF) {
399 pa_log(__FILE__": invalid port specified.");
400 goto fail;
401 }
402
403 m->userdata = u = pa_xmalloc(sizeof(struct userdata));
404 u->core = c;
405 u->port = (uint16_t) port;
406
407 if (!(u->howl_wrapper = pa_howl_wrapper_get(c)))
408 goto fail;
409
410 u->services = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
411 u->sink_dynarray = pa_dynarray_new();
412 u->source_dynarray = pa_dynarray_new();
413 u->autoload_dynarray = pa_dynarray_new();
414
415 u->subscription = pa_subscription_new(c,
416 PA_SUBSCRIPTION_MASK_SINK|
417 PA_SUBSCRIPTION_MASK_SOURCE|
418 PA_SUBSCRIPTION_MASK_AUTOLOAD, subscribe_callback, u);
419
420 for (sink = pa_idxset_first(c->sinks, &idx); sink; sink = pa_idxset_next(c->sinks, &idx))
421 if (publish_sink(u, sink) < 0)
422 goto fail;
423
424 for (source = pa_idxset_first(c->sources, &idx); source; source = pa_idxset_next(c->sources, &idx))
425 if (publish_source(u, source) < 0)
426 goto fail;
427
428 if (c->autoload_idxset)
429 for (autoload = pa_idxset_first(c->autoload_idxset, &idx); autoload; autoload = pa_idxset_next(c->autoload_idxset, &idx))
430 if (publish_autoload(u, autoload) < 0)
431 goto fail;
432
433 snprintf(t, sizeof(t), "Networked Audio Server on %s", pa_get_host_name(hn, sizeof(hn)));
434
435 if (sw_text_record_init(&txt) != SW_OKAY) {
436 pa_log(__FILE__": sw_text_record_init() failed");
437 goto fail;
438 }
439 free_txt = 1;
440
441 txt_record_server_data(u->core, txt);
442
443 if (sw_discovery_publish(pa_howl_wrapper_get_discovery(u->howl_wrapper), 0, t,
444 SERVICE_NAME_SERVER,
445 NULL, NULL, u->port, sw_text_record_bytes(txt), sw_text_record_len(txt),
446 publish_reply, u, &u->server_oid) != SW_OKAY) {
447 pa_log(__FILE__": failed to register server on zeroconf.");
448 goto fail;
449 }
450
451 sw_text_record_fina(txt);
452 pa_modargs_free(ma);
453
454 return 0;
455
456 fail:
457 pa__done(c, m);
458
459 if (ma)
460 pa_modargs_free(ma);
461
462 if (free_txt)
463 sw_text_record_fina(txt);
464
465 return -1;
466 }
467
468 static void service_free(void *p, void *userdata) {
469 struct service *s = p;
470 struct userdata *u = userdata;
471 assert(s && u);
472 sw_discovery_cancel(pa_howl_wrapper_get_discovery(u->howl_wrapper), s->oid);
473 pa_xfree(s->name);
474 pa_xfree(s);
475 }
476
477 void pa__done(pa_core *c, pa_module*m) {
478 struct userdata*u;
479 assert(c && m);
480
481 if (!(u = m->userdata))
482 return;
483
484 if (u->services)
485 pa_hashmap_free(u->services, service_free, u);
486
487 if (u->sink_dynarray)
488 pa_dynarray_free(u->sink_dynarray, NULL, NULL);
489 if (u->source_dynarray)
490 pa_dynarray_free(u->source_dynarray, NULL, NULL);
491 if (u->autoload_dynarray)
492 pa_dynarray_free(u->autoload_dynarray, NULL, NULL);
493
494 if (u->subscription)
495 pa_subscription_free(u->subscription);
496
497 if (u->howl_wrapper)
498 pa_howl_wrapper_unref(u->howl_wrapper);
499
500
501 pa_xfree(u);
502 }
503