]> code.delx.au - pulseaudio/commitdiff
Merge commit 'origin/master-tx'
authorLennart Poettering <lennart@poettering.net>
Wed, 19 Aug 2009 02:14:00 +0000 (04:14 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 19 Aug 2009 02:14:00 +0000 (04:14 +0200)
81 files changed:
configure.ac
man/pulse-client.conf.5.xml.in
man/pulse-daemon.conf.5.xml.in
src/Makefile.am
src/daemon/caps.c
src/daemon/daemon-conf.c
src/daemon/daemon.conf.in
src/daemon/main.c
src/daemon/pulseaudio-system.conf [new file with mode: 0644]
src/map-file
src/modules/alsa/alsa-mixer.c
src/modules/alsa/alsa-sink.c
src/modules/alsa/alsa-source.c
src/modules/alsa/mixer/paths/analog-output.conf.common
src/modules/bluetooth/bluetooth-util.c
src/modules/bluetooth/bluetooth-util.h
src/modules/bluetooth/module-bluetooth-device.c
src/modules/module-combine.c
src/modules/module-console-kit.c
src/modules/module-device-restore.c
src/modules/module-hal-detect-compat.c [new file with mode: 0644]
src/modules/module-hal-detect.c
src/modules/module-intended-roles.c
src/modules/module-ladspa-sink.c
src/modules/module-lirc.c
src/modules/module-match.c
src/modules/module-mmkbd-evdev.c
src/modules/module-remap-sink.c
src/modules/module-rescue-streams.c
src/modules/module-stream-restore.c
src/modules/module-tunnel.c
src/modules/module-udev-detect.c
src/modules/module-volume-restore.c
src/modules/oss/module-oss.c
src/modules/raop/module-raop-sink.c
src/modules/reserve-monitor.c
src/modules/reserve.c
src/modules/rtp/module-rtp-recv.c
src/pulse/channelmap.c
src/pulse/channelmap.h
src/pulse/client-conf.c
src/pulse/client.conf.in
src/pulse/context.c
src/pulse/sample.c
src/pulse/stream.c
src/pulse/volume.c
src/pulse/volume.h
src/pulsecore/asyncmsgq.c
src/pulsecore/asyncmsgq.h
src/pulsecore/aupdate.c
src/pulsecore/aupdate.h
src/pulsecore/cli-command.c
src/pulsecore/cli-text.c
src/pulsecore/conf-parser.c
src/pulsecore/conf-parser.h
src/pulsecore/core-util.c
src/pulsecore/core-util.h
src/pulsecore/core.h
src/pulsecore/database-simple.c [new file with mode: 0644]
src/pulsecore/hook-list.c
src/pulsecore/macro.h
src/pulsecore/memblock.c
src/pulsecore/memblockq.c
src/pulsecore/memblockq.h
src/pulsecore/namereg.c
src/pulsecore/protocol-native.c
src/pulsecore/resampler.c
src/pulsecore/shm.c
src/pulsecore/sink-input.c
src/pulsecore/sink-input.h
src/pulsecore/sink.c
src/pulsecore/sink.h
src/pulsecore/source-output.c
src/pulsecore/source-output.h
src/pulsecore/source.c
src/pulsecore/source.h
src/pulsecore/start-child.c
src/pulsecore/thread-mq.c
src/pulsecore/thread-mq.h
src/tests/voltest.c
src/utils/pacmd.c

index a35bef836c258c81c0153a21377da7ab8b964a93..05312d39ae385090e962de02fad301af2eefd184 100644 (file)
@@ -626,10 +626,11 @@ AM_CONDITIONAL([HAVE_LIBSAMPLERATE], [test "x$HAVE_LIBSAMPLERATE" = x1])
 
 HAVE_TDB=0
 HAVE_GDBM=0
+HAVE_SIMPLEDB=0
 
 AC_ARG_WITH(
         [database],
-        AS_HELP_STRING([--with-database=auto|tdb|gdbm],[Choose database backend.]),[],[with_database=auto])
+        AS_HELP_STRING([--with-database=auto|tdb|gdbm|simple],[Choose database backend.]),[],[with_database=auto])
 
 if test "x${with_database}" = "xauto" -o "x${with_database}" = "xtdb" ; then
     PKG_CHECK_MODULES(TDB, [ tdb ],
@@ -659,7 +660,12 @@ if test "x${with_database}" = "xauto" -o "x${with_database}" = "xgdbm" ; then
    fi
 fi
 
-if test "x${HAVE_TDB}" != x1 -a "x${HAVE_GDBM}" != x1; then
+if test "x${with_database}" = "xauto" -o "x${with_database}" = "xsimple" ; then
+    HAVE_SIMPLEDB=1
+    with_database=simple
+fi
+
+if test "x${HAVE_TDB}" != x1 -a "x${HAVE_GDBM}" != x1 -a "x${HAVE_SIMPLEDB}" != x1; then
    AC_MSG_ERROR([*** missing database backend])
 fi
 
@@ -671,6 +677,10 @@ if test "x${HAVE_GDBM}" = x1 ; then
    AC_DEFINE([HAVE_GDBM], 1, [Have gdbm?])
 fi
 
+if test "x${HAVE_SIMPLEDB}" = x1 ; then
+    AC_DEFINE([HAVE_SIMPLEDB], 1, [Have simple?])
+fi
+
 AC_SUBST(TDB_CFLAGS)
 AC_SUBST(TDB_LIBS)
 AC_SUBST(HAVE_TDB)
@@ -681,6 +691,9 @@ AC_SUBST(GDBM_LIBS)
 AC_SUBST(HAVE_GDBM)
 AM_CONDITIONAL([HAVE_GDBM], [test "x$HAVE_GDBM" = x1])
 
+AC_SUBST(HAVE_SIMPLEDB)
+AM_CONDITIONAL([HAVE_SIMPLEDB], [test "x$HAVE_SIMPLEDB" = x1])
+
 #### OSS support (optional) ####
 
 AC_ARG_ENABLE([oss-output],
@@ -1116,7 +1129,27 @@ AC_SUBST(UDEV_LIBS)
 AC_SUBST(HAVE_UDEV)
 AM_CONDITIONAL([HAVE_UDEV], [test "x$HAVE_UDEV" = x1])
 
-AC_DEFINE([LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE], 1, [I know the API is subject to change.])
+#### HAL compat support (optional) ####
+
+AC_ARG_ENABLE([hal-compat],
+    AS_HELP_STRING([--disable-hal-compat],[Disable optional HAL->udev transition compatibility support]),
+        [
+            case "${enableval}" in
+                yes) halcompat=yes ;;
+                no) halcompat=no ;;
+                *) AC_MSG_ERROR(bad value ${enableval} for --disable-hal-compat) ;;
+            esac
+        ],
+        [halcompat=auto])
+if test "x${halcompat}" != xno -a "x$HAVE_HAL" = "x0" -a "x$HAVE_UDEV" = "x1" ; then
+    HAVE_HAL_COMPAT=1
+    AC_DEFINE([HAVE_HAL_COMPAT], 1, [Have HAL compatibility.])
+else
+    HAVE_HAL_COMPAT=0
+fi
+
+AC_SUBST(HAVE_HAL_COMPAT)
+AM_CONDITIONAL([HAVE_HAL_COMPAT], [test "x$HAVE_HAL_COMPAT" = x1])
 
 #### BlueZ support (optional) ####
 
@@ -1441,6 +1474,11 @@ if test "x$HAVE_UDEV" = "x1" ; then
    ENABLE_UDEV=yes
 fi
 
+ENABLE_HAL_COMPAT=no
+if test "x$HAVE_HAL_COMPAT" = "x1" ; then
+   ENABLE_HAL_COMPAT=yes
+fi
+
 ENABLE_TCPWRAP=no
 if test "x${LIBWRAP_LIBS}" != x ; then
    ENABLE_TCPWRAP=yes
@@ -1466,6 +1504,11 @@ if test "x${HAVE_TDB}" = "x1" ; then
    ENABLE_TDB=yes
 fi
 
+ENABLE_SIMPLEDB=no
+if test "x${HAVE_SIMPLEDB}" = "x1" ; then
+    ENABLE_SIMPLEDB=yes
+fi
+
 ENABLE_OPENSSL=no
 if test "x${HAVE_OPENSSL}" = "x1" ; then
    ENABLE_OPENSSL=yes
@@ -1507,6 +1550,7 @@ echo "
     Enable LIRC:                   ${ENABLE_LIRC}
     Enable HAL:                    ${ENABLE_HAL}
     Enable udev:                   ${ENABLE_UDEV}
+    Enable HAL->udev compat:       ${ENABLE_HAL_COMPAT}
     Enable BlueZ:                  ${ENABLE_BLUEZ}
     Enable TCP Wrappers:           ${ENABLE_TCPWRAP}
     Enable libsamplerate:          ${ENABLE_LIBSAMPLERATE}
@@ -1514,6 +1558,7 @@ echo "
     Enable OpenSSL (for Airtunes): ${ENABLE_OPENSSL}
     Enable tdb:                    ${ENABLE_TDB}
     Enable gdbm:                   ${ENABLE_GDBM}
+    Enable simple database:        ${ENABLE_SIMPLEDB}
 
     System User:                   ${PA_SYSTEM_USER}
     System Group:                  ${PA_SYSTEM_GROUP}
index 26e389081a797040b3814673e2f3c0315df3e9d8..46cc8450f50567e5a88ff4ac29c7797d388ac1c6 100644 (file)
@@ -92,9 +92,9 @@ USA.
     </option>
 
     <option>
-      <p><opt>disable-shm=</opt> Disable data transfer via POSIX
+      <p><opt>enable-shm=</opt> Enable data transfer via POSIX
       shared memory. Takes a boolean argument, defaults to
-      <opt>no</opt>.</p>
+      <opt>yes</opt>.</p>
     </option>
 
     <option>
index f1d5b8f60b8374786fc2599f00332c20f975c932..82c2b8e5d9e9a7c2c10b37fb45e8194ef717e84f 100644 (file)
@@ -65,20 +65,21 @@ USA.
     </option>
 
     <option>
-      <p><opt>disallow-module-loading=</opt> Disallow module loading
-      after startup. This is a security feature that makes sure that
-      no further modules may be loaded into the PulseAudio server
-      after startup completed. It is recommended to enable this when
-      <opt>system-instance</opt> is enabled. Please note that certain
-      features like automatic hot-plug support will not work if this
-      option is enabled. Takes a boolean argument, defaults to
-      <opt>no</opt>. The <opt>--disallow-module-loading</opt> command line
-      option takes precedence.</p>
+      <p><opt>allow-module-loading=</opt> Allow/disallow module
+      loading after startup. This is a security feature that if
+      dsabled makes sure that no further modules may be loaded into
+      the PulseAudio server after startup completed. It is recommended
+      to disable this when <opt>system-instance</opt> is
+      enabled. Please note that certain features like automatic
+      hot-plug support will not work if this option is enabled. Takes
+      a boolean argument, defaults to <opt>yes</opt>. The
+      <opt>--disallow-module-loading</opt> command line option takes
+      precedence.</p>
     </option>
 
     <option>
-      <p><opt>disallow-exit=</opt> Disallow exit on user
-      request. Defaults to <opt>no</opt>.</p>
+      <p><opt>allow-exit=</opt> Allow/disallow exit on user
+      request. Defaults to <opt>yes</opt>.</p>
     </option>
 
     <option>
@@ -105,19 +106,19 @@ USA.
     </option>
 
     <option>
-      <p><opt>disable-remixing=</opt> Never upmix or downmix channels
-      to different channel maps. Instead, do a simple name-based
-      matching only.</p>
+      <p><opt>enable-remixing=</opt> If disabled never upmix or
+      downmix channels to different channel maps. Instead, do a simple
+      name-based matching only. Defaults to <opt>yes.</opt></p>
     </option>
 
     <option>
-      <p><opt>disable-lfe-remixing=</opt> When upmixing or downmixing
-      ignore LFE channels. When this option is on the output LFE
-      channel will only get a signal when an input LFE channel is
-      available as well. If no input LFE channel is available the
-      output LFE channel will always be 0. If no output LFE channel is
-      available the signal on the input LFE channel will be
-      ignored. Defaults to "on".</p>
+      <p><opt>enable-lfe-remixing=</opt> if disabeld when upmixing or
+      downmixing ignore LFE channels. When this option is dsabled the
+      output LFE channel will only get a signal when an input LFE
+      channel is available as well. If no input LFE channel is
+      available the output LFE channel will always be 0. If no output
+      LFE channel is available the signal on the input LFE channel
+      will be ignored. Defaults to <opt>no</opt>.</p>
     </option>
 
     <option>
@@ -132,12 +133,12 @@ USA.
     </option>
 
     <option>
-      <p><opt>no-cpu-limit=</opt> Do not install the CPU load limiter,
-      even on platforms where it is supported. This option is useful
-      when debugging/profiling PulseAudio to disable disturbing
-      SIGXCPU signals. Takes a boolean argument, defaults to <opt>no</opt>. The
-      <opt>--no-cpu-limit</opt> command line argument takes
-      precedence.</p>
+      <p><opt>cpu-limit=</opt> If disabled d not install the CPU load
+      limiter, even on platforms where it is supported. This option is
+      useful when debugging/profiling PulseAudio to disable disturbing
+      SIGXCPU signals. Takes a boolean argument, defaults to
+      <opt>yes</opt>. The <opt>--no-cpu-limit</opt> command line
+      argument takes precedence.</p>
     </option>
 
     <option>
@@ -148,9 +149,9 @@ USA.
     </option>
 
     <option>
-      <p><opt>disable-shm=</opt> Disable data transfer via POSIX
+      <p><opt>enable-shm=</opt> Enable data transfer via POSIX
       shared memory. Takes a boolean argument, defaults to
-      <opt>no</opt>. The <opt>--disable-shm</opt> command line
+      <opt>yes</opt>. The <opt>--disable-shm</opt> command line
       argument takes precedence.</p>
     </option>
 
index 5d7115770fee03923f63960e466598af384bbf12..17011cd36a86fb8e52827094a909463a46fa43d1 100644 (file)
@@ -32,6 +32,7 @@ xdgautostartdir=$(sysconfdir)/xdg/autostart
 alsaprofilesetsdir=$(datadir)/pulseaudio/alsa-mixer/profile-sets
 alsapathsdir=$(datadir)/pulseaudio/alsa-mixer/paths
 udevrulesdir=/lib/udev/rules.d
+dbuspolicydir=$(sysconfdir)/dbus-1/system.d
 
 ###################################
 #            Defines              #
@@ -119,6 +120,7 @@ EXTRA_DIST = \
                modules/module-defs.h.m4 \
                daemon/pulseaudio.desktop.in \
                map-file \
+               daemon/pulseaudio-system.conf \
                modules/alsa/mixer/profile-sets/default.conf \
                modules/alsa/mixer/profile-sets/native-instruments-audio4dj.conf \
                modules/alsa/mixer/profile-sets/native-instruments-audio8dj.conf \
@@ -145,6 +147,9 @@ pulseconf_DATA = \
                daemon.conf \
                client.conf
 
+dbuspolicy_DATA = \
+               daemon/pulseaudio-system.conf
+
 if HAVE_X11
 xdgautostart_in_files = \
                daemon/pulseaudio.desktop.in
@@ -865,6 +870,9 @@ libpulsecore_@PA_MAJORMINORMICRO@_la_CFLAGS += $(TDB_CFLAGS)
 libpulsecore_@PA_MAJORMINORMICRO@_la_LIBADD += $(TDB_LIBS)
 endif
 
+if HAVE_SIMPLEDB
+libpulsecore_@PA_MAJORMINORMICRO@_la_SOURCES += pulsecore/database-simple.c
+endif
 
 # We split the foreign code off to not be annoyed by warnings we don't care about
 noinst_LTLIBRARIES = libpulsecore-foreign.la
@@ -1120,6 +1128,11 @@ modlibexec_LTLIBRARIES += \
                module-hal-detect.la
 endif
 
+if HAVE_HAL_COMPAT
+modlibexec_LTLIBRARIES += \
+               module-hal-detect.la
+endif
+
 if HAVE_UDEV
 modlibexec_LTLIBRARIES += \
                module-udev-detect.la
@@ -1576,10 +1589,16 @@ module_jack_source_la_LDFLAGS = $(MODULE_LDFLAGS)
 module_jack_source_la_LIBADD = $(AM_LIBADD) libpulsecore-@PA_MAJORMINORMICRO@.la $(JACK_LIBS) libpulsecommon-@PA_MAJORMINORMICRO@.la libpulse.la
 module_jack_source_la_CFLAGS = $(AM_CFLAGS) $(JACK_CFLAGS)
 
+if HAVE_HAL_COMPAT
+module_hal_detect_la_SOURCES = modules/module-hal-detect-compat.c
+module_hal_detect_la_LIBADD = $(AM_LIBADD) libpulsecore-@PA_MAJORMINORMICRO@.la libpulsecommon-@PA_MAJORMINORMICRO@.la libpulse.la
+module_hal_detect_la_CFLAGS = $(AM_CFLAGS)
+else
 module_hal_detect_la_SOURCES = modules/module-hal-detect.c
-module_hal_detect_la_LDFLAGS = $(MODULE_LDFLAGS)
 module_hal_detect_la_LIBADD = $(AM_LIBADD) $(HAL_LIBS) libpulsecore-@PA_MAJORMINORMICRO@.la libpulsecommon-@PA_MAJORMINORMICRO@.la libpulse.la
 module_hal_detect_la_CFLAGS = $(AM_CFLAGS) $(HAL_CFLAGS)
+endif
+module_hal_detect_la_LDFLAGS = $(MODULE_LDFLAGS)
 
 module_udev_detect_la_SOURCES = modules/module-udev-detect.c
 module_udev_detect_la_LDFLAGS = $(MODULE_LDFLAGS)
index 294be494ed073b655b5783e93952b4dad4bbed13..76b62e03e80f217fefc12bf4d934430868167a96 100644 (file)
@@ -57,24 +57,29 @@ void pa_drop_root(void) {
 
 #ifdef HAVE_GETUID
     uid_t uid;
+    gid_t gid;
 
+    pa_log_debug(_("Cleaning up privileges."));
     uid = getuid();
-    if (uid == 0 || geteuid() != 0)
-        return;
-
-    pa_log_info(_("Dropping root privileges."));
+    gid = getgid();
 
 #if defined(HAVE_SETRESUID)
     pa_assert_se(setresuid(uid, uid, uid) >= 0);
+    pa_assert_se(setresgid(gid, gid, gid) >= 0);
 #elif defined(HAVE_SETREUID)
     pa_assert_se(setreuid(uid, uid) >= 0);
+    pa_assert_se(setregid(gid, gid) >= 0);
 #else
     pa_assert_se(setuid(uid) >= 0);
     pa_assert_se(seteuid(uid) >= 0);
+    pa_assert_se(setgid(gid) >= 0);
+    pa_assert_se(setegid(gid) >= 0);
 #endif
 
     pa_assert_se(getuid() == uid);
     pa_assert_se(geteuid() == uid);
+    pa_assert_se(getgid() == gid);
+    pa_assert_se(getegid() == gid);
 #endif
 
 #ifdef HAVE_SYS_PRCTL_H
@@ -82,7 +87,7 @@ void pa_drop_root(void) {
 #endif
 
 #ifdef HAVE_SYS_CAPABILITY_H
-    {
+    if (uid != 0) {
         cap_t caps;
         pa_assert_se(caps = cap_init());
         pa_assert_se(cap_clear(caps) == 0);
index 9010f2f6528d085e6df7fb450bbfc5422f6660ca..9a87b5552846ed1ca62375643bc76ee45cef83ee 100644 (file)
@@ -441,11 +441,15 @@ int pa_daemon_conf_load(pa_daemon_conf *c, const char *filename) {
         { "high-priority",              pa_config_parse_bool,     &c->high_priority, NULL },
         { "realtime-scheduling",        pa_config_parse_bool,     &c->realtime_scheduling, NULL },
         { "disallow-module-loading",    pa_config_parse_bool,     &c->disallow_module_loading, NULL },
+        { "allow-module-loading",       pa_config_parse_not_bool, &c->disallow_module_loading, NULL },
         { "disallow-exit",              pa_config_parse_bool,     &c->disallow_exit, NULL },
+        { "allow-exit",                 pa_config_parse_not_bool, &c->disallow_exit, NULL },
         { "use-pid-file",               pa_config_parse_bool,     &c->use_pid_file, NULL },
         { "system-instance",            pa_config_parse_bool,     &c->system_instance, NULL },
         { "no-cpu-limit",               pa_config_parse_bool,     &c->no_cpu_limit, NULL },
+        { "cpu-limit",                  pa_config_parse_not_bool, &c->no_cpu_limit, NULL },
         { "disable-shm",                pa_config_parse_bool,     &c->disable_shm, NULL },
+        { "enable-shm",                 pa_config_parse_not_bool, &c->disable_shm, NULL },
         { "flat-volumes",               pa_config_parse_bool,     &c->flat_volumes, NULL },
         { "lock-memory",                pa_config_parse_bool,     &c->lock_memory, NULL },
         { "exit-idle-time",             pa_config_parse_int,      &c->exit_idle_time, NULL },
@@ -465,7 +469,9 @@ int pa_daemon_conf_load(pa_daemon_conf *c, const char *filename) {
         { "default-fragment-size-msec", parse_fragment_size_msec, c, NULL },
         { "nice-level",                 parse_nice_level,         c, NULL },
         { "disable-remixing",           pa_config_parse_bool,     &c->disable_remixing, NULL },
+        { "enable-remixing",            pa_config_parse_not_bool, &c->disable_remixing, NULL },
         { "disable-lfe-remixing",       pa_config_parse_bool,     &c->disable_lfe_remixing, NULL },
+        { "enable-lfe-remixing",        pa_config_parse_not_bool, &c->disable_lfe_remixing, NULL },
         { "load-default-script-file",   pa_config_parse_bool,     &c->load_default_script_file, NULL },
         { "shm-size-bytes",             pa_config_parse_size,     &c->shm_size, NULL },
         { "log-meta",                   pa_config_parse_bool,     &c->log_meta, NULL },
@@ -623,12 +629,12 @@ char *pa_daemon_conf_dump(pa_daemon_conf *c) {
     pa_strbuf_printf(s, "nice-level = %i\n", c->nice_level);
     pa_strbuf_printf(s, "realtime-scheduling = %s\n", pa_yes_no(c->realtime_scheduling));
     pa_strbuf_printf(s, "realtime-priority = %i\n", c->realtime_priority);
-    pa_strbuf_printf(s, "disallow-module-loading = %s\n", pa_yes_no(c->disallow_module_loading));
-    pa_strbuf_printf(s, "disallow-exit = %s\n", pa_yes_no(c->disallow_exit));
+    pa_strbuf_printf(s, "allow-module-loading = %s\n", pa_yes_no(!c->disallow_module_loading));
+    pa_strbuf_printf(s, "allow-exit = %s\n", pa_yes_no(!c->disallow_exit));
     pa_strbuf_printf(s, "use-pid-file = %s\n", pa_yes_no(c->use_pid_file));
     pa_strbuf_printf(s, "system-instance = %s\n", pa_yes_no(c->system_instance));
-    pa_strbuf_printf(s, "no-cpu-limit = %s\n", pa_yes_no(c->no_cpu_limit));
-    pa_strbuf_printf(s, "disable-shm = %s\n", pa_yes_no(c->disable_shm));
+    pa_strbuf_printf(s, "cpu-limit = %s\n", pa_yes_no(!c->no_cpu_limit));
+    pa_strbuf_printf(s, "enable-shm = %s\n", pa_yes_no(!c->disable_shm));
     pa_strbuf_printf(s, "flat-volumes = %s\n", pa_yes_no(c->flat_volumes));
     pa_strbuf_printf(s, "lock-memory = %s\n", pa_yes_no(c->lock_memory));
     pa_strbuf_printf(s, "exit-idle-time = %i\n", c->exit_idle_time);
@@ -639,8 +645,8 @@ char *pa_daemon_conf_dump(pa_daemon_conf *c) {
     pa_strbuf_printf(s, "log-target = %s\n", c->auto_log_target ? "auto" : (c->log_target == PA_LOG_SYSLOG ? "syslog" : "stderr"));
     pa_strbuf_printf(s, "log-level = %s\n", log_level_to_string[c->log_level]);
     pa_strbuf_printf(s, "resample-method = %s\n", pa_resample_method_to_string(c->resample_method));
-    pa_strbuf_printf(s, "disable-remixing = %s\n", pa_yes_no(c->disable_remixing));
-    pa_strbuf_printf(s, "disable-lfe-remixing = %s\n", pa_yes_no(c->disable_lfe_remixing));
+    pa_strbuf_printf(s, "enable-remixing = %s\n", pa_yes_no(!c->disable_remixing));
+    pa_strbuf_printf(s, "enable-lfe-remixing = %s\n", pa_yes_no(!c->disable_lfe_remixing));
     pa_strbuf_printf(s, "default-sample-format = %s\n", pa_sample_format_to_string(c->default_sample_spec.format));
     pa_strbuf_printf(s, "default-sample-rate = %u\n", c->default_sample_spec.rate);
     pa_strbuf_printf(s, "default-sample-channels = %u\n", c->default_sample_spec.channels);
index 6931359c1d1fb6aee15b160cebd1f6f83a52d75e..d8b58d8a0a40d729fa24ee388e82f79594322c60 100644 (file)
 
 ; daemonize = no
 ; fail = yes
-; disallow-module-loading = no
-; disallow-exit = no
+; allow-module-loading = yes
+; allow-exit = yes
 ; use-pid-file = yes
 ; system-instance = no
-; disable-shm = no
+; enable-shm = yes
 ; shm-size-bytes = 0 # setting this 0 will use the system-default, usually 64 MiB
 ; lock-memory = no
-; no-cpu-limit = no
+; cpu-limit = yes
 
 ; high-priority = yes
 ; nice-level = -11
@@ -51,8 +51,8 @@
 ; log-backtrace = 0
 
 ; resample-method = speex-float-3
-; disable-remixing = no
-; disable-lfe-remixing = yes
+; enable-remixing = yes
+; enable-lfe-remixing = no
 
 ; flat-volumes = yes
 
index 7a95195479be85cad229ea6b10131650b9700d9e..8521e720f2f0d2279871e8d1c68b9e5157c4a811 100644 (file)
 #include <dbus/dbus.h>
 #endif
 
-#ifdef __linux__
-#include <sys/personality.h>
-#endif
-
 #include <pulse/mainloop.h>
 #include <pulse/mainloop-signal.h>
 #include <pulse/timeval.h>
@@ -446,15 +442,12 @@ int main(int argc, char *argv[]) {
      * context we have been started. Let's cleanup our execution
      * context as good as possible */
 
-#ifdef __linux__
-    if (personality(PER_LINUX) < 0)
-        pa_log_warn("Uh, personality() failed: %s", pa_cstrerror(errno));
-#endif
-
+    pa_reset_personality();
     pa_drop_root();
     pa_close_all(passed_fd, -1);
     pa_reset_sigs(-1);
     pa_unblock_sigs(-1);
+    pa_reset_priority();
 
     setlocale(LC_ALL, "");
     pa_init_i18n();
diff --git a/src/daemon/pulseaudio-system.conf b/src/daemon/pulseaudio-system.conf
new file mode 100644 (file)
index 0000000..edddaf9
--- /dev/null
@@ -0,0 +1,37 @@
+<?xml version="1.0"?><!--*-nxml-*-->
+<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
+ "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
+
+<!--
+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 Lesser 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.
+-->
+
+<busconfig>
+
+  <!-- System-wide PulseAudio runs as 'pulse' user. This fragment is
+       not necessary for user PulseAudio instances. -->
+
+  <policy user="pulse">
+    <allow own="org.pulseaudio.Server"/>
+
+    <!-- Allow pulseaudio to talk to HAL for device detection -->
+    <allow send_destination="org.freedesktop.Hal" send_interface="org.freedesktop.Hal.Manager"/>
+    <allow send_destination="org.freedesktop.Hal" send_interface="org.freedesktop.Hal.Device"/>
+  </policy>
+
+</busconfig>
index c6c8acca2fc6a4515b35a9c070dbad1ae845fa1d..95b2803abf1c8aee6c3fb6e38324f7e30607bba1 100644 (file)
@@ -123,13 +123,18 @@ pa_cvolume_avg_mask;
 pa_cvolume_channels_equal_to;
 pa_cvolume_compatible;
 pa_cvolume_compatible_with_channel_map;
+pa_cvolume_dec;
 pa_cvolume_equal;
 pa_cvolume_get_balance;
 pa_cvolume_get_fade;
 pa_cvolume_get_position;
+pa_cvolume_inc;
 pa_cvolume_init;
 pa_cvolume_max;
 pa_cvolume_max_mask;
+pa_cvolume_merge;
+pa_cvolume_min;
+pa_cvolume_min_mask;
 pa_cvolume_remap;
 pa_cvolume_scale;
 pa_cvolume_scale_mask;
index 6a0b4ab780391e41afe14e475405fb8f536b1b21..a4c2ee0f7adb7b68280069b5291bae5ed06fb0aa 100644 (file)
@@ -2838,9 +2838,9 @@ static int mapping_verify(pa_alsa_mapping *m, const pa_channel_map *bonus) {
 
     if (bonus) {
         if (pa_channel_map_equal(&m->channel_map, bonus))
-            m->priority += 5000;
+            m->priority += 50;
         else if (m->channel_map.channels == bonus->channels)
-            m->priority += 4000;
+            m->priority += 30;
     }
 
     return 0;
index 1c38430f22b36fa2ad2f3add191dee19b0152a38..e3707ae7e3d660b52025a9bd93aaea54d1ce0281 100644 (file)
@@ -68,6 +68,8 @@
 #define TSCHED_MIN_SLEEP_USEC (10*PA_USEC_PER_MSEC)               /* 10ms -- Sleep at least 10ms on each iteration */
 #define TSCHED_MIN_WAKEUP_USEC (4*PA_USEC_PER_MSEC)               /* 4ms  -- Wakeup at least this long before the buffer runs empty*/
 
+#define VOLUME_ACCURACY (PA_VOLUME_NORM/100)  /* don't require volume adjustments to be perfectly correct. don't necessarily extend granularity in software unless the differences get greater than this level */
+
 struct userdata {
     pa_core *core;
     pa_module *module;
@@ -1007,7 +1009,7 @@ 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, FALSE);
+        pa_sink_get_volume(u->sink, TRUE);
         pa_sink_get_mute(u->sink, TRUE);
     }
 
@@ -1034,15 +1036,11 @@ static void sink_get_volume_cb(pa_sink *s) {
     if (pa_cvolume_equal(&u->hardware_volume, &r))
         return;
 
-    s->virtual_volume = u->hardware_volume = r;
-
-    if (u->mixer_path->has_dB) {
-        pa_cvolume reset;
+    s->real_volume = u->hardware_volume = r;
 
-        /* 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 */
+    if (u->mixer_path->has_dB)
+        pa_sink_set_soft_volume(s, NULL);
 }
 
 static void sink_set_volume_cb(pa_sink *s) {
@@ -1055,7 +1053,7 @@ static void sink_set_volume_cb(pa_sink *s) {
     pa_assert(u->mixer_handle);
 
     /* Shift up by the base volume */
-    pa_sw_cvolume_divide_scalar(&r, &s->virtual_volume, s->base_volume);
+    pa_sw_cvolume_divide_scalar(&r, &s->real_volume, s->base_volume);
 
     if (pa_alsa_path_set_volume(u->mixer_path, u->mixer_handle, &s->channel_map, &r) < 0)
         return;
@@ -1066,13 +1064,26 @@ static void sink_set_volume_cb(pa_sink *s) {
     u->hardware_volume = r;
 
     if (u->mixer_path->has_dB) {
+        pa_cvolume new_soft_volume;
+        pa_bool_t accurate_enough;
 
         /* Match exactly what the user requested by software */
-        pa_sw_cvolume_divide(&s->soft_volume, &s->virtual_volume, &u->hardware_volume);
+        pa_sw_cvolume_divide(&new_soft_volume, &s->real_volume, &u->hardware_volume);
+
+        /* If the adjustment to do in software is only minimal we
+         * can skip it. That saves us CPU at the expense of a bit of
+         * accuracy */
+        accurate_enough =
+            (pa_cvolume_min(&new_soft_volume) >= (PA_VOLUME_NORM - VOLUME_ACCURACY)) &&
+            (pa_cvolume_max(&new_soft_volume) <= (PA_VOLUME_NORM + VOLUME_ACCURACY));
 
-        pa_log_debug("Requested volume: %s", pa_cvolume_snprint(t, sizeof(t), &s->virtual_volume));
+        pa_log_debug("Requested volume: %s", pa_cvolume_snprint(t, sizeof(t), &s->real_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));
+        pa_log_debug("Calculated software volume: %s (accurate-enough=%s)", pa_cvolume_snprint(t, sizeof(t), &new_soft_volume),
+                     pa_yes_no(accurate_enough));
+
+        if (!accurate_enough)
+            s->soft_volume = new_soft_volume;
 
     } else {
         pa_log_debug("Wrote hardware volume: %s", pa_cvolume_snprint(t, sizeof(t), &r));
@@ -1080,7 +1091,7 @@ static void sink_set_volume_cb(pa_sink *s) {
         /* We can't match exactly what the user requested, hence let's
          * at least tell the user about it */
 
-        s->virtual_volume = r;
+        s->real_volume = r;
     }
 }
 
@@ -1292,7 +1303,8 @@ static void thread_func(void *userdata) {
                      * we have filled the buffer at least once
                      * completely.*/
 
-                    pa_log_debug("Cutting sleep time for the initial iterations by half.");
+                    if (pa_log_ratelimit())
+                        pa_log_debug("Cutting sleep time for the initial iterations by half.");
                     sleep_usec /= 2;
                 }
 
index 9a51f85729d17691f46d88189a26f1c3f4bca344..7da37553320977303038fec2a96fd3e22dc1eb95 100644 (file)
@@ -65,6 +65,8 @@
 #define TSCHED_MIN_SLEEP_USEC (10*PA_USEC_PER_MSEC)          /* 10ms */
 #define TSCHED_MIN_WAKEUP_USEC (4*PA_USEC_PER_MSEC)          /* 4ms */
 
+#define VOLUME_ACCURACY (PA_VOLUME_NORM/100)
+
 struct userdata {
     pa_core *core;
     pa_module *module;
@@ -987,15 +989,11 @@ static void source_get_volume_cb(pa_source *s) {
     if (pa_cvolume_equal(&u->hardware_volume, &r))
         return;
 
-    s->virtual_volume = u->hardware_volume = r;
-
-    if (u->mixer_path->has_dB) {
-        pa_cvolume reset;
+    s->volume = u->hardware_volume = r;
 
-        /* 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 */
+    if (u->mixer_path->has_dB)
+        pa_source_set_soft_volume(s, NULL);
 }
 
 static void source_set_volume_cb(pa_source *s) {
@@ -1008,7 +1006,7 @@ static void source_set_volume_cb(pa_source *s) {
     pa_assert(u->mixer_handle);
 
     /* Shift up by the base volume */
-    pa_sw_cvolume_divide_scalar(&r, &s->virtual_volume, s->base_volume);
+    pa_sw_cvolume_divide_scalar(&r, &s->volume, s->base_volume);
 
     if (pa_alsa_path_set_volume(u->mixer_path, u->mixer_handle, &s->channel_map, &r) < 0)
         return;
@@ -1019,13 +1017,26 @@ static void source_set_volume_cb(pa_source *s) {
     u->hardware_volume = r;
 
     if (u->mixer_path->has_dB) {
+        pa_cvolume new_soft_volume;
+        pa_bool_t accurate_enough;
 
         /* Match exactly what the user requested by software */
-        pa_sw_cvolume_divide(&s->soft_volume, &s->virtual_volume, &u->hardware_volume);
+        pa_sw_cvolume_divide(&new_soft_volume, &s->volume, &u->hardware_volume);
+
+        /* If the adjustment to do in software is only minimal we
+         * can skip it. That saves us CPU at the expense of a bit of
+         * accuracy */
+        accurate_enough =
+            (pa_cvolume_min(&new_soft_volume) >= (PA_VOLUME_NORM - VOLUME_ACCURACY)) &&
+            (pa_cvolume_max(&new_soft_volume) <= (PA_VOLUME_NORM + VOLUME_ACCURACY));
 
-        pa_log_debug("Requested volume: %s", pa_cvolume_snprint(t, sizeof(t), &s->virtual_volume));
+        pa_log_debug("Requested volume: %s", pa_cvolume_snprint(t, sizeof(t), &s->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));
+        pa_log_debug("Calculated software volume: %s (accurate-enough=%s)", pa_cvolume_snprint(t, sizeof(t), &new_soft_volume),
+                     pa_yes_no(accurate_enough));
+
+        if (!accurate_enough)
+            s->soft_volume = new_soft_volume;
 
     } else {
         pa_log_debug("Wrote hardware volume: %s", pa_cvolume_snprint(t, sizeof(t), &r));
@@ -1033,7 +1044,7 @@ static void source_set_volume_cb(pa_source *s) {
         /* We can't match exactly what the user requested, hence let's
          * at least tell the user about it */
 
-        s->virtual_volume = r;
+        s->volume = r;
     }
 }
 
index cc1185f4f95906bea78c7ebaf30068a15bcbe35a..3c6ce80366fde0b74a2a56efe4bef4d50d7b1305 100644 (file)
@@ -104,8 +104,8 @@ switch = select
 
 [Option External Amplifier:on]
 name = output-amplifier-on
-priority = 0
+priority = 10
 
 [Option External Amplifier:off]
 name = output-amplifier-off
-priority = 10
+priority = 0
index 66e1c31eb6eea5c6cc027831192e250fb7a90819..f576823da12021430052c9cfa62a067365ea5727 100644 (file)
@@ -122,9 +122,9 @@ static pa_bool_t device_is_audio(pa_bluetooth_device *d) {
 
     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);
+        (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) {
@@ -226,10 +226,6 @@ static int parse_device_property(pa_bluetooth_discovery *y, pa_bluetooth_device
                     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"));
@@ -239,6 +235,10 @@ static int parse_device_property(pa_bluetooth_discovery *y, pa_bluetooth_device
                         send_and_add_to_pending(y, d, m, get_properties_reply);
                     }
 
+                    /* 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);
+
                     if (!dbus_message_iter_next(&ai))
                         break;
                 }
@@ -758,7 +758,7 @@ pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *c) {
 
     if (pa_dbus_add_matches(
                 pa_dbus_connection_get(y->connection), &err,
-                "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged'",
+                "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0='org.bluez'",
                 "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'",
@@ -809,7 +809,7 @@ void pa_bluetooth_discovery_unref(pa_bluetooth_discovery *y) {
 
     if (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.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0='org.bluez'",
                                "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'",
index 265caf40442147151ba51615ccec18c4378d71c3..f15f21701a640cafb779544d36ceeab75356b615 100644 (file)
@@ -59,8 +59,7 @@ typedef enum pa_bt_audio_state {
     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_PLAYING
 } pa_bt_audio_state_t;
 
 struct pa_bluetooth_device {
index e682997f76e0fef7a253ba0a8dc6a712f001e04d..4e23862c5752998972688c44ad404c1e21871f44 100644 (file)
@@ -881,7 +881,7 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
                 *((pa_usec_t*) data) = wi > ri ? wi - ri : 0;
             }
 
-            *((pa_usec_t*) data) += u->sink->fixed_latency;
+            *((pa_usec_t*) data) += u->sink->thread_info.fixed_latency;
             return 0;
         }
     }
@@ -943,7 +943,7 @@ static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t off
             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;
+            *((pa_usec_t*) data) = (wi > ri ? wi - ri : 0) + u->source->thread_info.fixed_latency;
             return 0;
         }
 
@@ -1262,11 +1262,11 @@ static void thread_func(void *userdata) {
     if (u->core->realtime_scheduling)
         pa_make_realtime(u->core->realtime_priority);
 
+    pa_thread_mq_install(&u->thread_mq);
+
     if (start_stream_fd(u) < 0)
         goto fail;
 
-    pa_thread_mq_install(&u->thread_mq);
-
     for (;;) {
         struct pollfd *pollfd;
         int ret;
@@ -1319,18 +1319,21 @@ static void thread_func(void *userdata) {
                         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);
 
-                            pa_log_warn("Skipping %llu us (= %llu bytes) in audio stream",
-                                        (unsigned long long) skip_usec,
-                                        (unsigned long long) skip_bytes);
+                            if (skip_bytes > 0) {
+                                pa_memchunk tmp;
+
+                                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;
+                                pa_sink_render_full(u->sink, skip_bytes, &tmp);
+                                pa_memblock_unref(tmp.memblock);
+                                u->write_index += skip_bytes;
+                            }
                         }
 
                         do_write = 1;
@@ -1473,12 +1476,12 @@ static void sink_set_volume_cb(pa_sink *s) {
     if (u->profile != PROFILE_HSP)
         return;
 
-    gain = (pa_cvolume_max(&s->virtual_volume) * 15) / PA_VOLUME_NORM;
+    gain = (pa_cvolume_max(&s->real_volume) * 15) / PA_VOLUME_NORM;
 
     if (gain > 15)
         gain = 15;
 
-    pa_cvolume_set(&s->virtual_volume, u->sample_spec.channels, (pa_volume_t) (gain * PA_VOLUME_NORM / 15));
+    pa_cvolume_set(&s->real_volume, u->sample_spec.channels, (pa_volume_t) (gain * PA_VOLUME_NORM / 15));
 
     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));
@@ -1497,12 +1500,12 @@ static void source_set_volume_cb(pa_source *s) {
     if (u->profile != PROFILE_HSP)
         return;
 
-    gain = (pa_cvolume_max(&s->virtual_volume) * 15) / PA_VOLUME_NORM;
+    gain = (pa_cvolume_max(&s->volume) * 15) / PA_VOLUME_NORM;
 
     if (gain > 15)
         gain = 15;
 
-    pa_cvolume_set(&s->virtual_volume, u->sample_spec.channels, (pa_volume_t) (gain * PA_VOLUME_NORM / 15));
+    pa_cvolume_set(&s->volume, u->sample_spec.channels, (pa_volume_t) (gain * PA_VOLUME_NORM / 15));
 
     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));
@@ -1736,7 +1739,8 @@ static void shutdown_bt(struct userdata *u) {
     if (u->service_fd >= 0) {
         pa_close(u->service_fd);
         u->service_fd = -1;
-        u->service_write_type = u->service_write_type = 0;
+        u->service_write_type = 0;
+        u->service_read_type = 0;
     }
 
     if (u->write_memchunk.memblock) {
@@ -1752,7 +1756,8 @@ static int init_bt(struct userdata *u) {
     shutdown_bt(u);
 
     u->stream_write_type = 0;
-    u->service_write_type = u->service_write_type = 0;
+    u->service_write_type = 0;
+    u->service_read_type = 0;
 
     if ((u->service_fd = bt_audio_service_open()) < 0) {
         pa_log_error("Couldn't connect to bluetooth audio service");
index 16de6890254a08c503fa8d7ccfb95a28e42ce4b2..582cbce193ce0ceb83664280b11a78ed10318aba 100644 (file)
@@ -92,6 +92,8 @@ struct output {
     pa_sink *sink;
     pa_sink_input *sink_input;
 
+    pa_bool_t ignore_state_change;
+
     pa_asyncmsgq *inq,    /* Message queue from the sink thread to this sink input */
                  *outq;   /* Message queue from this sink input to the sink thread */
     pa_rtpoll_item *inq_rtpoll_item_read, *inq_rtpoll_item_write;
@@ -99,9 +101,12 @@ struct output {
 
     pa_memblockq *memblockq;
 
+    /* For communication of the stream latencies to the main thread */
     pa_usec_t total_latency;
 
+    /* For coomunication of the stream parameters to the sink thread */
     pa_atomic_t max_request;
+    pa_atomic_t requested_latency;
 
     PA_LLIST_FIELDS(struct output);
 };
@@ -125,8 +130,6 @@ struct userdata {
 
     pa_resample_method_t resample_method;
 
-    struct timeval adjust_timestamp;
-
     pa_usec_t block_usec;
 
     pa_idxset* outputs; /* managed in main context */
@@ -146,13 +149,16 @@ enum {
     SINK_MESSAGE_REMOVE_OUTPUT,
     SINK_MESSAGE_NEED,
     SINK_MESSAGE_UPDATE_LATENCY,
-    SINK_MESSAGE_UPDATE_MAX_REQUEST
+    SINK_MESSAGE_UPDATE_MAX_REQUEST,
+    SINK_MESSAGE_UPDATE_REQUESTED_LATENCY
 };
 
 enum {
     SINK_INPUT_MESSAGE_POST = PA_SINK_INPUT_MESSAGE_MAX,
 };
 
+static void output_disable(struct output *o);
+static void output_enable(struct output *o);
 static void output_free(struct output *o);
 static int output_create_sink_input(struct output *o);
 
@@ -172,7 +178,7 @@ static void adjust_rates(struct userdata *u) {
     if (!PA_SINK_IS_OPENED(pa_sink_get_state(u->sink)))
         return;
 
-    for (o = pa_idxset_first(u->outputs, &idx); o; o = pa_idxset_next(u->outputs, &idx)) {
+    PA_IDXSET_FOREACH(o, u->outputs, idx) {
         pa_usec_t sink_latency;
 
         if (!o->sink_input || !PA_SINK_IS_OPENED(pa_sink_get_state(o->sink)))
@@ -189,6 +195,11 @@ static void adjust_rates(struct userdata *u) {
 
         avg_total_latency += o->total_latency;
         n++;
+
+        pa_log_debug("[%s] total=%0.2fms sink=%0.2fms ", o->sink->name, (double) o->total_latency / PA_USEC_PER_MSEC, (double) sink_latency / PA_USEC_PER_MSEC);
+
+        if (o->total_latency > 10*PA_USEC_PER_SEC)
+            pa_log_warn("[%s] Total latency of output is very high (%0.2fms), most likely the audio timing in one of your drivers is broken.", o->sink->name, (double) o->total_latency / PA_USEC_PER_MSEC);
     }
 
     if (min_total_latency == (pa_usec_t) -1)
@@ -203,7 +214,7 @@ static void adjust_rates(struct userdata *u) {
 
     base_rate = u->sink->sample_spec.rate;
 
-    for (o = pa_idxset_first(u->outputs, &idx); o; o = pa_idxset_next(u->outputs, &idx)) {
+    PA_IDXSET_FOREACH(o, u->outputs, idx) {
         uint32_t r = base_rate;
 
         if (!o->sink_input || !PA_SINK_IS_OPENED(pa_sink_get_state(o->sink)))
@@ -215,10 +226,10 @@ static void adjust_rates(struct userdata *u) {
             r += (uint32_t) ((((double) (o->total_latency - target_latency))/(double)u->adjust_time)*(double)r/PA_USEC_PER_SEC);
 
         if (r < (uint32_t) (base_rate*0.9) || r > (uint32_t) (base_rate*1.1)) {
-            pa_log_warn("[%s] sample rates too different, not adjusting (%u vs. %u).", pa_proplist_gets(o->sink_input->proplist, PA_PROP_MEDIA_NAME), base_rate, r);
+            pa_log_warn("[%s] sample rates too different, not adjusting (%u vs. %u).", o->sink_input->sink->name, base_rate, r);
             pa_sink_input_set_rate(o->sink_input, base_rate);
         } else {
-            pa_log_info("[%s] new rate is %u Hz; ratio is %0.3f; latency is %0.0f usec.", pa_proplist_gets(o->sink_input->proplist, PA_PROP_MEDIA_NAME), r, (double) r / base_rate, (float) o->total_latency);
+            pa_log_info("[%s] new rate is %u Hz; ratio is %0.3f; latency is %0.0f usec.", o->sink_input->sink->name, r, (double) r / base_rate, (float) o->total_latency);
             pa_sink_input_set_rate(o->sink_input, r);
         }
     }
@@ -355,18 +366,15 @@ static void render_memblock(struct userdata *u, struct output *o, size_t length)
         u->thread_info.counter += chunk.length;
 
         /* OK, let's send this data to the other threads */
-        for (j = u->thread_info.active_outputs; j; j = j->next)
-
-            /* Send to other outputs, which are not the requesting
-             * one */
+        PA_LLIST_FOREACH(j, u->thread_info.active_outputs) {
+            if (j == o)
+                continue;
 
-            if (j != o)
-                pa_asyncmsgq_post(j->inq, PA_MSGOBJECT(j->sink_input), SINK_INPUT_MESSAGE_POST, NULL, 0, &chunk, NULL);
+            pa_asyncmsgq_post(j->inq, PA_MSGOBJECT(j->sink_input), SINK_INPUT_MESSAGE_POST, NULL, 0, &chunk, NULL);
+        }
 
         /* And place it directly into the requesting output's queue */
-        if (o)
-            pa_memblockq_push_align(o->memblockq, &chunk);
-
+        pa_memblockq_push_align(o->memblockq, &chunk);
         pa_memblock_unref(chunk.memblock);
     }
 }
@@ -402,10 +410,18 @@ static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk
     /* If necessary, get some new data */
     request_memblock(o, nbytes);
 
+    /* pa_log("%s q size is %u + %u (%u/%u)", */
+    /*        i->sink->name, */
+    /*        pa_memblockq_get_nblocks(o->memblockq), */
+    /*        pa_memblockq_get_nblocks(i->thread_info.render_memblockq), */
+    /*        pa_memblockq_get_maxrewind(o->memblockq), */
+    /*        pa_memblockq_get_maxrewind(i->thread_info.render_memblockq)); */
+
     if (pa_memblockq_peek(o->memblockq, chunk) < 0)
         return -1;
 
     pa_memblockq_drop(o->memblockq, chunk->length);
+
     return 0;
 }
 
@@ -440,13 +456,35 @@ static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
         return;
 
     pa_atomic_store(&o->max_request, (int) nbytes);
-
     pa_asyncmsgq_post(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_UPDATE_MAX_REQUEST, NULL, 0, NULL, NULL);
 }
 
+/* Called from thread context */
+static void sink_input_update_sink_requested_latency_cb(pa_sink_input *i) {
+    struct output *o;
+    pa_usec_t c;
+
+    pa_assert(i);
+
+    pa_sink_input_assert_ref(i);
+    pa_assert_se(o = i->userdata);
+
+    c = pa_sink_get_requested_latency_within_thread(i->sink);
+
+    if (c == (pa_usec_t) -1)
+        c = i->sink->thread_info.max_latency;
+
+    if (pa_atomic_load(&o->requested_latency) == (int) c)
+        return;
+
+    pa_atomic_store(&o->requested_latency, (int) c);
+    pa_asyncmsgq_post(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_UPDATE_REQUESTED_LATENCY, NULL, 0, NULL, NULL);
+}
+
 /* Called from I/O thread context */
 static void sink_input_attach_cb(pa_sink_input *i) {
     struct output *o;
+    pa_usec_t c;
 
     pa_sink_input_assert_ref(i);
     pa_assert_se(o = i->userdata);
@@ -455,14 +493,24 @@ static void sink_input_attach_cb(pa_sink_input *i) {
     pa_assert(!o->inq_rtpoll_item_read && !o->outq_rtpoll_item_write);
 
     o->inq_rtpoll_item_read = pa_rtpoll_item_new_asyncmsgq_read(
-            i->sink->rtpoll,
+            i->sink->thread_info.rtpoll,
             PA_RTPOLL_LATE,  /* This one is not that important, since we check for data in _peek() anyway. */
             o->inq);
 
     o->outq_rtpoll_item_write = pa_rtpoll_item_new_asyncmsgq_write(
-            i->sink->rtpoll,
+            i->sink->thread_info.rtpoll,
             PA_RTPOLL_EARLY,
             o->outq);
+
+    pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
+
+    pa_atomic_store(&o->max_request, (int) pa_sink_input_get_max_request(i));
+
+    c = pa_sink_get_requested_latency_within_thread(i->sink);
+    pa_atomic_store(&o->requested_latency, (int) (c == (pa_usec_t) -1 ? 0 : c));
+
+    pa_asyncmsgq_post(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_UPDATE_MAX_REQUEST, NULL, 0, NULL, NULL);
+    pa_asyncmsgq_post(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_UPDATE_REQUESTED_LATENCY, NULL, 0, NULL, NULL);
 }
 
 /* Called from I/O thread context */
@@ -472,14 +520,15 @@ static void sink_input_detach_cb(pa_sink_input *i) {
     pa_sink_input_assert_ref(i);
     pa_assert_se(o = i->userdata);
 
-    /* Shut down the queue from the sink thread to us */
-    pa_assert(o->inq_rtpoll_item_read && o->outq_rtpoll_item_write);
-
-    pa_rtpoll_item_free(o->inq_rtpoll_item_read);
-    o->inq_rtpoll_item_read = NULL;
+    if (o->inq_rtpoll_item_read) {
+        pa_rtpoll_item_free(o->inq_rtpoll_item_read);
+        o->inq_rtpoll_item_read = NULL;
+    }
 
-    pa_rtpoll_item_free(o->outq_rtpoll_item_write);
-    o->outq_rtpoll_item_write = NULL;
+    if (o->outq_rtpoll_item_write) {
+        pa_rtpoll_item_free(o->outq_rtpoll_item_write);
+        o->outq_rtpoll_item_write = NULL;
+    }
 }
 
 /* Called from main context */
@@ -493,20 +542,6 @@ static void sink_input_kill_cb(pa_sink_input *i) {
     output_free(o);
 }
 
-/* Called from IO thread context */
-static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
-    struct userdata *u;
-
-    pa_sink_input_assert_ref(i);
-    pa_assert_se(u = i->userdata);
-
-    /* If we are added for the first time, ask for a rewinding so that
-     * we are heard right-away. */
-    if (PA_SINK_INPUT_IS_LINKED(state) &&
-        i->thread_info.state == PA_SINK_INPUT_INIT)
-        pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
-}
-
 /* Called from thread context */
 static int sink_input_process_msg(pa_msgobject *obj, int code, void *data, int64_t offset, pa_memchunk *chunk) {
     struct output *o = PA_SINK_INPUT(obj)->userdata;
@@ -536,37 +571,6 @@ static int sink_input_process_msg(pa_msgobject *obj, int code, void *data, int64
     return pa_sink_input_process_msg(obj, code, data, offset, chunk);
 }
 
-/* Called from main context */
-static void disable_output(struct output *o) {
-    pa_assert(o);
-
-    if (!o->sink_input)
-        return;
-
-    pa_sink_input_unlink(o->sink_input);
-    pa_asyncmsgq_send(o->userdata->sink->asyncmsgq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_REMOVE_OUTPUT, o, 0, NULL);
-    pa_sink_input_unref(o->sink_input);
-    o->sink_input = NULL;
-}
-
-/* Called from main context */
-static void enable_output(struct output *o) {
-    pa_assert(o);
-
-    if (o->sink_input)
-        return;
-
-    if (output_create_sink_input(o) >= 0) {
-
-        pa_memblockq_flush_write(o->memblockq);
-
-        pa_sink_input_put(o->sink_input);
-
-        if (o->userdata->sink && PA_SINK_IS_LINKED(pa_sink_get_state(o->userdata->sink)))
-            pa_asyncmsgq_send(o->userdata->sink->asyncmsgq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_ADD_OUTPUT, o, 0, NULL);
-    }
-}
-
 /* Called from main context */
 static void suspend(struct userdata *u) {
     struct output *o;
@@ -575,8 +579,8 @@ static void suspend(struct userdata *u) {
     pa_assert(u);
 
     /* Let's suspend by unlinking all streams */
-    for (o = pa_idxset_first(u->outputs, &idx); o; o = pa_idxset_next(u->outputs, &idx))
-        disable_output(o);
+    PA_IDXSET_FOREACH(o, u->outputs, idx)
+        output_disable(o);
 
     pa_log_info("Device suspended...");
 }
@@ -589,13 +593,8 @@ static void unsuspend(struct userdata *u) {
     pa_assert(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_SUSPEND_IDLE);
-
-        if (PA_SINK_IS_OPENED(pa_sink_get_state(o->sink)))
-            enable_output(o);
-    }
+    PA_IDXSET_FOREACH(o, u->outputs, idx)
+        output_enable(o);
 
     pa_log_info("Resumed successfully...");
 }
@@ -639,7 +638,13 @@ static void update_max_request(struct userdata *u) {
     size_t max_request = 0;
     struct output *o;
 
-    for (o = u->thread_info.active_outputs; o; o = o->next) {
+    pa_assert(u);
+    pa_sink_assert_io_context(u->sink);
+
+    /* Collects the max_request values of all streams and sets the
+     * largest one locally */
+
+    PA_LLIST_FOREACH(o, u->thread_info.active_outputs) {
         size_t mr = (size_t) pa_atomic_load(&o->max_request);
 
         if (mr > max_request)
@@ -652,6 +657,67 @@ static void update_max_request(struct userdata *u) {
     pa_sink_set_max_request_within_thread(u->sink, max_request);
 }
 
+/* Called from IO context */
+static void update_fixed_latency(struct userdata *u) {
+    pa_usec_t fixed_latency = 0;
+    struct output *o;
+
+    pa_assert(u);
+    pa_sink_assert_io_context(u->sink);
+
+    /* Collects the requested_latency values of all streams and sets
+     * the largest one as fixed_latency locally */
+
+    PA_LLIST_FOREACH(o, u->thread_info.active_outputs) {
+        pa_usec_t rl = (size_t) pa_atomic_load(&o->requested_latency);
+
+        if (rl > fixed_latency)
+            fixed_latency = rl;
+    }
+
+    if (fixed_latency <= 0)
+        fixed_latency = u->block_usec;
+
+    pa_sink_set_fixed_latency_within_thread(u->sink, fixed_latency);
+}
+
+/* Called from thread context of the io thread */
+static void output_add_within_thread(struct output *o) {
+    pa_assert(o);
+    pa_sink_assert_io_context(o->sink);
+
+    PA_LLIST_PREPEND(struct output, o->userdata->thread_info.active_outputs, o);
+
+    pa_assert(!o->outq_rtpoll_item_read && !o->inq_rtpoll_item_write);
+
+    o->outq_rtpoll_item_read = pa_rtpoll_item_new_asyncmsgq_read(
+            o->userdata->rtpoll,
+            PA_RTPOLL_EARLY-1,  /* This item is very important */
+            o->outq);
+    o->inq_rtpoll_item_write = pa_rtpoll_item_new_asyncmsgq_write(
+            o->userdata->rtpoll,
+            PA_RTPOLL_EARLY,
+            o->inq);
+}
+
+/* Called from thread context of the io thread */
+static void output_remove_within_thread(struct output *o) {
+    pa_assert(o);
+    pa_sink_assert_io_context(o->sink);
+
+    PA_LLIST_REMOVE(struct output, o->userdata->thread_info.active_outputs, o);
+
+    if (o->outq_rtpoll_item_read) {
+        pa_rtpoll_item_free(o->outq_rtpoll_item_read);
+        o->outq_rtpoll_item_read = NULL;
+    }
+
+    if (o->inq_rtpoll_item_write) {
+        pa_rtpoll_item_free(o->inq_rtpoll_item_write);
+        o->inq_rtpoll_item_write = NULL;
+    }
+}
+
 /* Called from thread context of the 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;
@@ -684,42 +750,17 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
             return 0;
         }
 
-        case SINK_MESSAGE_ADD_OUTPUT: {
-            struct output *op = data;
-
-            PA_LLIST_PREPEND(struct output, u->thread_info.active_outputs, op);
-
-            pa_assert(!op->outq_rtpoll_item_read && !op->inq_rtpoll_item_write);
-
-            op->outq_rtpoll_item_read = pa_rtpoll_item_new_asyncmsgq_read(
-                    u->rtpoll,
-                    PA_RTPOLL_EARLY-1,  /* This item is very important */
-                    op->outq);
-            op->inq_rtpoll_item_write = pa_rtpoll_item_new_asyncmsgq_write(
-                    u->rtpoll,
-                    PA_RTPOLL_EARLY,
-                    op->inq);
-
+        case SINK_MESSAGE_ADD_OUTPUT:
+            output_add_within_thread(data);
             update_max_request(u);
+            update_fixed_latency(u);
             return 0;
-        }
-
-        case SINK_MESSAGE_REMOVE_OUTPUT: {
-            struct output *op = data;
-
-            PA_LLIST_REMOVE(struct output, u->thread_info.active_outputs, op);
-
-            pa_assert(op->outq_rtpoll_item_read && op->inq_rtpoll_item_write);
-
-            pa_rtpoll_item_free(op->outq_rtpoll_item_read);
-            op->outq_rtpoll_item_read = NULL;
-
-            pa_rtpoll_item_free(op->inq_rtpoll_item_write);
-            op->inq_rtpoll_item_write = NULL;
 
+        case SINK_MESSAGE_REMOVE_OUTPUT:
+            output_remove_within_thread(data);
             update_max_request(u);
+            update_fixed_latency(u);
             return 0;
-        }
 
         case SINK_MESSAGE_NEED:
             render_memblock(u, (struct output*) data, (size_t) offset);
@@ -741,10 +782,13 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
         }
 
         case SINK_MESSAGE_UPDATE_MAX_REQUEST:
-
             update_max_request(u);
             break;
-    }
+
+        case SINK_MESSAGE_UPDATE_REQUESTED_LATENCY:
+            update_fixed_latency(u);
+            break;
+}
 
     return pa_sink_process_msg(o, code, data, offset, chunk);
 }
@@ -767,7 +811,7 @@ static void update_description(struct userdata *u) {
 
     t = pa_xstrdup("Simultaneous output to");
 
-    for (o = pa_idxset_first(u->outputs, &idx); o; o = pa_idxset_next(u->outputs, &idx)) {
+    PA_IDXSET_FOREACH(o, u->outputs, idx) {
         char *e;
 
         if (first) {
@@ -802,7 +846,7 @@ static int output_create_sink_input(struct output *o) {
     data.module = o->userdata->module;
     data.resample_method = o->userdata->resample_method;
 
-    pa_sink_input_new(&o->sink_input, o->userdata->core, &data, PA_SINK_INPUT_VARIABLE_RATE|PA_SINK_INPUT_DONT_MOVE);
+    pa_sink_input_new(&o->sink_input, o->userdata->core, &data, PA_SINK_INPUT_VARIABLE_RATE|PA_SINK_INPUT_DONT_MOVE|PA_SINK_INPUT_NO_CREATE_ON_SUSPEND);
 
     pa_sink_input_new_data_done(&data);
 
@@ -812,9 +856,9 @@ static int output_create_sink_input(struct output *o) {
     o->sink_input->parent.process_msg = sink_input_process_msg;
     o->sink_input->pop = sink_input_pop_cb;
     o->sink_input->process_rewind = sink_input_process_rewind_cb;
-    o->sink_input->state_change = sink_input_state_change_cb;
     o->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
     o->sink_input->update_max_request = sink_input_update_max_request_cb;
+    o->sink_input->update_sink_requested_latency = sink_input_update_sink_requested_latency_cb;
     o->sink_input->attach = sink_input_attach_cb;
     o->sink_input->detach = sink_input_detach_cb;
     o->sink_input->kill = sink_input_kill_cb;
@@ -825,22 +869,19 @@ static int output_create_sink_input(struct output *o) {
     return 0;
 }
 
+/* Called from main context */
 static struct output *output_new(struct userdata *u, pa_sink *sink) {
     struct output *o;
-    pa_sink_state_t state;
 
     pa_assert(u);
     pa_assert(sink);
     pa_assert(u->sink);
 
-    o = pa_xnew(struct output, 1);
+    o = pa_xnew0(struct output, 1);
     o->userdata = u;
     o->inq = pa_asyncmsgq_new(0);
     o->outq = pa_asyncmsgq_new(0);
-    o->inq_rtpoll_item_write = o->inq_rtpoll_item_read = NULL;
-    o->outq_rtpoll_item_write = o->outq_rtpoll_item_read = NULL;
     o->sink = sink;
-    o->sink_input = NULL;
     o->memblockq = pa_memblockq_new(
             0,
             MEMBLOCKQ_MAXLENGTH,
@@ -850,84 +891,135 @@ static struct output *output_new(struct userdata *u, pa_sink *sink) {
             0,
             0,
             NULL);
-    pa_atomic_store(&o->max_request, 0);
-    PA_LLIST_INIT(struct output, o);
 
     pa_assert_se(pa_idxset_put(u->outputs, o, NULL) == 0);
+    update_description(u);
 
-    state = pa_sink_get_state(u->sink);
-
-    if (state != PA_SINK_INIT)
-        pa_asyncmsgq_send(u->sink->asyncmsgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_ADD_OUTPUT, o, 0, NULL);
-    else {
-        /* If the sink is not yet started, we need to do the activation ourselves */
-        PA_LLIST_PREPEND(struct output, u->thread_info.active_outputs, o);
-
-        o->outq_rtpoll_item_read = pa_rtpoll_item_new_asyncmsgq_read(
-                u->rtpoll,
-                PA_RTPOLL_EARLY-1,  /* This item is very important */
-                o->outq);
-        o->inq_rtpoll_item_write = pa_rtpoll_item_new_asyncmsgq_write(
-                u->rtpoll,
-                PA_RTPOLL_EARLY,
-                o->inq);
-    }
+    return o;
+}
+
+/* Called from main context */
+static void output_free(struct output *o) {
+    pa_assert(o);
 
-    if (PA_SINK_IS_OPENED(state) || state == PA_SINK_INIT) {
-        pa_sink_suspend(sink, FALSE, PA_SUSPEND_IDLE);
+    output_disable(o);
 
-        if (PA_SINK_IS_OPENED(pa_sink_get_state(sink)))
-            if (output_create_sink_input(o) < 0)
-                goto fail;
-    }
+    pa_assert_se(pa_idxset_remove_by_data(o->userdata->outputs, o, NULL));
+    update_description(o->userdata);
 
-    update_description(u);
+    if (o->inq_rtpoll_item_read)
+        pa_rtpoll_item_free(o->inq_rtpoll_item_read);
+    if (o->inq_rtpoll_item_write)
+        pa_rtpoll_item_free(o->inq_rtpoll_item_write);
 
-    return o;
+    if (o->outq_rtpoll_item_read)
+        pa_rtpoll_item_free(o->outq_rtpoll_item_read);
+    if (o->outq_rtpoll_item_write)
+        pa_rtpoll_item_free(o->outq_rtpoll_item_write);
 
-fail:
+    if (o->inq)
+        pa_asyncmsgq_unref(o->inq);
 
-    if (o) {
-        pa_idxset_remove_by_data(u->outputs, o, NULL);
+    if (o->outq)
+        pa_asyncmsgq_unref(o->outq);
 
-        if (o->sink_input) {
-            pa_sink_input_unlink(o->sink_input);
-            pa_sink_input_unref(o->sink_input);
-        }
+    if (o->memblockq)
+        pa_memblockq_free(o->memblockq);
+
+    pa_xfree(o);
+}
+
+/* Called from main context */
+static void output_enable(struct output *o) {
+    pa_assert(o);
 
-        if (o->memblockq)
-            pa_memblockq_free(o->memblockq);
+    if (o->sink_input)
+        return;
 
-        if (o->inq)
-            pa_asyncmsgq_unref(o->inq);
+    /* This might cause the sink to be resumed. The state change hook
+     * of the sink might hence be called from here, which might then
+     * cause us to be called in a loop. Make sure that state changes
+     * for this output don't cause this loop by setting a flag here */
+    o->ignore_state_change = TRUE;
+
+    if (output_create_sink_input(o) >= 0) {
 
-        if (o->outq)
-            pa_asyncmsgq_unref(o->outq);
+        if (pa_sink_get_state(o->sink) != PA_SINK_INIT) {
 
-        pa_xfree(o);
+            /* First we register the output. That means that the sink
+             * will start to pass data to this output. */
+            pa_asyncmsgq_send(o->userdata->sink->asyncmsgq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_ADD_OUTPUT, o, 0, NULL);
+
+            /* Then we enable the sink input. That means that the sink
+             * is now asked for new data. */
+            pa_sink_input_put(o->sink_input);
+
+        } else
+            /* Hmm the sink is not yet started, do things right here */
+            output_add_within_thread(o);
     }
 
-    return NULL;
+    o->ignore_state_change = FALSE;
+}
+
+/* Called from main context */
+static void output_disable(struct output *o) {
+    pa_assert(o);
+
+    if (!o->sink_input)
+        return;
+
+    /* First we disable the sink input. That means that the sink is
+     * not asked for new data anymore  */
+    pa_sink_input_unlink(o->sink_input);
+
+    /* Then we unregister the output. That means that the sink doesn't
+     * pass any further data to this output */
+    pa_asyncmsgq_send(o->userdata->sink->asyncmsgq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_REMOVE_OUTPUT, o, 0, NULL);
+
+    /* Now dellocate the stream */
+    pa_sink_input_unref(o->sink_input);
+    o->sink_input = NULL;
+
+    /* Finally, drop all queued data */
+    pa_memblockq_flush_write(o->memblockq);
+    pa_asyncmsgq_flush(o->inq, FALSE);
+    pa_asyncmsgq_flush(o->outq, FALSE);
+}
+
+/* Called from main context */
+static void output_verify(struct output *o) {
+    pa_assert(o);
+
+    if (PA_SINK_IS_OPENED(pa_sink_get_state(o->userdata->sink)))
+        output_enable(o);
+    else
+        output_disable(o);
 }
 
+/* Called from main context */
 static pa_bool_t is_suitable_sink(struct userdata *u, pa_sink *s) {
     const char *t;
 
     pa_sink_assert_ref(s);
 
+    if (s == u->sink)
+        return FALSE;
+
     if (!(s->flags & PA_SINK_HARDWARE))
         return FALSE;
 
-    if (s == u->sink)
+    if (!(s->flags & PA_SINK_LATENCY))
         return FALSE;
 
     if ((t = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_CLASS)))
-        if (strcmp(t, "sound"))
+        if (!pa_streq(t, "sound"))
             return FALSE;
 
     return TRUE;
 }
 
+/* Called from main context */
 static pa_hook_result_t sink_put_hook_cb(pa_core *c, pa_sink *s, struct userdata* u) {
     struct output *o;
 
@@ -940,18 +1032,17 @@ static pa_hook_result_t sink_put_hook_cb(pa_core *c, pa_sink *s, struct userdata
         return PA_HOOK_OK;
 
     pa_log_info("Configuring new sink: %s", s->name);
-
     if (!(o = output_new(u, s))) {
         pa_log("Failed to create sink input on sink '%s'.", s->name);
         return PA_HOOK_OK;
     }
 
-    if (o->sink_input)
-        pa_sink_input_put(o->sink_input);
+    output_verify(o);
 
     return PA_HOOK_OK;
 }
 
+/* Called from main context */
 static struct output* find_output(struct userdata *u, pa_sink *s) {
     struct output *o;
     uint32_t idx;
@@ -962,13 +1053,14 @@ static struct output* find_output(struct userdata *u, pa_sink *s) {
     if (u->sink == s)
         return NULL;
 
-    for (o = pa_idxset_first(u->outputs, &idx); o; o = pa_idxset_next(u->outputs, &idx))
+    PA_IDXSET_FOREACH(o, u->outputs, idx)
         if (o->sink == s)
             return o;
 
     return NULL;
 }
 
+/* Called from main context */
 static pa_hook_result_t sink_unlink_hook_cb(pa_core *c, pa_sink *s, struct userdata* u) {
     struct output *o;
 
@@ -980,26 +1072,25 @@ static pa_hook_result_t sink_unlink_hook_cb(pa_core *c, pa_sink *s, struct userd
         return PA_HOOK_OK;
 
     pa_log_info("Unconfiguring sink: %s", s->name);
-
     output_free(o);
 
     return PA_HOOK_OK;
 }
 
+/* Called from main context */
 static pa_hook_result_t sink_state_changed_hook_cb(pa_core *c, pa_sink *s, struct userdata* u) {
     struct output *o;
-    pa_sink_state_t state;
 
     if (!(o = find_output(u, s)))
         return PA_HOOK_OK;
 
-    state = pa_sink_get_state(s);
-
-    if (PA_SINK_IS_OPENED(state) && PA_SINK_IS_OPENED(pa_sink_get_state(u->sink)) && !o->sink_input)
-        enable_output(o);
+    /* This state change might be triggered because we are creating a
+     * stream here, in that case we don't want to create it a second
+     * time here and enter a loop */
+    if (o->ignore_state_change)
+        return PA_HOOK_OK;
 
-    if (state == PA_SINK_SUSPENDED && o->sink_input)
-        disable_output(o);
+    output_verify(o);
 
     return PA_HOOK_OK;
 }
@@ -1029,18 +1120,14 @@ int pa__init(pa_module*m) {
         }
     }
 
-    m->userdata = u = pa_xnew(struct userdata, 1);
+    m->userdata = u = pa_xnew0(struct userdata, 1);
     u->core = m->core;
     u->module = m;
-    u->sink = NULL;
-    u->time_event = NULL;
     u->adjust_time = DEFAULT_ADJUST_TIME;
     u->rtpoll = pa_rtpoll_new();
     pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
-    u->thread = NULL;
     u->resample_method = resample_method;
     u->outputs = pa_idxset_new(NULL, NULL);
-    memset(&u->adjust_timestamp, 0, sizeof(u->adjust_timestamp));
     u->sink_put_slot = u->sink_unlink_slot = u->sink_state_changed_slot = NULL;
     PA_LLIST_HEAD_INIT(struct output, u->thread_info.active_outputs);
     pa_atomic_store(&u->thread_info.running, FALSE);
@@ -1065,6 +1152,55 @@ int pa__init(pa_module*m) {
 
     ss = m->core->default_sample_spec;
     map = m->core->default_channel_map;
+
+    /* Check the specified slave sinks for sample_spec and channel_map to use for the combined sink */
+    if (!u->automatic) {
+        const char*split_state = NULL;
+        char *n = NULL;
+        pa_sample_spec slaves_spec;
+        pa_channel_map slaves_map;
+        pa_bool_t is_first_slave = TRUE;
+
+        while ((n = pa_split(slaves, ",", &split_state))) {
+            pa_sink *slave_sink;
+
+            if (!(slave_sink = pa_namereg_get(m->core, n, PA_NAMEREG_SINK))) {
+                pa_log("Invalid slave sink '%s'", n);
+                pa_xfree(n);
+                goto fail;
+            }
+
+            pa_xfree(n);
+
+            if (is_first_slave) {
+                slaves_spec = slave_sink->sample_spec;
+                slaves_map = slave_sink->channel_map;
+                is_first_slave = FALSE;
+            } else {
+                if (slaves_spec.format != slave_sink->sample_spec.format)
+                    slaves_spec.format = PA_SAMPLE_INVALID;
+
+                if (slaves_spec.rate < slave_sink->sample_spec.rate)
+                    slaves_spec.rate = slave_sink->sample_spec.rate;
+
+                if (!pa_channel_map_equal(&slaves_map, &slave_sink->channel_map))
+                    slaves_spec.channels = 0;
+            }
+        }
+
+        if (!is_first_slave) {
+            if (slaves_spec.format != PA_SAMPLE_INVALID)
+                ss.format = slaves_spec.format;
+
+            ss.rate = slaves_spec.rate;
+
+            if (slaves_spec.channels > 0) {
+                map = slaves_map;
+                ss.channels = slaves_map.channels;
+            }
+        }
+    }
+
     if ((pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0)) {
         pa_log("Invalid sample specification.");
         goto fail;
@@ -1095,7 +1231,6 @@ int pa__init(pa_module*m) {
         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);
 
@@ -1149,7 +1284,7 @@ int pa__init(pa_module*m) {
 
         /* We're in automatic mode, we add every sink that matches our needs  */
 
-        for (s = pa_idxset_first(m->core->sinks, &idx); s; s = pa_idxset_next(m->core->sinks, &idx)) {
+        PA_IDXSET_FOREACH(s, m->core->sinks, idx) {
 
             if (!is_suitable_sink(u, s))
                 continue;
@@ -1174,9 +1309,8 @@ int pa__init(pa_module*m) {
     /* Activate the sink and the sink inputs */
     pa_sink_put(u->sink);
 
-    for (o = pa_idxset_first(u->outputs, &idx); o; o = pa_idxset_next(u->outputs, &idx))
-        if (o->sink_input)
-            pa_sink_input_put(o->sink_input);
+    PA_IDXSET_FOREACH(o, u->outputs, idx)
+        output_verify(o);
 
     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);
@@ -1195,37 +1329,6 @@ fail:
     return -1;
 }
 
-static void output_free(struct output *o) {
-    pa_assert(o);
-
-    disable_output(o);
-
-    pa_assert_se(pa_idxset_remove_by_data(o->userdata->outputs, o, NULL));
-
-    update_description(o->userdata);
-
-    if (o->inq_rtpoll_item_read)
-        pa_rtpoll_item_free(o->inq_rtpoll_item_read);
-    if (o->inq_rtpoll_item_write)
-        pa_rtpoll_item_free(o->inq_rtpoll_item_write);
-
-    if (o->outq_rtpoll_item_read)
-        pa_rtpoll_item_free(o->outq_rtpoll_item_read);
-    if (o->outq_rtpoll_item_write)
-        pa_rtpoll_item_free(o->outq_rtpoll_item_write);
-
-    if (o->inq)
-        pa_asyncmsgq_unref(o->inq);
-
-    if (o->outq)
-        pa_asyncmsgq_unref(o->outq);
-
-    if (o->memblockq)
-        pa_memblockq_free(o->memblockq);
-
-    pa_xfree(o);
-}
-
 void pa__done(pa_module*m) {
     struct userdata *u;
     struct output *o;
index a666073cfc6583b9604cc47bc9548eaef57f5e20..103f5c48dcdd5d02deb640cd7bc3720325ae9afd 100644 (file)
@@ -187,7 +187,6 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *message, vo
         }
 
         add_session(u, path);
-        return DBUS_HANDLER_RESULT_HANDLED;
 
     } else if (dbus_message_is_signal(message, "org.freedesktop.ConsoleKit.Seat", "SessionRemoved")) {
 
@@ -202,7 +201,6 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *message, vo
         }
 
         remove_session(u, path);
-        return DBUS_HANDLER_RESULT_HANDLED;
     }
 
 finish:
index 120b762c143eb65f1af047204a827c919ef80d02..da6c96661df28af606bfa93f057d8cc575ab7d58 100644 (file)
@@ -218,7 +218,7 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3
 
         if (sink->save_volume) {
             entry.channel_map = sink->channel_map;
-            entry.volume = *pa_sink_get_volume(sink, FALSE, TRUE);
+            entry.volume = *pa_sink_get_volume(sink, FALSE);
             entry.volume_valid = TRUE;
         }
 
diff --git a/src/modules/module-hal-detect-compat.c b/src/modules/module-hal-detect-compat.c
new file mode 100644 (file)
index 0000000..14cf814
--- /dev/null
@@ -0,0 +1,84 @@
+/***
+  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 <pulsecore/module.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/core-util.h>
+
+#include "module-hal-detect-symdef.h"
+
+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-udev-detect instead of module-hal-detect!");
+
+static const char* const valid_modargs[] = {
+    "api",
+    "tsched",
+    "subdevices",
+    NULL,
+};
+
+int pa__init(pa_module*m) {
+    pa_modargs *ma = NULL;
+    pa_bool_t tsched = TRUE;
+    pa_module *n;
+    char *t;
+
+    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, "tsched", &tsched) < 0) {
+        pa_log("tsched= expects boolean arguments");
+        goto fail;
+    }
+
+    pa_log_warn("We will now load module-udev-detect. Please make sure to remove module-hal-detect from your configuration.");
+
+    t = pa_sprintf_malloc("tsched=%s", pa_yes_no(tsched));
+    n = pa_module_load(m->core, "module-udev-detect", t);
+    pa_xfree(t);
+
+    if (n)
+        pa_module_unload_request(m, TRUE);
+
+    pa_modargs_free(ma);
+
+    return n ? 0 : -1;
+
+fail:
+    if (ma)
+        pa_modargs_free(ma);
+
+    return -1;
+}
index b5b2aaf05d877c1b182fc8f0983a6f221a60f072..6034d0ee6408689df4972fdb8c0f57df5b2e878c 100644 (file)
@@ -58,13 +58,13 @@ PA_MODULE_LOAD_ONCE(TRUE);
 #if defined(HAVE_ALSA) && defined(HAVE_OSS_OUTPUT)
 PA_MODULE_USAGE("api=<alsa or oss> "
                 "tsched=<enable system timer based scheduling mode?>"
-                "subdevs=<init all subdevices>");
+                "subdevices=<init all subdevices>");
 #elif defined(HAVE_ALSA)
 PA_MODULE_USAGE("api=<alsa> "
                 "tsched=<enable system timer based scheduling mode?>");
 #elif defined(HAVE_OSS_OUTPUT)
 PA_MODULE_USAGE("api=<oss>"
-                "subdevs=<init all subdevices>");
+                "subdevices=<init all subdevices>");
 #endif
 PA_MODULE_DEPRECATED("Please use module-udev-detect instead of module-hal-detect!");
 
@@ -98,7 +98,7 @@ static const char* const valid_modargs[] = {
     "tsched",
 #endif
 #ifdef HAVE_OSS_OUTPUT
-    "subdevs",
+    "subdevices",
 #endif
     NULL
 };
@@ -623,8 +623,6 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *message, vo
 
         }
 
-        return DBUS_HANDLER_RESULT_HANDLED;
-
     } else if (dbus_message_is_signal(message, "org.pulseaudio.Server", "DirtyGiveUpMessage")) {
         /* We use this message to avoid a dirty race condition when we
            get an ACLAdded message before the previously owning PA
@@ -668,7 +666,6 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *message, vo
             /* Yes, we don't check the UDI for validity, but hopefully HAL will */
             device_added_cb(u->context, udi);
 
-        return DBUS_HANDLER_RESULT_HANDLED;
     }
 
 finish:
@@ -772,8 +769,8 @@ int pa__init(pa_module*m) {
     }
 
 #ifdef HAVE_OSS_OUTPUT
-    if (pa_modargs_get_value_boolean(ma, "subdevs", &u->init_subdevs) < 0) {
-        pa_log("Failed to parse subdevs argument.");
+    if (pa_modargs_get_value_boolean(ma, "subdevices", &u->init_subdevs) < 0) {
+        pa_log("Failed to parse subdevices= argument.");
         goto fail;
     }
 #endif
index c697209a8dde747bc16f8eaa6b51457c655ffda0..b9924dfd92065525f054fa87363e77ceaeffea78 100644 (file)
@@ -127,6 +127,9 @@ static pa_hook_result_t sink_input_new_hook_callback(pa_core *c, pa_sink_input_n
         if (s == def)
             continue;
 
+        if (!PA_SINK_IS_LINKED(pa_sink_get_state(s)))
+            continue;
+
         if (role_match(s->proplist, role)) {
             new_data->sink = s;
             new_data->save_sink = FALSE;
@@ -173,6 +176,9 @@ static pa_hook_result_t source_output_new_hook_callback(pa_core *c, pa_source_ou
         if (s == def)
             continue;
 
+        if (!PA_SOURCE_IS_LINKED(pa_source_get_state(s)))
+            continue;
+
         if (role_match(s->proplist, role)) {
             new_data->source = s;
             new_data->save_source = FALSE;
@@ -201,6 +207,17 @@ static pa_hook_result_t sink_put_hook_callback(pa_core *c, pa_sink *sink, struct
         if (si->save_sink)
             continue;
 
+        /* Skip this if it is already in the process of being moved
+         * anyway */
+        if (!si->sink)
+            continue;
+
+        /* It might happen that a stream and a sink are set up at the
+           same time, in which case we want to make sure we don't
+           interfere with that */
+        if (!PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(si)))
+            continue;
+
         if (!(role = pa_proplist_gets(si->proplist, PA_PROP_MEDIA_ROLE)))
             continue;
 
@@ -237,6 +254,17 @@ static pa_hook_result_t source_put_hook_callback(pa_core *c, pa_source *source,
         if (so->direct_on_input)
             continue;
 
+        /* Skip this if it is already in the process of being moved
+         * anyway */
+        if (!so->source)
+            continue;
+
+        /* It might happen that a stream and a source are set up at the
+           same time, in which case we want to make sure we don't
+           interfere with that */
+        if (!PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(so)))
+            continue;
+
         if (!(role = pa_proplist_gets(so->proplist, PA_PROP_MEDIA_ROLE)))
             continue;
 
@@ -275,24 +303,28 @@ static pa_hook_result_t sink_unlink_hook_callback(pa_core *c, pa_sink *sink, str
         uint32_t jdx;
         pa_sink *d;
 
+        if (!si->sink)
+            continue;
+
         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;
-        }
+        if (def != sink && role_match(def->proplist, role))
+            if (pa_sink_input_move_to(si, def, FALSE) >= 0)
+                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;
-            }
+            if (!PA_SINK_IS_LINKED(pa_sink_get_state(d)))
+                continue;
+
+            if (role_match(d->proplist, role))
+                if (pa_sink_input_move_to(si, d, FALSE) >= 0)
+                    break;
         }
     }
 
@@ -325,6 +357,9 @@ static pa_hook_result_t source_unlink_hook_callback(pa_core *c, pa_source *sourc
         if (so->direct_on_input)
             continue;
 
+        if (!so->source)
+            continue;
+
         if (!(role = pa_proplist_gets(so->proplist, PA_PROP_MEDIA_ROLE)))
             continue;
 
@@ -339,6 +374,9 @@ static pa_hook_result_t source_unlink_hook_callback(pa_core *c, pa_source *sourc
             if (d == def || d == source)
                 continue;
 
+            if (!PA_SOURCE_IS_LINKED(pa_source_get_state(d)))
+                continue;
+
             if (role_match(d->proplist, role) && !source->monitor_of == !d->monitor_of) {
                 pa_source_output_move_to(so, d, FALSE);
                 break;
index b26330c8613ae8d534e231a7707e17f918a6c646..f2d53d00244352f2efaba6468cc749903c83ff61 100644 (file)
@@ -64,10 +64,9 @@ PA_MODULE_USAGE(
 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
 
 struct userdata {
-    pa_core *core;
     pa_module *module;
 
-    pa_sink *sink, *master;
+    pa_sink *sink;
     pa_sink_input *sink_input;
 
     const LADSPA_Descriptor *descriptor;
@@ -105,19 +104,26 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
 
     switch (code) {
 
-        case PA_SINK_MESSAGE_GET_LATENCY: {
-            pa_usec_t usec = 0;
+        case PA_SINK_MESSAGE_GET_LATENCY:
 
-            /* Get the latency of the master sink */
-            if (PA_MSGOBJECT(u->master)->process_msg(PA_MSGOBJECT(u->master), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
-                usec = 0;
+            /* The sink is _put() before the sink input is, so let's
+             * make sure we don't access it in that time. Also, the
+             * sink input is first shut down, the sink second. */
+            if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
+                !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
+                *((pa_usec_t*) data) = 0;
+                return 0;
+            }
+
+            *((pa_usec_t*) data) =
 
-            /* Add the latency internal to our sink input on top */
-            usec += pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->master->sample_spec);
+                /* Get the latency of the master sink */
+                pa_sink_get_latency_within_thread(u->sink_input->sink) +
+
+                /* Add the latency internal to our sink input on top */
+                pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec);
 
-            *((pa_usec_t*) data) = usec;
             return 0;
-        }
     }
 
     return pa_sink_process_msg(o, code, data, offset, chunk);
@@ -130,12 +136,11 @@ static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
     pa_sink_assert_ref(s);
     pa_assert_se(u = s->userdata);
 
-    if (PA_SINK_IS_LINKED(state) &&
-        u->sink_input &&
-        PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
-
-        pa_sink_input_cork(u->sink_input, state == PA_SINK_SUSPENDED);
+    if (!PA_SINK_IS_LINKED(state) ||
+        !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
+        return 0;
 
+    pa_sink_input_cork(u->sink_input, state == PA_SINK_SUSPENDED);
     return 0;
 }
 
@@ -146,6 +151,10 @@ static void sink_request_rewind(pa_sink *s) {
     pa_sink_assert_ref(s);
     pa_assert_se(u = s->userdata);
 
+    if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
+        !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
+        return;
+
     /* Just hand this one over to the master sink */
     pa_sink_input_request_rewind(u->sink_input, s->thread_info.rewind_nbytes + pa_memblockq_get_length(u->memblockq), TRUE, FALSE, FALSE);
 }
@@ -157,6 +166,10 @@ static void sink_update_requested_latency(pa_sink *s) {
     pa_sink_assert_ref(s);
     pa_assert_se(u = s->userdata);
 
+    if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
+        !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
+        return;
+
     /* Just hand this one over to the master sink */
     pa_sink_input_set_requested_latency_within_thread(
             u->sink_input,
@@ -175,9 +188,6 @@ static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk
     pa_assert(chunk);
     pa_assert_se(u = i->userdata);
 
-    if (!u->sink || !PA_SINK_IS_OPENED(u->sink->thread_info.state))
-        return -1;
-
     /* Hmm, process any rewind request that might be queued up */
     pa_sink_process_rewind(u->sink, 0);
 
@@ -228,9 +238,6 @@ static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
     pa_sink_input_assert_ref(i);
     pa_assert_se(u = i->userdata);
 
-    if (!u->sink || !PA_SINK_IS_OPENED(u->sink->thread_info.state))
-        return;
-
     if (u->sink->thread_info.rewind_nbytes > 0) {
         size_t max_rewrite;
 
@@ -266,9 +273,6 @@ static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
     pa_sink_input_assert_ref(i);
     pa_assert_se(u = i->userdata);
 
-    if (!u->sink || !PA_SINK_IS_LINKED(u->sink->thread_info.state))
-        return;
-
     pa_memblockq_set_maxrewind(u->memblockq, nbytes);
     pa_sink_set_max_rewind_within_thread(u->sink, nbytes);
 }
@@ -280,9 +284,6 @@ static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
     pa_sink_input_assert_ref(i);
     pa_assert_se(u = i->userdata);
 
-    if (!u->sink || !PA_SINK_IS_LINKED(u->sink->thread_info.state))
-        return;
-
     pa_sink_set_max_request_within_thread(u->sink, nbytes);
 }
 
@@ -293,24 +294,28 @@ static void sink_input_update_sink_latency_range_cb(pa_sink_input *i) {
     pa_sink_input_assert_ref(i);
     pa_assert_se(u = i->userdata);
 
-    if (!u->sink || !PA_SINK_IS_LINKED(u->sink->thread_info.state))
-        return;
-
     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 */
-static void sink_input_detach_cb(pa_sink_input *i) {
+static void sink_input_update_sink_fixed_latency_cb(pa_sink_input *i) {
     struct userdata *u;
 
     pa_sink_input_assert_ref(i);
     pa_assert_se(u = i->userdata);
 
-    if (!u->sink || !PA_SINK_IS_LINKED(u->sink->thread_info.state))
-        return;
+    pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
+}
+
+/* Called from I/O thread context */
+static void sink_input_detach_cb(pa_sink_input *i) {
+    struct userdata *u;
+
+    pa_sink_input_assert_ref(i);
+    pa_assert_se(u = i->userdata);
 
     pa_sink_detach_within_thread(u->sink);
-    pa_sink_set_asyncmsgq(u->sink, NULL);
+
     pa_sink_set_rtpoll(u->sink, NULL);
 }
 
@@ -321,14 +326,13 @@ static void sink_input_attach_cb(pa_sink_input *i) {
     pa_sink_input_assert_ref(i);
     pa_assert_se(u = i->userdata);
 
-    if (!u->sink || !PA_SINK_IS_LINKED(u->sink->thread_info.state))
-        return;
+    pa_sink_set_rtpoll(u->sink, i->sink->thread_info.rtpoll);
+    pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
+    pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
+    pa_sink_set_max_request_within_thread(u->sink, pa_sink_input_get_max_request(i));
+    pa_sink_set_max_rewind_within_thread(u->sink, pa_sink_input_get_max_rewind(i));
 
-    pa_sink_set_asyncmsgq(u->sink, i->sink->asyncmsgq);
-    pa_sink_set_rtpoll(u->sink, i->sink->rtpoll);
     pa_sink_attach_within_thread(u->sink);
-
-    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 */
@@ -338,14 +342,18 @@ static void sink_input_kill_cb(pa_sink_input *i) {
     pa_sink_input_assert_ref(i);
     pa_assert_se(u = i->userdata);
 
-    pa_sink_unlink(u->sink);
+    /* The order here matters! We first kill the sink input, followed
+     * by the sink. That means the sink callbacks must be protected
+     * against an unconnected sink input! */
     pa_sink_input_unlink(u->sink_input);
+    pa_sink_unlink(u->sink);
 
-    pa_sink_unref(u->sink);
-    u->sink = NULL;
     pa_sink_input_unref(u->sink_input);
     u->sink_input = NULL;
 
+    pa_sink_unref(u->sink);
+    u->sink = NULL;
+
     pa_module_unload_request(u->module, TRUE);
 }
 
@@ -375,6 +383,17 @@ static pa_bool_t sink_input_may_move_to_cb(pa_sink_input *i, pa_sink *dest) {
     return u->sink != dest;
 }
 
+/* Called from main context */
+static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) {
+    struct userdata *u;
+
+    pa_sink_input_assert_ref(i);
+    pa_assert_se(u = i->userdata);
+
+    pa_sink_set_asyncmsgq(u->sink, dest->asyncmsgq);
+    pa_sink_update_flags(u->sink, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY, dest->flags);
+}
+
 int pa__init(pa_module*m) {
     struct userdata *u;
     pa_sample_spec ss;
@@ -395,7 +414,7 @@ int pa__init(pa_module*m) {
 
     pa_assert(m);
 
-    pa_assert(sizeof(LADSPA_Data) == sizeof(float));
+    pa_assert_cc(sizeof(LADSPA_Data) == sizeof(float));
 
     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
         pa_log("Failed to parse module arguments.");
@@ -428,12 +447,8 @@ int pa__init(pa_module*m) {
     cdata = pa_modargs_get_value(ma, "control", NULL);
 
     u = pa_xnew0(struct userdata, 1);
-    u->core = m->core;
     u->module = m;
     m->userdata = u;
-    u->master = master;
-    u->sink = NULL;
-    u->sink_input = NULL;
     u->memblockq = pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH, 0, pa_frame_size(&ss), 1, 1, 0, NULL);
 
     if (!(e = getenv("LADSPA_PATH")))
@@ -697,11 +712,10 @@ int pa__init(pa_module*m) {
     sink_data.module = m;
     if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
         sink_data.name = pa_sprintf_malloc("%s.ladspa", master->name);
-    sink_data.namereg_fail = FALSE;
     pa_sink_new_data_set_sample_spec(&sink_data, &ss);
     pa_sink_new_data_set_channel_map(&sink_data, &map);
     z = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION);
-    pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "LADSPA Plugin %s on %s", label, z ? z : master->name);
+    pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "LADSPA Plugin %s on %s", d->Name, z ? z : master->name);
     pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name);
     pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
     pa_proplist_sets(sink_data.proplist, "device.ladspa.module", plugin);
@@ -717,7 +731,7 @@ int pa__init(pa_module*m) {
         goto fail;
     }
 
-    u->sink = pa_sink_new(m->core, &sink_data, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY);
+    u->sink = pa_sink_new(m->core, &sink_data, master->flags & (PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY));
     pa_sink_new_data_done(&sink_data);
 
     if (!u->sink) {
@@ -732,19 +746,18 @@ int pa__init(pa_module*m) {
     u->sink->userdata = u;
 
     pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
-    pa_sink_set_rtpoll(u->sink, master->rtpoll);
 
     /* Create sink input */
     pa_sink_input_new_data_init(&sink_input_data);
     sink_input_data.driver = __FILE__;
     sink_input_data.module = m;
-    sink_input_data.sink = u->master;
+    sink_input_data.sink = master;
     pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "LADSPA Stream");
     pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
     pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss);
     pa_sink_input_new_data_set_channel_map(&sink_input_data, &map);
 
-    pa_sink_input_new(&u->sink_input, m->core, &sink_input_data, PA_SINK_INPUT_DONT_MOVE);
+    pa_sink_input_new(&u->sink_input, m->core, &sink_input_data, 0);
     pa_sink_input_new_data_done(&sink_input_data);
 
     if (!u->sink_input)
@@ -755,11 +768,13 @@ int pa__init(pa_module*m) {
     u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
     u->sink_input->update_max_request = sink_input_update_max_request_cb;
     u->sink_input->update_sink_latency_range = sink_input_update_sink_latency_range_cb;
+    u->sink_input->update_sink_fixed_latency = sink_input_update_sink_fixed_latency_cb;
     u->sink_input->kill = sink_input_kill_cb;
     u->sink_input->attach = sink_input_attach_cb;
     u->sink_input->detach = sink_input_detach_cb;
     u->sink_input->state_change = sink_input_state_change_cb;
     u->sink_input->may_move_to = sink_input_may_move_to_cb;
+    u->sink_input->moving = sink_input_moving_cb;
     u->sink_input->userdata = u;
 
     pa_sink_put(u->sink);
@@ -800,15 +815,20 @@ void pa__done(pa_module*m) {
     if (!(u = m->userdata))
         return;
 
-    if (u->sink) {
-        pa_sink_unlink(u->sink);
-        pa_sink_unref(u->sink);
-    }
+    /* See comments in sink_input_kill_cb() above regarding
+     * destruction order! */
 
-    if (u->sink_input) {
+    if (u->sink_input)
         pa_sink_input_unlink(u->sink_input);
+
+    if (u->sink)
+        pa_sink_unlink(u->sink);
+
+    if (u->sink_input)
         pa_sink_input_unref(u->sink_input);
-    }
+
+    if (u->sink)
+        pa_sink_unref(u->sink);
 
     for (c = 0; c < u->channels; c++)
         if (u->handle[c]) {
index 06efeb8faa77ae76994735ad6a330e41dcad03b0..d0e902f69b3b080e34b9005d68d30af49aa9fbc5 100644 (file)
@@ -63,6 +63,8 @@ struct userdata {
     float mute_toggle_save;
 };
 
+#define DELTA (PA_VOLUME_NORM/20)
+
 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;
@@ -119,32 +121,17 @@ static void io_callback(pa_mainloop_api *io, pa_io_event *e, int fd, pa_io_event
                 if (!(s = pa_namereg_get(u->module->core, u->sink_name, PA_NAMEREG_SINK)))
                     pa_log("Failed to get sink '%s'", u->sink_name);
                 else {
-                    int i;
-                    pa_cvolume cv = *pa_sink_get_volume(s, FALSE, FALSE);
-
-#define DELTA (PA_VOLUME_NORM/20)
+                    pa_cvolume cv = *pa_sink_get_volume(s, FALSE);
 
                     switch (volchange) {
                         case UP:
-                            for (i = 0; i < cv.channels; i++) {
-                                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, TRUE, TRUE);
+                            pa_cvolume_inc(&cv, DELTA);
+                            pa_sink_set_volume(s, &cv, TRUE, TRUE);
                             break;
 
                         case DOWN:
-                            for (i = 0; i < cv.channels; i++) {
-                                if (cv.values[i] > DELTA)
-                                    cv.values[i] -= DELTA;
-                                else
-                                    cv.values[i] = PA_VOLUME_MUTED;
-                            }
-
-                            pa_sink_set_volume(s, &cv, TRUE, TRUE, TRUE, TRUE);
+                            pa_cvolume_dec(&cv, DELTA);
+                            pa_sink_set_volume(s, &cv, TRUE, TRUE);
                             break;
 
                         case MUTE:
@@ -156,7 +143,6 @@ static void io_callback(pa_mainloop_api *io, pa_io_event *e, int fd, pa_io_event
                             break;
 
                         case MUTE_TOGGLE:
-
                             pa_sink_set_mute(s, !pa_sink_get_mute(s, FALSE), TRUE);
                             break;
 
index 625f2a8bbad389b64681e4ec88ae746387884a61..0bd781d2b2beae0a441e42b26bc996abf08ea324 100644 (file)
@@ -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, TRUE);
+                pa_sink_input_set_volume(si, &cv, TRUE, FALSE);
             }
         }
     }
@@ -243,6 +243,9 @@ int pa__init(pa_module*m) {
     if (load_rules(u, pa_modargs_get_value(ma, "table", NULL)) < 0)
         goto fail;
 
+    /* FIXME: Doing this asynchronously is just broken. This needs to
+     * use a hook! */
+
     u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_SINK_INPUT, callback, u);
 
     pa_modargs_free(ma);
index b30fae5122fb6c493f99d3c9f7d6f6a7c27750db..516bf41374f953d796f6a48690c0d24cde620262 100644 (file)
@@ -65,6 +65,8 @@ struct userdata {
     pa_module *module;
 };
 
+#define DELTA (PA_VOLUME_NORM/20)
+
 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;
 
@@ -85,14 +87,27 @@ static void io_callback(pa_mainloop_api *io, pa_io_event *e, int fd, pa_io_event
         }
 
         if (ev.type == EV_KEY && (ev.value == 1 || ev.value == 2)) {
-            enum { INVALID, UP, DOWN, MUTE_TOGGLE } volchange = INVALID;
+            enum {
+                INVALID,
+                UP,
+                DOWN,
+                MUTE_TOGGLE
+            } volchange = INVALID;
 
             pa_log_debug("Key code=%u, value=%u", ev.code, ev.value);
 
             switch (ev.code) {
-                case KEY_VOLUMEDOWN:  volchange = DOWN; break;
-                case KEY_VOLUMEUP:    volchange = UP; break;
-                case KEY_MUTE:        volchange = MUTE_TOGGLE; break;
+                case KEY_VOLUMEDOWN:
+                    volchange = DOWN;
+                    break;
+
+                case KEY_VOLUMEUP:
+                    volchange = UP;
+                    break;
+
+                case KEY_MUTE:
+                    volchange = MUTE_TOGGLE;
+                    break;
             }
 
             if (volchange != INVALID) {
@@ -101,36 +116,20 @@ static void io_callback(pa_mainloop_api *io, pa_io_event *e, int fd, pa_io_event
                 if (!(s = pa_namereg_get(u->module->core, u->sink_name, PA_NAMEREG_SINK)))
                     pa_log("Failed to get sink '%s'", u->sink_name);
                 else {
-                    int i;
-                    pa_cvolume cv = *pa_sink_get_volume(s, FALSE, FALSE);
-
-#define DELTA (PA_VOLUME_NORM/20)
+                    pa_cvolume cv = *pa_sink_get_volume(s, FALSE);
 
                     switch (volchange) {
                         case UP:
-                            for (i = 0; i < cv.channels; i++) {
-                                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, TRUE, TRUE);
+                            pa_cvolume_inc(&cv, DELTA);
+                            pa_sink_set_volume(s, &cv, TRUE, TRUE);
                             break;
 
                         case DOWN:
-                            for (i = 0; i < cv.channels; i++) {
-                                if (cv.values[i] > DELTA)
-                                    cv.values[i] -= DELTA;
-                                else
-                                    cv.values[i] = PA_VOLUME_MUTED;
-                            }
-
-                            pa_sink_set_volume(s, &cv, TRUE, TRUE, TRUE, TRUE);
+                            pa_cvolume_dec(&cv, DELTA);
+                            pa_sink_set_volume(s, &cv, TRUE, TRUE);
                             break;
 
                         case MUTE_TOGGLE:
-
                             pa_sink_set_mute(s, !pa_sink_get_mute(s, FALSE), TRUE);
                             break;
 
index 0b7b9b8f900ea5f379619a0141f141da2e9d5591..0b4fdc9bb182bb09012f33c56e9f2318b2b3036b 100644 (file)
@@ -1,7 +1,7 @@
 /***
   This file is part of PulseAudio.
 
-  Copyright 2004-2008 Lennart Poettering
+  Copyright 2004-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
@@ -48,16 +48,15 @@ PA_MODULE_USAGE(
         "master=<name of sink to remap> "
         "master_channel_map=<channel map> "
         "format=<sample format> "
-        "channels=<number of channels> "
         "rate=<sample rate> "
+        "channels=<number of channels> "
         "channel_map=<channel map> "
         "remix=<remix channels?>");
 
 struct userdata {
-    pa_core *core;
     pa_module *module;
 
-    pa_sink *sink, *master;
+    pa_sink *sink;
     pa_sink_input *sink_input;
 };
 
@@ -80,19 +79,24 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
 
     switch (code) {
 
-        case PA_SINK_MESSAGE_GET_LATENCY: {
-            pa_usec_t usec = 0;
+        case PA_SINK_MESSAGE_GET_LATENCY:
 
-            /* Get the latency of the master sink */
-            if (PA_MSGOBJECT(u->master)->process_msg(PA_MSGOBJECT(u->master), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
-                usec = 0;
+            /* The sink is _put() before the sink input is, so let's
+             * make sure we don't access it yet */
+            if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
+                !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
+                *((pa_usec_t*) data) = 0;
+                return 0;
+            }
 
-            /* Add the latency internal to our sink input on top */
-            usec += pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->master->sample_spec);
+            *((pa_usec_t*) data) =
+                /* Get the latency of the master sink */
+                pa_sink_get_latency_within_thread(u->sink_input->sink) +
+
+                /* Add the latency internal to our sink input on top */
+                pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec);
 
-            *((pa_usec_t*) data) = usec;
             return 0;
-        }
     }
 
     return pa_sink_process_msg(o, code, data, offset, chunk);
@@ -105,12 +109,11 @@ static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
     pa_sink_assert_ref(s);
     pa_assert_se(u = s->userdata);
 
-    if (PA_SINK_IS_LINKED(state) &&
-        u->sink_input &&
-        PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
-
-        pa_sink_input_cork(u->sink_input, state == PA_SINK_SUSPENDED);
+    if (!PA_SINK_IS_LINKED(state) ||
+        !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
+        return 0;
 
+    pa_sink_input_cork(u->sink_input, state == PA_SINK_SUSPENDED);
     return 0;
 }
 
@@ -121,6 +124,10 @@ static void sink_request_rewind(pa_sink *s) {
     pa_sink_assert_ref(s);
     pa_assert_se(u = s->userdata);
 
+    if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
+        !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
+        return;
+
     pa_sink_input_request_rewind(u->sink_input, s->thread_info.rewind_nbytes, TRUE, FALSE, FALSE);
 }
 
@@ -131,6 +138,10 @@ static void sink_update_requested_latency(pa_sink *s) {
     pa_sink_assert_ref(s);
     pa_assert_se(u = s->userdata);
 
+    if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
+        !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
+        return;
+
     /* Just hand this one over to the master sink */
     pa_sink_input_set_requested_latency_within_thread(
             u->sink_input,
@@ -145,9 +156,6 @@ static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk
     pa_assert(chunk);
     pa_assert_se(u = i->userdata);
 
-    if (!u->sink || !PA_SINK_IS_OPENED(u->sink->thread_info.state))
-        return -1;
-
     /* Hmm, process any rewind request that might be queued up */
     pa_sink_process_rewind(u->sink, 0);
 
@@ -163,9 +171,6 @@ static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
     pa_sink_input_assert_ref(i);
     pa_assert_se(u = i->userdata);
 
-    if (!u->sink || !PA_SINK_IS_OPENED(u->sink->thread_info.state))
-        return;
-
     if (u->sink->thread_info.rewind_nbytes > 0) {
         amount = PA_MIN(u->sink->thread_info.rewind_nbytes, nbytes);
         u->sink->thread_info.rewind_nbytes = 0;
@@ -181,9 +186,6 @@ static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
     pa_sink_input_assert_ref(i);
     pa_assert_se(u = i->userdata);
 
-    if (!u->sink || !PA_SINK_IS_LINKED(u->sink->thread_info.state))
-        return;
-
     pa_sink_set_max_rewind_within_thread(u->sink, nbytes);
 }
 
@@ -194,9 +196,6 @@ static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
     pa_sink_input_assert_ref(i);
     pa_assert_se(u = i->userdata);
 
-    if (!u->sink || !PA_SINK_IS_LINKED(u->sink->thread_info.state))
-        return;
-
     pa_sink_set_max_request_within_thread(u->sink, nbytes);
 }
 
@@ -207,24 +206,28 @@ static void sink_input_update_sink_latency_range_cb(pa_sink_input *i) {
     pa_sink_input_assert_ref(i);
     pa_assert_se(u = i->userdata);
 
-    if (!u->sink || !PA_SINK_IS_LINKED(u->sink->thread_info.state))
-        return;
-
     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 */
-static void sink_input_detach_cb(pa_sink_input *i) {
+static void sink_input_update_sink_fixed_latency_cb(pa_sink_input *i) {
     struct userdata *u;
 
     pa_sink_input_assert_ref(i);
     pa_assert_se(u = i->userdata);
 
-    if (!u->sink || !PA_SINK_IS_LINKED(u->sink->thread_info.state))
-        return;
+    pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
+}
+
+/* Called from I/O thread context */
+static void sink_input_detach_cb(pa_sink_input *i) {
+    struct userdata *u;
+
+    pa_sink_input_assert_ref(i);
+    pa_assert_se(u = i->userdata);
 
     pa_sink_detach_within_thread(u->sink);
-    pa_sink_set_asyncmsgq(u->sink, NULL);
+
     pa_sink_set_rtpoll(u->sink, NULL);
 }
 
@@ -235,14 +238,13 @@ static void sink_input_attach_cb(pa_sink_input *i) {
     pa_sink_input_assert_ref(i);
     pa_assert_se(u = i->userdata);
 
-    if (!u->sink || !PA_SINK_IS_LINKED(u->sink->thread_info.state))
-        return;
+    pa_sink_set_rtpoll(u->sink, i->sink->thread_info.rtpoll);
+    pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
+    pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
+    pa_sink_set_max_request_within_thread(u->sink, pa_sink_input_get_max_request(i));
+    pa_sink_set_max_rewind_within_thread(u->sink, pa_sink_input_get_max_rewind(i));
 
-    pa_sink_set_asyncmsgq(u->sink, i->sink->asyncmsgq);
-    pa_sink_set_rtpoll(u->sink, i->sink->rtpoll);
     pa_sink_attach_within_thread(u->sink);
-
-    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 */
@@ -252,14 +254,18 @@ static void sink_input_kill_cb(pa_sink_input *i) {
     pa_sink_input_assert_ref(i);
     pa_assert_se(u = i->userdata);
 
-    pa_sink_unlink(u->sink);
+    /* The order here matters! We first kill the sink input, followed
+     * by the sink. That means the sink callbacks must be protected
+     * against an unconnected sink input! */
     pa_sink_input_unlink(u->sink_input);
+    pa_sink_unlink(u->sink);
 
-    pa_sink_unref(u->sink);
-    u->sink = NULL;
     pa_sink_input_unref(u->sink_input);
     u->sink_input = NULL;
 
+    pa_sink_unref(u->sink);
+    u->sink = NULL;
+
     pa_module_unload_request(u->module, TRUE);
 }
 
@@ -289,6 +295,17 @@ static pa_bool_t sink_input_may_move_to_cb(pa_sink_input *i, pa_sink *dest) {
     return u->sink != dest;
 }
 
+/* Called from main context */
+static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) {
+    struct userdata *u;
+
+    pa_sink_input_assert_ref(i);
+    pa_assert_se(u = i->userdata);
+
+    pa_sink_set_asyncmsgq(u->sink, dest->asyncmsgq);
+    pa_sink_update_flags(u->sink, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY, dest->flags);
+}
+
 int pa__init(pa_module*m) {
     struct userdata *u;
     pa_sample_spec ss;
@@ -339,12 +356,8 @@ int pa__init(pa_module*m) {
     }
 
     u = pa_xnew0(struct userdata, 1);
-    u->core = m->core;
     u->module = m;
     m->userdata = u;
-    u->master = master;
-    u->sink = NULL;
-    u->sink_input = NULL;
 
     /* Create sink */
     pa_sink_new_data_init(&sink_data);
@@ -365,7 +378,7 @@ int pa__init(pa_module*m) {
         goto fail;
     }
 
-    u->sink = pa_sink_new(m->core, &sink_data, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY);
+    u->sink = pa_sink_new(m->core, &sink_data, master->flags & (PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY));
     pa_sink_new_data_done(&sink_data);
 
     if (!u->sink) {
@@ -380,19 +393,18 @@ int pa__init(pa_module*m) {
     u->sink->userdata = u;
 
     pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
-    pa_sink_set_rtpoll(u->sink, master->rtpoll);
 
     /* Create sink input */
     pa_sink_input_new_data_init(&sink_input_data);
     sink_input_data.driver = __FILE__;
     sink_input_data.module = m;
-    sink_input_data.sink = u->master;
+    sink_input_data.sink = master;
     pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "Remapped Stream");
     pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
     pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss);
     pa_sink_input_new_data_set_channel_map(&sink_input_data, &stream_map);
 
-    pa_sink_input_new(&u->sink_input, m->core, &sink_input_data, PA_SINK_INPUT_DONT_MOVE | (remix ? 0 : PA_SINK_INPUT_NO_REMIX));
+    pa_sink_input_new(&u->sink_input, m->core, &sink_input_data, (remix ? 0 : PA_SINK_INPUT_NO_REMIX));
     pa_sink_input_new_data_done(&sink_input_data);
 
     if (!u->sink_input)
@@ -403,11 +415,13 @@ int pa__init(pa_module*m) {
     u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
     u->sink_input->update_max_request = sink_input_update_max_request_cb;
     u->sink_input->update_sink_latency_range = sink_input_update_sink_latency_range_cb;
+    u->sink_input->update_sink_fixed_latency = sink_input_update_sink_fixed_latency_cb;
     u->sink_input->attach = sink_input_attach_cb;
     u->sink_input->detach = sink_input_detach_cb;
     u->sink_input->kill = sink_input_kill_cb;
     u->sink_input->state_change = sink_input_state_change_cb;
     u->sink_input->may_move_to = sink_input_may_move_to_cb;
+    u->sink_input->moving = sink_input_moving_cb;
     u->sink_input->userdata = u;
 
     pa_sink_put(u->sink);
@@ -443,15 +457,20 @@ void pa__done(pa_module*m) {
     if (!(u = m->userdata))
         return;
 
-    if (u->sink) {
-        pa_sink_unlink(u->sink);
-        pa_sink_unref(u->sink);
-    }
+    /* See comments in sink_input_kill_cb() above regarding
+     * destruction order! */
 
-    if (u->sink_input) {
+    if (u->sink_input)
         pa_sink_input_unlink(u->sink_input);
+
+    if (u->sink)
+        pa_sink_unlink(u->sink);
+
+    if (u->sink_input)
         pa_sink_input_unref(u->sink_input);
-    }
+
+    if (u->sink)
+        pa_sink_unref(u->sink);
 
     pa_xfree(u);
 }
index c23fecebea56abb311278e3c7a643e099a51d955..722d84b29ac9d247401c56c9e823a9fb16c7f8e4 100644 (file)
@@ -45,13 +45,46 @@ static const char* const valid_modargs[] = {
 };
 
 struct userdata {
-    pa_hook_slot *sink_slot, *source_slot;
+    pa_hook_slot
+        *sink_unlink_slot,
+        *source_unlink_slot,
+        *sink_input_move_fail_slot,
+        *source_output_move_fail_slot;
 };
 
-static pa_hook_result_t sink_hook_callback(pa_core *c, pa_sink *sink, void* userdata) {
+static pa_sink* find_evacuation_sink(pa_core *c, pa_sink_input *i, pa_sink *skip) {
+    pa_sink *target, *def;
+    uint32_t idx;
+
+    pa_assert(c);
+    pa_assert(i);
+
+    def = pa_namereg_get_default_sink(c);
+
+    if (def && def != skip && pa_sink_input_may_move_to(i, def))
+        return def;
+
+    PA_IDXSET_FOREACH(target, c->sinks, idx) {
+        if (target == def)
+            continue;
+
+        if (target == skip)
+            continue;
+
+        if (!PA_SINK_IS_LINKED(pa_sink_get_state(target)))
+            continue;
+
+        if (pa_sink_input_may_move_to(i, target))
+            return target;
+    }
+
+    pa_log_debug("No evacuation sink found.");
+    return NULL;
+}
+
+static pa_hook_result_t sink_unlink_hook_callback(pa_core *c, pa_sink *sink, void* userdata) {
     pa_sink_input *i;
     uint32_t idx;
-    pa_sink *target;
 
     pa_assert(c);
     pa_assert(sink);
@@ -65,21 +98,12 @@ static pa_hook_result_t sink_hook_callback(pa_core *c, pa_sink *sink, void* user
         return PA_HOOK_OK;
     }
 
-    if (!(target = pa_namereg_get_default_sink(c)) || target == sink) {
-
-        PA_IDXSET_FOREACH(target, c->sinks, idx)
-            if (target != sink)
-                break;
-
-        if (!target) {
-            pa_log_debug("No evacuation sink found.");
-            return PA_HOOK_OK;
-        }
-    }
+    PA_IDXSET_FOREACH(i, sink->inputs, idx) {
+        pa_sink *target;
 
-    pa_assert(target != sink);
+        if (!(target = find_evacuation_sink(c, i, sink)))
+            continue;
 
-    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);
@@ -91,9 +115,66 @@ static pa_hook_result_t sink_hook_callback(pa_core *c, pa_sink *sink, void* user
     return PA_HOOK_OK;
 }
 
-static pa_hook_result_t source_hook_callback(pa_core *c, pa_source *source, void* userdata) {
+static pa_hook_result_t sink_input_move_fail_hook_callback(pa_core *c, pa_sink_input *i, void *userdata) {
+    pa_sink *target;
+
+    pa_assert(c);
+    pa_assert(i);
+
+    /* 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 (!(target = find_evacuation_sink(c, i, NULL)))
+        return PA_HOOK_OK;
+
+    if (pa_sink_input_finish_move(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);
+        return PA_HOOK_OK;
+
+    } 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_STOP;
+    }
+}
+
+static pa_source* find_evacuation_source(pa_core *c, pa_source_output *o, pa_source *skip) {
+    pa_source *target, *def;
+    uint32_t idx;
+
+    pa_assert(c);
+    pa_assert(o);
+
+    def = pa_namereg_get_default_source(c);
+
+    if (def && def != skip && pa_source_output_may_move_to(o, def))
+        return def;
+
+    PA_IDXSET_FOREACH(target, c->sources, idx) {
+        if (target == def)
+            continue;
+
+        if (target == skip)
+            continue;
+
+        if (!target->monitor_of != !skip->monitor_of)
+            continue;
+
+        if (!PA_SOURCE_IS_LINKED(pa_source_get_state(target)))
+            continue;
+
+        if (pa_source_output_may_move_to(o, target))
+            return target;
+    }
+
+    pa_log_debug("No evacuation source found.");
+    return NULL;
+}
+
+static pa_hook_result_t source_unlink_hook_callback(pa_core *c, pa_source *source, void* userdata) {
     pa_source_output *o;
-    pa_source *target;
     uint32_t idx;
 
     pa_assert(c);
@@ -108,21 +189,12 @@ static pa_hook_result_t source_hook_callback(pa_core *c, pa_source *source, void
         return PA_HOOK_OK;
     }
 
-    if (!(target = pa_namereg_get_default_source(c)) || target == source) {
-
-        PA_IDXSET_FOREACH(target, c->sources, idx)
-            if (target != source && !target->monitor_of == !source->monitor_of)
-                break;
-
-        if (!target) {
-            pa_log_info("No evacuation source found.");
-            return PA_HOOK_OK;
-        }
-    }
+    PA_IDXSET_FOREACH(o, source->outputs, idx) {
+        pa_source *target;
 
-    pa_assert(target != source);
+        if (!(target = find_evacuation_source(c, o, source)))
+            continue;
 
-    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);
@@ -134,6 +206,31 @@ static pa_hook_result_t source_hook_callback(pa_core *c, pa_source *source, void
     return PA_HOOK_OK;
 }
 
+static pa_hook_result_t source_output_move_fail_hook_callback(pa_core *c, pa_source_output *i, void *userdata) {
+    pa_source *target;
+
+    pa_assert(c);
+    pa_assert(i);
+
+    /* 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 (!(target = find_evacuation_source(c, i, NULL)))
+        return PA_HOOK_OK;
+
+    if (pa_source_output_finish_move(i, target, FALSE) < 0) {
+        pa_log_info("Failed to move source input %u \"%s\" to %s.", i->index,
+                        pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
+        return PA_HOOK_OK;
+
+    } else {
+        pa_log_info("Sucessfully moved source input %u \"%s\" to %s.", i->index,
+                    pa_strnull(pa_proplist_gets(i->proplist, PA_PROP_APPLICATION_NAME)), target->name);
+        return PA_HOOK_STOP;
+    }
+}
+
 int pa__init(pa_module*m) {
     pa_modargs *ma;
     struct userdata *u;
@@ -148,8 +245,11 @@ int pa__init(pa_module*m) {
     m->userdata = u = pa_xnew(struct userdata, 1);
 
     /* 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);
+    u->sink_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE+20, (pa_hook_cb_t) sink_unlink_hook_callback, u);
+    u->source_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE+20, (pa_hook_cb_t) source_unlink_hook_callback, u);
+
+    u->sink_input_move_fail_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_FAIL], PA_HOOK_LATE+20, (pa_hook_cb_t) sink_input_move_fail_hook_callback, u);
+    u->source_output_move_fail_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FAIL], PA_HOOK_LATE+20, (pa_hook_cb_t) source_output_move_fail_hook_callback, u);
 
     pa_modargs_free(ma);
     return 0;
@@ -163,10 +263,15 @@ void pa__done(pa_module*m) {
     if (!(u = m->userdata))
         return;
 
-    if (u->sink_slot)
-        pa_hook_slot_free(u->sink_slot);
-    if (u->source_slot)
-        pa_hook_slot_free(u->source_slot);
+    if (u->sink_unlink_slot)
+        pa_hook_slot_free(u->sink_unlink_slot);
+    if (u->source_unlink_slot)
+        pa_hook_slot_free(u->source_unlink_slot);
+
+    if (u->sink_input_move_fail_slot)
+        pa_hook_slot_free(u->sink_input_move_fail_slot);
+    if (u->source_output_move_fail_slot)
+        pa_hook_slot_free(u->source_output_move_fail_slot);
 
     pa_xfree(u);
 }
index 8c0bb6b0083b4042d7844bfe5740854f3fb1fc0f..e560bd2805594137e1ec0724c7e3996f7faf24d7 100644 (file)
@@ -102,15 +102,16 @@ struct userdata {
     pa_idxset *subscribed;
 };
 
-#define ENTRY_VERSION 2
+#define ENTRY_VERSION 3
 
 struct entry {
     uint8_t version;
-    pa_bool_t muted_valid:1, volume_valid:1, device_valid:1;
+    pa_bool_t muted_valid:1, volume_valid:1, device_valid:1, card_valid:1;
     pa_bool_t muted:1;
     pa_channel_map channel_map;
     pa_cvolume volume;
     char device[PA_NAME_MAX];
+    char card[PA_NAME_MAX];
 } PA_GCC_PACKED;
 
 enum {
@@ -196,11 +197,21 @@ static struct entry* read_entry(struct userdata *u, const char *name) {
         goto fail;
     }
 
+    if (!memchr(e->card, 0, sizeof(e->card))) {
+        pa_log_warn("Database contains entry for stream %s with missing NUL byte in card name", name);
+        goto fail;
+    }
+
     if (e->device_valid && !pa_namereg_is_valid_name(e->device)) {
         pa_log_warn("Invalid device name stored in database for stream %s", name);
         goto fail;
     }
 
+    if (e->card_valid && !pa_namereg_is_valid_name(e->card)) {
+        pa_log_warn("Invalid card name stored in database for stream %s", name);
+        goto fail;
+    }
+
     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;
@@ -252,6 +263,10 @@ static pa_bool_t entries_equal(const struct entry *a, const struct entry *b) {
         (a->device_valid && strncmp(a->device, b->device, sizeof(a->device))))
         return FALSE;
 
+    if (a->card_valid != b->card_valid ||
+        (a->card_valid && strncmp(a->card, b->card, sizeof(a->card))))
+        return FALSE;
+
     if (a->muted_valid != b->muted_valid ||
         (a->muted_valid && (a->muted != b->muted)))
         return FALSE;
@@ -308,6 +323,11 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3
         if (sink_input->save_sink) {
             pa_strlcpy(entry.device, sink_input->sink->name, sizeof(entry.device));
             entry.device_valid = TRUE;
+
+            if (sink_input->sink->card) {
+                pa_strlcpy(entry.card, sink_input->sink->card->name, sizeof(entry.card));
+                entry.card_valid = TRUE;
+            }
         }
 
     } else {
@@ -327,6 +347,11 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3
         if (source_output->save_source) {
             pa_strlcpy(entry.device, source_output->source->name, sizeof(entry.device));
             entry.device_valid = source_output->save_source;
+
+            if (source_output->source->card) {
+                pa_strlcpy(entry.card, source_output->source->card->name, sizeof(entry.card));
+                entry.card_valid = TRUE;
+            }
         }
     }
 
@@ -368,19 +393,28 @@ static pa_hook_result_t sink_input_new_hook_callback(pa_core *c, pa_sink_input_n
     if (!(name = get_name(new_data->proplist, "sink-input")))
         return PA_HOOK_OK;
 
-    if ((e = read_entry(u, name))) {
+    if (new_data->sink)
+        pa_log_debug("Not restoring device for stream %s, because already set.", name);
+    else if ((e = read_entry(u, name))) {
+        pa_sink *s = NULL;
 
-        if (e->device_valid) {
-            pa_sink *s;
+        if (e->device_valid)
+            s = pa_namereg_get(c, e->device, PA_NAMEREG_SINK);
 
-            if ((s = pa_namereg_get(c, e->device, PA_NAMEREG_SINK))) {
-                if (!new_data->sink) {
-                    pa_log_info("Restoring device for stream %s.", name);
-                    new_data->sink = s;
-                    new_data->save_sink = TRUE;
-                } else
-                    pa_log_debug("Not restoring device for stream %s, because already set.", name);
-            }
+        if (!s && e->card_valid) {
+            pa_card *card;
+
+            if ((card = pa_namereg_get(c, e->card, PA_NAMEREG_CARD)))
+                s = pa_idxset_first(card->sinks, NULL);
+        }
+
+        /* It might happen that a stream and a sink are set up at the
+           same time, in which case we want to make sure we don't
+           interfere with that */
+        if (s && PA_SINK_IS_LINKED(pa_sink_get_state(s))) {
+            pa_log_info("Restoring device for stream %s.", name);
+            new_data->sink = s;
+            new_data->save_sink = TRUE;
         }
 
         pa_xfree(e);
@@ -455,18 +489,28 @@ static pa_hook_result_t source_output_new_hook_callback(pa_core *c, pa_source_ou
     if (!(name = get_name(new_data->proplist, "source-output")))
         return PA_HOOK_OK;
 
-    if ((e = read_entry(u, name))) {
-        pa_source *s;
+    if (new_data->source)
+        pa_log_debug("Not restoring device for stream %s, because already set", name);
+    else if ((e = read_entry(u, name))) {
+        pa_source *s = NULL;
 
-        if (e->device_valid) {
-            if ((s = pa_namereg_get(c, e->device, PA_NAMEREG_SOURCE))) {
-                if (!new_data->source) {
-                    pa_log_info("Restoring device for stream %s.", name);
-                    new_data->source = s;
-                    new_data->save_source = TRUE;
-                } else
-                    pa_log_debug("Not restoring device for stream %s, because already set", name);
-            }
+        if (e->device_valid)
+            s = pa_namereg_get(c, e->device, PA_NAMEREG_SOURCE);
+
+        if (!s && e->card_valid) {
+            pa_card *card;
+
+            if ((card = pa_namereg_get(c, e->card, PA_NAMEREG_CARD)))
+                s = pa_idxset_first(card->sources, NULL);
+        }
+
+        /* It might happen that a stream and a sink are set up at the
+           same time, in which case we want to make sure we don't
+           interfere with that */
+        if (s && PA_SOURCE_IS_LINKED(pa_source_get_state(s))) {
+            pa_log_info("Restoring device for stream %s.", name);
+            new_data->source = s;
+            new_data->save_source = TRUE;
         }
 
         pa_xfree(e);
@@ -496,6 +540,17 @@ static pa_hook_result_t sink_put_hook_callback(pa_core *c, pa_sink *sink, struct
         if (si->save_sink)
             continue;
 
+        /* Skip this if it is already in the process of being moved
+         * anyway */
+        if (!si->sink)
+            continue;
+
+        /* It might happen that a stream and a sink are set up at the
+           same time, in which case we want to make sure we don't
+           interfere with that */
+        if (!PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(si)))
+            continue;
+
         if (!(name = get_name(si->proplist, "sink-input")))
             continue;
 
@@ -534,6 +589,16 @@ static pa_hook_result_t source_put_hook_callback(pa_core *c, pa_source *source,
         if (so->direct_on_input)
             continue;
 
+        /* Skip this if it is already in the process of being moved anyway */
+        if (!so->source)
+            continue;
+
+        /* It might happen that a stream and a sink are set up at the
+           same time, in which case we want to make sure we don't
+           interfere with that */
+        if (!PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(so)))
+            continue;
+
         if (!(name = get_name(so->proplist, "source-input")))
             continue;
 
@@ -567,6 +632,9 @@ static pa_hook_result_t sink_unlink_hook_callback(pa_core *c, pa_sink *sink, str
         char *name;
         struct entry *e;
 
+        if (!si->sink)
+            continue;
+
         if (!(name = get_name(si->proplist, "sink-input")))
             continue;
 
@@ -575,7 +643,9 @@ static pa_hook_result_t sink_unlink_hook_callback(pa_core *c, pa_sink *sink, str
             if (e->device_valid) {
                 pa_sink *d;
 
-                if ((d = pa_namereg_get(c, e->device, PA_NAMEREG_SINK)) && d != sink)
+                if ((d = pa_namereg_get(c, e->device, PA_NAMEREG_SINK)) &&
+                    d != sink &&
+                    PA_SINK_IS_LINKED(pa_sink_get_state(d)))
                     pa_sink_input_move_to(si, d, TRUE);
             }
 
@@ -605,6 +675,12 @@ static pa_hook_result_t source_unlink_hook_callback(pa_core *c, pa_source *sourc
         char *name;
         struct entry *e;
 
+        if (so->direct_on_input)
+            continue;
+
+        if (!so->source)
+            continue;
+
         if (!(name = get_name(so->proplist, "source-output")))
             continue;
 
@@ -613,7 +689,9 @@ static pa_hook_result_t source_unlink_hook_callback(pa_core *c, pa_source *sourc
             if (e->device_valid) {
                 pa_source *d;
 
-                if ((d = pa_namereg_get(c, e->device, PA_NAMEREG_SOURCE)) && d != source)
+                if ((d = pa_namereg_get(c, e->device, PA_NAMEREG_SOURCE)) &&
+                    d != source &&
+                    PA_SOURCE_IS_LINKED(pa_source_get_state(d)))
                     pa_source_output_move_to(so, d, TRUE);
             }
 
index eaccea4e58f257e81b310d8d2e9210c227e1afe2..5ccb81d0659c2c3cac989d781a222e9641375e94 100644 (file)
@@ -1162,7 +1162,7 @@ static void sink_input_info_cb(pa_pdispatch *pd, uint32_t command,  uint32_t tag
     pa_assert(u->sink);
 
     if ((u->version < 11 || !!mute == !!u->sink->muted) &&
-        pa_cvolume_equal(&volume, &u->sink->virtual_volume))
+        pa_cvolume_equal(&volume, &u->sink->real_volume))
         return;
 
     pa_sink_volume_changed(u->sink, &volume);
@@ -1763,7 +1763,7 @@ static void sink_set_volume(pa_sink *sink) {
     pa_tagstruct_putu32(t, PA_COMMAND_SET_SINK_INPUT_VOLUME);
     pa_tagstruct_putu32(t, tag = u->ctag++);
     pa_tagstruct_putu32(t, u->device_index);
-    pa_tagstruct_put_cvolume(t, &sink->virtual_volume);
+    pa_tagstruct_put_cvolume(t, &sink->real_volume);
     pa_pstream_send_tagstruct(u->pstream, t);
 }
 
index 11de1ccbe9fff25d82ef10ca8f3e9f8f16989f15..0b30fd540df41af3ed9e3f872879a26f29bb8103 100644 (file)
@@ -39,6 +39,9 @@ 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);
+PA_MODULE_USAGE(
+        "tsched=<enable system timer based scheduling mode?> "
+        "ignore_dB=<ignore dB information from the device?>");
 
 struct device {
     char *path;
@@ -50,7 +53,9 @@ struct device {
 struct userdata {
     pa_core *core;
     pa_hashmap *devices;
-    pa_bool_t use_tsched;
+
+    pa_bool_t use_tsched:1;
+    pa_bool_t ignore_dB:1;
 
     struct udev* udev;
     struct udev_monitor *monitor;
@@ -62,6 +67,7 @@ struct userdata {
 
 static const char* const valid_modargs[] = {
     "tsched",
+    "ignore_dB",
     NULL
 };
 
@@ -140,12 +146,14 @@ static void card_changed(struct userdata *u, struct udev_device *dev) {
     args = pa_sprintf_malloc("device_id=\"%s\" "
                              "name=\"%s\" "
                              "card_name=\"%s\" "
-                             "tsched=%i "
+                             "tsched=%s "
+                             "ignore_dB=%s "
                              "card_properties=\"module-udev-detect.discovered=1\"",
                              path_get_card_id(path),
                              n,
                              card_name,
-                             (int) u->use_tsched);
+                             pa_yes_no(u->use_tsched),
+                             pa_yes_no(u->ignore_dB));
 
     pa_log_debug("Loading module-alsa-card with arguments '%s'", args);
     m = pa_module_load(u->core, "module-alsa-card", args);
@@ -364,6 +372,7 @@ int pa__init(pa_module *m) {
     struct udev_enumerate *enumerate = NULL;
     struct udev_list_entry *item = NULL, *first = NULL;
     int fd;
+    pa_bool_t use_tsched = TRUE, ignore_dB = FALSE;
 
     pa_assert(m);
 
@@ -375,13 +384,19 @@ int pa__init(pa_module *m) {
     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.");
+    if (pa_modargs_get_value_boolean(ma, "tsched", &use_tsched) < 0) {
+        pa_log("Failed to parse tsched= argument.");
+        goto fail;
+    }
+    u->use_tsched = use_tsched;
+
+    if (pa_modargs_get_value_boolean(ma, "ignore_dB", &ignore_dB) < 0) {
+        pa_log("Failed to parse ignore_dB= argument.");
         goto fail;
     }
+    u->ignore_dB = ignore_dB;
 
     if (!(u->udev = udev_new())) {
         pa_log("Failed to initialize udev library.");
index 91da598e3287589af22c75886662e10e24248c5d..6e484eae2cf4b754b350b9833000e5ef59cfe267 100644 (file)
@@ -48,6 +48,7 @@ static const char* const valid_modargs[] = {
 int pa__init(pa_module*m) {
     pa_modargs *ma = NULL;
     pa_bool_t restore_device = TRUE, restore_volume = TRUE;
+    pa_module *n;
     char *t;
 
     pa_assert(m);
@@ -66,13 +67,15 @@ int pa__init(pa_module*m) {
     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);
+    n = pa_module_load(m->core, "module-stream-restore", t);
     pa_xfree(t);
 
-    pa_module_unload_request(m, TRUE);
+    if (n)
+        pa_module_unload_request(m, TRUE);
 
     pa_modargs_free(ma);
-    return 0;
+
+    return n ? 0 : -1;
 
 fail:
     if (ma)
index c44b882b79b2740298361b9a7020b9edfbed67b9..71536260237dcffd1f6a26db511f7d759feba302 100644 (file)
@@ -812,11 +812,11 @@ static void sink_get_volume(pa_sink *s) {
     pa_assert(u->mixer_devmask & (SOUND_MASK_VOLUME|SOUND_MASK_PCM));
 
     if (u->mixer_devmask & SOUND_MASK_VOLUME)
-        if (pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_VOLUME, &s->sample_spec, &s->virtual_volume) >= 0)
+        if (pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_VOLUME, &s->sample_spec, &s->real_volume) >= 0)
             return;
 
     if (u->mixer_devmask & SOUND_MASK_PCM)
-        if (pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_PCM, &s->sample_spec, &s->virtual_volume) >= 0)
+        if (pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_PCM, &s->sample_spec, &s->real_volume) >= 0)
             return;
 
     pa_log_info("Device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
@@ -830,11 +830,11 @@ static void sink_set_volume(pa_sink *s) {
     pa_assert(u->mixer_devmask & (SOUND_MASK_VOLUME|SOUND_MASK_PCM));
 
     if (u->mixer_devmask & SOUND_MASK_VOLUME)
-        if (pa_oss_set_volume(u->mixer_fd, SOUND_MIXER_WRITE_VOLUME, &s->sample_spec, &s->virtual_volume) >= 0)
+        if (pa_oss_set_volume(u->mixer_fd, SOUND_MIXER_WRITE_VOLUME, &s->sample_spec, &s->real_volume) >= 0)
             return;
 
     if (u->mixer_devmask & SOUND_MASK_PCM)
-        if (pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_WRITE_PCM, &s->sample_spec, &s->virtual_volume) >= 0)
+        if (pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_WRITE_PCM, &s->sample_spec, &s->real_volume) >= 0)
             return;
 
     pa_log_info("Device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
@@ -848,11 +848,11 @@ static void source_get_volume(pa_source *s) {
     pa_assert(u->mixer_devmask & (SOUND_MASK_IGAIN|SOUND_MASK_RECLEV));
 
     if (u->mixer_devmask & SOUND_MASK_IGAIN)
-        if (pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_IGAIN, &s->sample_spec, &s->virtual_volume) >= 0)
+        if (pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_IGAIN, &s->sample_spec, &s->volume) >= 0)
             return;
 
     if (u->mixer_devmask & SOUND_MASK_RECLEV)
-        if (pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_RECLEV, &s->sample_spec, &s->virtual_volume) >= 0)
+        if (pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_READ_RECLEV, &s->sample_spec, &s->volume) >= 0)
             return;
 
     pa_log_info("Device doesn't support reading mixer settings: %s", pa_cstrerror(errno));
@@ -866,11 +866,11 @@ static void source_set_volume(pa_source *s) {
     pa_assert(u->mixer_devmask & (SOUND_MASK_IGAIN|SOUND_MASK_RECLEV));
 
     if (u->mixer_devmask & SOUND_MASK_IGAIN)
-        if (pa_oss_set_volume(u->mixer_fd, SOUND_MIXER_WRITE_IGAIN, &s->sample_spec, &s->virtual_volume) >= 0)
+        if (pa_oss_set_volume(u->mixer_fd, SOUND_MIXER_WRITE_IGAIN, &s->sample_spec, &s->volume) >= 0)
             return;
 
     if (u->mixer_devmask & SOUND_MASK_RECLEV)
-        if (pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_WRITE_RECLEV, &s->sample_spec, &s->virtual_volume) >= 0)
+        if (pa_oss_get_volume(u->mixer_fd, SOUND_MIXER_WRITE_RECLEV, &s->sample_spec, &s->volume) >= 0)
             return;
 
     pa_log_info("Device doesn't support writing mixer settings: %s", pa_cstrerror(errno));
index 9699132df3981a871b53667a43dc6b1627ebf49d..ac48ab105f7aa0f7e1975916f0fe34e7dcc188f5 100644 (file)
@@ -283,15 +283,15 @@ static void sink_set_volume_cb(pa_sink *s) {
     /* 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);
+    v = pa_cvolume_max(&s->real_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_sw_cvolume_divide(&s->soft_volume, &s->real_volume, &hw);
 
-    pa_log_debug("Requested volume: %s", pa_cvolume_snprint(t, sizeof(t), &s->virtual_volume));
+    pa_log_debug("Requested volume: %s", pa_cvolume_snprint(t, sizeof(t), &s->real_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));
 
index 13ecde2b0fdcfdc29055c661d29420b49d31b309..ab453e61aaa50c215be2ecb44945f20f9f0160c1 100644 (file)
@@ -38,6 +38,7 @@ struct rm_monitor {
 
        char *device_name;
        char *service_name;
+       char *match;
 
        DBusConnection *connection;
 
@@ -51,12 +52,18 @@ struct rm_monitor {
 
 #define SERVICE_PREFIX "org.freedesktop.ReserveDevice1."
 
+#define SERVICE_FILTER                         \
+       "type='signal',"                        \
+       "sender='" DBUS_SERVICE_DBUS "',"       \
+       "interface='" DBUS_INTERFACE_DBUS "',"  \
+       "member='NameOwnerChanged',"            \
+       "arg0='%s'"
+
 static DBusHandlerResult filter_handler(
        DBusConnection *c,
        DBusMessage *s,
        void *userdata) {
 
-       DBusMessage *reply;
        rm_monitor *m;
        DBusError error;
 
@@ -97,31 +104,10 @@ static DBusHandlerResult filter_handler(
                }
        }
 
-       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;
+       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 }
 
 int rm_watch(
@@ -175,11 +161,13 @@ int rm_watch(
 
        m->filtering = 1;
 
-       dbus_bus_add_match(m->connection,
-                          "type='signal',"
-                          "sender='" DBUS_SERVICE_DBUS "',"
-                          "interface='" DBUS_INTERFACE_DBUS "',"
-                          "member='NameOwnerChanged'", error);
+       if (!(m->match = malloc(sizeof(SERVICE_FILTER) - 2 + strlen(m->service_name)))) {
+               r = -ENOMEM;
+               goto fail;
+       }
+
+       sprintf(m->match, SERVICE_FILTER, m->service_name);
+       dbus_bus_add_match(m->connection, m->match, error);
 
        if (dbus_error_is_set(error)) {
                r = -EIO;
@@ -220,10 +208,8 @@ void rm_release(rm_monitor *m) {
        if (m->matching)
                dbus_bus_remove_match(
                        m->connection,
-                       "type='signal',"
-                       "sender='" DBUS_SERVICE_DBUS "',"
-                       "interface='" DBUS_INTERFACE_DBUS "',"
-                       "member='NameOwnerChanged'", NULL);
+                       m->match,
+                       NULL);
 
        if (m->filtering)
                dbus_connection_remove_filter(
@@ -233,6 +219,7 @@ void rm_release(rm_monitor *m) {
 
        free(m->device_name);
        free(m->service_name);
+       free(m->match);
 
        if (m->connection)
                dbus_connection_unref(m->connection);
index 5597f177df359e662dd530b1c70ebaa715141a99..b4c168cf78983d173b3ef35071d7ebafa2122e12 100644 (file)
@@ -291,7 +291,6 @@ static DBusHandlerResult filter_handler(
        DBusMessage *m,
        void *userdata) {
 
-       DBusMessage *reply;
        rd_device *d;
        DBusError error;
 
@@ -323,35 +322,13 @@ static DBusHandlerResult filter_handler(
                                rd_release(d);
                        }
 
-                       return DBUS_HANDLER_RESULT_HANDLED;
                }
        }
 
-       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
-
 invalid:
-       if (!(reply = dbus_message_new_error(
-                     m,
-                     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;
+       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 }
 
 
index 5caf8272be7b21816594f18d66b0746638868ccd..c195c045e063aae5f3d325fa28e3989aa166e695 100644 (file)
@@ -361,7 +361,7 @@ static void sink_input_attach(pa_sink_input *i) {
     pa_assert_se(s = i->userdata);
 
     pa_assert(!s->rtpoll_item);
-    s->rtpoll_item = pa_rtpoll_item_new(i->sink->rtpoll, PA_RTPOLL_LATE, 1);
+    s->rtpoll_item = pa_rtpoll_item_new(i->sink->thread_info.rtpoll, PA_RTPOLL_LATE, 1);
 
     p = pa_rtpoll_item_get_pollfd(s->rtpoll_item, NULL);
     p->fd = s->rtp_context.fd;
index 8882301276c06d2da4f0bd6a98c6af2326f679bc..9b5162628c6689db47b2a90fa0e703b1aa4236c9 100644 (file)
@@ -219,11 +219,11 @@ pa_channel_map* pa_channel_map_init_auto(pa_channel_map *m, unsigned channels, p
 
                 case 6:
                     m->map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
-                    m->map[1] = PA_CHANNEL_POSITION_REAR_LEFT;
+                    m->map[1] = PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER;
                     m->map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
                     m->map[3] = PA_CHANNEL_POSITION_FRONT_RIGHT;
-                    m->map[4] = PA_CHANNEL_POSITION_REAR_RIGHT;
-                    m->map[5] = PA_CHANNEL_POSITION_LFE;
+                    m->map[4] = PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
+                    m->map[5] = PA_CHANNEL_POSITION_REAR_CENTER;
                     return m;
 
                 case 5:
@@ -247,7 +247,7 @@ pa_channel_map* pa_channel_map_init_auto(pa_channel_map *m, unsigned channels, p
                     m->map[0] = PA_CHANNEL_POSITION_LEFT;
                     m->map[1] = PA_CHANNEL_POSITION_CENTER;
                     m->map[2] = PA_CHANNEL_POSITION_RIGHT;
-                    m->map[3] = PA_CHANNEL_POSITION_LFE;
+                    m->map[3] = PA_CHANNEL_POSITION_REAR_CENTER;
                     return m;
 
                 default:
@@ -299,6 +299,8 @@ pa_channel_map* pa_channel_map_init_auto(pa_channel_map *m, unsigned channels, p
 
         case PA_CHANNEL_MAP_WAVEEX:
 
+            /* Following http://www.microsoft.com/whdc/device/audio/multichaud.mspx#EKLAC */
+
             switch (channels) {
                 case 1:
                     m->map[0] = PA_CHANNEL_POSITION_MONO;
@@ -451,6 +453,10 @@ int pa_channel_map_equal(const pa_channel_map *a, const pa_channel_map *b) {
     pa_assert(b);
 
     pa_return_val_if_fail(pa_channel_map_valid(a), 0);
+
+    if (PA_UNLIKELY(a == b))
+        return 1;
+
     pa_return_val_if_fail(pa_channel_map_valid(b), 0);
 
     if (a->channels != b->channels)
@@ -639,6 +645,10 @@ int pa_channel_map_superset(const pa_channel_map *a, const pa_channel_map *b) {
     pa_assert(b);
 
     pa_return_val_if_fail(pa_channel_map_valid(a), 0);
+
+    if (PA_UNLIKELY(a == b))
+        return 1;
+
     pa_return_val_if_fail(pa_channel_map_valid(b), 0);
 
     am = pa_channel_map_mask(a);
index d7901ac210d63ff0e6e1d5939c1b80f12678aef2..469effc86402dfa111e3a9a1a0db3a6e45dcc328 100644 (file)
@@ -216,17 +216,27 @@ typedef enum pa_channel_map_def {
     PA_CHANNEL_MAP_AIFF,
     /**< The mapping from RFC3551, which is based on AIFF-C */
 
+/** \cond fulldocs */
     PA_CHANNEL_MAP_ALSA,
-    /**< The default mapping used by ALSA */
+    /**< The default mapping used by ALSA. This mapping is probably
+     * not too useful since ALSA's default channel mapping depends on
+     * the device string used. */
+/** \endcond */
 
     PA_CHANNEL_MAP_AUX,
     /**< Only aux channels */
 
     PA_CHANNEL_MAP_WAVEEX,
-    /**< Microsoft's WAVEFORMATEXTENSIBLE mapping */
+    /**< Microsoft's WAVEFORMATEXTENSIBLE mapping. This mapping works
+     * as if all LSBs of dwChannelMask are set.  */
 
+/** \cond fulldocs */
     PA_CHANNEL_MAP_OSS,
-    /**< The default channel mapping used by OSS as defined in the OSS 4.0 API specs */
+    /**< The default channel mapping used by OSS as defined in the OSS
+     * 4.0 API specs. This mapping is probably not too useful since
+     * the OSS API has changed in this respect and no longer knows a
+     * default channel mapping based on the number of channels. */
+/** \endcond */
 
     /**< Upper limit of valid channel mapping definitions */
     PA_CHANNEL_MAP_DEF_MAX,
@@ -282,7 +292,7 @@ pa_channel_map* pa_channel_map_init_extend(pa_channel_map *m, unsigned channels,
 /** Return a text label for the specified channel position */
 const char* pa_channel_position_to_string(pa_channel_position_t pos) PA_GCC_PURE;
 
-/* The inverse of pa_channel_position_to_string(). \since 0.9.16 */
+/** The inverse of pa_channel_position_to_string(). \since 0.9.16 */
 pa_channel_position_t pa_channel_position_from_string(const char *s) PA_GCC_PURE;
 
 /** Return a human readable text label for the specified channel position. \since 0.9.7 */
index 940d0b673286bbba4f3e42cdafa1c69d3cec6254..4aa4ba1fc82cf5e66378129cc6df9942f8c4e3db 100644 (file)
@@ -92,16 +92,17 @@ int pa_client_conf_load(pa_client_conf *c, const char *filename) {
 
     /* Prepare the configuration parse table */
     pa_config_item table[] = {
-        { "daemon-binary",          pa_config_parse_string,  &c->daemon_binary, NULL },
-        { "extra-arguments",        pa_config_parse_string,  &c->extra_arguments, NULL },
-        { "default-sink",           pa_config_parse_string,  &c->default_sink, NULL },
-        { "default-source",         pa_config_parse_string,  &c->default_source, NULL },
-        { "default-server",         pa_config_parse_string,  &c->default_server, NULL },
-        { "autospawn",              pa_config_parse_bool,    &c->autospawn, NULL },
-        { "cookie-file",            pa_config_parse_string,  &c->cookie_file, NULL },
-        { "disable-shm",            pa_config_parse_bool,    &c->disable_shm, NULL },
-        { "shm-size-bytes",         pa_config_parse_size,    &c->shm_size, NULL },
-        { NULL,                     NULL,                    NULL, NULL },
+        { "daemon-binary",          pa_config_parse_string,   &c->daemon_binary, NULL },
+        { "extra-arguments",        pa_config_parse_string,   &c->extra_arguments, NULL },
+        { "default-sink",           pa_config_parse_string,   &c->default_sink, NULL },
+        { "default-source",         pa_config_parse_string,   &c->default_source, NULL },
+        { "default-server",         pa_config_parse_string,   &c->default_server, NULL },
+        { "autospawn",              pa_config_parse_bool,     &c->autospawn, NULL },
+        { "cookie-file",            pa_config_parse_string,   &c->cookie_file, NULL },
+        { "disable-shm",            pa_config_parse_bool,     &c->disable_shm, NULL },
+        { "enable-shm",             pa_config_parse_not_bool, &c->disable_shm, NULL },
+        { "shm-size-bytes",         pa_config_parse_size,     &c->shm_size, NULL },
+        { NULL,                     NULL,                     NULL, NULL },
     };
 
     if (filename) {
index 579bcc2046fc16665df5130498bb4d43ac1ac4a9..6c8d371c4c9e4055ca1765ab42aac0a2a5ef97f6 100644 (file)
@@ -29,5 +29,5 @@
 
 ; cookie-file =
 
-; disable-shm = no
+; enable-shm = yes
 ; shm-size-bytes = 0 # setting this 0 will use the system-default, usually 64 MiB
index 7c3717fa8f6c424aaf2e5af5cf8ecfc7ba7e390a..894ab2e0f964e157f2d7b54919d8ac9a09a01184 100644 (file)
@@ -707,10 +707,13 @@ static int context_autospawn(pa_context *c) {
         if (c->spawn_api.atfork)
             c->spawn_api.atfork();
 
+        /* We leave most of the cleaning up of the process environment
+         * to the executable. We only clean up the file descriptors to
+         * make sure the executable can actually be loaded
+         * correctly. */
         pa_close_all(-1);
 
         /* Setup argv */
-
         argv[n++] = c->conf->daemon_binary;
         argv[n++] = "--start";
 
index 0f19f8ebf639d81650e3c75340a03147af9d0619..d5d38eda5af1cfb21901f89e27b3d012b10cb605 100644 (file)
@@ -125,6 +125,10 @@ int pa_sample_spec_equal(const pa_sample_spec*a, const pa_sample_spec*b) {
     pa_assert(b);
 
     pa_return_val_if_fail(pa_sample_spec_valid(a), 0);
+
+    if (PA_UNLIKELY(a == b))
+        return 1;
+
     pa_return_val_if_fail(pa_sample_spec_valid(b), 0);
 
     return
index 72d49e11e91514e4bdc1b13117cdda8b67543c8f..2bc2b1e4ae0fd1dc189ca02ce73ae15147d2550e 100644 (file)
@@ -867,7 +867,7 @@ static void automatic_buffer_attr(pa_stream *s, pa_buffer_attr *attr, const pa_s
 
 void pa_create_stream_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
     pa_stream *s = userdata;
-    uint32_t requested_bytes;
+    uint32_t requested_bytes = 0;
 
     pa_assert(pd);
     pa_assert(s);
index 42cde5b9676bd4d0c313854601bfda277be0c908..234c3f72862d5f6aad754eacfe5aad3a8b2ca8bc 100644 (file)
@@ -40,6 +40,10 @@ int pa_cvolume_equal(const pa_cvolume *a, const pa_cvolume *b) {
     pa_assert(b);
 
     pa_return_val_if_fail(pa_cvolume_valid(a), 0);
+
+    if (PA_UNLIKELY(a == b))
+        return 1;
+
     pa_return_val_if_fail(pa_cvolume_valid(b), 0);
 
     if (a->channels != b->channels)
@@ -122,7 +126,7 @@ pa_volume_t pa_cvolume_avg_mask(const pa_cvolume *a, const pa_channel_map *cm, p
 }
 
 pa_volume_t pa_cvolume_max(const pa_cvolume *a) {
-    pa_volume_t m = 0;
+    pa_volume_t m = PA_VOLUME_MUTED;
     unsigned c;
 
     pa_assert(a);
@@ -135,8 +139,22 @@ pa_volume_t pa_cvolume_max(const pa_cvolume *a) {
     return m;
 }
 
+pa_volume_t pa_cvolume_min(const pa_cvolume *a) {
+    pa_volume_t m = PA_VOLUME_MAX;
+    unsigned c;
+
+    pa_assert(a);
+    pa_return_val_if_fail(pa_cvolume_valid(a), PA_VOLUME_MUTED);
+
+    for (c = 0; c < a->channels; c++)
+        if (a->values[c] < m)
+            m = a->values[c];
+
+    return m;
+}
+
 pa_volume_t pa_cvolume_max_mask(const pa_cvolume *a, const pa_channel_map *cm, pa_channel_position_mask_t mask) {
-    pa_volume_t m = 0;
+    pa_volume_t m = PA_VOLUME_MUTED;
     unsigned c, n;
 
     pa_assert(a);
@@ -158,17 +176,42 @@ pa_volume_t pa_cvolume_max_mask(const pa_cvolume *a, const pa_channel_map *cm, p
     return m;
 }
 
+pa_volume_t pa_cvolume_min_mask(const pa_cvolume *a, const pa_channel_map *cm, pa_channel_position_mask_t mask) {
+    pa_volume_t m = PA_VOLUME_MAX;
+    unsigned c, n;
+
+    pa_assert(a);
+
+    if (!cm)
+        return pa_cvolume_min(a);
+
+    pa_return_val_if_fail(pa_cvolume_compatible_with_channel_map(a, cm), PA_VOLUME_MUTED);
+
+    for (c = n = 0; c < a->channels; c++) {
+
+        if (!(PA_CHANNEL_POSITION_MASK(cm->map[c]) & mask))
+            continue;
+
+        if (a->values[c] < m)
+            m = a->values[c];
+    }
+
+    return m;
+}
+
 pa_volume_t pa_sw_volume_multiply(pa_volume_t a, pa_volume_t b) {
-    return pa_sw_volume_from_linear(pa_sw_volume_to_linear(a) * pa_sw_volume_to_linear(b));
+
+    /* cbrt((a/PA_VOLUME_NORM)^3*(b/PA_VOLUME_NORM)^3)*PA_VOLUME_NORM = a*b/PA_VOLUME_NORM */
+
+    return (pa_volume_t) (((uint64_t) a * (uint64_t) b + (uint64_t) PA_VOLUME_NORM / 2ULL) / (uint64_t) PA_VOLUME_NORM);
 }
 
 pa_volume_t pa_sw_volume_divide(pa_volume_t a, pa_volume_t b) {
-    double v = pa_sw_volume_to_linear(b);
 
-    if (v <= 0)
+    if (b <= PA_VOLUME_MUTED)
         return 0;
 
-    return pa_sw_volume_from_linear(pa_sw_volume_to_linear(a) / v);
+    return (pa_volume_t) (((uint64_t) a * (uint64_t) PA_VOLUME_NORM + (uint64_t) b / 2ULL) / (uint64_t) b);
 }
 
 /* Amplitude, not power */
@@ -205,9 +248,12 @@ pa_volume_t pa_sw_volume_from_linear(double v) {
      *
      * http://www.robotplanet.dk/audio/audio_gui_design/
      * http://lists.linuxaudio.org/pipermail/linux-audio-dev/2009-May/thread.html#23151
+     *
+     * We make sure that the conversion to linear and back yields the
+     * same volume value! That's why we need the lround() below!
      */
 
-    return (pa_volume_t) (cbrt(v) * PA_VOLUME_NORM);
+    return (pa_volume_t) lround(cbrt(v) * PA_VOLUME_NORM);
 }
 
 double pa_sw_volume_to_linear(pa_volume_t v) {
@@ -246,7 +292,7 @@ char *pa_cvolume_snprint(char *s, size_t l, const pa_cvolume *c) {
         l -= pa_snprintf(e, l, "%s%u: %3u%%",
                       first ? "" : " ",
                       channel,
-                      (c->values[channel]*100)/PA_VOLUME_NORM);
+                      (c->values[channel]*100+PA_VOLUME_NORM/2)/PA_VOLUME_NORM);
 
         e = strchr(e, 0);
         first = FALSE;
@@ -266,7 +312,7 @@ char *pa_volume_snprint(char *s, size_t l, pa_volume_t v) {
         return s;
     }
 
-    pa_snprintf(s, l, "%3u%%", (v*100)/PA_VOLUME_NORM);
+    pa_snprintf(s, l, "%3u%%", (v*100+PA_VOLUME_NORM/2)/PA_VOLUME_NORM);
     return s;
 }
 
@@ -812,3 +858,55 @@ pa_volume_t pa_cvolume_get_position(
 
     return v;
 }
+
+pa_cvolume* pa_cvolume_merge(pa_cvolume *dest, const pa_cvolume *a, const pa_cvolume *b) {
+    unsigned i;
+
+    pa_assert(dest);
+    pa_assert(a);
+    pa_assert(b);
+
+    pa_return_val_if_fail(pa_cvolume_valid(a), NULL);
+    pa_return_val_if_fail(pa_cvolume_valid(b), NULL);
+
+    for (i = 0; i < a->channels && i < b->channels; i++)
+        dest->values[i] = PA_MAX(a->values[i], b->values[i]);
+
+    dest->channels = (uint8_t) i;
+
+    return dest;
+}
+
+pa_cvolume* pa_cvolume_inc(pa_cvolume *v, pa_volume_t inc) {
+    pa_volume_t m;
+
+    pa_assert(v);
+
+    pa_return_val_if_fail(pa_cvolume_valid(v), NULL);
+
+    m = pa_cvolume_max(v);
+
+    if (m >= PA_VOLUME_MAX - inc)
+        m = PA_VOLUME_MAX;
+    else
+        m += inc;
+
+    return pa_cvolume_scale(v, m);
+}
+
+pa_cvolume* pa_cvolume_dec(pa_cvolume *v, pa_volume_t dec) {
+    pa_volume_t m;
+
+    pa_assert(v);
+
+    pa_return_val_if_fail(pa_cvolume_valid(v), NULL);
+
+    m = pa_cvolume_max(v);
+
+    if (m <= PA_VOLUME_MUTED + dec)
+        m = PA_VOLUME_MUTED;
+    else
+        m -= dec;
+
+    return pa_cvolume_scale(v, m);
+}
index 05b7ebb404678a05fdaa18d2f8681e4b1e5e84ce..543b0af161ad0babf1dc72a61455e38babad8ec4 100644 (file)
@@ -195,6 +195,16 @@ pa_volume_t pa_cvolume_max(const pa_cvolume *a) PA_GCC_PURE;
  * \since 0.9.16 */
 pa_volume_t pa_cvolume_max_mask(const pa_cvolume *a, const pa_channel_map *cm, pa_channel_position_mask_t mask) PA_GCC_PURE;
 
+/** Return the minimum volume of all channels. \since 0.9.16 */
+pa_volume_t pa_cvolume_min(const pa_cvolume *a) PA_GCC_PURE;
+
+/** Return the minimum volume of all channels that are included in the
+ * specified channel map with the specified channel position mask. If
+ * cm is NULL this call is identical to pa_cvolume_min(). If no
+ * channel is selected the returned value will be PA_VOLUME_MUTED.
+ * \since 0.9.16 */
+pa_volume_t pa_cvolume_min_mask(const pa_cvolume *a, const pa_channel_map *cm, pa_channel_position_mask_t mask) PA_GCC_PURE;
+
 /** Return TRUE when the passed cvolume structure is valid, FALSE otherwise */
 int pa_cvolume_valid(const pa_cvolume *v) PA_GCC_PURE;
 
@@ -213,11 +223,13 @@ int pa_cvolume_channels_equal_to(const pa_cvolume *a, pa_volume_t v) PA_GCC_PURE
 pa_volume_t pa_sw_volume_multiply(pa_volume_t a, pa_volume_t b) PA_GCC_CONST;
 
 /** Multiply two per-channel volumes and return the result in
- * *dest. This is only valid for software volumes! */
+ * *dest. This is only valid for software volumes! a, b and dest may
+ * point to the same structure. */
 pa_cvolume *pa_sw_cvolume_multiply(pa_cvolume *dest, const pa_cvolume *a, const pa_cvolume *b);
 
 /** Multiply a per-channel volume with a scalar volume and return the
- * result in *dest. This is only valid for software volumes! \since
+ * result in *dest. This is only valid for software volumes! a
+ * and dest may point to the same structure. \since
  * 0.9.16 */
 pa_cvolume *pa_sw_cvolume_multiply_scalar(pa_cvolume *dest, const pa_cvolume *a, pa_volume_t b);
 
@@ -228,11 +240,13 @@ pa_cvolume *pa_sw_cvolume_multiply_scalar(pa_cvolume *dest, const pa_cvolume *a,
 pa_volume_t pa_sw_volume_divide(pa_volume_t a, pa_volume_t b) PA_GCC_CONST;
 
 /** Divide two per-channel volumes and return the result in
- * *dest. This is only valid for software volumes! \since 0.9.13 */
+ * *dest. This is only valid for software volumes! a, b
+ * and dest may point to the same structure. \since 0.9.13 */
 pa_cvolume *pa_sw_cvolume_divide(pa_cvolume *dest, const pa_cvolume *a, const pa_cvolume *b);
 
 /** Divide a per-channel volume by a scalar volume and return the
- * result in *dest. This is only valid for software volumes! \since
+ * result in *dest. This is only valid for software volumes! a
+ * and dest may point to the same structure. \since
  * 0.9.16 */
 pa_cvolume *pa_sw_cvolume_divide_scalar(pa_cvolume *dest, const pa_cvolume *a, pa_volume_t b);
 
@@ -326,6 +340,19 @@ pa_cvolume* pa_cvolume_set_position(pa_cvolume *cv, const pa_channel_map *map, p
  * position by calling pa_channel_map_has_position(). \since 0.9.16 */
 pa_volume_t pa_cvolume_get_position(pa_cvolume *cv, const pa_channel_map *map, pa_channel_position_t t) PA_GCC_PURE;
 
+/** This goes through all channels in a and b and sets the
+ * corresponding channel in dest to the greater volume of both. a, b
+ * and dest may point to the same structure. \since 0.9.16 */
+pa_cvolume* pa_cvolume_merge(pa_cvolume *dest, const pa_cvolume *a, const pa_cvolume *b);
+
+/** Increase the volume passed in by 'inc'. The proportions between
+ * the channels are kept. \since 0.9.16 */
+pa_cvolume* pa_cvolume_inc(pa_cvolume *v, pa_volume_t inc);
+
+/** Increase the volume passed in by 'inc'. The proportions between
+ * the channels are kept. \since 0.9.16 */
+pa_cvolume* pa_cvolume_dec(pa_cvolume *v, pa_volume_t dec);
+
 PA_C_DECL_END
 
 #endif
index 083d9de21bd20e0135b996268cea957198bd7418..b0804f79ff8781c160777da3a5beb66f24269baf 100644 (file)
 #include <unistd.h>
 #include <errno.h>
 
+#include <pulse/xmalloc.h>
+
 #include <pulsecore/atomic.h>
+#include <pulsecore/macro.h>
 #include <pulsecore/log.h>
 #include <pulsecore/thread.h>
 #include <pulsecore/semaphore.h>
 #include <pulsecore/macro.h>
 #include <pulsecore/core-util.h>
 #include <pulsecore/flist.h>
-#include <pulse/xmalloc.h>
 
 #include "asyncmsgq.h"
 
@@ -76,7 +78,7 @@ static void asyncmsgq_free(pa_asyncmsgq *a) {
     struct asyncmsgq_item *i;
     pa_assert(a);
 
-    while ((i = pa_asyncq_pop(a->asyncq, 0))) {
+    while ((i = pa_asyncq_pop(a->asyncq, FALSE))) {
 
         pa_assert(!i->semaphore);
 
@@ -246,7 +248,7 @@ int pa_asyncmsgq_wait_for(pa_asyncmsgq *a, int code) {
         pa_memchunk chunk;
         int ret;
 
-        if (pa_asyncmsgq_get(a, &o, &c, &data, &offset, &chunk, 1) < 0)
+        if (pa_asyncmsgq_get(a, &o, &c, &data, &offset, &chunk, TRUE) < 0)
             return -1;
 
         ret = pa_asyncmsgq_dispatch(o, c, data, offset, &chunk);
@@ -269,7 +271,7 @@ int pa_asyncmsgq_process_one(pa_asyncmsgq *a) {
 
     pa_assert(PA_REFCNT_VALUE(a) > 0);
 
-    if (pa_asyncmsgq_get(a, &object, &code, &data, &offset, &chunk, 0) < 0)
+    if (pa_asyncmsgq_get(a, &object, &code, &data, &offset, &chunk, FALSE) < 0)
         return 0;
 
     pa_asyncmsgq_ref(a);
@@ -323,3 +325,35 @@ int pa_asyncmsgq_dispatch(pa_msgobject *object, int code, void *userdata, int64_
 
     return 0;
 }
+
+void pa_asyncmsgq_flush(pa_asyncmsgq *a, pa_bool_t run) {
+    pa_assert(PA_REFCNT_VALUE(a) > 0);
+
+    for (;;) {
+        pa_msgobject *object;
+        int code;
+        void *data;
+        int64_t offset;
+        pa_memchunk chunk;
+        int ret;
+
+        if (pa_asyncmsgq_get(a, &object, &code, &data, &offset, &chunk, FALSE) < 0)
+            return;
+
+        if (!run) {
+            pa_asyncmsgq_done(a, -1);
+            continue;
+        }
+
+        pa_asyncmsgq_ref(a);
+        ret = pa_asyncmsgq_dispatch(object, code, data, offset, &chunk);
+        pa_asyncmsgq_done(a, ret);
+        pa_asyncmsgq_unref(a);
+    }
+}
+
+pa_bool_t pa_asyncmsgq_dispatching(pa_asyncmsgq *a) {
+    pa_assert(PA_REFCNT_VALUE(a) > 0);
+
+    return !!a->current;
+}
index 1f38207a34837b80ffbfb2feb48d1bfdd7fff322..1085c2f025b8c86c690ce3a3049910e443d989e4 100644 (file)
@@ -66,6 +66,8 @@ void pa_asyncmsgq_done(pa_asyncmsgq *q, int ret);
 int pa_asyncmsgq_wait_for(pa_asyncmsgq *a, int code);
 int pa_asyncmsgq_process_one(pa_asyncmsgq *a);
 
+void pa_asyncmsgq_flush(pa_asyncmsgq *a, pa_bool_t run);
+
 /* For the reading side */
 int pa_asyncmsgq_read_fd(pa_asyncmsgq *q);
 int pa_asyncmsgq_read_before_poll(pa_asyncmsgq *a);
@@ -76,4 +78,6 @@ int pa_asyncmsgq_write_fd(pa_asyncmsgq *q);
 void pa_asyncmsgq_write_before_poll(pa_asyncmsgq *a);
 void pa_asyncmsgq_write_after_poll(pa_asyncmsgq *a);
 
+pa_bool_t pa_asyncmsgq_dispatching(pa_asyncmsgq *a);
+
 #endif
index 56ebb8e5d6af145ef6dee221125b0b6563c3996d..85b6e00e898fcccd8a5ad13b26590e74385da858 100644 (file)
@@ -39,6 +39,7 @@ struct pa_aupdate {
     pa_atomic_t read_lock;
     pa_mutex *write_lock;
     pa_semaphore *semaphore;
+    pa_bool_t swapped;
 };
 
 pa_aupdate *pa_aupdate_new(void) {
@@ -101,6 +102,8 @@ unsigned pa_aupdate_write_begin(pa_aupdate *a) {
 
     n = (unsigned) pa_atomic_load(&a->read_lock);
 
+    a->swapped = FALSE;
+
     return !WHICH(n);
 }
 
@@ -119,11 +122,16 @@ unsigned pa_aupdate_write_swap(pa_aupdate *a) {
             break;
     }
 
+    a->swapped = TRUE;
+
     return WHICH(n);
 }
 
 void pa_aupdate_write_end(pa_aupdate *a) {
     pa_assert(a);
 
+    if (!a->swapped)
+        pa_aupdate_write_swap(a);
+
     pa_mutex_unlock(a->write_lock);
 }
index 072e382d0da09bfeabe3f9029b64c1313f7fd94d..fb38ffa22ec9e3f3881a26f3dd8bd3bdcbc067f1 100644 (file)
@@ -93,6 +93,10 @@ unsigned pa_aupdate_write_swap(pa_aupdate *a);
  *    pa_update_write_end(a)
  * }
  *
+ * In some cases keeping both structures up-to-date might not be
+ * necessary, since they are fully rebuilt on each iteration
+ * anyway. In that case you may leave the _write_swap() call out, it
+ * will then be done implicitly in the _write_end() invocation.
  */
 
 #endif
index e2c3c066c3c009a75e8d9883f6805109061b9b01..6ec746475c0f187efa3bf65081aff39df4848a25 100644 (file)
@@ -530,7 +530,7 @@ static int pa_cli_command_sink_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *bu
     }
 
     pa_cvolume_set(&cvolume, sink->sample_spec.channels, volume);
-    pa_sink_set_volume(sink, &cvolume, TRUE, TRUE, TRUE, TRUE);
+    pa_sink_set_volume(sink, &cvolume, TRUE, TRUE);
     return 0;
 }
 
@@ -1586,7 +1586,7 @@ static int pa_cli_command_dump(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_b
             nl = 1;
         }
 
-        pa_strbuf_printf(buf, "set-sink-volume %s 0x%03x\n", sink->name, pa_cvolume_avg(pa_sink_get_volume(sink, FALSE, TRUE)));
+        pa_strbuf_printf(buf, "set-sink-volume %s 0x%03x\n", sink->name, pa_cvolume_avg(pa_sink_get_volume(sink, FALSE)));
         pa_strbuf_printf(buf, "set-sink-mute %s %s\n", sink->name, pa_yes_no(pa_sink_get_mute(sink, FALSE)));
         pa_strbuf_printf(buf, "suspend-sink %s %s\n", sink->name, pa_yes_no(pa_sink_get_state(sink) == PA_SINK_SUSPENDED));
     }
index 9395513d1ce7f21f1bc9a05e01dbb3b85a8608a5..c7a178d6c2ffe2cd6d528ace5664c6ac6a6b37ed 100644 (file)
@@ -262,10 +262,10 @@ char *pa_sink_list_to_string(pa_core *c) {
             sink->suspend_cause & PA_SUSPEND_APPLICATION ? "APPLICATION " : "",
             sink->suspend_cause & PA_SUSPEND_IDLE ? "IDLE " : "",
             sink->suspend_cause & PA_SUSPEND_SESSION ? "SESSION" : "",
-            pa_cvolume_snprint(cv, sizeof(cv), pa_sink_get_volume(sink, FALSE, FALSE)),
+            pa_cvolume_snprint(cv, sizeof(cv), pa_sink_get_volume(sink, FALSE)),
             sink->flags & PA_SINK_DECIBEL_VOLUME ? "\n\t        " : "",
-            sink->flags & PA_SINK_DECIBEL_VOLUME ? pa_sw_cvolume_snprint_dB(cvdb, sizeof(cvdb), pa_sink_get_volume(sink, FALSE, FALSE)) : "",
-            pa_cvolume_get_balance(pa_sink_get_volume(sink, FALSE, FALSE), &sink->channel_map),
+            sink->flags & PA_SINK_DECIBEL_VOLUME ? pa_sw_cvolume_snprint_dB(cvdb, sizeof(cvdb), pa_sink_get_volume(sink, FALSE)) : "",
+            pa_cvolume_get_balance(pa_sink_get_volume(sink, FALSE), &sink->channel_map),
             pa_volume_snprint(v, sizeof(v), sink->base_volume),
             sink->flags & PA_SINK_DECIBEL_VOLUME ? "\n\t             " : "",
             sink->flags & PA_SINK_DECIBEL_VOLUME ? pa_sw_volume_snprint_dB(vdb, sizeof(vdb), sink->base_volume) : "",
@@ -296,7 +296,7 @@ char *pa_sink_list_to_string(pa_core *c) {
             pa_strbuf_printf(
                     s,
                     "\tfixed latency: %0.2f ms\n",
-                    (double) pa_sink_get_requested_latency(sink) / PA_USEC_PER_MSEC);
+                    (double) pa_sink_get_fixed_latency(sink) / PA_USEC_PER_MSEC);
 
         if (sink->card)
             pa_strbuf_printf(s, "\tcard: %u <%s>\n", sink->card->index, sink->card->name);
@@ -415,7 +415,7 @@ char *pa_source_list_to_string(pa_core *c) {
             pa_strbuf_printf(
                     s,
                     "\tfixed latency: %0.2f ms\n",
-                    (double) pa_source_get_requested_latency(source) / PA_USEC_PER_MSEC);
+                    (double) pa_source_get_fixed_latency(source) / PA_USEC_PER_MSEC);
 
         if (source->monitor_of)
             pa_strbuf_printf(s, "\tmonitor_of: %u\n", source->monitor_of->index);
@@ -482,7 +482,7 @@ char *pa_source_output_list_to_string(pa_core *c) {
             s,
             "    index: %u\n"
             "\tdriver: <%s>\n"
-            "\tflags: %s%s%s%s%s%s%s%s%s%s\n"
+            "\tflags: %s%s%s%s%s%s%s%s%s%s%s\n"
             "\tstate: %s\n"
             "\tsource: %u <%s>\n"
             "\tcurrent latency: %0.2f ms\n"
@@ -501,7 +501,8 @@ char *pa_source_output_list_to_string(pa_core *c) {
             o->flags & PA_SOURCE_OUTPUT_FIX_RATE ? "FIX_RATE " : "",
             o->flags & PA_SOURCE_OUTPUT_FIX_CHANNELS ? "FIX_CHANNELS " : "",
             o->flags & PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND ? "DONT_INHIBIT_AUTO_SUSPEND " : "",
-            o->flags & PA_SOURCE_OUTPUT_FAIL_ON_SUSPEND ? "FAIL_ON_SUSPEND " : "",
+            o->flags & PA_SOURCE_OUTPUT_NO_CREATE_ON_SUSPEND ? "NO_CREATE_ON_SUSPEND " : "",
+            o->flags & PA_SOURCE_OUTPUT_KILL_ON_SUSPEND ? "KILL_ON_SUSPEND " : "",
             state_table[pa_source_output_get_state(o)],
             o->source->index, o->source->name,
             (double) pa_source_output_get_latency(o, NULL) / PA_USEC_PER_MSEC,
@@ -564,7 +565,7 @@ char *pa_sink_input_list_to_string(pa_core *c) {
             s,
             "    index: %u\n"
             "\tdriver: <%s>\n"
-            "\tflags: %s%s%s%s%s%s%s%s%s%s\n"
+            "\tflags: %s%s%s%s%s%s%s%s%s%s%s\n"
             "\tstate: %s\n"
             "\tsink: %u <%s>\n"
             "\tvolume: %s\n"
@@ -587,7 +588,8 @@ char *pa_sink_input_list_to_string(pa_core *c) {
             i->flags & PA_SINK_INPUT_FIX_RATE ? "FIX_RATE " : "",
             i->flags & PA_SINK_INPUT_FIX_CHANNELS ? "FIX_CHANNELS " : "",
             i->flags & PA_SINK_INPUT_DONT_INHIBIT_AUTO_SUSPEND ? "DONT_INHIBIT_AUTO_SUSPEND " : "",
-            i->flags & PA_SINK_INPUT_FAIL_ON_SUSPEND ? "FAIL_ON_SUSPEND " : "",
+            i->flags & PA_SINK_INPUT_NO_CREATE_ON_SUSPEND ? "NO_CREATE_SUSPEND " : "",
+            i->flags & PA_SINK_INPUT_KILL_ON_SUSPEND ? "KILL_ON_SUSPEND " : "",
             state_table[pa_sink_input_get_state(i)],
             i->sink->index, i->sink->name,
             pa_cvolume_snprint(cv, sizeof(cv), &v),
index 2dc9a2239cd4b4568be60dc46e610483a3d9683b..b4ab23cc4279a9956b8a40fe2ea652cfb86febe3 100644 (file)
@@ -278,6 +278,30 @@ int pa_config_parse_bool(const char *filename, unsigned line, const char *sectio
     return 0;
 }
 
+int pa_config_parse_not_bool(
+        const char *filename, unsigned line,
+        const char *section,
+        const char *lvalue, const char *rvalue,
+        void *data, void *userdata) {
+
+    int k;
+    pa_bool_t *b = data;
+
+    pa_assert(filename);
+    pa_assert(lvalue);
+    pa_assert(rvalue);
+    pa_assert(data);
+
+    if ((k = pa_parse_boolean(rvalue)) < 0) {
+        pa_log("[%s:%u] Failed to parse boolean value: %s", filename, line, rvalue);
+        return -1;
+    }
+
+    *b = !k;
+
+    return 0;
+}
+
 int pa_config_parse_string(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata) {
     char **s = data;
 
index 08e17ca7b90df191cd4a2973a430d4eba7ab1613..c6c8a148fd99aa72cdfb681a9e9dd6d8ac7bbe1b 100644 (file)
@@ -47,6 +47,7 @@ int pa_config_parse_int(const char *filename, unsigned line, const char *section
 int pa_config_parse_unsigned(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata);
 int pa_config_parse_size(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata);
 int pa_config_parse_bool(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata);
+int pa_config_parse_not_bool(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata);
 int pa_config_parse_string(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata);
 
 #endif
index d4baf6977be97cfd11f5c6e0959bdacc5bd7be38..6494244e229bab5322c91aa44bfaffab399225ba 100644 (file)
 #include "rtkit.h"
 #endif
 
+#ifdef __linux__
+#include <sys/personality.h>
+#endif
+
 #include <pulse/xmalloc.h>
 #include <pulse/util.h>
 #include <pulse/utf8.h>
@@ -2855,3 +2859,12 @@ size_t pa_pipe_buf(int fd) {
     return 4096;
 #endif
 }
+
+void pa_reset_personality(void) {
+
+#ifdef __linux__
+    if (personality(PER_LINUX) < 0)
+        pa_log_warn("Uh, personality() failed: %s", pa_cstrerror(errno));
+#endif
+
+}
index 6de4b7710c16511ec48106417b25db3bb4fc19d4..3d3aec714f0509477aa7d4819b3b6144dbf2153e 100644 (file)
@@ -241,4 +241,6 @@ char* pa_maybe_prefix_path(const char *path, const char *prefix);
 /* Returns size of the specified pipe or 4096 on failure */
 size_t pa_pipe_buf(int fd);
 
+void pa_reset_personality(void);
+
 #endif
index e7abd61b9a357fc48df53b883e2efca7e9167d3a..f6ec7122f86694728d3f80fc993c82686fe64dca 100644 (file)
@@ -83,7 +83,6 @@ typedef enum pa_core_hook {
     PA_CORE_HOOK_SINK_INPUT_MOVE_FAIL,
     PA_CORE_HOOK_SINK_INPUT_STATE_CHANGED,
     PA_CORE_HOOK_SINK_INPUT_PROPLIST_CHANGED,
-    PA_CORE_HOOK_SINK_INPUT_SET_VOLUME,
     PA_CORE_HOOK_SINK_INPUT_SEND_EVENT,
     PA_CORE_HOOK_SOURCE_OUTPUT_NEW,
     PA_CORE_HOOK_SOURCE_OUTPUT_FIXATE,
diff --git a/src/pulsecore/database-simple.c b/src/pulsecore/database-simple.c
new file mode 100644 (file)
index 0000000..1f4caf7
--- /dev/null
@@ -0,0 +1,510 @@
+/***
+  This file is part of PulseAudio.
+
+  Copyright 2009 Nokia Corporation
+  Contact: Maemo Multimedia <multimedia@maemo.org>
+
+  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
+  Lesser 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 <sys/stat.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdio.h>
+
+#include <pulse/xmalloc.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/log.h>
+#include <pulsecore/core-error.h>
+#include <pulsecore/hashmap.h>
+
+#include "database.h"
+
+
+typedef struct simple_data {
+    char *filename;
+    char *tmp_filename;
+    pa_hashmap *map;
+    pa_bool_t read_only;
+} simple_data;
+
+typedef struct entry {
+    pa_datum key;
+    pa_datum data;
+} entry;
+
+void pa_datum_free(pa_datum *d) {
+    pa_assert(d);
+
+    pa_xfree(d->data);
+    d->data = NULL;
+    d->size = 0;
+}
+
+static int compare_func(const void *a, const void *b) {
+    const pa_datum *aa, *bb;
+
+    aa = (const pa_datum*)a;
+    bb = (const pa_datum*)b;
+
+    if (aa->size != bb->size)
+        return aa->size > bb->size ? 1 : -1;
+
+    return memcmp(aa->data, bb->data, aa->size);
+}
+
+/* pa_idxset_string_hash_func modified for our use */
+static unsigned hash_func(const void *p) {
+    const pa_datum *d;
+    unsigned hash = 0;
+    const char *c;
+    unsigned i;
+
+    d = (const pa_datum*)p;
+    c = d->data;
+
+    for (i = 0; i < d->size; i++) {
+        hash = 31 * hash + (unsigned) *c;
+        c++;
+    }
+
+    return hash;
+}
+
+static entry* new_entry(const pa_datum *key, const pa_datum *data) {
+    entry *e;
+
+    pa_assert(key);
+    pa_assert(data);
+
+    e = pa_xnew0(entry, 1);
+    e->key.data = key->size > 0 ? pa_xmemdup(key->data, key->size) : NULL;
+    e->key.size = key->size;
+    e->data.data = data->size > 0 ? pa_xmemdup(data->data, data->size) : NULL;
+    e->data.size = data->size;
+    return e;
+}
+
+static void free_entry(entry *e) {
+    if (e) {
+        if (e->key.data)
+            pa_xfree(e->key.data);
+        if (e->data.data)
+            pa_xfree(e->data.data);
+        pa_xfree(e);
+    }
+}
+
+static int read_uint(FILE *f, uint32_t *res) {
+    size_t items = 0;
+    uint8_t values[4];
+    uint32_t tmp;
+    int i;
+
+    items = fread(&values, sizeof(values), sizeof(uint8_t), f);
+
+    if (feof(f)) /* EOF */
+        return 0;
+
+    if (ferror(f))
+        return -1;
+
+    for (i = 0; i < 4; ++i) {
+        tmp = values[i];
+        *res += (tmp << (i*8));
+    }
+
+    return items;
+}
+
+static int read_data(FILE *f, void **data, ssize_t *length) {
+    size_t items = 0;
+    uint32_t data_len = 0;
+
+    pa_assert(f);
+
+    *data = NULL;
+    *length = 0;
+
+    if ((items = read_uint(f, &data_len)) <= 0)
+        return -1;
+
+    if (data_len > 0) {
+        *data = pa_xmalloc0(data_len);
+        items = fread(*data, data_len, 1, f);
+
+        if (feof(f)) /* EOF */
+            goto reset;
+
+        if (ferror(f))
+            goto reset;
+
+        *length = data_len;
+
+    } else { /* no data? */
+        return -1;
+    }
+
+    return 0;
+
+reset:
+    pa_xfree(*data);
+    *data = NULL;
+    *length = 0;
+    return -1;
+}
+
+static int fill_data(simple_data *db, FILE *f) {
+    pa_datum key;
+    pa_datum data;
+    void *d = NULL;
+    ssize_t l = 0;
+    pa_bool_t append = FALSE;
+    enum { FIELD_KEY = 0, FIELD_DATA } field = FIELD_KEY;
+
+    pa_assert(db);
+    pa_assert(db->map);
+
+    errno = 0;
+
+    key.size = 0;
+    key.data = NULL;
+
+    while (!read_data(f, &d, &l)) {
+
+        switch (field) {
+            case FIELD_KEY:
+                key.data = d;
+                key.size = l;
+                field = FIELD_DATA;
+                break;
+            case FIELD_DATA:
+                data.data = d;
+                data.size = l;
+                append = TRUE;
+                break;
+        }
+
+        if (append) {
+            entry *e = pa_xnew0(entry, 1);
+            e->key.data = key.data;
+            e->key.size = key.size;
+            e->data.data = data.data;
+            e->data.size = data.size;
+            pa_hashmap_put(db->map, &e->key, e);
+            append = FALSE;
+            field = FIELD_KEY;
+        }
+    }
+
+    if (ferror(f)) {
+        pa_log_warn("read error. %s", pa_cstrerror(errno));
+        pa_database_clear((pa_database*)db);
+    }
+
+    if (field == FIELD_DATA && d)
+        pa_xfree(d);
+
+    return pa_hashmap_size(db->map);
+}
+
+pa_database* pa_database_open(const char *fn, pa_bool_t for_write) {
+    FILE *f;
+    char *path;
+    simple_data *db;
+
+    pa_assert(fn);
+
+    path = pa_sprintf_malloc("%s."CANONICAL_HOST".simple", fn);
+    errno = 0;
+
+    f = fopen(path, "r");
+
+    if (f || errno == ENOENT) { /* file not found is ok */
+        db = pa_xnew0(simple_data, 1);
+        db->map = pa_hashmap_new(hash_func, compare_func);
+        db->filename = pa_xstrdup(path);
+        db->tmp_filename = pa_sprintf_malloc(".%s.tmp", db->filename);
+        db->read_only = !for_write;
+
+        if (f) {
+            fill_data(db, f);
+            fclose(f);
+        }
+    } else {
+        if (errno == 0)
+            errno = EIO;
+        db = NULL;
+    }
+
+    pa_xfree(path);
+
+    return (pa_database*) db;
+}
+
+void pa_database_close(pa_database *database) {
+    simple_data *db = (simple_data*)database;
+    pa_assert(db);
+
+    pa_database_sync(database);
+    pa_database_clear(database);
+    pa_xfree(db->filename);
+    pa_xfree(db->tmp_filename);
+    pa_hashmap_free(db->map, NULL, NULL);
+    pa_xfree(db);
+}
+
+pa_datum* pa_database_get(pa_database *database, const pa_datum *key, pa_datum* data) {
+    simple_data *db = (simple_data*)database;
+    entry *e;
+
+    pa_assert(db);
+    pa_assert(key);
+    pa_assert(data);
+
+    e = pa_hashmap_get(db->map, key);
+
+    if (!e)
+        return NULL;
+
+    data->data = e->data.size > 0 ? pa_xmemdup(e->data.data, e->data.size) : NULL;
+    data->size = e->data.size;
+
+    return data;
+}
+
+int pa_database_set(pa_database *database, const pa_datum *key, const pa_datum* data, pa_bool_t overwrite) {
+    simple_data *db = (simple_data*)database;
+    entry *e;
+    int ret = 0;
+
+    pa_assert(db);
+    pa_assert(key);
+    pa_assert(data);
+
+    if (db->read_only)
+        return -1;
+
+    e = new_entry(key, data);
+
+    if (pa_hashmap_put(db->map, &e->key, e) < 0) {
+        /* entry with same key exists in hashmap */
+        entry *r;
+        if (overwrite) {
+            r = pa_hashmap_remove(db->map, key);
+            pa_hashmap_put(db->map, &e->key, e);
+        } else {
+            /* wont't overwrite, so clean new entry */
+            r = e;
+            ret = -1;
+        }
+
+        free_entry(r);
+    }
+
+    return ret;
+}
+
+int pa_database_unset(pa_database *database, const pa_datum *key) {
+    simple_data *db = (simple_data*)database;
+    entry *e;
+
+    pa_assert(db);
+    pa_assert(key);
+
+    e = pa_hashmap_remove(db->map, key);
+    if (!e)
+        return -1;
+
+    free_entry(e);
+
+    return 0;
+}
+
+int pa_database_clear(pa_database *database) {
+    simple_data *db = (simple_data*)database;
+    entry *e;
+
+    pa_assert(db);
+
+    while ((e = pa_hashmap_steal_first(db->map)))
+        free_entry(e);
+
+    return 0;
+}
+
+signed pa_database_size(pa_database *database) {
+    simple_data *db = (simple_data*)database;
+    pa_assert(db);
+
+    return (signed) pa_hashmap_size(db->map);
+}
+
+pa_datum* pa_database_first(pa_database *database, pa_datum *key, pa_datum *data) {
+    simple_data *db = (simple_data*)database;
+    entry *e;
+
+    pa_assert(db);
+    pa_assert(key);
+
+    e = pa_hashmap_first(db->map);
+
+    if (!e)
+        return NULL;
+
+    key->data = e->key.size > 0 ? pa_xmemdup(e->key.data, e->key.size) : NULL;
+    key->size = e->key.size;
+
+    if (data) {
+        data->data = e->data.size > 0 ? pa_xmemdup(e->data.data, e->data.size) : NULL;
+        data->size = e->data.size;
+    }
+
+    return key;
+}
+
+pa_datum* pa_database_next(pa_database *database, const pa_datum *key, pa_datum *next, pa_datum *data) {
+    simple_data *db = (simple_data*)database;
+    entry *e;
+    entry *search;
+    void *state;
+    pa_bool_t pick_now;
+
+    pa_assert(db);
+    pa_assert(next);
+
+    if (!key)
+        return pa_database_first(database, next, data);
+
+    search = pa_hashmap_get(db->map, key);
+
+    state = NULL;
+    pick_now = FALSE;
+
+    while ((e = pa_hashmap_iterate(db->map, &state, NULL))) {
+        if (pick_now)
+            break;
+
+        if (search == e)
+            pick_now = TRUE;
+    }
+
+    if (!pick_now || !e)
+        return NULL;
+
+    next->data = e->key.size > 0 ? pa_xmemdup(e->key.data, e->key.size) : NULL;
+    next->size = e->key.size;
+
+    if (data) {
+        data->data = e->data.size > 0 ? pa_xmemdup(e->data.data, e->data.size) : NULL;
+        data->size = e->data.size;
+    }
+
+    return next;
+}
+
+static int write_uint(FILE *f, const uint32_t num) {
+    size_t items;
+    uint8_t values[4];
+    int i;
+    errno = 0;
+
+    for (i = 0; i < 4; i++)
+         values[i] = (num >> (i*8)) & 0xFF;
+
+    items = fwrite(&values, sizeof(values), sizeof(uint8_t), f);
+
+    if (ferror(f))
+        return -1;
+
+    return items;
+}
+
+static int write_data(FILE *f, void *data, const size_t length) {
+    size_t items;
+    uint32_t len;
+
+    len = length;
+    if ((items = write_uint(f, len)) <= 0)
+        return -1;
+
+    items = fwrite(data, length, 1, f);
+
+    if (ferror(f) || items != 1)
+        return -1;
+
+    return 0;
+}
+
+static int write_entry(FILE *f, const entry *e) {
+    pa_assert(f);
+    pa_assert(e);
+
+    if (write_data(f, e->key.data, e->key.size) < 0)
+        return -1;
+    if (write_data(f, e->data.data, e->data.size) < 0)
+        return -1;
+
+    return 0;
+}
+
+int pa_database_sync(pa_database *database) {
+    simple_data *db = (simple_data*)database;
+    FILE *f;
+    void *state;
+    entry *e;
+
+    pa_assert(db);
+
+    if (db->read_only)
+        return 0;
+
+    errno = 0;
+
+    f = fopen(db->tmp_filename, "w");
+
+    if (!f)
+        goto fail;
+
+    state = NULL;
+    while((e = pa_hashmap_iterate(db->map, &state, NULL))) {
+        if (write_entry(f, e) < 0) {
+            pa_log_warn("error while writing to file. %s", pa_cstrerror(errno));
+            goto fail;
+        }
+    }
+
+    fclose(f);
+    f = NULL;
+
+    if (rename(db->tmp_filename, db->filename) < 0) {
+        pa_log_warn("error while renaming file. %s", pa_cstrerror(errno));
+        goto fail;
+    }
+
+    return 0;
+
+fail:
+    if (f)
+        fclose(f);
+    return -1;
+}
index a00116d1ffda312bc773b859b1367c9f4ab68d7f..d9b9917d40f9916efa674dd21a688da30792caa3 100644 (file)
@@ -97,7 +97,7 @@ pa_hook_result_t pa_hook_fire(pa_hook *hook, void *data) {
 
     hook->n_firing ++;
 
-    for (slot = hook->slots; slot; slot = slot->next) {
+    PA_LLIST_FOREACH(slot, hook->slots) {
         if (slot->dead)
             continue;
 
index cf662510897b0c375336e89aeebf117e1d44852a..ce88c1b9d33871a78d1200bfb306d33c059aed31 100644 (file)
 #endif
 
 /* Rounds down */
-static inline void* pa_align_ptr(const void *p) {
-    return (void*) (((size_t) p) & ~(sizeof(void*)-1));
+static inline void* PA_ALIGN_PTR(const void *p) {
+    return (void*) (((size_t) p) & ~(sizeof(void*) - 1));
 }
-#define PA_ALIGN_PTR(x) (pa_align_ptr(x))
 
 /* Rounds up */
-static inline size_t pa_align(size_t l) {
-    return (((l + sizeof(void*) - 1) / sizeof(void*)) * sizeof(void*));
+static inline size_t PA_ALIGN(size_t l) {
+    return ((l + sizeof(void*) - 1) & ~(sizeof(void*) - 1));
 }
-#define PA_ALIGN(x) (pa_align(x))
 
 /* Rounds down */
-static inline void* pa_page_align_ptr(const void *p) {
-    return (void*) (((size_t) p) & ~(PA_PAGE_SIZE-1));
+static inline void* PA_PAGE_ALIGN_PTR(const void *p) {
+    return (void*) (((size_t) p) & ~(PA_PAGE_SIZE - 1));
 }
-#define PA_PAGE_ALIGN_PTR(x) (pa_page_align_ptr(x))
 
 /* Rounds up */
-static inline size_t pa_page_align(size_t l) {
-    return ((l + PA_PAGE_SIZE - 1) / PA_PAGE_SIZE) * PA_PAGE_SIZE;
+static inline size_t PA_PAGE_ALIGN(size_t l) {
+    return (l + PA_PAGE_SIZE - 1) & ~(PA_PAGE_SIZE - 1);
 }
-#define PA_PAGE_ALIGN(x) (pa_page_align(x))
 
 #define PA_ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
 
-/* The users of PA_MIN and PA_MAX should be aware that these macros on
- * non-GCC executed code with side effects twice. It is thus
- * considered misuse to use code with side effects as arguments to MIN
- * and MAX. */
+/* The users of PA_MIN and PA_MAX, PA_CLAMP, PA_ROUND_UP should be
+ * aware that these macros on non-GCC executed code with side effects
+ * twice. It is thus considered misuse to use code with side effects
+ * as arguments to MIN and MAX. */
 
 #ifdef __GNUC__
 #define PA_MAX(a,b)                             \
-    __extension__ ({ typeof(a) _a = (a);        \
+    __extension__ ({                            \
+            typeof(a) _a = (a);                 \
             typeof(b) _b = (b);                 \
             _a > _b ? _a : _b;                  \
         })
@@ -101,7 +98,8 @@ static inline size_t pa_page_align(size_t l) {
 
 #ifdef __GNUC__
 #define PA_MIN(a,b)                             \
-    __extension__ ({ typeof(a) _a = (a);        \
+    __extension__ ({                            \
+            typeof(a) _a = (a);                 \
             typeof(b) _b = (b);                 \
             _a < _b ? _a : _b;                  \
         })
@@ -111,7 +109,8 @@ static inline size_t pa_page_align(size_t l) {
 
 #ifdef __GNUC__
 #define PA_CLAMP(x, low, high)                                          \
-    __extension__ ({ typeof(x) _x = (x);                                \
+    __extension__ ({                                                    \
+            typeof(x) _x = (x);                                         \
             typeof(low) _low = (low);                                   \
             typeof(high) _high = (high);                                \
             ((_x > _high) ? _high : ((_x < _low) ? _low : _x));         \
@@ -122,7 +121,8 @@ static inline size_t pa_page_align(size_t l) {
 
 #ifdef __GNUC__
 #define PA_CLAMP_UNLIKELY(x, low, high)                                 \
-    __extension__ ({ typeof(x) _x = (x);                                \
+    __extension__ ({                                                    \
+            typeof(x) _x = (x);                                         \
             typeof(low) _low = (low);                                   \
             typeof(high) _high = (high);                                \
             (PA_UNLIKELY(_x > _high) ? _high : (PA_UNLIKELY(_x < _low) ? _low : _x)); \
@@ -135,6 +135,28 @@ static inline size_t pa_page_align(size_t l) {
  * make sense: we cannot know if it is more likely that the values is
  * lower or greater than the boundaries.*/
 
+#ifdef __GNUC__
+#define PA_ROUND_UP(a, b)                       \
+    __extension__ ({                            \
+            typeof(a) _a = (a);                 \
+            typeof(b) _b = (b);                 \
+            ((_a + _b - 1) / _b) * _b;          \
+        })
+#else
+#define PA_ROUND_UP(a, b) ((((a) + (b) - 1) / (b)) * (b))
+#endif
+
+#ifdef __GNUC__
+#define PA_ROUND_DOWN(a, b)                     \
+    __extension__ ({                            \
+            typeof(a) _a = (a);                 \
+            typeof(b) _b = (b);                 \
+            (_a / _b) * _b;                     \
+        })
+#else
+#define PA_ROUND_DOWN(a, b) (((a) / (b)) * (b))
+#endif
+
 /* This type is not intended to be used in exported APIs! Use classic "int" there! */
 #ifdef HAVE_STD_BOOL
 typedef _Bool pa_bool_t;
index 3bc10de538c55740e5ba3766011e58c6433c6ac2..441b397b0faafa3d653a8d7d53c0f2927efd0e71 100644 (file)
@@ -258,7 +258,8 @@ static struct mempool_slot* mempool_allocate_slot(pa_mempool *p) {
             slot = (struct mempool_slot*) ((uint8_t*) p->memory.ptr + (p->block_size * (size_t) idx));
 
         if (!slot) {
-            pa_log_debug("Pool full");
+            if (pa_log_ratelimit())
+                pa_log_debug("Pool full");
             pa_atomic_inc(&p->stat.n_pool_full);
             return NULL;
         }
index 77f9efc9eb41f21b9da8ac4eb933776646822b34..32758be323968f9ccc7b017bf34c54eaeb2858df 100644 (file)
@@ -692,6 +692,12 @@ size_t pa_memblockq_get_minreq(pa_memblockq *bq) {
     return bq->minreq;
 }
 
+size_t pa_memblockq_get_maxrewind(pa_memblockq *bq) {
+    pa_assert(bq);
+
+    return bq->maxrewind;
+}
+
 int64_t pa_memblockq_get_read_index(pa_memblockq *bq) {
     pa_assert(bq);
 
index 146d261b7f9386e029bd70c1e323a457868e509d..587c364b1cd021ef8d26e6e2aca36eeb887b2d23 100644 (file)
@@ -141,6 +141,9 @@ size_t pa_memblockq_get_prebuf(pa_memblockq *bq);
 /* Returns the minimal request value */
 size_t pa_memblockq_get_minreq(pa_memblockq *bq);
 
+/* Returns the maximal rewind value */
+size_t pa_memblockq_get_maxrewind(pa_memblockq *bq);
+
 /* Return the base unit in bytes */
 size_t pa_memblockq_get_base(pa_memblockq *bq);
 
index 9df2f583a18936ed010e0ff696281d7760616362..e26923d41617b4eaacb189f89ed89bfba33012fc 100644 (file)
@@ -223,6 +223,9 @@ void* pa_namereg_get(pa_core *c, const char *name, pa_namereg_type_t type) {
 pa_sink* pa_namereg_set_default_sink(pa_core*c, pa_sink *s) {
     pa_assert(c);
 
+    if (s && !PA_SINK_IS_LINKED(pa_sink_get_state(s)))
+        return NULL;
+
     if (c->default_sink != s) {
         c->default_sink = s;
         pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
@@ -234,6 +237,9 @@ pa_sink* pa_namereg_set_default_sink(pa_core*c, pa_sink *s) {
 pa_source* pa_namereg_set_default_source(pa_core*c, pa_source *s) {
     pa_assert(c);
 
+    if (s && !PA_SOURCE_IS_LINKED(pa_source_get_state(s)))
+        return NULL;
+
     if (c->default_source != s) {
         c->default_source = s;
         pa_subscription_post(c, PA_SUBSCRIPTION_EVENT_SERVER|PA_SUBSCRIPTION_EVENT_CHANGE, PA_INVALID_INDEX);
@@ -244,14 +250,19 @@ pa_source* pa_namereg_set_default_source(pa_core*c, pa_source *s) {
 
 pa_sink *pa_namereg_get_default_sink(pa_core *c) {
     pa_sink *s;
+    uint32_t idx;
 
     pa_assert(c);
 
-    if (c->default_sink)
+    if (c->default_sink && PA_SINK_IS_LINKED(pa_sink_get_state(c->default_sink)))
         return c->default_sink;
 
-    if ((s = pa_idxset_first(c->sinks, NULL)))
-        return pa_namereg_set_default_sink(c, s);
+    /* FIXME: the selection here should be based priority values on
+     * the sinks */
+
+    PA_IDXSET_FOREACH(s, c->sinks, idx)
+        if (PA_SINK_IS_LINKED(pa_sink_get_state(s)))
+            return pa_namereg_set_default_sink(c, s);
 
     return NULL;
 }
@@ -262,15 +273,18 @@ pa_source *pa_namereg_get_default_source(pa_core *c) {
 
     pa_assert(c);
 
-    if (c->default_source)
+    if (c->default_source && PA_SOURCE_IS_LINKED(pa_source_get_state(c->default_source)))
         return c->default_source;
 
-    for (s = PA_SOURCE(pa_idxset_first(c->sources, &idx)); s; s = PA_SOURCE(pa_idxset_next(c->sources, &idx)))
-        if (!s->monitor_of)
+    /* First, try to find one that isn't a monitor */
+    PA_IDXSET_FOREACH(s, c->sources, idx)
+        if (!s->monitor_of && PA_SOURCE_IS_LINKED(pa_source_get_state(s)))
             return pa_namereg_set_default_source(c, s);
 
-    if ((s = pa_idxset_first(c->sources, NULL)))
-        return pa_namereg_set_default_source(c, s);
+    /* Then, fallback to a monitor */
+    PA_IDXSET_FOREACH(s, c->sources, idx)
+        if (PA_SOURCE_IS_LINKED(pa_source_get_state(s)))
+            return pa_namereg_set_default_source(c, s);
 
     return NULL;
 }
index 9a37c565ed1bd64dba1f837397ce0388e694b351..b1285e1551f6458835f2611b978d88279bf20511 100644 (file)
@@ -762,6 +762,7 @@ static int playback_stream_process_msg(pa_msgobject *o, int code, void*userdata,
         return -1;
 
     switch (code) {
+
         case PLAYBACK_STREAM_MESSAGE_REQUEST_DATA: {
             pa_tagstruct *t;
             int l = 0;
@@ -1130,6 +1131,12 @@ static void playback_stream_request_bytes(playback_stream *s) {
 
     m = pa_memblockq_pop_missing(s->memblockq);
 
+    /* pa_log("request_bytes(%lu) (tlength=%lu minreq=%lu length=%lu)", */
+    /*        (unsigned long) m, */
+    /*        pa_memblockq_get_tlength(s->memblockq), */
+    /*        pa_memblockq_get_minreq(s->memblockq), */
+    /*        pa_memblockq_get_length(s->memblockq)); */
+
     if (m <= 0)
         return;
 
@@ -1143,7 +1150,6 @@ static void playback_stream_request_bytes(playback_stream *s) {
         pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(s), PLAYBACK_STREAM_MESSAGE_REQUEST_DATA, NULL, 0, NULL, NULL);
 }
 
-
 /* Called from main context */
 static void playback_stream_send_killed(playback_stream *p) {
     pa_tagstruct *t;
@@ -1345,7 +1351,9 @@ static int sink_input_process_msg(pa_msgobject *o, int code, void *userdata, int
 /*             pa_log("sink input post: %lu %lli", (unsigned long) chunk->length, (long long) windex); */
 
             if (pa_memblockq_push_align(s->memblockq, chunk) < 0) {
-                pa_log_warn("Failed to push data into queue");
+
+                if (pa_log_ratelimit())
+                    pa_log_warn("Failed to push data into queue");
                 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(s), PLAYBACK_STREAM_MESSAGE_OVERFLOW, NULL, 0, NULL, NULL);
                 pa_memblockq_seek(s->memblockq, (int64_t) chunk->length, PA_SEEK_RELATIVE, TRUE);
             }
@@ -1617,6 +1625,9 @@ static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) {
     s = PLAYBACK_STREAM(i->userdata);
     playback_stream_assert_ref(s);
 
+    if (!dest)
+        return;
+
     fix_playback_buffer_attr(s);
     pa_memblockq_apply_attr(s->memblockq, &s->buffer_attr);
     pa_memblockq_get_attr(s->memblockq, &s->buffer_attr);
@@ -1752,6 +1763,9 @@ static void source_output_moving_cb(pa_source_output *o, pa_source *dest) {
     s = RECORD_STREAM(o->userdata);
     record_stream_assert_ref(s);
 
+    if (!dest)
+        return;
+
     fix_record_buffer_attr_pre(s);
     pa_memblockq_set_maxlength(s->memblockq, s->buffer_attr.maxlength);
     pa_memblockq_get_attr(s->memblockq, &s->buffer_attr);
@@ -1951,7 +1965,7 @@ static void command_create_playback_stream(pa_pdispatch *pd, uint32_t command, u
         (no_move ?  PA_SINK_INPUT_DONT_MOVE : 0) |
         (variable_rate ?  PA_SINK_INPUT_VARIABLE_RATE : 0) |
         (dont_inhibit_auto_suspend ? PA_SINK_INPUT_DONT_INHIBIT_AUTO_SUSPEND : 0) |
-        (fail_on_suspend ? PA_SINK_INPUT_FAIL_ON_SUSPEND : 0);
+        (fail_on_suspend ? PA_SINK_INPUT_NO_CREATE_ON_SUSPEND|PA_SINK_INPUT_KILL_ON_SUSPEND : 0);
 
     /* Only since protocol version 15 there's a seperate muted_set
      * flag. For older versions we synthesize it here */
@@ -2207,7 +2221,7 @@ static void command_create_record_stream(pa_pdispatch *pd, uint32_t command, uin
         (no_move ?  PA_SOURCE_OUTPUT_DONT_MOVE : 0) |
         (variable_rate ?  PA_SOURCE_OUTPUT_VARIABLE_RATE : 0) |
         (dont_inhibit_auto_suspend ? PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND : 0) |
-        (fail_on_suspend ? PA_SOURCE_OUTPUT_FAIL_ON_SUSPEND : 0);
+        (fail_on_suspend ? PA_SOURCE_OUTPUT_NO_CREATE_ON_SUSPEND|PA_SOURCE_OUTPUT_KILL_ON_SUSPEND : 0);
 
     s = record_stream_new(c, source, &ss, &map, peak_detect, &attr, flags, p, adjust_latency, direct_on_input, early_requests, &ret);
     pa_proplist_free(p);
@@ -2826,7 +2840,7 @@ static void sink_fill_tagstruct(pa_native_connection *c, pa_tagstruct *t, pa_sin
         PA_TAG_SAMPLE_SPEC, &fixed_ss,
         PA_TAG_CHANNEL_MAP, &sink->channel_map,
         PA_TAG_U32, sink->module ? sink->module->index : PA_INVALID_INDEX,
-        PA_TAG_CVOLUME, pa_sink_get_volume(sink, FALSE, FALSE),
+        PA_TAG_CVOLUME, pa_sink_get_volume(sink, FALSE),
         PA_TAG_BOOLEAN, pa_sink_get_mute(sink, FALSE),
         PA_TAG_U32, sink->monitor_source ? sink->monitor_source->index : PA_INVALID_INDEX,
         PA_TAG_STRING, sink->monitor_source ? sink->monitor_source->name : NULL,
@@ -3323,6 +3337,7 @@ static void command_set_volume(
     pa_source *source = NULL;
     pa_sink_input *si = NULL;
     const char *name = NULL;
+    const char *client_name;
 
     pa_native_connection_assert_ref(c);
     pa_assert(t);
@@ -3369,12 +3384,20 @@ static void command_set_volume(
 
     CHECK_VALIDITY(c->pstream, si || sink || source, tag, PA_ERR_NOENTITY);
 
-    if (sink)
-        pa_sink_set_volume(sink, &volume, TRUE, TRUE, TRUE, TRUE);
-    else if (source)
+    client_name = pa_strnull(pa_proplist_gets(c->client->proplist, PA_PROP_APPLICATION_PROCESS_BINARY));
+
+    if (sink) {
+        pa_log_debug("Client %s changes volume of sink %s.", client_name, sink->name);
+        pa_sink_set_volume(sink, &volume, TRUE, TRUE);
+    } else if (source) {
+        pa_log_debug("Client %s changes volume of sink %s.", client_name, source->name);
         pa_source_set_volume(source, &volume, TRUE);
-    else if (si)
+    } else if (si) {
+        pa_log_debug("Client %s changes volume of sink %s.",
+                     client_name,
+                     pa_strnull(pa_proplist_gets(si->proplist, PA_PROP_MEDIA_NAME)));
         pa_sink_input_set_volume(si, &volume, TRUE, TRUE);
+    }
 
     pa_pstream_send_simple_ack(c->pstream, tag);
 }
index 17fb8480a2c0fe08f6d44860394116ec28b66af9..59e0a0c19eb5ea621b936cc9d4e16a225f45ff0e 100644 (file)
@@ -347,13 +347,17 @@ void pa_resampler_set_output_rate(pa_resampler *r, uint32_t rate) {
 size_t pa_resampler_request(pa_resampler *r, size_t out_length) {
     pa_assert(r);
 
-    return (((out_length / r->o_fz)*r->i_ss.rate)/r->o_ss.rate) * r->i_fz;
+    /* Let's round up here */
+
+    return (((((out_length + r->o_fz-1) / r->o_fz) * r->i_ss.rate) + r->o_ss.rate-1) / r->o_ss.rate) * r->i_fz;
 }
 
 size_t pa_resampler_result(pa_resampler *r, size_t in_length) {
     pa_assert(r);
 
-    return (((in_length / r->i_fz)*r->o_ss.rate)/r->i_ss.rate) * r->o_fz;
+    /* Let's round up here */
+
+    return (((((in_length + r->i_fz-1) / r->i_fz) * r->o_ss.rate) + r->i_ss.rate-1) / r->i_ss.rate) * r->o_fz;
 }
 
 size_t pa_resampler_max_block_size(pa_resampler *r) {
index 6e4284269aa1ee5d253e105ef7716ed54d09710f..fbf777a4687e87d7f2dbd1847bee7b3942b4c048 100644 (file)
@@ -60,7 +60,8 @@
 #define MADV_REMOVE 9
 #endif
 
-#define MAX_SHM_SIZE (PA_ALIGN(1024*1024*64))
+/* 1 GiB at max */
+#define MAX_SHM_SIZE (PA_ALIGN(1024*1024*1024))
 
 #ifdef __linux__
 /* On Linux we know that the shared memory blocks are files in
index a5f96351242f8844ef6b0034532567c2f7999d52..a29334f953bbdb788f901236fd43bc77c0bbab78 100644 (file)
 static PA_DEFINE_CHECK_TYPE(pa_sink_input, pa_msgobject);
 
 static void sink_input_free(pa_object *o);
+static void set_real_ratio(pa_sink_input *i, const pa_cvolume *v);
 
 pa_sink_input_new_data* pa_sink_input_new_data_init(pa_sink_input_new_data *data) {
     pa_assert(data);
 
-    memset(data, 0, sizeof(*data));
+    pa_zero(*data);
     data->resample_method = PA_RESAMPLER_INVALID;
     data->proplist = pa_proplist_new();
 
@@ -114,6 +115,7 @@ static void reset_callbacks(pa_sink_input *i) {
     i->update_max_request = NULL;
     i->update_sink_requested_latency = NULL;
     i->update_sink_latency_range = NULL;
+    i->update_sink_fixed_latency = NULL;
     i->attach = NULL;
     i->detach = NULL;
     i->suspend = NULL;
@@ -142,6 +144,7 @@ int pa_sink_input_new(
     pa_assert(_i);
     pa_assert(core);
     pa_assert(data);
+    pa_assert_ctl_context();
 
     if (data->client)
         pa_proplist_update(data->proplist, PA_UPDATE_MERGE, data->client->proplist);
@@ -220,7 +223,7 @@ int pa_sink_input_new(
     if ((r = pa_hook_fire(&core->hooks[PA_CORE_HOOK_SINK_INPUT_FIXATE], data)) < 0)
         return r;
 
-    if ((flags & PA_SINK_INPUT_FAIL_ON_SUSPEND) &&
+    if ((flags & PA_SINK_INPUT_NO_CREATE_ON_SUSPEND) &&
         pa_sink_get_state(data->sink) == PA_SINK_SUSPENDED) {
         pa_log_warn("Failed to create sink input: sink is suspended.");
         return -PA_ERR_BADSTATE;
@@ -268,18 +271,20 @@ int pa_sink_input_new(
     i->channel_map = data->channel_map;
 
     if ((i->sink->flags & PA_SINK_FLAT_VOLUME) && !data->volume_is_absolute) {
+        pa_cvolume remapped;
+
         /* When the 'absolute' bool is not set then we'll treat the volume
          * as relative to the sink volume even in flat volume mode */
-
-        pa_cvolume v = data->sink->reference_volume;
-        pa_cvolume_remap(&v, &data->sink->channel_map, &data->channel_map);
-        pa_sw_cvolume_multiply(&i->virtual_volume, &data->volume, &v);
+        remapped = data->sink->reference_volume;
+        pa_cvolume_remap(&remapped, &data->sink->channel_map, &data->channel_map);
+        pa_sw_cvolume_multiply(&i->volume, &data->volume, &remapped);
     } else
-        i->virtual_volume = data->volume;
+        i->volume = data->volume;
 
     i->volume_factor = data->volume_factor;
-    pa_cvolume_init(&i->soft_volume);
-    memset(i->relative_volume, 0, sizeof(i->relative_volume));
+    i->real_ratio = i->reference_ratio = data->volume;
+    pa_cvolume_reset(&i->soft_volume, i->sample_spec.channels);
+    pa_cvolume_reset(&i->real_ratio, i->sample_spec.channels);
     i->save_volume = data->save_volume;
     i->save_sink = data->save_sink;
     i->save_muted = data->save_muted;
@@ -348,6 +353,7 @@ int pa_sink_input_new(
 /* Called from main context */
 static void update_n_corked(pa_sink_input *i, pa_sink_input_state_t state) {
     pa_assert(i);
+    pa_assert_ctl_context();
 
     if (!i->sink)
         return;
@@ -362,6 +368,7 @@ static void update_n_corked(pa_sink_input *i, pa_sink_input_state_t state) {
 static void sink_input_set_state(pa_sink_input *i, pa_sink_input_state_t state) {
     pa_sink_input *ssync;
     pa_assert(i);
+    pa_assert_ctl_context();
 
     if (state == PA_SINK_INPUT_DRAINED)
         state = PA_SINK_INPUT_RUNNING;
@@ -400,7 +407,9 @@ static void sink_input_set_state(pa_sink_input *i, pa_sink_input_state_t state)
 void pa_sink_input_unlink(pa_sink_input *i) {
     pa_bool_t linked;
     pa_source_output *o, *p =  NULL;
+
     pa_assert(i);
+    pa_assert_ctl_context();
 
     /* See pa_sink_unlink() for a couple of comments how this function
      * works */
@@ -439,11 +448,8 @@ void pa_sink_input_unlink(pa_sink_input *i) {
 
     if (linked && i->sink) {
         /* We might need to update the sink's volume if we are in flat volume mode. */
-        if (i->sink->flags & PA_SINK_FLAT_VOLUME) {
-            pa_cvolume new_volume;
-            pa_sink_update_flat_volume(i->sink, &new_volume);
-            pa_sink_set_volume(i->sink, &new_volume, FALSE, FALSE, FALSE, FALSE);
-        }
+        if (i->sink->flags & PA_SINK_FLAT_VOLUME)
+            pa_sink_set_volume(i->sink, NULL, FALSE, FALSE);
 
         if (i->sink->asyncmsgq)
             pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_REMOVE_INPUT, i, 0, NULL) == 0);
@@ -471,6 +477,7 @@ static void sink_input_free(pa_object *o) {
     pa_sink_input* i = PA_SINK_INPUT(o);
 
     pa_assert(i);
+    pa_assert_ctl_context();
     pa_assert(pa_sink_input_refcnt(i) == 0);
 
     if (PA_SINK_INPUT_IS_LINKED(i->state))
@@ -502,7 +509,9 @@ static void sink_input_free(pa_object *o) {
 /* Called from main context */
 void pa_sink_input_put(pa_sink_input *i) {
     pa_sink_input_state_t state;
+
     pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
 
     pa_assert(i->state == PA_SINK_INPUT_INIT);
 
@@ -517,12 +526,10 @@ void pa_sink_input_put(pa_sink_input *i) {
     i->state = state;
 
     /* We might need to update the sink's volume if we are in flat volume mode. */
-    if (i->sink->flags & PA_SINK_FLAT_VOLUME) {
-        pa_cvolume new_volume;
-        pa_sink_update_flat_volume(i->sink, &new_volume);
-        pa_sink_set_volume(i->sink, &new_volume, FALSE, FALSE, FALSE, FALSE);
-    } else
-        pa_sink_input_set_relative_volume(i, &i->virtual_volume);
+    if (i->sink->flags & PA_SINK_FLAT_VOLUME)
+        pa_sink_set_volume(i->sink, NULL, FALSE, i->save_volume);
+    else
+        set_real_ratio(i, &i->volume);
 
     i->thread_info.soft_volume = i->soft_volume;
     i->thread_info.muted = i->muted;
@@ -538,6 +545,7 @@ void pa_sink_input_put(pa_sink_input *i) {
 /* Called from main context */
 void pa_sink_input_kill(pa_sink_input*i) {
     pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
 
     i->kill(i);
@@ -548,6 +556,7 @@ pa_usec_t pa_sink_input_get_latency(pa_sink_input *i, pa_usec_t *sink_latency) {
     pa_usec_t r[2] = { 0, 0 };
 
     pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
 
     pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_GET_LATENCY, r, 0, NULL) == 0);
@@ -569,6 +578,7 @@ void pa_sink_input_peek(pa_sink_input *i, size_t slength /* in sink frames */, p
     size_t ilength;
 
     pa_sink_input_assert_ref(i);
+    pa_sink_input_assert_io_context(i);
     pa_assert(PA_SINK_INPUT_IS_LINKED(i->thread_info.state));
     pa_assert(pa_frame_aligned(slength, &i->sink->sample_spec));
     pa_assert(chunk);
@@ -706,8 +716,9 @@ void pa_sink_input_peek(pa_sink_input *i, size_t slength /* in sink frames */, p
 
 /* Called from thread context */
 void pa_sink_input_drop(pa_sink_input *i, size_t nbytes /* in sink sample spec */) {
-    pa_sink_input_assert_ref(i);
 
+    pa_sink_input_assert_ref(i);
+    pa_sink_input_assert_io_context(i);
     pa_assert(PA_SINK_INPUT_IS_LINKED(i->thread_info.state));
     pa_assert(pa_frame_aligned(nbytes, &i->sink->sample_spec));
     pa_assert(nbytes > 0);
@@ -721,8 +732,9 @@ void pa_sink_input_drop(pa_sink_input *i, size_t nbytes /* in sink sample spec *
 void pa_sink_input_process_rewind(pa_sink_input *i, size_t nbytes /* in sink sample spec */) {
     size_t lbq;
     pa_bool_t called = FALSE;
-    pa_sink_input_assert_ref(i);
 
+    pa_sink_input_assert_ref(i);
+    pa_sink_input_assert_io_context(i);
     pa_assert(PA_SINK_INPUT_IS_LINKED(i->thread_info.state));
     pa_assert(pa_frame_aligned(nbytes, &i->sink->sample_spec));
 
@@ -789,9 +801,29 @@ void pa_sink_input_process_rewind(pa_sink_input *i, size_t nbytes /* in sink sam
     i->thread_info.dont_rewind_render = FALSE;
 }
 
+/* Called from thread context */
+size_t pa_sink_input_get_max_rewind(pa_sink_input *i) {
+    pa_sink_input_assert_ref(i);
+    pa_sink_input_assert_io_context(i);
+
+    return i->thread_info.resampler ? pa_resampler_request(i->thread_info.resampler, i->sink->thread_info.max_rewind) : i->sink->thread_info.max_rewind;
+}
+
+/* Called from thread context */
+size_t pa_sink_input_get_max_request(pa_sink_input *i) {
+    pa_sink_input_assert_ref(i);
+    pa_sink_input_assert_io_context(i);
+
+    /* We're not verifying the status here, to allow this to be called
+     * in the state change handler between _INIT and _RUNNING */
+
+    return i->thread_info.resampler ? pa_resampler_request(i->thread_info.resampler, i->sink->thread_info.max_request) : i->sink->thread_info.max_request;
+}
+
 /* Called from thread context */
 void pa_sink_input_update_max_rewind(pa_sink_input *i, size_t nbytes  /* in the sink's sample spec */) {
     pa_sink_input_assert_ref(i);
+    pa_sink_input_assert_io_context(i);
     pa_assert(PA_SINK_INPUT_IS_LINKED(i->thread_info.state));
     pa_assert(pa_frame_aligned(nbytes, &i->sink->sample_spec));
 
@@ -804,6 +836,7 @@ void pa_sink_input_update_max_rewind(pa_sink_input *i, size_t nbytes  /* in the
 /* Called from thread context */
 void pa_sink_input_update_max_request(pa_sink_input *i, size_t nbytes  /* in the sink's sample spec */) {
     pa_sink_input_assert_ref(i);
+    pa_sink_input_assert_io_context(i);
     pa_assert(PA_SINK_INPUT_IS_LINKED(i->thread_info.state));
     pa_assert(pa_frame_aligned(nbytes, &i->sink->sample_spec));
 
@@ -814,15 +847,16 @@ void pa_sink_input_update_max_request(pa_sink_input *i, size_t nbytes  /* in the
 /* Called from thread context */
 pa_usec_t pa_sink_input_set_requested_latency_within_thread(pa_sink_input *i, pa_usec_t usec) {
     pa_sink_input_assert_ref(i);
+    pa_sink_input_assert_io_context(i);
 
     if (!(i->sink->flags & PA_SINK_DYNAMIC_LATENCY))
-        usec = i->sink->fixed_latency;
+        usec = i->sink->thread_info.fixed_latency;
 
     if (usec != (pa_usec_t) -1)
         usec = PA_CLAMP(usec, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
 
     i->thread_info.requested_sink_latency = usec;
-    pa_sink_invalidate_requested_latency(i->sink);
+    pa_sink_invalidate_requested_latency(i->sink, TRUE);
 
     return usec;
 }
@@ -830,6 +864,7 @@ pa_usec_t pa_sink_input_set_requested_latency_within_thread(pa_sink_input *i, pa
 /* Called from main context */
 pa_usec_t pa_sink_input_set_requested_latency(pa_sink_input *i, pa_usec_t usec) {
     pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
 
     if (PA_SINK_INPUT_IS_LINKED(i->state) && i->sink) {
         pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
@@ -841,7 +876,7 @@ pa_usec_t pa_sink_input_set_requested_latency(pa_sink_input *i, pa_usec_t usec)
 
     if (i->sink) {
         if (!(i->sink->flags & PA_SINK_DYNAMIC_LATENCY))
-            usec = i->sink->fixed_latency;
+            usec = pa_sink_get_fixed_latency(i->sink);
 
         if (usec != (pa_usec_t) -1) {
             pa_usec_t min_latency, max_latency;
@@ -858,6 +893,7 @@ pa_usec_t pa_sink_input_set_requested_latency(pa_sink_input *i, pa_usec_t usec)
 /* Called from main context */
 pa_usec_t pa_sink_input_get_requested_latency(pa_sink_input *i) {
     pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
 
     if (PA_SINK_INPUT_IS_LINKED(i->state) && i->sink) {
         pa_usec_t usec = 0;
@@ -871,11 +907,33 @@ pa_usec_t pa_sink_input_get_requested_latency(pa_sink_input *i) {
     return i->thread_info.requested_sink_latency;
 }
 
+/* Called from main context */
+static void set_real_ratio(pa_sink_input *i, const pa_cvolume *v) {
+    pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
+    pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
+    pa_assert(!v || pa_cvolume_compatible(v, &i->sample_spec));
+
+    /* This basically calculates:
+     *
+     * i->real_ratio := v
+     * i->soft_volume := i->real_ratio * i->volume_factor */
+
+    if (v)
+        i->real_ratio = *v;
+    else
+        pa_cvolume_reset(&i->real_ratio, i->sample_spec.channels);
+
+    pa_sw_cvolume_multiply(&i->soft_volume, &i->real_ratio, &i->volume_factor);
+    /* We don't copy the data to the thread_info data. That's left for someone else to do */
+}
+
 /* Called from main context */
 void pa_sink_input_set_volume(pa_sink_input *i, const pa_cvolume *volume, pa_bool_t save, pa_bool_t absolute) {
     pa_cvolume v;
 
     pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
     pa_assert(volume);
     pa_assert(pa_cvolume_valid(volume));
@@ -887,29 +945,24 @@ void pa_sink_input_set_volume(pa_sink_input *i, const pa_cvolume *volume, pa_boo
         volume = pa_sw_cvolume_multiply(&v, &v, volume);
     }
 
-    if (pa_cvolume_equal(volume, &i->virtual_volume))
+    if (pa_cvolume_equal(volume, &i->volume)) {
+        i->save_volume = i->save_volume || save;
         return;
+    }
 
-    i->virtual_volume = *volume;
+    i->volume = *volume;
     i->save_volume = save;
 
-    if (i->sink->flags & PA_SINK_FLAT_VOLUME) {
-        pa_cvolume new_volume;
-
+    if (i->sink->flags & PA_SINK_FLAT_VOLUME)
         /* We are in flat volume mode, so let's update all sink input
          * volumes and update the flat volume of the sink */
 
-        pa_sink_update_flat_volume(i->sink, &new_volume);
-        pa_sink_set_volume(i->sink, &new_volume, FALSE, TRUE, FALSE, FALSE);
-
-    } else {
+        pa_sink_set_volume(i->sink, NULL, TRUE, save);
 
+    else {
         /* OK, we are in normal volume mode. The volume only affects
          * ourselves */
-        pa_sink_input_set_relative_volume(i, volume);
-
-        /* Hooks have the ability to play games with i->soft_volume */
-        pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_SET_VOLUME], i);
+        set_real_ratio(i, volume);
 
         /* Copy the new soft_volume to the thread_info struct */
         pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_SOFT_VOLUME, NULL, 0, NULL) == 0);
@@ -922,71 +975,21 @@ void pa_sink_input_set_volume(pa_sink_input *i, const pa_cvolume *volume, pa_boo
 /* Called from main context */
 pa_cvolume *pa_sink_input_get_volume(pa_sink_input *i, pa_cvolume *volume, pa_bool_t absolute) {
     pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
 
-    if ((i->sink->flags & PA_SINK_FLAT_VOLUME) && !absolute) {
-        pa_cvolume v = i->sink->reference_volume;
-        pa_cvolume_remap(&v, &i->sink->channel_map, &i->channel_map);
-        pa_sw_cvolume_divide(volume, &i->virtual_volume, &v);
-    } else
-        *volume = i->virtual_volume;
+    if (absolute || !(i->sink->flags & PA_SINK_FLAT_VOLUME))
+        *volume = i->volume;
+    else
+        *volume = i->reference_ratio;
 
     return volume;
 }
 
-/* Called from main context */
-pa_cvolume *pa_sink_input_get_relative_volume(pa_sink_input *i, pa_cvolume *v) {
-    unsigned c;
-
-    pa_sink_input_assert_ref(i);
-    pa_assert(v);
-    pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
-
-    /* This always returns the relative volume. Converts the float
-     * version into a pa_cvolume */
-
-    v->channels = i->sample_spec.channels;
-
-    for (c = 0; c < v->channels; c++)
-        v->values[c] = pa_sw_volume_from_linear(i->relative_volume[c]);
-
-    return v;
-}
-
-/* Called from main context */
-void pa_sink_input_set_relative_volume(pa_sink_input *i, const pa_cvolume *v) {
-    unsigned c;
-    pa_cvolume _v;
-
-    pa_sink_input_assert_ref(i);
-    pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
-    pa_assert(!v || pa_cvolume_compatible(v, &i->sample_spec));
-
-    if (!v)
-        v = pa_cvolume_reset(&_v, i->sample_spec.channels);
-
-    /* This basically calculates:
-     *
-     * i->relative_volume := v
-     * i->soft_volume := i->relative_volume * i->volume_factor */
-
-    i->soft_volume.channels = i->sample_spec.channels;
-
-    for (c = 0; c < i->sample_spec.channels; c++) {
-        i->relative_volume[c] = pa_sw_volume_to_linear(v->values[c]);
-
-        i->soft_volume.values[c] = pa_sw_volume_from_linear(
-                i->relative_volume[c] *
-                pa_sw_volume_to_linear(i->volume_factor.values[c]));
-    }
-
-    /* We don't copy the data to the thread_info data. That's left for someone else to do */
-}
-
 /* Called from main context */
 void pa_sink_input_set_mute(pa_sink_input *i, pa_bool_t mute, pa_bool_t save) {
-    pa_assert(i);
     pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
 
     if (!i->muted == !mute)
@@ -1002,6 +1005,7 @@ void pa_sink_input_set_mute(pa_sink_input *i, pa_bool_t mute, pa_bool_t save) {
 /* Called from main context */
 pa_bool_t pa_sink_input_get_mute(pa_sink_input *i) {
     pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
 
     return i->muted;
@@ -1010,6 +1014,7 @@ pa_bool_t pa_sink_input_get_mute(pa_sink_input *i) {
 /* Called from main thread */
 void pa_sink_input_update_proplist(pa_sink_input *i, pa_update_mode_t mode, pa_proplist *p) {
     pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
 
     if (p)
         pa_proplist_update(i->proplist, mode, p);
@@ -1023,6 +1028,7 @@ void pa_sink_input_update_proplist(pa_sink_input *i, pa_update_mode_t mode, pa_p
 /* Called from main context */
 void pa_sink_input_cork(pa_sink_input *i, pa_bool_t b) {
     pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
 
     sink_input_set_state(i, b ? PA_SINK_INPUT_CORKED : PA_SINK_INPUT_RUNNING);
@@ -1031,6 +1037,7 @@ void pa_sink_input_cork(pa_sink_input *i, pa_bool_t b) {
 /* Called from main context */
 int pa_sink_input_set_rate(pa_sink_input *i, uint32_t rate) {
     pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
     pa_return_val_if_fail(i->thread_info.resampler, -PA_ERR_BADSTATE);
 
@@ -1049,13 +1056,14 @@ int pa_sink_input_set_rate(pa_sink_input *i, uint32_t rate) {
 void pa_sink_input_set_name(pa_sink_input *i, const char *name) {
     const char *old;
     pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
 
     if (!name && !pa_proplist_contains(i->proplist, PA_PROP_MEDIA_NAME))
         return;
 
     old = pa_proplist_gets(i->proplist, PA_PROP_MEDIA_NAME);
 
-    if (old && name && !strcmp(old, name))
+    if (old && name && pa_streq(old, name))
         return;
 
     if (name)
@@ -1072,6 +1080,7 @@ void pa_sink_input_set_name(pa_sink_input *i, const char *name) {
 /* Called from main context */
 pa_resample_method_t pa_sink_input_get_resample_method(pa_sink_input *i) {
     pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
 
     return i->actual_resample_method;
 }
@@ -1079,6 +1088,7 @@ pa_resample_method_t pa_sink_input_get_resample_method(pa_sink_input *i) {
 /* Called from main context */
 pa_bool_t pa_sink_input_may_move(pa_sink_input *i) {
     pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
 
     if (i->flags & PA_SINK_INPUT_DONT_MOVE)
@@ -1095,6 +1105,7 @@ pa_bool_t pa_sink_input_may_move(pa_sink_input *i) {
 /* Called from main context */
 pa_bool_t pa_sink_input_may_move_to(pa_sink_input *i, pa_sink *dest) {
     pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
     pa_sink_assert_ref(dest);
 
@@ -1123,6 +1134,7 @@ int pa_sink_input_start_move(pa_sink_input *i) {
     int r;
 
     pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
     pa_assert(i->sink);
 
@@ -1147,20 +1159,10 @@ int pa_sink_input_start_move(pa_sink_input *i) {
     if (pa_sink_input_get_state(i) == PA_SINK_INPUT_CORKED)
         pa_assert_se(i->sink->n_corked-- >= 1);
 
-    if (i->sink->flags & PA_SINK_FLAT_VOLUME) {
-        pa_cvolume new_volume;
-
-        /* Make the virtual volume relative */
-        pa_sink_input_get_relative_volume(i, &i->virtual_volume);
-
-        /* And reset the the relative volume */
-        pa_sink_input_set_relative_volume(i, NULL);
-
+    if (i->sink->flags & PA_SINK_FLAT_VOLUME)
         /* We might need to update the sink's volume if we are in flat
          * volume mode. */
-        pa_sink_update_flat_volume(i->sink, &new_volume);
-        pa_sink_set_volume(i->sink, &new_volume, FALSE, FALSE, FALSE, FALSE);
-    }
+        pa_sink_set_volume(i->sink, NULL, FALSE, FALSE);
 
     pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_START_MOVE, i, 0, NULL) == 0);
 
@@ -1177,6 +1179,7 @@ int pa_sink_input_finish_move(pa_sink_input *i, pa_sink *dest, pa_bool_t save) {
     pa_resampler *new_resampler;
 
     pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
     pa_assert(!i->sink);
     pa_sink_assert_ref(dest);
@@ -1243,16 +1246,15 @@ int pa_sink_input_finish_move(pa_sink_input *i, pa_sink *dest, pa_bool_t save) {
     pa_sink_update_status(dest);
 
     if (i->sink->flags & PA_SINK_FLAT_VOLUME) {
-        pa_cvolume new_volume;
+        pa_cvolume remapped;
 
-        /* Make relative volume absolute again */
-        pa_cvolume t = dest->reference_volume;
-        pa_cvolume_remap(&t, &dest->channel_map, &i->channel_map);
-        pa_sw_cvolume_multiply(&i->virtual_volume, &i->virtual_volume, &t);
+        /* Make relative volumes absolute */
+        remapped = dest->reference_volume;
+        pa_cvolume_remap(&remapped, &dest->channel_map, &i->channel_map);
+        pa_sw_cvolume_multiply(&i->volume, &i->reference_ratio, &remapped);
 
         /* We might need to update the sink's volume if we are in flat volume mode. */
-        pa_sink_update_flat_volume(i->sink, &new_volume);
-        pa_sink_set_volume(i->sink, &new_volume, FALSE, FALSE, FALSE, FALSE);
+        pa_sink_set_volume(i->sink, NULL, FALSE, i->save_volume);
     }
 
     pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i->sink), PA_SINK_MESSAGE_FINISH_MOVE, i, 0, NULL) == 0);
@@ -1266,11 +1268,30 @@ int pa_sink_input_finish_move(pa_sink_input *i, pa_sink *dest, pa_bool_t save) {
     return 0;
 }
 
+/* Called from main context */
+void pa_sink_input_fail_move(pa_sink_input *i) {
+
+    pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
+    pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
+    pa_assert(!i->sink);
+
+    /* Check if someone wants this sink input? */
+    if (pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_FAIL], i) == PA_HOOK_STOP)
+        return;
+
+    if (i->moving)
+        i->moving(i, NULL);
+
+    pa_sink_input_kill(i);
+}
+
 /* Called from main context */
 int pa_sink_input_move_to(pa_sink_input *i, pa_sink *dest, pa_bool_t save) {
     int r;
 
     pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
     pa_assert(i->sink);
     pa_sink_assert_ref(dest);
@@ -1289,6 +1310,7 @@ int pa_sink_input_move_to(pa_sink_input *i, pa_sink *dest, pa_bool_t save) {
     }
 
     if ((r = pa_sink_input_finish_move(i, dest, save)) < 0) {
+        pa_sink_input_fail_move(i);
         pa_sink_input_unref(i);
         return r;
     }
@@ -1301,7 +1323,9 @@ int pa_sink_input_move_to(pa_sink_input *i, pa_sink *dest, pa_bool_t save) {
 /* Called from IO thread context */
 void pa_sink_input_set_state_within_thread(pa_sink_input *i, pa_sink_input_state_t state) {
     pa_bool_t corking, uncorking;
+
     pa_sink_input_assert_ref(i);
+    pa_sink_input_assert_io_context(i);
 
     if (state == i->thread_info.state)
         return;
@@ -1411,6 +1435,7 @@ int pa_sink_input_process_msg(pa_msgobject *o, int code, void *userdata, int64_t
 /* Called from main thread */
 pa_sink_input_state_t pa_sink_input_get_state(pa_sink_input *i) {
     pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
 
     if (i->state == PA_SINK_INPUT_RUNNING || i->state == PA_SINK_INPUT_DRAINED)
         return pa_atomic_load(&i->thread_info.drained) ? PA_SINK_INPUT_DRAINED : PA_SINK_INPUT_RUNNING;
@@ -1421,6 +1446,7 @@ pa_sink_input_state_t pa_sink_input_get_state(pa_sink_input *i) {
 /* Called from IO context */
 pa_bool_t pa_sink_input_safe_to_remove(pa_sink_input *i) {
     pa_sink_input_assert_ref(i);
+    pa_sink_input_assert_io_context(i);
 
     if (PA_SINK_INPUT_IS_LINKED(i->thread_info.state))
         return pa_memblockq_is_empty(i->thread_info.render_memblockq);
@@ -1445,6 +1471,7 @@ void pa_sink_input_request_rewind(pa_sink_input *i, size_t nbytes  /* in our sam
      * rewound. */
 
     pa_sink_input_assert_ref(i);
+    pa_sink_input_assert_io_context(i);
 
     nbytes = PA_MAX(i->thread_info.rewrite_nbytes, nbytes);
 
@@ -1511,8 +1538,11 @@ void pa_sink_input_request_rewind(pa_sink_input *i, size_t nbytes  /* in our sam
 /* Called from main context */
 pa_memchunk* pa_sink_input_get_silence(pa_sink_input *i, pa_memchunk *ret) {
     pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
     pa_assert(ret);
 
+    /* FIXME: Shouldn't access resampler object from main context! */
+
     pa_silence_memchunk_get(
                 &i->core->silence_cache,
                 i->core->mempool,
@@ -1529,6 +1559,7 @@ void pa_sink_input_send_event(pa_sink_input *i, const char *event, pa_proplist *
     pa_sink_input_send_event_hook_data hook_data;
 
     pa_sink_input_assert_ref(i);
+    pa_assert_ctl_context();
     pa_assert(event);
 
     if (!i->send_event)
index 98144d412dcdbf80573e676dd1eaedf134f14cbd..ea0f8c0ecf775b7e3f1a06124ec00eeddeecbae8 100644 (file)
@@ -42,6 +42,7 @@ typedef enum pa_sink_input_state {
     PA_SINK_INPUT_RUNNING,      /*< The stream is alive and kicking */
     PA_SINK_INPUT_CORKED,       /*< The stream was corked on user request */
     PA_SINK_INPUT_UNLINKED      /*< The stream is dead */
+    /* FIXME: we need a state for MOVING here */
 } pa_sink_input_state_t;
 
 static inline pa_bool_t PA_SINK_INPUT_IS_LINKED(pa_sink_input_state_t x) {
@@ -58,7 +59,8 @@ typedef enum pa_sink_input_flags {
     PA_SINK_INPUT_FIX_RATE = 64,
     PA_SINK_INPUT_FIX_CHANNELS = 128,
     PA_SINK_INPUT_DONT_INHIBIT_AUTO_SUSPEND = 256,
-    PA_SINK_INPUT_FAIL_ON_SUSPEND = 512
+    PA_SINK_INPUT_NO_CREATE_ON_SUSPEND = 512,
+    PA_SINK_INPUT_KILL_ON_SUSPEND = 1024
 } pa_sink_input_flags_t;
 
 struct pa_sink_input {
@@ -92,10 +94,12 @@ struct pa_sink_input {
     pa_sink_input *sync_prev, *sync_next;
 
     /* Also see http://pulseaudio.org/wiki/InternalVolumes */
-    pa_cvolume virtual_volume;  /* The volume clients are informed about */
-    pa_cvolume volume_factor;   /* An internally used volume factor that can be used by modules to apply effects and suchlike without having that visible to the outside */
-    double relative_volume[PA_CHANNELS_MAX]; /* The calculated volume relative to the sink volume as linear factors. */
-    pa_cvolume soft_volume;     /* The internal software volume we apply to all PCM data while it passes through. Usually calculated as relative_volume * volume_factor  */
+    pa_cvolume volume;             /* The volume clients are informed about */
+    pa_cvolume reference_ratio;    /* The ratio of the stream's volume to the sink's reference volume */
+    pa_cvolume real_ratio;         /* The ratio of the stream's volume to the sink's real volume */
+    pa_cvolume volume_factor;      /* An internally used volume factor that can be used by modules to apply effects and suchlike without having that visible to the outside */
+    pa_cvolume soft_volume;        /* The internal software volume we apply to all PCM data while it passes through. Usually calculated as real_ratio * volume_factor */
+
     pa_bool_t muted:1;
 
     /* if TRUE then the source we are connected to and/or the volume
@@ -137,6 +141,10 @@ struct pa_sink_input {
      * from IO context. */
     void (*update_sink_latency_range) (pa_sink_input *i); /* may be NULL */
 
+    /* Called whenver the fixed latency of the sink changes, if there
+     * is one. Called from IO context. */
+    void (*update_sink_fixed_latency) (pa_sink_input *i); /* may be NULL */
+
     /* If non-NULL this function is called when the input is first
      * connected to a sink or when the rtpoll/asyncmsgq fields
      * change. You usually don't need to implement this function
@@ -159,7 +167,9 @@ struct pa_sink_input {
     /* If non-NULL called whenever the sink input is moved to a new
      * sink. Called from main context after the sink input has been
      * detached from the old sink and before it has been attached to
-     * the new sink. */
+     * the new sink. If dest is NULL the move was executed in two
+     * phases and the second one failed; the stream will be destroyed
+     * after this call. */
     void (*moving) (pa_sink_input *i, pa_sink *dest);   /* may be NULL */
 
     /* Supposed to unlink and destroy this stream. Called from main
@@ -303,6 +313,10 @@ void pa_sink_input_cork(pa_sink_input *i, pa_bool_t b);
 
 int pa_sink_input_set_rate(pa_sink_input *i, uint32_t rate);
 
+/* This returns the sink's fields converted into out sample type */
+size_t pa_sink_input_get_max_rewind(pa_sink_input *i);
+size_t pa_sink_input_get_max_request(pa_sink_input *i);
+
 /* Callable by everyone from main thread*/
 
 /* External code may request disconnection with this function */
@@ -313,8 +327,6 @@ pa_usec_t pa_sink_input_get_latency(pa_sink_input *i, pa_usec_t *sink_latency);
 void pa_sink_input_set_volume(pa_sink_input *i, const pa_cvolume *volume, pa_bool_t save, pa_bool_t absolute);
 pa_cvolume *pa_sink_input_get_volume(pa_sink_input *i, pa_cvolume *volume, pa_bool_t absolute);
 
-pa_cvolume *pa_sink_input_get_relative_volume(pa_sink_input *i, pa_cvolume *v);
-
 void pa_sink_input_set_mute(pa_sink_input *i, pa_bool_t mute, pa_bool_t save);
 pa_bool_t pa_sink_input_get_mute(pa_sink_input *i);
 
@@ -333,6 +345,7 @@ pa_bool_t pa_sink_input_may_move_to(pa_sink_input *i, pa_sink *dest); /* may thi
  * new sink */
 int pa_sink_input_start_move(pa_sink_input *i);
 int pa_sink_input_finish_move(pa_sink_input *i, pa_sink *dest, pa_bool_t save);
+void pa_sink_input_fail_move(pa_sink_input *i);
 
 pa_sink_input_state_t pa_sink_input_get_state(pa_sink_input *i);
 
@@ -356,7 +369,7 @@ pa_bool_t pa_sink_input_safe_to_remove(pa_sink_input *i);
 
 pa_memchunk* pa_sink_input_get_silence(pa_sink_input *i, pa_memchunk *ret);
 
-/* To be used by sink.c only */
-void pa_sink_input_set_relative_volume(pa_sink_input *i, const pa_cvolume *v);
+#define pa_sink_input_assert_io_context(s) \
+    pa_assert(pa_thread_mq_get() || !PA_SINK_INPUT_IS_LINKED((s)->state))
 
 #endif
index 5e9662c2b75794b3904a6a41aad99dc286dd2acf..1cce8e6b688295e442ea428f068f4fcce49b01c4 100644 (file)
@@ -59,7 +59,7 @@ static void sink_free(pa_object *s);
 pa_sink_new_data* pa_sink_new_data_init(pa_sink_new_data *data) {
     pa_assert(data);
 
-    memset(data, 0, sizeof(*data));
+    pa_zero(*data);
     data->proplist = pa_proplist_new();
 
     return data;
@@ -177,6 +177,7 @@ pa_sink* pa_sink_new(
     pa_assert(core);
     pa_assert(data);
     pa_assert(data->name);
+    pa_assert_ctl_context();
 
     s = pa_msgobject_new(pa_sink);
 
@@ -211,7 +212,7 @@ pa_sink* pa_sink_new(
         pa_cvolume_reset(&data->volume, data->sample_spec.channels);
 
     pa_return_null_if_fail(pa_cvolume_valid(&data->volume));
-    pa_return_null_if_fail(data->volume.channels == data->sample_spec.channels);
+    pa_return_null_if_fail(pa_cvolume_compatible(&data->volume, &data->sample_spec));
 
     if (!data->muted_is_set)
         data->muted = FALSE;
@@ -248,20 +249,17 @@ pa_sink* pa_sink_new(
     s->inputs = pa_idxset_new(NULL, NULL);
     s->n_corked = 0;
 
-    s->reference_volume = s->virtual_volume = data->volume;
+    s->reference_volume = s->real_volume = data->volume;
     pa_cvolume_reset(&s->soft_volume, s->sample_spec.channels);
     s->base_volume = PA_VOLUME_NORM;
     s->n_volume_steps = PA_VOLUME_NORM+1;
     s->muted = data->muted;
     s->refresh_volume = s->refresh_muted = FALSE;
 
-    s->fixed_latency = flags & PA_SINK_DYNAMIC_LATENCY ? 0 : DEFAULT_FIXED_LATENCY;
-
     reset_callbacks(s);
     s->userdata = NULL;
 
     s->asyncmsgq = NULL;
-    s->rtpoll = NULL;
 
     /* As a minor optimization we just steal the list instead of
      * copying it here */
@@ -294,6 +292,7 @@ pa_sink* pa_sink_new(
             &s->sample_spec,
             0);
 
+    s->thread_info.rtpoll = NULL;
     s->thread_info.inputs = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
     s->thread_info.soft_volume =  s->soft_volume;
     s->thread_info.soft_muted = s->muted;
@@ -306,7 +305,9 @@ pa_sink* pa_sink_new(
     s->thread_info.requested_latency = 0;
     s->thread_info.min_latency = ABSOLUTE_MIN_LATENCY;
     s->thread_info.max_latency = ABSOLUTE_MAX_LATENCY;
+    s->thread_info.fixed_latency = flags & PA_SINK_DYNAMIC_LATENCY ? 0 : DEFAULT_FIXED_LATENCY;
 
+    /* FIXME: This should probably be moved to pa_sink_put() */
     pa_assert_se(pa_idxset_put(core->sinks, s, &s->index) >= 0);
 
     if (s->card)
@@ -348,6 +349,7 @@ pa_sink* pa_sink_new(
     s->monitor_source->monitor_of = s;
 
     pa_source_set_latency_range(s->monitor_source, s->thread_info.min_latency, s->thread_info.max_latency);
+    pa_source_set_fixed_latency(s->monitor_source, s->thread_info.fixed_latency);
     pa_source_set_max_rewind(s->monitor_source, s->thread_info.max_rewind);
 
     return s;
@@ -360,6 +362,7 @@ static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
     pa_sink_state_t original_state;
 
     pa_assert(s);
+    pa_assert_ctl_context();
 
     if (s->state == state)
         return 0;
@@ -396,9 +399,9 @@ static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
 
         /* We're suspending or resuming, tell everyone about it */
 
-        for (i = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); i; i = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx)))
+        PA_IDXSET_FOREACH(i, s->inputs, idx)
             if (s->state == PA_SINK_SUSPENDED &&
-                (i->flags & PA_SINK_INPUT_FAIL_ON_SUSPEND))
+                (i->flags & PA_SINK_INPUT_KILL_ON_SUSPEND))
                 pa_sink_input_kill(i);
             else if (i->suspend)
                 i->suspend(i, state == PA_SINK_SUSPENDED);
@@ -413,12 +416,12 @@ static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
 /* Called from main context */
 void pa_sink_put(pa_sink* s) {
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
 
     pa_assert(s->state == PA_SINK_INIT);
 
     /* The following fields must be initialized properly when calling _put() */
     pa_assert(s->asyncmsgq);
-    pa_assert(s->rtpoll);
     pa_assert(s->thread_info.min_latency <= s->thread_info.max_latency);
 
     /* Generally, flags should be initialized via pa_sink_new(). As a
@@ -431,16 +434,21 @@ void pa_sink_put(pa_sink* s) {
     if ((s->flags & PA_SINK_DECIBEL_VOLUME) && s->core->flat_volumes)
         s->flags |= PA_SINK_FLAT_VOLUME;
 
+    /* We assume that if the sink implementor changed the default
+     * volume he did so in real_volume, because that is the usual
+     * place where he is supposed to place his changes.  */
+    s->reference_volume = s->real_volume;
+
     s->thread_info.soft_volume = s->soft_volume;
     s->thread_info.soft_muted = s->muted;
 
     pa_assert((s->flags & PA_SINK_HW_VOLUME_CTRL) || (s->base_volume == PA_VOLUME_NORM && s->flags & PA_SINK_DECIBEL_VOLUME));
     pa_assert(!(s->flags & PA_SINK_DECIBEL_VOLUME) || s->n_volume_steps == PA_VOLUME_NORM+1);
-    pa_assert(!(s->flags & PA_SINK_DYNAMIC_LATENCY) == (s->fixed_latency != 0));
+    pa_assert(!(s->flags & PA_SINK_DYNAMIC_LATENCY) == (s->thread_info.fixed_latency != 0));
     pa_assert(!(s->flags & PA_SINK_LATENCY) == !(s->monitor_source->flags & PA_SOURCE_LATENCY));
     pa_assert(!(s->flags & PA_SINK_DYNAMIC_LATENCY) == !(s->monitor_source->flags & PA_SOURCE_DYNAMIC_LATENCY));
 
-    pa_assert(s->monitor_source->fixed_latency == s->fixed_latency);
+    pa_assert(s->monitor_source->thread_info.fixed_latency == s->thread_info.fixed_latency);
     pa_assert(s->monitor_source->thread_info.min_latency == s->thread_info.min_latency);
     pa_assert(s->monitor_source->thread_info.max_latency == s->thread_info.max_latency);
 
@@ -458,6 +466,7 @@ void pa_sink_unlink(pa_sink* s) {
     pa_sink_input *i, *j = NULL;
 
     pa_assert(s);
+    pa_assert_ctl_context();
 
     /* Please note that pa_sink_unlink() does more than simply
      * reversing pa_sink_put(). It also undoes the registrations
@@ -507,6 +516,7 @@ static void sink_free(pa_object *o) {
     pa_sink_input *i;
 
     pa_assert(s);
+    pa_assert_ctl_context();
     pa_assert(pa_sink_refcnt(s) == 0);
 
     if (PA_SINK_IS_LINKED(s->state))
@@ -547,9 +557,10 @@ static void sink_free(pa_object *o) {
     pa_xfree(s);
 }
 
-/* Called from main context */
+/* Called from main context, and not while the IO thread is active, please */
 void pa_sink_set_asyncmsgq(pa_sink *s, pa_asyncmsgq *q) {
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
 
     s->asyncmsgq = q;
 
@@ -557,11 +568,32 @@ void pa_sink_set_asyncmsgq(pa_sink *s, pa_asyncmsgq *q) {
         pa_source_set_asyncmsgq(s->monitor_source, q);
 }
 
-/* Called from main context */
+/* Called from main context, and not while the IO thread is active, please */
+void pa_sink_update_flags(pa_sink *s, pa_sink_flags_t mask, pa_sink_flags_t value) {
+    pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
+
+    if (mask == 0)
+        return;
+
+    /* For now, allow only a minimal set of flags to be changed. */
+    pa_assert((mask & ~(PA_SINK_DYNAMIC_LATENCY|PA_SINK_LATENCY)) == 0);
+
+    s->flags = (s->flags & ~mask) | (value & mask);
+
+    pa_source_update_flags(s->monitor_source,
+                           ((mask & PA_SINK_LATENCY) ? PA_SOURCE_LATENCY : 0) |
+                           ((mask & PA_SINK_DYNAMIC_LATENCY) ? PA_SOURCE_DYNAMIC_LATENCY : 0),
+                           ((value & PA_SINK_LATENCY) ? PA_SOURCE_LATENCY : 0) |
+                           ((value & PA_SINK_DYNAMIC_LATENCY) ? PA_SINK_DYNAMIC_LATENCY : 0));
+}
+
+/* Called from IO context, or before _put() from main context */
 void pa_sink_set_rtpoll(pa_sink *s, pa_rtpoll *p) {
     pa_sink_assert_ref(s);
+    pa_sink_assert_io_context(s);
 
-    s->rtpoll = p;
+    s->thread_info.rtpoll = p;
 
     if (s->monitor_source)
         pa_source_set_rtpoll(s->monitor_source, p);
@@ -570,6 +602,7 @@ void pa_sink_set_rtpoll(pa_sink *s, pa_rtpoll *p) {
 /* Called from main context */
 int pa_sink_update_status(pa_sink*s) {
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_IS_LINKED(s->state));
 
     if (s->state == PA_SINK_SUSPENDED)
@@ -581,6 +614,7 @@ int pa_sink_update_status(pa_sink*s) {
 /* Called from main context */
 int pa_sink_suspend(pa_sink *s, pa_bool_t suspend, pa_suspend_cause_t cause) {
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_IS_LINKED(s->state));
     pa_assert(cause != 0);
 
@@ -609,6 +643,7 @@ pa_queue *pa_sink_move_all_start(pa_sink *s, pa_queue *q) {
     uint32_t idx;
 
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_IS_LINKED(s->state));
 
     if (!q)
@@ -633,12 +668,13 @@ void pa_sink_move_all_finish(pa_sink *s, pa_queue *q, pa_bool_t save) {
     pa_sink_input *i;
 
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_IS_LINKED(s->state));
     pa_assert(q);
 
     while ((i = PA_SINK_INPUT(pa_queue_pop(q)))) {
         if (pa_sink_input_finish_move(i, s, save) < 0)
-            pa_sink_input_kill(i);
+            pa_sink_input_fail_move(i);
 
         pa_sink_input_unref(i);
     }
@@ -649,13 +685,13 @@ void pa_sink_move_all_finish(pa_sink *s, pa_queue *q, pa_bool_t save) {
 /* Called from main context */
 void pa_sink_move_all_fail(pa_queue *q) {
     pa_sink_input *i;
+
+    pa_assert_ctl_context();
     pa_assert(q);
 
     while ((i = PA_SINK_INPUT(pa_queue_pop(q)))) {
-        if (pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_FAIL], i) == PA_HOOK_OK) {
-            pa_sink_input_kill(i);
-            pa_sink_input_unref(i);
-        }
+        pa_sink_input_fail_move(i);
+        pa_sink_input_unref(i);
     }
 
     pa_queue_free(q, NULL, NULL);
@@ -667,6 +703,7 @@ void pa_sink_process_rewind(pa_sink *s, size_t nbytes) {
     void *state = NULL;
 
     pa_sink_assert_ref(s);
+    pa_sink_assert_io_context(s);
     pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
 
     /* If nobody requested this and this is actually no real rewind
@@ -703,6 +740,7 @@ static unsigned fill_mix_info(pa_sink *s, size_t *length, pa_mix_info *info, uns
     size_t mixlength = *length;
 
     pa_sink_assert_ref(s);
+    pa_sink_assert_io_context(s);
     pa_assert(info);
 
     while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)) && maxinfo > 0) {
@@ -742,6 +780,7 @@ static void inputs_drop(pa_sink *s, pa_mix_info *info, unsigned n, pa_memchunk *
     unsigned n_unreffed = 0;
 
     pa_sink_assert_ref(s);
+    pa_sink_assert_io_context(s);
     pa_assert(result);
     pa_assert(result->memblock);
     pa_assert(result->length > 0);
@@ -837,6 +876,7 @@ void pa_sink_render(pa_sink*s, size_t length, pa_memchunk *result) {
     size_t block_size_max;
 
     pa_sink_assert_ref(s);
+    pa_sink_assert_io_context(s);
     pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
     pa_assert(pa_frame_aligned(length, &s->sample_spec));
     pa_assert(result);
@@ -923,6 +963,7 @@ void pa_sink_render_into(pa_sink*s, pa_memchunk *target) {
     size_t length, block_size_max;
 
     pa_sink_assert_ref(s);
+    pa_sink_assert_io_context(s);
     pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
     pa_assert(target);
     pa_assert(target->memblock);
@@ -1006,6 +1047,7 @@ void pa_sink_render_into_full(pa_sink *s, pa_memchunk *target) {
     size_t l, d;
 
     pa_sink_assert_ref(s);
+    pa_sink_assert_io_context(s);
     pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
     pa_assert(target);
     pa_assert(target->memblock);
@@ -1040,6 +1082,7 @@ void pa_sink_render_full(pa_sink *s, size_t length, pa_memchunk *result) {
     unsigned n;
 
     pa_sink_assert_ref(s);
+    pa_sink_assert_io_context(s);
     pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
     pa_assert(length > 0);
     pa_assert(pa_frame_aligned(length, &s->sample_spec));
@@ -1131,6 +1174,7 @@ pa_usec_t pa_sink_get_latency(pa_sink *s) {
     pa_usec_t usec = 0;
 
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_IS_LINKED(s->state));
 
     /* The returned value is supposed to be in the time domain of the sound card! */
@@ -1152,6 +1196,7 @@ pa_usec_t pa_sink_get_latency_within_thread(pa_sink *s) {
     pa_msgobject *o;
 
     pa_sink_assert_ref(s);
+    pa_sink_assert_io_context(s);
     pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
 
     /* The returned value is supposed to be in the time domain of the sound card! */
@@ -1164,7 +1209,7 @@ pa_usec_t pa_sink_get_latency_within_thread(pa_sink *s) {
 
     o = PA_MSGOBJECT(s);
 
-    /* We probably should make this a proper vtable callback instead of going through process_msg() */
+    /* FIXME: We probably should make this a proper vtable callback instead of going through process_msg() */
 
     if (o->process_msg(o, PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
         return -1;
@@ -1172,108 +1217,149 @@ pa_usec_t pa_sink_get_latency_within_thread(pa_sink *s) {
     return usec;
 }
 
-static void compute_new_soft_volume(pa_sink_input *i, const pa_cvolume *new_volume) {
-    unsigned c;
+/* Called from main context */
+static void compute_reference_ratios(pa_sink *s) {
+    uint32_t idx;
+    pa_sink_input *i;
 
-    pa_sink_input_assert_ref(i);
-    pa_assert(new_volume->channels == i->sample_spec.channels);
+    pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
+    pa_assert(PA_SINK_IS_LINKED(s->state));
+    pa_assert(s->flags & PA_SINK_FLAT_VOLUME);
 
-    /*
-     * This basically calculates:
-     *
-     * i->relative_volume := i->virtual_volume / new_volume
-     * i->soft_volume := i->relative_volume * i->volume_factor
-     */
+    PA_IDXSET_FOREACH(i, s->inputs, idx) {
+        unsigned c;
+        pa_cvolume remapped;
 
-    /* The new sink volume passed in here must already be remapped to
-     * the sink input's channel map! */
+        /*
+         * Calculates the reference volume from the sink's reference
+         * volume. This basically calculates:
+         *
+         * i->reference_ratio = i->volume / s->reference_volume
+         */
 
-    i->soft_volume.channels = i->sample_spec.channels;
+        remapped = s->reference_volume;
+        pa_cvolume_remap(&remapped, &s->channel_map, &i->channel_map);
 
-    for (c = 0; c < i->sample_spec.channels; c++)
+        i->reference_ratio.channels = i->sample_spec.channels;
 
-        if (new_volume->values[c] <= PA_VOLUME_MUTED)
-            /* We leave i->relative_volume untouched */
-            i->soft_volume.values[c] = PA_VOLUME_MUTED;
-        else {
-            i->relative_volume[c] =
-                pa_sw_volume_to_linear(i->virtual_volume.values[c]) /
-                pa_sw_volume_to_linear(new_volume->values[c]);
+        for (c = 0; c < i->sample_spec.channels; c++) {
 
-            i->soft_volume.values[c] = pa_sw_volume_from_linear(
-                    i->relative_volume[c] *
-                    pa_sw_volume_to_linear(i->volume_factor.values[c]));
+            /* We don't update when the sink volume is 0 anyway */
+            if (remapped.values[c] <= PA_VOLUME_MUTED)
+                continue;
+
+            /* Don't update the reference ratio unless necessary */
+            if (pa_sw_volume_multiply(
+                        i->reference_ratio.values[c],
+                        remapped.values[c]) == i->volume.values[c])
+                continue;
+
+            i->reference_ratio.values[c] = pa_sw_volume_divide(
+                    i->volume.values[c],
+                    remapped.values[c]);
         }
+    }
+}
+
+/* Called from main context */
+static void compute_real_ratios(pa_sink *s) {
+    pa_sink_input *i;
+    uint32_t idx;
+
+    pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
+    pa_assert(PA_SINK_IS_LINKED(s->state));
+    pa_assert(s->flags & PA_SINK_FLAT_VOLUME);
+
+    PA_IDXSET_FOREACH(i, s->inputs, idx) {
+        unsigned c;
+        pa_cvolume remapped;
+
+        /*
+         * This basically calculates:
+         *
+         * i->real_ratio := i->volume / s->real_volume
+         * i->soft_volume := i->real_ratio * i->volume_factor
+         */
+
+        remapped = s->real_volume;
+        pa_cvolume_remap(&remapped, &s->channel_map, &i->channel_map);
+
+        i->real_ratio.channels = i->sample_spec.channels;
+        i->soft_volume.channels = i->sample_spec.channels;
+
+        for (c = 0; c < i->sample_spec.channels; c++) {
+
+            if (remapped.values[c] <= PA_VOLUME_MUTED) {
+                /* We leave i->real_ratio untouched */
+                i->soft_volume.values[c] = PA_VOLUME_MUTED;
+                continue;
+            }
+
+            /* Don't lose accuracy unless necessary */
+            if (pa_sw_volume_multiply(
+                        i->real_ratio.values[c],
+                        remapped.values[c]) != i->volume.values[c])
+
+                i->real_ratio.values[c] = pa_sw_volume_divide(
+                        i->volume.values[c],
+                        remapped.values[c]);
 
-    /* Hooks have the ability to play games with i->soft_volume */
-    pa_hook_fire(&i->core->hooks[PA_CORE_HOOK_SINK_INPUT_SET_VOLUME], i);
+            i->soft_volume.values[c] = pa_sw_volume_multiply(
+                    i->real_ratio.values[c],
+                    i->volume_factor.values[c]);
+        }
 
-    /* We don't copy the soft_volume to the thread_info data
-     * here. That must be done by the caller */
+        /* We don't copy the soft_volume to the thread_info data
+         * here. That must be done by the caller */
+    }
 }
 
 /* Called from main thread */
-void pa_sink_update_flat_volume(pa_sink *s, pa_cvolume *new_volume) {
+static void compute_real_volume(pa_sink *s) {
     pa_sink_input *i;
     uint32_t idx;
 
     pa_sink_assert_ref(s);
-    pa_assert(new_volume);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_IS_LINKED(s->state));
     pa_assert(s->flags & PA_SINK_FLAT_VOLUME);
 
-    /* This is called whenever a sink input volume changes or a sink
-     * input is added/removed and we might need to fix up the sink
-     * volume accordingly. Please note that we don't actually update
-     * the sinks volume here, we only return how it needs to be
-     * updated. The caller should then call pa_sink_set_volume().*/
+    /* This determines the maximum volume of all streams and sets
+     * s->real_volume accordingly. */
 
     if (pa_idxset_isempty(s->inputs)) {
         /* In the special case that we have no sink input we leave the
          * volume unmodified. */
-        *new_volume = s->reference_volume;
+        s->real_volume = s->reference_volume;
         return;
     }
 
-    pa_cvolume_mute(new_volume, s->channel_map.channels);
+    pa_cvolume_mute(&s->real_volume, s->channel_map.channels);
 
     /* First let's determine the new maximum volume of all inputs
      * connected to this sink */
-    for (i = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); i; i = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx))) {
-        unsigned c;
-        pa_cvolume remapped_volume;
-
-        remapped_volume = i->virtual_volume;
-        pa_cvolume_remap(&remapped_volume, &i->channel_map, &s->channel_map);
+    PA_IDXSET_FOREACH(i, s->inputs, idx) {
+        pa_cvolume remapped;
 
-        for (c = 0; c < new_volume->channels; c++)
-            if (remapped_volume.values[c] > new_volume->values[c])
-                new_volume->values[c] = remapped_volume.values[c];
+        remapped = i->volume;
+        pa_cvolume_remap(&remapped, &i->channel_map, &s->channel_map);
+        pa_cvolume_merge(&s->real_volume, &s->real_volume, &remapped);
     }
 
-    /* Then, let's update the soft volumes of all inputs connected
-     * to this sink */
-    for (i = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); i; i = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx))) {
-        pa_cvolume remapped_new_volume;
-
-        remapped_new_volume = *new_volume;
-        pa_cvolume_remap(&remapped_new_volume, &s->channel_map, &i->channel_map);
-        compute_new_soft_volume(i, &remapped_new_volume);
-
-        /* We don't copy soft_volume to the thread_info data here
-         * (i.e. issue PA_SINK_INPUT_MESSAGE_SET_VOLUME) because we
-         * want the update to be atomically with the sink volume
-         * update, hence we do it within the pa_sink_set_volume() call
-         * below */
-    }
+    /* Then, let's update the real ratios/soft volumes of all inputs
+     * connected to this sink */
+    compute_real_ratios(s);
 }
 
 /* Called from main thread */
-void pa_sink_propagate_flat_volume(pa_sink *s) {
+static void propagate_reference_volume(pa_sink *s) {
     pa_sink_input *i;
     uint32_t idx;
 
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_IS_LINKED(s->state));
     pa_assert(s->flags & PA_SINK_FLAT_VOLUME);
 
@@ -1281,63 +1367,77 @@ void pa_sink_propagate_flat_volume(pa_sink *s) {
      * caused by a sink input volume change. We need to fix up the
      * sink input volumes accordingly */
 
-    for (i = PA_SINK_INPUT(pa_idxset_first(s->inputs, &idx)); i; i = PA_SINK_INPUT(pa_idxset_next(s->inputs, &idx))) {
-        pa_cvolume sink_volume, new_virtual_volume;
-        unsigned c;
-
-        /* This basically calculates i->virtual_volume := i->relative_volume * s->virtual_volume  */
-
-        sink_volume = s->virtual_volume;
-        pa_cvolume_remap(&sink_volume, &s->channel_map, &i->channel_map);
-
-        for (c = 0; c < i->sample_spec.channels; c++)
-            new_virtual_volume.values[c] = pa_sw_volume_from_linear(
-                    i->relative_volume[c] *
-                    pa_sw_volume_to_linear(sink_volume.values[c]));
+    PA_IDXSET_FOREACH(i, s->inputs, idx) {
+        pa_cvolume old_volume, remapped;
 
-        new_virtual_volume.channels = i->sample_spec.channels;
+        old_volume = i->volume;
 
-        if (!pa_cvolume_equal(&new_virtual_volume, &i->virtual_volume)) {
-            i->virtual_volume = new_virtual_volume;
+        /* This basically calculates:
+         *
+         * i->volume := s->reference_volume * i->reference_ratio  */
 
-            /* Hmm, the soft volume might no longer actually match
-             * what has been chosen as new virtual volume here,
-             * especially when the old volume was
-             * PA_VOLUME_MUTED. Hence let's recalculate the soft
-             * volumes here. */
-            compute_new_soft_volume(i, &sink_volume);
+        remapped = s->reference_volume;
+        pa_cvolume_remap(&remapped, &s->channel_map, &i->channel_map);
+        pa_sw_cvolume_multiply(&i->volume, &remapped, &i->reference_ratio);
 
-            /* The virtual volume changed, let's tell people so */
+        /* The reference volume changed, let's tell people so */
+        if (!pa_cvolume_equal(&old_volume, &i->volume))
             pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
-        }
     }
-
-    /* If the soft_volume of any of the sink inputs got changed, let's
-     * make sure the thread copies are synced up. */
-    pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SYNC_VOLUMES, NULL, 0, NULL) == 0);
 }
 
 /* Called from main thread */
-void pa_sink_set_volume(pa_sink *s, const pa_cvolume *volume, pa_bool_t propagate, pa_bool_t sendmsg, pa_bool_t become_reference, pa_bool_t save) {
-    pa_bool_t virtual_volume_changed;
+void pa_sink_set_volume(
+        pa_sink *s,
+        const pa_cvolume *volume,
+        pa_bool_t sendmsg,
+        pa_bool_t save) {
+
+    pa_cvolume old_reference_volume;
+    pa_bool_t reference_changed;
 
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_IS_LINKED(s->state));
-    pa_assert(volume);
-    pa_assert(pa_cvolume_valid(volume));
-    pa_assert(pa_cvolume_compatible(volume, &s->sample_spec));
+    pa_assert(!volume || pa_cvolume_valid(volume));
+    pa_assert(!volume || pa_cvolume_compatible(volume, &s->sample_spec));
+    pa_assert(volume || (s->flags & PA_SINK_FLAT_VOLUME));
+
+    /* If volume is NULL we synchronize the sink's real and reference
+     * volumes with the stream volumes. If it is not NULL we update
+     * the reference_volume with it. */
+
+    old_reference_volume = s->reference_volume;
 
-    virtual_volume_changed = !pa_cvolume_equal(volume, &s->virtual_volume);
-    s->virtual_volume = *volume;
-    s->save_volume = (!virtual_volume_changed && s->save_volume) || save;
+    if (volume) {
 
-    if (become_reference)
-        s->reference_volume = s->virtual_volume;
+        s->reference_volume = *volume;
 
-    /* Propagate this volume change back to the inputs */
-    if (virtual_volume_changed)
-        if (propagate && (s->flags & PA_SINK_FLAT_VOLUME))
-            pa_sink_propagate_flat_volume(s);
+        if (s->flags & PA_SINK_FLAT_VOLUME) {
+            /* OK, propagate this volume change back to the inputs */
+            propagate_reference_volume(s);
+
+            /* And now recalculate the real volume */
+            compute_real_volume(s);
+        } else
+            s->real_volume = s->reference_volume;
+
+    } else {
+        pa_assert(s->flags & PA_SINK_FLAT_VOLUME);
+
+        /* Ok, let's determine the new real volume */
+        compute_real_volume(s);
+
+        /* Let's 'push' the reference volume if necessary */
+        pa_cvolume_merge(&s->reference_volume, &s->reference_volume, &s->real_volume);
+
+        /* We need to fix the reference ratios of all streams now that
+         * we changed the reference volume */
+        compute_reference_ratios(s);
+    }
+
+    reference_changed = !pa_cvolume_equal(&old_reference_volume, &s->reference_volume);
+    s->save_volume = (!reference_changed && s->save_volume) || save;
 
     if (s->set_volume) {
         /* If we have a function set_volume(), then we do not apply a
@@ -1350,76 +1450,128 @@ void pa_sink_set_volume(pa_sink *s, const pa_cvolume *volume, pa_bool_t propagat
     } else
         /* If we have no function set_volume(), then the soft volume
          * becomes the virtual volume */
-        s->soft_volume = s->virtual_volume;
+        s->soft_volume = s->real_volume;
 
     /* This tells the sink that soft and/or virtual volume changed */
     if (sendmsg)
         pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_VOLUME, NULL, 0, NULL) == 0);
 
-    if (virtual_volume_changed)
+    if (reference_changed)
         pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
 }
 
 /* Called from main thread. Only to be called by sink implementor */
 void pa_sink_set_soft_volume(pa_sink *s, const pa_cvolume *volume) {
     pa_sink_assert_ref(s);
-    pa_assert(volume);
+    pa_assert_ctl_context();
 
-    s->soft_volume = *volume;
+    if (!volume)
+        pa_cvolume_reset(&s->soft_volume, s->sample_spec.channels);
+    else
+        s->soft_volume = *volume;
 
     if (PA_SINK_IS_LINKED(s->state))
         pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_VOLUME, NULL, 0, NULL) == 0);
     else
-        s->thread_info.soft_volume = *volume;
+        s->thread_info.soft_volume = s->soft_volume;
 }
 
-/* Called from main thread */
-const pa_cvolume *pa_sink_get_volume(pa_sink *s, pa_bool_t force_refresh, pa_bool_t reference) {
+static void propagate_real_volume(pa_sink *s, const pa_cvolume *old_real_volume) {
+    pa_sink_input *i;
+    uint32_t idx;
+    pa_cvolume old_reference_volume;
+
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
+    pa_assert(PA_SINK_IS_LINKED(s->state));
 
-    if (s->refresh_volume || force_refresh) {
-        struct pa_cvolume old_virtual_volume = s->virtual_volume;
+    /* This is called when the hardware's real volume changes due to
+     * some external event. We copy the real volume into our
+     * reference volume and then rebuild the stream volumes based on
+     * i->real_ratio which should stay fixed. */
 
-        if (s->get_volume)
-            s->get_volume(s);
+    if (pa_cvolume_equal(old_real_volume, &s->real_volume))
+        return;
 
-        pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_VOLUME, NULL, 0, NULL) == 0);
+    old_reference_volume = s->reference_volume;
 
-        if (!pa_cvolume_equal(&old_virtual_volume, &s->virtual_volume)) {
+    /* 1. Make the real volume the reference volume */
+    s->reference_volume = s->real_volume;
 
-            s->reference_volume = s->virtual_volume;
+    if (s->flags & PA_SINK_FLAT_VOLUME) {
 
-            /* Something got changed in the hardware. It probably
-             * makes sense to save changed hw settings given that hw
-             * volume changes not triggered by PA are almost certainly
-             * done by the user. */
-            s->save_volume = TRUE;
+        PA_IDXSET_FOREACH(i, s->inputs, idx) {
+            pa_cvolume old_volume, remapped;
 
-            if (s->flags & PA_SINK_FLAT_VOLUME)
-                pa_sink_propagate_flat_volume(s);
+            old_volume = i->volume;
 
-            pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
+            /* 2. Since the sink's reference and real volumes are equal
+             * now our ratios should be too. */
+            i->reference_ratio = i->real_ratio;
+
+            /* 3. Recalculate the new stream reference volume based on the
+             * reference ratio and the sink's reference volume.
+             *
+             * This basically calculates:
+             *
+             * i->volume = s->reference_volume * i->reference_ratio
+             *
+             * This is identical to propagate_reference_volume() */
+            remapped = s->reference_volume;
+            pa_cvolume_remap(&remapped, &s->channel_map, &i->channel_map);
+            pa_sw_cvolume_multiply(&i->volume, &remapped, &i->reference_ratio);
+
+            /* Notify if something changed */
+            if (!pa_cvolume_equal(&old_volume, &i->volume))
+                pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
         }
     }
 
-    return reference ? &s->reference_volume : &s->virtual_volume;
+    /* Something got changed in the hardware. It probably makes sense
+     * to save changed hw settings given that hw volume changes not
+     * triggered by PA are almost certainly done by the user. */
+    s->save_volume = TRUE;
+
+    if (!pa_cvolume_equal(&old_reference_volume, &s->reference_volume))
+        pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
 }
 
 /* Called from main thread */
-void pa_sink_volume_changed(pa_sink *s, const pa_cvolume *new_volume) {
+const pa_cvolume *pa_sink_get_volume(pa_sink *s, pa_bool_t force_refresh) {
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
+    pa_assert(PA_SINK_IS_LINKED(s->state));
 
-    /* The sink implementor may call this if the volume changed to make sure everyone is notified */
-    if (pa_cvolume_equal(&s->virtual_volume, new_volume))
-        return;
+    if (s->refresh_volume || force_refresh) {
+        struct pa_cvolume old_real_volume;
 
-    s->reference_volume = s->virtual_volume = *new_volume;
-    s->save_volume = TRUE;
+        old_real_volume = s->real_volume;
 
-    if (s->flags & PA_SINK_FLAT_VOLUME)
-        pa_sink_propagate_flat_volume(s);
+        if (s->get_volume)
+            s->get_volume(s);
 
-    pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
+        pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_VOLUME, NULL, 0, NULL) == 0);
+
+        propagate_real_volume(s, &old_real_volume);
+    }
+
+    return &s->reference_volume;
+}
+
+/* Called from main thread */
+void pa_sink_volume_changed(pa_sink *s, const pa_cvolume *new_real_volume) {
+    pa_cvolume old_real_volume;
+
+    pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
+    pa_assert(PA_SINK_IS_LINKED(s->state));
+
+    /* The sink implementor may call this if the volume changed to make sure everyone is notified */
+
+    old_real_volume = s->real_volume;
+    s->real_volume = *new_real_volume;
+
+    propagate_real_volume(s, &old_real_volume);
 }
 
 /* Called from main thread */
@@ -1427,6 +1579,7 @@ void pa_sink_set_mute(pa_sink *s, pa_bool_t mute, pa_bool_t save) {
     pa_bool_t old_muted;
 
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_IS_LINKED(s->state));
 
     old_muted = s->muted;
@@ -1446,6 +1599,8 @@ void pa_sink_set_mute(pa_sink *s, pa_bool_t mute, pa_bool_t save) {
 pa_bool_t pa_sink_get_mute(pa_sink *s, pa_bool_t force_refresh) {
 
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
+    pa_assert(PA_SINK_IS_LINKED(s->state));
 
     if (s->refresh_muted || force_refresh) {
         pa_bool_t old_muted = s->muted;
@@ -1465,13 +1620,14 @@ pa_bool_t pa_sink_get_mute(pa_sink *s, pa_bool_t force_refresh) {
         }
     }
 
-
     return s->muted;
 }
 
 /* Called from main thread */
 void pa_sink_mute_changed(pa_sink *s, pa_bool_t new_muted) {
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
+    pa_assert(PA_SINK_IS_LINKED(s->state));
 
     /* The sink implementor may call this if the volume changed to make sure everyone is notified */
 
@@ -1487,6 +1643,7 @@ void pa_sink_mute_changed(pa_sink *s, pa_bool_t new_muted) {
 /* Called from main thread */
 pa_bool_t pa_sink_update_proplist(pa_sink *s, pa_update_mode_t mode, pa_proplist *p) {
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
 
     if (p)
         pa_proplist_update(s->proplist, mode, p);
@@ -1500,16 +1657,18 @@ pa_bool_t pa_sink_update_proplist(pa_sink *s, pa_update_mode_t mode, pa_proplist
 }
 
 /* Called from main thread */
+/* FIXME -- this should be dropped and be merged into pa_sink_update_proplist() */
 void pa_sink_set_description(pa_sink *s, const char *description) {
     const char *old;
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
 
     if (!description && !pa_proplist_contains(s->proplist, PA_PROP_DEVICE_DESCRIPTION))
         return;
 
     old = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
 
-    if (old && description && !strcmp(old, description))
+    if (old && description && pa_streq(old, description))
         return;
 
     if (description)
@@ -1536,6 +1695,7 @@ unsigned pa_sink_linked_by(pa_sink *s) {
     unsigned ret;
 
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_IS_LINKED(s->state));
 
     ret = pa_idxset_size(s->inputs);
@@ -1554,6 +1714,7 @@ unsigned pa_sink_used_by(pa_sink *s) {
     unsigned ret;
 
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_IS_LINKED(s->state));
 
     ret = pa_idxset_size(s->inputs);
@@ -1572,6 +1733,7 @@ unsigned pa_sink_check_suspend(pa_sink *s) {
     uint32_t idx;
 
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
 
     if (!PA_SINK_IS_LINKED(s->state))
         return 0;
@@ -1605,8 +1767,9 @@ static void sync_input_volumes_within_thread(pa_sink *s) {
     void *state = NULL;
 
     pa_sink_assert_ref(s);
+    pa_sink_assert_io_context(s);
 
-    while ((i = PA_SINK_INPUT(pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))) {
+    PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state) {
         if (pa_cvolume_equal(&i->thread_info.soft_volume, &i->soft_volume))
             continue;
 
@@ -1709,7 +1872,7 @@ int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offse
             if (pa_hashmap_remove(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index)))
                 pa_sink_input_unref(i);
 
-            pa_sink_invalidate_requested_latency(s);
+            pa_sink_invalidate_requested_latency(s, TRUE);
             pa_sink_request_rewind(s, (size_t) -1);
 
             /* In flat volume mode we need to update the volume as
@@ -1731,10 +1894,7 @@ int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offse
                 size_t sink_nbytes, total_nbytes;
 
                 /* Get the latency of the sink */
-                if (!(s->flags & PA_SINK_LATENCY) ||
-                    PA_MSGOBJECT(s)->process_msg(PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
-                    usec = 0;
-
+                usec = pa_sink_get_latency_within_thread(s);
                 sink_nbytes = pa_usec_to_bytes(usec, &s->sample_spec);
                 total_nbytes = sink_nbytes + pa_memblockq_get_length(i->thread_info.render_memblockq);
 
@@ -1755,7 +1915,7 @@ int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offse
             if (pa_hashmap_remove(s->thread_info.inputs, PA_UINT32_TO_PTR(i->index)))
                 pa_sink_input_unref(i);
 
-            pa_sink_invalidate_requested_latency(s);
+            pa_sink_invalidate_requested_latency(s, TRUE);
 
             pa_log_debug("Requesting rewind due to started move");
             pa_sink_request_rewind(s, (size_t) -1);
@@ -1793,10 +1953,7 @@ int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offse
                 size_t nbytes;
 
                 /* Get the latency of the sink */
-                if (!(s->flags & PA_SINK_LATENCY) ||
-                    PA_MSGOBJECT(s)->process_msg(PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
-                    usec = 0;
-
+                usec = pa_sink_get_latency_within_thread(s);
                 nbytes = pa_usec_to_bytes(usec, &s->sample_spec);
 
                 if (nbytes > 0)
@@ -1884,6 +2041,9 @@ int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offse
             pa_usec_t *usec = userdata;
             *usec = pa_sink_get_requested_latency_within_thread(s);
 
+            /* Yes, that's right, the IO thread will see -1 when no
+             * explicit requested latency is configured, the main
+             * thread will see max_latency */
             if (*usec == (pa_usec_t) -1)
                 *usec = s->thread_info.max_latency;
 
@@ -1907,6 +2067,16 @@ int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offse
             return 0;
         }
 
+        case PA_SINK_MESSAGE_GET_FIXED_LATENCY:
+
+            *((pa_usec_t*) userdata) = s->thread_info.fixed_latency;
+            return 0;
+
+        case PA_SINK_MESSAGE_SET_FIXED_LATENCY:
+
+            pa_sink_set_fixed_latency_within_thread(s, (pa_usec_t) offset);
+            return 0;
+
         case PA_SINK_MESSAGE_GET_MAX_REWIND:
 
             *((size_t*) userdata) = s->thread_info.max_rewind;
@@ -1942,9 +2112,10 @@ int pa_sink_suspend_all(pa_core *c, pa_bool_t suspend, pa_suspend_cause_t cause)
     int ret = 0;
 
     pa_core_assert_ref(c);
+    pa_assert_ctl_context();
     pa_assert(cause != 0);
 
-    for (sink = PA_SINK(pa_idxset_first(c->sinks, &idx)); sink; sink = PA_SINK(pa_idxset_next(c->sinks, &idx))) {
+    PA_IDXSET_FOREACH(sink, c->sinks, idx) {
         int r;
 
         if ((r = pa_sink_suspend(sink, suspend, cause)) < 0)
@@ -1957,6 +2128,7 @@ int pa_sink_suspend_all(pa_core *c, pa_bool_t suspend, pa_suspend_cause_t cause)
 /* Called from main thread */
 void pa_sink_detach(pa_sink *s) {
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_IS_LINKED(s->state));
 
     pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_DETACH, NULL, 0, NULL) == 0);
@@ -1965,6 +2137,7 @@ void pa_sink_detach(pa_sink *s) {
 /* Called from main thread */
 void pa_sink_attach(pa_sink *s) {
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_IS_LINKED(s->state));
 
     pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_ATTACH, NULL, 0, NULL) == 0);
@@ -1976,9 +2149,10 @@ void pa_sink_detach_within_thread(pa_sink *s) {
     void *state = NULL;
 
     pa_sink_assert_ref(s);
+    pa_sink_assert_io_context(s);
     pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
 
-    while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
+    PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state)
         if (i->detach)
             i->detach(i);
 
@@ -1992,9 +2166,10 @@ void pa_sink_attach_within_thread(pa_sink *s) {
     void *state = NULL;
 
     pa_sink_assert_ref(s);
+    pa_sink_assert_io_context(s);
     pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
 
-    while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
+    PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state)
         if (i->attach)
             i->attach(i);
 
@@ -2005,6 +2180,7 @@ void pa_sink_attach_within_thread(pa_sink *s) {
 /* Called from IO thread */
 void pa_sink_request_rewind(pa_sink*s, size_t nbytes) {
     pa_sink_assert_ref(s);
+    pa_sink_assert_io_context(s);
     pa_assert(PA_SINK_IS_LINKED(s->thread_info.state));
 
     if (s->thread_info.state == PA_SINK_SUSPENDED)
@@ -2034,15 +2210,15 @@ pa_usec_t pa_sink_get_requested_latency_within_thread(pa_sink *s) {
     pa_usec_t monitor_latency;
 
     pa_sink_assert_ref(s);
+    pa_sink_assert_io_context(s);
 
     if (!(s->flags & PA_SINK_DYNAMIC_LATENCY))
-        return PA_CLAMP(s->fixed_latency, s->thread_info.min_latency, s->thread_info.max_latency);
+        return PA_CLAMP(s->thread_info.fixed_latency, s->thread_info.min_latency, s->thread_info.max_latency);
 
     if (s->thread_info.requested_latency_valid)
         return s->thread_info.requested_latency;
 
-    while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
-
+    PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state)
         if (i->thread_info.requested_sink_latency != (pa_usec_t) -1 &&
             (result == (pa_usec_t) -1 || result > i->thread_info.requested_sink_latency))
             result = i->thread_info.requested_sink_latency;
@@ -2070,6 +2246,7 @@ pa_usec_t pa_sink_get_requested_latency(pa_sink *s) {
     pa_usec_t usec = 0;
 
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
     pa_assert(PA_SINK_IS_LINKED(s->state));
 
     if (s->state == PA_SINK_SUSPENDED)
@@ -2085,16 +2262,16 @@ void pa_sink_set_max_rewind_within_thread(pa_sink *s, size_t max_rewind) {
     void *state = NULL;
 
     pa_sink_assert_ref(s);
+    pa_sink_assert_io_context(s);
 
     if (max_rewind == s->thread_info.max_rewind)
         return;
 
     s->thread_info.max_rewind = max_rewind;
 
-    if (PA_SINK_IS_LINKED(s->thread_info.state)) {
-        while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
+    if (PA_SINK_IS_LINKED(s->thread_info.state))
+        PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state)
             pa_sink_input_update_max_rewind(i, s->thread_info.max_rewind);
-    }
 
     if (s->monitor_source)
         pa_source_set_max_rewind_within_thread(s->monitor_source, s->thread_info.max_rewind);
@@ -2103,6 +2280,7 @@ void pa_sink_set_max_rewind_within_thread(pa_sink *s, size_t max_rewind) {
 /* Called from main thread */
 void pa_sink_set_max_rewind(pa_sink *s, size_t max_rewind) {
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
 
     if (PA_SINK_IS_LINKED(s->state))
         pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_MAX_REWIND, NULL, max_rewind, NULL) == 0);
@@ -2115,6 +2293,7 @@ void pa_sink_set_max_request_within_thread(pa_sink *s, size_t max_request) {
     void *state = NULL;
 
     pa_sink_assert_ref(s);
+    pa_sink_assert_io_context(s);
 
     if (max_request == s->thread_info.max_request)
         return;
@@ -2124,7 +2303,7 @@ void pa_sink_set_max_request_within_thread(pa_sink *s, size_t max_request) {
     if (PA_SINK_IS_LINKED(s->thread_info.state)) {
         pa_sink_input *i;
 
-        while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
+        PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state)
             pa_sink_input_update_max_request(i, s->thread_info.max_request);
     }
 }
@@ -2132,6 +2311,7 @@ void pa_sink_set_max_request_within_thread(pa_sink *s, size_t max_request) {
 /* Called from main thread */
 void pa_sink_set_max_request(pa_sink *s, size_t max_request) {
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
 
     if (PA_SINK_IS_LINKED(s->state))
         pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_MAX_REQUEST, NULL, max_request, NULL) == 0);
@@ -2140,23 +2320,24 @@ void pa_sink_set_max_request(pa_sink *s, size_t max_request) {
 }
 
 /* Called from IO thread */
-void pa_sink_invalidate_requested_latency(pa_sink *s) {
+void pa_sink_invalidate_requested_latency(pa_sink *s, pa_bool_t dynamic) {
     pa_sink_input *i;
     void *state = NULL;
 
     pa_sink_assert_ref(s);
+    pa_sink_assert_io_context(s);
 
-    if (!(s->flags & PA_SINK_DYNAMIC_LATENCY))
+    if ((s->flags & PA_SINK_DYNAMIC_LATENCY))
+        s->thread_info.requested_latency_valid = FALSE;
+    else if (dynamic)
         return;
 
-    s->thread_info.requested_latency_valid = FALSE;
-
     if (PA_SINK_IS_LINKED(s->thread_info.state)) {
 
         if (s->update_requested_latency)
             s->update_requested_latency(s);
 
-        while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
+        PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state)
             if (i->update_sink_requested_latency)
                 i->update_sink_requested_latency(i);
     }
@@ -2165,6 +2346,7 @@ void pa_sink_invalidate_requested_latency(pa_sink *s) {
 /* Called from main thread */
 void pa_sink_set_latency_range(pa_sink *s, pa_usec_t min_latency, pa_usec_t max_latency) {
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
 
     /* min_latency == 0:           no limit
      * min_latency anything else:  specified limit
@@ -2199,6 +2381,7 @@ void pa_sink_set_latency_range(pa_sink *s, pa_usec_t min_latency, pa_usec_t max_
 /* Called from main thread */
 void pa_sink_get_latency_range(pa_sink *s, pa_usec_t *min_latency, pa_usec_t *max_latency) {
    pa_sink_assert_ref(s);
+   pa_assert_ctl_context();
    pa_assert(min_latency);
    pa_assert(max_latency);
 
@@ -2217,9 +2400,8 @@ void pa_sink_get_latency_range(pa_sink *s, pa_usec_t *min_latency, pa_usec_t *ma
 
 /* Called from IO thread */
 void pa_sink_set_latency_range_within_thread(pa_sink *s, pa_usec_t min_latency, pa_usec_t max_latency) {
-    void *state = NULL;
-
     pa_sink_assert_ref(s);
+    pa_sink_assert_io_context(s);
 
     pa_assert(min_latency >= ABSOLUTE_MIN_LATENCY);
     pa_assert(max_latency <= ABSOLUTE_MAX_LATENCY);
@@ -2230,27 +2412,36 @@ void pa_sink_set_latency_range_within_thread(pa_sink *s, pa_usec_t min_latency,
                max_latency == ABSOLUTE_MAX_LATENCY) ||
               (s->flags & PA_SINK_DYNAMIC_LATENCY));
 
+    if (s->thread_info.min_latency == min_latency &&
+        s->thread_info.max_latency == max_latency)
+        return;
+
     s->thread_info.min_latency = min_latency;
     s->thread_info.max_latency = max_latency;
 
     if (PA_SINK_IS_LINKED(s->thread_info.state)) {
         pa_sink_input *i;
+        void *state = NULL;
 
-        while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL)))
+        PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state)
             if (i->update_sink_latency_range)
                 i->update_sink_latency_range(i);
     }
 
-    pa_sink_invalidate_requested_latency(s);
+    pa_sink_invalidate_requested_latency(s, FALSE);
 
     pa_source_set_latency_range_within_thread(s->monitor_source, min_latency, max_latency);
 }
 
-/* Called from main thread, before the sink is put */
+/* Called from main thread */
 void pa_sink_set_fixed_latency(pa_sink *s, pa_usec_t latency) {
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
 
-    pa_assert(pa_sink_get_state(s) == PA_SINK_INIT);
+    if (s->flags & PA_SINK_DYNAMIC_LATENCY) {
+        pa_assert(latency == 0);
+        return;
+    }
 
     if (latency < ABSOLUTE_MIN_LATENCY)
         latency = ABSOLUTE_MIN_LATENCY;
@@ -2258,14 +2449,69 @@ void pa_sink_set_fixed_latency(pa_sink *s, pa_usec_t latency) {
     if (latency > ABSOLUTE_MAX_LATENCY)
         latency = ABSOLUTE_MAX_LATENCY;
 
-    s->fixed_latency = latency;
+    if (PA_SINK_IS_LINKED(s->state))
+        pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_FIXED_LATENCY, NULL, (int64_t) latency, NULL) == 0);
+    else
+        s->thread_info.fixed_latency = latency;
+
     pa_source_set_fixed_latency(s->monitor_source, latency);
 }
 
+/* Called from main thread */
+pa_usec_t pa_sink_get_fixed_latency(pa_sink *s) {
+    pa_usec_t latency;
+
+    pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
+
+    if (s->flags & PA_SINK_DYNAMIC_LATENCY)
+        return 0;
+
+    if (PA_SINK_IS_LINKED(s->state))
+        pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_FIXED_LATENCY, &latency, 0, NULL) == 0);
+    else
+        latency = s->thread_info.fixed_latency;
+
+    return latency;
+}
+
+/* Called from IO thread */
+void pa_sink_set_fixed_latency_within_thread(pa_sink *s, pa_usec_t latency) {
+    pa_sink_assert_ref(s);
+    pa_sink_assert_io_context(s);
+
+    if (s->flags & PA_SINK_DYNAMIC_LATENCY) {
+        pa_assert(latency == 0);
+        return;
+    }
+
+    pa_assert(latency >= ABSOLUTE_MIN_LATENCY);
+    pa_assert(latency <= ABSOLUTE_MAX_LATENCY);
+
+    if (s->thread_info.fixed_latency == latency)
+        return;
+
+    s->thread_info.fixed_latency = latency;
+
+    if (PA_SINK_IS_LINKED(s->thread_info.state)) {
+        pa_sink_input *i;
+        void *state = NULL;
+
+        PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state)
+            if (i->update_sink_fixed_latency)
+                i->update_sink_fixed_latency(i);
+    }
+
+    pa_sink_invalidate_requested_latency(s, FALSE);
+
+    pa_source_set_fixed_latency_within_thread(s->monitor_source, latency);
+}
+
 /* Called from main context */
 size_t pa_sink_get_max_rewind(pa_sink *s) {
     size_t r;
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
 
     if (!PA_SINK_IS_LINKED(s->state))
         return s->thread_info.max_rewind;
@@ -2279,6 +2525,7 @@ size_t pa_sink_get_max_rewind(pa_sink *s) {
 size_t pa_sink_get_max_request(pa_sink *s) {
     size_t r;
     pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
 
     if (!PA_SINK_IS_LINKED(s->state))
         return s->thread_info.max_request;
@@ -2292,7 +2539,8 @@ size_t pa_sink_get_max_request(pa_sink *s) {
 int pa_sink_set_port(pa_sink *s, const char *name, pa_bool_t save) {
     pa_device_port *port;
 
-    pa_assert(s);
+    pa_sink_assert_ref(s);
+    pa_assert_ctl_context();
 
     if (!s->set_port) {
         pa_log_debug("set_port() operation not implemented for sink %u \"%s\"", s->index, s->name);
@@ -2323,7 +2571,6 @@ int pa_sink_set_port(pa_sink *s, const char *name, pa_bool_t save) {
     return 0;
 }
 
-/* Called from main context */
 pa_bool_t pa_device_init_icon(pa_proplist *p, pa_bool_t is_sink) {
     const char *ff, *c, *t = NULL, *s = "", *profile, *bus;
 
index 7a8cdaf1f48db99b48901c3b4cc65490cd86829a..936d1c2a1db4fc97b2bdfd4fb8ad607bb480796e 100644 (file)
@@ -42,6 +42,7 @@ typedef struct pa_device_port pa_device_port;
 #include <pulsecore/rtpoll.h>
 #include <pulsecore/card.h>
 #include <pulsecore/queue.h>
+#include <pulsecore/thread-mq.h>
 
 #define PA_MAX_INPUTS_PER_SINK 32
 
@@ -89,9 +90,10 @@ struct pa_sink {
     unsigned n_volume_steps; /* shall be constant */
 
     /* Also see http://pulseaudio.org/wiki/InternalVolumes */
-    pa_cvolume virtual_volume;   /* The volume clients are informed about */
-    pa_cvolume reference_volume; /* The volume taken as refernce base for relative sink input volumes */
+    pa_cvolume reference_volume; /* The volume exported and taken as reference base for relative sink input volumes */
+    pa_cvolume real_volume;      /* The volume that the hardware is configured to  */
     pa_cvolume soft_volume;      /* The internal software volume we apply to all PCM data while it passes through */
+
     pa_bool_t muted:1;
 
     pa_bool_t refresh_volume:1;
@@ -101,12 +103,9 @@ struct pa_sink {
     pa_bool_t save_muted:1;
 
     pa_asyncmsgq *asyncmsgq;
-    pa_rtpoll *rtpoll;
 
     pa_memchunk silence;
 
-    pa_usec_t fixed_latency; /* for sinks with PA_SINK_DYNAMIC_LATENCY this is 0 */
-
     pa_hashmap *ports;
     pa_device_port *active_port;
 
@@ -155,9 +154,14 @@ struct pa_sink {
         pa_sink_state_t state;
         pa_hashmap *inputs;
 
+        pa_rtpoll *rtpoll;
+
         pa_cvolume soft_volume;
         pa_bool_t soft_muted:1;
 
+        /* The requested latency is used for dynamic latency
+         * sinks. For fixed latency sinks it is always identical to
+         * the fixed_latency. See below. */
         pa_bool_t requested_latency_valid:1;
         pa_usec_t requested_latency;
 
@@ -173,8 +177,15 @@ struct pa_sink {
         size_t rewind_nbytes;
         pa_bool_t rewind_requested;
 
+        /* Both dynamic and fixed latencies will be clamped to this
+         * range. */
         pa_usec_t min_latency; /* we won't go below this latency */
         pa_usec_t max_latency; /* An upper limit for the latencies */
+
+        /* 'Fixed' simply means that the latency is exclusively
+         * decided on by the sink, and the clients have no influence
+         * in changing it */
+        pa_usec_t fixed_latency; /* for sinks with PA_SINK_DYNAMIC_LATENCY this is 0 */
     } thread_info;
 
     void *userdata;
@@ -200,6 +211,8 @@ typedef enum pa_sink_message {
     PA_SINK_MESSAGE_DETACH,
     PA_SINK_MESSAGE_SET_LATENCY_RANGE,
     PA_SINK_MESSAGE_GET_LATENCY_RANGE,
+    PA_SINK_MESSAGE_SET_FIXED_LATENCY,
+    PA_SINK_MESSAGE_GET_FIXED_LATENCY,
     PA_SINK_MESSAGE_GET_MAX_REWIND,
     PA_SINK_MESSAGE_GET_MAX_REQUEST,
     PA_SINK_MESSAGE_SET_MAX_REWIND,
@@ -270,6 +283,8 @@ void pa_sink_set_soft_volume(pa_sink *s, const pa_cvolume *volume);
 void pa_sink_volume_changed(pa_sink *s, const pa_cvolume *new_volume);
 void pa_sink_mute_changed(pa_sink *s, pa_bool_t new_muted);
 
+void pa_sink_update_flags(pa_sink *s, pa_sink_flags_t mask, pa_sink_flags_t value);
+
 pa_bool_t pa_device_init_description(pa_proplist *p);
 pa_bool_t pa_device_init_icon(pa_proplist *p, pa_bool_t is_sink);
 pa_bool_t pa_device_init_intended_roles(pa_proplist *p);
@@ -280,6 +295,7 @@ pa_bool_t pa_device_init_intended_roles(pa_proplist *p);
 pa_usec_t pa_sink_get_latency(pa_sink *s);
 pa_usec_t pa_sink_get_requested_latency(pa_sink *s);
 void pa_sink_get_latency_range(pa_sink *s, pa_usec_t *min_latency, pa_usec_t *max_latency);
+pa_usec_t pa_sink_get_fixed_latency(pa_sink *s);
 
 size_t pa_sink_get_max_rewind(pa_sink *s);
 size_t pa_sink_get_max_request(pa_sink *s);
@@ -288,11 +304,8 @@ int pa_sink_update_status(pa_sink*s);
 int pa_sink_suspend(pa_sink *s, pa_bool_t suspend, pa_suspend_cause_t cause);
 int pa_sink_suspend_all(pa_core *c, pa_bool_t suspend, pa_suspend_cause_t cause);
 
-void pa_sink_update_flat_volume(pa_sink *s, pa_cvolume *new_volume);
-void pa_sink_propagate_flat_volume(pa_sink *s);
-
-void pa_sink_set_volume(pa_sink *sink, const pa_cvolume *volume, pa_bool_t propagate, pa_bool_t sendmsg, pa_bool_t become_reference, pa_bool_t save);
-const pa_cvolume *pa_sink_get_volume(pa_sink *sink, pa_bool_t force_refresh, pa_bool_t reference);
+void pa_sink_set_volume(pa_sink *sink, const pa_cvolume *volume, pa_bool_t sendmsg, pa_bool_t save);
+const pa_cvolume *pa_sink_get_volume(pa_sink *sink, pa_bool_t force_refresh);
 
 void pa_sink_set_mute(pa_sink *sink, pa_bool_t mute, pa_bool_t save);
 pa_bool_t pa_sink_get_mute(pa_sink *sink, pa_bool_t force_refresh);
@@ -331,16 +344,23 @@ void pa_sink_set_max_rewind_within_thread(pa_sink *s, size_t max_rewind);
 void pa_sink_set_max_request_within_thread(pa_sink *s, size_t max_request);
 
 void pa_sink_set_latency_range_within_thread(pa_sink *s, pa_usec_t min_latency, pa_usec_t max_latency);
+void pa_sink_set_fixed_latency_within_thread(pa_sink *s, pa_usec_t latency);
 
 /*** To be called exclusively by sink input drivers, from IO context */
 
 void pa_sink_request_rewind(pa_sink*s, size_t nbytes);
 
-void pa_sink_invalidate_requested_latency(pa_sink *s);
+void pa_sink_invalidate_requested_latency(pa_sink *s, pa_bool_t dynamic);
 
 pa_usec_t pa_sink_get_latency_within_thread(pa_sink *s);
 
 pa_device_port *pa_device_port_new(const char *name, const char *description, size_t extra);
 void pa_device_port_free(pa_device_port *p);
 
+/* Verify that we called in IO context (aka 'thread context), or that
+ * the sink is not yet set up, i.e. the thread not set up yet. See
+ * pa_assert_io_context() in thread-mq.h for more information. */
+#define pa_sink_assert_io_context(s) \
+    pa_assert(pa_thread_mq_get() || !PA_SINK_IS_LINKED((s)->state))
+
 #endif
index 4ba25ae482500fbefbe76748bce00f99b9e762de..3803a6cc502b23b76a7c041cb7a99b7052703496 100644 (file)
@@ -48,7 +48,7 @@ static void source_output_free(pa_object* mo);
 pa_source_output_new_data* pa_source_output_new_data_init(pa_source_output_new_data *data) {
     pa_assert(data);
 
-    memset(data, 0, sizeof(*data));
+    pa_zero(*data);
     data->resample_method = PA_RESAMPLER_INVALID;
     data->proplist = pa_proplist_new();
 
@@ -84,6 +84,7 @@ static void reset_callbacks(pa_source_output *o) {
     o->update_max_rewind = NULL;
     o->update_source_requested_latency = NULL;
     o->update_source_latency_range = NULL;
+    o->update_source_fixed_latency = NULL;
     o->attach = NULL;
     o->detach = NULL;
     o->suspend = NULL;
@@ -111,6 +112,7 @@ int pa_source_output_new(
     pa_assert(_o);
     pa_assert(core);
     pa_assert(data);
+    pa_assert_ctl_context();
 
     if (data->client)
         pa_proplist_update(data->proplist, PA_UPDATE_MERGE, data->client->proplist);
@@ -166,7 +168,7 @@ int pa_source_output_new(
     if ((r = pa_hook_fire(&core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_FIXATE], data)) < 0)
         return r;
 
-    if ((flags & PA_SOURCE_OUTPUT_FAIL_ON_SUSPEND) &&
+    if ((flags & PA_SOURCE_OUTPUT_NO_CREATE_ON_SUSPEND) &&
         pa_source_get_state(data->source) == PA_SOURCE_SUSPENDED) {
         pa_log("Failed to create source output: source is suspended.");
         return -PA_ERR_BADSTATE;
@@ -262,6 +264,7 @@ int pa_source_output_new(
 /* Called from main context */
 static void update_n_corked(pa_source_output *o, pa_source_output_state_t state) {
     pa_assert(o);
+    pa_assert_ctl_context();
 
     if (!o->source)
         return;
@@ -275,6 +278,7 @@ static void update_n_corked(pa_source_output *o, pa_source_output_state_t state)
 /* Called from main context */
 static void source_output_set_state(pa_source_output *o, pa_source_output_state_t state) {
     pa_assert(o);
+    pa_assert_ctl_context();
 
     if (o->state == state)
         return;
@@ -294,6 +298,7 @@ static void source_output_set_state(pa_source_output *o, pa_source_output_state_
 void pa_source_output_unlink(pa_source_output*o) {
     pa_bool_t linked;
     pa_assert(o);
+    pa_assert_ctl_context();
 
     /* See pa_sink_unlink() for a couple of comments how this function
      * works */
@@ -346,6 +351,7 @@ static void source_output_free(pa_object* mo) {
     pa_source_output *o = PA_SOURCE_OUTPUT(mo);
 
     pa_assert(o);
+    pa_assert_ctl_context();
     pa_assert(pa_source_output_refcnt(o) == 0);
 
     if (PA_SOURCE_OUTPUT_IS_LINKED(o->state))
@@ -371,7 +377,9 @@ static void source_output_free(pa_object* mo) {
 /* Called from main context */
 void pa_source_output_put(pa_source_output *o) {
     pa_source_output_state_t state;
+
     pa_source_output_assert_ref(o);
+    pa_assert_ctl_context();
 
     pa_assert(o->state == PA_SOURCE_OUTPUT_INIT);
 
@@ -395,6 +403,7 @@ void pa_source_output_put(pa_source_output *o) {
 /* Called from main context */
 void pa_source_output_kill(pa_source_output*o) {
     pa_source_output_assert_ref(o);
+    pa_assert_ctl_context();
     pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
 
     o->kill(o);
@@ -405,6 +414,7 @@ pa_usec_t pa_source_output_get_latency(pa_source_output *o, pa_usec_t *source_la
     pa_usec_t r[2] = { 0, 0 };
 
     pa_source_output_assert_ref(o);
+    pa_assert_ctl_context();
     pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
 
     pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_GET_LATENCY, r, 0, NULL) == 0);
@@ -424,6 +434,7 @@ void pa_source_output_push(pa_source_output *o, const pa_memchunk *chunk) {
     size_t limit, mbs = 0;
 
     pa_source_output_assert_ref(o);
+    pa_source_output_assert_io_context(o);
     pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->thread_info.state));
     pa_assert(chunk);
     pa_assert(pa_frame_aligned(chunk->length, &o->source->sample_spec));
@@ -499,8 +510,9 @@ void pa_source_output_push(pa_source_output *o, const pa_memchunk *chunk) {
 
 /* Called from thread context */
 void pa_source_output_process_rewind(pa_source_output *o, size_t nbytes /* in source sample spec */) {
-    pa_source_output_assert_ref(o);
 
+    pa_source_output_assert_ref(o);
+    pa_source_output_assert_io_context(o);
     pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->thread_info.state));
     pa_assert(pa_frame_aligned(nbytes, &o->source->sample_spec));
 
@@ -525,9 +537,18 @@ void pa_source_output_process_rewind(pa_source_output *o, size_t nbytes /* in so
         pa_memblockq_rewind(o->thread_info.delay_memblockq, nbytes);
 }
 
+/* Called from thread context */
+size_t pa_source_output_get_max_rewind(pa_source_output *o) {
+    pa_source_output_assert_ref(o);
+    pa_source_output_assert_io_context(o);
+
+    return o->thread_info.resampler ? pa_resampler_request(o->thread_info.resampler, o->source->thread_info.max_rewind) : o->source->thread_info.max_rewind;
+}
+
 /* Called from thread context */
 void pa_source_output_update_max_rewind(pa_source_output *o, size_t nbytes  /* in the source's sample spec */) {
     pa_source_output_assert_ref(o);
+    pa_source_output_assert_io_context(o);
     pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->thread_info.state));
     pa_assert(pa_frame_aligned(nbytes, &o->source->sample_spec));
 
@@ -538,15 +559,16 @@ void pa_source_output_update_max_rewind(pa_source_output *o, size_t nbytes  /* i
 /* Called from thread context */
 pa_usec_t pa_source_output_set_requested_latency_within_thread(pa_source_output *o, pa_usec_t usec) {
     pa_source_output_assert_ref(o);
+    pa_source_output_assert_io_context(o);
 
     if (!(o->source->flags & PA_SOURCE_DYNAMIC_LATENCY))
-        usec = o->source->fixed_latency;
+        usec = o->source->thread_info.fixed_latency;
 
     if (usec != (pa_usec_t) -1)
         usec = PA_CLAMP(usec, o->source->thread_info.min_latency, o->source->thread_info.max_latency);
 
     o->thread_info.requested_source_latency = usec;
-    pa_source_invalidate_requested_latency(o->source);
+    pa_source_invalidate_requested_latency(o->source, TRUE);
 
     return usec;
 }
@@ -554,6 +576,7 @@ pa_usec_t pa_source_output_set_requested_latency_within_thread(pa_source_output
 /* Called from main context */
 pa_usec_t pa_source_output_set_requested_latency(pa_source_output *o, pa_usec_t usec) {
     pa_source_output_assert_ref(o);
+    pa_assert_ctl_context();
 
     if (PA_SOURCE_OUTPUT_IS_LINKED(o->state) && o->source) {
         pa_assert_se(pa_asyncmsgq_send(o->source->asyncmsgq, PA_MSGOBJECT(o), PA_SOURCE_OUTPUT_MESSAGE_SET_REQUESTED_LATENCY, &usec, 0, NULL) == 0);
@@ -565,7 +588,7 @@ pa_usec_t pa_source_output_set_requested_latency(pa_source_output *o, pa_usec_t
 
     if (o->source) {
         if (!(o->source->flags & PA_SOURCE_DYNAMIC_LATENCY))
-            usec = o->source->fixed_latency;
+            usec = pa_source_get_fixed_latency(o->source);
 
         if (usec != (pa_usec_t) -1) {
             pa_usec_t min_latency, max_latency;
@@ -582,6 +605,7 @@ pa_usec_t pa_source_output_set_requested_latency(pa_source_output *o, pa_usec_t
 /* Called from main context */
 pa_usec_t pa_source_output_get_requested_latency(pa_source_output *o) {
     pa_source_output_assert_ref(o);
+    pa_assert_ctl_context();
 
     if (PA_SOURCE_OUTPUT_IS_LINKED(o->state) && o->source) {
         pa_usec_t usec = 0;
@@ -598,6 +622,7 @@ pa_usec_t pa_source_output_get_requested_latency(pa_source_output *o) {
 /* Called from main context */
 void pa_source_output_cork(pa_source_output *o, pa_bool_t b) {
     pa_source_output_assert_ref(o);
+    pa_assert_ctl_context();
     pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
 
     source_output_set_state(o, b ? PA_SOURCE_OUTPUT_CORKED : PA_SOURCE_OUTPUT_RUNNING);
@@ -606,6 +631,7 @@ void pa_source_output_cork(pa_source_output *o, pa_bool_t b) {
 /* Called from main context */
 int pa_source_output_set_rate(pa_source_output *o, uint32_t rate) {
     pa_source_output_assert_ref(o);
+    pa_assert_ctl_context();
     pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
     pa_return_val_if_fail(o->thread_info.resampler, -PA_ERR_BADSTATE);
 
@@ -623,6 +649,7 @@ int pa_source_output_set_rate(pa_source_output *o, uint32_t rate) {
 /* Called from main context */
 void pa_source_output_set_name(pa_source_output *o, const char *name) {
     const char *old;
+    pa_assert_ctl_context();
     pa_source_output_assert_ref(o);
 
     if (!name && !pa_proplist_contains(o->proplist, PA_PROP_MEDIA_NAME))
@@ -647,11 +674,12 @@ void pa_source_output_set_name(pa_source_output *o, const char *name) {
 /* Called from main thread */
 void pa_source_output_update_proplist(pa_source_output *o, pa_update_mode_t mode, pa_proplist *p) {
     pa_source_output_assert_ref(o);
+    pa_assert_ctl_context();
 
     if (p)
         pa_proplist_update(o->proplist, mode, p);
 
-    if (PA_SINK_IS_LINKED(o->state)) {
+    if (PA_SOURCE_OUTPUT_IS_LINKED(o->state)) {
         pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_PROPLIST_CHANGED], o);
         pa_subscription_post(o->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
     }
@@ -660,6 +688,7 @@ void pa_source_output_update_proplist(pa_source_output *o, pa_update_mode_t mode
 /* Called from main context */
 pa_resample_method_t pa_source_output_get_resample_method(pa_source_output *o) {
     pa_source_output_assert_ref(o);
+    pa_assert_ctl_context();
 
     return o->actual_resample_method;
 }
@@ -667,6 +696,7 @@ pa_resample_method_t pa_source_output_get_resample_method(pa_source_output *o) {
 /* Called from main context */
 pa_bool_t pa_source_output_may_move(pa_source_output *o) {
     pa_source_output_assert_ref(o);
+    pa_assert_ctl_context();
     pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
 
     if (o->flags & PA_SOURCE_OUTPUT_DONT_MOVE)
@@ -708,6 +738,7 @@ int pa_source_output_start_move(pa_source_output *o) {
     int r;
 
     pa_source_output_assert_ref(o);
+    pa_assert_ctl_context();
     pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
     pa_assert(o->source);
 
@@ -739,6 +770,7 @@ int pa_source_output_finish_move(pa_source_output *o, pa_source *dest, pa_bool_t
     pa_resampler *new_resampler;
 
     pa_source_output_assert_ref(o);
+    pa_assert_ctl_context();
     pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
     pa_assert(!o->source);
     pa_source_assert_ref(dest);
@@ -815,11 +847,30 @@ int pa_source_output_finish_move(pa_source_output *o, pa_source *dest, pa_bool_t
     return 0;
 }
 
+/* Called from main context */
+void pa_source_output_fail_move(pa_source_output *o) {
+
+    pa_source_output_assert_ref(o);
+    pa_assert_ctl_context();
+    pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
+    pa_assert(!o->source);
+
+    /* Check if someone wants this source output? */
+    if (pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FAIL], o) == PA_HOOK_STOP)
+        return;
+
+    if (o->moving)
+        o->moving(o, NULL);
+
+    pa_source_output_kill(o);
+}
+
 /* Called from main context */
 int pa_source_output_move_to(pa_source_output *o, pa_source *dest, pa_bool_t save) {
     int r;
 
     pa_source_output_assert_ref(o);
+    pa_assert_ctl_context();
     pa_assert(PA_SOURCE_OUTPUT_IS_LINKED(o->state));
     pa_assert(o->source);
     pa_source_assert_ref(dest);
@@ -838,6 +889,7 @@ int pa_source_output_move_to(pa_source_output *o, pa_source *dest, pa_bool_t sav
     }
 
     if ((r = pa_source_output_finish_move(o, dest, save)) < 0) {
+        pa_source_output_fail_move(o);
         pa_source_output_unref(o);
         return r;
     }
@@ -850,6 +902,7 @@ int pa_source_output_move_to(pa_source_output *o, pa_source *dest, pa_bool_t sav
 /* Called from IO thread context */
 void pa_source_output_set_state_within_thread(pa_source_output *o, pa_source_output_state_t state) {
     pa_source_output_assert_ref(o);
+    pa_source_output_assert_io_context(o);
 
     if (state == o->thread_info.state)
         return;
@@ -906,11 +959,13 @@ int pa_source_output_process_msg(pa_msgobject *mo, int code, void *userdata, int
     return -PA_ERR_NOTIMPLEMENTED;
 }
 
+/* Called from main context */
 void pa_source_output_send_event(pa_source_output *o, const char *event, pa_proplist *data) {
     pa_proplist *pl = NULL;
     pa_source_output_send_event_hook_data hook_data;
 
     pa_source_output_assert_ref(o);
+    pa_assert_ctl_context();
     pa_assert(event);
 
     if (!o->send_event)
index 9824e160d1dbbf2120682fb2686e876bf8d9db29..a70a3fdbc29379669235311b1e07f75a5514d08a 100644 (file)
@@ -55,7 +55,8 @@ typedef enum pa_source_output_flags {
     PA_SOURCE_OUTPUT_FIX_RATE = 64,
     PA_SOURCE_OUTPUT_FIX_CHANNELS = 128,
     PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND = 256,
-    PA_SOURCE_OUTPUT_FAIL_ON_SUSPEND = 512
+    PA_SOURCE_OUTPUT_NO_CREATE_ON_SUSPEND = 512,
+    PA_SOURCE_OUTPUT_KILL_ON_SUSPEND = 1024
 } pa_source_output_flags_t;
 
 struct pa_source_output {
@@ -108,6 +109,10 @@ struct pa_source_output {
      * from IO context. */
     void (*update_source_latency_range) (pa_source_output *o); /* may be NULL */
 
+    /* Called whenver the fixed latency of the source changes, if there
+     * is one. Called from IO context. */
+    void (*update_source_fixed_latency) (pa_source_output *i); /* may be NULL */
+
     /* If non-NULL this function is called when the output is first
      * connected to a source. Called from IO thread context */
     void (*attach) (pa_source_output *o);           /* may be NULL */
@@ -127,7 +132,9 @@ struct pa_source_output {
     /* If non-NULL called whenever the source output is moved to a new
      * source. Called from main context after the stream was detached
      * from the old source and before it is attached to the new
-     * source. */
+     * source. If dest is NULL the move was executed in two
+     * phases and the second one failed; the stream will be destroyed
+     * after this call. */
     void (*moving) (pa_source_output *o, pa_source *dest);   /* may be NULL */
 
     /* Supposed to unlink and destroy this stream. Called from main
@@ -238,6 +245,8 @@ void pa_source_output_cork(pa_source_output *o, pa_bool_t b);
 
 int pa_source_output_set_rate(pa_source_output *o, uint32_t rate);
 
+size_t pa_source_output_get_max_rewind(pa_source_output *o);
+
 /* Callable by everyone */
 
 /* External code may request disconnection with this funcion */
@@ -260,6 +269,7 @@ int pa_source_output_move_to(pa_source_output *o, pa_source *dest, pa_bool_t sav
  * new source */
 int pa_source_output_start_move(pa_source_output *o);
 int pa_source_output_finish_move(pa_source_output *o, pa_source *dest, pa_bool_t save);
+void pa_source_output_fail_move(pa_source_output *o);
 
 #define pa_source_output_get_state(o) ((o)->state)
 
@@ -277,4 +287,7 @@ int pa_source_output_process_msg(pa_msgobject *mo, int code, void *userdata, int
 
 pa_usec_t pa_source_output_set_requested_latency_within_thread(pa_source_output *o, pa_usec_t usec);
 
+#define pa_source_output_assert_io_context(s) \
+    pa_assert(pa_thread_mq_get() || !PA_SOURCE_OUTPUT_IS_LINKED((s)->state))
+
 #endif
index b8af148fb3ef46e809fb4c6438c61a2a93c8660c..8aa07f5e58c805873aa9bb1efc865a90975a0b30 100644 (file)
@@ -33,6 +33,7 @@
 #include <pulse/timeval.h>
 #include <pulse/util.h>
 
+#include <pulsecore/core-util.h>
 #include <pulsecore/source-output.h>
 #include <pulsecore/namereg.h>
 #include <pulsecore/core-subscribe.h>
@@ -52,7 +53,7 @@ static void source_free(pa_object *o);
 pa_source_new_data* pa_source_new_data_init(pa_source_new_data *data) {
     pa_assert(data);
 
-    memset(data, 0, sizeof(*data));
+    pa_zero(*data);
     data->proplist = pa_proplist_new();
 
     return data;
@@ -145,6 +146,7 @@ pa_source* pa_source_new(
     pa_assert(core);
     pa_assert(data);
     pa_assert(data->name);
+    pa_assert_ctl_context();
 
     s = pa_msgobject_new(pa_source);
 
@@ -179,7 +181,7 @@ pa_source* pa_source_new(
         pa_cvolume_reset(&data->volume, data->sample_spec.channels);
 
     pa_return_null_if_fail(pa_cvolume_valid(&data->volume));
-    pa_return_null_if_fail(data->volume.channels == data->sample_spec.channels);
+    pa_return_null_if_fail(pa_cvolume_compatible(&data->volume, &data->sample_spec));
 
     if (!data->muted_is_set)
         data->muted = FALSE;
@@ -217,20 +219,17 @@ pa_source* pa_source_new(
     s->n_corked = 0;
     s->monitor_of = NULL;
 
-    s->virtual_volume = data->volume;
+    s->volume = data->volume;
     pa_cvolume_reset(&s->soft_volume, s->sample_spec.channels);
     s->base_volume = PA_VOLUME_NORM;
     s->n_volume_steps = PA_VOLUME_NORM+1;
     s->muted = data->muted;
     s->refresh_volume = s->refresh_muted = FALSE;
 
-    s->fixed_latency = flags & PA_SOURCE_DYNAMIC_LATENCY ? 0 : DEFAULT_FIXED_LATENCY;
-
     reset_callbacks(s);
     s->userdata = NULL;
 
     s->asyncmsgq = NULL;
-    s->rtpoll = NULL;
 
     /* As a minor optimization we just steal the list instead of
      * copying it here */
@@ -263,6 +262,7 @@ pa_source* pa_source_new(
             &s->sample_spec,
             0);
 
+    s->thread_info.rtpoll = NULL;
     s->thread_info.outputs = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
     s->thread_info.soft_volume = s->soft_volume;
     s->thread_info.soft_muted = s->muted;
@@ -272,6 +272,7 @@ pa_source* pa_source_new(
     s->thread_info.requested_latency = 0;
     s->thread_info.min_latency = ABSOLUTE_MIN_LATENCY;
     s->thread_info.max_latency = ABSOLUTE_MAX_LATENCY;
+    s->thread_info.fixed_latency = flags & PA_SOURCE_DYNAMIC_LATENCY ? 0 : DEFAULT_FIXED_LATENCY;
 
     pa_assert_se(pa_idxset_put(core->sources, s, &s->index) >= 0);
 
@@ -297,6 +298,7 @@ static int source_set_state(pa_source *s, pa_source_state_t state) {
     pa_source_state_t original_state;
 
     pa_assert(s);
+    pa_assert_ctl_context();
 
     if (s->state == state)
         return 0;
@@ -333,27 +335,26 @@ static int source_set_state(pa_source *s, pa_source_state_t state) {
 
         /* We're suspending or resuming, tell everyone about it */
 
-        for (o = PA_SOURCE_OUTPUT(pa_idxset_first(s->outputs, &idx)); o; o = PA_SOURCE_OUTPUT(pa_idxset_next(s->outputs, &idx)))
+        PA_IDXSET_FOREACH(o, s->outputs, idx)
             if (s->state == PA_SOURCE_SUSPENDED &&
-                (o->flags & PA_SOURCE_OUTPUT_FAIL_ON_SUSPEND))
+                (o->flags & PA_SOURCE_OUTPUT_KILL_ON_SUSPEND))
                 pa_source_output_kill(o);
             else if (o->suspend)
                 o->suspend(o, state == PA_SOURCE_SUSPENDED);
     }
 
-
     return 0;
 }
 
 /* Called from main context */
 void pa_source_put(pa_source *s) {
     pa_source_assert_ref(s);
+    pa_assert_ctl_context();
 
     pa_assert(s->state == PA_SOURCE_INIT);
 
     /* The following fields must be initialized properly when calling _put() */
     pa_assert(s->asyncmsgq);
-    pa_assert(s->rtpoll);
     pa_assert(s->thread_info.min_latency <= s->thread_info.max_latency);
 
     /* Generally, flags should be initialized via pa_source_new(). As
@@ -368,7 +369,7 @@ void pa_source_put(pa_source *s) {
 
     pa_assert((s->flags & PA_SOURCE_HW_VOLUME_CTRL) || (s->base_volume == PA_VOLUME_NORM && s->flags & PA_SOURCE_DECIBEL_VOLUME));
     pa_assert(!(s->flags & PA_SOURCE_DECIBEL_VOLUME) || s->n_volume_steps == PA_VOLUME_NORM+1);
-    pa_assert(!(s->flags & PA_SOURCE_DYNAMIC_LATENCY) == (s->fixed_latency != 0));
+    pa_assert(!(s->flags & PA_SOURCE_DYNAMIC_LATENCY) == (s->thread_info.fixed_latency != 0));
 
     pa_assert_se(source_set_state(s, PA_SOURCE_IDLE) == 0);
 
@@ -382,6 +383,7 @@ void pa_source_unlink(pa_source *s) {
     pa_source_output *o, *j = NULL;
 
     pa_assert(s);
+    pa_assert_ctl_context();
 
     /* See pa_sink_unlink() for a couple of comments how this function
      * works. */
@@ -423,6 +425,7 @@ static void source_free(pa_object *o) {
     pa_source *s = PA_SOURCE(o);
 
     pa_assert(s);
+    pa_assert_ctl_context();
     pa_assert(pa_source_refcnt(s) == 0);
 
     if (PA_SOURCE_IS_LINKED(s->state))
@@ -458,23 +461,40 @@ static void source_free(pa_object *o) {
     pa_xfree(s);
 }
 
-/* Called from main context */
+/* Called from main context, and not while the IO thread is active, please */
 void pa_source_set_asyncmsgq(pa_source *s, pa_asyncmsgq *q) {
     pa_source_assert_ref(s);
+    pa_assert_ctl_context();
 
     s->asyncmsgq = q;
 }
 
-/* Called from main context */
+/* Called from main context, and not while the IO thread is active, please */
+void pa_source_update_flags(pa_source *s, pa_source_flags_t mask, pa_source_flags_t value) {
+    pa_source_assert_ref(s);
+    pa_assert_ctl_context();
+
+    if (mask == 0)
+        return;
+
+    /* For now, allow only a minimal set of flags to be changed. */
+    pa_assert((mask & ~(PA_SOURCE_DYNAMIC_LATENCY|PA_SOURCE_LATENCY)) == 0);
+
+    s->flags = (s->flags & ~mask) | (value & mask);
+}
+
+/* Called from IO context, or before _put() from main context */
 void pa_source_set_rtpoll(pa_source *s, pa_rtpoll *p) {
     pa_source_assert_ref(s);
+    pa_source_assert_io_context(s);
 
-    s->rtpoll = p;
+    s->thread_info.rtpoll = p;
 }
 
 /* Called from main context */
 int pa_source_update_status(pa_source*s) {
     pa_source_assert_ref(s);
+    pa_assert_ctl_context();
     pa_assert(PA_SOURCE_IS_LINKED(s->state));
 
     if (s->state == PA_SOURCE_SUSPENDED)
@@ -486,6 +506,7 @@ int pa_source_update_status(pa_source*s) {
 /* Called from main context */
 int pa_source_suspend(pa_source *s, pa_bool_t suspend, pa_suspend_cause_t cause) {
     pa_source_assert_ref(s);
+    pa_assert_ctl_context();
     pa_assert(PA_SOURCE_IS_LINKED(s->state));
     pa_assert(cause != 0);
 
@@ -513,6 +534,7 @@ int pa_source_sync_suspend(pa_source *s) {
     pa_sink_state_t state;
 
     pa_source_assert_ref(s);
+    pa_assert_ctl_context();
     pa_assert(PA_SOURCE_IS_LINKED(s->state));
     pa_assert(s->monitor_of);
 
@@ -532,6 +554,7 @@ pa_queue *pa_source_move_all_start(pa_source *s, pa_queue *q) {
     uint32_t idx;
 
     pa_source_assert_ref(s);
+    pa_assert_ctl_context();
     pa_assert(PA_SOURCE_IS_LINKED(s->state));
 
     if (!q)
@@ -556,12 +579,13 @@ void pa_source_move_all_finish(pa_source *s, pa_queue *q, pa_bool_t save) {
     pa_source_output *o;
 
     pa_source_assert_ref(s);
+    pa_assert_ctl_context();
     pa_assert(PA_SOURCE_IS_LINKED(s->state));
     pa_assert(q);
 
     while ((o = PA_SOURCE_OUTPUT(pa_queue_pop(q)))) {
         if (pa_source_output_finish_move(o, s, save) < 0)
-            pa_source_output_kill(o);
+            pa_source_output_fail_move(o);
 
         pa_source_output_unref(o);
     }
@@ -572,13 +596,13 @@ void pa_source_move_all_finish(pa_source *s, pa_queue *q, pa_bool_t save) {
 /* Called from main context */
 void pa_source_move_all_fail(pa_queue *q) {
     pa_source_output *o;
+
+    pa_assert_ctl_context();
     pa_assert(q);
 
     while ((o = PA_SOURCE_OUTPUT(pa_queue_pop(q)))) {
-        if (pa_hook_fire(&o->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FAIL], o) == PA_HOOK_OK) {
-            pa_source_output_kill(o);
-            pa_source_output_unref(o);
-        }
+        pa_source_output_fail_move(o);
+        pa_source_output_unref(o);
     }
 
     pa_queue_free(q, NULL, NULL);
@@ -590,6 +614,7 @@ void pa_source_process_rewind(pa_source *s, size_t nbytes) {
     void *state = NULL;
 
     pa_source_assert_ref(s);
+    pa_source_assert_io_context(s);
     pa_assert(PA_SOURCE_IS_LINKED(s->thread_info.state));
 
     if (nbytes <= 0)
@@ -612,6 +637,7 @@ void pa_source_post(pa_source*s, const pa_memchunk *chunk) {
     void *state = NULL;
 
     pa_source_assert_ref(s);
+    pa_source_assert_io_context(s);
     pa_assert(PA_SOURCE_IS_LINKED(s->thread_info.state));
     pa_assert(chunk);
 
@@ -651,6 +677,7 @@ void pa_source_post(pa_source*s, const pa_memchunk *chunk) {
 /* Called from IO thread context */
 void pa_source_post_direct(pa_source*s, pa_source_output *o, const pa_memchunk *chunk) {
     pa_source_assert_ref(s);
+    pa_source_assert_io_context(s);
     pa_assert(PA_SOURCE_IS_LINKED(s->thread_info.state));
     pa_source_output_assert_ref(o);
     pa_assert(o->thread_info.direct_on_input);
@@ -682,6 +709,7 @@ pa_usec_t pa_source_get_latency(pa_source *s) {
     pa_usec_t usec;
 
     pa_source_assert_ref(s);
+    pa_assert_ctl_context();
     pa_assert(PA_SOURCE_IS_LINKED(s->state));
 
     if (s->state == PA_SOURCE_SUSPENDED)
@@ -701,6 +729,7 @@ pa_usec_t pa_source_get_latency_within_thread(pa_source *s) {
     pa_msgobject *o;
 
     pa_source_assert_ref(s);
+    pa_source_assert_io_context(s);
     pa_assert(PA_SOURCE_IS_LINKED(s->thread_info.state));
 
     /* The returned value is supposed to be in the time domain of the sound card! */
@@ -722,76 +751,88 @@ pa_usec_t pa_source_get_latency_within_thread(pa_source *s) {
 }
 
 /* Called from main thread */
-void pa_source_set_volume(pa_source *s, const pa_cvolume *volume, pa_bool_t save) {
-    pa_cvolume old_virtual_volume;
-    pa_bool_t virtual_volume_changed;
+void pa_source_set_volume(
+        pa_source *s,
+        const pa_cvolume *volume,
+        pa_bool_t save) {
+
+    pa_bool_t real_changed;
 
     pa_source_assert_ref(s);
+    pa_assert_ctl_context();
     pa_assert(PA_SOURCE_IS_LINKED(s->state));
-    pa_assert(volume);
     pa_assert(pa_cvolume_valid(volume));
     pa_assert(pa_cvolume_compatible(volume, &s->sample_spec));
 
-    old_virtual_volume = s->virtual_volume;
-    s->virtual_volume = *volume;
-    virtual_volume_changed = !pa_cvolume_equal(&old_virtual_volume, &s->virtual_volume);
-    s->save_volume = (!virtual_volume_changed && s->save_volume) || save;
+    real_changed = !pa_cvolume_equal(volume, &s->volume);
+    s->volume = *volume;
+    s->save_volume = (!real_changed && s->save_volume) || save;
 
     if (s->set_volume) {
         pa_cvolume_reset(&s->soft_volume, s->sample_spec.channels);
         s->set_volume(s);
     } else
-        s->soft_volume = s->virtual_volume;
+        s->soft_volume = s->volume;
 
     pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_VOLUME, NULL, 0, NULL) == 0);
 
-    if (virtual_volume_changed)
+    if (real_changed)
         pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
 }
 
 /* Called from main thread. Only to be called by source implementor */
 void pa_source_set_soft_volume(pa_source *s, const pa_cvolume *volume) {
     pa_source_assert_ref(s);
-    pa_assert(volume);
+    pa_assert_ctl_context();
+
+    if (!volume)
+        pa_cvolume_reset(&s->soft_volume, s->sample_spec.channels);
+    else
+        s->soft_volume = *volume;
 
     if (PA_SOURCE_IS_LINKED(s->state))
         pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_VOLUME, NULL, 0, NULL) == 0);
     else
-        s->thread_info.soft_volume = *volume;
+        s->thread_info.soft_volume = s->soft_volume;
 }
 
 /* Called from main thread */
 const pa_cvolume *pa_source_get_volume(pa_source *s, pa_bool_t force_refresh) {
     pa_source_assert_ref(s);
+    pa_assert_ctl_context();
     pa_assert(PA_SOURCE_IS_LINKED(s->state));
 
     if (s->refresh_volume || force_refresh) {
-        pa_cvolume old_virtual_volume = s->virtual_volume;
+        pa_cvolume old_volume;
+
+        old_volume = s->volume;
 
         if (s->get_volume)
             s->get_volume(s);
 
         pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_VOLUME, NULL, 0, NULL) == 0);
 
-        if (!pa_cvolume_equal(&old_virtual_volume, &s->virtual_volume)) {
+        if (!pa_cvolume_equal(&old_volume, &s->volume)) {
             s->save_volume = TRUE;
             pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
         }
     }
 
-    return &s->virtual_volume;
+    return &s->volume;
 }
 
 /* Called from main thread */
 void pa_source_volume_changed(pa_source *s, const pa_cvolume *new_volume) {
     pa_source_assert_ref(s);
+    pa_assert_ctl_context();
+    pa_assert(PA_SOURCE_IS_LINKED(s->state));
 
     /* The source implementor may call this if the volume changed to make sure everyone is notified */
 
-    if (pa_cvolume_equal(&s->virtual_volume, new_volume))
+    if (pa_cvolume_equal(&s->volume, new_volume))
         return;
 
-    s->virtual_volume = *new_volume;
+    s->volume = *new_volume;
     s->save_volume = TRUE;
 
     pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, s->index);
@@ -802,6 +843,7 @@ void pa_source_set_mute(pa_source *s, pa_bool_t mute, pa_bool_t save) {
     pa_bool_t old_muted;
 
     pa_source_assert_ref(s);
+    pa_assert_ctl_context();
     pa_assert(PA_SOURCE_IS_LINKED(s->state));
 
     old_muted = s->muted;
@@ -820,6 +862,7 @@ void pa_source_set_mute(pa_source *s, pa_bool_t mute, pa_bool_t save) {
 /* Called from main thread */
 pa_bool_t pa_source_get_mute(pa_source *s, pa_bool_t force_refresh) {
     pa_source_assert_ref(s);
+    pa_assert_ctl_context();
     pa_assert(PA_SOURCE_IS_LINKED(s->state));
 
     if (s->refresh_muted || force_refresh) {
@@ -846,6 +889,8 @@ pa_bool_t pa_source_get_mute(pa_source *s, pa_bool_t force_refresh) {
 /* Called from main thread */
 void pa_source_mute_changed(pa_source *s, pa_bool_t new_muted) {
     pa_source_assert_ref(s);
+    pa_assert_ctl_context();
+    pa_assert(PA_SOURCE_IS_LINKED(s->state));
 
     /* The source implementor may call this if the mute state changed to make sure everyone is notified */
 
@@ -861,6 +906,7 @@ void pa_source_mute_changed(pa_source *s, pa_bool_t new_muted) {
 /* Called from main thread */
 pa_bool_t pa_source_update_proplist(pa_source *s, pa_update_mode_t mode, pa_proplist *p) {
     pa_source_assert_ref(s);
+    pa_assert_ctl_context();
 
     if (p)
         pa_proplist_update(s->proplist, mode, p);
@@ -874,16 +920,18 @@ pa_bool_t pa_source_update_proplist(pa_source *s, pa_update_mode_t mode, pa_prop
 }
 
 /* Called from main thread */
+/* FIXME -- this should be dropped and be merged into pa_source_update_proplist() */
 void pa_source_set_description(pa_source *s, const char *description) {
     const char *old;
     pa_source_assert_ref(s);
+    pa_assert_ctl_context();
 
     if (!description && !pa_proplist_contains(s->proplist, PA_PROP_DEVICE_DESCRIPTION))
         return;
 
     old = pa_proplist_gets(s->proplist, PA_PROP_DEVICE_DESCRIPTION);
 
-    if (old && description && !strcmp(old, description))
+    if (old && description && pa_streq(old, description))
         return;
 
     if (description)
@@ -901,6 +949,7 @@ void pa_source_set_description(pa_source *s, const char *description) {
 unsigned pa_source_linked_by(pa_source *s) {
     pa_source_assert_ref(s);
     pa_assert(PA_SOURCE_IS_LINKED(s->state));
+    pa_assert_ctl_context();
 
     return pa_idxset_size(s->outputs);
 }
@@ -911,6 +960,7 @@ unsigned pa_source_used_by(pa_source *s) {
 
     pa_source_assert_ref(s);
     pa_assert(PA_SOURCE_IS_LINKED(s->state));
+    pa_assert_ctl_context();
 
     ret = pa_idxset_size(s->outputs);
     pa_assert(ret >= s->n_corked);
@@ -925,6 +975,7 @@ unsigned pa_source_check_suspend(pa_source *s) {
     uint32_t idx;
 
     pa_source_assert_ref(s);
+    pa_assert_ctl_context();
 
     if (!PA_SOURCE_IS_LINKED(s->state))
         return 0;
@@ -1006,7 +1057,7 @@ int pa_source_process_msg(pa_msgobject *object, int code, void *userdata, int64_
             if (pa_hashmap_remove(s->thread_info.outputs, PA_UINT32_TO_PTR(o->index)))
                 pa_source_output_unref(o);
 
-            pa_source_invalidate_requested_latency(s);
+            pa_source_invalidate_requested_latency(s, TRUE);
 
             return 0;
         }
@@ -1086,6 +1137,16 @@ int pa_source_process_msg(pa_msgobject *object, int code, void *userdata, int64_
             return 0;
         }
 
+        case PA_SOURCE_MESSAGE_GET_FIXED_LATENCY:
+
+            *((pa_usec_t*) userdata) = s->thread_info.fixed_latency;
+            return 0;
+
+        case PA_SOURCE_MESSAGE_SET_FIXED_LATENCY:
+
+            pa_source_set_fixed_latency_within_thread(s, (pa_usec_t) offset);
+            return 0;
+
         case PA_SOURCE_MESSAGE_GET_MAX_REWIND:
 
             *((size_t*) userdata) = s->thread_info.max_rewind;
@@ -1120,6 +1181,7 @@ int pa_source_suspend_all(pa_core *c, pa_bool_t suspend, pa_suspend_cause_t caus
     int ret = 0;
 
     pa_core_assert_ref(c);
+    pa_assert_ctl_context();
     pa_assert(cause != 0);
 
     for (source = PA_SOURCE(pa_idxset_first(c->sources, &idx)); source; source = PA_SOURCE(pa_idxset_next(c->sources, &idx))) {
@@ -1138,6 +1200,7 @@ int pa_source_suspend_all(pa_core *c, pa_bool_t suspend, pa_suspend_cause_t caus
 /* Called from main thread */
 void pa_source_detach(pa_source *s) {
     pa_source_assert_ref(s);
+    pa_assert_ctl_context();
     pa_assert(PA_SOURCE_IS_LINKED(s->state));
 
     pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_DETACH, NULL, 0, NULL) == 0);
@@ -1146,6 +1209,7 @@ void pa_source_detach(pa_source *s) {
 /* Called from main thread */
 void pa_source_attach(pa_source *s) {
     pa_source_assert_ref(s);
+    pa_assert_ctl_context();
     pa_assert(PA_SOURCE_IS_LINKED(s->state));
 
     pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_ATTACH, NULL, 0, NULL) == 0);
@@ -1157,9 +1221,10 @@ void pa_source_detach_within_thread(pa_source *s) {
     void *state = NULL;
 
     pa_source_assert_ref(s);
+    pa_source_assert_io_context(s);
     pa_assert(PA_SOURCE_IS_LINKED(s->thread_info.state));
 
-    while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
+    PA_HASHMAP_FOREACH(o, s->thread_info.outputs, state)
         if (o->detach)
             o->detach(o);
 }
@@ -1170,9 +1235,10 @@ void pa_source_attach_within_thread(pa_source *s) {
     void *state = NULL;
 
     pa_source_assert_ref(s);
+    pa_source_assert_io_context(s);
     pa_assert(PA_SOURCE_IS_LINKED(s->thread_info.state));
 
-    while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
+    PA_HASHMAP_FOREACH(o, s->thread_info.outputs, state)
         if (o->attach)
             o->attach(o);
 }
@@ -1184,15 +1250,15 @@ pa_usec_t pa_source_get_requested_latency_within_thread(pa_source *s) {
     void *state = NULL;
 
     pa_source_assert_ref(s);
+    pa_source_assert_io_context(s);
 
     if (!(s->flags & PA_SOURCE_DYNAMIC_LATENCY))
-        return PA_CLAMP(s->fixed_latency, s->thread_info.min_latency, s->thread_info.max_latency);
+        return PA_CLAMP(s->thread_info.fixed_latency, s->thread_info.min_latency, s->thread_info.max_latency);
 
     if (s->thread_info.requested_latency_valid)
         return s->thread_info.requested_latency;
 
-    while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
-
+    PA_HASHMAP_FOREACH(o, s->thread_info.outputs, state)
         if (o->thread_info.requested_source_latency != (pa_usec_t) -1 &&
             (result == (pa_usec_t) -1 || result > o->thread_info.requested_source_latency))
             result = o->thread_info.requested_source_latency;
@@ -1214,6 +1280,7 @@ pa_usec_t pa_source_get_requested_latency(pa_source *s) {
     pa_usec_t usec = 0;
 
     pa_source_assert_ref(s);
+    pa_assert_ctl_context();
     pa_assert(PA_SOURCE_IS_LINKED(s->state));
 
     if (s->state == PA_SOURCE_SUSPENDED)
@@ -1230,21 +1297,22 @@ void pa_source_set_max_rewind_within_thread(pa_source *s, size_t max_rewind) {
     void *state = NULL;
 
     pa_source_assert_ref(s);
+    pa_source_assert_io_context(s);
 
     if (max_rewind == s->thread_info.max_rewind)
         return;
 
     s->thread_info.max_rewind = max_rewind;
 
-    if (PA_SOURCE_IS_LINKED(s->thread_info.state)) {
-        while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
+    if (PA_SOURCE_IS_LINKED(s->thread_info.state))
+        PA_HASHMAP_FOREACH(o, s->thread_info.outputs, state)
             pa_source_output_update_max_rewind(o, s->thread_info.max_rewind);
-    }
 }
 
 /* Called from main thread */
 void pa_source_set_max_rewind(pa_source *s, size_t max_rewind) {
     pa_source_assert_ref(s);
+    pa_assert_ctl_context();
 
     if (PA_SOURCE_IS_LINKED(s->state))
         pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_MAX_REWIND, NULL, max_rewind, NULL) == 0);
@@ -1253,17 +1321,18 @@ void pa_source_set_max_rewind(pa_source *s, size_t max_rewind) {
 }
 
 /* Called from IO thread */
-void pa_source_invalidate_requested_latency(pa_source *s) {
+void pa_source_invalidate_requested_latency(pa_source *s, pa_bool_t dynamic) {
     pa_source_output *o;
     void *state = NULL;
 
     pa_source_assert_ref(s);
+    pa_source_assert_io_context(s);
 
-    if (!(s->flags & PA_SOURCE_DYNAMIC_LATENCY))
+    if ((s->flags & PA_SOURCE_DYNAMIC_LATENCY))
+        s->thread_info.requested_latency_valid = FALSE;
+    else if (dynamic)
         return;
 
-    s->thread_info.requested_latency_valid = FALSE;
-
     if (PA_SOURCE_IS_LINKED(s->thread_info.state)) {
 
         if (s->update_requested_latency)
@@ -1275,12 +1344,13 @@ void pa_source_invalidate_requested_latency(pa_source *s) {
     }
 
     if (s->monitor_of)
-        pa_sink_invalidate_requested_latency(s->monitor_of);
+        pa_sink_invalidate_requested_latency(s->monitor_of, dynamic);
 }
 
 /* Called from main thread */
 void pa_source_set_latency_range(pa_source *s, pa_usec_t min_latency, pa_usec_t max_latency) {
     pa_source_assert_ref(s);
+    pa_assert_ctl_context();
 
     /* min_latency == 0:           no limit
      * min_latency anything else:  specified limit
@@ -1315,6 +1385,7 @@ void pa_source_set_latency_range(pa_source *s, pa_usec_t min_latency, pa_usec_t
 /* Called from main thread */
 void pa_source_get_latency_range(pa_source *s, pa_usec_t *min_latency, pa_usec_t *max_latency) {
    pa_source_assert_ref(s);
+   pa_assert_ctl_context();
    pa_assert(min_latency);
    pa_assert(max_latency);
 
@@ -1333,9 +1404,8 @@ void pa_source_get_latency_range(pa_source *s, pa_usec_t *min_latency, pa_usec_t
 
 /* Called from IO thread, and from main thread before pa_source_put() is called */
 void pa_source_set_latency_range_within_thread(pa_source *s, pa_usec_t min_latency, pa_usec_t max_latency) {
-    void *state = NULL;
-
     pa_source_assert_ref(s);
+    pa_source_assert_io_context(s);
 
     pa_assert(min_latency >= ABSOLUTE_MIN_LATENCY);
     pa_assert(max_latency <= ABSOLUTE_MAX_LATENCY);
@@ -1347,25 +1417,34 @@ void pa_source_set_latency_range_within_thread(pa_source *s, pa_usec_t min_laten
               (s->flags & PA_SOURCE_DYNAMIC_LATENCY) ||
               s->monitor_of);
 
+    if (s->thread_info.min_latency == min_latency &&
+        s->thread_info.max_latency == max_latency)
+        return;
+
     s->thread_info.min_latency = min_latency;
     s->thread_info.max_latency = max_latency;
 
     if (PA_SOURCE_IS_LINKED(s->thread_info.state)) {
         pa_source_output *o;
+        void *state = NULL;
 
-        while ((o = pa_hashmap_iterate(s->thread_info.outputs, &state, NULL)))
+        PA_HASHMAP_FOREACH(o, s->thread_info.outputs, state)
             if (o->update_source_latency_range)
                 o->update_source_latency_range(o);
     }
 
-    pa_source_invalidate_requested_latency(s);
+    pa_source_invalidate_requested_latency(s, FALSE);
 }
 
 /* Called from main thread, before the source is put */
 void pa_source_set_fixed_latency(pa_source *s, pa_usec_t latency) {
     pa_source_assert_ref(s);
+    pa_assert_ctl_context();
 
-    pa_assert(pa_source_get_state(s) == PA_SOURCE_INIT);
+    if (s->flags & PA_SOURCE_DYNAMIC_LATENCY) {
+        pa_assert(latency == 0);
+        return;
+    }
 
     if (latency < ABSOLUTE_MIN_LATENCY)
         latency = ABSOLUTE_MIN_LATENCY;
@@ -1373,12 +1452,64 @@ void pa_source_set_fixed_latency(pa_source *s, pa_usec_t latency) {
     if (latency > ABSOLUTE_MAX_LATENCY)
         latency = ABSOLUTE_MAX_LATENCY;
 
-    s->fixed_latency = latency;
+    if (PA_SOURCE_IS_LINKED(s->state))
+        pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_SET_FIXED_LATENCY, NULL, (int64_t) latency, NULL) == 0);
+    else
+        s->thread_info.fixed_latency = latency;
+}
+
+/* Called from main thread */
+pa_usec_t pa_source_get_fixed_latency(pa_source *s) {
+    pa_usec_t latency;
+
+    pa_source_assert_ref(s);
+    pa_assert_ctl_context();
+
+    if (s->flags & PA_SOURCE_DYNAMIC_LATENCY)
+        return 0;
+
+    if (PA_SOURCE_IS_LINKED(s->state))
+        pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_MESSAGE_GET_FIXED_LATENCY, &latency, 0, NULL) == 0);
+    else
+        latency = s->thread_info.fixed_latency;
+
+    return latency;
+}
+
+/* Called from IO thread */
+void pa_source_set_fixed_latency_within_thread(pa_source *s, pa_usec_t latency) {
+    pa_source_assert_ref(s);
+    pa_source_assert_io_context(s);
+
+    if (s->flags & PA_SOURCE_DYNAMIC_LATENCY) {
+        pa_assert(latency == 0);
+        return;
+    }
+
+    pa_assert(latency >= ABSOLUTE_MIN_LATENCY);
+    pa_assert(latency <= ABSOLUTE_MAX_LATENCY);
+
+    if (s->thread_info.fixed_latency == latency)
+        return;
+
+    s->thread_info.fixed_latency = latency;
+
+    if (PA_SOURCE_IS_LINKED(s->thread_info.state)) {
+        pa_source_output *o;
+        void *state = NULL;
+
+        PA_HASHMAP_FOREACH(o, s->thread_info.outputs, state)
+            if (o->update_source_fixed_latency)
+                o->update_source_fixed_latency(o);
+    }
+
+    pa_source_invalidate_requested_latency(s, FALSE);
 }
 
 /* Called from main thread */
 size_t pa_source_get_max_rewind(pa_source *s) {
     size_t r;
+    pa_assert_ctl_context();
     pa_source_assert_ref(s);
 
     if (!PA_SOURCE_IS_LINKED(s->state))
@@ -1394,9 +1525,10 @@ int pa_source_set_port(pa_source *s, const char *name, pa_bool_t save) {
     pa_device_port *port;
 
     pa_assert(s);
+    pa_assert_ctl_context();
 
     if (!s->set_port) {
-        pa_log_debug("set_port() operation not implemented for sink %u \"%s\"", s->index, s->name);
+        pa_log_debug("set_port() operation not implemented for source %u \"%s\"", s->index, s->name);
         return -PA_ERR_NOTIMPLEMENTED;
     }
 
index d22e7ca59ccb1f16246101284e5c9dc8d2c7d03b..7b3e4953561bbdacfc289c4731cdddb979dd74a8 100644 (file)
@@ -43,6 +43,7 @@ typedef struct pa_source pa_source;
 #include <pulsecore/source-output.h>
 #include <pulsecore/card.h>
 #include <pulsecore/queue.h>
+#include <pulsecore/thread-mq.h>
 
 #define PA_MAX_OUTPUTS_PER_SOURCE 32
 
@@ -78,7 +79,7 @@ struct pa_source {
     pa_volume_t base_volume; /* shall be constant */
     unsigned n_volume_steps; /* shall be constant */
 
-    pa_cvolume virtual_volume, soft_volume;
+    pa_cvolume volume, soft_volume;
     pa_bool_t muted:1;
 
     pa_bool_t refresh_volume:1;
@@ -89,12 +90,9 @@ struct pa_source {
     pa_bool_t save_muted:1;
 
     pa_asyncmsgq *asyncmsgq;
-    pa_rtpoll *rtpoll;
 
     pa_memchunk silence;
 
-    pa_usec_t fixed_latency; /* for sources with PA_SOURCE_DYNAMIC_LATENCY this is 0 */
-
     pa_hashmap *ports;
     pa_device_port *active_port;
 
@@ -139,6 +137,8 @@ struct pa_source {
         pa_source_state_t state;
         pa_hashmap *outputs;
 
+        pa_rtpoll *rtpoll;
+
         pa_cvolume soft_volume;
         pa_bool_t soft_muted:1;
 
@@ -151,7 +151,9 @@ struct pa_source {
 
         pa_usec_t min_latency; /* we won't go below this latency */
         pa_usec_t max_latency; /* An upper limit for the latencies */
-    } thread_info;
+
+        pa_usec_t fixed_latency; /* for sources with PA_SOURCE_DYNAMIC_LATENCY this is 0 */
+ } thread_info;
 
     void *userdata;
 };
@@ -173,6 +175,8 @@ typedef enum pa_source_message {
     PA_SOURCE_MESSAGE_DETACH,
     PA_SOURCE_MESSAGE_SET_LATENCY_RANGE,
     PA_SOURCE_MESSAGE_GET_LATENCY_RANGE,
+    PA_SOURCE_MESSAGE_SET_FIXED_LATENCY,
+    PA_SOURCE_MESSAGE_GET_FIXED_LATENCY,
     PA_SOURCE_MESSAGE_GET_MAX_REWIND,
     PA_SOURCE_MESSAGE_SET_MAX_REWIND,
     PA_SOURCE_MESSAGE_MAX
@@ -242,12 +246,15 @@ void pa_source_mute_changed(pa_source *s, pa_bool_t new_muted);
 
 int pa_source_sync_suspend(pa_source *s);
 
+void pa_source_update_flags(pa_source *s, pa_source_flags_t mask, pa_source_flags_t value);
+
 /*** May be called by everyone, from main context */
 
 /* The returned value is supposed to be in the time domain of the sound card! */
 pa_usec_t pa_source_get_latency(pa_source *s);
 pa_usec_t pa_source_get_requested_latency(pa_source *s);
 void pa_source_get_latency_range(pa_source *s, pa_usec_t *min_latency, pa_usec_t *max_latency);
+pa_usec_t pa_source_get_fixed_latency(pa_source *s);
 
 size_t pa_source_get_max_rewind(pa_source *s);
 
@@ -257,6 +264,7 @@ int pa_source_suspend_all(pa_core *c, pa_bool_t suspend, pa_suspend_cause_t caus
 
 void pa_source_set_volume(pa_source *source, const pa_cvolume *volume, pa_bool_t save);
 const pa_cvolume *pa_source_get_volume(pa_source *source, pa_bool_t force_refresh);
+
 void pa_source_set_mute(pa_source *source, pa_bool_t mute, pa_bool_t save);
 pa_bool_t pa_source_get_mute(pa_source *source, pa_bool_t force_refresh);
 
@@ -288,11 +296,16 @@ void pa_source_detach_within_thread(pa_source *s);
 pa_usec_t pa_source_get_requested_latency_within_thread(pa_source *s);
 
 void pa_source_set_max_rewind_within_thread(pa_source *s, size_t max_rewind);
+
 void pa_source_set_latency_range_within_thread(pa_source *s, pa_usec_t min_latency, pa_usec_t max_latency);
+void pa_source_set_fixed_latency_within_thread(pa_source *s, pa_usec_t latency);
 
 /*** To be called exclusively by source output drivers, from IO context */
 
-void pa_source_invalidate_requested_latency(pa_source *s);
+void pa_source_invalidate_requested_latency(pa_source *s, pa_bool_t dynamic);
 pa_usec_t pa_source_get_latency_within_thread(pa_source *s);
 
+#define pa_source_assert_io_context(s) \
+    pa_assert(pa_thread_mq_get() || !PA_SOURCE_IS_LINKED((s)->state))
+
 #endif
index 7774bde62be6f4c8b469c525ea92c782a81dbdd4..b3bce1317e3419d83b9bf988b37848cca2556d67 100644 (file)
@@ -68,23 +68,24 @@ int pa_start_child_for_read(const char *name, const char *argv1, pid_t *pid) {
     } else {
         /* child */
 
-        pa_reset_priority();
+        pa_reset_personality();
 
         pa_assert_se(pa_close(pipe_fds[0]) == 0);
-        pa_assert_se(dup2(pipe_fds[1], 1) == 1);
+        pa_assert_se(dup2(pipe_fds[1], STDOUT_FILENO) == STDOUT_FILENO);
 
-        if (pipe_fds[1] != 1)
+        if (pipe_fds[1] != STDOUT_FILENO)
             pa_assert_se(pa_close(pipe_fds[1]) == 0);
 
-        pa_close(0);
-        pa_assert_se(open("/dev/null", O_RDONLY) == 0);
+        pa_close(STDIN_FILENO);
+        pa_assert_se(open("/dev/null", O_RDONLY) == STDIN_FILENO);
 
-        pa_close(2);
-        pa_assert_se(open("/dev/null", O_WRONLY) == 2);
+        pa_close(STDERR_FILENO);
+        pa_assert_se(open("/dev/null", O_WRONLY) == STDERR_FILENO);
 
         pa_close_all(-1);
         pa_reset_sigs(-1);
         pa_unblock_sigs(-1);
+        pa_reset_priority();
 
 #ifdef PR_SET_PDEATHSIG
         /* On Linux we can use PR_SET_PDEATHSIG to have the helper
index 34f92a7e39040fe2fe010638a44f5793f1e9de82..73997a74367401479b4cb9489a9132a695ac8efc 100644 (file)
@@ -59,7 +59,7 @@ static void asyncmsgq_read_cb(pa_mainloop_api*api, pa_io_event* e, int fd, pa_io
         pa_memchunk chunk;
 
         /* Check whether there is a message for us to process */
-        while (pa_asyncmsgq_get(aq, &object, &code, &data, &offset, &chunk, 0) == 0) {
+        while (pa_asyncmsgq_get(aq, &object, &code, &data, &offset, &chunk, 0) >= 0) {
             int ret;
 
             ret = pa_asyncmsgq_dispatch(object, code, data, offset, &chunk);
@@ -104,6 +104,15 @@ void pa_thread_mq_init(pa_thread_mq *q, pa_mainloop_api *mainloop, pa_rtpoll *rt
 void pa_thread_mq_done(pa_thread_mq *q) {
     pa_assert(q);
 
+    /* Since we are called from main context we can be sure that the
+     * inq is empty. However, the outq might still contain messages
+     * for the main loop, which we need to dispatch (e.g. release
+     * msgs, other stuff). Hence do so if we aren't currently
+     * dispatching anyway. */
+
+    if (!pa_asyncmsgq_dispatching(q->outq))
+        pa_asyncmsgq_flush(q->outq, TRUE);
+
     q->mainloop->io_free(q->read_event);
     q->mainloop->io_free(q->write_event);
     q->read_event = q->write_event = NULL;
index 3b5e0e780d95124edf25faca8460ba842b0d8eab..96839d25f1cf4292b188605644406b7b6a28d1be 100644 (file)
@@ -45,4 +45,12 @@ void pa_thread_mq_install(pa_thread_mq *q);
 /* Return the pa_thread_mq object that is set for the current thread */
 pa_thread_mq *pa_thread_mq_get(void);
 
+/* Verify that we are in control context (aka 'main context'). */
+#define pa_assert_ctl_context(s) \
+    pa_assert(!pa_thread_mq_get())
+
+/* Verify that we are in IO context (aka 'thread context'). */
+#define pa_assert_io_context(s) \
+    pa_assert(pa_thread_mq_get())
+
 #endif
index 2dcfa53cdce2367ba782fa06c4ce6e8a10d5706a..551f7ecd6eb303e15a1bcd391569ab14fa95890b 100644 (file)
@@ -1,13 +1,40 @@
+/***
+  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.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
 #include <stdio.h>
 
 #include <pulse/volume.h>
 #include <pulse/gccmacro.h>
 
+#include <pulsecore/macro.h>
+
 int main(int argc, char *argv[]) {
     pa_volume_t v;
     pa_cvolume cv;
     float b;
     pa_channel_map map;
+    pa_volume_t md = 0;
+    unsigned mdn = 0;
 
     printf("Attenuation of sample 1 against 32767: %g dB\n", 20.0*log10(1.0/32767.0));
     printf("Smallest possible attenutation > 0 applied to 32767: %li\n", lrint(32767.0*pa_sw_volume_to_linear(1)));
@@ -60,5 +87,48 @@ int main(int argc, char *argv[]) {
                 printf("After: volume: [%s]; balance: %2.1f (intended: %2.1f) %s\n", pa_cvolume_snprint(s, sizeof(s), &r), k, b, k < b-.05 || k > b+.5 ? "MISMATCH" : "");
             }
 
+    for (v = PA_VOLUME_MUTED; v <= PA_VOLUME_NORM*2; v += 51) {
+
+        double l = pa_sw_volume_to_linear(v);
+        pa_volume_t k = pa_sw_volume_from_linear(l);
+        double db = pa_sw_volume_to_dB(v);
+        pa_volume_t r = pa_sw_volume_from_dB(db);
+        pa_volume_t w;
+
+        pa_assert(k == v);
+        pa_assert(r == v);
+
+        for (w = PA_VOLUME_MUTED; w < PA_VOLUME_NORM*2; w += 37) {
+
+            double t = pa_sw_volume_to_linear(w);
+            double db2 = pa_sw_volume_to_dB(w);
+            pa_volume_t p, p1, p2;
+            double q, qq;
+
+            p = pa_sw_volume_multiply(v, w);
+            qq = db + db2;
+            p2 = pa_sw_volume_from_dB(qq);
+            q = l*t;
+            p1 = pa_sw_volume_from_linear(q);
+
+            if (p2 > p && p2 - p > md)
+                md = p2 - p;
+            if (p2 < p && p - p2 > md)
+                md = p - p2;
+            if (p1 > p && p1 - p > md)
+                md = p1 - p;
+            if (p1 < p && p - p1 > md)
+                md = p - p1;
+
+            if (p1 != p || p2 != p)
+                mdn++;
+        }
+    }
+
+    printf("max deviation: %lu n=%lu\n", (unsigned long) md, (unsigned long) mdn);
+
+    pa_assert(md <= 1);
+    pa_assert(mdn <= 251);
+
     return 0;
 }
index ac60a0bcd80876051b09047d6492eed88b2b40b0..5ef57e3b5b9ed310c64ed2cdf4cde08bbd046627 100644 (file)
@@ -25,7 +25,7 @@
 
 #include <assert.h>
 #include <signal.h>
-#include <sys/select.h>
+#include <sys/poll.h>
 #include <sys/socket.h>
 #include <unistd.h>
 #include <errno.h>
 
 int main(int argc, char*argv[]) {
 
+    enum {
+        WATCH_STDIN,
+        WATCH_STDOUT,
+        WATCH_SOCKET,
+        N_WATCH
+    };
+
     pid_t pid ;
     int fd = -1;
     int ret = 1, i;
@@ -53,6 +60,7 @@ int main(int argc, char*argv[]) {
     size_t ibuf_index, ibuf_length, obuf_index, obuf_length;
     char *cli;
     pa_bool_t ibuf_eof, obuf_eof, ibuf_closed, obuf_closed;
+    struct pollfd pollfd[N_WATCH];
 
     setlocale(LC_ALL, "");
     bindtextdomain(GETTEXT_PACKAGE, PULSE_LOCALEDIR);
@@ -108,7 +116,7 @@ int main(int argc, char*argv[]) {
             size_t k;
 
             k = PA_MIN(sizeof(ibuf) - ibuf_length, strlen(argv[i]));
-            memcpy(ibuf + ibuf_length, argv[1], k);
+            memcpy(ibuf + ibuf_length, argv[i], k);
             ibuf_length += k;
 
             if (ibuf_length < sizeof(ibuf)) {
@@ -120,38 +128,45 @@ int main(int argc, char*argv[]) {
         ibuf_eof = TRUE;
     }
 
-    for (;;) {
-        fd_set ifds, ofds;
+    pa_zero(pollfd);
 
+    pollfd[WATCH_STDIN].fd = STDIN_FILENO;
+    pollfd[WATCH_STDOUT].fd = STDOUT_FILENO;
+    pollfd[WATCH_SOCKET].fd = fd;
+
+    for (;;) {
         if (ibuf_eof &&
             obuf_eof &&
             ibuf_length <= 0 &&
             obuf_length <= 0)
             break;
 
-        FD_ZERO(&ifds);
-        FD_ZERO(&ofds);
+        pollfd[WATCH_STDIN].events = pollfd[WATCH_STDOUT].events = pollfd[WATCH_SOCKET].events = 0;
 
         if (obuf_length > 0)
-            FD_SET(1, &ofds);
+            pollfd[WATCH_STDOUT].events |= POLLOUT;
         else if (!obuf_eof)
-            FD_SET(fd, &ifds);
+            pollfd[WATCH_SOCKET].events |= POLLIN;
 
         if (ibuf_length > 0)
-            FD_SET(fd, &ofds);
+            pollfd[WATCH_SOCKET].events |= POLLOUT;
         else if (!ibuf_eof)
-            FD_SET(0, &ifds);
+            pollfd[WATCH_STDIN].events |= POLLIN;
 
-        if (select(FD_SETSIZE, &ifds, &ofds, NULL, NULL) < 0) {
-            pa_log(_("select(): %s"), strerror(errno));
+        if (poll(pollfd, N_WATCH, -1) < 0) {
+
+            if (errno == EINTR)
+                continue;
+
+            pa_log(_("poll(): %s"), strerror(errno));
             goto fail;
         }
 
-        if (FD_ISSET(0, &ifds)) {
+        if (pollfd[WATCH_STDIN].revents & POLLIN) {
             ssize_t r;
             pa_assert(!ibuf_length);
 
-            if ((r = pa_read(0, ibuf, sizeof(ibuf), NULL)) <= 0) {
+            if ((r = pa_read(STDIN_FILENO, ibuf, sizeof(ibuf), NULL)) <= 0) {
                 if (r < 0) {
                     pa_log(_("read(): %s"), strerror(errno));
                     goto fail;
@@ -164,7 +179,7 @@ int main(int argc, char*argv[]) {
             }
         }
 
-        if (FD_ISSET(fd, &ifds)) {
+        if (pollfd[WATCH_SOCKET].revents & POLLIN) {
             ssize_t r;
             pa_assert(!obuf_length);
 
@@ -181,21 +196,26 @@ int main(int argc, char*argv[]) {
             }
         }
 
-        if (FD_ISSET(1, &ofds)) {
+        if (pollfd[WATCH_STDOUT].revents & POLLHUP) {
+            obuf_eof = TRUE;
+            obuf_length = 0;
+        } else if (pollfd[WATCH_STDOUT].revents & POLLOUT) {
             ssize_t r;
             pa_assert(obuf_length);
 
-            if ((r = pa_write(1, obuf + obuf_index, obuf_length, NULL)) < 0) {
+            if ((r = pa_write(STDOUT_FILENO, obuf + obuf_index, obuf_length, NULL)) < 0) {
                 pa_log(_("write(): %s"), strerror(errno));
                 goto fail;
             }
 
             obuf_length -= (size_t) r;
             obuf_index += obuf_index;
-
         }
 
-        if (FD_ISSET(fd, &ofds)) {
+        if (pollfd[WATCH_SOCKET].revents & POLLHUP) {
+            ibuf_eof = TRUE;
+            ibuf_length = 0;
+        } if (pollfd[WATCH_SOCKET].revents & POLLOUT) {
             ssize_t r;
             pa_assert(ibuf_length);
 
@@ -209,14 +229,14 @@ int main(int argc, char*argv[]) {
         }
 
         if (ibuf_length <= 0 && ibuf_eof && !ibuf_closed) {
-            pa_close(0);
+            pa_close(STDIN_FILENO);
             shutdown(fd, SHUT_WR);
             ibuf_closed = TRUE;
         }
 
         if (obuf_length <= 0 && obuf_eof && !obuf_closed) {
             shutdown(fd, SHUT_RD);
-            pa_close(1);
+            pa_close(STDOUT_FILENO);
             obuf_closed = TRUE;
         }
     }