2004-12-11 16:48:45 +00:00
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-02-13 15:35:19 +00:00
|
|
|
Copyright 2004-2006 Lennart Poettering
|
|
|
|
|
|
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
|
2009-03-03 20:23:02 +00:00
|
|
|
published by the Free Software Foundation; either version 2.1 of the
|
2004-12-11 16:48:45 +00:00
|
|
|
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 <stdlib.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>
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <avahi-common/domain.h>
|
2006-07-13 15:54:13 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulse/xmalloc.h>
|
|
|
|
|
#include <pulse/util.h>
|
2013-05-15 09:41:19 +05:30
|
|
|
#include <pulse/thread-mainloop.h>
|
2006-05-17 16:34:18 +00:00
|
|
|
|
2009-04-30 02:36:02 +02:00
|
|
|
#include <pulsecore/parseaddr.h>
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/sink.h>
|
|
|
|
|
#include <pulsecore/source.h>
|
|
|
|
|
#include <pulsecore/native-common.h>
|
|
|
|
|
#include <pulsecore/core-util.h>
|
|
|
|
|
#include <pulsecore/log.h>
|
|
|
|
|
#include <pulsecore/dynarray.h>
|
|
|
|
|
#include <pulsecore/modargs.h>
|
2006-07-13 15:54:13 +00:00
|
|
|
#include <pulsecore/avahi-wrap.h>
|
2009-04-30 02:36:02 +02:00
|
|
|
#include <pulsecore/protocol-native.h>
|
2006-02-17 12:10:58 +00:00
|
|
|
|
2004-12-11 16:48:45 +00:00
|
|
|
#include "module-zeroconf-publish-symdef.h"
|
|
|
|
|
|
2007-11-09 18:25:40 +00:00
|
|
|
PA_MODULE_AUTHOR("Lennart Poettering");
|
|
|
|
|
PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Publisher");
|
|
|
|
|
PA_MODULE_VERSION(PACKAGE_VERSION);
|
2013-06-27 19:28:09 +02:00
|
|
|
PA_MODULE_LOAD_ONCE(true);
|
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"
|
2007-10-29 20:03:07 +00:00
|
|
|
#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
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
/*
|
|
|
|
|
* Note: Because the core avahi-client calls result in synchronous D-Bus
|
|
|
|
|
* communication, calling any of those functions in the PA mainloop context
|
|
|
|
|
* could lead to the mainloop being blocked for long periods.
|
|
|
|
|
*
|
|
|
|
|
* To avoid this, we create a threaded-mainloop for Avahi calls, and push all
|
|
|
|
|
* D-Bus communication into that thread. The thumb-rule for the split is:
|
|
|
|
|
*
|
|
|
|
|
* 1. If access to PA data structures is needed, use the PA mainloop context
|
|
|
|
|
*
|
|
|
|
|
* 2. If a (blocking) avahi-client call is needed, use the Avahi mainloop
|
|
|
|
|
*
|
|
|
|
|
* We do have message queue to pass messages from the Avahi mainloop to the PA
|
|
|
|
|
* mainloop.
|
|
|
|
|
*/
|
|
|
|
|
|
2004-12-15 01:02:50 +00:00
|
|
|
static const char* const valid_modargs[] = {
|
|
|
|
|
NULL
|
|
|
|
|
};
|
|
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
struct avahi_msg {
|
|
|
|
|
pa_msgobject parent;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef struct avahi_msg avahi_msg;
|
|
|
|
|
PA_DEFINE_PRIVATE_CLASS(avahi_msg, pa_msgobject);
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
|
AVAHI_MESSAGE_PUBLISH_ALL,
|
|
|
|
|
AVAHI_MESSAGE_SHUTDOWN_START,
|
|
|
|
|
AVAHI_MESSAGE_SHUTDOWN_COMPLETE,
|
|
|
|
|
};
|
|
|
|
|
|
2007-10-29 20:03:07 +00:00
|
|
|
enum service_subtype {
|
|
|
|
|
SUBTYPE_HARDWARE,
|
|
|
|
|
SUBTYPE_VIRTUAL,
|
|
|
|
|
SUBTYPE_MONITOR
|
|
|
|
|
};
|
|
|
|
|
|
2004-12-11 16:48:45 +00:00
|
|
|
struct service {
|
2013-05-15 09:41:19 +05:30
|
|
|
void *key;
|
|
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
struct userdata *userdata;
|
|
|
|
|
AvahiEntryGroup *entry_group;
|
|
|
|
|
char *service_name;
|
2013-05-15 09:41:19 +05:30
|
|
|
const char *service_type;
|
2007-10-29 20:03:07 +00:00
|
|
|
enum service_subtype subtype;
|
2013-05-15 09:41:19 +05:30
|
|
|
|
|
|
|
|
char *name;
|
|
|
|
|
bool is_sink;
|
|
|
|
|
pa_sample_spec ss;
|
|
|
|
|
pa_channel_map cm;
|
|
|
|
|
pa_proplist *proplist;
|
2004-12-11 16:48:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct userdata {
|
2013-05-15 09:41:19 +05:30
|
|
|
pa_thread_mq thread_mq;
|
|
|
|
|
pa_rtpoll *rtpoll;
|
|
|
|
|
avahi_msg *msg;
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_core *core;
|
2007-10-29 20:03:07 +00:00
|
|
|
pa_module *module;
|
2013-05-15 09:41:19 +05:30
|
|
|
pa_mainloop_api *api;
|
|
|
|
|
pa_threaded_mainloop *mainloop;
|
2009-04-30 02:36:02 +02:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
AvahiPoll *avahi_poll;
|
|
|
|
|
AvahiClient *client;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
pa_hashmap *services; /* protect with mainloop lock */
|
2006-07-13 15:54:13 +00:00
|
|
|
char *service_name;
|
|
|
|
|
|
|
|
|
|
AvahiEntryGroup *main_entry_group;
|
2004-12-15 01:02:50 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_hook_slot *sink_new_slot, *source_new_slot, *sink_unlink_slot, *source_unlink_slot, *sink_changed_slot, *source_changed_slot;
|
2009-04-30 02:36:02 +02:00
|
|
|
|
|
|
|
|
pa_native_protocol *native;
|
2014-03-15 09:37:06 +02:00
|
|
|
|
2014-03-19 09:50:39 +02:00
|
|
|
bool shutting_down; /* Used in the main thread. */
|
|
|
|
|
bool client_freed; /* Used in the Avahi thread. */
|
2004-12-11 16:48:45 +00:00
|
|
|
};
|
|
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
/* Runs in PA mainloop context */
|
|
|
|
|
static void get_service_data(struct service *s, pa_object *device) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(s);
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
if (pa_sink_isinstance(device)) {
|
|
|
|
|
pa_sink *sink = PA_SINK(device);
|
|
|
|
|
|
2013-06-27 19:28:09 +02:00
|
|
|
s->is_sink = true;
|
2013-05-15 09:41:19 +05:30
|
|
|
s->service_type = SERVICE_TYPE_SINK;
|
|
|
|
|
s->ss = sink->sample_spec;
|
|
|
|
|
s->cm = sink->channel_map;
|
|
|
|
|
s->name = pa_xstrdup(sink->name);
|
|
|
|
|
s->proplist = pa_proplist_copy(sink->proplist);
|
|
|
|
|
s->subtype = sink->flags & PA_SINK_HARDWARE ? SUBTYPE_HARDWARE : SUBTYPE_VIRTUAL;
|
|
|
|
|
|
|
|
|
|
} else if (pa_source_isinstance(device)) {
|
|
|
|
|
pa_source *source = PA_SOURCE(device);
|
|
|
|
|
|
2013-06-27 19:28:09 +02:00
|
|
|
s->is_sink = false;
|
2013-05-15 09:41:19 +05:30
|
|
|
s->service_type = SERVICE_TYPE_SOURCE;
|
|
|
|
|
s->ss = source->sample_spec;
|
|
|
|
|
s->cm = source->channel_map;
|
|
|
|
|
s->name = pa_xstrdup(source->name);
|
|
|
|
|
s->proplist = pa_proplist_copy(source->proplist);
|
|
|
|
|
s->subtype = source->monitor_of ? SUBTYPE_MONITOR : (source->flags & PA_SOURCE_HARDWARE ? SUBTYPE_HARDWARE : SUBTYPE_VIRTUAL);
|
2007-10-29 20:03:07 +00:00
|
|
|
|
2004-12-11 16:48:45 +00:00
|
|
|
} else
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert_not_reached();
|
2004-12-11 16:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
/* Can be used in either PA or Avahi mainloop context since the bits of u->core
|
|
|
|
|
* that we access don't change after startup. */
|
2006-07-13 15:54:13 +00:00
|
|
|
static AvahiStringList* txt_record_server_data(pa_core *c, AvahiStringList *l) {
|
|
|
|
|
char s[128];
|
2009-04-30 02:36:40 +02:00
|
|
|
char *t;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
pa_assert(c);
|
2004-12-12 22:58:53 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
l = avahi_string_list_add_pair(l, "server-version", PACKAGE_NAME" "PACKAGE_VERSION);
|
2009-04-30 02:36:40 +02:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
t = pa_uname_string();
|
|
|
|
|
l = avahi_string_list_add_pair(l, "uname", t);
|
|
|
|
|
pa_xfree(t);
|
|
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
static void publish_service(pa_mainloop_api *api, void *service);
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
/* Runs in Avahi mainloop context */
|
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
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
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;
|
|
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
publish_service(NULL, s);
|
2007-10-28 19:13:50 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case AVAHI_ENTRY_GROUP_FAILURE: {
|
|
|
|
|
pa_log("Failed to register service: %s", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
avahi_entry_group_free(g);
|
|
|
|
|
s->entry_group = NULL;
|
2006-07-13 15:54:13 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case AVAHI_ENTRY_GROUP_UNCOMMITED:
|
|
|
|
|
case AVAHI_ENTRY_GROUP_REGISTERING:
|
|
|
|
|
;
|
2004-12-11 16:48:45 +00:00
|
|
|
}
|
2006-07-13 15:54:13 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static void service_free(struct service *s);
|
|
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
/* Can run in either context */
|
2009-04-30 02:36:02 +02:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return PA_NATIVE_DEFAULT_PORT;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
/* Runs in Avahi mainloop context */
|
|
|
|
|
static void publish_service(pa_mainloop_api *api PA_GCC_UNUSED, void *service) {
|
|
|
|
|
struct service *s = (struct service *) service;
|
2006-07-13 15:54:13 +00:00
|
|
|
int r = -1;
|
|
|
|
|
AvahiStringList *txt = NULL;
|
2007-10-28 19:13:50 +00:00
|
|
|
char cm[PA_CHANNEL_MAP_SNPRINT_MAX];
|
2013-05-15 09:41:19 +05:30
|
|
|
const char *t;
|
2007-10-29 20:03:07 +00:00
|
|
|
|
|
|
|
|
const char * const subtype_text[] = {
|
|
|
|
|
[SUBTYPE_HARDWARE] = "hardware",
|
|
|
|
|
[SUBTYPE_VIRTUAL] = "virtual",
|
|
|
|
|
[SUBTYPE_MONITOR] = "monitor"
|
|
|
|
|
};
|
2006-07-13 15:54:13 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(s);
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (!s->userdata->client || avahi_client_get_state(s->userdata->client) != AVAHI_CLIENT_S_RUNNING)
|
2013-05-15 09:41:19 +05:30
|
|
|
return;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
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
|
2006-07-13 15:54:13 +00:00
|
|
|
avahi_entry_group_reset(s->entry_group);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
txt = txt_record_server_data(s->userdata->core, txt);
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
txt = avahi_string_list_add_pair(txt, "device", s->name);
|
|
|
|
|
txt = avahi_string_list_add_printf(txt, "rate=%u", s->ss.rate);
|
|
|
|
|
txt = avahi_string_list_add_printf(txt, "channels=%u", s->ss.channels);
|
|
|
|
|
txt = avahi_string_list_add_pair(txt, "format", pa_sample_format_to_string(s->ss.format));
|
|
|
|
|
txt = avahi_string_list_add_pair(txt, "channel_map", pa_channel_map_snprint(cm, sizeof(cm), &s->cm));
|
|
|
|
|
txt = avahi_string_list_add_pair(txt, "subtype", subtype_text[s->subtype]);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
if ((t = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION)))
|
2009-04-30 02:36:40 +02:00
|
|
|
txt = avahi_string_list_add_pair(txt, "description", t);
|
2013-05-15 09:41:19 +05:30
|
|
|
if ((t = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_ICON_NAME)))
|
2009-04-30 02:36:40 +02:00
|
|
|
txt = avahi_string_list_add_pair(txt, "icon-name", t);
|
2013-05-15 09:41:19 +05:30
|
|
|
if ((t = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_VENDOR_NAME)))
|
2009-04-30 02:36:40 +02:00
|
|
|
txt = avahi_string_list_add_pair(txt, "vendor-name", t);
|
2013-05-15 09:41:19 +05:30
|
|
|
if ((t = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_PRODUCT_NAME)))
|
2009-04-30 02:36:40 +02:00
|
|
|
txt = avahi_string_list_add_pair(txt, "product-name", t);
|
2013-05-15 09:41:19 +05:30
|
|
|
if ((t = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_CLASS)))
|
2009-04-30 02:36:40 +02:00
|
|
|
txt = avahi_string_list_add_pair(txt, "class", t);
|
2013-05-15 09:41:19 +05:30
|
|
|
if ((t = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_FORM_FACTOR)))
|
2009-04-30 02:36:40 +02:00
|
|
|
txt = avahi_string_list_add_pair(txt, "form-factor", t);
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (avahi_entry_group_add_service_strlst(
|
|
|
|
|
s->entry_group,
|
|
|
|
|
AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
|
|
|
|
|
0,
|
|
|
|
|
s->service_name,
|
2013-05-15 09:41:19 +05:30
|
|
|
s->service_type,
|
2007-10-28 19:13:50 +00:00
|
|
|
NULL,
|
|
|
|
|
NULL,
|
2009-04-30 02:36:02 +02:00
|
|
|
compute_port(s->userdata),
|
2007-10-28 19:13:50 +00:00
|
|
|
txt) < 0) {
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_log("avahi_entry_group_add_service_strlst(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-29 20:03:07 +00:00
|
|
|
if (avahi_entry_group_add_service_subtype(
|
|
|
|
|
s->entry_group,
|
|
|
|
|
AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
|
|
|
|
|
0,
|
|
|
|
|
s->service_name,
|
2013-05-15 09:41:19 +05:30
|
|
|
s->service_type,
|
2007-10-29 20:03:07 +00:00
|
|
|
NULL,
|
2013-05-15 09:41:19 +05:30
|
|
|
s->is_sink ? (s->subtype == SUBTYPE_HARDWARE ? SERVICE_SUBTYPE_SINK_HARDWARE : SERVICE_SUBTYPE_SINK_VIRTUAL) :
|
|
|
|
|
(s->subtype == SUBTYPE_HARDWARE ? SERVICE_SUBTYPE_SOURCE_HARDWARE : (s->subtype == SUBTYPE_VIRTUAL ? SERVICE_SUBTYPE_SOURCE_VIRTUAL : SERVICE_SUBTYPE_SOURCE_MONITOR))) < 0) {
|
2007-10-29 20:03:07 +00:00
|
|
|
|
|
|
|
|
pa_log("avahi_entry_group_add_service_subtype(): %s", avahi_strerror(avahi_client_errno(s->userdata->client)));
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
if (!s->is_sink && s->subtype != SUBTYPE_MONITOR) {
|
2007-10-29 20:03:07 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
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;
|
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-10-28 19:13:50 +00:00
|
|
|
pa_log_debug("Successfully created entry group for %s.", s->service_name);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-12-11 16:48:45 +00:00
|
|
|
finish:
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
/* Remove this service */
|
2013-02-12 21:36:53 +02:00
|
|
|
if (r < 0) {
|
2013-05-15 09:41:19 +05:30
|
|
|
pa_hashmap_remove(s->userdata->services, s->key);
|
2007-10-28 19:13:50 +00:00
|
|
|
service_free(s);
|
2013-02-12 21:36:53 +02:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
avahi_string_list_free(txt);
|
2004-12-11 16:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
/* Runs in PA mainloop context */
|
2007-10-28 19:13:50 +00:00
|
|
|
static struct service *get_service(struct userdata *u, pa_object *device) {
|
2004-12-11 16:48:45 +00:00
|
|
|
struct service *s;
|
2009-04-30 02:37:05 +02:00
|
|
|
char *hn, *un;
|
2007-10-28 19:13:50 +00:00
|
|
|
const char *n;
|
|
|
|
|
|
|
|
|
|
pa_assert(u);
|
|
|
|
|
pa_object_assert_ref(device);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
pa_threaded_mainloop_lock(u->mainloop);
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if ((s = pa_hashmap_get(u->services, device)))
|
2013-05-15 09:41:19 +05:30
|
|
|
goto out;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
s = pa_xnew(struct service, 1);
|
2013-05-15 09:41:19 +05:30
|
|
|
s->key = device;
|
2006-07-13 15:54:13 +00:00
|
|
|
s->userdata = u;
|
|
|
|
|
s->entry_group = NULL;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
get_service_data(s, device);
|
|
|
|
|
|
|
|
|
|
if (!(n = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION)))
|
|
|
|
|
n = s->name;
|
2004-12-12 15:56:22 +00:00
|
|
|
|
2009-04-30 02:37:05 +02:00
|
|
|
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);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
pa_hashmap_put(u->services, device, s);
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
pa_threaded_mainloop_unlock(u->mainloop);
|
2006-08-13 20:44:32 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
return s;
|
2004-12-11 16:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
/* Run from Avahi mainloop context */
|
2007-10-28 19:13:50 +00:00
|
|
|
static void service_free(struct service *s) {
|
|
|
|
|
pa_assert(s);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (s->entry_group) {
|
|
|
|
|
pa_log_debug("Removing entry group for %s.", s->service_name);
|
|
|
|
|
avahi_entry_group_free(s->entry_group);
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_xfree(s->service_name);
|
2013-05-15 09:41:19 +05:30
|
|
|
|
|
|
|
|
pa_xfree(s->name);
|
|
|
|
|
pa_proplist_free(s->proplist);
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_xfree(s);
|
2004-12-11 16:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
/* Runs in PA mainloop context */
|
2013-06-27 19:28:09 +02:00
|
|
|
static bool shall_ignore(pa_object *o) {
|
2007-10-29 21:23:08 +00:00
|
|
|
pa_object_assert_ref(o);
|
|
|
|
|
|
|
|
|
|
if (pa_sink_isinstance(o))
|
|
|
|
|
return !!(PA_SINK(o)->flags & PA_SINK_NETWORK);
|
|
|
|
|
|
|
|
|
|
if (pa_source_isinstance(o))
|
2007-10-29 23:54:46 +00:00
|
|
|
return PA_SOURCE(o)->monitor_of || (PA_SOURCE(o)->flags & PA_SOURCE_NETWORK);
|
2007-10-29 21:23:08 +00:00
|
|
|
|
|
|
|
|
pa_assert_not_reached();
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
/* Runs in PA mainloop context */
|
2007-10-28 19:13:50 +00:00
|
|
|
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);
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2013-06-17 18:11:08 +05:30
|
|
|
if (!shall_ignore(o)) {
|
|
|
|
|
pa_threaded_mainloop_lock(u->mainloop);
|
2013-05-15 09:41:19 +05:30
|
|
|
pa_mainloop_api_once(u->api, publish_service, get_service(u, o));
|
2013-06-17 18:11:08 +05:30
|
|
|
pa_threaded_mainloop_unlock(u->mainloop);
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
return PA_HOOK_OK;
|
2004-12-11 16:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
/* Runs in PA mainloop context */
|
2007-10-28 19:13:50 +00:00
|
|
|
static pa_hook_result_t device_unlink_cb(pa_core *c, pa_object *o, struct userdata *u) {
|
|
|
|
|
struct service *s;
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(c);
|
|
|
|
|
pa_object_assert_ref(o);
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
pa_threaded_mainloop_lock(u->mainloop);
|
|
|
|
|
|
2013-02-12 21:36:53 +02:00
|
|
|
if ((s = pa_hashmap_remove(u->services, o)))
|
2007-10-28 19:13:50 +00:00
|
|
|
service_free(s);
|
2004-12-12 15:56:22 +00:00
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
pa_threaded_mainloop_unlock(u->mainloop);
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
return PA_HOOK_OK;
|
2004-12-11 16:48:45 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static int publish_main_service(struct userdata *u);
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
/* Runs in Avahi mainloop context */
|
2007-10-28 19:13:50 +00:00
|
|
|
static void main_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
|
2004-12-11 16:48:45 +00:00
|
|
|
struct userdata *u = userdata;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(u);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
switch (state) {
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
case AVAHI_ENTRY_GROUP_ESTABLISHED:
|
|
|
|
|
pa_log_info("Successfully established main service.");
|
|
|
|
|
break;
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
case AVAHI_ENTRY_GROUP_COLLISION: {
|
|
|
|
|
char *t;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
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;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
publish_main_service(u);
|
2004-12-11 16:48:45 +00:00
|
|
|
break;
|
2007-10-28 19:13:50 +00:00
|
|
|
}
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
case AVAHI_ENTRY_GROUP_FAILURE: {
|
|
|
|
|
pa_log("Failed to register main service: %s", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
avahi_entry_group_free(g);
|
|
|
|
|
u->main_entry_group = NULL;
|
2004-12-11 16:48:45 +00:00
|
|
|
break;
|
2007-10-28 19:13:50 +00:00
|
|
|
}
|
2006-07-13 15:54:13 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
case AVAHI_ENTRY_GROUP_UNCOMMITED:
|
|
|
|
|
case AVAHI_ENTRY_GROUP_REGISTERING:
|
|
|
|
|
break;
|
2006-07-13 15:54:13 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
/* Runs in Avahi mainloop context */
|
2006-07-13 15:54:13 +00:00
|
|
|
static int publish_main_service(struct userdata *u) {
|
|
|
|
|
AvahiStringList *txt = NULL;
|
|
|
|
|
int r = -1;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(u);
|
|
|
|
|
|
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
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
txt = txt_record_server_data(u->core, txt);
|
2006-07-13 15:54:13 +00:00
|
|
|
|
|
|
|
|
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,
|
2009-04-30 02:36:02 +02:00
|
|
|
compute_port(u),
|
2006-07-13 15:54:13 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
/* Runs in PA mainloop context */
|
2006-07-13 15:54:13 +00:00
|
|
|
static int publish_all_services(struct userdata *u) {
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_sink *sink;
|
|
|
|
|
pa_source *source;
|
2006-07-13 15:54:13 +00:00
|
|
|
int r = -1;
|
|
|
|
|
uint32_t idx;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(u);
|
2006-07-13 15:54:13 +00:00
|
|
|
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log_debug("Publishing services in Zeroconf");
|
2006-07-13 15:54:13 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
for (sink = PA_SINK(pa_idxset_first(u->core->sinks, &idx)); sink; sink = PA_SINK(pa_idxset_next(u->core->sinks, &idx)))
|
2013-06-17 18:11:08 +05:30
|
|
|
if (!shall_ignore(PA_OBJECT(sink))) {
|
|
|
|
|
pa_threaded_mainloop_lock(u->mainloop);
|
2013-05-15 09:41:19 +05:30
|
|
|
pa_mainloop_api_once(u->api, publish_service, get_service(u, PA_OBJECT(sink)));
|
2013-06-17 18:11:08 +05:30
|
|
|
pa_threaded_mainloop_unlock(u->mainloop);
|
|
|
|
|
}
|
2006-07-13 15:54:13 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
for (source = PA_SOURCE(pa_idxset_first(u->core->sources, &idx)); source; source = PA_SOURCE(pa_idxset_next(u->core->sources, &idx)))
|
2013-06-17 18:11:08 +05:30
|
|
|
if (!shall_ignore(PA_OBJECT(source))) {
|
|
|
|
|
pa_threaded_mainloop_lock(u->mainloop);
|
2013-05-15 09:41:19 +05:30
|
|
|
pa_mainloop_api_once(u->api, publish_service, get_service(u, PA_OBJECT(source)));
|
2013-06-17 18:11:08 +05:30
|
|
|
pa_threaded_mainloop_unlock(u->mainloop);
|
|
|
|
|
}
|
2006-07-13 15:54:13 +00:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
/* Runs in Avahi mainloop context */
|
2013-06-27 19:28:09 +02:00
|
|
|
static void unpublish_all_services(struct userdata *u, bool rem) {
|
2006-07-13 15:54:13 +00:00
|
|
|
void *state = NULL;
|
|
|
|
|
struct service *s;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(u);
|
2006-07-13 15:54:13 +00:00
|
|
|
|
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) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_log_debug("Removing entry group for %s.", s->service_name);
|
2006-07-13 15:54:13 +00:00
|
|
|
avahi_entry_group_free(s->entry_group);
|
|
|
|
|
s->entry_group = NULL;
|
2007-10-28 19:13:50 +00:00
|
|
|
} else {
|
2006-07-13 15:54:13 +00:00
|
|
|
avahi_entry_group_reset(s->entry_group);
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_log_debug("Resetting entry group for %s.", s->service_name);
|
|
|
|
|
}
|
2006-07-13 15:54:13 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (u->main_entry_group) {
|
|
|
|
|
if (rem) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_log_debug("Removing main entry group.");
|
2006-07-13 15:54:13 +00:00
|
|
|
avahi_entry_group_free(u->main_entry_group);
|
|
|
|
|
u->main_entry_group = NULL;
|
2007-10-28 19:13:50 +00:00
|
|
|
} else {
|
2006-07-13 15:54:13 +00:00
|
|
|
avahi_entry_group_reset(u->main_entry_group);
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_log_debug("Resetting main entry group.");
|
|
|
|
|
}
|
2006-07-13 15:54:13 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
/* Runs in PA mainloop context */
|
|
|
|
|
static int avahi_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
|
|
|
|
|
struct userdata *u = (struct userdata *) data;
|
|
|
|
|
|
2014-03-15 09:37:06 +02:00
|
|
|
pa_assert(u);
|
|
|
|
|
|
|
|
|
|
if (u->shutting_down)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
switch (code) {
|
|
|
|
|
case AVAHI_MESSAGE_PUBLISH_ALL:
|
|
|
|
|
publish_all_services(u);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case AVAHI_MESSAGE_SHUTDOWN_START:
|
|
|
|
|
pa_module_unload(u->core, u->module, true);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
pa_assert_not_reached();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Runs in Avahi mainloop context */
|
2006-07-13 15:54:13 +00:00
|
|
|
static void client_callback(AvahiClient *c, AvahiClientState state, void *userdata) {
|
|
|
|
|
struct userdata *u = userdata;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
pa_assert(c);
|
|
|
|
|
pa_assert(u);
|
2006-07-13 15:54:13 +00:00
|
|
|
|
|
|
|
|
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:
|
2013-05-15 09:41:19 +05:30
|
|
|
/* Collect all sinks/sources, and publish them */
|
|
|
|
|
pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->msg), AVAHI_MESSAGE_PUBLISH_ALL, u, 0, NULL, NULL);
|
2006-07-13 15:54:13 +00:00
|
|
|
break;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
case AVAHI_CLIENT_S_COLLISION:
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_log_debug("Host name collision");
|
2013-06-27 19:28:09 +02:00
|
|
|
unpublish_all_services(u, false);
|
2006-07-13 15:54:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case AVAHI_CLIENT_FAILURE:
|
|
|
|
|
if (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
|
|
|
|
|
int error;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
pa_log_debug("Avahi daemon disconnected.");
|
|
|
|
|
|
2013-06-27 19:28:09 +02:00
|
|
|
unpublish_all_services(u, true);
|
2006-07-13 15:54:13 +00:00
|
|
|
avahi_client_free(u->client);
|
|
|
|
|
|
2007-10-29 20:03:07 +00:00
|
|
|
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));
|
2013-06-27 19:28:09 +02:00
|
|
|
pa_module_unload_request(u->module, true);
|
2007-10-29 20:03:07 +00:00
|
|
|
}
|
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: ;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
/* Runs in Avahi mainloop context */
|
|
|
|
|
static void create_client(pa_mainloop_api *api PA_GCC_UNUSED, void *userdata) {
|
|
|
|
|
struct userdata *u = (struct userdata *) userdata;
|
|
|
|
|
int error;
|
|
|
|
|
|
2014-03-19 09:50:39 +02:00
|
|
|
/* create_client() and client_free() are called via defer events. If the
|
|
|
|
|
* two defer events are created very quickly one after another, we can't
|
|
|
|
|
* assume that the defer event that runs create_client() will be dispatched
|
|
|
|
|
* before the defer event that runs client_free() (at the time of writing,
|
|
|
|
|
* pa_mainloop actually always dispatches queued defer events in reverse
|
|
|
|
|
* creation order). For that reason we must be prepared for the case where
|
|
|
|
|
* client_free() has already been called. */
|
|
|
|
|
if (u->client_freed)
|
|
|
|
|
return;
|
|
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
pa_thread_mq_install(&u->thread_mq);
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_log_debug("Started Avahi threaded mainloop");
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->msg), AVAHI_MESSAGE_SHUTDOWN_START, u, 0, NULL, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
int pa__init(pa_module*m) {
|
|
|
|
|
|
2006-07-13 15:54:13 +00:00
|
|
|
struct userdata *u;
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_modargs *ma = NULL;
|
2009-04-30 02:37:05 +02:00
|
|
|
char *hn, *un;
|
2004-12-15 01:02:50 +00:00
|
|
|
|
|
|
|
|
if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
|
2007-10-29 20:03:07 +00:00
|
|
|
pa_log("Failed to parse module arguments.");
|
2004-12-15 01:02:50 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-19 09:50:39 +02:00
|
|
|
m->userdata = u = pa_xnew0(struct userdata, 1);
|
2007-10-28 19:13:50 +00:00
|
|
|
u->core = m->core;
|
2007-10-29 20:03:07 +00:00
|
|
|
u->module = m;
|
2009-04-30 02:36:02 +02:00
|
|
|
u->native = pa_native_protocol_get(u->core);
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
u->rtpoll = pa_rtpoll_new();
|
|
|
|
|
u->mainloop = pa_threaded_mainloop_new();
|
|
|
|
|
u->api = pa_threaded_mainloop_get_api(u->mainloop);
|
|
|
|
|
|
|
|
|
|
pa_thread_mq_init(&u->thread_mq, u->core->mainloop, u->rtpoll);
|
|
|
|
|
u->msg = pa_msgobject_new(avahi_msg);
|
|
|
|
|
u->msg->parent.process_msg = avahi_process_msg;
|
|
|
|
|
|
|
|
|
|
u->avahi_poll = pa_avahi_poll_new(u->api);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2013-09-14 11:50:10 +05:30
|
|
|
u->services = pa_hashmap_new_full(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func, NULL, (pa_free_cb_t) service_free);
|
2006-07-13 15:54:13 +00:00
|
|
|
|
2008-06-11 19:45:57 +00:00
|
|
|
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);
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2009-04-30 02:37:05 +02:00
|
|
|
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);
|
2004-12-11 16:48:45 +00:00
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
pa_threaded_mainloop_set_name(u->mainloop, "avahi-ml");
|
|
|
|
|
pa_threaded_mainloop_start(u->mainloop);
|
2013-06-17 18:11:08 +05:30
|
|
|
|
|
|
|
|
pa_threaded_mainloop_lock(u->mainloop);
|
2013-05-15 09:41:19 +05:30
|
|
|
pa_mainloop_api_once(u->api, create_client, u);
|
2013-06-17 18:11:08 +05:30
|
|
|
pa_threaded_mainloop_unlock(u->mainloop);
|
2004-12-15 01:02:50 +00:00
|
|
|
|
|
|
|
|
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:
|
2007-10-28 19:13:50 +00:00
|
|
|
pa__done(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;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
/* Runs in Avahi mainloop context */
|
|
|
|
|
static void client_free(pa_mainloop_api *api PA_GCC_UNUSED, void *userdata) {
|
|
|
|
|
struct userdata *u = (struct userdata *) userdata;
|
|
|
|
|
|
2013-09-14 11:50:10 +05:30
|
|
|
pa_hashmap_free(u->services);
|
2013-05-15 09:41:19 +05:30
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2014-03-15 09:37:06 +02:00
|
|
|
pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->msg), AVAHI_MESSAGE_SHUTDOWN_COMPLETE, u, 0, NULL, NULL);
|
2014-03-19 09:50:39 +02:00
|
|
|
|
|
|
|
|
u->client_freed = true;
|
2013-05-15 09:41:19 +05:30
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
void pa__done(pa_module*m) {
|
2004-12-11 16:48:45 +00:00
|
|
|
struct userdata*u;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(m);
|
2004-12-11 16:48:45 +00:00
|
|
|
|
|
|
|
|
if (!(u = m->userdata))
|
|
|
|
|
return;
|
|
|
|
|
|
2014-03-15 09:37:06 +02:00
|
|
|
u->shutting_down = true;
|
|
|
|
|
|
2014-03-15 09:37:05 +02:00
|
|
|
pa_threaded_mainloop_lock(u->mainloop);
|
2013-05-15 09:41:19 +05:30
|
|
|
pa_mainloop_api_once(u->api, client_free, u);
|
2014-03-15 09:37:05 +02:00
|
|
|
pa_threaded_mainloop_unlock(u->mainloop);
|
2013-05-15 09:41:19 +05:30
|
|
|
pa_asyncmsgq_wait_for(u->thread_mq.outq, AVAHI_MESSAGE_SHUTDOWN_COMPLETE);
|
|
|
|
|
|
|
|
|
|
pa_threaded_mainloop_stop(u->mainloop);
|
|
|
|
|
pa_threaded_mainloop_free(u->mainloop);
|
|
|
|
|
|
|
|
|
|
pa_thread_mq_done(&u->thread_mq);
|
|
|
|
|
pa_rtpoll_free(u->rtpoll);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
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);
|
2004-12-12 15:56:22 +00:00
|
|
|
|
2009-05-01 04:12:45 +02:00
|
|
|
if (u->native)
|
|
|
|
|
pa_native_protocol_unref(u->native);
|
|
|
|
|
|
2013-05-15 09:41:19 +05:30
|
|
|
pa_xfree(u->msg);
|
2006-07-13 15:54:13 +00:00
|
|
|
pa_xfree(u->service_name);
|
2004-12-11 16:48:45 +00:00
|
|
|
pa_xfree(u);
|
|
|
|
|
}
|