Version 0.1.1 from FTP
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@137 f38db490-d61c-443f-a65b-d21fe96a405b
This commit is contained in:
parent
cb85ba7522
commit
78f58dab5c
|
@ -0,0 +1,473 @@
|
||||||
|
/*
|
||||||
|
* Asterisk -- A telephony toolkit for Linux.
|
||||||
|
*
|
||||||
|
* A/Open ITU-56/2 Voice Modem Driver (Rockwell, IS-101, and others)
|
||||||
|
*
|
||||||
|
* Copyright (C) 1999, Mark Spencer
|
||||||
|
*
|
||||||
|
* Mark Spencer <markster@linux-support.net>
|
||||||
|
*
|
||||||
|
* This program is free software, distributed under the terms of
|
||||||
|
* the GNU General Public License
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <asterisk/vmodem.h>
|
||||||
|
#include <asterisk/module.h>
|
||||||
|
#include <asterisk/frame.h>
|
||||||
|
#include <asterisk/logger.h>
|
||||||
|
#include <asterisk/options.h>
|
||||||
|
|
||||||
|
#define STATE_COMMAND 0
|
||||||
|
#define STATE_VOICE 1
|
||||||
|
|
||||||
|
#define VRA "40" /* Number of 100ms of non-ring after a ring cadence after which we consider the lien to be answered */
|
||||||
|
#define VRN "100" /* Number of 100ms of non-ring with no cadence after which we assume an answer */
|
||||||
|
|
||||||
|
static char *breakcmd = "\0x10\0x03";
|
||||||
|
|
||||||
|
static char *desc = "A/Open (Rockwell Chipset) ITU-2 VoiceModem Driver";
|
||||||
|
|
||||||
|
int usecnt;
|
||||||
|
pthread_mutex_t usecnt_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
|
||||||
|
static char *aopen_idents[] = {
|
||||||
|
/* Identify A/Open Modem */
|
||||||
|
"V2.210-V90_2M_DLP",
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
|
static int aopen_setdev(struct ast_modem_pvt *p, int dev)
|
||||||
|
{
|
||||||
|
char cmd[80];
|
||||||
|
if (ast_modem_send(p, "AT#VLS?", 0)) {
|
||||||
|
ast_log(LOG_WARNING, "Unable to select current mode %d\n", dev);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (ast_modem_read_response(p, 5)) {
|
||||||
|
ast_log(LOG_WARNING, "Unable to select device %d\n", dev);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
ast_modem_trim(p->response);
|
||||||
|
strncpy(cmd, p->response, sizeof(cmd));
|
||||||
|
if (ast_modem_expect(p, "OK", 5)) {
|
||||||
|
ast_log(LOG_WARNING, "Modem did not respond properly\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (dev == atoi(cmd)) {
|
||||||
|
/* We're already in the right mode, don't bother changing for fear of
|
||||||
|
hanging up */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
snprintf(cmd, sizeof(cmd), "AT#VLS=%d", dev);
|
||||||
|
if (ast_modem_send(p, cmd, 0)) {
|
||||||
|
ast_log(LOG_WARNING, "Unable to select device %d\n", dev);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (ast_modem_read_response(p, 5)) {
|
||||||
|
ast_log(LOG_WARNING, "Unable to select device %d\n", dev);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
ast_modem_trim(p->response);
|
||||||
|
if (strcasecmp(p->response, "VCON") && strcasecmp(p->response, "OK")) {
|
||||||
|
ast_log(LOG_WARNING, "Unexpected reply: %s\n", p->response);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int aopen_startrec(struct ast_modem_pvt *p)
|
||||||
|
{
|
||||||
|
if (ast_modem_send(p, "AT#VRX", 0) ||
|
||||||
|
ast_modem_expect(p, "CONNECT", 5)) {
|
||||||
|
ast_log(LOG_WARNING, "Unable to start recording\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
p->ministate = STATE_VOICE;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int aopen_break(struct ast_modem_pvt *p)
|
||||||
|
{
|
||||||
|
if (ast_modem_send(p, "\r\n", 2)) {
|
||||||
|
ast_log(LOG_WARNING, "Failed to send enter?\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (ast_modem_send(p, breakcmd, 2)) {
|
||||||
|
ast_log(LOG_WARNING, "Failed to break\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (ast_modem_send(p, "\r\n", 2)) {
|
||||||
|
ast_log(LOG_WARNING, "Failed to send enter?\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
/* Read any outstanding junk */
|
||||||
|
while(!ast_modem_read_response(p, 1));
|
||||||
|
if (ast_modem_send(p, "AT", 0)) {
|
||||||
|
/* Modem might be stuck in some weird mode, try to get it out */
|
||||||
|
ast_modem_send(p, "+++", 3);
|
||||||
|
if (ast_modem_expect(p, "OK", 10)) {
|
||||||
|
ast_log(LOG_WARNING, "Modem is not responding\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (ast_modem_send(p, "AT", 0)) {
|
||||||
|
ast_log(LOG_WARNING, "Modem is not responding\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (ast_modem_expect(p, "OK", 5)) {
|
||||||
|
ast_log(LOG_WARNING, "Modem did not respond properly\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int aopen_init(struct ast_modem_pvt *p)
|
||||||
|
{
|
||||||
|
if (option_debug)
|
||||||
|
ast_log(LOG_DEBUG, "aopen_init()\n");
|
||||||
|
if (aopen_break(p))
|
||||||
|
return -1;
|
||||||
|
/* Force into command mode */
|
||||||
|
p->ministate = STATE_COMMAND;
|
||||||
|
if (ast_modem_send(p, "AT#BDR=0", 0) ||
|
||||||
|
ast_modem_expect(p, "OK", 5)) {
|
||||||
|
ast_log(LOG_WARNING, "Unable to set to auto-baud\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (ast_modem_send(p, "AT#CLS=8", 0) ||
|
||||||
|
ast_modem_expect(p, "OK", 5)) {
|
||||||
|
ast_log(LOG_WARNING, "Unable to set to voice mode\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (ast_modem_send(p, "AT#VBS=8", 0) ||
|
||||||
|
ast_modem_expect(p, "OK", 5)) {
|
||||||
|
ast_log(LOG_WARNING, "Unable to set to 8-bit mode\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (ast_modem_send(p, "AT#VSR=8000", 0) ||
|
||||||
|
ast_modem_expect(p, "OK", 5)) {
|
||||||
|
ast_log(LOG_WARNING, "Unable to set to 8000 Hz sampling\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (ast_modem_send(p, "AT#VLS=0", 0) ||
|
||||||
|
ast_modem_expect(p, "OK", 5)) {
|
||||||
|
ast_log(LOG_WARNING, "Unable to set to telco interface\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (ast_modem_send(p, "AT#VRA=" VRA, 0) ||
|
||||||
|
ast_modem_expect(p, "OK", 5)) {
|
||||||
|
ast_log(LOG_WARNING, "Unable to set to 'ringback goes away' timer\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (ast_modem_send(p, "AT#VRN=" VRN, 0) ||
|
||||||
|
ast_modem_expect(p, "OK", 5)) {
|
||||||
|
ast_log(LOG_WARNING, "Unable to set to 'ringback never came timer'\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (ast_modem_send(p, "AT#VTD=3F,3F,3F", 0) ||
|
||||||
|
ast_modem_expect(p, "OK", 5)) {
|
||||||
|
ast_log(LOG_WARNING, "Unable to set to tone detection\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct ast_frame *aopen_handle_escape(struct ast_modem_pvt *p, char esc)
|
||||||
|
{
|
||||||
|
/* Handle escaped characters -- but sometimes we call it directly as
|
||||||
|
a quick way to cause known responses */
|
||||||
|
p->fr.frametype = AST_FRAME_NULL;
|
||||||
|
p->fr.subclass = 0;
|
||||||
|
p->fr.data = NULL;
|
||||||
|
p->fr.datalen = 0;
|
||||||
|
p->fr.timelen = 0;
|
||||||
|
p->fr.offset = 0;
|
||||||
|
p->fr.mallocd = 0;
|
||||||
|
if (esc)
|
||||||
|
ast_log(LOG_DEBUG, "Escaped character '%c'\n", esc);
|
||||||
|
|
||||||
|
switch(esc) {
|
||||||
|
case 'X': /* Pseudo connect */
|
||||||
|
p->fr.frametype = AST_FRAME_CONTROL;
|
||||||
|
p->fr.subclass = AST_CONTROL_ANSWER;
|
||||||
|
if (p->owner)
|
||||||
|
p->owner->state = AST_STATE_UP;
|
||||||
|
if (aopen_startrec(p))
|
||||||
|
return NULL;
|
||||||
|
return &p->fr;
|
||||||
|
case 'b': /* Busy signal */
|
||||||
|
p->fr.frametype = AST_FRAME_CONTROL;
|
||||||
|
p->fr.subclass = AST_CONTROL_BUSY;
|
||||||
|
return &p->fr;
|
||||||
|
case 'o': /* Overrun */
|
||||||
|
ast_log(LOG_WARNING, "Overflow on modem, flushing buffers\n");
|
||||||
|
if (ast_modem_send(p, "\0x10E", 2))
|
||||||
|
ast_log(LOG_WARNING, "Unable to flush buffers\n");
|
||||||
|
return &p->fr;
|
||||||
|
case 'u': /* Underrun */
|
||||||
|
ast_log(LOG_WARNING, "Data underrun\n");
|
||||||
|
/* Fall Through */
|
||||||
|
case CHAR_ETX: /* End Transmission */
|
||||||
|
case 'd': /* Dialtone */
|
||||||
|
case 'c': /* Calling Tone */
|
||||||
|
case 'e': /* European version */
|
||||||
|
case 'a': /* Answer Tone */
|
||||||
|
case 'f': /* Bell Answer Tone */
|
||||||
|
case 'T': /* Timing mark */
|
||||||
|
case 't': /* Handset off hook */
|
||||||
|
case 'h': /* Handset hungup */
|
||||||
|
case 0: /* Pseudo signal */
|
||||||
|
/* Ignore */
|
||||||
|
return &p->fr;
|
||||||
|
default:
|
||||||
|
ast_log(LOG_DEBUG, "Unknown Escaped character '%c' (%d)\n", esc, esc);
|
||||||
|
}
|
||||||
|
return &p->fr;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct ast_frame *aopen_read(struct ast_modem_pvt *p)
|
||||||
|
{
|
||||||
|
char result[256];
|
||||||
|
short *b;
|
||||||
|
struct ast_frame *f=NULL;
|
||||||
|
int res;
|
||||||
|
int x;
|
||||||
|
if (p->ministate == STATE_COMMAND) {
|
||||||
|
/* Read the first two bytes, first, in case it's a control message */
|
||||||
|
fread(result, 1, 2, p->f);
|
||||||
|
if (result[0] == CHAR_DLE) {
|
||||||
|
return aopen_handle_escape(p, result[1]);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
if ((result[0] == '\n') || (result[0] == '\r'))
|
||||||
|
return aopen_handle_escape(p, 0);
|
||||||
|
/* Read the rest of the line */
|
||||||
|
fgets(result + 2, sizeof(result) - 2, p->f);
|
||||||
|
ast_modem_trim(result);
|
||||||
|
if (!strcasecmp(result, "VCON")) {
|
||||||
|
/* If we're in immediate mode, reply now */
|
||||||
|
if (p->mode == MODEM_MODE_IMMEDIATE)
|
||||||
|
return aopen_handle_escape(p, 'X');
|
||||||
|
}
|
||||||
|
if (!strcasecmp(result, "BUSY")) {
|
||||||
|
/* Same as a busy signal */
|
||||||
|
return aopen_handle_escape(p, 'b');
|
||||||
|
}
|
||||||
|
if (!strcasecmp(result, "NO DIALTONE")) {
|
||||||
|
/* There's no dialtone, so the line isn't working */
|
||||||
|
ast_log(LOG_WARNING, "Device '%s' lacking dialtone\n", p->dev);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
ast_log(LOG_DEBUG, "Modem said '%s'\n", result);
|
||||||
|
return aopen_handle_escape(p, 0);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
/* We have to be more efficient in voice mode */
|
||||||
|
b = (short *)(p->obuf + p->obuflen);
|
||||||
|
while (p->obuflen/2 < 240) {
|
||||||
|
/* Read ahead the full amount */
|
||||||
|
res = fread(result, 1, 240 - p->obuflen/2, p->f);
|
||||||
|
if (res < 1) {
|
||||||
|
/* If there's nothing there, just continue on */
|
||||||
|
if (errno == EAGAIN)
|
||||||
|
return aopen_handle_escape(p, 0);
|
||||||
|
ast_log(LOG_WARNING, "Read failed: %s\n", strerror(errno));
|
||||||
|
}
|
||||||
|
for (x=0;x<res;x++) {
|
||||||
|
/* Process all the bytes that we've read */
|
||||||
|
if (result[x] == CHAR_DLE) {
|
||||||
|
/* We assume there is no more than one signal frame among our
|
||||||
|
data. */
|
||||||
|
if (f)
|
||||||
|
ast_log(LOG_WARNING, "Warning: Dropped a signal frame\n");
|
||||||
|
f = aopen_handle_escape(p, result[x+1]);
|
||||||
|
/* If aopen_handle_escape says NULL, say it now, doesn't matter
|
||||||
|
what else is there, the connection is dead. */
|
||||||
|
if (!f)
|
||||||
|
return NULL;
|
||||||
|
} else {
|
||||||
|
/* Generate a 16-bit signed linear value from our
|
||||||
|
unsigned 8-bit value */
|
||||||
|
*(b++) = (((short)result[x]) - 127) * 0xff;
|
||||||
|
p->obuflen += 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (f)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
/* If we have a control frame, return it now */
|
||||||
|
if (f)
|
||||||
|
return f;
|
||||||
|
/* If we get here, we have a complete voice frame */
|
||||||
|
p->fr.frametype = AST_FRAME_VOICE;
|
||||||
|
p->fr.subclass = AST_FORMAT_SLINEAR;
|
||||||
|
p->fr.timelen = 30;
|
||||||
|
p->fr.data = p->obuf;
|
||||||
|
p->fr.datalen = p->obuflen;
|
||||||
|
p->fr.mallocd = 0;
|
||||||
|
p->fr.offset = AST_FRIENDLY_OFFSET;
|
||||||
|
p->fr.src = __FUNCTION__;
|
||||||
|
if (option_debug)
|
||||||
|
ast_log(LOG_DEBUG, "aopen_read(voice frame)\n");
|
||||||
|
p->obuflen = 0;
|
||||||
|
return &p->fr;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int aopen_write(struct ast_modem_pvt *p, struct ast_frame *f)
|
||||||
|
{
|
||||||
|
if (option_debug)
|
||||||
|
ast_log(LOG_DEBUG, "aopen_write()\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *aopen_identify(struct ast_modem_pvt *p)
|
||||||
|
{
|
||||||
|
char identity[256];
|
||||||
|
char mfr[80];
|
||||||
|
char mdl[80];
|
||||||
|
char rev[80];
|
||||||
|
ast_modem_send(p, "AT#MDL?", 0);
|
||||||
|
ast_modem_read_response(p, 5);
|
||||||
|
strncpy(mdl, p->response, sizeof(mdl));
|
||||||
|
ast_modem_trim(mdl);
|
||||||
|
ast_modem_expect(p, "OK", 5);
|
||||||
|
ast_modem_send(p, "AT#MFR?", 0);
|
||||||
|
ast_modem_read_response(p, 5);
|
||||||
|
strncpy(mfr, p->response, sizeof(mfr));
|
||||||
|
ast_modem_trim(mfr);
|
||||||
|
ast_modem_expect(p, "OK", 5);
|
||||||
|
ast_modem_send(p, "AT#REV?", 0);
|
||||||
|
ast_modem_read_response(p, 5);
|
||||||
|
strncpy(rev, p->response, sizeof(rev));
|
||||||
|
ast_modem_trim(rev);
|
||||||
|
ast_modem_expect(p, "OK", 5);
|
||||||
|
snprintf(identity, sizeof(identity), "%s Model %s Revision %s", mfr, mdl, rev);
|
||||||
|
return strdup(identity);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void aopen_incusecnt()
|
||||||
|
{
|
||||||
|
pthread_mutex_lock(&usecnt_lock);
|
||||||
|
usecnt++;
|
||||||
|
pthread_mutex_unlock(&usecnt_lock);
|
||||||
|
ast_update_use_count();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void aopen_decusecnt()
|
||||||
|
{
|
||||||
|
pthread_mutex_lock(&usecnt_lock);
|
||||||
|
usecnt++;
|
||||||
|
pthread_mutex_unlock(&usecnt_lock);
|
||||||
|
ast_update_use_count();
|
||||||
|
}
|
||||||
|
|
||||||
|
static int aopen_answer(struct ast_modem_pvt *p)
|
||||||
|
{
|
||||||
|
if (ast_modem_send(p, "ATA", 0) ||
|
||||||
|
ast_modem_expect(p, "VCON", 10)) {
|
||||||
|
ast_log(LOG_WARNING, "Unable to answer: %s", p->response);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int aopen_dialdigit(struct ast_modem_pvt *p, char digit)
|
||||||
|
{
|
||||||
|
char cmd[80];
|
||||||
|
snprintf(cmd, sizeof(cmd), "AT#VTS=%c", digit);
|
||||||
|
if (ast_modem_send(p, cmd, 0) ||
|
||||||
|
ast_modem_expect(p, "VCON", 10)) {
|
||||||
|
ast_log(LOG_WARNING, "Unable to answer: %s", p->response);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int aopen_dial(struct ast_modem_pvt *p, char *stuff)
|
||||||
|
{
|
||||||
|
char cmd[80];
|
||||||
|
snprintf(cmd, sizeof(cmd), "ATD%c %s", p->dialtype,stuff);
|
||||||
|
if (ast_modem_send(p, cmd, 0)) {
|
||||||
|
ast_log(LOG_WARNING, "Unable to dial\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int aopen_hangup(struct ast_modem_pvt *p)
|
||||||
|
{
|
||||||
|
if (aopen_break(p))
|
||||||
|
return -1;
|
||||||
|
/* Hangup by switching to data, then back to voice */
|
||||||
|
if (ast_modem_send(p, "ATH", 0) ||
|
||||||
|
ast_modem_expect(p, "OK", 8)) {
|
||||||
|
ast_log(LOG_WARNING, "Unable to set to data mode\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (ast_modem_send(p, "AT#CLS=8", 0) ||
|
||||||
|
ast_modem_expect(p, "OK", 5)) {
|
||||||
|
ast_log(LOG_WARNING, "Unable to set to voice mode\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct ast_modem_driver aopen_driver =
|
||||||
|
{
|
||||||
|
"AOpen",
|
||||||
|
aopen_idents,
|
||||||
|
AST_FORMAT_SLINEAR,
|
||||||
|
0, /* Not full duplex */
|
||||||
|
aopen_incusecnt, /* incusecnt */
|
||||||
|
aopen_decusecnt, /* decusecnt */
|
||||||
|
aopen_identify, /* identify */
|
||||||
|
aopen_init, /* init */
|
||||||
|
aopen_setdev, /* setdev */
|
||||||
|
aopen_read,
|
||||||
|
aopen_write,
|
||||||
|
aopen_dial, /* dial */
|
||||||
|
aopen_answer, /* answer */
|
||||||
|
aopen_hangup, /* hangup */
|
||||||
|
aopen_startrec, /* start record */
|
||||||
|
NULL, /* stop record */
|
||||||
|
NULL, /* start playback */
|
||||||
|
NULL, /* stop playback */
|
||||||
|
NULL, /* set silence supression */
|
||||||
|
aopen_dialdigit, /* dialdigit */
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int usecount(void)
|
||||||
|
{
|
||||||
|
int res;
|
||||||
|
pthread_mutex_lock(&usecnt_lock);
|
||||||
|
res = usecnt;
|
||||||
|
pthread_mutex_unlock(&usecnt_lock);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
int load_module(void)
|
||||||
|
{
|
||||||
|
return ast_register_modem_driver(&aopen_driver);
|
||||||
|
}
|
||||||
|
|
||||||
|
int unload_module(void)
|
||||||
|
{
|
||||||
|
return ast_unregister_modem_driver(&aopen_driver);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *description()
|
||||||
|
{
|
||||||
|
return desc;
|
||||||
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
#
|
#
|
||||||
# Makefile for PBX frontends (dynamically loaded)
|
# Makefile for PBX frontends (dynamically loaded)
|
||||||
#
|
#
|
||||||
# Copyright (C) 1999, Adtran Inc. and Linux Support Services, LLC
|
# Copyright (C) 1999, Mark Spencer
|
||||||
#
|
#
|
||||||
# Mark Spencer <markster@linux-support.net>
|
# Mark Spencer <markster@linux-support.net>
|
||||||
#
|
#
|
||||||
|
@ -27,14 +27,14 @@ LIBG723B=g723.1b/libg723b.a
|
||||||
LIBGSM=gsm/lib/libgsm.a
|
LIBGSM=gsm/lib/libgsm.a
|
||||||
LIBMP3=mp3/libmp3.a
|
LIBMP3=mp3/libmp3.a
|
||||||
|
|
||||||
CODECS+=$(MODG723) codec_gsm.so #codec_mp3_d.so
|
CODECS+=$(MODG723) codec_gsm.so codec_mp3_d.so
|
||||||
|
|
||||||
all: $(CODECS)
|
all: $(CODECS)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f *.so *.o
|
rm -f *.so *.o
|
||||||
make -C g723.1 clean
|
! [ -d g723.1 ] || make -C g723.1 clean
|
||||||
make -C g723.1b clean
|
! [ -d g723.1b ] || make -C g723.1b clean
|
||||||
make -C gsm clean
|
make -C gsm clean
|
||||||
make -C mp3 clean
|
make -C mp3 clean
|
||||||
|
|
||||||
|
|
63
translate.c
63
translate.c
|
@ -3,7 +3,7 @@
|
||||||
*
|
*
|
||||||
* Translate via the use of pseudo channels
|
* Translate via the use of pseudo channels
|
||||||
*
|
*
|
||||||
* Copyright (C) 1999, Adtran Inc. and Linux Support Services, LLC
|
* Copyright (C) 1999, Mark Spencer
|
||||||
*
|
*
|
||||||
* Mark Spencer <markster@linux-support.net>
|
* Mark Spencer <markster@linux-support.net>
|
||||||
*
|
*
|
||||||
|
@ -58,6 +58,8 @@ struct translator_pvt {
|
||||||
int comm[2];
|
int comm[2];
|
||||||
struct ast_trans_pvt *system;
|
struct ast_trans_pvt *system;
|
||||||
struct ast_trans_pvt *rsystem;
|
struct ast_trans_pvt *rsystem;
|
||||||
|
struct timeval lastpass;
|
||||||
|
pthread_t threadid;
|
||||||
};
|
};
|
||||||
|
|
||||||
static int translator_hangup(struct ast_channel *chan)
|
static int translator_hangup(struct ast_channel *chan)
|
||||||
|
@ -155,7 +157,6 @@ struct ast_trans_pvt *ast_translator_build_path(int source, int dest)
|
||||||
|
|
||||||
static struct ast_frame *fd_read(int fd)
|
static struct ast_frame *fd_read(int fd)
|
||||||
{
|
{
|
||||||
/* XXX Wrong: Not thread safe! XXX */
|
|
||||||
char buf[4096];
|
char buf[4096];
|
||||||
int res;
|
int res;
|
||||||
struct ast_frame *f = (struct ast_frame *)buf;
|
struct ast_frame *f = (struct ast_frame *)buf;
|
||||||
|
@ -166,8 +167,13 @@ static struct ast_frame *fd_read(int fd)
|
||||||
== sizeof(struct ast_frame)) {
|
== sizeof(struct ast_frame)) {
|
||||||
/* read the frame header */
|
/* read the frame header */
|
||||||
f->mallocd = 0;
|
f->mallocd = 0;
|
||||||
|
/* Re-write data position */
|
||||||
f->data = buf + sizeof(struct ast_frame) + AST_FRIENDLY_OFFSET;
|
f->data = buf + sizeof(struct ast_frame) + AST_FRIENDLY_OFFSET;
|
||||||
f->offset = AST_FRIENDLY_OFFSET;
|
f->offset = AST_FRIENDLY_OFFSET;
|
||||||
|
/* Forget about being mallocd */
|
||||||
|
f->mallocd = 0;
|
||||||
|
/* Re-write the source */
|
||||||
|
f->src = __FUNCTION__;
|
||||||
if (f->datalen > sizeof(buf) - sizeof(struct ast_frame) - AST_FRIENDLY_OFFSET) {
|
if (f->datalen > sizeof(buf) - sizeof(struct ast_frame) - AST_FRIENDLY_OFFSET) {
|
||||||
/* Really bad read */
|
/* Really bad read */
|
||||||
ast_log(LOG_WARNING, "Strange read (%d bytes)\n", f->datalen);
|
ast_log(LOG_WARNING, "Strange read (%d bytes)\n", f->datalen);
|
||||||
|
@ -250,13 +256,14 @@ struct ast_frame_chain *ast_translate(struct ast_trans_pvt *path, struct ast_fra
|
||||||
return outc;
|
return outc;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* XXX There's an experimentally derived fudge factor XXX */
|
#define FUDGE 2
|
||||||
#define FUDGE 4
|
|
||||||
|
|
||||||
static void translator_apply(struct ast_trans_pvt *path, struct ast_frame *f, int fd, struct ast_channel *c)
|
static void translator_apply(struct ast_trans_pvt *path, struct ast_frame *f, int fd, struct ast_channel *c, struct timeval *last)
|
||||||
{
|
{
|
||||||
struct ast_trans_pvt *p;
|
struct ast_trans_pvt *p;
|
||||||
struct ast_frame *out;
|
struct ast_frame *out;
|
||||||
|
struct timeval tv;
|
||||||
|
int ms;
|
||||||
p = path;
|
p = path;
|
||||||
/* Feed the first frame into the first translator */
|
/* Feed the first frame into the first translator */
|
||||||
p->step->framein(p->state, f);
|
p->step->framein(p->state, f);
|
||||||
|
@ -267,11 +274,22 @@ static void translator_apply(struct ast_trans_pvt *path, struct ast_frame *f, in
|
||||||
/* Feed to next layer */
|
/* Feed to next layer */
|
||||||
p->next->step->framein(p->next->state, out);
|
p->next->step->framein(p->next->state, out);
|
||||||
} else {
|
} else {
|
||||||
|
/* Delay if needed */
|
||||||
|
if (last->tv_sec || last->tv_usec) {
|
||||||
|
gettimeofday(&tv, NULL);
|
||||||
|
ms = 1000 * (tv.tv_sec - last->tv_sec) +
|
||||||
|
(tv.tv_usec - last->tv_usec) / 1000;
|
||||||
|
if (ms + FUDGE < out->timelen)
|
||||||
|
usleep((out->timelen - ms - FUDGE) * 1000);
|
||||||
|
last->tv_sec = tv.tv_sec;
|
||||||
|
last->tv_usec = tv.tv_usec;
|
||||||
|
}
|
||||||
if (c)
|
if (c)
|
||||||
ast_write(c, out);
|
ast_write(c, out);
|
||||||
else
|
else
|
||||||
fd_write(fd, out);
|
fd_write(fd, out);
|
||||||
}
|
}
|
||||||
|
ast_frfree(out);
|
||||||
}
|
}
|
||||||
p = p->next;
|
p = p->next;
|
||||||
}
|
}
|
||||||
|
@ -288,6 +306,8 @@ static void *translator_thread(void *data)
|
||||||
int res;
|
int res;
|
||||||
/* Read from the real, translate, write as necessary to the fake */
|
/* Read from the real, translate, write as necessary to the fake */
|
||||||
for(;;) {
|
for(;;) {
|
||||||
|
/* Break here if need be */
|
||||||
|
pthread_testcancel();
|
||||||
if (!real->trans) {
|
if (!real->trans) {
|
||||||
ast_log(LOG_WARNING, "No translator anymore\n");
|
ast_log(LOG_WARNING, "No translator anymore\n");
|
||||||
break;
|
break;
|
||||||
|
@ -299,6 +319,8 @@ static void *translator_thread(void *data)
|
||||||
CHECK_BLOCKING(real);
|
CHECK_BLOCKING(real);
|
||||||
res = ast_waitfor_n_fd(fds, 2, &ms);
|
res = ast_waitfor_n_fd(fds, 2, &ms);
|
||||||
real->blocking = 0;
|
real->blocking = 0;
|
||||||
|
/* Or we can die here, that's fine too */
|
||||||
|
pthread_testcancel();
|
||||||
if (res >= 0) {
|
if (res >= 0) {
|
||||||
if (res == real->fd) {
|
if (res == real->fd) {
|
||||||
f = ast_read(real);
|
f = ast_read(real);
|
||||||
|
@ -309,7 +331,7 @@ static void *translator_thread(void *data)
|
||||||
}
|
}
|
||||||
if (f->frametype == AST_FRAME_VOICE) {
|
if (f->frametype == AST_FRAME_VOICE) {
|
||||||
if (pvt->system)
|
if (pvt->system)
|
||||||
translator_apply(pvt->system, f, fd, NULL);
|
translator_apply(pvt->system, f, fd, NULL, &pvt->lastpass);
|
||||||
} else {
|
} else {
|
||||||
/* If it's not voice, just pass it along */
|
/* If it's not voice, just pass it along */
|
||||||
fd_write(fd, f);
|
fd_write(fd, f);
|
||||||
|
@ -322,9 +344,10 @@ static void *translator_thread(void *data)
|
||||||
ast_log(LOG_DEBUG, "Empty (hangup) frame\n");
|
ast_log(LOG_DEBUG, "Empty (hangup) frame\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (f->frametype == AST_FRAME_VOICE) {
|
if (f->frametype == AST_FRAME_VOICE) {
|
||||||
if (pvt->rsystem)
|
if (pvt->rsystem)
|
||||||
translator_apply(pvt->rsystem, f, -1, real);
|
translator_apply(pvt->rsystem, f, -1, real, &pvt->lastpass);
|
||||||
} else {
|
} else {
|
||||||
ast_write(real, f);
|
ast_write(real, f);
|
||||||
}
|
}
|
||||||
|
@ -349,7 +372,6 @@ struct ast_channel *ast_translator_create(struct ast_channel *real, int format,
|
||||||
{
|
{
|
||||||
struct ast_channel *tmp;
|
struct ast_channel *tmp;
|
||||||
struct translator_pvt *pvt;
|
struct translator_pvt *pvt;
|
||||||
pthread_t t;
|
|
||||||
if (real->trans) {
|
if (real->trans) {
|
||||||
ast_log(LOG_WARNING, "Translator already exists on '%s'\n", real->name);
|
ast_log(LOG_WARNING, "Translator already exists on '%s'\n", real->name);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -360,6 +382,8 @@ struct ast_channel *ast_translator_create(struct ast_channel *real, int format,
|
||||||
}
|
}
|
||||||
pvt->comm[0] = -1;
|
pvt->comm[0] = -1;
|
||||||
pvt->comm[1] = -1;
|
pvt->comm[1] = -1;
|
||||||
|
pvt->lastpass.tv_usec = 0;
|
||||||
|
pvt->lastpass.tv_sec = 0;
|
||||||
if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pvt->comm)) {
|
if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pvt->comm)) {
|
||||||
ast_log(LOG_WARNING, "Unable to create UNIX domain socket on '%s'\n", real->name);
|
ast_log(LOG_WARNING, "Unable to create UNIX domain socket on '%s'\n", real->name);
|
||||||
ast_translator_free(pvt);
|
ast_translator_free(pvt);
|
||||||
|
@ -404,10 +428,11 @@ struct ast_channel *ast_translator_create(struct ast_channel *real, int format,
|
||||||
tmp->pvt->answer = translator_answer;
|
tmp->pvt->answer = translator_answer;
|
||||||
tmp->pvt->read = translator_read;
|
tmp->pvt->read = translator_read;
|
||||||
tmp->pvt->write = translator_write;
|
tmp->pvt->write = translator_write;
|
||||||
|
tmp->pvt->pvt = pvt;
|
||||||
real->trans = tmp;
|
real->trans = tmp;
|
||||||
if (option_verbose > 2)
|
if (option_verbose > 2)
|
||||||
ast_verbose(VERBOSE_PREFIX_3 "Created translator %s\n", tmp->name);
|
ast_verbose(VERBOSE_PREFIX_3 "Created translator %s\n", tmp->name);
|
||||||
if (pthread_create(&t, NULL, translator_thread, real) < 0) {
|
if (pthread_create(&pvt->threadid, NULL, translator_thread, real) < 0) {
|
||||||
ast_translator_destroy(tmp);
|
ast_translator_destroy(tmp);
|
||||||
tmp = NULL;
|
tmp = NULL;
|
||||||
ast_log(LOG_WARNING, "Failed to start thread\n");
|
ast_log(LOG_WARNING, "Failed to start thread\n");
|
||||||
|
@ -550,8 +575,7 @@ int ast_unregister_translator(struct ast_translator *t)
|
||||||
|
|
||||||
void ast_translator_destroy(struct ast_channel *trans)
|
void ast_translator_destroy(struct ast_channel *trans)
|
||||||
{
|
{
|
||||||
char dummy;
|
struct translator_pvt *pvt;
|
||||||
int ms = 1000;
|
|
||||||
if (!trans->master) {
|
if (!trans->master) {
|
||||||
ast_log(LOG_WARNING, "Translator is not part of a real channel?\n");
|
ast_log(LOG_WARNING, "Translator is not part of a real channel?\n");
|
||||||
return;
|
return;
|
||||||
|
@ -561,19 +585,14 @@ void ast_translator_destroy(struct ast_channel *trans)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
trans->master->trans = NULL;
|
trans->master->trans = NULL;
|
||||||
/* Write an invalid frame to kill off the main thread, which will
|
pvt = trans->pvt->pvt;
|
||||||
in turn acknowledge by writing an invalid frame back to us, unless
|
/* Cancel the running translator thread */
|
||||||
they're already closed. */
|
pthread_cancel(pvt->threadid);
|
||||||
if (trans->fd > -1) {
|
pthread_join(pvt->threadid, NULL);
|
||||||
if (write(trans->fd, trans, 1) == 1) {
|
ast_translator_free(pvt);
|
||||||
/* Wait for a respnose, but no more than 1 second */
|
trans->pvt->pvt = NULL;
|
||||||
if (ast_waitfor_n_fd(&trans->fd, 1, &ms) == trans->fd)
|
|
||||||
read(trans->fd, &dummy, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (option_verbose > 2)
|
if (option_verbose > 2)
|
||||||
ast_verbose(VERBOSE_PREFIX_3 "Destroyed translator %s\n", trans->name);
|
ast_verbose(VERBOSE_PREFIX_3 "Destroyed translator %s\n", trans->name);
|
||||||
close(trans->fd);
|
|
||||||
ast_channel_free(trans);
|
ast_channel_free(trans);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in New Issue