[svn-commits] tzafrir: branch tzafrir/monitor-rtp r214061 - /team/tzafrir/monitor-rtp/formats/

SVN commits to the Digium repositories svn-commits at lists.digium.com
Tue Aug 25 13:33:09 CDT 2009


Author: tzafrir
Date: Tue Aug 25 13:33:06 2009
New Revision: 214061

URL: http://svn.asterisk.org/svn-view/asterisk?view=rev&rev=214061
Log:
format_rtp: a "file format" to dump an RTP stream

Add a new format module to dump frames into a simple "context-less" RTP
stream.

Unlike the existing RTP APIs, we don't want to assume that the RTP
stream is part of some VoIP session, and would like to avoid any setup.
The remote party here is completely passive and will not respond to a
single packet we send.

Added:
    team/tzafrir/monitor-rtp/formats/format_rtp.c   (with props)

Added: team/tzafrir/monitor-rtp/formats/format_rtp.c
URL: http://svn.asterisk.org/svn-view/asterisk/team/tzafrir/monitor-rtp/formats/format_rtp.c?view=auto&rev=214061
==============================================================================
--- team/tzafrir/monitor-rtp/formats/format_rtp.c (added)
+++ team/tzafrir/monitor-rtp/formats/format_rtp.c Tue Aug 25 13:33:06 2009
@@ -1,0 +1,170 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2009, Xorcom
+ *
+ * Written by Oron Peled <oron at actcom.co.il>
+ *
+ * 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 Convert to RTP package payload
+ * \ingroup formats
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include <unistd.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <stdlib.h>
+#include <sys/time.h>
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+
+#include "asterisk/mod_format.h"
+#include "asterisk/module.h"
+#include "asterisk/rtp.h"
+#include "asterisk/monitor.h"
+
+#define	SLIN_SAMPLES	160
+#define BUF_SIZE	(SLIN_SAMPLES*2)		/* 320 bytes, 160 samples */
+
+/*
+ * Grabbed from DAHDI sources
+ */
+static unsigned char lineartoulaw(short sample)
+{
+  static int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
+                             4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
+                             5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
+                             5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
+                             6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
+                             6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
+                             6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
+                             6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
+                             7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
+                             7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
+                             7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
+                             7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
+                             7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
+                             7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
+                             7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
+                             7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
+  int sign, exponent, mantissa;
+  unsigned char ulawbyte;
+  const int CLIP = 32635;
+  const int BIAS = 0x84;   /* define the add-in bias for 16 bit samples */
+
+  /* Get the sample into sign-magnitude. */
+  sign = (sample >> 8) & 0x80;          /* set aside the sign */
+  if (sign != 0) sample = -sample;              /* get magnitude */
+  if (sample > CLIP) sample = CLIP;             /* clip the magnitude */
+
+  /* Convert from 16 bit linear to ulaw. */
+  sample = sample + BIAS;
+  exponent = exp_lut[(sample >> 7) & 0xFF];
+  mantissa = (sample >> (exponent + 3)) & 0x0F;
+  ulawbyte = ~(sign | (exponent << 4) | mantissa);
+  if (ulawbyte == 0) ulawbyte = 0x02;   /* optional CCITT trap */
+  if (ulawbyte == 0xff) ulawbyte = 0x7f;   /* never return 0xff */
+  return(ulawbyte);
+}
+
+static void rtp_convert_toulaw(unsigned char *dst, const int16_t *src, int samples)
+{
+	int	i;
+
+	for(i = 0; i < samples; i++)
+		dst[i] = lineartoulaw(src[i]);
+}
+
+static int rtp_write(struct ast_filestream *fs, struct ast_frame *f)
+{
+	int res;
+	static int rate_limit;
+	int fd;
+	uint32_t buf[BUFSIZ];
+	uint8_t codec;
+	struct monitor_rtp_state *rtp_state;
+	uint32_t tdiff;
+
+	if (f->frametype != AST_FRAME_VOICE) {
+		ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
+		return -1;
+	}
+	if (f->subclass != AST_FORMAT_SLINEAR) {
+		ast_log(LOG_WARNING, "Asked to write non-slinear frame (%d)!\n", f->subclass);
+		return -1;
+	}
+	rtp_state = (struct monitor_rtp_state *)fs->filename;
+	if(!rtp_state) {
+		ast_log(LOG_WARNING, "No RTP state information for filestream\n");
+		return -1;
+	}
+	fd = fileno(fs->f);
+	/*
+	 * Build RTP header
+	 */
+	codec = 0;	/* AST_FORMAT_ULAW */
+	tdiff = (long)ast_tvdiff_ms(ast_tvnow(), rtp_state->ts_start);
+	buf[0] = htonl((2 << 30) | (codec << 16) | (rtp_state->seqno) | (0 << 23));
+	buf[1] = htonl(tdiff * 8);
+	buf[2] = htonl(rtp_state->ssrc);
+	//memcpy(&buf[3], f->data, f->datalen);
+	rtp_convert_toulaw((unsigned char*)&buf[3], f->data.ptr, f->datalen / 2);
+	if((res = sendto(fd, buf, 4*3 + f->datalen / 2, 0, &rtp_state->dest, sizeof(rtp_state->dest))) < 0) {
+		ast_log(LOG_WARNING, "Bad sendto #%d (%d bytes): %s\n", rtp_state->seqno, f->datalen, strerror(errno));
+		return -1;
+	}
+	rtp_state->seqno++;
+	if((rate_limit++ % 1000) == 0)
+		ast_log(LOG_NOTICE, "%s (fd=%d, datalen=%d res=%d)\n", __FUNCTION__, fd, f->datalen, res);
+	return 0;
+}
+
+static int rtp_open(struct ast_filestream *s)
+{
+	ast_log(LOG_NOTICE, "%s called\n", __FUNCTION__);
+	return 0;
+}
+
+static int rtp_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
+{
+	return 0;
+}
+
+static const struct ast_format rtp_f = {
+	.name = "rtpmonitor",
+	.exts = "rtpmonitor",
+	.format = AST_FORMAT_SLINEAR,
+	.open = rtp_open,
+	.write = rtp_write,
+	.seek = rtp_seek,
+	.buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
+};
+
+static int load_module(void)
+{
+	return ast_format_register(&rtp_f);
+}
+
+static int unload_module(void)
+{
+	return ast_format_unregister(rtp_f.name);
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "PCM Convertion to RTP payload");

Propchange: team/tzafrir/monitor-rtp/formats/format_rtp.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/tzafrir/monitor-rtp/formats/format_rtp.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/tzafrir/monitor-rtp/formats/format_rtp.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain




More information about the svn-commits mailing list