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