2011-11-25 15:17:12 +01:00
|
|
|
/***
|
|
|
|
|
This file is part of PulseAudio.
|
|
|
|
|
|
|
|
|
|
Copyright 2004-2006 Lennart Poettering
|
|
|
|
|
Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
|
|
|
|
|
Copyright 2011 David Henningsson, Canonical Ltd.
|
|
|
|
|
|
|
|
|
|
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.1 of the License,
|
|
|
|
|
or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
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
|
2014-11-26 14:14:51 +01:00
|
|
|
along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
|
2011-11-25 15:17:12 +01:00
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
#include "device-port.h"
|
2011-11-25 15:17:14 +01:00
|
|
|
#include <pulsecore/card.h>
|
2015-11-17 15:10:31 +01:00
|
|
|
#include <pulsecore/core-util.h>
|
2011-11-25 15:17:12 +01:00
|
|
|
|
|
|
|
|
PA_DEFINE_PUBLIC_CLASS(pa_device_port, pa_object);
|
|
|
|
|
|
2013-03-28 12:05:11 +01:00
|
|
|
pa_device_port_new_data *pa_device_port_new_data_init(pa_device_port_new_data *data) {
|
|
|
|
|
pa_assert(data);
|
|
|
|
|
|
|
|
|
|
pa_zero(*data);
|
|
|
|
|
data->available = PA_AVAILABLE_UNKNOWN;
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_device_port_new_data_set_name(pa_device_port_new_data *data, const char *name) {
|
|
|
|
|
pa_assert(data);
|
|
|
|
|
|
|
|
|
|
pa_xfree(data->name);
|
|
|
|
|
data->name = pa_xstrdup(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_device_port_new_data_set_description(pa_device_port_new_data *data, const char *description) {
|
|
|
|
|
pa_assert(data);
|
|
|
|
|
|
|
|
|
|
pa_xfree(data->description);
|
|
|
|
|
data->description = pa_xstrdup(description);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_device_port_new_data_set_available(pa_device_port_new_data *data, pa_available_t available) {
|
|
|
|
|
pa_assert(data);
|
|
|
|
|
|
|
|
|
|
data->available = available;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_device_port_new_data_set_direction(pa_device_port_new_data *data, pa_direction_t direction) {
|
|
|
|
|
pa_assert(data);
|
|
|
|
|
|
|
|
|
|
data->direction = direction;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_device_port_new_data_done(pa_device_port_new_data *data) {
|
|
|
|
|
pa_assert(data);
|
|
|
|
|
|
|
|
|
|
pa_xfree(data->name);
|
|
|
|
|
pa_xfree(data->description);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-17 15:10:31 +01:00
|
|
|
void pa_device_port_set_preferred_profile(pa_device_port *p, const char *new_pp) {
|
|
|
|
|
pa_assert(p);
|
|
|
|
|
|
|
|
|
|
if (!pa_safe_streq(p->preferred_profile, new_pp)) {
|
|
|
|
|
pa_xfree(p->preferred_profile);
|
|
|
|
|
p->preferred_profile = pa_xstrdup(new_pp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-18 22:24:24 +02:00
|
|
|
void pa_device_port_set_available(pa_device_port *p, pa_available_t status) {
|
2011-11-25 15:17:14 +01:00
|
|
|
pa_assert(p);
|
|
|
|
|
|
|
|
|
|
if (p->available == status)
|
|
|
|
|
return;
|
|
|
|
|
|
2013-02-18 16:13:24 +01:00
|
|
|
/* pa_assert(status != PA_AVAILABLE_UNKNOWN); */
|
2011-11-25 15:17:14 +01:00
|
|
|
|
|
|
|
|
p->available = status;
|
2013-02-18 16:13:24 +01:00
|
|
|
pa_log_debug("Setting port %s to status %s", p->name, status == PA_AVAILABLE_YES ? "yes" :
|
|
|
|
|
status == PA_AVAILABLE_NO ? "no" : "unknown");
|
2011-11-25 15:17:14 +01:00
|
|
|
|
|
|
|
|
/* Post subscriptions to the card which owns us */
|
2015-05-04 21:03:43 +03:00
|
|
|
/* XXX: We need to check p->card, because this function may be called
|
|
|
|
|
* before the card object has been created. The card object should probably
|
|
|
|
|
* be created before port objects, and then p->card could be non-NULL for
|
|
|
|
|
* the whole lifecycle of pa_device_port. */
|
2017-07-31 09:42:31 +03:00
|
|
|
if (p->card && p->card->linked) {
|
2017-02-16 12:09:39 +02:00
|
|
|
/* A sink or source whose active port is unavailable can't be the
|
|
|
|
|
* default sink/source, so port availability changes may affect the
|
|
|
|
|
* default sink/source choice. */
|
|
|
|
|
if (p->direction == PA_DIRECTION_OUTPUT)
|
|
|
|
|
pa_core_update_default_sink(p->core);
|
|
|
|
|
else
|
|
|
|
|
pa_core_update_default_source(p->core);
|
|
|
|
|
|
2015-05-04 21:03:43 +03:00
|
|
|
pa_subscription_post(p->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE, p->card->index);
|
|
|
|
|
pa_hook_fire(&p->core->hooks[PA_CORE_HOOK_PORT_AVAILABLE_CHANGED], p);
|
|
|
|
|
}
|
2011-11-25 15:17:14 +01:00
|
|
|
}
|
|
|
|
|
|
2011-11-25 15:17:12 +01:00
|
|
|
static void device_port_free(pa_object *o) {
|
|
|
|
|
pa_device_port *p = PA_DEVICE_PORT(o);
|
|
|
|
|
|
|
|
|
|
pa_assert(p);
|
|
|
|
|
pa_assert(pa_device_port_refcnt(p) == 0);
|
|
|
|
|
|
2016-05-03 18:22:08 +05:30
|
|
|
if (p->impl_free)
|
|
|
|
|
p->impl_free(p);
|
|
|
|
|
|
2011-12-02 13:34:54 +01:00
|
|
|
if (p->proplist)
|
|
|
|
|
pa_proplist_free(p->proplist);
|
2013-02-12 21:36:53 +02:00
|
|
|
|
2011-11-25 15:17:13 +01:00
|
|
|
if (p->profiles)
|
2013-09-14 11:50:10 +05:30
|
|
|
pa_hashmap_free(p->profiles);
|
2013-02-12 21:36:53 +02:00
|
|
|
|
2015-11-17 15:10:31 +01:00
|
|
|
pa_xfree(p->preferred_profile);
|
2011-11-25 15:17:12 +01:00
|
|
|
pa_xfree(p->name);
|
|
|
|
|
pa_xfree(p->description);
|
|
|
|
|
pa_xfree(p);
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-28 12:05:11 +01:00
|
|
|
pa_device_port *pa_device_port_new(pa_core *c, pa_device_port_new_data *data, size_t extra) {
|
2011-11-25 15:17:12 +01:00
|
|
|
pa_device_port *p;
|
|
|
|
|
|
2013-03-28 12:05:11 +01:00
|
|
|
pa_assert(data);
|
|
|
|
|
pa_assert(data->name);
|
2013-06-19 18:39:58 +03:00
|
|
|
pa_assert(data->description);
|
2013-03-28 12:05:11 +01:00
|
|
|
pa_assert(data->direction == PA_DIRECTION_OUTPUT || data->direction == PA_DIRECTION_INPUT);
|
2011-11-25 15:17:12 +01:00
|
|
|
|
|
|
|
|
p = PA_DEVICE_PORT(pa_object_new_internal(PA_ALIGN(sizeof(pa_device_port)) + extra, pa_device_port_type_id, pa_device_port_check_type));
|
|
|
|
|
p->parent.free = device_port_free;
|
|
|
|
|
|
2013-03-28 12:05:11 +01:00
|
|
|
p->name = data->name;
|
|
|
|
|
data->name = NULL;
|
|
|
|
|
p->description = data->description;
|
|
|
|
|
data->description = NULL;
|
2011-11-25 15:17:14 +01:00
|
|
|
p->core = c;
|
2013-01-17 20:55:14 +01:00
|
|
|
p->card = NULL;
|
2011-11-25 15:17:12 +01:00
|
|
|
p->priority = 0;
|
2013-03-28 12:05:11 +01:00
|
|
|
p->available = data->available;
|
2012-06-04 17:44:20 +03:00
|
|
|
p->profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
|
2013-03-28 12:05:11 +01:00
|
|
|
p->direction = data->direction;
|
|
|
|
|
|
2012-06-22 20:55:53 +02:00
|
|
|
p->latency_offset = 0;
|
2011-12-02 13:34:54 +01:00
|
|
|
p->proplist = pa_proplist_new();
|
2011-11-25 15:17:12 +01:00
|
|
|
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-27 17:38:42 +02:00
|
|
|
void pa_device_port_set_latency_offset(pa_device_port *p, int64_t offset) {
|
2012-06-22 20:55:54 +02:00
|
|
|
uint32_t state;
|
2012-06-28 13:17:21 +02:00
|
|
|
pa_core *core;
|
2012-06-22 20:55:54 +02:00
|
|
|
|
2012-06-22 20:55:53 +02:00
|
|
|
pa_assert(p);
|
|
|
|
|
|
2013-01-22 08:48:02 +02:00
|
|
|
if (offset == p->latency_offset)
|
|
|
|
|
return;
|
|
|
|
|
|
2012-06-22 20:55:53 +02:00
|
|
|
p->latency_offset = offset;
|
2012-06-22 20:55:54 +02:00
|
|
|
|
2012-06-29 18:04:59 +03:00
|
|
|
switch (p->direction) {
|
|
|
|
|
case PA_DIRECTION_OUTPUT: {
|
|
|
|
|
pa_sink *sink;
|
2012-06-22 20:55:54 +02:00
|
|
|
|
2012-06-29 18:04:59 +03:00
|
|
|
PA_IDXSET_FOREACH(sink, p->core->sinks, state) {
|
|
|
|
|
if (sink->active_port == p) {
|
2016-01-23 12:31:34 +11:00
|
|
|
pa_sink_set_port_latency_offset(sink, p->latency_offset);
|
2012-06-29 18:04:59 +03:00
|
|
|
break;
|
|
|
|
|
}
|
2012-06-22 20:55:54 +02:00
|
|
|
}
|
|
|
|
|
|
2012-06-29 18:04:59 +03:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case PA_DIRECTION_INPUT: {
|
|
|
|
|
pa_source *source;
|
2012-06-22 20:55:54 +02:00
|
|
|
|
2012-06-29 18:04:59 +03:00
|
|
|
PA_IDXSET_FOREACH(source, p->core->sources, state) {
|
|
|
|
|
if (source->active_port == p) {
|
2016-01-23 12:31:34 +11:00
|
|
|
pa_source_set_port_latency_offset(source, p->latency_offset);
|
2012-06-29 18:04:59 +03:00
|
|
|
break;
|
|
|
|
|
}
|
2012-06-22 20:55:54 +02:00
|
|
|
}
|
2012-06-29 18:04:59 +03:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
2012-06-22 20:55:54 +02:00
|
|
|
}
|
2012-06-28 13:17:21 +02:00
|
|
|
|
|
|
|
|
pa_assert_se(core = p->core);
|
2013-01-17 20:55:15 +01:00
|
|
|
pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE, p->card->index);
|
2013-01-17 20:55:17 +01:00
|
|
|
pa_hook_fire(&core->hooks[PA_CORE_HOOK_PORT_LATENCY_OFFSET_CHANGED], p);
|
2012-06-22 20:55:53 +02:00
|
|
|
}
|
2014-03-21 10:19:19 +01:00
|
|
|
|
|
|
|
|
pa_device_port *pa_device_port_find_best(pa_hashmap *ports)
|
|
|
|
|
{
|
|
|
|
|
void *state;
|
|
|
|
|
pa_device_port *p, *best = NULL;
|
|
|
|
|
|
|
|
|
|
if (!ports)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
/* First run: skip unavailable ports */
|
|
|
|
|
PA_HASHMAP_FOREACH(p, ports, state) {
|
|
|
|
|
if (p->available == PA_AVAILABLE_NO)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (!best || p->priority > best->priority)
|
|
|
|
|
best = p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Second run: if only unavailable ports exist, still suggest a port */
|
|
|
|
|
if (!best) {
|
|
|
|
|
PA_HASHMAP_FOREACH(p, ports, state)
|
|
|
|
|
if (!best || p->priority > best->priority)
|
|
|
|
|
best = p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return best;
|
|
|
|
|
}
|