improve default sink/source handling

Currently the default sink policy is simple: either the user has
configured it explicitly, in which case we always use that as the
default, or we pick the sink with the highest priority. The sink
priorities are currently static, so there's no need to worry about
updating the default sink when sink priorities change.

I intend to make things a bit more complex: if the active port of a sink
is unavailable, the sink should not be the default sink, and I also want
to make sink priorities dependent on the active port, so changing the
port should cause re-evaluation of which sink to choose as the default.
Currently the default sink choice is done only when someone calls
pa_namereg_get_default_sink(), and change notifications are only sent
when a sink is created or destroyed. That makes it hard to add new rules
to the default sink selection policy.

This patch moves the default sink selection to
pa_core_update_default_sink(), which is called whenever something
happens that can affect the default sink choice. That function needs to
know the previous choice in order to send change notifications as
appropriate, but previously pa_core.default_sink was only set when the
user had configured it explicitly. Now pa_core.default_sink is always
set (unless there are no sinks at all), so pa_core_update_default_sink()
can use that to get the previous choice. The user configuration is saved
in a new variable, pa_core.configured_default_sink.

pa_namereg_get_default_sink() is now unnecessary, because
pa_core.default_sink can be used directly to get the
currently-considered-best sink. pa_namereg_set_default_sink() is
replaced by pa_core_set_configured_default_sink().

I haven't confirmed it, but I expect that this patch will fix problems
in the D-Bus protocol related to default sink handling. The D-Bus
protocol used to get confused when the current default sink gets
removed. It would incorrectly think that if there's no explicitly
configured default sink, then there's no default sink at all. Even
worse, when the D-Bus thinks that there's no default sink, it concludes
that there are no sinks at all, which made it impossible to configure
the default sink via the D-Bus interface. Now that pa_core.default_sink
is always set, except when there really aren't any sinks, the D-Bus
protocol should behave correctly.

BugLink: https://bugs.freedesktop.org/show_bug.cgi?id=99425
This commit is contained in:
Tanu Kaskinen 2017-02-16 12:09:38 +02:00
parent ea3ebd09d1
commit 6b34896130
15 changed files with 370 additions and 239 deletions

View file

@ -344,8 +344,6 @@ static int pa_cli_command_stat(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, bool
char bytes[PA_BYTES_SNPRINT_MAX];
const pa_mempool_stat *mstat;
unsigned k;
pa_sink *def_sink;
pa_source *def_source;
static const char* const type_table[PA_MEMBLOCK_TYPE_MAX] = {
[PA_MEMBLOCK_POOL] = "POOL",
@ -388,12 +386,10 @@ static int pa_cli_command_stat(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, bool
pa_strbuf_printf(buf, "Default channel map: %s\n",
pa_channel_map_snprint(cm, sizeof(cm), &c->default_channel_map));
def_sink = pa_namereg_get_default_sink(c);
def_source = pa_namereg_get_default_source(c);
pa_strbuf_printf(buf, "Default sink name: %s\n"
"Default source name: %s\n",
def_sink ? def_sink->name : "none",
def_source ? def_source->name : "none");
c->default_sink ? c->default_sink->name : "none",
c->default_source ? c->default_source->name : "none");
for (k = 0; k < PA_MEMBLOCK_TYPE_MAX; k++)
pa_strbuf_printf(buf,
@ -1034,7 +1030,7 @@ static int pa_cli_command_sink_default(pa_core *c, pa_tokenizer *t, pa_strbuf *b
}
if ((s = pa_namereg_get(c, n, PA_NAMEREG_SINK)))
pa_namereg_set_default_sink(c, s);
pa_core_set_configured_default_sink(c, s);
else
pa_strbuf_printf(buf, "Sink %s does not exist.\n", n);
@ -1056,7 +1052,7 @@ static int pa_cli_command_source_default(pa_core *c, pa_tokenizer *t, pa_strbuf
}
if ((s = pa_namereg_get(c, n, PA_NAMEREG_SOURCE)))
pa_namereg_set_default_source(c, s);
pa_core_set_configured_default_source(c, s);
else
pa_strbuf_printf(buf, "Source %s does not exist.\n", n);
return 0;
@ -1850,20 +1846,20 @@ static int pa_cli_command_dump(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, bool
}
nl = false;
if ((sink = pa_namereg_get_default_sink(c))) {
if (c->default_sink) {
if (!nl) {
pa_strbuf_puts(buf, "\n");
nl = true;
}
pa_strbuf_printf(buf, "set-default-sink %s\n", sink->name);
pa_strbuf_printf(buf, "set-default-sink %s\n", c->default_sink->name);
}
if ((source = pa_namereg_get_default_source(c))) {
if (c->default_source) {
if (!nl)
pa_strbuf_puts(buf, "\n");
pa_strbuf_printf(buf, "set-default-source %s\n", source->name);
pa_strbuf_printf(buf, "set-default-source %s\n", c->default_source->name);
}
pa_strbuf_puts(buf, "\n### EOF\n");

View file

@ -232,7 +232,7 @@ static const char *source_state_to_string(pa_source_state_t state) {
char *pa_sink_list_to_string(pa_core *c) {
pa_strbuf *s;
pa_sink *sink, *default_sink;
pa_sink *sink;
uint32_t idx = PA_IDXSET_INVALID;
pa_assert(c);
@ -240,8 +240,6 @@ char *pa_sink_list_to_string(pa_core *c) {
pa_strbuf_printf(s, "%u sink(s) available.\n", pa_idxset_size(c->sinks));
default_sink = pa_namereg_get_default_sink(c);
PA_IDXSET_FOREACH(sink, c->sinks, idx) {
char ss[PA_SAMPLE_SPEC_SNPRINT_MAX],
cv[PA_CVOLUME_SNPRINT_VERBOSE_MAX],
@ -273,7 +271,7 @@ char *pa_sink_list_to_string(pa_core *c) {
"\tchannel map: %s%s%s\n"
"\tused by: %u\n"
"\tlinked by: %u\n",
sink == default_sink ? '*' : ' ',
sink == c->default_sink ? '*' : ' ',
sink->index,
sink->name,
sink->driver,
@ -350,7 +348,7 @@ char *pa_sink_list_to_string(pa_core *c) {
char *pa_source_list_to_string(pa_core *c) {
pa_strbuf *s;
pa_source *source, *default_source;
pa_source *source;
uint32_t idx = PA_IDXSET_INVALID;
pa_assert(c);
@ -358,8 +356,6 @@ char *pa_source_list_to_string(pa_core *c) {
pa_strbuf_printf(s, "%u source(s) available.\n", pa_idxset_size(c->sources));
default_source = pa_namereg_get_default_source(c);
PA_IDXSET_FOREACH(source, c->sources, idx) {
char ss[PA_SAMPLE_SPEC_SNPRINT_MAX],
cv[PA_CVOLUME_SNPRINT_VERBOSE_MAX],
@ -389,7 +385,7 @@ char *pa_source_list_to_string(pa_core *c) {
"\tchannel map: %s%s%s\n"
"\tused by: %u\n"
"\tlinked by: %u\n",
source == default_source ? '*' : ' ',
source == c->default_source ? '*' : ' ',
source->index,
source->name,
source->driver,

View file

@ -224,6 +224,176 @@ static void core_free(pa_object *o) {
pa_xfree(c);
}
void pa_core_set_configured_default_sink(pa_core *core, pa_sink *sink) {
pa_sink *old_sink;
pa_assert(core);
old_sink = core->configured_default_sink;
if (sink == old_sink)
return;
core->configured_default_sink = sink;
pa_log_info("configured_default_sink: %s -> %s",
old_sink ? old_sink->name : "(unset)", sink ? sink->name : "(unset)");
pa_core_update_default_sink(core);
}
void pa_core_set_configured_default_source(pa_core *core, pa_source *source) {
pa_source *old_source;
pa_assert(core);
old_source = core->configured_default_source;
if (source == old_source)
return;
core->configured_default_source = source;
pa_log_info("configured_default_source: %s -> %s",
old_source ? old_source->name : "(unset)", source ? source->name : "(unset)");
pa_core_update_default_source(core);
}
/* a < b -> return -1
* a == b -> return 0
* a > b -> return 1 */
static int compare_sinks(pa_sink *a, pa_sink *b) {
pa_core *core;
core = a->core;
/* The configured default sink is preferred over any other sink. */
if (b == core->configured_default_sink)
return -1;
if (a == core->configured_default_sink)
return 1;
if (a->priority < b->priority)
return -1;
if (a->priority > b->priority)
return 1;
/* It's hard to find any difference between these sinks, but maybe one of
* them is already the default sink? If so, it's best to keep it as the
* default to avoid changing the routing for no good reason. */
if (b == core->default_sink)
return -1;
if (a == core->default_sink)
return 1;
return 0;
}
void pa_core_update_default_sink(pa_core *core) {
pa_sink *best = NULL;
pa_sink *sink;
uint32_t idx;
pa_sink *old_default_sink;
pa_assert(core);
PA_IDXSET_FOREACH(sink, core->sinks, idx) {
if (!best) {
best = sink;
continue;
}
if (compare_sinks(sink, best) > 0)
best = sink;
}
old_default_sink = core->default_sink;
if (best == old_default_sink)
return;
core->default_sink = best;
pa_log_info("default_sink: %s -> %s",
old_default_sink ? old_default_sink->name : "(unset)", best ? best->name : "(unset)");
/* If the default sink changed, it may be that the default source has to be
* changed too, because monitor sources are prioritized partly based on the
* priorities of the monitored sinks. */
pa_core_update_default_source(core);
pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SERVER | PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
pa_hook_fire(&core->hooks[PA_CORE_HOOK_DEFAULT_SINK_CHANGED], core->default_sink);
}
/* a < b -> return -1
* a == b -> return 0
* a > b -> return 1 */
static int compare_sources(pa_source *a, pa_source *b) {
pa_core *core;
core = a->core;
/* The configured default source is preferred over any other source. */
if (b == core->configured_default_source)
return -1;
if (a == core->configured_default_source)
return 1;
/* Monitor sources lose to non-monitor sources. */
if (a->monitor_of && !b->monitor_of)
return -1;
if (!a->monitor_of && b->monitor_of)
return 1;
if (a->priority < b->priority)
return -1;
if (a->priority > b->priority)
return 1;
/* If the sources are monitors, we can compare the monitored sinks. */
if (a->monitor_of)
return compare_sinks(a->monitor_of, b->monitor_of);
/* It's hard to find any difference between these sources, but maybe one of
* them is already the default source? If so, it's best to keep it as the
* default to avoid changing the routing for no good reason. */
if (b == core->default_source)
return -1;
if (a == core->default_source)
return 1;
return 0;
}
void pa_core_update_default_source(pa_core *core) {
pa_source *best = NULL;
pa_source *source;
uint32_t idx;
pa_source *old_default_source;
pa_assert(core);
PA_IDXSET_FOREACH(source, core->sources, idx) {
if (!best) {
best = source;
continue;
}
if (compare_sources(source, best) > 0)
best = source;
}
old_default_source = core->default_source;
if (best == old_default_source)
return;
core->default_source = best;
pa_log_info("default_source: %s -> %s",
old_default_source ? old_default_source->name : "(unset)", best ? best->name : "(unset)");
pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SERVER | PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
pa_hook_fire(&core->hooks[PA_CORE_HOOK_DEFAULT_SOURCE_CHANGED], core->default_source);
}
static void exit_callback(pa_mainloop_api *m, pa_time_event *e, const struct timeval *t, void *userdata) {
pa_core *c = userdata;
pa_assert(c->exit_event == e);

View file

@ -162,9 +162,18 @@ struct pa_core {
/* Some hashmaps for all sorts of entities */
pa_hashmap *namereg, *shared;
/* The default sink/source */
pa_source *default_source;
/* The default sink/source as configured by the user. If the user hasn't
* explicitly configured anything, these are set to NULL. */
pa_sink *configured_default_sink;
pa_source *configured_default_source;
/* The effective default sink/source. If no sink or source is explicitly
* configured as the default, we pick the device that ranks highest
* according to the compare_sinks() and compare_sources() functions in
* core.c. pa_core_update_default_sink/source() has to be called whenever
* anything changes that might change the comparison results. */
pa_sink *default_sink;
pa_source *default_source;
pa_channel_map default_channel_map;
pa_sample_spec default_sample_spec;
@ -227,6 +236,21 @@ enum {
pa_core* pa_core_new(pa_mainloop_api *m, bool shared, bool enable_memfd, size_t shm_size);
void pa_core_set_configured_default_sink(pa_core *core, pa_sink *sink);
void pa_core_set_configured_default_source(pa_core *core, pa_source *source);
/* These should be called whenever something changes that may affect the
* default sink or source choice.
*
* If the default source choice happens between two monitor sources, the
* monitored sinks are compared, so if the default sink changes, the default
* source may change too. However, pa_core_update_default_sink() calls
* pa_core_update_default_source() internally, so it's sufficient to only call
* pa_core_update_default_sink() when something happens that affects the sink
* ordering. */
void pa_core_update_default_sink(pa_core *core);
void pa_core_update_default_source(pa_core *core);
/* Check whether no one is connected to this core */
void pa_core_check_idle(pa_core *c);

View file

@ -168,17 +168,6 @@ const char *pa_namereg_register(pa_core *c, const char *name, pa_namereg_type_t
pa_assert_se(pa_hashmap_put(c->namereg, e->name, e) >= 0);
/* If a sink or source is registered and there was none registered
* before we inform the clients which then can ask for the default
* sink/source which is then assigned. We don't adjust the default
* sink/source here right away to give the module the chance to
* register more sinks/sources before we choose a new default
* sink/source. */
if ((!c->default_sink && type == PA_NAMEREG_SINK) ||
(!c->default_source && type == PA_NAMEREG_SOURCE))
pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
return e->name;
}
@ -189,12 +178,6 @@ void pa_namereg_unregister(pa_core *c, const char *name) {
pa_assert(name);
pa_assert_se(e = pa_hashmap_remove(c->namereg, name));
if (c->default_sink == e->data)
pa_namereg_set_default_sink(c, NULL);
else if (c->default_source == e->data)
pa_namereg_set_default_source(c, NULL);
pa_xfree(e->name);
pa_xfree(e);
}
@ -205,22 +188,16 @@ void* pa_namereg_get(pa_core *c, const char *name, pa_namereg_type_t type) {
pa_assert(c);
if (type == PA_NAMEREG_SOURCE && (!name || pa_streq(name, "@DEFAULT_SOURCE@"))) {
pa_source *s;
if ((s = pa_namereg_get_default_source(c)))
return s;
return c->default_source;
} else if (type == PA_NAMEREG_SINK && (!name || pa_streq(name, "@DEFAULT_SINK@"))) {
pa_sink *s;
if ((s = pa_namereg_get_default_sink(c)))
return s;
return c->default_sink;
} else if (type == PA_NAMEREG_SOURCE && name && pa_streq(name, "@DEFAULT_MONITOR@")) {
pa_sink *s;
if ((s = pa_namereg_get(c, NULL, PA_NAMEREG_SINK)))
return s->monitor_source;
if (c->default_sink)
return c->default_sink->monitor_source;
else
return NULL;
}
if (!name)
@ -248,83 +225,3 @@ void* pa_namereg_get(pa_core *c, const char *name, pa_namereg_type_t type) {
return NULL;
}
pa_sink* pa_namereg_set_default_sink(pa_core*c, pa_sink *s) {
pa_assert(c);
if (s && !PA_SINK_IS_LINKED(pa_sink_get_state(s)))
return NULL;
if (c->default_sink != s) {
c->default_sink = s;
pa_hook_fire(&c->hooks[PA_CORE_HOOK_DEFAULT_SINK_CHANGED], c->default_sink);
pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
}
return s;
}
pa_source* pa_namereg_set_default_source(pa_core*c, pa_source *s) {
pa_assert(c);
if (s && !PA_SOURCE_IS_LINKED(pa_source_get_state(s)))
return NULL;
if (c->default_source != s) {
c->default_source = s;
pa_hook_fire(&c->hooks[PA_CORE_HOOK_DEFAULT_SOURCE_CHANGED], c->default_source);
pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
}
return s;
}
pa_sink *pa_namereg_get_default_sink(pa_core *c) {
pa_sink *s, *best = NULL;
uint32_t idx;
pa_assert(c);
if (c->default_sink && PA_SINK_IS_LINKED(pa_sink_get_state(c->default_sink)))
return c->default_sink;
PA_IDXSET_FOREACH(s, c->sinks, idx)
if (PA_SINK_IS_LINKED(pa_sink_get_state(s)))
if (!best || s->priority > best->priority)
best = s;
return best;
}
pa_source *pa_namereg_get_default_source(pa_core *c) {
pa_source *s, *best = NULL;
uint32_t idx;
pa_assert(c);
if (c->default_source && PA_SOURCE_IS_LINKED(pa_source_get_state(c->default_source)))
return c->default_source;
/* First, try to find one that isn't a monitor */
PA_IDXSET_FOREACH(s, c->sources, idx)
if (!s->monitor_of && PA_SOURCE_IS_LINKED(pa_source_get_state(s)))
if (!best ||
s->priority > best->priority)
best = s;
if (best)
return best;
/* Then, fallback to a monitor */
PA_IDXSET_FOREACH(s, c->sources, idx)
if (PA_SOURCE_IS_LINKED(pa_source_get_state(s)))
if (!best ||
s->priority > best->priority ||
(s->priority == best->priority &&
s->monitor_of &&
best->monitor_of &&
s->monitor_of->priority > best->monitor_of->priority))
best = s;
return best;
}

View file

@ -36,12 +36,6 @@ const char *pa_namereg_register(pa_core *c, const char *name, pa_namereg_type_t
void pa_namereg_unregister(pa_core *c, const char *name);
void* pa_namereg_get(pa_core *c, const char *name, pa_namereg_type_t type);
pa_sink* pa_namereg_set_default_sink(pa_core*c, pa_sink *s);
pa_source* pa_namereg_set_default_source(pa_core*c, pa_source *s);
pa_sink *pa_namereg_get_default_sink(pa_core *c);
pa_source *pa_namereg_get_default_source(pa_core *c);
bool pa_namereg_is_valid_name(const char *name);
bool pa_namereg_is_valid_name_or_wildcard(const char *name, pa_namereg_type_t type);
char* pa_namereg_make_valid_name(const char *name);

View file

@ -3637,10 +3637,9 @@ static void command_get_info_list(pa_pdispatch *pd, uint32_t command, uint32_t t
static void command_get_server_info(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
pa_native_connection *c = PA_NATIVE_CONNECTION(userdata);
pa_tagstruct *reply;
pa_sink *def_sink;
pa_source *def_source;
pa_sample_spec fixed_ss;
char *h, *u;
pa_core *core;
pa_native_connection_assert_ref(c);
pa_assert(t);
@ -3664,18 +3663,18 @@ static void command_get_server_info(pa_pdispatch *pd, uint32_t command, uint32_t
pa_tagstruct_puts(reply, h);
pa_xfree(h);
fixup_sample_spec(c, &fixed_ss, &c->protocol->core->default_sample_spec);
core = c->protocol->core;
fixup_sample_spec(c, &fixed_ss, &core->default_sample_spec);
pa_tagstruct_put_sample_spec(reply, &fixed_ss);
def_sink = pa_namereg_get_default_sink(c->protocol->core);
pa_tagstruct_puts(reply, def_sink ? def_sink->name : NULL);
def_source = pa_namereg_get_default_source(c->protocol->core);
pa_tagstruct_puts(reply, def_source ? def_source->name : NULL);
pa_tagstruct_puts(reply, core->default_sink ? core->default_sink->name : NULL);
pa_tagstruct_puts(reply, core->default_source ? core->default_source->name : NULL);
pa_tagstruct_putu32(reply, c->protocol->core->cookie);
if (c->version >= 15)
pa_tagstruct_put_channel_map(reply, &c->protocol->core->default_channel_map);
pa_tagstruct_put_channel_map(reply, &core->default_channel_map);
pa_pstream_send_tagstruct(c->pstream, reply);
}
@ -4347,7 +4346,7 @@ static void command_set_default_sink_or_source(pa_pdispatch *pd, uint32_t comman
source = pa_namereg_get(c->protocol->core, s, PA_NAMEREG_SOURCE);
CHECK_VALIDITY(c->pstream, source, tag, PA_ERR_NOENTITY);
pa_namereg_set_default_source(c->protocol->core, source);
pa_core_set_configured_default_source(c->protocol->core, source);
} else {
pa_sink *sink;
pa_assert(command == PA_COMMAND_SET_DEFAULT_SINK);
@ -4355,7 +4354,7 @@ static void command_set_default_sink_or_source(pa_pdispatch *pd, uint32_t comman
sink = pa_namereg_get(c->protocol->core, s, PA_NAMEREG_SINK);
CHECK_VALIDITY(c->pstream, sink, tag, PA_ERR_NOENTITY);
pa_namereg_set_default_sink(c->protocol->core, sink);
pa_core_set_configured_default_sink(c->protocol->core, sink);
}
pa_pstream_send_simple_ack(c->pstream, tag);

View file

@ -660,6 +660,8 @@ void pa_sink_put(pa_sink* s) {
pa_source_put(s->monitor_source);
pa_core_update_default_sink(s->core);
pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK | PA_SUBSCRIPTION_EVENT_NEW, s->index);
pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SINK_PUT], s);
}
@ -690,6 +692,11 @@ void pa_sink_unlink(pa_sink* s) {
pa_namereg_unregister(s->core, s->name);
pa_idxset_remove_by_data(s->core->sinks, s, NULL);
if (s == s->core->configured_default_sink)
pa_core_set_configured_default_sink(s->core, NULL);
else
pa_core_update_default_sink(s->core);
if (s->card)
pa_idxset_remove_by_data(s->card->sinks, s, NULL);

View file

@ -603,6 +603,8 @@ void pa_source_put(pa_source *s) {
else
pa_assert_se(source_set_state(s, PA_SOURCE_IDLE) == 0);
pa_core_update_default_source(s->core);
pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_NEW, s->index);
pa_hook_fire(&s->core->hooks[PA_CORE_HOOK_SOURCE_PUT], s);
}
@ -632,6 +634,11 @@ void pa_source_unlink(pa_source *s) {
pa_namereg_unregister(s->core, s->name);
pa_idxset_remove_by_data(s->core->sources, s, NULL);
if (s == s->core->configured_default_source)
pa_core_set_configured_default_source(s->core, NULL);
else
pa_core_update_default_source(s->core);
if (s->card)
pa_idxset_remove_by_data(s->card->sources, s, NULL);