2004-12-11 16:48:45 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2004-12-11 16:48:45 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
published by the Free Software Foundation; either version 2 of the
|
|
|
|
|
License, or (at your option) any later version.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2004-12-11 16:48:45 +00:00
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
General Public License for more details.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-12-11 16:48:45 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2006-06-19 21:53:48 +00:00
|
|
|
License along with PulseAudio; if not, write to the Free Software
|
2004-12-11 16:48:45 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
#include <avahi-client/client.h>
|
|
|
|
|
#include <avahi-client/publish.h>
|
|
|
|
|
#include <avahi-common/alternative.h>
|
|
|
|
|
#include <avahi-common/error.h>
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulse/xmalloc.h>
|
|
|
|
|
#include <pulse/util.h>
|
2006-05-17 16:34:18 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/autoload.h>
|
|
|
|
|
#include <pulsecore/sink.h>
|
|
|
|
|
#include <pulsecore/source.h>
|
|
|
|
|
#include <pulsecore/native-common.h>
|
|
|
|
|
#include <pulsecore/core-util.h>
|
|
|
|
|
#include <pulsecore/log.h>
|
|
|
|
|
#include <pulsecore/core-subscribe.h>
|
|
|
|
|
#include <pulsecore/dynarray.h>
|
|
|
|
|
#include <pulsecore/modargs.h>
|
2006-07-13 15:54:13 +00:00
|
|
|
#include <pulsecore/avahi-wrap.h>
|
|
|
|
|
#include <pulsecore/endianmacros.h>
|
2006-02-17 12:10:58 +00:00
|
|
|
|
2004-12-11 16:48:45 +00:00
|
|
|
#include "module-zeroconf-publish-symdef.h"
|
|
|
|
|
|
|
|
|
|
PA_MODULE_AUTHOR("Lennart Poettering")
|
|
|
|
|
PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Publisher")
|
|
|
|
|
PA_MODULE_VERSION(PACKAGE_VERSION)
|
2004-12-15 01:02:50 +00:00
|
|
|
PA_MODULE_USAGE("port=<IP port number>")
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
#define SERVICE_TYPE_SINK "_pulse-sink._tcp"
|
|
|
|
|
#define SERVICE_TYPE_SOURCE "_pulse-source._tcp"
|
|
|
|
|
#define SERVICE_TYPE_SERVER "_pulse-server._tcp"
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2004-12-15 01:02:50 +00:00
|
|
|
static const char* const valid_modargs[] = {
|
|
|
|
|
"port",
|
|
|
|
|
NULL
|
|
|
|
|
};
|
|
|
|
|
|
2004-12-11 16:48:45 +00:00
|
|
|
struct service {
|
2006-07-13 15:54:13 +00:00
|
|
|
struct userdata *userdata;
|
|
|
|
|
AvahiEntryGroup *entry_group;
|
|
|
|
|
char *service_name;
|
2004-12-11 16:48:45 +00:00
|
|
|
char *name;
|
2006-07-13 15:54:13 +00:00
|
|
|
enum { UNPUBLISHED, PUBLISHED_REAL, PUBLISHED_AUTOLOAD } published ;
|
2004-12-11 16:48:45 +00:00
|
|
|
|
|
|
|
|
struct {
|
|
|
|
|
int valid;
|
2006-02-22 20:11:56 +00:00
|
|
|
pa_namereg_type_t type;
|
2004-12-11 16:48:45 +00:00
|
|
|
uint32_t index;
|
|
|
|
|
} loaded;
|
|
|
|
|
|
|
|
|
|
struct {
|
|
|
|
|
int valid;
|
2006-02-22 20:11:56 +00:00
|
|
|
pa_namereg_type_t type;
|
2004-12-11 16:48:45 +00:00
|
|
|
uint32_t index;
|
|
|
|
|
} autoload;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct userdata {
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_core *core;
|
2006-07-13 15:54:13 +00:00
|
|
|
AvahiPoll *avahi_poll;
|
|
|
|
|
AvahiClient *client;
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_hashmap *services;
|
|
|
|
|
pa_dynarray *sink_dynarray, *source_dynarray, *autoload_dynarray;
|
|
|
|
|
pa_subscription *subscription;
|
2006-07-13 15:54:13 +00:00
|
|
|
char *service_name;
|
|
|
|
|
|
|
|
|
|
AvahiEntryGroup *main_entry_group;
|
2004-12-15 01:02:50 +00:00
|
|
|
|
|
|
|
|
uint16_t port;
|
2004-12-11 16:48:45 +00:00
|
|
|
};
|
|
|
|
|
|
2006-02-22 20:11:56 +00:00
|
|
|
static void get_service_data(struct userdata *u, struct service *s, pa_sample_spec *ret_ss, char **ret_description) {
|
|
|
|
|
assert(u && s && s->loaded.valid && ret_ss && ret_description);
|
2004-12-11 16:48:45 +00:00
|
|
|
|
|
|
|
|
if (s->loaded.type == PA_NAMEREG_SINK) {
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_sink *sink = pa_idxset_get_by_index(u->core->sinks, s->loaded.index);
|
2004-12-11 16:48:45 +00:00
|
|
|
assert(sink);
|
|
|
|
|
*ret_ss = sink->sample_spec;
|
2004-12-12 22:58:53 +00:00
|
|
|
*ret_description = sink->description;
|
2004-12-11 16:48:45 +00:00
|
|
|
} else if (s->loaded.type == PA_NAMEREG_SOURCE) {
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_source *source = pa_idxset_get_by_index(u->core->sources, s->loaded.index);
|
2004-12-11 16:48:45 +00:00
|
|
|
assert(source);
|
|
|
|
|
*ret_ss = source->sample_spec;
|
2004-12-12 22:58:53 +00:00
|
|
|
*ret_description = source->description;
|
2004-12-11 16:48:45 +00:00
|
|
|
} else
|
|
|
|
|
assert(0);
|
|
|
|
|
}
|
|
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
static AvahiStringList* txt_record_server_data(pa_core *c, AvahiStringList *l) {
|
|
|
|
|
char s[128];
|
2004-12-12 22:58:53 +00:00
|
|
|
assert(c);
|
|
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
l = avahi_string_list_add_pair(l, "server-version", PACKAGE_NAME" "PACKAGE_VERSION);
|
|
|
|
|
l = avahi_string_list_add_pair(l, "user-name", pa_get_user_name(s, sizeof(s)));
|
|
|
|
|
l = avahi_string_list_add_pair(l, "fqdn", pa_get_fqdn(s, sizeof(s)));
|
|
|
|
|
l = avahi_string_list_add_printf(l, "cookie=0x%08x", c->cookie);
|
|
|
|
|
|
|
|
|
|
return l;
|
2004-12-12 22:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
static int publish_service(struct userdata *u, struct service *s);
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
static void service_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
|
|
|
|
|
struct service *s = userdata;
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
if (state == AVAHI_ENTRY_GROUP_COLLISION) {
|
|
|
|
|
char *t;
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
t = avahi_alternative_service_name(s->service_name);
|
|
|
|
|
pa_xfree(s->service_name);
|
|
|
|
|
s->service_name = t;
|
|
|
|
|
|
|
|
|
|
publish_service(s->userdata, s);
|
2004-12-11 16:48:45 +00:00
|
|
|
}
|
2006-07-13 15:54:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int publish_service(struct userdata *u, struct service *s) {
|
|
|
|
|
int r = -1;
|
|
|
|
|
AvahiStringList *txt = NULL;
|
|
|
|
|
|
|
|
|
|
assert(u);
|
|
|
|
|
assert(s);
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
if (!u->client || avahi_client_get_state(u->client) != AVAHI_CLIENT_S_RUNNING)
|
|
|
|
|
return 0;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
if ((s->published == PUBLISHED_REAL && s->loaded.valid) ||
|
|
|
|
|
(s->published == PUBLISHED_AUTOLOAD && s->autoload.valid && !s->loaded.valid))
|
|
|
|
|
return 0;
|
2004-12-12 22:58:53 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
if (s->published != UNPUBLISHED) {
|
|
|
|
|
avahi_entry_group_reset(s->entry_group);
|
|
|
|
|
s->published = UNPUBLISHED;
|
2007-01-04 13:43:45 +00:00
|
|
|
}
|
|
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
if (s->loaded.valid || s->autoload.valid) {
|
|
|
|
|
pa_namereg_type_t type;
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
if (!s->entry_group) {
|
|
|
|
|
if (!(s->entry_group = avahi_entry_group_new(u->client, service_entry_group_callback, s))) {
|
|
|
|
|
pa_log("avahi_entry_group_new(): %s", avahi_strerror(avahi_client_errno(u->client)));
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
txt = avahi_string_list_add_pair(txt, "device", s->name);
|
|
|
|
|
txt = txt_record_server_data(u->core, txt);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
if (s->loaded.valid) {
|
|
|
|
|
char *description;
|
|
|
|
|
pa_sample_spec ss;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
get_service_data(u, s, &ss, &description);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
txt = avahi_string_list_add_printf(txt, "rate=%u", ss.rate);
|
|
|
|
|
txt = avahi_string_list_add_printf(txt, "channels=%u", ss.channels);
|
|
|
|
|
txt = avahi_string_list_add_pair(txt, "format", pa_sample_format_to_string(ss.format));
|
2006-08-11 23:58:55 +00:00
|
|
|
if (description)
|
|
|
|
|
txt = avahi_string_list_add_pair(txt, "description", description);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
type = s->loaded.type;
|
|
|
|
|
} else if (s->autoload.valid)
|
|
|
|
|
type = s->autoload.type;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
if (avahi_entry_group_add_service_strlst(
|
|
|
|
|
s->entry_group,
|
|
|
|
|
AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
|
|
|
|
|
0,
|
|
|
|
|
s->service_name,
|
|
|
|
|
type == PA_NAMEREG_SINK ? SERVICE_TYPE_SINK : SERVICE_TYPE_SOURCE,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
u->port,
|
|
|
|
|
txt) < 0) {
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("avahi_entry_group_add_service_strlst(): %s", avahi_strerror(avahi_client_errno(u->client)));
|
2004-12-11 16:48:45 +00:00
|
|
|
goto finish;
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
if (avahi_entry_group_commit(s->entry_group) < 0) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("avahi_entry_group_commit(): %s", avahi_strerror(avahi_client_errno(u->client)));
|
2004-12-11 16:48:45 +00:00
|
|
|
goto finish;
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
if (s->loaded.valid)
|
|
|
|
|
s->published = PUBLISHED_REAL;
|
|
|
|
|
else if (s->autoload.valid)
|
|
|
|
|
s->published = PUBLISHED_AUTOLOAD;
|
2004-12-11 16:48:45 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-12-11 16:48:45 +00:00
|
|
|
r = 0;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-12-11 16:48:45 +00:00
|
|
|
finish:
|
|
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
if (s->published == UNPUBLISHED) {
|
2004-12-11 16:48:45 +00:00
|
|
|
/* Remove this service */
|
2006-07-13 15:54:13 +00:00
|
|
|
|
2006-08-13 20:44:32 +00:00
|
|
|
if (s->entry_group)
|
2006-07-13 15:54:13 +00:00
|
|
|
avahi_entry_group_free(s->entry_group);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-12-11 16:48:45 +00:00
|
|
|
pa_hashmap_remove(u->services, s->name);
|
|
|
|
|
pa_xfree(s->name);
|
2006-07-13 15:54:13 +00:00
|
|
|
pa_xfree(s->service_name);
|
2004-12-11 16:48:45 +00:00
|
|
|
pa_xfree(s);
|
|
|
|
|
}
|
|
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
if (txt)
|
|
|
|
|
avahi_string_list_free(txt);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-12-11 16:48:45 +00:00
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-15 18:15:00 +00:00
|
|
|
static struct service *get_service(struct userdata *u, const char *name, const char *description) {
|
2004-12-11 16:48:45 +00:00
|
|
|
struct service *s;
|
2006-07-13 15:54:13 +00:00
|
|
|
char hn[64];
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-12-11 16:48:45 +00:00
|
|
|
if ((s = pa_hashmap_get(u->services, name)))
|
|
|
|
|
return s;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
s = pa_xnew(struct service, 1);
|
|
|
|
|
s->userdata = u;
|
|
|
|
|
s->entry_group = NULL;
|
|
|
|
|
s->published = UNPUBLISHED;
|
2004-12-11 16:48:45 +00:00
|
|
|
s->name = pa_xstrdup(name);
|
|
|
|
|
s->loaded.valid = s->autoload.valid = 0;
|
2006-08-15 18:15:00 +00:00
|
|
|
s->service_name = pa_sprintf_malloc("%s on %s", description ? description : s->name, pa_get_host_name(hn, sizeof(hn)));
|
2004-12-11 16:48:45 +00:00
|
|
|
|
|
|
|
|
pa_hashmap_put(u->services, s->name, s);
|
|
|
|
|
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static int publish_sink(struct userdata *u, pa_sink *s) {
|
2004-12-11 16:48:45 +00:00
|
|
|
struct service *svc;
|
2006-08-13 20:44:32 +00:00
|
|
|
int ret;
|
2004-12-11 16:48:45 +00:00
|
|
|
assert(u && s);
|
|
|
|
|
|
2006-08-15 18:15:00 +00:00
|
|
|
svc = get_service(u, s->name, s->description);
|
2004-12-11 16:48:45 +00:00
|
|
|
if (svc->loaded.valid)
|
2006-07-13 15:54:13 +00:00
|
|
|
return publish_service(u, svc);
|
2004-12-11 16:48:45 +00:00
|
|
|
|
|
|
|
|
svc->loaded.valid = 1;
|
|
|
|
|
svc->loaded.type = PA_NAMEREG_SINK;
|
|
|
|
|
svc->loaded.index = s->index;
|
|
|
|
|
|
2006-08-13 20:44:32 +00:00
|
|
|
if ((ret = publish_service(u, svc)) < 0)
|
|
|
|
|
return ret;
|
2004-12-12 15:56:22 +00:00
|
|
|
|
2006-08-13 20:44:32 +00:00
|
|
|
pa_dynarray_put(u->sink_dynarray, s->index, svc);
|
|
|
|
|
return ret;
|
2004-12-11 16:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static int publish_source(struct userdata *u, pa_source *s) {
|
2004-12-11 16:48:45 +00:00
|
|
|
struct service *svc;
|
2006-08-13 20:44:32 +00:00
|
|
|
int ret;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-12-11 16:48:45 +00:00
|
|
|
assert(u && s);
|
|
|
|
|
|
2006-08-15 18:15:00 +00:00
|
|
|
svc = get_service(u, s->name, s->description);
|
2004-12-11 16:48:45 +00:00
|
|
|
if (svc->loaded.valid)
|
2006-07-13 15:54:13 +00:00
|
|
|
return publish_service(u, svc);
|
2004-12-11 16:48:45 +00:00
|
|
|
|
|
|
|
|
svc->loaded.valid = 1;
|
|
|
|
|
svc->loaded.type = PA_NAMEREG_SOURCE;
|
|
|
|
|
svc->loaded.index = s->index;
|
2004-12-12 15:56:22 +00:00
|
|
|
|
|
|
|
|
pa_dynarray_put(u->source_dynarray, s->index, svc);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-08-13 20:44:32 +00:00
|
|
|
if ((ret = publish_service(u, svc)) < 0)
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
|
pa_dynarray_put(u->sink_dynarray, s->index, svc);
|
|
|
|
|
return ret;
|
2004-12-11 16:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static int publish_autoload(struct userdata *u, pa_autoload_entry *s) {
|
2004-12-11 16:48:45 +00:00
|
|
|
struct service *svc;
|
2006-08-13 20:44:32 +00:00
|
|
|
int ret;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-12-11 16:48:45 +00:00
|
|
|
assert(u && s);
|
|
|
|
|
|
2006-08-15 18:15:00 +00:00
|
|
|
svc = get_service(u, s->name, NULL);
|
2004-12-11 16:48:45 +00:00
|
|
|
if (svc->autoload.valid)
|
2006-07-13 15:54:13 +00:00
|
|
|
return publish_service(u, svc);
|
2004-12-11 16:48:45 +00:00
|
|
|
|
|
|
|
|
svc->autoload.valid = 1;
|
|
|
|
|
svc->autoload.type = s->type;
|
|
|
|
|
svc->autoload.index = s->index;
|
2004-12-12 15:56:22 +00:00
|
|
|
|
2006-08-13 20:44:32 +00:00
|
|
|
if ((ret = publish_service(u, svc)) < 0)
|
|
|
|
|
return ret;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-08-13 20:44:32 +00:00
|
|
|
pa_dynarray_put(u->autoload_dynarray, s->index, svc);
|
|
|
|
|
return ret;
|
2004-12-11 16:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
2006-02-22 20:11:56 +00:00
|
|
|
static int remove_sink(struct userdata *u, uint32_t idx) {
|
2004-12-11 16:48:45 +00:00
|
|
|
struct service *svc;
|
2006-02-22 20:11:56 +00:00
|
|
|
assert(u && idx != PA_INVALID_INDEX);
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2006-02-22 20:11:56 +00:00
|
|
|
if (!(svc = pa_dynarray_get(u->sink_dynarray, idx)))
|
2004-12-11 16:48:45 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (!svc->loaded.valid || svc->loaded.type != PA_NAMEREG_SINK)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
svc->loaded.valid = 0;
|
2006-02-22 20:11:56 +00:00
|
|
|
pa_dynarray_put(u->sink_dynarray, idx, NULL);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-12-11 16:48:45 +00:00
|
|
|
return publish_service(u, svc);
|
|
|
|
|
}
|
|
|
|
|
|
2006-02-22 20:11:56 +00:00
|
|
|
static int remove_source(struct userdata *u, uint32_t idx) {
|
2004-12-11 16:48:45 +00:00
|
|
|
struct service *svc;
|
2006-02-22 20:11:56 +00:00
|
|
|
assert(u && idx != PA_INVALID_INDEX);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-22 20:11:56 +00:00
|
|
|
if (!(svc = pa_dynarray_get(u->source_dynarray, idx)))
|
2004-12-11 16:48:45 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (!svc->loaded.valid || svc->loaded.type != PA_NAMEREG_SOURCE)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
svc->loaded.valid = 0;
|
2006-02-22 20:11:56 +00:00
|
|
|
pa_dynarray_put(u->source_dynarray, idx, NULL);
|
2004-12-12 15:56:22 +00:00
|
|
|
|
2004-12-11 16:48:45 +00:00
|
|
|
return publish_service(u, svc);
|
|
|
|
|
}
|
|
|
|
|
|
2006-02-22 20:11:56 +00:00
|
|
|
static int remove_autoload(struct userdata *u, uint32_t idx) {
|
2004-12-11 16:48:45 +00:00
|
|
|
struct service *svc;
|
2006-02-22 20:11:56 +00:00
|
|
|
assert(u && idx != PA_INVALID_INDEX);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-22 20:11:56 +00:00
|
|
|
if (!(svc = pa_dynarray_get(u->autoload_dynarray, idx)))
|
2004-12-11 16:48:45 +00:00
|
|
|
return 0;
|
|
|
|
|
|
2004-12-12 15:56:22 +00:00
|
|
|
if (!svc->autoload.valid)
|
2004-12-11 16:48:45 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
svc->autoload.valid = 0;
|
2006-02-22 20:11:56 +00:00
|
|
|
pa_dynarray_put(u->autoload_dynarray, idx, NULL);
|
2004-12-12 15:56:22 +00:00
|
|
|
|
2004-12-11 16:48:45 +00:00
|
|
|
return publish_service(u, svc);
|
|
|
|
|
}
|
|
|
|
|
|
2006-02-22 20:11:56 +00:00
|
|
|
static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
|
2004-12-11 16:48:45 +00:00
|
|
|
struct userdata *u = userdata;
|
|
|
|
|
assert(u && c);
|
|
|
|
|
|
2004-12-12 15:56:22 +00:00
|
|
|
switch (t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK)
|
2004-12-11 16:48:45 +00:00
|
|
|
case PA_SUBSCRIPTION_EVENT_SINK: {
|
2004-12-12 15:56:22 +00:00
|
|
|
if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_sink *sink;
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2006-02-22 20:11:56 +00:00
|
|
|
if ((sink = pa_idxset_get_by_index(c->sinks, idx))) {
|
2004-12-11 16:48:45 +00:00
|
|
|
if (publish_sink(u, sink) < 0)
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
2004-12-12 15:56:22 +00:00
|
|
|
} else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
|
2006-02-22 20:11:56 +00:00
|
|
|
if (remove_sink(u, idx) < 0)
|
2004-12-12 15:56:22 +00:00
|
|
|
goto fail;
|
2004-12-11 16:48:45 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-12-11 16:48:45 +00:00
|
|
|
break;
|
|
|
|
|
|
2004-12-12 15:56:22 +00:00
|
|
|
case PA_SUBSCRIPTION_EVENT_SOURCE:
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2004-12-12 15:56:22 +00:00
|
|
|
if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_source *source;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-22 20:11:56 +00:00
|
|
|
if ((source = pa_idxset_get_by_index(c->sources, idx))) {
|
2004-12-11 16:48:45 +00:00
|
|
|
if (publish_source(u, source) < 0)
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
2004-12-12 15:56:22 +00:00
|
|
|
} else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
|
2006-02-22 20:11:56 +00:00
|
|
|
if (remove_source(u, idx) < 0)
|
2004-12-12 15:56:22 +00:00
|
|
|
goto fail;
|
2004-12-11 16:48:45 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-12-11 16:48:45 +00:00
|
|
|
break;
|
|
|
|
|
|
2004-12-12 15:56:22 +00:00
|
|
|
case PA_SUBSCRIPTION_EVENT_AUTOLOAD:
|
|
|
|
|
if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_autoload_entry *autoload;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-02-22 20:11:56 +00:00
|
|
|
if ((autoload = pa_idxset_get_by_index(c->autoload_idxset, idx))) {
|
2004-12-11 16:48:45 +00:00
|
|
|
if (publish_autoload(u, autoload) < 0)
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
2004-12-12 15:56:22 +00:00
|
|
|
} else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
|
2006-02-22 20:11:56 +00:00
|
|
|
if (remove_autoload(u, idx) < 0)
|
2004-12-12 15:56:22 +00:00
|
|
|
goto fail;
|
2004-12-11 16:48:45 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-12-11 16:48:45 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
if (u->subscription) {
|
|
|
|
|
pa_subscription_free(u->subscription);
|
|
|
|
|
u->subscription = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
static int publish_main_service(struct userdata *u);
|
|
|
|
|
|
|
|
|
|
static void main_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
|
|
|
|
|
struct userdata *u = userdata;
|
|
|
|
|
assert(u);
|
|
|
|
|
|
|
|
|
|
if (state == AVAHI_ENTRY_GROUP_COLLISION) {
|
|
|
|
|
char *t;
|
|
|
|
|
|
|
|
|
|
t = avahi_alternative_service_name(u->service_name);
|
|
|
|
|
pa_xfree(u->service_name);
|
|
|
|
|
u->service_name = t;
|
|
|
|
|
|
|
|
|
|
publish_main_service(u);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int publish_main_service(struct userdata *u) {
|
|
|
|
|
AvahiStringList *txt = NULL;
|
|
|
|
|
int r = -1;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
if (!u->main_entry_group) {
|
|
|
|
|
if (!(u->main_entry_group = avahi_entry_group_new(u->client, main_entry_group_callback, u))) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("avahi_entry_group_new() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
|
2006-07-13 15:54:13 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
} else
|
|
|
|
|
avahi_entry_group_reset(u->main_entry_group);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
txt = txt_record_server_data(u->core, NULL);
|
|
|
|
|
|
|
|
|
|
if (avahi_entry_group_add_service_strlst(
|
|
|
|
|
u->main_entry_group,
|
|
|
|
|
AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
|
|
|
|
|
0,
|
|
|
|
|
u->service_name,
|
|
|
|
|
SERVICE_TYPE_SERVER,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
|
|
|
|
u->port,
|
|
|
|
|
txt) < 0) {
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("avahi_entry_group_add_service_strlst() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
|
2006-07-13 15:54:13 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
if (avahi_entry_group_commit(u->main_entry_group) < 0) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("avahi_entry_group_commit() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
|
2006-07-13 15:54:13 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r = 0;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
fail:
|
|
|
|
|
avahi_string_list_free(txt);
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int publish_all_services(struct userdata *u) {
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_sink *sink;
|
|
|
|
|
pa_source *source;
|
|
|
|
|
pa_autoload_entry *autoload;
|
2006-07-13 15:54:13 +00:00
|
|
|
int r = -1;
|
|
|
|
|
uint32_t idx;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
assert(u);
|
|
|
|
|
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log_debug("Publishing services in Zeroconf");
|
2006-07-13 15:54:13 +00:00
|
|
|
|
|
|
|
|
for (sink = pa_idxset_first(u->core->sinks, &idx); sink; sink = pa_idxset_next(u->core->sinks, &idx))
|
|
|
|
|
if (publish_sink(u, sink) < 0)
|
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
for (source = pa_idxset_first(u->core->sources, &idx); source; source = pa_idxset_next(u->core->sources, &idx))
|
|
|
|
|
if (publish_source(u, source) < 0)
|
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
if (u->core->autoload_idxset)
|
|
|
|
|
for (autoload = pa_idxset_first(u->core->autoload_idxset, &idx); autoload; autoload = pa_idxset_next(u->core->autoload_idxset, &idx))
|
|
|
|
|
if (publish_autoload(u, autoload) < 0)
|
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
if (publish_main_service(u) < 0)
|
|
|
|
|
goto fail;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
r = 0;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
fail:
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void unpublish_all_services(struct userdata *u, int rem) {
|
|
|
|
|
void *state = NULL;
|
|
|
|
|
struct service *s;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
assert(u);
|
|
|
|
|
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log_debug("Unpublishing services in Zeroconf");
|
2006-07-13 15:54:13 +00:00
|
|
|
|
|
|
|
|
while ((s = pa_hashmap_iterate(u->services, &state, NULL))) {
|
|
|
|
|
if (s->entry_group) {
|
|
|
|
|
if (rem) {
|
|
|
|
|
avahi_entry_group_free(s->entry_group);
|
|
|
|
|
s->entry_group = NULL;
|
2007-01-04 13:43:45 +00:00
|
|
|
} else
|
2006-07-13 15:54:13 +00:00
|
|
|
avahi_entry_group_reset(s->entry_group);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s->published = UNPUBLISHED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (u->main_entry_group) {
|
|
|
|
|
if (rem) {
|
|
|
|
|
avahi_entry_group_free(u->main_entry_group);
|
|
|
|
|
u->main_entry_group = NULL;
|
|
|
|
|
} else
|
|
|
|
|
avahi_entry_group_reset(u->main_entry_group);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void client_callback(AvahiClient *c, AvahiClientState state, void *userdata) {
|
|
|
|
|
struct userdata *u = userdata;
|
|
|
|
|
assert(c);
|
|
|
|
|
|
|
|
|
|
u->client = c;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
switch (state) {
|
|
|
|
|
case AVAHI_CLIENT_S_RUNNING:
|
|
|
|
|
publish_all_services(u);
|
|
|
|
|
break;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
case AVAHI_CLIENT_S_COLLISION:
|
|
|
|
|
unpublish_all_services(u, 0);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case AVAHI_CLIENT_FAILURE:
|
|
|
|
|
if (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
|
|
|
|
|
int error;
|
|
|
|
|
unpublish_all_services(u, 1);
|
|
|
|
|
avahi_client_free(u->client);
|
|
|
|
|
|
|
|
|
|
if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error)))
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("pa_avahi_client_new() failed: %s", avahi_strerror(error));
|
2006-07-13 15:54:13 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default: ;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pa__init(pa_core *c, pa_module*m) {
|
|
|
|
|
struct userdata *u;
|
|
|
|
|
uint32_t port = PA_NATIVE_DEFAULT_PORT;
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_modargs *ma = NULL;
|
2006-07-13 15:54:13 +00:00
|
|
|
char hn[256];
|
|
|
|
|
int error;
|
2004-12-15 01:02:50 +00:00
|
|
|
|
|
|
|
|
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("failed to parse module arguments.");
|
2004-12-15 01:02:50 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port == 0 || port >= 0xFFFF) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("invalid port specified.");
|
2004-12-15 01:02:50 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
m->userdata = u = pa_xnew(struct userdata, 1);
|
2004-12-11 16:48:45 +00:00
|
|
|
u->core = c;
|
2004-12-15 01:02:50 +00:00
|
|
|
u->port = (uint16_t) port;
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
u->avahi_poll = pa_avahi_poll_new(c->mainloop);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-12-11 16:48:45 +00:00
|
|
|
u->services = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
|
2004-12-12 15:56:22 +00:00
|
|
|
u->sink_dynarray = pa_dynarray_new();
|
|
|
|
|
u->source_dynarray = pa_dynarray_new();
|
|
|
|
|
u->autoload_dynarray = pa_dynarray_new();
|
2006-07-13 15:54:13 +00:00
|
|
|
|
2004-12-11 16:48:45 +00:00
|
|
|
u->subscription = pa_subscription_new(c,
|
|
|
|
|
PA_SUBSCRIPTION_MASK_SINK|
|
|
|
|
|
PA_SUBSCRIPTION_MASK_SOURCE|
|
|
|
|
|
PA_SUBSCRIPTION_MASK_AUTOLOAD, subscribe_callback, u);
|
|
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
u->main_entry_group = NULL;
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
u->service_name = pa_xstrdup(pa_get_host_name(hn, sizeof(hn)));
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("pa_avahi_client_new() failed: %s", avahi_strerror(error));
|
2004-12-15 01:02:50 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_modargs_free(ma);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-12-11 16:48:45 +00:00
|
|
|
return 0;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-12-11 16:48:45 +00:00
|
|
|
fail:
|
|
|
|
|
pa__done(c, m);
|
2004-12-15 01:02:50 +00:00
|
|
|
|
|
|
|
|
if (ma)
|
|
|
|
|
pa_modargs_free(ma);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-12-11 16:48:45 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void service_free(void *p, void *userdata) {
|
|
|
|
|
struct service *s = p;
|
|
|
|
|
struct userdata *u = userdata;
|
2006-07-13 15:54:13 +00:00
|
|
|
|
|
|
|
|
assert(s);
|
|
|
|
|
assert(u);
|
|
|
|
|
|
|
|
|
|
if (s->entry_group)
|
|
|
|
|
avahi_entry_group_free(s->entry_group);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
pa_xfree(s->service_name);
|
2004-12-11 16:48:45 +00:00
|
|
|
pa_xfree(s->name);
|
|
|
|
|
pa_xfree(s);
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa__done(pa_core *c, pa_module*m) {
|
2004-12-11 16:48:45 +00:00
|
|
|
struct userdata*u;
|
|
|
|
|
assert(c && m);
|
|
|
|
|
|
|
|
|
|
if (!(u = m->userdata))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (u->services)
|
|
|
|
|
pa_hashmap_free(u->services, service_free, u);
|
|
|
|
|
|
2006-08-13 20:44:32 +00:00
|
|
|
if (u->subscription)
|
|
|
|
|
pa_subscription_free(u->subscription);
|
|
|
|
|
|
2004-12-12 15:56:22 +00:00
|
|
|
if (u->sink_dynarray)
|
|
|
|
|
pa_dynarray_free(u->sink_dynarray, NULL, NULL);
|
|
|
|
|
if (u->source_dynarray)
|
|
|
|
|
pa_dynarray_free(u->source_dynarray, NULL, NULL);
|
|
|
|
|
if (u->autoload_dynarray)
|
|
|
|
|
pa_dynarray_free(u->autoload_dynarray, NULL, NULL);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-12-12 15:56:22 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
if (u->main_entry_group)
|
|
|
|
|
avahi_entry_group_free(u->main_entry_group);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
if (u->client)
|
|
|
|
|
avahi_client_free(u->client);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
if (u->avahi_poll)
|
|
|
|
|
pa_avahi_poll_free(u->avahi_poll);
|
|
|
|
|
|
|
|
|
|
pa_xfree(u->service_name);
|
2004-12-11 16:48:45 +00:00
|
|
|
pa_xfree(u);
|
|
|
|
|
}
|
|
|
|
|
|