blob: fc63e7fca17e26cb8fdfa19fb5705291ef71b205 [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 *
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>
Max73b9bc72016-05-18 15:52:37 +020026#include <stdbool.h>
Harald Welte65a50892011-09-08 14:42:58 +020027#include <inttypes.h>
Harald Welte41d0d842011-09-03 15:33:24 +020028#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 Welte65a50892011-09-08 14:42:58 +020037#include <ortp/rtp.h>
Harald Welte0b5ffc12011-10-22 15:58:02 +020038#include <ortp/port.h>
Harald Weltee7a3f432013-02-19 13:35:22 +010039#include <ortp/rtpsession.h>
Harald Welte41d0d842011-09-03 15:33:24 +020040
Harald Welte2bfc01d2013-10-06 12:23:35 +020041#include "config.h"
Harald Welte41d0d842011-09-03 15:33:24 +020042
43static PayloadType *payload_type_efr;
44static PayloadType *payload_type_hr;
45static RtpProfile *osmo_pt_profile;
46
47static void *tall_rtp_ctx;
48
49/* malloc integration */
50
51static void *osmo_ortp_malloc(size_t sz)
52{
53 return talloc_size(tall_rtp_ctx, sz);
54}
55
56static void *osmo_ortp_realloc(void *ptr, size_t sz)
57{
58 return talloc_realloc_size(tall_rtp_ctx, ptr, sz);
59}
60
61static void osmo_ortp_free(void *ptr)
62{
63 talloc_free(ptr);
64}
65
66static 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
74struct level_map {
75 OrtpLogLevel ortp;
76 int osmo_level;
77};
78static 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};
85static 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
97static void my_ortp_logfn(OrtpLogLevel lev, const char *fmt,
98 va_list args)
99{
100 osmo_vlogp(DLMIB, ortp_to_osmo_lvl(lev), __FILE__, 0,
101 0, fmt, args);
102}
103
104/* ORTP signal callbacks */
105
106static void ortp_sig_cb_ssrc(RtpSession *rs, void *data)
107{
Harald Weltee7a3f432013-02-19 13:35:22 +0100108 int port = rtp_session_get_local_port(rs);
109#if 0 /* post 0.20.0 ORTP has this function */
110 uint32_t ssrc = rtp_session_get_recv_ssrc(rs);
111#else
112 uint32_t ssrc = rs->rcv.ssrc;
113#endif
114
115 LOGP(DLMIB, LOGL_INFO,
116 "osmo-ortp(%d): ssrc_changed to 0x%08x\n", port, ssrc);
Harald Welte41d0d842011-09-03 15:33:24 +0200117}
118
119static void ortp_sig_cb_pt(RtpSession *rs, void *data)
120{
Harald Weltee7a3f432013-02-19 13:35:22 +0100121 int port = rtp_session_get_local_port(rs);
122 int pt = rtp_session_get_recv_payload_type(rs);
123
124 LOGP(DLMIB, LOGL_NOTICE,
125 "osmo-ortp(%d): payload_type_changed to 0x%02x\n", port, pt);
Harald Welte41d0d842011-09-03 15:33:24 +0200126}
127
128static void ortp_sig_cb_net(RtpSession *rs, void *data)
129{
Harald Weltee7a3f432013-02-19 13:35:22 +0100130 int port = rtp_session_get_local_port(rs);
131
132 LOGP(DLMIB, LOGL_ERROR,
133 "osmo-ortp(%d): network_error\n", port);
Harald Welte41d0d842011-09-03 15:33:24 +0200134}
135
136static void ortp_sig_cb_ts(RtpSession *rs, void *data)
137{
Harald Weltee7a3f432013-02-19 13:35:22 +0100138 int port = rtp_session_get_local_port(rs);
139 uint32_t ts = rtp_session_get_current_recv_ts(rs);
140
141 LOGP(DLMIB, LOGL_NOTICE,
Yves Godin2c32f0a2016-10-06 15:55:06 +0200142 "osmo-ortp(%d): timestamp_jump, new TS %d, resyncing\n", port, ts);
143 rtp_session_resync(rs);
Harald Welte41d0d842011-09-03 15:33:24 +0200144}
145
Maxf6906002016-10-24 14:10:10 +0200146static inline bool recv_with_cb(struct osmo_rtp_socket *rs)
147{
148 mblk_t *mblk = rtp_session_recvm_with_ts(rs->sess, rs->rx_user_ts);
149 if (!mblk)
150 return false;
151
152 int plen = rtp_get_payload(mblk, &mblk->b_rptr);
153 /* hand into receiver */
154 if (rs->rx_cb && plen > 0)
155 rs->rx_cb(rs, mblk->b_rptr, plen, rtp_get_seqnumber(mblk),
156 rtp_get_timestamp(mblk), rtp_get_markbit(mblk));
157 freemsg(mblk);
158 if (plen > 0)
159 return true;
160 return false;
161}
Harald Welte41d0d842011-09-03 15:33:24 +0200162
Harald Welte9b737df2011-09-07 00:59:11 +0200163/*! \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 */
167int osmo_rtp_socket_poll(struct osmo_rtp_socket *rs)
168{
Maxe54d7bc2016-04-29 12:39:33 +0200169 if (rs->flags & OSMO_RTP_F_DISABLED)
170 return 0;
Harald Welte9b737df2011-09-07 00:59:11 +0200171
Maxf6906002016-10-24 14:10:10 +0200172 if (recv_with_cb(rs))
Harald Welte9b737df2011-09-07 00:59:11 +0200173 return 1;
Maxf6906002016-10-24 14:10:10 +0200174
175 LOGP(DLMIB, LOGL_INFO, "osmo_rtp_poll(%u): ERROR!\n", rs->rx_user_ts);
176 return 0;
Harald Welte9b737df2011-09-07 00:59:11 +0200177}
178
Harald Welte41d0d842011-09-03 15:33:24 +0200179/* Osmo FD callbacks */
180
181static int osmo_rtp_fd_cb(struct osmo_fd *fd, unsigned int what)
182{
183 struct osmo_rtp_socket *rs = fd->data;
Harald Welte41d0d842011-09-03 15:33:24 +0200184
185 if (what & BSC_FD_READ) {
Harald Welte9b737df2011-09-07 00:59:11 +0200186 /* in polling mode, we don't want to be called here */
187 if (rs->flags & OSMO_RTP_F_POLL) {
188 fd->when &= ~BSC_FD_READ;
189 return 0;
190 }
Maxf6906002016-10-24 14:10:10 +0200191 if (!recv_with_cb(rs))
Harald Welte9b737df2011-09-07 00:59:11 +0200192 LOGP(DLMIB, LOGL_INFO, "recvm_with_ts(%u): ERROR!\n",
193 rs->rx_user_ts);
Harald Welte41d0d842011-09-03 15:33:24 +0200194 rs->rx_user_ts += 160;
195 }
196 /* writing is not queued at the moment, so BSC_FD_WRITE
197 * shouldn't occur */
198 return 0;
199}
200
201static int osmo_rtcp_fd_cb(struct osmo_fd *fd, unsigned int what)
202{
203 struct osmo_rtp_socket *rs = fd->data;
204
205 /* We probably don't need this at all, as
206 * rtp_session_recvm_with_ts() will alway also poll the RTCP
207 * file descriptor for new data */
208 return rtp_session_rtcp_recv(rs->sess);
209}
210
211static int osmo_rtp_socket_fdreg(struct osmo_rtp_socket *rs)
212{
213 rs->rtp_bfd.fd = rtp_session_get_rtp_socket(rs->sess);
214 rs->rtcp_bfd.fd = rtp_session_get_rtcp_socket(rs->sess);
215 rs->rtp_bfd.when = rs->rtcp_bfd.when = BSC_FD_READ;
216 rs->rtp_bfd.data = rs->rtcp_bfd.data = rs;
217 rs->rtp_bfd.cb = osmo_rtp_fd_cb;
218 rs->rtcp_bfd.cb = osmo_rtcp_fd_cb;
219
220 osmo_fd_register(&rs->rtp_bfd);
221 osmo_fd_register(&rs->rtcp_bfd);
222
223 return 0;
224}
225
226static void create_payload_types()
227{
228 PayloadType *pt;
229
230 /* EFR */
231 pt = payload_type_new();
232 pt->type = PAYLOAD_AUDIO_PACKETIZED;
233 pt->clock_rate = 8000;
234 pt->mime_type = "EFR";
235 pt->normal_bitrate = 12200;
236 pt->channels = 1;
237 payload_type_efr = pt;
238
239 /* HR */
240 pt = payload_type_new();
241 pt->type = PAYLOAD_AUDIO_PACKETIZED;
242 pt->clock_rate = 8000;
243 pt->mime_type = "HR";
244 pt->normal_bitrate = 6750; /* FIXME */
245 pt->channels = 1;
246 payload_type_hr = pt;
247
248 /* create a new RTP profile as clone of AV profile */
249 osmo_pt_profile = rtp_profile_clone(&av_profile);
250
251 /* add the GSM specific payload types. They are all dynamically
252 * assigned, but in the Osmocom GSM system we have allocated
253 * them as follows: */
254 rtp_profile_set_payload(osmo_pt_profile, RTP_PT_GSM_EFR, payload_type_efr);
255 rtp_profile_set_payload(osmo_pt_profile, RTP_PT_GSM_HALF, payload_type_hr);
256 rtp_profile_set_payload(osmo_pt_profile, RTP_PT_AMR, &payload_type_amr);
257}
258
259/* public functions */
260
261/*! \brief initialize Osmocom RTP code
Harald Weltefcb1fe82011-09-07 11:51:52 +0200262 * \param[in] ctx default talloc context for library-internal allocations
Harald Welte41d0d842011-09-03 15:33:24 +0200263 */
264void osmo_rtp_init(void *ctx)
265{
266 tall_rtp_ctx = ctx;
267 ortp_set_memory_functions(&osmo_ortp_memfn);
268 ortp_init();
269 ortp_set_log_level_mask(0xffff);
270 ortp_set_log_handler(my_ortp_logfn);
271 create_payload_types();
272}
273
Harald Welte65a50892011-09-08 14:42:58 +0200274int osmo_rtp_socket_set_param(struct osmo_rtp_socket *rs,
275 enum osmo_rtp_param param, int val)
276{
277 int rc = 0;
278
279 switch (param) {
280 case OSMO_RTP_P_JITBUF:
Andreas Eversberg41bc6152013-02-14 11:03:26 +0100281 rtp_session_enable_jitter_buffer(rs->sess,
282 (val) ? TRUE : FALSE);
283 if (val)
284 rtp_session_set_jitter_compensation(rs->sess, val);
Harald Welte65a50892011-09-08 14:42:58 +0200285 break;
286#if 0
287 case OSMO_RTP_P_JIT_ADAP:
288 rc = jitter_control_enable_adaptive(rs->sess, val);
289 break;
290#endif
291 default:
292 return -EINVAL;
293 }
294
295 return rc;
296}
297
Harald Weltefcb1fe82011-09-07 11:51:52 +0200298/*! \brief Create a new RTP socket
299 * \param[in] talloc_cxt talloc context for this allocation. NULL for
300 * dafault context
301 * \param[in] flags Flags like OSMO_RTP_F_POLL
302 * \returns pointer to library-allocated \a struct osmo_rtp_socket
303 */
Harald Welte9b737df2011-09-07 00:59:11 +0200304struct osmo_rtp_socket *osmo_rtp_socket_create(void *talloc_ctx, unsigned int flags)
Harald Welte41d0d842011-09-03 15:33:24 +0200305{
306 struct osmo_rtp_socket *rs;
307
308 if (!talloc_ctx)
309 talloc_ctx = tall_rtp_ctx;
310
311 rs = talloc_zero(talloc_ctx, struct osmo_rtp_socket);
312 if (!rs)
313 return NULL;
314
Maxe54d7bc2016-04-29 12:39:33 +0200315 rs->flags = OSMO_RTP_F_DISABLED | flags;
Harald Welte41d0d842011-09-03 15:33:24 +0200316 rs->sess = rtp_session_new(RTP_SESSION_SENDRECV);
317 if (!rs->sess) {
318 talloc_free(rs);
319 return NULL;
320 }
321 rtp_session_set_data(rs->sess, rs);
322 rtp_session_set_profile(rs->sess, osmo_pt_profile);
Harald Welte9b737df2011-09-07 00:59:11 +0200323 rtp_session_set_jitter_compensation(rs->sess, 100);
324 //jitter_control_enable_adaptive(rs->sess, 0);
Harald Welte41d0d842011-09-03 15:33:24 +0200325
326 rtp_session_signal_connect(rs->sess, "ssrc_changed",
327 (RtpCallback) ortp_sig_cb_ssrc,
328 (unsigned long) rs);
329 rtp_session_signal_connect(rs->sess, "payload_type_changed",
330 (RtpCallback) ortp_sig_cb_pt,
331 (unsigned long) rs);
332 rtp_session_signal_connect(rs->sess, "network_error",
333 (RtpCallback) ortp_sig_cb_net,
334 (unsigned long) rs);
335 rtp_session_signal_connect(rs->sess, "timestamp_jump",
336 (RtpCallback) ortp_sig_cb_ts,
337 (unsigned long) rs);
338
Holger Hans Peter Freytherbf6f1f42014-06-24 10:57:35 +0200339 /* initialize according to the RFC */
340 rtp_session_set_seq_number(rs->sess, random());
Holger Hans Peter Freytherfb6e1e92014-06-24 11:02:01 +0200341 rs->tx_timestamp = random();
Holger Hans Peter Freytherbf6f1f42014-06-24 10:57:35 +0200342
343
Harald Welte41d0d842011-09-03 15:33:24 +0200344 return rs;
345}
346
Harald Weltefcb1fe82011-09-07 11:51:52 +0200347/*! \brief bind a RTP socket to a local port
348 * \param[in] rs OsmoRTP socket
349 * \param[in] ip hostname/ip as string
350 * \param[in] port UDP port number, -1 for random selection
351 * \returns 0 on success, <0 on error
352 */
Harald Welte41d0d842011-09-03 15:33:24 +0200353int osmo_rtp_socket_bind(struct osmo_rtp_socket *rs, const char *ip, int port)
354{
Max15d9b792016-04-28 12:05:27 +0200355 int rc, rtcp = (-1 != port) ? port + 1 : -1;
Max80f7c042016-04-29 12:39:34 +0200356 rc = rtp_session_set_local_addr(rs->sess, ip, port, rtcp);
Max15d9b792016-04-28 12:05:27 +0200357
Harald Welte41d0d842011-09-03 15:33:24 +0200358 if (rc < 0)
359 return rc;
360
361 rs->rtp_bfd.fd = rtp_session_get_rtp_socket(rs->sess);
362 rs->rtcp_bfd.fd = rtp_session_get_rtcp_socket(rs->sess);
363
364 return 0;
365}
366
Harald Weltefcb1fe82011-09-07 11:51:52 +0200367/*! \brief connect a OsmoRTP socket to a remote port
368 * \param[in] rs OsmoRTP socket
369 * \param[in] ip String representation of remote hostname or IP address
370 * \param[in] port UDP port number to connect to
371 *
372 * If the OsmoRTP socket is not in POLL mode, this function will also
373 * cause the RTP and RTCP file descriptors to be registred with the
374 * libosmocore select() loop integration.
375 *
376 * \returns 0 on success, <0 in case of error
377 */
Harald Welte41d0d842011-09-03 15:33:24 +0200378int osmo_rtp_socket_connect(struct osmo_rtp_socket *rs, const char *ip, uint16_t port)
379{
380 int rc;
Maxe54d7bc2016-04-29 12:39:33 +0200381 if (!port) {
382 LOGP(DLMIB, LOGL_INFO, "osmo_rtp_socket_connect() refused to "
383 "set remote %s:%u\n", ip, port);
384 return 0;
385 }
Max8c119f72016-04-29 12:39:35 +0200386
Neels Hofmeyra0ff9422016-10-06 15:43:38 +0200387 /* We don't want the connected mode enabled during
388 * rtp_session_set_remote_addr(), because that will already setup a
389 * connection and updating the remote address will no longer have an
390 * effect. Contrary to what one may expect, this must be 0 at first,
391 * and we're setting to 1 further down to establish a connection once
392 * the first RTP packet is received (OS#1661). */
393 rtp_session_set_connected_mode(rs->sess, 0);
394
395 rc = rtp_session_set_remote_addr(rs->sess, ip, port);
396 if (rc < 0)
397 return rc;
398
Harald Welted426d452013-02-09 11:01:19 +0100399 /* enable the use of connect() so later getsockname() will
400 * actually return the IP address that was chosen for the local
401 * sid of the connection */
402 rtp_session_set_connected_mode(rs->sess, 1);
Maxe54d7bc2016-04-29 12:39:33 +0200403 rs->flags &= ~OSMO_RTP_F_DISABLED;
Harald Welted426d452013-02-09 11:01:19 +0100404
Harald Welte9b737df2011-09-07 00:59:11 +0200405 if (rs->flags & OSMO_RTP_F_POLL)
406 return rc;
407 else
408 return osmo_rtp_socket_fdreg(rs);
Harald Welte41d0d842011-09-03 15:33:24 +0200409}
410
Harald Weltefcb1fe82011-09-07 11:51:52 +0200411/*! \brief Send one RTP frame via a RTP socket
412 * \param[in] rs OsmoRTP socket
413 * \param[in] payload pointer to buffer with RTP payload data
414 * \param[in] payload_len length of \a payload in bytes
415 * \param[in] duration duration in number of RTP clock ticks
416 * \returns 0 on success, <0 in case of error.
417 */
Harald Welte41d0d842011-09-03 15:33:24 +0200418int osmo_rtp_send_frame(struct osmo_rtp_socket *rs, const uint8_t *payload,
419 unsigned int payload_len, unsigned int duration)
420{
Max73b9bc72016-05-18 15:52:37 +0200421 return osmo_rtp_send_frame_ext(rs, payload, payload_len, duration,
422 false);
423}
424
425/*! \brief Send one RTP frame via a RTP socket
426 * \param[in] rs OsmoRTP socket
427 * \param[in] payload pointer to buffer with RTP payload data
428 * \param[in] payload_len length of \a payload in bytes
429 * \param[in] duration duration in number of RTP clock ticks
430 * \param[in] marker the status of Marker bit in RTP header
431 * \returns 0 on success, <0 in case of error.
432 */
433int osmo_rtp_send_frame_ext(struct osmo_rtp_socket *rs, const uint8_t *payload,
434 unsigned int payload_len, unsigned int duration,
435 bool marker)
436{
Harald Welte41d0d842011-09-03 15:33:24 +0200437 mblk_t *mblk;
438 int rc;
439
Maxe54d7bc2016-04-29 12:39:33 +0200440 if (rs->flags & OSMO_RTP_F_DISABLED)
441 return 0;
442
Harald Welte41d0d842011-09-03 15:33:24 +0200443 mblk = rtp_session_create_packet(rs->sess, RTP_FIXED_HEADER_SIZE,
444 payload, payload_len);
445 if (!mblk)
446 return -ENOMEM;
447
Max73b9bc72016-05-18 15:52:37 +0200448 rtp_set_markbit(mblk, marker);
Harald Welte41d0d842011-09-03 15:33:24 +0200449 rc = rtp_session_sendm_with_ts(rs->sess, mblk,
450 rs->tx_timestamp);
451 rs->tx_timestamp += duration;
452 if (rc < 0) {
453 /* no need to free() the mblk, as rtp_session_rtp_send()
454 * unconditionally free()s the mblk even in case of
455 * error */
456 return rc;
457 }
458
459 return rc;
460}
461
Harald Weltefcb1fe82011-09-07 11:51:52 +0200462/*! \brief Set the payload type of a RTP socket
463 * \param[in] rs OsmoRTP socket
464 * \param[in] payload_type RTP payload type
465 * \returns 0 on success, < 0 otherwise
466 */
Harald Welte41d0d842011-09-03 15:33:24 +0200467int osmo_rtp_socket_set_pt(struct osmo_rtp_socket *rs, int payload_type)
468{
469 int rc;
470
471 rc = rtp_session_set_payload_type(rs->sess, payload_type);
472 //rtp_session_set_rtcp_report_interval(rs->sess, 5*1000);
473
474 return rc;
475}
476
Harald Weltefcb1fe82011-09-07 11:51:52 +0200477/*! \brief completely close the RTP socket and release all resources
478 * \param[in] rs OsmoRTP socket to be released
479 * \returns 0 on success
480 */
Harald Welte41d0d842011-09-03 15:33:24 +0200481int osmo_rtp_socket_free(struct osmo_rtp_socket *rs)
482{
483 if (rs->rtp_bfd.list.next && rs->rtp_bfd.list.next != LLIST_POISON1)
484 osmo_fd_unregister(&rs->rtp_bfd);
485
486 if (rs->rtcp_bfd.list.next && rs->rtcp_bfd.list.next != LLIST_POISON1)
487 osmo_fd_unregister(&rs->rtcp_bfd);
488
489 if (rs->sess) {
490 rtp_session_release_sockets(rs->sess);
491 rtp_session_destroy(rs->sess);
492 rs->sess = NULL;
493 }
494
495 talloc_free(rs);
496
497 return 0;
498}
499
Harald Weltefcb1fe82011-09-07 11:51:52 +0200500/*! \brief obtain the locally bound IPv4 address and UDP port
501 * \param[in] rs OsmoRTP socket
502 * \param[out] ip Pointer to caller-allocated uint32_t for IPv4 address
503 * \oaram[out] port Pointer to caller-allocated int for UDP port number
504 * \returns 0 on success, <0 on error, -EIO in case of IPv6 socket
505 */
Harald Welte41d0d842011-09-03 15:33:24 +0200506int osmo_rtp_get_bound_ip_port(struct osmo_rtp_socket *rs,
507 uint32_t *ip, int *port)
508{
509 int rc;
510 struct sockaddr_storage ss;
511 struct sockaddr_in *sin = (struct sockaddr_in *) &ss;
512 socklen_t alen = sizeof(ss);
513
514 rc = getsockname(rs->rtp_bfd.fd, (struct sockaddr *)&ss, &alen);
515 if (rc < 0)
516 return rc;
517
518 if (ss.ss_family != AF_INET)
519 return -EIO;
520
521 *ip = ntohl(sin->sin_addr.s_addr);
522 *port = rtp_session_get_local_port(rs->sess);
523
524 return 0;
525}
526
Harald Weltefcb1fe82011-09-07 11:51:52 +0200527/*! \brief obtain the locally bound address and port
528 * \param[in] rs OsmoRTP socket
529 * \param[out] addr caller-allocated char ** to which the string pointer for
530 * the address is stored
531 * \param[out] port caller-allocated int * to which the port number is
532 * stored
533 * \returns 0 on success, <0 in case of error
534 */
Harald Welte41d0d842011-09-03 15:33:24 +0200535int osmo_rtp_get_bound_addr(struct osmo_rtp_socket *rs,
536 const char **addr, int *port)
537{
538 int rc;
539 struct sockaddr_storage ss;
540 socklen_t alen = sizeof(ss);
541 static char hostbuf[256];
542
543 memset(hostbuf, 0, sizeof(hostbuf));
544
545 rc = getsockname(rs->rtp_bfd.fd, (struct sockaddr *)&ss, &alen);
546 if (rc < 0)
547 return rc;
548
549 rc = getnameinfo((struct sockaddr *)&ss, alen,
550 hostbuf, sizeof(hostbuf), NULL, 0,
551 NI_NUMERICHOST);
552 if (rc < 0)
553 return rc;
554
555 *port = rtp_session_get_local_port(rs->sess);
556 *addr = hostbuf;
557
558 return 0;
559}
Harald Welte65a50892011-09-08 14:42:58 +0200560
561
562void osmo_rtp_socket_log_stats(struct osmo_rtp_socket *rs,
563 int subsys, int level,
564 const char *pfx)
565{
566 const rtp_stats_t *stats;
567
568 stats = rtp_session_get_stats(rs->sess);
569 if (!stats)
570 return;
571
572 LOGP(subsys, level, "%sRTP Tx(%"PRIu64" pkts, %"PRIu64" bytes) "
573 "Rx(%"PRIu64" pkts, %"PRIu64" bytes, %"PRIu64" late, "
574 "%"PRIu64" loss, %"PRIu64" qmax)\n",
575 pfx, stats->packet_sent, stats->sent,
576 stats->packet_recv, stats->hw_recv, stats->outoftime,
577 stats->cum_packet_loss, stats->discarded);
578}
Holger Hans Peter Freytherfe019082015-09-21 10:52:52 +0200579
580void osmo_rtp_socket_stats(struct osmo_rtp_socket *rs,
581 uint32_t *sent_packets, uint32_t *sent_octets,
582 uint32_t *recv_packets, uint32_t *recv_octets,
583 uint32_t *recv_lost, uint32_t *last_jitter)
584{
585 const rtp_stats_t *stats;
Holger Hans Peter Freytherfe019082015-09-21 10:52:52 +0200586
587 *sent_packets = *sent_octets = *recv_packets = *recv_octets = 0;
588 *recv_lost = *last_jitter = 0;
589
590 stats = rtp_session_get_stats(rs->sess);
591 if (stats) {
592 /* truncate from 64bit to 32bit here */
593 *sent_packets = stats->packet_sent;
594 *sent_octets = stats->sent;
595 *recv_packets = stats->packet_recv;
596 *recv_octets = stats->recv;
597 *recv_lost = stats->cum_packet_loss;
598 }
599
Holger Hans Peter Freyther71bc9e22015-09-21 12:18:37 +0200600 const jitter_stats_t *jitter;
601
Holger Hans Peter Freytherfe019082015-09-21 10:52:52 +0200602 jitter = rtp_session_get_jitter_stats(rs->sess);
603 if (jitter)
604 *last_jitter = jitter->jitter;
605}