Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 1 | /* MGCP Private Data */ |
| 2 | |
| 3 | /* |
Holger Hans Peter Freyther | 38e02c5 | 2012-10-22 18:09:35 +0200 | [diff] [blame] | 4 | * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * (C) 2009-2012 by On-Waves |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 9 | * it under the terms of the GNU Affero General Public License as published by |
| 10 | * the Free Software Foundation; either version 3 of the License, or |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 17 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 18 | * You should have received a copy of the GNU Affero General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 20 | * |
| 21 | */ |
| 22 | |
| 23 | #ifndef OPENBSC_MGCP_DATA_H |
| 24 | #define OPENBSC_MGCP_DATA_H |
| 25 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 26 | #include <osmocom/core/select.h> |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 27 | |
| 28 | #define CI_UNUSED 0 |
| 29 | |
Holger Hans Peter Freyther | 98a3877 | 2010-08-03 02:27:21 +0800 | [diff] [blame] | 30 | enum mgcp_connection_mode { |
| 31 | MGCP_CONN_NONE = 0, |
| 32 | MGCP_CONN_RECV_ONLY = 1, |
| 33 | MGCP_CONN_SEND_ONLY = 2, |
| 34 | MGCP_CONN_RECV_SEND = MGCP_CONN_RECV_ONLY | MGCP_CONN_SEND_ONLY, |
Jacob Erlbeck | e35fd13 | 2014-01-16 16:50:40 +0100 | [diff] [blame^] | 35 | MGCP_CONN_LOOPBACK = 4 | MGCP_CONN_RECV_SEND, |
Holger Hans Peter Freyther | 98a3877 | 2010-08-03 02:27:21 +0800 | [diff] [blame] | 36 | }; |
| 37 | |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 38 | enum mgcp_trunk_type { |
| 39 | MGCP_TRUNK_VIRTUAL, |
| 40 | MGCP_TRUNK_E1, |
| 41 | }; |
| 42 | |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 43 | struct mgcp_rtp_stream_state { |
| 44 | uint32_t ssrc; |
| 45 | uint16_t last_seq; |
| 46 | uint32_t last_timestamp; |
| 47 | uint32_t err_ts_counter; |
| 48 | int32_t last_tsdelta; |
| 49 | }; |
| 50 | |
Holger Hans Peter Freyther | 3186892 | 2010-08-03 11:59:04 +0000 | [diff] [blame] | 51 | struct mgcp_rtp_state { |
| 52 | int initialized; |
Jacob Erlbeck | 58340e5 | 2013-11-29 11:20:06 +0100 | [diff] [blame] | 53 | int patch_ssrc; |
Holger Hans Peter Freyther | 3186892 | 2010-08-03 11:59:04 +0000 | [diff] [blame] | 54 | |
Holger Hans Peter Freyther | 6aa882b | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 55 | uint32_t orig_ssrc; |
Holger Hans Peter Freyther | ed3a661 | 2012-10-22 17:08:48 +0200 | [diff] [blame] | 56 | |
| 57 | uint16_t base_seq; |
Holger Hans Peter Freyther | 6aa882b | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 58 | int seq_offset; |
Holger Hans Peter Freyther | 769912c | 2012-10-22 17:30:12 +0200 | [diff] [blame] | 59 | int cycles; |
Holger Hans Peter Freyther | ed3a661 | 2012-10-22 17:08:48 +0200 | [diff] [blame] | 60 | |
Holger Hans Peter Freyther | 6aa882b | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 61 | int32_t timestamp_offset; |
Jacob Erlbeck | 30ce422 | 2013-12-05 12:02:15 +0100 | [diff] [blame] | 62 | uint32_t packet_duration; |
Holger Hans Peter Freyther | 30690ad | 2012-10-24 20:31:25 +0200 | [diff] [blame] | 63 | uint32_t jitter; |
| 64 | int32_t transit; |
Jacob Erlbeck | 50079a1 | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 65 | |
| 66 | struct mgcp_rtp_stream_state in_stream; |
| 67 | struct mgcp_rtp_stream_state out_stream; |
Holger Hans Peter Freyther | 3186892 | 2010-08-03 11:59:04 +0000 | [diff] [blame] | 68 | }; |
Holger Hans Peter Freyther | 98a3877 | 2010-08-03 02:27:21 +0800 | [diff] [blame] | 69 | |
Holger Hans Peter Freyther | a17d701 | 2010-08-05 01:34:51 +0800 | [diff] [blame] | 70 | struct mgcp_rtp_end { |
| 71 | /* statistics */ |
| 72 | unsigned int packets; |
Holger Hans Peter Freyther | 952f752 | 2012-09-12 11:38:37 +0200 | [diff] [blame] | 73 | unsigned int octets; |
Jacob Erlbeck | 0970bab | 2013-12-19 12:13:32 +0100 | [diff] [blame] | 74 | unsigned int dropped_packets; |
Holger Hans Peter Freyther | a17d701 | 2010-08-05 01:34:51 +0800 | [diff] [blame] | 75 | struct in_addr addr; |
| 76 | |
| 77 | /* in network byte order */ |
| 78 | int rtp_port, rtcp_port; |
| 79 | |
Holger Hans Peter Freyther | 5ea1bc7 | 2012-09-03 00:07:39 +0200 | [diff] [blame] | 80 | /* per endpoint data */ |
Holger Hans Peter Freyther | a17d701 | 2010-08-05 01:34:51 +0800 | [diff] [blame] | 81 | int payload_type; |
Jacob Erlbeck | 72c3090 | 2013-11-25 15:23:35 +0100 | [diff] [blame] | 82 | uint32_t rate; |
| 83 | uint32_t frame_duration_num; |
| 84 | uint32_t frame_duration_den; |
| 85 | int frames_per_packet; |
Jacob Erlbeck | f6ec0e9 | 2013-12-04 10:30:11 +0100 | [diff] [blame] | 86 | uint32_t packet_duration_ms; |
Holger Hans Peter Freyther | 5ea1bc7 | 2012-09-03 00:07:39 +0200 | [diff] [blame] | 87 | char *fmtp_extra; |
Jacob Erlbeck | 0970bab | 2013-12-19 12:13:32 +0100 | [diff] [blame] | 88 | int output_enabled; |
Holger Hans Peter Freyther | 58ff219 | 2010-08-05 03:08:35 +0800 | [diff] [blame] | 89 | |
Jacob Erlbeck | db2d431 | 2013-12-03 14:43:34 +0100 | [diff] [blame] | 90 | /* RTP patching */ |
Jacob Erlbeck | e2292f3 | 2013-12-03 15:13:12 +0100 | [diff] [blame] | 91 | int force_constant_ssrc; /* -1: always, 0: don't, 1: once */ |
Jacob Erlbeck | 4bbddc6 | 2013-12-18 12:54:51 +0100 | [diff] [blame] | 92 | int force_aligned_timing; |
Jacob Erlbeck | db2d431 | 2013-12-03 14:43:34 +0100 | [diff] [blame] | 93 | |
Holger Hans Peter Freyther | c492127 | 2010-08-05 03:37:22 +0800 | [diff] [blame] | 94 | /* |
| 95 | * Each end has a socket... |
| 96 | */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 97 | struct osmo_fd rtp; |
| 98 | struct osmo_fd rtcp; |
Holger Hans Peter Freyther | c492127 | 2010-08-05 03:37:22 +0800 | [diff] [blame] | 99 | |
Holger Hans Peter Freyther | 58ff219 | 2010-08-05 03:08:35 +0800 | [diff] [blame] | 100 | int local_port; |
Holger Hans Peter Freyther | f138f91 | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 101 | int local_alloc; |
Holger Hans Peter Freyther | a17d701 | 2010-08-05 01:34:51 +0800 | [diff] [blame] | 102 | }; |
| 103 | |
Holger Hans Peter Freyther | 260d6ed | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 104 | enum { |
| 105 | MGCP_TAP_BTS_IN, |
| 106 | MGCP_TAP_BTS_OUT, |
| 107 | MGCP_TAP_NET_IN, |
| 108 | MGCP_TAP_NET_OUT, |
| 109 | |
| 110 | /* last element */ |
| 111 | MGCP_TAP_COUNT |
| 112 | }; |
| 113 | |
| 114 | struct mgcp_rtp_tap { |
| 115 | int enabled; |
| 116 | struct sockaddr_in forward; |
| 117 | }; |
| 118 | |
Jacob Erlbeck | 2c2ca4d | 2013-12-09 14:32:03 +0100 | [diff] [blame] | 119 | struct mgcp_lco { |
| 120 | char *string; |
| 121 | int pkt_period_min; /* time in ms */ |
| 122 | int pkt_period_max; /* time in ms */ |
| 123 | }; |
| 124 | |
Pablo Neira Ayuso | 46bd424 | 2013-07-08 05:09:46 +0200 | [diff] [blame] | 125 | enum mgcp_type { |
| 126 | MGCP_RTP_DEFAULT = 0, |
| 127 | MGCP_RTP_TRANSCODED, |
| 128 | }; |
| 129 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 130 | struct mgcp_endpoint { |
Holger Hans Peter Freyther | 39a97e2 | 2010-08-06 18:03:11 +0800 | [diff] [blame] | 131 | int allocated; |
Holger Hans Peter Freyther | 4634013 | 2010-08-06 08:26:54 +0800 | [diff] [blame] | 132 | uint32_t ci; |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 133 | char *callid; |
Jacob Erlbeck | 2c2ca4d | 2013-12-09 14:32:03 +0100 | [diff] [blame] | 134 | struct mgcp_lco local_options; |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 135 | int conn_mode; |
Holger Hans Peter Freyther | c597a4e | 2010-08-03 02:57:02 +0800 | [diff] [blame] | 136 | int orig_mode; |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 137 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 138 | /* backpointer */ |
| 139 | struct mgcp_config *cfg; |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 140 | struct mgcp_trunk_config *tcfg; |
Holger Hans Peter Freyther | b4b135e | 2010-04-07 09:37:17 +0200 | [diff] [blame] | 141 | |
Holger Hans Peter Freyther | a17d701 | 2010-08-05 01:34:51 +0800 | [diff] [blame] | 142 | /* port status for bts/net */ |
| 143 | struct mgcp_rtp_end bts_end; |
| 144 | struct mgcp_rtp_end net_end; |
Holger Hans Peter Freyther | 2126233 | 2010-11-01 20:53:31 +0100 | [diff] [blame] | 145 | |
Holger Hans Peter Freyther | bd7b3c5 | 2010-11-01 21:04:54 +0100 | [diff] [blame] | 146 | /* |
| 147 | * For transcoding we will send from the local_port |
| 148 | * of trans_bts and it will arrive at trans_net from |
| 149 | * where we will forward it to the network. |
| 150 | */ |
| 151 | struct mgcp_rtp_end trans_bts; |
Holger Hans Peter Freyther | 2126233 | 2010-11-01 20:53:31 +0100 | [diff] [blame] | 152 | struct mgcp_rtp_end trans_net; |
Pablo Neira Ayuso | 46bd424 | 2013-07-08 05:09:46 +0200 | [diff] [blame] | 153 | enum mgcp_type type; |
Holger Hans Peter Freyther | 380b871 | 2010-07-29 02:38:39 +0800 | [diff] [blame] | 154 | |
| 155 | /* sequence bits */ |
Holger Hans Peter Freyther | 3186892 | 2010-08-03 11:59:04 +0000 | [diff] [blame] | 156 | struct mgcp_rtp_state net_state; |
| 157 | struct mgcp_rtp_state bts_state; |
Holger Hans Peter Freyther | 6357a8e | 2010-08-05 12:07:00 +0000 | [diff] [blame] | 158 | |
Holger Hans Peter Freyther | ce55361 | 2012-12-07 15:04:07 +0100 | [diff] [blame] | 159 | /* fields for re-transmission */ |
| 160 | char *last_trans; |
| 161 | char *last_response; |
| 162 | |
Holger Hans Peter Freyther | 260d6ed | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 163 | /* tap for the endpoint */ |
| 164 | struct mgcp_rtp_tap taps[MGCP_TAP_COUNT]; |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 165 | }; |
| 166 | |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 167 | #define ENDPOINT_NUMBER(endp) abs(endp - endp->tcfg->endpoints) |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 168 | |
Holger Hans Peter Freyther | f2f1591 | 2010-04-01 03:27:04 +0200 | [diff] [blame] | 169 | struct mgcp_msg_ptr { |
| 170 | unsigned int start; |
| 171 | unsigned int length; |
| 172 | }; |
| 173 | |
Holger Hans Peter Freyther | b844b87 | 2010-04-21 21:25:13 +0800 | [diff] [blame] | 174 | int mgcp_send_dummy(struct mgcp_endpoint *endp); |
Holger Hans Peter Freyther | bb89aa1 | 2010-08-05 03:29:33 +0800 | [diff] [blame] | 175 | int mgcp_bind_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port); |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 176 | int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port); |
Holger Hans Peter Freyther | bd7b3c5 | 2010-11-01 21:04:54 +0100 | [diff] [blame] | 177 | int mgcp_bind_trans_bts_rtp_port(struct mgcp_endpoint *enp, int rtp_port); |
| 178 | int mgcp_bind_trans_net_rtp_port(struct mgcp_endpoint *enp, int rtp_port); |
Holger Hans Peter Freyther | f138f91 | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 179 | int mgcp_free_rtp_port(struct mgcp_rtp_end *end); |
Holger Hans Peter Freyther | f2f1591 | 2010-04-01 03:27:04 +0200 | [diff] [blame] | 180 | |
Holger Hans Peter Freyther | bd7b3c5 | 2010-11-01 21:04:54 +0100 | [diff] [blame] | 181 | /* For transcoding we need to manage an in and an output that are connected */ |
| 182 | static inline int endp_back_channel(int endpoint) |
| 183 | { |
| 184 | return endpoint + 60; |
| 185 | } |
| 186 | |
Holger Hans Peter Freyther | 0e939fe | 2011-02-28 12:11:02 +0100 | [diff] [blame] | 187 | struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int index); |
| 188 | struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index); |
| 189 | |
Jacob Erlbeck | db2d431 | 2013-12-03 14:43:34 +0100 | [diff] [blame] | 190 | void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change, |
| 191 | struct mgcp_rtp_end *rtp); |
Jacob Erlbeck | f6ec0e9 | 2013-12-04 10:30:11 +0100 | [diff] [blame] | 192 | uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp, |
| 193 | struct mgcp_rtp_end *rtp); |
Jacob Erlbeck | db2d431 | 2013-12-03 14:43:34 +0100 | [diff] [blame] | 194 | |
Holger Hans Peter Freyther | 38e02c5 | 2012-10-22 18:09:35 +0200 | [diff] [blame] | 195 | void mgcp_state_calc_loss(struct mgcp_rtp_state *s, struct mgcp_rtp_end *, |
| 196 | uint32_t *expected, int *loss); |
Holger Hans Peter Freyther | cb306a6 | 2012-10-24 21:22:47 +0200 | [diff] [blame] | 197 | uint32_t mgcp_state_calc_jitter(struct mgcp_rtp_state *); |
Holger Hans Peter Freyther | 38e02c5 | 2012-10-22 18:09:35 +0200 | [diff] [blame] | 198 | |
Holger Hans Peter Freyther | bd7b3c5 | 2010-11-01 21:04:54 +0100 | [diff] [blame] | 199 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 200 | #endif |