2006-01-03 22:13:59 +00:00
|
|
|
/*
|
|
|
|
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
*
|
|
|
|
* Version: MPL 1.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
* Portions created by the Initial Developer are Copyright (C)
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
*
|
|
|
|
*
|
2006-01-13 14:53:26 +00:00
|
|
|
* mod_iax.c -- IAX2 Endpoint Module
|
2006-01-03 22:13:59 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <switch.h>
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#include <iax2.h>
|
|
|
|
#include <iax-client.h>
|
|
|
|
#include <iax2-parser.h>
|
|
|
|
#include <sys/timeb.h>
|
|
|
|
#else
|
|
|
|
#include <iax/iax2.h>
|
|
|
|
#include <iax/iax-client.h>
|
|
|
|
#include <iax/iax2-parser.h>
|
|
|
|
#endif
|
|
|
|
|
2006-01-13 14:53:26 +00:00
|
|
|
static const char modname[] = "mod_iax";
|
2006-01-03 22:13:59 +00:00
|
|
|
|
2006-04-29 01:00:52 +00:00
|
|
|
static switch_memory_pool_t *module_pool = NULL;
|
2006-01-03 22:13:59 +00:00
|
|
|
static int running = 1;
|
|
|
|
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
TFLAG_IO = (1 << 0),
|
|
|
|
TFLAG_INBOUND = (1 << 1),
|
|
|
|
TFLAG_OUTBOUND = (1 << 2),
|
|
|
|
TFLAG_DTMF = (1 << 3),
|
|
|
|
TFLAG_VOICE = (1 << 4),
|
|
|
|
TFLAG_HANGUP = (1 << 5),
|
2006-04-18 16:20:47 +00:00
|
|
|
TFLAG_LINEAR = (1 << 6),
|
|
|
|
TFLAG_CODEC = (1 << 7)
|
2006-01-03 22:13:59 +00:00
|
|
|
} TFLAGS;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
GFLAG_MY_CODEC_PREFS = (1 << 0)
|
|
|
|
} GFLAGS;
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
int debug;
|
2006-04-13 01:05:47 +00:00
|
|
|
char *ip;
|
2006-01-03 22:13:59 +00:00
|
|
|
int port;
|
|
|
|
char *dialplan;
|
|
|
|
char *codec_string;
|
|
|
|
char *codec_order[SWITCH_MAX_CODECS];
|
|
|
|
int codec_order_last;
|
2006-01-12 17:51:08 +00:00
|
|
|
char *codec_rates_string;
|
|
|
|
char *codec_rates[SWITCH_MAX_CODECS];
|
|
|
|
int codec_rates_last;
|
2006-01-03 22:13:59 +00:00
|
|
|
unsigned int flags;
|
|
|
|
} globals;
|
|
|
|
|
|
|
|
struct private_object {
|
|
|
|
unsigned int flags;
|
2006-04-29 06:05:03 +00:00
|
|
|
switch_codec_t read_codec;
|
|
|
|
switch_codec_t write_codec;
|
|
|
|
switch_frame_t read_frame;
|
2006-01-14 16:44:52 +00:00
|
|
|
unsigned char databuf[SWITCH_RECCOMMENDED_BUFFER_SIZE];
|
2006-04-29 06:05:03 +00:00
|
|
|
switch_core_session_t *session;
|
2006-01-03 22:13:59 +00:00
|
|
|
struct iax_session *iax_session;
|
2006-04-29 06:05:03 +00:00
|
|
|
switch_caller_profile_t *caller_profile;
|
2006-01-03 22:13:59 +00:00
|
|
|
unsigned int codec;
|
|
|
|
unsigned int codecs;
|
2006-01-12 20:14:06 +00:00
|
|
|
unsigned short samprate;
|
2006-01-03 22:13:59 +00:00
|
|
|
switch_mutex_t *mutex;
|
2006-01-14 16:01:52 +00:00
|
|
|
//switch_thread_cond_t *cond;
|
2006-01-03 22:13:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_dialplan, globals.dialplan)
|
2006-04-13 01:05:47 +00:00
|
|
|
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_codec_string, globals.codec_string)
|
|
|
|
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_codec_rates_string, globals.codec_rates_string)
|
|
|
|
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_ip, globals.ip)
|
2006-01-03 22:13:59 +00:00
|
|
|
|
|
|
|
|
2006-01-20 15:05:05 +00:00
|
|
|
static char *IAXNAMES[] =
|
|
|
|
{ "IAX_EVENT_CONNECT", "IAX_EVENT_ACCEPT", "IAX_EVENT_HANGUP", "IAX_EVENT_REJECT", "IAX_EVENT_VOICE",
|
|
|
|
"IAX_EVENT_DTMF", "IAX_EVENT_TIMEOUT", "IAX_EVENT_LAGRQ", "IAX_EVENT_LAGRP", "IAX_EVENT_RINGA",
|
|
|
|
"IAX_EVENT_PING", "IAX_EVENT_PONG", "IAX_EVENT_BUSY", "IAX_EVENT_ANSWER", "IAX_EVENT_IMAGE",
|
|
|
|
"IAX_EVENT_AUTHRQ", "IAX_EVENT_AUTHRP", "IAX_EVENT_REGREQ", "IAX_EVENT_REGACK",
|
|
|
|
"IAX_EVENT_URL", "IAX_EVENT_LDCOMPLETE", "IAX_EVENT_TRANSFER", "IAX_EVENT_DPREQ",
|
|
|
|
"IAX_EVENT_DPREP", "IAX_EVENT_DIAL", "IAX_EVENT_QUELCH", "IAX_EVENT_UNQUELCH",
|
|
|
|
"IAX_EVENT_UNLINK", "IAX_EVENT_LINKREJECT", "IAX_EVENT_TEXT", "IAX_EVENT_REGREJ",
|
|
|
|
"IAX_EVENT_LINKURL", "IAX_EVENT_CNG", "IAX_EVENT_REREQUEST", "IAX_EVENT_TXREPLY",
|
|
|
|
"IAX_EVENT_TXREJECT", "IAX_EVENT_TXACCEPT", "IAX_EVENT_TXREADY"
|
|
|
|
};
|
2006-01-03 22:13:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
struct ast_iana {
|
|
|
|
const unsigned int ast;
|
|
|
|
const int iana;
|
|
|
|
char *name;
|
|
|
|
};
|
|
|
|
|
|
|
|
//999 means it's wrong nad i dont know the real one
|
2006-01-20 15:05:05 +00:00
|
|
|
static struct ast_iana AST_IANA[] = { {AST_FORMAT_G723_1, 4, "g723.1"},
|
|
|
|
{AST_FORMAT_GSM, 3, "gsm"},
|
|
|
|
{AST_FORMAT_ULAW, 0, "ulaw"},
|
|
|
|
{AST_FORMAT_ALAW, 8, "alaw"},
|
|
|
|
{AST_FORMAT_G726, 999, "g726"},
|
|
|
|
{AST_FORMAT_ADPCM, 999, "adpcm"},
|
|
|
|
{AST_FORMAT_SLINEAR, 10, "slinear"},
|
|
|
|
{AST_FORMAT_LPC10, 7, "lpc10"},
|
|
|
|
{AST_FORMAT_G729A, 18, "g729"},
|
|
|
|
{AST_FORMAT_SPEEX, 98, "speex"},
|
2006-04-19 19:04:30 +00:00
|
|
|
{AST_FORMAT_ILBC, 102, "ilbc"},
|
2006-01-20 15:05:05 +00:00
|
|
|
{AST_FORMAT_MAX_AUDIO, 999, ""},
|
|
|
|
{AST_FORMAT_JPEG, 999, ""},
|
|
|
|
{AST_FORMAT_PNG, 999, ""},
|
|
|
|
{AST_FORMAT_H261, 999, ""},
|
|
|
|
{AST_FORMAT_H263, 999, ""},
|
|
|
|
{AST_FORMAT_MAX_VIDEO, 999, ""},
|
|
|
|
{0, 0}
|
|
|
|
};
|
2006-01-03 22:13:59 +00:00
|
|
|
|
2006-02-20 06:13:56 +00:00
|
|
|
static char *ast2str(int ast)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
|
|
|
int x;
|
2006-01-20 15:05:05 +00:00
|
|
|
for (x = 0; x < 32; x++) {
|
2006-01-03 22:13:59 +00:00
|
|
|
if ((1 << x) == ast) {
|
|
|
|
return AST_IANA[x].name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int iana2ast(int iana)
|
|
|
|
{
|
|
|
|
int x = 0;
|
|
|
|
unsigned int ast = 0;
|
|
|
|
|
2006-01-20 15:05:05 +00:00
|
|
|
for (x = 0; AST_IANA[x].ast; x++) {
|
2006-01-03 22:13:59 +00:00
|
|
|
if (AST_IANA[x].iana == iana) {
|
|
|
|
ast = AST_IANA[x].ast;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ast;
|
|
|
|
}
|
|
|
|
|
2006-01-20 15:05:05 +00:00
|
|
|
static unsigned short iax_build_codec_rates(void)
|
2006-01-12 18:16:12 +00:00
|
|
|
{
|
|
|
|
int x;
|
|
|
|
unsigned short samples = 0;
|
2006-01-20 15:05:05 +00:00
|
|
|
|
2006-01-12 18:16:12 +00:00
|
|
|
for (x = 0; x < globals.codec_rates_last; x++) {
|
|
|
|
int rate = atoi(globals.codec_rates[x]);
|
|
|
|
switch (rate) {
|
|
|
|
case 8:
|
|
|
|
samples |= IAX_RATE_8KHZ;
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
samples |= IAX_RATE_16KHZ;
|
|
|
|
break;
|
|
|
|
case 22:
|
|
|
|
samples |= IAX_RATE_22KHZ;
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
samples |= IAX_RATE_32KHZ;
|
|
|
|
break;
|
|
|
|
case 44:
|
|
|
|
samples |= IAX_RATE_44KHZ;
|
|
|
|
break;
|
|
|
|
case 48:
|
|
|
|
samples |= IAX_RATE_48KHZ;
|
|
|
|
break;
|
|
|
|
default:
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "I don't know rate %d\n", rate);
|
2006-01-12 18:16:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return samples;
|
|
|
|
}
|
|
|
|
|
2006-01-03 22:13:59 +00:00
|
|
|
typedef enum {
|
|
|
|
IAX_SET = 1,
|
|
|
|
IAX_QUERY = 2
|
|
|
|
} iax_io_t;
|
|
|
|
|
2006-01-20 15:05:05 +00:00
|
|
|
static switch_status iax_set_codec(struct private_object *tech_pvt, struct iax_session *iax_session,
|
|
|
|
unsigned int *format, unsigned int *cababilities, unsigned short *samprate,
|
|
|
|
iax_io_t io)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
|
|
|
char *dname = NULL;
|
|
|
|
//int rate = 8000;
|
|
|
|
//int codec_ms = 20;
|
2006-04-29 06:05:03 +00:00
|
|
|
switch_channel_t *channel;
|
2006-01-03 22:13:59 +00:00
|
|
|
switch_codec_interface *codecs[SWITCH_MAX_CODECS];
|
|
|
|
int num_codecs = 0;
|
|
|
|
unsigned int local_cap = 0, mixed_cap = 0, chosen = 0, leading = 0;
|
2006-01-12 18:16:12 +00:00
|
|
|
int x, srate = 8000;
|
2006-01-03 22:13:59 +00:00
|
|
|
|
|
|
|
if (globals.codec_string) {
|
2006-03-30 23:02:50 +00:00
|
|
|
if ((num_codecs = switch_loadable_module_get_codecs_sorted(codecs,
|
2006-01-20 15:05:05 +00:00
|
|
|
SWITCH_MAX_CODECS,
|
|
|
|
globals.codec_order,
|
2006-02-20 06:13:56 +00:00
|
|
|
globals.codec_order_last)) <= 0) {
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "NO codecs?\n");
|
2006-01-20 15:05:05 +00:00
|
|
|
return SWITCH_STATUS_GENERR;
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
2006-01-20 15:05:05 +00:00
|
|
|
} else
|
2006-02-20 06:13:56 +00:00
|
|
|
if (((num_codecs =
|
|
|
|
switch_loadable_module_get_codecs(switch_core_session_get_pool(tech_pvt->session), codecs, SWITCH_MAX_CODECS))) <= 0) {
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "NO codecs?\n");
|
2006-01-03 22:13:59 +00:00
|
|
|
return SWITCH_STATUS_GENERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (x = 0; x < num_codecs; x++) {
|
|
|
|
unsigned int codec = iana2ast(codecs[x]->ianacode);
|
|
|
|
if (io == IAX_QUERY) {
|
|
|
|
iax_pref_codec_add(iax_session, codec);
|
|
|
|
}
|
|
|
|
local_cap |= codec;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (io == IAX_SET) {
|
|
|
|
mixed_cap = (local_cap & *cababilities);
|
|
|
|
} else {
|
|
|
|
mixed_cap = local_cap;
|
|
|
|
}
|
|
|
|
|
|
|
|
leading = iana2ast(codecs[0]->ianacode);
|
|
|
|
if (io == IAX_QUERY) {
|
|
|
|
chosen = leading;
|
|
|
|
*format = chosen;
|
|
|
|
*cababilities = local_cap;
|
2006-01-12 17:51:08 +00:00
|
|
|
if (globals.codec_rates_last) {
|
2006-01-12 18:16:12 +00:00
|
|
|
*samprate = iax_build_codec_rates();
|
2006-01-12 20:14:06 +00:00
|
|
|
tech_pvt->samprate = *samprate;
|
2006-01-12 17:51:08 +00:00
|
|
|
}
|
2006-01-03 22:13:59 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
} else if (switch_test_flag(&globals, GFLAG_MY_CODEC_PREFS) && (leading & mixed_cap)) {
|
|
|
|
chosen = leading;
|
|
|
|
dname = codecs[0]->iananame;
|
|
|
|
} else {
|
|
|
|
unsigned int prefs[32];
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
if (!switch_test_flag(&globals, GFLAG_MY_CODEC_PREFS)) {
|
|
|
|
len = iax_pref_codec_get(iax_session, prefs, sizeof(prefs));
|
|
|
|
}
|
|
|
|
|
2006-01-20 15:05:05 +00:00
|
|
|
if (len) { /*they sent us a pref and we don't want to be codec master */
|
2006-01-03 22:13:59 +00:00
|
|
|
char pref_str[256] = "(";
|
|
|
|
|
|
|
|
for (x = 0; x < len; x++) {
|
|
|
|
strncat(pref_str, ast2str(prefs[x]), sizeof(pref_str));
|
|
|
|
strncat(pref_str, x == len - 1 ? ")" : ",", sizeof(pref_str));
|
|
|
|
}
|
|
|
|
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Codec Prefs Detected: %s\n", pref_str);
|
2006-01-03 22:13:59 +00:00
|
|
|
|
|
|
|
for (x = 0; x < len; x++) {
|
|
|
|
if ((prefs[x] & mixed_cap)) {
|
|
|
|
int z;
|
|
|
|
chosen = prefs[x];
|
|
|
|
for (z = 0; z < num_codecs; z++) {
|
|
|
|
if (prefs[x] == iana2ast(codecs[z]->ianacode)) {
|
|
|
|
dname = codecs[z]->iananame;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2006-01-20 15:05:05 +00:00
|
|
|
if (*format & mixed_cap) { /* is the one we asked for here? */
|
2006-01-03 22:13:59 +00:00
|
|
|
chosen = *format;
|
|
|
|
for (x = 0; x < num_codecs; x++) {
|
|
|
|
unsigned int cap = iana2ast(codecs[x]->ianacode);
|
|
|
|
if (cap == chosen) {
|
|
|
|
dname = codecs[x]->iananame;
|
|
|
|
}
|
|
|
|
}
|
2006-01-20 15:05:05 +00:00
|
|
|
} else { /* c'mon there has to be SOMETHING... */
|
2006-01-03 22:13:59 +00:00
|
|
|
for (x = 0; x < num_codecs; x++) {
|
|
|
|
unsigned int cap = iana2ast(codecs[x]->ianacode);
|
|
|
|
if (cap & mixed_cap) {
|
|
|
|
chosen = cap;
|
|
|
|
dname = codecs[x]->iananame;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dname && !chosen) {
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "NO codecs?\n");
|
2006-01-03 22:13:59 +00:00
|
|
|
return SWITCH_STATUS_GENERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
channel = switch_core_session_get_channel(tech_pvt->session);
|
|
|
|
assert(channel != NULL);
|
|
|
|
|
2006-01-12 19:38:26 +00:00
|
|
|
|
2006-01-12 20:14:06 +00:00
|
|
|
if (tech_pvt->samprate || *samprate) {
|
2006-01-12 18:16:12 +00:00
|
|
|
unsigned short samples = iax_build_codec_rates();
|
2006-01-12 20:14:06 +00:00
|
|
|
unsigned short mixed = ((tech_pvt->samprate ? tech_pvt->samprate : *samprate) & samples);
|
2006-01-12 18:16:12 +00:00
|
|
|
|
2006-01-13 02:05:39 +00:00
|
|
|
//printf("\n\n******WTF %u %u %u\n******\n", *samprate, samples, mixed);
|
2006-01-12 18:16:55 +00:00
|
|
|
srate = 8000;
|
2006-01-20 15:05:05 +00:00
|
|
|
|
2006-01-12 18:16:12 +00:00
|
|
|
if (mixed & IAX_RATE_16KHZ) {
|
|
|
|
srate = 16000;
|
|
|
|
}
|
|
|
|
if (mixed & IAX_RATE_22KHZ) {
|
2006-01-12 21:01:57 +00:00
|
|
|
srate = 22050;
|
2006-01-12 18:16:12 +00:00
|
|
|
}
|
|
|
|
if (mixed & IAX_RATE_32KHZ) {
|
|
|
|
srate = 32000;
|
|
|
|
}
|
|
|
|
if (mixed & IAX_RATE_44KHZ) {
|
|
|
|
srate = 44000;
|
|
|
|
}
|
|
|
|
if (mixed & IAX_RATE_48KHZ) {
|
|
|
|
srate = 48000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-03 22:13:59 +00:00
|
|
|
if (!strcasecmp(dname, "l16")) {
|
|
|
|
switch_set_flag(tech_pvt, TFLAG_LINEAR);
|
|
|
|
}
|
|
|
|
if (switch_core_codec_init(&tech_pvt->read_codec,
|
2006-01-12 18:16:12 +00:00
|
|
|
dname,
|
|
|
|
srate,
|
|
|
|
0,
|
|
|
|
1,
|
2006-01-20 15:05:05 +00:00
|
|
|
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
|
|
|
|
NULL, switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) {
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't load codec?\n");
|
2006-01-12 18:16:12 +00:00
|
|
|
return SWITCH_STATUS_GENERR;
|
2006-01-03 22:13:59 +00:00
|
|
|
} else {
|
|
|
|
if (switch_core_codec_init(&tech_pvt->write_codec,
|
2006-01-12 18:16:12 +00:00
|
|
|
dname,
|
|
|
|
srate,
|
|
|
|
0,
|
|
|
|
1,
|
2006-01-20 15:05:05 +00:00
|
|
|
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
|
|
|
|
NULL, switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) {
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't load codec?\n");
|
2006-01-20 15:05:05 +00:00
|
|
|
switch_core_codec_destroy(&tech_pvt->read_codec);
|
2006-01-12 18:16:12 +00:00
|
|
|
return SWITCH_STATUS_GENERR;
|
2006-01-03 22:13:59 +00:00
|
|
|
} else {
|
|
|
|
int ms;
|
|
|
|
int rate;
|
|
|
|
ms = tech_pvt->write_codec.implementation->microseconds_per_frame / 1000;
|
|
|
|
rate = tech_pvt->write_codec.implementation->samples_per_second;
|
|
|
|
tech_pvt->read_frame.rate = rate;
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Activate Codec %s/%d %d ms\n", dname, rate, ms);
|
2006-01-03 22:13:59 +00:00
|
|
|
tech_pvt->read_frame.codec = &tech_pvt->read_codec;
|
|
|
|
switch_core_session_set_read_codec(tech_pvt->session, &tech_pvt->read_codec);
|
|
|
|
switch_core_session_set_write_codec(tech_pvt->session, &tech_pvt->write_codec);
|
2006-04-18 16:20:47 +00:00
|
|
|
switch_set_flag(tech_pvt, TFLAG_CODEC);
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
|
|
|
tech_pvt->codec = chosen;
|
|
|
|
tech_pvt->codecs = local_cap;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (io == IAX_QUERY) {
|
|
|
|
*format = tech_pvt->codec;
|
|
|
|
*cababilities = local_cap;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-04-29 06:05:03 +00:00
|
|
|
static switch_status channel_on_init(switch_core_session_t *session);
|
|
|
|
static switch_status channel_on_hangup(switch_core_session_t *session);
|
|
|
|
static switch_status channel_on_ring(switch_core_session_t *session);
|
|
|
|
static switch_status channel_on_loopback(switch_core_session_t *session);
|
|
|
|
static switch_status channel_on_transmit(switch_core_session_t *session);
|
|
|
|
static switch_status channel_outgoing_channel(switch_core_session_t *session, switch_caller_profile_t *outbound_profile,
|
|
|
|
switch_core_session_t **new_session, switch_memory_pool_t *pool);
|
|
|
|
static switch_status channel_read_frame(switch_core_session_t *session, switch_frame_t **frame, int timeout,
|
2006-01-20 15:05:05 +00:00
|
|
|
switch_io_flag flags, int stream_id);
|
2006-04-29 06:05:03 +00:00
|
|
|
static switch_status channel_write_frame(switch_core_session_t *session, switch_frame_t *frame, int timeout,
|
2006-01-20 15:05:05 +00:00
|
|
|
switch_io_flag flags, int stream_id);
|
2006-04-29 06:05:03 +00:00
|
|
|
static switch_status channel_kill_channel(switch_core_session_t *session, int sig);
|
2006-01-03 22:13:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void iax_err_cb(const char *s)
|
|
|
|
{
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN, SWITCH_LOG_ERROR, "IAX ERR: %s", s);
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void iax_out_cb(const char *s)
|
|
|
|
{
|
2006-04-18 16:20:47 +00:00
|
|
|
if (globals.debug > 1) {
|
2006-04-11 21:13:44 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN, SWITCH_LOG_DEBUG, "IAX INFO: %s", s);
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
State methods they get called when the state changes to the specific state
|
|
|
|
returning SWITCH_STATUS_SUCCESS tells the core to execute the standard state method next
|
|
|
|
so if you fully implement the state you can return SWITCH_STATUS_FALSE to skip it.
|
|
|
|
*/
|
2006-04-29 06:05:03 +00:00
|
|
|
static switch_status channel_on_init(switch_core_session_t *session)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
2006-04-29 06:05:03 +00:00
|
|
|
switch_channel_t *channel;
|
2006-01-03 22:13:59 +00:00
|
|
|
struct private_object *tech_pvt = NULL;
|
|
|
|
|
|
|
|
tech_pvt = switch_core_session_get_private(session);
|
|
|
|
assert(tech_pvt != NULL);
|
|
|
|
|
|
|
|
channel = switch_core_session_get_channel(session);
|
|
|
|
assert(channel != NULL);
|
|
|
|
|
|
|
|
tech_pvt->read_frame.data = tech_pvt->databuf;
|
|
|
|
tech_pvt->read_frame.buflen = sizeof(tech_pvt->databuf);
|
|
|
|
iax_set_private(tech_pvt->iax_session, tech_pvt);
|
|
|
|
|
|
|
|
switch_set_flag(tech_pvt, TFLAG_IO);
|
|
|
|
|
|
|
|
switch_mutex_init(&tech_pvt->mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(session));
|
2006-01-20 15:05:05 +00:00
|
|
|
//switch_thread_cond_create(&tech_pvt->cond, switch_core_session_get_pool(session));
|
2006-01-14 16:01:52 +00:00
|
|
|
//switch_mutex_lock(tech_pvt->mutex);
|
2006-01-03 22:13:59 +00:00
|
|
|
|
|
|
|
/* Move Channel's State Machine to RING */
|
|
|
|
switch_channel_set_state(channel, CS_RING);
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-04-29 06:05:03 +00:00
|
|
|
static switch_status channel_on_ring(switch_core_session_t *session)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
2006-04-29 06:05:03 +00:00
|
|
|
switch_channel_t *channel = NULL;
|
2006-01-03 22:13:59 +00:00
|
|
|
struct private_object *tech_pvt = NULL;
|
|
|
|
|
|
|
|
channel = switch_core_session_get_channel(session);
|
|
|
|
assert(channel != NULL);
|
|
|
|
|
|
|
|
tech_pvt = switch_core_session_get_private(session);
|
|
|
|
assert(tech_pvt != NULL);
|
|
|
|
|
2006-04-11 21:13:44 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s CHANNEL RING\n", switch_channel_get_name(channel));
|
2006-01-03 22:13:59 +00:00
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-04-29 06:05:03 +00:00
|
|
|
static switch_status channel_on_execute(switch_core_session_t *session)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
|
|
|
|
2006-04-29 06:05:03 +00:00
|
|
|
switch_channel_t *channel = NULL;
|
2006-01-03 22:13:59 +00:00
|
|
|
struct private_object *tech_pvt = NULL;
|
|
|
|
|
|
|
|
channel = switch_core_session_get_channel(session);
|
|
|
|
assert(channel != NULL);
|
|
|
|
|
|
|
|
tech_pvt = switch_core_session_get_private(session);
|
|
|
|
assert(tech_pvt != NULL);
|
|
|
|
|
2006-04-11 21:13:44 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s CHANNEL EXECUTE\n", switch_channel_get_name(channel));
|
2006-01-03 22:13:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-04-29 06:05:03 +00:00
|
|
|
static switch_status channel_on_hangup(switch_core_session_t *session)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
2006-04-29 06:05:03 +00:00
|
|
|
switch_channel_t *channel = NULL;
|
2006-01-03 22:13:59 +00:00
|
|
|
struct private_object *tech_pvt = NULL;
|
|
|
|
|
|
|
|
channel = switch_core_session_get_channel(session);
|
|
|
|
assert(channel != NULL);
|
|
|
|
|
|
|
|
tech_pvt = switch_core_session_get_private(session);
|
|
|
|
assert(tech_pvt != NULL);
|
|
|
|
|
|
|
|
switch_clear_flag(tech_pvt, TFLAG_IO);
|
2006-01-14 16:01:52 +00:00
|
|
|
switch_clear_flag(tech_pvt, TFLAG_VOICE);
|
|
|
|
//switch_thread_cond_signal(tech_pvt->cond);
|
2006-01-03 22:13:59 +00:00
|
|
|
|
2006-01-06 23:01:28 +00:00
|
|
|
if (tech_pvt->read_codec.implementation) {
|
|
|
|
switch_core_codec_destroy(&tech_pvt->read_codec);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tech_pvt->write_codec.implementation) {
|
|
|
|
switch_core_codec_destroy(&tech_pvt->write_codec);
|
|
|
|
}
|
2006-01-03 22:13:59 +00:00
|
|
|
|
|
|
|
if (tech_pvt->iax_session) {
|
|
|
|
if (!switch_test_flag(tech_pvt, TFLAG_HANGUP)) {
|
|
|
|
iax_hangup(tech_pvt->iax_session, "Hangup");
|
|
|
|
switch_set_flag(tech_pvt, TFLAG_HANGUP);
|
|
|
|
}
|
|
|
|
iax_session_destroy(&tech_pvt->iax_session);
|
|
|
|
}
|
|
|
|
|
2006-04-11 21:13:44 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s CHANNEL HANGUP\n", switch_channel_get_name(channel));
|
2006-01-03 22:13:59 +00:00
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-04-29 06:05:03 +00:00
|
|
|
static switch_status channel_kill_channel(switch_core_session_t *session, int sig)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
2006-04-29 06:05:03 +00:00
|
|
|
switch_channel_t *channel = NULL;
|
2006-01-03 22:13:59 +00:00
|
|
|
struct private_object *tech_pvt = NULL;
|
|
|
|
|
|
|
|
channel = switch_core_session_get_channel(session);
|
|
|
|
assert(channel != NULL);
|
|
|
|
|
|
|
|
tech_pvt = switch_core_session_get_private(session);
|
|
|
|
assert(tech_pvt != NULL);
|
|
|
|
|
|
|
|
switch_clear_flag(tech_pvt, TFLAG_IO);
|
|
|
|
switch_clear_flag(tech_pvt, TFLAG_VOICE);
|
2006-04-22 03:05:25 +00:00
|
|
|
switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING);
|
2006-01-14 16:01:52 +00:00
|
|
|
//switch_thread_cond_signal(tech_pvt->cond);
|
2006-01-03 22:13:59 +00:00
|
|
|
|
2006-04-11 21:13:44 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s CHANNEL KILL\n", switch_channel_get_name(channel));
|
2006-01-03 22:13:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-04-29 06:05:03 +00:00
|
|
|
static switch_status channel_on_loopback(switch_core_session_t *session)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
2006-04-11 21:13:44 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "CHANNEL LOOPBACK\n");
|
2006-01-03 22:13:59 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-04-29 06:05:03 +00:00
|
|
|
static switch_status channel_on_transmit(switch_core_session_t *session)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
2006-04-11 21:13:44 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "CHANNEL TRANSMIT\n");
|
2006-01-03 22:13:59 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-04-29 06:05:03 +00:00
|
|
|
static switch_status channel_waitfor_read(switch_core_session_t *session, int ms, int stream_id)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
|
|
|
struct private_object *tech_pvt = NULL;
|
|
|
|
|
|
|
|
tech_pvt = switch_core_session_get_private(session);
|
|
|
|
assert(tech_pvt != NULL);
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-04-29 06:05:03 +00:00
|
|
|
static switch_status channel_waitfor_write(switch_core_session_t *session, int ms, int stream_id)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
|
|
|
struct private_object *tech_pvt = NULL;
|
|
|
|
|
|
|
|
tech_pvt = switch_core_session_get_private(session);
|
|
|
|
assert(tech_pvt != NULL);
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-04-29 06:05:03 +00:00
|
|
|
static switch_status channel_send_dtmf(switch_core_session_t *session, char *dtmf)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
|
|
|
struct private_object *tech_pvt = NULL;
|
|
|
|
char *digit;
|
|
|
|
|
|
|
|
tech_pvt = switch_core_session_get_private(session);
|
|
|
|
assert(tech_pvt != NULL);
|
|
|
|
if (tech_pvt->iax_session) {
|
2006-01-20 15:05:05 +00:00
|
|
|
for (digit = dtmf; *digit; digit++) {
|
2006-01-03 22:13:59 +00:00
|
|
|
iax_send_dtmf(tech_pvt->iax_session, *digit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-04-29 06:05:03 +00:00
|
|
|
static switch_status channel_read_frame(switch_core_session_t *session, switch_frame_t **frame, int timeout,
|
2006-01-20 15:05:05 +00:00
|
|
|
switch_io_flag flags, int stream_id)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
2006-04-29 06:05:03 +00:00
|
|
|
switch_channel_t *channel = NULL;
|
2006-01-03 22:13:59 +00:00
|
|
|
struct private_object *tech_pvt = NULL;
|
2006-03-21 00:20:24 +00:00
|
|
|
switch_time_t started = switch_time_now();
|
|
|
|
unsigned int elapsed;
|
2006-01-03 22:13:59 +00:00
|
|
|
|
|
|
|
channel = switch_core_session_get_channel(session);
|
|
|
|
assert(channel != NULL);
|
|
|
|
|
|
|
|
tech_pvt = switch_core_session_get_private(session);
|
|
|
|
assert(tech_pvt != NULL);
|
|
|
|
|
|
|
|
|
2006-01-14 16:01:52 +00:00
|
|
|
while (switch_test_flag(tech_pvt, TFLAG_IO)) {
|
|
|
|
//switch_thread_cond_wait(tech_pvt->cond, tech_pvt->mutex);
|
|
|
|
if (!switch_test_flag(tech_pvt, TFLAG_IO)) {
|
2006-01-20 15:05:05 +00:00
|
|
|
return SWITCH_STATUS_FALSE;
|
2006-01-14 16:01:52 +00:00
|
|
|
}
|
2006-01-20 15:05:05 +00:00
|
|
|
|
2006-01-14 16:01:52 +00:00
|
|
|
if (switch_test_flag(tech_pvt, TFLAG_IO) && switch_test_flag(tech_pvt, TFLAG_VOICE)) {
|
|
|
|
switch_clear_flag(tech_pvt, TFLAG_VOICE);
|
2006-01-20 15:05:05 +00:00
|
|
|
if (!tech_pvt->read_frame.datalen) {
|
2006-01-14 16:01:52 +00:00
|
|
|
continue;
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
2006-01-20 15:05:05 +00:00
|
|
|
|
2006-01-14 16:01:52 +00:00
|
|
|
*frame = &tech_pvt->read_frame;
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
2006-03-21 00:20:24 +00:00
|
|
|
|
|
|
|
if (timeout > -1) {
|
|
|
|
elapsed = (unsigned int)((switch_time_now() - started) / 1000);
|
2006-03-21 15:34:54 +00:00
|
|
|
if (elapsed >= (unsigned int)timeout) {
|
2006-03-21 00:20:24 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-01-14 16:01:52 +00:00
|
|
|
switch_yield(1000);
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
2006-01-20 15:05:05 +00:00
|
|
|
|
|
|
|
|
2006-01-03 22:13:59 +00:00
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
|
2006-04-29 06:05:03 +00:00
|
|
|
static switch_status channel_write_frame(switch_core_session_t *session, switch_frame_t *frame, int timeout,
|
2006-01-20 15:05:05 +00:00
|
|
|
switch_io_flag flags, int stream_id)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
2006-04-29 06:05:03 +00:00
|
|
|
switch_channel_t *channel = NULL;
|
2006-01-03 22:13:59 +00:00
|
|
|
struct private_object *tech_pvt = NULL;
|
2006-04-29 06:05:03 +00:00
|
|
|
//switch_frame_t *pframe;
|
2006-01-03 22:13:59 +00:00
|
|
|
|
|
|
|
channel = switch_core_session_get_channel(session);
|
|
|
|
assert(channel != NULL);
|
|
|
|
|
|
|
|
tech_pvt = switch_core_session_get_private(session);
|
|
|
|
assert(tech_pvt != NULL);
|
|
|
|
|
2006-01-20 15:05:05 +00:00
|
|
|
if (!switch_test_flag(tech_pvt, TFLAG_IO)) {
|
2006-01-03 22:13:59 +00:00
|
|
|
return SWITCH_STATUS_FALSE;
|
|
|
|
}
|
|
|
|
#ifndef BIGENDIAN
|
|
|
|
if (switch_test_flag(tech_pvt, TFLAG_LINEAR)) {
|
2006-01-20 15:05:05 +00:00
|
|
|
switch_swap_linear(frame->data, (int) frame->datalen / 2);
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
|
|
|
#endif
|
2006-04-06 19:50:53 +00:00
|
|
|
//printf("Send %ld %d\n", time(NULL), (int) frame->datalen);
|
2006-01-20 15:05:05 +00:00
|
|
|
iax_send_voice(tech_pvt->iax_session, tech_pvt->codec, frame->data, (int) frame->datalen,
|
|
|
|
tech_pvt->write_codec.implementation->samples_per_frame);
|
2006-01-03 22:13:59 +00:00
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-04-29 06:05:03 +00:00
|
|
|
static switch_status channel_answer_channel(switch_core_session_t *session)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
|
|
|
struct private_object *tech_pvt;
|
2006-04-29 06:05:03 +00:00
|
|
|
switch_channel_t *channel = NULL;
|
2006-01-03 22:13:59 +00:00
|
|
|
|
|
|
|
channel = switch_core_session_get_channel(session);
|
|
|
|
assert(channel != NULL);
|
|
|
|
|
|
|
|
tech_pvt = switch_core_session_get_private(session);
|
|
|
|
assert(tech_pvt != NULL);
|
|
|
|
|
2006-01-12 20:15:38 +00:00
|
|
|
if (!switch_test_flag(tech_pvt, TFLAG_OUTBOUND)) {
|
2006-01-12 20:14:06 +00:00
|
|
|
iax_answer(tech_pvt->iax_session);
|
|
|
|
}
|
2006-01-03 22:13:59 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-04-29 06:05:03 +00:00
|
|
|
static const switch_state_handler_table_t channel_event_handlers = {
|
2006-01-20 15:05:05 +00:00
|
|
|
/*.on_init */ channel_on_init,
|
|
|
|
/*.on_ring */ channel_on_ring,
|
|
|
|
/*.on_execute */ channel_on_execute,
|
|
|
|
/*.on_hangup */ channel_on_hangup,
|
|
|
|
/*.on_loopback */ channel_on_loopback,
|
|
|
|
/*.on_transmit */ channel_on_transmit
|
2006-01-03 22:13:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const switch_io_routines channel_io_routines = {
|
2006-01-20 15:05:05 +00:00
|
|
|
/*.outgoing_channel */ channel_outgoing_channel,
|
|
|
|
/*.answer_channel */ channel_answer_channel,
|
|
|
|
/*.read_frame */ channel_read_frame,
|
|
|
|
/*.write_frame */ channel_write_frame,
|
|
|
|
/*.kill_channel */ channel_kill_channel,
|
|
|
|
/*.waitfor_read */ channel_waitfor_read,
|
|
|
|
/*.waitfor_write */ channel_waitfor_write,
|
|
|
|
/*.send_dtmf */ channel_send_dtmf
|
2006-01-03 22:13:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const switch_endpoint_interface channel_endpoint_interface = {
|
2006-01-20 15:05:05 +00:00
|
|
|
/*.interface_name */ "iax",
|
|
|
|
/*.io_routines */ &channel_io_routines,
|
|
|
|
/*.event_handlers */ &channel_event_handlers,
|
|
|
|
/*.private */ NULL,
|
|
|
|
/*.next */ NULL
|
2006-01-03 22:13:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const switch_loadable_module_interface channel_module_interface = {
|
2006-01-20 15:05:05 +00:00
|
|
|
/*.module_name */ modname,
|
|
|
|
/*.endpoint_interface */ &channel_endpoint_interface,
|
|
|
|
/*.timer_interface */ NULL,
|
|
|
|
/*.dialplan_interface */ NULL,
|
|
|
|
/*.codec_interface */ NULL,
|
|
|
|
/*.application_interface */ NULL
|
2006-01-03 22:13:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-02-20 06:13:56 +00:00
|
|
|
/* Make sure when you have 2 sessions in the same scope that you pass the appropriate one to the routines
|
|
|
|
that allocate memory or you will have 1 channel with memory allocated from another channel's pool!
|
|
|
|
*/
|
2006-04-29 06:05:03 +00:00
|
|
|
static switch_status channel_outgoing_channel(switch_core_session_t *session, switch_caller_profile_t *outbound_profile,
|
|
|
|
switch_core_session_t **new_session, switch_memory_pool_t *pool)
|
2006-02-20 06:13:56 +00:00
|
|
|
{
|
2006-03-02 14:49:23 +00:00
|
|
|
if ((*new_session = switch_core_session_request(&channel_endpoint_interface, pool)) != 0) {
|
2006-02-20 06:13:56 +00:00
|
|
|
struct private_object *tech_pvt;
|
2006-04-29 06:05:03 +00:00
|
|
|
switch_channel_t *channel;
|
|
|
|
switch_caller_profile_t *caller_profile;
|
2006-02-20 06:13:56 +00:00
|
|
|
unsigned int req = 0, cap = 0;
|
|
|
|
unsigned short samprate = 0;
|
|
|
|
|
|
|
|
switch_core_session_add_stream(*new_session, NULL);
|
|
|
|
if ((tech_pvt =
|
|
|
|
(struct private_object *) switch_core_session_alloc(*new_session, sizeof(struct private_object))) != 0) {
|
|
|
|
memset(tech_pvt, 0, sizeof(*tech_pvt));
|
|
|
|
channel = switch_core_session_get_channel(*new_session);
|
|
|
|
switch_core_session_set_private(*new_session, tech_pvt);
|
|
|
|
tech_pvt->session = *new_session;
|
|
|
|
} else {
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Hey where is my memory pool?\n");
|
2006-02-20 06:13:56 +00:00
|
|
|
switch_core_session_destroy(new_session);
|
|
|
|
return SWITCH_STATUS_GENERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outbound_profile) {
|
|
|
|
char name[128];
|
2006-02-22 02:50:33 +00:00
|
|
|
|
|
|
|
snprintf(name, sizeof(name), "IAX/%s-%04x", outbound_profile->destination_number, rand() & 0xffff);
|
|
|
|
switch_channel_set_name(channel, name);
|
|
|
|
|
2006-02-20 06:13:56 +00:00
|
|
|
caller_profile = switch_caller_profile_clone(*new_session, outbound_profile);
|
|
|
|
switch_channel_set_caller_profile(channel, caller_profile);
|
|
|
|
tech_pvt->caller_profile = caller_profile;
|
|
|
|
} else {
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Doh! no caller profile\n");
|
2006-02-20 06:13:56 +00:00
|
|
|
switch_core_session_destroy(new_session);
|
|
|
|
return SWITCH_STATUS_GENERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((tech_pvt->iax_session = iax_session_new()) == 0) {
|
|
|
|
switch_core_session_destroy(new_session);
|
|
|
|
return SWITCH_STATUS_GENERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (iax_set_codec(tech_pvt, tech_pvt->iax_session, &req, &cap, &samprate, IAX_QUERY) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_core_session_destroy(new_session);
|
|
|
|
return SWITCH_STATUS_GENERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (samprate) {
|
|
|
|
iax_set_samplerate(tech_pvt->iax_session, samprate);
|
|
|
|
}
|
|
|
|
|
|
|
|
iax_call(tech_pvt->iax_session,
|
|
|
|
caller_profile->caller_id_number,
|
|
|
|
caller_profile->caller_id_name, caller_profile->destination_number, NULL, 0, req, cap);
|
|
|
|
|
|
|
|
switch_channel_set_flag(channel, CF_OUTBOUND);
|
|
|
|
switch_set_flag(tech_pvt, TFLAG_OUTBOUND);
|
|
|
|
switch_channel_set_state(channel, CS_INIT);
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SWITCH_STATUS_GENERR;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-01-20 15:05:05 +00:00
|
|
|
SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_module_interface **interface, char *filename)
|
|
|
|
{
|
2006-01-03 22:13:59 +00:00
|
|
|
|
|
|
|
if (switch_core_new_memory_pool(&module_pool) != SWITCH_STATUS_SUCCESS) {
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "OH OH no pool\n");
|
2006-01-03 22:13:59 +00:00
|
|
|
return SWITCH_STATUS_TERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* connect my internal structure to the blank pointer passed to me */
|
|
|
|
*interface = &channel_module_interface;
|
|
|
|
|
|
|
|
/* indicate that the module should continue to be loaded */
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PHONE_FREED 0
|
|
|
|
#define PHONE_CALLACCEPTED 1
|
|
|
|
#define PHONE_RINGING 2
|
|
|
|
#define PHONE_ANSWERED 3
|
|
|
|
#define PHONE_CONNECTED 4
|
|
|
|
|
|
|
|
#define UNREGISTERED 0
|
|
|
|
#define REGISTERED 1
|
|
|
|
|
|
|
|
|
|
|
|
static switch_status load_config(void)
|
|
|
|
{
|
2006-04-29 06:05:03 +00:00
|
|
|
switch_config_t cfg;
|
2006-01-03 22:13:59 +00:00
|
|
|
char *var, *val;
|
|
|
|
char *cf = "iax.conf";
|
|
|
|
|
|
|
|
memset(&globals, 0, sizeof(globals));
|
|
|
|
|
|
|
|
if (!switch_config_open_file(&cfg, cf)) {
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", cf);
|
2006-01-03 22:13:59 +00:00
|
|
|
return SWITCH_STATUS_TERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (switch_config_next_pair(&cfg, &var, &val)) {
|
|
|
|
if (!strcasecmp(cfg.category, "settings")) {
|
|
|
|
if (!strcmp(var, "debug")) {
|
|
|
|
globals.debug = atoi(val);
|
|
|
|
} else if (!strcmp(var, "port")) {
|
|
|
|
globals.port = atoi(val);
|
2006-04-13 01:05:47 +00:00
|
|
|
} else if (!strcmp(var, "ip")) {
|
|
|
|
set_global_ip(val);
|
2006-01-03 22:13:59 +00:00
|
|
|
} else if (!strcmp(var, "codec_master")) {
|
|
|
|
if (!strcasecmp(val, "us")) {
|
|
|
|
switch_set_flag(&globals, GFLAG_MY_CODEC_PREFS);
|
|
|
|
}
|
|
|
|
} else if (!strcmp(var, "dialplan")) {
|
|
|
|
set_global_dialplan(val);
|
|
|
|
} else if (!strcmp(var, "codec_prefs")) {
|
|
|
|
set_global_codec_string(val);
|
2006-01-20 15:05:05 +00:00
|
|
|
globals.codec_order_last =
|
|
|
|
switch_separate_string(globals.codec_string, ',', globals.codec_order, SWITCH_MAX_CODECS);
|
2006-01-12 17:51:08 +00:00
|
|
|
} else if (!strcmp(var, "codec_rates")) {
|
|
|
|
set_global_codec_rates_string(val);
|
2006-01-20 15:05:05 +00:00
|
|
|
globals.codec_rates_last =
|
|
|
|
switch_separate_string(globals.codec_rates_string, ',', globals.codec_rates, SWITCH_MAX_CODECS);
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!globals.dialplan) {
|
|
|
|
set_global_dialplan("default");
|
|
|
|
}
|
|
|
|
if (!globals.port) {
|
|
|
|
globals.port = 4569;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_config_close_file(&cfg);
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SWITCH_MOD_DECLARE(switch_status) switch_module_runtime(void)
|
|
|
|
{
|
2006-03-30 23:02:50 +00:00
|
|
|
//int refresh;
|
2006-01-03 22:13:59 +00:00
|
|
|
struct iax_event *iaxevent = NULL;
|
2006-04-29 01:00:52 +00:00
|
|
|
switch_event_t *s_event;
|
2006-03-01 00:58:32 +00:00
|
|
|
if (load_config() != SWITCH_STATUS_SUCCESS) {
|
|
|
|
return SWITCH_STATUS_TERM;
|
|
|
|
}
|
2006-01-03 22:13:59 +00:00
|
|
|
|
2006-04-18 16:20:47 +00:00
|
|
|
if (globals.debug > 1) {
|
2006-01-03 22:13:59 +00:00
|
|
|
iax_enable_debug();
|
|
|
|
}
|
2006-04-13 01:05:47 +00:00
|
|
|
if (iax_init(globals.ip, globals.port) < 0) {
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Binding Port!\n");
|
2006-01-03 22:13:59 +00:00
|
|
|
return SWITCH_STATUS_TERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
iax_set_error(iax_err_cb);
|
|
|
|
iax_set_output(iax_out_cb);
|
2006-03-30 23:02:50 +00:00
|
|
|
//netfd = iax_get_fd();
|
2006-01-03 22:13:59 +00:00
|
|
|
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "IAX Ready Port %d\n", globals.port);
|
2006-01-03 22:13:59 +00:00
|
|
|
|
2006-02-14 16:42:26 +00:00
|
|
|
if (switch_event_create(&s_event, SWITCH_EVENT_PUBLISH) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "service", "_iax2._udp");
|
|
|
|
switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "port", "%d", globals.port);
|
|
|
|
switch_event_fire(&s_event);
|
|
|
|
}
|
|
|
|
|
2006-01-20 15:05:05 +00:00
|
|
|
for (;;) {
|
2006-01-03 22:13:59 +00:00
|
|
|
|
|
|
|
if (running == -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-01-20 15:05:05 +00:00
|
|
|
/* Wait for an event. */
|
2006-01-03 22:13:59 +00:00
|
|
|
if ((iaxevent = iax_get_event(0)) == NULL) {
|
|
|
|
switch_yield(1000);
|
|
|
|
continue;
|
|
|
|
} else if (iaxevent) {
|
|
|
|
struct private_object *tech_pvt = iax_get_private(iaxevent->session);
|
|
|
|
|
|
|
|
if (globals.debug && iaxevent->etype != IAX_EVENT_VOICE) {
|
2006-04-11 21:13:44 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Event %d [%s]!\n",
|
2006-04-18 16:20:47 +00:00
|
|
|
iaxevent->etype, IAXNAMES[iaxevent->etype]);
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
2006-04-18 16:20:47 +00:00
|
|
|
|
2006-01-03 22:13:59 +00:00
|
|
|
switch (iaxevent->etype) {
|
2006-01-20 15:05:05 +00:00
|
|
|
case IAX_EVENT_REGACK:
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Registration completed successfully.\n");
|
2006-03-30 23:02:50 +00:00
|
|
|
//if (iaxevent->ies.refresh)
|
|
|
|
//refresh = iaxevent->ies.refresh;
|
2006-01-20 15:05:05 +00:00
|
|
|
break;
|
|
|
|
case IAX_EVENT_REGREJ:
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Registration failed.\n");
|
2006-01-20 15:05:05 +00:00
|
|
|
break;
|
|
|
|
case IAX_EVENT_TIMEOUT:
|
|
|
|
break;
|
|
|
|
case IAX_EVENT_ACCEPT:
|
|
|
|
if (tech_pvt) {
|
|
|
|
unsigned int cap = iax_session_get_capability(iaxevent->session);
|
|
|
|
unsigned int format = iaxevent->ies.format;
|
2006-04-18 16:20:47 +00:00
|
|
|
|
2006-01-20 15:05:05 +00:00
|
|
|
if (iax_set_codec(tech_pvt, iaxevent->session, &format, &cap, &iaxevent->ies.samprate, IAX_SET) !=
|
|
|
|
SWITCH_STATUS_SUCCESS) {
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "WTF? %u %u\n", iaxevent->ies.format,
|
2006-01-20 15:05:05 +00:00
|
|
|
iaxevent->ies.capability);
|
|
|
|
}
|
|
|
|
}
|
2006-01-20 00:41:30 +00:00
|
|
|
|
2006-01-20 15:05:05 +00:00
|
|
|
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Call accepted.\n");
|
2006-01-20 15:05:05 +00:00
|
|
|
break;
|
|
|
|
case IAX_EVENT_RINGA:
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Ringing heard.\n");
|
2006-01-20 15:05:05 +00:00
|
|
|
break;
|
|
|
|
case IAX_EVENT_PONG:
|
|
|
|
// informative only
|
|
|
|
break;
|
|
|
|
case IAX_EVENT_ANSWER:
|
|
|
|
// the other side answered our call
|
|
|
|
if (tech_pvt) {
|
2006-04-29 06:05:03 +00:00
|
|
|
switch_channel_t *channel;
|
2006-02-20 06:13:56 +00:00
|
|
|
if ((channel = switch_core_session_get_channel(tech_pvt->session)) != 0) {
|
2006-01-20 15:05:05 +00:00
|
|
|
if (switch_channel_test_flag(channel, CF_ANSWERED)) {
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "WTF Mutiple Answer %s?\n",
|
2006-04-18 16:20:47 +00:00
|
|
|
switch_channel_get_name(channel));
|
2006-01-20 15:05:05 +00:00
|
|
|
} else {
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Answer %s\n",
|
2006-01-20 15:05:05 +00:00
|
|
|
switch_channel_get_name(channel));
|
|
|
|
switch_channel_answer(channel);
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
|
|
|
}
|
2006-01-20 15:05:05 +00:00
|
|
|
}
|
2006-01-03 22:13:59 +00:00
|
|
|
|
2006-01-20 15:05:05 +00:00
|
|
|
break;
|
|
|
|
case IAX_EVENT_CONNECT:
|
|
|
|
// incoming call detected
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE,
|
2006-03-30 23:02:50 +00:00
|
|
|
"Incoming call connected %s, %s, %s %u/%u\n",
|
2006-01-20 15:05:05 +00:00
|
|
|
iaxevent->ies.called_number,
|
|
|
|
iaxevent->ies.calling_number,
|
|
|
|
iaxevent->ies.calling_name, iaxevent->ies.format, iaxevent->ies.capability);
|
|
|
|
|
|
|
|
if (iaxevent) {
|
2006-04-29 06:05:03 +00:00
|
|
|
switch_core_session_t *session;
|
2006-01-20 15:05:05 +00:00
|
|
|
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "New Inbound Channel %s!\n",
|
2006-01-20 15:05:05 +00:00
|
|
|
iaxevent->ies.calling_name);
|
2006-02-20 06:13:56 +00:00
|
|
|
if ((session = switch_core_session_request(&channel_endpoint_interface, NULL)) != 0) {
|
2006-01-20 15:05:05 +00:00
|
|
|
struct private_object *tech_pvt;
|
2006-04-29 06:05:03 +00:00
|
|
|
switch_channel_t *channel;
|
2006-01-20 15:05:05 +00:00
|
|
|
|
|
|
|
switch_core_session_add_stream(session, NULL);
|
|
|
|
if ((tech_pvt =
|
|
|
|
(struct private_object *) switch_core_session_alloc(session,
|
2006-02-20 06:13:56 +00:00
|
|
|
sizeof(struct private_object))) != 0) {
|
2006-01-20 15:05:05 +00:00
|
|
|
memset(tech_pvt, 0, sizeof(*tech_pvt));
|
|
|
|
channel = switch_core_session_get_channel(session);
|
|
|
|
switch_core_session_set_private(session, tech_pvt);
|
|
|
|
tech_pvt->session = session;
|
|
|
|
} else {
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Hey where is my memory pool?\n");
|
2006-01-20 15:05:05 +00:00
|
|
|
switch_core_session_destroy(&session);
|
|
|
|
break;
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
|
|
|
|
2006-01-20 15:05:05 +00:00
|
|
|
|
2006-03-01 22:55:28 +00:00
|
|
|
if ((tech_pvt->caller_profile = switch_caller_profile_new(switch_core_session_get_pool(session),
|
2006-01-20 15:05:05 +00:00
|
|
|
globals.dialplan,
|
|
|
|
iaxevent->ies.calling_name,
|
|
|
|
iaxevent->ies.calling_number,
|
|
|
|
iax_get_peer_ip(iaxevent->session),
|
|
|
|
iaxevent->ies.calling_ani,
|
2006-04-21 22:31:08 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
(char *)modname,
|
2006-04-28 19:46:57 +00:00
|
|
|
NULL,
|
2006-04-21 22:31:08 +00:00
|
|
|
iaxevent->ies.called_number)) != 0) {
|
2006-01-20 15:05:05 +00:00
|
|
|
char name[128];
|
|
|
|
snprintf(name, sizeof(name), "IAX/%s-%04x", tech_pvt->caller_profile->destination_number,
|
|
|
|
rand() & 0xffff);
|
|
|
|
switch_channel_set_name(channel, name);
|
2006-02-22 02:50:33 +00:00
|
|
|
switch_channel_set_caller_profile(channel, tech_pvt->caller_profile);
|
|
|
|
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
|
|
|
|
2006-01-20 15:05:05 +00:00
|
|
|
if (iax_set_codec(tech_pvt, iaxevent->session,
|
|
|
|
&iaxevent->ies.format,
|
|
|
|
&iaxevent->ies.capability,
|
|
|
|
&iaxevent->ies.samprate, IAX_SET) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
iax_reject(iaxevent->session, "Codec Error!");
|
|
|
|
switch_core_session_destroy(&session);
|
2006-01-03 22:13:59 +00:00
|
|
|
} else {
|
2006-01-20 15:05:05 +00:00
|
|
|
tech_pvt->iax_session = iaxevent->session;
|
|
|
|
tech_pvt->session = session;
|
|
|
|
iax_accept(tech_pvt->iax_session, tech_pvt->codec);
|
|
|
|
iax_ring_announce(tech_pvt->iax_session);
|
|
|
|
switch_channel_set_state(channel, CS_INIT);
|
|
|
|
switch_core_session_thread_launch(session);
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
|
|
|
}
|
2006-01-20 15:05:05 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IAX_EVENT_REJECT:
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Rejected call.\n");
|
2006-01-20 15:05:05 +00:00
|
|
|
case IAX_EVENT_BUSY:
|
|
|
|
case IAX_EVENT_HANGUP:
|
|
|
|
if (tech_pvt) {
|
2006-04-29 06:05:03 +00:00
|
|
|
switch_channel_t *channel;
|
2006-01-20 15:05:05 +00:00
|
|
|
|
|
|
|
switch_clear_flag(tech_pvt, TFLAG_IO);
|
|
|
|
switch_clear_flag(tech_pvt, TFLAG_VOICE);
|
2006-02-20 06:13:56 +00:00
|
|
|
if ((channel = switch_core_session_get_channel(tech_pvt->session)) != 0) {
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Hangup %s\n", switch_channel_get_name(channel));
|
2006-01-20 15:05:05 +00:00
|
|
|
switch_set_flag(tech_pvt, TFLAG_HANGUP);
|
2006-04-22 03:05:25 +00:00
|
|
|
switch_channel_hangup(channel, iaxevent->etype == IAX_EVENT_HANGUP ? SWITCH_CAUSE_NORMAL_CLEARING : SWITCH_CAUSE_FACILITY_REJECTED);
|
2006-01-20 15:05:05 +00:00
|
|
|
//switch_thread_cond_signal(tech_pvt->cond);
|
|
|
|
iaxevent->session = NULL;
|
|
|
|
} else {
|
2006-04-11 21:13:44 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "No Session? %s\n",
|
2006-01-20 15:05:05 +00:00
|
|
|
switch_test_flag(tech_pvt, TFLAG_VOICE) ? "yes" : "no");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IAX_EVENT_CNG:
|
|
|
|
// pseudo-silence
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "sending silence\n");
|
2006-01-20 15:05:05 +00:00
|
|
|
break;
|
|
|
|
case IAX_EVENT_VOICE:
|
2006-02-20 06:13:56 +00:00
|
|
|
if (tech_pvt && (tech_pvt->read_frame.datalen = iaxevent->datalen) != 0) {
|
2006-04-29 06:05:03 +00:00
|
|
|
switch_channel_t *channel;
|
2006-02-20 06:13:56 +00:00
|
|
|
if (((channel = switch_core_session_get_channel(tech_pvt->session)) != 0)
|
2006-01-20 15:05:05 +00:00
|
|
|
&& switch_channel_get_state(channel) <= CS_HANGUP) {
|
2006-04-18 16:20:47 +00:00
|
|
|
int bytes, frames;
|
|
|
|
|
|
|
|
if (!switch_test_flag(tech_pvt, TFLAG_CODEC)) {
|
|
|
|
//switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "audio with no codec yet!\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes = tech_pvt->read_codec.implementation->encoded_bytes_per_frame;
|
|
|
|
frames = (int) (tech_pvt->read_frame.datalen / bytes);
|
|
|
|
|
|
|
|
|
2006-01-20 15:05:05 +00:00
|
|
|
tech_pvt->read_frame.samples = frames * tech_pvt->read_codec.implementation->samples_per_frame;
|
|
|
|
memcpy(tech_pvt->read_frame.data, iaxevent->data, iaxevent->datalen);
|
|
|
|
/* wake up the i/o thread */
|
|
|
|
switch_set_flag(tech_pvt, TFLAG_VOICE);
|
|
|
|
//switch_thread_cond_signal(tech_pvt->cond);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IAX_EVENT_TRANSFER:
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Call transfer occurred.\n");
|
2006-01-20 15:05:05 +00:00
|
|
|
//session[0] = iaxevent->session;
|
|
|
|
break;
|
|
|
|
case IAX_EVENT_DTMF:
|
|
|
|
if (tech_pvt) {
|
2006-04-29 06:05:03 +00:00
|
|
|
switch_channel_t *channel;
|
2006-02-20 06:13:56 +00:00
|
|
|
if ((channel = switch_core_session_get_channel(tech_pvt->session)) != 0) {
|
|
|
|
char str[2] = { (char)iaxevent->subclass };
|
2006-01-20 15:05:05 +00:00
|
|
|
if (globals.debug) {
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s DTMF %s\n", str,
|
2006-01-20 15:05:05 +00:00
|
|
|
switch_channel_get_name(channel));
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
2006-01-20 15:05:05 +00:00
|
|
|
switch_channel_queue_dtmf(channel, str);
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
2006-01-20 15:05:05 +00:00
|
|
|
}
|
2006-01-03 22:13:59 +00:00
|
|
|
|
2006-01-20 15:05:05 +00:00
|
|
|
break;
|
|
|
|
default:
|
2006-04-11 21:13:44 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Don't know what to do with IAX event %d.\n",
|
2006-01-20 15:05:05 +00:00
|
|
|
iaxevent->etype);
|
|
|
|
break;
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
iax_event_free(iaxevent);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
running = 0;
|
|
|
|
|
|
|
|
return SWITCH_STATUS_TERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SWITCH_MOD_DECLARE(switch_status) switch_module_shutdown(void)
|
|
|
|
{
|
|
|
|
int x = 0;
|
|
|
|
|
|
|
|
running = -1;
|
|
|
|
|
|
|
|
iax_shutdown();
|
|
|
|
|
|
|
|
while (running) {
|
|
|
|
if (x++ > 100) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch_yield(20000);
|
|
|
|
}
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|