comparison sip-manual-out/rtp.c @ 188:6aecee01cf0a

sip-manual-out: add RTP stream continuity analysis
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 17 Mar 2023 01:14:57 -0800
parents 258932879f8b
children 1266e024de6c
comparison
equal deleted inserted replaced
187:258932879f8b 188:6aecee01cf0a
6 6
7 #include <sys/types.h> 7 #include <sys/types.h>
8 #include <sys/socket.h> 8 #include <sys/socket.h>
9 #include <netinet/in.h> 9 #include <netinet/in.h>
10 #include <stdio.h> 10 #include <stdio.h>
11 #include <stdint.h>
11 #include <stdlib.h> 12 #include <stdlib.h>
12 #include <string.h> 13 #include <string.h>
13 #include <strings.h> 14 #include <strings.h>
14 #include "../include/tmgw_const.h" 15 #include "../include/tmgw_const.h"
16 #include "../include/rtp_defs.h"
15 #include "../librtpalloc/rtp_alloc_simple.h" 17 #include "../librtpalloc/rtp_alloc_simple.h"
16 18
17 struct sockaddr_in rtp_local_addr; 19 struct sockaddr_in rtp_local_addr;
18 int rtp_udp_fd, rtcp_udp_fd; 20 int rtp_udp_fd, rtcp_udp_fd;
19 21
20 static int got_some_rtp, got_some_rtcp; 22 static int rtp_start_flag, rtp_bad_flag, rtp_ssrc_chg_flag;
23 static int rtp_seq_brk_flag, rtp_seq_zero_flag, rtp_seq_neg_flag;
24 static int rtp_ts_brk_flag;
25 static uint32_t rtp_ssrc;
26 static uint32_t rtp_last_ts;
27 static uint16_t rtp_last_seq;
28 static int got_some_rtcp;
21 29
22 void 30 void
23 obtain_rtp_endp() 31 obtain_rtp_endp()
24 { 32 {
25 int rc; 33 int rc;
34 } 42 }
35 43
36 void 44 void
37 rtp_rx_select() 45 rtp_rx_select()
38 { 46 {
39 u_char buf[512]; 47 struct rtp_packet pkt;
48 struct sockaddr_in sin_from;
49 socklen_t addrlen;
50 int16_t seq_delta;
51 int32_t ts_delta;
52 int rc;
40 53
41 recv(rtp_udp_fd, buf, sizeof buf, 0); 54 addrlen = sizeof(struct sockaddr_in);
42 if (!got_some_rtp) { 55 rc = recvfrom(rtp_udp_fd, &pkt, sizeof pkt, 0,
43 printf("Got some RTP\n"); 56 (struct sockaddr *) &sin_from, &addrlen);
44 got_some_rtp = 1; 57 if (rc < 0)
58 return;
59 if (rc != RTP_PACKET_SIZE_PSTN) {
60 bad_rtp_pkt: if (!rtp_bad_flag) {
61 printf("Got a bad RTP packet\n");
62 rtp_bad_flag = 1;
63 }
64 return;
65 }
66 if (pkt.v_p_x_cc != 0x80)
67 goto bad_rtp_pkt;
68 switch (pkt.m_pt & 0x7F) {
69 case PSTN_CODEC_PCMU:
70 case PSTN_CODEC_PCMA:
71 break;
72 default:
73 goto bad_rtp_pkt;
74 }
75 if (rtp_start_flag && pkt.ssrc != rtp_ssrc) {
76 if (!rtp_ssrc_chg_flag) {
77 printf("Rx RTP stream changed SSRC\n");
78 rtp_ssrc_chg_flag = 1;
79 }
80 } else if (rtp_start_flag) {
81 seq_delta = ntohs(pkt.seq) - rtp_last_seq;
82 ts_delta = ntohl(pkt.tstamp) - rtp_last_ts;
83 if (seq_delta == 0) {
84 if (!rtp_seq_zero_flag) {
85 printf("Rx RTP seq zero increment\n");
86 rtp_seq_zero_flag = 1;
87 }
88 return;
89 }
90 if (seq_delta < 0) {
91 if (!rtp_seq_neg_flag) {
92 printf("Rx RTP seq negative increment\n");
93 rtp_seq_neg_flag = 1;
94 }
95 return;
96 }
97 if (seq_delta != 1) {
98 if (!rtp_seq_brk_flag) {
99 printf("Rx RTP stream seq break\n");
100 rtp_seq_brk_flag = 1;
101 }
102 } else if (ts_delta != 160) {
103 if (!rtp_ts_brk_flag) {
104 printf("Rx RTP stream tstamp break\n");
105 rtp_ts_brk_flag = 1;
106 }
107 }
108 }
109 rtp_ssrc = pkt.ssrc;
110 rtp_last_ts = ntohl(pkt.tstamp);
111 rtp_last_seq = ntohs(pkt.seq);
112 if (!rtp_start_flag) {
113 printf("Rx RTP stream begins with seq=%u ts=%u\n",
114 rtp_last_seq, rtp_last_ts);
115 rtp_start_flag = 1;
45 } 116 }
46 } 117 }
47 118
48 void 119 void
49 rtcp_rx_select() 120 rtcp_rx_select()