FreeCalypso > hg > gsm-codec-lib
comparison pcap/rtp-jitter-view.c @ 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 | pcap/rtp-cont-check.c@75607bc26f57 |
children | 10f11a2d4042 |
comparison
equal
deleted
inserted
replaced
171:75607bc26f57 | 172:693a0958a303 |
---|---|
1 /* | |
2 * This program reads a pcap file, extracts packets belonging to a | |
3 * particular RTP stream as identified by a source or destination | |
4 * IP:port, checks its continuity at the packet header level | |
5 * (verifies that the sequence number always increments by 1 and | |
6 * that the timestamp always increments by 160 units) and prints out | |
7 * the Rx time delta of each stream packet (the capture time of | |
8 * the current packet minus the capture time of the previous packet) | |
9 * on stdout, allowing visual analysis of timing jitter. | |
10 * | |
11 * The codec can be anything, and this program can also be used to | |
12 * examine the jitter of RTP streams coming from PSTN/SIP, but the | |
13 * core assumption is that packets must arrive every 20 ms, with | |
14 * the timestamp field incrementing by 160 units each time. | |
15 */ | |
16 | |
17 #include <sys/types.h> | |
18 #include <sys/socket.h> | |
19 #include <sys/time.h> | |
20 #include <netinet/in.h> | |
21 #include <arpa/inet.h> | |
22 #include <stdio.h> | |
23 #include <stdlib.h> | |
24 #include <string.h> | |
25 #include <strings.h> | |
26 #include <pcap/pcap.h> | |
27 | |
28 static pcap_t *pcap; | |
29 static in_addr_t match_ip_addr; | |
30 static u_short match_udp_port; | |
31 static unsigned iphdr_addr_offset, udphdr_port_offset; | |
32 static unsigned link_hdr_len, ethertype_offset; | |
33 static int stream_init_flag; | |
34 static unsigned last_seq, last_tstamp, stream_ssrc; | |
35 static struct timeval last_pkt_time; | |
36 | |
37 static void | |
38 check_dl_type() | |
39 { | |
40 int dltype; | |
41 | |
42 dltype = pcap_datalink(pcap); | |
43 switch (dltype) { | |
44 case DLT_EN10MB: | |
45 link_hdr_len = 14; | |
46 ethertype_offset = 12; | |
47 break; | |
48 case DLT_RAW: | |
49 link_hdr_len = 0; | |
50 break; | |
51 case DLT_LINUX_SLL: | |
52 link_hdr_len = 16; | |
53 ethertype_offset = 14; | |
54 break; | |
55 default: | |
56 fprintf(stderr, "error: unsupported data link type %d\n", | |
57 dltype); | |
58 exit(1); | |
59 } | |
60 } | |
61 | |
62 static void | |
63 rtp_stream_logic(rtp_hdr, pkt_idx, pkt_time) | |
64 u_char *rtp_hdr; | |
65 unsigned pkt_idx; | |
66 struct timeval *pkt_time; | |
67 { | |
68 unsigned cur_seq, cur_tstamp, cur_ssrc; | |
69 struct timeval deltat; | |
70 | |
71 cur_seq = (rtp_hdr[2] << 8) | rtp_hdr[3]; | |
72 cur_tstamp = (rtp_hdr[4] << 24) | (rtp_hdr[5] << 16) | | |
73 (rtp_hdr[6] << 8) | rtp_hdr[7]; | |
74 cur_ssrc = (rtp_hdr[8] << 24) | (rtp_hdr[9] << 16) | | |
75 (rtp_hdr[10] << 8) | rtp_hdr[11]; | |
76 if (stream_init_flag) { | |
77 if (cur_ssrc != stream_ssrc) { | |
78 fprintf(stderr, | |
79 "error in packet #%u: SSRC change from 0x%08X to 0x%08X\n", | |
80 pkt_idx, stream_ssrc, cur_ssrc); | |
81 exit(1); | |
82 } | |
83 if (cur_seq != last_seq + 1) { | |
84 fprintf(stderr, | |
85 "error in packet #%u: seq break from 0x%04X to 0x%04X\n", | |
86 pkt_idx, last_seq, cur_seq); | |
87 exit(1); | |
88 } | |
89 if (cur_tstamp != last_tstamp + 160) { | |
90 fprintf(stderr, | |
91 "error in packet #%u: timestamp break from 0x%08X to 0x%08X\n", | |
92 pkt_idx, last_tstamp, cur_tstamp); | |
93 exit(1); | |
94 } | |
95 if (timercmp(pkt_time, &last_pkt_time, <)) { | |
96 fprintf(stderr, | |
97 "packet #%u timing error: Rx time goes backward\n", | |
98 pkt_idx); | |
99 exit(1); | |
100 } | |
101 timersub(pkt_time, &last_pkt_time, &deltat); | |
102 if (deltat.tv_sec) { | |
103 fprintf(stderr, | |
104 "packet #%u timing error: Rx time gap >= 1 s\n", | |
105 pkt_idx); | |
106 exit(1); | |
107 } | |
108 printf("Packet #%u: time delta %u us\n", pkt_idx, | |
109 (unsigned) deltat.tv_usec); | |
110 } else { | |
111 stream_init_flag = 1; | |
112 stream_ssrc = cur_ssrc; | |
113 } | |
114 last_seq = cur_seq; | |
115 last_tstamp = cur_tstamp; | |
116 bcopy(pkt_time, &last_pkt_time, sizeof(struct timeval)); | |
117 } | |
118 | |
119 static void | |
120 process_packet(pkt, caplen, pkt_idx, pkt_time) | |
121 u_char *pkt; | |
122 unsigned caplen, pkt_idx; | |
123 struct timeval *pkt_time; | |
124 { | |
125 unsigned udplen; | |
126 | |
127 if (caplen < link_hdr_len + 28) | |
128 return; | |
129 if (link_hdr_len) { | |
130 if (pkt[ethertype_offset] != 0x08) | |
131 return; | |
132 if (pkt[ethertype_offset+1] != 0x00) | |
133 return; | |
134 pkt += link_hdr_len; | |
135 caplen -= link_hdr_len; | |
136 } | |
137 /* check IP header */ | |
138 if (pkt[0] != 0x45) | |
139 return; | |
140 if (pkt[9] != 17) /* UDP */ | |
141 return; | |
142 if (bcmp(pkt + iphdr_addr_offset, &match_ip_addr, 4)) | |
143 return; | |
144 /* check UDP header */ | |
145 if (bcmp(pkt + 20 + udphdr_port_offset, &match_udp_port, 2)) | |
146 return; | |
147 /* it is our target - now scrutinize it */ | |
148 udplen = (pkt[24] << 8) | pkt[25]; | |
149 if (caplen < udplen + 20) { | |
150 fprintf(stderr, | |
151 "error: packet #%u is truncated in the capture\n", | |
152 pkt_idx); | |
153 exit(1); | |
154 } | |
155 if (udplen < 20) { | |
156 fprintf(stderr, | |
157 "error in packet #%u: UDP length is too short for RTP header\n", | |
158 pkt_idx); | |
159 exit(1); | |
160 } | |
161 if (pkt[28] != 0x80) { | |
162 fprintf(stderr, | |
163 "error in packet #%u: unsupported RTP header structure\n", | |
164 pkt_idx); | |
165 exit(1); | |
166 } | |
167 rtp_stream_logic(pkt + 28, pkt_idx, pkt_time); | |
168 } | |
169 | |
170 main(argc, argv) | |
171 char **argv; | |
172 { | |
173 char errbuf[PCAP_ERRBUF_SIZE]; | |
174 u_char *pkt; | |
175 struct pcap_pkthdr pkthdr; | |
176 unsigned pkt_idx; | |
177 | |
178 if (argc != 5) { | |
179 fprintf(stderr, | |
180 "usage: %s pcap-file src|dest ip-addr udp-port\n", | |
181 argv[0]); | |
182 exit(1); | |
183 } | |
184 pcap = pcap_open_offline(argv[1], errbuf); | |
185 if (!pcap) { | |
186 fprintf(stderr, "%s: %s\n", argv[1], errbuf); | |
187 exit(1); | |
188 } | |
189 check_dl_type(); | |
190 if (!strcmp(argv[2], "src")) { | |
191 iphdr_addr_offset = 12; | |
192 udphdr_port_offset = 0; | |
193 } else if (!strcmp(argv[2], "dest")) { | |
194 iphdr_addr_offset = 16; | |
195 udphdr_port_offset = 2; | |
196 } else { | |
197 fprintf(stderr, | |
198 "error: direction argument must be \"src\" or \"dest\"\n"); | |
199 exit(1); | |
200 } | |
201 match_ip_addr = inet_addr(argv[3]); | |
202 if (match_ip_addr == INADDR_NONE) { | |
203 fprintf(stderr, "error: IP address argument is invalid\n"); | |
204 exit(1); | |
205 } | |
206 match_udp_port = htons(strtoul(argv[4], 0, 0)); | |
207 for (pkt_idx = 0; ; pkt_idx++) { | |
208 pkt = pcap_next(pcap, &pkthdr); | |
209 if (!pkt) | |
210 break; | |
211 process_packet(pkt, (unsigned) pkthdr.caplen, pkt_idx, | |
212 &pkthdr.ts); | |
213 } | |
214 if (!stream_init_flag) { | |
215 fprintf(stderr, "error: specified RTP stream not found\n"); | |
216 exit(1); | |
217 } | |
218 exit(0); | |
219 } |