summaryrefslogtreecommitdiffstats
path: root/src/pulsecore
diff options
context:
space:
mode:
Diffstat (limited to 'src/pulsecore')
-rw-r--r--src/pulsecore/atomic.h104
-rw-r--r--src/pulsecore/authkey.c10
-rw-r--r--src/pulsecore/cli-command.c2
-rw-r--r--src/pulsecore/conf-parser.c2
-rw-r--r--src/pulsecore/core-rtclock.c54
-rw-r--r--src/pulsecore/core-util.c235
-rw-r--r--src/pulsecore/core-util.h12
-rw-r--r--src/pulsecore/cpu-arm.c2
-rw-r--r--src/pulsecore/database-simple.c4
-rw-r--r--src/pulsecore/database-tdb.c43
-rw-r--r--src/pulsecore/envelope.c335
-rw-r--r--src/pulsecore/envelope.h3
-rw-r--r--src/pulsecore/fdsem.c13
-rw-r--r--src/pulsecore/lock-autospawn.c5
-rw-r--r--src/pulsecore/memblock.c2
-rw-r--r--src/pulsecore/pid.c7
-rw-r--r--src/pulsecore/protocol-dbus.c32
-rw-r--r--src/pulsecore/protocol-native.c31
-rw-r--r--src/pulsecore/random.c6
-rw-r--r--src/pulsecore/sample-util.c2
-rw-r--r--src/pulsecore/semaphore-osx.c63
-rw-r--r--src/pulsecore/sink-input.c394
-rw-r--r--src/pulsecore/sink-input.h21
-rw-r--r--src/pulsecore/sink.c32
-rw-r--r--src/pulsecore/socket-client.c4
-rw-r--r--src/pulsecore/socket-server.c16
-rw-r--r--src/pulsecore/socket-util.c30
-rw-r--r--src/pulsecore/sound-file-stream.c6
-rw-r--r--src/pulsecore/sound-file.c6
-rw-r--r--src/pulsecore/svolume_mmx.c11
-rw-r--r--src/pulsecore/svolume_sse.c10
-rw-r--r--src/pulsecore/time-smoother.c7
-rw-r--r--src/pulsecore/x11prop.c20
33 files changed, 1233 insertions, 291 deletions
diff --git a/src/pulsecore/atomic.h b/src/pulsecore/atomic.h
index 119c445b..51d08210 100644
--- a/src/pulsecore/atomic.h
+++ b/src/pulsecore/atomic.h
@@ -180,6 +180,110 @@ static inline pa_bool_t pa_atomic_ptr_cmpxchg(pa_atomic_ptr_t *a, void *old_p, v
return r == old_p;
}
+#elif defined(__FreeBSD__)
+
+#include <sys/cdefs.h>
+#include <sys/types.h>
+#include <sys/param.h>
+#include <machine/atomic.h>
+
+#if __FreeBSD_version < 600000
+#if defined(__i386__) || defined(__amd64__)
+#if defined(__amd64__)
+#define atomic_load_acq_64 atomic_load_acq_long
+#endif
+static inline u_int atomic_fetchadd_int(volatile u_int *p, u_int v) {
+ __asm __volatile(
+ " " __XSTRING(MPLOCKED) " "
+ " xaddl %0, %1 ; "
+ "# atomic_fetchadd_int"
+ : "+r" (v),
+ "=m" (*p)
+ : "m" (*p));
+
+ return (v);
+}
+#elif defined(__sparc64__)
+#define atomic_load_acq_64 atomic_load_acq_long
+#define atomic_fetchadd_int atomic_add_int
+#elif defined(__ia64__)
+#define atomic_load_acq_64 atomic_load_acq_long
+static inline uint32_t
+atomic_fetchadd_int(volatile uint32_t *p, uint32_t v) {
+ uint32_t value;
+
+ do {
+ value = *p;
+ } while (!atomic_cmpset_32(p, value, value + v));
+ return (value);
+}
+#endif
+#endif
+
+typedef struct pa_atomic {
+ volatile unsigned long value;
+} pa_atomic_t;
+
+#define PA_ATOMIC_INIT(v) { .value = (v) }
+
+static inline int pa_atomic_load(const pa_atomic_t *a) {
+ return (int) atomic_load_acq_int((unsigned int *) &a->value);
+}
+
+static inline void pa_atomic_store(pa_atomic_t *a, int i) {
+ atomic_store_rel_int((unsigned int *) &a->value, i);
+}
+
+static inline int pa_atomic_add(pa_atomic_t *a, int i) {
+ return atomic_fetchadd_int((unsigned int *) &a->value, i);
+}
+
+static inline int pa_atomic_sub(pa_atomic_t *a, int i) {
+ return atomic_fetchadd_int((unsigned int *) &a->value, -(i));
+}
+
+static inline int pa_atomic_inc(pa_atomic_t *a) {
+ return atomic_fetchadd_int((unsigned int *) &a->value, 1);
+}
+
+static inline int pa_atomic_dec(pa_atomic_t *a) {
+ return atomic_fetchadd_int((unsigned int *) &a->value, -1);
+}
+
+static inline int pa_atomic_cmpxchg(pa_atomic_t *a, int old_i, int new_i) {
+ return atomic_cmpset_int((unsigned int *) &a->value, old_i, new_i);
+}
+
+typedef struct pa_atomic_ptr {
+ volatile unsigned long value;
+} pa_atomic_ptr_t;
+
+#define PA_ATOMIC_PTR_INIT(v) { .value = (unsigned long) (v) }
+
+static inline void* pa_atomic_ptr_load(const pa_atomic_ptr_t *a) {
+#ifdef atomic_load_acq_64
+ return (void*) atomic_load_acq_ptr((unsigned long *) &a->value);
+#else
+ return (void*) atomic_load_acq_ptr((unsigned int *) &a->value);
+#endif
+}
+
+static inline void pa_atomic_ptr_store(pa_atomic_ptr_t *a, void *p) {
+#ifdef atomic_load_acq_64
+ atomic_store_rel_ptr(&a->value, (unsigned long) p);
+#else
+ atomic_store_rel_ptr((unsigned int *) &a->value, (unsigned int) p);
+#endif
+}
+
+static inline int pa_atomic_ptr_cmpxchg(pa_atomic_ptr_t *a, void *old_p, void* new_p) {
+#ifdef atomic_load_acq_64
+ return atomic_cmpset_ptr(&a->value, (unsigned long) old_p, (unsigned long) new_p);
+#else
+ return atomic_cmpset_ptr((unsigned int *) &a->value, (unsigned int) old_p, (unsigned int) new_p);
+#endif
+}
+
#elif defined(__GNUC__) && (defined(__amd64__) || defined(__x86_64__))
#warn "The native atomic operations implementation for AMD64 has not been tested thoroughly. libatomic_ops is known to not work properly on AMD64 and your gcc version is too old for the gcc-builtin atomic ops support. You have three options now: test the native atomic operations implementation for AMD64, fix libatomic_ops, or upgrade your GCC."
diff --git a/src/pulsecore/authkey.c b/src/pulsecore/authkey.c
index 15613e27..d671e367 100644
--- a/src/pulsecore/authkey.c
+++ b/src/pulsecore/authkey.c
@@ -70,10 +70,6 @@ static int generate(int fd, void *ret_data, size_t length) {
#define O_BINARY 0
#endif
-#ifndef O_NOCTTY
-#define O_NOCTTY 0
-#endif
-
/* Load an euthorization cookie from file fn and store it in data. If
* the cookie file doesn't exist, create it */
static int load(const char *fn, void *data, size_t length) {
@@ -86,9 +82,9 @@ static int load(const char *fn, void *data, size_t length) {
pa_assert(data);
pa_assert(length > 0);
- if ((fd = open(fn, O_RDWR|O_CREAT|O_BINARY|O_NOCTTY, S_IRUSR|S_IWUSR)) < 0) {
+ if ((fd = pa_open_cloexec(fn, O_RDWR|O_CREAT|O_BINARY, S_IRUSR|S_IWUSR)) < 0) {
- if (errno != EACCES || (fd = open(fn, O_RDONLY|O_BINARY|O_NOCTTY)) < 0) {
+ if (errno != EACCES || (fd = open(fn, O_RDONLY|O_BINARY)) < 0) {
pa_log_warn("Failed to open cookie file '%s': %s", fn, pa_cstrerror(errno));
goto finish;
} else
@@ -204,7 +200,7 @@ int pa_authkey_save(const char *fn, const void *data, size_t length) {
if (!(p = normalize_path(fn)))
return -2;
- if ((fd = open(p, O_RDWR|O_CREAT|O_NOCTTY, S_IRUSR|S_IWUSR)) < 0) {
+ if ((fd = pa_open_cloexec(p, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
pa_log_warn("Failed to open cookie file '%s': %s", fn, pa_cstrerror(errno));
goto finish;
}
diff --git a/src/pulsecore/cli-command.c b/src/pulsecore/cli-command.c
index b57919a4..82a44d84 100644
--- a/src/pulsecore/cli-command.c
+++ b/src/pulsecore/cli-command.c
@@ -1804,7 +1804,7 @@ int pa_cli_command_execute_file(pa_core *c, const char *fn, pa_strbuf *buf, pa_b
if (!fail)
fail = &_fail;
- if (!(f = fopen(fn, "r"))) {
+ if (!(f = pa_fopen_cloexec(fn, "r"))) {
pa_strbuf_printf(buf, "open('%s') failed: %s\n", fn, pa_cstrerror(errno));
if (!*fail)
ret = 0;
diff --git a/src/pulsecore/conf-parser.c b/src/pulsecore/conf-parser.c
index dd4a99ee..34b4d6fe 100644
--- a/src/pulsecore/conf-parser.c
+++ b/src/pulsecore/conf-parser.c
@@ -168,7 +168,7 @@ int pa_config_parse(const char *filename, FILE *f, const pa_config_item *t, void
pa_assert(filename);
pa_assert(t);
- if (!f && !(f = fopen(filename, "r"))) {
+ if (!f && !(f = pa_fopen_cloexec(filename, "r"))) {
if (errno == ENOENT) {
pa_log_debug("Failed to open configuration file '%s': %s", filename, pa_cstrerror(errno));
r = 0;
diff --git a/src/pulsecore/core-rtclock.c b/src/pulsecore/core-rtclock.c
index 1420470a..4fe0a47b 100644
--- a/src/pulsecore/core-rtclock.c
+++ b/src/pulsecore/core-rtclock.c
@@ -33,6 +33,12 @@
#include <sys/prctl.h>
#endif
+#ifdef OS_IS_DARWIN
+#include <CoreServices/CoreServices.h>
+#include <mach/mach.h>
+#include <mach/mach_time.h>
+#endif
+
#include <pulse/timeval.h>
#include <pulsecore/macro.h>
#include <pulsecore/core-error.h>
@@ -47,7 +53,8 @@ pa_usec_t pa_rtclock_age(const struct timeval *tv) {
}
struct timeval *pa_rtclock_get(struct timeval *tv) {
-#ifdef HAVE_CLOCK_GETTIME
+
+#if defined(HAVE_CLOCK_GETTIME)
struct timespec ts;
#ifdef CLOCK_MONOTONIC
@@ -59,7 +66,7 @@ struct timeval *pa_rtclock_get(struct timeval *tv) {
no_monotonic = TRUE;
if (no_monotonic)
-#endif
+#endif /* CLOCK_MONOTONIC */
pa_assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
pa_assert(tv);
@@ -69,7 +76,30 @@ struct timeval *pa_rtclock_get(struct timeval *tv) {
return tv;
-#else /* HAVE_CLOCK_GETTIME */
+#elif defined(OS_IS_DARWIN)
+ static mach_timebase_info_data_t tbi;
+ uint64_t nticks;
+ uint64_t time_nsec;
+
+ /* Refer Apple ADC QA1398
+ Also: http://devworld.apple.com/documentation/Darwin/Conceptual/KernelProgramming/services/services.html
+
+ Note: argument is timespec NOT timeval (timespec uses nsec, timeval uses usec)
+ */
+
+ /* try and be a mite efficient - maybe I should keep the N/D as a float !? */
+ if (tbi.denom == 0)
+ mach_timebase_info(&tbi);
+
+ nticks = mach_absolute_time();
+ time_nsec = nticks * tbi.numer / tbi.denom; // see above
+
+ tv->tv_sec = time_nsec / PA_NSEC_PER_SEC;
+ tv->tv_usec = time_nsec / PA_NSEC_PER_USEC;
+
+ return tv;
+
+#else /* OS_IS_DARWIN */
return pa_gettimeofday(tv);
@@ -77,19 +107,30 @@ struct timeval *pa_rtclock_get(struct timeval *tv) {
}
pa_bool_t pa_rtclock_hrtimer(void) {
-#ifdef HAVE_CLOCK_GETTIME
+
+#if defined(HAVE_CLOCK_GETTIME)
struct timespec ts;
#ifdef CLOCK_MONOTONIC
+
if (clock_getres(CLOCK_MONOTONIC, &ts) >= 0)
return ts.tv_sec == 0 && ts.tv_nsec <= (long) (PA_HRTIMER_THRESHOLD_USEC*PA_NSEC_PER_USEC);
-#endif
+#endif /* CLOCK_MONOTONIC */
pa_assert_se(clock_getres(CLOCK_REALTIME, &ts) == 0);
return ts.tv_sec == 0 && ts.tv_nsec <= (long) (PA_HRTIMER_THRESHOLD_USEC*PA_NSEC_PER_USEC);
-#else /* HAVE_CLOCK_GETTIME */
+#elif defined (OS_IS_DARWIN)
+ mach_timebase_info_data_t tbi;
+ uint64_t time_nsec;
+
+ mach_timebase_info(&tbi);
+ /* nsec = nticks * (N/D) - we want 1 tick == resolution !? */
+ time_nsec = tbi.numer / tbi.denom;
+ return time_nsec <= (long) (PA_HRTIMER_THRESHOLD_USEC*PA_NSEC_PER_USEC);
+
+#else /* OS_IS_DARWIN */
return FALSE;
#endif
@@ -98,6 +139,7 @@ pa_bool_t pa_rtclock_hrtimer(void) {
#define TIMER_SLACK_NS (int) ((500 * PA_NSEC_PER_USEC))
void pa_rtclock_hrtimer_enable(void) {
+
#ifdef PR_SET_TIMERSLACK
int slack_ns;
diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c
index 27e09cbc..d596c481 100644
--- a/src/pulsecore/core-util.c
+++ b/src/pulsecore/core-util.c
@@ -117,6 +117,7 @@
#include <pulsecore/strbuf.h>
#include <pulsecore/usergroup.h>
#include <pulsecore/strlist.h>
+#include <pulsecore/cpu-x86.h>
#include "core-util.h"
@@ -1200,10 +1201,7 @@ int pa_lock_lockfile(const char *fn) {
for (;;) {
struct stat st;
- if ((fd = open(fn, O_CREAT|O_RDWR
-#ifdef O_NOCTTY
- |O_NOCTTY
-#endif
+ if ((fd = pa_open_cloexec(fn, O_CREAT|O_RDWR
#ifdef O_NOFOLLOW
|O_NOFOLLOW
#endif
@@ -1603,7 +1601,7 @@ FILE *pa_open_config_file(const char *global, const char *local, const char *env
fn = buf;
#endif
- if ((f = fopen(fn, "r"))) {
+ if ((f = pa_fopen_cloexec(fn, "r"))) {
if (result)
*result = pa_xstrdup(fn);
@@ -1637,7 +1635,7 @@ FILE *pa_open_config_file(const char *global, const char *local, const char *env
fn = buf;
#endif
- if ((f = fopen(fn, "r"))) {
+ if ((f = pa_fopen_cloexec(fn, "r"))) {
if (result)
*result = pa_xstrdup(fn);
@@ -1664,7 +1662,7 @@ FILE *pa_open_config_file(const char *global, const char *local, const char *env
global = buf;
#endif
- if ((f = fopen(global, "r"))) {
+ if ((f = pa_fopen_cloexec(global, "r"))) {
if (result)
*result = pa_xstrdup(global);
@@ -2563,7 +2561,7 @@ char *pa_machine_id(void) {
* since it fits perfectly our needs and is not as volatile as the
* hostname which might be set from dhcp. */
- if ((f = fopen(PA_MACHINE_ID, "r"))) {
+ if ((f = pa_fopen_cloexec(PA_MACHINE_ID, "r"))) {
char ln[34] = "", *r;
r = fgets(ln, sizeof(ln)-1, f);
@@ -2889,3 +2887,224 @@ const char *pa_get_temp_dir(void) {
return "/tmp";
}
+
+int pa_open_cloexec(const char *fn, int flags, mode_t mode) {
+ int fd;
+
+#ifdef O_NOCTTY
+ flags |= O_NOCTTY;
+#endif
+
+#ifdef O_CLOEXEC
+ if ((fd = open(fn, flags|O_CLOEXEC, mode)) >= 0)
+ goto finish;
+
+ if (errno != EINVAL)
+ return fd;
+#endif
+
+ if ((fd = open(fn, flags, mode)) < 0)
+ return fd;
+
+finish:
+ /* Some implementations might simply ignore O_CLOEXEC if it is not
+ * understood, make sure FD_CLOEXEC is enabled anyway */
+
+ pa_make_fd_cloexec(fd);
+ return fd;
+}
+
+int pa_socket_cloexec(int domain, int type, int protocol) {
+ int fd;
+
+#ifdef SOCK_CLOEXEC
+ if ((fd = socket(domain, type | SOCK_CLOEXEC, protocol)) >= 0)
+ goto finish;
+
+ if (errno != EINVAL)
+ return fd;
+#endif
+
+ if ((fd = socket(domain, type, protocol)) < 0)
+ return fd;
+
+finish:
+ /* Some implementations might simply ignore SOCK_CLOEXEC if it is
+ * not understood, make sure FD_CLOEXEC is enabled anyway */
+
+ pa_make_fd_cloexec(fd);
+ return fd;
+}
+
+int pa_pipe_cloexec(int pipefd[2]) {
+ int r;
+
+#ifdef HAVE_PIPE2
+ if ((r = pipe2(pipefd, O_CLOEXEC)) >= 0)
+ goto finish;
+
+ if (errno != EINVAL && errno != ENOSYS)
+ return r;
+
+#endif
+
+ if ((r = pipe(pipefd)) < 0)
+ return r;
+
+finish:
+ pa_make_fd_cloexec(pipefd[0]);
+ pa_make_fd_cloexec(pipefd[1]);
+
+ return 0;
+}
+
+int pa_accept_cloexec(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
+ int fd;
+
+#ifdef HAVE_ACCEPT4
+ if ((fd = accept4(sockfd, addr, addrlen, SOCK_CLOEXEC)) >= 0)
+ goto finish;
+
+ if (errno != EINVAL && errno != ENOSYS)
+ return fd;
+
+#endif
+
+ if ((fd = accept(sockfd, addr, addrlen)) < 0)
+ return fd;
+
+finish:
+ pa_make_fd_cloexec(fd);
+ return fd;
+}
+
+FILE* pa_fopen_cloexec(const char *path, const char *mode) {
+ FILE *f;
+ char *m;
+
+ m = pa_sprintf_malloc("%se", mode);
+
+ errno = 0;
+ if ((f = fopen(path, m))) {
+ pa_xfree(m);
+ goto finish;
+ }
+
+ pa_xfree(m);
+
+ if (errno != EINVAL)
+ return NULL;
+
+ if (!(f = fopen(path, mode)))
+ return NULL;
+
+finish:
+ pa_make_fd_cloexec(fileno(f));
+ return f;
+}
+
+void pa_nullify_stdfds(void) {
+
+#ifndef OS_IS_WIN32
+ pa_close(STDIN_FILENO);
+ pa_close(STDOUT_FILENO);
+ pa_close(STDERR_FILENO);
+
+ pa_assert_se(open("/dev/null", O_RDONLY) == STDIN_FILENO);
+ pa_assert_se(open("/dev/null", O_WRONLY) == STDOUT_FILENO);
+ pa_assert_se(open("/dev/null", O_WRONLY) == STDERR_FILENO);
+#else
+ FreeConsole();
+#endif
+
+}
+
+char *pa_read_line_from_file(const char *fn) {
+ FILE *f;
+ char ln[256] = "", *r;
+
+ if (!(f = pa_fopen_cloexec(fn, "r")))
+ return NULL;
+
+ r = fgets(ln, sizeof(ln)-1, f);
+ fclose(f);
+
+ if (!r) {
+ errno = EIO;
+ return NULL;
+ }
+
+ pa_strip_nl(ln);
+ return pa_xstrdup(ln);
+}
+
+pa_bool_t pa_running_in_vm(void) {
+
+#if defined(__i386__) || defined(__x86_64__)
+
+ /* Both CPUID and DMI are x86 specific interfaces... */
+
+ uint32_t eax = 0x40000000;
+ union {
+ uint32_t sig32[3];
+ char text[13];
+ } sig;
+
+#ifdef __linux__
+ const char *const dmi_vendors[] = {
+ "/sys/class/dmi/id/sys_vendor",
+ "/sys/class/dmi/id/board_vendor",
+ "/sys/class/dmi/id/bios_vendor"
+ };
+
+ unsigned i;
+
+ for (i = 0; i < PA_ELEMENTSOF(dmi_vendors); i++) {
+ char *s;
+
+ if ((s = pa_read_line_from_file(dmi_vendors[i]))) {
+
+ if (pa_startswith(s, "QEMU") ||
+ /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
+ pa_startswith(s, "VMware") ||
+ pa_startswith(s, "VMW") ||
+ pa_startswith(s, "Microsoft Corporation") ||
+ pa_startswith(s, "innotek GmbH") ||
+ pa_startswith(s, "Xen")) {
+
+ pa_xfree(s);
+ return TRUE;
+ }
+
+ pa_xfree(s);
+ }
+ }
+
+#endif
+
+ /* http://lwn.net/Articles/301888/ */
+ pa_zero(sig);
+
+ __asm__ __volatile__ (
+ /* ebx/rbx is being used for PIC! */
+ " push %%"PA_REG_b" \n\t"
+ " cpuid \n\t"
+ " mov %%ebx, %1 \n\t"
+ " pop %%"PA_REG_b" \n\t"
+
+ : "=a" (eax), "=r" (sig.sig32[0]), "=c" (sig.sig32[1]), "=d" (sig.sig32[2])
+ : "0" (eax)
+ );
+
+ if (pa_streq(sig.text, "XenVMMXenVMM") ||
+ pa_streq(sig.text, "KVMKVMKVM") ||
+ /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
+ pa_streq(sig.text, "VMwareVMware") ||
+ /* http://msdn.microsoft.com/en-us/library/bb969719.aspx */
+ pa_streq(sig.text, "Microsoft Hv"))
+ return TRUE;
+
+#endif
+
+ return FALSE;
+}
diff --git a/src/pulsecore/core-util.h b/src/pulsecore/core-util.h
index 9986b14a..31a83bcc 100644
--- a/src/pulsecore/core-util.h
+++ b/src/pulsecore/core-util.h
@@ -28,6 +28,7 @@
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
+#include <sys/socket.h>
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
@@ -258,4 +259,15 @@ pa_bool_t pa_run_from_build_tree(void);
const char *pa_get_temp_dir(void);
+int pa_open_cloexec(const char *fn, int flags, mode_t mode);
+int pa_socket_cloexec(int domain, int type, int protocol);
+int pa_pipe_cloexec(int pipefd[2]);
+int pa_accept_cloexec(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
+FILE* pa_fopen_cloexec(const char *path, const char *mode);
+
+void pa_nullify_stdfds(void);
+
+char *pa_read_line_from_file(const char *fn);
+pa_bool_t pa_running_in_vm(void);
+
#endif
diff --git a/src/pulsecore/cpu-arm.c b/src/pulsecore/cpu-arm.c
index 453b7848..6bb2eadd 100644
--- a/src/pulsecore/cpu-arm.c
+++ b/src/pulsecore/cpu-arm.c
@@ -62,7 +62,7 @@ static char *get_cpuinfo(void) {
cpuinfo = pa_xmalloc(MAX_BUFFER);
- if ((fd = open("/proc/cpuinfo", O_RDONLY)) < 0) {
+ if ((fd = pa_open_cloexec("/proc/cpuinfo", O_RDONLY, 0)) < 0) {
pa_xfree(cpuinfo);
return NULL;
}
diff --git a/src/pulsecore/database-simple.c b/src/pulsecore/database-simple.c
index 1f4caf71..754930db 100644
--- a/src/pulsecore/database-simple.c
+++ b/src/pulsecore/database-simple.c
@@ -237,7 +237,7 @@ pa_database* pa_database_open(const char *fn, pa_bool_t for_write) {
path = pa_sprintf_malloc("%s."CANONICAL_HOST".simple", fn);
errno = 0;
- f = fopen(path, "r");
+ f = pa_fopen_cloexec(path, "r");
if (f || errno == ENOENT) { /* file not found is ok */
db = pa_xnew0(simple_data, 1);
@@ -480,7 +480,7 @@ int pa_database_sync(pa_database *database) {
errno = 0;
- f = fopen(db->tmp_filename, "w");
+ f = pa_fopen_cloexec(db->tmp_filename, "w");
if (!f)
goto fail;
diff --git a/src/pulsecore/database-tdb.c b/src/pulsecore/database-tdb.c
index b79d2837..4e782d65 100644
--- a/src/pulsecore/database-tdb.c
+++ b/src/pulsecore/database-tdb.c
@@ -66,6 +66,39 @@ void pa_datum_free(pa_datum *d) {
pa_zero(d);
}
+static struct tdb_context *tdb_open_cloexec(
+ const char *name,
+ int hash_size,
+ int tdb_flags,
+ int open_flags,
+ mode_t mode) {
+
+ /* Mimics pa_open_cloexec() */
+
+ struct tdb_context *c;
+
+#ifdef O_NOCTTY
+ open_flags |= O_NOCTTY;
+#endif
+
+#ifdef O_CLOEXEC
+ errno = 0;
+ if ((c = tdb_open(name, hash_size, tdb_flags, open_flags | O_CLOEXEC, mode)))
+ goto finish;
+
+ if (errno != EINVAL)
+ return NULL;
+#endif
+
+ errno = 0;
+ if (!(c = tdb_open(name, hash_size, tdb_flags, open_flags, mode)))
+ return NULL;
+
+finish:
+ pa_make_fd_cloexec(tdb_fd(c));
+ return c;
+}
+
pa_database* pa_database_open(const char *fn, pa_bool_t for_write) {
struct tdb_context *c;
char *path;
@@ -73,15 +106,7 @@ pa_database* pa_database_open(const char *fn, pa_bool_t for_write) {
pa_assert(fn);
path = pa_sprintf_malloc("%s.tdb", fn);
- errno = 0;
- c = tdb_open(path, 0, TDB_NOSYNC|TDB_NOLOCK,
- (for_write ? O_RDWR|O_CREAT : O_RDONLY)|O_NOCTTY
-#ifdef O_CLOEXEC
- |O_CLOEXEC
-#endif
- , 0644);
-
- if (c)
+ if ((c = tdb_open_cloexec(path, 0, TDB_NOSYNC|TDB_NOLOCK, (for_write ? O_RDWR|O_CREAT : O_RDONLY), 0644)))
pa_log_debug("Opened TDB database '%s'", path);
pa_xfree(path);
diff --git a/src/pulsecore/envelope.c b/src/pulsecore/envelope.c
index fd6a9487..0eca8115 100644
--- a/src/pulsecore/envelope.c
+++ b/src/pulsecore/envelope.c
@@ -177,7 +177,7 @@ static int32_t item_get_int(pa_envelope_item *i, pa_usec_t x) {
pa_assert(i->j > 0);
pa_assert(i->def->points_x[i->j-1] <= x);
- pa_assert(x < i->def->points_x[i->j]);
+ pa_assert(x <= i->def->points_x[i->j]);
return linear_interpolate_int(i->def->points_x[i->j-1], i->def->points_y.i[i->j-1],
i->def->points_x[i->j], i->def->points_y.i[i->j], x);
@@ -200,7 +200,7 @@ static float item_get_float(pa_envelope_item *i, pa_usec_t x) {
pa_assert(i->j > 0);
pa_assert(i->def->points_x[i->j-1] <= x);
- pa_assert(x < i->def->points_x[i->j]);
+ pa_assert(x <= i->def->points_x[i->j]);
return linear_interpolate_float(i->def->points_x[i->j-1], i->def->points_y.f[i->j-1],
i->def->points_x[i->j], i->def->points_y.f[i->j], x);
@@ -550,7 +550,7 @@ static int32_t linear_get_int(pa_envelope *e, int v) {
e->points[v].cached_valid = TRUE;
}
- return e->points[v].y.i[e->points[v].n_current] + (e->points[v].cached_dy_i * (int32_t) (e->x - e->points[v].x[e->points[v].n_current])) / (int32_t) e->points[v].cached_dx;
+ return e->points[v].y.i[e->points[v].n_current] + ((float)e->points[v].cached_dy_i * (int32_t) (e->x - e->points[v].x[e->points[v].n_current])) / (int32_t) e->points[v].cached_dx;
}
static float linear_get_float(pa_envelope *e, int v) {
@@ -597,34 +597,60 @@ void pa_envelope_apply(pa_envelope *e, pa_memchunk *chunk) {
fs = pa_frame_size(&e->sample_spec);
n = chunk->length;
+ pa_log_debug("Envelop position %d applying factor %d=%f, sample spec is %d, chunk's length is %d, fs is %d\n", e->x, linear_get_int(e, v), ((float) linear_get_int(e,v))/0x10000, e->sample_spec.format, n, fs);
+
switch (e->sample_spec.format) {
case PA_SAMPLE_U8: {
- uint8_t *t;
+ uint8_t *d, *s;
+ unsigned channel;
+ int32_t factor = linear_get_int(e, v);
- for (t = p; n > 0; n -= fs) {
- int32_t factor = linear_get_int(e, v);
- unsigned c;
- e->x += fs;
+ s = (uint8_t*) p + n;
- for (c = 0; c < e->sample_spec.channels; c++, t++)
- *t = (uint8_t) (((factor * ((int16_t) *t - 0x80)) / 0x10000) + 0x80);
+ for (channel = 0, d = p; d < s; d++) {
+ int32_t t, hi, lo;
+
+ hi = factor >> 16;
+ lo = factor & 0xFFFF;
+
+ t = (int32_t) *d - 0x80;
+ t = ((t * lo) >> 16) + (t * hi);
+ t = PA_CLAMP_UNLIKELY(t, -0x80, 0x7F);
+ *d = (uint8_t) (t + 0x80);
+
+ if (PA_UNLIKELY(++channel >= e->sample_spec.channels)) {
+ channel = 0;
+ e->x += fs;
+ factor = linear_get_int(e, v);
+ }
}
break;
}
case PA_SAMPLE_ULAW: {
- uint8_t *t;
+ uint8_t *d, *s;
+ unsigned channel;
+ int32_t factor = linear_get_int(e, v);
- for (t = p; n > 0; n -= fs) {
- int32_t factor = linear_get_int(e, v);
- unsigned c;
- e->x += fs;
+ s = (uint8_t*) p + n;
- for (c = 0; c < e->sample_spec.channels; c++, t++) {
- int16_t k = st_ulaw2linear16(*t);
- *t = (uint8_t) st_14linear2ulaw((int16_t) (((factor * k) / 0x10000) >> 2));
+ for (channel = 0, d = p; d < s; d++) {
+ int32_t t, hi, lo;
+
+ hi = factor >> 16;
+ lo = factor & 0xFFFF;
+
+ t = (int32_t) st_ulaw2linear16(*d);
+ t = ((t * lo) >> 16) + (t * hi);
+ t = PA_CLAMP_UNLIKELY(t, -0x8000, 0x7FFF);
+ *d = (uint8_t) st_14linear2ulaw((int16_t) t >> 2);
+
+ if (PA_UNLIKELY(++channel >= e->sample_spec.channels)) {
+ channel = 0;
+ e->x += fs;
+ factor = linear_get_int(e, v);
}
}
@@ -632,16 +658,27 @@ void pa_envelope_apply(pa_envelope *e, pa_memchunk *chunk) {
}
case PA_SAMPLE_ALAW: {
- uint8_t *t;
+ uint8_t *d, *s;
+ unsigned channel;
+ int32_t factor = linear_get_int(e, v);
- for (t = p; n > 0; n -= fs) {
- int32_t factor = linear_get_int(e, v);
- unsigned c;
- e->x += fs;
+ s = (uint8_t*) p + n;
- for (c = 0; c < e->sample_spec.channels; c++, t++) {
- int16_t k = st_alaw2linear16(*t);
- *t = (uint8_t) st_13linear2alaw((int16_t) (((factor * k) / 0x10000) >> 3));
+ for (channel = 0, d = p; d < s; d++) {
+ int32_t t, hi, lo;
+
+ hi = factor >> 16;
+ lo = factor & 0xFFFF;
+
+ t = (int32_t) st_alaw2linear16(*d);
+ t = ((t * lo) >> 16) + (t * hi);
+ t = PA_CLAMP_UNLIKELY(t, -0x8000, 0x7FFF);
+ *d = (uint8_t) st_13linear2alaw((int16_t) t >> 3);
+
+ if (PA_UNLIKELY(++channel >= e->sample_spec.channels)) {
+ channel = 0;
+ e->x += fs;
+ factor = linear_get_int(e, v);
}
}
@@ -649,31 +686,55 @@ void pa_envelope_apply(pa_envelope *e, pa_memchunk *chunk) {
}
case PA_SAMPLE_S16NE: {
- int16_t *t;
+ int16_t *d, *s;
+ unsigned channel;
+ int32_t factor = linear_get_int(e, v);
- for (t = p; n > 0; n -= fs) {
- int32_t factor = linear_get_int(e, v);
- unsigned c;
- e->x += fs;
+ s = (int16_t*) p + n/sizeof(int16_t);
- for (c = 0; c < e->sample_spec.channels; c++, t++)
- *t = (int16_t) ((factor * *t) / 0x10000);
+ for (channel = 0, d = p; d < s; d++) {
+ int32_t t, hi, lo;
+
+ hi = factor >> 16;
+ lo = factor & 0xFFFF;
+
+ t = (int32_t)(*d);
+ t = ((t * lo) >> 16) + (t * hi);
+ t = PA_CLAMP_UNLIKELY(t, -0x8000, 0x7FFF);
+ *d = (int16_t) t;
+
+ if (PA_UNLIKELY(++channel >= e->sample_spec.channels)) {
+ channel = 0;
+ e->x += fs;
+ factor = linear_get_int(e, v);
+ }
}
break;
}
case PA_SAMPLE_S16RE: {
- int16_t *t;
+ int16_t *d, *s;
+ unsigned channel;
+ int32_t factor = linear_get_int(e, v);
- for (t = p; n > 0; n -= fs) {
- int32_t factor = linear_get_int(e, v);
- unsigned c;
- e->x += fs;
+ s = (int16_t*) p + n/sizeof(int16_t);
- for (c = 0; c < e->sample_spec.channels; c++, t++) {
- int16_t r = (int16_t) ((factor * PA_INT16_SWAP(*t)) / 0x10000);
- *t = PA_INT16_SWAP(r);
+ for (channel = 0, d = p; d < s; d++) {
+ int32_t t, hi, lo;
+
+ hi = factor >> 16;
+ lo = factor & 0xFFFF;
+
+ t = (int32_t) PA_INT16_SWAP(*d);
+ t = ((t * lo) >> 16) + (t * hi);
+ t = PA_CLAMP_UNLIKELY(t, -0x8000, 0x7FFF);
+ *d = PA_INT16_SWAP((int16_t) t);
+
+ if (PA_UNLIKELY(++channel >= e->sample_spec.channels)) {
+ channel = 0;
+ e->x += fs;
+ factor = linear_get_int(e, v);
}
}
@@ -681,31 +742,49 @@ void pa_envelope_apply(pa_envelope *e, pa_memchunk *chunk) {
}
case PA_SAMPLE_S32NE: {
- int32_t *t;
+ int32_t *d, *s;
+ unsigned channel;
+ int32_t factor = linear_get_int(e, v);
- for (t = p; n > 0; n -= fs) {
- int32_t factor = linear_get_int(e, v);
- unsigned c;
- e->x += fs;
+ s = (int32_t*) p + n/sizeof(int32_t);
- for (c = 0; c < e->sample_spec.channels; c++, t++)
- *t = (int32_t) (((int64_t) factor * (int64_t) *t) / 0x10000);
+ for (channel = 0, d = p; d < s; d++) {
+ int64_t t;
+
+ t = (int64_t)(*d);
+ t = (t * factor) >> 16;
+ t = PA_CLAMP_UNLIKELY(t, -0x80000000LL, 0x7FFFFFFFLL);
+ *d = (int32_t) t;
+
+ if (PA_UNLIKELY(++channel >= e->sample_spec.channels)) {
+ channel = 0;
+ e->x += fs;
+ factor = linear_get_int(e, v);
+ }
}
break;
}
case PA_SAMPLE_S32RE: {
- int32_t *t;
+ int32_t *d, *s;
+ unsigned channel;
+ int32_t factor = linear_get_int(e, v);
- for (t = p; n > 0; n -= fs) {
- int32_t factor = linear_get_int(e, v);
- unsigned c;
- e->x += fs;
+ s = (int32_t*) p + n/sizeof(int32_t);
- for (c = 0; c < e->sample_spec.channels; c++, t++) {
- int32_t r = (int32_t) (((int64_t) factor * (int64_t) PA_INT32_SWAP(*t)) / 0x10000);
- *t = PA_INT32_SWAP(r);
+ for (channel = 0, d = p; d < s; d++) {
+ int64_t t;
+
+ t = (int64_t) PA_INT32_SWAP(*d);
+ t = (t * factor) >> 16;
+ t = PA_CLAMP_UNLIKELY(t, -0x80000000LL, 0x7FFFFFFFLL);
+ *d = PA_INT32_SWAP((int32_t) t);
+
+ if (PA_UNLIKELY(++channel >= e->sample_spec.channels)) {
+ channel = 0;
+ e->x += fs;
+ factor = linear_get_int(e, v);
}
}
@@ -713,6 +792,7 @@ void pa_envelope_apply(pa_envelope *e, pa_memchunk *chunk) {
}
case PA_SAMPLE_FLOAT32NE: {
+ /*Seems the FLOAT32NE part of pa_volume_memchunk not right, do not reuse here*/
float *t;
for (t = p; n > 0; n -= fs) {
@@ -728,6 +808,7 @@ void pa_envelope_apply(pa_envelope *e, pa_memchunk *chunk) {
}
case PA_SAMPLE_FLOAT32RE: {
+ /*Seems the FLOAT32RE part of pa_volume_memchunk not right, do not reuse here*/
float *t;
for (t = p; n > 0; n -= fs) {
@@ -744,10 +825,101 @@ void pa_envelope_apply(pa_envelope *e, pa_memchunk *chunk) {
break;
}
- case PA_SAMPLE_S24LE:
- case PA_SAMPLE_S24BE:
- case PA_SAMPLE_S24_32LE:
- case PA_SAMPLE_S24_32BE:
+ case PA_SAMPLE_S24NE: {
+ uint8_t *d, *s;
+ unsigned channel;
+ int32_t factor = linear_get_int(e, v);
+
+ s = (uint8_t*) p + n/3;
+
+ for (channel = 0, d = p; d < s; d++) {
+ int64_t t;
+
+ t = (int64_t)((int32_t) (PA_READ24NE(d) << 8));
+ t = (t * factor) >> 16;
+ t = PA_CLAMP_UNLIKELY(t, -0x80000000LL, 0x7FFFFFFFLL);
+ PA_WRITE24NE(d, ((uint32_t) (int32_t) t) >> 8);
+
+ if (PA_UNLIKELY(++channel >= e->sample_spec.channels)) {
+ channel = 0;
+ e->x += fs;
+ factor = linear_get_int(e, v);
+ }
+ }
+
+ break;
+ }
+ case PA_SAMPLE_S24RE: {
+ uint8_t *d, *s;
+ unsigned channel;
+ int32_t factor = linear_get_int(e, v);
+
+ s = (uint8_t*) p + n/3;
+
+ for (channel = 0, d = p; d < s; d++) {
+ int64_t t;
+
+ t = (int64_t)((int32_t) (PA_READ24RE(d) << 8));
+ t = (t * factor) >> 16;
+ t = PA_CLAMP_UNLIKELY(t, -0x80000000LL, 0x7FFFFFFFLL);
+ PA_WRITE24RE(d, ((uint32_t) (int32_t) t) >> 8);
+
+ if (PA_UNLIKELY(++channel >= e->sample_spec.channels)) {
+ channel = 0;
+ e->x += fs;
+ factor = linear_get_int(e, v);
+ }
+ }
+
+ break;
+ }
+ case PA_SAMPLE_S24_32NE: {
+ uint32_t *d, *s;
+ unsigned channel;
+ int32_t factor = linear_get_int(e, v);
+
+ s = (uint32_t*) p + n/sizeof(uint32_t);
+
+ for (channel = 0, d = p; d < s; d++) {
+ int64_t t;
+
+ t = (int64_t) ((int32_t) (*d << 8));
+ t = (t * factor) >> 16;
+ t = PA_CLAMP_UNLIKELY(t, -0x80000000LL, 0x7FFFFFFFLL);
+ *d = ((uint32_t) ((int32_t) t)) >> 8;
+
+ if (PA_UNLIKELY(++channel >= e->sample_spec.channels)) {
+ channel = 0;
+ e->x += fs;
+ factor = linear_get_int(e, v);
+ }
+ }
+
+ break;
+ }
+ case PA_SAMPLE_S24_32RE: {
+ uint32_t *d, *s;
+ unsigned channel;
+ int32_t factor = linear_get_int(e, v);
+
+ s = (uint32_t*) p + n/sizeof(uint32_t);
+
+ for (channel = 0, d = p; d < s; d++) {
+ int64_t t;
+
+ t = (int64_t) ((int32_t) (PA_UINT32_SWAP(*d) << 8));
+ t = (t * factor) >> 16;
+ t = PA_CLAMP_UNLIKELY(t, -0x80000000LL, 0x7FFFFFFFLL);
+ *d = PA_UINT32_SWAP(((uint32_t) ((int32_t) t)) >> 8);
+
+ if (PA_UNLIKELY(++channel >= e->sample_spec.channels)) {
+ channel = 0;
+ e->x += fs;
+ factor = linear_get_int(e, v);
+ }
+ }
+ break;
+ }
/* FIXME */
pa_assert_not_reached();
@@ -757,8 +929,6 @@ void pa_envelope_apply(pa_envelope *e, pa_memchunk *chunk) {
}
pa_memblock_release(chunk->memblock);
-
- e->x += chunk->length;
} else {
/* When we have no envelope to apply we reset our origin */
e->x = 0;
@@ -774,13 +944,48 @@ void pa_envelope_rewind(pa_envelope *e, size_t n_bytes) {
envelope_begin_read(e, &v);
- if (n_bytes < e->x)
- e->x -= n_bytes;
+ if (e->x - n_bytes <= e->points[v].x[0])
+ e->x = e->points[v].x[0];
else
- e->x = 0;
+ e->x -= n_bytes;
e->points[v].n_current = 0;
e->points[v].cached_valid = FALSE;
envelope_commit_read(e, v);
}
+
+void pa_envelope_restart(pa_envelope* e) {
+ int v;
+ pa_assert(e);
+
+ envelope_begin_read(e, &v);
+ e->x = e->points[v].x[0];
+ envelope_commit_read(e, v);
+}
+
+pa_bool_t pa_envelope_is_finished(pa_envelope* e) {
+ pa_assert(e);
+
+ int v;
+ pa_bool_t finished;
+
+ envelope_begin_read(e, &v);
+ finished = (e->x >= e->points[v].x[e->points[v].n_points-1]);
+ envelope_commit_read(e, v);
+
+ return finished;
+}
+
+int32_t pa_envelope_length(pa_envelope *e) {
+ pa_assert(e);
+
+ int v;
+ size_t size;
+
+ envelope_begin_read(e, &v);
+ size = e->points[v].x[e->points[v].n_points-1] - e->points[v].x[0];
+ envelope_commit_read(e, v);
+
+ return size;
+}
diff --git a/src/pulsecore/envelope.h b/src/pulsecore/envelope.h
index 5296415a..4fa36579 100644
--- a/src/pulsecore/envelope.h
+++ b/src/pulsecore/envelope.h
@@ -49,5 +49,8 @@ pa_envelope_item *pa_envelope_replace(pa_envelope *e, pa_envelope_item *i, const
void pa_envelope_remove(pa_envelope *e, pa_envelope_item *i);
void pa_envelope_apply(pa_envelope *e, pa_memchunk *chunk);
void pa_envelope_rewind(pa_envelope *e, size_t n_bytes);
+void pa_envelope_restart(pa_envelope* e);
+pa_bool_t pa_envelope_is_finished(pa_envelope* e);
+int32_t pa_envelope_length(pa_envelope *e);
#endif
diff --git a/src/pulsecore/fdsem.c b/src/pulsecore/fdsem.c
index 380f34f5..ea14e8a7 100644
--- a/src/pulsecore/fdsem.c
+++ b/src/pulsecore/fdsem.c
@@ -62,19 +62,15 @@ pa_fdsem *pa_fdsem_new(void) {
f = pa_xmalloc(PA_ALIGN(sizeof(pa_fdsem)) + PA_ALIGN(sizeof(pa_fdsem_data)));
#ifdef HAVE_SYS_EVENTFD_H
- if ((f->efd = eventfd(0, 0)) >= 0) {
- pa_make_fd_cloexec(f->efd);
+ if ((f->efd = eventfd(0, EFD_CLOEXEC)) >= 0)
f->fds[0] = f->fds[1] = -1;
- } else
+ else
#endif
{
- if (pipe(f->fds) < 0) {
+ if (pa_pipe_cloexec(f->fds) < 0) {
pa_xfree(f);
return NULL;
}
-
- pa_make_fd_cloexec(f->fds[0]);
- pa_make_fd_cloexec(f->fds[1]);
}
f->data = (pa_fdsem_data*) ((uint8_t*) f + PA_ALIGN(sizeof(pa_fdsem)));
@@ -114,12 +110,11 @@ pa_fdsem *pa_fdsem_new_shm(pa_fdsem_data *data, int* event_fd) {
f = pa_xnew(pa_fdsem, 1);
- if ((f->efd = eventfd(0, 0)) < 0) {
+ if ((f->efd = eventfd(0, EFD_CLOEXEC)) < 0) {
pa_xfree(f);
return NULL;
}
- pa_make_fd_cloexec(f->efd);
f->fds[0] = f->fds[1] = -1;
f->data = data;
diff --git a/src/pulsecore/lock-autospawn.c b/src/pulsecore/lock-autospawn.c
index c0df7938..65e35634 100644
--- a/src/pulsecore/lock-autospawn.c
+++ b/src/pulsecore/lock-autospawn.c
@@ -87,12 +87,9 @@ static int ref(void) {
pa_assert(pipe_fd[0] < 0);
pa_assert(pipe_fd[1] < 0);
- if (pipe(pipe_fd) < 0)
+ if (pa_pipe_cloexec(pipe_fd) < 0)
return -1;
- pa_make_fd_cloexec(pipe_fd[0]);
- pa_make_fd_cloexec(pipe_fd[1]);
-
pa_make_fd_nonblock(pipe_fd[1]);
pa_make_fd_nonblock(pipe_fd[0]);
diff --git a/src/pulsecore/memblock.c b/src/pulsecore/memblock.c
index eac4a59b..f38b17c6 100644
--- a/src/pulsecore/memblock.c
+++ b/src/pulsecore/memblock.c
@@ -54,7 +54,7 @@
* stored in SHM and our OS does not commit the memory before we use
* it for the first time. */
#define PA_MEMPOOL_SLOTS_MAX 1024
-#define PA_MEMPOOL_SLOT_SIZE (128*1024)
+#define PA_MEMPOOL_SLOT_SIZE (64*1024)
#define PA_MEMEXPORT_SLOTS_MAX 128
diff --git a/src/pulsecore/pid.c b/src/pulsecore/pid.c
index 996946c2..213e7983 100644
--- a/src/pulsecore/pid.c
+++ b/src/pulsecore/pid.c
@@ -88,10 +88,7 @@ static int open_pid_file(const char *fn, int mode) {
for (;;) {
struct stat st;
- if ((fd = open(fn, mode
-#ifdef O_NOCTTY
- |O_NOCTTY
-#endif
+ if ((fd = pa_open_cloexec(fn, mode
#ifdef O_NOFOLLOW
|O_NOFOLLOW
#endif
@@ -146,7 +143,7 @@ static int proc_name_ours(pid_t pid, const char *procname) {
pa_snprintf(bn, sizeof(bn), "/proc/%lu/stat", (unsigned long) pid);
- if (!(f = fopen(bn, "r"))) {
+ if (!(f = pa_fopen_cloexec(bn, "r"))) {
pa_log_info("Failed to open %s: %s", bn, pa_cstrerror(errno));
return -1;
} else {
diff --git a/src/pulsecore/protocol-dbus.c b/src/pulsecore/protocol-dbus.c
index 5c1127be..e427bb19 100644
--- a/src/pulsecore/protocol-dbus.c
+++ b/src/pulsecore/protocol-dbus.c
@@ -958,7 +958,7 @@ pa_client *pa_dbus_protocol_get_client(pa_dbus_protocol *p, DBusConnection *conn
void pa_dbus_protocol_add_signal_listener(
pa_dbus_protocol *p,
DBusConnection *conn,
- const char *signal,
+ const char *signal_name,
char **objects,
unsigned n_objects) {
struct connection_entry *conn_entry;
@@ -978,18 +978,18 @@ void pa_dbus_protocol_add_signal_listener(
while ((object_path = pa_idxset_steal_first(conn_entry->all_signals_objects, NULL)))
pa_xfree(object_path);
- if (signal) {
+ if (signal_name) {
conn_entry->listening_for_all_signals = FALSE;
/* Replace the old object list with a new one. */
- if ((object_set = pa_hashmap_remove(conn_entry->listening_signals, signal)))
+ if ((object_set = pa_hashmap_remove(conn_entry->listening_signals, signal_name)))
pa_idxset_free(object_set, free_listened_object_name_cb, NULL);
object_set = pa_idxset_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
for (i = 0; i < n_objects; ++i)
pa_idxset_put(object_set, pa_xstrdup(objects[i]), NULL);
- pa_hashmap_put(conn_entry->listening_signals, signal, object_set);
+ pa_hashmap_put(conn_entry->listening_signals, signal_name, object_set);
} else {
conn_entry->listening_for_all_signals = TRUE;
@@ -1004,7 +1004,7 @@ void pa_dbus_protocol_add_signal_listener(
}
}
-void pa_dbus_protocol_remove_signal_listener(pa_dbus_protocol *p, DBusConnection *conn, const char *signal) {
+void pa_dbus_protocol_remove_signal_listener(pa_dbus_protocol *p, DBusConnection *conn, const char *signal_name) {
struct connection_entry *conn_entry;
pa_idxset *object_set;
@@ -1013,8 +1013,8 @@ void pa_dbus_protocol_remove_signal_listener(pa_dbus_protocol *p, DBusConnection
pa_assert_se((conn_entry = pa_hashmap_get(p->connections, conn)));
- if (signal) {
- if ((object_set = pa_hashmap_get(conn_entry->listening_signals, signal)))
+ if (signal_name) {
+ if ((object_set = pa_hashmap_get(conn_entry->listening_signals, signal_name)))
pa_idxset_free(object_set, free_listened_object_name_cb, NULL);
} else {
@@ -1030,7 +1030,7 @@ void pa_dbus_protocol_remove_signal_listener(pa_dbus_protocol *p, DBusConnection
}
}
-void pa_dbus_protocol_send_signal(pa_dbus_protocol *p, DBusMessage *signal) {
+void pa_dbus_protocol_send_signal(pa_dbus_protocol *p, DBusMessage *signal_name) {
struct connection_entry *conn_entry;
void *state = NULL;
pa_idxset *object_set;
@@ -1038,24 +1038,24 @@ void pa_dbus_protocol_send_signal(pa_dbus_protocol *p, DBusMessage *signal) {
char *signal_string;
pa_assert(p);
- pa_assert(signal);
- pa_assert(dbus_message_get_type(signal) == DBUS_MESSAGE_TYPE_SIGNAL);
- pa_assert_se(dbus_message_get_interface(signal));
- pa_assert_se(dbus_message_get_member(signal));
+ pa_assert(signal_name);
+ pa_assert(dbus_message_get_type(signal_name) == DBUS_MESSAGE_TYPE_SIGNAL);
+ pa_assert_se(dbus_message_get_interface(signal_name));
+ pa_assert_se(dbus_message_get_member(signal_name));
- signal_string = pa_sprintf_malloc("%s.%s", dbus_message_get_interface(signal), dbus_message_get_member(signal));
+ signal_string = pa_sprintf_malloc("%s.%s", dbus_message_get_interface(signal_name), dbus_message_get_member(signal_name));
PA_HASHMAP_FOREACH(conn_entry, p->connections, state) {
if ((conn_entry->listening_for_all_signals /* Case 1: listening for all signals */
- && (pa_idxset_get_by_data(conn_entry->all_signals_objects, dbus_message_get_path(signal), NULL)
+ && (pa_idxset_get_by_data(conn_entry->all_signals_objects, dbus_message_get_path(signal_name), NULL)
|| pa_idxset_isempty(conn_entry->all_signals_objects)))
|| (!conn_entry->listening_for_all_signals /* Case 2: not listening for all signals */
&& (object_set = pa_hashmap_get(conn_entry->listening_signals, signal_string))
- && (pa_idxset_get_by_data(object_set, dbus_message_get_path(signal), NULL)
+ && (pa_idxset_get_by_data(object_set, dbus_message_get_path(signal_name), NULL)
|| pa_idxset_isempty(object_set)))) {
- pa_assert_se(signal_copy = dbus_message_copy(signal));
+ pa_assert_se(signal_copy = dbus_message_copy(signal_name));
pa_assert_se(dbus_connection_send(conn_entry->connection, signal_copy, NULL));
dbus_message_unref(signal_copy);
}
diff --git a/src/pulsecore/protocol-native.c b/src/pulsecore/protocol-native.c
index d06dd4eb..d49a78e5 100644
--- a/src/pulsecore/protocol-native.c
+++ b/src/pulsecore/protocol-native.c
@@ -1002,6 +1002,7 @@ static playback_stream* playback_stream_new(
pa_proplist *p,
pa_bool_t adjust_latency,
pa_bool_t early_requests,
+ pa_bool_t relative_volume,
int *ret) {
playback_stream *s, *ssync;
@@ -1044,13 +1045,21 @@ static playback_stream* playback_stream_new(
data.driver = __FILE__;
data.module = c->options->module;
data.client = c->client;
- data.sink = sink;
+ if (sink) {
+ data.sink = sink;
+ data.save_sink = TRUE;
+ }
pa_sink_input_new_data_set_sample_spec(&data, ss);
pa_sink_input_new_data_set_channel_map(&data, map);
- if (volume)
+ if (volume) {
pa_sink_input_new_data_set_volume(&data, volume);
- if (muted_set)
+ data.volume_is_absolute = !relative_volume;
+ data.save_volume = TRUE;
+ }
+ if (muted_set) {
pa_sink_input_new_data_set_muted(&data, muted);
+ data.save_muted = TRUE;
+ }
data.sync_base = ssync ? ssync->sink_input : NULL;
data.flags = flags;
@@ -1838,7 +1847,8 @@ static void command_create_playback_stream(pa_pdispatch *pd, uint32_t command, u
early_requests = FALSE,
dont_inhibit_auto_suspend = FALSE,
muted_set = FALSE,
- fail_on_suspend = FALSE;
+ fail_on_suspend = FALSE,
+ relative_volume = FALSE;
pa_sink_input_flags_t flags = 0;
pa_proplist *p;
pa_bool_t volume_set = TRUE;
@@ -1931,6 +1941,15 @@ static void command_create_playback_stream(pa_pdispatch *pd, uint32_t command, u
}
}
+ if (c->version >= 17) {
+
+ if (pa_tagstruct_get_boolean(t, &relative_volume) < 0) {
+ protocol_error(c);
+ pa_proplist_free(p);
+ return;
+ }
+ }
+
if (!pa_tagstruct_eof(t)) {
protocol_error(c);
pa_proplist_free(p);
@@ -1970,7 +1989,7 @@ static void command_create_playback_stream(pa_pdispatch *pd, uint32_t command, u
* flag. For older versions we synthesize it here */
muted_set = muted_set || muted;
- s = playback_stream_new(c, sink, &ss, &map, &attr, volume_set ? &volume : NULL, muted, muted_set, syncid, &missing, flags, p, adjust_latency, early_requests, &ret);
+ s = playback_stream_new(c, sink, &ss, &map, &attr, volume_set ? &volume : NULL, muted, muted_set, syncid, &missing, flags, p, adjust_latency, early_requests, relative_volume, &ret);
pa_proplist_free(p);
CHECK_VALIDITY(c->pstream, s, tag, ret);
@@ -2612,7 +2631,7 @@ static void command_get_record_latency(pa_pdispatch *pd, uint32_t command, uint3
pa_tagstruct_put_usec(reply, s->current_monitor_latency);
pa_tagstruct_put_usec(reply,
s->current_source_latency +
- pa_bytes_to_usec(s->on_the_fly_snapshot, &s->source_output->sample_spec));
+ pa_bytes_to_usec(s->on_the_fly_snapshot, &s->source_output->source->sample_spec));
pa_tagstruct_put_boolean(reply,
pa_source_get_state(s->source_output->source) == PA_SOURCE_RUNNING &&
pa_source_output_get_state(s->source_output) == PA_SOURCE_OUTPUT_RUNNING);
diff --git a/src/pulsecore/random.c b/src/pulsecore/random.c
index 518c281a..a87d24e3 100644
--- a/src/pulsecore/random.c
+++ b/src/pulsecore/random.c
@@ -62,11 +62,7 @@ static int random_proper(void *ret_data, size_t length) {
while (*device) {
ret = 0;
- if ((fd = open(*device, O_RDONLY
-#ifdef O_NOCTTY
- | O_NOCTTY
-#endif
- )) >= 0) {
+ if ((fd = pa_open_cloexec(*device, O_RDONLY, 0)) >= 0) {
if ((r = pa_loop_read(fd, ret_data, length, NULL)) < 0 || (size_t) r != length)
ret = -1;
diff --git a/src/pulsecore/sample-util.c b/src/pulsecore/sample-util.c
index a26dc876..74600dec 100644
--- a/src/pulsecore/sample-util.c
+++ b/src/pulsecore/sample-util.c
@@ -1007,7 +1007,7 @@ void pa_memchunk_dump_to_file(pa_memchunk *c, const char *fn) {
/* Only for debugging purposes */
- f = fopen(fn, "a");
+ f = pa_fopen_cloexec(fn, "a");
if (!f) {
pa_log_warn("Failed to open '%s': %s", fn, pa_cstrerror(errno));
diff --git a/src/pulsecore/semaphore-osx.c b/src/pulsecore/semaphore-osx.c
new file mode 100644
index 00000000..73f43559
--- /dev/null
+++ b/src/pulsecore/semaphore-osx.c
@@ -0,0 +1,63 @@
+/***
+ This file is part of PulseAudio.
+
+ Copyright 2009 Kim Lester <kim@dfusion.com.au>
+
+ 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 <Multiprocessing.h>
+
+#include <pulse/xmalloc.h>
+#include <pulsecore/macro.h>
+
+#include "semaphore.h"
+
+struct pa_semaphore
+{
+ MPSemaphoreID sema;
+};
+
+pa_semaphore* pa_semaphore_new(unsigned int value) {
+ /* NOTE: Can't assume boolean - ie value = 0,1, so use UINT_MAX (boolean more efficient ?) */
+ pa_semaphore *s;
+
+ s = pa_xnew(pa_semaphore, 1);
+ pa_assert_se(MPCreateSemaphore(UINT_MAX, value, &s->sema) == 0);
+
+ return s;
+}
+
+void pa_semaphore_free(pa_semaphore *s) {
+ pa_assert(s);
+ pa_assert_se(MPDeleteSemaphore(s->sema) == 0);
+ pa_xfree(s);
+}
+
+void pa_semaphore_post(pa_semaphore *s) {
+ pa_assert(s);
+ pa_assert_se(MPSignalSemaphore(s->sema) == 0);
+}
+
+void pa_semaphore_wait(pa_semaphore *s) {
+ pa_assert(s);
+ /* should probably check return value (-ve is error), noErr is ok. */
+ pa_assert_se(MPWaitOnSemaphore(s->sema, kDurationForever) == 0);
+}
diff --git a/src/pulsecore/sink-input.c b/src/pulsecore/sink-input.c
index 1af2823f..35e3d4a9 100644
--- a/src/pulsecore/sink-input.c
+++ b/src/pulsecore/sink-input.c
@@ -38,6 +38,7 @@
#include <pulsecore/play-memblockq.h>
#include <pulsecore/namereg.h>
#include <pulsecore/core-util.h>
+#include <pulse/timeval.h>
#include "sink-input.h"
@@ -48,6 +49,11 @@ PA_DEFINE_PUBLIC_CLASS(pa_sink_input, pa_msgobject);
static void sink_input_free(pa_object *o);
static void set_real_ratio(pa_sink_input *i, const pa_cvolume *v);
+static void sink_input_set_ramping_info(pa_sink_input* i, pa_volume_t pre_virtual_volume, pa_volume_t target_virtual_volume, pa_usec_t t);
+static void sink_input_set_ramping_info_for_mute(pa_sink_input* i, pa_bool_t mute, pa_usec_t t);
+static void sink_input_volume_ramping(pa_sink_input* i, pa_memchunk* chunk);
+static void sink_input_rewind_ramp_info(pa_sink_input *i, size_t nbytes);
+static void sink_input_release_envelope(pa_sink_input *i);
pa_sink_input_new_data* pa_sink_input_new_data_init(pa_sink_input_new_data *data) {
pa_assert(data);
@@ -323,6 +329,16 @@ int pa_sink_input_new(
reset_callbacks(i);
i->userdata = NULL;
+ /* Set Ramping info */
+ i->thread_info.ramp_info.is_ramping = FALSE;
+ i->thread_info.ramp_info.envelope_dead = TRUE;
+ i->thread_info.ramp_info.envelope = NULL;
+ i->thread_info.ramp_info.item = NULL;
+ i->thread_info.ramp_info.envelope_dying = 0;
+
+ pa_atomic_store(&i->before_ramping_v, 0);
+ pa_atomic_store(&i->before_ramping_m, 0);
+
i->thread_info.state = i->state;
i->thread_info.attached = FALSE;
pa_atomic_store(&i->thread_info.drained, 1);
@@ -510,6 +526,12 @@ static void sink_input_free(pa_object *o) {
* "half-moved" or are connected to sinks that have no asyncmsgq
* and are hence half-destructed themselves! */
+ if (i->thread_info.ramp_info.envelope) {
+ pa_log_debug ("Freeing envelope\n");
+ pa_envelope_free(i->thread_info.ramp_info.envelope);
+ i->thread_info.ramp_info.envelope = NULL;
+ }
+
if (i->thread_info.render_memblockq)
pa_memblockq_free(i->thread_info.render_memblockq);
@@ -597,6 +619,7 @@ pa_usec_t pa_sink_input_get_latency(pa_sink_input *i, pa_usec_t *sink_latency) {
void pa_sink_input_peek(pa_sink_input *i, size_t slength /* in sink frames */, pa_memchunk *chunk, pa_cvolume *volume) {
pa_bool_t do_volume_adj_here, need_volume_factor_sink;
pa_bool_t volume_is_norm;
+ pa_bool_t ramping;
size_t block_size_max_sink, block_size_max_sink_input;
size_t ilength;
@@ -641,7 +664,7 @@ void pa_sink_input_peek(pa_sink_input *i, size_t slength /* in sink frames */, p
* to adjust the volume *before* we resample. Otherwise we can do
* it after and leave it for the sink code */
- do_volume_adj_here = !pa_channel_map_equal(&i->channel_map, &i->sink->channel_map);
+ do_volume_adj_here = !pa_channel_map_equal(&i->channel_map, &i->sink->channel_map) || i->thread_info.ramp_info.is_ramping;
volume_is_norm = pa_cvolume_is_norm(&i->thread_info.soft_volume) && !i->thread_info.muted;
need_volume_factor_sink = !pa_cvolume_is_norm(&i->volume_factor_sink);
@@ -684,7 +707,7 @@ void pa_sink_input_peek(pa_sink_input *i, size_t slength /* in sink frames */, p
wchunk.length = block_size_max_sink_input;
/* It might be necessary to adjust the volume here */
- if (do_volume_adj_here && !volume_is_norm) {
+ if (do_volume_adj_here && !volume_is_norm && !i->thread_info.ramp_info.is_ramping) {
pa_memchunk_make_writable(&wchunk, 0);
if (i->thread_info.muted) {
@@ -717,14 +740,15 @@ void pa_sink_input_peek(pa_sink_input *i, size_t slength /* in sink frames */, p
pa_memchunk rchunk;
pa_resampler_run(i->thread_info.resampler, &wchunk, &rchunk);
- if (nvfs) {
- pa_memchunk_make_writable(&rchunk, 0);
- pa_volume_memchunk(&rchunk, &i->sink->sample_spec, &i->volume_factor_sink);
- }
-
/* pa_log_debug("pushing %lu", (unsigned long) rchunk.length); */
if (rchunk.memblock) {
+
+ if (nvfs) {
+ pa_memchunk_make_writable(&rchunk, 0);
+ pa_volume_memchunk(&rchunk, &i->sink->sample_spec, &i->volume_factor_sink);
+ }
+
pa_memblockq_push_align(i->thread_info.render_memblockq, &rchunk);
pa_memblock_unref(rchunk.memblock);
}
@@ -749,6 +773,23 @@ void pa_sink_input_peek(pa_sink_input *i, size_t slength /* in sink frames */, p
if (chunk->length > block_size_max_sink)
chunk->length = block_size_max_sink;
+ ramping = i->thread_info.ramp_info.is_ramping;
+ if (ramping)
+ sink_input_volume_ramping(i, chunk);
+
+ if (!i->thread_info.ramp_info.envelope_dead) {
+ i->thread_info.ramp_info.envelope_dying += chunk->length;
+ pa_log_debug("Envelope dying is %d, chunk length is %d, dead thresholder is %d\n", i->thread_info.ramp_info.envelope_dying,
+ chunk->length,
+ i->sink->thread_info.max_rewind + pa_envelope_length(i->thread_info.ramp_info.envelope));
+
+ if (i->thread_info.ramp_info.envelope_dying >= (i->sink->thread_info.max_rewind + pa_envelope_length(i->thread_info.ramp_info.envelope))) {
+ pa_log_debug("RELEASE Envelop");
+ i->thread_info.ramp_info.envelope_dead = TRUE;
+ sink_input_release_envelope(i);
+ }
+ }
+
/* Let's see if we had to apply the volume adjustment ourselves,
* or if this can be done by the sink for us */
@@ -793,6 +834,7 @@ void pa_sink_input_process_rewind(pa_sink_input *i, size_t nbytes /* in sink sam
if (nbytes > 0 && !i->thread_info.dont_rewind_render) {
pa_log_debug("Have to rewind %lu bytes on render memblockq.", (unsigned long) nbytes);
pa_memblockq_rewind(i->thread_info.render_memblockq, nbytes);
+ sink_input_rewind_ramp_info(i, nbytes);
}
if (i->thread_info.rewrite_nbytes == (size_t) -1) {
@@ -978,59 +1020,8 @@ static void set_real_ratio(pa_sink_input *i, const pa_cvolume *v) {
/* Called from main context */
void pa_sink_input_set_volume(pa_sink_input *i, const pa_cvolume *volume, pa_bool_t save, pa_bool_t absolute) {
- pa_cvolume v;
-
- pa_sink_input_assert_ref(i);
- pa_assert_ctl_context();
- pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
- pa_assert(volume);
- pa_assert(pa_cvolume_valid(volume));
- 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);
-
- 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)) {
- i->save_volume = i->save_volume || save;
- return;
- }
-
- i->volume = *volume;
- i->save_volume = save;
-
- if (i->sink->flags & PA_SINK_FLAT_VOLUME)
- /* We are in flat volume mode, so let's update all sink input
- * volumes and update the flat volume of the sink */
-
- pa_sink_set_volume(i->sink, NULL, TRUE, save);
-
- else {
- /* OK, we are in normal volume mode. The volume only affects
- * ourselves */
- set_real_ratio(i, volume);
-
- /* Copy the new soft_volume to the thread_info struct */
- pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_SOFT_VOLUME, NULL, 0, NULL) == 0);
- }
-
- /* The volume changed, let's tell people so */
- if (i->volume_changed)
- i->volume_changed(i);
-
- pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
+ /* test ramping -> return pa_sink_input_set_volume_with_ramping(i, volume, save, absolute, 2000 * PA_USEC_PER_MSEC); */
+ return pa_sink_input_set_volume_with_ramping(i, volume, save, absolute, 0);
}
/* Called from main context */
@@ -1049,23 +1040,8 @@ pa_cvolume *pa_sink_input_get_volume(pa_sink_input *i, pa_cvolume *volume, pa_bo
/* Called from main context */
void pa_sink_input_set_mute(pa_sink_input *i, pa_bool_t mute, pa_bool_t save) {
- pa_sink_input_assert_ref(i);
- pa_assert_ctl_context();
- pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
-
- if (!i->muted == !mute)
- return;
-
- i->muted = mute;
- i->save_muted = save;
-
- pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_SOFT_MUTE, NULL, 0, NULL) == 0);
-
- /* The mute status changed, let's tell people so */
- if (i->mute_changed)
- i->mute_changed(i);
-
- pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
+ /* test ramping -> return pa_sink_input_set_mute_with_ramping(i, mute, save, 2000 * PA_USEC_PER_MSEC); */
+ return pa_sink_input_set_mute_with_ramping(i, mute, save, 0);
}
/* Called from main context */
@@ -1441,15 +1417,23 @@ int pa_sink_input_process_msg(pa_msgobject *o, int code, void *userdata, int64_t
switch (code) {
case PA_SINK_INPUT_MESSAGE_SET_SOFT_VOLUME:
+ if (pa_atomic_load(&i->before_ramping_v))
+ i->thread_info.future_soft_volume = i->soft_volume;
+
if (!pa_cvolume_equal(&i->thread_info.soft_volume, &i->soft_volume)) {
- i->thread_info.soft_volume = i->soft_volume;
+ if (!pa_atomic_load(&i->before_ramping_v))
+ i->thread_info.soft_volume = i->soft_volume;
pa_sink_input_request_rewind(i, 0, TRUE, FALSE, FALSE);
}
return 0;
case PA_SINK_INPUT_MESSAGE_SET_SOFT_MUTE:
+ if (pa_atomic_load(&i->before_ramping_m))
+ i->thread_info.future_muted = i->muted;
+
if (i->thread_info.muted != i->muted) {
- i->thread_info.muted = i->muted;
+ if (!pa_atomic_load(&i->before_ramping_m))
+ i->thread_info.muted = i->muted;
pa_sink_input_request_rewind(i, 0, TRUE, FALSE, FALSE);
}
return 0;
@@ -1497,6 +1481,26 @@ int pa_sink_input_process_msg(pa_msgobject *o, int code, void *userdata, int64_t
*r = i->thread_info.requested_sink_latency;
return 0;
}
+
+ case PA_SINK_INPUT_MESSAGE_SET_ENVELOPE: {
+ if (!i->thread_info.ramp_info.envelope)
+ i->thread_info.ramp_info.envelope = pa_envelope_new(&i->sink->sample_spec);
+
+ if (i->thread_info.ramp_info.envelope && i->thread_info.ramp_info.item) {
+ pa_envelope_remove(i->thread_info.ramp_info.envelope, i->thread_info.ramp_info.item);
+ i->thread_info.ramp_info.item = NULL;
+ }
+
+ i->thread_info.ramp_info.item = pa_envelope_add(i->thread_info.ramp_info.envelope, &i->using_def);
+ i->thread_info.ramp_info.is_ramping = TRUE;
+ i->thread_info.ramp_info.envelope_dead = FALSE;
+ i->thread_info.ramp_info.envelope_dying = 0;
+
+ if (i->thread_info.ramp_info.envelope)
+ pa_envelope_restart(i->thread_info.ramp_info.envelope);
+
+ return 0;
+ }
}
return -PA_ERR_NOTIMPLEMENTED;
@@ -1659,3 +1663,235 @@ finish:
if (pl)
pa_proplist_free(pl);
}
+
+/* Called from IO context */
+static void sink_input_volume_ramping(pa_sink_input* i, pa_memchunk* chunk) {
+ pa_assert(i);
+ pa_assert(chunk);
+ pa_assert(chunk->memblock);
+ pa_assert(i->thread_info.ramp_info.is_ramping);
+
+ /* Volume is adjusted with ramping effect here */
+ pa_envelope_apply(i->thread_info.ramp_info.envelope, chunk);
+
+ if (pa_envelope_is_finished(i->thread_info.ramp_info.envelope)) {
+ i->thread_info.ramp_info.is_ramping = FALSE;
+ if (pa_atomic_load(&i->before_ramping_v)) {
+ i->thread_info.soft_volume = i->thread_info.future_soft_volume;
+ pa_atomic_store(&i->before_ramping_v, 0);
+ }
+ else if (pa_atomic_load(&i->before_ramping_m)) {
+ i->thread_info.muted = i->thread_info.future_muted;
+ pa_atomic_store(&i->before_ramping_m, 0);
+ }
+ }
+}
+
+/*
+ * Called from main context
+ * This function should be called inside pa_sink_input_set_volume_with_ramping
+ * should be called after soft_volume of sink_input and sink are all adjusted
+ */
+static void sink_input_set_ramping_info(pa_sink_input* i, pa_volume_t pre_virtual_volume, pa_volume_t target_virtual_volume, pa_usec_t t) {
+
+ int32_t target_abs_vol, target_apply_vol, pre_apply_vol;
+ pa_assert(i);
+
+ pa_log_debug("Sink input's soft volume is %d= %f ", pa_cvolume_avg(&i->soft_volume), pa_sw_volume_to_linear(pa_cvolume_avg(&i->soft_volume)));
+
+ /* Calculation formula are target_abs_vol := i->soft_volume
+ * target_apply_vol := lrint(pa_sw_volume_to_linear(target_abs_vol) * 0x10000)
+ * pre_apply_vol := ( previous_virtual_volume / target_virtual_volume ) * target_apply_vol
+ *
+ * Will do volume adjustment inside pa_sink_input_peek
+ */
+ target_abs_vol = pa_cvolume_avg(&i->soft_volume);
+ target_apply_vol = (int32_t) lrint(pa_sw_volume_to_linear(target_abs_vol) * 0x10000);
+ pre_apply_vol = (int32_t) ((pa_sw_volume_to_linear(pre_virtual_volume) / pa_sw_volume_to_linear(target_virtual_volume)) * target_apply_vol);
+
+ i->using_def.n_points = 2;
+ i->using_def.points_x[0] = 0;
+ i->using_def.points_x[1] = t;
+ i->using_def.points_y.i[0] = pre_apply_vol;
+ i->using_def.points_y.i[1] = target_apply_vol;
+ i->using_def.points_y.f[0] = ((float) i->using_def.points_y.i[0]) /0x10000;
+ i->using_def.points_y.f[1] = ((float) i->using_def.points_y.i[1]) /0x10000;
+
+ pa_log_debug("Volume Ramping: Point 1 is %d=%f, Point 2 is %d=%f\n", i->using_def.points_y.i[0], i->using_def.points_y.f[0],
+ i->using_def.points_y.i[1], i->using_def.points_y.f[1]);
+
+ pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_ENVELOPE, NULL, 0, NULL) == 0);
+}
+
+/* Called from main context */
+static void sink_input_set_ramping_info_for_mute(pa_sink_input* i, pa_bool_t mute, pa_usec_t t) {
+
+ int32_t cur_vol;
+ pa_assert(i);
+
+ i->using_def.n_points = 2;
+ i->using_def.points_x[0] = 0;
+ i->using_def.points_x[1] = t;
+ cur_vol = (int32_t) lrint( pa_sw_volume_to_linear(pa_cvolume_avg(&i->soft_volume)) * 0x10000);
+
+ if (mute) {
+ i->using_def.points_y.i[0] = cur_vol;
+ i->using_def.points_y.i[1] = 0;
+ } else {
+ i->using_def.points_y.i[0] = 0;
+ i->using_def.points_y.i[1] = cur_vol;
+ }
+
+ i->using_def.points_y.f[0] = ((float) i->using_def.points_y.i[0]) /0x10000;
+ i->using_def.points_y.f[1] = ((float) i->using_def.points_y.i[1]) /0x10000;
+
+ pa_log_debug("Mute Ramping: Point 1 is %d=%f, Point 2 is %d=%f\n", i->using_def.points_y.i[0], i->using_def.points_y.f[0],
+ i->using_def.points_y.i[1], i->using_def.points_y.f[1]);
+
+ pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_ENVELOPE, NULL, 0, NULL) == 0);
+}
+
+/* Called from IO context */
+static void sink_input_release_envelope(pa_sink_input *i) {
+ pa_assert(i);
+ pa_assert(!i->thread_info.ramp_info.is_ramping);
+ pa_assert(i->thread_info.ramp_info.envelope_dead);
+
+ pa_envelope_free(i->thread_info.ramp_info.envelope);
+ i->thread_info.ramp_info.envelope = NULL;
+ i->thread_info.ramp_info.item = NULL;
+}
+
+/* Called from IO context */
+static void sink_input_rewind_ramp_info(pa_sink_input *i, size_t nbytes) {
+ pa_assert(i);
+
+ if (!i->thread_info.ramp_info.envelope_dead) {
+ pa_assert(i->thread_info.ramp_info.envelope);
+
+ int32_t envelope_length = pa_envelope_length(i->thread_info.ramp_info.envelope);
+
+ if (i->thread_info.ramp_info.envelope_dying > envelope_length) {
+ if ((i->thread_info.ramp_info.envelope_dying - nbytes) < envelope_length) {
+ pa_log_debug("Envelope Become Alive");
+ pa_envelope_rewind(i->thread_info.ramp_info.envelope, envelope_length - (i->thread_info.ramp_info.envelope_dying - nbytes));
+ i->thread_info.ramp_info.is_ramping = TRUE;
+ }
+ } else if (i->thread_info.ramp_info.envelope_dying < envelope_length) {
+ if ((i->thread_info.ramp_info.envelope_dying - nbytes) <= 0) {
+ pa_log_debug("Envelope Restart");
+ pa_envelope_restart(i->thread_info.ramp_info.envelope);
+ }
+ else {
+ pa_log_debug("Envelope Simple Rewind");
+ pa_envelope_rewind(i->thread_info.ramp_info.envelope, nbytes);
+ }
+ }
+
+ i->thread_info.ramp_info.envelope_dying -= nbytes;
+ if (i->thread_info.ramp_info.envelope_dying <= 0)
+ i->thread_info.ramp_info.envelope_dying = 0;
+ }
+}
+
+void pa_sink_input_set_volume_with_ramping(pa_sink_input *i, const pa_cvolume *volume, pa_bool_t save, pa_bool_t absolute, pa_usec_t t){
+ pa_cvolume v;
+ pa_volume_t previous_virtual_volume, target_virtual_volume;
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_ctl_context();
+ pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
+ pa_assert(volume);
+ pa_assert(pa_cvolume_valid(volume));
+ 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);
+
+ 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)) {
+ i->save_volume = i->save_volume || save;
+ return;
+ }
+
+ previous_virtual_volume = pa_cvolume_avg(&i->volume);
+ target_virtual_volume = pa_cvolume_avg(volume);
+
+ if (t > 0 && target_virtual_volume > 0)
+ pa_log_debug("SetVolumeWithRamping: Virtual Volume From %u=%f to %u=%f\n", previous_virtual_volume, pa_sw_volume_to_linear(previous_virtual_volume),
+ target_virtual_volume, pa_sw_volume_to_linear(target_virtual_volume));
+
+ i->volume = *volume;
+ i->save_volume = save;
+
+ /* Set this flag before the following code modify i->thread_info.soft_volume */
+ if (t > 0 && target_virtual_volume > 0)
+ pa_atomic_store(&i->before_ramping_v, 1);
+
+ if (i->sink->flags & PA_SINK_FLAT_VOLUME) {
+ /* We are in flat volume mode, so let's update all sink input
+ * volumes and update the flat volume of the sink */
+
+ pa_sink_set_volume(i->sink, NULL, TRUE, save);
+
+ } else {
+ /* OK, we are in normal volume mode. The volume only affects
+ * ourselves */
+ set_real_ratio(i, volume);
+
+ /* Copy the new soft_volume to the thread_info struct */
+ pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_SOFT_VOLUME, NULL, 0, NULL) == 0);
+ }
+
+ if (t > 0 && target_virtual_volume > 0)
+ sink_input_set_ramping_info(i, previous_virtual_volume, target_virtual_volume, t);
+
+ /* The volume changed, let's tell people so */
+ if (i->volume_changed)
+ i->volume_changed(i);
+
+ /* The virtual volume changed, let's tell people so */
+ pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
+}
+
+void pa_sink_input_set_mute_with_ramping(pa_sink_input *i, pa_bool_t mute, pa_bool_t save, pa_usec_t t){
+
+ pa_sink_input_assert_ref(i);
+ pa_assert_ctl_context();
+ pa_assert(PA_SINK_INPUT_IS_LINKED(i->state));
+
+ if (!i->muted == !mute) {
+ i->save_muted = i->save_muted || mute;
+ return;
+ }
+
+ i->muted = mute;
+ i->save_muted = save;
+
+ /* Set this flag before the following code modify i->thread_info.muted, otherwise distortion will be heard */
+ if (t > 0)
+ pa_atomic_store(&i->before_ramping_m, 1);
+
+ pa_assert_se(pa_asyncmsgq_send(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_SOFT_MUTE, NULL, 0, NULL) == 0);
+
+ if (t > 0)
+ sink_input_set_ramping_info_for_mute(i, mute, t);
+
+ /* The mute status changed, let's tell people so */
+ if (i->mute_changed)
+ i->mute_changed(i);
+
+ pa_subscription_post(i->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
+}
diff --git a/src/pulsecore/sink-input.h b/src/pulsecore/sink-input.h
index 415a801f..56ac3d60 100644
--- a/src/pulsecore/sink-input.h
+++ b/src/pulsecore/sink-input.h
@@ -35,6 +35,7 @@ typedef struct pa_sink_input pa_sink_input;
#include <pulsecore/client.h>
#include <pulsecore/sink.h>
#include <pulsecore/core.h>
+#include <pulsecore/envelope.h>
typedef enum pa_sink_input_state {
PA_SINK_INPUT_INIT, /*< The stream is not active yet, because pa_sink_put() has not been called yet */
@@ -232,8 +233,23 @@ struct pa_sink_input {
pa_usec_t requested_sink_latency;
pa_hashmap *direct_outputs;
+
+ struct {
+ pa_bool_t is_ramping:1;
+ pa_bool_t envelope_dead:1;
+ int32_t envelope_dying; /* Increasing while envelop is not dead. Reduce it while process_rewind. */
+ pa_envelope *envelope;
+ pa_envelope_item *item;
+ } ramp_info;
+ pa_cvolume future_soft_volume;
+ pa_bool_t future_muted;
+
} thread_info;
+ pa_atomic_t before_ramping_v; /* Indicates future volume */
+ pa_atomic_t before_ramping_m; /* Indicates future mute */
+ pa_envelope_def using_def;
+
void *userdata;
};
@@ -248,6 +264,7 @@ enum {
PA_SINK_INPUT_MESSAGE_SET_STATE,
PA_SINK_INPUT_MESSAGE_SET_REQUESTED_LATENCY,
PA_SINK_INPUT_MESSAGE_GET_REQUESTED_LATENCY,
+ PA_SINK_INPUT_MESSAGE_SET_ENVELOPE,
PA_SINK_INPUT_MESSAGE_MAX
};
@@ -384,4 +401,8 @@ pa_memchunk* pa_sink_input_get_silence(pa_sink_input *i, pa_memchunk *ret);
#define pa_sink_input_assert_io_context(s) \
pa_assert(pa_thread_mq_get() || !PA_SINK_INPUT_IS_LINKED((s)->state))
+/* Volume ramping*/
+void pa_sink_input_set_volume_with_ramping(pa_sink_input *i, const pa_cvolume *volume, pa_bool_t save, pa_bool_t absolute, pa_usec_t t);
+void pa_sink_input_set_mute_with_ramping(pa_sink_input *i, pa_bool_t mute, pa_bool_t save, pa_usec_t t);
+
#endif
diff --git a/src/pulsecore/sink.c b/src/pulsecore/sink.c
index bda92fcc..24fad34d 100644
--- a/src/pulsecore/sink.c
+++ b/src/pulsecore/sink.c
@@ -926,18 +926,16 @@ void pa_sink_render(pa_sink*s, size_t length, pa_memchunk *result) {
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, 0);
- pa_volume_memchunk(result, &s->sample_spec, &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 if (!pa_cvolume_is_norm(&volume)) {
+ pa_memchunk_make_writable(result, 0);
+ pa_volume_memchunk(result, &s->sample_spec, &volume);
}
} else {
void *ptr;
@@ -1342,7 +1340,7 @@ static void propagate_reference_volume(pa_sink *s) {
void pa_sink_set_volume(
pa_sink *s,
const pa_cvolume *volume,
- pa_bool_t sendmsg,
+ pa_bool_t send_msg,
pa_bool_t save) {
pa_cvolume old_reference_volume;
@@ -1411,7 +1409,7 @@ void pa_sink_set_volume(
s->soft_volume = s->real_volume;
/* This tells the sink that soft and/or virtual volume changed */
- if (sendmsg)
+ if (send_msg)
pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_SET_VOLUME, NULL, 0, NULL) == 0);
if (reference_changed)
@@ -1733,10 +1731,14 @@ static void sync_input_volumes_within_thread(pa_sink *s) {
pa_sink_assert_io_context(s);
PA_HASHMAP_FOREACH(i, s->thread_info.inputs, state) {
+ if (pa_atomic_load(&i->before_ramping_v))
+ i->thread_info.future_soft_volume = i->soft_volume;
+
if (pa_cvolume_equal(&i->thread_info.soft_volume, &i->soft_volume))
continue;
- i->thread_info.soft_volume = i->soft_volume;
+ if (!pa_atomic_load(&i->before_ramping_v))
+ i->thread_info.soft_volume = i->soft_volume;
pa_sink_input_request_rewind(i, 0, TRUE, FALSE, FALSE);
}
}
diff --git a/src/pulsecore/socket-client.c b/src/pulsecore/socket-client.c
index b9d69505..ef3c29ee 100644
--- a/src/pulsecore/socket-client.c
+++ b/src/pulsecore/socket-client.c
@@ -257,13 +257,11 @@ static int sockaddr_prepare(pa_socket_client *c, const struct sockaddr *sa, size
c->local = pa_socket_address_is_local(sa);
- if ((c->fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) {
+ if ((c->fd = pa_socket_cloexec(sa->sa_family, SOCK_STREAM, 0)) < 0) {
pa_log("socket(): %s", pa_cstrerror(errno));
return -1;
}
- pa_make_fd_cloexec(c->fd);
-
#ifdef HAVE_IPV6
if (sa->sa_family == AF_INET || sa->sa_family == AF_INET6)
#else
diff --git a/src/pulsecore/socket-server.c b/src/pulsecore/socket-server.c
index e660700c..5d55de3e 100644
--- a/src/pulsecore/socket-server.c
+++ b/src/pulsecore/socket-server.c
@@ -104,13 +104,11 @@ static void callback(pa_mainloop_api *mainloop, pa_io_event *e, int fd, pa_io_ev
pa_socket_server_ref(s);
- if ((nfd = accept(fd, NULL, NULL)) < 0) {
+ if ((nfd = pa_accept_cloexec(fd, NULL, NULL)) < 0) {
pa_log("accept(): %s", pa_cstrerror(errno));
goto finish;
}
- pa_make_fd_cloexec(nfd);
-
if (!s->on_connection) {
pa_close(nfd);
goto finish;
@@ -186,13 +184,11 @@ pa_socket_server* pa_socket_server_new_unix(pa_mainloop_api *m, const char *file
pa_assert(m);
pa_assert(filename);
- if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
+ if ((fd = pa_socket_cloexec(PF_UNIX, SOCK_STREAM, 0)) < 0) {
pa_log("socket(): %s", pa_cstrerror(errno));
goto fail;
}
- pa_make_fd_cloexec(fd);
-
memset(&sa, 0, sizeof(sa));
sa.sun_family = AF_UNIX;
pa_strlcpy(sa.sun_path, filename, sizeof(sa.sun_path));
@@ -246,13 +242,11 @@ pa_socket_server* pa_socket_server_new_ipv4(pa_mainloop_api *m, uint32_t address
pa_assert(m);
pa_assert(port);
- if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
+ if ((fd = pa_socket_cloexec(PF_INET, SOCK_STREAM, 0)) < 0) {
pa_log("socket(PF_INET): %s", pa_cstrerror(errno));
goto fail;
}
- pa_make_fd_cloexec(fd);
-
#ifdef SO_REUSEADDR
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
pa_log("setsockopt(): %s", pa_cstrerror(errno));
@@ -299,13 +293,11 @@ pa_socket_server* pa_socket_server_new_ipv6(pa_mainloop_api *m, const uint8_t ad
pa_assert(m);
pa_assert(port > 0);
- if ((fd = socket(PF_INET6, SOCK_STREAM, 0)) < 0) {
+ if ((fd = pa_socket_cloexec(PF_INET6, SOCK_STREAM, 0)) < 0) {
pa_log("socket(PF_INET6): %s", pa_cstrerror(errno));
goto fail;
}
- pa_make_fd_cloexec(fd);
-
#ifdef IPV6_V6ONLY
on = 1;
if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0)
diff --git a/src/pulsecore/socket-util.c b/src/pulsecore/socket-util.c
index 5fd5dd67..2cc9882a 100644
--- a/src/pulsecore/socket-util.c
+++ b/src/pulsecore/socket-util.c
@@ -85,12 +85,11 @@ void pa_socket_peer_to_string(int fd, char *c, size_t l) {
#ifndef OS_IS_WIN32
pa_assert_se(fstat(fd, &st) == 0);
-#endif
-#ifndef OS_IS_WIN32
if (S_ISSOCK(st.st_mode)) {
#endif
union {
+ struct sockaddr_storage storage;
struct sockaddr sa;
struct sockaddr_in in;
#ifdef HAVE_IPV6
@@ -152,7 +151,7 @@ void pa_make_socket_low_delay(int fd) {
pa_assert(fd >= 0);
priority = 6;
- if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY, (void*)&priority, sizeof(priority)) < 0)
+ if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &priority, sizeof(priority)) < 0)
pa_log_warn("SO_PRIORITY failed: %s", pa_cstrerror(errno));
#endif
}
@@ -166,9 +165,9 @@ void pa_make_tcp_socket_low_delay(int fd) {
{
int on = 1;
#if defined(SOL_TCP)
- if (setsockopt(fd, SOL_TCP, TCP_NODELAY, (void*)&on, sizeof(on)) < 0)
+ if (setsockopt(fd, SOL_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
#else
- if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on)) < 0)
+ if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0)
#endif
pa_log_warn("TCP_NODELAY failed: %s", pa_cstrerror(errno));
}
@@ -178,9 +177,9 @@ void pa_make_tcp_socket_low_delay(int fd) {
{
int tos = IPTOS_LOWDELAY;
#ifdef SOL_IP
- if (setsockopt(fd, SOL_IP, IP_TOS, (void*)&tos, sizeof(tos)) < 0)
+ if (setsockopt(fd, SOL_IP, IP_TOS, &tos, sizeof(tos)) < 0)
#else
- if (setsockopt(fd, IPPROTO_IP, IP_TOS, (void*)&tos, sizeof(tos)) < 0)
+ if (setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
#endif
pa_log_warn("IP_TOS failed: %s", pa_cstrerror(errno));
}
@@ -196,9 +195,9 @@ void pa_make_udp_socket_low_delay(int fd) {
{
int tos = IPTOS_LOWDELAY;
#ifdef SOL_IP
- if (setsockopt(fd, SOL_IP, IP_TOS, (void*)&tos, sizeof(tos)) < 0)
+ if (setsockopt(fd, SOL_IP, IP_TOS, &tos, sizeof(tos)) < 0)
#else
- if (setsockopt(fd, IPPROTO_IP, IP_TOS, (void*)&tos, sizeof(tos)) < 0)
+ if (setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
#endif
pa_log_warn("IP_TOS failed: %s", pa_cstrerror(errno));
}
@@ -206,11 +205,11 @@ void pa_make_udp_socket_low_delay(int fd) {
}
int pa_socket_set_rcvbuf(int fd, size_t l) {
- int bufsz = (int)l;
+ int bufsz = (int) l;
pa_assert(fd >= 0);
- if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void*)&bufsz, sizeof(bufsz)) < 0) {
+ if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bufsz, sizeof(bufsz)) < 0) {
pa_log_warn("SO_RCVBUF: %s", pa_cstrerror(errno));
return -1;
}
@@ -219,12 +218,12 @@ int pa_socket_set_rcvbuf(int fd, size_t l) {
}
int pa_socket_set_sndbuf(int fd, size_t l) {
- int bufsz = (int)l;
+ int bufsz = (int) l;
pa_assert(fd >= 0);
- if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void*)&bufsz, sizeof(bufsz)) < 0) {
- pa_log("SO_SNDBUF: %s", pa_cstrerror(errno));
+ if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &bufsz, sizeof(bufsz)) < 0) {
+ pa_log_warn("SO_SNDBUF: %s", pa_cstrerror(errno));
return -1;
}
@@ -239,7 +238,7 @@ int pa_unix_socket_is_stale(const char *fn) {
pa_assert(fn);
- if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
+ if ((fd = pa_socket_cloexec(PF_UNIX, SOCK_STREAM, 0)) < 0) {
pa_log("socket(): %s", pa_cstrerror(errno));
goto finish;
}
@@ -315,6 +314,7 @@ pa_bool_t pa_socket_address_is_local(const struct sockaddr *sa) {
pa_bool_t pa_socket_is_local(int fd) {
union {
+ struct sockaddr_storage storage;
struct sockaddr sa;
struct sockaddr_in in;
#ifdef HAVE_IPV6
diff --git a/src/pulsecore/sound-file-stream.c b/src/pulsecore/sound-file-stream.c
index 16de4923..53674ba1 100644
--- a/src/pulsecore/sound-file-stream.c
+++ b/src/pulsecore/sound-file-stream.c
@@ -252,11 +252,7 @@ int pa_play_file(
u->readf_function = NULL;
u->memblockq = NULL;
- if ((fd = open(fname, O_RDONLY
-#ifdef O_NOCTTY
- |O_NOCTTY
-#endif
- )) < 0) {
+ if ((fd = pa_open_cloexec(fname, O_RDONLY, 0)) < 0) {
pa_log("Failed to open file %s: %s", fname, pa_cstrerror(errno));
goto fail;
}
diff --git a/src/pulsecore/sound-file.c b/src/pulsecore/sound-file.c
index 2d9b76ad..d8c10b1e 100644
--- a/src/pulsecore/sound-file.c
+++ b/src/pulsecore/sound-file.c
@@ -62,11 +62,7 @@ int pa_sound_file_load(
pa_memchunk_reset(chunk);
- if ((fd = open(fname, O_RDONLY
-#ifdef O_NOCTTY
- |O_NOCTTY
-#endif
- )) < 0) {
+ if ((fd = pa_open_cloexec(fname, O_RDONLY, 0)) < 0) {
pa_log("Failed to open file %s: %s", fname, pa_cstrerror(errno));
goto finish;
}
diff --git a/src/pulsecore/svolume_mmx.c b/src/pulsecore/svolume_mmx.c
index 745c7de0..a011789c 100644
--- a/src/pulsecore/svolume_mmx.c
+++ b/src/pulsecore/svolume_mmx.c
@@ -25,6 +25,8 @@
#endif
#include <pulse/timeval.h>
+#include <pulse/rtclock.h>
+
#include <pulsecore/random.h>
#include <pulsecore/macro.h>
#include <pulsecore/g711.h>
@@ -60,7 +62,9 @@
" movq "#s", %%mm5 \n\t" \
" pmulhw "#v", "#s" \n\t" /* .. | 0 | vl*p0 | */ \
" paddw %%mm4, "#s" \n\t" /* .. | 0 | vl*p0 | + sign correct */ \
+ " pslld $16, "#s" \n\t" /* .. | vl*p0 | 0 | */ \
" psrld $16, "#v" \n\t" /* .. | 0 | vh | */ \
+ " psrad $16, "#s" \n\t" /* .. | vl*p0 | sign extend */ \
" pmaddwd %%mm5, "#v" \n\t" /* .. | p0 * vh | */ \
" paddd "#s", "#v" \n\t" /* .. | p0 * v0 | */ \
" packssdw "#v", "#v" \n\t" /* .. | p1*v1 | p0*v0 | */
@@ -255,11 +259,14 @@ static void run_test (void) {
printf ("checking MMX %zd\n", sizeof (samples));
pa_random (samples, sizeof (samples));
+ /* for (i = 0; i < SAMPLES; i++)
+ samples[i] = -1; */
memcpy (samples_ref, samples, sizeof (samples));
memcpy (samples_orig, samples, sizeof (samples));
for (i = 0; i < CHANNELS; i++)
volumes[i] = rand() >> 1;
+ /* volumes[i] = 0x0000ffff; */
for (padding = 0; padding < PADDING; padding++, i++)
volumes[i] = volumes[padding];
@@ -267,7 +274,7 @@ static void run_test (void) {
pa_volume_s16ne_mmx (samples, volumes, CHANNELS, sizeof (samples));
for (i = 0; i < SAMPLES; i++) {
if (samples[i] != samples_ref[i]) {
- printf ("%d: %04x != %04x (%04x * %04x)\n", i, samples[i], samples_ref[i],
+ printf ("%d: %04x != %04x (%04x * %08x)\n", i, samples[i], samples_ref[i],
samples_orig[i], volumes[i % CHANNELS]);
}
}
@@ -287,6 +294,8 @@ static void run_test (void) {
}
stop = pa_rtclock_now();
pa_log_info("ref: %llu usec.", (long long unsigned int)(stop - start));
+
+ pa_assert_se(memcmp(samples_ref, samples, sizeof(samples)) == 0);
}
#endif
diff --git a/src/pulsecore/svolume_sse.c b/src/pulsecore/svolume_sse.c
index 1cc4e0aa..620524fa 100644
--- a/src/pulsecore/svolume_sse.c
+++ b/src/pulsecore/svolume_sse.c
@@ -25,6 +25,8 @@
#endif
#include <pulse/timeval.h>
+#include <pulse/rtclock.h>
+
#include <pulsecore/random.h>
#include <pulsecore/macro.h>
#include <pulsecore/g711.h>
@@ -261,7 +263,7 @@ static void run_test (void) {
func = pa_get_volume_func (PA_SAMPLE_S16NE);
- printf ("checking SSE %zd\n", sizeof (samples));
+ printf ("checking SSE2 %zd\n", sizeof (samples));
pa_random (samples, sizeof (samples));
memcpy (samples_ref, samples, sizeof (samples));
@@ -273,7 +275,7 @@ static void run_test (void) {
volumes[i] = volumes[padding];
func (samples_ref, volumes, CHANNELS, sizeof (samples));
- pa_volume_s16ne_sse (samples, volumes, CHANNELS, sizeof (samples));
+ pa_volume_s16ne_sse2 (samples, volumes, CHANNELS, sizeof (samples));
for (i = 0; i < SAMPLES; i++) {
if (samples[i] != samples_ref[i]) {
printf ("%d: %04x != %04x (%04x * %04x)\n", i, samples[i], samples_ref[i],
@@ -284,7 +286,7 @@ static void run_test (void) {
start = pa_rtclock_now();
for (j = 0; j < TIMES; j++) {
memcpy (samples, samples_orig, sizeof (samples));
- pa_volume_s16ne_sse (samples, volumes, CHANNELS, sizeof (samples));
+ pa_volume_s16ne_sse2 (samples, volumes, CHANNELS, sizeof (samples));
}
stop = pa_rtclock_now();
pa_log_info("SSE: %llu usec.", (long long unsigned int)(stop - start));
@@ -296,6 +298,8 @@ static void run_test (void) {
}
stop = pa_rtclock_now();
pa_log_info("ref: %llu usec.", (long long unsigned int)(stop - start));
+
+ pa_assert_se(memcmp(samples_ref, samples, sizeof(samples)) == 0);
}
#endif
#endif /* defined (__i386__) || defined (__amd64__) */
diff --git a/src/pulsecore/time-smoother.c b/src/pulsecore/time-smoother.c
index d6c37878..1371ad56 100644
--- a/src/pulsecore/time-smoother.c
+++ b/src/pulsecore/time-smoother.c
@@ -196,6 +196,13 @@ static double avg_gradient(pa_smoother *s, pa_usec_t x) {
int64_t ax = 0, ay = 0, k, t;
double r;
+ /* FIXME: Optimization: Jason Newton suggested that instead of
+ * going through the history on each iteration we could calculated
+ * avg_gradient() as we go.
+ *
+ * Second idea: it might make sense to weight history entries:
+ * more recent entries should matter more than old ones. */
+
/* Too few measurements, assume gradient of 1 */
if (s->n_history < s->min_history)
return 1;
diff --git a/src/pulsecore/x11prop.c b/src/pulsecore/x11prop.c
index 873a76e7..dc8ec294 100644
--- a/src/pulsecore/x11prop.c
+++ b/src/pulsecore/x11prop.c
@@ -32,12 +32,12 @@
void pa_x11_set_prop(Display *d, const char *name, const char *data) {
Atom a = XInternAtom(d, name, False);
- XChangeProperty(d, RootWindow(d, 0), a, XA_STRING, 8, PropModeReplace, (const unsigned char*) data, (int) (strlen(data)+1));
+ XChangeProperty(d, DefaultRootWindow(d), a, XA_STRING, 8, PropModeReplace, (const unsigned char*) data, (int) (strlen(data)+1));
}
void pa_x11_del_prop(Display *d, const char *name) {
Atom a = XInternAtom(d, name, False);
- XDeleteProperty(d, RootWindow(d, 0), a);
+ XDeleteProperty(d, DefaultRootWindow(d), a);
}
char* pa_x11_get_prop(Display *d, const char *name, char *p, size_t l) {
@@ -47,13 +47,21 @@ char* pa_x11_get_prop(Display *d, const char *name, char *p, size_t l) {
unsigned long nbytes_after;
unsigned char *prop = NULL;
char *ret = NULL;
+ int window_ret;
Atom a = XInternAtom(d, name, False);
- if (XGetWindowProperty(d, RootWindow(d, 0), a, 0, (long) ((l+2)/4), False, XA_STRING, &actual_type, &actual_format, &nitems, &nbytes_after, &prop) != Success)
- goto finish;
- if (actual_type != XA_STRING)
- goto finish;
+ window_ret = XGetWindowProperty(d, DefaultRootWindow(d), a, 0, (long) ((l+2)/4), False, XA_STRING, &actual_type, &actual_format, &nitems, &nbytes_after, &prop);
+
+ if (window_ret != Success || actual_type != XA_STRING) {
+ if (DefaultScreen(d) != 0) {
+ window_ret = XGetWindowProperty(d, RootWindow(d, 0), a, 0, (long) ((l+2)/4), False, XA_STRING, &actual_type, &actual_format, &nitems, &nbytes_after, &prop);
+
+ if (window_ret != Success || actual_type != XA_STRING)
+ goto finish;
+ } else
+ goto finish;
+ }
memcpy(p, prop, nitems);
p[nitems] = 0;