[asterisk-commits] file: branch file/rtp_engine-mark2 r183439 - /team/file/rtp_engine-mark2/res/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Mar 19 15:47:13 CDT 2009


Author: file
Date: Thu Mar 19 15:47:10 2009
New Revision: 183439

URL: http://svn.digium.com/svn-view/asterisk?view=rev&rev=183439
Log:
Add in further work. This is equal to rtp.c in trunk except it is in a module.

Added:
    team/file/rtp_engine-mark2/res/res_rtp_asterisk.c   (with props)

Added: team/file/rtp_engine-mark2/res/res_rtp_asterisk.c
URL: http://svn.digium.com/svn-view/asterisk/team/file/rtp_engine-mark2/res/res_rtp_asterisk.c?view=auto&rev=183439
==============================================================================
--- team/file/rtp_engine-mark2/res/res_rtp_asterisk.c (added)
+++ team/file/rtp_engine-mark2/res/res_rtp_asterisk.c Thu Mar 19 15:47:10 2009
@@ -1,0 +1,2497 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 1999 - 2008, Digium, Inc.
+ *
+ * Mark Spencer <markster at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ *
+ * \brief Supports RTP and RTCP with Symmetric RTP support for NAT traversal.
+ *
+ * \author Mark Spencer <markster at digium.com>
+ *
+ * \note RTP is defined in RFC 3550.
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision: 138083 $")
+
+#include <sys/time.h>
+#include <signal.h>
+#include <fcntl.h>
+#include <math.h>
+
+#include "asterisk/stun.h"
+#include "asterisk/pbx.h"
+#include "asterisk/frame.h"
+#include "asterisk/channel.h"
+#include "asterisk/acl.h"
+#include "asterisk/config.h"
+#include "asterisk/lock.h"
+#include "asterisk/utils.h"
+#include "asterisk/netsock.h"
+#include "asterisk/cli.h"
+#include "asterisk/manager.h"
+#include "asterisk/unaligned.h"
+#include "asterisk/module.h"
+#include "asterisk/rtp_engine.h"
+
+#define MAX_TIMESTAMP_SKEW	640
+
+#define RTP_SEQ_MOD     (1<<16)	/*!< A sequence number can't be more than 16 bits */
+#define RTCP_DEFAULT_INTERVALMS   5000	/*!< Default milli-seconds between RTCP reports we send */
+#define RTCP_MIN_INTERVALMS       500	/*!< Min milli-seconds between RTCP reports we send */
+#define RTCP_MAX_INTERVALMS       60000	/*!< Max milli-seconds between RTCP reports we send */
+
+#define RTCP_PT_FUR     192
+#define RTCP_PT_SR      200
+#define RTCP_PT_RR      201
+#define RTCP_PT_SDES    202
+#define RTCP_PT_BYE     203
+#define RTCP_PT_APP     204
+
+#define RTP_MTU		1200
+
+#define DEFAULT_DTMF_TIMEOUT 3000	/*!< samples */
+
+#define ZFONE_PROFILE_ID 0x505a
+
+static int dtmftimeout = DEFAULT_DTMF_TIMEOUT;
+
+static int rtpstart = 5000;			/*!< First port for RTP sessions (set in rtp.conf) */
+static int rtpend = 31000;			/*!< Last port for RTP sessions (set in rtp.conf) */
+static int rtpdebug;			/*!< Are we debugging? */
+static int rtcpdebug;			/*!< Are we debugging RTCP? */
+static int rtcpstats;			/*!< Are we debugging RTCP? */
+static int rtcpinterval = RTCP_DEFAULT_INTERVALMS; /*!< Time between rtcp reports in millisecs */
+static struct sockaddr_in rtpdebugaddr;	/*!< Debug packets to/from this host */
+static struct sockaddr_in rtcpdebugaddr;	/*!< Debug RTCP packets to/from this host */
+#ifdef SO_NO_CHECK
+static int nochecksums;
+#endif
+static int strictrtp;
+
+enum strict_rtp_state {
+	STRICT_RTP_OPEN = 0, /*! No RTP packets should be dropped, all sources accepted */
+	STRICT_RTP_LEARN,    /*! Accept next packet as source */
+	STRICT_RTP_CLOSED,   /*! Drop all RTP packets not coming from source that was learned */
+};
+
+#define FLAG_3389_WARNING               (1 << 0)
+#define FLAG_NAT_ACTIVE                 (3 << 1)
+#define FLAG_NAT_INACTIVE               (0 << 1)
+#define FLAG_NAT_INACTIVE_NOWARN        (1 << 1)
+#define FLAG_NEED_MARKER_BIT            (1 << 3)
+#define FLAG_DTMF_COMPENSATE            (1 << 4)
+
+/*! \brief RTP session description */
+struct ast_rtp {
+	int s;
+	struct ast_frame f;
+	unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET];
+	unsigned int ssrc;		/*!< Synchronization source, RFC 3550, page 10. */
+	unsigned int themssrc;		/*!< Their SSRC */
+	unsigned int rxssrc;
+	unsigned int lastts;
+	unsigned int lastrxts;
+	unsigned int lastividtimestamp;
+	unsigned int lastovidtimestamp;
+	unsigned int lastitexttimestamp;
+	unsigned int lastotexttimestamp;
+	unsigned int lasteventseqn;
+	int lastrxseqno;                /*!< Last received sequence number */
+	unsigned short seedrxseqno;     /*!< What sequence number did they start with?*/
+	unsigned int seedrxts;          /*!< What RTP timestamp did they start with? */
+	unsigned int rxcount;           /*!< How many packets have we received? */
+	unsigned int rxoctetcount;      /*!< How many octets have we received? should be rxcount *160*/
+	unsigned int txcount;           /*!< How many packets have we sent? */
+	unsigned int txoctetcount;      /*!< How many octets have we sent? (txcount*160)*/
+	unsigned int cycles;            /*!< Shifted count of sequence number cycles */
+	double rxjitter;                /*!< Interarrival jitter at the moment */
+	double rxtransit;               /*!< Relative transit time for previous packet */
+	int lasttxformat;
+	int lastrxformat;
+
+	int rtptimeout;			/*!< RTP timeout time (negative or zero means disabled, negative value means temporarily disabled) */
+	int rtpholdtimeout;		/*!< RTP timeout when on hold (negative or zero means disabled, negative value means temporarily disabled). */
+	int rtpkeepalive;		/*!< Send RTP comfort noice packets for keepalive */
+
+	/* DTMF Reception Variables */
+	char resp;
+	unsigned int lastevent;
+	int dtmfcount;
+	unsigned int dtmfsamples;
+	/* DTMF Transmission Variables */
+	unsigned int lastdigitts;
+	char sending_digit;	/*!< boolean - are we sending digits */
+	char send_digit;	/*!< digit we are sending */
+	int send_payload;
+	int send_duration;
+	unsigned int flags;
+	struct timeval rxcore;
+	struct timeval txcore;
+	double drxcore;                 /*!< The double representation of the first received packet */
+	struct timeval lastrx;          /*!< timeval when we last received a packet */
+	struct timeval dtmfmute;
+	struct ast_smoother *smoother;
+	int *ioid;
+	unsigned short seqno;		/*!< Sequence number, RFC 3550, page 13. */
+	unsigned short rxseqno;
+	struct sched_context *sched;
+	struct io_context *io;
+	void *data;
+	struct ast_rtcp *rtcp;
+	struct ast_rtp *bridged;        /*!< Who we are Packet bridged to */
+
+	enum strict_rtp_state strict_rtp_state; /*!< Current state that strict RTP protection is in */
+	struct sockaddr_in strict_rtp_address;  /*!< Remote address information for strict RTP purposes */
+
+	struct rtp_red *red;
+};
+
+/*!
+ * \brief Structure defining an RTCP session.
+ *
+ * The concept "RTCP session" is not defined in RFC 3550, but since
+ * this structure is analogous to ast_rtp, which tracks a RTP session,
+ * it is logical to think of this as a RTCP session.
+ *
+ * RTCP packet is defined on page 9 of RFC 3550.
+ *
+ */
+struct ast_rtcp {
+	int rtcp_info;
+	int s;				/*!< Socket */
+	struct sockaddr_in us;		/*!< Socket representation of the local endpoint. */
+	struct sockaddr_in them;	/*!< Socket representation of the remote endpoint. */
+	unsigned int soc;		/*!< What they told us */
+	unsigned int spc;		/*!< What they told us */
+	unsigned int themrxlsr;		/*!< The middle 32 bits of the NTP timestamp in the last received SR*/
+	struct timeval rxlsr;		/*!< Time when we got their last SR */
+	struct timeval txlsr;		/*!< Time when we sent or last SR*/
+	unsigned int expected_prior;	/*!< no. packets in previous interval */
+	unsigned int received_prior;	/*!< no. packets received in previous interval */
+	int schedid;			/*!< Schedid returned from ast_sched_add() to schedule RTCP-transmissions*/
+	unsigned int rr_count;		/*!< number of RRs we've sent, not including report blocks in SR's */
+	unsigned int sr_count;		/*!< number of SRs we've sent */
+	unsigned int lastsrtxcount;     /*!< Transmit packet count when last SR sent */
+	double accumulated_transit;	/*!< accumulated a-dlsr-lsr */
+	double rtt;			/*!< Last reported rtt */
+	unsigned int reported_jitter;	/*!< The contents of their last jitter entry in the RR */
+	unsigned int reported_lost;	/*!< Reported lost packets in their RR */
+
+	double reported_maxjitter;
+	double reported_minjitter;
+	double reported_normdev_jitter;
+	double reported_stdev_jitter;
+	unsigned int reported_jitter_count;
+
+	double reported_maxlost;
+	double reported_minlost;
+	double reported_normdev_lost;
+	double reported_stdev_lost;
+
+	double rxlost;
+	double maxrxlost;
+	double minrxlost;
+	double normdev_rxlost;
+	double stdev_rxlost;
+	unsigned int rxlost_count;
+
+	double maxrxjitter;
+	double minrxjitter;
+	double normdev_rxjitter;
+	double stdev_rxjitter;
+	unsigned int rxjitter_count;
+	double maxrtt;
+	double minrtt;
+	double normdevrtt;
+	double stdevrtt;
+	unsigned int rtt_count;
+};
+
+struct rtp_red {
+	struct ast_frame t140;  /*!< Primary data  */
+	struct ast_frame t140red;   /*!< Redundant t140*/
+	unsigned char pt[AST_RED_MAX_GENERATION];  /*!< Payload types for redundancy data */
+	unsigned char ts[AST_RED_MAX_GENERATION]; /*!< Time stamps */
+	unsigned char len[AST_RED_MAX_GENERATION]; /*!< length of each generation */
+	int num_gen; /*!< Number of generations */
+	int schedid; /*!< Timer id */
+	int ti; /*!< How long to buffer data before send */
+	unsigned char t140red_data[64000];
+	unsigned char buf_data[64000]; /*!< buffered primary data */
+	int hdrlen;
+	long int prev_ts;
+};
+
+/* Forward Declarations */
+static int ast_rtp_new(struct ast_rtp_instance *instance, struct sched_context *sched, struct sockaddr_in *sin, void *data);
+static int ast_rtp_destroy(struct ast_rtp_instance *instance);
+static int ast_rtp_dtmf_begin(struct ast_rtp_instance *instance, char digit);
+static int ast_rtp_dtmf_end(struct ast_rtp_instance *instance, char digit);
+static void ast_rtp_new_source(struct ast_rtp_instance *instance);
+static int ast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame);
+static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtcp);
+static void ast_rtp_prop_set(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value);
+static int ast_rtp_fd(struct ast_rtp_instance *instance, int rtcp);
+static void ast_rtp_remote_address_set(struct ast_rtp_instance *instance, struct sockaddr_in *sin);
+static int rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations);
+static int rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame);
+static int ast_rtp_local_bridge(struct ast_rtp_instance *instance0, struct ast_rtp_instance *instance1);
+static int ast_rtp_get_stat(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat);
+static int ast_rtp_dtmf_compatible(struct ast_channel *chan0, struct ast_rtp_instance *instance0, struct ast_channel *chan1, struct ast_rtp_instance *instance1);
+
+/* RTP Engine Declaration */
+static struct ast_rtp_engine asterisk_rtp_engine = {
+	.name = "asterisk",
+	.new = ast_rtp_new,
+	.destroy = ast_rtp_destroy,
+	.dtmf_begin = ast_rtp_dtmf_begin,
+	.dtmf_end = ast_rtp_dtmf_end,
+	.new_source = ast_rtp_new_source,
+	.write = ast_rtp_write,
+	.read = ast_rtp_read,
+	.prop_set = ast_rtp_prop_set,
+	.fd = ast_rtp_fd,
+	.remote_address_set = ast_rtp_remote_address_set,
+	.red_init = rtp_red_init,
+	.red_buffer = rtp_red_buffer,
+	.local_bridge = ast_rtp_local_bridge,
+	.get_stat = ast_rtp_get_stat,
+	.dtmf_compatible = ast_rtp_dtmf_compatible,
+};
+
+static inline int rtp_debug_test_addr(struct sockaddr_in *addr)
+{
+	if (!rtpdebug) {
+		return 0;
+	}
+
+	if (rtpdebugaddr.sin_addr.s_addr) {
+		if (((ntohs(rtpdebugaddr.sin_port) != 0)
+		     && (rtpdebugaddr.sin_port != addr->sin_port))
+		    || (rtpdebugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
+			return 0;
+	}
+
+	return 1;
+}
+
+static inline int rtcp_debug_test_addr(struct sockaddr_in *addr)
+{
+	if (!rtcpdebug) {
+		return 0;
+	}
+
+	if (rtcpdebugaddr.sin_addr.s_addr) {
+		if (((ntohs(rtcpdebugaddr.sin_port) != 0)
+		     && (rtcpdebugaddr.sin_port != addr->sin_port))
+		    || (rtcpdebugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
+			return 0;
+	}
+
+	return 1;
+}
+
+static unsigned int ast_rtcp_calc_interval(struct ast_rtp *rtp)
+{
+	unsigned int interval;
+	/*! \todo XXX Do a more reasonable calculation on this one
+	 * Look in RFC 3550 Section A.7 for an example*/
+	interval = rtcpinterval;
+	return interval;
+}
+
+/*! \brief Calculate normal deviation */
+static double normdev_compute(double normdev, double sample, unsigned int sample_count)
+{
+	normdev = normdev * sample_count + sample;
+	sample_count++;
+
+	return normdev / sample_count;
+}
+
+static double stddev_compute(double stddev, double sample, double normdev, double normdev_curent, unsigned int sample_count)
+{
+/*
+		for the formula check http://www.cs.umd.edu/~austinjp/constSD.pdf
+		return sqrt( (sample_count*pow(stddev,2) + sample_count*pow((sample-normdev)/(sample_count+1),2) + pow(sample-normdev_curent,2)) / (sample_count+1));
+		we can compute the sigma^2 and that way we would have to do the sqrt only 1 time at the end and would save another pow 2 compute
+		optimized formula
+*/
+#define SQUARE(x) ((x) * (x))
+
+	stddev = sample_count * stddev;
+	sample_count++;
+
+	return stddev +
+		( sample_count * SQUARE( (sample - normdev) / sample_count ) ) +
+		( SQUARE(sample - normdev_curent) / sample_count );
+
+#undef SQUARE
+}
+
+static int create_new_socket(const char *type)
+{
+	int sock = socket(AF_INET, SOCK_DGRAM, 0);
+
+	if (sock < 0) {
+		if (!type) {
+			type = "RTP/RTCP";
+		}
+		ast_log(LOG_WARNING, "Unable to allocate %s socket: %s\n", type, strerror(errno));
+	} else {
+		long flags = fcntl(sock, F_GETFL);
+		fcntl(sock, F_SETFL, flags | O_NONBLOCK);
+#ifdef SO_NO_CHECK
+		if (nochecksums) {
+			setsockopt(sock, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
+		}
+#endif
+	}
+
+	return sock;
+}
+
+static int ast_rtp_new(struct ast_rtp_instance *instance, struct sched_context *sched, struct sockaddr_in *sin, void *data)
+{
+	struct ast_rtp *rtp = NULL;
+	int x, startplace;
+
+	/* Create a new RTP structure to hold all of our data */
+	if (!(rtp = ast_calloc(1, sizeof(*rtp)))) {
+		return -1;
+	}
+
+	/* Set default parameters on the newly created RTP structure */
+	rtp->ssrc = ast_random();
+	rtp->seqno = ast_random() & 0xffff;
+	rtp->strict_rtp_state = (strictrtp ? STRICT_RTP_LEARN : STRICT_RTP_OPEN);
+
+	/* Create a new socket for us to listen on and use */
+	if ((rtp->s = create_new_socket("RTP")) < 0) {
+		ast_debug(1, "Failed to create a new socket for RTP instance '%p'\n", instance);
+		ast_free(rtp);
+		return -1;
+	}
+
+	/* Now actually find a free RTP port to use */
+	x = (rtpend == rtpstart) ? rtpstart : (ast_random() % (rtpend - rtpstart)) + rtpstart;
+	x = x & ~1;
+	startplace = x;
+
+	for (;;) {
+		instance->local_address.sin_port = htons(x);
+		/* Try to bind, this will tell us whether the port is available or not */
+		if (!bind(rtp->s, (struct sockaddr*)&instance->local_address, sizeof(instance->local_address))) {
+			ast_debug(1, "Allocated port %d for RTP instance '%p'\n", x, instance);
+			break;
+		}
+
+		x += 2;
+		if (x > rtpend) {
+			x = (rtpstart + 1) & ~1;
+		}
+
+		/* See if we ran out of ports or if the bind actually failed because of something other than the address being in use */
+		if (x == startplace || errno != EADDRINUSE) {
+			ast_log(LOG_ERROR, "Oh dear... we couldn't allocate a port for RTP instance '%p'\n", instance);
+			return -1;
+		}
+	}
+
+	/* Record any information we may need */
+	rtp->sched = sched;
+
+	/* Associate the RTP structure with the RTP instance and be done */
+	instance->data = rtp;
+
+	return 0;
+}
+
+static int ast_rtp_destroy(struct ast_rtp_instance *instance)
+{
+	struct ast_rtp *rtp = instance->data;
+
+	/* Destroy the smoother that was smoothing out audio if present */
+	if (rtp->smoother) {
+		ast_smoother_free(rtp->smoother);
+	}
+
+	/* Close our own socket so we no longer get packets */
+	if (rtp->s > -1) {
+		close(rtp->s);
+	}
+
+	/* Destroy RTCP if it was being used */
+	if (rtp->rtcp) {
+		AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
+		close(rtp->rtcp->s);
+		ast_free(rtp->rtcp);
+	}
+
+	/* Destroy RED if it was being used */
+	if (rtp->red) {
+		AST_SCHED_DEL(rtp->sched, rtp->red->schedid);
+		ast_free(rtp->red);
+	}
+
+	/* Finally destroy ourselves */
+	ast_free(rtp);
+
+	return 0;
+}
+
+static int ast_rtp_dtmf_begin(struct ast_rtp_instance *instance, char digit)
+{
+	struct ast_rtp *rtp = instance->data;
+	int hdrlen = 12, res = 0, i = 0, payload = 101;
+	char data[256];
+	unsigned int *rtpheader = (unsigned int*)data;
+
+	/* If we have no remote address information bail out now */
+	if (!instance->remote_address.sin_addr.s_addr || !instance->remote_address.sin_port) {
+		return -1;
+	}
+
+	/* Convert given digit into what we want to transmit */
+	if ((digit <= '9') && (digit >= '0')) {
+		digit -= '0';
+	} else if (digit == '*') {
+		digit = 10;
+	} else if (digit == '#') {
+		digit = 11;
+	} else if ((digit >= 'A') && (digit <= 'D')) {
+		digit = digit - 'A' + 12;
+	} else if ((digit >= 'a') && (digit <= 'd')) {
+		digit = digit - 'a' + 12;
+	} else {
+		ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
+		return -1;
+	}
+
+	/* Grab the payload that they expect the RFC2833 packet to be received in */
+	payload = ast_rtp_codecs_payload_code(&instance->codecs, 0, AST_RTP_DTMF);
+
+	rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
+	rtp->send_duration = 160;
+	rtp->lastdigitts = rtp->lastts + rtp->send_duration;
+
+	/* Create the actual packet that we will be sending */
+	rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno));
+	rtpheader[1] = htonl(rtp->lastdigitts);
+	rtpheader[2] = htonl(rtp->ssrc);
+
+	/* Actually send the packet */
+	for (i = 0; i < 2; i++) {
+		rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
+		res = sendto(rtp->s, (void *) rtpheader, hdrlen + 4, 0, (struct sockaddr *) &instance->remote_address, sizeof(instance->remote_address));
+		if (res < 0) {
+			ast_log(LOG_ERROR, "RTP Transmission error to %s:%u: %s\n",
+				ast_inet_ntoa(instance->remote_address.sin_addr), ntohs(instance->remote_address.sin_port), strerror(errno));
+		}
+		if (rtp_debug_test_addr(&instance->remote_address)) {
+			ast_verbose("Sent RTP DTMF packet to %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
+				    ast_inet_ntoa(instance->remote_address.sin_addr),
+				    ntohs(instance->remote_address.sin_port), payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
+		}
+		rtp->seqno++;
+		rtp->send_duration += 160;
+		rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno));
+	}
+
+	/* Record that we are in the process of sending a digit and information needed to continue doing so */
+	rtp->sending_digit = 1;
+	rtp->send_digit = digit;
+	rtp->send_payload = payload;
+
+	return 0;
+}
+
+static int ast_rtp_dtmf_continuation(struct ast_rtp_instance *instance)
+{
+	struct ast_rtp *rtp = instance->data;
+	int hdrlen = 12, res = 0;
+	char data[256];
+	unsigned int *rtpheader = (unsigned int*)data;
+
+	/* Make sure we know where the other side is so we can send them the packet */
+	if (!instance->remote_address.sin_addr.s_addr || !instance->remote_address.sin_port) {
+		return -1;
+	}
+
+	/* Actually create the packet we will be sending */
+	rtpheader[0] = htonl((2 << 30) | (1 << 23) | (rtp->send_payload << 16) | (rtp->seqno));
+	rtpheader[1] = htonl(rtp->lastdigitts);
+	rtpheader[2] = htonl(rtp->ssrc);
+	rtpheader[3] = htonl((rtp->send_digit << 24) | (0xa << 16) | (rtp->send_duration));
+	rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
+
+	/* Boom, send it on out */
+	res = sendto(rtp->s, (void *) rtpheader, hdrlen + 4, 0, (struct sockaddr *) &instance->remote_address, sizeof(instance->remote_address));
+	if (res < 0) {
+		ast_log(LOG_ERROR, "RTP Transmission error to %s:%d: %s\n",
+			ast_inet_ntoa(instance->remote_address.sin_addr),
+			ntohs(instance->remote_address.sin_port), strerror(errno));
+	}
+
+	if (rtp_debug_test_addr(&instance->remote_address)) {
+		ast_verbose("Sent RTP DTMF packet to %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
+			    ast_inet_ntoa(instance->remote_address.sin_addr),
+			    ntohs(instance->remote_address.sin_port), rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
+	}
+
+	/* And now we increment some values for the next time we swing by */
+	rtp->seqno++;
+	rtp->send_duration += 160;
+
+	return 0;
+}
+
+static int ast_rtp_dtmf_end(struct ast_rtp_instance *instance, char digit)
+{
+	struct ast_rtp *rtp = instance->data;
+	int hdrlen = 12, res = 0, i = 0;
+	char data[256];
+	unsigned int *rtpheader = (unsigned int*)data;
+
+	/* Make sure we know where the remote side is so we can send them the packet we construct */
+	if (!instance->remote_address.sin_addr.s_addr || !instance->remote_address.sin_port) {
+		return -1;
+	}
+
+	/* Convert the given digit to the one we are going to send */
+	if ((digit <= '9') && (digit >= '0')) {
+		digit -= '0';
+	} else if (digit == '*') {
+		digit = 10;
+	} else if (digit == '#') {
+		digit = 11;
+	} else if ((digit >= 'A') && (digit <= 'D')) {
+		digit = digit - 'A' + 12;
+	} else if ((digit >= 'a') && (digit <= 'd')) {
+		digit = digit - 'a' + 12;
+	} else {
+		ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
+		return -1;
+	}
+
+	rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
+
+	/* Construct the packet we are going to send */
+	rtpheader[0] = htonl((2 << 30) | (1 << 23) | (rtp->send_payload << 16) | (rtp->seqno));
+	rtpheader[1] = htonl(rtp->lastdigitts);
+	rtpheader[2] = htonl(rtp->ssrc);
+	rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
+	rtpheader[3] |= htonl((1 << 23));
+	rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
+
+	/* Send it 3 times, that's the magical number */
+	for (i = 0; i < 3; i++) {
+		res = sendto(rtp->s, (void *) rtpheader, hdrlen + 4, 0, (struct sockaddr *) &instance->remote_address, sizeof(instance->remote_address));
+		if (res < 0) {
+			ast_log(LOG_ERROR, "RTP Transmission error to %s:%d: %s\n",
+				ast_inet_ntoa(instance->remote_address.sin_addr),
+				ntohs(instance->remote_address.sin_port), strerror(errno));
+		}
+		if (rtp_debug_test_addr(&instance->remote_address)) {
+			ast_verbose("Sent RTP DTMF packet to %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
+				    ast_inet_ntoa(instance->remote_address.sin_addr),
+				    ntohs(instance->remote_address.sin_port), rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
+		}
+	}
+
+	/* Oh and we can't forget to turn off the stuff that says we are sending DTMF */
+	rtp->lastts += rtp->send_duration;
+	rtp->sending_digit = 0;
+	rtp->send_digit = 0;
+
+	return 0;
+}
+
+static void ast_rtp_new_source(struct ast_rtp_instance *instance)
+{
+	struct ast_rtp *rtp = instance->data;
+
+	/* We simply set this bit so that the next packet sent will have the marker bit turned on */
+	ast_set_flag(rtp, FLAG_NEED_MARKER_BIT);
+
+	return;
+}
+
+static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery)
+{
+	struct timeval t;
+	long ms;
+
+	if (ast_tvzero(rtp->txcore)) {
+		rtp->txcore = ast_tvnow();
+		rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000;
+	}
+
+	t = (delivery && !ast_tvzero(*delivery)) ? *delivery : ast_tvnow();
+	if ((ms = ast_tvdiff_ms(t, rtp->txcore)) < 0) {
+		ms = 0;
+	}
+	rtp->txcore = t;
+
+	return (unsigned int) ms;
+}
+
+static void timeval2ntp(struct timeval tv, unsigned int *msw, unsigned int *lsw)
+{
+	unsigned int sec, usec, frac;
+	sec = tv.tv_sec + 2208988800u; /* Sec between 1900 and 1970 */
+	usec = tv.tv_usec;
+	frac = (usec << 12) + (usec << 8) - ((usec * 3650) >> 6);
+	*msw = sec;
+	*lsw = frac;
+}
+
+/*! \brief Send RTCP recipient's report */
+static int ast_rtcp_write_rr(const void *data)
+{
+	struct ast_rtp *rtp = (struct ast_rtp *)data;
+	int res;
+	int len = 32;
+	unsigned int lost;
+	unsigned int extended;
+	unsigned int expected;
+	unsigned int expected_interval;
+	unsigned int received_interval;
+	int lost_interval;
+	struct timeval now;
+	unsigned int *rtcpheader;
+	char bdata[1024];
+	struct timeval dlsr;
+	int fraction;
+
+	double rxlost_current;
+
+	if (!rtp || !rtp->rtcp || (&rtp->rtcp->them.sin_addr == 0))
+		return 0;
+
+	if (!rtp->rtcp->them.sin_addr.s_addr) {
+		ast_log(LOG_ERROR, "RTCP RR transmission error, rtcp halted\n");
+		AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
+		return 0;
+	}
+
+	extended = rtp->cycles + rtp->lastrxseqno;
+	expected = extended - rtp->seedrxseqno + 1;
+	lost = expected - rtp->rxcount;
+	expected_interval = expected - rtp->rtcp->expected_prior;
+	rtp->rtcp->expected_prior = expected;
+	received_interval = rtp->rxcount - rtp->rtcp->received_prior;
+	rtp->rtcp->received_prior = rtp->rxcount;
+	lost_interval = expected_interval - received_interval;
+
+	if (lost_interval <= 0)
+		rtp->rtcp->rxlost = 0;
+	else rtp->rtcp->rxlost = rtp->rtcp->rxlost;
+	if (rtp->rtcp->rxlost_count == 0)
+		rtp->rtcp->minrxlost = rtp->rtcp->rxlost;
+	if (lost_interval < rtp->rtcp->minrxlost)
+		rtp->rtcp->minrxlost = rtp->rtcp->rxlost;
+	if (lost_interval > rtp->rtcp->maxrxlost)
+		rtp->rtcp->maxrxlost = rtp->rtcp->rxlost;
+
+	rxlost_current = normdev_compute(rtp->rtcp->normdev_rxlost, rtp->rtcp->rxlost, rtp->rtcp->rxlost_count);
+	rtp->rtcp->stdev_rxlost = stddev_compute(rtp->rtcp->stdev_rxlost, rtp->rtcp->rxlost, rtp->rtcp->normdev_rxlost, rxlost_current, rtp->rtcp->rxlost_count);
+	rtp->rtcp->normdev_rxlost = rxlost_current;
+	rtp->rtcp->rxlost_count++;
+
+	if (expected_interval == 0 || lost_interval <= 0)
+		fraction = 0;
+	else
+		fraction = (lost_interval << 8) / expected_interval;
+	gettimeofday(&now, NULL);
+	timersub(&now, &rtp->rtcp->rxlsr, &dlsr);
+	rtcpheader = (unsigned int *)bdata;
+	rtcpheader[0] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_RR << 16) | ((len/4)-1));
+	rtcpheader[1] = htonl(rtp->ssrc);
+	rtcpheader[2] = htonl(rtp->themssrc);
+	rtcpheader[3] = htonl(((fraction & 0xff) << 24) | (lost & 0xffffff));
+	rtcpheader[4] = htonl((rtp->cycles) | ((rtp->lastrxseqno & 0xffff)));
+	rtcpheader[5] = htonl((unsigned int)(rtp->rxjitter * 65536.));
+	rtcpheader[6] = htonl(rtp->rtcp->themrxlsr);
+	rtcpheader[7] = htonl((((dlsr.tv_sec * 1000) + (dlsr.tv_usec / 1000)) * 65536) / 1000);
+
+	/*! \note Insert SDES here. Probably should make SDES text equal to mimetypes[code].type (not subtype 'cos
+	  it can change mid call, and SDES can't) */
+	rtcpheader[len/4]     = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES << 16) | 2);
+	rtcpheader[(len/4)+1] = htonl(rtp->ssrc);               /* Our SSRC */
+	rtcpheader[(len/4)+2] = htonl(0x01 << 24);              /* Empty for the moment */
+	len += 12;
+
+	res = sendto(rtp->rtcp->s, (unsigned int *)rtcpheader, len, 0, (struct sockaddr *)&rtp->rtcp->them, sizeof(rtp->rtcp->them));
+
+	if (res < 0) {
+		ast_log(LOG_ERROR, "RTCP RR transmission error, rtcp halted: %s\n",strerror(errno));
+		/* Remove the scheduler */
+		AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
+		return 0;
+	}
+
+	rtp->rtcp->rr_count++;
+	if (rtcp_debug_test_addr(&rtp->rtcp->them)) {
+		ast_verbose("\n* Sending RTCP RR to %s:%d\n"
+			"  Our SSRC: %u\nTheir SSRC: %u\niFraction lost: %d\nCumulative loss: %u\n"
+			"  IA jitter: %.4f\n"
+			"  Their last SR: %u\n"
+			    "  DLSR: %4.4f (sec)\n\n",
+			    ast_inet_ntoa(rtp->rtcp->them.sin_addr),
+			    ntohs(rtp->rtcp->them.sin_port),
+			    rtp->ssrc, rtp->themssrc, fraction, lost,
+			    rtp->rxjitter,
+			    rtp->rtcp->themrxlsr,
+			    (double)(ntohl(rtcpheader[7])/65536.0));
+	}
+
+	return res;
+}
+
+/*! \brief Send RTCP sender's report */
+static int ast_rtcp_write_sr(const void *data)
+{
+	struct ast_rtp *rtp = (struct ast_rtp *)data;
+	int res;
+	int len = 0;
+	struct timeval now;
+	unsigned int now_lsw;
+	unsigned int now_msw;
+	unsigned int *rtcpheader;
+	unsigned int lost;
+	unsigned int extended;
+	unsigned int expected;
+	unsigned int expected_interval;
+	unsigned int received_interval;
+	int lost_interval;
+	int fraction;
+	struct timeval dlsr;
+	char bdata[512];
+
+	/* Commented condition is always not NULL if rtp->rtcp is not NULL */
+	if (!rtp || !rtp->rtcp/* || (&rtp->rtcp->them.sin_addr == 0)*/)
+		return 0;
+
+	if (!rtp->rtcp->them.sin_addr.s_addr) {  /* This'll stop rtcp for this rtp session */
+		ast_verbose("RTCP SR transmission error, rtcp halted\n");
+		AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
+		return 0;
+	}
+
+	gettimeofday(&now, NULL);
+	timeval2ntp(now, &now_msw, &now_lsw); /* fill thses ones in from utils.c*/
+	rtcpheader = (unsigned int *)bdata;
+	rtcpheader[1] = htonl(rtp->ssrc);               /* Our SSRC */
+	rtcpheader[2] = htonl(now_msw);                 /* now, MSW. gettimeofday() + SEC_BETWEEN_1900_AND_1970*/
+	rtcpheader[3] = htonl(now_lsw);                 /* now, LSW */
+	rtcpheader[4] = htonl(rtp->lastts);             /* FIXME shouldn't be that, it should be now */
+	rtcpheader[5] = htonl(rtp->txcount);            /* No. packets sent */
+	rtcpheader[6] = htonl(rtp->txoctetcount);       /* No. bytes sent */
+	len += 28;
+
+	extended = rtp->cycles + rtp->lastrxseqno;
+	expected = extended - rtp->seedrxseqno + 1;
+	if (rtp->rxcount > expected)
+		expected += rtp->rxcount - expected;
+	lost = expected - rtp->rxcount;
+	expected_interval = expected - rtp->rtcp->expected_prior;
+	rtp->rtcp->expected_prior = expected;
+	received_interval = rtp->rxcount - rtp->rtcp->received_prior;
+	rtp->rtcp->received_prior = rtp->rxcount;
+	lost_interval = expected_interval - received_interval;
+	if (expected_interval == 0 || lost_interval <= 0)
+		fraction = 0;
+	else
+		fraction = (lost_interval << 8) / expected_interval;
+	timersub(&now, &rtp->rtcp->rxlsr, &dlsr);
+	rtcpheader[7] = htonl(rtp->themssrc);
+	rtcpheader[8] = htonl(((fraction & 0xff) << 24) | (lost & 0xffffff));
+	rtcpheader[9] = htonl((rtp->cycles) | ((rtp->lastrxseqno & 0xffff)));
+	rtcpheader[10] = htonl((unsigned int)(rtp->rxjitter * 65536.));
+	rtcpheader[11] = htonl(rtp->rtcp->themrxlsr);
+	rtcpheader[12] = htonl((((dlsr.tv_sec * 1000) + (dlsr.tv_usec / 1000)) * 65536) / 1000);
+	len += 24;
+
+	rtcpheader[0] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SR << 16) | ((len/4)-1));
+
+	/* Insert SDES here. Probably should make SDES text equal to mimetypes[code].type (not subtype 'cos */
+	/* it can change mid call, and SDES can't) */
+	rtcpheader[len/4]     = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES << 16) | 2);
+	rtcpheader[(len/4)+1] = htonl(rtp->ssrc);               /* Our SSRC */
+	rtcpheader[(len/4)+2] = htonl(0x01 << 24);                    /* Empty for the moment */
+	len += 12;
+
+	res = sendto(rtp->rtcp->s, (unsigned int *)rtcpheader, len, 0, (struct sockaddr *)&rtp->rtcp->them, sizeof(rtp->rtcp->them));
+	if (res < 0) {
+		ast_log(LOG_ERROR, "RTCP SR transmission error to %s:%d, rtcp halted %s\n",ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port), strerror(errno));
+		AST_SCHED_DEL(rtp->sched, rtp->rtcp->schedid);
+		return 0;
+	}
+
+	/* FIXME Don't need to get a new one */
+	gettimeofday(&rtp->rtcp->txlsr, NULL);
+	rtp->rtcp->sr_count++;
+
+	rtp->rtcp->lastsrtxcount = rtp->txcount;
+
+	if (rtcp_debug_test_addr(&rtp->rtcp->them)) {
+		ast_verbose("* Sent RTCP SR to %s:%d\n", ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
+		ast_verbose("  Our SSRC: %u\n", rtp->ssrc);
+		ast_verbose("  Sent(NTP): %u.%010u\n", (unsigned int)now.tv_sec, (unsigned int)now.tv_usec*4096);
+		ast_verbose("  Sent(RTP): %u\n", rtp->lastts);
+		ast_verbose("  Sent packets: %u\n", rtp->txcount);
+		ast_verbose("  Sent octets: %u\n", rtp->txoctetcount);
+		ast_verbose("  Report block:\n");
+		ast_verbose("  Fraction lost: %u\n", fraction);
+		ast_verbose("  Cumulative loss: %u\n", lost);
+		ast_verbose("  IA jitter: %.4f\n", rtp->rxjitter);
+		ast_verbose("  Their last SR: %u\n", rtp->rtcp->themrxlsr);
+		ast_verbose("  DLSR: %4.4f (sec)\n\n", (double)(ntohl(rtcpheader[12])/65536.0));
+	}
+	manager_event(EVENT_FLAG_REPORTING, "RTCPSent", "To %s:%d\r\n"
+					    "OurSSRC: %u\r\n"
+					    "SentNTP: %u.%010u\r\n"
+					    "SentRTP: %u\r\n"
+					    "SentPackets: %u\r\n"
+					    "SentOctets: %u\r\n"
+					    "ReportBlock:\r\n"
+					    "FractionLost: %u\r\n"
+					    "CumulativeLoss: %u\r\n"
+					    "IAJitter: %.4f\r\n"
+					    "TheirLastSR: %u\r\n"
+		      "DLSR: %4.4f (sec)\r\n",
+		      ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port),
+		      rtp->ssrc,
+		      (unsigned int)now.tv_sec, (unsigned int)now.tv_usec*4096,
+		      rtp->lastts,
+		      rtp->txcount,
+		      rtp->txoctetcount,
+		      fraction,
+		      lost,
+		      rtp->rxjitter,
+		      rtp->rtcp->themrxlsr,
+		      (double)(ntohl(rtcpheader[12])/65536.0));
+	return res;
+}
+
+/*! \brief Write and RTCP packet to the far end
+ * \note Decide if we are going to send an SR (with Reception Block) or RR
+ * RR is sent if we have not sent any rtp packets in the previous interval */
+static int ast_rtcp_write(const void *data)
+{
+	struct ast_rtp *rtp = (struct ast_rtp *)data;
+	int res;
+
+	if (!rtp || !rtp->rtcp)
+		return 0;
+
+	if (rtp->txcount > rtp->rtcp->lastsrtxcount)
+		res = ast_rtcp_write_sr(data);
+	else
+		res = ast_rtcp_write_rr(data);
+
+	return res;
+}
+
+static int ast_rtp_raw_write(struct ast_rtp_instance *instance, struct ast_frame *frame, int codec)
+{
+	struct ast_rtp *rtp = instance->data;
+	int pred, mark = 0;
+	unsigned int ms = calc_txstamp(rtp, &frame->delivery);
+
+	if (rtp->sending_digit) {
+		return 0;
+	}
+
+	if (frame->frametype == AST_FRAME_VOICE) {
+		pred = rtp->lastts + frame->samples;
+
+		/* Re-calculate last TS */
+		rtp->lastts = rtp->lastts + ms * 8;
+		if (ast_tvzero(frame->delivery)) {
+			/* If this isn't an absolute delivery time, Check if it is close to our prediction,
+			   and if so, go with our prediction */
+			if (abs(rtp->lastts - pred) < MAX_TIMESTAMP_SKEW) {
+				rtp->lastts = pred;
+			} else {
+				ast_debug(3, "Difference is %d, ms is %d\n", abs(rtp->lastts - pred), ms);
+				mark = 1;
+			}
+		}
+	} else if (frame->frametype == AST_FRAME_VIDEO) {
+		mark = frame->subclass & 0x1;
+		pred = rtp->lastovidtimestamp + frame->samples;
+		/* Re-calculate last TS */
+		rtp->lastts = rtp->lastts + ms * 90;
+		/* If it's close to our prediction, go for it */
+		if (ast_tvzero(frame->delivery)) {
+			if (abs(rtp->lastts - pred) < 7200) {
+				rtp->lastts = pred;
+				rtp->lastovidtimestamp += frame->samples;
+			} else {
+				ast_debug(3, "Difference is %d, ms is %d (%d), pred/ts/samples %d/%d/%d\n", abs(rtp->lastts - pred), ms, ms * 90, rtp->lastts, pred, frame->samples);
+				rtp->lastovidtimestamp = rtp->lastts;
+			}
+		}
+	} else {
+		pred = rtp->lastotexttimestamp + frame->samples;
+		/* Re-calculate last TS */
+		rtp->lastts = rtp->lastts + ms * 90;
+		/* If it's close to our prediction, go for it */
+		if (ast_tvzero(frame->delivery)) {
+			if (abs(rtp->lastts - pred) < 7200) {
+				rtp->lastts = pred;
+				rtp->lastotexttimestamp += frame->samples;
+			} else {
+				ast_debug(3, "Difference is %d, ms is %d (%d), pred/ts/samples %d/%d/%d\n", abs(rtp->lastts - pred), ms, ms * 90, rtp->lastts, pred, frame->samples);
+				rtp->lastotexttimestamp = rtp->lastts;
+			}
+		}
+	}
+
+	/* If we have been explicitly told to set the marker bit then do so */
+	if (ast_test_flag(rtp, FLAG_NEED_MARKER_BIT)) {
+		mark = 1;
+		ast_clear_flag(rtp, FLAG_NEED_MARKER_BIT);
+	}
+
+	/* If the timestamp for non-digt packets has moved beyond the timestamp for digits, update the digit timestamp */
+	if (rtp->lastts > rtp->lastdigitts) {
+		rtp->lastdigitts = rtp->lastts;
+	}
+
+	if (ast_test_flag(frame, AST_FRFLAG_HAS_TIMING_INFO)) {
+		rtp->lastts = frame->ts * 8;
+	}
+
+	/* If we know the remote address construct a packet and send it out */
+	if (instance->remote_address.sin_port && instance->remote_address.sin_addr.s_addr) {
+		int hdrlen = 12, res;
+		unsigned char *rtpheader = (unsigned char *)(frame->data.ptr - hdrlen);
+
+		put_unaligned_uint32(rtpheader, htonl((2 << 30) | (codec << 16) | (rtp->seqno) | (mark << 23)));
+		put_unaligned_uint32(rtpheader + 4, htonl(rtp->lastts));
+		put_unaligned_uint32(rtpheader + 8, htonl(rtp->ssrc));
+
+		if ((res = sendto(rtp->s, (void *)rtpheader, frame->datalen + hdrlen, 0, (struct sockaddr *)&instance->remote_address, sizeof(instance->remote_address))) < 0) {
+			if (!instance->properties[AST_RTP_PROPERTY_NAT] || (instance->properties[AST_RTP_PROPERTY_NAT] && (ast_test_flag(rtp, FLAG_NAT_ACTIVE) == FLAG_NAT_ACTIVE))) {
+				ast_debug(1, "RTP Transmission error of packet %d to %s:%d: %s\n", rtp->seqno, ast_inet_ntoa(instance->remote_address.sin_addr), ntohs(instance->remote_address.sin_port), strerror(errno));
+			} else if (((ast_test_flag(rtp, FLAG_NAT_ACTIVE) == FLAG_NAT_INACTIVE) || rtpdebug) && !ast_test_flag(rtp, FLAG_NAT_INACTIVE_NOWARN)) {
+				/* Only give this error message once if we are not RTP debugging */
+				if (option_debug || rtpdebug)
+					ast_debug(0, "RTP NAT: Can't write RTP to private address %s:%d, waiting for other end to send audio...\n", ast_inet_ntoa(instance->remote_address.sin_addr), ntohs(instance->remote_address.sin_port));
+				ast_set_flag(rtp, FLAG_NAT_INACTIVE_NOWARN);
+			}
+		} else {
+			rtp->txcount++;
+			rtp->txoctetcount += (res - hdrlen);
+
+			if (rtp->rtcp && rtp->rtcp->schedid < 1) {
+				ast_debug(1, "Starting RTCP transmission on RTP instance '%p'\n", instance);
+				rtp->rtcp->schedid = ast_sched_add(rtp->sched, ast_rtcp_calc_interval(rtp), ast_rtcp_write, rtp);
+			}
+		}
+
+		if (rtp_debug_test_addr(&instance->remote_address)) {
+			ast_verbose("Sent RTP packet to      %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
+				    ast_inet_ntoa(instance->remote_address.sin_addr), ntohs(instance->remote_address.sin_port), codec, rtp->seqno, rtp->lastts, res - hdrlen);
+		}
+	}
+
+	rtp->seqno++;
+
+	return 0;
+}
+
+static struct ast_frame *red_t140_to_red(struct rtp_red *red) {
+	unsigned char *data = red->t140red.data.ptr;
+	int len = 0;
+	int i;
+
+	/* replace most aged generation */
+	if (red->len[0]) {
+		for (i = 1; i < red->num_gen+1; i++)
+			len += red->len[i];
+
+		memmove(&data[red->hdrlen], &data[red->hdrlen+red->len[0]], len);
+	}
+
+	/* Store length of each generation and primary data length*/
+	for (i = 0; i < red->num_gen; i++)
+		red->len[i] = red->len[i+1];
+	red->len[i] = red->t140.datalen;
+
+	/* write each generation length in red header */
+	len = red->hdrlen;
+	for (i = 0; i < red->num_gen; i++)
+		len += data[i*4+3] = red->len[i];
+
+	/* add primary data to buffer */
+	memcpy(&data[len], red->t140.data.ptr, red->t140.datalen);
+	red->t140red.datalen = len + red->t140.datalen;
+
+	/* no primary data and no generations to send */
+	if (len == red->hdrlen && !red->t140.datalen)
+		return NULL;
+
+	/* reset t.140 buffer */
+	red->t140.datalen = 0;
+
+	return &red->t140red;
+}
+
+static int ast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
+{
+	struct ast_rtp *rtp = instance->data;
+	int codec, subclass;
+
+	/* If we don't actually know the remote address don't even bother doing anything */
+	if (!instance->remote_address.sin_addr.s_addr) {
+		ast_debug(1, "No remote address on RTP instance '%p' so dropping frame\n", instance);
+		return -1;
+	}
+

[... 1439 lines stripped ...]



More information about the asterisk-commits mailing list