2010-08-18 08:29:03 +02:00
|
|
|
/*
|
|
|
|
* A simple PCM loopback utility with adaptive sample rate support
|
|
|
|
*
|
|
|
|
* Author: Jaroslav Kysela <perex@perex.cz>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2017-11-14 14:28:51 +01:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2010-08-18 08:29:03 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2023-08-30 18:48:23 +02:00
|
|
|
#include "aconfig.h"
|
2010-08-18 08:29:03 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <alsa/asoundlib.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <syslog.h>
|
2013-07-23 14:57:05 +02:00
|
|
|
#include <signal.h>
|
2010-08-18 08:29:03 +02:00
|
|
|
#include "alsaloop.h"
|
2022-12-31 08:03:34 +01:00
|
|
|
#include "os_compat.h"
|
2010-08-18 08:29:03 +02:00
|
|
|
|
|
|
|
struct loopback_thread {
|
|
|
|
int threaded;
|
|
|
|
pthread_t thread;
|
|
|
|
int exitcode;
|
|
|
|
struct loopback **loopbacks;
|
|
|
|
int loopbacks_count;
|
|
|
|
snd_output_t *output;
|
|
|
|
};
|
|
|
|
|
2010-10-13 23:39:36 +02:00
|
|
|
int quit = 0;
|
2010-08-18 08:29:03 +02:00
|
|
|
int verbose = 0;
|
2010-10-11 10:24:14 +02:00
|
|
|
int workarounds = 0;
|
2010-08-18 08:29:03 +02:00
|
|
|
int daemonize = 0;
|
|
|
|
int use_syslog = 0;
|
2010-10-06 18:30:18 +02:00
|
|
|
struct loopback **loopbacks = NULL;
|
2010-08-18 08:29:03 +02:00
|
|
|
int loopbacks_count = 0;
|
2010-10-08 22:23:05 +02:00
|
|
|
char **my_argv = NULL;
|
|
|
|
int my_argc = 0;
|
2010-10-13 23:39:36 +02:00
|
|
|
struct loopback_thread *threads;
|
|
|
|
int threads_count = 0;
|
|
|
|
pthread_t main_job;
|
|
|
|
int arg_default_xrun = 0;
|
2010-10-14 11:17:25 +02:00
|
|
|
int arg_default_wake = 0;
|
2010-08-18 08:29:03 +02:00
|
|
|
|
|
|
|
static void my_exit(struct loopback_thread *thread, int exitcode)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < thread->loopbacks_count; i++)
|
|
|
|
pcmjob_done(thread->loopbacks[i]);
|
|
|
|
if (thread->threaded) {
|
|
|
|
thread->exitcode = exitcode;
|
|
|
|
pthread_exit(0);
|
|
|
|
}
|
|
|
|
exit(exitcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int create_loopback_handle(struct loopback_handle **_handle,
|
|
|
|
const char *device,
|
2010-10-11 11:53:50 +02:00
|
|
|
const char *ctldev,
|
2010-08-18 08:29:03 +02:00
|
|
|
const char *id)
|
|
|
|
{
|
|
|
|
char idbuf[1024];
|
|
|
|
struct loopback_handle *handle;
|
|
|
|
|
|
|
|
handle = calloc(1, sizeof(*handle));
|
|
|
|
if (handle == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
if (device == NULL)
|
|
|
|
device = "hw:0,0";
|
|
|
|
handle->device = strdup(device);
|
2021-01-08 18:21:39 +01:00
|
|
|
if (handle->device == NULL) {
|
|
|
|
free(handle);
|
2010-10-11 11:53:50 +02:00
|
|
|
return -ENOMEM;
|
2021-01-08 18:21:39 +01:00
|
|
|
}
|
2010-10-11 11:53:50 +02:00
|
|
|
if (ctldev) {
|
|
|
|
handle->ctldev = strdup(ctldev);
|
2021-01-08 18:21:39 +01:00
|
|
|
if (handle->ctldev == NULL) {
|
|
|
|
free(handle->device);
|
|
|
|
free(handle);
|
2010-10-11 11:53:50 +02:00
|
|
|
return -ENOMEM;
|
2021-01-08 18:21:39 +01:00
|
|
|
}
|
2010-10-11 11:53:50 +02:00
|
|
|
} else {
|
|
|
|
handle->ctldev = NULL;
|
|
|
|
}
|
2010-08-18 08:29:03 +02:00
|
|
|
snprintf(idbuf, sizeof(idbuf)-1, "%s %s", id, device);
|
|
|
|
idbuf[sizeof(idbuf)-1] = '\0';
|
|
|
|
handle->id = strdup(idbuf);
|
|
|
|
handle->access = SND_PCM_ACCESS_RW_INTERLEAVED;
|
|
|
|
handle->format = SND_PCM_FORMAT_S16_LE;
|
2010-10-07 20:38:36 +02:00
|
|
|
handle->rate = handle->rate_req = 48000;
|
2010-08-18 08:29:03 +02:00
|
|
|
handle->channels = 2;
|
2010-10-07 00:12:38 +02:00
|
|
|
handle->resample = 0;
|
2010-08-18 08:29:03 +02:00
|
|
|
*_handle = handle;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int create_loopback(struct loopback **_handle,
|
|
|
|
struct loopback_handle *play,
|
|
|
|
struct loopback_handle *capt,
|
|
|
|
snd_output_t *output)
|
|
|
|
{
|
|
|
|
struct loopback *handle;
|
|
|
|
|
|
|
|
handle = calloc(1, sizeof(*handle));
|
|
|
|
if (handle == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
handle->play = play;
|
|
|
|
handle->capt = capt;
|
|
|
|
play->loopback = handle;
|
|
|
|
capt->loopback = handle;
|
|
|
|
handle->latency_req = 0;
|
|
|
|
handle->latency_reqtime = 10000;
|
|
|
|
handle->loop_time = ~0UL;
|
|
|
|
handle->loop_limit = ~0ULL;
|
|
|
|
handle->output = output;
|
2010-10-13 23:39:36 +02:00
|
|
|
handle->state = output;
|
2010-08-18 08:29:03 +02:00
|
|
|
#ifdef USE_SAMPLERATE
|
|
|
|
handle->src_enable = 1;
|
|
|
|
handle->src_converter_type = SRC_SINC_BEST_QUALITY;
|
|
|
|
#endif
|
|
|
|
*_handle = handle;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_loop_time(struct loopback *loop, unsigned long loop_time)
|
|
|
|
{
|
|
|
|
loop->loop_time = loop_time;
|
|
|
|
loop->loop_limit = loop->capt->rate * loop_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setscheduler(void)
|
|
|
|
{
|
|
|
|
struct sched_param sched_param;
|
|
|
|
|
|
|
|
if (sched_getparam(0, &sched_param) < 0) {
|
|
|
|
logit(LOG_WARNING, "Scheduler getparam failed.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sched_param.sched_priority = sched_get_priority_max(SCHED_RR);
|
|
|
|
if (!sched_setscheduler(0, SCHED_RR, &sched_param)) {
|
|
|
|
if (verbose)
|
|
|
|
logit(LOG_WARNING, "Scheduler set to Round Robin with priority %i\n", sched_param.sched_priority);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (verbose)
|
|
|
|
logit(LOG_INFO, "!!!Scheduler set to Round Robin with priority %i FAILED!\n", sched_param.sched_priority);
|
|
|
|
}
|
|
|
|
|
|
|
|
void help(void)
|
|
|
|
{
|
|
|
|
int k;
|
|
|
|
printf(
|
|
|
|
"Usage: alsaloop [OPTION]...\n\n"
|
|
|
|
"-h,--help help\n"
|
|
|
|
"-g,--config configuration file (one line = one job specified)\n"
|
|
|
|
"-d,--daemonize daemonize the main process and use syslog for errors\n"
|
|
|
|
"-P,--pdevice playback device\n"
|
|
|
|
"-C,--cdevice capture device\n"
|
2010-10-11 11:53:50 +02:00
|
|
|
"-X,--pctl playback ctl device\n"
|
|
|
|
"-Y,--cctl capture ctl device\n"
|
2021-10-13 12:33:43 +02:00
|
|
|
"-x,--prateshift playback 'PCM Rate Shift 100000' ascii ctl name\n"
|
2010-08-18 08:29:03 +02:00
|
|
|
"-l,--latency requested latency in frames\n"
|
|
|
|
"-t,--tlatency requested latency in usec (1/1000000sec)\n"
|
|
|
|
"-f,--format sample format\n"
|
|
|
|
"-c,--channels channels\n"
|
|
|
|
"-r,--rate rate\n"
|
|
|
|
"-n,--resample resample in alsa-lib\n"
|
|
|
|
"-A,--samplerate use converter (0=sincbest,1=sincmedium,2=sincfastest,\n"
|
|
|
|
" 3=zerohold,4=linear)\n"
|
|
|
|
"-B,--buffer buffer size in frames\n"
|
|
|
|
"-E,--period period size in frames\n"
|
|
|
|
"-s,--seconds duration of loop in seconds\n"
|
|
|
|
"-b,--nblock non-block mode (very early process wakeup)\n"
|
|
|
|
"-S,--sync sync mode(0=none,1=simple,2=captshift,3=playshift,4=samplerate,\n"
|
|
|
|
" 5=auto)\n"
|
|
|
|
"-a,--slave stream parameters slave mode (0=auto, 1=on, 2=off)\n"
|
|
|
|
"-T,--thread thread number (-1 = create unique)\n"
|
|
|
|
"-m,--mixer redirect mixer, argument is:\n"
|
2010-10-08 15:10:23 +02:00
|
|
|
" SRC_SLAVE_ID(PLAYBACK)[@DST_SLAVE_ID(CAPTURE)]\n"
|
|
|
|
"-O,--ossmixer rescan and redirect oss mixer, argument is:\n"
|
|
|
|
" ALSA_ID@OSS_ID (for example: \"Master@VOLUME\")\n"
|
2010-08-18 08:29:03 +02:00
|
|
|
"-e,--effect apply an effect (bandpass filter sweep)\n"
|
|
|
|
"-v,--verbose verbose mode (more -v means more verbose)\n"
|
2010-10-11 10:24:14 +02:00
|
|
|
"-w,--workaround use workaround (serialopen)\n"
|
2010-10-13 23:39:36 +02:00
|
|
|
"-U,--xrun xrun profiling\n"
|
2010-10-14 11:17:25 +02:00
|
|
|
"-W,--wake process wake timeout in ms\n"
|
2013-07-26 16:16:00 +02:00
|
|
|
"-z,--syslog use syslog for errors\n"
|
2010-08-18 08:29:03 +02:00
|
|
|
);
|
|
|
|
printf("\nRecognized sample formats are:");
|
|
|
|
for (k = 0; k < SND_PCM_FORMAT_LAST; ++k) {
|
|
|
|
const char *s = snd_pcm_format_name(k);
|
|
|
|
if (s)
|
|
|
|
printf(" %s", s);
|
|
|
|
}
|
|
|
|
printf("\n\n");
|
|
|
|
printf(
|
|
|
|
"Tip #1 (usable 500ms latency, good CPU usage, superb xrun prevention):\n"
|
|
|
|
" alsaloop -t 500000\n"
|
|
|
|
"Tip #2 (superb 1ms latency, but heavy CPU usage):\n"
|
|
|
|
" alsaloop -t 1000\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static long timediff(struct timeval t1, struct timeval t2)
|
|
|
|
{
|
|
|
|
signed long l;
|
|
|
|
|
|
|
|
t1.tv_sec -= t2.tv_sec;
|
|
|
|
l = (signed long) t1.tv_usec - (signed long) t2.tv_usec;
|
|
|
|
if (l < 0) {
|
|
|
|
t1.tv_sec--;
|
|
|
|
l = 1000000 + l;
|
|
|
|
l %= 1000000;
|
|
|
|
}
|
|
|
|
return (t1.tv_sec * 1000000) + l;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_loop(struct loopback *loop)
|
|
|
|
{
|
2010-10-06 18:30:18 +02:00
|
|
|
loopbacks = realloc(loopbacks, (loopbacks_count + 1) *
|
|
|
|
sizeof(struct loopback *));
|
2010-08-18 08:29:03 +02:00
|
|
|
if (loopbacks == NULL) {
|
|
|
|
logit(LOG_CRIT, "No enough memory\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
loopbacks[loopbacks_count++] = loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int init_mixer_control(struct loopback_control *control,
|
|
|
|
char *id)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = snd_ctl_elem_id_malloc(&control->id);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
err = snd_ctl_elem_info_malloc(&control->info);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
err = snd_ctl_elem_value_malloc(&control->value);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
err = control_parse_id(id, control->id);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int add_mixers(struct loopback *loop,
|
|
|
|
char **mixers,
|
|
|
|
int mixers_count)
|
|
|
|
{
|
|
|
|
struct loopback_mixer *mixer, *last = NULL;
|
|
|
|
char *str1;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
while (mixers_count > 0) {
|
|
|
|
mixer = calloc(1, sizeof(*mixer));
|
|
|
|
if (mixer == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
if (last)
|
|
|
|
last->next = mixer;
|
|
|
|
else
|
|
|
|
loop->controls = mixer;
|
|
|
|
last = mixer;
|
|
|
|
str1 = strchr(*mixers, '@');
|
|
|
|
if (str1)
|
|
|
|
*str1 = '\0';
|
|
|
|
err = init_mixer_control(&mixer->src, *mixers);
|
|
|
|
if (err < 0) {
|
|
|
|
logit(LOG_CRIT, "Wrong mixer control ID syntax '%s'\n", *mixers);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
err = init_mixer_control(&mixer->dst, str1 ? str1 + 1 : *mixers);
|
|
|
|
if (err < 0) {
|
|
|
|
logit(LOG_CRIT, "Wrong mixer control ID syntax '%s'\n", str1 ? str1 + 1 : *mixers);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (str1)
|
|
|
|
*str1 = '@';
|
|
|
|
mixers++;
|
|
|
|
mixers_count--;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-10-08 15:10:23 +02:00
|
|
|
static int add_oss_mixers(struct loopback *loop,
|
|
|
|
char **mixers,
|
|
|
|
int mixers_count)
|
|
|
|
{
|
|
|
|
struct loopback_ossmixer *mixer, *last = NULL;
|
|
|
|
char *str1, *str2;
|
|
|
|
|
|
|
|
while (mixers_count > 0) {
|
|
|
|
mixer = calloc(1, sizeof(*mixer));
|
|
|
|
if (mixer == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
if (last)
|
|
|
|
last->next = mixer;
|
|
|
|
else
|
|
|
|
loop->oss_controls = mixer;
|
|
|
|
last = mixer;
|
|
|
|
str1 = strchr(*mixers, ',');
|
|
|
|
if (str1)
|
|
|
|
*str1 = '\0';
|
|
|
|
str2 = strchr(str1 ? str1 + 1 : *mixers, '@');
|
|
|
|
if (str2)
|
|
|
|
*str2 = '\0';
|
|
|
|
mixer->alsa_id = strdup(*mixers);
|
|
|
|
if (str1)
|
|
|
|
mixer->alsa_index = atoi(str1);
|
|
|
|
mixer->oss_id = strdup(str2 ? str2 + 1 : *mixers);
|
|
|
|
if (mixer->alsa_id == NULL || mixer->oss_id == NULL) {
|
|
|
|
logit(LOG_CRIT, "Not enough memory");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
if (str1)
|
|
|
|
*str1 = ',';
|
|
|
|
if (str2)
|
|
|
|
*str2 = ',';
|
|
|
|
mixers++;
|
|
|
|
mixers_count--;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-26 16:16:00 +02:00
|
|
|
static void enable_syslog(void)
|
|
|
|
{
|
|
|
|
if (!use_syslog) {
|
|
|
|
use_syslog = 1;
|
|
|
|
openlog("alsaloop", LOG_NDELAY|LOG_PID, LOG_DAEMON);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-18 08:29:03 +02:00
|
|
|
static int parse_config_file(const char *file, snd_output_t *output);
|
|
|
|
|
2010-10-13 23:39:36 +02:00
|
|
|
static int parse_config(int argc, char *argv[], snd_output_t *output,
|
|
|
|
int cmdline)
|
2010-08-18 08:29:03 +02:00
|
|
|
{
|
|
|
|
struct option long_option[] =
|
|
|
|
{
|
|
|
|
{"help", 0, NULL, 'h'},
|
|
|
|
{"config", 1, NULL, 'g'},
|
|
|
|
{"daemonize", 0, NULL, 'd'},
|
|
|
|
{"pdevice", 1, NULL, 'P'},
|
|
|
|
{"cdevice", 1, NULL, 'C'},
|
2010-10-11 11:53:50 +02:00
|
|
|
{"pctl", 1, NULL, 'X'},
|
|
|
|
{"cctl", 1, NULL, 'Y'},
|
2021-10-13 12:33:43 +02:00
|
|
|
{"prateshift", 1, NULL, 'x'},
|
2010-08-18 08:29:03 +02:00
|
|
|
{"latency", 1, NULL, 'l'},
|
|
|
|
{"tlatency", 1, NULL, 't'},
|
|
|
|
{"format", 1, NULL, 'f'},
|
|
|
|
{"channels", 1, NULL, 'c'},
|
|
|
|
{"rate", 1, NULL, 'r'},
|
|
|
|
{"buffer", 1, NULL, 'B'},
|
|
|
|
{"period", 1, NULL, 'E'},
|
|
|
|
{"seconds", 1, NULL, 's'},
|
|
|
|
{"nblock", 0, NULL, 'b'},
|
|
|
|
{"effect", 0, NULL, 'e'},
|
|
|
|
{"verbose", 0, NULL, 'v'},
|
|
|
|
{"resample", 0, NULL, 'n'},
|
|
|
|
{"samplerate", 1, NULL, 'A'},
|
|
|
|
{"sync", 1, NULL, 'S'},
|
|
|
|
{"slave", 1, NULL, 'a'},
|
|
|
|
{"thread", 1, NULL, 'T'},
|
|
|
|
{"mixer", 1, NULL, 'm'},
|
2010-10-08 15:10:23 +02:00
|
|
|
{"ossmixer", 1, NULL, 'O'},
|
2010-10-11 10:24:14 +02:00
|
|
|
{"workaround", 1, NULL, 'w'},
|
2010-10-13 23:39:36 +02:00
|
|
|
{"xrun", 0, NULL, 'U'},
|
2013-07-26 16:16:00 +02:00
|
|
|
{"syslog", 0, NULL, 'z'},
|
2010-08-18 08:29:03 +02:00
|
|
|
{NULL, 0, NULL, 0},
|
|
|
|
};
|
|
|
|
int err, morehelp;
|
|
|
|
char *arg_config = NULL;
|
|
|
|
char *arg_pdevice = NULL;
|
|
|
|
char *arg_cdevice = NULL;
|
2010-10-11 11:53:50 +02:00
|
|
|
char *arg_pctl = NULL;
|
|
|
|
char *arg_cctl = NULL;
|
2021-10-13 12:33:43 +02:00
|
|
|
char *arg_prateshift = NULL;
|
2010-08-18 08:29:03 +02:00
|
|
|
unsigned int arg_latency_req = 0;
|
|
|
|
unsigned int arg_latency_reqtime = 10000;
|
|
|
|
snd_pcm_format_t arg_format = SND_PCM_FORMAT_S16_LE;
|
|
|
|
unsigned int arg_channels = 2;
|
|
|
|
unsigned int arg_rate = 48000;
|
|
|
|
snd_pcm_uframes_t arg_buffer_size = 0;
|
|
|
|
snd_pcm_uframes_t arg_period_size = 0;
|
|
|
|
unsigned long arg_loop_time = ~0UL;
|
|
|
|
int arg_nblock = 0;
|
2023-08-30 12:36:15 +02:00
|
|
|
// int arg_effect = 0;
|
2010-08-18 08:29:03 +02:00
|
|
|
int arg_resample = 0;
|
2012-11-30 14:35:31 +01:00
|
|
|
#ifdef USE_SAMPLERATE
|
2010-10-07 20:38:36 +02:00
|
|
|
int arg_samplerate = SRC_SINC_FASTEST + 1;
|
2012-11-30 14:53:12 +01:00
|
|
|
#endif
|
2010-08-18 08:29:03 +02:00
|
|
|
int arg_sync = SYNC_TYPE_AUTO;
|
|
|
|
int arg_slave = SLAVE_TYPE_AUTO;
|
|
|
|
int arg_thread = 0;
|
|
|
|
struct loopback *loop = NULL;
|
|
|
|
char *arg_mixers[MAX_MIXERS];
|
|
|
|
int arg_mixers_count = 0;
|
2010-10-08 15:10:23 +02:00
|
|
|
char *arg_ossmixers[MAX_MIXERS];
|
|
|
|
int arg_ossmixers_count = 0;
|
2010-10-13 23:39:36 +02:00
|
|
|
int arg_xrun = arg_default_xrun;
|
2010-10-14 11:17:25 +02:00
|
|
|
int arg_wake = arg_default_wake;
|
2010-08-18 08:29:03 +02:00
|
|
|
|
|
|
|
morehelp = 0;
|
|
|
|
while (1) {
|
|
|
|
int c;
|
2010-10-08 15:10:23 +02:00
|
|
|
if ((c = getopt_long(argc, argv,
|
2021-10-13 12:33:43 +02:00
|
|
|
"hdg:P:C:X:Y:x:l:t:F:f:c:r:s:benvA:S:a:m:T:O:w:UW:z",
|
2010-10-08 15:10:23 +02:00
|
|
|
long_option, NULL)) < 0)
|
2010-08-18 08:29:03 +02:00
|
|
|
break;
|
|
|
|
switch (c) {
|
|
|
|
case 'h':
|
|
|
|
morehelp++;
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
arg_config = strdup(optarg);
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
daemonize = 1;
|
2013-07-26 16:16:00 +02:00
|
|
|
enable_syslog();
|
2010-08-18 08:29:03 +02:00
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
arg_pdevice = strdup(optarg);
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
arg_cdevice = strdup(optarg);
|
|
|
|
break;
|
2010-10-11 11:53:50 +02:00
|
|
|
case 'X':
|
|
|
|
arg_pctl = strdup(optarg);
|
|
|
|
break;
|
|
|
|
case 'Y':
|
|
|
|
arg_cctl = strdup(optarg);
|
|
|
|
break;
|
2021-10-13 12:33:43 +02:00
|
|
|
case 'x':
|
|
|
|
arg_prateshift = strdup(optarg);
|
|
|
|
break;
|
2010-08-18 08:29:03 +02:00
|
|
|
case 'l':
|
|
|
|
err = atoi(optarg);
|
|
|
|
arg_latency_req = err >= 4 ? err : 4;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
err = atoi(optarg);
|
|
|
|
arg_latency_reqtime = err >= 500 ? err : 500;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
arg_format = snd_pcm_format_value(optarg);
|
|
|
|
if (arg_format == SND_PCM_FORMAT_UNKNOWN) {
|
|
|
|
logit(LOG_WARNING, "Unknown format, setting to default S16_LE\n");
|
|
|
|
arg_format = SND_PCM_FORMAT_S16_LE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
err = atoi(optarg);
|
|
|
|
arg_channels = err >= 1 && err < 1024 ? err : 1;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
err = atoi(optarg);
|
|
|
|
arg_rate = err >= 4000 && err < 200000 ? err : 44100;
|
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
err = atoi(optarg);
|
|
|
|
arg_buffer_size = err >= 32 && err < 200000 ? err : 0;
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
err = atoi(optarg);
|
|
|
|
arg_period_size = err >= 32 && err < 200000 ? err : 0;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
err = atoi(optarg);
|
|
|
|
arg_loop_time = err >= 1 && err <= 100000 ? err : 30;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
arg_nblock = 1;
|
|
|
|
break;
|
|
|
|
case 'e':
|
2023-08-30 12:36:15 +02:00
|
|
|
// arg_effect = 1;
|
2010-08-18 08:29:03 +02:00
|
|
|
break;
|
|
|
|
case 'n':
|
2010-10-07 00:12:38 +02:00
|
|
|
arg_resample = 1;
|
2010-08-18 08:29:03 +02:00
|
|
|
break;
|
2012-11-30 14:35:31 +01:00
|
|
|
#ifdef USE_SAMPLERATE
|
2010-08-18 08:29:03 +02:00
|
|
|
case 'A':
|
|
|
|
if (strcasecmp(optarg, "sincbest") == 0)
|
|
|
|
arg_samplerate = SRC_SINC_BEST_QUALITY;
|
|
|
|
else if (strcasecmp(optarg, "sincmedium") == 0)
|
|
|
|
arg_samplerate = SRC_SINC_MEDIUM_QUALITY;
|
|
|
|
else if (strcasecmp(optarg, "sincfastest") == 0)
|
|
|
|
arg_samplerate = SRC_SINC_FASTEST;
|
|
|
|
else if (strcasecmp(optarg, "zerohold") == 0)
|
|
|
|
arg_samplerate = SRC_ZERO_ORDER_HOLD;
|
|
|
|
else if (strcasecmp(optarg, "linear") == 0)
|
|
|
|
arg_samplerate = SRC_LINEAR;
|
|
|
|
else
|
|
|
|
arg_samplerate = atoi(optarg);
|
|
|
|
if (arg_samplerate < 0 || arg_samplerate > SRC_LINEAR)
|
|
|
|
arg_sync = SRC_SINC_FASTEST;
|
|
|
|
arg_samplerate += 1;
|
|
|
|
break;
|
2012-11-30 14:35:31 +01:00
|
|
|
#endif
|
2010-08-18 08:29:03 +02:00
|
|
|
case 'S':
|
|
|
|
if (strcasecmp(optarg, "samplerate") == 0)
|
|
|
|
arg_sync = SYNC_TYPE_SAMPLERATE;
|
|
|
|
else if (optarg[0] == 'n')
|
|
|
|
arg_sync = SYNC_TYPE_NONE;
|
|
|
|
else if (optarg[0] == 's')
|
|
|
|
arg_sync = SYNC_TYPE_SIMPLE;
|
|
|
|
else if (optarg[0] == 'c')
|
|
|
|
arg_sync = SYNC_TYPE_CAPTRATESHIFT;
|
|
|
|
else if (optarg[0] == 'p')
|
|
|
|
arg_sync = SYNC_TYPE_PLAYRATESHIFT;
|
|
|
|
else if (optarg[0] == 'r')
|
|
|
|
arg_sync = SYNC_TYPE_SAMPLERATE;
|
2021-10-13 14:00:49 +02:00
|
|
|
else if (optarg[0] == 'a')
|
|
|
|
arg_sync = SYNC_TYPE_AUTO;
|
2010-08-18 08:29:03 +02:00
|
|
|
else
|
|
|
|
arg_sync = atoi(optarg);
|
|
|
|
if (arg_sync < 0 || arg_sync > SYNC_TYPE_LAST)
|
|
|
|
arg_sync = SYNC_TYPE_AUTO;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
if (optarg[0] == 'a')
|
|
|
|
arg_slave = SLAVE_TYPE_AUTO;
|
2010-10-06 20:34:03 +02:00
|
|
|
else if (strcasecmp(optarg, "on") == 0)
|
2010-08-18 08:29:03 +02:00
|
|
|
arg_slave = SLAVE_TYPE_ON;
|
2010-10-06 20:34:03 +02:00
|
|
|
else if (strcasecmp(optarg, "off") == 0)
|
2010-08-18 08:29:03 +02:00
|
|
|
arg_slave = SLAVE_TYPE_OFF;
|
|
|
|
else
|
|
|
|
arg_slave = atoi(optarg);
|
|
|
|
if (arg_slave < 0 || arg_slave > SLAVE_TYPE_LAST)
|
|
|
|
arg_slave = SLAVE_TYPE_AUTO;
|
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
arg_thread = atoi(optarg);
|
|
|
|
if (arg_thread < 0)
|
|
|
|
arg_thread = 10000000 + loopbacks_count;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
if (arg_mixers_count >= MAX_MIXERS) {
|
|
|
|
logit(LOG_CRIT, "Maximum redirected mixer controls reached (max %i)\n", (int)MAX_MIXERS);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
arg_mixers[arg_mixers_count++] = optarg;
|
|
|
|
break;
|
2010-10-08 15:10:23 +02:00
|
|
|
case 'O':
|
|
|
|
if (arg_ossmixers_count >= MAX_MIXERS) {
|
|
|
|
logit(LOG_CRIT, "Maximum redirected mixer controls reached (max %i)\n", (int)MAX_MIXERS);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
arg_ossmixers[arg_ossmixers_count++] = optarg;
|
|
|
|
break;
|
2010-08-18 08:29:03 +02:00
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
break;
|
2010-10-11 10:24:14 +02:00
|
|
|
case 'w':
|
|
|
|
if (strcasecmp(optarg, "serialopen") == 0)
|
|
|
|
workarounds |= WORKAROUND_SERIALOPEN;
|
|
|
|
break;
|
2010-10-13 23:39:36 +02:00
|
|
|
case 'U':
|
|
|
|
arg_xrun = 1;
|
|
|
|
if (cmdline)
|
|
|
|
arg_default_xrun = 1;
|
|
|
|
break;
|
2010-10-14 11:17:25 +02:00
|
|
|
case 'W':
|
|
|
|
arg_wake = atoi(optarg);
|
|
|
|
if (cmdline)
|
|
|
|
arg_default_wake = arg_wake;
|
|
|
|
break;
|
2013-07-26 16:16:00 +02:00
|
|
|
case 'z':
|
|
|
|
enable_syslog();
|
|
|
|
break;
|
2010-08-18 08:29:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (morehelp) {
|
|
|
|
help();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
if (arg_config == NULL) {
|
|
|
|
struct loopback_handle *play;
|
|
|
|
struct loopback_handle *capt;
|
2010-10-11 11:53:50 +02:00
|
|
|
err = create_loopback_handle(&play, arg_pdevice, arg_pctl, "playback");
|
2010-08-18 08:29:03 +02:00
|
|
|
if (err < 0) {
|
|
|
|
logit(LOG_CRIT, "Unable to create playback handle.\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2010-10-11 11:53:50 +02:00
|
|
|
err = create_loopback_handle(&capt, arg_cdevice, arg_cctl, "capture");
|
2010-08-18 08:29:03 +02:00
|
|
|
if (err < 0) {
|
|
|
|
logit(LOG_CRIT, "Unable to create capture handle.\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
err = create_loopback(&loop, play, capt, output);
|
|
|
|
if (err < 0) {
|
|
|
|
logit(LOG_CRIT, "Unable to create loopback handle.\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
play->format = capt->format = arg_format;
|
2010-10-07 20:38:36 +02:00
|
|
|
play->rate = play->rate_req = capt->rate = capt->rate_req = arg_rate;
|
2010-08-18 08:29:03 +02:00
|
|
|
play->channels = capt->channels = arg_channels;
|
|
|
|
play->buffer_size_req = capt->buffer_size_req = arg_buffer_size;
|
|
|
|
play->period_size_req = capt->period_size_req = arg_period_size;
|
|
|
|
play->resample = capt->resample = arg_resample;
|
|
|
|
play->nblock = capt->nblock = arg_nblock ? 1 : 0;
|
|
|
|
loop->latency_req = arg_latency_req;
|
|
|
|
loop->latency_reqtime = arg_latency_reqtime;
|
|
|
|
loop->sync = arg_sync;
|
|
|
|
loop->slave = arg_slave;
|
|
|
|
loop->thread = arg_thread;
|
2010-10-13 23:39:36 +02:00
|
|
|
loop->xrun = arg_xrun;
|
2010-10-14 11:17:25 +02:00
|
|
|
loop->wake = arg_wake;
|
2010-08-18 08:29:03 +02:00
|
|
|
err = add_mixers(loop, arg_mixers, arg_mixers_count);
|
|
|
|
if (err < 0) {
|
|
|
|
logit(LOG_CRIT, "Unable to add mixer controls.\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2010-10-08 15:10:23 +02:00
|
|
|
err = add_oss_mixers(loop, arg_ossmixers, arg_ossmixers_count);
|
|
|
|
if (err < 0) {
|
|
|
|
logit(LOG_CRIT, "Unable to add ossmixer controls.\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2021-10-13 12:33:43 +02:00
|
|
|
if (arg_prateshift)
|
|
|
|
play->prateshift_name = arg_prateshift;
|
|
|
|
|
2010-08-18 08:29:03 +02:00
|
|
|
#ifdef USE_SAMPLERATE
|
|
|
|
loop->src_enable = arg_samplerate > 0;
|
|
|
|
if (loop->src_enable)
|
|
|
|
loop->src_converter_type = arg_samplerate - 1;
|
|
|
|
#endif
|
|
|
|
set_loop_time(loop, arg_loop_time);
|
|
|
|
add_loop(loop);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parse_config_file(arg_config, output);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_config_file(const char *file, snd_output_t *output)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char line[2048], word[2048];
|
|
|
|
char *str, *ptr;
|
|
|
|
int argc, c, err = 0;
|
|
|
|
char **argv;
|
|
|
|
|
|
|
|
fp = fopen(file, "r");
|
|
|
|
if (fp == NULL) {
|
|
|
|
logit(LOG_CRIT, "Unable to open file '%s': %s\n", file, strerror(errno));
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
while (!feof(fp)) {
|
|
|
|
if (fgets(line, sizeof(line)-1, fp) == NULL)
|
|
|
|
break;
|
|
|
|
line[sizeof(line)-1] = '\0';
|
2010-10-08 22:23:05 +02:00
|
|
|
my_argv = realloc(my_argv, my_argc + MAX_ARGS * sizeof(char *));
|
|
|
|
if (my_argv == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
argv = my_argv + my_argc;
|
2010-08-18 08:29:03 +02:00
|
|
|
argc = 0;
|
|
|
|
argv[argc++] = strdup("<prog>");
|
2010-10-08 22:23:05 +02:00
|
|
|
my_argc++;
|
2010-08-18 08:29:03 +02:00
|
|
|
str = line;
|
|
|
|
while (*str) {
|
|
|
|
ptr = word;
|
|
|
|
while (*str && (*str == ' ' || *str < ' '))
|
|
|
|
str++;
|
|
|
|
if (*str == '#')
|
|
|
|
goto __next;
|
|
|
|
if (*str == '\'' || *str == '\"') {
|
|
|
|
c = *str++;
|
|
|
|
while (*str && *str != c)
|
|
|
|
*ptr++ = *str++;
|
|
|
|
if (*str == c)
|
|
|
|
str++;
|
|
|
|
} else {
|
|
|
|
while (*str && *str != ' ' && *str != '\t')
|
|
|
|
*ptr++ = *str++;
|
|
|
|
}
|
|
|
|
if (ptr != word) {
|
2010-10-08 22:23:05 +02:00
|
|
|
if (*(ptr-1) == '\n')
|
|
|
|
ptr--;
|
2010-08-18 08:29:03 +02:00
|
|
|
*ptr = '\0';
|
2010-10-08 22:23:05 +02:00
|
|
|
if (argc >= MAX_ARGS) {
|
|
|
|
logit(LOG_CRIT, "Too many arguments.");
|
|
|
|
goto __error;
|
|
|
|
}
|
2010-08-18 08:29:03 +02:00
|
|
|
argv[argc++] = strdup(word);
|
2010-10-08 22:23:05 +02:00
|
|
|
my_argc++;
|
2010-08-18 08:29:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* erase runtime variables for getopt */
|
|
|
|
optarg = NULL;
|
|
|
|
optind = opterr = 1;
|
2010-10-08 22:23:05 +02:00
|
|
|
optopt = '?';
|
2010-08-18 08:29:03 +02:00
|
|
|
|
2010-10-13 23:39:36 +02:00
|
|
|
err = parse_config(argc, argv, output, 0);
|
2010-08-18 08:29:03 +02:00
|
|
|
__next:
|
|
|
|
if (err < 0)
|
|
|
|
break;
|
|
|
|
err = 0;
|
|
|
|
}
|
2010-10-08 22:23:05 +02:00
|
|
|
__error:
|
2010-08-18 08:29:03 +02:00
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void thread_job1(void *_data)
|
|
|
|
{
|
|
|
|
struct loopback_thread *thread = _data;
|
|
|
|
snd_output_t *output = thread->output;
|
|
|
|
struct pollfd *pfds = NULL;
|
|
|
|
int pfds_count = 0;
|
2010-10-14 11:17:25 +02:00
|
|
|
int i, j, err, wake = 1000000;
|
2010-08-18 08:29:03 +02:00
|
|
|
|
|
|
|
setscheduler();
|
|
|
|
|
|
|
|
for (i = 0; i < thread->loopbacks_count; i++) {
|
|
|
|
err = pcmjob_init(thread->loopbacks[i]);
|
|
|
|
if (err < 0) {
|
|
|
|
logit(LOG_CRIT, "Loopback initialization failure.\n");
|
|
|
|
my_exit(thread, EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < thread->loopbacks_count; i++) {
|
|
|
|
err = pcmjob_start(thread->loopbacks[i]);
|
|
|
|
if (err < 0) {
|
|
|
|
logit(LOG_CRIT, "Loopback start failure.\n");
|
|
|
|
my_exit(thread, EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
pfds_count += thread->loopbacks[i]->pollfd_count;
|
2010-10-14 11:17:25 +02:00
|
|
|
j = thread->loopbacks[i]->wake;
|
|
|
|
if (j > 0 && j < wake)
|
|
|
|
wake = j;
|
2010-08-18 08:29:03 +02:00
|
|
|
}
|
2010-10-14 11:17:25 +02:00
|
|
|
if (wake >= 1000000)
|
|
|
|
wake = -1;
|
2010-08-18 08:29:03 +02:00
|
|
|
pfds = calloc(pfds_count, sizeof(struct pollfd));
|
2010-10-08 22:23:05 +02:00
|
|
|
if (pfds == NULL || pfds_count <= 0) {
|
2010-08-18 08:29:03 +02:00
|
|
|
logit(LOG_CRIT, "Poll FDs allocation failed.\n");
|
|
|
|
my_exit(thread, EXIT_FAILURE);
|
|
|
|
}
|
2010-10-13 23:39:36 +02:00
|
|
|
while (!quit) {
|
2010-08-18 08:29:03 +02:00
|
|
|
struct timeval tv1, tv2;
|
|
|
|
for (i = j = 0; i < thread->loopbacks_count; i++) {
|
|
|
|
err = pcmjob_pollfds_init(thread->loopbacks[i], &pfds[j]);
|
|
|
|
if (err < 0) {
|
|
|
|
logit(LOG_CRIT, "Poll FD initialization failed.\n");
|
|
|
|
my_exit(thread, EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
j += err;
|
|
|
|
}
|
|
|
|
if (verbose > 10)
|
|
|
|
gettimeofday(&tv1, NULL);
|
2010-10-14 11:17:25 +02:00
|
|
|
err = poll(pfds, j, wake);
|
2010-10-13 23:39:36 +02:00
|
|
|
if (err < 0)
|
|
|
|
err = -errno;
|
2010-08-18 08:29:03 +02:00
|
|
|
if (verbose > 10) {
|
|
|
|
gettimeofday(&tv2, NULL);
|
|
|
|
snd_output_printf(output, "pool took %lius\n", timediff(tv2, tv1));
|
|
|
|
}
|
|
|
|
if (err < 0) {
|
2010-10-13 23:39:36 +02:00
|
|
|
if (err == -EINTR || err == -ERESTART)
|
|
|
|
continue;
|
|
|
|
logit(LOG_CRIT, "Poll failed: %s\n", strerror(-err));
|
2010-08-18 08:29:03 +02:00
|
|
|
my_exit(thread, EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
for (i = j = 0; i < thread->loopbacks_count; i++) {
|
|
|
|
struct loopback *loop = thread->loopbacks[i];
|
|
|
|
if (j < loop->active_pollfd_count) {
|
|
|
|
err = pcmjob_pollfds_handle(loop, &pfds[j]);
|
|
|
|
if (err < 0) {
|
|
|
|
logit(LOG_CRIT, "pcmjob failed.\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
j += loop->active_pollfd_count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my_exit(thread, EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void thread_job(struct loopback_thread *thread)
|
|
|
|
{
|
|
|
|
if (!thread->threaded) {
|
|
|
|
thread_job1(thread);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pthread_create(&thread->thread, NULL, (void *) &thread_job1,
|
|
|
|
(void *) thread);
|
|
|
|
}
|
|
|
|
|
2010-10-13 23:39:36 +02:00
|
|
|
static void send_to_all(int sig)
|
|
|
|
{
|
|
|
|
struct loopback_thread *thread;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < threads_count; i++) {
|
|
|
|
thread = &threads[i];
|
|
|
|
if (thread->threaded)
|
|
|
|
pthread_kill(thread->thread, sig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-04 16:45:09 +02:00
|
|
|
static void signal_handler(int sig ATTRIBUTE_UNUSED)
|
2010-10-13 23:39:36 +02:00
|
|
|
{
|
|
|
|
quit = 1;
|
|
|
|
send_to_all(SIGUSR2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void signal_handler_state(int sig)
|
|
|
|
{
|
|
|
|
pthread_t self = pthread_self();
|
|
|
|
struct loopback_thread *thread;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
if (pthread_equal(main_job, self))
|
|
|
|
send_to_all(SIGUSR1);
|
|
|
|
for (i = 0; i < threads_count; i++) {
|
|
|
|
thread = &threads[i];
|
|
|
|
if (thread->thread == self) {
|
|
|
|
for (j = 0; j < thread->loopbacks_count; j++)
|
|
|
|
pcmjob_state(thread->loopbacks[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
signal(sig, signal_handler_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void signal_handler_ignore(int sig)
|
|
|
|
{
|
|
|
|
signal(sig, signal_handler_ignore);
|
|
|
|
}
|
|
|
|
|
2010-08-18 08:29:03 +02:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
snd_output_t *output;
|
|
|
|
int i, j, k, l, err;
|
|
|
|
|
|
|
|
err = snd_output_stdio_attach(&output, stdout, 0);
|
|
|
|
if (err < 0) {
|
|
|
|
logit(LOG_CRIT, "Output failed: %s\n", snd_strerror(err));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2010-10-13 23:39:36 +02:00
|
|
|
err = parse_config(argc, argv, output, 1);
|
2010-08-18 08:29:03 +02:00
|
|
|
if (err < 0) {
|
|
|
|
logit(LOG_CRIT, "Unable to parse arguments or configuration...\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2010-10-08 22:23:05 +02:00
|
|
|
while (my_argc > 0)
|
|
|
|
free(my_argv[--my_argc]);
|
|
|
|
free(my_argv);
|
2010-08-18 08:29:03 +02:00
|
|
|
|
|
|
|
if (loopbacks_count <= 0) {
|
|
|
|
logit(LOG_CRIT, "No loopback defined...\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (daemonize) {
|
|
|
|
if (daemon(0, 0) < 0) {
|
|
|
|
logit(LOG_CRIT, "daemon() failed: %s\n", strerror(errno));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
i = fork();
|
|
|
|
if (i < 0) {
|
|
|
|
logit(LOG_CRIT, "fork() failed: %s\n", strerror(errno));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (i > 0) {
|
|
|
|
/* wait(&i); */
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we must sort thread IDs */
|
2010-10-06 18:51:29 +02:00
|
|
|
j = -1;
|
2010-08-18 08:29:03 +02:00
|
|
|
do {
|
|
|
|
k = 0x7fffffff;
|
|
|
|
for (i = 0; i < loopbacks_count; i++) {
|
|
|
|
if (loopbacks[i]->thread < k &&
|
|
|
|
loopbacks[i]->thread > j)
|
|
|
|
k = loopbacks[i]->thread;
|
|
|
|
}
|
2010-10-06 18:51:29 +02:00
|
|
|
j++;
|
2010-08-18 08:29:03 +02:00
|
|
|
for (i = 0; i < loopbacks_count; i++) {
|
|
|
|
if (loopbacks[i]->thread == k)
|
|
|
|
loopbacks[i]->thread = j;
|
|
|
|
}
|
|
|
|
} while (k != 0x7fffffff);
|
|
|
|
/* fix maximum thread id */
|
|
|
|
for (i = 0, j = -1; i < loopbacks_count; i++) {
|
|
|
|
if (loopbacks[i]->thread > j)
|
|
|
|
j = loopbacks[i]->thread;
|
|
|
|
}
|
|
|
|
j += 1;
|
|
|
|
threads = calloc(1, sizeof(struct loopback_thread) * j);
|
|
|
|
if (threads == NULL) {
|
|
|
|
logit(LOG_CRIT, "No enough memory\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
/* sort all threads */
|
|
|
|
for (k = 0; k < j; k++) {
|
|
|
|
for (i = l = 0; i < loopbacks_count; i++)
|
|
|
|
if (loopbacks[i]->thread == k)
|
|
|
|
l++;
|
|
|
|
threads[k].loopbacks = malloc(l * sizeof(struct loopback *));
|
|
|
|
threads[k].loopbacks_count = l;
|
|
|
|
threads[k].output = output;
|
|
|
|
threads[k].threaded = j > 1;
|
|
|
|
for (i = l = 0; i < loopbacks_count; i++)
|
|
|
|
if (loopbacks[i]->thread == k)
|
|
|
|
threads[k].loopbacks[l++] = loopbacks[i];
|
|
|
|
}
|
2010-10-13 23:39:36 +02:00
|
|
|
threads_count = j;
|
|
|
|
main_job = pthread_self();
|
|
|
|
|
|
|
|
signal(SIGINT, signal_handler);
|
|
|
|
signal(SIGTERM, signal_handler);
|
|
|
|
signal(SIGABRT, signal_handler);
|
|
|
|
signal(SIGUSR1, signal_handler_state);
|
|
|
|
signal(SIGUSR2, signal_handler_ignore);
|
|
|
|
|
|
|
|
for (k = 0; k < threads_count; k++)
|
2010-08-18 08:29:03 +02:00
|
|
|
thread_job(&threads[k]);
|
|
|
|
|
|
|
|
if (j > 1) {
|
2010-10-13 23:39:36 +02:00
|
|
|
for (k = 0; k < threads_count; k++)
|
2010-08-18 08:29:03 +02:00
|
|
|
pthread_join(threads[k].thread, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (use_syslog)
|
|
|
|
closelog();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|