[asterisk-commits] tzafrir: branch tzafrir/monitor-rtp-14 r213894 - /team/tzafrir/monitor-rtp-14...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Aug 24 18:40:12 CDT 2009


Author: tzafrir
Date: Mon Aug 24 18:40:08 2009
New Revision: 213894

URL: http://svn.asterisk.org/svn-view/asterisk?view=rev&rev=213894
Log:
bulk of the rtp monitoring code

At the moment we don't have a simple way to allocate RTP port numbers,
hence the recorded channels are responsible for providing the variable
RTP_PORT_OFFSET . The actual UDP port number used will be:

  base + 2(${RTP_PORT_OFFSET} - 1)

where 'base' defaults to 9000 for the RX side and 11000 for the TX side.

Modified:
    team/tzafrir/monitor-rtp-14/res/res_monitor.c

Modified: team/tzafrir/monitor-rtp-14/res/res_monitor.c
URL: http://svn.asterisk.org/svn-view/asterisk/team/tzafrir/monitor-rtp-14/res/res_monitor.c?view=diff&rev=213894&r1=213893&r2=213894
==============================================================================
--- team/tzafrir/monitor-rtp-14/res/res_monitor.c (original)
+++ team/tzafrir/monitor-rtp-14/res/res_monitor.c Mon Aug 24 18:40:08 2009
@@ -332,6 +332,119 @@
 	return 0;
 }
 
+static int monitor2rtp_stop(struct ast_channel *chan, int need_lock)
+{
+	ast_log(LOG_NOTICE, "STOP monitor2rtp\n");
+	LOCK_IF_NEEDED(chan, need_lock);
+	if ((chan->monitor)) {
+		struct monitor_rtp_state *rtp_state_rx;
+		struct monitor_rtp_state *rtp_state_tx;
+
+		rtp_state_rx = (struct monitor_rtp_state *)chan->monitor->read_stream->filename;
+		rtp_state_tx = (struct monitor_rtp_state *)chan->monitor->write_stream->filename;
+
+		if (chan->monitor->read_stream) {
+			ast_log(LOG_NOTICE, "%s: close read_stream\n", __FUNCTION__);
+			if(rtp_state_rx) {
+				send_cdr(chan, 0, 1, rtp_state_rx);
+				chan->monitor->read_stream->filename = NULL;
+				ast_free(rtp_state_rx);
+			}
+			ast_closestream(chan->monitor->read_stream);
+		}
+		if (chan->monitor->write_stream) {
+			ast_log(LOG_NOTICE, "%s: close write_stream\n", __FUNCTION__);
+			if(rtp_state_tx) {
+				send_cdr(chan, 0, 0, rtp_state_tx);
+				chan->monitor->write_stream->filename = NULL;
+				ast_free(rtp_state_tx);
+			}
+			ast_closestream(chan->monitor->write_stream);
+		}
+	}
+	UNLOCK_IF_NEEDED(chan, need_lock);
+	return 0;
+}
+
+static void calc_ssrc(struct ast_channel *chan, uint32_t *rx_ssrc, uint32_t *tx_ssrc)
+{
+	float	val;
+
+	if(chan->uniqueid && sscanf(chan->uniqueid, "%f", &val) == 1) {
+		time_t		t;
+		int		a;
+		uint32_t	ssrc;
+
+		t = (time_t)val;
+		a = (val - t) * 10;
+		ast_log(LOG_DEBUG, "%s: val=%f t=%lu a=%d\n", __FUNCTION__, val, (unsigned long)t, a);
+		ssrc = (t << 8) | ((a << 4) & 0xFF);
+		ast_log(LOG_DEBUG, "%s: ssrc=%u\n", __FUNCTION__, ssrc);
+		*rx_ssrc = ssrc;
+		*tx_ssrc = ssrc | 0x1;
+		return;
+	}
+	ast_log(LOG_WARNING,
+		"%s: Failed to parse uniqueid='%s' using random SSRC\n",
+		__FUNCTION__, chan->uniqueid);
+	*rx_ssrc = ast_random();
+	*tx_ssrc = ast_random();
+}
+
+static int monitor2rtp_start(struct ast_channel *chan, int need_lock)
+{
+	struct ast_channel_monitor *monitor;
+	struct monitor_rtp_state *rtp_state_rx;
+	struct monitor_rtp_state *rtp_state_tx;
+	const char	*port_offset;
+	uint32_t	rx_ssrc;
+	uint32_t	tx_ssrc;
+	int		portnum;
+	int		ret = -1;
+
+	ast_log(LOG_NOTICE, "START monitor2rtp\n");
+	LOCK_IF_NEEDED(chan, need_lock);
+	port_offset = pbx_builtin_getvar_helper(chan, "RTP_PORT_OFFSET");
+	portnum = atoi(port_offset);
+	if(portnum <= 0) {
+		ast_log(LOG_WARNING, "Bad RTP_PORT_OFFSET='%s'\n", port_offset);
+		goto out;
+	}
+	if (!(monitor = ast_calloc(1, sizeof(*monitor)))) {
+		goto out;
+	}
+	calc_ssrc(chan, &rx_ssrc, &tx_ssrc);
+	ast_log(LOG_NOTICE, "%s: rx_ssrc=%u tx_ssrc=%u\n", __FUNCTION__, rx_ssrc, tx_ssrc);
+	/* RTP uses even port numbers */
+	portnum = (portnum - 1) * 2;
+	/* RX port */
+	monitor->read_stream = rtp_stream(chan, rtp_server_name, rtp_portbase_rx + portnum, rx_ssrc);
+	if (!monitor->read_stream) {
+		ast_log(LOG_WARNING, "Could not create UDP stream for read\n");
+		goto out;
+	}
+	/* TX port */
+	monitor->write_stream = rtp_stream(chan, rtp_server_name, rtp_portbase_tx + portnum, tx_ssrc);
+	if (!monitor->write_stream) {
+		ast_log(LOG_WARNING, "Could not create UDP stream for write\n");
+		ast_closestream(monitor->read_stream);
+		goto out;
+	}
+	rtp_state_rx = (struct monitor_rtp_state *)(monitor->read_stream->filename);
+	rtp_state_tx = (struct monitor_rtp_state *)(monitor->write_stream->filename);
+	send_cdr(chan, 1, 1, rtp_state_rx);
+	send_cdr(chan, 1, 0, rtp_state_tx);
+	monitor->stop = monitor2rtp_stop;
+	chan->monitor = monitor;
+	ast_monitor_set_state(chan, AST_MONITOR_RUNNING);
+	/* so we know this call has been monitored in case we need to bill for it or something */
+	pbx_builtin_setvar_helper(chan, "__MONITORED","true");
+	ret = 0;
+out:
+	UNLOCK_IF_NEEDED(chan, need_lock);
+	return 0;
+}
+
 /* Start monitoring a channel */
 int ast_monitor_start(	struct ast_channel *chan, const char *format_spec,
 		const char *fname_base, int need_lock)




More information about the asterisk-commits mailing list