FreeCalypso > hg > themwi-system-sw
annotate sip-manual-out/rtp_tx.c @ 198:cf1ba5d65188
mgw: start using project-global rtp_defs.h
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Wed, 29 Mar 2023 20:06:40 -0800 |
parents | a3d71489672f |
children | d3c99b41fb04 |
rev | line source |
---|---|
192
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * In this module we implement outgoing RTP stream generation. |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 */ |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 #include <sys/types.h> |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <sys/socket.h> |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <sys/time.h> |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <netinet/in.h> |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <stdio.h> |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <stdint.h> |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include <stdlib.h> |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 #include <string.h> |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 #include <strings.h> |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 #include <unistd.h> |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 #include "../include/tmgw_const.h" |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 #include "../include/rtp_defs.h" |
195
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
17 #include "../libutil/osmo_bits.h" |
192
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 extern struct sockaddr_in rtp_local_addr, rtp_remote_addr; |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 extern int rtp_udp_fd, rtcp_udp_fd, pcma_selected; |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 extern struct timeval cur_event_time; |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 static uint32_t rtp_ssrc; |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 static uint32_t rtp_out_ts; |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 static uint16_t rtp_out_seq; |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 |
195
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
27 static uint16_t is_out_buf[7], *is_out_ptr; |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
28 static unsigned is_out_count; |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
29 |
192
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 void |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 assign_rtpout_ssrc() |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 { |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 rtp_ssrc = cur_event_time.tv_sec ^ cur_event_time.tv_usec ^ getpid(); |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 } |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 |
195
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
36 static void |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
37 insert_is_msg(payload, word) |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
38 uint8_t *payload; |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
39 uint16_t word; |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
40 { |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
41 ubit_t is_bits[10]; |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
42 uint8_t *bytep; |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
43 unsigned n; |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
44 |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
45 uint16_to_bits(word, is_bits, 10); |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
46 for (n = 0; n < 10; n++) { |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
47 bytep = payload + n * 16; |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
48 *bytep &= 0xFE; |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
49 *bytep |= is_bits[n]; |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
50 } |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
51 } |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
52 |
192
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 void |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 generate_rtp_packet() |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 { |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 struct rtp_packet pkt; |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 socklen_t addrlen; |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 pkt.v_p_x_cc = 0x80; |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 pkt.m_pt = pcma_selected ? PSTN_CODEC_PCMA : PSTN_CODEC_PCMU; |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 pkt.seq = htons(rtp_out_seq++); |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 pkt.tstamp = htonl(rtp_out_ts); |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 rtp_out_ts += 160; |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 pkt.ssrc = rtp_ssrc; |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 memset(pkt.payload, pcma_selected ? 0xD5 : 0xFF, RTP_MAX_PAYLOAD); |
195
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
66 if (is_out_count) { |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
67 insert_is_msg(pkt.payload, *is_out_ptr++); |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
68 is_out_count--; |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
69 } |
192
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 addrlen = sizeof(struct sockaddr_in); |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 sendto(rtp_udp_fd, &pkt, RTP_PACKET_SIZE_PSTN, 0, |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 (struct sockaddr *) &rtp_remote_addr, addrlen); |
f8a33603288f
sip-manual-out: generate outgoing RTP stream with PCM silence
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 } |
195
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
74 |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
75 void |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
76 send_tfo_req(sig, codec) |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
77 unsigned sig, codec; |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
78 { |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
79 is_out_buf[0] = 0x15A; |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
80 is_out_buf[1] = 0x1A9; |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
81 is_out_buf[2] = 0x05D; |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
82 is_out_buf[3] = 0x14E; |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
83 is_out_buf[4] = 0x14B; |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
84 encode_tfo_ext_words(sig, codec, 0, is_out_buf + 5); |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
85 is_out_ptr = is_out_buf; |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
86 is_out_count = 7; |
a3d71489672f
sip-manual-out: implement tfo-req command
Mychaela Falconia <falcon@freecalypso.org>
parents:
192
diff
changeset
|
87 } |