blob: 578b1cb9298189a0c92a0489c178911056c519fa [file] [log] [blame]
Harald Welte41d0d842011-09-03 15:33:24 +02001#ifndef _OSMO_ORTP_H
2#define _OSMO_ORTP_H
3
4#include <stdint.h>
Max73b9bc72016-05-18 15:52:37 +02005#include <stdbool.h>
Harald Welte41d0d842011-09-03 15:33:24 +02006
7#include <osmocom/core/linuxlist.h>
8#include <osmocom/core/select.h>
9
Harald Weltefcb1fe82011-09-07 11:51:52 +020010/* we cannot include ortp/ortp.h here, as they also use 'struct msgb' */
Harald Welte41d0d842011-09-03 15:33:24 +020011struct _RtpSession;
12
Harald Welteddfaca42011-09-07 11:52:43 +020013/*! \brief default duration of a 20ms GSM codec frame */
14#define GSM_RTP_DURATION 160
15
Harald Weltefcb1fe82011-09-07 11:51:52 +020016/*! \brief standard payload type for GSM Full Rate (FR) */
Harald Welte41d0d842011-09-03 15:33:24 +020017#define RTP_PT_GSM_FULL 3
Harald Weltefcb1fe82011-09-07 11:51:52 +020018/*! \brief Osmocom pseudo-static paylaod type for Half Rate (HR) */
Harald Welte41d0d842011-09-03 15:33:24 +020019#define RTP_PT_GSM_HALF 96
Harald Weltefcb1fe82011-09-07 11:51:52 +020020/*! \brief Osmocom pseudo-static paylaod type for Enhanced Full Rate (EFR) */
Harald Welte41d0d842011-09-03 15:33:24 +020021#define RTP_PT_GSM_EFR 97
Harald Weltefcb1fe82011-09-07 11:51:52 +020022/*! \brief Osmocom pseudo-static paylaod type for Adaptive Multi Rate (AMR) */
Harald Welte41d0d842011-09-03 15:33:24 +020023#define RTP_PT_AMR 98
24
Maxc42c2ca2016-06-06 10:09:06 +020025#define GSM_VOICE_SAMPLE_RATE_HZ 8000
26#define GSM_VOICE_SAMPLES_PER_MS (GSM_VOICE_SAMPLE_RATE_HZ / 1000)
27#define GSM_VOICE_MULTIFRAME 26
28#define GSM_RTP_FRAME_DURATION_MS 20
29#define GSM_SAMPLES_PER_RTP_FRAME (GSM_RTP_FRAME_DURATION_MS * GSM_VOICE_SAMPLES_PER_MS)
30#define GSM_TDMA_FRAME_MS (120 / GSM_VOICE_MULTIFRAME)
Maxc1cf14c2016-06-07 18:40:01 +020031#define GSM_MS_TO_SAMPLES(ms) ((ms) * GSM_VOICE_SAMPLES_PER_MS)
32#define GSM_FN_TO_MS(fn) ((fn) * GSM_TDMA_FRAME_MS)
Maxc42c2ca2016-06-06 10:09:06 +020033
Harald Welte65a50892011-09-08 14:42:58 +020034/*! \brief Parameter to osmo_rtp_socket_param_set() */
35enum osmo_rtp_param {
36 OSMO_RTP_P_JITBUF = 1,
37 OSMO_RTP_P_JIT_ADAP,
38};
Harald Welteddfaca42011-09-07 11:52:43 +020039
Harald Weltefcb1fe82011-09-07 11:51:52 +020040/*! \brief Flag to indicate the socket is in polling-only mode */
Harald Welte9b737df2011-09-07 00:59:11 +020041#define OSMO_RTP_F_POLL 0x0001
Maxe54d7bc2016-04-29 12:39:33 +020042#define OSMO_RTP_F_DISABLED 0x0002
Harald Welte9b737df2011-09-07 00:59:11 +020043
Harald Weltefcb1fe82011-09-07 11:51:52 +020044/*! \brief A structure representing one RTP socket */
Harald Welte41d0d842011-09-03 15:33:24 +020045struct osmo_rtp_socket {
46 /*! \biref list header for global list of sockets */
47 struct llist_head list;
48
49 /*! \brief libortp RTP session pointer */
50 struct _RtpSession *sess;
51 /*! \brief Osmo file descriptor for RTP socket FD */
52 struct osmo_fd rtp_bfd;
53 /*! \brief Osmo file descriptor for RTCP socket FD */
54 struct osmo_fd rtcp_bfd;
55
56 /*! \brief callback for incoming data */
57 void (*rx_cb)(struct osmo_rtp_socket *rs, const uint8_t *payload,
Max02ceea82016-10-21 19:31:46 +020058 unsigned int payload_len, uint16_t seq_number,
59 uint32_t timestamp, bool marker);
Harald Welte41d0d842011-09-03 15:33:24 +020060
Harald Weltefcb1fe82011-09-07 11:51:52 +020061 /*! \brief Receive user timestamp, to be incremented by user */
Harald Welte41d0d842011-09-03 15:33:24 +020062 uint32_t rx_user_ts;
63
Harald Weltefcb1fe82011-09-07 11:51:52 +020064 /*! \brief Transmit timestamp, incremented by library */
Harald Welte41d0d842011-09-03 15:33:24 +020065 uint32_t tx_timestamp;
66
Harald Weltefcb1fe82011-09-07 11:51:52 +020067 /*! \brief Flags like OSMO_RTP_F_POLL */
Harald Welte9b737df2011-09-07 00:59:11 +020068 unsigned int flags;
69
Harald Welte41d0d842011-09-03 15:33:24 +020070 void *priv;
71};
72
73void osmo_rtp_init(void *ctx);
Harald Welte9b737df2011-09-07 00:59:11 +020074struct osmo_rtp_socket *osmo_rtp_socket_create(void *talloc_ctx, unsigned int flags);
Harald Welte41d0d842011-09-03 15:33:24 +020075int osmo_rtp_socket_bind(struct osmo_rtp_socket *rs, const char *ip, int port);
76int osmo_rtp_socket_connect(struct osmo_rtp_socket *rs, const char *ip, uint16_t port);
77int osmo_rtp_socket_set_pt(struct osmo_rtp_socket *rs, int payload_type);
78int osmo_rtp_socket_free(struct osmo_rtp_socket *rs);
Pau Espin Pedrol524923a2017-06-28 15:31:46 +020079int osmo_rtp_skipped_frame(struct osmo_rtp_socket *rs, unsigned int duration);
Harald Welte41d0d842011-09-03 15:33:24 +020080int osmo_rtp_send_frame(struct osmo_rtp_socket *rs, const uint8_t *payload,
81 unsigned int payload_len, unsigned int duration);
Max73b9bc72016-05-18 15:52:37 +020082int osmo_rtp_send_frame_ext(struct osmo_rtp_socket *rs, const uint8_t *payload,
83 unsigned int payload_len, unsigned int duration,
84 bool marker);
Harald Welte9b737df2011-09-07 00:59:11 +020085int osmo_rtp_socket_poll(struct osmo_rtp_socket *rs);
Harald Welte41d0d842011-09-03 15:33:24 +020086
87int osmo_rtp_get_bound_ip_port(struct osmo_rtp_socket *rs,
88 uint32_t *ip, int *port);
89int osmo_rtp_get_bound_addr(struct osmo_rtp_socket *rs,
90 const char **addr, int *port);
Harald Welte65a50892011-09-08 14:42:58 +020091int osmo_rtp_socket_set_param(struct osmo_rtp_socket *rs,
92 enum osmo_rtp_param param, int val);
93
94void osmo_rtp_socket_log_stats(struct osmo_rtp_socket *rs,
95 int subsys, int level,
96 const char *pfx);
Harald Welte41d0d842011-09-03 15:33:24 +020097
Holger Hans Peter Freytherfe019082015-09-21 10:52:52 +020098void osmo_rtp_socket_stats(struct osmo_rtp_socket *rs,
99 uint32_t *sent_packets, uint32_t *sent_octets,
100 uint32_t *recv_packets, uint32_t *recv_octets,
101 uint32_t *recv_lost, uint32_t *last_jitter);
102
103
Harald Welte41d0d842011-09-03 15:33:24 +0200104#endif /* _OSMO_ORTP_H */