Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 1 | module RTP_Emulation { |
| 2 | |
| 3 | /* Functionalities that we want this module to imeplement: |
| 4 | * * act as a RTP source that generates a RTP Stream |
| 5 | * * act asaa RTP sink that consumes a RTP Stream |
| 6 | * |
Pau Espin Pedrol | 08005d7 | 2020-09-08 13:16:14 +0200 | [diff] [blame] | 7 | * for all of the above, we want to be able to |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 8 | * * specify the payload type |
| 9 | * * specify the interval / sample rate |
| 10 | * * create drop-outs in the stream |
| 11 | * * detect reordered or lost frames |
| 12 | * * validate if the size of the frames matches epectations |
| 13 | * * play back real audio (at least some tones?) |
| 14 | * * enable/disable generation/verification of RTCP |
| 15 | */ |
| 16 | |
Harald Welte | 34b5a95 | 2019-05-27 11:54:11 +0200 | [diff] [blame] | 17 | /* (C) 2017-2018 Harald Welte <laforge@gnumonks.org> |
| 18 | * (C) 2018-2019 sysmocom - s.f.m.c. GmbH |
| 19 | * All rights reserved. |
| 20 | * |
| 21 | * Released under the terms of GNU General Public License, Version 2 or |
| 22 | * (at your option) any later version. |
| 23 | * |
| 24 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 25 | */ |
| 26 | |
| 27 | |
Harald Welte | a4ca446 | 2018-02-09 00:17:14 +0100 | [diff] [blame] | 28 | /* Ideas: |
| 29 | |
| 30 | * each component consists of transmitter and receiver |
| 31 | * transmitters and receivers can be operated as tuple? |
| 32 | * high-level operation |
| 33 | ** set-up config at transmitter + receiver |
| 34 | ** transmit sequence of payloads |
| 35 | ** verify receiption of those payloads |
| 36 | * can operate full-duplex/bi-directional as needed |
| 37 | |
| 38 | * transmitter |
| 39 | ** trigger transmission of n number of packets |
| 40 | ** transmit them at normal ptime interval |
| 41 | ** payload size configurable |
| 42 | ** payload contents PRBS or the like |
| 43 | |
| 44 | * receiver |
| 45 | ** count number of related packets at receiver |
| 46 | ** check received payload type |
| 47 | ** check received timestamp increments |
| 48 | ** check received seq_nr increments |
| 49 | ** (optionally) check for SSRC |
| 50 | ** (optionally) check for payload size |
| 51 | ** (optionally) check for payload contents |
| 52 | |
| 53 | * later |
| 54 | ** how to test transcoding? |
| 55 | ** how to test pure play-out endpoints (rx only)? |
| 56 | ** how to test "Rx from wrong IP/port" scenarios? |
| 57 | ** how to test RTCP? |
| 58 | ** maybe keep ports un-connected to show wrong src -lrt |
| 59 | |
| 60 | */ |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 65 | import from General_Types all; |
| 66 | import from Osmocom_Types all; |
| 67 | import from IPL4asp_Types all; |
| 68 | import from RTP_Types all; |
| 69 | import from RTP_CodecPort all; |
| 70 | import from RTP_CodecPort_CtrlFunct all; |
| 71 | |
Harald Welte | 8098164 | 2017-12-24 23:59:47 +0100 | [diff] [blame] | 72 | import from IuUP_Types all; |
| 73 | import from IuUP_Emulation all; |
| 74 | |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 75 | type component RTP_Emulation_CT { |
| 76 | /* down-facing ports for RTP and RTCP codec ports on top of IPL4asp */ |
| 77 | port RTP_CODEC_PT RTP; |
| 78 | var integer g_rtp_conn_id := -1; |
| 79 | port RTP_CODEC_PT RTCP; |
| 80 | var integer g_rtcp_conn_id := -1; |
| 81 | |
| 82 | /* user-facing port for controlling the binding */ |
| 83 | port RTPEM_CTRL_PT CTRL; |
| 84 | |
| 85 | /* configurable by user, should be fixed */ |
Harald Welte | 8098164 | 2017-12-24 23:59:47 +0100 | [diff] [blame] | 86 | var RtpemConfig g_cfg := c_RtpemDefaultCfg; |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 87 | |
Harald Welte | cb8b427 | 2018-03-28 19:57:58 +0200 | [diff] [blame] | 88 | /* statistics */ |
| 89 | var RtpemStats g_stats_rtp := c_RtpemStatsReset; |
| 90 | var RtpemStats g_stats_rtcp := c_RtpemStatsReset; |
| 91 | |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 92 | var HostName g_remote_host; |
| 93 | var PortNumber g_remote_port; |
| 94 | var HostName g_local_host; |
| 95 | var PortNumber g_local_port; |
| 96 | |
| 97 | /* state variables, change over time */ |
| 98 | var boolean g_rx_enabled := false; |
Pau Espin Pedrol | e38bfe0 | 2019-05-17 18:40:43 +0200 | [diff] [blame] | 99 | var boolean g_tx_connected := false; /* Set to true after connect() */ |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 100 | var LIN2_BO_LAST g_tx_next_seq := 0; |
| 101 | var uint32_t g_tx_next_ts := 0; |
| 102 | |
| 103 | var INT7b g_rx_payload_type := 0; |
| 104 | var LIN2_BO_LAST g_rx_last_seq; |
| 105 | var uint32_t g_rx_last_ts; |
Harald Welte | 8098164 | 2017-12-24 23:59:47 +0100 | [diff] [blame] | 106 | |
| 107 | var IuUP_Entity g_iuup_ent; // := valueof(t_IuUP_Entity(1)); |
Oliver Smith | 216019f | 2019-06-26 12:21:38 +0200 | [diff] [blame] | 108 | |
| 109 | var boolean g_conn_refuse_expect := false; |
| 110 | var boolean g_conn_refuse_received := false; |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | type enumerated RtpemMode { |
| 114 | RTPEM_MODE_NONE, |
| 115 | RTPEM_MODE_TXONLY, |
| 116 | RTPEM_MODE_RXONLY, |
| 117 | RTPEM_MODE_BIDIR |
| 118 | }; |
| 119 | |
Harald Welte | cb8b427 | 2018-03-28 19:57:58 +0200 | [diff] [blame] | 120 | type record RtpemStats { |
| 121 | /* number of packets transmitted */ |
| 122 | integer num_pkts_tx, |
| 123 | /* number of RTP payload bytes transmitted */ |
| 124 | integer bytes_payload_tx, |
| 125 | |
| 126 | /* number of packets received */ |
| 127 | integer num_pkts_rx, |
| 128 | /* number of RTP payload bytes received */ |
| 129 | integer bytes_payload_rx, |
| 130 | /* number of packets received out-of-sequence */ |
| 131 | integer num_pkts_rx_err_seq, |
| 132 | /* number of packets received wrong timestamp */ |
| 133 | integer num_pkts_rx_err_ts, |
Philipp Maier | c290d72 | 2018-07-24 18:51:36 +0200 | [diff] [blame] | 134 | /* number of packets received wrong payload type */ |
| 135 | integer num_pkts_rx_err_pt, |
Harald Welte | cb8b427 | 2018-03-28 19:57:58 +0200 | [diff] [blame] | 136 | /* number of packets received during Rx disable */ |
Philipp Maier | a071ee4 | 2019-02-21 16:17:32 +0100 | [diff] [blame] | 137 | integer num_pkts_rx_err_disabled, |
| 138 | /* number of packets received with mismatching payload */ |
| 139 | integer num_pkts_rx_err_payload |
Harald Welte | cb8b427 | 2018-03-28 19:57:58 +0200 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | const RtpemStats c_RtpemStatsReset := { |
| 143 | num_pkts_tx := 0, |
| 144 | bytes_payload_tx := 0, |
| 145 | num_pkts_rx := 0, |
| 146 | bytes_payload_rx := 0, |
| 147 | num_pkts_rx_err_seq := 0, |
| 148 | num_pkts_rx_err_ts := 0, |
Philipp Maier | c290d72 | 2018-07-24 18:51:36 +0200 | [diff] [blame] | 149 | num_pkts_rx_err_pt := 0, |
Philipp Maier | a071ee4 | 2019-02-21 16:17:32 +0100 | [diff] [blame] | 150 | num_pkts_rx_err_disabled := 0, |
| 151 | num_pkts_rx_err_payload := 0 |
Harald Welte | cb8b427 | 2018-03-28 19:57:58 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Harald Welte | 3f6f48f | 2017-12-24 21:48:33 +0100 | [diff] [blame] | 154 | type record RtpemConfig { |
| 155 | INT7b tx_payload_type, |
| 156 | integer tx_samplerate_hz, |
| 157 | integer tx_duration_ms, |
| 158 | BIT32_BO_LAST tx_ssrc, |
Harald Welte | 8098164 | 2017-12-24 23:59:47 +0100 | [diff] [blame] | 159 | octetstring tx_fixed_payload optional, |
Philipp Maier | a071ee4 | 2019-02-21 16:17:32 +0100 | [diff] [blame] | 160 | octetstring rx_fixed_payload optional, |
Harald Welte | 8098164 | 2017-12-24 23:59:47 +0100 | [diff] [blame] | 161 | boolean iuup_mode, |
| 162 | boolean iuup_tx_init |
Harald Welte | 3f6f48f | 2017-12-24 21:48:33 +0100 | [diff] [blame] | 163 | }; |
| 164 | |
Harald Welte | 8098164 | 2017-12-24 23:59:47 +0100 | [diff] [blame] | 165 | const RtpemConfig c_RtpemDefaultCfg := { |
Harald Welte | 3f6f48f | 2017-12-24 21:48:33 +0100 | [diff] [blame] | 166 | tx_payload_type := 0, |
| 167 | tx_samplerate_hz := 8000, |
| 168 | tx_duration_ms := 20, |
| 169 | tx_ssrc := '11011110101011011011111011101111'B, |
Harald Welte | 8098164 | 2017-12-24 23:59:47 +0100 | [diff] [blame] | 170 | tx_fixed_payload := '01020304'O, |
Philipp Maier | a071ee4 | 2019-02-21 16:17:32 +0100 | [diff] [blame] | 171 | rx_fixed_payload := '01020304'O, |
Harald Welte | 8098164 | 2017-12-24 23:59:47 +0100 | [diff] [blame] | 172 | iuup_mode := false, |
| 173 | iuup_tx_init := true |
Harald Welte | 3f6f48f | 2017-12-24 21:48:33 +0100 | [diff] [blame] | 174 | } |
| 175 | |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 176 | signature RTPEM_bind(in HostName local_host, inout PortNumber local_port); |
| 177 | signature RTPEM_connect(in HostName remote_host, in PortNumber remote_port); |
| 178 | signature RTPEM_mode(in RtpemMode mode); |
Harald Welte | 3f6f48f | 2017-12-24 21:48:33 +0100 | [diff] [blame] | 179 | signature RTPEM_configure(in RtpemConfig cfg); |
Harald Welte | cb8b427 | 2018-03-28 19:57:58 +0200 | [diff] [blame] | 180 | signature RTPEM_stats_get(out RtpemStats stats, in boolean rtcp); |
Oliver Smith | 216019f | 2019-06-26 12:21:38 +0200 | [diff] [blame] | 181 | signature RTPEM_conn_refuse_expect(in boolean expect); |
| 182 | signature RTPEM_conn_refuse_received(out boolean received); |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 183 | |
| 184 | type port RTPEM_CTRL_PT procedure { |
Oliver Smith | 216019f | 2019-06-26 12:21:38 +0200 | [diff] [blame] | 185 | inout RTPEM_bind, RTPEM_connect, RTPEM_mode, RTPEM_configure, RTPEM_stats_get, RTPEM_conn_refuse_expect, |
| 186 | RTPEM_conn_refuse_received; |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 187 | } with { extension "internal" }; |
| 188 | |
Harald Welte | 1af4033 | 2018-03-29 08:50:33 +0200 | [diff] [blame] | 189 | function f_rtpem_bind(RTPEM_CTRL_PT pt, in HostName local_host, inout PortNumber local_port) { |
| 190 | pt.call(RTPEM_bind:{local_host, local_port}) { |
| 191 | [] pt.getreply(RTPEM_bind:{local_host, ?}) -> param (local_port) {}; |
| 192 | } |
| 193 | } |
| 194 | function f_rtpem_connect(RTPEM_CTRL_PT pt, in HostName remote_host, in PortNumber remote_port) { |
| 195 | pt.call(RTPEM_connect:{remote_host, remote_port}) { |
| 196 | [] pt.getreply(RTPEM_connect:{remote_host, remote_port}) {}; |
| 197 | } |
| 198 | } |
| 199 | function f_rtpem_mode(RTPEM_CTRL_PT pt, in RtpemMode mode) { |
| 200 | pt.call(RTPEM_mode:{mode}) { |
| 201 | [] pt.getreply(RTPEM_mode:{mode}) {}; |
| 202 | } |
| 203 | } |
| 204 | function f_rtpem_configure(RTPEM_CTRL_PT pt, in RtpemConfig cfg) { |
| 205 | pt.call(RTPEM_configure:{cfg}) { |
| 206 | [] pt.getreply(RTPEM_configure:{cfg}) {}; |
| 207 | } |
| 208 | } |
| 209 | function f_rtpem_stats_get(RTPEM_CTRL_PT pt, boolean rtcp := false) return RtpemStats { |
| 210 | var RtpemStats stats; |
| 211 | pt.call(RTPEM_stats_get:{-, rtcp}) { |
| 212 | [] pt.getreply(RTPEM_stats_get:{?, rtcp}) -> param(stats) {}; |
| 213 | } |
| 214 | return stats; |
| 215 | } |
| 216 | |
Philipp Maier | 2321ef9 | 2018-06-27 17:52:04 +0200 | [diff] [blame] | 217 | function f_rtpem_stats_compare_value(integer a, integer b, integer tolerance := 0) return boolean { |
| 218 | var integer temp; |
Harald Welte | 5c49ba4 | 2018-03-29 08:51:20 +0200 | [diff] [blame] | 219 | |
Philipp Maier | 2321ef9 | 2018-06-27 17:52:04 +0200 | [diff] [blame] | 220 | temp := (a - b) |
| 221 | if (temp < 0) { |
| 222 | temp := -temp; |
| 223 | } |
| 224 | |
| 225 | if (temp > tolerance) { |
Harald Welte | 5c49ba4 | 2018-03-29 08:51:20 +0200 | [diff] [blame] | 226 | return false; |
| 227 | } |
Philipp Maier | 2321ef9 | 2018-06-27 17:52:04 +0200 | [diff] [blame] | 228 | |
Harald Welte | 5c49ba4 | 2018-03-29 08:51:20 +0200 | [diff] [blame] | 229 | return true; |
| 230 | } |
Harald Welte | 1af4033 | 2018-03-29 08:50:33 +0200 | [diff] [blame] | 231 | |
Philipp Maier | 2321ef9 | 2018-06-27 17:52:04 +0200 | [diff] [blame] | 232 | /* Cross-compare two rtpem-statistics. The transmission statistics on the a side |
| 233 | * must match the reception statistics on the other side and vice versa. The |
| 234 | * user may also supply a tolerance value (number of packets) when deviations |
| 235 | * are acceptable */ |
| 236 | function f_rtpem_stats_compare(RtpemStats a, RtpemStats b, integer tolerance := 0) return boolean { |
| 237 | var integer plen; |
| 238 | |
| 239 | log("stats A: ", a); |
| 240 | log("stats B: ", b); |
| 241 | log("tolerance: ", tolerance, " packets"); |
| 242 | |
| 243 | if (f_rtpem_stats_compare_value(a.num_pkts_tx, b.num_pkts_rx, tolerance) == false) { |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | if (f_rtpem_stats_compare_value(a.num_pkts_rx, b.num_pkts_tx, tolerance) == false) { |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | if(a.num_pkts_tx > 0) { |
| 252 | plen := a.bytes_payload_tx / a.num_pkts_tx; |
| 253 | } else { |
| 254 | plen := 0; |
| 255 | } |
| 256 | |
| 257 | if (f_rtpem_stats_compare_value(a.bytes_payload_tx, b.bytes_payload_rx, tolerance * plen) == false) { |
| 258 | return false; |
| 259 | } |
| 260 | |
| 261 | if (f_rtpem_stats_compare_value(a.bytes_payload_rx, b.bytes_payload_tx, tolerance * plen) == false) { |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | return true; |
| 266 | } |
Harald Welte | 1af4033 | 2018-03-29 08:50:33 +0200 | [diff] [blame] | 267 | |
Philipp Maier | 3629139 | 2018-07-25 09:40:44 +0200 | [diff] [blame] | 268 | /* Check the statistics for general signs of errors. This is a basic general |
| 269 | * check that will fit most situations and is intended to be executed by |
| 270 | * the testcases as as needed. */ |
| 271 | function f_rtpem_stats_err_check(RtpemStats s) { |
| 272 | log("stats: ", s); |
| 273 | |
| 274 | /* Check if there was some activity at either on the RX or on the |
| 275 | * TX side, but complete silence would indicate some problem */ |
| 276 | if (s.num_pkts_tx < 1 and s.num_pkts_rx < 1) { |
| 277 | setverdict(fail, "no RTP packet activity detected (packets)"); |
| 278 | mtc.stop; |
| 279 | } |
| 280 | if (s.bytes_payload_tx < 1 and s.bytes_payload_rx < 1) { |
| 281 | setverdict(fail, "no RTP packet activity detected (bytes)"); |
| 282 | mtc.stop; |
| 283 | } |
| 284 | |
| 285 | /* Check error counters */ |
| 286 | if (s.num_pkts_rx_err_seq != 0) { |
| 287 | setverdict(fail, "RTP packet sequence number errors occurred"); |
| 288 | mtc.stop; |
| 289 | } |
| 290 | if (s.num_pkts_rx_err_ts != 0) { |
| 291 | setverdict(fail, "RTP packet timestamp errors occurred"); |
| 292 | mtc.stop; |
| 293 | } |
| 294 | if (s.num_pkts_rx_err_pt != 0) { |
| 295 | setverdict(fail, "RTP packet payload type errors occurred"); |
| 296 | mtc.stop; |
| 297 | } |
| 298 | if (s.num_pkts_rx_err_disabled != 0) { |
| 299 | setverdict(fail, "RTP packets received while RX was disabled"); |
| 300 | mtc.stop; |
| 301 | } |
Philipp Maier | a071ee4 | 2019-02-21 16:17:32 +0100 | [diff] [blame] | 302 | if (s.num_pkts_rx_err_payload != 0) { |
| 303 | setverdict(fail, "RTP packets with mismatching payload received"); |
| 304 | mtc.stop; |
| 305 | } |
Philipp Maier | 3629139 | 2018-07-25 09:40:44 +0200 | [diff] [blame] | 306 | } |
| 307 | |
Oliver Smith | 216019f | 2019-06-26 12:21:38 +0200 | [diff] [blame] | 308 | function f_rtpem_conn_refuse_expect(RTPEM_CTRL_PT pt) { |
| 309 | pt.call(RTPEM_conn_refuse_expect:{true}) { |
| 310 | [] pt.getreply(RTPEM_conn_refuse_expect:{true}) {}; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | function f_rtpem_conn_refuse_verify(RTPEM_CTRL_PT pt) { |
| 315 | pt.call(RTPEM_conn_refuse_received:{?}) { |
| 316 | [] pt.getreply(RTPEM_conn_refuse_received:{true}) {}; |
| 317 | [] pt.getreply(RTPEM_conn_refuse_received:{false}) { |
| 318 | setverdict(fail, "Expected to receive connection refused"); |
| 319 | }; |
| 320 | } |
| 321 | } |
| 322 | |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 323 | template PDU_RTP ts_RTP(BIT32_BO_LAST ssrc, INT7b pt, LIN2_BO_LAST seq, uint32_t ts, |
| 324 | octetstring payload, BIT1 marker := '0'B) := { |
| 325 | version := 2, |
| 326 | padding_ind := '0'B, |
| 327 | extension_ind := '0'B, |
| 328 | CSRC_count := 0, |
| 329 | marker_bit := marker, |
| 330 | payload_type := pt, |
| 331 | sequence_number := seq, |
Harald Welte | 8a4d395 | 2017-12-24 21:30:23 +0100 | [diff] [blame] | 332 | time_stamp := int2bit(ts, 32), |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 333 | SSRC_id := ssrc, |
| 334 | CSRCs := omit, |
| 335 | ext_header := omit, |
| 336 | data := payload |
| 337 | } |
| 338 | |
| 339 | private function f_tx_rtp(octetstring payload, BIT1 marker := '0'B) runs on RTP_Emulation_CT { |
Harald Welte | 8098164 | 2017-12-24 23:59:47 +0100 | [diff] [blame] | 340 | if (g_cfg.iuup_mode) { |
| 341 | payload := f_IuUP_Em_tx_encap(g_iuup_ent, payload); |
| 342 | } |
Harald Welte | 3f6f48f | 2017-12-24 21:48:33 +0100 | [diff] [blame] | 343 | var PDU_RTP rtp := valueof(ts_RTP(g_cfg.tx_ssrc, g_cfg.tx_payload_type, g_tx_next_seq, |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 344 | g_tx_next_ts, payload, marker)); |
| 345 | RTP.send(t_RTP_Send(g_rtp_conn_id, RTP_messages_union:{rtp:=rtp})); |
| 346 | /* increment sequence + timestamp for next transmit */ |
| 347 | g_tx_next_seq := g_tx_next_seq + 1; |
Harald Welte | 3f6f48f | 2017-12-24 21:48:33 +0100 | [diff] [blame] | 348 | g_tx_next_ts := g_tx_next_ts + (g_cfg.tx_samplerate_hz / (1000 / g_cfg.tx_duration_ms)); |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | function f_main() runs on RTP_Emulation_CT |
| 352 | { |
| 353 | var Result res; |
Harald Welte | cb8b427 | 2018-03-28 19:57:58 +0200 | [diff] [blame] | 354 | var boolean is_rtcp |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 355 | |
Harald Welte | 3f6f48f | 2017-12-24 21:48:33 +0100 | [diff] [blame] | 356 | timer T_transmit := int2float(g_cfg.tx_duration_ms)/1000.0; |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 357 | var RTP_RecvFrom rx_rtp; |
Harald Welte | 3f6f48f | 2017-12-24 21:48:33 +0100 | [diff] [blame] | 358 | var RtpemConfig cfg; |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 359 | var template RTP_RecvFrom tr := { |
| 360 | connId := ?, |
| 361 | remName := ?, |
| 362 | remPort := ?, |
| 363 | locName := ?, |
| 364 | locPort := ?, |
| 365 | msg := ? |
| 366 | }; |
| 367 | var template RTP_RecvFrom tr_rtp := tr; |
| 368 | var template RTP_RecvFrom tr_rtcp := tr; |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 369 | tr_rtp.msg := { rtp := ? }; |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 370 | tr_rtcp.msg := { rtcp := ? }; |
| 371 | |
Oliver Smith | 216019f | 2019-06-26 12:21:38 +0200 | [diff] [blame] | 372 | var template ASP_Event tr_conn_refuse := {result := { errorCode := ERROR_SOCKET, |
| 373 | connId := ?, |
| 374 | os_error_code := 111, |
| 375 | os_error_text := ? /* "Connection refused" */}}; |
| 376 | |
Harald Welte | 8098164 | 2017-12-24 23:59:47 +0100 | [diff] [blame] | 377 | g_iuup_ent := valueof(t_IuUP_Entity(g_cfg.iuup_tx_init)); |
| 378 | |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 379 | while (true) { |
| 380 | alt { |
| 381 | /* control procedures (calls) from the user */ |
| 382 | [] CTRL.getcall(RTPEM_bind:{?,?}) -> param(g_local_host, g_local_port) { |
| 383 | if (g_local_port rem 2 == 1) { |
| 384 | //CTRL.raise(RTPEM_bind, "Local Port is not an even port number!"); |
| 385 | log("Local Port is not an even port number!"); |
| 386 | continue; |
| 387 | } |
Pau Espin Pedrol | e38bfe0 | 2019-05-17 18:40:43 +0200 | [diff] [blame] | 388 | |
| 389 | g_tx_connected := false; /* will set it back to true upon next connect() call */ |
Pau Espin Pedrol | 08005d7 | 2020-09-08 13:16:14 +0200 | [diff] [blame] | 390 | |
| 391 | if (g_rtp_conn_id != -1) { |
| 392 | res := RTP_CodecPort_CtrlFunct.f_IPL4_close(RTP, g_rtp_conn_id, {udp := {}}); |
| 393 | g_rtp_conn_id := -1; |
| 394 | } |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 395 | res := RTP_CodecPort_CtrlFunct.f_IPL4_listen(RTP, g_local_host, |
| 396 | g_local_port, {udp:={}}); |
Harald Welte | 9220f63 | 2018-05-23 20:27:02 +0200 | [diff] [blame] | 397 | if (not ispresent(res.connId)) { |
| 398 | setverdict(fail, "Could not listen on RTP socket, check your configuration"); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 399 | mtc.stop; |
Harald Welte | 9220f63 | 2018-05-23 20:27:02 +0200 | [diff] [blame] | 400 | } |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 401 | g_rtp_conn_id := res.connId; |
Harald Welte | 9774e7a | 2018-03-29 08:49:38 +0200 | [diff] [blame] | 402 | tr_rtp.connId := g_rtp_conn_id; |
Pau Espin Pedrol | 08005d7 | 2020-09-08 13:16:14 +0200 | [diff] [blame] | 403 | |
| 404 | if (g_rtcp_conn_id != -1) { |
| 405 | res := RTP_CodecPort_CtrlFunct.f_IPL4_close(RTCP, g_rtcp_conn_id, {udp := {}}); |
| 406 | g_rtcp_conn_id := -1; |
| 407 | } |
Harald Welte | 98eb1bf | 2018-04-02 18:18:44 +0200 | [diff] [blame] | 408 | res := RTP_CodecPort_CtrlFunct.f_IPL4_listen(RTCP, g_local_host, |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 409 | g_local_port+1, {udp:={}}); |
Harald Welte | 9220f63 | 2018-05-23 20:27:02 +0200 | [diff] [blame] | 410 | if (not ispresent(res.connId)) { |
| 411 | setverdict(fail, "Could not listen on RTCP socket, check your configuration"); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 412 | mtc.stop; |
Harald Welte | 9220f63 | 2018-05-23 20:27:02 +0200 | [diff] [blame] | 413 | } |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 414 | g_rtcp_conn_id := res.connId; |
Harald Welte | 9774e7a | 2018-03-29 08:49:38 +0200 | [diff] [blame] | 415 | tr_rtcp.connId := g_rtcp_conn_id; |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 416 | CTRL.reply(RTPEM_bind:{g_local_host, g_local_port}); |
| 417 | } |
| 418 | [] CTRL.getcall(RTPEM_connect:{?,?}) -> param (g_remote_host, g_remote_port) { |
| 419 | if (g_remote_port rem 2 == 1) { |
| 420 | //CTRL.raise(RTPEM_connect, "Remote Port is not an even number!"); |
| 421 | log("Remote Port is not an even number!"); |
| 422 | continue; |
| 423 | } |
| 424 | res := RTP_CodecPort_CtrlFunct.f_IPL4_connect(RTP, g_remote_host, |
| 425 | g_remote_port, |
| 426 | g_local_host, g_local_port, |
| 427 | g_rtp_conn_id, {udp:={}}); |
Harald Welte | 9220f63 | 2018-05-23 20:27:02 +0200 | [diff] [blame] | 428 | if (not ispresent(res.connId)) { |
| 429 | setverdict(fail, "Could not connect to RTP socket, check your configuration"); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 430 | mtc.stop; |
Harald Welte | 9220f63 | 2018-05-23 20:27:02 +0200 | [diff] [blame] | 431 | } |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 432 | res := RTP_CodecPort_CtrlFunct.f_IPL4_connect(RTCP, g_remote_host, |
| 433 | g_remote_port+1, |
| 434 | g_local_host, g_local_port+1, |
| 435 | g_rtcp_conn_id, {udp:={}}); |
Harald Welte | 9220f63 | 2018-05-23 20:27:02 +0200 | [diff] [blame] | 436 | if (not ispresent(res.connId)) { |
| 437 | setverdict(fail, "Could not connect to RTCP socket, check your configuration"); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 438 | mtc.stop; |
Harald Welte | 9220f63 | 2018-05-23 20:27:02 +0200 | [diff] [blame] | 439 | } |
Pau Espin Pedrol | e38bfe0 | 2019-05-17 18:40:43 +0200 | [diff] [blame] | 440 | g_tx_connected := true; |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 441 | CTRL.reply(RTPEM_connect:{g_remote_host, g_remote_port}); |
| 442 | } |
| 443 | [] CTRL.getcall(RTPEM_mode:{RTPEM_MODE_NONE}) { |
| 444 | T_transmit.stop; |
| 445 | g_rx_enabled := false; |
Harald Welte | 8098164 | 2017-12-24 23:59:47 +0100 | [diff] [blame] | 446 | CTRL.reply(RTPEM_mode:{RTPEM_MODE_NONE}); |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 447 | } |
| 448 | [] CTRL.getcall(RTPEM_mode:{RTPEM_MODE_TXONLY}) { |
| 449 | /* start transmit timer */ |
| 450 | T_transmit.start; |
| 451 | g_rx_enabled := false; |
Harald Welte | 8098164 | 2017-12-24 23:59:47 +0100 | [diff] [blame] | 452 | CTRL.reply(RTPEM_mode:{RTPEM_MODE_TXONLY}); |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 453 | } |
| 454 | [] CTRL.getcall(RTPEM_mode:{RTPEM_MODE_RXONLY}) { |
| 455 | |
| 456 | T_transmit.stop; |
| 457 | if (g_rx_enabled == false) { |
| 458 | /* flush queues */ |
| 459 | RTP.clear; |
| 460 | RTCP.clear; |
| 461 | g_rx_enabled := true; |
| 462 | } |
Harald Welte | 8098164 | 2017-12-24 23:59:47 +0100 | [diff] [blame] | 463 | CTRL.reply(RTPEM_mode:{RTPEM_MODE_RXONLY}); |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 464 | } |
| 465 | [] CTRL.getcall(RTPEM_mode:{RTPEM_MODE_BIDIR}) { |
| 466 | T_transmit.start; |
| 467 | if (g_rx_enabled == false) { |
| 468 | /* flush queues */ |
| 469 | RTP.clear; |
| 470 | RTCP.clear; |
| 471 | g_rx_enabled := true; |
| 472 | } |
Harald Welte | 8098164 | 2017-12-24 23:59:47 +0100 | [diff] [blame] | 473 | CTRL.reply(RTPEM_mode:{RTPEM_MODE_BIDIR}); |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 474 | } |
Harald Welte | 3f6f48f | 2017-12-24 21:48:33 +0100 | [diff] [blame] | 475 | [] CTRL.getcall(RTPEM_configure:{?}) -> param (cfg) { |
| 476 | g_cfg := cfg; |
Harald Welte | 8098164 | 2017-12-24 23:59:47 +0100 | [diff] [blame] | 477 | g_iuup_ent.cfg.active_init := g_cfg.iuup_tx_init; |
| 478 | CTRL.reply(RTPEM_configure:{cfg}); |
Harald Welte | 3f6f48f | 2017-12-24 21:48:33 +0100 | [diff] [blame] | 479 | } |
Harald Welte | cb8b427 | 2018-03-28 19:57:58 +0200 | [diff] [blame] | 480 | [] CTRL.getcall(RTPEM_stats_get:{?, ?}) -> param (is_rtcp) { |
| 481 | if (is_rtcp) { |
| 482 | CTRL.reply(RTPEM_stats_get:{g_stats_rtcp, is_rtcp}); |
| 483 | } else { |
| 484 | CTRL.reply(RTPEM_stats_get:{g_stats_rtp, is_rtcp}); |
| 485 | } |
| 486 | } |
Oliver Smith | 216019f | 2019-06-26 12:21:38 +0200 | [diff] [blame] | 487 | [] CTRL.getcall(RTPEM_conn_refuse_expect:{?}) -> param(g_conn_refuse_expect) { |
| 488 | CTRL.reply(RTPEM_conn_refuse_expect:{g_conn_refuse_expect}); |
| 489 | } |
| 490 | [] CTRL.getcall(RTPEM_conn_refuse_received:{?}) { |
| 491 | CTRL.reply(RTPEM_conn_refuse_received:{g_conn_refuse_received}); |
| 492 | } |
Harald Welte | cb8b427 | 2018-03-28 19:57:58 +0200 | [diff] [blame] | 493 | |
| 494 | |
| 495 | /* simply ignore any RTTP/RTP if receiver not enabled */ |
| 496 | [g_rx_enabled==false] RTP.receive(tr_rtp) { |
| 497 | g_stats_rtp.num_pkts_rx_err_disabled := g_stats_rtp.num_pkts_rx_err_disabled+1; |
| 498 | } |
Harald Welte | 98eb1bf | 2018-04-02 18:18:44 +0200 | [diff] [blame] | 499 | [g_rx_enabled==false] RTCP.receive(tr_rtcp) { |
Harald Welte | cb8b427 | 2018-03-28 19:57:58 +0200 | [diff] [blame] | 500 | g_stats_rtcp.num_pkts_rx_err_disabled := g_stats_rtcp.num_pkts_rx_err_disabled+1; |
| 501 | } |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 502 | |
| 503 | /* process received RTCP/RTP if receiver enabled */ |
| 504 | [g_rx_enabled] RTP.receive(tr_rtp) -> value rx_rtp { |
Harald Welte | cb8b427 | 2018-03-28 19:57:58 +0200 | [diff] [blame] | 505 | /* increment counters */ |
Philipp Maier | c290d72 | 2018-07-24 18:51:36 +0200 | [diff] [blame] | 506 | if (rx_rtp.msg.rtp.payload_type != g_cfg.tx_payload_type) { |
| 507 | g_stats_rtp.num_pkts_rx_err_pt := g_stats_rtp.num_pkts_rx_err_pt+1; |
| 508 | } |
Harald Welte | cb8b427 | 2018-03-28 19:57:58 +0200 | [diff] [blame] | 509 | g_stats_rtp.num_pkts_rx := g_stats_rtp.num_pkts_rx+1; |
| 510 | g_stats_rtp.bytes_payload_rx := g_stats_rtp.bytes_payload_rx + |
| 511 | lengthof(rx_rtp.msg.rtp.data); |
Philipp Maier | a071ee4 | 2019-02-21 16:17:32 +0100 | [diff] [blame] | 512 | if (ispresent(g_cfg.rx_fixed_payload) and rx_rtp.msg.rtp.data != g_cfg.rx_fixed_payload) { |
| 513 | g_stats_rtp.num_pkts_rx_err_payload := g_stats_rtp.num_pkts_rx_err_payload + 1; |
| 514 | } |
Harald Welte | 8098164 | 2017-12-24 23:59:47 +0100 | [diff] [blame] | 515 | if (g_cfg.iuup_mode) { |
| 516 | rx_rtp.msg.rtp.data := f_IuUP_Em_rx_decaps(g_iuup_ent, rx_rtp.msg.rtp.data); |
| 517 | } |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 518 | } |
| 519 | [g_rx_enabled] RTCP.receive(tr_rtcp) -> value rx_rtp { |
Harald Welte | 8d3ea0e | 2018-03-29 08:50:18 +0200 | [diff] [blame] | 520 | //log("RX RTCP: ", rx_rtp); |
Harald Welte | cb8b427 | 2018-03-28 19:57:58 +0200 | [diff] [blame] | 521 | g_stats_rtcp.num_pkts_rx := g_stats_rtcp.num_pkts_rx+1; |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | /* transmit if timer has expired */ |
Pau Espin Pedrol | e38bfe0 | 2019-05-17 18:40:43 +0200 | [diff] [blame] | 525 | [g_tx_connected] T_transmit.timeout { |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 526 | /* send one RTP frame, re-start timer */ |
Harald Welte | 3f6f48f | 2017-12-24 21:48:33 +0100 | [diff] [blame] | 527 | f_tx_rtp(g_cfg.tx_fixed_payload); |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 528 | T_transmit.start; |
Harald Welte | cb8b427 | 2018-03-28 19:57:58 +0200 | [diff] [blame] | 529 | /* update counters */ |
| 530 | g_stats_rtp.num_pkts_tx := g_stats_rtp.num_pkts_tx+1; |
| 531 | g_stats_rtp.bytes_payload_tx := g_stats_rtp.bytes_payload_tx + |
| 532 | lengthof(g_cfg.tx_fixed_payload); |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 533 | } |
| 534 | |
Oliver Smith | 216019f | 2019-06-26 12:21:38 +0200 | [diff] [blame] | 535 | /* connection refused */ |
| 536 | [g_conn_refuse_expect] RTP.receive(tr_conn_refuse) { |
| 537 | log("Connection refused (expected)"); |
| 538 | g_conn_refuse_received := true; |
| 539 | } |
| 540 | [not g_conn_refuse_expect] RTP.receive(tr_conn_refuse) { |
| 541 | setverdict(fail, "Connection refused (unexpected)"); |
| 542 | mtc.stop; |
| 543 | } |
| 544 | |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 545 | /* fail on any unexpected messages */ |
| 546 | [] RTP.receive { |
| 547 | setverdict(fail, "Received unexpected type from RTP"); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 548 | mtc.stop; |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 549 | } |
| 550 | [] RTCP.receive { |
| 551 | setverdict(fail, "Received unexpected type from RTCP"); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 552 | mtc.stop; |
Harald Welte | 067d66e | 2017-12-13 17:24:03 +0100 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | |
| 559 | } |