--- /home/anton/ast-cvs/asterisk/channels/chan_alsa.c 2004-04-11 03:59:11.000000000 +0200 +++ channels/chan_alsa.c 2004-06-02 14:12:48.000000000 +0200 @@ -59,7 +59,6 @@ #define MIN_SWITCH_TIME 600 static snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE; -//static int block = O_NONBLOCK; static char indevname[50] = ALSA_INDEV; static char outdevname[50] = ALSA_OUTDEV; @@ -74,8 +73,6 @@ static int silencesuppression = 0; static int silencethreshold = 1000; -static char digits[80] = ""; -static char text2send[80] = ""; static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER; @@ -90,7 +87,6 @@ /* Command pipe */ static int cmd[2]; - int hookstate=0; static short silence[FRAME_SIZE] = {0, }; @@ -121,13 +117,8 @@ struct ast_channel *owner; char exten[AST_MAX_EXTENSION]; char context[AST_MAX_EXTENSION]; -#if 0 - snd_pcm_t *card; -#endif snd_pcm_t *icard, *ocard; - } alsa; - #if 0 static int time_has_passed(void) { @@ -141,7 +132,6 @@ return 0; } #endif - /* Number of buffers... Each is FRAMESIZE/8 ms long. For example with 160 sample frames, and a buffer size of 3, we have a 60ms buffer, usually plenty. */ @@ -149,12 +139,6 @@ pthread_t sthread; #define MAX_BUFFER_SIZE 100 -//static int buffersize = 3; - -//static int full_duplex = 0; - -/* Are we reading or writing (simulated full duplex) */ -//static int readmode = 1; /* File descriptors for sound device */ static int readdev = -1; @@ -244,7 +228,6 @@ res = snd_pcm_writei(alsa.ocard, frame, res); if (res > 0) return 0; - return 0; } return 0; } @@ -255,6 +238,11 @@ fd_set wfds; int max; int res; + char ign[4096]; + if (read(readdev, ign, sizeof(readdev)) < 0) + ast_verbose("Read error on sound device: %s\n", strerror(errno)); + + for(;;) { FD_ZERO(&rfds); FD_ZERO(&wfds); @@ -326,12 +314,9 @@ snd_pcm_sw_params_t *swparams = NULL; struct pollfd pfd; snd_pcm_uframes_t period_size = PERIOD_FRAMES * 4; - //int period_bytes = 0; snd_pcm_uframes_t buffer_size = 0; unsigned int rate = DESIRED_RATE; - unsigned int per_min = 1; - //unsigned int per_max = 8; snd_pcm_uframes_t start_threshold, stop_threshold; err = snd_pcm_open(&handle, dev, stream, O_NONBLOCK); @@ -493,13 +478,19 @@ static int alsa_call(struct ast_channel *c, char *dest, int timeout) { int res = 3; + struct ast_frame f = { 0, }; ast_verbose( " << Call placed to '%s' on console >> \n", dest); if (autoanswer) { ast_verbose( " << Auto-answered >> \n" ); - needanswer = 1; + f.frametype = AST_FRAME_CONTROL; + f.subclass = AST_CONTROL_ANSWER; + ast_queue_frame(c, &f, 0); } else { + nosound = 1; ast_verbose( " << Type 'answer' to answer, or use 'autoanswer' for future calls >> \n"); - needringing = 1; + f.frametype = AST_FRAME_CONTROL; + f.subclass = AST_CONTROL_RINGING; + ast_queue_frame(c, &f, 0); write(sndcmd[1], &res, sizeof(res)); } return 0; @@ -520,12 +511,13 @@ answer_sound(); ast_setstate(c, AST_STATE_UP); cursound = -1; + nosound=0; return 0; } static int alsa_hangup(struct ast_channel *c) { - int res; + int res = 0; cursound = -1; c->pvt->pvt = NULL; alsa.owner = NULL; @@ -533,8 +525,6 @@ ast_mutex_lock(&usecnt_lock); usecnt--; ast_mutex_unlock(&usecnt_lock); - needhangup = 0; - needanswer = 0; if (hookstate) { res = 2; write(sndcmd[1], &res, sizeof(res)); @@ -636,7 +626,6 @@ return 0; } - static struct ast_frame *alsa_read(struct ast_channel *chan) { static struct ast_frame f; @@ -650,7 +639,6 @@ snd_pcm_state_t state; int r = 0; int off = 0; - /* Acknowledge any pending cmd */ res = read(cmd[0], &b, sizeof(b)); if (res > 0) @@ -665,41 +653,6 @@ f.src = type; f.mallocd = 0; - if (needringing) { - f.frametype = AST_FRAME_CONTROL; - f.subclass = AST_CONTROL_RINGING; - needringing = 0; - return &f; - } - - if (needhangup) { - needhangup = 0; - return NULL; - } - if (strlen(text2send)) { - f.frametype = AST_FRAME_TEXT; - f.subclass = 0; - f.data = text2send; - f.datalen = strlen(text2send); - strcpy(text2send,""); - return &f; - } - if (strlen(digits)) { - f.frametype = AST_FRAME_DTMF; - f.subclass = digits[0]; - for (res=0;resname, sizeof(tmp->name), "ALSA/%s", indevname); tmp->type = type; - tmp->fds[0] = readdev; + tmp->fds[0] = sndcmd[0]; tmp->fds[1] = cmd[0]; tmp->nativeformats = AST_FORMAT_SLINEAR; tmp->pvt->pvt = p; @@ -918,21 +864,29 @@ static int console_sendtext(int fd, int argc, char *argv[]) { int tmparg = 2; - if (argc < 2) - return RESULT_SHOWUSAGE; - if (!alsa.owner) { - ast_cli(fd, "No one is calling us\n"); - return RESULT_FAILURE; - } - if (strlen(text2send)) - ast_cli(fd, "Warning: message already waiting to be sent, overwriting\n"); - strcpy(text2send, ""); - while(tmparg <= argc) { - strncat(text2send, argv[tmparg++], sizeof(text2send) - strlen(text2send)); - strncat(text2send, " ", sizeof(text2send) - strlen(text2send)); - } - needanswer++; - return RESULT_SUCCESS; + char text2send[256]; + struct ast_frame f = { 0, }; + if (argc < 2) + return RESULT_SHOWUSAGE; + if (!alsa.owner) { + ast_cli(fd, "No one is calling us\n"); + return RESULT_FAILURE; + } + if (strlen(text2send)) + ast_cli(fd, "Warning: message already waiting to be sent, overwriting\n"); + strcpy(text2send, ""); + while(tmparg < argc) { + strncat(text2send, argv[tmparg++], sizeof(text2send) - strlen(text2send)); + strncat(text2send, " ", sizeof(text2send) - strlen(text2send)); + } + if (strlen(text2send)) { + f.frametype = AST_FRAME_TEXT; + f.subclass = 0; + f.data = text2send; + f.datalen = strlen(text2send); + ast_queue_frame(alsa.owner, &f, 1); + } + return RESULT_SUCCESS; } static char answer_usage[] = @@ -950,7 +904,7 @@ } hookstate = 0; if (alsa.owner) { - ast_queue_hangup(alsa.owner); + ast_queue_hangup(alsa.owner, 1); } return RESULT_SUCCESS; } @@ -964,14 +918,16 @@ { char tmp[256], *tmp2; char *mye, *myc; - int b = 0; + int x; + struct ast_frame f = { AST_FRAME_DTMF, 0 }; if ((argc != 1) && (argc != 2)) return RESULT_SHOWUSAGE; if (alsa.owner) { if (argc == 2) { - strncat(digits, argv[1], sizeof(digits) - strlen(digits)); - /* Wake up the polling thread */ - write(cmd[1], &b, sizeof(b)); + for (x=0;x