Index: chan_sip.c
===================================================================
RCS file: /usr/cvsroot/asterisk/channels/chan_sip.c,v
retrieving revision 1.556
diff -u -r1.556 chan_sip.c
--- chan_sip.c	9 Nov 2004 02:38:18 -0000	1.556
+++ chan_sip.c	18 Nov 2004 21:51:40 -0000
@@ -141,6 +141,8 @@
 
 static int global_rtpholdtimeout = 0;
 
+static int global_rtpkeepalive = 0;
+
 static int global_trustrpid = 0;
 
 static int global_progressinband = 0;
@@ -325,8 +327,10 @@
 	int initid;				/* Auto-congest ID if appropriate */
 	int autokillid;				/* Auto-kill ID */
 	time_t lastrtprx;			/* Last RTP received */
+	time_t lastrtptx;			/* Last RTP sent */
 	int rtptimeout;				/* RTP timeout time */
 	int rtpholdtimeout;			/* RTP timeout when on hold */
+	int rtpkeepalive;			/* Send RTP packets for keepalive */
 
 	int subscribed;
     	int stateid;
@@ -427,6 +431,7 @@
 	int capability;
 	int rtptimeout;
 	int rtpholdtimeout;
+	int rtpkeepalive;
 	int insecure;
 #ifdef OSP_SUPPORT
 	int ospauth;				/* Allow OSP Authentication */
@@ -1822,6 +1827,7 @@
 					transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, 0);
 					p->progress = 1;
 				}
+				time(&p->lastrtptx);
 				res =  ast_rtp_write(p->rtp, frame);
 			}
 			ast_mutex_unlock(&p->lock);
@@ -1834,6 +1840,7 @@
 					transmit_response_with_sdp(p, "183 Session Progress", &p->initreq, 0);
 					p->progress = 1;
 				}
+				time(&p->lastrtptx);
 				res =  ast_rtp_write(p->vrtp, frame);
 			}
 			ast_mutex_unlock(&p->lock);
@@ -2325,6 +2332,7 @@
 #endif
 	p->rtptimeout = global_rtptimeout;
 	p->rtpholdtimeout = global_rtpholdtimeout;
+	p->rtpkeepalive = global_rtpkeepalive;
 	p->capability = global_capability;
 	if (p->dtmfmode & SIP_DTMF_RFC2833)
 		p->noncodeccapability |= AST_RTP_DTMF;
@@ -2616,6 +2624,7 @@
 
 	/* Update our last rtprx when we receive an SDP, too */
 	time(&p->lastrtprx);
+	time(&p->lastrtptx);
 
 	/* Get codec and RTP info from SDP */
 	if (strcasecmp(get_header(req, "Content-Type"), "application/sdp")) {
@@ -3472,6 +3481,7 @@
 	}
 	/* Update lastrtprx when we send our SDP */
 	time(&p->lastrtprx);
+	time(&p->lastrtptx);
 	return 0;
 }
 
@@ -7731,6 +7741,20 @@
 	int lastpeernum = -1;
 	int curpeernum;
 	int reloading;
+	struct ast_frame f = { AST_FRAME_NULL };
+	char rtpdata[160];
+
+	/* Create an empty voice frame for keepalive */
+	memset(rtpdata, 255, 160);
+	f.frametype = AST_FRAME_VOICE;
+	f.subclass = AST_FORMAT_ULAW;
+	f.data = rtpdata;
+	f.datalen = 160;
+	f.samples = 160;
+	f.offset = AST_FRIENDLY_OFFSET;
+	f.mallocd = 0;
+	f.src = "RTPKeepAlive";
+
 	/* Add an I/O event to our UDP socket */
 	if (sipsock > -1) 
 		ast_io_add(io, sipsock, sipsock_read, AST_IO_IN, NULL);
@@ -7756,8 +7780,13 @@
 		sip = iflist;
 		while(sip) {
 			ast_mutex_lock(&sip->lock);
-			if (sip->rtp && sip->owner && (sip->owner->_state == AST_STATE_UP) && sip->lastrtprx && (sip->rtptimeout || sip->rtpholdtimeout) && !sip->redirip.sin_addr.s_addr) {
-				if (t > sip->lastrtprx + sip->rtptimeout) {
+			if (sip->rtp && sip->owner && (sip->owner->_state == AST_STATE_UP) && !sip->redirip.sin_addr.s_addr) {
+				if (sip->lastrtptx && sip->rtpkeepalive && t > sip->lastrtptx + sip->rtpkeepalive) {
+					/* Need to send an empty RTP packet */
+					time(&sip->lastrtptx);
+					ast_rtp_write(sip->rtp, &f);
+				}
+				if (sip->lastrtprx && (sip->rtptimeout || sip->rtpholdtimeout) && t > sip->lastrtprx + sip->rtptimeout) {
 					/* Might be a timeout now -- see if we're on hold */
 					struct sockaddr_in sin;
 					ast_rtp_get_peer(sip->rtp, &sin);
@@ -8241,6 +8270,7 @@
 	peer->nat = global_nat;
 	peer->rtptimeout = global_rtptimeout;
 	peer->rtpholdtimeout = global_rtpholdtimeout;
+	peer->rtpkeepalive = global_rtptkeepalive;
 	peer->selfdestruct = 1;
 	peer->dynamic = 1;
 	peer->trustrpid = global_trustrpid;
@@ -8313,6 +8343,7 @@
 		peer->canreinvite = global_canreinvite;
 		peer->rtptimeout = global_rtptimeout;
 		peer->rtpholdtimeout = global_rtpholdtimeout;
+		peer->rtpkeepalive = global_rtpkeepalive;
 		peer->dtmfmode = global_dtmfmode;
 		peer->promiscredir = global_promiscredir;
 		peer->trustrpid = global_trustrpid;
@@ -8447,6 +8478,11 @@
 					ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d.  Using default.\n", v->value, v->lineno);
 					peer->rtpholdtimeout = global_rtpholdtimeout;
 				}
+			} else if (!strcasecmp(v->name, "rtpkeepalive")) {
+				if ((sscanf(v->value, "%d", &peer->rtpkeepalive) != 1) || (peer->rtpkeepalive < 0)) {
+					ast_log(LOG_WARNING, "'%s' is not a valid RTP keepalive time at line %d.  Using default.\n", v->value, v->lineno);
+					peer->rtpkeepalive = global_rtpkeepalive;
+				}
 			} else if (!strcasecmp(v->name, "qualify")) {
 				if (!strcasecmp(v->value, "no")) {
 					peer->maxms = 0;
@@ -8536,6 +8572,7 @@
 	ourport = DEFAULT_SIP_PORT;
 	global_rtptimeout = 0;
 	global_rtpholdtimeout = 0;
+	global_rtpkeepalive = 0;
 	pedanticsipchecking=0;
 	v = ast_variable_browse(cfg, "general");
 	while(v) {
@@ -8574,6 +8611,11 @@
 				ast_log(LOG_WARNING, "'%s' is not a valid RTP hold time at line %d.  Using default.\n", v->value, v->lineno);
 				global_rtpholdtimeout = 0;
 			}
+		} else if (!strcasecmp(v->name, "rtpkeepalive")) {
+			if ((sscanf(v->value, "%d", &global_rtpkeepalive) != 1) || (global_rtpkeepalive < 0)) {
+				ast_log(LOG_WARNING, "'%s' is not a valid RTP keepalive time at line %d.  Using default.\n", v->value, v->lineno);
+				global_rtpkeepalive = 0;
+			}
 		} else if (!strcasecmp(v->name, "videosupport")) {
 			videosupport = ast_true(v->value);
 		} else if (!strcasecmp(v->name, "notifymimetype")) {
@@ -8838,6 +8880,7 @@
 		}
 		/* Reset lastrtprx timer */
 		time(&p->lastrtprx);
+		time(&p->lastrtptx);
 		ast_mutex_unlock(&p->lock);
 		return 0;
 	}
