From ae84538819c9769fe2cd59472cbb6d4942514b13 Mon Sep 17 00:00:00 2001 From: Mathieu Rene Date: Wed, 25 Jul 2012 20:49:33 -0400 Subject: [PATCH 01/12] mark tdm channel as answered immediately --- libs/freetdm/mod_freetdm/tdm.c | 2 ++ src/switch_rtp.c | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/libs/freetdm/mod_freetdm/tdm.c b/libs/freetdm/mod_freetdm/tdm.c index 9ef95a7bfc..a01ac153b2 100644 --- a/libs/freetdm/mod_freetdm/tdm.c +++ b/libs/freetdm/mod_freetdm/tdm.c @@ -228,6 +228,8 @@ static switch_call_cause_t channel_outgoing_channel(switch_core_session_t *sessi goto fail; } + switch_channel_mark_answered(channel); + return SWITCH_CAUSE_SUCCESS; fail: diff --git a/src/switch_rtp.c b/src/switch_rtp.c index 3bad17ce17..3cc5255e8b 100644 --- a/src/switch_rtp.c +++ b/src/switch_rtp.c @@ -34,6 +34,8 @@ //#define RTP_DEBUG_WRITE_DELTA //#define DEBUG_MISSED_SEQ +typedef unsigned long u_long; + #include #include #include @@ -1451,7 +1453,7 @@ SWITCH_DECLARE(switch_status_t) switch_rtp_add_crypto_key(switch_rtp_t *rtp_sess uint32_t index, switch_rtp_crypto_key_type_t type, unsigned char *key, switch_size_t keylen) { #ifndef ENABLE_SRTP - switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, "SRTP NOT SUPPORTED IN THIS BUILD!\n"); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "SRTP NOT SUPPORTED IN THIS BUILD!\n"); return SWITCH_STATUS_FALSE; #else switch_rtp_crypto_key_t *crypto_key; From c6fecc1ee814939dfefdbe80a84fff6d964cddc8 Mon Sep 17 00:00:00 2001 From: Mathieu Rene Date: Wed, 25 Jul 2012 20:50:18 -0400 Subject: [PATCH 02/12] go in consume media right away --- libs/freetdm/mod_freetdm/tdm.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libs/freetdm/mod_freetdm/tdm.c b/libs/freetdm/mod_freetdm/tdm.c index a01ac153b2..6076c193fe 100644 --- a/libs/freetdm/mod_freetdm/tdm.c +++ b/libs/freetdm/mod_freetdm/tdm.c @@ -256,6 +256,9 @@ fail: static switch_status_t channel_on_init(switch_core_session_t *session) { + switch_channel_t *channel = switch_core_session_get_channel(session); + + switch_channel_set_state(channel, CS_CONSUME_MEDIA); return SWITCH_STATUS_SUCCESS; } From 3bf9fef864ceb8854734003b51880b38bee1ed47 Mon Sep 17 00:00:00 2001 From: Mathieu Rene Date: Wed, 25 Jul 2012 20:54:47 -0400 Subject: [PATCH 03/12] originate returns a status, not a cause --- src/mod/endpoints/mod_media_gateway/media_gateway.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mod/endpoints/mod_media_gateway/media_gateway.c b/src/mod/endpoints/mod_media_gateway/media_gateway.c index 70d31e6f55..3ed79ab36a 100644 --- a/src/mod/endpoints/mod_media_gateway/media_gateway.c +++ b/src/mod/endpoints/mod_media_gateway/media_gateway.c @@ -134,7 +134,7 @@ switch_status_t megaco_activate_termination(mg_termination_t *term) } if (zstr(term->uuid)) { - if (switch_ivr_originate(NULL, &session, &cause, dialstring, 0, NULL, NULL, NULL, NULL, var_event, 0, NULL) != SWITCH_CAUSE_SUCCESS) { + if (switch_ivr_originate(NULL, &session, &cause, dialstring, 0, NULL, NULL, NULL, NULL, var_event, 0, NULL) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to instanciate termination [%s]: %s\n", term->name, switch_channel_cause2str(cause)); status = SWITCH_STATUS_FALSE; goto done; From b1398aaccbd6cf7aa4bde189c6006ebc099b076e Mon Sep 17 00:00:00 2001 From: Mathieu Rene Date: Wed, 25 Jul 2012 20:59:09 -0400 Subject: [PATCH 04/12] Im feeling bold --- src/switch_rtp.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/switch_rtp.c b/src/switch_rtp.c index 3cc5255e8b..16e473e30f 100644 --- a/src/switch_rtp.c +++ b/src/switch_rtp.c @@ -1834,6 +1834,7 @@ SWITCH_DECLARE(switch_rtp_t *) switch_rtp_new(const char *rx_host, goto end; } +#if 0 if (zstr(tx_host)) { *err = "Missing remote host"; goto end; @@ -1843,6 +1844,7 @@ SWITCH_DECLARE(switch_rtp_t *) switch_rtp_new(const char *rx_host, *err = "Missing remote port"; goto end; } +#endif if (switch_rtp_create(&rtp_session, payload, samples_per_interval, ms_per_packet, flags, timer_name, err, pool) != SWITCH_STATUS_SUCCESS) { goto end; @@ -1856,7 +1858,7 @@ SWITCH_DECLARE(switch_rtp_t *) switch_rtp_new(const char *rx_host, goto end; } - if (switch_rtp_set_remote_address(rtp_session, tx_host, tx_port, 0, SWITCH_TRUE, err) != SWITCH_STATUS_SUCCESS) { + if (!zstr(tx_host) && switch_rtp_set_remote_address(rtp_session, tx_host, tx_port, 0, SWITCH_TRUE, err) != SWITCH_STATUS_SUCCESS) { switch_mutex_unlock(rtp_session->flag_mutex); rtp_session = NULL; goto end; From 051692d268e9b180ec6e50b16af18d0f398af5a3 Mon Sep 17 00:00:00 2001 From: Mathieu Rene Date: Wed, 25 Jul 2012 21:04:15 -0400 Subject: [PATCH 05/12] save the uuid --- src/mod/endpoints/mod_media_gateway/media_gateway.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mod/endpoints/mod_media_gateway/media_gateway.c b/src/mod/endpoints/mod_media_gateway/media_gateway.c index 3ed79ab36a..9805548b6f 100644 --- a/src/mod/endpoints/mod_media_gateway/media_gateway.c +++ b/src/mod/endpoints/mod_media_gateway/media_gateway.c @@ -140,6 +140,7 @@ switch_status_t megaco_activate_termination(mg_termination_t *term) goto done; } + term->uuid = switch_core_strdup(term->pool, switch_core_session_get_uuid(session)); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Termination [%s] successfully instanciated as [%s] [%s]\n", term->name, dialstring, switch_core_session_get_uuid(session)); } From 0494df1ac4054e34ee6210442ba98bc50eec853d Mon Sep 17 00:00:00 2001 From: Mathieu Rene Date: Wed, 25 Jul 2012 21:06:47 -0400 Subject: [PATCH 06/12] give the tdm channels their caller profile so switch_ivr_uuid_bridge doesn't piss its pants off --- libs/freetdm/mod_freetdm/tdm.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libs/freetdm/mod_freetdm/tdm.c b/libs/freetdm/mod_freetdm/tdm.c index 6076c193fe..6398515b72 100644 --- a/libs/freetdm/mod_freetdm/tdm.c +++ b/libs/freetdm/mod_freetdm/tdm.c @@ -101,7 +101,7 @@ static switch_call_cause_t channel_outgoing_channel(switch_core_session_t *sessi *span_name = switch_event_get_header(var_event, kSPAN_NAME); int chan_id; int span_id; - + switch_caller_profile_t *caller_profile; ftdm_span_t *span; ftdm_channel_t *chan; switch_channel_t *channel; @@ -149,6 +149,10 @@ static switch_call_cause_t channel_outgoing_channel(switch_core_session_t *sessi tech_pvt->session = *new_session; switch_core_session_set_private(*new_session, tech_pvt); + + caller_profile = switch_caller_profile_clone(*new_session, outbound_profile); + switch_channel_set_caller_profile(channel, caller_profile); + snprintf(name, sizeof(name), "tdm/%d:%d", span_id, chan_id); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Connect outbound channel %s\n", name); switch_channel_set_name(channel, name); From 4d3038c9e4e882116ca53e2fe25910caf91b7078 Mon Sep 17 00:00:00 2001 From: Mathieu Rene Date: Wed, 25 Jul 2012 21:16:14 -0400 Subject: [PATCH 07/12] add logging before uuid_bridge, set frame->codec from tech_pvt->read_codec --- libs/freetdm/mod_freetdm/tdm.c | 1 + src/mod/endpoints/mod_media_gateway/media_gateway.c | 4 ++++ src/mod/endpoints/mod_sofia/rtp.c | 1 + 3 files changed, 6 insertions(+) diff --git a/libs/freetdm/mod_freetdm/tdm.c b/libs/freetdm/mod_freetdm/tdm.c index 6398515b72..89e0d3c464 100644 --- a/libs/freetdm/mod_freetdm/tdm.c +++ b/libs/freetdm/mod_freetdm/tdm.c @@ -337,6 +337,7 @@ top: *frame = &tech_pvt->read_frame; tech_pvt->read_frame.datalen = (uint32_t)len; tech_pvt->read_frame.samples = tech_pvt->read_frame.datalen; + tech_pvt->read_frame = &tech_pvt->read_codec; if (ftdm_channel_get_codec(tech_pvt->ftdm_channel) == FTDM_CODEC_SLIN) { tech_pvt->read_frame.samples /= 2; diff --git a/src/mod/endpoints/mod_media_gateway/media_gateway.c b/src/mod/endpoints/mod_media_gateway/media_gateway.c index 9805548b6f..3d10abf05c 100644 --- a/src/mod/endpoints/mod_media_gateway/media_gateway.c +++ b/src/mod/endpoints/mod_media_gateway/media_gateway.c @@ -286,6 +286,10 @@ switch_status_t megaco_context_add_termination(mg_context_t *ctx, mg_termination megaco_activate_termination(ctx->terminations[1]); } + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Bridging: %s (%s) <> %s (%s)\n", + ctx->terminations[0]->name, ctx->terminations[0]->uuid, + ctx->terminations[1]->name, ctx->terminations[1]->uuid); + switch_ivr_uuid_bridge(ctx->terminations[0]->uuid, ctx->terminations[1]->uuid); } diff --git a/src/mod/endpoints/mod_sofia/rtp.c b/src/mod/endpoints/mod_sofia/rtp.c index 631c8e7711..43e2725224 100644 --- a/src/mod/endpoints/mod_sofia/rtp.c +++ b/src/mod/endpoints/mod_sofia/rtp.c @@ -325,6 +325,7 @@ static switch_status_t channel_read_frame(switch_core_session_t *session, switch } tech_pvt->read_frame.flags = SFF_NONE; + tech_pvt->read_frame.codec = &tech_pvt->read_codec; status = switch_rtp_zerocopy_read_frame(tech_pvt->rtp_session, &tech_pvt->read_frame, flags); if (status != SWITCH_STATUS_SUCCESS && status != SWITCH_STATUS_BREAK) { From f85347cf7221f6c0a5eee1a783a28f2ba71ef1d9 Mon Sep 17 00:00:00 2001 From: Mathieu Rene Date: Wed, 25 Jul 2012 21:16:46 -0400 Subject: [PATCH 08/12] add logging before uuid_bridge, set frame->codec from tech_pvt->read_codec --- libs/freetdm/mod_freetdm/tdm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/freetdm/mod_freetdm/tdm.c b/libs/freetdm/mod_freetdm/tdm.c index 89e0d3c464..f22ded6cb1 100644 --- a/libs/freetdm/mod_freetdm/tdm.c +++ b/libs/freetdm/mod_freetdm/tdm.c @@ -337,7 +337,7 @@ top: *frame = &tech_pvt->read_frame; tech_pvt->read_frame.datalen = (uint32_t)len; tech_pvt->read_frame.samples = tech_pvt->read_frame.datalen; - tech_pvt->read_frame = &tech_pvt->read_codec; + tech_pvt->read_frame.codec = &tech_pvt->read_codec; if (ftdm_channel_get_codec(tech_pvt->ftdm_channel) == FTDM_CODEC_SLIN) { tech_pvt->read_frame.samples /= 2; From 039c18638c16c71b7165615b34437a8e871af961 Mon Sep 17 00:00:00 2001 From: Mathieu Rene Date: Wed, 25 Jul 2012 21:26:53 -0400 Subject: [PATCH 09/12] allocate recv buffer for tdm --- libs/freetdm/mod_freetdm/tdm.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libs/freetdm/mod_freetdm/tdm.c b/libs/freetdm/mod_freetdm/tdm.c index f22ded6cb1..2e81c59c71 100644 --- a/libs/freetdm/mod_freetdm/tdm.c +++ b/libs/freetdm/mod_freetdm/tdm.c @@ -52,6 +52,8 @@ typedef struct { switch_core_session_t *session; switch_codec_t read_codec, write_codec; switch_frame_t read_frame; + + unsigned char databuf[SWITCH_RECOMMENDED_BUFFER_SIZE]; } ctdm_private_t; static switch_status_t channel_on_init(switch_core_session_t *session); @@ -147,6 +149,8 @@ static switch_call_cause_t channel_outgoing_channel(switch_core_session_t *sessi tech_pvt->span_id = span_id; tech_pvt->ftdm_channel = chan; tech_pvt->session = *new_session; + tech_pvt->read_frame.buflen = sizeof(tech_pvt->databuf); + tech_pvt->read_frame.data = tech_pvt->databuf; switch_core_session_set_private(*new_session, tech_pvt); From c649c5b02355934e5394b3eba0d15be6a4906427 Mon Sep 17 00:00:00 2001 From: Mathieu Rene Date: Wed, 25 Jul 2012 21:49:47 -0400 Subject: [PATCH 10/12] allow remote addr modification on rtp --- src/mod/endpoints/mod_sofia/rtp.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/mod/endpoints/mod_sofia/rtp.c b/src/mod/endpoints/mod_sofia/rtp.c index 43e2725224..215f545990 100644 --- a/src/mod/endpoints/mod_sofia/rtp.c +++ b/src/mod/endpoints/mod_sofia/rtp.c @@ -410,15 +410,25 @@ static switch_status_t channel_receive_event(switch_core_session_t *session, swi { const char *command = switch_event_get_header(event, "command"); switch_channel_t *channel = switch_core_session_get_channel(session); + crtp_private_t *tech_pvt = switch_core_session_get_private(session); if (!zstr(command) && !strcasecmp(command, "media_modify")) { /* Compare parameters */ if (compare_var(event, channel, kREMOTEADDR) || - compare_var(event, channel, kREMOTEPORT) || - compare_var(event, channel, kLOCALADDR) || - compare_var(event, channel, kLOCALPORT)) { - /* We need to reset the rtp session */ + compare_var(event, channel, kREMOTEPORT)) { + char *remote_addr = switch_event_get_header(event, kREMOTEADDR); + char *szremote_port = switch_event_get_header(event, kREMOTEADDR); + switch_port_t remote_port = !zstr(szremote_port) ? atoi(szremote_port) : 0; + switch_channel_set_variable(channel, kREMOTEADDR, remote_addr); + switch_channel_set_variable(channel, kREMOTEPORT, szremote_port); + const char *err; + + if (switch_rtp_set_remote_address(tech_pvt->rtp_session, remote_addr, remote_port, 0, SWITCH_TRUE, &err) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Error setting RTP remote address: %s\n", err); + } else { + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Set RTP remote: %s:%d\n", remote_addr, (int)remote_port); + } } if (compare_var(event, channel, kCODEC) || @@ -426,7 +436,7 @@ static switch_status_t channel_receive_event(switch_core_session_t *session, swi compare_var(event, channel, kPT) || compare_var(event, channel, kRATE)) { /* Reset codec */ - + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, "Switching codec not yet implemented\n"); } } else { From 84796f84f1b2625bf85bb3d208da698ac9b87dae Mon Sep 17 00:00:00 2001 From: Mathieu Rene Date: Wed, 25 Jul 2012 21:58:03 -0400 Subject: [PATCH 11/12] allow remote addr modification on rtp --- src/mod/endpoints/mod_sofia/rtp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mod/endpoints/mod_sofia/rtp.c b/src/mod/endpoints/mod_sofia/rtp.c index 215f545990..b24bb1653a 100644 --- a/src/mod/endpoints/mod_sofia/rtp.c +++ b/src/mod/endpoints/mod_sofia/rtp.c @@ -419,10 +419,11 @@ static switch_status_t channel_receive_event(switch_core_session_t *session, swi char *remote_addr = switch_event_get_header(event, kREMOTEADDR); char *szremote_port = switch_event_get_header(event, kREMOTEADDR); switch_port_t remote_port = !zstr(szremote_port) ? atoi(szremote_port) : 0; + const char *err; + switch_channel_set_variable(channel, kREMOTEADDR, remote_addr); switch_channel_set_variable(channel, kREMOTEPORT, szremote_port); - const char *err; if (switch_rtp_set_remote_address(tech_pvt->rtp_session, remote_addr, remote_port, 0, SWITCH_TRUE, &err) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Error setting RTP remote address: %s\n", err); @@ -438,7 +439,6 @@ static switch_status_t channel_receive_event(switch_core_session_t *session, swi /* Reset codec */ switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, "Switching codec not yet implemented\n"); } - } else { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Received unknown command [%s] in event.\n", !command ? "null" : command); } From 43e39619ae76be00bedf2f15276b04cc0fbb6ec5 Mon Sep 17 00:00:00 2001 From: Mathieu Rene Date: Wed, 25 Jul 2012 22:24:12 -0400 Subject: [PATCH 12/12] prevent writes if we dont have a remote addr --- src/mod/endpoints/mod_sofia/rtp.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/mod/endpoints/mod_sofia/rtp.c b/src/mod/endpoints/mod_sofia/rtp.c index b24bb1653a..a12b3c6e55 100644 --- a/src/mod/endpoints/mod_sofia/rtp.c +++ b/src/mod/endpoints/mod_sofia/rtp.c @@ -188,11 +188,12 @@ static switch_call_cause_t channel_outgoing_channel(switch_core_session_t *sessi tech_pvt->dtmf_type = DTMF_2833; /* XXX */ if (zstr(local_addr) || local_port == 0) { - tech_pvt->mode = RTP_SENDONLY; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "The local address and port must be set\n"); + goto fail; } else if (zstr(remote_addr) || remote_port == 0) { - tech_pvt->mode = RTP_SENDRECV; + tech_pvt->mode = RTP_RECVONLY; } else { - + tech_pvt->mode = RTP_SENDRECV; } switch_core_session_set_private(*new_session, tech_pvt); @@ -314,7 +315,7 @@ static switch_status_t channel_read_frame(switch_core_session_t *session, switch tech_pvt = switch_core_session_get_private(session); assert(tech_pvt != NULL); - if (!tech_pvt->rtp_session) { + if (!tech_pvt->rtp_session || tech_pvt->mode == RTP_RECVONLY) { goto cng; } @@ -429,6 +430,7 @@ static switch_status_t channel_receive_event(switch_core_session_t *session, swi switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Error setting RTP remote address: %s\n", err); } else { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Set RTP remote: %s:%d\n", remote_addr, (int)remote_port); + tech_pvt->mode = RTP_SENDRECV; } } @@ -439,6 +441,15 @@ static switch_status_t channel_receive_event(switch_core_session_t *session, swi /* Reset codec */ switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, "Switching codec not yet implemented\n"); } + + if (compare_var(event, channel, kRFC2833PT)) { + const char *szpt = switch_channel_get_variable(channel, kRFC2833PT); + int pt = !zstr(szpt) ? atoi(szpt) : 0; + + switch_channel_set_variable(channel, kRFC2833PT, szpt); + switch_rtp_set_telephony_event(tech_pvt->rtp_session, pt); + } + } else { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Received unknown command [%s] in event.\n", !command ? "null" : command); }