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) */ |
| 12 | static 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 Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 20 | #define RTP_BUF_SIZE 4096 |
| 21 | |
| 22 | struct 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 | |
| 31 | struct 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 Pedrol | 4c77e9b | 2021-07-07 12:12:58 +0200 | [diff] [blame] | 37 | bool patch_ssrc; |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 38 | /* 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 | |
| 77 | struct 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; |
Eric | 2ebcf5c | 2021-08-09 23:26:48 +0200 | [diff] [blame] | 84 | char audio_name[64]; |
| 85 | char subtype_name[64]; |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 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 */ |
| 92 | struct mgcp_rtp_end { |
Pau Espin Pedrol | 0ab152b | 2020-09-03 14:08:04 +0200 | [diff] [blame] | 93 | /* remote IP address of the RTP socket */ |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 94 | struct osmo_sockaddr addr; |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 95 | |
| 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 Pedrol | 71d42e7 | 2020-09-03 14:20:07 +0200 | [diff] [blame] | 130 | /* where the endpoint RTP connection binds to, set during CRCX and |
| 131 | * possibly updated during MDCX */ |
| 132 | char local_addr[INET6_ADDRSTRLEN]; |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 133 | }; |
| 134 | |
Pau Espin Pedrol | ca280a1 | 2021-07-06 18:15:35 +0200 | [diff] [blame] | 135 | bool mgcp_rtp_end_remote_addr_available(const struct mgcp_rtp_end *rtp_end); |
| 136 | |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 137 | struct mgcp_rtp_tap { |
| 138 | /* is this tap active (1) or not (0) */ |
| 139 | int enabled; |
| 140 | /* IP/port to which we're forwarding the tapped data */ |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 141 | struct osmo_sockaddr forward; |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | struct mgcp_conn; |
| 145 | |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 146 | 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] | 147 | struct msgb *msg, struct mgcp_conn_rtp *conn_src, |
| 148 | struct mgcp_conn_rtp *conn_dst); |
| 149 | int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn); |
| 150 | int mgcp_dispatch_rtp_bridge_cb(struct msgb *msg); |
| 151 | void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn); |
| 152 | int mgcp_dispatch_e1_bridge_cb(struct msgb *msg); |
| 153 | void mgcp_cleanup_e1_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn); |
| 154 | int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port, |
| 155 | struct mgcp_conn_rtp *conn); |
| 156 | void mgcp_free_rtp_port(struct mgcp_rtp_end *end); |
Pau Espin Pedrol | 8358c4b | 2021-07-07 12:41:38 +0200 | [diff] [blame] | 157 | void mgcp_patch_and_count(const struct mgcp_endpoint *endp, |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 158 | struct mgcp_rtp_state *state, |
| 159 | struct mgcp_rtp_end *rtp_end, |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 160 | struct osmo_sockaddr *addr, struct msgb *msg); |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 161 | void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn); |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 162 | |
| 163 | /* payload processing default functions */ |
| 164 | int mgcp_rtp_processing_default(struct mgcp_endpoint *endp, struct mgcp_rtp_end *dst_end, |
| 165 | char *data, int *len, int buf_size); |
| 166 | |
| 167 | int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp, |
| 168 | struct mgcp_conn_rtp *conn_dst, |
| 169 | struct mgcp_conn_rtp *conn_src); |
| 170 | |
| 171 | void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp, |
| 172 | const struct mgcp_rtp_codec **codec, |
| 173 | const char **fmtp_extra, |
| 174 | struct mgcp_conn_rtp *conn); |
| 175 | |
| 176 | /* internal RTP Annex A counting */ |
Pau Espin Pedrol | 8358c4b | 2021-07-07 12:41:38 +0200 | [diff] [blame] | 177 | 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] | 178 | const uint16_t seq, const int32_t transit, |
Pau Espin Pedrol | b066bd0 | 2021-07-07 13:41:19 +0200 | [diff] [blame] | 179 | const uint32_t ssrc, const bool marker_bit); |