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 | |
| 10 | #define MGCP_DUMMY_LOAD 0x23 |
| 11 | #define RTP_BUF_SIZE 4096 |
| 12 | |
| 13 | struct 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 | |
| 22 | struct 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 | |
| 68 | struct 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 */ |
| 83 | struct mgcp_rtp_end { |
Pau Espin Pedrol | 0ab152b | 2020-09-03 14:08:04 +0200 | [diff] [blame] | 84 | /* remote IP address of the RTP socket */ |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 85 | struct osmo_sockaddr addr; |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 86 | |
| 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; |
Pau Espin Pedrol | 71d42e7 | 2020-09-03 14:20:07 +0200 | [diff] [blame] | 121 | /* where the endpoint RTP connection binds to, set during CRCX and |
| 122 | * possibly updated during MDCX */ |
| 123 | char local_addr[INET6_ADDRSTRLEN]; |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | struct mgcp_rtp_tap { |
| 127 | /* is this tap active (1) or not (0) */ |
| 128 | int enabled; |
| 129 | /* IP/port to which we're forwarding the tapped data */ |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 130 | struct osmo_sockaddr forward; |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | struct mgcp_conn; |
| 134 | |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 135 | 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] | 136 | struct msgb *msg, struct mgcp_conn_rtp *conn_src, |
| 137 | struct mgcp_conn_rtp *conn_dst); |
| 138 | int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn); |
| 139 | int mgcp_dispatch_rtp_bridge_cb(struct msgb *msg); |
| 140 | void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn); |
| 141 | int mgcp_dispatch_e1_bridge_cb(struct msgb *msg); |
| 142 | void mgcp_cleanup_e1_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn); |
| 143 | int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port, |
| 144 | struct mgcp_conn_rtp *conn); |
| 145 | void mgcp_free_rtp_port(struct mgcp_rtp_end *end); |
| 146 | void mgcp_patch_and_count(struct mgcp_endpoint *endp, |
| 147 | struct mgcp_rtp_state *state, |
| 148 | struct mgcp_rtp_end *rtp_end, |
Pau Espin Pedrol | a790f0c | 2020-08-31 13:29:11 +0200 | [diff] [blame] | 149 | struct osmo_sockaddr *addr, struct msgb *msg); |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 150 | void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn); |
| 151 | int mgcp_set_ip_tos(int fd, int tos); |
| 152 | |
| 153 | /* payload processing default functions */ |
| 154 | int mgcp_rtp_processing_default(struct mgcp_endpoint *endp, struct mgcp_rtp_end *dst_end, |
| 155 | char *data, int *len, int buf_size); |
| 156 | |
| 157 | int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp, |
| 158 | struct mgcp_conn_rtp *conn_dst, |
| 159 | struct mgcp_conn_rtp *conn_src); |
| 160 | |
| 161 | void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp, |
| 162 | const struct mgcp_rtp_codec **codec, |
| 163 | const char **fmtp_extra, |
| 164 | struct mgcp_conn_rtp *conn); |
| 165 | |
| 166 | /* internal RTP Annex A counting */ |
| 167 | void mgcp_rtp_annex_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state, |
| 168 | const uint16_t seq, const int32_t transit, |
| 169 | const uint32_t ssrc); |