Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 1 | /* (C) 2011 by Harald Welte <laforge@gnumonks.org> |
| 2 | * (C) 2011 by On-Waves e.h.f |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | /*! \file osmo_ortp.c |
| 22 | * \brief Integration of libortp into osmocom framework (select, logging) |
| 23 | */ |
| 24 | |
| 25 | #include <stdint.h> |
Max | 73b9bc7 | 2016-05-18 15:52:37 +0200 | [diff] [blame] | 26 | #include <stdbool.h> |
Harald Welte | 65a5089 | 2011-09-08 14:42:58 +0200 | [diff] [blame] | 27 | #include <inttypes.h> |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 28 | #include <netdb.h> |
| 29 | |
| 30 | #include <osmocom/core/logging.h> |
| 31 | #include <osmocom/core/talloc.h> |
| 32 | #include <osmocom/core/utils.h> |
| 33 | #include <osmocom/core/select.h> |
| 34 | #include <osmocom/trau/osmo_ortp.h> |
| 35 | |
| 36 | #include <ortp/ortp.h> |
Harald Welte | 65a5089 | 2011-09-08 14:42:58 +0200 | [diff] [blame] | 37 | #include <ortp/rtp.h> |
Harald Welte | 0b5ffc1 | 2011-10-22 15:58:02 +0200 | [diff] [blame] | 38 | #include <ortp/port.h> |
Harald Welte | e7a3f43 | 2013-02-19 13:35:22 +0100 | [diff] [blame] | 39 | #include <ortp/rtpsession.h> |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 40 | |
Harald Welte | 2bfc01d | 2013-10-06 12:23:35 +0200 | [diff] [blame] | 41 | #include "config.h" |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 42 | |
| 43 | static PayloadType *payload_type_efr; |
| 44 | static PayloadType *payload_type_hr; |
| 45 | static RtpProfile *osmo_pt_profile; |
| 46 | |
| 47 | static void *tall_rtp_ctx; |
| 48 | |
| 49 | /* malloc integration */ |
| 50 | |
| 51 | static void *osmo_ortp_malloc(size_t sz) |
| 52 | { |
| 53 | return talloc_size(tall_rtp_ctx, sz); |
| 54 | } |
| 55 | |
| 56 | static void *osmo_ortp_realloc(void *ptr, size_t sz) |
| 57 | { |
| 58 | return talloc_realloc_size(tall_rtp_ctx, ptr, sz); |
| 59 | } |
| 60 | |
| 61 | static void osmo_ortp_free(void *ptr) |
| 62 | { |
| 63 | talloc_free(ptr); |
| 64 | } |
| 65 | |
| 66 | static OrtpMemoryFunctions osmo_ortp_memfn = { |
| 67 | .malloc_fun = osmo_ortp_malloc, |
| 68 | .realloc_fun = osmo_ortp_realloc, |
| 69 | .free_fun = osmo_ortp_free |
| 70 | }; |
| 71 | |
| 72 | /* logging */ |
| 73 | |
| 74 | struct level_map { |
| 75 | OrtpLogLevel ortp; |
| 76 | int osmo_level; |
| 77 | }; |
| 78 | static const struct level_map level_map[] = { |
| 79 | { ORTP_DEBUG, LOGL_DEBUG }, |
| 80 | { ORTP_MESSAGE, LOGL_INFO }, |
| 81 | { ORTP_WARNING, LOGL_NOTICE }, |
| 82 | { ORTP_ERROR, LOGL_ERROR }, |
| 83 | { ORTP_FATAL, LOGL_FATAL }, |
| 84 | }; |
| 85 | static int ortp_to_osmo_lvl(OrtpLogLevel lev) |
| 86 | { |
| 87 | int i; |
| 88 | |
| 89 | for (i = 0; i < ARRAY_SIZE(level_map); i++) { |
| 90 | if (level_map[i].ortp == lev) |
| 91 | return level_map[i].osmo_level; |
| 92 | } |
| 93 | /* default */ |
| 94 | return LOGL_ERROR; |
| 95 | } |
| 96 | |
Pau Espin Pedrol | c42bf19 | 2017-03-24 17:26:49 +0100 | [diff] [blame] | 97 | static void my_ortp_logfn( |
| 98 | #if HAVE_ORTP_LOG_DOMAIN |
| 99 | const char *domain, |
| 100 | #endif |
| 101 | OrtpLogLevel lev, const char *fmt, va_list args) |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 102 | { |
| 103 | osmo_vlogp(DLMIB, ortp_to_osmo_lvl(lev), __FILE__, 0, |
| 104 | 0, fmt, args); |
| 105 | } |
| 106 | |
| 107 | /* ORTP signal callbacks */ |
| 108 | |
| 109 | static void ortp_sig_cb_ssrc(RtpSession *rs, void *data) |
| 110 | { |
Harald Welte | e7a3f43 | 2013-02-19 13:35:22 +0100 | [diff] [blame] | 111 | int port = rtp_session_get_local_port(rs); |
Harald Welte | e7a3f43 | 2013-02-19 13:35:22 +0100 | [diff] [blame] | 112 | uint32_t ssrc = rtp_session_get_recv_ssrc(rs); |
Harald Welte | e7a3f43 | 2013-02-19 13:35:22 +0100 | [diff] [blame] | 113 | |
| 114 | LOGP(DLMIB, LOGL_INFO, |
| 115 | "osmo-ortp(%d): ssrc_changed to 0x%08x\n", port, ssrc); |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static void ortp_sig_cb_pt(RtpSession *rs, void *data) |
| 119 | { |
Harald Welte | e7a3f43 | 2013-02-19 13:35:22 +0100 | [diff] [blame] | 120 | int port = rtp_session_get_local_port(rs); |
| 121 | int pt = rtp_session_get_recv_payload_type(rs); |
| 122 | |
| 123 | LOGP(DLMIB, LOGL_NOTICE, |
| 124 | "osmo-ortp(%d): payload_type_changed to 0x%02x\n", port, pt); |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static void ortp_sig_cb_net(RtpSession *rs, void *data) |
| 128 | { |
Harald Welte | e7a3f43 | 2013-02-19 13:35:22 +0100 | [diff] [blame] | 129 | int port = rtp_session_get_local_port(rs); |
| 130 | |
| 131 | LOGP(DLMIB, LOGL_ERROR, |
Max | 7c840be | 2016-12-05 16:13:53 +0100 | [diff] [blame] | 132 | "osmo-ortp(%d): network_error %s\n", port, (char *)data); |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static void ortp_sig_cb_ts(RtpSession *rs, void *data) |
| 136 | { |
Harald Welte | e7a3f43 | 2013-02-19 13:35:22 +0100 | [diff] [blame] | 137 | int port = rtp_session_get_local_port(rs); |
| 138 | uint32_t ts = rtp_session_get_current_recv_ts(rs); |
| 139 | |
| 140 | LOGP(DLMIB, LOGL_NOTICE, |
Yves Godin | 2c32f0a | 2016-10-06 15:55:06 +0200 | [diff] [blame] | 141 | "osmo-ortp(%d): timestamp_jump, new TS %d, resyncing\n", port, ts); |
| 142 | rtp_session_resync(rs); |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Max | f690600 | 2016-10-24 14:10:10 +0200 | [diff] [blame] | 145 | static inline bool recv_with_cb(struct osmo_rtp_socket *rs) |
| 146 | { |
Harald Welte | 7895e04 | 2016-10-28 10:40:24 +0200 | [diff] [blame] | 147 | uint8_t *payload; |
Max | f690600 | 2016-10-24 14:10:10 +0200 | [diff] [blame] | 148 | mblk_t *mblk = rtp_session_recvm_with_ts(rs->sess, rs->rx_user_ts); |
| 149 | if (!mblk) |
| 150 | return false; |
| 151 | |
Harald Welte | 7895e04 | 2016-10-28 10:40:24 +0200 | [diff] [blame] | 152 | int plen = rtp_get_payload(mblk, &payload); |
Max | f690600 | 2016-10-24 14:10:10 +0200 | [diff] [blame] | 153 | /* hand into receiver */ |
| 154 | if (rs->rx_cb && plen > 0) |
Harald Welte | 7895e04 | 2016-10-28 10:40:24 +0200 | [diff] [blame] | 155 | rs->rx_cb(rs, payload, plen, rtp_get_seqnumber(mblk), |
Max | f690600 | 2016-10-24 14:10:10 +0200 | [diff] [blame] | 156 | rtp_get_timestamp(mblk), rtp_get_markbit(mblk)); |
| 157 | freemsg(mblk); |
| 158 | if (plen > 0) |
| 159 | return true; |
| 160 | return false; |
| 161 | } |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 162 | |
Harald Welte | 9b737df | 2011-09-07 00:59:11 +0200 | [diff] [blame] | 163 | /*! \brief poll the socket for incoming data |
| 164 | * \param[in] rs the socket to be polled |
| 165 | * \returns number of packets received + handed to the rx_cb |
| 166 | */ |
| 167 | int osmo_rtp_socket_poll(struct osmo_rtp_socket *rs) |
| 168 | { |
Max | e54d7bc | 2016-04-29 12:39:33 +0200 | [diff] [blame] | 169 | if (rs->flags & OSMO_RTP_F_DISABLED) |
| 170 | return 0; |
Harald Welte | 9b737df | 2011-09-07 00:59:11 +0200 | [diff] [blame] | 171 | |
Max | f690600 | 2016-10-24 14:10:10 +0200 | [diff] [blame] | 172 | if (recv_with_cb(rs)) |
Harald Welte | 9b737df | 2011-09-07 00:59:11 +0200 | [diff] [blame] | 173 | return 1; |
Max | f690600 | 2016-10-24 14:10:10 +0200 | [diff] [blame] | 174 | |
Max | 78d0486 | 2016-12-05 16:02:52 +0100 | [diff] [blame] | 175 | LOGP(DLMIB, LOGL_INFO, "osmo_rtp_socket_poll(%u): ERROR!\n", |
| 176 | rs->rx_user_ts); |
Max | f690600 | 2016-10-24 14:10:10 +0200 | [diff] [blame] | 177 | return 0; |
Harald Welte | 9b737df | 2011-09-07 00:59:11 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 180 | /* Osmo FD callbacks */ |
| 181 | |
| 182 | static int osmo_rtp_fd_cb(struct osmo_fd *fd, unsigned int what) |
| 183 | { |
| 184 | struct osmo_rtp_socket *rs = fd->data; |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 185 | |
| 186 | if (what & BSC_FD_READ) { |
Harald Welte | 9b737df | 2011-09-07 00:59:11 +0200 | [diff] [blame] | 187 | /* in polling mode, we don't want to be called here */ |
| 188 | if (rs->flags & OSMO_RTP_F_POLL) { |
| 189 | fd->when &= ~BSC_FD_READ; |
| 190 | return 0; |
| 191 | } |
Max | f690600 | 2016-10-24 14:10:10 +0200 | [diff] [blame] | 192 | if (!recv_with_cb(rs)) |
Harald Welte | 9b737df | 2011-09-07 00:59:11 +0200 | [diff] [blame] | 193 | LOGP(DLMIB, LOGL_INFO, "recvm_with_ts(%u): ERROR!\n", |
| 194 | rs->rx_user_ts); |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 195 | rs->rx_user_ts += 160; |
| 196 | } |
| 197 | /* writing is not queued at the moment, so BSC_FD_WRITE |
| 198 | * shouldn't occur */ |
| 199 | return 0; |
| 200 | } |
| 201 | |
Pau Espin Pedrol | b0c3a4a | 2017-06-21 07:25:18 +0200 | [diff] [blame] | 202 | /* Internal API coming from rtpsession_priv.h, used in osmo_rtcp_fd_cb */ |
| 203 | #pragma message ("Using internal ortp API: rtp_session_rtcp_rec") |
| 204 | int rtp_session_rtcp_recv(RtpSession * session); |
| 205 | |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 206 | static int osmo_rtcp_fd_cb(struct osmo_fd *fd, unsigned int what) |
| 207 | { |
| 208 | struct osmo_rtp_socket *rs = fd->data; |
| 209 | |
| 210 | /* We probably don't need this at all, as |
| 211 | * rtp_session_recvm_with_ts() will alway also poll the RTCP |
| 212 | * file descriptor for new data */ |
| 213 | return rtp_session_rtcp_recv(rs->sess); |
| 214 | } |
| 215 | |
| 216 | static int osmo_rtp_socket_fdreg(struct osmo_rtp_socket *rs) |
| 217 | { |
Harald Welte | 34260c8 | 2016-11-26 09:27:04 +0100 | [diff] [blame] | 218 | int rc; |
| 219 | |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 220 | rs->rtp_bfd.fd = rtp_session_get_rtp_socket(rs->sess); |
| 221 | rs->rtcp_bfd.fd = rtp_session_get_rtcp_socket(rs->sess); |
| 222 | rs->rtp_bfd.when = rs->rtcp_bfd.when = BSC_FD_READ; |
| 223 | rs->rtp_bfd.data = rs->rtcp_bfd.data = rs; |
| 224 | rs->rtp_bfd.cb = osmo_rtp_fd_cb; |
| 225 | rs->rtcp_bfd.cb = osmo_rtcp_fd_cb; |
| 226 | |
Harald Welte | 34260c8 | 2016-11-26 09:27:04 +0100 | [diff] [blame] | 227 | rc = osmo_fd_register(&rs->rtp_bfd); |
| 228 | if (rc < 0) |
| 229 | return rc; |
| 230 | |
| 231 | rc = osmo_fd_register(&rs->rtcp_bfd); |
| 232 | if (rc < 0) { |
| 233 | osmo_fd_unregister(&rs->rtp_bfd); |
| 234 | return rc; |
| 235 | } |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | static void create_payload_types() |
| 241 | { |
| 242 | PayloadType *pt; |
| 243 | |
| 244 | /* EFR */ |
| 245 | pt = payload_type_new(); |
| 246 | pt->type = PAYLOAD_AUDIO_PACKETIZED; |
| 247 | pt->clock_rate = 8000; |
| 248 | pt->mime_type = "EFR"; |
| 249 | pt->normal_bitrate = 12200; |
| 250 | pt->channels = 1; |
| 251 | payload_type_efr = pt; |
| 252 | |
| 253 | /* HR */ |
| 254 | pt = payload_type_new(); |
| 255 | pt->type = PAYLOAD_AUDIO_PACKETIZED; |
| 256 | pt->clock_rate = 8000; |
| 257 | pt->mime_type = "HR"; |
| 258 | pt->normal_bitrate = 6750; /* FIXME */ |
| 259 | pt->channels = 1; |
| 260 | payload_type_hr = pt; |
| 261 | |
| 262 | /* create a new RTP profile as clone of AV profile */ |
| 263 | osmo_pt_profile = rtp_profile_clone(&av_profile); |
| 264 | |
| 265 | /* add the GSM specific payload types. They are all dynamically |
| 266 | * assigned, but in the Osmocom GSM system we have allocated |
| 267 | * them as follows: */ |
| 268 | rtp_profile_set_payload(osmo_pt_profile, RTP_PT_GSM_EFR, payload_type_efr); |
| 269 | rtp_profile_set_payload(osmo_pt_profile, RTP_PT_GSM_HALF, payload_type_hr); |
| 270 | rtp_profile_set_payload(osmo_pt_profile, RTP_PT_AMR, &payload_type_amr); |
| 271 | } |
| 272 | |
| 273 | /* public functions */ |
| 274 | |
| 275 | /*! \brief initialize Osmocom RTP code |
Harald Welte | fcb1fe8 | 2011-09-07 11:51:52 +0200 | [diff] [blame] | 276 | * \param[in] ctx default talloc context for library-internal allocations |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 277 | */ |
| 278 | void osmo_rtp_init(void *ctx) |
| 279 | { |
| 280 | tall_rtp_ctx = ctx; |
| 281 | ortp_set_memory_functions(&osmo_ortp_memfn); |
| 282 | ortp_init(); |
Pau Espin Pedrol | c42bf19 | 2017-03-24 17:26:49 +0100 | [diff] [blame] | 283 | ortp_set_log_level_mask( |
| 284 | #if HAVE_ORTP_LOG_DOMAIN |
| 285 | ORTP_LOG_DOMAIN, |
| 286 | #endif |
| 287 | 0xffff); |
| 288 | |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 289 | ortp_set_log_handler(my_ortp_logfn); |
| 290 | create_payload_types(); |
| 291 | } |
| 292 | |
Max | bf42e0a | 2016-12-15 19:57:57 +0100 | [diff] [blame] | 293 | /*! \brief Set Osmocom RTP socket parameters |
| 294 | * \param[in] rs OsmoRTP socket |
| 295 | * \param[in] param defined which parameter to set |
| 296 | OSMO_RTP_P_JITBUF - enables regular jitter buffering |
| 297 | OSMO_RTP_P_JIT_ADAP - enables adaptive jitter buffering |
| 298 | * \param[in] val Size of jitter buffer (in ms), 0 means disable buffering |
| 299 | * \returns negative value on error, 0 or 1 otherwise |
| 300 | (depending on whether given jitter buffering is enabled) |
| 301 | */ |
Harald Welte | 65a5089 | 2011-09-08 14:42:58 +0200 | [diff] [blame] | 302 | int osmo_rtp_socket_set_param(struct osmo_rtp_socket *rs, |
| 303 | enum osmo_rtp_param param, int val) |
| 304 | { |
Harald Welte | 65a5089 | 2011-09-08 14:42:58 +0200 | [diff] [blame] | 305 | switch (param) { |
Max | bf42e0a | 2016-12-15 19:57:57 +0100 | [diff] [blame] | 306 | case OSMO_RTP_P_JIT_ADAP: |
| 307 | rtp_session_enable_adaptive_jitter_compensation(rs->sess, |
| 308 | (bool)val); |
| 309 | /* fall-through on-purpose - we have to set val anyway */ |
Harald Welte | 65a5089 | 2011-09-08 14:42:58 +0200 | [diff] [blame] | 310 | case OSMO_RTP_P_JITBUF: |
Andreas Eversberg | 41bc615 | 2013-02-14 11:03:26 +0100 | [diff] [blame] | 311 | rtp_session_enable_jitter_buffer(rs->sess, |
| 312 | (val) ? TRUE : FALSE); |
| 313 | if (val) |
| 314 | rtp_session_set_jitter_compensation(rs->sess, val); |
Harald Welte | 65a5089 | 2011-09-08 14:42:58 +0200 | [diff] [blame] | 315 | break; |
Harald Welte | 65a5089 | 2011-09-08 14:42:58 +0200 | [diff] [blame] | 316 | default: |
| 317 | return -EINVAL; |
| 318 | } |
Max | bf42e0a | 2016-12-15 19:57:57 +0100 | [diff] [blame] | 319 | if (param == OSMO_RTP_P_JIT_ADAP) |
| 320 | return rtp_session_adaptive_jitter_compensation_enabled(rs->sess); |
| 321 | return rtp_session_jitter_buffer_enabled(rs->sess); |
Harald Welte | 65a5089 | 2011-09-08 14:42:58 +0200 | [diff] [blame] | 322 | } |
| 323 | |
Harald Welte | fcb1fe8 | 2011-09-07 11:51:52 +0200 | [diff] [blame] | 324 | /*! \brief Create a new RTP socket |
| 325 | * \param[in] talloc_cxt talloc context for this allocation. NULL for |
| 326 | * dafault context |
| 327 | * \param[in] flags Flags like OSMO_RTP_F_POLL |
| 328 | * \returns pointer to library-allocated \a struct osmo_rtp_socket |
| 329 | */ |
Harald Welte | 9b737df | 2011-09-07 00:59:11 +0200 | [diff] [blame] | 330 | struct osmo_rtp_socket *osmo_rtp_socket_create(void *talloc_ctx, unsigned int flags) |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 331 | { |
| 332 | struct osmo_rtp_socket *rs; |
| 333 | |
| 334 | if (!talloc_ctx) |
| 335 | talloc_ctx = tall_rtp_ctx; |
| 336 | |
| 337 | rs = talloc_zero(talloc_ctx, struct osmo_rtp_socket); |
| 338 | if (!rs) |
| 339 | return NULL; |
| 340 | |
Max | e54d7bc | 2016-04-29 12:39:33 +0200 | [diff] [blame] | 341 | rs->flags = OSMO_RTP_F_DISABLED | flags; |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 342 | rs->sess = rtp_session_new(RTP_SESSION_SENDRECV); |
| 343 | if (!rs->sess) { |
| 344 | talloc_free(rs); |
| 345 | return NULL; |
| 346 | } |
| 347 | rtp_session_set_data(rs->sess, rs); |
| 348 | rtp_session_set_profile(rs->sess, osmo_pt_profile); |
Harald Welte | 9b737df | 2011-09-07 00:59:11 +0200 | [diff] [blame] | 349 | rtp_session_set_jitter_compensation(rs->sess, 100); |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 350 | |
| 351 | rtp_session_signal_connect(rs->sess, "ssrc_changed", |
| 352 | (RtpCallback) ortp_sig_cb_ssrc, |
Pau Espin Pedrol | 05df2d6 | 2017-06-21 07:37:45 +0200 | [diff] [blame] | 353 | RTP_SIGNAL_PTR_CAST(rs)); |
| 354 | |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 355 | rtp_session_signal_connect(rs->sess, "payload_type_changed", |
| 356 | (RtpCallback) ortp_sig_cb_pt, |
Pau Espin Pedrol | 05df2d6 | 2017-06-21 07:37:45 +0200 | [diff] [blame] | 357 | RTP_SIGNAL_PTR_CAST(rs)); |
| 358 | |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 359 | rtp_session_signal_connect(rs->sess, "network_error", |
| 360 | (RtpCallback) ortp_sig_cb_net, |
Pau Espin Pedrol | 05df2d6 | 2017-06-21 07:37:45 +0200 | [diff] [blame] | 361 | RTP_SIGNAL_PTR_CAST(rs)); |
| 362 | |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 363 | rtp_session_signal_connect(rs->sess, "timestamp_jump", |
| 364 | (RtpCallback) ortp_sig_cb_ts, |
Pau Espin Pedrol | 05df2d6 | 2017-06-21 07:37:45 +0200 | [diff] [blame] | 365 | RTP_SIGNAL_PTR_CAST(rs)); |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 366 | |
Holger Hans Peter Freyther | bf6f1f4 | 2014-06-24 10:57:35 +0200 | [diff] [blame] | 367 | /* initialize according to the RFC */ |
| 368 | rtp_session_set_seq_number(rs->sess, random()); |
Holger Hans Peter Freyther | fb6e1e9 | 2014-06-24 11:02:01 +0200 | [diff] [blame] | 369 | rs->tx_timestamp = random(); |
Pau Espin Pedrol | b0c3a4a | 2017-06-21 07:25:18 +0200 | [diff] [blame] | 370 | |
Holger Hans Peter Freyther | bf6f1f4 | 2014-06-24 10:57:35 +0200 | [diff] [blame] | 371 | |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 372 | return rs; |
| 373 | } |
| 374 | |
Harald Welte | fcb1fe8 | 2011-09-07 11:51:52 +0200 | [diff] [blame] | 375 | /*! \brief bind a RTP socket to a local port |
| 376 | * \param[in] rs OsmoRTP socket |
| 377 | * \param[in] ip hostname/ip as string |
| 378 | * \param[in] port UDP port number, -1 for random selection |
| 379 | * \returns 0 on success, <0 on error |
| 380 | */ |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 381 | int osmo_rtp_socket_bind(struct osmo_rtp_socket *rs, const char *ip, int port) |
| 382 | { |
Max | 15d9b79 | 2016-04-28 12:05:27 +0200 | [diff] [blame] | 383 | int rc, rtcp = (-1 != port) ? port + 1 : -1; |
Max | 80f7c04 | 2016-04-29 12:39:34 +0200 | [diff] [blame] | 384 | rc = rtp_session_set_local_addr(rs->sess, ip, port, rtcp); |
Max | 15d9b79 | 2016-04-28 12:05:27 +0200 | [diff] [blame] | 385 | |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 386 | if (rc < 0) |
| 387 | return rc; |
| 388 | |
| 389 | rs->rtp_bfd.fd = rtp_session_get_rtp_socket(rs->sess); |
| 390 | rs->rtcp_bfd.fd = rtp_session_get_rtcp_socket(rs->sess); |
| 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
Harald Welte | fcb1fe8 | 2011-09-07 11:51:52 +0200 | [diff] [blame] | 395 | /*! \brief connect a OsmoRTP socket to a remote port |
| 396 | * \param[in] rs OsmoRTP socket |
| 397 | * \param[in] ip String representation of remote hostname or IP address |
| 398 | * \param[in] port UDP port number to connect to |
| 399 | * |
| 400 | * If the OsmoRTP socket is not in POLL mode, this function will also |
| 401 | * cause the RTP and RTCP file descriptors to be registred with the |
| 402 | * libosmocore select() loop integration. |
| 403 | * |
| 404 | * \returns 0 on success, <0 in case of error |
| 405 | */ |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 406 | int osmo_rtp_socket_connect(struct osmo_rtp_socket *rs, const char *ip, uint16_t port) |
| 407 | { |
| 408 | int rc; |
Max | e54d7bc | 2016-04-29 12:39:33 +0200 | [diff] [blame] | 409 | if (!port) { |
| 410 | LOGP(DLMIB, LOGL_INFO, "osmo_rtp_socket_connect() refused to " |
| 411 | "set remote %s:%u\n", ip, port); |
| 412 | return 0; |
| 413 | } |
Max | 8c119f7 | 2016-04-29 12:39:35 +0200 | [diff] [blame] | 414 | |
Neels Hofmeyr | a0ff942 | 2016-10-06 15:43:38 +0200 | [diff] [blame] | 415 | /* We don't want the connected mode enabled during |
| 416 | * rtp_session_set_remote_addr(), because that will already setup a |
| 417 | * connection and updating the remote address will no longer have an |
| 418 | * effect. Contrary to what one may expect, this must be 0 at first, |
| 419 | * and we're setting to 1 further down to establish a connection once |
| 420 | * the first RTP packet is received (OS#1661). */ |
| 421 | rtp_session_set_connected_mode(rs->sess, 0); |
| 422 | |
| 423 | rc = rtp_session_set_remote_addr(rs->sess, ip, port); |
| 424 | if (rc < 0) |
| 425 | return rc; |
| 426 | |
Harald Welte | d426d45 | 2013-02-09 11:01:19 +0100 | [diff] [blame] | 427 | /* enable the use of connect() so later getsockname() will |
| 428 | * actually return the IP address that was chosen for the local |
| 429 | * sid of the connection */ |
| 430 | rtp_session_set_connected_mode(rs->sess, 1); |
Max | e54d7bc | 2016-04-29 12:39:33 +0200 | [diff] [blame] | 431 | rs->flags &= ~OSMO_RTP_F_DISABLED; |
Harald Welte | d426d45 | 2013-02-09 11:01:19 +0100 | [diff] [blame] | 432 | |
Harald Welte | 9b737df | 2011-09-07 00:59:11 +0200 | [diff] [blame] | 433 | if (rs->flags & OSMO_RTP_F_POLL) |
| 434 | return rc; |
| 435 | else |
| 436 | return osmo_rtp_socket_fdreg(rs); |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 437 | } |
| 438 | |
Pau Espin Pedrol | 524923a | 2017-06-28 15:31:46 +0200 | [diff] [blame] | 439 | /*! \brief Increment timestamp on a RTP socket without sending any packet |
| 440 | * \param[in] rs OsmoRTP socket |
| 441 | * \param[in] duration duration in number of RTP clock ticks |
| 442 | * |
| 443 | * Useful to keep the RTP internal clock up to date if an RTP frame should be |
| 444 | * send at a given time but no audio content is available. When next packet is |
| 445 | * sent, the receiver will see a different increase on the sequence number and |
| 446 | * the timestamp, and it should then take it as a synchronization point. For |
| 447 | * that same reason, it is advisable to enable the marker bit on the next RTP |
| 448 | * packet to be sent after calling this function. |
| 449 | * |
| 450 | * \returns 0 on success, <0 in case of error. |
| 451 | */ |
| 452 | int osmo_rtp_skipped_frame(struct osmo_rtp_socket *rs, unsigned int duration) |
| 453 | { |
| 454 | if (rs->flags & OSMO_RTP_F_DISABLED) |
| 455 | return 0; |
| 456 | |
| 457 | rs->tx_timestamp += duration; |
| 458 | return 0; |
| 459 | } |
| 460 | |
Harald Welte | fcb1fe8 | 2011-09-07 11:51:52 +0200 | [diff] [blame] | 461 | /*! \brief Send one RTP frame via a RTP socket |
| 462 | * \param[in] rs OsmoRTP socket |
| 463 | * \param[in] payload pointer to buffer with RTP payload data |
| 464 | * \param[in] payload_len length of \a payload in bytes |
| 465 | * \param[in] duration duration in number of RTP clock ticks |
| 466 | * \returns 0 on success, <0 in case of error. |
| 467 | */ |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 468 | int osmo_rtp_send_frame(struct osmo_rtp_socket *rs, const uint8_t *payload, |
| 469 | unsigned int payload_len, unsigned int duration) |
| 470 | { |
Max | 73b9bc7 | 2016-05-18 15:52:37 +0200 | [diff] [blame] | 471 | return osmo_rtp_send_frame_ext(rs, payload, payload_len, duration, |
| 472 | false); |
| 473 | } |
| 474 | |
| 475 | /*! \brief Send one RTP frame via a RTP socket |
| 476 | * \param[in] rs OsmoRTP socket |
| 477 | * \param[in] payload pointer to buffer with RTP payload data |
| 478 | * \param[in] payload_len length of \a payload in bytes |
| 479 | * \param[in] duration duration in number of RTP clock ticks |
| 480 | * \param[in] marker the status of Marker bit in RTP header |
| 481 | * \returns 0 on success, <0 in case of error. |
| 482 | */ |
| 483 | int osmo_rtp_send_frame_ext(struct osmo_rtp_socket *rs, const uint8_t *payload, |
| 484 | unsigned int payload_len, unsigned int duration, |
| 485 | bool marker) |
| 486 | { |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 487 | mblk_t *mblk; |
| 488 | int rc; |
| 489 | |
Max | e54d7bc | 2016-04-29 12:39:33 +0200 | [diff] [blame] | 490 | if (rs->flags & OSMO_RTP_F_DISABLED) |
| 491 | return 0; |
| 492 | |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 493 | mblk = rtp_session_create_packet(rs->sess, RTP_FIXED_HEADER_SIZE, |
| 494 | payload, payload_len); |
| 495 | if (!mblk) |
| 496 | return -ENOMEM; |
| 497 | |
Max | 73b9bc7 | 2016-05-18 15:52:37 +0200 | [diff] [blame] | 498 | rtp_set_markbit(mblk, marker); |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 499 | rc = rtp_session_sendm_with_ts(rs->sess, mblk, |
| 500 | rs->tx_timestamp); |
Pau Espin Pedrol | 9e992c2 | 2017-06-29 18:04:18 +0200 | [diff] [blame] | 501 | rs->tx_timestamp += duration; |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 502 | if (rc < 0) { |
| 503 | /* no need to free() the mblk, as rtp_session_rtp_send() |
| 504 | * unconditionally free()s the mblk even in case of |
| 505 | * error */ |
| 506 | return rc; |
| 507 | } |
| 508 | |
| 509 | return rc; |
| 510 | } |
| 511 | |
Harald Welte | fcb1fe8 | 2011-09-07 11:51:52 +0200 | [diff] [blame] | 512 | /*! \brief Set the payload type of a RTP socket |
| 513 | * \param[in] rs OsmoRTP socket |
| 514 | * \param[in] payload_type RTP payload type |
| 515 | * \returns 0 on success, < 0 otherwise |
| 516 | */ |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 517 | int osmo_rtp_socket_set_pt(struct osmo_rtp_socket *rs, int payload_type) |
| 518 | { |
| 519 | int rc; |
| 520 | |
| 521 | rc = rtp_session_set_payload_type(rs->sess, payload_type); |
| 522 | //rtp_session_set_rtcp_report_interval(rs->sess, 5*1000); |
| 523 | |
| 524 | return rc; |
| 525 | } |
| 526 | |
Harald Welte | fcb1fe8 | 2011-09-07 11:51:52 +0200 | [diff] [blame] | 527 | /*! \brief completely close the RTP socket and release all resources |
| 528 | * \param[in] rs OsmoRTP socket to be released |
| 529 | * \returns 0 on success |
| 530 | */ |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 531 | int osmo_rtp_socket_free(struct osmo_rtp_socket *rs) |
| 532 | { |
| 533 | if (rs->rtp_bfd.list.next && rs->rtp_bfd.list.next != LLIST_POISON1) |
| 534 | osmo_fd_unregister(&rs->rtp_bfd); |
| 535 | |
| 536 | if (rs->rtcp_bfd.list.next && rs->rtcp_bfd.list.next != LLIST_POISON1) |
| 537 | osmo_fd_unregister(&rs->rtcp_bfd); |
| 538 | |
| 539 | if (rs->sess) { |
| 540 | rtp_session_release_sockets(rs->sess); |
| 541 | rtp_session_destroy(rs->sess); |
| 542 | rs->sess = NULL; |
| 543 | } |
| 544 | |
| 545 | talloc_free(rs); |
| 546 | |
| 547 | return 0; |
| 548 | } |
| 549 | |
Harald Welte | fcb1fe8 | 2011-09-07 11:51:52 +0200 | [diff] [blame] | 550 | /*! \brief obtain the locally bound IPv4 address and UDP port |
| 551 | * \param[in] rs OsmoRTP socket |
| 552 | * \param[out] ip Pointer to caller-allocated uint32_t for IPv4 address |
| 553 | * \oaram[out] port Pointer to caller-allocated int for UDP port number |
| 554 | * \returns 0 on success, <0 on error, -EIO in case of IPv6 socket |
| 555 | */ |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 556 | int osmo_rtp_get_bound_ip_port(struct osmo_rtp_socket *rs, |
| 557 | uint32_t *ip, int *port) |
| 558 | { |
| 559 | int rc; |
| 560 | struct sockaddr_storage ss; |
| 561 | struct sockaddr_in *sin = (struct sockaddr_in *) &ss; |
| 562 | socklen_t alen = sizeof(ss); |
| 563 | |
| 564 | rc = getsockname(rs->rtp_bfd.fd, (struct sockaddr *)&ss, &alen); |
| 565 | if (rc < 0) |
| 566 | return rc; |
| 567 | |
| 568 | if (ss.ss_family != AF_INET) |
| 569 | return -EIO; |
| 570 | |
| 571 | *ip = ntohl(sin->sin_addr.s_addr); |
| 572 | *port = rtp_session_get_local_port(rs->sess); |
| 573 | |
| 574 | return 0; |
| 575 | } |
| 576 | |
Harald Welte | fcb1fe8 | 2011-09-07 11:51:52 +0200 | [diff] [blame] | 577 | /*! \brief obtain the locally bound address and port |
| 578 | * \param[in] rs OsmoRTP socket |
| 579 | * \param[out] addr caller-allocated char ** to which the string pointer for |
| 580 | * the address is stored |
| 581 | * \param[out] port caller-allocated int * to which the port number is |
| 582 | * stored |
| 583 | * \returns 0 on success, <0 in case of error |
| 584 | */ |
Harald Welte | 41d0d84 | 2011-09-03 15:33:24 +0200 | [diff] [blame] | 585 | int osmo_rtp_get_bound_addr(struct osmo_rtp_socket *rs, |
| 586 | const char **addr, int *port) |
| 587 | { |
| 588 | int rc; |
| 589 | struct sockaddr_storage ss; |
| 590 | socklen_t alen = sizeof(ss); |
| 591 | static char hostbuf[256]; |
| 592 | |
| 593 | memset(hostbuf, 0, sizeof(hostbuf)); |
| 594 | |
| 595 | rc = getsockname(rs->rtp_bfd.fd, (struct sockaddr *)&ss, &alen); |
| 596 | if (rc < 0) |
| 597 | return rc; |
| 598 | |
| 599 | rc = getnameinfo((struct sockaddr *)&ss, alen, |
| 600 | hostbuf, sizeof(hostbuf), NULL, 0, |
| 601 | NI_NUMERICHOST); |
| 602 | if (rc < 0) |
| 603 | return rc; |
| 604 | |
| 605 | *port = rtp_session_get_local_port(rs->sess); |
| 606 | *addr = hostbuf; |
| 607 | |
| 608 | return 0; |
| 609 | } |
Harald Welte | 65a5089 | 2011-09-08 14:42:58 +0200 | [diff] [blame] | 610 | |
| 611 | |
| 612 | void osmo_rtp_socket_log_stats(struct osmo_rtp_socket *rs, |
| 613 | int subsys, int level, |
| 614 | const char *pfx) |
| 615 | { |
| 616 | const rtp_stats_t *stats; |
| 617 | |
| 618 | stats = rtp_session_get_stats(rs->sess); |
| 619 | if (!stats) |
| 620 | return; |
| 621 | |
| 622 | LOGP(subsys, level, "%sRTP Tx(%"PRIu64" pkts, %"PRIu64" bytes) " |
| 623 | "Rx(%"PRIu64" pkts, %"PRIu64" bytes, %"PRIu64" late, " |
| 624 | "%"PRIu64" loss, %"PRIu64" qmax)\n", |
| 625 | pfx, stats->packet_sent, stats->sent, |
| 626 | stats->packet_recv, stats->hw_recv, stats->outoftime, |
| 627 | stats->cum_packet_loss, stats->discarded); |
| 628 | } |
Holger Hans Peter Freyther | fe01908 | 2015-09-21 10:52:52 +0200 | [diff] [blame] | 629 | |
| 630 | void osmo_rtp_socket_stats(struct osmo_rtp_socket *rs, |
| 631 | uint32_t *sent_packets, uint32_t *sent_octets, |
| 632 | uint32_t *recv_packets, uint32_t *recv_octets, |
| 633 | uint32_t *recv_lost, uint32_t *last_jitter) |
| 634 | { |
| 635 | const rtp_stats_t *stats; |
Holger Hans Peter Freyther | fe01908 | 2015-09-21 10:52:52 +0200 | [diff] [blame] | 636 | |
| 637 | *sent_packets = *sent_octets = *recv_packets = *recv_octets = 0; |
| 638 | *recv_lost = *last_jitter = 0; |
| 639 | |
| 640 | stats = rtp_session_get_stats(rs->sess); |
| 641 | if (stats) { |
| 642 | /* truncate from 64bit to 32bit here */ |
| 643 | *sent_packets = stats->packet_sent; |
| 644 | *sent_octets = stats->sent; |
| 645 | *recv_packets = stats->packet_recv; |
| 646 | *recv_octets = stats->recv; |
| 647 | *recv_lost = stats->cum_packet_loss; |
| 648 | } |
| 649 | |
Holger Hans Peter Freyther | 71bc9e2 | 2015-09-21 12:18:37 +0200 | [diff] [blame] | 650 | const jitter_stats_t *jitter; |
| 651 | |
Holger Hans Peter Freyther | fe01908 | 2015-09-21 10:52:52 +0200 | [diff] [blame] | 652 | jitter = rtp_session_get_jitter_stats(rs->sess); |
| 653 | if (jitter) |
| 654 | *last_jitter = jitter->jitter; |
| 655 | } |