Don't do reinvite if both parties talk diffrent codecs
[asterisk/asterisk.git] / channels / chan_oss.c
index 784067a..3557ac0 100755 (executable)
@@ -15,6 +15,7 @@
  * the GNU General Public License
  */
 
+#include <asterisk/lock.h>
 #include <asterisk/frame.h>
 #include <asterisk/logger.h>
 #include <asterisk/channel.h>
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
+#ifdef __linux
 #include <linux/soundcard.h>
+#elif defined(__FreeBSD__)
+#include <machine/soundcard.h>
+#else
+#include <soundcard.h>
+#endif
 #include "busy.h"
 #include "ringtone.h"
 #include "ring10.h"
 #include "answer.h"
 
 /* Which device to use */
+#ifdef __OpenBSD__
+#define DEV_DSP "/dev/audio"
+#else
 #define DEV_DSP "/dev/dsp"
+#endif
 
 /* Lets use 160 sample frames, just like GSM.  */
 #define FRAME_SIZE 160
 static struct timeval lasttime;
 
 static int usecnt;
-static int needanswer = 0;
-static int needhangup = 0;
 static int silencesuppression = 0;
 static int silencethreshold = 1000;
 
-static char digits[80] = "";
-static char text2send[80] = "";
 
-static pthread_mutex_t usecnt_lock = PTHREAD_MUTEX_INITIALIZER;
+static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
 
 static char *type = "Console";
 static char *desc = "OSS Console Channel Driver";
@@ -74,10 +81,7 @@ static char context[AST_MAX_EXTENSION] = "default";
 static char language[MAX_LANGUAGE] = "";
 static char exten[AST_MAX_EXTENSION] = "s";
 
-/* Command pipe */
-static int cmd[2];
-
-int hookstate=0;
+static int hookstate=0;
 
 static short silence[FRAME_SIZE] = {0, };
 
@@ -109,7 +113,7 @@ static struct chan_oss_pvt {
        char context[AST_MAX_EXTENSION];
 } oss;
 
-static int time_has_passed()
+static int time_has_passed(void)
 {
        struct timeval tv;
        int ms;
@@ -125,7 +129,7 @@ static int time_has_passed()
    with 160 sample frames, and a buffer size of 3, we have a 60ms buffer, 
    usually plenty. */
 
-pthread_t sthread;
+static pthread_t sthread;
 
 #define MAX_BUFFER_SIZE 100
 static int buffersize = 3;
@@ -140,6 +144,7 @@ static int sounddev = -1;
 
 static int autoanswer = 1;
  
+#if 0
 static int calc_loudness(short *frame)
 {
        int sum = 0;
@@ -153,6 +158,7 @@ static int calc_loudness(short *frame)
        sum = sum/FRAME_SIZE;
        return sum;
 }
+#endif
 
 static int cursound = -1;
 static int sampsent = 0;
@@ -212,7 +218,8 @@ static int send_sound(void)
                                }
                        }
                }
-               res = write(sounddev, frame, res * 2);
+               if (frame)
+                       res = write(sounddev, frame, res * 2);
                if (res > 0)
                        return 0;
                return res;
@@ -226,17 +233,25 @@ static void *sound_thread(void *unused)
        fd_set wfds;
        int max;
        int res;
+       char ign[4096];
+       if (read(sounddev, ign, sizeof(sounddev)) < 0)
+               ast_log(LOG_WARNING, "Read error on sound device: %s\n", strerror(errno));
        for(;;) {
                FD_ZERO(&rfds);
                FD_ZERO(&wfds);
                max = sndcmd[0];
                FD_SET(sndcmd[0], &rfds);
+               if (!oss.owner) {
+                       FD_SET(sounddev, &rfds);
+                       if (sounddev > max)
+                               max = sounddev;
+               }
                if (cursound > -1) {
                        FD_SET(sounddev, &wfds);
                        if (sounddev > max)
                                max = sounddev;
                }
-               res = select(max + 1, &rfds, &wfds, NULL, NULL);
+               res = ast_select(max + 1, &rfds, &wfds, NULL, NULL);
                if (res < 1) {
                        ast_log(LOG_WARNING, "select failed: %s\n", strerror(errno));
                        continue;
@@ -247,6 +262,11 @@ static void *sound_thread(void *unused)
                        offset = 0;
                        sampsent = 0;
                }
+               if (FD_ISSET(sounddev, &rfds)) {
+                       /* Ignore read */
+                       if (read(sounddev, ign, sizeof(ign)) < 0)
+                               ast_log(LOG_WARNING, "Read error on sound device: %s\n", strerror(errno));
+               }
                if (FD_ISSET(sounddev, &wfds))
                        if (send_sound())
                                ast_log(LOG_WARNING, "Failed to write sound\n");
@@ -401,7 +421,7 @@ static int soundcard_setinput(int force)
        return 1;
 }
 
-static int soundcard_init()
+static int soundcard_init(void)
 {
        /* Assume it's full duplex for starters */
        int fd = open(DEV_DSP,  O_RDWR | O_NONBLOCK);
@@ -432,12 +452,19 @@ static int oss_text(struct ast_channel *c, char *text)
 static int oss_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");
+               f.frametype = AST_FRAME_CONTROL;
+               f.subclass = AST_CONTROL_RINGING;
+               ast_queue_frame(c, &f, 0);
                write(sndcmd[1], &res, sizeof(res));
        }
        return 0;
@@ -456,26 +483,31 @@ static int oss_answer(struct ast_channel *c)
 {
        ast_verbose( " << Console call has been answered >> \n");
        answer_sound();
-       c->state = AST_STATE_UP;
+       ast_setstate(c, AST_STATE_UP);
        cursound = -1;
+       nosound=0;
        return 0;
 }
 
 static int oss_hangup(struct ast_channel *c)
 {
-       int res;
+       int res = 0;
        cursound = -1;
        c->pvt->pvt = NULL;
        oss.owner = NULL;
        ast_verbose( " << Hangup on console >> \n");
-       ast_pthread_mutex_lock(&usecnt_lock);
+       ast_mutex_lock(&usecnt_lock);
        usecnt--;
-       ast_pthread_mutex_unlock(&usecnt_lock);
-       needhangup = 0;
-       needanswer = 0;
+       ast_mutex_unlock(&usecnt_lock);
        if (hookstate) {
-               res = 2;
-               write(sndcmd[1], &res, sizeof(res));
+               if (autoanswer) {
+                       /* Assume auto-hangup too */
+                       hookstate = 0;
+               } else {
+                       /* Make congestion noise */
+                       res = 2;
+                       write(sndcmd[1], &res, sizeof(res));
+               }
        }
        return 0;
 }
@@ -527,7 +559,7 @@ static int oss_write(struct ast_channel *chan, struct ast_frame *f)
                return 0;
        /* Stop any currently playing sound */
        cursound = -1;
-       if (!full_duplex && (strlen(digits) || needhangup || needanswer)) {
+       if (!full_duplex) {
                /* If we're half duplex, we have to switch to read mode
                   to honor immediate needs if necessary */
                res = soundcard_setinput(1);
@@ -570,57 +602,20 @@ static struct ast_frame *oss_read(struct ast_channel *chan)
        static char buf[FRAME_SIZE * 2 + AST_FRIENDLY_OFFSET];
        static int readpos = 0;
        int res;
-       int b;
-       int nonull=0;
        
 #if 0
        ast_log(LOG_DEBUG, "oss_read()\n");
 #endif
-       
-       /* Acknowledge any pending cmd */
-       res = read(cmd[0], &b, sizeof(b));
-       if (res > 0)
-               nonull = 1;
-       
+               
        f.frametype = AST_FRAME_NULL;
        f.subclass = 0;
-       f.timelen = 0;
+       f.samples = 0;
        f.datalen = 0;
        f.data = NULL;
        f.offset = 0;
        f.src = type;
        f.mallocd = 0;
        
-       if (needhangup) {
-               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;res<strlen(digits);res++)
-                       digits[res] = digits[res + 1];
-               return &f;
-       }
-       
-       if (needanswer) {
-               needanswer = 0;
-               f.frametype = AST_FRAME_CONTROL;
-               f.subclass = AST_CONTROL_ANSWER;
-               chan->state = AST_STATE_UP;
-               return &f;
-       }
-       
-       if (nonull)
-               return &f;
-               
        res = soundcard_setinput(0);
        if (res < 0) {
                ast_log(LOG_WARNING, "Unable to set input mode\n");
@@ -632,8 +627,10 @@ static struct ast_frame *oss_read(struct ast_channel *chan)
        }
        res = read(sounddev, buf + AST_FRIENDLY_OFFSET + readpos, FRAME_SIZE * 2 - readpos);
        if (res < 0) {
-               ast_log(LOG_WARNING, "Error reading from sound device: %s\n", strerror(errno));
+               ast_log(LOG_WARNING, "Error reading from sound device (If you're running 'artsd' then kill it): %s\n", strerror(errno));
+#if 0
                CRASH;
+#endif         
                return NULL;
        }
        readpos += res;
@@ -641,9 +638,13 @@ static struct ast_frame *oss_read(struct ast_channel *chan)
        if (readpos >= FRAME_SIZE * 2) {
                /* A real frame */
                readpos = 0;
+               if (chan->_state != AST_STATE_UP) {
+                       /* Don't transmit unless it's up */
+                       return &f;
+               }
                f.frametype = AST_FRAME_VOICE;
                f.subclass = AST_FORMAT_SLINEAR;
-               f.timelen = FRAME_SIZE / 8;
+               f.samples = FRAME_SIZE;
                f.datalen = FRAME_SIZE * 2;
                f.data = buf + AST_FRIENDLY_OFFSET;
                f.offset = AST_FRIENDLY_OFFSET;
@@ -693,12 +694,11 @@ static int oss_indicate(struct ast_channel *chan, int cond)
 static struct ast_channel *oss_new(struct chan_oss_pvt *p, int state)
 {
        struct ast_channel *tmp;
-       tmp = ast_channel_alloc();
+       tmp = ast_channel_alloc(1);
        if (tmp) {
                snprintf(tmp->name, sizeof(tmp->name), "OSS/%s", DEV_DSP + 5);
                tmp->type = type;
                tmp->fds[0] = sounddev;
-               tmp->fds[1] = cmd[0];
                tmp->nativeformats = AST_FORMAT_SLINEAR;
                tmp->pvt->pvt = p;
                tmp->pvt->send_digit = oss_digit;
@@ -711,16 +711,16 @@ static struct ast_channel *oss_new(struct chan_oss_pvt *p, int state)
                tmp->pvt->indicate = oss_indicate;
                tmp->pvt->fixup = oss_fixup;
                if (strlen(p->context))
-                       strncpy(tmp->context, p->context, sizeof(tmp->context));
+                       strncpy(tmp->context, p->context, sizeof(tmp->context)-1);
                if (strlen(p->exten))
-                       strncpy(tmp->exten, p->exten, sizeof(tmp->exten));
+                       strncpy(tmp->exten, p->exten, sizeof(tmp->exten)-1);
                if (strlen(language))
-                       strncpy(tmp->language, language, sizeof(tmp->language));
+                       strncpy(tmp->language, language, sizeof(tmp->language)-1);
                p->owner = tmp;
-               tmp->state = state;
-               ast_pthread_mutex_lock(&usecnt_lock);
+               ast_setstate(tmp, state);
+               ast_mutex_lock(&usecnt_lock);
                usecnt++;
-               ast_pthread_mutex_unlock(&usecnt_lock);
+               ast_mutex_unlock(&usecnt_lock);
                ast_update_use_count();
                if (state != AST_STATE_DOWN) {
                        if (ast_pbx_start(tmp)) {
@@ -797,6 +797,7 @@ static char autoanswer_usage[] =
 
 static int console_answer(int fd, int argc, char *argv[])
 {
+       struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
        if (argc != 1)
                return RESULT_SHOWUSAGE;
        if (!oss.owner) {
@@ -805,7 +806,7 @@ static int console_answer(int fd, int argc, char *argv[])
        }
        hookstate = 1;
        cursound = -1;
-       needanswer++;
+       ast_queue_frame(oss.owner, &f, 1);
        answer_sound();
        return RESULT_SUCCESS;
 }
@@ -816,8 +817,10 @@ static char sendtext_usage[] =
 
 static int console_sendtext(int fd, int argc, char *argv[])
 {
-       int tmparg = 1;
-       if (argc < 1)
+       int tmparg = 2;
+       char text2send[256];
+       struct ast_frame f = { 0, };
+       if (argc < 2)
                return RESULT_SHOWUSAGE;
        if (!oss.owner) {
                ast_cli(fd, "No one is calling us\n");
@@ -826,11 +829,17 @@ static int console_sendtext(int fd, int argc, char *argv[])
        if (strlen(text2send))
                ast_cli(fd, "Warning: message already waiting to be sent, overwriting\n");
        strcpy(text2send, "");
-       while(tmparg <= argc) {
+       while(tmparg < argc) {
                strncat(text2send, argv[tmparg++], sizeof(text2send) - strlen(text2send));
                strncat(text2send, " ", sizeof(text2send) - strlen(text2send));
        }
-       needanswer++;
+       if (strlen(text2send)) {
+               f.frametype = AST_FRAME_TEXT;
+               f.subclass = 0;
+               f.data = text2send;
+               f.datalen = strlen(text2send);
+               ast_queue_frame(oss.owner, &f, 1);
+       }
        return RESULT_SUCCESS;
 }
 
@@ -848,8 +857,9 @@ static int console_hangup(int fd, int argc, char *argv[])
                return RESULT_FAILURE;
        }
        hookstate = 0;
-       if (oss.owner)
-               needhangup++;
+       if (oss.owner) {
+               ast_queue_hangup(oss.owner, 1);
+       }
        return RESULT_SUCCESS;
 }
 
@@ -862,14 +872,16 @@ static int console_dial(int fd, int argc, char *argv[])
 {
        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 (oss.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<strlen(argv[1]);x++) {
+                               f.subclass = argv[1][x];
+                               ast_queue_frame(oss.owner, &f, 1);
+                       }
                } else {
                        ast_cli(fd, "You're already in a call.  You can use this only to dial digits until you hangup\n");
                        return RESULT_FAILURE;
@@ -879,19 +891,21 @@ static int console_dial(int fd, int argc, char *argv[])
        mye = exten;
        myc = context;
        if (argc == 2) {
-               strncpy(tmp, argv[1], sizeof(tmp));
-               strtok(tmp, "@");
-               tmp2 = strtok(NULL, "@");
+               char *stringp=NULL;
+               strncpy(tmp, argv[1], sizeof(tmp)-1);
+               stringp=tmp;
+               strsep(&stringp, "@");
+               tmp2 = strsep(&stringp, "@");
                if (strlen(tmp))
                        mye = tmp;
                if (tmp2 && strlen(tmp2))
                        myc = tmp2;
        }
-       if (ast_exists_extension(NULL, myc, mye, 1)) {
-               strncpy(oss.exten, mye, sizeof(oss.exten));
-               strncpy(oss.context, myc, sizeof(oss.context));
+       if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
+               strncpy(oss.exten, mye, sizeof(oss.exten)-1);
+               strncpy(oss.context, myc, sizeof(oss.context)-1);
                hookstate = 1;
-               oss_new(&oss, AST_STATE_UP);
+               oss_new(&oss, AST_STATE_RINGING);
        } else
                ast_cli(fd, "No such extension '%s' in context '%s'\n", mye, myc);
        return RESULT_SUCCESS;
@@ -901,12 +915,45 @@ static char dial_usage[] =
 "Usage: dial [extension[@context]]\n"
 "       Dials a given extensison (";
 
+static int console_transfer(int fd, int argc, char *argv[])
+{
+       char tmp[256];
+       char *context;
+       if (argc != 2)
+               return RESULT_SHOWUSAGE;
+       if (oss.owner && oss.owner->bridge) {
+               strncpy(tmp, argv[1], sizeof(tmp) - 1);
+               context = strchr(tmp, '@');
+               if (context) {
+                       *context = '\0';
+                       context++;
+               } else
+                       context = oss.owner->context;
+               if (ast_exists_extension(oss.owner->bridge, context, tmp, 1, oss.owner->bridge->callerid)) {
+                       ast_cli(fd, "Whee, transferring %s to %s@%s.\n", 
+                                       oss.owner->bridge->name, tmp, context);
+                       if (ast_async_goto(oss.owner->bridge, context, tmp, 1, 1))
+                               ast_cli(fd, "Failed to transfer :(\n");
+               } else {
+                       ast_cli(fd, "No such extension exists\n");
+               }
+       } else {
+               ast_cli(fd, "There is no call to transfer\n");
+       }
+       return RESULT_SUCCESS;
+}
+
+static char transfer_usage[] =
+"Usage: transfer <extension>[@context]\n"
+"       Transfers the currently connected call to the given extension (and\n"
+"context if specified)\n";
 
 static struct ast_cli_entry myclis[] = {
        { { "answer", NULL }, console_answer, "Answer an incoming console call", answer_usage },
        { { "hangup", NULL }, console_hangup, "Hangup a call on the console", hangup_usage },
        { { "dial", NULL }, console_dial, "Dial an extension on the console", dial_usage },
-       { { "send text", NULL }, console_sendtext, "Send text to the remote device", sendtext_usage },
+       { { "transfer", NULL }, console_transfer, "Transfer a call to a different extension", transfer_usage },
+       { { "send", "text", NULL }, console_sendtext, "Send text to the remote device", sendtext_usage },
        { { "autoanswer", NULL }, console_autoanswer, "Sets/displays autoanswer", autoanswer_usage, autoanswer_complete }
 };
 
@@ -914,23 +961,15 @@ int load_module()
 {
        int res;
        int x;
-       int flags;
-       struct ast_config *cfg = ast_load(config);
+       struct ast_config *cfg;
        struct ast_variable *v;
-       res = pipe(cmd);
        res = pipe(sndcmd);
        if (res) {
                ast_log(LOG_ERROR, "Unable to create pipe\n");
                return -1;
        }
-       flags = fcntl(cmd[0], F_GETFL);
-       fcntl(cmd[0], F_SETFL, flags | O_NONBLOCK);
-       flags = fcntl(cmd[1], F_GETFL);
-       fcntl(cmd[1], F_SETFL, flags | O_NONBLOCK);
        res = soundcard_init();
        if (res < 0) {
-               close(cmd[1]);
-               close(cmd[0]);
                if (option_verbose > 1) {
                        ast_verbose(VERBOSE_PREFIX_2 "No sound card detected -- console channel will be unavailable\n");
                        ast_verbose(VERBOSE_PREFIX_2 "Turn off OSS support by adding 'noload=chan_oss.so' in /etc/asterisk/modules.conf\n");
@@ -946,7 +985,7 @@ int load_module()
        }
        for (x=0;x<sizeof(myclis)/sizeof(struct ast_cli_entry); x++)
                ast_cli_register(myclis + x);
-       if (cfg) {
+       if ((cfg = ast_load(config))) {
                v = ast_variable_browse(cfg, "general");
                while(v) {
                        if (!strcasecmp(v->name, "autoanswer"))
@@ -956,11 +995,11 @@ int load_module()
                        else if (!strcasecmp(v->name, "silencethreshold"))
                                silencethreshold = atoi(v->value);
                        else if (!strcasecmp(v->name, "context"))
-                               strncpy(context, v->value, sizeof(context));
+                               strncpy(context, v->value, sizeof(context)-1);
                        else if (!strcasecmp(v->name, "language"))
-                               strncpy(language, v->value, sizeof(language));
+                               strncpy(language, v->value, sizeof(language)-1);
                        else if (!strcasecmp(v->name, "extension"))
-                               strncpy(exten, v->value, sizeof(exten));
+                               strncpy(exten, v->value, sizeof(exten)-1);
                        v=v->next;
                }
                ast_destroy(cfg);
@@ -977,16 +1016,12 @@ int unload_module()
        for (x=0;x<sizeof(myclis)/sizeof(struct ast_cli_entry); x++)
                ast_cli_unregister(myclis + x);
        close(sounddev);
-       if (cmd[0] > 0) {
-               close(cmd[0]);
-               close(cmd[1]);
-       }
        if (sndcmd[0] > 0) {
                close(sndcmd[0]);
                close(sndcmd[1]);
        }
        if (oss.owner)
-               ast_softhangup(oss.owner);
+               ast_softhangup(oss.owner, AST_SOFTHANGUP_APPUNLOAD);
        if (oss.owner)
                return -1;
        return 0;
@@ -1000,9 +1035,9 @@ char *description()
 int usecount()
 {
        int res;
-       ast_pthread_mutex_lock(&usecnt_lock);
+       ast_mutex_lock(&usecnt_lock);
        res = usecnt;
-       ast_pthread_mutex_unlock(&usecnt_lock);
+       ast_mutex_unlock(&usecnt_lock);
        return res;
 }