annotate mgw/gsm2pstn.c @ 247:c6a3fb913c53

top Makefile: mtctest changed to use libnumdb2
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 14 Aug 2023 22:39:28 -0800
parents 0047c4c08d9e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
103
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * In this module we implement our RTP gateway function
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * in the GSM to PSTN direction.
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/socket.h>
127
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents: 103
diff changeset
8 #include <sys/time.h>
103
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <netinet/in.h>
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdio.h>
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <stdint.h>
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <stdlib.h>
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <string.h>
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include <strings.h>
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 #include <syslog.h>
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 #include <unistd.h>
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 #include <gsm.h> /* libgsm dependency */
168
8c28426abef0 mgw: start using the new libgsmfrp dependency
Mychaela Falconia <falcon@freecalypso.org>
parents: 127
diff changeset
18 #include <gsm_fr_preproc.h>
174
c985c33baeac mgw: support EFR by way of libgsmefr
Mychaela Falconia <falcon@freecalypso.org>
parents: 170
diff changeset
19 #include <gsm_efr.h>
205
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
20 #include "../include/codec_defs.h"
198
cf1ba5d65188 mgw: start using project-global rtp_defs.h
Mychaela Falconia <falcon@freecalypso.org>
parents: 174
diff changeset
21 #include "../include/rtp_defs.h"
103
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 #include "../include/tmgw_ctrl.h"
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 #include "../include/tmgw_const.h"
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 #include "struct.h"
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 #include "select.h"
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 #include "int_defs.h"
170
a6eb2de277f6 mgw: massive simplification for continuous RTP stream from BTS
Mychaela Falconia <falcon@freecalypso.org>
parents: 169
diff changeset
27 #include "dtmf_defs.h"
127
f062c32a5116 mgw: implement DTMF
Mychaela Falconia <falcon@freecalypso.org>
parents: 103
diff changeset
28
103
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 #define ERR_WRONG_UDP_SRC 0x0001
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 #define ERR_BAD_RTP_PACKET 0x0002
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 #define ERR_SSRC_CHANGE 0x0004
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 #define ERR_SEQ_BREAK 0x0008
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 #define ERR_TSTAMP_BREAK 0x0010
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 void
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 gsm2pstn_rtp_in(in_fd, ep)
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 struct endpoint *ep;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 {
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 struct rtp_packet pkt;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 struct sockaddr_in sin_from;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 socklen_t addrlen;
169
648a64b2e16b mgw: accept Themyscira-extension RTP BFI packets
Mychaela Falconia <falcon@freecalypso.org>
parents: 168
diff changeset
42 unsigned pktsize;
103
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 int16_t seq_delta;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 int32_t ts_delta;
205
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
45 uint8_t codec_frame[GSM_FR_BYTES];
103
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 int16_t pcm_samples[SAMPLES_PER_FRAME];
205
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
47 int rc, bfi, bfi_nodata, taf, m_out;
103
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 addrlen = sizeof(struct sockaddr_in);
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 rc = recvfrom(in_fd, &pkt, sizeof pkt, 0,
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 (struct sockaddr *) &sin_from, &addrlen);
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 if (rc < 0)
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 return;
169
648a64b2e16b mgw: accept Themyscira-extension RTP BFI packets
Mychaela Falconia <falcon@freecalypso.org>
parents: 168
diff changeset
54 pktsize = rc;
103
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 if (sin_from.sin_addr.s_addr != ep->rtp_gsm.remote_addr.sin_addr.s_addr
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 || sin_from.sin_port != ep->rtp_gsm.remote_addr.sin_port) {
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 if (!(ep->g2p_err_flags & ERR_WRONG_UDP_SRC)) {
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 syslog(LOG_ERR,
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 "GSM RTP ep got UDP packet from wrong source");
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 ep->g2p_err_flags |= ERR_WRONG_UDP_SRC;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 }
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 return;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 }
199
e6c7ced3c031 mgw: accept zero-length RTP payload as BFI
Mychaela Falconia <falcon@freecalypso.org>
parents: 198
diff changeset
64 if (pktsize < RTP_PACKET_HDR_SIZE) {
103
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 bad_rtp_pkt: if (!(ep->g2p_err_flags & ERR_BAD_RTP_PACKET)) {
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 syslog(LOG_ERR, "Rx bad RTP packet on GSM side");
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 ep->g2p_err_flags |= ERR_BAD_RTP_PACKET;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 }
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 return;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 }
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 if (pkt.v_p_x_cc != 0x80)
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 goto bad_rtp_pkt;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 if ((pkt.m_pt & 0x7F) != ep->gsm_payload_type)
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 goto bad_rtp_pkt;
205
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
75 pktsize -= RTP_PACKET_HDR_SIZE;
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
76 if (!pktsize) {
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
77 /* zero length payload */
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
78 bfi = bfi_nodata = 1;
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
79 taf = 0;
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
80 } else if (pktsize == ep->gsm_payload_len &&
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
81 (pkt.payload[0] & 0xF0) == ep->gsm_payload_magic) {
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
82 /* standard RFC 3551 or TS 101 318 payload */
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
83 bfi = bfi_nodata = 0;
199
e6c7ced3c031 mgw: accept zero-length RTP payload as BFI
Mychaela Falconia <falcon@freecalypso.org>
parents: 198
diff changeset
84 taf = 0;
205
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
85 bcopy(pkt.payload, codec_frame, pktsize);
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
86 } else if (pktsize == 2 && pkt.payload[0] == 0xBF) {
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
87 /* old BFI marker */
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
88 bfi = bfi_nodata = 1;
169
648a64b2e16b mgw: accept Themyscira-extension RTP BFI packets
Mychaela Falconia <falcon@freecalypso.org>
parents: 168
diff changeset
89 taf = pkt.payload[1] & 1;
205
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
90 } else if ((pkt.payload[0] & 0xF0) == 0xE0) {
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
91 /* new TRAUlike RTP payload format */
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
92 bfi_nodata = (pkt.payload[0] & 0x04) >> 2;
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
93 bfi = (pkt.payload[0] & 0x02) >> 1;
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
94 taf = pkt.payload[0] & 0x01;
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
95 if (bfi_nodata) {
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
96 if (!bfi)
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
97 goto bad_rtp_pkt;
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
98 if (pktsize != 1)
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
99 goto bad_rtp_pkt;
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
100 } else {
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
101 if (pktsize != ep->gsm_payload_len + 1)
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
102 goto bad_rtp_pkt;
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
103 if ((pkt.payload[1] & 0xF0) != ep->gsm_payload_magic)
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
104 goto bad_rtp_pkt;
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
105 bcopy(pkt.payload+1, codec_frame, ep->gsm_payload_len);
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
106 }
169
648a64b2e16b mgw: accept Themyscira-extension RTP BFI packets
Mychaela Falconia <falcon@freecalypso.org>
parents: 168
diff changeset
107 } else
103
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 goto bad_rtp_pkt;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 if (ep->g2p_state && pkt.ssrc != ep->g2p_ssrc) {
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 if (!(ep->g2p_err_flags & ERR_SSRC_CHANGE)) {
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 syslog(LOG_ERR, "GSM RTP stream changed SSRC");
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 ep->g2p_err_flags |= ERR_SSRC_CHANGE;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 }
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 ep->g2p_state = 0;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 }
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 if (ep->g2p_state) {
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 seq_delta = ntohs(pkt.seq) - ep->g2p_last_seq;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 ts_delta = ntohl(pkt.tstamp) - ep->g2p_last_ts;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 if (seq_delta <= 0)
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 return; /* discard old or duplicate */
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 if (seq_delta != 1) {
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122 if (!(ep->g2p_err_flags & ERR_SEQ_BREAK)) {
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 syslog(LOG_ERR, "GSM RTP stream seq break");
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124 ep->g2p_err_flags |= ERR_SEQ_BREAK;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125 }
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126 m_out = 1;
170
a6eb2de277f6 mgw: massive simplification for continuous RTP stream from BTS
Mychaela Falconia <falcon@freecalypso.org>
parents: 169
diff changeset
127 } else if (ts_delta != SAMPLES_PER_FRAME) {
a6eb2de277f6 mgw: massive simplification for continuous RTP stream from BTS
Mychaela Falconia <falcon@freecalypso.org>
parents: 169
diff changeset
128 if (!(ep->g2p_err_flags & ERR_TSTAMP_BREAK)) {
a6eb2de277f6 mgw: massive simplification for continuous RTP stream from BTS
Mychaela Falconia <falcon@freecalypso.org>
parents: 169
diff changeset
129 syslog(LOG_ERR, "GSM RTP stream tstamp break");
a6eb2de277f6 mgw: massive simplification for continuous RTP stream from BTS
Mychaela Falconia <falcon@freecalypso.org>
parents: 169
diff changeset
130 ep->g2p_err_flags |= ERR_TSTAMP_BREAK;
103
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
131 }
170
a6eb2de277f6 mgw: massive simplification for continuous RTP stream from BTS
Mychaela Falconia <falcon@freecalypso.org>
parents: 169
diff changeset
132 m_out = 1;
a6eb2de277f6 mgw: massive simplification for continuous RTP stream from BTS
Mychaela Falconia <falcon@freecalypso.org>
parents: 169
diff changeset
133 } else
a6eb2de277f6 mgw: massive simplification for continuous RTP stream from BTS
Mychaela Falconia <falcon@freecalypso.org>
parents: 169
diff changeset
134 m_out = 0;
103
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
135 } else
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
136 m_out = 1;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
137 ep->g2p_state = 1;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
138 ep->g2p_ssrc = pkt.ssrc;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
139 ep->g2p_last_ts = ntohl(pkt.tstamp);
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
140 ep->g2p_last_seq = ntohs(pkt.seq);
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
141 /* actual transcoding and forwarding */
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
142 if (!(ep->fwd_mode & TMGW_FWD_ENABLE_GSM2PSTN)) {
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
143 ep->g2p_drop_flag = 1;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
144 return;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
145 }
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
146 if (ep->g2p_drop_flag) {
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
147 ep->g2p_drop_flag = 0;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
148 m_out = 1;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
149 }
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
150 switch (ep->gsm_payload_msg_type) {
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
151 case GSM_TCHF_FRAME:
169
648a64b2e16b mgw: accept Themyscira-extension RTP BFI packets
Mychaela Falconia <falcon@freecalypso.org>
parents: 168
diff changeset
152 if (bfi)
648a64b2e16b mgw: accept Themyscira-extension RTP BFI packets
Mychaela Falconia <falcon@freecalypso.org>
parents: 168
diff changeset
153 gsmfr_preproc_bfi(ep->gsm_decoder_extra_state, taf,
205
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
154 codec_frame);
169
648a64b2e16b mgw: accept Themyscira-extension RTP BFI packets
Mychaela Falconia <falcon@freecalypso.org>
parents: 168
diff changeset
155 else
648a64b2e16b mgw: accept Themyscira-extension RTP BFI packets
Mychaela Falconia <falcon@freecalypso.org>
parents: 168
diff changeset
156 gsmfr_preproc_good_frame(ep->gsm_decoder_extra_state,
205
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
157 codec_frame);
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
158 gsm_decode(ep->gsm_decoder_state, codec_frame, pcm_samples);
103
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
159 break;
174
c985c33baeac mgw: support EFR by way of libgsmefr
Mychaela Falconia <falcon@freecalypso.org>
parents: 170
diff changeset
160 case GSM_TCHF_FRAME_EFR:
205
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
161 if (bfi_nodata)
174
c985c33baeac mgw: support EFR by way of libgsmefr
Mychaela Falconia <falcon@freecalypso.org>
parents: 170
diff changeset
162 EFR_decode_bfi_nodata(ep->gsm_decoder_state, taf,
c985c33baeac mgw: support EFR by way of libgsmefr
Mychaela Falconia <falcon@freecalypso.org>
parents: 170
diff changeset
163 pcm_samples);
c985c33baeac mgw: support EFR by way of libgsmefr
Mychaela Falconia <falcon@freecalypso.org>
parents: 170
diff changeset
164 else
205
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
165 EFR_decode_frame(ep->gsm_decoder_state, codec_frame,
0047c4c08d9e mgw: accept the new TRAUlike RTP format
Mychaela Falconia <falcon@freecalypso.org>
parents: 199
diff changeset
166 bfi, taf, pcm_samples);
174
c985c33baeac mgw: support EFR by way of libgsmefr
Mychaela Falconia <falcon@freecalypso.org>
parents: 170
diff changeset
167 break;
103
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
168 }
170
a6eb2de277f6 mgw: massive simplification for continuous RTP stream from BTS
Mychaela Falconia <falcon@freecalypso.org>
parents: 169
diff changeset
169 if (ep->dtmf_sample_ptr) {
a6eb2de277f6 mgw: massive simplification for continuous RTP stream from BTS
Mychaela Falconia <falcon@freecalypso.org>
parents: 169
diff changeset
170 bcopy(ep->dtmf_sample_ptr, pcm_samples, SAMPLES_PER_FRAME*2);
a6eb2de277f6 mgw: massive simplification for continuous RTP stream from BTS
Mychaela Falconia <falcon@freecalypso.org>
parents: 169
diff changeset
171 ep->dtmf_sample_ptr += SAMPLES_PER_FRAME;
a6eb2de277f6 mgw: massive simplification for continuous RTP stream from BTS
Mychaela Falconia <falcon@freecalypso.org>
parents: 169
diff changeset
172 ep->dtmf_frames_sent++;
a6eb2de277f6 mgw: massive simplification for continuous RTP stream from BTS
Mychaela Falconia <falcon@freecalypso.org>
parents: 169
diff changeset
173 if (ep->dtmf_frames_sent >= DTMF_MAX_FRAMES)
a6eb2de277f6 mgw: massive simplification for continuous RTP stream from BTS
Mychaela Falconia <falcon@freecalypso.org>
parents: 169
diff changeset
174 ep->dtmf_sample_ptr = 0;
a6eb2de277f6 mgw: massive simplification for continuous RTP stream from BTS
Mychaela Falconia <falcon@freecalypso.org>
parents: 169
diff changeset
175 }
103
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
176 pkt.m_pt = ep->pstn_payload_type;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
177 if (m_out)
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
178 pkt.m_pt |= 0x80;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
179 pkt.seq = htons(++ep->g2p_out_seq);
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
180 g711_encode_frame(pcm_samples, pkt.payload, ep->pstn_payload_type);
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
181 addrlen = sizeof(struct sockaddr_in);
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
182 sendto(ep->rtp_pstn.rtp_fd, &pkt, RTP_PACKET_SIZE_PSTN, 0,
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
183 (struct sockaddr *) &ep->rtp_pstn.remote_addr, addrlen);
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
184 }
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
185
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
186 gsm2pstn_init(ep)
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
187 struct endpoint *ep;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
188 {
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
189 if (ep->gsm_decoder_state)
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
190 return TMGW_RESP_OK;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
191 switch (ep->gsm_payload_msg_type) {
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
192 case GSM_TCHF_FRAME:
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
193 ep->gsm_decoder_state = gsm_create();
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
194 if (!ep->gsm_decoder_state)
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
195 return TMGW_RESP_ERR_RSRC;
168
8c28426abef0 mgw: start using the new libgsmfrp dependency
Mychaela Falconia <falcon@freecalypso.org>
parents: 127
diff changeset
196 ep->gsm_decoder_extra_state = gsmfr_preproc_create();
8c28426abef0 mgw: start using the new libgsmfrp dependency
Mychaela Falconia <falcon@freecalypso.org>
parents: 127
diff changeset
197 if (!ep->gsm_decoder_extra_state) {
8c28426abef0 mgw: start using the new libgsmfrp dependency
Mychaela Falconia <falcon@freecalypso.org>
parents: 127
diff changeset
198 free(ep->gsm_decoder_state);
8c28426abef0 mgw: start using the new libgsmfrp dependency
Mychaela Falconia <falcon@freecalypso.org>
parents: 127
diff changeset
199 ep->gsm_decoder_state = 0;
8c28426abef0 mgw: start using the new libgsmfrp dependency
Mychaela Falconia <falcon@freecalypso.org>
parents: 127
diff changeset
200 return TMGW_RESP_ERR_RSRC;
8c28426abef0 mgw: start using the new libgsmfrp dependency
Mychaela Falconia <falcon@freecalypso.org>
parents: 127
diff changeset
201 }
103
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
202 break;
174
c985c33baeac mgw: support EFR by way of libgsmefr
Mychaela Falconia <falcon@freecalypso.org>
parents: 170
diff changeset
203 case GSM_TCHF_FRAME_EFR:
c985c33baeac mgw: support EFR by way of libgsmefr
Mychaela Falconia <falcon@freecalypso.org>
parents: 170
diff changeset
204 ep->gsm_decoder_state = EFR_decoder_create();
c985c33baeac mgw: support EFR by way of libgsmefr
Mychaela Falconia <falcon@freecalypso.org>
parents: 170
diff changeset
205 if (!ep->gsm_decoder_state)
c985c33baeac mgw: support EFR by way of libgsmefr
Mychaela Falconia <falcon@freecalypso.org>
parents: 170
diff changeset
206 return TMGW_RESP_ERR_RSRC;
c985c33baeac mgw: support EFR by way of libgsmefr
Mychaela Falconia <falcon@freecalypso.org>
parents: 170
diff changeset
207 break;
103
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
208 }
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
209 select_handlers[ep->rtp_gsm.rtp_fd] = gsm2pstn_rtp_in;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
210 return TMGW_RESP_OK;
3b3f07b112f3 mgw: implement GSM to PSTN forwarding
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
211 }