blob: 13e24788b465c96712f01d30fe1f6fc2f019eafe [file] [log] [blame]
Harald Welte41d0d842011-09-03 15:33:24 +02001/* (C) 2011 by Harald Welte <laforge@gnumonks.org>
2 * (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>
36#include <osmocom/trau/osmo_ortp.h>
37
38#include <ortp/ortp.h>
Harald Welte65a50892011-09-08 14:42:58 +020039#include <ortp/rtp.h>
Harald Welte0b5ffc12011-10-22 15:58:02 +020040#include <ortp/port.h>
Harald Weltee7a3f432013-02-19 13:35:22 +010041#include <ortp/rtpsession.h>
Harald Welte41d0d842011-09-03 15:33:24 +020042
Harald Welte2bfc01d2013-10-06 12:23:35 +020043#include "config.h"
Harald Welte41d0d842011-09-03 15:33:24 +020044
45static PayloadType *payload_type_efr;
46static PayloadType *payload_type_hr;
47static RtpProfile *osmo_pt_profile;
48
49static void *tall_rtp_ctx;
50
51/* malloc integration */
52
53static void *osmo_ortp_malloc(size_t sz)
54{
55 return talloc_size(tall_rtp_ctx, sz);
56}
57
58static void *osmo_ortp_realloc(void *ptr, size_t sz)
59{
60 return talloc_realloc_size(tall_rtp_ctx, ptr, sz);
61}
62
63static void osmo_ortp_free(void *ptr)
64{
65 talloc_free(ptr);
66}
67
68static OrtpMemoryFunctions osmo_ortp_memfn = {
69 .malloc_fun = osmo_ortp_malloc,
70 .realloc_fun = osmo_ortp_realloc,
71 .free_fun = osmo_ortp_free
72};
73
74/* logging */
75
76struct level_map {
77 OrtpLogLevel ortp;
78 int osmo_level;
79};
80static const struct level_map level_map[] = {
81 { ORTP_DEBUG, LOGL_DEBUG },
82 { ORTP_MESSAGE, LOGL_INFO },
83 { ORTP_WARNING, LOGL_NOTICE },
84 { ORTP_ERROR, LOGL_ERROR },
85 { ORTP_FATAL, LOGL_FATAL },
86};
87static int ortp_to_osmo_lvl(OrtpLogLevel lev)
88{
89 int i;
90
91 for (i = 0; i < ARRAY_SIZE(level_map); i++) {
92 if (level_map[i].ortp == lev)
93 return level_map[i].osmo_level;
94 }
95 /* default */
96 return LOGL_ERROR;
97}
98
Pau Espin Pedrolc42bf192017-03-24 17:26:49 +010099static void my_ortp_logfn(
100#if HAVE_ORTP_LOG_DOMAIN
101 const char *domain,
102#endif
103 OrtpLogLevel lev, const char *fmt, va_list args)
Harald Welte41d0d842011-09-03 15:33:24 +0200104{
105 osmo_vlogp(DLMIB, ortp_to_osmo_lvl(lev), __FILE__, 0,
106 0, fmt, args);
107}
108
109/* ORTP signal callbacks */
110
111static void ortp_sig_cb_ssrc(RtpSession *rs, void *data)
112{
Harald Weltee7a3f432013-02-19 13:35:22 +0100113 int port = rtp_session_get_local_port(rs);
Harald Weltee7a3f432013-02-19 13:35:22 +0100114 uint32_t ssrc = rtp_session_get_recv_ssrc(rs);
Harald Weltee7a3f432013-02-19 13:35:22 +0100115
116 LOGP(DLMIB, LOGL_INFO,
Philipp Maier28eeb6b2018-05-30 10:52:41 +0200117 "osmo-ortp(%d): ssrc_changed to 0x%08x, resetting\n", port, ssrc);
118 rtp_session_reset(rs);
Harald Welte41d0d842011-09-03 15:33:24 +0200119}
120
121static void ortp_sig_cb_pt(RtpSession *rs, void *data)
122{
Harald Weltee7a3f432013-02-19 13:35:22 +0100123 int port = rtp_session_get_local_port(rs);
124 int pt = rtp_session_get_recv_payload_type(rs);
125
126 LOGP(DLMIB, LOGL_NOTICE,
127 "osmo-ortp(%d): payload_type_changed to 0x%02x\n", port, pt);
Harald Welte41d0d842011-09-03 15:33:24 +0200128}
129
130static void ortp_sig_cb_net(RtpSession *rs, void *data)
131{
Harald Weltee7a3f432013-02-19 13:35:22 +0100132 int port = rtp_session_get_local_port(rs);
133
134 LOGP(DLMIB, LOGL_ERROR,
Max7c840be2016-12-05 16:13:53 +0100135 "osmo-ortp(%d): network_error %s\n", port, (char *)data);
Harald Welte41d0d842011-09-03 15:33:24 +0200136}
137
138static void ortp_sig_cb_ts(RtpSession *rs, void *data)
139{
Harald Weltee7a3f432013-02-19 13:35:22 +0100140 int port = rtp_session_get_local_port(rs);
141 uint32_t ts = rtp_session_get_current_recv_ts(rs);
142
143 LOGP(DLMIB, LOGL_NOTICE,
Yves Godin2c32f0a2016-10-06 15:55:06 +0200144 "osmo-ortp(%d): timestamp_jump, new TS %d, resyncing\n", port, ts);
145 rtp_session_resync(rs);
Harald Welte41d0d842011-09-03 15:33:24 +0200146}
147
Maxf6906002016-10-24 14:10:10 +0200148static inline bool recv_with_cb(struct osmo_rtp_socket *rs)
149{
Harald Welte7895e042016-10-28 10:40:24 +0200150 uint8_t *payload;
Maxf6906002016-10-24 14:10:10 +0200151 mblk_t *mblk = rtp_session_recvm_with_ts(rs->sess, rs->rx_user_ts);
152 if (!mblk)
153 return false;
154
Harald Welte7895e042016-10-28 10:40:24 +0200155 int plen = rtp_get_payload(mblk, &payload);
Maxf6906002016-10-24 14:10:10 +0200156 /* hand into receiver */
157 if (rs->rx_cb && plen > 0)
Harald Welte7895e042016-10-28 10:40:24 +0200158 rs->rx_cb(rs, payload, plen, rtp_get_seqnumber(mblk),
Maxf6906002016-10-24 14:10:10 +0200159 rtp_get_timestamp(mblk), rtp_get_markbit(mblk));
160 freemsg(mblk);
161 if (plen > 0)
162 return true;
163 return false;
164}
Harald Welte41d0d842011-09-03 15:33:24 +0200165
Harald Welte9b737df2011-09-07 00:59:11 +0200166/*! \brief poll the socket for incoming data
167 * \param[in] rs the socket to be polled
168 * \returns number of packets received + handed to the rx_cb
169 */
170int osmo_rtp_socket_poll(struct osmo_rtp_socket *rs)
171{
Maxe54d7bc2016-04-29 12:39:33 +0200172 if (rs->flags & OSMO_RTP_F_DISABLED)
173 return 0;
Harald Welte9b737df2011-09-07 00:59:11 +0200174
Maxf6906002016-10-24 14:10:10 +0200175 if (recv_with_cb(rs))
Harald Welte9b737df2011-09-07 00:59:11 +0200176 return 1;
Maxf6906002016-10-24 14:10:10 +0200177
Max78d04862016-12-05 16:02:52 +0100178 LOGP(DLMIB, LOGL_INFO, "osmo_rtp_socket_poll(%u): ERROR!\n",
179 rs->rx_user_ts);
Maxf6906002016-10-24 14:10:10 +0200180 return 0;
Harald Welte9b737df2011-09-07 00:59:11 +0200181}
182
Harald Welte41d0d842011-09-03 15:33:24 +0200183/* Osmo FD callbacks */
184
185static int osmo_rtp_fd_cb(struct osmo_fd *fd, unsigned int what)
186{
187 struct osmo_rtp_socket *rs = fd->data;
Harald Welte41d0d842011-09-03 15:33:24 +0200188
189 if (what & BSC_FD_READ) {
Harald Welte9b737df2011-09-07 00:59:11 +0200190 /* in polling mode, we don't want to be called here */
191 if (rs->flags & OSMO_RTP_F_POLL) {
192 fd->when &= ~BSC_FD_READ;
193 return 0;
194 }
Maxf6906002016-10-24 14:10:10 +0200195 if (!recv_with_cb(rs))
Harald Welte9b737df2011-09-07 00:59:11 +0200196 LOGP(DLMIB, LOGL_INFO, "recvm_with_ts(%u): ERROR!\n",
197 rs->rx_user_ts);
Harald Welte41d0d842011-09-03 15:33:24 +0200198 rs->rx_user_ts += 160;
199 }
200 /* writing is not queued at the moment, so BSC_FD_WRITE
201 * shouldn't occur */
202 return 0;
203}
204
Pau Espin Pedrolb0c3a4a2017-06-21 07:25:18 +0200205/* Internal API coming from rtpsession_priv.h, used in osmo_rtcp_fd_cb */
206#pragma message ("Using internal ortp API: rtp_session_rtcp_rec")
207int rtp_session_rtcp_recv(RtpSession * session);
208
Harald Welte41d0d842011-09-03 15:33:24 +0200209static int osmo_rtcp_fd_cb(struct osmo_fd *fd, unsigned int what)
210{
211 struct osmo_rtp_socket *rs = fd->data;
212
213 /* We probably don't need this at all, as
214 * rtp_session_recvm_with_ts() will alway also poll the RTCP
215 * file descriptor for new data */
216 return rtp_session_rtcp_recv(rs->sess);
217}
218
219static int osmo_rtp_socket_fdreg(struct osmo_rtp_socket *rs)
220{
Harald Welte34260c82016-11-26 09:27:04 +0100221 int rc;
222
Harald Welte41d0d842011-09-03 15:33:24 +0200223 rs->rtp_bfd.fd = rtp_session_get_rtp_socket(rs->sess);
224 rs->rtcp_bfd.fd = rtp_session_get_rtcp_socket(rs->sess);
225 rs->rtp_bfd.when = rs->rtcp_bfd.when = BSC_FD_READ;
226 rs->rtp_bfd.data = rs->rtcp_bfd.data = rs;
227 rs->rtp_bfd.cb = osmo_rtp_fd_cb;
228 rs->rtcp_bfd.cb = osmo_rtcp_fd_cb;
229
Harald Welte34260c82016-11-26 09:27:04 +0100230 rc = osmo_fd_register(&rs->rtp_bfd);
231 if (rc < 0)
232 return rc;
233
234 rc = osmo_fd_register(&rs->rtcp_bfd);
235 if (rc < 0) {
236 osmo_fd_unregister(&rs->rtp_bfd);
237 return rc;
238 }
Harald Welte41d0d842011-09-03 15:33:24 +0200239
240 return 0;
241}
242
243static void create_payload_types()
244{
245 PayloadType *pt;
246
247 /* EFR */
248 pt = payload_type_new();
249 pt->type = PAYLOAD_AUDIO_PACKETIZED;
250 pt->clock_rate = 8000;
251 pt->mime_type = "EFR";
252 pt->normal_bitrate = 12200;
253 pt->channels = 1;
254 payload_type_efr = pt;
255
256 /* HR */
257 pt = payload_type_new();
258 pt->type = PAYLOAD_AUDIO_PACKETIZED;
259 pt->clock_rate = 8000;
260 pt->mime_type = "HR";
261 pt->normal_bitrate = 6750; /* FIXME */
262 pt->channels = 1;
263 payload_type_hr = pt;
264
265 /* create a new RTP profile as clone of AV profile */
266 osmo_pt_profile = rtp_profile_clone(&av_profile);
267
268 /* add the GSM specific payload types. They are all dynamically
269 * assigned, but in the Osmocom GSM system we have allocated
270 * them as follows: */
271 rtp_profile_set_payload(osmo_pt_profile, RTP_PT_GSM_EFR, payload_type_efr);
272 rtp_profile_set_payload(osmo_pt_profile, RTP_PT_GSM_HALF, payload_type_hr);
273 rtp_profile_set_payload(osmo_pt_profile, RTP_PT_AMR, &payload_type_amr);
274}
275
276/* public functions */
277
278/*! \brief initialize Osmocom RTP code
Harald Weltefcb1fe82011-09-07 11:51:52 +0200279 * \param[in] ctx default talloc context for library-internal allocations
Harald Welte41d0d842011-09-03 15:33:24 +0200280 */
281void osmo_rtp_init(void *ctx)
282{
283 tall_rtp_ctx = ctx;
284 ortp_set_memory_functions(&osmo_ortp_memfn);
285 ortp_init();
Pau Espin Pedrolc42bf192017-03-24 17:26:49 +0100286 ortp_set_log_level_mask(
287#if HAVE_ORTP_LOG_DOMAIN
288 ORTP_LOG_DOMAIN,
289#endif
290 0xffff);
291
Harald Welte41d0d842011-09-03 15:33:24 +0200292 ortp_set_log_handler(my_ortp_logfn);
293 create_payload_types();
Philipp Maier76322782018-05-29 16:04:41 +0200294 ortp_scheduler_init();
Harald Welte41d0d842011-09-03 15:33:24 +0200295}
296
Maxbf42e0a2016-12-15 19:57:57 +0100297/*! \brief Set Osmocom RTP socket parameters
298 * \param[in] rs OsmoRTP socket
299 * \param[in] param defined which parameter to set
300 OSMO_RTP_P_JITBUF - enables regular jitter buffering
301 OSMO_RTP_P_JIT_ADAP - enables adaptive jitter buffering
302 * \param[in] val Size of jitter buffer (in ms), 0 means disable buffering
303 * \returns negative value on error, 0 or 1 otherwise
304 (depending on whether given jitter buffering is enabled)
305 */
Harald Welte65a50892011-09-08 14:42:58 +0200306int osmo_rtp_socket_set_param(struct osmo_rtp_socket *rs,
307 enum osmo_rtp_param param, int val)
308{
Harald Welte65a50892011-09-08 14:42:58 +0200309 switch (param) {
Maxbf42e0a2016-12-15 19:57:57 +0100310 case OSMO_RTP_P_JIT_ADAP:
311 rtp_session_enable_adaptive_jitter_compensation(rs->sess,
312 (bool)val);
313 /* fall-through on-purpose - we have to set val anyway */
Harald Welte65a50892011-09-08 14:42:58 +0200314 case OSMO_RTP_P_JITBUF:
Andreas Eversberg41bc6152013-02-14 11:03:26 +0100315 rtp_session_enable_jitter_buffer(rs->sess,
316 (val) ? TRUE : FALSE);
317 if (val)
318 rtp_session_set_jitter_compensation(rs->sess, val);
Harald Welte65a50892011-09-08 14:42:58 +0200319 break;
Harald Welte65a50892011-09-08 14:42:58 +0200320 default:
321 return -EINVAL;
322 }
Maxbf42e0a2016-12-15 19:57:57 +0100323 if (param == OSMO_RTP_P_JIT_ADAP)
324 return rtp_session_adaptive_jitter_compensation_enabled(rs->sess);
325 return rtp_session_jitter_buffer_enabled(rs->sess);
Harald Welte65a50892011-09-08 14:42:58 +0200326}
327
Harald Weltefcb1fe82011-09-07 11:51:52 +0200328/*! \brief Create a new RTP socket
329 * \param[in] talloc_cxt talloc context for this allocation. NULL for
330 * dafault context
331 * \param[in] flags Flags like OSMO_RTP_F_POLL
332 * \returns pointer to library-allocated \a struct osmo_rtp_socket
333 */
Harald Welte9b737df2011-09-07 00:59:11 +0200334struct osmo_rtp_socket *osmo_rtp_socket_create(void *talloc_ctx, unsigned int flags)
Harald Welte41d0d842011-09-03 15:33:24 +0200335{
336 struct osmo_rtp_socket *rs;
337
338 if (!talloc_ctx)
339 talloc_ctx = tall_rtp_ctx;
340
341 rs = talloc_zero(talloc_ctx, struct osmo_rtp_socket);
342 if (!rs)
343 return NULL;
344
Maxe54d7bc2016-04-29 12:39:33 +0200345 rs->flags = OSMO_RTP_F_DISABLED | flags;
Harald Welte41d0d842011-09-03 15:33:24 +0200346 rs->sess = rtp_session_new(RTP_SESSION_SENDRECV);
347 if (!rs->sess) {
348 talloc_free(rs);
349 return NULL;
350 }
351 rtp_session_set_data(rs->sess, rs);
352 rtp_session_set_profile(rs->sess, osmo_pt_profile);
Harald Welte9b737df2011-09-07 00:59:11 +0200353 rtp_session_set_jitter_compensation(rs->sess, 100);
Philipp Maier5a236ce2018-05-30 14:36:55 +0200354 rtp_session_set_scheduling_mode(rs->sess, TRUE);
Harald Welte41d0d842011-09-03 15:33:24 +0200355
356 rtp_session_signal_connect(rs->sess, "ssrc_changed",
357 (RtpCallback) ortp_sig_cb_ssrc,
Pau Espin Pedrol05df2d62017-06-21 07:37:45 +0200358 RTP_SIGNAL_PTR_CAST(rs));
359
Harald Welte41d0d842011-09-03 15:33:24 +0200360 rtp_session_signal_connect(rs->sess, "payload_type_changed",
361 (RtpCallback) ortp_sig_cb_pt,
Pau Espin Pedrol05df2d62017-06-21 07:37:45 +0200362 RTP_SIGNAL_PTR_CAST(rs));
363
Harald Welte41d0d842011-09-03 15:33:24 +0200364 rtp_session_signal_connect(rs->sess, "network_error",
365 (RtpCallback) ortp_sig_cb_net,
Pau Espin Pedrol05df2d62017-06-21 07:37:45 +0200366 RTP_SIGNAL_PTR_CAST(rs));
367
Harald Welte41d0d842011-09-03 15:33:24 +0200368 rtp_session_signal_connect(rs->sess, "timestamp_jump",
369 (RtpCallback) ortp_sig_cb_ts,
Pau Espin Pedrol05df2d62017-06-21 07:37:45 +0200370 RTP_SIGNAL_PTR_CAST(rs));
Harald Welte41d0d842011-09-03 15:33:24 +0200371
Holger Hans Peter Freytherbf6f1f42014-06-24 10:57:35 +0200372 /* initialize according to the RFC */
373 rtp_session_set_seq_number(rs->sess, random());
Holger Hans Peter Freytherfb6e1e92014-06-24 11:02:01 +0200374 rs->tx_timestamp = random();
Pau Espin Pedrolb0c3a4a2017-06-21 07:25:18 +0200375
Philipp Maierc81b68f2018-05-30 11:00:21 +0200376 /* Make sure ssrc changes are detected immediately */
377 rtp_session_set_ssrc_changed_threshold(rs->sess, 0);
Holger Hans Peter Freytherbf6f1f42014-06-24 10:57:35 +0200378
Harald Welte41d0d842011-09-03 15:33:24 +0200379 return rs;
380}
381
Harald Weltefcb1fe82011-09-07 11:51:52 +0200382/*! \brief bind a RTP socket to a local port
383 * \param[in] rs OsmoRTP socket
384 * \param[in] ip hostname/ip as string
385 * \param[in] port UDP port number, -1 for random selection
386 * \returns 0 on success, <0 on error
387 */
Harald Welte41d0d842011-09-03 15:33:24 +0200388int osmo_rtp_socket_bind(struct osmo_rtp_socket *rs, const char *ip, int port)
389{
Max15d9b792016-04-28 12:05:27 +0200390 int rc, rtcp = (-1 != port) ? port + 1 : -1;
Max80f7c042016-04-29 12:39:34 +0200391 rc = rtp_session_set_local_addr(rs->sess, ip, port, rtcp);
Max15d9b792016-04-28 12:05:27 +0200392
Harald Welte41d0d842011-09-03 15:33:24 +0200393 if (rc < 0)
394 return rc;
395
396 rs->rtp_bfd.fd = rtp_session_get_rtp_socket(rs->sess);
397 rs->rtcp_bfd.fd = rtp_session_get_rtcp_socket(rs->sess);
398
399 return 0;
400}
401
Harald Weltefcb1fe82011-09-07 11:51:52 +0200402/*! \brief connect a OsmoRTP socket to a remote port
403 * \param[in] rs OsmoRTP socket
404 * \param[in] ip String representation of remote hostname or IP address
405 * \param[in] port UDP port number to connect to
406 *
407 * If the OsmoRTP socket is not in POLL mode, this function will also
408 * cause the RTP and RTCP file descriptors to be registred with the
409 * libosmocore select() loop integration.
410 *
411 * \returns 0 on success, <0 in case of error
412 */
Harald Welte41d0d842011-09-03 15:33:24 +0200413int osmo_rtp_socket_connect(struct osmo_rtp_socket *rs, const char *ip, uint16_t port)
414{
415 int rc;
Maxe54d7bc2016-04-29 12:39:33 +0200416 if (!port) {
417 LOGP(DLMIB, LOGL_INFO, "osmo_rtp_socket_connect() refused to "
418 "set remote %s:%u\n", ip, port);
419 return 0;
420 }
Max8c119f72016-04-29 12:39:35 +0200421
Neels Hofmeyra0ff9422016-10-06 15:43:38 +0200422 /* We don't want the connected mode enabled during
423 * rtp_session_set_remote_addr(), because that will already setup a
424 * connection and updating the remote address will no longer have an
425 * effect. Contrary to what one may expect, this must be 0 at first,
426 * and we're setting to 1 further down to establish a connection once
427 * the first RTP packet is received (OS#1661). */
428 rtp_session_set_connected_mode(rs->sess, 0);
429
430 rc = rtp_session_set_remote_addr(rs->sess, ip, port);
431 if (rc < 0)
432 return rc;
433
Harald Welted426d452013-02-09 11:01:19 +0100434 /* enable the use of connect() so later getsockname() will
435 * actually return the IP address that was chosen for the local
436 * sid of the connection */
437 rtp_session_set_connected_mode(rs->sess, 1);
Maxe54d7bc2016-04-29 12:39:33 +0200438 rs->flags &= ~OSMO_RTP_F_DISABLED;
Harald Welted426d452013-02-09 11:01:19 +0100439
Harald Welte9b737df2011-09-07 00:59:11 +0200440 if (rs->flags & OSMO_RTP_F_POLL)
441 return rc;
442 else
443 return osmo_rtp_socket_fdreg(rs);
Harald Welte41d0d842011-09-03 15:33:24 +0200444}
445
Pau Espin Pedrol524923a2017-06-28 15:31:46 +0200446/*! \brief Increment timestamp on a RTP socket without sending any packet
447 * \param[in] rs OsmoRTP socket
448 * \param[in] duration duration in number of RTP clock ticks
449 *
450 * Useful to keep the RTP internal clock up to date if an RTP frame should be
451 * send at a given time but no audio content is available. When next packet is
452 * sent, the receiver will see a different increase on the sequence number and
453 * the timestamp, and it should then take it as a synchronization point. For
454 * that same reason, it is advisable to enable the marker bit on the next RTP
455 * packet to be sent after calling this function.
456 *
457 * \returns 0 on success, <0 in case of error.
458 */
459int osmo_rtp_skipped_frame(struct osmo_rtp_socket *rs, unsigned int duration)
460{
461 if (rs->flags & OSMO_RTP_F_DISABLED)
462 return 0;
463
464 rs->tx_timestamp += duration;
465 return 0;
466}
467
Harald Weltefcb1fe82011-09-07 11:51:52 +0200468/*! \brief Send one RTP frame via a RTP socket
469 * \param[in] rs OsmoRTP socket
470 * \param[in] payload pointer to buffer with RTP payload data
471 * \param[in] payload_len length of \a payload in bytes
472 * \param[in] duration duration in number of RTP clock ticks
473 * \returns 0 on success, <0 in case of error.
474 */
Harald Welte41d0d842011-09-03 15:33:24 +0200475int osmo_rtp_send_frame(struct osmo_rtp_socket *rs, const uint8_t *payload,
476 unsigned int payload_len, unsigned int duration)
477{
Max73b9bc72016-05-18 15:52:37 +0200478 return osmo_rtp_send_frame_ext(rs, payload, payload_len, duration,
479 false);
480}
481
482/*! \brief Send one RTP frame via a RTP socket
483 * \param[in] rs OsmoRTP socket
484 * \param[in] payload pointer to buffer with RTP payload data
485 * \param[in] payload_len length of \a payload in bytes
486 * \param[in] duration duration in number of RTP clock ticks
487 * \param[in] marker the status of Marker bit in RTP header
488 * \returns 0 on success, <0 in case of error.
489 */
490int osmo_rtp_send_frame_ext(struct osmo_rtp_socket *rs, const uint8_t *payload,
491 unsigned int payload_len, unsigned int duration,
492 bool marker)
493{
Harald Welte41d0d842011-09-03 15:33:24 +0200494 mblk_t *mblk;
495 int rc;
496
Maxe54d7bc2016-04-29 12:39:33 +0200497 if (rs->flags & OSMO_RTP_F_DISABLED)
498 return 0;
499
Harald Welte41d0d842011-09-03 15:33:24 +0200500 mblk = rtp_session_create_packet(rs->sess, RTP_FIXED_HEADER_SIZE,
501 payload, payload_len);
502 if (!mblk)
503 return -ENOMEM;
504
Max73b9bc72016-05-18 15:52:37 +0200505 rtp_set_markbit(mblk, marker);
Harald Welte41d0d842011-09-03 15:33:24 +0200506 rc = rtp_session_sendm_with_ts(rs->sess, mblk,
507 rs->tx_timestamp);
Pau Espin Pedrol9e992c22017-06-29 18:04:18 +0200508 rs->tx_timestamp += duration;
Harald Welte41d0d842011-09-03 15:33:24 +0200509 if (rc < 0) {
510 /* no need to free() the mblk, as rtp_session_rtp_send()
511 * unconditionally free()s the mblk even in case of
512 * error */
513 return rc;
514 }
515
516 return rc;
517}
518
Harald Weltefcb1fe82011-09-07 11:51:52 +0200519/*! \brief Set the payload type of a RTP socket
520 * \param[in] rs OsmoRTP socket
521 * \param[in] payload_type RTP payload type
522 * \returns 0 on success, < 0 otherwise
523 */
Harald Welte41d0d842011-09-03 15:33:24 +0200524int osmo_rtp_socket_set_pt(struct osmo_rtp_socket *rs, int payload_type)
525{
526 int rc;
527
528 rc = rtp_session_set_payload_type(rs->sess, payload_type);
529 //rtp_session_set_rtcp_report_interval(rs->sess, 5*1000);
530
531 return rc;
532}
533
Harald Weltefcb1fe82011-09-07 11:51:52 +0200534/*! \brief completely close the RTP socket and release all resources
535 * \param[in] rs OsmoRTP socket to be released
536 * \returns 0 on success
537 */
Harald Welte41d0d842011-09-03 15:33:24 +0200538int osmo_rtp_socket_free(struct osmo_rtp_socket *rs)
539{
540 if (rs->rtp_bfd.list.next && rs->rtp_bfd.list.next != LLIST_POISON1)
541 osmo_fd_unregister(&rs->rtp_bfd);
542
543 if (rs->rtcp_bfd.list.next && rs->rtcp_bfd.list.next != LLIST_POISON1)
544 osmo_fd_unregister(&rs->rtcp_bfd);
545
546 if (rs->sess) {
547 rtp_session_release_sockets(rs->sess);
548 rtp_session_destroy(rs->sess);
549 rs->sess = NULL;
550 }
551
552 talloc_free(rs);
553
554 return 0;
555}
556
Harald Weltefcb1fe82011-09-07 11:51:52 +0200557/*! \brief obtain the locally bound IPv4 address and UDP port
558 * \param[in] rs OsmoRTP socket
559 * \param[out] ip Pointer to caller-allocated uint32_t for IPv4 address
560 * \oaram[out] port Pointer to caller-allocated int for UDP port number
561 * \returns 0 on success, <0 on error, -EIO in case of IPv6 socket
562 */
Harald Welte41d0d842011-09-03 15:33:24 +0200563int osmo_rtp_get_bound_ip_port(struct osmo_rtp_socket *rs,
564 uint32_t *ip, int *port)
565{
566 int rc;
567 struct sockaddr_storage ss;
568 struct sockaddr_in *sin = (struct sockaddr_in *) &ss;
569 socklen_t alen = sizeof(ss);
570
571 rc = getsockname(rs->rtp_bfd.fd, (struct sockaddr *)&ss, &alen);
572 if (rc < 0)
573 return rc;
574
575 if (ss.ss_family != AF_INET)
576 return -EIO;
577
578 *ip = ntohl(sin->sin_addr.s_addr);
579 *port = rtp_session_get_local_port(rs->sess);
580
581 return 0;
582}
583
Harald Weltefcb1fe82011-09-07 11:51:52 +0200584/*! \brief obtain the locally bound address and port
585 * \param[in] rs OsmoRTP socket
586 * \param[out] addr caller-allocated char ** to which the string pointer for
587 * the address is stored
588 * \param[out] port caller-allocated int * to which the port number is
589 * stored
590 * \returns 0 on success, <0 in case of error
591 */
Harald Welte41d0d842011-09-03 15:33:24 +0200592int osmo_rtp_get_bound_addr(struct osmo_rtp_socket *rs,
593 const char **addr, int *port)
594{
595 int rc;
596 struct sockaddr_storage ss;
597 socklen_t alen = sizeof(ss);
598 static char hostbuf[256];
599
600 memset(hostbuf, 0, sizeof(hostbuf));
601
602 rc = getsockname(rs->rtp_bfd.fd, (struct sockaddr *)&ss, &alen);
603 if (rc < 0)
604 return rc;
605
606 rc = getnameinfo((struct sockaddr *)&ss, alen,
607 hostbuf, sizeof(hostbuf), NULL, 0,
608 NI_NUMERICHOST);
609 if (rc < 0)
610 return rc;
611
612 *port = rtp_session_get_local_port(rs->sess);
613 *addr = hostbuf;
614
615 return 0;
616}
Harald Welte65a50892011-09-08 14:42:58 +0200617
618
619void osmo_rtp_socket_log_stats(struct osmo_rtp_socket *rs,
620 int subsys, int level,
621 const char *pfx)
622{
623 const rtp_stats_t *stats;
624
625 stats = rtp_session_get_stats(rs->sess);
626 if (!stats)
627 return;
628
629 LOGP(subsys, level, "%sRTP Tx(%"PRIu64" pkts, %"PRIu64" bytes) "
630 "Rx(%"PRIu64" pkts, %"PRIu64" bytes, %"PRIu64" late, "
631 "%"PRIu64" loss, %"PRIu64" qmax)\n",
632 pfx, stats->packet_sent, stats->sent,
633 stats->packet_recv, stats->hw_recv, stats->outoftime,
634 stats->cum_packet_loss, stats->discarded);
635}
Holger Hans Peter Freytherfe019082015-09-21 10:52:52 +0200636
637void osmo_rtp_socket_stats(struct osmo_rtp_socket *rs,
638 uint32_t *sent_packets, uint32_t *sent_octets,
639 uint32_t *recv_packets, uint32_t *recv_octets,
640 uint32_t *recv_lost, uint32_t *last_jitter)
641{
642 const rtp_stats_t *stats;
Holger Hans Peter Freytherfe019082015-09-21 10:52:52 +0200643
644 *sent_packets = *sent_octets = *recv_packets = *recv_octets = 0;
645 *recv_lost = *last_jitter = 0;
646
647 stats = rtp_session_get_stats(rs->sess);
648 if (stats) {
649 /* truncate from 64bit to 32bit here */
650 *sent_packets = stats->packet_sent;
651 *sent_octets = stats->sent;
652 *recv_packets = stats->packet_recv;
653 *recv_octets = stats->recv;
654 *recv_lost = stats->cum_packet_loss;
655 }
656
Holger Hans Peter Freyther71bc9e22015-09-21 12:18:37 +0200657 const jitter_stats_t *jitter;
658
Holger Hans Peter Freytherfe019082015-09-21 10:52:52 +0200659 jitter = rtp_session_get_jitter_stats(rs->sess);
660 if (jitter)
661 *last_jitter = jitter->jitter;
662}
Harald Welted1dd22c2017-12-03 10:00:16 +0100663
664void osmo_rtp_set_source_desc(struct osmo_rtp_socket *rs, const char *cname,
665 const char *name, const char *email, const char *phone,
666 const char *loc, const char *tool, const char *note)
667{
668 rtp_session_set_source_description(rs->sess, cname, name, email, phone, loc, tool, note);
669}