From 3dcc973c9da8f971334454bca61e4ed6b1c76da4 Mon Sep 17 00:00:00 2001
From: Anthony Minessale <anthony.minessale@gmail.com>
Date: Wed, 22 Feb 2006 21:04:53 +0000
Subject: [PATCH] 1 of many PRI/SIP gateway related updates

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@653 d0543943-73ff-0310-b7d9-9358b9ac24b2
---
 libs/jrtplib/src/jrtp4c.cpp               |  8 +++-
 libs/jrtplib/src/jrtp4c.h                 |  2 +-
 src/include/switch_platform.h             |  4 ++
 src/mod/endpoints/mod_exosip/mod_exosip.c | 53 ++++++++++++++++++++++-
 4 files changed, 64 insertions(+), 3 deletions(-)

diff --git a/libs/jrtplib/src/jrtp4c.cpp b/libs/jrtplib/src/jrtp4c.cpp
index 395ead0e63..cb22768498 100644
--- a/libs/jrtplib/src/jrtp4c.cpp
+++ b/libs/jrtplib/src/jrtp4c.cpp
@@ -113,12 +113,14 @@ extern "C" {
 		jrtp4c->session->AbortWait();
 	}
 
-	int jrtp4c_read(struct jrtp4c *jrtp4c, void *data, int datalen)
+	int jrtp4c_read(struct jrtp4c *jrtp4c, void *data, int datalen, int *payload_type)
 	{
 		RTPPacket *pack;
 		int slen = 0;
 		bool hasdata = 0;
 
+		*payload_type = 0;
+
 		jrtp4c->session->BeginDataAccess();
 
 		jrtp4c->session->WaitForIncomingData(RTPTime(.5), &hasdata);
@@ -136,7 +138,11 @@ extern "C" {
 				slen = datalen;
 			}
 
+		
+			*payload_type = pack->GetPayloadType();
+
 			memcpy(data, pack->GetPayloadData(), slen);
+
 			delete pack;
 		}
 		jrtp4c->session->EndDataAccess();
diff --git a/libs/jrtplib/src/jrtp4c.h b/libs/jrtplib/src/jrtp4c.h
index a65c821a65..f168892468 100644
--- a/libs/jrtplib/src/jrtp4c.h
+++ b/libs/jrtplib/src/jrtp4c.h
@@ -49,7 +49,7 @@ extern "C" {
 	struct jrtp4c;
 	struct jrtp4c *jrtp4c_new(char *rx_ip, int rx_port, char *tx_ip, int tx_port, int payload, int sps, const char **err);
 	void jrtp4c_destroy(struct jrtp4c **jrtp4c);
-	int jrtp4c_read(struct jrtp4c *jrtp4c, void *data, int datalen);
+	int jrtp4c_read(struct jrtp4c *jrtp4c, void *data, int datalen, int *payload_type);
 	int jrtp4c_write(struct jrtp4c *jrtp4c, void *data, int datalen, uint32_t ts);
 	uint32_t jrtp4c_start(struct jrtp4c *jrtp4c);
 	uint32_t jrtp4c_get_ssrc(struct jrtp4c *jrtp4c);
diff --git a/src/include/switch_platform.h b/src/include/switch_platform.h
index a08fcfb7a0..ee4c953560 100644
--- a/src/include/switch_platform.h
+++ b/src/include/switch_platform.h
@@ -77,6 +77,10 @@ typedef unsigned long	in_addr_t;
 #endif
 
 #else
+/* packed attribute */
+#ifndef PACKED
+#define PACKED __attribute__ ((packed))
+#endif
 #include <sys/types.h>
 #ifndef getpid
 #include <unistd.h>
diff --git a/src/mod/endpoints/mod_exosip/mod_exosip.c b/src/mod/endpoints/mod_exosip/mod_exosip.c
index 8b56889aef..2dc793bce2 100644
--- a/src/mod/endpoints/mod_exosip/mod_exosip.c
+++ b/src/mod/endpoints/mod_exosip/mod_exosip.c
@@ -148,6 +148,7 @@ static switch_status parse_sdp_media(sdp_media_t * media, char **dname, char **d
 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);
+static void sdp_add_rfc2833(struct osip_rfc3264 *cnf);
 
 static struct private_object *get_pvt_by_call_id(int id)
 {
@@ -284,6 +285,9 @@ static switch_status exosip_on_init(switch_core_session *session)
 				}
 			}
 		}
+
+		sdp_add_rfc2833(tech_pvt->sdp_config);
+
 		/* Setup our INVITE */
 		eXosip_lock();
 		if ((dest_uri =
@@ -493,6 +497,7 @@ static switch_status exosip_read_frame(switch_core_session *session, switch_fram
 	struct private_object *tech_pvt = NULL;
 	size_t bytes = 0, samples = 0, frames = 0, ms = 0;
 	switch_channel *channel = NULL;
+	int payload = 0;
 
 	channel = switch_core_session_get_channel(session);
 	assert(channel != NULL);
@@ -523,7 +528,32 @@ static switch_status exosip_read_frame(switch_core_session *session, switch_fram
 		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));
+				jrtp4c_read(tech_pvt->rtp_session, tech_pvt->read_frame.data, sizeof(tech_pvt->read_buf), &payload);
+
+
+			
+			if (payload == 101) {
+				unsigned char *data;
+				unsigned int event;
+                unsigned int event_end;
+                unsigned int duration;
+				data = tech_pvt->read_frame.data;
+				
+				event = ntohl(*((unsigned int *)(data)));
+                event >>= 24;
+                event_end = ntohl(*((unsigned int *)(data)));
+                event_end <<= 8;
+                event_end >>= 24;
+                duration = ntohl(*((unsigned int *)(data)));
+                duration &= 0xFFFF;
+
+				printf("DTMF %d %d %d\n", event, event_end, duration);
+			}
+
+			if (payload != tech_pvt->payload_num) {
+				printf("lets skip %d\n", payload);
+				continue;
+			}
 
 			if (tech_pvt->read_frame.datalen > 0) {
 				bytes = tech_pvt->read_codec.implementation->encoded_bytes_per_frame;
@@ -696,6 +726,7 @@ static switch_status exosip_receive_message(switch_core_session *session, switch
 				/* Transmit 183 Progress with SDP */
 				eXosip_lock();
 				eXosip_call_build_answer(tech_pvt->tid, 183, &progress);
+				sdp_add_rfc2833(tech_pvt->sdp_config);
 				sdp_message_to_str(tech_pvt->local_sdp, &buf);
 				osip_message_set_body(progress, buf, strlen(buf));
 				osip_message_set_content_type(progress, "application/sdp");
@@ -828,6 +859,23 @@ SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_modul
 	return SWITCH_STATUS_SUCCESS;
 }
 
+static void sdp_add_rfc2833(struct osip_rfc3264 *cnf)
+{
+	sdp_media_t *med = NULL;
+	sdp_attribute_t *attr = NULL;
+
+	sdp_media_init(&med);
+	sdp_attribute_init(&attr);
+	attr->a_att_field = osip_strdup("rtpmap");
+	attr->a_att_value = osip_strdup("101 telephony-event/8000");
+	osip_list_add(med->a_attributes, attr, -1);
+	
+
+	med->m_media = osip_strdup("telephony-event");
+	osip_rfc3264_add_audio_media(cnf, med, -1);
+
+}
+
 static switch_status exosip_create_call(eXosip_event_t * event)
 {
 	switch_core_session *session;
@@ -908,8 +956,11 @@ static switch_status exosip_create_call(eXosip_event_t * event)
 			int i;
 			static const switch_codec_implementation *imp;
 
+			sdp_add_rfc2833(tech_pvt->sdp_config);
+
 			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++);