blob: a7bd3330d72dae7cef84827be54569e2fd9e55ee [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) */
Pau Espin Pedrole7406652022-10-04 11:58:56 +020012#define MGCP_DUMMY_LOAD 0x23
13static const char rtp_dummy_payload[] = { MGCP_DUMMY_LOAD };
Philipp Maierb3d14eb2021-05-20 14:18:52 +020014
15/* Check if the data in a given message buffer matches the rtp dummy payload
16 * defined above */
17#define mgcp_is_rtp_dummy_payload(msg) \
18 (msgb_length(msg) == sizeof(rtp_dummy_payload) && \
19 memcmp(msgb_data(msg), rtp_dummy_payload, sizeof(rtp_dummy_payload)) == 0)
20
Philipp Maier993ea6b2020-08-04 18:26:50 +020021#define RTP_BUF_SIZE 4096
22
23struct mgcp_rtp_stream_state {
24 uint32_t ssrc;
25 uint16_t last_seq;
26 uint32_t last_timestamp;
27 struct rate_ctr *err_ts_ctr;
28 int32_t last_tsdelta;
29 uint32_t last_arrival_time;
30};
31
32struct mgcp_rtp_state {
33 /* has this state structure been initialized? */
34 int initialized;
35
36 struct {
37 /* are we patching the SSRC value? */
Pau Espin Pedrol4c77e9b2021-07-07 12:12:58 +020038 bool patch_ssrc;
Philipp Maier993ea6b2020-08-04 18:26:50 +020039 /* original SSRC (to which we shall patch any different SSRC) */
40 uint32_t orig_ssrc;
41 /* offset to apply on the sequence number */
42 int seq_offset;
43 /* offset to apply on the timestamp number */
44 int32_t timestamp_offset;
45 } patch;
46
47 /* duration of a packet (FIXME: in which unit?) */
48 uint32_t packet_duration;
49
50 /* Note: These states are not continuously updated, they serve as an
51 * information source to patch certain values in the RTP header. Do
52 * not use this state if constantly updated data about the RTP stream
53 * is needed. (see also mgcp_patch_and_count() */
54 struct mgcp_rtp_stream_state in_stream;
55 struct mgcp_rtp_stream_state out_stream;
56
57 /* jitter and packet loss calculation */
58 struct {
59 int initialized;
60 uint16_t base_seq;
61 uint16_t max_seq;
62 uint32_t ssrc;
63 uint32_t jitter;
64 int32_t transit;
65 int cycles;
66 } stats;
67
68 /* Alternative values for RTP tx, in case no sufficient header
69 * information is available so the header needs to be generated
70 * locally (when just forwarding packets, the header of incoming
71 * data is just re-used) */
72 uint16_t alt_rtp_tx_sequence;
73 uint32_t alt_rtp_tx_ssrc;
Philipp Maier993ea6b2020-08-04 18:26:50 +020074};
75
76struct mgcp_rtp_codec {
77 uint32_t rate;
78 int channels;
79 uint32_t frame_duration_num;
80 uint32_t frame_duration_den;
81
82 int payload_type;
Eric2ebcf5c2021-08-09 23:26:48 +020083 char audio_name[64];
84 char subtype_name[64];
Philipp Maier993ea6b2020-08-04 18:26:50 +020085
86 bool param_present;
87 struct mgcp_codec_param param;
88};
89
90/* 'mgcp_rtp_end': basically a wrapper around the RTP+RTCP ports */
91struct mgcp_rtp_end {
Pau Espin Pedrol0ab152b2020-09-03 14:08:04 +020092 /* remote IP address of the RTP socket */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +020093 struct osmo_sockaddr addr;
Philipp Maier993ea6b2020-08-04 18:26:50 +020094
95 /* in network byte order */
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +020096 int rtcp_port;
Philipp Maier993ea6b2020-08-04 18:26:50 +020097
98 /* currently selected audio codec */
99 struct mgcp_rtp_codec *codec;
100
101 /* array with assigned audio codecs to choose from (SDP) */
102 struct mgcp_rtp_codec codecs[MGCP_MAX_CODECS];
103
104 /* number of assigned audio codecs (SDP) */
105 unsigned int codecs_assigned;
106
107 /* per endpoint data */
108 int frames_per_packet;
109 uint32_t packet_duration_ms;
110 int maximum_packet_time; /* -1: not set */
Pau Espin Pedrol2c401642021-12-24 14:48:26 +0100111 /* are we transmitting packets (true) or dropping (false) outbound packets */
112 bool output_enabled;
Philipp Maier993ea6b2020-08-04 18:26:50 +0200113 /* FIXME: This parameter can be set + printed, but is nowhere used! */
114 int force_output_ptime;
115
116 /* RTP patching */
117 int force_constant_ssrc; /* -1: always, 0: don't, 1: once */
118 /* should we perform align_rtp_timestamp_offset() (1) or not (0) */
119 int force_aligned_timing;
120 bool rfc5993_hr_convert;
121
122 /* Each end has a separate socket for RTP and RTCP */
123 struct osmo_fd rtp;
124 struct osmo_fd rtcp;
125
126 /* local UDP port number of the RTP socket; RTCP is +1 */
127 int local_port;
Pau Espin Pedrol71d42e72020-09-03 14:20:07 +0200128 /* where the endpoint RTP connection binds to, set during CRCX and
129 * possibly updated during MDCX */
130 char local_addr[INET6_ADDRSTRLEN];
Philipp Maier993ea6b2020-08-04 18:26:50 +0200131};
132
Pau Espin Pedrolca280a12021-07-06 18:15:35 +0200133bool mgcp_rtp_end_remote_addr_available(const struct mgcp_rtp_end *rtp_end);
134
Philipp Maier993ea6b2020-08-04 18:26:50 +0200135struct 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);
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200155void mgcp_patch_and_count(const struct mgcp_endpoint *endp,
Philipp Maier993ea6b2020-08-04 18:26:50 +0200156 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);
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +0200159int 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 */
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200175void mgcp_rtp_annex_count(const struct mgcp_endpoint *endp, struct mgcp_rtp_state *state,
Philipp Maier993ea6b2020-08-04 18:26:50 +0200176 const uint16_t seq, const int32_t transit,
Pau Espin Pedrolb066bd02021-07-07 13:41:19 +0200177 const uint32_t ssrc, const bool marker_bit);
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100178
179void rtpconn_rate_ctr_add(struct mgcp_conn_rtp *conn_rtp, struct mgcp_endpoint *endp,
180 int id, int inc);
Pau Espin Pedrol582c2bf2022-09-22 17:53:44 +0200181void rtpconn_rate_ctr_inc(struct mgcp_conn_rtp *conn_rtp, struct mgcp_endpoint *endp,
182 int id);
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100183void forward_data_tap(int fd, struct mgcp_rtp_tap *tap, struct msgb *msg);
184uint32_t mgcp_get_current_ts(unsigned codec_rate);
Pau Espin Pedrol377d3912022-10-25 13:52:19 +0200185
186int amr_oa_bwe_convert(struct mgcp_endpoint *endp, struct msgb *msg, bool target_is_oa);