git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@418 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Anthony Minessale
2006-01-20 02:02:03 +00:00
parent 1e04eb13b5
commit 0ea203849f
43 changed files with 3940 additions and 4103 deletions
+299 -298
View File
@@ -61,7 +61,7 @@ typedef enum {
TFLAG_DTMF = (1 << 3),
TFLAG_READING = (1 << 4),
TFLAG_WRITING = (1 << 5),
TFLAG_USING_CODEC = (1 << 6),
TFLAG_USING_CODEC = (1 << 6),
TFLAG_RTP = (1 << 7),
TFLAG_BYE = (1 << 8)
} TFLAGS;
@@ -87,7 +87,7 @@ static struct {
} globals;
struct private_object {
unsigned int flags;
unsigned int flags;
switch_core_session *session;
switch_frame read_frame;
switch_codec read_codec;
@@ -144,14 +144,11 @@ static switch_status exosip_on_init(switch_core_session *session);
static switch_status exosip_on_hangup(switch_core_session *session);
static switch_status exosip_on_loopback(switch_core_session *session);
static switch_status exosip_on_transmit(switch_core_session *session);
static switch_status exosip_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile,
switch_core_session **new_session);
static switch_status exosip_read_frame(switch_core_session *session, switch_frame **frame, int timeout,
switch_io_flag flags, int stream_id);
static switch_status exosip_write_frame(switch_core_session *session, switch_frame *frame, int timeout,
switch_io_flag flags, int stream_id);
static switch_status exosip_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile, switch_core_session **new_session);
static switch_status exosip_read_frame(switch_core_session *session, switch_frame **frame, int timeout, switch_io_flag flags, int stream_id);
static switch_status exosip_write_frame(switch_core_session *session, switch_frame *frame, int timeout, switch_io_flag flags, int stream_id);
static int config_exosip(int reload);
static switch_status parse_sdp_media(sdp_media_t * media, char **dname, char **drate, char **dpayload);
static switch_status parse_sdp_media(sdp_media_t *media, char **dname, char **drate, char **dpayload);
static switch_status exosip_kill_channel(switch_core_session *session, int sig);
static void activate_rtp(struct private_object *tech_pvt);
static void deactivate_rtp(struct private_object *tech_pvt);
@@ -193,16 +190,16 @@ static int sdp_add_codec(struct osip_rfc3264 *cnf, int codec_type, int payload,
}
switch (codec_type) {
case SWITCH_CODEC_TYPE_AUDIO:
med->m_media = osip_strdup("audio");
osip_rfc3264_add_audio_media(cnf, med, -1);
break;
case SWITCH_CODEC_TYPE_VIDEO:
med->m_media = osip_strdup("video");
osip_rfc3264_add_video_media(cnf, med, -1);
break;
default:
break;
case SWITCH_CODEC_TYPE_AUDIO:
med->m_media = osip_strdup("audio");
osip_rfc3264_add_audio_media(cnf, med, -1);
break;
case SWITCH_CODEC_TYPE_VIDEO:
med->m_media = osip_strdup("video");
osip_rfc3264_add_video_media(cnf, med, -1);
break;
default:
break;
}
return 0;
}
@@ -235,7 +232,7 @@ static switch_status exosip_on_init(switch_core_session *session)
char *dest_uri;
switch_codec_interface *codecs[SWITCH_MAX_CODECS];
int num_codecs = 0;
/* do SIP Goodies... */
/* do SIP Goodies...*/
/* Generate callerid URI */
eXosip_guess_localip(AF_INET, localip, 128);
@@ -248,17 +245,14 @@ static switch_status exosip_on_init(switch_core_session *session)
/* Initialize SDP */
sdp_message_init(&tech_pvt->local_sdp);
sdp_message_v_version_set(tech_pvt->local_sdp, "0");
sdp_message_o_origin_set(tech_pvt->local_sdp, "OpenSWITCH2", "0", "0", "IN", "IP4",
tech_pvt->local_sdp_audio_ip);
sdp_message_o_origin_set(tech_pvt->local_sdp, "OpenSWITCH2", "0", "0", "IN", "IP4", tech_pvt->local_sdp_audio_ip);
sdp_message_s_name_set(tech_pvt->local_sdp, "SIP Call");
sdp_message_c_connection_add(tech_pvt->local_sdp, -1, "IN", "IP4", tech_pvt->local_sdp_audio_ip, NULL, NULL);
sdp_message_t_time_descr_add(tech_pvt->local_sdp, "0", "0");
snprintf(port, sizeof(port), "%i", tech_pvt->local_sdp_audio_port);
sdp_message_m_media_add(tech_pvt->local_sdp, "audio", port, NULL, "RTP/AVP");
/* Add in every codec we support on this outbound call */
if ((num_codecs =
switch_loadable_module_get_codecs(switch_core_session_get_pool(session), codecs,
sizeof(codecs) / sizeof(codecs[0]))) > 0) {
if ((num_codecs = switch_loadable_module_get_codecs(switch_core_session_get_pool(session), codecs, sizeof(codecs)/sizeof(codecs[0]))) > 0) {
int i;
static const switch_codec_implementation *imp;
for (i = 0; i < num_codecs; i++) {
@@ -266,14 +260,12 @@ static switch_status exosip_on_init(switch_core_session *session)
snprintf(tmp, sizeof(tmp), "%i", codecs[i]->ianacode);
sdp_message_m_payload_add(tech_pvt->local_sdp, 0, osip_strdup(tmp));
for (imp = codecs[i]->implementations; imp; imp = imp->next) {
for (imp = codecs[i]->implementations ; imp ; imp = imp->next) {
/* Add to SDP config */
sdp_add_codec(tech_pvt->sdp_config, codecs[i]->codec_type, codecs[i]->ianacode, codecs[i]->iananame,
imp->samples_per_second, x++);
sdp_add_codec(tech_pvt->sdp_config, codecs[i]->codec_type, codecs[i]->ianacode, codecs[i]->iananame, imp->samples_per_second, x++);
/* Add to SDP message */
snprintf(tmp, sizeof(tmp), "%i %s/%i", codecs[i]->ianacode, codecs[i]->iananame,
imp->samples_per_second);
snprintf(tmp, sizeof(tmp), "%i %s/%i", codecs[i]->ianacode, codecs[i]->iananame, imp->samples_per_second);
sdp_message_a_attribute_add(tech_pvt->local_sdp, 0, "rtpmap", osip_strdup(tmp));
memset(tmp, 0, sizeof(tmp));
}
@@ -281,9 +273,7 @@ static switch_status exosip_on_init(switch_core_session *session)
}
/* Setup our INVITE */
eXosip_lock();
if (!
(dest_uri =
(char *) switch_core_session_alloc(session, strlen(tech_pvt->caller_profile->destination_number) + 10))) {
if (!(dest_uri = (char *) switch_core_session_alloc(session, strlen(tech_pvt->caller_profile->destination_number) + 10))) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "AIEEEE!\n");
assert(dest_uri != NULL);
}
@@ -302,7 +292,7 @@ static switch_status exosip_on_init(switch_core_session *session)
switch_core_hash_insert(globals.call_hash, tech_pvt->call_id, tech_pvt);
tech_pvt->did = -1;
eXosip_unlock();
}
}
/* Let Media Work */
switch_set_flag(tech_pvt, TFLAG_IO);
@@ -341,7 +331,7 @@ static switch_status exosip_on_hangup(switch_core_session *session)
assert(tech_pvt != NULL);
switch_core_hash_delete(globals.call_hash, tech_pvt->call_id);
switch_core_hash_delete(globals.call_hash, tech_pvt->call_id);
switch_set_flag(tech_pvt, TFLAG_BYE);
@@ -356,8 +346,7 @@ static switch_status exosip_on_hangup(switch_core_session *session)
switch_core_codec_destroy(&tech_pvt->write_codec);
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "EXOSIP HANGUP %s %d/%d=%d\n", switch_channel_get_name(channel),
tech_pvt->cid, tech_pvt->did, i);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "EXOSIP HANGUP %s %d/%d=%d\n", switch_channel_get_name(channel), tech_pvt->cid, tech_pvt->did, i);
return SWITCH_STATUS_SUCCESS;
}
@@ -373,16 +362,14 @@ static switch_status exosip_on_transmit(switch_core_session *session)
return SWITCH_STATUS_SUCCESS;
}
static switch_status exosip_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile,
switch_core_session **new_session)
static switch_status exosip_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile, switch_core_session **new_session)
{
if ((*new_session = switch_core_session_request(&exosip_endpoint_interface, NULL))) {
struct private_object *tech_pvt;
switch_channel *channel;
switch_core_session_add_stream(*new_session, NULL);
if ((tech_pvt =
(struct private_object *) switch_core_session_alloc(*new_session, sizeof(struct private_object)))) {
if ((tech_pvt = (struct private_object *) switch_core_session_alloc(*new_session, sizeof(struct private_object)))) {
memset(tech_pvt, 0, sizeof(*tech_pvt));
channel = switch_core_session_get_channel(*new_session);
switch_core_session_set_private(*new_session, tech_pvt);
@@ -425,7 +412,7 @@ static void deactivate_rtp(struct private_object *tech_pvt)
if (tech_pvt->rtp_session) {
switch_mutex_lock(tech_pvt->rtp_lock);
while (loops < 10 && (switch_test_flag(tech_pvt, TFLAG_READING) || switch_test_flag(tech_pvt, TFLAG_WRITING))) {
while(loops < 10 && (switch_test_flag(tech_pvt, TFLAG_READING) || switch_test_flag(tech_pvt, TFLAG_WRITING))) {
switch_yield(10000);
loops++;
}
@@ -467,20 +454,25 @@ static void activate_rtp(struct private_object *tech_pvt)
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Activating RTP %s:%d->%s:%d codec: %d ms: %d\n",
tech_pvt->local_sdp_audio_ip,
tech_pvt->local_sdp_audio_port,
tech_pvt->remote_sdp_audio_ip,
tech_pvt->remote_sdp_audio_port, tech_pvt->read_codec.codec_interface->ianacode, ms);
tech_pvt->local_sdp_audio_ip,
tech_pvt->local_sdp_audio_port,
tech_pvt->remote_sdp_audio_ip,
tech_pvt->remote_sdp_audio_port,
tech_pvt->read_codec.codec_interface->ianacode,
ms
);
tech_pvt->rtp_session = jrtp4c_new(tech_pvt->local_sdp_audio_ip,
tech_pvt->local_sdp_audio_port,
tech_pvt->remote_sdp_audio_ip,
tech_pvt->remote_sdp_audio_port,
tech_pvt->read_codec.codec_interface->ianacode,
tech_pvt->read_codec.implementation->samples_per_second, &err);
tech_pvt->rtp_session = jrtp4c_new(
tech_pvt->local_sdp_audio_ip,
tech_pvt->local_sdp_audio_port,
tech_pvt->remote_sdp_audio_ip,
tech_pvt->remote_sdp_audio_port,
tech_pvt->read_codec.codec_interface->ianacode,
tech_pvt->read_codec.implementation->samples_per_second,
&err);
if (tech_pvt->rtp_session) {
tech_pvt->ssrc = jrtp4c_get_ssrc(tech_pvt->rtp_session);
@@ -519,21 +511,20 @@ static switch_status exosip_answer_channel(switch_core_session *session)
sdp_message_to_str(tech_pvt->local_sdp, &buf);
osip_message_set_body(answer, buf, strlen(buf));
osip_message_set_content_type(answer, "application/sdp");
free(buf);
free(buf);
eXosip_call_send_answer(tech_pvt->tid, 200, answer);
eXosip_unlock();
}
}
return SWITCH_STATUS_SUCCESS;
}
static switch_status exosip_read_frame(switch_core_session *session, switch_frame **frame, int timeout,
switch_io_flag flags, int stream_id)
static switch_status exosip_read_frame(switch_core_session *session, switch_frame **frame, int timeout, switch_io_flag flags, int stream_id)
{
struct private_object *tech_pvt = NULL;
size_t bytes = 0, samples = 0, frames = 0, ms = 0;
size_t bytes = 0, samples = 0, frames=0, ms=0;
switch_channel *channel = NULL;
channel = switch_core_session_get_channel(session);
@@ -562,18 +553,18 @@ static switch_status exosip_read_frame(switch_core_session *session, switch_fram
assert(tech_pvt->rtp_session != NULL);
tech_pvt->read_frame.datalen = 0;
while (!switch_test_flag(tech_pvt, TFLAG_BYE) && switch_test_flag(tech_pvt, TFLAG_IO)
&& tech_pvt->read_frame.datalen == 0) {
tech_pvt->read_frame.datalen =
jrtp4c_read(tech_pvt->rtp_session, tech_pvt->read_frame.data, sizeof(tech_pvt->read_buf));
while(!switch_test_flag(tech_pvt, TFLAG_BYE) && switch_test_flag(tech_pvt, TFLAG_IO) && tech_pvt->read_frame.datalen == 0) {
tech_pvt->read_frame.datalen = jrtp4c_read(tech_pvt->rtp_session,
tech_pvt->read_frame.data,
sizeof(tech_pvt->read_buf));
if (tech_pvt->read_frame.datalen > 0) {
bytes = tech_pvt->read_codec.implementation->encoded_bytes_per_frame;
frames = (tech_pvt->read_frame.datalen / bytes);
samples = frames * tech_pvt->read_codec.implementation->samples_per_frame;
ms = frames * tech_pvt->read_codec.implementation->microseconds_per_frame;
tech_pvt->timestamp_recv += (int32_t) samples;
tech_pvt->read_frame.samples = (int) samples;
tech_pvt->timestamp_recv += (int32_t)samples;
tech_pvt->read_frame.samples = (int)samples;
break;
}
@@ -607,13 +598,12 @@ static switch_status exosip_read_frame(switch_core_session *session, switch_fram
}
static switch_status exosip_write_frame(switch_core_session *session, switch_frame *frame, int timeout,
switch_io_flag flags, int stream_id)
static switch_status exosip_write_frame(switch_core_session *session, switch_frame *frame, int timeout, switch_io_flag flags, int stream_id)
{
struct private_object *tech_pvt;
switch_channel *channel = NULL;
switch_status status = SWITCH_STATUS_SUCCESS;
int bytes = 0, samples = 0, ms = 0, frames = 0;
int bytes=0, samples=0, ms=0, frames=0;
channel = switch_core_session_get_channel(session);
assert(channel != NULL);
@@ -639,7 +629,7 @@ static switch_status exosip_write_frame(switch_core_session *session, switch_fra
if (switch_test_flag(tech_pvt, TFLAG_USING_CODEC)) {
bytes = tech_pvt->read_codec.implementation->encoded_bytes_per_frame;
frames = ((int) frame->datalen / bytes);
frames = ((int)frame->datalen / bytes);
samples = frames * tech_pvt->read_codec.implementation->samples_per_frame;
ms = frames * tech_pvt->read_codec.implementation->microseconds_per_frame / 1000;
} else {
@@ -650,8 +640,8 @@ static switch_status exosip_write_frame(switch_core_session *session, switch_fra
//printf("%s %s->%s send %d bytes %d samples in %d frames taking up %d ms ts=%d\n", switch_channel_get_name(channel), tech_pvt->local_sdp_audio_ip, tech_pvt->remote_sdp_audio_ip, frame->datalen, samples, frames, ms, tech_pvt->timestamp_send);
jrtp4c_write(tech_pvt->rtp_session, frame->data, (int) frame->datalen, samples);
tech_pvt->timestamp_send += (int) samples;
jrtp4c_write(tech_pvt->rtp_session, frame->data, (int)frame->datalen, samples);
tech_pvt->timestamp_send += (int)samples;
switch_clear_flag(tech_pvt, TFLAG_WRITING);
//switch_mutex_unlock(tech_pvt->rtp_lock);
@@ -710,39 +700,39 @@ static switch_status exosip_waitfor_write(switch_core_session *session, int ms,
}
static const switch_io_routines exosip_io_routines = {
/*.outgoing_channel */ exosip_outgoing_channel,
/*.answer_channel */ exosip_answer_channel,
/*.read_frame */ exosip_read_frame,
/*.write_frame */ exosip_write_frame,
/*.kill_channel */ exosip_kill_channel,
/*.waitfor_read */ exosip_waitfor_read,
/*.waitfor_read */ exosip_waitfor_write
/*.outgoing_channel*/ exosip_outgoing_channel,
/*.answer_channel*/ exosip_answer_channel,
/*.read_frame*/ exosip_read_frame,
/*.write_frame*/ exosip_write_frame,
/*.kill_channel*/ exosip_kill_channel,
/*.waitfor_read*/ exosip_waitfor_read,
/*.waitfor_read*/ exosip_waitfor_write
};
static const switch_event_handler_table exosip_event_handlers = {
/*.on_init */ exosip_on_init,
/*.on_ring */ exosip_on_ring,
/*.on_execute */ exosip_on_execute,
/*.on_hangup */ exosip_on_hangup,
/*.on_loopback */ exosip_on_loopback,
/*.on_transmit */ exosip_on_transmit
static const switch_event_handler_table exosip_event_handlers = {
/*.on_init*/ exosip_on_init,
/*.on_ring*/ exosip_on_ring,
/*.on_execute*/ exosip_on_execute,
/*.on_hangup*/ exosip_on_hangup,
/*.on_loopback*/ exosip_on_loopback,
/*.on_transmit*/ exosip_on_transmit
};
static const switch_endpoint_interface exosip_endpoint_interface = {
/*.interface_name */ "exosip",
/*.io_routines */ &exosip_io_routines,
/*.event_handlers */ &exosip_event_handlers,
/*.private */ NULL,
/*.next */ NULL
/*.interface_name*/ "exosip",
/*.io_routines*/ &exosip_io_routines,
/*.event_handlers*/ &exosip_event_handlers,
/*.private*/ NULL,
/*.next*/ NULL
};
static const switch_loadable_module_interface exosip_module_interface = {
/*.module_name */ modname,
/*.endpoint_interface */ &exosip_endpoint_interface,
/*.timer_interface */ NULL,
/*.dialplan_interface */ NULL,
/*.codec_interface */ NULL,
/*.application_interface */ NULL
/*.module_name*/ modname,
/*.endpoint_interface*/ &exosip_endpoint_interface,
/*.timer_interface*/ NULL,
/*.dialplan_interface*/ NULL,
/*.codec_interface*/ NULL,
/*.application_interface*/ NULL
};
#if 1
@@ -750,15 +740,14 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_shutdown(void)
{
if (globals.running) {
globals.running = -1;
while (globals.running) {
while(globals.running) {
switch_yield(100000);
}
}
return SWITCH_STATUS_SUCCESS;
}
#endif
SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_module_interface **interface, char *filename)
{
SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_module_interface **interface, char *filename) {
/* NOTE: **interface is **_interface because the common lib redefines interface to struct in some situations */
if (switch_core_new_memory_pool(&module_pool) != SWITCH_STATUS_SUCCESS) {
@@ -773,7 +762,7 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_modul
return SWITCH_STATUS_SUCCESS;
}
static switch_status exosip_create_call(eXosip_event_t * event)
static switch_status exosip_create_call(eXosip_event_t *event)
{
switch_core_session *session;
sdp_message_t *remote_sdp = NULL;
@@ -805,12 +794,14 @@ static switch_status exosip_create_call(eXosip_event_t * event)
}
if ((tech_pvt->caller_profile = switch_caller_profile_new(session,
globals.dialplan,
event->request->from->displayname,
event->request->from->url->username,
event->request->from->url->host,
NULL, NULL, event->request->req_uri->username))) {
switch_channel_set_caller_profile(channel, tech_pvt->caller_profile);
globals.dialplan,
event->request->from->displayname,
event->request->from->url->username,
event->request->from->url->host,
NULL,
NULL,
event->request->req_uri->username))) {
switch_channel_set_caller_profile(channel, tech_pvt->caller_profile);
}
switch_set_flag(tech_pvt, TFLAG_INBOUND);
@@ -833,17 +824,14 @@ static switch_status exosip_create_call(eXosip_event_t * event)
osip_rfc3264_init(&tech_pvt->sdp_config);
/* Add in what codecs we support locally */
if ((num_codecs =
switch_loadable_module_get_codecs(switch_core_session_get_pool(session), codecs,
sizeof(codecs) / sizeof(codecs[0]))) > 0) {
if ((num_codecs = switch_loadable_module_get_codecs(switch_core_session_get_pool(session), codecs, sizeof(codecs)/sizeof(codecs[0]))) > 0) {
int i;
static const switch_codec_implementation *imp;
for (i = 0; i < num_codecs; i++) {
int x = 0;
for (imp = codecs[i]->implementations; imp; imp = imp->next) {
sdp_add_codec(tech_pvt->sdp_config, codecs[i]->codec_type, codecs[i]->ianacode, codecs[i]->iananame,
imp->samples_per_second, x++);
for (imp = codecs[i]->implementations ; imp ; imp = imp->next) {
sdp_add_codec(tech_pvt->sdp_config, codecs[i]->codec_type, codecs[i]->ianacode, codecs[i]->iananame, imp->samples_per_second, x++);
}
}
}
@@ -852,18 +840,17 @@ static switch_status exosip_create_call(eXosip_event_t * event)
sdp_message_parse(tech_pvt->local_sdp, local_sdp_str);
sdp_message_to_str(remote_sdp, &remote_sdp_str);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "LOCAL SDP:\n%s\nREMOTE SDP:\n%s", local_sdp_str, remote_sdp_str);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "LOCAL SDP:\n%s\nREMOTE SDP:\n%s", local_sdp_str,remote_sdp_str);
mline = 0;
while (0 == osip_rfc3264_match(tech_pvt->sdp_config, remote_sdp, audio_tab, video_tab, t38_tab, app_tab, mline)) {
while (0==osip_rfc3264_match(tech_pvt->sdp_config, remote_sdp, audio_tab, video_tab, t38_tab, app_tab, mline)) {
if (audio_tab[0] == NULL && video_tab[0] == NULL && t38_tab[0] == NULL && app_tab[0] == NULL) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Got no compatible codecs!\n");
break;
}
for (pos = 0; audio_tab[pos] != NULL; pos++) {
osip_rfc3264_complete_answer(tech_pvt->sdp_config, remote_sdp, tech_pvt->local_sdp, audio_tab[pos],
mline);
for (pos=0; audio_tab[pos]!=NULL; pos++) {
osip_rfc3264_complete_answer(tech_pvt->sdp_config, remote_sdp, tech_pvt->local_sdp, audio_tab[pos], mline);
if (parse_sdp_media(audio_tab[pos], &dname, &drate, &dpayload) == SWITCH_STATUS_SUCCESS) {
tech_pvt->payload_num = atoi(dpayload);
break;
@@ -872,8 +859,7 @@ static switch_status exosip_create_call(eXosip_event_t * event)
mline++;
}
free(remote_sdp_str);
sdp_message_o_origin_set(tech_pvt->local_sdp, "OpenSWITCH2", "0", "0", "IN", "IP4",
tech_pvt->local_sdp_audio_ip);
sdp_message_o_origin_set(tech_pvt->local_sdp, "OpenSWITCH2", "0", "0", "IN", "IP4", tech_pvt->local_sdp_audio_ip);
sdp_message_s_name_set(tech_pvt->local_sdp, "SIP Call");
sdp_message_c_connection_add(tech_pvt->local_sdp, -1, "IN", "IP4", tech_pvt->local_sdp_audio_ip, NULL, NULL);
snprintf(port, sizeof(port), "%i", tech_pvt->local_sdp_audio_port);
@@ -886,7 +872,7 @@ static switch_status exosip_create_call(eXosip_event_t * event)
tech_pvt->remote_sdp_audio_port = atoi(remote_med->m_port);
snprintf(tech_pvt->call_id, sizeof(tech_pvt->call_id), "%d", event->cid);
switch_core_hash_insert(globals.call_hash, tech_pvt->call_id, tech_pvt);
switch_core_hash_insert(globals.call_hash, tech_pvt->call_id, tech_pvt);
if (!dname) {
exosip_on_hangup(session);
@@ -901,33 +887,34 @@ static switch_status exosip_create_call(eXosip_event_t * event)
int rate = atoi(drate);
if (switch_core_codec_init(&tech_pvt->read_codec,
dname,
rate,
globals.codec_ms,
1,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL, switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't load codec?\n");
switch_channel_hangup(channel);
return SWITCH_STATUS_FALSE;
} else {
if (switch_core_codec_init(&tech_pvt->write_codec,
dname,
rate,
globals.codec_ms,
1,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL, switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
dname,
rate,
globals.codec_ms,
1,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL,
switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't load codec?\n");
switch_channel_hangup(channel);
return SWITCH_STATUS_FALSE;
} else {
if (switch_core_codec_init(&tech_pvt->write_codec,
dname,
rate,
globals.codec_ms,
1,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL,
switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't load codec?\n");
switch_channel_hangup(channel);
return SWITCH_STATUS_FALSE;
} else {
int ms;
tech_pvt->read_frame.rate = rate;
switch_set_flag(tech_pvt, TFLAG_USING_CODEC);
ms = tech_pvt->write_codec.implementation->microseconds_per_frame / 1000;
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Activate Inbound Codec %s/%d %d ms\n", dname, rate,
ms);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Activate Inbound Codec %s/%d %d ms\n", dname, rate, ms);
tech_pvt->read_frame.codec = &tech_pvt->read_codec;
switch_core_session_set_read_codec(session, &tech_pvt->read_codec);
switch_core_session_set_write_codec(session, &tech_pvt->write_codec);
@@ -952,14 +939,13 @@ static switch_status exosip_create_call(eXosip_event_t * event)
}
static void destroy_call_by_event(eXosip_event_t * event)
static void destroy_call_by_event(eXosip_event_t *event)
{
struct private_object *tech_pvt;
switch_channel *channel = NULL;
if (!(tech_pvt = get_pvt_by_call_id(event->cid))) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Um in case you are interested, Can't find the pvt [%d]!\n",
event->cid);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Um in case you are interested, Can't find the pvt [%d]!\n", event->cid);
return;
}
@@ -972,7 +958,7 @@ static void destroy_call_by_event(eXosip_event_t * event)
}
static switch_status parse_sdp_media(sdp_media_t * media, char **dname, char **drate, char **dpayload)
static switch_status parse_sdp_media(sdp_media_t *media, char **dname, char **drate, char **dpayload)
{
int pos = 0;
sdp_attribute_t *attr = NULL;
@@ -980,7 +966,7 @@ static switch_status parse_sdp_media(sdp_media_t * media, char **dname, char **d
switch_status status = SWITCH_STATUS_GENERR;
while (osip_list_eol(media->a_attributes, pos) == 0) {
attr = (sdp_attribute_t *) osip_list_get(media->a_attributes, pos);
attr = (sdp_attribute_t *)osip_list_get(media->a_attributes, pos);
if (attr != NULL && strcasecmp(attr->a_att_field, "rtpmap") == 0) {
payload = attr->a_att_value;
if ((name = strchr(payload, ' '))) {
@@ -1001,8 +987,7 @@ static switch_status parse_sdp_media(sdp_media_t * media, char **dname, char **d
*dname = strdup("L16");
*drate = strdup("8000");
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Found negotiated codec Payload: %s Name: %s Rate: %s\n",
*dpayload, *dname, *drate);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Found negotiated codec Payload: %s Name: %s Rate: %s\n", *dpayload, *dname, *drate);
break;
}
attr = NULL;
@@ -1012,7 +997,7 @@ static switch_status parse_sdp_media(sdp_media_t * media, char **dname, char **d
return status;
}
static void handle_answer(eXosip_event_t * event)
static void handle_answer(eXosip_event_t *event)
{
osip_message_t *ack = NULL;
sdp_message_t *remote_sdp = NULL;
@@ -1067,27 +1052,28 @@ static void handle_answer(eXosip_event_t * event)
if (switch_core_codec_init(&tech_pvt->read_codec,
dname,
rate,
globals.codec_ms,
1,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL, switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't load codec?\n");
switch_channel_hangup(channel);
return;
} else {
if (switch_core_codec_init(&tech_pvt->write_codec,
dname,
rate,
globals.codec_ms,
1,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL,
switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) {
dname,
rate,
globals.codec_ms,
1,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL,
switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't load codec?\n");
switch_channel_hangup(channel);
return;
} else {
if (switch_core_codec_init(&tech_pvt->write_codec,
dname,
rate,
globals.codec_ms,
1,
SWITCH_CODEC_FLAG_ENCODE |SWITCH_CODEC_FLAG_DECODE,
NULL,
switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't load codec?\n");
switch_channel_hangup(channel);
return;
} else {
int ms;
tech_pvt->read_frame.rate = rate;
@@ -1121,119 +1107,133 @@ static void handle_answer(eXosip_event_t * event)
}
}
static void log_event(eXosip_event_t * je)
static void log_event(eXosip_event_t *je)
{
char buf[100];
buf[0] = '\0';
if (je->type == EXOSIP_CALL_NOANSWER) {
snprintf(buf, 99, "<- (%i %i) No answer", je->cid, je->did);
snprintf (buf, 99, "<- (%i %i) No answer", je->cid, je->did);
} else if (je->type == EXOSIP_CALL_CLOSED) {
snprintf(buf, 99, "<- (%i %i) Call Closed", je->cid, je->did);
snprintf (buf, 99, "<- (%i %i) Call Closed", je->cid, je->did);
} else if (je->type == EXOSIP_CALL_RELEASED) {
snprintf(buf, 99, "<- (%i %i) Call released", je->cid, je->did);
} else if (je->type == EXOSIP_MESSAGE_NEW && je->request != NULL && MSG_IS_MESSAGE(je->request)) {
char *tmp = NULL;
snprintf (buf, 99, "<- (%i %i) Call released", je->cid, je->did);
} else if (je->type == EXOSIP_MESSAGE_NEW
&& je->request!=NULL && MSG_IS_MESSAGE(je->request)) {
char *tmp = NULL;
if (je->request != NULL) {
osip_body_t *body;
osip_from_to_str(je->request->from, &tmp);
if (je->request != NULL) {
osip_body_t *body;
osip_from_to_str (je->request->from, &tmp);
osip_message_get_body(je->request, 0, &body);
if (body != NULL && body->body != NULL) {
snprintf(buf, 99, "<- (%i) from: %s TEXT: %s", je->tid, tmp, body->body);
osip_message_get_body (je->request, 0, &body);
if (body != NULL && body->body != NULL) {
snprintf (buf, 99, "<- (%i) from: %s TEXT: %s",
je->tid, tmp, body->body);
}
osip_free (tmp);
} else {
snprintf (buf, 99, "<- (%i) New event for unknown request?", je->tid);
}
osip_free(tmp);
} else {
snprintf(buf, 99, "<- (%i) New event for unknown request?", je->tid);
}
} else if (je->type == EXOSIP_MESSAGE_NEW) {
char *tmp = NULL;
osip_from_to_str(je->request->from, &tmp);
snprintf(buf, 99, "<- (%i) %s from: %s", je->tid, je->request->sip_method, tmp);
osip_free(tmp);
osip_from_to_str (je->request->from, &tmp);
snprintf (buf, 99, "<- (%i) %s from: %s",
je->tid, je->request->sip_method, tmp);
osip_free (tmp);
} else if (je->type == EXOSIP_MESSAGE_PROCEEDING
|| je->type == EXOSIP_MESSAGE_ANSWERED
|| je->type == EXOSIP_MESSAGE_REDIRECTED
|| je->type == EXOSIP_MESSAGE_REQUESTFAILURE
|| je->type == EXOSIP_MESSAGE_SERVERFAILURE || je->type == EXOSIP_MESSAGE_GLOBALFAILURE) {
if (je->response != NULL && je->request != NULL) {
char *tmp = NULL;
|| je->type == EXOSIP_MESSAGE_ANSWERED
|| je->type == EXOSIP_MESSAGE_REDIRECTED
|| je->type == EXOSIP_MESSAGE_REQUESTFAILURE
|| je->type == EXOSIP_MESSAGE_SERVERFAILURE
|| je->type == EXOSIP_MESSAGE_GLOBALFAILURE) {
if (je->response != NULL && je->request != NULL) {
char *tmp = NULL;
osip_to_to_str(je->request->to, &tmp);
snprintf(buf, 99, "<- (%i) [%i %s for %s] to: %s",
je->tid, je->response->status_code, je->response->reason_phrase, je->request->sip_method, tmp);
osip_free(tmp);
} else if (je->request != NULL) {
snprintf(buf, 99, "<- (%i) Error for %s request", je->tid, je->request->sip_method);
} else {
snprintf(buf, 99, "<- (%i) Error for unknown request", je->tid);
}
osip_to_to_str (je->request->to, &tmp);
snprintf (buf, 99, "<- (%i) [%i %s for %s] to: %s",
je->tid, je->response->status_code,
je->response->reason_phrase, je->request->sip_method, tmp);
osip_free (tmp);
} else if (je->request != NULL) {
snprintf (buf, 99, "<- (%i) Error for %s request",
je->tid, je->request->sip_method);
} else {
snprintf (buf, 99, "<- (%i) Error for unknown request", je->tid);
}
} else if (je->response == NULL && je->request != NULL && je->cid > 0) {
char *tmp = NULL;
osip_from_to_str(je->request->from, &tmp);
snprintf(buf, 99, "<- (%i %i) %s from: %s", je->cid, je->did, je->request->cseq->method, tmp);
osip_free(tmp);
osip_from_to_str (je->request->from, &tmp);
snprintf (buf, 99, "<- (%i %i) %s from: %s",
je->cid, je->did, je->request->cseq->method, tmp);
osip_free (tmp);
} else if (je->response != NULL && je->cid > 0) {
char *tmp = NULL;
osip_to_to_str(je->request->to, &tmp);
snprintf(buf, 99, "<- (%i %i) [%i %s] for %s to: %s",
je->cid, je->did, je->response->status_code,
je->response->reason_phrase, je->request->sip_method, tmp);
osip_free(tmp);
osip_to_to_str (je->request->to, &tmp);
snprintf (buf, 99, "<- (%i %i) [%i %s] for %s to: %s",
je->cid, je->did, je->response->status_code,
je->response->reason_phrase, je->request->sip_method, tmp);
osip_free (tmp);
} else if (je->response == NULL && je->request != NULL && je->rid > 0) {
char *tmp = NULL;
osip_from_to_str(je->request->from, &tmp);
snprintf(buf, 99, "<- (%i) %s from: %s", je->rid, je->request->cseq->method, tmp);
osip_free(tmp);
osip_from_to_str (je->request->from, &tmp);
snprintf (buf, 99, "<- (%i) %s from: %s",
je->rid, je->request->cseq->method, tmp);
osip_free (tmp);
} else if (je->response != NULL && je->rid > 0) {
char *tmp = NULL;
osip_from_to_str(je->request->from, &tmp);
snprintf(buf, 99, "<- (%i) [%i %s] from: %s",
je->rid, je->response->status_code, je->response->reason_phrase, tmp);
osip_free(tmp);
osip_from_to_str (je->request->from, &tmp);
snprintf (buf, 99, "<- (%i) [%i %s] from: %s",
je->rid, je->response->status_code,
je->response->reason_phrase, tmp);
osip_free (tmp);
} else if (je->response == NULL && je->request != NULL && je->sid > 0) {
char *tmp = NULL;
char *stat = NULL;
osip_header_t *sub_state;
osip_message_header_get_byname(je->request, "subscription-state", 0, &sub_state);
osip_message_header_get_byname (je->request, "subscription-state",
0, &sub_state);
if (sub_state != NULL && sub_state->hvalue != NULL)
stat = sub_state->hvalue;
osip_uri_to_str(je->request->from->url, &tmp);
snprintf(buf, 99, "<- (%i) [%s] %s from: %s", je->sid, stat, je->request->cseq->method, tmp);
osip_free(tmp);
osip_uri_to_str (je->request->from->url, &tmp);
snprintf (buf, 99, "<- (%i) [%s] %s from: %s",
je->sid, stat, je->request->cseq->method, tmp);
osip_free (tmp);
} else if (je->response != NULL && je->sid > 0) {
char *tmp = NULL;
osip_uri_to_str(je->request->to->url, &tmp);
snprintf(buf, 99, "<- (%i) [%i %s] from: %s",
je->sid, je->response->status_code, je->response->reason_phrase, tmp);
osip_free(tmp);
osip_uri_to_str (je->request->to->url, &tmp);
snprintf (buf, 99, "<- (%i) [%i %s] from: %s",
je->sid, je->response->status_code,
je->response->reason_phrase, tmp);
osip_free (tmp);
} else if (je->response == NULL && je->request != NULL) {
char *tmp = NULL;
osip_from_to_str(je->request->from, &tmp);
snprintf(buf, 99, "<- (c=%i|d=%i|s=%i|n=%i) %s from: %s",
je->cid, je->did, je->sid, je->nid, je->request->sip_method, tmp);
osip_free(tmp);
osip_from_to_str (je->request->from, &tmp);
snprintf (buf, 99, "<- (c=%i|d=%i|s=%i|n=%i) %s from: %s",
je->cid, je->did, je->sid, je->nid,
je->request->sip_method, tmp);
osip_free (tmp);
} else if (je->response != NULL) {
char *tmp = NULL;
osip_from_to_str(je->request->from, &tmp);
snprintf(buf, 99, "<- (c=%i|d=%i|s=%i|n=%i) [%i %s] for %s from: %s",
je->cid, je->did, je->sid, je->nid,
je->response->status_code, je->response->reason_phrase, je->request->sip_method, tmp);
osip_free(tmp);
osip_from_to_str (je->request->from, &tmp);
snprintf (buf, 99, "<- (c=%i|d=%i|s=%i|n=%i) [%i %s] for %s from: %s",
je->cid, je->did, je->sid, je->nid,
je->response->status_code, je->response->reason_phrase,
je->request->sip_method, tmp);
osip_free (tmp);
} else {
snprintf(buf, 99, "<- (c=%i|d=%i|s=%i|n=%i|t=%i) %s",
je->cid, je->did, je->sid, je->nid, je->tid, je->textinfo);
snprintf (buf, 99, "<- (c=%i|d=%i|s=%i|n=%i|t=%i) %s",
je->cid, je->did, je->sid, je->nid, je->tid, je->textinfo);
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "\n%s\n", buf);
/* Print it out */
@@ -1241,7 +1241,7 @@ static void log_event(eXosip_event_t * je)
static int config_exosip(int reload)
static int config_exosip(int reload)
{
switch_config cfg;
char *var, *val;
@@ -1307,88 +1307,88 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_runtime(void)
config_exosip(0);
if (eXosip_init()) {
if (eXosip_init ()) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "eXosip_init initialization failed!\n");
return SWITCH_STATUS_TERM;
}
if (eXosip_listen_addr(IPPROTO_UDP, NULL, globals.port, AF_INET, 0)) {
if (eXosip_listen_addr (IPPROTO_UDP, NULL, globals.port, AF_INET, 0)) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "eXosip_listen_addr failed!\n");
return SWITCH_STATUS_TERM;
}
globals.running = 1;
while (globals.running > 0) {
if (!(event = eXosip_event_wait(0, 100))) {
if (!(event = eXosip_event_wait(0,100))) {
switch_yield(1000);
continue;
}
eXosip_lock();
eXosip_automatic_action();
eXosip_automatic_action ();
eXosip_unlock();
log_event(event);
switch (event->type) {
case EXOSIP_CALL_INVITE:
exosip_create_call(event);
break;
case EXOSIP_CALL_REINVITE:
/* See what the reinvite is about - on hold or whatever */
//handle_reinvite(event);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Got a reinvite.\n");
break;
case EXOSIP_CALL_MESSAGE_NEW:
if (event->request != NULL && MSG_IS_REFER(event->request)) {
//handle_call_transfer(event);
}
break;
case EXOSIP_CALL_ACK:
/* If audio is not flowing and this has SDP - fire it up! */
break;
case EXOSIP_CALL_ANSWERED:
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "The call was answered.\n");
handle_answer(event);
break;
case EXOSIP_CALL_PROCEEDING:
/* This is like a 100 Trying... yeah */
break;
case EXOSIP_CALL_RINGING:
//handle_ringing(event);
break;
case EXOSIP_CALL_REDIRECTED:
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Call was redirect\n");
break;
case EXOSIP_CALL_CLOSED:
destroy_call_by_event(event);
break;
case EXOSIP_CALL_RELEASED:
destroy_call_by_event(event);
break;
case EXOSIP_CALL_NOANSWER:
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "The call was not answered.\n");
destroy_call_by_event(event);
break;
case EXOSIP_CALL_REQUESTFAILURE:
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Request failure\n");
destroy_call_by_event(event);
break;
case EXOSIP_CALL_SERVERFAILURE:
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Server failure\n");
destroy_call_by_event(event);
break;
case EXOSIP_CALL_GLOBALFAILURE:
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Global failure\n");
destroy_call_by_event(event);
break;
/* Registration related stuff */
case EXOSIP_REGISTRATION_NEW:
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Received registration attempt\n");
break;
default:
/* Unknown event... casually absorb it for now */
break;
switch(event->type) {
case EXOSIP_CALL_INVITE:
exosip_create_call(event);
break;
case EXOSIP_CALL_REINVITE:
/* See what the reinvite is about - on hold or whatever */
//handle_reinvite(event);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Got a reinvite.\n");
break;
case EXOSIP_CALL_MESSAGE_NEW:
if (event->request != NULL && MSG_IS_REFER(event->request)) {
//handle_call_transfer(event);
}
break;
case EXOSIP_CALL_ACK:
/* If audio is not flowing and this has SDP - fire it up! */
break;
case EXOSIP_CALL_ANSWERED:
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "The call was answered.\n");
handle_answer(event);
break;
case EXOSIP_CALL_PROCEEDING:
/* This is like a 100 Trying... yeah */
break;
case EXOSIP_CALL_RINGING:
//handle_ringing(event);
break;
case EXOSIP_CALL_REDIRECTED:
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Call was redirect\n");
break;
case EXOSIP_CALL_CLOSED:
destroy_call_by_event(event);
break;
case EXOSIP_CALL_RELEASED:
destroy_call_by_event(event);
break;
case EXOSIP_CALL_NOANSWER:
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "The call was not answered.\n");
destroy_call_by_event(event);
break;
case EXOSIP_CALL_REQUESTFAILURE:
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Request failure\n");
destroy_call_by_event(event);
break;
case EXOSIP_CALL_SERVERFAILURE:
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Server failure\n");
destroy_call_by_event(event);
break;
case EXOSIP_CALL_GLOBALFAILURE:
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Global failure\n");
destroy_call_by_event(event);
break;
/* Registration related stuff */
case EXOSIP_REGISTRATION_NEW:
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Received registration attempt\n");
break;
default:
/* Unknown event... casually absorb it for now */
break;
}
//switch_console_printf(SWITCH_CHANNEL_CONSOLE, "There was an event (%d) [%s]\n", event->type, event->textinfo);
@@ -1399,7 +1399,8 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_runtime(void)
eXosip_quit();
globals.running = 0;
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Monitor Thread Exiting\n");
//switch_sleep(2000000);
//switch_sleep(2000000);
return SWITCH_STATUS_TERM;
}
+220 -225
View File
@@ -62,7 +62,7 @@ typedef enum {
TFLAG_DTMF = (1 << 3),
TFLAG_READING = (1 << 4),
TFLAG_WRITING = (1 << 5),
TFLAG_USING_CODEC = (1 << 6),
TFLAG_USING_CODEC = (1 << 6),
TFLAG_RTP = (1 << 7),
TFLAG_BYE = (1 << 8)
} TFLAGS;
@@ -88,7 +88,7 @@ static struct {
} globals;
struct private_object {
unsigned int flags;
unsigned int flags;
switch_core_session *session;
switch_frame read_frame;
switch_codec read_codec;
@@ -136,7 +136,7 @@ static void set_global_dialplan(char *dialplan)
free(globals.dialplan);
globals.dialplan = NULL;
}
globals.dialplan = strdup(dialplan);
}
@@ -145,14 +145,11 @@ static switch_status exosip_on_init(switch_core_session *session);
static switch_status exosip_on_hangup(switch_core_session *session);
static switch_status exosip_on_loopback(switch_core_session *session);
static switch_status exosip_on_transmit(switch_core_session *session);
static switch_status exosip_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile,
switch_core_session **new_session);
static switch_status exosip_read_frame(switch_core_session *session, switch_frame **frame, int timeout,
switch_io_flag flags);
static switch_status exosip_write_frame(switch_core_session *session, switch_frame *frame, int timeout,
switch_io_flag flags);
static switch_status exosip_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile, switch_core_session **new_session);
static switch_status exosip_read_frame(switch_core_session *session, switch_frame **frame, int timeout, switch_io_flag flags);
static switch_status exosip_write_frame(switch_core_session *session, switch_frame *frame, int timeout, switch_io_flag flags);
static int config_exosip(int reload);
static switch_status parse_sdp_media(sdp_media_t * media, char **dname, char **drate, char **dpayload);
static switch_status parse_sdp_media(sdp_media_t *media, char **dname, char **drate, char **dpayload);
static switch_status exosip_kill_channel(switch_core_session *session, int sig);
static void activate_rtp(struct private_object *tech_pvt);
static void deactivate_rtp(struct private_object *tech_pvt);
@@ -205,7 +202,7 @@ static int sdp_add_codec(struct osip_rfc3264 *cnf, int codec_type, int payload,
default:
break;
}
return 0;
return 0;
}
@@ -235,7 +232,7 @@ static switch_status exosip_on_init(switch_core_session *session)
char *dest_uri;
switch_codec_interface *codecs[512];
int num_codecs = 0;
/* do SIP Goodies... */
/* do SIP Goodies...*/
/* Generate callerid URI */
eXosip_guess_localip(AF_INET, localip, 128);
@@ -248,17 +245,14 @@ static switch_status exosip_on_init(switch_core_session *session)
/* Initialize SDP */
sdp_message_init(&tech_pvt->local_sdp);
sdp_message_v_version_set(tech_pvt->local_sdp, "0");
sdp_message_o_origin_set(tech_pvt->local_sdp, "OpenSWITCH2", "0", "0", "IN", "IP4",
tech_pvt->local_sdp_audio_ip);
sdp_message_o_origin_set(tech_pvt->local_sdp, "OpenSWITCH2", "0", "0", "IN", "IP4", tech_pvt->local_sdp_audio_ip);
sdp_message_s_name_set(tech_pvt->local_sdp, "SIP Call");
sdp_message_c_connection_add(tech_pvt->local_sdp, -1, "IN", "IP4", tech_pvt->local_sdp_audio_ip, NULL, NULL);
sdp_message_t_time_descr_add(tech_pvt->local_sdp, "0", "0");
snprintf(port, sizeof(port), "%i", tech_pvt->local_sdp_audio_port);
sdp_message_m_media_add(tech_pvt->local_sdp, "audio", port, NULL, "RTP/AVP");
/* Add in every codec we support on this outbound call */
if ((num_codecs =
loadable_module_get_codecs(switch_core_session_get_pool(session), codecs,
sizeof(codecs) / sizeof(codecs[0]))) > 0) {
if ((num_codecs = loadable_module_get_codecs(switch_core_session_get_pool(session), codecs, sizeof(codecs)/sizeof(codecs[0]))) > 0) {
int i;
static const switch_codec_implementation *imp;
for (i = 0; i < num_codecs; i++) {
@@ -266,14 +260,12 @@ static switch_status exosip_on_init(switch_core_session *session)
snprintf(tmp, sizeof(tmp), "%i", codecs[i]->ianacode);
sdp_message_m_payload_add(tech_pvt->local_sdp, 0, osip_strdup(tmp));
for (imp = codecs[i]->implementations; imp; imp = imp->next) {
for (imp = codecs[i]->implementations ; imp ; imp = imp->next) {
/* Add to SDP config */
sdp_add_codec(tech_pvt->sdp_config, codecs[i]->codec_type, codecs[i]->ianacode, codecs[i]->iananame,
imp->samples_per_second, x++);
sdp_add_codec(tech_pvt->sdp_config, codecs[i]->codec_type, codecs[i]->ianacode, codecs[i]->iananame, imp->samples_per_second, x++);
/* Add to SDP message */
snprintf(tmp, sizeof(tmp), "%i %s/%i", codecs[i]->ianacode, codecs[i]->iananame,
imp->samples_per_second);
snprintf(tmp, sizeof(tmp), "%i %s/%i", codecs[i]->ianacode, codecs[i]->iananame, imp->samples_per_second);
sdp_message_a_attribute_add(tech_pvt->local_sdp, 0, "rtpmap", osip_strdup(tmp));
memset(tmp, 0, sizeof(tmp));
}
@@ -281,9 +273,7 @@ static switch_status exosip_on_init(switch_core_session *session)
}
/* Setup our INVITE */
eXosip_lock();
if (!
(dest_uri =
(char *) switch_core_session_alloc(session, strlen(tech_pvt->caller_profile->destination_number) + 10))) {
if (!(dest_uri = (char *) switch_core_session_alloc(session, strlen(tech_pvt->caller_profile->destination_number) + 10))) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "AIEEEE!\n");
assert(dest_uri != NULL);
}
@@ -302,7 +292,7 @@ static switch_status exosip_on_init(switch_core_session *session)
switch_core_hash_insert(globals.call_hash, tech_pvt->call_id, tech_pvt);
tech_pvt->did = -1;
eXosip_unlock();
}
}
/* Let Media Work */
switch_set_flag(tech_pvt, TFLAG_IO);
@@ -341,8 +331,8 @@ static switch_status exosip_on_hangup(switch_core_session *session)
assert(tech_pvt != NULL);
switch_core_hash_delete(globals.call_hash, tech_pvt->call_id);
switch_core_hash_delete(globals.call_hash, tech_pvt->call_id);
switch_set_flag(tech_pvt, TFLAG_BYE);
switch_clear_flag(tech_pvt, TFLAG_IO);
@@ -356,8 +346,7 @@ static switch_status exosip_on_hangup(switch_core_session *session)
switch_core_codec_destroy(&tech_pvt->write_codec);
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "EXOSIP HANGUP %s %d/%d=%d\n", switch_channel_get_name(channel),
tech_pvt->cid, tech_pvt->did, i);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "EXOSIP HANGUP %s %d/%d=%d\n", switch_channel_get_name(channel), tech_pvt->cid, tech_pvt->did, i);
return SWITCH_STATUS_SUCCESS;
}
@@ -373,8 +362,7 @@ static switch_status exosip_on_transmit(switch_core_session *session)
return SWITCH_STATUS_SUCCESS;
}
static switch_status exosip_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile,
switch_core_session **new_session)
static switch_status exosip_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile, switch_core_session **new_session)
{
if ((*new_session = switch_core_session_request(&exosip_endpoint_interface, NULL))) {
struct private_object *tech_pvt;
@@ -382,8 +370,7 @@ static switch_status exosip_outgoing_channel(switch_core_session *session, switc
switch_caller_profile *caller_profile, *originator_caller_profile = NULL;
switch_core_session_add_stream(*new_session, NULL);
if ((tech_pvt =
(struct private_object *) switch_core_session_alloc(*new_session, sizeof(struct private_object)))) {
if ((tech_pvt = (struct private_object *) switch_core_session_alloc(*new_session, sizeof(struct private_object)))) {
memset(tech_pvt, 0, sizeof(*tech_pvt));
channel = switch_core_session_get_channel(*new_session);
switch_core_session_set_private(*new_session, tech_pvt);
@@ -417,7 +404,7 @@ static switch_status exosip_outgoing_channel(switch_core_session *session, switc
switch_channel_set_originator_caller_profile(channel, cloned_profile);
}
}
switch_channel_set_flag(channel, CF_OUTBOUND);
switch_set_flag(tech_pvt, TFLAG_OUTBOUND);
switch_channel_set_state(channel, CS_INIT);
@@ -434,7 +421,7 @@ static void deactivate_rtp(struct private_object *tech_pvt)
if (tech_pvt->rtp_session) {
//switch_mutex_lock(tech_pvt->rtp_lock);
while (loops < 10 && (switch_test_flag(tech_pvt, TFLAG_READING) || switch_test_flag(tech_pvt, TFLAG_WRITING))) {
while(loops < 10 && (switch_test_flag(tech_pvt, TFLAG_READING) || switch_test_flag(tech_pvt, TFLAG_WRITING))) {
switch_yield(10000);
loops++;
}
@@ -470,16 +457,22 @@ static void activate_rtp(struct private_object *tech_pvt)
tech_pvt->local_sdp_audio_ip,
tech_pvt->local_sdp_audio_port,
tech_pvt->remote_sdp_audio_ip,
tech_pvt->remote_sdp_audio_port, tech_pvt->read_codec.codec_interface->ianacode, ms);
tech_pvt->remote_sdp_audio_port,
tech_pvt->read_codec.codec_interface->ianacode,
ms
);
tech_pvt->rtp_session = ccrtp4c_new(tech_pvt->local_sdp_audio_ip,
tech_pvt->rtp_session = ccrtp4c_new(
tech_pvt->local_sdp_audio_ip,
tech_pvt->local_sdp_audio_port,
tech_pvt->remote_sdp_audio_ip,
tech_pvt->remote_sdp_audio_port,
tech_pvt->read_codec.codec_interface->ianacode, ms, ms * 15);
tech_pvt->read_codec.codec_interface->ianacode,
ms,
ms * 15);
if (tech_pvt->rtp_session) {
tech_pvt->ssrc = ccrtp4c_get_ssrc(tech_pvt->rtp_session);
@@ -514,21 +507,20 @@ static switch_status exosip_answer_channel(switch_core_session *session)
sdp_message_to_str(tech_pvt->local_sdp, &buf);
osip_message_set_body(answer, buf, strlen(buf));
osip_message_set_content_type(answer, "application/sdp");
free(buf);
free(buf);
eXosip_call_send_answer(tech_pvt->tid, 200, answer);
eXosip_unlock();
}
}
return SWITCH_STATUS_SUCCESS;
}
static switch_status exosip_read_frame(switch_core_session *session, switch_frame **frame, int timeout,
switch_io_flag flags)
static switch_status exosip_read_frame(switch_core_session *session, switch_frame **frame, int timeout, switch_io_flag flags)
{
struct private_object *tech_pvt = NULL;
size_t bytes = 0, samples = 0, frames = 0, ms = 0;
size_t bytes = 0, samples = 0, frames=0, ms=0;
switch_channel *channel = NULL;
channel = switch_core_session_get_channel(session);
@@ -554,11 +546,12 @@ static switch_status exosip_read_frame(switch_core_session *session, switch_fram
assert(tech_pvt->rtp_session != NULL);
tech_pvt->read_frame.datalen = 0;
while (!switch_test_flag(tech_pvt, TFLAG_BYE) && switch_test_flag(tech_pvt, TFLAG_IO)
&& tech_pvt->read_frame.datalen == 0) {
if ((tech_pvt->read_frame.datalen =
ccrtp4c_read(tech_pvt->rtp_session, tech_pvt->read_frame.data, sizeof(tech_pvt->read_buf),
while(!switch_test_flag(tech_pvt, TFLAG_BYE) && switch_test_flag(tech_pvt, TFLAG_IO) && tech_pvt->read_frame.datalen == 0) {
if ((tech_pvt->read_frame.datalen =
ccrtp4c_read(tech_pvt->rtp_session,
tech_pvt->read_frame.data,
sizeof(tech_pvt->read_buf),
&tech_pvt->timestamp_recv))) {
bytes = tech_pvt->read_codec.implementation->encoded_bytes_per_frame;
frames = (tech_pvt->read_frame.datalen / bytes);
@@ -569,9 +562,9 @@ static switch_status exosip_read_frame(switch_core_session *session, switch_fram
}
switch_yield(100);
}
//printf("%s %s->%s recv %d bytes %d samples in %d frames taking up %d ms ts=%d\n", switch_channel_get_name(channel), tech_pvt->local_sdp_audio_ip, tech_pvt->local_sdp_audio_ip, tech_pvt->read_frame.datalen, samples, frames, ms, tech_pvt->timestamp_recv);
//switch_mutex_unlock(tech_pvt->rtp_lock);
@@ -589,25 +582,24 @@ static switch_status exosip_read_frame(switch_core_session *session, switch_fram
*frame = &tech_pvt->read_frame;
return SWITCH_STATUS_SUCCESS;
}
static switch_status exosip_write_frame(switch_core_session *session, switch_frame *frame, int timeout,
switch_io_flag flags)
static switch_status exosip_write_frame(switch_core_session *session, switch_frame *frame, int timeout, switch_io_flag flags)
{
struct private_object *tech_pvt;
switch_channel *channel = NULL;
switch_status status = SWITCH_STATUS_SUCCESS;
int bytes = 0, samples = 0, ms = 0, frames = 0;
int bytes=0, samples=0, ms=0, frames=0;
channel = switch_core_session_get_channel(session);
assert(channel != NULL);
tech_pvt = switch_core_session_get_private(session);
assert(tech_pvt != NULL);
if (!switch_test_flag(tech_pvt, TFLAG_RTP)) {
activate_rtp(tech_pvt);
}
@@ -632,14 +624,14 @@ static switch_status exosip_write_frame(switch_core_session *session, switch_fra
} else {
assert(0);
}
//printf("%s %s->%s send %d bytes %d samples in %d frames taking up %d ms ts=%d\n", switch_channel_get_name(channel), tech_pvt->local_sdp_audio_ip, tech_pvt->remote_sdp_audio_ip, frame->datalen, samples, frames, ms, tech_pvt->timestamp_send);
tech_pvt->timestamp_send += (int) samples;
tech_pvt->timestamp_send += (int)samples;
ccrtp4c_write(tech_pvt->rtp_session, frame->data, frame->datalen, &tech_pvt->timestamp_send);
switch_clear_flag(tech_pvt, TFLAG_WRITING);
@@ -663,7 +655,7 @@ static switch_status exosip_kill_channel(switch_core_session *session, int sig)
switch_clear_flag(tech_pvt, TFLAG_IO);
switch_set_flag(tech_pvt, TFLAG_BYE);
return SWITCH_STATUS_SUCCESS;
}
@@ -693,45 +685,45 @@ static switch_status exosip_waitfor_write(switch_core_session *session, int ms)
tech_pvt = switch_core_session_get_private(session);
assert(tech_pvt != NULL);
return SWITCH_STATUS_SUCCESS;
}
static const switch_io_routines exosip_io_routines = {
/*.outgoing_channel */ exosip_outgoing_channel,
/*.answer_channel */ exosip_answer_channel,
/*.read_frame */ exosip_read_frame,
/*.write_frame */ exosip_write_frame,
/*.kill_channel */ exosip_kill_channel,
/*.waitfor_read */ exosip_waitfor_read,
/*.waitfor_read */ exosip_waitfor_write
/*.outgoing_channel*/ exosip_outgoing_channel,
/*.answer_channel*/ exosip_answer_channel,
/*.read_frame*/ exosip_read_frame,
/*.write_frame*/ exosip_write_frame,
/*.kill_channel*/ exosip_kill_channel,
/*.waitfor_read*/ exosip_waitfor_read,
/*.waitfor_read*/ exosip_waitfor_write
};
static const switch_event_handler_table exosip_event_handlers = {
/*.on_init */ exosip_on_init,
/*.on_ring */ exosip_on_ring,
/*.on_execute */ exosip_on_execute,
/*.on_hangup */ exosip_on_hangup,
/*.on_loopback */ exosip_on_loopback,
/*.on_transmit */ exosip_on_transmit
static const switch_event_handler_table exosip_event_handlers = {
/*.on_init*/ exosip_on_init,
/*.on_ring*/ exosip_on_ring,
/*.on_execute*/ exosip_on_execute,
/*.on_hangup*/ exosip_on_hangup,
/*.on_loopback*/ exosip_on_loopback,
/*.on_transmit*/ exosip_on_transmit
};
static const switch_endpoint_interface exosip_endpoint_interface = {
/*.interface_name */ "exosip",
/*.io_routines */ &exosip_io_routines,
/*.event_handlers */ &exosip_event_handlers,
/*.private */ NULL,
/*.next */ NULL
/*.interface_name*/ "exosip",
/*.io_routines*/ &exosip_io_routines,
/*.event_handlers*/ &exosip_event_handlers,
/*.private*/ NULL,
/*.next*/ NULL
};
static const switch_loadable_module_interface exosip_module_interface = {
/*.module_name */ modname,
/*.endpoint_interface */ &exosip_endpoint_interface,
/*.timer_interface */ NULL,
/*.dialplan_interface */ NULL,
/*.codec_interface */ NULL,
/*.application_interface */ NULL
/*.module_name*/ modname,
/*.endpoint_interface*/ &exosip_endpoint_interface,
/*.timer_interface*/ NULL,
/*.dialplan_interface*/ NULL,
/*.codec_interface*/ NULL,
/*.application_interface*/ NULL
};
@@ -739,7 +731,7 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_shutdown(void)
{
if (globals.running) {
globals.running = -1;
while (globals.running) {
while(globals.running) {
switch_yield(1000);
}
}
@@ -747,15 +739,14 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_shutdown(void)
return SWITCH_STATUS_SUCCESS;
}
SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_module_interface **interface, char *filename)
{
SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_module_interface **interface, char *filename) {
/* NOTE: **interface is **_interface because the common lib redefines interface to struct in some situations */
if (switch_core_new_memory_pool(&module_pool) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "OH OH no pool\n");
return SWITCH_STATUS_TERM;
}
/* connect my internal structure to the blank pointer passed to me */
*interface = &exosip_module_interface;
@@ -763,7 +754,7 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_modul
return SWITCH_STATUS_SUCCESS;
}
static switch_status exosip_create_call(eXosip_event_t * event)
static switch_status exosip_create_call(eXosip_event_t *event)
{
switch_core_session *session;
sdp_message_t *remote_sdp = NULL;
@@ -793,13 +784,14 @@ static switch_status exosip_create_call(eXosip_event_t * event)
switch_core_session_destroy(&session);
return SWITCH_STATUS_MEMERR;
}
if ((tech_pvt->caller_profile = switch_caller_profile_new(session,
globals.dialplan,
event->request->from->displayname,
event->request->from->url->username,
event->request->from->url->username,
NULL, event->request->req_uri->username))) {
globals.dialplan,
event->request->from->displayname,
event->request->from->url->username,
event->request->from->url->username,
NULL,
event->request->req_uri->username))) {
switch_channel_set_caller_profile(channel, tech_pvt->caller_profile);
}
@@ -810,7 +802,7 @@ static switch_status exosip_create_call(eXosip_event_t * event)
snprintf(name, sizeof(name), "Exosip/%s-%04x", tech_pvt->caller_profile->destination_number, rand() & 0xffff);
switch_channel_set_name(channel, name);
if (!(remote_sdp = eXosip_get_sdp_info(event->request))) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Cannot Find Remote SDP!\n");
exosip_on_hangup(session);
@@ -822,18 +814,15 @@ static switch_status exosip_create_call(eXosip_event_t * event)
tech_pvt->local_sdp_audio_port = next_rtp_port();
osip_rfc3264_init(&tech_pvt->sdp_config);
/* Add in what codecs we support locally */
if ((num_codecs =
loadable_module_get_codecs(switch_core_session_get_pool(session), codecs,
sizeof(codecs) / sizeof(codecs[0]))) > 0) {
if ((num_codecs = loadable_module_get_codecs(switch_core_session_get_pool(session), codecs, sizeof(codecs)/sizeof(codecs[0]))) > 0) {
int i;
static const switch_codec_implementation *imp;
for (i = 0; i < num_codecs; i++) {
int x = 0;
for (imp = codecs[i]->implementations; imp; imp = imp->next) {
sdp_add_codec(tech_pvt->sdp_config, codecs[i]->codec_type, codecs[i]->ianacode, codecs[i]->iananame,
imp->samples_per_second, x++);
for (imp = codecs[i]->implementations ; imp ; imp = imp->next) {
sdp_add_codec(tech_pvt->sdp_config, codecs[i]->codec_type, codecs[i]->ianacode, codecs[i]->iananame, imp->samples_per_second, x++);
}
}
}
@@ -842,18 +831,17 @@ static switch_status exosip_create_call(eXosip_event_t * event)
sdp_message_parse(tech_pvt->local_sdp, local_sdp_str);
sdp_message_to_str(remote_sdp, &remote_sdp_str);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "LOCAL SDP:\n%s\nREMOTE SDP:\n%s", local_sdp_str, remote_sdp_str);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "LOCAL SDP:\n%s\nREMOTE SDP:\n%s", local_sdp_str,remote_sdp_str);
mline = 0;
while (0 == osip_rfc3264_match(tech_pvt->sdp_config, remote_sdp, audio_tab, video_tab, t38_tab, app_tab, mline)) {
while (0==osip_rfc3264_match(tech_pvt->sdp_config, remote_sdp, audio_tab, video_tab, t38_tab, app_tab, mline)) {
if (audio_tab[0] == NULL && video_tab[0] == NULL && t38_tab[0] == NULL && app_tab[0] == NULL) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Got no compatible codecs!\n");
break;
}
for (pos = 0; audio_tab[pos] != NULL; pos++) {
osip_rfc3264_complete_answer(tech_pvt->sdp_config, remote_sdp, tech_pvt->local_sdp, audio_tab[pos],
mline);
for (pos=0; audio_tab[pos]!=NULL; pos++) {
osip_rfc3264_complete_answer(tech_pvt->sdp_config, remote_sdp, tech_pvt->local_sdp, audio_tab[pos], mline);
if (parse_sdp_media(audio_tab[pos], &dname, &drate, &dpayload) == SWITCH_STATUS_SUCCESS) {
tech_pvt->payload_num = atoi(dpayload);
break;
@@ -862,8 +850,7 @@ static switch_status exosip_create_call(eXosip_event_t * event)
mline++;
}
free(remote_sdp_str);
sdp_message_o_origin_set(tech_pvt->local_sdp, "OpenSWITCH2", "0", "0", "IN", "IP4",
tech_pvt->local_sdp_audio_ip);
sdp_message_o_origin_set(tech_pvt->local_sdp, "OpenSWITCH2", "0", "0", "IN", "IP4", tech_pvt->local_sdp_audio_ip);
sdp_message_s_name_set(tech_pvt->local_sdp, "SIP Call");
sdp_message_c_connection_add(tech_pvt->local_sdp, -1, "IN", "IP4", tech_pvt->local_sdp_audio_ip, NULL, NULL);
snprintf(port, sizeof(port), "%i", tech_pvt->local_sdp_audio_port);
@@ -874,9 +861,9 @@ static switch_status exosip_create_call(eXosip_event_t * event)
snprintf(tech_pvt->remote_sdp_audio_ip, 50, conn->c_addr);
tech_pvt->remote_sdp_audio_port = atoi(remote_med->m_port);
snprintf(tech_pvt->call_id, sizeof(tech_pvt->call_id), "%d", event->cid);
switch_core_hash_insert(globals.call_hash, tech_pvt->call_id, tech_pvt);
switch_core_hash_insert(globals.call_hash, tech_pvt->call_id, tech_pvt);
if (!dname) {
exosip_on_hangup(session);
@@ -891,21 +878,21 @@ static switch_status exosip_create_call(eXosip_event_t * event)
int rate = atoi(drate);
if (switch_core_codec_init(&tech_pvt->read_codec,
dname,
rate,
globals.codec_ms,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL) != SWITCH_STATUS_SUCCESS) {
dname,
rate,
globals.codec_ms,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't load codec?\n");
switch_channel_hangup(channel);
return SWITCH_STATUS_FALSE;
} else {
if (switch_core_codec_init(&tech_pvt->write_codec,
dname,
rate,
globals.codec_ms,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL) != SWITCH_STATUS_SUCCESS) {
dname,
rate,
globals.codec_ms,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't load codec?\n");
switch_channel_hangup(channel);
return SWITCH_STATUS_FALSE;
@@ -913,15 +900,14 @@ static switch_status exosip_create_call(eXosip_event_t * event)
int ms;
switch_set_flag(tech_pvt, TFLAG_USING_CODEC);
ms = tech_pvt->write_codec.implementation->nanoseconds_per_frame / 1000;
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Activate Inbound Codec %s/%d %d ms\n", dname, rate,
ms);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Activate Inbound Codec %s/%d %d ms\n", dname, rate, ms);
tech_pvt->read_frame.codec = &tech_pvt->read_codec;
switch_core_session_set_read_codec(session, &tech_pvt->read_codec);
switch_core_session_set_write_codec(session, &tech_pvt->write_codec);
}
}
}
switch_core_session_thread_launch(session);
} else {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Cannot Create new Inbound Channel!\n");
@@ -934,14 +920,13 @@ static switch_status exosip_create_call(eXosip_event_t * event)
}
static void destroy_call_by_event(eXosip_event_t * event)
static void destroy_call_by_event(eXosip_event_t *event)
{
struct private_object *tech_pvt;
switch_channel *channel = NULL;
if (!(tech_pvt = get_pvt_by_call_id(event->cid))) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Um in case you are interested, Can't find the pvt [%d]!\n",
event->cid);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Um in case you are interested, Can't find the pvt [%d]!\n", event->cid);
return;
}
@@ -951,10 +936,10 @@ static void destroy_call_by_event(eXosip_event_t * event)
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "destroy %s\n", switch_channel_get_name(channel));
exosip_kill_channel(tech_pvt->session, SWITCH_SIG_KILL);
switch_channel_hangup(channel);
}
static switch_status parse_sdp_media(sdp_media_t * media, char **dname, char **drate, char **dpayload)
static switch_status parse_sdp_media(sdp_media_t *media, char **dname, char **drate, char **dpayload)
{
int pos = 0;
sdp_attribute_t *attr = NULL;
@@ -962,7 +947,7 @@ static switch_status parse_sdp_media(sdp_media_t * media, char **dname, char **d
switch_status status = SWITCH_STATUS_GENERR;
while (osip_list_eol(media->a_attributes, pos) == 0) {
attr = (sdp_attribute_t *) osip_list_get(media->a_attributes, pos);
attr = (sdp_attribute_t *)osip_list_get(media->a_attributes, pos);
if (attr != NULL && strcasecmp(attr->a_att_field, "rtpmap") == 0) {
payload = attr->a_att_value;
if ((name = strchr(payload, ' '))) {
@@ -983,8 +968,7 @@ static switch_status parse_sdp_media(sdp_media_t * media, char **dname, char **d
*dname = strdup("L16");
*drate = strdup("8000");
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Found negotiated codec Payload: %s Name: %s Rate: %s\n",
*dpayload, *dname, *drate);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Found negotiated codec Payload: %s Name: %s Rate: %s\n", *dpayload, *dname, *drate);
break;
}
attr = NULL;
@@ -994,7 +978,7 @@ static switch_status parse_sdp_media(sdp_media_t * media, char **dname, char **d
return status;
}
static void handle_answer(eXosip_event_t * event)
static void handle_answer(eXosip_event_t *event)
{
osip_message_t *ack = NULL;
sdp_message_t *remote_sdp = NULL;
@@ -1042,22 +1026,18 @@ static void handle_answer(eXosip_event_t * event)
/* Assign them thar IDs */
tech_pvt->did = event->did;
tech_pvt->tid = event->tid;
if (1) {
int rate = atoi(drate);
if (switch_core_codec_init
(&tech_pvt->read_codec, dname, rate, globals.codec_ms, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL) != SWITCH_STATUS_SUCCESS) {
if (switch_core_codec_init(&tech_pvt->read_codec, dname, rate, globals.codec_ms, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't load codec?\n");
switch_channel_hangup(channel);
return;
} else {
if (switch_core_codec_init
(&tech_pvt->write_codec, dname, rate, globals.codec_ms,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL) != SWITCH_STATUS_SUCCESS) {
if (switch_core_codec_init(&tech_pvt->write_codec, dname, rate, globals.codec_ms, SWITCH_CODEC_FLAG_ENCODE |SWITCH_CODEC_FLAG_DECODE, NULL) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't load codec?\n");
switch_channel_hangup(channel);
return;
@@ -1078,7 +1058,7 @@ static void handle_answer(eXosip_event_t * event)
eXosip_call_build_ack(event->did, &ack);
eXosip_call_send_ack(event->did, ack);
eXosip_unlock();
free(dname);
free(drate);
free(dpayload);
@@ -1092,119 +1072,133 @@ static void handle_answer(eXosip_event_t * event)
}
static void log_event(eXosip_event_t * je)
static void log_event(eXosip_event_t *je)
{
char buf[100];
buf[0] = '\0';
if (je->type == EXOSIP_CALL_NOANSWER) {
snprintf(buf, 99, "<- (%i %i) No answer", je->cid, je->did);
snprintf (buf, 99, "<- (%i %i) No answer", je->cid, je->did);
} else if (je->type == EXOSIP_CALL_CLOSED) {
snprintf(buf, 99, "<- (%i %i) Call Closed", je->cid, je->did);
snprintf (buf, 99, "<- (%i %i) Call Closed", je->cid, je->did);
} else if (je->type == EXOSIP_CALL_RELEASED) {
snprintf(buf, 99, "<- (%i %i) Call released", je->cid, je->did);
} else if (je->type == EXOSIP_MESSAGE_NEW && je->request != NULL && MSG_IS_MESSAGE(je->request)) {
snprintf (buf, 99, "<- (%i %i) Call released", je->cid, je->did);
} else if (je->type == EXOSIP_MESSAGE_NEW
&& je->request!=NULL && MSG_IS_MESSAGE(je->request)) {
char *tmp = NULL;
if (je->request != NULL) {
osip_body_t *body;
osip_from_to_str(je->request->from, &tmp);
osip_message_get_body(je->request, 0, &body);
osip_from_to_str (je->request->from, &tmp);
osip_message_get_body (je->request, 0, &body);
if (body != NULL && body->body != NULL) {
snprintf(buf, 99, "<- (%i) from: %s TEXT: %s", je->tid, tmp, body->body);
snprintf (buf, 99, "<- (%i) from: %s TEXT: %s",
je->tid, tmp, body->body);
}
osip_free(tmp);
osip_free (tmp);
} else {
snprintf(buf, 99, "<- (%i) New event for unknown request?", je->tid);
snprintf (buf, 99, "<- (%i) New event for unknown request?", je->tid);
}
} else if (je->type == EXOSIP_MESSAGE_NEW) {
char *tmp = NULL;
osip_from_to_str(je->request->from, &tmp);
snprintf(buf, 99, "<- (%i) %s from: %s", je->tid, je->request->sip_method, tmp);
osip_free(tmp);
osip_from_to_str (je->request->from, &tmp);
snprintf (buf, 99, "<- (%i) %s from: %s",
je->tid, je->request->sip_method, tmp);
osip_free (tmp);
} else if (je->type == EXOSIP_MESSAGE_PROCEEDING
|| je->type == EXOSIP_MESSAGE_ANSWERED
|| je->type == EXOSIP_MESSAGE_REDIRECTED
|| je->type == EXOSIP_MESSAGE_REQUESTFAILURE
|| je->type == EXOSIP_MESSAGE_SERVERFAILURE || je->type == EXOSIP_MESSAGE_GLOBALFAILURE) {
|| je->type == EXOSIP_MESSAGE_ANSWERED
|| je->type == EXOSIP_MESSAGE_REDIRECTED
|| je->type == EXOSIP_MESSAGE_REQUESTFAILURE
|| je->type == EXOSIP_MESSAGE_SERVERFAILURE
|| je->type == EXOSIP_MESSAGE_GLOBALFAILURE) {
if (je->response != NULL && je->request != NULL) {
char *tmp = NULL;
osip_to_to_str(je->request->to, &tmp);
snprintf(buf, 99, "<- (%i) [%i %s for %s] to: %s",
je->tid, je->response->status_code, je->response->reason_phrase, je->request->sip_method, tmp);
osip_free(tmp);
osip_to_to_str (je->request->to, &tmp);
snprintf (buf, 99, "<- (%i) [%i %s for %s] to: %s",
je->tid, je->response->status_code,
je->response->reason_phrase, je->request->sip_method, tmp);
osip_free (tmp);
} else if (je->request != NULL) {
snprintf(buf, 99, "<- (%i) Error for %s request", je->tid, je->request->sip_method);
snprintf (buf, 99, "<- (%i) Error for %s request",
je->tid, je->request->sip_method);
} else {
snprintf(buf, 99, "<- (%i) Error for unknown request", je->tid);
snprintf (buf, 99, "<- (%i) Error for unknown request", je->tid);
}
} else if (je->response == NULL && je->request != NULL && je->cid > 0) {
char *tmp = NULL;
osip_from_to_str(je->request->from, &tmp);
snprintf(buf, 99, "<- (%i %i) %s from: %s", je->cid, je->did, je->request->cseq->method, tmp);
osip_free(tmp);
osip_from_to_str (je->request->from, &tmp);
snprintf (buf, 99, "<- (%i %i) %s from: %s",
je->cid, je->did, je->request->cseq->method, tmp);
osip_free (tmp);
} else if (je->response != NULL && je->cid > 0) {
char *tmp = NULL;
osip_to_to_str(je->request->to, &tmp);
snprintf(buf, 99, "<- (%i %i) [%i %s] for %s to: %s",
je->cid, je->did, je->response->status_code,
je->response->reason_phrase, je->request->sip_method, tmp);
osip_free(tmp);
osip_to_to_str (je->request->to, &tmp);
snprintf (buf, 99, "<- (%i %i) [%i %s] for %s to: %s",
je->cid, je->did, je->response->status_code,
je->response->reason_phrase, je->request->sip_method, tmp);
osip_free (tmp);
} else if (je->response == NULL && je->request != NULL && je->rid > 0) {
char *tmp = NULL;
osip_from_to_str(je->request->from, &tmp);
snprintf(buf, 99, "<- (%i) %s from: %s", je->rid, je->request->cseq->method, tmp);
osip_free(tmp);
osip_from_to_str (je->request->from, &tmp);
snprintf (buf, 99, "<- (%i) %s from: %s",
je->rid, je->request->cseq->method, tmp);
osip_free (tmp);
} else if (je->response != NULL && je->rid > 0) {
char *tmp = NULL;
osip_from_to_str(je->request->from, &tmp);
snprintf(buf, 99, "<- (%i) [%i %s] from: %s",
je->rid, je->response->status_code, je->response->reason_phrase, tmp);
osip_free(tmp);
osip_from_to_str (je->request->from, &tmp);
snprintf (buf, 99, "<- (%i) [%i %s] from: %s",
je->rid, je->response->status_code,
je->response->reason_phrase, tmp);
osip_free (tmp);
} else if (je->response == NULL && je->request != NULL && je->sid > 0) {
char *tmp = NULL;
char *stat = NULL;
osip_header_t *sub_state;
osip_message_header_get_byname(je->request, "subscription-state", 0, &sub_state);
osip_message_header_get_byname (je->request, "subscription-state",
0, &sub_state);
if (sub_state != NULL && sub_state->hvalue != NULL)
stat = sub_state->hvalue;
osip_uri_to_str(je->request->from->url, &tmp);
snprintf(buf, 99, "<- (%i) [%s] %s from: %s", je->sid, stat, je->request->cseq->method, tmp);
osip_free(tmp);
osip_uri_to_str (je->request->from->url, &tmp);
snprintf (buf, 99, "<- (%i) [%s] %s from: %s",
je->sid, stat, je->request->cseq->method, tmp);
osip_free (tmp);
} else if (je->response != NULL && je->sid > 0) {
char *tmp = NULL;
osip_uri_to_str(je->request->to->url, &tmp);
snprintf(buf, 99, "<- (%i) [%i %s] from: %s",
je->sid, je->response->status_code, je->response->reason_phrase, tmp);
osip_free(tmp);
osip_uri_to_str (je->request->to->url, &tmp);
snprintf (buf, 99, "<- (%i) [%i %s] from: %s",
je->sid, je->response->status_code,
je->response->reason_phrase, tmp);
osip_free (tmp);
} else if (je->response == NULL && je->request != NULL) {
char *tmp = NULL;
osip_from_to_str(je->request->from, &tmp);
snprintf(buf, 99, "<- (c=%i|d=%i|s=%i|n=%i) %s from: %s",
je->cid, je->did, je->sid, je->nid, je->request->sip_method, tmp);
osip_free(tmp);
osip_from_to_str (je->request->from, &tmp);
snprintf (buf, 99, "<- (c=%i|d=%i|s=%i|n=%i) %s from: %s",
je->cid, je->did, je->sid, je->nid,
je->request->sip_method, tmp);
osip_free (tmp);
} else if (je->response != NULL) {
char *tmp = NULL;
osip_from_to_str(je->request->from, &tmp);
snprintf(buf, 99, "<- (c=%i|d=%i|s=%i|n=%i) [%i %s] for %s from: %s",
je->cid, je->did, je->sid, je->nid,
je->response->status_code, je->response->reason_phrase, je->request->sip_method, tmp);
osip_free(tmp);
osip_from_to_str (je->request->from, &tmp);
snprintf (buf, 99, "<- (c=%i|d=%i|s=%i|n=%i) [%i %s] for %s from: %s",
je->cid, je->did, je->sid, je->nid,
je->response->status_code, je->response->reason_phrase,
je->request->sip_method, tmp);
osip_free (tmp);
} else {
snprintf(buf, 99, "<- (c=%i|d=%i|s=%i|n=%i|t=%i) %s",
je->cid, je->did, je->sid, je->nid, je->tid, je->textinfo);
snprintf (buf, 99, "<- (c=%i|d=%i|s=%i|n=%i|t=%i) %s",
je->cid, je->did, je->sid, je->nid, je->tid, je->textinfo);
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "\n%s\n", buf);
/* Print it out */
@@ -1215,21 +1209,21 @@ static void log_event(eXosip_event_t * je)
static void *monitor_thread_run(void)
{
eXosip_event_t *event = NULL;
globals.running = 1;
while (globals.running > 0) {
if (!(event = eXosip_event_wait(0, 100))) {
if (!(event = eXosip_event_wait(0,100))) {
switch_yield(100);
continue;
}
eXosip_lock();
eXosip_automatic_action();
eXosip_automatic_action ();
eXosip_unlock();
log_event(event);
switch (event->type) {
switch(event->type) {
case EXOSIP_CALL_INVITE:
exosip_create_call(event);
break;
@@ -1301,7 +1295,7 @@ static void *monitor_thread_run(void)
}
static int config_exosip(int reload)
static int config_exosip(int reload)
{
switch_config cfg;
char *var, *val;
@@ -1335,7 +1329,7 @@ static int config_exosip(int reload)
}
}
}
if (!globals.codec_ms) {
globals.codec_ms = 20;
}
@@ -1350,11 +1344,11 @@ static int config_exosip(int reload)
set_global_dialplan("default");
}
if (eXosip_init()) {
if (eXosip_init ()) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "eXosip_init initialization failed!\n");
return SWITCH_STATUS_GENERR;
}
if (eXosip_listen_addr(IPPROTO_UDP, NULL, globals.port, AF_INET, 0)) {
if (eXosip_listen_addr (IPPROTO_UDP, NULL, globals.port, AF_INET, 0)) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "eXosip_listen_addr failed!\n");
return SWITCH_STATUS_GENERR;
}
@@ -1377,3 +1371,4 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_runtime(void)
config_exosip(0);
return SWITCH_STATUS_TERM;
}
+225 -234
View File
@@ -77,7 +77,7 @@ typedef enum {
TFLAG_DTMF = (1 << 3),
TFLAG_READING = (1 << 4),
TFLAG_WRITING = (1 << 5),
TFLAG_USING_CODEC = (1 << 6),
TFLAG_USING_CODEC = (1 << 6),
TFLAG_RTP = (1 << 7),
TFLAG_BYE = (1 << 8)
} TFLAGS;
@@ -103,7 +103,7 @@ static struct {
} globals;
struct private_object {
unsigned int flags;
unsigned int flags;
switch_core_session *session;
switch_frame read_frame;
switch_codec read_codec;
@@ -116,7 +116,7 @@ struct private_object {
int32_t timestamp_send;
int32_t timestamp_recv;
int payload_num;
struct rtp *rtp_session;
struct rtp *rtp_session;
struct osip_rfc3264 *sdp_config;
sdp_message_t *remote_sdp;
sdp_message_t *local_sdp;
@@ -151,7 +151,7 @@ static void set_global_dialplan(char *dialplan)
free(globals.dialplan);
globals.dialplan = NULL;
}
globals.dialplan = strdup(dialplan);
}
@@ -160,14 +160,11 @@ static switch_status exosip_on_init(switch_core_session *session);
static switch_status exosip_on_hangup(switch_core_session *session);
static switch_status exosip_on_loopback(switch_core_session *session);
static switch_status exosip_on_transmit(switch_core_session *session);
static switch_status exosip_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile,
switch_core_session **new_session);
static switch_status exosip_read_frame(switch_core_session *session, switch_frame **frame, int timeout,
switch_io_flag flags);
static switch_status exosip_write_frame(switch_core_session *session, switch_frame *frame, int timeout,
switch_io_flag flags);
static switch_status exosip_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile, switch_core_session **new_session);
static switch_status exosip_read_frame(switch_core_session *session, switch_frame **frame, int timeout, switch_io_flag flags);
static switch_status exosip_write_frame(switch_core_session *session, switch_frame *frame, int timeout, switch_io_flag flags);
static int config_exosip(int reload);
static switch_status parse_sdp_media(sdp_media_t * media, char **dname, char **drate, char **dpayload);
static switch_status parse_sdp_media(sdp_media_t *media, char **dname, char **drate, char **dpayload);
static switch_status exosip_kill_channel(switch_core_session *session, int sig);
static void activate_rtp(struct private_object *tech_pvt);
static void deactivate_rtp(struct private_object *tech_pvt);
@@ -220,7 +217,7 @@ static int sdp_add_codec(struct osip_rfc3264 *cnf, int codec_type, int payload,
default:
break;
}
return 0;
return 0;
}
@@ -250,7 +247,7 @@ static switch_status exosip_on_init(switch_core_session *session)
char *dest_uri;
switch_codec_interface *codecs[512];
int num_codecs = 0;
/* do SIP Goodies... */
/* do SIP Goodies...*/
/* Generate callerid URI */
eXosip_guess_localip(AF_INET, localip, 128);
@@ -263,17 +260,14 @@ static switch_status exosip_on_init(switch_core_session *session)
/* Initialize SDP */
sdp_message_init(&tech_pvt->local_sdp);
sdp_message_v_version_set(tech_pvt->local_sdp, "0");
sdp_message_o_origin_set(tech_pvt->local_sdp, "OpenSWITCH2", "0", "0", "IN", "IP4",
tech_pvt->local_sdp_audio_ip);
sdp_message_o_origin_set(tech_pvt->local_sdp, "OpenSWITCH2", "0", "0", "IN", "IP4", tech_pvt->local_sdp_audio_ip);
sdp_message_s_name_set(tech_pvt->local_sdp, "SIP Call");
sdp_message_c_connection_add(tech_pvt->local_sdp, -1, "IN", "IP4", tech_pvt->local_sdp_audio_ip, NULL, NULL);
sdp_message_t_time_descr_add(tech_pvt->local_sdp, "0", "0");
snprintf(port, sizeof(port), "%i", tech_pvt->local_sdp_audio_port);
sdp_message_m_media_add(tech_pvt->local_sdp, "audio", port, NULL, "RTP/AVP");
/* Add in every codec we support on this outbound call */
if ((num_codecs =
loadable_module_get_codecs(switch_core_session_get_pool(session), codecs,
sizeof(codecs) / sizeof(codecs[0]))) > 0) {
if ((num_codecs = loadable_module_get_codecs(switch_core_session_get_pool(session), codecs, sizeof(codecs)/sizeof(codecs[0]))) > 0) {
int i;
static const switch_codec_implementation *imp;
for (i = 0; i < num_codecs; i++) {
@@ -281,14 +275,12 @@ static switch_status exosip_on_init(switch_core_session *session)
snprintf(tmp, sizeof(tmp), "%i", codecs[i]->ianacode);
sdp_message_m_payload_add(tech_pvt->local_sdp, 0, osip_strdup(tmp));
for (imp = codecs[i]->implementations; imp; imp = imp->next) {
for (imp = codecs[i]->implementations ; imp ; imp = imp->next) {
/* Add to SDP config */
sdp_add_codec(tech_pvt->sdp_config, codecs[i]->codec_type, codecs[i]->ianacode, codecs[i]->iananame,
imp->samples_per_second, x++);
sdp_add_codec(tech_pvt->sdp_config, codecs[i]->codec_type, codecs[i]->ianacode, codecs[i]->iananame, imp->samples_per_second, x++);
/* Add to SDP message */
snprintf(tmp, sizeof(tmp), "%i %s/%i", codecs[i]->ianacode, codecs[i]->iananame,
imp->samples_per_second);
snprintf(tmp, sizeof(tmp), "%i %s/%i", codecs[i]->ianacode, codecs[i]->iananame, imp->samples_per_second);
sdp_message_a_attribute_add(tech_pvt->local_sdp, 0, "rtpmap", osip_strdup(tmp));
memset(tmp, 0, sizeof(tmp));
}
@@ -296,9 +288,7 @@ static switch_status exosip_on_init(switch_core_session *session)
}
/* Setup our INVITE */
eXosip_lock();
if (!
(dest_uri =
(char *) switch_core_session_alloc(session, strlen(tech_pvt->caller_profile->destination_number) + 10))) {
if (!(dest_uri = (char *) switch_core_session_alloc(session, strlen(tech_pvt->caller_profile->destination_number) + 10))) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "AIEEEE!\n");
assert(dest_uri != NULL);
}
@@ -317,7 +307,7 @@ static switch_status exosip_on_init(switch_core_session *session)
switch_core_hash_insert(globals.call_hash, tech_pvt->call_id, tech_pvt);
tech_pvt->did = -1;
eXosip_unlock();
}
}
/* Let Media Work */
@@ -357,8 +347,8 @@ static switch_status exosip_on_hangup(switch_core_session *session)
assert(tech_pvt != NULL);
switch_core_hash_delete(globals.call_hash, tech_pvt->call_id);
switch_core_hash_delete(globals.call_hash, tech_pvt->call_id);
switch_set_flag(tech_pvt, TFLAG_BYE);
switch_clear_flag(tech_pvt, TFLAG_IO);
@@ -372,8 +362,7 @@ static switch_status exosip_on_hangup(switch_core_session *session)
switch_core_codec_destroy(&tech_pvt->write_codec);
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "EXOSIP HANGUP %s %d/%d=%d\n", switch_channel_get_name(channel),
tech_pvt->cid, tech_pvt->did, i);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "EXOSIP HANGUP %s %d/%d=%d\n", switch_channel_get_name(channel), tech_pvt->cid, tech_pvt->did, i);
return SWITCH_STATUS_SUCCESS;
}
@@ -389,8 +378,7 @@ static switch_status exosip_on_transmit(switch_core_session *session)
return SWITCH_STATUS_SUCCESS;
}
static switch_status exosip_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile,
switch_core_session **new_session)
static switch_status exosip_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile, switch_core_session **new_session)
{
if ((*new_session = switch_core_session_request(&exosip_endpoint_interface, NULL))) {
struct private_object *tech_pvt;
@@ -398,8 +386,7 @@ static switch_status exosip_outgoing_channel(switch_core_session *session, switc
switch_caller_profile *caller_profile, *originator_caller_profile = NULL;
switch_core_session_add_stream(*new_session, NULL);
if ((tech_pvt =
(struct private_object *) switch_core_session_alloc(*new_session, sizeof(struct private_object)))) {
if ((tech_pvt = (struct private_object *) switch_core_session_alloc(*new_session, sizeof(struct private_object)))) {
memset(tech_pvt, 0, sizeof(*tech_pvt));
channel = switch_core_session_get_channel(*new_session);
switch_core_session_set_private(*new_session, tech_pvt);
@@ -433,7 +420,7 @@ static switch_status exosip_outgoing_channel(switch_core_session *session, switc
switch_channel_set_originator_caller_profile(channel, cloned_profile);
}
}
switch_channel_set_flag(channel, CF_OUTBOUND);
switch_set_flag(tech_pvt, TFLAG_OUTBOUND);
switch_channel_set_state(channel, CS_INIT);
@@ -446,7 +433,7 @@ static switch_status exosip_outgoing_channel(switch_core_session *session, switc
#if 0
static const char *event_names[] = {
"RX_RTP",
"RX_SR",
"RX_SR",
"RX_RR",
"RX_SDES",
"RX_BYE",
@@ -460,29 +447,30 @@ static const char *event_names[] = {
};
#endif
static void rtp_event_handler(struct rtp *session, rtp_event * event)
static void rtp_event_handler(struct rtp *session, rtp_event *event)
{
rtp_packet *packet;
rtp_packet *packet;
struct private_object *tech_pvt = (struct private_object *) rtp_get_userdata(session);
if (!session) {
return;
}
//switch_console_printf(SWITCH_CHANNEL_CONSOLE, "RTP EVENT (%s)\n", event_names[event->type]);
switch (event->type) {
//switch_console_printf(SWITCH_CHANNEL_CONSOLE, "RTP EVENT (%s)\n", event_names[event->type]);
switch(event->type) {
case RX_RTP:
if ((packet = (rtp_packet *) event->data)) {
if((packet = (rtp_packet *) event->data)) {
if (packet->data_len < 10000) {
memcpy(tech_pvt->read_buf, packet->data, packet->data_len);
tech_pvt->read_frame.datalen = packet->data_len;
} else {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "WTF PACKET LEN %d\n", packet->data_len);
}
free(packet); /* xfree() is mandatory to release RTP packet data */
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "WTF PACKET LEN %d\n", packet->data_len);
}
free(packet); /* xfree() is mandatory to release RTP packet data */
}
break;
break;
case RR_TIMEOUT:
case RX_BYE:
switch_set_flag(tech_pvt, TFLAG_BYE);
@@ -497,7 +485,7 @@ static void deactivate_rtp(struct private_object *tech_pvt)
if (tech_pvt->rtp_session) {
//switch_mutex_lock(tech_pvt->rtp_lock);
while (loops < 10 && (switch_test_flag(tech_pvt, TFLAG_READING) || switch_test_flag(tech_pvt, TFLAG_WRITING))) {
while(loops < 10 && (switch_test_flag(tech_pvt, TFLAG_READING) || switch_test_flag(tech_pvt, TFLAG_WRITING))) {
switch_yield(10000);
loops++;
}
@@ -519,8 +507,9 @@ static void activate_rtp(struct private_object *tech_pvt)
assert(channel != NULL);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Activating RTP %d->%s:%d\n",
tech_pvt->local_sdp_audio_port,
tech_pvt->remote_sdp_audio_ip, tech_pvt->remote_sdp_audio_port);
tech_pvt->local_sdp_audio_port,
tech_pvt->remote_sdp_audio_ip,
tech_pvt->remote_sdp_audio_port);
//switch_mutex_lock(tech_pvt->rtp_lock);
@@ -532,13 +521,13 @@ static void activate_rtp(struct private_object *tech_pvt)
bw *= 8;
}
tech_pvt->rtp_session = rtp_init(tech_pvt->remote_sdp_audio_ip, /* Host/Group IP address */
tech_pvt->rtp_session = rtp_init(tech_pvt->remote_sdp_audio_ip, /* Host/Group IP address */
tech_pvt->local_sdp_audio_port, /* receive port */
tech_pvt->remote_sdp_audio_port, /* transmit port */
ms * 2, /* time-to-live */
bw, /* B/W estimate */
rtp_event_handler, /* RTP event callback */
(void *) tech_pvt); /* App. specific data */
ms * 2, /* time-to-live */
bw, /* B/W estimate */
rtp_event_handler, /* RTP event callback */
(void *) tech_pvt); /* App. specific data */
if (tech_pvt->rtp_session) {
@@ -548,7 +537,7 @@ static void activate_rtp(struct private_object *tech_pvt)
rtp_set_sdes(tech_pvt->rtp_session, tech_pvt->ssrc, RTCP_SDES_NAME, "test", 4);
rtp_set_sdes(tech_pvt->rtp_session, tech_pvt->ssrc, RTCP_SDES_PHONE, "test", 4);
rtp_set_sdes(tech_pvt->rtp_session, tech_pvt->ssrc, RTCP_SDES_TOOL, "test", 4);
switch_set_flag(tech_pvt, TFLAG_RTP);
switch_set_flag(tech_pvt, TFLAG_RTP);
} else {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Oh oh is your port even?\n");
}
@@ -577,23 +566,22 @@ static switch_status exosip_answer_channel(switch_core_session *session)
sdp_message_to_str(tech_pvt->local_sdp, &buf);
osip_message_set_body(answer, buf, strlen(buf));
osip_message_set_content_type(answer, "application/sdp");
free(buf);
free(buf);
eXosip_call_send_answer(tech_pvt->tid, 200, answer);
eXosip_unlock();
}
}
return SWITCH_STATUS_SUCCESS;
}
static switch_status exosip_read_frame(switch_core_session *session, switch_frame **frame, int timeout,
switch_io_flag flags)
static switch_status exosip_read_frame(switch_core_session *session, switch_frame **frame, int timeout, switch_io_flag flags)
{
struct private_object *tech_pvt = NULL;
size_t bytes = 0, samples = 0, frames, ms, x;
switch_channel *channel = NULL;
int ns = 0;
int ns=0;
struct timeval tv;
channel = switch_core_session_get_channel(session);
@@ -628,9 +616,9 @@ static switch_status exosip_read_frame(switch_core_session *session, switch_fram
//switch_mutex_lock(tech_pvt->rtp_lock);
tv.tv_sec = 0;
tv.tv_usec = ns;
while (switch_test_flag(tech_pvt, TFLAG_IO) && !switch_test_flag(tech_pvt, TFLAG_BYE)) {
while(switch_test_flag(tech_pvt, TFLAG_IO) && !switch_test_flag(tech_pvt, TFLAG_BYE)) {
x = rtp_recv(tech_pvt->rtp_session, &tv, tech_pvt->timestamp_recv);
if (x < 0) {
if(x < 0) {
break;
}
if (tech_pvt->read_frame.datalen) {
@@ -641,7 +629,7 @@ static switch_status exosip_read_frame(switch_core_session *session, switch_fram
break;
}
}
tech_pvt->timestamp_recv += (int) samples;
tech_pvt->timestamp_recv += (int)samples;
//printf("%s\trecv %d bytes %d samples in %d frames taking up %d ms ts=%d\n", switch_channel_get_name(channel), tech_pvt->read_frame.datalen, samples, frames, ms, tech_pvt->timestamp_recv);
//switch_mutex_unlock(tech_pvt->rtp_lock);
@@ -663,8 +651,7 @@ static switch_status exosip_read_frame(switch_core_session *session, switch_fram
}
static switch_status exosip_write_frame(switch_core_session *session, switch_frame *frame, int timeout,
switch_io_flag flags)
static switch_status exosip_write_frame(switch_core_session *session, switch_frame *frame, int timeout, switch_io_flag flags)
{
struct private_object *tech_pvt;
switch_channel *channel = NULL;
@@ -676,7 +663,7 @@ static switch_status exosip_write_frame(switch_core_session *session, switch_fra
tech_pvt = switch_core_session_get_private(session);
assert(tech_pvt != NULL);
if (!switch_test_flag(tech_pvt, TFLAG_RTP)) {
activate_rtp(tech_pvt);
}
@@ -701,14 +688,16 @@ static switch_status exosip_write_frame(switch_core_session *session, switch_fra
} else {
assert(0);
}
tech_pvt->timestamp_send += (int) samples;
tech_pvt->timestamp_send += (int)samples;
//printf("%s %s->%s send %d bytes %d samples in %d frames taking up %d ms ts=%d\n", switch_channel_get_name(channel), tech_pvt->local_sdp_audio_ip, tech_pvt->remote_sdp_audio_ip, frame->datalen, samples, frames, ms, tech_pvt->timestamp_recv);
rtp_send_ctrl(tech_pvt->rtp_session, tech_pvt->timestamp_send, NULL);
x = rtp_send_data(tech_pvt->rtp_session, tech_pvt->timestamp_send, tech_pvt->payload_num,
0, 0, 0, (char *) frame->data, (int) frame->datalen, 0, 0, 0);
0, 0, 0,
(char *)frame->data, (int)frame->datalen,
0, 0, 0);
switch_clear_flag(tech_pvt, TFLAG_WRITING);
@@ -763,45 +752,45 @@ static switch_status exosip_waitfor_write(switch_core_session *session, int ms)
tech_pvt = switch_core_session_get_private(session);
assert(tech_pvt != NULL);
return SWITCH_STATUS_SUCCESS;
}
static const switch_io_routines exosip_io_routines = {
/*.outgoing_channel */ exosip_outgoing_channel,
/*.answer_channel */ exosip_answer_channel,
/*.read_frame */ exosip_read_frame,
/*.write_frame */ exosip_write_frame,
/*.kill_channel */ exosip_kill_channel,
/*.waitfor_read */ exosip_waitfor_read,
/*.waitfor_read */ exosip_waitfor_write
/*.outgoing_channel*/ exosip_outgoing_channel,
/*.answer_channel*/ exosip_answer_channel,
/*.read_frame*/ exosip_read_frame,
/*.write_frame*/ exosip_write_frame,
/*.kill_channel*/ exosip_kill_channel,
/*.waitfor_read*/ exosip_waitfor_read,
/*.waitfor_read*/ exosip_waitfor_write
};
static const switch_event_handler_table exosip_event_handlers = {
/*.on_init */ exosip_on_init,
/*.on_ring */ exosip_on_ring,
/*.on_execute */ exosip_on_execute,
/*.on_hangup */ exosip_on_hangup,
/*.on_loopback */ exosip_on_loopback,
/*.on_transmit */ exosip_on_transmit
static const switch_event_handler_table exosip_event_handlers = {
/*.on_init*/ exosip_on_init,
/*.on_ring*/ exosip_on_ring,
/*.on_execute*/ exosip_on_execute,
/*.on_hangup*/ exosip_on_hangup,
/*.on_loopback*/ exosip_on_loopback,
/*.on_transmit*/ exosip_on_transmit
};
static const switch_endpoint_interface exosip_endpoint_interface = {
/*.interface_name */ "exosip",
/*.io_routines */ &exosip_io_routines,
/*.event_handlers */ &exosip_event_handlers,
/*.private */ NULL,
/*.next */ NULL
/*.interface_name*/ "exosip",
/*.io_routines*/ &exosip_io_routines,
/*.event_handlers*/ &exosip_event_handlers,
/*.private*/ NULL,
/*.next*/ NULL
};
static const switch_loadable_module_interface exosip_module_interface = {
/*.module_name */ modname,
/*.endpoint_interface */ &exosip_endpoint_interface,
/*.timer_interface */ NULL,
/*.dialplan_interface */ NULL,
/*.codec_interface */ NULL,
/*.application_interface */ NULL
/*.module_name*/ modname,
/*.endpoint_interface*/ &exosip_endpoint_interface,
/*.timer_interface*/ NULL,
/*.dialplan_interface*/ NULL,
/*.codec_interface*/ NULL,
/*.application_interface*/ NULL
};
@@ -809,7 +798,7 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_shutdown(void)
{
if (globals.running) {
globals.running = -1;
while (globals.running) {
while(globals.running) {
switch_yield(1000);
}
}
@@ -817,16 +806,14 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_shutdown(void)
return SWITCH_STATUS_SUCCESS;
}
SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_module_interface **_interface,
char *filename)
{
SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_module_interface **_interface, char *filename) {
/* NOTE: **interface is **_interface because the common lib redefines interface to struct in some situations */
if (switch_core_new_memory_pool(&module_pool) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "OH OH no pool\n");
return SWITCH_STATUS_TERM;
}
/* connect my internal structure to the blank pointer passed to me */
*_interface = &exosip_module_interface;
@@ -834,7 +821,7 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_modul
return SWITCH_STATUS_SUCCESS;
}
static switch_status exosip_create_call(eXosip_event_t * event)
static switch_status exosip_create_call(eXosip_event_t *event)
{
switch_core_session *session;
sdp_message_t *remote_sdp = NULL;
@@ -864,13 +851,14 @@ static switch_status exosip_create_call(eXosip_event_t * event)
switch_core_session_destroy(&session);
return SWITCH_STATUS_MEMERR;
}
if ((tech_pvt->caller_profile = switch_caller_profile_new(session,
globals.dialplan,
event->request->from->displayname,
event->request->from->url->username,
event->request->from->url->username,
NULL, event->request->req_uri->username))) {
globals.dialplan,
event->request->from->displayname,
event->request->from->url->username,
event->request->from->url->username,
NULL,
event->request->req_uri->username))) {
switch_channel_set_caller_profile(channel, tech_pvt->caller_profile);
}
@@ -881,7 +869,7 @@ static switch_status exosip_create_call(eXosip_event_t * event)
snprintf(name, sizeof(name), "Exosip/%s-%04x", tech_pvt->caller_profile->destination_number, rand() & 0xffff);
switch_channel_set_name(channel, name);
if (!(remote_sdp = eXosip_get_sdp_info(event->request))) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Cannot Find Remote SDP!\n");
exosip_on_hangup(session);
@@ -893,18 +881,15 @@ static switch_status exosip_create_call(eXosip_event_t * event)
tech_pvt->local_sdp_audio_port = next_rtp_port();
osip_rfc3264_init(&tech_pvt->sdp_config);
/* Add in what codecs we support locally */
if ((num_codecs =
loadable_module_get_codecs(switch_core_session_get_pool(session), codecs,
sizeof(codecs) / sizeof(codecs[0]))) > 0) {
if ((num_codecs = loadable_module_get_codecs(switch_core_session_get_pool(session), codecs, sizeof(codecs)/sizeof(codecs[0]))) > 0) {
int i;
static const switch_codec_implementation *imp;
for (i = 0; i < num_codecs; i++) {
int x = 0;
for (imp = codecs[i]->implementations; imp; imp = imp->next) {
sdp_add_codec(tech_pvt->sdp_config, codecs[i]->codec_type, codecs[i]->ianacode, codecs[i]->iananame,
imp->samples_per_second, x++);
for (imp = codecs[i]->implementations ; imp ; imp = imp->next) {
sdp_add_codec(tech_pvt->sdp_config, codecs[i]->codec_type, codecs[i]->ianacode, codecs[i]->iananame, imp->samples_per_second, x++);
}
}
}
@@ -913,18 +898,17 @@ static switch_status exosip_create_call(eXosip_event_t * event)
sdp_message_parse(tech_pvt->local_sdp, local_sdp_str);
sdp_message_to_str(remote_sdp, &remote_sdp_str);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "LOCAL SDP:\n%s\nREMOTE SDP:\n%s", local_sdp_str, remote_sdp_str);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "LOCAL SDP:\n%s\nREMOTE SDP:\n%s", local_sdp_str,remote_sdp_str);
mline = 0;
while (0 == osip_rfc3264_match(tech_pvt->sdp_config, remote_sdp, audio_tab, video_tab, t38_tab, app_tab, mline)) {
while (0==osip_rfc3264_match(tech_pvt->sdp_config, remote_sdp, audio_tab, video_tab, t38_tab, app_tab, mline)) {
if (audio_tab[0] == NULL && video_tab[0] == NULL && t38_tab[0] == NULL && app_tab[0] == NULL) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Got no compatible codecs!\n");
break;
}
for (pos = 0; audio_tab[pos] != NULL; pos++) {
osip_rfc3264_complete_answer(tech_pvt->sdp_config, remote_sdp, tech_pvt->local_sdp, audio_tab[pos],
mline);
for (pos=0; audio_tab[pos]!=NULL; pos++) {
osip_rfc3264_complete_answer(tech_pvt->sdp_config, remote_sdp, tech_pvt->local_sdp, audio_tab[pos], mline);
if (parse_sdp_media(audio_tab[pos], &dname, &drate, &dpayload) == SWITCH_STATUS_SUCCESS) {
tech_pvt->payload_num = atoi(dpayload);
break;
@@ -933,8 +917,7 @@ static switch_status exosip_create_call(eXosip_event_t * event)
mline++;
}
free(remote_sdp_str);
sdp_message_o_origin_set(tech_pvt->local_sdp, "OpenSWITCH2", "0", "0", "IN", "IP4",
tech_pvt->local_sdp_audio_ip);
sdp_message_o_origin_set(tech_pvt->local_sdp, "OpenSWITCH2", "0", "0", "IN", "IP4", tech_pvt->local_sdp_audio_ip);
sdp_message_s_name_set(tech_pvt->local_sdp, "SIP Call");
sdp_message_c_connection_add(tech_pvt->local_sdp, -1, "IN", "IP4", tech_pvt->local_sdp_audio_ip, NULL, NULL);
snprintf(port, sizeof(port), "%i", tech_pvt->local_sdp_audio_port);
@@ -945,9 +928,9 @@ static switch_status exosip_create_call(eXosip_event_t * event)
snprintf(tech_pvt->remote_sdp_audio_ip, 50, conn->c_addr);
tech_pvt->remote_sdp_audio_port = atoi(remote_med->m_port);
snprintf(tech_pvt->call_id, sizeof(tech_pvt->call_id), "%d", event->cid);
switch_core_hash_insert(globals.call_hash, tech_pvt->call_id, tech_pvt);
switch_core_hash_insert(globals.call_hash, tech_pvt->call_id, tech_pvt);
if (!dname) {
exosip_on_hangup(session);
@@ -962,21 +945,21 @@ static switch_status exosip_create_call(eXosip_event_t * event)
int rate = atoi(drate);
if (switch_core_codec_init(&tech_pvt->read_codec,
dname,
rate,
globals.codec_ms,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL) != SWITCH_STATUS_SUCCESS) {
dname,
rate,
globals.codec_ms,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't load codec?\n");
switch_channel_hangup(channel);
return SWITCH_STATUS_FALSE;
} else {
if (switch_core_codec_init(&tech_pvt->write_codec,
dname,
rate,
globals.codec_ms,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL) != SWITCH_STATUS_SUCCESS) {
dname,
rate,
globals.codec_ms,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't load codec?\n");
switch_channel_hangup(channel);
return SWITCH_STATUS_FALSE;
@@ -984,15 +967,14 @@ static switch_status exosip_create_call(eXosip_event_t * event)
int ms;
switch_set_flag(tech_pvt, TFLAG_USING_CODEC);
ms = tech_pvt->write_codec.implementation->nanoseconds_per_frame / 1000;
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Activate Inbound Codec %s/%d %d ms\n", dname, rate,
ms);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Activate Inbound Codec %s/%d %d ms\n", dname, rate, ms);
tech_pvt->read_frame.codec = &tech_pvt->read_codec;
switch_core_session_set_read_codec(session, &tech_pvt->read_codec);
switch_core_session_set_write_codec(session, &tech_pvt->write_codec);
}
}
}
switch_core_session_thread_launch(session);
} else {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Cannot Create new Inbound Channel!\n");
@@ -1005,14 +987,13 @@ static switch_status exosip_create_call(eXosip_event_t * event)
}
static void destroy_call_by_event(eXosip_event_t * event)
static void destroy_call_by_event(eXosip_event_t *event)
{
struct private_object *tech_pvt;
switch_channel *channel = NULL;
if (!(tech_pvt = get_pvt_by_call_id(event->cid))) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Um in case you are interested, Can't find the pvt [%d]!\n",
event->cid);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Um in case you are interested, Can't find the pvt [%d]!\n", event->cid);
return;
}
@@ -1022,10 +1003,10 @@ static void destroy_call_by_event(eXosip_event_t * event)
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "destroy %s\n", switch_channel_get_name(channel));
exosip_kill_channel(tech_pvt->session, SWITCH_SIG_KILL);
switch_channel_hangup(channel);
}
static switch_status parse_sdp_media(sdp_media_t * media, char **dname, char **drate, char **dpayload)
static switch_status parse_sdp_media(sdp_media_t *media, char **dname, char **drate, char **dpayload)
{
int pos = 0;
sdp_attribute_t *attr = NULL;
@@ -1033,7 +1014,7 @@ static switch_status parse_sdp_media(sdp_media_t * media, char **dname, char **d
switch_status status = SWITCH_STATUS_GENERR;
while (osip_list_eol(media->a_attributes, pos) == 0) {
attr = (sdp_attribute_t *) osip_list_get(media->a_attributes, pos);
attr = (sdp_attribute_t *)osip_list_get(media->a_attributes, pos);
if (attr != NULL && strcasecmp(attr->a_att_field, "rtpmap") == 0) {
payload = attr->a_att_value;
if ((name = strchr(payload, ' '))) {
@@ -1054,8 +1035,7 @@ static switch_status parse_sdp_media(sdp_media_t * media, char **dname, char **d
*dname = strdup("L16");
*drate = strdup("8000");
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Found negotiated codec Payload: %s Name: %s Rate: %s\n",
*dpayload, *dname, *drate);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Found negotiated codec Payload: %s Name: %s Rate: %s\n", *dpayload, *dname, *drate);
break;
}
attr = NULL;
@@ -1065,7 +1045,7 @@ static switch_status parse_sdp_media(sdp_media_t * media, char **dname, char **d
return status;
}
static void handle_answer(eXosip_event_t * event)
static void handle_answer(eXosip_event_t *event)
{
osip_message_t *ack = NULL;
sdp_message_t *remote_sdp = NULL;
@@ -1113,22 +1093,18 @@ static void handle_answer(eXosip_event_t * event)
/* Assign them thar IDs */
tech_pvt->did = event->did;
tech_pvt->tid = event->tid;
if (1) {
int rate = atoi(drate);
if (switch_core_codec_init
(&tech_pvt->read_codec, dname, rate, globals.codec_ms, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL) != SWITCH_STATUS_SUCCESS) {
if (switch_core_codec_init(&tech_pvt->read_codec, dname, rate, globals.codec_ms, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't load codec?\n");
switch_channel_hangup(channel);
return;
} else {
if (switch_core_codec_init
(&tech_pvt->write_codec, dname, rate, globals.codec_ms,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL) != SWITCH_STATUS_SUCCESS) {
if (switch_core_codec_init(&tech_pvt->write_codec, dname, rate, globals.codec_ms, SWITCH_CODEC_FLAG_ENCODE |SWITCH_CODEC_FLAG_DECODE, NULL) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't load codec?\n");
switch_channel_hangup(channel);
return;
@@ -1149,7 +1125,7 @@ static void handle_answer(eXosip_event_t * event)
eXosip_call_build_ack(event->did, &ack);
eXosip_call_send_ack(event->did, ack);
eXosip_unlock();
free(dname);
free(drate);
free(dpayload);
@@ -1161,119 +1137,133 @@ static void handle_answer(eXosip_event_t * event)
}
static void log_event(eXosip_event_t * je)
static void log_event(eXosip_event_t *je)
{
char buf[100];
buf[0] = '\0';
if (je->type == EXOSIP_CALL_NOANSWER) {
snprintf(buf, 99, "<- (%i %i) No answer", je->cid, je->did);
snprintf (buf, 99, "<- (%i %i) No answer", je->cid, je->did);
} else if (je->type == EXOSIP_CALL_CLOSED) {
snprintf(buf, 99, "<- (%i %i) Call Closed", je->cid, je->did);
snprintf (buf, 99, "<- (%i %i) Call Closed", je->cid, je->did);
} else if (je->type == EXOSIP_CALL_RELEASED) {
snprintf(buf, 99, "<- (%i %i) Call released", je->cid, je->did);
} else if (je->type == EXOSIP_MESSAGE_NEW && je->request != NULL && MSG_IS_MESSAGE(je->request)) {
snprintf (buf, 99, "<- (%i %i) Call released", je->cid, je->did);
} else if (je->type == EXOSIP_MESSAGE_NEW
&& je->request!=NULL && MSG_IS_MESSAGE(je->request)) {
char *tmp = NULL;
if (je->request != NULL) {
osip_body_t *body;
osip_from_to_str(je->request->from, &tmp);
osip_message_get_body(je->request, 0, &body);
osip_from_to_str (je->request->from, &tmp);
osip_message_get_body (je->request, 0, &body);
if (body != NULL && body->body != NULL) {
snprintf(buf, 99, "<- (%i) from: %s TEXT: %s", je->tid, tmp, body->body);
snprintf (buf, 99, "<- (%i) from: %s TEXT: %s",
je->tid, tmp, body->body);
}
osip_free(tmp);
osip_free (tmp);
} else {
snprintf(buf, 99, "<- (%i) New event for unknown request?", je->tid);
snprintf (buf, 99, "<- (%i) New event for unknown request?", je->tid);
}
} else if (je->type == EXOSIP_MESSAGE_NEW) {
char *tmp = NULL;
osip_from_to_str(je->request->from, &tmp);
snprintf(buf, 99, "<- (%i) %s from: %s", je->tid, je->request->sip_method, tmp);
osip_free(tmp);
osip_from_to_str (je->request->from, &tmp);
snprintf (buf, 99, "<- (%i) %s from: %s",
je->tid, je->request->sip_method, tmp);
osip_free (tmp);
} else if (je->type == EXOSIP_MESSAGE_PROCEEDING
|| je->type == EXOSIP_MESSAGE_ANSWERED
|| je->type == EXOSIP_MESSAGE_REDIRECTED
|| je->type == EXOSIP_MESSAGE_REQUESTFAILURE
|| je->type == EXOSIP_MESSAGE_SERVERFAILURE || je->type == EXOSIP_MESSAGE_GLOBALFAILURE) {
|| je->type == EXOSIP_MESSAGE_ANSWERED
|| je->type == EXOSIP_MESSAGE_REDIRECTED
|| je->type == EXOSIP_MESSAGE_REQUESTFAILURE
|| je->type == EXOSIP_MESSAGE_SERVERFAILURE
|| je->type == EXOSIP_MESSAGE_GLOBALFAILURE) {
if (je->response != NULL && je->request != NULL) {
char *tmp = NULL;
osip_to_to_str(je->request->to, &tmp);
snprintf(buf, 99, "<- (%i) [%i %s for %s] to: %s",
je->tid, je->response->status_code, je->response->reason_phrase, je->request->sip_method, tmp);
osip_free(tmp);
osip_to_to_str (je->request->to, &tmp);
snprintf (buf, 99, "<- (%i) [%i %s for %s] to: %s",
je->tid, je->response->status_code,
je->response->reason_phrase, je->request->sip_method, tmp);
osip_free (tmp);
} else if (je->request != NULL) {
snprintf(buf, 99, "<- (%i) Error for %s request", je->tid, je->request->sip_method);
snprintf (buf, 99, "<- (%i) Error for %s request",
je->tid, je->request->sip_method);
} else {
snprintf(buf, 99, "<- (%i) Error for unknown request", je->tid);
snprintf (buf, 99, "<- (%i) Error for unknown request", je->tid);
}
} else if (je->response == NULL && je->request != NULL && je->cid > 0) {
char *tmp = NULL;
osip_from_to_str(je->request->from, &tmp);
snprintf(buf, 99, "<- (%i %i) %s from: %s", je->cid, je->did, je->request->cseq->method, tmp);
osip_free(tmp);
osip_from_to_str (je->request->from, &tmp);
snprintf (buf, 99, "<- (%i %i) %s from: %s",
je->cid, je->did, je->request->cseq->method, tmp);
osip_free (tmp);
} else if (je->response != NULL && je->cid > 0) {
char *tmp = NULL;
osip_to_to_str(je->request->to, &tmp);
snprintf(buf, 99, "<- (%i %i) [%i %s] for %s to: %s",
je->cid, je->did, je->response->status_code,
je->response->reason_phrase, je->request->sip_method, tmp);
osip_free(tmp);
osip_to_to_str (je->request->to, &tmp);
snprintf (buf, 99, "<- (%i %i) [%i %s] for %s to: %s",
je->cid, je->did, je->response->status_code,
je->response->reason_phrase, je->request->sip_method, tmp);
osip_free (tmp);
} else if (je->response == NULL && je->request != NULL && je->rid > 0) {
char *tmp = NULL;
osip_from_to_str(je->request->from, &tmp);
snprintf(buf, 99, "<- (%i) %s from: %s", je->rid, je->request->cseq->method, tmp);
osip_free(tmp);
osip_from_to_str (je->request->from, &tmp);
snprintf (buf, 99, "<- (%i) %s from: %s",
je->rid, je->request->cseq->method, tmp);
osip_free (tmp);
} else if (je->response != NULL && je->rid > 0) {
char *tmp = NULL;
osip_from_to_str(je->request->from, &tmp);
snprintf(buf, 99, "<- (%i) [%i %s] from: %s",
je->rid, je->response->status_code, je->response->reason_phrase, tmp);
osip_free(tmp);
osip_from_to_str (je->request->from, &tmp);
snprintf (buf, 99, "<- (%i) [%i %s] from: %s",
je->rid, je->response->status_code,
je->response->reason_phrase, tmp);
osip_free (tmp);
} else if (je->response == NULL && je->request != NULL && je->sid > 0) {
char *tmp = NULL;
char *stat = NULL;
osip_header_t *sub_state;
osip_message_header_get_byname(je->request, "subscription-state", 0, &sub_state);
osip_message_header_get_byname (je->request, "subscription-state",
0, &sub_state);
if (sub_state != NULL && sub_state->hvalue != NULL)
stat = sub_state->hvalue;
osip_uri_to_str(je->request->from->url, &tmp);
snprintf(buf, 99, "<- (%i) [%s] %s from: %s", je->sid, stat, je->request->cseq->method, tmp);
osip_free(tmp);
osip_uri_to_str (je->request->from->url, &tmp);
snprintf (buf, 99, "<- (%i) [%s] %s from: %s",
je->sid, stat, je->request->cseq->method, tmp);
osip_free (tmp);
} else if (je->response != NULL && je->sid > 0) {
char *tmp = NULL;
osip_uri_to_str(je->request->to->url, &tmp);
snprintf(buf, 99, "<- (%i) [%i %s] from: %s",
je->sid, je->response->status_code, je->response->reason_phrase, tmp);
osip_free(tmp);
osip_uri_to_str (je->request->to->url, &tmp);
snprintf (buf, 99, "<- (%i) [%i %s] from: %s",
je->sid, je->response->status_code,
je->response->reason_phrase, tmp);
osip_free (tmp);
} else if (je->response == NULL && je->request != NULL) {
char *tmp = NULL;
osip_from_to_str(je->request->from, &tmp);
snprintf(buf, 99, "<- (c=%i|d=%i|s=%i|n=%i) %s from: %s",
je->cid, je->did, je->sid, je->nid, je->request->sip_method, tmp);
osip_free(tmp);
osip_from_to_str (je->request->from, &tmp);
snprintf (buf, 99, "<- (c=%i|d=%i|s=%i|n=%i) %s from: %s",
je->cid, je->did, je->sid, je->nid,
je->request->sip_method, tmp);
osip_free (tmp);
} else if (je->response != NULL) {
char *tmp = NULL;
osip_from_to_str(je->request->from, &tmp);
snprintf(buf, 99, "<- (c=%i|d=%i|s=%i|n=%i) [%i %s] for %s from: %s",
je->cid, je->did, je->sid, je->nid,
je->response->status_code, je->response->reason_phrase, je->request->sip_method, tmp);
osip_free(tmp);
osip_from_to_str (je->request->from, &tmp);
snprintf (buf, 99, "<- (c=%i|d=%i|s=%i|n=%i) [%i %s] for %s from: %s",
je->cid, je->did, je->sid, je->nid,
je->response->status_code, je->response->reason_phrase,
je->request->sip_method, tmp);
osip_free (tmp);
} else {
snprintf(buf, 99, "<- (c=%i|d=%i|s=%i|n=%i|t=%i) %s",
je->cid, je->did, je->sid, je->nid, je->tid, je->textinfo);
snprintf (buf, 99, "<- (c=%i|d=%i|s=%i|n=%i|t=%i) %s",
je->cid, je->did, je->sid, je->nid, je->tid, je->textinfo);
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "\n%s\n", buf);
/* Print it out */
@@ -1284,21 +1274,21 @@ static void log_event(eXosip_event_t * je)
static void *monitor_thread_run(void)
{
eXosip_event_t *event = NULL;
globals.running = 1;
while (globals.running > 0) {
if (!(event = eXosip_event_wait(0, 100))) {
if (!(event = eXosip_event_wait(0,100))) {
switch_yield(100);
continue;
}
eXosip_lock();
eXosip_automatic_action();
eXosip_automatic_action ();
eXosip_unlock();
log_event(event);
switch (event->type) {
switch(event->type) {
case EXOSIP_CALL_INVITE:
exosip_create_call(event);
break;
@@ -1370,7 +1360,7 @@ static void *monitor_thread_run(void)
}
static int config_exosip(int reload)
static int config_exosip(int reload)
{
switch_config cfg;
char *var, *val;
@@ -1404,7 +1394,7 @@ static int config_exosip(int reload)
}
}
}
if (!globals.codec_ms) {
globals.codec_ms = 20;
}
@@ -1419,11 +1409,11 @@ static int config_exosip(int reload)
set_global_dialplan("default");
}
if (eXosip_init()) {
if (eXosip_init ()) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "eXosip_init initialization failed!\n");
return SWITCH_STATUS_GENERR;
}
if (eXosip_listen_addr(IPPROTO_UDP, NULL, globals.port, AF_INET, 0)) {
if (eXosip_listen_addr (IPPROTO_UDP, NULL, globals.port, AF_INET, 0)) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "eXosip_listen_addr failed!\n");
return SWITCH_STATUS_GENERR;
}
@@ -1446,3 +1436,4 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_runtime(void)
config_exosip(0);
return SWITCH_STATUS_TERM;
}
+261 -282
View File
@@ -83,7 +83,7 @@ struct private_object {
unsigned char databuf[SWITCH_RECCOMMENDED_BUFFER_SIZE];
switch_core_session *session;
struct iax_session *iax_session;
switch_caller_profile *caller_profile;
switch_caller_profile *caller_profile;
unsigned int codec;
unsigned int codecs;
unsigned short samprate;
@@ -92,21 +92,19 @@ struct private_object {
};
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_codec_string, globals.codec_string)
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_codec_rates_string, globals.codec_rates_string)
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"
};
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"};
struct ast_iana {
@@ -116,30 +114,31 @@ struct ast_iana {
};
//999 means it's wrong nad i dont know the real one
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"},
{AST_FORMAT_ILBC, 999, "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}
};
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"},
{AST_FORMAT_ILBC, 999, "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}
};
static char *ast2str(unsigned int ast)
{
int x;
for (x = 0; x < 32; x++) {
for(x = 0; x < 32; x++) {
if ((1 << x) == ast) {
return AST_IANA[x].name;
}
@@ -152,7 +151,7 @@ static unsigned int iana2ast(int iana)
int x = 0;
unsigned int ast = 0;
for (x = 0; AST_IANA[x].ast; x++) {
for(x = 0; AST_IANA[x].ast; x++) {
if (AST_IANA[x].iana == iana) {
ast = AST_IANA[x].ast;
break;
@@ -162,11 +161,11 @@ static unsigned int iana2ast(int iana)
return ast;
}
static unsigned short iax_build_codec_rates(void)
static unsigned short iax_build_codec_rates(void)
{
int x;
unsigned short samples = 0;
for (x = 0; x < globals.codec_rates_last; x++) {
int rate = atoi(globals.codec_rates[x]);
switch (rate) {
@@ -201,9 +200,7 @@ typedef enum {
IAX_QUERY = 2
} iax_io_t;
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)
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)
{
char *dname = NULL;
//int rate = 8000;
@@ -216,18 +213,14 @@ static switch_status iax_set_codec(struct private_object *tech_pvt, struct iax_s
if (globals.codec_string) {
if (!(num_codecs = switch_loadable_module_get_codecs_sorted(switch_core_session_get_pool(tech_pvt->session),
codecs,
SWITCH_MAX_CODECS,
globals.codec_order,
globals.codec_order_last)) > 0) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "NO codecs?\n");
return SWITCH_STATUS_GENERR;
codecs,
SWITCH_MAX_CODECS,
globals.codec_order,
globals.codec_order_last)) > 0) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "NO codecs?\n");
return SWITCH_STATUS_GENERR;
}
} else
if (!
(num_codecs =
switch_loadable_module_get_codecs(switch_core_session_get_pool(tech_pvt->session), codecs,
SWITCH_MAX_CODECS)) > 0) {
} else if (!(num_codecs = switch_loadable_module_get_codecs(switch_core_session_get_pool(tech_pvt->session), codecs, SWITCH_MAX_CODECS)) > 0) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "NO codecs?\n");
return SWITCH_STATUS_GENERR;
}
@@ -267,7 +260,7 @@ static switch_status iax_set_codec(struct private_object *tech_pvt, struct iax_s
len = iax_pref_codec_get(iax_session, prefs, sizeof(prefs));
}
if (len) { /*they sent us a pref and we don't want to be codec master */
if (len) { /*they sent us a pref and we don't want to be codec master*/
char pref_str[256] = "(";
for (x = 0; x < len; x++) {
@@ -290,7 +283,7 @@ static switch_status iax_set_codec(struct private_object *tech_pvt, struct iax_s
}
}
} else {
if (*format & mixed_cap) { /* is the one we asked for here? */
if (*format & mixed_cap) { /* is the one we asked for here? */
chosen = *format;
for (x = 0; x < num_codecs; x++) {
unsigned int cap = iana2ast(codecs[x]->ianacode);
@@ -298,7 +291,7 @@ static switch_status iax_set_codec(struct private_object *tech_pvt, struct iax_s
dname = codecs[x]->iananame;
}
}
} else { /* c'mon there has to be SOMETHING... */
} else { /* c'mon there has to be SOMETHING...*/
for (x = 0; x < num_codecs; x++) {
unsigned int cap = iana2ast(codecs[x]->ianacode);
if (cap & mixed_cap) {
@@ -325,7 +318,7 @@ static switch_status iax_set_codec(struct private_object *tech_pvt, struct iax_s
//printf("\n\n******WTF %u %u %u\n******\n", *samprate, samples, mixed);
srate = 8000;
if (mixed & IAX_RATE_16KHZ) {
srate = 16000;
}
@@ -351,8 +344,9 @@ static switch_status iax_set_codec(struct private_object *tech_pvt, struct iax_s
srate,
0,
1,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL, switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) {
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL,
switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't load codec?\n");
return SWITCH_STATUS_GENERR;
} else {
@@ -361,10 +355,11 @@ static switch_status iax_set_codec(struct private_object *tech_pvt, struct iax_s
srate,
0,
1,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL, switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) {
SWITCH_CODEC_FLAG_ENCODE |SWITCH_CODEC_FLAG_DECODE,
NULL,
switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't load codec?\n");
switch_core_codec_destroy(&tech_pvt->read_codec);
switch_core_codec_destroy(&tech_pvt->read_codec);
return SWITCH_STATUS_GENERR;
} else {
int ms;
@@ -397,12 +392,9 @@ static switch_status channel_on_hangup(switch_core_session *session);
static switch_status channel_on_ring(switch_core_session *session);
static switch_status channel_on_loopback(switch_core_session *session);
static switch_status channel_on_transmit(switch_core_session *session);
static switch_status channel_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile,
switch_core_session **new_session);
static switch_status channel_read_frame(switch_core_session *session, switch_frame **frame, int timeout,
switch_io_flag flags, int stream_id);
static switch_status channel_write_frame(switch_core_session *session, switch_frame *frame, int timeout,
switch_io_flag flags, int stream_id);
static switch_status channel_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile, switch_core_session **new_session);
static switch_status channel_read_frame(switch_core_session *session, switch_frame **frame, int timeout, switch_io_flag flags, int stream_id);
static switch_status channel_write_frame(switch_core_session *session, switch_frame *frame, int timeout, switch_io_flag flags, int stream_id);
static switch_status channel_kill_channel(switch_core_session *session, int sig);
@@ -442,7 +434,7 @@ static switch_status channel_on_init(switch_core_session *session)
switch_set_flag(tech_pvt, TFLAG_IO);
switch_mutex_init(&tech_pvt->mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(session));
//switch_thread_cond_create(&tech_pvt->cond, switch_core_session_get_pool(session));
//switch_thread_cond_create(&tech_pvt->cond, switch_core_session_get_pool(session));
//switch_mutex_lock(tech_pvt->mutex);
/* Move Channel's State Machine to RING */
@@ -559,8 +551,7 @@ static switch_status channel_on_transmit(switch_core_session *session)
/* 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!
*/
static switch_status channel_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile,
switch_core_session **new_session)
static switch_status channel_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile, switch_core_session **new_session)
{
if ((*new_session = switch_core_session_request(&channel_endpoint_interface, NULL))) {
struct private_object *tech_pvt;
@@ -570,8 +561,7 @@ static switch_status channel_outgoing_channel(switch_core_session *session, swit
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)))) {
if ((tech_pvt = (struct private_object *) switch_core_session_alloc(*new_session, sizeof(struct private_object)))) {
memset(tech_pvt, 0, sizeof(*tech_pvt));
channel = switch_core_session_get_channel(*new_session);
switch_core_session_set_private(*new_session, tech_pvt);
@@ -611,8 +601,10 @@ static switch_status channel_outgoing_channel(switch_core_session *session, swit
}
iax_call(tech_pvt->iax_session,
caller_profile->caller_id_number,
caller_profile->caller_id_name, caller_profile->destination_number, NULL, 0, req, cap);
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);
@@ -653,7 +645,7 @@ static switch_status channel_send_dtmf(switch_core_session *session, char *dtmf)
tech_pvt = switch_core_session_get_private(session);
assert(tech_pvt != NULL);
if (tech_pvt->iax_session) {
for (digit = dtmf; *digit; digit++) {
for(digit = dtmf; *digit; digit++) {
iax_send_dtmf(tech_pvt->iax_session, *digit);
}
}
@@ -661,8 +653,7 @@ static switch_status channel_send_dtmf(switch_core_session *session, char *dtmf)
return SWITCH_STATUS_SUCCESS;
}
static switch_status channel_read_frame(switch_core_session *session, switch_frame **frame, int timeout,
switch_io_flag flags, int stream_id)
static switch_status channel_read_frame(switch_core_session *session, switch_frame **frame, int timeout, switch_io_flag flags, int stream_id)
{
switch_channel *channel = NULL;
struct private_object *tech_pvt = NULL;
@@ -677,27 +668,26 @@ static switch_status channel_read_frame(switch_core_session *session, switch_fra
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)) {
return SWITCH_STATUS_FALSE;
return SWITCH_STATUS_FALSE;
}
if (switch_test_flag(tech_pvt, TFLAG_IO) && switch_test_flag(tech_pvt, TFLAG_VOICE)) {
switch_clear_flag(tech_pvt, TFLAG_VOICE);
if (!tech_pvt->read_frame.datalen) {
if(!tech_pvt->read_frame.datalen) {
continue;
}
*frame = &tech_pvt->read_frame;
return SWITCH_STATUS_SUCCESS;
}
switch_yield(1000);
}
return SWITCH_STATUS_FALSE;
}
static switch_status channel_write_frame(switch_core_session *session, switch_frame *frame, int timeout,
switch_io_flag flags, int stream_id)
static switch_status channel_write_frame(switch_core_session *session, switch_frame *frame, int timeout, switch_io_flag flags, int stream_id)
{
switch_channel *channel = NULL;
struct private_object *tech_pvt = NULL;
@@ -709,16 +699,16 @@ static switch_status channel_write_frame(switch_core_session *session, switch_fr
tech_pvt = switch_core_session_get_private(session);
assert(tech_pvt != NULL);
if (!switch_test_flag(tech_pvt, TFLAG_IO)) {
if(!switch_test_flag(tech_pvt, TFLAG_IO)) {
return SWITCH_STATUS_FALSE;
}
#ifndef BIGENDIAN
if (switch_test_flag(tech_pvt, TFLAG_LINEAR)) {
switch_swap_linear(frame->data, (int) frame->datalen / 2);
switch_swap_linear(frame->data, (int)frame->datalen / 2);
}
#endif
iax_send_voice(tech_pvt->iax_session, tech_pvt->codec, frame->data, (int) frame->datalen,
tech_pvt->write_codec.implementation->samples_per_frame);
iax_send_voice(tech_pvt->iax_session, tech_pvt->codec, frame->data, (int)frame->datalen, tech_pvt->write_codec.implementation->samples_per_frame);
return SWITCH_STATUS_SUCCESS;
@@ -742,45 +732,44 @@ static switch_status channel_answer_channel(switch_core_session *session)
}
static const switch_event_handler_table channel_event_handlers = {
/*.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
/*.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
};
static const switch_io_routines channel_io_routines = {
/*.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
/*.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
};
static const switch_endpoint_interface channel_endpoint_interface = {
/*.interface_name */ "iax",
/*.io_routines */ &channel_io_routines,
/*.event_handlers */ &channel_event_handlers,
/*.private */ NULL,
/*.next */ NULL
/*.interface_name*/ "iax",
/*.io_routines*/ &channel_io_routines,
/*.event_handlers*/ &channel_event_handlers,
/*.private*/ NULL,
/*.next*/ NULL
};
static const switch_loadable_module_interface channel_module_interface = {
/*.module_name */ modname,
/*.endpoint_interface */ &channel_endpoint_interface,
/*.timer_interface */ NULL,
/*.dialplan_interface */ NULL,
/*.codec_interface */ NULL,
/*.application_interface */ NULL
/*.module_name*/ modname,
/*.endpoint_interface*/ &channel_endpoint_interface,
/*.timer_interface*/ NULL,
/*.dialplan_interface*/ NULL,
/*.codec_interface*/ NULL,
/*.application_interface*/ NULL
};
SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_module_interface **interface, char *filename)
{
SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_module_interface **interface, char *filename) {
if (switch_core_new_memory_pool(&module_pool) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "OH OH no pool\n");
@@ -831,12 +820,10 @@ static switch_status load_config(void)
set_global_dialplan(val);
} else if (!strcmp(var, "codec_prefs")) {
set_global_codec_string(val);
globals.codec_order_last =
switch_separate_string(globals.codec_string, ',', globals.codec_order, SWITCH_MAX_CODECS);
globals.codec_order_last = switch_separate_string(globals.codec_string, ',', globals.codec_order, SWITCH_MAX_CODECS);
} else if (!strcmp(var, "codec_rates")) {
set_global_codec_rates_string(val);
globals.codec_rates_last =
switch_separate_string(globals.codec_rates_string, ',', globals.codec_rates, SWITCH_MAX_CODECS);
globals.codec_rates_last = switch_separate_string(globals.codec_rates_string, ',', globals.codec_rates, SWITCH_MAX_CODECS);
}
}
}
@@ -873,17 +860,17 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_runtime(void)
iax_set_error(iax_err_cb);
iax_set_output(iax_out_cb);
netfd = iax_get_fd();
netfd = iax_get_fd();
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "IAX Ready Port %d\n", globals.port);
for (;;) {
for(;;) {
if (running == -1) {
break;
}
/* Wait for an event. */
/* Wait for an event.*/
if ((iaxevent = iax_get_event(0)) == NULL) {
switch_yield(1000);
continue;
@@ -892,182 +879,173 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_runtime(void)
if (globals.debug && iaxevent->etype != IAX_EVENT_VOICE) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Event %d [%s]!\n",
iaxevent->etype, IAXNAMES[iaxevent->etype]);
iaxevent->etype, IAXNAMES[iaxevent->etype]);
}
switch (iaxevent->etype) {
case IAX_EVENT_REGACK:
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Registration completed successfully.\n");
if (iaxevent->ies.refresh)
refresh = iaxevent->ies.refresh;
break;
case IAX_EVENT_REGREJ:
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Registration failed.\n");
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;
case IAX_EVENT_REGACK:
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Registration completed successfully.\n");
if (iaxevent->ies.refresh) refresh = iaxevent->ies.refresh;
break;
case IAX_EVENT_REGREJ:
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Registration failed.\n");
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;
if (iax_set_codec(tech_pvt, iaxevent->session, &format, &cap, &iaxevent->ies.samprate, IAX_SET) !=
SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "WTF? %d %d\n", iaxevent->ies.format,
iaxevent->ies.capability);
}
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Call accepted.\n");
break;
case IAX_EVENT_RINGA:
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Ringing heard.\n");
break;
case IAX_EVENT_PONG:
// informative only
break;
case IAX_EVENT_ANSWER:
// the other side answered our call
if (tech_pvt) {
switch_channel *channel;
if ((channel = switch_core_session_get_channel(tech_pvt->session))) {
if (switch_channel_test_flag(channel, CF_ANSWERED)) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "WTF Mutiple Answer %s?\n",
switch_channel_get_name(channel));
} else {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Answer %s\n",
switch_channel_get_name(channel));
switch_channel_answer(channel);
if (iax_set_codec(tech_pvt, iaxevent->session, &format, &cap, &iaxevent->ies.samprate, IAX_SET) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "WTF? %d %d\n",iaxevent->ies.format, iaxevent->ies.capability);
}
}
}
break;
case IAX_EVENT_CONNECT:
// incoming call detected
switch_console_printf(SWITCH_CHANNEL_CONSOLE,
"Incoming call connected %s, %s, %s %d/%d\n",
iaxevent->ies.called_number,
iaxevent->ies.calling_number,
iaxevent->ies.calling_name, iaxevent->ies.format, iaxevent->ies.capability);
if (iaxevent) {
switch_core_session *session;
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Call accepted.\n");
break;
case IAX_EVENT_RINGA:
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Ringing heard.\n");
break;
case IAX_EVENT_PONG:
// informative only
break;
case IAX_EVENT_ANSWER:
// the other side answered our call
if (tech_pvt) {
switch_channel *channel;
if ((channel = switch_core_session_get_channel(tech_pvt->session))) {
if (switch_channel_test_flag(channel, CF_ANSWERED)) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "WTF Mutiple Answer %s?\n", switch_channel_get_name(channel));
} else {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Answer %s\n", switch_channel_get_name(channel));
switch_channel_answer(channel);
}
}
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "New Inbound Channel %s!\n",
iaxevent->ies.calling_name);
if ((session = switch_core_session_request(&channel_endpoint_interface, NULL))) {
struct private_object *tech_pvt;
break;
case IAX_EVENT_CONNECT:
// incoming call detected
switch_console_printf(SWITCH_CHANNEL_CONSOLE,
"Incoming call connected %s, %s, %s %d/%d\n",
iaxevent->ies.called_number,
iaxevent->ies.calling_number,
iaxevent->ies.calling_name,
iaxevent->ies.format,
iaxevent->ies.capability);
if (iaxevent) {
switch_core_session *session;
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "New Inbound Channel %s!\n", iaxevent->ies.calling_name);
if ((session = switch_core_session_request(&channel_endpoint_interface, NULL))) {
struct private_object *tech_pvt;
switch_channel *channel;
switch_core_session_add_stream(session, NULL);
if ((tech_pvt = (struct private_object *) switch_core_session_alloc(session, sizeof(struct private_object)))) {
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 {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Hey where is my memory pool?\n");
switch_core_session_destroy(&session);
break;
}
if ((tech_pvt->caller_profile = switch_caller_profile_new(session,
globals.dialplan,
iaxevent->ies.calling_name,
iaxevent->ies.calling_number,
iax_get_peer_ip(iaxevent->session),
iaxevent->ies.calling_ani,
NULL,
iaxevent->ies.called_number))) {
char name[128];
switch_channel_set_caller_profile(channel, tech_pvt->caller_profile);
snprintf(name, sizeof(name), "IAX/%s-%04x", tech_pvt->caller_profile->destination_number, rand() & 0xffff);
switch_channel_set_name(channel, name);
}
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);
} else {
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);
}
}
}
break;
case IAX_EVENT_REJECT:
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Rejected call.\n");
case IAX_EVENT_BUSY:
case IAX_EVENT_HANGUP:
if (tech_pvt) {
switch_channel *channel;
switch_core_session_add_stream(session, NULL);
if ((tech_pvt =
(struct private_object *) switch_core_session_alloc(session,
sizeof(struct private_object)))) {
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;
switch_clear_flag(tech_pvt, TFLAG_IO);
switch_clear_flag(tech_pvt, TFLAG_VOICE);
if ((channel = switch_core_session_get_channel(tech_pvt->session))) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Hangup %s\n", switch_channel_get_name(channel));
switch_set_flag(tech_pvt, TFLAG_HANGUP);
switch_channel_hangup(channel);
//switch_thread_cond_signal(tech_pvt->cond);
iaxevent->session = NULL;
} else {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Hey where is my memory pool?\n");
switch_core_session_destroy(&session);
break;
}
if ((tech_pvt->caller_profile = switch_caller_profile_new(session,
globals.dialplan,
iaxevent->ies.calling_name,
iaxevent->ies.calling_number,
iax_get_peer_ip(iaxevent->session),
iaxevent->ies.calling_ani,
NULL, iaxevent->ies.called_number))) {
char name[128];
switch_channel_set_caller_profile(channel, tech_pvt->caller_profile);
snprintf(name, sizeof(name), "IAX/%s-%04x", tech_pvt->caller_profile->destination_number,
rand() & 0xffff);
switch_channel_set_name(channel, name);
}
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);
} else {
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);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "No Session? %s\n", switch_test_flag(tech_pvt, TFLAG_VOICE) ? "yes" : "no");
}
}
}
break;
case IAX_EVENT_REJECT:
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Rejected call.\n");
case IAX_EVENT_BUSY:
case IAX_EVENT_HANGUP:
if (tech_pvt) {
switch_channel *channel;
switch_clear_flag(tech_pvt, TFLAG_IO);
switch_clear_flag(tech_pvt, TFLAG_VOICE);
if ((channel = switch_core_session_get_channel(tech_pvt->session))) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Hangup %s\n", switch_channel_get_name(channel));
switch_set_flag(tech_pvt, TFLAG_HANGUP);
switch_channel_hangup(channel);
//switch_thread_cond_signal(tech_pvt->cond);
iaxevent->session = NULL;
} else {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "No Session? %s\n",
switch_test_flag(tech_pvt, TFLAG_VOICE) ? "yes" : "no");
}
}
break;
case IAX_EVENT_CNG:
// pseudo-silence
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "sending silence\n");
break;
case IAX_EVENT_VOICE:
if (tech_pvt && (tech_pvt->read_frame.datalen = iaxevent->datalen)) {
switch_channel *channel;
if ((channel = switch_core_session_get_channel(tech_pvt->session))
&& switch_channel_get_state(channel) <= CS_HANGUP) {
int bytes = tech_pvt->read_codec.implementation->encoded_bytes_per_frame;
int frames = (int) (tech_pvt->read_frame.datalen / bytes);
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:
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Call transfer occurred.\n");
//session[0] = iaxevent->session;
break;
case IAX_EVENT_DTMF:
if (tech_pvt) {
switch_channel *channel;
if ((channel = switch_core_session_get_channel(tech_pvt->session))) {
char str[2] = { iaxevent->subclass };
if (globals.debug) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "%s DTMF %s\n", str,
switch_channel_get_name(channel));
break;
case IAX_EVENT_CNG:
// pseudo-silence
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "sending silence\n");
break;
case IAX_EVENT_VOICE:
if (tech_pvt && (tech_pvt->read_frame.datalen = iaxevent->datalen)) {
switch_channel *channel;
if ((channel = switch_core_session_get_channel(tech_pvt->session)) && switch_channel_get_state(channel) <= CS_HANGUP) {
int bytes = tech_pvt->read_codec.implementation->encoded_bytes_per_frame;
int frames = (int)(tech_pvt->read_frame.datalen / bytes);
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:
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Call transfer occurred.\n");
//session[0] = iaxevent->session;
break;
case IAX_EVENT_DTMF:
if (tech_pvt) {
switch_channel *channel;
if ((channel = switch_core_session_get_channel(tech_pvt->session))) {
char str[2] = {iaxevent->subclass};
if (globals.debug) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "%s DTMF %s\n", str, switch_channel_get_name(channel));
}
switch_channel_queue_dtmf(channel, str);
}
switch_channel_queue_dtmf(channel, str);
}
}
break;
default:
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Don't know what to do with IAX event %d.\n",
iaxevent->etype);
break;
break;
default:
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Don't know what to do with IAX event %d.\n", iaxevent->etype);
break;
}
iax_event_free(iaxevent);
@@ -1097,3 +1075,4 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_shutdown(void)
}
return SWITCH_STATUS_SUCCESS;
}
+44 -52
View File
@@ -54,7 +54,7 @@ struct private_object {
struct switch_frame frame;
unsigned char databuf[SWITCH_RECCOMMENDED_BUFFER_SIZE];
switch_core_session *session;
switch_caller_profile *caller_profile;
switch_caller_profile *caller_profile;
};
@@ -65,12 +65,9 @@ static switch_status channel_on_hangup(switch_core_session *session);
static switch_status channel_on_ring(switch_core_session *session);
static switch_status channel_on_loopback(switch_core_session *session);
static switch_status channel_on_transmit(switch_core_session *session);
static switch_status channel_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile,
switch_core_session **new_session);
static switch_status channel_read_frame(switch_core_session *session, switch_frame **frame, int timeout,
switch_io_flag flags, int stream_id);
static switch_status channel_write_frame(switch_core_session *session, switch_frame *frame, int timeout,
switch_io_flag flags, int stream_id);
static switch_status channel_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile, switch_core_session **new_session);
static switch_status channel_read_frame(switch_core_session *session, switch_frame **frame, int timeout, switch_io_flag flags, int stream_id);
static switch_status channel_write_frame(switch_core_session *session, switch_frame *frame, int timeout, switch_io_flag flags, int stream_id);
static switch_status channel_kill_channel(switch_core_session *session, int sig);
@@ -122,7 +119,7 @@ static switch_status channel_on_execute(switch_core_session *session)
tech_pvt = switch_core_session_get_private(session);
assert(tech_pvt != NULL);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "%s CHANNEL EXECUTE\n", switch_channel_get_name(channel));
@@ -133,7 +130,7 @@ static switch_status channel_on_hangup(switch_core_session *session)
{
switch_channel *channel = NULL;
struct private_object *tech_pvt = NULL;
channel = switch_core_session_get_channel(session);
assert(channel != NULL);
@@ -150,7 +147,7 @@ static switch_status channel_kill_channel(switch_core_session *session, int sig)
{
switch_channel *channel = NULL;
struct private_object *tech_pvt = NULL;
channel = switch_core_session_get_channel(session);
assert(channel != NULL);
@@ -160,7 +157,7 @@ static switch_status channel_kill_channel(switch_core_session *session, int sig)
switch_channel_hangup(channel);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "%s CHANNEL KILL\n", switch_channel_get_name(channel));
return SWITCH_STATUS_SUCCESS;
}
@@ -180,17 +177,15 @@ static switch_status channel_on_transmit(switch_core_session *session)
/* 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!
*/
static switch_status channel_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile,
switch_core_session **new_session)
static switch_status channel_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile, switch_core_session **new_session)
{
if ((*new_session = switch_core_session_request(&channel_endpoint_interface, NULL))) {
struct private_object *tech_pvt;
switch_channel *channel, *orig_channel;
switch_caller_profile *caller_profile, *originator_caller_profile = NULL;
switch_core_session_add_stream(*new_session, NULL);
if ((tech_pvt =
(struct private_object *) switch_core_session_alloc(*new_session, sizeof(struct private_object)))) {
if ((tech_pvt = (struct private_object *) switch_core_session_alloc(*new_session, sizeof(struct private_object)))) {
memset(tech_pvt, 0, sizeof(*tech_pvt));
channel = switch_core_session_get_channel(*new_session);
switch_core_session_set_private(*new_session, tech_pvt);
@@ -223,7 +218,7 @@ static switch_status channel_outgoing_channel(switch_core_session *session, swit
switch_channel_set_originator_caller_profile(channel, cloned_profile);
}
}
switch_channel_set_flag(channel, CF_OUTBOUND);
switch_set_flag(tech_pvt, TFLAG_OUTBOUND);
switch_channel_set_state(channel, CS_INIT);
@@ -255,8 +250,7 @@ static switch_status channel_waitfor_write(switch_core_session *session, int ms,
}
static switch_status channel_read_frame(switch_core_session *session, switch_frame **frame, int timeout,
switch_io_flag flags, int stream_id)
static switch_status channel_read_frame(switch_core_session *session, switch_frame **frame, int timeout, switch_io_flag flags, int stream_id)
{
switch_channel *channel = NULL;
struct private_object *tech_pvt = NULL;
@@ -272,8 +266,7 @@ static switch_status channel_read_frame(switch_core_session *session, switch_fra
return SWITCH_STATUS_SUCCESS;
}
static switch_status channel_write_frame(switch_core_session *session, switch_frame *frame, int timeout,
switch_io_flag flags, int stream_id)
static switch_status channel_write_frame(switch_core_session *session, switch_frame *frame, int timeout, switch_io_flag flags, int stream_id)
{
switch_channel *channel = NULL;
struct private_object *tech_pvt = NULL;
@@ -286,43 +279,43 @@ static switch_status channel_write_frame(switch_core_session *session, switch_fr
assert(tech_pvt != NULL);
return SWITCH_STATUS_SUCCESS;
}
static const switch_event_handler_table channel_event_handlers = {
/*.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
/*.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
};
static const switch_io_routines channel_io_routines = {
/*.outgoing_channel */ channel_outgoing_channel,
/*.answer_channel */ NULL,
/*.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
/*.outgoing_channel*/ channel_outgoing_channel,
/*.answer_channel*/ NULL,
/*.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
};
static const switch_endpoint_interface channel_endpoint_interface = {
/*.interface_name */ "opal",
/*.io_routines */ &channel_io_routines,
/*.event_handlers */ &channel_event_handlers,
/*.private */ NULL,
/*.next */ NULL
/*.interface_name*/ "opal",
/*.io_routines*/ &channel_io_routines,
/*.event_handlers*/ &channel_event_handlers,
/*.private*/ NULL,
/*.next*/ NULL
};
static const switch_loadable_module_interface channel_module_interface = {
/*.module_name */ modname,
/*.endpoint_interface */ &channel_endpoint_interface,
/*.timer_interface */ NULL,
/*.dialplan_interface */ NULL,
/*.codec_interface */ NULL,
/*.application_interface */ NULL
/*.module_name*/ modname,
/*.endpoint_interface*/ &channel_endpoint_interface,
/*.timer_interface*/ NULL,
/*.dialplan_interface*/ NULL,
/*.codec_interface*/ NULL,
/*.application_interface*/ NULL
};
@@ -345,20 +338,19 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_shutdown(void)
}
*/
SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_module_interface **interface, char *filename)
{
SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_module_interface **interface, char *filename) {
switch_config cfg;
char *var, *val;
char *cf = "opal.conf";
memset(&globals, 0, sizeof(globals));
if (!switch_config_open_file(&cfg, cf)) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "open of %s failed\n", cf);
return SWITCH_STATUS_TERM;
}
while (switch_config_next_pair(&cfg, &var, &val)) {
if (!strcasecmp(cfg.category, "settings")) {
@@ -372,7 +364,7 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_modul
switch_config_close_file(&cfg);
if (switch_core_new_memory_pool(&module_pool) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "OH OH no pool\n");
return SWITCH_STATUS_TERM;
+233 -250
View File
@@ -81,55 +81,51 @@ static struct {
struct private_object {
unsigned int flags;
switch_codec read_codec;
switch_codec write_codec;
switch_codec write_codec;
struct switch_frame read_frame;
unsigned char databuf[SWITCH_RECCOMMENDED_BUFFER_SIZE];
switch_core_session *session;
switch_caller_profile *caller_profile;
switch_caller_profile *caller_profile;
char call_id[50];
PaError err;
PABLIO_Stream *audio_in;
PABLIO_Stream *audio_out;
PABLIO_Stream *audio_in;
PABLIO_Stream *audio_out;
int indev;
int outdev;
};
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_dialplan, globals.dialplan)
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_cid_name, globals.cid_name)
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_cid_num, globals.cid_num)
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_cid_name, globals.cid_name)
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_cid_num, globals.cid_num)
static const switch_endpoint_interface channel_endpoint_interface;
static const switch_endpoint_interface channel_endpoint_interface;
static switch_status channel_on_init(switch_core_session *session);
static switch_status channel_on_hangup(switch_core_session *session);
static switch_status channel_on_ring(switch_core_session *session);
static switch_status channel_on_loopback(switch_core_session *session);
static switch_status channel_on_transmit(switch_core_session *session);
static switch_status channel_outgoing_channel(switch_core_session *session,
switch_caller_profile *outbound_profile,
switch_core_session **new_session);
static switch_status channel_read_frame(switch_core_session *session, switch_frame **frame, int timeout,
switch_io_flag flags, int stream_id);
static switch_status channel_write_frame(switch_core_session *session, switch_frame *frame, int timeout,
switch_io_flag flags, int stream_id);
static switch_status channel_kill_channel(switch_core_session *session, int sig);
static switch_status engage_device(struct private_object *tech_pvt);
static int dump_info(void);
static switch_status load_config(void);
static int get_dev_by_name(char *name, int in);
static switch_status place_call(char *dest, char *out, size_t outlen);
static switch_status hup_call(char *callid, char *out, size_t outlen);
static switch_status call_info(char *callid, char *out, size_t outlen);
static switch_status send_dtmf(char *callid, char *out, size_t outlen);
static switch_status answer_call(char *callid, char *out, size_t outlen);
static switch_status channel_on_init(switch_core_session *session);
static switch_status channel_on_hangup(switch_core_session *session);
static switch_status channel_on_ring(switch_core_session *session);
static switch_status channel_on_loopback(switch_core_session *session);
static switch_status channel_on_transmit(switch_core_session *session);
static switch_status channel_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile, switch_core_session **new_session);
static switch_status channel_read_frame(switch_core_session *session, switch_frame **frame, int timeout, switch_io_flag flags, int stream_id);
static switch_status channel_write_frame(switch_core_session *session, switch_frame *frame, int timeout, switch_io_flag flags, int stream_id);
static switch_status channel_kill_channel(switch_core_session *session, int sig);
static switch_status engage_device(struct private_object *tech_pvt);
static int dump_info(void);
static switch_status load_config(void);
static int get_dev_by_name(char *name, int in);
static switch_status place_call(char *dest, char *out, size_t outlen);
static switch_status hup_call(char *callid, char *out, size_t outlen);
static switch_status call_info(char *callid, char *out, size_t outlen);
static switch_status send_dtmf(char *callid, char *out, size_t outlen);
static switch_status answer_call(char *callid, char *out, size_t outlen);
/*
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.
*/
static switch_status channel_on_init(switch_core_session *session)
static switch_status channel_on_init(switch_core_session *session)
{
switch_channel *channel;
struct private_object *tech_pvt = NULL;
@@ -144,7 +140,7 @@ SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_global_dialplan, globals.dialplan)
tech_pvt->read_frame.buflen = sizeof(tech_pvt->databuf);
switch_set_flag(tech_pvt, TFLAG_IO);
/* Move Channel's State Machine to RING */
switch_channel_set_state(channel, CS_RING);
@@ -178,7 +174,7 @@ static switch_status channel_on_execute(switch_core_session *session)
tech_pvt = switch_core_session_get_private(session);
assert(tech_pvt != NULL);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "%s CHANNEL EXECUTE\n", switch_channel_get_name(channel));
@@ -227,7 +223,7 @@ static switch_status channel_kill_channel(switch_core_session *session, int sig)
{
switch_channel *channel = NULL;
struct private_object *tech_pvt = NULL;
channel = switch_core_session_get_channel(session);
assert(channel != NULL);
@@ -240,7 +236,7 @@ static switch_status channel_kill_channel(switch_core_session *session, int sig)
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "%s CHANNEL KILL\n", switch_channel_get_name(channel));
return SWITCH_STATUS_SUCCESS;
}
@@ -268,25 +264,25 @@ static switch_status channel_on_transmit(switch_core_session *session)
/* Turn on the device */
engage_device(tech_pvt);
while (switch_channel_get_state(channel) == CS_TRANSMIT && !switch_test_flag(tech_pvt, TFLAG_ANSWER)) {
while(switch_channel_get_state(channel) == CS_TRANSMIT && !switch_test_flag(tech_pvt, TFLAG_ANSWER)) {
if (switch_time_now() - last >= waitsec) {
char buf[512];
switch_event *event;
char buf[512];
switch_event *event;
snprintf(buf, sizeof(buf), "BRRRRING! BRRRRING! call %s\n", tech_pvt->call_id);
snprintf(buf, sizeof(buf), "BRRRRING! BRRRRING! call %s\n", tech_pvt->call_id);
if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, MY_EVENT_RINGING) == SWITCH_STATUS_SUCCESS) {
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "event_info", buf);
switch_event_fire(&event);
}
if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, MY_EVENT_RINGING) == SWITCH_STATUS_SUCCESS) {
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "event_info", buf);
switch_event_fire(&event);
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "%s\n", buf);
last = switch_time_now();
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "%s\n", buf);
last = switch_time_now();
}
switch_yield(50000);
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "CHANNEL TRANSMIT\n");
@@ -297,8 +293,7 @@ static switch_status channel_on_transmit(switch_core_session *session)
/* 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!
*/
static switch_status channel_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile,
switch_core_session **new_session)
static switch_status channel_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile, switch_core_session **new_session)
{
if ((*new_session = switch_core_session_request(&channel_endpoint_interface, NULL))) {
struct private_object *tech_pvt;
@@ -306,8 +301,7 @@ static switch_status channel_outgoing_channel(switch_core_session *session, swit
switch_caller_profile *caller_profile;
switch_core_session_add_stream(*new_session, NULL);
if ((tech_pvt =
(struct private_object *) switch_core_session_alloc(*new_session, sizeof(struct private_object)))) {
if ((tech_pvt = (struct private_object *) switch_core_session_alloc(*new_session, sizeof(struct private_object)))) {
memset(tech_pvt, 0, sizeof(*tech_pvt));
channel = switch_core_session_get_channel(*new_session);
switch_core_session_set_private(*new_session, tech_pvt);
@@ -323,9 +317,7 @@ static switch_status channel_outgoing_channel(switch_core_session *session, swit
caller_profile = switch_caller_profile_clone(*new_session, outbound_profile);
switch_channel_set_caller_profile(channel, caller_profile);
tech_pvt->caller_profile = caller_profile;
snprintf(name, sizeof(name), "PortAudio/%s-%04x",
caller_profile->destination_number ? caller_profile->destination_number : modname,
rand() & 0xffff);
snprintf(name, sizeof(name), "PortAudio/%s-%04x", caller_profile->destination_number ? caller_profile->destination_number : modname, rand() & 0xffff);
switch_channel_set_name(channel, name);
} else {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Doh! no caller profile\n");
@@ -373,11 +365,10 @@ static switch_status channel_send_dtmf(switch_core_session *session, char *dtmf)
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "DTMF ON CALL %s [%s]\n", tech_pvt->call_id, dtmf);
return SWITCH_STATUS_SUCCESS;
return SWITCH_STATUS_SUCCESS;
}
static switch_status channel_read_frame(switch_core_session *session, switch_frame **frame, int timeout,
switch_io_flag flags, int stream_id)
static switch_status channel_read_frame(switch_core_session *session, switch_frame **frame, int timeout, switch_io_flag flags, int stream_id)
{
switch_channel *channel = NULL;
struct private_object *tech_pvt = NULL;
@@ -394,12 +385,10 @@ static switch_status channel_read_frame(switch_core_session *session, switch_fra
return SWITCH_STATUS_FALSE;
}
switch_mutex_lock(globals.device_lock);
if (tech_pvt->audio_in &&
(samples =
ReadAudioStream(tech_pvt->audio_in, tech_pvt->read_frame.data,
tech_pvt->read_codec.implementation->samples_per_frame))) {
if (tech_pvt->audio_in &&
(samples = ReadAudioStream(tech_pvt->audio_in, tech_pvt->read_frame.data, tech_pvt->read_codec.implementation->samples_per_frame))) {
tech_pvt->read_frame.datalen = samples * 2;
tech_pvt->read_frame.samples = samples;
*frame = &tech_pvt->read_frame;
@@ -410,8 +399,7 @@ static switch_status channel_read_frame(switch_core_session *session, switch_fra
return status;
}
static switch_status channel_write_frame(switch_core_session *session, switch_frame *frame, int timeout,
switch_io_flag flags, int stream_id)
static switch_status channel_write_frame(switch_core_session *session, switch_frame *frame, int timeout, switch_io_flag flags, int stream_id)
{
switch_channel *channel = NULL;
struct private_object *tech_pvt = NULL;
@@ -422,18 +410,19 @@ static switch_status channel_write_frame(switch_core_session *session, switch_fr
tech_pvt = switch_core_session_get_private(session);
assert(tech_pvt != NULL);
if (!switch_test_flag(tech_pvt, TFLAG_IO)) {
if(!switch_test_flag(tech_pvt, TFLAG_IO)) {
return SWITCH_STATUS_FALSE;
}
//switch_mutex_lock(globals.device_lock);
if (tech_pvt->audio_out) {
WriteAudioStream(tech_pvt->audio_out, (short *) frame->data, (int) (frame->datalen / sizeof(SAMPLE)));
WriteAudioStream(tech_pvt->audio_out, (short *)frame->data, (int)(frame->datalen / sizeof(SAMPLE)));
status = SWITCH_STATUS_SUCCESS;
}
//switch_mutex_unlock(globals.device_lock);
return status;
}
static switch_status channel_answer_channel(switch_core_session *session)
@@ -452,83 +441,82 @@ static switch_status channel_answer_channel(switch_core_session *session)
static struct switch_api_interface send_dtmf_interface = {
/*.interface_name */ "padtmf",
/*.desc */ "PortAudio Dial DTMF",
/*.function */ send_dtmf,
/*.next */ NULL
/*.interface_name*/ "padtmf",
/*.desc*/ "PortAudio Dial DTMF",
/*.function*/ send_dtmf,
/*.next*/ NULL
};
static struct switch_api_interface answer_call_interface = {
/*.interface_name */ "paoffhook",
/*.desc */ "PortAudio Answer Call",
/*.function */ answer_call,
/*.next */ &send_dtmf_interface
/*.interface_name*/ "paoffhook",
/*.desc*/ "PortAudio Answer Call",
/*.function*/ answer_call,
/*.next*/ &send_dtmf_interface
};
static struct switch_api_interface channel_info_interface = {
/*.interface_name */ "painfo",
/*.desc */ "PortAudio Call Info",
/*.function */ call_info,
/*.next */ &answer_call_interface
/*.interface_name*/ "painfo",
/*.desc*/ "PortAudio Call Info",
/*.function*/ call_info,
/*.next*/ &answer_call_interface
};
static struct switch_api_interface channel_hup_interface = {
/*.interface_name */ "pahup",
/*.desc */ "PortAudio Hangup Call",
/*.function */ hup_call,
/*.next */ &channel_info_interface
/*.interface_name*/ "pahup",
/*.desc*/ "PortAudio Hangup Call",
/*.function*/ hup_call,
/*.next*/ &channel_info_interface
};
static struct switch_api_interface channel_api_interface = {
/*.interface_name */ "pacall",
/*.desc */ "PortAudio Call",
/*.function */ place_call,
/*.next */ &channel_hup_interface
/*.interface_name*/ "pacall",
/*.desc*/ "PortAudio Call",
/*.function*/ place_call,
/*.next*/ &channel_hup_interface
};
static const switch_event_handler_table channel_event_handlers = {
/*.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
/*.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
};
static const switch_io_routines channel_io_routines = {
/*.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
/*.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
};
static const switch_endpoint_interface channel_endpoint_interface = {
/*.interface_name */ "portaudio",
/*.io_routines */ &channel_io_routines,
/*.event_handlers */ &channel_event_handlers,
/*.private */ NULL,
/*.next */ NULL
/*.interface_name*/ "portaudio",
/*.io_routines*/ &channel_io_routines,
/*.event_handlers*/ &channel_event_handlers,
/*.private*/ NULL,
/*.next*/ NULL
};
static const switch_loadable_module_interface channel_module_interface = {
/*.module_name */ modname,
/*.endpoint_interface */ &channel_endpoint_interface,
/*.timer_interface */ NULL,
/*.dialplan_interface */ NULL,
/*.codec_interface */ NULL,
/*.application_interface */ NULL,
/*.api_interface */ &channel_api_interface
/*.module_name*/ modname,
/*.endpoint_interface*/ &channel_endpoint_interface,
/*.timer_interface*/ NULL,
/*.dialplan_interface*/ NULL,
/*.codec_interface*/ NULL,
/*.application_interface*/ NULL,
/*.api_interface*/ &channel_api_interface
};
SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_module_interface **interface, char *filename)
{
SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_module_interface **interface, char *filename) {
if (switch_core_new_memory_pool(&module_pool) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "OH OH no pool\n");
@@ -540,7 +528,7 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_modul
load_config();
switch_core_hash_init(&globals.call_hash, module_pool);
switch_mutex_init(&globals.device_lock, SWITCH_MUTEX_NESTED, module_pool);
dump_info();
if (switch_event_reserve_subclass(MY_EVENT_RINGING) != SWITCH_STATUS_SUCCESS) {
@@ -561,14 +549,14 @@ static switch_status load_config(void)
switch_config cfg;
char *var, *val;
char *cf = "portaudio.conf";
memset(&globals, 0, sizeof(globals));
if (!switch_config_open_file(&cfg, cf)) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "open of %s failed\n", cf);
return SWITCH_STATUS_TERM;
}
while (switch_config_next_pair(&cfg, &var, &val)) {
if (!strcasecmp(cfg.category, "settings")) {
if (!strcmp(var, "debug")) {
@@ -583,13 +571,13 @@ static switch_status load_config(void)
set_global_cid_num(val);
} else if (!strcmp(var, "indev")) {
if (*val == '#') {
globals.indev = atoi(val + 1);
globals.indev = atoi(val+1);
} else {
globals.indev = get_dev_by_name(val, 1);
}
} else if (!strcmp(var, "outdev")) {
if (*val == '#') {
globals.outdev = atoi(val + 1);
globals.outdev = atoi(val+1);
} else {
globals.outdev = get_dev_by_name(val, 0);
}
@@ -600,13 +588,13 @@ static switch_status load_config(void)
if (!globals.dialplan) {
set_global_dialplan("default");
}
if (!globals.sample_rate) {
globals.sample_rate = 8000;
}
switch_config_close_file(&cfg);
return SWITCH_STATUS_SUCCESS;
}
@@ -622,28 +610,28 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_runtime(void)
SWITCH_MOD_DECLARE(switch_status) switch_module_shutdown(void)
{
Pa_Terminate();
Pa_Terminate();
return SWITCH_STATUS_SUCCESS;
}
static int get_dev_by_name(char *name, int in)
{
int i;
int numDevices;
const PaDeviceInfo *pdi;
numDevices = Pa_CountDevices();
int i;
int numDevices;
const PaDeviceInfo *pdi;
numDevices = Pa_CountDevices();
if (numDevices < 0) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "ERROR: Pa_CountDevices returned 0x%x\n", numDevices);
return -2;
}
for (i = 0; i < numDevices; i++) {
pdi = Pa_GetDeviceInfo(i);
if (strstr(pdi->name, name)) {
if (in && pdi->maxInputChannels) {
if( numDevices < 0 ) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "ERROR: Pa_CountDevices returned 0x%x\n", numDevices );
return -2;
}
for( i=0; i<numDevices; i++ ) {
pdi = Pa_GetDeviceInfo( i );
if(strstr(pdi->name, name)) {
if(in && pdi->maxInputChannels) {
return i;
} else if (!in && pdi->maxOutputChannels) {
return i;
@@ -656,62 +644,58 @@ static int get_dev_by_name(char *name, int in)
static int dump_info(void)
{
int i, j;
int numDevices;
const PaDeviceInfo *pdi;
PaError err;
numDevices = Pa_CountDevices();
if (numDevices < 0) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "ERROR: Pa_CountDevices returned 0x%x\n", numDevices);
err = numDevices;
goto error;
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "Number of devices = %d\n", numDevices);
for (i = 0; i < numDevices; i++) {
pdi = Pa_GetDeviceInfo(i);
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "---------------------------------------------- #%d", i);
if (i == Pa_GetDefaultInputDeviceID())
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, " DefaultInput");
if (i == Pa_GetDefaultOutputDeviceID())
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, " DefaultOutput");
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "\nName = %s\n", pdi->name);
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "Max Inputs = %d", pdi->maxInputChannels);
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, ", Max Outputs = %d\n", pdi->maxOutputChannels);
if (pdi->numSampleRates == -1) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "Sample Rate Range = %f to %f\n", pdi->sampleRates[0],
pdi->sampleRates[1]);
} else {
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "Sample Rates =");
for (j = 0; j < pdi->numSampleRates; j++) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, " %8.2f,", pdi->sampleRates[j]);
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "\n");
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "Native Sample Formats = ");
if (pdi->nativeSampleFormats & paInt8)
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "paInt8, ");
if (pdi->nativeSampleFormats & paUInt8)
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "paUInt8, ");
if (pdi->nativeSampleFormats & paInt16)
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "paInt16, ");
if (pdi->nativeSampleFormats & paInt32)
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "paInt32, ");
if (pdi->nativeSampleFormats & paFloat32)
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "paFloat32, ");
if (pdi->nativeSampleFormats & paInt24)
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "paInt24, ");
if (pdi->nativeSampleFormats & paPackedInt24)
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "paPackedInt24, ");
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "\n");
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "----------------------------------------------\n");
return 0;
error:
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "An error occured while using the portaudio stream\n");
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "Error number: %d\n", err);
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "Error message: %s\n", Pa_GetErrorText(err));
return err;
int i,j;
int numDevices;
const PaDeviceInfo *pdi;
PaError err;
numDevices = Pa_CountDevices();
if( numDevices < 0 )
{
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "ERROR: Pa_CountDevices returned 0x%x\n", numDevices );
err = numDevices;
goto error;
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "Number of devices = %d\n", numDevices );
for( i=0; i<numDevices; i++ )
{
pdi = Pa_GetDeviceInfo( i );
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "---------------------------------------------- #%d", i );
if( i == Pa_GetDefaultInputDeviceID() ) switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, " DefaultInput");
if( i == Pa_GetDefaultOutputDeviceID() ) switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, " DefaultOutput");
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "\nName = %s\n", pdi->name );
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "Max Inputs = %d", pdi->maxInputChannels );
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, ", Max Outputs = %d\n", pdi->maxOutputChannels );
if( pdi->numSampleRates == -1 )
{
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "Sample Rate Range = %f to %f\n", pdi->sampleRates[0], pdi->sampleRates[1] );
}
else
{
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "Sample Rates =");
for( j=0; j<pdi->numSampleRates; j++ )
{
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, " %8.2f,", pdi->sampleRates[j] );
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "\n");
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "Native Sample Formats = ");
if( pdi->nativeSampleFormats & paInt8 ) switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "paInt8, ");
if( pdi->nativeSampleFormats & paUInt8 ) switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "paUInt8, ");
if( pdi->nativeSampleFormats & paInt16 ) switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "paInt16, ");
if( pdi->nativeSampleFormats & paInt32 ) switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "paInt32, ");
if( pdi->nativeSampleFormats & paFloat32 ) switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "paFloat32, ");
if( pdi->nativeSampleFormats & paInt24 ) switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "paInt24, ");
if( pdi->nativeSampleFormats & paPackedInt24 ) switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "paPackedInt24, ");
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "\n");
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "----------------------------------------------\n");
return 0;
error:
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "An error occured while using the portaudio stream\n" );
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "Error number: %d\n", err );
switch_console_printf(SWITCH_CHANNEL_CONSOLE_CLEAN, "Error message: %s\n", Pa_GetErrorText( err ) );
return err;
}
static switch_status engage_device(struct private_object *tech_pvt)
@@ -729,59 +713,56 @@ static switch_status engage_device(struct private_object *tech_pvt)
codec_ms,
1,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL, switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't load codec?\n");
return SWITCH_STATUS_FALSE;
} else {
if (switch_core_codec_init(&tech_pvt->write_codec,
"L16",
sample_rate,
codec_ms,
1,
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE,
NULL, switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) {
NULL,
switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't load codec?\n");
switch_core_codec_destroy(&tech_pvt->read_codec);
return SWITCH_STATUS_FALSE;
} else {
if (switch_core_codec_init(&tech_pvt->write_codec,
"L16",
sample_rate,
codec_ms,
1,
SWITCH_CODEC_FLAG_ENCODE |SWITCH_CODEC_FLAG_DECODE,
NULL,
switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't load codec?\n");
switch_core_codec_destroy(&tech_pvt->read_codec);
return SWITCH_STATUS_FALSE;
}
}
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Loaded codec L16 %dhz %dms on %s\n", sample_rate, codec_ms,
switch_channel_get_name(channel));
tech_pvt->read_frame.rate = sample_rate;
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);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Loaded codec L16 %dhz %dms on %s\n", sample_rate, codec_ms, switch_channel_get_name(channel));
tech_pvt->read_frame.rate = sample_rate;
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);
tech_pvt->indev = globals.indev;
tech_pvt->outdev = globals.outdev;
switch_mutex_lock(globals.device_lock);
if ((tech_pvt->err =
OpenAudioStream(&tech_pvt->audio_in, sample_rate, SAMPLE_TYPE, PABLIO_READ | PABLIO_MONO, tech_pvt->indev,
-1)) == paNoError) {
if ((tech_pvt->err =
OpenAudioStream(&tech_pvt->audio_out, sample_rate, SAMPLE_TYPE, PABLIO_WRITE | PABLIO_MONO, -1,
tech_pvt->outdev)) != paNoError) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't open audio out [%d]!\n", tech_pvt->outdev);
CloseAudioStream(tech_pvt->audio_in);
tech_pvt->audio_in = NULL;
tech_pvt->indev = globals.indev;
tech_pvt->outdev = globals.outdev;
switch_mutex_lock(globals.device_lock);
if ((tech_pvt->err = OpenAudioStream( &tech_pvt->audio_in, sample_rate, SAMPLE_TYPE, PABLIO_READ | PABLIO_MONO, tech_pvt->indev, -1)) == paNoError) {
if ((tech_pvt->err = OpenAudioStream(&tech_pvt->audio_out, sample_rate, SAMPLE_TYPE, PABLIO_WRITE | PABLIO_MONO, -1, tech_pvt->outdev)) != paNoError) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't open audio out [%d]!\n", tech_pvt->outdev);
CloseAudioStream(tech_pvt->audio_in);
tech_pvt->audio_in = NULL;
}
} else {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't open audio in [%d]!\n", tech_pvt->indev);
}
} else {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't open audio in [%d]!\n", tech_pvt->indev);
}
switch_mutex_unlock(globals.device_lock);
switch_mutex_unlock(globals.device_lock);
if (tech_pvt->err == paNoError) {
snprintf(tech_pvt->call_id, sizeof(tech_pvt->call_id), "%d", globals.call_id++);
switch_core_hash_insert(globals.call_hash, tech_pvt->call_id, tech_pvt);
return SWITCH_STATUS_SUCCESS;
} else {
switch_core_codec_destroy(&tech_pvt->read_codec);
switch_core_codec_destroy(&tech_pvt->write_codec);
switch_core_session_destroy(&tech_pvt->session);
}
return SWITCH_STATUS_FALSE;
if (tech_pvt->err == paNoError) {
snprintf(tech_pvt->call_id, sizeof(tech_pvt->call_id), "%d", globals.call_id++);
switch_core_hash_insert(globals.call_hash, tech_pvt->call_id, tech_pvt);
return SWITCH_STATUS_SUCCESS;
} else {
switch_core_codec_destroy(&tech_pvt->read_codec);
switch_core_codec_destroy(&tech_pvt->write_codec);
switch_core_session_destroy(&tech_pvt->session);
}
return SWITCH_STATUS_FALSE;
}
static switch_status place_call(char *dest, char *out, size_t outlen)
@@ -815,12 +796,14 @@ static switch_status place_call(char *dest, char *out, size_t outlen)
if ((tech_pvt->caller_profile = switch_caller_profile_new(session,
globals.dialplan,
globals.cid_name,
globals.cid_num, NULL, NULL, NULL, dest))) {
globals.cid_num,
NULL,
NULL,
NULL,
dest))) {
char name[128];
switch_channel_set_caller_profile(channel, tech_pvt->caller_profile);
snprintf(name, sizeof(name), "PortAudio/%s-%04x",
tech_pvt->caller_profile->destination_number ? tech_pvt->caller_profile->
destination_number : modname, rand() & 0xffff);
snprintf(name, sizeof(name), "PortAudio/%s-%04x", tech_pvt->caller_profile->destination_number ? tech_pvt->caller_profile->destination_number : modname, rand() & 0xffff);
switch_channel_set_name(channel, name);
}
tech_pvt->session = session;
@@ -829,7 +812,7 @@ static switch_status place_call(char *dest, char *out, size_t outlen)
switch_core_session_thread_launch(tech_pvt->session);
snprintf(out, outlen, "SUCCESS: %s", tech_pvt->call_id);
}
}
}
return status;
}
@@ -845,7 +828,7 @@ static switch_status hup_call(char *callid, char *out, size_t outlen)
callid = tmp;
}
if (!callid || !strcasecmp(callid, "all")) {
switch_hash_index_t *hi;
switch_hash_index_t* hi;
void *val;
int i = 0;
@@ -862,7 +845,7 @@ static switch_status hup_call(char *callid, char *out, size_t outlen)
return SWITCH_STATUS_SUCCESS;
}
if ((tech_pvt = switch_core_hash_find(globals.call_hash, callid))) {
channel = switch_core_session_get_channel(tech_pvt->session);
@@ -926,7 +909,7 @@ static void print_info(struct private_object *tech_pvt, char *out, size_t outlen
assert(channel != NULL);
snprintf(out, outlen, "CALL %s\t%s\t%s\t%s\t%s\n",
tech_pvt->call_id,
tech_pvt->call_id,
tech_pvt->caller_profile->caller_id_name ? tech_pvt->caller_profile->caller_id_name : "n/a",
tech_pvt->caller_profile->caller_id_number ? tech_pvt->caller_profile->caller_id_number : "n/a",
tech_pvt->caller_profile->destination_number ? tech_pvt->caller_profile->destination_number : "n/a",
@@ -937,19 +920,19 @@ static void print_info(struct private_object *tech_pvt, char *out, size_t outlen
static switch_status call_info(char *callid, char *out, size_t outlen)
{
struct private_object *tech_pvt;
switch_hash_index_t *hi;
switch_hash_index_t* hi;
void *val;
if (!callid || !strcasecmp(callid, "all")) {
for (hi = apr_hash_first(module_pool, globals.call_hash); hi; hi = switch_hash_next(hi)) {
switch_hash_this(hi, NULL, NULL, &val);
tech_pvt = val;
print_info(tech_pvt, out + strlen(out), outlen - strlen(out));
print_info(tech_pvt, out + strlen(out), outlen - strlen(out));
}
} else if ((tech_pvt = switch_core_hash_find(globals.call_hash, callid))) {
print_info(tech_pvt, out, outlen);
} else {
strncpy(out, "NO SUCH CALL", outlen - 1);
}
return SWITCH_STATUS_SUCCESS;
}
+260 -266
View File
@@ -32,12 +32,12 @@
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
*/
/* History:
* PLB021214 - check for valid stream in CloseAudioStream() to prevent hang.
* add timeOutMSec to CloseAudioStream() to prevent hang.
*/
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
@@ -45,155 +45,146 @@
#include "ringbuffer.h"
#include "pablio.h"
#include <string.h>
/************************************************************************/
/******** Constants *****************************************************/
/************************************************************************/
/************************************************************************/
/******** Constants *****************************************************/
/************************************************************************/
#define FRAMES_PER_BUFFER (256)
/************************************************************************/
/******** Prototypes ****************************************************/
/************************************************************************/
static int blockingIOCallback(void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
PaTimestamp outTime, void *userData);
static PaError PABLIO_InitFIFO(RingBuffer * rbuf, long numFrames, long bytesPerFrame);
static PaError PABLIO_TermFIFO(RingBuffer * rbuf);
/************************************************************************/
/******** Prototypes ****************************************************/
/************************************************************************/
static int blockingIOCallback( void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
PaTimestamp outTime, void *userData );
static PaError PABLIO_InitFIFO( RingBuffer *rbuf, long numFrames, long bytesPerFrame );
static PaError PABLIO_TermFIFO( RingBuffer *rbuf );
/************************************************************************/
/******** Functions *****************************************************/
/************************************************************************/
/* Called from PortAudio.
* Read and write data only if there is room in FIFOs.
/************************************************************************/
/******** Functions *****************************************************/
/************************************************************************/
/* Called from PortAudio.
* Read and write data only if there is room in FIFOs.
*/
static int blockingIOCallback(void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
PaTimestamp outTime, void *userData)
{
PABLIO_Stream * data = (PABLIO_Stream *) userData;
long numBytes = data->bytesPerFrame * framesPerBuffer;
(void) outTime;
/* This may get called with NULL inputBuffer during initial setup. */
if (inputBuffer != NULL)
{
RingBuffer_Write(&data->inFIFO, inputBuffer, numBytes);
}
if (outputBuffer != NULL)
{
int i;
int numRead = RingBuffer_Read(&data->outFIFO, outputBuffer, numBytes);
/* Zero out remainder of buffer if we run out of data. */
for (i = numRead; i < numBytes; i++)
{
((char *) outputBuffer)[i] = 0;
}
}
return 0;
*/
static int blockingIOCallback( void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
PaTimestamp outTime, void *userData )
{
PABLIO_Stream *data = (PABLIO_Stream*)userData;
long numBytes = data->bytesPerFrame * framesPerBuffer;
(void) outTime;
/* This may get called with NULL inputBuffer during initial setup. */
if( inputBuffer != NULL )
{
RingBuffer_Write( &data->inFIFO, inputBuffer, numBytes );
}
if( outputBuffer != NULL )
{
int i;
int numRead = RingBuffer_Read( &data->outFIFO, outputBuffer, numBytes );
/* Zero out remainder of buffer if we run out of data. */
for( i=numRead; i<numBytes; i++ )
{
((char *)outputBuffer)[i] = 0;
}
}
return 0;
}
/* Allocate buffer. */
static PaError PABLIO_InitFIFO( RingBuffer *rbuf, long numFrames, long bytesPerFrame )
{
long numBytes = numFrames * bytesPerFrame;
char *buffer = (char *) malloc( numBytes );
if( buffer == NULL ) return paInsufficientMemory;
memset( buffer, 0, numBytes );
return (PaError) RingBuffer_Init( rbuf, numBytes, buffer );
}
/* Free buffer. */
static PaError PABLIO_TermFIFO( RingBuffer *rbuf )
{
if( rbuf->buffer ) free( rbuf->buffer );
rbuf->buffer = NULL;
return paNoError;
}
/************************************************************
* Write data to ring buffer.
* Will not return until all the data has been written.
/* Allocate buffer. */
static PaError PABLIO_InitFIFO(RingBuffer * rbuf, long numFrames, long bytesPerFrame)
{
long numBytes = numFrames * bytesPerFrame;
char *buffer = (char *) malloc(numBytes);
if (buffer == NULL)
return paInsufficientMemory;
memset(buffer, 0, numBytes);
return (PaError) RingBuffer_Init(rbuf, numBytes, buffer);
}
*/
long WriteAudioStream( PABLIO_Stream *aStream, void *data, long numFrames )
{
long bytesWritten;
char *p = (char *) data;
long numBytes = aStream->bytesPerFrame * numFrames;
while( numBytes > 0)
{
bytesWritten = RingBuffer_Write( &aStream->outFIFO, p, numBytes );
numBytes -= bytesWritten;
p += bytesWritten;
if( numBytes > 0) Pa_Sleep(10);
}
return numFrames;
}
/************************************************************
* Read data from ring buffer.
* Will not return until all the data has been read.
/* Free buffer. */
static PaError PABLIO_TermFIFO(RingBuffer * rbuf)
{
if (rbuf->buffer)
free(rbuf->buffer);
rbuf->buffer = NULL;
return paNoError;
}
/************************************************************
* Write data to ring buffer.
* Will not return until all the data has been written.
*/
long ReadAudioStream( PABLIO_Stream *aStream, void *data, long numFrames )
{
long bytesRead;
char *p = (char *) data;
long numBytes = aStream->bytesPerFrame * numFrames;
while( numBytes > 0)
{
bytesRead = RingBuffer_Read( &aStream->inFIFO, p, numBytes );
numBytes -= bytesRead;
p += bytesRead;
if( numBytes > 0) Pa_Sleep(10);
}
return numFrames;
}
/************************************************************
* Return the number of frames that could be written to the stream without
* having to wait.
long bytesWritten;
char *p = (char *) data;
long numBytes = aStream->bytesPerFrame * numFrames;
while (numBytes > 0)
*/
long GetAudioStreamWriteable( PABLIO_Stream *aStream )
{
int bytesEmpty = RingBuffer_GetWriteAvailable( &aStream->outFIFO );
return bytesEmpty / aStream->bytesPerFrame;
}
/************************************************************
* Return the number of frames that are available to be read from the
* stream without having to wait.
bytesWritten = RingBuffer_Write(&aStream->outFIFO, p, numBytes);
numBytes -= bytesWritten;
p += bytesWritten;
if (numBytes > 0)
Pa_Sleep(10);
}
return numFrames;
}
/************************************************************
* Read data from ring buffer.
* Will not return until all the data has been read.
*/
long ReadAudioStream(PABLIO_Stream * aStream, void *data, long numFrames)
{
*/
long GetAudioStreamReadable( PABLIO_Stream *aStream )
{
int bytesFull = RingBuffer_GetReadAvailable( &aStream->inFIFO );
return bytesFull / aStream->bytesPerFrame;
}
/************************************************************/
static unsigned long RoundUpToNextPowerOf2( unsigned long n )
{
long numBits = 0;
if( ((n-1) & n) == 0) return n; /* Already Power of two. */
while( n > 0 )
{
n= n>>1;
numBits++;
}
return (1<<numBits);
}
/************************************************************
* Opens a PortAudio stream with default characteristics.
* Allocates PABLIO_Stream structure.
@@ -201,133 +192,136 @@ static unsigned long RoundUpToNextPowerOf2(unsigned long n)
* flags parameter can be an ORed combination of:
* PABLIO_READ, PABLIO_WRITE, or PABLIO_READ_WRITE,
* and either PABLIO_MONO or PABLIO_STEREO
while (numBytes > 0)
{
bytesRead = RingBuffer_Read(&aStream->inFIFO, p, numBytes);
numBytes -= bytesRead;
p += bytesRead;
if (numBytes > 0)
Pa_Sleep(10);
}
return numFrames;
}
/************************************************************
* Return the number of frames that could be written to the stream without
* having to wait.
*/
long GetAudioStreamWriteable(PABLIO_Stream * aStream)
{
int bytesEmpty = RingBuffer_GetWriteAvailable(&aStream->outFIFO);
return bytesEmpty / aStream->bytesPerFrame;
}
/************************************************************
* Return the number of frames that are available to be read from the
* stream without having to wait.
*/
long GetAudioStreamReadable(PABLIO_Stream * aStream)
{
int bytesFull = RingBuffer_GetReadAvailable(&aStream->inFIFO);
return bytesFull / aStream->bytesPerFrame;
}
/************************************************************/
static unsigned long RoundUpToNextPowerOf2(unsigned long n)
{
long numBits = 0;
if (((n - 1) & n) == 0)
return n; /* Already Power of two. */
while (n > 0)
{
n = n >> 1;
numBits++;
}
return (1 << numBits);
}
/************************************************************
* Opens a PortAudio stream with default characteristics.
* Allocates PABLIO_Stream structure.
*
* flags parameter can be an ORed combination of:
* PABLIO_READ, PABLIO_WRITE, or PABLIO_READ_WRITE,
* and either PABLIO_MONO or PABLIO_STEREO
*/
PaError OpenAudioStream(PABLIO_Stream ** rwblPtr, double sampleRate,
PaSampleFormat format, long flags, int indev,
int outdev)
{
long bytesPerSample;
long doRead = 0;
long doWrite = 0;
PaError err;
PABLIO_Stream * aStream;
long minNumBuffers;
long numFrames;
/* Allocate PABLIO_Stream structure for caller. */
aStream = (PABLIO_Stream *) malloc(sizeof(PABLIO_Stream));
if (aStream == NULL)
return paInsufficientMemory;
memset(aStream, 0, sizeof(PABLIO_Stream));
/* Determine size of a sample. */
bytesPerSample = Pa_GetSampleSize(format);
if (bytesPerSample < 0)
{
err = (PaError) bytesPerSample;
goto error;
}
aStream->samplesPerFrame = ((flags & PABLIO_MONO) != 0) ? 1 : 2;
aStream->bytesPerFrame = bytesPerSample * aStream->samplesPerFrame;
*/
PaError OpenAudioStream( PABLIO_Stream **rwblPtr, double sampleRate,
PaSampleFormat format, long flags, int indev, int outdev )
{
long bytesPerSample;
long doRead = 0;
long doWrite = 0;
PaError err;
PABLIO_Stream *aStream;
long minNumBuffers;
long numFrames;
/* Allocate PABLIO_Stream structure for caller. */
aStream = (PABLIO_Stream *) malloc( sizeof(PABLIO_Stream) );
if( aStream == NULL ) return paInsufficientMemory;
memset( aStream, 0, sizeof(PABLIO_Stream) );
/* Determine size of a sample. */
bytesPerSample = Pa_GetSampleSize( format );
if( bytesPerSample < 0 )
{
err = (PaError) bytesPerSample;
goto error;
}
aStream->samplesPerFrame = ((flags&PABLIO_MONO) != 0) ? 1 : 2;
aStream->bytesPerFrame = bytesPerSample * aStream->samplesPerFrame;
/* Initialize PortAudio */
err = Pa_Initialize();
if( err != paNoError ) goto error;
/* Warning: numFrames must be larger than amount of data processed per interrupt
* inside PA to prevent glitches. Just to be safe, adjust size upwards.
*/
minNumBuffers = 2 * Pa_GetMinNumBuffers( FRAMES_PER_BUFFER, sampleRate );
numFrames = minNumBuffers * FRAMES_PER_BUFFER;
/* The PortAudio callback runs in a high priority thread. But PABLIO
* runs in a normal foreground thread. So we may have much worse
* latency in PABLIO. So adjust latency to a safe level.
*/
{
const int safeLatencyMSec = 200;
int minLatencyMSec = (int) ((1000 * numFrames) / sampleRate);
if( minLatencyMSec < safeLatencyMSec )
{
numFrames = (int) ((safeLatencyMSec * sampleRate) / 1000);
}
}
numFrames = RoundUpToNextPowerOf2( numFrames );
/* Initialize Ring Buffers */
doRead = ((flags & PABLIO_READ) != 0);
doWrite = ((flags & PABLIO_WRITE) != 0);
if(doRead)
{
err = PABLIO_InitFIFO( &aStream->inFIFO, numFrames, aStream->bytesPerFrame );
if( err != paNoError ) goto error;
}
if(doWrite)
{
long numBytes;
err = PABLIO_InitFIFO( &aStream->outFIFO, numFrames, aStream->bytesPerFrame );
if( err != paNoError ) goto error;
/* Make Write FIFO appear full initially. */
numBytes = RingBuffer_GetWriteAvailable( &aStream->outFIFO );
RingBuffer_AdvanceWriteIndex( &aStream->outFIFO, numBytes );
}
/* Open a PortAudio stream that we will use to communicate with the underlying
* audio drivers. */
err = Pa_OpenStream(
&aStream->stream,
(doRead ? (indev > -1) ? indev : Pa_GetDefaultInputDeviceID() : paNoDevice),
(doRead ? aStream->samplesPerFrame : 0 ),
format,
NULL,
(doWrite ? (outdev > -1) ? outdev : Pa_GetDefaultOutputDeviceID() : paNoDevice),
(doWrite ? aStream->samplesPerFrame : 0 ),
format,
NULL,
sampleRate,
FRAMES_PER_BUFFER,
minNumBuffers,
paClipOff, /* we won't output out of range samples so don't bother clipping them */
blockingIOCallback,
aStream );
if( err != paNoError ) goto error;
err = Pa_StartStream( aStream->stream );
if( err != paNoError ) goto error;
*rwblPtr = aStream;
return paNoError;
error:
CloseAudioStream( aStream );
*rwblPtr = NULL;
return err;
}
/************************************************************/
PaError CloseAudioStream( PABLIO_Stream *aStream )
{
PaError err = paNoError;
int bytesEmpty;
int byteSize = aStream->outFIFO.bufferSize;
if( aStream->stream != NULL ) /* Make sure stream was opened. PLB021214 */
{
/* If we are writing data, make sure we play everything written. */
if( byteSize > 0 )
{
int timeOutMSec = 2000;
bytesEmpty = RingBuffer_GetWriteAvailable( &aStream->outFIFO );
while( (bytesEmpty < byteSize) && (timeOutMSec > 0) )
{
Pa_Sleep( 20 );
timeOutMSec -= 20;
bytesEmpty = RingBuffer_GetWriteAvailable( &aStream->outFIFO );
}
}
err = Pa_StopStream( aStream->stream );
if( err != paNoError ) goto error;
err = Pa_CloseStream( aStream->stream );
}
error:
Pa_Terminate();
PABLIO_TermFIFO( &aStream->inFIFO );
PABLIO_TermFIFO( &aStream->outFIFO );
free( aStream );
return err;
}
+140 -160
View File
@@ -32,188 +32,168 @@
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "ringbuffer.h"
#include <string.h>
/***************************************************************************
* Initialize FIFO.
* numBytes must be power of 2, returns -1 if not.
*/
long RingBuffer_Init(RingBuffer * rbuf, long numBytes, void *dataPtr)
{
if (((numBytes - 1) & numBytes) != 0)
return -1; /* Not Power of two. */
rbuf->bufferSize = numBytes;
rbuf->buffer = (char *) dataPtr;
RingBuffer_Flush(rbuf);
*/
long RingBuffer_Init( RingBuffer *rbuf, long numBytes, void *dataPtr )
{
if( ((numBytes-1) & numBytes) != 0) return -1; /* Not Power of two. */
rbuf->bufferSize = numBytes;
rbuf->buffer = (char *)dataPtr;
RingBuffer_Flush( rbuf );
rbuf->bigMask = (numBytes*2)-1;
rbuf->smallMask = (numBytes)-1;
return 0;
}
/***************************************************************************
** Return number of bytes available for reading. */
long RingBuffer_GetReadAvailable( RingBuffer *rbuf )
{
return ( (rbuf->writeIndex - rbuf->readIndex) & rbuf->bigMask );
}
/***************************************************************************
** Return number of bytes available for writing. */
long RingBuffer_GetWriteAvailable( RingBuffer *rbuf )
{
return ( rbuf->bufferSize - RingBuffer_GetReadAvailable(rbuf));
}
/***************************************************************************
rbuf->smallMask = (numBytes) - 1;
return 0;
}
** Clear buffer. Should only be called when buffer is NOT being read. */
void RingBuffer_Flush( RingBuffer *rbuf )
{
rbuf->writeIndex = rbuf->readIndex = 0;
}
/***************************************************************************
** Return number of bytes available for reading. */
long RingBuffer_GetReadAvailable(RingBuffer * rbuf)
{
return ((rbuf->writeIndex - rbuf->readIndex) & rbuf->bigMask);
}
/***************************************************************************
** Return number of bytes available for writing. */
long RingBuffer_GetWriteAvailable(RingBuffer * rbuf)
{
/***************************************************************************
** Get address of region(s) to which we can write data.
** If the region is contiguous, size2 will be zero.
** If non-contiguous, size2 will be the size of second region.
** Returns room available to be written or numBytes, whichever is smaller.
/***************************************************************************
** Clear buffer. Should only be called when buffer is NOT being read. */
void RingBuffer_Flush(RingBuffer * rbuf)
{
rbuf->writeIndex = rbuf->readIndex = 0;
}
/***************************************************************************
** Get address of region(s) to which we can write data.
** If the region is contiguous, size2 will be zero.
** If non-contiguous, size2 will be the size of second region.
** Returns room available to be written or numBytes, whichever is smaller.
*/
long RingBuffer_GetWriteRegions(RingBuffer * rbuf, long numBytes,
void **dataPtr1, long *sizePtr1,
void **dataPtr2,
long *sizePtr2)
{
long index;
long available = RingBuffer_GetWriteAvailable(rbuf);
if (numBytes > available)
numBytes = available;
/* Check to see if write is not contiguous. */
index = rbuf->writeIndex & rbuf->smallMask;
*/
long RingBuffer_GetWriteRegions( RingBuffer *rbuf, long numBytes,
void **dataPtr1, long *sizePtr1,
void **dataPtr2, long *sizePtr2 )
{
long index;
long available = RingBuffer_GetWriteAvailable( rbuf );
if( numBytes > available ) numBytes = available;
/* Check to see if write is not contiguous. */
index = rbuf->writeIndex & rbuf->smallMask;
if( (index + numBytes) > rbuf->bufferSize )
{
/* Write data in two blocks that wrap the buffer. */
long firstHalf = rbuf->bufferSize - index;
*dataPtr1 = &rbuf->buffer[index];
*sizePtr1 = firstHalf;
*dataPtr2 = &rbuf->buffer[0];
*sizePtr2 = numBytes - firstHalf;
}
else
{
*dataPtr1 = &rbuf->buffer[index];
*sizePtr1 = numBytes;
*dataPtr2 = NULL;
*sizePtr2 = 0;
}
return numBytes;
}
/***************************************************************************
{
/* Write data in two blocks that wrap the buffer. */
long firstHalf = rbuf->bufferSize - index;
*dataPtr1 = &rbuf->buffer[index];
*/
long RingBuffer_AdvanceWriteIndex( RingBuffer *rbuf, long numBytes )
{
return rbuf->writeIndex = (rbuf->writeIndex + numBytes) & rbuf->bigMask;
}
/***************************************************************************
** Get address of region(s) from which we can read data.
** If the region is contiguous, size2 will be zero.
** If non-contiguous, size2 will be the size of second region.
** Returns room available to be written or numBytes, whichever is smaller.
*sizePtr2 = numBytes - firstHalf;
}
else
{
*dataPtr1 = &rbuf->buffer[index];
*sizePtr1 = numBytes;
*dataPtr2 = NULL;
*sizePtr2 = 0;
}
return numBytes;
}
/***************************************************************************
*/
long RingBuffer_AdvanceWriteIndex(RingBuffer * rbuf, long numBytes)
{
return rbuf->writeIndex = (rbuf->writeIndex + numBytes) & rbuf->bigMask;
*/
long RingBuffer_GetReadRegions( RingBuffer *rbuf, long numBytes,
void **dataPtr1, long *sizePtr1,
void **dataPtr2, long *sizePtr2 )
{
long index;
long available = RingBuffer_GetReadAvailable( rbuf );
if( numBytes > available ) numBytes = available;
/* Check to see if read is not contiguous. */
index = rbuf->readIndex & rbuf->smallMask;
if( (index + numBytes) > rbuf->bufferSize )
{
/* Write data in two blocks that wrap the buffer. */
long firstHalf = rbuf->bufferSize - index;
*dataPtr1 = &rbuf->buffer[index];
*sizePtr1 = firstHalf;
*dataPtr2 = &rbuf->buffer[0];
*sizePtr2 = numBytes - firstHalf;
}
else
{
*dataPtr1 = &rbuf->buffer[index];
*sizePtr1 = numBytes;
*dataPtr2 = NULL;
*sizePtr2 = 0;
}
return numBytes;
}
/***************************************************************************
*/
long RingBuffer_AdvanceReadIndex( RingBuffer *rbuf, long numBytes )
{
return rbuf->readIndex = (rbuf->readIndex + numBytes) & rbuf->bigMask;
}
/***************************************************************************
/***************************************************************************
** Get address of region(s) from which we can read data.
** If the region is contiguous, size2 will be zero.
** If non-contiguous, size2 will be the size of second region.
** Returns room available to be written or numBytes, whichever is smaller.
*/
long RingBuffer_GetReadRegions(RingBuffer * rbuf, long numBytes,
void **dataPtr1, long *sizePtr1,
void **dataPtr2,
long *sizePtr2)
{
long index;
long available = RingBuffer_GetReadAvailable(rbuf);
if (numBytes > available)
numBytes = available;
/* Check to see if read is not contiguous. */
index = rbuf->readIndex & rbuf->smallMask;
if ((index + numBytes) > rbuf->bufferSize)
{
/* Write data in two blocks that wrap the buffer. */
long firstHalf = rbuf->bufferSize - index;
*dataPtr1 = &rbuf->buffer[index];
*sizePtr1 = firstHalf;
*dataPtr2 = &rbuf->buffer[0];
*sizePtr2 = numBytes - firstHalf;
}
else
{
*dataPtr1 = &rbuf->buffer[index];
*sizePtr1 = numBytes;
*dataPtr2 = NULL;
*sizePtr2 = 0;
** Return bytes written. */
long RingBuffer_Write( RingBuffer *rbuf, void *data, long numBytes )
{
long size1, size2, numWritten;
void *data1, *data2;
numWritten = RingBuffer_GetWriteRegions( rbuf, numBytes, &data1, &size1, &data2, &size2 );
if( size2 > 0 )
{
memcpy( data1, data, size1 );
data = ((char *)data) + size1;
memcpy( data2, data, size2 );
}
else
{
memcpy( data1, data, size1 );
}
RingBuffer_AdvanceWriteIndex( rbuf, numWritten );
return numWritten;
}
/***************************************************************************
** Return bytes read. */
long RingBuffer_Read( RingBuffer *rbuf, void *data, long numBytes )
{
long size1, size2, numRead;
void *data1, *data2;
numRead = RingBuffer_GetReadRegions( rbuf, numBytes, &data1, &size1, &data2, &size2 );
if( size2 > 0 )
{
memcpy( data, data1, size1 );
data = ((char *)data) + size1;
memcpy( data, data2, size2 );
}
else
{
memcpy( data, data1, size1 );
}
RingBuffer_AdvanceReadIndex( rbuf, numRead );
return numRead;
}
+99 -100
View File
@@ -86,8 +86,8 @@ static struct {
struct private_object {
unsigned int flags; /* FLAGS */
struct switch_frame frame; /* Frame for Writing */
unsigned int flags; /* FLAGS */
struct switch_frame frame; /* Frame for Writing */
unsigned char databuf[SWITCH_RECCOMMENDED_BUFFER_SIZE];
struct sangoma_pri *spri;
pri_event ring_event;
@@ -110,7 +110,7 @@ static void set_global_dialplan(char *dialplan)
free(globals.dialplan);
globals.dialplan = NULL;
}
globals.dialplan = strdup(dialplan);
}
@@ -153,18 +153,15 @@ static switch_status wanpipe_on_init(switch_core_session *session);
static switch_status wanpipe_on_hangup(switch_core_session *session);
static switch_status wanpipe_on_loopback(switch_core_session *session);
static switch_status wanpipe_on_transmit(switch_core_session *session);
static switch_status wanpipe_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile,
switch_core_session **new_session);
static switch_status wanpipe_read_frame(switch_core_session *session, switch_frame **frame, int timeout,
switch_io_flag flags, int stream_id);
static switch_status wanpipe_write_frame(switch_core_session *session, switch_frame *frame, int timeout,
switch_io_flag flags, int stream_id);
static int on_info(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event * event);
static int on_hangup(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event * event);
static int on_ring(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event * event);
static switch_status wanpipe_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile, switch_core_session **new_session);
static switch_status wanpipe_read_frame(switch_core_session *session, switch_frame **frame, int timeout, switch_io_flag flags, int stream_id);
static switch_status wanpipe_write_frame(switch_core_session *session, switch_frame *frame, int timeout, switch_io_flag flags, int stream_id);
static int on_info(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event *event);
static int on_hangup(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event *event);
static int on_ring(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event *event);
static int check_flags(struct sangoma_pri *spri);
static int on_restart(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event * event);
static int on_anything(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event * event);
static int on_restart(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event *event);
static int on_anything(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event *event);
static void *pri_thread_run(switch_thread *thread, void *obj);
static int config_wanpipe(int reload);
@@ -187,10 +184,10 @@ static switch_status wanpipe_on_init(switch_core_session *session)
assert(tech_pvt != NULL);
tech_pvt->frame.data = tech_pvt->databuf;
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "WANPIPE INIT\n");
/* Move Channel's State Machine to RING */
switch_channel_set_state(channel, CS_RING);
return SWITCH_STATUS_SUCCESS;
@@ -224,14 +221,10 @@ static switch_status wanpipe_on_hangup(switch_core_session *session)
assert(tech_pvt != NULL);
switch_socket_close(&tech_pvt->socket);
pri_hangup(tech_pvt->spri->pri,
tech_pvt->hangup_event.hangup.call ? tech_pvt->hangup_event.hangup.call : tech_pvt->ring_event.ring.call,
tech_pvt->cause);
pri_destroycall(tech_pvt->spri->pri,
tech_pvt->hangup_event.hangup.call ? tech_pvt->hangup_event.hangup.call : tech_pvt->ring_event.ring.
call);
pri_hangup(tech_pvt->spri->pri, tech_pvt->hangup_event.hangup.call ? tech_pvt->hangup_event.hangup.call : tech_pvt->ring_event.ring.call, tech_pvt->cause);
pri_destroycall(tech_pvt->spri->pri, tech_pvt->hangup_event.hangup.call ? tech_pvt->hangup_event.hangup.call : tech_pvt->ring_event.ring.call);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "WANPIPE HANGUP\n");
return SWITCH_STATUS_SUCCESS;
}
@@ -248,8 +241,7 @@ static switch_status wanpipe_on_transmit(switch_core_session *session)
return SWITCH_STATUS_SUCCESS;
}
static switch_status wanpipe_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile,
switch_core_session **new_session)
static switch_status wanpipe_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile, switch_core_session **new_session)
{
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "NOT IMPLEMENTED\n");
@@ -260,7 +252,7 @@ static switch_status wanpipe_answer_channel(switch_core_session *session)
{
struct private_object *tech_pvt;
switch_channel *channel = NULL;
channel = switch_core_session_get_channel(session);
assert(channel != NULL);
@@ -268,14 +260,13 @@ static switch_status wanpipe_answer_channel(switch_core_session *session)
assert(tech_pvt != NULL);
pri_answer(tech_pvt->spri->pri, tech_pvt->ring_event.ring.call, 0, 1);
return SWITCH_STATUS_SUCCESS;
}
static switch_status wanpipe_read_frame(switch_core_session *session, switch_frame **frame, int timeout,
switch_io_flag flags, int stream_id)
static switch_status wanpipe_read_frame(switch_core_session *session, switch_frame **frame, int timeout, switch_io_flag flags, int stream_id)
{
struct private_object *tech_pvt;
switch_channel *channel = NULL;
@@ -287,22 +278,25 @@ static switch_status wanpipe_read_frame(switch_core_session *session, switch_fra
tech_pvt = switch_core_session_get_private(session);
assert(tech_pvt != NULL);
bp = tech_pvt->databuf;
*frame = NULL;
memset(tech_pvt->databuf, 0, sizeof(tech_pvt->databuf));
while (bytes < globals.bytes_per_frame) {
if ((res = switch_socket_waitfor(tech_pvt->socket, timeout, POLLIN | POLLERR)) < 0) {
if ((res = switch_socket_waitfor(tech_pvt->socket, timeout, POLLIN|POLLERR)) < 0) {
return SWITCH_STATUS_GENERR;
} else if (res == 0) {
tech_pvt->frame.datalen = 0;
return SWITCH_STATUS_SUCCESS;
}
if ((res = sangoma_readmsg_socket(tech_pvt->socket,
if ((res = sangoma_readmsg_socket(
tech_pvt->socket,
&tech_pvt->hdrframe,
sizeof(tech_pvt->hdrframe), bp, sizeof(tech_pvt->databuf) - bytes, 0)) < 0) {
sizeof(tech_pvt->hdrframe),
bp,
sizeof(tech_pvt->databuf) - bytes, 0)) < 0) {
if (errno == EBUSY) {
continue;
} else {
@@ -312,14 +306,13 @@ static switch_status wanpipe_read_frame(switch_core_session *session, switch_fra
bytes += res;
bp += bytes;
}
tech_pvt->frame.datalen = bytes;
tech_pvt->frame.datalen = bytes;
*frame = &tech_pvt->frame;
return SWITCH_STATUS_SUCCESS;
}
static switch_status wanpipe_write_frame(switch_core_session *session, switch_frame *frame, int timeout,
switch_io_flag flags, int stream_id)
static switch_status wanpipe_write_frame(switch_core_session *session, switch_frame *frame, int timeout, switch_io_flag flags, int stream_id)
{
struct private_object *tech_pvt;
switch_channel *channel = NULL;
@@ -336,12 +329,14 @@ static switch_status wanpipe_write_frame(switch_core_session *session, switch_fr
while (bytes > 0) {
switch_socket_waitfor(tech_pvt->socket, -1, POLLOUT | POLLERR | POLLHUP);
res = sangoma_sendmsg_socket(tech_pvt->socket,
&tech_pvt->hdrframe, sizeof(tech_pvt->hdrframe), bp, PACKET_LEN, 0);
res = sangoma_sendmsg_socket(
tech_pvt->socket,
&tech_pvt->hdrframe,
sizeof(tech_pvt->hdrframe),
bp,
PACKET_LEN, 0);
if (res < 0) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE,
"Bad Write frame len %d write %d bytes returned %d errno %d!\n", frame->datalen,
PACKET_LEN, res, errno);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Bad Write frame len %d write %d bytes returned %d errno %d!\n", frame->datalen, PACKET_LEN, res, errno);
if (errno == EBUSY) {
continue;
}
@@ -357,48 +352,47 @@ static switch_status wanpipe_write_frame(switch_core_session *session, switch_fr
return status;
}
static const switch_io_routines wanpipe_io_routines */ {
/*.outgoing_channel */ wanpipe_outgoing_channel,
/*.answer_channel */ wanpipe_answer_channel,
/*.read_frame */ wanpipe_read_frame,
/*.write_frame */ wanpipe_write_frame
static const switch_io_routines wanpipe_io_routines*/ {
/*.outgoing_channel*/ wanpipe_outgoing_channel,
/*.answer_channel*/ wanpipe_answer_channel,
/*.read_frame*/ wanpipe_read_frame,
/*.write_frame*/ wanpipe_write_frame
};
static const switch_event_handler_table wanpipe_event_handlers = {
/*.on_init */ wanpipe_on_init,
/*.on_ring */ wanpipe_on_ring,
/*.on_execute */ NULL,
/*.on_hangup */ wanpipe_on_hangup,
/*.on_loopback */ wanpipe_on_loopback,
/*.on_transmit */ wanpipe_on_transmit
static const switch_event_handler_table wanpipe_event_handlers = {
/*.on_init*/ wanpipe_on_init,
/*.on_ring*/ wanpipe_on_ring,
/*.on_execute*/ NULL,
/*.on_hangup*/ wanpipe_on_hangup,
/*.on_loopback*/ wanpipe_on_loopback,
/*.on_transmit*/ wanpipe_on_transmit
};
static const switch_endpoint_interface wanpipe_endpoint_interface = {
/*.interface_name */ "wanpipe",
/*.io_routines */ &wanpipe_io_routines,
/*.event_handlers */ &wanpipe_event_handlers,
/*.private */ NULL,
/*.next */ NULL
/*.interface_name*/ "wanpipe",
/*.io_routines*/ &wanpipe_io_routines,
/*.event_handlers*/ &wanpipe_event_handlers,
/*.private*/ NULL,
/*.next*/ NULL
};
static const switch_loadable_module_interface wanpipe_module_interface = {
/*.module_name */ modname,
/*.endpoint_interface */ &wanpipe_endpoint_interface,
/*.timer_interface */ NULL,
/*.dialplan_interface */ NULL,
/*.codec_interface */ NULL,
/*.application_interface */ NULL
/*.module_name*/ modname,
/*.endpoint_interface*/ &wanpipe_endpoint_interface,
/*.timer_interface*/ NULL,
/*.dialplan_interface*/ NULL,
/*.codec_interface*/ NULL,
/*.application_interface*/ NULL
};
Public switch_status switch_module_load(const switch_loadable_module_interface **interface, chanr * filename)
{
Public switch_status switch_module_load(const switch_loadable_module_interface **interface, chanr *filename) {
if (switch_core_new_memory_pool(&module_pool) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "OH OH no pool\n");
return SWITCH_STATUS_TERM;
}
/* connect my internal structure to the blank pointer passed to me */
*interface = &wanpipe_module_interface;
@@ -412,7 +406,7 @@ Public switch_status switch_module_load(const switch_loadable_module_interface *
/* Event Handlers */
static int on_info(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event * event)
static int on_info(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event *event)
{
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "number is: %s\n", event->ring.callednum);
if (strlen(event->ring.callednum) > 3) {
@@ -422,7 +416,7 @@ static int on_info(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri
return 0;
}
static int on_hangup(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event * event)
static int on_hangup(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event *event)
{
struct channel_map *chanmap;
switch_core_session *session;
@@ -437,18 +431,18 @@ static int on_hangup(struct sangoma_pri *spri, sangoma_pri_event_t event_type, p
tech_pvt = switch_core_session_get_private(session);
assert(tech_pvt != NULL);
tech_pvt->cause = event->hangup.cause;
memcpy(&tech_pvt->hangup_event, event, sizeof(*event));
switch_channel_set_state(channel, CS_HANGUP);
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "-- Hanging up channel %d\n", event->hangup.channel);
switch_console_printf(SWITCH_CHANNEL_CONSOLE,"-- Hanging up channel %d\n", event->hangup.channel);
return 0;
}
static int on_ring(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event * event)
static int on_ring(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event *event)
{
char name[128];
switch_core_session *session;
@@ -459,13 +453,11 @@ static int on_ring(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri
chanmap = spri->private;
if (chanmap->map[event->ring.channel]) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "--Duplicate Ring on channel %d (ignored)\n",
event->ring.channel);
switch_console_printf(SWITCH_CHANNEL_CONSOLE,"--Duplicate Ring on channel %d (ignored)\n", event->ring.channel);
return 0;
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "-- Ring on channel %d (from %s to %s)\n", event->ring.channel,
event->ring.callingnum, event->ring.callednum);
switch_console_printf(SWITCH_CHANNEL_CONSOLE,"-- Ring on channel %d (from %s to %s)\n", event->ring.channel, event->ring.callingnum, event->ring.callednum);
sprintf(name, "w%dg%d", globals.span, event->ring.channel);
if ((session = switch_core_session_request(&wanpipe_endpoint_interface, NULL))) {
@@ -484,18 +476,18 @@ static int on_ring(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri
switch_core_session_destroy(&session);
return 0;
}
if (event->ring.ani2 >= 0) {
snprintf(ani2str, 5, "%.2d", event->ring.ani2);
}
if ((tech_pvt->caller_profile = switch_caller_profile_new(session,
globals.dialplan,
"wanpipe fixme",
event->ring.callingnum,
event->ring.callingani,
switch_strlen_zero(ani2str) ? NULL : ani2str,
event->ring.callednum))) {
globals.dialplan,
"wanpipe fixme",
event->ring.callingnum,
event->ring.callingani,
switch_strlen_zero(ani2str) ? NULL : ani2str,
event->ring.callednum))) {
switch_channel_set_caller_profile(channel, tech_pvt->caller_profile);
}
@@ -510,6 +502,7 @@ static int on_ring(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri
if ((fd = sangoma_create_socket_intr(spri->span, event->ring.channel)) < 0) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't open fd!\n");
}
//sangoma_tdm_set_hw_period(fd, &tdm_api, 480);
tech_pvt->socket = fd;
@@ -527,28 +520,27 @@ static int on_ring(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri
static int check_flags(struct sangoma_pri *spri)
{
return 0;
}
static int on_restart(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event * event)
static int on_restart(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event *event)
{
int fd;
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "-- Restarting channel %d\n", event->restart.channel);
switch_console_printf(SWITCH_CHANNEL_CONSOLE,"-- Restarting channel %d\n", event->restart.channel);
if ((fd = sangoma_create_socket_intr(spri->span, event->restart.channel)) < 0) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't open fd!\n");
switch_console_printf(SWITCH_CHANNEL_CONSOLE,"Can't open fd!\n");
} else {
close(fd);
}
return 0;
}
static int on_anything(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event * event)
static int on_anything(struct sangoma_pri *spri, sangoma_pri_event_t event_type, pri_event *event)
{
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Caught Event %d (%s)\n", event_type,
sangoma_pri_event_str(event_type));
switch_console_printf(SWITCH_CHANNEL_CONSOLE,"Caught Event %d (%s)\n", event_type, sangoma_pri_event_str(event_type));
return 0;
}
@@ -567,13 +559,13 @@ static void *pri_thread_run(switch_thread *thread, void *obj)
spri->on_loop = check_flags;
spri->private = &chanmap;
sangoma_run_pri(spri);
free(spri);
return NULL;
}
static int config_wanpipe(int reload)
static int config_wanpipe(int reload)
{
switch_config cfg;
char *var, *val;
@@ -604,7 +596,7 @@ static int config_wanpipe(int reload)
globals.bytes_per_frame = atoi(val);
} else if (!strcmp(var, "dialplan")) {
set_global_dialplan(val);
}
}
}
}
@@ -614,12 +606,18 @@ static int config_wanpipe(int reload)
set_global_dialplan("default");
}
if ((spri = switch_core_alloc(module_pool, sizeof(*spri)))) {
if ((spri=switch_core_alloc(module_pool, sizeof(*spri)))) {
memset(spri, 0, sizeof(*spri));
sangoma_init_pri(spri, globals.span, globals.dchan, 23, globals.pswitch, globals.node, globals.debug);
sangoma_init_pri(spri,
globals.span,
globals.dchan,
23,
globals.pswitch,
globals.node,
globals.debug);
pri_thread_run(NULL, spri);
} else {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "error!\n");
}
@@ -634,3 +632,4 @@ Public switch_status switch_module_runtime(void)
config_wanpipe(0);
return SWITCH_STATUS_TERM;
}
+241 -229
View File
@@ -39,7 +39,7 @@
#define WOOMERA_LINE_SEPERATOR "\r\n"
#define WOOMERA_RECORD_SEPERATOR "\r\n\r\n"
#define WOOMERA_DEBUG_PREFIX "**[DEBUG]** "
#define WOOMERA_DEBUG_LINE "--------------------------------------------------------------------------------"
#define WOOMERA_DEBUG_LINE "--------------------------------------------------------------------------------"
#define WOOMERA_HARD_TIMEOUT -10000
#define WOOMERA_QLEN 10
#define WOOMERA_RECONNECT_TIME 5000000
@@ -104,7 +104,7 @@ struct woomera_message {
static struct {
int next_woomera_port;
int next_woomera_port;
int debug;
int panic;
int rtpmode;
@@ -122,7 +122,7 @@ struct woomera_profile {
int woomera_port;
char audio_ip[WOOMERA_STRLEN];
char dialplan[WOOMERA_STRLEN];
// pthread_t thread;
// pthread_t thread;
unsigned int flags;
int thread_running;
struct woomera_event_queue event_queue;
@@ -171,27 +171,23 @@ static switch_status woomerachan_on_hangup(switch_core_session *session);
static switch_status woomerachan_on_ring(switch_core_session *session);
static switch_status woomerachan_on_loopback(switch_core_session *session);
static switch_status woomerachan_on_transmit(switch_core_session *session);
static switch_status woomerachan_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile,
switch_core_session **new_session);
static switch_status woomerachan_read_frame(switch_core_session *session, switch_frame **frame, int timeout,
switch_io_flag flags, int stream_id);
static switch_status woomerachan_write_frame(switch_core_session *session, switch_frame *frame, int timeout,
switch_io_flag flags, int stream_id);
static switch_status woomerachan_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile, switch_core_session **new_session);
static switch_status woomerachan_read_frame(switch_core_session *session, switch_frame **frame, int timeout, switch_io_flag flags, int stream_id);
static switch_status woomerachan_write_frame(switch_core_session *session, switch_frame *frame, int timeout, switch_io_flag flags, int stream_id);
static switch_status woomerachan_kill_channel(switch_core_session *session, int sig);
static void tech_destroy(private_object * tech_pvt);
static void woomera_printf(woomera_profile * profile, switch_socket_t *socket, char *fmt, ...);
static char *woomera_message_header(woomera_message * wmsg, char *key);
static int woomera_enqueue_event(woomera_event_queue * event_queue, woomera_message * wmsg);
static int woomera_dequeue_event(woomera_event_queue * event_queue, woomera_message * wmsg);
static int woomera_message_parse(switch_socket_t *fd, woomera_message * wmsg, int timeout, woomera_profile * profile,
woomera_event_queue * event_queue);
static int connect_woomera(switch_socket_t **new_sock, woomera_profile * profile, int flags);
static int woomera_profile_thread_running(woomera_profile * profile, int set, int new);
static int woomera_locate_socket(woomera_profile * profile, switch_socket_t **woomera_socket);
static int tech_create_read_socket(private_object * tech_pvt);
static void tech_destroy(private_object *tech_pvt);
static void woomera_printf(woomera_profile *profile, switch_socket_t *socket, char *fmt, ...);
static char *woomera_message_header(woomera_message *wmsg, char *key);
static int woomera_enqueue_event(woomera_event_queue *event_queue, woomera_message *wmsg);
static int woomera_dequeue_event(woomera_event_queue *event_queue, woomera_message *wmsg);
static int woomera_message_parse(switch_socket_t *fd, woomera_message *wmsg, int timeout, woomera_profile *profile, woomera_event_queue *event_queue);
static int connect_woomera(switch_socket_t **new_sock, woomera_profile *profile, int flags);
static int woomera_profile_thread_running(woomera_profile *profile, int set, int new);
static int woomera_locate_socket(woomera_profile *profile, switch_socket_t **woomera_socket);
static int tech_create_read_socket(private_object *tech_pvt);
static void *woomera_channel_thread_run(switch_thread *thread, void *obj);
static void *woomera_thread_run(void *obj);
static int tech_activate(private_object * tech_pvt);
static int tech_activate(private_object *tech_pvt);
/*
State methods they get called when the state changes to the specific state
@@ -212,17 +208,13 @@ static switch_status woomerachan_on_init(switch_core_session *session)
tech_pvt->frame.data = tech_pvt->databuf;
if (switch_core_codec_init
(&tech_pvt->read_codec, "L16", rate, 30, 1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL,
switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
if (switch_core_codec_init(&tech_pvt->read_codec, "L16", rate, 30, 1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL, switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "%s Cannot set read codec\n", switch_channel_get_name(channel));
switch_channel_hangup(channel);
return SWITCH_STATUS_FALSE;
}
if (switch_core_codec_init
(&tech_pvt->write_codec, "L16", rate, 30, 1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL,
switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
if (switch_core_codec_init(&tech_pvt->write_codec, "L16", rate, 30, 1, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL, switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "%s Cannot set read codec\n", switch_channel_get_name(channel));
switch_channel_hangup(channel);
return SWITCH_STATUS_FALSE;
@@ -236,7 +228,7 @@ static switch_status woomerachan_on_init(switch_core_session *session)
switch_set_flag(tech_pvt, TFLAG_ACTIVATE);
switch_core_session_launch_thread(session, woomera_channel_thread_run, session);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "%s WOOMERACHAN INIT\n", switch_channel_get_name(channel));
return SWITCH_STATUS_SUCCESS;
@@ -269,7 +261,7 @@ static switch_status woomerachan_on_execute(switch_core_session *session)
tech_pvt = switch_core_session_get_private(session);
assert(tech_pvt != NULL);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "%s WOOMERACHAN EXECUTE\n", switch_channel_get_name(channel));
@@ -280,7 +272,7 @@ static switch_status woomerachan_on_hangup(switch_core_session *session)
{
switch_channel *channel = NULL;
struct private_object *tech_pvt = NULL;
channel = switch_core_session_get_channel(session);
assert(channel != NULL);
@@ -303,7 +295,7 @@ static void woomera_socket_close(switch_socket_t **socket)
}
static void udp_socket_close(struct private_object *tech_pvt)
static void udp_socket_close(struct private_object *tech_pvt)
{
if (tech_pvt->udp_socket) {
apr_socket_shutdown(tech_pvt->udp_socket, APR_SHUTDOWN_READWRITE);
@@ -316,7 +308,7 @@ static switch_status woomerachan_kill_channel(switch_core_session *session, int
{
switch_channel *channel = NULL;
struct private_object *tech_pvt = NULL;
channel = switch_core_session_get_channel(session);
assert(channel != NULL);
@@ -330,10 +322,9 @@ static switch_status woomerachan_kill_channel(switch_core_session *session, int
udp_socket_close(tech_pvt);
switch_channel_hangup(channel);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "%s WOOMERACHAN KILL %d\n", switch_channel_get_name(channel),
tech_pvt->udp_socket);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "%s WOOMERACHAN KILL %d\n", switch_channel_get_name(channel), tech_pvt->udp_socket);
return SWITCH_STATUS_SUCCESS;
}
@@ -353,16 +344,14 @@ static switch_status woomerachan_on_transmit(switch_core_session *session)
/* 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!
*/
static switch_status woomerachan_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile,
switch_core_session **new_session)
static switch_status woomerachan_outgoing_channel(switch_core_session *session, switch_caller_profile *outbound_profile, switch_core_session **new_session)
{
if ((*new_session = switch_core_session_request(&woomerachan_endpoint_interface, NULL))) {
struct private_object *tech_pvt;
switch_channel *channel;
switch_core_session_add_stream(*new_session, NULL);
if ((tech_pvt =
(struct private_object *) switch_core_session_alloc(*new_session, sizeof(struct private_object)))) {
if ((tech_pvt = (struct private_object *) switch_core_session_alloc(*new_session, sizeof(struct private_object)))) {
memset(tech_pvt, 0, sizeof(*tech_pvt));
tech_pvt->profile = &default_profile;
channel = switch_core_session_get_channel(*new_session);
@@ -420,8 +409,7 @@ static switch_status woomerachan_waitfor_write(switch_core_session *session, int
return switch_socket_waitfor(&tech_pvt->write_poll, ms);
}
static switch_status woomerachan_read_frame(switch_core_session *session, switch_frame **frame, int timeout,
switch_io_flag flags, int stream_id)
static switch_status woomerachan_read_frame(switch_core_session *session, switch_frame **frame, int timeout, switch_io_flag flags, int stream_id)
{
switch_channel *channel = NULL;
struct private_object *tech_pvt = NULL;
@@ -438,24 +426,21 @@ static switch_status woomerachan_read_frame(switch_core_session *session, switch
return SWITCH_STATUS_GENERR;
}
/*
if ((status = woomerachan_waitfor_read(session, -1)) != SWITCH_STATUS_SUCCESS) {
return status;
}1<
*/
if ((status = woomerachan_waitfor_read(session, -1)) != SWITCH_STATUS_SUCCESS) {
return status;
}1<
*/
pframe = &tech_pvt->frame;
*frame = pframe;
*frame = pframe;
pframe->datalen = sizeof(tech_pvt->databuf);
if ((status =
switch_socket_recvfrom(tech_pvt->udpread, tech_pvt->udp_socket, 0, tech_pvt->databuf,
&pframe->datalen)) == SWITCH_STATUS_SUCCESS) {
pframe->samples = (int) pframe->datalen / 2;
if ((status = switch_socket_recvfrom (tech_pvt->udpread, tech_pvt->udp_socket, 0, tech_pvt->databuf, &pframe->datalen)) == SWITCH_STATUS_SUCCESS) {
pframe->samples = (int)pframe->datalen / 2;
}
return status;
}
static switch_status woomerachan_write_frame(switch_core_session *session, switch_frame *frame, int timeout,
switch_io_flag flags, int stream_id)
static switch_status woomerachan_write_frame(switch_core_session *session, switch_frame *frame, int timeout, switch_io_flag flags, int stream_id)
{
switch_channel *channel = NULL;
struct private_object *tech_pvt = NULL;
@@ -473,69 +458,72 @@ static switch_status woomerachan_write_frame(switch_core_session *session, switc
pframe = &tech_pvt->frame;
return switch_socket_sendto(tech_pvt->udp_socket, tech_pvt->udpwrite, 0, frame->data, &frame->datalen);
}
static const switch_event_handler_table woomerachan_event_handlers = {
/*.on_init */ woomerachan_on_init,
/*.on_ring */ woomerachan_on_ring,
/*.on_execute */ woomerachan_on_execute,
/*.on_hangup */ woomerachan_on_hangup,
/*.on_loopback */ woomerachan_on_loopback,
/*.on_transmit */ woomerachan_on_transmit
/*.on_init*/ woomerachan_on_init,
/*.on_ring*/ woomerachan_on_ring,
/*.on_execute*/ woomerachan_on_execute,
/*.on_hangup*/ woomerachan_on_hangup,
/*.on_loopback*/ woomerachan_on_loopback,
/*.on_transmit*/ woomerachan_on_transmit
};
static const switch_io_routines woomerachan_io_routines = {
/*.outgoing_channel */ woomerachan_outgoing_channel,
/*.answer_channel */ NULL,
/*.read_frame */ woomerachan_read_frame,
/*.write_frame */ woomerachan_write_frame,
/*.kill_channel */ woomerachan_kill_channel,
/*.waitfor_read */ woomerachan_waitfor_read,
/*.waitfor_write */ woomerachan_waitfor_write
/*.outgoing_channel*/ woomerachan_outgoing_channel,
/*.answer_channel*/ NULL,
/*.read_frame*/ woomerachan_read_frame,
/*.write_frame*/ woomerachan_write_frame,
/*.kill_channel*/ woomerachan_kill_channel,
/*.waitfor_read*/ woomerachan_waitfor_read,
/*.waitfor_write*/ woomerachan_waitfor_write
};
static const switch_endpoint_interface woomerachan_endpoint_interface = {
/*.interface_name */ "woomera",
/*.io_routines */ &woomerachan_io_routines,
/*.event_handlers */ &woomerachan_event_handlers,
/*.private */ NULL,
/*.next */ NULL
/*.interface_name*/ "woomera",
/*.io_routines*/ &woomerachan_io_routines,
/*.event_handlers*/ &woomerachan_event_handlers,
/*.private*/ NULL,
/*.next*/ NULL
};
static const switch_loadable_module_interface woomerachan_module_interface = {
/*.module_name */ modname,
/*.endpoint_interface */ &woomerachan_endpoint_interface,
/*.timer_interface */ NULL,
/*.dialplan_interface */ NULL,
/*.codec_interface */ NULL,
/*.application_interface */ NULL
/*.module_name*/ modname,
/*.endpoint_interface*/ &woomerachan_endpoint_interface,
/*.timer_interface*/ NULL,
/*.dialplan_interface*/ NULL,
/*.codec_interface*/ NULL,
/*.application_interface*/ NULL
};
static void tech_destroy(private_object * tech_pvt)
static void tech_destroy(private_object *tech_pvt)
{
woomera_message wmsg;
if (globals.debug > 1) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, WOOMERA_DEBUG_PREFIX "+++DESTROY\n");
}
woomera_printf(tech_pvt->profile, tech_pvt->command_channel, "hangup %s%s", tech_pvt->call_info.callid,
WOOMERA_RECORD_SEPERATOR);
if (woomera_message_parse
(tech_pvt->command_channel, &wmsg, WOOMERA_HARD_TIMEOUT, tech_pvt->profile, &tech_pvt->event_queue) < 0) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "{%s} Already Disconnected\n", tech_pvt->profile->name);
woomera_printf(tech_pvt->profile, tech_pvt->command_channel, "hangup %s%s", tech_pvt->call_info.callid, WOOMERA_RECORD_SEPERATOR);
if(woomera_message_parse(tech_pvt->command_channel,
&wmsg,
WOOMERA_HARD_TIMEOUT,
tech_pvt->profile,
&tech_pvt->event_queue
) < 0) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "{%s} Already Disconnected\n", tech_pvt->profile->name);
}
woomera_printf(tech_pvt->profile, tech_pvt->command_channel, "bye%s", WOOMERA_RECORD_SEPERATOR);
woomera_socket_close(&tech_pvt->command_channel);
udp_socket_close(tech_pvt);
}
static void woomera_printf(woomera_profile * profile, switch_socket_t *socket, char *fmt, ...)
static void woomera_printf(woomera_profile *profile, switch_socket_t *socket, char *fmt, ...)
{
char *stuff;
size_t res = 0, len = 0;
@@ -543,7 +531,7 @@ static void woomera_printf(woomera_profile * profile, switch_socket_t *socket, c
va_list ap;
va_start(ap, fmt);
#ifndef vasprintf
stuff = (char *) malloc(10240);
stuff = (char *)malloc(10240);
vsnprintf(stuff, 10240, fmt, ap);
#else
res = vasprintf(&stuff, fmt, ap);
@@ -553,23 +541,22 @@ static void woomera_printf(woomera_profile * profile, switch_socket_t *socket, c
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Out of memory\n");
} else {
if (profile && globals.debug) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Send Message: {%s} [%s/%d]\n%s\n%s", profile->name,
profile->woomera_host, profile->woomera_port, WOOMERA_DEBUG_LINE, stuff);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Send Message: {%s} [%s/%d]\n%s\n%s", profile->name, profile->woomera_host, profile->woomera_port, WOOMERA_DEBUG_LINE, stuff);
}
len = strlen(stuff);
switch_socket_send(socket, stuff, &len);
free(stuff);
}
}
static char *woomera_message_header(woomera_message * wmsg, char *key)
static char *woomera_message_header(woomera_message *wmsg, char *key)
{
int x = 0;
char *value = NULL;
for (x = 0; x < wmsg->last; x++) {
for (x = 0 ; x < wmsg->last ; x++) {
if (!strcasecmp(wmsg->names[x], key)) {
value = wmsg->values[x];
break;
@@ -579,7 +566,7 @@ static char *woomera_message_header(woomera_message * wmsg, char *key)
return value;
}
static int woomera_enqueue_event(woomera_event_queue * event_queue, woomera_message * wmsg)
static int woomera_enqueue_event(woomera_event_queue *event_queue, woomera_message *wmsg)
{
woomera_message *new, *mptr;
@@ -590,7 +577,7 @@ static int woomera_enqueue_event(woomera_event_queue * event_queue, woomera_mess
if (!event_queue->head) {
event_queue->head = new;
} else {
for (mptr = event_queue->head; mptr && mptr->next; mptr = mptr->next);
for (mptr = event_queue->head; mptr && mptr->next ; mptr = mptr->next);
mptr->next = new;
}
return 1;
@@ -601,10 +588,10 @@ static int woomera_enqueue_event(woomera_event_queue * event_queue, woomera_mess
return 0;
}
static int woomera_dequeue_event(woomera_event_queue * event_queue, woomera_message * wmsg)
static int woomera_dequeue_event(woomera_event_queue *event_queue, woomera_message *wmsg)
{
woomera_message *mptr = NULL;
if (event_queue->head) {
mptr = event_queue->head;
event_queue->head = mptr->next;
@@ -617,12 +604,11 @@ static int woomera_dequeue_event(woomera_event_queue * event_queue, woomera_mess
} else {
memset(wmsg, 0, sizeof(woomera_message));
}
return 0;
}
static int woomera_message_parse(switch_socket_t *fd, woomera_message * wmsg, int timeout, woomera_profile * profile,
woomera_event_queue * event_queue)
static int woomera_message_parse(switch_socket_t *fd, woomera_message *wmsg, int timeout, woomera_profile *profile, woomera_event_queue *event_queue)
{
char *cur, *cr, *next = NULL, *eor = NULL;
char buf[2048] = "", *ptr;
@@ -631,7 +617,7 @@ static int woomera_message_parse(switch_socket_t *fd, woomera_message * wmsg, in
memset(wmsg, 0, sizeof(woomera_message));
if (fd < 0) {
if (fd < 0 ) {
return -1;
}
@@ -661,18 +647,17 @@ static int woomera_message_parse(switch_socket_t *fd, woomera_message * wmsg, in
next = buf;
if (globals.debug) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Receive Message: {%s} [%s/%d]\n%s\n%s", profile->name,
profile->woomera_host, profile->woomera_port, WOOMERA_DEBUG_LINE, buf);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Receive Message: {%s} [%s/%d]\n%s\n%s", profile->name, profile->woomera_host, profile->woomera_port, WOOMERA_DEBUG_LINE, buf);
}
while ((cur = next)) {
while((cur = next)) {
if ((cr = strstr(cur, WOOMERA_LINE_SEPERATOR))) {
*cr = '\0';
next = cr + (sizeof(WOOMERA_LINE_SEPERATOR) - 1);
if (!strcmp(next, WOOMERA_RECORD_SEPERATOR)) {
break;
}
}
}
if (!cur || !cur[0]) {
break;
@@ -725,9 +710,9 @@ static int woomera_message_parse(switch_socket_t *fd, woomera_message * wmsg, in
*val = '\0';
val++;
}
strncpy(wmsg->values[wmsg->last - 1], val, WOOMERA_STRLEN);
strncpy(wmsg->values[wmsg->last-1], val, WOOMERA_STRLEN);
}
strncpy(wmsg->names[wmsg->last - 1], name, WOOMERA_STRLEN);
strncpy(wmsg->names[wmsg->last-1], name, WOOMERA_STRLEN);
if (name && val && !strcasecmp(name, "content-type")) {
switch_set_flag(wmsg, WFLAG_CONTENT);
bytes = atoi(val);
@@ -759,11 +744,11 @@ static int woomera_message_parse(switch_socket_t *fd, woomera_message * wmsg, in
* wmsg will be overwritten but it's ok we just queued it.
*/
return woomera_message_parse(fd, wmsg, timeout, profile, event_queue);
} else if (wmsg->mval > 99 && wmsg->mval < 200) {
/* reply in the 100's are nice but we need to wait for another reply
call ourself recursively to find the reply > 199 and forget this reply.
*/
*/
return woomera_message_parse(fd, wmsg, timeout, profile, event_queue);
} else {
return switch_test_flag(wmsg, WFLAG_EXISTS);
@@ -771,7 +756,7 @@ static int woomera_message_parse(switch_socket_t *fd, woomera_message * wmsg, in
}
static int connect_woomera(switch_socket_t **new_sock, woomera_profile * profile, int flags)
static int connect_woomera(switch_socket_t **new_sock, woomera_profile *profile, int flags)
{
switch_sockaddr_t *sa;
@@ -786,12 +771,12 @@ static int connect_woomera(switch_socket_t **new_sock, woomera_profile * profile
return -1;
}
/*
status = switch_socket_bind((*new_sock), sa);
if (0 && status != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't Bind to %s:%d!\n", profile->woomera_host, profile->woomera_port);
return -1;
}
*/
status = switch_socket_bind((*new_sock), sa);
if (0 && status != SWITCH_STATUS_SUCCESS) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't Bind to %s:%d!\n", profile->woomera_host, profile->woomera_port);
return -1;
}
*/
status = switch_socket_connect((*new_sock), sa);
if (status != SWITCH_STATUS_SUCCESS) {
return -1;
@@ -800,7 +785,7 @@ static int connect_woomera(switch_socket_t **new_sock, woomera_profile * profile
return 1;
}
static int woomera_profile_thread_running(woomera_profile * profile, int set, int new)
static int woomera_profile_thread_running(woomera_profile *profile, int set, int new)
{
int running = 0;
@@ -811,21 +796,20 @@ static int woomera_profile_thread_running(woomera_profile * profile, int set, in
running = profile->thread_running;
switch_mutex_unlock(profile->iolock);
return running;
}
static int woomera_locate_socket(woomera_profile * profile, switch_socket_t **woomera_socket)
static int woomera_locate_socket(woomera_profile *profile, switch_socket_t **woomera_socket)
{
woomera_message wmsg;
for (;;) {
while (connect_woomera(woomera_socket, profile, 0) < 0) {
if (!woomera_profile_thread_running(profile, 0, 0)) {
break;
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "{%s} Cannot Reconnect to Woomera! retry in 5 seconds\n",
profile->name);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "{%s} Cannot Reconnect to Woomera! retry in 5 seconds\n", profile->name);
switch_sleep(WOOMERA_RECONNECT_TIME);
}
@@ -833,7 +817,11 @@ static int woomera_locate_socket(woomera_profile * profile, switch_socket_t **wo
if (switch_test_flag(profile, PFLAG_INBOUND)) {
woomera_printf(profile, *woomera_socket, "LISTEN%s", WOOMERA_RECORD_SEPERATOR);
if (woomera_message_parse(*woomera_socket,
&wmsg, WOOMERA_HARD_TIMEOUT, profile, &profile->event_queue) < 0) {
&wmsg,
WOOMERA_HARD_TIMEOUT,
profile,
&profile->event_queue
) < 0) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "{%s} HELP! Woomera is broken!\n", profile->name);
globals.panic = 1;
woomera_profile_thread_running(&default_profile, 1, 0);
@@ -841,7 +829,7 @@ static int woomera_locate_socket(woomera_profile * profile, switch_socket_t **wo
if (*woomera_socket) {
woomera_socket_close(woomera_socket);
}
continue;
}
}
@@ -849,13 +837,13 @@ static int woomera_locate_socket(woomera_profile * profile, switch_socket_t **wo
}
switch_sleep(100);
break;
}
}
return *woomera_socket ? 1 : 0;
}
static int tech_create_read_socket(private_object * tech_pvt)
static int tech_create_read_socket(private_object *tech_pvt)
{
switch_memory_pool *pool = switch_core_session_get_pool(tech_pvt->session);
@@ -869,20 +857,20 @@ static int tech_create_read_socket(private_object * tech_pvt)
switch_sockaddr_info_get(&tech_pvt->udpread, tech_pvt->profile->audio_ip, SWITCH_UNSPEC, tech_pvt->port, 0, pool);
if (switch_socket_create(&tech_pvt->udp_socket, AF_INET, SOCK_DGRAM, 0, pool) == SWITCH_STATUS_SUCCESS) {
switch_socket_bind(tech_pvt->udp_socket, tech_pvt->udpread);
switch_socket_create_pollfd(&tech_pvt->read_poll, tech_pvt->udp_socket, SWITCH_POLLIN | SWITCH_POLLERR, pool);
switch_socket_create_pollfd(&tech_pvt->write_poll, tech_pvt->udp_socket, SWITCH_POLLOUT | SWITCH_POLLERR, pool);
switch_socket_create_pollfd(&tech_pvt->read_poll, tech_pvt->udp_socket, SWITCH_POLLIN|SWITCH_POLLERR, pool);
switch_socket_create_pollfd(&tech_pvt->write_poll, tech_pvt->udp_socket, SWITCH_POLLOUT|SWITCH_POLLERR, pool);
}
return 0;
}
static int tech_activate(private_object * tech_pvt)
static int tech_activate(private_object *tech_pvt)
{
woomera_message wmsg;
if (tech_pvt) {
if ((connect_woomera(&tech_pvt->command_channel, tech_pvt->profile, 0))) {
if((connect_woomera(&tech_pvt->command_channel, tech_pvt->profile, 0))) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "connected to woomera!\n");
} else {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Can't connect to woomera!\n");
@@ -893,25 +881,34 @@ static int tech_activate(private_object * tech_pvt)
if (switch_test_flag(tech_pvt, TFLAG_OUTBOUND)) {
woomera_printf(tech_pvt->profile,
tech_pvt->command_channel,
"CALL %s%sRaw-Audio: %s/%d%sLocal-Name: %s!%s%s",
tech_pvt->command_channel,
"CALL %s%sRaw-Audio: %s/%d%sLocal-Name: %s!%s%s",
tech_pvt->caller_profile->destination_number,
WOOMERA_LINE_SEPERATOR,
tech_pvt->profile->audio_ip,
tech_pvt->port,
WOOMERA_LINE_SEPERATOR,
tech_pvt->caller_profile->caller_id_name,
tech_pvt->caller_profile->caller_id_number, WOOMERA_RECORD_SEPERATOR);
tech_pvt->caller_profile->caller_id_number,
WOOMERA_RECORD_SEPERATOR
);
woomera_message_parse(tech_pvt->command_channel,
&wmsg, WOOMERA_HARD_TIMEOUT, tech_pvt->profile, &tech_pvt->event_queue);
&wmsg,
WOOMERA_HARD_TIMEOUT,
tech_pvt->profile,
&tech_pvt->event_queue
);
} else {
switch_set_flag(tech_pvt, TFLAG_PARSE_INCOMING);
woomera_printf(tech_pvt->profile, tech_pvt->command_channel, "LISTEN%s", WOOMERA_RECORD_SEPERATOR);
if (woomera_message_parse(tech_pvt->command_channel,
&wmsg, WOOMERA_HARD_TIMEOUT, tech_pvt->profile, &tech_pvt->event_queue) < 0) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "{%s} HELP! Woomera is broken!\n",
tech_pvt->profile->name);
&wmsg,
WOOMERA_HARD_TIMEOUT,
tech_pvt->profile,
&tech_pvt->event_queue
) < 0) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "{%s} HELP! Woomera is broken!\n", tech_pvt->profile->name);
switch_set_flag(tech_pvt, TFLAG_ABORT);
globals.panic = 1;
}
@@ -944,9 +941,9 @@ static void *woomera_channel_thread_run(switch_thread *thread, void *obj)
if (!tech_pvt->udp_socket) {
tech_create_read_socket(tech_pvt);
}
for (;;) {
for(;;) {
if (globals.panic) {
switch_set_flag(tech_pvt, TFLAG_ABORT);
}
@@ -967,29 +964,31 @@ static void *woomera_channel_thread_run(switch_thread *thread, void *obj)
if (switch_test_flag(tech_pvt, TFLAG_ANSWER)) {
switch_clear_flag(tech_pvt, TFLAG_ANSWER);
#ifdef USE_ANSWER
woomera_printf(tech_pvt->profile, tech_pvt->command_channel, "ANSWER %s%s", tech_pvt->call_info.callid,
WOOMERA_RECORD_SEPERATOR);
if (woomera_message_parse
(tech_pvt->command_channel, &wmsg, WOOMERA_HARD_TIMEOUT, tech_pvt->profile,
&tech_pvt->event_queue) < 0) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "{%s} HELP! Woomera is broken!\n",
tech_pvt->profile->name);
woomera_printf(tech_pvt->profile, tech_pvt->command_channel, "ANSWER %s%s",tech_pvt->call_info.callid, WOOMERA_RECORD_SEPERATOR);
if(woomera_message_parse(tech_pvt->command_channel,
&wmsg,
WOOMERA_HARD_TIMEOUT,
tech_pvt->profile,
&tech_pvt->event_queue
) < 0) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "{%s} HELP! Woomera is broken!\n", tech_pvt->profile->name);
switch_set_flag(tech_pvt, TFLAG_ABORT);
globals.panic = 1;
continue;
}
#endif
}
if (switch_test_flag(tech_pvt, TFLAG_DTMF)) {
switch_mutex_lock(tech_pvt->iolock);
woomera_printf(tech_pvt->profile, tech_pvt->command_channel, "DTMF %s %s%s", tech_pvt->call_info.callid,
tech_pvt->dtmfbuf, WOOMERA_RECORD_SEPERATOR);
if (woomera_message_parse
(tech_pvt->command_channel, &wmsg, WOOMERA_HARD_TIMEOUT, tech_pvt->profile,
&tech_pvt->event_queue) < 0) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "{%s} HELP! Woomera is broken!\n",
tech_pvt->profile->name);
woomera_printf(tech_pvt->profile, tech_pvt->command_channel, "DTMF %s %s%s",tech_pvt->call_info.callid, tech_pvt->dtmfbuf, WOOMERA_RECORD_SEPERATOR);
if(woomera_message_parse(tech_pvt->command_channel,
&wmsg,
WOOMERA_HARD_TIMEOUT,
tech_pvt->profile,
&tech_pvt->event_queue
) < 0) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "{%s} HELP! Woomera is broken!\n", tech_pvt->profile->name);
switch_set_flag(tech_pvt, TFLAG_ABORT);
globals.panic = 1;
continue;
@@ -998,14 +997,13 @@ static void *woomera_channel_thread_run(switch_thread *thread, void *obj)
memset(tech_pvt->dtmfbuf, 0, sizeof(tech_pvt->dtmfbuf));
switch_mutex_unlock(tech_pvt->iolock);
}
#if 1==0 /*convert to use switch_time_now */
if (tech_pvt->timeout) {
#if 1==0 /*convert to use switch_time_now */
if(tech_pvt->timeout) {
struct timeval now;
int elapsed;
gettimeofday(&now, NULL);
elapsed =
(((now.tv_sec * 1000) + now.tv_usec / 1000) -
((tech_pvt->started.tv_sec * 1000) + tech_pvt->started.tv_usec / 1000));
elapsed = (((now.tv_sec * 1000) + now.tv_usec / 1000) - ((tech_pvt->started.tv_sec * 1000) + tech_pvt->started.tv_usec / 1000));
if (elapsed > tech_pvt->timeout) {
/* call timed out! */
switch_set_flag(tech_pvt, TFLAG_ABORT);
@@ -1017,24 +1015,29 @@ static void *woomera_channel_thread_run(switch_thread *thread, void *obj)
break;
}
/* Check for events */
if ((res = woomera_dequeue_event(&tech_pvt->event_queue, &wmsg)) ||
(res = woomera_message_parse(tech_pvt->command_channel, &wmsg, 100, tech_pvt->profile, NULL))) {
if((res = woomera_dequeue_event(&tech_pvt->event_queue, &wmsg)) ||
(res = woomera_message_parse(tech_pvt->command_channel,
&wmsg,
100,
tech_pvt->profile,
NULL
))) {
if (res < 0 || !strcasecmp(wmsg.command, "HANGUP")) {
switch_set_flag(tech_pvt, TFLAG_ABORT);
continue;
} else if (!strcasecmp(wmsg.command, "DTMF")) {
/*
struct ast_frame dtmf_frame = {AST_FRAME_DTMF};
int x = 0;
for (x = 0; x < strlen(wmsg.command_args); x++) {
dtmf_frame.subclass = wmsg.command_args[x];
ast_queue_frame(tech_pvt->owner, ast_frdup(&dtmf_frame));
if (globals.debug > 1) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, WOOMERA_DEBUG_PREFIX "SEND DTMF [%c] to %s\n", dtmf_frame.subclass, tech_pvt->owner->name);
}
}
*/
struct ast_frame dtmf_frame = {AST_FRAME_DTMF};
int x = 0;
for (x = 0; x < strlen(wmsg.command_args); x++) {
dtmf_frame.subclass = wmsg.command_args[x];
ast_queue_frame(tech_pvt->owner, ast_frdup(&dtmf_frame));
if (globals.debug > 1) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, WOOMERA_DEBUG_PREFIX "SEND DTMF [%c] to %s\n", dtmf_frame.subclass, tech_pvt->owner->name);
}
}
*/
} else if (!strcasecmp(wmsg.command, "PROCEED")) {
/* This packet has lots of info so well keep it */
tech_pvt->call_info = wmsg;
@@ -1056,7 +1059,7 @@ static void *woomera_channel_thread_run(switch_thread *thread, void *obj)
if ((p = woomera_message_header(&wmsg, "Remote-Name"))) {
strncpy(cid_name, p, sizeof(cid_name));
}
if ((cid_num = strchr(cid_name, '!'))) {
*cid_num = '\0';
cid_num++;
@@ -1067,31 +1070,40 @@ static void *woomera_channel_thread_run(switch_thread *thread, void *obj)
if ((tech_pvt->caller_profile = switch_caller_profile_new(session,
tech_pvt->profile->dialplan,
cid_name, cid_num, ip, NULL, NULL, exten))) {
cid_name,
cid_num,
ip,
NULL,
NULL,
exten))) {
char name[128];
switch_channel_set_caller_profile(channel, tech_pvt->caller_profile);
snprintf(name, sizeof(name), "Woomera/%s-%04x", tech_pvt->caller_profile->destination_number,
rand() & 0xffff);
snprintf(name, sizeof(name), "Woomera/%s-%04x", tech_pvt->caller_profile->destination_number, rand() & 0xffff);
switch_channel_set_name(channel, name);
}
woomera_printf(tech_pvt->profile, tech_pvt->command_channel,
woomera_printf(tech_pvt->profile, tech_pvt->command_channel,
"%s %s%s"
"Raw-Audio: %s/%d%s",
MEDIA_ANSWER,
wmsg.callid,
WOOMERA_LINE_SEPERATOR,
tech_pvt->profile->audio_ip, tech_pvt->port, WOOMERA_RECORD_SEPERATOR);
if (woomera_message_parse(tech_pvt->command_channel,
&wmsg, WOOMERA_HARD_TIMEOUT, tech_pvt->profile, &tech_pvt->event_queue) < 0) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "{%s} HELP! Woomera is broken!\n",
tech_pvt->profile->name);
tech_pvt->profile->audio_ip,
tech_pvt->port,
WOOMERA_RECORD_SEPERATOR);
if(woomera_message_parse(tech_pvt->command_channel,
&wmsg,
WOOMERA_HARD_TIMEOUT,
tech_pvt->profile,
&tech_pvt->event_queue
) < 0) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "{%s} HELP! Woomera is broken!\n", tech_pvt->profile->name);
switch_set_flag(tech_pvt, TFLAG_ABORT);
globals.panic = 1;
continue;
}
} else if (!strcasecmp(wmsg.command, "CONNECT")) {
} else if (!strcasecmp(wmsg.command, "MEDIA")) {
@@ -1103,7 +1115,7 @@ static void *woomera_channel_thread_run(switch_thread *thread, void *obj)
int port = 0;
strncpy(ip, raw_audio_header, sizeof(ip) - 1);
if ((ptr = strchr(ip, '/'))) {
if ((ptr=strchr(ip, '/'))) {
*ptr = '\0';
ptr++;
port = atoi(ptr);
@@ -1117,12 +1129,9 @@ static void *woomera_channel_thread_run(switch_thread *thread, void *obj)
SWITCH_UNSPEC,
port,
0,
switch_core_session_get_pool(tech_pvt->session)) !=
SWITCH_STATUS_SUCCESS) {
switch_core_session_get_pool(tech_pvt->session)) != SWITCH_STATUS_SUCCESS) {
if (globals.debug) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE,
WOOMERA_DEBUG_PREFIX "{%s} Cannot resolve %s\n",
tech_pvt->profile->name, ip);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, WOOMERA_DEBUG_PREFIX "{%s} Cannot resolve %s\n", tech_pvt->profile->name, ip);
}
switch_channel_hangup(channel);
}
@@ -1130,22 +1139,20 @@ static void *woomera_channel_thread_run(switch_thread *thread, void *obj)
}
}
if (globals.debug > 2) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, WOOMERA_DEBUG_PREFIX "CHECK {%s}(%d)\n",
tech_pvt->profile->name, res);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, WOOMERA_DEBUG_PREFIX "CHECK {%s}(%d)\n", tech_pvt->profile->name, res);
}
}
if (globals.debug > 1) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, WOOMERA_DEBUG_PREFIX "Monitor thread for %s done.\n",
tech_pvt->profile->name);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, WOOMERA_DEBUG_PREFIX "Monitor thread for %s done.\n", tech_pvt->profile->name);
}
return NULL;
}
static void *woomera_thread_run(void *obj)
static void *woomera_thread_run(void *obj)
{
int res = 0;
@@ -1154,12 +1161,12 @@ static void *woomera_thread_run(void *obj)
profile = obj;
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Started Woomera Thread {%s}.\n", profile->name);
profile->thread_running = 1;
profile->woomera_socket = NULL;
while (woomera_profile_thread_running(profile, 0, 0)) {
while(woomera_profile_thread_running(profile, 0, 0)) {
/* listen on socket and handle events */
if (globals.panic == 2) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Woomera is disabled!\n");
@@ -1167,22 +1174,21 @@ static void *woomera_thread_run(void *obj)
continue;
}
if (!profile->woomera_socket) {
if (! profile->woomera_socket) {
if (woomera_locate_socket(profile, &profile->woomera_socket)) {
globals.panic = 0;
}
if (!woomera_profile_thread_running(profile, 0, 0)) {
break;
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Woomera Thread Up {%s} %s/%d\n", profile->name,
profile->woomera_host, profile->woomera_port);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Woomera Thread Up {%s} %s/%d\n", profile->name, profile->woomera_host, profile->woomera_port);
}
if (globals.panic) {
if (globals.panic != 2) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Help I'm in a state of panic!\n");
globals.panic = 0; //fix
globals.panic = 0;//fix
}
woomera_socket_close(&profile->woomera_socket);
@@ -1191,12 +1197,15 @@ static void *woomera_thread_run(void *obj)
}
if ((res = woomera_dequeue_event(&profile->event_queue, &wmsg) ||
(res = woomera_message_parse(profile->woomera_socket, &wmsg,
(res = woomera_message_parse(profile->woomera_socket,
&wmsg,
/* if we are not stingy with threads we can block forever */
0, profile, NULL)))) {
0,
profile,
NULL
)))) {
if (res < 0) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "{%s} HELP! I lost my connection to woomera!\n",
profile->name);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "{%s} HELP! I lost my connection to woomera!\n", profile->name);
woomera_socket_close(&profile->woomera_socket);
//global_set_flag(TFLAG_ABORT);
@@ -1207,16 +1216,18 @@ static void *woomera_thread_run(void *obj)
if (switch_test_flag(profile, PFLAG_INBOUND)) {
woomera_printf(profile, profile->woomera_socket, "LISTEN%s", WOOMERA_RECORD_SEPERATOR);
if (woomera_message_parse(profile->woomera_socket,
&wmsg, WOOMERA_HARD_TIMEOUT, profile, &profile->event_queue) < 0) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "{%s} HELP! Woomera is broken!\n",
profile->name);
&wmsg,
WOOMERA_HARD_TIMEOUT,
profile,
&profile->event_queue
) < 0) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "{%s} HELP! Woomera is broken!\n", profile->name);
globals.panic = 1;
woomera_socket_close(&profile->woomera_socket);
}
}
}
if (profile->woomera_socket) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Woomera Thread Up {%s} %s/%d\n", profile->name,
profile->woomera_host, profile->woomera_port);
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Woomera Thread Up {%s} %s/%d\n", profile->name, profile->woomera_host, profile->woomera_port);
}
}
continue;
@@ -1225,11 +1236,11 @@ static void *woomera_thread_run(void *obj)
if (!strcasecmp(wmsg.command, "INCOMING")) {
char *name;
switch_core_session *session;
if (!(name = woomera_message_header(&wmsg, "Remote-Address"))) {
name = woomera_message_header(&wmsg, "Channel-Name");
}
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "New Inbound Channel %s!\n", name);
if ((session = switch_core_session_request(&woomerachan_endpoint_interface, NULL))) {
struct private_object *tech_pvt;
@@ -1237,8 +1248,7 @@ static void *woomera_thread_run(void *obj)
switch_core_session_add_stream(session, NULL);
if ((tech_pvt =
(struct private_object *) switch_core_session_alloc(session, sizeof(struct private_object)))) {
if ((tech_pvt = (struct private_object *) switch_core_session_alloc(session, sizeof(struct private_object)))) {
memset(tech_pvt, 0, sizeof(*tech_pvt));
tech_pvt->profile = &default_profile;
channel = switch_core_session_get_channel(session);
@@ -1254,14 +1264,14 @@ static void *woomera_thread_run(void *obj)
}
}
}
if (globals.debug > 2) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Main Thread {%s} Select Return %d\n", profile->name, res);
}
switch_yield(100);
}
if (profile->woomera_socket) {
woomera_printf(profile, profile->woomera_socket, "BYE%s", WOOMERA_RECORD_SEPERATOR);
@@ -1295,31 +1305,30 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_shutdown(void)
return SWITCH_STATUS_SUCCESS;
}
SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_module_interface **interface, char *filename)
{
SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_module_interface **interface, char *filename) {
switch_config cfg;
char *var, *val;
struct woomera_profile *profile = &default_profile;
char *cf = "woomera.conf";
memset(&globals, 0, sizeof(globals));
globals.next_woomera_port = WOOMERA_MIN_PORT;
if (!switch_config_open_file(&cfg, cf)) {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "open of %s failed\n", cf);
return SWITCH_STATUS_TERM;
}
switch_set_flag(profile, PFLAG_INBOUND | PFLAG_OUTBOUND);
profile->name = "main";
strncpy(profile->dialplan, "default", sizeof(profile->dialplan) - 1);
while (switch_config_next_pair(&cfg, &var, &val)) {
if (!strcasecmp(cfg.category, "settings")) {
if (!strcmp(var, "noload") && atoi(val)) {
return SWITCH_STATUS_TERM;
}
if (!strcmp(var, "noload") && atoi(val)) {
return SWITCH_STATUS_TERM;
}
if (!strcmp(var, "debug")) {
globals.debug = atoi(val);
}
@@ -1350,7 +1359,7 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_modul
switch_config_close_file(&cfg);
if (switch_core_new_memory_pool(&module_pool) != SWITCH_STATUS_SUCCESS) {
//switch_console_printf(SWITCH_CHANNEL_CONSOLE, "OH OH no pool\n");
@@ -1367,7 +1376,7 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_modul
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "OH OH no lock\n");
return SWITCH_STATUS_TERM;
}
/* connect my internal structure to the blank pointer passed to me */
*interface = &woomerachan_module_interface;
@@ -1375,3 +1384,6 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_modul
/* indicate that the module should continue to be loaded */
return SWITCH_STATUS_SUCCESS;
}