diff options
Diffstat (limited to 'src/modules')
109 files changed, 15704 insertions, 3217 deletions
diff --git a/src/modules/alsa/alsa-mixer.c b/src/modules/alsa/alsa-mixer.c new file mode 100644 index 00000000..a5515e1b --- /dev/null +++ b/src/modules/alsa/alsa-mixer.c @@ -0,0 +1,3406 @@ +/*** + This file is part of PulseAudio. + + Copyright 2004-2009 Lennart Poettering + Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB + + 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 + along with PulseAudio; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <sys/types.h> +#include <limits.h> +#include <asoundlib.h> + +#ifdef HAVE_VALGRIND_MEMCHECK_H +#include <valgrind/memcheck.h> +#endif + +#include <pulse/sample.h> +#include <pulse/xmalloc.h> +#include <pulse/timeval.h> +#include <pulse/util.h> +#include <pulse/i18n.h> +#include <pulse/utf8.h> + +#include <pulsecore/log.h> +#include <pulsecore/macro.h> +#include <pulsecore/core-util.h> +#include <pulsecore/atomic.h> +#include <pulsecore/core-error.h> +#include <pulsecore/once.h> +#include <pulsecore/thread.h> +#include <pulsecore/conf-parser.h> +#include <pulsecore/strbuf.h> + +#include "alsa-mixer.h" +#include "alsa-util.h" + +struct description_map { + const char *name; + const char *description; +}; + +static const char *lookup_description(const char *name, const struct description_map dm[], unsigned n) { + unsigned i; + + for (i = 0; i < n; i++) + if (pa_streq(dm[i].name, name)) + return dm[i].description; + + return NULL; +} + +struct pa_alsa_fdlist { + unsigned num_fds; + struct pollfd *fds; + /* This is a temporary buffer used to avoid lots of mallocs */ + struct pollfd *work_fds; + + snd_mixer_t *mixer; + + pa_mainloop_api *m; + pa_defer_event *defer; + pa_io_event **ios; + + pa_bool_t polled; + + void (*cb)(void *userdata); + void *userdata; +}; + +static void io_cb(pa_mainloop_api*a, pa_io_event* e, int fd, pa_io_event_flags_t events, void *userdata) { + + struct pa_alsa_fdlist *fdl = userdata; + int err; + unsigned i; + unsigned short revents; + + pa_assert(a); + pa_assert(fdl); + pa_assert(fdl->mixer); + pa_assert(fdl->fds); + pa_assert(fdl->work_fds); + + if (fdl->polled) + return; + + fdl->polled = TRUE; + + memcpy(fdl->work_fds, fdl->fds, sizeof(struct pollfd) * fdl->num_fds); + + for (i = 0; i < fdl->num_fds; i++) { + if (e == fdl->ios[i]) { + if (events & PA_IO_EVENT_INPUT) + fdl->work_fds[i].revents |= POLLIN; + if (events & PA_IO_EVENT_OUTPUT) + fdl->work_fds[i].revents |= POLLOUT; + if (events & PA_IO_EVENT_ERROR) + fdl->work_fds[i].revents |= POLLERR; + if (events & PA_IO_EVENT_HANGUP) + fdl->work_fds[i].revents |= POLLHUP; + break; + } + } + + pa_assert(i != fdl->num_fds); + + if ((err = snd_mixer_poll_descriptors_revents(fdl->mixer, fdl->work_fds, fdl->num_fds, &revents)) < 0) { + pa_log_error("Unable to get poll revent: %s", pa_alsa_strerror(err)); + return; + } + + a->defer_enable(fdl->defer, 1); + + if (revents) + snd_mixer_handle_events(fdl->mixer); +} + +static void defer_cb(pa_mainloop_api*a, pa_defer_event* e, void *userdata) { + struct pa_alsa_fdlist *fdl = userdata; + unsigned num_fds, i; + int err, n; + struct pollfd *temp; + + pa_assert(a); + pa_assert(fdl); + pa_assert(fdl->mixer); + + a->defer_enable(fdl->defer, 0); + + if ((n = snd_mixer_poll_descriptors_count(fdl->mixer)) < 0) { + pa_log("snd_mixer_poll_descriptors_count() failed: %s", pa_alsa_strerror(n)); + return; + } + num_fds = (unsigned) n; + + if (num_fds != fdl->num_fds) { + if (fdl->fds) + pa_xfree(fdl->fds); + if (fdl->work_fds) + pa_xfree(fdl->work_fds); + fdl->fds = pa_xnew0(struct pollfd, num_fds); + fdl->work_fds = pa_xnew(struct pollfd, num_fds); + } + + memset(fdl->work_fds, 0, sizeof(struct pollfd) * num_fds); + + if ((err = snd_mixer_poll_descriptors(fdl->mixer, fdl->work_fds, num_fds)) < 0) { + pa_log_error("Unable to get poll descriptors: %s", pa_alsa_strerror(err)); + return; + } + + fdl->polled = FALSE; + + if (memcmp(fdl->fds, fdl->work_fds, sizeof(struct pollfd) * num_fds) == 0) + return; + + if (fdl->ios) { + for (i = 0; i < fdl->num_fds; i++) + a->io_free(fdl->ios[i]); + + if (num_fds != fdl->num_fds) { + pa_xfree(fdl->ios); + fdl->ios = NULL; + } + } + + if (!fdl->ios) + fdl->ios = pa_xnew(pa_io_event*, num_fds); + + /* Swap pointers */ + temp = fdl->work_fds; + fdl->work_fds = fdl->fds; + fdl->fds = temp; + + fdl->num_fds = num_fds; + + for (i = 0;i < num_fds;i++) + fdl->ios[i] = a->io_new(a, fdl->fds[i].fd, + ((fdl->fds[i].events & POLLIN) ? PA_IO_EVENT_INPUT : 0) | + ((fdl->fds[i].events & POLLOUT) ? PA_IO_EVENT_OUTPUT : 0), + io_cb, fdl); +} + +struct pa_alsa_fdlist *pa_alsa_fdlist_new(void) { + struct pa_alsa_fdlist *fdl; + + fdl = pa_xnew0(struct pa_alsa_fdlist, 1); + + return fdl; +} + +void pa_alsa_fdlist_free(struct pa_alsa_fdlist *fdl) { + pa_assert(fdl); + + if (fdl->defer) { + pa_assert(fdl->m); + fdl->m->defer_free(fdl->defer); + } + + if (fdl->ios) { + unsigned i; + pa_assert(fdl->m); + for (i = 0; i < fdl->num_fds; i++) + fdl->m->io_free(fdl->ios[i]); + pa_xfree(fdl->ios); + } + + if (fdl->fds) + pa_xfree(fdl->fds); + if (fdl->work_fds) + pa_xfree(fdl->work_fds); + + pa_xfree(fdl); +} + +int pa_alsa_fdlist_set_mixer(struct pa_alsa_fdlist *fdl, snd_mixer_t *mixer_handle, pa_mainloop_api* m) { + pa_assert(fdl); + pa_assert(mixer_handle); + pa_assert(m); + pa_assert(!fdl->m); + + fdl->mixer = mixer_handle; + fdl->m = m; + fdl->defer = m->defer_new(m, defer_cb, fdl); + + return 0; +} + +static int prepare_mixer(snd_mixer_t *mixer, const char *dev) { + int err; + + pa_assert(mixer); + pa_assert(dev); + + if ((err = snd_mixer_attach(mixer, dev)) < 0) { + pa_log_info("Unable to attach to mixer %s: %s", dev, pa_alsa_strerror(err)); + return -1; + } + + if ((err = snd_mixer_selem_register(mixer, NULL, NULL)) < 0) { + pa_log_warn("Unable to register mixer: %s", pa_alsa_strerror(err)); + return -1; + } + + if ((err = snd_mixer_load(mixer)) < 0) { + pa_log_warn("Unable to load mixer: %s", pa_alsa_strerror(err)); + return -1; + } + + pa_log_info("Successfully attached to mixer '%s'", dev); + return 0; +} + +snd_mixer_t *pa_alsa_open_mixer_for_pcm(snd_pcm_t *pcm, char **ctl_device) { + int err; + snd_mixer_t *m; + const char *dev; + snd_pcm_info_t* info; + snd_pcm_info_alloca(&info); + + pa_assert(pcm); + + if ((err = snd_mixer_open(&m, 0)) < 0) { + pa_log("Error opening mixer: %s", pa_alsa_strerror(err)); + return NULL; + } + + /* First, try by name */ + if ((dev = snd_pcm_name(pcm))) + if (prepare_mixer(m, dev) >= 0) { + if (ctl_device) + *ctl_device = pa_xstrdup(dev); + + return m; + } + + /* Then, try by card index */ + if (snd_pcm_info(pcm, info) >= 0) { + char *md; + int card_idx; + + if ((card_idx = snd_pcm_info_get_card(info)) >= 0) { + + md = pa_sprintf_malloc("hw:%i", card_idx); + + if (!dev || !pa_streq(dev, md)) + if (prepare_mixer(m, md) >= 0) { + + if (ctl_device) + *ctl_device = md; + else + pa_xfree(md); + + return m; + } + + pa_xfree(md); + } + } + + snd_mixer_close(m); + return NULL; +} + +static const snd_mixer_selem_channel_id_t alsa_channel_ids[PA_CHANNEL_POSITION_MAX] = { + [PA_CHANNEL_POSITION_MONO] = SND_MIXER_SCHN_MONO, /* The ALSA name is just an alias! */ + + [PA_CHANNEL_POSITION_FRONT_CENTER] = SND_MIXER_SCHN_FRONT_CENTER, + [PA_CHANNEL_POSITION_FRONT_LEFT] = SND_MIXER_SCHN_FRONT_LEFT, + [PA_CHANNEL_POSITION_FRONT_RIGHT] = SND_MIXER_SCHN_FRONT_RIGHT, + + [PA_CHANNEL_POSITION_REAR_CENTER] = SND_MIXER_SCHN_REAR_CENTER, + [PA_CHANNEL_POSITION_REAR_LEFT] = SND_MIXER_SCHN_REAR_LEFT, + [PA_CHANNEL_POSITION_REAR_RIGHT] = SND_MIXER_SCHN_REAR_RIGHT, + + [PA_CHANNEL_POSITION_LFE] = SND_MIXER_SCHN_WOOFER, + + [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] = SND_MIXER_SCHN_UNKNOWN, + + [PA_CHANNEL_POSITION_SIDE_LEFT] = SND_MIXER_SCHN_SIDE_LEFT, + [PA_CHANNEL_POSITION_SIDE_RIGHT] = SND_MIXER_SCHN_SIDE_RIGHT, + + [PA_CHANNEL_POSITION_AUX0] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX1] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX2] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX3] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX4] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX5] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX6] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX7] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX8] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX9] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX10] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX11] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX12] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX13] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX14] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX15] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX16] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX17] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX18] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX19] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX20] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX21] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX22] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX23] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX24] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX25] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX26] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX27] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX28] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX29] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX30] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_AUX31] = SND_MIXER_SCHN_UNKNOWN, + + [PA_CHANNEL_POSITION_TOP_CENTER] = SND_MIXER_SCHN_UNKNOWN, + + [PA_CHANNEL_POSITION_TOP_FRONT_CENTER] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_TOP_FRONT_LEFT] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_TOP_FRONT_RIGHT] = SND_MIXER_SCHN_UNKNOWN, + + [PA_CHANNEL_POSITION_TOP_REAR_CENTER] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_TOP_REAR_LEFT] = SND_MIXER_SCHN_UNKNOWN, + [PA_CHANNEL_POSITION_TOP_REAR_RIGHT] = SND_MIXER_SCHN_UNKNOWN +}; + +static void setting_free(pa_alsa_setting *s) { + pa_assert(s); + + if (s->options) + pa_idxset_free(s->options, NULL, NULL); + + pa_xfree(s->name); + pa_xfree(s->description); + pa_xfree(s); +} + +static void option_free(pa_alsa_option *o) { + pa_assert(o); + + pa_xfree(o->alsa_name); + pa_xfree(o->name); + pa_xfree(o->description); + pa_xfree(o); +} + +static void element_free(pa_alsa_element *e) { + pa_alsa_option *o; + pa_assert(e); + + while ((o = e->options)) { + PA_LLIST_REMOVE(pa_alsa_option, e->options, o); + option_free(o); + } + + pa_xfree(e->alsa_name); + pa_xfree(e); +} + +void pa_alsa_path_free(pa_alsa_path *p) { + pa_alsa_element *e; + pa_alsa_setting *s; + + pa_assert(p); + + while ((e = p->elements)) { + PA_LLIST_REMOVE(pa_alsa_element, p->elements, e); + element_free(e); + } + + while ((s = p->settings)) { + PA_LLIST_REMOVE(pa_alsa_setting, p->settings, s); + setting_free(s); + } + + pa_xfree(p->name); + pa_xfree(p->description); + pa_xfree(p); +} + +void pa_alsa_path_set_free(pa_alsa_path_set *ps) { + pa_alsa_path *p; + pa_assert(ps); + + while ((p = ps->paths)) { + PA_LLIST_REMOVE(pa_alsa_path, ps->paths, p); + pa_alsa_path_free(p); + } + + pa_xfree(ps); +} + +static long to_alsa_dB(pa_volume_t v) { + return (long) (pa_sw_volume_to_dB(v) * 100.0); +} + +static pa_volume_t from_alsa_dB(long v) { + return pa_sw_volume_from_dB((double) v / 100.0); +} + +static long to_alsa_volume(pa_volume_t v, long min, long max) { + long w; + + w = (long) round(((double) v * (double) (max - min)) / PA_VOLUME_NORM) + min; + return PA_CLAMP_UNLIKELY(w, min, max); +} + +static pa_volume_t from_alsa_volume(long v, long min, long max) { + return (pa_volume_t) round(((double) (v - min) * PA_VOLUME_NORM) / (double) (max - min)); +} + +#define SELEM_INIT(sid, name) \ + do { \ + snd_mixer_selem_id_alloca(&(sid)); \ + snd_mixer_selem_id_set_name((sid), (name)); \ + snd_mixer_selem_id_set_index((sid), 0); \ + } while(FALSE) + +static int element_get_volume(pa_alsa_element *e, snd_mixer_t *m, const pa_channel_map *cm, pa_cvolume *v) { + snd_mixer_selem_id_t *sid; + snd_mixer_elem_t *me; + snd_mixer_selem_channel_id_t c; + pa_channel_position_mask_t mask = 0; + pa_volume_t max_channel_volume = PA_VOLUME_MUTED; + unsigned k; + + pa_assert(m); + pa_assert(e); + pa_assert(cm); + pa_assert(v); + + SELEM_INIT(sid, e->alsa_name); + if (!(me = snd_mixer_find_selem(m, sid))) { + pa_log_warn("Element %s seems to have disappeared.", e->alsa_name); + return -1; + } + + pa_cvolume_mute(v, cm->channels); + + /* We take the highest volume of all channels that match */ + + for (c = 0; c <= SND_MIXER_SCHN_LAST; c++) { + int r; + pa_volume_t f; + + if (e->has_dB) { + long value = 0; + + if (e->direction == PA_ALSA_DIRECTION_OUTPUT) { + if (snd_mixer_selem_has_playback_channel(me, c)) + r = snd_mixer_selem_get_playback_dB(me, c, &value); + else + r = -1; + } else { + if (snd_mixer_selem_has_capture_channel(me, c)) + r = snd_mixer_selem_get_capture_dB(me, c, &value); + else + r = -1; + } + + if (r < 0) + continue; + +#ifdef HAVE_VALGRIND_MEMCHECK_H + VALGRIND_MAKE_MEM_DEFINED(&value, sizeof(value)); +#endif + + f = from_alsa_dB(value); + + } else { + long value = 0; + + if (e->direction == PA_ALSA_DIRECTION_OUTPUT) { + if (snd_mixer_selem_has_playback_channel(me, c)) + r = snd_mixer_selem_get_playback_volume(me, c, &value); + else + r = -1; + } else { + if (snd_mixer_selem_has_capture_channel(me, c)) + r = snd_mixer_selem_get_capture_volume(me, c, &value); + else + r = -1; + } + + if (r < 0) + continue; + + f = from_alsa_volume(value, e->min_volume, e->max_volume); + } + + if (f > max_channel_volume) + max_channel_volume = f; + + for (k = 0; k < cm->channels; k++) + if (e->masks[c][e->n_channels-1] & PA_CHANNEL_POSITION_MASK(cm->map[k])) + if (v->values[k] < f) + v->values[k] = f; + + mask |= e->masks[c][e->n_channels-1]; + } + + for (k = 0; k < cm->channels; k++) + if (!(mask & PA_CHANNEL_POSITION_MASK(cm->map[k]))) + v->values[k] = max_channel_volume; + + return 0; +} + +int pa_alsa_path_get_volume(pa_alsa_path *p, snd_mixer_t *m, const pa_channel_map *cm, pa_cvolume *v) { + pa_alsa_element *e; + + pa_assert(m); + pa_assert(p); + pa_assert(cm); + pa_assert(v); + + if (!p->has_volume) + return -1; + + pa_cvolume_reset(v, cm->channels); + + PA_LLIST_FOREACH(e, p->elements) { + pa_cvolume ev; + + if (e->volume_use != PA_ALSA_VOLUME_MERGE) + continue; + + pa_assert(!p->has_dB || e->has_dB); + + if (element_get_volume(e, m, cm, &ev) < 0) + return -1; + + /* If we have no dB information all we can do is take the first element and leave */ + if (!p->has_dB) { + *v = ev; + return 0; + } + + pa_sw_cvolume_multiply(v, v, &ev); + } + + return 0; +} + +static int element_get_switch(pa_alsa_element *e, snd_mixer_t *m, pa_bool_t *b) { + snd_mixer_selem_id_t *sid; + snd_mixer_elem_t *me; + snd_mixer_selem_channel_id_t c; + + pa_assert(m); + pa_assert(e); + pa_assert(b); + + SELEM_INIT(sid, e->alsa_name); + if (!(me = snd_mixer_find_selem(m, sid))) { + pa_log_warn("Element %s seems to have disappeared.", e->alsa_name); + return -1; + } + + /* We return muted if at least one channel is muted */ + + for (c = 0; c <= SND_MIXER_SCHN_LAST; c++) { + int r; + int value = 0; + + if (e->direction == PA_ALSA_DIRECTION_OUTPUT) { + if (snd_mixer_selem_has_playback_channel(me, c)) + r = snd_mixer_selem_get_playback_switch(me, c, &value); + else + r = -1; + } else { + if (snd_mixer_selem_has_capture_channel(me, c)) + r = snd_mixer_selem_get_capture_switch(me, c, &value); + else + r = -1; + } + + if (r < 0) + continue; + + if (!value) { + *b = FALSE; + return 0; + } + } + + *b = TRUE; + return 0; +} + +int pa_alsa_path_get_mute(pa_alsa_path *p, snd_mixer_t *m, pa_bool_t *muted) { + pa_alsa_element *e; + + pa_assert(m); + pa_assert(p); + pa_assert(muted); + + if (!p->has_mute) + return -1; + + PA_LLIST_FOREACH(e, p->elements) { + pa_bool_t b; + + if (e->switch_use != PA_ALSA_SWITCH_MUTE) + continue; + + if (element_get_switch(e, m, &b) < 0) + return -1; + + if (!b) { + *muted = TRUE; + return 0; + } + } + + *muted = FALSE; + return 0; +} + +static int element_set_volume(pa_alsa_element *e, snd_mixer_t *m, const pa_channel_map *cm, pa_cvolume *v) { + snd_mixer_selem_id_t *sid; + pa_cvolume rv; + snd_mixer_elem_t *me; + snd_mixer_selem_channel_id_t c; + pa_channel_position_mask_t mask = 0; + pa_volume_t max_channel_volume = PA_VOLUME_MUTED; + unsigned k; + + pa_assert(m); + pa_assert(e); + pa_assert(cm); + pa_assert(v); + pa_assert(pa_cvolume_compatible_with_channel_map(v, cm)); + + SELEM_INIT(sid, e->alsa_name); + if (!(me = snd_mixer_find_selem(m, sid))) { + pa_log_warn("Element %s seems to have disappeared.", e->alsa_name); + return -1; + } + + pa_cvolume_mute(&rv, cm->channels); + + for (c = 0; c <= SND_MIXER_SCHN_LAST; c++) { + int r; + pa_volume_t f = PA_VOLUME_MUTED; + pa_bool_t found = FALSE; + + for (k = 0; k < cm->channels; k++) + if (e->masks[c][e->n_channels-1] & PA_CHANNEL_POSITION_MASK(cm->map[k])) { + found = TRUE; + if (v->values[k] > f) + f = v->values[k]; + } + + if (!found) { + /* Hmm, so this channel does not exist in the volume + * struct, so let's bind it to the overall max of the + * volume. */ + f = pa_cvolume_max(v); + } + + if (e->has_dB) { + long value = to_alsa_dB(f); + + if (e->direction == PA_ALSA_DIRECTION_OUTPUT) { + /* If we call set_play_volume() without checking first + * if the channel is available, ALSA behaves ver + * strangely and doesn't fail the call */ + if (snd_mixer_selem_has_playback_channel(me, c)) { + if ((r = snd_mixer_selem_set_playback_dB(me, c, value, +1)) >= 0) + r = snd_mixer_selem_get_playback_dB(me, c, &value); + } else + r = -1; + } else { + if (snd_mixer_selem_has_capture_channel(me, c)) { + if ((r = snd_mixer_selem_set_capture_dB(me, c, value, +1)) >= 0) + r = snd_mixer_selem_get_capture_dB(me, c, &value); + } else + r = -1; + } + + if (r < 0) + continue; + +#ifdef HAVE_VALGRIND_MEMCHECK_H + VALGRIND_MAKE_MEM_DEFINED(&value, sizeof(value)); +#endif + + f = from_alsa_dB(value); + + } else { + long value; + + value = to_alsa_volume(f, e->min_volume, e->max_volume); + + if (e->direction == PA_ALSA_DIRECTION_OUTPUT) { + if (snd_mixer_selem_has_playback_channel(me, c)) { + if ((r = snd_mixer_selem_set_playback_volume(me, c, value)) >= 0) + r = snd_mixer_selem_get_playback_volume(me, c, &value); + } else + r = -1; + } else { + if (snd_mixer_selem_has_capture_channel(me, c)) { + if ((r = snd_mixer_selem_set_capture_volume(me, c, value)) >= 0) + r = snd_mixer_selem_get_capture_volume(me, c, &value); + } else + r = -1; + } + + if (r < 0) + continue; + + f = from_alsa_volume(value, e->min_volume, e->max_volume); + } + + if (f > max_channel_volume) + max_channel_volume = f; + + for (k = 0; k < cm->channels; k++) + if (e->masks[c][e->n_channels-1] & PA_CHANNEL_POSITION_MASK(cm->map[k])) + if (rv.values[k] < f) + rv.values[k] = f; + + mask |= e->masks[c][e->n_channels-1]; + } + + for (k = 0; k < cm->channels; k++) + if (!(mask & PA_CHANNEL_POSITION_MASK(cm->map[k]))) + rv.values[k] = max_channel_volume; + + *v = rv; + return 0; +} + +int pa_alsa_path_set_volume(pa_alsa_path *p, snd_mixer_t *m, const pa_channel_map *cm, pa_cvolume *v) { + pa_alsa_element *e; + pa_cvolume rv; + + pa_assert(m); + pa_assert(p); + pa_assert(cm); + pa_assert(v); + pa_assert(pa_cvolume_compatible_with_channel_map(v, cm)); + + if (!p->has_volume) + return -1; + + rv = *v; /* Remaining adjustment */ + pa_cvolume_reset(v, cm->channels); /* Adjustment done */ + + PA_LLIST_FOREACH(e, p->elements) { + pa_cvolume ev; + + if (e->volume_use != PA_ALSA_VOLUME_MERGE) + continue; + + pa_assert(!p->has_dB || e->has_dB); + + ev = rv; + if (element_set_volume(e, m, cm, &ev) < 0) + return -1; + + if (!p->has_dB) { + *v = ev; + return 0; + } + + pa_sw_cvolume_multiply(v, v, &ev); + pa_sw_cvolume_divide(&rv, &rv, &ev); + } + + return 0; +} + +static int element_set_switch(pa_alsa_element *e, snd_mixer_t *m, pa_bool_t b) { + snd_mixer_elem_t *me; + snd_mixer_selem_id_t *sid; + int r; + + pa_assert(m); + pa_assert(e); + + SELEM_INIT(sid, e->alsa_name); + if (!(me = snd_mixer_find_selem(m, sid))) { + pa_log_warn("Element %s seems to have disappeared.", e->alsa_name); + return -1; + } + + if (e->direction == PA_ALSA_DIRECTION_OUTPUT) + r = snd_mixer_selem_set_playback_switch_all(me, b); + else + r = snd_mixer_selem_set_capture_switch_all(me, b); + + if (r < 0) + pa_log_warn("Failed to set switch of %s: %s", e->alsa_name, pa_alsa_strerror(errno)); + + return r; +} + +int pa_alsa_path_set_mute(pa_alsa_path *p, snd_mixer_t *m, pa_bool_t muted) { + pa_alsa_element *e; + + pa_assert(m); + pa_assert(p); + + if (!p->has_mute) + return -1; + + PA_LLIST_FOREACH(e, p->elements) { + + if (e->switch_use != PA_ALSA_SWITCH_MUTE) + continue; + + if (element_set_switch(e, m, !muted) < 0) + return -1; + } + + return 0; +} + +static int element_mute_volume(pa_alsa_element *e, snd_mixer_t *m) { + snd_mixer_elem_t *me; + snd_mixer_selem_id_t *sid; + int r; + + pa_assert(m); + pa_assert(e); + + SELEM_INIT(sid, e->alsa_name); + if (!(me = snd_mixer_find_selem(m, sid))) { + pa_log_warn("Element %s seems to have disappeared.", e->alsa_name); + return -1; + } + + if (e->direction == PA_ALSA_DIRECTION_OUTPUT) + r = snd_mixer_selem_set_playback_volume_all(me, e->min_volume); + else + r = snd_mixer_selem_set_capture_volume_all(me, e->min_volume); + + if (r < 0) + pa_log_warn("Faile to set volume to muted of %s: %s", e->alsa_name, pa_alsa_strerror(errno)); + + return r; +} + +/* The volume to 0dB */ +static int element_zero_volume(pa_alsa_element *e, snd_mixer_t *m) { + snd_mixer_elem_t *me; + snd_mixer_selem_id_t *sid; + int r; + + pa_assert(m); + pa_assert(e); + + SELEM_INIT(sid, e->alsa_name); + if (!(me = snd_mixer_find_selem(m, sid))) { + pa_log_warn("Element %s seems to have disappeared.", e->alsa_name); + return -1; + } + + if (e->direction == PA_ALSA_DIRECTION_OUTPUT) + r = snd_mixer_selem_set_playback_dB_all(me, 0, +1); + else + r = snd_mixer_selem_set_capture_dB_all(me, 0, +1); + + if (r < 0) + pa_log_warn("Faile to set volume to 0dB of %s: %s", e->alsa_name, pa_alsa_strerror(errno)); + + return r; +} + +int pa_alsa_path_select(pa_alsa_path *p, snd_mixer_t *m) { + pa_alsa_element *e; + int r; + + pa_assert(m); + pa_assert(p); + + pa_log_debug("Activating path %s", p->name); + pa_alsa_path_dump(p); + + PA_LLIST_FOREACH(e, p->elements) { + + switch (e->switch_use) { + case PA_ALSA_SWITCH_MUTE: + case PA_ALSA_SWITCH_OFF: + r = element_set_switch(e, m, FALSE); + break; + + case PA_ALSA_SWITCH_ON: + r = element_set_switch(e, m, TRUE); + break; + + case PA_ALSA_SWITCH_IGNORE: + case PA_ALSA_SWITCH_SELECT: + r = 0; + break; + } + + if (r < 0) + return -1; + + switch (e->volume_use) { + case PA_ALSA_VOLUME_OFF: + case PA_ALSA_VOLUME_MERGE: + r = element_mute_volume(e, m); + break; + + case PA_ALSA_VOLUME_ZERO: + r = element_zero_volume(e, m); + break; + + case PA_ALSA_VOLUME_IGNORE: + r = 0; + break; + } + + if (r < 0) + return -1; + } + + return 0; +} + +static int check_required(pa_alsa_element *e, snd_mixer_elem_t *me) { + pa_bool_t has_switch; + pa_bool_t has_enumeration; + pa_bool_t has_volume; + + pa_assert(e); + pa_assert(me); + + if (e->direction == PA_ALSA_DIRECTION_OUTPUT) { + has_switch = + snd_mixer_selem_has_playback_switch(me) || + (e->direction_try_other && snd_mixer_selem_has_capture_switch(me)); + } else { + has_switch = + snd_mixer_selem_has_capture_switch(me) || + (e->direction_try_other && snd_mixer_selem_has_playback_switch(me)); + } + + if (e->direction == PA_ALSA_DIRECTION_OUTPUT) { + has_volume = + snd_mixer_selem_has_playback_volume(me) || + (e->direction_try_other && snd_mixer_selem_has_capture_volume(me)); + } else { + has_volume = + snd_mixer_selem_has_capture_volume(me) || + (e->direction_try_other && snd_mixer_selem_has_playback_volume(me)); + } + + has_enumeration = snd_mixer_selem_is_enumerated(me); + + if ((e->required == PA_ALSA_REQUIRED_SWITCH && !has_switch) || + (e->required == PA_ALSA_REQUIRED_VOLUME && !has_volume) || + (e->required == PA_ALSA_REQUIRED_ENUMERATION && !has_enumeration)) + return -1; + + if (e->required == PA_ALSA_REQUIRED_ANY && !(has_switch || has_volume || has_enumeration)) + return -1; + + if ((e->required_absent == PA_ALSA_REQUIRED_SWITCH && has_switch) || + (e->required_absent == PA_ALSA_REQUIRED_VOLUME && has_volume) || + (e->required_absent == PA_ALSA_REQUIRED_ENUMERATION && has_enumeration)) + return -1; + + if (e->required_absent == PA_ALSA_REQUIRED_ANY && (has_switch || has_volume || has_enumeration)) + return -1; + + return 0; +} + +static int element_probe(pa_alsa_element *e, snd_mixer_t *m) { + snd_mixer_selem_id_t *sid; + snd_mixer_elem_t *me; + + pa_assert(m); + pa_assert(e); + + SELEM_INIT(sid, e->alsa_name); + + if (!(me = snd_mixer_find_selem(m, sid))) { + + if (e->required != PA_ALSA_REQUIRED_IGNORE) + return -1; + + e->switch_use = PA_ALSA_SWITCH_IGNORE; + e->volume_use = PA_ALSA_VOLUME_IGNORE; + e->enumeration_use = PA_ALSA_VOLUME_IGNORE; + + return 0; + } + + if (e->switch_use != PA_ALSA_SWITCH_IGNORE) { + if (e->direction == PA_ALSA_DIRECTION_OUTPUT) { + + if (!snd_mixer_selem_has_playback_switch(me)) { + if (e->direction_try_other && snd_mixer_selem_has_capture_switch(me)) + e->direction = PA_ALSA_DIRECTION_INPUT; + else + e->switch_use = PA_ALSA_SWITCH_IGNORE; + } + + } else { + + if (!snd_mixer_selem_has_capture_switch(me)) { + if (e->direction_try_other && snd_mixer_selem_has_playback_switch(me)) + e->direction = PA_ALSA_DIRECTION_OUTPUT; + else + e->switch_use = PA_ALSA_SWITCH_IGNORE; + } + } + + if (e->switch_use != PA_ALSA_SWITCH_IGNORE) + e->direction_try_other = FALSE; + } + + if (e->volume_use != PA_ALSA_VOLUME_IGNORE) { + + if (e->direction == PA_ALSA_DIRECTION_OUTPUT) { + + if (!snd_mixer_selem_has_playback_volume(me)) { + if (e->direction_try_other && snd_mixer_selem_has_capture_volume(me)) + e->direction = PA_ALSA_DIRECTION_INPUT; + else + e->volume_use = PA_ALSA_VOLUME_IGNORE; + } + + } else { + + if (!snd_mixer_selem_has_capture_volume(me)) { + if (e->direction_try_other && snd_mixer_selem_has_playback_volume(me)) + e->direction = PA_ALSA_DIRECTION_OUTPUT; + else + e->volume_use = PA_ALSA_VOLUME_IGNORE; + } + } + + if (e->volume_use != PA_ALSA_VOLUME_IGNORE) { + long min_dB = 0, max_dB = 0; + int r; + + e->direction_try_other = FALSE; + + if (e->direction == PA_ALSA_DIRECTION_OUTPUT) + e->has_dB = snd_mixer_selem_get_playback_dB_range(me, &min_dB, &max_dB) >= 0; + else + e->has_dB = snd_mixer_selem_get_capture_dB_range(me, &min_dB, &max_dB) >= 0; + + if (e->has_dB) { +#ifdef HAVE_VALGRIND_MEMCHECK_H + VALGRIND_MAKE_MEM_DEFINED(&min_dB, sizeof(min_dB)); + VALGRIND_MAKE_MEM_DEFINED(&max_dB, sizeof(max_dB)); +#endif + + e->min_dB = ((double) min_dB) / 100.0; + e->max_dB = ((double) max_dB) / 100.0; + + if (min_dB >= max_dB) { + pa_log_warn("Your kernel driver is broken: it reports a volume range from %0.2f dB to %0.2f dB which makes no sense.", e->min_dB, e->max_dB); + e->has_dB = FALSE; + } + } + + if (e->direction == PA_ALSA_DIRECTION_OUTPUT) + r = snd_mixer_selem_get_playback_volume_range(me, &e->min_volume, &e->max_volume); + else + r = snd_mixer_selem_get_capture_volume_range(me, &e->min_volume, &e->max_volume); + + if (r < 0) { + pa_log_warn("Failed to get volume range of %s: %s", e->alsa_name, pa_alsa_strerror(r)); + return -1; + } + + + if (e->min_volume >= e->max_volume) { + pa_log_warn("Your kernel driver is broken: it reports a volume range from %li to %li which makes no sense.", e->min_volume, e->max_volume); + e->volume_use = PA_ALSA_VOLUME_IGNORE; + + } else { + pa_bool_t is_mono; + pa_channel_position_t p; + + if (e->direction == PA_ALSA_DIRECTION_OUTPUT) + is_mono = snd_mixer_selem_is_playback_mono(me) > 0; + else + is_mono = snd_mixer_selem_is_capture_mono(me) > 0; + + if (is_mono) { + e->n_channels = 1; + + if (!e->override_map) { + for (p = PA_CHANNEL_POSITION_FRONT_LEFT; p < PA_CHANNEL_POSITION_MAX; p++) + e->masks[alsa_channel_ids[p]][e->n_channels-1] = 0; + e->masks[SND_MIXER_SCHN_MONO][e->n_channels-1] = PA_CHANNEL_POSITION_MASK_ALL; + } + + e->merged_mask = e->masks[SND_MIXER_SCHN_MONO][e->n_channels-1]; + } else { + e->n_channels = 0; + for (p = PA_CHANNEL_POSITION_FRONT_LEFT; p < PA_CHANNEL_POSITION_MAX; p++) { + + if (alsa_channel_ids[p] == SND_MIXER_SCHN_UNKNOWN) + continue; + + if (e->direction == PA_ALSA_DIRECTION_OUTPUT) + e->n_channels += snd_mixer_selem_has_playback_channel(me, alsa_channel_ids[p]) > 0; + else + e->n_channels += snd_mixer_selem_has_capture_channel(me, alsa_channel_ids[p]) > 0; + } + + if (e->n_channels <= 0) { + pa_log_warn("Volume element %s with no channels?", e->alsa_name); + return -1; + } + + if (!e->override_map) { + for (p = PA_CHANNEL_POSITION_FRONT_LEFT; p < PA_CHANNEL_POSITION_MAX; p++) { + pa_bool_t has_channel; + + if (alsa_channel_ids[p] == SND_MIXER_SCHN_UNKNOWN) + continue; + + if (e->direction == PA_ALSA_DIRECTION_OUTPUT) + has_channel = snd_mixer_selem_has_playback_channel(me, alsa_channel_ids[p]) > 0; + else + has_channel = snd_mixer_selem_has_capture_channel(me, alsa_channel_ids[p]) > 0; + + e->masks[alsa_channel_ids[p]][e->n_channels-1] = has_channel ? PA_CHANNEL_POSITION_MASK(p) : 0; + } + } + + e->merged_mask = 0; + for (p = PA_CHANNEL_POSITION_FRONT_LEFT; p < PA_CHANNEL_POSITION_MAX; p++) + e->merged_mask |= e->masks[alsa_channel_ids[p]][e->n_channels-1]; + } + } + } + + } + + if (check_required(e, me) < 0) + return -1; + + if (e->switch_use == PA_ALSA_SWITCH_SELECT) { + pa_alsa_option *o; + + PA_LLIST_FOREACH(o, e->options) + o->alsa_idx = pa_streq(o->alsa_name, "on") ? 1 : 0; + } else if (e->enumeration_use == PA_ALSA_ENUMERATION_SELECT) { + int n; + pa_alsa_option *o; + + if ((n = snd_mixer_selem_get_enum_items(me)) < 0) { + pa_log("snd_mixer_selem_get_enum_items() failed: %s", pa_alsa_strerror(n)); + return -1; + } + + PA_LLIST_FOREACH(o, e->options) { + int i; + + for (i = 0; i < n; i++) { + char buf[128]; + + if (snd_mixer_selem_get_enum_item_name(me, i, sizeof(buf), buf) < 0) + continue; + + if (!pa_streq(buf, o->alsa_name)) + continue; + + o->alsa_idx = i; + } + } + } + + return 0; +} + +static pa_alsa_element* element_get(pa_alsa_path *p, const char *section, pa_bool_t prefixed) { + pa_alsa_element *e; + + pa_assert(p); + pa_assert(section); + + if (prefixed) { + if (!pa_startswith(section, "Element ")) + return NULL; + + section += 8; + } + + /* This is not an element section, but an enum section? */ + if (strchr(section, ':')) + return NULL; + + if (p->last_element && pa_streq(p->last_element->alsa_name, section)) + return p->last_element; + + PA_LLIST_FOREACH(e, p->elements) + if (pa_streq(e->alsa_name, section)) + goto finish; + + e = pa_xnew0(pa_alsa_element, 1); + e->path = p; + e->alsa_name = pa_xstrdup(section); + e->direction = p->direction; + + PA_LLIST_INSERT_AFTER(pa_alsa_element, p->elements, p->last_element, e); + +finish: + p->last_element = e; + return e; +} + +static pa_alsa_option* option_get(pa_alsa_path *p, const char *section) { + char *en; + const char *on; + pa_alsa_option *o; + pa_alsa_element *e; + + if (!pa_startswith(section, "Option ")) + return NULL; + + section += 7; + + /* This is not an enum section, but an element section? */ + if (!(on = strchr(section, ':'))) + return NULL; + + en = pa_xstrndup(section, on - section); + on++; + + if (p->last_option && + pa_streq(p->last_option->element->alsa_name, en) && + pa_streq(p->last_option->alsa_name, on)) { + pa_xfree(en); + return p->last_option; + } + + pa_assert_se(e = element_get(p, en, FALSE)); + pa_xfree(en); + + PA_LLIST_FOREACH(o, e->options) + if (pa_streq(o->alsa_name, on)) + goto finish; + + o = pa_xnew0(pa_alsa_option, 1); + o->element = e; + o->alsa_name = pa_xstrdup(on); + o->alsa_idx = -1; + + if (p->last_option && p->last_option->element == e) + PA_LLIST_INSERT_AFTER(pa_alsa_option, e->options, p->last_option, o); + else + PA_LLIST_PREPEND(pa_alsa_option, e->options, o); + +finish: + p->last_option = o; + return o; +} + +static int element_parse_switch( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + const char *rvalue, + void *data, + void *userdata) { + + pa_alsa_path *p = userdata; + pa_alsa_element *e; + + pa_assert(p); + + if (!(e = element_get(p, section, TRUE))) { + pa_log("[%s:%u] Switch makes no sense in '%s'", filename, line, section); + return -1; + } + + if (pa_streq(rvalue, "ignore")) + e->switch_use = PA_ALSA_SWITCH_IGNORE; + else if (pa_streq(rvalue, "mute")) + e->switch_use = PA_ALSA_SWITCH_MUTE; + else if (pa_streq(rvalue, "off")) + e->switch_use = PA_ALSA_SWITCH_OFF; + else if (pa_streq(rvalue, "on")) + e->switch_use = PA_ALSA_SWITCH_ON; + else if (pa_streq(rvalue, "select")) + e->switch_use = PA_ALSA_SWITCH_SELECT; + else { + pa_log("[%s:%u] Switch invalid of '%s'", filename, line, section); + return -1; + } + + return 0; +} + +static int element_parse_volume( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + const char *rvalue, + void *data, + void *userdata) { + + pa_alsa_path *p = userdata; + pa_alsa_element *e; + + pa_assert(p); + + if (!(e = element_get(p, section, TRUE))) { + pa_log("[%s:%u] Volume makes no sense in '%s'", filename, line, section); + return -1; + } + + if (pa_streq(rvalue, "ignore")) + e->volume_use = PA_ALSA_VOLUME_IGNORE; + else if (pa_streq(rvalue, "merge")) + e->volume_use = PA_ALSA_VOLUME_MERGE; + else if (pa_streq(rvalue, "off")) + e->volume_use = PA_ALSA_VOLUME_OFF; + else if (pa_streq(rvalue, "zero")) + e->volume_use = PA_ALSA_VOLUME_ZERO; + else { + pa_log("[%s:%u] Volume invalid of '%s'", filename, line, section); + return -1; + } + + return 0; +} + +static int element_parse_enumeration( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + const char *rvalue, + void *data, + void *userdata) { + + pa_alsa_path *p = userdata; + pa_alsa_element *e; + + pa_assert(p); + + if (!(e = element_get(p, section, TRUE))) { + pa_log("[%s:%u] Enumeration makes no sense in '%s'", filename, line, section); + return -1; + } + + if (pa_streq(rvalue, "ignore")) + e->enumeration_use = PA_ALSA_ENUMERATION_IGNORE; + else if (pa_streq(rvalue, "select")) + e->enumeration_use = PA_ALSA_ENUMERATION_SELECT; + else { + pa_log("[%s:%u] Enumeration invalid of '%s'", filename, line, section); + return -1; + } + + return 0; +} + +static int option_parse_priority( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + const char *rvalue, + void *data, + void *userdata) { + + pa_alsa_path *p = userdata; + pa_alsa_option *o; + uint32_t prio; + + pa_assert(p); + + if (!(o = option_get(p, section))) { + pa_log("[%s:%u] Priority makes no sense in '%s'", filename, line, section); + return -1; + } + + if (pa_atou(rvalue, &prio) < 0) { + pa_log("[%s:%u] Priority invalid of '%s'", filename, line, section); + return -1; + } + + o->priority = prio; + return 0; +} + +static int option_parse_name( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + const char *rvalue, + void *data, + void *userdata) { + + pa_alsa_path *p = userdata; + pa_alsa_option *o; + + pa_assert(p); + + if (!(o = option_get(p, section))) { + pa_log("[%s:%u] Name makes no sense in '%s'", filename, line, section); + return -1; + } + + pa_xfree(o->name); + o->name = pa_xstrdup(rvalue); + + return 0; +} + +static int element_parse_required( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + const char *rvalue, + void *data, + void *userdata) { + + pa_alsa_path *p = userdata; + pa_alsa_element *e; + pa_alsa_required_t req; + + pa_assert(p); + + if (!(e = element_get(p, section, TRUE))) { + pa_log("[%s:%u] Required makes no sense in '%s'", filename, line, section); + return -1; + } + + if (pa_streq(rvalue, "ignore")) + req = PA_ALSA_REQUIRED_IGNORE; + else if (pa_streq(rvalue, "switch")) + req = PA_ALSA_REQUIRED_SWITCH; + else if (pa_streq(rvalue, "volume")) + req = PA_ALSA_REQUIRED_VOLUME; + else if (pa_streq(rvalue, "enumeration")) + req = PA_ALSA_REQUIRED_ENUMERATION; + else if (pa_streq(rvalue, "any")) + req = PA_ALSA_REQUIRED_ANY; + else { + pa_log("[%s:%u] Required invalid of '%s'", filename, line, section); + return -1; + } + + if (pa_streq(lvalue, "required-absent")) + e->required_absent = req; + else + e->required = req; + + return 0; +} + +static int element_parse_direction( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + const char *rvalue, + void *data, + void *userdata) { + + pa_alsa_path *p = userdata; + pa_alsa_element *e; + + pa_assert(p); + + if (!(e = element_get(p, section, TRUE))) { + pa_log("[%s:%u] Direction makes no sense in '%s'", filename, line, section); + return -1; + } + + if (pa_streq(rvalue, "playback")) + e->direction = PA_ALSA_DIRECTION_OUTPUT; + else if (pa_streq(rvalue, "capture")) + e->direction = PA_ALSA_DIRECTION_INPUT; + else { + pa_log("[%s:%u] Direction invalid of '%s'", filename, line, section); + return -1; + } + + return 0; +} + +static int element_parse_direction_try_other( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + const char *rvalue, + void *data, + void *userdata) { + + pa_alsa_path *p = userdata; + pa_alsa_element *e; + int yes; + + if (!(e = element_get(p, section, TRUE))) { + pa_log("[%s:%u] Direction makes no sense in '%s'", filename, line, section); + return -1; + } + + if ((yes = pa_parse_boolean(rvalue)) < 0) { + pa_log("[%s:%u] Direction invalid of '%s'", filename, line, section); + return -1; + } + + e->direction_try_other = !!yes; + return 0; +} + +static pa_channel_position_mask_t parse_mask(const char *m) { + pa_channel_position_mask_t v; + + if (pa_streq(m, "all-left")) + v = PA_CHANNEL_POSITION_MASK_LEFT; + else if (pa_streq(m, "all-right")) + v = PA_CHANNEL_POSITION_MASK_RIGHT; + else if (pa_streq(m, "all-center")) + v = PA_CHANNEL_POSITION_MASK_CENTER; + else if (pa_streq(m, "all-front")) + v = PA_CHANNEL_POSITION_MASK_FRONT; + else if (pa_streq(m, "all-rear")) + v = PA_CHANNEL_POSITION_MASK_REAR; + else if (pa_streq(m, "all-side")) + v = PA_CHANNEL_POSITION_MASK_SIDE_OR_TOP_CENTER; + else if (pa_streq(m, "all-top")) + v = PA_CHANNEL_POSITION_MASK_TOP; + else if (pa_streq(m, "all-no-lfe")) + v = PA_CHANNEL_POSITION_MASK_ALL ^ PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_LFE); + else if (pa_streq(m, "all")) + v = PA_CHANNEL_POSITION_MASK_ALL; + else { + pa_channel_position_t p; + + if ((p = pa_channel_position_from_string(m)) == PA_CHANNEL_POSITION_INVALID) + return 0; + + v = PA_CHANNEL_POSITION_MASK(p); + } + + return v; +} + +static int element_parse_override_map( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + const char *rvalue, + void *data, + void *userdata) { + + pa_alsa_path *p = userdata; + pa_alsa_element *e; + const char *state = NULL; + unsigned i = 0; + char *n; + + if (!(e = element_get(p, section, TRUE))) { + pa_log("[%s:%u] Override map makes no sense in '%s'", filename, line, section); + return -1; + } + + while ((n = pa_split(rvalue, ",", &state))) { + pa_channel_position_mask_t m; + + if (!*n) + m = 0; + else { + if ((m = parse_mask(n)) == 0) { + pa_log("[%s:%u] Override map '%s' invalid in '%s'", filename, line, n, section); + pa_xfree(n); + return -1; + } + } + + if (pa_streq(lvalue, "override-map.1")) + e->masks[i++][0] = m; + else + e->masks[i++][1] = m; + + /* Later on we might add override-map.3 and so on here ... */ + + pa_xfree(n); + } + + e->override_map = TRUE; + + return 0; +} + +static int element_set_option(pa_alsa_element *e, snd_mixer_t *m, int alsa_idx) { + snd_mixer_selem_id_t *sid; + snd_mixer_elem_t *me; + int r; + + pa_assert(e); + pa_assert(m); + + SELEM_INIT(sid, e->alsa_name); + if (!(me = snd_mixer_find_selem(m, sid))) { + pa_log_warn("Element %s seems to have disappeared.", e->alsa_name); + return -1; + } + + if (e->switch_use == PA_ALSA_SWITCH_SELECT) { + + if (e->direction == PA_ALSA_DIRECTION_OUTPUT) + r = snd_mixer_selem_set_playback_switch_all(me, alsa_idx); + else + r = snd_mixer_selem_set_capture_switch_all(me, alsa_idx); + + if (r < 0) + pa_log_warn("Faile to set switch of %s: %s", e->alsa_name, pa_alsa_strerror(errno)); + + } else { + pa_assert(e->enumeration_use == PA_ALSA_ENUMERATION_SELECT); + + if ((r = snd_mixer_selem_set_enum_item(me, 0, alsa_idx)) < 0) + pa_log_warn("Faile to set enumeration of %s: %s", e->alsa_name, pa_alsa_strerror(errno)); + } + + return r; +} + +int pa_alsa_setting_select(pa_alsa_setting *s, snd_mixer_t *m) { + pa_alsa_option *o; + uint32_t idx; + + pa_assert(s); + pa_assert(m); + + PA_IDXSET_FOREACH(o, s->options, idx) + element_set_option(o->element, m, o->alsa_idx); + + return 0; +} + +static int option_verify(pa_alsa_option *o) { + static const struct description_map well_known_descriptions[] = { + { "input", N_("Input") }, + { "input-docking", N_("Docking Station Input") }, + { "input-docking-microphone", N_("Docking Station Microphone") }, + { "input-linein", N_("Line-In") }, + { "input-microphone", N_("Microphone") }, + { "input-microphone-external", N_("External Microphone") }, + { "input-microphone-internal", N_("Internal Microphone") }, + { "input-radio", N_("Radio") }, + { "input-video", N_("Video") }, + { "input-agc-on", N_("Automatic Gain Control") }, + { "input-agc-off", "" }, + { "input-boost-on", N_("Boost") }, + { "input-boost-off", "" }, + { "output-amplifier-on", N_("Amplifier") }, + { "output-amplifier-off", "" } + }; + + pa_assert(o); + + if (!o->name) { + pa_log("No name set for option %s", o->alsa_name); + return -1; + } + + if (o->element->enumeration_use != PA_ALSA_ENUMERATION_SELECT && + o->element->switch_use != PA_ALSA_SWITCH_SELECT) { + pa_log("Element %s of option %s not set for select.", o->element->alsa_name, o->name); + return -1; + } + + if (o->element->switch_use == PA_ALSA_SWITCH_SELECT && + !pa_streq(o->alsa_name, "on") && + !pa_streq(o->alsa_name, "off")) { + pa_log("Switch %s options need be named off or on ", o->element->alsa_name); + return -1; + } + + if (!o->description) + o->description = pa_xstrdup(lookup_description(o->name, + well_known_descriptions, + PA_ELEMENTSOF(well_known_descriptions))); + if (!o->description) + o->description = pa_xstrdup(o->name); + + return 0; +} + +static int element_verify(pa_alsa_element *e) { + pa_alsa_option *o; + + pa_assert(e); + + if ((e->required != PA_ALSA_REQUIRED_IGNORE && e->required == e->required_absent) || + (e->required_absent == PA_ALSA_REQUIRED_ANY && e->required != PA_ALSA_REQUIRED_IGNORE)) { + pa_log("Element %s cannot be required and absent at the same time.", e->alsa_name); + return -1; + } + + if (e->switch_use == PA_ALSA_SWITCH_SELECT && e->enumeration_use == PA_ALSA_ENUMERATION_SELECT) { + pa_log("Element %s cannot set select for both switch and enumeration.", e->alsa_name); + return -1; + } + + PA_LLIST_FOREACH(o, e->options) + if (option_verify(o) < 0) + return -1; + + return 0; +} + +static int path_verify(pa_alsa_path *p) { + static const struct description_map well_known_descriptions[] = { + { "analog-input", N_("Analog Input") }, + { "analog-input-microphone", N_("Analog Microphone") }, + { "analog-input-linein", N_("Analog Line-In") }, + { "analog-input-radio", N_("Analog Radio") }, + { "analog-input-video", N_("Analog Video") }, + { "analog-output", N_("Analog Output") }, + { "analog-output-headphones", N_("Analog Headphones") }, + { "analog-output-lfe-on-mono", N_("Analog Output (LFE)") }, + { "analog-output-mono", N_("Analog Mono Output") } + }; + + pa_alsa_element *e; + + pa_assert(p); + + PA_LLIST_FOREACH(e, p->elements) + if (element_verify(e) < 0) + return -1; + + if (!p->description) + p->description = pa_xstrdup(lookup_description(p->name, + well_known_descriptions, + PA_ELEMENTSOF(well_known_descriptions))); + + if (!p->description) + p->description = pa_xstrdup(p->name); + + return 0; +} + +pa_alsa_path* pa_alsa_path_new(const char *fname, pa_alsa_direction_t direction) { + pa_alsa_path *p; + char *fn; + int r; + const char *n; + + pa_config_item items[] = { + /* [General] */ + { "priority", pa_config_parse_unsigned, NULL, "General" }, + { "description", pa_config_parse_string, NULL, "General" }, + { "name", pa_config_parse_string, NULL, "General" }, + + /* [Option ...] */ + { "priority", option_parse_priority, NULL, NULL }, + { "name", option_parse_name, NULL, NULL }, + + /* [Element ...] */ + { "switch", element_parse_switch, NULL, NULL }, + { "volume", element_parse_volume, NULL, NULL }, + { "enumeration", element_parse_enumeration, NULL, NULL }, + { "override-map.1", element_parse_override_map, NULL, NULL }, + { "override-map.2", element_parse_override_map, NULL, NULL }, + /* ... later on we might add override-map.3 and so on here ... */ + { "required", element_parse_required, NULL, NULL }, + { "required-absent", element_parse_required, NULL, NULL }, + { "direction", element_parse_direction, NULL, NULL }, + { "direction-try-other", element_parse_direction_try_other, NULL, NULL }, + { NULL, NULL, NULL, NULL } + }; + + pa_assert(fname); + + p = pa_xnew0(pa_alsa_path, 1); + n = pa_path_get_filename(fname); + p->name = pa_xstrndup(n, strcspn(n, ".")); + p->direction = direction; + + items[0].data = &p->priority; + items[1].data = &p->description; + items[2].data = &p->name; + + fn = pa_maybe_prefix_path(fname, PA_ALSA_PATHS_DIR); + r = pa_config_parse(fn, NULL, items, p); + pa_xfree(fn); + + if (r < 0) + goto fail; + + if (path_verify(p) < 0) + goto fail; + + return p; + +fail: + pa_alsa_path_free(p); + return NULL; +} + +pa_alsa_path* pa_alsa_path_synthesize(const char*element, pa_alsa_direction_t direction) { + pa_alsa_path *p; + pa_alsa_element *e; + + pa_assert(element); + + p = pa_xnew0(pa_alsa_path, 1); + p->name = pa_xstrdup(element); + p->direction = direction; + + e = pa_xnew0(pa_alsa_element, 1); + e->path = p; + e->alsa_name = pa_xstrdup(element); + e->direction = direction; + + e->switch_use = PA_ALSA_SWITCH_MUTE; + e->volume_use = PA_ALSA_VOLUME_MERGE; + + PA_LLIST_PREPEND(pa_alsa_element, p->elements, e); + return p; +} + +static pa_bool_t element_drop_unsupported(pa_alsa_element *e) { + pa_alsa_option *o, *n; + + pa_assert(e); + + for (o = e->options; o; o = n) { + n = o->next; + + if (o->alsa_idx < 0) { + PA_LLIST_REMOVE(pa_alsa_option, e->options, o); + option_free(o); + } + } + + return + e->switch_use != PA_ALSA_SWITCH_IGNORE || + e->volume_use != PA_ALSA_VOLUME_IGNORE || + e->enumeration_use != PA_ALSA_ENUMERATION_IGNORE; +} + +static void path_drop_unsupported(pa_alsa_path *p) { + pa_alsa_element *e, *n; + + pa_assert(p); + + for (e = p->elements; e; e = n) { + n = e->next; + + if (!element_drop_unsupported(e)) { + PA_LLIST_REMOVE(pa_alsa_element, p->elements, e); + element_free(e); + } + } +} + +static void path_make_options_unique(pa_alsa_path *p) { + pa_alsa_element *e; + pa_alsa_option *o, *u; + + PA_LLIST_FOREACH(e, p->elements) { + PA_LLIST_FOREACH(o, e->options) { + unsigned i; + char *m; + + for (u = o->next; u; u = u->next) + if (pa_streq(u->name, o->name)) + break; + + if (!u) + continue; + + m = pa_xstrdup(o->name); + + /* OK, this name is not unique, hence let's rename */ + for (i = 1, u = o; u; u = u->next) { + char *nn, *nd; + + if (!pa_streq(u->name, m)) + continue; + + nn = pa_sprintf_malloc("%s-%u", m, i); + pa_xfree(u->name); + u->name = nn; + + nd = pa_sprintf_malloc("%s %u", u->description, i); + pa_xfree(u->description); + u->description = nd; + + i++; + } + + pa_xfree(m); + } + } +} + +static pa_bool_t element_create_settings(pa_alsa_element *e, pa_alsa_setting *template) { + pa_alsa_option *o; + + for (; e; e = e->next) + if (e->switch_use == PA_ALSA_SWITCH_SELECT || + e->enumeration_use == PA_ALSA_ENUMERATION_SELECT) + break; + + if (!e) + return FALSE; + + for (o = e->options; o; o = o->next) { + pa_alsa_setting *s; + + if (template) { + s = pa_xnewdup(pa_alsa_setting, template, 1); + s->options = pa_idxset_copy(template->options); + s->name = pa_sprintf_malloc(_("%s+%s"), template->name, o->name); + s->description = + (template->description[0] && o->description[0]) + ? pa_sprintf_malloc(_("%s / %s"), template->description, o->description) + : (template->description[0] + ? pa_xstrdup(template->description) + : pa_xstrdup(o->description)); + + s->priority = PA_MAX(template->priority, o->priority); + } else { + s = pa_xnew0(pa_alsa_setting, 1); + s->options = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func); + s->name = pa_xstrdup(o->name); + s->description = pa_xstrdup(o->description); + s->priority = o->priority; + } + + pa_idxset_put(s->options, o, NULL); + + if (element_create_settings(e->next, s)) + /* This is not a leaf, so let's get rid of it */ + setting_free(s); + else { + /* This is a leaf, so let's add it */ + PA_LLIST_INSERT_AFTER(pa_alsa_setting, e->path->settings, e->path->last_setting, s); + + e->path->last_setting = s; + } + } + + return TRUE; +} + +static void path_create_settings(pa_alsa_path *p) { + pa_assert(p); + + element_create_settings(p->elements, NULL); +} + +int pa_alsa_path_probe(pa_alsa_path *p, snd_mixer_t *m, pa_bool_t ignore_dB) { + pa_alsa_element *e; + double min_dB[PA_CHANNEL_POSITION_MAX], max_dB[PA_CHANNEL_POSITION_MAX]; + pa_channel_position_t t; + + pa_assert(p); + pa_assert(m); + + if (p->probed) + return 0; + + pa_zero(min_dB); + pa_zero(max_dB); + + pa_log_debug("Probing path '%s'", p->name); + + PA_LLIST_FOREACH(e, p->elements) { + if (element_probe(e, m) < 0) { + p->supported = FALSE; + pa_log_debug("Probe of element '%s' failed.", e->alsa_name); + return -1; + } + + if (ignore_dB) + e->has_dB = FALSE; + + if (e->volume_use == PA_ALSA_VOLUME_MERGE) { + + if (!p->has_volume) { + p->min_volume = e->min_volume; + p->max_volume = e->max_volume; + } + + if (e->has_dB) { + if (!p->has_volume) { + for (t = 0; t < PA_CHANNEL_POSITION_MAX; t++) + if (PA_CHANNEL_POSITION_MASK(t) & e->merged_mask) { + min_dB[t] = e->min_dB; + max_dB[t] = e->max_dB; + } + + p->has_dB = TRUE; + } else { + + if (p->has_dB) { + for (t = 0; t < PA_CHANNEL_POSITION_MAX; t++) + if (PA_CHANNEL_POSITION_MASK(t) & e->merged_mask) { + min_dB[t] += e->min_dB; + max_dB[t] += e->max_dB; + } + } else + /* Hmm, there's another element before us + * which cannot do dB volumes, so we we need + * to 'neutralize' this slider */ + e->volume_use = PA_ALSA_VOLUME_ZERO; + } + } else if (p->has_volume) + /* We can't use this volume, so let's ignore it */ + e->volume_use = PA_ALSA_VOLUME_IGNORE; + + p->has_volume = TRUE; + } + + if (e->switch_use == PA_ALSA_SWITCH_MUTE) + p->has_mute = TRUE; + } + + path_drop_unsupported(p); + path_make_options_unique(p); + path_create_settings(p); + + p->supported = TRUE; + p->probed = TRUE; + + p->min_dB = INFINITY; + p->max_dB = -INFINITY; + + for (t = 0; t < PA_CHANNEL_POSITION_MAX; t++) { + if (p->min_dB > min_dB[t]) + p->min_dB = min_dB[t]; + + if (p->max_dB < max_dB[t]) + p->max_dB = max_dB[t]; + } + + return 0; +} + +void pa_alsa_setting_dump(pa_alsa_setting *s) { + pa_assert(s); + + pa_log_debug("Setting %s (%s) priority=%u", + s->name, + pa_strnull(s->description), + s->priority); +} + +void pa_alsa_option_dump(pa_alsa_option *o) { + pa_assert(o); + + pa_log_debug("Option %s (%s/%s) index=%i, priority=%u", + o->alsa_name, + pa_strnull(o->name), + pa_strnull(o->description), + o->alsa_idx, + o->priority); +} + +void pa_alsa_element_dump(pa_alsa_element *e) { + pa_alsa_option *o; + pa_assert(e); + + pa_log_debug("Element %s, direction=%i, switch=%i, volume=%i, enumeration=%i, required=%i, required_absent=%i, mask=0x%llx, n_channels=%u, override_map=%s", + e->alsa_name, + e->direction, + e->switch_use, + e->volume_use, + e->enumeration_use, + e->required, + e->required_absent, + (long long unsigned) e->merged_mask, + e->n_channels, + pa_yes_no(e->override_map)); + + PA_LLIST_FOREACH(o, e->options) + pa_alsa_option_dump(o); +} + +void pa_alsa_path_dump(pa_alsa_path *p) { + pa_alsa_element *e; + pa_alsa_setting *s; + pa_assert(p); + + pa_log_debug("Path %s (%s), direction=%i, priority=%u, probed=%s, supported=%s, has_mute=%s, has_volume=%s, " + "has_dB=%s, min_volume=%li, max_volume=%li, min_dB=%g, max_dB=%g", + p->name, + pa_strnull(p->description), + p->direction, + p->priority, + pa_yes_no(p->probed), + pa_yes_no(p->supported), + pa_yes_no(p->has_mute), + pa_yes_no(p->has_volume), + pa_yes_no(p->has_dB), + p->min_volume, p->max_volume, + p->min_dB, p->max_dB); + + PA_LLIST_FOREACH(e, p->elements) + pa_alsa_element_dump(e); + + PA_LLIST_FOREACH(s, p->settings) + pa_alsa_setting_dump(s); +} + +static void element_set_callback(pa_alsa_element *e, snd_mixer_t *m, snd_mixer_elem_callback_t cb, void *userdata) { + snd_mixer_selem_id_t *sid; + snd_mixer_elem_t *me; + + pa_assert(e); + pa_assert(m); + pa_assert(cb); + + SELEM_INIT(sid, e->alsa_name); + if (!(me = snd_mixer_find_selem(m, sid))) { + pa_log_warn("Element %s seems to have disappeared.", e->alsa_name); + return; + } + + snd_mixer_elem_set_callback(me, cb); + snd_mixer_elem_set_callback_private(me, userdata); +} + +void pa_alsa_path_set_callback(pa_alsa_path *p, snd_mixer_t *m, snd_mixer_elem_callback_t cb, void *userdata) { + pa_alsa_element *e; + + pa_assert(p); + pa_assert(m); + pa_assert(cb); + + PA_LLIST_FOREACH(e, p->elements) + element_set_callback(e, m, cb, userdata); +} + +void pa_alsa_path_set_set_callback(pa_alsa_path_set *ps, snd_mixer_t *m, snd_mixer_elem_callback_t cb, void *userdata) { + pa_alsa_path *p; + + pa_assert(ps); + pa_assert(m); + pa_assert(cb); + + PA_LLIST_FOREACH(p, ps->paths) + pa_alsa_path_set_callback(p, m, cb, userdata); +} + +pa_alsa_path_set *pa_alsa_path_set_new(pa_alsa_mapping *m, pa_alsa_direction_t direction) { + pa_alsa_path_set *ps; + char **pn = NULL, **en = NULL, **ie; + + pa_assert(m); + pa_assert(direction == PA_ALSA_DIRECTION_OUTPUT || direction == PA_ALSA_DIRECTION_INPUT); + + if (m->direction != PA_ALSA_DIRECTION_ANY && m->direction != direction) + return NULL; + + ps = pa_xnew0(pa_alsa_path_set, 1); + ps->direction = direction; + + if (direction == PA_ALSA_DIRECTION_OUTPUT) + pn = m->output_path_names; + else if (direction == PA_ALSA_DIRECTION_INPUT) + pn = m->input_path_names; + + if (pn) { + char **in; + + for (in = pn; *in; in++) { + pa_alsa_path *p; + pa_bool_t duplicate = FALSE; + char **kn, *fn; + + for (kn = pn; kn != in; kn++) + if (pa_streq(*kn, *in)) { + duplicate = TRUE; + break; + } + + if (duplicate) + continue; + + fn = pa_sprintf_malloc("%s.conf", *in); + + if ((p = pa_alsa_path_new(fn, direction))) { + p->path_set = ps; + PA_LLIST_INSERT_AFTER(pa_alsa_path, ps->paths, ps->last_path, p); + ps->last_path = p; + } + + pa_xfree(fn); + } + + return ps; + } + + if (direction == PA_ALSA_DIRECTION_OUTPUT) + en = m->output_element; + else if (direction == PA_ALSA_DIRECTION_INPUT) + en = m->input_element; + + if (!en) { + pa_alsa_path_set_free(ps); + return NULL; + } + + for (ie = en; *ie; ie++) { + char **je; + pa_alsa_path *p; + + p = pa_alsa_path_synthesize(*ie, direction); + p->path_set = ps; + + /* Mark all other passed elements for require-absent */ + for (je = en; *je; je++) { + pa_alsa_element *e; + e = pa_xnew0(pa_alsa_element, 1); + e->path = p; + e->alsa_name = pa_xstrdup(*je); + e->direction = direction; + e->required_absent = PA_ALSA_REQUIRED_ANY; + + PA_LLIST_INSERT_AFTER(pa_alsa_element, p->elements, p->last_element, e); + p->last_element = e; + } + + PA_LLIST_INSERT_AFTER(pa_alsa_path, ps->paths, ps->last_path, p); + ps->last_path = p; + } + + return ps; +} + +void pa_alsa_path_set_dump(pa_alsa_path_set *ps) { + pa_alsa_path *p; + pa_assert(ps); + + pa_log_debug("Path Set %p, direction=%i, probed=%s", + (void*) ps, + ps->direction, + pa_yes_no(ps->probed)); + + PA_LLIST_FOREACH(p, ps->paths) + pa_alsa_path_dump(p); +} + +static void path_set_unify(pa_alsa_path_set *ps) { + pa_alsa_path *p; + pa_bool_t has_dB = TRUE, has_volume = TRUE, has_mute = TRUE; + pa_assert(ps); + + /* We have issues dealing with paths that vary too wildly. That + * means for now we have to have all paths support volume/mute/dB + * or none. */ + + PA_LLIST_FOREACH(p, ps->paths) { + pa_assert(p->probed); + + if (!p->has_volume) + has_volume = FALSE; + else if (!p->has_dB) + has_dB = FALSE; + + if (!p->has_mute) + has_mute = FALSE; + } + + if (!has_volume || !has_dB || !has_mute) { + + if (!has_volume) + pa_log_debug("Some paths of the device lack hardware volume control, disabling hardware control altogether."); + else if (!has_dB) + pa_log_debug("Some paths of the device lack dB information, disabling dB logic altogether."); + + if (!has_mute) + pa_log_debug("Some paths of the device lack hardware mute control, disabling hardware control altogether."); + + PA_LLIST_FOREACH(p, ps->paths) { + if (!has_volume) + p->has_volume = FALSE; + else if (!has_dB) + p->has_dB = FALSE; + + if (!has_mute) + p->has_mute = FALSE; + } + } +} + +static void path_set_make_paths_unique(pa_alsa_path_set *ps) { + pa_alsa_path *p, *q; + + PA_LLIST_FOREACH(p, ps->paths) { + unsigned i; + char *m; + + for (q = p->next; q; q = q->next) + if (pa_streq(q->name, p->name)) + break; + + if (!q) + continue; + + m = pa_xstrdup(p->name); + + /* OK, this name is not unique, hence let's rename */ + for (i = 1, q = p; q; q = q->next) { + char *nn, *nd; + + if (!pa_streq(q->name, m)) + continue; + + nn = pa_sprintf_malloc("%s-%u", m, i); + pa_xfree(q->name); + q->name = nn; + + nd = pa_sprintf_malloc("%s %u", q->description, i); + pa_xfree(q->description); + q->description = nd; + + i++; + } + + pa_xfree(m); + } +} + +void pa_alsa_path_set_probe(pa_alsa_path_set *ps, snd_mixer_t *m, pa_bool_t ignore_dB) { + pa_alsa_path *p, *n; + + pa_assert(ps); + + if (ps->probed) + return; + + for (p = ps->paths; p; p = n) { + n = p->next; + + if (pa_alsa_path_probe(p, m, ignore_dB) < 0) { + PA_LLIST_REMOVE(pa_alsa_path, ps->paths, p); + pa_alsa_path_free(p); + } + } + + path_set_unify(ps); + path_set_make_paths_unique(ps); + ps->probed = TRUE; +} + +static void mapping_free(pa_alsa_mapping *m) { + pa_assert(m); + + pa_xfree(m->name); + pa_xfree(m->description); + + pa_xstrfreev(m->device_strings); + pa_xstrfreev(m->input_path_names); + pa_xstrfreev(m->output_path_names); + pa_xstrfreev(m->input_element); + pa_xstrfreev(m->output_element); + + pa_assert(!m->input_pcm); + pa_assert(!m->output_pcm); + + pa_xfree(m); +} + +static void profile_free(pa_alsa_profile *p) { + pa_assert(p); + + pa_xfree(p->name); + pa_xfree(p->description); + + pa_xstrfreev(p->input_mapping_names); + pa_xstrfreev(p->output_mapping_names); + + if (p->input_mappings) + pa_idxset_free(p->input_mappings, NULL, NULL); + + if (p->output_mappings) + pa_idxset_free(p->output_mappings, NULL, NULL); + + pa_xfree(p); +} + +void pa_alsa_profile_set_free(pa_alsa_profile_set *ps) { + pa_assert(ps); + + if (ps->profiles) { + pa_alsa_profile *p; + + while ((p = pa_hashmap_steal_first(ps->profiles))) + profile_free(p); + + pa_hashmap_free(ps->profiles, NULL, NULL); + } + + if (ps->mappings) { + pa_alsa_mapping *m; + + while ((m = pa_hashmap_steal_first(ps->mappings))) + mapping_free(m); + + pa_hashmap_free(ps->mappings, NULL, NULL); + } + + pa_xfree(ps); +} + +static pa_alsa_mapping *mapping_get(pa_alsa_profile_set *ps, const char *name) { + pa_alsa_mapping *m; + + if (!pa_startswith(name, "Mapping ")) + return NULL; + + name += 8; + + if ((m = pa_hashmap_get(ps->mappings, name))) + return m; + + m = pa_xnew0(pa_alsa_mapping, 1); + m->profile_set = ps; + m->name = pa_xstrdup(name); + pa_channel_map_init(&m->channel_map); + + pa_hashmap_put(ps->mappings, m->name, m); + + return m; +} + +static pa_alsa_profile *profile_get(pa_alsa_profile_set *ps, const char *name) { + pa_alsa_profile *p; + + if (!pa_startswith(name, "Profile ")) + return NULL; + + name += 8; + + if ((p = pa_hashmap_get(ps->profiles, name))) + return p; + + p = pa_xnew0(pa_alsa_profile, 1); + p->profile_set = ps; + p->name = pa_xstrdup(name); + + pa_hashmap_put(ps->profiles, p->name, p); + + return p; +} + +static int mapping_parse_device_strings( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + const char *rvalue, + void *data, + void *userdata) { + + pa_alsa_profile_set *ps = userdata; + pa_alsa_mapping *m; + + pa_assert(ps); + + if (!(m = mapping_get(ps, section))) { + pa_log("[%s:%u] %s invalid in section %s", filename, line, lvalue, section); + return -1; + } + + pa_xstrfreev(m->device_strings); + if (!(m->device_strings = pa_split_spaces_strv(rvalue))) { + pa_log("[%s:%u] Device string list empty of '%s'", filename, line, section); + return -1; + } + + return 0; +} + +static int mapping_parse_channel_map( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + const char *rvalue, + void *data, + void *userdata) { + + pa_alsa_profile_set *ps = userdata; + pa_alsa_mapping *m; + + pa_assert(ps); + + if (!(m = mapping_get(ps, section))) { + pa_log("[%s:%u] %s invalid in section %s", filename, line, lvalue, section); + return -1; + } + + if (!(pa_channel_map_parse(&m->channel_map, rvalue))) { + pa_log("[%s:%u] Channel map invalid of '%s'", filename, line, section); + return -1; + } + + return 0; +} + +static int mapping_parse_paths( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + const char *rvalue, + void *data, + void *userdata) { + + pa_alsa_profile_set *ps = userdata; + pa_alsa_mapping *m; + + pa_assert(ps); + + if (!(m = mapping_get(ps, section))) { + pa_log("[%s:%u] %s invalid in section %s", filename, line, lvalue, section); + return -1; + } + + if (pa_streq(lvalue, "paths-input")) { + pa_xstrfreev(m->input_path_names); + m->input_path_names = pa_split_spaces_strv(rvalue); + } else { + pa_xstrfreev(m->output_path_names); + m->output_path_names = pa_split_spaces_strv(rvalue); + } + + return 0; +} + +static int mapping_parse_element( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + const char *rvalue, + void *data, + void *userdata) { + + pa_alsa_profile_set *ps = userdata; + pa_alsa_mapping *m; + + pa_assert(ps); + + if (!(m = mapping_get(ps, section))) { + pa_log("[%s:%u] %s invalid in section %s", filename, line, lvalue, section); + return -1; + } + + if (pa_streq(lvalue, "element-input")) { + pa_xstrfreev(m->input_element); + m->input_element = pa_split_spaces_strv(rvalue); + } else { + pa_xstrfreev(m->output_element); + m->output_element = pa_split_spaces_strv(rvalue); + } + + return 0; +} + +static int mapping_parse_direction( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + const char *rvalue, + void *data, + void *userdata) { + + pa_alsa_profile_set *ps = userdata; + pa_alsa_mapping *m; + + pa_assert(ps); + + if (!(m = mapping_get(ps, section))) { + pa_log("[%s:%u] Section name %s invalid.", filename, line, section); + return -1; + } + + if (pa_streq(rvalue, "input")) + m->direction = PA_ALSA_DIRECTION_INPUT; + else if (pa_streq(rvalue, "output")) + m->direction = PA_ALSA_DIRECTION_OUTPUT; + else if (pa_streq(rvalue, "any")) + m->direction = PA_ALSA_DIRECTION_ANY; + else { + pa_log("[%s:%u] Direction %s invalid.", filename, line, rvalue); + return -1; + } + + return 0; +} + +static int mapping_parse_description( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + const char *rvalue, + void *data, + void *userdata) { + + pa_alsa_profile_set *ps = userdata; + pa_alsa_profile *p; + pa_alsa_mapping *m; + + pa_assert(ps); + + if ((m = mapping_get(ps, section))) { + pa_xstrdup(m->description); + m->description = pa_xstrdup(rvalue); + } else if ((p = profile_get(ps, section))) { + pa_xfree(p->description); + p->description = pa_xstrdup(rvalue); + } else { + pa_log("[%s:%u] Section name %s invalid.", filename, line, section); + return -1; + } + + return 0; +} + +static int mapping_parse_priority( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + const char *rvalue, + void *data, + void *userdata) { + + pa_alsa_profile_set *ps = userdata; + pa_alsa_profile *p; + pa_alsa_mapping *m; + uint32_t prio; + + pa_assert(ps); + + if (pa_atou(rvalue, &prio) < 0) { + pa_log("[%s:%u] Priority invalid of '%s'", filename, line, section); + return -1; + } + + if ((m = mapping_get(ps, section))) + m->priority = prio; + else if ((p = profile_get(ps, section))) + p->priority = prio; + else { + pa_log("[%s:%u] Section name %s invalid.", filename, line, section); + return -1; + } + + return 0; +} + +static int profile_parse_mappings( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + const char *rvalue, + void *data, + void *userdata) { + + pa_alsa_profile_set *ps = userdata; + pa_alsa_profile *p; + + pa_assert(ps); + + if (!(p = profile_get(ps, section))) { + pa_log("[%s:%u] %s invalid in section %s", filename, line, lvalue, section); + return -1; + } + + if (pa_streq(lvalue, "input-mappings")) { + pa_xstrfreev(p->input_mapping_names); + p->input_mapping_names = pa_split_spaces_strv(rvalue); + } else { + pa_xstrfreev(p->output_mapping_names); + p->output_mapping_names = pa_split_spaces_strv(rvalue); + } + + return 0; +} + +static int profile_parse_skip_probe( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + const char *rvalue, + void *data, + void *userdata) { + + pa_alsa_profile_set *ps = userdata; + pa_alsa_profile *p; + int b; + + pa_assert(ps); + + if (!(p = profile_get(ps, section))) { + pa_log("[%s:%u] %s invalid in section %s", filename, line, lvalue, section); + return -1; + } + + if ((b = pa_parse_boolean(rvalue)) < 0) { + pa_log("[%s:%u] Skip probe invalid of '%s'", filename, line, section); + return -1; + } + + p->supported = b; + + return 0; +} + +static int mapping_verify(pa_alsa_mapping *m, const pa_channel_map *bonus) { + + static const struct description_map well_known_descriptions[] = { + { "analog-mono", N_("Analog Mono") }, + { "analog-stereo", N_("Analog Stereo") }, + { "analog-surround-21", N_("Analog Surround 2.1") }, + { "analog-surround-30", N_("Analog Surround 3.0") }, + { "analog-surround-31", N_("Analog Surround 3.1") }, + { "analog-surround-40", N_("Analog Surround 4.0") }, + { "analog-surround-41", N_("Analog Surround 4.1") }, + { "analog-surround-50", N_("Analog Surround 5.0") }, + { "analog-surround-51", N_("Analog Surround 5.1") }, + { "analog-surround-61", N_("Analog Surround 6.0") }, + { "analog-surround-61", N_("Analog Surround 6.1") }, + { "analog-surround-70", N_("Analog Surround 7.0") }, + { "analog-surround-71", N_("Analog Surround 7.1") }, + { "iec958-stereo", N_("Digital Stereo (IEC958)") }, + { "iec958-surround-40", N_("Digital Surround 4.0 (IEC958)") }, + { "iec958-ac3-surround-40", N_("Digital Surround 4.0 (IEC958/AC3)") }, + { "iec958-ac3-surround-51", N_("Digital Surround 5.1 (IEC958/AC3)") }, + { "hdmi-stereo", N_("Digital Stereo (HDMI)") } + }; + + pa_assert(m); + + if (!pa_channel_map_valid(&m->channel_map)) { + pa_log("Mapping %s is missing channel map.", m->name); + return -1; + } + + if (!m->device_strings) { + pa_log("Mapping %s is missing device strings.", m->name); + return -1; + } + + if ((m->input_path_names && m->input_element) || + (m->output_path_names && m->output_element)) { + pa_log("Mapping %s must have either mixer path or mixer elment, not both.", m->name); + return -1; + } + + if (!m->description) + m->description = pa_xstrdup(lookup_description(m->name, + well_known_descriptions, + PA_ELEMENTSOF(well_known_descriptions))); + + if (!m->description) + m->description = pa_xstrdup(m->name); + + if (bonus) { + if (pa_channel_map_equal(&m->channel_map, bonus)) + m->priority += 5000; + else if (m->channel_map.channels == bonus->channels) + m->priority += 4000; + } + + return 0; +} + +void pa_alsa_mapping_dump(pa_alsa_mapping *m) { + char cm[PA_CHANNEL_MAP_SNPRINT_MAX]; + + pa_assert(m); + + pa_log_debug("Mapping %s (%s), priority=%u, channel_map=%s, supported=%s, direction=%i", + m->name, + pa_strnull(m->description), + m->priority, + pa_channel_map_snprint(cm, sizeof(cm), &m->channel_map), + pa_yes_no(m->supported), + m->direction); +} + +static void profile_set_add_auto_pair( + pa_alsa_profile_set *ps, + pa_alsa_mapping *m, /* output */ + pa_alsa_mapping *n /* input */) { + + char *name; + pa_alsa_profile *p; + + pa_assert(ps); + pa_assert(m || n); + + if (m && m->direction == PA_ALSA_DIRECTION_INPUT) + return; + + if (n && n->direction == PA_ALSA_DIRECTION_OUTPUT) + return; + + if (m && n) + name = pa_sprintf_malloc("output:%s+input:%s", m->name, n->name); + else if (m) + name = pa_sprintf_malloc("output:%s", m->name); + else + name = pa_sprintf_malloc("input:%s", n->name); + + if ((p = pa_hashmap_get(ps->profiles, name))) { + pa_xfree(name); + return; + } + + p = pa_xnew0(pa_alsa_profile, 1); + p->profile_set = ps; + p->name = name; + + if (m) { + p->output_mappings = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func); + pa_idxset_put(p->output_mappings, m, NULL); + p->priority += m->priority * 100; + } + + if (n) { + p->input_mappings = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func); + pa_idxset_put(p->input_mappings, n, NULL); + p->priority += n->priority; + } + + pa_hashmap_put(ps->profiles, p->name, p); +} + +static void profile_set_add_auto(pa_alsa_profile_set *ps) { + pa_alsa_mapping *m, *n; + void *m_state, *n_state; + + pa_assert(ps); + + PA_HASHMAP_FOREACH(m, ps->mappings, m_state) { + profile_set_add_auto_pair(ps, m, NULL); + + PA_HASHMAP_FOREACH(n, ps->mappings, n_state) + profile_set_add_auto_pair(ps, m, n); + } + + PA_HASHMAP_FOREACH(n, ps->mappings, n_state) + profile_set_add_auto_pair(ps, NULL, n); +} + +static int profile_verify(pa_alsa_profile *p) { + + static const struct description_map well_known_descriptions[] = { + { "output:analog-mono+input:analog-mono", N_("Analog Mono Duplex") }, + { "output:analog-stereo+input:analog-stereo", N_("Analog Stereo Duplex") }, + { "output:iec958-stereo", N_("Digital Stereo Duplex (IEC958)") }, + { "off", N_("Off") } + }; + + pa_assert(p); + + /* Replace the output mapping names by the actual mappings */ + if (p->output_mapping_names) { + char **name; + + pa_assert(!p->output_mappings); + p->output_mappings = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func); + + for (name = p->output_mapping_names; *name; name++) { + pa_alsa_mapping *m; + char **in; + pa_bool_t duplicate = FALSE; + + for (in = name + 1; *in; in++) + if (pa_streq(*name, *in)) { + duplicate = TRUE; + break; + } + + if (duplicate) + continue; + + if (!(m = pa_hashmap_get(p->profile_set->mappings, *name)) || m->direction == PA_ALSA_DIRECTION_INPUT) { + pa_log("Profile '%s' refers to unexistant mapping '%s'.", p->name, *name); + return -1; + } + + pa_idxset_put(p->output_mappings, m, NULL); + + if (p->supported) + m->supported++; + } + + pa_xstrfreev(p->output_mapping_names); + p->output_mapping_names = NULL; + } + + /* Replace the input mapping names by the actual mappings */ + if (p->input_mapping_names) { + char **name; + + pa_assert(!p->input_mappings); + p->input_mappings = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func); + + for (name = p->input_mapping_names; *name; name++) { + pa_alsa_mapping *m; + char **in; + pa_bool_t duplicate = FALSE; + + for (in = name + 1; *in; in++) + if (pa_streq(*name, *in)) { + duplicate = TRUE; + break; + } + + if (duplicate) + continue; + + if (!(m = pa_hashmap_get(p->profile_set->mappings, *name)) || m->direction == PA_ALSA_DIRECTION_OUTPUT) { + pa_log("Profile '%s' refers to unexistant mapping '%s'.", p->name, *name); + return -1; + } + + pa_idxset_put(p->input_mappings, m, NULL); + + if (p->supported) + m->supported++; + } + + pa_xstrfreev(p->input_mapping_names); + p->input_mapping_names = NULL; + } + + if (!p->input_mappings && !p->output_mappings) { + pa_log("Profile '%s' lacks mappings.", p->name); + return -1; + } + + if (!p->description) + p->description = pa_xstrdup(lookup_description(p->name, + well_known_descriptions, + PA_ELEMENTSOF(well_known_descriptions))); + + if (!p->description) { + pa_strbuf *sb; + uint32_t idx; + pa_alsa_mapping *m; + + sb = pa_strbuf_new(); + + if (p->output_mappings) + PA_IDXSET_FOREACH(m, p->output_mappings, idx) { + if (!pa_strbuf_isempty(sb)) + pa_strbuf_puts(sb, " + "); + + pa_strbuf_printf(sb, "%s Output", m->description); + } + + if (p->input_mappings) + PA_IDXSET_FOREACH(m, p->input_mappings, idx) { + if (!pa_strbuf_isempty(sb)) + pa_strbuf_puts(sb, " + "); + + pa_strbuf_printf(sb, "%s Input", m->description); + } + + p->description = pa_strbuf_tostring_free(sb); + } + + return 0; +} + +void pa_alsa_profile_dump(pa_alsa_profile *p) { + uint32_t idx; + pa_alsa_mapping *m; + pa_assert(p); + + pa_log_debug("Profile %s (%s), priority=%u, supported=%s n_input_mappings=%u, n_output_mappings=%u", + p->name, + pa_strnull(p->description), + p->priority, + pa_yes_no(p->supported), + p->input_mappings ? pa_idxset_size(p->input_mappings) : 0, + p->output_mappings ? pa_idxset_size(p->output_mappings) : 0); + + if (p->input_mappings) + PA_IDXSET_FOREACH(m, p->input_mappings, idx) + pa_log_debug("Input %s", m->name); + + if (p->output_mappings) + PA_IDXSET_FOREACH(m, p->output_mappings, idx) + pa_log_debug("Output %s", m->name); +} + +pa_alsa_profile_set* pa_alsa_profile_set_new(const char *fname, const pa_channel_map *bonus) { + pa_alsa_profile_set *ps; + pa_alsa_profile *p; + pa_alsa_mapping *m; + char *fn; + int r; + void *state; + + static pa_config_item items[] = { + /* [General] */ + { "auto-profiles", pa_config_parse_bool, NULL, "General" }, + + /* [Mapping ...] */ + { "device-strings", mapping_parse_device_strings, NULL, NULL }, + { "channel-map", mapping_parse_channel_map, NULL, NULL }, + { "paths-input", mapping_parse_paths, NULL, NULL }, + { "paths-output", mapping_parse_paths, NULL, NULL }, + { "element-input", mapping_parse_element, NULL, NULL }, + { "element-output", mapping_parse_element, NULL, NULL }, + { "direction", mapping_parse_direction, NULL, NULL }, + + /* Shared by [Mapping ...] and [Profile ...] */ + { "description", mapping_parse_description, NULL, NULL }, + { "priority", mapping_parse_priority, NULL, NULL }, + + /* [Profile ...] */ + { "input-mappings", profile_parse_mappings, NULL, NULL }, + { "output-mappings", profile_parse_mappings, NULL, NULL }, + { "skip-probe", profile_parse_skip_probe, NULL, NULL }, + { NULL, NULL, NULL, NULL } + }; + + ps = pa_xnew0(pa_alsa_profile_set, 1); + ps->mappings = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func); + ps->profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func); + + items[0].data = &ps->auto_profiles; + + if (!fname) + fname = "default.conf"; + + fn = pa_maybe_prefix_path(fname, PA_ALSA_PROFILE_SETS_DIR); + r = pa_config_parse(fn, NULL, items, ps); + pa_xfree(fn); + + if (r < 0) + goto fail; + + PA_HASHMAP_FOREACH(m, ps->mappings, state) + if (mapping_verify(m, bonus) < 0) + goto fail; + + if (ps->auto_profiles) + profile_set_add_auto(ps); + + PA_HASHMAP_FOREACH(p, ps->profiles, state) + if (profile_verify(p) < 0) + goto fail; + + return ps; + +fail: + pa_alsa_profile_set_free(ps); + return NULL; +} + +void pa_alsa_profile_set_probe(pa_alsa_profile_set *ps, const char *dev_id, const pa_sample_spec *ss) { + void *state; + pa_alsa_profile *p, *last = NULL; + pa_alsa_mapping *m; + + pa_assert(ps); + pa_assert(dev_id); + pa_assert(ss); + + if (ps->probed) + return; + + PA_HASHMAP_FOREACH(p, ps->profiles, state) { + pa_sample_spec try_ss; + pa_channel_map try_map; + uint32_t idx; + + /* Is this already marked that it is supported? (i.e. from the config file) */ + if (p->supported) + continue; + + pa_log_debug("Looking at profile %s", p->name); + + /* Close PCMs from the last iteration we don't need anymore */ + if (last && last->output_mappings) + PA_IDXSET_FOREACH(m, last->output_mappings, idx) { + + if (!m->output_pcm) + break; + + if (last->supported) + m->supported++; + + if (!p->output_mappings || !pa_idxset_get_by_data(p->output_mappings, m, NULL)) { + snd_pcm_close(m->output_pcm); + m->output_pcm = NULL; + } + } + + if (last && last->input_mappings) + PA_IDXSET_FOREACH(m, last->input_mappings, idx) { + + if (!m->input_pcm) + break; + + if (last->supported) + m->supported++; + + if (!p->input_mappings || !pa_idxset_get_by_data(p->input_mappings, m, NULL)) { + snd_pcm_close(m->input_pcm); + m->input_pcm = NULL; + } + } + + p->supported = TRUE; + + /* Check if we can open all new ones */ + if (p->output_mappings) + PA_IDXSET_FOREACH(m, p->output_mappings, idx) { + + if (m->output_pcm) + continue; + + pa_log_debug("Checking for playback on %s (%s)", m->description, m->name); + try_map = m->channel_map; + try_ss = *ss; + try_ss.channels = try_map.channels; + + if (!(m ->output_pcm = pa_alsa_open_by_template( + m->device_strings, + dev_id, + NULL, + &try_ss, &try_map, + SND_PCM_STREAM_PLAYBACK, + NULL, NULL, 0, NULL, NULL, + TRUE))) { + p->supported = FALSE; + break; + } + } + + if (p->input_mappings && p->supported) + PA_IDXSET_FOREACH(m, p->input_mappings, idx) { + + if (m->input_pcm) + continue; + + pa_log_debug("Checking for recording on %s (%s)", m->description, m->name); + try_map = m->channel_map; + try_ss = *ss; + try_ss.channels = try_map.channels; + + if (!(m ->input_pcm = pa_alsa_open_by_template( + m->device_strings, + dev_id, + NULL, + &try_ss, &try_map, + SND_PCM_STREAM_CAPTURE, + NULL, NULL, 0, NULL, NULL, + TRUE))) { + p->supported = FALSE; + break; + } + } + + last = p; + + if (p->supported) + pa_log_debug("Profile %s supported.", p->name); + } + + /* Clean up */ + if (last) { + uint32_t idx; + + if (last->output_mappings) + PA_IDXSET_FOREACH(m, last->output_mappings, idx) + if (m->output_pcm) { + + if (last->supported) + m->supported++; + + snd_pcm_close(m->output_pcm); + m->output_pcm = NULL; + } + + if (last->input_mappings) + PA_IDXSET_FOREACH(m, last->input_mappings, idx) + if (m->input_pcm) { + + if (last->supported) + m->supported++; + + snd_pcm_close(m->input_pcm); + m->input_pcm = NULL; + } + } + + PA_HASHMAP_FOREACH(p, ps->profiles, state) + if (!p->supported) { + pa_hashmap_remove(ps->profiles, p->name); + profile_free(p); + } + + PA_HASHMAP_FOREACH(m, ps->mappings, state) + if (m->supported <= 0) { + pa_hashmap_remove(ps->mappings, m->name); + mapping_free(m); + } + + ps->probed = TRUE; +} + +void pa_alsa_profile_set_dump(pa_alsa_profile_set *ps) { + pa_alsa_profile *p; + pa_alsa_mapping *m; + void *state; + + pa_assert(ps); + + pa_log_debug("Profile set %p, auto_profiles=%s, probed=%s, n_mappings=%u, n_profiles=%u", + (void*) + ps, + pa_yes_no(ps->auto_profiles), + pa_yes_no(ps->probed), + pa_hashmap_size(ps->mappings), + pa_hashmap_size(ps->profiles)); + + PA_HASHMAP_FOREACH(m, ps->mappings, state) + pa_alsa_mapping_dump(m); + + PA_HASHMAP_FOREACH(p, ps->profiles, state) + pa_alsa_profile_dump(p); +} + +void pa_alsa_add_ports(pa_hashmap **p, pa_alsa_path_set *ps) { + pa_alsa_path *path; + + pa_assert(p); + pa_assert(!*p); + pa_assert(ps); + + /* if there is no path, we don't want a port list */ + if (!ps->paths) + return; + + if (!ps->paths->next){ + pa_alsa_setting *s; + + /* If there is only one path, but no or only one setting, then + * we want a port list either */ + if (!ps->paths->settings || !ps->paths->settings->next) + return; + + /* Ok, there is only one path, however with multiple settings, + * so let's create a port for each setting */ + *p = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func); + + PA_LLIST_FOREACH(s, ps->paths->settings) { + pa_device_port *port; + pa_alsa_port_data *data; + + port = pa_device_port_new(s->name, s->description, sizeof(pa_alsa_port_data)); + port->priority = s->priority; + + data = PA_DEVICE_PORT_DATA(port); + data->path = ps->paths; + data->setting = s; + + pa_hashmap_put(*p, port->name, port); + } + + } else { + + /* We have multiple paths, so let's create a port for each + * one, and each of each settings */ + *p = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func); + + PA_LLIST_FOREACH(path, ps->paths) { + + if (!path->settings || !path->settings->next) { + pa_device_port *port; + pa_alsa_port_data *data; + + /* If there is no or just one setting we only need a + * single entry */ + + port = pa_device_port_new(path->name, path->description, sizeof(pa_alsa_port_data)); + port->priority = path->priority * 100; + + + data = PA_DEVICE_PORT_DATA(port); + data->path = path; + data->setting = path->settings; + + pa_hashmap_put(*p, port->name, port); + } else { + pa_alsa_setting *s; + + PA_LLIST_FOREACH(s, path->settings) { + pa_device_port *port; + pa_alsa_port_data *data; + char *n, *d; + + n = pa_sprintf_malloc("%s;%s", path->name, s->name); + + if (s->description[0]) + d = pa_sprintf_malloc(_("%s / %s"), path->description, s->description); + else + d = pa_xstrdup(path->description); + + port = pa_device_port_new(n, d, sizeof(pa_alsa_port_data)); + port->priority = path->priority * 100 + s->priority; + + pa_xfree(n); + pa_xfree(d); + + data = PA_DEVICE_PORT_DATA(port); + data->path = path; + data->setting = s; + + pa_hashmap_put(*p, port->name, port); + } + } + } + } + + pa_log_debug("Added %u ports", pa_hashmap_size(*p)); +} diff --git a/src/modules/alsa/alsa-mixer.h b/src/modules/alsa/alsa-mixer.h new file mode 100644 index 00000000..76788183 --- /dev/null +++ b/src/modules/alsa/alsa-mixer.h @@ -0,0 +1,292 @@ +#ifndef fooalsamixerhfoo +#define fooalsamixerhfoo + +/*** + This file is part of PulseAudio. + + Copyright 2004-2006 Lennart Poettering + Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB + + 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 + along with PulseAudio; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#include <asoundlib.h> + +#include <pulse/sample.h> +#include <pulse/volume.h> +#include <pulse/mainloop-api.h> +#include <pulse/channelmap.h> +#include <pulse/proplist.h> +#include <pulse/volume.h> + +#include <pulsecore/llist.h> +#include <pulsecore/rtpoll.h> +#include <pulsecore/core.h> +#include <pulsecore/log.h> + +typedef struct pa_alsa_fdlist pa_alsa_fdlist; +typedef struct pa_alsa_setting pa_alsa_setting; +typedef struct pa_alsa_option pa_alsa_option; +typedef struct pa_alsa_element pa_alsa_element; +typedef struct pa_alsa_path pa_alsa_path; +typedef struct pa_alsa_path_set pa_alsa_path_set; +typedef struct pa_alsa_mapping pa_alsa_mapping; +typedef struct pa_alsa_profile pa_alsa_profile; +typedef struct pa_alsa_profile_set pa_alsa_profile_set; +typedef struct pa_alsa_port_data pa_alsa_port_data; + +#include "alsa-util.h" + +typedef enum pa_alsa_switch_use { + PA_ALSA_SWITCH_IGNORE, + PA_ALSA_SWITCH_MUTE, /* make this switch follow mute status */ + PA_ALSA_SWITCH_OFF, /* set this switch to 'off' unconditionally */ + PA_ALSA_SWITCH_ON, /* set this switch to 'on' unconditionally */ + PA_ALSA_SWITCH_SELECT /* allow the user to select switch status through a setting */ +} pa_alsa_switch_use_t; + +typedef enum pa_alsa_volume_use { + PA_ALSA_VOLUME_IGNORE, + PA_ALSA_VOLUME_MERGE, /* merge this volume slider into the global volume slider */ + PA_ALSA_VOLUME_OFF, /* set this volume to minimal unconditionally */ + PA_ALSA_VOLUME_ZERO /* set this volume to 0dB unconditionally */ +} pa_alsa_volume_use_t; + +typedef enum pa_alsa_enumeration_use { + PA_ALSA_ENUMERATION_IGNORE, + PA_ALSA_ENUMERATION_SELECT +} pa_alsa_enumeration_use_t; + +typedef enum pa_alsa_required { + PA_ALSA_REQUIRED_IGNORE, + PA_ALSA_REQUIRED_SWITCH, + PA_ALSA_REQUIRED_VOLUME, + PA_ALSA_REQUIRED_ENUMERATION, + PA_ALSA_REQUIRED_ANY +} pa_alsa_required_t; + +typedef enum pa_alsa_direction { + PA_ALSA_DIRECTION_ANY, + PA_ALSA_DIRECTION_OUTPUT, + PA_ALSA_DIRECTION_INPUT +} pa_alsa_direction_t; + +/* A setting combines a couple of options into a single entity that + * may be selected. Only one setting can be active at the same + * time. */ +struct pa_alsa_setting { + pa_alsa_path *path; + PA_LLIST_FIELDS(pa_alsa_setting); + + pa_idxset *options; + + char *name; + char *description; + unsigned priority; +}; + +/* An option belongs to an element and refers to one enumeration item + * of the element is an enumeration item, or a switch status if the + * element is a switch item. */ +struct pa_alsa_option { + pa_alsa_element *element; + PA_LLIST_FIELDS(pa_alsa_option); + + char *alsa_name; + int alsa_idx; + + char *name; + char *description; + unsigned priority; +}; + +/* And element wraps one specific ALSA element. A series of elements * +make up a path (see below). If the element is an enumeration or switch +* element it may includes a list of options. */ +struct pa_alsa_element { + pa_alsa_path *path; + PA_LLIST_FIELDS(pa_alsa_element); + + char *alsa_name; + pa_alsa_direction_t direction; + + pa_alsa_switch_use_t switch_use; + pa_alsa_volume_use_t volume_use; + pa_alsa_enumeration_use_t enumeration_use; + + pa_alsa_required_t required; + pa_alsa_required_t required_absent; + + pa_bool_t override_map:1; + pa_bool_t direction_try_other:1; + + pa_bool_t has_dB:1; + long min_volume, max_volume; + double min_dB, max_dB; + + pa_channel_position_mask_t masks[SND_MIXER_SCHN_LAST][2]; + unsigned n_channels; + + pa_channel_position_mask_t merged_mask; + + PA_LLIST_HEAD(pa_alsa_option, options); +}; + +/* A path wraps a series of elements into a single entity which can be + * used to control it as if it had a single volume slider, a single + * mute switch and a single list of selectable options. */ +struct pa_alsa_path { + pa_alsa_path_set *path_set; + PA_LLIST_FIELDS(pa_alsa_path); + + pa_alsa_direction_t direction; + + char *name; + char *description; + unsigned priority; + + pa_bool_t probed:1; + pa_bool_t supported:1; + pa_bool_t has_mute:1; + pa_bool_t has_volume:1; + pa_bool_t has_dB:1; + + long min_volume, max_volume; + double min_dB, max_dB; + + /* This is used during parsing only, as a shortcut so that we + * don't have to iterate the list all the time */ + pa_alsa_element *last_element; + pa_alsa_option *last_option; + pa_alsa_setting *last_setting; + + PA_LLIST_HEAD(pa_alsa_element, elements); + PA_LLIST_HEAD(pa_alsa_setting, settings); +}; + +/* A path set is simply a set of paths that are applicable to a + * device */ +struct pa_alsa_path_set { + PA_LLIST_HEAD(pa_alsa_path, paths); + pa_alsa_direction_t direction; + pa_bool_t probed:1; + + /* This is used during parsing only, as a shortcut so that we + * don't have to iterate the list all the time */ + pa_alsa_path *last_path; +}; + +int pa_alsa_setting_select(pa_alsa_setting *s, snd_mixer_t *m); +void pa_alsa_setting_dump(pa_alsa_setting *s); + +void pa_alsa_option_dump(pa_alsa_option *o); + +void pa_alsa_element_dump(pa_alsa_element *e); + +pa_alsa_path *pa_alsa_path_new(const char *fname, pa_alsa_direction_t direction); +pa_alsa_path *pa_alsa_path_synthesize(const char *element, pa_alsa_direction_t direction); +int pa_alsa_path_probe(pa_alsa_path *p, snd_mixer_t *m, pa_bool_t ignore_dB); +void pa_alsa_path_dump(pa_alsa_path *p); +int pa_alsa_path_get_volume(pa_alsa_path *p, snd_mixer_t *m, const pa_channel_map *cm, pa_cvolume *v); +int pa_alsa_path_get_mute(pa_alsa_path *path, snd_mixer_t *m, pa_bool_t *muted); +int pa_alsa_path_set_volume(pa_alsa_path *path, snd_mixer_t *m, const pa_channel_map *cm, pa_cvolume *v); +int pa_alsa_path_set_mute(pa_alsa_path *path, snd_mixer_t *m, pa_bool_t muted); +int pa_alsa_path_select(pa_alsa_path *p, snd_mixer_t *m); +void pa_alsa_path_set_callback(pa_alsa_path *p, snd_mixer_t *m, snd_mixer_elem_callback_t cb, void *userdata); +void pa_alsa_path_free(pa_alsa_path *p); + +pa_alsa_path_set *pa_alsa_path_set_new(pa_alsa_mapping *m, pa_alsa_direction_t direction); +void pa_alsa_path_set_probe(pa_alsa_path_set *s, snd_mixer_t *m, pa_bool_t ignore_dB); +void pa_alsa_path_set_dump(pa_alsa_path_set *s); +void pa_alsa_path_set_set_callback(pa_alsa_path_set *ps, snd_mixer_t *m, snd_mixer_elem_callback_t cb, void *userdata); +void pa_alsa_path_set_free(pa_alsa_path_set *s); + +struct pa_alsa_mapping { + pa_alsa_profile_set *profile_set; + + char *name; + char *description; + unsigned priority; + pa_alsa_direction_t direction; + + pa_channel_map channel_map; + + char **device_strings; + + char **input_path_names; + char **output_path_names; + char **input_element; /* list of fallbacks */ + char **output_element; + + unsigned supported; + + /* Temporarily used during probing */ + snd_pcm_t *input_pcm; + snd_pcm_t *output_pcm; + + pa_sink *sink; + pa_source *source; +}; + +struct pa_alsa_profile { + pa_alsa_profile_set *profile_set; + + char *name; + char *description; + unsigned priority; + + pa_bool_t supported:1; + + char **input_mapping_names; + char **output_mapping_names; + + pa_idxset *input_mappings; + pa_idxset *output_mappings; +}; + +struct pa_alsa_profile_set { + pa_hashmap *mappings; + pa_hashmap *profiles; + + pa_bool_t auto_profiles; + pa_bool_t probed:1; +}; + +void pa_alsa_mapping_dump(pa_alsa_mapping *m); +void pa_alsa_profile_dump(pa_alsa_profile *p); + +pa_alsa_profile_set* pa_alsa_profile_set_new(const char *fname, const pa_channel_map *bonus); +void pa_alsa_profile_set_probe(pa_alsa_profile_set *ps, const char *dev_id, const pa_sample_spec *ss); +void pa_alsa_profile_set_free(pa_alsa_profile_set *s); +void pa_alsa_profile_set_dump(pa_alsa_profile_set *s); + +snd_mixer_t *pa_alsa_open_mixer_for_pcm(snd_pcm_t *pcm, char **ctl_device); + +pa_alsa_fdlist *pa_alsa_fdlist_new(void); +void pa_alsa_fdlist_free(pa_alsa_fdlist *fdl); +int pa_alsa_fdlist_set_mixer(pa_alsa_fdlist *fdl, snd_mixer_t *mixer_handle, pa_mainloop_api* m); + +/* Data structure for inclusion in pa_device_port for alsa + * sinks/sources. This contains nothing that needs to be freed + * individually */ +struct pa_alsa_port_data { + pa_alsa_path *path; + pa_alsa_setting *setting; +}; + +void pa_alsa_add_ports(pa_hashmap **p, pa_alsa_path_set *ps); + +#endif diff --git a/src/modules/alsa/alsa-sink.c b/src/modules/alsa/alsa-sink.c index d95f3c2f..b14afd0a 100644 --- a/src/modules/alsa/alsa-sink.c +++ b/src/modules/alsa/alsa-sink.c @@ -32,16 +32,18 @@ #include <valgrind/memcheck.h> #endif -#include <pulse/xmalloc.h> -#include <pulse/util.h> -#include <pulse/timeval.h> #include <pulse/i18n.h> +#include <pulse/rtclock.h> +#include <pulse/timeval.h> +#include <pulse/util.h> +#include <pulse/xmalloc.h> #include <pulsecore/core.h> #include <pulsecore/module.h> #include <pulsecore/memchunk.h> #include <pulsecore/sink.h> #include <pulsecore/modargs.h> +#include <pulsecore/core-rtclock.h> #include <pulsecore/core-util.h> #include <pulsecore/sample-util.h> #include <pulsecore/log.h> @@ -50,7 +52,6 @@ #include <pulsecore/core-error.h> #include <pulsecore/thread-mq.h> #include <pulsecore/rtpoll.h> -#include <pulsecore/rtclock.h> #include <pulsecore/time-smoother.h> #include <modules/reserve-wrap.h> @@ -80,11 +81,9 @@ struct userdata { pa_alsa_fdlist *mixer_fdl; snd_mixer_t *mixer_handle; - snd_mixer_elem_t *mixer_elem; - long hw_volume_max, hw_volume_min; - long hw_dB_max, hw_dB_min; - pa_bool_t hw_dB_supported:1; - pa_bool_t mixer_seperate_channels:1; + pa_alsa_path_set *mixer_path_set; + pa_alsa_path *mixer_path; + pa_cvolume hardware_volume; size_t @@ -100,7 +99,8 @@ struct userdata { unsigned nfragments; pa_memchunk memchunk; - char *device_name; + char *device_name; /* name of the PCM device */ + char *control_device; /* name of the control device */ pa_bool_t use_mmap:1, use_tsched:1; @@ -116,6 +116,8 @@ struct userdata { pa_reserve_wrapper *reserve; pa_hook_slot *reserve_slot; + pa_reserve_monitor_wrapper *monitor; + pa_hook_slot *monitor_slot; }; static void userdata_free(struct userdata *u); @@ -124,7 +126,7 @@ static pa_hook_result_t reserve_cb(pa_reserve_wrapper *r, void *forced, struct u pa_assert(r); pa_assert(u); - if (pa_sink_suspend(u->sink, TRUE) < 0) + if (pa_sink_suspend(u->sink, TRUE, PA_SUSPEND_APPLICATION) < 0) return PA_HOOK_CANCEL; return PA_HOOK_OK; @@ -167,10 +169,10 @@ static int reserve_init(struct userdata *u, const char *dname) { if (pa_in_system_mode()) return 0; - /* We are resuming, try to lock the device */ if (!(rname = pa_alsa_get_reserve_name(dname))) return 0; + /* We are resuming, try to lock the device */ u->reserve = pa_reserve_wrapper_get(u->core, rname); pa_xfree(rname); @@ -185,6 +187,56 @@ static int reserve_init(struct userdata *u, const char *dname) { return 0; } +static pa_hook_result_t monitor_cb(pa_reserve_monitor_wrapper *w, void* busy, struct userdata *u) { + pa_bool_t b; + + pa_assert(w); + pa_assert(u); + + b = PA_PTR_TO_UINT(busy) && !u->reserve; + + pa_sink_suspend(u->sink, b, PA_SUSPEND_APPLICATION); + return PA_HOOK_OK; +} + +static void monitor_done(struct userdata *u) { + pa_assert(u); + + if (u->monitor_slot) { + pa_hook_slot_free(u->monitor_slot); + u->monitor_slot = NULL; + } + + if (u->monitor) { + pa_reserve_monitor_wrapper_unref(u->monitor); + u->monitor = NULL; + } +} + +static int reserve_monitor_init(struct userdata *u, const char *dname) { + char *rname; + + pa_assert(u); + pa_assert(dname); + + if (pa_in_system_mode()) + return 0; + + if (!(rname = pa_alsa_get_reserve_name(dname))) + return 0; + + u->monitor = pa_reserve_monitor_wrapper_get(u->core, rname); + pa_xfree(rname); + + if (!(u->monitor)) + return -1; + + pa_assert(!u->monitor_slot); + u->monitor_slot = pa_hook_connect(pa_reserve_monitor_wrapper_hook(u->monitor), PA_HOOK_NORMAL, (pa_hook_cb_t) monitor_cb, u); + + return 0; +} + static void fix_min_sleep_wakeup(struct userdata *u) { size_t max_use, max_use_2; @@ -240,7 +292,7 @@ static void adjust_after_underrun(struct userdata *u) { pa_log_notice("Increasing minimal latency to %0.2f ms", (double) new_min_latency / PA_USEC_PER_MSEC); - pa_sink_update_latency_range(u->sink, new_min_latency, u->sink->thread_info.max_latency); + pa_sink_set_latency_range_within_thread(u->sink, new_min_latency, u->sink->thread_info.max_latency); return; } @@ -281,7 +333,7 @@ static int try_recover(struct userdata *u, const char *call, int err) { pa_assert(call); pa_assert(err < 0); - pa_log_debug("%s: %s", call, snd_strerror(err)); + pa_log_debug("%s: %s", call, pa_alsa_strerror(err)); pa_assert(err != -EAGAIN); @@ -289,7 +341,7 @@ static int try_recover(struct userdata *u, const char *call, int err) { pa_log_debug("%s: Buffer underrun!", call); if ((err = snd_pcm_recover(u->pcm_handle, err, 1)) < 0) { - pa_log("%s: %s", call, snd_strerror(err)); + pa_log("%s: %s", call, pa_alsa_strerror(err)); return -1; } @@ -473,7 +525,7 @@ static int mmap_write(struct userdata *u, pa_usec_t *sleep_usec, pa_bool_t polle u->since_start += frames * u->frame_size; #ifdef DEBUG_TIMING - pa_log_debug("Wrote %lu bytes", (unsigned long) (frames * u->frame_size)); + pa_log_debug("Wrote %lu bytes (of possible %lu bytes)", (unsigned long) (frames * u->frame_size), (unsigned long) n_bytes); #endif if ((size_t) frames * u->frame_size >= n_bytes) @@ -483,7 +535,13 @@ static int mmap_write(struct userdata *u, pa_usec_t *sleep_usec, pa_bool_t polle } } - *sleep_usec = pa_bytes_to_usec(left_to_play, &u->sink->sample_spec) - process_usec; + *sleep_usec = pa_bytes_to_usec(left_to_play, &u->sink->sample_spec); + + if (*sleep_usec > process_usec) + *sleep_usec -= process_usec; + else + *sleep_usec = 0; + return work_done ? 1 : 0; } @@ -605,7 +663,13 @@ static int unix_write(struct userdata *u, pa_usec_t *sleep_usec, pa_bool_t polle } } - *sleep_usec = pa_bytes_to_usec(left_to_play, &u->sink->sample_spec) - process_usec; + *sleep_usec = pa_bytes_to_usec(left_to_play, &u->sink->sample_spec); + + if (*sleep_usec > process_usec) + *sleep_usec -= process_usec; + else + *sleep_usec = 0; + return work_done ? 1 : 0; } @@ -624,12 +688,12 @@ static void update_smoother(struct userdata *u) { /* Let's update the time smoother */ if (PA_UNLIKELY((err = pa_alsa_safe_delay(u->pcm_handle, &delay, u->hwbuf_size, &u->sink->sample_spec)) < 0)) { - pa_log_warn("Failed to query DSP status data: %s", snd_strerror(err)); + pa_log_warn("Failed to query DSP status data: %s", pa_alsa_strerror(err)); return; } if (PA_UNLIKELY((err = snd_pcm_status(u->pcm_handle, status)) < 0)) - pa_log_warn("Failed to get timestamp: %s", snd_strerror(err)); + pa_log_warn("Failed to get timestamp: %s", pa_alsa_strerror(err)); else { snd_htimestamp_t htstamp = { 0, 0 }; snd_pcm_status_get_htstamp(status, &htstamp); @@ -643,7 +707,7 @@ static void update_smoother(struct userdata *u) { /* Hmm, if the timestamp is 0, then it wasn't set and we take the current time */ if (now1 <= 0) - now1 = pa_rtclock_usec(); + now1 = pa_rtclock_now(); now2 = pa_bytes_to_usec((uint64_t) position, &u->sink->sample_spec); @@ -657,7 +721,7 @@ static pa_usec_t sink_get_latency(struct userdata *u) { pa_assert(u); - now1 = pa_rtclock_usec(); + now1 = pa_rtclock_now(); now2 = pa_smoother_get(u->smoother, now1); delay = (int64_t) pa_bytes_to_usec(u->write_count, &u->sink->sample_spec) - (int64_t) now2; @@ -688,7 +752,7 @@ static int suspend(struct userdata *u) { pa_assert(u); pa_assert(u->pcm_handle); - pa_smoother_pause(u->smoother, pa_rtclock_usec()); + pa_smoother_pause(u->smoother, pa_rtclock_now()); /* Let's suspend -- we don't call snd_pcm_drain() here since that might * take awfully long with our long buffer sizes today. */ @@ -752,11 +816,11 @@ static int update_sw_params(struct userdata *u) { pa_log_debug("setting avail_min=%lu", (unsigned long) avail_min); if ((err = pa_alsa_set_sw_params(u->pcm_handle, avail_min)) < 0) { - pa_log("Failed to set software parameters: %s", snd_strerror(err)); + pa_log("Failed to set software parameters: %s", pa_alsa_strerror(err)); return err; } - pa_sink_set_max_request(u->sink, u->hwbuf_size - u->hwbuf_unused); + pa_sink_set_max_request_within_thread(u->sink, u->hwbuf_size - u->hwbuf_unused); return 0; } @@ -774,13 +838,12 @@ static int unsuspend(struct userdata *u) { pa_log_info("Trying resume..."); - snd_config_update_free_global(); if ((err = snd_pcm_open(&u->pcm_handle, u->device_name, SND_PCM_STREAM_PLAYBACK, /*SND_PCM_NONBLOCK|*/ SND_PCM_NO_AUTO_RESAMPLE| SND_PCM_NO_AUTO_CHANNELS| SND_PCM_NO_AUTO_FORMAT)) < 0) { - pa_log("Error opening PCM device %s: %s", u->device_name, snd_strerror(err)); + pa_log("Error opening PCM device %s: %s", u->device_name, pa_alsa_strerror(err)); goto fail; } @@ -791,7 +854,7 @@ static int unsuspend(struct userdata *u) { d = u->use_tsched; if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &nfrags, &period_size, u->hwbuf_size / u->frame_size, &b, &d, TRUE)) < 0) { - pa_log("Failed to set hardware parameters: %s", snd_strerror(err)); + pa_log("Failed to set hardware parameters: %s", pa_alsa_strerror(err)); goto fail; } @@ -919,198 +982,65 @@ static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) { return 0; if (mask & SND_CTL_EVENT_MASK_VALUE) { - pa_sink_get_volume(u->sink, TRUE); + pa_sink_get_volume(u->sink, TRUE, FALSE); pa_sink_get_mute(u->sink, TRUE); } return 0; } -static pa_volume_t from_alsa_volume(struct userdata *u, long alsa_vol) { - - return (pa_volume_t) round(((double) (alsa_vol - u->hw_volume_min) * PA_VOLUME_NORM) / - (double) (u->hw_volume_max - u->hw_volume_min)); -} - -static long to_alsa_volume(struct userdata *u, pa_volume_t vol) { - long alsa_vol; - - alsa_vol = (long) round(((double) vol * (double) (u->hw_volume_max - u->hw_volume_min)) - / PA_VOLUME_NORM) + u->hw_volume_min; - - return PA_CLAMP_UNLIKELY(alsa_vol, u->hw_volume_min, u->hw_volume_max); -} - static void sink_get_volume_cb(pa_sink *s) { struct userdata *u = s->userdata; - int err; - unsigned i; pa_cvolume r; char t[PA_CVOLUME_SNPRINT_MAX]; pa_assert(u); - pa_assert(u->mixer_elem); - - if (u->mixer_seperate_channels) { - - r.channels = s->sample_spec.channels; - - for (i = 0; i < s->sample_spec.channels; i++) { - long alsa_vol; - - if (u->hw_dB_supported) { - - if ((err = snd_mixer_selem_get_playback_dB(u->mixer_elem, u->mixer_map[i], &alsa_vol)) < 0) - goto fail; - -#ifdef HAVE_VALGRIND_MEMCHECK_H - VALGRIND_MAKE_MEM_DEFINED(&alsa_vol, sizeof(alsa_vol)); -#endif - - r.values[i] = pa_sw_volume_from_dB((double) (alsa_vol - u->hw_dB_max) / 100.0); - } else { - - if ((err = snd_mixer_selem_get_playback_volume(u->mixer_elem, u->mixer_map[i], &alsa_vol)) < 0) - goto fail; - - r.values[i] = from_alsa_volume(u, alsa_vol); - } - } - - } else { - long alsa_vol; - - if (u->hw_dB_supported) { - - if ((err = snd_mixer_selem_get_playback_dB(u->mixer_elem, SND_MIXER_SCHN_MONO, &alsa_vol)) < 0) - goto fail; - -#ifdef HAVE_VALGRIND_MEMCHECK_H - VALGRIND_MAKE_MEM_DEFINED(&alsa_vol, sizeof(alsa_vol)); -#endif - - pa_cvolume_set(&r, s->sample_spec.channels, pa_sw_volume_from_dB((double) (alsa_vol - u->hw_dB_max) / 100.0)); - - } else { + pa_assert(u->mixer_path); + pa_assert(u->mixer_handle); - if ((err = snd_mixer_selem_get_playback_volume(u->mixer_elem, SND_MIXER_SCHN_MONO, &alsa_vol)) < 0) - goto fail; + if (pa_alsa_path_get_volume(u->mixer_path, u->mixer_handle, &s->channel_map, &r) < 0) + return; - pa_cvolume_set(&r, s->sample_spec.channels, from_alsa_volume(u, alsa_vol)); - } - } + /* Shift down by the base volume, so that 0dB becomes maximum volume */ + pa_sw_cvolume_multiply_scalar(&r, &r, s->base_volume); pa_log_debug("Read hardware volume: %s", pa_cvolume_snprint(t, sizeof(t), &r)); - if (!pa_cvolume_equal(&u->hardware_volume, &r)) { + if (pa_cvolume_equal(&u->hardware_volume, &r)) + return; - s->virtual_volume = u->hardware_volume = r; + s->virtual_volume = u->hardware_volume = r; - if (u->hw_dB_supported) { - pa_cvolume reset; + if (u->mixer_path->has_dB) { + pa_cvolume reset; - /* Hmm, so the hardware volume changed, let's reset our software volume */ - pa_cvolume_reset(&reset, s->sample_spec.channels); - pa_sink_set_soft_volume(s, &reset); - } + /* Hmm, so the hardware volume changed, let's reset our software volume */ + pa_cvolume_reset(&reset, s->sample_spec.channels); + pa_sink_set_soft_volume(s, &reset); } - - return; - -fail: - pa_log_error("Unable to read volume: %s", snd_strerror(err)); } static void sink_set_volume_cb(pa_sink *s) { struct userdata *u = s->userdata; - int err; - unsigned i; pa_cvolume r; + char t[PA_CVOLUME_SNPRINT_MAX]; pa_assert(u); - pa_assert(u->mixer_elem); - - if (u->mixer_seperate_channels) { - - r.channels = s->sample_spec.channels; - - for (i = 0; i < s->sample_spec.channels; i++) { - long alsa_vol; - pa_volume_t vol; - - vol = s->virtual_volume.values[i]; - - if (u->hw_dB_supported) { - - alsa_vol = (long) (pa_sw_volume_to_dB(vol) * 100); - alsa_vol += u->hw_dB_max; - alsa_vol = PA_CLAMP_UNLIKELY(alsa_vol, u->hw_dB_min, u->hw_dB_max); - - if ((err = snd_mixer_selem_set_playback_dB(u->mixer_elem, u->mixer_map[i], alsa_vol, 1)) < 0) - goto fail; - - if ((err = snd_mixer_selem_get_playback_dB(u->mixer_elem, u->mixer_map[i], &alsa_vol)) < 0) - goto fail; - -#ifdef HAVE_VALGRIND_MEMCHECK_H - VALGRIND_MAKE_MEM_DEFINED(&alsa_vol, sizeof(alsa_vol)); -#endif - - r.values[i] = pa_sw_volume_from_dB((double) (alsa_vol - u->hw_dB_max) / 100.0); - - } else { - alsa_vol = to_alsa_volume(u, vol); - - if ((err = snd_mixer_selem_set_playback_volume(u->mixer_elem, u->mixer_map[i], alsa_vol)) < 0) - goto fail; - - if ((err = snd_mixer_selem_get_playback_volume(u->mixer_elem, u->mixer_map[i], &alsa_vol)) < 0) - goto fail; - - r.values[i] = from_alsa_volume(u, alsa_vol); - } - } - - } else { - pa_volume_t vol; - long alsa_vol; - - vol = pa_cvolume_max(&s->virtual_volume); - - if (u->hw_dB_supported) { - alsa_vol = (long) (pa_sw_volume_to_dB(vol) * 100); - alsa_vol += u->hw_dB_max; - alsa_vol = PA_CLAMP_UNLIKELY(alsa_vol, u->hw_dB_min, u->hw_dB_max); - - if ((err = snd_mixer_selem_set_playback_dB_all(u->mixer_elem, alsa_vol, 1)) < 0) - goto fail; - - if ((err = snd_mixer_selem_get_playback_dB(u->mixer_elem, SND_MIXER_SCHN_MONO, &alsa_vol)) < 0) - goto fail; - -#ifdef HAVE_VALGRIND_MEMCHECK_H - VALGRIND_MAKE_MEM_DEFINED(&alsa_vol, sizeof(alsa_vol)); -#endif - - pa_cvolume_set(&r, s->sample_spec.channels, pa_sw_volume_from_dB((double) (alsa_vol - u->hw_dB_max) / 100.0)); - - } else { - alsa_vol = to_alsa_volume(u, vol); + pa_assert(u->mixer_path); + pa_assert(u->mixer_handle); - if ((err = snd_mixer_selem_set_playback_volume_all(u->mixer_elem, alsa_vol)) < 0) - goto fail; + /* Shift up by the base volume */ + pa_sw_cvolume_divide_scalar(&r, &s->virtual_volume, s->base_volume); - if ((err = snd_mixer_selem_get_playback_volume(u->mixer_elem, SND_MIXER_SCHN_MONO, &alsa_vol)) < 0) - goto fail; + if (pa_alsa_path_set_volume(u->mixer_path, u->mixer_handle, &s->channel_map, &r) < 0) + return; - pa_cvolume_set(&r, s->sample_spec.channels, from_alsa_volume(u, alsa_vol)); - } - } + /* Shift down by the base volume, so that 0dB becomes maximum volume */ + pa_sw_cvolume_multiply_scalar(&r, &r, s->base_volume); u->hardware_volume = r; - if (u->hw_dB_supported) { - char t[PA_CVOLUME_SNPRINT_MAX]; + if (u->mixer_path->has_dB) { /* Match exactly what the user requested by software */ pa_sw_cvolume_divide(&s->soft_volume, &s->virtual_volume, &u->hardware_volume); @@ -1119,45 +1049,75 @@ static void sink_set_volume_cb(pa_sink *s) { pa_log_debug("Got hardware volume: %s", pa_cvolume_snprint(t, sizeof(t), &u->hardware_volume)); pa_log_debug("Calculated software volume: %s", pa_cvolume_snprint(t, sizeof(t), &s->soft_volume)); - } else + } else { + pa_log_debug("Wrote hardware volume: %s", pa_cvolume_snprint(t, sizeof(t), &r)); /* We can't match exactly what the user requested, hence let's * at least tell the user about it */ s->virtual_volume = r; - - return; - -fail: - pa_log_error("Unable to set volume: %s", snd_strerror(err)); + } } static void sink_get_mute_cb(pa_sink *s) { struct userdata *u = s->userdata; - int err, sw; + pa_bool_t b; pa_assert(u); - pa_assert(u->mixer_elem); + pa_assert(u->mixer_path); + pa_assert(u->mixer_handle); - if ((err = snd_mixer_selem_get_playback_switch(u->mixer_elem, 0, &sw)) < 0) { - pa_log_error("Unable to get switch: %s", snd_strerror(err)); + if (pa_alsa_path_get_mute(u->mixer_path, u->mixer_handle, &b) < 0) return; - } - s->muted = !sw; + s->muted = b; } static void sink_set_mute_cb(pa_sink *s) { struct userdata *u = s->userdata; - int err; pa_assert(u); - pa_assert(u->mixer_elem); + pa_assert(u->mixer_path); + pa_assert(u->mixer_handle); - if ((err = snd_mixer_selem_set_playback_switch_all(u->mixer_elem, !s->muted)) < 0) { - pa_log_error("Unable to set switch: %s", snd_strerror(err)); - return; + pa_alsa_path_set_mute(u->mixer_path, u->mixer_handle, s->muted); +} + +static int sink_set_port_cb(pa_sink *s, pa_device_port *p) { + struct userdata *u = s->userdata; + pa_alsa_port_data *data; + + pa_assert(u); + pa_assert(p); + pa_assert(u->mixer_handle); + + data = PA_DEVICE_PORT_DATA(p); + + pa_assert_se(u->mixer_path = data->path); + pa_alsa_path_select(u->mixer_path, u->mixer_handle); + + if (u->mixer_path->has_volume && u->mixer_path->has_dB) { + s->base_volume = pa_sw_volume_from_dB(-u->mixer_path->max_dB); + s->n_volume_steps = PA_VOLUME_NORM+1; + + if (u->mixer_path->max_dB > 0.0) + pa_log_info("Fixing base volume to %0.2f dB", pa_sw_volume_to_dB(s->base_volume)); + else + pa_log_info("No particular base volume set, fixing to 0 dB"); + } else { + s->base_volume = PA_VOLUME_NORM; + s->n_volume_steps = u->mixer_path->max_volume - u->mixer_path->min_volume + 1; } + + if (data->setting) + pa_alsa_setting_select(data->setting, u->mixer_handle); + + if (s->set_mute) + s->set_mute(s); + if (s->set_volume) + s->set_volume(s); + + return 0; } static void sink_update_requested_latency_cb(pa_sink *s) { @@ -1173,7 +1133,7 @@ static void sink_update_requested_latency_cb(pa_sink *s) { /* Let's check whether we now use only a smaller part of the buffer then before. If so, we need to make sure that subsequent - rewinds are relative to the new maxium fill level and not to the + rewinds are relative to the new maximum fill level and not to the current fill level. Thus, let's do a full rewind once, to clear things up. */ @@ -1194,7 +1154,7 @@ static int process_rewind(struct userdata *u) { pa_log_debug("Requested to rewind %lu bytes.", (unsigned long) rewind_nbytes); if (PA_UNLIKELY((unused = pa_alsa_safe_avail(u->pcm_handle, u->hwbuf_size, &u->sink->sample_spec)) < 0)) { - pa_log("snd_pcm_avail() failed: %s", snd_strerror((int) unused)); + pa_log("snd_pcm_avail() failed: %s", pa_alsa_strerror((int) unused)); return -1; } @@ -1216,7 +1176,7 @@ static int process_rewind(struct userdata *u) { in_frames = (snd_pcm_sframes_t) (rewind_nbytes / u->frame_size); pa_log_debug("before: %lu", (unsigned long) in_frames); if ((out_frames = snd_pcm_rewind(u->pcm_handle, (snd_pcm_uframes_t) in_frames)) < 0) { - pa_log("snd_pcm_rewind() failed: %s", snd_strerror((int) out_frames)); + pa_log("snd_pcm_rewind() failed: %s", pa_alsa_strerror((int) out_frames)); return -1; } pa_log_debug("after: %lu", (unsigned long) out_frames); @@ -1252,7 +1212,6 @@ static void thread_func(void *userdata) { pa_make_realtime(u->core->realtime_priority); pa_thread_mq_install(&u->thread_mq); - pa_rtpoll_install(u->rtpoll); for (;;) { int ret; @@ -1286,7 +1245,7 @@ static void thread_func(void *userdata) { pa_log_info("Starting playback."); snd_pcm_start(u->pcm_handle); - pa_smoother_resume(u->smoother, pa_rtclock_usec()); + pa_smoother_resume(u->smoother, pa_rtclock_now(), TRUE); } update_smoother(u); @@ -1300,7 +1259,7 @@ static void thread_func(void *userdata) { /* USB devices on ALSA seem to hit a buffer * underrun during the first iterations much * quicker then we calculate here, probably due to - * the transport latency. To accomodate for that + * the transport latency. To accommodate for that * we artificially decrease the sleep time until * we have filled the buffer at least once * completely.*/ @@ -1315,7 +1274,7 @@ static void thread_func(void *userdata) { /* Convert from the sound card time domain to the * system time domain */ - cusec = pa_smoother_translate(u->smoother, pa_rtclock_usec(), sleep_usec); + cusec = pa_smoother_translate(u->smoother, pa_rtclock_now(), sleep_usec); /* pa_log_debug("Waking up in %0.2fms (system clock).", (double) cusec / PA_USEC_PER_MSEC); */ @@ -1347,7 +1306,7 @@ static void thread_func(void *userdata) { pollfd = pa_rtpoll_item_get_pollfd(u->alsa_rtpoll_item, &n); if ((err = snd_pcm_poll_descriptors_revents(u->pcm_handle, pollfd, n, &revents)) < 0) { - pa_log("snd_pcm_poll_descriptors_revents() failed: %s", snd_strerror(err)); + pa_log("snd_pcm_poll_descriptors_revents() failed: %s", pa_alsa_strerror(err)); goto fail; } @@ -1374,7 +1333,7 @@ finish: pa_log_debug("Thread shutting down"); } -static void set_sink_name(pa_sink_new_data *data, pa_modargs *ma, const char *device_id, const char *device_name) { +static void set_sink_name(pa_sink_new_data *data, pa_modargs *ma, const char *device_id, const char *device_name, pa_alsa_mapping *mapping) { const char *n; char *t; @@ -1395,82 +1354,136 @@ static void set_sink_name(pa_sink_new_data *data, pa_modargs *ma, const char *de data->namereg_fail = FALSE; } - t = pa_sprintf_malloc("alsa_output.%s", n); + if (mapping) + t = pa_sprintf_malloc("alsa_output.%s.%s", n, mapping->name); + else + t = pa_sprintf_malloc("alsa_output.%s", n); + pa_sink_new_data_set_name(data, t); pa_xfree(t); } +static void find_mixer(struct userdata *u, pa_alsa_mapping *mapping, const char *element, pa_bool_t ignore_dB) { + + if (!mapping && !element) + return; + + if (!(u->mixer_handle = pa_alsa_open_mixer_for_pcm(u->pcm_handle, &u->control_device))) { + pa_log_info("Failed to find a working mixer device."); + return; + } + + if (element) { + + if (!(u->mixer_path = pa_alsa_path_synthesize(element, PA_ALSA_DIRECTION_OUTPUT))) + goto fail; + + if (pa_alsa_path_probe(u->mixer_path, u->mixer_handle, ignore_dB) < 0) + goto fail; + + pa_log_debug("Probed mixer path %s:", u->mixer_path->name); + pa_alsa_path_dump(u->mixer_path); + } else { + + if (!(u->mixer_path_set = pa_alsa_path_set_new(mapping, PA_ALSA_DIRECTION_OUTPUT))) + goto fail; + + pa_alsa_path_set_probe(u->mixer_path_set, u->mixer_handle, ignore_dB); + + pa_log_debug("Probed mixer paths:"); + pa_alsa_path_set_dump(u->mixer_path_set); + } + + return; + +fail: + + if (u->mixer_path_set) { + pa_alsa_path_set_free(u->mixer_path_set); + u->mixer_path_set = NULL; + } else if (u->mixer_path) { + pa_alsa_path_free(u->mixer_path); + u->mixer_path = NULL; + } + + if (u->mixer_handle) { + snd_mixer_close(u->mixer_handle); + u->mixer_handle = NULL; + } +} + static int setup_mixer(struct userdata *u, pa_bool_t ignore_dB) { pa_assert(u); if (!u->mixer_handle) return 0; - pa_assert(u->mixer_elem); + if (u->sink->active_port) { + pa_alsa_port_data *data; - if (snd_mixer_selem_has_playback_volume(u->mixer_elem)) { - pa_bool_t suitable = FALSE; + /* We have a list of supported paths, so let's activate the + * one that has been chosen as active */ - if (snd_mixer_selem_get_playback_volume_range(u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max) < 0) - pa_log_info("Failed to get volume range. Falling back to software volume control."); - else if (u->hw_volume_min >= u->hw_volume_max) - pa_log_warn("Your kernel driver is broken: it reports a volume range from %li to %li which makes no sense.", u->hw_volume_min, u->hw_volume_max); - else { - pa_log_info("Volume ranges from %li to %li.", u->hw_volume_min, u->hw_volume_max); - suitable = TRUE; - } + data = PA_DEVICE_PORT_DATA(u->sink->active_port); + u->mixer_path = data->path; - if (suitable) { - if (ignore_dB || snd_mixer_selem_get_playback_dB_range(u->mixer_elem, &u->hw_dB_min, &u->hw_dB_max) < 0) - pa_log_info("Mixer doesn't support dB information or data is ignored."); - else { -#ifdef HAVE_VALGRIND_MEMCHECK_H - VALGRIND_MAKE_MEM_DEFINED(&u->hw_dB_min, sizeof(u->hw_dB_min)); - VALGRIND_MAKE_MEM_DEFINED(&u->hw_dB_max, sizeof(u->hw_dB_max)); -#endif + pa_alsa_path_select(data->path, u->mixer_handle); - if (u->hw_dB_min >= u->hw_dB_max) - pa_log_warn("Your kernel driver is broken: it reports a volume range from %0.2f dB to %0.2f dB which makes no sense.", (double) u->hw_dB_min/100.0, (double) u->hw_dB_max/100.0); - else { - pa_log_info("Volume ranges from %0.2f dB to %0.2f dB.", (double) u->hw_dB_min/100.0, (double) u->hw_dB_max/100.0); - u->hw_dB_supported = TRUE; - - if (u->hw_dB_max > 0) { - u->sink->base_volume = pa_sw_volume_from_dB(- (double) u->hw_dB_max/100.0); - pa_log_info("Fixing base volume to %0.2f dB", pa_sw_volume_to_dB(u->sink->base_volume)); - } else - pa_log_info("No particular base volume set, fixing to 0 dB"); - } - } + if (data->setting) + pa_alsa_setting_select(data->setting, u->mixer_handle); - if (!u->hw_dB_supported && - u->hw_volume_max - u->hw_volume_min < 3) { + } else { - pa_log_info("Device doesn't do dB volume and has less than 4 volume levels. Falling back to software volume control."); - suitable = FALSE; - } - } + if (!u->mixer_path && u->mixer_path_set) + u->mixer_path = u->mixer_path_set->paths; - if (suitable) { - u->mixer_seperate_channels = pa_alsa_calc_mixer_map(u->mixer_elem, &u->sink->channel_map, u->mixer_map, TRUE) >= 0; + if (u->mixer_path) { + /* Hmm, we have only a single path, then let's activate it */ - u->sink->get_volume = sink_get_volume_cb; - u->sink->set_volume = sink_set_volume_cb; - u->sink->flags |= PA_SINK_HW_VOLUME_CTRL | (u->hw_dB_supported ? PA_SINK_DECIBEL_VOLUME : 0); - pa_log_info("Using hardware volume control. Hardware dB scale %s.", u->hw_dB_supported ? "supported" : "not supported"); + pa_alsa_path_select(u->mixer_path, u->mixer_handle); - if (!u->hw_dB_supported) - u->sink->n_volume_steps = u->hw_volume_max - u->hw_volume_min + 1; + if (u->mixer_path->settings) + pa_alsa_setting_select(u->mixer_path->settings, u->mixer_handle); } else - pa_log_info("Using software volume control."); + return 0; } - if (snd_mixer_selem_has_playback_switch(u->mixer_elem)) { + if (!u->mixer_path->has_volume) + pa_log_info("Driver does not support hardware volume control, falling back to software volume control."); + else { + + if (u->mixer_path->has_dB) { + pa_log_info("Hardware volume ranges from %0.2f dB to %0.2f dB.", u->mixer_path->min_dB, u->mixer_path->max_dB); + + u->sink->base_volume = pa_sw_volume_from_dB(-u->mixer_path->max_dB); + u->sink->n_volume_steps = PA_VOLUME_NORM+1; + + if (u->mixer_path->max_dB > 0.0) + pa_log_info("Fixing base volume to %0.2f dB", pa_sw_volume_to_dB(u->sink->base_volume)); + else + pa_log_info("No particular base volume set, fixing to 0 dB"); + + } else { + pa_log_info("Hardware volume ranges from %li to %li.", u->mixer_path->min_volume, u->mixer_path->max_volume); + u->sink->base_volume = PA_VOLUME_NORM; + u->sink->n_volume_steps = u->mixer_path->max_volume - u->mixer_path->min_volume + 1; + } + + u->sink->get_volume = sink_get_volume_cb; + u->sink->set_volume = sink_set_volume_cb; + + u->sink->flags |= PA_SINK_HW_VOLUME_CTRL | (u->mixer_path->has_dB ? PA_SINK_DECIBEL_VOLUME : 0); + pa_log_info("Using hardware volume control. Hardware dB scale %s.", u->mixer_path->has_dB ? "supported" : "not supported"); + } + + if (!u->mixer_path->has_mute) { + pa_log_info("Driver does not support hardware mute control, falling back to software mute control."); + } else { u->sink->get_mute = sink_get_mute_cb; u->sink->set_mute = sink_set_mute_cb; u->sink->flags |= PA_SINK_HW_MUTE_CTRL; - } else - pa_log_info("Using software mute control."); + pa_log_info("Using hardware mute control."); + } u->mixer_fdl = pa_alsa_fdlist_new(); @@ -1479,13 +1492,15 @@ static int setup_mixer(struct userdata *u, pa_bool_t ignore_dB) { return -1; } - snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback); - snd_mixer_elem_set_callback_private(u->mixer_elem, u); + if (u->mixer_path_set) + pa_alsa_path_set_set_callback(u->mixer_path_set, u->mixer_handle, mixer_callback, u); + else + pa_alsa_path_set_callback(u->mixer_path, u->mixer_handle, mixer_callback, u); return 0; } -pa_sink *pa_alsa_sink_new(pa_module *m, pa_modargs *ma, const char*driver, pa_card *card, const pa_alsa_profile_info *profile) { +pa_sink *pa_alsa_sink_new(pa_module *m, pa_modargs *ma, const char*driver, pa_card *card, pa_alsa_mapping *mapping) { struct userdata *u = NULL; const char *dev_id = NULL; @@ -1495,8 +1510,8 @@ pa_sink *pa_alsa_sink_new(pa_module *m, pa_modargs *ma, const char*driver, pa_ca snd_pcm_uframes_t period_frames, tsched_frames; size_t frame_size; pa_bool_t use_mmap = TRUE, b, use_tsched = TRUE, d, ignore_dB = FALSE; - pa_usec_t usec; pa_sink_new_data data; + pa_alsa_profile_set *profile_set = NULL; pa_assert(m); pa_assert(ma); @@ -1559,45 +1574,57 @@ pa_sink *pa_alsa_sink_new(pa_module *m, pa_modargs *ma, const char*driver, pa_ca u->rtpoll = pa_rtpoll_new(); pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll); - u->smoother = pa_smoother_new(DEFAULT_TSCHED_BUFFER_USEC*2, DEFAULT_TSCHED_BUFFER_USEC*2, TRUE, 5); - usec = pa_rtclock_usec(); - pa_smoother_set_time_offset(u->smoother, usec); - pa_smoother_pause(u->smoother, usec); + u->smoother = pa_smoother_new( + DEFAULT_TSCHED_BUFFER_USEC*2, + DEFAULT_TSCHED_BUFFER_USEC*2, + TRUE, + TRUE, + 5, + pa_rtclock_now(), + TRUE); + + dev_id = pa_modargs_get_value( + ma, "device_id", + pa_modargs_get_value(ma, "device", DEFAULT_DEVICE)); + + if (reserve_init(u, dev_id) < 0) + goto fail; - if (reserve_init(u, pa_modargs_get_value( - ma, "device_id", - pa_modargs_get_value(ma, "device", DEFAULT_DEVICE))) < 0) + if (reserve_monitor_init(u, dev_id) < 0) goto fail; b = use_mmap; d = use_tsched; - if (profile) { + if (mapping) { if (!(dev_id = pa_modargs_get_value(ma, "device_id", NULL))) { pa_log("device_id= not set"); goto fail; } - if (!(u->pcm_handle = pa_alsa_open_by_device_id_profile( + if (!(u->pcm_handle = pa_alsa_open_by_device_id_mapping( dev_id, &u->device_name, &ss, &map, SND_PCM_STREAM_PLAYBACK, &nfrags, &period_frames, tsched_frames, - &b, &d, profile))) + &b, &d, mapping))) goto fail; } else if ((dev_id = pa_modargs_get_value(ma, "device_id", NULL))) { + if (!(profile_set = pa_alsa_profile_set_new(NULL, &map))) + goto fail; + if (!(u->pcm_handle = pa_alsa_open_by_device_id_auto( dev_id, &u->device_name, &ss, &map, SND_PCM_STREAM_PLAYBACK, &nfrags, &period_frames, tsched_frames, - &b, &d, &profile))) + &b, &d, profile_set, &mapping))) goto fail; @@ -1611,14 +1638,18 @@ pa_sink *pa_alsa_sink_new(pa_module *m, pa_modargs *ma, const char*driver, pa_ca &nfrags, &period_frames, tsched_frames, &b, &d, FALSE))) goto fail; - } pa_assert(u->device_name); pa_log_info("Successfully opened device %s.", u->device_name); - if (profile) - pa_log_info("Selected configuration '%s' (%s).", profile->description, profile->name); + if (pa_alsa_pcm_is_modem(u->pcm_handle)) { + pa_log_notice("Device %s is modem, refusing further initialization.", u->device_name); + goto fail; + } + + if (mapping) + pa_log_info("Selected mapping '%s' (%s).", mapping->description, mapping->name); if (use_mmap && !b) { pa_log_info("Device doesn't support mmap(), falling back to UNIX read/write mode."); @@ -1630,6 +1661,11 @@ pa_sink *pa_alsa_sink_new(pa_module *m, pa_modargs *ma, const char*driver, pa_ca u->use_tsched = use_tsched = FALSE; } + if (use_tsched && !pa_alsa_pcm_is_hw(u->pcm_handle)) { + pa_log_info("Device is not a hardware device, disabling timer-based scheduling."); + u->use_tsched = use_tsched = FALSE; + } + if (u->use_mmap) pa_log_info("Successfully enabled mmap() mode."); @@ -1639,13 +1675,13 @@ pa_sink *pa_alsa_sink_new(pa_module *m, pa_modargs *ma, const char*driver, pa_ca /* ALSA might tweak the sample spec, so recalculate the frame size */ frame_size = pa_frame_size(&ss); - pa_alsa_find_mixer_and_elem(u->pcm_handle, &u->mixer_handle, &u->mixer_elem); + find_mixer(u, mapping, pa_modargs_get_value(ma, "control", NULL), ignore_dB); pa_sink_new_data_init(&data); data.driver = driver; data.module = m; data.card = card; - set_sink_name(&data, ma, dev_id, u->device_name); + set_sink_name(&data, ma, dev_id, u->device_name, mapping); pa_sink_new_data_set_sample_spec(&data, &ss); pa_sink_new_data_set_channel_map(&data, &map); @@ -1655,14 +1691,26 @@ pa_sink *pa_alsa_sink_new(pa_module *m, pa_modargs *ma, const char*driver, pa_ca pa_proplist_setf(data.proplist, PA_PROP_DEVICE_BUFFERING_FRAGMENT_SIZE, "%lu", (unsigned long) (period_frames * frame_size)); pa_proplist_sets(data.proplist, PA_PROP_DEVICE_ACCESS_MODE, u->use_tsched ? "mmap+timer" : (u->use_mmap ? "mmap" : "serial")); - if (profile) { - pa_proplist_sets(data.proplist, PA_PROP_DEVICE_PROFILE_NAME, profile->name); - pa_proplist_sets(data.proplist, PA_PROP_DEVICE_PROFILE_DESCRIPTION, profile->description); + if (mapping) { + pa_proplist_sets(data.proplist, PA_PROP_DEVICE_PROFILE_NAME, mapping->name); + pa_proplist_sets(data.proplist, PA_PROP_DEVICE_PROFILE_DESCRIPTION, mapping->description); } pa_alsa_init_description(data.proplist); - u->sink = pa_sink_new(m->core, &data, PA_SINK_HARDWARE|PA_SINK_LATENCY); + if (u->control_device) + pa_alsa_init_proplist_ctl(data.proplist, u->control_device); + + if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) { + pa_log("Invalid properties"); + pa_sink_new_data_done(&data); + goto fail; + } + + if (u->mixer_path_set) + pa_alsa_add_ports(&data.ports, u->mixer_path_set); + + u->sink = pa_sink_new(m->core, &data, PA_SINK_HARDWARE|PA_SINK_LATENCY|(u->use_tsched ? PA_SINK_DYNAMIC_LATENCY : 0)); pa_sink_new_data_done(&data); if (!u->sink) { @@ -1673,6 +1721,7 @@ pa_sink *pa_alsa_sink_new(pa_module *m, pa_modargs *ma, const char*driver, pa_ca u->sink->parent.process_msg = sink_process_msg; u->sink->update_requested_latency = sink_update_requested_latency_cb; u->sink->set_state = sink_set_state_cb; + u->sink->set_port = sink_set_port_cb; u->sink->userdata = u; pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq); @@ -1685,26 +1734,27 @@ pa_sink *pa_alsa_sink_new(pa_module *m, pa_modargs *ma, const char*driver, pa_ca u->tsched_watermark = pa_usec_to_bytes_round_up(pa_bytes_to_usec_round_up(tsched_watermark, &requested_ss), &u->sink->sample_spec); pa_cvolume_mute(&u->hardware_volume, u->sink->sample_spec.channels); - if (use_tsched) { - fix_min_sleep_wakeup(u); - fix_tsched_watermark(u); + pa_log_info("Using %u fragments of size %lu bytes, buffer time is %0.2fms", + nfrags, (long unsigned) u->fragment_size, + (double) pa_bytes_to_usec(u->hwbuf_size, &ss) / PA_USEC_PER_MSEC); + pa_sink_set_max_request(u->sink, u->hwbuf_size); + pa_sink_set_max_rewind(u->sink, u->hwbuf_size); + + if (u->use_tsched) { u->watermark_step = pa_usec_to_bytes(TSCHED_WATERMARK_STEP_USEC, &u->sink->sample_spec); - } - pa_sink_set_max_rewind(u->sink, use_tsched ? u->hwbuf_size : 0); - pa_sink_set_max_request(u->sink, u->hwbuf_size); - pa_sink_set_latency_range(u->sink, - use_tsched ? (pa_usec_t) -1 : pa_bytes_to_usec(u->hwbuf_size, &ss), - pa_bytes_to_usec(u->hwbuf_size, &ss)); + fix_min_sleep_wakeup(u); + fix_tsched_watermark(u); - pa_log_info("Using %u fragments of size %lu bytes, buffer time is %0.2fms", - nfrags, (long unsigned) u->fragment_size, - (double) pa_bytes_to_usec(u->hwbuf_size, &ss) / PA_USEC_PER_MSEC); + pa_sink_set_latency_range(u->sink, + 0, + pa_bytes_to_usec(u->hwbuf_size, &ss)); - if (use_tsched) pa_log_info("Time scheduling watermark is %0.2fms", (double) pa_bytes_to_usec(u->tsched_watermark, &ss) / PA_USEC_PER_MSEC); + } else + pa_sink_set_fixed_latency(u->sink, pa_bytes_to_usec(u->hwbuf_size, &ss)); reserve_update(u); @@ -1714,7 +1764,7 @@ pa_sink *pa_alsa_sink_new(pa_module *m, pa_modargs *ma, const char*driver, pa_ca if (setup_mixer(u, ignore_dB) < 0) goto fail; - pa_alsa_dump(u->pcm_handle); + pa_alsa_dump(PA_LOG_DEBUG, u->pcm_handle); if (!(u->thread = pa_thread_new(thread_func, u))) { pa_log("Failed to create thread."); @@ -1740,11 +1790,18 @@ pa_sink *pa_alsa_sink_new(pa_module *m, pa_modargs *ma, const char*driver, pa_ca pa_sink_put(u->sink); + if (profile_set) + pa_alsa_profile_set_free(profile_set); + return u->sink; fail: - userdata_free(u); + if (u) + userdata_free(u); + + if (profile_set) + pa_alsa_profile_set_free(profile_set); return NULL; } @@ -1774,23 +1831,30 @@ static void userdata_free(struct userdata *u) { if (u->rtpoll) pa_rtpoll_free(u->rtpoll); + if (u->pcm_handle) { + snd_pcm_drop(u->pcm_handle); + snd_pcm_close(u->pcm_handle); + } + if (u->mixer_fdl) pa_alsa_fdlist_free(u->mixer_fdl); + if (u->mixer_path_set) + pa_alsa_path_set_free(u->mixer_path_set); + else if (u->mixer_path) + pa_alsa_path_free(u->mixer_path); + if (u->mixer_handle) snd_mixer_close(u->mixer_handle); - if (u->pcm_handle) { - snd_pcm_drop(u->pcm_handle); - snd_pcm_close(u->pcm_handle); - } - if (u->smoother) pa_smoother_free(u->smoother); reserve_done(u); + monitor_done(u); pa_xfree(u->device_name); + pa_xfree(u->control_device); pa_xfree(u); } diff --git a/src/modules/alsa/alsa-sink.h b/src/modules/alsa/alsa-sink.h index bbf64234..b9a4ac2a 100644 --- a/src/modules/alsa/alsa-sink.h +++ b/src/modules/alsa/alsa-sink.h @@ -28,8 +28,9 @@ #include <pulsecore/sink.h> #include "alsa-util.h" +#include "alsa-mixer.h" -pa_sink* pa_alsa_sink_new(pa_module *m, pa_modargs *ma, const char*driver, pa_card *card, const pa_alsa_profile_info *profile); +pa_sink* pa_alsa_sink_new(pa_module *m, pa_modargs *ma, const char*driver, pa_card *card, pa_alsa_mapping *mapping); void pa_alsa_sink_free(pa_sink *s); diff --git a/src/modules/alsa/alsa-source.c b/src/modules/alsa/alsa-source.c index 63b5e460..13a2c186 100644 --- a/src/modules/alsa/alsa-source.c +++ b/src/modules/alsa/alsa-source.c @@ -28,14 +28,11 @@ #include <asoundlib.h> -#ifdef HAVE_VALGRIND_MEMCHECK_H -#include <valgrind/memcheck.h> -#endif - -#include <pulse/xmalloc.h> -#include <pulse/util.h> -#include <pulse/timeval.h> #include <pulse/i18n.h> +#include <pulse/rtclock.h> +#include <pulse/timeval.h> +#include <pulse/util.h> +#include <pulse/xmalloc.h> #include <pulsecore/core-error.h> #include <pulsecore/core.h> @@ -43,6 +40,7 @@ #include <pulsecore/memchunk.h> #include <pulsecore/sink.h> #include <pulsecore/modargs.h> +#include <pulsecore/core-rtclock.h> #include <pulsecore/core-util.h> #include <pulsecore/sample-util.h> #include <pulsecore/log.h> @@ -52,7 +50,6 @@ #include <pulsecore/thread-mq.h> #include <pulsecore/rtpoll.h> #include <pulsecore/time-smoother.h> -#include <pulsecore/rtclock.h> #include <modules/reserve-wrap.h> @@ -81,11 +78,8 @@ struct userdata { pa_alsa_fdlist *mixer_fdl; snd_mixer_t *mixer_handle; - snd_mixer_elem_t *mixer_elem; - long hw_volume_max, hw_volume_min; - long hw_dB_max, hw_dB_min; - pa_bool_t hw_dB_supported:1; - pa_bool_t mixer_seperate_channels:1; + pa_alsa_path_set *mixer_path_set; + pa_alsa_path *mixer_path; pa_cvolume hardware_volume; @@ -102,6 +96,7 @@ struct userdata { unsigned nfragments; char *device_name; + char *control_device; pa_bool_t use_mmap:1, use_tsched:1; @@ -114,6 +109,8 @@ struct userdata { pa_reserve_wrapper *reserve; pa_hook_slot *reserve_slot; + pa_reserve_monitor_wrapper *monitor; + pa_hook_slot *monitor_slot; }; static void userdata_free(struct userdata *u); @@ -122,7 +119,7 @@ static pa_hook_result_t reserve_cb(pa_reserve_wrapper *r, void *forced, struct u pa_assert(r); pa_assert(u); - if (pa_source_suspend(u->source, TRUE) < 0) + if (pa_source_suspend(u->source, TRUE, PA_SUSPEND_APPLICATION) < 0) return PA_HOOK_CANCEL; return PA_HOOK_OK; @@ -183,6 +180,57 @@ static int reserve_init(struct userdata *u, const char *dname) { return 0; } +static pa_hook_result_t monitor_cb(pa_reserve_monitor_wrapper *w, void* busy, struct userdata *u) { + pa_bool_t b; + + pa_assert(w); + pa_assert(u); + + b = PA_PTR_TO_UINT(busy) && !u->reserve; + + pa_source_suspend(u->source, b, PA_SUSPEND_APPLICATION); + return PA_HOOK_OK; +} + +static void monitor_done(struct userdata *u) { + pa_assert(u); + + if (u->monitor_slot) { + pa_hook_slot_free(u->monitor_slot); + u->monitor_slot = NULL; + } + + if (u->monitor) { + pa_reserve_monitor_wrapper_unref(u->monitor); + u->monitor = NULL; + } +} + +static int reserve_monitor_init(struct userdata *u, const char *dname) { + char *rname; + + pa_assert(u); + pa_assert(dname); + + if (pa_in_system_mode()) + return 0; + + /* We are resuming, try to lock the device */ + if (!(rname = pa_alsa_get_reserve_name(dname))) + return 0; + + u->monitor = pa_reserve_monitor_wrapper_get(u->core, rname); + pa_xfree(rname); + + if (!(u->monitor)) + return -1; + + pa_assert(!u->monitor_slot); + u->monitor_slot = pa_hook_connect(pa_reserve_monitor_wrapper_hook(u->monitor), PA_HOOK_NORMAL, (pa_hook_cb_t) monitor_cb, u); + + return 0; +} + static void fix_min_sleep_wakeup(struct userdata *u) { size_t max_use, max_use_2; pa_assert(u); @@ -238,7 +286,7 @@ static void adjust_after_overrun(struct userdata *u) { pa_log_notice("Increasing minimal latency to %0.2f ms", (double) new_min_latency / PA_USEC_PER_MSEC); - pa_source_update_latency_range(u->source, new_min_latency, u->source->thread_info.max_latency); + pa_source_set_latency_range_within_thread(u->source, new_min_latency, u->source->thread_info.max_latency); return; } @@ -278,7 +326,7 @@ static int try_recover(struct userdata *u, const char *call, int err) { pa_assert(call); pa_assert(err < 0); - pa_log_debug("%s: %s", call, snd_strerror(err)); + pa_log_debug("%s: %s", call, pa_alsa_strerror(err)); pa_assert(err != -EAGAIN); @@ -286,7 +334,7 @@ static int try_recover(struct userdata *u, const char *call, int err) { pa_log_debug("%s: Buffer overrun!", call); if ((err = snd_pcm_recover(u->pcm_handle, err, 1)) < 0) { - pa_log("%s: %s", call, snd_strerror(err)); + pa_log("%s: %s", call, pa_alsa_strerror(err)); return -1; } @@ -455,7 +503,7 @@ static int mmap_read(struct userdata *u, pa_usec_t *sleep_usec, pa_bool_t polled u->read_count += frames * u->frame_size; #ifdef DEBUG_TIMING - pa_log_debug("Read %lu bytes", (unsigned long) (frames * u->frame_size)); + pa_log_debug("Read %lu bytes (of possible %lu bytes)", (unsigned long) (frames * u->frame_size), (unsigned long) n_bytes); #endif if ((size_t) frames * u->frame_size >= n_bytes) @@ -465,7 +513,13 @@ static int mmap_read(struct userdata *u, pa_usec_t *sleep_usec, pa_bool_t polled } } - *sleep_usec = pa_bytes_to_usec(left_to_record, &u->source->sample_spec) - process_usec; + *sleep_usec = pa_bytes_to_usec(left_to_record, &u->source->sample_spec); + + if (*sleep_usec > process_usec) + *sleep_usec -= process_usec; + else + *sleep_usec = 0; + return work_done ? 1 : 0; } @@ -575,7 +629,13 @@ static int unix_read(struct userdata *u, pa_usec_t *sleep_usec, pa_bool_t polled } } - *sleep_usec = pa_bytes_to_usec(left_to_record, &u->source->sample_spec) - process_usec; + *sleep_usec = pa_bytes_to_usec(left_to_record, &u->source->sample_spec); + + if (*sleep_usec > process_usec) + *sleep_usec -= process_usec; + else + *sleep_usec = 0; + return work_done ? 1 : 0; } @@ -594,12 +654,12 @@ static void update_smoother(struct userdata *u) { /* Let's update the time smoother */ if (PA_UNLIKELY((err = pa_alsa_safe_delay(u->pcm_handle, &delay, u->hwbuf_size, &u->source->sample_spec)) < 0)) { - pa_log_warn("Failed to get delay: %s", snd_strerror(err)); + pa_log_warn("Failed to get delay: %s", pa_alsa_strerror(err)); return; } if (PA_UNLIKELY((err = snd_pcm_status(u->pcm_handle, status)) < 0)) - pa_log_warn("Failed to get timestamp: %s", snd_strerror(err)); + pa_log_warn("Failed to get timestamp: %s", pa_alsa_strerror(err)); else { snd_htimestamp_t htstamp = { 0, 0 }; snd_pcm_status_get_htstamp(status, &htstamp); @@ -610,7 +670,7 @@ static void update_smoother(struct userdata *u) { /* Hmm, if the timestamp is 0, then it wasn't set and we take the current time */ if (now1 <= 0) - now1 = pa_rtclock_usec(); + now1 = pa_rtclock_now(); now2 = pa_bytes_to_usec(position, &u->source->sample_spec); @@ -623,7 +683,7 @@ static pa_usec_t source_get_latency(struct userdata *u) { pa_assert(u); - now1 = pa_rtclock_usec(); + now1 = pa_rtclock_now(); now2 = pa_smoother_get(u->smoother, now1); delay = (int64_t) now2 - (int64_t) pa_bytes_to_usec(u->read_count, &u->source->sample_spec); @@ -648,7 +708,7 @@ static int suspend(struct userdata *u) { pa_assert(u); pa_assert(u->pcm_handle); - pa_smoother_pause(u->smoother, pa_rtclock_usec()); + pa_smoother_pause(u->smoother, pa_rtclock_now()); /* Let's suspend */ snd_pcm_close(u->pcm_handle); @@ -709,7 +769,7 @@ static int update_sw_params(struct userdata *u) { pa_log_debug("setting avail_min=%lu", (unsigned long) avail_min); if ((err = pa_alsa_set_sw_params(u->pcm_handle, avail_min)) < 0) { - pa_log("Failed to set software parameters: %s", snd_strerror(err)); + pa_log("Failed to set software parameters: %s", pa_alsa_strerror(err)); return err; } @@ -728,14 +788,12 @@ static int unsuspend(struct userdata *u) { pa_log_info("Trying resume..."); - snd_config_update_free_global(); - if ((err = snd_pcm_open(&u->pcm_handle, u->device_name, SND_PCM_STREAM_CAPTURE, /*SND_PCM_NONBLOCK|*/ SND_PCM_NO_AUTO_RESAMPLE| SND_PCM_NO_AUTO_CHANNELS| SND_PCM_NO_AUTO_FORMAT)) < 0) { - pa_log("Error opening PCM device %s: %s", u->device_name, snd_strerror(err)); + pa_log("Error opening PCM device %s: %s", u->device_name, pa_alsa_strerror(err)); goto fail; } @@ -746,7 +804,7 @@ static int unsuspend(struct userdata *u) { d = u->use_tsched; if ((err = pa_alsa_set_hw_params(u->pcm_handle, &ss, &nfrags, &period_size, u->hwbuf_size / u->frame_size, &b, &d, TRUE)) < 0) { - pa_log("Failed to set hardware parameters: %s", snd_strerror(err)); + pa_log("Failed to set hardware parameters: %s", pa_alsa_strerror(err)); goto fail; } @@ -776,7 +834,7 @@ static int unsuspend(struct userdata *u) { /* FIXME: We need to reload the volume somehow */ snd_pcm_start(u->pcm_handle); - pa_smoother_resume(u->smoother, pa_rtclock_usec()); + pa_smoother_resume(u->smoother, pa_rtclock_now(), TRUE); pa_log_info("Resumed successfully..."); @@ -884,239 +942,135 @@ static int mixer_callback(snd_mixer_elem_t *elem, unsigned int mask) { return 0; } -static pa_volume_t from_alsa_volume(struct userdata *u, long alsa_vol) { - - return (pa_volume_t) round(((double) (alsa_vol - u->hw_volume_min) * PA_VOLUME_NORM) / - (double) (u->hw_volume_max - u->hw_volume_min)); -} - -static long to_alsa_volume(struct userdata *u, pa_volume_t vol) { - long alsa_vol; - - alsa_vol = (long) round(((double) vol * (double) (u->hw_volume_max - u->hw_volume_min)) - / PA_VOLUME_NORM) + u->hw_volume_min; - - return PA_CLAMP_UNLIKELY(alsa_vol, u->hw_volume_min, u->hw_volume_max); -} - static void source_get_volume_cb(pa_source *s) { struct userdata *u = s->userdata; - int err; - unsigned i; pa_cvolume r; char t[PA_CVOLUME_SNPRINT_MAX]; pa_assert(u); - pa_assert(u->mixer_elem); - - if (u->mixer_seperate_channels) { - - r.channels = s->sample_spec.channels; - - for (i = 0; i < s->sample_spec.channels; i++) { - long alsa_vol; - - if (u->hw_dB_supported) { - - if ((err = snd_mixer_selem_get_capture_dB(u->mixer_elem, u->mixer_map[i], &alsa_vol)) < 0) - goto fail; - -#ifdef HAVE_VALGRIND_MEMCHECK_H - VALGRIND_MAKE_MEM_DEFINED(&alsa_vol, sizeof(alsa_vol)); -#endif - - r.values[i] = pa_sw_volume_from_dB((double) (alsa_vol - u->hw_dB_max) / 100.0); - } else { - - if ((err = snd_mixer_selem_get_capture_volume(u->mixer_elem, u->mixer_map[i], &alsa_vol)) < 0) - goto fail; - - r.values[i] = from_alsa_volume(u, alsa_vol); - } - } - - } else { - long alsa_vol; - - if (u->hw_dB_supported) { - - if ((err = snd_mixer_selem_get_capture_dB(u->mixer_elem, SND_MIXER_SCHN_MONO, &alsa_vol)) < 0) - goto fail; - -#ifdef HAVE_VALGRIND_MEMCHECK_H - VALGRIND_MAKE_MEM_DEFINED(&alsa_vol, sizeof(alsa_vol)); -#endif - - pa_cvolume_set(&r, s->sample_spec.channels, pa_sw_volume_from_dB((double) (alsa_vol - u->hw_dB_max) / 100.0)); - - } else { + pa_assert(u->mixer_path); + pa_assert(u->mixer_handle); - if ((err = snd_mixer_selem_get_capture_volume(u->mixer_elem, SND_MIXER_SCHN_MONO, &alsa_vol)) < 0) - goto fail; + if (pa_alsa_path_get_volume(u->mixer_path, u->mixer_handle, &s->channel_map, &r) < 0) + return; - pa_cvolume_set(&r, s->sample_spec.channels, from_alsa_volume(u, alsa_vol)); - } - } + /* Shift down by the base volume, so that 0dB becomes maximum volume */ + pa_sw_cvolume_multiply_scalar(&r, &r, s->base_volume); pa_log_debug("Read hardware volume: %s", pa_cvolume_snprint(t, sizeof(t), &r)); - if (!pa_cvolume_equal(&u->hardware_volume, &r)) { + if (pa_cvolume_equal(&u->hardware_volume, &r)) + return; - s->virtual_volume = u->hardware_volume = r; + s->virtual_volume = u->hardware_volume = r; - if (u->hw_dB_supported) { - pa_cvolume reset; + if (u->mixer_path->has_dB) { + pa_cvolume reset; - /* Hmm, so the hardware volume changed, let's reset our software volume */ - pa_cvolume_reset(&reset, s->sample_spec.channels); - pa_source_set_soft_volume(s, &reset); - } + /* Hmm, so the hardware volume changed, let's reset our software volume */ + pa_cvolume_reset(&reset, s->sample_spec.channels); + pa_source_set_soft_volume(s, &reset); } - - return; - -fail: - pa_log_error("Unable to read volume: %s", snd_strerror(err)); } static void source_set_volume_cb(pa_source *s) { struct userdata *u = s->userdata; - int err; - unsigned i; pa_cvolume r; + char t[PA_CVOLUME_SNPRINT_MAX]; pa_assert(u); - pa_assert(u->mixer_elem); - - if (u->mixer_seperate_channels) { - - r.channels = s->sample_spec.channels; - - for (i = 0; i < s->sample_spec.channels; i++) { - long alsa_vol; - pa_volume_t vol; - - vol = s->virtual_volume.values[i]; - - if (u->hw_dB_supported) { - - alsa_vol = (long) (pa_sw_volume_to_dB(vol) * 100); - alsa_vol += u->hw_dB_max; - alsa_vol = PA_CLAMP_UNLIKELY(alsa_vol, u->hw_dB_min, u->hw_dB_max); - - if ((err = snd_mixer_selem_set_capture_dB(u->mixer_elem, u->mixer_map[i], alsa_vol, 1)) < 0) - goto fail; - - if ((err = snd_mixer_selem_get_capture_dB(u->mixer_elem, u->mixer_map[i], &alsa_vol)) < 0) - goto fail; - -#ifdef HAVE_VALGRIND_MEMCHECK_H - VALGRIND_MAKE_MEM_DEFINED(&alsa_vol, sizeof(alsa_vol)); -#endif - - r.values[i] = pa_sw_volume_from_dB((double) (alsa_vol - u->hw_dB_max) / 100.0); - - } else { - alsa_vol = to_alsa_volume(u, vol); - - if ((err = snd_mixer_selem_set_capture_volume(u->mixer_elem, u->mixer_map[i], alsa_vol)) < 0) - goto fail; - - if ((err = snd_mixer_selem_get_capture_volume(u->mixer_elem, u->mixer_map[i], &alsa_vol)) < 0) - goto fail; - - r.values[i] = from_alsa_volume(u, alsa_vol); - } - } - - } else { - pa_volume_t vol; - long alsa_vol; - - vol = pa_cvolume_max(&s->virtual_volume); - - if (u->hw_dB_supported) { - alsa_vol = (long) (pa_sw_volume_to_dB(vol) * 100); - alsa_vol += u->hw_dB_max; - alsa_vol = PA_CLAMP_UNLIKELY(alsa_vol, u->hw_dB_min, u->hw_dB_max); - - if ((err = snd_mixer_selem_set_capture_dB_all(u->mixer_elem, alsa_vol, 1)) < 0) - goto fail; - - if ((err = snd_mixer_selem_get_capture_dB(u->mixer_elem, SND_MIXER_SCHN_MONO, &alsa_vol)) < 0) - goto fail; - -#ifdef HAVE_VALGRIND_MEMCHECK_H - VALGRIND_MAKE_MEM_DEFINED(&alsa_vol, sizeof(alsa_vol)); -#endif - - pa_cvolume_set(&r, s->sample_spec.channels, pa_sw_volume_from_dB((double) (alsa_vol - u->hw_dB_max) / 100.0)); - - } else { - alsa_vol = to_alsa_volume(u, vol); + pa_assert(u->mixer_path); + pa_assert(u->mixer_handle); - if ((err = snd_mixer_selem_set_capture_volume_all(u->mixer_elem, alsa_vol)) < 0) - goto fail; + /* Shift up by the base volume */ + pa_sw_cvolume_divide_scalar(&r, &s->virtual_volume, s->base_volume); - if ((err = snd_mixer_selem_get_capture_volume(u->mixer_elem, SND_MIXER_SCHN_MONO, &alsa_vol)) < 0) - goto fail; + if (pa_alsa_path_set_volume(u->mixer_path, u->mixer_handle, &s->channel_map, &r) < 0) + return; - pa_cvolume_set(&r, s->sample_spec.channels, from_alsa_volume(u, alsa_vol)); - } - } + /* Shift down by the base volume, so that 0dB becomes maximum volume */ + pa_sw_cvolume_multiply_scalar(&r, &r, s->base_volume); u->hardware_volume = r; - if (u->hw_dB_supported) { - char t[PA_CVOLUME_SNPRINT_MAX]; + if (u->mixer_path->has_dB) { /* Match exactly what the user requested by software */ - pa_sw_cvolume_divide(&s->soft_volume, &s->virtual_volume, &u->hardware_volume); pa_log_debug("Requested volume: %s", pa_cvolume_snprint(t, sizeof(t), &s->virtual_volume)); pa_log_debug("Got hardware volume: %s", pa_cvolume_snprint(t, sizeof(t), &u->hardware_volume)); pa_log_debug("Calculated software volume: %s", pa_cvolume_snprint(t, sizeof(t), &s->soft_volume)); - } else + } else { + pa_log_debug("Wrote hardware volume: %s", pa_cvolume_snprint(t, sizeof(t), &r)); /* We can't match exactly what the user requested, hence let's * at least tell the user about it */ s->virtual_volume = r; - - return; - -fail: - pa_log_error("Unable to set volume: %s", snd_strerror(err)); + } } static void source_get_mute_cb(pa_source *s) { struct userdata *u = s->userdata; - int err, sw; + pa_bool_t b; pa_assert(u); - pa_assert(u->mixer_elem); + pa_assert(u->mixer_path); + pa_assert(u->mixer_handle); - if ((err = snd_mixer_selem_get_capture_switch(u->mixer_elem, 0, &sw)) < 0) { - pa_log_error("Unable to get switch: %s", snd_strerror(err)); + if (pa_alsa_path_get_mute(u->mixer_path, u->mixer_handle, &b) < 0) return; - } - s->muted = !sw; + s->muted = b; } static void source_set_mute_cb(pa_source *s) { struct userdata *u = s->userdata; - int err; pa_assert(u); - pa_assert(u->mixer_elem); + pa_assert(u->mixer_path); + pa_assert(u->mixer_handle); - if ((err = snd_mixer_selem_set_capture_switch_all(u->mixer_elem, !s->muted)) < 0) { - pa_log_error("Unable to set switch: %s", snd_strerror(err)); - return; + pa_alsa_path_set_mute(u->mixer_path, u->mixer_handle, s->muted); +} + +static int source_set_port_cb(pa_source *s, pa_device_port *p) { + struct userdata *u = s->userdata; + pa_alsa_port_data *data; + + pa_assert(u); + pa_assert(p); + pa_assert(u->mixer_handle); + + data = PA_DEVICE_PORT_DATA(p); + + pa_assert_se(u->mixer_path = data->path); + pa_alsa_path_select(u->mixer_path, u->mixer_handle); + + if (u->mixer_path->has_volume && u->mixer_path->has_dB) { + s->base_volume = pa_sw_volume_from_dB(-u->mixer_path->max_dB); + s->n_volume_steps = PA_VOLUME_NORM+1; + + if (u->mixer_path->max_dB > 0.0) + pa_log_info("Fixing base volume to %0.2f dB", pa_sw_volume_to_dB(s->base_volume)); + else + pa_log_info("No particular base volume set, fixing to 0 dB"); + } else { + s->base_volume = PA_VOLUME_NORM; + s->n_volume_steps = u->mixer_path->max_volume - u->mixer_path->min_volume + 1; } + + if (data->setting) + pa_alsa_setting_select(data->setting, u->mixer_handle); + + if (s->set_mute) + s->set_mute(s); + if (s->set_volume) + s->set_volume(s); + + return 0; } static void source_update_requested_latency_cb(pa_source *s) { @@ -1141,7 +1095,6 @@ static void thread_func(void *userdata) { pa_make_realtime(u->core->realtime_priority); pa_thread_mq_install(&u->thread_mq); - pa_rtpoll_install(u->rtpoll); for (;;) { int ret; @@ -1178,7 +1131,7 @@ static void thread_func(void *userdata) { /* Convert from the sound card time domain to the * system time domain */ - cusec = pa_smoother_translate(u->smoother, pa_rtclock_usec(), sleep_usec); + cusec = pa_smoother_translate(u->smoother, pa_rtclock_now(), sleep_usec); /* pa_log_debug("Waking up in %0.2fms (system clock).", (double) cusec / PA_USEC_PER_MSEC); */ @@ -1206,7 +1159,7 @@ static void thread_func(void *userdata) { pollfd = pa_rtpoll_item_get_pollfd(u->alsa_rtpoll_item, &n); if ((err = snd_pcm_poll_descriptors_revents(u->pcm_handle, pollfd, n, &revents)) < 0) { - pa_log("snd_pcm_poll_descriptors_revents() failed: %s", snd_strerror(err)); + pa_log("snd_pcm_poll_descriptors_revents() failed: %s", pa_alsa_strerror(err)); goto fail; } @@ -1232,7 +1185,7 @@ finish: pa_log_debug("Thread shutting down"); } -static void set_source_name(pa_source_new_data *data, pa_modargs *ma, const char *device_id, const char *device_name) { +static void set_source_name(pa_source_new_data *data, pa_modargs *ma, const char *device_id, const char *device_name, pa_alsa_mapping *mapping) { const char *n; char *t; @@ -1253,82 +1206,136 @@ static void set_source_name(pa_source_new_data *data, pa_modargs *ma, const char data->namereg_fail = FALSE; } - t = pa_sprintf_malloc("alsa_input.%s", n); + if (mapping) + t = pa_sprintf_malloc("alsa_input.%s.%s", n, mapping->name); + else + t = pa_sprintf_malloc("alsa_input.%s", n); + pa_source_new_data_set_name(data, t); pa_xfree(t); } +static void find_mixer(struct userdata *u, pa_alsa_mapping *mapping, const char *element, pa_bool_t ignore_dB) { + + if (!mapping && !element) + return; + + if (!(u->mixer_handle = pa_alsa_open_mixer_for_pcm(u->pcm_handle, &u->control_device))) { + pa_log_info("Failed to find a working mixer device."); + return; + } + + if (element) { + + if (!(u->mixer_path = pa_alsa_path_synthesize(element, PA_ALSA_DIRECTION_INPUT))) + goto fail; + + if (pa_alsa_path_probe(u->mixer_path, u->mixer_handle, ignore_dB) < 0) + goto fail; + + pa_log_debug("Probed mixer path %s:", u->mixer_path->name); + pa_alsa_path_dump(u->mixer_path); + } else { + + if (!(u->mixer_path_set = pa_alsa_path_set_new(mapping, PA_ALSA_DIRECTION_INPUT))) + goto fail; + + pa_alsa_path_set_probe(u->mixer_path_set, u->mixer_handle, ignore_dB); + + pa_log_debug("Probed mixer paths:"); + pa_alsa_path_set_dump(u->mixer_path_set); + } + + return; + +fail: + + if (u->mixer_path_set) { + pa_alsa_path_set_free(u->mixer_path_set); + u->mixer_path_set = NULL; + } else if (u->mixer_path) { + pa_alsa_path_free(u->mixer_path); + u->mixer_path = NULL; + } + + if (u->mixer_handle) { + snd_mixer_close(u->mixer_handle); + u->mixer_handle = NULL; + } +} + static int setup_mixer(struct userdata *u, pa_bool_t ignore_dB) { pa_assert(u); if (!u->mixer_handle) return 0; - pa_assert(u->mixer_elem); + if (u->source->active_port) { + pa_alsa_port_data *data; - if (snd_mixer_selem_has_capture_volume(u->mixer_elem)) { - pa_bool_t suitable = FALSE; + /* We have a list of supported paths, so let's activate the + * one that has been chosen as active */ - if (snd_mixer_selem_get_capture_volume_range(u->mixer_elem, &u->hw_volume_min, &u->hw_volume_max) < 0) - pa_log_info("Failed to get volume range. Falling back to software volume control."); - else if (u->hw_volume_min >= u->hw_volume_max) - pa_log_warn("Your kernel driver is broken: it reports a volume range from %li to %li which makes no sense.", u->hw_volume_min, u->hw_volume_max); - else { - pa_log_info("Volume ranges from %li to %li.", u->hw_volume_min, u->hw_volume_max); - suitable = TRUE; - } + data = PA_DEVICE_PORT_DATA(u->source->active_port); + u->mixer_path = data->path; - if (suitable) { - if (ignore_dB || snd_mixer_selem_get_capture_dB_range(u->mixer_elem, &u->hw_dB_min, &u->hw_dB_max) < 0) - pa_log_info("Mixer doesn't support dB information or data is ignored."); - else { -#ifdef HAVE_VALGRIND_MEMCHECK_H - VALGRIND_MAKE_MEM_DEFINED(&u->hw_dB_min, sizeof(u->hw_dB_min)); - VALGRIND_MAKE_MEM_DEFINED(&u->hw_dB_max, sizeof(u->hw_dB_max)); -#endif + pa_alsa_path_select(data->path, u->mixer_handle); - if (u->hw_dB_min >= u->hw_dB_max) - pa_log_warn("Your kernel driver is broken: it reports a volume range from %0.2f dB to %0.2f dB which makes no sense.", (double) u->hw_dB_min/100.0, (double) u->hw_dB_max/100.0); - else { - pa_log_info("Volume ranges from %0.2f dB to %0.2f dB.", (double) u->hw_dB_min/100.0, (double) u->hw_dB_max/100.0); - u->hw_dB_supported = TRUE; - - if (u->hw_dB_max > 0) { - u->source->base_volume = pa_sw_volume_from_dB(- (double) u->hw_dB_max/100.0); - pa_log_info("Fixing base volume to %0.2f dB", pa_sw_volume_to_dB(u->source->base_volume)); - } else - pa_log_info("No particular base volume set, fixing to 0 dB"); - } - } + if (data->setting) + pa_alsa_setting_select(data->setting, u->mixer_handle); - if (!u->hw_dB_supported && - u->hw_volume_max - u->hw_volume_min < 3) { + } else { - pa_log_info("Device has less than 4 volume levels. Falling back to software volume control."); - suitable = FALSE; - } - } + if (!u->mixer_path && u->mixer_path_set) + u->mixer_path = u->mixer_path_set->paths; - if (suitable) { - u->mixer_seperate_channels = pa_alsa_calc_mixer_map(u->mixer_elem, &u->source->channel_map, u->mixer_map, FALSE) >= 0; + if (u->mixer_path) { + /* Hmm, we have only a single path, then let's activate it */ - u->source->get_volume = source_get_volume_cb; - u->source->set_volume = source_set_volume_cb; - u->source->flags |= PA_SOURCE_HW_VOLUME_CTRL | (u->hw_dB_supported ? PA_SOURCE_DECIBEL_VOLUME : 0); - pa_log_info("Using hardware volume control. Hardware dB scale %s.", u->hw_dB_supported ? "supported" : "not supported"); + pa_alsa_path_select(u->mixer_path, u->mixer_handle); - if (!u->hw_dB_supported) - u->source->n_volume_steps = u->hw_volume_max - u->hw_volume_min + 1; + if (u->mixer_path->settings) + pa_alsa_setting_select(u->mixer_path->settings, u->mixer_handle); } else - pa_log_info("Using software volume control."); + return 0; + } + + if (!u->mixer_path->has_volume) + pa_log_info("Driver does not support hardware volume control, falling back to software volume control."); + else { + + if (u->mixer_path->has_dB) { + pa_log_info("Hardware volume ranges from %0.2f dB to %0.2f dB.", u->mixer_path->min_dB, u->mixer_path->max_dB); + + u->source->base_volume = pa_sw_volume_from_dB(-u->mixer_path->max_dB); + u->source->n_volume_steps = PA_VOLUME_NORM+1; + + if (u->mixer_path->max_dB > 0.0) + pa_log_info("Fixing base volume to %0.2f dB", pa_sw_volume_to_dB(u->source->base_volume)); + else + pa_log_info("No particular base volume set, fixing to 0 dB"); + + } else { + pa_log_info("Hardware volume ranges from %li to %li.", u->mixer_path->min_volume, u->mixer_path->max_volume); + u->source->base_volume = PA_VOLUME_NORM; + u->source->n_volume_steps = u->mixer_path->max_volume - u->mixer_path->min_volume + 1; + } + + u->source->get_volume = source_get_volume_cb; + u->source->set_volume = source_set_volume_cb; + + u->source->flags |= PA_SOURCE_HW_VOLUME_CTRL | (u->mixer_path->has_dB ? PA_SOURCE_DECIBEL_VOLUME : 0); + pa_log_info("Using hardware volume control. Hardware dB scale %s.", u->mixer_path->has_dB ? "supported" : "not supported"); } - if (snd_mixer_selem_has_capture_switch(u->mixer_elem)) { + if (!u->mixer_path->has_mute) { + pa_log_info("Driver does not support hardware mute control, falling back to software mute control."); + } else { u->source->get_mute = source_get_mute_cb; u->source->set_mute = source_set_mute_cb; u->source->flags |= PA_SOURCE_HW_MUTE_CTRL; - } else - pa_log_info("Using software mute control."); + pa_log_info("Using hardware mute control."); + } u->mixer_fdl = pa_alsa_fdlist_new(); @@ -1337,13 +1344,15 @@ static int setup_mixer(struct userdata *u, pa_bool_t ignore_dB) { return -1; } - snd_mixer_elem_set_callback(u->mixer_elem, mixer_callback); - snd_mixer_elem_set_callback_private(u->mixer_elem, u); + if (u->mixer_path_set) + pa_alsa_path_set_set_callback(u->mixer_path_set, u->mixer_handle, mixer_callback, u); + else + pa_alsa_path_set_callback(u->mixer_path, u->mixer_handle, mixer_callback, u); return 0; } -pa_source *pa_alsa_source_new(pa_module *m, pa_modargs *ma, const char*driver, pa_card *card, const pa_alsa_profile_info *profile) { +pa_source *pa_alsa_source_new(pa_module *m, pa_modargs *ma, const char*driver, pa_card *card, pa_alsa_mapping *mapping) { struct userdata *u = NULL; const char *dev_id = NULL; @@ -1354,6 +1363,7 @@ pa_source *pa_alsa_source_new(pa_module *m, pa_modargs *ma, const char*driver, p size_t frame_size; pa_bool_t use_mmap = TRUE, b, use_tsched = TRUE, d, ignore_dB = FALSE; pa_source_new_data data; + pa_alsa_profile_set *profile_set = NULL; pa_assert(m); pa_assert(ma); @@ -1414,44 +1424,57 @@ pa_source *pa_alsa_source_new(pa_module *m, pa_modargs *ma, const char*driver, p u->use_tsched = use_tsched; u->rtpoll = pa_rtpoll_new(); pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll); - u->alsa_rtpoll_item = NULL; - u->smoother = pa_smoother_new(DEFAULT_TSCHED_WATERMARK_USEC*2, DEFAULT_TSCHED_WATERMARK_USEC*2, TRUE, 5); - pa_smoother_set_time_offset(u->smoother, pa_rtclock_usec()); + u->smoother = pa_smoother_new( + DEFAULT_TSCHED_WATERMARK_USEC*2, + DEFAULT_TSCHED_WATERMARK_USEC*2, + TRUE, + TRUE, + 5, + pa_rtclock_now(), + FALSE); - if (reserve_init(u, pa_modargs_get_value( - ma, "device_id", - pa_modargs_get_value(ma, "device", DEFAULT_DEVICE))) < 0) + dev_id = pa_modargs_get_value( + ma, "device_id", + pa_modargs_get_value(ma, "device", DEFAULT_DEVICE)); + + if (reserve_init(u, dev_id) < 0) + goto fail; + + if (reserve_monitor_init(u, dev_id) < 0) goto fail; b = use_mmap; d = use_tsched; - if (profile) { + if (mapping) { if (!(dev_id = pa_modargs_get_value(ma, "device_id", NULL))) { pa_log("device_id= not set"); goto fail; } - if (!(u->pcm_handle = pa_alsa_open_by_device_id_profile( + if (!(u->pcm_handle = pa_alsa_open_by_device_id_mapping( dev_id, &u->device_name, &ss, &map, SND_PCM_STREAM_CAPTURE, &nfrags, &period_frames, tsched_frames, - &b, &d, profile))) + &b, &d, mapping))) goto fail; } else if ((dev_id = pa_modargs_get_value(ma, "device_id", NULL))) { + if (!(profile_set = pa_alsa_profile_set_new(NULL, &map))) + goto fail; + if (!(u->pcm_handle = pa_alsa_open_by_device_id_auto( dev_id, &u->device_name, &ss, &map, SND_PCM_STREAM_CAPTURE, &nfrags, &period_frames, tsched_frames, - &b, &d, &profile))) + &b, &d, profile_set, &mapping))) goto fail; } else { @@ -1469,8 +1492,13 @@ pa_source *pa_alsa_source_new(pa_module *m, pa_modargs *ma, const char*driver, p pa_assert(u->device_name); pa_log_info("Successfully opened device %s.", u->device_name); - if (profile) - pa_log_info("Selected configuration '%s' (%s).", profile->description, profile->name); + if (pa_alsa_pcm_is_modem(u->pcm_handle)) { + pa_log_notice("Device %s is modem, refusing further initialization.", u->device_name); + goto fail; + } + + if (mapping) + pa_log_info("Selected mapping '%s' (%s).", mapping->description, mapping->name); if (use_mmap && !b) { pa_log_info("Device doesn't support mmap(), falling back to UNIX read/write mode."); @@ -1482,6 +1510,11 @@ pa_source *pa_alsa_source_new(pa_module *m, pa_modargs *ma, const char*driver, p u->use_tsched = use_tsched = FALSE; } + if (use_tsched && !pa_alsa_pcm_is_hw(u->pcm_handle)) { + pa_log_info("Device is not a hardware device, disabling timer-based scheduling."); + u->use_tsched = use_tsched = FALSE; + } + if (u->use_mmap) pa_log_info("Successfully enabled mmap() mode."); @@ -1491,13 +1524,13 @@ pa_source *pa_alsa_source_new(pa_module *m, pa_modargs *ma, const char*driver, p /* ALSA might tweak the sample spec, so recalculate the frame size */ frame_size = pa_frame_size(&ss); - pa_alsa_find_mixer_and_elem(u->pcm_handle, &u->mixer_handle, &u->mixer_elem); + find_mixer(u, mapping, pa_modargs_get_value(ma, "control", NULL), ignore_dB); pa_source_new_data_init(&data); data.driver = driver; data.module = m; data.card = card; - set_source_name(&data, ma, dev_id, u->device_name); + set_source_name(&data, ma, dev_id, u->device_name, mapping); pa_source_new_data_set_sample_spec(&data, &ss); pa_source_new_data_set_channel_map(&data, &map); @@ -1507,14 +1540,26 @@ pa_source *pa_alsa_source_new(pa_module *m, pa_modargs *ma, const char*driver, p pa_proplist_setf(data.proplist, PA_PROP_DEVICE_BUFFERING_FRAGMENT_SIZE, "%lu", (unsigned long) (period_frames * frame_size)); pa_proplist_sets(data.proplist, PA_PROP_DEVICE_ACCESS_MODE, u->use_tsched ? "mmap+timer" : (u->use_mmap ? "mmap" : "serial")); - if (profile) { - pa_proplist_sets(data.proplist, PA_PROP_DEVICE_PROFILE_NAME, profile->name); - pa_proplist_sets(data.proplist, PA_PROP_DEVICE_PROFILE_DESCRIPTION, profile->description); + if (mapping) { + pa_proplist_sets(data.proplist, PA_PROP_DEVICE_PROFILE_NAME, mapping->name); + pa_proplist_sets(data.proplist, PA_PROP_DEVICE_PROFILE_DESCRIPTION, mapping->description); } pa_alsa_init_description(data.proplist); - u->source = pa_source_new(m->core, &data, PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY); + if (u->control_device) + pa_alsa_init_proplist_ctl(data.proplist, u->control_device); + + if (pa_modargs_get_proplist(ma, "source_properties", data.proplist, PA_UPDATE_REPLACE) < 0) { + pa_log("Invalid properties"); + pa_source_new_data_done(&data); + goto fail; + } + + if (u->mixer_path_set) + pa_alsa_add_ports(&data.ports, u->mixer_path_set); + + u->source = pa_source_new(m->core, &data, PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY|(u->use_tsched ? PA_SOURCE_DYNAMIC_LATENCY : 0)); pa_source_new_data_done(&data); if (!u->source) { @@ -1525,6 +1570,7 @@ pa_source *pa_alsa_source_new(pa_module *m, pa_modargs *ma, const char*driver, p u->source->parent.process_msg = source_process_msg; u->source->update_requested_latency = source_update_requested_latency_cb; u->source->set_state = source_set_state_cb; + u->source->set_port = source_set_port_cb; u->source->userdata = u; pa_source_set_asyncmsgq(u->source, u->thread_mq.inq); @@ -1537,24 +1583,24 @@ pa_source *pa_alsa_source_new(pa_module *m, pa_modargs *ma, const char*driver, p u->tsched_watermark = pa_usec_to_bytes_round_up(pa_bytes_to_usec_round_up(tsched_watermark, &requested_ss), &u->source->sample_spec); pa_cvolume_mute(&u->hardware_volume, u->source->sample_spec.channels); - if (use_tsched) { - fix_min_sleep_wakeup(u); - fix_tsched_watermark(u); + pa_log_info("Using %u fragments of size %lu bytes, buffer time is %0.2fms", + nfrags, (long unsigned) u->fragment_size, + (double) pa_bytes_to_usec(u->hwbuf_size, &ss) / PA_USEC_PER_MSEC); + if (u->use_tsched) { u->watermark_step = pa_usec_to_bytes(TSCHED_WATERMARK_STEP_USEC, &u->source->sample_spec); - } - pa_source_set_latency_range(u->source, - use_tsched ? (pa_usec_t) -1 : pa_bytes_to_usec(u->hwbuf_size, &ss), - pa_bytes_to_usec(u->hwbuf_size, &ss)); + fix_min_sleep_wakeup(u); + fix_tsched_watermark(u); - pa_log_info("Using %u fragments of size %lu bytes, buffer time is %0.2fms", - nfrags, (long unsigned) u->fragment_size, - (double) pa_bytes_to_usec(u->hwbuf_size, &ss) / PA_USEC_PER_MSEC); + pa_source_set_latency_range(u->source, + 0, + pa_bytes_to_usec(u->hwbuf_size, &ss)); - if (use_tsched) pa_log_info("Time scheduling watermark is %0.2fms", (double) pa_bytes_to_usec(u->tsched_watermark, &ss) / PA_USEC_PER_MSEC); + } else + pa_source_set_fixed_latency(u->source, pa_bytes_to_usec(u->hwbuf_size, &ss)); reserve_update(u); @@ -1564,7 +1610,7 @@ pa_source *pa_alsa_source_new(pa_module *m, pa_modargs *ma, const char*driver, p if (setup_mixer(u, ignore_dB) < 0) goto fail; - pa_alsa_dump(u->pcm_handle); + pa_alsa_dump(PA_LOG_DEBUG, u->pcm_handle); if (!(u->thread = pa_thread_new(thread_func, u))) { pa_log("Failed to create thread."); @@ -1589,11 +1635,18 @@ pa_source *pa_alsa_source_new(pa_module *m, pa_modargs *ma, const char*driver, p pa_source_put(u->source); + if (profile_set) + pa_alsa_profile_set_free(profile_set); + return u->source; fail: - userdata_free(u); + if (u) + userdata_free(u); + + if (profile_set) + pa_alsa_profile_set_free(profile_set); return NULL; } @@ -1620,23 +1673,30 @@ static void userdata_free(struct userdata *u) { if (u->rtpoll) pa_rtpoll_free(u->rtpoll); + if (u->pcm_handle) { + snd_pcm_drop(u->pcm_handle); + snd_pcm_close(u->pcm_handle); + } + if (u->mixer_fdl) pa_alsa_fdlist_free(u->mixer_fdl); + if (u->mixer_path_set) + pa_alsa_path_set_free(u->mixer_path_set); + else if (u->mixer_path) + pa_alsa_path_free(u->mixer_path); + if (u->mixer_handle) snd_mixer_close(u->mixer_handle); - if (u->pcm_handle) { - snd_pcm_drop(u->pcm_handle); - snd_pcm_close(u->pcm_handle); - } - if (u->smoother) pa_smoother_free(u->smoother); reserve_done(u); + monitor_done(u); pa_xfree(u->device_name); + pa_xfree(u->control_device); pa_xfree(u); } diff --git a/src/modules/alsa/alsa-source.h b/src/modules/alsa/alsa-source.h index 9cbb0e17..5d9409e2 100644 --- a/src/modules/alsa/alsa-source.h +++ b/src/modules/alsa/alsa-source.h @@ -29,7 +29,7 @@ #include "alsa-util.h" -pa_source* pa_alsa_source_new(pa_module *m, pa_modargs *ma, const char*driver, pa_card *card, const pa_alsa_profile_info *profile); +pa_source* pa_alsa_source_new(pa_module *m, pa_modargs *ma, const char*driver, pa_card *card, pa_alsa_mapping *mapping); void pa_alsa_source_free(pa_source *s); diff --git a/src/modules/alsa/alsa-util.c b/src/modules/alsa/alsa-util.c index 454cfd4e..1f3e5dcd 100644 --- a/src/modules/alsa/alsa-util.c +++ b/src/modules/alsa/alsa-util.c @@ -33,6 +33,7 @@ #include <pulse/timeval.h> #include <pulse/util.h> #include <pulse/i18n.h> +#include <pulse/utf8.h> #include <pulsecore/log.h> #include <pulsecore/macro.h> @@ -40,8 +41,11 @@ #include <pulsecore/atomic.h> #include <pulsecore/core-error.h> #include <pulsecore/once.h> +#include <pulsecore/thread.h> +#include <pulsecore/conf-parser.h> #include "alsa-util.h" +#include "alsa-mixer.h" #ifdef HAVE_HAL #include "hal-util.h" @@ -51,191 +55,6 @@ #include "udev-util.h" #endif -struct pa_alsa_fdlist { - unsigned num_fds; - struct pollfd *fds; - /* This is a temporary buffer used to avoid lots of mallocs */ - struct pollfd *work_fds; - - snd_mixer_t *mixer; - - pa_mainloop_api *m; - pa_defer_event *defer; - pa_io_event **ios; - - pa_bool_t polled; - - void (*cb)(void *userdata); - void *userdata; -}; - -static void io_cb(pa_mainloop_api*a, pa_io_event* e, int fd, pa_io_event_flags_t events, void *userdata) { - - struct pa_alsa_fdlist *fdl = userdata; - int err; - unsigned i; - unsigned short revents; - - pa_assert(a); - pa_assert(fdl); - pa_assert(fdl->mixer); - pa_assert(fdl->fds); - pa_assert(fdl->work_fds); - - if (fdl->polled) - return; - - fdl->polled = TRUE; - - memcpy(fdl->work_fds, fdl->fds, sizeof(struct pollfd) * fdl->num_fds); - - for (i = 0; i < fdl->num_fds; i++) { - if (e == fdl->ios[i]) { - if (events & PA_IO_EVENT_INPUT) - fdl->work_fds[i].revents |= POLLIN; - if (events & PA_IO_EVENT_OUTPUT) - fdl->work_fds[i].revents |= POLLOUT; - if (events & PA_IO_EVENT_ERROR) - fdl->work_fds[i].revents |= POLLERR; - if (events & PA_IO_EVENT_HANGUP) - fdl->work_fds[i].revents |= POLLHUP; - break; - } - } - - pa_assert(i != fdl->num_fds); - - if ((err = snd_mixer_poll_descriptors_revents(fdl->mixer, fdl->work_fds, fdl->num_fds, &revents)) < 0) { - pa_log_error("Unable to get poll revent: %s", snd_strerror(err)); - return; - } - - a->defer_enable(fdl->defer, 1); - - if (revents) - snd_mixer_handle_events(fdl->mixer); -} - -static void defer_cb(pa_mainloop_api*a, pa_defer_event* e, void *userdata) { - struct pa_alsa_fdlist *fdl = userdata; - unsigned num_fds, i; - int err, n; - struct pollfd *temp; - - pa_assert(a); - pa_assert(fdl); - pa_assert(fdl->mixer); - - a->defer_enable(fdl->defer, 0); - - if ((n = snd_mixer_poll_descriptors_count(fdl->mixer)) < 0) { - pa_log("snd_mixer_poll_descriptors_count() failed: %s", snd_strerror(n)); - return; - } - num_fds = (unsigned) n; - - if (num_fds != fdl->num_fds) { - if (fdl->fds) - pa_xfree(fdl->fds); - if (fdl->work_fds) - pa_xfree(fdl->work_fds); - fdl->fds = pa_xnew0(struct pollfd, num_fds); - fdl->work_fds = pa_xnew(struct pollfd, num_fds); - } - - memset(fdl->work_fds, 0, sizeof(struct pollfd) * num_fds); - - if ((err = snd_mixer_poll_descriptors(fdl->mixer, fdl->work_fds, num_fds)) < 0) { - pa_log_error("Unable to get poll descriptors: %s", snd_strerror(err)); - return; - } - - fdl->polled = FALSE; - - if (memcmp(fdl->fds, fdl->work_fds, sizeof(struct pollfd) * num_fds) == 0) - return; - - if (fdl->ios) { - for (i = 0; i < fdl->num_fds; i++) - a->io_free(fdl->ios[i]); - - if (num_fds != fdl->num_fds) { - pa_xfree(fdl->ios); - fdl->ios = NULL; - } - } - - if (!fdl->ios) - fdl->ios = pa_xnew(pa_io_event*, num_fds); - - /* Swap pointers */ - temp = fdl->work_fds; - fdl->work_fds = fdl->fds; - fdl->fds = temp; - - fdl->num_fds = num_fds; - - for (i = 0;i < num_fds;i++) - fdl->ios[i] = a->io_new(a, fdl->fds[i].fd, - ((fdl->fds[i].events & POLLIN) ? PA_IO_EVENT_INPUT : 0) | - ((fdl->fds[i].events & POLLOUT) ? PA_IO_EVENT_OUTPUT : 0), - io_cb, fdl); -} - -struct pa_alsa_fdlist *pa_alsa_fdlist_new(void) { - struct pa_alsa_fdlist *fdl; - - fdl = pa_xnew0(struct pa_alsa_fdlist, 1); - - fdl->num_fds = 0; - fdl->fds = NULL; - fdl->work_fds = NULL; - fdl->mixer = NULL; - fdl->m = NULL; - fdl->defer = NULL; - fdl->ios = NULL; - fdl->polled = FALSE; - - return fdl; -} - -void pa_alsa_fdlist_free(struct pa_alsa_fdlist *fdl) { - pa_assert(fdl); - - if (fdl->defer) { - pa_assert(fdl->m); - fdl->m->defer_free(fdl->defer); - } - - if (fdl->ios) { - unsigned i; - pa_assert(fdl->m); - for (i = 0; i < fdl->num_fds; i++) - fdl->m->io_free(fdl->ios[i]); - pa_xfree(fdl->ios); - } - - if (fdl->fds) - pa_xfree(fdl->fds); - if (fdl->work_fds) - pa_xfree(fdl->work_fds); - - pa_xfree(fdl); -} - -int pa_alsa_fdlist_set_mixer(struct pa_alsa_fdlist *fdl, snd_mixer_t *mixer_handle, pa_mainloop_api* m) { - pa_assert(fdl); - pa_assert(mixer_handle); - pa_assert(m); - pa_assert(!fdl->m); - - fdl->mixer = mixer_handle; - fdl->m = m; - fdl->defer = m->defer_new(m, defer_cb, fdl); - - return 0; -} - static int set_format(snd_pcm_t *pcm_handle, snd_pcm_hw_params_t *hwparams, pa_sample_format_t *f) { static const snd_pcm_format_t format_trans[] = { @@ -267,11 +86,11 @@ static int set_format(snd_pcm_t *pcm_handle, snd_pcm_hw_params_t *hwparams, pa_s PA_SAMPLE_S16RE, PA_SAMPLE_ALAW, PA_SAMPLE_ULAW, - PA_SAMPLE_U8, - PA_SAMPLE_INVALID + PA_SAMPLE_U8 }; - int i, ret; + unsigned i; + int ret; pa_assert(pcm_handle); pa_assert(f); @@ -279,6 +98,10 @@ static int set_format(snd_pcm_t *pcm_handle, snd_pcm_hw_params_t *hwparams, pa_s if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0) return ret; + pa_log_debug("snd_pcm_hw_params_set_format(%s) failed: %s", + snd_pcm_format_description(format_trans[*f]), + pa_alsa_strerror(ret)); + if (*f == PA_SAMPLE_FLOAT32BE) *f = PA_SAMPLE_FLOAT32LE; else if (*f == PA_SAMPLE_FLOAT32LE) @@ -305,13 +128,21 @@ static int set_format(snd_pcm_t *pcm_handle, snd_pcm_hw_params_t *hwparams, pa_s if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0) return ret; + pa_log_debug("snd_pcm_hw_params_set_format(%s) failed: %s", + snd_pcm_format_description(format_trans[*f]), + pa_alsa_strerror(ret)); + try_auto: - for (i = 0; try_order[i] != PA_SAMPLE_INVALID; i++) { + for (i = 0; i < PA_ELEMENTSOF(try_order); i++) { *f = try_order[i]; if ((ret = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format_trans[*f])) >= 0) return ret; + + pa_log_debug("snd_pcm_hw_params_set_format(%s) failed: %s", + snd_pcm_format_description(format_trans[*f]), + pa_alsa_strerror(ret)); } return -1; @@ -332,7 +163,6 @@ int pa_alsa_set_hw_params( int ret = -1; snd_pcm_uframes_t _period_size = period_size ? *period_size : 0; unsigned int _periods = periods ? *periods : 0; - snd_pcm_uframes_t buffer_size; unsigned int r = ss->rate; unsigned int c = ss->channels; pa_sample_format_t f = ss->format; @@ -346,11 +176,15 @@ int pa_alsa_set_hw_params( snd_pcm_hw_params_alloca(&hwparams); - if ((ret = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0) + if ((ret = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0) { + pa_log_debug("snd_pcm_hw_params_any() failed: %s", pa_alsa_strerror(ret)); goto finish; + } - if ((ret = snd_pcm_hw_params_set_rate_resample(pcm_handle, hwparams, 0)) < 0) + if ((ret = snd_pcm_hw_params_set_rate_resample(pcm_handle, hwparams, 0)) < 0) { + pa_log_debug("snd_pcm_hw_params_set_rate_resample() failed: %s", pa_alsa_strerror(ret)); goto finish; + } if (_use_mmap) { @@ -358,14 +192,18 @@ int pa_alsa_set_hw_params( /* mmap() didn't work, fall back to interleaved */ - if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) + if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) { + pa_log_debug("snd_pcm_hw_params_set_access() failed: %s", pa_alsa_strerror(ret)); goto finish; + } _use_mmap = FALSE; } - } else if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) + } else if ((ret = snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) { + pa_log_debug("snd_pcm_hw_params_set_access() failed: %s", pa_alsa_strerror(ret)); goto finish; + } if (!_use_mmap) _use_tsched = FALSE; @@ -373,54 +211,72 @@ int pa_alsa_set_hw_params( if ((ret = set_format(pcm_handle, hwparams, &f)) < 0) goto finish; - if ((ret = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &r, NULL)) < 0) + if ((ret = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &r, NULL)) < 0) { + pa_log_debug("snd_pcm_hw_params_set_rate_near() failed: %s", pa_alsa_strerror(ret)); goto finish; + } if (require_exact_channel_number) { - if ((ret = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, c)) < 0) + if ((ret = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, c)) < 0) { + pa_log_debug("snd_pcm_hw_params_set_channels(%u) failed: %s", c, pa_alsa_strerror(ret)); goto finish; + } } else { - if ((ret = snd_pcm_hw_params_set_channels_near(pcm_handle, hwparams, &c)) < 0) + if ((ret = snd_pcm_hw_params_set_channels_near(pcm_handle, hwparams, &c)) < 0) { + pa_log_debug("snd_pcm_hw_params_set_channels_near(%u) failed: %s", c, pa_alsa_strerror(ret)); goto finish; + } } - if ((ret = snd_pcm_hw_params_set_periods_integer(pcm_handle, hwparams)) < 0) + if ((ret = snd_pcm_hw_params_set_periods_integer(pcm_handle, hwparams)) < 0) { + pa_log_debug("snd_pcm_hw_params_set_periods_integer() failed: %s", pa_alsa_strerror(ret)); goto finish; + } if (_period_size && tsched_size && _periods) { + /* Adjust the buffer sizes, if we didn't get the rate we were asking for */ _period_size = (snd_pcm_uframes_t) (((uint64_t) _period_size * r) / ss->rate); tsched_size = (snd_pcm_uframes_t) (((uint64_t) tsched_size * r) / ss->rate); if (_use_tsched) { + snd_pcm_uframes_t buffer_size = 0; + + if ((ret = snd_pcm_hw_params_get_buffer_size_max(hwparams, &buffer_size)) < 0) + pa_log_warn("snd_pcm_hw_params_get_buffer_size_max() failed: %s", pa_alsa_strerror(ret)); + else + pa_log_debug("Maximum hw buffer size is %u ms", (unsigned) buffer_size * 1000 / r); + _period_size = tsched_size; _periods = 1; - - pa_assert_se(snd_pcm_hw_params_get_buffer_size_max(hwparams, &buffer_size) == 0); - pa_log_debug("Maximum hw buffer size is %u ms", (unsigned) buffer_size * 1000 / r); } - buffer_size = _periods * _period_size; + if (_period_size > 0 && _periods > 0) { + snd_pcm_uframes_t buffer_size; + + buffer_size = _periods * _period_size; + + if ((ret = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &buffer_size)) < 0) + pa_log_info("snd_pcm_hw_params_set_buffer_size_near() failed: %s", pa_alsa_strerror(ret)); + } if (_periods > 0) { - /* First we pass 0 as direction to get exactly what we asked - * for. That this is necessary is presumably a bug in ALSA */ + /* First we pass 0 as direction to get exactly what we + * asked for. That this is necessary is presumably a bug + * in ALSA. All in all this is mostly a hint to ALSA, so + * we don't care if this fails. */ dir = 0; - if ((ret = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &_periods, &dir)) < 0) { + if (snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &_periods, &dir) < 0) { dir = 1; - if ((ret = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &_periods, &dir)) < 0) { + if (snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &_periods, &dir) < 0) { dir = -1; if ((ret = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &_periods, &dir)) < 0) - goto finish; + pa_log_info("snd_pcm_hw_params_set_periods_near() failed: %s", pa_alsa_strerror(ret)); } } } - - if (_period_size > 0) - if ((ret = snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hwparams, &buffer_size)) < 0) - goto finish; } if ((ret = snd_pcm_hw_params(pcm_handle, hwparams)) < 0) @@ -435,12 +291,16 @@ int pa_alsa_set_hw_params( if (ss->format != f) pa_log_info("Device %s doesn't support sample format %s, changed to %s.", snd_pcm_name(pcm_handle), pa_sample_format_to_string(ss->format), pa_sample_format_to_string(f)); - if ((ret = snd_pcm_prepare(pcm_handle)) < 0) + if ((ret = snd_pcm_prepare(pcm_handle)) < 0) { + pa_log_info("snd_pcm_prepare() failed: %s", pa_alsa_strerror(ret)); goto finish; + } if ((ret = snd_pcm_hw_params_get_period_size(hwparams, &_period_size, &dir)) < 0 || - (ret = snd_pcm_hw_params_get_periods(hwparams, &_periods, &dir)) < 0) + (ret = snd_pcm_hw_params_get_periods(hwparams, &_periods, &dir)) < 0) { + pa_log_info("snd_pcm_hw_params_get_period{s|_size}() failed: %s", pa_alsa_strerror(ret)); goto finish; + } /* If the sample rate deviates too much, we need to resample */ if (r < ss->rate*.95 || r > ss->rate*1.05) @@ -482,131 +342,48 @@ int pa_alsa_set_sw_params(snd_pcm_t *pcm, snd_pcm_uframes_t avail_min) { snd_pcm_sw_params_alloca(&swparams); if ((err = snd_pcm_sw_params_current(pcm, swparams) < 0)) { - pa_log_warn("Unable to determine current swparams: %s\n", snd_strerror(err)); + pa_log_warn("Unable to determine current swparams: %s\n", pa_alsa_strerror(err)); return err; } if ((err = snd_pcm_sw_params_set_period_event(pcm, swparams, 0)) < 0) { - pa_log_warn("Unable to disable period event: %s\n", snd_strerror(err)); + pa_log_warn("Unable to disable period event: %s\n", pa_alsa_strerror(err)); return err; } if ((err = snd_pcm_sw_params_set_tstamp_mode(pcm, swparams, SND_PCM_TSTAMP_ENABLE)) < 0) { - pa_log_warn("Unable to enable time stamping: %s\n", snd_strerror(err)); + pa_log_warn("Unable to enable time stamping: %s\n", pa_alsa_strerror(err)); return err; } if ((err = snd_pcm_sw_params_get_boundary(swparams, &boundary)) < 0) { - pa_log_warn("Unable to get boundary: %s\n", snd_strerror(err)); + pa_log_warn("Unable to get boundary: %s\n", pa_alsa_strerror(err)); return err; } if ((err = snd_pcm_sw_params_set_stop_threshold(pcm, swparams, boundary)) < 0) { - pa_log_warn("Unable to set stop threshold: %s\n", snd_strerror(err)); + pa_log_warn("Unable to set stop threshold: %s\n", pa_alsa_strerror(err)); return err; } if ((err = snd_pcm_sw_params_set_start_threshold(pcm, swparams, (snd_pcm_uframes_t) -1)) < 0) { - pa_log_warn("Unable to set start threshold: %s\n", snd_strerror(err)); + pa_log_warn("Unable to set start threshold: %s\n", pa_alsa_strerror(err)); return err; } if ((err = snd_pcm_sw_params_set_avail_min(pcm, swparams, avail_min)) < 0) { - pa_log_error("snd_pcm_sw_params_set_avail_min() failed: %s", snd_strerror(err)); + pa_log_error("snd_pcm_sw_params_set_avail_min() failed: %s", pa_alsa_strerror(err)); return err; } if ((err = snd_pcm_sw_params(pcm, swparams)) < 0) { - pa_log_warn("Unable to set sw params: %s\n", snd_strerror(err)); + pa_log_warn("Unable to set sw params: %s\n", pa_alsa_strerror(err)); return err; } return 0; } -static const struct pa_alsa_profile_info device_table[] = { - {{ 1, { PA_CHANNEL_POSITION_MONO }}, - "hw", - N_("Analog Mono"), - "analog-mono", - 1 }, - - {{ 2, { PA_CHANNEL_POSITION_LEFT, PA_CHANNEL_POSITION_RIGHT }}, - "front", - N_("Analog Stereo"), - "analog-stereo", - 10 }, - - {{ 2, { PA_CHANNEL_POSITION_LEFT, PA_CHANNEL_POSITION_RIGHT }}, - "iec958", - N_("Digital Stereo (IEC958)"), - "iec958-stereo", - 5 }, - - {{ 2, { PA_CHANNEL_POSITION_LEFT, PA_CHANNEL_POSITION_RIGHT }}, - "hdmi", - N_("Digital Stereo (HDMI)"), - "hdmi-stereo", - 4 }, - - {{ 4, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT, - PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT }}, - "surround40", - N_("Analog Surround 4.0"), - "analog-surround-40", - 7 }, - - {{ 4, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT, - PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT }}, - "a52", - N_("Digital Surround 4.0 (IEC958/AC3)"), - "iec958-ac3-surround-40", - 2 }, - - {{ 5, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT, - PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT, - PA_CHANNEL_POSITION_LFE }}, - "surround41", - N_("Analog Surround 4.1"), - "analog-surround-41", - 7 }, - - {{ 5, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT, - PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT, - PA_CHANNEL_POSITION_CENTER }}, - "surround50", - N_("Analog Surround 5.0"), - "analog-surround-50", - 7 }, - - {{ 6, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT, - PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT, - PA_CHANNEL_POSITION_CENTER, PA_CHANNEL_POSITION_LFE }}, - "surround51", - N_("Analog Surround 5.1"), - "analog-surround-51", - 8 }, - - {{ 6, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_CENTER, - PA_CHANNEL_POSITION_FRONT_RIGHT, PA_CHANNEL_POSITION_REAR_LEFT, - PA_CHANNEL_POSITION_REAR_RIGHT, PA_CHANNEL_POSITION_LFE}}, - "a52", - N_("Digital Surround 5.1 (IEC958/AC3)"), - "iec958-ac3-surround-51", - 3 }, - - {{ 8, { PA_CHANNEL_POSITION_FRONT_LEFT, PA_CHANNEL_POSITION_FRONT_RIGHT, - PA_CHANNEL_POSITION_REAR_LEFT, PA_CHANNEL_POSITION_REAR_RIGHT, - PA_CHANNEL_POSITION_CENTER, PA_CHANNEL_POSITION_LFE, - PA_CHANNEL_POSITION_SIDE_LEFT, PA_CHANNEL_POSITION_SIDE_RIGHT }}, - "surround71", - N_("Analog Surround 7.1"), - "analog-surround-71", - 7 }, - - {{ 0, { 0 }}, NULL, NULL, NULL, 0 } -}; - snd_pcm_t *pa_alsa_open_by_device_id_auto( const char *dev_id, char **dev, @@ -618,12 +395,13 @@ snd_pcm_t *pa_alsa_open_by_device_id_auto( snd_pcm_uframes_t tsched_size, pa_bool_t *use_mmap, pa_bool_t *use_tsched, - const pa_alsa_profile_info **profile) { + pa_alsa_profile_set *ps, + pa_alsa_mapping **mapping) { - int i; - int direction = 1; char *d; snd_pcm_t *pcm_handle; + void *state; + pa_alsa_mapping *m; pa_assert(dev_id); pa_assert(dev); @@ -631,114 +409,82 @@ snd_pcm_t *pa_alsa_open_by_device_id_auto( pa_assert(map); pa_assert(nfrags); pa_assert(period_size); + pa_assert(ps); /* First we try to find a device string with a superset of the - * requested channel map and open it without the plug: prefix. We - * iterate through our device table from top to bottom and take - * the first that matches. If we didn't find a working device that - * way, we iterate backwards, and check all devices that do not - * provide a superset of the requested channel map.*/ - - i = 0; - for (;;) { - - if ((direction > 0) == pa_channel_map_superset(&device_table[i].map, map)) { - pa_sample_spec try_ss; - - pa_log_debug("Checking for %s (%s)", device_table[i].name, device_table[i].alsa_name); - - d = pa_sprintf_malloc("%s:%s", device_table[i].alsa_name, dev_id); - - try_ss.channels = device_table[i].map.channels; - try_ss.rate = ss->rate; - try_ss.format = ss->format; - - pcm_handle = pa_alsa_open_by_device_string( - d, - dev, - &try_ss, - map, - mode, - nfrags, - period_size, - tsched_size, - use_mmap, - use_tsched, - TRUE); - - pa_xfree(d); - - if (pcm_handle) { - - *ss = try_ss; - *map = device_table[i].map; - pa_assert(map->channels == ss->channels); - - if (profile) - *profile = &device_table[i]; - - return pcm_handle; - } - } - - if (direction > 0) { - if (!device_table[i+1].alsa_name) { - /* OK, so we are at the end of our list. Let's turn - * back. */ - direction = -1; - } else { - /* We are not at the end of the list, so let's simply - * try the next entry */ - i++; - } + * requested channel map. We iterate through our device table from + * top to bottom and take the first that matches. If we didn't + * find a working device that way, we iterate backwards, and check + * all devices that do not provide a superset of the requested + * channel map.*/ + + PA_HASHMAP_FOREACH(m, ps->mappings, state) { + if (!pa_channel_map_superset(&m->channel_map, map)) + continue; + + pa_log_debug("Checking for superset %s (%s)", m->name, m->device_strings[0]); + + pcm_handle = pa_alsa_open_by_device_id_mapping( + dev_id, + dev, + ss, + map, + mode, + nfrags, + period_size, + tsched_size, + use_mmap, + use_tsched, + m); + + if (pcm_handle) { + if (mapping) + *mapping = m; + + return pcm_handle; } + } - if (direction < 0) { - - if (device_table[i+1].alsa_name && - device_table[i].map.channels == device_table[i+1].map.channels) { - - /* OK, the next entry has the same number of channels, - * let's try it */ - i++; - - } else { - /* Hmm, so the next entry does not have the same - * number of channels, so let's go backwards until we - * find the next entry with a differnt number of - * channels */ - - for (i--; i >= 0; i--) - if (device_table[i].map.channels != device_table[i+1].map.channels) - break; - - /* Hmm, there is no entry with a different number of - * entries, then we're done */ - if (i < 0) - break; - - /* OK, now lets find go back as long as we have the same number of channels */ - for (; i > 0; i--) - if (device_table[i].map.channels != device_table[i-1].map.channels) - break; - } + PA_HASHMAP_FOREACH_BACKWARDS(m, ps->mappings, state) { + if (pa_channel_map_superset(&m->channel_map, map)) + continue; + + pa_log_debug("Checking for subset %s (%s)", m->name, m->device_strings[0]); + + pcm_handle = pa_alsa_open_by_device_id_mapping( + dev_id, + dev, + ss, + map, + mode, + nfrags, + period_size, + tsched_size, + use_mmap, + use_tsched, + m); + + if (pcm_handle) { + if (mapping) + *mapping = m; + + return pcm_handle; } } - /* OK, we didn't find any good device, so let's try the raw plughw: stuff */ - + /* OK, we didn't find any good device, so let's try the raw hw: stuff */ d = pa_sprintf_malloc("hw:%s", dev_id); pa_log_debug("Trying %s as last resort...", d); pcm_handle = pa_alsa_open_by_device_string(d, dev, ss, map, mode, nfrags, period_size, tsched_size, use_mmap, use_tsched, FALSE); pa_xfree(d); - if (pcm_handle && profile) - *profile = NULL; + if (pcm_handle && mapping) + *mapping = NULL; return pcm_handle; } -snd_pcm_t *pa_alsa_open_by_device_id_profile( +snd_pcm_t *pa_alsa_open_by_device_id_mapping( const char *dev_id, char **dev, pa_sample_spec *ss, @@ -749,11 +495,11 @@ snd_pcm_t *pa_alsa_open_by_device_id_profile( snd_pcm_uframes_t tsched_size, pa_bool_t *use_mmap, pa_bool_t *use_tsched, - const pa_alsa_profile_info *profile) { + pa_alsa_mapping *m) { - char *d; snd_pcm_t *pcm_handle; pa_sample_spec try_ss; + pa_channel_map try_map; pa_assert(dev_id); pa_assert(dev); @@ -761,19 +507,19 @@ snd_pcm_t *pa_alsa_open_by_device_id_profile( pa_assert(map); pa_assert(nfrags); pa_assert(period_size); - pa_assert(profile); - - d = pa_sprintf_malloc("%s:%s", profile->alsa_name, dev_id); + pa_assert(m); - try_ss.channels = profile->map.channels; + try_ss.channels = m->channel_map.channels; try_ss.rate = ss->rate; try_ss.format = ss->format; + try_map = m->channel_map; - pcm_handle = pa_alsa_open_by_device_string( - d, + pcm_handle = pa_alsa_open_by_template( + m->device_strings, + dev_id, dev, &try_ss, - map, + &try_map, mode, nfrags, period_size, @@ -782,13 +528,11 @@ snd_pcm_t *pa_alsa_open_by_device_id_profile( use_tsched, TRUE); - pa_xfree(d); - if (!pcm_handle) return NULL; *ss = try_ss; - *map = profile->map; + *map = try_map; pa_assert(map->channels == ss->channels); return pcm_handle; @@ -821,21 +565,17 @@ snd_pcm_t *pa_alsa_open_by_device_string( for (;;) { pa_log_debug("Trying %s %s SND_PCM_NO_AUTO_FORMAT ...", d, reformat ? "without" : "with"); - /* We don't pass SND_PCM_NONBLOCK here, since alsa-lib <= - * 1.0.17a would then ignore the SND_PCM_NO_xxx flags. Instead - * we enable nonblock mode afterwards via - * snd_pcm_nonblock(). Also see - * http://mailman.alsa-project.org/pipermail/alsa-devel/2008-August/010258.html */ - if ((err = snd_pcm_open(&pcm_handle, d, mode, - /*SND_PCM_NONBLOCK|*/ + SND_PCM_NONBLOCK| SND_PCM_NO_AUTO_RESAMPLE| SND_PCM_NO_AUTO_CHANNELS| (reformat ? 0 : SND_PCM_NO_AUTO_FORMAT))) < 0) { - pa_log_info("Error opening PCM device %s: %s", d, snd_strerror(err)); + pa_log_info("Error opening PCM device %s: %s", d, pa_alsa_strerror(err)); goto fail; } + pa_log_debug("Managed to open %s", d); + if ((err = pa_alsa_set_hw_params(pcm_handle, ss, nfrags, period_size, tsched_size, use_mmap, use_tsched, require_exact_channel_number)) < 0) { if (!reformat) { @@ -846,7 +586,6 @@ snd_pcm_t *pa_alsa_open_by_device_string( } /* Hmm, some hw is very exotic, so we retry with plug, if without it didn't work */ - if (!pa_startswith(d, "plug:") && !pa_startswith(d, "plughw:")) { char *t; @@ -860,7 +599,7 @@ snd_pcm_t *pa_alsa_open_by_device_string( continue; } - pa_log_info("Failed to set hardware parameters on %s: %s", d, snd_strerror(err)); + pa_log_info("Failed to set hardware parameters on %s: %s", d, pa_alsa_strerror(err)); snd_pcm_close(pcm_handle); goto fail; @@ -883,414 +622,51 @@ fail: return NULL; } -int pa_alsa_probe_profiles( +snd_pcm_t *pa_alsa_open_by_template( + char **template, const char *dev_id, - const pa_sample_spec *ss, - void (*cb)(const pa_alsa_profile_info *sink, const pa_alsa_profile_info *source, void *userdata), - void *userdata) { - - const pa_alsa_profile_info *i; - - pa_assert(dev_id); - pa_assert(ss); - pa_assert(cb); - - /* We try each combination of playback/capture. We also try to - * open only for capture resp. only for sink. Don't get confused - * by the trailing entry in device_table we use for this! */ - - for (i = device_table; i < device_table + PA_ELEMENTSOF(device_table); i++) { - const pa_alsa_profile_info *j; - snd_pcm_t *pcm_i = NULL; - - if (i->alsa_name) { - char *id; - pa_sample_spec try_ss; - pa_channel_map try_map; - - pa_log_debug("Checking for playback on %s (%s)", i->name, i->alsa_name); - id = pa_sprintf_malloc("%s:%s", i->alsa_name, dev_id); - - try_ss = *ss; - try_ss.channels = i->map.channels; - try_map = i->map; - - pcm_i = pa_alsa_open_by_device_string( - id, NULL, - &try_ss, &try_map, - SND_PCM_STREAM_PLAYBACK, - NULL, NULL, 0, NULL, NULL, - TRUE); - - pa_xfree(id); - - if (!pcm_i) - continue; - } - - for (j = device_table; j < device_table + PA_ELEMENTSOF(device_table); j++) { - snd_pcm_t *pcm_j = NULL; - - if (j->alsa_name) { - char *jd; - pa_sample_spec try_ss; - pa_channel_map try_map; - - pa_log_debug("Checking for capture on %s (%s)", j->name, j->alsa_name); - jd = pa_sprintf_malloc("%s:%s", j->alsa_name, dev_id); - - try_ss = *ss; - try_ss.channels = j->map.channels; - try_map = j->map; - - pcm_j = pa_alsa_open_by_device_string( - jd, NULL, - &try_ss, &try_map, - SND_PCM_STREAM_CAPTURE, - NULL, NULL, 0, NULL, NULL, - TRUE); - - pa_xfree(jd); - - if (!pcm_j) - continue; - } - - if (pcm_j) - snd_pcm_close(pcm_j); - - if (i->alsa_name || j->alsa_name) - cb(i->alsa_name ? i : NULL, - j->alsa_name ? j : NULL, userdata); - } - - if (pcm_i) - snd_pcm_close(pcm_i); - } - - return TRUE; -} - -int pa_alsa_prepare_mixer(snd_mixer_t *mixer, const char *dev) { - int err; - - pa_assert(mixer); - pa_assert(dev); - - if ((err = snd_mixer_attach(mixer, dev)) < 0) { - pa_log_info("Unable to attach to mixer %s: %s", dev, snd_strerror(err)); - return -1; - } - - if ((err = snd_mixer_selem_register(mixer, NULL, NULL)) < 0) { - pa_log_warn("Unable to register mixer: %s", snd_strerror(err)); - return -1; - } - - if ((err = snd_mixer_load(mixer)) < 0) { - pa_log_warn("Unable to load mixer: %s", snd_strerror(err)); - return -1; - } - - pa_log_info("Successfully attached to mixer '%s'", dev); - - return 0; -} - -static pa_bool_t elem_has_volume(snd_mixer_elem_t *elem, pa_bool_t playback) { - pa_assert(elem); - - if (playback && snd_mixer_selem_has_playback_volume(elem)) - return TRUE; - - if (!playback && snd_mixer_selem_has_capture_volume(elem)) - return TRUE; - - return FALSE; -} - -static pa_bool_t elem_has_switch(snd_mixer_elem_t *elem, pa_bool_t playback) { - pa_assert(elem); - - if (playback && snd_mixer_selem_has_playback_switch(elem)) - return TRUE; - - if (!playback && snd_mixer_selem_has_capture_switch(elem)) - return TRUE; - - return FALSE; -} - -snd_mixer_elem_t *pa_alsa_find_elem(snd_mixer_t *mixer, const char *name, const char *fallback, pa_bool_t playback) { - snd_mixer_elem_t *elem = NULL, *fallback_elem = NULL; - snd_mixer_selem_id_t *sid = NULL; - - snd_mixer_selem_id_alloca(&sid); - - pa_assert(mixer); - pa_assert(name); - - snd_mixer_selem_id_set_name(sid, name); - snd_mixer_selem_id_set_index(sid, 0); - - if ((elem = snd_mixer_find_selem(mixer, sid))) { - - if (elem_has_volume(elem, playback) && - elem_has_switch(elem, playback)) - goto success; - - if (!elem_has_volume(elem, playback) && - !elem_has_switch(elem, playback)) - elem = NULL; - } - - pa_log_info("Cannot find mixer control \"%s\" or mixer control is no combination of switch/volume.", snd_mixer_selem_id_get_name(sid)); - - if (fallback) { - snd_mixer_selem_id_set_name(sid, fallback); - snd_mixer_selem_id_set_index(sid, 0); - - if ((fallback_elem = snd_mixer_find_selem(mixer, sid))) { - - if (elem_has_volume(fallback_elem, playback) && - elem_has_switch(fallback_elem, playback)) { - elem = fallback_elem; - goto success; - } - - if (!elem_has_volume(fallback_elem, playback) && - !elem_has_switch(fallback_elem, playback)) - fallback_elem = NULL; - } - - pa_log_warn("Cannot find fallback mixer control \"%s\" or mixer control is no combination of switch/volume.", snd_mixer_selem_id_get_name(sid)); - } - - if (elem && fallback_elem) { - - /* Hmm, so we have both elements, but neither has both mute - * and volume. Let's prefer the one with the volume */ - - if (elem_has_volume(elem, playback)) - goto success; - - if (elem_has_volume(fallback_elem, playback)) { - elem = fallback_elem; - goto success; - } - } - - if (!elem && fallback_elem) - elem = fallback_elem; - -success: - - if (elem) - pa_log_info("Using mixer control \"%s\".", snd_mixer_selem_id_get_name(sid)); - - return elem; -} - -int pa_alsa_find_mixer_and_elem( - snd_pcm_t *pcm, - snd_mixer_t **_m, - snd_mixer_elem_t **_e) { - - int err; - snd_mixer_t *m; - snd_mixer_elem_t *e; - pa_bool_t found = FALSE; - const char *dev; - - pa_assert(pcm); - pa_assert(_m); - pa_assert(_e); - - if ((err = snd_mixer_open(&m, 0)) < 0) { - pa_log("Error opening mixer: %s", snd_strerror(err)); - return -1; - } - - /* First, try by name */ - if ((dev = snd_pcm_name(pcm))) - if (pa_alsa_prepare_mixer(m, dev) >= 0) - found = TRUE; - - /* Then, try by card index */ - if (!found) { - snd_pcm_info_t* info; - snd_pcm_info_alloca(&info); - - if (snd_pcm_info(pcm, info) >= 0) { - char *md; - int card_idx; - - if ((card_idx = snd_pcm_info_get_card(info)) >= 0) { - - md = pa_sprintf_malloc("hw:%i", card_idx); - - if (!dev || !pa_streq(dev, md)) - if (pa_alsa_prepare_mixer(m, md) >= 0) - found = TRUE; - - pa_xfree(md); - } - } - } - - if (!found) { - snd_mixer_close(m); - return -1; - } - - switch (snd_pcm_stream(pcm)) { - - case SND_PCM_STREAM_PLAYBACK: - e = pa_alsa_find_elem(m, "Master", "PCM", TRUE); - break; - - case SND_PCM_STREAM_CAPTURE: - e = pa_alsa_find_elem(m, "Capture", "Mic", FALSE); - break; - - default: - pa_assert_not_reached(); - } - - if (!e) { - snd_mixer_close(m); - return -1; - } - - pa_assert(e && m); - - *_m = m; - *_e = e; - - return 0; -} - -static const snd_mixer_selem_channel_id_t alsa_channel_ids[PA_CHANNEL_POSITION_MAX] = { - [PA_CHANNEL_POSITION_MONO] = SND_MIXER_SCHN_MONO, /* The ALSA name is just an alias! */ - - [PA_CHANNEL_POSITION_FRONT_CENTER] = SND_MIXER_SCHN_FRONT_CENTER, - [PA_CHANNEL_POSITION_FRONT_LEFT] = SND_MIXER_SCHN_FRONT_LEFT, - [PA_CHANNEL_POSITION_FRONT_RIGHT] = SND_MIXER_SCHN_FRONT_RIGHT, - - [PA_CHANNEL_POSITION_REAR_CENTER] = SND_MIXER_SCHN_REAR_CENTER, - [PA_CHANNEL_POSITION_REAR_LEFT] = SND_MIXER_SCHN_REAR_LEFT, - [PA_CHANNEL_POSITION_REAR_RIGHT] = SND_MIXER_SCHN_REAR_RIGHT, - - [PA_CHANNEL_POSITION_LFE] = SND_MIXER_SCHN_WOOFER, - - [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] = SND_MIXER_SCHN_UNKNOWN, - - [PA_CHANNEL_POSITION_SIDE_LEFT] = SND_MIXER_SCHN_SIDE_LEFT, - [PA_CHANNEL_POSITION_SIDE_RIGHT] = SND_MIXER_SCHN_SIDE_RIGHT, - - [PA_CHANNEL_POSITION_AUX0] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX1] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX2] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX3] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX4] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX5] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX6] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX7] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX8] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX9] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX10] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX11] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX12] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX13] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX14] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX15] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX16] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX17] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX18] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX19] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX20] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX21] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX22] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX23] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX24] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX25] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX26] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX27] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX28] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX29] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX30] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_AUX31] = SND_MIXER_SCHN_UNKNOWN, - - [PA_CHANNEL_POSITION_TOP_CENTER] = SND_MIXER_SCHN_UNKNOWN, - - [PA_CHANNEL_POSITION_TOP_FRONT_CENTER] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_TOP_FRONT_LEFT] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_TOP_FRONT_RIGHT] = SND_MIXER_SCHN_UNKNOWN, - - [PA_CHANNEL_POSITION_TOP_REAR_CENTER] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_TOP_REAR_LEFT] = SND_MIXER_SCHN_UNKNOWN, - [PA_CHANNEL_POSITION_TOP_REAR_RIGHT] = SND_MIXER_SCHN_UNKNOWN -}; - - -int pa_alsa_calc_mixer_map(snd_mixer_elem_t *elem, const pa_channel_map *channel_map, snd_mixer_selem_channel_id_t mixer_map[], pa_bool_t playback) { - unsigned i; - pa_bool_t alsa_channel_used[SND_MIXER_SCHN_LAST]; - pa_bool_t mono_used = FALSE; - - pa_assert(elem); - pa_assert(channel_map); - pa_assert(mixer_map); - - memset(&alsa_channel_used, 0, sizeof(alsa_channel_used)); - - if (channel_map->channels > 1 && - ((playback && snd_mixer_selem_has_playback_volume_joined(elem)) || - (!playback && snd_mixer_selem_has_capture_volume_joined(elem)))) { - pa_log_info("ALSA device lacks independant volume controls for each channel."); - return -1; - } - - for (i = 0; i < channel_map->channels; i++) { - snd_mixer_selem_channel_id_t id; - pa_bool_t is_mono; - - is_mono = channel_map->map[i] == PA_CHANNEL_POSITION_MONO; - id = alsa_channel_ids[channel_map->map[i]]; - - if (!is_mono && id == SND_MIXER_SCHN_UNKNOWN) { - pa_log_info("Configured channel map contains channel '%s' that is unknown to the ALSA mixer.", pa_channel_position_to_string(channel_map->map[i])); - return -1; - } - - if ((is_mono && mono_used) || (!is_mono && alsa_channel_used[id])) { - pa_log_info("Channel map has duplicate channel '%s', falling back to software volume control.", pa_channel_position_to_string(channel_map->map[i])); - return -1; - } - - if ((playback && (!snd_mixer_selem_has_playback_channel(elem, id) || (is_mono && !snd_mixer_selem_is_playback_mono(elem)))) || - (!playback && (!snd_mixer_selem_has_capture_channel(elem, id) || (is_mono && !snd_mixer_selem_is_capture_mono(elem))))) { - - pa_log_info("ALSA device lacks separate volumes control for channel '%s'", pa_channel_position_to_string(channel_map->map[i])); - return -1; - } + char **dev, + pa_sample_spec *ss, + pa_channel_map* map, + int mode, + uint32_t *nfrags, + snd_pcm_uframes_t *period_size, + snd_pcm_uframes_t tsched_size, + pa_bool_t *use_mmap, + pa_bool_t *use_tsched, + pa_bool_t require_exact_channel_number) { - if (is_mono) { - mixer_map[i] = SND_MIXER_SCHN_MONO; - mono_used = TRUE; - } else { - mixer_map[i] = id; - alsa_channel_used[id] = TRUE; - } + snd_pcm_t *pcm_handle; + char **i; + + for (i = template; *i; i++) { + char *d; + + d = pa_replace(*i, "%f", dev_id); + + pcm_handle = pa_alsa_open_by_device_string( + d, + dev, + ss, + map, + mode, + nfrags, + period_size, + tsched_size, + use_mmap, + use_tsched, + require_exact_channel_number); + + pa_xfree(d); + + if (pcm_handle) + return pcm_handle; } - pa_log_info("All %u channels can be mapped to mixer channels.", channel_map->channels); - - return 0; + return NULL; } -void pa_alsa_dump(snd_pcm_t *pcm) { +void pa_alsa_dump(pa_log_level_t level, snd_pcm_t *pcm) { int err; snd_output_t *out; @@ -1299,11 +675,11 @@ void pa_alsa_dump(snd_pcm_t *pcm) { pa_assert_se(snd_output_buffer_open(&out) == 0); if ((err = snd_pcm_dump(pcm, out)) < 0) - pa_log_debug("snd_pcm_dump(): %s", snd_strerror(err)); + pa_logl(level, "snd_pcm_dump(): %s", pa_alsa_strerror(err)); else { char *s = NULL; snd_output_buffer_string(out, &s); - pa_log_debug("snd_pcm_dump():\n%s", pa_strnull(s)); + pa_logl(level, "snd_pcm_dump():\n%s", pa_strnull(s)); } pa_assert_se(snd_output_close(out) == 0); @@ -1313,24 +689,33 @@ void pa_alsa_dump_status(snd_pcm_t *pcm) { int err; snd_output_t *out; snd_pcm_status_t *status; + char *s = NULL; pa_assert(pcm); snd_pcm_status_alloca(&status); - pa_assert_se(snd_output_buffer_open(&out) == 0); + if ((err = snd_output_buffer_open(&out)) < 0) { + pa_log_debug("snd_output_buffer_open() failed: %s", pa_cstrerror(err)); + return; + } - pa_assert_se(snd_pcm_status(pcm, status) == 0); + if ((err = snd_pcm_status(pcm, status)) < 0) { + pa_log_debug("snd_pcm_status() failed: %s", pa_cstrerror(err)); + goto finish; + } - if ((err = snd_pcm_status_dump(status, out)) < 0) - pa_log_debug("snd_pcm_dump(): %s", snd_strerror(err)); - else { - char *s = NULL; - snd_output_buffer_string(out, &s); - pa_log_debug("snd_pcm_dump():\n%s", pa_strnull(s)); + if ((err = snd_pcm_status_dump(status, out)) < 0) { + pa_log_debug("snd_pcm_dump(): %s", pa_alsa_strerror(err)); + goto finish; } - pa_assert_se(snd_output_close(out) == 0); + snd_output_buffer_string(out, &s); + pa_log_debug("snd_pcm_dump():\n%s", pa_strnull(s)); + +finish: + + snd_output_close(out); } static void alsa_error_handler(const char *file, int line, const char *function, int err, const char *fmt,...) { @@ -1350,38 +735,43 @@ static void alsa_error_handler(const char *file, int line, const char *function, static pa_atomic_t n_error_handler_installed = PA_ATOMIC_INIT(0); -void pa_alsa_redirect_errors_inc(void) { +void pa_alsa_refcnt_inc(void) { /* This is not really thread safe, but we do our best */ if (pa_atomic_inc(&n_error_handler_installed) == 0) snd_lib_error_set_handler(alsa_error_handler); } -void pa_alsa_redirect_errors_dec(void) { +void pa_alsa_refcnt_dec(void) { int r; pa_assert_se((r = pa_atomic_dec(&n_error_handler_installed)) >= 1); - if (r == 1) + if (r == 1) { snd_lib_error_set_handler(NULL); + snd_config_update_free_global(); + } } pa_bool_t pa_alsa_init_description(pa_proplist *p) { - const char *s; + const char *d, *k; pa_assert(p); if (pa_device_init_description(p)) return TRUE; - if ((s = pa_proplist_gets(p, "alsa.card_name"))) { - pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, s); - return TRUE; - } + if (!(d = pa_proplist_gets(p, "alsa.card_name"))) + d = pa_proplist_gets(p, "alsa.name"); - if ((s = pa_proplist_gets(p, "alsa.name"))) { - pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, s); - return TRUE; - } + if (!d) + return FALSE; + + k = pa_proplist_gets(p, PA_PROP_DEVICE_PROFILE_DESCRIPTION); + + if (d && k) + pa_proplist_setf(p, PA_PROP_DEVICE_DESCRIPTION, _("%s %s"), d, k); + else if (d) + pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, d); return FALSE; } @@ -1410,7 +800,7 @@ void pa_alsa_init_proplist_card(pa_core *c, pa_proplist *p, int card) { } #ifdef HAVE_UDEV - pa_udev_get_info(c, p, card); + pa_udev_get_info(card, p); #endif #ifdef HAVE_HAL @@ -1447,16 +837,14 @@ void pa_alsa_init_proplist_pcm_info(pa_core *c, pa_proplist *p, snd_pcm_info_t * pa_proplist_sets(p, PA_PROP_DEVICE_API, "alsa"); - class = snd_pcm_info_get_class(pcm_info); - if (class <= SND_PCM_CLASS_LAST) { + if ((class = snd_pcm_info_get_class(pcm_info)) <= SND_PCM_CLASS_LAST) { if (class_table[class]) pa_proplist_sets(p, PA_PROP_DEVICE_CLASS, class_table[class]); if (alsa_class_table[class]) pa_proplist_sets(p, "alsa.class", alsa_class_table[class]); } - subclass = snd_pcm_info_get_subclass(pcm_info); - if (subclass <= SND_PCM_SUBCLASS_LAST) + if ((subclass = snd_pcm_info_get_subclass(pcm_info)) <= SND_PCM_SUBCLASS_LAST) if (alsa_subclass_table[subclass]) pa_proplist_sets(p, "alsa.subclass", alsa_subclass_table[subclass]); @@ -1485,7 +873,7 @@ void pa_alsa_init_proplist_pcm(pa_core *c, pa_proplist *p, snd_pcm_t *pcm) { snd_pcm_info_alloca(&info); if ((err = snd_pcm_hw_params_current(pcm, hwparams)) < 0) - pa_log_warn("Error fetching hardware parameter info: %s", snd_strerror(err)); + pa_log_warn("Error fetching hardware parameter info: %s", pa_alsa_strerror(err)); else { if ((bits = snd_pcm_hw_params_get_sbits(hwparams)) >= 0) @@ -1493,11 +881,41 @@ void pa_alsa_init_proplist_pcm(pa_core *c, pa_proplist *p, snd_pcm_t *pcm) { } if ((err = snd_pcm_info(pcm, info)) < 0) - pa_log_warn("Error fetching PCM info: %s", snd_strerror(err)); + pa_log_warn("Error fetching PCM info: %s", pa_alsa_strerror(err)); else pa_alsa_init_proplist_pcm_info(c, p, info); } +void pa_alsa_init_proplist_ctl(pa_proplist *p, const char *name) { + int err; + snd_ctl_t *ctl; + snd_ctl_card_info_t *info; + const char *t; + + pa_assert(p); + + snd_ctl_card_info_alloca(&info); + + if ((err = snd_ctl_open(&ctl, name, 0)) < 0) { + pa_log_warn("Error opening low-level control device '%s'", name); + return; + } + + if ((err = snd_ctl_card_info(ctl, info)) < 0) { + pa_log_warn("Control device %s card info: %s", name, snd_strerror(err)); + snd_ctl_close(ctl); + return; + } + + if ((t = snd_ctl_card_info_get_mixername(info)) && *t) + pa_proplist_sets(p, "alsa.mixer_name", t); + + if ((t = snd_ctl_card_info_get_components(info)) && *t) + pa_proplist_sets(p, "alsa.components", t); + + snd_ctl_close(ctl); +} + int pa_alsa_recover_from_poll(snd_pcm_t *pcm, int revents) { snd_pcm_state_t state; int err; @@ -1526,14 +944,14 @@ int pa_alsa_recover_from_poll(snd_pcm_t *pcm, int revents) { case SND_PCM_STATE_XRUN: if ((err = snd_pcm_recover(pcm, -EPIPE, 1)) != 0) { - pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and XRUN: %s", snd_strerror(err)); + pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and XRUN: %s", pa_alsa_strerror(err)); return -1; } break; case SND_PCM_STATE_SUSPENDED: if ((err = snd_pcm_recover(pcm, -ESTRPIPE, 1)) != 0) { - pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and SUSPENDED: %s", snd_strerror(err)); + pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP and SUSPENDED: %s", pa_alsa_strerror(err)); return -1; } break; @@ -1543,7 +961,7 @@ int pa_alsa_recover_from_poll(snd_pcm_t *pcm, int revents) { snd_pcm_drop(pcm); if ((err = snd_pcm_prepare(pcm)) < 0) { - pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP with snd_pcm_prepare(): %s", snd_strerror(err)); + pa_log_warn("Could not recover from POLLERR|POLLNVAL|POLLHUP with snd_pcm_prepare(): %s", pa_alsa_strerror(err)); return -1; } break; @@ -1560,7 +978,7 @@ pa_rtpoll_item* pa_alsa_build_pollfd(snd_pcm_t *pcm, pa_rtpoll *rtpoll) { pa_assert(pcm); if ((n = snd_pcm_poll_descriptors_count(pcm)) < 0) { - pa_log("snd_pcm_poll_descriptors_count() failed: %s", snd_strerror(n)); + pa_log("snd_pcm_poll_descriptors_count() failed: %s", pa_alsa_strerror(n)); return NULL; } @@ -1568,7 +986,7 @@ pa_rtpoll_item* pa_alsa_build_pollfd(snd_pcm_t *pcm, pa_rtpoll *rtpoll) { pollfd = pa_rtpoll_item_get_pollfd(item, NULL); if ((err = snd_pcm_poll_descriptors(pcm, pollfd, (unsigned) n)) < 0) { - pa_log("snd_pcm_poll_descriptors() failed: %s", snd_strerror(err)); + pa_log("snd_pcm_poll_descriptors() failed: %s", pa_alsa_strerror(err)); pa_rtpoll_item_free(item); return NULL; } @@ -1605,6 +1023,7 @@ snd_pcm_sframes_t pa_alsa_safe_avail(snd_pcm_t *pcm, size_t hwbuf_size, const pa (unsigned long) (pa_bytes_to_usec(k, ss) / PA_USEC_PER_MSEC), pa_strnull(dn)); pa_xfree(dn); + pa_alsa_dump(PA_LOG_ERROR, pcm); } PA_ONCE_END; /* Mhmm, let's try not to fail completely */ @@ -1646,6 +1065,7 @@ int pa_alsa_safe_delay(snd_pcm_t *pcm, snd_pcm_sframes_t *delay, size_t hwbuf_si (unsigned long) (pa_bytes_to_usec(abs_k, ss) / PA_USEC_PER_MSEC), pa_strnull(dn)); pa_xfree(dn); + pa_alsa_dump(PA_LOG_ERROR, pcm); } PA_ONCE_END; /* Mhmm, let's try not to fail completely */ @@ -1691,6 +1111,7 @@ int pa_alsa_safe_mmap_begin(snd_pcm_t *pcm, const snd_pcm_channel_area_t **areas (unsigned long) (pa_bytes_to_usec(k, ss) / PA_USEC_PER_MSEC), pa_strnull(dn)); pa_xfree(dn); + pa_alsa_dump(PA_LOG_ERROR, pcm); } PA_ONCE_END; return r; @@ -1716,10 +1137,11 @@ char *pa_alsa_get_driver_name(int card) { char *pa_alsa_get_driver_name_by_pcm(snd_pcm_t *pcm) { int card; - snd_pcm_info_t* info; snd_pcm_info_alloca(&info); + pa_assert(pcm); + if (snd_pcm_info(pcm, info) < 0) return NULL; @@ -1749,3 +1171,54 @@ char *pa_alsa_get_reserve_name(const char *device) { return pa_sprintf_malloc("Audio%i", i); } + +pa_bool_t pa_alsa_pcm_is_hw(snd_pcm_t *pcm) { + snd_pcm_info_t* info; + snd_pcm_info_alloca(&info); + + pa_assert(pcm); + + if (snd_pcm_info(pcm, info) < 0) + return FALSE; + + return snd_pcm_info_get_card(info) >= 0; +} + +pa_bool_t pa_alsa_pcm_is_modem(snd_pcm_t *pcm) { + snd_pcm_info_t* info; + snd_pcm_info_alloca(&info); + + pa_assert(pcm); + + if (snd_pcm_info(pcm, info) < 0) + return FALSE; + + return snd_pcm_info_get_class(info) == SND_PCM_CLASS_MODEM; +} + +PA_STATIC_TLS_DECLARE(cstrerror, pa_xfree); + +const char* pa_alsa_strerror(int errnum) { + const char *original = NULL; + char *translated, *t; + char errbuf[128]; + + if ((t = PA_STATIC_TLS_GET(cstrerror))) + pa_xfree(t); + + original = snd_strerror(errnum); + + if (!original) { + pa_snprintf(errbuf, sizeof(errbuf), "Unknown error %i", errnum); + original = errbuf; + } + + if (!(translated = pa_locale_to_utf8(original))) { + pa_log_warn("Unable to convert error string to locale, filtering."); + translated = pa_utf8_filter(original); + } + + PA_STATIC_TLS_SET(cstrerror, translated); + + return translated; +} diff --git a/src/modules/alsa/alsa-util.h b/src/modules/alsa/alsa-util.h index fe0f71e0..830a922e 100644 --- a/src/modules/alsa/alsa-util.h +++ b/src/modules/alsa/alsa-util.h @@ -30,99 +30,97 @@ #include <pulse/mainloop-api.h> #include <pulse/channelmap.h> #include <pulse/proplist.h> +#include <pulse/volume.h> +#include <pulsecore/llist.h> #include <pulsecore/rtpoll.h> #include <pulsecore/core.h> +#include <pulsecore/log.h> -typedef struct pa_alsa_fdlist pa_alsa_fdlist; - -struct pa_alsa_fdlist *pa_alsa_fdlist_new(void); -void pa_alsa_fdlist_free(struct pa_alsa_fdlist *fdl); -int pa_alsa_fdlist_set_mixer(struct pa_alsa_fdlist *fdl, snd_mixer_t *mixer_handle, pa_mainloop_api* m); +#include "alsa-mixer.h" int pa_alsa_set_hw_params( snd_pcm_t *pcm_handle, - pa_sample_spec *ss, - uint32_t *periods, - snd_pcm_uframes_t *period_size, + pa_sample_spec *ss, /* modified at return */ + uint32_t *periods, /* modified at return */ + snd_pcm_uframes_t *period_size, /* modified at return */ snd_pcm_uframes_t tsched_size, - pa_bool_t *use_mmap, - pa_bool_t *use_tsched, + pa_bool_t *use_mmap, /* modified at return */ + pa_bool_t *use_tsched, /* modified at return */ pa_bool_t require_exact_channel_number); -int pa_alsa_set_sw_params(snd_pcm_t *pcm, snd_pcm_uframes_t avail_min); - -int pa_alsa_prepare_mixer(snd_mixer_t *mixer, const char *dev); -snd_mixer_elem_t *pa_alsa_find_elem(snd_mixer_t *mixer, const char *name, const char *fallback, pa_bool_t playback); -int pa_alsa_find_mixer_and_elem(snd_pcm_t *pcm, snd_mixer_t **_m, snd_mixer_elem_t **_e); +int pa_alsa_set_sw_params( + snd_pcm_t *pcm, + snd_pcm_uframes_t avail_min); -typedef struct pa_alsa_profile_info { - pa_channel_map map; - const char *alsa_name; - const char *description; /* internationalized */ - const char *name; - unsigned priority; -} pa_alsa_profile_info; - -/* Picks a working profile based on the specified ss/map */ +/* Picks a working mapping from the profile set based on the specified ss/map */ snd_pcm_t *pa_alsa_open_by_device_id_auto( const char *dev_id, - char **dev, - pa_sample_spec *ss, - pa_channel_map* map, + char **dev, /* modified at return */ + pa_sample_spec *ss, /* modified at return */ + pa_channel_map* map, /* modified at return */ int mode, - uint32_t *nfrags, - snd_pcm_uframes_t *period_size, + uint32_t *nfrags, /* modified at return */ + snd_pcm_uframes_t *period_size, /* modified at return */ snd_pcm_uframes_t tsched_size, - pa_bool_t *use_mmap, - pa_bool_t *use_tsched, - const pa_alsa_profile_info **profile); + pa_bool_t *use_mmap, /* modified at return */ + pa_bool_t *use_tsched, /* modified at return */ + pa_alsa_profile_set *ps, + pa_alsa_mapping **mapping); /* modified at return */ -/* Uses the specified profile */ -snd_pcm_t *pa_alsa_open_by_device_id_profile( +/* Uses the specified mapping */ +snd_pcm_t *pa_alsa_open_by_device_id_mapping( const char *dev_id, - char **dev, - pa_sample_spec *ss, - pa_channel_map* map, + char **dev, /* modified at return */ + pa_sample_spec *ss, /* modified at return */ + pa_channel_map* map, /* modified at return */ int mode, - uint32_t *nfrags, - snd_pcm_uframes_t *period_size, + uint32_t *nfrags, /* modified at return */ + snd_pcm_uframes_t *period_size, /* modified at return */ snd_pcm_uframes_t tsched_size, - pa_bool_t *use_mmap, - pa_bool_t *use_tsched, - const pa_alsa_profile_info *profile); + pa_bool_t *use_mmap, /* modified at return */ + pa_bool_t *use_tsched, /* modified at return */ + pa_alsa_mapping *mapping); /* Opens the explicit ALSA device */ snd_pcm_t *pa_alsa_open_by_device_string( - const char *device, - char **dev, - pa_sample_spec *ss, - pa_channel_map* map, + const char *dir, + char **dev, /* modified at return */ + pa_sample_spec *ss, /* modified at return */ + pa_channel_map* map, /* modified at return */ int mode, - uint32_t *nfrags, - snd_pcm_uframes_t *period_size, + uint32_t *nfrags, /* modified at return */ + snd_pcm_uframes_t *period_size, /* modified at return */ snd_pcm_uframes_t tsched_size, - pa_bool_t *use_mmap, - pa_bool_t *use_tsched, + pa_bool_t *use_mmap, /* modified at return */ + pa_bool_t *use_tsched, /* modified at return */ pa_bool_t require_exact_channel_number); -int pa_alsa_probe_profiles( +/* Opens the explicit ALSA device with a fallback list */ +snd_pcm_t *pa_alsa_open_by_template( + char **template, const char *dev_id, - const pa_sample_spec *ss, - void (*cb)(const pa_alsa_profile_info *sink, const pa_alsa_profile_info *source, void *userdata), - void *userdata); - -int pa_alsa_calc_mixer_map(snd_mixer_elem_t *elem, const pa_channel_map *channel_map, snd_mixer_selem_channel_id_t mixer_map[], pa_bool_t playback); + char **dev, /* modified at return */ + pa_sample_spec *ss, /* modified at return */ + pa_channel_map* map, /* modified at return */ + int mode, + uint32_t *nfrags, /* modified at return */ + snd_pcm_uframes_t *period_size, /* modified at return */ + snd_pcm_uframes_t tsched_size, + pa_bool_t *use_mmap, /* modified at return */ + pa_bool_t *use_tsched, /* modified at return */ + pa_bool_t require_exact_channel_number); -void pa_alsa_dump(snd_pcm_t *pcm); +void pa_alsa_dump(pa_log_level_t level, snd_pcm_t *pcm); void pa_alsa_dump_status(snd_pcm_t *pcm); -void pa_alsa_redirect_errors_inc(void); -void pa_alsa_redirect_errors_dec(void); +void pa_alsa_refcnt_inc(void); +void pa_alsa_refcnt_dec(void); void pa_alsa_init_proplist_pcm_info(pa_core *c, pa_proplist *p, snd_pcm_info_t *pcm_info); void pa_alsa_init_proplist_card(pa_core *c, pa_proplist *p, int card); void pa_alsa_init_proplist_pcm(pa_core *c, pa_proplist *p, snd_pcm_t *pcm); +void pa_alsa_init_proplist_ctl(pa_proplist *p, const char *name); pa_bool_t pa_alsa_init_description(pa_proplist *p); int pa_alsa_recover_from_poll(snd_pcm_t *pcm, int revents); @@ -134,9 +132,13 @@ int pa_alsa_safe_delay(snd_pcm_t *pcm, snd_pcm_sframes_t *delay, size_t hwbuf_si int pa_alsa_safe_mmap_begin(snd_pcm_t *pcm, const snd_pcm_channel_area_t **areas, snd_pcm_uframes_t *offset, snd_pcm_uframes_t *frames, size_t hwbuf_size, const pa_sample_spec *ss); char *pa_alsa_get_driver_name(int card); - char *pa_alsa_get_driver_name_by_pcm(snd_pcm_t *pcm); char *pa_alsa_get_reserve_name(const char *device); +pa_bool_t pa_alsa_pcm_is_hw(snd_pcm_t *pcm); +pa_bool_t pa_alsa_pcm_is_modem(snd_pcm_t *pcm); + +const char* pa_alsa_strerror(int errnum); + #endif diff --git a/src/modules/alsa/mixer/paths/analog-input-aux.conf b/src/modules/alsa/mixer/paths/analog-input-aux.conf new file mode 100644 index 00000000..db78eb48 --- /dev/null +++ b/src/modules/alsa/mixer/paths/analog-input-aux.conf @@ -0,0 +1,62 @@ +# This file is part of PulseAudio. +# +# 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 +# along with PulseAudio; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + +; For devices where an 'Aux' element exists +; +; See analog-output.conf.common for an explanation on the directives + +[General] +priority = 90 +name = analog-input + +[Element Capture] +switch = mute +volume = merge +override-map.1 = all +override-map.2 = all-left,all-right + +[Element Mic] +switch = off +volume = off + +[Element Line] +switch = off +volume = off + +[Element Aux] +required = any +switch = mute +volume = merge +override-map.1 = all +override-map.2 = all-left,all-right + +[Element Video] +switch = off +volume = off + +[Element Mic/Line] +switch = off +volume = off + +[Element TV Tuner] +switch = off +volume = off + +[Element FM] +switch = off +volume = off + +.include analog-input.conf.common diff --git a/src/modules/alsa/mixer/paths/analog-input-fm.conf b/src/modules/alsa/mixer/paths/analog-input-fm.conf new file mode 100644 index 00000000..baf674aa --- /dev/null +++ b/src/modules/alsa/mixer/paths/analog-input-fm.conf @@ -0,0 +1,62 @@ +# This file is part of PulseAudio. +# +# 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 +# along with PulseAudio; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + +; For devices where an 'FM' element exists +; +; See analog-output.conf.common for an explanation on the directives + +[General] +priority = 70 +name = analog-input-radio + +[Element Capture] +switch = mute +volume = merge +override-map.1 = all +override-map.2 = all-left,all-right + +[Element Mic] +switch = off +volume = off + +[Element Line] +switch = off +volume = off + +[Element Aux] +switch = off +volume = off + +[Element Video] +switch = off +volume = off + +[Element Mic/Line] +switch = off +volume = off + +[Element TV Tuner] +switch = off +volume = off + +[Element FM] +required = any +switch = mute +volume = merge +override-map.1 = all +override-map.2 = all-left,all-right + +.include analog-input.conf.common diff --git a/src/modules/alsa/mixer/paths/analog-input-linein.conf b/src/modules/alsa/mixer/paths/analog-input-linein.conf new file mode 100644 index 00000000..4be5722d --- /dev/null +++ b/src/modules/alsa/mixer/paths/analog-input-linein.conf @@ -0,0 +1,61 @@ +# This file is part of PulseAudio. +# +# 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 +# along with PulseAudio; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + +; For devices where a 'Line' element exists +; +; See analog-output.conf.common for an explanation on the directives + +[General] +priority = 90 + +[Element Capture] +switch = mute +volume = merge +override-map.1 = all +override-map.2 = all-left,all-right + +[Element Mic] +switch = off +volume = off + +[Element Line] +required = any +switch = mute +volume = merge +override-map.1 = all +override-map.2 = all-left,all-right + +[Element Aux] +switch = off +volume = off + +[Element Video] +switch = off +volume = off + +[Element Mic/Line] +switch = off +volume = off + +[Element TV Tuner] +switch = off +volume = off + +[Element FM] +switch = off +volume = off + +.include analog-input.conf.common diff --git a/src/modules/alsa/mixer/paths/analog-input-mic-line.conf b/src/modules/alsa/mixer/paths/analog-input-mic-line.conf new file mode 100644 index 00000000..f7f30854 --- /dev/null +++ b/src/modules/alsa/mixer/paths/analog-input-mic-line.conf @@ -0,0 +1,63 @@ +# This file is part of PulseAudio. +# +# 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 +# along with PulseAudio; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + +; For devices where a 'Mic/Line' element exists +; +; See analog-output.conf.common for an explanation on the directives + +[General] +priority = 90 +name = analog-input + +[Element Capture] +switch = mute +volume = merge +override-map.1 = all +override-map.2 = all-left,all-right + +[Element Mic] +switch = off +volume = off + +[Element Line] +switch = off +volume = off + +[Element Aux] +switch = off +volume = off + +[Element Video] +switch = off +volume = off + +[Element Mic/Line] +required = any +switch = mute +volume = merge +override-map.1 = all +override-map.2 = all-left,all-right + +[Element TV Tuner] +switch = off +volume = off + +[Element FM] +switch = off +volume = off + +.include analog-input.conf.common +.include analog-input-mic.conf.common diff --git a/src/modules/alsa/mixer/paths/analog-input-mic.conf b/src/modules/alsa/mixer/paths/analog-input-mic.conf new file mode 100644 index 00000000..2a36f2f3 --- /dev/null +++ b/src/modules/alsa/mixer/paths/analog-input-mic.conf @@ -0,0 +1,63 @@ +# This file is part of PulseAudio. +# +# 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 +# along with PulseAudio; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + +; For devices where a 'Mic' element exists +; +; See analog-output.conf.common for an explanation on the directives + +[General] +priority = 100 +name = analog-input-microphone + +[Element Capture] +switch = mute +volume = merge +override-map.1 = all +override-map.2 = all-left,all-right + +[Element Mic] +required = any +switch = mute +volume = merge +override-map.1 = all +override-map.2 = all-left,all-right + +[Element Line] +switch = off +volume = off + +[Element Aux] +switch = off +volume = off + +[Element Video] +switch = off +volume = off + +[Element Mic/Line] +switch = off +volume = off + +[Element TV Tuner] +switch = off +volume = off + +[Element FM] +switch = off +volume = off + +.include analog-input.conf.common +.include analog-input-mic.conf.common diff --git a/src/modules/alsa/mixer/paths/analog-input-mic.conf.common b/src/modules/alsa/mixer/paths/analog-input-mic.conf.common new file mode 100644 index 00000000..b35e7af8 --- /dev/null +++ b/src/modules/alsa/mixer/paths/analog-input-mic.conf.common @@ -0,0 +1,63 @@ +# This file is part of PulseAudio. +# +# 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 +# along with PulseAudio; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + +; Common element for all microphone inputs +; +; See analog-output.conf.common for an explanation on the directives + +;;; 'Mic Select' + +[Element Mic Select] +enumeration = select + +[Option Mic Select:Mic1] +name = input-microphone +priority = 20 + +[Option Mic Select:Mic2] +name = input-microphone +priority = 19 + +;;; Various Boosts + +[Element Mic Boost (+20dB)] +switch = select +volume = merge + +[Option Mic Boost (+20dB):on] +name = input-boost-on + +[Option Mic Boost (+20dB):off] +name = input-boost-off + +[Element Mic Boost] +switch = select +volume = merge + +[Option Mic Boost:on] +name = input-boost-on + +[Option Mic Boost:off] +name = input-boost-off + +[Element Front Mic Boost] +switch = select + +[Option Front Mic Boost:on] +name = input-boost-on + +[Option Front Mic Boost:off] +name = input-boost-off diff --git a/src/modules/alsa/mixer/paths/analog-input-tvtuner.conf b/src/modules/alsa/mixer/paths/analog-input-tvtuner.conf new file mode 100644 index 00000000..8531ec70 --- /dev/null +++ b/src/modules/alsa/mixer/paths/analog-input-tvtuner.conf @@ -0,0 +1,62 @@ +# This file is part of PulseAudio. +# +# 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 +# along with PulseAudio; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + +; For devices where a 'TV Tuner' element exists +; +; See analog-output.conf.common for an explanation on the directives + +[General] +priority = 70 +name = analog-input-video + +[Element Capture] +switch = mute +volume = merge +override-map.1 = all +override-map.2 = all-left,all-right + +[Element Mic] +switch = off +volume = off + +[Element Line] +switch = off +volume = off + +[Element Aux] +switch = off +volume = off + +[Element Video] +switch = off +volume = off + +[Element Mic/Line] +switch = off +volume = off + +[Element TV Tuner] +required = any +switch = mute +volume = merge +override-map.1 = all +override-map.2 = all-left,all-right + +[Element FM] +switch = off +volume = off + +.include analog-input.conf.common diff --git a/src/modules/alsa/mixer/paths/analog-input-video.conf b/src/modules/alsa/mixer/paths/analog-input-video.conf new file mode 100644 index 00000000..74c76f07 --- /dev/null +++ b/src/modules/alsa/mixer/paths/analog-input-video.conf @@ -0,0 +1,61 @@ +# This file is part of PulseAudio. +# +# 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 +# along with PulseAudio; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + +; For devices where a 'Video' element exists +; +; See analog-output.conf.common for an explanation on the directives + +[General] +priority = 70 + +[Element Capture] +switch = mute +volume = merge +override-map.1 = all +override-map.2 = all-left,all-right + +[Element Mic] +switch = off +volume = off + +[Element Line] +switch = off +volume = off + +[Element Aux] +switch = off +volume = off + +[Element Video] +required = any +switch = mute +volume = merge +override-map.1 = all +override-map.2 = all-left,all-right + +[Element Mic/Line] +switch = off +volume = off + +[Element TV Tuner] +switch = off +volume = off + +[Element FM] +switch = off +volume = off + +.include analog-input.conf.common diff --git a/src/modules/alsa/mixer/paths/analog-input.conf b/src/modules/alsa/mixer/paths/analog-input.conf new file mode 100644 index 00000000..5055f90a --- /dev/null +++ b/src/modules/alsa/mixer/paths/analog-input.conf @@ -0,0 +1,54 @@ +# This file is part of PulseAudio. +# +# 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 +# along with PulseAudio; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + +; A fallback for devices that lack seperate Mic/Line/Aux/Video/TV +; Tuner/FM elements +; +; See analog-output.conf.common for an explanation on the directives + +[General] +priority = 100 + +[Element Capture] +required = volume +switch = mute +volume = merge +override-map.1 = all +override-map.2 = all-left,all-right + +[Element Mic] +required-absent = any + +[Element Line] +required-absent = any + +[Element Aux] +required-absent = any + +[Element Video] +required-absent = any + +[Element Mic/Line] +required-absent = any + +[Element TV Tuner] +required-absent = any + +[Element FM] +required-absent = any + +.include analog-input.conf.common +.include analog-input-mic.conf.common diff --git a/src/modules/alsa/mixer/paths/analog-input.conf.common b/src/modules/alsa/mixer/paths/analog-input.conf.common new file mode 100644 index 00000000..6728a6ae --- /dev/null +++ b/src/modules/alsa/mixer/paths/analog-input.conf.common @@ -0,0 +1,257 @@ +# This file is part of PulseAudio. +# +# 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 +# along with PulseAudio; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + +; Mixer path for PulseAudio's ALSA backend, common elements for all +; input paths. If multiple options by the same id are discovered they +; will be suffixed with a number to distuingish them, in the same +; order they appear here. +; +; Source selection should use the following names: +; +; input -- If we don't know the exact kind of input +; input-microphone +; input-microphone-internal +; input-microphone-external +; input-linein +; input-video +; input-radio +; input-docking-microphone +; input-docking-linein +; input-docking +; +; We explicitly don't want to wrap the following sources: +; +; CD +; Synth/MIDI +; Phone +; Mix +; Digital/SPDIF +; Master +; PC Speaker +; +; See analog-output.conf.common for an explanation on the directives + +;;; 'Input Source Select' + +[Element Input Source Select] +enumeration = select + +[Option Input Source Select:Input1] +name = input +priority = 10 + +[Option Input Source Select:Input2] +name = input +priority = 5 + +;;; 'Input Source' + +[Element Input Source] +enumeration = select + +[Option Input Source:Mic] +name = input-microphone +priority = 20 + +[Option Input Source:Microphone] +name = input-microphone +priority = 20 + +[Option Input Source:Front Mic] +name = input-microphone +priority = 19 + +[Option Input Source:Front Microphone] +name = input-microphone +priority = 19 + +[Option Input Source:Line] +name = input-linein +priority = 18 + +[Option Input Source:Line-In] +name = input-linein +priority = 18 + +[Option Input Source:Line In] +name = input-linein +priority = 18 + +;;; ' Capture Source' + +[Element Capture Source] +enumeration = select + +[Option Capture Source:TV Tuner] +name = input-video + +[Option Capture Source:FM] +name = input-radio + +[Option Capture Source:Mic/Line] +name = input + +[Option Capture Source:Line/Mic] +name = input + +[Option Capture Source:Mic] +name = input-microphone + +[Option Capture Source:Microphone] +name = input-microphone + +[Option Capture Source:Int Mic] +name = input-microphone-internal + +[Option Capture Source:Int DMic] +name = input-microphone-internal + +[Option Capture Source:Internal Mic] +name = input-microphone-internal + +[Option Capture Source:iMic] +name = input-microphone-internal + +[Option Capture Source:i-Mic] +name = input-microphone-internal + +[Option Capture Source:Internal Microphone] +name = input-microphone-internal + +[Option Capture Source:Front Mic] +name = input-microphone + +[Option Capture Source:Front Microphone] +name = input-microphone + +[Option Capture Source:Rear Mic] +name = input-microphone + +[Option Capture Source:Mic1] +name = input-microphone + +[Option Capture Source:Mic2] +name = input-microphone + +[Option Capture Source:D-Mic] +name = input-microphone + +[Option Capture Source:IntMic] +name = input-microphone-internal + +[Option Capture Source:ExtMic] +name = input-microphone-external + +[Option Capture Source:Ext Mic] +name = input-microphone-external + +[Option Capture Source:E-Mic] +name = input-microphone-external + +[Option Capture Source:e-Mic] +name = input-microphone-external + +[Option Capture Source:LineIn] +name = input-linein + +[Option Capture Source:Analog] +name = input + +[Option Capture Source:Line] +name = input-linein + +[Option Capture Source:Line-In] +name = input-linein + +[Option Capture Source:Line In] +name = input-linein + +[Option Capture Source:Video] +name = input-video + +[Option Capture Source:Aux] +name = input + +[Option Capture Source:Aux0] +name = input + +[Option Capture Source:Aux1] +name = input + +[Option Capture Source:Aux2] +name = input + +[Option Capture Source:Aux3] +name = input + +[Option Capture Source:AUX IN] +name = input + +[Option Capture Source:Aux In] +name = input + +[Option Capture Source:AOUT] +name = input + +[Option Capture Source:AUX] +name = input + +[Option Capture Source:Cam Mic] +name = input-microphone + +[Option Capture Source:Digital Mic] +name = input-microphone + +[Option Capture Source:Digital Mic 1] +name = input-microphone + +[Option Capture Source:Digital Mic 2] +name = input-microphone + +[Option Capture Source:Analog Inputs] +name = input + +[Option Capture Source:Unknown1] +name = input + +[Option Capture Source:Unknown2] +name = input + +[Option Capture Source:Docking-Station] +name = input-docking + +[Option Capture Source:Dock Mic] +name = input-docking-microphone + +;;; Various Boosts + +[Element Capture Boost] +switch = select + +[Option Capture Boost:on] +name = input-boost-on + +[Option Capture Boost:off] +name = input-boost-off + +[Element Auto Gain Control] +switch = select + +[Option Auto Gain Control:on] +name = input-agc-on + +[Option Auto Gain Control:off] +name = input-agc-off diff --git a/src/modules/alsa/mixer/paths/analog-output-headphones.conf b/src/modules/alsa/mixer/paths/analog-output-headphones.conf new file mode 100644 index 00000000..c018e0eb --- /dev/null +++ b/src/modules/alsa/mixer/paths/analog-output-headphones.conf @@ -0,0 +1,71 @@ +# This file is part of PulseAudio. +# +# 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 +# along with PulseAudio; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + +; Path for mixers that have a 'Headphone' control +; +; See analog-output.conf.common for an explanation on the directives + +[General] +priority = 90 + +[Element Hardware Master] +switch = mute +volume = merge +override-map.1 = all +override-map.2 = all-left,all-right + +[Element Master] +switch = mute +volume = merge +override-map.1 = all +override-map.2 = all-left,all-right + +[Element Master Mono] +switch = off +volume = off + +[Element Headphone] +required = any +switch = mute +volume = merge +override-map.1 = all +override-map.2 = all-left,all-right + +[Element Front] +switch = off +volume = off + +[Element Rear] +switch = off +volume = off + +[Element Sourround] +switch = off +volume = off + +[Element Side] +switch = off +volume = off + +[Element Center] +switch = off +volume = off + +[Element LFE] +switch = off +volume = off + +.include analog-output.conf.common diff --git a/src/modules/alsa/mixer/paths/analog-output-lfe-on-mono.conf b/src/modules/alsa/mixer/paths/analog-output-lfe-on-mono.conf new file mode 100644 index 00000000..7a267890 --- /dev/null +++ b/src/modules/alsa/mixer/paths/analog-output-lfe-on-mono.conf @@ -0,0 +1,72 @@ +# This file is part of PulseAudio. +# +# 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 +# along with PulseAudio; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + +; Intended for usage in laptops that have a seperate LFE speaker +; connected to the Master mono connector +; +; See analog-output.conf.common for an explanation on the directives + +[General] +priority = 40 + +[Element Hardware Master] +switch = mute +volume = merge +override-map.1 = all +override-map.2 = all-left,all-right + +[Element Master] +switch = mute +volume = merge +override-map.1 = all-no-lfe +override-map.2 = all-left,all-right + +[Element Master Mono] +required = any +switch = mute +volume = merge +override-map.1 = lfe +override-map.2 = lfe,lfe + +[Element Headphone] +switch = off +volume = off + +[Element Front] +switch = off +volume = off + +[Element Rear] +switch = off +volume = off + +[Element Sourround] +switch = off +volume = off + +[Element Side] +switch = off +volume = off + +[Element Center] +switch = off +volume = off + +[Element LFE] +switch = off +volume = off + +.include analog-output.conf.common diff --git a/src/modules/alsa/mixer/paths/analog-output-mono.conf b/src/modules/alsa/mixer/paths/analog-output-mono.conf new file mode 100644 index 00000000..f6cb9f8a --- /dev/null +++ b/src/modules/alsa/mixer/paths/analog-output-mono.conf @@ -0,0 +1,69 @@ +# This file is part of PulseAudio. +# +# 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 +# along with PulseAudio; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + +; Intended for usage on boards that have a seperate Mono output plug. +; +; See analog-output.conf.common for an explanation on the directives + +[General] +priority = 50 + +[Element Hardware Master] +switch = mute +volume = merge +override-map.1 = all +override-map.2 = all-left,all-right + +[Element Master] +switch = off +volume = off + +[Element Master Mono] +required = any +switch = mute +volume = merge +override-map.1 = all +override-map.2 = all-left,all-right + +[Element Headphone] +switch = off +volume = off + +[Element Front] +switch = off +volume = off + +[Element Rear] +switch = off +volume = off + +[Element Sourround] +switch = off +volume = off + +[Element Side] +switch = off +volume = off + +[Element Center] +switch = off +volume = off + +[Element LFE] +switch = off +volume = off + +.include analog-output.conf.common diff --git a/src/modules/alsa/mixer/paths/analog-output.conf b/src/modules/alsa/mixer/paths/analog-output.conf new file mode 100644 index 00000000..ea108aaf --- /dev/null +++ b/src/modules/alsa/mixer/paths/analog-output.conf @@ -0,0 +1,80 @@ +# This file is part of PulseAudio. +# +# 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 +# along with PulseAudio; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + +; Intended for the 'default' output +; +; See analog-output.conf.common for an explanation on the directives + +[General] +priority = 100 + +[Element Hardware Master] +switch = mute +volume = merge +override-map.1 = all +override-map.2 = all-left,all-right + +[Element Master] +switch = mute +volume = merge +override-map.1 = all +override-map.2 = all-left,all-right + +[Element Master Mono] +switch = off +volume = off + +[Element Headphone] +switch = off +volume = off + +[Element Front] +switch = mute +volume = merge +override-map.1 = all-front +override-map.2 = front-left,front-right + +[Element Rear] +switch = mute +volume = merge +override-map.1 = all-rear +override-map.2 = rear-left,rear-right + +[Element Surround] +switch = mute +volume = merge +override-map.1 = all-rear +override-map.2 = rear-left,rear-right + +[Element Side] +switch = mute +volume = merge +override-map.1 = all-side +override-map.2 = side-left,side-right + +[Element Center] +switch = mute +volume = merge +override-map.1 = all-center +override-map.2 = all-center,all-center + +[Element LFE] +switch = mute +volume = merge +override-map.1 = lfe +override-map.2 = lfe,lfe + +.include analog-output.conf.common diff --git a/src/modules/alsa/mixer/paths/analog-output.conf.common b/src/modules/alsa/mixer/paths/analog-output.conf.common new file mode 100644 index 00000000..cc1185f4 --- /dev/null +++ b/src/modules/alsa/mixer/paths/analog-output.conf.common @@ -0,0 +1,111 @@ +# This file is part of PulseAudio. +# +# 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 +# along with PulseAudio; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + +; Common part of all paths + +; So here's generally how mixer paths are used by PA: PA goes through +; a mixer path file from top to bottom and checks if a mixer element +; described therein exists. If so it is added to the list of mixer +; elements PA will control, keeping the order it read them in. If a +; mixer element described here has set the required= or +; required-absent= directives a path might not be accepted as valid +; and is ignored in its entirety (see below). However usually if a +; element listed here is missing this one element is ignored but not +; the entire path. +; +; When a device shall be muted/unmuted *all* elements listed in a path +; file with "switch = mute" will be toggled. +; +; When a device shall change its volume, PA will got through the list +; of all elements with "volume = merge" and set the volume on the +; first element. If that element does not support dB volumes, this is +; where the story ends. If it does support dB volumes, PA divides the +; requested volume by the volume that was set on this element, and +; then go on to the next element with "volume = merge" and then set +; that there, and so on. That way the first volume element in the +; path will be the one that does the 'biggest' part of the overall +; volume adjustment, with the remaining elements usually being set to +; some value next to 0dB. This logic makes sure we get the full range +; over all volume sliders and a very high granularity of volumes +; already in hardware. +; +; All switches and enumerations set to "select" are exposed via the +; "port" functionality of sinks/sources. Basically every possible +; switch setting and every possible enumeration setting will be +; combined and made into a "port". So make sure you don't list too +; many switches/enums for exposing, because the number of ports might +; rise exponentially. +; +; Only one path can be selected at a time. All paths that are valid +; for an audio device will be exposed as "port" for the sink/source. + + +; [General] +; priority = ... # Priority for this path +; description = ... +; +; [Option ...:...] # For each option of an enumeration or switch element +; # that shall be exposed as a sink/source port. Needs to +; # be named after the Element, followed by a colon, followed +; # by the option name, resp. on/off if the element is a switch. +; name = ... # Logical name to use in the path identifier +; priority = ... # Priority if this is made into a device port +; +; [Element ...] # For each element that we shall control +; required = ignore | switch | volume | enumeration | any # If set, require this element to be of this kind and available, +; # otherwise don't consider this path valid for the card +; required-absent = ignore | switch | volume # If set, require this element to not be of this kind and not +; # available, otherwise don't consider this path valid for the card +; +; switch = ignore | mute | off | on | select # What to do with this switch: ignore it, make it follow mute status, +; # always set it to off, always to on, or make it selectable as port. +; # If set to 'select' you need to define an Option section for on +; # and off +; volume = ignore | merge | off | zero # What to do with this volume: ignore it, merge it into the device +; # volume slider, always set it to the lowest value possible, or always +; # set it to 0 dB (for whatever that means) +; enumeration = ignore | select # What to do with this enumeration, ignore it or make it selectable +; # via device ports. If set to 'select' you need to define an Option section +; # for each of the items you want to expose +; direction = playback | capture # Is this relevant only for playback or capture? If not set this will implicitly be +; # set the direction of the PCM device is opened as. Generally this doesn't need to be set +; # unless you have a broken driver that has playback controls marked for capture or vice +; # versa +; direction-try-other = no | yes # If the element does not supported what is requested, try the other direction, too? +; +; override-map.1 = ... # Override the channel mask of the mixer control if the control only exposes a single channel +; override-map.2 = ... # Override the channel masks of the mixer control if the control only exposes two channels +; # Override maps should list for each element channel which high-level channels it controls via a +; # channel mask. A channel mask may either be the name of a single channel, or the words "all-left", +; # "all-right", "all-center", "all-front", "all-rear", and "all" to encode a specific subset of +; # channels in a mask + +[Element PCM] +switch = mute +volume = merge +override-map.1 = all +override-map.2 = all-left,all-right + +[Element External Amplifier] +switch = select + +[Option External Amplifier:on] +name = output-amplifier-on +priority = 0 + +[Option External Amplifier:off] +name = output-amplifier-off +priority = 10 diff --git a/src/modules/alsa/mixer/profile-sets/90-pulseaudio.rules b/src/modules/alsa/mixer/profile-sets/90-pulseaudio.rules new file mode 100644 index 00000000..ea1a2fed --- /dev/null +++ b/src/modules/alsa/mixer/profile-sets/90-pulseaudio.rules @@ -0,0 +1,26 @@ +# do not edit this file, it will be overwritten on update + +# This file is part of PulseAudio. +# +# 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 +# along with PulseAudio; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + +SUBSYSTEM!="sound", GOTO="pulseaudio_end" +ACTION!="change", GOTO="pulseaudio_end" +KERNEL!="card*", GOTO="pulseaudio_end" + +SUBSYSTEMS=="usb", ATTRS{idVendor}=="17cc", ATTRS{idProduct}=="1978", ENV{PULSE_PROFILE_SET}="native-instruments-audio8dj.conf" +SUBSYSTEMS=="usb", ATTRS{idVendor}=="17cc", ATTRS{idProduct}=="0839", ENV{PULSE_PROFILE_SET}="native-instruments-audio4dj.conf" + +LABEL="pulseaudio_end" diff --git a/src/modules/alsa/mixer/profile-sets/default.conf b/src/modules/alsa/mixer/profile-sets/default.conf new file mode 100644 index 00000000..ac41a8d3 --- /dev/null +++ b/src/modules/alsa/mixer/profile-sets/default.conf @@ -0,0 +1,144 @@ +# This file is part of PulseAudio. +# +# 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 +# along with PulseAudio; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + +; Default profile definitions for the ALSA backend of PulseAudio. This +; is used as fallback for all cards that have no special mapping +; assigned. (and should be good enough for the vast majority of +; cards). Use the udev property PULSE_PROFILE_SET to assign a +; different profile set than this one to a device. So what is this +; about? Simply, what we do here is map ALSA devices to how they are +; exposed in PA. We say which ALSA device string to use to open a +; device, which channel mapping to use then, and which mixer path to +; use. This is encoded in a 'mapping'. Multiple of these mappings can +; be bound together in a 'profile' which is then directly exposed in +; the UI as a card profile. Each mapping assigned to a profile will +; result in one sink/source to be created if the profile is selected +; for the card. + +; [General] +; auto-profiles = no | yes # Instead of defining all profiles manually, autogenerate +; # them by combining every input mapping with every output mapping. +; +; [Mapping id] +; device-strings = ... # ALSA device string. %f will be replaced by the card identifier. +; channel-map = ... # Channel mapping to use for this device +; description = ... +; paths-input = ... # A list of mixer paths to use. Every path in this list will be probed. +; # If multiple are found to be working they will be available as device ports +; paths-output = ... +; element-input = ... # Instead of configuring a full mixer path simply configure a single +; # mixer element for volume/mute handling +; element-output = ... +; priority = ... +; direction = any | input | output # Only useful for? +; +; [Profile id] +; input-mappings = ... # Lists mappings for sources on this profile, those mapping must be +; # defined in this file too +; output-mappings = ... # Lists mappings for sinks on this profile, those mappings must be +; # defined in this file too +; description = ... +; priority = ... # Numeric value to deduce priority for this profile +; skip-probe = no | yes # Skip probing for availability? If this is yes then this profile +; # will be assumed as working without probing. Makes initialization +; # a bit faster but only works if the card is really known well. + +[General] +auto-profiles = yes + +[Mapping analog-mono] +device-strings = hw:%f +channel-map = mono +paths-output = analog-output analog-output-headphones analog-output-mono analog-output-lfe-on-mono +paths-input = analog-input analog-input-mic analog-input-linein analog-input-aux analog-input-video analog-input-tvtuner analog-input-fm analog-input-mic-line +priority = 1 + +[Mapping analog-stereo] +device-strings = front:%f hw:%f +channel-map = left,right +paths-output = analog-output analog-output-headphones analog-output-mono analog-output-lfe-on-mono +paths-input = analog-input analog-input-mic analog-input-linein analog-input-aux analog-input-video analog-input-tvtuner analog-input-fm analog-input-mic-line +priority = 10 + +[Mapping analog-surround-40] +device-strings = surround40:%f +channel-map = front-left,front-right,rear-left,rear-right +paths-output = analog-output analog-output-lfe-on-mono +priority = 7 +direction = output + +[Mapping analog-surround-41] +device-strings = surround41:%f +channel-map = front-left,front-right,rear-left,rear-right,lfe +paths-output = analog-output analog-output-lfe-on-mono +priority = 8 +direction = output + +[Mapping analog-surround-50] +device-strings = surround50:%f +channel-map = front-left,front-right,rear-left,rear-right,front-center +paths-output = analog-output analog-output-lfe-on-mono +priority = 7 +direction = output + +[Mapping analog-surround-51] +device-strings = surround51:%f +channel-map = front-left,front-right,rear-left,rear-right,front-center,lfe +paths-output = analog-output analog-output-lfe-on-mono +priority = 8 +direction = output + +[Mapping analog-surround-71] +device-strings = surround71:%f +channel-map = front-left,front-right,rear-left,rear-right,front-center,lfe,side-left,side-right +description = Analog Surround 7.1 +paths-output = analog-output analog-output-lfe-on-mono +priority = 7 +direction = output + +[Mapping iec958-stereo] +device-strings = iec958:%f +channel-map = left,right +priority = 5 + +[Mapping iec958-surround-40] +device-strings = iec958:%f +channel-map = front-left,front-right,rear-left,rear-right +priority = 1 + +[Mapping iec958-ac3-surround-40] +device-strings = a52:%f +channel-map = front-left,front-right,rear-left,rear-right +priority = 2 +direction = output + +[Mapping iec958-ac3-surround-51] +device-strings = a52:%f +channel-map = front-left,front-right,rear-left,rear-right,front-center,lfe +priority = 3 +direction = output + +[Mapping hdmi-stereo] +device-strings = hdmi:%f +channel-map = left,right +priority = 4 +direction = output + +; An example for defining multiple-sink profiles +#[Profile output:analog-stereo+output:iec958-stereo+input:analog-stereo] +#description = Foobar +#output-mappings = analog-stereo iec958-stereo +#input-mappings = analog-stereo diff --git a/src/modules/alsa/mixer/profile-sets/native-instruments-audio4dj.conf b/src/modules/alsa/mixer/profile-sets/native-instruments-audio4dj.conf new file mode 100644 index 00000000..2b835308 --- /dev/null +++ b/src/modules/alsa/mixer/profile-sets/native-instruments-audio4dj.conf @@ -0,0 +1,91 @@ +# This file is part of PulseAudio. +# +# 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 +# along with PulseAudio; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + +; Native Instruments Audio 4 DJ +; +; This card has two stereo pairs of input and two stereo pairs of +; output, named channels A and B. Channel B has an additional +; Headphone connector. +; +; We knowingly only define a subset of the theoretically possible +; mapping combinations as profiles here. +; +; See default.conf for an explanation on the directives used here. + +[General] +auto-profiles = no + +[Mapping analog-stereo-a] +description = Analog Stereo Channel A +device-strings = hw:%f,0,0 +channel-map = left,right + +[Mapping analog-stereo-b-output] +description = Analog Stereo Channel B (Headphones) +device-strings = hw:%f,0,1 +channel-map = left,right +direction = output + +[Mapping analog-stereo-b-input] +description = Analog Stereo Channel B +device-strings = hw:%f,0,1 +channel-map = left,right +direction = input + +[Profile output:analog-stereo-all+input:analog-stereo-all] +description = Analog Stereo Duplex Channels A, B (Headphones) +output-mappings = analog-stereo-a analog-stereo-b-output +input-mappings = analog-stereo-a analog-stereo-b-input +priority = 100 +skip-probe = yes + +[Profile output:analog-stereo-a+input:analog-stereo-a] +description = Analog Stereo Duplex Channel A +output-mappings = analog-stereo-a +input-mappings = analog-stereo-a +priority = 40 +skip-probe = yes + +[Profile output:analog-stereo-b+input:analog-stereo-b] +description = Analog Stereo Duplex Channel B (Headphones) +output-mappings = analog-stereo-b-output +input-mappings = analog-stereo-b-input +priority = 50 +skip-probe = yes + +[Profile output:analog-stereo-a] +description = Analog Stereo Output Channel A +output-mappings = analog-stereo-a +priority = 5 +skip-probe = yes + +[Profile output:analog-stereo-b] +description = Analog Stereo Output Channel B (Headphones) +output-mappings = analog-stereo-b-output +priority = 6 +skip-probe = yes + +[Profile input:analog-stereo-a] +description = Analog Stereo Input Channel A +input-mappings = analog-stereo-a +priority = 2 +skip-probe = yes + +[Profile input:analog-stereo-b] +description = Analog Stereo Input Channel B +input-mappings = analog-stereo-b-input +priority = 1 +skip-probe = yes diff --git a/src/modules/alsa/mixer/profile-sets/native-instruments-audio8dj.conf b/src/modules/alsa/mixer/profile-sets/native-instruments-audio8dj.conf new file mode 100644 index 00000000..3fe3cc56 --- /dev/null +++ b/src/modules/alsa/mixer/profile-sets/native-instruments-audio8dj.conf @@ -0,0 +1,162 @@ +# This file is part of PulseAudio. +# +# 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 +# along with PulseAudio; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + +; Native Instruments Audio 8 DJ +; +; This card has four stereo pairs of input and four stereo pairs of +; output, named channels A to D. Channel C has an additional Mic/Line +; connector, channel D an additional Headphone connector. +; +; We knowingly only define a subset of the theoretically possible +; mapping combinations as profiles here. +; +; See default.conf for an explanation on the directives used here. + +[General] +auto-profiles = no + +[Mapping analog-stereo-a] +description = Analog Stereo Channel A +device-strings = hw:%f,0,0 +channel-map = left,right + +[Mapping analog-stereo-b] +description = Analog Stereo Channel B +device-strings = hw:%f,0,1 +channel-map = left,right + +# Since we want to set a different description for channel C's/D's input +# and output we define two seperate mappings for them +[Mapping analog-stereo-c-output] +description = Analog Stereo Channel C +device-strings = hw:%f,0,2 +channel-map = left,right +direction = output + +[Mapping analog-stereo-c-input] +description = Analog Stereo Channel C (Line/Mic) +device-strings = hw:%f,0,2 +channel-map = left,right +direction = input + +[Mapping analog-stereo-d-output] +description = Analog Stereo Channel D (Headphones) +device-strings = hw:%f,0,3 +channel-map = left,right +direction = output + +[Mapping analog-stereo-d-input] +description = Analog Stereo Channel D +device-strings = hw:%f,0,3 +channel-map = left,right +direction = input + +[Profile output:analog-stereo-all+input:analog-stereo-all] +description = Analog Stereo Duplex Channels A, B, C (Line/Mic), D (Headphones) +output-mappings = analog-stereo-a analog-stereo-b analog-stereo-c-output analog-stereo-d-output +input-mappings = analog-stereo-a analog-stereo-b analog-stereo-c-input analog-stereo-d-input +priority = 100 +skip-probe = yes + +[Profile output:analog-stereo-d+input:analog-stereo-c] +description = Analog Stereo Channel D (Headphones) Output, Channel C (Line/Mic) Input +output-mappings = analog-stereo-d-output +input-mappings = analog-stereo-c-input +priority = 90 +skip-probe = yes + +[Profile output:analog-stereo-c-d+input:analog-stereo-c-d] +description = Analog Stereo Duplex Channels C (Line/Mic), D (Line/Mic) +output-mappings = analog-stereo-c-output analog-stereo-d-output +input-mappings = analog-stereo-c-input analog-stereo-d-input +priority = 80 +skip-probe = yes + +[Profile output:analog-stereo-a+input:analog-stereo-a] +description = Analog Stereo Duplex Channel A +output-mappings = analog-stereo-a +input-mappings = analog-stereo-a +priority = 50 +skip-probe = yes + +[Profile output:analog-stereo-b+input:analog-stereo-b] +description = Analog Stereo Duplex Channel B +output-mappings = analog-stereo-b +input-mappings = analog-stereo-b +priority = 40 +skip-probe = yes + +[Profile output:analog-stereo-c+input:analog-stereo-c] +description = Analog Stereo Duplex Channel C (Line/Mic) +output-mappings = analog-stereo-c-output +input-mappings = analog-stereo-c-input +priority = 60 +skip-probe = yes + +[Profile output:analog-stereo-d+input:analog-stereo-d] +description = Analog Stereo Duplex Channel D (Headphones) +output-mappings = analog-stereo-d-output +input-mappings = analog-stereo-d-input +priority = 70 +skip-probe = yes + +[Profile output:analog-stereo-a] +description = Analog Stereo Output Channel A +output-mappings = analog-stereo-a +priority = 6 +skip-probe = yes + +[Profile output:analog-stereo-b] +description = Analog Stereo Output Channel B +output-mappings = analog-stereo-b +priority = 5 +skip-probe = yes + +[Profile output:analog-stereo-c] +description = Analog Stereo Output Channel C +output-mappings = analog-stereo-c-output +priority = 7 +skip-probe = yes + +[Profile output:analog-stereo-d] +description = Analog Stereo Output Channel D (Headphones) +output-mappings = analog-stereo-d-output +priority = 8 +skip-probe = yes + +[Profile input:analog-stereo-a] +description = Analog Stereo Input Channel A +input-mappings = analog-stereo-a +priority = 2 +skip-probe = yes + +[Profile input:analog-stereo-b] +description = Analog Stereo Input Channel B +input-mappings = analog-stereo-b +priority = 1 +skip-probe = yes + +[Profile input:analog-stereo-c] +description = Analog Stereo Input Channel C (Line/Mic) +input-mappings = analog-stereo-c-input +priority = 4 +skip-probe = yes + +[Profile input:analog-stereo-d] +description = Analog Stereo Input Channel D +input-mappings = analog-stereo-d-input +priority = 3 +skip-probe = yes diff --git a/src/modules/alsa/mixer/samples/ATI IXP--Realtek ALC655 rev 0 b/src/modules/alsa/mixer/samples/ATI IXP--Realtek ALC655 rev 0 new file mode 100644 index 00000000..082c9a1b --- /dev/null +++ b/src/modules/alsa/mixer/samples/ATI IXP--Realtek ALC655 rev 0 @@ -0,0 +1,150 @@ +Simple mixer control 'Master',0 + Capabilities: pvolume pswitch pswitch-joined + Playback channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: + Front Left: Playback 29 [94%] [-3.00dB] [on] + Front Right: Playback 29 [94%] [-3.00dB] [on] +Simple mixer control 'Master Mono',0 + Capabilities: pvolume pvolume-joined pswitch pswitch-joined + Playback channels: Mono + Limits: Playback 0 - 31 + Mono: Playback 0 [0%] [-46.50dB] [off] +Simple mixer control 'PCM',0 + Capabilities: pvolume pswitch pswitch-joined + Playback channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: + Front Left: Playback 23 [74%] [0.00dB] [on] + Front Right: Playback 23 [74%] [0.00dB] [on] +Simple mixer control 'Surround',0 + Capabilities: pvolume pswitch + Playback channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: + Front Left: Playback 0 [0%] [-46.50dB] [off] + Front Right: Playback 0 [0%] [-46.50dB] [off] +Simple mixer control 'Surround Jack Mode',0 + Capabilities: enum + Items: 'Shared' 'Independent' + Item0: 'Shared' +Simple mixer control 'Center',0 + Capabilities: pvolume pvolume-joined pswitch pswitch-joined + Playback channels: Mono + Limits: Playback 0 - 31 + Mono: Playback 0 [0%] [-46.50dB] [off] +Simple mixer control 'LFE',0 + Capabilities: pvolume pvolume-joined pswitch pswitch-joined + Playback channels: Mono + Limits: Playback 0 - 31 + Mono: Playback 0 [0%] [-46.50dB] [off] +Simple mixer control 'Line',0 + Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Front Left - Front Right + Capture channels: Front Left - Front Right + Limits: Playback 0 - 31 + Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off] + Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off] +Simple mixer control 'CD',0 + Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Front Left - Front Right + Capture channels: Front Left - Front Right + Limits: Playback 0 - 31 + Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off] + Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off] +Simple mixer control 'Mic',0 + Capabilities: pvolume pvolume-joined pswitch pswitch-joined cswitch cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Mono + Capture channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: Playback 0 [0%] [-34.50dB] [off] + Front Left: Capture [on] + Front Right: Capture [on] +Simple mixer control 'Mic Boost (+20dB)',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [off] +Simple mixer control 'Mic Select',0 + Capabilities: enum + Items: 'Mic1' 'Mic2' + Item0: 'Mic1' +Simple mixer control 'Video',0 + Capabilities: cswitch cswitch-exclusive + Capture exclusive group: 0 + Capture channels: Front Left - Front Right + Front Left: Capture [off] + Front Right: Capture [off] +Simple mixer control 'Phone',0 + Capabilities: pvolume pvolume-joined pswitch pswitch-joined cswitch cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Mono + Capture channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: Playback 31 [100%] [12.00dB] [off] + Front Left: Capture [off] + Front Right: Capture [off] +Simple mixer control 'IEC958',0 + Capabilities: pswitch pswitch-joined cswitch cswitch-joined + Playback channels: Mono + Capture channels: Mono + Mono: Playback [off] Capture [off] +Simple mixer control 'IEC958 Playback AC97-SPSA',0 + Capabilities: volume volume-joined + Playback channels: Mono + Capture channels: Mono + Limits: 0 - 3 + Mono: 0 [0%] +Simple mixer control 'IEC958 Playback Source',0 + Capabilities: enum + Items: 'PCM' 'Analog In' 'IEC958 In' + Item0: 'PCM' +Simple mixer control 'PC Speaker',0 + Capabilities: pvolume pvolume-joined pswitch pswitch-joined + Playback channels: Mono + Limits: Playback 0 - 15 + Mono: Playback 0 [0%] [-45.00dB] [on] +Simple mixer control 'Aux',0 + Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Front Left - Front Right + Capture channels: Front Left - Front Right + Limits: Playback 0 - 31 + Front Left: Playback 0 [0%] [-34.50dB] [on] Capture [off] + Front Right: Playback 0 [0%] [-34.50dB] [on] Capture [off] +Simple mixer control 'Mono Output Select',0 + Capabilities: enum + Items: 'Mix' 'Mic' + Item0: 'Mix' +Simple mixer control 'Capture',0 + Capabilities: cvolume cswitch cswitch-joined + Capture channels: Front Left - Front Right + Limits: Capture 0 - 15 + Front Left: Capture 12 [80%] [18.00dB] [on] + Front Right: Capture 12 [80%] [18.00dB] [on] +Simple mixer control 'Mix',0 + Capabilities: cswitch cswitch-exclusive + Capture exclusive group: 0 + Capture channels: Front Left - Front Right + Front Left: Capture [off] + Front Right: Capture [off] +Simple mixer control 'Mix Mono',0 + Capabilities: cswitch cswitch-exclusive + Capture exclusive group: 0 + Capture channels: Front Left - Front Right + Front Left: Capture [off] + Front Right: Capture [off] +Simple mixer control 'Channel Mode',0 + Capabilities: enum + Items: '2ch' '4ch' '6ch' + Item0: '2ch' +Simple mixer control 'Duplicate Front',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [off] +Simple mixer control 'External Amplifier',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [on] diff --git a/src/modules/alsa/mixer/samples/Brooktree Bt878--Bt87x b/src/modules/alsa/mixer/samples/Brooktree Bt878--Bt87x new file mode 100644 index 00000000..b8f61fab --- /dev/null +++ b/src/modules/alsa/mixer/samples/Brooktree Bt878--Bt87x @@ -0,0 +1,24 @@ +Simple mixer control 'FM',0 + Capabilities: cswitch cswitch-joined cswitch-exclusive + Capture exclusive group: 0 + Capture channels: Mono + Mono: Capture [off] +Simple mixer control 'Mic/Line',0 + Capabilities: cswitch cswitch-joined cswitch-exclusive + Capture exclusive group: 0 + Capture channels: Mono + Mono: Capture [off] +Simple mixer control 'Capture',0 + Capabilities: cvolume cvolume-joined + Capture channels: Mono + Limits: Capture 0 - 15 + Mono: Capture 13 [87%] +Simple mixer control 'Capture Boost',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [on] +Simple mixer control 'TV Tuner',0 + Capabilities: cswitch cswitch-joined cswitch-exclusive + Capture exclusive group: 0 + Capture channels: Mono + Mono: Capture [on] diff --git a/src/modules/alsa/mixer/samples/Ensoniq AudioPCI--Cirrus Logic CS4297A rev 3 b/src/modules/alsa/mixer/samples/Ensoniq AudioPCI--Cirrus Logic CS4297A rev 3 new file mode 100644 index 00000000..a500a817 --- /dev/null +++ b/src/modules/alsa/mixer/samples/Ensoniq AudioPCI--Cirrus Logic CS4297A rev 3 @@ -0,0 +1,135 @@ +Simple mixer control 'Master',0 + Capabilities: pvolume pswitch pswitch-joined + Playback channels: Front Left - Front Right + Limits: Playback 0 - 63 + Mono: + Front Left: Playback 63 [100%] [0.00dB] [on] + Front Right: Playback 63 [100%] [0.00dB] [on] +Simple mixer control 'Master Mono',0 + Capabilities: pvolume pvolume-joined pswitch pswitch-joined + Playback channels: Mono + Limits: Playback 0 - 31 + Mono: Playback 0 [0%] [-46.50dB] [off] +Simple mixer control 'Headphone',0 + Capabilities: pvolume pswitch pswitch-joined + Playback channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: + Front Left: Playback 0 [0%] [-46.50dB] [off] + Front Right: Playback 0 [0%] [-46.50dB] [off] +Simple mixer control '3D Control - Center',0 + Capabilities: volume volume-joined + Playback channels: Mono + Capture channels: Mono + Limits: 0 - 15 + Mono: 0 [0%] +Simple mixer control '3D Control - Depth',0 + Capabilities: volume volume-joined + Playback channels: Mono + Capture channels: Mono + Limits: 0 - 15 + Mono: 0 [0%] +Simple mixer control '3D Control - Switch',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [off] +Simple mixer control 'PCM',0 + Capabilities: pvolume pswitch pswitch-joined + Playback channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: + Front Left: Playback 23 [74%] [0.00dB] [on] + Front Right: Playback 23 [74%] [0.00dB] [on] +Simple mixer control 'Line',0 + Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Front Left - Front Right + Capture channels: Front Left - Front Right + Limits: Playback 0 - 31 + Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [on] + Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [on] +Simple mixer control 'CD',0 + Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Front Left - Front Right + Capture channels: Front Left - Front Right + Limits: Playback 0 - 31 + Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off] + Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off] +Simple mixer control 'Mic',0 + Capabilities: pvolume pvolume-joined pswitch pswitch-joined cswitch cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Mono + Capture channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: Playback 23 [74%] [0.00dB] [on] + Front Left: Capture [off] + Front Right: Capture [off] +Simple mixer control 'Mic Boost (+20dB)',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [off] +Simple mixer control 'Mic Select',0 + Capabilities: enum + Items: 'Mic1' 'Mic2' + Item0: 'Mic1' +Simple mixer control 'Video',0 + Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Front Left - Front Right + Capture channels: Front Left - Front Right + Limits: Playback 0 - 31 + Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off] + Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off] +Simple mixer control 'Phone',0 + Capabilities: pvolume pvolume-joined pswitch pswitch-joined cswitch cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Mono + Capture channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: Playback 0 [0%] [-34.50dB] [off] + Front Left: Capture [off] + Front Right: Capture [off] +Simple mixer control 'IEC958',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [off] +Simple mixer control 'PC Speaker',0 + Capabilities: pvolume pvolume-joined pswitch pswitch-joined + Playback channels: Mono + Limits: Playback 0 - 15 + Mono: Playback 0 [0%] [-45.00dB] [off] +Simple mixer control 'Aux',0 + Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Front Left - Front Right + Capture channels: Front Left - Front Right + Limits: Playback 0 - 31 + Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off] + Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off] +Simple mixer control 'Mono Output Select',0 + Capabilities: enum + Items: 'Mix' 'Mic' + Item0: 'Mic' +Simple mixer control 'Capture',0 + Capabilities: cvolume cswitch cswitch-joined + Capture channels: Front Left - Front Right + Limits: Capture 0 - 15 + Front Left: Capture 15 [100%] [22.50dB] [on] + Front Right: Capture 15 [100%] [22.50dB] [on] +Simple mixer control 'Mix',0 + Capabilities: cswitch cswitch-exclusive + Capture exclusive group: 0 + Capture channels: Front Left - Front Right + Front Left: Capture [off] + Front Right: Capture [off] +Simple mixer control 'Mix Mono',0 + Capabilities: cswitch cswitch-exclusive + Capture exclusive group: 0 + Capture channels: Front Left - Front Right + Front Left: Capture [off] + Front Right: Capture [off] +Simple mixer control 'External Amplifier',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [off] diff --git a/src/modules/alsa/mixer/samples/HDA ATI HDMI--ATI R6xx HDMI b/src/modules/alsa/mixer/samples/HDA ATI HDMI--ATI R6xx HDMI new file mode 100644 index 00000000..244f24a8 --- /dev/null +++ b/src/modules/alsa/mixer/samples/HDA ATI HDMI--ATI R6xx HDMI @@ -0,0 +1,4 @@ +Simple mixer control 'IEC958',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [on] diff --git a/src/modules/alsa/mixer/samples/HDA Intel--Analog Devices AD1981 b/src/modules/alsa/mixer/samples/HDA Intel--Analog Devices AD1981 new file mode 100644 index 00000000..165522fa --- /dev/null +++ b/src/modules/alsa/mixer/samples/HDA Intel--Analog Devices AD1981 @@ -0,0 +1,62 @@ +Simple mixer control 'Master',0 + Capabilities: pvolume pswitch + Playback channels: Front Left - Front Right + Limits: Playback 0 - 63 + Mono: + Front Left: Playback 63 [100%] [3.00dB] [on] + Front Right: Playback 63 [100%] [3.00dB] [on] +Simple mixer control 'PCM',0 + Capabilities: pvolume pswitch + Playback channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: + Front Left: Playback 23 [74%] [0.00dB] [on] + Front Right: Playback 23 [74%] [0.00dB] [on] +Simple mixer control 'CD',0 + Capabilities: pvolume pswitch cswitch cswitch-joined cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Front Left - Front Right + Capture channels: Mono + Limits: Playback 0 - 31 + Mono: Capture [off] + Front Left: Playback 0 [0%] [-34.50dB] [off] + Front Right: Playback 0 [0%] [-34.50dB] [off] +Simple mixer control 'Mic',0 + Capabilities: pvolume pswitch cswitch cswitch-joined cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Front Left - Front Right + Capture channels: Mono + Limits: Playback 0 - 31 + Mono: Capture [on] + Front Left: Playback 0 [0%] [-34.50dB] [off] + Front Right: Playback 0 [0%] [-34.50dB] [off] +Simple mixer control 'Mic Boost',0 + Capabilities: volume + Playback channels: Front Left - Front Right + Capture channels: Front Left - Front Right + Limits: 0 - 3 + Front Left: 0 [0%] + Front Right: 0 [0%] +Simple mixer control 'IEC958',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [off] +Simple mixer control 'IEC958 Default PCM',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [off] +Simple mixer control 'IEC958 Playback Source',0 + Capabilities: enum + Items: 'PCM' 'ADC' + Item0: 'PCM' +Simple mixer control 'Capture',0 + Capabilities: cvolume cswitch + Capture channels: Front Left - Front Right + Limits: Capture 0 - 15 + Front Left: Capture 0 [0%] [0.00dB] [on] + Front Right: Capture 0 [0%] [0.00dB] [on] +Simple mixer control 'Mix',0 + Capabilities: cswitch cswitch-joined cswitch-exclusive + Capture exclusive group: 0 + Capture channels: Mono + Mono: Capture [off] diff --git a/src/modules/alsa/mixer/samples/HDA Intel--Conexant CX20551 (Waikiki) b/src/modules/alsa/mixer/samples/HDA Intel--Conexant CX20551 (Waikiki) new file mode 100644 index 00000000..16f7c49b --- /dev/null +++ b/src/modules/alsa/mixer/samples/HDA Intel--Conexant CX20551 (Waikiki) @@ -0,0 +1,42 @@ +Simple mixer control 'Master',0 + Capabilities: pvolume pswitch pswitch-joined + Playback channels: Front Left - Front Right + Limits: Playback 0 - 30 + Mono: + Front Left: Playback 17 [57%] [-21.00dB] [on] + Front Right: Playback 17 [57%] [-21.00dB] [on] +Simple mixer control 'PCM',0 + Capabilities: pvolume pswitch + Playback channels: Front Left - Front Right + Limits: Playback 0 - 255 + Mono: + Front Left: Playback 230 [90%] [-5.00dB] [on] + Front Right: Playback 230 [90%] [-5.00dB] [on] +Simple mixer control 'Mic',0 + Capabilities: pvolume pswitch + Playback channels: Front Left - Front Right + Limits: Playback 0 - 30 + Mono: + Front Left: Playback 19 [63%] [-6.00dB] [on] + Front Right: Playback 19 [63%] [-6.00dB] [on] +Simple mixer control 'Mic Boost',0 + Capabilities: volume + Playback channels: Front Left - Front Right + Capture channels: Front Left - Front Right + Limits: 0 - 3 + Front Left: 3 [100%] + Front Right: 3 [100%] +Simple mixer control 'IEC958',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [on] +Simple mixer control 'IEC958 Default PCM',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [on] +Simple mixer control 'Capture',0 + Capabilities: cvolume cswitch + Capture channels: Front Left - Front Right + Limits: Capture 0 - 14 + Front Left: Capture 0 [0%] [0.00dB] [off] + Front Right: Capture 0 [0%] [0.00dB] [off]
\ No newline at end of file diff --git a/src/modules/alsa/mixer/samples/HDA Intel--Realtek ALC889A b/src/modules/alsa/mixer/samples/HDA Intel--Realtek ALC889A new file mode 100644 index 00000000..28a2e73c --- /dev/null +++ b/src/modules/alsa/mixer/samples/HDA Intel--Realtek ALC889A @@ -0,0 +1,113 @@ +Simple mixer control 'Master',0 + Capabilities: pvolume pvolume-joined pswitch pswitch-joined + Playback channels: Mono + Limits: Playback 0 - 64 + Mono: Playback 64 [100%] [0.00dB] [on] +Simple mixer control 'Headphone',0 + Capabilities: pswitch + Playback channels: Front Left - Front Right + Mono: + Front Left: Playback [on] + Front Right: Playback [on] +Simple mixer control 'PCM',0 + Capabilities: pvolume + Playback channels: Front Left - Front Right + Limits: Playback 0 - 255 + Mono: + Front Left: Playback 255 [100%] [0.00dB] + Front Right: Playback 255 [100%] [0.00dB] +Simple mixer control 'Front',0 + Capabilities: pvolume pswitch + Playback channels: Front Left - Front Right + Limits: Playback 0 - 64 + Mono: + Front Left: Playback 44 [69%] [-20.00dB] [on] + Front Right: Playback 44 [69%] [-20.00dB] [on] +Simple mixer control 'Front Mic',0 + Capabilities: pvolume pswitch + Playback channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: + Front Left: Playback 0 [0%] [-34.50dB] [off] + Front Right: Playback 0 [0%] [-34.50dB] [off] +Simple mixer control 'Front Mic Boost',0 + Capabilities: volume + Playback channels: Front Left - Front Right + Capture channels: Front Left - Front Right + Limits: 0 - 3 + Front Left: 0 [0%] + Front Right: 0 [0%] +Simple mixer control 'Surround',0 + Capabilities: pvolume pswitch + Playback channels: Front Left - Front Right + Limits: Playback 0 - 64 + Mono: + Front Left: Playback 0 [0%] [-64.00dB] [on] + Front Right: Playback 0 [0%] [-64.00dB] [on] +Simple mixer control 'Center',0 + Capabilities: pvolume pvolume-joined pswitch pswitch-joined + Playback channels: Mono + Limits: Playback 0 - 64 + Mono: Playback 0 [0%] [-64.00dB] [on] +Simple mixer control 'LFE',0 + Capabilities: pvolume pvolume-joined pswitch pswitch-joined + Playback channels: Mono + Limits: Playback 0 - 64 + Mono: Playback 0 [0%] [-64.00dB] [on] +Simple mixer control 'Side',0 + Capabilities: pvolume pswitch + Playback channels: Front Left - Front Right + Limits: Playback 0 - 64 + Mono: + Front Left: Playback 0 [0%] [-64.00dB] [on] + Front Right: Playback 0 [0%] [-64.00dB] [on] +Simple mixer control 'Line',0 + Capabilities: pvolume pswitch + Playback channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: + Front Left: Playback 0 [0%] [-34.50dB] [off] + Front Right: Playback 0 [0%] [-34.50dB] [off] +Simple mixer control 'Mic',0 + Capabilities: pvolume pswitch + Playback channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: + Front Left: Playback 0 [0%] [-34.50dB] [off] + Front Right: Playback 0 [0%] [-34.50dB] [off] +Simple mixer control 'Mic Boost',0 + Capabilities: volume + Playback channels: Front Left - Front Right + Capture channels: Front Left - Front Right + Limits: 0 - 3 + Front Left: 0 [0%] + Front Right: 0 [0%] +Simple mixer control 'IEC958',0 + Capabilities: pswitch pswitch-joined cswitch cswitch-joined + Playback channels: Mono + Capture channels: Mono + Mono: Playback [on] Capture [on] +Simple mixer control 'IEC958 Default PCM',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [on] +Simple mixer control 'Capture',0 + Capabilities: cvolume cswitch + Capture channels: Front Left - Front Right + Limits: Capture 0 - 46 + Front Left: Capture 23 [50%] [7.00dB] [on] + Front Right: Capture 23 [50%] [7.00dB] [on] +Simple mixer control 'Capture',1 + Capabilities: cvolume cswitch + Capture channels: Front Left - Front Right + Limits: Capture 0 - 46 + Front Left: Capture 0 [0%] [-16.00dB] [off] + Front Right: Capture 0 [0%] [-16.00dB] [off] +Simple mixer control 'Input Source',0 + Capabilities: cenum + Items: 'Mic' 'Front Mic' 'Line' + Item0: 'Mic' +Simple mixer control 'Input Source',1 + Capabilities: cenum + Items: 'Mic' 'Front Mic' 'Line' + Item0: 'Mic' diff --git a/src/modules/alsa/mixer/samples/Intel 82801CA-ICH3--Analog Devices AD1881A b/src/modules/alsa/mixer/samples/Intel 82801CA-ICH3--Analog Devices AD1881A new file mode 100644 index 00000000..3ddd8af6 --- /dev/null +++ b/src/modules/alsa/mixer/samples/Intel 82801CA-ICH3--Analog Devices AD1881A @@ -0,0 +1,128 @@ +Simple mixer control 'Master',0 + Capabilities: pvolume pswitch pswitch-joined + Playback channels: Front Left - Front Right + Limits: Playback 0 - 63 + Mono: + Front Left: Playback 44 [70%] [-28.50dB] [on] + Front Right: Playback 60 [95%] [-4.50dB] [on] +Simple mixer control 'Master Mono',0 + Capabilities: pvolume pvolume-joined pswitch pswitch-joined + Playback channels: Mono + Limits: Playback 0 - 31 + Mono: Playback 17 [55%] [-21.00dB] [on] +Simple mixer control '3D Control - Center',0 + Capabilities: volume volume-joined + Playback channels: Mono + Capture channels: Mono + Limits: 0 - 15 + Mono: 0 [0%] +Simple mixer control '3D Control - Depth',0 + Capabilities: volume volume-joined + Playback channels: Mono + Capture channels: Mono + Limits: 0 - 15 + Mono: 0 [0%] +Simple mixer control '3D Control - Switch',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [off] +Simple mixer control 'PCM',0 + Capabilities: pvolume pswitch pswitch-joined + Playback channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: + Front Left: Playback 9 [29%] [-21.00dB] [on] + Front Right: Playback 9 [29%] [-21.00dB] [on] +Simple mixer control 'PCM Out Path & Mute',0 + Capabilities: enum + Items: 'pre 3D' 'post 3D' + Item0: 'pre 3D' +Simple mixer control 'Line',0 + Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Front Left - Front Right + Capture channels: Front Left - Front Right + Limits: Playback 0 - 31 + Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off] + Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off] +Simple mixer control 'CD',0 + Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Front Left - Front Right + Capture channels: Front Left - Front Right + Limits: Playback 0 - 31 + Front Left: Playback 9 [29%] [-21.00dB] [on] Capture [off] + Front Right: Playback 9 [29%] [-21.00dB] [on] Capture [off] +Simple mixer control 'Mic',0 + Capabilities: pvolume pvolume-joined pswitch pswitch-joined cswitch cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Mono + Capture channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: Playback 0 [0%] [-34.50dB] [off] + Front Left: Capture [on] + Front Right: Capture [on] +Simple mixer control 'Mic Boost (+20dB)',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [off] +Simple mixer control 'Mic Select',0 + Capabilities: enum + Items: 'Mic1' 'Mic2' + Item0: 'Mic1' +Simple mixer control 'Video',0 + Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Front Left - Front Right + Capture channels: Front Left - Front Right + Limits: Playback 0 - 31 + Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off] + Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off] +Simple mixer control 'Phone',0 + Capabilities: pvolume pvolume-joined pswitch pswitch-joined cswitch cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Mono + Capture channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: Playback 0 [0%] [-34.50dB] [off] + Front Left: Capture [off] + Front Right: Capture [off] +Simple mixer control 'PC Speaker',0 + Capabilities: pvolume pvolume-joined pswitch pswitch-joined + Playback channels: Mono + Limits: Playback 0 - 15 + Mono: Playback 8 [53%] [-21.00dB] [on] +Simple mixer control 'Aux',0 + Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Front Left - Front Right + Capture channels: Front Left - Front Right + Limits: Playback 0 - 31 + Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off] + Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off] +Simple mixer control 'Mono Output Select',0 + Capabilities: enum + Items: 'Mix' 'Mic' + Item0: 'Mix' +Simple mixer control 'Capture',0 + Capabilities: cvolume cswitch cswitch-joined + Capture channels: Front Left - Front Right + Limits: Capture 0 - 15 + Front Left: Capture 13 [87%] [19.50dB] [on] + Front Right: Capture 13 [87%] [19.50dB] [on] +Simple mixer control 'Mix',0 + Capabilities: cswitch cswitch-exclusive + Capture exclusive group: 0 + Capture channels: Front Left - Front Right + Front Left: Capture [off] + Front Right: Capture [off] +Simple mixer control 'Mix Mono',0 + Capabilities: cswitch cswitch-exclusive + Capture exclusive group: 0 + Capture channels: Front Left - Front Right + Front Left: Capture [off] + Front Right: Capture [off] +Simple mixer control 'External Amplifier',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [on] diff --git a/src/modules/alsa/mixer/samples/Logitech USB Speaker--USB Mixer b/src/modules/alsa/mixer/samples/Logitech USB Speaker--USB Mixer new file mode 100644 index 00000000..38cf6778 --- /dev/null +++ b/src/modules/alsa/mixer/samples/Logitech USB Speaker--USB Mixer @@ -0,0 +1,27 @@ +Simple mixer control 'Bass',0 + Capabilities: volume volume-joined + Playback channels: Mono + Capture channels: Mono + Limits: 0 - 48 + Mono: 22 [46%] +Simple mixer control 'Bass Boost',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [off] +Simple mixer control 'Treble',0 + Capabilities: volume volume-joined + Playback channels: Mono + Capture channels: Mono + Limits: 0 - 48 + Mono: 25 [52%] +Simple mixer control 'PCM',0 + Capabilities: pvolume pswitch pswitch-joined + Playback channels: Front Left - Front Right + Limits: Playback 0 - 44 + Mono: + Front Left: Playback 10 [23%] [-31.00dB] [on] + Front Right: Playback 10 [23%] [-31.00dB] [on] +Simple mixer control 'Auto Gain Control',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [off] diff --git a/src/modules/alsa/mixer/samples/USB Audio--USB Mixer b/src/modules/alsa/mixer/samples/USB Audio--USB Mixer new file mode 100644 index 00000000..9cb4fa7f --- /dev/null +++ b/src/modules/alsa/mixer/samples/USB Audio--USB Mixer @@ -0,0 +1,37 @@ +Simple mixer control 'Master',0 + Capabilities: pvolume pvolume-joined pswitch pswitch-joined + Playback channels: Mono + Limits: Playback 0 - 255 + Mono: Playback 105 [41%] [-28.97dB] [on] +Simple mixer control 'Line',0 + Capabilities: pvolume cvolume pswitch pswitch-joined cswitch cswitch-joined + Playback channels: Front Left - Front Right + Capture channels: Front Left - Front Right + Limits: Playback 0 - 255 Capture 0 - 128 + Front Left: Playback 191 [75%] [34.38dB] [off] Capture 0 [0%] [0.18dB] [off] + Front Right: Playback 191 [75%] [34.38dB] [off] Capture 0 [0%] [0.18dB] [off] +Simple mixer control 'Mic',0 + Capabilities: pvolume pvolume-joined cvolume cvolume-joined pswitch pswitch-joined cswitch cswitch-joined cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Mono + Capture channels: Mono + Limits: Playback 0 - 255 Capture 0 - 128 + Mono: Playback 191 [75%] [34.38dB] [off] Capture 0 [0%] [0.18dB] [on] +Simple mixer control 'Mic Capture',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [off] +Simple mixer control 'IEC958 In',0 + Capabilities: cswitch cswitch-joined + Capture channels: Mono + Mono: Capture [off] +Simple mixer control 'Input 1',0 + Capabilities: cswitch cswitch-joined cswitch-exclusive + Capture exclusive group: 0 + Capture channels: Mono + Mono: Capture [off] +Simple mixer control 'Input 2',0 + Capabilities: cswitch cswitch-joined cswitch-exclusive + Capture exclusive group: 0 + Capture channels: Mono + Mono: Capture [off] diff --git a/src/modules/alsa/mixer/samples/USB Device 0x46d:0x9a4--USB Mixer b/src/modules/alsa/mixer/samples/USB Device 0x46d:0x9a4--USB Mixer new file mode 100644 index 00000000..783f826f --- /dev/null +++ b/src/modules/alsa/mixer/samples/USB Device 0x46d:0x9a4--USB Mixer @@ -0,0 +1,5 @@ +Simple mixer control 'Mic',0 + Capabilities: cvolume cvolume-joined cswitch cswitch-joined + Capture channels: Mono + Limits: Capture 0 - 3072 + Mono: Capture 1536 [50%] [23.00dB] [on] diff --git a/src/modules/alsa/mixer/samples/VIA 8237--Analog Devices AD1888 b/src/modules/alsa/mixer/samples/VIA 8237--Analog Devices AD1888 new file mode 100644 index 00000000..15e7b5a6 --- /dev/null +++ b/src/modules/alsa/mixer/samples/VIA 8237--Analog Devices AD1888 @@ -0,0 +1,211 @@ +Simple mixer control 'Master',0 + Capabilities: pvolume pswitch + Playback channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: + Front Left: Playback 31 [100%] [0.00dB] [on] + Front Right: Playback 31 [100%] [0.00dB] [on] +Simple mixer control 'Master Mono',0 + Capabilities: pvolume pvolume-joined pswitch pswitch-joined + Playback channels: Mono + Limits: Playback 0 - 31 + Mono: Playback 0 [0%] [-46.50dB] [off] +Simple mixer control 'Master Surround',0 + Capabilities: pvolume pswitch + Playback channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: + Front Left: Playback 0 [0%] [-46.50dB] [off] + Front Right: Playback 0 [0%] [-46.50dB] [off] +Simple mixer control 'Headphone Jack Sense',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [off] +Simple mixer control 'PCM',0 + Capabilities: pvolume pswitch + Playback channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: + Front Left: Playback 23 [74%] [0.00dB] [on] + Front Right: Playback 23 [74%] [0.00dB] [on] +Simple mixer control 'Surround',0 + Capabilities: pvolume pswitch + Playback channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: + Front Left: Playback 0 [0%] [-46.50dB] [off] + Front Right: Playback 0 [0%] [-46.50dB] [off] +Simple mixer control 'Surround Jack Mode',0 + Capabilities: enum + Items: 'Shared' 'Independent' + Item0: 'Shared' +Simple mixer control 'Center',0 + Capabilities: pvolume pvolume-joined pswitch pswitch-joined + Playback channels: Mono + Limits: Playback 0 - 31 + Mono: Playback 31 [100%] [0.00dB] [off] +Simple mixer control 'LFE',0 + Capabilities: pvolume pvolume-joined pswitch pswitch-joined + Playback channels: Mono + Limits: Playback 0 - 31 + Mono: Playback 0 [0%] [-46.50dB] [off] +Simple mixer control 'Line',0 + Capabilities: pvolume pswitch cswitch cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Front Left - Front Right + Capture channels: Front Left - Front Right + Limits: Playback 0 - 31 + Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off] + Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off] +Simple mixer control 'Line Jack Sense',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [off] +Simple mixer control 'CD',0 + Capabilities: pvolume pswitch cswitch cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Front Left - Front Right + Capture channels: Front Left - Front Right + Limits: Playback 0 - 31 + Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off] + Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off] +Simple mixer control 'Mic',0 + Capabilities: pvolume pvolume-joined pswitch pswitch-joined cswitch cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Mono + Capture channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: Playback 0 [0%] [-34.50dB] [off] + Front Left: Capture [on] + Front Right: Capture [on] +Simple mixer control 'Mic Boost (+20dB)',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [off] +Simple mixer control 'Mic Select',0 + Capabilities: enum + Items: 'Mic1' 'Mic2' + Item0: 'Mic1' +Simple mixer control 'Video',0 + Capabilities: cswitch cswitch-exclusive + Capture exclusive group: 0 + Capture channels: Front Left - Front Right + Front Left: Capture [off] + Front Right: Capture [off] +Simple mixer control 'Phone',0 + Capabilities: pvolume pvolume-joined pswitch pswitch-joined cswitch cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Mono + Capture channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: Playback 0 [0%] [-34.50dB] [off] + Front Left: Capture [off] + Front Right: Capture [off] +Simple mixer control 'IEC958',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [off] +Simple mixer control 'IEC958 Output',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [off] +Simple mixer control 'IEC958 Playback AC97-SPSA',0 + Capabilities: volume volume-joined + Playback channels: Mono + Capture channels: Mono + Limits: 0 - 3 + Mono: 3 [100%] +Simple mixer control 'IEC958 Playback Source',0 + Capabilities: enum + Items: 'AC-Link' 'A/D Converter' + Item0: 'AC-Link' +Simple mixer control 'Aux',0 + Capabilities: pvolume pswitch cswitch cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Front Left - Front Right + Capture channels: Front Left - Front Right + Limits: Playback 0 - 31 + Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off] + Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off] +Simple mixer control 'Capture',0 + Capabilities: cvolume cswitch + Capture channels: Front Left - Front Right + Limits: Capture 0 - 15 + Front Left: Capture 0 [0%] [0.00dB] [on] + Front Right: Capture 0 [0%] [0.00dB] [on] +Simple mixer control 'Mix',0 + Capabilities: cswitch cswitch-exclusive + Capture exclusive group: 0 + Capture channels: Front Left - Front Right + Front Left: Capture [off] + Front Right: Capture [off] +Simple mixer control 'Mix Mono',0 + Capabilities: cswitch cswitch-exclusive + Capture exclusive group: 0 + Capture channels: Front Left - Front Right + Front Left: Capture [off] + Front Right: Capture [off] +Simple mixer control 'Channel Mode',0 + Capabilities: enum + Items: '2ch' '4ch' '6ch' + Item0: '2ch' +Simple mixer control 'Downmix',0 + Capabilities: enum + Items: 'Off' '6 -> 4' '6 -> 2' + Item0: 'Off' +Simple mixer control 'Exchange Front/Surround',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [off] +Simple mixer control 'External Amplifier',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [on] +Simple mixer control 'High Pass Filter Enable',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [off] +Simple mixer control 'Input Source Select',0 + Capabilities: enum + Items: 'Input1' 'Input2' + Item0: 'Input1' +Simple mixer control 'Input Source Select',1 + Capabilities: enum + Items: 'Input1' 'Input2' + Item0: 'Input1' +Simple mixer control 'Spread Front to Surround and Center/LFE',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [off] +Simple mixer control 'VIA DXS',0 + Capabilities: pvolume + Playback channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: + Front Left: Playback 31 [100%] [-48.00dB] + Front Right: Playback 31 [100%] [-48.00dB] +Simple mixer control 'VIA DXS',1 + Capabilities: pvolume + Playback channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: + Front Left: Playback 31 [100%] [-48.00dB] + Front Right: Playback 31 [100%] [-48.00dB] +Simple mixer control 'VIA DXS',2 + Capabilities: pvolume + Playback channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: + Front Left: Playback 31 [100%] [-48.00dB] + Front Right: Playback 31 [100%] [-48.00dB] +Simple mixer control 'VIA DXS',3 + Capabilities: pvolume + Playback channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: + Front Left: Playback 31 [100%] [-48.00dB] + Front Right: Playback 31 [100%] [-48.00dB] +Simple mixer control 'V_REFOUT Enable',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [on] diff --git a/src/modules/alsa/mixer/samples/VIA 8237--C-Media Electronics CMI9761A+ b/src/modules/alsa/mixer/samples/VIA 8237--C-Media Electronics CMI9761A+ new file mode 100644 index 00000000..d4f3db62 --- /dev/null +++ b/src/modules/alsa/mixer/samples/VIA 8237--C-Media Electronics CMI9761A+ @@ -0,0 +1,160 @@ +Simple mixer control 'Master',0 + Capabilities: pvolume pswitch pswitch-joined + Playback channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: + Front Left: Playback 0 [0%] [-46.50dB] [off] + Front Right: Playback 0 [0%] [-46.50dB] [off] +Simple mixer control 'PCM',0 + Capabilities: pvolume pswitch pswitch-joined + Playback channels: Front Left - Front Right + Limits: Playback 0 - 31 + Mono: + Front Left: Playback 31 [100%] [-48.00dB] [off] + Front Right: Playback 31 [100%] [-48.00dB] [off] +Simple mixer control 'Surround',0 + Capabilities: pswitch + Playback channels: Front Left - Front Right + Mono: + Front Left: Playback [off] + Front Right: Playback [off] +Simple mixer control 'Surround Jack Mode',0 + Capabilities: enum + Items: 'Shared' 'Independent' + Item0: 'Shared' +Simple mixer control 'Center',0 + Capabilities: pvolume pvolume-joined pswitch pswitch-joined + Playback channels: Mono + Limits: Playback 0 - 31 + Mono: Playback 31 [100%] [0.00dB] [off] +Simple mixer control 'LFE',0 + Capabilities: pvolume pvolume-joined pswitch pswitch-joined + Playback channels: Mono + Limits: Playback 0 - 31 + Mono: Playback 0 [0%] [-46.50dB] [off] +Simple mixer control 'Line',0 + Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Front Left - Front Right + Capture channels: Front Left - Front Right + Limits: Playback 0 - 31 + Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off] + Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off] +Simple mixer control 'CD',0 + Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Front Left - Front Right + Capture channels: Front Left - Front Right + Limits: Playback 0 - 31 + Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off] + Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off] +Simple mixer control 'Mic',0 + Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Front Left - Front Right + Capture channels: Front Left - Front Right + Limits: Playback 0 - 31 + Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [on] + Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [on] +Simple mixer control 'Mic Boost (+20dB)',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [off] +Simple mixer control 'Mic Select',0 + Capabilities: enum + Items: 'Mic1' 'Mic2' + Item0: 'Mic1' +Simple mixer control 'Video',0 + Capabilities: cswitch cswitch-exclusive + Capture exclusive group: 0 + Capture channels: Front Left - Front Right + Front Left: Capture [off] + Front Right: Capture [off] +Simple mixer control 'Phone',0 + Capabilities: cswitch cswitch-exclusive + Capture exclusive group: 0 + Capture channels: Front Left - Front Right + Front Left: Capture [off] + Front Right: Capture [off] +Simple mixer control 'IEC958',0 + Capabilities: pswitch pswitch-joined cswitch cswitch-joined + Playback channels: Mono + Capture channels: Mono + Mono: Playback [off] Capture [off] +Simple mixer control 'IEC958 Capture Monitor',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [off] +Simple mixer control 'IEC958 Capture Valid',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [off] +Simple mixer control 'IEC958 Output',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [off] +Simple mixer control 'IEC958 Playback AC97-SPSA',0 + Capabilities: volume volume-joined + Playback channels: Mono + Capture channels: Mono + Limits: 0 - 3 + Mono: 3 [100%] +Simple mixer control 'IEC958 Playback Source',0 + Capabilities: enum + Items: 'AC-Link' 'ADC' 'SPDIF-In' + Item0: 'AC-Link' +Simple mixer control 'PC Speaker',0 + Capabilities: pvolume pvolume-joined pswitch pswitch-joined + Playback channels: Mono + Limits: Playback 0 - 15 + Mono: Playback 0 [0%] [-45.00dB] [off] +Simple mixer control 'Aux',0 + Capabilities: pvolume pswitch pswitch-joined cswitch cswitch-exclusive + Capture exclusive group: 0 + Playback channels: Front Left - Front Right + Capture channels: Front Left - Front Right + Limits: Playback 0 - 31 + Front Left: Playback 0 [0%] [-34.50dB] [off] Capture [off] + Front Right: Playback 0 [0%] [-34.50dB] [off] Capture [off] +Simple mixer control 'Mono Output Select',0 + Capabilities: enum + Items: 'Mix' 'Mic' + Item0: 'Mix' +Simple mixer control 'Capture',0 + Capabilities: cvolume cswitch cswitch-joined + Capture channels: Front Left - Front Right + Limits: Capture 0 - 15 + Front Left: Capture 0 [0%] [0.00dB] [on] + Front Right: Capture 0 [0%] [0.00dB] [on] +Simple mixer control 'Mix',0 + Capabilities: cswitch cswitch-exclusive + Capture exclusive group: 0 + Capture channels: Front Left - Front Right + Front Left: Capture [off] + Front Right: Capture [off] +Simple mixer control 'Mix Mono',0 + Capabilities: cswitch cswitch-exclusive + Capture exclusive group: 0 + Capture channels: Front Left - Front Right + Front Left: Capture [off] + Front Right: Capture [off] +Simple mixer control 'Channel Mode',0 + Capabilities: enum + Items: '2ch' '4ch' '6ch' + Item0: '2ch' +Simple mixer control 'DAC Clock Source',0 + Capabilities: enum + Items: 'AC-Link' 'SPDIF-In' 'Both' + Item0: 'AC-Link' +Simple mixer control 'External Amplifier',0 + Capabilities: pswitch pswitch-joined + Playback channels: Mono + Mono: Playback [on] +Simple mixer control 'Input Source Select',0 + Capabilities: enum + Items: 'Input1' 'Input2' + Item0: 'Input1' +Simple mixer control 'Input Source Select',1 + Capabilities: enum + Items: 'Input1' 'Input2' + Item0: 'Input1' diff --git a/src/modules/alsa/module-alsa-card.c b/src/modules/alsa/module-alsa-card.c index d5e2cdc2..55f6a6e2 100644 --- a/src/modules/alsa/module-alsa-card.c +++ b/src/modules/alsa/module-alsa-card.c @@ -32,6 +32,10 @@ #include <modules/reserve-wrap.h> +#ifdef HAVE_UDEV +#include <modules/udev-util.h> +#endif + #include "alsa-util.h" #include "alsa-sink.h" #include "alsa-source.h" @@ -43,9 +47,12 @@ PA_MODULE_VERSION(PACKAGE_VERSION); PA_MODULE_LOAD_ONCE(FALSE); PA_MODULE_USAGE( "name=<name for the card/sink/source, to be prefixed> " - "card_name=<name for card> " - "sink_name=<name for sink> " - "source_name=<name for source> " + "card_name=<name for the card> " + "card_properties=<properties for the card> " + "sink_name=<name for the sink> " + "sink_properties=<properties for the sink> " + "source_name=<name for the source> " + "source_properties=<properties for the source> " "device_id=<ALSA card index> " "format=<sample format> " "rate=<sample rate> " @@ -61,8 +68,11 @@ PA_MODULE_USAGE( static const char* const valid_modargs[] = { "name", "card_name", + "card_properties", "sink_name", + "sink_properties", "source_name", + "source_properties", "device_id", "format", "rate", @@ -86,81 +96,53 @@ struct userdata { char *device_id; pa_card *card; - pa_sink *sink; - pa_source *source; pa_modargs *modargs; - pa_hashmap *profiles; + pa_alsa_profile_set *profile_set; }; struct profile_data { - const pa_alsa_profile_info *sink_profile, *source_profile; + pa_alsa_profile *profile; }; -static void enumerate_cb( - const pa_alsa_profile_info *sink, - const pa_alsa_profile_info *source, - void *userdata) { - - struct userdata *u = userdata; - char *t, *n; - pa_card_profile *p; - struct profile_data *d; - unsigned bonus = 0; - - if (sink && source) { - n = pa_sprintf_malloc("output-%s+input-%s", sink->name, source->name); - t = pa_sprintf_malloc(_("Output %s + Input %s"), sink->description, _(source->description)); - } else if (sink) { - n = pa_sprintf_malloc("output-%s", sink->name); - t = pa_sprintf_malloc(_("Output %s"), _(sink->description)); - } else { - pa_assert(source); - n = pa_sprintf_malloc("input-%s", source->name); - t = pa_sprintf_malloc(_("Input %s"), _(source->description)); - } - - if (sink) { - if (pa_channel_map_equal(&sink->map, &u->core->default_channel_map)) - bonus += 50000; - else if (sink->map.channels == u->core->default_channel_map.channels) - bonus += 40000; - } +static void add_profiles(struct userdata *u, pa_hashmap *h) { + pa_alsa_profile *ap; + void *state; - if (source) { - if (pa_channel_map_equal(&source->map, &u->core->default_channel_map)) - bonus += 30000; - else if (source->map.channels == u->core->default_channel_map.channels) - bonus += 20000; - } - - pa_log_info("Found output profile '%s'", t); + pa_assert(u); + pa_assert(h); - p = pa_card_profile_new(n, t, sizeof(struct profile_data)); + PA_HASHMAP_FOREACH(ap, u->profile_set->profiles, state) { + struct profile_data *d; + pa_card_profile *cp; + pa_alsa_mapping *m; + uint32_t idx; - pa_xfree(t); - pa_xfree(n); + cp = pa_card_profile_new(ap->name, ap->description, sizeof(struct profile_data)); + cp->priority = ap->priority; - p->priority = - (sink ? sink->priority : 0) * 100 + - (source ? source->priority : 0) + - bonus; + if (ap->output_mappings) { + cp->n_sinks = pa_idxset_size(ap->output_mappings); - p->n_sinks = !!sink; - p->n_sources = !!source; + PA_IDXSET_FOREACH(m, ap->output_mappings, idx) + if (m->channel_map.channels > cp->max_sink_channels) + cp->max_sink_channels = m->channel_map.channels; + } - if (sink) - p->max_sink_channels = sink->map.channels; - if (source) - p->max_source_channels = source->map.channels; + if (ap->input_mappings) { + cp->n_sources = pa_idxset_size(ap->input_mappings); - d = PA_CARD_PROFILE_DATA(p); + PA_IDXSET_FOREACH(m, ap->input_mappings, idx) + if (m->channel_map.channels > cp->max_source_channels) + cp->max_source_channels = m->channel_map.channels; + } - d->sink_profile = sink; - d->source_profile = source; + d = PA_CARD_PROFILE_DATA(cp); + d->profile = ap; - pa_hashmap_put(u->profiles, p->name, p); + pa_hashmap_put(h, cp->name, cp); + } } static void add_disabled_profile(pa_hashmap *profiles) { @@ -170,7 +152,7 @@ static void add_disabled_profile(pa_hashmap *profiles) { p = pa_card_profile_new("off", _("Off"), sizeof(struct profile_data)); d = PA_CARD_PROFILE_DATA(p); - d->sink_profile = d->source_profile = NULL; + d->profile = NULL; pa_hashmap_put(profiles, p->name, p); } @@ -178,6 +160,9 @@ static void add_disabled_profile(pa_hashmap *profiles) { static int card_set_profile(pa_card *c, pa_card_profile *new_profile) { struct userdata *u; struct profile_data *nd, *od; + uint32_t idx; + pa_alsa_mapping *am; + pa_queue *sink_inputs = NULL, *source_outputs = NULL; pa_assert(c); pa_assert(new_profile); @@ -186,67 +171,85 @@ static int card_set_profile(pa_card *c, pa_card_profile *new_profile) { nd = PA_CARD_PROFILE_DATA(new_profile); od = PA_CARD_PROFILE_DATA(c->active_profile); - if (od->sink_profile != nd->sink_profile) { - pa_queue *inputs = NULL; + if (od->profile && od->profile->output_mappings) + PA_IDXSET_FOREACH(am, od->profile->output_mappings, idx) { + if (!am->sink) + continue; - if (u->sink) { - if (nd->sink_profile) - inputs = pa_sink_move_all_start(u->sink); + if (nd->profile && + nd->profile->output_mappings && + pa_idxset_get_by_data(nd->profile->output_mappings, am, NULL)) + continue; - pa_alsa_sink_free(u->sink); - u->sink = NULL; + sink_inputs = pa_sink_move_all_start(am->sink, sink_inputs); + pa_alsa_sink_free(am->sink); + am->sink = NULL; } - if (nd->sink_profile) { - u->sink = pa_alsa_sink_new(c->module, u->modargs, __FILE__, c, nd->sink_profile); + if (od->profile && od->profile->input_mappings) + PA_IDXSET_FOREACH(am, od->profile->input_mappings, idx) { + if (!am->source) + continue; - if (inputs) { - if (u->sink) - pa_sink_move_all_finish(u->sink, inputs, FALSE); - else - pa_sink_move_all_fail(inputs); - } + if (nd->profile && + nd->profile->input_mappings && + pa_idxset_get_by_data(nd->profile->input_mappings, am, NULL)) + continue; + + source_outputs = pa_source_move_all_start(am->source, source_outputs); + pa_alsa_source_free(am->source); + am->source = NULL; } - } - if (od->source_profile != nd->source_profile) { - pa_queue *outputs = NULL; + if (nd->profile && nd->profile->output_mappings) + PA_IDXSET_FOREACH(am, nd->profile->output_mappings, idx) { - if (u->source) { - if (nd->source_profile) - outputs = pa_source_move_all_start(u->source); + if (!am->sink) + am->sink = pa_alsa_sink_new(c->module, u->modargs, __FILE__, c, am); - pa_alsa_source_free(u->source); - u->source = NULL; + if (sink_inputs && am->sink) { + pa_sink_move_all_finish(am->sink, sink_inputs, FALSE); + sink_inputs = NULL; + } } - if (nd->source_profile) { - u->source = pa_alsa_source_new(c->module, u->modargs, __FILE__, c, nd->source_profile); + if (nd->profile && nd->profile->input_mappings) + PA_IDXSET_FOREACH(am, nd->profile->input_mappings, idx) { + + if (!am->source) + am->source = pa_alsa_source_new(c->module, u->modargs, __FILE__, c, am); - if (outputs) { - if (u->source) - pa_source_move_all_finish(u->source, outputs, FALSE); - else - pa_source_move_all_fail(outputs); + if (source_outputs && am->source) { + pa_source_move_all_finish(am->source, source_outputs, FALSE); + source_outputs = NULL; } } - } + + if (sink_inputs) + pa_sink_move_all_fail(sink_inputs); + + if (source_outputs) + pa_source_move_all_fail(source_outputs); return 0; } static void init_profile(struct userdata *u) { + uint32_t idx; + pa_alsa_mapping *am; struct profile_data *d; pa_assert(u); d = PA_CARD_PROFILE_DATA(u->card->active_profile); - if (d->sink_profile) - u->sink = pa_alsa_sink_new(u->module, u->modargs, __FILE__, u->card, d->sink_profile); + if (d->profile && d->profile->output_mappings) + PA_IDXSET_FOREACH(am, d->profile->output_mappings, idx) + am->sink = pa_alsa_sink_new(u->module, u->modargs, __FILE__, u->card, am); - if (d->source_profile) - u->source = pa_alsa_source_new(u->module, u->modargs, __FILE__, u->card, d->source_profile); + if (d->profile && d->profile->input_mappings) + PA_IDXSET_FOREACH(am, d->profile->input_mappings, idx) + am->source = pa_alsa_source_new(u->module, u->modargs, __FILE__, u->card, am); } static void set_card_name(pa_card_new_data *data, pa_modargs *ma, const char *device_id) { @@ -280,12 +283,11 @@ int pa__init(pa_module *m) { pa_modargs *ma; int alsa_card_index; struct userdata *u; - char rname[32]; pa_reserve_wrapper *reserve = NULL; const char *description; + char *fn = NULL; - pa_alsa_redirect_errors_inc(); - snd_config_update_free_global(); + pa_alsa_refcnt_inc(); pa_assert(m); @@ -294,30 +296,47 @@ int pa__init(pa_module *m) { goto fail; } - m->userdata = u = pa_xnew(struct userdata, 1); + m->userdata = u = pa_xnew0(struct userdata, 1); u->core = m->core; u->module = m; u->device_id = pa_xstrdup(pa_modargs_get_value(ma, "device_id", DEFAULT_DEVICE_ID)); - u->card = NULL; - u->sink = NULL; - u->source = NULL; u->modargs = ma; if ((alsa_card_index = snd_card_get_index(u->device_id)) < 0) { - pa_log("Card '%s' doesn't exist: %s", u->device_id, snd_strerror(alsa_card_index)); + pa_log("Card '%s' doesn't exist: %s", u->device_id, pa_alsa_strerror(alsa_card_index)); goto fail; } - pa_snprintf(rname, sizeof(rname), "Audio%i", alsa_card_index); + if (!pa_in_system_mode()) { + char *rname; - if (!pa_in_system_mode()) - if (!(reserve = pa_reserve_wrapper_get(m->core, rname))) - goto fail; + if ((rname = pa_alsa_get_reserve_name(u->device_id))) { + reserve = pa_reserve_wrapper_get(m->core, rname); + pa_xfree(rname); + + if (!reserve) + goto fail; + } + } + +#ifdef HAVE_UDEV + fn = pa_udev_get_property(alsa_card_index, "PULSE_PROFILE_SET"); +#endif + + u->profile_set = pa_alsa_profile_set_new(fn, &u->core->default_channel_map); + pa_xfree(fn); + + if (!u->profile_set) + goto fail; + + pa_alsa_profile_set_probe(u->profile_set, u->device_id, &m->core->default_sample_spec); pa_card_new_data_init(&data); data.driver = __FILE__; data.module = m; + pa_alsa_init_proplist_card(m->core, data.proplist, alsa_card_index); + pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->device_id); pa_alsa_init_description(data.proplist); set_card_name(&data, ma, u->device_id); @@ -326,11 +345,8 @@ int pa__init(pa_module *m) { if ((description = pa_proplist_gets(data.proplist, PA_PROP_DEVICE_DESCRIPTION))) pa_reserve_wrapper_set_application_device_name(reserve, description); - u->profiles = data.profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func); - if (pa_alsa_probe_profiles(u->device_id, &m->core->default_sample_spec, enumerate_cb, u) < 0) { - pa_card_new_data_done(&data); - goto fail; - } + data.profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func); + add_profiles(u, data.profiles); if (pa_hashmap_isempty(data.profiles)) { pa_log("Failed to find a working profile."); @@ -340,6 +356,12 @@ int pa__init(pa_module *m) { add_disabled_profile(data.profiles); + if (pa_modargs_get_proplist(ma, "card_properties", data.proplist, PA_UPDATE_REPLACE) < 0) { + pa_log("Invalid properties"); + pa_card_new_data_done(&data); + goto fail; + } + u->card = pa_card_new(m->core, &data); pa_card_new_data_done(&data); @@ -351,7 +373,8 @@ int pa__init(pa_module *m) { init_profile(u); - pa_reserve_wrapper_unref(reserve); + if (reserve) + pa_reserve_wrapper_unref(reserve); return 0; @@ -366,13 +389,22 @@ fail: int pa__get_n_used(pa_module *m) { struct userdata *u; + int n = 0; + uint32_t idx; + pa_sink *sink; + pa_source *source; pa_assert(m); pa_assert_se(u = m->userdata); + pa_assert(u->card); + + PA_IDXSET_FOREACH(sink, u->card->sinks, idx) + n += pa_sink_linked_by(sink); - return - (u->sink ? pa_sink_linked_by(u->sink) : 0) + - (u->source ? pa_source_linked_by(u->source) : 0); + PA_IDXSET_FOREACH(source, u->card->sources, idx) + n += pa_source_linked_by(source); + + return n; } void pa__done(pa_module*m) { @@ -383,11 +415,19 @@ void pa__done(pa_module*m) { if (!(u = m->userdata)) goto finish; - if (u->sink) - pa_alsa_sink_free(u->sink); + if (u->card && u->card->sinks) { + pa_sink *s; + + while ((s = pa_idxset_steal_first(u->card->sinks, NULL))) + pa_alsa_sink_free(s); + } + + if (u->card && u->card->sources) { + pa_source *s; - if (u->source) - pa_alsa_source_free(u->source); + while ((s = pa_idxset_steal_first(u->card->sources, NULL))) + pa_alsa_source_free(s); + } if (u->card) pa_card_free(u->card); @@ -395,10 +435,12 @@ void pa__done(pa_module*m) { if (u->modargs) pa_modargs_free(u->modargs); + if (u->profile_set) + pa_alsa_profile_set_free(u->profile_set); + pa_xfree(u->device_id); pa_xfree(u); finish: - snd_config_update_free_global(); - pa_alsa_redirect_errors_dec(); + pa_alsa_refcnt_dec(); } diff --git a/src/modules/alsa/module-alsa-sink.c b/src/modules/alsa/module-alsa-sink.c index c728a446..3aa89b2a 100644 --- a/src/modules/alsa/module-alsa-sink.c +++ b/src/modules/alsa/module-alsa-sink.c @@ -40,6 +40,7 @@ PA_MODULE_LOAD_ONCE(FALSE); PA_MODULE_USAGE( "name=<name of the sink, to be prefixed> " "sink_name=<name for the sink> " + "sink_properities=<properties for the sink> " "device=<ALSA device> " "device_id=<ALSA card index> " "format=<sample format> " @@ -52,11 +53,13 @@ PA_MODULE_USAGE( "tsched=<enable system timer based scheduling mode?> " "tsched_buffer_size=<buffer size when using timer based scheduling> " "tsched_buffer_watermark=<lower fill watermark> " - "ignore_dB=<ignore dB information from the device?>"); + "ignore_dB=<ignore dB information from the device?> " + "control=<name of mixer control>"); static const char* const valid_modargs[] = { "name", "sink_name", + "sink_properties", "device", "device_id", "format", @@ -70,6 +73,7 @@ static const char* const valid_modargs[] = { "tsched_buffer_size", "tsched_buffer_watermark", "ignore_dB", + "control", NULL }; @@ -78,8 +82,7 @@ int pa__init(pa_module*m) { pa_assert(m); - pa_alsa_redirect_errors_inc(); - snd_config_update_free_global(); + pa_alsa_refcnt_inc(); if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { pa_log("Failed to parse module arguments"); @@ -120,6 +123,5 @@ void pa__done(pa_module*m) { if ((sink = m->userdata)) pa_alsa_sink_free(sink); - snd_config_update_free_global(); - pa_alsa_redirect_errors_dec(); + pa_alsa_refcnt_dec(); } diff --git a/src/modules/alsa/module-alsa-source.c b/src/modules/alsa/module-alsa-source.c index 6188019f..23da4185 100644 --- a/src/modules/alsa/module-alsa-source.c +++ b/src/modules/alsa/module-alsa-source.c @@ -37,6 +37,7 @@ #include <pulse/timeval.h> #include <pulsecore/core-error.h> +#include <pulsecore/core-rtclock.h> #include <pulsecore/core.h> #include <pulsecore/module.h> #include <pulsecore/memchunk.h> @@ -51,7 +52,6 @@ #include <pulsecore/thread-mq.h> #include <pulsecore/rtpoll.h> #include <pulsecore/time-smoother.h> -#include <pulsecore/rtclock.h> #include "alsa-util.h" #include "alsa-source.h" @@ -64,6 +64,7 @@ PA_MODULE_LOAD_ONCE(FALSE); PA_MODULE_USAGE( "name=<name for the source, to be prefixed> " "source_name=<name for the source> " + "source_properties=<properties for the source> " "device=<ALSA device> " "device_id=<ALSA card index> " "format=<sample format> " @@ -76,11 +77,13 @@ PA_MODULE_USAGE( "tsched=<enable system timer based scheduling mode?> " "tsched_buffer_size=<buffer size when using timer based scheduling> " "tsched_buffer_watermark=<upper fill watermark> " - "ignore_dB=<ignore dB information from the device?>"); + "ignore_dB=<ignore dB information from the device?> " + "control=<name of mixer control>"); static const char* const valid_modargs[] = { "name", "source_name", + "source_properties", "device", "device_id", "format", @@ -94,6 +97,7 @@ static const char* const valid_modargs[] = { "tsched_buffer_size", "tsched_buffer_watermark", "ignore_dB", + "control", NULL }; @@ -102,8 +106,7 @@ int pa__init(pa_module*m) { pa_assert(m); - pa_alsa_redirect_errors_inc(); - snd_config_update_free_global(); + pa_alsa_refcnt_inc(); if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { pa_log("Failed to parse module arguments"); @@ -144,6 +147,5 @@ void pa__done(pa_module*m) { if ((source = m->userdata)) pa_alsa_source_free(source); - snd_config_update_free_global(); - pa_alsa_redirect_errors_dec(); + pa_alsa_refcnt_dec(); } diff --git a/src/modules/bluetooth/bluetooth-util.c b/src/modules/bluetooth/bluetooth-util.c index ccc8bee3..66e1c31e 100644 --- a/src/modules/bluetooth/bluetooth-util.c +++ b/src/modules/bluetooth/bluetooth-util.c @@ -24,32 +24,38 @@ #endif #include <pulsecore/core-util.h> -#include <modules/dbus-util.h> +#include <pulsecore/shared.h> +#include <pulsecore/dbus-shared.h> #include "bluetooth-util.h" -enum mode { - MODE_FIND, - MODE_GET, - MODE_DISCOVER -}; - struct pa_bluetooth_discovery { - DBusConnection *connection; + PA_REFCNT_DECLARE; + + pa_core *core; + pa_dbus_connection *connection; PA_LLIST_HEAD(pa_dbus_pending, pending); + pa_hashmap *devices; + pa_hook hook; +}; - enum mode mode; +static void get_properties_reply(DBusPendingCall *pending, void *userdata); +static pa_dbus_pending* send_and_add_to_pending(pa_bluetooth_discovery *y, pa_bluetooth_device *d, DBusMessage *m, DBusPendingCallNotifyFunction func); - /* If mode == MODE_FIND look for a specific device by its address. - If mode == MODE_GET look for a specific device by its path. */ - const char *looking_for; - pa_bluetooth_device *found_device; +static pa_bt_audio_state_t pa_bt_audio_state_from_string(const char* value) { + pa_assert(value); - /* If looking_for is NULL we do long-time discovery */ - pa_hashmap *devices; - pa_bluetooth_device_callback_t callback; - struct userdata *userdata; -}; + if (pa_streq(value, "disconnected")) + return PA_BT_AUDIO_STATE_DISCONNECTED; + else if (pa_streq(value, "connecting")) + return PA_BT_AUDIO_STATE_CONNECTING; + else if (pa_streq(value, "connected")) + return PA_BT_AUDIO_STATE_CONNECTED; + else if (pa_streq(value, "playing")) + return PA_BT_AUDIO_STATE_PLAYING; + + return PA_BT_AUDIO_STATE_INVALID; +} static pa_bluetooth_uuid *uuid_new(const char *uuid) { pa_bluetooth_uuid *u; @@ -73,9 +79,9 @@ static pa_bluetooth_device* device_new(const char *path) { d = pa_xnew(pa_bluetooth_device, 1); - d->device_info_valid = d->audio_sink_info_valid = d->headset_info_valid = 0; + d->dead = FALSE; - d->data = NULL; + d->device_info_valid = 0; d->name = NULL; d->path = pa_xstrdup(path); @@ -87,14 +93,14 @@ static pa_bluetooth_device* device_new(const char *path) { d->class = -1; d->trusted = -1; - d->audio_sink_connected = -1; - - d->headset_connected = -1; + d->audio_state = PA_BT_AUDIO_STATE_INVALID; + d->audio_sink_state = PA_BT_AUDIO_STATE_INVALID; + d->headset_state = PA_BT_AUDIO_STATE_INVALID; return d; } -void pa_bluetooth_device_free(pa_bluetooth_device *d) { +static void device_free(pa_bluetooth_device *d) { pa_bluetooth_uuid *u; pa_assert(d); @@ -111,24 +117,14 @@ void pa_bluetooth_device_free(pa_bluetooth_device *d) { pa_xfree(d); } -static pa_bool_t device_is_loaded(pa_bluetooth_device *d) { - pa_assert(d); - - /* FIXME: e83621724d7939b97b4f01f0d7e965d61ef8e55e, f1daa282f030e4e2381341e0f65faca47c4b891b is borked, probably needs to be reversed */ - - return d->device_info_valid && (d->audio_sink_info_valid || d->headset_info_valid); -} - static pa_bool_t device_is_audio(pa_bluetooth_device *d) { pa_assert(d); - pa_assert(d->device_info_valid); - pa_assert(d->audio_sink_info_valid || d->headset_info_valid); - - /* FIXME: e83621724d7939b97b4f01f0d7e965d61ef8e55e, f1daa282f030e4e2381341e0f65faca47c4b891b is borked, probably needs to be reversed */ - - return d->device_info_valid > 0 && - (d->audio_sink_info_valid > 0 || d->headset_info_valid > 0); + return + d->device_info_valid && + (d->audio_state != PA_BT_AUDIO_STATE_INVALID || + d->audio_sink_state != PA_BT_AUDIO_STATE_INVALID || + d->headset_state != PA_BT_AUDIO_STATE_INVALID); } static int parse_device_property(pa_bluetooth_discovery *y, pa_bluetooth_device *d, DBusMessageIter *i) { @@ -224,11 +220,25 @@ static int parse_device_property(pa_bluetooth_discovery *y, pa_bluetooth_device while (dbus_message_iter_get_arg_type(&ai) != DBUS_TYPE_INVALID) { pa_bluetooth_uuid *node; const char *value; + DBusMessage *m; dbus_message_iter_get_basic(&ai, &value); node = uuid_new(value); PA_LLIST_PREPEND(pa_bluetooth_uuid, d->uuids, node); + /* this might eventually be racy if .Audio is not there yet, but the State change will come anyway later, so this call is for cold-detection mostly */ + pa_assert_se(m = dbus_message_new_method_call("org.bluez", d->path, "org.bluez.Audio", "GetProperties")); + send_and_add_to_pending(y, d, m, get_properties_reply); + + /* Vudentz said the interfaces are here when the UUIDs are announced */ + if (strcasecmp(HSP_HS_UUID, value) == 0 || strcasecmp(HFP_HS_UUID, value) == 0) { + pa_assert_se(m = dbus_message_new_method_call("org.bluez", d->path, "org.bluez.Headset", "GetProperties")); + send_and_add_to_pending(y, d, m, get_properties_reply); + } else if (strcasecmp(A2DP_SINK_UUID, value) == 0) { + pa_assert_se(m = dbus_message_new_method_call("org.bluez", d->path, "org.bluez.AudioSink", "GetProperties")); + send_and_add_to_pending(y, d, m, get_properties_reply); + } + if (!dbus_message_iter_next(&ai)) break; } @@ -241,12 +251,12 @@ static int parse_device_property(pa_bluetooth_discovery *y, pa_bluetooth_device return 0; } -static int parse_audio_property(pa_bluetooth_discovery *u, int *connected, DBusMessageIter *i) { +static int parse_audio_property(pa_bluetooth_discovery *u, int *state, DBusMessageIter *i) { const char *key; DBusMessageIter variant_i; pa_assert(u); - pa_assert(connected); + pa_assert(state); pa_assert(i); if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_STRING) { @@ -268,19 +278,18 @@ static int parse_audio_property(pa_bluetooth_discovery *u, int *connected, DBusM dbus_message_iter_recurse(i, &variant_i); -/* pa_log_debug("Parsing property org.bluez.{AudioSink|Headset}.%s", key); */ +/* pa_log_debug("Parsing property org.bluez.{Audio|AudioSink|Headset}.%s", key); */ switch (dbus_message_iter_get_arg_type(&variant_i)) { - case DBUS_TYPE_BOOLEAN: { + case DBUS_TYPE_STRING: { - dbus_bool_t value; + const char *value; dbus_message_iter_get_basic(&variant_i, &value); - if (pa_streq(key, "Connected")) - *connected = !!value; - -/* pa_log_debug("Value %s", pa_yes_no(value)); */ + if (pa_streq(key, "State")) + *state = pa_bt_audio_state_from_string(value); +/* pa_log_debug("Value %s", value); */ break; } @@ -289,21 +298,26 @@ static int parse_audio_property(pa_bluetooth_discovery *u, int *connected, DBusM return 0; } -static void run_callback(pa_bluetooth_discovery *y, pa_bluetooth_device *d, pa_bool_t good) { +static void run_callback(pa_bluetooth_discovery *y, pa_bluetooth_device *d, pa_bool_t dead) { pa_assert(y); pa_assert(d); - if (y->mode != MODE_DISCOVER) + if (!device_is_audio(d)) return; - if (!device_is_loaded(d)) - return; + d->dead = dead; + pa_hook_fire(&y->hook, d); +} - if (!device_is_audio(d)) - return; +static void remove_all_devices(pa_bluetooth_discovery *y) { + pa_bluetooth_device *d; - y->callback(y->userdata, d, good); + pa_assert(y); + while ((d = pa_hashmap_steal_first(y->devices))) { + run_callback(y, d, TRUE); + device_free(d); + } } static void get_properties_reply(DBusPendingCall *pending, void *userdata) { @@ -328,10 +342,12 @@ static void get_properties_reply(DBusPendingCall *pending, void *userdata) { if (dbus_message_is_method_call(p->message, "org.bluez.Device", "GetProperties")) d->device_info_valid = valid; - else if (dbus_message_is_method_call(p->message, "org.bluez.Headset", "GetProperties")) - d->headset_info_valid = valid; - else if (dbus_message_is_method_call(p->message, "org.bluez.AudioSink", "GetProperties")) - d->audio_sink_info_valid = valid; + + if (dbus_message_is_error(r, DBUS_ERROR_SERVICE_UNKNOWN)) { + pa_log_debug("Bluetooth daemon is apparently not available."); + remove_all_devices(y); + goto finish2; + } if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) { @@ -363,12 +379,16 @@ static void get_properties_reply(DBusPendingCall *pending, void *userdata) { if (parse_device_property(y, d, &dict_i) < 0) goto finish; + } else if (dbus_message_has_interface(p->message, "org.bluez.Audio")) { + if (parse_audio_property(y, &d->audio_state, &dict_i) < 0) + goto finish; + } else if (dbus_message_has_interface(p->message, "org.bluez.Headset")) { - if (parse_audio_property(y, &d->headset_connected, &dict_i) < 0) + if (parse_audio_property(y, &d->headset_state, &dict_i) < 0) goto finish; } else if (dbus_message_has_interface(p->message, "org.bluez.AudioSink")) { - if (parse_audio_property(y, &d->audio_sink_connected, &dict_i) < 0) + if (parse_audio_property(y, &d->audio_sink_state, &dict_i) < 0) goto finish; } } @@ -378,8 +398,9 @@ static void get_properties_reply(DBusPendingCall *pending, void *userdata) { } finish: - run_callback(y, d, TRUE); + run_callback(y, d, FALSE); +finish2: dbus_message_unref(r); PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p); @@ -393,9 +414,9 @@ static pa_dbus_pending* send_and_add_to_pending(pa_bluetooth_discovery *y, pa_bl pa_assert(y); pa_assert(m); - pa_assert_se(dbus_connection_send_with_reply(y->connection, m, &call, -1)); + pa_assert_se(dbus_connection_send_with_reply(pa_dbus_connection_get(y->connection), m, &call, -1)); - p = pa_dbus_pending_new(m, call, y, d); + p = pa_dbus_pending_new(pa_dbus_connection_get(y->connection), m, call, y, d); PA_LLIST_PREPEND(pa_dbus_pending, y->pending, p); dbus_pending_call_set_notify(call, func, p, NULL); @@ -409,24 +430,18 @@ static void found_device(pa_bluetooth_discovery *y, const char* path) { pa_assert(y); pa_assert(path); + if (pa_hashmap_get(y->devices, path)) + return; + d = device_new(path); - if (y->mode == MODE_DISCOVER) { - pa_assert(y->devices); - pa_hashmap_put(y->devices, d->path, d); - } else { - pa_assert(!y->found_device); - y->found_device = d; - } + pa_hashmap_put(y->devices, d->path, d); pa_assert_se(m = dbus_message_new_method_call("org.bluez", path, "org.bluez.Device", "GetProperties")); send_and_add_to_pending(y, d, m, get_properties_reply); - pa_assert_se(m = dbus_message_new_method_call("org.bluez", path, "org.bluez.Headset", "GetProperties")); - send_and_add_to_pending(y, d, m, get_properties_reply); - - pa_assert_se(m = dbus_message_new_method_call("org.bluez", path, "org.bluez.AudioSink", "GetProperties")); - send_and_add_to_pending(y, d, m, get_properties_reply); + /* Before we read the other properties (Audio, AudioSink, Headset) we wait + * that the UUID is read */ } static void list_devices_reply(DBusPendingCall *pending, void *userdata) { @@ -445,9 +460,15 @@ static void list_devices_reply(DBusPendingCall *pending, void *userdata) { pa_assert_se(y = p->context_data); pa_assert_se(r = dbus_pending_call_steal_reply(pending)); + if (dbus_message_is_error(r, DBUS_ERROR_SERVICE_UNKNOWN)) { + pa_log_debug("Bluetooth daemon is apparently not available."); + remove_all_devices(y); + goto finish; + } + if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) { pa_log("Error from ListDevices reply: %s", dbus_message_get_error_name(r)); - goto end; + goto finish; } if (!dbus_message_get_args(r, &e, DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &paths, &num, DBUS_TYPE_INVALID)) { @@ -460,7 +481,7 @@ static void list_devices_reply(DBusPendingCall *pending, void *userdata) { found_device(y, paths[i]); } -end: +finish: if (paths) dbus_free_string_array (paths); @@ -470,57 +491,11 @@ end: pa_dbus_pending_free(p); } -static void find_device_reply(DBusPendingCall *pending, void *userdata) { - DBusError e; - DBusMessage *r; - char *path = NULL; - pa_dbus_pending *p; - pa_bluetooth_discovery *y; - - pa_assert(pending); - - dbus_error_init(&e); - - pa_assert_se(p = userdata); - pa_assert_se(y = p->context_data); - pa_assert_se(r = dbus_pending_call_steal_reply(pending)); - - if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) { - pa_log("Error from FindDevice reply: %s", dbus_message_get_error_name(r)); - goto end; - } - - if (!dbus_message_get_args(r, &e, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) { - pa_log("org.bluez.Adapter.FindDevice returned an error: '%s'\n", e.message); - dbus_error_free(&e); - } else - found_device(y, path); - -end: - dbus_message_unref(r); - - PA_LLIST_REMOVE(pa_dbus_pending, y->pending, p); - pa_dbus_pending_free(p); -} - static void found_adapter(pa_bluetooth_discovery *y, const char *path) { DBusMessage *m; - if (y->mode == MODE_FIND) { - pa_assert_se(m = dbus_message_new_method_call("org.bluez", path, "org.bluez.Adapter", "FindDevice")); - - pa_assert_se(dbus_message_append_args(m, - DBUS_TYPE_STRING, &y->looking_for, - DBUS_TYPE_INVALID)); - - send_and_add_to_pending(y, NULL, m, find_device_reply); - - } else { - pa_assert(y->mode == MODE_DISCOVER); - - pa_assert_se(m = dbus_message_new_method_call("org.bluez", path, "org.bluez.Adapter", "ListDevices")); - send_and_add_to_pending(y, NULL, m, list_devices_reply); - } + pa_assert_se(m = dbus_message_new_method_call("org.bluez", path, "org.bluez.Adapter", "ListDevices")); + send_and_add_to_pending(y, NULL, m, list_devices_reply); } static void list_adapters_reply(DBusPendingCall *pending, void *userdata) { @@ -539,9 +514,15 @@ static void list_adapters_reply(DBusPendingCall *pending, void *userdata) { pa_assert_se(y = p->context_data); pa_assert_se(r = dbus_pending_call_steal_reply(pending)); + if (dbus_message_is_error(r, DBUS_ERROR_SERVICE_UNKNOWN)) { + pa_log_debug("Bluetooth daemon is apparently not available."); + remove_all_devices(y); + goto finish; + } + if (dbus_message_get_type(r) == DBUS_MESSAGE_TYPE_ERROR) { pa_log("Error from ListAdapters reply: %s", dbus_message_get_error_name(r)); - goto end; + goto finish; } if (!dbus_message_get_args(r, &e, DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &paths, &num, DBUS_TYPE_INVALID)) { @@ -554,7 +535,7 @@ static void list_adapters_reply(DBusPendingCall *pending, void *userdata) { found_adapter(y, paths[i]); } -end: +finish: if (paths) dbus_free_string_array (paths); @@ -600,11 +581,8 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *us pa_log_debug("Device %s removed", path); if ((d = pa_hashmap_remove(y->devices, path))) { - - pa_assert_se(y->mode == MODE_DISCOVER); - run_callback(y, d, FALSE); - - pa_bluetooth_device_free(d); + run_callback(y, d, TRUE); + device_free(d); } return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; @@ -635,7 +613,8 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *us found_adapter(y, path); return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; - } else if (dbus_message_is_signal(m, "org.bluez.Headset", "PropertyChanged") || + } else if (dbus_message_is_signal(m, "org.bluez.Audio", "PropertyChanged") || + dbus_message_is_signal(m, "org.bluez.Headset", "PropertyChanged") || dbus_message_is_signal(m, "org.bluez.AudioSink", "PropertyChanged") || dbus_message_is_signal(m, "org.bluez.Device", "PropertyChanged")) { @@ -653,19 +632,46 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *us if (parse_device_property(y, d, &arg_i) < 0) goto fail; + } else if (dbus_message_has_interface(m, "org.bluez.Audio")) { + if (parse_audio_property(y, &d->audio_state, &arg_i) < 0) + goto fail; + } else if (dbus_message_has_interface(m, "org.bluez.Headset")) { - if (parse_audio_property(y, &d->headset_connected, &arg_i) < 0) + if (parse_audio_property(y, &d->headset_state, &arg_i) < 0) goto fail; - d->headset_info_valid = 1; } else if (dbus_message_has_interface(m, "org.bluez.AudioSink")) { - if (parse_audio_property(y, &d->audio_sink_connected, &arg_i) < 0) + if (parse_audio_property(y, &d->audio_sink_state, &arg_i) < 0) goto fail; - d->audio_sink_info_valid = 1; } - pa_assert_se(y->mode == MODE_DISCOVER); - run_callback(y, d, TRUE); + run_callback(y, d, FALSE); + } + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + + } else if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameOwnerChanged")) { + const char *name, *old_owner, *new_owner; + + if (!dbus_message_get_args(m, &err, + DBUS_TYPE_STRING, &name, + DBUS_TYPE_STRING, &old_owner, + DBUS_TYPE_STRING, &new_owner, + DBUS_TYPE_INVALID)) { + pa_log("Failed to parse org.freedesktop.DBus.NameOwnerChanged: %s", err.message); + goto fail; + } + + if (pa_streq(name, "org.bluez")) { + if (old_owner && *old_owner) { + pa_log_debug("Bluetooth daemon disappeared."); + remove_all_devices(y); + } + + if (new_owner && *new_owner) { + pa_log_debug("Bluetooth daemon appeared."); + list_adapters(y); + } } return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; @@ -677,86 +683,87 @@ fail: return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } -pa_bluetooth_device* pa_bluetooth_find_device(DBusConnection *c, const char* address) { - pa_bluetooth_discovery y; - - memset(&y, 0, sizeof(y)); - y.mode = MODE_FIND; - y.looking_for = address; - y.connection = c; - PA_LLIST_HEAD_INIT(pa_dbus_pending, y.pending); +const pa_bluetooth_device* pa_bluetooth_discovery_get_by_address(pa_bluetooth_discovery *y, const char* address) { + pa_bluetooth_device *d; + void *state = NULL; - list_adapters(&y); + pa_assert(y); + pa_assert(PA_REFCNT_VALUE(y) > 0); + pa_assert(address); - pa_dbus_sync_pending_list(&y.pending); - pa_assert(!y.pending); + if (!pa_hook_is_firing(&y->hook)) + pa_bluetooth_discovery_sync(y); - if (y.found_device) { - pa_assert(device_is_loaded(y.found_device)); + while ((d = pa_hashmap_iterate(y->devices, &state, NULL))) + if (pa_streq(d->address, address)) + return d; - if (!device_is_audio(y.found_device)) { - pa_bluetooth_device_free(y.found_device); - return NULL; - } - } - - return y.found_device; + return NULL; } -pa_bluetooth_device* pa_bluetooth_get_device(DBusConnection *c, const char* path) { - pa_bluetooth_discovery y; +const pa_bluetooth_device* pa_bluetooth_discovery_get_by_path(pa_bluetooth_discovery *y, const char* path) { + pa_assert(y); + pa_assert(PA_REFCNT_VALUE(y) > 0); + pa_assert(path); + + if (!pa_hook_is_firing(&y->hook)) + pa_bluetooth_discovery_sync(y); - memset(&y, 0, sizeof(y)); - y.mode = MODE_GET; - y.connection = c; - PA_LLIST_HEAD_INIT(pa_dbus_pending, y.pending); + return pa_hashmap_get(y->devices, path); +} - found_device(&y, path); +static int setup_dbus(pa_bluetooth_discovery *y) { + DBusError err; - pa_dbus_sync_pending_list(&y.pending); - pa_assert(!y.pending); + dbus_error_init(&err); - if (y.found_device) { - pa_assert(device_is_loaded(y.found_device)); + y->connection = pa_dbus_bus_get(y->core, DBUS_BUS_SYSTEM, &err); - if (!device_is_audio(y.found_device)) { - pa_bluetooth_device_free(y.found_device); - return NULL; - } + if (dbus_error_is_set(&err) || !y->connection) { + pa_log("Failed to get D-Bus connection: %s", err.message); + dbus_error_free(&err); + return -1; } - return y.found_device; + return 0; } -pa_bluetooth_discovery* pa_bluetooth_discovery_new(DBusConnection *c, pa_bluetooth_device_callback_t cb, struct userdata *u) { +pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *c) { DBusError err; pa_bluetooth_discovery *y; pa_assert(c); - pa_assert(cb); dbus_error_init(&err); + if ((y = pa_shared_get(c, "bluetooth-discovery"))) + return pa_bluetooth_discovery_ref(y); + y = pa_xnew0(pa_bluetooth_discovery, 1); - y->mode = MODE_DISCOVER; - y->connection = c; - y->callback = cb; - y->userdata = u; + PA_REFCNT_INIT(y); + y->core = c; y->devices = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func); PA_LLIST_HEAD_INIT(pa_dbus_pending, y->pending); + pa_hook_init(&y->hook, y); + pa_shared_set(c, "bluetooth-discovery", y); + + if (setup_dbus(y) < 0) + goto fail; /* dynamic detection of bluetooth audio devices */ - if (!dbus_connection_add_filter(c, filter_cb, y, NULL)) { + if (!dbus_connection_add_filter(pa_dbus_connection_get(y->connection), filter_cb, y, NULL)) { pa_log_error("Failed to add filter function"); goto fail; } if (pa_dbus_add_matches( - c, &err, + pa_dbus_connection_get(y->connection), &err, + "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged'", "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'", "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'", "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'", "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'", + "type='signal',sender='org.bluez',interface='org.bluez.Audio',member='PropertyChanged'", "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'", "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'", NULL) < 0) { pa_log("Failed to add D-Bus matches: %s", err.message); @@ -768,46 +775,77 @@ pa_bluetooth_discovery* pa_bluetooth_discovery_new(DBusConnection *c, pa_bluetoo return y; fail: + + if (y) + pa_bluetooth_discovery_unref(y); + dbus_error_free(&err); + return NULL; } -void pa_bluetooth_discovery_free(pa_bluetooth_discovery *y) { - pa_bluetooth_device *d; +pa_bluetooth_discovery* pa_bluetooth_discovery_ref(pa_bluetooth_discovery *y) { + pa_assert(y); + pa_assert(PA_REFCNT_VALUE(y) > 0); + + PA_REFCNT_INC(y); + + return y; +} +void pa_bluetooth_discovery_unref(pa_bluetooth_discovery *y) { pa_assert(y); + pa_assert(PA_REFCNT_VALUE(y) > 0); + + if (PA_REFCNT_DEC(y) > 0) + return; pa_dbus_free_pending_list(&y->pending); if (y->devices) { - while ((d = pa_hashmap_steal_first(y->devices))) { - run_callback(y, d, FALSE); - pa_bluetooth_device_free(d); - } - + remove_all_devices(y); pa_hashmap_free(y->devices, NULL, NULL); } if (y->connection) { - pa_dbus_remove_matches(y->connection, + pa_dbus_remove_matches(pa_dbus_connection_get(y->connection), + "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged'", "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterAdded'", "type='signal',sender='org.bluez',interface='org.bluez.Manager',member='AdapterRemoved'", "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'", "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'", "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'", + "type='signal',sender='org.bluez',interface='org.bluez.Audio',member='PropertyChanged'", "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'", "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'", NULL); - dbus_connection_remove_filter(y->connection, filter_cb, y); + dbus_connection_remove_filter(pa_dbus_connection_get(y->connection), filter_cb, y); + + pa_dbus_connection_unref(y->connection); } + + pa_hook_done(&y->hook); + + if (y->core) + pa_shared_remove(y->core, "bluetooth-discovery"); + + pa_xfree(y); } void pa_bluetooth_discovery_sync(pa_bluetooth_discovery *y) { pa_assert(y); + pa_assert(PA_REFCNT_VALUE(y) > 0); pa_dbus_sync_pending_list(&y->pending); } +pa_hook* pa_bluetooth_discovery_hook(pa_bluetooth_discovery *y) { + pa_assert(y); + pa_assert(PA_REFCNT_VALUE(y) > 0); + + return &y->hook; +} + const char*pa_bluetooth_get_form_factor(uint32_t class) { unsigned i; const char *r; @@ -867,3 +905,16 @@ char *pa_bluetooth_cleanup_name(const char *name) { return t; } + +pa_bool_t pa_bluetooth_uuid_has(pa_bluetooth_uuid *uuids, const char *uuid) { + pa_assert(uuid); + + while (uuids) { + if (strcasecmp(uuids->uuid, uuid) == 0) + return TRUE; + + uuids = uuids->next; + } + + return FALSE; +} diff --git a/src/modules/bluetooth/bluetooth-util.h b/src/modules/bluetooth/bluetooth-util.h index 0364c972..265caf40 100644 --- a/src/modules/bluetooth/bluetooth-util.h +++ b/src/modules/bluetooth/bluetooth-util.h @@ -28,6 +28,20 @@ #include <pulsecore/macro.h> #include <pulsecore/core-util.h> +/* UUID copied from bluez/audio/device.h */ +#define GENERIC_AUDIO_UUID "00001203-0000-1000-8000-00805F9B34FB" + +#define HSP_HS_UUID "00001108-0000-1000-8000-00805F9B34FB" +#define HSP_AG_UUID "00001112-0000-1000-8000-00805F9B34FB" + +#define HFP_HS_UUID "0000111E-0000-1000-8000-00805F9B34FB" +#define HFP_AG_UUID "0000111F-0000-1000-8000-00805F9B34FB" + +#define ADVANCED_AUDIO_UUID "0000110D-0000-1000-8000-00805F9B34FB" + +#define A2DP_SOURCE_UUID "0000110A-0000-1000-8000-00805F9B34FB" +#define A2DP_SINK_UUID "0000110B-0000-1000-8000-00805F9B34FB" + typedef struct pa_bluetooth_uuid pa_bluetooth_uuid; typedef struct pa_bluetooth_device pa_bluetooth_device; typedef struct pa_bluetooth_discovery pa_bluetooth_discovery; @@ -39,12 +53,20 @@ struct pa_bluetooth_uuid { PA_LLIST_FIELDS(pa_bluetooth_uuid); }; +/* This enum is shared among Audio, Headset, and AudioSink, although not all values are acceptable in all profiles */ +typedef enum pa_bt_audio_state { + PA_BT_AUDIO_STATE_INVALID = -1, + PA_BT_AUDIO_STATE_DISCONNECTED, + PA_BT_AUDIO_STATE_CONNECTING, + PA_BT_AUDIO_STATE_CONNECTED, + PA_BT_AUDIO_STATE_PLAYING, + PA_BT_AUDIO_STATE_LAST +} pa_bt_audio_state_t; + struct pa_bluetooth_device { - void *data; /* arbitrary information for the one owning the discovery object */ + pa_bool_t dead; int device_info_valid; /* 0: no results yet; 1: good results; -1: bad results ... */ - int audio_sink_info_valid; /* ... same here ... */ - int headset_info_valid; /* ... and here */ /* Device information */ char *name; @@ -57,25 +79,31 @@ struct pa_bluetooth_device { int class; int trusted; - /* AudioSink information */ - int audio_sink_connected; + /* Audio state */ + pa_bt_audio_state_t audio_state; - /* Headset information */ - int headset_connected; -}; + /* AudioSink state */ + pa_bt_audio_state_t audio_sink_state; -void pa_bluetooth_device_free(pa_bluetooth_device *d); + /* Headset state */ + pa_bt_audio_state_t headset_state; +}; -pa_bluetooth_device* pa_bluetooth_get_device(DBusConnection *c, const char* path); -pa_bluetooth_device* pa_bluetooth_find_device(DBusConnection *c, const char* address); +pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *core); +pa_bluetooth_discovery* pa_bluetooth_discovery_ref(pa_bluetooth_discovery *y); +void pa_bluetooth_discovery_unref(pa_bluetooth_discovery *d); -typedef void (*pa_bluetooth_device_callback_t)(struct userdata *u, pa_bluetooth_device *d, pa_bool_t good); -pa_bluetooth_discovery* pa_bluetooth_discovery_new(DBusConnection *c, pa_bluetooth_device_callback_t cb, struct userdata *u); -void pa_bluetooth_discovery_free(pa_bluetooth_discovery *d); void pa_bluetooth_discovery_sync(pa_bluetooth_discovery *d); -const char*pa_bluetooth_get_form_factor(uint32_t class); +const pa_bluetooth_device* pa_bluetooth_discovery_get_by_path(pa_bluetooth_discovery *d, const char* path); +const pa_bluetooth_device* pa_bluetooth_discovery_get_by_address(pa_bluetooth_discovery *d, const char* address); + +pa_hook* pa_bluetooth_discovery_hook(pa_bluetooth_discovery *d); + +const char* pa_bluetooth_get_form_factor(uint32_t class); char *pa_bluetooth_cleanup_name(const char *name); +pa_bool_t pa_bluetooth_uuid_has(pa_bluetooth_uuid *uuids, const char *uuid); + #endif diff --git a/src/modules/bluetooth/ipc.c b/src/modules/bluetooth/ipc.c index f14c92c4..dcecad8a 100644 --- a/src/modules/bluetooth/ipc.c +++ b/src/modules/bluetooth/ipc.c @@ -35,6 +35,7 @@ static const char *strtypes[] = { /* This table contains the string representation for messages names */ static const char *strnames[] = { "BT_GET_CAPABILITIES", + "BT_OPEN", "BT_SET_CONFIGURATION", "BT_NEW_STREAM", "BT_START_STREAM", diff --git a/src/modules/bluetooth/ipc.h b/src/modules/bluetooth/ipc.h index f030acfa..2e170f50 100644 --- a/src/modules/bluetooth/ipc.h +++ b/src/modules/bluetooth/ipc.h @@ -71,7 +71,7 @@ extern "C" { #include <sys/un.h> #include <errno.h> -#define BT_SUGGESTED_BUFFER_SIZE 128 +#define BT_SUGGESTED_BUFFER_SIZE 512 #define BT_IPC_SOCKET_NAME "\0/org/bluez/audio" /* Generic message header definition, except for RESPONSE messages */ @@ -94,10 +94,12 @@ typedef struct { /* Messages names */ #define BT_GET_CAPABILITIES 0 -#define BT_SET_CONFIGURATION 1 -#define BT_NEW_STREAM 2 -#define BT_START_STREAM 3 -#define BT_STOP_STREAM 4 +#define BT_OPEN 1 +#define BT_SET_CONFIGURATION 2 +#define BT_NEW_STREAM 3 +#define BT_START_STREAM 4 +#define BT_STOP_STREAM 5 +#define BT_CLOSE 6 #define BT_CONTROL 7 #define BT_CAPABILITIES_TRANSPORT_A2DP 0 @@ -112,19 +114,31 @@ typedef struct { struct bt_get_capabilities_req { bt_audio_msg_header_t h; - char device[18]; /* Address of the remote Device */ + char source[18]; /* Address of the local Device */ + char destination[18];/* Address of the remote Device */ + char object[128]; /* DBus object path */ uint8_t transport; /* Requested transport */ uint8_t flags; /* Requested flags */ + uint8_t seid; /* Requested capability configuration */ } __attribute__ ((packed)); /** - * SBC Codec parameters as per A2DP profile 1.0 § 4.3 + * SBC Codec parameters as per A2DP profile 1.0 § 4.3 */ -#define BT_A2DP_CODEC_SBC 0x00 -#define BT_A2DP_CODEC_MPEG12 0x01 -#define BT_A2DP_CODEC_MPEG24 0x02 -#define BT_A2DP_CODEC_ATRAC 0x03 +/* A2DP seid are 6 bytes long so HSP/HFP are assigned to 7-8 bits */ +#define BT_A2DP_SEID_RANGE (1 << 6) - 1 + +#define BT_A2DP_SBC_SOURCE 0x00 +#define BT_A2DP_SBC_SINK 0x01 +#define BT_A2DP_MPEG12_SOURCE 0x02 +#define BT_A2DP_MPEG12_SINK 0x03 +#define BT_A2DP_MPEG24_SOURCE 0x04 +#define BT_A2DP_MPEG24_SINK 0x05 +#define BT_A2DP_ATRAC_SOURCE 0x06 +#define BT_A2DP_ATRAC_SINK 0x07 +#define BT_A2DP_UNKNOWN_SOURCE 0x08 +#define BT_A2DP_UNKNOWN_SINK 0x09 #define BT_SBC_SAMPLING_FREQ_16000 (1 << 3) #define BT_SBC_SAMPLING_FREQ_32000 (1 << 2) @@ -163,10 +177,16 @@ struct bt_get_capabilities_req { #define BT_PCM_FLAG_NREC 0x01 #define BT_PCM_FLAG_PCM_ROUTING 0x02 +#define BT_WRITE_LOCK (1 << 1) +#define BT_READ_LOCK 1 + typedef struct { + uint8_t seid; uint8_t transport; uint8_t type; uint8_t length; + uint8_t configured; + uint8_t lock; uint8_t data[0]; } __attribute__ ((packed)) codec_capabilities_t; @@ -199,20 +219,35 @@ typedef struct { struct bt_get_capabilities_rsp { bt_audio_msg_header_t h; + char source[18]; /* Address of the local Device */ + char destination[18];/* Address of the remote Device */ + char object[128]; /* DBus object path */ uint8_t data[0]; /* First codec_capabilities_t */ } __attribute__ ((packed)); +struct bt_open_req { + bt_audio_msg_header_t h; + char source[18]; /* Address of the local Device */ + char destination[18];/* Address of the remote Device */ + char object[128]; /* DBus object path */ + uint8_t seid; /* Requested capability configuration to lock */ + uint8_t lock; /* Requested lock */ +} __attribute__ ((packed)); + +struct bt_open_rsp { + bt_audio_msg_header_t h; + char source[18]; /* Address of the local Device */ + char destination[18];/* Address of the remote Device */ + char object[128]; /* DBus object path */ +} __attribute__ ((packed)); + struct bt_set_configuration_req { bt_audio_msg_header_t h; - char device[18]; /* Address of the remote Device */ - uint8_t access_mode; /* Requested access mode */ codec_capabilities_t codec; /* Requested codec */ } __attribute__ ((packed)); struct bt_set_configuration_rsp { bt_audio_msg_header_t h; - uint8_t transport; /* Granted transport */ - uint8_t access_mode; /* Granted access mode */ uint16_t link_mtu; /* Max length that transport supports */ } __attribute__ ((packed)); @@ -241,6 +276,14 @@ struct bt_stop_stream_rsp { bt_audio_msg_header_t h; } __attribute__ ((packed)); +struct bt_close_req { + bt_audio_msg_header_t h; +} __attribute__ ((packed)); + +struct bt_close_rsp { + bt_audio_msg_header_t h; +} __attribute__ ((packed)); + struct bt_suspend_stream_ind { bt_audio_msg_header_t h; } __attribute__ ((packed)); diff --git a/src/modules/bluetooth/module-bluetooth-device.c b/src/modules/bluetooth/module-bluetooth-device.c index 93673cb9..609efd17 100644 --- a/src/modules/bluetooth/module-bluetooth-device.c +++ b/src/modules/bluetooth/module-bluetooth-device.c @@ -30,13 +30,15 @@ #include <linux/sockios.h> #include <arpa/inet.h> -#include <pulse/xmalloc.h> -#include <pulse/timeval.h> -#include <pulse/sample.h> #include <pulse/i18n.h> +#include <pulse/rtclock.h> +#include <pulse/sample.h> +#include <pulse/timeval.h> +#include <pulse/xmalloc.h> #include <pulsecore/module.h> #include <pulsecore/modargs.h> +#include <pulsecore/core-rtclock.h> #include <pulsecore/core-util.h> #include <pulsecore/core-error.h> #include <pulsecore/socket-util.h> @@ -44,10 +46,8 @@ #include <pulsecore/thread-mq.h> #include <pulsecore/rtpoll.h> #include <pulsecore/time-smoother.h> -#include <pulsecore/rtclock.h> #include <pulsecore/namereg.h> - -#include <modules/dbus-util.h> +#include <pulsecore/dbus-shared.h> #include "module-bluetooth-device-symdef.h" #include "ipc.h" @@ -65,32 +65,43 @@ PA_MODULE_LOAD_ONCE(FALSE); PA_MODULE_USAGE( "name=<name for the card/sink/source, to be prefixed> " "card_name=<name for the card> " + "card_properties=<properties for the card> " "sink_name=<name for the sink> " + "sink_properties=<properties for the sink> " "source_name=<name for the source> " + "source_properties=<properties for the source> " "address=<address of the device> " "profile=<a2dp|hsp> " "rate=<sample rate> " "channels=<number of channels> " - "path=<device object path> " + "path=<device object path>"); + +/* +#ifdef NOKIA "sco_sink=<SCO over PCM sink name> " - "sco_source=<SCO over PCM source name>"); + "sco_source=<SCO over PCM source name>" +#endif +*/ /* TODO: not close fd when entering suspend mode in a2dp */ -/* TODO: BT_PCM_FLAG_NREC */ - static const char* const valid_modargs[] = { "name", "card_name", + "card_properties", "sink_name", + "sink_properties", "source_name", + "source_properties", "address", "profile", "rate", "channels", "path", +#ifdef NOKIA "sco_sink", "sco_source", +#endif NULL }; @@ -98,7 +109,7 @@ struct a2dp_info { sbc_capabilities_t sbc_capabilities; sbc_t sbc; /* Codec data */ pa_bool_t sbc_initialized; /* Keep track if the encoder is initialized */ - size_t codesize; /* SBC codesize */ + size_t codesize, frame_length; /* SBC Codesize, frame_length. We simply cache those values here */ void* buffer; /* Codec transfer buffer */ size_t buffer_size; /* Size of the buffer */ @@ -108,8 +119,10 @@ struct a2dp_info { struct hsp_info { pcm_capabilities_t pcm_capabilities; +#ifdef NOKIA pa_sink *sco_sink; pa_source *sco_source; +#endif pa_hook_slot *sink_state_changed_slot; pa_hook_slot *source_state_changed_slot; }; @@ -124,6 +137,12 @@ struct userdata { pa_core *core; pa_module *module; + char *address; + char *path; + pa_bluetooth_discovery *discovery; + + pa_dbus_connection *connection; + pa_card *card; pa_sink *sink; pa_source *source; @@ -149,19 +168,24 @@ struct userdata { struct a2dp_info a2dp; struct hsp_info hsp; - pa_dbus_connection *connection; enum profile profile; pa_modargs *modargs; - pa_bluetooth_device *device; - - int stream_write_type, stream_read_type; + int stream_write_type; int service_write_type, service_read_type; }; +#define FIXED_LATENCY_PLAYBACK_A2DP (25*PA_USEC_PER_MSEC) +#define FIXED_LATENCY_PLAYBACK_HSP (125*PA_USEC_PER_MSEC) +#define FIXED_LATENCY_RECORD_HSP (25*PA_USEC_PER_MSEC) + +#define MAX_PLAYBACK_CATCH_UP_USEC (100*PA_USEC_PER_MSEC) + +#ifdef NOKIA #define USE_SCO_OVER_PCM(u) (u->profile == PROFILE_HSP && (u->hsp.sco_sink && u->hsp.sco_source)) +#endif static int init_bt(struct userdata *u); static int init_profile(struct userdata *u); @@ -264,7 +288,8 @@ static ssize_t service_expect(struct userdata*u, bt_audio_msg_header_t *rsp, siz return 0; } -static int parse_caps(struct userdata *u, const struct bt_get_capabilities_rsp *rsp) { +/* Run from main thread */ +static int parse_caps(struct userdata *u, uint8_t seid, const struct bt_get_capabilities_rsp *rsp) { uint16_t bytes_left; const codec_capabilities_t *codec; @@ -295,12 +320,15 @@ static int parse_caps(struct userdata *u, const struct bt_get_capabilities_rsp * pa_assert(codec->type == BT_HFP_CODEC_PCM); + if (codec->configured && seid == 0) + return codec->seid; + memcpy(&u->hsp.pcm_capabilities, codec, sizeof(u->hsp.pcm_capabilities)); } else if (u->profile == PROFILE_A2DP) { while (bytes_left > 0) { - if (codec->type == BT_A2DP_CODEC_SBC) + if ((codec->type == BT_A2DP_SBC_SINK) && !codec->lock) break; bytes_left -= codec->length; @@ -310,7 +338,10 @@ static int parse_caps(struct userdata *u, const struct bt_get_capabilities_rsp * if (bytes_left <= 0 || codec->length != sizeof(u->a2dp.sbc_capabilities)) return -1; - pa_assert(codec->type == BT_A2DP_CODEC_SBC); + pa_assert(codec->type == BT_A2DP_SBC_SINK); + + if (codec->configured && seid == 0) + return codec->seid; memcpy(&u->a2dp.sbc_capabilities, codec, sizeof(u->a2dp.sbc_capabilities)); } @@ -318,13 +349,15 @@ static int parse_caps(struct userdata *u, const struct bt_get_capabilities_rsp * return 0; } -static int get_caps(struct userdata *u) { +/* Run from main thread */ +static int get_caps(struct userdata *u, uint8_t seid) { union { struct bt_get_capabilities_req getcaps_req; struct bt_get_capabilities_rsp getcaps_rsp; bt_audio_error_t error; uint8_t buf[BT_SUGGESTED_BUFFER_SIZE]; } msg; + int ret; pa_assert(u); @@ -332,8 +365,9 @@ static int get_caps(struct userdata *u) { msg.getcaps_req.h.type = BT_REQUEST; msg.getcaps_req.h.name = BT_GET_CAPABILITIES; msg.getcaps_req.h.length = sizeof(msg.getcaps_req); + msg.getcaps_req.seid = seid; - pa_strlcpy(msg.getcaps_req.device, u->device->address, sizeof(msg.getcaps_req.device)); + pa_strlcpy(msg.getcaps_req.object, u->path, sizeof(msg.getcaps_req.object)); if (u->profile == PROFILE_A2DP) msg.getcaps_req.transport = BT_CAPABILITIES_TRANSPORT_A2DP; else { @@ -348,9 +382,14 @@ static int get_caps(struct userdata *u) { if (service_expect(u, &msg.getcaps_rsp.h, sizeof(msg), BT_GET_CAPABILITIES, 0) < 0) return -1; - return parse_caps(u, &msg.getcaps_rsp); + ret = parse_caps(u, seid, &msg.getcaps_rsp); + if (ret <= 0) + return ret; + + return get_caps(u, ret); } +/* Run from main thread */ static uint8_t a2dp_default_bitpool(uint8_t freq, uint8_t mode) { switch (freq) { @@ -396,6 +435,7 @@ static uint8_t a2dp_default_bitpool(uint8_t freq, uint8_t mode) { } } +/* Run from main thread */ static int setup_a2dp(struct userdata *u) { sbc_capabilities_t *cap; int i; @@ -424,8 +464,8 @@ static int setup_a2dp(struct userdata *u) { break; } - if ((unsigned) i >= PA_ELEMENTSOF(freq_table)) { - for (; i >= 0; i--) { + if ((unsigned) i == PA_ELEMENTSOF(freq_table)) { + for (--i; i >= 0; i--) { if (cap->frequency & freq_table[i].cap) { u->sample_spec.rate = freq_table[i].rate; cap->frequency = freq_table[i].cap; @@ -439,6 +479,11 @@ static int setup_a2dp(struct userdata *u) { } } + pa_assert((unsigned) i < PA_ELEMENTSOF(freq_table)); + + if (cap->capability.configured) + return 0; + if (u->sample_spec.channels <= 1) { if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_MONO) { cap->channel_mode = BT_A2DP_CHANNEL_MODE_MONO; @@ -498,6 +543,7 @@ static int setup_a2dp(struct userdata *u) { return 0; } +/* Run from main thread */ static void setup_sbc(struct a2dp_info *a2dp) { sbc_capabilities_t *active_capabilities; @@ -585,17 +631,36 @@ static void setup_sbc(struct a2dp_info *a2dp) { } a2dp->sbc.bitpool = active_capabilities->max_bitpool; - a2dp->codesize = (uint16_t) sbc_get_codesize(&a2dp->sbc); + a2dp->codesize = sbc_get_codesize(&a2dp->sbc); + a2dp->frame_length = sbc_get_frame_length(&a2dp->sbc); } +/* Run from main thread */ static int set_conf(struct userdata *u) { union { + struct bt_open_req open_req; + struct bt_open_rsp open_rsp; struct bt_set_configuration_req setconf_req; struct bt_set_configuration_rsp setconf_rsp; bt_audio_error_t error; uint8_t buf[BT_SUGGESTED_BUFFER_SIZE]; } msg; + memset(&msg, 0, sizeof(msg)); + msg.open_req.h.type = BT_REQUEST; + msg.open_req.h.name = BT_OPEN; + msg.open_req.h.length = sizeof(msg.open_req); + + pa_strlcpy(msg.open_req.object, u->path, sizeof(msg.open_req.object)); + msg.open_req.seid = u->profile == PROFILE_A2DP ? u->a2dp.sbc_capabilities.capability.seid : BT_A2DP_SEID_RANGE + 1; + msg.open_req.lock = u->profile == PROFILE_A2DP ? BT_WRITE_LOCK : BT_READ_LOCK | BT_WRITE_LOCK; + + if (service_send(u, &msg.open_req.h) < 0) + return -1; + + if (service_expect(u, &msg.open_rsp.h, sizeof(msg), BT_OPEN, sizeof(msg.open_rsp)) < 0) + return -1; + if (u->profile == PROFILE_A2DP ) { u->sample_spec.format = PA_SAMPLE_S16LE; @@ -614,15 +679,14 @@ static int set_conf(struct userdata *u) { msg.setconf_req.h.name = BT_SET_CONFIGURATION; msg.setconf_req.h.length = sizeof(msg.setconf_req); - pa_strlcpy(msg.setconf_req.device, u->device->address, sizeof(msg.setconf_req.device)); - msg.setconf_req.access_mode = u->profile == PROFILE_A2DP ? BT_CAPABILITIES_ACCESS_MODE_WRITE : BT_CAPABILITIES_ACCESS_MODE_READWRITE; - - msg.setconf_req.codec.transport = u->profile == PROFILE_A2DP ? BT_CAPABILITIES_TRANSPORT_A2DP : BT_CAPABILITIES_TRANSPORT_SCO; - if (u->profile == PROFILE_A2DP) { memcpy(&msg.setconf_req.codec, &u->a2dp.sbc_capabilities, sizeof(u->a2dp.sbc_capabilities)); - msg.setconf_req.h.length += msg.setconf_req.codec.length - sizeof(msg.setconf_req.codec); + } else { + msg.setconf_req.codec.transport = BT_CAPABILITIES_TRANSPORT_SCO; + msg.setconf_req.codec.seid = BT_A2DP_SEID_RANGE + 1; + msg.setconf_req.codec.length = sizeof(pcm_capabilities_t); } + msg.setconf_req.h.length += msg.setconf_req.codec.length - sizeof(msg.setconf_req.codec); if (service_send(u, &msg.setconf_req.h) < 0) return -1; @@ -630,24 +694,17 @@ static int set_conf(struct userdata *u) { if (service_expect(u, &msg.setconf_rsp.h, sizeof(msg), BT_SET_CONFIGURATION, sizeof(msg.setconf_rsp)) < 0) return -1; - if ((u->profile == PROFILE_A2DP && msg.setconf_rsp.transport != BT_CAPABILITIES_TRANSPORT_A2DP) || - (u->profile == PROFILE_HSP && msg.setconf_rsp.transport != BT_CAPABILITIES_TRANSPORT_SCO)) { - pa_log("Transport doesn't match what we requested."); - return -1; - } - - if ((u->profile == PROFILE_A2DP && msg.setconf_rsp.access_mode != BT_CAPABILITIES_ACCESS_MODE_WRITE) || - (u->profile == PROFILE_HSP && msg.setconf_rsp.access_mode != BT_CAPABILITIES_ACCESS_MODE_READWRITE)) { - pa_log("Access mode doesn't match what we requested."); - return -1; - } - u->link_mtu = msg.setconf_rsp.link_mtu; /* setup SBC encoder now we agree on parameters */ if (u->profile == PROFILE_A2DP) { setup_sbc(&u->a2dp); - u->block_size = u->a2dp.codesize; + + u->block_size = + ((u->link_mtu - sizeof(struct rtp_header) - sizeof(struct rtp_payload)) + / u->a2dp.frame_length + * u->a2dp.codesize); + pa_log_info("SBC parameters:\n\tallocation=%u\n\tsubbands=%u\n\tblocks=%u\n\tbitpool=%u\n", u->a2dp.sbc.allocation, u->a2dp.sbc.subbands, u->a2dp.sbc.blocks, u->a2dp.sbc.bitpool); } else @@ -667,6 +724,7 @@ static int start_stream_fd(struct userdata *u) { uint8_t buf[BT_SUGGESTED_BUFFER_SIZE]; } msg; struct pollfd *pollfd; + int one; pa_assert(u); pa_assert(u->rtpoll); @@ -695,13 +753,29 @@ static int start_stream_fd(struct userdata *u) { pa_make_fd_nonblock(u->stream_fd); pa_make_socket_low_delay(u->stream_fd); + one = 1; + if (setsockopt(u->stream_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one)) < 0) + pa_log_warn("Failed to enable SO_TIMESTAMP: %s", pa_cstrerror(errno)); + + pa_log_debug("Stream properly set up, we're ready to roll!"); + u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1); pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL); pollfd->fd = u->stream_fd; pollfd->events = pollfd->revents = 0; - u->read_index = 0; - u->write_index = 0; + u->read_index = u->write_index = 0; + u->started_at = 0; + + if (u->source) + u->read_smoother = pa_smoother_new( + PA_USEC_PER_SEC, + PA_USEC_PER_SEC*2, + TRUE, + TRUE, + 10, + pa_rtclock_now(), + TRUE); return 0; } @@ -737,9 +811,15 @@ static int stop_stream_fd(struct userdata *u) { pa_close(u->stream_fd); u->stream_fd = -1; + if (u->read_smoother) { + pa_smoother_free(u->read_smoother); + u->read_smoother = NULL; + } + return r; } +/* Run from IO thread */ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { struct userdata *u = PA_SINK(o)->userdata; pa_bool_t failed = FALSE; @@ -747,7 +827,6 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse pa_assert(u->sink == PA_SINK(o)); - pa_log_debug("got message: %d", code); switch (code) { case PA_SINK_MESSAGE_SET_STATE: @@ -775,8 +854,6 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse if (!u->source || u->source->state == PA_SOURCE_SUSPENDED) if (start_stream_fd(u) < 0) failed = TRUE; - - u->started_at = pa_rtclock_usec(); break; case PA_SINK_UNLINKED: @@ -787,7 +864,24 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse break; case PA_SINK_MESSAGE_GET_LATENCY: { - *((pa_usec_t*) data) = 0; + + if (u->read_smoother) { + pa_usec_t wi, ri; + + ri = pa_smoother_get(u->read_smoother, pa_rtclock_now()); + wi = pa_bytes_to_usec(u->write_index + u->block_size, &u->sample_spec); + + *((pa_usec_t*) data) = wi > ri ? wi - ri : 0; + } else { + pa_usec_t ri, wi; + + ri = pa_rtclock_now() - u->started_at; + wi = pa_bytes_to_usec(u->write_index, &u->sample_spec); + + *((pa_usec_t*) data) = wi > ri ? wi - ri : 0; + } + + *((pa_usec_t*) data) += u->sink->fixed_latency; return 0; } } @@ -797,6 +891,7 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse return (r < 0 || !failed) ? r : -1; } +/* Run from IO thread */ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { struct userdata *u = PA_SOURCE(o)->userdata; pa_bool_t failed = FALSE; @@ -804,7 +899,6 @@ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off pa_assert(u->source == PA_SOURCE(o)); - pa_log_debug("got message: %d", code); switch (code) { case PA_SOURCE_MESSAGE_SET_STATE: @@ -818,7 +912,8 @@ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off if (!u->sink || u->sink->state == PA_SINK_SUSPENDED) stop_stream_fd(u); - pa_smoother_pause(u->read_smoother, pa_rtclock_usec()); + if (u->read_smoother) + pa_smoother_pause(u->read_smoother, pa_rtclock_now()); break; case PA_SOURCE_IDLE: @@ -831,7 +926,8 @@ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off if (start_stream_fd(u) < 0) failed = TRUE; - pa_smoother_resume(u->read_smoother, pa_rtclock_usec()); + /* We don't resume the smoother here. Instead we + * wait until the first packet arrives */ break; case PA_SOURCE_UNLINKED: @@ -842,7 +938,12 @@ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off break; case PA_SOURCE_MESSAGE_GET_LATENCY: { - *((pa_usec_t*) data) = 0; + pa_usec_t wi, ri; + + wi = pa_smoother_get(u->read_smoother, pa_rtclock_now()); + ri = pa_bytes_to_usec(u->read_index, &u->sample_spec); + + *((pa_usec_t*) data) = (wi > ri ? wi - ri : 0) + u->source->fixed_latency; return 0; } @@ -853,54 +954,71 @@ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off return (r < 0 || !failed) ? r : -1; } +/* Run from IO thread */ static int hsp_process_render(struct userdata *u) { int ret = 0; - pa_memchunk memchunk; pa_assert(u); pa_assert(u->profile == PROFILE_HSP); pa_assert(u->sink); - pa_sink_render_full(u->sink, u->block_size, &memchunk); + /* First, render some data */ + if (!u->write_memchunk.memblock) + pa_sink_render_full(u->sink, u->block_size, &u->write_memchunk); + + pa_assert(u->write_memchunk.length == u->block_size); for (;;) { ssize_t l; const void *p; - p = (const uint8_t*) pa_memblock_acquire(memchunk.memblock) + memchunk.index; - l = pa_write(u->stream_fd, p, memchunk.length, &u->stream_write_type); - pa_memblock_release(memchunk.memblock); + /* Now write that data to the socket. The socket is of type + * SEQPACKET, and we generated the data of the MTU size, so this + * should just work. */ - pa_log_debug("Memblock written to socket: %lli bytes", (long long) l); + p = (const uint8_t*) pa_memblock_acquire(u->write_memchunk.memblock) + u->write_memchunk.index; + l = pa_write(u->stream_fd, p, u->write_memchunk.length, &u->stream_write_type); + pa_memblock_release(u->write_memchunk.memblock); pa_assert(l != 0); if (l < 0) { - if (errno == EINTR || errno == EAGAIN) /*** FIXME: EAGAIN handling borked ***/ + + if (errno == EINTR) + /* Retry right away if we got interrupted */ continue; - else { - pa_log_error("Failed to write data to SCO socket: %s", pa_cstrerror(errno)); - ret = -1; + + else if (errno == EAGAIN) + /* Hmm, apparently the socket was not writable, give up for now */ break; - } - } else { - pa_assert((size_t) l <= memchunk.length); - memchunk.index += (size_t) l; - memchunk.length -= (size_t) l; + pa_log_error("Failed to write data to SCO socket: %s", pa_cstrerror(errno)); + ret = -1; + break; + } - u->write_index += (uint64_t) l; + pa_assert((size_t) l <= u->write_memchunk.length); - if (memchunk.length <= 0) - break; + if ((size_t) l != u->write_memchunk.length) { + pa_log_error("Wrote memory block to socket only partially! %llu written, wanted to write %llu.", + (unsigned long long) l, + (unsigned long long) u->write_memchunk.length); + ret = -1; + break; } - } - pa_memblock_unref(memchunk.memblock); + u->write_index += (uint64_t) u->write_memchunk.length; + pa_memblock_unref(u->write_memchunk.memblock); + pa_memchunk_reset(&u->write_memchunk); + + ret = 1; + break; + } return ret; } +/* Run from IO thread */ static int hsp_process_push(struct userdata *u) { int ret = 0; pa_memchunk memchunk; @@ -908,6 +1026,7 @@ static int hsp_process_push(struct userdata *u) { pa_assert(u); pa_assert(u->profile == PROFILE_HSP); pa_assert(u->source); + pa_assert(u->read_smoother); memchunk.memblock = pa_memblock_new(u->core->mempool, u->block_size); memchunk.index = memchunk.length = 0; @@ -915,26 +1034,69 @@ static int hsp_process_push(struct userdata *u) { for (;;) { ssize_t l; void *p; + struct msghdr m; + struct cmsghdr *cm; + uint8_t aux[1024]; + struct iovec iov; + pa_bool_t found_tstamp = FALSE; + pa_usec_t tstamp; + + memset(&m, 0, sizeof(m)); + memset(&aux, 0, sizeof(aux)); + memset(&iov, 0, sizeof(iov)); + + m.msg_iov = &iov; + m.msg_iovlen = 1; + m.msg_control = aux; + m.msg_controllen = sizeof(aux); p = pa_memblock_acquire(memchunk.memblock); - l = pa_read(u->stream_fd, p, pa_memblock_get_length(memchunk.memblock), &u->stream_read_type); + iov.iov_base = p; + iov.iov_len = pa_memblock_get_length(memchunk.memblock); + l = recvmsg(u->stream_fd, &m, 0); pa_memblock_release(memchunk.memblock); if (l <= 0) { - if (l < 0 && (errno == EINTR || errno == EAGAIN)) /*** FIXME: EAGAIN handling borked ***/ + + if (l < 0 && errno == EINTR) + /* Retry right away if we got interrupted */ continue; - else { - pa_log_error("Failed to read data from SCO socket: %s", l < 0 ? pa_cstrerror(errno) : "EOF"); - ret = -1; + + else if (l < 0 && errno == EAGAIN) + /* Hmm, apparently the socket was not readable, give up for now. */ break; - } - } else { - memchunk.length = (size_t) l; - u->read_index += (uint64_t) l; - pa_source_post(u->source, &memchunk); + pa_log_error("Failed to read data from SCO socket: %s", l < 0 ? pa_cstrerror(errno) : "EOF"); + ret = -1; break; } + + pa_assert((size_t) l <= pa_memblock_get_length(memchunk.memblock)); + + memchunk.length = (size_t) l; + u->read_index += (uint64_t) l; + + for (cm = CMSG_FIRSTHDR(&m); cm; cm = CMSG_NXTHDR(&m, cm)) + if (cm->cmsg_level == SOL_SOCKET && cm->cmsg_type == SO_TIMESTAMP) { + struct timeval *tv = (struct timeval*) CMSG_DATA(cm); + pa_rtclock_from_wallclock(tv); + tstamp = pa_timeval_load(tv); + found_tstamp = TRUE; + break; + } + + if (!found_tstamp) { + pa_log_warn("Couldn't find SO_TIMESTAMP data in auxiliary recvmsg() data!"); + tstamp = pa_rtclock_now(); + } + + pa_smoother_put(u->read_smoother, tstamp, pa_bytes_to_usec(u->read_index, &u->sample_spec)); + pa_smoother_resume(u->read_smoother, tstamp, TRUE); + + pa_source_post(u->source, &memchunk); + + ret = 1; + break; } pa_memblock_unref(memchunk.memblock); @@ -942,132 +1104,156 @@ static int hsp_process_push(struct userdata *u) { return ret; } +/* Run from IO thread */ +static void a2dp_prepare_buffer(struct userdata *u) { + pa_assert(u); + + if (u->a2dp.buffer_size >= u->link_mtu) + return; + + u->a2dp.buffer_size = 2 * u->link_mtu; + pa_xfree(u->a2dp.buffer); + u->a2dp.buffer = pa_xmalloc(u->a2dp.buffer_size); +} + +/* Run from IO thread */ static int a2dp_process_render(struct userdata *u) { - size_t frame_size; struct a2dp_info *a2dp; struct rtp_header *header; struct rtp_payload *payload; - size_t left; + size_t nbytes; void *d; const void *p; + size_t to_write, to_encode; unsigned frame_count; - size_t written; - uint64_t writing_at; + int ret = 0; pa_assert(u); pa_assert(u->profile == PROFILE_A2DP); pa_assert(u->sink); - a2dp = &u->a2dp; + /* First, render some data */ + if (!u->write_memchunk.memblock) + pa_sink_render_full(u->sink, u->block_size, &u->write_memchunk); - if (a2dp->buffer_size < u->link_mtu) { - a2dp->buffer_size = 2*u->link_mtu; - pa_xfree(a2dp->buffer); - a2dp->buffer = pa_xmalloc(a2dp->buffer_size); - } + pa_assert(u->write_memchunk.length == u->block_size); - header = (struct rtp_header*) a2dp->buffer; + a2dp_prepare_buffer(u); + + a2dp = &u->a2dp; + header = a2dp->buffer; payload = (struct rtp_payload*) ((uint8_t*) a2dp->buffer + sizeof(*header)); - d = (uint8_t*) a2dp->buffer + sizeof(*header) + sizeof(*payload); - left = a2dp->buffer_size - sizeof(*header) - sizeof(*payload); - frame_size = sbc_get_frame_length(&a2dp->sbc); frame_count = 0; - writing_at = u->write_index; + /* Try to create a packet of the full MTU */ - do { - ssize_t encoded; + p = (const uint8_t*) pa_memblock_acquire(u->write_memchunk.memblock) + u->write_memchunk.index; + to_encode = u->write_memchunk.length; - if (!u->write_memchunk.memblock) - pa_sink_render_full(u->sink, u->block_size, &u->write_memchunk); + d = (uint8_t*) a2dp->buffer + sizeof(*header) + sizeof(*payload); + to_write = a2dp->buffer_size - sizeof(*header) - sizeof(*payload); + + while (PA_LIKELY(to_encode > 0 && to_write > 0)) { + size_t written; + ssize_t encoded; - p = (const uint8_t*) pa_memblock_acquire(u->write_memchunk.memblock) + u->write_memchunk.index; encoded = sbc_encode(&a2dp->sbc, - p, u->write_memchunk.length, - d, left, + p, to_encode, + d, to_write, &written); - PA_ONCE_BEGIN { - pa_log_debug("Using SBC encoder implementation: %s", pa_strnull(sbc_get_implementation_info(&a2dp->sbc))); - } PA_ONCE_END; - - pa_memblock_release(u->write_memchunk.memblock); - - if (encoded <= 0) { - pa_log_error("SBC encoding error (%d)", encoded); + if (PA_UNLIKELY(encoded <= 0)) { + pa_log_error("SBC encoding error (%li)", (long) encoded); + pa_memblock_release(u->write_memchunk.memblock); return -1; } - pa_assert((size_t) encoded <= u->write_memchunk.length); - pa_assert((size_t) encoded == sbc_get_codesize(&a2dp->sbc)); +/* pa_log_debug("SBC: encoded: %lu; written: %lu", (unsigned long) encoded, (unsigned long) written); */ +/* pa_log_debug("SBC: codesize: %lu; frame_length: %lu", (unsigned long) a2dp->codesize, (unsigned long) a2dp->frame_length); */ - pa_assert((size_t) written <= left); - pa_assert((size_t) written == sbc_get_frame_length(&a2dp->sbc)); + pa_assert_fp((size_t) encoded <= to_encode); + pa_assert_fp((size_t) encoded == a2dp->codesize); -/* pa_log_debug("SBC: encoded: %d; written: %d", encoded, written); */ + pa_assert_fp((size_t) written <= to_write); + pa_assert_fp((size_t) written == a2dp->frame_length); - u->write_memchunk.index += encoded; - u->write_memchunk.length -= encoded; - - if (u->write_memchunk.length <= 0) { - pa_memblock_unref(u->write_memchunk.memblock); - pa_memchunk_reset(&u->write_memchunk); - } - - u->write_index += encoded; + p = (const uint8_t*) p + encoded; + to_encode -= encoded; d = (uint8_t*) d + written; - left -= written; + to_write -= written; frame_count++; + } - } while (((uint8_t*) d - ((uint8_t*) a2dp->buffer + sbc_get_frame_length(&a2dp->sbc))) < (ptrdiff_t) u->link_mtu); + pa_memblock_release(u->write_memchunk.memblock); + + pa_assert(to_encode == 0); + + PA_ONCE_BEGIN { + pa_log_debug("Using SBC encoder implementation: %s", pa_strnull(sbc_get_implementation_info(&a2dp->sbc))); + } PA_ONCE_END; /* write it to the fifo */ memset(a2dp->buffer, 0, sizeof(*header) + sizeof(*payload)); - payload->frame_count = frame_count; header->v = 2; header->pt = 1; header->sequence_number = htons(a2dp->seq_num++); - header->timestamp = htonl(writing_at / frame_size); + header->timestamp = htonl(u->write_index / pa_frame_size(&u->sample_spec)); header->ssrc = htonl(1); + payload->frame_count = frame_count; - p = a2dp->buffer; - left = (uint8_t*) d - (uint8_t*) a2dp->buffer; + nbytes = (uint8_t*) d - (uint8_t*) a2dp->buffer; for (;;) { ssize_t l; - l = pa_write(u->stream_fd, p, left, &u->stream_write_type); -/* pa_log_debug("write: requested %lu bytes; written %li bytes; mtu=%li", (unsigned long) left, (long) l, (unsigned long) u->link_mtu); */ + l = pa_write(u->stream_fd, a2dp->buffer, nbytes, &u->stream_write_type); pa_assert(l != 0); if (l < 0) { - if (errno == EINTR || errno == EAGAIN) /*** FIXME: EAGAIN handling borked ***/ - continue; - else { - pa_log_error("Failed to write data to socket: %s", pa_cstrerror(errno)); - return -1; - } - } else { - pa_assert((size_t) l <= left); - d = (uint8_t*) d + l; - left -= l; + if (errno == EINTR) + /* Retry right away if we got interrupted */ + continue; - if (left <= 0) + else if (errno == EAGAIN) + /* Hmm, apparently the socket was not writable, give up for now */ break; + + pa_log_error("Failed to write data to socket: %s", pa_cstrerror(errno)); + ret = -1; + break; + } + + pa_assert((size_t) l <= nbytes); + + if ((size_t) l != nbytes) { + pa_log_warn("Wrote memory block to socket only partially! %llu written, wanted to write %llu.", + (unsigned long long) l, + (unsigned long long) nbytes); + ret = -1; + break; } + + u->write_index += (uint64_t) u->write_memchunk.length; + pa_memblock_unref(u->write_memchunk.memblock); + pa_memchunk_reset(&u->write_memchunk); + + ret = 1; + + break; } - return 0; + return ret; } static void thread_func(void *userdata) { struct userdata *u = userdata; - pa_bool_t do_write = FALSE, writable = FALSE; + unsigned do_write = 0; + pa_bool_t writable = FALSE; pa_assert(u); @@ -1080,9 +1266,6 @@ static void thread_func(void *userdata) { goto fail; pa_thread_mq_install(&u->thread_mq); - pa_rtpoll_install(u->rtpoll); - - pa_smoother_set_time_offset(u->read_smoother, pa_rtclock_usec()); for (;;) { struct pollfd *pollfd; @@ -1093,13 +1276,20 @@ static void thread_func(void *userdata) { if (u->source && PA_SOURCE_IS_LINKED(u->source->thread_info.state)) { + /* We should send two blocks to the device before we expect + * a response. */ + + if (u->write_index == 0 && u->read_index <= 0) + do_write = 2; + if (pollfd && (pollfd->revents & POLLIN)) { + int n_read; - if (hsp_process_push(u) < 0) + if ((n_read = hsp_process_push(u)) < 0) goto fail; /* We just read something, so we are supposed to write something, too */ - do_write = TRUE; + do_write += n_read; } } @@ -1112,44 +1302,70 @@ static void thread_func(void *userdata) { if (pollfd->revents & POLLOUT) writable = TRUE; - if ((!u->source || !PA_SOURCE_IS_LINKED(u->source->thread_info.state)) && !do_write && writable) { + if ((!u->source || !PA_SOURCE_IS_LINKED(u->source->thread_info.state)) && do_write <= 0 && writable) { pa_usec_t time_passed; - uint64_t should_have_written; + pa_usec_t audio_sent; /* Hmm, there is no input stream we could synchronize * to. So let's do things by time */ - time_passed = pa_rtclock_usec() - u->started_at; - should_have_written = pa_usec_to_bytes(time_passed, &u->sink->sample_spec); + time_passed = pa_rtclock_now() - u->started_at; + audio_sent = pa_bytes_to_usec(u->write_index, &u->sample_spec); + + if (audio_sent <= time_passed) { + pa_usec_t audio_to_send = time_passed - audio_sent; + + /* Never try to catch up for more than 100ms */ + if (u->write_index > 0 && audio_to_send > MAX_PLAYBACK_CATCH_UP_USEC) { + pa_usec_t skip_usec; + uint64_t skip_bytes; + pa_memchunk tmp; + + skip_usec = audio_to_send - MAX_PLAYBACK_CATCH_UP_USEC; + skip_bytes = pa_usec_to_bytes(skip_usec, &u->sample_spec); - do_write = u->write_index <= should_have_written ; -/* pa_log_debug("Time has come: %s", pa_yes_no(do_write)); */ + pa_log_warn("Skipping %llu us (= %llu bytes) in audio stream", + (unsigned long long) skip_usec, + (unsigned long long) skip_bytes); + + pa_sink_render_full(u->sink, skip_bytes, &tmp); + pa_memblock_unref(tmp.memblock); + u->write_index += skip_bytes; + } + + do_write = 1; + } } - if (writable && do_write) { - if (u->write_index == 0) - u->started_at = pa_rtclock_usec(); + if (writable && do_write > 0) { + int n_written; + + if (u->write_index <= 0) + u->started_at = pa_rtclock_now(); if (u->profile == PROFILE_A2DP) { - if (a2dp_process_render(u) < 0) + if ((n_written = a2dp_process_render(u)) < 0) goto fail; } else { - if (hsp_process_render(u) < 0) + if ((n_written = hsp_process_render(u)) < 0) goto fail; } - do_write = FALSE; + if (n_written == 0) + pa_log("Broken kernel: we got EAGAIN on write() after POLLOUT!"); + + do_write -= n_written; writable = FALSE; } - if ((!u->source || !PA_SOURCE_IS_LINKED(u->source->thread_info.state)) && !do_write) { + if ((!u->source || !PA_SOURCE_IS_LINKED(u->source->thread_info.state)) && do_write <= 0 && writable) { pa_usec_t time_passed, next_write_at, sleep_for; /* Hmm, there is no input stream we could synchronize * to. So let's estimate when we need to wake up the latest */ - time_passed = pa_rtclock_usec() - u->started_at; - next_write_at = pa_bytes_to_usec(u->write_index, &u->sink->sample_spec); + time_passed = pa_rtclock_now() - u->started_at; + next_write_at = pa_bytes_to_usec(u->write_index, &u->sample_spec); sleep_for = time_passed < next_write_at ? next_write_at - time_passed : 0; /* pa_log("Sleeping for %lu; time passed %lu, next write at %lu", (unsigned long) sleep_for, (unsigned long) time_passed, (unsigned long)next_write_at); */ @@ -1177,7 +1393,11 @@ static void thread_func(void *userdata) { pollfd = u->rtpoll_item ? pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL) : NULL; if (pollfd && (pollfd->revents & ~(POLLOUT|POLLIN))) { - pa_log_error("FD error."); + pa_log_info("FD error: %s%s%s%s", + pollfd->revents & POLLERR ? "POLLERR " :"", + pollfd->revents & POLLHUP ? "POLLHUP " :"", + pollfd->revents & POLLPRI ? "POLLPRI " :"", + pollfd->revents & POLLNVAL ? "POLLNVAL " :""); goto fail; } } @@ -1192,146 +1412,105 @@ finish: pa_log_debug("IO thread shutting down"); } -/* static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *msg, void *userdata) { */ -/* DBusMessageIter arg_i; */ -/* DBusError err; */ -/* const char *value; */ -/* struct userdata *u; */ - -/* pa_assert(bus); */ -/* pa_assert(msg); */ -/* pa_assert(userdata); */ -/* u = userdata; */ - -/* pa_log_debug("dbus: interface=%s, path=%s, member=%s\n", */ -/* dbus_message_get_interface(msg), */ -/* dbus_message_get_path(msg), */ -/* dbus_message_get_member(msg)); */ - -/* dbus_error_init(&err); */ - -/* if (!dbus_message_has_path(msg, u->path)) */ -/* goto done; */ - -/* if (dbus_message_is_signal(msg, "org.bluez.Headset", "PropertyChanged") || */ -/* dbus_message_is_signal(msg, "org.bluez.AudioSink", "PropertyChanged")) { */ - -/* struct device *d; */ -/* const char *profile; */ -/* DBusMessageIter variant_i; */ -/* dbus_uint16_t gain; */ - -/* if (!dbus_message_iter_init(msg, &arg_i)) { */ -/* pa_log("dbus: message has no parameters"); */ -/* goto done; */ -/* } */ +/* Run from main thread */ +static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *userdata) { + DBusError err; + struct userdata *u; -/* if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_STRING) { */ -/* pa_log("Property name not a string."); */ -/* goto done; */ -/* } */ + pa_assert(bus); + pa_assert(m); + pa_assert_se(u = userdata); -/* dbus_message_iter_get_basic(&arg_i, &value); */ + dbus_error_init(&err); -/* if (!dbus_message_iter_next(&arg_i)) { */ -/* pa_log("Property value missing"); */ -/* goto done; */ -/* } */ + pa_log_debug("dbus: interface=%s, path=%s, member=%s\n", + dbus_message_get_interface(m), + dbus_message_get_path(m), + dbus_message_get_member(m)); -/* if (dbus_message_iter_get_arg_type(&arg_i) != DBUS_TYPE_VARIANT) { */ -/* pa_log("Property value not a variant."); */ -/* goto done; */ -/* } */ + if (!dbus_message_has_path(m, u->path)) + goto fail; -/* dbus_message_iter_recurse(&arg_i, &variant_i); */ + if (dbus_message_is_signal(m, "org.bluez.Headset", "SpeakerGainChanged") || + dbus_message_is_signal(m, "org.bluez.Headset", "MicrophoneGainChanged")) { -/* if (dbus_message_iter_get_arg_type(&variant_i) != DBUS_TYPE_UINT16) { */ -/* dbus_message_iter_get_basic(&variant_i, &gain); */ + dbus_uint16_t gain; + pa_cvolume v; -/* if (pa_streq(value, "SpeakerGain")) { */ -/* pa_log("spk gain: %d", gain); */ -/* pa_cvolume_set(&u->sink->virtual_volume, 1, (pa_volume_t) (gain * PA_VOLUME_NORM / 15)); */ -/* pa_subscription_post(u->sink->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, u->sink->index); */ -/* } else { */ -/* pa_log("mic gain: %d", gain); */ -/* if (!u->source) */ -/* goto done; */ + if (!dbus_message_get_args(m, &err, DBUS_TYPE_UINT16, &gain, DBUS_TYPE_INVALID) || gain > 15) { + pa_log("Failed to parse org.bluez.Headset.{Speaker|Microphone}GainChanged: %s", err.message); + goto fail; + } -/* pa_cvolume_set(&u->source->virtual_volume, 1, (pa_volume_t) (gain * PA_VOLUME_NORM / 15)); */ -/* pa_subscription_post(u->source->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, u->source->index); */ -/* } */ -/* } */ -/* } */ + if (u->profile == PROFILE_HSP) { + if (u->sink && dbus_message_is_signal(m, "org.bluez.Headset", "SpeakerGainChanged")) { -/* done: */ -/* dbus_error_free(&err); */ -/* return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; */ -/* } */ + pa_cvolume_set(&v, u->sample_spec.channels, (pa_volume_t) (gain * PA_VOLUME_NORM / 15)); + pa_sink_volume_changed(u->sink, &v, TRUE); -/* static int sink_get_volume_cb(pa_sink *s) { */ -/* struct userdata *u = s->userdata; */ -/* pa_assert(u); */ + } else if (u->source && dbus_message_is_signal(m, "org.bluez.Headset", "MicrophoneGainChanged")) { -/* /\* refresh? *\/ */ + pa_cvolume_set(&v, u->sample_spec.channels, (pa_volume_t) (gain * PA_VOLUME_NORM / 15)); + pa_source_volume_changed(u->source, &v, TRUE); + } + } + } -/* return 0; */ -/* } */ +fail: + dbus_error_free(&err); -/* static int source_get_volume_cb(pa_source *s) { */ -/* struct userdata *u = s->userdata; */ -/* pa_assert(u); */ + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; +} -/* /\* refresh? *\/ */ +/* Run from main thread */ +static void sink_set_volume_cb(pa_sink *s) { + struct userdata *u = s->userdata; + DBusMessage *m; + dbus_uint16_t gain; -/* return 0; */ -/* } */ + pa_assert(u); -/* static int sink_set_volume_cb(pa_sink *s) { */ -/* DBusError e; */ -/* DBusMessage *m, *r; */ -/* DBusMessageIter it, itvar; */ -/* dbus_uint16_t vol; */ -/* const char *spkgain = "SpeakerGain"; */ -/* struct userdata *u = s->userdata; */ -/* pa_assert(u); */ + if (u->profile != PROFILE_HSP) + return; -/* dbus_error_init(&e); */ + gain = (pa_cvolume_max(&s->virtual_volume) * 15) / PA_VOLUME_NORM; -/* vol = ((float) pa_cvolume_max(&s->virtual_volume) / PA_VOLUME_NORM) * 15; */ -/* pa_log_debug("set headset volume: %d", vol); */ + if (gain > 15) + gain = 15; -/* pa_assert_se(m = dbus_message_new_method_call("org.bluez", u->path, "org.bluez.Headset", "SetProperty")); */ -/* dbus_message_iter_init_append(m, &it); */ -/* dbus_message_iter_append_basic(&it, DBUS_TYPE_STRING, &spkgain); */ -/* dbus_message_iter_open_container(&it, DBUS_TYPE_VARIANT, DBUS_TYPE_UINT16_AS_STRING, &itvar); */ -/* dbus_message_iter_append_basic(&itvar, DBUS_TYPE_UINT16, &vol); */ -/* dbus_message_iter_close_container(&it, &itvar); */ + pa_cvolume_set(&s->virtual_volume, u->sample_spec.channels, (pa_volume_t) (gain * PA_VOLUME_NORM / 15)); -/* r = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u->conn), m, -1, &e); */ + pa_assert_se(m = dbus_message_new_method_call("org.bluez", u->path, "org.bluez.Headset", "SetSpeakerGain")); + pa_assert_se(dbus_message_append_args(m, DBUS_TYPE_UINT16, &gain, DBUS_TYPE_INVALID)); + pa_assert_se(dbus_connection_send(pa_dbus_connection_get(u->connection), m, NULL)); + dbus_message_unref(m); +} -/* finish: */ -/* if (m) */ -/* dbus_message_unref(m); */ -/* if (r) */ -/* dbus_message_unref(r); */ +/* Run from main thread */ +static void source_set_volume_cb(pa_source *s) { + struct userdata *u = s->userdata; + DBusMessage *m; + dbus_uint16_t gain; -/* dbus_error_free(&e); */ + pa_assert(u); -/* return 0; */ -/* } */ + if (u->profile != PROFILE_HSP) + return; -/* static int source_set_volume_cb(pa_source *s) { */ -/* dbus_uint16_t vol; */ -/* struct userdata *u = s->userdata; */ -/* pa_assert(u); */ + gain = (pa_cvolume_max(&s->virtual_volume) * 15) / PA_VOLUME_NORM; -/* vol = ((float)pa_cvolume_max(&s->virtual_volume) / PA_VOLUME_NORM) * 15; */ + if (gain > 15) + gain = 15; -/* pa_log_debug("set headset mic volume: %d (not implemented yet)", vol); */ + pa_cvolume_set(&s->virtual_volume, u->sample_spec.channels, (pa_volume_t) (gain * PA_VOLUME_NORM / 15)); -/* return 0; */ -/* } */ + pa_assert_se(m = dbus_message_new_method_call("org.bluez", u->path, "org.bluez.Headset", "SetMicrophoneGain")); + pa_assert_se(dbus_message_append_args(m, DBUS_TYPE_UINT16, &gain, DBUS_TYPE_INVALID)); + pa_assert_se(dbus_connection_send(pa_dbus_connection_get(u->connection), m, NULL)); + dbus_message_unref(m); +} +/* Run from main thread */ static char *get_name(const char *type, pa_modargs *ma, const char *device_id, pa_bool_t *namereg_fail) { char *t; const char *n; @@ -1360,6 +1539,8 @@ static char *get_name(const char *type, pa_modargs *ma, const char *device_id, p return pa_sprintf_malloc("bluez_%s.%s", type, n); } +#ifdef NOKIA + static void sco_over_pcm_state_update(struct userdata *u) { pa_assert(u); pa_assert(USE_SCO_OVER_PCM(u)); @@ -1414,8 +1595,12 @@ static pa_hook_result_t source_state_changed_cb(pa_core *c, pa_source *s, struct return PA_HOOK_OK; } +#endif + +/* Run from main thread */ static int add_sink(struct userdata *u) { +#ifdef NOKIA if (USE_SCO_OVER_PCM(u)) { pa_proplist *p; @@ -1428,7 +1613,10 @@ static int add_sink(struct userdata *u) { if (!u->hsp.sink_state_changed_slot) u->hsp.sink_state_changed_slot = pa_hook_connect(&u->core->hooks[PA_CORE_HOOK_SINK_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_state_changed_cb, u); - } else { + } else +#endif + + { pa_sink_new_data data; pa_bool_t b; @@ -1437,11 +1625,19 @@ static int add_sink(struct userdata *u) { data.module = u->module; pa_sink_new_data_set_sample_spec(&data, &u->sample_spec); pa_proplist_sets(data.proplist, "bluetooth.protocol", u->profile == PROFILE_A2DP ? "a2dp" : "sco"); + if (u->profile == PROFILE_HSP) + pa_proplist_sets(data.proplist, PA_PROP_DEVICE_INTENDED_ROLES, "phone"); data.card = u->card; - data.name = get_name("sink", u->modargs, u->device->address, &b); + data.name = get_name("sink", u->modargs, u->address, &b); data.namereg_fail = b; - u->sink = pa_sink_new(u->core, &data, PA_SINK_HARDWARE|PA_SINK_LATENCY); + if (pa_modargs_get_proplist(u->modargs, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) { + pa_log("Invalid properties"); + pa_sink_new_data_done(&data); + return -1; + } + + u->sink = pa_sink_new(u->core, &data, PA_SINK_HARDWARE|PA_SINK_LATENCY | (u->profile == PROFILE_HSP ? PA_SINK_HW_VOLUME_CTRL : 0)); pa_sink_new_data_done(&data); if (!u->sink) { @@ -1451,28 +1647,36 @@ static int add_sink(struct userdata *u) { u->sink->userdata = u; u->sink->parent.process_msg = sink_process_msg; + + pa_sink_set_max_request(u->sink, u->block_size); + pa_sink_set_fixed_latency(u->sink, + (u->profile == PROFILE_A2DP ? FIXED_LATENCY_PLAYBACK_A2DP : FIXED_LATENCY_PLAYBACK_HSP) + + pa_bytes_to_usec(u->block_size, &u->sample_spec)); } -/* u->sink->get_volume = sink_get_volume_cb; */ -/* u->sink->set_volume = sink_set_volume_cb; */ + if (u->profile == PROFILE_HSP) { + u->sink->set_volume = sink_set_volume_cb; + u->sink->n_volume_steps = 16; + } return 0; } +/* Run from main thread */ static int add_source(struct userdata *u) { - pa_proplist *p; +#ifdef NOKIA if (USE_SCO_OVER_PCM(u)) { u->source = u->hsp.sco_source; - p = pa_proplist_new(); - pa_proplist_sets(p, "bluetooth.protocol", "sco"); - pa_proplist_update(u->source->proplist, PA_UPDATE_MERGE, p); - pa_proplist_free(p); + pa_proplist_sets(u->source->proplist, "bluetooth.protocol", "hsp"); if (!u->hsp.source_state_changed_slot) u->hsp.source_state_changed_slot = pa_hook_connect(&u->core->hooks[PA_CORE_HOOK_SOURCE_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) source_state_changed_cb, u); - } else { + } else +#endif + + { pa_source_new_data data; pa_bool_t b; @@ -1480,12 +1684,20 @@ static int add_source(struct userdata *u) { data.driver = __FILE__; data.module = u->module; pa_source_new_data_set_sample_spec(&data, &u->sample_spec); - pa_proplist_sets(data.proplist, "bluetooth.protocol", u->profile == PROFILE_A2DP ? "a2dp" : "sco"); + pa_proplist_sets(data.proplist, "bluetooth.protocol", u->profile == PROFILE_A2DP ? "a2dp" : "hsp"); + if (u->profile == PROFILE_HSP) + pa_proplist_sets(data.proplist, PA_PROP_DEVICE_INTENDED_ROLES, "phone"); data.card = u->card; - data.name = get_name("source", u->modargs, u->device->address, &b); + data.name = get_name("source", u->modargs, u->address, &b); data.namereg_fail = b; - u->source = pa_source_new(u->core, &data, PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY); + if (pa_modargs_get_proplist(u->modargs, "source_properties", data.proplist, PA_UPDATE_REPLACE) < 0) { + pa_log("Invalid properties"); + pa_source_new_data_done(&data); + return -1; + } + + u->source = pa_source_new(u->core, &data, PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY | (u->profile == PROFILE_HSP ? PA_SOURCE_HW_VOLUME_CTRL : 0)); pa_source_new_data_done(&data); if (!u->source) { @@ -1495,39 +1707,51 @@ static int add_source(struct userdata *u) { u->source->userdata = u; u->source->parent.process_msg = source_process_msg; - } -/* u->source->get_volume = source_get_volume_cb; */ -/* u->source->set_volume = source_set_volume_cb; */ + pa_source_set_fixed_latency(u->source, + (/* u->profile == PROFILE_A2DP ? FIXED_LATENCY_RECORD_A2DP : */ FIXED_LATENCY_RECORD_HSP) + + pa_bytes_to_usec(u->block_size, &u->sample_spec)); + } - p = pa_proplist_new(); - pa_proplist_sets(p, "bluetooth.nrec", pa_yes_no(u->hsp.pcm_capabilities.flags & BT_PCM_FLAG_NREC)); - pa_proplist_update(u->source->proplist, PA_UPDATE_MERGE, p); - pa_proplist_free(p); + if (u->profile == PROFILE_HSP) { + pa_proplist_sets(u->source->proplist, "bluetooth.nrec", (u->hsp.pcm_capabilities.flags & BT_PCM_FLAG_NREC) ? "1" : "0"); + u->source->set_volume = source_set_volume_cb; + u->source->n_volume_steps = 16; + } return 0; } +/* Run from main thread */ static void shutdown_bt(struct userdata *u) { pa_assert(u); if (u->stream_fd >= 0) { pa_close(u->stream_fd); u->stream_fd = -1; + + u->stream_write_type = 0; } if (u->service_fd >= 0) { pa_close(u->service_fd); u->service_fd = -1; + u->service_write_type = u->service_write_type = 0; + } + + if (u->write_memchunk.memblock) { + pa_memblock_unref(u->write_memchunk.memblock); + pa_memchunk_reset(&u->write_memchunk); } } +/* Run from main thread */ static int init_bt(struct userdata *u) { pa_assert(u); shutdown_bt(u); - u->stream_write_type = u->stream_read_type = 0; + u->stream_write_type = 0; u->service_write_type = u->service_write_type = 0; if ((u->service_fd = bt_audio_service_open()) < 0) { @@ -1540,10 +1764,11 @@ static int init_bt(struct userdata *u) { return 0; } +/* Run from main thread */ static int setup_bt(struct userdata *u) { pa_assert(u); - if (get_caps(u) < 0) + if (get_caps(u, 0) < 0) return -1; pa_log_debug("Got device capabilities"); @@ -1553,16 +1778,19 @@ static int setup_bt(struct userdata *u) { pa_log_debug("Connection to the device configured"); +#ifdef NOKIA if (USE_SCO_OVER_PCM(u)) { pa_log_debug("Configured to use SCO over PCM"); return 0; } +#endif pa_log_debug("Got the stream socket"); return 0; } +/* Run from main thread */ static int init_profile(struct userdata *u) { int r = 0; pa_assert(u); @@ -1583,6 +1811,7 @@ static int init_profile(struct userdata *u) { return r; } +/* Run from main thread */ static void stop_thread(struct userdata *u) { pa_assert(u); @@ -1623,8 +1852,14 @@ static void stop_thread(struct userdata *u) { pa_rtpoll_free(u->rtpoll); u->rtpoll = NULL; } + + if (u->read_smoother) { + pa_smoother_free(u->read_smoother); + u->read_smoother = NULL; + } } +/* Run from main thread */ static int start_thread(struct userdata *u) { pa_assert(u); pa_assert(!u->thread); @@ -1634,6 +1869,7 @@ static int start_thread(struct userdata *u) { u->rtpoll = pa_rtpoll_new(); pa_thread_mq_init(&u->thread_mq, u->core->mainloop, u->rtpoll); +#ifdef NOKIA if (USE_SCO_OVER_PCM(u)) { if (start_stream_fd(u) < 0) return -1; @@ -1643,6 +1879,7 @@ static int start_thread(struct userdata *u) { /* FIXME: monitor stream_fd error */ return 0; } +#endif if (!(u->thread = pa_thread_new(thread_func, u))) { pa_log_error("Failed to create IO thread"); @@ -1654,21 +1891,29 @@ static int start_thread(struct userdata *u) { pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq); pa_sink_set_rtpoll(u->sink, u->rtpoll); pa_sink_put(u->sink); + + if (u->sink->set_volume) + u->sink->set_volume(u->sink); } if (u->source) { pa_source_set_asyncmsgq(u->source, u->thread_mq.inq); pa_source_set_rtpoll(u->source, u->rtpoll); pa_source_put(u->source); + + if (u->source->set_volume) + u->source->set_volume(u->source); } return 0; } +/* Run from main thread */ static int card_set_profile(pa_card *c, pa_card_profile *new_profile) { struct userdata *u; enum profile *d; pa_queue *inputs = NULL, *outputs = NULL; + const pa_bluetooth_device *device; pa_assert(c); pa_assert(new_profile); @@ -1676,26 +1921,44 @@ static int card_set_profile(pa_card *c, pa_card_profile *new_profile) { d = PA_CARD_PROFILE_DATA(new_profile); + if (!(device = pa_bluetooth_discovery_get_by_path(u->discovery, u->path))) { + pa_log_error("Failed to get device object."); + return -PA_ERR_IO; + } + + /* The state signal is sent by bluez, so it is racy to check + strictly for CONNECTED, we should also accept STREAMING state + as being good enough. However, if the profile is used + concurrently (which is unlikely), ipc will fail later on, and + module will be unloaded. */ + if (device->headset_state < PA_BT_AUDIO_STATE_CONNECTED && *d == PROFILE_HSP) { + pa_log_warn("HSP is not connected, refused to switch profile"); + return -PA_ERR_IO; + } + else if (device->audio_sink_state < PA_BT_AUDIO_STATE_CONNECTED && *d == PROFILE_A2DP) { + pa_log_warn("A2DP is not connected, refused to switch profile"); + return -PA_ERR_IO; + } + if (u->sink) { - inputs = pa_sink_move_all_start(u->sink); + inputs = pa_sink_move_all_start(u->sink, NULL); +#ifdef NOKIA if (!USE_SCO_OVER_PCM(u)) +#endif pa_sink_unlink(u->sink); } if (u->source) { - outputs = pa_source_move_all_start(u->source); + outputs = pa_source_move_all_start(u->source, NULL); +#ifdef NOKIA if (!USE_SCO_OVER_PCM(u)) +#endif pa_source_unlink(u->source); } stop_thread(u); shutdown_bt(u); - if (u->write_memchunk.memblock) { - pa_memblock_unref(u->write_memchunk.memblock); - pa_memchunk_reset(&u->write_memchunk); - } - u->profile = *d; u->sample_spec = u->requested_sample_spec; @@ -1724,36 +1987,51 @@ static int card_set_profile(pa_card *c, pa_card_profile *new_profile) { return 0; } -static int add_card(struct userdata *u, const char * default_profile) { +/* Run from main thread */ +static int add_card(struct userdata *u, const pa_bluetooth_device *device) { pa_card_new_data data; pa_bool_t b; pa_card_profile *p; enum profile *d; const char *ff; char *n; + const char *default_profile; + + pa_assert(u); + pa_assert(device); pa_card_new_data_init(&data); data.driver = __FILE__; data.module = u->module; - n = pa_bluetooth_cleanup_name(u->device->name); + n = pa_bluetooth_cleanup_name(device->name); pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, n); pa_xfree(n); - pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->device->address); + pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, device->address); pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "bluez"); pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "sound"); pa_proplist_sets(data.proplist, PA_PROP_DEVICE_BUS, "bluetooth"); - if ((ff = pa_bluetooth_get_form_factor(u->device->class))) + if ((ff = pa_bluetooth_get_form_factor(device->class))) pa_proplist_sets(data.proplist, PA_PROP_DEVICE_FORM_FACTOR, ff); - pa_proplist_sets(data.proplist, "bluez.path", u->device->path); - pa_proplist_setf(data.proplist, "bluez.class", "0x%06x", (unsigned) u->device->class); - pa_proplist_sets(data.proplist, "bluez.name", u->device->name); - data.name = get_name("card", u->modargs, u->device->address, &b); + pa_proplist_sets(data.proplist, "bluez.path", device->path); + pa_proplist_setf(data.proplist, "bluez.class", "0x%06x", (unsigned) device->class); + pa_proplist_sets(data.proplist, "bluez.name", device->name); + data.name = get_name("card", u->modargs, device->address, &b); data.namereg_fail = b; + if (pa_modargs_get_proplist(u->modargs, "card_properties", data.proplist, PA_UPDATE_REPLACE) < 0) { + pa_log("Invalid properties"); + pa_card_new_data_done(&data); + return -1; + } + data.profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func); - if (u->device->audio_sink_info_valid > 0) { + /* we base hsp/a2dp availability on UUIDs. + Ideally, it would be based on "Connected" state, but + we can't afford to wait for this information when + we are loaded with profile="hsp", for instance */ + if (pa_bluetooth_uuid_has(device->uuids, A2DP_SINK_UUID)) { p = pa_card_profile_new("a2dp", _("High Fidelity Playback (A2DP)"), sizeof(enum profile)); p->priority = 10; p->n_sinks = 1; @@ -1767,7 +2045,8 @@ static int add_card(struct userdata *u, const char * default_profile) { pa_hashmap_put(data.profiles, p->name, p); } - if (u->device->headset_info_valid > 0) { + if (pa_bluetooth_uuid_has(device->uuids, HSP_HS_UUID) || + pa_bluetooth_uuid_has(device->uuids, HFP_HS_UUID)) { p = pa_card_profile_new("hsp", _("Telephony Duplex (HSP/HFP)"), sizeof(enum profile)); p->priority = 20; p->n_sinks = 1; @@ -1788,7 +2067,7 @@ static int add_card(struct userdata *u, const char * default_profile) { *d = PROFILE_OFF; pa_hashmap_put(data.profiles, p->name, p); - if (default_profile) { + if ((default_profile = pa_modargs_get_value(u->modargs, "profile", NULL))) { if (pa_hashmap_get(data.profiles, default_profile)) pa_card_new_data_set_profile(&data, default_profile); else @@ -1807,51 +2086,71 @@ static int add_card(struct userdata *u, const char * default_profile) { u->card->set_profile = card_set_profile; d = PA_CARD_PROFILE_DATA(u->card->active_profile); - u->profile = *d; - return 0; -} - -static int setup_dbus(struct userdata *u) { - DBusError error; - - dbus_error_init(&error); - - u->connection = pa_dbus_bus_get(u->core, DBUS_BUS_SYSTEM, &error); - if (dbus_error_is_set(&error) || (!u->connection)) { - pa_log("Failed to get D-Bus connection: %s", error.message); - dbus_error_free(&error); - return -1; + if ((device->headset_state < PA_BT_AUDIO_STATE_CONNECTED && *d == PROFILE_HSP) || + (device->audio_sink_state < PA_BT_AUDIO_STATE_CONNECTED && *d == PROFILE_A2DP)) { + pa_log_warn("Default profile not connected, selecting off profile"); + u->card->active_profile = pa_hashmap_get(u->card->profiles, "off"); + u->card->save_profile = FALSE; } + d = PA_CARD_PROFILE_DATA(u->card->active_profile); + u->profile = *d; + return 0; } -static int find_device(struct userdata *u, const char *address, const char *path) { +/* Run from main thread */ +static const pa_bluetooth_device* find_device(struct userdata *u, const char *address, const char *path) { + const pa_bluetooth_device *d = NULL; + pa_assert(u); if (!address && !path) { pa_log_error("Failed to get device address/path from module arguments."); - return -1; + return NULL; } if (path) { - if (!(u->device = pa_bluetooth_get_device(pa_dbus_connection_get(u->connection), path))) { + if (!(d = pa_bluetooth_discovery_get_by_path(u->discovery, path))) { pa_log_error("%s is not a valid BlueZ audio device.", path); - return -1; + return NULL; } - if (address && !(pa_streq(u->device->address, address))) { + if (address && !(pa_streq(d->address, address))) { pa_log_error("Passed path %s and address %s don't match.", path, address); - return -1; + return NULL; } + } else { - if (!(u->device = pa_bluetooth_find_device(pa_dbus_connection_get(u->connection), address))) { + if (!(d = pa_bluetooth_discovery_get_by_address(u->discovery, address))) { pa_log_error("%s is not known.", address); - return -1; + return NULL; } } + if (d) { + u->address = pa_xstrdup(d->address); + u->path = pa_xstrdup(d->path); + } + + return d; +} + +/* Run from main thread */ +static int setup_dbus(struct userdata *u) { + DBusError err; + + dbus_error_init(&err); + + u->connection = pa_dbus_bus_get(u->core, DBUS_BUS_SYSTEM, &err); + + if (dbus_error_is_set(&err) || !u->connection) { + pa_log("Failed to get D-Bus connection: %s", err.message); + dbus_error_free(&err); + return -1; + } + return 0; } @@ -1860,9 +2159,14 @@ int pa__init(pa_module* m) { uint32_t channels; struct userdata *u; const char *address, *path; + DBusError err; + char *mike, *speaker; + const pa_bluetooth_device *device; pa_assert(m); + dbus_error_init(&err); + if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { pa_log_error("Failed to parse module arguments"); goto fail; @@ -1873,10 +2177,10 @@ int pa__init(pa_module* m) { u->core = m->core; u->service_fd = -1; u->stream_fd = -1; - u->read_smoother = pa_smoother_new(PA_USEC_PER_SEC, PA_USEC_PER_SEC*2, TRUE, 10); u->sample_spec = m->core->default_sample_spec; u->modargs = ma; +#ifdef NOKIA if (pa_modargs_get_value(ma, "sco_sink", NULL) && !(u->hsp.sco_sink = pa_namereg_get(m->core, pa_modargs_get_value(ma, "sco_sink", NULL), PA_NAMEREG_SINK))) { pa_log("SCO sink not found"); @@ -1888,6 +2192,7 @@ int pa__init(pa_module* m) { pa_log("SCO source not found"); goto fail; } +#endif if (pa_modargs_get_value_u32(ma, "rate", &u->sample_spec.rate) < 0 || u->sample_spec.rate <= 0 || u->sample_spec.rate > PA_RATE_MAX) { @@ -1904,65 +2209,54 @@ int pa__init(pa_module* m) { u->sample_spec.channels = (uint8_t) channels; u->requested_sample_spec = u->sample_spec; - if (setup_dbus(u) < 0) - goto fail; - address = pa_modargs_get_value(ma, "address", NULL); path = pa_modargs_get_value(ma, "path", NULL); - if (find_device(u, address, path) < 0) + if (setup_dbus(u) < 0) + goto fail; + + if (!(u->discovery = pa_bluetooth_discovery_get(m->core))) goto fail; - pa_assert(u->device); + if (!(device = find_device(u, address, path))) + goto fail; /* Add the card structure. This will also initialize the default profile */ - if (add_card(u, pa_modargs_get_value(ma, "profile", NULL)) < 0) + if (add_card(u, device) < 0) goto fail; /* Connect to the BT service and query capabilities */ if (init_bt(u) < 0) goto fail; + if (!dbus_connection_add_filter(pa_dbus_connection_get(u->connection), filter_cb, u, NULL)) { + pa_log_error("Failed to add filter function"); + goto fail; + } + + speaker = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.Headset',member='SpeakerGainChanged',path='%s'", u->path); + mike = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.Headset',member='MicrophoneGainChanged',path='%s'", u->path); + + if (pa_dbus_add_matches( + pa_dbus_connection_get(u->connection), &err, + speaker, + mike, + NULL) < 0) { + + pa_xfree(speaker); + pa_xfree(mike); + + pa_log("Failed to add D-Bus matches: %s", err.message); + goto fail; + } + + pa_xfree(speaker); + pa_xfree(mike); + if (u->profile != PROFILE_OFF) if (init_profile(u) < 0) goto fail; -/* if (u->path) { */ -/* DBusError err; */ -/* dbus_error_init(&err); */ -/* char *t; */ - - -/* if (!dbus_connection_add_filter(pa_dbus_connection_get(u->conn), filter_cb, u, NULL)) { */ -/* pa_log_error("Failed to add filter function"); */ -/* goto fail; */ -/* } */ - -/* if (u->transport == BT_CAPABILITIES_TRANSPORT_SCO || */ -/* u->transport == BT_CAPABILITIES_TRANSPORT_ANY) { */ -/* t = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged',path='%s'", u->path); */ -/* dbus_bus_add_match(pa_dbus_connection_get(u->conn), t, &err); */ -/* pa_xfree(t); */ - -/* if (dbus_error_is_set(&err)) { */ -/* pa_log_error("Unable to subscribe to org.bluez.Headset signals: %s: %s", err.name, err.message); */ -/* goto fail; */ -/* } */ -/* } */ - -/* if (u->transport == BT_CAPABILITIES_TRANSPORT_A2DP || */ -/* u->transport == BT_CAPABILITIES_TRANSPORT_ANY) { */ -/* t = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged',path='%s'", u->path); */ -/* dbus_bus_add_match(pa_dbus_connection_get(u->conn), t, &err); */ -/* pa_xfree(t); */ - -/* if (dbus_error_is_set(&err)) { */ -/* pa_log_error("Unable to subscribe to org.bluez.AudioSink signals: %s: %s", err.name, err.message); */ -/* goto fail; */ -/* } */ -/* } */ -/* } */ - if (u->sink || u->source) if (start_thread(u) < 0) goto fail; @@ -1970,7 +2264,11 @@ int pa__init(pa_module* m) { return 0; fail: + pa__done(m); + + dbus_error_free(&err); + return -1; } @@ -1992,39 +2290,39 @@ void pa__done(pa_module *m) { if (!(u = m->userdata)) return; - if (u->sink && !USE_SCO_OVER_PCM(u)) + if (u->sink +#ifdef NOKIA + && !USE_SCO_OVER_PCM(u) +#endif + ) pa_sink_unlink(u->sink); - if (u->source && !USE_SCO_OVER_PCM(u)) + if (u->source +#ifdef NOKIA + && !USE_SCO_OVER_PCM(u) +#endif + ) pa_source_unlink(u->source); stop_thread(u); if (u->connection) { -/* DBusError error; */ -/* char *t; */ - -/* if (u->transport == BT_CAPABILITIES_TRANSPORT_SCO || */ -/* u->transport == BT_CAPABILITIES_TRANSPORT_ANY) { */ - -/* t = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged',path='%s'", u->path); */ -/* dbus_error_init(&error); */ -/* dbus_bus_remove_match(pa_dbus_connection_get(u->conn), t, &error); */ -/* dbus_error_free(&error); */ -/* pa_xfree(t); */ -/* } */ - -/* if (u->transport == BT_CAPABILITIES_TRANSPORT_A2DP || */ -/* u->transport == BT_CAPABILITIES_TRANSPORT_ANY) { */ - -/* t = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged',path='%s'", u->path); */ -/* dbus_error_init(&error); */ -/* dbus_bus_remove_match(pa_dbus_connection_get(u->conn), t, &error); */ -/* dbus_error_free(&error); */ -/* pa_xfree(t); */ -/* } */ - -/* dbus_connection_remove_filter(pa_dbus_connection_get(u->conn), filter_cb, u); */ + + if (u->path) { + char *speaker, *mike; + speaker = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.Headset',member='SpeakerGainChanged',path='%s'", u->path); + mike = pa_sprintf_malloc("type='signal',sender='org.bluez',interface='org.bluez.Headset',member='MicrophoneGainChanged',path='%s'", u->path); + + pa_dbus_remove_matches(pa_dbus_connection_get(u->connection), + speaker, + mike, + NULL); + + pa_xfree(speaker); + pa_xfree(mike); + } + + dbus_connection_remove_filter(pa_dbus_connection_get(u->connection), filter_cb, u); pa_dbus_connection_unref(u->connection); } @@ -2036,12 +2334,6 @@ void pa__done(pa_module *m) { shutdown_bt(u); - if (u->device) - pa_bluetooth_device_free(u->device); - - if (u->write_memchunk.memblock) - pa_memblock_unref(u->write_memchunk.memblock); - if (u->a2dp.buffer) pa_xfree(u->a2dp.buffer); @@ -2050,5 +2342,11 @@ void pa__done(pa_module *m) { if (u->modargs) pa_modargs_free(u->modargs); + pa_xfree(u->address); + pa_xfree(u->path); + + if (u->discovery) + pa_bluetooth_discovery_unref(u->discovery); + pa_xfree(u); } diff --git a/src/modules/bluetooth/module-bluetooth-discover.c b/src/modules/bluetooth/module-bluetooth-discover.c index 4586d8ca..788fee00 100644 --- a/src/modules/bluetooth/module-bluetooth-discover.c +++ b/src/modules/bluetooth/module-bluetooth-discover.c @@ -34,7 +34,7 @@ #include <pulsecore/macro.h> #include <pulsecore/llist.h> #include <pulsecore/core-util.h> -#include <modules/dbus-util.h> +#include <pulsecore/dbus-shared.h> #include "module-bluetooth-discover-symdef.h" #include "bluetooth-util.h" @@ -42,13 +42,21 @@ PA_MODULE_AUTHOR("Joao Paulo Rechi Vita"); PA_MODULE_DESCRIPTION("Detect available bluetooth audio devices and load bluetooth audio drivers"); PA_MODULE_VERSION(PACKAGE_VERSION); -PA_MODULE_USAGE("sco_sink=<name of sink> " - "sco_source=<name of source>" - "async=<Asynchronous initialization?>"); +PA_MODULE_USAGE("async=<Asynchronous initialization?>"); +PA_MODULE_LOAD_ONCE(TRUE); + +/* +#ifdef NOKIA + "sco_sink=<name of sink> " + "sco_source=<name of source>" +#endif +*/ static const char* const valid_modargs[] = { +#ifdef NOKIA "sco_sink", "sco_source", +#endif "async", NULL }; @@ -57,26 +65,45 @@ struct userdata { pa_module *module; pa_modargs *modargs; pa_core *core; - pa_dbus_connection *connection; pa_bluetooth_discovery *discovery; + pa_hook_slot *slot; + pa_hashmap *hashmap; }; -static void load_module_for_device(struct userdata *u, pa_bluetooth_device *d, pa_bool_t good) { +struct module_info { + char *path; + uint32_t module; +}; + +static pa_hook_result_t load_module_for_device(pa_bluetooth_discovery *y, const pa_bluetooth_device *d, struct userdata *u) { + struct module_info *mi; + pa_assert(u); pa_assert(d); - if (good && - d->device_connected > 0 && - (d->audio_sink_connected > 0 || d->headset_connected > 0)) { + mi = pa_hashmap_get(u->hashmap, d->path); + + if (!d->dead && + d->device_connected > 0 && d->audio_state >= PA_BT_AUDIO_STATE_CONNECTED) { - if (((uint32_t) PA_PTR_TO_UINT(d->data))-1 == PA_INVALID_INDEX) { + if (!mi) { pa_module *m = NULL; char *args; /* Oh, awesome, a new device has shown up and been connected! */ - args = pa_sprintf_malloc("address=\"%s\" path=\"%s\" profile=\"%s\"", d->address, d->path, d->headset_connected ? "hsp" : "a2dp"); + args = pa_sprintf_malloc("address=\"%s\" path=\"%s\"", d->address, d->path); +#if 0 + /* This is in case we have to use hsp immediately, without waiting for .Audio.State = Connected */ + if (d->headset_state >= PA_BT_AUDIO_STATE_CONNECTED && somecondition) { + char *tmp; + tmp = pa_sprintf_malloc("%s profile=\"hsp\"", args); + pa_xfree(args); + args = tmp; + } +#endif +#ifdef NOKIA if (pa_modargs_get_value(u->modargs, "sco_sink", NULL) && pa_modargs_get_value(u->modargs, "sco_source", NULL)) { char *tmp; @@ -87,44 +114,38 @@ static void load_module_for_device(struct userdata *u, pa_bluetooth_device *d, p pa_xfree(args); args = tmp; } +#endif pa_log_debug("Loading module-bluetooth-device %s", args); m = pa_module_load(u->module->core, "module-bluetooth-device", args); pa_xfree(args); - if (m) - d->data = PA_UINT_TO_PTR((uint32_t) (m->index+1)); - else + if (m) { + mi = pa_xnew(struct module_info, 1); + mi->module = m->index; + mi->path = pa_xstrdup(d->path); + + pa_hashmap_put(u->hashmap, mi->path, mi); + } else pa_log_debug("Failed to load module for device %s", d->path); } } else { - if (((uint32_t) PA_PTR_TO_UINT(d->data))-1 != PA_INVALID_INDEX) { + if (mi) { /* Hmm, disconnection? Then let's unload our module */ pa_log_debug("Unloading module for %s", d->path); - pa_module_unload_request_by_index(u->core, (uint32_t) (PA_PTR_TO_UINT(d->data))-1, TRUE); - d->data = NULL; - } - } -} - -static int setup_dbus(struct userdata *u) { - DBusError err; + pa_module_unload_request_by_index(u->core, mi->module, TRUE); - dbus_error_init(&err); - - u->connection = pa_dbus_bus_get(u->core, DBUS_BUS_SYSTEM, &err); - - if (dbus_error_is_set(&err) || !u->connection) { - pa_log("Failed to get D-Bus connection: %s", err.message); - dbus_error_free(&err); - return -1; + pa_hashmap_remove(u->hashmap, mi->path); + pa_xfree(mi->path); + pa_xfree(mi); + } } - return 0; + return PA_HOOK_OK; } int pa__init(pa_module* m) { @@ -149,12 +170,12 @@ int pa__init(pa_module* m) { u->core = m->core; u->modargs = ma; ma = NULL; + u->hashmap = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func); - if (setup_dbus(u) < 0) + if (!(u->discovery = pa_bluetooth_discovery_get(u->core))) goto fail; - if (!(u->discovery = pa_bluetooth_discovery_new(pa_dbus_connection_get(u->connection), load_module_for_device, u))) - goto fail; + u->slot = pa_hook_connect(pa_bluetooth_discovery_hook(u->discovery), PA_HOOK_NORMAL, (pa_hook_cb_t) load_module_for_device, u); if (!async) pa_bluetooth_discovery_sync(u->discovery); @@ -178,11 +199,22 @@ void pa__done(pa_module* m) { if (!(u = m->userdata)) return; + if (u->slot) + pa_hook_slot_free(u->slot); + if (u->discovery) - pa_bluetooth_discovery_free(u->discovery); + pa_bluetooth_discovery_unref(u->discovery); + + if (u->hashmap) { + struct module_info *mi; - if (u->connection) - pa_dbus_connection_unref(u->connection); + while ((mi = pa_hashmap_steal_first(u->hashmap))) { + pa_xfree(mi->path); + pa_xfree(mi); + } + + pa_hashmap_free(u->hashmap, NULL, NULL); + } if (u->modargs) pa_modargs_free(u->modargs); diff --git a/src/modules/bluetooth/module-bluetooth-proximity.c b/src/modules/bluetooth/module-bluetooth-proximity.c index c8d7b4d9..c4cfd733 100644 --- a/src/modules/bluetooth/module-bluetooth-proximity.c +++ b/src/modules/bluetooth/module-bluetooth-proximity.c @@ -42,8 +42,8 @@ #include <pulsecore/core-util.h> #include <pulsecore/core-error.h> #include <pulsecore/start-child.h> +#include <pulsecore/dbus-shared.h> -#include "../dbus-util.h" #include "module-bluetooth-proximity-symdef.h" PA_MODULE_AUTHOR("Lennart Poettering"); @@ -109,7 +109,7 @@ static void update_volume(struct userdata *u) { } pa_log_info("Found %u BT devices, unmuting.", u->n_found); - pa_sink_set_mute(s, FALSE); + pa_sink_set_mute(s, FALSE, FALSE); } else if (!u->muted && (u->n_found+u->n_unknown) <= 0) { pa_sink *s; @@ -122,7 +122,7 @@ static void update_volume(struct userdata *u) { } pa_log_info("No BT devices found, muting."); - pa_sink_set_mute(s, TRUE); + pa_sink_set_mute(s, TRUE, FALSE); } else pa_log_info("%u devices now active, %u with unknown state.", u->n_found, u->n_unknown); diff --git a/src/modules/bluetooth/sbc.c b/src/modules/bluetooth/sbc.c index 6fa54796..779be4bd 100644 --- a/src/modules/bluetooth/sbc.c +++ b/src/modules/bluetooth/sbc.c @@ -973,13 +973,13 @@ int sbc_init(sbc_t *sbc, unsigned long flags) return 0; } -int sbc_parse(sbc_t *sbc, void *input, int input_len) +ssize_t sbc_parse(sbc_t *sbc, const void *input, size_t input_len) { return sbc_decode(sbc, input, input_len, NULL, 0, NULL); } -int sbc_decode(sbc_t *sbc, void *input, int input_len, void *output, - int output_len, int *written) +ssize_t sbc_decode(sbc_t *sbc, const void *input, size_t input_len, + void *output, size_t output_len, size_t *written) { struct sbc_priv *priv; char *ptr; @@ -1004,7 +1004,7 @@ int sbc_decode(sbc_t *sbc, void *input, int input_len, void *output, sbc->bitpool = priv->frame.bitpool; priv->frame.codesize = sbc_get_codesize(sbc); - priv->frame.length = sbc_get_frame_length(sbc); + priv->frame.length = framelen; } if (!output) @@ -1020,7 +1020,7 @@ int sbc_decode(sbc_t *sbc, void *input, int input_len, void *output, ptr = output; - if (output_len < samples * priv->frame.channels * 2) + if (output_len < (size_t) (samples * priv->frame.channels * 2)) samples = output_len / (priv->frame.channels * 2); for (i = 0; i < samples; i++) { @@ -1044,10 +1044,8 @@ int sbc_decode(sbc_t *sbc, void *input, int input_len, void *output, return framelen; } -ssize_t sbc_encode(sbc_t *sbc, - const void *input, size_t input_len, - void *output, size_t output_len, - size_t *written) +ssize_t sbc_encode(sbc_t *sbc, const void *input, size_t input_len, + void *output, size_t output_len, size_t *written) { struct sbc_priv *priv; int framelen, samples; @@ -1138,30 +1136,25 @@ void sbc_finish(sbc_t *sbc) size_t sbc_get_frame_length(sbc_t *sbc) { size_t ret; - uint8_t subbands, channels, blocks, joint; + uint8_t subbands, channels, blocks, joint, bitpool; struct sbc_priv *priv; priv = sbc->priv; - if (!priv->init) { - subbands = sbc->subbands ? 8 : 4; - blocks = 4 + (sbc->blocks * 4); - channels = sbc->mode == SBC_MODE_MONO ? 1 : 2; - joint = sbc->mode == SBC_MODE_JOINT_STEREO ? 1 : 0; - } else { - subbands = priv->frame.subbands; - blocks = priv->frame.blocks; - channels = priv->frame.channels; - joint = priv->frame.joint; - } + if (priv->init) + return priv->frame.length; - ret = 4 + (4 * subbands * channels) / 8; + subbands = sbc->subbands ? 8 : 4; + blocks = 4 + (sbc->blocks * 4); + channels = sbc->mode == SBC_MODE_MONO ? 1 : 2; + joint = sbc->mode == SBC_MODE_JOINT_STEREO ? 1 : 0; + bitpool = sbc->bitpool; + ret = 4 + (4 * subbands * channels) / 8; /* This term is not always evenly divide so we round it up */ if (channels == 1) - ret += ((blocks * channels * sbc->bitpool) + 7) / 8; + ret += ((blocks * channels * bitpool) + 7) / 8; else - ret += (((joint ? subbands : 0) + blocks * sbc->bitpool) + 7) - / 8; + ret += (((joint ? subbands : 0) + blocks * bitpool) + 7) / 8; return ret; } diff --git a/src/modules/bluetooth/sbc.h b/src/modules/bluetooth/sbc.h index 25a12885..65435884 100644 --- a/src/modules/bluetooth/sbc.h +++ b/src/modules/bluetooth/sbc.h @@ -82,15 +82,15 @@ typedef struct sbc_struct sbc_t; int sbc_init(sbc_t *sbc, unsigned long flags); int sbc_reinit(sbc_t *sbc, unsigned long flags); -int sbc_parse(sbc_t *sbc, void *input, int input_len); -int sbc_decode(sbc_t *sbc, void *input, int input_len, void *output, - int output_len, int *len); + +ssize_t sbc_parse(sbc_t *sbc, const void *input, size_t input_len); + +ssize_t sbc_decode(sbc_t *sbc, const void *input, size_t input_len, + void *output, size_t output_len, size_t *written); /* Encodes ONE input block into ONE output block */ -ssize_t sbc_encode(sbc_t *sbc, - const void *input, size_t input_len, - void *output, size_t output_len, - size_t *written); +ssize_t sbc_encode(sbc_t *sbc, const void *input, size_t input_len, + void *output, size_t output_len, size_t *written); /* Returns the output block size in bytes */ size_t sbc_get_frame_length(sbc_t *sbc); diff --git a/src/modules/hal-util.c b/src/modules/hal-util.c index 422ae4ec..e2a2d8d7 100644 --- a/src/modules/hal-util.c +++ b/src/modules/hal-util.c @@ -24,10 +24,10 @@ #endif #include <pulsecore/log.h> +#include <pulsecore/dbus-shared.h> #include <hal/libhal.h> -#include "dbus-util.h" #include "hal-util.h" int pa_hal_get_info(pa_core *core, pa_proplist *p, int card) { diff --git a/src/modules/jack/module-jack-sink.c b/src/modules/jack/module-jack-sink.c new file mode 100644 index 00000000..fc976fa7 --- /dev/null +++ b/src/modules/jack/module-jack-sink.c @@ -0,0 +1,504 @@ +/*** + This file is part of PulseAudio. + + Copyright 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.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 + along with PulseAudio; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stdlib.h> +#include <sys/stat.h> +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <fcntl.h> +#include <unistd.h> +#include <limits.h> + +#include <jack/jack.h> + +#include <pulse/xmalloc.h> + +#include <pulsecore/core-error.h> +#include <pulsecore/sink.h> +#include <pulsecore/module.h> +#include <pulsecore/core-util.h> +#include <pulsecore/modargs.h> +#include <pulsecore/log.h> +#include <pulsecore/thread.h> +#include <pulsecore/thread-mq.h> +#include <pulsecore/rtpoll.h> +#include <pulsecore/sample-util.h> + +#include "module-jack-sink-symdef.h" + +/* General overview: + * + * Because JACK has a very unflexible event loop management which + * doesn't allow us to add our own event sources to the event thread + * we cannot use the JACK real-time thread for dispatching our PA + * work. Instead, we run an additional RT thread which does most of + * the PA handling, and have the JACK RT thread request data from it + * via pa_asyncmsgq. The cost is an additional context switch which + * should hopefully not be that expensive if RT scheduling is + * enabled. A better fix would only be possible with additional event + * source support in JACK. + */ + +PA_MODULE_AUTHOR("Lennart Poettering"); +PA_MODULE_DESCRIPTION("JACK Sink"); +PA_MODULE_LOAD_ONCE(TRUE); +PA_MODULE_VERSION(PACKAGE_VERSION); +PA_MODULE_USAGE( + "sink_name=<name for the sink> " + "sink_properties=<properties for the card> " + "server_name=<jack server name> " + "client_name=<jack client name> " + "channels=<number of channels> " + "channel_map=<channel map> " + "connect=<connect ports?>"); + +#define DEFAULT_SINK_NAME "jack_out" + +struct userdata { + pa_core *core; + pa_module *module; + pa_sink *sink; + + unsigned channels; + + jack_port_t* port[PA_CHANNELS_MAX]; + jack_client_t *client; + + void *buffer[PA_CHANNELS_MAX]; + + pa_thread_mq thread_mq; + pa_asyncmsgq *jack_msgq; + pa_rtpoll *rtpoll; + pa_rtpoll_item *rtpoll_item; + + pa_thread *thread; + + jack_nframes_t frames_in_buffer; + jack_nframes_t saved_frame_time; + pa_bool_t saved_frame_time_valid; +}; + +static const char* const valid_modargs[] = { + "sink_name", + "sink_properties", + "server_name", + "client_name", + "channels", + "channel_map", + "connect", + NULL +}; + +enum { + SINK_MESSAGE_RENDER = PA_SINK_MESSAGE_MAX, + SINK_MESSAGE_BUFFER_SIZE, + SINK_MESSAGE_ON_SHUTDOWN +}; + +static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *memchunk) { + struct userdata *u = PA_SINK(o)->userdata; + + switch (code) { + + case SINK_MESSAGE_RENDER: + + /* Handle the request from the JACK thread */ + + if (u->sink->thread_info.state == PA_SINK_RUNNING) { + pa_memchunk chunk; + size_t nbytes; + void *p; + + pa_assert(offset > 0); + nbytes = (size_t) offset * pa_frame_size(&u->sink->sample_spec); + + pa_sink_render_full(u->sink, nbytes, &chunk); + + p = (uint8_t*) pa_memblock_acquire(chunk.memblock) + chunk.index; + pa_deinterleave(p, u->buffer, u->channels, sizeof(float), (unsigned) offset); + pa_memblock_release(chunk.memblock); + + pa_memblock_unref(chunk.memblock); + } else { + unsigned c; + pa_sample_spec ss; + + /* Humm, we're not RUNNING, hence let's write some silence */ + + ss = u->sink->sample_spec; + ss.channels = 1; + + for (c = 0; c < u->channels; c++) + pa_silence_memory(u->buffer[c], (size_t) offset * pa_sample_size(&ss), &ss); + } + + u->frames_in_buffer = (jack_nframes_t) offset; + u->saved_frame_time = * (jack_nframes_t*) data; + u->saved_frame_time_valid = TRUE; + + return 0; + + case SINK_MESSAGE_BUFFER_SIZE: + pa_sink_set_max_request_within_thread(u->sink, (size_t) offset * pa_frame_size(&u->sink->sample_spec)); + return 0; + + case SINK_MESSAGE_ON_SHUTDOWN: + pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL); + return 0; + + case PA_SINK_MESSAGE_GET_LATENCY: { + jack_nframes_t l, ft, d; + size_t n; + + /* This is the "worst-case" latency */ + l = jack_port_get_total_latency(u->client, u->port[0]) + u->frames_in_buffer; + + if (u->saved_frame_time_valid) { + /* Adjust the worst case latency by the time that + * passed since we last handed data to JACK */ + + ft = jack_frame_time(u->client); + d = ft > u->saved_frame_time ? ft - u->saved_frame_time : 0; + l = l > d ? l - d : 0; + } + + /* Convert it to usec */ + n = l * pa_frame_size(&u->sink->sample_spec); + *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->sink->sample_spec); + + return 0; + } + + } + + return pa_sink_process_msg(o, code, data, offset, memchunk); +} + +static int jack_process(jack_nframes_t nframes, void *arg) { + struct userdata *u = arg; + unsigned c; + jack_nframes_t frame_time; + pa_assert(u); + + /* We just forward the request to our other RT thread */ + + for (c = 0; c < u->channels; c++) + pa_assert_se(u->buffer[c] = jack_port_get_buffer(u->port[c], nframes)); + + frame_time = jack_frame_time(u->client); + + pa_assert_se(pa_asyncmsgq_send(u->jack_msgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_RENDER, &frame_time, nframes, NULL) == 0); + return 0; +} + +static void thread_func(void *userdata) { + struct userdata *u = userdata; + + pa_assert(u); + + pa_log_debug("Thread starting up"); + + if (u->core->realtime_scheduling) + pa_make_realtime(u->core->realtime_priority); + + pa_thread_mq_install(&u->thread_mq); + + for (;;) { + int ret; + + if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) + if (u->sink->thread_info.rewind_requested) + pa_sink_process_rewind(u->sink, 0); + + if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0) + goto fail; + + if (ret == 0) + goto finish; + } + +fail: + /* If this was no regular exit from the loop we have to continue + * processing messages until we received PA_MESSAGE_SHUTDOWN */ + pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL); + pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN); + +finish: + pa_log_debug("Thread shutting down"); +} + +static void jack_error_func(const char*t) { + char *s; + + s = pa_xstrndup(t, strcspn(t, "\n\r")); + pa_log_warn("JACK error >%s<", s); + pa_xfree(s); +} + +static void jack_init(void *arg) { + struct userdata *u = arg; + + pa_log_info("JACK thread starting up."); + + if (u->core->realtime_scheduling) + pa_make_realtime(u->core->realtime_priority+4); +} + +static void jack_shutdown(void* arg) { + struct userdata *u = arg; + + pa_log_info("JACK thread shutting down."); + pa_asyncmsgq_post(u->jack_msgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_ON_SHUTDOWN, NULL, 0, NULL, NULL); +} + +static int jack_buffer_size(jack_nframes_t nframes, void *arg) { + struct userdata *u = arg; + + pa_log_info("JACK buffer size changed."); + pa_asyncmsgq_post(u->jack_msgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_BUFFER_SIZE, NULL, nframes, NULL, NULL); + return 0; +} + +int pa__init(pa_module*m) { + struct userdata *u = NULL; + pa_sample_spec ss; + pa_channel_map map; + pa_modargs *ma = NULL; + jack_status_t status; + const char *server_name, *client_name; + uint32_t channels = 0; + pa_bool_t do_connect = TRUE; + unsigned i; + const char **ports = NULL, **p; + pa_sink_new_data data; + + pa_assert(m); + + jack_set_error_function(jack_error_func); + + if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { + pa_log("Failed to parse module arguments."); + goto fail; + } + + if (pa_modargs_get_value_boolean(ma, "connect", &do_connect) < 0) { + pa_log("Failed to parse connect= argument."); + goto fail; + } + + server_name = pa_modargs_get_value(ma, "server_name", NULL); + client_name = pa_modargs_get_value(ma, "client_name", "PulseAudio JACK Sink"); + + m->userdata = u = pa_xnew0(struct userdata, 1); + u->core = m->core; + u->module = m; + u->saved_frame_time_valid = FALSE; + u->rtpoll = pa_rtpoll_new(); + pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll); + + /* The queue linking the JACK thread and our RT thread */ + u->jack_msgq = pa_asyncmsgq_new(0); + + /* The msgq from the JACK RT thread should have an even higher + * priority than the normal message queues, to match the guarantee + * all other drivers make: supplying the audio device with data is + * the top priority -- and as long as that is possible we don't do + * anything else */ + u->rtpoll_item = pa_rtpoll_item_new_asyncmsgq_read(u->rtpoll, PA_RTPOLL_EARLY-1, u->jack_msgq); + + if (!(u->client = jack_client_open(client_name, server_name ? JackServerName : JackNullOption, &status, server_name))) { + pa_log("jack_client_open() failed."); + goto fail; + } + + ports = jack_get_ports(u->client, NULL, NULL, JackPortIsPhysical|JackPortIsInput); + + channels = 0; + for (p = ports; *p; p++) + channels++; + + if (!channels) + channels = m->core->default_sample_spec.channels; + + if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 || + channels <= 0 || + channels > PA_CHANNELS_MAX) { + pa_log("Failed to parse channels= argument."); + goto fail; + } + + if (channels == m->core->default_channel_map.channels) + map = m->core->default_channel_map; + else + pa_channel_map_init_extend(&map, channels, PA_CHANNEL_MAP_ALSA); + + if (pa_modargs_get_channel_map(ma, NULL, &map) < 0 || map.channels != channels) { + pa_log("Failed to parse channel_map= argument."); + goto fail; + } + + pa_log_info("Successfully connected as '%s'", jack_get_client_name(u->client)); + + u->channels = ss.channels = (uint8_t) channels; + ss.rate = jack_get_sample_rate(u->client); + ss.format = PA_SAMPLE_FLOAT32NE; + + pa_assert(pa_sample_spec_valid(&ss)); + + for (i = 0; i < ss.channels; i++) { + if (!(u->port[i] = jack_port_register(u->client, pa_channel_position_to_string(map.map[i]), JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput|JackPortIsTerminal, 0))) { + pa_log("jack_port_register() failed."); + goto fail; + } + } + + pa_sink_new_data_init(&data); + data.driver = __FILE__; + data.module = m; + pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME)); + pa_sink_new_data_set_sample_spec(&data, &ss); + pa_sink_new_data_set_channel_map(&data, &map); + pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "jack"); + if (server_name) + pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, server_name); + pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Jack sink (%s)", jack_get_client_name(u->client)); + pa_proplist_sets(data.proplist, "jack.client_name", jack_get_client_name(u->client)); + + if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) { + pa_log("Invalid properties"); + pa_sink_new_data_done(&data); + goto fail; + } + + u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY); + pa_sink_new_data_done(&data); + + if (!u->sink) { + pa_log("Failed to create sink."); + goto fail; + } + + u->sink->parent.process_msg = sink_process_msg; + u->sink->userdata = u; + + pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq); + pa_sink_set_rtpoll(u->sink, u->rtpoll); + pa_sink_set_max_request(u->sink, jack_get_buffer_size(u->client) * pa_frame_size(&u->sink->sample_spec)); + + jack_set_process_callback(u->client, jack_process, u); + jack_on_shutdown(u->client, jack_shutdown, u); + jack_set_thread_init_callback(u->client, jack_init, u); + jack_set_buffer_size_callback(u->client, jack_buffer_size, u); + + if (!(u->thread = pa_thread_new(thread_func, u))) { + pa_log("Failed to create thread."); + goto fail; + } + + if (jack_activate(u->client)) { + pa_log("jack_activate() failed"); + goto fail; + } + + if (do_connect) { + for (i = 0, p = ports; i < ss.channels; i++, p++) { + + if (!*p) { + pa_log("Not enough physical output ports, leaving unconnected."); + break; + } + + pa_log_info("Connecting %s to %s", jack_port_name(u->port[i]), *p); + + if (jack_connect(u->client, jack_port_name(u->port[i]), *p)) { + pa_log("Failed to connect %s to %s, leaving unconnected.", jack_port_name(u->port[i]), *p); + break; + } + } + } + + pa_sink_put(u->sink); + + free(ports); + pa_modargs_free(ma); + + return 0; + +fail: + if (ma) + pa_modargs_free(ma); + + free(ports); + + pa__done(m); + + return -1; +} + +int pa__get_n_used(pa_module *m) { + struct userdata *u; + + pa_assert(m); + pa_assert_se(u = m->userdata); + + return pa_sink_linked_by(u->sink); +} + +void pa__done(pa_module*m) { + struct userdata *u; + + pa_assert(m); + + if (!(u = m->userdata)) + return; + + if (u->client) + jack_client_close(u->client); + + if (u->sink) + pa_sink_unlink(u->sink); + + if (u->thread) { + pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL); + pa_thread_free(u->thread); + } + + pa_thread_mq_done(&u->thread_mq); + + if (u->sink) + pa_sink_unref(u->sink); + + if (u->rtpoll_item) + pa_rtpoll_item_free(u->rtpoll_item); + + if (u->jack_msgq) + pa_asyncmsgq_unref(u->jack_msgq); + + if (u->rtpoll) + pa_rtpoll_free(u->rtpoll); + + pa_xfree(u); +} diff --git a/src/modules/jack/module-jack-source.c b/src/modules/jack/module-jack-source.c new file mode 100644 index 00000000..a898e0e5 --- /dev/null +++ b/src/modules/jack/module-jack-source.c @@ -0,0 +1,454 @@ +/*** + This file is part of PulseAudio. + + Copyright 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.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 + along with PulseAudio; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stdlib.h> +#include <sys/stat.h> +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <fcntl.h> +#include <unistd.h> +#include <limits.h> + +#include <jack/jack.h> + +#include <pulse/xmalloc.h> + +#include <pulsecore/core-error.h> +#include <pulsecore/source.h> +#include <pulsecore/module.h> +#include <pulsecore/core-util.h> +#include <pulsecore/modargs.h> +#include <pulsecore/log.h> +#include <pulsecore/thread.h> +#include <pulsecore/thread-mq.h> +#include <pulsecore/rtpoll.h> +#include <pulsecore/sample-util.h> + +#include "module-jack-source-symdef.h" + +/* See module-jack-sink for a few comments how this module basically + * works */ + +PA_MODULE_AUTHOR("Lennart Poettering"); +PA_MODULE_DESCRIPTION("JACK Source"); +PA_MODULE_VERSION(PACKAGE_VERSION); +PA_MODULE_LOAD_ONCE(TRUE); +PA_MODULE_USAGE( + "source_name=<name for the source> " + "source_properties=<properties for the source> " + "server_name=<jack server name> " + "client_name=<jack client name> " + "channels=<number of channels> " + "channel_map=<channel map> " + "connect=<connect ports?>"); + +#define DEFAULT_SOURCE_NAME "jack_in" + +struct userdata { + pa_core *core; + pa_module *module; + pa_source *source; + + unsigned channels; + + jack_port_t* port[PA_CHANNELS_MAX]; + jack_client_t *client; + + pa_thread_mq thread_mq; + pa_asyncmsgq *jack_msgq; + pa_rtpoll *rtpoll; + pa_rtpoll_item *rtpoll_item; + + pa_thread *thread; + + jack_nframes_t saved_frame_time; + pa_bool_t saved_frame_time_valid; +}; + +static const char* const valid_modargs[] = { + "source_name", + "source_properties", + "server_name", + "client_name", + "channels", + "channel_map", + "connect", + NULL +}; + +enum { + SOURCE_MESSAGE_POST = PA_SOURCE_MESSAGE_MAX, + SOURCE_MESSAGE_ON_SHUTDOWN +}; + +static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { + struct userdata *u = PA_SOURCE(o)->userdata; + + switch (code) { + + case SOURCE_MESSAGE_POST: + + /* Handle the new block from the JACK thread */ + pa_assert(chunk); + pa_assert(chunk->length > 0); + + if (u->source->thread_info.state == PA_SOURCE_RUNNING) + pa_source_post(u->source, chunk); + + u->saved_frame_time = (jack_nframes_t) offset; + u->saved_frame_time_valid = TRUE; + + return 0; + + case SOURCE_MESSAGE_ON_SHUTDOWN: + pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL); + return 0; + + case PA_SOURCE_MESSAGE_GET_LATENCY: { + jack_nframes_t l, ft, d; + size_t n; + + /* This is the "worst-case" latency */ + l = jack_port_get_total_latency(u->client, u->port[0]); + + if (u->saved_frame_time_valid) { + /* Adjust the worst case latency by the time that + * passed since we last handed data to JACK */ + + ft = jack_frame_time(u->client); + d = ft > u->saved_frame_time ? ft - u->saved_frame_time : 0; + l += d; + } + + /* Convert it to usec */ + n = l * pa_frame_size(&u->source->sample_spec); + *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->source->sample_spec); + + return 0; + } + } + + return pa_source_process_msg(o, code, data, offset, chunk); +} + +static int jack_process(jack_nframes_t nframes, void *arg) { + unsigned c; + struct userdata *u = arg; + const void *buffer[PA_CHANNELS_MAX]; + void *p; + jack_nframes_t frame_time; + pa_memchunk chunk; + + pa_assert(u); + + for (c = 0; c < u->channels; c++) + pa_assert_se(buffer[c] = jack_port_get_buffer(u->port[c], nframes)); + + /* We interleave the data and pass it on to the other RT thread */ + + pa_memchunk_reset(&chunk); + chunk.length = nframes * pa_frame_size(&u->source->sample_spec); + chunk.memblock = pa_memblock_new(u->core->mempool, chunk.length); + p = pa_memblock_acquire(chunk.memblock); + pa_interleave(buffer, u->channels, p, sizeof(float), nframes); + pa_memblock_release(chunk.memblock); + + frame_time = jack_frame_time(u->client); + + pa_asyncmsgq_post(u->jack_msgq, PA_MSGOBJECT(u->source), SOURCE_MESSAGE_POST, NULL, frame_time, &chunk, NULL); + + pa_memblock_unref(chunk.memblock); + + return 0; +} + +static void thread_func(void *userdata) { + struct userdata *u = userdata; + + pa_assert(u); + + pa_log_debug("Thread starting up"); + + if (u->core->realtime_scheduling) + pa_make_realtime(u->core->realtime_priority); + + pa_thread_mq_install(&u->thread_mq); + + for (;;) { + int ret; + + if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0) + goto fail; + + if (ret == 0) + goto finish; + } + +fail: + /* If this was no regular exit from the loop we have to continue + * processing messages until we received PA_MESSAGE_SHUTDOWN */ + pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL); + pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN); + +finish: + pa_log_debug("Thread shutting down"); +} + +static void jack_error_func(const char*t) { + char *s; + + s = pa_xstrndup(t, strcspn(t, "\n\r")); + pa_log_warn("JACK error >%s<", s); + pa_xfree(s); +} + +static void jack_init(void *arg) { + struct userdata *u = arg; + + pa_log_info("JACK thread starting up."); + + if (u->core->realtime_scheduling) + pa_make_realtime(u->core->realtime_priority+4); +} + +static void jack_shutdown(void* arg) { + struct userdata *u = arg; + + pa_log_info("JACK thread shutting down.."); + pa_asyncmsgq_post(u->jack_msgq, PA_MSGOBJECT(u->source), SOURCE_MESSAGE_ON_SHUTDOWN, NULL, 0, NULL, NULL); +} + +int pa__init(pa_module*m) { + struct userdata *u = NULL; + pa_sample_spec ss; + pa_channel_map map; + pa_modargs *ma = NULL; + jack_status_t status; + const char *server_name, *client_name; + uint32_t channels = 0; + pa_bool_t do_connect = TRUE; + unsigned i; + const char **ports = NULL, **p; + pa_source_new_data data; + + pa_assert(m); + + jack_set_error_function(jack_error_func); + + if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { + pa_log("Failed to parse module arguments."); + goto fail; + } + + if (pa_modargs_get_value_boolean(ma, "connect", &do_connect) < 0) { + pa_log("Failed to parse connect= argument."); + goto fail; + } + + server_name = pa_modargs_get_value(ma, "server_name", NULL); + client_name = pa_modargs_get_value(ma, "client_name", "PulseAudio JACK Source"); + + m->userdata = u = pa_xnew0(struct userdata, 1); + u->core = m->core; + u->module = m; + u->saved_frame_time_valid = FALSE; + u->rtpoll = pa_rtpoll_new(); + pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll); + + u->jack_msgq = pa_asyncmsgq_new(0); + u->rtpoll_item = pa_rtpoll_item_new_asyncmsgq_read(u->rtpoll, PA_RTPOLL_EARLY-1, u->jack_msgq); + + if (!(u->client = jack_client_open(client_name, server_name ? JackServerName : JackNullOption, &status, server_name))) { + pa_log("jack_client_open() failed."); + goto fail; + } + + ports = jack_get_ports(u->client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput); + + channels = 0; + for (p = ports; *p; p++) + channels++; + + if (!channels) + channels = m->core->default_sample_spec.channels; + + if (pa_modargs_get_value_u32(ma, "channels", &channels) < 0 || + channels <= 0 || + channels >= PA_CHANNELS_MAX) { + pa_log("failed to parse channels= argument."); + goto fail; + } + + if (channels == m->core->default_channel_map.channels) + map = m->core->default_channel_map; + else + pa_channel_map_init_extend(&map, channels, PA_CHANNEL_MAP_ALSA); + + if (pa_modargs_get_channel_map(ma, NULL, &map) < 0 || map.channels != channels) { + pa_log("failed to parse channel_map= argument."); + goto fail; + } + + pa_log_info("Successfully connected as '%s'", jack_get_client_name(u->client)); + + u->channels = ss.channels = (uint8_t) channels; + ss.rate = jack_get_sample_rate(u->client); + ss.format = PA_SAMPLE_FLOAT32NE; + + pa_assert(pa_sample_spec_valid(&ss)); + + for (i = 0; i < ss.channels; i++) { + if (!(u->port[i] = jack_port_register(u->client, pa_channel_position_to_string(map.map[i]), JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput|JackPortIsTerminal, 0))) { + pa_log("jack_port_register() failed."); + goto fail; + } + } + + pa_source_new_data_init(&data); + data.driver = __FILE__; + data.module = m; + pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME)); + pa_source_new_data_set_sample_spec(&data, &ss); + pa_source_new_data_set_channel_map(&data, &map); + pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "jack"); + if (server_name) + pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, server_name); + pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Jack source (%s)", jack_get_client_name(u->client)); + pa_proplist_sets(data.proplist, "jack.client_name", jack_get_client_name(u->client)); + + if (pa_modargs_get_proplist(ma, "source_properties", data.proplist, PA_UPDATE_REPLACE) < 0) { + pa_log("Invalid properties"); + pa_source_new_data_done(&data); + goto fail; + } + + u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY); + pa_source_new_data_done(&data); + + if (!u->source) { + pa_log("Failed to create source."); + goto fail; + } + + u->source->parent.process_msg = source_process_msg; + u->source->userdata = u; + + pa_source_set_asyncmsgq(u->source, u->thread_mq.inq); + pa_source_set_rtpoll(u->source, u->rtpoll); + + jack_set_process_callback(u->client, jack_process, u); + jack_on_shutdown(u->client, jack_shutdown, u); + jack_set_thread_init_callback(u->client, jack_init, u); + + if (!(u->thread = pa_thread_new(thread_func, u))) { + pa_log("Failed to create thread."); + goto fail; + } + + if (jack_activate(u->client)) { + pa_log("jack_activate() failed"); + goto fail; + } + + if (do_connect) { + for (i = 0, p = ports; i < ss.channels; i++, p++) { + + if (!*p) { + pa_log("Not enough physical output ports, leaving unconnected."); + break; + } + + pa_log_info("Connecting %s to %s", jack_port_name(u->port[i]), *p); + + if (jack_connect(u->client, *p, jack_port_name(u->port[i]))) { + pa_log("Failed to connect %s to %s, leaving unconnected.", jack_port_name(u->port[i]), *p); + break; + } + } + + } + + pa_source_put(u->source); + + free(ports); + pa_modargs_free(ma); + + return 0; + +fail: + if (ma) + pa_modargs_free(ma); + + free(ports); + + pa__done(m); + + return -1; +} + +int pa__get_n_used(pa_module *m) { + struct userdata *u; + + pa_assert(m); + pa_assert_se(u = m->userdata); + + return pa_source_linked_by(u->source); +} + +void pa__done(pa_module*m) { + struct userdata *u; + pa_assert(m); + + if (!(u = m->userdata)) + return; + + if (u->client) + jack_client_close(u->client); + + if (u->source) + pa_source_unlink(u->source); + + if (u->thread) { + pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL); + pa_thread_free(u->thread); + } + + pa_thread_mq_done(&u->thread_mq); + + if (u->source) + pa_source_unref(u->source); + + if (u->rtpoll_item) + pa_rtpoll_item_free(u->rtpoll_item); + + if (u->jack_msgq) + pa_asyncmsgq_unref(u->jack_msgq); + + if (u->rtpoll) + pa_rtpoll_free(u->rtpoll); + + pa_xfree(u); +} diff --git a/src/modules/module-augment-properties.c b/src/modules/module-augment-properties.c index c3e5997a..15aa3a1e 100644 --- a/src/modules/module-augment-properties.c +++ b/src/modules/module-augment-properties.c @@ -58,6 +58,7 @@ struct rule { char *process_name; char *application_name; char *icon_name; + char *role; pa_proplist *proplist; }; @@ -72,12 +73,21 @@ static void rule_free(struct rule *r) { pa_xfree(r->process_name); pa_xfree(r->application_name); pa_xfree(r->icon_name); + pa_xfree(r->role); if (r->proplist) pa_proplist_free(r->proplist); pa_xfree(r); } -static int parse_properties(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata) { +static int parse_properties( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + const char *rvalue, + void *data, + void *userdata) { + struct rule *r = userdata; pa_proplist *n; @@ -93,11 +103,56 @@ static int parse_properties(const char *filename, unsigned line, const char *sec return 0; } -static int check_type(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata) { +static int parse_categories( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + const char *rvalue, + void *data, + void *userdata) { + + struct rule *r = userdata; + const char *state = NULL; + char *c; + + while ((c = pa_split(rvalue, ";", &state))) { + + if (pa_streq(c, "Game")) { + pa_xfree(r->role); + r->role = pa_xstrdup("game"); + } else if (pa_streq(c, "Telephony")) { + pa_xfree(r->role); + r->role = pa_xstrdup("phone"); + } + + pa_xfree(c); + } + + return 0; +} + +static int check_type( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + const char *rvalue, + void *data, + void *userdata) { + return pa_streq(rvalue, "Application") ? 0 : -1; } -static int catch_all(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata) { +static int catch_all( + const char *filename, + unsigned line, + const char *section, + const char *lvalue, + const char *rvalue, + void *data, + void *userdata) { + return 0; } @@ -109,6 +164,7 @@ static void update_rule(struct rule *r) { { "Icon", pa_config_parse_string, NULL, "Desktop Entry" }, { "Type", check_type, NULL, "Desktop Entry" }, { "X-PulseAudio-Properties", parse_properties, NULL, "Desktop Entry" }, + { "Categories", parse_categories, NULL, "Desktop Entry" }, { NULL, catch_all, NULL, NULL }, { NULL, NULL, NULL, NULL }, }; @@ -131,7 +187,8 @@ static void update_rule(struct rule *r) { r->mtime = st.st_mtime; pa_xfree(r->application_name); pa_xfree(r->icon_name); - r->application_name = r->icon_name = NULL; + pa_xfree(r->role); + r->application_name = r->icon_name = r->role = NULL; if (r->proplist) pa_proplist_clear(r->proplist); @@ -151,6 +208,9 @@ static void apply_rule(struct rule *r, pa_proplist *p) { if (!r->good) return; + if (r->proplist) + pa_proplist_update(p, PA_UPDATE_MERGE, r->proplist); + if (r->icon_name) if (!pa_proplist_contains(p, PA_PROP_APPLICATION_ICON_NAME)) pa_proplist_sets(p, PA_PROP_APPLICATION_ICON_NAME, r->icon_name); @@ -164,8 +224,9 @@ static void apply_rule(struct rule *r, pa_proplist *p) { pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, r->application_name); } - if (r->proplist) - pa_proplist_update(p, PA_UPDATE_MERGE, r->proplist); + if (r->role) + if (!pa_proplist_contains(p, PA_PROP_MEDIA_ROLE)) + pa_proplist_sets(p, PA_PROP_MEDIA_ROLE, r->role); } static void make_room(pa_hashmap *cache) { diff --git a/src/modules/module-card-restore.c b/src/modules/module-card-restore.c index 0afb9353..7dea94f7 100644 --- a/src/modules/module-card-restore.c +++ b/src/modules/module-card-restore.c @@ -30,12 +30,12 @@ #include <stdio.h> #include <stdlib.h> #include <ctype.h> -#include <gdbm.h> #include <pulse/xmalloc.h> #include <pulse/volume.h> #include <pulse/timeval.h> #include <pulse/util.h> +#include <pulse/rtclock.h> #include <pulsecore/core-error.h> #include <pulsecore/module.h> @@ -45,6 +45,7 @@ #include <pulsecore/core-subscribe.h> #include <pulsecore/card.h> #include <pulsecore/namereg.h> +#include <pulsecore/database.h> #include "module-card-restore-symdef.h" @@ -53,7 +54,7 @@ PA_MODULE_DESCRIPTION("Automatically restore profile of cards"); PA_MODULE_VERSION(PACKAGE_VERSION); PA_MODULE_LOAD_ONCE(TRUE); -#define SAVE_INTERVAL 10 +#define SAVE_INTERVAL (10 * PA_USEC_PER_SEC) static const char* const valid_modargs[] = { NULL @@ -65,7 +66,7 @@ struct userdata { pa_subscription *subscription; pa_hook_slot *card_new_hook_slot; pa_time_event *save_time_event; - GDBM_FILE gdbm_file; + pa_database *database; }; #define ENTRY_VERSION 1 @@ -75,43 +76,42 @@ struct entry { char profile[PA_NAME_MAX]; } PA_GCC_PACKED ; -static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) { +static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) { struct userdata *u = userdata; pa_assert(a); pa_assert(e); - pa_assert(tv); pa_assert(u); pa_assert(e == u->save_time_event); u->core->mainloop->time_free(u->save_time_event); u->save_time_event = NULL; - gdbm_sync(u->gdbm_file); + pa_database_sync(u->database); pa_log_info("Synced."); } static struct entry* read_entry(struct userdata *u, const char *name) { - datum key, data; + pa_datum key, data; struct entry *e; pa_assert(u); pa_assert(name); - key.dptr = (char*) name; - key.dsize = (int) strlen(name); + key.data = (char*) name; + key.size = strlen(name); - data = gdbm_fetch(u->gdbm_file, key); + pa_zero(data); - if (!data.dptr) + if (!pa_database_get(u->database, &key, &data)) goto fail; - if (data.dsize != sizeof(struct entry)) { - pa_log_debug("Database contains entry for card %s of wrong size %lu != %lu. Probably due to upgrade, ignoring.", name, (unsigned long) data.dsize, (unsigned long) sizeof(struct entry)); + if (data.size != sizeof(struct entry)) { + pa_log_debug("Database contains entry for card %s of wrong size %lu != %lu. Probably due to upgrade, ignoring.", name, (unsigned long) data.size, (unsigned long) sizeof(struct entry)); goto fail; } - e = (struct entry*) data.dptr; + e = (struct entry*) data.data; if (e->version != ENTRY_VERSION) { pa_log_debug("Version of database entry for card %s doesn't match our version. Probably due to upgrade, ignoring.", name); @@ -127,25 +127,21 @@ static struct entry* read_entry(struct userdata *u, const char *name) { fail: - pa_xfree(data.dptr); + pa_datum_free(&data); return NULL; } static void trigger_save(struct userdata *u) { - struct timeval tv; - if (u->save_time_event) return; - pa_gettimeofday(&tv); - tv.tv_sec += SAVE_INTERVAL; - u->save_time_event = u->core->mainloop->time_new(u->core->mainloop, &tv, save_time_callback, u); + u->save_time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + SAVE_INTERVAL, save_time_callback, u); } static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) { struct userdata *u = userdata; struct entry entry, *old; - datum key, data; + pa_datum key, data; pa_card *card; pa_assert(c); @@ -155,12 +151,15 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3 t != (PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE)) return; - memset(&entry, 0, sizeof(entry)); + pa_zero(entry); entry.version = ENTRY_VERSION; if (!(card = pa_idxset_get_by_index(c->cards, idx))) return; + if (!card->save_profile) + return; + pa_strlcpy(entry.profile, card->active_profile ? card->active_profile->name : "", sizeof(entry.profile)); if ((old = read_entry(u, card->name))) { @@ -173,15 +172,15 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3 pa_xfree(old); } - key.dptr = card->name; - key.dsize = (int) strlen(card->name); + key.data = card->name; + key.size = strlen(card->name); - data.dptr = (void*) &entry; - data.dsize = sizeof(entry); + data.data = &entry; + data.size = sizeof(entry); pa_log_info("Storing profile for card %s.", card->name); - gdbm_store(u->gdbm_file, key, data, GDBM_REPLACE); + pa_database_set(u->database, &key, &data, TRUE); trigger_save(u); } @@ -194,8 +193,9 @@ static pa_hook_result_t card_new_hook_callback(pa_core *c, pa_card_new_data *new if ((e = read_entry(u, new_data->name)) && e->profile[0]) { if (!new_data->active_profile) { - pa_card_new_data_set_profile(new_data, e->profile); pa_log_info("Restoring profile for card %s.", new_data->name); + pa_card_new_data_set_profile(new_data, e->profile); + new_data->save_profile = TRUE; } else pa_log_debug("Not restoring profile for card %s, because already set.", new_data->name); @@ -208,10 +208,9 @@ static pa_hook_result_t card_new_hook_callback(pa_core *c, pa_card_new_data *new int pa__init(pa_module*m) { pa_modargs *ma = NULL; struct userdata *u; - char *fname, *fn; + char *fname; pa_card *card; uint32_t idx; - int gdbm_cache_size; pa_assert(m); @@ -220,37 +219,23 @@ int pa__init(pa_module*m) { goto fail; } - m->userdata = u = pa_xnew(struct userdata, 1); + m->userdata = u = pa_xnew0(struct userdata, 1); u->core = m->core; u->module = m; - u->save_time_event = NULL; - u->gdbm_file = NULL; u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_CARD, subscribe_callback, u); u->card_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_CARD_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) card_new_hook_callback, u); - /* We include the host identifier in the file name because gdbm - * files are CPU dependant, and we don't want things to go wrong - * if we are on a multiarch system. */ - - fn = pa_sprintf_malloc("card-database."CANONICAL_HOST".gdbm"); - fname = pa_state_path(fn, TRUE); - pa_xfree(fn); - - if (!fname) + if (!(fname = pa_state_path("card-database", TRUE))) goto fail; - if (!(u->gdbm_file = gdbm_open(fname, 0, GDBM_WRCREAT|GDBM_NOLOCK, 0600, NULL))) { - pa_log("Failed to open volume database '%s': %s", fname, gdbm_strerror(gdbm_errno)); + if (!(u->database = pa_database_open(fname, TRUE))) { + pa_log("Failed to open volume database '%s': %s", fname, pa_cstrerror(errno)); pa_xfree(fname); goto fail; } - /* By default the cache of gdbm is rather large, let's reduce it a bit to save memory */ - gdbm_cache_size = 10; - gdbm_setopt(u->gdbm_file, GDBM_CACHESIZE, &gdbm_cache_size, sizeof(gdbm_cache_size)); - pa_log_info("Sucessfully opened database file '%s'.", fname); pa_xfree(fname); @@ -286,8 +271,8 @@ void pa__done(pa_module*m) { if (u->save_time_event) u->core->mainloop->time_free(u->save_time_event); - if (u->gdbm_file) - gdbm_close(u->gdbm_file); + if (u->database) + pa_database_close(u->database); pa_xfree(u); } diff --git a/src/modules/module-combine.c b/src/modules/module-combine.c index 43ad9680..8443a5a3 100644 --- a/src/modules/module-combine.c +++ b/src/modules/module-combine.c @@ -26,6 +26,7 @@ #include <stdio.h> #include <errno.h> +#include <pulse/rtclock.h> #include <pulse/timeval.h> #include <pulse/xmalloc.h> @@ -36,6 +37,7 @@ #include <pulsecore/sink-input.h> #include <pulsecore/memblockq.h> #include <pulsecore/log.h> +#include <pulsecore/core-rtclock.h> #include <pulsecore/core-util.h> #include <pulsecore/modargs.h> #include <pulsecore/namereg.h> @@ -43,7 +45,6 @@ #include <pulsecore/thread.h> #include <pulsecore/thread-mq.h> #include <pulsecore/rtpoll.h> -#include <pulsecore/rtclock.h> #include <pulsecore/core-error.h> #include <pulsecore/time-smoother.h> @@ -55,12 +56,13 @@ PA_MODULE_VERSION(PACKAGE_VERSION); PA_MODULE_LOAD_ONCE(FALSE); PA_MODULE_USAGE( "sink_name=<name for the sink> " + "sink_properties=<properties for the sink> " "slaves=<slave sinks> " "adjust_time=<seconds> " "resample_method=<method> " "format=<sample format> " - "channels=<number of channels> " "rate=<sample rate> " + "channels=<number of channels> " "channel_map=<channel map>"); #define DEFAULT_SINK_NAME "combined" @@ -69,16 +71,17 @@ PA_MODULE_USAGE( #define DEFAULT_ADJUST_TIME 10 -#define REQUEST_LATENCY_USEC (PA_USEC_PER_MSEC * 200) +#define BLOCK_USEC (PA_USEC_PER_MSEC * 200) static const char* const valid_modargs[] = { "sink_name", + "sink_properties", "slaves", "adjust_time", "resample_method", "format", - "channels", "rate", + "channels", "channel_map", NULL }; @@ -116,6 +119,7 @@ struct userdata { uint32_t adjust_time; pa_bool_t automatic; + pa_bool_t auto_desc; pa_hook_slot *sink_put_slot, *sink_unlink_slot, *sink_state_changed_slot; @@ -222,9 +226,8 @@ static void adjust_rates(struct userdata *u) { pa_asyncmsgq_send(u->sink->asyncmsgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_UPDATE_LATENCY, NULL, (int64_t) avg_total_latency, NULL); } -static void time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) { +static void time_callback(pa_mainloop_api *a, pa_time_event *e, const struct timeval *t, void *userdata) { struct userdata *u = userdata; - struct timeval n; pa_assert(u); pa_assert(a); @@ -232,9 +235,7 @@ static void time_callback(pa_mainloop_api*a, pa_time_event* e, const struct time adjust_rates(u); - pa_gettimeofday(&n); - n.tv_sec += (time_t) u->adjust_time; - u->sink->core->mainloop->time_restart(e, &n); + pa_core_rttime_restart(u->core, e, pa_rtclock_now() + u->adjust_time * PA_USEC_PER_SEC); } static void process_render_null(struct userdata *u, pa_usec_t now) { @@ -278,9 +279,8 @@ static void thread_func(void *userdata) { pa_make_realtime(u->core->realtime_priority+1); pa_thread_mq_install(&u->thread_mq); - pa_rtpoll_install(u->rtpoll); - u->thread_info.timestamp = pa_rtclock_usec(); + u->thread_info.timestamp = pa_rtclock_now(); u->thread_info.in_null_mode = FALSE; for (;;) { @@ -294,7 +294,7 @@ static void thread_func(void *userdata) { if (PA_SINK_IS_OPENED(u->sink->thread_info.state) && !u->thread_info.active_outputs) { pa_usec_t now; - now = pa_rtclock_usec(); + now = pa_rtclock_now(); if (!u->thread_info.in_null_mode || u->thread_info.timestamp <= now) process_render_null(u, now); @@ -591,7 +591,7 @@ static void unsuspend(struct userdata *u) { /* Let's resume */ for (o = pa_idxset_first(u->outputs, &idx); o; o = pa_idxset_next(u->outputs, &idx)) { - pa_sink_suspend(o->sink, FALSE); + pa_sink_suspend(o->sink, FALSE, PA_SUSPEND_IDLE); if (PA_SINK_IS_OPENED(pa_sink_get_state(o->sink))) enable_output(o); @@ -649,7 +649,7 @@ static void update_max_request(struct userdata *u) { if (max_request <= 0) max_request = pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec); - pa_sink_set_max_request(u->sink, max_request); + pa_sink_set_max_request_within_thread(u->sink, max_request); } /* Called from thread context of the io thread */ @@ -662,16 +662,16 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse pa_atomic_store(&u->thread_info.running, PA_PTR_TO_UINT(data) == PA_SINK_RUNNING); if (PA_PTR_TO_UINT(data) == PA_SINK_SUSPENDED) - pa_smoother_pause(u->thread_info.smoother, pa_rtclock_usec()); + pa_smoother_pause(u->thread_info.smoother, pa_rtclock_now()); else - pa_smoother_resume(u->thread_info.smoother, pa_rtclock_usec()); + pa_smoother_resume(u->thread_info.smoother, pa_rtclock_now(), TRUE); break; case PA_SINK_MESSAGE_GET_LATENCY: { pa_usec_t x, y, c, *delay = data; - x = pa_rtclock_usec(); + x = pa_rtclock_now(); y = pa_smoother_get(u->thread_info.smoother, x); c = pa_bytes_to_usec(u->thread_info.counter, &u->sink->sample_spec); @@ -728,7 +728,7 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse case SINK_MESSAGE_UPDATE_LATENCY: { pa_usec_t x, y, latency = (pa_usec_t) offset; - x = pa_rtclock_usec(); + x = pa_rtclock_now(); y = pa_bytes_to_usec(u->thread_info.counter, &u->sink->sample_spec); if (y > latency) @@ -757,6 +757,9 @@ static void update_description(struct userdata *u) { pa_assert(u); + if (!u->auto_desc) + return; + if (pa_idxset_isempty(u->outputs)) { pa_sink_set_description(u->sink, "Simultaneous output"); return; @@ -817,7 +820,7 @@ static int output_create_sink_input(struct output *o) { o->sink_input->kill = sink_input_kill_cb; o->sink_input->userdata = o; - pa_sink_input_set_requested_latency(o->sink_input, REQUEST_LATENCY_USEC); + pa_sink_input_set_requested_latency(o->sink_input, BLOCK_USEC); return 0; } @@ -871,7 +874,7 @@ static struct output *output_new(struct userdata *u, pa_sink *sink) { } if (PA_SINK_IS_OPENED(state) || state == PA_SINK_INIT) { - pa_sink_suspend(sink, FALSE); + pa_sink_suspend(sink, FALSE, PA_SUSPEND_IDLE); if (PA_SINK_IS_OPENED(pa_sink_get_state(sink))) if (output_create_sink_input(o) < 0) @@ -1043,7 +1046,14 @@ int pa__init(pa_module*m) { pa_atomic_store(&u->thread_info.running, FALSE); u->thread_info.in_null_mode = FALSE; u->thread_info.counter = 0; - u->thread_info.smoother = pa_smoother_new(PA_USEC_PER_SEC, PA_USEC_PER_SEC*2, TRUE, 10); + u->thread_info.smoother = pa_smoother_new( + PA_USEC_PER_SEC, + PA_USEC_PER_SEC*2, + TRUE, + TRUE, + 10, + 0, + FALSE); if (pa_modargs_get_value_u32(ma, "adjust_time", &u->adjust_time) < 0) { pa_log("Failed to parse adjust_time value"); @@ -1067,12 +1077,25 @@ int pa__init(pa_module*m) { pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME)); pa_sink_new_data_set_sample_spec(&data, &ss); pa_sink_new_data_set_channel_map(&data, &map); - pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Simultaneous Output"); pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "filter"); if (slaves) pa_proplist_sets(data.proplist, "combine.slaves", slaves); + if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) { + pa_log("Invalid properties"); + pa_sink_new_data_done(&data); + goto fail; + } + + /* Check proplist for a description & fill in a default value if not */ + u->auto_desc = FALSE; + if (NULL == pa_proplist_gets(data.proplist, PA_PROP_DEVICE_DESCRIPTION)) { + u->auto_desc = TRUE; + pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Simultaneous Output"); + } + + u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY); pa_sink_new_data_done(&data); @@ -1088,11 +1111,8 @@ int pa__init(pa_module*m) { pa_sink_set_rtpoll(u->sink, u->rtpoll); pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq); - pa_sink_set_latency_range(u->sink, REQUEST_LATENCY_USEC, REQUEST_LATENCY_USEC); - u->block_usec = u->sink->thread_info.max_latency; - - u->sink->thread_info.max_request = - pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec); + u->block_usec = BLOCK_USEC; + pa_sink_set_max_request(u->sink, pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec)); if (!u->automatic) { const char*split_state; @@ -1158,12 +1178,8 @@ int pa__init(pa_module*m) { if (o->sink_input) pa_sink_input_put(o->sink_input); - if (u->adjust_time > 0) { - struct timeval tv; - pa_gettimeofday(&tv); - tv.tv_sec += (time_t) u->adjust_time; - u->time_event = m->core->mainloop->time_new(m->core->mainloop, &tv, time_callback, u); - } + if (u->adjust_time > 0) + u->time_event = pa_core_rttime_new(m->core, pa_rtclock_now() + u->adjust_time * PA_USEC_PER_SEC, time_callback, u); pa_modargs_free(ma); diff --git a/src/modules/module-console-kit.c b/src/modules/module-console-kit.c index 3fba7ef6..a666073c 100644 --- a/src/modules/module-console-kit.c +++ b/src/modules/module-console-kit.c @@ -44,8 +44,8 @@ #include <pulsecore/namereg.h> #include <pulsecore/core-scache.h> #include <pulsecore/modargs.h> +#include <pulsecore/dbus-shared.h> -#include "dbus-util.h" #include "module-console-kit-symdef.h" PA_MODULE_AUTHOR("Lennart Poettering"); diff --git a/src/modules/module-cork-music-on-phone.c b/src/modules/module-cork-music-on-phone.c index c0f5eea4..d3a2b00e 100644 --- a/src/modules/module-cork-music-on-phone.c +++ b/src/modules/module-cork-music-on-phone.c @@ -143,7 +143,6 @@ static pa_hook_result_t sink_input_put_cb(pa_core *core, pa_sink_input *i, struc } static pa_hook_result_t sink_input_unlink_cb(pa_core *core, pa_sink_input *i, struct userdata *u) { - pa_core_assert_ref(core); pa_sink_input_assert_ref(i); return process(u, i, FALSE); diff --git a/src/modules/module-default-device-restore.c b/src/modules/module-default-device-restore.c index a25aafcb..27ae60e5 100644 --- a/src/modules/module-default-device-restore.c +++ b/src/modules/module-default-device-restore.c @@ -26,6 +26,7 @@ #include <errno.h> #include <stdio.h> +#include <pulse/rtclock.h> #include <pulse/timeval.h> #include <pulse/util.h> @@ -42,7 +43,7 @@ PA_MODULE_DESCRIPTION("Automatically restore the default sink and source"); PA_MODULE_VERSION(PACKAGE_VERSION); PA_MODULE_LOAD_ONCE(TRUE); -#define DEFAULT_SAVE_INTERVAL 5 +#define SAVE_INTERVAL (5 * PA_USEC_PER_SEC) struct userdata { pa_core *core; @@ -63,7 +64,7 @@ static void load(struct userdata *u) { char ln[256] = ""; pa_sink *s; - fgets(ln, sizeof(ln)-1, f); + (void) fgets(ln, sizeof(ln)-1, f); pa_strip_nl(ln); fclose(f); @@ -84,7 +85,7 @@ static void load(struct userdata *u) { char ln[256] = ""; pa_source *s; - fgets(ln, sizeof(ln)-1, f); + (void) fgets(ln, sizeof(ln)-1, f); pa_strip_nl(ln); fclose(f); @@ -127,7 +128,7 @@ static void save(struct userdata *u) { u->modified = FALSE; } -static void time_cb(pa_mainloop_api *a, pa_time_event *e, const struct timeval *tv, void *userdata) { +static void time_cb(pa_mainloop_api *a, pa_time_event *e, const struct timeval *t, void *userdata) { struct userdata *u = userdata; pa_assert(u); @@ -146,12 +147,8 @@ static void subscribe_cb(pa_core *c, pa_subscription_event_type_t t, uint32_t id u->modified = TRUE; - if (!u->time_event) { - struct timeval tv; - pa_gettimeofday(&tv); - pa_timeval_add(&tv, DEFAULT_SAVE_INTERVAL*PA_USEC_PER_SEC); - u->time_event = u->core->mainloop->time_new(u->core->mainloop, &tv, time_cb, u); - } + if (!u->time_event) + u->time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + SAVE_INTERVAL, time_cb, u); } int pa__init(pa_module *m) { diff --git a/src/modules/module-defs.h.m4 b/src/modules/module-defs.h.m4 index f9924cfa..b6a60b6a 100644 --- a/src/modules/module-defs.h.m4 +++ b/src/modules/module-defs.h.m4 @@ -17,6 +17,7 @@ gen_symbol(pa__get_author) gen_symbol(pa__get_description) gen_symbol(pa__get_usage) gen_symbol(pa__get_version) +gen_symbol(pa__get_deprecated) gen_symbol(pa__load_once) gen_symbol(pa__get_n_used) @@ -28,6 +29,7 @@ const char* pa__get_author(void); const char* pa__get_description(void); const char* pa__get_usage(void); const char* pa__get_version(void); +const char* pa__get_deprecated(void); pa_bool_t pa__load_once(void); #endif diff --git a/src/modules/module-detect.c b/src/modules/module-detect.c index 49127abc..18479df3 100644 --- a/src/modules/module-detect.c +++ b/src/modules/module-detect.c @@ -50,6 +50,7 @@ PA_MODULE_DESCRIPTION("Detect available audio hardware and load matching drivers PA_MODULE_VERSION(PACKAGE_VERSION); PA_MODULE_LOAD_ONCE(TRUE); PA_MODULE_USAGE("just-one=<boolean>"); +PA_MODULE_DEPRECATED("Please use module-hal-detect instead of module-detect!"); static const char* const valid_modargs[] = { "just-one", diff --git a/src/modules/module-device-restore.c b/src/modules/module-device-restore.c index 0ca3dd83..120b762c 100644 --- a/src/modules/module-device-restore.c +++ b/src/modules/module-device-restore.c @@ -30,12 +30,12 @@ #include <stdio.h> #include <stdlib.h> #include <ctype.h> -#include <gdbm.h> #include <pulse/xmalloc.h> #include <pulse/volume.h> #include <pulse/timeval.h> #include <pulse/util.h> +#include <pulse/rtclock.h> #include <pulsecore/core-error.h> #include <pulsecore/module.h> @@ -46,6 +46,7 @@ #include <pulsecore/sink-input.h> #include <pulsecore/source-output.h> #include <pulsecore/namereg.h> +#include <pulsecore/database.h> #include "module-device-restore-symdef.h" @@ -54,14 +55,16 @@ PA_MODULE_DESCRIPTION("Automatically restore the volume/mute state of devices"); PA_MODULE_VERSION(PACKAGE_VERSION); PA_MODULE_LOAD_ONCE(TRUE); PA_MODULE_USAGE( + "restore_port=<Save/restore port?> " "restore_volume=<Save/restore volumes?> " "restore_muted=<Save/restore muted states?>"); -#define SAVE_INTERVAL 10 +#define SAVE_INTERVAL (10 * PA_USEC_PER_SEC) static const char* const valid_modargs[] = { "restore_volume", "restore_muted", + "restore_port", NULL }; @@ -70,78 +73,82 @@ struct userdata { pa_module *module; pa_subscription *subscription; pa_hook_slot + *sink_new_hook_slot, *sink_fixate_hook_slot, + *source_new_hook_slot, *source_fixate_hook_slot; pa_time_event *save_time_event; - GDBM_FILE gdbm_file; + pa_database *database; pa_bool_t restore_volume:1; pa_bool_t restore_muted:1; + pa_bool_t restore_port:1; }; -#define ENTRY_VERSION 1 +#define ENTRY_VERSION 2 struct entry { uint8_t version; + pa_bool_t muted_valid:1, volume_valid:1, port_valid:1; pa_bool_t muted:1; pa_channel_map channel_map; pa_cvolume volume; + char port[PA_NAME_MAX]; } PA_GCC_PACKED; -static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) { +static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) { struct userdata *u = userdata; pa_assert(a); pa_assert(e); - pa_assert(tv); pa_assert(u); pa_assert(e == u->save_time_event); u->core->mainloop->time_free(u->save_time_event); u->save_time_event = NULL; - gdbm_sync(u->gdbm_file); + pa_database_sync(u->database); pa_log_info("Synced."); } static struct entry* read_entry(struct userdata *u, const char *name) { - datum key, data; + pa_datum key, data; struct entry *e; pa_assert(u); pa_assert(name); - key.dptr = (char*) name; - key.dsize = (int) strlen(name); + key.data = (char*) name; + key.size = strlen(name); - data = gdbm_fetch(u->gdbm_file, key); + pa_zero(data); - if (!data.dptr) + if (!pa_database_get(u->database, &key, &data)) goto fail; - if (data.dsize != sizeof(struct entry)) { - pa_log_debug("Database contains entry for device %s of wrong size %lu != %lu. Probably due to upgrade, ignoring.", name, (unsigned long) data.dsize, (unsigned long) sizeof(struct entry)); + if (data.size != sizeof(struct entry)) { + pa_log_debug("Database contains entry for device %s of wrong size %lu != %lu. Probably due to upgrade, ignoring.", name, (unsigned long) data.size, (unsigned long) sizeof(struct entry)); goto fail; } - e = (struct entry*) data.dptr; + e = (struct entry*) data.data; if (e->version != ENTRY_VERSION) { pa_log_debug("Version of database entry for device %s doesn't match our version. Probably due to upgrade, ignoring.", name); goto fail; } - if (!(pa_cvolume_valid(&e->volume))) { - pa_log_warn("Invalid volume stored in database for device %s", name); + if (!memchr(e->port, 0, sizeof(e->port))) { + pa_log_warn("Database contains entry for device %s with missing NUL byte in port name", name); goto fail; } - if (!(pa_channel_map_valid(&e->channel_map))) { + if (e->volume_valid && !pa_channel_map_valid(&e->channel_map)) { pa_log_warn("Invalid channel map stored in database for device %s", name); goto fail; } - if (e->volume.channels != e->channel_map.channels) { + if (e->volume_valid && (!pa_cvolume_valid(&e->volume) || !pa_cvolume_compatible_with_channel_map(&e->volume, &e->channel_map))) { pa_log_warn("Volume and channel map don't match in database entry for device %s", name); goto fail; } @@ -150,26 +157,41 @@ static struct entry* read_entry(struct userdata *u, const char *name) { fail: - pa_xfree(data.dptr); + pa_datum_free(&data); return NULL; } static void trigger_save(struct userdata *u) { - struct timeval tv; - if (u->save_time_event) return; - pa_gettimeofday(&tv); - tv.tv_sec += SAVE_INTERVAL; - u->save_time_event = u->core->mainloop->time_new(u->core->mainloop, &tv, save_time_callback, u); + u->save_time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + SAVE_INTERVAL, save_time_callback, u); +} + +static pa_bool_t entries_equal(const struct entry *a, const struct entry *b) { + pa_cvolume t; + + if (a->port_valid != b->port_valid || + (a->port_valid && strncmp(a->port, b->port, sizeof(a->port)))) + return FALSE; + + if (a->muted_valid != b->muted_valid || + (a->muted_valid && (a->muted != b->muted))) + return FALSE; + + t = b->volume; + if (a->volume_valid != b->volume_valid || + (a->volume_valid && !pa_cvolume_equal(pa_cvolume_remap(&t, &b->channel_map, &a->channel_map), &a->volume))) + return FALSE; + + return TRUE; } static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) { struct userdata *u = userdata; struct entry entry, *old; char *name; - datum key, data; + pa_datum key, data; pa_assert(c); pa_assert(u); @@ -180,7 +202,7 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3 t != (PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE)) return; - memset(&entry, 0, sizeof(entry)); + pa_zero(entry); entry.version = ENTRY_VERSION; if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK) { @@ -190,9 +212,25 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3 return; name = pa_sprintf_malloc("sink:%s", sink->name); - entry.channel_map = sink->channel_map; - entry.volume = *pa_sink_get_volume(sink, FALSE); - entry.muted = pa_sink_get_mute(sink, FALSE); + + if ((old = read_entry(u, name))) + entry = *old; + + if (sink->save_volume) { + entry.channel_map = sink->channel_map; + entry.volume = *pa_sink_get_volume(sink, FALSE, TRUE); + entry.volume_valid = TRUE; + } + + if (sink->save_muted) { + entry.muted = pa_sink_get_mute(sink, FALSE); + entry.muted_valid = TRUE; + } + + if (sink->save_port) { + pa_strlcpy(entry.port, sink->active_port ? sink->active_port->name : "", sizeof(entry.port)); + entry.port_valid = TRUE; + } } else { pa_source *source; @@ -203,16 +241,30 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3 return; name = pa_sprintf_malloc("source:%s", source->name); - entry.channel_map = source->channel_map; - entry.volume = *pa_source_get_volume(source, FALSE); - entry.muted = pa_source_get_mute(source, FALSE); - } - if ((old = read_entry(u, name))) { + if ((old = read_entry(u, name))) + entry = *old; + + if (source->save_volume) { + entry.channel_map = source->channel_map; + entry.volume = *pa_source_get_volume(source, FALSE); + entry.volume_valid = TRUE; + } + + if (source->save_muted) { + entry.muted = pa_source_get_mute(source, FALSE); + entry.muted_valid = TRUE; + } + + if (source->save_port) { + pa_strlcpy(entry.port, source->active_port ? source->active_port->name : "", sizeof(entry.port)); + entry.port_valid = TRUE; + } + } - if (pa_cvolume_equal(pa_cvolume_remap(&old->volume, &old->channel_map, &entry.channel_map), &entry.volume) && - !old->muted == !entry.muted) { + if (old) { + if (entries_equal(old, &entry)) { pa_xfree(old); pa_xfree(name); return; @@ -221,46 +273,86 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3 pa_xfree(old); } - key.dptr = name; - key.dsize = (int) strlen(name); + key.data = name; + key.size = strlen(name); - data.dptr = (void*) &entry; - data.dsize = sizeof(entry); + data.data = &entry; + data.size = sizeof(entry); - pa_log_info("Storing volume/mute for device %s.", name); + pa_log_info("Storing volume/mute/port for device %s.", name); - gdbm_store(u->gdbm_file, key, data, GDBM_REPLACE); + pa_database_set(u->database, &key, &data, TRUE); pa_xfree(name); trigger_save(u); } +static pa_hook_result_t sink_new_hook_callback(pa_core *c, pa_sink_new_data *new_data, struct userdata *u) { + char *name; + struct entry *e; + + pa_assert(c); + pa_assert(new_data); + pa_assert(u); + pa_assert(u->restore_port); + + name = pa_sprintf_malloc("sink:%s", new_data->name); + + if ((e = read_entry(u, name))) { + + if (e->port_valid) { + if (!new_data->active_port) { + pa_log_info("Restoring port for sink %s.", name); + pa_sink_new_data_set_port(new_data, e->port); + new_data->save_port = TRUE; + } else + pa_log_debug("Not restoring port for sink %s, because already set.", name); + } + + pa_xfree(e); + } + + pa_xfree(name); + + return PA_HOOK_OK; +} + static pa_hook_result_t sink_fixate_hook_callback(pa_core *c, pa_sink_new_data *new_data, struct userdata *u) { char *name; struct entry *e; + pa_assert(c); pa_assert(new_data); + pa_assert(u); + pa_assert(u->restore_volume || u->restore_muted); name = pa_sprintf_malloc("sink:%s", new_data->name); if ((e = read_entry(u, name))) { - if (u->restore_volume) { + if (u->restore_volume && e->volume_valid) { if (!new_data->volume_is_set) { + pa_cvolume v; + pa_log_info("Restoring volume for sink %s.", new_data->name); - pa_sink_new_data_set_volume(new_data, pa_cvolume_remap(&e->volume, &e->channel_map, &new_data->channel_map)); + + v = e->volume; + pa_cvolume_remap(&v, &e->channel_map, &new_data->channel_map); + pa_sink_new_data_set_volume(new_data, &v); + + new_data->save_volume = TRUE; } else pa_log_debug("Not restoring volume for sink %s, because already set.", new_data->name); - } - if (u->restore_muted) { + if (u->restore_muted && e->muted_valid) { if (!new_data->muted_is_set) { pa_log_info("Restoring mute state for sink %s.", new_data->name); pa_sink_new_data_set_muted(new_data, e->muted); + new_data->save_muted = TRUE; } else pa_log_debug("Not restoring mute state for sink %s, because already set.", new_data->name); } @@ -273,30 +365,71 @@ static pa_hook_result_t sink_fixate_hook_callback(pa_core *c, pa_sink_new_data * return PA_HOOK_OK; } +static pa_hook_result_t source_new_hook_callback(pa_core *c, pa_source_new_data *new_data, struct userdata *u) { + char *name; + struct entry *e; + + pa_assert(c); + pa_assert(new_data); + pa_assert(u); + pa_assert(u->restore_port); + + name = pa_sprintf_malloc("source:%s", new_data->name); + + if ((e = read_entry(u, name))) { + + if (e->port_valid) { + if (!new_data->active_port) { + pa_log_info("Restoring port for source %s.", name); + pa_source_new_data_set_port(new_data, e->port); + new_data->save_port = TRUE; + } else + pa_log_debug("Not restoring port for source %s, because already set.", name); + } + + pa_xfree(e); + } + + pa_xfree(name); + + return PA_HOOK_OK; +} + static pa_hook_result_t source_fixate_hook_callback(pa_core *c, pa_source_new_data *new_data, struct userdata *u) { char *name; struct entry *e; + pa_assert(c); pa_assert(new_data); + pa_assert(u); + pa_assert(u->restore_volume || u->restore_muted); name = pa_sprintf_malloc("source:%s", new_data->name); if ((e = read_entry(u, name))) { - if (u->restore_volume) { + if (u->restore_volume && e->volume_valid) { if (!new_data->volume_is_set) { + pa_cvolume v; + pa_log_info("Restoring volume for source %s.", new_data->name); - pa_source_new_data_set_volume(new_data, pa_cvolume_remap(&e->volume, &e->channel_map, &new_data->channel_map)); + + v = e->volume; + pa_cvolume_remap(&v, &e->channel_map, &new_data->channel_map); + pa_source_new_data_set_volume(new_data, &v); + + new_data->save_volume = TRUE; } else pa_log_debug("Not restoring volume for source %s, because already set.", new_data->name); } - if (u->restore_muted) { + if (u->restore_muted && e->muted_valid) { if (!new_data->muted_is_set) { pa_log_info("Restoring mute state for source %s.", new_data->name); pa_source_new_data_set_muted(new_data, e->muted); + new_data->save_muted = TRUE; } else pa_log_debug("Not restoring mute state for source %s, because already set.", new_data->name); } @@ -312,12 +445,11 @@ static pa_hook_result_t source_fixate_hook_callback(pa_core *c, pa_source_new_da int pa__init(pa_module*m) { pa_modargs *ma = NULL; struct userdata *u; - char *fname, *fn; + char *fname; pa_sink *sink; pa_source *source; uint32_t idx; - pa_bool_t restore_volume = TRUE, restore_muted = TRUE; - int gdbm_cache_size; + pa_bool_t restore_volume = TRUE, restore_muted = TRUE, restore_port = TRUE; pa_assert(m); @@ -327,50 +459,43 @@ int pa__init(pa_module*m) { } if (pa_modargs_get_value_boolean(ma, "restore_volume", &restore_volume) < 0 || - pa_modargs_get_value_boolean(ma, "restore_muted", &restore_muted) < 0) { - pa_log("restore_volume= and restore_muted= expect boolean arguments"); + pa_modargs_get_value_boolean(ma, "restore_muted", &restore_muted) < 0 || + pa_modargs_get_value_boolean(ma, "restore_port", &restore_port) < 0) { + pa_log("restore_port=, restore_volume= and restore_muted= expect boolean arguments"); goto fail; } - if (!restore_muted && !restore_volume) - pa_log_warn("Neither restoring volume nor restoring muted enabled!"); + if (!restore_muted && !restore_volume && !restore_port) + pa_log_warn("Neither restoring volume, nor restoring muted, nor restoring port enabled!"); - m->userdata = u = pa_xnew(struct userdata, 1); + m->userdata = u = pa_xnew0(struct userdata, 1); u->core = m->core; u->module = m; - u->save_time_event = NULL; u->restore_volume = restore_volume; u->restore_muted = restore_muted; - u->gdbm_file = NULL; + u->restore_port = restore_port; u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_SINK|PA_SUBSCRIPTION_MASK_SOURCE, subscribe_callback, u); + if (restore_port) { + u->sink_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) sink_new_hook_callback, u); + u->source_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) source_new_hook_callback, u); + } + if (restore_muted || restore_volume) { u->sink_fixate_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_FIXATE], PA_HOOK_EARLY, (pa_hook_cb_t) sink_fixate_hook_callback, u); u->source_fixate_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_FIXATE], PA_HOOK_EARLY, (pa_hook_cb_t) source_fixate_hook_callback, u); } - /* We include the host identifier in the file name because gdbm - * files are CPU dependant, and we don't want things to go wrong - * if we are on a multiarch system. */ - - fn = pa_sprintf_malloc("device-volumes."CANONICAL_HOST".gdbm"); - fname = pa_state_path(fn, TRUE); - pa_xfree(fn); - - if (!fname) + if (!(fname = pa_state_path("device-volumes", TRUE))) goto fail; - if (!(u->gdbm_file = gdbm_open(fname, 0, GDBM_WRCREAT|GDBM_NOLOCK, 0600, NULL))) { - pa_log("Failed to open volume database '%s': %s", fname, gdbm_strerror(gdbm_errno)); + if (!(u->database = pa_database_open(fname, TRUE))) { + pa_log("Failed to open volume database '%s': %s", fname, pa_cstrerror(errno)); pa_xfree(fname); goto fail; } - /* By default the cache of gdbm is rather large, let's reduce it a bit to save memory */ - gdbm_cache_size = 10; - gdbm_setopt(u->gdbm_file, GDBM_CACHESIZE, &gdbm_cache_size, sizeof(gdbm_cache_size)); - pa_log_info("Sucessfully opened database file '%s'.", fname); pa_xfree(fname); @@ -407,12 +532,16 @@ void pa__done(pa_module*m) { pa_hook_slot_free(u->sink_fixate_hook_slot); if (u->source_fixate_hook_slot) pa_hook_slot_free(u->source_fixate_hook_slot); + if (u->sink_new_hook_slot) + pa_hook_slot_free(u->sink_new_hook_slot); + if (u->source_new_hook_slot) + pa_hook_slot_free(u->source_new_hook_slot); if (u->save_time_event) u->core->mainloop->time_free(u->save_time_event); - if (u->gdbm_file) - gdbm_close(u->gdbm_file); + if (u->database) + pa_database_close(u->database); pa_xfree(u); } diff --git a/src/modules/module-esound-sink.c b/src/modules/module-esound-sink.c index 2b45e302..d7c678ca 100644 --- a/src/modules/module-esound-sink.c +++ b/src/modules/module-esound-sink.c @@ -41,13 +41,15 @@ #include <linux/sockios.h> #endif -#include <pulse/xmalloc.h> +#include <pulse/rtclock.h> #include <pulse/timeval.h> +#include <pulse/xmalloc.h> #include <pulsecore/core-error.h> #include <pulsecore/iochannel.h> #include <pulsecore/sink.h> #include <pulsecore/module.h> +#include <pulsecore/core-rtclock.h> #include <pulsecore/core-util.h> #include <pulsecore/modargs.h> #include <pulsecore/log.h> @@ -57,7 +59,6 @@ #include <pulsecore/thread-mq.h> #include <pulsecore/thread.h> #include <pulsecore/time-smoother.h> -#include <pulsecore/rtclock.h> #include <pulsecore/socket-util.h> #include "module-esound-sink-symdef.h" @@ -68,10 +69,11 @@ PA_MODULE_VERSION(PACKAGE_VERSION); PA_MODULE_LOAD_ONCE(FALSE); PA_MODULE_USAGE( "sink_name=<name for the sink> " + "sink_properties=<properties for the sink> " "server=<address> cookie=<filename> " "format=<sample format> " - "channels=<number of channels> " - "rate=<sample rate>"); + "rate=<sample rate> " + "channels=<number of channels>"); #define DEFAULT_SINK_NAME "esound_out" @@ -118,12 +120,13 @@ struct userdata { }; static const char* const valid_modargs[] = { + "sink_name", + "sink_properties", "server", "cookie", - "rate", "format", + "rate", "channels", - "sink_name", NULL }; @@ -143,14 +146,14 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse case PA_SINK_SUSPENDED: pa_assert(PA_SINK_IS_OPENED(u->sink->thread_info.state)); - pa_smoother_pause(u->smoother, pa_rtclock_usec()); + pa_smoother_pause(u->smoother, pa_rtclock_now()); break; case PA_SINK_IDLE: case PA_SINK_RUNNING: if (u->sink->thread_info.state == PA_SINK_SUSPENDED) - pa_smoother_resume(u->smoother, pa_rtclock_usec()); + pa_smoother_resume(u->smoother, pa_rtclock_now(), TRUE); break; @@ -165,7 +168,7 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse case PA_SINK_MESSAGE_GET_LATENCY: { pa_usec_t w, r; - r = pa_smoother_get(u->smoother, pa_rtclock_usec()); + r = pa_smoother_get(u->smoother, pa_rtclock_now()); w = pa_bytes_to_usec((uint64_t) u->offset + u->memchunk.length, &u->sink->sample_spec); *((pa_usec_t*) data) = w > r ? w - r : 0; @@ -198,9 +201,8 @@ static void thread_func(void *userdata) { pa_log_debug("Thread starting up"); pa_thread_mq_install(&u->thread_mq); - pa_rtpoll_install(u->rtpoll); - pa_smoother_set_time_offset(u->smoother, pa_rtclock_usec()); + pa_smoother_set_time_offset(u->smoother, pa_rtclock_now()); for (;;) { int ret; @@ -293,7 +295,7 @@ static void thread_func(void *userdata) { else usec = 0; - pa_smoother_put(u->smoother, pa_rtclock_usec(), usec); + pa_smoother_put(u->smoother, pa_rtclock_now(), usec); } /* Hmm, nothing to do. Let's sleep */ @@ -354,6 +356,9 @@ static int do_write(struct userdata *u) { } if (!u->write_data && u->state == STATE_PREPARE) { + int so_sndbuf = 0; + socklen_t sl = sizeof(int); + /* OK, we're done with sending all control data we need to, so * let's hand the socket over to the IO thread now */ @@ -366,6 +371,13 @@ static int do_write(struct userdata *u) { pa_make_tcp_socket_low_delay(u->fd); + if (getsockopt(u->fd, SOL_SOCKET, SO_SNDBUF, &so_sndbuf, &sl) < 0) + pa_log_warn("getsockopt(SO_SNDBUF) failed: %s", pa_cstrerror(errno)); + else { + pa_log_debug("SO_SNDBUF is %zu.", (size_t) so_sndbuf); + pa_sink_set_max_request(u->sink, PA_MAX((size_t) so_sndbuf, u->block_size)); + } + pa_log_debug("Connection authenticated, handing fd to IO thread..."); pa_asyncmsgq_post(u->thread_mq.inq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_PASS_SOCKET, NULL, 0, NULL, NULL); @@ -535,7 +547,14 @@ int pa__init(pa_module*m) { u->module = m; m->userdata = u; u->fd = -1; - u->smoother = pa_smoother_new(PA_USEC_PER_SEC, PA_USEC_PER_SEC*2, TRUE, 10); + u->smoother = pa_smoother_new( + PA_USEC_PER_SEC, + PA_USEC_PER_SEC*2, + TRUE, + TRUE, + 10, + 0, + FALSE); pa_memchunk_reset(&u->memchunk); u->offset = 0; @@ -566,7 +585,14 @@ int pa__init(pa_module*m) { pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME)); pa_sink_new_data_set_sample_spec(&data, &ss); pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, espeaker); - pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Esound sink '%s'", espeaker); + pa_proplist_sets(data.proplist, PA_PROP_DEVICE_API, "esd"); + pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "EsounD Output on %s", espeaker); + + if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) { + pa_log("Invalid properties"); + pa_sink_new_data_done(&data); + goto fail; + } u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY|PA_SINK_NETWORK); pa_sink_new_data_done(&data); @@ -582,7 +608,7 @@ int pa__init(pa_module*m) { pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq); pa_sink_set_rtpoll(u->sink, u->rtpoll); - if (!(u->client = pa_socket_client_new_string(u->core->mainloop, espeaker, ESD_DEFAULT_PORT))) { + if (!(u->client = pa_socket_client_new_string(u->core->mainloop, TRUE, espeaker, ESD_DEFAULT_PORT))) { pa_log("Failed to connect to server."); goto fail; } diff --git a/src/modules/module-hal-detect.c b/src/modules/module-hal-detect.c index 9b0d71c9..658b3e55 100644 --- a/src/modules/module-hal-detect.c +++ b/src/modules/module-hal-detect.c @@ -45,10 +45,10 @@ #include <pulsecore/namereg.h> #include <pulsecore/core-scache.h> #include <pulsecore/modargs.h> +#include <pulsecore/dbus-shared.h> #include <hal/libhal.h> -#include "dbus-util.h" #include "module-hal-detect-symdef.h" PA_MODULE_AUTHOR("Shahms King"); @@ -64,6 +64,7 @@ PA_MODULE_USAGE("api=<alsa> " #elif defined(HAVE_OSS) PA_MODULE_USAGE("api=<oss>"); #endif +PA_MODULE_DEPRECATED("Please use module-udev-detect instead of module-hal-detect!"); struct device { char *udi, *originating_udi; @@ -121,6 +122,7 @@ static const char *strip_udi(const char *udi) { enum alsa_type { ALSA_TYPE_PLAYBACK, ALSA_TYPE_CAPTURE, + ALSA_TYPE_CONTROL, ALSA_TYPE_OTHER }; @@ -141,6 +143,8 @@ static enum alsa_type hal_alsa_device_get_type(LibHalContext *context, const cha t = ALSA_TYPE_PLAYBACK; else if (pa_streq(type, "capture")) t = ALSA_TYPE_CAPTURE; + else if (pa_streq(type, "control")) + t = ALSA_TYPE_CONTROL; libhal_free_string(type); @@ -171,7 +175,8 @@ static pa_bool_t hal_alsa_device_is_modem(LibHalContext *context, const char *ud finish: if (dbus_error_is_set(&error)) { - pa_log_error("D-Bus error while parsing HAL ALSA data: %s: %s", error.name, error.message); + if (!dbus_error_has_name(&error, "org.freedesktop.Hal.NoSuchProperty")) + pa_log_error("D-Bus error while parsing HAL ALSA data: %s: %s", error.name, error.message); dbus_error_free(&error); } @@ -193,10 +198,23 @@ static int hal_device_load_alsa(struct userdata *u, const char *udi, struct devi /* We only care for PCM devices */ type = hal_alsa_device_get_type(u->context, udi); - if (type == ALSA_TYPE_OTHER) + + /* For each ALSA card that appears the control device will be the + * last one to be created, this is considered part of the ALSA + * usperspace API. We rely on this and load our modules only when + * the control device is available assuming that *all* device + * nodes have been properly created and assigned the right ACLs at + * that time. Also see: + * + * http://mailman.alsa-project.org/pipermail/alsa-devel/2009-April/015958.html + * + * and the associated thread.*/ + + if (type != ALSA_TYPE_CONTROL) goto fail; - /* We don't care for modems */ + /* We don't care for modems -- this is most likely not set for + * control devices, so kind of pointless here. */ if (hal_alsa_device_is_modem(u->context, udi)) goto fail; @@ -215,7 +233,7 @@ static int hal_device_load_alsa(struct userdata *u, const char *udi, struct devi goto fail; card_name = pa_sprintf_malloc("alsa_card.%s", strip_udi(originating_udi)); - args = pa_sprintf_malloc("device_id=%u name=%s card_name=%s tsched=%i", card, strip_udi(originating_udi), card_name, (int) u->use_tsched); + args = pa_sprintf_malloc("device_id=%u name=\"%s\" card_name=\"%s\" tsched=%i card_properties=\"module-hal-detect.discovered=1\"", card, strip_udi(originating_udi), card_name, (int) u->use_tsched); pa_log_debug("Loading module-alsa-card with arguments '%s'", args); m = pa_module_load(u->core, "module-alsa-card", args); @@ -411,9 +429,10 @@ static int hal_device_add_all(struct userdata *u) { for (i = 0; i < n; i++) { struct device *d; - if ((d = hal_device_add(u, udis[i]))) + if ((d = hal_device_add(u, udis[i]))) { count++; - else + pa_log_debug("Loaded device %s", udis[i]); + } else pa_log_debug("Not loaded device %s", udis[i]); } } @@ -449,6 +468,8 @@ static void device_added_cb(LibHalContext *context, const char *udi) { if (!hal_device_add(u, udi)) pa_log_debug("Not loaded device %s", udi); + else + pa_log_debug("Loaded device %s", udi); finish: if (dbus_error_is_set(&error)) { @@ -547,7 +568,7 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *message, vo pa_sink *sink; if ((sink = pa_namereg_get(u->core, d->sink_name, PA_NAMEREG_SINK))) { - pa_bool_t success = pa_sink_suspend(sink, suspend) >= 0; + pa_bool_t success = pa_sink_suspend(sink, suspend, PA_SUSPEND_SESSION) >= 0; if (!success && !suspend) d->acl_race_fix = TRUE; /* resume failed, let's try again */ @@ -560,7 +581,7 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *message, vo pa_source *source; if ((source = pa_namereg_get(u->core, d->source_name, PA_NAMEREG_SOURCE))) { - pa_bool_t success = pa_source_suspend(source, suspend) >= 0; + pa_bool_t success = pa_source_suspend(source, suspend, PA_SUSPEND_SESSION) >= 0; if (!success && !suspend) d->acl_race_fix = TRUE; /* resume failed, let's try again */ @@ -573,7 +594,7 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *message, vo pa_card *card; if ((card = pa_namereg_get(u->core, d->card_name, PA_NAMEREG_CARD))) { - pa_bool_t success = pa_card_suspend(card, suspend) >= 0; + pa_bool_t success = pa_card_suspend(card, suspend, PA_SUSPEND_SESSION) >= 0; if (!success && !suspend) d->acl_race_fix = TRUE; /* resume failed, let's try again */ @@ -617,21 +638,21 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *message, vo pa_sink *sink; if ((sink = pa_namereg_get(u->core, d->sink_name, PA_NAMEREG_SINK))) - pa_sink_suspend(sink, FALSE); + pa_sink_suspend(sink, FALSE, PA_SUSPEND_SESSION); } if (d->source_name) { pa_source *source; if ((source = pa_namereg_get(u->core, d->source_name, PA_NAMEREG_SOURCE))) - pa_source_suspend(source, FALSE); + pa_source_suspend(source, FALSE, PA_SUSPEND_SESSION); } if (d->card_name) { pa_card *card; if ((card = pa_namereg_get(u->core, d->source_name, PA_NAMEREG_CARD))) - pa_card_suspend(card, FALSE); + pa_card_suspend(card, FALSE, PA_SUSPEND_SESSION); } } diff --git a/src/modules/module-intended-roles.c b/src/modules/module-intended-roles.c new file mode 100644 index 00000000..c697209a --- /dev/null +++ b/src/modules/module-intended-roles.c @@ -0,0 +1,428 @@ +/*** + This file is part of PulseAudio. + + Copyright 2009 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.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 + along with PulseAudio; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <pulse/xmalloc.h> +#include <pulse/volume.h> +#include <pulse/timeval.h> +#include <pulse/util.h> + +#include <pulsecore/core-error.h> +#include <pulsecore/module.h> +#include <pulsecore/core-util.h> +#include <pulsecore/modargs.h> +#include <pulsecore/log.h> +#include <pulsecore/core-subscribe.h> +#include <pulsecore/sink-input.h> +#include <pulsecore/source-output.h> +#include <pulsecore/namereg.h> + +#include "module-intended-roles-symdef.h" + +PA_MODULE_AUTHOR("Lennart Poettering"); +PA_MODULE_DESCRIPTION("Automatically set device of streams based of intended roles of devices"); +PA_MODULE_VERSION(PACKAGE_VERSION); +PA_MODULE_LOAD_ONCE(TRUE); +PA_MODULE_USAGE( + "on_hotplug=<When new device becomes available, recheck streams?> " + "on_rescue=<When device becomes unavailable, recheck streams?>"); + +static const char* const valid_modargs[] = { + "on_hotplug", + "on_rescue", + NULL +}; + +struct userdata { + pa_core *core; + pa_module *module; + + pa_hook_slot + *sink_input_new_hook_slot, + *source_output_new_hook_slot, + *sink_put_hook_slot, + *source_put_hook_slot, + *sink_unlink_hook_slot, + *source_unlink_hook_slot; + + pa_bool_t on_hotplug:1; + pa_bool_t on_rescue:1; +}; + +static pa_bool_t role_match(pa_proplist *proplist, const char *role) { + const char *ir; + char *r; + const char *state = NULL; + + if (!(ir = pa_proplist_gets(proplist, PA_PROP_DEVICE_INTENDED_ROLES))) + return FALSE; + + while ((r = pa_split_spaces(ir, &state))) { + + if (pa_streq(role, r)) { + pa_xfree(r); + return TRUE; + } + + pa_xfree(r); + } + + return FALSE; +} + +static pa_hook_result_t sink_input_new_hook_callback(pa_core *c, pa_sink_input_new_data *new_data, struct userdata *u) { + const char *role; + pa_sink *s, *def; + uint32_t idx; + + pa_assert(c); + pa_assert(new_data); + pa_assert(u); + + if (!new_data->proplist) { + pa_log_debug("New stream lacks property data."); + return PA_HOOK_OK; + } + + if (new_data->sink) { + pa_log_debug("Not setting device for stream %s, because already set.", pa_strnull(pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_NAME))); + return PA_HOOK_OK; + } + + if (!(role = pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_ROLE))) { + pa_log_debug("Not setting device for stream %s, because it lacks role.", pa_strnull(pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_NAME))); + return PA_HOOK_OK; + } + + /* Prefer the default sink over any other sink, just in case... */ + if ((def = pa_namereg_get_default_sink(c))) + if (role_match(def->proplist, role)) { + new_data->sink = def; + new_data->save_sink = FALSE; + return PA_HOOK_OK; + } + + PA_IDXSET_FOREACH(s, c->sinks, idx) { + if (s == def) + continue; + + if (role_match(s->proplist, role)) { + new_data->sink = s; + new_data->save_sink = FALSE; + return PA_HOOK_OK; + } + } + + return PA_HOOK_OK; +} + +static pa_hook_result_t source_output_new_hook_callback(pa_core *c, pa_source_output_new_data *new_data, struct userdata *u) { + const char *role; + pa_source *s, *def; + uint32_t idx; + + pa_assert(c); + pa_assert(new_data); + pa_assert(u); + + if (!new_data->proplist) { + pa_log_debug("New stream lacks property data."); + return PA_HOOK_OK; + } + + if (new_data->source) { + pa_log_debug("Not setting device for stream %s, because already set.", pa_strnull(pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_NAME))); + return PA_HOOK_OK; + } + + if (!(role = pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_ROLE))) { + pa_log_debug("Not setting device for stream %s, because it lacks role.", pa_strnull(pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_NAME))); + return PA_HOOK_OK; + } + + /* Prefer the default source over any other source, just in case... */ + if ((def = pa_namereg_get_default_source(c))) + if (role_match(def->proplist, role)) { + new_data->source = def; + new_data->save_source = FALSE; + return PA_HOOK_OK; + } + + PA_IDXSET_FOREACH(s, c->sources, idx) { + if (s == def) + continue; + + if (role_match(s->proplist, role)) { + new_data->source = s; + new_data->save_source = FALSE; + return PA_HOOK_OK; + } + } + + return PA_HOOK_OK; +} + +static pa_hook_result_t sink_put_hook_callback(pa_core *c, pa_sink *sink, struct userdata *u) { + pa_sink_input *si; + uint32_t idx; + + pa_assert(c); + pa_assert(sink); + pa_assert(u); + pa_assert(u->on_hotplug); + + PA_IDXSET_FOREACH(si, c->sink_inputs, idx) { + const char *role; + + if (si->sink == sink) + continue; + + if (si->save_sink) + continue; + + if (!(role = pa_proplist_gets(si->proplist, PA_PROP_MEDIA_ROLE))) + continue; + + if (role_match(si->sink->proplist, role)) + continue; + + if (!role_match(sink->proplist, role)) + continue; + + pa_sink_input_move_to(si, sink, FALSE); + } + + return PA_HOOK_OK; +} + +static pa_hook_result_t source_put_hook_callback(pa_core *c, pa_source *source, struct userdata *u) { + pa_source_output *so; + uint32_t idx; + + pa_assert(c); + pa_assert(source); + pa_assert(u); + pa_assert(u->on_hotplug); + + PA_IDXSET_FOREACH(so, c->source_outputs, idx) { + const char *role; + + if (so->source == source) + continue; + + if (so->save_source) + continue; + + if (so->direct_on_input) + continue; + + if (!(role = pa_proplist_gets(so->proplist, PA_PROP_MEDIA_ROLE))) + continue; + + if (role_match(so->source->proplist, role)) + continue; + + if (!role_match(source->proplist, role)) + continue; + + pa_source_output_move_to(so, source, FALSE); + } + + return PA_HOOK_OK; +} + +static pa_hook_result_t sink_unlink_hook_callback(pa_core *c, pa_sink *sink, struct userdata *u) { + pa_sink_input *si; + uint32_t idx; + pa_sink *def; + + pa_assert(c); + pa_assert(sink); + pa_assert(u); + pa_assert(u->on_rescue); + + /* There's no point in doing anything if the core is shut down anyway */ + if (c->state == PA_CORE_SHUTDOWN) + return PA_HOOK_OK; + + /* If there not default sink, then there is no sink at all */ + if (!(def = pa_namereg_get_default_sink(c))) + return PA_HOOK_OK; + + PA_IDXSET_FOREACH(si, sink->inputs, idx) { + const char *role; + uint32_t jdx; + pa_sink *d; + + if (!(role = pa_proplist_gets(si->proplist, PA_PROP_MEDIA_ROLE))) + continue; + + /* Would the default sink fit? If so, let's use it */ + if (def != sink && role_match(def->proplist, role)) { + pa_sink_input_move_to(si, def, FALSE); + continue; + } + + /* Try to find some other fitting sink */ + PA_IDXSET_FOREACH(d, c->sinks, jdx) { + if (d == def || d == sink) + continue; + + if (role_match(d->proplist, role)) { + pa_sink_input_move_to(si, d, FALSE); + break; + } + } + } + + return PA_HOOK_OK; +} + +static pa_hook_result_t source_unlink_hook_callback(pa_core *c, pa_source *source, struct userdata *u) { + pa_source_output *so; + uint32_t idx; + pa_source *def; + + pa_assert(c); + pa_assert(source); + pa_assert(u); + pa_assert(u->on_rescue); + + /* There's no point in doing anything if the core is shut down anyway */ + if (c->state == PA_CORE_SHUTDOWN) + return PA_HOOK_OK; + + /* If there not default source, then there is no source at all */ + if (!(def = pa_namereg_get_default_source(c))) + return PA_HOOK_OK; + + PA_IDXSET_FOREACH(so, source->outputs, idx) { + const char *role; + uint32_t jdx; + pa_source *d; + + if (so->direct_on_input) + continue; + + if (!(role = pa_proplist_gets(so->proplist, PA_PROP_MEDIA_ROLE))) + continue; + + /* Would the default source fit? If so, let's use it */ + if (def != source && role_match(def->proplist, role) && !source->monitor_of == !def->monitor_of) { + pa_source_output_move_to(so, def, FALSE); + continue; + } + + /* Try to find some other fitting source */ + PA_IDXSET_FOREACH(d, c->sources, jdx) { + if (d == def || d == source) + continue; + + if (role_match(d->proplist, role) && !source->monitor_of == !d->monitor_of) { + pa_source_output_move_to(so, d, FALSE); + break; + } + } + } + + return PA_HOOK_OK; +} + +int pa__init(pa_module*m) { + pa_modargs *ma = NULL; + struct userdata *u; + pa_bool_t on_hotplug = TRUE, on_rescue = TRUE; + + pa_assert(m); + + if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { + pa_log("Failed to parse module arguments"); + goto fail; + } + + if (pa_modargs_get_value_boolean(ma, "on_hotplug", &on_hotplug) < 0 || + pa_modargs_get_value_boolean(ma, "on_rescue", &on_rescue) < 0) { + pa_log("on_hotplug= and on_rescue= expect boolean arguments"); + goto fail; + } + + m->userdata = u = pa_xnew0(struct userdata, 1); + u->core = m->core; + u->module = m; + u->on_hotplug = on_hotplug; + u->on_rescue = on_rescue; + + /* A little bit later than module-stream-restore */ + u->sink_input_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_NEW], PA_HOOK_EARLY+10, (pa_hook_cb_t) sink_input_new_hook_callback, u); + u->source_output_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_NEW], PA_HOOK_EARLY+10, (pa_hook_cb_t) source_output_new_hook_callback, u); + + if (on_hotplug) { + /* A little bit later than module-stream-restore */ + u->sink_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_LATE+10, (pa_hook_cb_t) sink_put_hook_callback, u); + u->source_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_LATE+10, (pa_hook_cb_t) source_put_hook_callback, u); + } + + if (on_rescue) { + /* A little bit later than module-stream-restore, a little bit earlier than module-rescue-streams, ... */ + u->sink_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE+10, (pa_hook_cb_t) sink_unlink_hook_callback, u); + u->source_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE+10, (pa_hook_cb_t) source_unlink_hook_callback, u); + } + + pa_modargs_free(ma); + return 0; + +fail: + pa__done(m); + + if (ma) + pa_modargs_free(ma); + + return -1; +} + +void pa__done(pa_module*m) { + struct userdata* u; + + pa_assert(m); + + if (!(u = m->userdata)) + return; + + if (u->sink_input_new_hook_slot) + pa_hook_slot_free(u->sink_input_new_hook_slot); + if (u->source_output_new_hook_slot) + pa_hook_slot_free(u->source_output_new_hook_slot); + + if (u->sink_put_hook_slot) + pa_hook_slot_free(u->sink_put_hook_slot); + if (u->source_put_hook_slot) + pa_hook_slot_free(u->source_put_hook_slot); + + if (u->sink_unlink_hook_slot) + pa_hook_slot_free(u->sink_unlink_hook_slot); + if (u->source_unlink_hook_slot) + pa_hook_slot_free(u->source_unlink_hook_slot); + + pa_xfree(u); +} diff --git a/src/modules/module-ladspa-sink.c b/src/modules/module-ladspa-sink.c index 33562b10..21f4a8f1 100644 --- a/src/modules/module-ladspa-sink.c +++ b/src/modules/module-ladspa-sink.c @@ -27,6 +27,7 @@ #endif #include <pulse/xmalloc.h> +#include <pulse/i18n.h> #include <pulsecore/core-error.h> #include <pulsecore/namereg.h> @@ -45,19 +46,20 @@ #include "ladspa.h" PA_MODULE_AUTHOR("Lennart Poettering"); -PA_MODULE_DESCRIPTION("Virtual LADSPA sink"); +PA_MODULE_DESCRIPTION(_("Virtual LADSPA sink")); PA_MODULE_VERSION(PACKAGE_VERSION); PA_MODULE_LOAD_ONCE(FALSE); PA_MODULE_USAGE( - "sink_name=<name for the sink> " - "master=<name of sink to remap> " - "format=<sample format> " - "channels=<number of channels> " - "rate=<sample rate> " - "channel_map=<channel map> " - "plugin=<ladspa plugin name> " - "label=<ladspa plugin label> " - "control=<comma seperated list of input control values>"); + _("sink_name=<name for the sink> " + "sink_properties=<properties for the sink> " + "master=<name of sink to filter> " + "format=<sample format> " + "rate=<sample rate> " + "channels=<number of channels> " + "channel_map=<channel map> " + "plugin=<ladspa plugin name> " + "label=<ladspa plugin label> " + "control=<comma seperated list of input control values>")); #define MEMBLOCKQ_MAXLENGTH (16*1024*1024) @@ -85,10 +87,11 @@ struct userdata { static const char* const valid_modargs[] = { "sink_name", + "sink_properties", "master", "format", - "channels", "rate", + "channels", "channel_map", "plugin", "label", @@ -235,7 +238,7 @@ static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) { if (amount > 0) { unsigned c; - pa_memblockq_seek(u->memblockq, - (int64_t) amount, PA_SEEK_RELATIVE); + pa_memblockq_seek(u->memblockq, - (int64_t) amount, PA_SEEK_RELATIVE, TRUE); pa_log_debug("Resetting plugin"); @@ -264,7 +267,7 @@ static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) { return; pa_memblockq_set_maxrewind(u->memblockq, nbytes); - pa_sink_set_max_rewind(u->sink, nbytes); + pa_sink_set_max_rewind_within_thread(u->sink, nbytes); } /* Called from I/O thread context */ @@ -277,7 +280,7 @@ static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) { if (!u->sink || !PA_SINK_IS_LINKED(u->sink->thread_info.state)) return; - pa_sink_set_max_request(u->sink, nbytes); + pa_sink_set_max_request_within_thread(u->sink, nbytes); } /* Called from I/O thread context */ @@ -290,7 +293,7 @@ static void sink_input_update_sink_latency_range_cb(pa_sink_input *i) { if (!u->sink || !PA_SINK_IS_LINKED(u->sink->thread_info.state)) return; - pa_sink_update_latency_range(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency); + pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency); } /* Called from I/O thread context */ @@ -322,7 +325,7 @@ static void sink_input_attach_cb(pa_sink_input *i) { pa_sink_set_rtpoll(u->sink, i->sink->rtpoll); pa_sink_attach_within_thread(u->sink); - pa_sink_update_latency_range(u->sink, u->master->thread_info.min_latency, u->master->thread_info.max_latency); + pa_sink_set_latency_range_within_thread(u->sink, u->master->thread_info.min_latency, u->master->thread_info.max_latency); } /* Called from main context */ @@ -705,7 +708,13 @@ int pa__init(pa_module*m) { pa_proplist_sets(sink_data.proplist, "device.ladspa.copyright", d->Copyright); pa_proplist_setf(sink_data.proplist, "device.ladspa.unique_id", "%lu", (unsigned long) d->UniqueID); - u->sink = pa_sink_new(m->core, &sink_data, PA_SINK_LATENCY); + if (pa_modargs_get_proplist(ma, "sink_properties", sink_data.proplist, PA_UPDATE_REPLACE) < 0) { + pa_log("Invalid properties"); + pa_sink_new_data_done(&sink_data); + goto fail; + } + + u->sink = pa_sink_new(m->core, &sink_data, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY); pa_sink_new_data_done(&sink_data); if (!u->sink) { diff --git a/src/modules/module-lirc.c b/src/modules/module-lirc.c index bdb8bb71..06efeb8f 100644 --- a/src/modules/module-lirc.c +++ b/src/modules/module-lirc.c @@ -63,8 +63,6 @@ struct userdata { float mute_toggle_save; }; -static int lirc_in_use = 0; - static void io_callback(pa_mainloop_api *io, pa_io_event *e, int fd, pa_io_event_flags_t events, void*userdata) { struct userdata *u = userdata; char *name = NULL, *code = NULL; @@ -114,7 +112,7 @@ static void io_callback(pa_mainloop_api *io, pa_io_event *e, int fd, pa_io_event volchange = RESET; if (volchange == INVALID) - pa_log_warn("Recieved unknown IR code '%s'", name); + pa_log_warn("Received unknown IR code '%s'", name); else { pa_sink *s; @@ -122,48 +120,48 @@ static void io_callback(pa_mainloop_api *io, pa_io_event *e, int fd, pa_io_event pa_log("Failed to get sink '%s'", u->sink_name); else { int i; - pa_cvolume cv = *pa_sink_get_volume(s, FALSE); + pa_cvolume cv = *pa_sink_get_volume(s, FALSE, FALSE); #define DELTA (PA_VOLUME_NORM/20) switch (volchange) { case UP: for (i = 0; i < cv.channels; i++) { - cv.values[i] += DELTA; - - if (cv.values[i] > PA_VOLUME_NORM) - cv.values[i] = PA_VOLUME_NORM; + if (cv.values[i] < PA_VOLUME_MAX - DELTA) + cv.values[i] += DELTA; + else + cv.values[i] = PA_VOLUME_MAX; } - pa_sink_set_volume(s, &cv, TRUE, TRUE); + pa_sink_set_volume(s, &cv, TRUE, TRUE, TRUE, TRUE); break; case DOWN: for (i = 0; i < cv.channels; i++) { - if (cv.values[i] >= DELTA) + if (cv.values[i] > DELTA) cv.values[i] -= DELTA; else cv.values[i] = PA_VOLUME_MUTED; } - pa_sink_set_volume(s, &cv, TRUE, TRUE); + pa_sink_set_volume(s, &cv, TRUE, TRUE, TRUE, TRUE); break; case MUTE: - pa_sink_set_mute(s, 0); + pa_sink_set_mute(s, TRUE, TRUE); break; case RESET: - pa_sink_set_mute(s, 1); + pa_sink_set_mute(s, FALSE, TRUE); break; case MUTE_TOGGLE: - pa_sink_set_mute(s, !pa_sink_get_mute(s, FALSE)); + pa_sink_set_mute(s, !pa_sink_get_mute(s, FALSE), TRUE); break; case INVALID: - ; + pa_assert_not_reached(); } } } @@ -189,11 +187,6 @@ int pa__init(pa_module*m) { pa_assert(m); - if (lirc_in_use) { - pa_log("module-lirc may no be loaded twice."); - return -1; - } - if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { pa_log("Failed to parse module arguments"); goto fail; @@ -219,8 +212,6 @@ int pa__init(pa_module*m) { u->io = m->core->mainloop->io_new(m->core->mainloop, u->lirc_fd, PA_IO_EVENT_INPUT|PA_IO_EVENT_HANGUP, io_callback, u); - lirc_in_use = 1; - pa_modargs_free(ma); return 0; @@ -252,6 +243,4 @@ void pa__done(pa_module*m) { pa_xfree(u->sink_name); pa_xfree(u); - - lirc_in_use = 0; } diff --git a/src/modules/module-match.c b/src/modules/module-match.c index d7365ca7..625f2a8b 100644 --- a/src/modules/module-match.c +++ b/src/modules/module-match.c @@ -216,7 +216,7 @@ static void callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, v pa_cvolume cv; pa_log_debug("changing volume of sink input '%s' to 0x%03x", n, r->volume); pa_cvolume_set(&cv, si->sample_spec.channels, r->volume); - pa_sink_input_set_volume(si, &cv, TRUE); + pa_sink_input_set_volume(si, &cv, TRUE, TRUE); } } } diff --git a/src/modules/module-mmkbd-evdev.c b/src/modules/module-mmkbd-evdev.c index 2f87dd22..b30fae51 100644 --- a/src/modules/module-mmkbd-evdev.c +++ b/src/modules/module-mmkbd-evdev.c @@ -52,17 +52,6 @@ PA_MODULE_USAGE("device=<evdev device> sink=<sink name>"); #define DEFAULT_DEVICE "/dev/input/event0" -/* - * This isn't defined in older kernel headers and there is no way of - * detecting it. - */ -struct _input_id { - __u16 bustype; - __u16 vendor; - __u16 product; - __u16 version; -}; - static const char* const valid_modargs[] = { "device", "sink", @@ -113,40 +102,40 @@ static void io_callback(pa_mainloop_api *io, pa_io_event *e, int fd, pa_io_event pa_log("Failed to get sink '%s'", u->sink_name); else { int i; - pa_cvolume cv = *pa_sink_get_volume(s, FALSE); + pa_cvolume cv = *pa_sink_get_volume(s, FALSE, FALSE); #define DELTA (PA_VOLUME_NORM/20) switch (volchange) { case UP: for (i = 0; i < cv.channels; i++) { - cv.values[i] += DELTA; - - if (cv.values[i] > PA_VOLUME_NORM) - cv.values[i] = PA_VOLUME_NORM; + if (cv.values[i] < PA_VOLUME_MAX - DELTA) + cv.values[i] += DELTA; + else + cv.values[i] = PA_VOLUME_MAX; } - pa_sink_set_volume(s, &cv, TRUE, TRUE); + pa_sink_set_volume(s, &cv, TRUE, TRUE, TRUE, TRUE); break; case DOWN: for (i = 0; i < cv.channels; i++) { - if (cv.values[i] >= DELTA) + if (cv.values[i] > DELTA) cv.values[i] -= DELTA; else cv.values[i] = PA_VOLUME_MUTED; } - pa_sink_set_volume(s, &cv, TRUE, TRUE); + pa_sink_set_volume(s, &cv, TRUE, TRUE, TRUE, TRUE); break; case MUTE_TOGGLE: - pa_sink_set_mute(s, !pa_sink_get_mute(s, FALSE)); + pa_sink_set_mute(s, !pa_sink_get_mute(s, FALSE), TRUE); break; case INVALID: - ; + pa_assert_not_reached(); } } } @@ -169,7 +158,7 @@ int pa__init(pa_module*m) { pa_modargs *ma = NULL; struct userdata *u; int version; - struct _input_id input_id; + struct input_id input_id; char name[256]; uint8_t evtype_bitmask[EV_MAX/8 + 1]; @@ -180,15 +169,15 @@ int pa__init(pa_module*m) { goto fail; } - m->userdata = u = pa_xnew(struct userdata,1); + m->userdata = u = pa_xnew(struct userdata, 1); u->module = m; u->io = NULL; u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL)); u->fd = -1; u->fd_type = 0; - if ((u->fd = open(pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), O_RDONLY)) < 0) { - pa_log("failed to open evdev device: %s", pa_cstrerror(errno)); + if ((u->fd = open(pa_modargs_get_value(ma, "device", DEFAULT_DEVICE), O_RDONLY|O_NOCTTY)) < 0) { + pa_log("Failed to open evdev device: %s", pa_cstrerror(errno)); goto fail; } @@ -208,7 +197,7 @@ int pa__init(pa_module*m) { input_id.vendor, input_id.product, input_id.version, input_id.bustype); memset(name, 0, sizeof(name)); - if(ioctl(u->fd, EVIOCGNAME(sizeof(name)), name) < 0) { + if (ioctl(u->fd, EVIOCGNAME(sizeof(name)), name) < 0) { pa_log("EVIOCGNAME failed: %s", pa_cstrerror(errno)); goto fail; } diff --git a/src/modules/module-null-sink.c b/src/modules/module-null-sink.c index b5952d69..228a9716 100644 --- a/src/modules/module-null-sink.c +++ b/src/modules/module-null-sink.c @@ -32,12 +32,14 @@ #include <unistd.h> #include <limits.h> +#include <pulse/rtclock.h> #include <pulse/timeval.h> #include <pulse/xmalloc.h> #include <pulsecore/macro.h> #include <pulsecore/sink.h> #include <pulsecore/module.h> +#include <pulsecore/core-rtclock.h> #include <pulsecore/core-util.h> #include <pulsecore/core-error.h> #include <pulsecore/modargs.h> @@ -45,7 +47,6 @@ #include <pulsecore/thread.h> #include <pulsecore/thread-mq.h> #include <pulsecore/rtpoll.h> -#include <pulsecore/rtclock.h> #include "module-null-sink-symdef.h" @@ -54,15 +55,15 @@ PA_MODULE_DESCRIPTION("Clocked NULL sink"); PA_MODULE_VERSION(PACKAGE_VERSION); PA_MODULE_LOAD_ONCE(FALSE); PA_MODULE_USAGE( + "sink_name=<name of sink> " + "sink_properties=<properties for the sink> " "format=<sample format> " - "channels=<number of channels> " "rate=<sample rate> " - "sink_name=<name of sink> " - "channel_map=<channel map> " - "description=<description for the sink>"); + "channels=<number of channels> " + "channel_map=<channel map>"); #define DEFAULT_SINK_NAME "null" -#define MAX_LATENCY_USEC (PA_USEC_PER_SEC * 2) +#define BLOCK_USEC (PA_USEC_PER_SEC * 2) struct userdata { pa_core *core; @@ -78,12 +79,13 @@ struct userdata { }; static const char* const valid_modargs[] = { - "rate", + "sink_name", + "sink_properties", "format", + "rate", "channels", - "sink_name", "channel_map", - "description", + "description", /* supported for compatibility reasons, made redundant by sink_properties= */ NULL }; @@ -100,14 +102,14 @@ static int sink_process_msg( case PA_SINK_MESSAGE_SET_STATE: if (PA_PTR_TO_UINT(data) == PA_SINK_RUNNING) - u->timestamp = pa_rtclock_usec(); + u->timestamp = pa_rtclock_now(); break; case PA_SINK_MESSAGE_GET_LATENCY: { pa_usec_t now; - now = pa_rtclock_usec(); + now = pa_rtclock_now(); *((pa_usec_t*) data) = u->timestamp > now ? u->timestamp - now : 0ULL; return 0; @@ -119,6 +121,7 @@ static int sink_process_msg( static void sink_update_requested_latency_cb(pa_sink *s) { struct userdata *u; + size_t nbytes; pa_sink_assert_ref(s); pa_assert_se(u = s->userdata); @@ -127,6 +130,10 @@ static void sink_update_requested_latency_cb(pa_sink *s) { if (u->block_usec == (pa_usec_t) -1) u->block_usec = s->thread_info.max_latency; + + nbytes = pa_usec_to_bytes(u->block_usec, &s->sample_spec); + pa_sink_set_max_rewind_within_thread(s, nbytes); + pa_sink_set_max_request_within_thread(s, nbytes); } static void process_rewind(struct userdata *u, pa_usec_t now) { @@ -202,9 +209,8 @@ static void thread_func(void *userdata) { pa_log_debug("Thread starting up"); pa_thread_mq_install(&u->thread_mq); - pa_rtpoll_install(u->rtpoll); - u->timestamp = pa_rtclock_usec(); + u->timestamp = pa_rtclock_now(); for (;;) { int ret; @@ -213,7 +219,7 @@ static void thread_func(void *userdata) { if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) { pa_usec_t now; - now = pa_rtclock_usec(); + now = pa_rtclock_now(); if (u->sink->thread_info.rewind_requested) { if (u->sink->thread_info.rewind_nbytes > 0) @@ -253,6 +259,7 @@ int pa__init(pa_module*m) { pa_channel_map map; pa_modargs *ma = NULL; pa_sink_new_data data; + size_t nbytes; pa_assert(m); @@ -283,7 +290,13 @@ int pa__init(pa_module*m) { pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, pa_modargs_get_value(ma, "description", "Null Output")); pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract"); - u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY); + if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) { + pa_log("Invalid properties"); + pa_sink_new_data_done(&data); + goto fail; + } + + u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY); pa_sink_new_data_done(&data); if (!u->sink) { @@ -298,12 +311,10 @@ int pa__init(pa_module*m) { pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq); pa_sink_set_rtpoll(u->sink, u->rtpoll); - pa_sink_set_latency_range(u->sink, (pa_usec_t) -1, MAX_LATENCY_USEC); - u->block_usec = u->sink->thread_info.max_latency; - - u->sink->thread_info.max_rewind = - u->sink->thread_info.max_request = - pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec); + u->block_usec = BLOCK_USEC; + nbytes = pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec); + pa_sink_set_max_rewind(u->sink, nbytes); + pa_sink_set_max_request(u->sink, nbytes); if (!(u->thread = pa_thread_new(thread_func, u))) { pa_log("Failed to create thread."); diff --git a/src/modules/module-pipe-sink.c b/src/modules/module-pipe-sink.c index def4f758..8a7dc846 100644 --- a/src/modules/module-pipe-sink.c +++ b/src/modules/module-pipe-sink.c @@ -54,10 +54,11 @@ PA_MODULE_VERSION(PACKAGE_VERSION); PA_MODULE_LOAD_ONCE(FALSE); PA_MODULE_USAGE( "sink_name=<name for the sink> " + "sink_properties=<properties for the sink> " "file=<path of the FIFO> " "format=<sample format> " - "channels=<number of channels> " "rate=<sample rate>" + "channels=<number of channels> " "channel_map=<channel map>"); #define DEFAULT_FILE_NAME "fifo_output" @@ -83,11 +84,12 @@ struct userdata { }; static const char* const valid_modargs[] = { + "sink_name", + "sink_properties", "file", - "rate", "format", + "rate", "channels", - "sink_name", "channel_map", NULL }; @@ -168,7 +170,6 @@ static void thread_func(void *userdata) { pa_log_debug("Thread starting up"); pa_thread_mq_install(&u->thread_mq); - pa_rtpoll_install(u->rtpoll); for (;;) { struct pollfd *pollfd; @@ -279,6 +280,12 @@ int pa__init(pa_module*m) { pa_sink_new_data_set_sample_spec(&data, &ss); pa_sink_new_data_set_channel_map(&data, &map); + if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) { + pa_log("Invalid properties"); + pa_sink_new_data_done(&data); + goto fail; + } + u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY); pa_sink_new_data_done(&data); @@ -292,6 +299,8 @@ int pa__init(pa_module*m) { pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq); pa_sink_set_rtpoll(u->sink, u->rtpoll); + pa_sink_set_max_request(u->sink, PIPE_BUF); + pa_sink_set_fixed_latency(u->sink, pa_bytes_to_usec(PIPE_BUF, &u->sink->sample_spec)); u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1); pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL); diff --git a/src/modules/module-pipe-source.c b/src/modules/module-pipe-source.c index 3d40fdf3..e5609fb5 100644 --- a/src/modules/module-pipe-source.c +++ b/src/modules/module-pipe-source.c @@ -54,10 +54,11 @@ PA_MODULE_VERSION(PACKAGE_VERSION); PA_MODULE_LOAD_ONCE(FALSE); PA_MODULE_USAGE( "source_name=<name for the source> " + "source_properties=<properties for the source> " "file=<path of the FIFO> " "format=<sample format> " - "channels=<number of channels> " "rate=<sample rate> " + "channels=<number of channels> " "channel_map=<channel map>"); #define DEFAULT_FILE_NAME "/tmp/music.input" @@ -81,11 +82,12 @@ struct userdata { }; static const char* const valid_modargs[] = { + "source_name", + "source_properties", "file", + "format", "rate", "channels", - "format", - "source_name", "channel_map", NULL }; @@ -127,7 +129,6 @@ static void thread_func(void *userdata) { pa_log_debug("Thread starting up"); pa_thread_mq_install(&u->thread_mq); - pa_rtpoll_install(u->rtpoll); for (;;) { int ret; @@ -264,6 +265,12 @@ int pa__init(pa_module*m) { pa_source_new_data_set_sample_spec(&data, &ss); pa_source_new_data_set_channel_map(&data, &map); + if (pa_modargs_get_proplist(ma, "source_properties", data.proplist, PA_UPDATE_REPLACE) < 0) { + pa_log("Invalid properties"); + pa_source_new_data_done(&data); + goto fail; + } + u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY); pa_source_new_data_done(&data); @@ -277,6 +284,7 @@ int pa__init(pa_module*m) { pa_source_set_asyncmsgq(u->source, u->thread_mq.inq); pa_source_set_rtpoll(u->source, u->rtpoll); + pa_source_set_fixed_latency(u->source, pa_bytes_to_usec(PIPE_BUF, &u->source->sample_spec)); u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1); pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL); diff --git a/src/modules/module-protocol-stub.c b/src/modules/module-protocol-stub.c index ce3dcd03..5b351d19 100644 --- a/src/modules/module-protocol-stub.c +++ b/src/modules/module-protocol-stub.c @@ -251,7 +251,7 @@ int pa__init(pa_module*m) { int r; #endif -#if defined(USE_PROTOCOL_NATIVE) +#if defined(USE_PROTOCOL_NATIVE) || defined(USE_PROTOCOL_HTTP) char t[256]; #endif @@ -382,6 +382,24 @@ int pa__init(pa_module*m) { # endif #endif +#if defined(USE_PROTOCOL_HTTP) +#if defined(USE_TCP_SOCKETS) + if (u->socket_server_ipv4) + if (pa_socket_server_get_address(u->socket_server_ipv4, t, sizeof(t))) + pa_http_protocol_add_server_string(u->http_protocol, t); + +#ifdef HAVE_IPV6 + if (u->socket_server_ipv6) + if (pa_socket_server_get_address(u->socket_server_ipv6, t, sizeof(t))) + pa_http_protocol_add_server_string(u->http_protocol, t); +#endif /* HAVE_IPV6 */ +#else /* USE_TCP_SOCKETS */ + if (pa_socket_server_get_address(u->socket_server_unix, t, sizeof(t))) + pa_http_protocol_add_server_string(u->http_protocol, t); + +#endif /* USE_TCP_SOCKETS */ +#endif /* USE_PROTOCOL_HTTP */ + if (ma) pa_modargs_free(ma); @@ -419,6 +437,24 @@ void pa__done(pa_module*m) { } #elif defined(USE_PROTOCOL_HTTP) if (u->http_protocol) { + char t[256]; + +#if defined(USE_TCP_SOCKETS) + if (u->socket_server_ipv4) + if (pa_socket_server_get_address(u->socket_server_ipv4, t, sizeof(t))) + pa_http_protocol_remove_server_string(u->http_protocol, t); + +#ifdef HAVE_IPV6 + if (u->socket_server_ipv6) + if (pa_socket_server_get_address(u->socket_server_ipv6, t, sizeof(t))) + pa_http_protocol_remove_server_string(u->http_protocol, t); +#endif /* HAVE_IPV6 */ +#else /* USE_TCP_SOCKETS */ + if (u->socket_server_unix) + if (pa_socket_server_get_address(u->socket_server_unix, t, sizeof(t))) + pa_http_protocol_remove_server_string(u->http_protocol, t); +#endif /* USE_PROTOCOL_HTTP */ + pa_http_protocol_disconnect(u->http_protocol, u->module); pa_http_protocol_unref(u->http_protocol); } diff --git a/src/modules/module-remap-sink.c b/src/modules/module-remap-sink.c index 31824bc5..119f5b9f 100644 --- a/src/modules/module-remap-sink.c +++ b/src/modules/module-remap-sink.c @@ -44,6 +44,7 @@ PA_MODULE_VERSION(PACKAGE_VERSION); PA_MODULE_LOAD_ONCE(FALSE); PA_MODULE_USAGE( "sink_name=<name for the sink> " + "sink_properties=<properties for the sink> " "master=<name of sink to remap> " "master_channel_map=<channel map> " "format=<sample format> " @@ -62,10 +63,11 @@ struct userdata { static const char* const valid_modargs[] = { "sink_name", + "sink_properties", "master", "master_channel_map", - "rate", "format", + "rate", "channels", "channel_map", "remix", @@ -179,7 +181,7 @@ static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) { if (!u->sink || !PA_SINK_IS_LINKED(u->sink->thread_info.state)) return; - pa_sink_set_max_rewind(u->sink, nbytes); + pa_sink_set_max_rewind_within_thread(u->sink, nbytes); } /* Called from I/O thread context */ @@ -192,7 +194,7 @@ static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) { if (!u->sink || !PA_SINK_IS_LINKED(u->sink->thread_info.state)) return; - pa_sink_set_max_request(u->sink, nbytes); + pa_sink_set_max_request_within_thread(u->sink, nbytes); } /* Called from I/O thread context */ @@ -205,7 +207,7 @@ static void sink_input_update_sink_latency_range_cb(pa_sink_input *i) { if (!u->sink || !PA_SINK_IS_LINKED(u->sink->thread_info.state)) return; - pa_sink_update_latency_range(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency); + pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency); } /* Called from I/O thread context */ @@ -237,7 +239,7 @@ static void sink_input_attach_cb(pa_sink_input *i) { pa_sink_set_rtpoll(u->sink, i->sink->rtpoll); pa_sink_attach_within_thread(u->sink); - pa_sink_update_latency_range(u->sink, u->master->thread_info.min_latency, u->master->thread_info.max_latency); + pa_sink_set_latency_range_within_thread(u->sink, u->master->thread_info.min_latency, u->master->thread_info.max_latency); } /* Called from main context */ @@ -354,7 +356,13 @@ int pa__init(pa_module*m) { pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name); pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_CLASS, "filter"); - u->sink = pa_sink_new(m->core, &sink_data, PA_SINK_LATENCY); + if (pa_modargs_get_proplist(ma, "sink_properties", sink_data.proplist, PA_UPDATE_REPLACE) < 0) { + pa_log("Invalid properties"); + pa_sink_new_data_done(&sink_data); + goto fail; + } + + u->sink = pa_sink_new(m->core, &sink_data, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY); pa_sink_new_data_done(&sink_data); if (!u->sink) { diff --git a/src/modules/module-rescue-streams.c b/src/modules/module-rescue-streams.c index 4f616e05..c23feceb 100644 --- a/src/modules/module-rescue-streams.c +++ b/src/modules/module-rescue-streams.c @@ -31,6 +31,7 @@ #include <pulsecore/modargs.h> #include <pulsecore/log.h> #include <pulsecore/namereg.h> +#include <pulsecore/core-util.h> #include "module-rescue-streams-symdef.h" @@ -49,6 +50,7 @@ struct userdata { static pa_hook_result_t sink_hook_callback(pa_core *c, pa_sink *sink, void* userdata) { pa_sink_input *i; + uint32_t idx; pa_sink *target; pa_assert(c); @@ -58,40 +60,41 @@ static pa_hook_result_t sink_hook_callback(pa_core *c, pa_sink *sink, void* user if (c->state == PA_CORE_SHUTDOWN) return PA_HOOK_OK; - if (!pa_idxset_size(sink->inputs)) { + if (pa_idxset_size(sink->inputs) <= 0) { pa_log_debug("No sink inputs to move away."); return PA_HOOK_OK; } - if (!(target = pa_namereg_get(c, NULL, PA_NAMEREG_SINK)) || target == sink) { - uint32_t idx; + if (!(target = pa_namereg_get_default_sink(c)) || target == sink) { - for (target = pa_idxset_first(c->sinks, &idx); target; target = pa_idxset_next(c->sinks, &idx)) + PA_IDXSET_FOREACH(target, c->sinks, idx) if (target != sink) break; if (!target) { - pa_log_info("No evacuation sink found."); + pa_log_debug("No evacuation sink found."); return PA_HOOK_OK; } } - while ((i = pa_idxset_first(sink->inputs, NULL))) { - if (pa_sink_input_move_to(i, target, FALSE) < 0) { - pa_log_warn("Failed to move sink input %u \"%s\" to %s.", i->index, pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME), target->name); - return PA_HOOK_OK; - } + pa_assert(target != sink); - pa_log_info("Sucessfully moved sink input %u \"%s\" to %s.", i->index, pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME), target->name); + PA_IDXSET_FOREACH(i, sink->inputs, idx) { + if (pa_sink_input_move_to(i, target, FALSE) < 0) + pa_log_info("Failed to move sink input %u \"%s\" to %s.", i->index, + pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name); + else + pa_log_info("Sucessfully moved sink input %u \"%s\" to %s.", i->index, + pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name); } - return PA_HOOK_OK; } static pa_hook_result_t source_hook_callback(pa_core *c, pa_source *source, void* userdata) { pa_source_output *o; pa_source *target; + uint32_t idx; pa_assert(c); pa_assert(source); @@ -100,15 +103,14 @@ static pa_hook_result_t source_hook_callback(pa_core *c, pa_source *source, void if (c->state == PA_CORE_SHUTDOWN) return PA_HOOK_OK; - if (!pa_idxset_size(source->outputs)) { + if (pa_idxset_size(source->outputs) <= 0) { pa_log_debug("No source outputs to move away."); return PA_HOOK_OK; } - if (!(target = pa_namereg_get(c, NULL, PA_NAMEREG_SOURCE)) || target == source) { - uint32_t idx; + if (!(target = pa_namereg_get_default_source(c)) || target == source) { - for (target = pa_idxset_first(c->sources, &idx); target; target = pa_idxset_next(c->sources, &idx)) + PA_IDXSET_FOREACH(target, c->sources, idx) if (target != source && !target->monitor_of == !source->monitor_of) break; @@ -120,21 +122,20 @@ static pa_hook_result_t source_hook_callback(pa_core *c, pa_source *source, void pa_assert(target != source); - while ((o = pa_idxset_first(source->outputs, NULL))) { - if (pa_source_output_move_to(o, target, FALSE) < 0) { - pa_log_warn("Failed to move source output %u \"%s\" to %s.", o->index, pa_proplist_gets(o->proplist, PA_PROP_APPLICATION_NAME), target->name); - return PA_HOOK_OK; - } - - pa_log_info("Sucessfully moved source output %u \"%s\" to %s.", o->index, pa_proplist_gets(o->proplist, PA_PROP_APPLICATION_NAME), target->name); + PA_IDXSET_FOREACH(o, source->outputs, idx) { + if (pa_source_output_move_to(o, target, FALSE) < 0) + pa_log_info("Failed to move source output %u \"%s\" to %s.", o->index, + pa_strnull(pa_proplist_gets(o->proplist, PA_PROP_APPLICATION_NAME)), target->name); + else + pa_log_info("Sucessfully moved source output %u \"%s\" to %s.", o->index, + pa_strnull(pa_proplist_gets(o->proplist, PA_PROP_APPLICATION_NAME)), target->name); } - return PA_HOOK_OK; } int pa__init(pa_module*m) { - pa_modargs *ma = NULL; + pa_modargs *ma; struct userdata *u; pa_assert(m); @@ -145,8 +146,10 @@ int pa__init(pa_module*m) { } m->userdata = u = pa_xnew(struct userdata, 1); - u->sink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) sink_hook_callback, NULL); - u->source_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) source_hook_callback, NULL); + + /* A little bit later than module-stream-restore, module-intended-roles... */ + u->sink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE+20, (pa_hook_cb_t) sink_hook_callback, u); + u->source_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE+20, (pa_hook_cb_t) source_hook_callback, u); pa_modargs_free(ma); return 0; @@ -157,10 +160,9 @@ void pa__done(pa_module*m) { pa_assert(m); - if (!m->userdata) + if (!(u = m->userdata)) return; - u = m->userdata; if (u->sink_slot) pa_hook_slot_free(u->sink_slot); if (u->source_slot) diff --git a/src/modules/module-rygel-media-server.c b/src/modules/module-rygel-media-server.c new file mode 100644 index 00000000..4c02e958 --- /dev/null +++ b/src/modules/module-rygel-media-server.c @@ -0,0 +1,847 @@ +/*** + This file is part of PulseAudio. + + Copyright 2005-2009 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.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 + along with PulseAudio; if not, write to the Free Software + 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 <string.h> +#include <unistd.h> + +#include <pulse/xmalloc.h> +#include <pulse/util.h> +#include <pulse/i18n.h> +#include <pulse/utf8.h> + +#include <pulsecore/sink.h> +#include <pulsecore/source.h> +#include <pulsecore/core-util.h> +#include <pulsecore/log.h> +#include <pulsecore/modargs.h> +#include <pulsecore/dbus-shared.h> +#include <pulsecore/endianmacros.h> +#include <pulsecore/namereg.h> +#include <pulsecore/mime-type.h> +#include <pulsecore/strbuf.h> +#include <pulsecore/protocol-http.h> +#include <pulsecore/parseaddr.h> + +#include "module-rygel-media-server-symdef.h" + +PA_MODULE_AUTHOR("Lennart Poettering"); +PA_MODULE_DESCRIPTION("UPnP MediaServer Plugin for Rygel"); +PA_MODULE_VERSION(PACKAGE_VERSION); +PA_MODULE_LOAD_ONCE(TRUE); +PA_MODULE_USAGE( + "display_name=<UPnP Media Server name>"); + +/* This implements http://live.gnome.org/Rygel/MediaServerSpec */ + +#define SERVICE_NAME "org.gnome.UPnP.MediaServer1.PulseAudio" + +#define OBJECT_ROOT "/org/gnome/UPnP/MediaServer1/PulseAudio" +#define OBJECT_SINKS "/org/gnome/UPnP/MediaServer1/PulseAudio/Sinks" +#define OBJECT_SOURCES "/org/gnome/UPnP/MediaServer1/PulseAudio/Sources" + +#define CONTAINER_INTROSPECT_XML_PREFIX \ + DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \ + "<node>" \ + " <!-- If you are looking for documentation make sure to check out" \ + " http://live.gnome.org/Rygel/MediaServerSpec -->" \ + " <interface name=\"org.gnome.UPnP.MediaContainer1\">" \ + " <signal name=\"Updated\">" \ + " <arg name=\"path\" type=\"o\"/>" \ + " </signal>" \ + " <property name=\"Items\" type=\"ao\" access=\"read\"/>" \ + " <property name=\"ItemCount\" type=\"u\" access=\"read\"/>" \ + " <property name=\"Containers\" type=\"ao\" access=\"read\"/>" \ + " <property name=\"ContainerCount\" type=\"u\" access=\"read\"/>" \ + " </interface>" \ + " <interface name=\"org.gnome.UPnP.MediaObject1\">" \ + " <property name=\"Parent\" type=\"s\" access=\"read\"/>" \ + " <property name=\"DisplayName\" type=\"s\" access=\"read\"/>" \ + " </interface>" \ + " <interface name=\"org.freedesktop.DBus.Properties\">" \ + " <method name=\"Get\">" \ + " <arg name=\"interface\" direction=\"in\" type=\"s\"/>" \ + " <arg name=\"property\" direction=\"in\" type=\"s\"/>" \ + " <arg name=\"value\" direction=\"out\" type=\"v\"/>" \ + " </method>" \ + " <method name=\"GetAll\">" \ + " <arg name=\"interface\" direction=\"in\" type=\"s\"/>" \ + " <arg name=\"properties\" direction=\"out\" type=\"a{sv}\"/>" \ + " </method>" \ + " </interface>" \ + " <interface name=\"org.freedesktop.DBus.Introspectable\">" \ + " <method name=\"Introspect\">" \ + " <arg name=\"data\" type=\"s\" direction=\"out\"/>" \ + " </method>" \ + " </interface>" + +#define CONTAINER_INTROSPECT_XML_POSTFIX \ + "</node>" + +#define ROOT_INTROSPECT_XML \ + CONTAINER_INTROSPECT_XML_PREFIX \ + "<node name=\"Sinks\"/>" \ + "<node name=\"Sources\"/>" \ + CONTAINER_INTROSPECT_XML_POSTFIX + +#define ITEM_INTROSPECT_XML \ + DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \ + "<node>" \ + " <!-- If you are looking for documentation make sure to check out" \ + " http://live.gnome.org/Rygel/MediaProviderSpec -->" \ + " <interface name=\"org.gnome.UPnP.MediaItem1\">" \ + " <property name=\"URLs\" type=\"as\" access=\"read\"/>" \ + " <property name=\"MIMEType\" type=\"s\" access=\"read\"/>" \ + " <property name=\"Type\" type=\"s\" access=\"read\"/>" \ + " </interface>" \ + " <interface name=\"org.gnome.UPnP.MediaObject1\">" \ + " <property name=\"Parent\" type=\"s\" access=\"read\"/>" \ + " <property name=\"DisplayName\" type=\"s\" access=\"read\"/>" \ + " </interface>" \ + " <interface name=\"org.freedesktop.DBus.Properties\">" \ + " <method name=\"Get\">" \ + " <arg name=\"interface\" direction=\"in\" type=\"s\"/>" \ + " <arg name=\"property\" direction=\"in\" type=\"s\"/>" \ + " <arg name=\"value\" direction=\"out\" type=\"v\"/>" \ + " </method>" \ + " <method name=\"GetAll\">" \ + " <arg name=\"interface\" direction=\"in\" type=\"s\"/>" \ + " <arg name=\"properties\" direction=\"out\" type=\"a{sv}\"/>" \ + " </method>" \ + " </interface>" \ + " <interface name=\"org.freedesktop.DBus.Introspectable\">" \ + " <method name=\"Introspect\">" \ + " <arg name=\"data\" type=\"s\" direction=\"out\"/>" \ + " </method>" \ + " </interface>" \ + "</node>" + + +static const char* const valid_modargs[] = { + "display_name", + NULL +}; + +struct userdata { + pa_core *core; + pa_module *module; + + pa_dbus_connection *bus; + pa_bool_t got_name:1; + + char *display_name; + + pa_hook_slot *source_new_slot, *source_unlink_slot; + + pa_http_protocol *http; +}; + +static void send_signal(struct userdata *u, pa_source *s) { + DBusMessage *m; + const char *parent; + + pa_assert(u); + pa_source_assert_ref(s); + + if (u->core->state == PA_CORE_SHUTDOWN) + return; + + if (s->monitor_of) + parent = OBJECT_SINKS; + else + parent = OBJECT_SOURCES; + + pa_assert_se(m = dbus_message_new_signal(parent, "org.gnome.UPnP.MediaContainer1", "Updated")); + pa_assert_se(dbus_connection_send(pa_dbus_connection_get(u->bus), m, NULL)); + + dbus_message_unref(m); +} + +static pa_hook_result_t source_new_or_unlink_cb(pa_core *c, pa_source *s, struct userdata *u) { + pa_assert(c); + pa_source_assert_ref(s); + + send_signal(u, s); + + return PA_HOOK_OK; +} + +static pa_bool_t message_is_property_get(DBusMessage *m, const char *interface, const char *property) { + const char *i, *p; + DBusError error; + + dbus_error_init(&error); + + pa_assert(m); + + if (!dbus_message_is_method_call(m, "org.freedesktop.DBus.Properties", "Get")) + return FALSE; + + if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &i, DBUS_TYPE_STRING, &p, DBUS_TYPE_INVALID) || dbus_error_is_set(&error)) { + dbus_error_free(&error); + return FALSE; + } + + return pa_streq(i, interface) && pa_streq(p, property); +} + +static pa_bool_t message_is_property_get_all(DBusMessage *m, const char *interface) { + const char *i; + DBusError error; + + dbus_error_init(&error); + + pa_assert(m); + + if (!dbus_message_is_method_call(m, "org.freedesktop.DBus.Properties", "GetAll")) + return FALSE; + + if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &i, DBUS_TYPE_INVALID) || dbus_error_is_set(&error)) { + dbus_error_free(&error); + return FALSE; + } + + return pa_streq(i, interface); +} + +static void append_variant_object_array(DBusMessage *m, DBusMessageIter *iter, const char *path[], unsigned n) { + DBusMessageIter _iter, variant, array; + unsigned c; + + pa_assert(m); + pa_assert(path); + + if (!iter) { + dbus_message_iter_init_append(m, &_iter); + iter = &_iter; + } + + pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "ao", &variant)); + pa_assert_se(dbus_message_iter_open_container(&variant, DBUS_TYPE_ARRAY, "o", &array)); + + for (c = 0; c < n; c++) + pa_assert_se(dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH, path + c)); + + pa_assert_se(dbus_message_iter_close_container(&variant, &array)); + pa_assert_se(dbus_message_iter_close_container(iter, &variant)); +} + +static void append_variant_string(DBusMessage *m, DBusMessageIter *iter, const char *s) { + DBusMessageIter _iter, sub; + + pa_assert(m); + pa_assert(s); + + if (!iter) { + dbus_message_iter_init_append(m, &_iter); + iter = &_iter; + } + + pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "s", &sub)); + pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &s)); + pa_assert_se(dbus_message_iter_close_container(iter, &sub)); +} + +static void append_variant_object(DBusMessage *m, DBusMessageIter *iter, const char *s) { + DBusMessageIter _iter, sub; + + pa_assert(m); + pa_assert(s); + + if (!iter) { + dbus_message_iter_init_append(m, &_iter); + iter = &_iter; + } + + pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "o", &sub)); + pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &s)); + pa_assert_se(dbus_message_iter_close_container(iter, &sub)); +} + +static void append_variant_unsigned(DBusMessage *m, DBusMessageIter *iter, unsigned u) { + DBusMessageIter _iter, sub; + + pa_assert(m); + + if (!iter) { + dbus_message_iter_init_append(m, &_iter); + iter = &_iter; + } + + pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "u", &sub)); + pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &u)); + pa_assert_se(dbus_message_iter_close_container(iter, &sub)); +} + +static void append_property_dict_entry_object_array(DBusMessage *m, DBusMessageIter *iter, const char *name, const char *path[], unsigned n) { + DBusMessageIter sub; + + pa_assert(iter); + + pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY, NULL, &sub)); + pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &name)); + append_variant_object_array(m, &sub, path, n); + pa_assert_se(dbus_message_iter_close_container(iter, &sub)); +} + +static void append_property_dict_entry_string(DBusMessage *m, DBusMessageIter *iter, const char *name, const char *value) { + DBusMessageIter sub; + + pa_assert(iter); + + pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY, NULL, &sub)); + pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &name)); + append_variant_string(m, &sub, value); + pa_assert_se(dbus_message_iter_close_container(iter, &sub)); +} + +static void append_property_dict_entry_object(DBusMessage *m, DBusMessageIter *iter, const char *name, const char *value) { + DBusMessageIter sub; + + pa_assert(iter); + + pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY, NULL, &sub)); + pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &name)); + append_variant_object(m, &sub, value); + pa_assert_se(dbus_message_iter_close_container(iter, &sub)); +} + +static void append_property_dict_entry_unsigned(DBusMessage *m, DBusMessageIter *iter, const char *name, unsigned u) { + DBusMessageIter sub; + + pa_assert(iter); + + pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY, NULL, &sub)); + pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &name)); + append_variant_unsigned(m, &sub, u); + pa_assert_se(dbus_message_iter_close_container(iter, &sub)); +} + +static const char *array_root_containers[] = { OBJECT_SINKS, OBJECT_SOURCES }; +static const char *array_no_children[] = { }; + +static DBusHandlerResult root_handler(DBusConnection *c, DBusMessage *m, void *userdata) { + struct userdata *u = userdata; + DBusMessage *r = NULL; + + pa_assert(u); + + if (message_is_property_get(m, "org.gnome.UPnP.MediaContainer1", "Containers")) { + pa_assert_se(r = dbus_message_new_method_return(m)); + append_variant_object_array(r, NULL, (const char**) array_root_containers, PA_ELEMENTSOF(array_root_containers)); + + } else if (message_is_property_get(m, "org.gnome.UPnP.MediaContainer1", "ContainerCount")) { + pa_assert_se(r = dbus_message_new_method_return(m)); + append_variant_unsigned(r, NULL, PA_ELEMENTSOF(array_root_containers)); + + } else if (message_is_property_get(m, "org.gnome.UPnP.MediaContainer1", "Items")) { + pa_assert_se(r = dbus_message_new_method_return(m)); + append_variant_object_array(r, NULL, array_no_children, PA_ELEMENTSOF(array_no_children)); + + } else if (message_is_property_get(m, "org.gnome.UPnP.MediaContainer1", "ItemCount")) { + pa_assert_se(r = dbus_message_new_method_return(m)); + append_variant_unsigned(r, NULL, PA_ELEMENTSOF(array_no_children)); + + } else if (message_is_property_get_all(m, "org.gnome.UPnP.MediaContainer1")) { + DBusMessageIter iter, sub; + + pa_assert_se(r = dbus_message_new_method_return(m)); + dbus_message_iter_init_append(r, &iter); + + pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub)); + append_property_dict_entry_object_array(r, &sub, "Containers", array_root_containers, PA_ELEMENTSOF(array_root_containers)); + append_property_dict_entry_unsigned(r, &sub, "ContainerCount", PA_ELEMENTSOF(array_root_containers)); + append_property_dict_entry_object_array(r, &sub, "Items", array_no_children, PA_ELEMENTSOF(array_no_children)); + append_property_dict_entry_unsigned(r, &sub, "ItemCount", PA_ELEMENTSOF(array_no_children)); + pa_assert_se(dbus_message_iter_close_container(&iter, &sub)); + + } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject1", "Parent")) { + pa_assert_se(r = dbus_message_new_method_return(m)); + append_variant_object(r, NULL, OBJECT_ROOT); + + } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject1", "DisplayName")) { + pa_assert_se(r = dbus_message_new_method_return(m)); + append_variant_string(r, NULL, u->display_name); + + } else if (message_is_property_get_all(m, "org.gnome.UPnP.MediaObject1")) { + DBusMessageIter iter, sub; + + pa_assert_se(r = dbus_message_new_method_return(m)); + dbus_message_iter_init_append(r, &iter); + + pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub)); + append_property_dict_entry_string(r, &sub, "DisplayName", u->display_name); + pa_assert_se(dbus_message_iter_close_container(&iter, &sub)); + + } else if (dbus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) { + const char *xml = ROOT_INTROSPECT_XML; + + pa_assert_se(r = dbus_message_new_method_return(m)); + pa_assert_se(dbus_message_append_args( + r, + DBUS_TYPE_STRING, &xml, + DBUS_TYPE_INVALID)); + + } else + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + + if (r) { + pa_assert_se(dbus_connection_send(pa_dbus_connection_get(u->bus), r, NULL)); + dbus_message_unref(r); + } + + return DBUS_HANDLER_RESULT_HANDLED; +} + +static char *compute_url(struct userdata *u, const char *name) { + pa_strlist *i; + + pa_assert(u); + pa_assert(name); + + for (i = pa_http_protocol_servers(u->http); 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)) { + + const char *address; + char *s; + + if (pa_is_ip_address(a.path_or_host)) + address = a.path_or_host; + else + address = "@ADDRESS@"; + + if (a.port <= 0) + a.port = 4714; + + s = pa_sprintf_malloc("http://%s:%u/listen/source/%s", address, a.port, name); + + pa_xfree(a.path_or_host); + return s; + } + + pa_xfree(a.path_or_host); + } + + return pa_sprintf_malloc("http://@ADDRESS@:4714/listen/source/%s", name); +} + +static char **child_array(struct userdata *u, const char *path, unsigned *n) { + unsigned m; + uint32_t idx; + char **array; + + pa_assert(u); + pa_assert(path); + pa_assert(n); + + if (pa_streq(path, OBJECT_SINKS)) + m = pa_idxset_size(u->core->sinks); + else + m = pa_idxset_size(u->core->sources); + + array = pa_xnew(char*, m); + *n = 0; + + if (pa_streq(path, OBJECT_SINKS)) { + pa_sink *sink; + + PA_IDXSET_FOREACH(sink, u->core->sinks, idx) + array[(*n)++] = pa_sprintf_malloc(OBJECT_SINKS "/%u", sink->index); + } else { + pa_source *source; + + PA_IDXSET_FOREACH(source, u->core->sources, idx) + if (!source->monitor_of) + array[(*n)++] = pa_sprintf_malloc(OBJECT_SOURCES "/%u", source->index); + } + + pa_assert((*n) <= m); + + return array; +} + +static void free_child_array(char **array, unsigned n) { + + for (; n >= 1; n--) + pa_xfree(array[n-1]); + + pa_xfree(array); +} + +static DBusHandlerResult sinks_and_sources_handler(DBusConnection *c, DBusMessage *m, void *userdata) { + struct userdata *u = userdata; + DBusMessage *r = NULL; + const char *path; + + pa_assert(u); + + path = dbus_message_get_path(m); + + if (pa_streq(path, OBJECT_SINKS) || pa_streq(path, OBJECT_SOURCES)) { + + /* Container nodes */ + + if (message_is_property_get(m, "org.gnome.UPnP.MediaContainer1", "Containers")) { + pa_assert_se(r = dbus_message_new_method_return(m)); + append_variant_object_array(r, NULL, array_no_children, PA_ELEMENTSOF(array_no_children)); + + } else if (message_is_property_get(m, "org.gnome.UPnP.MediaContainer1", "ContainerCount")) { + pa_assert_se(r = dbus_message_new_method_return(m)); + append_variant_unsigned(r, NULL, PA_ELEMENTSOF(array_no_children)); + + } else if (message_is_property_get(m, "org.gnome.UPnP.MediaContainer1", "Items")) { + char ** array; + unsigned n; + + array = child_array(u, path, &n); + + pa_assert_se(r = dbus_message_new_method_return(m)); + append_variant_object_array(r, NULL, (const char**) array, n); + + free_child_array(array, n); + + } else if (message_is_property_get(m, "org.gnome.UPnP.MediaContainer1", "ItemCount")) { + pa_assert_se(r = dbus_message_new_method_return(m)); + append_variant_unsigned(r, NULL, + pa_streq(path, OBJECT_SINKS) ? + pa_idxset_size(u->core->sinks) : + pa_idxset_size(u->core->sources)); + + } else if (message_is_property_get_all(m, "org.gnome.UPnP.MediaContainer1")) { + DBusMessageIter iter, sub; + char **array; + unsigned n; + + pa_assert_se(r = dbus_message_new_method_return(m)); + dbus_message_iter_init_append(r, &iter); + + pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub)); + append_property_dict_entry_object_array(r, &sub, "Containers", array_no_children, PA_ELEMENTSOF(array_no_children)); + append_property_dict_entry_unsigned(r, &sub, "ContainerCount", 0); + + array = child_array(u, path, &n); + append_property_dict_entry_object_array(r, &sub, "Items", (const char**) array, n); + free_child_array(array, n); + append_property_dict_entry_unsigned(r, &sub, "ItemCount", + pa_streq(path, OBJECT_SINKS) ? + pa_idxset_size(u->core->sinks) : + pa_idxset_size(u->core->sources)); + + pa_assert_se(dbus_message_iter_close_container(&iter, &sub)); + + } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject1", "Parent")) { + pa_assert_se(r = dbus_message_new_method_return(m)); + append_variant_object(r, NULL, OBJECT_ROOT); + + } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject1", "DisplayName")) { + pa_assert_se(r = dbus_message_new_method_return(m)); + append_variant_string(r, + NULL, + pa_streq(path, OBJECT_SINKS) ? + _("Output Devices") : + _("Input Devices")); + + } else if (message_is_property_get_all(m, "org.gnome.UPnP.MediaObject1")) { + DBusMessageIter iter, sub; + + pa_assert_se(r = dbus_message_new_method_return(m)); + + dbus_message_iter_init_append(r, &iter); + + pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub)); + append_property_dict_entry_object(m, &sub, "Parent", OBJECT_ROOT); + append_property_dict_entry_string(m, &sub, "DisplayName", + pa_streq(path, OBJECT_SINKS) ? + _("Output Devices") : + _("Input Devices")); + pa_assert_se(dbus_message_iter_close_container(&iter, &sub)); + + } else if (dbus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) { + pa_strbuf *sb; + char *xml; + uint32_t idx; + + sb = pa_strbuf_new(); + pa_strbuf_puts(sb, CONTAINER_INTROSPECT_XML_PREFIX); + + if (pa_streq(path, OBJECT_SINKS)) { + pa_sink *sink; + + PA_IDXSET_FOREACH(sink, u->core->sinks, idx) + pa_strbuf_printf(sb, "<node name=\"%u\"/>", sink->index); + } else { + pa_source *source; + + PA_IDXSET_FOREACH(source, u->core->sources, idx) + if (!source->monitor_of) + pa_strbuf_printf(sb, "<node name=\"%u\"/>", source->index); + } + + pa_strbuf_puts(sb, CONTAINER_INTROSPECT_XML_POSTFIX); + xml = pa_strbuf_tostring_free(sb); + + pa_assert_se(r = dbus_message_new_method_return(m)); + pa_assert_se(dbus_message_append_args( + r, + DBUS_TYPE_STRING, &xml, + DBUS_TYPE_INVALID)); + + pa_xfree(xml); + } else + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + + } else { + pa_sink *sink = NULL; + pa_source *source = NULL; + + /* Child nodes */ + + if (pa_startswith(path, OBJECT_SINKS "/")) + sink = pa_namereg_get(u->core, path + sizeof(OBJECT_SINKS), PA_NAMEREG_SINK); + else if (pa_startswith(path, OBJECT_SOURCES "/")) + source = pa_namereg_get(u->core, path + sizeof(OBJECT_SOURCES), PA_NAMEREG_SOURCE); + + if (!sink && !source) + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + + if (message_is_property_get(m, "org.gnome.UPnP.MediaObject1", "Parent")) { + pa_assert_se(r = dbus_message_new_method_return(m)); + append_variant_object(r, NULL, sink ? OBJECT_SINKS : OBJECT_SOURCES); + + } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject1", "DisplayName")) { + pa_assert_se(r = dbus_message_new_method_return(m)); + append_variant_string(r, NULL, pa_strna(pa_proplist_gets(sink ? sink->proplist : source->proplist, PA_PROP_DEVICE_DESCRIPTION))); + + } else if (message_is_property_get_all(m, "org.gnome.UPnP.MediaObject1")) { + DBusMessageIter iter, sub; + + pa_assert_se(r = dbus_message_new_method_return(m)); + dbus_message_iter_init_append(r, &iter); + + pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub)); + append_property_dict_entry_object(r, &sub, "Parent", sink ? OBJECT_SINKS : OBJECT_SOURCES); + append_property_dict_entry_string(r, &sub, "DisplayName", pa_strna(pa_proplist_gets(sink ? sink->proplist : source->proplist, PA_PROP_DEVICE_DESCRIPTION))); + pa_assert_se(dbus_message_iter_close_container(&iter, &sub)); + + } else if (message_is_property_get(m, "org.gnome.UPnP.MediaItem1", "Type")) { + pa_assert_se(r = dbus_message_new_method_return(m)); + append_variant_string(r, NULL, "audio"); + + } else if (message_is_property_get(m, "org.gnome.UPnP.MediaItem1", "MIMEType")) { + char *t; + + if (sink) + t = pa_sample_spec_to_mime_type_mimefy(&sink->sample_spec, &sink->channel_map); + else + t = pa_sample_spec_to_mime_type_mimefy(&source->sample_spec, &source->channel_map); + + pa_assert_se(r = dbus_message_new_method_return(m)); + append_variant_string(r, NULL, t); + pa_xfree(t); + + } else if (message_is_property_get(m, "org.gnome.UPnP.MediaItem1", "URLs")) { + DBusMessageIter iter, sub, array; + char *url; + + pa_assert_se(r = dbus_message_new_method_return(m)); + + dbus_message_iter_init_append(r, &iter); + + url = compute_url(u, sink ? sink->monitor_source->name : source->name); + + pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, "as", &sub)); + pa_assert_se(dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "s", &array)); + pa_assert_se(dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING, &url)); + pa_assert_se(dbus_message_iter_close_container(&sub, &array)); + pa_assert_se(dbus_message_iter_close_container(&iter, &sub)); + + pa_xfree(url); + + } else if (message_is_property_get_all(m, "org.gnome.UPnP.MediaItem1")) { + DBusMessageIter iter, sub, dict, variant, array; + char *url, *t; + const char *un = "URLs"; + + pa_assert_se(r = dbus_message_new_method_return(m)); + dbus_message_iter_init_append(r, &iter); + + pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub)); + append_property_dict_entry_string(r, &sub, "Type", "audio"); + + if (sink) + t = pa_sample_spec_to_mime_type_mimefy(&sink->sample_spec, &sink->channel_map); + else + t = pa_sample_spec_to_mime_type_mimefy(&source->sample_spec, &source->channel_map); + + append_property_dict_entry_string(r, &sub, "MIMEType", t); + pa_xfree(t); + + pa_assert_se(dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &dict)); + pa_assert_se(dbus_message_iter_append_basic(&dict, DBUS_TYPE_STRING, &un)); + + url = compute_url(u, sink ? sink->monitor_source->name : source->name); + + pa_assert_se(dbus_message_iter_open_container(&dict, DBUS_TYPE_VARIANT, "as", &variant)); + pa_assert_se(dbus_message_iter_open_container(&variant, DBUS_TYPE_ARRAY, "s", &array)); + pa_assert_se(dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING, &url)); + pa_assert_se(dbus_message_iter_close_container(&variant, &array)); + pa_assert_se(dbus_message_iter_close_container(&dict, &variant)); + pa_assert_se(dbus_message_iter_close_container(&sub, &dict)); + + pa_xfree(url); + + pa_assert_se(dbus_message_iter_close_container(&iter, &sub)); + + } else if (dbus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) { + const char *xml = + ITEM_INTROSPECT_XML; + + pa_assert_se(r = dbus_message_new_method_return(m)); + pa_assert_se(dbus_message_append_args( + r, + DBUS_TYPE_STRING, &xml, + DBUS_TYPE_INVALID)); + + } else + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + } + + if (r) { + pa_assert_se(dbus_connection_send(pa_dbus_connection_get(u->bus), r, NULL)); + dbus_message_unref(r); + } + + return DBUS_HANDLER_RESULT_HANDLED; +} + +int pa__init(pa_module *m) { + + struct userdata *u; + pa_modargs *ma = NULL; + DBusError error; + const char *t; + + static const DBusObjectPathVTable vtable_root = { + .message_function = root_handler, + }; + static const DBusObjectPathVTable vtable_sinks_and_sources = { + .message_function = sinks_and_sources_handler, + }; + + dbus_error_init(&error); + + if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { + pa_log("Failed to parse module arguments."); + goto fail; + } + + m->userdata = u = pa_xnew0(struct userdata, 1); + u->core = m->core; + u->module = m; + u->http = pa_http_protocol_get(u->core); + + if ((t = pa_modargs_get_value(ma, "display_name", NULL))) + u->display_name = pa_utf8_filter(t); + else + u->display_name = pa_xstrdup(_("Audio on @HOSTNAME@")); + + u->source_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_LATE, (pa_hook_cb_t) source_new_or_unlink_cb, u); + u->source_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) source_new_or_unlink_cb, u); + + if (!(u->bus = pa_dbus_bus_get(m->core, DBUS_BUS_SESSION, &error))) { + pa_log("Failed to get session bus connection: %s", error.message); + goto fail; + } + + pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(u->bus), OBJECT_ROOT, &vtable_root, u)); + pa_assert_se(dbus_connection_register_fallback(pa_dbus_connection_get(u->bus), OBJECT_SINKS, &vtable_sinks_and_sources, u)); + pa_assert_se(dbus_connection_register_fallback(pa_dbus_connection_get(u->bus), OBJECT_SOURCES, &vtable_sinks_and_sources, u)); + + if (dbus_bus_request_name(pa_dbus_connection_get(u->bus), SERVICE_NAME, DBUS_NAME_FLAG_DO_NOT_QUEUE, &error) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) { + pa_log("Failed to request service name " SERVICE_NAME ": %s", error.message); + goto fail; + } + + u->got_name = TRUE; + + pa_modargs_free(ma); + + return 0; + +fail: + pa__done(m); + + if (ma) + pa_modargs_free(ma); + + dbus_error_free(&error); + + return -1; +} + +void pa__done(pa_module*m) { + struct userdata*u; + pa_assert(m); + + if (!(u = m->userdata)) + return; + + if (u->source_new_slot) + pa_hook_slot_free(u->source_new_slot); + if (u->source_unlink_slot) + pa_hook_slot_free(u->source_unlink_slot); + + if (u->bus) { + DBusError error; + + dbus_error_init(&error); + + dbus_connection_unregister_object_path(pa_dbus_connection_get(u->bus), OBJECT_ROOT); + dbus_connection_unregister_object_path(pa_dbus_connection_get(u->bus), OBJECT_SINKS); + dbus_connection_unregister_object_path(pa_dbus_connection_get(u->bus), OBJECT_SOURCES); + + if (u->got_name) { + if (dbus_bus_release_name(pa_dbus_connection_get(u->bus), SERVICE_NAME, &error) != DBUS_RELEASE_NAME_REPLY_RELEASED) { + pa_log("Failed to release service name " SERVICE_NAME ": %s", error.message); + dbus_error_free(&error); + } + } + + pa_dbus_connection_unref(u->bus); + } + + pa_xfree(u->display_name); + + if (u->http) + pa_http_protocol_unref(u->http); + + pa_xfree(u); +} diff --git a/src/modules/module-sine-source.c b/src/modules/module-sine-source.c index 23831a0d..3145684c 100644 --- a/src/modules/module-sine-source.c +++ b/src/modules/module-sine-source.c @@ -34,19 +34,20 @@ #include <sys/ioctl.h> #include <sys/poll.h> -#include <pulse/xmalloc.h> +#include <pulse/rtclock.h> #include <pulse/timeval.h> +#include <pulse/xmalloc.h> #include <pulsecore/core-error.h> #include <pulsecore/source.h> #include <pulsecore/module.h> +#include <pulsecore/core-rtclock.h> #include <pulsecore/core-util.h> #include <pulsecore/modargs.h> #include <pulsecore/log.h> #include <pulsecore/thread.h> #include <pulsecore/thread-mq.h> #include <pulsecore/rtpoll.h> -#include <pulsecore/rtclock.h> #include "module-sine-source-symdef.h" @@ -55,12 +56,13 @@ PA_MODULE_DESCRIPTION("Sine wave generator source"); PA_MODULE_VERSION(PACKAGE_VERSION); PA_MODULE_LOAD_ONCE(FALSE); PA_MODULE_USAGE( - "rate=<sample rate> " "source_name=<name for the source> " + "source_properties=<properties for the source> " + "rate=<sample rate> " "frequency=<frequency in Hz>"); #define DEFAULT_SOURCE_NAME "sine_input" -#define MAX_LATENCY_USEC (PA_USEC_PER_SEC * 2) +#define BLOCK_USEC (PA_USEC_PER_SEC * 2) struct userdata { pa_core *core; @@ -79,8 +81,9 @@ struct userdata { }; static const char* const valid_modargs[] = { - "rate", "source_name", + "source_properties", + "rate", "frequency", NULL }; @@ -99,14 +102,14 @@ static int source_process_msg( case PA_SOURCE_MESSAGE_SET_STATE: if (PA_PTR_TO_UINT(data) == PA_SOURCE_RUNNING) - u->timestamp = pa_rtclock_usec(); + u->timestamp = pa_rtclock_now(); break; case PA_SOURCE_MESSAGE_GET_LATENCY: { pa_usec_t now, left_to_fill; - now = pa_rtclock_usec(); + now = pa_rtclock_now(); left_to_fill = u->timestamp > now ? u->timestamp - now : 0ULL; *((pa_usec_t*) data) = u->block_usec > left_to_fill ? u->block_usec - left_to_fill : 0ULL; @@ -164,9 +167,8 @@ static void thread_func(void *userdata) { pa_log_debug("Thread starting up"); pa_thread_mq_install(&u->thread_mq); - pa_rtpoll_install(u->rtpoll); - u->timestamp = pa_rtclock_usec(); + u->timestamp = pa_rtclock_now(); for (;;) { int ret; @@ -174,7 +176,7 @@ static void thread_func(void *userdata) { if (PA_SOURCE_IS_OPENED(u->source->thread_info.state)) { pa_usec_t now; - now = pa_rtclock_usec(); + now = pa_rtclock_now(); if (u->timestamp <= now) process_render(u, now); @@ -248,6 +250,12 @@ int pa__init(pa_module*m) { pa_proplist_setf(data.proplist, "sine.hz", "%u", frequency); pa_source_new_data_set_sample_spec(&data, &ss); + if (pa_modargs_get_proplist(ma, "source_properties", data.proplist, PA_UPDATE_REPLACE) < 0) { + pa_log("Invalid properties"); + pa_source_new_data_done(&data); + goto fail; + } + u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY); pa_source_new_data_done(&data); @@ -260,11 +268,11 @@ int pa__init(pa_module*m) { u->source->update_requested_latency = source_update_requested_latency_cb; u->source->userdata = u; + u->block_usec = BLOCK_USEC; + pa_source_set_asyncmsgq(u->source, u->thread_mq.inq); pa_source_set_rtpoll(u->source, u->rtpoll); - - pa_source_set_latency_range(u->source, (pa_usec_t) -1, MAX_LATENCY_USEC); - u->block_usec = u->source->thread_info.max_latency; + pa_source_set_fixed_latency(u->source, u->block_usec); if (!(u->thread = pa_thread_new(thread_func, u))) { pa_log("Failed to create thread."); diff --git a/src/modules/module-solaris.c b/src/modules/module-solaris.c index 995b3c63..0920d25e 100644 --- a/src/modules/module-solaris.c +++ b/src/modules/module-solaris.c @@ -46,6 +46,7 @@ #include <pulse/xmalloc.h> #include <pulse/timeval.h> #include <pulse/util.h> +#include <pulse/rtclock.h> #include <pulsecore/iochannel.h> #include <pulsecore/sink.h> @@ -59,7 +60,6 @@ #include <pulsecore/thread-mq.h> #include <pulsecore/rtpoll.h> #include <pulsecore/thread.h> -#include <pulsecore/rtclock.h> #include "module-solaris-symdef.h" @@ -68,14 +68,16 @@ PA_MODULE_DESCRIPTION("Solaris Sink/Source"); PA_MODULE_VERSION(PACKAGE_VERSION); PA_MODULE_USAGE( "sink_name=<name for the sink> " + "sink_properties=<properties for the sink> " "source_name=<name for the source> " + "source_properties=<properties for the source> " "device=<audio device file name> " "record=<enable source?> " "playback=<enable sink?> " "format=<sample format> " "channels=<number of channels> " "rate=<sample rate> " - "buffer_size=<record buffer size> " + "buffer_length=<milliseconds> " "channel_map=<channel map>"); PA_MODULE_LOAD_ONCE(FALSE); @@ -94,8 +96,7 @@ struct userdata { uint32_t frame_size; int32_t buffer_size; - volatile uint64_t written_bytes, read_bytes; - pa_mutex *written_bytes_lock; + uint64_t written_bytes, read_bytes; char *device_name; int mode; @@ -107,18 +108,19 @@ struct userdata { uint32_t play_samples_msw, record_samples_msw; uint32_t prev_playback_samples, prev_record_samples; - pa_mutex *sample_counter_lock; - size_t min_request; + int32_t minimum_request; }; static const char* const valid_modargs[] = { "sink_name", + "sink_properties", "source_name", + "source_properties", "device", "record", "playback", - "buffer_size", + "buffer_length", "format", "rate", "channels", @@ -127,13 +129,9 @@ static const char* const valid_modargs[] = { }; #define DEFAULT_DEVICE "/dev/audio" -#define MIN_BUFFER_SIZE (640) -#define MAX_RENDER_HZ (300) -/* This render rate limit implies a minimum latency, but without it we waste too much CPU time in the - * IO thread. The maximum render rate and minimum latency (or minimum buffer size) are unachievable on - * common hardware anyway. Note that MIN_BUFFER_SIZE * MAX_RENDER_HZ >= 4 * 48000 Bps. - */ +#define MAX_RENDER_HZ (300) +/* This render rate limit imposes a minimum latency, but without it we waste too much CPU time. */ static uint64_t get_playback_buffered_bytes(struct userdata *u) { audio_info_t info; @@ -142,8 +140,6 @@ static uint64_t get_playback_buffered_bytes(struct userdata *u) { pa_assert(u->sink); - pa_mutex_lock(u->sample_counter_lock); - err = ioctl(u->fd, AUDIO_GETINFO, &info); pa_assert(err >= 0); @@ -159,8 +155,6 @@ static uint64_t get_playback_buffered_bytes(struct userdata *u) { u->prev_playback_samples = info.play.samples; played_bytes = (((uint64_t)u->play_samples_msw << 32) + info.play.samples) * u->frame_size; - pa_mutex_unlock(u->sample_counter_lock); - return u->written_bytes - played_bytes; } @@ -171,11 +165,9 @@ static pa_usec_t sink_get_latency(struct userdata *u, pa_sample_spec *ss) { pa_assert(ss); if (u->fd >= 0) { - pa_mutex_lock(u->written_bytes_lock); r = pa_bytes_to_usec(get_playback_buffered_bytes(u), ss); if (u->memchunk.memblock) r += pa_bytes_to_usec(u->memchunk.length, ss); - pa_mutex_unlock(u->written_bytes_lock); } return r; } @@ -487,7 +479,7 @@ static void sink_set_volume(pa_sink *s) { if (u->fd >= 0) { AUDIO_INITINFO(&info); - info.play.gain = pa_cvolume_avg(&s->virtual_volume) * AUDIO_MAX_GAIN / PA_VOLUME_NORM; + info.play.gain = pa_cvolume_max(&s->virtual_volume) * AUDIO_MAX_GAIN / PA_VOLUME_NORM; assert(info.play.gain <= AUDIO_MAX_GAIN); if (ioctl(u->fd, AUDIO_SETINFO, &info) < 0) { @@ -523,7 +515,7 @@ static void source_set_volume(pa_source *s) { if (u->fd >= 0) { AUDIO_INITINFO(&info); - info.play.gain = pa_cvolume_avg(&s->virtual_volume) * AUDIO_MAX_GAIN / PA_VOLUME_NORM; + info.play.gain = pa_cvolume_max(&s->virtual_volume) * AUDIO_MAX_GAIN / PA_VOLUME_NORM; assert(info.play.gain <= AUDIO_MAX_GAIN); if (ioctl(u->fd, AUDIO_SETINFO, &info) < 0) { @@ -580,6 +572,25 @@ static void sink_get_mute(pa_sink *s) { } } +static void process_rewind(struct userdata *u) { + size_t rewind_nbytes; + + pa_assert(u); + + /* Figure out how much we shall rewind and reset the counter */ + rewind_nbytes = u->sink->thread_info.rewind_nbytes; + u->sink->thread_info.rewind_nbytes = 0; + + if (rewind_nbytes > 0) { + pa_log_debug("Requested to rewind %lu bytes.", (unsigned long) rewind_nbytes); + rewind_nbytes = PA_MIN(u->memchunk.length, rewind_nbytes); + u->memchunk.length -= rewind_nbytes; + pa_log_debug("Rewound %lu bytes.", (unsigned long) rewind_nbytes); + } + + pa_sink_process_rewind(u->sink, rewind_nbytes); +} + static void thread_func(void *userdata) { struct userdata *u = userdata; unsigned short revents = 0; @@ -594,7 +605,6 @@ static void thread_func(void *userdata) { pa_make_realtime(u->core->realtime_priority); pa_thread_mq_install(&u->thread_mq); - pa_rtpoll_install(u->rtpoll); for (;;) { /* Render some data and write it to the dsp */ @@ -604,10 +614,13 @@ static void thread_func(void *userdata) { uint64_t buffered_bytes; if (u->sink->thread_info.rewind_requested) - pa_sink_process_rewind(u->sink, 0); + process_rewind(u); err = ioctl(u->fd, AUDIO_GETINFO, &info); - pa_assert(err >= 0); + if (err < 0) { + pa_log("AUDIO_GETINFO ioctl failed: %s", pa_cstrerror(errno)); + goto fail; + } if (info.play.error) { pa_log_debug("buffer under-run!"); @@ -627,7 +640,7 @@ static void thread_func(void *userdata) { * Since we cannot modify the size of the output buffer we fake it * by not filling it more than u->buffer_size. */ - xtime0 = pa_rtclock_usec(); + xtime0 = pa_rtclock_now(); buffered_bytes = get_playback_buffered_bytes(u); if (buffered_bytes >= (uint64_t)u->buffer_size) break; @@ -635,7 +648,7 @@ static void thread_func(void *userdata) { len = u->buffer_size - buffered_bytes; len -= len % u->frame_size; - if (len < u->min_request) + if (len < (size_t) u->minimum_request) break; if (u->memchunk.length < len) @@ -648,12 +661,16 @@ static void thread_func(void *userdata) { if (w <= 0) { switch (errno) { case EINTR: - break; + continue; case EAGAIN: + /* If the buffer_size is too big, we get EAGAIN. Avoiding that limit by trial and error + * is not ideal, but I don't know how to get the system to tell me what the limit is. + */ u->buffer_size = u->buffer_size * 18 / 25; u->buffer_size -= u->buffer_size % u->frame_size; - u->buffer_size = PA_MAX(u->buffer_size, (int32_t)MIN_BUFFER_SIZE); - pa_sink_set_max_request(u->sink, u->buffer_size); + u->buffer_size = PA_MAX(u->buffer_size, 2 * u->minimum_request); + pa_sink_set_max_request_within_thread(u->sink, u->buffer_size); + pa_sink_set_max_rewind_within_thread(u->sink, u->buffer_size); pa_log("EAGAIN. Buffer size is now %u bytes (%llu buffered)", u->buffer_size, buffered_bytes); break; default: @@ -663,10 +680,8 @@ static void thread_func(void *userdata) { } else { pa_assert(w % u->frame_size == 0); - pa_mutex_lock(u->written_bytes_lock); u->written_bytes += w; u->memchunk.length -= w; - pa_mutex_unlock(u->written_bytes_lock); u->memchunk.index += w; if (u->memchunk.length <= 0) { @@ -677,9 +692,8 @@ static void thread_func(void *userdata) { } pa_rtpoll_set_timer_absolute(u->rtpoll, xtime0 + pa_bytes_to_usec(buffered_bytes / 2, &u->sink->sample_spec)); - } else { + } else pa_rtpoll_set_timer_disabled(u->rtpoll); - } /* Try to read some data and pass it on to the source driver */ @@ -783,7 +797,7 @@ static void sig_callback(pa_mainloop_api *api, pa_signal_event*e, int sig, void pa_log_debug("caught signal"); if (u->sink) { - pa_sink_get_volume(u->sink, TRUE); + pa_sink_get_volume(u->sink, TRUE, FALSE); pa_sink_get_mute(u->sink, TRUE); } @@ -797,6 +811,7 @@ int pa__init(pa_module *m) { pa_sample_spec ss; pa_channel_map map; pa_modargs *ma = NULL; + uint32_t buffer_length_msec; int fd; pa_sink_new_data sink_new_data; pa_source_new_data source_new_data; @@ -822,8 +837,6 @@ int pa__init(pa_module *m) { } u = pa_xnew0(struct userdata, 1); - u->sample_counter_lock = pa_mutex_new(FALSE, FALSE); - u->written_bytes_lock = pa_mutex_new(FALSE, FALSE); /* * For a process (or several processes) to use the same audio device for both @@ -839,13 +852,15 @@ int pa__init(pa_module *m) { } u->frame_size = pa_frame_size(&ss); - u->buffer_size = 16384; - if (pa_modargs_get_value_s32(ma, "buffer_size", &u->buffer_size) < 0) { - pa_log("failed to parse buffer size argument"); + u->minimum_request = pa_usec_to_bytes(PA_USEC_PER_SEC / MAX_RENDER_HZ, &ss); + + buffer_length_msec = 100; + if (pa_modargs_get_value_u32(ma, "buffer_length", &buffer_length_msec) < 0) { + pa_log("failed to parse buffer_length argument"); goto fail; } - u->buffer_size -= u->buffer_size % u->frame_size; - if (u->buffer_size < (int32_t)MIN_BUFFER_SIZE) { + u->buffer_size = pa_usec_to_bytes(1000 * buffer_length_msec, &ss); + if (u->buffer_size < 2 * u->minimum_request) { pa_log("supplied buffer size argument is too small"); goto fail; } @@ -885,10 +900,16 @@ int pa__init(pa_module *m) { pa_source_new_data_set_channel_map(&source_new_data, &map); pa_proplist_sets(source_new_data.proplist, PA_PROP_DEVICE_STRING, u->device_name); pa_proplist_sets(source_new_data.proplist, PA_PROP_DEVICE_API, "solaris"); - pa_proplist_setf(source_new_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Solaris PCM source"); + pa_proplist_sets(source_new_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Solaris PCM source"); pa_proplist_sets(source_new_data.proplist, PA_PROP_DEVICE_ACCESS_MODE, "serial"); pa_proplist_setf(source_new_data.proplist, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE, "%lu", (unsigned long) u->buffer_size); + if (pa_modargs_get_proplist(ma, "source_properties", source_new_data.proplist, PA_UPDATE_REPLACE) < 0) { + pa_log("Invalid properties"); + pa_source_new_data_done(&source_new_data); + goto fail; + } + u->source = pa_source_new(m->core, &source_new_data, PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY|PA_SOURCE_HW_VOLUME_CTRL); pa_source_new_data_done(&source_new_data); pa_xfree(name_buf); @@ -927,9 +948,15 @@ int pa__init(pa_module *m) { pa_sink_new_data_set_channel_map(&sink_new_data, &map); pa_proplist_sets(sink_new_data.proplist, PA_PROP_DEVICE_STRING, u->device_name); pa_proplist_sets(sink_new_data.proplist, PA_PROP_DEVICE_API, "solaris"); - pa_proplist_setf(sink_new_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Solaris PCM sink"); + pa_proplist_sets(sink_new_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Solaris PCM sink"); pa_proplist_sets(sink_new_data.proplist, PA_PROP_DEVICE_ACCESS_MODE, "serial"); + if (pa_modargs_get_proplist(ma, "sink_properties", sink_new_data.proplist, PA_UPDATE_REPLACE) < 0) { + pa_log("Invalid properties"); + pa_sink_new_data_done(&sink_new_data); + goto fail; + } + u->sink = pa_sink_new(m->core, &sink_new_data, PA_SINK_HARDWARE|PA_SINK_LATENCY|PA_SINK_HW_VOLUME_CTRL|PA_SINK_HW_MUTE_CTRL); pa_sink_new_data_done(&sink_new_data); @@ -946,16 +973,18 @@ int pa__init(pa_module *m) { u->sink->set_mute = sink_set_mute; u->sink->refresh_volume = u->sink->refresh_muted = TRUE; - u->sink->thread_info.max_request = u->buffer_size; - u->min_request = pa_usec_to_bytes(PA_USEC_PER_SEC / MAX_RENDER_HZ, &ss); + pa_sink_set_max_request(u->sink, u->buffer_size); + pa_sink_set_max_rewind(u->sink, u->buffer_size); } else u->sink = NULL; pa_assert(u->source || u->sink); u->sig = pa_signal_new(SIGPOLL, sig_callback, u); - pa_assert(u->sig); - ioctl(u->fd, I_SETSIG, S_MSG); + if (u->sig) + ioctl(u->fd, I_SETSIG, S_MSG); + else + pa_log_warn("Could not register SIGPOLL handler"); if (!(u->thread = pa_thread_new(thread_func, u))) { pa_log("Failed to create thread."); @@ -1010,8 +1039,10 @@ void pa__done(pa_module *m) { if (!(u = m->userdata)) return; - ioctl(u->fd, I_SETSIG, 0); - pa_signal_free(u->sig); + if (u->sig) { + ioctl(u->fd, I_SETSIG, 0); + pa_signal_free(u->sig); + } if (u->sink) pa_sink_unlink(u->sink); @@ -1044,9 +1075,6 @@ void pa__done(pa_module *m) { if (u->fd >= 0) close(u->fd); - pa_mutex_free(u->written_bytes_lock); - pa_mutex_free(u->sample_counter_lock); - pa_xfree(u->device_name); pa_xfree(u); diff --git a/src/modules/module-stream-restore.c b/src/modules/module-stream-restore.c index 723b5d73..8c0bb6b0 100644 --- a/src/modules/module-stream-restore.c +++ b/src/modules/module-stream-restore.c @@ -30,12 +30,12 @@ #include <stdio.h> #include <stdlib.h> #include <ctype.h> -#include <gdbm.h> #include <pulse/xmalloc.h> #include <pulse/volume.h> #include <pulse/timeval.h> #include <pulse/util.h> +#include <pulse/rtclock.h> #include <pulsecore/core-error.h> #include <pulsecore/module.h> @@ -49,6 +49,7 @@ #include <pulsecore/protocol-native.h> #include <pulsecore/pstream.h> #include <pulsecore/pstream-util.h> +#include <pulsecore/database.h> #include "module-stream-restore-symdef.h" @@ -59,15 +60,19 @@ PA_MODULE_LOAD_ONCE(TRUE); PA_MODULE_USAGE( "restore_device=<Save/restore sinks/sources?> " "restore_volume=<Save/restore volumes?> " - "restore_muted=<Save/restore muted states?>"); + "restore_muted=<Save/restore muted states?> " + "on_hotplug=<When new device becomes available, recheck streams?> " + "on_rescue=<When device becomes unavailable, recheck streams?>"); -#define SAVE_INTERVAL 10 +#define SAVE_INTERVAL (10 * PA_USEC_PER_SEC) #define IDENTIFICATION_PROPERTY "module-stream-restore.id" static const char* const valid_modargs[] = { "restore_device", "restore_volume", "restore_muted", + "on_hotplug", + "on_rescue", NULL }; @@ -79,27 +84,32 @@ struct userdata { *sink_input_new_hook_slot, *sink_input_fixate_hook_slot, *source_output_new_hook_slot, + *sink_put_hook_slot, + *source_put_hook_slot, + *sink_unlink_hook_slot, + *source_unlink_hook_slot, *connection_unlink_hook_slot; pa_time_event *save_time_event; - GDBM_FILE gdbm_file; + pa_database* database; pa_bool_t restore_device:1; pa_bool_t restore_volume:1; pa_bool_t restore_muted:1; + pa_bool_t on_hotplug:1; + pa_bool_t on_rescue:1; pa_native_protocol *protocol; pa_idxset *subscribed; }; -#define ENTRY_VERSION 1 +#define ENTRY_VERSION 2 struct entry { uint8_t version; - pa_bool_t muted_valid:1, relative_volume_valid:1, absolute_volume_valid:1, device_valid:1; + pa_bool_t muted_valid:1, volume_valid:1, device_valid:1; pa_bool_t muted:1; pa_channel_map channel_map; - pa_cvolume relative_volume; - pa_cvolume absolute_volume; + pa_cvolume volume; char device[PA_NAME_MAX]; } PA_GCC_PACKED; @@ -112,19 +122,18 @@ enum { SUBCOMMAND_EVENT }; -static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) { +static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) { struct userdata *u = userdata; pa_assert(a); pa_assert(e); - pa_assert(tv); pa_assert(u); pa_assert(e == u->save_time_event); u->core->mainloop->time_free(u->save_time_event); u->save_time_event = NULL; - gdbm_sync(u->gdbm_file); + pa_database_sync(u->database); pa_log_info("Synced."); } @@ -154,28 +163,28 @@ static char *get_name(pa_proplist *p, const char *prefix) { } static struct entry* read_entry(struct userdata *u, const char *name) { - datum key, data; + pa_datum key, data; struct entry *e; pa_assert(u); pa_assert(name); - key.dptr = (char*) name; - key.dsize = (int) strlen(name); + key.data = (char*) name; + key.size = strlen(name); - data = gdbm_fetch(u->gdbm_file, key); + pa_zero(data); - if (!data.dptr) + if (!pa_database_get(u->database, &key, &data)) goto fail; - if (data.dsize != sizeof(struct entry)) { + if (data.size != sizeof(struct entry)) { /* This is probably just a database upgrade, hence let's not * consider this more than a debug message */ - pa_log_debug("Database contains entry for stream %s of wrong size %lu != %lu. Probably due to uprade, ignoring.", name, (unsigned long) data.dsize, (unsigned long) sizeof(struct entry)); + pa_log_debug("Database contains entry for stream %s of wrong size %lu != %lu. Probably due to uprade, ignoring.", name, (unsigned long) data.size, (unsigned long) sizeof(struct entry)); goto fail; } - e = (struct entry*) data.dptr; + e = (struct entry*) data.data; if (e->version != ENTRY_VERSION) { pa_log_debug("Version of database entry for stream %s doesn't match our version. Probably due to upgrade, ignoring.", name); @@ -192,13 +201,12 @@ static struct entry* read_entry(struct userdata *u, const char *name) { goto fail; } - if ((e->relative_volume_valid || e->absolute_volume_valid) && !(pa_channel_map_valid(&e->channel_map))) { + if (e->volume_valid && !pa_channel_map_valid(&e->channel_map)) { pa_log_warn("Invalid channel map stored in database for stream %s", name); goto fail; } - if ((e->relative_volume_valid && (!pa_cvolume_valid(&e->relative_volume) || e->relative_volume.channels != e->channel_map.channels)) || - (e->absolute_volume_valid && (!pa_cvolume_valid(&e->absolute_volume) || e->absolute_volume.channels != e->channel_map.channels))) { + if (e->volume_valid && (!pa_cvolume_valid(&e->volume) || !pa_cvolume_compatible_with_channel_map(&e->volume, &e->channel_map))) { pa_log_warn("Invalid volume stored in database for stream %s", name); goto fail; } @@ -207,12 +215,11 @@ static struct entry* read_entry(struct userdata *u, const char *name) { fail: - pa_xfree(data.dptr); + pa_datum_free(&data); return NULL; } static void trigger_save(struct userdata *u) { - struct timeval tv; pa_native_connection *c; uint32_t idx; @@ -232,9 +239,7 @@ static void trigger_save(struct userdata *u) { if (u->save_time_event) return; - pa_gettimeofday(&tv); - tv.tv_sec += SAVE_INTERVAL; - u->save_time_event = u->core->mainloop->time_new(u->core->mainloop, &tv, save_time_callback, u); + u->save_time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + SAVE_INTERVAL, save_time_callback, u); } static pa_bool_t entries_equal(const struct entry *a, const struct entry *b) { @@ -251,14 +256,9 @@ static pa_bool_t entries_equal(const struct entry *a, const struct entry *b) { (a->muted_valid && (a->muted != b->muted))) return FALSE; - t = b->relative_volume; - if (a->relative_volume_valid != b->relative_volume_valid || - (a->relative_volume_valid && !pa_cvolume_equal(pa_cvolume_remap(&t, &b->channel_map, &a->channel_map), &a->relative_volume))) - return FALSE; - - t = b->absolute_volume; - if (a->absolute_volume_valid != b->absolute_volume_valid || - (a->absolute_volume_valid && !pa_cvolume_equal(pa_cvolume_remap(&t, &b->channel_map, &a->channel_map), &a->absolute_volume))) + t = b->volume; + if (a->volume_valid != b->volume_valid || + (a->volume_valid && !pa_cvolume_equal(pa_cvolume_remap(&t, &b->channel_map, &a->channel_map), &a->volume))) return FALSE; return TRUE; @@ -268,7 +268,7 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3 struct userdata *u = userdata; struct entry entry, *old; char *name; - datum key, data; + pa_datum key, data; pa_assert(c); pa_assert(u); @@ -279,7 +279,7 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3 t != (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE)) return; - memset(&entry, 0, sizeof(entry)); + pa_zero(entry); entry.version = ENTRY_VERSION; if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK_INPUT) { @@ -291,24 +291,24 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3 if (!(name = get_name(sink_input->proplist, "sink-input"))) return; - entry.channel_map = sink_input->channel_map; - - if (sink_input->sink->flags & PA_SINK_FLAT_VOLUME) { - entry.absolute_volume = *pa_sink_input_get_volume(sink_input); - entry.absolute_volume_valid = sink_input->save_volume; + if ((old = read_entry(u, name))) + entry = *old; - pa_sw_cvolume_divide(&entry.relative_volume, &entry.absolute_volume, pa_sink_get_volume(sink_input->sink, FALSE)); - entry.relative_volume_valid = sink_input->save_volume; - } else { - entry.relative_volume = *pa_sink_input_get_volume(sink_input); - entry.relative_volume_valid = sink_input->save_volume; + if (sink_input->save_volume) { + entry.channel_map = sink_input->channel_map; + pa_sink_input_get_volume(sink_input, &entry.volume, FALSE); + entry.volume_valid = TRUE; } - entry.muted = pa_sink_input_get_mute(sink_input); - entry.muted_valid = sink_input->save_muted; + if (sink_input->save_muted) { + entry.muted = pa_sink_input_get_mute(sink_input); + entry.muted_valid = TRUE; + } - pa_strlcpy(entry.device, sink_input->sink->name, sizeof(entry.device)); - entry.device_valid = sink_input->save_sink; + if (sink_input->save_sink) { + pa_strlcpy(entry.device, sink_input->sink->name, sizeof(entry.device)); + entry.device_valid = TRUE; + } } else { pa_source_output *source_output; @@ -321,13 +321,16 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3 if (!(name = get_name(source_output->proplist, "source-output"))) return; - entry.channel_map = source_output->channel_map; + if ((old = read_entry(u, name))) + entry = *old; - pa_strlcpy(entry.device, source_output->source->name, sizeof(entry.device)); - entry.device_valid = source_output->save_source; + if (source_output->save_source) { + pa_strlcpy(entry.device, source_output->source->name, sizeof(entry.device)); + entry.device_valid = source_output->save_source; + } } - if ((old = read_entry(u, name))) { + if (old) { if (entries_equal(old, &entry)) { pa_xfree(old); @@ -338,15 +341,15 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3 pa_xfree(old); } - key.dptr = name; - key.dsize = (int) strlen(name); + key.data = name; + key.size = strlen(name); - data.dptr = (void*) &entry; - data.dsize = sizeof(entry); + data.data = &entry; + data.size = sizeof(entry); pa_log_info("Storing volume/mute/device for stream %s.", name); - gdbm_store(u->gdbm_file, key, data, GDBM_REPLACE); + pa_database_set(u->database, &key, &data, TRUE); pa_xfree(name); @@ -357,18 +360,18 @@ static pa_hook_result_t sink_input_new_hook_callback(pa_core *c, pa_sink_input_n char *name; struct entry *e; + pa_assert(c); pa_assert(new_data); - - if (!u->restore_device) - return PA_HOOK_OK; + pa_assert(u); + pa_assert(u->restore_device); if (!(name = get_name(new_data->proplist, "sink-input"))) return PA_HOOK_OK; if ((e = read_entry(u, name))) { - pa_sink *s; if (e->device_valid) { + pa_sink *s; if ((s = pa_namereg_get(c, e->device, PA_NAMEREG_SINK))) { if (!new_data->sink) { @@ -376,7 +379,7 @@ static pa_hook_result_t sink_input_new_hook_callback(pa_core *c, pa_sink_input_n new_data->sink = s; new_data->save_sink = TRUE; } else - pa_log_info("Not restore device for stream %s, because already set.", name); + pa_log_debug("Not restoring device for stream %s, because already set.", name); } } @@ -392,52 +395,35 @@ static pa_hook_result_t sink_input_fixate_hook_callback(pa_core *c, pa_sink_inpu char *name; struct entry *e; + pa_assert(c); pa_assert(new_data); - - if (!u->restore_volume && !u->restore_muted) - return PA_HOOK_OK; + pa_assert(u); + pa_assert(u->restore_volume || u->restore_muted); if (!(name = get_name(new_data->proplist, "sink-input"))) return PA_HOOK_OK; if ((e = read_entry(u, name))) { - if (u->restore_volume) { + if (u->restore_volume && e->volume_valid) { if (!new_data->volume_is_set) { pa_cvolume v; - pa_cvolume_init(&v); - - if (new_data->sink->flags & PA_SINK_FLAT_VOLUME) { - - /* We don't check for e->device_valid here because - that bit marks whether it is a good choice for - restoring, not just if the data is filled in. */ - if (e->absolute_volume_valid && - (e->device[0] == 0 || pa_streq(new_data->sink->name, e->device))) { - - v = e->absolute_volume; - new_data->volume_is_absolute = TRUE; - } else if (e->relative_volume_valid) { - v = e->relative_volume; - new_data->volume_is_absolute = FALSE; - } - - } else if (e->relative_volume_valid) { - v = e->relative_volume; - new_data->volume_is_absolute = FALSE; - } - if (v.channels > 0) { - pa_log_info("Restoring volume for sink input %s.", name); - pa_sink_input_new_data_set_volume(new_data, pa_cvolume_remap(&v, &e->channel_map, &new_data->channel_map)); - new_data->save_volume = TRUE; - } + pa_log_info("Restoring volume for sink input %s.", name); + + v = e->volume; + pa_cvolume_remap(&v, &e->channel_map, &new_data->channel_map); + pa_sink_input_new_data_set_volume(new_data, &v); + + new_data->volume_is_absolute = FALSE; + new_data->save_volume = TRUE; } else pa_log_debug("Not restoring volume for sink input %s, because already set.", name); } if (u->restore_muted && e->muted_valid) { + if (!new_data->muted_is_set) { pa_log_info("Restoring mute state for sink input %s.", name); pa_sink_input_new_data_set_muted(new_data, e->muted); @@ -458,10 +444,10 @@ static pa_hook_result_t source_output_new_hook_callback(pa_core *c, pa_source_ou char *name; struct entry *e; + pa_assert(c); pa_assert(new_data); - - if (!u->restore_device) - return PA_HOOK_OK; + pa_assert(u); + pa_assert(u->restore_device); if (new_data->direct_on_input) return PA_HOOK_OK; @@ -479,7 +465,7 @@ static pa_hook_result_t source_output_new_hook_callback(pa_core *c, pa_source_ou new_data->source = s; new_data->save_source = TRUE; } else - pa_log_info("Not restoring device for stream %s, because already set", name); + pa_log_debug("Not restoring device for stream %s, because already set", name); } } @@ -491,27 +477,157 @@ static pa_hook_result_t source_output_new_hook_callback(pa_core *c, pa_source_ou return PA_HOOK_OK; } -#define EXT_VERSION 1 +static pa_hook_result_t sink_put_hook_callback(pa_core *c, pa_sink *sink, struct userdata *u) { + pa_sink_input *si; + uint32_t idx; -static void clear_db(struct userdata *u) { - datum key; + pa_assert(c); + pa_assert(sink); + pa_assert(u); + pa_assert(u->on_hotplug && u->restore_device); + + PA_IDXSET_FOREACH(si, c->sink_inputs, idx) { + char *name; + struct entry *e; + + if (si->sink == sink) + continue; + + if (si->save_sink) + continue; + if (!(name = get_name(si->proplist, "sink-input"))) + continue; + + if ((e = read_entry(u, name))) { + if (e->device_valid && pa_streq(e->device, sink->name)) + pa_sink_input_move_to(si, sink, TRUE); + + pa_xfree(e); + } + + pa_xfree(name); + } + + return PA_HOOK_OK; +} + +static pa_hook_result_t source_put_hook_callback(pa_core *c, pa_source *source, struct userdata *u) { + pa_source_output *so; + uint32_t idx; + + pa_assert(c); + pa_assert(source); pa_assert(u); + pa_assert(u->on_hotplug && u->restore_device); + + PA_IDXSET_FOREACH(so, c->source_outputs, idx) { + char *name; + struct entry *e; - key = gdbm_firstkey(u->gdbm_file); - while (key.dptr) { - datum next_key; - next_key = gdbm_nextkey(u->gdbm_file, key); + if (so->source == source) + continue; - gdbm_delete(u->gdbm_file, key); - pa_xfree(key.dptr); + if (so->save_source) + continue; - key = next_key; + if (so->direct_on_input) + continue; + + if (!(name = get_name(so->proplist, "source-input"))) + continue; + + if ((e = read_entry(u, name))) { + if (e->device_valid && pa_streq(e->device, source->name)) + pa_source_output_move_to(so, source, TRUE); + + pa_xfree(e); + } + + pa_xfree(name); } - gdbm_reorganize(u->gdbm_file); + return PA_HOOK_OK; } +static pa_hook_result_t sink_unlink_hook_callback(pa_core *c, pa_sink *sink, struct userdata *u) { + pa_sink_input *si; + uint32_t idx; + + pa_assert(c); + pa_assert(sink); + pa_assert(u); + pa_assert(u->on_rescue && u->restore_device); + + /* There's no point in doing anything if the core is shut down anyway */ + if (c->state == PA_CORE_SHUTDOWN) + return PA_HOOK_OK; + + PA_IDXSET_FOREACH(si, sink->inputs, idx) { + char *name; + struct entry *e; + + if (!(name = get_name(si->proplist, "sink-input"))) + continue; + + if ((e = read_entry(u, name))) { + + if (e->device_valid) { + pa_sink *d; + + if ((d = pa_namereg_get(c, e->device, PA_NAMEREG_SINK)) && d != sink) + pa_sink_input_move_to(si, d, TRUE); + } + + pa_xfree(e); + } + + pa_xfree(name); + } + + return PA_HOOK_OK; +} + +static pa_hook_result_t source_unlink_hook_callback(pa_core *c, pa_source *source, struct userdata *u) { + pa_source_output *so; + uint32_t idx; + + pa_assert(c); + pa_assert(source); + pa_assert(u); + pa_assert(u->on_rescue && u->restore_device); + + /* There's no point in doing anything if the core is shut down anyway */ + if (c->state == PA_CORE_SHUTDOWN) + return PA_HOOK_OK; + + PA_IDXSET_FOREACH(so, source->outputs, idx) { + char *name; + struct entry *e; + + if (!(name = get_name(so->proplist, "source-output"))) + continue; + + if ((e = read_entry(u, name))) { + + if (e->device_valid) { + pa_source *d; + + if ((d = pa_namereg_get(c, e->device, PA_NAMEREG_SOURCE)) && d != source) + pa_source_output_move_to(so, d, TRUE); + } + + pa_xfree(e); + } + + pa_xfree(name); + } + + return PA_HOOK_OK; +} + +#define EXT_VERSION 1 + static void apply_entry(struct userdata *u, const char *name, struct entry *e) { pa_sink_input *si; pa_source_output *so; @@ -521,43 +637,29 @@ static void apply_entry(struct userdata *u, const char *name, struct entry *e) { pa_assert(name); pa_assert(e); - for (si = pa_idxset_first(u->core->sink_inputs, &idx); si; si = pa_idxset_next(u->core->sink_inputs, &idx)) { + PA_IDXSET_FOREACH(si, u->core->sink_inputs, idx) { char *n; pa_sink *s; if (!(n = get_name(si->proplist, "sink-input"))) continue; - if (strcmp(name, n)) { + if (!pa_streq(name, n)) { pa_xfree(n); continue; } - pa_xfree(n); + pa_xfree(n); - if (u->restore_volume) { + if (u->restore_volume && e->volume_valid) { pa_cvolume v; - pa_cvolume_init(&v); - if (si->sink->flags & PA_SINK_FLAT_VOLUME) { - - if (e->absolute_volume_valid && - (e->device[0] == 0 || pa_streq(e->device, si->sink->name))) - v = e->absolute_volume; - else if (e->relative_volume_valid) { - pa_cvolume t = *pa_sink_get_volume(si->sink, FALSE); - pa_sw_cvolume_multiply(&v, &e->relative_volume, pa_cvolume_remap(&t, &si->sink->channel_map, &e->channel_map)); - } - } else if (e->relative_volume_valid) - v = e->relative_volume; - - if (v.channels > 0) { - pa_log_info("Restoring volume for sink input %s.", name); - pa_sink_input_set_volume(si, pa_cvolume_remap(&v, &e->channel_map, &si->channel_map), TRUE); - } + v = e->volume; + pa_log_info("Restoring volume for sink input %s.", name); + pa_cvolume_remap(&v, &e->channel_map, &si->channel_map); + pa_sink_input_set_volume(si, &v, TRUE, FALSE); } - if (u->restore_muted && - e->muted_valid) { + if (u->restore_muted && e->muted_valid) { pa_log_info("Restoring mute state for sink input %s.", name); pa_sink_input_set_mute(si, e->muted, TRUE); } @@ -571,18 +673,18 @@ static void apply_entry(struct userdata *u, const char *name, struct entry *e) { } } - for (so = pa_idxset_first(u->core->source_outputs, &idx); so; so = pa_idxset_next(u->core->source_outputs, &idx)) { + PA_IDXSET_FOREACH(so, u->core->source_outputs, idx) { char *n; pa_source *s; if (!(n = get_name(so->proplist, "source-output"))) continue; - if (strcmp(name, n)) { + if (!pa_streq(name, n)) { pa_xfree(n); continue; } - pa_xfree(n); + pa_xfree(n); if (u->restore_device && e->device_valid && @@ -596,26 +698,28 @@ static void apply_entry(struct userdata *u, const char *name, struct entry *e) { #if 0 static void dump_database(struct userdata *u) { - datum key; + pa_datum key; + pa_bool_t done; + + done = !pa_database_first(u->database, &key, NULL); - key = gdbm_firstkey(u->gdbm_file); - while (key.dptr) { - datum next_key; + while (!done) { + pa_datum next_key; struct entry *e; char *name; - next_key = gdbm_nextkey(u->gdbm_file, key); + done = !pa_database_next(u->database, &key, &next_key, NULL); - name = pa_xstrndup(key.dptr, key.dsize); - pa_xfree(key.dptr); + name = pa_xstrndup(key.data, key.size); + pa_datum_free(&key); if ((e = read_entry(u, name))) { char t[256]; pa_log("name=%s", name); - pa_log("device=%s", e->device); + pa_log("device=%s %s", e->device, pa_yes_no(e->device_valid)); pa_log("channel_map=%s", pa_channel_map_snprint(t, sizeof(t), &e->channel_map)); - pa_log("volume=%s", pa_cvolume_snprint(t, sizeof(t), &e->volume)); - pa_log("mute=%s", pa_yes_no(e->muted)); + pa_log("volume=%s %s", pa_cvolume_snprint(t, sizeof(t), &e->volume), pa_yes_no(e->volume_valid)); + pa_log("mute=%s %s", pa_yes_no(e->muted), pa_yes_no(e->volume_valid)); pa_xfree(e); } @@ -655,29 +759,31 @@ static int extension_cb(pa_native_protocol *p, pa_module *m, pa_native_connectio } case SUBCOMMAND_READ: { - datum key; + pa_datum key; + pa_bool_t done; if (!pa_tagstruct_eof(t)) goto fail; - key = gdbm_firstkey(u->gdbm_file); - while (key.dptr) { - datum next_key; + done = !pa_database_first(u->database, &key, NULL); + + while (!done) { + pa_datum next_key; struct entry *e; char *name; - next_key = gdbm_nextkey(u->gdbm_file, key); + done = !pa_database_next(u->database, &key, &next_key, NULL); - name = pa_xstrndup(key.dptr, (size_t) key.dsize); - pa_xfree(key.dptr); + name = pa_xstrndup(key.data, key.size); + pa_datum_free(&key); if ((e = read_entry(u, name))) { pa_cvolume r; pa_channel_map cm; pa_tagstruct_puts(reply, name); - pa_tagstruct_put_channel_map(reply, (e->relative_volume_valid || e->absolute_volume_valid) ? &e->channel_map : pa_channel_map_init(&cm)); - pa_tagstruct_put_cvolume(reply, e->absolute_volume_valid ? &e->absolute_volume : (e->relative_volume_valid ? &e->relative_volume : pa_cvolume_init(&r))); + pa_tagstruct_put_channel_map(reply, e->volume_valid ? &e->channel_map : pa_channel_map_init(&cm)); + pa_tagstruct_put_cvolume(reply, e->volume_valid ? &e->volume : pa_cvolume_init(&r)); pa_tagstruct_puts(reply, e->device_valid ? e->device : NULL); pa_tagstruct_put_boolean(reply, e->muted_valid ? e->muted : FALSE); @@ -706,21 +812,20 @@ static int extension_cb(pa_native_protocol *p, pa_module *m, pa_native_connectio goto fail; if (mode == PA_UPDATE_SET) - clear_db(u); + pa_database_clear(u->database); while (!pa_tagstruct_eof(t)) { const char *name, *device; pa_bool_t muted; struct entry entry; - datum key, data; - int k; + pa_datum key, data; - memset(&entry, 0, sizeof(entry)); + pa_zero(entry); entry.version = ENTRY_VERSION; if (pa_tagstruct_gets(t, &name) < 0 || pa_tagstruct_get_channel_map(t, &entry.channel_map) || - pa_tagstruct_get_cvolume(t, &entry.absolute_volume) < 0 || + pa_tagstruct_get_cvolume(t, &entry.volume) < 0 || pa_tagstruct_gets(t, &device) < 0 || pa_tagstruct_get_boolean(t, &muted) < 0) goto fail; @@ -728,11 +833,10 @@ static int extension_cb(pa_native_protocol *p, pa_module *m, pa_native_connectio if (!name || !*name) goto fail; - entry.relative_volume = entry.absolute_volume; - entry.absolute_volume_valid = entry.relative_volume_valid = entry.relative_volume.channels > 0; + entry.volume_valid = entry.volume.channels > 0; - if (entry.relative_volume_valid) - if (!pa_cvolume_compatible_with_channel_map(&entry.relative_volume, &entry.channel_map)) + if (entry.volume_valid) + if (!pa_cvolume_compatible_with_channel_map(&entry.volume, &entry.channel_map)) goto fail; entry.muted = muted; @@ -746,13 +850,13 @@ static int extension_cb(pa_native_protocol *p, pa_module *m, pa_native_connectio !pa_namereg_is_valid_name(entry.device)) goto fail; - key.dptr = (void*) name; - key.dsize = (int) strlen(name); + key.data = (char*) name; + key.size = strlen(name); - data.dptr = (void*) &entry; - data.dsize = sizeof(entry); + data.data = &entry; + data.size = sizeof(entry); - if ((k = gdbm_store(u->gdbm_file, key, data, mode == PA_UPDATE_REPLACE ? GDBM_REPLACE : GDBM_INSERT)) == 0) + if (pa_database_set(u->database, &key, &data, mode == PA_UPDATE_REPLACE) == 0) if (apply_immediately) apply_entry(u, name, &entry); } @@ -766,15 +870,15 @@ static int extension_cb(pa_native_protocol *p, pa_module *m, pa_native_connectio while (!pa_tagstruct_eof(t)) { const char *name; - datum key; + pa_datum key; if (pa_tagstruct_gets(t, &name) < 0) goto fail; - key.dptr = (void*) name; - key.dsize = (int) strlen(name); + key.data = (char*) name; + key.size = strlen(name); - gdbm_delete(u->gdbm_file, key); + pa_database_unset(u->database, &key); } trigger_save(u); @@ -824,12 +928,11 @@ static pa_hook_result_t connection_unlink_hook_cb(pa_native_protocol *p, pa_nati int pa__init(pa_module*m) { pa_modargs *ma = NULL; struct userdata *u; - char *fname, *fn; + char *fname; pa_sink_input *si; pa_source_output *so; uint32_t idx; - pa_bool_t restore_device = TRUE, restore_volume = TRUE, restore_muted = TRUE; - int gdbm_cache_size; + pa_bool_t restore_device = TRUE, restore_volume = TRUE, restore_muted = TRUE, on_hotplug = TRUE, on_rescue = TRUE; pa_assert(m); @@ -840,22 +943,24 @@ int pa__init(pa_module*m) { if (pa_modargs_get_value_boolean(ma, "restore_device", &restore_device) < 0 || pa_modargs_get_value_boolean(ma, "restore_volume", &restore_volume) < 0 || - pa_modargs_get_value_boolean(ma, "restore_muted", &restore_muted) < 0) { - pa_log("restore_device=, restore_volume= and restore_muted= expect boolean arguments"); + pa_modargs_get_value_boolean(ma, "restore_muted", &restore_muted) < 0 || + pa_modargs_get_value_boolean(ma, "on_hotplug", &on_hotplug) < 0 || + pa_modargs_get_value_boolean(ma, "on_rescue", &on_rescue) < 0) { + pa_log("restore_device=, restore_volume=, restore_muted=, on_hotplug= and on_rescue= expect boolean arguments"); goto fail; } if (!restore_muted && !restore_volume && !restore_device) pa_log_warn("Neither restoring volume, nor restoring muted, nor restoring device enabled!"); - m->userdata = u = pa_xnew(struct userdata, 1); + m->userdata = u = pa_xnew0(struct userdata, 1); u->core = m->core; u->module = m; - u->save_time_event = NULL; u->restore_device = restore_device; u->restore_volume = restore_volume; u->restore_muted = restore_muted; - u->gdbm_file = NULL; + u->on_hotplug = on_hotplug; + u->on_rescue = on_rescue; u->subscribed = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func); u->protocol = pa_native_protocol_get(m->core); @@ -866,41 +971,42 @@ int pa__init(pa_module*m) { u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_SINK_INPUT|PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT, subscribe_callback, u); if (restore_device) { + /* A little bit earlier than module-intended-roles ... */ u->sink_input_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) sink_input_new_hook_callback, u); u->source_output_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) source_output_new_hook_callback, u); } - if (restore_volume || restore_muted) - u->sink_input_fixate_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_FIXATE], PA_HOOK_EARLY, (pa_hook_cb_t) sink_input_fixate_hook_callback, u); + if (restore_device && on_hotplug) { + /* A little bit earlier than module-intended-roles ... */ + u->sink_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_LATE, (pa_hook_cb_t) sink_put_hook_callback, u); + u->source_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_LATE, (pa_hook_cb_t) source_put_hook_callback, u); + } - /* We include the host identifier in the file name because gdbm - * files are CPU dependant, and we don't want things to go wrong - * if we are on a multiarch system. */ + if (restore_device && on_rescue) { + /* A little bit earlier than module-intended-roles, module-rescue-streams, ... */ + u->sink_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) sink_unlink_hook_callback, u); + u->source_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) source_unlink_hook_callback, u); + } - fn = pa_sprintf_malloc("stream-volumes."CANONICAL_HOST".gdbm"); - fname = pa_state_path(fn, TRUE); - pa_xfree(fn); + if (restore_volume || restore_muted) + u->sink_input_fixate_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_FIXATE], PA_HOOK_EARLY, (pa_hook_cb_t) sink_input_fixate_hook_callback, u); - if (!fname) + if (!(fname = pa_state_path("stream-volumes", TRUE))) goto fail; - if (!(u->gdbm_file = gdbm_open(fname, 0, GDBM_WRCREAT|GDBM_NOLOCK, 0600, NULL))) { - pa_log("Failed to open volume database '%s': %s", fname, gdbm_strerror(gdbm_errno)); + if (!(u->database = pa_database_open(fname, TRUE))) { + pa_log("Failed to open volume database '%s': %s", fname, pa_cstrerror(errno)); pa_xfree(fname); goto fail; } - /* By default the cache of gdbm is rather large, let's reduce it a bit to save memory */ - gdbm_cache_size = 10; - gdbm_setopt(u->gdbm_file, GDBM_CACHESIZE, &gdbm_cache_size, sizeof(gdbm_cache_size)); - pa_log_info("Sucessfully opened database file '%s'.", fname); pa_xfree(fname); - for (si = pa_idxset_first(m->core->sink_inputs, &idx); si; si = pa_idxset_next(m->core->sink_inputs, &idx)) + PA_IDXSET_FOREACH(si, m->core->sink_inputs, idx) subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW, si->index, u); - for (so = pa_idxset_first(m->core->source_outputs, &idx); so; so = pa_idxset_next(m->core->source_outputs, &idx)) + PA_IDXSET_FOREACH(so, m->core->source_outputs, idx) subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_NEW, so->index, u); pa_modargs_free(ma); @@ -933,14 +1039,24 @@ void pa__done(pa_module*m) { if (u->source_output_new_hook_slot) pa_hook_slot_free(u->source_output_new_hook_slot); + if (u->sink_put_hook_slot) + pa_hook_slot_free(u->sink_put_hook_slot); + if (u->source_put_hook_slot) + pa_hook_slot_free(u->source_put_hook_slot); + + if (u->sink_unlink_hook_slot) + pa_hook_slot_free(u->sink_unlink_hook_slot); + if (u->source_unlink_hook_slot) + pa_hook_slot_free(u->source_unlink_hook_slot); + if (u->connection_unlink_hook_slot) pa_hook_slot_free(u->connection_unlink_hook_slot); if (u->save_time_event) u->core->mainloop->time_free(u->save_time_event); - if (u->gdbm_file) - gdbm_close(u->gdbm_file); + if (u->database) + pa_database_close(u->database); if (u->protocol) { pa_native_protocol_remove_ext(u->protocol, m); diff --git a/src/modules/module-suspend-on-idle.c b/src/modules/module-suspend-on-idle.c index 7e17f8f7..70a7b049 100644 --- a/src/modules/module-suspend-on-idle.c +++ b/src/modules/module-suspend-on-idle.c @@ -25,8 +25,10 @@ #include <pulse/xmalloc.h> #include <pulse/timeval.h> +#include <pulse/rtclock.h> #include <pulsecore/core.h> +#include <pulsecore/core-util.h> #include <pulsecore/sink-input.h> #include <pulsecore/source-output.h> #include <pulsecore/modargs.h> @@ -74,41 +76,48 @@ struct device_info { struct userdata *userdata; pa_sink *sink; pa_source *source; - struct timeval last_use; + pa_usec_t last_use; pa_time_event *time_event; }; -static void timeout_cb(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) { +static void timeout_cb(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) { struct device_info *d = userdata; pa_assert(d); d->userdata->core->mainloop->time_restart(d->time_event, NULL); - if (d->sink && pa_sink_check_suspend(d->sink) <= 0 && pa_sink_get_state(d->sink) != PA_SINK_SUSPENDED) { + if (d->sink && pa_sink_check_suspend(d->sink) <= 0 && !(d->sink->suspend_cause & PA_SUSPEND_IDLE)) { pa_log_info("Sink %s idle for too long, suspending ...", d->sink->name); - pa_sink_suspend(d->sink, TRUE); + pa_sink_suspend(d->sink, TRUE, PA_SUSPEND_IDLE); } - if (d->source && pa_source_check_suspend(d->source) <= 0 && pa_source_get_state(d->source) != PA_SOURCE_SUSPENDED) { + if (d->source && pa_source_check_suspend(d->source) <= 0 && !(d->source->suspend_cause & PA_SUSPEND_IDLE)) { pa_log_info("Source %s idle for too long, suspending ...", d->source->name); - pa_source_suspend(d->source, TRUE); + pa_source_suspend(d->source, TRUE, PA_SUSPEND_IDLE); } } static void restart(struct device_info *d) { - struct timeval tv; + pa_usec_t now; + const char *s; + uint32_t timeout; + pa_assert(d); + pa_assert(d->sink || d->source); + + d->last_use = now = pa_rtclock_now(); + + s = pa_proplist_gets(d->sink ? d->sink->proplist : d->source->proplist, "module-suspend-on-idle.timeout"); + if (!s || pa_atou(s, &timeout) < 0) + timeout = d->userdata->timeout; - pa_gettimeofday(&tv); - d->last_use = tv; - pa_timeval_add(&tv, d->userdata->timeout*1000000); - d->userdata->core->mainloop->time_restart(d->time_event, &tv); + pa_core_rttime_restart(d->userdata->core, d->time_event, now + timeout * PA_USEC_PER_SEC); if (d->sink) - pa_log_debug("Sink %s becomes idle.", d->sink->name); + pa_log_debug("Sink %s becomes idle, timeout in %u seconds.", d->sink->name, timeout); if (d->source) - pa_log_debug("Source %s becomes idle.", d->source->name); + pa_log_debug("Source %s becomes idle, timeout in %u seconds.", d->source->name, timeout); } static void resume(struct device_info *d) { @@ -117,13 +126,13 @@ static void resume(struct device_info *d) { d->userdata->core->mainloop->time_restart(d->time_event, NULL); if (d->sink) { - pa_sink_suspend(d->sink, FALSE); + pa_sink_suspend(d->sink, FALSE, PA_SUSPEND_IDLE); pa_log_debug("Sink %s becomes busy.", d->sink->name); } if (d->source) { - pa_source_suspend(d->source, FALSE); + pa_source_suspend(d->source, FALSE, PA_SUSPEND_IDLE); pa_log_debug("Source %s becomes busy.", d->source->name); } @@ -328,7 +337,7 @@ static pa_hook_result_t device_new_hook_cb(pa_core *c, pa_object *o, struct user d->userdata = u; d->source = source ? pa_source_ref(source) : NULL; d->sink = sink ? pa_sink_ref(sink) : NULL; - d->time_event = c->mainloop->time_new(c->mainloop, NULL, timeout_cb, d); + d->time_event = pa_core_rttime_new(c, PA_USEC_INVALID, timeout_cb, d); pa_hashmap_put(u->device_infos, o, d); if ((d->sink && pa_sink_check_suspend(d->sink) <= 0) || diff --git a/src/modules/module-tunnel.c b/src/modules/module-tunnel.c index c1488841..d1153829 100644 --- a/src/modules/module-tunnel.c +++ b/src/modules/module-tunnel.c @@ -31,6 +31,7 @@ #include <stdio.h> #include <stdlib.h> +#include <pulse/rtclock.h> #include <pulse/timeval.h> #include <pulse/util.h> #include <pulse/version.h> @@ -50,7 +51,7 @@ #include <pulsecore/time-smoother.h> #include <pulsecore/thread.h> #include <pulsecore/thread-mq.h> -#include <pulsecore/rtclock.h> +#include <pulsecore/core-rtclock.h> #include <pulsecore/core-error.h> #include <pulsecore/proplist-util.h> #include <pulsecore/auth-cookie.h> @@ -64,24 +65,26 @@ #ifdef TUNNEL_SINK PA_MODULE_DESCRIPTION("Tunnel module for sinks"); PA_MODULE_USAGE( + "sink_name=<name for the local sink> " + "sink_properties=<properties for the local sink> " "server=<address> " "sink=<remote sink name> " "cookie=<filename> " "format=<sample format> " "channels=<number of channels> " "rate=<sample rate> " - "sink_name=<name for the local sink> " "channel_map=<channel map>"); #else PA_MODULE_DESCRIPTION("Tunnel module for sources"); PA_MODULE_USAGE( + "source_name=<name for the local source> " + "source_properties=<properties for the local source> " "server=<address> " "source=<remote source name> " "cookie=<filename> " "format=<sample format> " "channels=<number of channels> " "rate=<sample rate> " - "source_name=<name for the local source> " "channel_map=<channel map>"); #endif @@ -97,9 +100,11 @@ static const char* const valid_modargs[] = { "rate", #ifdef TUNNEL_SINK "sink_name", + "sink_properties", "sink", #else "source_name", + "source_properties", "source", #endif "channel_map", @@ -108,7 +113,7 @@ static const char* const valid_modargs[] = { #define DEFAULT_TIMEOUT 5 -#define LATENCY_INTERVAL 10 +#define LATENCY_INTERVAL (10*PA_USEC_PER_SEC) #define MIN_NETWORK_LATENCY_USEC (8*PA_USEC_PER_MSEC) @@ -145,6 +150,8 @@ static void command_stream_killed(pa_pdispatch *pd, uint32_t command, uint32_t t static void command_overflow_or_underflow(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata); static void command_suspended(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata); static void command_moved(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata); +static void command_stream_or_client_event(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata); +static void command_stream_buffer_attr_changed(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata); static const pa_pdispatch_cb_t command_table[PA_COMMAND_MAX] = { #ifdef TUNNEL_SINK @@ -159,7 +166,12 @@ static const pa_pdispatch_cb_t command_table[PA_COMMAND_MAX] = { [PA_COMMAND_PLAYBACK_STREAM_SUSPENDED] = command_suspended, [PA_COMMAND_RECORD_STREAM_SUSPENDED] = command_suspended, [PA_COMMAND_PLAYBACK_STREAM_MOVED] = command_moved, - [PA_COMMAND_RECORD_STREAM_MOVED] = command_moved + [PA_COMMAND_RECORD_STREAM_MOVED] = command_moved, + [PA_COMMAND_PLAYBACK_STREAM_EVENT] = command_stream_or_client_event, + [PA_COMMAND_RECORD_STREAM_EVENT] = command_stream_or_client_event, + [PA_COMMAND_CLIENT_EVENT] = command_stream_or_client_event, + [PA_COMMAND_PLAYBACK_BUFFER_ATTR_CHANGED] = command_stream_buffer_attr_changed, + [PA_COMMAND_RECORD_BUFFER_ATTR_CHANGED] = command_stream_buffer_attr_changed }; struct userdata { @@ -196,8 +208,8 @@ struct userdata { pa_bool_t remote_corked:1; pa_bool_t remote_suspended:1; - pa_usec_t transport_usec; - pa_bool_t transport_usec_valid; + pa_usec_t transport_usec; /* maintained in the main thread */ + pa_usec_t thread_transport_usec; /* maintained in the IO thread */ uint32_t ignore_latency_before; @@ -222,6 +234,11 @@ struct userdata { static void request_latency(struct userdata *u); /* Called from main context */ +static void command_stream_or_client_event(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) { + pa_log_debug("Got stream or client event."); +} + +/* Called from main context */ static void command_stream_killed(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) { struct userdata *u = userdata; @@ -261,11 +278,14 @@ static void command_suspended(pa_pdispatch *pd, uint32_t command, uint32_t tag if (pa_tagstruct_getu32(t, &channel) < 0 || pa_tagstruct_get_boolean(t, &suspended) < 0 || !pa_tagstruct_eof(t)) { - pa_log("Invalid packet"); + + pa_log("Invalid packet."); pa_module_unload_request(u->module, TRUE); return; } + pa_log_debug("Server reports device suspend."); + #ifdef TUNNEL_SINK pa_asyncmsgq_send(u->sink->asyncmsgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_REMOTE_SUSPEND, PA_UINT32_TO_PTR(!!suspended), 0, NULL); #else @@ -278,13 +298,78 @@ static void command_suspended(pa_pdispatch *pd, uint32_t command, uint32_t tag /* Called from main context */ static void command_moved(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) { struct userdata *u = userdata; + uint32_t channel, di; + const char *dn; + pa_bool_t suspended; pa_assert(pd); pa_assert(t); pa_assert(u); pa_assert(u->pdispatch == pd); + if (pa_tagstruct_getu32(t, &channel) < 0 || + pa_tagstruct_getu32(t, &di) < 0 || + pa_tagstruct_gets(t, &dn) < 0 || + pa_tagstruct_get_boolean(t, &suspended) < 0) { + + pa_log_error("Invalid packet."); + pa_module_unload_request(u->module, TRUE); + return; + } + pa_log_debug("Server reports a stream move."); + +#ifdef TUNNEL_SINK + pa_asyncmsgq_send(u->sink->asyncmsgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_REMOTE_SUSPEND, PA_UINT32_TO_PTR(!!suspended), 0, NULL); +#else + pa_asyncmsgq_send(u->source->asyncmsgq, PA_MSGOBJECT(u->source), SOURCE_MESSAGE_REMOTE_SUSPEND, PA_UINT32_TO_PTR(!!suspended), 0, NULL); +#endif + + request_latency(u); +} + +static void command_stream_buffer_attr_changed(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) { + struct userdata *u = userdata; + uint32_t channel, maxlength, tlength, fragsize, prebuf, minreq; + pa_usec_t usec; + + pa_assert(pd); + pa_assert(t); + pa_assert(u); + pa_assert(u->pdispatch == pd); + + if (pa_tagstruct_getu32(t, &channel) < 0 || + pa_tagstruct_getu32(t, &maxlength) < 0) { + + pa_log_error("Invalid packet."); + pa_module_unload_request(u->module, TRUE); + return; + } + + if (command == PA_COMMAND_RECORD_BUFFER_ATTR_CHANGED) { + if (pa_tagstruct_getu32(t, &fragsize) < 0 || + pa_tagstruct_get_usec(t, &usec) < 0) { + + pa_log_error("Invalid packet."); + pa_module_unload_request(u->module, TRUE); + return; + } + } else { + if (pa_tagstruct_getu32(t, &tlength) < 0 || + pa_tagstruct_getu32(t, &prebuf) < 0 || + pa_tagstruct_getu32(t, &minreq) < 0 || + pa_tagstruct_get_usec(t, &usec) < 0) { + + pa_log_error("Invalid packet."); + pa_module_unload_request(u->module, TRUE); + return; + } + } + +#ifdef TUNNEL_SINK + pa_log_debug("Server reports buffer attrs changed. tlength now at %lu, before %lu.", (unsigned long) tlength, (unsigned long) u->tlength); +#endif + request_latency(u); } @@ -306,26 +391,37 @@ static void command_started(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa #endif /* Called from IO thread context */ -static void stream_cork_within_thread(struct userdata *u, pa_bool_t cork) { +static void check_smoother_status(struct userdata *u, pa_bool_t past) { pa_usec_t x; + pa_assert(u); - if (u->remote_corked == cork) - return; + x = pa_rtclock_now(); - u->remote_corked = cork; - x = pa_rtclock_usec(); + /* Correct by the time the requested issued needs to travel to the + * other side. This is a valid thread-safe access, because the + * main thread is waiting for us */ - /* Correct by the time this needs to travel to the other side. - * This is a valid thread-safe access, because the main thread is - * waiting for us */ - if (u->transport_usec_valid) - x += u->transport_usec; + if (past) + x -= u->thread_transport_usec; + else + x += u->thread_transport_usec; if (u->remote_suspended || u->remote_corked) pa_smoother_pause(u->smoother, x); else - pa_smoother_resume(u->smoother, x); + pa_smoother_resume(u->smoother, x, TRUE); +} + +/* Called from IO thread context */ +static void stream_cork_within_thread(struct userdata *u, pa_bool_t cork) { + pa_assert(u); + + if (u->remote_corked == cork) + return; + + u->remote_corked = cork; + check_smoother_status(u, FALSE); } /* Called from main context */ @@ -352,26 +448,13 @@ static void stream_cork(struct userdata *u, pa_bool_t cork) { /* Called from IO thread context */ static void stream_suspend_within_thread(struct userdata *u, pa_bool_t suspend) { - pa_usec_t x; pa_assert(u); if (u->remote_suspended == suspend) return; u->remote_suspended = suspend; - - x = pa_rtclock_usec(); - - /* Correct by the time this needed to travel from the other side. - * This is a valid thread-safe access, because the main thread is - * waiting for us */ - if (u->transport_usec_valid) - x -= u->transport_usec; - - if (u->remote_suspended || u->remote_corked) - pa_smoother_pause(u->smoother, x); - else - pa_smoother_resume(u->smoother, x); + check_smoother_status(u, TRUE); } #ifdef TUNNEL_SINK @@ -418,7 +501,7 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse pa_usec_t yl, yr, *usec = data; yl = pa_bytes_to_usec((uint64_t) u->counter, &u->sink->sample_spec); - yr = pa_smoother_get(u->smoother, pa_rtclock_usec()); + yr = pa_smoother_get(u->smoother, pa_rtclock_now()); *usec = yl > yr ? yl - yr : 0; return 0; @@ -446,12 +529,15 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse y = pa_bytes_to_usec((uint64_t) u->counter, &u->sink->sample_spec); - if (y > (pa_usec_t) offset || offset < 0) + if (y > (pa_usec_t) offset) y -= (pa_usec_t) offset; else y = 0; - pa_smoother_put(u->smoother, pa_rtclock_usec(), y); + pa_smoother_put(u->smoother, pa_rtclock_now(), y); + + /* We can access this freely here, since the main thread is waiting for us */ + u->thread_transport_usec = u->transport_usec; return 0; } @@ -522,7 +608,7 @@ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off pa_usec_t yr, yl, *usec = data; yl = pa_bytes_to_usec((uint64_t) u->counter, &PA_SOURCE(o)->sample_spec); - yr = pa_smoother_get(u->smoother, pa_rtclock_usec()); + yr = pa_smoother_get(u->smoother, pa_rtclock_now()); *usec = yr > yl ? yr - yl : 0; return 0; @@ -546,13 +632,12 @@ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off pa_usec_t y; y = pa_bytes_to_usec((uint64_t) u->counter, &u->source->sample_spec); + y += (pa_usec_t) offset; - if (offset >= 0 || y > (pa_usec_t) -offset) - y += (pa_usec_t) offset; - else - y = 0; + pa_smoother_put(u->smoother, pa_rtclock_now(), y); - pa_smoother_put(u->smoother, pa_rtclock_usec(), y); + /* We can access this freely here, since the main thread is waiting for us */ + u->thread_transport_usec = u->transport_usec; return 0; } @@ -599,14 +684,13 @@ static void thread_func(void *userdata) { pa_log_debug("Thread starting up"); pa_thread_mq_install(&u->thread_mq); - pa_rtpoll_install(u->rtpoll); for (;;) { int ret; #ifdef TUNNEL_SINK if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) - if (u->sink->thread_info.rewind_requested) + if (PA_UNLIKELY(u->sink->thread_info.rewind_requested)) pa_sink_process_rewind(u->sink, 0); #endif @@ -646,7 +730,7 @@ static void command_request(pa_pdispatch *pd, uint32_t command, uint32_t tag, p } if (channel != u->channel) { - pa_log("Recieved data for invalid channel"); + pa_log("Received data for invalid channel"); goto fail; } @@ -662,7 +746,7 @@ fail: /* Called from main context */ static void stream_get_latency_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) { struct userdata *u = userdata; - pa_usec_t sink_usec, source_usec, transport_usec = 0; + pa_usec_t sink_usec, source_usec; pa_bool_t playing; int64_t write_index, read_index; struct timeval local, remote, now; @@ -709,7 +793,6 @@ static void stream_get_latency_callback(pa_pdispatch *pd, uint32_t command, uint } if (tag < u->ignore_latency_before) { - request_latency(u); return; } @@ -725,7 +808,6 @@ static void stream_get_latency_callback(pa_pdispatch *pd, uint32_t command, uint #endif } else u->transport_usec = pa_timeval_diff(&now, &local)/2; - u->transport_usec_valid = TRUE; /* First, take the device's delay */ #ifdef TUNNEL_SINK @@ -745,9 +827,9 @@ static void stream_get_latency_callback(pa_pdispatch *pd, uint32_t command, uint /* Our measurements are already out of date, hence correct by the * * transport latency */ #ifdef TUNNEL_SINK - delay -= (int64_t) transport_usec; + delay -= (int64_t) u->transport_usec; #else - delay += (int64_t) transport_usec; + delay += (int64_t) u->transport_usec; #endif /* Now correct by what we have have read/written since we requested the update */ @@ -786,8 +868,7 @@ static void request_latency(struct userdata *u) { pa_tagstruct_putu32(t, tag = u->ctag++); pa_tagstruct_putu32(t, u->channel); - pa_gettimeofday(&now); - pa_tagstruct_put_timeval(t, &now); + pa_tagstruct_put_timeval(t, pa_gettimeofday(&now)); pa_pstream_send_tagstruct(u->pstream, t); pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, stream_get_latency_callback, u, NULL); @@ -797,9 +878,8 @@ static void request_latency(struct userdata *u) { } /* Called from main context */ -static void timeout_callback(pa_mainloop_api *m, pa_time_event*e, const struct timeval *tv, void *userdata) { +static void timeout_callback(pa_mainloop_api *m, pa_time_event *e, const struct timeval *t, void *userdata) { struct userdata *u = userdata; - struct timeval ntv; pa_assert(m); pa_assert(e); @@ -807,9 +887,7 @@ static void timeout_callback(pa_mainloop_api *m, pa_time_event*e, const struct request_latency(u); - pa_gettimeofday(&ntv); - ntv.tv_sec += LATENCY_INTERVAL; - m->time_restart(e, &ntv); + pa_core_rttime_restart(u->core, e, pa_rtclock_now() + LATENCY_INTERVAL); } /* Called from main context */ @@ -861,6 +939,7 @@ static void update_description(struct userdata *u) { static void server_info_cb(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) { struct userdata *u = userdata; pa_sample_spec ss; + pa_channel_map cm; const char *server_name, *server_version, *user_name, *host_name, *default_sink_name, *default_source_name; uint32_t cookie; @@ -882,7 +961,9 @@ static void server_info_cb(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa pa_tagstruct_get_sample_spec(t, &ss) < 0 || pa_tagstruct_gets(t, &default_sink_name) < 0 || pa_tagstruct_gets(t, &default_source_name) < 0 || - pa_tagstruct_getu32(t, &cookie) < 0) { + pa_tagstruct_getu32(t, &cookie) < 0 || + (u->version >= 15 && + pa_tagstruct_get_channel_map(t, &cm) < 0)) { pa_log("Parse failure"); goto fail; @@ -963,6 +1044,20 @@ static void sink_info_cb(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_t } } + if (u->version >= 15) { + pa_volume_t base_volume; + uint32_t state, n_volume_steps, card; + + if (pa_tagstruct_get_volume(t, &base_volume) < 0 || + pa_tagstruct_getu32(t, &state) < 0 || + pa_tagstruct_getu32(t, &n_volume_steps) < 0 || + pa_tagstruct_getu32(t, &card) < 0) { + + pa_log("Parse failure"); + goto fail; + } + } + if (!pa_tagstruct_eof(t)) { pa_log("Packet too long"); goto fail; @@ -1059,12 +1154,11 @@ static void sink_input_info_cb(pa_pdispatch *pd, uint32_t command, uint32_t tag pa_cvolume_equal(&volume, &u->sink->virtual_volume)) return; - memcpy(&u->sink->virtual_volume, &volume, sizeof(pa_cvolume)); + pa_sink_volume_changed(u->sink, &volume, FALSE); if (u->version >= 11) - u->sink->muted = !!mute; + pa_sink_mute_changed(u->sink, mute, FALSE); - pa_subscription_post(u->sink->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, u->sink->index); return; fail: @@ -1126,6 +1220,20 @@ static void source_info_cb(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa } } + if (u->version >= 15) { + pa_volume_t base_volume; + uint32_t state, n_volume_steps, card; + + if (pa_tagstruct_get_volume(t, &base_volume) < 0 || + pa_tagstruct_getu32(t, &state) < 0 || + pa_tagstruct_getu32(t, &n_volume_steps) < 0 || + pa_tagstruct_getu32(t, &card) < 0) { + + pa_log("Parse failure"); + goto fail; + } + } + if (!pa_tagstruct_eof(t)) { pa_log("Packet too long"); goto fail; @@ -1246,7 +1354,6 @@ static void start_subscribe(struct userdata *u) { /* Called from main context */ static void create_stream_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) { struct userdata *u = userdata; - struct timeval ntv; #ifdef TUNNEL_SINK uint32_t bytes; #endif @@ -1314,11 +1421,11 @@ static void create_stream_callback(pa_pdispatch *pd, uint32_t command, uint32_t if (pa_tagstruct_get_usec(t, &usec) < 0) goto parse_error; -#ifdef TUNNEL_SINK - pa_sink_set_latency_range(u->sink, usec + MIN_NETWORK_LATENCY_USEC, 0); -#else - pa_source_set_latency_range(u->source, usec + MIN_NETWORK_LATENCY_USEC, 0); -#endif +/* #ifdef TUNNEL_SINK */ +/* pa_sink_set_latency_range(u->sink, usec + MIN_NETWORK_LATENCY_USEC, 0); */ +/* #else */ +/* pa_source_set_latency_range(u->source, usec + MIN_NETWORK_LATENCY_USEC, 0); */ +/* #endif */ } if (!pa_tagstruct_eof(t)) @@ -1328,9 +1435,7 @@ static void create_stream_callback(pa_pdispatch *pd, uint32_t command, uint32_t request_info(u); pa_assert(!u->time_event); - pa_gettimeofday(&ntv); - ntv.tv_sec += LATENCY_INTERVAL; - u->time_event = u->core->mainloop->time_new(u->core->mainloop, &ntv, timeout_callback, u); + u->time_event = pa_core_rttime_new(u->core, pa_rtclock_now() + LATENCY_INTERVAL, timeout_callback, u); request_latency(u); @@ -1391,11 +1496,17 @@ static void setup_complete_callback(pa_pdispatch *pd, uint32_t command, uint32_t pa_log_debug("Protocol version: remote %u, local %u", u->version, PA_PROTOCOL_VERSION); #ifdef TUNNEL_SINK + pa_proplist_setf(u->sink->proplist, "tunnel.remote_version", "%u", u->version); + pa_sink_update_proplist(u->sink, 0, NULL); + pa_snprintf(name, sizeof(name), "%s for %s@%s", u->sink_name, pa_get_user_name(un, sizeof(un)), pa_get_host_name(hn, sizeof(hn))); #else + pa_proplist_setf(u->source->proplist, "tunnel.remote_version", "%u", u->version); + pa_source_update_proplist(u->source, 0, NULL); + pa_snprintf(name, sizeof(name), "%s for %s@%s", u->source_name, pa_get_user_name(un, sizeof(un)), @@ -1409,9 +1520,9 @@ static void setup_complete_callback(pa_pdispatch *pd, uint32_t command, uint32_t if (u->version >= 13) { pa_proplist *pl; pl = pa_proplist_new(); - pa_init_proplist(pl); pa_proplist_sets(pl, PA_PROP_APPLICATION_ID, "org.PulseAudio.PulseAudio"); pa_proplist_sets(pl, PA_PROP_APPLICATION_VERSION, PACKAGE_VERSION); + pa_init_proplist(pl); pa_tagstruct_put_proplist(reply, pl); pa_proplist_free(pl); } else @@ -1503,6 +1614,14 @@ static void setup_complete_callback(pa_pdispatch *pd, uint32_t command, uint32_t pa_tagstruct_put_boolean(reply, TRUE); /* early rquests */ } + if (u->version >= 15) { +#ifdef TUNNEL_SINK + pa_tagstruct_put_boolean(reply, FALSE); /* muted_set */ +#endif + pa_tagstruct_put_boolean(reply, FALSE); /* don't inhibit auto suspend */ + pa_tagstruct_put_boolean(reply, FALSE); /* fail on suspend */ + } + pa_pstream_send_tagstruct(u->pstream, reply); pa_pdispatch_register_reply(u->pdispatch, tag, DEFAULT_TIMEOUT, create_stream_callback, u, NULL); @@ -1550,7 +1669,7 @@ static void pstream_memblock_callback(pa_pstream *p, uint32_t channel, int64_t o pa_assert(u); if (channel != u->channel) { - pa_log("Recieved memory block on bad channel."); + pa_log("Received memory block on bad channel."); pa_module_unload_request(u->module, TRUE); return; } @@ -1559,7 +1678,6 @@ static void pstream_memblock_callback(pa_pstream *p, uint32_t channel, int64_t o u->counter_delta += (int64_t) chunk->length; } - #endif /* Called from main context */ @@ -1582,7 +1700,7 @@ static void on_connection(pa_socket_client *sc, pa_iochannel *io, void *userdata } u->pstream = pa_pstream_new(u->core->mainloop, io, u->core->mempool); - u->pdispatch = pa_pdispatch_new(u->core->mainloop, command_table, PA_COMMAND_MAX); + u->pdispatch = pa_pdispatch_new(u->core->mainloop, TRUE, command_table, PA_COMMAND_MAX); pa_pstream_set_die_callback(u->pstream, pstream_die_callback, u); pa_pstream_set_recieve_packet_callback(u->pstream, pstream_packet_callback, u); @@ -1695,13 +1813,19 @@ int pa__init(pa_module*m) { u->source_name = pa_xstrdup(pa_modargs_get_value(ma, "source", NULL));; u->source = NULL; #endif - u->smoother = pa_smoother_new(PA_USEC_PER_SEC, PA_USEC_PER_SEC*2, TRUE, 10); + u->smoother = pa_smoother_new( + PA_USEC_PER_SEC, + PA_USEC_PER_SEC*2, + TRUE, + TRUE, + 10, + pa_rtclock_now(), + FALSE); u->ctag = 1; u->device_index = u->channel = PA_INVALID_INDEX; u->time_event = NULL; u->ignore_latency_before = 0; - u->transport_usec = 0; - u->transport_usec_valid = FALSE; + u->transport_usec = u->thread_transport_usec = 0; u->remote_suspended = u->remote_corked = FALSE; u->counter = u->counter_delta = 0; @@ -1723,7 +1847,7 @@ int pa__init(pa_module*m) { goto fail; } - if (!(u->client = pa_socket_client_new_string(m->core->mainloop, u->server_name, PA_NATIVE_DEFAULT_PORT))) { + if (!(u->client = pa_socket_client_new_string(m->core->mainloop, TRUE, u->server_name, PA_NATIVE_DEFAULT_PORT))) { pa_log("Failed to connect to server '%s'", u->server_name); goto fail; } @@ -1747,7 +1871,13 @@ int pa__init(pa_module*m) { if (u->sink_name) pa_proplist_sets(data.proplist, "tunnel.remote.sink", u->sink_name); - u->sink = pa_sink_new(m->core, &data, PA_SINK_NETWORK|PA_SINK_LATENCY|PA_SINK_HW_VOLUME_CTRL); + if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) { + pa_log("Invalid properties"); + pa_sink_new_data_done(&data); + goto fail; + } + + u->sink = pa_sink_new(m->core, &data, PA_SINK_NETWORK|PA_SINK_LATENCY|PA_SINK_HW_VOLUME_CTRL|PA_SINK_HW_MUTE_CTRL); pa_sink_new_data_done(&data); if (!u->sink) { @@ -1763,7 +1893,7 @@ int pa__init(pa_module*m) { u->sink->refresh_volume = u->sink->refresh_muted = FALSE; - pa_sink_set_latency_range(u->sink, MIN_NETWORK_LATENCY_USEC, 0); +/* pa_sink_set_latency_range(u->sink, MIN_NETWORK_LATENCY_USEC, 0); */ pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq); pa_sink_set_rtpoll(u->sink, u->rtpoll); @@ -1785,6 +1915,12 @@ int pa__init(pa_module*m) { if (u->source_name) pa_proplist_sets(data.proplist, "tunnel.remote.source", u->source_name); + if (pa_modargs_get_proplist(ma, "source_properties", data.proplist, PA_UPDATE_REPLACE) < 0) { + pa_log("Invalid properties"); + pa_source_new_data_done(&data); + goto fail; + } + u->source = pa_source_new(m->core, &data, PA_SOURCE_NETWORK|PA_SOURCE_LATENCY); pa_source_new_data_done(&data); @@ -1797,7 +1933,7 @@ int pa__init(pa_module*m) { u->source->set_state = source_set_state; u->source->userdata = u; - pa_source_set_latency_range(u->source, MIN_NETWORK_LATENCY_USEC, 0); +/* pa_source_set_latency_range(u->source, MIN_NETWORK_LATENCY_USEC, 0); */ pa_source_set_asyncmsgq(u->source, u->thread_mq.inq); pa_source_set_rtpoll(u->source, u->rtpoll); @@ -1807,15 +1943,13 @@ int pa__init(pa_module*m) { u->time_event = NULL; - u->maxlength = 0; + u->maxlength = (uint32_t) -1; #ifdef TUNNEL_SINK - u->tlength = u->minreq = u->prebuf = 0; + u->tlength = u->minreq = u->prebuf = (uint32_t) -1; #else - u->fragsize = 0; + u->fragsize = (uint32_t) -1; #endif - pa_smoother_set_time_offset(u->smoother, pa_rtclock_usec()); - if (!(u->thread = pa_thread_new(thread_func, u))) { pa_log("Failed to create thread."); goto fail; diff --git a/src/modules/module-udev-detect.c b/src/modules/module-udev-detect.c new file mode 100644 index 00000000..1ad6fa2d --- /dev/null +++ b/src/modules/module-udev-detect.c @@ -0,0 +1,457 @@ +/*** + This file is part of PulseAudio. + + Copyright 2009 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.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 along with PulseAudio; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <errno.h> +#include <limits.h> +#include <sys/inotify.h> +#include <libudev.h> + +#include <pulsecore/modargs.h> +#include <pulsecore/core-error.h> +#include <pulsecore/core-util.h> +#include <pulsecore/namereg.h> + +#include "module-udev-detect-symdef.h" + +PA_MODULE_AUTHOR("Lennart Poettering"); +PA_MODULE_DESCRIPTION("Detect available audio hardware and load matching drivers"); +PA_MODULE_VERSION(PACKAGE_VERSION); +PA_MODULE_LOAD_ONCE(TRUE); + +struct device { + char *path; + pa_bool_t accessible; + char *card_name; + uint32_t module; +}; + +struct userdata { + pa_core *core; + pa_hashmap *devices; + pa_bool_t use_tsched; + + struct udev* udev; + struct udev_monitor *monitor; + pa_io_event *udev_io; + + int inotify_fd; + pa_io_event *inotify_io; +}; + +static const char* const valid_modargs[] = { + "tsched", + NULL +}; + +static void device_free(struct device *d) { + pa_assert(d); + + pa_xfree(d->path); + pa_xfree(d->card_name); + pa_xfree(d); +} + +static const char *path_get_card_id(const char *path) { + const char *e; + + if (!path) + return NULL; + + if (!(e = strrchr(path, '/'))) + return NULL; + + if (!pa_startswith(e, "/card")) + return NULL; + + return e + 5; +} + +static void verify_access(struct userdata *u, struct device *d) { + char *cd; + pa_card *card; + + pa_assert(u); + pa_assert(d); + + if (!(card = pa_namereg_get(u->core, d->card_name, PA_NAMEREG_CARD))) + return; + + cd = pa_sprintf_malloc("%s/snd/controlC%s", udev_get_dev_path(u->udev), path_get_card_id(d->path)); + d->accessible = access(cd, W_OK) >= 0; + pa_log_info("%s is accessible: %s", cd, pa_yes_no(d->accessible)); + pa_xfree(cd); + + pa_card_suspend(card, !d->accessible, PA_SUSPEND_SESSION); +} + +static void card_changed(struct userdata *u, struct udev_device *dev) { + struct device *d; + const char *path; + const char *t; + char *card_name, *args; + pa_module *m; + char *n; + + pa_assert(u); + pa_assert(dev); + + path = udev_device_get_devpath(dev); + + if ((d = pa_hashmap_get(u->devices, path))) { + verify_access(u, d); + return; + } + + if (!(t = udev_device_get_property_value(dev, "PULSE_NAME"))) + if (!(t = udev_device_get_property_value(dev, "ID_ID"))) + if (!(t = udev_device_get_property_value(dev, "ID_PATH"))) + t = path_get_card_id(path); + + n = pa_namereg_make_valid_name(t); + + card_name = pa_sprintf_malloc("alsa_card.%s", n); + args = pa_sprintf_malloc("device_id=\"%s\" " + "name=\"%s\" " + "card_name=\"%s\" " + "tsched=%i " + "card_properties=\"module-udev-detect.discovered=1\"", + path_get_card_id(path), + n, + card_name, + (int) u->use_tsched); + + pa_log_debug("Loading module-alsa-card with arguments '%s'", args); + m = pa_module_load(u->core, "module-alsa-card", args); + pa_xfree(args); + + if (m) { + pa_log_info("Card %s (%s) added.", path, n); + + d = pa_xnew(struct device, 1); + d->path = pa_xstrdup(path); + d->card_name = card_name; + d->module = m->index; + d->accessible = TRUE; + + pa_hashmap_put(u->devices, d->path, d); + } else + pa_xfree(card_name); + + pa_xfree(n); +} + +static void remove_card(struct userdata *u, struct udev_device *dev) { + struct device *d; + + pa_assert(u); + pa_assert(dev); + + if (!(d = pa_hashmap_remove(u->devices, udev_device_get_devpath(dev)))) + return; + + pa_log_info("Card %s removed.", d->path); + pa_module_unload_request_by_index(u->core, d->module, TRUE); + device_free(d); +} + +static void process_device(struct userdata *u, struct udev_device *dev) { + const char *action, *ff; + + pa_assert(u); + pa_assert(dev); + + if (udev_device_get_property_value(dev, "PULSE_IGNORE")) { + pa_log_debug("Ignoring %s, because marked so.", udev_device_get_devpath(dev)); + return; + } + + if ((ff = udev_device_get_property_value(dev, "SOUND_FORM_FACTOR")) && + pa_streq(ff, "modem")) { + pa_log_debug("Ignoring %s, because it is a modem.", udev_device_get_devpath(dev)); + return; + } + + action = udev_device_get_action(dev); + + if (action && pa_streq(action, "remove")) + remove_card(u, dev); + else if ((!action || pa_streq(action, "change")) && + udev_device_get_property_value(dev, "SOUND_INITIALIZED")) + card_changed(u, dev); + + /* For an explanation why we don't look for 'add' events here + * have a look into /lib/udev/rules.d/78-sound-card.rules! */ +} + +static void process_path(struct userdata *u, const char *path) { + struct udev_device *dev; + + if (!path_get_card_id(path)) + return; + + if (!(dev = udev_device_new_from_syspath(u->udev, path))) { + pa_log("Failed to get udev device object from udev."); + return; + } + + process_device(u, dev); + udev_device_unref(dev); +} + +static void monitor_cb( + pa_mainloop_api*a, + pa_io_event* e, + int fd, + pa_io_event_flags_t events, + void *userdata) { + + struct userdata *u = userdata; + struct udev_device *dev; + + pa_assert(a); + + if (!(dev = udev_monitor_receive_device(u->monitor))) { + pa_log("Failed to get udev device object from monitor."); + goto fail; + } + + if (!path_get_card_id(udev_device_get_devpath(dev))) + return; + + process_device(u, dev); + udev_device_unref(dev); + return; + +fail: + a->io_free(u->udev_io); + u->udev_io = NULL; +} + +static void inotify_cb( + pa_mainloop_api*a, + pa_io_event* e, + int fd, + pa_io_event_flags_t events, + void *userdata) { + + struct { + struct inotify_event e; + char name[NAME_MAX]; + } buf; + struct userdata *u = userdata; + static int type = 0; + pa_bool_t verify = FALSE; + + for (;;) { + ssize_t r; + + pa_zero(buf); + if ((r = pa_read(fd, &buf, sizeof(buf), &type)) <= 0) { + + if (r < 0 && errno == EAGAIN) + break; + + pa_log("read() from inotify failed: %s", r < 0 ? pa_cstrerror(errno) : "EOF"); + goto fail; + } + + if ((buf.e.mask & IN_CLOSE_WRITE) && pa_startswith(buf.e.name, "pcmC")) + verify = TRUE; + } + + if (verify) { + struct device *d; + void *state; + + pa_log_debug("Verifying access."); + + PA_HASHMAP_FOREACH(d, u->devices, state) + verify_access(u, d); + } + + return; + +fail: + a->io_free(u->inotify_io); + u->inotify_io = NULL; + + if (u->inotify_fd >= 0) { + pa_close(u->inotify_fd); + u->inotify_fd = -1; + } +} + +static int setup_inotify(struct userdata *u) { + char *dev_snd; + int r; + + if ((u->inotify_fd = inotify_init1(IN_CLOEXEC|IN_NONBLOCK)) < 0) { + pa_log("inotify_init1() failed: %s", pa_cstrerror(errno)); + return -1; + } + + dev_snd = pa_sprintf_malloc("%s/snd", udev_get_dev_path(u->udev)); + r = inotify_add_watch(u->inotify_fd, dev_snd, IN_CLOSE_WRITE); + pa_xfree(dev_snd); + + if (r < 0) { + pa_log("inotify_add_watch() failed: %s", pa_cstrerror(errno)); + return -1; + } + + pa_assert_se(u->inotify_io = u->core->mainloop->io_new(u->core->mainloop, u->inotify_fd, PA_IO_EVENT_INPUT, inotify_cb, u)); + + return 0; +} + +int pa__init(pa_module *m) { + struct userdata *u = NULL; + pa_modargs *ma; + struct udev_enumerate *enumerate = NULL; + struct udev_list_entry *item = NULL, *first = NULL; + int fd; + + pa_assert(m); + + if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { + pa_log("Failed to parse module arguments"); + goto fail; + } + + m->userdata = u = pa_xnew0(struct userdata, 1); + u->core = m->core; + u->devices = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func); + u->use_tsched = TRUE; + u->inotify_fd = -1; + + if (pa_modargs_get_value_boolean(ma, "tsched", &u->use_tsched) < 0) { + pa_log("Failed to parse tsched argument."); + goto fail; + } + + if (!(u->udev = udev_new())) { + pa_log("Failed to initialize udev library."); + goto fail; + } + + if (setup_inotify(u) < 0) + goto fail; + + if (!(u->monitor = udev_monitor_new_from_netlink(u->udev, "udev"))) { + pa_log("Failed to initialize monitor."); + goto fail; + } + + errno = 0; + if (udev_monitor_enable_receiving(u->monitor) < 0) { + pa_log("Failed to enable monitor: %s", pa_cstrerror(errno)); + if (errno == EPERM) + pa_log_info("Most likely your kernel is simply too old and " + "allows only priviliged processes to listen to device events. " + "Please upgrade your kernel to at least 2.6.30."); + goto fail; + } + + if ((fd = udev_monitor_get_fd(u->monitor)) < 0) { + pa_log("Failed to get udev monitor fd."); + goto fail; + } + + pa_assert_se(u->udev_io = u->core->mainloop->io_new(u->core->mainloop, fd, PA_IO_EVENT_INPUT, monitor_cb, u)); + + if (!(enumerate = udev_enumerate_new(u->udev))) { + pa_log("Failed to initialize udev enumerator."); + goto fail; + } + + if (udev_enumerate_add_match_subsystem(enumerate, "sound") < 0) { + pa_log("Failed to match to subsystem."); + goto fail; + } + + if (udev_enumerate_scan_devices(enumerate) < 0) { + pa_log("Failed to scan for devices."); + goto fail; + } + + first = udev_enumerate_get_list_entry(enumerate); + udev_list_entry_foreach(item, first) + process_path(u, udev_list_entry_get_name(item)); + + udev_enumerate_unref(enumerate); + + pa_log_info("Loaded %u modules.", pa_hashmap_size(u->devices)); + + pa_modargs_free(ma); + + return 0; + +fail: + + if (enumerate) + udev_enumerate_unref(enumerate); + + if (ma) + pa_modargs_free(ma); + + pa__done(m); + + return -1; +} + +void pa__done(pa_module *m) { + struct userdata *u; + + pa_assert(m); + + if (!(u = m->userdata)) + return; + + if (u->udev_io) + m->core->mainloop->io_free(u->udev_io); + + if (u->monitor) + udev_monitor_unref(u->monitor); + + if (u->udev) + udev_unref(u->udev); + + if (u->inotify_io) + m->core->mainloop->io_free(u->inotify_io); + + if (u->inotify_fd >= 0) + pa_close(u->inotify_fd); + + if (u->devices) { + struct device *d; + + while ((d = pa_hashmap_steal_first(u->devices))) + device_free(d); + + pa_hashmap_free(u->devices, NULL, NULL); + } + + pa_xfree(u); +} diff --git a/src/modules/module-volume-restore.c b/src/modules/module-volume-restore.c index 61858afa..91da598e 100644 --- a/src/modules/module-volume-restore.c +++ b/src/modules/module-volume-restore.c @@ -36,6 +36,7 @@ PA_MODULE_AUTHOR("Lennart Poettering"); PA_MODULE_DESCRIPTION("Compatibility module"); PA_MODULE_VERSION(PACKAGE_VERSION); PA_MODULE_LOAD_ONCE(TRUE); +PA_MODULE_DEPRECATED("Please use module-stream-restore instead of module-volume-restore!"); static const char* const valid_modargs[] = { "table", @@ -62,7 +63,7 @@ int pa__init(pa_module*m) { goto fail; } - pa_log_warn("module-volume-restore is obsolete. It has been replaced by module-stream-restore. We will now load the latter but please make sure to remove module-volume-restore from your configuration."); + pa_log_warn("We will now load module-stream-restore. Please make sure to remove module-volume-restore from your configuration."); t = pa_sprintf_malloc("restore_volume=%s restore_device=%s", pa_yes_no(restore_volume), pa_yes_no(restore_device)); pa_module_load(m->core, "module-stream-restore", t); diff --git a/src/modules/module-waveout.c b/src/modules/module-waveout.c index 2d35828d..d1b9f2ff 100644 --- a/src/modules/module-waveout.c +++ b/src/modules/module-waveout.c @@ -256,7 +256,7 @@ static void poll_cb(pa_mainloop_api*a, pa_time_event *e, const struct timeval *t pa_gettimeofday(&ntv); pa_timeval_add(&ntv, u->poll_timeout); - a->time_restart(e, &ntv); + a->rtclock_time_restart(e, &ntv); } static void defer_cb(pa_mainloop_api*a, pa_defer_event *e, void *userdata) { @@ -549,7 +549,7 @@ int pa__init(pa_core *c, pa_module*m) { pa_gettimeofday(&tv); pa_timeval_add(&tv, u->poll_timeout); - u->event = c->mainloop->time_new(c->mainloop, &tv, poll_cb, u); + u->event = c->mainloop->rtclock_time_new(c->mainloop, &tv, poll_cb, u); assert(u->event); u->defer = c->mainloop->defer_new(c->mainloop, defer_cb, u); diff --git a/src/modules/module-zeroconf-discover.c b/src/modules/module-zeroconf-discover.c index 3da946e0..1fdc1f46 100644 --- a/src/modules/module-zeroconf-discover.c +++ b/src/modules/module-zeroconf-discover.c @@ -279,7 +279,7 @@ static void browser_cb( pa_log("avahi_service_resolver_new() failed: %s", avahi_strerror(avahi_client_errno(u->client))); /* We ignore the returned resolver object here, since the we don't - * need to attach any special data to it, and we can still destory + * need to attach any special data to it, and we can still destroy * it from the callback */ } else if (event == AVAHI_BROWSER_REMOVE) { diff --git a/src/modules/module-zeroconf-publish.c b/src/modules/module-zeroconf-publish.c index 692ffe91..d72d2647 100644 --- a/src/modules/module-zeroconf-publish.c +++ b/src/modules/module-zeroconf-publish.c @@ -37,6 +37,7 @@ #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> @@ -47,6 +48,7 @@ #include <pulsecore/modargs.h> #include <pulsecore/avahi-wrap.h> #include <pulsecore/endianmacros.h> +#include <pulsecore/protocol-native.h> #include "module-zeroconf-publish-symdef.h" @@ -54,7 +56,6 @@ PA_MODULE_AUTHOR("Lennart Poettering"); PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Publisher"); PA_MODULE_VERSION(PACKAGE_VERSION); PA_MODULE_LOAD_ONCE(TRUE); -PA_MODULE_USAGE("port=<IP port number>"); #define SERVICE_TYPE_SINK "_pulse-sink._tcp" #define SERVICE_TYPE_SOURCE "_pulse-source._tcp" @@ -67,7 +68,6 @@ PA_MODULE_USAGE("port=<IP port number>"); #define SERVICE_SUBTYPE_SOURCE_NON_MONITOR "_non-monitor._sub."SERVICE_TYPE_SOURCE static const char* const valid_modargs[] = { - "port", NULL }; @@ -88,6 +88,7 @@ struct service { struct userdata { pa_core *core; pa_module *module; + AvahiPoll *avahi_poll; AvahiClient *client; @@ -96,15 +97,15 @@ struct userdata { AvahiEntryGroup *main_entry_group; - uint16_t port; - pa_hook_slot *sink_new_slot, *source_new_slot, *sink_unlink_slot, *source_unlink_slot, *sink_changed_slot, *source_changed_slot; + + pa_native_protocol *native; }; -static void get_service_data(struct service *s, pa_sample_spec *ret_ss, pa_channel_map *ret_map, const char **ret_name, const char **ret_description, enum service_subtype *ret_subtype) { +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_description); + pa_assert(ret_proplist); pa_assert(ret_subtype); if (pa_sink_isinstance(s->device)) { @@ -113,7 +114,7 @@ static void get_service_data(struct service *s, pa_sample_spec *ret_ss, pa_chann *ret_ss = sink->sample_spec; *ret_map = sink->channel_map; *ret_name = sink->name; - *ret_description = pa_strnull(pa_proplist_gets(sink->proplist, PA_PROP_DEVICE_DESCRIPTION)); + *ret_proplist = sink->proplist; *ret_subtype = sink->flags & PA_SINK_HARDWARE ? SUBTYPE_HARDWARE : SUBTYPE_VIRTUAL; } else if (pa_source_isinstance(s->device)) { @@ -122,7 +123,7 @@ static void get_service_data(struct service *s, pa_sample_spec *ret_ss, pa_chann *ret_ss = source->sample_spec; *ret_map = source->channel_map; *ret_name = source->name; - *ret_description = pa_strnull(pa_proplist_gets(source->proplist, PA_PROP_DEVICE_DESCRIPTION)); + *ret_proplist = source->proplist; *ret_subtype = source->monitor_of ? SUBTYPE_MONITOR : (source->flags & PA_SOURCE_HARDWARE ? SUBTYPE_HARDWARE : SUBTYPE_VIRTUAL); } else @@ -131,11 +132,24 @@ static void get_service_data(struct service *s, pa_sample_spec *ret_ss, pa_chann 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); - l = avahi_string_list_add_pair(l, "user-name", pa_get_user_name(s, sizeof(s))); + + 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); + 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); @@ -184,10 +198,35 @@ static void service_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupStat static void service_free(struct service *s); +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; +} + static int publish_service(struct service *s) { int r = -1; AvahiStringList *txt = NULL; - const char *description = NULL, *name = 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]; @@ -214,7 +253,7 @@ static int publish_service(struct service *s) { txt = txt_record_server_data(s->userdata->core, txt); - get_service_data(s, &ss, &map, &name, &description, &subtype); + 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); @@ -222,6 +261,19 @@ static int publish_service(struct service *s) { 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, @@ -230,7 +282,7 @@ static int publish_service(struct service *s) { pa_sink_isinstance(s->device) ? SERVICE_TYPE_SINK : SERVICE_TYPE_SOURCE, NULL, NULL, - s->userdata->port, + compute_port(s->userdata), txt) < 0) { pa_log("avahi_entry_group_add_service_strlst(): %s", avahi_strerror(avahi_client_errno(s->userdata->client))); @@ -287,7 +339,7 @@ finish: static struct service *get_service(struct userdata *u, pa_object *device) { struct service *s; - char hn[64], un[64]; + char *hn, *un; const char *n; pa_assert(u); @@ -309,11 +361,13 @@ static struct service *get_service(struct userdata *u, pa_object *device) { n = PA_SOURCE(device)->name; } - s->service_name = pa_truncate_utf8(pa_sprintf_malloc("%s@%s: %s", - pa_get_user_name(un, sizeof(un)), - pa_get_host_name(hn, sizeof(hn)), - n), - AVAHI_LABEL_MAX-1); + 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); @@ -430,7 +484,7 @@ static int publish_main_service(struct userdata *u) { SERVICE_TYPE_SERVER, NULL, NULL, - u->port, + compute_port(u), txt) < 0) { pa_log("avahi_entry_group_add_service_strlst() failed: %s", avahi_strerror(avahi_client_errno(u->client))); @@ -552,9 +606,8 @@ static void client_callback(AvahiClient *c, AvahiClientState state, void *userda int pa__init(pa_module*m) { struct userdata *u; - uint32_t port = PA_NATIVE_DEFAULT_PORT; pa_modargs *ma = NULL; - char hn[256], un[256]; + char *hn, *un; int error; if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { @@ -562,15 +615,10 @@ int pa__init(pa_module*m) { goto fail; } - if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port <= 0 || port > 0xFFFF) { - pa_log("Invalid port specified."); - goto fail; - } - m->userdata = u = pa_xnew(struct userdata, 1); u->core = m->core; u->module = m; - u->port = (uint16_t) port; + u->native = pa_native_protocol_get(u->core); u->avahi_poll = pa_avahi_poll_new(m->core->mainloop); @@ -585,7 +633,11 @@ int pa__init(pa_module*m) { u->main_entry_group = NULL; - u->service_name = pa_truncate_utf8(pa_sprintf_malloc("%s@%s", pa_get_user_name(un, sizeof(un)), pa_get_host_name(hn, sizeof(hn))), AVAHI_LABEL_MAX); + 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 (!(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)); @@ -643,6 +695,9 @@ void pa__done(pa_module*m) { 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); } diff --git a/src/modules/oss/module-oss.c b/src/modules/oss/module-oss.c index 7bce8d00..c44b882b 100644 --- a/src/modules/oss/module-oss.c +++ b/src/modules/oss/module-oss.c @@ -85,17 +85,22 @@ PA_MODULE_VERSION(PACKAGE_VERSION); PA_MODULE_LOAD_ONCE(FALSE); PA_MODULE_USAGE( "sink_name=<name for the sink> " + "sink_properties=<properties for the sink> " "source_name=<name for the source> " + "source_properties=<properties for the source> " "device=<OSS device> " "record=<enable source?> " "playback=<enable sink?> " "format=<sample format> " - "channels=<number of channels> " "rate=<sample rate> " + "channels=<number of channels> " + "channel_map=<channel map> " "fragments=<number of fragments> " "fragment_size=<fragment size> " - "channel_map=<channel map> " "mmap=<enable memory mapping?>"); +#ifdef __linux__ +PA_MODULE_DEPRECATED("Please use module-alsa-card instead of module-oss!"); +#endif #define DEFAULT_DEVICE "/dev/dsp" @@ -140,7 +145,9 @@ struct userdata { static const char* const valid_modargs[] = { "sink_name", + "sink_properties", "source_name", + "source_properties", "device", "record", "playback", @@ -477,6 +484,7 @@ static void build_pollfd(struct userdata *u) { pollfd->revents = 0; } +/* Called from IO context */ static int suspend(struct userdata *u) { pa_assert(u); pa_assert(u->fd >= 0); @@ -526,6 +534,7 @@ static int suspend(struct userdata *u) { return 0; } +/* Called from IO context */ static int unsuspend(struct userdata *u) { int m; pa_sample_spec ss, *ss_original; @@ -616,10 +625,10 @@ static int unsuspend(struct userdata *u) { build_pollfd(u); - if (u->sink) - pa_sink_get_volume(u->sink, TRUE); - if (u->source) - pa_source_get_volume(u->source, TRUE); + if (u->sink && u->sink->get_volume) + u->sink->get_volume(u->sink); + if (u->source && u->source->get_volume) + u->source->get_volume(u->source); pa_log_info("Resumed successfully..."); @@ -631,6 +640,7 @@ fail: return -1; } +/* Called from IO context */ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { struct userdata *u = PA_SINK(o)->userdata; int ret; @@ -879,7 +889,6 @@ static void thread_func(void *userdata) { pa_make_realtime(u->core->realtime_priority); pa_thread_mq_install(&u->thread_mq); - pa_rtpoll_install(u->rtpoll); for (;;) { int ret; @@ -1311,6 +1320,12 @@ int pa__init(pa_module*m) { pa_proplist_setf(source_new_data.proplist, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE, "%lu", (unsigned long) (u->in_hwbuf_size)); pa_proplist_setf(source_new_data.proplist, PA_PROP_DEVICE_BUFFERING_FRAGMENT_SIZE, "%lu", (unsigned long) (u->in_fragment_size)); + if (pa_modargs_get_proplist(ma, "source_properties", source_new_data.proplist, PA_UPDATE_REPLACE) < 0) { + pa_log("Invalid properties"); + pa_source_new_data_done(&source_new_data); + goto fail; + } + u->source = pa_source_new(m->core, &source_new_data, PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY); pa_source_new_data_done(&source_new_data); pa_xfree(name_buf); @@ -1325,6 +1340,7 @@ int pa__init(pa_module*m) { pa_source_set_asyncmsgq(u->source, u->thread_mq.inq); pa_source_set_rtpoll(u->source, u->rtpoll); + pa_source_set_fixed_latency(u->source, pa_bytes_to_usec(u->in_hwbuf_size, &u->source->sample_spec)); u->source->refresh_volume = TRUE; if (use_mmap) @@ -1372,6 +1388,12 @@ int pa__init(pa_module*m) { pa_proplist_setf(sink_new_data.proplist, PA_PROP_DEVICE_BUFFERING_BUFFER_SIZE, "%lu", (unsigned long) (u->out_hwbuf_size)); pa_proplist_setf(sink_new_data.proplist, PA_PROP_DEVICE_BUFFERING_FRAGMENT_SIZE, "%lu", (unsigned long) (u->out_fragment_size)); + if (pa_modargs_get_proplist(ma, "sink_properties", sink_new_data.proplist, PA_UPDATE_REPLACE) < 0) { + pa_log("Invalid properties"); + pa_sink_new_data_done(&sink_new_data); + goto fail; + } + u->sink = pa_sink_new(m->core, &sink_new_data, PA_SINK_HARDWARE|PA_SINK_LATENCY); pa_sink_new_data_done(&sink_new_data); pa_xfree(name_buf); @@ -1386,9 +1408,10 @@ int pa__init(pa_module*m) { pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq); pa_sink_set_rtpoll(u->sink, u->rtpoll); + pa_sink_set_fixed_latency(u->sink, pa_bytes_to_usec(u->out_hwbuf_size, &u->sink->sample_spec)); u->sink->refresh_volume = TRUE; - u->sink->thread_info.max_request = u->out_hwbuf_size; + pa_sink_set_max_request(u->sink, u->out_hwbuf_size); if (use_mmap) u->out_mmap_memblocks = pa_xnew0(pa_memblock*, u->out_nfrags); diff --git a/src/modules/raop/module-raop-discover.c b/src/modules/raop/module-raop-discover.c new file mode 100644 index 00000000..eaeb77fc --- /dev/null +++ b/src/modules/raop/module-raop-discover.c @@ -0,0 +1,397 @@ +/*** + This file is part of PulseAudio. + + Copyright 2004-2006 Lennart Poettering + Copyright 2008 Colin Guthrie + + 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 along with PulseAudio; if not, write to the Free Software + 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 <string.h> +#include <unistd.h> + +#include <avahi-client/client.h> +#include <avahi-client/lookup.h> +#include <avahi-common/alternative.h> +#include <avahi-common/error.h> +#include <avahi-common/domain.h> +#include <avahi-common/malloc.h> + +#include <pulse/xmalloc.h> +#include <pulse/util.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/hashmap.h> +#include <pulsecore/modargs.h> +#include <pulsecore/namereg.h> +#include <pulsecore/avahi-wrap.h> + +#include "module-raop-discover-symdef.h" + +PA_MODULE_AUTHOR("Colin Guthrie"); +PA_MODULE_DESCRIPTION("mDNS/DNS-SD Service Discovery of RAOP devices"); +PA_MODULE_VERSION(PACKAGE_VERSION); +PA_MODULE_LOAD_ONCE(TRUE); + +#define SERVICE_TYPE_SINK "_raop._tcp" + +static const char* const valid_modargs[] = { + NULL +}; + +struct tunnel { + AvahiIfIndex interface; + AvahiProtocol protocol; + char *name, *type, *domain; + uint32_t module_index; +}; + +struct userdata { + pa_core *core; + pa_module *module; + AvahiPoll *avahi_poll; + AvahiClient *client; + AvahiServiceBrowser *sink_browser; + + pa_hashmap *tunnels; +}; + +static unsigned tunnel_hash(const void *p) { + const struct tunnel *t = p; + + return + (unsigned) t->interface + + (unsigned) t->protocol + + pa_idxset_string_hash_func(t->name) + + pa_idxset_string_hash_func(t->type) + + pa_idxset_string_hash_func(t->domain); +} + +static int tunnel_compare(const void *a, const void *b) { + const struct tunnel *ta = a, *tb = b; + int r; + + if (ta->interface != tb->interface) + return 1; + if (ta->protocol != tb->protocol) + return 1; + if ((r = strcmp(ta->name, tb->name))) + return r; + if ((r = strcmp(ta->type, tb->type))) + return r; + if ((r = strcmp(ta->domain, tb->domain))) + return r; + + return 0; +} + +static struct tunnel *tunnel_new( + AvahiIfIndex interface, AvahiProtocol protocol, + const char *name, const char *type, const char *domain) { + + struct tunnel *t; + t = pa_xnew(struct tunnel, 1); + t->interface = interface; + t->protocol = protocol; + t->name = pa_xstrdup(name); + t->type = pa_xstrdup(type); + t->domain = pa_xstrdup(domain); + t->module_index = PA_IDXSET_INVALID; + return t; +} + +static void tunnel_free(struct tunnel *t) { + pa_assert(t); + pa_xfree(t->name); + pa_xfree(t->type); + pa_xfree(t->domain); + pa_xfree(t); +} + +static void resolver_cb( + AvahiServiceResolver *r, + AvahiIfIndex interface, AvahiProtocol protocol, + AvahiResolverEvent event, + const char *name, const char *type, const char *domain, + const char *host_name, const AvahiAddress *a, uint16_t port, + AvahiStringList *txt, + AvahiLookupResultFlags flags, + void *userdata) { + + struct userdata *u = userdata; + struct tunnel *tnl; + + pa_assert(u); + + tnl = tunnel_new(interface, protocol, name, type, domain); + + if (event != AVAHI_RESOLVER_FOUND) + pa_log("Resolving of '%s' failed: %s", name, avahi_strerror(avahi_client_errno(u->client))); + else { + char *device = NULL, *nicename, *dname, *vname, *args; + char at[AVAHI_ADDRESS_STR_MAX]; + AvahiStringList *l; + pa_module *m; + + if ((nicename = strstr(name, "@"))) { + ++nicename; + if (strlen(nicename) > 0) { + pa_log_debug("Found RAOP: %s", nicename); + } + } + + for (l = txt; l; l = l->next) { + char *key, *value; + pa_assert_se(avahi_string_list_get_pair(l, &key, &value, NULL) == 0); + + pa_log_debug("Found key: '%s' with value: '%s'", key, value); + if (strcmp(key, "device") == 0) { + pa_xfree(device); + device = value; + value = NULL; + } + avahi_free(key); + avahi_free(value); + } + + if (device) + dname = pa_sprintf_malloc("raop.%s.%s", host_name, device); + else + dname = pa_sprintf_malloc("raop.%s", host_name); + + if (!(vname = pa_namereg_make_valid_name(dname))) { + pa_log("Cannot construct valid device name from '%s'.", dname); + avahi_free(device); + pa_xfree(dname); + goto finish; + } + pa_xfree(dname); + + /* + TODO: allow this syntax of server name in things.... + args = pa_sprintf_malloc("server=[%s]:%u " + "sink_name=%s", + avahi_address_snprint(at, sizeof(at), a), port, + vname);*/ + if (nicename) { + args = pa_sprintf_malloc("server=%s " + "sink_name=%s " + "description=\"%s\"", + avahi_address_snprint(at, sizeof(at), a), + vname, + nicename); + + } else { + args = pa_sprintf_malloc("server=%s " + "sink_name=%s", + avahi_address_snprint(at, sizeof(at), a), + vname); + } + + pa_log_debug("Loading module-raop-sink with arguments '%s'", args); + + if ((m = pa_module_load(u->core, "module-raop-sink", args))) { + tnl->module_index = m->index; + pa_hashmap_put(u->tunnels, tnl, tnl); + tnl = NULL; + } + + pa_xfree(vname); + pa_xfree(args); + avahi_free(device); + } + +finish: + + avahi_service_resolver_free(r); + + if (tnl) + tunnel_free(tnl); +} + +static void browser_cb( + AvahiServiceBrowser *b, + AvahiIfIndex interface, AvahiProtocol protocol, + AvahiBrowserEvent event, + const char *name, const char *type, const char *domain, + AvahiLookupResultFlags flags, + void *userdata) { + + struct userdata *u = userdata; + struct tunnel *t; + + pa_assert(u); + + if (flags & AVAHI_LOOKUP_RESULT_LOCAL) + return; + + t = tunnel_new(interface, protocol, name, type, domain); + + if (event == AVAHI_BROWSER_NEW) { + + if (!pa_hashmap_get(u->tunnels, t)) + if (!(avahi_service_resolver_new(u->client, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, resolver_cb, u))) + pa_log("avahi_service_resolver_new() failed: %s", avahi_strerror(avahi_client_errno(u->client))); + + /* We ignore the returned resolver object here, since the we don't + * need to attach any special data to it, and we can still destroy + * it from the callback */ + + } else if (event == AVAHI_BROWSER_REMOVE) { + struct tunnel *t2; + + if ((t2 = pa_hashmap_get(u->tunnels, t))) { + pa_module_unload_by_index(u->core, t2->module_index, TRUE); + pa_hashmap_remove(u->tunnels, t2); + tunnel_free(t2); + } + } + + tunnel_free(t); +} + +static void client_callback(AvahiClient *c, AvahiClientState state, void *userdata) { + struct userdata *u = userdata; + + pa_assert(c); + pa_assert(u); + + u->client = c; + + switch (state) { + case AVAHI_CLIENT_S_REGISTERING: + case AVAHI_CLIENT_S_RUNNING: + case AVAHI_CLIENT_S_COLLISION: + + if (!u->sink_browser) { + + if (!(u->sink_browser = avahi_service_browser_new( + c, + AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, + SERVICE_TYPE_SINK, + NULL, + 0, + browser_cb, u))) { + + pa_log("avahi_service_browser_new() failed: %s", avahi_strerror(avahi_client_errno(c))); + pa_module_unload_request(u->module, TRUE); + } + } + + break; + + case AVAHI_CLIENT_FAILURE: + if (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) { + int error; + + pa_log_debug("Avahi daemon disconnected."); + + 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); + } + } + + /* Fall through */ + + case AVAHI_CLIENT_CONNECTING: + + if (u->sink_browser) { + avahi_service_browser_free(u->sink_browser); + u->sink_browser = NULL; + } + + break; + + default: ; + } +} + +int pa__init(pa_module*m) { + + struct userdata *u; + pa_modargs *ma = NULL; + int error; + + if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { + pa_log("Failed to parse module arguments."); + goto fail; + } + + m->userdata = u = pa_xnew(struct userdata, 1); + u->core = m->core; + u->module = m; + u->sink_browser = NULL; + + u->tunnels = pa_hashmap_new(tunnel_hash, tunnel_compare); + + u->avahi_poll = pa_avahi_poll_new(m->core->mainloop); + + if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) { + pa_log("pa_avahi_client_new() failed: %s", avahi_strerror(error)); + goto fail; + } + + pa_modargs_free(ma); + + return 0; + +fail: + pa__done(m); + + if (ma) + pa_modargs_free(ma); + + return -1; +} + +void pa__done(pa_module*m) { + struct userdata*u; + pa_assert(m); + + if (!(u = m->userdata)) + return; + + if (u->client) + avahi_client_free(u->client); + + if (u->avahi_poll) + pa_avahi_poll_free(u->avahi_poll); + + if (u->tunnels) { + struct tunnel *t; + + while ((t = pa_hashmap_steal_first(u->tunnels))) { + pa_module_unload_by_index(u->core, t->module_index, TRUE); + tunnel_free(t); + } + + pa_hashmap_free(u->tunnels, NULL, NULL); + } + + pa_xfree(u); +} diff --git a/src/modules/raop/module-raop-sink.c b/src/modules/raop/module-raop-sink.c new file mode 100644 index 00000000..9699132d --- /dev/null +++ b/src/modules/raop/module-raop-sink.c @@ -0,0 +1,697 @@ +/*** + This file is part of PulseAudio. + + Copyright 2004-2006 Lennart Poettering + Copyright 2008 Colin Guthrie + + 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 + along with PulseAudio; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stdlib.h> +#include <sys/stat.h> +#include <stdio.h> +#include <errno.h> +#include <string.h> +#include <fcntl.h> +#include <unistd.h> +#include <limits.h> +#include <poll.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <netinet/tcp.h> +#include <sys/ioctl.h> + +#ifdef HAVE_LINUX_SOCKIOS_H +#include <linux/sockios.h> +#endif + +#include <pulse/rtclock.h> +#include <pulse/timeval.h> +#include <pulse/xmalloc.h> + +#include <pulsecore/core-error.h> +#include <pulsecore/iochannel.h> +#include <pulsecore/sink.h> +#include <pulsecore/module.h> +#include <pulsecore/core-rtclock.h> +#include <pulsecore/core-util.h> +#include <pulsecore/modargs.h> +#include <pulsecore/log.h> +#include <pulsecore/socket-client.h> +#include <pulsecore/authkey.h> +#include <pulsecore/thread-mq.h> +#include <pulsecore/thread.h> +#include <pulsecore/time-smoother.h> +#include <pulsecore/socket-util.h> + +#include "module-raop-sink-symdef.h" +#include "rtp.h" +#include "sdp.h" +#include "sap.h" +#include "raop_client.h" + +PA_MODULE_AUTHOR("Colin Guthrie"); +PA_MODULE_DESCRIPTION("RAOP Sink"); +PA_MODULE_VERSION(PACKAGE_VERSION); +PA_MODULE_LOAD_ONCE(FALSE); +PA_MODULE_USAGE( + "sink_name=<name for the sink> " + "sink_properties=<properties for the sink> " + "server=<address> " + "format=<sample format> " + "rate=<sample rate> " + "channels=<number of channels>"); + +#define DEFAULT_SINK_NAME "raop" + +struct userdata { + pa_core *core; + pa_module *module; + pa_sink *sink; + + pa_thread_mq thread_mq; + pa_rtpoll *rtpoll; + pa_rtpoll_item *rtpoll_item; + pa_thread *thread; + + pa_memchunk raw_memchunk; + pa_memchunk encoded_memchunk; + + void *write_data; + size_t write_length, write_index; + + void *read_data; + size_t read_length, read_index; + + pa_usec_t latency; + + /*esd_format_t format;*/ + int32_t rate; + + pa_smoother *smoother; + int fd; + + int64_t offset; + int64_t encoding_overhead; + int32_t next_encoding_overhead; + double encoding_ratio; + + pa_raop_client *raop; + + size_t block_size; +}; + +static const char* const valid_modargs[] = { + "sink_name", + "sink_properties", + "server", + "format", + "rate", + "channels", + "description", /* supported for compatibility reasons, made redundant by sink_properties= */ + NULL +}; + +enum { + SINK_MESSAGE_PASS_SOCKET = PA_SINK_MESSAGE_MAX, + SINK_MESSAGE_RIP_SOCKET +}; + +/* Forward declaration */ +static void sink_set_volume_cb(pa_sink *); + +static void on_connection(int fd, void*userdata) { + int so_sndbuf = 0; + socklen_t sl = sizeof(int); + struct userdata *u = userdata; + pa_assert(u); + + pa_assert(u->fd < 0); + u->fd = fd; + + if (getsockopt(u->fd, SOL_SOCKET, SO_SNDBUF, &so_sndbuf, &sl) < 0) + pa_log_warn("getsockopt(SO_SNDBUF) failed: %s", pa_cstrerror(errno)); + else { + pa_log_debug("SO_SNDBUF is %zu.", (size_t) so_sndbuf); + pa_sink_set_max_request(u->sink, PA_MAX((size_t) so_sndbuf, u->block_size)); + } + + /* Set the initial volume */ + sink_set_volume_cb(u->sink); + + pa_log_debug("Connection authenticated, handing fd to IO thread..."); + + pa_asyncmsgq_post(u->thread_mq.inq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_PASS_SOCKET, NULL, 0, NULL, NULL); +} + +static void on_close(void*userdata) { + struct userdata *u = userdata; + pa_assert(u); + + pa_log_debug("Connection closed, informing IO thread..."); + + pa_asyncmsgq_post(u->thread_mq.inq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_RIP_SOCKET, NULL, 0, NULL, NULL); +} + +static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) { + struct userdata *u = PA_SINK(o)->userdata; + + switch (code) { + + case PA_SINK_MESSAGE_SET_STATE: + + switch ((pa_sink_state_t) PA_PTR_TO_UINT(data)) { + + case PA_SINK_SUSPENDED: + pa_assert(PA_SINK_IS_OPENED(u->sink->thread_info.state)); + + pa_smoother_pause(u->smoother, pa_rtclock_now()); + + /* Issue a FLUSH if we are connected */ + if (u->fd >= 0) { + pa_raop_flush(u->raop); + } + break; + + case PA_SINK_IDLE: + case PA_SINK_RUNNING: + + if (u->sink->thread_info.state == PA_SINK_SUSPENDED) { + pa_smoother_resume(u->smoother, pa_rtclock_now(), TRUE); + + /* The connection can be closed when idle, so check to + see if we need to reestablish it */ + if (u->fd < 0) + pa_raop_connect(u->raop); + else + pa_raop_flush(u->raop); + } + + break; + + case PA_SINK_UNLINKED: + case PA_SINK_INIT: + case PA_SINK_INVALID_STATE: + ; + } + + break; + + case PA_SINK_MESSAGE_GET_LATENCY: { + pa_usec_t w, r; + + r = pa_smoother_get(u->smoother, pa_rtclock_now()); + w = pa_bytes_to_usec((u->offset - u->encoding_overhead + (u->encoded_memchunk.length / u->encoding_ratio)), &u->sink->sample_spec); + + *((pa_usec_t*) data) = w > r ? w - r : 0; + return 0; + } + + case SINK_MESSAGE_PASS_SOCKET: { + struct pollfd *pollfd; + + pa_assert(!u->rtpoll_item); + + u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1); + pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL); + pollfd->fd = u->fd; + pollfd->events = POLLOUT; + /*pollfd->events = */pollfd->revents = 0; + + if (u->sink->thread_info.state == PA_SINK_SUSPENDED) { + /* Our stream has been suspended so we just flush it.... */ + pa_raop_flush(u->raop); + } + return 0; + } + + case SINK_MESSAGE_RIP_SOCKET: { + pa_assert(u->fd >= 0); + + pa_close(u->fd); + u->fd = -1; + + if (u->sink->thread_info.state == PA_SINK_SUSPENDED) { + + pa_log_debug("RTSP control connection closed, but we're suspended so let's not worry about it... we'll open it again later"); + + if (u->rtpoll_item) + pa_rtpoll_item_free(u->rtpoll_item); + u->rtpoll_item = NULL; + } else { + /* Quesiton: is this valid here: or should we do some sort of: + return pa_sink_process_msg(PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL); + ?? */ + pa_module_unload_request(u->module, TRUE); + } + return 0; + } + } + + return pa_sink_process_msg(o, code, data, offset, chunk); +} + +static void sink_set_volume_cb(pa_sink *s) { + struct userdata *u = s->userdata; + pa_cvolume hw; + pa_volume_t v; + char t[PA_CVOLUME_SNPRINT_MAX]; + + pa_assert(u); + + /* If we're muted we don't need to do anything */ + if (s->muted) + return; + + /* Calculate the max volume of all channels. + We'll use this as our (single) volume on the APEX device and emulate + any variation in channel volumes in software */ + v = pa_cvolume_max(&s->virtual_volume); + + /* Create a pa_cvolume version of our single value */ + pa_cvolume_set(&hw, s->sample_spec.channels, v); + + /* Perform any software manipulation of the volume needed */ + pa_sw_cvolume_divide(&s->soft_volume, &s->virtual_volume, &hw); + + pa_log_debug("Requested volume: %s", pa_cvolume_snprint(t, sizeof(t), &s->virtual_volume)); + pa_log_debug("Got hardware volume: %s", pa_cvolume_snprint(t, sizeof(t), &hw)); + pa_log_debug("Calculated software volume: %s", pa_cvolume_snprint(t, sizeof(t), &s->soft_volume)); + + /* Any necessary software volume manipulateion is done so set + our hw volume (or v as a single value) on the device */ + pa_raop_client_set_volume(u->raop, v); +} + +static void sink_set_mute_cb(pa_sink *s) { + struct userdata *u = s->userdata; + + pa_assert(u); + + if (s->muted) { + pa_raop_client_set_volume(u->raop, PA_VOLUME_MUTED); + } else { + sink_set_volume_cb(s); + } +} + +static void thread_func(void *userdata) { + struct userdata *u = userdata; + int write_type = 0; + pa_memchunk silence; + uint32_t silence_overhead = 0; + double silence_ratio = 0; + + pa_assert(u); + + pa_log_debug("Thread starting up"); + + pa_thread_mq_install(&u->thread_mq); + + pa_smoother_set_time_offset(u->smoother, pa_rtclock_now()); + + /* Create a chunk of memory that is our encoded silence sample. */ + pa_memchunk_reset(&silence); + + for (;;) { + int ret; + + if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) + if (u->sink->thread_info.rewind_requested) + pa_sink_process_rewind(u->sink, 0); + + if (u->rtpoll_item) { + struct pollfd *pollfd; + pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL); + + /* Render some data and write it to the fifo */ + if (/*PA_SINK_IS_OPENED(u->sink->thread_info.state) && */pollfd->revents) { + pa_usec_t usec; + int64_t n; + void *p; + + if (!silence.memblock) { + pa_memchunk silence_tmp; + + pa_memchunk_reset(&silence_tmp); + silence_tmp.memblock = pa_memblock_new(u->core->mempool, 4096); + silence_tmp.length = 4096; + p = pa_memblock_acquire(silence_tmp.memblock); + memset(p, 0, 4096); + pa_memblock_release(silence_tmp.memblock); + pa_raop_client_encode_sample(u->raop, &silence_tmp, &silence); + pa_assert(0 == silence_tmp.length); + silence_overhead = silence_tmp.length - 4096; + silence_ratio = silence_tmp.length / 4096; + pa_memblock_unref(silence_tmp.memblock); + } + + for (;;) { + ssize_t l; + + if (u->encoded_memchunk.length <= 0) { + if (u->encoded_memchunk.memblock) + pa_memblock_unref(u->encoded_memchunk.memblock); + if (PA_SINK_IS_OPENED(u->sink->thread_info.state)) { + size_t rl; + + /* We render real data */ + if (u->raw_memchunk.length <= 0) { + if (u->raw_memchunk.memblock) + pa_memblock_unref(u->raw_memchunk.memblock); + pa_memchunk_reset(&u->raw_memchunk); + + /* Grab unencoded data */ + pa_sink_render(u->sink, u->block_size, &u->raw_memchunk); + } + pa_assert(u->raw_memchunk.length > 0); + + /* Encode it */ + rl = u->raw_memchunk.length; + u->encoding_overhead += u->next_encoding_overhead; + pa_raop_client_encode_sample(u->raop, &u->raw_memchunk, &u->encoded_memchunk); + u->next_encoding_overhead = (u->encoded_memchunk.length - (rl - u->raw_memchunk.length)); + u->encoding_ratio = u->encoded_memchunk.length / (rl - u->raw_memchunk.length); + } else { + /* We render some silence into our memchunk */ + memcpy(&u->encoded_memchunk, &silence, sizeof(pa_memchunk)); + pa_memblock_ref(silence.memblock); + + /* Calculate/store some values to be used with the smoother */ + u->next_encoding_overhead = silence_overhead; + u->encoding_ratio = silence_ratio; + } + } + pa_assert(u->encoded_memchunk.length > 0); + + p = pa_memblock_acquire(u->encoded_memchunk.memblock); + l = pa_write(u->fd, (uint8_t*) p + u->encoded_memchunk.index, u->encoded_memchunk.length, &write_type); + pa_memblock_release(u->encoded_memchunk.memblock); + + pa_assert(l != 0); + + if (l < 0) { + + if (errno == EINTR) + continue; + else if (errno == EAGAIN) { + + /* OK, we filled all socket buffers up + * now. */ + goto filled_up; + + } else { + pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno)); + goto fail; + } + + } else { + u->offset += l; + + u->encoded_memchunk.index += l; + u->encoded_memchunk.length -= l; + + pollfd->revents = 0; + + if (u->encoded_memchunk.length > 0) { + /* we've completely written the encoded data, so update our overhead */ + u->encoding_overhead += u->next_encoding_overhead; + + /* OK, we wrote less that we asked for, + * hence we can assume that the socket + * buffers are full now */ + goto filled_up; + } + } + } + + filled_up: + + /* At this spot we know that the socket buffers are + * fully filled up. This is the best time to estimate + * the playback position of the server */ + + n = u->offset - u->encoding_overhead; + +#ifdef SIOCOUTQ + { + int l; + if (ioctl(u->fd, SIOCOUTQ, &l) >= 0 && l > 0) + n -= (l / u->encoding_ratio); + } +#endif + + usec = pa_bytes_to_usec(n, &u->sink->sample_spec); + + if (usec > u->latency) + usec -= u->latency; + else + usec = 0; + + pa_smoother_put(u->smoother, pa_rtclock_now(), usec); + } + + /* Hmm, nothing to do. Let's sleep */ + pollfd->events = POLLOUT; /*PA_SINK_IS_OPENED(u->sink->thread_info.state) ? POLLOUT : 0;*/ + } + + if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0) + goto fail; + + if (ret == 0) + goto finish; + + if (u->rtpoll_item) { + struct pollfd* pollfd; + + pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL); + + if (pollfd->revents & ~POLLOUT) { + if (u->sink->thread_info.state != PA_SINK_SUSPENDED) { + pa_log("FIFO shutdown."); + goto fail; + } + + /* We expect this to happen on occasion if we are not sending data. + It's perfectly natural and normal and natural */ + if (u->rtpoll_item) + pa_rtpoll_item_free(u->rtpoll_item); + u->rtpoll_item = NULL; + } + } + } + +fail: + /* If this was no regular exit from the loop we have to continue + * processing messages until we received PA_MESSAGE_SHUTDOWN */ + pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL); + pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN); + +finish: + if (silence.memblock) + pa_memblock_unref(silence.memblock); + pa_log_debug("Thread shutting down"); +} + +int pa__init(pa_module*m) { + struct userdata *u = NULL; + pa_sample_spec ss; + pa_modargs *ma = NULL; + const char *server, *desc; + pa_sink_new_data data; + + pa_assert(m); + + if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { + pa_log("failed to parse module arguments"); + goto fail; + } + + ss = m->core->default_sample_spec; + if (pa_modargs_get_sample_spec(ma, &ss) < 0) { + pa_log("invalid sample format specification"); + goto fail; + } + + if ((/*ss.format != PA_SAMPLE_U8 &&*/ ss.format != PA_SAMPLE_S16NE) || + (ss.channels > 2)) { + pa_log("sample type support is limited to mono/stereo and U8 or S16NE sample data"); + goto fail; + } + + u = pa_xnew0(struct userdata, 1); + u->core = m->core; + u->module = m; + m->userdata = u; + u->fd = -1; + u->smoother = pa_smoother_new( + PA_USEC_PER_SEC, + PA_USEC_PER_SEC*2, + TRUE, + TRUE, + 10, + 0, + FALSE); + pa_memchunk_reset(&u->raw_memchunk); + pa_memchunk_reset(&u->encoded_memchunk); + u->offset = 0; + u->encoding_overhead = 0; + u->next_encoding_overhead = 0; + u->encoding_ratio = 1.0; + + u->rtpoll = pa_rtpoll_new(); + pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll); + u->rtpoll_item = NULL; + + /*u->format = + (ss.format == PA_SAMPLE_U8 ? ESD_BITS8 : ESD_BITS16) | + (ss.channels == 2 ? ESD_STEREO : ESD_MONO);*/ + u->rate = ss.rate; + u->block_size = pa_usec_to_bytes(PA_USEC_PER_SEC/20, &ss); + + u->read_data = u->write_data = NULL; + u->read_index = u->write_index = u->read_length = u->write_length = 0; + + /*u->state = STATE_AUTH;*/ + u->latency = 0; + + if (!(server = pa_modargs_get_value(ma, "server", NULL))) { + pa_log("No server argument given."); + goto fail; + } + + pa_sink_new_data_init(&data); + data.driver = __FILE__; + data.module = m; + pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME)); + pa_sink_new_data_set_sample_spec(&data, &ss); + pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, server); + pa_proplist_sets(data.proplist, PA_PROP_DEVICE_INTENDED_ROLES, "music"); + if ((desc = pa_modargs_get_value(ma, "description", NULL))) + pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, desc); + else + pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "RAOP sink '%s'", server); + + if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) { + pa_log("Invalid properties"); + pa_sink_new_data_done(&data); + goto fail; + } + + u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY|PA_SINK_NETWORK); + pa_sink_new_data_done(&data); + + if (!u->sink) { + pa_log("Failed to create sink."); + goto fail; + } + + u->sink->parent.process_msg = sink_process_msg; + u->sink->userdata = u; + u->sink->set_volume = sink_set_volume_cb; + u->sink->set_mute = sink_set_mute_cb; + u->sink->flags = PA_SINK_LATENCY|PA_SINK_NETWORK|PA_SINK_HW_VOLUME_CTRL; + + pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq); + pa_sink_set_rtpoll(u->sink, u->rtpoll); + + if (!(u->raop = pa_raop_client_new(u->core, server))) { + pa_log("Failed to connect to server."); + goto fail; + } + + pa_raop_client_set_callback(u->raop, on_connection, u); + pa_raop_client_set_closed_callback(u->raop, on_close, u); + + if (!(u->thread = pa_thread_new(thread_func, u))) { + pa_log("Failed to create thread."); + goto fail; + } + + pa_sink_put(u->sink); + + pa_modargs_free(ma); + + return 0; + +fail: + if (ma) + pa_modargs_free(ma); + + pa__done(m); + + return -1; +} + +int pa__get_n_used(pa_module *m) { + struct userdata *u; + + pa_assert(m); + pa_assert_se(u = m->userdata); + + return pa_sink_linked_by(u->sink); +} + +void pa__done(pa_module*m) { + struct userdata *u; + pa_assert(m); + + if (!(u = m->userdata)) + return; + + if (u->sink) + pa_sink_unlink(u->sink); + + if (u->thread) { + pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL); + pa_thread_free(u->thread); + } + + pa_thread_mq_done(&u->thread_mq); + + if (u->sink) + pa_sink_unref(u->sink); + + if (u->rtpoll_item) + pa_rtpoll_item_free(u->rtpoll_item); + + if (u->rtpoll) + pa_rtpoll_free(u->rtpoll); + + if (u->raw_memchunk.memblock) + pa_memblock_unref(u->raw_memchunk.memblock); + + if (u->encoded_memchunk.memblock) + pa_memblock_unref(u->encoded_memchunk.memblock); + + if (u->raop) + pa_raop_client_free(u->raop); + + pa_xfree(u->read_data); + pa_xfree(u->write_data); + + if (u->smoother) + pa_smoother_free(u->smoother); + + if (u->fd >= 0) + pa_close(u->fd); + + pa_xfree(u); +} diff --git a/src/modules/raop/raop_client.c b/src/modules/raop/raop_client.c index b3f243c3..c4b02371 100644 --- a/src/modules/raop/raop_client.c +++ b/src/modules/raop/raop_client.c @@ -331,7 +331,7 @@ static void rtsp_cb(pa_rtsp_client *rtsp, pa_rtsp_state state, pa_headerlist* he uint32_t port = pa_rtsp_serverport(c->rtsp); pa_log_debug("RAOP: RECORDED"); - if (!(c->sc = pa_socket_client_new_string(c->core->mainloop, c->host, port))) { + if (!(c->sc = pa_socket_client_new_string(c->core->mainloop, TRUE, c->host, port))) { pa_log("failed to connect to server '%s:%d'", c->host, port); return; } diff --git a/src/modules/reserve-monitor.c b/src/modules/reserve-monitor.c new file mode 100644 index 00000000..13ecde2b --- /dev/null +++ b/src/modules/reserve-monitor.c @@ -0,0 +1,269 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: t -*-*/ + +/*** + Copyright 2009 Lennart Poettering + + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation files + (the "Software"), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, + publish, distribute, sublicense, and/or sell copies of the Software, + and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +***/ + +#include <string.h> +#include <unistd.h> +#include <errno.h> +#include <stdlib.h> +#include <stdio.h> +#include <assert.h> + +#include "reserve-monitor.h" + +struct rm_monitor { + int ref; + + char *device_name; + char *service_name; + + DBusConnection *connection; + + unsigned busy:1; + unsigned filtering:1; + unsigned matching:1; + + rm_change_cb_t change_cb; + void *userdata; +}; + +#define SERVICE_PREFIX "org.freedesktop.ReserveDevice1." + +static DBusHandlerResult filter_handler( + DBusConnection *c, + DBusMessage *s, + void *userdata) { + + DBusMessage *reply; + rm_monitor *m; + DBusError error; + + dbus_error_init(&error); + + m = userdata; + assert(m->ref >= 1); + + if (dbus_message_is_signal(s, "org.freedesktop.DBus", "NameOwnerChanged")) { + const char *name, *old, *new; + + if (!dbus_message_get_args( + s, + &error, + DBUS_TYPE_STRING, &name, + DBUS_TYPE_STRING, &old, + DBUS_TYPE_STRING, &new, + DBUS_TYPE_INVALID)) + goto invalid; + + if (strcmp(name, m->service_name) == 0) { + m->busy = !!(new && *new); + + /* If we ourselves own the device, then don't consider this 'busy' */ + if (m->busy) { + const char *un; + + if ((un = dbus_bus_get_unique_name(c))) + if (strcmp(new, un) == 0) + m->busy = FALSE; + } + + if (m->change_cb) { + m->ref++; + m->change_cb(m); + rm_release(m); + } + } + } + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + +invalid: + if (!(reply = dbus_message_new_error( + s, + DBUS_ERROR_INVALID_ARGS, + "Invalid arguments"))) + goto oom; + + if (!dbus_connection_send(c, reply, NULL)) + goto oom; + + dbus_message_unref(reply); + + dbus_error_free(&error); + + return DBUS_HANDLER_RESULT_HANDLED; + +oom: + if (reply) + dbus_message_unref(reply); + + dbus_error_free(&error); + + return DBUS_HANDLER_RESULT_NEED_MEMORY; +} + +int rm_watch( + rm_monitor **_m, + DBusConnection *connection, + const char*device_name, + rm_change_cb_t change_cb, + DBusError *error) { + + rm_monitor *m = NULL; + int r; + DBusError _error; + + if (!error) + error = &_error; + + dbus_error_init(error); + + if (!_m) + return -EINVAL; + + if (!connection) + return -EINVAL; + + if (!device_name) + return -EINVAL; + + if (!(m = calloc(sizeof(rm_monitor), 1))) + return -ENOMEM; + + m->ref = 1; + + if (!(m->device_name = strdup(device_name))) { + r = -ENOMEM; + goto fail; + } + + m->connection = dbus_connection_ref(connection); + m->change_cb = change_cb; + + if (!(m->service_name = malloc(sizeof(SERVICE_PREFIX) + strlen(device_name)))) { + r = -ENOMEM; + goto fail; + } + sprintf(m->service_name, SERVICE_PREFIX "%s", m->device_name); + + if (!(dbus_connection_add_filter(m->connection, filter_handler, m, NULL))) { + r = -ENOMEM; + goto fail; + } + + m->filtering = 1; + + dbus_bus_add_match(m->connection, + "type='signal'," + "sender='" DBUS_SERVICE_DBUS "'," + "interface='" DBUS_INTERFACE_DBUS "'," + "member='NameOwnerChanged'", error); + + if (dbus_error_is_set(error)) { + r = -EIO; + goto fail; + } + + m->matching = 1; + + m->busy = dbus_bus_name_has_owner(m->connection, m->service_name, error); + + if (dbus_error_is_set(error)) { + r = -EIO; + goto fail; + } + + *_m = m; + return 0; + +fail: + if (&_error == error) + dbus_error_free(&_error); + + if (m) + rm_release(m); + + return r; +} + +void rm_release(rm_monitor *m) { + if (!m) + return; + + assert(m->ref > 0); + + if (--m->ref > 0) + return; + + if (m->matching) + dbus_bus_remove_match( + m->connection, + "type='signal'," + "sender='" DBUS_SERVICE_DBUS "'," + "interface='" DBUS_INTERFACE_DBUS "'," + "member='NameOwnerChanged'", NULL); + + if (m->filtering) + dbus_connection_remove_filter( + m->connection, + filter_handler, + m); + + free(m->device_name); + free(m->service_name); + + if (m->connection) + dbus_connection_unref(m->connection); + + free(m); +} + +int rm_busy(rm_monitor *m) { + if (!m) + return -EINVAL; + + assert(m->ref > 0); + + return m->busy; +} + +void rm_set_userdata(rm_monitor *m, void *userdata) { + + if (!m) + return; + + assert(m->ref > 0); + m->userdata = userdata; +} + +void* rm_get_userdata(rm_monitor *m) { + + if (!m) + return NULL; + + assert(m->ref > 0); + + return m->userdata; +} diff --git a/src/modules/reserve-monitor.h b/src/modules/reserve-monitor.h new file mode 100644 index 00000000..421a52e0 --- /dev/null +++ b/src/modules/reserve-monitor.h @@ -0,0 +1,72 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: t -*-*/ + +#ifndef fooreservemonitorhfoo +#define fooreservemonitorhfoo + +/*** + Copyright 2009 Lennart Poettering + + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation files + (the "Software"), to deal in the Software without restriction, + including without limitation the rights to use, copy, modify, merge, + publish, distribute, sublicense, and/or sell copies of the Software, + and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +***/ + +#include <dbus/dbus.h> +#include <inttypes.h> + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct rm_monitor rm_monitor; + +/* Prototype for a function that is called whenever the reservation + * device of a device changes. Use rm_monitor_busy() to find out the + * new state.*/ +typedef void (*rm_change_cb_t)(rm_monitor *m); + +/* Creates a monitor for watching the lock status of a device. Returns + * 0 on success, a negative errno style return value on error. The + * DBus error might be set as well if the error was caused D-Bus. */ +int rm_watch( + rm_monitor **m, /* On success a pointer to the newly allocated rm_device object will be filled in here */ + DBusConnection *connection, /* Session bus (when D-Bus learns about user busses we should switchg to user busses) */ + const char *device_name, /* The device to monitor, e.g. "Audio0" */ + rm_change_cb_t change_cb, /* Will be called whenever the lock status changes. May be NULL */ + DBusError *error); /* If we fail due to a D-Bus related issue the error will be filled in here. May be NULL. */ + +/* Free a rm_monitor object */ +void rm_release(rm_monitor *m); + +/* Checks whether the device is currently reserved, and returns 1 + * then, 0 if not, negative errno style error code value on error. */ +int rm_busy(rm_monitor *m); + +/* Attach a userdata pointer to an rm_monitor */ +void rm_set_userdata(rm_monitor *m, void *userdata); + +/* Query the userdata pointer from an rm_monitor. Returns NULL if no + * userdata was set. */ +void* rm_get_userdata(rm_monitor *m); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/modules/reserve-wrap.c b/src/modules/reserve-wrap.c index 7d339270..6086fc99 100644 --- a/src/modules/reserve-wrap.c +++ b/src/modules/reserve-wrap.c @@ -23,6 +23,8 @@ #include <config.h> #endif +#include <errno.h> + #include <pulse/xmalloc.h> #include <pulse/i18n.h> @@ -30,30 +32,48 @@ #include <pulsecore/core-util.h> #include <pulsecore/shared.h> -#include <modules/dbus-util.h> - +#ifdef HAVE_DBUS +#include <pulsecore/dbus-shared.h> #include "reserve.h" +#include "reserve-monitor.h" +#endif + #include "reserve-wrap.h" struct pa_reserve_wrapper { PA_REFCNT_DECLARE; pa_core *core; - pa_dbus_connection *connection; pa_hook hook; + char *shared_name; +#ifdef HAVE_DBUS + pa_dbus_connection *connection; struct rd_device *device; +#endif +}; + +struct pa_reserve_monitor_wrapper { + PA_REFCNT_DECLARE; + pa_core *core; + pa_hook hook; char *shared_name; +#ifdef HAVE_DBUS + pa_dbus_connection *connection; + struct rm_monitor *monitor; +#endif }; static void reserve_wrapper_free(pa_reserve_wrapper *r) { pa_assert(r); +#ifdef HAVE_DBUS if (r->device) rd_release(r->device); - pa_hook_done(&r->hook); - if (r->connection) pa_dbus_connection_unref(r->connection); +#endif + + pa_hook_done(&r->hook); if (r->shared_name) { pa_assert_se(pa_shared_remove(r->core, r->shared_name) >= 0); @@ -63,6 +83,7 @@ static void reserve_wrapper_free(pa_reserve_wrapper *r) { pa_xfree(r); } +#ifdef HAVE_DBUS static int request_cb(rd_device *d, int forced) { pa_reserve_wrapper *r; int k; @@ -74,20 +95,23 @@ static int request_cb(rd_device *d, int forced) { PA_REFCNT_INC(r); k = pa_hook_fire(&r->hook, PA_INT_TO_PTR(forced)); - pa_log_debug("Device unlock has been requested and %s.", k < 0 ? "failed" : "succeeded"); + pa_log_debug("Device unlock of %s has been requested and %s.", r->shared_name, k < 0 ? "failed" : "succeeded"); pa_reserve_wrapper_unref(r); return k < 0 ? -1 : 1; } +#endif pa_reserve_wrapper* pa_reserve_wrapper_get(pa_core *c, const char *device_name) { pa_reserve_wrapper *r; - DBusError error; int k; char *t; +#ifdef HAVE_DBUS + DBusError error; dbus_error_init(&error); +#endif pa_assert(c); pa_assert(device_name); @@ -111,9 +135,13 @@ pa_reserve_wrapper* pa_reserve_wrapper_get(pa_core *c, const char *device_name) pa_assert_se(pa_shared_set(c, r->shared_name, r) >= 0); +#ifdef HAVE_DBUS if (!(r->connection = pa_dbus_bus_get(c, DBUS_BUS_SESSION, &error)) || dbus_error_is_set(&error)) { - pa_log_error("Unable to contact D-Bus session bus: %s: %s", error.name, error.message); - goto fail; + pa_log_warn("Unable to contact D-Bus session bus: %s: %s", error.name, error.message); + + /* We don't treat this as error here because we want allow PA + * to run even when no session bus is available. */ + return r; } if ((k = rd_acquire( @@ -125,8 +153,13 @@ pa_reserve_wrapper* pa_reserve_wrapper_get(pa_core *c, const char *device_name) request_cb, NULL)) < 0) { - pa_log_error("Failed to acquire reservation lock on device '%s': %s", device_name, pa_cstrerror(-k)); - goto fail; + if (k == -EBUSY) { + pa_log_error("Device '%s' already locked.", device_name); + goto fail; + } else { + pa_log_warn("Failed to acquire reservation lock on device '%s': %s", device_name, pa_cstrerror(-k)); + return r; + } } pa_log_debug("Successfully acquired reservation lock on device '%s'", device_name); @@ -134,13 +167,15 @@ pa_reserve_wrapper* pa_reserve_wrapper_get(pa_core *c, const char *device_name) rd_set_userdata(r->device, r); return r; - fail: dbus_error_free(&error); reserve_wrapper_free(r); return NULL; +#else + return r; +#endif } void pa_reserve_wrapper_unref(pa_reserve_wrapper *r) { @@ -164,5 +199,146 @@ void pa_reserve_wrapper_set_application_device_name(pa_reserve_wrapper *r, const pa_assert(r); pa_assert(PA_REFCNT_VALUE(r) >= 1); +#ifdef HAVE_DBUS rd_set_application_device_name(r->device, name); +#endif +} + +static void reserve_monitor_wrapper_free(pa_reserve_monitor_wrapper *w) { + pa_assert(w); + +#ifdef HAVE_DBUS + if (w->monitor) + rm_release(w->monitor); + + if (w->connection) + pa_dbus_connection_unref(w->connection); +#endif + + pa_hook_done(&w->hook); + + if (w->shared_name) { + pa_assert_se(pa_shared_remove(w->core, w->shared_name) >= 0); + pa_xfree(w->shared_name); + } + + pa_xfree(w); +} + +#ifdef HAVE_DBUS +static void change_cb(rm_monitor *m) { + pa_reserve_monitor_wrapper *w; + int k; + + pa_assert(m); + pa_assert_se(w = rm_get_userdata(m)); + pa_assert(PA_REFCNT_VALUE(w) >= 1); + + PA_REFCNT_INC(w); + + if ((k = rm_busy(w->monitor)) < 0) + return; + + pa_hook_fire(&w->hook, PA_INT_TO_PTR(!!k)); + pa_log_debug("Device lock status of %s changed: %s", w->shared_name, k ? "busy" : "not busy"); + + pa_reserve_monitor_wrapper_unref(w); +} +#endif + +pa_reserve_monitor_wrapper* pa_reserve_monitor_wrapper_get(pa_core *c, const char *device_name) { + pa_reserve_monitor_wrapper *w; + int k; + char *t; +#ifdef HAVE_DBUS + DBusError error; + + dbus_error_init(&error); +#endif + + pa_assert(c); + pa_assert(device_name); + + t = pa_sprintf_malloc("reserve-monitor-wrapper@%s", device_name); + + if ((w = pa_shared_get(c, t))) { + pa_xfree(t); + + pa_assert(PA_REFCNT_VALUE(w) >= 1); + PA_REFCNT_INC(w); + + return w; + } + + w = pa_xnew0(pa_reserve_monitor_wrapper, 1); + PA_REFCNT_INIT(w); + w->core = c; + pa_hook_init(&w->hook, w); + w->shared_name = t; + + pa_assert_se(pa_shared_set(c, w->shared_name, w) >= 0); + +#ifdef HAVE_DBUS + if (!(w->connection = pa_dbus_bus_get(c, DBUS_BUS_SESSION, &error)) || dbus_error_is_set(&error)) { + pa_log_warn("Unable to contact D-Bus session bus: %s: %s", error.name, error.message); + + /* We don't treat this as error here because we want allow PA + * to run even when no session bus is available. */ + return w; + } + + if ((k = rm_watch( + &w->monitor, + pa_dbus_connection_get(w->connection), + device_name, + change_cb, + NULL)) < 0) { + + pa_log_warn("Failed to create watch on device '%s': %s", device_name, pa_cstrerror(-k)); + goto fail; + } + + pa_log_debug("Successfully create reservation lock monitor for device '%s'", device_name); + + rm_set_userdata(w->monitor, w); + return w; + +fail: + dbus_error_free(&error); + + reserve_monitor_wrapper_free(w); + + return NULL; +#else + return w; +#endif +} + +void pa_reserve_monitor_wrapper_unref(pa_reserve_monitor_wrapper *w) { + pa_assert(w); + pa_assert(PA_REFCNT_VALUE(w) >= 1); + + if (PA_REFCNT_DEC(w) > 0) + return; + + reserve_monitor_wrapper_free(w); +} + +pa_hook* pa_reserve_monitor_wrapper_hook(pa_reserve_monitor_wrapper *w) { + pa_assert(w); + pa_assert(PA_REFCNT_VALUE(w) >= 1); + + return &w->hook; +} + +pa_bool_t pa_reserve_monitor_wrapper_busy(pa_reserve_monitor_wrapper *w) { + pa_assert(w); + + pa_assert(PA_REFCNT_VALUE(w) >= 1); + +#ifdef HAVE_DBUS + return rm_busy(w->monitor) > 0; +#else + return FALSE; +#endif } diff --git a/src/modules/reserve-wrap.h b/src/modules/reserve-wrap.h index 2b97c91c..2de6c093 100644 --- a/src/modules/reserve-wrap.h +++ b/src/modules/reserve-wrap.h @@ -28,11 +28,18 @@ typedef struct pa_reserve_wrapper pa_reserve_wrapper; pa_reserve_wrapper* pa_reserve_wrapper_get(pa_core *c, const char *device_name); - void pa_reserve_wrapper_unref(pa_reserve_wrapper *r); pa_hook* pa_reserve_wrapper_hook(pa_reserve_wrapper *r); void pa_reserve_wrapper_set_application_device_name(pa_reserve_wrapper *r, const char *name); +typedef struct pa_reserve_monitor_wrapper pa_reserve_monitor_wrapper; + +pa_reserve_monitor_wrapper* pa_reserve_monitor_wrapper_get(pa_core *c, const char *device_name); +void pa_reserve_monitor_wrapper_unref(pa_reserve_monitor_wrapper *m); + +pa_hook* pa_reserve_monitor_wrapper_hook(pa_reserve_monitor_wrapper *m); +pa_bool_t pa_reserve_monitor_wrapper_busy(pa_reserve_monitor_wrapper *m); + #endif diff --git a/src/modules/reserve.c b/src/modules/reserve.c index 9a9591d2..5597f177 100644 --- a/src/modules/reserve.c +++ b/src/modules/reserve.c @@ -1,3 +1,5 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: t -*-*/ + /*** Copyright 2009 Lennart Poettering @@ -43,16 +45,15 @@ struct rd_device { DBusConnection *connection; - int owning:1; - int registered:1; - int filtering:1; - int gave_up:1; + unsigned owning:1; + unsigned registered:1; + unsigned filtering:1; + unsigned gave_up:1; rd_request_cb_t request_cb; void *userdata; }; - #define SERVICE_PREFIX "org.freedesktop.ReserveDevice1." #define OBJECT_PREFIX "/org/freedesktop/ReserveDevice1/" @@ -297,6 +298,7 @@ static DBusHandlerResult filter_handler( dbus_error_init(&error); d = userdata; + assert(d->ref >= 1); if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameLost")) { const char *name; @@ -560,7 +562,7 @@ void rd_release( assert(d->ref > 0); - if (--d->ref) + if (--d->ref > 0) return; @@ -575,17 +577,11 @@ void rd_release( d->connection, d->object_path); - if (d->owning) { - DBusError error; - dbus_error_init(&error); - + if (d->owning) dbus_bus_release_name( d->connection, d->service_name, - &error); - - dbus_error_free(&error); - } + NULL); free(d->device_name); free(d->application_name); diff --git a/src/modules/reserve.h b/src/modules/reserve.h index b315a08c..9ae49cf5 100644 --- a/src/modules/reserve.h +++ b/src/modules/reserve.h @@ -1,3 +1,5 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: t -*-*/ + #ifndef fooreservehfoo #define fooreservehfoo @@ -28,6 +30,10 @@ #include <dbus/dbus.h> #include <inttypes.h> +#ifdef __cplusplus +extern "C" { +#endif + typedef struct rd_device rd_device; /* Prototype for a function that is called whenever someone else wants @@ -45,7 +51,7 @@ typedef int (*rd_request_cb_t)( * the error was caused D-Bus. */ int rd_acquire( rd_device **d, /* On success a pointer to the newly allocated rd_device object will be filled in here */ - DBusConnection *connection, + DBusConnection *connection, /* Session bus (when D-Bus learns about user busses we should switchg to user busses) */ const char *device_name, /* The device to lock, e.g. "Audio0" */ const char *application_name, /* A human readable name of the application, e.g. "PulseAudio Sound Server" */ int32_t priority, /* The priority for this application. If unsure use 0 */ @@ -66,4 +72,8 @@ void rd_set_userdata(rd_device *d, void *userdata); * userdata was set. */ void* rd_get_userdata(rd_device *d); +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/modules/rtp/module-rtp-recv.c b/src/modules/rtp/module-rtp-recv.c index 33e23af2..5caf8272 100644 --- a/src/modules/rtp/module-rtp-recv.c +++ b/src/modules/rtp/module-rtp-recv.c @@ -33,6 +33,7 @@ #include <unistd.h> #include <poll.h> +#include <pulse/rtclock.h> #include <pulse/timeval.h> #include <pulse/xmalloc.h> @@ -43,15 +44,17 @@ #include <pulsecore/sink-input.h> #include <pulsecore/memblockq.h> #include <pulsecore/log.h> +#include <pulsecore/core-rtclock.h> #include <pulsecore/core-util.h> #include <pulsecore/modargs.h> #include <pulsecore/namereg.h> #include <pulsecore/sample-util.h> #include <pulsecore/macro.h> #include <pulsecore/atomic.h> -#include <pulsecore/rtclock.h> #include <pulsecore/atomic.h> #include <pulsecore/time-smoother.h> +#include <pulsecore/socket-util.h> +#include <pulsecore/once.h> #include "module-rtp-recv-symdef.h" @@ -60,7 +63,7 @@ #include "sap.h" PA_MODULE_AUTHOR("Lennart Poettering"); -PA_MODULE_DESCRIPTION("Recieve data from a network via RTP/SAP/SDP"); +PA_MODULE_DESCRIPTION("Receive data from a network via RTP/SAP/SDP"); PA_MODULE_VERSION(PACKAGE_VERSION); PA_MODULE_LOAD_ONCE(FALSE); PA_MODULE_USAGE( @@ -110,6 +113,7 @@ struct session { struct userdata { pa_module *module; + pa_core *core; pa_sap_context sap_context; pa_io_event* sap_event; @@ -165,7 +169,7 @@ static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) { pa_memblockq_rewind(s->memblockq, nbytes); } -/* Called from thread context */ +/* Called from I/O thread context */ static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) { struct session *s; @@ -184,11 +188,24 @@ static void sink_input_kill(pa_sink_input* i) { session_free(s); } +/* Called from IO context */ +static void sink_input_suspend_within_thread(pa_sink_input* i, pa_bool_t b) { + struct session *s; + pa_sink_input_assert_ref(i); + pa_assert_se(s = i->userdata); + + if (b) { + pa_smoother_pause(s->smoother, pa_rtclock_now()); + pa_memblockq_flush_read(s->memblockq); + } else + s->first_packet = FALSE; +} + /* Called from I/O thread context */ static int rtpoll_work_cb(pa_rtpoll_item *i) { pa_memchunk chunk; int64_t k, j, delta; - struct timeval now; + struct timeval now = { 0, 0 }; struct session *s; struct pollfd *p; @@ -206,10 +223,11 @@ static int rtpoll_work_cb(pa_rtpoll_item *i) { p->revents = 0; - if (pa_rtp_recv(&s->rtp_context, &chunk, s->userdata->module->core->mempool) < 0) + if (pa_rtp_recv(&s->rtp_context, &chunk, s->userdata->module->core->mempool, &now) < 0) return 0; - if (s->sdp_info.payload != s->rtp_context.payload) { + if (s->sdp_info.payload != s->rtp_context.payload || + !PA_SINK_IS_OPENED(s->sink_input->sink->thread_info.state)) { pa_memblock_unref(chunk.memblock); return 0; } @@ -229,7 +247,7 @@ static int rtpoll_work_cb(pa_rtpoll_item *i) { } } - /* Check wheter there was a timestamp overflow */ + /* Check whether there was a timestamp overflow */ k = (int64_t) s->rtp_context.timestamp - (int64_t) s->offset; j = (int64_t) 0x100000000LL - (int64_t) s->offset + (int64_t) s->rtp_context.timestamp; @@ -238,15 +256,24 @@ static int rtpoll_work_cb(pa_rtpoll_item *i) { else delta = j; - pa_memblockq_seek(s->memblockq, delta * (int64_t) s->rtp_context.frame_size, PA_SEEK_RELATIVE); + pa_memblockq_seek(s->memblockq, delta * (int64_t) s->rtp_context.frame_size, PA_SEEK_RELATIVE, TRUE); - pa_rtclock_get(&now); + if (now.tv_sec == 0) { + PA_ONCE_BEGIN { + pa_log_warn("Using artificial time instead of timestamp"); + } PA_ONCE_END; + pa_rtclock_get(&now); + } else + pa_rtclock_from_wallclock(&now); pa_smoother_put(s->smoother, pa_timeval_load(&now), pa_bytes_to_usec((uint64_t) pa_memblockq_get_write_index(s->memblockq), &s->sink_input->sample_spec)); + /* Tell the smoother that we are rolling now, in case it is still paused */ + pa_smoother_resume(s->smoother, pa_timeval_load(&now), TRUE); + if (pa_memblockq_push(s->memblockq, &chunk) < 0) { pa_log_warn("Queue overrun"); - pa_memblockq_seek(s->memblockq, (int64_t) chunk.length, PA_SEEK_RELATIVE); + pa_memblockq_seek(s->memblockq, (int64_t) chunk.length, PA_SEEK_RELATIVE, TRUE); } /* pa_log("blocks in q: %u", pa_memblockq_get_nblocks(s->memblockq)); */ @@ -262,14 +289,14 @@ static int rtpoll_work_cb(pa_rtpoll_item *i) { pa_usec_t wi, ri, render_delay, sink_delay = 0, latency, fix; unsigned fix_samples; - pa_log("Updating sample rate"); + pa_log_debug("Updating sample rate"); wi = pa_smoother_get(s->smoother, pa_timeval_load(&now)); ri = pa_bytes_to_usec((uint64_t) pa_memblockq_get_read_index(s->memblockq), &s->sink_input->sample_spec); - if (PA_MSGOBJECT(s->sink_input->sink)->process_msg(PA_MSGOBJECT(s->sink_input->sink), PA_SINK_MESSAGE_GET_LATENCY, &sink_delay, 0, NULL) < 0) - sink_delay = 0; + pa_log_debug("wi=%lu ri=%lu", (unsigned long) wi, (unsigned long) ri); + sink_delay = pa_sink_get_latency_within_thread(s->sink_input->sink); render_delay = pa_bytes_to_usec(pa_memblockq_get_length(s->sink_input->thread_info.render_memblockq), &s->sink_input->sink->sample_spec); if (ri > render_delay+sink_delay) @@ -294,14 +321,20 @@ static int rtpoll_work_cb(pa_rtpoll_item *i) { fix_samples = (unsigned) (fix * (pa_usec_t) s->sink_input->thread_info.sample_spec.rate / (pa_usec_t) RATE_UPDATE_INTERVAL); /* Check if deviation is in bounds */ - if (fix_samples > s->sink_input->sample_spec.rate*.20) + if (fix_samples > s->sink_input->sample_spec.rate*.50) pa_log_debug("Hmmm, rate fix is too large (%lu Hz), not applying.", (unsigned long) fix_samples); + else { + /* Fix up rate */ + if (latency < s->intended_latency) + s->sink_input->sample_spec.rate -= fix_samples; + else + s->sink_input->sample_spec.rate += fix_samples; + + if (s->sink_input->sample_spec.rate > PA_RATE_MAX) + s->sink_input->sample_spec.rate = PA_RATE_MAX; + } - /* Fix up rate */ - if (latency < s->intended_latency) - s->sink_input->sample_spec.rate -= fix_samples; - else - s->sink_input->sample_spec.rate += fix_samples; + pa_assert(pa_sample_spec_valid(&s->sink_input->sample_spec)); pa_resampler_set_input_rate(s->sink_input->thread_info.resampler, s->sink_input->sample_spec.rate); @@ -362,6 +395,14 @@ static int mcast_socket(const struct sockaddr* sa, socklen_t salen) { goto fail; } + pa_make_udp_socket_low_delay(fd); + + one = 1; + if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one)) < 0) { + pa_log("SO_TIMESTAMP failed: %s", pa_cstrerror(errno)); + goto fail; + } + one = 1; if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) { pa_log("SO_REUSEADDR failed: %s", pa_cstrerror(errno)); @@ -430,8 +471,14 @@ static struct session *session_new(struct userdata *u, const pa_sdp_info *sdp_in s->sdp_info = *sdp_info; s->rtpoll_item = NULL; s->intended_latency = LATENCY_USEC; - s->smoother = pa_smoother_new(PA_USEC_PER_SEC*5, PA_USEC_PER_SEC*2, TRUE, 10); - pa_smoother_set_time_offset(s->smoother, pa_timeval_load(&now)); + s->smoother = pa_smoother_new( + PA_USEC_PER_SEC*5, + PA_USEC_PER_SEC*2, + TRUE, + TRUE, + 10, + pa_timeval_load(&now), + TRUE); s->last_rate_update = pa_timeval_load(&now); pa_atomic_store(&s->timestamp, (int) now.tv_sec); @@ -472,6 +519,7 @@ static struct session *session_new(struct userdata *u, const pa_sdp_info *sdp_in s->sink_input->kill = sink_input_kill; s->sink_input->attach = sink_input_attach; s->sink_input->detach = sink_input_detach; + s->sink_input->suspend_within_thread = sink_input_suspend_within_thread; pa_sink_input_get_silence(s->sink_input, &silence); @@ -575,15 +623,13 @@ static void sap_event_cb(pa_mainloop_api *m, pa_io_event *e, int fd, pa_io_event } } -static void check_death_event_cb(pa_mainloop_api *m, pa_time_event *t, const struct timeval *ptv, void *userdata) { +static void check_death_event_cb(pa_mainloop_api *m, pa_time_event *t, const struct timeval *tv, void *userdata) { struct session *s, *n; struct userdata *u = userdata; struct timeval now; - struct timeval tv; pa_assert(m); pa_assert(t); - pa_assert(ptv); pa_assert(u); pa_rtclock_get(&now); @@ -601,9 +647,7 @@ static void check_death_event_cb(pa_mainloop_api *m, pa_time_event *t, const str } /* Restart timer */ - pa_gettimeofday(&tv); - pa_timeval_add(&tv, DEATH_TIMEOUT*PA_USEC_PER_SEC); - m->time_restart(t, &tv); + pa_core_rttime_restart(u->module->core, t, pa_rtclock_now() + DEATH_TIMEOUT * PA_USEC_PER_SEC); } int pa__init(pa_module*m) { @@ -617,7 +661,6 @@ int pa__init(pa_module*m) { socklen_t salen; const char *sap_address; int fd = -1; - struct timeval tv; pa_assert(m); @@ -648,9 +691,9 @@ int pa__init(pa_module*m) { if ((fd = mcast_socket(sa, salen)) < 0) goto fail; - u = pa_xnew(struct userdata, 1); - m->userdata = u; + m->userdata = u = pa_xnew(struct userdata, 1); u->module = m; + u->core = m->core; u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL)); u->sap_event = m->core->mainloop->io_new(m->core->mainloop, fd, PA_IO_EVENT_INPUT, sap_event_cb, u); @@ -660,9 +703,7 @@ int pa__init(pa_module*m) { u->n_sessions = 0; u->by_origin = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func); - pa_gettimeofday(&tv); - pa_timeval_add(&tv, DEATH_TIMEOUT * PA_USEC_PER_SEC); - u->check_death_event = m->core->mainloop->time_new(m->core->mainloop, &tv, check_death_event_cb, u); + u->check_death_event = pa_core_rttime_new(m->core, pa_rtclock_now() + DEATH_TIMEOUT * PA_USEC_PER_SEC, check_death_event_cb, u); pa_modargs_free(ma); diff --git a/src/modules/rtp/module-rtp-send.c b/src/modules/rtp/module-rtp-send.c index 722d12bd..f147364d 100644 --- a/src/modules/rtp/module-rtp-send.c +++ b/src/modules/rtp/module-rtp-send.c @@ -31,6 +31,7 @@ #include <string.h> #include <unistd.h> +#include <pulse/rtclock.h> #include <pulse/timeval.h> #include <pulse/util.h> #include <pulse/xmalloc.h> @@ -77,7 +78,7 @@ PA_MODULE_USAGE( #define DEFAULT_DESTINATION "224.0.0.56" #define MEMBLOCKQ_MAXLENGTH (1024*170) #define DEFAULT_MTU 1280 -#define SAP_INTERVAL 5 +#define SAP_INTERVAL (5*PA_USEC_PER_SEC) static const char* const valid_modargs[] = { "source", @@ -151,18 +152,14 @@ static void source_output_kill(pa_source_output* o) { static void sap_event_cb(pa_mainloop_api *m, pa_time_event *t, const struct timeval *tv, void *userdata) { struct userdata *u = userdata; - struct timeval next; pa_assert(m); pa_assert(t); - pa_assert(tv); pa_assert(u); pa_sap_send(&u->sap_context, 0); - pa_gettimeofday(&next); - pa_timeval_add(&next, SAP_INTERVAL * PA_USEC_PER_SEC); - m->time_restart(t, &next); + pa_core_rttime_restart(u->module->core, t, pa_rtclock_now() + SAP_INTERVAL); } int pa__init(pa_module*m) { @@ -186,7 +183,6 @@ int pa__init(pa_module*m) { char *p; int r, j; socklen_t k; - struct timeval tv; char hn[128], *n; pa_bool_t loop = FALSE; pa_source_output_new_data data; @@ -347,10 +343,10 @@ int pa__init(pa_module*m) { o->push = source_output_push; o->kill = source_output_kill; - u = pa_xnew(struct userdata, 1); - m->userdata = u; - o->userdata = u; + pa_log_info("Configured source latency of %llu ms.", + (unsigned long long) pa_source_output_set_requested_latency(o, pa_bytes_to_usec(mtu, &o->sample_spec)) / PA_USEC_PER_MSEC); + m->userdata = o->userdata = u = pa_xnew(struct userdata, 1); u->module = m; u->source_output = o; @@ -395,9 +391,7 @@ int pa__init(pa_module*m) { pa_sap_send(&u->sap_context, 0); - pa_gettimeofday(&tv); - pa_timeval_add(&tv, SAP_INTERVAL * PA_USEC_PER_SEC); - u->sap_event = m->core->mainloop->time_new(m->core->mainloop, &tv, sap_event_cb, u); + u->sap_event = pa_core_rttime_new(m->core, pa_rtclock_now() + SAP_INTERVAL, sap_event_cb, u); pa_source_output_put(u->source_output); diff --git a/src/modules/rtp/rtp.c b/src/modules/rtp/rtp.c index 7537c1f5..6706a10f 100644 --- a/src/modules/rtp/rtp.c +++ b/src/modules/rtp/rtp.c @@ -162,13 +162,16 @@ pa_rtp_context* pa_rtp_context_init_recv(pa_rtp_context *c, int fd, size_t frame return c; } -int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk, pa_mempool *pool) { +int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk, pa_mempool *pool, struct timeval *tstamp) { int size; struct msghdr m; + struct cmsghdr *cm; struct iovec iov; uint32_t header; unsigned cc; ssize_t r; + uint8_t aux[1024]; + pa_bool_t found_tstamp = FALSE; pa_assert(c); pa_assert(chunk); @@ -208,8 +211,8 @@ int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk, pa_mempool *pool) { m.msg_namelen = 0; m.msg_iov = &iov; m.msg_iovlen = 1; - m.msg_control = NULL; - m.msg_controllen = 0; + m.msg_control = aux; + m.msg_controllen = sizeof(aux); m.msg_flags = 0; r = recvmsg(c->fd, &m, 0); @@ -275,6 +278,18 @@ int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk, pa_mempool *pool) { pa_memchunk_reset(&c->memchunk); } + for (cm = CMSG_FIRSTHDR(&m); cm; cm = CMSG_NXTHDR(&m, cm)) { + if (cm->cmsg_level == SOL_SOCKET && cm->cmsg_type == SO_TIMESTAMP) + memcpy(tstamp, CMSG_DATA(cm), sizeof(struct timeval)); + found_tstamp = TRUE; + break; + } + + if (!found_tstamp) { + pa_log_warn("Couldn't find SO_TIMESTAMP data in auxiliary recvmsg() data!"); + memset(tstamp, 0, sizeof(tstamp)); + } + return 0; fail: diff --git a/src/modules/rtp/rtp.h b/src/modules/rtp/rtp.h index eff5e75b..b197e82f 100644 --- a/src/modules/rtp/rtp.h +++ b/src/modules/rtp/rtp.h @@ -43,7 +43,7 @@ pa_rtp_context* pa_rtp_context_init_send(pa_rtp_context *c, int fd, uint32_t ssr int pa_rtp_send(pa_rtp_context *c, size_t size, pa_memblockq *q); pa_rtp_context* pa_rtp_context_init_recv(pa_rtp_context *c, int fd, size_t frame_size); -int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk, pa_mempool *pool); +int pa_rtp_recv(pa_rtp_context *c, pa_memchunk *chunk, pa_mempool *pool, struct timeval *tstamp); void pa_rtp_context_destroy(pa_rtp_context *c); diff --git a/src/modules/rtp/rtsp_client.c b/src/modules/rtp/rtsp_client.c index 98db05dd..72d304e8 100644 --- a/src/modules/rtp/rtsp_client.c +++ b/src/modules/rtp/rtsp_client.c @@ -30,6 +30,7 @@ #include <arpa/inet.h> #include <unistd.h> #include <sys/ioctl.h> +#include <netinet/in.h> #ifdef HAVE_SYS_FILIO_H #include <sys/filio.h> @@ -211,7 +212,7 @@ static void line_callback(pa_ioline *line, const char *s, void *userdata) { } if (!strlen(s2)) { /* End of headers */ - /* We will have a header left from our looping itteration, so add it in :) */ + /* We will have a header left from our looping iteration, so add it in :) */ if (c->last_header) { /* This is not a continuation header so let's dump it into our proplist */ pa_headerlist_puts(c->response_headers, c->last_header, pa_strbuf_tostring_free(c->header_buffer)); @@ -332,7 +333,7 @@ int pa_rtsp_connect(pa_rtsp_client *c) { pa_xfree(c->session); c->session = NULL; - if (!(c->sc = pa_socket_client_new_string(c->mainloop, c->hostname, c->port))) { + if (!(c->sc = pa_socket_client_new_string(c->mainloop, TRUE, c->hostname, c->port))) { pa_log("failed to connect to server '%s:%d'", c->hostname, c->port); return -1; } @@ -488,7 +489,7 @@ int pa_rtsp_record(pa_rtsp_client* c, uint16_t* seq, uint32_t* rtptime) { pa_assert(c); if (!c->session) { - /* No seesion in progres */ + /* No session in progress */ return -1; } diff --git a/src/modules/udev-util.c b/src/modules/udev-util.c index 8ffb76a8..cc824465 100644 --- a/src/modules/udev-util.c +++ b/src/modules/udev-util.c @@ -58,15 +58,14 @@ static int read_id(struct udev_device *d, const char *n) { return u; } -int pa_udev_get_info(pa_core *core, pa_proplist *p, int card_idx) { +int pa_udev_get_info(int card_idx, pa_proplist *p) { int r = -1; struct udev *udev; - struct udev_device *card; + struct udev_device *card = NULL; char *t; const char *v; int id; - pa_assert(core); pa_assert(p); pa_assert(card_idx >= 0); @@ -84,12 +83,25 @@ int pa_udev_get_info(pa_core *core, pa_proplist *p, int card_idx) { goto finish; } + if (!pa_proplist_contains(p, PA_PROP_DEVICE_BUS_PATH)) + if (((v = udev_device_get_property_value(card, "ID_PATH")) && *v) || + (v = udev_device_get_devpath(card))) + pa_proplist_sets(p, PA_PROP_DEVICE_BUS_PATH, v); + + if (!pa_proplist_contains(p, "sysfs.path")) + if ((v = udev_device_get_devpath(card))) + pa_proplist_sets(p, "sysfs.path", v); + + if (!pa_proplist_contains(p, "udev.id")) + if ((v = udev_device_get_property_value(card, "ID_ID")) && *v) + pa_proplist_sets(p, "udev.id", v); + if (!pa_proplist_contains(p, PA_PROP_DEVICE_BUS)) if ((v = udev_device_get_property_value(card, "ID_BUS")) && *v) pa_proplist_sets(p, PA_PROP_DEVICE_BUS, v); if (!pa_proplist_contains(p, PA_PROP_DEVICE_VENDOR_ID)) - if ((id = read_id(card, "ID_VENDOR_ID")) > 0 && *v) + if ((id = read_id(card, "ID_VENDOR_ID")) > 0) pa_proplist_setf(p, PA_PROP_DEVICE_VENDOR_ID, "%04x", id); if (!pa_proplist_contains(p, PA_PROP_DEVICE_VENDOR_NAME)) { @@ -114,15 +126,15 @@ int pa_udev_get_info(pa_core *core, pa_proplist *p, int card_idx) { if ((v = udev_device_get_property_value(card, "ID_SERIAL")) && *v) pa_proplist_sets(p, PA_PROP_DEVICE_SERIAL, v); + if (!pa_proplist_contains(p, PA_PROP_DEVICE_CLASS)) + if ((v = udev_device_get_property_value(card, "SOUND_CLASS")) && *v) + pa_proplist_sets(p, PA_PROP_DEVICE_CLASS, v); + if (!pa_proplist_contains(p, PA_PROP_DEVICE_FORM_FACTOR)) if ((v = udev_device_get_property_value(card, "SOUND_FORM_FACTOR")) && *v) pa_proplist_sets(p, PA_PROP_DEVICE_FORM_FACTOR, v); - if (!pa_proplist_contains(p, PA_PROP_DEVICE_BUS_PATH)) - if ((v = udev_device_get_devpath(card))) - pa_proplist_sets(p, PA_PROP_DEVICE_BUS_PATH, v); - - /* This is normaly not set by th udev rules but may be useful to + /* This is normaly not set by the udev rules but may be useful to * allow administrators to overwrite the device description.*/ if (!pa_proplist_contains(p, PA_PROP_DEVICE_DESCRIPTION)) if ((v = udev_device_get_property_value(card, "SOUND_DESCRIPTION")) && *v) @@ -140,3 +152,40 @@ finish: return r; } + +char* pa_udev_get_property(int card_idx, const char *name) { + struct udev *udev; + struct udev_device *card = NULL; + char *t, *r = NULL; + const char *v; + + pa_assert(card_idx >= 0); + pa_assert(name); + + if (!(udev = udev_new())) { + pa_log_error("Failed to allocate udev context."); + goto finish; + } + + t = pa_sprintf_malloc("%s/class/sound/card%i", udev_get_sys_path(udev), card_idx); + card = udev_device_new_from_syspath(udev, t); + pa_xfree(t); + + if (!card) { + pa_log_error("Failed to get card object."); + goto finish; + } + + if ((v = udev_device_get_property_value(card, name)) && *v) + r = pa_xstrdup(v); + +finish: + + if (card) + udev_device_unref(card); + + if (udev) + udev_unref(udev); + + return r; +} diff --git a/src/modules/udev-util.h b/src/modules/udev-util.h index 5120abdd..8523bc4d 100644 --- a/src/modules/udev-util.h +++ b/src/modules/udev-util.h @@ -25,6 +25,7 @@ #include <pulsecore/core.h> -int pa_udev_get_info(pa_core *core, pa_proplist *p, int card); +int pa_udev_get_info(int card_idx, pa_proplist *p); +char* pa_udev_get_property(int card_idx, const char *name); #endif diff --git a/src/modules/x11/module-x11-bell.c b/src/modules/x11/module-x11-bell.c new file mode 100644 index 00000000..ac303c3b --- /dev/null +++ b/src/modules/x11/module-x11-bell.c @@ -0,0 +1,190 @@ +/*** + 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.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 + along with PulseAudio; if not, write to the Free Software + 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 <string.h> + +#include <X11/Xlib.h> +#include <X11/XKBlib.h> + +#include <pulse/xmalloc.h> + +#include <pulsecore/iochannel.h> +#include <pulsecore/sink.h> +#include <pulsecore/core-scache.h> +#include <pulsecore/modargs.h> +#include <pulsecore/namereg.h> +#include <pulsecore/log.h> +#include <pulsecore/x11wrap.h> + +#include "module-x11-bell-symdef.h" + +PA_MODULE_AUTHOR("Lennart Poettering"); +PA_MODULE_DESCRIPTION("X11 bell interceptor"); +PA_MODULE_VERSION(PACKAGE_VERSION); +PA_MODULE_LOAD_ONCE(FALSE); +PA_MODULE_USAGE("sink=<sink to connect to> sample=<sample name> display=<X11 display>"); + +static const char* const valid_modargs[] = { + "sink", + "sample", + "display", + NULL +}; + +struct userdata { + pa_core *core; + pa_module *module; + + int xkb_event_base; + + char *sink_name; + char *scache_item; + + pa_x11_wrapper *x11_wrapper; + pa_x11_client *x11_client; +}; + +static int x11_event_cb(pa_x11_wrapper *w, XEvent *e, void *userdata) { + XkbBellNotifyEvent *bne; + struct userdata *u = userdata; + + pa_assert(w); + pa_assert(e); + pa_assert(u); + pa_assert(u->x11_wrapper == w); + + if (((XkbEvent*) e)->any.xkb_type != XkbBellNotify) + return 0; + + bne = (XkbBellNotifyEvent*) e; + + if (pa_scache_play_item_by_name(u->core, u->scache_item, u->sink_name, ((pa_volume_t) bne->percent*PA_VOLUME_NORM)/100U, NULL, NULL) < 0) { + pa_log_info("Ringing bell failed, reverting to X11 device bell."); + XkbForceDeviceBell(pa_x11_wrapper_get_display(w), bne->device, bne->bell_class, bne->bell_id, bne->percent); + } + + return 1; +} + +static void x11_kill_cb(pa_x11_wrapper *w, void *userdata) { + struct userdata *u = userdata; + + pa_assert(w); + pa_assert(u); + pa_assert(u->x11_wrapper == w); + + if (u->x11_client) + pa_x11_client_free(u->x11_client); + + if (u->x11_wrapper) + pa_x11_wrapper_unref(u->x11_wrapper); + + u->x11_client = NULL; + u->x11_wrapper = NULL; + + pa_module_unload_request(u->module, TRUE); +} + +int pa__init(pa_module*m) { + + struct userdata *u = NULL; + pa_modargs *ma = NULL; + int major, minor; + unsigned int auto_ctrls, auto_values; + + pa_assert(m); + + if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { + pa_log("Failed to parse module arguments"); + goto fail; + } + + m->userdata = u = pa_xnew(struct userdata, 1); + u->core = m->core; + u->module = m; + u->scache_item = pa_xstrdup(pa_modargs_get_value(ma, "sample", "bell-window-system")); + u->sink_name = pa_xstrdup(pa_modargs_get_value(ma, "sink", NULL)); + u->x11_client = NULL; + + if (!(u->x11_wrapper = pa_x11_wrapper_get(m->core, pa_modargs_get_value(ma, "display", NULL)))) + goto fail; + + major = XkbMajorVersion; + minor = XkbMinorVersion; + + if (!XkbLibraryVersion(&major, &minor)) { + pa_log("XkbLibraryVersion() failed"); + goto fail; + } + + major = XkbMajorVersion; + minor = XkbMinorVersion; + + if (!XkbQueryExtension(pa_x11_wrapper_get_display(u->x11_wrapper), NULL, &u->xkb_event_base, NULL, &major, &minor)) { + pa_log("XkbQueryExtension() failed"); + goto fail; + } + + XkbSelectEvents(pa_x11_wrapper_get_display(u->x11_wrapper), XkbUseCoreKbd, XkbBellNotifyMask, XkbBellNotifyMask); + auto_ctrls = auto_values = XkbAudibleBellMask; + XkbSetAutoResetControls(pa_x11_wrapper_get_display(u->x11_wrapper), XkbAudibleBellMask, &auto_ctrls, &auto_values); + XkbChangeEnabledControls(pa_x11_wrapper_get_display(u->x11_wrapper), XkbUseCoreKbd, XkbAudibleBellMask, 0); + + u->x11_client = pa_x11_client_new(u->x11_wrapper, x11_event_cb, x11_kill_cb, u); + + pa_modargs_free(ma); + + return 0; + +fail: + if (ma) + pa_modargs_free(ma); + + pa__done(m); + + return -1; +} + +void pa__done(pa_module*m) { + struct userdata *u; + + pa_assert(m); + + if (!(u = m->userdata)) + return; + + pa_xfree(u->scache_item); + pa_xfree(u->sink_name); + + if (u->x11_client) + pa_x11_client_free(u->x11_client); + + if (u->x11_wrapper) + pa_x11_wrapper_unref(u->x11_wrapper); + + pa_xfree(u); +} diff --git a/src/modules/x11/module-x11-cork-request.c b/src/modules/x11/module-x11-cork-request.c new file mode 100644 index 00000000..c1380c27 --- /dev/null +++ b/src/modules/x11/module-x11-cork-request.c @@ -0,0 +1,189 @@ +/*** + This file is part of PulseAudio. + + Copyright 2009 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.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 + along with PulseAudio; if not, write to the Free Software + 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 <string.h> +#include <unistd.h> + +#include <X11/Xlib.h> +#include <X11/extensions/XTest.h> +#include <X11/XF86keysym.h> +#include <X11/keysym.h> + +#include <pulse/util.h> +#include <pulse/xmalloc.h> + +#include <pulsecore/module.h> +#include <pulsecore/modargs.h> +#include <pulsecore/log.h> +#include <pulsecore/x11wrap.h> +#include <pulsecore/core-util.h> + +#include "module-x11-cork-request-symdef.h" + +PA_MODULE_AUTHOR("Lennart Poettering"); +PA_MODULE_DESCRIPTION("Synthesize X11 media key events when cork/uncork is requested"); +PA_MODULE_VERSION(PACKAGE_VERSION); +PA_MODULE_LOAD_ONCE(FALSE); +PA_MODULE_USAGE("display=<X11 display>"); + +static const char* const valid_modargs[] = { + "display", + NULL +}; + +struct userdata { + pa_module *module; + + pa_x11_wrapper *x11_wrapper; + pa_x11_client *x11_client; + + pa_hook_slot *hook_slot; +}; + +static void x11_kill_cb(pa_x11_wrapper *w, void *userdata) { + struct userdata *u = userdata; + + pa_assert(w); + pa_assert(u); + pa_assert(u->x11_wrapper == w); + + if (u->x11_client) { + pa_x11_client_free(u->x11_client); + u->x11_client = NULL; + } + + if (u->x11_wrapper) { + pa_x11_wrapper_unref(u->x11_wrapper); + u->x11_wrapper = NULL; + } + + pa_module_unload_request(u->module, TRUE); +} + +static pa_hook_result_t sink_input_send_event_hook_cb( + pa_core *c, + pa_sink_input_send_event_hook_data *data, + struct userdata *u) { + + KeySym sym; + KeyCode code; + Display *display; + + pa_assert(c); + pa_assert(data); + pa_assert(u); + + if (pa_streq(data->event, PA_STREAM_EVENT_REQUEST_CORK)) + sym = XF86XK_AudioPause; + else if (pa_streq(data->event, PA_STREAM_EVENT_REQUEST_UNCORK)) + sym = XF86XK_AudioPlay; + else + return PA_HOOK_OK; + + pa_log_debug("Triggering X11 keysym: %s", XKeysymToString(sym)); + + display = pa_x11_wrapper_get_display(u->x11_wrapper); + code = XKeysymToKeycode(display, sym); + + XTestFakeKeyEvent(display, code, True, CurrentTime); + XSync(display, False); + + XTestFakeKeyEvent(display, code, False, CurrentTime); + XSync(display, False); + + return PA_HOOK_OK; +} + +int pa__init(pa_module *m) { + struct userdata *u; + pa_modargs *ma; + int xtest_event_base, xtest_error_base; + int major_version, minor_version; + + pa_assert(m); + + if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { + pa_log("failed to parse module arguments"); + goto fail; + } + + m->userdata = u = pa_xnew0(struct userdata, 1); + u->module = m; + + if (!(u->x11_wrapper = pa_x11_wrapper_get(m->core, pa_modargs_get_value(ma, "display", NULL)))) + goto fail; + + if (!XTestQueryExtension( + pa_x11_wrapper_get_display(u->x11_wrapper), + &xtest_event_base, &xtest_error_base, + &major_version, &minor_version)) { + + pa_log("XTest extension not supported."); + goto fail; + } + + pa_log_debug("XTest %i.%i supported.", major_version, minor_version); + + u->x11_client = pa_x11_client_new(u->x11_wrapper, NULL, x11_kill_cb, u); + + u->hook_slot = pa_hook_connect( + &m->core->hooks[PA_CORE_HOOK_SINK_INPUT_SEND_EVENT], + PA_HOOK_NORMAL, + (pa_hook_cb_t) sink_input_send_event_hook_cb, u); + + pa_modargs_free(ma); + + return 0; + +fail: + if (ma) + pa_modargs_free(ma); + + pa__done(m); + + return -1; +} + +void pa__done(pa_module*m) { + struct userdata*u; + + pa_assert(m); + + if (!(u = m->userdata)) + return; + + if (u->x11_client) + pa_x11_client_free(u->x11_client); + + if (u->x11_wrapper) + pa_x11_wrapper_unref(u->x11_wrapper); + + if (u->hook_slot) + pa_hook_slot_free(u->hook_slot); + + pa_xfree(u); +} diff --git a/src/modules/x11/module-x11-publish.c b/src/modules/x11/module-x11-publish.c new file mode 100644 index 00000000..2c7fdc12 --- /dev/null +++ b/src/modules/x11/module-x11-publish.c @@ -0,0 +1,245 @@ +/*** + 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.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 + along with PulseAudio; if not, write to the Free Software + 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 <string.h> +#include <unistd.h> + +#include <X11/Xlib.h> +#include <X11/Xatom.h> + +#include <pulse/util.h> +#include <pulse/xmalloc.h> + +#include <pulsecore/module.h> +#include <pulsecore/sink.h> +#include <pulsecore/core-scache.h> +#include <pulsecore/modargs.h> +#include <pulsecore/namereg.h> +#include <pulsecore/log.h> +#include <pulsecore/x11wrap.h> +#include <pulsecore/core-util.h> +#include <pulsecore/native-common.h> +#include <pulsecore/auth-cookie.h> +#include <pulsecore/x11prop.h> +#include <pulsecore/strlist.h> +#include <pulsecore/shared.h> +#include <pulsecore/protocol-native.h> + +#include "module-x11-publish-symdef.h" + +PA_MODULE_AUTHOR("Lennart Poettering"); +PA_MODULE_DESCRIPTION("X11 credential publisher"); +PA_MODULE_VERSION(PACKAGE_VERSION); +PA_MODULE_LOAD_ONCE(FALSE); +PA_MODULE_USAGE( + "display=<X11 display> " + "sink=<Sink to publish> " + "source=<Source to publish> " + "cookie=<Cookie file to publish> "); + +static const char* const valid_modargs[] = { + "display", + "sink", + "source", + "cookie", + NULL +}; + +struct userdata { + pa_core *core; + pa_module *module; + pa_native_protocol *protocol; + + char *id; + pa_auth_cookie *auth_cookie; + + pa_x11_wrapper *x11_wrapper; + pa_x11_client *x11_client; + + pa_hook_slot *hook_slot; +}; + +static void publish_servers(struct userdata *u, pa_strlist *l) { + + if (l) { + char *s; + + l = pa_strlist_reverse(l); + s = pa_strlist_tostring(l); + l = pa_strlist_reverse(l); + + pa_x11_set_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SERVER", s); + pa_xfree(s); + } else + pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SERVER"); +} + +static pa_hook_result_t servers_changed_cb(void *hook_data, void *call_data, void *slot_data) { + pa_strlist *servers = call_data; + struct userdata *u = slot_data; + char t[256]; + + pa_assert(u); + + if (!pa_x11_get_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_ID", t, sizeof(t)) || strcmp(t, u->id)) { + pa_log_warn("PulseAudio information vanished from X11!"); + return PA_HOOK_OK; + } + + publish_servers(u, servers); + return PA_HOOK_OK; +} + +static void x11_kill_cb(pa_x11_wrapper *w, void *userdata) { + struct userdata *u = userdata; + + pa_assert(w); + pa_assert(u); + pa_assert(u->x11_wrapper == w); + + if (u->x11_client) + pa_x11_client_free(u->x11_client); + + if (u->x11_wrapper) + pa_x11_wrapper_unref(u->x11_wrapper); + + u->x11_client = NULL; + u->x11_wrapper = NULL; + + pa_module_unload_request(u->module, TRUE); +} + +int pa__init(pa_module*m) { + struct userdata *u; + pa_modargs *ma = NULL; + char *mid, *sid; + char hx[PA_NATIVE_COOKIE_LENGTH*2+1]; + const char *t; + + pa_assert(m); + + if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { + pa_log("failed to parse module arguments"); + goto fail; + } + + m->userdata = u = pa_xnew(struct userdata, 1); + u->core = m->core; + u->module = m; + u->protocol = pa_native_protocol_get(m->core); + u->id = NULL; + u->auth_cookie = NULL; + u->x11_client = NULL; + u->x11_wrapper = NULL; + + u->hook_slot = pa_hook_connect(&pa_native_protocol_hooks(u->protocol)[PA_NATIVE_HOOK_SERVERS_CHANGED], PA_HOOK_NORMAL, servers_changed_cb, u); + + if (!(u->auth_cookie = pa_auth_cookie_get(m->core, pa_modargs_get_value(ma, "cookie", PA_NATIVE_COOKIE_FILE), PA_NATIVE_COOKIE_LENGTH))) + goto fail; + + if (!(u->x11_wrapper = pa_x11_wrapper_get(m->core, pa_modargs_get_value(ma, "display", NULL)))) + goto fail; + + mid = pa_machine_id(); + u->id = pa_sprintf_malloc("%lu@%s/%lu", (unsigned long) getuid(), mid, (unsigned long) getpid()); + pa_xfree(mid); + + pa_x11_set_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_ID", u->id); + + if ((sid = pa_session_id())) { + pa_x11_set_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SESSION_ID", sid); + pa_xfree(sid); + } + + publish_servers(u, pa_native_protocol_servers(u->protocol)); + + if ((t = pa_modargs_get_value(ma, "source", NULL))) + pa_x11_set_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SOURCE", t); + + if ((t = pa_modargs_get_value(ma, "sink", NULL))) + pa_x11_set_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SINK", t); + + pa_x11_set_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_COOKIE", + pa_hexstr(pa_auth_cookie_read(u->auth_cookie, PA_NATIVE_COOKIE_LENGTH), PA_NATIVE_COOKIE_LENGTH, hx, sizeof(hx))); + + u->x11_client = pa_x11_client_new(u->x11_wrapper, NULL, x11_kill_cb, u); + + pa_modargs_free(ma); + + return 0; + +fail: + if (ma) + pa_modargs_free(ma); + + pa__done(m); + + return -1; +} + +void pa__done(pa_module*m) { + struct userdata*u; + + pa_assert(m); + + if (!(u = m->userdata)) + return; + + if (u->x11_client) + pa_x11_client_free(u->x11_client); + + if (u->x11_wrapper) { + char t[256]; + + /* Yes, here is a race condition */ + if (!pa_x11_get_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_ID", t, sizeof(t)) || strcmp(t, u->id)) + pa_log_warn("PulseAudio information vanished from X11!"); + else { + pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_ID"); + pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SERVER"); + pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SINK"); + pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SOURCE"); + pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_COOKIE"); + pa_x11_del_prop(pa_x11_wrapper_get_display(u->x11_wrapper), "PULSE_SESSION_ID"); + XSync(pa_x11_wrapper_get_display(u->x11_wrapper), False); + } + + pa_x11_wrapper_unref(u->x11_wrapper); + } + + if (u->auth_cookie) + pa_auth_cookie_unref(u->auth_cookie); + + if (u->hook_slot) + pa_hook_slot_free(u->hook_slot); + + if (u->protocol) + pa_native_protocol_unref(u->protocol); + + pa_xfree(u->id); + pa_xfree(u); +} diff --git a/src/modules/x11/module-x11-xsmp.c b/src/modules/x11/module-x11-xsmp.c new file mode 100644 index 00000000..28fd373a --- /dev/null +++ b/src/modules/x11/module-x11-xsmp.c @@ -0,0 +1,254 @@ +/*** + 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.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 + along with PulseAudio; if not, write to the Free Software + 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 <string.h> + +#include <X11/Xlib.h> +#include <X11/SM/SMlib.h> + +#include <pulse/xmalloc.h> +#include <pulse/util.h> + +#include <pulsecore/iochannel.h> +#include <pulsecore/sink.h> +#include <pulsecore/core-scache.h> +#include <pulsecore/modargs.h> +#include <pulsecore/namereg.h> +#include <pulsecore/log.h> +#include <pulsecore/core-util.h> +#include <pulsecore/x11wrap.h> + +#include "module-x11-xsmp-symdef.h" + +PA_MODULE_AUTHOR("Lennart Poettering"); +PA_MODULE_DESCRIPTION("X11 session management"); +PA_MODULE_VERSION(PACKAGE_VERSION); +PA_MODULE_LOAD_ONCE(FALSE); +PA_MODULE_USAGE("session_manager=<session manager string> display=<X11 display>"); + +static pa_bool_t ice_in_use = FALSE; + +static const char* const valid_modargs[] = { + "session_manager", + "display", + NULL +}; + +struct userdata { + pa_core *core; + pa_module *module; + pa_client *client; + SmcConn connection; + pa_x11_wrapper *x11; +}; + +static void die_cb(SmcConn connection, SmPointer client_data){ + struct userdata *u = client_data; + pa_assert(u); + + pa_log_debug("Got die message from XSMP."); + + pa_x11_wrapper_kill(u->x11); + + pa_x11_wrapper_unref(u->x11); + u->x11 = NULL; + + pa_module_unload_request(u->module, TRUE); +} + +static void save_complete_cb(SmcConn connection, SmPointer client_data) { +} + +static void shutdown_cancelled_cb(SmcConn connection, SmPointer client_data) { + SmcSaveYourselfDone(connection, True); +} + +static void save_yourself_cb(SmcConn connection, SmPointer client_data, int save_type, Bool _shutdown, int interact_style, Bool fast) { + SmcSaveYourselfDone(connection, True); +} + +static void ice_io_cb(pa_mainloop_api*a, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) { + IceConn connection = userdata; + + if (IceProcessMessages(connection, NULL, NULL) == IceProcessMessagesIOError) { + IceSetShutdownNegotiation(connection, False); + IceCloseConnection(connection); + } +} + +static void new_ice_connection(IceConn connection, IcePointer client_data, Bool opening, IcePointer *watch_data) { + struct pa_core *c = client_data; + + if (opening) + *watch_data = c->mainloop->io_new( + c->mainloop, + IceConnectionNumber(connection), + PA_IO_EVENT_INPUT, + ice_io_cb, + connection); + else + c->mainloop->io_free(*watch_data); +} + +int pa__init(pa_module*m) { + + pa_modargs *ma = NULL; + char t[256], *vendor, *client_id; + SmcCallbacks callbacks; + SmProp prop_program, prop_user; + SmProp *prop_list[2]; + SmPropValue val_program, val_user; + struct userdata *u; + const char *e; + pa_client_new_data data; + + pa_assert(m); + + if (ice_in_use) { + pa_log("module-x11-xsmp may no be loaded twice."); + return -1; + } + + IceAddConnectionWatch(new_ice_connection, m->core); + ice_in_use = TRUE; + + m->userdata = u = pa_xnew(struct userdata, 1); + u->core = m->core; + u->module = m; + u->client = NULL; + u->connection = NULL; + u->x11 = NULL; + + if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { + pa_log("Failed to parse module arguments"); + goto fail; + } + + if (!(u->x11 = pa_x11_wrapper_get(m->core, pa_modargs_get_value(ma, "display", NULL)))) + goto fail; + + e = pa_modargs_get_value(ma, "session_manager", NULL); + + if (!e && !getenv("SESSION_MANAGER")) { + pa_log("X11 session manager not running."); + goto fail; + } + + memset(&callbacks, 0, sizeof(callbacks)); + callbacks.die.callback = die_cb; + callbacks.die.client_data = u; + callbacks.save_yourself.callback = save_yourself_cb; + callbacks.save_yourself.client_data = m->core; + callbacks.save_complete.callback = save_complete_cb; + callbacks.save_complete.client_data = m->core; + callbacks.shutdown_cancelled.callback = shutdown_cancelled_cb; + callbacks.shutdown_cancelled.client_data = m->core; + + if (!(u->connection = SmcOpenConnection( + (char*) e, m->core, + SmProtoMajor, SmProtoMinor, + SmcSaveYourselfProcMask | SmcDieProcMask | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask, + &callbacks, NULL, &client_id, + sizeof(t), t))) { + + pa_log("Failed to open connection to session manager: %s", t); + goto fail; + } + + prop_program.name = (char*) SmProgram; + prop_program.type = (char*) SmARRAY8; + val_program.value = (char*) PACKAGE_NAME; + val_program.length = (int) strlen(val_program.value); + prop_program.num_vals = 1; + prop_program.vals = &val_program; + prop_list[0] = &prop_program; + + prop_user.name = (char*) SmUserID; + prop_user.type = (char*) SmARRAY8; + pa_get_user_name(t, sizeof(t)); + val_user.value = t; + val_user.length = (int) strlen(val_user.value); + prop_user.num_vals = 1; + prop_user.vals = &val_user; + prop_list[1] = &prop_user; + + SmcSetProperties(u->connection, PA_ELEMENTSOF(prop_list), prop_list); + + pa_log_info("Connected to session manager '%s' as '%s'.", vendor = SmcVendor(u->connection), client_id); + + pa_client_new_data_init(&data); + data.module = m; + data.driver = __FILE__; + pa_proplist_setf(data.proplist, PA_PROP_APPLICATION_NAME, "XSMP Session on %s as %s", vendor, client_id); + pa_proplist_sets(data.proplist, "xsmp.vendor", vendor); + pa_proplist_sets(data.proplist, "xsmp.client.id", client_id); + u->client = pa_client_new(u->core, &data); + pa_client_new_data_done(&data); + + free(vendor); + free(client_id); + + if (!u->client) + goto fail; + + pa_modargs_free(ma); + + return 0; + +fail: + if (ma) + pa_modargs_free(ma); + + pa__done(m); + + return -1; +} + +void pa__done(pa_module*m) { + struct userdata *u; + + pa_assert(m); + + if ((u = m->userdata)) { + + if (u->connection) + SmcCloseConnection(u->connection, 0, NULL); + + if (u->client) + pa_client_free(u->client); + + if (u->x11) + pa_x11_wrapper_unref(u->x11); + + pa_xfree(u); + } + + if (ice_in_use) { + IceRemoveConnectionWatch(new_ice_connection, m->core); + ice_in_use = FALSE; + } +} |