2006-05-23 14:42:23 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2006-05-23 14:42:23 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2006-05-23 14:42:23 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
|
|
|
|
by the Free Software Foundation; either version 2 of the License,
|
|
|
|
|
or (at your option) any later version.
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2006-05-23 14:42:23 +00:00
|
|
|
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
|
2006-06-19 21:53:48 +00:00
|
|
|
along with PulseAudio; if not, write to the Free Software
|
2006-05-23 14:42:23 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef _FILE_OFFSET_BITS
|
|
|
|
|
#undef _FILE_OFFSET_BITS
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifndef _LARGEFILE64_SOURCE
|
|
|
|
|
#define _LARGEFILE64_SOURCE 1
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <sys/soundcard.h>
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
|
|
#include <linux/sockios.h>
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulse/pulseaudio.h>
|
|
|
|
|
#include <pulsecore/llist.h>
|
|
|
|
|
#include <pulsecore/gccmacro.h>
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
|
FD_INFO_MIXER,
|
2006-06-16 21:07:32 +00:00
|
|
|
FD_INFO_STREAM,
|
2006-05-23 14:42:23 +00:00
|
|
|
} fd_info_type_t;
|
|
|
|
|
|
|
|
|
|
typedef struct fd_info fd_info;
|
|
|
|
|
|
|
|
|
|
struct fd_info {
|
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
|
int ref;
|
2006-05-23 23:06:28 +00:00
|
|
|
int unusable;
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
fd_info_type_t type;
|
|
|
|
|
int app_fd, thread_fd;
|
|
|
|
|
|
|
|
|
|
pa_sample_spec sample_spec;
|
|
|
|
|
size_t fragment_size;
|
|
|
|
|
unsigned n_fragments;
|
|
|
|
|
|
|
|
|
|
pa_threaded_mainloop *mainloop;
|
|
|
|
|
pa_context *context;
|
2006-06-16 21:07:32 +00:00
|
|
|
pa_stream *play_stream;
|
|
|
|
|
pa_stream *rec_stream;
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
pa_io_event *io_event;
|
|
|
|
|
|
|
|
|
|
void *buf;
|
2006-06-16 21:07:32 +00:00
|
|
|
size_t rec_offset;
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
int operation_success;
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
pa_cvolume sink_volume, source_volume;
|
|
|
|
|
uint32_t sink_index, source_index;
|
2006-05-24 02:13:29 +00:00
|
|
|
int volume_modify_count;
|
|
|
|
|
|
2006-05-23 14:42:23 +00:00
|
|
|
PA_LLIST_FIELDS(fd_info);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int dsp_drain(fd_info *i);
|
2006-05-23 23:06:28 +00:00
|
|
|
static void fd_info_remove_from_list(fd_info *i);
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
static pthread_mutex_t fd_infos_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
static pthread_mutex_t func_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
|
|
|
|
|
static PA_LLIST_HEAD(fd_info, fd_infos) = NULL;
|
|
|
|
|
|
|
|
|
|
static int (*_ioctl)(int, int, void*) = NULL;
|
|
|
|
|
static int (*_close)(int) = NULL;
|
|
|
|
|
static int (*_open)(const char *, int, mode_t) = NULL;
|
|
|
|
|
static FILE* (*_fopen)(const char *path, const char *mode) = NULL;
|
|
|
|
|
static int (*_open64)(const char *, int, mode_t) = NULL;
|
|
|
|
|
static FILE* (*_fopen64)(const char *path, const char *mode) = NULL;
|
|
|
|
|
static int (*_fclose)(FILE *f) = NULL;
|
2006-06-13 13:21:14 +00:00
|
|
|
static int (*_access)(const char *, int) = NULL;
|
2006-05-23 14:42:23 +00:00
|
|
|
|
2006-05-24 13:23:15 +00:00
|
|
|
/* dlsym() violates ISO C, so confide the breakage into this function to
|
|
|
|
|
* avoid warnings. */
|
|
|
|
|
typedef void (*fnptr)(void);
|
|
|
|
|
static inline fnptr dlsym_fn(void *handle, const char *symbol) {
|
|
|
|
|
return (fnptr) (long) dlsym(handle, symbol);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-23 14:42:23 +00:00
|
|
|
#define LOAD_IOCTL_FUNC() \
|
|
|
|
|
do { \
|
|
|
|
|
pthread_mutex_lock(&func_mutex); \
|
|
|
|
|
if (!_ioctl) \
|
2006-05-24 13:23:15 +00:00
|
|
|
_ioctl = (int (*)(int, int, void*)) dlsym_fn(RTLD_NEXT, "ioctl"); \
|
2006-05-23 14:42:23 +00:00
|
|
|
pthread_mutex_unlock(&func_mutex); \
|
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
|
|
#define LOAD_OPEN_FUNC() \
|
|
|
|
|
do { \
|
|
|
|
|
pthread_mutex_lock(&func_mutex); \
|
|
|
|
|
if (!_open) \
|
2006-05-24 13:23:15 +00:00
|
|
|
_open = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open"); \
|
2006-05-23 14:42:23 +00:00
|
|
|
pthread_mutex_unlock(&func_mutex); \
|
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
|
|
#define LOAD_OPEN64_FUNC() \
|
|
|
|
|
do { \
|
|
|
|
|
pthread_mutex_lock(&func_mutex); \
|
|
|
|
|
if (!_open64) \
|
2006-05-24 13:23:15 +00:00
|
|
|
_open64 = (int (*)(const char *, int, mode_t)) dlsym_fn(RTLD_NEXT, "open64"); \
|
2006-05-23 14:42:23 +00:00
|
|
|
pthread_mutex_unlock(&func_mutex); \
|
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
|
|
#define LOAD_CLOSE_FUNC() \
|
|
|
|
|
do { \
|
|
|
|
|
pthread_mutex_lock(&func_mutex); \
|
|
|
|
|
if (!_close) \
|
2006-05-24 13:23:15 +00:00
|
|
|
_close = (int (*)(int)) dlsym_fn(RTLD_NEXT, "close"); \
|
2006-05-23 14:42:23 +00:00
|
|
|
pthread_mutex_unlock(&func_mutex); \
|
|
|
|
|
} while(0)
|
|
|
|
|
|
2006-06-13 13:21:14 +00:00
|
|
|
#define LOAD_ACCESS_FUNC() \
|
|
|
|
|
do { \
|
|
|
|
|
pthread_mutex_lock(&func_mutex); \
|
|
|
|
|
if (!_access) \
|
|
|
|
|
_access = (int (*)(const char*, int)) dlsym_fn(RTLD_NEXT, "access"); \
|
|
|
|
|
pthread_mutex_unlock(&func_mutex); \
|
|
|
|
|
} while(0)
|
|
|
|
|
|
2006-05-23 14:42:23 +00:00
|
|
|
#define LOAD_FOPEN_FUNC() \
|
|
|
|
|
do { \
|
|
|
|
|
pthread_mutex_lock(&func_mutex); \
|
|
|
|
|
if (!_fopen) \
|
2006-05-24 13:23:15 +00:00
|
|
|
_fopen = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen"); \
|
2006-05-23 14:42:23 +00:00
|
|
|
pthread_mutex_unlock(&func_mutex); \
|
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
|
|
#define LOAD_FOPEN64_FUNC() \
|
|
|
|
|
do { \
|
|
|
|
|
pthread_mutex_lock(&func_mutex); \
|
|
|
|
|
if (!_fopen64) \
|
2006-05-24 13:23:15 +00:00
|
|
|
_fopen64 = (FILE* (*)(const char *, const char*)) dlsym_fn(RTLD_NEXT, "fopen64"); \
|
2006-05-23 14:42:23 +00:00
|
|
|
pthread_mutex_unlock(&func_mutex); \
|
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
|
|
#define LOAD_FCLOSE_FUNC() \
|
|
|
|
|
do { \
|
|
|
|
|
pthread_mutex_lock(&func_mutex); \
|
|
|
|
|
if (!_fclose) \
|
2006-05-24 13:23:15 +00:00
|
|
|
_fclose = (int (*)(FILE *)) dlsym_fn(RTLD_NEXT, "fclose"); \
|
2006-05-23 14:42:23 +00:00
|
|
|
pthread_mutex_unlock(&func_mutex); \
|
|
|
|
|
} while(0)
|
|
|
|
|
|
2006-05-24 02:13:29 +00:00
|
|
|
#define CONTEXT_CHECK_DEAD_GOTO(i, label) do { \
|
|
|
|
|
if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY) { \
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
|
2006-05-24 02:13:29 +00:00
|
|
|
goto label; \
|
|
|
|
|
} \
|
|
|
|
|
} while(0);
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
#define PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, label) do { \
|
2006-05-24 02:13:29 +00:00
|
|
|
if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
|
2006-06-16 21:07:32 +00:00
|
|
|
!(i)->play_stream || pa_stream_get_state((i)->play_stream) != PA_STREAM_READY) { \
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
|
|
|
|
|
goto label; \
|
|
|
|
|
} \
|
|
|
|
|
} while(0);
|
|
|
|
|
|
|
|
|
|
#define RECORD_STREAM_CHECK_DEAD_GOTO(i, label) do { \
|
|
|
|
|
if (!(i)->context || pa_context_get_state((i)->context) != PA_CONTEXT_READY || \
|
|
|
|
|
!(i)->rec_stream || pa_stream_get_state((i)->rec_stream) != PA_STREAM_READY) { \
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": Not connected: %s", (i)->context ? pa_strerror(pa_context_errno((i)->context)) : "NULL"); \
|
2006-05-24 02:13:29 +00:00
|
|
|
goto label; \
|
|
|
|
|
} \
|
|
|
|
|
} while(0);
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
static void debug(int level, const char *format, ...) PA_GCC_PRINTF_ATTR(2,3);
|
2006-05-23 14:42:23 +00:00
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
#define DEBUG_LEVEL_ALWAYS 0
|
|
|
|
|
#define DEBUG_LEVEL_NORMAL 1
|
|
|
|
|
#define DEBUG_LEVEL_VERBOSE 2
|
|
|
|
|
|
|
|
|
|
static void debug(int level, const char *format, ...) {
|
2006-05-23 14:42:23 +00:00
|
|
|
va_list ap;
|
2006-06-15 14:47:14 +00:00
|
|
|
const char *dlevel_s;
|
|
|
|
|
int dlevel;
|
|
|
|
|
|
|
|
|
|
dlevel_s = getenv("PADSP_DEBUG");
|
|
|
|
|
if (!dlevel_s)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
dlevel = atoi(dlevel_s);
|
|
|
|
|
|
|
|
|
|
if (dlevel < level)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
|
vfprintf(stderr, format, ap);
|
|
|
|
|
va_end(ap);
|
2006-05-23 14:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
2006-05-25 01:14:06 +00:00
|
|
|
static int padsp_disabled(void) {
|
|
|
|
|
static int *sym;
|
|
|
|
|
static int sym_resolved = 0;
|
|
|
|
|
|
|
|
|
|
/* If the current process has a symbol __padsp_disabled__ we use
|
|
|
|
|
* it to detect whether we should enable our stuff or not. A
|
|
|
|
|
* program needs to be compiled with -rdynamic for this to work!
|
|
|
|
|
* The symbol must be an int containing a three bit bitmask: bit 1
|
|
|
|
|
* -> disable /dev/dsp emulation, bit 2 -> disable /dev/sndstat
|
|
|
|
|
* emulation, bit 3 -> disable /dev/mixer emulation. Hence a value
|
2006-05-25 01:19:56 +00:00
|
|
|
* of 7 disables padsp entirely. */
|
2006-05-25 01:14:06 +00:00
|
|
|
|
|
|
|
|
pthread_mutex_lock(&func_mutex);
|
|
|
|
|
if (!sym_resolved) {
|
2006-05-25 17:18:42 +00:00
|
|
|
sym = (int*) dlsym(RTLD_DEFAULT, "__padsp_disabled__");
|
2006-05-25 01:14:06 +00:00
|
|
|
sym_resolved = 1;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
pthread_mutex_unlock(&func_mutex);
|
|
|
|
|
|
|
|
|
|
if (!sym)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
return *sym;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int dsp_cloak_enable(void) {
|
|
|
|
|
if (padsp_disabled() & 1)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (getenv("PADSP_NO_DSP"))
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int sndstat_cloak_enable(void) {
|
|
|
|
|
if (padsp_disabled() & 2)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (getenv("PADSP_NO_SNDSTAT"))
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int mixer_cloak_enable(void) {
|
|
|
|
|
if (padsp_disabled() & 4)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (getenv("PADSP_NO_MIXER"))
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2006-05-23 14:42:23 +00:00
|
|
|
static pthread_key_t recursion_key;
|
|
|
|
|
|
|
|
|
|
static void recursion_key_alloc(void) {
|
|
|
|
|
pthread_key_create(&recursion_key, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int function_enter(void) {
|
|
|
|
|
/* Avoid recursive calls */
|
|
|
|
|
static pthread_once_t recursion_key_once = PTHREAD_ONCE_INIT;
|
|
|
|
|
pthread_once(&recursion_key_once, recursion_key_alloc);
|
|
|
|
|
|
|
|
|
|
if (pthread_getspecific(recursion_key))
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
pthread_setspecific(recursion_key, (void*) 1);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void function_exit(void) {
|
|
|
|
|
pthread_setspecific(recursion_key, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void fd_info_free(fd_info *i) {
|
|
|
|
|
assert(i);
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": freeing fd info (fd=%i)\n", i->app_fd);
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
dsp_drain(i);
|
|
|
|
|
|
|
|
|
|
if (i->mainloop)
|
|
|
|
|
pa_threaded_mainloop_stop(i->mainloop);
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
if (i->play_stream) {
|
|
|
|
|
pa_stream_disconnect(i->play_stream);
|
|
|
|
|
pa_stream_unref(i->play_stream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (i->rec_stream) {
|
|
|
|
|
pa_stream_disconnect(i->rec_stream);
|
|
|
|
|
pa_stream_unref(i->rec_stream);
|
2006-05-23 14:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (i->context) {
|
|
|
|
|
pa_context_disconnect(i->context);
|
|
|
|
|
pa_context_unref(i->context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (i->mainloop)
|
|
|
|
|
pa_threaded_mainloop_free(i->mainloop);
|
|
|
|
|
|
|
|
|
|
if (i->app_fd >= 0) {
|
|
|
|
|
LOAD_CLOSE_FUNC();
|
|
|
|
|
_close(i->app_fd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (i->thread_fd >= 0) {
|
|
|
|
|
LOAD_CLOSE_FUNC();
|
|
|
|
|
_close(i->thread_fd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(i->buf);
|
|
|
|
|
|
|
|
|
|
pthread_mutex_destroy(&i->mutex);
|
|
|
|
|
free(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static fd_info *fd_info_ref(fd_info *i) {
|
|
|
|
|
assert(i);
|
|
|
|
|
|
|
|
|
|
pthread_mutex_lock(&i->mutex);
|
|
|
|
|
assert(i->ref >= 1);
|
|
|
|
|
i->ref++;
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_VERBOSE, __FILE__": ref++, now %i\n", i->ref);
|
2006-05-23 14:42:23 +00:00
|
|
|
pthread_mutex_unlock(&i->mutex);
|
|
|
|
|
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void fd_info_unref(fd_info *i) {
|
|
|
|
|
int r;
|
|
|
|
|
pthread_mutex_lock(&i->mutex);
|
|
|
|
|
assert(i->ref >= 1);
|
|
|
|
|
r = --i->ref;
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_VERBOSE, __FILE__": ref--, now %i\n", i->ref);
|
2006-05-23 14:42:23 +00:00
|
|
|
pthread_mutex_unlock(&i->mutex);
|
|
|
|
|
|
|
|
|
|
if (r <= 0)
|
|
|
|
|
fd_info_free(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void context_state_cb(pa_context *c, void *userdata) {
|
|
|
|
|
fd_info *i = userdata;
|
|
|
|
|
assert(c);
|
|
|
|
|
|
|
|
|
|
switch (pa_context_get_state(c)) {
|
|
|
|
|
case PA_CONTEXT_READY:
|
|
|
|
|
case PA_CONTEXT_TERMINATED:
|
|
|
|
|
case PA_CONTEXT_FAILED:
|
|
|
|
|
pa_threaded_mainloop_signal(i->mainloop, 0);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PA_CONTEXT_UNCONNECTED:
|
|
|
|
|
case PA_CONTEXT_CONNECTING:
|
|
|
|
|
case PA_CONTEXT_AUTHORIZING:
|
|
|
|
|
case PA_CONTEXT_SETTING_NAME:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void reset_params(fd_info *i) {
|
|
|
|
|
assert(i);
|
|
|
|
|
|
2006-06-16 21:11:45 +00:00
|
|
|
i->sample_spec.format = PA_SAMPLE_U8;
|
2006-05-23 14:42:23 +00:00
|
|
|
i->sample_spec.channels = 1;
|
|
|
|
|
i->sample_spec.rate = 8000;
|
2006-05-26 12:18:07 +00:00
|
|
|
i->fragment_size = 0;
|
2006-05-23 14:42:23 +00:00
|
|
|
i->n_fragments = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-25 17:16:55 +00:00
|
|
|
static const char *client_name(char *buf, size_t n) {
|
2006-05-23 16:37:33 +00:00
|
|
|
char p[PATH_MAX];
|
2006-05-25 01:19:56 +00:00
|
|
|
const char *e;
|
|
|
|
|
|
|
|
|
|
if ((e = getenv("PADSP_CLIENT_NAME")))
|
|
|
|
|
return e;
|
2006-05-23 16:37:33 +00:00
|
|
|
|
|
|
|
|
if (pa_get_binary_name(p, sizeof(p)))
|
2006-05-25 01:19:56 +00:00
|
|
|
snprintf(buf, n, "OSS Emulation[%s]", pa_path_get_filename(p));
|
2006-05-23 16:37:33 +00:00
|
|
|
else
|
2006-05-25 01:19:56 +00:00
|
|
|
snprintf(buf, n, "OSS");
|
2006-05-23 16:37:33 +00:00
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-25 17:16:55 +00:00
|
|
|
static const char *stream_name(void) {
|
2006-05-25 01:19:56 +00:00
|
|
|
const char *e;
|
|
|
|
|
|
|
|
|
|
if ((e = getenv("PADSP_STREAM_NAME")))
|
|
|
|
|
return e;
|
|
|
|
|
|
|
|
|
|
return "Audio Stream";
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-23 23:06:28 +00:00
|
|
|
static void atfork_prepare(void) {
|
|
|
|
|
fd_info *i;
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_prepare() enter\n");
|
2006-05-23 23:06:28 +00:00
|
|
|
|
|
|
|
|
function_enter();
|
|
|
|
|
|
|
|
|
|
pthread_mutex_lock(&fd_infos_mutex);
|
|
|
|
|
|
|
|
|
|
for (i = fd_infos; i; i = i->next) {
|
|
|
|
|
pthread_mutex_lock(&i->mutex);
|
|
|
|
|
pa_threaded_mainloop_lock(i->mainloop);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pthread_mutex_lock(&func_mutex);
|
|
|
|
|
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_prepare() exit\n");
|
2006-05-23 23:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void atfork_parent(void) {
|
|
|
|
|
fd_info *i;
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_parent() enter\n");
|
2006-05-23 23:06:28 +00:00
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&func_mutex);
|
|
|
|
|
|
|
|
|
|
for (i = fd_infos; i; i = i->next) {
|
|
|
|
|
pa_threaded_mainloop_unlock(i->mainloop);
|
|
|
|
|
pthread_mutex_unlock(&i->mutex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&fd_infos_mutex);
|
|
|
|
|
|
|
|
|
|
function_exit();
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_parent() exit\n");
|
2006-05-23 23:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void atfork_child(void) {
|
|
|
|
|
fd_info *i;
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_child() enter\n");
|
2006-05-23 23:06:28 +00:00
|
|
|
|
|
|
|
|
/* We do only the bare minimum to get all fds closed */
|
|
|
|
|
pthread_mutex_init(&func_mutex, NULL);
|
|
|
|
|
pthread_mutex_init(&fd_infos_mutex, NULL);
|
|
|
|
|
|
|
|
|
|
for (i = fd_infos; i; i = i->next) {
|
|
|
|
|
pthread_mutex_init(&i->mutex, NULL);
|
|
|
|
|
|
|
|
|
|
if (i->context) {
|
|
|
|
|
pa_context_disconnect(i->context);
|
|
|
|
|
pa_context_unref(i->context);
|
|
|
|
|
i->context = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
if (i->play_stream) {
|
|
|
|
|
pa_stream_unref(i->play_stream);
|
|
|
|
|
i->play_stream = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (i->rec_stream) {
|
|
|
|
|
pa_stream_unref(i->rec_stream);
|
|
|
|
|
i->rec_stream = NULL;
|
2006-05-23 23:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (i->app_fd >= 0) {
|
|
|
|
|
close(i->app_fd);
|
|
|
|
|
i->app_fd = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (i->thread_fd >= 0) {
|
|
|
|
|
close(i->thread_fd);
|
|
|
|
|
i->thread_fd = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i->unusable = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function_exit();
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": atfork_child() exit\n");
|
2006-05-23 23:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void install_atfork(void) {
|
|
|
|
|
pthread_atfork(atfork_prepare, atfork_parent, atfork_child);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-24 02:13:29 +00:00
|
|
|
static void stream_success_cb(pa_stream *s, int success, void *userdata) {
|
|
|
|
|
fd_info *i = userdata;
|
|
|
|
|
|
|
|
|
|
assert(s);
|
|
|
|
|
assert(i);
|
|
|
|
|
|
|
|
|
|
i->operation_success = success;
|
|
|
|
|
pa_threaded_mainloop_signal(i->mainloop, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void context_success_cb(pa_context *c, int success, void *userdata) {
|
|
|
|
|
fd_info *i = userdata;
|
|
|
|
|
|
|
|
|
|
assert(c);
|
|
|
|
|
assert(i);
|
|
|
|
|
|
|
|
|
|
i->operation_success = success;
|
|
|
|
|
pa_threaded_mainloop_signal(i->mainloop, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-23 14:42:23 +00:00
|
|
|
static fd_info* fd_info_new(fd_info_type_t type, int *_errno) {
|
|
|
|
|
fd_info *i;
|
|
|
|
|
int sfds[2] = { -1, -1 };
|
2006-05-23 16:37:33 +00:00
|
|
|
char name[64];
|
2006-05-23 23:06:28 +00:00
|
|
|
static pthread_once_t install_atfork_once = PTHREAD_ONCE_INIT;
|
2006-05-23 14:42:23 +00:00
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": fd_info_new()\n");
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
signal(SIGPIPE, SIG_IGN); /* Yes, ugly as hell */
|
2006-05-23 23:06:28 +00:00
|
|
|
|
|
|
|
|
pthread_once(&install_atfork_once, install_atfork);
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
if (!(i = malloc(sizeof(fd_info)))) {
|
|
|
|
|
*_errno = ENOMEM;
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i->app_fd = i->thread_fd = -1;
|
|
|
|
|
i->type = type;
|
|
|
|
|
|
|
|
|
|
i->mainloop = NULL;
|
|
|
|
|
i->context = NULL;
|
2006-06-16 21:07:32 +00:00
|
|
|
i->play_stream = NULL;
|
|
|
|
|
i->rec_stream = NULL;
|
2006-05-23 14:42:23 +00:00
|
|
|
i->io_event = NULL;
|
|
|
|
|
pthread_mutex_init(&i->mutex, NULL);
|
|
|
|
|
i->ref = 1;
|
|
|
|
|
i->buf = NULL;
|
2006-06-16 21:07:32 +00:00
|
|
|
i->rec_offset = 0;
|
2006-05-23 23:06:28 +00:00
|
|
|
i->unusable = 0;
|
2006-06-16 21:07:32 +00:00
|
|
|
pa_cvolume_reset(&i->sink_volume, 2);
|
|
|
|
|
pa_cvolume_reset(&i->source_volume, 2);
|
2006-05-24 02:13:29 +00:00
|
|
|
i->volume_modify_count = 0;
|
|
|
|
|
i->sink_index = (uint32_t) -1;
|
2006-06-16 21:07:32 +00:00
|
|
|
i->source_index = (uint32_t) -1;
|
2006-05-23 14:42:23 +00:00
|
|
|
PA_LLIST_INIT(fd_info, i);
|
|
|
|
|
|
|
|
|
|
reset_params(i);
|
|
|
|
|
|
|
|
|
|
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sfds) < 0) {
|
|
|
|
|
*_errno = errno;
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": socket() failed: %s\n", strerror(errno));
|
2006-05-23 14:42:23 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i->app_fd = sfds[0];
|
|
|
|
|
i->thread_fd = sfds[1];
|
|
|
|
|
|
|
|
|
|
if (!(i->mainloop = pa_threaded_mainloop_new())) {
|
|
|
|
|
*_errno = EIO;
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_threaded_mainloop_new() failed\n");
|
2006-05-23 14:42:23 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-23 16:37:33 +00:00
|
|
|
if (!(i->context = pa_context_new(pa_threaded_mainloop_get_api(i->mainloop), client_name(name, sizeof(name))))) {
|
2006-05-23 14:42:23 +00:00
|
|
|
*_errno = EIO;
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_context_new() failed\n");
|
2006-05-23 14:42:23 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_context_set_state_callback(i->context, context_state_cb, i);
|
|
|
|
|
|
|
|
|
|
if (pa_context_connect(i->context, NULL, 0, NULL) < 0) {
|
|
|
|
|
*_errno = ECONNREFUSED;
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
|
2006-05-23 14:42:23 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_threaded_mainloop_lock(i->mainloop);
|
|
|
|
|
|
|
|
|
|
if (pa_threaded_mainloop_start(i->mainloop) < 0) {
|
|
|
|
|
*_errno = EIO;
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_threaded_mainloop_start() failed\n");
|
2006-05-23 14:42:23 +00:00
|
|
|
goto unlock_and_fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Wait until the context is ready */
|
|
|
|
|
pa_threaded_mainloop_wait(i->mainloop);
|
|
|
|
|
|
|
|
|
|
if (pa_context_get_state(i->context) != PA_CONTEXT_READY) {
|
|
|
|
|
*_errno = ECONNREFUSED;
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_context_connect() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
|
2006-05-23 14:42:23 +00:00
|
|
|
goto unlock_and_fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_threaded_mainloop_unlock(i->mainloop);
|
|
|
|
|
return i;
|
|
|
|
|
|
|
|
|
|
unlock_and_fail:
|
|
|
|
|
|
|
|
|
|
pa_threaded_mainloop_unlock(i->mainloop);
|
|
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
|
|
|
|
|
if (i)
|
|
|
|
|
fd_info_unref(i);
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void fd_info_add_to_list(fd_info *i) {
|
|
|
|
|
assert(i);
|
|
|
|
|
|
|
|
|
|
pthread_mutex_lock(&fd_infos_mutex);
|
|
|
|
|
PA_LLIST_PREPEND(fd_info, fd_infos, i);
|
|
|
|
|
pthread_mutex_unlock(&fd_infos_mutex);
|
|
|
|
|
|
|
|
|
|
fd_info_ref(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void fd_info_remove_from_list(fd_info *i) {
|
|
|
|
|
assert(i);
|
|
|
|
|
|
|
|
|
|
pthread_mutex_lock(&fd_infos_mutex);
|
|
|
|
|
PA_LLIST_REMOVE(fd_info, fd_infos, i);
|
|
|
|
|
pthread_mutex_unlock(&fd_infos_mutex);
|
|
|
|
|
|
|
|
|
|
fd_info_unref(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static fd_info* fd_info_find(int fd) {
|
|
|
|
|
fd_info *i;
|
|
|
|
|
|
|
|
|
|
pthread_mutex_lock(&fd_infos_mutex);
|
|
|
|
|
|
|
|
|
|
for (i = fd_infos; i; i = i->next)
|
2006-05-23 23:06:28 +00:00
|
|
|
if (i->app_fd == fd && !i->unusable) {
|
2006-05-23 14:42:23 +00:00
|
|
|
fd_info_ref(i);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&fd_infos_mutex);
|
|
|
|
|
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void fix_metrics(fd_info *i) {
|
|
|
|
|
size_t fs;
|
|
|
|
|
char t[PA_SAMPLE_SPEC_SNPRINT_MAX];
|
|
|
|
|
|
|
|
|
|
fs = pa_frame_size(&i->sample_spec);
|
2006-06-20 15:36:28 +00:00
|
|
|
|
|
|
|
|
/* Don't fix things more than necessary */
|
|
|
|
|
if ((i->fragment_size % fs) == 0 &&
|
|
|
|
|
i->n_fragments >= 2 &&
|
|
|
|
|
i->fragment_size > 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2006-05-23 14:42:23 +00:00
|
|
|
i->fragment_size = (i->fragment_size/fs)*fs;
|
|
|
|
|
|
2006-05-26 12:18:07 +00:00
|
|
|
/* Number of fragments set? */
|
|
|
|
|
if (i->n_fragments < 2) {
|
|
|
|
|
if (i->fragment_size > 0) {
|
|
|
|
|
i->n_fragments = pa_bytes_per_second(&i->sample_spec) / 2 / i->fragment_size;
|
|
|
|
|
if (i->n_fragments < 2)
|
|
|
|
|
i->n_fragments = 2;
|
|
|
|
|
} else
|
|
|
|
|
i->n_fragments = 12;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Fragment size set? */
|
|
|
|
|
if (i->fragment_size <= 0) {
|
|
|
|
|
i->fragment_size = pa_bytes_per_second(&i->sample_spec) / 2 / i->n_fragments;
|
|
|
|
|
if (i->fragment_size < 1024)
|
2006-05-23 14:42:23 +00:00
|
|
|
i->fragment_size = 1024;
|
2006-05-26 12:18:07 +00:00
|
|
|
}
|
2006-05-23 14:42:23 +00:00
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": sample spec: %s\n", pa_sample_spec_snprint(t, sizeof(t), &i->sample_spec));
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": fixated metrics to %i fragments, %li bytes each.\n", i->n_fragments, (long)i->fragment_size);
|
2006-05-23 14:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
|
|
|
|
|
fd_info *i = userdata;
|
|
|
|
|
assert(s);
|
|
|
|
|
|
|
|
|
|
if (i->io_event) {
|
|
|
|
|
pa_mainloop_api *api;
|
2006-06-16 21:07:32 +00:00
|
|
|
pa_io_event_flags_t flags;
|
|
|
|
|
size_t n;
|
|
|
|
|
|
2006-05-23 14:42:23 +00:00
|
|
|
api = pa_threaded_mainloop_get_api(i->mainloop);
|
2006-06-16 21:07:32 +00:00
|
|
|
|
|
|
|
|
flags = 0;
|
|
|
|
|
|
|
|
|
|
if (s == i->play_stream) {
|
|
|
|
|
n = pa_stream_writable_size(i->play_stream);
|
|
|
|
|
if (n == (size_t)-1) {
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_writable_size(): %s\n",
|
|
|
|
|
pa_strerror(pa_context_errno(i->context)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (n >= i->fragment_size)
|
|
|
|
|
flags |= PA_IO_EVENT_INPUT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (s == i->rec_stream) {
|
|
|
|
|
n = pa_stream_readable_size(i->rec_stream);
|
|
|
|
|
if (n == (size_t)-1) {
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_readable_size(): %s\n",
|
|
|
|
|
pa_strerror(pa_context_errno(i->context)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (n >= i->fragment_size)
|
|
|
|
|
flags |= PA_IO_EVENT_OUTPUT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
api->io_enable(i->io_event, flags);
|
2006-05-23 14:42:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void stream_latency_update_cb(pa_stream *s, void *userdata) {
|
|
|
|
|
fd_info *i = userdata;
|
|
|
|
|
assert(s);
|
|
|
|
|
|
|
|
|
|
pa_threaded_mainloop_signal(i->mainloop, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void fd_info_shutdown(fd_info *i) {
|
|
|
|
|
assert(i);
|
|
|
|
|
|
|
|
|
|
if (i->io_event) {
|
|
|
|
|
pa_mainloop_api *api;
|
|
|
|
|
api = pa_threaded_mainloop_get_api(i->mainloop);
|
|
|
|
|
api->io_free(i->io_event);
|
|
|
|
|
i->io_event = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (i->thread_fd >= 0) {
|
|
|
|
|
close(i->thread_fd);
|
|
|
|
|
i->thread_fd = -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int fd_info_copy_data(fd_info *i, int force) {
|
|
|
|
|
size_t n;
|
2006-06-16 21:07:32 +00:00
|
|
|
pa_io_event_flags_t flags;
|
2006-05-23 14:42:23 +00:00
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
if (!i->play_stream && !i->rec_stream)
|
2006-05-23 14:42:23 +00:00
|
|
|
return -1;
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
flags = 0;
|
|
|
|
|
|
2006-06-16 21:08:35 +00:00
|
|
|
if ((i->play_stream) && (pa_stream_get_state(i->play_stream) == PA_STREAM_READY)) {
|
2006-06-16 21:07:32 +00:00
|
|
|
n = pa_stream_writable_size(i->play_stream);
|
|
|
|
|
|
|
|
|
|
if (n == (size_t)-1) {
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_writable_size(): %s\n",
|
|
|
|
|
pa_strerror(pa_context_errno(i->context)));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (n >= i->fragment_size || force) {
|
|
|
|
|
ssize_t r;
|
|
|
|
|
|
|
|
|
|
if (!i->buf) {
|
|
|
|
|
if (!(i->buf = malloc(i->fragment_size))) {
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": malloc() failed.\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((r = read(i->thread_fd, i->buf, i->fragment_size)) <= 0) {
|
|
|
|
|
|
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": read(): %s\n", r == 0 ? "EOF" : strerror(errno));
|
2006-05-23 14:42:23 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
if (pa_stream_write(i->play_stream, i->buf, r, free, 0, PA_SEEK_RELATIVE) < 0) {
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_write(): %s\n", pa_strerror(pa_context_errno(i->context)));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i->buf = NULL;
|
|
|
|
|
|
|
|
|
|
assert(n >= (size_t) r);
|
|
|
|
|
n -= r;
|
2006-05-23 14:42:23 +00:00
|
|
|
}
|
2006-06-16 21:07:32 +00:00
|
|
|
|
|
|
|
|
if (n >= i->fragment_size)
|
|
|
|
|
flags |= PA_IO_EVENT_INPUT;
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-16 21:08:35 +00:00
|
|
|
if ((i->rec_stream) && (pa_stream_get_state(i->rec_stream) == PA_STREAM_READY)) {
|
2006-06-16 21:07:32 +00:00
|
|
|
n = pa_stream_readable_size(i->rec_stream);
|
|
|
|
|
|
|
|
|
|
if (n == (size_t)-1) {
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_readable_size(): %s\n",
|
|
|
|
|
pa_strerror(pa_context_errno(i->context)));
|
2006-05-23 14:42:23 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
while (n >= i->fragment_size || force) {
|
|
|
|
|
ssize_t r;
|
|
|
|
|
const void *data;
|
|
|
|
|
const char *buf;
|
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
|
|
if (pa_stream_peek(i->rec_stream, &data, &len) < 0) {
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_peek(): %s\n", pa_strerror(pa_context_errno(i->context)));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2006-05-23 14:42:23 +00:00
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
if (!data)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
buf = (const char*)data + i->rec_offset;
|
|
|
|
|
|
|
|
|
|
if ((r = write(i->thread_fd, buf, len - i->rec_offset)) <= 0) {
|
|
|
|
|
|
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": write(): %s\n", strerror(errno));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert((size_t)r <= len - i->rec_offset);
|
|
|
|
|
i->rec_offset += r;
|
|
|
|
|
|
|
|
|
|
if (i->rec_offset == len) {
|
|
|
|
|
if (pa_stream_drop(i->rec_stream) < 0) {
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_drop(): %s\n", pa_strerror(pa_context_errno(i->context)));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
i->rec_offset = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert(n >= (size_t) r);
|
|
|
|
|
n -= r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (n >= i->fragment_size)
|
|
|
|
|
flags |= PA_IO_EVENT_OUTPUT;
|
2006-05-23 14:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (i->io_event) {
|
|
|
|
|
pa_mainloop_api *api;
|
2006-06-16 21:07:32 +00:00
|
|
|
|
2006-05-23 14:42:23 +00:00
|
|
|
api = pa_threaded_mainloop_get_api(i->mainloop);
|
2006-06-16 21:07:32 +00:00
|
|
|
api->io_enable(i->io_event, flags);
|
2006-05-23 14:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void stream_state_cb(pa_stream *s, void * userdata) {
|
|
|
|
|
fd_info *i = userdata;
|
|
|
|
|
assert(s);
|
|
|
|
|
|
|
|
|
|
switch (pa_stream_get_state(s)) {
|
|
|
|
|
|
|
|
|
|
case PA_STREAM_READY:
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": stream established.\n");
|
2006-05-23 14:42:23 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PA_STREAM_FAILED:
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
|
2006-05-23 14:42:23 +00:00
|
|
|
fd_info_shutdown(i);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PA_STREAM_TERMINATED:
|
|
|
|
|
case PA_STREAM_UNCONNECTED:
|
|
|
|
|
case PA_STREAM_CREATING:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
static int create_playback_stream(fd_info *i) {
|
2006-05-23 14:42:23 +00:00
|
|
|
pa_buffer_attr attr;
|
|
|
|
|
int n;
|
|
|
|
|
|
|
|
|
|
assert(i);
|
|
|
|
|
|
|
|
|
|
fix_metrics(i);
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
if (!(i->play_stream = pa_stream_new(i->context, stream_name(), &i->sample_spec, NULL))) {
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
|
2006-05-23 14:42:23 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
pa_stream_set_state_callback(i->play_stream, stream_state_cb, i);
|
|
|
|
|
pa_stream_set_write_callback(i->play_stream, stream_request_cb, i);
|
|
|
|
|
pa_stream_set_latency_update_callback(i->play_stream, stream_latency_update_cb, i);
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
memset(&attr, 0, sizeof(attr));
|
|
|
|
|
attr.maxlength = i->fragment_size * (i->n_fragments+1);
|
|
|
|
|
attr.tlength = i->fragment_size * i->n_fragments;
|
|
|
|
|
attr.prebuf = i->fragment_size;
|
|
|
|
|
attr.minreq = i->fragment_size;
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
if (pa_stream_connect_playback(i->play_stream, NULL, &attr, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL) < 0) {
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
|
2006-05-23 14:42:23 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
n = i->fragment_size;
|
|
|
|
|
setsockopt(i->app_fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n));
|
|
|
|
|
n = i->fragment_size;
|
|
|
|
|
setsockopt(i->thread_fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n));
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
static int create_record_stream(fd_info *i) {
|
|
|
|
|
pa_buffer_attr attr;
|
|
|
|
|
int n;
|
|
|
|
|
|
|
|
|
|
assert(i);
|
|
|
|
|
|
|
|
|
|
fix_metrics(i);
|
|
|
|
|
|
|
|
|
|
if (!(i->rec_stream = pa_stream_new(i->context, stream_name(), &i->sample_spec, NULL))) {
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_new() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_stream_set_state_callback(i->rec_stream, stream_state_cb, i);
|
|
|
|
|
pa_stream_set_read_callback(i->rec_stream, stream_request_cb, i);
|
|
|
|
|
pa_stream_set_latency_update_callback(i->rec_stream, stream_latency_update_cb, i);
|
|
|
|
|
|
|
|
|
|
memset(&attr, 0, sizeof(attr));
|
|
|
|
|
attr.maxlength = i->fragment_size * (i->n_fragments+1);
|
|
|
|
|
attr.fragsize = i->fragment_size;
|
|
|
|
|
|
|
|
|
|
if (pa_stream_connect_record(i->rec_stream, NULL, &attr, PA_STREAM_INTERPOLATE_TIMING|PA_STREAM_AUTO_TIMING_UPDATE) < 0) {
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_connect_playback() failed: %s\n", pa_strerror(pa_context_errno(i->context)));
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
n = i->fragment_size;
|
|
|
|
|
setsockopt(i->app_fd, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n));
|
|
|
|
|
n = i->fragment_size;
|
|
|
|
|
setsockopt(i->thread_fd, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n));
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void free_streams(fd_info *i) {
|
2006-05-23 14:42:23 +00:00
|
|
|
assert(i);
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
if (i->play_stream) {
|
|
|
|
|
pa_stream_disconnect(i->play_stream);
|
|
|
|
|
pa_stream_unref(i->play_stream);
|
|
|
|
|
i->play_stream = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (i->rec_stream) {
|
|
|
|
|
pa_stream_disconnect(i->rec_stream);
|
|
|
|
|
pa_stream_unref(i->rec_stream);
|
|
|
|
|
i->rec_stream = NULL;
|
2006-05-23 14:42:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void io_event_cb(pa_mainloop_api *api, pa_io_event *e, int fd, pa_io_event_flags_t flags, void *userdata) {
|
|
|
|
|
fd_info *i = userdata;
|
|
|
|
|
|
|
|
|
|
pa_threaded_mainloop_signal(i->mainloop, 0);
|
|
|
|
|
|
|
|
|
|
if (flags & PA_IO_EVENT_INPUT) {
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
if (!i->play_stream) {
|
|
|
|
|
if (create_playback_stream(i) < 0)
|
2006-05-23 14:42:23 +00:00
|
|
|
goto fail;
|
|
|
|
|
} else {
|
|
|
|
|
if (fd_info_copy_data(i, 0) < 0)
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
} else if (flags & PA_IO_EVENT_OUTPUT) {
|
|
|
|
|
|
|
|
|
|
if (!i->rec_stream) {
|
|
|
|
|
if (create_record_stream(i) < 0)
|
|
|
|
|
goto fail;
|
|
|
|
|
} else {
|
|
|
|
|
if (fd_info_copy_data(i, 0) < 0)
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-23 14:42:23 +00:00
|
|
|
} else if (flags & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR))
|
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
/* We can't do anything better than removing the event source */
|
|
|
|
|
fd_info_shutdown(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int dsp_open(int flags, int *_errno) {
|
|
|
|
|
fd_info *i;
|
|
|
|
|
pa_mainloop_api *api;
|
|
|
|
|
int ret;
|
|
|
|
|
int f;
|
2006-06-16 21:07:32 +00:00
|
|
|
pa_io_event_flags_t ioflags;
|
2006-05-23 14:42:23 +00:00
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": dsp_open()\n");
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
if (!(i = fd_info_new(FD_INFO_STREAM, _errno)))
|
2006-05-23 14:42:23 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
if ((flags & O_NONBLOCK) == O_NONBLOCK) {
|
|
|
|
|
if ((f = fcntl(i->app_fd, F_GETFL)) >= 0)
|
|
|
|
|
fcntl(i->app_fd, F_SETFL, f|O_NONBLOCK);
|
|
|
|
|
}
|
|
|
|
|
if ((f = fcntl(i->thread_fd, F_GETFL)) >= 0)
|
|
|
|
|
fcntl(i->thread_fd, F_SETFL, f|O_NONBLOCK);
|
|
|
|
|
|
|
|
|
|
fcntl(i->app_fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
|
fcntl(i->thread_fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
|
|
|
|
|
|
pa_threaded_mainloop_lock(i->mainloop);
|
|
|
|
|
api = pa_threaded_mainloop_get_api(i->mainloop);
|
2006-06-16 21:07:32 +00:00
|
|
|
|
|
|
|
|
switch (flags & O_ACCMODE) {
|
|
|
|
|
case O_RDONLY:
|
|
|
|
|
ioflags = PA_IO_EVENT_OUTPUT;
|
|
|
|
|
shutdown(i->thread_fd, SHUT_RD);
|
|
|
|
|
shutdown(i->app_fd, SHUT_WR);
|
|
|
|
|
break;
|
|
|
|
|
case O_WRONLY:
|
|
|
|
|
ioflags = PA_IO_EVENT_INPUT;
|
|
|
|
|
shutdown(i->thread_fd, SHUT_WR);
|
|
|
|
|
shutdown(i->app_fd, SHUT_RD);
|
|
|
|
|
break;
|
|
|
|
|
case O_RDWR:
|
|
|
|
|
ioflags = PA_IO_EVENT_INPUT | PA_IO_EVENT_OUTPUT;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!(i->io_event = api->io_new(api, i->thread_fd, ioflags, io_event_cb, i)))
|
2006-05-23 14:42:23 +00:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
pa_threaded_mainloop_unlock(i->mainloop);
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": dsp_open() succeeded, fd=%i\n", i->app_fd);
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
fd_info_add_to_list(i);
|
|
|
|
|
ret = i->app_fd;
|
|
|
|
|
fd_info_unref(i);
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
pa_threaded_mainloop_unlock(i->mainloop);
|
|
|
|
|
|
|
|
|
|
if (i)
|
|
|
|
|
fd_info_unref(i);
|
|
|
|
|
|
|
|
|
|
*_errno = EIO;
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": dsp_open() failed\n");
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-24 02:13:29 +00:00
|
|
|
static void sink_info_cb(pa_context *context, const pa_sink_info *si, int eol, void *userdata) {
|
|
|
|
|
fd_info *i = userdata;
|
|
|
|
|
|
|
|
|
|
if (!si && eol < 0) {
|
|
|
|
|
i->operation_success = 0;
|
|
|
|
|
pa_threaded_mainloop_signal(i->mainloop, 0);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (eol)
|
|
|
|
|
return;
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
if (!pa_cvolume_equal(&i->sink_volume, &si->volume))
|
2006-05-24 02:13:29 +00:00
|
|
|
i->volume_modify_count++;
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
i->sink_volume = si->volume;
|
2006-05-24 02:13:29 +00:00
|
|
|
i->sink_index = si->index;
|
|
|
|
|
|
|
|
|
|
i->operation_success = 1;
|
|
|
|
|
pa_threaded_mainloop_signal(i->mainloop, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
static void source_info_cb(pa_context *context, const pa_source_info *si, int eol, void *userdata) {
|
|
|
|
|
fd_info *i = userdata;
|
|
|
|
|
|
|
|
|
|
if (!si && eol < 0) {
|
|
|
|
|
i->operation_success = 0;
|
|
|
|
|
pa_threaded_mainloop_signal(i->mainloop, 0);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (eol)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!pa_cvolume_equal(&i->source_volume, &si->volume))
|
|
|
|
|
i->volume_modify_count++;
|
|
|
|
|
|
|
|
|
|
i->source_volume = si->volume;
|
|
|
|
|
i->source_index = si->index;
|
|
|
|
|
|
|
|
|
|
i->operation_success = 1;
|
|
|
|
|
pa_threaded_mainloop_signal(i->mainloop, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-24 02:13:29 +00:00
|
|
|
static void subscribe_cb(pa_context *context, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
|
|
|
|
|
fd_info *i = userdata;
|
|
|
|
|
pa_operation *o = NULL;
|
|
|
|
|
|
|
|
|
|
if (i->sink_index != idx)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) != PA_SUBSCRIPTION_EVENT_CHANGE)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!(o = pa_context_get_sink_info_by_index(i->context, i->sink_index, sink_info_cb, i))) {
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get sink info: %s", pa_strerror(pa_context_errno(i->context)));
|
2006-05-24 02:13:29 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_operation_unref(o);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-23 14:42:23 +00:00
|
|
|
static int mixer_open(int flags, int *_errno) {
|
2006-05-24 02:13:29 +00:00
|
|
|
fd_info *i;
|
2006-06-16 21:07:32 +00:00
|
|
|
pa_operation *o = NULL;
|
2006-05-24 02:13:29 +00:00
|
|
|
int ret;
|
2006-05-23 14:42:23 +00:00
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": mixer_open()\n");
|
|
|
|
|
|
2006-05-24 02:13:29 +00:00
|
|
|
if (!(i = fd_info_new(FD_INFO_MIXER, _errno)))
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
pa_threaded_mainloop_lock(i->mainloop);
|
|
|
|
|
|
|
|
|
|
pa_context_set_subscribe_callback(i->context, subscribe_cb, i);
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
if (!(o = pa_context_subscribe(i->context, PA_SUBSCRIPTION_MASK_SINK | PA_SUBSCRIPTION_MASK_SOURCE, context_success_cb, i))) {
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i->context)));
|
2006-05-24 02:13:29 +00:00
|
|
|
*_errno = EIO;
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i->operation_success = 0;
|
|
|
|
|
while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
|
|
|
|
|
pa_threaded_mainloop_wait(i->mainloop);
|
|
|
|
|
CONTEXT_CHECK_DEAD_GOTO(i, fail);
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
pa_operation_unref(o);
|
|
|
|
|
o = NULL;
|
|
|
|
|
|
2006-05-24 02:13:29 +00:00
|
|
|
if (!i->operation_success) {
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__":Failed to subscribe to events: %s", pa_strerror(pa_context_errno(i->context)));
|
2006-05-24 02:13:29 +00:00
|
|
|
*_errno = EIO;
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Get sink info */
|
|
|
|
|
|
|
|
|
|
if (!(o = pa_context_get_sink_info_by_name(i->context, NULL, sink_info_cb, i))) {
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get sink info: %s", pa_strerror(pa_context_errno(i->context)));
|
2006-05-24 02:13:29 +00:00
|
|
|
*_errno = EIO;
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i->operation_success = 0;
|
|
|
|
|
while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
|
|
|
|
|
pa_threaded_mainloop_wait(i->mainloop);
|
|
|
|
|
CONTEXT_CHECK_DEAD_GOTO(i, fail);
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
pa_operation_unref(o);
|
|
|
|
|
o = NULL;
|
|
|
|
|
|
2006-05-24 02:13:29 +00:00
|
|
|
if (!i->operation_success) {
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get sink info: %s", pa_strerror(pa_context_errno(i->context)));
|
2006-05-24 02:13:29 +00:00
|
|
|
*_errno = EIO;
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
/* Get source info */
|
|
|
|
|
|
|
|
|
|
if (!(o = pa_context_get_source_info_by_name(i->context, NULL, source_info_cb, i))) {
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get source info: %s", pa_strerror(pa_context_errno(i->context)));
|
|
|
|
|
*_errno = EIO;
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i->operation_success = 0;
|
|
|
|
|
while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
|
|
|
|
|
pa_threaded_mainloop_wait(i->mainloop);
|
|
|
|
|
CONTEXT_CHECK_DEAD_GOTO(i, fail);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_operation_unref(o);
|
|
|
|
|
o = NULL;
|
|
|
|
|
|
|
|
|
|
if (!i->operation_success) {
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to get source info: %s", pa_strerror(pa_context_errno(i->context)));
|
|
|
|
|
*_errno = EIO;
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-24 02:13:29 +00:00
|
|
|
pa_threaded_mainloop_unlock(i->mainloop);
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": mixer_open() succeeded, fd=%i\n", i->app_fd);
|
2006-05-24 02:13:29 +00:00
|
|
|
|
|
|
|
|
fd_info_add_to_list(i);
|
|
|
|
|
ret = i->app_fd;
|
|
|
|
|
fd_info_unref(i);
|
|
|
|
|
|
|
|
|
|
return ret;
|
2006-05-23 14:42:23 +00:00
|
|
|
|
2006-05-24 02:13:29 +00:00
|
|
|
fail:
|
2006-06-16 21:07:32 +00:00
|
|
|
if (o)
|
|
|
|
|
pa_operation_unref(o);
|
|
|
|
|
|
2006-05-24 02:13:29 +00:00
|
|
|
pa_threaded_mainloop_unlock(i->mainloop);
|
2006-05-23 14:42:23 +00:00
|
|
|
|
2006-05-24 02:13:29 +00:00
|
|
|
if (i)
|
|
|
|
|
fd_info_unref(i);
|
|
|
|
|
|
|
|
|
|
*_errno = EIO;
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": mixer_open() failed\n");
|
2006-05-24 02:13:29 +00:00
|
|
|
|
|
|
|
|
return -1;
|
2006-05-23 14:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
2006-05-23 23:57:50 +00:00
|
|
|
static int sndstat_open(int flags, int *_errno) {
|
|
|
|
|
static const char sndstat[] =
|
2006-06-19 22:11:49 +00:00
|
|
|
"Sound Driver:3.8.1a-980706 (PulseAudio Virtual OSS)\n"
|
2006-05-23 23:57:50 +00:00
|
|
|
"Kernel: POSIX\n"
|
|
|
|
|
"Config options: 0\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"Installed drivers:\n"
|
2006-06-19 22:11:49 +00:00
|
|
|
"Type 255: PulseAudio Virtual OSS\n"
|
2006-05-23 23:57:50 +00:00
|
|
|
"\n"
|
|
|
|
|
"Card config:\n"
|
2006-06-19 22:11:49 +00:00
|
|
|
"PulseAudio Virtual OSS\n"
|
2006-05-23 23:57:50 +00:00
|
|
|
"\n"
|
|
|
|
|
"Audio devices:\n"
|
2006-06-19 22:11:49 +00:00
|
|
|
"0: PulseAudio Virtual OSS\n"
|
2006-05-23 23:57:50 +00:00
|
|
|
"\n"
|
|
|
|
|
"Synth devices: NOT ENABLED IN CONFIG\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"Midi devices:\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"Timers:\n"
|
|
|
|
|
"\n"
|
2006-05-24 08:14:19 +00:00
|
|
|
"Mixers:\n"
|
2006-06-19 22:11:49 +00:00
|
|
|
"0: PulseAudio Virtual OSS\n";
|
2006-05-23 23:57:50 +00:00
|
|
|
|
|
|
|
|
char fn[] = "/tmp/padsp-sndstat-XXXXXX";
|
|
|
|
|
mode_t u;
|
|
|
|
|
int fd = -1;
|
|
|
|
|
int e;
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": sndstat_open()\n");
|
2006-05-23 23:57:50 +00:00
|
|
|
|
|
|
|
|
if (flags != O_RDONLY && flags != (O_RDONLY|O_LARGEFILE)) {
|
|
|
|
|
*_errno = EACCES;
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": bad access!\n");
|
2006-05-23 23:57:50 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u = umask(0077);
|
|
|
|
|
fd = mkstemp(fn);
|
|
|
|
|
e = errno;
|
|
|
|
|
umask(u);
|
|
|
|
|
|
|
|
|
|
if (fd < 0) {
|
|
|
|
|
*_errno = e;
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": mkstemp() failed: %s\n", strerror(errno));
|
2006-05-23 23:57:50 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unlink(fn);
|
|
|
|
|
|
|
|
|
|
if (write(fd, sndstat, sizeof(sndstat) -1) != sizeof(sndstat)-1) {
|
|
|
|
|
*_errno = errno;
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": write() failed: %s\n", strerror(errno));
|
2006-05-23 23:57:50 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lseek(fd, SEEK_SET, 0) < 0) {
|
|
|
|
|
*_errno = errno;
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": lseek() failed: %s\n", strerror(errno));
|
2006-05-23 23:57:50 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
if (fd >= 0)
|
|
|
|
|
close(fd);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-23 14:42:23 +00:00
|
|
|
int open(const char *filename, int flags, ...) {
|
|
|
|
|
va_list args;
|
|
|
|
|
mode_t mode = 0;
|
|
|
|
|
int r, _errno = 0;
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_VERBOSE, __FILE__": open(%s)\n", filename);
|
2006-05-23 23:57:50 +00:00
|
|
|
|
2006-05-23 14:42:23 +00:00
|
|
|
va_start(args, flags);
|
|
|
|
|
if (flags & O_CREAT)
|
|
|
|
|
mode = va_arg(args, mode_t);
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
|
|
if (!function_enter()) {
|
|
|
|
|
LOAD_OPEN_FUNC();
|
|
|
|
|
return _open(filename, flags, mode);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-25 01:14:06 +00:00
|
|
|
if (dsp_cloak_enable() && (strcmp(filename, "/dev/dsp") == 0 || strcmp(filename, "/dev/adsp") == 0)) {
|
2006-05-23 14:42:23 +00:00
|
|
|
r = dsp_open(flags, &_errno);
|
2006-05-25 01:14:06 +00:00
|
|
|
} else if (mixer_cloak_enable() && strcmp(filename, "/dev/mixer") == 0) {
|
2006-05-23 14:42:23 +00:00
|
|
|
r = mixer_open(flags, &_errno);
|
2006-05-25 01:14:06 +00:00
|
|
|
} else if (sndstat_cloak_enable() && strcmp(filename, "/dev/sndstat") == 0) {
|
2006-05-23 23:57:50 +00:00
|
|
|
r = sndstat_open(flags, &_errno);
|
2006-05-23 14:42:23 +00:00
|
|
|
} else {
|
|
|
|
|
function_exit();
|
|
|
|
|
LOAD_OPEN_FUNC();
|
|
|
|
|
return _open(filename, flags, mode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function_exit();
|
|
|
|
|
|
|
|
|
|
if (_errno)
|
|
|
|
|
errno = _errno;
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int mixer_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno) {
|
2006-05-24 02:13:29 +00:00
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
|
|
switch (request) {
|
|
|
|
|
case SOUND_MIXER_READ_DEVMASK :
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_DEVMASK\n");
|
2006-05-24 02:13:29 +00:00
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
*(int*) argp = SOUND_MASK_PCM | SOUND_MASK_IGAIN;
|
2006-05-24 02:13:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SOUND_MIXER_READ_RECMASK :
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_RECMASK\n");
|
2006-05-24 02:13:29 +00:00
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
*(int*) argp = SOUND_MASK_IGAIN;
|
2006-05-24 02:13:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SOUND_MIXER_READ_STEREODEVS:
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_STEREODEVS\n");
|
2006-05-24 02:13:29 +00:00
|
|
|
|
|
|
|
|
pa_threaded_mainloop_lock(i->mainloop);
|
2006-06-16 21:07:32 +00:00
|
|
|
*(int*) argp = 0;
|
|
|
|
|
if (i->sink_volume.channels > 1)
|
|
|
|
|
*(int*) argp |= SOUND_MASK_PCM;
|
|
|
|
|
if (i->source_volume.channels > 1)
|
|
|
|
|
*(int*) argp |= SOUND_MASK_IGAIN;
|
2006-05-24 02:13:29 +00:00
|
|
|
pa_threaded_mainloop_unlock(i->mainloop);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SOUND_MIXER_READ_RECSRC:
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_RECSRC\n");
|
2006-05-24 02:13:29 +00:00
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
*(int*) argp = SOUND_MASK_IGAIN;
|
2006-05-24 02:13:29 +00:00
|
|
|
break;
|
2006-06-16 21:07:32 +00:00
|
|
|
|
|
|
|
|
case SOUND_MIXER_WRITE_RECSRC:
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_WRITE_RECSRC\n");
|
|
|
|
|
break;
|
|
|
|
|
|
2006-05-26 09:21:03 +00:00
|
|
|
case SOUND_MIXER_READ_CAPS:
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_CAPS\n");
|
2006-05-24 02:13:29 +00:00
|
|
|
|
|
|
|
|
*(int*) argp = 0;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SOUND_MIXER_READ_PCM:
|
2006-06-16 21:07:32 +00:00
|
|
|
case SOUND_MIXER_READ_IGAIN: {
|
|
|
|
|
pa_cvolume *v;
|
|
|
|
|
|
|
|
|
|
if (request == SOUND_MIXER_READ_PCM)
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_PCM\n");
|
|
|
|
|
else
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_READ_IGAIN\n");
|
|
|
|
|
|
2006-05-24 02:13:29 +00:00
|
|
|
pa_threaded_mainloop_lock(i->mainloop);
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
if (request == SOUND_MIXER_READ_PCM)
|
|
|
|
|
v = &i->sink_volume;
|
|
|
|
|
else
|
|
|
|
|
v = &i->source_volume;
|
|
|
|
|
|
2006-05-24 02:13:29 +00:00
|
|
|
*(int*) argp =
|
2006-06-16 21:07:32 +00:00
|
|
|
((v->values[0]*100/PA_VOLUME_NORM)) |
|
|
|
|
|
((v->values[v->channels > 1 ? 1 : 0]*100/PA_VOLUME_NORM) << 8);
|
|
|
|
|
|
2006-05-24 02:13:29 +00:00
|
|
|
pa_threaded_mainloop_unlock(i->mainloop);
|
2006-06-16 21:07:32 +00:00
|
|
|
|
2006-05-24 02:13:29 +00:00
|
|
|
break;
|
2006-06-16 21:07:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case SOUND_MIXER_WRITE_PCM:
|
|
|
|
|
case SOUND_MIXER_WRITE_IGAIN: {
|
|
|
|
|
pa_cvolume v, *pv;
|
|
|
|
|
|
|
|
|
|
if (request == SOUND_MIXER_READ_PCM)
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_WRITE_PCM\n");
|
|
|
|
|
else
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_WRITE_IGAIN\n");
|
2006-05-24 02:13:29 +00:00
|
|
|
|
|
|
|
|
pa_threaded_mainloop_lock(i->mainloop);
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
if (request == SOUND_MIXER_READ_PCM) {
|
|
|
|
|
v = i->sink_volume;
|
|
|
|
|
pv = &i->sink_volume;
|
|
|
|
|
} else {
|
|
|
|
|
v = i->source_volume;
|
|
|
|
|
pv = &i->source_volume;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pv->values[0] = ((*(int*) argp & 0xFF)*PA_VOLUME_NORM)/100;
|
|
|
|
|
pv->values[1] = ((*(int*) argp >> 8)*PA_VOLUME_NORM)/100;
|
2006-05-24 02:13:29 +00:00
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
if (!pa_cvolume_equal(pv, &v)) {
|
2006-05-24 02:13:29 +00:00
|
|
|
pa_operation *o;
|
2006-06-16 21:07:32 +00:00
|
|
|
|
|
|
|
|
if (request == SOUND_MIXER_READ_PCM)
|
|
|
|
|
o = pa_context_set_sink_volume_by_index(i->context, i->sink_index, pv, NULL, NULL);
|
|
|
|
|
else
|
|
|
|
|
o = pa_context_set_source_volume_by_index(i->context, i->source_index, pv, NULL, NULL);
|
|
|
|
|
|
|
|
|
|
if (!o)
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__":Failed set volume: %s", pa_strerror(pa_context_errno(i->context)));
|
2006-05-25 00:25:03 +00:00
|
|
|
else {
|
|
|
|
|
|
|
|
|
|
i->operation_success = 0;
|
|
|
|
|
while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
|
|
|
|
|
CONTEXT_CHECK_DEAD_GOTO(i, exit_loop);
|
|
|
|
|
|
|
|
|
|
pa_threaded_mainloop_wait(i->mainloop);
|
|
|
|
|
}
|
|
|
|
|
exit_loop:
|
|
|
|
|
|
|
|
|
|
if (!i->operation_success)
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": Failed to set volume: %s\n", pa_strerror(pa_context_errno(i->context)));
|
2006-05-25 00:25:03 +00:00
|
|
|
|
2006-05-24 02:13:29 +00:00
|
|
|
pa_operation_unref(o);
|
2006-05-25 00:25:03 +00:00
|
|
|
}
|
2006-05-24 02:13:29 +00:00
|
|
|
|
|
|
|
|
/* We don't wait for completion here */
|
|
|
|
|
i->volume_modify_count++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_threaded_mainloop_unlock(i->mainloop);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case SOUND_MIXER_INFO: {
|
|
|
|
|
mixer_info *mi = argp;
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SOUND_MIXER_INFO\n");
|
|
|
|
|
|
2006-05-24 02:13:29 +00:00
|
|
|
memset(mi, 0, sizeof(mixer_info));
|
2006-06-19 23:56:54 +00:00
|
|
|
strncpy(mi->id, "PULSEAUDIO", sizeof(mi->id));
|
2006-06-19 22:11:49 +00:00
|
|
|
strncpy(mi->name, "PulseAudio Virtual OSS", sizeof(mi->name));
|
2006-05-24 02:13:29 +00:00
|
|
|
pa_threaded_mainloop_lock(i->mainloop);
|
|
|
|
|
mi->modify_counter = i->volume_modify_count;
|
|
|
|
|
pa_threaded_mainloop_unlock(i->mainloop);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": unknown ioctl 0x%08lx\n", request);
|
2006-05-24 02:13:29 +00:00
|
|
|
|
|
|
|
|
*_errno = EINVAL;
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
|
|
|
|
|
return ret;
|
2006-05-23 14:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int map_format(int *fmt, pa_sample_spec *ss) {
|
|
|
|
|
|
|
|
|
|
switch (*fmt) {
|
|
|
|
|
case AFMT_MU_LAW:
|
|
|
|
|
ss->format = PA_SAMPLE_ULAW;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case AFMT_A_LAW:
|
|
|
|
|
ss->format = PA_SAMPLE_ALAW;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case AFMT_S8:
|
|
|
|
|
*fmt = AFMT_U8;
|
|
|
|
|
/* fall through */
|
|
|
|
|
case AFMT_U8:
|
|
|
|
|
ss->format = PA_SAMPLE_U8;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case AFMT_U16_BE:
|
|
|
|
|
*fmt = AFMT_S16_BE;
|
|
|
|
|
/* fall through */
|
|
|
|
|
case AFMT_S16_BE:
|
|
|
|
|
ss->format = PA_SAMPLE_S16BE;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case AFMT_U16_LE:
|
|
|
|
|
*fmt = AFMT_S16_LE;
|
|
|
|
|
/* fall through */
|
|
|
|
|
case AFMT_S16_LE:
|
|
|
|
|
ss->format = PA_SAMPLE_S16LE;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
ss->format = PA_SAMPLE_S16NE;
|
|
|
|
|
*fmt = AFMT_S16_NE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int map_format_back(pa_sample_format_t format) {
|
|
|
|
|
switch (format) {
|
|
|
|
|
case PA_SAMPLE_S16LE: return AFMT_S16_LE;
|
|
|
|
|
case PA_SAMPLE_S16BE: return AFMT_S16_BE;
|
|
|
|
|
case PA_SAMPLE_ULAW: return AFMT_MU_LAW;
|
|
|
|
|
case PA_SAMPLE_ALAW: return AFMT_A_LAW;
|
|
|
|
|
case PA_SAMPLE_U8: return AFMT_U8;
|
|
|
|
|
default:
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
static int dsp_flush_fd(int fd) {
|
2006-05-23 23:06:28 +00:00
|
|
|
int l;
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
if (ioctl(fd, SIOCINQ, &l) < 0) {
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ: %s\n", strerror(errno));
|
2006-05-23 23:06:28 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (l > 0) {
|
|
|
|
|
char buf[1024];
|
|
|
|
|
size_t k;
|
|
|
|
|
|
|
|
|
|
k = (size_t) l > sizeof(buf) ? sizeof(buf) : (size_t) l;
|
2006-06-16 21:07:32 +00:00
|
|
|
if (read(fd, buf, k) < 0)
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": read(): %s\n", strerror(errno));
|
2006-05-23 23:06:28 +00:00
|
|
|
l -= k;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
static int dsp_flush_socket(fd_info *i) {
|
|
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
|
|
if ((i->thread_fd < 0) && (i->app_fd < 0))
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
if (i->thread_fd >= 0)
|
|
|
|
|
res = dsp_flush_fd(i->thread_fd);
|
|
|
|
|
|
|
|
|
|
if (res < 0)
|
|
|
|
|
return res;
|
|
|
|
|
|
|
|
|
|
if (i->app_fd >= 0)
|
|
|
|
|
res = dsp_flush_fd(i->app_fd);
|
|
|
|
|
|
|
|
|
|
if (res < 0)
|
|
|
|
|
return res;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-23 14:42:23 +00:00
|
|
|
static int dsp_empty_socket(fd_info *i) {
|
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
|
|
/* Empty the socket */
|
|
|
|
|
for (;;) {
|
|
|
|
|
int l;
|
|
|
|
|
|
2006-05-24 00:42:30 +00:00
|
|
|
if (i->thread_fd < 0)
|
2006-05-23 14:42:23 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ: %s\n", strerror(errno));
|
2006-05-23 14:42:23 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-24 00:42:30 +00:00
|
|
|
if (!l) {
|
|
|
|
|
ret = 0;
|
2006-05-23 14:42:23 +00:00
|
|
|
break;
|
2006-05-24 00:42:30 +00:00
|
|
|
}
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
pa_threaded_mainloop_wait(i->mainloop);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int dsp_drain(fd_info *i) {
|
|
|
|
|
pa_operation *o = NULL;
|
|
|
|
|
int r = -1;
|
|
|
|
|
|
|
|
|
|
if (!i->mainloop)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": Draining.\n");
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
pa_threaded_mainloop_lock(i->mainloop);
|
|
|
|
|
|
|
|
|
|
if (dsp_empty_socket(i) < 0)
|
|
|
|
|
goto fail;
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
if (!i->play_stream)
|
2006-05-23 14:42:23 +00:00
|
|
|
goto fail;
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": Really draining.\n");
|
2006-05-23 14:42:23 +00:00
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
if (!(o = pa_stream_drain(i->play_stream, stream_success_cb, i))) {
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_drain(): %s\n", pa_strerror(pa_context_errno(i->context)));
|
2006-05-23 14:42:23 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i->operation_success = 0;
|
|
|
|
|
while (pa_operation_get_state(o) != PA_OPERATION_DONE) {
|
2006-06-16 21:07:32 +00:00
|
|
|
PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, fail);
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
pa_threaded_mainloop_wait(i->mainloop);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!i->operation_success) {
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_drain() 2: %s\n", pa_strerror(pa_context_errno(i->context)));
|
2006-05-23 14:42:23 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r = 0;
|
|
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
|
|
|
|
|
if (o)
|
|
|
|
|
pa_operation_unref(o);
|
|
|
|
|
|
|
|
|
|
pa_threaded_mainloop_unlock(i->mainloop);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int dsp_trigger(fd_info *i) {
|
|
|
|
|
pa_operation *o = NULL;
|
|
|
|
|
int r = -1;
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
if (!i->play_stream)
|
2006-05-23 14:42:23 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
pa_threaded_mainloop_lock(i->mainloop);
|
|
|
|
|
|
|
|
|
|
if (dsp_empty_socket(i) < 0)
|
|
|
|
|
goto fail;
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": Triggering.\n");
|
2006-05-23 14:42:23 +00:00
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
if (!(o = pa_stream_trigger(i->play_stream, stream_success_cb, i))) {
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i->context)));
|
2006-05-23 14:42:23 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i->operation_success = 0;
|
|
|
|
|
while (!pa_operation_get_state(o) != PA_OPERATION_DONE) {
|
2006-06-16 21:07:32 +00:00
|
|
|
PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, fail);
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
pa_threaded_mainloop_wait(i->mainloop);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!i->operation_success) {
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_trigger(): %s\n", pa_strerror(pa_context_errno(i->context)));
|
2006-05-23 14:42:23 +00:00
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r = 0;
|
|
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
|
|
|
|
|
if (o)
|
|
|
|
|
pa_operation_unref(o);
|
|
|
|
|
|
|
|
|
|
pa_threaded_mainloop_unlock(i->mainloop);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int dsp_ioctl(fd_info *i, unsigned long request, void*argp, int *_errno) {
|
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
|
|
switch (request) {
|
|
|
|
|
case SNDCTL_DSP_SETFMT: {
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SETFMT: %i\n", *(int*) argp);
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
pa_threaded_mainloop_lock(i->mainloop);
|
|
|
|
|
|
|
|
|
|
if (*(int*) argp == AFMT_QUERY)
|
|
|
|
|
*(int*) argp = map_format_back(i->sample_spec.format);
|
|
|
|
|
else {
|
|
|
|
|
map_format((int*) argp, &i->sample_spec);
|
2006-06-16 21:07:32 +00:00
|
|
|
free_streams(i);
|
2006-05-23 14:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_threaded_mainloop_unlock(i->mainloop);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case SNDCTL_DSP_SPEED: {
|
|
|
|
|
pa_sample_spec ss;
|
|
|
|
|
int valid;
|
2006-05-23 23:06:28 +00:00
|
|
|
char t[256];
|
2006-05-23 14:42:23 +00:00
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SPEED: %i\n", *(int*) argp);
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
pa_threaded_mainloop_lock(i->mainloop);
|
|
|
|
|
|
|
|
|
|
ss = i->sample_spec;
|
|
|
|
|
ss.rate = *(int*) argp;
|
|
|
|
|
|
|
|
|
|
if ((valid = pa_sample_spec_valid(&ss))) {
|
|
|
|
|
i->sample_spec = ss;
|
2006-06-16 21:07:32 +00:00
|
|
|
free_streams(i);
|
2006-05-23 14:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": ss: %s\n", pa_sample_spec_snprint(t, sizeof(t), &i->sample_spec));
|
2006-05-23 23:06:28 +00:00
|
|
|
|
2006-05-23 14:42:23 +00:00
|
|
|
pa_threaded_mainloop_unlock(i->mainloop);
|
|
|
|
|
|
|
|
|
|
if (!valid) {
|
|
|
|
|
*_errno = EINVAL;
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
2006-05-23 23:06:28 +00:00
|
|
|
|
2006-05-23 14:42:23 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case SNDCTL_DSP_STEREO:
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_STEREO: %i\n", *(int*) argp);
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
pa_threaded_mainloop_lock(i->mainloop);
|
|
|
|
|
|
|
|
|
|
i->sample_spec.channels = *(int*) argp ? 2 : 1;
|
2006-06-16 21:07:32 +00:00
|
|
|
free_streams(i);
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
pa_threaded_mainloop_unlock(i->mainloop);
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
case SNDCTL_DSP_CHANNELS: {
|
|
|
|
|
pa_sample_spec ss;
|
|
|
|
|
int valid;
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_CHANNELS: %i\n", *(int*) argp);
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
pa_threaded_mainloop_lock(i->mainloop);
|
|
|
|
|
|
|
|
|
|
ss = i->sample_spec;
|
|
|
|
|
ss.channels = *(int*) argp;
|
|
|
|
|
|
|
|
|
|
if ((valid = pa_sample_spec_valid(&ss))) {
|
|
|
|
|
i->sample_spec = ss;
|
2006-06-16 21:07:32 +00:00
|
|
|
free_streams(i);
|
2006-05-23 14:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_threaded_mainloop_unlock(i->mainloop);
|
|
|
|
|
|
|
|
|
|
if (!valid) {
|
|
|
|
|
*_errno = EINVAL;
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case SNDCTL_DSP_GETBLKSIZE:
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETBLKSIZE\n");
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
pa_threaded_mainloop_lock(i->mainloop);
|
|
|
|
|
|
|
|
|
|
fix_metrics(i);
|
|
|
|
|
*(int*) argp = i->fragment_size;
|
|
|
|
|
|
|
|
|
|
pa_threaded_mainloop_unlock(i->mainloop);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SNDCTL_DSP_SETFRAGMENT:
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SETFRAGMENT: 0x%8x\n", *(int*) argp);
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
pa_threaded_mainloop_lock(i->mainloop);
|
|
|
|
|
|
|
|
|
|
i->fragment_size = 1 << (*(int*) argp);
|
|
|
|
|
i->n_fragments = (*(int*) argp) >> 16;
|
2006-06-19 16:39:28 +00:00
|
|
|
|
|
|
|
|
/* 0x7FFF means that we can set whatever we like */
|
|
|
|
|
if (i->n_fragments == 0x7FFF)
|
|
|
|
|
i->n_fragments = 12;
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
free_streams(i);
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
pa_threaded_mainloop_unlock(i->mainloop);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SNDCTL_DSP_GETCAPS:
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_CAPS\n");
|
2006-05-23 14:42:23 +00:00
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
*(int*) argp = DSP_CAP_DUPLEX | DSP_CAP_MULTI;
|
2006-05-23 14:42:23 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SNDCTL_DSP_GETODELAY: {
|
|
|
|
|
int l;
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETODELAY\n");
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
pa_threaded_mainloop_lock(i->mainloop);
|
|
|
|
|
|
|
|
|
|
*(int*) argp = 0;
|
|
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
pa_usec_t usec;
|
2006-05-24 02:13:29 +00:00
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
PLAYBACK_STREAM_CHECK_DEAD_GOTO(i, exit_loop);
|
2006-05-23 14:42:23 +00:00
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
if (pa_stream_get_latency(i->play_stream, &usec, NULL) >= 0) {
|
2006-05-23 14:42:23 +00:00
|
|
|
*(int*) argp = pa_usec_to_bytes(usec, &i->sample_spec);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pa_context_errno(i->context) != PA_ERR_NODATA) {
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_get_latency(): %s\n", pa_strerror(pa_context_errno(i->context)));
|
2006-05-23 14:42:23 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_threaded_mainloop_wait(i->mainloop);
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-24 02:13:29 +00:00
|
|
|
exit_loop:
|
|
|
|
|
|
2006-05-23 14:42:23 +00:00
|
|
|
if (ioctl(i->thread_fd, SIOCINQ, &l) < 0)
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ failed: %s\n", strerror(errno));
|
2006-05-23 14:42:23 +00:00
|
|
|
else
|
|
|
|
|
*(int*) argp += l;
|
|
|
|
|
|
|
|
|
|
pa_threaded_mainloop_unlock(i->mainloop);
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": ODELAY: %i\n", *(int*) argp);
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case SNDCTL_DSP_RESET: {
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_RESET\n");
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
pa_threaded_mainloop_lock(i->mainloop);
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
free_streams(i);
|
2006-05-23 23:06:28 +00:00
|
|
|
dsp_flush_socket(i);
|
2006-05-23 14:42:23 +00:00
|
|
|
reset_params(i);
|
|
|
|
|
|
|
|
|
|
pa_threaded_mainloop_unlock(i->mainloop);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case SNDCTL_DSP_GETFMTS: {
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETFMTS\n");
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
*(int*) argp = AFMT_MU_LAW|AFMT_A_LAW|AFMT_U8|AFMT_S16_LE|AFMT_S16_BE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case SNDCTL_DSP_POST:
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_POST\n");
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
if (dsp_trigger(i) < 0)
|
|
|
|
|
*_errno = EIO;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SNDCTL_DSP_SYNC:
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_SYNC\n");
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
if (dsp_drain(i) < 0)
|
|
|
|
|
*_errno = EIO;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
case SNDCTL_DSP_GETOSPACE:
|
|
|
|
|
case SNDCTL_DSP_GETISPACE: {
|
2006-05-23 14:42:23 +00:00
|
|
|
audio_buf_info *bi = (audio_buf_info*) argp;
|
|
|
|
|
int l;
|
|
|
|
|
size_t k = 0;
|
|
|
|
|
|
2006-06-16 21:07:32 +00:00
|
|
|
if (request == SNDCTL_DSP_GETOSPACE)
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETOSPACE\n");
|
|
|
|
|
else
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SNDCTL_DSP_GETISPACE\n");
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
pa_threaded_mainloop_lock(i->mainloop);
|
|
|
|
|
|
|
|
|
|
fix_metrics(i);
|
2006-06-16 21:07:32 +00:00
|
|
|
|
|
|
|
|
if (request == SNDCTL_DSP_GETOSPACE) {
|
|
|
|
|
if (i->play_stream) {
|
|
|
|
|
if ((k = pa_stream_writable_size(i->play_stream)) == (size_t) -1)
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_writable_size(): %s\n", pa_strerror(pa_context_errno(i->context)));
|
|
|
|
|
} else
|
|
|
|
|
k = i->fragment_size * i->n_fragments;
|
|
|
|
|
|
|
|
|
|
if (ioctl(i->thread_fd, SIOCINQ, &l) < 0) {
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ failed: %s\n", strerror(errno));
|
|
|
|
|
l = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bi->bytes = k > (size_t) l ? k - l : 0;
|
|
|
|
|
} else {
|
|
|
|
|
if (i->rec_stream) {
|
|
|
|
|
if ((k = pa_stream_readable_size(i->rec_stream)) == (size_t) -1)
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": pa_stream_readable_size(): %s\n", pa_strerror(pa_context_errno(i->context)));
|
|
|
|
|
} else
|
|
|
|
|
k = 0;
|
|
|
|
|
|
|
|
|
|
if (ioctl(i->app_fd, SIOCINQ, &l) < 0) {
|
|
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": SIOCINQ failed: %s\n", strerror(errno));
|
|
|
|
|
l = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bi->bytes = k + l;
|
2006-05-23 14:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bi->fragsize = i->fragment_size;
|
|
|
|
|
bi->fragstotal = i->n_fragments;
|
|
|
|
|
bi->fragments = bi->bytes / bi->fragsize;
|
|
|
|
|
|
|
|
|
|
pa_threaded_mainloop_unlock(i->mainloop);
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": fragsize=%i, fragstotal=%i, bytes=%i, fragments=%i\n", bi->fragsize, bi->fragstotal, bi->bytes, bi->fragments);
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": unknown ioctl 0x%08lx\n", request);
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
*_errno = EINVAL;
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ioctl(int fd, unsigned long request, ...) {
|
|
|
|
|
fd_info *i;
|
|
|
|
|
va_list args;
|
|
|
|
|
void *argp;
|
|
|
|
|
int r, _errno = 0;
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_VERBOSE, __FILE__": ioctl()\n");
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
va_start(args, request);
|
|
|
|
|
argp = va_arg(args, void *);
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
|
|
if (!function_enter()) {
|
|
|
|
|
LOAD_IOCTL_FUNC();
|
|
|
|
|
return _ioctl(fd, request, argp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!(i = fd_info_find(fd))) {
|
|
|
|
|
function_exit();
|
|
|
|
|
LOAD_IOCTL_FUNC();
|
|
|
|
|
return _ioctl(fd, request, argp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (i->type == FD_INFO_MIXER)
|
|
|
|
|
r = mixer_ioctl(i, request, argp, &_errno);
|
|
|
|
|
else
|
|
|
|
|
r = dsp_ioctl(i, request, argp, &_errno);
|
|
|
|
|
|
|
|
|
|
fd_info_unref(i);
|
|
|
|
|
|
|
|
|
|
if (_errno)
|
|
|
|
|
errno = _errno;
|
|
|
|
|
|
|
|
|
|
function_exit();
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int close(int fd) {
|
|
|
|
|
fd_info *i;
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_VERBOSE, __FILE__": close()\n");
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
if (!function_enter()) {
|
|
|
|
|
LOAD_CLOSE_FUNC();
|
|
|
|
|
return _close(fd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!(i = fd_info_find(fd))) {
|
|
|
|
|
function_exit();
|
|
|
|
|
LOAD_CLOSE_FUNC();
|
|
|
|
|
return _close(fd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fd_info_remove_from_list(i);
|
|
|
|
|
fd_info_unref(i);
|
|
|
|
|
|
|
|
|
|
function_exit();
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-13 13:21:14 +00:00
|
|
|
int access(const char *pathname, int mode) {
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_VERBOSE, __FILE__": access(%s)\n", pathname);
|
2006-06-13 13:21:14 +00:00
|
|
|
|
|
|
|
|
if (strcmp(pathname, "/dev/dsp") != 0 &&
|
|
|
|
|
strcmp(pathname, "/dev/adsp") != 0 &&
|
|
|
|
|
strcmp(pathname, "/dev/sndstat") != 0 &&
|
|
|
|
|
strcmp(pathname, "/dev/mixer") != 0) {
|
|
|
|
|
LOAD_ACCESS_FUNC();
|
|
|
|
|
return _access(pathname, mode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mode & (W_OK | X_OK)) {
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": access(%s, %x) = EACCESS\n", pathname, mode);
|
2006-06-13 13:21:14 +00:00
|
|
|
errno = EACCES;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_NORMAL, __FILE__": access(%s, %x) = OK\n", pathname, mode);
|
|
|
|
|
|
2006-06-13 13:21:14 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-23 14:42:23 +00:00
|
|
|
int open64(const char *filename, int flags, ...) {
|
|
|
|
|
va_list args;
|
|
|
|
|
mode_t mode = 0;
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_VERBOSE, __FILE__": open64(%s)\n", filename);
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
va_start(args, flags);
|
|
|
|
|
if (flags & O_CREAT)
|
|
|
|
|
mode = va_arg(args, mode_t);
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
|
|
if (strcmp(filename, "/dev/dsp") != 0 &&
|
|
|
|
|
strcmp(filename, "/dev/adsp") != 0 &&
|
2006-05-23 23:57:50 +00:00
|
|
|
strcmp(filename, "/dev/sndstat") != 0 &&
|
2006-05-23 14:42:23 +00:00
|
|
|
strcmp(filename, "/dev/mixer") != 0) {
|
|
|
|
|
LOAD_OPEN64_FUNC();
|
|
|
|
|
return _open64(filename, flags, mode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return open(filename, flags, mode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FILE* fopen(const char *filename, const char *mode) {
|
|
|
|
|
FILE *f = NULL;
|
|
|
|
|
int fd;
|
2006-05-23 23:57:50 +00:00
|
|
|
mode_t m;
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_VERBOSE, __FILE__": fopen(%s)\n", filename);
|
2006-05-23 14:42:23 +00:00
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
if (strcmp(filename, "/dev/dsp") != 0 &&
|
|
|
|
|
strcmp(filename, "/dev/adsp") != 0 &&
|
|
|
|
|
strcmp(filename, "/dev/sndstat") != 0 &&
|
|
|
|
|
strcmp(filename, "/dev/mixer") != 0) {
|
|
|
|
|
LOAD_FOPEN_FUNC();
|
|
|
|
|
return _fopen(filename, mode);
|
|
|
|
|
}
|
2006-05-23 23:57:50 +00:00
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
switch (mode[0]) {
|
|
|
|
|
case 'r':
|
|
|
|
|
m = O_RDONLY;
|
|
|
|
|
break;
|
|
|
|
|
case 'w':
|
|
|
|
|
case 'a':
|
2006-05-23 23:57:50 +00:00
|
|
|
m = O_WRONLY;
|
2006-06-15 14:47:14 +00:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
errno = EINVAL;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2006-05-23 23:57:50 +00:00
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
if ((((mode[1] == 'b') || (mode[1] == 't')) && (mode[2] == '+')) || (mode[1] == '+'))
|
2006-05-23 23:57:50 +00:00
|
|
|
m = O_RDWR;
|
2006-05-23 14:42:23 +00:00
|
|
|
|
2006-05-23 23:57:50 +00:00
|
|
|
if ((fd = open(filename, m)) < 0)
|
2006-05-23 14:42:23 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
2006-05-23 23:57:50 +00:00
|
|
|
if (!(f = fdopen(fd, mode))) {
|
2006-05-23 14:42:23 +00:00
|
|
|
close(fd);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FILE *fopen64(const char *filename, const char *mode) {
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_VERBOSE, __FILE__": fopen64(%s)\n", filename);
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
if (strcmp(filename, "/dev/dsp") != 0 &&
|
|
|
|
|
strcmp(filename, "/dev/adsp") != 0 &&
|
2006-05-23 23:57:50 +00:00
|
|
|
strcmp(filename, "/dev/sndstat") != 0 &&
|
2006-05-23 14:42:23 +00:00
|
|
|
strcmp(filename, "/dev/mixer") != 0) {
|
|
|
|
|
LOAD_FOPEN64_FUNC();
|
|
|
|
|
return _fopen64(filename, mode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fopen(filename, mode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int fclose(FILE *f) {
|
|
|
|
|
fd_info *i;
|
|
|
|
|
|
2006-06-15 14:47:14 +00:00
|
|
|
debug(DEBUG_LEVEL_VERBOSE, __FILE__": fclose()\n");
|
2006-05-23 14:42:23 +00:00
|
|
|
|
|
|
|
|
if (!function_enter()) {
|
|
|
|
|
LOAD_FCLOSE_FUNC();
|
|
|
|
|
return _fclose(f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!(i = fd_info_find(fileno(f)))) {
|
|
|
|
|
function_exit();
|
|
|
|
|
LOAD_FCLOSE_FUNC();
|
|
|
|
|
return _fclose(f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fd_info_remove_from_list(i);
|
|
|
|
|
|
|
|
|
|
/* Dirty trick to avoid that the fd is not freed twice, once by us
|
|
|
|
|
* and once by the real fclose() */
|
|
|
|
|
i->app_fd = -1;
|
|
|
|
|
|
|
|
|
|
fd_info_unref(i);
|
|
|
|
|
|
|
|
|
|
function_exit();
|
|
|
|
|
|
|
|
|
|
LOAD_FCLOSE_FUNC();
|
|
|
|
|
return _fclose(f);
|
|
|
|
|
}
|