comparison sip-manual-out/rtp_rx.c @ 192:f8a33603288f

sip-manual-out: generate outgoing RTP stream with PCM silence
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 17 Mar 2023 13:45:31 -0800
parents sip-manual-out/rtp.c@62ecc0aa081f
children 834656633fa0
comparison
equal deleted inserted replaced
191:6ac96217c442 192:f8a33603288f
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;
36 static uint32_t is_rx_word;
37
38 static void
39 reset_is_hunt()
40 {
41 memset(is_hunt_buf, 0xFF, 320);
42 is_state = 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 }
67
68 static void
69 is_process_cmd()
70 {
71 int cont;
72
73 printf("IS_Command: 0x%03X", is_rx_word);
74 switch (is_rx_word) {
75 case 0x05D:
76 printf(" (REQ)\n");
77 cont = 1;
78 break;
79 case 0x0BA:
80 printf(" (ACK)\n");
81 cont = 1;
82 break;
83 case 0x0E7:
84 printf(" (IPE)\n");
85 cont = 1;
86 break;
87 case 0x129:
88 printf(" (FILL)\n");
89 cont = 0;
90 break;
91 case 0x174:
92 printf(" (DUP)\n");
93 cont = 0;
94 break;
95 case 0x193:
96 printf(" (SYL)\n");
97 cont = 0;
98 break;
99 default:
100 printf(" (bad)\n");
101 cont = 0;
102 }
103 if (cont) {
104 is_state = 2;
105 is_bit_count = 0;
106 is_rx_word = 0;
107 } else
108 is_state = 0;
109 }
110
111 static void
112 is_process_ext()
113 {
114 printf("IS_Extension: 0x%05X", is_rx_word);
115 if (is_rx_word & 0x80200) {
116 printf(" (bad sync)\n");
117 is_state = 0;
118 return;
119 }
120 switch (is_rx_word & 3) {
121 case 0:
122 printf(" (final)\n");
123 is_state = 0;
124 return;
125 case 3:
126 printf(" (continue)\n");
127 is_state = 2;
128 is_bit_count = 0;
129 is_rx_word = 0;
130 return;
131 default:
132 printf(" (bad EX)\n");
133 is_state = 0;
134 }
135 }
136
137 static void
138 is_rx_process(input, input_pos)
139 uint8_t *input;
140 unsigned input_pos;
141 {
142 unsigned new_bit;
143
144 memmove(is_hunt_buf, is_hunt_buf + 16, 304);
145 memcpy(is_hunt_buf + 304, input, 16);
146 if (!is_state) {
147 is_rx_hunt(input_pos);
148 return;
149 }
150 new_bit = input[is_offset] & 1;
151 is_rx_word <<= 1;
152 is_rx_word |= new_bit;
153 is_bit_count++;
154 if (is_state == 1 && is_bit_count == 10)
155 is_process_cmd();
156 else if (is_state == 2 && is_bit_count == 20)
157 is_process_ext();
158 }
159
160 void
161 obtain_rtp_endp()
162 {
163 int rc;
164 struct rtp_alloc_simple res;
165
166 rc = rtp_alloc_simple(TMGW_EP_TYPE_PSTN_ONLY, &res);
167 if (rc < 0)
168 exit(1); /* error msg already printed */
169 bcopy(&res.pstn_addr, &rtp_local_addr, sizeof(struct sockaddr_in));
170 rtp_udp_fd = res.pstn_rtp_fd;
171 rtcp_udp_fd = res.pstn_rtcp_fd;
172 reset_is_hunt();
173 }
174
175 void
176 rtp_rx_select()
177 {
178 struct rtp_packet pkt;
179 struct sockaddr_in sin_from;
180 socklen_t addrlen;
181 int16_t seq_delta;
182 int32_t ts_delta;
183 int rc;
184 unsigned is_chunk;
185
186 addrlen = sizeof(struct sockaddr_in);
187 rc = recvfrom(rtp_udp_fd, &pkt, sizeof pkt, 0,
188 (struct sockaddr *) &sin_from, &addrlen);
189 if (rc < 0)
190 return;
191 if (rc != RTP_PACKET_SIZE_PSTN) {
192 bad_rtp_pkt: if (!rtp_bad_flag) {
193 printf("Got a bad RTP packet\n");
194 rtp_bad_flag = 1;
195 }
196 return;
197 }
198 if (pkt.v_p_x_cc != 0x80)
199 goto bad_rtp_pkt;
200 switch (pkt.m_pt & 0x7F) {
201 case PSTN_CODEC_PCMU:
202 case PSTN_CODEC_PCMA:
203 break;
204 default:
205 goto bad_rtp_pkt;
206 }
207 if (rtp_start_flag && pkt.ssrc != rtp_ssrc) {
208 if (!rtp_ssrc_chg_flag) {
209 printf("Rx RTP stream changed SSRC\n");
210 rtp_ssrc_chg_flag = 1;
211 }
212 reset_is_hunt();
213 } else if (rtp_start_flag) {
214 seq_delta = ntohs(pkt.seq) - rtp_last_seq;
215 ts_delta = ntohl(pkt.tstamp) - rtp_last_ts;
216 if (seq_delta == 0) {
217 if (!rtp_seq_zero_flag) {
218 printf("Rx RTP seq zero increment\n");
219 rtp_seq_zero_flag = 1;
220 }
221 return;
222 }
223 if (seq_delta < 0) {
224 if (!rtp_seq_neg_flag) {
225 printf("Rx RTP seq negative increment\n");
226 rtp_seq_neg_flag = 1;
227 }
228 return;
229 }
230 if (seq_delta != 1) {
231 if (!rtp_seq_brk_flag) {
232 printf("Rx RTP stream seq break\n");
233 rtp_seq_brk_flag = 1;
234 }
235 reset_is_hunt();
236 } else if (ts_delta != 160) {
237 if (!rtp_ts_brk_flag) {
238 printf("Rx RTP stream tstamp break\n");
239 rtp_ts_brk_flag = 1;
240 }
241 reset_is_hunt();
242 }
243 }
244 rtp_ssrc = pkt.ssrc;
245 rtp_last_ts = ntohl(pkt.tstamp);
246 rtp_last_seq = ntohs(pkt.seq);
247 if (!rtp_start_flag) {
248 printf("Rx RTP stream begins with seq=%u ts=%u\n",
249 rtp_last_seq, rtp_last_ts);
250 rtp_start_flag = 1;
251 }
252 for (is_chunk = 0; is_chunk < 10; is_chunk++)
253 is_rx_process(pkt.payload + is_chunk * 16, is_chunk);
254 }
255
256 void
257 rtcp_rx_select()
258 {
259 u_char buf[512];
260
261 recv(rtcp_udp_fd, buf, sizeof buf, 0);
262 if (!got_some_rtcp) {
263 printf("Got some RTCP\n");
264 got_some_rtcp = 1;
265 }
266 }