Formatting fixes for chan_alsa (issue #7807 reported by Mithraen with more mods done by myself)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@42388 65c4cc65-6c06-0410-ace0-fbb531ad65f3
1.4
Joshua Colp 19 years ago
parent d597b76c36
commit fd9f37a63f

@ -73,8 +73,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#endif
/*! Global jitterbuffer configuration - by default, jb is disabled */
static struct ast_jb_conf default_jbconf =
{
static struct ast_jb_conf default_jbconf = {
.flags = 0,
.max_size = -1,
.resync_threshold = -1,
@ -219,12 +218,12 @@ static int send_sound(void)
short myframe[FRAME_SIZE];
int total = FRAME_SIZE;
short *frame = NULL;
int amt=0;
int res;
int myoff;
int amt = 0, res, myoff;
snd_pcm_state_t state;
if (cursound > -1) {
if (cursound == -1)
return 0;
res = total;
if (sampsent < sounds[cursound].samplen) {
myoff = 0;
@ -261,30 +260,27 @@ static int send_sound(void)
}
}
if (res == 0 || !frame) {
if (res == 0 || !frame)
return 0;
}
#ifdef ALSA_MONITOR
alsa_monitor_write((char *) frame, res * 2);
#endif
state = snd_pcm_state(alsa.ocard);
if (state == SND_PCM_STATE_XRUN) {
if (state == SND_PCM_STATE_XRUN)
snd_pcm_prepare(alsa.ocard);
}
res = snd_pcm_writei(alsa.ocard, frame, res);
if (res > 0)
return 0;
return 0;
}
return 0;
}
static void *sound_thread(void *unused)
{
fd_set rfds;
fd_set wfds;
int max;
int res;
int max, res;
for (;;) {
FD_ZERO(&rfds);
FD_ZERO(&wfds);
@ -370,121 +366,102 @@ static snd_pcm_t *alsa_card_init(char *dev, snd_pcm_stream_t stream)
if (err < 0) {
ast_log(LOG_ERROR, "snd_pcm_open failed: %s\n", snd_strerror(err));
return NULL;
} else {
} else
ast_log(LOG_DEBUG, "Opening device %s in %s mode\n", dev, (stream == SND_PCM_STREAM_CAPTURE) ? "read" : "write");
}
snd_pcm_hw_params_alloca(&hwparams);
snd_pcm_hw_params_any(handle, hwparams);
err = snd_pcm_hw_params_set_access(handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
if (err < 0) {
if (err < 0)
ast_log(LOG_ERROR, "set_access failed: %s\n", snd_strerror(err));
}
err = snd_pcm_hw_params_set_format(handle, hwparams, format);
if (err < 0) {
if (err < 0)
ast_log(LOG_ERROR, "set_format failed: %s\n", snd_strerror(err));
}
err = snd_pcm_hw_params_set_channels(handle, hwparams, 1);
if (err < 0) {
if (err < 0)
ast_log(LOG_ERROR, "set_channels failed: %s\n", snd_strerror(err));
}
direction = 0;
err = snd_pcm_hw_params_set_rate_near(handle, hwparams, &rate, &direction);
if (rate != DESIRED_RATE) {
if (rate != DESIRED_RATE)
ast_log(LOG_WARNING, "Rate not correct, requested %d, got %d\n", DESIRED_RATE, rate);
}
direction = 0;
err = snd_pcm_hw_params_set_period_size_near(handle, hwparams, &period_size, &direction);
if (err < 0) {
if (err < 0)
ast_log(LOG_ERROR, "period_size(%ld frames) is bad: %s\n", period_size, snd_strerror(err));
} else {
else
ast_log(LOG_DEBUG, "Period size is %d\n", err);
}
buffer_size = 4096 * 2; /* period_size * 16; */
err = snd_pcm_hw_params_set_buffer_size_near(handle, hwparams, &buffer_size);
if (err < 0) {
if (err < 0)
ast_log(LOG_WARNING, "Problem setting buffer size of %ld: %s\n", buffer_size, snd_strerror(err));
} else {
else
ast_log(LOG_DEBUG, "Buffer size is set to %d frames\n", err);
}
#if 0
direction = 0;
err = snd_pcm_hw_params_set_periods_min(handle, hwparams, &per_min, &direction);
if (err < 0) {
if (err < 0)
ast_log(LOG_ERROR, "periods_min: %s\n", snd_strerror(err));
}
err = snd_pcm_hw_params_set_periods_max(handle, hwparams, &per_max, 0);
if (err < 0) {
if (err < 0)
ast_log(LOG_ERROR, "periods_max: %s\n", snd_strerror(err));
}
#endif
err = snd_pcm_hw_params(handle, hwparams);
if (err < 0) {
if (err < 0)
ast_log(LOG_ERROR, "Couldn't set the new hw params: %s\n", snd_strerror(err));
}
snd_pcm_sw_params_alloca(&swparams);
snd_pcm_sw_params_current(handle, swparams);
#if 1
if (stream == SND_PCM_STREAM_PLAYBACK) {
if (stream == SND_PCM_STREAM_PLAYBACK)
start_threshold = period_size;
} else {
else
start_threshold = 1;
}
err = snd_pcm_sw_params_set_start_threshold(handle, swparams, start_threshold);
if (err < 0) {
if (err < 0)
ast_log(LOG_ERROR, "start threshold: %s\n", snd_strerror(err));
}
#endif
#if 1
if (stream == SND_PCM_STREAM_PLAYBACK) {
if (stream == SND_PCM_STREAM_PLAYBACK)
stop_threshold = buffer_size;
} else {
else
stop_threshold = buffer_size;
}
err = snd_pcm_sw_params_set_stop_threshold(handle, swparams, stop_threshold);
if (err < 0) {
if (err < 0)
ast_log(LOG_ERROR, "stop threshold: %s\n", snd_strerror(err));
}
#endif
#if 0
err = snd_pcm_sw_params_set_xfer_align(handle, swparams, PERIOD_FRAMES);
if (err < 0) {
if (err < 0)
ast_log(LOG_ERROR, "Unable to set xfer alignment: %s\n", snd_strerror(err));
}
#endif
#if 0
err = snd_pcm_sw_params_set_silence_threshold(handle, swparams, silencethreshold);
if (err < 0) {
if (err < 0)
ast_log(LOG_ERROR, "Unable to set silence threshold: %s\n", snd_strerror(err));
}
#endif
err = snd_pcm_sw_params(handle, swparams);
if (err < 0) {
if (err < 0)
ast_log(LOG_ERROR, "sw_params: %s\n", snd_strerror(err));
}
err = snd_pcm_poll_descriptors_count(handle);
if (err <= 0) {
if (err <= 0)
ast_log(LOG_ERROR, "Unable to get a poll descriptors count, error is %s\n", snd_strerror(err));
}
if (err != 1) {
if (err != 1)
ast_log(LOG_DEBUG, "Can't handle more than one device\n");
}
snd_pcm_poll_descriptors(handle, &pfd, err);
ast_log(LOG_DEBUG, "Acquired fd %d from the poll descriptor\n", pfd.fd);
@ -599,13 +576,11 @@ static int alsa_hangup(struct ast_channel *c)
usecnt--;
ast_mutex_unlock(&usecnt_lock);
if (hookstate) {
if (autoanswer) {
hookstate = 0;
} else {
if (!autoanswer) {
/* Congestion noise */
res = 2;
write(sndcmd[1], &res, sizeof(res));
hookstate = 0;
}
}
snd_pcm_drop(alsa.icard);
@ -622,9 +597,11 @@ static int alsa_write(struct ast_channel *chan, struct ast_frame *f)
int res = 0;
/* size_t frames = 0; */
snd_pcm_state_t state;
/* Immediately return if no sound is enabled */
if (nosound)
return 0;
ast_mutex_lock(&alsalock);
/* Stop any currently playing sound */
if (cursound != -1) {
@ -646,9 +623,8 @@ static int alsa_write(struct ast_channel *chan, struct ast_frame *f)
alsa_monitor_write(sizbuf, len);
#endif
state = snd_pcm_state(alsa.ocard);
if (state == SND_PCM_STATE_XRUN) {
if (state == SND_PCM_STATE_XRUN)
snd_pcm_prepare(alsa.ocard);
}
res = snd_pcm_writei(alsa.ocard, sizbuf, len / 2);
if (res == -EPIPE) {
#if DEBUG
@ -664,9 +640,9 @@ static int alsa_write(struct ast_channel *chan, struct ast_frame *f)
res = -1;
}
} else {
if (res == -ESTRPIPE) {
if (res == -ESTRPIPE)
ast_log(LOG_ERROR, "You've got some big problems\n");
} else if (res < 0)
else if (res < 0)
ast_log(LOG_NOTICE, "Error %d on write\n", res);
}
}
@ -702,8 +678,7 @@ static struct ast_frame *alsa_read(struct ast_channel *chan)
f.delivery.tv_usec = 0;
state = snd_pcm_state(alsa.icard);
if ((state != SND_PCM_STATE_PREPARED) &&
(state != SND_PCM_STATE_RUNNING)) {
if ((state != SND_PCM_STATE_PREPARED) && (state != SND_PCM_STATE_RUNNING)) {
snd_pcm_prepare(alsa.icard);
}
@ -806,9 +781,11 @@ static int alsa_indicate(struct ast_channel *chan, int cond, const void *data, s
static struct ast_channel *alsa_new(struct chan_alsa_pvt *p, int state)
{
struct ast_channel *tmp;
tmp = ast_channel_alloc(1);
if (tmp) {
struct ast_channel *tmp = NULL;
if (!(tmp = ast_channel_alloc(1)))
return NULL;
tmp->tech = &alsa_tech;
ast_string_field_build(tmp, name, "ALSA/%s", indevname);
tmp->fds[0] = readdev;
@ -836,7 +813,7 @@ static struct ast_channel *alsa_new(struct chan_alsa_pvt *p, int state)
tmp = NULL;
}
}
}
return tmp;
}
@ -844,31 +821,35 @@ static struct ast_channel *alsa_request(const char *type, int format, void *data
{
int oldformat = format;
struct ast_channel *tmp = NULL;
format &= AST_FORMAT_SLINEAR;
if (!format) {
ast_log(LOG_NOTICE, "Asked to get a channel of format '%d'\n", oldformat);
return NULL;
}
ast_mutex_lock(&alsalock);
if (alsa.owner) {
ast_log(LOG_NOTICE, "Already have a call on the ALSA channel\n");
*cause = AST_CAUSE_BUSY;
} else {
tmp= alsa_new(&alsa, AST_STATE_DOWN);
if (!tmp) {
} else if (!(tmp = alsa_new(&alsa, AST_STATE_DOWN)))
ast_log(LOG_WARNING, "Unable to create new ALSA channel\n");
}
}
ast_mutex_unlock(&alsalock);
return tmp;
}
static int console_autoanswer(int fd, int argc, char *argv[])
{
int res = RESULT_SUCCESS;;
int res = RESULT_SUCCESS;
if ((argc != 1) && (argc != 2))
return RESULT_SHOWUSAGE;
ast_mutex_lock(&alsalock);
if (argc == 1) {
ast_cli(fd, "Auto answer is %s.\n", autoanswer ? "on" : "off");
} else {
@ -879,7 +860,9 @@ static int console_autoanswer(int fd, int argc, char *argv[])
else
res = RESULT_SHOWUSAGE;
}
ast_mutex_unlock(&alsalock);
return res;
}
@ -904,15 +887,17 @@ static char *autoanswer_complete(const char *line, const char *word, int pos, in
static const char autoanswer_usage[] =
"Usage: autoanswer [on|off]\n"
" Enables or disables autoanswer feature. If used without\n"
" argument, displays the current on/off status of autoanswer.\n"
" The default value of autoanswer is in 'alsa.conf'.\n";
" argument, displays the current on/off status of autoanswer.\n" " The default value of autoanswer is in 'alsa.conf'.\n";
static int console_answer(int fd, int argc, char *argv[])
{
int res = RESULT_SUCCESS;
if (argc != 1)
return RESULT_SHOWUSAGE;
ast_mutex_lock(&alsalock);
if (!alsa.owner) {
ast_cli(fd, "No one is calling us\n");
res = RESULT_FAILURE;
@ -927,23 +912,27 @@ static int console_answer(int fd, int argc, char *argv[])
}
answer_sound();
}
snd_pcm_prepare(alsa.icard);
snd_pcm_start(alsa.icard);
ast_mutex_unlock(&alsalock);
return RESULT_SUCCESS;
}
static char sendtext_usage[] =
"Usage: send text <message>\n"
" Sends a text message for display on the remote terminal.\n";
static char sendtext_usage[] = "Usage: send text <message>\n" " Sends a text message for display on the remote terminal.\n";
static int console_sendtext(int fd, int argc, char *argv[])
{
int tmparg = 2;
int res = RESULT_SUCCESS;
if (argc < 2)
return RESULT_SHOWUSAGE;
ast_mutex_lock(&alsalock);
if (!alsa.owner) {
ast_cli(fd, "No one is calling us\n");
res = RESULT_FAILURE;
@ -969,21 +958,25 @@ static int console_sendtext(int fd, int argc, char *argv[])
ast_mutex_unlock(&alsa.owner->lock);
}
}
ast_mutex_unlock(&alsalock);
return res;
}
static char answer_usage[] =
"Usage: answer\n"
" Answers an incoming call on the console (ALSA) channel.\n";
static char answer_usage[] = "Usage: answer\n" " Answers an incoming call on the console (ALSA) channel.\n";
static int console_hangup(int fd, int argc, char *argv[])
{
int res = RESULT_SUCCESS;
if (argc != 1)
return RESULT_SHOWUSAGE;
cursound = -1;
ast_mutex_lock(&alsalock);
if (!alsa.owner && !hookstate) {
ast_cli(fd, "No call to hangup up\n");
res = RESULT_FAILURE;
@ -995,13 +988,13 @@ static int console_hangup(int fd, int argc, char *argv[])
ast_mutex_unlock(&alsa.owner->lock);
}
}
ast_mutex_unlock(&alsalock);
return res;
}
static char hangup_usage[] =
"Usage: hangup\n"
" Hangs up any call currently placed on the console.\n";
static char hangup_usage[] = "Usage: hangup\n" " Hangs up any call currently placed on the console.\n";
static int console_dial(int fd, int argc, char *argv[])
@ -1010,9 +1003,12 @@ static int console_dial(int fd, int argc, char *argv[])
char *mye, *myc;
char *d;
int res = RESULT_SUCCESS;
if ((argc != 1) && (argc != 2))
return RESULT_SHOWUSAGE;
ast_mutex_lock(&alsalock);
if (alsa.owner) {
if (argc == 2) {
d = argv[1];
@ -1052,13 +1048,13 @@ static int console_dial(int fd, int argc, char *argv[])
} else
ast_cli(fd, "No such extension '%s' in context '%s'\n", mye, myc);
}
ast_mutex_unlock(&alsalock);
return res;
}
static char dial_usage[] =
"Usage: dial [extension[@context]]\n"
" Dials a given extension (and context if specified)\n";
static char dial_usage[] = "Usage: dial [extension[@context]]\n" " Dials a given extension (and context if specified)\n";
static struct ast_cli_entry myclis[] = {
@ -1071,8 +1067,7 @@ static struct ast_cli_entry myclis[] = {
static int load_module(void)
{
int res;
int x;
int res, x;
struct ast_config *cfg;
struct ast_variable *v;
@ -1132,9 +1127,8 @@ static int load_module(void)
ast_cli_register(myclis + x);
ast_pthread_create(&sthread, NULL, sound_thread, NULL);
#ifdef ALSA_MONITOR
if (alsa_monitor_start()) {
if (alsa_monitor_start())
ast_log(LOG_ERROR, "Problem starting Monitoring\n");
}
#endif
return 0;
}

Loading…
Cancel
Save