blob: 967444508db7bbe20a8fc934b4faba0502355c02 [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
10#define MGCP_DUMMY_LOAD 0x23
11#define RTP_BUF_SIZE 4096
12
13struct mgcp_rtp_stream_state {
14 uint32_t ssrc;
15 uint16_t last_seq;
16 uint32_t last_timestamp;
17 struct rate_ctr *err_ts_ctr;
18 int32_t last_tsdelta;
19 uint32_t last_arrival_time;
20};
21
22struct mgcp_rtp_state {
23 /* has this state structure been initialized? */
24 int initialized;
25
26 struct {
27 /* are we patching the SSRC value? */
28 int patch_ssrc;
29 /* original SSRC (to which we shall patch any different SSRC) */
30 uint32_t orig_ssrc;
31 /* offset to apply on the sequence number */
32 int seq_offset;
33 /* offset to apply on the timestamp number */
34 int32_t timestamp_offset;
35 } patch;
36
37 /* duration of a packet (FIXME: in which unit?) */
38 uint32_t packet_duration;
39
40 /* Note: These states are not continuously updated, they serve as an
41 * information source to patch certain values in the RTP header. Do
42 * not use this state if constantly updated data about the RTP stream
43 * is needed. (see also mgcp_patch_and_count() */
44 struct mgcp_rtp_stream_state in_stream;
45 struct mgcp_rtp_stream_state out_stream;
46
47 /* jitter and packet loss calculation */
48 struct {
49 int initialized;
50 uint16_t base_seq;
51 uint16_t max_seq;
52 uint32_t ssrc;
53 uint32_t jitter;
54 int32_t transit;
55 int cycles;
56 } stats;
57
58 /* Alternative values for RTP tx, in case no sufficient header
59 * information is available so the header needs to be generated
60 * locally (when just forwarding packets, the header of incoming
61 * data is just re-used) */
62 uint16_t alt_rtp_tx_sequence;
63 uint32_t alt_rtp_tx_ssrc;
64
65 bool patched_first_rtp_payload; /* FIXME: drop this, see OS#2459 */
66};
67
68struct mgcp_rtp_codec {
69 uint32_t rate;
70 int channels;
71 uint32_t frame_duration_num;
72 uint32_t frame_duration_den;
73
74 int payload_type;
75 char *audio_name;
76 char *subtype_name;
77
78 bool param_present;
79 struct mgcp_codec_param param;
80};
81
82/* 'mgcp_rtp_end': basically a wrapper around the RTP+RTCP ports */
83struct mgcp_rtp_end {
Pau Espin Pedrol0ab152b2020-09-03 14:08:04 +020084 /* remote IP address of the RTP socket */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +020085 struct osmo_sockaddr addr;
Philipp Maier993ea6b2020-08-04 18:26:50 +020086
87 /* in network byte order */
88 int rtp_port, rtcp_port;
89
90 /* currently selected audio codec */
91 struct mgcp_rtp_codec *codec;
92
93 /* array with assigned audio codecs to choose from (SDP) */
94 struct mgcp_rtp_codec codecs[MGCP_MAX_CODECS];
95
96 /* number of assigned audio codecs (SDP) */
97 unsigned int codecs_assigned;
98
99 /* per endpoint data */
100 int frames_per_packet;
101 uint32_t packet_duration_ms;
102 int maximum_packet_time; /* -1: not set */
103 char *fmtp_extra;
104 /* are we transmitting packets (1) or dropping (0) outbound packets */
105 int output_enabled;
106 /* FIXME: This parameter can be set + printed, but is nowhere used! */
107 int force_output_ptime;
108
109 /* RTP patching */
110 int force_constant_ssrc; /* -1: always, 0: don't, 1: once */
111 /* should we perform align_rtp_timestamp_offset() (1) or not (0) */
112 int force_aligned_timing;
113 bool rfc5993_hr_convert;
114
115 /* Each end has a separate socket for RTP and RTCP */
116 struct osmo_fd rtp;
117 struct osmo_fd rtcp;
118
119 /* local UDP port number of the RTP socket; RTCP is +1 */
120 int local_port;
121};
122
123struct mgcp_rtp_tap {
124 /* is this tap active (1) or not (0) */
125 int enabled;
126 /* IP/port to which we're forwarding the tapped data */
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200127 struct osmo_sockaddr forward;
Philipp Maier993ea6b2020-08-04 18:26:50 +0200128};
129
130struct mgcp_conn;
131
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200132int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct osmo_sockaddr *addr,
Philipp Maier993ea6b2020-08-04 18:26:50 +0200133 struct msgb *msg, struct mgcp_conn_rtp *conn_src,
134 struct mgcp_conn_rtp *conn_dst);
135int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn);
136int mgcp_dispatch_rtp_bridge_cb(struct msgb *msg);
137void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn);
138int mgcp_dispatch_e1_bridge_cb(struct msgb *msg);
139void mgcp_cleanup_e1_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn);
140int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
141 struct mgcp_conn_rtp *conn);
142void mgcp_free_rtp_port(struct mgcp_rtp_end *end);
143void mgcp_patch_and_count(struct mgcp_endpoint *endp,
144 struct mgcp_rtp_state *state,
145 struct mgcp_rtp_end *rtp_end,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200146 struct osmo_sockaddr *addr, struct msgb *msg);
Philipp Maier993ea6b2020-08-04 18:26:50 +0200147void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn);
148int mgcp_set_ip_tos(int fd, int tos);
149
150/* payload processing default functions */
151int mgcp_rtp_processing_default(struct mgcp_endpoint *endp, struct mgcp_rtp_end *dst_end,
152 char *data, int *len, int buf_size);
153
154int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
155 struct mgcp_conn_rtp *conn_dst,
156 struct mgcp_conn_rtp *conn_src);
157
158void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
159 const struct mgcp_rtp_codec **codec,
160 const char **fmtp_extra,
161 struct mgcp_conn_rtp *conn);
162
163/* internal RTP Annex A counting */
164void mgcp_rtp_annex_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state,
165 const uint16_t seq, const int32_t transit,
166 const uint32_t ssrc);