45d566aeed6e5135aa28463b393b303736370588
[profile/ivi/pulseaudio.git] / 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/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>
44
45 #include "module-zeroconf-publish-symdef.h"
46
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>")
51
52 #define SERVICE_NAME_SINK "_polypaudio-sink._tcp"
53 #define SERVICE_NAME_SOURCE "_polypaudio-source._tcp"
54 #define SERVICE_NAME_SERVER "_polypaudio-server._tcp"
55
56 static const char* const valid_modargs[] = {
57     "port",
58     NULL
59 };
60
61 struct service {
62     sw_discovery_oid oid;
63     char *name;
64     int published; /* 0 -> not yet registered, 1 -> registered with data from real device, 2 -> registered with data from autoload device */
65
66     struct {
67         int valid;
68         pa_namereg_type type;
69         uint32_t index;
70     } loaded;
71
72     struct {
73         int valid;
74         pa_namereg_type type;
75         uint32_t index;
76     } autoload;
77 };
78
79 struct userdata {
80     pa_core *core;
81     pa_howl_wrapper *howl_wrapper;
82     pa_hashmap *services;
83     pa_dynarray *sink_dynarray, *source_dynarray, *autoload_dynarray;
84     pa_subscription *subscription;
85
86     uint16_t port;
87     sw_discovery_oid server_oid;
88 };
89
90 static sw_result publish_reply(sw_discovery discovery, sw_discovery_publish_status status, sw_discovery_oid oid, sw_opaque extra) {
91     return SW_OKAY;
92 }
93
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);
96
97     if (s->loaded.type == PA_NAMEREG_SINK) {
98         pa_sink *sink = pa_idxset_get_by_index(u->core->sinks, s->loaded.index);
99         assert(sink);
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);
105         assert(source);
106         *ret_ss = source->sample_spec;
107         *ret_description = source->description;
108         *ret_typeid = source->typeid;
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\n");
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_typeid_t typeid;
156         pa_sample_spec ss;
157
158         get_service_data(u, s, &ss, &description, &typeid);
159             
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));
165
166         sw_text_record_add_key_and_string_value(txt, "description", description);
167
168         snprintf(z, sizeof(z), "0x%8x", typeid);
169         sw_text_record_add_key_and_string_value(txt, "typeid", z);
170         
171         
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");
177             goto finish;
178         }
179
180         s->published = 1;
181     } else if (s->autoload.valid) {
182
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");
188             goto finish;
189         }
190
191         s->published = 2;
192     }
193
194     r = 0;
195     
196 finish:
197
198     if (!s->published) {
199         /* Remove this service */
200         pa_hashmap_remove(u->services, s->name);
201         pa_xfree(s->name);
202         pa_xfree(s);
203     }
204
205     if (free_txt)
206         sw_text_record_fina(txt);
207     
208     return r;
209 }
210
211 struct service *get_service(struct userdata *u, const char *name) {
212     struct service *s;
213     
214     if ((s = pa_hashmap_get(u->services, name)))
215         return s;
216     
217     s = pa_xmalloc(sizeof(struct service));
218     s->published = 0;
219     s->name = pa_xstrdup(name);
220     s->loaded.valid = s->autoload.valid = 0;
221
222     pa_hashmap_put(u->services, s->name, s);
223
224     return s;
225 }
226
227 static int publish_sink(struct userdata *u, pa_sink *s) {
228     struct service *svc;
229     assert(u && s);
230
231     svc = get_service(u, s->name);
232     if (svc->loaded.valid)
233         return 0;
234
235     svc->loaded.valid = 1;
236     svc->loaded.type = PA_NAMEREG_SINK;
237     svc->loaded.index = s->index;
238
239     pa_dynarray_put(u->sink_dynarray, s->index, svc);
240
241     return publish_service(u, svc);
242 }
243
244 static int publish_source(struct userdata *u, pa_source *s) {
245     struct service *svc;
246     assert(u && s);
247
248     svc = get_service(u, s->name);
249     if (svc->loaded.valid)
250         return 0;
251
252     svc->loaded.valid = 1;
253     svc->loaded.type = PA_NAMEREG_SOURCE;
254     svc->loaded.index = s->index;
255
256     pa_dynarray_put(u->source_dynarray, s->index, svc);
257     
258     return publish_service(u, svc);
259 }
260
261 static int publish_autoload(struct userdata *u, pa_autoload_entry *s) {
262     struct service *svc;
263     assert(u && s);
264
265     svc = get_service(u, s->name);
266     if (svc->autoload.valid)
267         return 0;
268
269     svc->autoload.valid = 1;
270     svc->autoload.type = s->type;
271     svc->autoload.index = s->index;
272
273     pa_dynarray_put(u->autoload_dynarray, s->index, svc);
274     
275     return publish_service(u, svc);
276 }
277
278 static int remove_sink(struct userdata *u, uint32_t index) {
279     struct service *svc;
280     assert(u && index != PA_INVALID_INDEX);
281
282     if (!(svc = pa_dynarray_get(u->sink_dynarray, index)))
283         return 0;
284
285     if (!svc->loaded.valid || svc->loaded.type != PA_NAMEREG_SINK)
286         return 0;
287
288     svc->loaded.valid = 0;
289     pa_dynarray_put(u->sink_dynarray, index, NULL);
290     
291     return publish_service(u, svc);
292 }
293
294 static int remove_source(struct userdata *u, uint32_t index) {
295     struct service *svc;
296     assert(u && index != PA_INVALID_INDEX);
297     
298     if (!(svc = pa_dynarray_get(u->source_dynarray, index)))
299         return 0;
300
301     if (!svc->loaded.valid || svc->loaded.type != PA_NAMEREG_SOURCE)
302         return 0;
303
304     svc->loaded.valid = 0;
305     pa_dynarray_put(u->source_dynarray, index, NULL);
306
307     return publish_service(u, svc);
308 }
309
310 static int remove_autoload(struct userdata *u, uint32_t index) {
311     struct service *svc;
312     assert(u && index != PA_INVALID_INDEX);
313     
314     if (!(svc = pa_dynarray_get(u->autoload_dynarray, index)))
315         return 0;
316
317     if (!svc->autoload.valid)
318         return 0;
319
320     svc->autoload.valid = 0;
321     pa_dynarray_put(u->autoload_dynarray, index, NULL);
322
323     return publish_service(u, svc);
324 }
325
326 static void subscribe_callback(pa_core *c, pa_subscription_event_type t, uint32_t index, void *userdata) {
327     struct userdata *u = userdata;
328     assert(u && c);
329
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) {
333                 pa_sink *sink;
334
335                 if ((sink = pa_idxset_get_by_index(c->sinks, index))) {
336                     if (publish_sink(u, sink) < 0)
337                         goto fail;
338                 }
339             } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
340                 if (remove_sink(u, index) < 0)
341                     goto fail;
342             }
343         
344             break;
345
346         case PA_SUBSCRIPTION_EVENT_SOURCE:
347
348             if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
349                 pa_source *source;
350                 
351                 if ((source = pa_idxset_get_by_index(c->sources, index))) {
352                     if (publish_source(u, source) < 0)
353                         goto fail;
354                 }
355             } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
356                 if (remove_source(u, index) < 0)
357                     goto fail;
358             }
359             
360             break;
361
362         case PA_SUBSCRIPTION_EVENT_AUTOLOAD:
363             if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
364                 pa_autoload_entry *autoload;
365                     
366                 if ((autoload = pa_idxset_get_by_index(c->autoload_idxset, index))) {
367                     if (publish_autoload(u, autoload) < 0)
368                         goto fail;
369                 }
370             } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
371                 if (remove_autoload(u, index) < 0)
372                         goto fail;
373             }
374             
375             break;
376     }
377
378     return;
379
380 fail:
381     if (u->subscription) {
382         pa_subscription_free(u->subscription);
383         u->subscription = NULL;
384     }
385 }
386
387 int pa__init(pa_core *c, pa_module*m) {
388     struct userdata *u;
389     uint32_t index, port = PA_NATIVE_DEFAULT_PORT;
390     pa_sink *sink;
391     pa_source *source;
392     pa_autoload_entry *autoload;
393     pa_modargs *ma = NULL;
394     char t[256], hn[256];
395     int free_txt = 0;
396     sw_text_record txt;
397
398     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
399         pa_log(__FILE__": failed to parse module arguments.\n");
400         goto fail;
401     }
402
403     if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port == 0 || port >= 0xFFFF) {
404         pa_log(__FILE__": invalid port specified.\n");
405         goto fail;
406     }
407
408     m->userdata = u = pa_xmalloc(sizeof(struct userdata));
409     u->core = c;
410     u->port = (uint16_t) port;
411
412     if (!(u->howl_wrapper = pa_howl_wrapper_get(c)))
413         goto fail;
414
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();
419     
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);
424
425     for (sink = pa_idxset_first(c->sinks, &index); sink; sink = pa_idxset_next(c->sinks, &index))
426         if (publish_sink(u, sink) < 0)
427             goto fail;
428
429     for (source = pa_idxset_first(c->sources, &index); source; source = pa_idxset_next(c->sources, &index))
430         if (publish_source(u, source) < 0)
431             goto fail;
432
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)
436                 goto fail;
437
438     snprintf(t, sizeof(t), "Networked Audio Server on %s", pa_get_host_name(hn, sizeof(hn)));   
439
440     if (sw_text_record_init(&txt) != SW_OKAY) {
441         pa_log(__FILE__": sw_text_record_init() failed\n");
442         goto fail;
443     }
444     free_txt = 1;
445
446     txt_record_server_data(u->core, txt);
447     
448     if (sw_discovery_publish(pa_howl_wrapper_get_discovery(u->howl_wrapper), 0, t,
449                              SERVICE_NAME_SERVER,
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");
453         goto fail;
454     }
455     
456     sw_text_record_fina(txt);
457     pa_modargs_free(ma);
458     
459     return 0;
460     
461 fail:
462     pa__done(c, m);
463
464     if (ma)
465         pa_modargs_free(ma);
466
467     if (free_txt)
468         sw_text_record_fina(txt);
469     
470     return -1;
471 }
472
473 static void service_free(void *p, void *userdata) {
474     struct service *s = p;
475     struct userdata *u = userdata;
476     assert(s && u);
477     sw_discovery_cancel(pa_howl_wrapper_get_discovery(u->howl_wrapper), s->oid);
478     pa_xfree(s->name);
479     pa_xfree(s);
480 }
481
482 void pa__done(pa_core *c, pa_module*m) {
483     struct userdata*u;
484     assert(c && m);
485
486     if (!(u = m->userdata))
487         return;
488
489     if (u->services)
490         pa_hashmap_free(u->services, service_free, u);
491
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);
498     
499     if (u->subscription)
500         pa_subscription_free(u->subscription);
501     
502     if (u->howl_wrapper)
503         pa_howl_wrapper_unref(u->howl_wrapper);
504
505     
506     pa_xfree(u);
507 }
508