diff options
58 files changed, 2179 insertions, 597 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 561891ed..6544e2aa 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -177,8 +177,8 @@ pulseaudio_SOURCES = \ daemon/ltdl-bind-now.c daemon/ltdl-bind-now.h \ daemon/main.c -pulseaudio_CFLAGS = $(AM_CFLAGS) $(LIBOIL_CFLAGS) $(LIBSAMPLERATE_CFLAGS) $(LIBSPEEX_CFLAGS) $(LIBSNDFILE_CFLAGS) $(CAP_CFLAGS) $(LIBOIL_CFLAGS) $(DBUS_CFLAGS) -pulseaudio_LDADD = $(AM_LDADD) libpulsecore-@PA_MAJORMINORMICRO@.la libpulsecommon-@PA_MAJORMINORMICRO@.la libpulse.la $(LIBLTDL) $(LIBSAMPLERATE_LIBS) $(LIBSPEEX_LIBS) $(LIBSNDFILE_LIBS) $(CAP_LIBS) $(LIBOIL_LIBS) $(DBUS_LIBS) +pulseaudio_CFLAGS = $(AM_CFLAGS) $(LIBSAMPLERATE_CFLAGS) $(LIBSPEEX_CFLAGS) $(LIBSNDFILE_CFLAGS) $(CAP_CFLAGS) $(DBUS_CFLAGS) +pulseaudio_LDADD = $(AM_LDADD) libpulsecore-@PA_MAJORMINORMICRO@.la libpulsecommon-@PA_MAJORMINORMICRO@.la libpulse.la $(LIBLTDL) $(LIBSAMPLERATE_LIBS) $(LIBSPEEX_LIBS) $(LIBSNDFILE_LIBS) $(CAP_LIBS) $(DBUS_LIBS) # This is needed because automake doesn't properly expand the foreach below pulseaudio_DEPENDENCIES = libpulsecore-@PA_MAJORMINORMICRO@.la libpulsecommon-@PA_MAJORMINORMICRO@.la libpulse.la $(PREOPEN_LIBS) @@ -496,18 +496,18 @@ sig2str_test_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS) resampler_test_SOURCES = tests/resampler-test.c resampler_test_LDADD = $(AM_LDADD) libpulsecore-@PA_MAJORMINORMICRO@.la libpulsecommon-@PA_MAJORMINORMICRO@.la -resampler_test_CFLAGS = $(AM_CFLAGS) $(LIBOIL_CFLAGS) -resampler_test_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS) $(LIBOIL_LIBS) +resampler_test_CFLAGS = $(AM_CFLAGS) +resampler_test_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS) mix_test_SOURCES = tests/mix-test.c mix_test_LDADD = $(AM_LDADD) libpulsecore-@PA_MAJORMINORMICRO@.la libpulsecommon-@PA_MAJORMINORMICRO@.la -mix_test_CFLAGS = $(AM_CFLAGS) $(LIBOIL_CFLAGS) -mix_test_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS) $(LIBOIL_LIBS) +mix_test_CFLAGS = $(AM_CFLAGS) +mix_test_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS) remix_test_SOURCES = tests/remix-test.c remix_test_LDADD = $(AM_LDADD) libpulsecore-@PA_MAJORMINORMICRO@.la libpulsecommon-@PA_MAJORMINORMICRO@.la -remix_test_CFLAGS = $(AM_CFLAGS) $(LIBOIL_CFLAGS) -remix_test_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS) $(LIBOIL_LIBS) +remix_test_CFLAGS = $(AM_CFLAGS) +remix_test_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS) smoother_test_SOURCES = tests/smoother-test.c smoother_test_LDADD = $(AM_LDADD) libpulsecore-@PA_MAJORMINORMICRO@.la libpulsecommon-@PA_MAJORMINORMICRO@.la @@ -516,38 +516,38 @@ smoother_test_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS) envelope_test_SOURCES = tests/envelope-test.c envelope_test_LDADD = $(AM_LDADD) libpulsecore-@PA_MAJORMINORMICRO@.la libpulsecommon-@PA_MAJORMINORMICRO@.la -envelope_test_CFLAGS = $(AM_CFLAGS) $(LIBOIL_CFLAGS) -envelope_test_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS) $(LIBOIL_LIBS) +envelope_test_CFLAGS = $(AM_CFLAGS) +envelope_test_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS) proplist_test_SOURCES = tests/proplist-test.c proplist_test_LDADD = $(AM_LDADD) libpulsecore-@PA_MAJORMINORMICRO@.la libpulsecommon-@PA_MAJORMINORMICRO@.la -proplist_test_CFLAGS = $(AM_CFLAGS) $(LIBOIL_CFLAGS) -proplist_test_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS) $(LIBOIL_LIBS) +proplist_test_CFLAGS = $(AM_CFLAGS) +proplist_test_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS) rtstutter_SOURCES = tests/rtstutter.c rtstutter_LDADD = $(AM_LDADD) libpulsecore-@PA_MAJORMINORMICRO@.la libpulsecommon-@PA_MAJORMINORMICRO@.la -rtstutter_CFLAGS = $(AM_CFLAGS) $(LIBOIL_CFLAGS) -rtstutter_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS) $(LIBOIL_LIBS) +rtstutter_CFLAGS = $(AM_CFLAGS) +rtstutter_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS) stripnul_SOURCES = tests/stripnul.c stripnul_LDADD = $(AM_LDADD) libpulsecore-@PA_MAJORMINORMICRO@.la libpulsecommon-@PA_MAJORMINORMICRO@.la -stripnul_CFLAGS = $(AM_CFLAGS) $(LIBOIL_CFLAGS) -stripnul_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS) $(LIBOIL_LIBS) +stripnul_CFLAGS = $(AM_CFLAGS) +stripnul_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS) lock_autospawn_test_SOURCES = tests/lock-autospawn-test.c lock_autospawn_test_LDADD = $(AM_LDADD) libpulsecore-@PA_MAJORMINORMICRO@.la libpulsecommon-@PA_MAJORMINORMICRO@.la -lock_autospawn_test_CFLAGS = $(AM_CFLAGS) $(LIBOIL_CFLAGS) -lock_autospawn_test_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS) $(LIBOIL_LIBS) +lock_autospawn_test_CFLAGS = $(AM_CFLAGS) +lock_autospawn_test_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS) prioq_test_SOURCES = tests/prioq-test.c prioq_test_LDADD = $(AM_LDADD) libpulsecore-@PA_MAJORMINORMICRO@.la libpulsecommon-@PA_MAJORMINORMICRO@.la -prioq_test_CFLAGS = $(AM_CFLAGS) $(LIBOIL_CFLAGS) -prioq_test_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS) $(LIBOIL_LIBS) +prioq_test_CFLAGS = $(AM_CFLAGS) +prioq_test_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS) sigbus_test_SOURCES = tests/sigbus-test.c sigbus_test_LDADD = $(AM_LDADD) libpulsecore-@PA_MAJORMINORMICRO@.la libpulsecommon-@PA_MAJORMINORMICRO@.la -sigbus_test_CFLAGS = $(AM_CFLAGS) $(LIBOIL_CFLAGS) -sigbus_test_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS) $(LIBOIL_LIBS) +sigbus_test_CFLAGS = $(AM_CFLAGS) +sigbus_test_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS) gtk_test_SOURCES = tests/gtk-test.c gtk_test_LDADD = $(AM_LDADD) libpulse.la libpulse-mainloop-glib.la @@ -832,7 +832,7 @@ libpulsecore_@PA_MAJORMINORMICRO@_la_SOURCES = \ pulsecore/play-memblockq.c pulsecore/play-memblockq.h \ pulsecore/play-memchunk.c pulsecore/play-memchunk.h \ pulsecore/remap.c pulsecore/remap.h \ - pulsecore/remap_mmx.c \ + pulsecore/remap_mmx.c pulsecore/remap_sse.c \ pulsecore/resampler.c pulsecore/resampler.h \ pulsecore/rtpoll.c pulsecore/rtpoll.h \ pulsecore/sample-util.c pulsecore/sample-util.h \ @@ -842,6 +842,7 @@ libpulsecore_@PA_MAJORMINORMICRO@_la_SOURCES = \ pulsecore/svolume_mmx.c pulsecore/svolume_sse.c \ pulsecore/sconv-s16be.c pulsecore/sconv-s16be.h \ pulsecore/sconv-s16le.c pulsecore/sconv-s16le.h \ + pulsecore/sconv_sse.c \ pulsecore/sconv.c pulsecore/sconv.h \ pulsecore/shared.c pulsecore/shared.h \ pulsecore/shm.c pulsecore/shm.h \ @@ -857,9 +858,9 @@ libpulsecore_@PA_MAJORMINORMICRO@_la_SOURCES = \ pulsecore/time-smoother.c pulsecore/time-smoother.h \ pulsecore/database.h -libpulsecore_@PA_MAJORMINORMICRO@_la_CFLAGS = $(AM_CFLAGS) $(LIBSAMPLERATE_CFLAGS) $(LIBSPEEX_CFLAGS) $(WINSOCK_CFLAGS) $(LIBOIL_CFLAGS) +libpulsecore_@PA_MAJORMINORMICRO@_la_CFLAGS = $(AM_CFLAGS) $(LIBSAMPLERATE_CFLAGS) $(LIBSPEEX_CFLAGS) $(WINSOCK_CFLAGS) libpulsecore_@PA_MAJORMINORMICRO@_la_LDFLAGS = -avoid-version -libpulsecore_@PA_MAJORMINORMICRO@_la_LIBADD = $(AM_LIBADD) $(LIBLTDL) $(LIBSAMPLERATE_LIBS) $(LIBSPEEX_LIBS) $(WINSOCK_LIBS) $(LIBOIL_LIBS) $(LTLIBICONV) libpulsecommon-@PA_MAJORMINORMICRO@.la libpulse.la libpulsecore-foreign.la +libpulsecore_@PA_MAJORMINORMICRO@_la_LIBADD = $(AM_LIBADD) $(LIBLTDL) $(LIBSAMPLERATE_LIBS) $(LIBSPEEX_LIBS) $(WINSOCK_LIBS) $(LTLIBICONV) libpulsecommon-@PA_MAJORMINORMICRO@.la libpulse.la libpulsecore-foreign.la if HAVE_X11 libpulsecore_@PA_MAJORMINORMICRO@_la_SOURCES += pulsecore/x11wrap.c pulsecore/x11wrap.h @@ -1010,8 +1011,8 @@ modlibexec_LTLIBRARIES += \ module-tunnel-source.la \ module-position-event-sounds.la \ module-augment-properties.la \ - module-cork-music-on-phone.la - + module-cork-music-on-phone.la \ + module-loopback.la # See comment at librtp.la above if !OS_IS_WIN32 @@ -1249,7 +1250,8 @@ SYMDEF_FILES = \ modules/module-position-event-sounds-symdef.h \ modules/module-augment-properties-symdef.h \ modules/module-cork-music-on-phone-symdef.h \ - modules/module-console-kit-symdef.h + modules/module-console-kit-symdef.h \ + modules/module-loopback-symdef.h EXTRA_DIST += $(SYMDEF_FILES) BUILT_SOURCES += $(SYMDEF_FILES) @@ -1391,6 +1393,10 @@ module_tunnel_source_la_SOURCES = modules/module-tunnel.c module_tunnel_source_la_LDFLAGS = $(MODULE_LDFLAGS) module_tunnel_source_la_LIBADD = $(AM_LIBADD) libpulsecore-@PA_MAJORMINORMICRO@.la libpulsecommon-@PA_MAJORMINORMICRO@.la libpulse.la +module_loopback_la_SOURCES = modules/module-loopback.c +module_loopback_la_LDFLAGS = $(MODULE_LDFLAGS) +module_loopback_la_LIBADD = $(AM_LIBADD) libpulsecore-@PA_MAJORMINORMICRO@.la libpulsecommon-@PA_MAJORMINORMICRO@.la libpulse.la + # X11 module_x11_bell_la_SOURCES = modules/x11/module-x11-bell.c @@ -1732,7 +1738,7 @@ daemon.conf: daemon/daemon.conf.in Makefile -e 's,@PA_DEFAULT_CONFIG_FILE\@,$(DEFAULT_CONFIG_DIR),g' < $< > $@ install-exec-hook: - chown root $(DESTDIR)$(bindir)/pulseaudio ; true + -chown root $(DESTDIR)$(pulselibexecdir)/proximity-helper -chmod u+s $(DESTDIR)$(pulselibexecdir)/proximity-helper ln -sf pacat $(DESTDIR)$(bindir)/parec ln -sf pacat $(DESTDIR)$(bindir)/pamon diff --git a/src/daemon/main.c b/src/daemon/main.c index b1d1109a..af59adef 100644 --- a/src/daemon/main.c +++ b/src/daemon/main.c @@ -259,9 +259,14 @@ static int change_user(void) { pa_set_env("HOME", PA_SYSTEM_RUNTIME_PATH); /* Relevant for pa_runtime_path() */ - pa_set_env("PULSE_RUNTIME_PATH", PA_SYSTEM_RUNTIME_PATH); - pa_set_env("PULSE_CONFIG_PATH", PA_SYSTEM_CONFIG_PATH); - pa_set_env("PULSE_STATE_PATH", PA_SYSTEM_STATE_PATH); + if (!getenv("PULSE_RUNTIME_PATH")) + pa_set_env("PULSE_RUNTIME_PATH", PA_SYSTEM_RUNTIME_PATH); + + if (!getenv("PULSE_CONFIG_PATH")) + pa_set_env("PULSE_CONFIG_PATH", PA_SYSTEM_CONFIG_PATH); + + if (!getenv("PULSE_STATE_PATH")) + pa_set_env("PULSE_STATE_PATH", PA_SYSTEM_STATE_PATH); pa_log_info(_("Successfully dropped root privileges.")); @@ -701,7 +706,7 @@ int main(int argc, char *argv[]) { #endif } - pa_set_env("PULSE_INTERNAL", "1"); + pa_set_env_and_record("PULSE_INTERNAL", "1"); pa_assert_se(chdir("/") == 0); umask(0022); @@ -716,7 +721,7 @@ int main(int argc, char *argv[]) { if (change_user() < 0) goto finish; - pa_set_env("PULSE_SYSTEM", conf->system_instance ? "1" : "0"); + pa_set_env_and_record("PULSE_SYSTEM", conf->system_instance ? "1" : "0"); pa_log_info(_("This is PulseAudio %s"), PACKAGE_VERSION); pa_log_debug(_("Compilation host: %s"), CANONICAL_HOST); @@ -823,8 +828,10 @@ int main(int argc, char *argv[]) { pa_memtrap_install(); - pa_cpu_init_x86(); - pa_cpu_init_arm(); + if (!getenv("PULSE_NO_SIMD")) { + pa_cpu_init_x86(); + pa_cpu_init_arm(); + } pa_assert_se(mainloop = pa_mainloop_new()); @@ -963,6 +970,9 @@ finish: if (valid_pid_file) pa_pid_file_remove(); + /* This has no real purpose except making things valgrind-clean */ + pa_unset_env_recorded(); + #ifdef OS_IS_WIN32 WSACleanup(); #endif diff --git a/src/daemon/polkit.c b/src/daemon/polkit.c deleted file mode 100644 index 9799e094..00000000 --- a/src/daemon/polkit.c +++ /dev/null @@ -1,172 +0,0 @@ -/*** - This file is part of PulseAudio. - - Copyright 2004-2006 Lennart Poettering - Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB - - PulseAudio is free software; you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published - by the Free Software Foundation; either version 2.1 of the License, - or (at your option) any later version. - - PulseAudio is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with PulseAudio; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 - USA. -***/ - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif - -#include <unistd.h> -#include <stdlib.h> -#include <inttypes.h> - -#include <dbus/dbus.h> -#include <polkit-dbus/polkit-dbus.h> - -#include <pulse/i18n.h> - -#include <pulsecore/log.h> -#include <pulsecore/macro.h> - -#include "polkit.h" - -int pa_polkit_check(const char *action_id) { - int ret = -1; - DBusError dbus_error; - DBusConnection *bus = NULL; - PolKitCaller *caller = NULL; - PolKitAction *action = NULL; - PolKitContext *context = NULL; - PolKitError *polkit_error = NULL; - PolKitSession *session = NULL; - PolKitResult polkit_result; - - dbus_error_init(&dbus_error); - - if (!(bus = dbus_bus_get(DBUS_BUS_SYSTEM, &dbus_error))) { - pa_log_error(_("Cannot connect to system bus: %s"), dbus_error.message); - goto finish; - } - - /* There seems to be a bug in some versions of D-Bus that causes - * dbus_shutdown() to call exit() when a connection without this - * flag disabled was created during runtime.*/ - dbus_connection_set_exit_on_disconnect(bus, FALSE); - - if (!(caller = polkit_caller_new_from_pid(bus, getpid(), &dbus_error))) { - pa_log_error(_("Cannot get caller from PID: %s"), dbus_error.message); - goto finish; - } - - /* This function is called when PulseAudio is called SUID root. We - * want to authenticate the real user that called us and not the - * effective user we gained through being SUID root. Hence we - * overwrite the UID caller data here explicitly, just for - * paranoia. In fact PolicyKit should fill in the UID here anyway - * -- an not the EUID or any other user id. */ - - if (!(polkit_caller_set_uid(caller, getuid()))) { - pa_log_error(_("Cannot set UID on caller object.")); - goto finish; - } - - if (!(polkit_caller_get_ck_session(caller, &session))) { - pa_log_error(_("Failed to get CK session.")); - goto finish; - } - - /* We need to overwrite the UID in both the caller and the session - * object */ - - if (!(polkit_session_set_uid(session, getuid()))) { - pa_log_error(_("Cannot set UID on session object.")); - goto finish; - } - - if (!(action = polkit_action_new())) { - pa_log_error(_("Cannot allocate PolKitAction.")); - goto finish; - } - - if (!polkit_action_set_action_id(action, action_id)) { - pa_log_error(_("Cannot set action_id")); - goto finish; - } - - if (!(context = polkit_context_new())) { - pa_log_error(_("Cannot allocate PolKitContext.")); - goto finish; - } - - if (!polkit_context_init(context, &polkit_error)) { - pa_log_error(_("Cannot initialize PolKitContext: %s"), polkit_error_get_error_message(polkit_error)); - goto finish; - } - - for (;;) { - - polkit_result = polkit_context_is_caller_authorized(context, action, caller, TRUE, &polkit_error); - - if (polkit_error_is_set(polkit_error)) { - pa_log_error(_("Could not determine whether caller is authorized: %s"), polkit_error_get_error_message(polkit_error)); - goto finish; - } - - if (polkit_result == POLKIT_RESULT_ONLY_VIA_ADMIN_AUTH || - polkit_result == POLKIT_RESULT_ONLY_VIA_ADMIN_AUTH_KEEP_SESSION || - polkit_result == POLKIT_RESULT_ONLY_VIA_ADMIN_AUTH_KEEP_ALWAYS || - polkit_result == POLKIT_RESULT_ONLY_VIA_ADMIN_AUTH_ONE_SHOT || - polkit_result == POLKIT_RESULT_ONLY_VIA_SELF_AUTH || - polkit_result == POLKIT_RESULT_ONLY_VIA_SELF_AUTH_KEEP_SESSION || - polkit_result == POLKIT_RESULT_ONLY_VIA_SELF_AUTH_KEEP_ALWAYS || - polkit_result == POLKIT_RESULT_ONLY_VIA_SELF_AUTH_ONE_SHOT - ) { - - if (polkit_auth_obtain(action_id, 0, getpid(), &dbus_error)) { - polkit_result = POLKIT_RESULT_YES; - break; - } - - if (dbus_error_is_set(&dbus_error)) { - pa_log_error(_("Cannot obtain auth: %s"), dbus_error.message); - goto finish; - } - } - - break; - } - - if (polkit_result != POLKIT_RESULT_YES && polkit_result != POLKIT_RESULT_NO) - pa_log_warn(_("PolicyKit responded with '%s'"), polkit_result_to_string_representation(polkit_result)); - - ret = polkit_result == POLKIT_RESULT_YES; - -finish: - - if (caller) - polkit_caller_unref(caller); - - if (action) - polkit_action_unref(action); - - if (context) - polkit_context_unref(context); - - if (bus) - dbus_connection_unref(bus); - - dbus_error_free(&dbus_error); - - if (polkit_error) - polkit_error_free(polkit_error); - - return ret; -} diff --git a/src/daemon/polkit.h b/src/daemon/polkit.h deleted file mode 100644 index 018f6ef1..00000000 --- a/src/daemon/polkit.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef foopolkithfoo -#define foopolkithfoo - -/*** - This file is part of PulseAudio. - - Copyright 2007 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. -***/ - -int pa_polkit_check(const char *action); - -#endif diff --git a/src/modules/alsa/alsa-sink.c b/src/modules/alsa/alsa-sink.c index 07d53880..195bdf6e 100644 --- a/src/modules/alsa/alsa-sink.c +++ b/src/modules/alsa/alsa-sink.c @@ -69,9 +69,12 @@ #define TSCHED_WATERMARK_INC_STEP_USEC (10*PA_USEC_PER_MSEC) /* 10ms -- On underrun, increase watermark by this */ #define TSCHED_WATERMARK_DEC_STEP_USEC (5*PA_USEC_PER_MSEC) /* 5ms -- When everything's great, decrease watermark by this */ #define TSCHED_WATERMARK_VERIFY_AFTER_USEC (20*PA_USEC_PER_SEC) /* 20s -- How long after a drop out recheck if things are good now */ -#define TSCHED_WATERMARK_INC_THRESHOLD_USEC (1*PA_USEC_PER_MSEC) /* 3ms -- If the buffer level ever below this theshold, increase the watermark */ +#define TSCHED_WATERMARK_INC_THRESHOLD_USEC (0*PA_USEC_PER_MSEC) /* 0ms -- If the buffer level ever below this theshold, increase the watermark */ #define TSCHED_WATERMARK_DEC_THRESHOLD_USEC (100*PA_USEC_PER_MSEC) /* 100ms -- If the buffer level didn't drop below this theshold in the verification time, decrease the watermark */ +/* Note that TSCHED_WATERMARK_INC_THRESHOLD_USEC == 0 means tht we + * will increase the watermark only if we hit a real underrun. */ + #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*/ @@ -410,6 +413,7 @@ static int try_recover(struct userdata *u, const char *call, int err) { static size_t check_left_to_play(struct userdata *u, size_t n_bytes, pa_bool_t on_timeout) { size_t left_to_play; + pa_bool_t underrun = FALSE; /* We use <= instead of < for this check here because an underrun * only happens after the last sample was processed, not already when @@ -422,6 +426,7 @@ static size_t check_left_to_play(struct userdata *u, size_t n_bytes, pa_bool_t o /* We got a dropout. What a mess! */ left_to_play = 0; + underrun = TRUE; #ifdef DEBUG_TIMING PA_DEBUG_TRAP; @@ -443,7 +448,7 @@ static size_t check_left_to_play(struct userdata *u, size_t n_bytes, pa_bool_t o pa_bool_t reset_not_before = TRUE; if (!u->first && !u->after_rewind) { - if (left_to_play < u->watermark_inc_threshold) + if (underrun || left_to_play < u->watermark_inc_threshold) increase_watermark(u); else if (left_to_play > u->watermark_dec_threshold) { reset_not_before = FALSE; diff --git a/src/modules/alsa/alsa-source.c b/src/modules/alsa/alsa-source.c index 165b2e3b..f42d3542 100644 --- a/src/modules/alsa/alsa-source.c +++ b/src/modules/alsa/alsa-source.c @@ -66,7 +66,7 @@ #define TSCHED_WATERMARK_INC_STEP_USEC (10*PA_USEC_PER_MSEC) /* 10ms */ #define TSCHED_WATERMARK_DEC_STEP_USEC (5*PA_USEC_PER_MSEC) /* 5ms */ #define TSCHED_WATERMARK_VERIFY_AFTER_USEC (20*PA_USEC_PER_SEC) /* 20s */ -#define TSCHED_WATERMARK_INC_THRESHOLD_USEC (1*PA_USEC_PER_MSEC) /* 3ms */ +#define TSCHED_WATERMARK_INC_THRESHOLD_USEC (0*PA_USEC_PER_MSEC) /* 0ms */ #define TSCHED_WATERMARK_DEC_THRESHOLD_USEC (100*PA_USEC_PER_MSEC) /* 100ms */ #define TSCHED_WATERMARK_STEP_USEC (10*PA_USEC_PER_MSEC) /* 10ms */ @@ -406,6 +406,7 @@ static int try_recover(struct userdata *u, const char *call, int err) { static size_t check_left_to_record(struct userdata *u, size_t n_bytes, pa_bool_t on_timeout) { size_t left_to_record; size_t rec_space = u->hwbuf_size - u->hwbuf_unused; + pa_bool_t overrun = FALSE; /* We use <= instead of < for this check here because an overrun * only happens after the last sample was processed, not already when @@ -418,6 +419,7 @@ static size_t check_left_to_record(struct userdata *u, size_t n_bytes, pa_bool_t /* We got a dropout. What a mess! */ left_to_record = 0; + overrun = TRUE; #ifdef DEBUG_TIMING PA_DEBUG_TRAP; @@ -434,7 +436,7 @@ static size_t check_left_to_record(struct userdata *u, size_t n_bytes, pa_bool_t if (u->use_tsched) { pa_bool_t reset_not_before = TRUE; - if (left_to_record < u->watermark_inc_threshold) + if (overrun || left_to_record < u->watermark_inc_threshold) increase_watermark(u); else if (left_to_record > u->watermark_dec_threshold) { reset_not_before = FALSE; diff --git a/src/modules/bluetooth/bluetooth-util.c b/src/modules/bluetooth/bluetooth-util.c index f576823d..f8c5b778 100644 --- a/src/modules/bluetooth/bluetooth-util.c +++ b/src/modules/bluetooth/bluetooth-util.c @@ -1,7 +1,7 @@ /*** This file is part of PulseAudio. - Copyright 2008 Joao Paulo Rechi Vita + Copyright 2008-2009 Joao Paulo Rechi Vita PulseAudio is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as @@ -95,6 +95,7 @@ static pa_bluetooth_device* device_new(const char *path) { d->audio_state = PA_BT_AUDIO_STATE_INVALID; d->audio_sink_state = PA_BT_AUDIO_STATE_INVALID; + d->audio_source_state = PA_BT_AUDIO_STATE_INVALID; d->headset_state = PA_BT_AUDIO_STATE_INVALID; return d; @@ -124,6 +125,7 @@ static pa_bool_t device_is_audio(pa_bluetooth_device *d) { d->device_info_valid && (d->audio_state != PA_BT_AUDIO_STATE_INVALID && (d->audio_sink_state != PA_BT_AUDIO_STATE_INVALID || + d->audio_source_state != PA_BT_AUDIO_STATE_INVALID || d->headset_state != PA_BT_AUDIO_STATE_INVALID)); } @@ -233,6 +235,9 @@ static int parse_device_property(pa_bluetooth_discovery *y, pa_bluetooth_device } else if (strcasecmp(A2DP_SINK_UUID, value) == 0) { pa_assert_se(m = dbus_message_new_method_call("org.bluez", d->path, "org.bluez.AudioSink", "GetProperties")); send_and_add_to_pending(y, d, m, get_properties_reply); + } else if (strcasecmp(A2DP_SOURCE_UUID, value) == 0) { + pa_assert_se(m = dbus_message_new_method_call("org.bluez", d->path, "org.bluez.AudioSource", "GetProperties")); + 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 */ @@ -278,7 +283,7 @@ static int parse_audio_property(pa_bluetooth_discovery *u, int *state, DBusMessa dbus_message_iter_recurse(i, &variant_i); -/* pa_log_debug("Parsing property org.bluez.{Audio|AudioSink|Headset}.%s", key); */ +/* pa_log_debug("Parsing property org.bluez.{Audio|AudioSink|AudioSource|Headset}.%s", key); */ switch (dbus_message_iter_get_arg_type(&variant_i)) { @@ -390,6 +395,9 @@ static void get_properties_reply(DBusPendingCall *pending, void *userdata) { } else if (dbus_message_has_interface(p->message, "org.bluez.AudioSink")) { if (parse_audio_property(y, &d->audio_sink_state, &dict_i) < 0) goto finish; + } else if (dbus_message_has_interface(p->message, "org.bluez.AudioSource")) { + if (parse_audio_property(y, &d->audio_source_state, &dict_i) < 0) + goto finish; } } @@ -440,8 +448,8 @@ static void found_device(pa_bluetooth_discovery *y, const char* path) { pa_assert_se(m = dbus_message_new_method_call("org.bluez", path, "org.bluez.Device", "GetProperties")); send_and_add_to_pending(y, d, m, get_properties_reply); - /* Before we read the other properties (Audio, AudioSink, Headset) we wait - * that the UUID is read */ + /* Before we read the other properties (Audio, AudioSink, AudioSource, + * Headset) we wait that the UUID is read */ } static void list_devices_reply(DBusPendingCall *pending, void *userdata) { @@ -616,6 +624,7 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *us } else if (dbus_message_is_signal(m, "org.bluez.Audio", "PropertyChanged") || dbus_message_is_signal(m, "org.bluez.Headset", "PropertyChanged") || dbus_message_is_signal(m, "org.bluez.AudioSink", "PropertyChanged") || + dbus_message_is_signal(m, "org.bluez.AudioSource", "PropertyChanged") || dbus_message_is_signal(m, "org.bluez.Device", "PropertyChanged")) { pa_bluetooth_device *d; @@ -643,6 +652,9 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *us } else if (dbus_message_has_interface(m, "org.bluez.AudioSink")) { if (parse_audio_property(y, &d->audio_sink_state, &arg_i) < 0) goto fail; + } else if (dbus_message_has_interface(m, "org.bluez.AudioSource")) { + if (parse_audio_property(y, &d->audio_source_state, &arg_i) < 0) + goto fail; } run_callback(y, d, FALSE); @@ -650,6 +662,21 @@ static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *m, void *us return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + } else if (dbus_message_is_signal(m, "org.bluez.Device", "DisconnectRequested")) { + pa_bluetooth_device *d; + + if ((d = pa_hashmap_get(y->devices, dbus_message_get_path(m)))) { + /* Device will disconnect in 2 sec */ + d->audio_state = PA_BT_AUDIO_STATE_DISCONNECTED; + d->audio_sink_state = PA_BT_AUDIO_STATE_DISCONNECTED; + d->audio_source_state = PA_BT_AUDIO_STATE_DISCONNECTED; + d->headset_state = PA_BT_AUDIO_STATE_DISCONNECTED; + + run_callback(y, d, FALSE); + } + + return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; + } else if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameOwnerChanged")) { const char *name, *old_owner, *new_owner; @@ -763,9 +790,11 @@ pa_bluetooth_discovery* pa_bluetooth_discovery_get(pa_core *c) { "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'", "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'", "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'", + "type='signal',sender='org.bluez',interface='org.bluez.Device',member='DisconnectRequested'", "type='signal',sender='org.bluez',interface='org.bluez.Audio',member='PropertyChanged'", "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'", - "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'", NULL) < 0) { + "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'", + "type='signal',sender='org.bluez',interface='org.bluez.AudioSource',member='PropertyChanged'", NULL) < 0) { pa_log("Failed to add D-Bus matches: %s", err.message); goto fail; } @@ -815,9 +844,11 @@ void pa_bluetooth_discovery_unref(pa_bluetooth_discovery *y) { "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceRemoved'", "type='signal',sender='org.bluez',interface='org.bluez.Adapter',member='DeviceCreated'", "type='signal',sender='org.bluez',interface='org.bluez.Device',member='PropertyChanged'", + "type='signal',sender='org.bluez',interface='org.bluez.Device',member='DisconnectRequested'", "type='signal',sender='org.bluez',interface='org.bluez.Audio',member='PropertyChanged'", "type='signal',sender='org.bluez',interface='org.bluez.Headset',member='PropertyChanged'", - "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'", NULL); + "type='signal',sender='org.bluez',interface='org.bluez.AudioSink',member='PropertyChanged'", + "type='signal',sender='org.bluez',interface='org.bluez.AudioSource',member='PropertyChanged'", NULL); dbus_connection_remove_filter(pa_dbus_connection_get(y->connection), filter_cb, y); diff --git a/src/modules/bluetooth/bluetooth-util.h b/src/modules/bluetooth/bluetooth-util.h index f15f2170..e2a0c3d5 100644 --- a/src/modules/bluetooth/bluetooth-util.h +++ b/src/modules/bluetooth/bluetooth-util.h @@ -4,7 +4,7 @@ /*** This file is part of PulseAudio. - Copyright 2008 Joao Paulo Rechi Vita + Copyright 2008-2009 Joao Paulo Rechi Vita PulseAudio is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as @@ -53,7 +53,7 @@ struct pa_bluetooth_uuid { PA_LLIST_FIELDS(pa_bluetooth_uuid); }; -/* This enum is shared among Audio, Headset, and AudioSink, although not all values are acceptable in all profiles */ +/* This enum is shared among Audio, Headset, AudioSink, and AudioSource, although not all values are acceptable in all profiles */ typedef enum pa_bt_audio_state { PA_BT_AUDIO_STATE_INVALID = -1, PA_BT_AUDIO_STATE_DISCONNECTED, @@ -84,6 +84,9 @@ struct pa_bluetooth_device { /* AudioSink state */ pa_bt_audio_state_t audio_sink_state; + /* AudioSource state */ + pa_bt_audio_state_t audio_source_state; + /* Headset state */ pa_bt_audio_state_t headset_state; }; diff --git a/src/modules/bluetooth/module-bluetooth-device.c b/src/modules/bluetooth/module-bluetooth-device.c index 4e23862c..b8a88042 100644 --- a/src/modules/bluetooth/module-bluetooth-device.c +++ b/src/modules/bluetooth/module-bluetooth-device.c @@ -1,7 +1,7 @@ /*** This file is part of PulseAudio. - Copyright 2008 Joao Paulo Rechi Vita + Copyright 2008-2009 Joao Paulo Rechi Vita PulseAudio is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as @@ -129,6 +129,7 @@ struct hsp_info { enum profile { PROFILE_A2DP, + PROFILE_A2DP_SOURCE, PROFILE_HSP, PROFILE_OFF }; @@ -178,6 +179,7 @@ struct userdata { }; #define FIXED_LATENCY_PLAYBACK_A2DP (25*PA_USEC_PER_MSEC) +#define FIXED_LATENCY_RECORD_A2DP (25*PA_USEC_PER_MSEC) #define FIXED_LATENCY_PLAYBACK_HSP (125*PA_USEC_PER_MSEC) #define FIXED_LATENCY_RECORD_HSP (25*PA_USEC_PER_MSEC) @@ -307,7 +309,7 @@ static int parse_caps(struct userdata *u, uint8_t seid, const struct bt_get_capa pa_log_debug("Payload size is %lu %lu", (unsigned long) bytes_left, (unsigned long) sizeof(*codec)); - if ((u->profile == PROFILE_A2DP && codec->transport != BT_CAPABILITIES_TRANSPORT_A2DP) || + if (((u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE) && codec->transport != BT_CAPABILITIES_TRANSPORT_A2DP) || (u->profile == PROFILE_HSP && codec->transport != BT_CAPABILITIES_TRANSPORT_SCO)) { pa_log_error("Got capabilities for wrong codec."); return -1; @@ -344,6 +346,26 @@ static int parse_caps(struct userdata *u, uint8_t seid, const struct bt_get_capa return codec->seid; memcpy(&u->a2dp.sbc_capabilities, codec, sizeof(u->a2dp.sbc_capabilities)); + + } else if (u->profile == PROFILE_A2DP_SOURCE) { + + while (bytes_left > 0) { + if ((codec->type == BT_A2DP_SBC_SOURCE) && !codec->lock) + break; + + bytes_left -= codec->length; + codec = (const codec_capabilities_t*) ((const uint8_t*) codec + codec->length); + } + + if (bytes_left <= 0 || codec->length != sizeof(u->a2dp.sbc_capabilities)) + return -1; + + pa_assert(codec->type == BT_A2DP_SBC_SOURCE); + + if (codec->configured && seid == 0) + return codec->seid; + + memcpy(&u->a2dp.sbc_capabilities, codec, sizeof(u->a2dp.sbc_capabilities)); } return 0; @@ -368,7 +390,7 @@ static int get_caps(struct userdata *u, uint8_t seid) { msg.getcaps_req.seid = seid; pa_strlcpy(msg.getcaps_req.object, u->path, sizeof(msg.getcaps_req.object)); - if (u->profile == PROFILE_A2DP) + if (u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE) msg.getcaps_req.transport = BT_CAPABILITIES_TRANSPORT_A2DP; else { pa_assert(u->profile == PROFILE_HSP); @@ -451,7 +473,7 @@ static int setup_a2dp(struct userdata *u) { }; pa_assert(u); - pa_assert(u->profile == PROFILE_A2DP); + pa_assert(u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE); cap = &u->a2dp.sbc_capabilities; @@ -652,8 +674,8 @@ static int set_conf(struct userdata *u) { msg.open_req.h.length = sizeof(msg.open_req); pa_strlcpy(msg.open_req.object, u->path, sizeof(msg.open_req.object)); - msg.open_req.seid = u->profile == PROFILE_A2DP ? u->a2dp.sbc_capabilities.capability.seid : BT_A2DP_SEID_RANGE + 1; - msg.open_req.lock = u->profile == PROFILE_A2DP ? BT_WRITE_LOCK : BT_READ_LOCK | BT_WRITE_LOCK; + msg.open_req.seid = (u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE) ? u->a2dp.sbc_capabilities.capability.seid : BT_A2DP_SEID_RANGE + 1; + msg.open_req.lock = (u->profile == PROFILE_A2DP) ? BT_WRITE_LOCK : BT_READ_LOCK | BT_WRITE_LOCK; if (service_send(u, &msg.open_req.h) < 0) return -1; @@ -661,7 +683,7 @@ static int set_conf(struct userdata *u) { if (service_expect(u, &msg.open_rsp.h, sizeof(msg), BT_OPEN, sizeof(msg.open_rsp)) < 0) return -1; - if (u->profile == PROFILE_A2DP ) { + if (u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE) { u->sample_spec.format = PA_SAMPLE_S16LE; if (setup_a2dp(u) < 0) @@ -679,7 +701,7 @@ static int set_conf(struct userdata *u) { msg.setconf_req.h.name = BT_SET_CONFIGURATION; msg.setconf_req.h.length = sizeof(msg.setconf_req); - if (u->profile == PROFILE_A2DP) { + if (u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE) { memcpy(&msg.setconf_req.codec, &u->a2dp.sbc_capabilities, sizeof(u->a2dp.sbc_capabilities)); } else { msg.setconf_req.codec.transport = BT_CAPABILITIES_TRANSPORT_SCO; @@ -697,7 +719,7 @@ static int set_conf(struct userdata *u) { u->link_mtu = msg.setconf_rsp.link_mtu; /* setup SBC encoder now we agree on parameters */ - if (u->profile == PROFILE_A2DP) { + if (u->profile == PROFILE_A2DP || u->profile == PROFILE_A2DP_SOURCE) { setup_sbc(&u->a2dp); u->block_size = @@ -1250,6 +1272,119 @@ static int a2dp_process_render(struct userdata *u) { return ret; } +static int a2dp_process_push(struct userdata *u) { + int ret = 0; + pa_memchunk memchunk; + + pa_assert(u); + pa_assert(u->profile == PROFILE_A2DP_SOURCE); + pa_assert(u->source); + pa_assert(u->read_smoother); + + memchunk.memblock = pa_memblock_new(u->core->mempool, u->block_size); + memchunk.index = memchunk.length = 0; + + for (;;) { + pa_bool_t found_tstamp = FALSE; + pa_usec_t tstamp; + struct a2dp_info *a2dp; + struct rtp_header *header; + struct rtp_payload *payload; + const void *p; + void *d; + ssize_t l; + size_t to_write, to_decode; + unsigned frame_count; + + a2dp_prepare_buffer(u); + + a2dp = &u->a2dp; + header = a2dp->buffer; + payload = (struct rtp_payload*) ((uint8_t*) a2dp->buffer + sizeof(*header)); + + l = pa_read(u->stream_fd, a2dp->buffer, a2dp->buffer_size, &u->stream_write_type); + + if (l <= 0) { + + if (l < 0 && errno == EINTR) + /* Retry right away if we got interrupted */ + continue; + + else if (l < 0 && errno == EAGAIN) + /* Hmm, apparently the socket was not readable, give up for now. */ + break; + + pa_log_error("Failed to read data from socket: %s", l < 0 ? pa_cstrerror(errno) : "EOF"); + ret = -1; + break; + } + + pa_assert((size_t) l <= a2dp->buffer_size); + + u->read_index += (uint64_t) l; + + /* TODO: get timestamp from rtp */ + if (!found_tstamp) { + /* pa_log_warn("Couldn't find SO_TIMESTAMP data in auxiliary recvmsg() data!"); */ + tstamp = pa_rtclock_now(); + } + + pa_smoother_put(u->read_smoother, tstamp, pa_bytes_to_usec(u->read_index, &u->sample_spec)); + pa_smoother_resume(u->read_smoother, tstamp, TRUE); + + p = (uint8_t*) a2dp->buffer + sizeof(*header) + sizeof(*payload); + to_decode = l - sizeof(*header) - sizeof(*payload); + + d = pa_memblock_acquire(memchunk.memblock); + to_write = memchunk.length = pa_memblock_get_length(memchunk.memblock); + + while (PA_LIKELY(to_decode > 0 && to_write > 0)) { + size_t written; + ssize_t decoded; + + decoded = sbc_decode(&a2dp->sbc, + p, to_decode, + d, to_write, + &written); + + if (PA_UNLIKELY(decoded <= 0)) { + pa_log_error("SBC decoding error (%li)", (long) decoded); + pa_memblock_release(memchunk.memblock); + pa_memblock_unref(memchunk.memblock); + return -1; + } + +/* pa_log_debug("SBC: decoded: %lu; written: %lu", (unsigned long) decoded, (unsigned long) written); */ +/* pa_log_debug("SBC: frame_length: %lu; codesize: %lu", (unsigned long) a2dp->frame_length, (unsigned long) a2dp->codesize); */ + + pa_assert_fp((size_t) decoded <= to_decode); + pa_assert_fp((size_t) decoded == a2dp->frame_length); + + pa_assert_fp((size_t) written <= to_write); + pa_assert_fp((size_t) written == a2dp->codesize); + + p = (const uint8_t*) p + decoded; + to_decode -= decoded; + + d = (uint8_t*) d + written; + to_write -= written; + + frame_count++; + } + + pa_memblock_release(memchunk.memblock); + + pa_source_post(u->source, &memchunk); + + ret = 1; + break; + } + + pa_memblock_unref(memchunk.memblock); + + return ret; +} + static void thread_func(void *userdata) { struct userdata *u = userdata; unsigned do_write = 0; @@ -1285,7 +1420,12 @@ static void thread_func(void *userdata) { if (pollfd && (pollfd->revents & POLLIN)) { int n_read; - if ((n_read = hsp_process_push(u)) < 0) + if (u->profile == PROFILE_HSP) + n_read = hsp_process_push(u); + else + n_read = a2dp_process_push(u); + + if (n_read < 0) goto fail; /* We just read something, so we are supposed to write something, too */ @@ -1687,7 +1827,7 @@ static int add_source(struct userdata *u) { data.driver = __FILE__; data.module = u->module; pa_source_new_data_set_sample_spec(&data, &u->sample_spec); - pa_proplist_sets(data.proplist, "bluetooth.protocol", u->profile == PROFILE_A2DP ? "a2dp" : "hsp"); + pa_proplist_sets(data.proplist, "bluetooth.protocol", u->profile == PROFILE_A2DP_SOURCE ? "a2dp_source" : "hsp"); if (u->profile == PROFILE_HSP) pa_proplist_sets(data.proplist, PA_PROP_DEVICE_INTENDED_ROLES, "phone"); data.card = u->card; @@ -1712,7 +1852,7 @@ static int add_source(struct userdata *u) { u->source->parent.process_msg = source_process_msg; pa_source_set_fixed_latency(u->source, - (/* u->profile == PROFILE_A2DP ? FIXED_LATENCY_RECORD_A2DP : */ FIXED_LATENCY_RECORD_HSP) + + (u->profile == PROFILE_A2DP_SOURCE ? FIXED_LATENCY_RECORD_A2DP : FIXED_LATENCY_RECORD_HSP) + pa_bytes_to_usec(u->block_size, &u->sample_spec)); } @@ -1809,7 +1949,8 @@ static int init_profile(struct userdata *u) { if (add_sink(u) < 0) r = -1; - if (u->profile == PROFILE_HSP) + if (u->profile == PROFILE_HSP || + u->profile == PROFILE_A2DP_SOURCE) if (add_source(u) < 0) r = -1; @@ -2050,6 +2191,20 @@ static int add_card(struct userdata *u, const pa_bluetooth_device *device) { pa_hashmap_put(data.profiles, p->name, p); } + if (pa_bluetooth_uuid_has(device->uuids, A2DP_SOURCE_UUID)) { + p = pa_card_profile_new("a2dp_source", _("High Fidelity Capture (A2DP)"), sizeof(enum profile)); + p->priority = 10; + p->n_sinks = 0; + p->n_sources = 1; + p->max_sink_channels = 0; + p->max_source_channels = 2; + + d = PA_CARD_PROFILE_DATA(p); + *d = PROFILE_A2DP_SOURCE; + + pa_hashmap_put(data.profiles, p->name, p); + } + if (pa_bluetooth_uuid_has(device->uuids, HSP_HS_UUID) || pa_bluetooth_uuid_has(device->uuids, HFP_HS_UUID)) { p = pa_card_profile_new("hsp", _("Telephony Duplex (HSP/HFP)"), sizeof(enum profile)); diff --git a/src/modules/bluetooth/module-bluetooth-discover.c b/src/modules/bluetooth/module-bluetooth-discover.c index 788fee00..02fd4240 100644 --- a/src/modules/bluetooth/module-bluetooth-discover.c +++ b/src/modules/bluetooth/module-bluetooth-discover.c @@ -1,7 +1,7 @@ /*** This file is part of PulseAudio. - Copyright 2008 Joao Paulo Rechi Vita + Copyright 2008-2009 Joao Paulo Rechi Vita PulseAudio is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as @@ -84,7 +84,7 @@ static pa_hook_result_t load_module_for_device(pa_bluetooth_discovery *y, const mi = pa_hashmap_get(u->hashmap, d->path); if (!d->dead && - d->device_connected > 0 && d->audio_state >= PA_BT_AUDIO_STATE_CONNECTED) { + d->device_connected > 0 && (d->audio_state >= PA_BT_AUDIO_STATE_CONNECTED || d->audio_source_state >= PA_BT_AUDIO_STATE_CONNECTED)) { if (!mi) { pa_module *m = NULL; @@ -116,6 +116,9 @@ static pa_hook_result_t load_module_for_device(pa_bluetooth_discovery *y, const } #endif + if (d->audio_source_state >= PA_BT_AUDIO_STATE_CONNECTED) + args = pa_sprintf_malloc("%s profile=\"a2dp_source\"", args); + pa_log_debug("Loading module-bluetooth-device %s", args); m = pa_module_load(u->module->core, "module-bluetooth-device", args); pa_xfree(args); diff --git a/src/modules/module-combine.c b/src/modules/module-combine.c index e90ef11c..a186c899 100644 --- a/src/modules/module-combine.c +++ b/src/modules/module-combine.c @@ -58,7 +58,7 @@ PA_MODULE_USAGE( "sink_name=<name for the sink> " "sink_properties=<properties for the sink> " "slaves=<slave sinks> " - "adjust_time=<seconds> " + "adjust_time=<how often to readjust rates in s> " "resample_method=<method> " "format=<sample format> " "rate=<sample rate> " @@ -69,7 +69,7 @@ PA_MODULE_USAGE( #define MEMBLOCKQ_MAXLENGTH (1024*1024*16) -#define DEFAULT_ADJUST_TIME 10 +#define DEFAULT_ADJUST_TIME_USEC (10*PA_USEC_PER_SEC) #define BLOCK_USEC (PA_USEC_PER_MSEC * 200) @@ -91,7 +91,6 @@ 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 */ @@ -121,7 +120,7 @@ struct userdata { pa_rtpoll *rtpoll; pa_time_event *time_event; - uint32_t adjust_time; + pa_usec_t adjust_time; pa_bool_t automatic; pa_bool_t auto_desc; @@ -221,9 +220,9 @@ static void adjust_rates(struct userdata *u) { continue; if (o->total_latency < target_latency) - r -= (uint32_t) ((((double) (target_latency - o->total_latency))/(double)u->adjust_time)*(double)r/PA_USEC_PER_SEC); + r -= (uint32_t) ((((double) (target_latency - o->total_latency))/(double)u->adjust_time)*(double)r); else if (o->total_latency > target_latency) - r += (uint32_t) ((((double) (o->total_latency - target_latency))/(double)u->adjust_time)*(double)r/PA_USEC_PER_SEC); + r += (uint32_t) ((((double) (o->total_latency - target_latency))/(double)u->adjust_time)*(double)r); 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).", o->sink_input->sink->name, base_rate, r); @@ -246,7 +245,7 @@ static void time_callback(pa_mainloop_api *a, pa_time_event *e, const struct tim adjust_rates(u); - pa_core_rttime_restart(u->core, e, pa_rtclock_now() + u->adjust_time * PA_USEC_PER_SEC); + pa_core_rttime_restart(u->core, e, pa_rtclock_now() + u->adjust_time); } static void process_render_null(struct userdata *u, pa_usec_t now) { @@ -845,8 +844,9 @@ static int output_create_sink_input(struct output *o) { pa_sink_input_new_data_set_channel_map(&data, &o->userdata->sink->channel_map); data.module = o->userdata->module; data.resample_method = o->userdata->resample_method; + data.flags = PA_SINK_INPUT_VARIABLE_RATE|PA_SINK_INPUT_DONT_MOVE|PA_SINK_INPUT_NO_CREATE_ON_SUSPEND; - 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(&o->sink_input, o->userdata->core, &data); pa_sink_input_new_data_done(&data); @@ -1105,6 +1105,7 @@ int pa__init(pa_module*m) { struct output *o; uint32_t idx; pa_sink_new_data data; + uint32_t adjust_time_sec; pa_assert(m); @@ -1123,16 +1124,10 @@ int pa__init(pa_module*m) { m->userdata = u = pa_xnew0(struct userdata, 1); u->core = m->core; u->module = m; - u->adjust_time = DEFAULT_ADJUST_TIME; u->rtpoll = pa_rtpoll_new(); pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll); u->resample_method = resample_method; u->outputs = pa_idxset_new(NULL, NULL); - 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); - u->thread_info.in_null_mode = FALSE; - u->thread_info.counter = 0; u->thread_info.smoother = pa_smoother_new( PA_USEC_PER_SEC, PA_USEC_PER_SEC*2, @@ -1142,11 +1137,17 @@ int pa__init(pa_module*m) { 0, FALSE); - if (pa_modargs_get_value_u32(ma, "adjust_time", &u->adjust_time) < 0) { + adjust_time_sec = DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC; + if (pa_modargs_get_value_u32(ma, "adjust_time", &adjust_time_sec) < 0) { pa_log("Failed to parse adjust_time value"); goto fail; } + if (adjust_time_sec != DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC) + u->adjust_time = adjust_time_sec * PA_USEC_PER_SEC; + else + u->adjust_time = DEFAULT_ADJUST_TIME_USEC; + slaves = pa_modargs_get_value(ma, "slaves", NULL); u->automatic = !slaves; @@ -1315,7 +1316,7 @@ int pa__init(pa_module*m) { 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); + u->time_event = pa_core_rttime_new(m->core, pa_rtclock_now() + u->adjust_time, time_callback, u); pa_modargs_free(ma); diff --git a/src/modules/module-ladspa-sink.c b/src/modules/module-ladspa-sink.c index aa28f7fd..994c778f 100644 --- a/src/modules/module-ladspa-sink.c +++ b/src/modules/module-ladspa-sink.c @@ -82,6 +82,8 @@ struct userdata { LADSPA_Data control_out; pa_memblockq *memblockq; + + pa_bool_t auto_desc; }; static const char* const valid_modargs[] = { @@ -423,6 +425,19 @@ static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) { pa_sink_update_flags(u->sink, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY, dest->flags); } else pa_sink_set_asyncmsgq(u->sink, NULL); + + if (u->auto_desc && dest) { + const char *z; + pa_proplist *pl; + + pl = pa_proplist_new(); + z = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION); + pa_proplist_setf(pl, PA_PROP_DEVICE_DESCRIPTION, "LADSPA Plugin %s on %s", + pa_proplist_gets(u->sink->proplist, "device.ladspa.name"), z ? z : dest->name); + + pa_sink_update_proplist(u->sink, PA_UPDATE_REPLACE, pl); + pa_proplist_free(pl); + } } /* Called from main context */ @@ -451,7 +466,6 @@ int pa__init(pa_module*m) { pa_channel_map map; pa_modargs *ma; char *t; - const char *z; pa_sink *master; pa_sink_input_new_data sink_input_data; pa_sink_new_data sink_data; @@ -765,8 +779,6 @@ int pa__init(pa_module*m) { sink_data.name = pa_sprintf_malloc("%s.ladspa", master->name); 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", 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); @@ -782,6 +794,13 @@ int pa__init(pa_module*m) { goto fail; } + if ((u->auto_desc = !pa_proplist_contains(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) { + const char *z; + + 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", d->Name, z ? z : master->name); + } + u->sink = pa_sink_new(m->core, &sink_data, PA_SINK_HW_MUTE_CTRL|PA_SINK_HW_VOLUME_CTRL|PA_SINK_DECIBEL_VOLUME| (master->flags & (PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY))); @@ -812,7 +831,7 @@ int pa__init(pa_module*m) { 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, 0); + pa_sink_input_new(&u->sink_input, m->core, &sink_input_data); pa_sink_input_new_data_done(&sink_input_data); if (!u->sink_input) diff --git a/src/modules/module-loopback.c b/src/modules/module-loopback.c new file mode 100644 index 00000000..29c3ddab --- /dev/null +++ b/src/modules/module-loopback.c @@ -0,0 +1,784 @@ +/*** + This file is part of PulseAudio. + + Copyright 2009 Intel Corporation + Contributor: Pierre-Louis Bossart <pierre-louis.bossart@intel.com> + + 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 <math.h> + +#include <pulse/xmalloc.h> + +#include <pulsecore/sink-input.h> +#include <pulsecore/module.h> +#include <pulsecore/modargs.h> +#include <pulsecore/namereg.h> +#include <pulsecore/log.h> +#include <pulsecore/core-util.h> + +#include <pulse/rtclock.h> +#include <pulse/timeval.h> + +#include "module-loopback-symdef.h" + +PA_MODULE_AUTHOR("Pierre-Louis Bossart"); +PA_MODULE_DESCRIPTION("Loopback from source to sink"); +PA_MODULE_VERSION(PACKAGE_VERSION); +PA_MODULE_LOAD_ONCE(FALSE); +PA_MODULE_USAGE( + "source=<source to connect to> " + "sink=<sink to connect to> " + "adjust_time=<how often to readjust rates in s> " + "latency_msec=<latency in ms> " + "format=<sample format> " + "rate=<sample rate> " + "channels=<number of channels> " + "channel_map=<channel map>"); + +#define DEFAULT_LATENCY_MSEC 200 + +#define MEMBLOCKQ_MAXLENGTH (1024*1024*16) + +#define DEFAULT_ADJUST_TIME_USEC (10*PA_USEC_PER_SEC) + +struct userdata { + pa_core *core; + pa_module *module; + + pa_sink_input *sink_input; + pa_source_output *source_output; + + pa_asyncmsgq *asyncmsgq; + pa_memblockq *memblockq; + + pa_rtpoll_item *rtpoll_item_read, *rtpoll_item_write; + + pa_time_event *time_event; + pa_usec_t adjust_time; + + int64_t recv_counter; + int64_t send_counter; + + size_t skip; + pa_usec_t latency; + + pa_bool_t in_pop; + size_t min_memblockq_length; + + struct { + int64_t send_counter; + size_t source_output_buffer; + pa_usec_t source_latency; + + int64_t recv_counter; + size_t sink_input_buffer; + pa_usec_t sink_latency; + + size_t min_memblockq_length; + size_t max_request; + } latency_snapshot; +}; + +static const char* const valid_modargs[] = { + "source", + "sink", + "latency", + "format", + "rate", + "channels", + "channel_map", + NULL, +}; + +enum { + SINK_INPUT_MESSAGE_POST = PA_SINK_INPUT_MESSAGE_MAX, + SINK_INPUT_MESSAGE_REWIND, + SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT, + SINK_INPUT_MESSAGE_MAX_REQUEST_CHANGED +}; + +enum { + SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT +}; + +/* Called from main context */ +static void teardown(struct userdata *u) { + pa_assert(u); + pa_assert_ctl_context(); + + if (u->sink_input) + pa_sink_input_unlink(u->sink_input); + + if (u->source_output) + pa_source_output_unlink(u->source_output); + + if (u->sink_input) { + pa_sink_input_unref(u->sink_input); + u->sink_input = NULL; + } + + if (u->source_output) { + pa_source_output_unref(u->source_output); + u->source_output = NULL; + } +} + +/* Called from main context */ +static void adjust_rates(struct userdata *u) { + size_t buffer, fs; + uint32_t old_rate, base_rate, new_rate; + pa_usec_t buffer_latency; + + pa_assert(u); + pa_assert_ctl_context(); + + pa_asyncmsgq_send(u->source_output->source->asyncmsgq, PA_MSGOBJECT(u->source_output), SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT, NULL, 0, NULL); + pa_asyncmsgq_send(u->sink_input->sink->asyncmsgq, PA_MSGOBJECT(u->sink_input), SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT, NULL, 0, NULL); + + buffer = + u->latency_snapshot.sink_input_buffer + + u->latency_snapshot.source_output_buffer; + + if (u->latency_snapshot.recv_counter <= u->latency_snapshot.send_counter) + buffer += (size_t) (u->latency_snapshot.send_counter - u->latency_snapshot.recv_counter); + else + buffer += PA_CLIP_SUB(buffer, (size_t) (u->latency_snapshot.recv_counter - u->latency_snapshot.send_counter)); + + buffer_latency = pa_bytes_to_usec(buffer, &u->sink_input->sample_spec); + + pa_log_info("Loopback overall latency is %0.2f ms + %0.2f ms + %0.2f ms = %0.2f ms", + (double) u->latency_snapshot.sink_latency / PA_USEC_PER_MSEC, + (double) buffer_latency / PA_USEC_PER_MSEC, + (double) u->latency_snapshot.source_latency / PA_USEC_PER_MSEC, + ((double) u->latency_snapshot.sink_latency + buffer_latency + u->latency_snapshot.source_latency) / PA_USEC_PER_MSEC); + + pa_log_info("Should buffer %zu bytes, buffered at minimum %zu bytes", + u->latency_snapshot.max_request*2, + u->latency_snapshot.min_memblockq_length); + + fs = pa_frame_size(&u->sink_input->sample_spec); + old_rate = u->sink_input->sample_spec.rate; + base_rate = u->source_output->sample_spec.rate; + + if (u->latency_snapshot.min_memblockq_length < u->latency_snapshot.max_request*2) + new_rate = base_rate - (((u->latency_snapshot.max_request*2 - u->latency_snapshot.min_memblockq_length) / fs) *PA_USEC_PER_SEC)/u->adjust_time; + else + new_rate = base_rate + (((u->latency_snapshot.min_memblockq_length - u->latency_snapshot.max_request*2) / fs) *PA_USEC_PER_SEC)/u->adjust_time; + + pa_log_info("Old rate %lu Hz, new rate %lu Hz", (unsigned long) old_rate, (unsigned long) new_rate); + + pa_sink_input_set_rate(u->sink_input, new_rate); + + pa_core_rttime_restart(u->core, u->time_event, pa_rtclock_now() + u->adjust_time); +} + +/* Called from main context */ +static void time_callback(pa_mainloop_api *a, pa_time_event *e, const struct timeval *t, void *userdata) { + struct userdata *u = userdata; + + pa_assert(u); + pa_assert(a); + pa_assert(u->time_event == e); + + adjust_rates(u); +} + +/* Called from input thread context */ +static void source_output_push_cb(pa_source_output *o, const pa_memchunk *chunk) { + struct userdata *u; + pa_memchunk copy; + + pa_source_output_assert_ref(o); + pa_source_output_assert_io_context(o); + pa_assert_se(u = o->userdata); + + if (u->skip > chunk->length) { + u->skip -= chunk->length; + return; + } + + if (u->skip > 0) { + copy = *chunk; + copy.index += u->skip; + copy.length -= u->skip; + u->skip = 0; + + chunk = © + } + + pa_asyncmsgq_post(u->asyncmsgq, PA_MSGOBJECT(u->sink_input), SINK_INPUT_MESSAGE_POST, NULL, 0, chunk, NULL); + u->send_counter += (int64_t) chunk->length; +} + +/* Called from input thread context */ +static void source_output_process_rewind_cb(pa_source_output *o, size_t nbytes) { + struct userdata *u; + + pa_source_output_assert_ref(o); + pa_source_output_assert_io_context(o); + pa_assert_se(u = o->userdata); + + pa_asyncmsgq_post(u->asyncmsgq, PA_MSGOBJECT(u->sink_input), SINK_INPUT_MESSAGE_REWIND, NULL, (int64_t) nbytes, NULL, NULL); + u->send_counter -= (int64_t) nbytes; +} + +/* Called from output thread context */ +static int source_output_process_msg_cb(pa_msgobject *obj, int code, void *data, int64_t offset, pa_memchunk *chunk) { + struct userdata *u = PA_SOURCE_OUTPUT(obj)->userdata; + + switch (code) { + + case SOURCE_OUTPUT_MESSAGE_LATENCY_SNAPSHOT: { + size_t length; + + length = pa_memblockq_get_length(u->source_output->thread_info.delay_memblockq); + + u->latency_snapshot.send_counter = u->send_counter; + u->latency_snapshot.source_output_buffer = u->source_output->thread_info.resampler ? pa_resampler_result(u->source_output->thread_info.resampler, length) : length; + u->latency_snapshot.source_latency = pa_source_get_latency_within_thread(u->source_output->source); + + return 0; + } + } + + return pa_source_output_process_msg(obj, code, data, offset, chunk); +} + +/* Called from output thread context */ +static void source_output_attach_cb(pa_source_output *o) { + struct userdata *u; + + pa_source_output_assert_ref(o); + pa_source_output_assert_io_context(o); + pa_assert_se(u = o->userdata); + + u->rtpoll_item_write = pa_rtpoll_item_new_asyncmsgq_write( + o->source->thread_info.rtpoll, + PA_RTPOLL_LATE, + u->asyncmsgq); +} + +/* Called from output thread context */ +static void source_output_detach_cb(pa_source_output *o) { + struct userdata *u; + + pa_source_output_assert_ref(o); + pa_source_output_assert_io_context(o); + pa_assert_se(u = o->userdata); + + if (u->rtpoll_item_write) { + pa_rtpoll_item_free(u->rtpoll_item_write); + u->rtpoll_item_write = NULL; + } +} + +/* Called from output thread context */ +static void source_output_state_change_cb(pa_source_output *o, pa_source_output_state_t state) { + struct userdata *u; + + pa_source_output_assert_ref(o); + pa_source_output_assert_io_context(o); + pa_assert_se(u = o->userdata); + + if (PA_SOURCE_OUTPUT_IS_LINKED(state) && o->thread_info.state == PA_SOURCE_OUTPUT_INIT) { + + u->skip = pa_usec_to_bytes(PA_CLIP_SUB(pa_source_get_latency_within_thread(o->source), + u->latency), + &o->sample_spec); + + pa_log_info("Skipping %lu bytes", (unsigned long) u->skip); + } +} + +/* Called from main thread */ +static void source_output_kill_cb(pa_source_output *o) { + struct userdata *u; + + pa_source_output_assert_ref(o); + pa_assert_ctl_context(); + pa_assert_se(u = o->userdata); + + teardown(u); + pa_module_unload_request(u->module, TRUE); +} + +/* Called from main thread */ +static pa_bool_t source_output_may_move_to_cb(pa_source_output *o, pa_source *dest) { + struct userdata *u; + + pa_source_output_assert_ref(o); + pa_assert_ctl_context(); + pa_assert_se(u = o->userdata); + + return dest != u->sink_input->sink->monitor_source; +} + +/* Called from main thread */ +static void source_output_moving_cb(pa_source_output *o, pa_source *dest) { + pa_proplist *p; + const char *n; + struct userdata *u; + + pa_source_output_assert_ref(o); + pa_assert_ctl_context(); + pa_assert_se(u = o->userdata); + + p = pa_proplist_new(); + pa_proplist_setf(p, PA_PROP_MEDIA_NAME, "Loopback of %s", pa_strnull(pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION))); + + if ((n = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_ICON_NAME))) + pa_proplist_sets(p, PA_PROP_MEDIA_ICON_NAME, n); + + pa_sink_input_update_proplist(u->sink_input, PA_UPDATE_REPLACE, p); + pa_proplist_free(p); +} + +/* Called from output thread context */ +static void update_min_memblockq_length(struct userdata *u) { + size_t length; + + pa_assert(u); + pa_sink_input_assert_io_context(u->sink_input); + + length = pa_memblockq_get_length(u->memblockq); + + if (u->min_memblockq_length == (size_t) -1 || + length < u->min_memblockq_length) + u->min_memblockq_length = length; +} + +/* Called from output thread context */ +static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) { + struct userdata *u; + + pa_sink_input_assert_ref(i); + pa_sink_input_assert_io_context(i); + pa_assert_se(u = i->userdata); + pa_assert(chunk); + + u->in_pop = TRUE; + while (pa_asyncmsgq_process_one(u->asyncmsgq) > 0) + ; + u->in_pop = FALSE; + + if (pa_memblockq_peek(u->memblockq, chunk) < 0) { + pa_log_info("Coud not peek into queue"); + return -1; + } + + chunk->length = PA_MIN(chunk->length, nbytes); + pa_memblockq_drop(u->memblockq, chunk->length); + + update_min_memblockq_length(u); + + return 0; +} + +/* Called from output thread context */ +static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) { + struct userdata *u; + + pa_sink_input_assert_ref(i); + pa_sink_input_assert_io_context(i); + pa_assert_se(u = i->userdata); + + pa_memblockq_rewind(u->memblockq, nbytes); +} + +/* Called from output thread context */ +static int sink_input_process_msg_cb(pa_msgobject *obj, int code, void *data, int64_t offset, pa_memchunk *chunk) { + struct userdata *u = PA_SINK_INPUT(obj)->userdata; + + switch (code) { + + case PA_SINK_INPUT_MESSAGE_GET_LATENCY: { + pa_usec_t *r = data; + + pa_sink_input_assert_io_context(u->sink_input); + + *r = pa_bytes_to_usec(pa_memblockq_get_length(u->memblockq), &u->sink_input->sample_spec); + + /* Fall through, the default handler will add in the extra + * latency added by the resampler */ + break; + } + + case SINK_INPUT_MESSAGE_POST: + + pa_sink_input_assert_io_context(u->sink_input); + + if (PA_SINK_IS_OPENED(u->sink_input->sink->thread_info.state)) + pa_memblockq_push_align(u->memblockq, chunk); + else + pa_memblockq_flush_write(u->memblockq); + + update_min_memblockq_length(u); + + /* Is this the end of an underrun? Then let's start things + * right-away */ + if (!u->in_pop && + u->sink_input->thread_info.underrun_for > 0 && + pa_memblockq_is_readable(u->memblockq)) { + + pa_log_debug("Requesting rewind due to end of underrun."); + pa_sink_input_request_rewind(u->sink_input, + (size_t) (u->sink_input->thread_info.underrun_for == (size_t) -1 ? 0 : u->sink_input->thread_info.underrun_for), + FALSE, TRUE, FALSE); + } + + u->recv_counter += (int64_t) chunk->length; + + return 0; + + case SINK_INPUT_MESSAGE_REWIND: + + pa_sink_input_assert_io_context(u->sink_input); + + if (PA_SINK_IS_OPENED(u->sink_input->sink->thread_info.state)) + pa_memblockq_seek(u->memblockq, -offset, PA_SEEK_RELATIVE, TRUE); + else + pa_memblockq_flush_write(u->memblockq); + + u->recv_counter -= offset; + + update_min_memblockq_length(u); + + return 0; + + case SINK_INPUT_MESSAGE_LATENCY_SNAPSHOT: { + size_t length; + + update_min_memblockq_length(u); + + length = pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq); + + u->latency_snapshot.recv_counter = u->recv_counter; + u->latency_snapshot.sink_input_buffer = + pa_memblockq_get_length(u->memblockq) + + (u->sink_input->thread_info.resampler ? pa_resampler_request(u->sink_input->thread_info.resampler, length) : length); + u->latency_snapshot.sink_latency = pa_sink_get_latency_within_thread(u->sink_input->sink); + + u->latency_snapshot.max_request = pa_sink_input_get_max_request(u->sink_input); + + u->latency_snapshot.min_memblockq_length = u->min_memblockq_length; + u->min_memblockq_length = (size_t) -1; + + return 0; + } + + case SINK_INPUT_MESSAGE_MAX_REQUEST_CHANGED: { + /* This message is sent from the IO thread to the main + * thread! So don't be confused. All the user cases above + * are executed in thread context, but this one is not! */ + + pa_assert_ctl_context(); + + adjust_rates(u); + return 0; + } + } + + return pa_sink_input_process_msg(obj, code, data, offset, chunk); +} + +/* Called from output thread context */ +static void sink_input_attach_cb(pa_sink_input *i) { + struct userdata *u; + + pa_sink_input_assert_ref(i); + pa_sink_input_assert_io_context(i); + pa_assert_se(u = i->userdata); + + u->rtpoll_item_read = pa_rtpoll_item_new_asyncmsgq_read( + i->sink->thread_info.rtpoll, + PA_RTPOLL_LATE, + u->asyncmsgq); + + pa_memblockq_set_prebuf(u->memblockq, pa_sink_input_get_max_request(i)*2); + pa_memblockq_set_maxrewind(u->memblockq, pa_sink_input_get_max_rewind(i)); + + u->min_memblockq_length = (size_t) -1; +} + +/* Called from output thread context */ +static void sink_input_detach_cb(pa_sink_input *i) { + struct userdata *u; + + pa_sink_input_assert_ref(i); + pa_sink_input_assert_io_context(i); + pa_assert_se(u = i->userdata); + + if (u->rtpoll_item_read) { + pa_rtpoll_item_free(u->rtpoll_item_read); + u->rtpoll_item_read = NULL; + } +} + +/* Called from output thread context */ +static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) { + struct userdata *u; + + pa_sink_input_assert_ref(i); + pa_sink_input_assert_io_context(i); + pa_assert_se(u = i->userdata); + + pa_memblockq_set_maxrewind(u->memblockq, nbytes); +} + +/* Called from output thread context */ +static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) { + struct userdata *u; + + pa_sink_input_assert_ref(i); + pa_sink_input_assert_io_context(i); + pa_assert_se(u = i->userdata); + + pa_memblockq_set_prebuf(u->memblockq, nbytes*2); + pa_log_info("Max request changed"); + pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(u->sink_input), SINK_INPUT_MESSAGE_MAX_REQUEST_CHANGED, NULL, 0, NULL, NULL); +} + +/* Called from main thread */ +static void sink_input_kill_cb(pa_sink_input *i) { + struct userdata *u; + + pa_sink_input_assert_ref(i); + pa_assert_ctl_context(); + pa_assert_se(u = i->userdata); + + teardown(u); + pa_module_unload_request(u->module, TRUE); +} + +/* Called from main thread */ +static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) { + struct userdata *u; + pa_proplist *p; + const char *n; + + pa_sink_input_assert_ref(i); + pa_assert_ctl_context(); + pa_assert_se(u = i->userdata); + + p = pa_proplist_new(); + pa_proplist_setf(p, PA_PROP_MEDIA_NAME, "Loopback to %s", pa_strnull(pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION))); + + if ((n = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_ICON_NAME))) + pa_proplist_sets(p, PA_PROP_MEDIA_ICON_NAME, n); + + pa_source_output_update_proplist(u->source_output, PA_UPDATE_REPLACE, p); + pa_proplist_free(p); +} + +/* Called from main thread */ +static pa_bool_t sink_input_may_move_to_cb(pa_sink_input *i, pa_sink *dest) { + struct userdata *u; + + pa_sink_input_assert_ref(i); + pa_assert_ctl_context(); + pa_assert_se(u = i->userdata); + + if (!u->source_output->source->monitor_of) + return TRUE; + + return dest != u->source_output->source->monitor_of; +} + +int pa__init(pa_module *m) { + pa_modargs *ma = NULL; + struct userdata *u; + pa_sink *sink; + pa_sink_input_new_data sink_input_data; + pa_source *source; + pa_source_output_new_data source_output_data; + uint32_t latency_msec; + pa_sample_spec ss; + pa_channel_map map; + pa_memchunk silence; + uint32_t adjust_time_sec; + const char *n; + + pa_assert(m); + + if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { + pa_log("Failed to parse module arguments"); + goto fail; + } + + if (!(source = pa_namereg_get(m->core, pa_modargs_get_value(ma, "source", NULL), PA_NAMEREG_SOURCE))) { + pa_log("No such source."); + goto fail; + } + + if (!(sink = pa_namereg_get(m->core, pa_modargs_get_value(ma, "sink", NULL), PA_NAMEREG_SINK))) { + pa_log("No such sink."); + goto fail; + } + + ss = sink->sample_spec; + map = sink->channel_map; + if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) { + pa_log("Invalid sample format specification or channel map"); + goto fail; + } + + latency_msec = DEFAULT_LATENCY_MSEC; + if (pa_modargs_get_value_u32(ma, "latency_msec", &latency_msec) < 0 || latency_msec < 1 || latency_msec > 2000) { + pa_log("Invalid latency specification"); + goto fail; + } + + m->userdata = u = pa_xnew0(struct userdata, 1); + u->core = m->core; + u->module = m; + u->latency = (pa_usec_t) latency_msec * PA_USEC_PER_MSEC; + + adjust_time_sec = DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC; + if (pa_modargs_get_value_u32(ma, "adjust_time", &adjust_time_sec) < 0) { + pa_log("Failed to parse adjust_time value"); + goto fail; + } + + if (adjust_time_sec != DEFAULT_ADJUST_TIME_USEC / PA_USEC_PER_SEC) + u->adjust_time = adjust_time_sec * PA_USEC_PER_SEC; + else + u->adjust_time = DEFAULT_ADJUST_TIME_USEC; + + pa_sink_input_new_data_init(&sink_input_data); + sink_input_data.driver = __FILE__; + sink_input_data.module = m; + sink_input_data.sink = sink; + + pa_proplist_setf(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "Loopback of %s", + pa_strnull(pa_proplist_gets(source->proplist, PA_PROP_DEVICE_DESCRIPTION))); + if ((n = pa_proplist_gets(source->proplist, PA_PROP_DEVICE_ICON_NAME))) + pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ICON_NAME, n); + pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "abstract"); + pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss); + pa_sink_input_new_data_set_channel_map(&sink_input_data, &map); + sink_input_data.flags = PA_SINK_INPUT_VARIABLE_RATE; + + pa_sink_input_new(&u->sink_input, m->core, &sink_input_data); + pa_sink_input_new_data_done(&sink_input_data); + + if (!u->sink_input) + goto fail; + + u->sink_input->parent.process_msg = sink_input_process_msg_cb; + u->sink_input->pop = sink_input_pop_cb; + u->sink_input->process_rewind = sink_input_process_rewind_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->update_max_rewind = sink_input_update_max_rewind_cb; + u->sink_input->update_max_request = sink_input_update_max_request_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_input_set_requested_latency(u->sink_input, u->latency/3); + + pa_source_output_new_data_init(&source_output_data); + source_output_data.driver = __FILE__; + source_output_data.module = m; + source_output_data.source = source; + pa_proplist_setf(source_output_data.proplist, PA_PROP_MEDIA_NAME, "Loopback to %s", + pa_strnull(pa_proplist_gets(sink->proplist, PA_PROP_DEVICE_DESCRIPTION))); + if ((n = pa_proplist_gets(sink->proplist, PA_PROP_DEVICE_ICON_NAME))) + pa_proplist_sets(source_output_data.proplist, PA_PROP_MEDIA_ICON_NAME, n); + pa_proplist_sets(source_output_data.proplist, PA_PROP_MEDIA_ROLE, "abstract"); + pa_source_output_new_data_set_sample_spec(&source_output_data, &ss); + pa_sink_input_new_data_set_channel_map(&sink_input_data, &map); + + pa_source_output_new(&u->source_output, m->core, &source_output_data); + pa_source_output_new_data_done(&source_output_data); + + if (!u->source_output) + goto fail; + + u->source_output->parent.process_msg = source_output_process_msg_cb; + u->source_output->push = source_output_push_cb; + u->source_output->process_rewind = source_output_process_rewind_cb; + u->source_output->kill = source_output_kill_cb; + u->source_output->attach = source_output_attach_cb; + u->source_output->detach = source_output_detach_cb; + u->source_output->state_change = source_output_state_change_cb; + u->source_output->may_move_to = source_output_may_move_to_cb; + u->source_output->moving = source_output_moving_cb; + u->source_output->userdata = u; + + pa_source_output_set_requested_latency(u->source_output, u->latency/3); + + pa_sink_input_get_silence(u->sink_input, &silence); + u->memblockq = pa_memblockq_new( + 0, /* idx */ + MEMBLOCKQ_MAXLENGTH, /* maxlength */ + MEMBLOCKQ_MAXLENGTH, /* tlength */ + pa_frame_size(&ss), /* base */ + 0, /* prebuf */ + 0, /* minreq */ + 0, /* maxrewind */ + &silence); /* silence frame */ + pa_memblock_unref(silence.memblock); + + u->asyncmsgq = pa_asyncmsgq_new(0); + + pa_sink_input_put(u->sink_input); + pa_source_output_put(u->source_output); + + if (u->adjust_time > 0) + u->time_event = pa_core_rttime_new(m->core, pa_rtclock_now() + u->adjust_time, time_callback, u); + + pa_modargs_free(ma); + return 0; + +fail: + if (ma) + pa_modargs_free(ma); + + pa__done(m); + + return -1; +} + +void pa__done(pa_module*m) { + struct userdata *u; + + pa_assert(m); + + if (!(u = m->userdata)) + return; + + teardown(u); + + if (u->memblockq) + pa_memblockq_free(u->memblockq); + + if (u->asyncmsgq) + pa_asyncmsgq_unref(u->asyncmsgq); + + if (u->time_event) + u->core->mainloop->time_free(u->time_event); + + pa_xfree(u); +} diff --git a/src/modules/module-remap-sink.c b/src/modules/module-remap-sink.c index becff55c..43748bd0 100644 --- a/src/modules/module-remap-sink.c +++ b/src/modules/module-remap-sink.c @@ -58,6 +58,8 @@ struct userdata { pa_sink *sink; pa_sink_input *sink_input; + + pa_bool_t auto_desc; }; static const char* const valid_modargs[] = { @@ -307,6 +309,18 @@ static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) { pa_sink_update_flags(u->sink, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY, dest->flags); } else pa_sink_set_asyncmsgq(u->sink, NULL); + + if (u->auto_desc && dest) { + const char *k; + pa_proplist *pl; + + pl = pa_proplist_new(); + k = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION); + pa_proplist_setf(pl, PA_PROP_DEVICE_DESCRIPTION, "Remapped %s", k ? k : dest->name); + + pa_sink_update_proplist(u->sink, PA_UPDATE_REPLACE, pl); + pa_proplist_free(pl); + } } int pa__init(pa_module*m) { @@ -314,7 +328,6 @@ int pa__init(pa_module*m) { pa_sample_spec ss; pa_channel_map sink_map, stream_map; pa_modargs *ma; - const char *k; pa_sink *master; pa_sink_input_new_data sink_input_data; pa_sink_new_data sink_data; @@ -370,8 +383,6 @@ int pa__init(pa_module*m) { sink_data.name = pa_sprintf_malloc("%s.remapped", master->name); pa_sink_new_data_set_sample_spec(&sink_data, &ss); pa_sink_new_data_set_channel_map(&sink_data, &sink_map); - k = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION); - pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Remapped %s", k ? k : 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"); @@ -381,6 +392,13 @@ int pa__init(pa_module*m) { goto fail; } + if ((u->auto_desc = !pa_proplist_contains(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) { + const char *k; + + k = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION); + pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Remapped %s", k ? k : master->name); + } + 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); @@ -406,8 +424,9 @@ int pa__init(pa_module*m) { 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); + sink_input_data.flags = (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(&u->sink_input, m->core, &sink_input_data); pa_sink_input_new_data_done(&sink_input_data); if (!u->sink_input) diff --git a/src/modules/module-sine.c b/src/modules/module-sine.c index 0be1d722..69b20028 100644 --- a/src/modules/module-sine.c +++ b/src/modules/module-sine.c @@ -163,7 +163,7 @@ int pa__init(pa_module*m) { pa_proplist_setf(data.proplist, "sine.hz", "%u", frequency); pa_sink_input_new_data_set_sample_spec(&data, &ss); - pa_sink_input_new(&u->sink_input, m->core, &data, 0); + pa_sink_input_new(&u->sink_input, m->core, &data); pa_sink_input_new_data_done(&data); if (!u->sink_input) diff --git a/src/modules/module-solaris.c b/src/modules/module-solaris.c index 71f14071..b0d4db43 100644 --- a/src/modules/module-solaris.c +++ b/src/modules/module-solaris.c @@ -600,6 +600,10 @@ static void process_rewind(struct userdata *u) { pa_log_debug("Requested to rewind %lu bytes.", (unsigned long) rewind_nbytes); rewind_nbytes = PA_MIN(u->memchunk.length, rewind_nbytes); u->memchunk.length -= rewind_nbytes; + if (u->memchunk.length <= 0 && u->memchunk.memblock) { + pa_memblock_unref(u->memchunk.memblock); + pa_memchunk_reset(&u->memchunk); + } pa_log_debug("Rewound %lu bytes.", (unsigned long) rewind_nbytes); } @@ -671,8 +675,8 @@ static void thread_func(void *userdata) { if (len < (size_t) u->minimum_request) break; - if (u->memchunk.length < len) - pa_sink_render(u->sink, len - u->memchunk.length, &u->memchunk); + if (!u->memchunk.length) + pa_sink_render(u->sink, u->sink->thread_info.max_request, &u->memchunk); len = PA_MIN(u->memchunk.length, len); diff --git a/src/modules/module-stream-restore.c b/src/modules/module-stream-restore.c index e560bd28..d6e3c153 100644 --- a/src/modules/module-stream-restore.c +++ b/src/modules/module-stream-restore.c @@ -934,6 +934,10 @@ static int extension_cb(pa_native_protocol *p, pa_module *m, pa_native_connectio data.data = &entry; data.size = sizeof(entry); + pa_log_debug("Client %s changes entry %s.", + pa_strnull(pa_proplist_gets(pa_native_connection_get_client(c)->proplist, PA_PROP_APPLICATION_PROCESS_BINARY)), + name); + if (pa_database_set(u->database, &key, &data, mode == PA_UPDATE_REPLACE) == 0) if (apply_immediately) apply_entry(u, name, &entry); diff --git a/src/modules/module-suspend-on-idle.c b/src/modules/module-suspend-on-idle.c index 70a7b049..7adaa0b1 100644 --- a/src/modules/module-suspend-on-idle.c +++ b/src/modules/module-suspend-on-idle.c @@ -145,6 +145,9 @@ static pa_hook_result_t sink_input_fixate_hook_cb(pa_core *c, pa_sink_input_new_ pa_assert(data); pa_assert(u); + if (data->flags & PA_SINK_INPUT_START_CORKED) + return PA_HOOK_OK; + if ((d = pa_hashmap_get(u->device_infos, data->sink))) resume(d); @@ -158,6 +161,9 @@ static pa_hook_result_t source_output_fixate_hook_cb(pa_core *c, pa_source_outpu pa_assert(data); pa_assert(u); + if (data->flags & PA_SOURCE_OUTPUT_START_CORKED) + return PA_HOOK_OK; + if (data->source->monitor_of) d = pa_hashmap_get(u->device_infos, data->source->monitor_of); else @@ -226,11 +232,16 @@ static pa_hook_result_t sink_input_move_start_hook_cb(pa_core *c, pa_sink_input static pa_hook_result_t sink_input_move_finish_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) { struct device_info *d; + pa_sink_input_state_t state; pa_assert(c); pa_sink_input_assert_ref(s); pa_assert(u); + state = pa_sink_input_get_state(s); + if (state != PA_SINK_INPUT_RUNNING && state != PA_SINK_INPUT_DRAINED) + return PA_HOOK_OK; + if ((d = pa_hashmap_get(u->device_infos, s->sink))) resume(d); @@ -265,6 +276,9 @@ static pa_hook_result_t source_output_move_finish_hook_cb(pa_core *c, pa_source_ pa_source_output_assert_ref(s); pa_assert(u); + if (pa_source_output_get_state(s) != PA_SOURCE_OUTPUT_RUNNING) + return PA_HOOK_OK; + if (s->source->monitor_of) d = pa_hashmap_get(u->device_infos, s->source->monitor_of); else @@ -279,6 +293,7 @@ static pa_hook_result_t source_output_move_finish_hook_cb(pa_core *c, pa_source_ static pa_hook_result_t sink_input_state_changed_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) { struct device_info *d; pa_sink_input_state_t state; + pa_assert(c); pa_sink_input_assert_ref(s); pa_assert(u); @@ -292,15 +307,11 @@ static pa_hook_result_t sink_input_state_changed_hook_cb(pa_core *c, pa_sink_inp } static pa_hook_result_t source_output_state_changed_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) { - pa_source_output_state_t state; - pa_assert(c); pa_source_output_assert_ref(s); pa_assert(u); - state = pa_source_output_get_state(s); - - if (state == PA_SOURCE_OUTPUT_RUNNING) { + if (pa_source_output_get_state(s) == PA_SOURCE_OUTPUT_RUNNING) { struct device_info *d; if (s->source->monitor_of) @@ -387,22 +398,17 @@ static pa_hook_result_t device_state_changed_hook_cb(pa_core *c, pa_object *o, s pa_sink *s = PA_SINK(o); pa_sink_state_t state = pa_sink_get_state(s); - if (pa_sink_check_suspend(s) <= 0) { - + if (pa_sink_check_suspend(s) <= 0) if (PA_SINK_IS_OPENED(state)) restart(d); - } - } else if (pa_source_isinstance(o)) { pa_source *s = PA_SOURCE(o); pa_source_state_t state = pa_source_get_state(s); - if (pa_source_check_suspend(s) <= 0) { - + if (pa_source_check_suspend(s) <= 0) if (PA_SOURCE_IS_OPENED(state)) restart(d); - } } return PA_HOOK_OK; diff --git a/src/modules/rtp/module-rtp-recv.c b/src/modules/rtp/module-rtp-recv.c index c195c045..1a05f57d 100644 --- a/src/modules/rtp/module-rtp-recv.c +++ b/src/modules/rtp/module-rtp-recv.c @@ -501,8 +501,9 @@ static struct session *session_new(struct userdata *u, const pa_sdp_info *sdp_in pa_proplist_setf(data.proplist, "rtp.payload", "%u", (unsigned) sdp_info->payload); data.module = u->module; pa_sink_input_new_data_set_sample_spec(&data, &sdp_info->sample_spec); + data.flags = PA_SINK_INPUT_VARIABLE_RATE; - pa_sink_input_new(&s->sink_input, u->module->core, &data, PA_SINK_INPUT_VARIABLE_RATE); + pa_sink_input_new(&s->sink_input, u->module->core, &data); pa_sink_input_new_data_done(&data); if (!s->sink_input) { diff --git a/src/modules/rtp/module-rtp-send.c b/src/modules/rtp/module-rtp-send.c index f147364d..8e1cfe36 100644 --- a/src/modules/rtp/module-rtp-send.c +++ b/src/modules/rtp/module-rtp-send.c @@ -330,8 +330,9 @@ int pa__init(pa_module*m) { data.source = s; pa_source_output_new_data_set_sample_spec(&data, &ss); pa_source_output_new_data_set_channel_map(&data, &cm); + data.flags = PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND; - pa_source_output_new(&o, m->core, &data, PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND); + pa_source_output_new(&o, m->core, &data); pa_source_output_new_data_done(&data); if (!o) { diff --git a/src/modules/rtp/rtsp_client.c b/src/modules/rtp/rtsp_client.c index 72d304e8..ba657f74 100644 --- a/src/modules/rtp/rtsp_client.c +++ b/src/modules/rtp/rtsp_client.c @@ -214,11 +214,13 @@ static void line_callback(pa_ioline *line, const char *s, void *userdata) { /* End of headers */ /* We will have a header left from our looping iteration, so add it in :) */ if (c->last_header) { + char *tmp = pa_strbuf_tostring_free(c->header_buffer); /* This is not a continuation header so let's dump it into our proplist */ - pa_headerlist_puts(c->response_headers, c->last_header, pa_strbuf_tostring_free(c->header_buffer)); + pa_headerlist_puts(c->response_headers, c->last_header, tmp); + pa_xfree(tmp); pa_xfree(c->last_header); c->last_header = NULL; - c->header_buffer= NULL; + c->header_buffer = NULL; } pa_log_debug("Full response received. Dispatching"); @@ -240,9 +242,11 @@ static void line_callback(pa_ioline *line, const char *s, void *userdata) { } if (c->last_header) { + char *tmp = pa_strbuf_tostring_free(c->header_buffer); /* This is not a continuation header so let's dump the full header/value into our proplist */ - pa_headerlist_puts(c->response_headers, c->last_header, pa_strbuf_tostring_free(c->header_buffer)); + pa_headerlist_puts(c->response_headers, c->last_header, tmp); + pa_xfree(tmp); pa_xfree(c->last_header); c->last_header = NULL; c->header_buffer = NULL; diff --git a/src/pulse/ext-stream-restore.c b/src/pulse/ext-stream-restore.c index 63c911f8..10e9fd5d 100644 --- a/src/pulse/ext-stream-restore.c +++ b/src/pulse/ext-stream-restore.c @@ -239,13 +239,10 @@ pa_operation *pa_ext_stream_restore_write( return o; fail: - if (o) { - pa_operation_cancel(o); - pa_operation_unref(o); - } + pa_operation_cancel(o); + pa_operation_unref(o); - if (t) - pa_tagstruct_free(t); + pa_tagstruct_free(t); pa_context_set_error(c, PA_ERR_INVALID); return NULL; @@ -290,13 +287,10 @@ pa_operation *pa_ext_stream_restore_delete( return o; fail: - if (o) { - pa_operation_cancel(o); - pa_operation_unref(o); - } + pa_operation_cancel(o); + pa_operation_unref(o); - if (t) - pa_tagstruct_free(t); + pa_tagstruct_free(t); pa_context_set_error(c, PA_ERR_INVALID); return NULL; diff --git a/src/pulse/proplist.c b/src/pulse/proplist.c index c904f533..048b241a 100644 --- a/src/pulse/proplist.c +++ b/src/pulse/proplist.c @@ -251,7 +251,7 @@ int pa_proplist_set(pa_proplist *p, const char *key, const void *data, size_t nb pa_assert(p); pa_assert(key); - pa_assert(data); + pa_assert(data || nbytes == 0); if (!property_name_valid(key)) return -1; @@ -264,7 +264,8 @@ int pa_proplist_set(pa_proplist *p, const char *key, const void *data, size_t nb pa_xfree(prop->value); prop->value = pa_xmalloc(nbytes+1); - memcpy(prop->value, data, nbytes); + if (nbytes > 0) + memcpy(prop->value, data, nbytes); ((char*) prop->value)[nbytes] = 0; prop->nbytes = nbytes; diff --git a/src/pulsecore/cli-command.c b/src/pulsecore/cli-command.c index 6ec74647..3c94960c 100644 --- a/src/pulsecore/cli-command.c +++ b/src/pulsecore/cli-command.c @@ -529,7 +529,7 @@ static int pa_cli_command_sink_volume(pa_core *c, pa_tokenizer *t, pa_strbuf *bu return -1; } - pa_cvolume_set(&cvolume, sink->sample_spec.channels, volume); + pa_cvolume_set(&cvolume, 1, volume); pa_sink_set_volume(sink, &cvolume, TRUE, TRUE); return 0; } @@ -571,7 +571,7 @@ static int pa_cli_command_sink_input_volume(pa_core *c, pa_tokenizer *t, pa_strb return -1; } - pa_cvolume_set(&cvolume, si->sample_spec.channels, volume); + pa_cvolume_set(&cvolume, 1, volume); pa_sink_input_set_volume(si, &cvolume, TRUE, TRUE); return 0; } @@ -607,7 +607,7 @@ static int pa_cli_command_source_volume(pa_core *c, pa_tokenizer *t, pa_strbuf * return -1; } - pa_cvolume_set(&cvolume, source->sample_spec.channels, volume); + pa_cvolume_set(&cvolume, 1, volume); pa_source_set_volume(source, &cvolume, 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))); + pa_strbuf_printf(buf, "set-sink-volume %s 0x%03x\n", sink->name, pa_cvolume_max(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)); } @@ -1598,7 +1598,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-source-volume %s 0x%03x\n", source->name, pa_cvolume_avg(pa_source_get_volume(source, FALSE))); + pa_strbuf_printf(buf, "set-source-volume %s 0x%03x\n", source->name, pa_cvolume_max(pa_source_get_volume(source, FALSE))); pa_strbuf_printf(buf, "set-source-mute %s %s\n", source->name, pa_yes_no(pa_source_get_mute(source, FALSE))); pa_strbuf_printf(buf, "suspend-source %s %s\n", source->name, pa_yes_no(pa_source_get_state(source) == PA_SOURCE_SUSPENDED)); } diff --git a/src/pulsecore/cli-text.c b/src/pulsecore/cli-text.c index c7a178d6..23a57d37 100644 --- a/src/pulsecore/cli-text.c +++ b/src/pulsecore/cli-text.c @@ -232,6 +232,7 @@ char *pa_sink_list_to_string(pa_core *c) { "\tflags: %s%s%s%s%s%s%s%s\n" "\tstate: %s\n" "\tsuspend cause: %s%s%s%s\n" + "\tpriority: %u\n" "\tvolume: %s%s%s\n" "\t balance %0.2f\n" "\tbase volume: %s%s%s\n" @@ -262,6 +263,7 @@ 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" : "", + sink->priority, 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)) : "", @@ -356,6 +358,7 @@ char *pa_source_list_to_string(pa_core *c) { "\tflags: %s%s%s%s%s%s%s\n" "\tstate: %s\n" "\tsuspend cause: %s%s%s%s\n" + "\tpriority: %u\n" "\tvolume: %s%s%s\n" "\t balance %0.2f\n" "\tbase volume: %s%s%s\n" @@ -383,6 +386,7 @@ char *pa_source_list_to_string(pa_core *c) { source->suspend_cause & PA_SUSPEND_APPLICATION ? "APPLICATION " : "", source->suspend_cause & PA_SUSPEND_IDLE ? "IDLE " : "", source->suspend_cause & PA_SUSPEND_SESSION ? "SESSION" : "", + source->priority, pa_cvolume_snprint(cv, sizeof(cv), pa_source_get_volume(source, FALSE)), source->flags & PA_SOURCE_DECIBEL_VOLUME ? "\n\t " : "", source->flags & PA_SOURCE_DECIBEL_VOLUME ? pa_sw_cvolume_snprint_dB(cvdb, sizeof(cvdb), pa_source_get_volume(source, FALSE)) : "", diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c index 1c8c6780..1daa46eb 100644 --- a/src/pulsecore/core-util.c +++ b/src/pulsecore/core-util.c @@ -116,6 +116,7 @@ #include <pulsecore/thread.h> #include <pulsecore/strbuf.h> #include <pulsecore/usergroup.h> +#include <pulsecore/strlist.h> #include "core-util.h" @@ -124,6 +125,8 @@ #define MSG_NOSIGNAL 0 #endif +static pa_strlist *recorded_env = NULL; + #ifdef OS_IS_WIN32 #define PULSE_ROOTENV "PULSE_ROOT" @@ -609,6 +612,11 @@ static int set_scheduler(int rtprio) { return -1; } + /* We need to disable exit on disconnect because otherwise + * dbus_shutdown will kill us. See + * https://bugs.freedesktop.org/show_bug.cgi?id=16924 */ + dbus_connection_set_exit_on_disconnect(bus, FALSE); + r = rtkit_make_realtime(bus, 0, rtprio); dbus_connection_unref(bus); @@ -677,6 +685,11 @@ static int set_nice(int nice_level) { return -1; } + /* We need to disable exit on disconnect because otherwise + * dbus_shutdown will kill us. See + * https://bugs.freedesktop.org/show_bug.cgi?id=16924 */ + dbus_connection_set_exit_on_disconnect(bus, FALSE); + r = rtkit_make_high_priority(bus, 0, nice_level); dbus_connection_unref(bus); @@ -1877,17 +1890,17 @@ char *pa_make_path_absolute(const char *p) { static char *get_path(const char *fn, pa_bool_t prependmid, pa_bool_t rt) { char *rtp; - if (pa_is_path_absolute(fn)) - return pa_xstrdup(fn); - rtp = rt ? pa_get_runtime_dir() : pa_get_state_dir(); - if (!rtp) - return NULL; - if (fn) { char *r; + if (pa_is_path_absolute(fn)) + return pa_xstrdup(fn); + + if (!rtp) + return NULL; + if (prependmid) { char *mid; @@ -2451,9 +2464,38 @@ void pa_set_env(const char *key, const char *value) { pa_assert(key); pa_assert(value); + /* This is not thread-safe */ + putenv(pa_sprintf_malloc("%s=%s", key, value)); } +void pa_set_env_and_record(const char *key, const char *value) { + pa_assert(key); + pa_assert(value); + + /* This is not thread-safe */ + + pa_set_env(key, value); + recorded_env = pa_strlist_prepend(recorded_env, key); +} + +void pa_unset_env_recorded(void) { + + /* This is not thread-safe */ + + for (;;) { + char *s; + + recorded_env = pa_strlist_pop(recorded_env, &s); + + if (!s) + break; + + unsetenv(s); + pa_xfree(s); + } +} + pa_bool_t pa_in_system_mode(void) { const char *e; diff --git a/src/pulsecore/core-util.h b/src/pulsecore/core-util.h index 2551f794..ccc9a38e 100644 --- a/src/pulsecore/core-util.h +++ b/src/pulsecore/core-util.h @@ -195,6 +195,8 @@ int pa_reset_sigs(int except, ...); int pa_reset_sigsv(const int except[]); void pa_set_env(const char *key, const char *value); +void pa_set_env_and_record(const char *key, const char *value); +void pa_unset_env_recorded(void); pa_bool_t pa_in_system_mode(void); diff --git a/src/pulsecore/cpu-x86.c b/src/pulsecore/cpu-x86.c index bc093ec0..1ba9f1a4 100644 --- a/src/pulsecore/cpu-x86.c +++ b/src/pulsecore/cpu-x86.c @@ -115,8 +115,11 @@ void pa_cpu_init_x86 (void) { pa_remap_func_init_mmx (flags); } - if (flags & PA_CPU_X86_SSE) + if (flags & PA_CPU_X86_SSE) { pa_volume_func_init_sse (flags); + pa_remap_func_init_sse (flags); + pa_convert_func_init_sse (flags); + } #endif /* defined (__i386__) || defined (__amd64__) */ } diff --git a/src/pulsecore/cpu-x86.h b/src/pulsecore/cpu-x86.h index b11ef6ea..b40eb5ce 100644 --- a/src/pulsecore/cpu-x86.h +++ b/src/pulsecore/cpu-x86.h @@ -64,5 +64,8 @@ void pa_volume_func_init_mmx(pa_cpu_x86_flag_t flags); void pa_volume_func_init_sse(pa_cpu_x86_flag_t flags); void pa_remap_func_init_mmx(pa_cpu_x86_flag_t flags); +void pa_remap_func_init_sse(pa_cpu_x86_flag_t flags); + +void pa_convert_func_init_sse (pa_cpu_x86_flag_t flags); #endif /* foocpux86hfoo */ diff --git a/src/pulsecore/llist.h b/src/pulsecore/llist.h index 58b51c68..27f174a9 100644 --- a/src/pulsecore/llist.h +++ b/src/pulsecore/llist.h @@ -107,4 +107,7 @@ #define PA_LLIST_FOREACH(i,head) \ for (i = (head); i; i = i->next) +#define PA_LLIST_FOREACH_SAFE(i,n,head) \ + for (i = (head); i && ((n = i->next), 1); i = n) + #endif diff --git a/src/pulsecore/macro.h b/src/pulsecore/macro.h index ce88c1b9..bffcc264 100644 --- a/src/pulsecore/macro.h +++ b/src/pulsecore/macro.h @@ -80,6 +80,12 @@ static inline size_t PA_PAGE_ALIGN(size_t l) { #define PA_ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0])) +#if defined(__GNUC__) + #define PA_DECLARE_ALIGNED(n,t,v) t v __attribute__ ((aligned (n))) +#else + #define PA_DECLARE_ALIGNED(n,t,v) t v +#endif + /* 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 @@ -157,6 +163,17 @@ static inline size_t PA_PAGE_ALIGN(size_t l) { #define PA_ROUND_DOWN(a, b) (((a) / (b)) * (b)) #endif +#ifdef __GNUC__ +#define PA_CLIP_SUB(a, b) \ + __extension__ ({ \ + typeof(a) _a = (a); \ + typeof(b) _b = (b); \ + _a > _b ? _a - _b : 0; \ + }) +#else +#define PA_CLIP_SUB(a, b) ((a) > (b) ? (a) - (b) : 0) +#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; diff --git a/src/pulsecore/namereg.c b/src/pulsecore/namereg.c index e26923d4..37755777 100644 --- a/src/pulsecore/namereg.c +++ b/src/pulsecore/namereg.c @@ -57,6 +57,8 @@ static pa_bool_t is_valid_char(char c) { pa_bool_t pa_namereg_is_valid_name(const char *name) { const char *c; + pa_assert(name); + if (*name == 0) return FALSE; @@ -70,6 +72,25 @@ pa_bool_t pa_namereg_is_valid_name(const char *name) { return TRUE; } +pa_bool_t pa_namereg_is_valid_name_or_wildcard(const char *name, pa_namereg_type_t type) { + + pa_assert(name); + + if (pa_namereg_is_valid_name(name)) + return TRUE; + + if (type == PA_NAMEREG_SINK && + pa_streq(name, "@DEFAULT_SINK@")) + return TRUE; + + if (type == PA_NAMEREG_SOURCE && + (pa_streq(name, "@DEFAULT_SOURCE@") || + pa_streq(name, "@DEFAULT_MONITOR@"))) + return TRUE; + + return FALSE; +} + char* pa_namereg_make_valid_name(const char *name) { const char *a; char *b, *n; @@ -191,7 +212,6 @@ void* pa_namereg_get(pa_core *c, const char *name, pa_namereg_type_t type) { if ((s = pa_namereg_get(c, NULL, PA_NAMEREG_SINK))) return s->monitor_source; - } if (!name) @@ -249,7 +269,7 @@ 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; + pa_sink *s, *best = NULL; uint32_t idx; pa_assert(c); @@ -257,18 +277,19 @@ pa_sink *pa_namereg_get_default_sink(pa_core *c) { if (c->default_sink && PA_SINK_IS_LINKED(pa_sink_get_state(c->default_sink))) return c->default_sink; - /* 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); + if (!best || s->priority > best->priority) + best = s; + + if (best) + return pa_namereg_set_default_sink(c, best); return NULL; } pa_source *pa_namereg_get_default_source(pa_core *c) { - pa_source *s; + pa_source *s, *best = NULL; uint32_t idx; pa_assert(c); @@ -279,12 +300,26 @@ pa_source *pa_namereg_get_default_source(pa_core *c) { /* 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 (!best || + s->priority > best->priority) + best = s; + + if (best) + return pa_namereg_set_default_source(c, best); /* 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); + if (!best || + s->priority > best->priority || + (s->priority == best->priority && + s->monitor_of && + best->monitor_of && + s->monitor_of->priority > best->monitor_of->priority)) + best = s; + + if (best) + return pa_namereg_set_default_source(c, best); return NULL; } diff --git a/src/pulsecore/namereg.h b/src/pulsecore/namereg.h index 38fae6f5..b5a976d7 100644 --- a/src/pulsecore/namereg.h +++ b/src/pulsecore/namereg.h @@ -45,6 +45,7 @@ pa_sink *pa_namereg_get_default_sink(pa_core *c); pa_source *pa_namereg_get_default_source(pa_core *c); pa_bool_t pa_namereg_is_valid_name(const char *name); +pa_bool_t pa_namereg_is_valid_name_or_wildcard(const char *name, pa_namereg_type_t type); char* pa_namereg_make_valid_name(const char *name); #endif diff --git a/src/pulsecore/pdispatch.c b/src/pulsecore/pdispatch.c index fc8ce76f..f15466ce 100644 --- a/src/pulsecore/pdispatch.c +++ b/src/pulsecore/pdispatch.c @@ -169,7 +169,7 @@ static const char *command_names[PA_COMMAND_MAX] = { /* Supported since protocol v14 (0.9.12) */ [PA_COMMAND_EXTENSION] = "EXTENSION", - + /* Supported since protocol v15 (0.9.15) */ [PA_COMMAND_GET_CARD_INFO] = "GET_CARD_INFO", [PA_COMMAND_GET_CARD_INFO_LIST] = "GET_CARD_INFO_LIST", [PA_COMMAND_SET_CARD_PROFILE] = "SET_CARD_PROFILE", @@ -180,7 +180,11 @@ static const char *command_names[PA_COMMAND_MAX] = { /* SERVER->CLIENT */ [PA_COMMAND_PLAYBACK_BUFFER_ATTR_CHANGED] = "PLAYBACK_BUFFER_ATTR_CHANGED", - [PA_COMMAND_RECORD_BUFFER_ATTR_CHANGED] = "RECORD_BUFFER_ATTR_CHANGED" + [PA_COMMAND_RECORD_BUFFER_ATTR_CHANGED] = "RECORD_BUFFER_ATTR_CHANGED", + + /* Supported since protocol v16 (0.9.16) */ + [PA_COMMAND_SET_SINK_PORT] = "SET_SINK_PORT", + [PA_COMMAND_SET_SOURCE_PORT] = "SET_SOURCE_PORT" }; #endif @@ -203,10 +207,10 @@ struct pa_pdispatch { const pa_pdispatch_cb_t *callback_table; unsigned n_commands; PA_LLIST_HEAD(struct reply_info, replies); - pa_pdispatch_drain_callback drain_callback; + pa_pdispatch_drain_cb_t drain_callback; void *drain_userdata; const pa_creds *creds; - pa_bool_t use_rtclock:1; + pa_bool_t use_rtclock; }; static void reply_info_free(struct reply_info *r) { @@ -225,19 +229,16 @@ static void reply_info_free(struct reply_info *r) { pa_pdispatch* pa_pdispatch_new(pa_mainloop_api *mainloop, pa_bool_t use_rtclock, const pa_pdispatch_cb_t *table, unsigned entries) { pa_pdispatch *pd; - pa_assert(mainloop); + pa_assert(mainloop); pa_assert((entries && table) || (!entries && !table)); - pd = pa_xnew(pa_pdispatch, 1); + pd = pa_xnew0(pa_pdispatch, 1); PA_REFCNT_INIT(pd); pd->mainloop = mainloop; pd->callback_table = table; pd->n_commands = entries; PA_LLIST_HEAD_INIT(struct reply_info, pd->replies); - pd->drain_callback = NULL; - pd->drain_userdata = NULL; - pd->creds = NULL; pd->use_rtclock = use_rtclock; return pd; @@ -317,7 +318,7 @@ int pa_pdispatch_run(pa_pdispatch *pd, pa_packet*packet, const pa_creds *creds, if (command == PA_COMMAND_ERROR || command == PA_COMMAND_REPLY) { struct reply_info *r; - for (r = pd->replies; r; r = r->next) + PA_LLIST_FOREACH(r, pd->replies) if (r->tag == tag) break; @@ -325,9 +326,9 @@ int pa_pdispatch_run(pa_pdispatch *pd, pa_packet*packet, const pa_creds *creds, run_action(pd, r, command, ts); } else if (pd->callback_table && (command < pd->n_commands) && pd->callback_table[command]) { - const pa_pdispatch_cb_t *c = pd->callback_table+command; + const pa_pdispatch_cb_t *cb = pd->callback_table+command; - (*c)(pd, command, tag, ts, userdata); + (*cb)(pd, command, tag, ts, userdata); } else { pa_log("Received unsupported command %u", command); goto finish; @@ -375,7 +376,9 @@ void pa_pdispatch_register_reply(pa_pdispatch *pd, uint32_t tag, int timeout, pa r->free_cb = free_cb; r->tag = tag; - pa_assert_se(r->time_event = pd->mainloop->time_new(pd->mainloop, pa_timeval_rtstore(&tv, pa_rtclock_now() + timeout * PA_USEC_PER_SEC, pd->use_rtclock), timeout_callback, r)); + pa_assert_se(r->time_event = pd->mainloop->time_new(pd->mainloop, + pa_timeval_rtstore(&tv, pa_rtclock_now() + timeout * PA_USEC_PER_SEC, pd->use_rtclock), + timeout_callback, r)); PA_LLIST_PREPEND(struct reply_info, pd->replies, r); } @@ -387,7 +390,7 @@ int pa_pdispatch_is_pending(pa_pdispatch *pd) { return !!pd->replies; } -void pa_pdispatch_set_drain_callback(pa_pdispatch *pd, void (*cb)(pa_pdispatch *pd, void *userdata), void *userdata) { +void pa_pdispatch_set_drain_callback(pa_pdispatch *pd, pa_pdispatch_drain_cb_t cb, void *userdata) { pa_assert(pd); pa_assert(PA_REFCNT_VALUE(pd) >= 1); pa_assert(!cb || pa_pdispatch_is_pending(pd)); @@ -402,12 +405,9 @@ void pa_pdispatch_unregister_reply(pa_pdispatch *pd, void *userdata) { pa_assert(pd); pa_assert(PA_REFCNT_VALUE(pd) >= 1); - for (r = pd->replies; r; r = n) { - n = r->next; - + PA_LLIST_FOREACH_SAFE(r, n, pd->replies) if (r->userdata == userdata) reply_info_free(r); - } } void pa_pdispatch_unref(pa_pdispatch *pd) { diff --git a/src/pulsecore/pdispatch.h b/src/pulsecore/pdispatch.h index dae475af..c5431c2e 100644 --- a/src/pulsecore/pdispatch.h +++ b/src/pulsecore/pdispatch.h @@ -35,9 +35,9 @@ typedef struct pa_pdispatch pa_pdispatch; typedef void (*pa_pdispatch_cb_t)(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata); -typedef void (*pa_pdispatch_drain_callback)(pa_pdispatch *pd, void *userdata); +typedef void (*pa_pdispatch_drain_cb_t)(pa_pdispatch *pd, void *userdata); -pa_pdispatch* pa_pdispatch_new(pa_mainloop_api *m, pa_bool_t use_rtclock, const pa_pdispatch_cb_t*table, unsigned entries); +pa_pdispatch* pa_pdispatch_new(pa_mainloop_api *m, pa_bool_t use_rtclock, const pa_pdispatch_cb_t *table, unsigned entries); void pa_pdispatch_unref(pa_pdispatch *pd); pa_pdispatch* pa_pdispatch_ref(pa_pdispatch *pd); @@ -47,7 +47,7 @@ void pa_pdispatch_register_reply(pa_pdispatch *pd, uint32_t tag, int timeout, pa int pa_pdispatch_is_pending(pa_pdispatch *pd); -void pa_pdispatch_set_drain_callback(pa_pdispatch *pd, pa_pdispatch_drain_callback callback, void *userdata); +void pa_pdispatch_set_drain_callback(pa_pdispatch *pd, pa_pdispatch_drain_cb_t callback, void *userdata); /* Remove all reply slots with the give userdata parameter */ void pa_pdispatch_unregister_reply(pa_pdispatch *pd, void *userdata); diff --git a/src/pulsecore/play-memblockq.c b/src/pulsecore/play-memblockq.c index b0d76993..f528c496 100644 --- a/src/pulsecore/play-memblockq.c +++ b/src/pulsecore/play-memblockq.c @@ -199,7 +199,7 @@ pa_sink_input* pa_memblockq_sink_input_new( pa_sink_input_new_data_set_volume(&data, volume); pa_proplist_update(data.proplist, PA_UPDATE_REPLACE, p); - pa_sink_input_new(&u->sink_input, sink->core, &data, 0); + pa_sink_input_new(&u->sink_input, sink->core, &data); pa_sink_input_new_data_done(&data); if (!u->sink_input) diff --git a/src/pulsecore/protocol-esound.c b/src/pulsecore/protocol-esound.c index cfbaee6f..480af6d5 100644 --- a/src/pulsecore/protocol-esound.c +++ b/src/pulsecore/protocol-esound.c @@ -429,7 +429,7 @@ static int esd_proto_stream_play(connection *c, esd_proto_t request, const void sdata.sink = sink; pa_sink_input_new_data_set_sample_spec(&sdata, &ss); - pa_sink_input_new(&c->sink_input, c->protocol->core, &sdata, 0); + pa_sink_input_new(&c->sink_input, c->protocol->core, &sdata); pa_sink_input_new_data_done(&sdata); CHECK_VALIDITY(c->sink_input, "Failed to create sink input."); @@ -525,7 +525,7 @@ static int esd_proto_stream_record(connection *c, esd_proto_t request, const voi sdata.source = source; pa_source_output_new_data_set_sample_spec(&sdata, &ss); - pa_source_output_new(&c->source_output, c->protocol->core, &sdata, 0); + pa_source_output_new(&c->source_output, c->protocol->core, &sdata); pa_source_output_new_data_done(&sdata); CHECK_VALIDITY(c->source_output, "Failed to create source output."); diff --git a/src/pulsecore/protocol-http.c b/src/pulsecore/protocol-http.c index 5220cc91..c09e5348 100644 --- a/src/pulsecore/protocol-http.c +++ b/src/pulsecore/protocol-http.c @@ -533,7 +533,7 @@ static void handle_listen_prefix(struct connection *c, const char *source_name) pa_source_output_new_data_set_sample_spec(&data, &ss); pa_source_output_new_data_set_channel_map(&data, &cm); - pa_source_output_new(&c->source_output, c->protocol->core, &data, 0); + pa_source_output_new(&c->source_output, c->protocol->core, &data); pa_source_output_new_data_done(&data); if (!c->source_output) { diff --git a/src/pulsecore/protocol-native.c b/src/pulsecore/protocol-native.c index 6678d847..d961dba2 100644 --- a/src/pulsecore/protocol-native.c +++ b/src/pulsecore/protocol-native.c @@ -648,8 +648,9 @@ static record_stream* record_stream_new( pa_source_output_new_data_set_channel_map(&data, map); if (peak_detect) data.resample_method = PA_RESAMPLER_PEAKS; + data.flags = flags; - *ret = -pa_source_output_new(&source_output, c->protocol->core, &data, flags); + *ret = -pa_source_output_new(&source_output, c->protocol->core, &data); pa_source_output_new_data_done(&data); @@ -1050,8 +1051,9 @@ static playback_stream* playback_stream_new( if (muted_set) pa_sink_input_new_data_set_muted(&data, muted); data.sync_base = ssync ? ssync->sink_input : NULL; + data.flags = flags; - *ret = -pa_sink_input_new(&sink_input, c->protocol->core, &data, flags); + *ret = -pa_sink_input_new(&sink_input, c->protocol->core, &data); pa_sink_input_new_data_done(&data); @@ -1289,7 +1291,8 @@ static void handle_seek(playback_stream *s, int64_t indexw) { pa_log_debug("Requesting rewind due to end of underrun."); pa_sink_input_request_rewind(s->sink_input, - (size_t) (s->sink_input->thread_info.underrun_for == (size_t) -1 ? 0 : s->sink_input->thread_info.underrun_for), + (size_t) (s->sink_input->thread_info.underrun_for == (uint64_t) -1 ? 0 : + s->sink_input->thread_info.underrun_for), FALSE, TRUE, FALSE); } @@ -1865,7 +1868,7 @@ static void command_create_playback_stream(pa_pdispatch *pd, uint32_t command, u } CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS); - CHECK_VALIDITY(c->pstream, !sink_name || pa_namereg_is_valid_name(sink_name), tag, PA_ERR_INVALID); + CHECK_VALIDITY(c->pstream, !sink_name || pa_namereg_is_valid_name_or_wildcard(sink_name, PA_NAMEREG_SINK), tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, sink_index == PA_INVALID_INDEX || !sink_name, tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, !sink_name || sink_index == PA_INVALID_INDEX, tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, pa_channel_map_valid(&map), tag, PA_ERR_INVALID); @@ -2114,7 +2117,7 @@ static void command_create_record_stream(pa_pdispatch *pd, uint32_t command, uin } CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS); - CHECK_VALIDITY(c->pstream, !source_name || pa_namereg_is_valid_name(source_name), tag, PA_ERR_INVALID); + CHECK_VALIDITY(c->pstream, !source_name || pa_namereg_is_valid_name_or_wildcard(source_name, PA_NAMEREG_SOURCE), tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, source_index == PA_INVALID_INDEX || !source_name, tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, !source_name || source_index == PA_INVALID_INDEX, tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, pa_sample_spec_valid(&ss), tag, PA_ERR_INVALID); @@ -2460,7 +2463,7 @@ static void command_lookup(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_ } CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS); - CHECK_VALIDITY(c->pstream, name && pa_namereg_is_valid_name(name), tag, PA_ERR_INVALID); + CHECK_VALIDITY(c->pstream, name && pa_namereg_is_valid_name_or_wildcard(name, command == PA_COMMAND_LOOKUP_SINK ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE), tag, PA_ERR_INVALID); if (command == PA_COMMAND_LOOKUP_SINK) { pa_sink *sink; @@ -2731,7 +2734,7 @@ static void command_play_sample(pa_pdispatch *pd, uint32_t command, uint32_t tag return; } - CHECK_VALIDITY(c->pstream, !sink_name || pa_namereg_is_valid_name(sink_name), tag, PA_ERR_INVALID); + CHECK_VALIDITY(c->pstream, !sink_name || pa_namereg_is_valid_name_or_wildcard(sink_name, PA_NAMEREG_SINK), tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, sink_index == PA_INVALID_INDEX || !sink_name, tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, !sink_name || sink_index == PA_INVALID_INDEX, tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, name && pa_namereg_is_valid_name(name), tag, PA_ERR_INVALID); @@ -3105,7 +3108,12 @@ static void command_get_info(pa_pdispatch *pd, uint32_t command, uint32_t tag, p } CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS); - CHECK_VALIDITY(c->pstream, !name || pa_namereg_is_valid_name(name), tag, PA_ERR_INVALID); + CHECK_VALIDITY(c->pstream, !name || + (command == PA_COMMAND_GET_SINK_INFO && + pa_namereg_is_valid_name_or_wildcard(name, PA_NAMEREG_SINK)) || + (command == PA_COMMAND_GET_SOURCE_INFO && + pa_namereg_is_valid_name_or_wildcard(name, PA_NAMEREG_SOURCE)) || + pa_namereg_is_valid_name(name), tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || name, tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, idx == PA_INVALID_INDEX || !name, tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, !name || idx == PA_INVALID_INDEX, tag, PA_ERR_INVALID); @@ -3347,7 +3355,7 @@ static void command_set_volume( } CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS); - CHECK_VALIDITY(c->pstream, !name || pa_namereg_is_valid_name(name), tag, PA_ERR_INVALID); + CHECK_VALIDITY(c->pstream, !name || pa_namereg_is_valid_name_or_wildcard(name, command == PA_COMMAND_SET_SINK_VOLUME ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE), tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || name, tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, idx == PA_INVALID_INDEX || !name, tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, !name || idx == PA_INVALID_INDEX, tag, PA_ERR_INVALID); @@ -3382,13 +3390,19 @@ static void command_set_volume( client_name = pa_strnull(pa_proplist_gets(c->client->proplist, PA_PROP_APPLICATION_PROCESS_BINARY)); if (sink) { + CHECK_VALIDITY(c->pstream, volume.channels == 1 || pa_cvolume_compatible(&volume, &sink->sample_spec), tag, PA_ERR_INVALID); + 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); + CHECK_VALIDITY(c->pstream, volume.channels == 1 || pa_cvolume_compatible(&volume, &source->sample_spec), tag, PA_ERR_INVALID); + + pa_log_debug("Client %s changes volume of source %s.", client_name, source->name); pa_source_set_volume(source, &volume, TRUE); } else if (si) { - pa_log_debug("Client %s changes volume of sink %s.", + CHECK_VALIDITY(c->pstream, volume.channels == 1 || pa_cvolume_compatible(&volume, &si->sample_spec), tag, PA_ERR_INVALID); + + pa_log_debug("Client %s changes volume of sink input %s.", client_name, pa_strnull(pa_proplist_gets(si->proplist, PA_PROP_MEDIA_NAME))); pa_sink_input_set_volume(si, &volume, TRUE, TRUE); @@ -3410,7 +3424,7 @@ static void command_set_mute( pa_sink *sink = NULL; pa_source *source = NULL; pa_sink_input *si = NULL; - const char *name = NULL; + const char *name = NULL, *client_name; pa_native_connection_assert_ref(c); pa_assert(t); @@ -3425,7 +3439,7 @@ static void command_set_mute( } CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS); - CHECK_VALIDITY(c->pstream, !name || pa_namereg_is_valid_name(name), tag, PA_ERR_INVALID); + CHECK_VALIDITY(c->pstream, !name || pa_namereg_is_valid_name_or_wildcard(name, command == PA_COMMAND_SET_SINK_MUTE ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE), tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || name, tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, idx == PA_INVALID_INDEX || !name, tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, !name || idx == PA_INVALID_INDEX, tag, PA_ERR_INVALID); @@ -3433,7 +3447,6 @@ static void command_set_mute( switch (command) { case PA_COMMAND_SET_SINK_MUTE: - if (idx != PA_INVALID_INDEX) sink = pa_idxset_get_by_index(c->protocol->core->sinks, idx); else @@ -3459,12 +3472,20 @@ static void command_set_mute( CHECK_VALIDITY(c->pstream, si || sink || source, tag, PA_ERR_NOENTITY); - if (sink) + client_name = pa_strnull(pa_proplist_gets(c->client->proplist, PA_PROP_APPLICATION_PROCESS_BINARY)); + + if (sink) { + pa_log_debug("Client %s changes mute of sink %s.", client_name, sink->name); pa_sink_set_mute(sink, mute, TRUE); - else if (source) + } else if (source) { + pa_log_debug("Client %s changes mute of source %s.", client_name, source->name); pa_source_set_mute(source, mute, TRUE); - else if (si) + } else if (si) { + pa_log_debug("Client %s changes mute of sink input %s.", + client_name, + pa_strnull(pa_proplist_gets(si->proplist, PA_PROP_MEDIA_NAME))); pa_sink_input_set_mute(si, mute, TRUE); + } pa_pstream_send_simple_ack(c->pstream, tag); } @@ -4083,7 +4104,7 @@ static void command_move_stream(pa_pdispatch *pd, uint32_t command, uint32_t tag CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS); CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX, tag, PA_ERR_INVALID); - CHECK_VALIDITY(c->pstream, !name_device || pa_namereg_is_valid_name(name_device), tag, PA_ERR_INVALID); + CHECK_VALIDITY(c->pstream, !name_device || pa_namereg_is_valid_name_or_wildcard(name_device, command == PA_COMMAND_MOVE_SINK_INPUT ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE), tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, idx_device != PA_INVALID_INDEX || name_device, tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, idx_device == PA_INVALID_INDEX || !name_device, tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, !name_device || idx_device == PA_INVALID_INDEX, tag, PA_ERR_INVALID); @@ -4147,7 +4168,7 @@ static void command_suspend(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa } CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS); - CHECK_VALIDITY(c->pstream, !name || pa_namereg_is_valid_name(name) || *name == 0, tag, PA_ERR_INVALID); + CHECK_VALIDITY(c->pstream, !name || pa_namereg_is_valid_name_or_wildcard(name, command == PA_COMMAND_SUSPEND_SINK ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE) || *name == 0, tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || name, tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, idx == PA_INVALID_INDEX || !name, tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, !name || idx == PA_INVALID_INDEX, tag, PA_ERR_INVALID); @@ -4307,7 +4328,7 @@ static void command_set_sink_or_source_port(pa_pdispatch *pd, uint32_t command, } CHECK_VALIDITY(c->pstream, c->authorized, tag, PA_ERR_ACCESS); - CHECK_VALIDITY(c->pstream, !name || pa_namereg_is_valid_name(name), tag, PA_ERR_INVALID); + CHECK_VALIDITY(c->pstream, !name || pa_namereg_is_valid_name_or_wildcard(name, command == PA_COMMAND_SET_SINK_PORT ? PA_NAMEREG_SINK : PA_NAMEREG_SOURCE), tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, idx != PA_INVALID_INDEX || name, tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, idx == PA_INVALID_INDEX || !name, tag, PA_ERR_INVALID); CHECK_VALIDITY(c->pstream, !name || idx == PA_INVALID_INDEX, tag, PA_ERR_INVALID); @@ -4842,3 +4863,9 @@ pa_pstream* pa_native_connection_get_pstream(pa_native_connection *c) { return c->pstream; } + +pa_client* pa_native_connection_get_client(pa_native_connection *c) { + pa_native_connection_assert_ref(c); + + return c->client; +} diff --git a/src/pulsecore/protocol-native.h b/src/pulsecore/protocol-native.h index 8a8d601c..97126274 100644 --- a/src/pulsecore/protocol-native.h +++ b/src/pulsecore/protocol-native.h @@ -47,7 +47,6 @@ typedef struct pa_native_options { char *auth_group; pa_ip_acl *auth_ip_acl; pa_auth_cookie *auth_cookie; - } pa_native_options; typedef enum pa_native_hook { @@ -80,6 +79,7 @@ int pa_native_protocol_install_ext(pa_native_protocol *p, pa_module *m, pa_nativ void pa_native_protocol_remove_ext(pa_native_protocol *p, pa_module *m); pa_pstream* pa_native_connection_get_pstream(pa_native_connection *c); +pa_client* pa_native_connection_get_client(pa_native_connection *c); pa_native_options* pa_native_options_new(void); pa_native_options* pa_native_options_ref(pa_native_options *o); diff --git a/src/pulsecore/protocol-simple.c b/src/pulsecore/protocol-simple.c index 95ec6ac8..d66db4b7 100644 --- a/src/pulsecore/protocol-simple.c +++ b/src/pulsecore/protocol-simple.c @@ -541,7 +541,7 @@ void pa_simple_protocol_connect(pa_simple_protocol *p, pa_iochannel *io, pa_simp pa_proplist_update(data.proplist, PA_UPDATE_MERGE, c->client->proplist); pa_sink_input_new_data_set_sample_spec(&data, &o->sample_spec); - pa_sink_input_new(&c->sink_input, p->core, &data, 0); + pa_sink_input_new(&c->sink_input, p->core, &data); pa_sink_input_new_data_done(&data); if (!c->sink_input) { @@ -593,7 +593,7 @@ void pa_simple_protocol_connect(pa_simple_protocol *p, pa_iochannel *io, pa_simp pa_proplist_update(data.proplist, PA_UPDATE_MERGE, c->client->proplist); pa_source_output_new_data_set_sample_spec(&data, &o->sample_spec); - pa_source_output_new(&c->source_output, p->core, &data, 0); + pa_source_output_new(&c->source_output, p->core, &data); pa_source_output_new_data_done(&data); if (!c->source_output) { @@ -627,8 +627,7 @@ void pa_simple_protocol_connect(pa_simple_protocol *p, pa_iochannel *io, pa_simp return; fail: - if (c) - connection_unlink(c); + connection_unlink(c); } void pa_simple_protocol_disconnect(pa_simple_protocol *p, pa_module *m) { diff --git a/src/pulsecore/remap_mmx.c b/src/pulsecore/remap_mmx.c index 00252dac..79e4f1fc 100644 --- a/src/pulsecore/remap_mmx.c +++ b/src/pulsecore/remap_mmx.c @@ -51,7 +51,7 @@ " punpckl"#s" %%mm4, %%mm4 \n\t" \ " punpckh"#s" %%mm5, %%mm5 \n\t" \ " punpckl"#s" %%mm6, %%mm6 \n\t" \ - " punpckh"#s" %%mm7, %%mm7 \n\t" \ + " punpckh"#s" %%mm7, %%mm7 \n\t" #define STORE_SAMPLES \ " movq %%mm0, (%0) \n\t" \ @@ -65,46 +65,53 @@ " add $32, %1 \n\t" \ " add $64, %0 \n\t" -#define HANDLE_SINGLE(s) \ +#define HANDLE_SINGLE_dq() \ " movd (%1), %%mm0 \n\t" \ - " movq %%mm0, %%mm1 \n\t" \ - " punpckl"#s" %%mm0, %%mm0 \n\t" \ + " punpckldq %%mm0, %%mm0 \n\t" \ " movq %%mm0, (%0) \n\t" \ " add $4, %1 \n\t" \ " add $8, %0 \n\t" -#define MONO_TO_STEREO(s) \ - " mov %3, %2 \n\t" \ - " sar $3, %2 \n\t" \ - " cmp $0, %2 \n\t" \ - " je 2f \n\t" \ - "1: \n\t" \ - LOAD_SAMPLES \ - UNPACK_SAMPLES(s) \ - STORE_SAMPLES \ - " dec %2 \n\t" \ - " jne 1b \n\t" \ - "2: \n\t" \ - " mov %3, %2 \n\t" \ - " and $7, %2 \n\t" \ - " je 4f \n\t" \ - "3: \n\t" \ - HANDLE_SINGLE(s) \ - " dec %2 \n\t" \ - " jne 3b \n\t" \ - "4: \n\t" \ +#define HANDLE_SINGLE_wd() \ + " movw (%1), %w3 \n\t" \ + " movd %3, %%mm0 \n\t" \ + " punpcklwd %%mm0, %%mm0 \n\t" \ + " movd %%mm0, (%0) \n\t" \ + " add $2, %1 \n\t" \ + " add $4, %0 \n\t" + +#define MONO_TO_STEREO(s,shift,mask) \ + " mov %4, %2 \n\t" \ + " sar $"#shift", %2 \n\t" \ + " cmp $0, %2 \n\t" \ + " je 2f \n\t" \ + "1: \n\t" \ + LOAD_SAMPLES \ + UNPACK_SAMPLES(s) \ + STORE_SAMPLES \ + " dec %2 \n\t" \ + " jne 1b \n\t" \ + "2: \n\t" \ + " mov %4, %2 \n\t" \ + " and $"#mask", %2 \n\t" \ + " je 4f \n\t" \ + "3: \n\t" \ + HANDLE_SINGLE_##s() \ + " dec %2 \n\t" \ + " jne 3b \n\t" \ + "4: \n\t" \ " emms \n\t" #if defined (__i386__) || defined (__amd64__) static void remap_mono_to_stereo_mmx (pa_remap_t *m, void *dst, const void *src, unsigned n) { - pa_reg_x86 temp; + pa_reg_x86 temp, temp2; switch (*m->format) { case PA_SAMPLE_FLOAT32NE: { __asm__ __volatile__ ( - MONO_TO_STEREO(dq) /* do doubles to quads */ - : "+r" (dst), "+r" (src), "=&r" (temp) + MONO_TO_STEREO(dq,3,7) /* do doubles to quads */ + : "+r" (dst), "+r" (src), "=&r" (temp), "=&r" (temp2) : "r" ((pa_reg_x86)n) : "cc" ); @@ -113,8 +120,8 @@ static void remap_mono_to_stereo_mmx (pa_remap_t *m, void *dst, const void *src, case PA_SAMPLE_S16NE: { __asm__ __volatile__ ( - MONO_TO_STEREO(wd) /* do words to doubles */ - : "+r" (dst), "+r" (src), "=&r" (temp) + MONO_TO_STEREO(wd,4,15) /* do words to doubles */ + : "+r" (dst), "+r" (src), "=&r" (temp), "=&r" (temp2) : "r" ((pa_reg_x86)n) : "cc" ); diff --git a/src/pulsecore/remap_sse.c b/src/pulsecore/remap_sse.c new file mode 100644 index 00000000..bf22df7c --- /dev/null +++ b/src/pulsecore/remap_sse.c @@ -0,0 +1,156 @@ +/*** + This file is part of PulseAudio. + + Copyright 2004-2006 Lennart Poettering + Copyright 2009 Wim Taymans <wim.taymans@collabora.co.uk.com> + + 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 <string.h> + +#include <pulse/sample.h> +#include <pulsecore/log.h> +#include <pulsecore/macro.h> + +#include "cpu-x86.h" +#include "remap.h" + +#define LOAD_SAMPLES \ + " movdqu (%1), %%xmm0 \n\t" \ + " movdqu 16(%1), %%xmm2 \n\t" \ + " movdqu 32(%1), %%xmm4 \n\t" \ + " movdqu 48(%1), %%xmm6 \n\t" \ + " movdqa %%xmm0, %%xmm1 \n\t" \ + " movdqa %%xmm2, %%xmm3 \n\t" \ + " movdqa %%xmm4, %%xmm5 \n\t" \ + " movdqa %%xmm6, %%xmm7 \n\t" + +#define UNPACK_SAMPLES(s) \ + " punpckl"#s" %%xmm0, %%xmm0 \n\t" \ + " punpckh"#s" %%xmm1, %%xmm1 \n\t" \ + " punpckl"#s" %%xmm2, %%xmm2 \n\t" \ + " punpckh"#s" %%xmm3, %%xmm3 \n\t" \ + " punpckl"#s" %%xmm4, %%xmm4 \n\t" \ + " punpckh"#s" %%xmm5, %%xmm5 \n\t" \ + " punpckl"#s" %%xmm6, %%xmm6 \n\t" \ + " punpckh"#s" %%xmm7, %%xmm7 \n\t" + +#define STORE_SAMPLES \ + " movdqu %%xmm0, (%0) \n\t" \ + " movdqu %%xmm1, 16(%0) \n\t" \ + " movdqu %%xmm2, 32(%0) \n\t" \ + " movdqu %%xmm3, 48(%0) \n\t" \ + " movdqu %%xmm4, 64(%0) \n\t" \ + " movdqu %%xmm5, 80(%0) \n\t" \ + " movdqu %%xmm6, 96(%0) \n\t" \ + " movdqu %%xmm7, 112(%0) \n\t" \ + " add $64, %1 \n\t" \ + " add $128, %0 \n\t" + +#define HANDLE_SINGLE_dq() \ + " movd (%1), %%xmm0 \n\t" \ + " punpckldq %%xmm0, %%xmm0 \n\t" \ + " movq %%xmm0, (%0) \n\t" \ + " add $4, %1 \n\t" \ + " add $8, %0 \n\t" + +#define HANDLE_SINGLE_wd() \ + " movw (%1), %w3 \n\t" \ + " movd %3, %%xmm0 \n\t" \ + " punpcklwd %%xmm0, %%xmm0 \n\t" \ + " movd %%xmm0, (%0) \n\t" \ + " add $2, %1 \n\t" \ + " add $4, %0 \n\t" + +#define MONO_TO_STEREO(s,shift,mask) \ + " mov %4, %2 \n\t" \ + " sar $"#shift", %2 \n\t" \ + " cmp $0, %2 \n\t" \ + " je 2f \n\t" \ + "1: \n\t" \ + LOAD_SAMPLES \ + UNPACK_SAMPLES(s) \ + STORE_SAMPLES \ + " dec %2 \n\t" \ + " jne 1b \n\t" \ + "2: \n\t" \ + " mov %4, %2 \n\t" \ + " and $"#mask", %2 \n\t" \ + " je 4f \n\t" \ + "3: \n\t" \ + HANDLE_SINGLE_##s() \ + " dec %2 \n\t" \ + " jne 3b \n\t" \ + "4: \n\t" + +#if defined (__i386__) || defined (__amd64__) +static void remap_mono_to_stereo_sse (pa_remap_t *m, void *dst, const void *src, unsigned n) { + pa_reg_x86 temp, temp2; + + switch (*m->format) { + case PA_SAMPLE_FLOAT32NE: + { + __asm__ __volatile__ ( + MONO_TO_STEREO(dq, 4, 15) /* do doubles to quads */ + : "+r" (dst), "+r" (src), "=&r" (temp), "=&r" (temp2) + : "r" ((pa_reg_x86)n) + : "cc" + ); + break; + } + case PA_SAMPLE_S16NE: + { + __asm__ __volatile__ ( + MONO_TO_STEREO(wd, 5, 31) /* do words to doubles */ + : "+r" (dst), "+r" (src), "=&r" (temp), "=&r" (temp2) + : "r" ((pa_reg_x86)n) + : "cc" + ); + break; + } + default: + pa_assert_not_reached(); + } +} + +/* set the function that will execute the remapping based on the matrices */ +static void init_remap_sse (pa_remap_t *m) { + unsigned n_oc, n_ic; + + n_oc = m->o_ss->channels; + n_ic = m->i_ss->channels; + + /* find some common channel remappings, fall back to full matrix operation. */ + if (n_ic == 1 && n_oc == 2 && + m->map_table_f[0][0] >= 1.0 && m->map_table_f[1][0] >= 1.0) { + m->do_remap = (pa_do_remap_func_t) remap_mono_to_stereo_sse; + pa_log_info("Using SSE mono to stereo remapping"); + } +} +#endif /* defined (__i386__) || defined (__amd64__) */ + +void pa_remap_func_init_sse (pa_cpu_x86_flag_t flags) { +#if defined (__i386__) || defined (__amd64__) + pa_log_info("Initialising SSE optimized remappers."); + + pa_set_init_remap_func ((pa_init_remap_func_t) init_remap_sse); +#endif /* defined (__i386__) || defined (__amd64__) */ +} diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c index f1bfa156..bed5a20d 100644 --- a/src/pulsecore/resampler.c +++ b/src/pulsecore/resampler.c @@ -299,8 +299,7 @@ pa_resampler* pa_resampler_new( return r; fail: - if (r) - pa_xfree(r); + pa_xfree(r); return NULL; } diff --git a/src/pulsecore/sample-util.c b/src/pulsecore/sample-util.c index 6e97e5a9..5fae1928 100644 --- a/src/pulsecore/sample-util.c +++ b/src/pulsecore/sample-util.c @@ -137,7 +137,7 @@ static void calc_linear_float_volume(float linear[], const pa_cvolume *volume) { static void calc_linear_integer_stream_volumes(pa_mix_info streams[], unsigned nstreams, const pa_cvolume *volume, const pa_sample_spec *spec) { unsigned k, channel; - float linear[PA_CHANNELS_MAX]; + float linear[PA_CHANNELS_MAX + VOLUME_PADDING]; pa_assert(streams); pa_assert(spec); @@ -156,7 +156,7 @@ static void calc_linear_integer_stream_volumes(pa_mix_info streams[], unsigned n static void calc_linear_float_stream_volumes(pa_mix_info streams[], unsigned nstreams, const pa_cvolume *volume, const pa_sample_spec *spec) { unsigned k, channel; - float linear[PA_CHANNELS_MAX]; + float linear[PA_CHANNELS_MAX + VOLUME_PADDING]; pa_assert(streams); pa_assert(spec); diff --git a/src/pulsecore/sconv.c b/src/pulsecore/sconv.c index d06d6985..301f08b4 100644 --- a/src/pulsecore/sconv.c +++ b/src/pulsecore/sconv.c @@ -52,8 +52,8 @@ static void u8_from_float32ne(unsigned n, const float *a, uint8_t *b) { for (; n > 0; n--, a++, b++) { float v; v = (*a * 127.0) + 128.0; - v = PA_CLAMP_UNLIKELY (v, 0.0, 255.0); - *b = rint (v); + v = PA_CLAMP_UNLIKELY (v, 0.0, 255.0); + *b = rint (v); } } diff --git a/src/pulsecore/sconv_sse.c b/src/pulsecore/sconv_sse.c new file mode 100644 index 00000000..7c3aa199 --- /dev/null +++ b/src/pulsecore/sconv_sse.c @@ -0,0 +1,233 @@ +/*** + This file is part of PulseAudio. + + Copyright 2004-2006 Lennart Poettering + Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB + + PulseAudio is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation; either version 2.1 of the License, + or (at your option) any later version. + + PulseAudio is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with PulseAudio; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stdio.h> +#include <stdlib.h> + +#include <pulsecore/g711.h> +#include <pulsecore/macro.h> + +#include "endianmacros.h" + +#include "cpu-x86.h" +#include "sconv.h" + +#if defined (__i386__) || defined (__amd64__) + +static const PA_DECLARE_ALIGNED (16, float, one[4]) = { 1.0, 1.0, 1.0, 1.0 }; +static const PA_DECLARE_ALIGNED (16, float, mone[4]) = { -1.0, -1.0, -1.0, -1.0 }; +static const PA_DECLARE_ALIGNED (16, float, scale[4]) = { 0x7fff, 0x7fff, 0x7fff, 0x7fff }; + +static void pa_sconv_s16le_from_f32ne_sse(unsigned n, const float *a, int16_t *b) { + pa_reg_x86 temp, i; + + __asm__ __volatile__ ( + " movaps %5, %%xmm5 \n\t" + " movaps %6, %%xmm6 \n\t" + " movaps %7, %%xmm7 \n\t" + " xor %0, %0 \n\t" + + " mov %4, %1 \n\t" + " sar $3, %1 \n\t" /* 8 floats at a time */ + " cmp $0, %1 \n\t" + " je 2f \n\t" + + "1: \n\t" + " movups (%2, %0, 2), %%xmm0 \n\t" /* read 8 floats */ + " movups 16(%2, %0, 2), %%xmm2 \n\t" + " minps %%xmm5, %%xmm0 \n\t" /* clamp to 1.0 */ + " minps %%xmm5, %%xmm2 \n\t" + " maxps %%xmm6, %%xmm0 \n\t" /* clamp to -1.0 */ + " maxps %%xmm6, %%xmm2 \n\t" + " mulps %%xmm7, %%xmm0 \n\t" /* *= 0x7fff */ + " mulps %%xmm7, %%xmm2 \n\t" + + " cvtps2pi %%xmm0, %%mm0 \n\t" /* low part to int */ + " cvtps2pi %%xmm2, %%mm2 \n\t" + " movhlps %%xmm0, %%xmm0 \n\t" /* bring high part in position */ + " movhlps %%xmm2, %%xmm2 \n\t" + " cvtps2pi %%xmm0, %%mm1 \n\t" /* high part to int */ + " cvtps2pi %%xmm2, %%mm3 \n\t" + + " packssdw %%mm1, %%mm0 \n\t" /* pack parts */ + " packssdw %%mm3, %%mm2 \n\t" + " movq %%mm0, (%3, %0) \n\t" + " movq %%mm2, 8(%3, %0) \n\t" + + " add $16, %0 \n\t" + " dec %1 \n\t" + " jne 1b \n\t" + + "2: \n\t" + " mov %4, %1 \n\t" /* prepare for leftovers */ + " and $7, %1 \n\t" + " je 4f \n\t" + + "3: \n\t" + " movss (%2, %0, 2), %%xmm0 \n\t" + " minss %%xmm5, %%xmm0 \n\t" + " maxss %%xmm6, %%xmm0 \n\t" + " mulss %%xmm7, %%xmm0 \n\t" + " cvtss2si %%xmm0, %4 \n\t" + " movw %w4, (%3, %0) \n\t" + " add $2, %0 \n\t" + " dec %1 \n\t" + " jne 3b \n\t" + + "4: \n\t" + " emms \n\t" + + : "=&r" (i), "=&r" (temp) + : "r" (a), "r" (b), "r" ((pa_reg_x86)n), "m" (*one), "m" (*mone), "m" (*scale) + : "cc", "memory" + ); +} + +static void pa_sconv_s16le_from_f32ne_sse2(unsigned n, const float *a, int16_t *b) { + pa_reg_x86 temp, i; + + __asm__ __volatile__ ( + " movaps %5, %%xmm5 \n\t" + " movaps %6, %%xmm6 \n\t" + " movaps %7, %%xmm7 \n\t" + " xor %0, %0 \n\t" + + " mov %4, %1 \n\t" + " sar $3, %1 \n\t" /* 8 floats at a time */ + " cmp $0, %1 \n\t" + " je 2f \n\t" + + "1: \n\t" + " movups (%2, %0, 2), %%xmm0 \n\t" /* read 8 floats */ + " movups 16(%2, %0, 2), %%xmm2 \n\t" + " minps %%xmm5, %%xmm0 \n\t" /* clamp to 1.0 */ + " minps %%xmm5, %%xmm2 \n\t" + " maxps %%xmm6, %%xmm0 \n\t" /* clamp to -1.0 */ + " maxps %%xmm6, %%xmm2 \n\t" + " mulps %%xmm7, %%xmm0 \n\t" /* *= 0x7fff */ + " mulps %%xmm7, %%xmm2 \n\t" + + " cvtps2dq %%xmm0, %%xmm0 \n\t" + " cvtps2dq %%xmm2, %%xmm2 \n\t" + + " packssdw %%xmm2, %%xmm0 \n\t" + " movdqu %%xmm0, (%3, %0) \n\t" + + " add $16, %0 \n\t" + " dec %1 \n\t" + " jne 1b \n\t" + + "2: \n\t" + " mov %4, %1 \n\t" /* prepare for leftovers */ + " and $7, %1 \n\t" + " je 4f \n\t" + + "3: \n\t" + " movss (%2, %0, 2), %%xmm0 \n\t" + " minss %%xmm5, %%xmm0 \n\t" + " maxss %%xmm6, %%xmm0 \n\t" + " mulss %%xmm7, %%xmm0 \n\t" + " cvtss2si %%xmm0, %4 \n\t" + " movw %w4, (%3, %0) \n\t" + " add $2, %0 \n\t" + " dec %1 \n\t" + " jne 3b \n\t" + + "4: \n\t" + + : "=&r" (i), "=&r" (temp) + : "r" (a), "r" (b), "r" ((pa_reg_x86)n), "m" (*one), "m" (*mone), "m" (*scale) + : "cc", "memory" + ); +} + +#undef RUN_TEST + +#ifdef RUN_TEST +#define SAMPLES 1019 +#define TIMES 1000 + +static void run_test (void) { + int16_t samples[SAMPLES]; + int16_t samples_ref[SAMPLES]; + float floats[SAMPLES]; + int i; + pa_usec_t start, stop; + pa_convert_func_t func; + + printf ("checking SSE %zd\n", sizeof (samples)); + + memset (samples_ref, 0, sizeof (samples_ref)); + memset (samples, 0, sizeof (samples)); + + for (i = 0; i < SAMPLES; i++) { + floats[i] = (rand()/(RAND_MAX+2.2)) - 1.1; + } + + func = pa_get_convert_from_float32ne_function (PA_SAMPLE_S16LE); + func (SAMPLES, floats, samples_ref); + pa_sconv_s16le_from_f32ne_sse2 (SAMPLES, floats, samples); + + for (i = 0; i < SAMPLES; i++) { + if (samples[i] != samples_ref[i]) { + printf ("%d: %04x != %04x (%f)\n", i, samples[i], samples_ref[i], + floats[i]); + } + } + + start = pa_rtclock_now(); + for (i = 0; i < TIMES; i++) { + pa_sconv_s16le_from_f32ne_sse2 (SAMPLES, floats, samples); + } + stop = pa_rtclock_now(); + pa_log_info("SSE: %llu usec.", (long long unsigned int)(stop - start)); + + start = pa_rtclock_now(); + for (i = 0; i < TIMES; i++) { + func (SAMPLES, floats, samples_ref); + } + stop = pa_rtclock_now(); + pa_log_info("ref: %llu usec.", (long long unsigned int)(stop - start)); +} +#endif +#endif /* defined (__i386__) || defined (__amd64__) */ + + +void pa_convert_func_init_sse (pa_cpu_x86_flag_t flags) { +#if defined (__i386__) || defined (__amd64__) + pa_log_info("Initialising SSE optimized conversions."); + +#ifdef RUN_TEST + run_test (); +#endif + + if (flags & PA_CPU_X86_SSE2) + pa_set_convert_from_float32ne_function (PA_SAMPLE_S16LE, (pa_convert_func_t) pa_sconv_s16le_from_f32ne_sse2); + else + pa_set_convert_from_float32ne_function (PA_SAMPLE_S16LE, (pa_convert_func_t) pa_sconv_s16le_from_f32ne_sse); + +#endif /* defined (__i386__) || defined (__amd64__) */ +} diff --git a/src/pulsecore/sink-input.c b/src/pulsecore/sink-input.c index 0ad95e6f..adda2aff 100644 --- a/src/pulsecore/sink-input.c +++ b/src/pulsecore/sink-input.c @@ -134,8 +134,7 @@ static void reset_callbacks(pa_sink_input *i) { int pa_sink_input_new( pa_sink_input **_i, pa_core *core, - pa_sink_input_new_data *data, - pa_sink_input_flags_t flags) { + pa_sink_input_new_data *data) { pa_sink_input *i; pa_resampler *resampler = NULL; @@ -198,15 +197,15 @@ int pa_sink_input_new( if (!data->muted_is_set) data->muted = FALSE; - if (flags & PA_SINK_INPUT_FIX_FORMAT) + if (data->flags & PA_SINK_INPUT_FIX_FORMAT) data->sample_spec.format = data->sink->sample_spec.format; - if (flags & PA_SINK_INPUT_FIX_RATE) + if (data->flags & PA_SINK_INPUT_FIX_RATE) data->sample_spec.rate = data->sink->sample_spec.rate; original_cm = data->channel_map; - if (flags & PA_SINK_INPUT_FIX_CHANNELS) { + if (data->flags & PA_SINK_INPUT_FIX_CHANNELS) { data->sample_spec.channels = data->sink->sample_spec.channels; data->channel_map = data->sink->channel_map; } @@ -225,7 +224,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_NO_CREATE_ON_SUSPEND) && + if ((data->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; @@ -236,7 +235,7 @@ int pa_sink_input_new( return -PA_ERR_TOOLARGE; } - if ((flags & PA_SINK_INPUT_VARIABLE_RATE) || + if ((data->flags & PA_SINK_INPUT_VARIABLE_RATE) || !pa_sample_spec_equal(&data->sample_spec, &data->sink->sample_spec) || !pa_channel_map_equal(&data->channel_map, &data->sink->channel_map)) { @@ -245,9 +244,9 @@ int pa_sink_input_new( &data->sample_spec, &data->channel_map, &data->sink->sample_spec, &data->sink->channel_map, data->resample_method, - ((flags & PA_SINK_INPUT_VARIABLE_RATE) ? PA_RESAMPLER_VARIABLE_RATE : 0) | - ((flags & PA_SINK_INPUT_NO_REMAP) ? PA_RESAMPLER_NO_REMAP : 0) | - (core->disable_remixing || (flags & PA_SINK_INPUT_NO_REMIX) ? PA_RESAMPLER_NO_REMIX : 0) | + ((data->flags & PA_SINK_INPUT_VARIABLE_RATE) ? PA_RESAMPLER_VARIABLE_RATE : 0) | + ((data->flags & PA_SINK_INPUT_NO_REMAP) ? PA_RESAMPLER_NO_REMAP : 0) | + (core->disable_remixing || (data->flags & PA_SINK_INPUT_NO_REMIX) ? PA_RESAMPLER_NO_REMIX : 0) | (core->disable_lfe_remixing ? PA_RESAMPLER_NO_LFE : 0)))) { pa_log_warn("Unsupported resampling operation."); return -PA_ERR_NOTSUPPORTED; @@ -260,7 +259,7 @@ int pa_sink_input_new( i->core = core; i->state = PA_SINK_INPUT_INIT; - i->flags = flags; + i->flags = data->flags; i->proplist = pa_proplist_copy(data->proplist); i->driver = pa_xstrdup(pa_path_get_filename(data->driver)); i->module = data->module; @@ -942,12 +941,22 @@ void pa_sink_input_set_volume(pa_sink_input *i, const pa_cvolume *volume, pa_boo pa_assert(PA_SINK_INPUT_IS_LINKED(i->state)); pa_assert(volume); pa_assert(pa_cvolume_valid(volume)); - pa_assert(pa_cvolume_compatible(volume, &i->sample_spec)); + pa_assert(volume->channels == 1 || pa_cvolume_compatible(volume, &i->sample_spec)); if ((i->sink->flags & PA_SINK_FLAT_VOLUME) && !absolute) { v = i->sink->reference_volume; pa_cvolume_remap(&v, &i->sink->channel_map, &i->channel_map); - volume = pa_sw_cvolume_multiply(&v, &v, volume); + + if (pa_cvolume_compatible(volume, &i->sample_spec)) + volume = pa_sw_cvolume_multiply(&v, &v, volume); + else + volume = pa_sw_cvolume_multiply_scalar(&v, &v, pa_cvolume_max(volume)); + } else { + + if (!pa_cvolume_compatible(volume, &i->sample_spec)) { + v = i->volume; + volume = pa_cvolume_scale(&v, pa_cvolume_max(volume)); + } } if (pa_cvolume_equal(volume, &i->volume)) { @@ -1472,7 +1481,13 @@ pa_bool_t pa_sink_input_safe_to_remove(pa_sink_input *i) { } /* Called from IO context */ -void pa_sink_input_request_rewind(pa_sink_input *i, size_t nbytes /* in our sample spec */, pa_bool_t rewrite, pa_bool_t flush, pa_bool_t dont_rewind_render) { +void pa_sink_input_request_rewind( + pa_sink_input *i, + size_t nbytes /* in our sample spec */, + pa_bool_t rewrite, + pa_bool_t flush, + pa_bool_t dont_rewind_render) { + size_t lbq; /* If 'rewrite' is TRUE the sink is rewound as far as requested @@ -1487,19 +1502,20 @@ void pa_sink_input_request_rewind(pa_sink_input *i, size_t nbytes /* in our sam * dont_rewind_render is TRUE then the render memblockq is not * rewound. */ + /* nbytes = 0 means maximum rewind request */ + pa_sink_input_assert_ref(i); pa_sink_input_assert_io_context(i); - - nbytes = PA_MAX(i->thread_info.rewrite_nbytes, nbytes); - -/* pa_log_debug("request rewrite %lu", (unsigned long) nbytes); */ + pa_assert(rewrite || flush); + pa_assert(!dont_rewind_render || !rewrite); /* We don't take rewind requests while we are corked */ if (i->thread_info.state == PA_SINK_INPUT_CORKED) return; - pa_assert(rewrite || flush); - pa_assert(!dont_rewind_render || !rewrite); + nbytes = PA_MAX(i->thread_info.rewrite_nbytes, nbytes); + + /* pa_log_debug("request rewrite %zu", nbytes); */ /* Calculate how much we can rewind locally without having to * touch the sink */ @@ -1519,6 +1535,7 @@ void pa_sink_input_request_rewind(pa_sink_input *i, size_t nbytes /* in our sam nbytes = pa_resampler_request(i->thread_info.resampler, nbytes); } + /* Remember how much we actually want to rewrite */ if (i->thread_info.rewrite_nbytes != (size_t) -1) { if (rewrite) { /* Make sure to not overwrite over underruns */ diff --git a/src/pulsecore/sink-input.h b/src/pulsecore/sink-input.h index fe6cf75c..59eabe36 100644 --- a/src/pulsecore/sink-input.h +++ b/src/pulsecore/sink-input.h @@ -212,7 +212,7 @@ struct pa_sink_input { pa_bool_t attached:1; /* True only between ->attach() and ->detach() calls */ - /* 0: rewrite nothing, (size_t) -1: rewrite everything, otherwise how many bytes to rewrite */ + /* rewrite_nbytes: 0: rewrite nothing, (size_t) -1: rewrite everything, otherwise how many bytes to rewrite */ pa_bool_t rewrite_flush:1, dont_rewind_render:1; size_t rewrite_nbytes; uint64_t underrun_for, playing_for; @@ -256,6 +256,8 @@ typedef struct pa_sink_input_send_event_hook_data { } pa_sink_input_send_event_hook_data; typedef struct pa_sink_input_new_data { + pa_sink_input_flags_t flags; + pa_proplist *proplist; const char *driver; @@ -298,8 +300,7 @@ void pa_sink_input_new_data_done(pa_sink_input_new_data *data); int pa_sink_input_new( pa_sink_input **i, pa_core *core, - pa_sink_input_new_data *data, - pa_sink_input_flags_t flags); + pa_sink_input_new_data *data); void pa_sink_input_put(pa_sink_input *i); void pa_sink_input_unlink(pa_sink_input* i); diff --git a/src/pulsecore/sink.c b/src/pulsecore/sink.c index 5cec7747..bda92fcc 100644 --- a/src/pulsecore/sink.c +++ b/src/pulsecore/sink.c @@ -236,6 +236,7 @@ pa_sink* pa_sink_new( s->core = core; s->state = PA_SINK_INIT; s->flags = flags; + s->priority = 0; s->suspend_cause = 0; s->name = pa_xstrdup(name); s->proplist = pa_proplist_copy(data->proplist); @@ -243,6 +244,8 @@ pa_sink* pa_sink_new( s->module = data->module; s->card = data->card; + s->priority = pa_device_init_priority(s->proplist); + s->sample_spec = data->sample_spec; s->channel_map = data->channel_map; @@ -775,7 +778,7 @@ static unsigned fill_mix_info(pa_sink *s, size_t *length, pa_mix_info *info, uns /* Called from IO thread context */ static void inputs_drop(pa_sink *s, pa_mix_info *info, unsigned n, pa_memchunk *result) { pa_sink_input *i; - void *state = NULL; + void *state; unsigned p = 0; unsigned n_unreffed = 0; @@ -787,7 +790,7 @@ static void inputs_drop(pa_sink *s, pa_mix_info *info, unsigned n, pa_memchunk * /* We optimize for the case where the order of the inputs has not changed */ - while ((i = pa_hashmap_iterate(s->thread_info.inputs, &state, NULL))) { + PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state) { unsigned j; pa_mix_info* m = NULL; @@ -881,8 +884,6 @@ void pa_sink_render(pa_sink*s, size_t length, pa_memchunk *result) { pa_assert(pa_frame_aligned(length, &s->sample_spec)); pa_assert(result); - pa_sink_ref(s); - pa_assert(!s->thread_info.rewind_requested); pa_assert(s->thread_info.rewind_nbytes == 0); @@ -893,6 +894,8 @@ void pa_sink_render(pa_sink*s, size_t length, pa_memchunk *result) { return; } + pa_sink_ref(s); + if (length <= 0) length = pa_frame_align(MIX_BUFFER_LENGTH, &s->sample_spec); @@ -970,8 +973,6 @@ void pa_sink_render_into(pa_sink*s, pa_memchunk *target) { pa_assert(target->length > 0); pa_assert(pa_frame_aligned(target->length, &s->sample_spec)); - pa_sink_ref(s); - pa_assert(!s->thread_info.rewind_requested); pa_assert(s->thread_info.rewind_nbytes == 0); @@ -980,6 +981,8 @@ void pa_sink_render_into(pa_sink*s, pa_memchunk *target) { return; } + pa_sink_ref(s); + length = target->length; block_size_max = pa_mempool_block_size_max(s->core->mempool); if (length > block_size_max) @@ -1054,11 +1057,16 @@ void pa_sink_render_into_full(pa_sink *s, pa_memchunk *target) { pa_assert(target->length > 0); pa_assert(pa_frame_aligned(target->length, &s->sample_spec)); - pa_sink_ref(s); - pa_assert(!s->thread_info.rewind_requested); pa_assert(s->thread_info.rewind_nbytes == 0); + if (s->thread_info.state == PA_SINK_SUSPENDED) { + pa_silence_memchunk(target, &s->sample_spec); + return; + } + + pa_sink_ref(s); + l = target->length; d = 0; while (l > 0) { @@ -1077,10 +1085,6 @@ void pa_sink_render_into_full(pa_sink *s, pa_memchunk *target) { /* Called from IO thread context */ void pa_sink_render_full(pa_sink *s, size_t length, pa_memchunk *result) { - pa_mix_info info[MAX_MIX_CHANNELS]; - size_t length1st = length; - unsigned n; - pa_sink_assert_ref(s); pa_sink_assert_io_context(s); pa_assert(PA_SINK_IS_LINKED(s->thread_info.state)); @@ -1088,81 +1092,24 @@ void pa_sink_render_full(pa_sink *s, size_t length, pa_memchunk *result) { pa_assert(pa_frame_aligned(length, &s->sample_spec)); pa_assert(result); - pa_sink_ref(s); - pa_assert(!s->thread_info.rewind_requested); pa_assert(s->thread_info.rewind_nbytes == 0); - pa_assert(length > 0); - - n = fill_mix_info(s, &length1st, info, MAX_MIX_CHANNELS); - - if (n == 0) { - pa_silence_memchunk_get(&s->core->silence_cache, - s->core->mempool, - result, - &s->sample_spec, - length1st); - } else if (n == 1) { - pa_cvolume volume; - - *result = info[0].chunk; - pa_memblock_ref(result->memblock); - - if (result->length > length) - result->length = length; - - pa_sw_cvolume_multiply(&volume, &s->thread_info.soft_volume, &info[0].volume); - - if (s->thread_info.soft_muted || !pa_cvolume_is_norm(&volume)) { - if (s->thread_info.soft_muted || pa_cvolume_is_muted(&volume)) { - pa_memblock_unref(result->memblock); - pa_silence_memchunk_get(&s->core->silence_cache, - s->core->mempool, - result, - &s->sample_spec, - result->length); - } else { - pa_memchunk_make_writable(result, length); - pa_volume_memchunk(result, &s->sample_spec, &volume); - } - } - } else { - void *ptr; - - result->index = 0; - result->memblock = pa_memblock_new(s->core->mempool, length); - - ptr = pa_memblock_acquire(result->memblock); - - result->length = pa_mix(info, n, - (uint8_t*) ptr + result->index, length1st, - &s->sample_spec, - &s->thread_info.soft_volume, - s->thread_info.soft_muted); - - pa_memblock_release(result->memblock); - } + pa_sink_ref(s); - inputs_drop(s, info, n, result); + pa_sink_render(s, length, result); if (result->length < length) { pa_memchunk chunk; - size_t l, d; + pa_memchunk_make_writable(result, length); - l = length - result->length; - d = result->index + result->length; - while (l > 0) { - chunk = *result; - chunk.index = d; - chunk.length = l; + chunk.memblock = result->memblock; + chunk.index = result->index + result->length; + chunk.length = length - result->length; - pa_sink_render_into(s, &chunk); + pa_sink_render_into_full(s, &chunk); - d += chunk.length; - l -= chunk.length; - } result->length = length; } @@ -1405,8 +1352,11 @@ void pa_sink_set_volume( pa_assert_ctl_context(); pa_assert(PA_SINK_IS_LINKED(s->state)); 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)); + pa_assert(!volume || volume->channels == 1 || pa_cvolume_compatible(volume, &s->sample_spec)); + + /* As a special exception we accept mono volumes on all sinks -- + * even on those with more complex channel maps */ /* If volume is NULL we synchronize the sink's real and reference * volumes with the stream volumes. If it is not NULL we update @@ -1416,7 +1366,10 @@ void pa_sink_set_volume( if (volume) { - s->reference_volume = *volume; + if (pa_cvolume_compatible(volume, &s->sample_spec)) + s->reference_volume = *volume; + else + pa_cvolume_scale(&s->reference_volume, pa_cvolume_max(volume)); if (s->flags & PA_SINK_FLAT_VOLUME) { /* OK, propagate this volume change back to the inputs */ @@ -2695,3 +2648,48 @@ pa_bool_t pa_device_init_intended_roles(pa_proplist *p) { return FALSE; } + +unsigned pa_device_init_priority(pa_proplist *p) { + const char *s; + unsigned priority = 0; + + pa_assert(p); + + if ((s = pa_proplist_gets(p, PA_PROP_DEVICE_CLASS))) { + + if (pa_streq(s, "sound")) + priority += 9000; + else if (!pa_streq(s, "modem")) + priority += 1000; + } + + if ((s = pa_proplist_gets(p, PA_PROP_DEVICE_FORM_FACTOR))) { + + if (pa_streq(s, "internal")) + priority += 900; + else if (pa_streq(s, "speaker")) + priority += 500; + else if (pa_streq(s, "headphone")) + priority += 400; + } + + if ((s = pa_proplist_gets(p, PA_PROP_DEVICE_BUS))) { + + if (pa_streq(s, "pci")) + priority += 50; + else if (pa_streq(s, "usb")) + priority += 40; + else if (pa_streq(s, "bluetooth")) + priority += 30; + } + + if ((s = pa_proplist_gets(p, PA_PROP_DEVICE_PROFILE_NAME))) { + + if (pa_startswith(s, "analog-")) + priority += 9; + else if (pa_startswith(s, "iec958-")) + priority += 8; + } + + return priority; +} diff --git a/src/pulsecore/sink.h b/src/pulsecore/sink.h index b5284b71..ba547fc3 100644 --- a/src/pulsecore/sink.h +++ b/src/pulsecore/sink.h @@ -109,6 +109,8 @@ struct pa_sink { pa_hashmap *ports; pa_device_port *active_port; + unsigned priority; + /* Called when the main loop requests a state change. Called from * main loop context. If returns -1 the state change will be * inhibited */ @@ -288,6 +290,7 @@ void pa_sink_update_flags(pa_sink *s, pa_sink_flags_t mask, pa_sink_flags_t valu 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); +unsigned pa_device_init_priority(pa_proplist *p); /**** May be called by everyone, from main context */ diff --git a/src/pulsecore/sound-file-stream.c b/src/pulsecore/sound-file-stream.c index f41c53f3..16de4923 100644 --- a/src/pulsecore/sound-file-stream.c +++ b/src/pulsecore/sound-file-stream.c @@ -311,7 +311,7 @@ int pa_play_file( pa_proplist_sets(data.proplist, PA_PROP_MEDIA_FILENAME, fname); pa_sndfile_init_proplist(u->sndfile, data.proplist); - pa_sink_input_new(&u->sink_input, sink->core, &data, 0); + pa_sink_input_new(&u->sink_input, sink->core, &data); pa_sink_input_new_data_done(&data); if (!u->sink_input) @@ -334,8 +334,7 @@ int pa_play_file( return 0; fail: - if (u) - file_stream_unref(u); + file_stream_unref(u); if (fd >= 0) pa_close(fd); diff --git a/src/pulsecore/source-output.c b/src/pulsecore/source-output.c index 43733400..1509807b 100644 --- a/src/pulsecore/source-output.c +++ b/src/pulsecore/source-output.c @@ -101,8 +101,7 @@ static void reset_callbacks(pa_source_output *o) { int pa_source_output_new( pa_source_output**_o, pa_core *core, - pa_source_output_new_data *data, - pa_source_output_flags_t flags) { + pa_source_output_new_data *data) { pa_source_output *o; pa_resampler *resampler = NULL; @@ -146,13 +145,13 @@ int pa_source_output_new( pa_return_val_if_fail(pa_channel_map_valid(&data->channel_map), -PA_ERR_INVALID); pa_return_val_if_fail(pa_channel_map_compatible(&data->channel_map, &data->sample_spec), -PA_ERR_INVALID); - if (flags & PA_SOURCE_OUTPUT_FIX_FORMAT) + if (data->flags & PA_SOURCE_OUTPUT_FIX_FORMAT) data->sample_spec.format = data->source->sample_spec.format; - if (flags & PA_SOURCE_OUTPUT_FIX_RATE) + if (data->flags & PA_SOURCE_OUTPUT_FIX_RATE) data->sample_spec.rate = data->source->sample_spec.rate; - if (flags & PA_SOURCE_OUTPUT_FIX_CHANNELS) { + if (data->flags & PA_SOURCE_OUTPUT_FIX_CHANNELS) { data->sample_spec.channels = data->source->sample_spec.channels; data->channel_map = data->source->channel_map; } @@ -168,7 +167,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_NO_CREATE_ON_SUSPEND) && + if ((data->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; @@ -179,7 +178,7 @@ int pa_source_output_new( return -PA_ERR_TOOLARGE; } - if ((flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) || + if ((data->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) || !pa_sample_spec_equal(&data->sample_spec, &data->source->sample_spec) || !pa_channel_map_equal(&data->channel_map, &data->source->channel_map)) { @@ -188,9 +187,9 @@ int pa_source_output_new( &data->source->sample_spec, &data->source->channel_map, &data->sample_spec, &data->channel_map, data->resample_method, - ((flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) ? PA_RESAMPLER_VARIABLE_RATE : 0) | - ((flags & PA_SOURCE_OUTPUT_NO_REMAP) ? PA_RESAMPLER_NO_REMAP : 0) | - (core->disable_remixing || (flags & PA_SOURCE_OUTPUT_NO_REMIX) ? PA_RESAMPLER_NO_REMIX : 0) | + ((data->flags & PA_SOURCE_OUTPUT_VARIABLE_RATE) ? PA_RESAMPLER_VARIABLE_RATE : 0) | + ((data->flags & PA_SOURCE_OUTPUT_NO_REMAP) ? PA_RESAMPLER_NO_REMAP : 0) | + (core->disable_remixing || (data->flags & PA_SOURCE_OUTPUT_NO_REMIX) ? PA_RESAMPLER_NO_REMIX : 0) | (core->disable_lfe_remixing ? PA_RESAMPLER_NO_LFE : 0)))) { pa_log_warn("Unsupported resampling operation."); return -PA_ERR_NOTSUPPORTED; @@ -203,7 +202,7 @@ int pa_source_output_new( o->core = core; o->state = PA_SOURCE_OUTPUT_INIT; - o->flags = flags; + o->flags = data->flags; o->proplist = pa_proplist_copy(data->proplist); o->driver = pa_xstrdup(pa_path_get_filename(data->driver)); o->module = data->module; diff --git a/src/pulsecore/source-output.h b/src/pulsecore/source-output.h index aca9ddf2..273b78fc 100644 --- a/src/pulsecore/source-output.h +++ b/src/pulsecore/source-output.h @@ -201,6 +201,8 @@ typedef struct pa_source_output_send_event_hook_data { } pa_source_output_send_event_hook_data; typedef struct pa_source_output_new_data { + pa_source_output_flags_t flags; + pa_proplist *proplist; pa_sink_input *direct_on_input; @@ -231,8 +233,7 @@ void pa_source_output_new_data_done(pa_source_output_new_data *data); int pa_source_output_new( pa_source_output**o, pa_core *core, - pa_source_output_new_data *data, - pa_source_output_flags_t flags); + pa_source_output_new_data *data); void pa_source_output_put(pa_source_output *o); void pa_source_output_unlink(pa_source_output*o); diff --git a/src/pulsecore/source.c b/src/pulsecore/source.c index 3026654e..415c54bc 100644 --- a/src/pulsecore/source.c +++ b/src/pulsecore/source.c @@ -205,6 +205,7 @@ pa_source* pa_source_new( s->core = core; s->state = PA_SOURCE_INIT; s->flags = flags; + s->priority = 0; s->suspend_cause = 0; s->name = pa_xstrdup(name); s->proplist = pa_proplist_copy(data->proplist); @@ -212,6 +213,8 @@ pa_source* pa_source_new( s->module = data->module; s->card = data->card; + s->priority = pa_device_init_priority(s->proplist); + s->sample_spec = data->sample_spec; s->channel_map = data->channel_map; @@ -757,15 +760,22 @@ void pa_source_set_volume( pa_bool_t save) { pa_bool_t real_changed; + pa_cvolume old_volume; pa_source_assert_ref(s); pa_assert_ctl_context(); pa_assert(PA_SOURCE_IS_LINKED(s->state)); pa_assert(pa_cvolume_valid(volume)); - pa_assert(pa_cvolume_compatible(volume, &s->sample_spec)); + pa_assert(volume->channels == 1 || pa_cvolume_compatible(volume, &s->sample_spec)); + + old_volume = s->volume; + + if (pa_cvolume_compatible(volume, &s->sample_spec)) + s->volume = *volume; + else + pa_cvolume_scale(&s->volume, pa_cvolume_max(volume)); - real_changed = !pa_cvolume_equal(volume, &s->volume); - s->volume = *volume; + real_changed = !pa_cvolume_equal(&old_volume, &s->volume); s->save_volume = (!real_changed && s->save_volume) || save; if (s->set_volume) { diff --git a/src/pulsecore/source.h b/src/pulsecore/source.h index df3f99df..e3e56bc4 100644 --- a/src/pulsecore/source.h +++ b/src/pulsecore/source.h @@ -96,6 +96,8 @@ struct pa_source { pa_hashmap *ports; pa_device_port *active_port; + unsigned priority; + /* Called when the main loop requests a state change. Called from * main loop context. If returns -1 the state change will be * inhibited */ diff --git a/src/pulsecore/start-child.c b/src/pulsecore/start-child.c index b3bce131..4a70aea1 100644 --- a/src/pulsecore/start-child.c +++ b/src/pulsecore/start-child.c @@ -86,6 +86,11 @@ int pa_start_child_for_read(const char *name, const char *argv1, pid_t *pid) { pa_reset_sigs(-1); pa_unblock_sigs(-1); pa_reset_priority(); + pa_unset_env_recorded(); + + /* Make sure our children are not influenced by the + * LD_BIND_NOW we set for ourselves. */ + unsetenv("LD_BIND_NOW"); #ifdef PR_SET_PDEATHSIG /* On Linux we can use PR_SET_PDEATHSIG to have the helper diff --git a/src/utils/pactl.c b/src/utils/pactl.c index c8c3a437..b8f4ea75 100644 --- a/src/utils/pactl.c +++ b/src/utils/pactl.c @@ -50,7 +50,6 @@ static pa_context *context = NULL; static pa_mainloop_api *mainloop_api = NULL; static char - *device = NULL, *sample_name = NULL, *sink_name = NULL, *source_name = NULL, @@ -66,6 +65,8 @@ static uint32_t static uint32_t module_index; static pa_bool_t suspend; +static pa_bool_t mute; +static pa_volume_t volume; static pa_proplist *proplist = NULL; @@ -74,7 +75,6 @@ static pa_stream *sample_stream = NULL; static pa_sample_spec sample_spec; static pa_channel_map channel_map; static size_t sample_length = 0; - static int actions = 1; static pa_bool_t nl = FALSE; @@ -95,7 +95,13 @@ static enum { SUSPEND_SOURCE, SET_CARD_PROFILE, SET_SINK_PORT, - SET_SOURCE_PORT + SET_SOURCE_PORT, + SET_SINK_VOLUME, + SET_SOURCE_VOLUME, + SET_SINK_INPUT_VOLUME, + SET_SINK_MUTE, + SET_SOURCE_MUTE, + SET_SINK_INPUT_MUTE } action = NONE; static void quit(int ret) { @@ -109,6 +115,7 @@ static void context_drain_complete(pa_context *c, void *userdata) { static void drain(void) { pa_operation *o; + if (!(o = pa_context_drain(context, context_drain_complete, NULL))) pa_context_disconnect(context); else @@ -125,7 +132,7 @@ static void complete_action(void) { static void stat_callback(pa_context *c, const pa_stat_info *i, void *userdata) { char s[128]; if (!i) { - pa_log(_("Failed to get statistics: %s\n"), pa_strerror(pa_context_errno(c))); + pa_log(_("Failed to get statistics: %s"), pa_strerror(pa_context_errno(c))); quit(1); return; } @@ -146,7 +153,7 @@ static void get_server_info_callback(pa_context *c, const pa_server_info *i, voi char ss[PA_SAMPLE_SPEC_SNPRINT_MAX], cm[PA_CHANNEL_MAP_SNPRINT_MAX]; if (!i) { - pa_log(_("Failed to get server information: %s\n"), pa_strerror(pa_context_errno(c))); + pa_log(_("Failed to get server information: %s"), pa_strerror(pa_context_errno(c))); quit(1); return; } @@ -195,7 +202,7 @@ static void get_sink_info_callback(pa_context *c, const pa_sink_info *i, int is_ char *pl; if (is_last < 0) { - pa_log(_("Failed to get sink information: %s\n"), pa_strerror(pa_context_errno(c))); + pa_log(_("Failed to get sink information: %s"), pa_strerror(pa_context_errno(c))); quit(1); return; } @@ -287,7 +294,7 @@ static void get_source_info_callback(pa_context *c, const pa_source_info *i, int char *pl; if (is_last < 0) { - pa_log(_("Failed to get source information: %s\n"), pa_strerror(pa_context_errno(c))); + pa_log(_("Failed to get source information: %s"), pa_strerror(pa_context_errno(c))); quit(1); return; } @@ -365,7 +372,7 @@ static void get_module_info_callback(pa_context *c, const pa_module_info *i, int char *pl; if (is_last < 0) { - pa_log(_("Failed to get module information: %s\n"), pa_strerror(pa_context_errno(c))); + pa_log(_("Failed to get module information: %s"), pa_strerror(pa_context_errno(c))); quit(1); return; } @@ -402,7 +409,7 @@ static void get_client_info_callback(pa_context *c, const pa_client_info *i, int char *pl; if (is_last < 0) { - pa_log(_("Failed to get client information: %s\n"), pa_strerror(pa_context_errno(c))); + pa_log(_("Failed to get client information: %s"), pa_strerror(pa_context_errno(c))); quit(1); return; } @@ -437,7 +444,7 @@ static void get_card_info_callback(pa_context *c, const pa_card_info *i, int is_ char *pl; if (is_last < 0) { - pa_log(_("Failed to get card information: %s\n"), pa_strerror(pa_context_errno(c))); + pa_log(_("Failed to get card information: %s"), pa_strerror(pa_context_errno(c))); complete_action(); return; } @@ -486,7 +493,7 @@ static void get_sink_input_info_callback(pa_context *c, const pa_sink_input_info char *pl; if (is_last < 0) { - pa_log(_("Failed to get sink input information: %s\n"), pa_strerror(pa_context_errno(c))); + pa_log(_("Failed to get sink input information: %s"), pa_strerror(pa_context_errno(c))); quit(1); return; } @@ -544,7 +551,7 @@ static void get_source_output_info_callback(pa_context *c, const pa_source_outpu char *pl; if (is_last < 0) { - pa_log(_("Failed to get source output information: %s\n"), pa_strerror(pa_context_errno(c))); + pa_log(_("Failed to get source output information: %s"), pa_strerror(pa_context_errno(c))); quit(1); return; } @@ -595,7 +602,7 @@ static void get_sample_info_callback(pa_context *c, const pa_sample_info *i, int char *pl; if (is_last < 0) { - pa_log(_("Failed to get sample information: %s\n"), pa_strerror(pa_context_errno(c))); + pa_log(_("Failed to get sample information: %s"), pa_strerror(pa_context_errno(c))); quit(1); return; } @@ -643,7 +650,7 @@ static void get_sample_info_callback(pa_context *c, const pa_sample_info *i, int static void simple_callback(pa_context *c, int success, void *userdata) { if (!success) { - pa_log(_("Failure: %s\n"), pa_strerror(pa_context_errno(c))); + pa_log(_("Failure: %s"), pa_strerror(pa_context_errno(c))); quit(1); return; } @@ -653,7 +660,7 @@ static void simple_callback(pa_context *c, int success, void *userdata) { static void index_callback(pa_context *c, uint32_t idx, void *userdata) { if (idx == PA_INVALID_INDEX) { - pa_log(_("Failure: %s\n"), pa_strerror(pa_context_errno(c))); + pa_log(_("Failure: %s"), pa_strerror(pa_context_errno(c))); quit(1); return; } @@ -677,7 +684,7 @@ static void stream_state_callback(pa_stream *s, void *userdata) { case PA_STREAM_FAILED: default: - pa_log(_("Failed to upload sample: %s\n"), pa_strerror(pa_context_errno(pa_stream_get_context(s)))); + pa_log(_("Failed to upload sample: %s"), pa_strerror(pa_context_errno(pa_stream_get_context(s)))); quit(1); } } @@ -694,7 +701,7 @@ static void stream_write_callback(pa_stream *s, size_t length, void *userdata) { if ((sf_readf_float(sndfile, d, l)) != l) { pa_xfree(d); - pa_log(_("Premature end of file\n")); + pa_log(_("Premature end of file")); quit(1); return; } @@ -726,7 +733,7 @@ static void context_state_callback(pa_context *c, void *userdata) { break; case PLAY_SAMPLE: - pa_operation_unref(pa_context_play_sample(c, sample_name, device, PA_VOLUME_NORM, simple_callback, NULL)); + pa_operation_unref(pa_context_play_sample(c, sample_name, sink_name, PA_VOLUME_NORM, simple_callback, NULL)); break; case REMOVE_SAMPLE: @@ -800,6 +807,42 @@ static void context_state_callback(pa_context *c, void *userdata) { pa_operation_unref(pa_context_set_source_port_by_name(c, source_name, port_name, simple_callback, NULL)); break; + case SET_SINK_MUTE: + pa_operation_unref(pa_context_set_sink_mute_by_name(c, sink_name, mute, simple_callback, NULL)); + break; + + case SET_SOURCE_MUTE: + pa_operation_unref(pa_context_set_source_mute_by_name(c, source_name, mute, simple_callback, NULL)); + break; + + case SET_SINK_INPUT_MUTE: + pa_operation_unref(pa_context_set_sink_input_mute(c, sink_input_idx, mute, simple_callback, NULL)); + break; + + case SET_SINK_VOLUME: { + pa_cvolume v; + + pa_cvolume_set(&v, 1, volume); + pa_operation_unref(pa_context_set_sink_volume_by_name(c, sink_name, &v, simple_callback, NULL)); + break; + } + + case SET_SOURCE_VOLUME: { + pa_cvolume v; + + pa_cvolume_set(&v, 1, volume); + pa_operation_unref(pa_context_set_source_volume_by_name(c, source_name, &v, simple_callback, NULL)); + break; + } + + case SET_SINK_INPUT_VOLUME: { + pa_cvolume v; + + pa_cvolume_set(&v, 1, volume); + pa_operation_unref(pa_context_set_sink_input_volume(c, sink_input_idx, &v, simple_callback, NULL)); + break; + } + default: pa_assert_not_reached(); } @@ -811,13 +854,13 @@ static void context_state_callback(pa_context *c, void *userdata) { case PA_CONTEXT_FAILED: default: - pa_log(_("Connection failure: %s\n"), pa_strerror(pa_context_errno(c))); + pa_log(_("Connection failure: %s"), pa_strerror(pa_context_errno(c))); quit(1); } } static void exit_signal_callback(pa_mainloop_api *m, pa_signal_event *e, int sig, void *userdata) { - pa_log(_("Got SIGINT, exiting.\n")); + pa_log(_("Got SIGINT, exiting.")); quit(0); } @@ -829,20 +872,30 @@ static void help(const char *argv0) { "%s [options] upload-sample FILENAME [NAME]\n" "%s [options] play-sample NAME [SINK]\n" "%s [options] remove-sample NAME\n" - "%s [options] move-sink-input ID SINK\n" - "%s [options] move-source-output ID SOURCE\n" + "%s [options] move-sink-input SINKINPUT SINK\n" + "%s [options] move-source-output SOURCEOUTPUT SOURCE\n" "%s [options] load-module NAME [ARGS ...]\n" - "%s [options] unload-module ID\n" - "%s [options] suspend-sink [SINK] 1|0\n" - "%s [options] suspend-source [SOURCE] 1|0\n" - "%s [options] set-card-profile [CARD] [PROFILE] \n" - "%s [options] set-sink-port [SINK] [PORT] \n" - "%s [options] set-source-port [SOURCE] [PORT] \n\n" + "%s [options] unload-module MODULE\n" + "%s [options] suspend-sink SINK 1|0\n" + "%s [options] suspend-source SOURCE 1|0\n" + "%s [options] set-card-profile CARD PROFILE\n" + "%s [options] set-sink-port SINK PORT\n" + "%s [options] set-source-port SOURCE PORT\n" + "%s [options] set-sink-volume SINK VOLUME\n" + "%s [options] set-source-volume SOURCE VOLUME\n" + "%s [options] set-sink-input-volume SINKINPUT VOLUME\n" + "%s [options] set-sink-mute SINK 1|0\n" + "%s [options] set-source-mute SOURCE 1|0\n" + "%s [options] set-sink-input-mute SINKINPUT 1|0\n\n" " -h, --help Show this help\n" " --version Show version\n\n" " -s, --server=SERVER The name of the server to connect to\n" " -n, --client-name=NAME How to call this client on the server\n"), - argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0, argv0); + argv0, argv0, argv0, argv0, argv0, + argv0, argv0, argv0, argv0, argv0, + argv0, argv0, argv0, argv0, argv0, + argv0, argv0, argv0, argv0, argv0, + argv0); } enum { @@ -897,7 +950,7 @@ int main(int argc, char *argv[]) { if (!(t = pa_locale_to_utf8(optarg)) || pa_proplist_sets(proplist, PA_PROP_APPLICATION_NAME, t) < 0) { - pa_log(_("Invalid client name '%s'\n"), t ? t : optarg); + pa_log(_("Invalid client name '%s'"), t ? t : optarg); pa_xfree(t); goto quit; } @@ -923,7 +976,7 @@ int main(int argc, char *argv[]) { action = UPLOAD_SAMPLE; if (optind+1 >= argc) { - pa_log(_("Please specify a sample file to load\n")); + pa_log(_("Please specify a sample file to load")); goto quit; } @@ -936,19 +989,19 @@ int main(int argc, char *argv[]) { pa_zero(sfi); if (!(sndfile = sf_open(argv[optind+1], SFM_READ, &sfi))) { - pa_log(_("Failed to open sound file.\n")); + pa_log(_("Failed to open sound file.")); goto quit; } if (pa_sndfile_read_sample_spec(sndfile, &sample_spec) < 0) { - pa_log(_("Failed to determine sample specification from file.\n")); + pa_log(_("Failed to determine sample specification from file.")); goto quit; } sample_spec.format = PA_SAMPLE_FLOAT32; if (pa_sndfile_read_channel_map(sndfile, &channel_map) < 0) { if (sample_spec.channels > 2) - pa_log(_("Warning: Failed to determine sample specification from file.\n")); + pa_log(_("Warning: Failed to determine sample specification from file.")); pa_channel_map_init_extend(&channel_map, sample_spec.channels, PA_CHANNEL_MAP_DEFAULT); } @@ -958,19 +1011,19 @@ int main(int argc, char *argv[]) { } else if (pa_streq(argv[optind], "play-sample")) { action = PLAY_SAMPLE; if (argc != optind+2 && argc != optind+3) { - pa_log(_("You have to specify a sample name to play\n")); + pa_log(_("You have to specify a sample name to play")); goto quit; } sample_name = pa_xstrdup(argv[optind+1]); if (optind+2 < argc) - device = pa_xstrdup(argv[optind+2]); + sink_name = pa_xstrdup(argv[optind+2]); } else if (pa_streq(argv[optind], "remove-sample")) { action = REMOVE_SAMPLE; if (argc != optind+2) { - pa_log(_("You have to specify a sample name to remove\n")); + pa_log(_("You have to specify a sample name to remove")); goto quit; } @@ -979,7 +1032,7 @@ int main(int argc, char *argv[]) { } else if (pa_streq(argv[optind], "move-sink-input")) { action = MOVE_SINK_INPUT; if (argc != optind+3) { - pa_log(_("You have to specify a sink input index and a sink\n")); + pa_log(_("You have to specify a sink input index and a sink")); goto quit; } @@ -989,7 +1042,7 @@ int main(int argc, char *argv[]) { } else if (pa_streq(argv[optind], "move-source-output")) { action = MOVE_SOURCE_OUTPUT; if (argc != optind+3) { - pa_log(_("You have to specify a source output index and a source\n")); + pa_log(_("You have to specify a source output index and a source")); goto quit; } @@ -1004,7 +1057,7 @@ int main(int argc, char *argv[]) { action = LOAD_MODULE; if (argc <= optind+1) { - pa_log(_("You have to specify a module name and arguments.\n")); + pa_log(_("You have to specify a module name and arguments.")); goto quit; } @@ -1024,7 +1077,7 @@ int main(int argc, char *argv[]) { action = UNLOAD_MODULE; if (argc != optind+2) { - pa_log(_("You have to specify a module index\n")); + pa_log(_("You have to specify a module index")); goto quit; } @@ -1034,7 +1087,7 @@ int main(int argc, char *argv[]) { action = SUSPEND_SINK; if (argc > optind+3 || optind+1 >= argc) { - pa_log(_("You may not specify more than one sink. You have to specify a boolean value.\n")); + pa_log(_("You may not specify more than one sink. You have to specify a boolean value.")); goto quit; } @@ -1047,7 +1100,7 @@ int main(int argc, char *argv[]) { action = SUSPEND_SOURCE; if (argc > optind+3 || optind+1 >= argc) { - pa_log(_("You may not specify more than one source. You have to specify a boolean value.\n")); + pa_log(_("You may not specify more than one source. You have to specify a boolean value.")); goto quit; } @@ -1059,7 +1112,7 @@ int main(int argc, char *argv[]) { action = SET_CARD_PROFILE; if (argc != optind+3) { - pa_log(_("You have to specify a card name/index and a profile name\n")); + pa_log(_("You have to specify a card name/index and a profile name")); goto quit; } @@ -1070,7 +1123,7 @@ int main(int argc, char *argv[]) { action = SET_SINK_PORT; if (argc != optind+3) { - pa_log(_("You have to specify a sink name/index and a port name\n")); + pa_log(_("You have to specify a sink name/index and a port name")); goto quit; } @@ -1081,13 +1134,123 @@ int main(int argc, char *argv[]) { action = SET_SOURCE_PORT; if (argc != optind+3) { - pa_log(_("You have to specify a source name/index and a port name\n")); + pa_log(_("You have to specify a source name/index and a port name")); goto quit; } source_name = pa_xstrdup(argv[optind+1]); port_name = pa_xstrdup(argv[optind+2]); + } else if (pa_streq(argv[optind], "set-sink-volume")) { + uint32_t v; + action = SET_SINK_VOLUME; + + if (argc != optind+3) { + pa_log(_("You have to specify a sink name/index and a volume")); + goto quit; + } + + if (pa_atou(argv[optind+2], &v) < 0) { + pa_log(_("Invalid volume specification")); + goto quit; + } + + sink_name = pa_xstrdup(argv[optind+1]); + volume = (pa_volume_t) v; + + } else if (pa_streq(argv[optind], "set-source-volume")) { + uint32_t v; + action = SET_SOURCE_VOLUME; + + if (argc != optind+3) { + pa_log(_("You have to specify a source name/index and a volume")); + goto quit; + } + + if (pa_atou(argv[optind+2], &v) < 0) { + pa_log(_("Invalid volume specification")); + goto quit; + } + + source_name = pa_xstrdup(argv[optind+1]); + volume = (pa_volume_t) v; + + } else if (pa_streq(argv[optind], "set-sink-input-volume")) { + uint32_t v; + action = SET_SINK_INPUT_VOLUME; + + if (argc != optind+3) { + pa_log(_("You have to specify a sink input index and a volume")); + goto quit; + } + + if (pa_atou(argv[optind+1], &sink_input_idx) < 0) { + pa_log(_("Invalid sink input index")); + goto quit; + } + + if (pa_atou(argv[optind+2], &v) < 0) { + pa_log(_("Invalid volume specification")); + goto quit; + } + + volume = (pa_volume_t) v; + + } else if (pa_streq(argv[optind], "set-sink-mute")) { + int b; + action = SET_SINK_MUTE; + + if (argc != optind+3) { + pa_log(_("You have to specify a sink name/index and a mute boolean")); + goto quit; + } + + if ((b = pa_parse_boolean(argv[optind+2])) < 0) { + pa_log(_("Invalid volume specification")); + goto quit; + } + + sink_name = pa_xstrdup(argv[optind+1]); + mute = b; + + } else if (pa_streq(argv[optind], "set-source-mute")) { + int b; + action = SET_SOURCE_MUTE; + + if (argc != optind+3) { + pa_log(_("You have to specify a source name/index and a mute boolean")); + goto quit; + } + + if ((b = pa_parse_boolean(argv[optind+2])) < 0) { + pa_log(_("Invalid volume specification")); + goto quit; + } + + source_name = pa_xstrdup(argv[optind+1]); + mute = b; + + } else if (pa_streq(argv[optind], "set-sink-input-mute")) { + int b; + action = SET_SINK_INPUT_MUTE; + + if (argc != optind+3) { + pa_log(_("You have to specify a sink input index and a mute boolean")); + goto quit; + } + + if (pa_atou(argv[optind+1], &sink_input_idx) < 0) { + pa_log(_("Invalid sink input index specification")); + goto quit; + } + + if ((b = pa_parse_boolean(argv[optind+2])) < 0) { + pa_log(_("Invalid volume specification")); + goto quit; + } + + mute = b; + } else if (pa_streq(argv[optind], "help")) { help(bn); ret = 0; @@ -1096,12 +1259,12 @@ int main(int argc, char *argv[]) { } if (action == NONE) { - pa_log(_("No valid command specified.\n")); + pa_log(_("No valid command specified.")); goto quit; } if (!(m = pa_mainloop_new())) { - pa_log(_("pa_mainloop_new() failed.\n")); + pa_log(_("pa_mainloop_new() failed.")); goto quit; } @@ -1113,7 +1276,7 @@ int main(int argc, char *argv[]) { pa_disable_sigpipe(); if (!(context = pa_context_new_with_proplist(mainloop_api, NULL, proplist))) { - pa_log(_("pa_context_new() failed.\n")); + pa_log(_("pa_context_new() failed.")); goto quit; } @@ -1124,7 +1287,7 @@ int main(int argc, char *argv[]) { } if (pa_mainloop_run(m, &ret) < 0) { - pa_log(_("pa_mainloop_run() failed.\n")); + pa_log(_("pa_mainloop_run() failed.")); goto quit; } @@ -1141,7 +1304,6 @@ quit: } pa_xfree(server); - pa_xfree(device); pa_xfree(sample_name); pa_xfree(sink_name); pa_xfree(source_name); |