-/* $Id$ */
-
/***
- This file is part of polypaudio.
-
- polypaudio is free software; you can redistribute it and/or modify
+ This file is part of PulseAudio.
+
+ Copyright 2004-2006 Lennart Poettering
+
+ PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
- published by the Free Software Foundation; either version 2 of the
+ published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
-
- polypaudio is distributed in the hope that it will be useful, but
+
+ PulseAudio is distributed in the hope that it will be useful, but
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.
-
+
You should have received a copy of the GNU Lesser General Public
- License along with polypaudio; if not, write to the Free Software
+ License along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.
***/
#endif
#include <stdio.h>
-#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#include <polyp/xmalloc.h>
-
-#include <polypcore/autoload.h>
-#include <polypcore/sink.h>
-#include <polypcore/source.h>
-#include <polypcore/native-common.h>
-#include <polypcore/core-util.h>
-#include <polypcore/log.h>
-#include <polypcore/core-subscribe.h>
-#include <polypcore/dynarray.h>
-#include <polypcore/modargs.h>
-
-#include "../polypcore/endianmacros.h"
-
-#include "howl-wrap.h"
+#include <avahi-client/client.h>
+#include <avahi-client/publish.h>
+#include <avahi-common/alternative.h>
+#include <avahi-common/error.h>
+#include <avahi-common/domain.h>
+
+#include <pulse/xmalloc.h>
+#include <pulse/util.h>
+
+#include <pulsecore/parseaddr.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>
+#include <pulsecore/avahi-wrap.h>
+#include <pulsecore/endianmacros.h>
+#include <pulsecore/protocol-native.h>
#include "module-zeroconf-publish-symdef.h"
-PA_MODULE_AUTHOR("Lennart Poettering")
-PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Publisher")
-PA_MODULE_VERSION(PACKAGE_VERSION)
-PA_MODULE_USAGE("port=<IP port number>")
-
-#define SERVICE_NAME_SINK "_polypaudio-sink._tcp"
-#define SERVICE_NAME_SOURCE "_polypaudio-source._tcp"
-#define SERVICE_NAME_SERVER "_polypaudio-server._tcp"
+PA_MODULE_AUTHOR("Lennart Poettering");
+PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Publisher");
+PA_MODULE_VERSION(PACKAGE_VERSION);
+PA_MODULE_LOAD_ONCE(TRUE);
+
+#define SERVICE_TYPE_SINK "_pulse-sink._tcp"
+#define SERVICE_TYPE_SOURCE "_pulse-source._tcp"
+#define SERVICE_TYPE_SERVER "_pulse-server._tcp"
+#define SERVICE_SUBTYPE_SINK_HARDWARE "_hardware._sub."SERVICE_TYPE_SINK
+#define SERVICE_SUBTYPE_SINK_VIRTUAL "_virtual._sub."SERVICE_TYPE_SINK
+#define SERVICE_SUBTYPE_SOURCE_HARDWARE "_hardware._sub."SERVICE_TYPE_SOURCE
+#define SERVICE_SUBTYPE_SOURCE_VIRTUAL "_virtual._sub."SERVICE_TYPE_SOURCE
+#define SERVICE_SUBTYPE_SOURCE_MONITOR "_monitor._sub."SERVICE_TYPE_SOURCE
+#define SERVICE_SUBTYPE_SOURCE_NON_MONITOR "_non-monitor._sub."SERVICE_TYPE_SOURCE
static const char* const valid_modargs[] = {
- "port",
NULL
};
+enum service_subtype {
+ SUBTYPE_HARDWARE,
+ SUBTYPE_VIRTUAL,
+ SUBTYPE_MONITOR
+};
+
struct service {
- sw_discovery_oid oid;
- char *name;
- int published; /* 0 -> not yet registered, 1 -> registered with data from real device, 2 -> registered with data from autoload device */
-
- struct {
- int valid;
- pa_namereg_type_t type;
- uint32_t index;
- } loaded;
-
- struct {
- int valid;
- pa_namereg_type_t type;
- uint32_t index;
- } autoload;
+ struct userdata *userdata;
+ AvahiEntryGroup *entry_group;
+ char *service_name;
+ pa_object *device;
+ enum service_subtype subtype;
};
struct userdata {
pa_core *core;
- pa_howl_wrapper *howl_wrapper;
+ pa_module *module;
+
+ AvahiPoll *avahi_poll;
+ AvahiClient *client;
+
pa_hashmap *services;
- pa_dynarray *sink_dynarray, *source_dynarray, *autoload_dynarray;
- pa_subscription *subscription;
+ char *service_name;
+
+ AvahiEntryGroup *main_entry_group;
+
+ pa_hook_slot *sink_new_slot, *source_new_slot, *sink_unlink_slot, *source_unlink_slot, *sink_changed_slot, *source_changed_slot;
- uint16_t port;
- sw_discovery_oid server_oid;
+ pa_native_protocol *native;
};
-static sw_result publish_reply(sw_discovery discovery, sw_discovery_publish_status status, sw_discovery_oid oid, sw_opaque extra) {
- return SW_OKAY;
-}
+static void get_service_data(struct service *s, pa_sample_spec *ret_ss, pa_channel_map *ret_map, const char **ret_name, pa_proplist **ret_proplist, enum service_subtype *ret_subtype) {
+ pa_assert(s);
+ pa_assert(ret_ss);
+ pa_assert(ret_proplist);
+ pa_assert(ret_subtype);
-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);
+ if (pa_sink_isinstance(s->device)) {
+ pa_sink *sink = PA_SINK(s->device);
- if (s->loaded.type == PA_NAMEREG_SINK) {
- pa_sink *sink = pa_idxset_get_by_index(u->core->sinks, s->loaded.index);
- assert(sink);
*ret_ss = sink->sample_spec;
- *ret_description = sink->description;
- } else if (s->loaded.type == PA_NAMEREG_SOURCE) {
- pa_source *source = pa_idxset_get_by_index(u->core->sources, s->loaded.index);
- assert(source);
+ *ret_map = sink->channel_map;
+ *ret_name = sink->name;
+ *ret_proplist = sink->proplist;
+ *ret_subtype = sink->flags & PA_SINK_HARDWARE ? SUBTYPE_HARDWARE : SUBTYPE_VIRTUAL;
+
+ } else if (pa_source_isinstance(s->device)) {
+ pa_source *source = PA_SOURCE(s->device);
+
*ret_ss = source->sample_spec;
- *ret_description = source->description;
+ *ret_map = source->channel_map;
+ *ret_name = source->name;
+ *ret_proplist = source->proplist;
+ *ret_subtype = source->monitor_of ? SUBTYPE_MONITOR : (source->flags & PA_SOURCE_HARDWARE ? SUBTYPE_HARDWARE : SUBTYPE_VIRTUAL);
+
} else
- assert(0);
+ pa_assert_not_reached();
}
-static void txt_record_server_data(pa_core *c, sw_text_record t) {
- char s[256];
- assert(c);
+static AvahiStringList* txt_record_server_data(pa_core *c, AvahiStringList *l) {
+ char s[128];
+ char *t;
+
+ pa_assert(c);
+
+ l = avahi_string_list_add_pair(l, "server-version", PACKAGE_NAME" "PACKAGE_VERSION);
+
+ t = pa_get_user_name_malloc();
+ l = avahi_string_list_add_pair(l, "user-name", t);
+ pa_xfree(t);
+
+ t = pa_machine_id();
+ l = avahi_string_list_add_pair(l, "machine-id", t);
+ pa_xfree(t);
- sw_text_record_add_key_and_string_value(t, "server-version", PACKAGE_NAME" "PACKAGE_VERSION);
- sw_text_record_add_key_and_string_value(t, "user-name", pa_get_user_name(s, sizeof(s)));
- sw_text_record_add_key_and_string_value(t, "fqdn", pa_get_fqdn(s, sizeof(s)));
- snprintf(s, sizeof(s), "0x%08x", c->cookie);
- sw_text_record_add_key_and_string_value(t, "cookie", s);
+ t = pa_uname_string();
+ l = avahi_string_list_add_pair(l, "uname", t);
+ pa_xfree(t);
+
+ 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;
}
-static int publish_service(struct userdata *u, struct service *s) {
- char t[256];
- char hn[256];
- int r = -1;
- sw_text_record txt;
- int free_txt = 0;
- assert(u && s);
-
- if ((s->published == 1 && s->loaded.valid) ||
- (s->published == 2 && s->autoload.valid && !s->loaded.valid))
- return 0;
+static int publish_service(struct service *s);
+
+static void service_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
+ struct service *s = userdata;
+
+ pa_assert(s);
+
+ switch (state) {
+
+ case AVAHI_ENTRY_GROUP_ESTABLISHED:
+ pa_log_info("Successfully established service %s.", s->service_name);
+ break;
+
+ case AVAHI_ENTRY_GROUP_COLLISION: {
+ char *t;
+
+ t = avahi_alternative_service_name(s->service_name);
+ pa_log_info("Name collision, renaming %s to %s.", s->service_name, t);
+ pa_xfree(s->service_name);
+ s->service_name = t;
- if (s->published) {
- sw_discovery_cancel(pa_howl_wrapper_get_discovery(u->howl_wrapper), s->oid);
- s->published = 0;
+ publish_service(s);
+ break;
+ }
+
+ case AVAHI_ENTRY_GROUP_FAILURE: {
+ pa_log("Failed to register service: %s", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
+
+ avahi_entry_group_free(g);
+ s->entry_group = NULL;
+
+ break;
+ }
+
+ case AVAHI_ENTRY_GROUP_UNCOMMITED:
+ case AVAHI_ENTRY_GROUP_REGISTERING:
+ ;
}
+}
- snprintf(t, sizeof(t), "Networked Audio Device %s on %s", s->name, pa_get_host_name(hn, sizeof(hn)));
+static void service_free(struct service *s);
- if (sw_text_record_init(&txt) != SW_OKAY) {
- pa_log(__FILE__": sw_text_record_init() failed");
- goto finish;
+static uint16_t compute_port(struct userdata *u) {
+ pa_strlist *i;
+
+ pa_assert(u);
+
+ for (i = pa_native_protocol_servers(u->native); i; i = pa_strlist_next(i)) {
+ pa_parsed_address a;
+
+ if (pa_parse_address(pa_strlist_data(i), &a) >= 0 &&
+ (a.type == PA_PARSED_ADDRESS_TCP4 ||
+ a.type == PA_PARSED_ADDRESS_TCP6 ||
+ a.type == PA_PARSED_ADDRESS_TCP_AUTO) &&
+ a.port > 0) {
+
+ pa_xfree(a.path_or_host);
+ return a.port;
+ }
+
+ pa_xfree(a.path_or_host);
}
- free_txt = 1;
-
- sw_text_record_add_key_and_string_value(txt, "device", s->name);
-
- txt_record_server_data(u->core, txt);
-
- if (s->loaded.valid) {
- char z[64], *description;
- pa_sample_spec ss;
-
- get_service_data(u, s, &ss, &description);
-
- snprintf(z, sizeof(z), "%u", ss.rate);
- sw_text_record_add_key_and_string_value(txt, "rate", z);
- snprintf(z, sizeof(z), "%u", ss.channels);
- sw_text_record_add_key_and_string_value(txt, "channels", z);
- sw_text_record_add_key_and_string_value(txt, "format", pa_sample_format_to_string(ss.format));
-
- sw_text_record_add_key_and_string_value(txt, "description", description);
-
- if (sw_discovery_publish(pa_howl_wrapper_get_discovery(u->howl_wrapper), 0, t,
- s->loaded.type == PA_NAMEREG_SINK ? SERVICE_NAME_SINK : SERVICE_NAME_SOURCE,
- NULL, NULL, u->port, sw_text_record_bytes(txt), sw_text_record_len(txt),
- publish_reply, s, &s->oid) != SW_OKAY) {
- pa_log(__FILE__": failed to register sink on zeroconf.");
+
+ return PA_NATIVE_DEFAULT_PORT;
+}
+
+static int publish_service(struct service *s) {
+ int r = -1;
+ AvahiStringList *txt = NULL;
+ const char *name = NULL, *t;
+ pa_proplist *proplist = NULL;
+ pa_sample_spec ss;
+ pa_channel_map map;
+ char cm[PA_CHANNEL_MAP_SNPRINT_MAX];
+ enum service_subtype subtype;
+
+ const char * const subtype_text[] = {
+ [SUBTYPE_HARDWARE] = "hardware",
+ [SUBTYPE_VIRTUAL] = "virtual",
+ [SUBTYPE_MONITOR] = "monitor"
+ };
+
+ pa_assert(s);
+
+ if (!s->userdata->client || avahi_client_get_state(s->userdata->client) != AVAHI_CLIENT_S_RUNNING)
+ return 0;
+
+ if (!s->entry_group) {
+ if (!(s->entry_group = avahi_entry_group_new(s->userdata->client, service_entry_group_callback, s))) {
+ pa_log("avahi_entry_group_new(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
goto finish;
}
+ } else
+ avahi_entry_group_reset(s->entry_group);
+
+ txt = txt_record_server_data(s->userdata->core, txt);
+
+ get_service_data(s, &ss, &map, &name, &proplist, &subtype);
+ txt = avahi_string_list_add_pair(txt, "device", name);
+ 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));
+ txt = avahi_string_list_add_pair(txt, "channel_map", pa_channel_map_snprint(cm, sizeof(cm), &map));
+ txt = avahi_string_list_add_pair(txt, "subtype", subtype_text[subtype]);
+
+ if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_DESCRIPTION)))
+ txt = avahi_string_list_add_pair(txt, "description", t);
+ if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_ICON_NAME)))
+ txt = avahi_string_list_add_pair(txt, "icon-name", t);
+ if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_VENDOR_NAME)))
+ txt = avahi_string_list_add_pair(txt, "vendor-name", t);
+ if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_PRODUCT_NAME)))
+ txt = avahi_string_list_add_pair(txt, "product-name", t);
+ if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_CLASS)))
+ txt = avahi_string_list_add_pair(txt, "class", t);
+ if ((t = pa_proplist_gets(proplist, PA_PROP_DEVICE_FORM_FACTOR)))
+ txt = avahi_string_list_add_pair(txt, "form-factor", t);
+
+ if (avahi_entry_group_add_service_strlst(
+ s->entry_group,
+ AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
+ 0,
+ s->service_name,
+ pa_sink_isinstance(s->device) ? SERVICE_TYPE_SINK : SERVICE_TYPE_SOURCE,
+ NULL,
+ NULL,
+ compute_port(s->userdata),
+ txt) < 0) {
+
+ pa_log("avahi_entry_group_add_service_strlst(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
+ goto finish;
+ }
- s->published = 1;
- } else if (s->autoload.valid) {
+ if (avahi_entry_group_add_service_subtype(
+ s->entry_group,
+ AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
+ 0,
+ s->service_name,
+ pa_sink_isinstance(s->device) ? SERVICE_TYPE_SINK : SERVICE_TYPE_SOURCE,
+ NULL,
+ pa_sink_isinstance(s->device) ? (subtype == SUBTYPE_HARDWARE ? SERVICE_SUBTYPE_SINK_HARDWARE : SERVICE_SUBTYPE_SINK_VIRTUAL) :
+ (subtype == SUBTYPE_HARDWARE ? SERVICE_SUBTYPE_SOURCE_HARDWARE : (subtype == SUBTYPE_VIRTUAL ? SERVICE_SUBTYPE_SOURCE_VIRTUAL : SERVICE_SUBTYPE_SOURCE_MONITOR))) < 0) {
+
+ pa_log("avahi_entry_group_add_service_subtype(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
+ goto finish;
+ }
- if (sw_discovery_publish(pa_howl_wrapper_get_discovery(u->howl_wrapper), 0, t,
- s->autoload.type == PA_NAMEREG_SINK ? SERVICE_NAME_SINK : SERVICE_NAME_SOURCE,
- NULL, NULL, u->port, sw_text_record_bytes(txt), sw_text_record_len(txt),
- publish_reply, s, &s->oid) != SW_OKAY) {
- pa_log(__FILE__": failed to register sink on zeroconf.");
+ if (pa_source_isinstance(s->device) && subtype != SUBTYPE_MONITOR) {
+ if (avahi_entry_group_add_service_subtype(
+ s->entry_group,
+ AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
+ 0,
+ s->service_name,
+ SERVICE_TYPE_SOURCE,
+ NULL,
+ SERVICE_SUBTYPE_SOURCE_NON_MONITOR) < 0) {
+
+ pa_log("avahi_entry_group_add_service_subtype(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
goto finish;
}
+ }
- s->published = 2;
+ if (avahi_entry_group_commit(s->entry_group) < 0) {
+ pa_log("avahi_entry_group_commit(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
+ goto finish;
}
r = 0;
-
+ pa_log_debug("Successfully created entry group for %s.", s->service_name);
+
finish:
- if (!s->published) {
- /* Remove this service */
- pa_hashmap_remove(u->services, s->name);
- pa_xfree(s->name);
- pa_xfree(s);
- }
+ /* Remove this service */
+ if (r < 0)
+ service_free(s);
+
+ avahi_string_list_free(txt);
- if (free_txt)
- sw_text_record_fina(txt);
-
return r;
}
-static struct service *get_service(struct userdata *u, const char *name) {
+static struct service *get_service(struct userdata *u, pa_object *device) {
struct service *s;
-
- if ((s = pa_hashmap_get(u->services, name)))
+ char *hn, *un;
+ const char *n;
+
+ pa_assert(u);
+ pa_object_assert_ref(device);
+
+ if ((s = pa_hashmap_get(u->services, device)))
return s;
-
- s = pa_xmalloc(sizeof(struct service));
- s->published = 0;
- s->name = pa_xstrdup(name);
- s->loaded.valid = s->autoload.valid = 0;
- pa_hashmap_put(u->services, s->name, s);
+ s = pa_xnew(struct service, 1);
+ s->userdata = u;
+ s->entry_group = NULL;
+ s->device = device;
+
+ if (pa_sink_isinstance(device)) {
+ if (!(n = pa_proplist_gets(PA_SINK(device)->proplist, PA_PROP_DEVICE_DESCRIPTION)))
+ n = PA_SINK(device)->name;
+ } else {
+ if (!(n = pa_proplist_gets(PA_SOURCE(device)->proplist, PA_PROP_DEVICE_DESCRIPTION)))
+ n = PA_SOURCE(device)->name;
+ }
+
+ hn = pa_get_host_name_malloc();
+ un = pa_get_user_name_malloc();
+
+ s->service_name = pa_truncate_utf8(pa_sprintf_malloc("%s@%s: %s", un, hn, n), AVAHI_LABEL_MAX-1);
+
+ pa_xfree(un);
+ pa_xfree(hn);
+
+ pa_hashmap_put(u->services, s->device, s);
return s;
}
-static int publish_sink(struct userdata *u, pa_sink *s) {
- struct service *svc;
- assert(u && s);
+static void service_free(struct service *s) {
+ pa_assert(s);
- svc = get_service(u, s->name);
- if (svc->loaded.valid)
- return 0;
+ pa_hashmap_remove(s->userdata->services, s->device);
+
+ if (s->entry_group) {
+ pa_log_debug("Removing entry group for %s.", s->service_name);
+ avahi_entry_group_free(s->entry_group);
+ }
+
+ pa_xfree(s->service_name);
+ pa_xfree(s);
+}
- svc->loaded.valid = 1;
- svc->loaded.type = PA_NAMEREG_SINK;
- svc->loaded.index = s->index;
+static pa_bool_t shall_ignore(pa_object *o) {
+ pa_object_assert_ref(o);
- pa_dynarray_put(u->sink_dynarray, s->index, svc);
+ if (pa_sink_isinstance(o))
+ return !!(PA_SINK(o)->flags & PA_SINK_NETWORK);
- return publish_service(u, svc);
+ if (pa_source_isinstance(o))
+ return PA_SOURCE(o)->monitor_of || (PA_SOURCE(o)->flags & PA_SOURCE_NETWORK);
+
+ pa_assert_not_reached();
}
-static int publish_source(struct userdata *u, pa_source *s) {
- struct service *svc;
- assert(u && s);
+static pa_hook_result_t device_new_or_changed_cb(pa_core *c, pa_object *o, struct userdata *u) {
+ pa_assert(c);
+ pa_object_assert_ref(o);
- svc = get_service(u, s->name);
- if (svc->loaded.valid)
- return 0;
+ if (!shall_ignore(o))
+ publish_service(get_service(u, o));
+
+ return PA_HOOK_OK;
+}
+
+static pa_hook_result_t device_unlink_cb(pa_core *c, pa_object *o, struct userdata *u) {
+ struct service *s;
- svc->loaded.valid = 1;
- svc->loaded.type = PA_NAMEREG_SOURCE;
- svc->loaded.index = s->index;
+ pa_assert(c);
+ pa_object_assert_ref(o);
- pa_dynarray_put(u->source_dynarray, s->index, svc);
-
- return publish_service(u, svc);
+ if ((s = pa_hashmap_get(u->services, o)))
+ service_free(s);
+
+ return PA_HOOK_OK;
}
-static int publish_autoload(struct userdata *u, pa_autoload_entry *s) {
- struct service *svc;
- assert(u && s);
+static int publish_main_service(struct userdata *u);
- svc = get_service(u, s->name);
- if (svc->autoload.valid)
- return 0;
+static void main_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
+ struct userdata *u = userdata;
+ pa_assert(u);
+
+ switch (state) {
+
+ case AVAHI_ENTRY_GROUP_ESTABLISHED:
+ pa_log_info("Successfully established main service.");
+ break;
+
+ case AVAHI_ENTRY_GROUP_COLLISION: {
+ char *t;
+
+ t = avahi_alternative_service_name(u->service_name);
+ pa_log_info("Name collision: renaming main service %s to %s.", u->service_name, t);
+ pa_xfree(u->service_name);
+ u->service_name = t;
+
+ publish_main_service(u);
+ break;
+ }
- svc->autoload.valid = 1;
- svc->autoload.type = s->type;
- svc->autoload.index = s->index;
+ case AVAHI_ENTRY_GROUP_FAILURE: {
+ pa_log("Failed to register main service: %s", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
- pa_dynarray_put(u->autoload_dynarray, s->index, svc);
-
- return publish_service(u, svc);
+ avahi_entry_group_free(g);
+ u->main_entry_group = NULL;
+ break;
+ }
+
+ case AVAHI_ENTRY_GROUP_UNCOMMITED:
+ case AVAHI_ENTRY_GROUP_REGISTERING:
+ break;
+ }
}
-static int remove_sink(struct userdata *u, uint32_t idx) {
- struct service *svc;
- assert(u && idx != PA_INVALID_INDEX);
+static int publish_main_service(struct userdata *u) {
+ AvahiStringList *txt = NULL;
+ int r = -1;
- if (!(svc = pa_dynarray_get(u->sink_dynarray, idx)))
- return 0;
+ pa_assert(u);
- if (!svc->loaded.valid || svc->loaded.type != PA_NAMEREG_SINK)
- return 0;
+ if (!u->main_entry_group) {
+ if (!(u->main_entry_group = avahi_entry_group_new(u->client, main_entry_group_callback, u))) {
+ pa_log("avahi_entry_group_new() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
+ goto fail;
+ }
+ } else
+ avahi_entry_group_reset(u->main_entry_group);
+
+ txt = txt_record_server_data(u->core, txt);
+
+ 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,
+ compute_port(u),
+ txt) < 0) {
+
+ pa_log("avahi_entry_group_add_service_strlst() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
+ goto fail;
+ }
- svc->loaded.valid = 0;
- pa_dynarray_put(u->sink_dynarray, idx, NULL);
-
- return publish_service(u, svc);
+ if (avahi_entry_group_commit(u->main_entry_group) < 0) {
+ pa_log("avahi_entry_group_commit() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
+ goto fail;
+ }
+
+ r = 0;
+
+fail:
+ avahi_string_list_free(txt);
+
+ return r;
}
-static int remove_source(struct userdata *u, uint32_t idx) {
- struct service *svc;
- assert(u && idx != PA_INVALID_INDEX);
-
- if (!(svc = pa_dynarray_get(u->source_dynarray, idx)))
- return 0;
+static int publish_all_services(struct userdata *u) {
+ pa_sink *sink;
+ pa_source *source;
+ int r = -1;
+ uint32_t idx;
- if (!svc->loaded.valid || svc->loaded.type != PA_NAMEREG_SOURCE)
- return 0;
+ pa_assert(u);
+
+ pa_log_debug("Publishing services in Zeroconf");
+
+ for (sink = PA_SINK(pa_idxset_first(u->core->sinks, &idx)); sink; sink = PA_SINK(pa_idxset_next(u->core->sinks, &idx)))
+ if (!shall_ignore(PA_OBJECT(sink)))
+ publish_service(get_service(u, PA_OBJECT(sink)));
- svc->loaded.valid = 0;
- pa_dynarray_put(u->source_dynarray, idx, NULL);
+ for (source = PA_SOURCE(pa_idxset_first(u->core->sources, &idx)); source; source = PA_SOURCE(pa_idxset_next(u->core->sources, &idx)))
+ if (!shall_ignore(PA_OBJECT(source)))
+ publish_service(get_service(u, PA_OBJECT(source)));
- return publish_service(u, svc);
+ if (publish_main_service(u) < 0)
+ goto fail;
+
+ r = 0;
+
+fail:
+ return r;
}
-static int remove_autoload(struct userdata *u, uint32_t idx) {
- struct service *svc;
- assert(u && idx != PA_INVALID_INDEX);
-
- if (!(svc = pa_dynarray_get(u->autoload_dynarray, idx)))
- return 0;
+static void unpublish_all_services(struct userdata *u, pa_bool_t rem) {
+ void *state = NULL;
+ struct service *s;
- if (!svc->autoload.valid)
- return 0;
+ pa_assert(u);
- svc->autoload.valid = 0;
- pa_dynarray_put(u->autoload_dynarray, idx, NULL);
+ pa_log_debug("Unpublishing services in Zeroconf");
+
+ while ((s = pa_hashmap_iterate(u->services, &state, NULL))) {
+ if (s->entry_group) {
+ if (rem) {
+ pa_log_debug("Removing entry group for %s.", s->service_name);
+ avahi_entry_group_free(s->entry_group);
+ s->entry_group = NULL;
+ } else {
+ avahi_entry_group_reset(s->entry_group);
+ pa_log_debug("Resetting entry group for %s.", s->service_name);
+ }
+ }
+ }
- return publish_service(u, svc);
+ if (u->main_entry_group) {
+ if (rem) {
+ pa_log_debug("Removing main entry group.");
+ avahi_entry_group_free(u->main_entry_group);
+ u->main_entry_group = NULL;
+ } else {
+ avahi_entry_group_reset(u->main_entry_group);
+ pa_log_debug("Resetting main entry group.");
+ }
+ }
}
-static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
+static void client_callback(AvahiClient *c, AvahiClientState state, void *userdata) {
struct userdata *u = userdata;
- assert(u && c);
- switch (t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK)
- case PA_SUBSCRIPTION_EVENT_SINK: {
- if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
- pa_sink *sink;
+ pa_assert(c);
+ pa_assert(u);
- if ((sink = pa_idxset_get_by_index(c->sinks, idx))) {
- if (publish_sink(u, sink) < 0)
- goto fail;
- }
- } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
- if (remove_sink(u, idx) < 0)
- goto fail;
- }
-
- break;
+ u->client = c;
- case PA_SUBSCRIPTION_EVENT_SOURCE:
+ switch (state) {
+ case AVAHI_CLIENT_S_RUNNING:
+ publish_all_services(u);
+ break;
- if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
- pa_source *source;
-
- if ((source = pa_idxset_get_by_index(c->sources, idx))) {
- if (publish_source(u, source) < 0)
- goto fail;
- }
- } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
- if (remove_source(u, idx) < 0)
- goto fail;
- }
-
+ case AVAHI_CLIENT_S_COLLISION:
+ pa_log_debug("Host name collision");
+ unpublish_all_services(u, FALSE);
break;
- case PA_SUBSCRIPTION_EVENT_AUTOLOAD:
- if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_NEW) {
- pa_autoload_entry *autoload;
-
- if ((autoload = pa_idxset_get_by_index(c->autoload_idxset, idx))) {
- if (publish_autoload(u, autoload) < 0)
- goto fail;
+ case AVAHI_CLIENT_FAILURE:
+ if (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
+ int error;
+
+ pa_log_debug("Avahi daemon disconnected.");
+
+ unpublish_all_services(u, TRUE);
+ avahi_client_free(u->client);
+
+ if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
+ pa_log("avahi_client_new() failed: %s", avahi_strerror(error));
+ pa_module_unload_request(u->module, TRUE);
}
- } else if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_REMOVE) {
- if (remove_autoload(u, idx) < 0)
- goto fail;
}
-
- break;
- }
- return;
+ break;
-fail:
- if (u->subscription) {
- pa_subscription_free(u->subscription);
- u->subscription = NULL;
+ default: ;
}
}
-int pa__init(pa_core *c, pa_module*m) {
+int pa__init(pa_module*m) {
+
struct userdata *u;
- uint32_t idx, port = PA_NATIVE_DEFAULT_PORT;
- pa_sink *sink;
- pa_source *source;
- pa_autoload_entry *autoload;
pa_modargs *ma = NULL;
- char t[256], hn[256];
- int free_txt = 0;
- sw_text_record txt;
+ char *hn, *un;
+ int error;
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
- pa_log(__FILE__": failed to parse module arguments.");
+ pa_log("Failed to parse module arguments.");
goto fail;
}
- if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port == 0 || port >= 0xFFFF) {
- pa_log(__FILE__": invalid port specified.");
- goto fail;
- }
+ m->userdata = u = pa_xnew(struct userdata, 1);
+ u->core = m->core;
+ u->module = m;
+ u->native = pa_native_protocol_get(u->core);
- m->userdata = u = pa_xmalloc(sizeof(struct userdata));
- u->core = c;
- u->port = (uint16_t) port;
+ u->avahi_poll = pa_avahi_poll_new(m->core->mainloop);
- if (!(u->howl_wrapper = pa_howl_wrapper_get(c)))
- goto fail;
+ u->services = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
- u->services = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
- u->sink_dynarray = pa_dynarray_new();
- u->source_dynarray = pa_dynarray_new();
- u->autoload_dynarray = pa_dynarray_new();
-
- u->subscription = pa_subscription_new(c,
- PA_SUBSCRIPTION_MASK_SINK|
- PA_SUBSCRIPTION_MASK_SOURCE|
- PA_SUBSCRIPTION_MASK_AUTOLOAD, subscribe_callback, u);
-
- for (sink = pa_idxset_first(c->sinks, &idx); sink; sink = pa_idxset_next(c->sinks, &idx))
- if (publish_sink(u, sink) < 0)
- goto fail;
-
- for (source = pa_idxset_first(c->sources, &idx); source; source = pa_idxset_next(c->sources, &idx))
- if (publish_source(u, source) < 0)
- goto fail;
+ u->sink_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_LATE, (pa_hook_cb_t) device_new_or_changed_cb, u);
+ u->sink_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PROPLIST_CHANGED], PA_HOOK_LATE, (pa_hook_cb_t) device_new_or_changed_cb, u);
+ u->sink_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) device_unlink_cb, u);
+ u->source_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_LATE, (pa_hook_cb_t) device_new_or_changed_cb, u);
+ u->source_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PROPLIST_CHANGED], PA_HOOK_LATE, (pa_hook_cb_t) device_new_or_changed_cb, u);
+ u->source_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) device_unlink_cb, u);
- if (c->autoload_idxset)
- for (autoload = pa_idxset_first(c->autoload_idxset, &idx); autoload; autoload = pa_idxset_next(c->autoload_idxset, &idx))
- if (publish_autoload(u, autoload) < 0)
- goto fail;
+ u->main_entry_group = NULL;
- snprintf(t, sizeof(t), "Networked Audio Server on %s", pa_get_host_name(hn, sizeof(hn)));
+ un = pa_get_user_name_malloc();
+ hn = pa_get_host_name_malloc();
+ u->service_name = pa_truncate_utf8(pa_sprintf_malloc("%s@%s", un, hn), AVAHI_LABEL_MAX-1);
+ pa_xfree(un);
+ pa_xfree(hn);
- if (sw_text_record_init(&txt) != SW_OKAY) {
- pa_log(__FILE__": sw_text_record_init() failed");
+ if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) {
+ pa_log("avahi_client_new() failed: %s", avahi_strerror(error));
goto fail;
}
- free_txt = 1;
-
- txt_record_server_data(u->core, txt);
-
- if (sw_discovery_publish(pa_howl_wrapper_get_discovery(u->howl_wrapper), 0, t,
- SERVICE_NAME_SERVER,
- NULL, NULL, u->port, sw_text_record_bytes(txt), sw_text_record_len(txt),
- publish_reply, u, &u->server_oid) != SW_OKAY) {
- pa_log(__FILE__": failed to register server on zeroconf.");
- goto fail;
- }
-
- sw_text_record_fina(txt);
+
pa_modargs_free(ma);
-
+
return 0;
-
+
fail:
- pa__done(c, m);
+ pa__done(m);
if (ma)
pa_modargs_free(ma);
- if (free_txt)
- sw_text_record_fina(txt);
-
return -1;
}
-static void service_free(void *p, void *userdata) {
- struct service *s = p;
- struct userdata *u = userdata;
- assert(s && u);
- sw_discovery_cancel(pa_howl_wrapper_get_discovery(u->howl_wrapper), s->oid);
- pa_xfree(s->name);
- pa_xfree(s);
-}
-
-void pa__done(pa_core *c, pa_module*m) {
+void pa__done(pa_module*m) {
struct userdata*u;
- assert(c && m);
+ pa_assert(m);
if (!(u = m->userdata))
return;
- if (u->services)
- pa_hashmap_free(u->services, service_free, u);
-
- 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);
-
- if (u->subscription)
- pa_subscription_free(u->subscription);
-
- if (u->howl_wrapper)
- pa_howl_wrapper_unref(u->howl_wrapper);
-
-
+ if (u->services) {
+ struct service *s;
+
+ while ((s = pa_hashmap_first(u->services)))
+ service_free(s);
+
+ pa_hashmap_free(u->services, NULL, NULL);
+ }
+
+ if (u->sink_new_slot)
+ pa_hook_slot_free(u->sink_new_slot);
+ if (u->source_new_slot)
+ pa_hook_slot_free(u->source_new_slot);
+ if (u->sink_changed_slot)
+ pa_hook_slot_free(u->sink_changed_slot);
+ if (u->source_changed_slot)
+ pa_hook_slot_free(u->source_changed_slot);
+ if (u->sink_unlink_slot)
+ pa_hook_slot_free(u->sink_unlink_slot);
+ if (u->source_unlink_slot)
+ pa_hook_slot_free(u->source_unlink_slot);
+
+ if (u->main_entry_group)
+ avahi_entry_group_free(u->main_entry_group);
+
+ if (u->client)
+ avahi_client_free(u->client);
+
+ if (u->avahi_poll)
+ pa_avahi_poll_free(u->avahi_poll);
+
+ if (u->native)
+ pa_native_protocol_unref(u->native);
+
+ pa_xfree(u->service_name);
pa_xfree(u);
}
-