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