Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 1 | /* A Media Gateway Control Protocol Media Gateway: RFC 3435 */ |
| 2 | /* The protocol implementation */ |
| 3 | |
| 4 | /* |
Holger Hans Peter Freyther | faa6793 | 2012-10-22 18:09:35 +0200 | [diff] [blame] | 5 | * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * (C) 2009-2012 by On-Waves |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 18 | * |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <string.h> |
Holger Hans Peter Freyther | adb1992 | 2010-05-14 02:14:09 +0800 | [diff] [blame] | 25 | #include <stdlib.h> |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 26 | #include <unistd.h> |
Holger Hans Peter Freyther | 9d7bd81 | 2010-04-07 12:55:40 +0200 | [diff] [blame] | 27 | #include <errno.h> |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 28 | |
| 29 | #include <sys/socket.h> |
| 30 | #include <arpa/inet.h> |
| 31 | |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 32 | #include <osmocom/core/msgb.h> |
| 33 | #include <osmocom/core/select.h> |
Holger Hans Peter Freyther | f41fb1f | 2010-02-26 20:16:37 +0100 | [diff] [blame] | 34 | |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 35 | #include <openbsc/mgcp.h> |
| 36 | #include <openbsc/mgcp_internal.h> |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 37 | |
| 38 | #warning "Make use of the rtp proxy code" |
| 39 | |
Holger Hans Peter Freyther | 19f4aa5 | 2010-10-12 15:39:46 +0200 | [diff] [blame] | 40 | /* attempt to determine byte order */ |
Holger Hans Peter Freyther | 19f4aa5 | 2010-10-12 15:39:46 +0200 | [diff] [blame] | 41 | #include <sys/param.h> |
| 42 | #include <limits.h> |
Holger Hans Peter Freyther | 13de794 | 2012-10-24 20:31:25 +0200 | [diff] [blame] | 43 | #include <time.h> |
Holger Hans Peter Freyther | 19f4aa5 | 2010-10-12 15:39:46 +0200 | [diff] [blame] | 44 | |
| 45 | #ifndef __BYTE_ORDER |
Tobias Engel | 82c64c9 | 2012-10-24 17:53:50 +0200 | [diff] [blame] | 46 | # ifdef __APPLE__ |
| 47 | # define __BYTE_ORDER __DARWIN_BYTE_ORDER |
| 48 | # define __LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN |
| 49 | # define __BIG_ENDIAN __DARWIN_BIG_ENDIAN |
| 50 | # else |
| 51 | # error "__BYTE_ORDER should be defined by someone" |
| 52 | # endif |
Holger Hans Peter Freyther | 19f4aa5 | 2010-10-12 15:39:46 +0200 | [diff] [blame] | 53 | #endif |
| 54 | |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 55 | /* according to rtp_proxy.c RFC 3550 */ |
| 56 | struct rtp_hdr { |
| 57 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
Holger Hans Peter Freyther | 1979f79 | 2010-07-23 18:56:26 +0800 | [diff] [blame] | 58 | uint8_t csrc_count:4, |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 59 | extension:1, |
| 60 | padding:1, |
| 61 | version:2; |
Holger Hans Peter Freyther | 1979f79 | 2010-07-23 18:56:26 +0800 | [diff] [blame] | 62 | uint8_t payload_type:7, |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 63 | marker:1; |
| 64 | #elif __BYTE_ORDER == __BIG_ENDIAN |
Holger Hans Peter Freyther | 1979f79 | 2010-07-23 18:56:26 +0800 | [diff] [blame] | 65 | uint8_t version:2, |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 66 | padding:1, |
| 67 | extension:1, |
| 68 | csrc_count:4; |
Holger Hans Peter Freyther | 1979f79 | 2010-07-23 18:56:26 +0800 | [diff] [blame] | 69 | uint8_t marker:1, |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 70 | payload_type:7; |
| 71 | #endif |
Holger Hans Peter Freyther | 1884623 | 2010-07-23 18:56:01 +0800 | [diff] [blame] | 72 | uint16_t sequence; |
Holger Hans Peter Freyther | 142475b | 2010-07-23 18:55:38 +0800 | [diff] [blame] | 73 | uint32_t timestamp; |
| 74 | uint32_t ssrc; |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 75 | } __attribute__((packed)); |
| 76 | |
Holger Hans Peter Freyther | 69849d1 | 2012-10-22 17:30:12 +0200 | [diff] [blame] | 77 | #define RTP_SEQ_MOD (1 << 16) |
| 78 | #define RTP_MAX_DROPOUT 3000 |
| 79 | #define RTP_MAX_MISORDER 100 |
| 80 | |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 81 | |
| 82 | enum { |
Pablo Neira Ayuso | 2a13ade | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 83 | MGCP_DEST_NET = 0, |
| 84 | MGCP_DEST_BTS, |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | enum { |
Pablo Neira Ayuso | 2a13ade | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 88 | MGCP_PROTO_RTP, |
| 89 | MGCP_PROTO_RTCP, |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 90 | }; |
| 91 | |
Pablo Neira Ayuso | 2a13ade | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 92 | #define MGCP_DUMMY_LOAD 0x23 |
Holger Hans Peter Freyther | b689426 | 2010-04-21 21:25:13 +0800 | [diff] [blame] | 93 | |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 94 | |
Holger Hans Peter Freyther | 13de794 | 2012-10-24 20:31:25 +0200 | [diff] [blame] | 95 | /** |
| 96 | * This does not need to be a precision timestamp and |
| 97 | * is allowed to wrap quite fast. The returned value is |
| 98 | * milli seconds now. |
| 99 | */ |
| 100 | uint32_t get_current_ts(void) |
| 101 | { |
| 102 | struct timespec tp; |
| 103 | uint64_t ret; |
| 104 | |
| 105 | memset(&tp, 0, sizeof(tp)); |
| 106 | if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0) |
| 107 | LOGP(DMGCP, LOGL_NOTICE, |
| 108 | "Getting the clock failed.\n"); |
| 109 | |
| 110 | /* convert it to useconds */ |
| 111 | ret = tp.tv_sec; |
| 112 | ret *= 1000; |
| 113 | ret += tp.tv_nsec / 1000 / 1000; |
| 114 | |
| 115 | return ret; |
| 116 | } |
| 117 | |
Pablo Neira Ayuso | ad11ffe | 2013-08-25 17:39:38 +0200 | [diff] [blame] | 118 | static int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, |
| 119 | int len) |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 120 | { |
| 121 | struct sockaddr_in out; |
| 122 | out.sin_family = AF_INET; |
| 123 | out.sin_port = port; |
| 124 | memcpy(&out.sin_addr, addr, sizeof(*addr)); |
| 125 | |
| 126 | return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out)); |
| 127 | } |
| 128 | |
Holger Hans Peter Freyther | b689426 | 2010-04-21 21:25:13 +0800 | [diff] [blame] | 129 | int mgcp_send_dummy(struct mgcp_endpoint *endp) |
| 130 | { |
Pablo Neira Ayuso | 2a13ade | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 131 | static char buf[] = { MGCP_DUMMY_LOAD }; |
Holger Hans Peter Freyther | b689426 | 2010-04-21 21:25:13 +0800 | [diff] [blame] | 132 | |
Pablo Neira Ayuso | ad11ffe | 2013-08-25 17:39:38 +0200 | [diff] [blame] | 133 | return mgcp_udp_send(endp->net_end.rtp.fd, &endp->net_end.addr, |
| 134 | endp->net_end.rtp_port, buf, 1); |
Holger Hans Peter Freyther | b689426 | 2010-04-21 21:25:13 +0800 | [diff] [blame] | 135 | } |
| 136 | |
Jacob Erlbeck | a2528da | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 137 | static int check_rtp_timestamp(struct mgcp_endpoint *endp, |
| 138 | struct mgcp_rtp_stream_state *state, |
| 139 | struct mgcp_rtp_end *rtp_end, |
| 140 | struct sockaddr_in *addr, |
| 141 | uint16_t seq, uint32_t timestamp, |
| 142 | const char *text, int32_t *tsdelta_out) |
| 143 | { |
| 144 | int32_t tsdelta; |
| 145 | |
| 146 | /* Not fully intialized, skip */ |
| 147 | if (state->last_tsdelta == 0 && timestamp == state->last_timestamp) |
| 148 | return 0; |
| 149 | |
| 150 | if (seq == state->last_seq) { |
| 151 | if (timestamp != state->last_timestamp) { |
| 152 | state->err_ts_counter += 1; |
| 153 | LOGP(DMGCP, LOGL_ERROR, |
| 154 | "The %s timestamp delta is != 0 but the sequence " |
| 155 | "number %d is the same" |
| 156 | "on 0x%x SSRC: %u timestamp: %u " |
| 157 | "from %s:%d in %d\n", |
| 158 | text, seq, |
| 159 | ENDPOINT_NUMBER(endp), state->ssrc, timestamp, |
| 160 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 161 | endp->conn_mode); |
| 162 | } |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | tsdelta = |
| 167 | (int32_t)(timestamp - state->last_timestamp) / |
| 168 | (int16_t)(seq - state->last_seq); |
| 169 | |
| 170 | if (tsdelta == 0) { |
| 171 | state->err_ts_counter += 1; |
| 172 | LOGP(DMGCP, LOGL_ERROR, |
| 173 | "The %s timestamp delta is %d " |
| 174 | "on 0x%x SSRC: %u timestamp: %u " |
| 175 | "from %s:%d in %d\n", |
| 176 | text, tsdelta, |
| 177 | ENDPOINT_NUMBER(endp), state->ssrc, timestamp, |
| 178 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 179 | endp->conn_mode); |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | if (state->last_tsdelta != tsdelta) { |
| 185 | if (state->last_tsdelta) { |
| 186 | state->err_ts_counter += 1; |
| 187 | LOGP(DMGCP, LOGL_ERROR, |
| 188 | "The %s timestamp delta changes from %d to %d " |
| 189 | "on 0x%x SSRC: %u timestamp: %u from %s:%d in %d\n", |
| 190 | text, state->last_tsdelta, tsdelta, |
| 191 | ENDPOINT_NUMBER(endp), state->ssrc, timestamp, |
| 192 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 193 | endp->conn_mode); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | if (tsdelta_out) |
| 198 | *tsdelta_out = tsdelta; |
| 199 | |
| 200 | return 1; |
| 201 | } |
| 202 | |
| 203 | |
Holger Hans Peter Freyther | 4e7b56f | 2012-10-22 17:15:31 +0200 | [diff] [blame] | 204 | /** |
| 205 | * The RFC 3550 Appendix A assumes there are multiple sources but |
| 206 | * some of the supported endpoints (e.g. the nanoBTS) can only handle |
| 207 | * one source and this code will patch packages to appear as if there |
| 208 | * is only one source. |
| 209 | * There is also no probation period for new sources. Every package |
| 210 | * we receive will be seen as a switch in streams. |
| 211 | */ |
Jacob Erlbeck | 4516a8a | 2013-11-25 12:53:29 +0100 | [diff] [blame] | 212 | void mgcp_patch_and_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state, |
| 213 | struct mgcp_rtp_end *rtp_end, struct sockaddr_in *addr, |
| 214 | char *data, int len) |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 215 | { |
Holger Hans Peter Freyther | 13de794 | 2012-10-24 20:31:25 +0200 | [diff] [blame] | 216 | uint32_t arrival_time; |
| 217 | int32_t transit, d; |
Holger Hans Peter Freyther | 69849d1 | 2012-10-22 17:30:12 +0200 | [diff] [blame] | 218 | uint16_t seq, udelta; |
Holger Hans Peter Freyther | 3040ed4 | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 219 | uint32_t timestamp; |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 220 | struct rtp_hdr *rtp_hdr; |
Jacob Erlbeck | a2528da | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 221 | int payload = rtp_end->payload_type; |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 222 | |
| 223 | if (len < sizeof(*rtp_hdr)) |
| 224 | return; |
| 225 | |
Holger Hans Peter Freyther | e758d95 | 2010-07-29 02:38:39 +0800 | [diff] [blame] | 226 | rtp_hdr = (struct rtp_hdr *) data; |
| 227 | seq = ntohs(rtp_hdr->sequence); |
Holger Hans Peter Freyther | 3040ed4 | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 228 | timestamp = ntohl(rtp_hdr->timestamp); |
Holger Hans Peter Freyther | 13de794 | 2012-10-24 20:31:25 +0200 | [diff] [blame] | 229 | arrival_time = get_current_ts(); |
Holger Hans Peter Freyther | e758d95 | 2010-07-29 02:38:39 +0800 | [diff] [blame] | 230 | |
Holger Hans Peter Freyther | 84ba528 | 2010-08-03 11:59:04 +0000 | [diff] [blame] | 231 | if (!state->initialized) { |
Jacob Erlbeck | a2528da | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 232 | state->in_stream.last_seq = seq - 1; |
| 233 | state->in_stream.ssrc = state->orig_ssrc = rtp_hdr->ssrc; |
| 234 | state->in_stream.last_tsdelta = 0; |
Holger Hans Peter Freyther | f6fe20f | 2012-10-22 17:08:48 +0200 | [diff] [blame] | 235 | state->base_seq = seq; |
Holger Hans Peter Freyther | 84ba528 | 2010-08-03 11:59:04 +0000 | [diff] [blame] | 236 | state->initialized = 1; |
Holger Hans Peter Freyther | 13de794 | 2012-10-24 20:31:25 +0200 | [diff] [blame] | 237 | state->jitter = 0; |
| 238 | state->transit = arrival_time - timestamp; |
Jacob Erlbeck | a2528da | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 239 | state->out_stream = state->in_stream; |
Jacob Erlbeck | 5065aef | 2013-11-29 11:16:07 +0100 | [diff] [blame] | 240 | state->out_stream.last_timestamp = timestamp; |
| 241 | /* force output SSRC change */ |
| 242 | state->out_stream.ssrc = rtp_hdr->ssrc - 1; |
| 243 | LOGP(DMGCP, LOGL_INFO, |
| 244 | "Initializing stream on 0x%x SSRC: %u timestamp: %u " |
| 245 | "from %s:%d in %d\n", |
| 246 | ENDPOINT_NUMBER(endp), state->in_stream.ssrc, |
| 247 | state->seq_offset, |
| 248 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 249 | endp->conn_mode); |
Jacob Erlbeck | a2528da | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 250 | } else if (state->in_stream.ssrc != rtp_hdr->ssrc) { |
Jacob Erlbeck | ac9f6ce | 2013-11-25 15:23:35 +0100 | [diff] [blame] | 251 | int32_t tsdelta = state->out_stream.last_tsdelta; |
| 252 | if (tsdelta == 0) { |
| 253 | tsdelta = rtp_end->rate * rtp_end->frames_per_packet * |
| 254 | rtp_end->frame_duration_num / |
| 255 | rtp_end->frame_duration_den; |
| 256 | LOGP(DMGCP, LOGL_NOTICE, |
| 257 | "Computed timestamp delta %d based on " |
| 258 | "rate %d, num frames %d, frame duration %d/%d\n", |
| 259 | tsdelta, rtp_end->rate, rtp_end->frames_per_packet, |
| 260 | rtp_end->frame_duration_num, |
| 261 | rtp_end->frame_duration_den); |
| 262 | } |
Jacob Erlbeck | a2528da | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 263 | state->in_stream.ssrc = rtp_hdr->ssrc; |
| 264 | state->seq_offset = (state->out_stream.last_seq + 1) - seq; |
Jacob Erlbeck | ac9f6ce | 2013-11-25 15:23:35 +0100 | [diff] [blame] | 265 | state->timestamp_offset = |
| 266 | (state->out_stream.last_timestamp + tsdelta) - timestamp; |
Jacob Erlbeck | e5546cd | 2013-12-03 14:43:34 +0100 | [diff] [blame^] | 267 | state->patch = rtp_end->force_constant_ssrc; |
Holger Hans Peter Freyther | eb0446b | 2010-08-04 21:45:39 +0800 | [diff] [blame] | 268 | LOGP(DMGCP, LOGL_NOTICE, |
Jacob Erlbeck | ac9f6ce | 2013-11-25 15:23:35 +0100 | [diff] [blame] | 269 | "The SSRC changed on 0x%x SSRC: %u offset: %d tsdelta: %d " |
| 270 | "from %s:%d in %d\n", |
Jacob Erlbeck | a2528da | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 271 | ENDPOINT_NUMBER(endp), state->in_stream.ssrc, |
Jacob Erlbeck | ac9f6ce | 2013-11-25 15:23:35 +0100 | [diff] [blame] | 272 | state->seq_offset, tsdelta, |
| 273 | inet_ntoa(addr->sin_addr), ntohs(addr->sin_port), |
| 274 | endp->conn_mode); |
Jacob Erlbeck | a2528da | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 275 | |
| 276 | state->in_stream.last_tsdelta = 0; |
| 277 | } else { |
| 278 | /* Compute current per-packet timestamp delta */ |
| 279 | check_rtp_timestamp(endp, &state->in_stream, rtp_end, addr, |
| 280 | seq, timestamp, "input", |
| 281 | &state->in_stream.last_tsdelta); |
Holger Hans Peter Freyther | 3040ed4 | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 282 | } |
Holger Hans Peter Freyther | e758d95 | 2010-07-29 02:38:39 +0800 | [diff] [blame] | 283 | |
Jacob Erlbeck | a2528da | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 284 | /* Save before patching */ |
| 285 | state->in_stream.last_timestamp = timestamp; |
| 286 | state->in_stream.last_seq = seq; |
| 287 | |
Holger Hans Peter Freyther | 3040ed4 | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 288 | /* apply the offset and store it back to the packet */ |
Holger Hans Peter Freyther | 6230eb2 | 2010-08-03 15:36:57 +0000 | [diff] [blame] | 289 | if (state->patch) { |
| 290 | seq += state->seq_offset; |
| 291 | rtp_hdr->sequence = htons(seq); |
| 292 | rtp_hdr->ssrc = state->orig_ssrc; |
Holger Hans Peter Freyther | 3040ed4 | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 293 | |
Holger Hans Peter Freyther | 6230eb2 | 2010-08-03 15:36:57 +0000 | [diff] [blame] | 294 | timestamp += state->timestamp_offset; |
| 295 | rtp_hdr->timestamp = htonl(timestamp); |
| 296 | } |
Holger Hans Peter Freyther | 3040ed4 | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 297 | |
Jacob Erlbeck | a2528da | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 298 | /* Check again, whether the timestamps are still valid */ |
Jacob Erlbeck | 5065aef | 2013-11-29 11:16:07 +0100 | [diff] [blame] | 299 | if (state->out_stream.ssrc == rtp_hdr->ssrc) |
| 300 | check_rtp_timestamp(endp, &state->out_stream, rtp_end, addr, |
| 301 | seq, timestamp, "output", |
| 302 | &state->out_stream.last_tsdelta); |
Jacob Erlbeck | a2528da | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 303 | |
Holger Hans Peter Freyther | 69849d1 | 2012-10-22 17:30:12 +0200 | [diff] [blame] | 304 | /* |
| 305 | * The below takes the shape of the validation from Appendix A. Check |
| 306 | * if there is something weird with the sequence number, otherwise check |
| 307 | * for a wrap around in the sequence number. |
Jacob Erlbeck | a2528da | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 308 | * |
| 309 | * Note that last_seq is used where the appendix mentions max_seq. |
Holger Hans Peter Freyther | 69849d1 | 2012-10-22 17:30:12 +0200 | [diff] [blame] | 310 | */ |
Jacob Erlbeck | a2528da | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 311 | udelta = seq - state->out_stream.last_seq; |
Holger Hans Peter Freyther | 69849d1 | 2012-10-22 17:30:12 +0200 | [diff] [blame] | 312 | if (udelta < RTP_MAX_DROPOUT) { |
Jacob Erlbeck | a2528da | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 313 | if (seq < state->out_stream.last_seq) |
Holger Hans Peter Freyther | 69849d1 | 2012-10-22 17:30:12 +0200 | [diff] [blame] | 314 | state->cycles += RTP_SEQ_MOD; |
Holger Hans Peter Freyther | 4ef2ef4 | 2012-12-16 13:07:45 +0100 | [diff] [blame] | 315 | } else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) { |
Holger Hans Peter Freyther | 69849d1 | 2012-10-22 17:30:12 +0200 | [diff] [blame] | 316 | LOGP(DMGCP, LOGL_NOTICE, |
| 317 | "RTP seqno made a very large jump on 0x%x delta: %u\n", |
| 318 | ENDPOINT_NUMBER(endp), udelta); |
| 319 | } |
| 320 | |
Holger Hans Peter Freyther | 13de794 | 2012-10-24 20:31:25 +0200 | [diff] [blame] | 321 | /* |
| 322 | * calculate the jitter between the two packages. The TS should be |
| 323 | * taken closer to the read function. This was taken from the |
| 324 | * Appendix A of RFC 3550. The local timestamp has a usec resolution. |
| 325 | */ |
| 326 | transit = arrival_time - timestamp; |
| 327 | d = transit - state->transit; |
| 328 | state->transit = transit; |
| 329 | if (d < 0) |
| 330 | d = -d; |
| 331 | state->jitter += d - ((state->jitter + 8) >> 4); |
| 332 | |
Jacob Erlbeck | a2528da | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 333 | /* Save output values */ |
| 334 | state->out_stream.last_seq = seq; |
| 335 | state->out_stream.last_timestamp = timestamp; |
| 336 | state->out_stream.ssrc = rtp_hdr->ssrc; |
Holger Hans Peter Freyther | 3040ed4 | 2010-08-03 14:35:50 +0000 | [diff] [blame] | 337 | |
Holger Hans Peter Freyther | 0052955 | 2010-04-06 11:15:50 +0200 | [diff] [blame] | 338 | if (payload < 0) |
| 339 | return; |
| 340 | |
Holger Hans Peter Freyther | f95e3ad | 2010-02-26 13:42:58 +0100 | [diff] [blame] | 341 | rtp_hdr->payload_type = payload; |
| 342 | } |
| 343 | |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 344 | /* |
Holger Hans Peter Freyther | cd144d5 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 345 | * The below code is for dispatching. We have a dedicated port for |
| 346 | * the data coming from the net and one to discover the BTS. |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 347 | */ |
Holger Hans Peter Freyther | 8fdb95e | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 348 | static int forward_data(int fd, struct mgcp_rtp_tap *tap, const char *buf, int len) |
| 349 | { |
| 350 | if (!tap->enabled) |
| 351 | return 0; |
| 352 | |
| 353 | return sendto(fd, buf, len, 0, |
| 354 | (struct sockaddr *)&tap->forward, sizeof(tap->forward)); |
| 355 | } |
Holger Hans Peter Freyther | 56ac269 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 356 | |
Pablo Neira Ayuso | 2a13ade | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 357 | static int mgcp_send_transcoder(struct mgcp_rtp_end *end, |
| 358 | struct mgcp_config *cfg, int is_rtp, |
| 359 | const char *buf, int len) |
Holger Hans Peter Freyther | 56ac269 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 360 | { |
| 361 | int rc; |
| 362 | int port; |
Holger Hans Peter Freyther | 56ac269 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 363 | struct sockaddr_in addr; |
| 364 | |
Holger Hans Peter Freyther | c9e3bc6 | 2010-11-01 22:38:25 +0100 | [diff] [blame] | 365 | port = is_rtp ? end->rtp_port : end->rtcp_port; |
Holger Hans Peter Freyther | 56ac269 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 366 | |
| 367 | addr.sin_family = AF_INET; |
| 368 | addr.sin_addr = cfg->transcoder_in; |
Holger Hans Peter Freyther | b1880ee | 2010-11-01 21:53:39 +0100 | [diff] [blame] | 369 | addr.sin_port = port; |
Holger Hans Peter Freyther | 56ac269 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 370 | |
| 371 | rc = sendto(is_rtp ? |
Holger Hans Peter Freyther | c9e3bc6 | 2010-11-01 22:38:25 +0100 | [diff] [blame] | 372 | end->rtp.fd : |
| 373 | end->rtcp.fd, buf, len, 0, |
Holger Hans Peter Freyther | 56ac269 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 374 | (struct sockaddr *) &addr, sizeof(addr)); |
| 375 | |
| 376 | if (rc != len) |
| 377 | LOGP(DMGCP, LOGL_ERROR, |
| 378 | "Failed to send data to the transcoder: %s\n", |
| 379 | strerror(errno)); |
| 380 | |
| 381 | return rc; |
| 382 | } |
| 383 | |
Pablo Neira Ayuso | 2a13ade | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 384 | static int mgcp_send(struct mgcp_endpoint *endp, int dest, int is_rtp, |
| 385 | struct sockaddr_in *addr, char *buf, int rc) |
Holger Hans Peter Freyther | cd144d5 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 386 | { |
Holger Hans Peter Freyther | ab56ce1 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 387 | struct mgcp_trunk_config *tcfg = endp->tcfg; |
Holger Hans Peter Freyther | 84026ba | 2010-08-05 06:31:58 +0800 | [diff] [blame] | 388 | /* For loop toggle the destination and then dispatch. */ |
Holger Hans Peter Freyther | ab56ce1 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 389 | if (tcfg->audio_loop) |
Holger Hans Peter Freyther | 84026ba | 2010-08-05 06:31:58 +0800 | [diff] [blame] | 390 | dest = !dest; |
| 391 | |
| 392 | /* Loop based on the conn_mode, maybe undoing the above */ |
| 393 | if (endp->conn_mode == MGCP_CONN_LOOPBACK) |
| 394 | dest = !dest; |
| 395 | |
Pablo Neira Ayuso | 2a13ade | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 396 | if (dest == MGCP_DEST_NET) { |
Holger Hans Peter Freyther | cd144d5 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 397 | if (is_rtp) { |
Jacob Erlbeck | 4516a8a | 2013-11-25 12:53:29 +0100 | [diff] [blame] | 398 | mgcp_patch_and_count(endp, &endp->bts_state, |
| 399 | &endp->net_end, |
| 400 | addr, buf, rc); |
Holger Hans Peter Freyther | 8fdb95e | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 401 | forward_data(endp->net_end.rtp.fd, |
| 402 | &endp->taps[MGCP_TAP_NET_OUT], buf, rc); |
Pablo Neira Ayuso | ad11ffe | 2013-08-25 17:39:38 +0200 | [diff] [blame] | 403 | return mgcp_udp_send(endp->net_end.rtp.fd, |
| 404 | &endp->net_end.addr, |
| 405 | endp->net_end.rtp_port, buf, rc); |
Holger Hans Peter Freyther | 51ade84 | 2012-05-11 13:00:45 +0200 | [diff] [blame] | 406 | } else if (!tcfg->omit_rtcp) { |
Pablo Neira Ayuso | ad11ffe | 2013-08-25 17:39:38 +0200 | [diff] [blame] | 407 | return mgcp_udp_send(endp->net_end.rtcp.fd, |
| 408 | &endp->net_end.addr, |
| 409 | endp->net_end.rtcp_port, buf, rc); |
Holger Hans Peter Freyther | cd144d5 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 410 | } |
| 411 | } else { |
| 412 | if (is_rtp) { |
Jacob Erlbeck | 4516a8a | 2013-11-25 12:53:29 +0100 | [diff] [blame] | 413 | mgcp_patch_and_count(endp, &endp->net_state, |
| 414 | &endp->bts_end, |
| 415 | addr, buf, rc); |
Holger Hans Peter Freyther | 8fdb95e | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 416 | forward_data(endp->bts_end.rtp.fd, |
| 417 | &endp->taps[MGCP_TAP_BTS_OUT], buf, rc); |
Pablo Neira Ayuso | ad11ffe | 2013-08-25 17:39:38 +0200 | [diff] [blame] | 418 | return mgcp_udp_send(endp->bts_end.rtp.fd, |
| 419 | &endp->bts_end.addr, |
| 420 | endp->bts_end.rtp_port, buf, rc); |
Holger Hans Peter Freyther | 51ade84 | 2012-05-11 13:00:45 +0200 | [diff] [blame] | 421 | } else if (!tcfg->omit_rtcp) { |
Pablo Neira Ayuso | ad11ffe | 2013-08-25 17:39:38 +0200 | [diff] [blame] | 422 | return mgcp_udp_send(endp->bts_end.rtcp.fd, |
| 423 | &endp->bts_end.addr, |
| 424 | endp->bts_end.rtcp_port, buf, rc); |
Holger Hans Peter Freyther | cd144d5 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 425 | } |
| 426 | } |
Holger Hans Peter Freyther | b8d5ca5 | 2012-09-11 12:31:25 +0200 | [diff] [blame] | 427 | |
| 428 | return 0; |
Holger Hans Peter Freyther | cd144d5 | 2010-08-05 06:19:24 +0800 | [diff] [blame] | 429 | } |
| 430 | |
Holger Hans Peter Freyther | 5444481 | 2011-06-02 19:04:22 +0200 | [diff] [blame] | 431 | static int receive_from(struct mgcp_endpoint *endp, int fd, struct sockaddr_in *addr, |
| 432 | char *buf, int bufsize) |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 433 | { |
Holger Hans Peter Freyther | 10b2e7a | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 434 | int rc; |
| 435 | socklen_t slen = sizeof(*addr); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 436 | |
Holger Hans Peter Freyther | 10b2e7a | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 437 | rc = recvfrom(fd, buf, bufsize, 0, |
| 438 | (struct sockaddr *) addr, &slen); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 439 | if (rc < 0) { |
Holger Hans Peter Freyther | 9d7bd81 | 2010-04-07 12:55:40 +0200 | [diff] [blame] | 440 | LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x errno: %d/%s\n", |
| 441 | ENDPOINT_NUMBER(endp), errno, strerror(errno)); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 442 | return -1; |
| 443 | } |
| 444 | |
| 445 | /* do not forward aynthing... maybe there is a packet from the bts */ |
Holger Hans Peter Freyther | e15223b | 2010-08-06 18:03:11 +0800 | [diff] [blame] | 446 | if (!endp->allocated) |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 447 | return -1; |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 448 | |
Holger Hans Peter Freyther | 10b2e7a | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 449 | #warning "Slight spec violation. With connection mode recvonly we should attempt to forward." |
| 450 | |
| 451 | return rc; |
| 452 | } |
| 453 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 454 | static int rtp_data_net(struct osmo_fd *fd, unsigned int what) |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 455 | { |
| 456 | char buf[4096]; |
| 457 | struct sockaddr_in addr; |
| 458 | struct mgcp_endpoint *endp; |
| 459 | int rc, proto; |
| 460 | |
| 461 | endp = (struct mgcp_endpoint *) fd->data; |
| 462 | |
Holger Hans Peter Freyther | 5444481 | 2011-06-02 19:04:22 +0200 | [diff] [blame] | 463 | rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf)); |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 464 | if (rc <= 0) |
| 465 | return -1; |
| 466 | |
| 467 | if (memcmp(&addr.sin_addr, &endp->net_end.addr, sizeof(addr.sin_addr)) != 0) { |
| 468 | LOGP(DMGCP, LOGL_ERROR, |
Holger Hans Peter Freyther | f90f8cc | 2012-11-12 10:55:29 +0100 | [diff] [blame] | 469 | "Endpoint 0x%x data from wrong address %s vs. ", |
| 470 | ENDPOINT_NUMBER(endp), inet_ntoa(addr.sin_addr)); |
| 471 | LOGPC(DMGCP, LOGL_ERROR, |
| 472 | "%s\n", inet_ntoa(endp->net_end.addr)); |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 473 | return -1; |
| 474 | } |
| 475 | |
| 476 | if (endp->net_end.rtp_port != addr.sin_port && |
| 477 | endp->net_end.rtcp_port != addr.sin_port) { |
| 478 | LOGP(DMGCP, LOGL_ERROR, |
| 479 | "Data from wrong source port %d on 0x%x\n", |
| 480 | ntohs(addr.sin_port), ENDPOINT_NUMBER(endp)); |
| 481 | return -1; |
| 482 | } |
| 483 | |
| 484 | /* throw away the dummy message */ |
Pablo Neira Ayuso | 2a13ade | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 485 | if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) { |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 486 | LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from network on 0x%x\n", |
| 487 | ENDPOINT_NUMBER(endp)); |
| 488 | return 0; |
| 489 | } |
| 490 | |
Pablo Neira Ayuso | 2a13ade | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 491 | proto = fd == &endp->net_end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP; |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 492 | endp->net_end.packets += 1; |
Holger Hans Peter Freyther | 3e45610 | 2012-09-12 11:38:37 +0200 | [diff] [blame] | 493 | endp->net_end.octets += rc; |
Holger Hans Peter Freyther | 8fdb95e | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 494 | |
| 495 | forward_data(fd->fd, &endp->taps[MGCP_TAP_NET_IN], buf, rc); |
Pablo Neira Ayuso | a3f26c0 | 2013-07-08 05:09:46 +0200 | [diff] [blame] | 496 | |
| 497 | switch (endp->type) { |
| 498 | case MGCP_RTP_DEFAULT: |
Pablo Neira Ayuso | 2a13ade | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 499 | return mgcp_send(endp, MGCP_DEST_BTS, proto == MGCP_PROTO_RTP, |
| 500 | &addr, buf, rc); |
Pablo Neira Ayuso | a3f26c0 | 2013-07-08 05:09:46 +0200 | [diff] [blame] | 501 | case MGCP_RTP_TRANSCODED: |
Pablo Neira Ayuso | 2a13ade | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 502 | return mgcp_send_transcoder(&endp->trans_net, endp->cfg, |
| 503 | proto == MGCP_PROTO_RTP, buf, rc); |
Pablo Neira Ayuso | a3f26c0 | 2013-07-08 05:09:46 +0200 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | LOGP(DMGCP, LOGL_ERROR, "Bad MGCP type %u on endpoint %u\n", |
| 507 | endp->type, ENDPOINT_NUMBER(endp)); |
| 508 | return 0; |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 509 | } |
| 510 | |
Holger Hans Peter Freyther | 6769ea0 | 2010-08-06 07:48:41 +0800 | [diff] [blame] | 511 | static void discover_bts(struct mgcp_endpoint *endp, int proto, struct sockaddr_in *addr) |
| 512 | { |
| 513 | struct mgcp_config *cfg = endp->cfg; |
| 514 | |
Pablo Neira Ayuso | 2a13ade | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 515 | if (proto == MGCP_PROTO_RTP && endp->bts_end.rtp_port == 0) { |
Holger Hans Peter Freyther | 6769ea0 | 2010-08-06 07:48:41 +0800 | [diff] [blame] | 516 | if (!cfg->bts_ip || |
| 517 | memcmp(&addr->sin_addr, |
| 518 | &cfg->bts_in, sizeof(cfg->bts_in)) == 0 || |
| 519 | memcmp(&addr->sin_addr, |
| 520 | &endp->bts_end.addr, sizeof(endp->bts_end.addr)) == 0) { |
| 521 | |
| 522 | endp->bts_end.rtp_port = addr->sin_port; |
| 523 | endp->bts_end.addr = addr->sin_addr; |
| 524 | |
| 525 | LOGP(DMGCP, LOGL_NOTICE, |
| 526 | "Found BTS for endpoint: 0x%x on port: %d/%d of %s\n", |
| 527 | ENDPOINT_NUMBER(endp), ntohs(endp->bts_end.rtp_port), |
| 528 | ntohs(endp->bts_end.rtcp_port), inet_ntoa(addr->sin_addr)); |
| 529 | } |
Pablo Neira Ayuso | 2a13ade | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 530 | } else if (proto == MGCP_PROTO_RTCP && endp->bts_end.rtcp_port == 0) { |
Holger Hans Peter Freyther | 6769ea0 | 2010-08-06 07:48:41 +0800 | [diff] [blame] | 531 | if (memcmp(&endp->bts_end.addr, &addr->sin_addr, |
| 532 | sizeof(endp->bts_end.addr)) == 0) { |
| 533 | endp->bts_end.rtcp_port = addr->sin_port; |
| 534 | } |
| 535 | } |
| 536 | } |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 537 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 538 | static int rtp_data_bts(struct osmo_fd *fd, unsigned int what) |
Holger Hans Peter Freyther | 10b2e7a | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 539 | { |
| 540 | char buf[4096]; |
| 541 | struct sockaddr_in addr; |
| 542 | struct mgcp_endpoint *endp; |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 543 | int rc, proto; |
Holger Hans Peter Freyther | 10b2e7a | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 544 | |
| 545 | endp = (struct mgcp_endpoint *) fd->data; |
Holger Hans Peter Freyther | 10b2e7a | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 546 | |
Holger Hans Peter Freyther | 5444481 | 2011-06-02 19:04:22 +0200 | [diff] [blame] | 547 | rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf)); |
Holger Hans Peter Freyther | 10b2e7a | 2010-08-05 06:27:22 +0800 | [diff] [blame] | 548 | if (rc <= 0) |
| 549 | return -1; |
| 550 | |
Pablo Neira Ayuso | 2a13ade | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 551 | proto = fd == &endp->bts_end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP; |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 552 | |
| 553 | /* We have no idea who called us, maybe it is the BTS. */ |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 554 | /* it was the BTS... */ |
Holger Hans Peter Freyther | 6769ea0 | 2010-08-06 07:48:41 +0800 | [diff] [blame] | 555 | discover_bts(endp, proto, &addr); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 556 | |
Holger Hans Peter Freyther | 935137c | 2010-08-05 10:53:26 +0000 | [diff] [blame] | 557 | if (memcmp(&endp->bts_end.addr, &addr.sin_addr, sizeof(addr.sin_addr)) != 0) { |
| 558 | LOGP(DMGCP, LOGL_ERROR, |
| 559 | "Data from wrong bts %s on 0x%x\n", |
| 560 | inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(endp)); |
| 561 | return -1; |
| 562 | } |
| 563 | |
| 564 | if (endp->bts_end.rtp_port != addr.sin_port && |
| 565 | endp->bts_end.rtcp_port != addr.sin_port) { |
| 566 | LOGP(DMGCP, LOGL_ERROR, |
| 567 | "Data from wrong bts source port %d on 0x%x\n", |
| 568 | ntohs(addr.sin_port), ENDPOINT_NUMBER(endp)); |
| 569 | return -1; |
| 570 | } |
| 571 | |
Holger Hans Peter Freyther | 906ed10 | 2010-05-14 02:18:59 +0800 | [diff] [blame] | 572 | /* throw away the dummy message */ |
Pablo Neira Ayuso | 2a13ade | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 573 | if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) { |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 574 | LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from bts on 0x%x\n", |
Holger Hans Peter Freyther | 040c130 | 2010-04-22 12:14:51 +0800 | [diff] [blame] | 575 | ENDPOINT_NUMBER(endp)); |
| 576 | return 0; |
| 577 | } |
| 578 | |
Holger Hans Peter Freyther | 6de5b11 | 2010-04-07 09:37:17 +0200 | [diff] [blame] | 579 | /* do this before the loop handling */ |
Holger Hans Peter Freyther | 77eae16 | 2010-08-05 06:55:58 +0800 | [diff] [blame] | 580 | endp->bts_end.packets += 1; |
Holger Hans Peter Freyther | 3e45610 | 2012-09-12 11:38:37 +0200 | [diff] [blame] | 581 | endp->bts_end.octets += rc; |
Holger Hans Peter Freyther | 6de5b11 | 2010-04-07 09:37:17 +0200 | [diff] [blame] | 582 | |
Holger Hans Peter Freyther | 8fdb95e | 2010-08-06 01:12:21 +0800 | [diff] [blame] | 583 | forward_data(fd->fd, &endp->taps[MGCP_TAP_BTS_IN], buf, rc); |
Pablo Neira Ayuso | a3f26c0 | 2013-07-08 05:09:46 +0200 | [diff] [blame] | 584 | |
| 585 | switch (endp->type) { |
| 586 | case MGCP_RTP_DEFAULT: |
Pablo Neira Ayuso | 2a13ade | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 587 | return mgcp_send(endp, MGCP_DEST_NET, proto == MGCP_PROTO_RTP, |
| 588 | &addr, buf, rc); |
Pablo Neira Ayuso | a3f26c0 | 2013-07-08 05:09:46 +0200 | [diff] [blame] | 589 | case MGCP_RTP_TRANSCODED: |
Pablo Neira Ayuso | 2a13ade | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 590 | return mgcp_send_transcoder(&endp->trans_bts, endp->cfg, |
| 591 | proto == MGCP_PROTO_RTP, buf, rc); |
Pablo Neira Ayuso | a3f26c0 | 2013-07-08 05:09:46 +0200 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | LOGP(DMGCP, LOGL_ERROR, "Bad MGCP type %u on endpoint %u\n", |
| 595 | endp->type, ENDPOINT_NUMBER(endp)); |
| 596 | return 0; |
Holger Hans Peter Freyther | 56ac269 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 597 | } |
| 598 | |
Holger Hans Peter Freyther | 4f2083b | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 599 | static int rtp_data_transcoder(struct mgcp_rtp_end *end, struct mgcp_endpoint *_endp, |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 600 | int dest, struct osmo_fd *fd) |
Holger Hans Peter Freyther | 56ac269 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 601 | { |
| 602 | char buf[4096]; |
| 603 | struct sockaddr_in addr; |
Holger Hans Peter Freyther | 56ac269 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 604 | struct mgcp_config *cfg; |
| 605 | int rc, proto; |
| 606 | |
Holger Hans Peter Freyther | 4f2083b | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 607 | cfg = _endp->cfg; |
Holger Hans Peter Freyther | 5444481 | 2011-06-02 19:04:22 +0200 | [diff] [blame] | 608 | rc = receive_from(_endp, fd->fd, &addr, buf, sizeof(buf)); |
Holger Hans Peter Freyther | 56ac269 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 609 | if (rc <= 0) |
| 610 | return -1; |
| 611 | |
Pablo Neira Ayuso | 2a13ade | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 612 | proto = fd == &end->rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP; |
Holger Hans Peter Freyther | 56ac269 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 613 | |
| 614 | if (memcmp(&addr.sin_addr, &cfg->transcoder_in, sizeof(addr.sin_addr)) != 0) { |
| 615 | LOGP(DMGCP, LOGL_ERROR, |
Holger Hans Peter Freyther | 4f2083b | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 616 | "Data not coming from transcoder dest: %d %s on 0x%x\n", |
| 617 | dest, inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(_endp)); |
Holger Hans Peter Freyther | 56ac269 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 618 | return -1; |
| 619 | } |
| 620 | |
Holger Hans Peter Freyther | 4f2083b | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 621 | if (end->rtp_port != addr.sin_port && |
| 622 | end->rtcp_port != addr.sin_port) { |
Holger Hans Peter Freyther | 56ac269 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 623 | LOGP(DMGCP, LOGL_ERROR, |
Holger Hans Peter Freyther | 4f2083b | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 624 | "Data from wrong transcoder dest %d source port %d on 0x%x\n", |
| 625 | dest, ntohs(addr.sin_port), ENDPOINT_NUMBER(_endp)); |
Holger Hans Peter Freyther | 56ac269 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 626 | return -1; |
| 627 | } |
| 628 | |
| 629 | /* throw away the dummy message */ |
Pablo Neira Ayuso | 2a13ade | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 630 | if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) { |
Holger Hans Peter Freyther | 4f2083b | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 631 | LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from transcoder dest %d on 0x%x\n", |
| 632 | dest, ENDPOINT_NUMBER(_endp)); |
Holger Hans Peter Freyther | 56ac269 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 633 | return 0; |
| 634 | } |
| 635 | |
Holger Hans Peter Freyther | 4f2083b | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 636 | end->packets += 1; |
Pablo Neira Ayuso | 2a13ade | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 637 | return mgcp_send(_endp, dest, proto == MGCP_PROTO_RTP, &addr, buf, rc); |
Holger Hans Peter Freyther | 4f2083b | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 638 | } |
| 639 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 640 | static int rtp_data_trans_net(struct osmo_fd *fd, unsigned int what) |
Holger Hans Peter Freyther | 4f2083b | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 641 | { |
| 642 | struct mgcp_endpoint *endp; |
| 643 | endp = (struct mgcp_endpoint *) fd->data; |
| 644 | |
Pablo Neira Ayuso | 2a13ade | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 645 | return rtp_data_transcoder(&endp->trans_net, endp, MGCP_DEST_NET, fd); |
Holger Hans Peter Freyther | 4f2083b | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 646 | } |
| 647 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 648 | static int rtp_data_trans_bts(struct osmo_fd *fd, unsigned int what) |
Holger Hans Peter Freyther | 4f2083b | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 649 | { |
| 650 | struct mgcp_endpoint *endp; |
| 651 | endp = (struct mgcp_endpoint *) fd->data; |
| 652 | |
Pablo Neira Ayuso | 2a13ade | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 653 | return rtp_data_transcoder(&endp->trans_bts, endp, MGCP_DEST_BTS, fd); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 654 | } |
| 655 | |
Pablo Neira Ayuso | 2a13ade | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 656 | static int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, |
| 657 | int port) |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 658 | { |
| 659 | struct sockaddr_in addr; |
| 660 | int on = 1; |
| 661 | |
| 662 | fd->fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 663 | if (fd->fd < 0) { |
| 664 | LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n"); |
| 665 | return -1; |
| 666 | } |
| 667 | |
| 668 | setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 669 | memset(&addr, 0, sizeof(addr)); |
| 670 | addr.sin_family = AF_INET; |
| 671 | addr.sin_port = htons(port); |
| 672 | inet_aton(source_addr, &addr.sin_addr); |
| 673 | |
| 674 | if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
Holger Hans Peter Freyther | 1de4d84 | 2010-08-06 03:00:17 +0800 | [diff] [blame] | 675 | close(fd->fd); |
| 676 | fd->fd = -1; |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 677 | return -1; |
| 678 | } |
| 679 | |
| 680 | return 0; |
| 681 | } |
| 682 | |
Holger Hans Peter Freyther | 489d0a6 | 2010-05-31 10:22:00 +0800 | [diff] [blame] | 683 | static int set_ip_tos(int fd, int tos) |
| 684 | { |
| 685 | int ret; |
| 686 | ret = setsockopt(fd, IPPROTO_IP, IP_TOS, |
| 687 | &tos, sizeof(tos)); |
| 688 | return ret != 0; |
| 689 | } |
| 690 | |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 691 | static int bind_rtp(struct mgcp_config *cfg, struct mgcp_rtp_end *rtp_end, int endpno) |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 692 | { |
Pablo Neira Ayuso | 2a13ade | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 693 | if (mgcp_create_bind(cfg->source_addr, &rtp_end->rtp, |
| 694 | rtp_end->local_port) != 0) { |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 695 | LOGP(DMGCP, LOGL_ERROR, "Failed to create RTP port: %s:%d on 0x%x\n", |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 696 | cfg->source_addr, rtp_end->local_port, endpno); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 697 | goto cleanup0; |
| 698 | } |
| 699 | |
Pablo Neira Ayuso | 2a13ade | 2013-08-14 15:55:45 +0200 | [diff] [blame] | 700 | if (mgcp_create_bind(cfg->source_addr, &rtp_end->rtcp, |
| 701 | rtp_end->local_port + 1) != 0) { |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 702 | LOGP(DMGCP, LOGL_ERROR, "Failed to create RTCP port: %s:%d on 0x%x\n", |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 703 | cfg->source_addr, rtp_end->local_port + 1, endpno); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 704 | goto cleanup1; |
| 705 | } |
| 706 | |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 707 | set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp); |
| 708 | set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp); |
Holger Hans Peter Freyther | 489d0a6 | 2010-05-31 10:22:00 +0800 | [diff] [blame] | 709 | |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 710 | rtp_end->rtp.when = BSC_FD_READ; |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 711 | if (osmo_fd_register(&rtp_end->rtp) != 0) { |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 712 | LOGP(DMGCP, LOGL_ERROR, "Failed to register RTP port %d on 0x%x\n", |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 713 | rtp_end->local_port, endpno); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 714 | goto cleanup2; |
| 715 | } |
| 716 | |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 717 | rtp_end->rtcp.when = BSC_FD_READ; |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 718 | if (osmo_fd_register(&rtp_end->rtcp) != 0) { |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 719 | LOGP(DMGCP, LOGL_ERROR, "Failed to register RTCP port %d on 0x%x\n", |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 720 | rtp_end->local_port + 1, endpno); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 721 | goto cleanup3; |
| 722 | } |
| 723 | |
| 724 | return 0; |
| 725 | |
| 726 | cleanup3: |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 727 | osmo_fd_unregister(&rtp_end->rtp); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 728 | cleanup2: |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 729 | close(rtp_end->rtcp.fd); |
| 730 | rtp_end->rtcp.fd = -1; |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 731 | cleanup1: |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 732 | close(rtp_end->rtp.fd); |
| 733 | rtp_end->rtp.fd = -1; |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 734 | cleanup0: |
| 735 | return -1; |
| 736 | } |
| 737 | |
Holger Hans Peter Freyther | 18f6787 | 2010-11-01 21:15:22 +0100 | [diff] [blame] | 738 | static int int_bind(const char *port, |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 739 | struct mgcp_rtp_end *end, int (*cb)(struct osmo_fd *, unsigned), |
Holger Hans Peter Freyther | 18f6787 | 2010-11-01 21:15:22 +0100 | [diff] [blame] | 740 | struct mgcp_endpoint *_endp, int rtp_port) |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 741 | { |
Holger Hans Peter Freyther | 18f6787 | 2010-11-01 21:15:22 +0100 | [diff] [blame] | 742 | if (end->rtp.fd != -1 || end->rtcp.fd != -1) { |
| 743 | LOGP(DMGCP, LOGL_ERROR, "Previous %s was still bound on %d\n", |
| 744 | port, ENDPOINT_NUMBER(_endp)); |
| 745 | mgcp_free_rtp_port(end); |
Holger Hans Peter Freyther | 6af36c0 | 2010-08-06 07:18:22 +0800 | [diff] [blame] | 746 | } |
| 747 | |
Holger Hans Peter Freyther | 18f6787 | 2010-11-01 21:15:22 +0100 | [diff] [blame] | 748 | end->local_port = rtp_port; |
| 749 | end->rtp.cb = cb; |
| 750 | end->rtp.data = _endp; |
| 751 | end->rtcp.data = _endp; |
| 752 | end->rtcp.cb = cb; |
| 753 | return bind_rtp(_endp->cfg, end, ENDPOINT_NUMBER(_endp)); |
| 754 | } |
| 755 | |
| 756 | |
| 757 | int mgcp_bind_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port) |
| 758 | { |
| 759 | return int_bind("bts-port", &endp->bts_end, |
| 760 | rtp_data_bts, endp, rtp_port); |
Holger Hans Peter Freyther | 81938e9 | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port) |
| 764 | { |
Holger Hans Peter Freyther | 18f6787 | 2010-11-01 21:15:22 +0100 | [diff] [blame] | 765 | return int_bind("net-port", &endp->net_end, |
| 766 | rtp_data_net, endp, rtp_port); |
Holger Hans Peter Freyther | 5ada53a | 2010-02-21 11:11:04 +0100 | [diff] [blame] | 767 | } |
Holger Hans Peter Freyther | 05d8f5f | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 768 | |
Holger Hans Peter Freyther | c983f83 | 2010-11-01 21:04:54 +0100 | [diff] [blame] | 769 | int mgcp_bind_trans_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port) |
Holger Hans Peter Freyther | 56ac269 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 770 | { |
Holger Hans Peter Freyther | 18f6787 | 2010-11-01 21:15:22 +0100 | [diff] [blame] | 771 | return int_bind("trans-net", &endp->trans_net, |
Holger Hans Peter Freyther | 4f2083b | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 772 | rtp_data_trans_net, endp, rtp_port); |
Holger Hans Peter Freyther | 56ac269 | 2010-09-18 02:30:02 +0800 | [diff] [blame] | 773 | } |
| 774 | |
Holger Hans Peter Freyther | c983f83 | 2010-11-01 21:04:54 +0100 | [diff] [blame] | 775 | int mgcp_bind_trans_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port) |
| 776 | { |
Holger Hans Peter Freyther | 18f6787 | 2010-11-01 21:15:22 +0100 | [diff] [blame] | 777 | return int_bind("trans-bts", &endp->trans_bts, |
Holger Hans Peter Freyther | 4f2083b | 2010-11-01 21:25:33 +0100 | [diff] [blame] | 778 | rtp_data_trans_bts, endp, rtp_port); |
Holger Hans Peter Freyther | c983f83 | 2010-11-01 21:04:54 +0100 | [diff] [blame] | 779 | } |
| 780 | |
Holger Hans Peter Freyther | 05d8f5f | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 781 | int mgcp_free_rtp_port(struct mgcp_rtp_end *end) |
| 782 | { |
Holger Hans Peter Freyther | 05d8f5f | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 783 | if (end->rtp.fd != -1) { |
| 784 | close(end->rtp.fd); |
| 785 | end->rtp.fd = -1; |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 786 | osmo_fd_unregister(&end->rtp); |
Holger Hans Peter Freyther | 05d8f5f | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | if (end->rtcp.fd != -1) { |
| 790 | close(end->rtcp.fd); |
| 791 | end->rtcp.fd = -1; |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 792 | osmo_fd_unregister(&end->rtcp); |
Holger Hans Peter Freyther | 05d8f5f | 2010-08-05 08:08:17 +0800 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | return 0; |
| 796 | } |
Holger Hans Peter Freyther | faa6793 | 2012-10-22 18:09:35 +0200 | [diff] [blame] | 797 | |
| 798 | |
| 799 | void mgcp_state_calc_loss(struct mgcp_rtp_state *state, |
| 800 | struct mgcp_rtp_end *end, uint32_t *expected, |
| 801 | int *loss) |
| 802 | { |
Jacob Erlbeck | a2528da | 2013-11-25 12:53:28 +0100 | [diff] [blame] | 803 | *expected = state->cycles + state->out_stream.last_seq; |
Holger Hans Peter Freyther | faa6793 | 2012-10-22 18:09:35 +0200 | [diff] [blame] | 804 | *expected = *expected - state->base_seq + 1; |
| 805 | |
| 806 | if (!state->initialized) { |
| 807 | *expected = 0; |
| 808 | *loss = 0; |
| 809 | return; |
| 810 | } |
| 811 | |
| 812 | /* |
| 813 | * Make sure the sign is correct and use the biggest |
| 814 | * positive/negative number that fits. |
| 815 | */ |
| 816 | *loss = *expected - end->packets; |
| 817 | if (*expected < end->packets) { |
| 818 | if (*loss > 0) |
| 819 | *loss = INT_MIN; |
| 820 | } else { |
| 821 | if (*loss < 0) |
| 822 | *loss = INT_MAX; |
| 823 | } |
| 824 | } |
Holger Hans Peter Freyther | 8c87a48 | 2012-10-24 21:22:47 +0200 | [diff] [blame] | 825 | |
| 826 | uint32_t mgcp_state_calc_jitter(struct mgcp_rtp_state *state) |
| 827 | { |
| 828 | if (!state->initialized) |
| 829 | return 0; |
| 830 | return state->jitter >> 4; |
| 831 | } |