blob: 0487b911f64b524d3e40770032d5cb5c36b49fda [file] [log] [blame]
Philipp Maier993ea6b2020-08-04 18:26:50 +02001#pragma once
2
3#include <inttypes.h>
4#include <stdbool.h>
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +02005
6#include <osmocom/core/socket.h>
7
Philipp Maier993ea6b2020-08-04 18:26:50 +02008#include <osmocom/mgcp/mgcp.h>
9
Philipp Maierb3d14eb2021-05-20 14:18:52 +020010/* The following constant defines an RTP dummy payload that is used for
11 * "UDP Hole Punching" (NAT) */
12static const char rtp_dummy_payload[] = { 0x23 };
13
14/* Check if the data in a given message buffer matches the rtp dummy payload
15 * defined above */
16#define mgcp_is_rtp_dummy_payload(msg) \
17 (msgb_length(msg) == sizeof(rtp_dummy_payload) && \
18 memcmp(msgb_data(msg), rtp_dummy_payload, sizeof(rtp_dummy_payload)) == 0)
19
Philipp Maier993ea6b2020-08-04 18:26:50 +020020#define RTP_BUF_SIZE 4096
21
22struct mgcp_rtp_stream_state {
23 uint32_t ssrc;
24 uint16_t last_seq;
25 uint32_t last_timestamp;
26 struct rate_ctr *err_ts_ctr;
27 int32_t last_tsdelta;
28 uint32_t last_arrival_time;
29};
30
31struct mgcp_rtp_state {
32 /* has this state structure been initialized? */
33 int initialized;
34
35 struct {
36 /* are we patching the SSRC value? */
Pau Espin Pedrol4c77e9b2021-07-07 12:12:58 +020037 bool patch_ssrc;
Philipp Maier993ea6b2020-08-04 18:26:50 +020038 /* original SSRC (to which we shall patch any different SSRC) */
39 uint32_t orig_ssrc;
40 /* offset to apply on the sequence number */
41 int seq_offset;
42 /* offset to apply on the timestamp number */
43 int32_t timestamp_offset;
44 } patch;
45
46 /* duration of a packet (FIXME: in which unit?) */
47 uint32_t packet_duration;
48
49 /* Note: These states are not continuously updated, they serve as an
50 * information source to patch certain values in the RTP header. Do
51 * not use this state if constantly updated data about the RTP stream
52 * is needed. (see also mgcp_patch_and_count() */
53 struct mgcp_rtp_stream_state in_stream;
54 struct mgcp_rtp_stream_state out_stream;
55
56 /* jitter and packet loss calculation */
57 struct {
58 int initialized;
59 uint16_t base_seq;
60 uint16_t max_seq;
61 uint32_t ssrc;
62 uint32_t jitter;
63 int32_t transit;
64 int cycles;
65 } stats;
66
67 /* Alternative values for RTP tx, in case no sufficient header
68 * information is available so the header needs to be generated
69 * locally (when just forwarding packets, the header of incoming
70 * data is just re-used) */
71 uint16_t alt_rtp_tx_sequence;
72 uint32_t alt_rtp_tx_ssrc;
73
74 bool patched_first_rtp_payload; /* FIXME: drop this, see OS#2459 */
75};
76
77struct mgcp_rtp_codec {
78 uint32_t rate;
79 int channels;
80 uint32_t frame_duration_num;
81 uint32_t frame_duration_den;
82
83 int payload_type;
84 char *audio_name;
85 char *subtype_name;
86
87 bool param_present;
88 struct mgcp_codec_param param;
89};
90
91/* 'mgcp_rtp_end': basically a wrapper around the RTP+RTCP ports */
92struct mgcp_rtp_end {
Pau Espin Pedrol0ab152b2020-09-03 14:08:04 +020093 /* remote IP address of the RTP socket */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +020094 struct osmo_sockaddr addr;
Philipp Maier993ea6b2020-08-04 18:26:50 +020095
96 /* in network byte order */
97 int rtp_port, rtcp_port;
98
99 /* currently selected audio codec */
100 struct mgcp_rtp_codec *codec;
101
102 /* array with assigned audio codecs to choose from (SDP) */
103 struct mgcp_rtp_codec codecs[MGCP_MAX_CODECS];
104
105 /* number of assigned audio codecs (SDP) */
106 unsigned int codecs_assigned;
107
108 /* per endpoint data */
109 int frames_per_packet;
110 uint32_t packet_duration_ms;
111 int maximum_packet_time; /* -1: not set */
112 char *fmtp_extra;
113 /* are we transmitting packets (1) or dropping (0) outbound packets */
114 int output_enabled;
115 /* FIXME: This parameter can be set + printed, but is nowhere used! */
116 int force_output_ptime;
117
118 /* RTP patching */
119 int force_constant_ssrc; /* -1: always, 0: don't, 1: once */
120 /* should we perform align_rtp_timestamp_offset() (1) or not (0) */
121 int force_aligned_timing;
122 bool rfc5993_hr_convert;
123
124 /* Each end has a separate socket for RTP and RTCP */
125 struct osmo_fd rtp;
126 struct osmo_fd rtcp;
127
128 /* local UDP port number of the RTP socket; RTCP is +1 */
129 int local_port;
Pau Espin Pedrol71d42e72020-09-03 14:20:07 +0200130 /* where the endpoint RTP connection binds to, set during CRCX and
131 * possibly updated during MDCX */
132 char local_addr[INET6_ADDRSTRLEN];
Philipp Maier993ea6b2020-08-04 18:26:50 +0200133};
134
135struct mgcp_rtp_tap {
136 /* is this tap active (1) or not (0) */
137 int enabled;
138 /* IP/port to which we're forwarding the tapped data */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200139 struct osmo_sockaddr forward;
Philipp Maier993ea6b2020-08-04 18:26:50 +0200140};
141
142struct mgcp_conn;
143
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200144int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct osmo_sockaddr *addr,
Philipp Maier993ea6b2020-08-04 18:26:50 +0200145 struct msgb *msg, struct mgcp_conn_rtp *conn_src,
146 struct mgcp_conn_rtp *conn_dst);
147int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn);
148int mgcp_dispatch_rtp_bridge_cb(struct msgb *msg);
149void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn);
150int mgcp_dispatch_e1_bridge_cb(struct msgb *msg);
151void mgcp_cleanup_e1_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn);
152int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
153 struct mgcp_conn_rtp *conn);
154void mgcp_free_rtp_port(struct mgcp_rtp_end *end);
155void mgcp_patch_and_count(struct mgcp_endpoint *endp,
156 struct mgcp_rtp_state *state,
157 struct mgcp_rtp_end *rtp_end,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200158 struct osmo_sockaddr *addr, struct msgb *msg);
Philipp Maier993ea6b2020-08-04 18:26:50 +0200159void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn);
Philipp Maier993ea6b2020-08-04 18:26:50 +0200160
161/* payload processing default functions */
162int mgcp_rtp_processing_default(struct mgcp_endpoint *endp, struct mgcp_rtp_end *dst_end,
163 char *data, int *len, int buf_size);
164
165int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
166 struct mgcp_conn_rtp *conn_dst,
167 struct mgcp_conn_rtp *conn_src);
168
169void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
170 const struct mgcp_rtp_codec **codec,
171 const char **fmtp_extra,
172 struct mgcp_conn_rtp *conn);
173
174/* internal RTP Annex A counting */
175void mgcp_rtp_annex_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state,
176 const uint16_t seq, const int32_t transit,
177 const uint32_t ssrc);