2006-01-03 22:13:59 +00:00
|
|
|
/*
|
|
|
|
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
2009-02-13 23:37:37 +00:00
|
|
|
* Copyright (C) 2005-2009, Anthony Minessale II <anthm@freeswitch.org>
|
2006-01-03 22:13:59 +00:00
|
|
|
*
|
|
|
|
* 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
|
2009-02-04 21:20:54 +00:00
|
|
|
* Anthony Minessale II <anthm@freeswitch.org>
|
2006-01-03 22:13:59 +00:00
|
|
|
* Portions created by the Initial Developer are Copyright (C)
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
2009-02-04 21:20:54 +00:00
|
|
|
* Anthony Minessale II <anthm@freeswitch.org>
|
2006-01-03 22:13:59 +00:00
|
|
|
*
|
|
|
|
*
|
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>
|
|
|
|
|
2007-06-13 14:35:55 +00:00
|
|
|
SWITCH_MODULE_LOAD_FUNCTION(mod_iax_load);
|
|
|
|
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_iax_shutdown);
|
|
|
|
SWITCH_MODULE_RUNTIME_FUNCTION(mod_iax_runtime);
|
|
|
|
SWITCH_MODULE_DEFINITION(mod_iax, mod_iax_load, mod_iax_shutdown, mod_iax_runtime);
|
|
|
|
|
2006-01-03 22:13:59 +00:00
|
|
|
#include <iax2.h>
|
|
|
|
#include <iax-client.h>
|
|
|
|
#include <iax2-parser.h>
|
2007-03-11 14:03:58 +00:00
|
|
|
#ifdef WIN32
|
2006-01-03 22:13:59 +00:00
|
|
|
#include <sys/timeb.h>
|
|
|
|
#endif
|
|
|
|
|
2007-10-29 18:10:06 +00:00
|
|
|
switch_endpoint_interface_t *iax_endpoint_interface;
|
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),
|
2006-12-30 17:22:17 +00:00
|
|
|
TFLAG_CODEC = (1 << 7),
|
|
|
|
TFLAG_BREAK = (1 << 8)
|
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;
|
2006-09-12 01:41:23 +00:00
|
|
|
int fd;
|
2006-09-13 19:02:22 +00:00
|
|
|
int calls;
|
|
|
|
switch_mutex_t *mutex;
|
2006-01-03 22:13:59 +00:00
|
|
|
} 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;
|
2007-03-09 23:51:52 +00:00
|
|
|
unsigned char databuf[SWITCH_RECOMMENDED_BUFFER_SIZE];
|
2008-03-19 17:56:27 +00:00
|
|
|
switch_frame_t cng_frame;
|
|
|
|
unsigned char cng_databuf[10];
|
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-06-23 16:59:47 +00:00
|
|
|
switch_mutex_t *flag_mutex;
|
2006-01-14 16:01:52 +00:00
|
|
|
//switch_thread_cond_t *cond;
|
2006-01-03 22:13:59 +00:00
|
|
|
};
|
|
|
|
|
2007-01-19 01:26:22 +00:00
|
|
|
typedef struct private_object private_t;
|
|
|
|
|
|
|
|
|
2007-10-17 14:59:17 +00:00
|
|
|
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_dialplan, globals.dialplan);
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
static char *IAXNAMES[] = { "IAX_EVENT_CONNECT", "IAX_EVENT_ACCEPT", "IAX_EVENT_HANGUP", "IAX_EVENT_REJECT", "IAX_EVENT_VOICE",
|
2008-05-27 04:54:52 +00:00
|
|
|
"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"
|
2007-10-17 14:59:17 +00:00
|
|
|
};
|
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"},
|
2008-05-27 04:54:52 +00:00
|
|
|
{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, 97, "speex"},
|
|
|
|
{AST_FORMAT_SPEEX, 98, "speex"},
|
|
|
|
{AST_FORMAT_ILBC, 102, "ilbc"},
|
|
|
|
{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-20 15:05:05 +00:00
|
|
|
};
|
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;
|
2007-12-13 00:19:53 +00:00
|
|
|
for (x = 0; x < 19; 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;
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
static switch_status_t iax_set_codec(private_t *tech_pvt, struct iax_session *iax_session,
|
2007-03-30 00:13:31 +00:00
|
|
|
unsigned int *format, unsigned int *cababilities, unsigned short *samprate, iax_io_t io)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
|
|
|
char *dname = NULL;
|
2006-04-29 06:05:03 +00:00
|
|
|
switch_channel_t *channel;
|
2006-07-10 22:08:02 +00:00
|
|
|
const switch_codec_implementation_t *codecs[SWITCH_MAX_CODECS];
|
2006-01-03 22:13:59 +00:00
|
|
|
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-07-10 22:24:57 +00:00
|
|
|
uint32_t interval = 0;
|
2006-01-03 22:13:59 +00:00
|
|
|
|
|
|
|
if (globals.codec_string) {
|
2007-12-13 00:19:53 +00:00
|
|
|
num_codecs = switch_loadable_module_get_codecs_sorted(codecs, SWITCH_MAX_CODECS, globals.codec_order, globals.codec_order_last);
|
|
|
|
} else {
|
|
|
|
num_codecs = switch_loadable_module_get_codecs(codecs, SWITCH_MAX_CODECS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_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++) {
|
2006-06-23 20:14:29 +00:00
|
|
|
static const switch_codec_implementation_t *imp;
|
2006-07-10 22:08:02 +00:00
|
|
|
for (imp = codecs[x]; imp; imp = imp->next) {
|
2006-06-23 20:14:29 +00:00
|
|
|
unsigned int codec = iana2ast(imp->ianacode);
|
2006-08-07 19:45:52 +00:00
|
|
|
if (io == IAX_QUERY && !(codec & local_cap)) {
|
2006-06-23 20:14:29 +00:00
|
|
|
iax_pref_codec_add(iax_session, codec);
|
2007-03-29 22:31:56 +00:00
|
|
|
}
|
2006-06-23 20:14:29 +00:00
|
|
|
local_cap |= codec;
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-13 00:19:53 +00:00
|
|
|
switch_assert(codecs[0]);
|
|
|
|
|
2006-01-03 22:13:59 +00:00
|
|
|
if (io == IAX_SET) {
|
|
|
|
mixed_cap = (local_cap & *cababilities);
|
|
|
|
} else {
|
|
|
|
mixed_cap = local_cap;
|
|
|
|
}
|
|
|
|
|
2006-07-10 22:08:02 +00:00
|
|
|
leading = iana2ast(codecs[0]->ianacode);
|
2008-10-20 17:48:42 +00:00
|
|
|
interval = codecs[0]->microseconds_per_packet / 1000;
|
2006-01-03 22:13:59 +00:00
|
|
|
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;
|
2006-07-10 22:08:02 +00:00
|
|
|
dname = codecs[0]->iananame;
|
2006-01-03 22:13:59 +00:00
|
|
|
} 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++) {
|
2008-05-27 04:54:52 +00:00
|
|
|
strncat(pref_str, ast2str(prefs[x]), sizeof(pref_str) - 1);
|
|
|
|
strncat(pref_str, x == len - 1 ? ")" : ",", sizeof(pref_str) - 1);
|
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, "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++) {
|
2006-06-23 20:14:29 +00:00
|
|
|
static const switch_codec_implementation_t *imp;
|
2006-07-10 22:08:02 +00:00
|
|
|
for (imp = codecs[z]; imp; imp = imp->next) {
|
2006-06-23 20:14:29 +00:00
|
|
|
if (prefs[x] == iana2ast(imp->ianacode)) {
|
|
|
|
dname = imp->iananame;
|
2008-10-20 17:48:42 +00:00
|
|
|
interval = imp->microseconds_per_packet / 1000;
|
2006-06-23 20:14:29 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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++) {
|
2006-06-23 20:14:29 +00:00
|
|
|
static const switch_codec_implementation_t *imp;
|
2006-07-10 22:08:02 +00:00
|
|
|
for (imp = codecs[x]; imp; imp = imp->next) {
|
2006-06-23 20:14:29 +00:00
|
|
|
unsigned int cap = iana2ast(imp->ianacode);
|
|
|
|
if (cap == chosen) {
|
|
|
|
dname = imp->iananame;
|
2008-10-20 17:48:42 +00:00
|
|
|
interval = imp->microseconds_per_packet / 1000;
|
2006-06-23 20:14:29 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
|
|
|
}
|
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++) {
|
2006-06-23 20:14:29 +00:00
|
|
|
static const switch_codec_implementation_t *imp;
|
2006-07-10 22:08:02 +00:00
|
|
|
for (imp = codecs[x]; imp; imp = imp->next) {
|
2006-06-23 20:14:29 +00:00
|
|
|
unsigned int cap = iana2ast(imp->ianacode);
|
|
|
|
if (cap & mixed_cap) {
|
|
|
|
chosen = cap;
|
|
|
|
dname = imp->iananame;
|
2008-10-20 17:48:42 +00:00
|
|
|
interval = imp->microseconds_per_packet / 1000;
|
2006-06-23 20:14:29 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
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-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")) {
|
2006-06-23 16:59:47 +00:00
|
|
|
switch_set_flag_locked(tech_pvt, TFLAG_LINEAR);
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
|
|
|
if (switch_core_codec_init(&tech_pvt->read_codec,
|
2006-01-12 18:16:12 +00:00
|
|
|
dname,
|
2006-10-09 02:24:43 +00:00
|
|
|
NULL,
|
2006-01-12 18:16:12 +00:00
|
|
|
srate,
|
2006-07-10 22:24:57 +00:00
|
|
|
interval,
|
2006-01-12 18:16:12 +00:00
|
|
|
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,
|
2006-10-09 02:24:43 +00:00
|
|
|
NULL,
|
2006-01-12 18:16:12 +00:00
|
|
|
srate,
|
2006-07-10 22:24:57 +00:00
|
|
|
interval,
|
2006-01-12 18:16:12 +00:00
|
|
|
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;
|
2008-10-20 17:48:42 +00:00
|
|
|
ms = tech_pvt->write_codec.implementation->microseconds_per_packet / 1000;
|
2006-01-03 22:13:59 +00:00
|
|
|
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-06-23 16:59:47 +00:00
|
|
|
switch_set_flag_locked(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 23:43:28 +00:00
|
|
|
static switch_status_t channel_on_init(switch_core_session_t *session);
|
|
|
|
static switch_status_t channel_on_hangup(switch_core_session_t *session);
|
2009-04-10 17:43:18 +00:00
|
|
|
static switch_status_t channel_on_destroy(switch_core_session_t *session);
|
2008-05-05 15:30:55 +00:00
|
|
|
static switch_status_t channel_on_routing(switch_core_session_t *session);
|
|
|
|
static switch_status_t channel_on_exchange_media(switch_core_session_t *session);
|
|
|
|
static switch_status_t channel_on_soft_execute(switch_core_session_t *session);
|
2008-05-15 19:29:35 +00:00
|
|
|
static switch_call_cause_t channel_outgoing_channel(switch_core_session_t *session, switch_event_t *var_event,
|
2007-03-29 22:31:56 +00:00
|
|
|
switch_caller_profile_t *outbound_profile,
|
2007-12-20 21:42:00 +00:00
|
|
|
switch_core_session_t **new_session, switch_memory_pool_t **pool, switch_originate_flag_t flags);
|
2008-05-08 19:19:47 +00:00
|
|
|
static switch_status_t channel_read_frame(switch_core_session_t *session, switch_frame_t **frame, switch_io_flag_t flags, int stream_id);
|
|
|
|
static switch_status_t channel_write_frame(switch_core_session_t *session, switch_frame_t *frame, switch_io_flag_t flags, int stream_id);
|
2006-04-29 23:43:28 +00:00
|
|
|
static switch_status_t 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
static void tech_init(private_t *tech_pvt, switch_core_session_t *session)
|
2007-01-19 01:26:22 +00:00
|
|
|
{
|
|
|
|
tech_pvt->read_frame.data = tech_pvt->databuf;
|
|
|
|
tech_pvt->read_frame.buflen = sizeof(tech_pvt->databuf);
|
2008-03-19 17:56:27 +00:00
|
|
|
tech_pvt->cng_frame.data = tech_pvt->cng_databuf;
|
|
|
|
tech_pvt->cng_frame.buflen = sizeof(tech_pvt->cng_databuf);
|
|
|
|
switch_set_flag((&tech_pvt->cng_frame), SFF_CNG);
|
|
|
|
tech_pvt->cng_frame.datalen = 2;
|
2007-01-19 01:26:22 +00:00
|
|
|
switch_mutex_init(&tech_pvt->mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(session));
|
2007-03-29 22:31:56 +00:00
|
|
|
switch_mutex_init(&tech_pvt->flag_mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(session));
|
|
|
|
switch_core_session_set_private(session, tech_pvt);
|
|
|
|
tech_pvt->session = session;
|
2007-01-19 01:26:22 +00:00
|
|
|
}
|
2006-01-03 22:13:59 +00:00
|
|
|
|
|
|
|
/*
|
2007-10-17 14:59:17 +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-01-03 22:13:59 +00:00
|
|
|
*/
|
2006-04-29 23:43:28 +00:00
|
|
|
static switch_status_t channel_on_init(switch_core_session_t *session)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
2008-01-28 07:26:10 +00:00
|
|
|
private_t *tech_pvt = switch_core_session_get_private(session);
|
2007-12-12 23:21:45 +00:00
|
|
|
switch_assert(tech_pvt != NULL);
|
2006-01-03 22:13:59 +00:00
|
|
|
|
|
|
|
iax_set_private(tech_pvt->iax_session, tech_pvt);
|
|
|
|
|
2006-06-23 16:59:47 +00:00
|
|
|
switch_set_flag_locked(tech_pvt, TFLAG_IO);
|
2006-01-03 22:13:59 +00:00
|
|
|
|
2008-05-06 13:59:37 +00:00
|
|
|
/* Move channel's state machine to ROUTING */
|
2008-05-05 15:30:55 +00:00
|
|
|
switch_channel_set_state(switch_core_session_get_channel(session), CS_ROUTING);
|
2006-09-13 19:02:22 +00:00
|
|
|
switch_mutex_lock(globals.mutex);
|
|
|
|
globals.calls++;
|
|
|
|
switch_mutex_unlock(globals.mutex);
|
2006-01-03 22:13:59 +00:00
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-05-05 15:30:55 +00:00
|
|
|
static switch_status_t channel_on_routing(switch_core_session_t *session)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
2008-05-12 13:46:07 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s CHANNEL ROUTING\n", switch_channel_get_name(switch_core_session_get_channel(session)));
|
2006-01-03 22:13:59 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-04-29 23:43:28 +00:00
|
|
|
static switch_status_t channel_on_execute(switch_core_session_t *session)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
2008-01-28 07:26:10 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s CHANNEL EXECUTE\n", switch_channel_get_name(switch_core_session_get_channel(session)));
|
2006-01-03 22:13:59 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2009-04-10 17:43:18 +00:00
|
|
|
static switch_status_t channel_on_destroy(switch_core_session_t *session)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
2008-01-28 07:26:10 +00:00
|
|
|
private_t *tech_pvt = switch_core_session_get_private(session);
|
2006-01-03 22:13:59 +00:00
|
|
|
|
2007-12-12 23:21:45 +00:00
|
|
|
switch_assert(tech_pvt != NULL);
|
2006-01-03 22:13:59 +00:00
|
|
|
|
2009-04-09 17:17:12 +00:00
|
|
|
if (switch_core_codec_ready(&tech_pvt->read_codec)) {
|
2006-01-06 23:01:28 +00:00
|
|
|
switch_core_codec_destroy(&tech_pvt->read_codec);
|
|
|
|
}
|
|
|
|
|
2009-04-09 17:17:12 +00:00
|
|
|
if (!switch_core_codec_ready(&tech_pvt->write_codec)) {
|
2006-01-06 23:01:28 +00:00
|
|
|
switch_core_codec_destroy(&tech_pvt->write_codec);
|
|
|
|
}
|
2006-01-03 22:13:59 +00:00
|
|
|
|
2009-04-10 17:43:18 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static switch_status_t channel_on_hangup(switch_core_session_t *session)
|
|
|
|
{
|
|
|
|
private_t *tech_pvt = switch_core_session_get_private(session);
|
|
|
|
|
|
|
|
switch_assert(tech_pvt != NULL);
|
|
|
|
|
|
|
|
switch_clear_flag_locked(tech_pvt, TFLAG_IO);
|
|
|
|
switch_clear_flag_locked(tech_pvt, TFLAG_VOICE);
|
|
|
|
switch_clear_flag_locked(tech_pvt, TFLAG_CODEC);
|
|
|
|
|
2008-03-31 15:31:05 +00:00
|
|
|
switch_mutex_lock(globals.mutex);
|
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");
|
2006-06-23 16:59:47 +00:00
|
|
|
switch_set_flag_locked(tech_pvt, TFLAG_HANGUP);
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
|
|
|
iax_session_destroy(&tech_pvt->iax_session);
|
|
|
|
}
|
|
|
|
|
2008-01-28 07:26:10 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s CHANNEL HANGUP\n", switch_channel_get_name(switch_core_session_get_channel(session)));
|
2006-09-13 19:02:22 +00:00
|
|
|
globals.calls--;
|
|
|
|
if (globals.calls < 0) {
|
|
|
|
globals.calls = 0;
|
|
|
|
}
|
|
|
|
switch_mutex_unlock(globals.mutex);
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2006-01-03 22:13:59 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-04-29 23:43:28 +00:00
|
|
|
static switch_status_t channel_kill_channel(switch_core_session_t *session, int sig)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
2008-01-28 07:26:10 +00:00
|
|
|
private_t *tech_pvt = switch_core_session_get_private(session);
|
2007-12-12 23:21:45 +00:00
|
|
|
switch_assert(tech_pvt != NULL);
|
2006-01-03 22:13:59 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
switch (sig) {
|
|
|
|
case SWITCH_SIG_KILL:
|
|
|
|
switch_clear_flag_locked(tech_pvt, TFLAG_IO);
|
|
|
|
switch_clear_flag_locked(tech_pvt, TFLAG_VOICE);
|
2008-01-28 07:26:10 +00:00
|
|
|
switch_channel_hangup(switch_core_session_get_channel(session), SWITCH_CAUSE_NORMAL_CLEARING);
|
2007-03-29 22:31:56 +00:00
|
|
|
break;
|
|
|
|
case SWITCH_SIG_BREAK:
|
|
|
|
switch_set_flag_locked(tech_pvt, TFLAG_BREAK);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-01-03 22:13:59 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-05-05 15:30:55 +00:00
|
|
|
static switch_status_t channel_on_exchange_media(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;
|
|
|
|
}
|
|
|
|
|
2008-05-05 15:30:55 +00:00
|
|
|
static switch_status_t channel_on_soft_execute(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;
|
|
|
|
}
|
|
|
|
|
2007-12-22 00:32:20 +00:00
|
|
|
static switch_status_t channel_send_dtmf(switch_core_session_t *session, const switch_dtmf_t *dtmf)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
2008-01-28 07:26:10 +00:00
|
|
|
private_t *tech_pvt = switch_core_session_get_private(session);
|
2007-12-12 23:21:45 +00:00
|
|
|
switch_assert(tech_pvt != NULL);
|
2006-01-03 22:13:59 +00:00
|
|
|
if (tech_pvt->iax_session) {
|
2008-03-31 15:31:05 +00:00
|
|
|
switch_mutex_lock(globals.mutex);
|
2007-12-22 00:32:20 +00:00
|
|
|
iax_send_dtmf(tech_pvt->iax_session, dtmf->digit);
|
2008-03-31 15:31:05 +00:00
|
|
|
switch_mutex_unlock(globals.mutex);
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-05-08 19:19:47 +00:00
|
|
|
static switch_status_t channel_read_frame(switch_core_session_t *session, switch_frame_t **frame, switch_io_flag_t flags, int stream_id)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
2008-01-28 07:26:10 +00:00
|
|
|
private_t *tech_pvt = switch_core_session_get_private(session);
|
2007-03-21 21:23:40 +00:00
|
|
|
switch_byte_t *data;
|
2008-06-24 22:43:58 +00:00
|
|
|
int ms_count = 0;
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2007-12-12 23:21:45 +00:00
|
|
|
switch_assert(tech_pvt != NULL);
|
2007-03-29 22:31:56 +00:00
|
|
|
tech_pvt->read_frame.flags = SFF_NONE;
|
|
|
|
*frame = NULL;
|
2007-01-19 01:26:22 +00:00
|
|
|
|
2006-01-14 16:01:52 +00:00
|
|
|
while (switch_test_flag(tech_pvt, TFLAG_IO)) {
|
2007-03-29 22:31:56 +00:00
|
|
|
|
|
|
|
if (!switch_test_flag(tech_pvt, TFLAG_CODEC)) {
|
2008-11-14 23:31:21 +00:00
|
|
|
switch_cond_next();
|
2007-03-29 22:31:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-01-02 19:31:06 +00:00
|
|
|
if (switch_test_flag(tech_pvt, TFLAG_BREAK)) {
|
2007-03-29 22:31:56 +00:00
|
|
|
switch_clear_flag(tech_pvt, TFLAG_BREAK);
|
|
|
|
goto cng;
|
|
|
|
}
|
2006-12-30 17:22:17 +00:00
|
|
|
|
2006-01-14 16:01:52 +00:00
|
|
|
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
|
|
|
|
2008-03-19 17:56:27 +00:00
|
|
|
if (switch_test_flag(tech_pvt, TFLAG_IO) && switch_test_flag(tech_pvt, TFLAG_DTMF)) {
|
|
|
|
switch_clear_flag_locked(tech_pvt, TFLAG_DTMF);
|
|
|
|
*frame = &tech_pvt->cng_frame;
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-01-14 16:01:52 +00:00
|
|
|
if (switch_test_flag(tech_pvt, TFLAG_IO) && switch_test_flag(tech_pvt, TFLAG_VOICE)) {
|
2006-06-23 16:59:47 +00:00
|
|
|
switch_clear_flag_locked(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-14 16:01:52 +00:00
|
|
|
*frame = &tech_pvt->read_frame;
|
2006-08-07 19:45:52 +00:00
|
|
|
#ifdef BIGENDIAN
|
|
|
|
if (switch_test_flag(tech_pvt, TFLAG_LINEAR)) {
|
|
|
|
switch_swap_linear((*frame)->data, (int) (*frame)->datalen);
|
|
|
|
}
|
|
|
|
#endif
|
2006-01-14 16:01:52 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
2006-03-21 00:20:24 +00:00
|
|
|
|
2008-11-14 23:31:21 +00:00
|
|
|
switch_cond_next();
|
2008-06-24 22:43:58 +00:00
|
|
|
if (++ms_count >= 30000) {
|
|
|
|
break;
|
|
|
|
}
|
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;
|
2007-01-19 01:26:22 +00:00
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
cng:
|
2007-03-21 21:23:40 +00:00
|
|
|
data = (switch_byte_t *) tech_pvt->read_frame.data;
|
|
|
|
data[0] = 65;
|
|
|
|
data[1] = 0;
|
2007-03-29 22:31:56 +00:00
|
|
|
tech_pvt->read_frame.datalen = 2;
|
|
|
|
tech_pvt->read_frame.flags = SFF_CNG;
|
|
|
|
*frame = &tech_pvt->read_frame;
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
|
|
|
|
2008-05-08 19:19:47 +00:00
|
|
|
static switch_status_t channel_write_frame(switch_core_session_t *session, switch_frame_t *frame, switch_io_flag_t flags, int stream_id)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
2008-01-28 07:26:10 +00:00
|
|
|
private_t *tech_pvt = switch_core_session_get_private(session);
|
2007-12-12 23:21:45 +00:00
|
|
|
switch_assert(tech_pvt != NULL);
|
2006-01-03 22:13:59 +00:00
|
|
|
|
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;
|
|
|
|
}
|
2006-08-07 19:45:52 +00:00
|
|
|
#ifdef BIGENDIAN
|
2006-01-03 22:13:59 +00:00
|
|
|
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
|
2008-10-20 17:48:42 +00:00
|
|
|
iax_send_voice(tech_pvt->iax_session, tech_pvt->codec, frame->data, (int) frame->datalen, tech_pvt->write_codec.implementation->samples_per_packet);
|
2006-01-03 22:13:59 +00:00
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-04-29 23:43:28 +00:00
|
|
|
static switch_status_t channel_answer_channel(switch_core_session_t *session)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
2008-01-28 07:26:10 +00:00
|
|
|
private_t *tech_pvt = switch_core_session_get_private(session);
|
2007-12-12 23:21:45 +00:00
|
|
|
switch_assert(tech_pvt != NULL);
|
2006-01-03 22:13:59 +00:00
|
|
|
|
2006-01-12 20:15:38 +00:00
|
|
|
if (!switch_test_flag(tech_pvt, TFLAG_OUTBOUND)) {
|
2008-03-31 15:31:05 +00:00
|
|
|
switch_mutex_lock(globals.mutex);
|
2006-01-12 20:14:06 +00:00
|
|
|
iax_answer(tech_pvt->iax_session);
|
2008-03-31 15:31:05 +00:00
|
|
|
switch_mutex_unlock(globals.mutex);
|
2006-01-12 20:14:06 +00:00
|
|
|
}
|
2006-01-03 22:13:59 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2007-04-16 16:53:30 +00:00
|
|
|
|
|
|
|
static switch_status_t channel_receive_message(switch_core_session_t *session, switch_core_session_message_t *msg)
|
|
|
|
{
|
2008-01-28 07:26:10 +00:00
|
|
|
private_t *tech_pvt = (private_t *) switch_core_session_get_private(session);
|
2007-12-12 23:21:45 +00:00
|
|
|
switch_assert(tech_pvt != NULL);
|
2007-04-16 16:53:30 +00:00
|
|
|
|
|
|
|
switch (msg->message_id) {
|
2008-01-03 00:50:53 +00:00
|
|
|
case SWITCH_MESSAGE_INDICATE_RESPOND:
|
2007-08-09 17:57:14 +00:00
|
|
|
{
|
|
|
|
if (tech_pvt->iax_session) {
|
2008-03-31 15:31:05 +00:00
|
|
|
switch_mutex_lock(globals.mutex);
|
2007-08-09 17:57:14 +00:00
|
|
|
iax_reject(tech_pvt->iax_session, msg->string_arg ? msg->string_arg : "Call Rejected");
|
2007-08-09 18:20:58 +00:00
|
|
|
switch_set_flag(tech_pvt, TFLAG_HANGUP);
|
2008-03-31 15:31:05 +00:00
|
|
|
switch_mutex_unlock(globals.mutex);
|
2007-08-09 17:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2007-04-16 16:53:30 +00:00
|
|
|
case SWITCH_MESSAGE_INDICATE_ANSWER:
|
2007-08-09 17:57:14 +00:00
|
|
|
{
|
|
|
|
channel_answer_channel(session);
|
|
|
|
}
|
2007-04-16 16:53:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
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
|
2007-10-17 14:59:17 +00:00
|
|
|
that allocate memory or you will have 1 channel with memory allocated from another channel's pool!
|
2006-02-20 06:13:56 +00:00
|
|
|
*/
|
2008-05-15 19:29:35 +00:00
|
|
|
static switch_call_cause_t channel_outgoing_channel(switch_core_session_t *session, switch_event_t *var_event,
|
2007-03-29 22:31:56 +00:00
|
|
|
switch_caller_profile_t *outbound_profile,
|
2007-12-20 21:42:00 +00:00
|
|
|
switch_core_session_t **new_session, switch_memory_pool_t **pool, switch_originate_flag_t flags)
|
2006-02-20 06:13:56 +00:00
|
|
|
{
|
2009-02-23 16:31:59 +00:00
|
|
|
if ((*new_session = switch_core_session_request(iax_endpoint_interface, SWITCH_CALL_DIRECTION_OUTBOUND, pool)) != 0) {
|
2007-01-19 01:26:22 +00:00
|
|
|
private_t *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);
|
2007-01-19 01:26:22 +00:00
|
|
|
if ((tech_pvt = (private_t *) switch_core_session_alloc(*new_session, sizeof(private_t))) != 0) {
|
2006-02-20 06:13:56 +00:00
|
|
|
channel = switch_core_session_get_channel(*new_session);
|
2007-03-29 22:31:56 +00:00
|
|
|
tech_init(tech_pvt, *new_session);
|
2006-02-20 06:13:56 +00:00
|
|
|
} 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);
|
2007-02-09 20:03:07 +00:00
|
|
|
return SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER;
|
2006-02-20 06:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (outbound_profile) {
|
|
|
|
char name[128];
|
2006-02-22 02:50:33 +00:00
|
|
|
|
2007-12-12 21:53:32 +00:00
|
|
|
switch_snprintf(name, sizeof(name), "IAX/%s-%04x", outbound_profile->destination_number, rand() & 0xffff);
|
2006-02-22 02:50:33 +00:00
|
|
|
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);
|
2007-02-13 21:54:16 +00:00
|
|
|
return SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER;
|
2006-02-20 06:13:56 +00:00
|
|
|
}
|
|
|
|
|
2008-03-31 15:31:05 +00:00
|
|
|
switch_mutex_lock(globals.mutex);
|
|
|
|
tech_pvt->iax_session = iax_session_new();
|
|
|
|
switch_mutex_unlock(globals.mutex);
|
|
|
|
|
|
|
|
if (!tech_pvt->iax_session) {
|
2006-02-20 06:13:56 +00:00
|
|
|
switch_core_session_destroy(new_session);
|
2007-02-09 20:03:07 +00:00
|
|
|
return SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER;
|
2006-02-20 06:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (iax_set_codec(tech_pvt, tech_pvt->iax_session, &req, &cap, &samprate, IAX_QUERY) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_core_session_destroy(new_session);
|
2007-02-09 20:03:07 +00:00
|
|
|
return SWITCH_CAUSE_BEARERCAPABILITY_NOTIMPL;
|
2006-02-20 06:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (samprate) {
|
|
|
|
iax_set_samplerate(tech_pvt->iax_session, samprate);
|
|
|
|
}
|
|
|
|
|
|
|
|
iax_call(tech_pvt->iax_session,
|
2007-03-30 00:13:31 +00:00
|
|
|
caller_profile->caller_id_number, caller_profile->caller_id_name, caller_profile->destination_number, NULL, 0, req, cap);
|
2006-02-20 06:13:56 +00:00
|
|
|
|
|
|
|
switch_channel_set_flag(channel, CF_OUTBOUND);
|
2006-06-23 16:59:47 +00:00
|
|
|
switch_set_flag_locked(tech_pvt, TFLAG_OUTBOUND);
|
2006-02-20 06:13:56 +00:00
|
|
|
switch_channel_set_state(channel, CS_INIT);
|
2007-02-09 20:03:07 +00:00
|
|
|
return SWITCH_CAUSE_SUCCESS;
|
2006-02-20 06:13:56 +00:00
|
|
|
}
|
|
|
|
|
2007-02-09 20:03:07 +00:00
|
|
|
return SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER;
|
2006-02-20 06:13:56 +00:00
|
|
|
}
|
2007-06-13 20:40:06 +00:00
|
|
|
|
2007-11-16 19:48:02 +00:00
|
|
|
static switch_status_t channel_receive_event(switch_core_session_t *session, switch_event_t *event)
|
|
|
|
{
|
2008-01-28 07:26:10 +00:00
|
|
|
struct private_object *tech_pvt = switch_core_session_get_private(session);
|
|
|
|
char *body = switch_event_get_body(event);
|
2007-11-16 19:48:02 +00:00
|
|
|
|
2007-12-12 23:21:45 +00:00
|
|
|
switch_assert(tech_pvt != NULL);
|
2007-11-16 19:48:02 +00:00
|
|
|
|
2008-01-28 07:26:10 +00:00
|
|
|
if (!body) {
|
2007-11-16 19:48:02 +00:00
|
|
|
body = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
iax_send_text(tech_pvt->iax_session, body);
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-29 18:10:06 +00:00
|
|
|
switch_state_handler_table_t iax_state_handlers = {
|
2007-06-13 20:40:06 +00:00
|
|
|
/*.on_init */ channel_on_init,
|
2008-05-05 15:30:55 +00:00
|
|
|
/*.on_routing */ channel_on_routing,
|
2007-06-13 20:40:06 +00:00
|
|
|
/*.on_execute */ channel_on_execute,
|
|
|
|
/*.on_hangup */ channel_on_hangup,
|
2008-05-05 15:30:55 +00:00
|
|
|
/*.on_exchange_media */ channel_on_exchange_media,
|
2009-04-10 17:43:18 +00:00
|
|
|
/*.on_soft_execute */ channel_on_soft_execute,
|
|
|
|
/*.on_consume_media*/ NULL,
|
|
|
|
/*.on_hibernate*/ NULL,
|
|
|
|
/*.on_reset*/ NULL,
|
|
|
|
/*.on_park*/ NULL,
|
|
|
|
/*.on_reporting*/ NULL,
|
|
|
|
/*.on_destroy*/ channel_on_destroy
|
2007-06-13 20:40:06 +00:00
|
|
|
};
|
|
|
|
|
2007-10-29 18:10:06 +00:00
|
|
|
switch_io_routines_t iax_io_routines = {
|
2007-06-13 20:40:06 +00:00
|
|
|
/*.outgoing_channel */ channel_outgoing_channel,
|
|
|
|
/*.read_frame */ channel_read_frame,
|
|
|
|
/*.write_frame */ channel_write_frame,
|
|
|
|
/*.kill_channel */ channel_kill_channel,
|
|
|
|
/*.send_dtmf */ channel_send_dtmf,
|
2008-05-27 04:54:52 +00:00
|
|
|
/*.receive_message */ channel_receive_message,
|
2007-11-16 19:48:02 +00:00
|
|
|
/*.receive_event */ channel_receive_event
|
2007-06-13 20:40:06 +00:00
|
|
|
};
|
|
|
|
|
2007-06-13 14:35:55 +00:00
|
|
|
SWITCH_MODULE_LOAD_FUNCTION(mod_iax_load)
|
2006-01-20 15:05:05 +00:00
|
|
|
{
|
2009-02-23 19:17:24 +00:00
|
|
|
memset(&globals, 0, sizeof(globals));
|
|
|
|
|
2007-06-13 20:40:06 +00:00
|
|
|
module_pool = pool;
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2007-06-13 20:40:06 +00:00
|
|
|
*module_interface = switch_loadable_module_create_module_interface(pool, modname);
|
2007-10-29 18:10:06 +00:00
|
|
|
iax_endpoint_interface = switch_loadable_module_create_interface(*module_interface, SWITCH_ENDPOINT_INTERFACE);
|
|
|
|
iax_endpoint_interface->interface_name = "iax";
|
|
|
|
iax_endpoint_interface->io_routines = &iax_io_routines;
|
|
|
|
iax_endpoint_interface->state_handler = &iax_state_handlers;
|
2006-01-03 22:13:59 +00:00
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
2006-04-29 23:43:28 +00:00
|
|
|
static switch_status_t load_config(void)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
|
|
|
char *cf = "iax.conf";
|
2006-05-10 03:23:05 +00:00
|
|
|
switch_xml_t cfg, xml, settings, param;
|
2006-01-03 22:13:59 +00:00
|
|
|
|
|
|
|
memset(&globals, 0, sizeof(globals));
|
2006-09-13 19:02:22 +00:00
|
|
|
switch_mutex_init(&globals.mutex, SWITCH_MUTEX_NESTED, module_pool);
|
2006-05-10 15:47:54 +00:00
|
|
|
if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) {
|
2008-10-11 06:19:56 +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;
|
|
|
|
}
|
|
|
|
|
2006-05-10 03:23:05 +00:00
|
|
|
if ((settings = switch_xml_child(cfg, "settings"))) {
|
|
|
|
for (param = switch_xml_child(settings, "param"); param; param = param->next) {
|
2006-05-17 00:58:21 +00:00
|
|
|
char *var = (char *) switch_xml_attr_soft(param, "name");
|
|
|
|
char *val = (char *) switch_xml_attr_soft(param, "value");
|
2006-05-10 03:23:05 +00:00
|
|
|
|
2006-01-03 22:13:59 +00:00
|
|
|
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-05-16 17:08:21 +00:00
|
|
|
} else if (!strcmp(var, "codec-master")) {
|
2006-01-03 22:13:59 +00:00
|
|
|
if (!strcasecmp(val, "us")) {
|
|
|
|
switch_set_flag(&globals, GFLAG_MY_CODEC_PREFS);
|
|
|
|
}
|
|
|
|
} else if (!strcmp(var, "dialplan")) {
|
|
|
|
set_global_dialplan(val);
|
2006-05-16 17:08:21 +00:00
|
|
|
} else if (!strcmp(var, "codec-prefs")) {
|
2006-01-03 22:13:59 +00:00
|
|
|
set_global_codec_string(val);
|
2007-03-30 00:13:31 +00:00
|
|
|
globals.codec_order_last = switch_separate_string(globals.codec_string, ',', globals.codec_order, SWITCH_MAX_CODECS);
|
2006-05-16 17:08:21 +00:00
|
|
|
} else if (!strcmp(var, "codec-rates")) {
|
2006-01-12 17:51:08 +00:00
|
|
|
set_global_codec_rates_string(val);
|
2007-03-30 00:13:31 +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");
|
|
|
|
}
|
2006-05-10 03:23:05 +00:00
|
|
|
|
2006-01-03 22:13:59 +00:00
|
|
|
if (!globals.port) {
|
|
|
|
globals.port = 4569;
|
|
|
|
}
|
|
|
|
|
2006-05-10 03:23:05 +00:00
|
|
|
switch_xml_free(xml);
|
2006-01-03 22:13:59 +00:00
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
static switch_status_t tech_media(private_t *tech_pvt, struct iax_event *iaxevent)
|
2007-01-19 01:26:22 +00:00
|
|
|
{
|
2007-03-29 22:31:56 +00:00
|
|
|
unsigned int cap = iax_session_get_capability(iaxevent->session);
|
|
|
|
unsigned int format = iaxevent->ies.format;
|
2007-01-19 01:26:22 +00:00
|
|
|
switch_status_t status = SWITCH_STATUS_SUCCESS;
|
2007-03-29 22:31:56 +00:00
|
|
|
|
|
|
|
if (!switch_test_flag(tech_pvt, TFLAG_CODEC) &&
|
2007-03-30 00:13:31 +00:00
|
|
|
(status = iax_set_codec(tech_pvt, iaxevent->session, &format, &cap, &iaxevent->ies.samprate, IAX_SET)) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Codec Error %u %u\n", iaxevent->ies.format, iaxevent->ies.capability);
|
2007-03-29 22:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
2007-01-19 01:26:22 +00:00
|
|
|
}
|
2006-01-03 22:13:59 +00:00
|
|
|
|
2007-06-13 14:35:55 +00:00
|
|
|
SWITCH_MODULE_RUNTIME_FUNCTION(mod_iax_runtime)
|
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-09-11 22:52:19 +00:00
|
|
|
globals.fd = 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) {
|
2008-08-16 02:19:43 +00:00
|
|
|
switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "service", "_iax2._udp");
|
2006-02-14 16:42:26 +00:00
|
|
|
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-09-13 19:02:22 +00:00
|
|
|
if ((iaxevent = iax_get_event(0)) == NULL) {
|
|
|
|
int waitlen = 0;
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2006-09-13 19:02:22 +00:00
|
|
|
if (globals.calls == 0) {
|
|
|
|
waitlen = 10000;
|
|
|
|
} else if (globals.calls < 10) {
|
2006-09-19 15:33:02 +00:00
|
|
|
waitlen = 1000;
|
2006-09-13 19:02:22 +00:00
|
|
|
} else {
|
2006-09-19 15:33:02 +00:00
|
|
|
waitlen = 500;
|
2006-09-13 19:02:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch_yield(waitlen);
|
|
|
|
continue;
|
2006-08-14 19:03:36 +00:00
|
|
|
} else {
|
2007-01-19 01:26:22 +00:00
|
|
|
private_t *tech_pvt = NULL;
|
2007-03-29 22:31:56 +00:00
|
|
|
switch_channel_t *channel = NULL;
|
|
|
|
|
|
|
|
if ((tech_pvt = iax_get_private(iaxevent->session))) {
|
|
|
|
channel = switch_core_session_get_channel(tech_pvt->session);
|
|
|
|
}
|
2006-01-03 22:13:59 +00:00
|
|
|
|
|
|
|
if (globals.debug && iaxevent->etype != IAX_EVENT_VOICE) {
|
2007-03-30 00:13:31 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Event %d [%s]!\n", iaxevent->etype, IAXNAMES[iaxevent->etype]);
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
2006-04-18 16:20:47 +00:00
|
|
|
|
2008-03-31 15:31:05 +00:00
|
|
|
switch_mutex_lock(globals.mutex);
|
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;
|
2007-05-12 02:09:59 +00:00
|
|
|
case IAX_EVENT_REGREQ:
|
|
|
|
/* what is the right way to handle this? */
|
2007-05-12 02:40:32 +00:00
|
|
|
iax_reject_registration(iaxevent->session, NULL);
|
2007-05-12 02:09:59 +00:00
|
|
|
break;
|
2006-01-20 15:05:05 +00:00
|
|
|
case IAX_EVENT_TIMEOUT:
|
|
|
|
break;
|
|
|
|
case IAX_EVENT_ACCEPT:
|
2007-01-19 01:36:20 +00:00
|
|
|
if (channel && !switch_channel_test_flag(channel, CF_ANSWERED)) {
|
2007-03-29 22:31:56 +00:00
|
|
|
if (tech_media(tech_pvt, iaxevent) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_channel_mark_pre_answered(channel);
|
|
|
|
} else {
|
|
|
|
switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
|
|
|
|
}
|
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:
|
2007-02-13 15:47:15 +00:00
|
|
|
if (channel) {
|
|
|
|
switch_core_session_queue_indication(tech_pvt->session, SWITCH_MESSAGE_INDICATE_RINGING);
|
|
|
|
switch_channel_mark_ring_ready(channel);
|
|
|
|
}
|
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
|
2007-01-19 01:26:22 +00:00
|
|
|
if (channel) {
|
2007-03-29 22:31:56 +00:00
|
|
|
if (tech_media(tech_pvt, iaxevent) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
if (switch_channel_test_flag(channel, CF_ANSWERED)) {
|
2008-09-02 03:46:27 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Multiple Answer %s?\n", switch_channel_get_name(channel));
|
2007-03-29 22:31:56 +00:00
|
|
|
|
|
|
|
} else {
|
2007-03-30 00:13:31 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Answer %s\n", switch_channel_get_name(channel));
|
2007-03-29 22:31:56 +00:00
|
|
|
switch_channel_mark_answered(channel);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER);
|
|
|
|
}
|
|
|
|
}
|
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,
|
2007-03-29 22:31:56 +00:00
|
|
|
"Incoming call connected %s, %s, %s %u/%u\n",
|
|
|
|
iaxevent->ies.called_number,
|
2007-03-30 00:13:31 +00:00
|
|
|
iaxevent->ies.calling_number, iaxevent->ies.calling_name, iaxevent->ies.format, iaxevent->ies.capability);
|
2006-01-20 15:05:05 +00:00
|
|
|
|
|
|
|
if (iaxevent) {
|
2006-04-29 06:05:03 +00:00
|
|
|
switch_core_session_t *session;
|
2006-01-20 15:05:05 +00:00
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "New Inbound Channel %s!\n", iaxevent->ies.calling_name);
|
2009-02-23 16:31:59 +00:00
|
|
|
if ((session = switch_core_session_request(iax_endpoint_interface, SWITCH_CALL_DIRECTION_INBOUND, NULL)) != 0) {
|
2006-01-20 15:05:05 +00:00
|
|
|
switch_core_session_add_stream(session, NULL);
|
2007-01-19 01:26:22 +00:00
|
|
|
if ((tech_pvt = (private_t *) switch_core_session_alloc(session, sizeof(private_t))) != 0) {
|
2006-01-20 15:05:05 +00:00
|
|
|
channel = switch_core_session_get_channel(session);
|
2007-03-29 22:31:56 +00:00
|
|
|
tech_init(tech_pvt, session);
|
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_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-03-01 22:55:28 +00:00
|
|
|
if ((tech_pvt->caller_profile = switch_caller_profile_new(switch_core_session_get_pool(session),
|
2006-05-26 16:00:08 +00:00
|
|
|
iaxevent->ies.username,
|
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,
|
2007-03-29 22:31:56 +00:00
|
|
|
NULL,
|
2007-08-28 22:44:02 +00:00
|
|
|
modname,
|
2008-05-27 04:54:52 +00:00
|
|
|
iaxevent->ies.called_context, iaxevent->ies.called_number)) != 0) {
|
2006-01-20 15:05:05 +00:00
|
|
|
char name[128];
|
2007-12-12 21:53:32 +00:00
|
|
|
switch_snprintf(name, sizeof(name), "IAX/%s-%04x", tech_pvt->caller_profile->destination_number, rand() & 0xffff);
|
2006-01-20 15:05:05 +00:00
|
|
|
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,
|
2007-03-30 00:13:31 +00:00
|
|
|
&iaxevent->ies.format, &iaxevent->ies.capability, &iaxevent->ies.samprate, IAX_SET) != SWITCH_STATUS_SUCCESS) {
|
2006-01-20 15:05:05 +00:00
|
|
|
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);
|
2007-05-11 00:27:55 +00:00
|
|
|
if (switch_core_session_thread_launch(session) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error spawning thread\n");
|
|
|
|
switch_core_session_destroy(&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:
|
2007-01-19 01:26:22 +00:00
|
|
|
if (channel) {
|
2008-03-31 17:14:49 +00:00
|
|
|
switch_mutex_lock(globals.mutex);
|
2006-06-23 16:59:47 +00:00
|
|
|
switch_mutex_lock(tech_pvt->flag_mutex);
|
2006-01-20 15:05:05 +00:00
|
|
|
switch_clear_flag(tech_pvt, TFLAG_IO);
|
|
|
|
switch_clear_flag(tech_pvt, TFLAG_VOICE);
|
2006-06-23 16:59:47 +00:00
|
|
|
switch_mutex_unlock(tech_pvt->flag_mutex);
|
|
|
|
|
2008-05-27 04:54:52 +00:00
|
|
|
|
2007-03-30 00:13:31 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Hangup %s\n", switch_channel_get_name(channel));
|
2007-03-29 22:31:56 +00:00
|
|
|
switch_set_flag_locked(tech_pvt, TFLAG_HANGUP);
|
2007-03-30 00:13:31 +00:00
|
|
|
switch_channel_hangup(channel, iaxevent->etype == IAX_EVENT_HANGUP ? SWITCH_CAUSE_NORMAL_CLEARING : SWITCH_CAUSE_FACILITY_REJECTED);
|
2007-03-29 22:31:56 +00:00
|
|
|
iaxevent->session = NULL;
|
2008-03-31 17:14:49 +00:00
|
|
|
switch_mutex_unlock(globals.mutex);
|
2006-01-20 15:05:05 +00:00
|
|
|
}
|
|
|
|
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) {
|
2009-02-25 03:41:18 +00:00
|
|
|
if (channel && switch_channel_up(channel)) {
|
2008-12-30 18:23:42 +00:00
|
|
|
int bytes = 0, frames = 1;
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2009-04-09 17:17:12 +00:00
|
|
|
if (!switch_test_flag(tech_pvt, TFLAG_CODEC) || !tech_pvt->read_codec.implementation ||
|
|
|
|
!switch_core_codec_ready(&tech_pvt->read_codec)) {
|
2008-12-30 18:23:42 +00:00
|
|
|
switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING);
|
2006-04-18 16:20:47 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-12-30 18:23:42 +00:00
|
|
|
|
2008-10-20 17:48:42 +00:00
|
|
|
if (tech_pvt->read_codec.implementation->encoded_bytes_per_packet) {
|
|
|
|
bytes = tech_pvt->read_codec.implementation->encoded_bytes_per_packet;
|
2006-07-18 18:34:42 +00:00
|
|
|
frames = (int) (tech_pvt->read_frame.datalen / bytes);
|
|
|
|
}
|
2006-04-18 16:20:47 +00:00
|
|
|
|
2008-10-20 17:48:42 +00:00
|
|
|
tech_pvt->read_frame.samples = frames * tech_pvt->read_codec.implementation->samples_per_packet;
|
2006-01-20 15:05:05 +00:00
|
|
|
memcpy(tech_pvt->read_frame.data, iaxevent->data, iaxevent->datalen);
|
|
|
|
/* wake up the i/o thread */
|
2006-06-23 16:59:47 +00:00
|
|
|
switch_set_flag_locked(tech_pvt, TFLAG_VOICE);
|
2006-01-20 15:05:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
break;
|
|
|
|
case IAX_EVENT_DTMF:
|
2007-01-19 01:26:22 +00:00
|
|
|
if (channel) {
|
2008-05-27 04:54:52 +00:00
|
|
|
switch_dtmf_t dtmf = { (char) iaxevent->subclass, switch_core_default_dtmf_duration(0) };
|
2007-03-29 22:31:56 +00:00
|
|
|
if (globals.debug) {
|
2007-12-22 00:32:20 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%c DTMF %s\n", dtmf.digit, switch_channel_get_name(channel));
|
2007-03-29 22:31:56 +00:00
|
|
|
}
|
2008-03-19 17:56:27 +00:00
|
|
|
switch_mutex_lock(tech_pvt->flag_mutex);
|
2007-12-22 00:32:20 +00:00
|
|
|
switch_channel_queue_dtmf(channel, &dtmf);
|
2008-03-19 17:56:27 +00:00
|
|
|
switch_set_flag(tech_pvt, TFLAG_DTMF);
|
|
|
|
switch_mutex_unlock(tech_pvt->flag_mutex);
|
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:
|
2007-03-30 00:13:31 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Don't know what to do with IAX event %d.\n", iaxevent->etype);
|
2006-01-20 15:05:05 +00:00
|
|
|
break;
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
|
|
|
iax_event_free(iaxevent);
|
2008-05-27 04:54:52 +00:00
|
|
|
switch_mutex_unlock(globals.mutex);
|
2006-01-03 22:13:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
running = 0;
|
|
|
|
|
|
|
|
return SWITCH_STATUS_TERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-13 14:35:55 +00:00
|
|
|
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_iax_shutdown)
|
2006-01-03 22:13:59 +00:00
|
|
|
{
|
|
|
|
int x = 0;
|
|
|
|
|
|
|
|
running = -1;
|
|
|
|
|
2006-09-11 22:52:19 +00:00
|
|
|
if (globals.fd) {
|
2007-03-29 22:31:56 +00:00
|
|
|
/* Die Mutha $%#$@% Die Mutha $#%#$^ Die */
|
2006-09-11 22:52:19 +00:00
|
|
|
shutdown(globals.fd, 2);
|
|
|
|
}
|
2006-01-03 22:13:59 +00:00
|
|
|
iax_shutdown();
|
|
|
|
|
|
|
|
while (running) {
|
|
|
|
if (x++ > 100) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch_yield(20000);
|
|
|
|
}
|
2009-02-23 19:17:24 +00:00
|
|
|
|
|
|
|
switch_safe_free(globals.dialplan);
|
|
|
|
switch_safe_free(globals.codec_string);
|
|
|
|
switch_safe_free(globals.codec_rates_string);
|
|
|
|
switch_safe_free(globals.ip);
|
|
|
|
|
2006-01-03 22:13:59 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
2006-11-27 22:30:48 +00:00
|
|
|
|
|
|
|
/* For Emacs:
|
|
|
|
* Local Variables:
|
|
|
|
* mode:c
|
2008-02-03 22:14:57 +00:00
|
|
|
* indent-tabs-mode:t
|
2006-11-27 22:30:48 +00:00
|
|
|
* tab-width:4
|
|
|
|
* c-basic-offset:4
|
|
|
|
* End:
|
|
|
|
* For VIM:
|
2008-07-03 19:12:26 +00:00
|
|
|
* vim:set softtabstop=4 shiftwidth=4 tabstop=4:
|
2006-11-27 22:30:48 +00:00
|
|
|
*/
|