blob: 5668711e4d50f8808fd86ee7a521872d4d37d5a3 [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;
Philipp Maier993ea6b2020-08-04 18:26:50 +020073};
74
75struct mgcp_rtp_codec {
76 uint32_t rate;
77 int channels;
78 uint32_t frame_duration_num;
79 uint32_t frame_duration_den;
80
81 int payload_type;
Eric2ebcf5c2021-08-09 23:26:48 +020082 char audio_name[64];
83 char subtype_name[64];
Philipp Maier993ea6b2020-08-04 18:26:50 +020084
85 bool param_present;
86 struct mgcp_codec_param param;
87};
88
89/* 'mgcp_rtp_end': basically a wrapper around the RTP+RTCP ports */
90struct mgcp_rtp_end {
Pau Espin Pedrol0ab152b2020-09-03 14:08:04 +020091 /* remote IP address of the RTP socket */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +020092 struct osmo_sockaddr addr;
Philipp Maier993ea6b2020-08-04 18:26:50 +020093
94 /* in network byte order */
95 int rtp_port, rtcp_port;
96
97 /* currently selected audio codec */
98 struct mgcp_rtp_codec *codec;
99
100 /* array with assigned audio codecs to choose from (SDP) */
101 struct mgcp_rtp_codec codecs[MGCP_MAX_CODECS];
102
103 /* number of assigned audio codecs (SDP) */
104 unsigned int codecs_assigned;
105
106 /* per endpoint data */
107 int frames_per_packet;
108 uint32_t packet_duration_ms;
109 int maximum_packet_time; /* -1: not set */
110 char *fmtp_extra;
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);
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 */
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);