blob: b72f681bdaf180182d75bdff5b51376bb67e8892 [file] [log] [blame]
Harald Welte14277e32021-04-29 21:38:25 +02001/* (C) 2011-2021 by Harald Welte <laforge@gnumonks.org>
Harald Welte41d0d842011-09-03 15:33:24 +02002 * (C) 2011 by On-Waves e.h.f
3 * All Rights Reserved
4 *
Harald Welte323d39d2017-11-13 01:09:21 +09005 * SPDX-License-Identifier: GPL-2.0+
6 *
Harald Welte41d0d842011-09-03 15:33:24 +02007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23/*! \file osmo_ortp.c
24 * \brief Integration of libortp into osmocom framework (select, logging)
25 */
26
27#include <stdint.h>
Max73b9bc72016-05-18 15:52:37 +020028#include <stdbool.h>
Harald Welte65a50892011-09-08 14:42:58 +020029#include <inttypes.h>
Harald Welte41d0d842011-09-03 15:33:24 +020030#include <netdb.h>
31
32#include <osmocom/core/logging.h>
33#include <osmocom/core/talloc.h>
34#include <osmocom/core/utils.h>
35#include <osmocom/core/select.h>
Harald Welte14277e32021-04-29 21:38:25 +020036#include <osmocom/core/socket.h>
Harald Welte41d0d842011-09-03 15:33:24 +020037#include <osmocom/trau/osmo_ortp.h>
38
39#include <ortp/ortp.h>
Harald Welte65a50892011-09-08 14:42:58 +020040#include <ortp/rtp.h>
Harald Welte0b5ffc12011-10-22 15:58:02 +020041#include <ortp/port.h>
Harald Weltee7a3f432013-02-19 13:35:22 +010042#include <ortp/rtpsession.h>
Harald Welte41d0d842011-09-03 15:33:24 +020043
Harald Welte2bfc01d2013-10-06 12:23:35 +020044#include "config.h"
Harald Welte41d0d842011-09-03 15:33:24 +020045
46static PayloadType *payload_type_efr;
47static PayloadType *payload_type_hr;
48static RtpProfile *osmo_pt_profile;
49
50static void *tall_rtp_ctx;
51
52/* malloc integration */
53
54static void *osmo_ortp_malloc(size_t sz)
55{
56 return talloc_size(tall_rtp_ctx, sz);
57}
58
59static void *osmo_ortp_realloc(void *ptr, size_t sz)
60{
61 return talloc_realloc_size(tall_rtp_ctx, ptr, sz);
62}
63
64static void osmo_ortp_free(void *ptr)
65{
66 talloc_free(ptr);
67}
68
69static OrtpMemoryFunctions osmo_ortp_memfn = {
70 .malloc_fun = osmo_ortp_malloc,
71 .realloc_fun = osmo_ortp_realloc,
72 .free_fun = osmo_ortp_free
73};
74
75/* logging */
76
77struct level_map {
78 OrtpLogLevel ortp;
79 int osmo_level;
80};
81static const struct level_map level_map[] = {
82 { ORTP_DEBUG, LOGL_DEBUG },
83 { ORTP_MESSAGE, LOGL_INFO },
84 { ORTP_WARNING, LOGL_NOTICE },
85 { ORTP_ERROR, LOGL_ERROR },
86 { ORTP_FATAL, LOGL_FATAL },
87};
88static int ortp_to_osmo_lvl(OrtpLogLevel lev)
89{
90 int i;
91
92 for (i = 0; i < ARRAY_SIZE(level_map); i++) {
93 if (level_map[i].ortp == lev)
94 return level_map[i].osmo_level;
95 }
96 /* default */
97 return LOGL_ERROR;
98}
99
Pau Espin Pedrolc42bf192017-03-24 17:26:49 +0100100static void my_ortp_logfn(
101#if HAVE_ORTP_LOG_DOMAIN
102 const char *domain,
103#endif
104 OrtpLogLevel lev, const char *fmt, va_list args)
Harald Welte41d0d842011-09-03 15:33:24 +0200105{
Pau Espin Pedrolde5758d2018-11-12 17:00:22 +0100106 /* Some strings coming from ortp are not endline terminated and mangle
107 * the output. Make sure all strings are endl terminated before
108 * printing.
109 */
110 int needs_endl;
111 const char *domain_str;
112 char *str;
113 size_t fmt_len = strlen(fmt);
114#if HAVE_ORTP_LOG_DOMAIN
115 /* domain can be NULL, found experimentally */
116 domain_str = domain ? : "";
117#else
118 domain_str = "";
119#endif
120 size_t domain_len = strlen(domain_str);
121
122 if (fmt_len == 0)
123 return;
124
125 needs_endl = fmt[fmt_len - 1] != '\n' ? 1 : 0;
126
127 str = osmo_ortp_malloc(domain_len + 2 /*": "*/ + fmt_len + needs_endl + 1);
128 sprintf(str, "%s%s%s%s", domain_str, domain_len ? ": " : "", fmt, needs_endl ? "\n" : "");
129
Harald Welte41d0d842011-09-03 15:33:24 +0200130 osmo_vlogp(DLMIB, ortp_to_osmo_lvl(lev), __FILE__, 0,
Pau Espin Pedrolde5758d2018-11-12 17:00:22 +0100131 0, str, args);
132
133 osmo_ortp_free(str);
134
Harald Welte41d0d842011-09-03 15:33:24 +0200135}
136
137/* ORTP signal callbacks */
138
139static void ortp_sig_cb_ssrc(RtpSession *rs, void *data)
140{
Harald Weltee7a3f432013-02-19 13:35:22 +0100141 int port = rtp_session_get_local_port(rs);
Harald Weltee7a3f432013-02-19 13:35:22 +0100142 uint32_t ssrc = rtp_session_get_recv_ssrc(rs);
Harald Weltee7a3f432013-02-19 13:35:22 +0100143
144 LOGP(DLMIB, LOGL_INFO,
Philipp Maier28eeb6b2018-05-30 10:52:41 +0200145 "osmo-ortp(%d): ssrc_changed to 0x%08x, resetting\n", port, ssrc);
146 rtp_session_reset(rs);
Harald Welte41d0d842011-09-03 15:33:24 +0200147}
148
149static void ortp_sig_cb_pt(RtpSession *rs, void *data)
150{
Harald Weltee7a3f432013-02-19 13:35:22 +0100151 int port = rtp_session_get_local_port(rs);
152 int pt = rtp_session_get_recv_payload_type(rs);
153
154 LOGP(DLMIB, LOGL_NOTICE,
155 "osmo-ortp(%d): payload_type_changed to 0x%02x\n", port, pt);
Harald Welte41d0d842011-09-03 15:33:24 +0200156}
157
158static void ortp_sig_cb_net(RtpSession *rs, void *data)
159{
Harald Weltee7a3f432013-02-19 13:35:22 +0100160 int port = rtp_session_get_local_port(rs);
161
162 LOGP(DLMIB, LOGL_ERROR,
Max7c840be2016-12-05 16:13:53 +0100163 "osmo-ortp(%d): network_error %s\n", port, (char *)data);
Harald Welte41d0d842011-09-03 15:33:24 +0200164}
165
166static void ortp_sig_cb_ts(RtpSession *rs, void *data)
167{
Harald Weltee7a3f432013-02-19 13:35:22 +0100168 int port = rtp_session_get_local_port(rs);
169 uint32_t ts = rtp_session_get_current_recv_ts(rs);
170
171 LOGP(DLMIB, LOGL_NOTICE,
Yves Godin2c32f0a2016-10-06 15:55:06 +0200172 "osmo-ortp(%d): timestamp_jump, new TS %d, resyncing\n", port, ts);
173 rtp_session_resync(rs);
Harald Welte41d0d842011-09-03 15:33:24 +0200174}
175
Maxf6906002016-10-24 14:10:10 +0200176static inline bool recv_with_cb(struct osmo_rtp_socket *rs)
177{
Harald Welte7895e042016-10-28 10:40:24 +0200178 uint8_t *payload;
Maxf6906002016-10-24 14:10:10 +0200179 mblk_t *mblk = rtp_session_recvm_with_ts(rs->sess, rs->rx_user_ts);
180 if (!mblk)
181 return false;
182
Harald Welte7895e042016-10-28 10:40:24 +0200183 int plen = rtp_get_payload(mblk, &payload);
Maxf6906002016-10-24 14:10:10 +0200184 /* hand into receiver */
185 if (rs->rx_cb && plen > 0)
Harald Welte7895e042016-10-28 10:40:24 +0200186 rs->rx_cb(rs, payload, plen, rtp_get_seqnumber(mblk),
Maxf6906002016-10-24 14:10:10 +0200187 rtp_get_timestamp(mblk), rtp_get_markbit(mblk));
188 freemsg(mblk);
189 if (plen > 0)
190 return true;
191 return false;
192}
Harald Welte41d0d842011-09-03 15:33:24 +0200193
Harald Welte9b737df2011-09-07 00:59:11 +0200194/*! \brief poll the socket for incoming data
195 * \param[in] rs the socket to be polled
196 * \returns number of packets received + handed to the rx_cb
197 */
198int osmo_rtp_socket_poll(struct osmo_rtp_socket *rs)
199{
Maxe54d7bc2016-04-29 12:39:33 +0200200 if (rs->flags & OSMO_RTP_F_DISABLED)
201 return 0;
Harald Welte9b737df2011-09-07 00:59:11 +0200202
Maxf6906002016-10-24 14:10:10 +0200203 if (recv_with_cb(rs))
Harald Welte9b737df2011-09-07 00:59:11 +0200204 return 1;
Maxf6906002016-10-24 14:10:10 +0200205
Harald Welte8c724c02021-02-06 15:25:39 +0100206 /* this happens every time we miss an incoming RTP frame, which is quite common
207 * when a voice channel is first activated, or also in case of packet loss.
208 * See also https://osmocom.org/issues/4464 */
209 LOGP(DLMIB, LOGL_DEBUG, "osmo_rtp_socket_poll(%u): No message received\n", rs->rx_user_ts);
Maxf6906002016-10-24 14:10:10 +0200210 return 0;
Harald Welte9b737df2011-09-07 00:59:11 +0200211}
212
Harald Welte41d0d842011-09-03 15:33:24 +0200213/* Osmo FD callbacks */
214
215static int osmo_rtp_fd_cb(struct osmo_fd *fd, unsigned int what)
216{
217 struct osmo_rtp_socket *rs = fd->data;
Harald Welte41d0d842011-09-03 15:33:24 +0200218
Harald Weltede3959e2020-10-18 22:28:50 +0200219 if (what & OSMO_FD_READ) {
Harald Welte9b737df2011-09-07 00:59:11 +0200220 /* in polling mode, we don't want to be called here */
221 if (rs->flags & OSMO_RTP_F_POLL) {
Harald Welte949b8a22020-10-18 23:01:53 +0200222 osmo_fd_read_disable(fd);
Harald Welte9b737df2011-09-07 00:59:11 +0200223 return 0;
224 }
Maxf6906002016-10-24 14:10:10 +0200225 if (!recv_with_cb(rs))
Harald Welte9b737df2011-09-07 00:59:11 +0200226 LOGP(DLMIB, LOGL_INFO, "recvm_with_ts(%u): ERROR!\n",
227 rs->rx_user_ts);
Harald Welte41d0d842011-09-03 15:33:24 +0200228 rs->rx_user_ts += 160;
229 }
Harald Weltede3959e2020-10-18 22:28:50 +0200230 /* writing is not queued at the moment, so OSMO_FD_WRITE
Harald Welte41d0d842011-09-03 15:33:24 +0200231 * shouldn't occur */
232 return 0;
233}
234
Pau Espin Pedrolb0c3a4a2017-06-21 07:25:18 +0200235/* Internal API coming from rtpsession_priv.h, used in osmo_rtcp_fd_cb */
236#pragma message ("Using internal ortp API: rtp_session_rtcp_rec")
237int rtp_session_rtcp_recv(RtpSession * session);
238
Harald Welte41d0d842011-09-03 15:33:24 +0200239static int osmo_rtcp_fd_cb(struct osmo_fd *fd, unsigned int what)
240{
241 struct osmo_rtp_socket *rs = fd->data;
242
243 /* We probably don't need this at all, as
244 * rtp_session_recvm_with_ts() will alway also poll the RTCP
245 * file descriptor for new data */
246 return rtp_session_rtcp_recv(rs->sess);
247}
248
249static int osmo_rtp_socket_fdreg(struct osmo_rtp_socket *rs)
250{
Harald Welte34260c82016-11-26 09:27:04 +0100251 int rc;
252
Harald Welte6e831b72020-10-18 22:59:58 +0200253 osmo_fd_setup(&rs->rtp_bfd, rtp_session_get_rtp_socket(rs->sess), OSMO_FD_READ, osmo_rtp_fd_cb, rs, 0);
254 osmo_fd_setup(&rs->rtcp_bfd, rtp_session_get_rtcp_socket(rs->sess), OSMO_FD_READ, osmo_rtcp_fd_cb, rs, 0);
Harald Welte41d0d842011-09-03 15:33:24 +0200255
Harald Welte34260c82016-11-26 09:27:04 +0100256 rc = osmo_fd_register(&rs->rtp_bfd);
257 if (rc < 0)
258 return rc;
259
260 rc = osmo_fd_register(&rs->rtcp_bfd);
261 if (rc < 0) {
262 osmo_fd_unregister(&rs->rtp_bfd);
263 return rc;
264 }
Harald Welte41d0d842011-09-03 15:33:24 +0200265
266 return 0;
267}
268
269static void create_payload_types()
270{
271 PayloadType *pt;
272
273 /* EFR */
274 pt = payload_type_new();
275 pt->type = PAYLOAD_AUDIO_PACKETIZED;
276 pt->clock_rate = 8000;
277 pt->mime_type = "EFR";
278 pt->normal_bitrate = 12200;
279 pt->channels = 1;
280 payload_type_efr = pt;
281
282 /* HR */
283 pt = payload_type_new();
284 pt->type = PAYLOAD_AUDIO_PACKETIZED;
285 pt->clock_rate = 8000;
286 pt->mime_type = "HR";
287 pt->normal_bitrate = 6750; /* FIXME */
288 pt->channels = 1;
289 payload_type_hr = pt;
290
291 /* create a new RTP profile as clone of AV profile */
292 osmo_pt_profile = rtp_profile_clone(&av_profile);
293
294 /* add the GSM specific payload types. They are all dynamically
295 * assigned, but in the Osmocom GSM system we have allocated
296 * them as follows: */
297 rtp_profile_set_payload(osmo_pt_profile, RTP_PT_GSM_EFR, payload_type_efr);
298 rtp_profile_set_payload(osmo_pt_profile, RTP_PT_GSM_HALF, payload_type_hr);
299 rtp_profile_set_payload(osmo_pt_profile, RTP_PT_AMR, &payload_type_amr);
300}
301
302/* public functions */
303
304/*! \brief initialize Osmocom RTP code
Harald Weltefcb1fe82011-09-07 11:51:52 +0200305 * \param[in] ctx default talloc context for library-internal allocations
Harald Welte41d0d842011-09-03 15:33:24 +0200306 */
307void osmo_rtp_init(void *ctx)
308{
309 tall_rtp_ctx = ctx;
310 ortp_set_memory_functions(&osmo_ortp_memfn);
311 ortp_init();
Pau Espin Pedrolc42bf192017-03-24 17:26:49 +0100312 ortp_set_log_level_mask(
313#if HAVE_ORTP_LOG_DOMAIN
314 ORTP_LOG_DOMAIN,
315#endif
316 0xffff);
317
Harald Welte41d0d842011-09-03 15:33:24 +0200318 ortp_set_log_handler(my_ortp_logfn);
319 create_payload_types();
320}
321
Maxbf42e0a2016-12-15 19:57:57 +0100322/*! \brief Set Osmocom RTP socket parameters
323 * \param[in] rs OsmoRTP socket
324 * \param[in] param defined which parameter to set
325 OSMO_RTP_P_JITBUF - enables regular jitter buffering
326 OSMO_RTP_P_JIT_ADAP - enables adaptive jitter buffering
327 * \param[in] val Size of jitter buffer (in ms), 0 means disable buffering
328 * \returns negative value on error, 0 or 1 otherwise
329 (depending on whether given jitter buffering is enabled)
330 */
Harald Welte65a50892011-09-08 14:42:58 +0200331int osmo_rtp_socket_set_param(struct osmo_rtp_socket *rs,
332 enum osmo_rtp_param param, int val)
333{
Harald Welte65a50892011-09-08 14:42:58 +0200334 switch (param) {
Maxbf42e0a2016-12-15 19:57:57 +0100335 case OSMO_RTP_P_JIT_ADAP:
336 rtp_session_enable_adaptive_jitter_compensation(rs->sess,
337 (bool)val);
338 /* fall-through on-purpose - we have to set val anyway */
Harald Welte65a50892011-09-08 14:42:58 +0200339 case OSMO_RTP_P_JITBUF:
Andreas Eversberg41bc6152013-02-14 11:03:26 +0100340 rtp_session_enable_jitter_buffer(rs->sess,
341 (val) ? TRUE : FALSE);
342 if (val)
343 rtp_session_set_jitter_compensation(rs->sess, val);
Harald Welte65a50892011-09-08 14:42:58 +0200344 break;
Harald Welte65a50892011-09-08 14:42:58 +0200345 default:
346 return -EINVAL;
347 }
Maxbf42e0a2016-12-15 19:57:57 +0100348 if (param == OSMO_RTP_P_JIT_ADAP)
349 return rtp_session_adaptive_jitter_compensation_enabled(rs->sess);
350 return rtp_session_jitter_buffer_enabled(rs->sess);
Harald Welte65a50892011-09-08 14:42:58 +0200351}
352
Harald Weltefcb1fe82011-09-07 11:51:52 +0200353/*! \brief Create a new RTP socket
354 * \param[in] talloc_cxt talloc context for this allocation. NULL for
355 * dafault context
356 * \param[in] flags Flags like OSMO_RTP_F_POLL
357 * \returns pointer to library-allocated \a struct osmo_rtp_socket
358 */
Harald Welte9b737df2011-09-07 00:59:11 +0200359struct osmo_rtp_socket *osmo_rtp_socket_create(void *talloc_ctx, unsigned int flags)
Harald Welte41d0d842011-09-03 15:33:24 +0200360{
361 struct osmo_rtp_socket *rs;
362
363 if (!talloc_ctx)
364 talloc_ctx = tall_rtp_ctx;
365
366 rs = talloc_zero(talloc_ctx, struct osmo_rtp_socket);
367 if (!rs)
368 return NULL;
369
Maxe54d7bc2016-04-29 12:39:33 +0200370 rs->flags = OSMO_RTP_F_DISABLED | flags;
Harald Welte41d0d842011-09-03 15:33:24 +0200371 rs->sess = rtp_session_new(RTP_SESSION_SENDRECV);
372 if (!rs->sess) {
373 talloc_free(rs);
374 return NULL;
375 }
376 rtp_session_set_data(rs->sess, rs);
377 rtp_session_set_profile(rs->sess, osmo_pt_profile);
Harald Welte9b737df2011-09-07 00:59:11 +0200378 rtp_session_set_jitter_compensation(rs->sess, 100);
Harald Welte41d0d842011-09-03 15:33:24 +0200379
Harald Welte54c919e2020-03-08 21:50:01 +0100380 /* ortp >= 0.24.0 doesn't differentiate between SO_REUSEADDR and
381 * SO_REUSEPORT, and has both enabled by default. The latter means that
382 * we can end up with non-unique port bindings as we will not fail to
383 * bind the same port twice */
384 rtp_session_set_reuseaddr(rs->sess, false);
385
Harald Welte41d0d842011-09-03 15:33:24 +0200386 rtp_session_signal_connect(rs->sess, "ssrc_changed",
387 (RtpCallback) ortp_sig_cb_ssrc,
Pau Espin Pedrol05df2d62017-06-21 07:37:45 +0200388 RTP_SIGNAL_PTR_CAST(rs));
389
Harald Welte41d0d842011-09-03 15:33:24 +0200390 rtp_session_signal_connect(rs->sess, "payload_type_changed",
391 (RtpCallback) ortp_sig_cb_pt,
Pau Espin Pedrol05df2d62017-06-21 07:37:45 +0200392 RTP_SIGNAL_PTR_CAST(rs));
393
Harald Welte41d0d842011-09-03 15:33:24 +0200394 rtp_session_signal_connect(rs->sess, "network_error",
395 (RtpCallback) ortp_sig_cb_net,
Pau Espin Pedrol05df2d62017-06-21 07:37:45 +0200396 RTP_SIGNAL_PTR_CAST(rs));
397
Harald Welte41d0d842011-09-03 15:33:24 +0200398 rtp_session_signal_connect(rs->sess, "timestamp_jump",
399 (RtpCallback) ortp_sig_cb_ts,
Pau Espin Pedrol05df2d62017-06-21 07:37:45 +0200400 RTP_SIGNAL_PTR_CAST(rs));
Harald Welte41d0d842011-09-03 15:33:24 +0200401
Holger Hans Peter Freytherbf6f1f42014-06-24 10:57:35 +0200402 /* initialize according to the RFC */
403 rtp_session_set_seq_number(rs->sess, random());
Holger Hans Peter Freytherfb6e1e92014-06-24 11:02:01 +0200404 rs->tx_timestamp = random();
Pau Espin Pedrolb0c3a4a2017-06-21 07:25:18 +0200405
Philipp Maierc81b68f2018-05-30 11:00:21 +0200406 /* Make sure ssrc changes are detected immediately */
407 rtp_session_set_ssrc_changed_threshold(rs->sess, 0);
Holger Hans Peter Freytherbf6f1f42014-06-24 10:57:35 +0200408
Harald Welte41d0d842011-09-03 15:33:24 +0200409 return rs;
410}
411
Harald Weltefcb1fe82011-09-07 11:51:52 +0200412/*! \brief bind a RTP socket to a local port
413 * \param[in] rs OsmoRTP socket
414 * \param[in] ip hostname/ip as string
415 * \param[in] port UDP port number, -1 for random selection
416 * \returns 0 on success, <0 on error
417 */
Harald Welte41d0d842011-09-03 15:33:24 +0200418int osmo_rtp_socket_bind(struct osmo_rtp_socket *rs, const char *ip, int port)
419{
Max15d9b792016-04-28 12:05:27 +0200420 int rc, rtcp = (-1 != port) ? port + 1 : -1;
Max80f7c042016-04-29 12:39:34 +0200421 rc = rtp_session_set_local_addr(rs->sess, ip, port, rtcp);
Max15d9b792016-04-28 12:05:27 +0200422
Harald Welte41d0d842011-09-03 15:33:24 +0200423 if (rc < 0)
424 return rc;
425
426 rs->rtp_bfd.fd = rtp_session_get_rtp_socket(rs->sess);
427 rs->rtcp_bfd.fd = rtp_session_get_rtcp_socket(rs->sess);
428
429 return 0;
430}
431
Harald Weltefcb1fe82011-09-07 11:51:52 +0200432/*! \brief connect a OsmoRTP socket to a remote port
433 * \param[in] rs OsmoRTP socket
434 * \param[in] ip String representation of remote hostname or IP address
435 * \param[in] port UDP port number to connect to
436 *
437 * If the OsmoRTP socket is not in POLL mode, this function will also
438 * cause the RTP and RTCP file descriptors to be registred with the
439 * libosmocore select() loop integration.
440 *
441 * \returns 0 on success, <0 in case of error
442 */
Harald Welte41d0d842011-09-03 15:33:24 +0200443int osmo_rtp_socket_connect(struct osmo_rtp_socket *rs, const char *ip, uint16_t port)
444{
445 int rc;
Maxe54d7bc2016-04-29 12:39:33 +0200446 if (!port) {
447 LOGP(DLMIB, LOGL_INFO, "osmo_rtp_socket_connect() refused to "
448 "set remote %s:%u\n", ip, port);
449 return 0;
450 }
Max8c119f72016-04-29 12:39:35 +0200451
Neels Hofmeyra0ff9422016-10-06 15:43:38 +0200452 /* We don't want the connected mode enabled during
453 * rtp_session_set_remote_addr(), because that will already setup a
454 * connection and updating the remote address will no longer have an
455 * effect. Contrary to what one may expect, this must be 0 at first,
456 * and we're setting to 1 further down to establish a connection once
457 * the first RTP packet is received (OS#1661). */
458 rtp_session_set_connected_mode(rs->sess, 0);
459
460 rc = rtp_session_set_remote_addr(rs->sess, ip, port);
461 if (rc < 0)
462 return rc;
463
Harald Welted426d452013-02-09 11:01:19 +0100464 /* enable the use of connect() so later getsockname() will
465 * actually return the IP address that was chosen for the local
466 * sid of the connection */
467 rtp_session_set_connected_mode(rs->sess, 1);
Maxe54d7bc2016-04-29 12:39:33 +0200468 rs->flags &= ~OSMO_RTP_F_DISABLED;
Harald Welted426d452013-02-09 11:01:19 +0100469
Harald Welte9b737df2011-09-07 00:59:11 +0200470 if (rs->flags & OSMO_RTP_F_POLL)
471 return rc;
472 else
473 return osmo_rtp_socket_fdreg(rs);
Harald Welte41d0d842011-09-03 15:33:24 +0200474}
475
Sylvain Munautbab7ae72019-03-11 15:36:13 +0100476/*! \brief Automatically associates a RTP socket with the first incoming UDP packet
477 * \param[in] rs OsmoRTP socket
478 *
479 * The bound RTP socket will wait for incoming RTP packets and as soon as it
480 * sees one, will 'connect' to it, so all replies will go to that sources and
481 * incoming messages from other sources will be discarded. This obviously only
482 * works once.
483 *
484 * \returns 0 on success, <0 in case of error.
485 */
486int osmo_rtp_socket_autoconnect(struct osmo_rtp_socket *rs)
487{
488 rtp_session_set_symmetric_rtp(rs->sess, 1);
489 rtp_session_set_connected_mode(rs->sess, 1);
490 rs->flags &= ~OSMO_RTP_F_DISABLED;
491
492 if (rs->flags & OSMO_RTP_F_POLL)
493 return 0;
494 else
495 return osmo_rtp_socket_fdreg(rs);
496}
497
Pau Espin Pedrol524923a2017-06-28 15:31:46 +0200498/*! \brief Increment timestamp on a RTP socket without sending any packet
499 * \param[in] rs OsmoRTP socket
500 * \param[in] duration duration in number of RTP clock ticks
501 *
502 * Useful to keep the RTP internal clock up to date if an RTP frame should be
503 * send at a given time but no audio content is available. When next packet is
504 * sent, the receiver will see a different increase on the sequence number and
505 * the timestamp, and it should then take it as a synchronization point. For
506 * that same reason, it is advisable to enable the marker bit on the next RTP
507 * packet to be sent after calling this function.
508 *
509 * \returns 0 on success, <0 in case of error.
510 */
511int osmo_rtp_skipped_frame(struct osmo_rtp_socket *rs, unsigned int duration)
512{
513 if (rs->flags & OSMO_RTP_F_DISABLED)
514 return 0;
515
516 rs->tx_timestamp += duration;
517 return 0;
518}
519
Harald Weltefcb1fe82011-09-07 11:51:52 +0200520/*! \brief Send one RTP frame via a RTP socket
521 * \param[in] rs OsmoRTP socket
522 * \param[in] payload pointer to buffer with RTP payload data
523 * \param[in] payload_len length of \a payload in bytes
524 * \param[in] duration duration in number of RTP clock ticks
525 * \returns 0 on success, <0 in case of error.
526 */
Harald Welte41d0d842011-09-03 15:33:24 +0200527int osmo_rtp_send_frame(struct osmo_rtp_socket *rs, const uint8_t *payload,
528 unsigned int payload_len, unsigned int duration)
529{
Max73b9bc72016-05-18 15:52:37 +0200530 return osmo_rtp_send_frame_ext(rs, payload, payload_len, duration,
531 false);
532}
533
534/*! \brief Send one RTP frame via a RTP socket
535 * \param[in] rs OsmoRTP socket
536 * \param[in] payload pointer to buffer with RTP payload data
537 * \param[in] payload_len length of \a payload in bytes
538 * \param[in] duration duration in number of RTP clock ticks
539 * \param[in] marker the status of Marker bit in RTP header
540 * \returns 0 on success, <0 in case of error.
541 */
542int osmo_rtp_send_frame_ext(struct osmo_rtp_socket *rs, const uint8_t *payload,
543 unsigned int payload_len, unsigned int duration,
544 bool marker)
545{
Harald Welte41d0d842011-09-03 15:33:24 +0200546 mblk_t *mblk;
547 int rc;
548
Maxe54d7bc2016-04-29 12:39:33 +0200549 if (rs->flags & OSMO_RTP_F_DISABLED)
550 return 0;
551
Harald Welte41d0d842011-09-03 15:33:24 +0200552 mblk = rtp_session_create_packet(rs->sess, RTP_FIXED_HEADER_SIZE,
553 payload, payload_len);
554 if (!mblk)
555 return -ENOMEM;
556
Max73b9bc72016-05-18 15:52:37 +0200557 rtp_set_markbit(mblk, marker);
Harald Welte41d0d842011-09-03 15:33:24 +0200558 rc = rtp_session_sendm_with_ts(rs->sess, mblk,
559 rs->tx_timestamp);
Pau Espin Pedrol9e992c22017-06-29 18:04:18 +0200560 rs->tx_timestamp += duration;
Harald Welte41d0d842011-09-03 15:33:24 +0200561 if (rc < 0) {
562 /* no need to free() the mblk, as rtp_session_rtp_send()
563 * unconditionally free()s the mblk even in case of
564 * error */
565 return rc;
566 }
567
568 return rc;
569}
570
Harald Weltefcb1fe82011-09-07 11:51:52 +0200571/*! \brief Set the payload type of a RTP socket
572 * \param[in] rs OsmoRTP socket
573 * \param[in] payload_type RTP payload type
574 * \returns 0 on success, < 0 otherwise
575 */
Harald Welte41d0d842011-09-03 15:33:24 +0200576int osmo_rtp_socket_set_pt(struct osmo_rtp_socket *rs, int payload_type)
577{
578 int rc;
579
580 rc = rtp_session_set_payload_type(rs->sess, payload_type);
581 //rtp_session_set_rtcp_report_interval(rs->sess, 5*1000);
582
583 return rc;
584}
585
Oliver Smith3c514822020-03-06 14:31:47 +0100586/*! \brief Set the DSCP (Differentiated Services Code Point) for outgoing RTP packets
587 * \param[in] rs OsmoRTP socket
588 * \param[in] dscp DSCP value
589 * \returns 0 on success, < 0 otherwise
590 */
591int osmo_rtp_socket_set_dscp(struct osmo_rtp_socket *rs, int dscp)
592{
593 return rtp_session_set_dscp(rs->sess, dscp);
594}
595
Harald Welte14277e32021-04-29 21:38:25 +0200596/*! \brief Set the socket priority for outgoing RTP packets
597 * \param[in] rs OsmoRTP socket
598 * \param[in] prio socket priority
599 * \returns 0 on success, < 0 otherwise
600 */
601int osmo_rtp_socket_set_priority(struct osmo_rtp_socket *rs, uint8_t prio)
602{
603 int rc;
604
605 rc = osmo_sock_set_priority(rs->rtp_bfd.fd, prio);
606 if (rc < 0)
607 return rc;
608 return osmo_sock_set_priority(rs->rtcp_bfd.fd, prio);
609}
610
Harald Weltefcb1fe82011-09-07 11:51:52 +0200611/*! \brief completely close the RTP socket and release all resources
612 * \param[in] rs OsmoRTP socket to be released
613 * \returns 0 on success
614 */
Harald Welte41d0d842011-09-03 15:33:24 +0200615int osmo_rtp_socket_free(struct osmo_rtp_socket *rs)
616{
617 if (rs->rtp_bfd.list.next && rs->rtp_bfd.list.next != LLIST_POISON1)
618 osmo_fd_unregister(&rs->rtp_bfd);
619
620 if (rs->rtcp_bfd.list.next && rs->rtcp_bfd.list.next != LLIST_POISON1)
621 osmo_fd_unregister(&rs->rtcp_bfd);
622
623 if (rs->sess) {
624 rtp_session_release_sockets(rs->sess);
625 rtp_session_destroy(rs->sess);
626 rs->sess = NULL;
627 }
628
629 talloc_free(rs);
630
631 return 0;
632}
633
Harald Weltefcb1fe82011-09-07 11:51:52 +0200634/*! \brief obtain the locally bound IPv4 address and UDP port
635 * \param[in] rs OsmoRTP socket
636 * \param[out] ip Pointer to caller-allocated uint32_t for IPv4 address
637 * \oaram[out] port Pointer to caller-allocated int for UDP port number
638 * \returns 0 on success, <0 on error, -EIO in case of IPv6 socket
639 */
Harald Welte41d0d842011-09-03 15:33:24 +0200640int osmo_rtp_get_bound_ip_port(struct osmo_rtp_socket *rs,
641 uint32_t *ip, int *port)
642{
643 int rc;
644 struct sockaddr_storage ss;
645 struct sockaddr_in *sin = (struct sockaddr_in *) &ss;
646 socklen_t alen = sizeof(ss);
647
648 rc = getsockname(rs->rtp_bfd.fd, (struct sockaddr *)&ss, &alen);
649 if (rc < 0)
650 return rc;
651
652 if (ss.ss_family != AF_INET)
653 return -EIO;
654
655 *ip = ntohl(sin->sin_addr.s_addr);
656 *port = rtp_session_get_local_port(rs->sess);
657
658 return 0;
659}
660
Harald Weltefcb1fe82011-09-07 11:51:52 +0200661/*! \brief obtain the locally bound address and port
662 * \param[in] rs OsmoRTP socket
663 * \param[out] addr caller-allocated char ** to which the string pointer for
664 * the address is stored
665 * \param[out] port caller-allocated int * to which the port number is
666 * stored
667 * \returns 0 on success, <0 in case of error
668 */
Harald Welte41d0d842011-09-03 15:33:24 +0200669int osmo_rtp_get_bound_addr(struct osmo_rtp_socket *rs,
670 const char **addr, int *port)
671{
672 int rc;
673 struct sockaddr_storage ss;
674 socklen_t alen = sizeof(ss);
675 static char hostbuf[256];
676
677 memset(hostbuf, 0, sizeof(hostbuf));
678
679 rc = getsockname(rs->rtp_bfd.fd, (struct sockaddr *)&ss, &alen);
680 if (rc < 0)
681 return rc;
682
683 rc = getnameinfo((struct sockaddr *)&ss, alen,
684 hostbuf, sizeof(hostbuf), NULL, 0,
685 NI_NUMERICHOST);
686 if (rc < 0)
687 return rc;
688
689 *port = rtp_session_get_local_port(rs->sess);
690 *addr = hostbuf;
691
692 return 0;
693}
Harald Welte65a50892011-09-08 14:42:58 +0200694
695
696void osmo_rtp_socket_log_stats(struct osmo_rtp_socket *rs,
697 int subsys, int level,
698 const char *pfx)
699{
700 const rtp_stats_t *stats;
701
702 stats = rtp_session_get_stats(rs->sess);
703 if (!stats)
704 return;
705
706 LOGP(subsys, level, "%sRTP Tx(%"PRIu64" pkts, %"PRIu64" bytes) "
707 "Rx(%"PRIu64" pkts, %"PRIu64" bytes, %"PRIu64" late, "
708 "%"PRIu64" loss, %"PRIu64" qmax)\n",
709 pfx, stats->packet_sent, stats->sent,
710 stats->packet_recv, stats->hw_recv, stats->outoftime,
711 stats->cum_packet_loss, stats->discarded);
712}
Holger Hans Peter Freytherfe019082015-09-21 10:52:52 +0200713
714void osmo_rtp_socket_stats(struct osmo_rtp_socket *rs,
715 uint32_t *sent_packets, uint32_t *sent_octets,
716 uint32_t *recv_packets, uint32_t *recv_octets,
717 uint32_t *recv_lost, uint32_t *last_jitter)
718{
719 const rtp_stats_t *stats;
Holger Hans Peter Freytherfe019082015-09-21 10:52:52 +0200720
721 *sent_packets = *sent_octets = *recv_packets = *recv_octets = 0;
722 *recv_lost = *last_jitter = 0;
723
724 stats = rtp_session_get_stats(rs->sess);
725 if (stats) {
726 /* truncate from 64bit to 32bit here */
727 *sent_packets = stats->packet_sent;
728 *sent_octets = stats->sent;
729 *recv_packets = stats->packet_recv;
730 *recv_octets = stats->recv;
731 *recv_lost = stats->cum_packet_loss;
732 }
733
Holger Hans Peter Freyther71bc9e22015-09-21 12:18:37 +0200734 const jitter_stats_t *jitter;
735
Holger Hans Peter Freytherfe019082015-09-21 10:52:52 +0200736 jitter = rtp_session_get_jitter_stats(rs->sess);
737 if (jitter)
738 *last_jitter = jitter->jitter;
739}
Harald Welted1dd22c2017-12-03 10:00:16 +0100740
741void osmo_rtp_set_source_desc(struct osmo_rtp_socket *rs, const char *cname,
742 const char *name, const char *email, const char *phone,
743 const char *loc, const char *tool, const char *note)
744{
745 rtp_session_set_source_description(rs->sess, cname, name, email, phone, loc, tool, note);
746}