Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <inttypes.h> |
| 4 | #include <stdbool.h> |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 5 | |
| 6 | #include <osmocom/core/socket.h> |
| 7 | |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 8 | #include <osmocom/mgcp/mgcp.h> |
| 9 | |
Philipp Maier | b3d14eb | 2021-05-20 14:18:52 +0200 | [diff] [blame] | 10 | /* The following constant defines an RTP dummy payload that is used for |
| 11 | * "UDP Hole Punching" (NAT) */ |
Pau Espin Pedrol | e740665 | 2022-10-04 11:58:56 +0200 | [diff] [blame] | 12 | #define MGCP_DUMMY_LOAD 0x23 |
| 13 | static const char rtp_dummy_payload[] = { MGCP_DUMMY_LOAD }; |
Philipp Maier | b3d14eb | 2021-05-20 14:18:52 +0200 | [diff] [blame] | 14 | |
| 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 Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 21 | #define RTP_BUF_SIZE 4096 |
| 22 | |
| 23 | struct 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 | |
| 32 | struct 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 Pedrol | 4c77e9b | 2021-07-07 12:12:58 +0200 | [diff] [blame] | 38 | bool patch_ssrc; |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 39 | /* 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 Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | struct 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; |
Eric | 2ebcf5c | 2021-08-09 23:26:48 +0200 | [diff] [blame] | 83 | char audio_name[64]; |
| 84 | char subtype_name[64]; |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 85 | |
| 86 | bool param_present; |
| 87 | struct mgcp_codec_param param; |
| 88 | }; |
| 89 | |
| 90 | /* 'mgcp_rtp_end': basically a wrapper around the RTP+RTCP ports */ |
| 91 | struct mgcp_rtp_end { |
Pau Espin Pedrol | 0ab152b | 2020-09-03 14:08:04 +0200 | [diff] [blame] | 92 | /* remote IP address of the RTP socket */ |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 93 | struct osmo_sockaddr addr; |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 94 | |
| 95 | /* in network byte order */ |
Pau Espin Pedrol | 5ffd127 | 2022-10-04 13:45:48 +0200 | [diff] [blame] | 96 | int rtcp_port; |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 97 | |
| 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 */ |
| 111 | char *fmtp_extra; |
Pau Espin Pedrol | 2c40164 | 2021-12-24 14:48:26 +0100 | [diff] [blame] | 112 | /* are we transmitting packets (true) or dropping (false) outbound packets */ |
| 113 | bool output_enabled; |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 114 | /* FIXME: This parameter can be set + printed, but is nowhere used! */ |
| 115 | int force_output_ptime; |
| 116 | |
| 117 | /* RTP patching */ |
| 118 | int force_constant_ssrc; /* -1: always, 0: don't, 1: once */ |
| 119 | /* should we perform align_rtp_timestamp_offset() (1) or not (0) */ |
| 120 | int force_aligned_timing; |
| 121 | bool rfc5993_hr_convert; |
| 122 | |
| 123 | /* Each end has a separate socket for RTP and RTCP */ |
| 124 | struct osmo_fd rtp; |
| 125 | struct osmo_fd rtcp; |
| 126 | |
| 127 | /* local UDP port number of the RTP socket; RTCP is +1 */ |
| 128 | int local_port; |
Pau Espin Pedrol | 71d42e7 | 2020-09-03 14:20:07 +0200 | [diff] [blame] | 129 | /* where the endpoint RTP connection binds to, set during CRCX and |
| 130 | * possibly updated during MDCX */ |
| 131 | char local_addr[INET6_ADDRSTRLEN]; |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 132 | }; |
| 133 | |
Pau Espin Pedrol | ca280a1 | 2021-07-06 18:15:35 +0200 | [diff] [blame] | 134 | bool mgcp_rtp_end_remote_addr_available(const struct mgcp_rtp_end *rtp_end); |
| 135 | |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 136 | struct mgcp_rtp_tap { |
| 137 | /* is this tap active (1) or not (0) */ |
| 138 | int enabled; |
| 139 | /* IP/port to which we're forwarding the tapped data */ |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 140 | struct osmo_sockaddr forward; |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | struct mgcp_conn; |
| 144 | |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 145 | int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct osmo_sockaddr *addr, |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 146 | struct msgb *msg, struct mgcp_conn_rtp *conn_src, |
| 147 | struct mgcp_conn_rtp *conn_dst); |
| 148 | int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn); |
| 149 | int mgcp_dispatch_rtp_bridge_cb(struct msgb *msg); |
| 150 | void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn); |
| 151 | int mgcp_dispatch_e1_bridge_cb(struct msgb *msg); |
| 152 | void mgcp_cleanup_e1_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn); |
| 153 | int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port, |
| 154 | struct mgcp_conn_rtp *conn); |
| 155 | void mgcp_free_rtp_port(struct mgcp_rtp_end *end); |
Pau Espin Pedrol | 8358c4b | 2021-07-07 12:41:38 +0200 | [diff] [blame] | 156 | void mgcp_patch_and_count(const struct mgcp_endpoint *endp, |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 157 | struct mgcp_rtp_state *state, |
| 158 | struct mgcp_rtp_end *rtp_end, |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 159 | struct osmo_sockaddr *addr, struct msgb *msg); |
Pau Espin Pedrol | 70c03f5 | 2022-10-04 16:49:41 +0200 | [diff] [blame] | 160 | int mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn); |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 161 | |
| 162 | /* payload processing default functions */ |
| 163 | int mgcp_rtp_processing_default(struct mgcp_endpoint *endp, struct mgcp_rtp_end *dst_end, |
| 164 | char *data, int *len, int buf_size); |
| 165 | |
| 166 | int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp, |
| 167 | struct mgcp_conn_rtp *conn_dst, |
| 168 | struct mgcp_conn_rtp *conn_src); |
| 169 | |
| 170 | void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp, |
| 171 | const struct mgcp_rtp_codec **codec, |
| 172 | const char **fmtp_extra, |
| 173 | struct mgcp_conn_rtp *conn); |
| 174 | |
| 175 | /* internal RTP Annex A counting */ |
Pau Espin Pedrol | 8358c4b | 2021-07-07 12:41:38 +0200 | [diff] [blame] | 176 | void mgcp_rtp_annex_count(const struct mgcp_endpoint *endp, struct mgcp_rtp_state *state, |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 177 | const uint16_t seq, const int32_t transit, |
Pau Espin Pedrol | b066bd0 | 2021-07-07 13:41:19 +0200 | [diff] [blame] | 178 | const uint32_t ssrc, const bool marker_bit); |
Pau Espin Pedrol | bb3ccde | 2021-12-23 19:49:26 +0100 | [diff] [blame] | 179 | |
| 180 | void rtpconn_rate_ctr_add(struct mgcp_conn_rtp *conn_rtp, struct mgcp_endpoint *endp, |
| 181 | int id, int inc); |
Pau Espin Pedrol | 582c2bf | 2022-09-22 17:53:44 +0200 | [diff] [blame] | 182 | void rtpconn_rate_ctr_inc(struct mgcp_conn_rtp *conn_rtp, struct mgcp_endpoint *endp, |
| 183 | int id); |
Pau Espin Pedrol | bb3ccde | 2021-12-23 19:49:26 +0100 | [diff] [blame] | 184 | void forward_data_tap(int fd, struct mgcp_rtp_tap *tap, struct msgb *msg); |
| 185 | uint32_t mgcp_get_current_ts(unsigned codec_rate); |
Pau Espin Pedrol | 377d391 | 2022-10-25 13:52:19 +0200 | [diff] [blame] | 186 | |
| 187 | int amr_oa_bwe_convert(struct mgcp_endpoint *endp, struct msgb *msg, bool target_is_oa); |