changeset 172:693a0958a303

yet another refactoring of RTP tools: the program that prints each time delta is now rtp-jitter-view, whereas rtp-cont-check now reports min and max instead.
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 26 Dec 2022 22:42:41 +0000
parents 75607bc26f57
children aaa0380f9958
files .hgignore pcap/Makefile pcap/rtp-cont-check.c pcap/rtp-jitter-view.c
diffstat 4 files changed, 244 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Mon Dec 26 21:35:09 2022 +0000
+++ b/.hgignore	Mon Dec 26 22:42:41 2022 +0000
@@ -42,3 +42,4 @@
 
 ^pcap/rtp-cont-check$
 ^pcap/rtp-gsmfr-extr$
+^pcap/rtp-jitter-view$
--- a/pcap/Makefile	Mon Dec 26 21:35:09 2022 +0000
+++ b/pcap/Makefile	Mon Dec 26 22:42:41 2022 +0000
@@ -1,6 +1,6 @@
 CC=	gcc
 CFLAGS=	-O2
-PROGS=	rtp-cont-check rtp-gsmfr-extr
+PROGS=	rtp-cont-check rtp-gsmfr-extr rtp-jitter-view
 INSTBIN=/opt/freecalypso/bin
 
 all:	${PROGS}
@@ -11,6 +11,9 @@
 rtp-gsmfr-extr:	rtp-gsmfr-extr.c
 	${CC} ${CFLAGS} -o $@ $@.c -lpcap
 
+rtp-jitter-view:	rtp-jitter-view.c
+	${CC} ${CFLAGS} -o $@ $@.c -lpcap
+
 install:
 	mkdir -p ${INSTBIN}
 	install -c ${PROGS} ${INSTBIN}
--- a/pcap/rtp-cont-check.c	Mon Dec 26 21:35:09 2022 +0000
+++ b/pcap/rtp-cont-check.c	Mon Dec 26 22:42:41 2022 +0000
@@ -4,9 +4,8 @@
  * IP:port, and checks its continuity: verifies that the sequence
  * number always increments by 1 and that the timestamp always
  * increments by 160 units.  Finally, this program prints out
- * the Rx time delta of each stream packet (the capture time of
- * the current packet minus the capture time of the previous packet)
- * on stdout, allowing visual timing analysis.
+ * the minimum and maximum observed capture time deltas between
+ * successive packets of the stream.
  *
  * The codec can be anything, and this program can also be used to
  * check continuity of RTP streams coming from PSTN/SIP, but the
@@ -30,9 +29,10 @@
 static u_short match_udp_port;
 static unsigned iphdr_addr_offset, udphdr_port_offset;
 static unsigned link_hdr_len, ethertype_offset;
-static int stream_init_flag;
+static int stream_init_flag, deltat_init_flag;
 static unsigned last_seq, last_tstamp, stream_ssrc;
 static struct timeval last_pkt_time;
+static unsigned deltat_min, deltat_max;
 
 static void
 check_dl_type()
@@ -105,8 +105,16 @@
 				pkt_idx);
 			exit(1);
 		}
-		printf("Packet #%u: time delta %u us\n", pkt_idx,
-			(unsigned) deltat.tv_usec);
+		if (deltat_init_flag) {
+			if (deltat.tv_usec < deltat_min)
+				deltat_min = deltat.tv_usec;
+			if (deltat.tv_usec > deltat_max)
+				deltat_max = deltat.tv_usec;
+		} else {
+			deltat_min = deltat.tv_usec;
+			deltat_max = deltat.tv_usec;
+			deltat_init_flag = 1;
+		}
 	} else {
 		stream_init_flag = 1;
 		stream_ssrc = cur_ssrc;
@@ -215,5 +223,11 @@
 		fprintf(stderr, "error: specified RTP stream not found\n");
 		exit(1);
 	}
+	if (!deltat_init_flag) {
+		fprintf(stderr, "error: found only one matching packet\n");
+		exit(1);
+	}
+	printf("Minimum time delta: %u us\n", deltat_min);
+	printf("Maximum time delta: %u us\n", deltat_max);
 	exit(0);
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pcap/rtp-jitter-view.c	Mon Dec 26 22:42:41 2022 +0000
@@ -0,0 +1,219 @@
+/*
+ * This program reads a pcap file, extracts packets belonging to a
+ * particular RTP stream as identified by a source or destination
+ * IP:port, checks its continuity at the packet header level
+ * (verifies that the sequence number always increments by 1 and
+ * that the timestamp always increments by 160 units) and prints out
+ * the Rx time delta of each stream packet (the capture time of
+ * the current packet minus the capture time of the previous packet)
+ * on stdout, allowing visual analysis of timing jitter.
+ *
+ * The codec can be anything, and this program can also be used to
+ * examine the jitter of RTP streams coming from PSTN/SIP, but the
+ * core assumption is that packets must arrive every 20 ms, with
+ * the timestamp field incrementing by 160 units each time.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <pcap/pcap.h>
+
+static pcap_t *pcap;
+static in_addr_t match_ip_addr;
+static u_short match_udp_port;
+static unsigned iphdr_addr_offset, udphdr_port_offset;
+static unsigned link_hdr_len, ethertype_offset;
+static int stream_init_flag;
+static unsigned last_seq, last_tstamp, stream_ssrc;
+static struct timeval last_pkt_time;
+
+static void
+check_dl_type()
+{
+	int dltype;
+
+	dltype = pcap_datalink(pcap);
+	switch (dltype) {
+	case DLT_EN10MB:
+		link_hdr_len = 14;
+		ethertype_offset = 12;
+		break;
+	case DLT_RAW:
+		link_hdr_len = 0;
+		break;
+	case DLT_LINUX_SLL:
+		link_hdr_len = 16;
+		ethertype_offset = 14;
+		break;
+	default:
+		fprintf(stderr, "error: unsupported data link type %d\n",
+			dltype);
+		exit(1);
+	}
+}
+
+static void
+rtp_stream_logic(rtp_hdr, pkt_idx, pkt_time)
+	u_char *rtp_hdr;
+	unsigned pkt_idx;
+	struct timeval *pkt_time;
+{
+	unsigned cur_seq, cur_tstamp, cur_ssrc;
+	struct timeval deltat;
+
+	cur_seq = (rtp_hdr[2] << 8) | rtp_hdr[3];
+	cur_tstamp = (rtp_hdr[4] << 24) | (rtp_hdr[5] << 16) |
+			(rtp_hdr[6] << 8) | rtp_hdr[7];
+	cur_ssrc = (rtp_hdr[8] << 24) | (rtp_hdr[9] << 16) |
+			(rtp_hdr[10] << 8) | rtp_hdr[11];
+	if (stream_init_flag) {
+		if (cur_ssrc != stream_ssrc) {
+			fprintf(stderr,
+		"error in packet #%u: SSRC change from 0x%08X to 0x%08X\n",
+				pkt_idx, stream_ssrc, cur_ssrc);
+			exit(1);
+		}
+		if (cur_seq != last_seq + 1) {
+			fprintf(stderr,
+		"error in packet #%u: seq break from 0x%04X to 0x%04X\n",
+				pkt_idx, last_seq, cur_seq);
+			exit(1);
+		}
+		if (cur_tstamp != last_tstamp + 160) {
+			fprintf(stderr,
+		"error in packet #%u: timestamp break from 0x%08X to 0x%08X\n",
+				pkt_idx, last_tstamp, cur_tstamp);
+			exit(1);
+		}
+		if (timercmp(pkt_time, &last_pkt_time, <)) {
+			fprintf(stderr,
+			"packet #%u timing error: Rx time goes backward\n",
+				pkt_idx);
+			exit(1);
+		}
+		timersub(pkt_time, &last_pkt_time, &deltat);
+		if (deltat.tv_sec) {
+			fprintf(stderr,
+				"packet #%u timing error: Rx time gap >= 1 s\n",
+				pkt_idx);
+			exit(1);
+		}
+		printf("Packet #%u: time delta %u us\n", pkt_idx,
+			(unsigned) deltat.tv_usec);
+	} else {
+		stream_init_flag = 1;
+		stream_ssrc = cur_ssrc;
+	}
+	last_seq = cur_seq;
+	last_tstamp = cur_tstamp;
+	bcopy(pkt_time, &last_pkt_time, sizeof(struct timeval));
+}
+
+static void
+process_packet(pkt, caplen, pkt_idx, pkt_time)
+	u_char *pkt;
+	unsigned caplen, pkt_idx;
+	struct timeval *pkt_time;
+{
+	unsigned udplen;
+
+	if (caplen < link_hdr_len + 28)
+		return;
+	if (link_hdr_len) {
+		if (pkt[ethertype_offset] != 0x08)
+			return;
+		if (pkt[ethertype_offset+1] != 0x00)
+			return;
+		pkt += link_hdr_len;
+		caplen -= link_hdr_len;
+	}
+	/* check IP header */
+	if (pkt[0] != 0x45)
+		return;
+	if (pkt[9] != 17)	/* UDP */
+		return;
+	if (bcmp(pkt + iphdr_addr_offset, &match_ip_addr, 4))
+		return;
+	/* check UDP header */
+	if (bcmp(pkt + 20 + udphdr_port_offset, &match_udp_port, 2))
+		return;
+	/* it is our target - now scrutinize it */
+	udplen = (pkt[24] << 8) | pkt[25];
+	if (caplen < udplen + 20) {
+		fprintf(stderr,
+			"error: packet #%u is truncated in the capture\n",
+			pkt_idx);
+		exit(1);
+	}
+	if (udplen < 20) {
+		fprintf(stderr,
+		"error in packet #%u: UDP length is too short for RTP header\n",
+			pkt_idx);
+		exit(1);
+	}
+	if (pkt[28] != 0x80) {
+		fprintf(stderr,
+		"error in packet #%u: unsupported RTP header structure\n",
+			pkt_idx);
+		exit(1);
+	}
+	rtp_stream_logic(pkt + 28, pkt_idx, pkt_time);
+}
+
+main(argc, argv)
+	char **argv;
+{
+	char errbuf[PCAP_ERRBUF_SIZE];
+	u_char *pkt;
+	struct pcap_pkthdr pkthdr;
+	unsigned pkt_idx;
+
+	if (argc != 5) {
+		fprintf(stderr,
+			"usage: %s pcap-file src|dest ip-addr udp-port\n",
+			argv[0]);
+		exit(1);
+	}
+	pcap = pcap_open_offline(argv[1], errbuf);
+	if (!pcap) {
+		fprintf(stderr, "%s: %s\n", argv[1], errbuf);
+		exit(1);
+	}
+	check_dl_type();
+	if (!strcmp(argv[2], "src")) {
+		iphdr_addr_offset = 12;
+		udphdr_port_offset = 0;
+	} else if (!strcmp(argv[2], "dest")) {
+		iphdr_addr_offset = 16;
+		udphdr_port_offset = 2;
+	} else {
+		fprintf(stderr,
+		"error: direction argument must be \"src\" or \"dest\"\n");
+		exit(1);
+	}
+	match_ip_addr = inet_addr(argv[3]);
+	if (match_ip_addr == INADDR_NONE) {
+		fprintf(stderr, "error: IP address argument is invalid\n");
+		exit(1);
+	}
+	match_udp_port = htons(strtoul(argv[4], 0, 0));
+	for (pkt_idx = 0; ; pkt_idx++) {
+		pkt = pcap_next(pcap, &pkthdr);
+		if (!pkt)
+			break;
+		process_packet(pkt, (unsigned) pkthdr.caplen, pkt_idx,
+				&pkthdr.ts);
+	}
+	if (!stream_init_flag) {
+		fprintf(stderr, "error: specified RTP stream not found\n");
+		exit(1);
+	}
+	exit(0);
+}