comparison test-fsk/rtp_rx.c @ 2:26383ed8b79f

test-fsk: starting as a copy of test-voice
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 04 Mar 2024 21:03:19 -0800
parents test-voice/rtp_rx.c@35c0d9f03c0a
children cc997f9ae186
comparison
equal deleted inserted replaced
1:38c4d09882f6 2:26383ed8b79f
1 /*
2 * In this module we implement our RTP handling: obtaining a PSTN-side
3 * RTP endpoint from themwi-rtp-mgr, then handling read select on RTP
4 * and RTCP UDP sockets.
5 */
6
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #include <stdio.h>
11 #include <stdint.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <strings.h>
15 #include "../include/tmgw_const.h"
16 #include "../include/rtp_defs.h"
17 #include "../librtpalloc/rtp_alloc_simple.h"
18
19 struct sockaddr_in rtp_local_addr;
20 int rtp_udp_fd, rtcp_udp_fd;
21
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;
29
30 static const uint8_t hdr_pattern[20] = {0, 1, 0, 1, 0, 1, 1, 0, 1, 0,
31 0, 1, 1, 0, 1, 0, 1, 0, 0, 1};
32
33 static uint8_t is_hunt_buf[320];
34 static int is_state;
35 static unsigned is_offset, is_bit_count, is_hunt_fill;
36 static uint32_t is_rx_word;
37
38 static void
39 reset_is_hunt()
40 {
41 is_state = 0;
42 is_hunt_fill = 0;
43 }
44
45 static void
46 is_rx_hunt(input_pos)
47 unsigned input_pos;
48 {
49 unsigned offset, n;
50
51 for (offset = 0; offset < 16; offset++) {
52 for (n = 0; n < 20; n++)
53 if ((is_hunt_buf[offset + n*16] & 1) != hdr_pattern[n])
54 break;
55 if (n == 20)
56 break;
57 }
58 if (n != 20)
59 return;
60 printf("Found IS_Header, last bit offset %u\n",
61 input_pos * 16 + offset);
62 is_offset = offset;
63 is_state = 1;
64 is_bit_count = 0;
65 is_rx_word = 0;
66 is_hunt_fill = 0;
67 }
68
69 static void
70 is_process_cmd()
71 {
72 int cont;
73
74 printf("IS_Command: 0x%03X", is_rx_word);
75 switch (is_rx_word) {
76 case 0x05D:
77 printf(" (REQ)\n");
78 cont = 1;
79 break;
80 case 0x0BA:
81 printf(" (ACK)\n");
82 cont = 1;
83 break;
84 case 0x0E7:
85 printf(" (IPE)\n");
86 cont = 1;
87 break;
88 case 0x129:
89 printf(" (FILL)\n");
90 cont = 0;
91 break;
92 case 0x174:
93 printf(" (DUP)\n");
94 cont = 0;
95 break;
96 case 0x193:
97 printf(" (SYL)\n");
98 cont = 0;
99 break;
100 default:
101 printf(" (bad)\n");
102 cont = 0;
103 }
104 if (cont) {
105 is_state = 2;
106 is_bit_count = 0;
107 is_rx_word = 0;
108 } else
109 is_state = 0;
110 }
111
112 static void
113 is_process_ext()
114 {
115 printf("IS_Extension: 0x%05X", is_rx_word);
116 if (is_rx_word & 0x80200) {
117 printf(" (bad sync)\n");
118 is_state = 0;
119 return;
120 }
121 switch (is_rx_word & 3) {
122 case 0:
123 printf(" (final)\n");
124 is_state = 0;
125 return;
126 case 3:
127 printf(" (continue)\n");
128 is_state = 2;
129 is_bit_count = 0;
130 is_rx_word = 0;
131 return;
132 default:
133 printf(" (bad EX)\n");
134 is_state = 0;
135 }
136 }
137
138 static void
139 is_rx_process(input, input_pos)
140 uint8_t *input;
141 unsigned input_pos;
142 {
143 unsigned new_bit;
144
145 memmove(is_hunt_buf, is_hunt_buf + 16, 304);
146 memcpy(is_hunt_buf + 304, input, 16);
147 if (!is_state) {
148 if (is_hunt_fill < 20)
149 is_hunt_fill++;
150 if (is_hunt_fill == 20)
151 is_rx_hunt(input_pos);
152 return;
153 }
154 new_bit = input[is_offset] & 1;
155 is_rx_word <<= 1;
156 is_rx_word |= new_bit;
157 is_bit_count++;
158 if (is_state == 1 && is_bit_count == 10)
159 is_process_cmd();
160 else if (is_state == 2 && is_bit_count == 20)
161 is_process_ext();
162 }
163
164 void
165 obtain_rtp_endp()
166 {
167 int rc;
168 struct rtp_alloc_simple res;
169
170 rc = rtp_alloc_simple(TMGW_EP_TYPE_PSTN_ONLY, &res);
171 if (rc < 0)
172 exit(1); /* error msg already printed */
173 bcopy(&res.pstn_addr, &rtp_local_addr, sizeof(struct sockaddr_in));
174 rtp_udp_fd = res.pstn_rtp_fd;
175 rtcp_udp_fd = res.pstn_rtcp_fd;
176 reset_is_hunt();
177 }
178
179 void
180 rtp_rx_select()
181 {
182 struct rtp_packet pkt;
183 struct sockaddr_in sin_from;
184 socklen_t addrlen;
185 int16_t seq_delta;
186 int32_t ts_delta;
187 int rc;
188 unsigned is_chunk;
189
190 addrlen = sizeof(struct sockaddr_in);
191 rc = recvfrom(rtp_udp_fd, &pkt, sizeof pkt, 0,
192 (struct sockaddr *) &sin_from, &addrlen);
193 if (rc < 0)
194 return;
195 if (rc != RTP_PACKET_SIZE_PSTN) {
196 bad_rtp_pkt: if (!rtp_bad_flag) {
197 printf("Got a bad RTP packet\n");
198 rtp_bad_flag = 1;
199 }
200 return;
201 }
202 if (pkt.v_p_x_cc != 0x80)
203 goto bad_rtp_pkt;
204 switch (pkt.m_pt & 0x7F) {
205 case PSTN_CODEC_PCMU:
206 case PSTN_CODEC_PCMA:
207 break;
208 default:
209 goto bad_rtp_pkt;
210 }
211 if (rtp_start_flag && pkt.ssrc != rtp_ssrc) {
212 if (!rtp_ssrc_chg_flag) {
213 printf("Rx RTP stream changed SSRC\n");
214 rtp_ssrc_chg_flag = 1;
215 }
216 reset_is_hunt();
217 } else if (rtp_start_flag) {
218 seq_delta = ntohs(pkt.seq) - rtp_last_seq;
219 ts_delta = ntohl(pkt.tstamp) - rtp_last_ts;
220 if (seq_delta == 0) {
221 if (!rtp_seq_zero_flag) {
222 printf("Rx RTP seq zero increment\n");
223 rtp_seq_zero_flag = 1;
224 }
225 return;
226 }
227 if (seq_delta < 0) {
228 if (!rtp_seq_neg_flag) {
229 printf("Rx RTP seq negative increment\n");
230 rtp_seq_neg_flag = 1;
231 }
232 return;
233 }
234 if (seq_delta != 1) {
235 if (!rtp_seq_brk_flag) {
236 printf("Rx RTP stream seq break\n");
237 rtp_seq_brk_flag = 1;
238 }
239 reset_is_hunt();
240 } else if (ts_delta != 160) {
241 if (!rtp_ts_brk_flag) {
242 printf("Rx RTP stream tstamp break\n");
243 rtp_ts_brk_flag = 1;
244 }
245 reset_is_hunt();
246 }
247 }
248 rtp_ssrc = pkt.ssrc;
249 rtp_last_ts = ntohl(pkt.tstamp);
250 rtp_last_seq = ntohs(pkt.seq);
251 if (!rtp_start_flag) {
252 printf("Rx RTP stream begins with seq=%u ts=%u\n",
253 rtp_last_seq, rtp_last_ts);
254 rtp_start_flag = 1;
255 }
256 for (is_chunk = 0; is_chunk < 10; is_chunk++)
257 is_rx_process(pkt.payload + is_chunk * 16, is_chunk);
258 }
259
260 void
261 rtcp_rx_select()
262 {
263 u_char buf[512];
264
265 recv(rtcp_udp_fd, buf, sizeof buf, 0);
266 if (!got_some_rtcp) {
267 printf("Got some RTCP\n");
268 got_some_rtcp = 1;
269 }
270 }