Asterisk - The Open Source Telephony Project GIT-master-a358458
Data Structures | Macros | Enumerations | Functions | Variables
res_rtp_asterisk.c File Reference

Supports RTP and RTCP with Symmetric RTP support for NAT traversal. More...

#include "asterisk.h"
#include <arpa/nameser.h>
#include "asterisk/dns_core.h"
#include "asterisk/dns_internal.h"
#include "asterisk/dns_recurring.h"
#include <sys/time.h>
#include <signal.h>
#include <fcntl.h>
#include <math.h>
#include "asterisk/conversions.h"
#include "asterisk/options.h"
#include "asterisk/logger_category.h"
#include "asterisk/stun.h"
#include "asterisk/pbx.h"
#include "asterisk/frame.h"
#include "asterisk/format_cache.h"
#include "asterisk/channel.h"
#include "asterisk/acl.h"
#include "asterisk/config.h"
#include "asterisk/lock.h"
#include "asterisk/utils.h"
#include "asterisk/cli.h"
#include "asterisk/manager.h"
#include "asterisk/unaligned.h"
#include "asterisk/module.h"
#include "asterisk/rtp_engine.h"
#include "asterisk/smoother.h"
#include "asterisk/uuid.h"
#include "asterisk/test.h"
#include "asterisk/data_buffer.h"
Include dependency graph for res_rtp_asterisk.c:

Go to the source code of this file.

Data Structures

struct  ast_rtcp
 Structure defining an RTCP session. More...
 
struct  ast_rtp
 RTP session description. More...
 
struct  ast_rtp_rtcp_nack_payload
 Structure for storing RTP packets for retransmission. More...
 
struct  frame_list
 
struct  optional_ts
 
struct  rtp_learning_info
 RTP learning mode tracking information. More...
 
struct  rtp_red
 
struct  rtp_ssrc_mapping
 Structure used for mapping an incoming SSRC to an RTP instance. More...
 
struct  rtp_transport_wide_cc_packet_statistics
 Packet statistics (used for transport-cc) More...
 
struct  rtp_transport_wide_cc_statistics
 Statistics information (used for transport-cc) More...
 

Macros

#define CALC_LEARNING_MIN_DURATION(count)   (((count) - 1) * 9 - 5)
 Calculate the min learning duration in ms. More...
 
#define DEFAULT_DTLS_MTU   1200
 
#define DEFAULT_DTMF_TIMEOUT   (150 * (8000 / 1000))
 
#define DEFAULT_ICESUPPORT   1
 
#define DEFAULT_LEARNING_MIN_DURATION   CALC_LEARNING_MIN_DURATION(DEFAULT_LEARNING_MIN_SEQUENTIAL)
 
#define DEFAULT_LEARNING_MIN_SEQUENTIAL   4
 
#define DEFAULT_RTP_END   31000
 
#define DEFAULT_RTP_RECV_BUFFER_SIZE   20
 
#define DEFAULT_RTP_SEND_BUFFER_SIZE   250
 
#define DEFAULT_RTP_START   5000
 
#define DEFAULT_SRTP_REPLAY_PROTECTION   1
 
#define DEFAULT_STRICT_RTP   STRICT_RTP_YES
 
#define DEFAULT_STUN_SOFTWARE_ATTRIBUTE   1
 
#define DEFAULT_TURN_PORT   3478
 
#define DTMF_SAMPLE_RATE_MS   8
 
#define FLAG_3389_WARNING   (1 << 0)
 
#define FLAG_DTMF_COMPENSATE   (1 << 4)
 
#define FLAG_NAT_ACTIVE   (3 << 1)
 
#define FLAG_NAT_INACTIVE   (0 << 1)
 
#define FLAG_NAT_INACTIVE_NOWARN   (1 << 1)
 
#define FLAG_NEED_MARKER_BIT   (1 << 3)
 
#define FLAG_REQ_LOCAL_BRIDGE_BIT   (1 << 5)
 
#define MAX_TIMESTAMP_SKEW   640
 
#define MAXIMUM_RTP_PORT   65535
 
#define MAXIMUM_RTP_RECV_BUFFER_SIZE   (DEFAULT_RTP_RECV_BUFFER_SIZE + 20)
 
#define MAXIMUM_RTP_SEND_BUFFER_SIZE   (DEFAULT_RTP_SEND_BUFFER_SIZE + 200)
 
#define MINIMUM_RTP_PORT   1024
 
#define MISSING_SEQNOS_ADDED_TRIGGER   2
 
#define OLD_PACKET_COUNT   1000
 
#define RESCALE(in, inmin, inmax, outmin, outmax)   ((((in - inmin)/(inmax-inmin))*(outmax-outmin))+outmin)
 
#define RTCP_DEFAULT_INTERVALMS   5000
 
#define RTCP_FB_NACK_BLOCK_WORD_LENGTH   2
 
#define RTCP_FB_REMB_BLOCK_WORD_LENGTH   4
 
#define RTCP_HEADER_SSRC_LENGTH   2
 
#define RTCP_LENGTH_MASK   0xFFFF
 
#define RTCP_LENGTH_SHIFT   0
 
#define RTCP_MAX_INTERVALMS   60000
 
#define RTCP_MIN_INTERVALMS   500
 
#define RTCP_PADDING_MASK   0x01
 
#define RTCP_PADDING_SHIFT   29
 
#define RTCP_PAYLOAD_TYPE_MASK   0xFF
 
#define RTCP_PAYLOAD_TYPE_SHIFT   16
 
#define RTCP_PT_APP   204
 
#define RTCP_PT_BYE   203
 
#define RTCP_PT_FUR   192
 
#define RTCP_PT_PSFB   AST_RTP_RTCP_PSFB
 
#define RTCP_PT_RR   AST_RTP_RTCP_RR
 
#define RTCP_PT_SDES   202
 
#define RTCP_PT_SR   AST_RTP_RTCP_SR
 
#define RTCP_REPORT_COUNT_MASK   0x1F
 
#define RTCP_REPORT_COUNT_SHIFT   24
 
#define RTCP_RR_BLOCK_WORD_LENGTH   6
 
#define RTCP_SR_BLOCK_WORD_LENGTH   5
 
#define RTCP_VALID_MASK   (RTCP_VERSION_MASK_SHIFTED | (((RTCP_PAYLOAD_TYPE_MASK & ~0x1)) << RTCP_PAYLOAD_TYPE_SHIFT))
 
#define RTCP_VALID_VALUE   (RTCP_VERSION_SHIFTED | (RTCP_PT_SR << RTCP_PAYLOAD_TYPE_SHIFT))
 
#define RTCP_VERSION   2U
 
#define RTCP_VERSION_MASK   0x03
 
#define RTCP_VERSION_MASK_SHIFTED   (RTCP_VERSION_MASK << RTCP_VERSION_SHIFT)
 
#define RTCP_VERSION_SHIFT   30
 
#define RTCP_VERSION_SHIFTED   (RTCP_VERSION << RTCP_VERSION_SHIFT)
 
#define RTP_DTLS_ESTABLISHED   -37
 
#define RTP_IGNORE_FIRST_PACKETS_COUNT   15
 
#define RTP_MTU   1200
 
#define RTP_SEQ_MOD   (1<<16)
 
#define SEQNO_CYCLE_OVER   65536
 
#define SRTP_MASTER_KEY_LEN   16
 
#define SRTP_MASTER_LEN   (SRTP_MASTER_KEY_LEN + SRTP_MASTER_SALT_LEN)
 
#define SRTP_MASTER_SALT_LEN   14
 
#define SSRC_MAPPING_ELEM_CMP(elem, value)   ((elem).instance == (value))
 SSRC mapping comparator for AST_VECTOR_REMOVE_CMP_UNORDERED() More...
 
#define STRICT_RTP_LEARN_TIMEOUT   5000
 Strict RTP learning timeout time in milliseconds. More...
 
#define TRANSPORT_SOCKET_RTCP   1
 
#define TRANSPORT_SOCKET_RTP   0
 
#define TRANSPORT_TURN_RTCP   3
 
#define TRANSPORT_TURN_RTP   2
 
#define TURN_STATE_WAIT_TIME   2000
 
#define ZFONE_PROFILE_ID   0x505a
 

Enumerations

enum  strict_rtp_mode { STRICT_RTP_NO = 0 , STRICT_RTP_YES , STRICT_RTP_SEQNO }
 
enum  strict_rtp_state { STRICT_RTP_OPEN = 0 , STRICT_RTP_LEARN , STRICT_RTP_CLOSED }
 

Functions

static void __reg_module (void)
 
static struct ast_rtp_instance__rtp_find_instance_by_ssrc (struct ast_rtp_instance *instance, struct ast_rtp *rtp, unsigned int ssrc, int source)
 
static int __rtp_recvfrom (struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int rtcp)
 
static int __rtp_sendto (struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int rtcp, int *via_ice, int use_srtp)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static unsigned int ast_rtcp_calc_interval (struct ast_rtp *rtp)
 
static int ast_rtcp_calculate_sr_rr_statistics (struct ast_rtp_instance *instance, struct ast_rtp_rtcp_report *rtcp_report, struct ast_sockaddr remote_address, int ice, int sr)
 
static int ast_rtcp_generate_compound_prefix (struct ast_rtp_instance *instance, unsigned char *rtcpheader, struct ast_rtp_rtcp_report *report, int *sr)
 
static int ast_rtcp_generate_nack (struct ast_rtp_instance *instance, unsigned char *rtcpheader)
 
static int ast_rtcp_generate_report (struct ast_rtp_instance *instance, unsigned char *rtcpheader, struct ast_rtp_rtcp_report *rtcp_report, int *sr)
 
static int ast_rtcp_generate_sdes (struct ast_rtp_instance *instance, unsigned char *rtcpheader, struct ast_rtp_rtcp_report *rtcp_report)
 
static struct ast_frameast_rtcp_interpret (struct ast_rtp_instance *instance, struct ast_srtp *srtp, const unsigned char *rtcpdata, size_t size, struct ast_sockaddr *addr)
 
static struct ast_frameast_rtcp_read (struct ast_rtp_instance *instance)
 
static int ast_rtcp_write (const void *data)
 Write a RTCP packet to the far end. More...
 
static int ast_rtp_bundle (struct ast_rtp_instance *child, struct ast_rtp_instance *parent)
 
static void ast_rtp_change_source (struct ast_rtp_instance *instance)
 
static int ast_rtp_destroy (struct ast_rtp_instance *instance)
 
static int ast_rtp_dtmf_begin (struct ast_rtp_instance *instance, char digit)
 
static int ast_rtp_dtmf_compatible (struct ast_channel *chan0, struct ast_rtp_instance *instance0, struct ast_channel *chan1, struct ast_rtp_instance *instance1)
 
static int ast_rtp_dtmf_continuation (struct ast_rtp_instance *instance)
 
static int ast_rtp_dtmf_end (struct ast_rtp_instance *instance, char digit)
 
static int ast_rtp_dtmf_end_with_duration (struct ast_rtp_instance *instance, char digit, unsigned int duration)
 
static enum ast_rtp_dtmf_mode ast_rtp_dtmf_mode_get (struct ast_rtp_instance *instance)
 
static int ast_rtp_dtmf_mode_set (struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
 
static int ast_rtp_extension_enable (struct ast_rtp_instance *instance, enum ast_rtp_extension extension)
 
static int ast_rtp_fd (struct ast_rtp_instance *instance, int rtcp)
 
static const char * ast_rtp_get_cname (struct ast_rtp_instance *instance)
 
static unsigned int ast_rtp_get_ssrc (struct ast_rtp_instance *instance)
 
static int ast_rtp_get_stat (struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
 
static struct ast_frameast_rtp_interpret (struct ast_rtp_instance *instance, struct ast_srtp *srtp, const struct ast_sockaddr *remote_address, unsigned char *read_area, int length, int prev_seqno, unsigned int bundled)
 
static int ast_rtp_local_bridge (struct ast_rtp_instance *instance0, struct ast_rtp_instance *instance1)
 
static int ast_rtp_new (struct ast_rtp_instance *instance, struct ast_sched_context *sched, struct ast_sockaddr *addr, void *data)
 
static void ast_rtp_prop_set (struct ast_rtp_instance *instance, enum ast_rtp_property property, int value)
 
static int ast_rtp_qos_set (struct ast_rtp_instance *instance, int tos, int cos, const char *desc)
 
static struct ast_frameast_rtp_read (struct ast_rtp_instance *instance, int rtcp)
 
static void ast_rtp_remote_address_set (struct ast_rtp_instance *instance, struct ast_sockaddr *addr)
 
static int ast_rtp_rtcp_handle_nack (struct ast_rtp_instance *instance, unsigned int *nackdata, unsigned int position, unsigned int length)
 
static int ast_rtp_sendcng (struct ast_rtp_instance *instance, int level)
 generate comfort noice (CNG) More...
 
static void ast_rtp_set_remote_ssrc (struct ast_rtp_instance *instance, unsigned int ssrc)
 
static void ast_rtp_set_stream_num (struct ast_rtp_instance *instance, int stream_num)
 
static void ast_rtp_stop (struct ast_rtp_instance *instance)
 
static void ast_rtp_stun_request (struct ast_rtp_instance *instance, struct ast_sockaddr *suggestion, const char *username)
 
static void ast_rtp_update_source (struct ast_rtp_instance *instance)
 
static int ast_rtp_write (struct ast_rtp_instance *instance, struct ast_frame *frame)
 
static int bridge_p2p_rtp_write (struct ast_rtp_instance *instance, struct ast_rtp_instance *instance1, unsigned int *rtpheader, int len, int hdrlen)
 
static void calc_mean_and_standard_deviation (double new_sample, double *mean, double *std_dev, unsigned int *count)
 
static double calc_media_experience_score (struct ast_rtp_instance *instance, double normdevrtt, double normdev_rxjitter, double stdev_rxjitter, double normdev_rxlost)
 Calculate a "media experience score" based on given data. More...
 
static void calc_rxstamp_and_jitter (struct timeval *tv, struct ast_rtp *rtp, unsigned int rx_rtp_ts, int mark)
 
static unsigned int calc_txstamp (struct ast_rtp *rtp, struct timeval *delivery)
 
static void calculate_lost_packet_statistics (struct ast_rtp *rtp, unsigned int *lost_packets, int *fraction_lost)
 
static int compare_by_value (int elem, int value)
 Helper function to compare an elem in a vector by value. More...
 
static struct ast_framecreate_dtmf_frame (struct ast_rtp_instance *instance, enum ast_frame_type type, int compensate)
 
static int create_new_socket (const char *type, int af)
 
static int find_by_value (int elem, int value)
 Helper function to find an elem in a vector by value. More...
 
static char * handle_cli_rtcp_set_debug (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_rtcp_set_stats (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_rtp_set_debug (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_rtp_settings (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static int load_module (void)
 
static void ntp2timeval (unsigned int msw, unsigned int lsw, struct timeval *tv)
 
static struct ast_frameprocess_cn_rfc3389 (struct ast_rtp_instance *instance, unsigned char *data, int len, unsigned int seqno, unsigned int timestamp, int payloadtype, int mark)
 
static struct ast_frameprocess_dtmf_cisco (struct ast_rtp_instance *instance, unsigned char *data, int len, unsigned int seqno, unsigned int timestamp, int payloadtype, int mark)
 
static void process_dtmf_rfc2833 (struct ast_rtp_instance *instance, unsigned char *data, int len, unsigned int seqno, unsigned int timestamp, int payloadtype, int mark, struct frame_list *frames)
 
static void put_unaligned_time24 (void *p, uint32_t time_msw, uint32_t time_lsw)
 
static struct ast_framered_t140_to_red (struct rtp_red *red)
 
static int red_write (const void *data)
 Write t140 redundancy frame. More...
 
static int reload_module (void)
 
static int rtcp_debug_test_addr (struct ast_sockaddr *addr)
 
static char * rtcp_do_debug_ip (struct ast_cli_args *a)
 
static int rtcp_mux (struct ast_rtp *rtp, const unsigned char *packet)
 
static const char * rtcp_payload_subtype2str (unsigned int pt, unsigned int subtype)
 
static const char * rtcp_payload_type2str (unsigned int pt)
 
static int rtcp_recvfrom (struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa)
 
static int rtcp_sendto (struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int *ice)
 
static int rtp_allocate_transport (struct ast_rtp_instance *instance, struct ast_rtp *rtp)
 
static void rtp_deallocate_transport (struct ast_rtp_instance *instance, struct ast_rtp *rtp)
 
static int rtp_debug_test_addr (struct ast_sockaddr *addr)
 
static char * rtp_do_debug_ip (struct ast_cli_args *a)
 
static struct ast_rtp_instancertp_find_instance_by_media_source_ssrc (struct ast_rtp_instance *instance, struct ast_rtp *rtp, unsigned int ssrc)
 
static struct ast_rtp_instancertp_find_instance_by_packet_source_ssrc (struct ast_rtp_instance *instance, struct ast_rtp *rtp, unsigned int ssrc)
 
static void rtp_instance_parse_extmap_extensions (struct ast_rtp_instance *instance, struct ast_rtp *rtp, unsigned char *extension, int len)
 
static void rtp_instance_parse_transport_wide_cc (struct ast_rtp_instance *instance, struct ast_rtp *rtp, unsigned char *data, int len)
 
static void rtp_instance_unlock (struct ast_rtp_instance *instance)
 
static int rtp_learning_rtp_seq_update (struct rtp_learning_info *info, uint16_t seq)
 
static void rtp_learning_seq_init (struct rtp_learning_info *info, uint16_t seq)
 
static void rtp_learning_start (struct ast_rtp *rtp)
 Start the strictrtp learning mode. More...
 
static int rtp_raw_write (struct ast_rtp_instance *instance, struct ast_frame *frame, int codec)
 
static int rtp_recvfrom (struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa)
 
static int rtp_red_buffer (struct ast_rtp_instance *instance, struct ast_frame *frame)
 
static int rtp_red_init (struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
 
static int rtp_reload (int reload, int by_external_config)
 
static int rtp_sendto (struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int *ice)
 
static int rtp_transport_wide_cc_feedback_produce (const void *data)
 
static void rtp_transport_wide_cc_feedback_status_append (unsigned char *rtcpheader, int *packet_len, int *status_vector_chunk_bits, uint16_t *status_vector_chunk, int *run_length_chunk_count, int *run_length_chunk_status, int status)
 
static void rtp_transport_wide_cc_feedback_status_vector_append (unsigned char *rtcpheader, int *packet_len, int *status_vector_chunk_bits, uint16_t *status_vector_chunk, int status)
 
static int rtp_transport_wide_cc_packet_statistics_cmp (struct rtp_transport_wide_cc_packet_statistics a, struct rtp_transport_wide_cc_packet_statistics b)
 
static void rtp_write_rtcp_fir (struct ast_rtp_instance *instance, struct ast_rtp *rtp, struct ast_sockaddr *remote_address)
 
static void rtp_write_rtcp_psfb (struct ast_rtp_instance *instance, struct ast_rtp *rtp, struct ast_frame *frame, struct ast_sockaddr *remote_address)
 
static void timeval2ntp (struct timeval tv, unsigned int *msw, unsigned int *lsw)
 
static int unload_module (void)
 
static void update_jitter_stats (struct ast_rtp *rtp, unsigned int ia_jitter)
 
static void update_local_mes_stats (struct ast_rtp *rtp)
 
static void update_lost_stats (struct ast_rtp *rtp, unsigned int lost_packets)
 
static void update_reported_mes_stats (struct ast_rtp *rtp)
 
static int update_rtt_stats (struct ast_rtp *rtp, unsigned int lsr, unsigned int dlsr)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Asterisk RTP Stack" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .reload = reload_module, .load_pri = AST_MODPRI_CHANNEL_DEPEND, #ifdef HAVE_PJPROJECT .requires = "res_pjproject", #endif }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_rtp_engine asterisk_rtp_engine
 
static struct ast_cli_entry cli_rtp []
 
static int dtmftimeout = DEFAULT_DTMF_TIMEOUT
 
static int learning_min_duration = DEFAULT_LEARNING_MIN_DURATION
 
static int learning_min_sequential = DEFAULT_LEARNING_MIN_SEQUENTIAL
 
struct ast_srtp_resres_srtp
 
struct ast_srtp_policy_resres_srtp_policy
 
static struct ast_sockaddr rtcpdebugaddr
 
static int rtcpdebugport
 
static int rtcpinterval = RTCP_DEFAULT_INTERVALMS
 
static int rtcpstats
 
static struct ast_sockaddr rtpdebugaddr
 
static int rtpdebugport
 
static int rtpend = DEFAULT_RTP_END
 
static int rtpstart = DEFAULT_RTP_START
 
static int srtp_replay_protection = DEFAULT_SRTP_REPLAY_PROTECTION
 
static int strictrtp = DEFAULT_STRICT_RTP
 

Detailed Description

Supports RTP and RTCP with Symmetric RTP support for NAT traversal.

Author
Mark Spencer marks.nosp@m.ter@.nosp@m.digiu.nosp@m.m.co.nosp@m.m
Note
RTP is defined in RFC 3550.

Definition in file res_rtp_asterisk.c.

Macro Definition Documentation

◆ CALC_LEARNING_MIN_DURATION

#define CALC_LEARNING_MIN_DURATION (   count)    (((count) - 1) * 9 - 5)

Calculate the min learning duration in ms.

The min supported packet size represents 10 ms and we need to account for some jitter and fast clocks while learning. Some messed up devices have very bad jitter for a small packet sample size. Jitter can also be introduced by the network itself.

So we'll allow packets to come in every 9ms on average for fast clocking with the last one coming in 5ms early for jitter.

Definition at line 160 of file res_rtp_asterisk.c.

◆ DEFAULT_DTLS_MTU

#define DEFAULT_DTLS_MTU   1200

Definition at line 194 of file res_rtp_asterisk.c.

◆ DEFAULT_DTMF_TIMEOUT

#define DEFAULT_DTMF_TIMEOUT   (150 * (8000 / 1000))

samples

Definition at line 143 of file res_rtp_asterisk.c.

◆ DEFAULT_ICESUPPORT

#define DEFAULT_ICESUPPORT   1

Definition at line 192 of file res_rtp_asterisk.c.

◆ DEFAULT_LEARNING_MIN_DURATION

#define DEFAULT_LEARNING_MIN_DURATION   CALC_LEARNING_MIN_DURATION(DEFAULT_LEARNING_MIN_SEQUENTIAL)

Definition at line 161 of file res_rtp_asterisk.c.

◆ DEFAULT_LEARNING_MIN_SEQUENTIAL

#define DEFAULT_LEARNING_MIN_SEQUENTIAL   4

Definition at line 147 of file res_rtp_asterisk.c.

◆ DEFAULT_RTP_END

#define DEFAULT_RTP_END   31000

Default maximum port number to end allocating RTP ports at

Definition at line 106 of file res_rtp_asterisk.c.

◆ DEFAULT_RTP_RECV_BUFFER_SIZE

#define DEFAULT_RTP_RECV_BUFFER_SIZE   20

The initial size of the RTP receiver buffer

Definition at line 117 of file res_rtp_asterisk.c.

◆ DEFAULT_RTP_SEND_BUFFER_SIZE

#define DEFAULT_RTP_SEND_BUFFER_SIZE   250

The initial size of the RTP send buffer

Definition at line 115 of file res_rtp_asterisk.c.

◆ DEFAULT_RTP_START

#define DEFAULT_RTP_START   5000

Default port number to start allocating RTP ports from

Definition at line 105 of file res_rtp_asterisk.c.

◆ DEFAULT_SRTP_REPLAY_PROTECTION

#define DEFAULT_SRTP_REPLAY_PROTECTION   1

Definition at line 191 of file res_rtp_asterisk.c.

◆ DEFAULT_STRICT_RTP

#define DEFAULT_STRICT_RTP   STRICT_RTP_YES

Enabled by default

Definition at line 190 of file res_rtp_asterisk.c.

◆ DEFAULT_STUN_SOFTWARE_ATTRIBUTE

#define DEFAULT_STUN_SOFTWARE_ATTRIBUTE   1

Definition at line 193 of file res_rtp_asterisk.c.

◆ DEFAULT_TURN_PORT

#define DEFAULT_TURN_PORT   3478

Definition at line 111 of file res_rtp_asterisk.c.

◆ DTMF_SAMPLE_RATE_MS

#define DTMF_SAMPLE_RATE_MS   8

DTMF samples per millisecond

Definition at line 141 of file res_rtp_asterisk.c.

◆ FLAG_3389_WARNING

#define FLAG_3389_WARNING   (1 << 0)

Definition at line 303 of file res_rtp_asterisk.c.

◆ FLAG_DTMF_COMPENSATE

#define FLAG_DTMF_COMPENSATE   (1 << 4)

Definition at line 308 of file res_rtp_asterisk.c.

◆ FLAG_NAT_ACTIVE

#define FLAG_NAT_ACTIVE   (3 << 1)

Definition at line 304 of file res_rtp_asterisk.c.

◆ FLAG_NAT_INACTIVE

#define FLAG_NAT_INACTIVE   (0 << 1)

Definition at line 305 of file res_rtp_asterisk.c.

◆ FLAG_NAT_INACTIVE_NOWARN

#define FLAG_NAT_INACTIVE_NOWARN   (1 << 1)

Definition at line 306 of file res_rtp_asterisk.c.

◆ FLAG_NEED_MARKER_BIT

#define FLAG_NEED_MARKER_BIT   (1 << 3)

Definition at line 307 of file res_rtp_asterisk.c.

◆ FLAG_REQ_LOCAL_BRIDGE_BIT

#define FLAG_REQ_LOCAL_BRIDGE_BIT   (1 << 5)

Definition at line 309 of file res_rtp_asterisk.c.

◆ MAX_TIMESTAMP_SKEW

#define MAX_TIMESTAMP_SKEW   640

Definition at line 98 of file res_rtp_asterisk.c.

◆ MAXIMUM_RTP_PORT

#define MAXIMUM_RTP_PORT   65535

Maximum port number to accept

Definition at line 109 of file res_rtp_asterisk.c.

◆ MAXIMUM_RTP_RECV_BUFFER_SIZE

#define MAXIMUM_RTP_RECV_BUFFER_SIZE   (DEFAULT_RTP_RECV_BUFFER_SIZE + 20)

Maximum RTP receive buffer size

Definition at line 118 of file res_rtp_asterisk.c.

◆ MAXIMUM_RTP_SEND_BUFFER_SIZE

#define MAXIMUM_RTP_SEND_BUFFER_SIZE   (DEFAULT_RTP_SEND_BUFFER_SIZE + 200)

Maximum RTP send buffer size

Definition at line 116 of file res_rtp_asterisk.c.

◆ MINIMUM_RTP_PORT

#define MINIMUM_RTP_PORT   1024

Minimum port number to accept

Definition at line 108 of file res_rtp_asterisk.c.

◆ MISSING_SEQNOS_ADDED_TRIGGER

#define MISSING_SEQNOS_ADDED_TRIGGER   2

The number of immediate missing packets that will trigger an immediate NACK

Definition at line 120 of file res_rtp_asterisk.c.

◆ OLD_PACKET_COUNT

#define OLD_PACKET_COUNT   1000

The number of previous packets that are considered old

Definition at line 119 of file res_rtp_asterisk.c.

◆ RESCALE

#define RESCALE (   in,
  inmin,
  inmax,
  outmin,
  outmax 
)    ((((in - inmin)/(inmax-inmin))*(outmax-outmin))+outmin)

Definition at line 6172 of file res_rtp_asterisk.c.

◆ RTCP_DEFAULT_INTERVALMS

#define RTCP_DEFAULT_INTERVALMS   5000

Default milli-seconds between RTCP reports we send

Definition at line 101 of file res_rtp_asterisk.c.

◆ RTCP_FB_NACK_BLOCK_WORD_LENGTH

#define RTCP_FB_NACK_BLOCK_WORD_LENGTH   2

Definition at line 6555 of file res_rtp_asterisk.c.

◆ RTCP_FB_REMB_BLOCK_WORD_LENGTH

#define RTCP_FB_REMB_BLOCK_WORD_LENGTH   4

Definition at line 6554 of file res_rtp_asterisk.c.

◆ RTCP_HEADER_SSRC_LENGTH

#define RTCP_HEADER_SSRC_LENGTH   2

Definition at line 6553 of file res_rtp_asterisk.c.

◆ RTCP_LENGTH_MASK

#define RTCP_LENGTH_MASK   0xFFFF

Definition at line 6518 of file res_rtp_asterisk.c.

◆ RTCP_LENGTH_SHIFT

#define RTCP_LENGTH_SHIFT   0

Definition at line 6527 of file res_rtp_asterisk.c.

◆ RTCP_MAX_INTERVALMS

#define RTCP_MAX_INTERVALMS   60000

Max milli-seconds between RTCP reports we send

Definition at line 103 of file res_rtp_asterisk.c.

◆ RTCP_MIN_INTERVALMS

#define RTCP_MIN_INTERVALMS   500

Min milli-seconds between RTCP reports we send

Definition at line 102 of file res_rtp_asterisk.c.

◆ RTCP_PADDING_MASK

#define RTCP_PADDING_MASK   0x01

Definition at line 6521 of file res_rtp_asterisk.c.

◆ RTCP_PADDING_SHIFT

#define RTCP_PADDING_SHIFT   29

Definition at line 6530 of file res_rtp_asterisk.c.

◆ RTCP_PAYLOAD_TYPE_MASK

#define RTCP_PAYLOAD_TYPE_MASK   0xFF

Definition at line 6519 of file res_rtp_asterisk.c.

◆ RTCP_PAYLOAD_TYPE_SHIFT

#define RTCP_PAYLOAD_TYPE_SHIFT   16

Definition at line 6528 of file res_rtp_asterisk.c.

◆ RTCP_PT_APP

#define RTCP_PT_APP   204

Application defined (From RFC3550)

Definition at line 135 of file res_rtp_asterisk.c.

◆ RTCP_PT_BYE

#define RTCP_PT_BYE   203

Goodbye (To remove SSRC's from tables) (From RFC3550)

Definition at line 133 of file res_rtp_asterisk.c.

◆ RTCP_PT_FUR

#define RTCP_PT_FUR   192

Full INTRA-frame Request / Fast Update Request (From RFC2032)

Definition at line 125 of file res_rtp_asterisk.c.

◆ RTCP_PT_PSFB

#define RTCP_PT_PSFB   AST_RTP_RTCP_PSFB

Payload Specific Feed Back (From RFC4585 also RFC5104)

Definition at line 138 of file res_rtp_asterisk.c.

◆ RTCP_PT_RR

#define RTCP_PT_RR   AST_RTP_RTCP_RR

Receiver Report (From RFC3550)

Definition at line 129 of file res_rtp_asterisk.c.

◆ RTCP_PT_SDES

#define RTCP_PT_SDES   202

Source Description (From RFC3550)

Definition at line 131 of file res_rtp_asterisk.c.

◆ RTCP_PT_SR

#define RTCP_PT_SR   AST_RTP_RTCP_SR

Sender Report (From RFC3550)

Definition at line 127 of file res_rtp_asterisk.c.

◆ RTCP_REPORT_COUNT_MASK

#define RTCP_REPORT_COUNT_MASK   0x1F

Definition at line 6520 of file res_rtp_asterisk.c.

◆ RTCP_REPORT_COUNT_SHIFT

#define RTCP_REPORT_COUNT_SHIFT   24

Definition at line 6529 of file res_rtp_asterisk.c.

◆ RTCP_RR_BLOCK_WORD_LENGTH

#define RTCP_RR_BLOCK_WORD_LENGTH   6

Definition at line 6552 of file res_rtp_asterisk.c.

◆ RTCP_SR_BLOCK_WORD_LENGTH

#define RTCP_SR_BLOCK_WORD_LENGTH   5

Definition at line 6551 of file res_rtp_asterisk.c.

◆ RTCP_VALID_MASK

#define RTCP_VALID_MASK   (RTCP_VERSION_MASK_SHIFTED | (((RTCP_PAYLOAD_TYPE_MASK & ~0x1)) << RTCP_PAYLOAD_TYPE_SHIFT))

Definition at line 6548 of file res_rtp_asterisk.c.

◆ RTCP_VALID_VALUE

#define RTCP_VALID_VALUE   (RTCP_VERSION_SHIFTED | (RTCP_PT_SR << RTCP_PAYLOAD_TYPE_SHIFT))

Definition at line 6549 of file res_rtp_asterisk.c.

◆ RTCP_VERSION

#define RTCP_VERSION   2U

Definition at line 6533 of file res_rtp_asterisk.c.

◆ RTCP_VERSION_MASK

#define RTCP_VERSION_MASK   0x03

Definition at line 6522 of file res_rtp_asterisk.c.

◆ RTCP_VERSION_MASK_SHIFTED

#define RTCP_VERSION_MASK_SHIFTED   (RTCP_VERSION_MASK << RTCP_VERSION_SHIFT)

Definition at line 6535 of file res_rtp_asterisk.c.

◆ RTCP_VERSION_SHIFT

#define RTCP_VERSION_SHIFT   30

Definition at line 6531 of file res_rtp_asterisk.c.

◆ RTCP_VERSION_SHIFTED

#define RTCP_VERSION_SHIFTED   (RTCP_VERSION << RTCP_VERSION_SHIFT)

Definition at line 6534 of file res_rtp_asterisk.c.

◆ RTP_DTLS_ESTABLISHED

#define RTP_DTLS_ESTABLISHED   -37

Definition at line 167 of file res_rtp_asterisk.c.

◆ RTP_IGNORE_FIRST_PACKETS_COUNT

#define RTP_IGNORE_FIRST_PACKETS_COUNT   15

Because both ends usually don't start sending RTP at the same time, some of the calculations like rtt and jitter will probably be unstable for a while so we'll skip some received packets before starting analyzing. This just affects analyzing; we still process the RTP as normal.

Definition at line 204 of file res_rtp_asterisk.c.

◆ RTP_MTU

#define RTP_MTU   1200

Definition at line 140 of file res_rtp_asterisk.c.

◆ RTP_SEQ_MOD

#define RTP_SEQ_MOD   (1<<16)

A sequence number can't be more than 16 bits

Definition at line 100 of file res_rtp_asterisk.c.

◆ SEQNO_CYCLE_OVER

#define SEQNO_CYCLE_OVER   65536

The number after the maximum allowed sequence number

Definition at line 122 of file res_rtp_asterisk.c.

◆ SRTP_MASTER_KEY_LEN

#define SRTP_MASTER_KEY_LEN   16

Definition at line 163 of file res_rtp_asterisk.c.

◆ SRTP_MASTER_LEN

#define SRTP_MASTER_LEN   (SRTP_MASTER_KEY_LEN + SRTP_MASTER_SALT_LEN)

Definition at line 165 of file res_rtp_asterisk.c.

◆ SRTP_MASTER_SALT_LEN

#define SRTP_MASTER_SALT_LEN   14

Definition at line 164 of file res_rtp_asterisk.c.

◆ SSRC_MAPPING_ELEM_CMP

#define SSRC_MAPPING_ELEM_CMP (   elem,
  value 
)    ((elem).instance == (value))

SSRC mapping comparator for AST_VECTOR_REMOVE_CMP_UNORDERED()

Parameters
elemElement to compare against
valueValue to compare with the vector element.
Return values
0if element does not match.
Non-zeroif element matches.

Definition at line 4192 of file res_rtp_asterisk.c.

◆ STRICT_RTP_LEARN_TIMEOUT

#define STRICT_RTP_LEARN_TIMEOUT   5000

Strict RTP learning timeout time in milliseconds.

Note
Set to 5 seconds to allow reinvite chains for direct media to settle before media actually starts to arrive. There may be a reinvite collision involved on the other leg.

Definition at line 188 of file res_rtp_asterisk.c.

◆ TRANSPORT_SOCKET_RTCP

#define TRANSPORT_SOCKET_RTCP   1

Definition at line 312 of file res_rtp_asterisk.c.

◆ TRANSPORT_SOCKET_RTP

#define TRANSPORT_SOCKET_RTP   0

Definition at line 311 of file res_rtp_asterisk.c.

◆ TRANSPORT_TURN_RTCP

#define TRANSPORT_TURN_RTCP   3

Definition at line 314 of file res_rtp_asterisk.c.

◆ TRANSPORT_TURN_RTP

#define TRANSPORT_TURN_RTP   2

Definition at line 313 of file res_rtp_asterisk.c.

◆ TURN_STATE_WAIT_TIME

#define TURN_STATE_WAIT_TIME   2000

Definition at line 113 of file res_rtp_asterisk.c.

◆ ZFONE_PROFILE_ID

#define ZFONE_PROFILE_ID   0x505a

Definition at line 145 of file res_rtp_asterisk.c.

Enumeration Type Documentation

◆ strict_rtp_mode

Enumerator
STRICT_RTP_NO 
STRICT_RTP_YES 

Don't adhere to any strict RTP rules

STRICT_RTP_SEQNO 

Strict RTP that restricts packets based on time and sequence number

Definition at line 175 of file res_rtp_asterisk.c.

175 {
176 STRICT_RTP_NO = 0, /*! Don't adhere to any strict RTP rules */
177 STRICT_RTP_YES, /*! Strict RTP that restricts packets based on time and sequence number */
178 STRICT_RTP_SEQNO, /*! Strict RTP that restricts packets based on sequence number */
179};
@ STRICT_RTP_SEQNO
@ STRICT_RTP_YES
@ STRICT_RTP_NO

◆ strict_rtp_state

Enumerator
STRICT_RTP_OPEN 
STRICT_RTP_LEARN 

No RTP packets should be dropped, all sources accepted

STRICT_RTP_CLOSED 

Accept next packet as source

Definition at line 169 of file res_rtp_asterisk.c.

169 {
170 STRICT_RTP_OPEN = 0, /*! No RTP packets should be dropped, all sources accepted */
171 STRICT_RTP_LEARN, /*! Accept next packet as source */
172 STRICT_RTP_CLOSED, /*! Drop all RTP packets not coming from source that was learned */
173};
@ STRICT_RTP_LEARN
@ STRICT_RTP_OPEN
@ STRICT_RTP_CLOSED

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 10346 of file res_rtp_asterisk.c.

◆ __rtp_find_instance_by_ssrc()

static struct ast_rtp_instance * __rtp_find_instance_by_ssrc ( struct ast_rtp_instance instance,
struct ast_rtp rtp,
unsigned int  ssrc,
int  source 
)
static
Precondition
instance is locked

Definition at line 6326 of file res_rtp_asterisk.c.

6328{
6329 int index;
6330
6331 if (!AST_VECTOR_SIZE(&rtp->ssrc_mapping)) {
6332 /* This instance is not bundled */
6333 return instance;
6334 }
6335
6336 /* Find the bundled child instance */
6337 for (index = 0; index < AST_VECTOR_SIZE(&rtp->ssrc_mapping); ++index) {
6338 struct rtp_ssrc_mapping *mapping = AST_VECTOR_GET_ADDR(&rtp->ssrc_mapping, index);
6339 unsigned int mapping_ssrc = source ? ast_rtp_get_ssrc(mapping->instance) : mapping->ssrc;
6340
6341 if (mapping->ssrc_valid && mapping_ssrc == ssrc) {
6342 return mapping->instance;
6343 }
6344 }
6345
6346 /* Does the SSRC match the bundled parent? */
6347 if (rtp->themssrc_valid && rtp->themssrc == ssrc) {
6348 return instance;
6349 }
6350 return NULL;
6351}
static unsigned int ast_rtp_get_ssrc(struct ast_rtp_instance *instance)
#define NULL
Definition: resample.c:96
unsigned int themssrc_valid
struct ast_rtp::@472 ssrc_mapping
unsigned int themssrc
Structure used for mapping an incoming SSRC to an RTP instance.
unsigned int ssrc
The received SSRC.
unsigned int ssrc_valid
struct ast_rtp_instance * instance
The RTP instance this SSRC belongs to.
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:609
#define AST_VECTOR_GET_ADDR(vec, idx)
Get an address of element in a vector.
Definition: vector.h:668

References ast_rtp_get_ssrc(), AST_VECTOR_GET_ADDR, AST_VECTOR_SIZE, rtp_ssrc_mapping::instance, NULL, rtp_ssrc_mapping::ssrc, ast_rtp::ssrc_mapping, rtp_ssrc_mapping::ssrc_valid, ast_rtp::themssrc, and ast_rtp::themssrc_valid.

Referenced by rtp_find_instance_by_media_source_ssrc(), and rtp_find_instance_by_packet_source_ssrc().

◆ __rtp_recvfrom()

static int __rtp_recvfrom ( struct ast_rtp_instance instance,
void *  buf,
size_t  size,
int  flags,
struct ast_sockaddr sa,
int  rtcp 
)
static
Precondition
instance is locked

Definition at line 3169 of file res_rtp_asterisk.c.

3170{
3171 int len;
3172 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
3173#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
3174 char *in = buf;
3175#endif
3176#ifdef HAVE_PJPROJECT
3177 struct ast_sockaddr *loop = rtcp ? &rtp->rtcp_loop : &rtp->rtp_loop;
3178#endif
3179#ifdef TEST_FRAMEWORK
3180 struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
3181#endif
3182
3183 if ((len = ast_recvfrom(rtcp ? rtp->rtcp->s : rtp->s, buf, size, flags, sa)) < 0) {
3184 return len;
3185 }
3186
3187#ifdef TEST_FRAMEWORK
3188 if (test && test->packets_to_drop > 0) {
3189 test->packets_to_drop--;
3190 return 0;
3191 }
3192#endif
3193
3194#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
3195 /* If this is an SSL packet pass it to OpenSSL for processing. RFC section for first byte value:
3196 * https://tools.ietf.org/html/rfc5764#section-5.1.2 */
3197 if ((*in >= 20) && (*in <= 63)) {
3198 struct dtls_details *dtls = !rtcp ? &rtp->dtls : &rtp->rtcp->dtls;
3199 int res = 0;
3200
3201 /* If no SSL session actually exists terminate things */
3202 if (!dtls->ssl) {
3203 ast_log(LOG_ERROR, "Received SSL traffic on RTP instance '%p' without an SSL session\n",
3204 instance);
3205 return -1;
3206 }
3207
3208 ast_debug_dtls(3, "(%p) DTLS - __rtp_recvfrom rtp=%p - Got SSL packet '%d'\n", instance, rtp, *in);
3209
3210 /*
3211 * If ICE is in use, we can prevent a possible DOS attack
3212 * by allowing DTLS protocol messages (client hello, etc)
3213 * only from sources that are in the active remote
3214 * candidates list.
3215 */
3216
3217#ifdef HAVE_PJPROJECT
3218 if (rtp->ice) {
3219 int pass_src_check = 0;
3220 int ix = 0;
3221
3222 /*
3223 * You'd think that this check would cause a "deadlock"
3224 * because ast_rtp_ice_start_media calls dtls_perform_handshake
3225 * before it sets ice_media_started = 1 so how can we do a
3226 * handshake if we're dropping packets before we send them
3227 * to openssl. Fortunately, dtls_perform_handshake just sets
3228 * up openssl to do the handshake and doesn't actually perform it
3229 * itself and the locking prevents __rtp_recvfrom from
3230 * running before the ice_media_started flag is set. So only
3231 * unexpected DTLS packets can get dropped here.
3232 */
3233 if (!rtp->ice_media_started) {
3234 ast_log(LOG_WARNING, "%s: DTLS packet from %s dropped. ICE not completed yet.\n",
3237 return 0;
3238 }
3239
3240 /*
3241 * If we got this far, then there have to be candidates.
3242 * We have to use pjproject's rcands because they may have
3243 * peer reflexive candidates that our ice_active_remote_candidates
3244 * won't.
3245 */
3246 for (ix = 0; ix < rtp->ice->real_ice->rcand_cnt; ix++) {
3247 pj_ice_sess_cand *rcand = &rtp->ice->real_ice->rcand[ix];
3248 if (ast_sockaddr_pj_sockaddr_cmp(sa, &rcand->addr) == 0) {
3249 pass_src_check = 1;
3250 break;
3251 }
3252 }
3253
3254 if (!pass_src_check) {
3255 ast_log(LOG_WARNING, "%s: DTLS packet from %s dropped. Source not in ICE active candidate list.\n",
3258 return 0;
3259 }
3260 }
3261#endif
3262
3263 /*
3264 * A race condition is prevented between dtls_perform_handshake()
3265 * and this function because both functions have to get the
3266 * instance lock before they can do anything. The
3267 * dtls_perform_handshake() function needs to start the timer
3268 * before we stop it below.
3269 */
3270
3271 /* Before we feed data into OpenSSL ensure that the timeout timer is either stopped or completed */
3272 ao2_unlock(instance);
3273 dtls_srtp_stop_timeout_timer(instance, rtp, rtcp);
3274 ao2_lock(instance);
3275
3276 /* If we don't yet know if we are active or passive and we receive a packet... we are obviously passive */
3277 if (dtls->dtls_setup == AST_RTP_DTLS_SETUP_ACTPASS) {
3278 dtls->dtls_setup = AST_RTP_DTLS_SETUP_PASSIVE;
3279 SSL_set_accept_state(dtls->ssl);
3280 }
3281
3282 BIO_write(dtls->read_bio, buf, len);
3283
3284 len = SSL_read(dtls->ssl, buf, len);
3285
3286 if ((len < 0) && (SSL_get_error(dtls->ssl, len) == SSL_ERROR_SSL)) {
3287 unsigned long error = ERR_get_error();
3288 ast_log(LOG_ERROR, "DTLS failure occurred on RTP instance '%p' due to reason '%s', terminating\n",
3289 instance, ERR_reason_error_string(error));
3290 return -1;
3291 }
3292
3293 if (SSL_is_init_finished(dtls->ssl)) {
3294 /* Any further connections will be existing since this is now established */
3295 dtls->connection = AST_RTP_DTLS_CONNECTION_EXISTING;
3296 /* Use the keying material to set up key/salt information */
3297 if ((res = dtls_srtp_setup(rtp, instance, rtcp))) {
3298 return res;
3299 }
3300 /* Notify that dtls has been established */
3302
3303 ast_debug_dtls(3, "(%p) DTLS - __rtp_recvfrom rtp=%p - established'\n", instance, rtp);
3304 } else {
3305 /* Since we've sent additional traffic start the timeout timer for retransmission */
3306 dtls_srtp_start_timeout_timer(instance, rtp, rtcp);
3307 }
3308
3309 return res;
3310 }
3311#endif
3312
3313#ifdef HAVE_PJPROJECT
3314 if (!ast_sockaddr_isnull(loop) && !ast_sockaddr_cmp(loop, sa)) {
3315 /* ICE traffic will have been handled in the TURN callback, so skip it but update the address
3316 * so it reflects the actual source and not the loopback
3317 */
3318 if (rtcp) {
3319 ast_sockaddr_copy(sa, &rtp->rtcp->them);
3320 } else {
3322 }
3323 } else if (rtp->ice) {
3324 pj_str_t combined = pj_str(ast_sockaddr_stringify(sa));
3325 pj_sockaddr address;
3326 pj_status_t status;
3327 struct ice_wrap *ice;
3328
3329 pj_thread_register_check();
3330
3331 pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &combined, &address);
3332
3333 /* Release the instance lock to avoid deadlock with PJPROJECT group lock */
3334 ice = rtp->ice;
3335 ao2_ref(ice, +1);
3336 ao2_unlock(instance);
3337 status = pj_ice_sess_on_rx_pkt(ice->real_ice,
3340 pj_sockaddr_get_len(&address));
3341 ao2_ref(ice, -1);
3342 ao2_lock(instance);
3343 if (status != PJ_SUCCESS) {
3344 char err_buf[100];
3345
3346 pj_strerror(status, err_buf, sizeof(err_buf));
3347 ast_log(LOG_WARNING, "PJ ICE Rx error status code: %d '%s'.\n",
3348 (int)status, err_buf);
3349 return -1;
3350 }
3351 if (!rtp->passthrough) {
3352 /* If a unidirectional ICE negotiation occurs then lock on to the source of the
3353 * ICE traffic and use it as the target. This will occur if the remote side only
3354 * wants to receive media but never send to us.
3355 */
3356 if (!rtp->ice_active_remote_candidates && !rtp->ice_proposed_remote_candidates) {
3357 if (rtcp) {
3358 ast_sockaddr_copy(&rtp->rtcp->them, sa);
3359 } else {
3361 }
3362 }
3363 return 0;
3364 }
3365 rtp->passthrough = 0;
3366 }
3367#endif
3368
3369 return len;
3370}
jack_status_t status
Definition: app_jack.c:146
#define ast_log
Definition: astobj2.c:42
#define ao2_unlock(a)
Definition: astobj2.h:729
#define ao2_lock(a)
Definition: astobj2.h:717
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
char * address
Definition: f2c.h:59
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define LOG_ERROR
#define LOG_WARNING
static char * ast_sockaddr_stringify(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() with default format.
Definition: netsock2.h:256
static void ast_sockaddr_copy(struct ast_sockaddr *dst, const struct ast_sockaddr *src)
Copies the data from one ast_sockaddr to another.
Definition: netsock2.h:167
static int ast_sockaddr_isnull(const struct ast_sockaddr *addr)
Checks if the ast_sockaddr is null. "null" in this sense essentially means uninitialized,...
Definition: netsock2.h:127
ssize_t ast_recvfrom(int sockfd, void *buf, size_t len, int flags, struct ast_sockaddr *src_addr)
Wrapper around recvfrom(2) that uses struct ast_sockaddr.
Definition: netsock2.c:606
int ast_sockaddr_cmp(const struct ast_sockaddr *a, const struct ast_sockaddr *b)
Compares two ast_sockaddr structures.
Definition: netsock2.c:388
int ast_sockaddr_pj_sockaddr_cmp(const struct ast_sockaddr *addr, const pj_sockaddr *pjaddr)
Compare an ast_sockaddr to a pj_sockaddr.
#define RTP_DTLS_ESTABLISHED
#define TRANSPORT_SOCKET_RTP
#define TRANSPORT_SOCKET_RTCP
@ AST_RTP_DTLS_SETUP_PASSIVE
Definition: rtp_engine.h:561
@ AST_RTP_DTLS_SETUP_ACTPASS
Definition: rtp_engine.h:562
@ AST_RTP_ICE_COMPONENT_RTCP
Definition: rtp_engine.h:510
@ AST_RTP_ICE_COMPONENT_RTP
Definition: rtp_engine.h:509
void * ast_rtp_instance_get_data(struct ast_rtp_instance *instance)
Get the data portion of an RTP instance.
Definition: rtp_engine.c:584
#define ast_rtp_instance_get_remote_address(instance, address)
Get the address of the remote endpoint that we are sending RTP to.
Definition: rtp_engine.h:1238
#define ast_rtp_instance_set_remote_address(instance, address)
Set the address of the remote endpoint that we are sending RTP to.
Definition: rtp_engine.h:1126
@ AST_RTP_DTLS_CONNECTION_EXISTING
Definition: rtp_engine.h:569
#define ast_debug_dtls(sublevel,...)
Log debug level DTLS information.
Definition: rtp_engine.h:2940
const char * ast_rtp_instance_get_channel_id(struct ast_rtp_instance *instance)
Get the unique ID of the channel that owns this RTP instance.
Definition: rtp_engine.c:569
struct ast_sockaddr them
RTP session description.
struct ast_rtcp * rtcp
Socket address structure.
Definition: netsock2.h:97
int error(const char *format,...)
Definition: utils/frame.c:999
FILE * in
Definition: utils/frame.c:33

References ao2_lock, ao2_ref, ao2_unlock, ast_debug_dtls, ast_log, ast_recvfrom(), AST_RTP_DTLS_CONNECTION_EXISTING, AST_RTP_DTLS_SETUP_ACTPASS, AST_RTP_DTLS_SETUP_PASSIVE, AST_RTP_ICE_COMPONENT_RTCP, AST_RTP_ICE_COMPONENT_RTP, ast_rtp_instance_get_channel_id(), ast_rtp_instance_get_data(), ast_rtp_instance_get_remote_address, ast_rtp_instance_set_remote_address, ast_sockaddr_cmp(), ast_sockaddr_copy(), ast_sockaddr_isnull(), ast_sockaddr_pj_sockaddr_cmp(), ast_sockaddr_stringify(), buf, error(), in, len(), LOG_ERROR, LOG_WARNING, ast_rtp::rtcp, RTP_DTLS_ESTABLISHED, ast_rtp::s, ast_rtcp::s, status, ast_rtcp::them, TRANSPORT_SOCKET_RTCP, and TRANSPORT_SOCKET_RTP.

Referenced by rtcp_recvfrom(), and rtp_recvfrom().

◆ __rtp_sendto()

static int __rtp_sendto ( struct ast_rtp_instance instance,
void *  buf,
size_t  size,
int  flags,
struct ast_sockaddr sa,
int  rtcp,
int *  via_ice,
int  use_srtp 
)
static
Precondition
instance is locked

Definition at line 3385 of file res_rtp_asterisk.c.

3386{
3387 int len = size;
3388 void *temp = buf;
3389 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
3390 struct ast_rtp_instance *transport = rtp->bundled ? rtp->bundled : instance;
3391 struct ast_rtp *transport_rtp = ast_rtp_instance_get_data(transport);
3392 struct ast_srtp *srtp = ast_rtp_instance_get_srtp(transport, rtcp);
3393 int res;
3394
3395 *via_ice = 0;
3396
3397 if (use_srtp && res_srtp && srtp && res_srtp->protect(srtp, &temp, &len, rtcp) < 0) {
3398 return -1;
3399 }
3400
3401#ifdef HAVE_PJPROJECT
3402 if (transport_rtp->ice) {
3404 pj_status_t status;
3405 struct ice_wrap *ice;
3406
3407 /* If RTCP is sharing the same socket then use the same component */
3408 if (rtcp && rtp->rtcp->s == rtp->s) {
3409 component = AST_RTP_ICE_COMPONENT_RTP;
3410 }
3411
3412 pj_thread_register_check();
3413
3414 /* Release the instance lock to avoid deadlock with PJPROJECT group lock */
3415 ice = transport_rtp->ice;
3416 ao2_ref(ice, +1);
3417 if (instance == transport) {
3418 ao2_unlock(instance);
3419 }
3420 status = pj_ice_sess_send_data(ice->real_ice, component, temp, len);
3421 ao2_ref(ice, -1);
3422 if (instance == transport) {
3423 ao2_lock(instance);
3424 }
3425 if (status == PJ_SUCCESS) {
3426 *via_ice = 1;
3427 return len;
3428 }
3429 }
3430#endif
3431
3432 res = ast_sendto(rtcp ? transport_rtp->rtcp->s : transport_rtp->s, temp, len, flags, sa);
3433 if (res > 0) {
3434 ast_rtp_instance_set_last_tx(instance, time(NULL));
3435 }
3436
3437 return res;
3438}
ssize_t ast_sendto(int sockfd, const void *buf, size_t len, int flags, const struct ast_sockaddr *dest_addr)
Wrapper around sendto(2) that uses ast_sockaddr.
Definition: netsock2.c:614
struct ast_srtp_res * res_srtp
Definition: rtp_engine.c:176
ast_rtp_ice_component_type
ICE component types.
Definition: rtp_engine.h:508
struct ast_srtp * ast_rtp_instance_get_srtp(struct ast_rtp_instance *instance, int rtcp)
Obtain the SRTP instance associated with an RTP instance.
Definition: rtp_engine.c:2800
void ast_rtp_instance_set_last_tx(struct ast_rtp_instance *rtp, time_t time)
Set the last RTP transmission time.
Definition: rtp_engine.c:3828
struct ast_rtp_instance * bundled
int(* protect)(struct ast_srtp *srtp, void **buf, int *size, int rtcp)
Definition: res_srtp.h:50

References ao2_lock, ao2_ref, ao2_unlock, AST_RTP_ICE_COMPONENT_RTCP, AST_RTP_ICE_COMPONENT_RTP, ast_rtp_instance_get_data(), ast_rtp_instance_get_srtp(), ast_rtp_instance_set_last_tx(), ast_sendto(), buf, ast_rtp::bundled, len(), NULL, ast_srtp_res::protect, res_srtp, ast_rtp::rtcp, ast_rtp::s, ast_rtcp::s, and status.

Referenced by rtcp_sendto(), and rtp_sendto().

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 10346 of file res_rtp_asterisk.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 10346 of file res_rtp_asterisk.c.

◆ ast_rtcp_calc_interval()

static unsigned int ast_rtcp_calc_interval ( struct ast_rtp rtp)
static
Todo:
XXX Do a more reasonable calculation on this one Look in RFC 3550 Section A.7 for an example

Definition at line 3461 of file res_rtp_asterisk.c.

3462{
3463 unsigned int interval;
3464 /*! \todo XXX Do a more reasonable calculation on this one
3465 * Look in RFC 3550 Section A.7 for an example*/
3466 interval = rtcpinterval;
3467 return interval;
3468}
static int rtcpinterval

References rtcpinterval.

Referenced by ast_rtp_interpret(), and rtp_raw_write().

◆ ast_rtcp_calculate_sr_rr_statistics()

static int ast_rtcp_calculate_sr_rr_statistics ( struct ast_rtp_instance instance,
struct ast_rtp_rtcp_report rtcp_report,
struct ast_sockaddr  remote_address,
int  ice,
int  sr 
)
static

Definition at line 4735 of file res_rtp_asterisk.c.

4737{
4738 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4739 struct ast_rtp_rtcp_report_block *report_block = NULL;
4740 RAII_VAR(struct ast_json *, message_blob, NULL, ast_json_unref);
4741
4742 if (!rtp || !rtp->rtcp) {
4743 return 0;
4744 }
4745
4746 if (ast_sockaddr_isnull(&rtp->rtcp->them)) {
4747 return 0;
4748 }
4749
4750 if (!rtcp_report) {
4751 return -1;
4752 }
4753
4754 report_block = rtcp_report->report_block[0];
4755
4756 if (sr) {
4757 rtp->rtcp->txlsr = rtcp_report->sender_information.ntp_timestamp;
4758 rtp->rtcp->sr_count++;
4759 rtp->rtcp->lastsrtxcount = rtp->txcount;
4760 } else {
4761 rtp->rtcp->rr_count++;
4762 }
4763
4764 if (rtcp_debug_test_addr(&rtp->rtcp->them)) {
4765 ast_verbose("* Sent RTCP %s to %s%s\n", sr ? "SR" : "RR",
4766 ast_sockaddr_stringify(&remote_address), ice ? " (via ICE)" : "");
4767 ast_verbose(" Our SSRC: %u\n", rtcp_report->ssrc);
4768 if (sr) {
4769 ast_verbose(" Sent(NTP): %u.%06u\n",
4770 (unsigned int)rtcp_report->sender_information.ntp_timestamp.tv_sec,
4771 (unsigned int)rtcp_report->sender_information.ntp_timestamp.tv_usec);
4772 ast_verbose(" Sent(RTP): %u\n", rtcp_report->sender_information.rtp_timestamp);
4773 ast_verbose(" Sent packets: %u\n", rtcp_report->sender_information.packet_count);
4774 ast_verbose(" Sent octets: %u\n", rtcp_report->sender_information.octet_count);
4775 }
4776 if (report_block) {
4777 int rate = ast_rtp_get_rate(rtp->f.subclass.format);
4778 ast_verbose(" Report block:\n");
4779 ast_verbose(" Their SSRC: %u\n", report_block->source_ssrc);
4780 ast_verbose(" Fraction lost: %d\n", report_block->lost_count.fraction);
4781 ast_verbose(" Cumulative loss: %u\n", report_block->lost_count.packets);
4782 ast_verbose(" Highest seq no: %u\n", report_block->highest_seq_no);
4783 ast_verbose(" IA jitter (samp): %u\n", report_block->ia_jitter);
4784 ast_verbose(" IA jitter (secs): %.6f\n", ast_samp2sec(report_block->ia_jitter, rate));
4785 ast_verbose(" Their last SR: %u\n", report_block->lsr);
4786 ast_verbose(" DLSR: %4.4f (sec)\n\n", (double)(report_block->dlsr / 65536.0));
4787 }
4788 }
4789
4790 message_blob = ast_json_pack("{s: s, s: s, s: f}",
4791 "to", ast_sockaddr_stringify(&remote_address),
4792 "from", rtp->rtcp->local_addr_str,
4793 "mes", rtp->rxmes);
4794
4796 rtcp_report, message_blob);
4797
4798 return 1;
4799}
void ast_verbose(const char *fmt,...)
Definition: extconf.c:2206
struct stasis_message_type * ast_rtp_rtcp_sent_type(void)
Message type for an RTCP message sent from this Asterisk instance.
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:612
static int rtcp_debug_test_addr(struct ast_sockaddr *addr)
void ast_rtp_publish_rtcp_message(struct ast_rtp_instance *rtp, struct stasis_message_type *message_type, struct ast_rtp_rtcp_report *report, struct ast_json *blob)
Publish an RTCP message to Stasis Message Bus API.
Definition: rtp_engine.c:3527
int ast_rtp_get_rate(const struct ast_format *format)
Retrieve the sample rate of a format according to RTP specifications.
Definition: rtp_engine.c:4108
struct ast_format * format
struct ast_frame_subclass subclass
Abstract JSON element (object, array, string, int, ...).
unsigned int sr_count
unsigned int lastsrtxcount
struct timeval txlsr
unsigned int rr_count
char * local_addr_str
A report block within a SR/RR report.
Definition: rtp_engine.h:341
unsigned int highest_seq_no
Definition: rtp_engine.h:347
struct ast_rtp_rtcp_report_block::@272 lost_count
unsigned short fraction
Definition: rtp_engine.h:344
unsigned int source_ssrc
Definition: rtp_engine.h:342
struct ast_rtp_rtcp_report::@273 sender_information
unsigned int rtp_timestamp
Definition: rtp_engine.h:362
struct ast_rtp_rtcp_report_block * report_block[0]
Definition: rtp_engine.h:369
struct timeval ntp_timestamp
Definition: rtp_engine.h:361
unsigned int octet_count
Definition: rtp_engine.h:364
unsigned int ssrc
Definition: rtp_engine.h:358
unsigned int packet_count
Definition: rtp_engine.h:363
double rxmes
struct ast_frame f
unsigned int txcount
double ast_samp2sec(unsigned int _nsamp, unsigned int _rate)
Returns the duration in seconds of _nsamp samples at rate _rate.
Definition: time.h:316
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941

References ast_json_pack(), ast_json_unref(), ast_rtp_get_rate(), ast_rtp_instance_get_data(), ast_rtp_publish_rtcp_message(), ast_rtp_rtcp_sent_type(), ast_samp2sec(), ast_sockaddr_isnull(), ast_sockaddr_stringify(), ast_verbose(), ast_rtp_rtcp_report_block::dlsr, ast_rtp::f, ast_frame_subclass::format, ast_rtp_rtcp_report_block::fraction, ast_rtp_rtcp_report_block::highest_seq_no, ast_rtp_rtcp_report_block::ia_jitter, ast_rtcp::lastsrtxcount, ast_rtcp::local_addr_str, ast_rtp_rtcp_report_block::lost_count, ast_rtp_rtcp_report_block::lsr, ast_rtp_rtcp_report::ntp_timestamp, NULL, ast_rtp_rtcp_report::octet_count, ast_rtp_rtcp_report::packet_count, ast_rtp_rtcp_report_block::packets, RAII_VAR, ast_rtp_rtcp_report::report_block, ast_rtcp::rr_count, ast_rtp::rtcp, rtcp_debug_test_addr(), ast_rtp_rtcp_report::rtp_timestamp, ast_rtp::rxmes, ast_rtp_rtcp_report::sender_information, ast_rtp_rtcp_report_block::source_ssrc, ast_rtcp::sr_count, ast_rtp_rtcp_report::ssrc, ast_frame::subclass, ast_rtcp::them, ast_rtp::txcount, and ast_rtcp::txlsr.

Referenced by ast_rtcp_write(), ast_rtp_read(), rtp_write_rtcp_fir(), and rtp_write_rtcp_psfb().

◆ ast_rtcp_generate_compound_prefix()

static int ast_rtcp_generate_compound_prefix ( struct ast_rtp_instance instance,
unsigned char *  rtcpheader,
struct ast_rtp_rtcp_report report,
int *  sr 
)
static

Definition at line 4859 of file res_rtp_asterisk.c.

4861{
4862 int packet_len = 0;
4863 int res;
4864
4865 /* Every RTCP packet needs to be sent out with a SR/RR and SDES prefixing it.
4866 * At the end of this function, rtcpheader should contain both of those packets,
4867 * and will return the length of the overall packet. This can be used to determine
4868 * where further packets can be inserted in the compound packet.
4869 */
4870 res = ast_rtcp_generate_report(instance, rtcpheader, report, sr);
4871
4872 if (res == 0 || res == 1) {
4873 ast_debug_rtcp(1, "(%p) RTCP failed to generate %s report!\n", instance, sr ? "SR" : "RR");
4874 return 0;
4875 }
4876
4877 packet_len += res;
4878
4879 res = ast_rtcp_generate_sdes(instance, rtcpheader + packet_len, report);
4880
4881 if (res == 0 || res == 1) {
4882 ast_debug_rtcp(1, "(%p) RTCP failed to generate SDES!\n", instance);
4883 return 0;
4884 }
4885
4886 return packet_len + res;
4887}
static int ast_rtcp_generate_report(struct ast_rtp_instance *instance, unsigned char *rtcpheader, struct ast_rtp_rtcp_report *rtcp_report, int *sr)
static int ast_rtcp_generate_sdes(struct ast_rtp_instance *instance, unsigned char *rtcpheader, struct ast_rtp_rtcp_report *rtcp_report)
#define ast_debug_rtcp(sublevel,...)
Log debug level RTCP information.
Definition: rtp_engine.h:2923

References ast_debug_rtcp, ast_rtcp_generate_report(), and ast_rtcp_generate_sdes().

Referenced by ast_rtcp_write(), ast_rtp_read(), rtp_write_rtcp_fir(), and rtp_write_rtcp_psfb().

◆ ast_rtcp_generate_nack()

static int ast_rtcp_generate_nack ( struct ast_rtp_instance instance,
unsigned char *  rtcpheader 
)
static

Definition at line 4889 of file res_rtp_asterisk.c.

4890{
4891 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4892 int packet_len;
4893 int blp_index = -1;
4894 int current_seqno;
4895 unsigned int fci = 0;
4896 size_t remaining_missing_seqno;
4897
4898 if (!rtp || !rtp->rtcp) {
4899 return 0;
4900 }
4901
4902 if (ast_sockaddr_isnull(&rtp->rtcp->them)) {
4903 return 0;
4904 }
4905
4906 current_seqno = rtp->expectedrxseqno;
4907 remaining_missing_seqno = AST_VECTOR_SIZE(&rtp->missing_seqno);
4908 packet_len = 12; /* The header length is 12 (version line, packet source SSRC, media source SSRC) */
4909
4910 /* If there are no missing sequence numbers then don't bother sending a NACK needlessly */
4911 if (!remaining_missing_seqno) {
4912 return 0;
4913 }
4914
4915 /* This iterates through the possible forward sequence numbers seeing which ones we
4916 * have no packet for, adding it to the NACK until we are out of missing packets.
4917 */
4918 while (remaining_missing_seqno) {
4919 int *missing_seqno;
4920
4921 /* On the first entry to this loop blp_index will be -1, so this will become 0
4922 * and the sequence number will be placed into the packet as the PID.
4923 */
4924 blp_index++;
4925
4926 missing_seqno = AST_VECTOR_GET_CMP(&rtp->missing_seqno, current_seqno,
4928 if (missing_seqno) {
4929 /* We hit the max blp size, reset */
4930 if (blp_index >= 17) {
4931 put_unaligned_uint32(rtcpheader + packet_len, htonl(fci));
4932 fci = 0;
4933 blp_index = 0;
4934 packet_len += 4;
4935 }
4936
4937 if (blp_index == 0) {
4938 fci |= (current_seqno << 16);
4939 } else {
4940 fci |= (1 << (blp_index - 1));
4941 }
4942
4943 /* Since we've used a missing sequence number, we're down one */
4944 remaining_missing_seqno--;
4945 }
4946
4947 /* Handle cycling of the sequence number */
4948 current_seqno++;
4949 if (current_seqno == SEQNO_CYCLE_OVER) {
4950 current_seqno = 0;
4951 }
4952 }
4953
4954 put_unaligned_uint32(rtcpheader + packet_len, htonl(fci));
4955 packet_len += 4;
4956
4957 /* Length MUST be 2+n, where n is the number of NACKs. Same as length in words minus 1 */
4958 put_unaligned_uint32(rtcpheader, htonl((2 << 30) | (AST_RTP_RTCP_FMT_NACK << 24)
4959 | (AST_RTP_RTCP_RTPFB << 16) | ((packet_len / 4) - 1)));
4960 put_unaligned_uint32(rtcpheader + 4, htonl(rtp->ssrc));
4961 put_unaligned_uint32(rtcpheader + 8, htonl(rtp->themssrc));
4962
4963 return packet_len;
4964}
static int find_by_value(int elem, int value)
Helper function to find an elem in a vector by value.
#define SEQNO_CYCLE_OVER
#define AST_RTP_RTCP_RTPFB
Definition: rtp_engine.h:322
#define AST_RTP_RTCP_FMT_NACK
Definition: rtp_engine.h:328
int expectedrxseqno
unsigned int ssrc
struct ast_rtp::@471 missing_seqno
static void put_unaligned_uint32(void *p, unsigned int datum)
Definition: unaligned.h:58
#define AST_VECTOR_GET_CMP(vec, value, cmp)
Get an element from a vector that matches the given comparison.
Definition: vector.h:731

References ast_rtp_instance_get_data(), AST_RTP_RTCP_FMT_NACK, AST_RTP_RTCP_RTPFB, ast_sockaddr_isnull(), AST_VECTOR_GET_CMP, AST_VECTOR_SIZE, ast_rtp::expectedrxseqno, find_by_value(), ast_rtp::missing_seqno, put_unaligned_uint32(), ast_rtp::rtcp, SEQNO_CYCLE_OVER, ast_rtp::ssrc, ast_rtcp::them, and ast_rtp::themssrc.

Referenced by ast_rtp_read().

◆ ast_rtcp_generate_report()

static int ast_rtcp_generate_report ( struct ast_rtp_instance instance,
unsigned char *  rtcpheader,
struct ast_rtp_rtcp_report rtcp_report,
int *  sr 
)
static

Definition at line 4642 of file res_rtp_asterisk.c.

4644{
4645 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4646 int len = 0;
4647 struct timeval now;
4648 unsigned int now_lsw;
4649 unsigned int now_msw;
4650 unsigned int lost_packets;
4651 int fraction_lost;
4652 struct timeval dlsr = { 0, };
4653 struct ast_rtp_rtcp_report_block *report_block = NULL;
4654
4655 if (!rtp || !rtp->rtcp) {
4656 return 0;
4657 }
4658
4659 if (ast_sockaddr_isnull(&rtp->rtcp->them)) { /* This'll stop rtcp for this rtp session */
4660 /* RTCP was stopped. */
4661 return 0;
4662 }
4663
4664 if (!rtcp_report) {
4665 return 1;
4666 }
4667
4668 *sr = rtp->txcount > rtp->rtcp->lastsrtxcount ? 1 : 0;
4669
4670 /* Compute statistics */
4671 calculate_lost_packet_statistics(rtp, &lost_packets, &fraction_lost);
4672 /*
4673 * update_local_mes_stats must be called AFTER
4674 * calculate_lost_packet_statistics
4675 */
4677
4678 gettimeofday(&now, NULL);
4679 rtcp_report->reception_report_count = rtp->themssrc_valid ? 1 : 0;
4680 rtcp_report->ssrc = rtp->ssrc;
4681 rtcp_report->type = *sr ? RTCP_PT_SR : RTCP_PT_RR;
4682 if (*sr) {
4683 rtcp_report->sender_information.ntp_timestamp = now;
4684 rtcp_report->sender_information.rtp_timestamp = rtp->lastts;
4685 rtcp_report->sender_information.packet_count = rtp->txcount;
4686 rtcp_report->sender_information.octet_count = rtp->txoctetcount;
4687 }
4688
4689 if (rtp->themssrc_valid) {
4690 report_block = ast_calloc(1, sizeof(*report_block));
4691 if (!report_block) {
4692 return 1;
4693 }
4694
4695 rtcp_report->report_block[0] = report_block;
4696 report_block->source_ssrc = rtp->themssrc;
4697 report_block->lost_count.fraction = (fraction_lost & 0xff);
4698 report_block->lost_count.packets = (lost_packets & 0xffffff);
4699 report_block->highest_seq_no = (rtp->cycles | (rtp->lastrxseqno & 0xffff));
4700 report_block->ia_jitter = (unsigned int)rtp->rxjitter_samples;
4701 report_block->lsr = rtp->rtcp->themrxlsr;
4702 /* If we haven't received an SR report, DLSR should be 0 */
4703 if (!ast_tvzero(rtp->rtcp->rxlsr)) {
4704 timersub(&now, &rtp->rtcp->rxlsr, &dlsr);
4705 report_block->dlsr = (((dlsr.tv_sec * 1000) + (dlsr.tv_usec / 1000)) * 65536) / 1000;
4706 }
4707 }
4708 timeval2ntp(rtcp_report->sender_information.ntp_timestamp, &now_msw, &now_lsw);
4709 put_unaligned_uint32(rtcpheader + 4, htonl(rtcp_report->ssrc)); /* Our SSRC */
4710 len += 8;
4711 if (*sr) {
4712 put_unaligned_uint32(rtcpheader + len, htonl(now_msw)); /* now, MSW. gettimeofday() + SEC_BETWEEN_1900_AND_1970 */
4713 put_unaligned_uint32(rtcpheader + len + 4, htonl(now_lsw)); /* now, LSW */
4714 put_unaligned_uint32(rtcpheader + len + 8, htonl(rtcp_report->sender_information.rtp_timestamp));
4715 put_unaligned_uint32(rtcpheader + len + 12, htonl(rtcp_report->sender_information.packet_count));
4716 put_unaligned_uint32(rtcpheader + len + 16, htonl(rtcp_report->sender_information.octet_count));
4717 len += 20;
4718 }
4719 if (report_block) {
4720 put_unaligned_uint32(rtcpheader + len, htonl(report_block->source_ssrc)); /* Their SSRC */
4721 put_unaligned_uint32(rtcpheader + len + 4, htonl((report_block->lost_count.fraction << 24) | report_block->lost_count.packets));
4722 put_unaligned_uint32(rtcpheader + len + 8, htonl(report_block->highest_seq_no));
4723 put_unaligned_uint32(rtcpheader + len + 12, htonl(report_block->ia_jitter));
4724 put_unaligned_uint32(rtcpheader + len + 16, htonl(report_block->lsr));
4725 put_unaligned_uint32(rtcpheader + len + 20, htonl(report_block->dlsr));
4726 len += 24;
4727 }
4728
4729 put_unaligned_uint32(rtcpheader, htonl((2 << 30) | (rtcp_report->reception_report_count << 24)
4730 | ((*sr ? RTCP_PT_SR : RTCP_PT_RR) << 16) | ((len/4)-1)));
4731
4732 return len;
4733}
if(!yyg->yy_init)
Definition: ast_expr2f.c:854
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
void timersub(struct timeval *tvend, struct timeval *tvstart, struct timeval *tvdiff)
#define RTCP_PT_RR
static void calculate_lost_packet_statistics(struct ast_rtp *rtp, unsigned int *lost_packets, int *fraction_lost)
#define RTCP_PT_SR
static void timeval2ntp(struct timeval tv, unsigned int *msw, unsigned int *lsw)
static void update_local_mes_stats(struct ast_rtp *rtp)
unsigned int themrxlsr
struct timeval rxlsr
unsigned int type
Definition: rtp_engine.h:359
unsigned short reception_report_count
Definition: rtp_engine.h:357
unsigned int lastts
unsigned int cycles
double rxjitter_samples
unsigned int txoctetcount
int ast_tvzero(const struct timeval t)
Returns true if the argument is 0,0.
Definition: time.h:117

References ast_calloc, ast_rtp_instance_get_data(), ast_sockaddr_isnull(), ast_tvzero(), calculate_lost_packet_statistics(), ast_rtp::cycles, ast_rtp_rtcp_report_block::dlsr, ast_rtp_rtcp_report_block::fraction, ast_rtp_rtcp_report_block::highest_seq_no, ast_rtp_rtcp_report_block::ia_jitter, if(), ast_rtp::lastrxseqno, ast_rtcp::lastsrtxcount, ast_rtp::lastts, len(), ast_rtp_rtcp_report_block::lost_count, ast_rtp_rtcp_report_block::lsr, ast_rtp_rtcp_report::ntp_timestamp, NULL, ast_rtp_rtcp_report::octet_count, ast_rtp_rtcp_report::packet_count, ast_rtp_rtcp_report_block::packets, put_unaligned_uint32(), ast_rtp_rtcp_report::reception_report_count, ast_rtp_rtcp_report::report_block, ast_rtp::rtcp, RTCP_PT_RR, RTCP_PT_SR, ast_rtp_rtcp_report::rtp_timestamp, ast_rtp::rxjitter_samples, ast_rtcp::rxlsr, ast_rtp_rtcp_report::sender_information, ast_rtp_rtcp_report_block::source_ssrc, ast_rtp_rtcp_report::ssrc, ast_rtp::ssrc, ast_rtcp::them, ast_rtcp::themrxlsr, ast_rtp::themssrc, ast_rtp::themssrc_valid, timersub(), timeval2ntp(), ast_rtp::txcount, ast_rtp::txoctetcount, ast_rtp_rtcp_report::type, and update_local_mes_stats().

Referenced by ast_rtcp_generate_compound_prefix().

◆ ast_rtcp_generate_sdes()

static int ast_rtcp_generate_sdes ( struct ast_rtp_instance instance,
unsigned char *  rtcpheader,
struct ast_rtp_rtcp_report rtcp_report 
)
static

Definition at line 4801 of file res_rtp_asterisk.c.

4803{
4804 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4805 int len = 0;
4806 uint16_t sdes_packet_len_bytes;
4807 uint16_t sdes_packet_len_rounded;
4808
4809 if (!rtp || !rtp->rtcp) {
4810 return 0;
4811 }
4812
4813 if (ast_sockaddr_isnull(&rtp->rtcp->them)) {
4814 return 0;
4815 }
4816
4817 if (!rtcp_report) {
4818 return -1;
4819 }
4820
4821 sdes_packet_len_bytes =
4822 4 + /* RTCP Header */
4823 4 + /* SSRC */
4824 1 + /* Type (CNAME) */
4825 1 + /* Text Length */
4826 AST_UUID_STR_LEN /* Text and NULL terminator */
4827 ;
4828
4829 /* Round to 32 bit boundary */
4830 sdes_packet_len_rounded = (sdes_packet_len_bytes + 3) & ~0x3;
4831
4832 put_unaligned_uint32(rtcpheader, htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES << 16) | ((sdes_packet_len_rounded / 4) - 1)));
4833 put_unaligned_uint32(rtcpheader + 4, htonl(rtcp_report->ssrc));
4834 rtcpheader[8] = 0x01; /* CNAME */
4835 rtcpheader[9] = AST_UUID_STR_LEN - 1; /* Number of bytes of text */
4836 memcpy(rtcpheader + 10, rtp->cname, AST_UUID_STR_LEN);
4837 len += 10 + AST_UUID_STR_LEN;
4838
4839 /* Padding - Note that we don't set the padded bit on the packet. From
4840 * RFC 3550 Section 6.5:
4841 *
4842 * No length octet follows the null item type octet, but additional null
4843 * octets MUST be included if needd to pad until the next 32-bit
4844 * boundary. Note that this padding is separate from that indicated by
4845 * the P bit in the RTCP header.
4846 *
4847 * These bytes will already be zeroed out during array initialization.
4848 */
4849 len += (sdes_packet_len_rounded - sdes_packet_len_bytes);
4850
4851 return len;
4852}
#define RTCP_PT_SDES
char cname[AST_UUID_STR_LEN]
#define AST_UUID_STR_LEN
Definition: uuid.h:27

References ast_rtp_instance_get_data(), ast_sockaddr_isnull(), AST_UUID_STR_LEN, ast_rtp::cname, len(), put_unaligned_uint32(), ast_rtp::rtcp, RTCP_PT_SDES, ast_rtp_rtcp_report::ssrc, and ast_rtcp::them.

Referenced by ast_rtcp_generate_compound_prefix().

◆ ast_rtcp_interpret()

static struct ast_frame * ast_rtcp_interpret ( struct ast_rtp_instance instance,
struct ast_srtp srtp,
const unsigned char *  rtcpdata,
size_t  size,
struct ast_sockaddr addr 
)
static

True if we have seen an acceptable SSRC to learn the remote RTCP address

True if the ssrc value we have is valid and not garbage because it doesn't exist.

Always use packet source SSRC to find the rtp instance unless explicitly told not to.

Definition at line 6557 of file res_rtp_asterisk.c.

6559{
6560 struct ast_rtp_instance *transport = instance;
6561 struct ast_rtp *transport_rtp = ast_rtp_instance_get_data(instance);
6562 int len = size;
6563 unsigned int *rtcpheader = (unsigned int *)(rtcpdata);
6564 unsigned int packetwords;
6565 unsigned int position;
6566 unsigned int first_word;
6567 /*! True if we have seen an acceptable SSRC to learn the remote RTCP address */
6568 unsigned int ssrc_seen;
6569 struct ast_rtp_rtcp_report_block *report_block;
6570 struct ast_frame *f = &ast_null_frame;
6571#ifdef TEST_FRAMEWORK
6572 struct ast_rtp_engine_test *test_engine;
6573#endif
6574
6575 /* If this is encrypted then decrypt the payload */
6576 if ((*rtcpheader & 0xC0) && res_srtp && srtp && res_srtp->unprotect(
6577 srtp, rtcpheader, &len, 1 | (srtp_replay_protection << 1)) < 0) {
6578 return &ast_null_frame;
6579 }
6580
6581 packetwords = len / 4;
6582
6583 ast_debug_rtcp(2, "(%s) RTCP got report of %d bytes from %s\n",
6586
6587 /*
6588 * Validate the RTCP packet according to an adapted and slightly
6589 * modified RFC3550 validation algorithm.
6590 */
6591 if (packetwords < RTCP_HEADER_SSRC_LENGTH) {
6592 ast_debug_rtcp(2, "(%s) RTCP %p -- from %s: Frame size (%u words) is too short\n",
6594 transport_rtp, ast_sockaddr_stringify(addr), packetwords);
6595 return &ast_null_frame;
6596 }
6597 position = 0;
6598 first_word = ntohl(rtcpheader[position]);
6599 if ((first_word & RTCP_VALID_MASK) != RTCP_VALID_VALUE) {
6600 ast_debug_rtcp(2, "(%s) RTCP %p -- from %s: Failed first packet validity check\n",
6602 transport_rtp, ast_sockaddr_stringify(addr));
6603 return &ast_null_frame;
6604 }
6605 do {
6606 position += ((first_word >> RTCP_LENGTH_SHIFT) & RTCP_LENGTH_MASK) + 1;
6607 if (packetwords <= position) {
6608 break;
6609 }
6610 first_word = ntohl(rtcpheader[position]);
6611 } while ((first_word & RTCP_VERSION_MASK_SHIFTED) == RTCP_VERSION_SHIFTED);
6612 if (position != packetwords) {
6613 ast_debug_rtcp(2, "(%s) RTCP %p -- from %s: Failed packet version or length check\n",
6615 transport_rtp, ast_sockaddr_stringify(addr));
6616 return &ast_null_frame;
6617 }
6618
6619 /*
6620 * Note: RFC3605 points out that true NAT (vs NAPT) can cause RTCP
6621 * to have a different IP address and port than RTP. Otherwise, when
6622 * strictrtp is enabled we could reject RTCP packets not coming from
6623 * the learned RTP IP address if it is available.
6624 */
6625
6626 /*
6627 * strictrtp safety needs SSRC to match before we use the
6628 * sender's address for symmetrical RTP to send our RTCP
6629 * reports.
6630 *
6631 * If strictrtp is not enabled then claim to have already seen
6632 * a matching SSRC so we'll accept this packet's address for
6633 * symmetrical RTP.
6634 */
6635 ssrc_seen = transport_rtp->strict_rtp_state == STRICT_RTP_OPEN;
6636
6637 position = 0;
6638 while (position < packetwords) {
6639 unsigned int i;
6640 unsigned int pt;
6641 unsigned int rc;
6642 unsigned int ssrc;
6643 /*! True if the ssrc value we have is valid and not garbage because it doesn't exist. */
6644 unsigned int ssrc_valid;
6645 unsigned int length;
6646 unsigned int min_length;
6647 /*! Always use packet source SSRC to find the rtp instance unless explicitly told not to. */
6648 unsigned int use_packet_source = 1;
6649
6650 struct ast_json *message_blob;
6651 RAII_VAR(struct ast_rtp_rtcp_report *, rtcp_report, NULL, ao2_cleanup);
6652 struct ast_rtp_instance *child;
6653 struct ast_rtp *rtp;
6654 struct ast_rtp_rtcp_feedback *feedback;
6655
6656 i = position;
6657 first_word = ntohl(rtcpheader[i]);
6658 pt = (first_word >> RTCP_PAYLOAD_TYPE_SHIFT) & RTCP_PAYLOAD_TYPE_MASK;
6659 rc = (first_word >> RTCP_REPORT_COUNT_SHIFT) & RTCP_REPORT_COUNT_MASK;
6660 /* RFC3550 says 'length' is the number of words in the packet - 1 */
6661 length = ((first_word >> RTCP_LENGTH_SHIFT) & RTCP_LENGTH_MASK) + 1;
6662
6663 /* Check expected RTCP packet record length */
6664 min_length = RTCP_HEADER_SSRC_LENGTH;
6665 switch (pt) {
6666 case RTCP_PT_SR:
6667 min_length += RTCP_SR_BLOCK_WORD_LENGTH;
6668 /* fall through */
6669 case RTCP_PT_RR:
6670 min_length += (rc * RTCP_RR_BLOCK_WORD_LENGTH);
6671 use_packet_source = 0;
6672 break;
6673 case RTCP_PT_FUR:
6674 break;
6675 case AST_RTP_RTCP_RTPFB:
6676 switch (rc) {
6678 min_length += RTCP_FB_NACK_BLOCK_WORD_LENGTH;
6679 break;
6680 default:
6681 break;
6682 }
6683 use_packet_source = 0;
6684 break;
6685 case RTCP_PT_PSFB:
6686 switch (rc) {
6688 min_length += RTCP_FB_REMB_BLOCK_WORD_LENGTH;
6689 break;
6690 default:
6691 break;
6692 }
6693 break;
6694 case RTCP_PT_SDES:
6695 case RTCP_PT_BYE:
6696 /*
6697 * There may not be a SSRC/CSRC present. The packet is
6698 * useless but still valid if it isn't present.
6699 *
6700 * We don't know what min_length should be so disable the check
6701 */
6702 min_length = length;
6703 break;
6704 default:
6705 ast_debug_rtcp(1, "(%p) RTCP %p -- from %s: %u(%s) skipping record\n",
6706 instance, transport_rtp, ast_sockaddr_stringify(addr), pt, rtcp_payload_type2str(pt));
6707 if (rtcp_debug_test_addr(addr)) {
6708 ast_verbose("\n");
6709 ast_verbose("RTCP from %s: %u(%s) skipping record\n",
6711 }
6712 position += length;
6713 continue;
6714 }
6715 if (length < min_length) {
6716 ast_debug_rtcp(1, "(%p) RTCP %p -- from %s: %u(%s) length field less than expected minimum. Min:%u Got:%u\n",
6717 instance, transport_rtp, ast_sockaddr_stringify(addr), pt, rtcp_payload_type2str(pt),
6718 min_length - 1, length - 1);
6719 return &ast_null_frame;
6720 }
6721
6722 /* Get the RTCP record SSRC if defined for the record */
6723 ssrc_valid = 1;
6724 switch (pt) {
6725 case RTCP_PT_SR:
6726 case RTCP_PT_RR:
6727 rtcp_report = ast_rtp_rtcp_report_alloc(rc);
6728 if (!rtcp_report) {
6729 return &ast_null_frame;
6730 }
6731 rtcp_report->reception_report_count = rc;
6732
6733 ssrc = ntohl(rtcpheader[i + 2]);
6734 rtcp_report->ssrc = ssrc;
6735 break;
6736 case RTCP_PT_FUR:
6737 case RTCP_PT_PSFB:
6738 ssrc = ntohl(rtcpheader[i + 1]);
6739 break;
6740 case AST_RTP_RTCP_RTPFB:
6741 ssrc = ntohl(rtcpheader[i + 2]);
6742 break;
6743 case RTCP_PT_SDES:
6744 case RTCP_PT_BYE:
6745 default:
6746 ssrc = 0;
6747 ssrc_valid = 0;
6748 break;
6749 }
6750
6751 if (rtcp_debug_test_addr(addr)) {
6752 const char *subtype = rtcp_payload_subtype2str(pt, rc);
6753
6754 ast_verbose("\n");
6755 ast_verbose("RTCP from %s\n", ast_sockaddr_stringify(addr));
6756 ast_verbose("PT: %u (%s)\n", pt, rtcp_payload_type2str(pt));
6757 if (subtype) {
6758 ast_verbose("Packet Subtype: %u (%s)\n", rc, subtype);
6759 } else {
6760 ast_verbose("Reception reports: %u\n", rc);
6761 }
6762 ast_verbose("SSRC of sender: %u\n", ssrc);
6763 }
6764
6765 /* Determine the appropriate instance for this */
6766 if (ssrc_valid) {
6767 /*
6768 * Depending on the payload type, either the packet source or media source
6769 * SSRC is used.
6770 */
6771 if (use_packet_source) {
6772 child = rtp_find_instance_by_packet_source_ssrc(transport, transport_rtp, ssrc);
6773 } else {
6774 child = rtp_find_instance_by_media_source_ssrc(transport, transport_rtp, ssrc);
6775 }
6776 if (child && child != transport) {
6777 /*
6778 * It is safe to hold the child lock while holding the parent lock.
6779 * We guarantee that the locking order is always parent->child or
6780 * that the child lock is not held when acquiring the parent lock.
6781 */
6782 ao2_lock(child);
6783 instance = child;
6784 rtp = ast_rtp_instance_get_data(instance);
6785 } else {
6786 /* The child is the parent! We don't need to unlock it. */
6787 child = NULL;
6788 rtp = transport_rtp;
6789 }
6790 } else {
6791 child = NULL;
6792 rtp = transport_rtp;
6793 }
6794
6795 if (ssrc_valid && rtp->themssrc_valid) {
6796 /*
6797 * If the SSRC is 1, we still need to handle RTCP since this could be a
6798 * special case. For example, if we have a unidirectional video stream, the
6799 * SSRC may be set to 1 by the browser (in the case of chromium), and requests
6800 * will still need to be processed so that video can flow as expected. This
6801 * should only be done for PLI and FUR, since there is not a way to get the
6802 * appropriate rtp instance when the SSRC is 1.
6803 */
6804 int exception = (ssrc == 1 && !((pt == RTCP_PT_PSFB && rc == AST_RTP_RTCP_FMT_PLI) || pt == RTCP_PT_FUR));
6805 if ((ssrc != rtp->themssrc && use_packet_source && ssrc != 1)
6806 || exception) {
6807 /*
6808 * Skip over this RTCP record as it does not contain the
6809 * correct SSRC. We should not act upon RTCP records
6810 * for a different stream.
6811 */
6812 position += length;
6813 ast_debug_rtcp(1, "(%p) RTCP %p -- from %s: Skipping record, received SSRC '%u' != expected '%u'\n",
6814 instance, rtp, ast_sockaddr_stringify(addr), ssrc, rtp->themssrc);
6815 if (child) {
6816 ao2_unlock(child);
6817 }
6818 continue;
6819 }
6820 ssrc_seen = 1;
6821 }
6822
6823 if (ssrc_seen && ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_NAT)) {
6824 /* Send to whoever sent to us */
6825 if (ast_sockaddr_cmp(&rtp->rtcp->them, addr)) {
6826 ast_sockaddr_copy(&rtp->rtcp->them, addr);
6828 ast_debug(0, "(%p) RTCP NAT: Got RTCP from other end. Now sending to address %s\n",
6829 instance, ast_sockaddr_stringify(addr));
6830 }
6831 }
6832 }
6833
6834 i += RTCP_HEADER_SSRC_LENGTH; /* Advance past header and ssrc */
6835 switch (pt) {
6836 case RTCP_PT_SR:
6837 gettimeofday(&rtp->rtcp->rxlsr, NULL);
6838 rtp->rtcp->themrxlsr = ((ntohl(rtcpheader[i]) & 0x0000ffff) << 16) | ((ntohl(rtcpheader[i + 1]) & 0xffff0000) >> 16);
6839 rtp->rtcp->spc = ntohl(rtcpheader[i + 3]);
6840 rtp->rtcp->soc = ntohl(rtcpheader[i + 4]);
6841
6842 rtcp_report->type = RTCP_PT_SR;
6843 rtcp_report->sender_information.packet_count = rtp->rtcp->spc;
6844 rtcp_report->sender_information.octet_count = rtp->rtcp->soc;
6845 ntp2timeval((unsigned int)ntohl(rtcpheader[i]),
6846 (unsigned int)ntohl(rtcpheader[i + 1]),
6847 &rtcp_report->sender_information.ntp_timestamp);
6848 rtcp_report->sender_information.rtp_timestamp = ntohl(rtcpheader[i + 2]);
6849 if (rtcp_debug_test_addr(addr)) {
6850 ast_verbose("NTP timestamp: %u.%06u\n",
6851 (unsigned int)rtcp_report->sender_information.ntp_timestamp.tv_sec,
6852 (unsigned int)rtcp_report->sender_information.ntp_timestamp.tv_usec);
6853 ast_verbose("RTP timestamp: %u\n", rtcp_report->sender_information.rtp_timestamp);
6854 ast_verbose("SPC: %u\tSOC: %u\n",
6855 rtcp_report->sender_information.packet_count,
6856 rtcp_report->sender_information.octet_count);
6857 }
6859 /* Intentional fall through */
6860 case RTCP_PT_RR:
6861 if (rtcp_report->type != RTCP_PT_SR) {
6862 rtcp_report->type = RTCP_PT_RR;
6863 }
6864
6865 if (rc > 0) {
6866 /* Don't handle multiple reception reports (rc > 1) yet */
6867 report_block = ast_calloc(1, sizeof(*report_block));
6868 if (!report_block) {
6869 if (child) {
6870 ao2_unlock(child);
6871 }
6872 return &ast_null_frame;
6873 }
6874 rtcp_report->report_block[0] = report_block;
6875 report_block->source_ssrc = ntohl(rtcpheader[i]);
6876 report_block->lost_count.packets = ntohl(rtcpheader[i + 1]) & 0x00ffffff;
6877 report_block->lost_count.fraction = ((ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24);
6878 report_block->highest_seq_no = ntohl(rtcpheader[i + 2]);
6879 report_block->ia_jitter = ntohl(rtcpheader[i + 3]);
6880 report_block->lsr = ntohl(rtcpheader[i + 4]);
6881 report_block->dlsr = ntohl(rtcpheader[i + 5]);
6882 if (report_block->lsr) {
6883 int skewed = update_rtt_stats(rtp, report_block->lsr, report_block->dlsr);
6884 if (skewed && rtcp_debug_test_addr(addr)) {
6885 struct timeval now;
6886 unsigned int lsr_now, lsw, msw;
6887 gettimeofday(&now, NULL);
6888 timeval2ntp(now, &msw, &lsw);
6889 lsr_now = (((msw & 0xffff) << 16) | ((lsw & 0xffff0000) >> 16));
6890 ast_verbose("Internal RTCP NTP clock skew detected: "
6891 "lsr=%u, now=%u, dlsr=%u (%u:%03ums), "
6892 "diff=%u\n",
6893 report_block->lsr, lsr_now, report_block->dlsr, report_block->dlsr / 65536,
6894 (report_block->dlsr % 65536) * 1000 / 65536,
6895 report_block->dlsr - (lsr_now - report_block->lsr));
6896 }
6897 }
6898 update_jitter_stats(rtp, report_block->ia_jitter);
6899 update_lost_stats(rtp, report_block->lost_count.packets);
6900 /*
6901 * update_reported_mes_stats must be called AFTER
6902 * update_rtt_stats, update_jitter_stats and
6903 * update_lost_stats.
6904 */
6906
6907 if (rtcp_debug_test_addr(addr)) {
6908 int rate = ast_rtp_get_rate(rtp->f.subclass.format);
6909
6910 ast_verbose(" Fraction lost: %d\n", report_block->lost_count.fraction);
6911 ast_verbose(" Packets lost so far: %u\n", report_block->lost_count.packets);
6912 ast_verbose(" Highest sequence number: %u\n", report_block->highest_seq_no & 0x0000ffff);
6913 ast_verbose(" Sequence number cycles: %u\n", report_block->highest_seq_no >> 16);
6914 ast_verbose(" Interarrival jitter (samp): %u\n", report_block->ia_jitter);
6915 ast_verbose(" Interarrival jitter (secs): %.6f\n", ast_samp2sec(report_block->ia_jitter, rate));
6916 ast_verbose(" Last SR(our NTP): %lu.%010lu\n",(unsigned long)(report_block->lsr) >> 16,((unsigned long)(report_block->lsr) << 16) * 4096);
6917 ast_verbose(" DLSR: %4.4f (sec)\n",(double)report_block->dlsr / 65536.0);
6918 ast_verbose(" RTT: %4.4f(sec)\n", rtp->rtcp->rtt);
6919 ast_verbose(" MES: %4.1f\n", rtp->rtcp->reported_mes);
6920 }
6921 }
6922 /* If and when we handle more than one report block, this should occur outside
6923 * this loop.
6924 */
6925
6926 message_blob = ast_json_pack("{s: s, s: s, s: f, s: f}",
6927 "from", ast_sockaddr_stringify(addr),
6928 "to", transport_rtp->rtcp->local_addr_str,
6929 "rtt", rtp->rtcp->rtt,
6930 "mes", rtp->rtcp->reported_mes);
6932 rtcp_report,
6933 message_blob);
6934 ast_json_unref(message_blob);
6935
6936 /* Return an AST_FRAME_RTCP frame with the ast_rtp_rtcp_report
6937 * object as a its data */
6938 transport_rtp->f.frametype = AST_FRAME_RTCP;
6939 transport_rtp->f.subclass.integer = pt;
6940 transport_rtp->f.data.ptr = rtp->rtcp->frame_buf + AST_FRIENDLY_OFFSET;
6941 memcpy(transport_rtp->f.data.ptr, rtcp_report, sizeof(struct ast_rtp_rtcp_report));
6942 transport_rtp->f.datalen = sizeof(struct ast_rtp_rtcp_report);
6943 if (rc > 0) {
6944 /* There's always a single report block stored, here */
6945 struct ast_rtp_rtcp_report *rtcp_report2;
6946 report_block = transport_rtp->f.data.ptr + transport_rtp->f.datalen + sizeof(struct ast_rtp_rtcp_report_block *);
6947 memcpy(report_block, rtcp_report->report_block[0], sizeof(struct ast_rtp_rtcp_report_block));
6948 rtcp_report2 = (struct ast_rtp_rtcp_report *)transport_rtp->f.data.ptr;
6949 rtcp_report2->report_block[0] = report_block;
6950 transport_rtp->f.datalen += sizeof(struct ast_rtp_rtcp_report_block);
6951 }
6952 transport_rtp->f.offset = AST_FRIENDLY_OFFSET;
6953 transport_rtp->f.samples = 0;
6954 transport_rtp->f.mallocd = 0;
6955 transport_rtp->f.delivery.tv_sec = 0;
6956 transport_rtp->f.delivery.tv_usec = 0;
6957 transport_rtp->f.src = "RTP";
6958 transport_rtp->f.stream_num = rtp->stream_num;
6959 f = &transport_rtp->f;
6960 break;
6961 case AST_RTP_RTCP_RTPFB:
6962 switch (rc) {
6964 /* If retransmissions are not enabled ignore this message */
6965 if (!rtp->send_buffer) {
6966 break;
6967 }
6968
6969 if (rtcp_debug_test_addr(addr)) {
6970 ast_verbose("Received generic RTCP NACK message\n");
6971 }
6972
6973 ast_rtp_rtcp_handle_nack(instance, rtcpheader, position, length);
6974 break;
6975 default:
6976 break;
6977 }
6978 break;
6979 case RTCP_PT_FUR:
6980 /* Handle RTCP FUR as FIR by setting the format to 4 */
6982 case RTCP_PT_PSFB:
6983 switch (rc) {
6986 if (rtcp_debug_test_addr(addr)) {
6987 ast_verbose("Received an RTCP Fast Update Request\n");
6988 }
6989 transport_rtp->f.frametype = AST_FRAME_CONTROL;
6990 transport_rtp->f.subclass.integer = AST_CONTROL_VIDUPDATE;
6991 transport_rtp->f.datalen = 0;
6992 transport_rtp->f.samples = 0;
6993 transport_rtp->f.mallocd = 0;
6994 transport_rtp->f.src = "RTP";
6995 f = &transport_rtp->f;
6996 break;
6998 /* If REMB support is not enabled ignore this message */
7000 break;
7001 }
7002
7003 if (rtcp_debug_test_addr(addr)) {
7004 ast_verbose("Received REMB report\n");
7005 }
7006 transport_rtp->f.frametype = AST_FRAME_RTCP;
7007 transport_rtp->f.subclass.integer = pt;
7008 transport_rtp->f.stream_num = rtp->stream_num;
7009 transport_rtp->f.data.ptr = rtp->rtcp->frame_buf + AST_FRIENDLY_OFFSET;
7010 feedback = transport_rtp->f.data.ptr;
7011 feedback->fmt = rc;
7012
7013 /* We don't actually care about the SSRC information in the feedback message */
7014 first_word = ntohl(rtcpheader[i + 2]);
7015 feedback->remb.br_exp = (first_word >> 18) & ((1 << 6) - 1);
7016 feedback->remb.br_mantissa = first_word & ((1 << 18) - 1);
7017
7018 transport_rtp->f.datalen = sizeof(struct ast_rtp_rtcp_feedback);
7019 transport_rtp->f.offset = AST_FRIENDLY_OFFSET;
7020 transport_rtp->f.samples = 0;
7021 transport_rtp->f.mallocd = 0;
7022 transport_rtp->f.delivery.tv_sec = 0;
7023 transport_rtp->f.delivery.tv_usec = 0;
7024 transport_rtp->f.src = "RTP";
7025 f = &transport_rtp->f;
7026 break;
7027 default:
7028 break;
7029 }
7030 break;
7031 case RTCP_PT_SDES:
7032 if (rtcp_debug_test_addr(addr)) {
7033 ast_verbose("Received an SDES from %s\n",
7035 }
7036#ifdef TEST_FRAMEWORK
7037 if ((test_engine = ast_rtp_instance_get_test(instance))) {
7038 test_engine->sdes_received = 1;
7039 }
7040#endif
7041 break;
7042 case RTCP_PT_BYE:
7043 if (rtcp_debug_test_addr(addr)) {
7044 ast_verbose("Received a BYE from %s\n",
7046 }
7047 break;
7048 default:
7049 break;
7050 }
7051 position += length;
7052 rtp->rtcp->rtcp_info = 1;
7053
7054 if (child) {
7055 ao2_unlock(child);
7056 }
7057 }
7058
7059 return f;
7060}
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
struct stasis_message_type * ast_rtp_rtcp_received_type(void)
Message type for an RTCP message received from some external source.
#define AST_FRIENDLY_OFFSET
Offset into a frame's data buffer.
@ AST_FRAME_RTCP
@ AST_FRAME_CONTROL
@ AST_CONTROL_VIDUPDATE
struct ast_frame ast_null_frame
Definition: main/frame.c:79
#define ast_debug(level,...)
Log a DEBUG message.
#define RTCP_LENGTH_SHIFT
static int ast_rtp_rtcp_handle_nack(struct ast_rtp_instance *instance, unsigned int *nackdata, unsigned int position, unsigned int length)
#define RTCP_PAYLOAD_TYPE_SHIFT
static void ntp2timeval(unsigned int msw, unsigned int lsw, struct timeval *tv)
#define RTCP_VALID_VALUE
#define RTCP_FB_NACK_BLOCK_WORD_LENGTH
#define RTCP_RR_BLOCK_WORD_LENGTH
static const char * rtcp_payload_subtype2str(unsigned int pt, unsigned int subtype)
#define RTCP_SR_BLOCK_WORD_LENGTH
static struct ast_rtp_instance * rtp_find_instance_by_packet_source_ssrc(struct ast_rtp_instance *instance, struct ast_rtp *rtp, unsigned int ssrc)
#define RTCP_REPORT_COUNT_SHIFT
#define RTCP_PT_FUR
static const char * rtcp_payload_type2str(unsigned int pt)
#define RTCP_PT_BYE
#define RTCP_HEADER_SSRC_LENGTH
#define RTCP_VALID_MASK
static int srtp_replay_protection
static void update_jitter_stats(struct ast_rtp *rtp, unsigned int ia_jitter)
#define RTCP_FB_REMB_BLOCK_WORD_LENGTH
#define RTCP_PT_PSFB
static int update_rtt_stats(struct ast_rtp *rtp, unsigned int lsr, unsigned int dlsr)
static struct ast_rtp_instance * rtp_find_instance_by_media_source_ssrc(struct ast_rtp_instance *instance, struct ast_rtp *rtp, unsigned int ssrc)
#define RTCP_VERSION_SHIFTED
#define RTCP_REPORT_COUNT_MASK
#define RTCP_PAYLOAD_TYPE_MASK
#define RTCP_VERSION_MASK_SHIFTED
static void update_reported_mes_stats(struct ast_rtp *rtp)
static void update_lost_stats(struct ast_rtp *rtp, unsigned int lost_packets)
#define RTCP_LENGTH_MASK
#define AST_RTP_RTCP_FMT_FIR
Definition: rtp_engine.h:332
struct ast_rtp_rtcp_report * ast_rtp_rtcp_report_alloc(unsigned int report_blocks)
Allocate an ao2 ref counted instance of ast_rtp_rtcp_report.
Definition: rtp_engine.c:3516
int ast_rtp_instance_get_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property)
Get the value of an RTP instance property.
Definition: rtp_engine.c:737
#define ast_debug_rtp_packet_is_allowed
Definition: rtp_engine.h:2914
#define AST_RTP_RTCP_FMT_REMB
Definition: rtp_engine.h:334
@ AST_RTP_PROPERTY_NAT
Definition: rtp_engine.h:115
@ AST_RTP_PROPERTY_REMB
Definition: rtp_engine.h:131
#define AST_RTP_RTCP_FMT_PLI
Definition: rtp_engine.h:330
Data structure associated with a single frame of data.
union ast_frame::@226 data
struct timeval delivery
enum ast_frame_type frametype
const char * src
double reported_mes
unsigned char frame_buf[512+AST_FRIENDLY_OFFSET]
unsigned int soc
unsigned int spc
An object that represents data received in a feedback report.
Definition: rtp_engine.h:383
unsigned int fmt
Definition: rtp_engine.h:384
struct ast_rtp_rtcp_feedback_remb remb
Definition: rtp_engine.h:386
An object that represents data sent during a SR/RR RTCP report.
Definition: rtp_engine.h:356
enum strict_rtp_state strict_rtp_state
struct ast_data_buffer * send_buffer
int(* unprotect)(struct ast_srtp *srtp, void *buf, int *size, int rtcp)
Definition: res_srtp.h:48

References ao2_cleanup, ao2_lock, ao2_unlock, ast_calloc, AST_CONTROL_VIDUPDATE, ast_debug, ast_debug_rtcp, ast_debug_rtp_packet_is_allowed, AST_FRAME_CONTROL, AST_FRAME_RTCP, AST_FRIENDLY_OFFSET, ast_json_pack(), ast_json_unref(), ast_null_frame, ast_rtp_get_rate(), ast_rtp_instance_get_channel_id(), ast_rtp_instance_get_data(), ast_rtp_instance_get_prop(), AST_RTP_PROPERTY_NAT, AST_RTP_PROPERTY_REMB, ast_rtp_publish_rtcp_message(), AST_RTP_RTCP_FMT_FIR, AST_RTP_RTCP_FMT_NACK, AST_RTP_RTCP_FMT_PLI, AST_RTP_RTCP_FMT_REMB, ast_rtp_rtcp_handle_nack(), ast_rtp_rtcp_received_type(), ast_rtp_rtcp_report_alloc(), AST_RTP_RTCP_RTPFB, ast_samp2sec(), ast_sockaddr_cmp(), ast_sockaddr_copy(), ast_sockaddr_stringify(), ast_verbose(), ast_rtp_rtcp_feedback_remb::br_exp, ast_rtp_rtcp_feedback_remb::br_mantissa, ast_frame::data, ast_frame::datalen, ast_frame::delivery, ast_rtp_rtcp_report_block::dlsr, ast_rtp::f, ast_rtp_rtcp_feedback::fmt, ast_frame_subclass::format, ast_rtp_rtcp_report_block::fraction, ast_rtcp::frame_buf, ast_frame::frametype, ast_rtp_rtcp_report_block::highest_seq_no, ast_rtp_rtcp_report_block::ia_jitter, ast_frame_subclass::integer, len(), ast_rtcp::local_addr_str, ast_rtp_rtcp_report_block::lost_count, ast_rtp_rtcp_report_block::lsr, ast_frame::mallocd, ntp2timeval(), NULL, ast_frame::offset, ast_rtp_rtcp_report_block::packets, ast_frame::ptr, RAII_VAR, ast_rtp_rtcp_feedback::remb, ast_rtp_rtcp_report::report_block, ast_rtcp::reported_mes, res_srtp, ast_rtp::rtcp, rtcp_debug_test_addr(), RTCP_FB_NACK_BLOCK_WORD_LENGTH, RTCP_FB_REMB_BLOCK_WORD_LENGTH, RTCP_HEADER_SSRC_LENGTH, ast_rtcp::rtcp_info, RTCP_LENGTH_MASK, RTCP_LENGTH_SHIFT, rtcp_payload_subtype2str(), rtcp_payload_type2str(), RTCP_PAYLOAD_TYPE_MASK, RTCP_PAYLOAD_TYPE_SHIFT, RTCP_PT_BYE, RTCP_PT_FUR, RTCP_PT_PSFB, RTCP_PT_RR, RTCP_PT_SDES, RTCP_PT_SR, RTCP_REPORT_COUNT_MASK, RTCP_REPORT_COUNT_SHIFT, RTCP_RR_BLOCK_WORD_LENGTH, RTCP_SR_BLOCK_WORD_LENGTH, RTCP_VALID_MASK, RTCP_VALID_VALUE, RTCP_VERSION_MASK_SHIFTED, RTCP_VERSION_SHIFTED, rtp_find_instance_by_media_source_ssrc(), rtp_find_instance_by_packet_source_ssrc(), ast_rtcp::rtt, ast_rtcp::rxlsr, ast_frame::samples, ast_rtp::send_buffer, ast_rtcp::soc, ast_rtp_rtcp_report_block::source_ssrc, ast_rtcp::spc, ast_frame::src, srtp_replay_protection, ast_frame::stream_num, ast_rtp::stream_num, STRICT_RTP_OPEN, ast_rtp::strict_rtp_state, ast_frame::subclass, ast_rtcp::them, ast_rtcp::themrxlsr, ast_rtp::themssrc, ast_rtp::themssrc_valid, timeval2ntp(), ast_srtp_res::unprotect, update_jitter_stats(), update_lost_stats(), update_reported_mes_stats(), and update_rtt_stats().

Referenced by ast_rtcp_read(), and ast_rtp_read().

◆ ast_rtcp_read()

static struct ast_frame * ast_rtcp_read ( struct ast_rtp_instance instance)
static
Precondition
instance is locked

Definition at line 7063 of file res_rtp_asterisk.c.

7064{
7065 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
7066 struct ast_srtp *srtp = ast_rtp_instance_get_srtp(instance, 1);
7067 struct ast_sockaddr addr;
7068 unsigned char rtcpdata[8192 + AST_FRIENDLY_OFFSET];
7069 unsigned char *read_area = rtcpdata + AST_FRIENDLY_OFFSET;
7070 size_t read_area_size = sizeof(rtcpdata) - AST_FRIENDLY_OFFSET;
7071 int res;
7072
7073 /* Read in RTCP data from the socket */
7074 if ((res = rtcp_recvfrom(instance, read_area, read_area_size,
7075 0, &addr)) < 0) {
7076 if (res == RTP_DTLS_ESTABLISHED) {
7079 return &rtp->f;
7080 }
7081
7082 ast_assert(errno != EBADF);
7083 if (errno != EAGAIN) {
7084 ast_log(LOG_WARNING, "RTCP Read error: %s. Hanging up.\n",
7085 (errno) ? strerror(errno) : "Unspecified");
7086 return NULL;
7087 }
7088 return &ast_null_frame;
7089 }
7090
7091 /* If this was handled by the ICE session don't do anything further */
7092 if (!res) {
7093 return &ast_null_frame;
7094 }
7095
7096 if (!*read_area) {
7097 struct sockaddr_in addr_tmp;
7098 struct ast_sockaddr addr_v4;
7099
7100 if (ast_sockaddr_is_ipv4(&addr)) {
7101 ast_sockaddr_to_sin(&addr, &addr_tmp);
7102 } else if (ast_sockaddr_ipv4_mapped(&addr, &addr_v4)) {
7103 ast_debug_stun(2, "(%p) STUN using IPv6 mapped address %s\n",
7104 instance, ast_sockaddr_stringify(&addr));
7105 ast_sockaddr_to_sin(&addr_v4, &addr_tmp);
7106 } else {
7107 ast_debug_stun(2, "(%p) STUN cannot do for non IPv4 address %s\n",
7108 instance, ast_sockaddr_stringify(&addr));
7109 return &ast_null_frame;
7110 }
7111 if ((ast_stun_handle_packet(rtp->rtcp->s, &addr_tmp, read_area, res, NULL, NULL) == AST_STUN_ACCEPT)) {
7112 ast_sockaddr_from_sin(&addr, &addr_tmp);
7113 ast_sockaddr_copy(&rtp->rtcp->them, &addr);
7114 }
7115 return &ast_null_frame;
7116 }
7117
7118 return ast_rtcp_interpret(instance, srtp, read_area, res, &addr);
7119}
@ AST_CONTROL_SRCCHANGE
int errno
int ast_sockaddr_ipv4_mapped(const struct ast_sockaddr *addr, struct ast_sockaddr *ast_mapped)
Convert an IPv4-mapped IPv6 address into an IPv4 address.
Definition: netsock2.c:37
#define ast_sockaddr_to_sin(addr, sin)
Converts a struct ast_sockaddr to a struct sockaddr_in.
Definition: netsock2.h:765
#define ast_sockaddr_from_sin(addr, sin)
Converts a struct sockaddr_in to a struct ast_sockaddr.
Definition: netsock2.h:778
int ast_sockaddr_is_ipv4(const struct ast_sockaddr *addr)
Determine if the address is an IPv4 address.
Definition: netsock2.c:497
static struct ast_frame * ast_rtcp_interpret(struct ast_rtp_instance *instance, struct ast_srtp *srtp, const unsigned char *rtcpdata, size_t size, struct ast_sockaddr *addr)
static int rtcp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa)
int ast_stun_handle_packet(int s, struct sockaddr_in *src, unsigned char *data, size_t len, stun_cb_f *stun_cb, void *arg)
handle an incoming STUN message.
Definition: stun.c:293
#define ast_debug_stun(sublevel,...)
Log debug level STUN information.
Definition: stun.h:54
@ AST_STUN_ACCEPT
Definition: stun.h:65
#define ast_assert(a)
Definition: utils.h:739

References ast_assert, AST_CONTROL_SRCCHANGE, ast_debug_stun, AST_FRAME_CONTROL, AST_FRIENDLY_OFFSET, ast_log, ast_null_frame, ast_rtcp_interpret(), ast_rtp_instance_get_data(), ast_rtp_instance_get_srtp(), ast_sockaddr_copy(), ast_sockaddr_from_sin, ast_sockaddr_ipv4_mapped(), ast_sockaddr_is_ipv4(), ast_sockaddr_stringify(), ast_sockaddr_to_sin, AST_STUN_ACCEPT, ast_stun_handle_packet(), errno, ast_rtp::f, ast_frame::frametype, ast_frame_subclass::integer, LOG_WARNING, NULL, ast_rtp::rtcp, rtcp_recvfrom(), RTP_DTLS_ESTABLISHED, ast_rtcp::s, ast_frame::subclass, and ast_rtcp::them.

Referenced by ast_rtp_read().

◆ ast_rtcp_write()

static int ast_rtcp_write ( const void *  data)
static

Write a RTCP packet to the far end.

Note
Decide if we are going to send an SR (with Reception Block) or RR RR is sent if we have not sent any rtp packets in the previous interval

Scheduler callback

Definition at line 4974 of file res_rtp_asterisk.c.

4975{
4976 struct ast_rtp_instance *instance = (struct ast_rtp_instance *) data;
4977 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4978 int res;
4979 int sr = 0;
4980 int packet_len = 0;
4981 int ice;
4982 struct ast_sockaddr remote_address = { { 0, } };
4983 unsigned char *rtcpheader;
4984 unsigned char bdata[AST_UUID_STR_LEN + 128] = ""; /* More than enough */
4985 RAII_VAR(struct ast_rtp_rtcp_report *, rtcp_report, NULL, ao2_cleanup);
4986
4987 if (!rtp || !rtp->rtcp || rtp->rtcp->schedid == -1) {
4988 ao2_ref(instance, -1);
4989 return 0;
4990 }
4991
4992 ao2_lock(instance);
4993 rtcpheader = bdata;
4994 rtcp_report = ast_rtp_rtcp_report_alloc(rtp->themssrc_valid ? 1 : 0);
4995 res = ast_rtcp_generate_compound_prefix(instance, rtcpheader, rtcp_report, &sr);
4996
4997 if (res == 0 || res == 1) {
4998 goto cleanup;
4999 }
5000
5001 packet_len += res;
5002
5003 if (rtp->bundled) {
5004 ast_rtp_instance_get_remote_address(instance, &remote_address);
5005 } else {
5006 ast_sockaddr_copy(&remote_address, &rtp->rtcp->them);
5007 }
5008
5009 res = rtcp_sendto(instance, (unsigned int *)rtcpheader, packet_len, 0, &remote_address, &ice);
5010 if (res < 0) {
5011 ast_log(LOG_ERROR, "RTCP %s transmission error to %s, rtcp halted %s\n",
5012 sr ? "SR" : "RR",
5014 strerror(errno));
5015 res = 0;
5016 } else {
5017 ast_rtcp_calculate_sr_rr_statistics(instance, rtcp_report, remote_address, ice, sr);
5018 }
5019
5020cleanup:
5021 ao2_unlock(instance);
5022
5023 if (!res) {
5024 /*
5025 * Not being rescheduled.
5026 */
5027 rtp->rtcp->schedid = -1;
5028 ao2_ref(instance, -1);
5029 }
5030
5031 return res;
5032}
static void * cleanup(void *unused)
Definition: pbx_realtime.c:124
static int ast_rtcp_generate_compound_prefix(struct ast_rtp_instance *instance, unsigned char *rtcpheader, struct ast_rtp_rtcp_report *report, int *sr)
static int rtcp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int *ice)
static int ast_rtcp_calculate_sr_rr_statistics(struct ast_rtp_instance *instance, struct ast_rtp_rtcp_report *rtcp_report, struct ast_sockaddr remote_address, int ice, int sr)

References ao2_cleanup, ao2_lock, ao2_ref, ao2_unlock, ast_log, ast_rtcp_calculate_sr_rr_statistics(), ast_rtcp_generate_compound_prefix(), ast_rtp_instance_get_data(), ast_rtp_instance_get_remote_address, ast_rtp_rtcp_report_alloc(), ast_sockaddr_copy(), ast_sockaddr_stringify(), AST_UUID_STR_LEN, ast_rtp::bundled, cleanup(), ast_rtp_instance::data, errno, LOG_ERROR, NULL, RAII_VAR, ast_rtp::rtcp, rtcp_sendto(), ast_rtcp::schedid, ast_rtcp::them, and ast_rtp::themssrc_valid.

Referenced by ast_rtp_interpret(), and rtp_raw_write().

◆ ast_rtp_bundle()

static int ast_rtp_bundle ( struct ast_rtp_instance child,
struct ast_rtp_instance parent 
)
static
Precondition
child is locked

Definition at line 9432 of file res_rtp_asterisk.c.

9433{
9434 struct ast_rtp *child_rtp = ast_rtp_instance_get_data(child);
9435 struct ast_rtp *parent_rtp;
9436 struct rtp_ssrc_mapping mapping;
9437 struct ast_sockaddr them = { { 0, } };
9438
9439 if (child_rtp->bundled == parent) {
9440 return 0;
9441 }
9442
9443 /* If this instance was already bundled then remove the SSRC mapping */
9444 if (child_rtp->bundled) {
9445 struct ast_rtp *bundled_rtp;
9446
9447 ao2_unlock(child);
9448
9449 /* The child lock can't be held while accessing the parent */
9450 ao2_lock(child_rtp->bundled);
9451 bundled_rtp = ast_rtp_instance_get_data(child_rtp->bundled);
9453 ao2_unlock(child_rtp->bundled);
9454
9455 ao2_lock(child);
9456 ao2_ref(child_rtp->bundled, -1);
9457 child_rtp->bundled = NULL;
9458 }
9459
9460 if (!parent) {
9461 /* We transitioned away from bundle so we need our own transport resources once again */
9462 rtp_allocate_transport(child, child_rtp);
9463 return 0;
9464 }
9465
9466 parent_rtp = ast_rtp_instance_get_data(parent);
9467
9468 /* We no longer need any transport related resources as we will use our parent RTP instance instead */
9469 rtp_deallocate_transport(child, child_rtp);
9470
9471 /* Children maintain a reference to the parent to guarantee that the transport doesn't go away on them */
9472 child_rtp->bundled = ao2_bump(parent);
9473
9474 mapping.ssrc = child_rtp->themssrc;
9475 mapping.ssrc_valid = child_rtp->themssrc_valid;
9476 mapping.instance = child;
9477
9478 ao2_unlock(child);
9479
9480 ao2_lock(parent);
9481
9482 AST_VECTOR_APPEND(&parent_rtp->ssrc_mapping, mapping);
9483
9484#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
9485 /* If DTLS-SRTP is already in use then add the local SSRC to it, otherwise it will get added once DTLS
9486 * negotiation has been completed.
9487 */
9488 if (parent_rtp->dtls.connection == AST_RTP_DTLS_CONNECTION_EXISTING) {
9489 dtls_srtp_add_local_ssrc(parent_rtp, parent, 0, child_rtp->ssrc, 0);
9490 }
9491#endif
9492
9493 /* Bundle requires that RTCP-MUX be in use so only the main remote address needs to match */
9495
9496 ao2_unlock(parent);
9497
9498 ao2_lock(child);
9499
9501
9502 return 0;
9503}
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
static void rtp_deallocate_transport(struct ast_rtp_instance *instance, struct ast_rtp *rtp)
static int rtp_allocate_transport(struct ast_rtp_instance *instance, struct ast_rtp *rtp)
#define SSRC_MAPPING_ELEM_CMP(elem, value)
SSRC mapping comparator for AST_VECTOR_REMOVE_CMP_UNORDERED()
#define AST_VECTOR_ELEM_CLEANUP_NOOP(elem)
Vector element cleanup that does nothing.
Definition: vector.h:571
#define AST_VECTOR_REMOVE_CMP_UNORDERED(vec, value, cmp, cleanup)
Remove an element from a vector that matches the given comparison.
Definition: vector.h:488
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Definition: vector.h:256

References ao2_bump, ao2_lock, ao2_ref, ao2_unlock, AST_RTP_DTLS_CONNECTION_EXISTING, ast_rtp_instance_get_data(), ast_rtp_instance_get_remote_address, ast_rtp_instance_set_remote_address, AST_VECTOR_APPEND, AST_VECTOR_ELEM_CLEANUP_NOOP, AST_VECTOR_REMOVE_CMP_UNORDERED, ast_rtp::bundled, rtp_ssrc_mapping::instance, NULL, rtp_allocate_transport(), rtp_deallocate_transport(), rtp_ssrc_mapping::ssrc, ast_rtp::ssrc, ast_rtp::ssrc_mapping, SSRC_MAPPING_ELEM_CMP, rtp_ssrc_mapping::ssrc_valid, ast_rtp::themssrc, and ast_rtp::themssrc_valid.

◆ ast_rtp_change_source()

static void ast_rtp_change_source ( struct ast_rtp_instance instance)
static
Precondition
instance is locked

Definition at line 4511 of file res_rtp_asterisk.c.

4512{
4513 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4514 struct ast_srtp *srtp = ast_rtp_instance_get_srtp(instance, 0);
4515 struct ast_srtp *rtcp_srtp = ast_rtp_instance_get_srtp(instance, 1);
4516 unsigned int ssrc = ast_random();
4517
4518 if (rtp->lastts) {
4519 /* We simply set this bit so that the next packet sent will have the marker bit turned on */
4521 }
4522
4523 ast_debug_rtp(3, "(%p) RTP changing ssrc from %u to %u due to a source change\n",
4524 instance, rtp->ssrc, ssrc);
4525
4526 if (srtp) {
4527 ast_debug_rtp(3, "(%p) RTP changing ssrc for SRTP from %u to %u\n",
4528 instance, rtp->ssrc, ssrc);
4529 res_srtp->change_source(srtp, rtp->ssrc, ssrc);
4530 if (rtcp_srtp != srtp) {
4531 res_srtp->change_source(rtcp_srtp, rtp->ssrc, ssrc);
4532 }
4533 }
4534
4535 rtp->ssrc = ssrc;
4536
4537 /* Since the source is changing, we don't know what sequence number to expect next */
4538 rtp->expectedrxseqno = -1;
4539
4540 return;
4541}
#define FLAG_NEED_MARKER_BIT
#define ast_debug_rtp(sublevel,...)
Log debug level RTP information.
Definition: rtp_engine.h:2906
int(* change_source)(struct ast_srtp *srtp, unsigned int from_ssrc, unsigned int to_ssrc)
Definition: res_srtp.h:44
struct ast_rtp_instance * rtp
Definition: res_srtp.c:67
long int ast_random(void)
Definition: utils.c:2312
#define ast_set_flag(p, flag)
Definition: utils.h:70

References ast_debug_rtp, ast_random(), ast_rtp_instance_get_data(), ast_rtp_instance_get_srtp(), ast_set_flag, ast_srtp_res::change_source, FLAG_NEED_MARKER_BIT, res_srtp, and ast_srtp::rtp.

◆ ast_rtp_destroy()

static int ast_rtp_destroy ( struct ast_rtp_instance instance)
static
Precondition
instance is locked

Definition at line 4195 of file res_rtp_asterisk.c.

4196{
4197 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4198
4199 if (rtp->bundled) {
4200 struct ast_rtp *bundled_rtp;
4201
4202 /* We can't hold our instance lock while removing ourselves from the parent */
4203 ao2_unlock(instance);
4204
4205 ao2_lock(rtp->bundled);
4206 bundled_rtp = ast_rtp_instance_get_data(rtp->bundled);
4208 ao2_unlock(rtp->bundled);
4209
4210 ao2_lock(instance);
4211 ao2_ref(rtp->bundled, -1);
4212 }
4213
4214 rtp_deallocate_transport(instance, rtp);
4215
4216 /* Destroy the smoother that was smoothing out audio if present */
4217 if (rtp->smoother) {
4219 }
4220
4221 /* Destroy RTCP if it was being used */
4222 if (rtp->rtcp) {
4223 /*
4224 * It is not possible for there to be an active RTCP scheduler
4225 * entry at this point since it holds a reference to the
4226 * RTP instance while it's active.
4227 */
4229 ast_free(rtp->rtcp);
4230 }
4231
4232 /* Destroy RED if it was being used */
4233 if (rtp->red) {
4234 ao2_unlock(instance);
4235 AST_SCHED_DEL(rtp->sched, rtp->red->schedid);
4236 ao2_lock(instance);
4237 ast_free(rtp->red);
4238 rtp->red = NULL;
4239 }
4240
4241 /* Destroy the send buffer if it was being used */
4242 if (rtp->send_buffer) {
4244 }
4245
4246 /* Destroy the recv buffer if it was being used */
4247 if (rtp->recv_buffer) {
4249 }
4250
4252
4258
4259 /* Finally destroy ourselves */
4260 rtp->owner = NULL;
4261 ast_free(rtp);
4262
4263 return 0;
4264}
#define ast_free(a)
Definition: astmm.h:180
void ast_data_buffer_free(struct ast_data_buffer *buffer)
Free a data buffer (and all held data payloads)
Definition: data_buffer.c:338
#define AST_SCHED_DEL(sched, id)
Remove a scheduler entry.
Definition: sched.h:46
void ast_smoother_free(struct ast_smoother *s)
Definition: smoother.c:220
struct ast_format * lasttxformat
struct rtp_transport_wide_cc_statistics transport_wide_cc
struct ast_smoother * smoother
struct ast_sched_context * sched
struct ast_data_buffer * recv_buffer
struct ast_rtp_instance * owner
The RTP instance owning us (used for debugging purposes) We don't hold a reference to the instance be...
struct rtp_red * red
struct ast_format * lastrxformat
struct rtp_transport_wide_cc_statistics::@470 packet_statistics
#define AST_VECTOR_FREE(vec)
Deallocates this vector.
Definition: vector.h:174

References ao2_cleanup, ao2_lock, ao2_ref, ao2_unlock, ast_data_buffer_free(), ast_free, ast_rtp_instance_get_data(), AST_SCHED_DEL, ast_smoother_free(), AST_VECTOR_ELEM_CLEANUP_NOOP, AST_VECTOR_FREE, AST_VECTOR_REMOVE_CMP_UNORDERED, ast_rtp::bundled, ast_rtp::f, ast_frame_subclass::format, ast_rtp::lastrxformat, ast_rtp::lasttxformat, ast_rtcp::local_addr_str, ast_rtp::missing_seqno, NULL, ast_rtp::owner, rtp_transport_wide_cc_statistics::packet_statistics, ast_rtp::recv_buffer, ast_rtp::red, ast_rtp::rtcp, rtp_deallocate_transport(), ast_rtp::sched, rtp_red::schedid, ast_rtp::send_buffer, ast_rtp::smoother, ast_rtp::ssrc_mapping, SSRC_MAPPING_ELEM_CMP, ast_frame::subclass, and ast_rtp::transport_wide_cc.

◆ ast_rtp_dtmf_begin()

static int ast_rtp_dtmf_begin ( struct ast_rtp_instance instance,
char  digit 
)
static
Precondition
instance is locked

Definition at line 4282 of file res_rtp_asterisk.c.

4283{
4284 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4285 struct ast_sockaddr remote_address = { {0,} };
4286 int hdrlen = 12, res = 0, i = 0, payload = 101;
4287 char data[256];
4288 unsigned int *rtpheader = (unsigned int*)data;
4289
4290 ast_rtp_instance_get_remote_address(instance, &remote_address);
4291
4292 /* If we have no remote address information bail out now */
4293 if (ast_sockaddr_isnull(&remote_address)) {
4294 return -1;
4295 }
4296
4297 /* Convert given digit into what we want to transmit */
4298 if ((digit <= '9') && (digit >= '0')) {
4299 digit -= '0';
4300 } else if (digit == '*') {
4301 digit = 10;
4302 } else if (digit == '#') {
4303 digit = 11;
4304 } else if ((digit >= 'A') && (digit <= 'D')) {
4305 digit = digit - 'A' + 12;
4306 } else if ((digit >= 'a') && (digit <= 'd')) {
4307 digit = digit - 'a' + 12;
4308 } else {
4309 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
4310 return -1;
4311 }
4312
4313 /* Grab the payload that they expect the RFC2833 packet to be received in */
4315
4316 rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
4317 rtp->send_duration = 160;
4319 rtp->lastdigitts = rtp->lastts + rtp->send_duration;
4320
4321 /* Create the actual packet that we will be sending */
4322 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno));
4323 rtpheader[1] = htonl(rtp->lastdigitts);
4324 rtpheader[2] = htonl(rtp->ssrc);
4325
4326 /* Actually send the packet */
4327 for (i = 0; i < 2; i++) {
4328 int ice;
4329
4330 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
4331 res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
4332 if (res < 0) {
4333 ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n",
4334 ast_sockaddr_stringify(&remote_address),
4335 strerror(errno));
4336 }
4337 if (rtp_debug_test_addr(&remote_address)) {
4338 ast_verbose("Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n",
4339 ast_sockaddr_stringify(&remote_address),
4340 ice ? " (via ICE)" : "",
4341 payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
4342 }
4343 rtp->seqno++;
4344 rtp->send_duration += 160;
4345 rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno));
4346 }
4347
4348 /* Record that we are in the process of sending a digit and information needed to continue doing so */
4349 rtp->sending_digit = 1;
4350 rtp->send_digit = digit;
4351 rtp->send_payload = payload;
4352
4353 return 0;
4354}
char digit
#define DTMF_SAMPLE_RATE_MS
static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery)
static int rtp_debug_test_addr(struct ast_sockaddr *addr)
static int rtp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int *ice)
#define AST_RTP_DTMF
Definition: rtp_engine.h:291
int ast_rtp_codecs_payload_code_tx(struct ast_rtp_codecs *codecs, int asterisk_format, const struct ast_format *format, int code)
Retrieve a tx mapped payload type based on whether it is an Asterisk format and the code.
Definition: rtp_engine.c:1941
struct ast_rtp_codecs * ast_rtp_instance_get_codecs(struct ast_rtp_instance *instance)
Get the codecs structure of an RTP instance.
Definition: rtp_engine.c:748
unsigned short seqno
struct timeval dtmfmute
unsigned int lastdigitts
char sending_digit
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
Definition: extconf.c:2282
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
Definition: time.h:235

References ast_log, ast_rtp_codecs_payload_code_tx(), AST_RTP_DTMF, ast_rtp_instance_get_codecs(), ast_rtp_instance_get_data(), ast_rtp_instance_get_remote_address, ast_sockaddr_isnull(), ast_sockaddr_stringify(), ast_tv(), ast_tvadd(), ast_tvnow(), ast_verbose(), calc_txstamp(), digit, DTMF_SAMPLE_RATE_MS, ast_rtp::dtmfmute, errno, ast_rtp::lastdigitts, ast_rtp::lastts, LOG_ERROR, LOG_WARNING, NULL, rtp_debug_test_addr(), rtp_sendto(), ast_rtp::send_digit, ast_rtp::send_duration, ast_rtp::send_payload, ast_rtp::sending_digit, ast_rtp::seqno, and ast_rtp::ssrc.

◆ ast_rtp_dtmf_compatible()

static int ast_rtp_dtmf_compatible ( struct ast_channel chan0,
struct ast_rtp_instance instance0,
struct ast_channel chan1,
struct ast_rtp_instance instance1 
)
static
Precondition
Neither instance0 nor instance1 are locked

Definition at line 9215 of file res_rtp_asterisk.c.

9216{
9217 /* If both sides are not using the same method of DTMF transmission
9218 * (ie: one is RFC2833, other is INFO... then we can not do direct media.
9219 * --------------------------------------------------
9220 * | DTMF Mode | HAS_DTMF | Accepts Begin Frames |
9221 * |-----------|------------|-----------------------|
9222 * | Inband | False | True |
9223 * | RFC2833 | True | True |
9224 * | SIP INFO | False | False |
9225 * --------------------------------------------------
9226 */
9228 (!ast_channel_tech(chan0)->send_digit_begin != !ast_channel_tech(chan1)->send_digit_begin)) ? 0 : 1);
9229}
const struct ast_channel_tech * ast_channel_tech(const struct ast_channel *chan)
@ AST_RTP_PROPERTY_DTMF
Definition: rtp_engine.h:117

References ast_channel_tech(), ast_rtp_instance_get_prop(), and AST_RTP_PROPERTY_DTMF.

◆ ast_rtp_dtmf_continuation()

static int ast_rtp_dtmf_continuation ( struct ast_rtp_instance instance)
static
Precondition
instance is locked

Definition at line 4357 of file res_rtp_asterisk.c.

4358{
4359 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4360 struct ast_sockaddr remote_address = { {0,} };
4361 int hdrlen = 12, res = 0;
4362 char data[256];
4363 unsigned int *rtpheader = (unsigned int*)data;
4364 int ice;
4365
4366 ast_rtp_instance_get_remote_address(instance, &remote_address);
4367
4368 /* Make sure we know where the other side is so we can send them the packet */
4369 if (ast_sockaddr_isnull(&remote_address)) {
4370 return -1;
4371 }
4372
4373 /* Actually create the packet we will be sending */
4374 rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
4375 rtpheader[1] = htonl(rtp->lastdigitts);
4376 rtpheader[2] = htonl(rtp->ssrc);
4377 rtpheader[3] = htonl((rtp->send_digit << 24) | (0xa << 16) | (rtp->send_duration));
4378
4379 /* Boom, send it on out */
4380 res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
4381 if (res < 0) {
4382 ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n",
4383 ast_sockaddr_stringify(&remote_address),
4384 strerror(errno));
4385 }
4386
4387 if (rtp_debug_test_addr(&remote_address)) {
4388 ast_verbose("Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n",
4389 ast_sockaddr_stringify(&remote_address),
4390 ice ? " (via ICE)" : "",
4391 rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
4392 }
4393
4394 /* And now we increment some values for the next time we swing by */
4395 rtp->seqno++;
4396 rtp->send_duration += 160;
4398
4399 return 0;
4400}

References ast_log, ast_rtp_instance_get_data(), ast_rtp_instance_get_remote_address, ast_sockaddr_isnull(), ast_sockaddr_stringify(), ast_verbose(), calc_txstamp(), DTMF_SAMPLE_RATE_MS, errno, ast_rtp::lastdigitts, ast_rtp::lastts, LOG_ERROR, NULL, rtp_debug_test_addr(), rtp_sendto(), ast_rtp::send_digit, ast_rtp::send_duration, ast_rtp::send_payload, ast_rtp::seqno, and ast_rtp::ssrc.

Referenced by ast_rtp_interpret().

◆ ast_rtp_dtmf_end()

static int ast_rtp_dtmf_end ( struct ast_rtp_instance instance,
char  digit 
)
static
Precondition
instance is locked

Definition at line 4493 of file res_rtp_asterisk.c.

4494{
4495 return ast_rtp_dtmf_end_with_duration(instance, digit, 0);
4496}
static int ast_rtp_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)

References ast_rtp_dtmf_end_with_duration(), and digit.

◆ ast_rtp_dtmf_end_with_duration()

static int ast_rtp_dtmf_end_with_duration ( struct ast_rtp_instance instance,
char  digit,
unsigned int  duration 
)
static
Precondition
instance is locked

Definition at line 4403 of file res_rtp_asterisk.c.

4404{
4405 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4406 struct ast_sockaddr remote_address = { {0,} };
4407 int hdrlen = 12, res = -1, i = 0;
4408 char data[256];
4409 unsigned int *rtpheader = (unsigned int*)data;
4410 unsigned int measured_samples;
4411
4412 ast_rtp_instance_get_remote_address(instance, &remote_address);
4413
4414 /* Make sure we know where the remote side is so we can send them the packet we construct */
4415 if (ast_sockaddr_isnull(&remote_address)) {
4416 goto cleanup;
4417 }
4418
4419 /* Convert the given digit to the one we are going to send */
4420 if ((digit <= '9') && (digit >= '0')) {
4421 digit -= '0';
4422 } else if (digit == '*') {
4423 digit = 10;
4424 } else if (digit == '#') {
4425 digit = 11;
4426 } else if ((digit >= 'A') && (digit <= 'D')) {
4427 digit = digit - 'A' + 12;
4428 } else if ((digit >= 'a') && (digit <= 'd')) {
4429 digit = digit - 'a' + 12;
4430 } else {
4431 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
4432 goto cleanup;
4433 }
4434
4435 rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
4436
4437 if (duration > 0 && (measured_samples = duration * ast_rtp_get_rate(rtp->f.subclass.format) / 1000) > rtp->send_duration) {
4438 ast_debug_rtp(2, "(%p) RTP adjusting final end duration from %d to %u\n",
4439 instance, rtp->send_duration, measured_samples);
4440 rtp->send_duration = measured_samples;
4441 }
4442
4443 /* Construct the packet we are going to send */
4444 rtpheader[1] = htonl(rtp->lastdigitts);
4445 rtpheader[2] = htonl(rtp->ssrc);
4446 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
4447 rtpheader[3] |= htonl((1 << 23));
4448
4449 /* Send it 3 times, that's the magical number */
4450 for (i = 0; i < 3; i++) {
4451 int ice;
4452
4453 rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
4454
4455 res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 4, 0, &remote_address, &ice);
4456
4457 if (res < 0) {
4458 ast_log(LOG_ERROR, "RTP Transmission error to %s: %s\n",
4459 ast_sockaddr_stringify(&remote_address),
4460 strerror(errno));
4461 }
4462
4463 if (rtp_debug_test_addr(&remote_address)) {
4464 ast_verbose("Sent RTP DTMF packet to %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n",
4465 ast_sockaddr_stringify(&remote_address),
4466 ice ? " (via ICE)" : "",
4467 rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
4468 }
4469
4470 rtp->seqno++;
4471 }
4472 res = 0;
4473
4474 /* Oh and we can't forget to turn off the stuff that says we are sending DTMF */
4476
4477 /* Reset the smoother as the delivery time stored in it is now out of date */
4478 if (rtp->smoother) {
4480 rtp->smoother = NULL;
4481 }
4482cleanup:
4483 rtp->sending_digit = 0;
4484 rtp->send_digit = 0;
4485
4486 /* Re-Learn expected seqno */
4487 rtp->expectedseqno = -1;
4488
4489 return res;
4490}

References ast_debug_rtp, ast_log, ast_rtp_get_rate(), ast_rtp_instance_get_data(), ast_rtp_instance_get_remote_address, ast_smoother_free(), ast_sockaddr_isnull(), ast_sockaddr_stringify(), ast_tv(), ast_tvadd(), ast_tvnow(), ast_verbose(), calc_txstamp(), cleanup(), digit, DTMF_SAMPLE_RATE_MS, ast_rtp::dtmfmute, errno, ast_rtp::expectedseqno, ast_rtp::f, ast_frame_subclass::format, ast_rtp::lastdigitts, ast_rtp::lastts, LOG_ERROR, LOG_WARNING, NULL, rtp_debug_test_addr(), rtp_sendto(), ast_rtp::send_digit, ast_rtp::send_duration, ast_rtp::send_payload, ast_rtp::sending_digit, ast_rtp::seqno, ast_rtp::smoother, ast_rtp::ssrc, and ast_frame::subclass.

Referenced by ast_rtp_dtmf_end().

◆ ast_rtp_dtmf_mode_get()

static enum ast_rtp_dtmf_mode ast_rtp_dtmf_mode_get ( struct ast_rtp_instance instance)
static
Precondition
instance is locked

Definition at line 4275 of file res_rtp_asterisk.c.

4276{
4277 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4278 return rtp->dtmfmode;
4279}
enum ast_rtp_dtmf_mode dtmfmode

References ast_rtp_instance_get_data(), and ast_rtp::dtmfmode.

◆ ast_rtp_dtmf_mode_set()

static int ast_rtp_dtmf_mode_set ( struct ast_rtp_instance instance,
enum ast_rtp_dtmf_mode  dtmf_mode 
)
static
Precondition
instance is locked

Definition at line 4267 of file res_rtp_asterisk.c.

4268{
4269 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4270 rtp->dtmfmode = dtmf_mode;
4271 return 0;
4272}

References ast_rtp_instance_get_data(), and ast_rtp::dtmfmode.

◆ ast_rtp_extension_enable()

static int ast_rtp_extension_enable ( struct ast_rtp_instance instance,
enum ast_rtp_extension  extension 
)
static

Definition at line 9420 of file res_rtp_asterisk.c.

9421{
9422 switch (extension) {
9425 return 1;
9426 default:
9427 return 0;
9428 }
9429}
@ AST_RTP_EXTENSION_TRANSPORT_WIDE_CC
Definition: rtp_engine.h:594
@ AST_RTP_EXTENSION_ABS_SEND_TIME
Definition: rtp_engine.h:592
structure to hold extensions

References AST_RTP_EXTENSION_ABS_SEND_TIME, and AST_RTP_EXTENSION_TRANSPORT_WIDE_CC.

◆ ast_rtp_fd()

static int ast_rtp_fd ( struct ast_rtp_instance instance,
int  rtcp 
)
static
Precondition
instance is locked

Definition at line 8968 of file res_rtp_asterisk.c.

8969{
8970 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
8971
8972 return rtcp ? (rtp->rtcp ? rtp->rtcp->s : -1) : rtp->s;
8973}

References ast_rtp_instance_get_data(), ast_rtp::rtcp, ast_rtp::s, and ast_rtcp::s.

◆ ast_rtp_get_cname()

static const char * ast_rtp_get_cname ( struct ast_rtp_instance instance)
static
Precondition
instance is locked

Definition at line 9367 of file res_rtp_asterisk.c.

9368{
9369 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
9370
9371 return rtp->cname;
9372}

References ast_rtp_instance_get_data(), and ast_rtp::cname.

◆ ast_rtp_get_ssrc()

static unsigned int ast_rtp_get_ssrc ( struct ast_rtp_instance instance)
static
Precondition
instance is locked

Definition at line 9359 of file res_rtp_asterisk.c.

9360{
9361 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
9362
9363 return rtp->ssrc;
9364}

References ast_rtp_instance_get_data(), and ast_rtp::ssrc.

Referenced by __rtp_find_instance_by_ssrc().

◆ ast_rtp_get_stat()

static int ast_rtp_get_stat ( struct ast_rtp_instance instance,
struct ast_rtp_instance_stats stats,
enum ast_rtp_instance_stat  stat 
)
static
Precondition
instance is locked

Definition at line 9150 of file res_rtp_asterisk.c.

9151{
9152 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
9153
9154 if (!rtp->rtcp) {
9155 return -1;
9156 }
9157
9162
9174
9186
9193
9205
9206
9210
9211 return 0;
9212}
#define AST_RTP_STAT_TERMINATOR(combined)
Definition: rtp_engine.h:495
@ AST_RTP_INSTANCE_STAT_LOCAL_MAXRXPLOSS
Definition: rtp_engine.h:204
@ AST_RTP_INSTANCE_STAT_REMOTE_NORMDEVRXPLOSS
Definition: rtp_engine.h:200
@ AST_RTP_INSTANCE_STAT_REMOTE_MAXRXPLOSS
Definition: rtp_engine.h:196
@ AST_RTP_INSTANCE_STAT_REMOTE_NORMDEVMES
Definition: rtp_engine.h:267
@ AST_RTP_INSTANCE_STAT_REMOTE_MINJITTER
Definition: rtp_engine.h:220
@ AST_RTP_INSTANCE_STAT_LOCAL_NORMDEVMES
Definition: rtp_engine.h:275
@ AST_RTP_INSTANCE_STAT_MIN_RTT
Definition: rtp_engine.h:240
@ AST_RTP_INSTANCE_STAT_TXMES
Definition: rtp_engine.h:259
@ AST_RTP_INSTANCE_STAT_CHANNEL_UNIQUEID
Definition: rtp_engine.h:250
@ AST_RTP_INSTANCE_STAT_TXPLOSS
Definition: rtp_engine.h:192
@ AST_RTP_INSTANCE_STAT_MAX_RTT
Definition: rtp_engine.h:238
@ AST_RTP_INSTANCE_STAT_RXPLOSS
Definition: rtp_engine.h:194
@ AST_RTP_INSTANCE_STAT_REMOTE_MAXJITTER
Definition: rtp_engine.h:218
@ AST_RTP_INSTANCE_STAT_LOCAL_MAXJITTER
Definition: rtp_engine.h:226
@ AST_RTP_INSTANCE_STAT_REMOTE_MINMES
Definition: rtp_engine.h:265
@ AST_RTP_INSTANCE_STAT_REMOTE_STDEVJITTER
Definition: rtp_engine.h:224
@ AST_RTP_INSTANCE_STAT_REMOTE_MINRXPLOSS
Definition: rtp_engine.h:198
@ AST_RTP_INSTANCE_STAT_LOCAL_MINMES
Definition: rtp_engine.h:273
@ AST_RTP_INSTANCE_STAT_TXOCTETCOUNT
Definition: rtp_engine.h:252
@ AST_RTP_INSTANCE_STAT_RXMES
Definition: rtp_engine.h:261
@ AST_RTP_INSTANCE_STAT_REMOTE_STDEVMES
Definition: rtp_engine.h:269
@ AST_RTP_INSTANCE_STAT_LOCAL_NORMDEVRXPLOSS
Definition: rtp_engine.h:208
@ AST_RTP_INSTANCE_STAT_REMOTE_STDEVRXPLOSS
Definition: rtp_engine.h:202
@ AST_RTP_INSTANCE_STAT_LOCAL_STDEVRXPLOSS
Definition: rtp_engine.h:210
@ AST_RTP_INSTANCE_STAT_REMOTE_MAXMES
Definition: rtp_engine.h:263
@ AST_RTP_INSTANCE_STAT_TXCOUNT
Definition: rtp_engine.h:186
@ AST_RTP_INSTANCE_STAT_STDEVRTT
Definition: rtp_engine.h:244
@ AST_RTP_INSTANCE_STAT_COMBINED_MES
Definition: rtp_engine.h:257
@ AST_RTP_INSTANCE_STAT_LOCAL_MAXMES
Definition: rtp_engine.h:271
@ AST_RTP_INSTANCE_STAT_RXJITTER
Definition: rtp_engine.h:216
@ AST_RTP_INSTANCE_STAT_LOCAL_MINRXPLOSS
Definition: rtp_engine.h:206
@ AST_RTP_INSTANCE_STAT_LOCAL_SSRC
Definition: rtp_engine.h:246
@ AST_RTP_INSTANCE_STAT_REMOTE_NORMDEVJITTER
Definition: rtp_engine.h:222
@ AST_RTP_INSTANCE_STAT_COMBINED_JITTER
Definition: rtp_engine.h:212
@ AST_RTP_INSTANCE_STAT_TXJITTER
Definition: rtp_engine.h:214
@ AST_RTP_INSTANCE_STAT_LOCAL_MINJITTER
Definition: rtp_engine.h:228
@ AST_RTP_INSTANCE_STAT_COMBINED_LOSS
Definition: rtp_engine.h:190
@ AST_RTP_INSTANCE_STAT_LOCAL_STDEVJITTER
Definition: rtp_engine.h:232
@ AST_RTP_INSTANCE_STAT_COMBINED_RTT
Definition: rtp_engine.h:234
@ AST_RTP_INSTANCE_STAT_NORMDEVRTT
Definition: rtp_engine.h:242
@ AST_RTP_INSTANCE_STAT_RTT
Definition: rtp_engine.h:236
@ AST_RTP_INSTANCE_STAT_RXOCTETCOUNT
Definition: rtp_engine.h:254
@ AST_RTP_INSTANCE_STAT_LOCAL_STDEVMES
Definition: rtp_engine.h:277
@ AST_RTP_INSTANCE_STAT_LOCAL_NORMDEVJITTER
Definition: rtp_engine.h:230
@ AST_RTP_INSTANCE_STAT_RXCOUNT
Definition: rtp_engine.h:188
@ AST_RTP_INSTANCE_STAT_REMOTE_SSRC
Definition: rtp_engine.h:248
#define AST_RTP_STAT_STRCPY(current_stat, combined, placement, value)
Definition: rtp_engine.h:487
#define AST_RTP_STAT_SET(current_stat, combined, placement, value)
Definition: rtp_engine.h:479
double maxrxmes
double maxrxlost
unsigned int received_prior
double reported_maxjitter
double reported_normdev_lost
double reported_minlost
double normdevrtt
double reported_normdev_mes
double stdevrtt
double minrxjitter
double reported_maxmes
unsigned int reported_lost
double reported_stdev_jitter
double normdev_rxjitter
double reported_stdev_lost
double normdev_rxlost
double reported_stdev_mes
double normdev_rxmes
double maxrxjitter
double reported_normdev_jitter
double reported_maxlost
double stdev_rxjitter
double reported_jitter
double reported_minjitter
double minrxlost
double minrxmes
unsigned int expected_prior
double reported_minmes
double stdev_rxlost
unsigned int remote_ssrc
Definition: rtp_engine.h:449
unsigned int rxcount
Definition: rtp_engine.h:395
unsigned int local_ssrc
Definition: rtp_engine.h:447
unsigned int rxoctetcount
Definition: rtp_engine.h:455
unsigned int rxploss
Definition: rtp_engine.h:419
unsigned int txcount
Definition: rtp_engine.h:393
unsigned int txploss
Definition: rtp_engine.h:417
unsigned int txoctetcount
Definition: rtp_engine.h:453
char channel_uniqueid[MAX_CHANNEL_ID]
Definition: rtp_engine.h:451
unsigned int rxcount
unsigned int rxoctetcount
double rxjitter

References ast_rtp_instance_get_channel_id(), ast_rtp_instance_get_data(), AST_RTP_INSTANCE_STAT_CHANNEL_UNIQUEID, AST_RTP_INSTANCE_STAT_COMBINED_JITTER, AST_RTP_INSTANCE_STAT_COMBINED_LOSS, AST_RTP_INSTANCE_STAT_COMBINED_MES, AST_RTP_INSTANCE_STAT_COMBINED_RTT, AST_RTP_INSTANCE_STAT_LOCAL_MAXJITTER, AST_RTP_INSTANCE_STAT_LOCAL_MAXMES, AST_RTP_INSTANCE_STAT_LOCAL_MAXRXPLOSS, AST_RTP_INSTANCE_STAT_LOCAL_MINJITTER, AST_RTP_INSTANCE_STAT_LOCAL_MINMES, AST_RTP_INSTANCE_STAT_LOCAL_MINRXPLOSS, AST_RTP_INSTANCE_STAT_LOCAL_NORMDEVJITTER, AST_RTP_INSTANCE_STAT_LOCAL_NORMDEVMES, AST_RTP_INSTANCE_STAT_LOCAL_NORMDEVRXPLOSS, AST_RTP_INSTANCE_STAT_LOCAL_SSRC, AST_RTP_INSTANCE_STAT_LOCAL_STDEVJITTER, AST_RTP_INSTANCE_STAT_LOCAL_STDEVMES, AST_RTP_INSTANCE_STAT_LOCAL_STDEVRXPLOSS, AST_RTP_INSTANCE_STAT_MAX_RTT, AST_RTP_INSTANCE_STAT_MIN_RTT, AST_RTP_INSTANCE_STAT_NORMDEVRTT, AST_RTP_INSTANCE_STAT_REMOTE_MAXJITTER, AST_RTP_INSTANCE_STAT_REMOTE_MAXMES, AST_RTP_INSTANCE_STAT_REMOTE_MAXRXPLOSS, AST_RTP_INSTANCE_STAT_REMOTE_MINJITTER, AST_RTP_INSTANCE_STAT_REMOTE_MINMES, AST_RTP_INSTANCE_STAT_REMOTE_MINRXPLOSS, AST_RTP_INSTANCE_STAT_REMOTE_NORMDEVJITTER, AST_RTP_INSTANCE_STAT_REMOTE_NORMDEVMES, AST_RTP_INSTANCE_STAT_REMOTE_NORMDEVRXPLOSS, AST_RTP_INSTANCE_STAT_REMOTE_SSRC, AST_RTP_INSTANCE_STAT_REMOTE_STDEVJITTER, AST_RTP_INSTANCE_STAT_REMOTE_STDEVMES, AST_RTP_INSTANCE_STAT_REMOTE_STDEVRXPLOSS, AST_RTP_INSTANCE_STAT_RTT, AST_RTP_INSTANCE_STAT_RXCOUNT, AST_RTP_INSTANCE_STAT_RXJITTER, AST_RTP_INSTANCE_STAT_RXMES, AST_RTP_INSTANCE_STAT_RXOCTETCOUNT, AST_RTP_INSTANCE_STAT_RXPLOSS, AST_RTP_INSTANCE_STAT_STDEVRTT, AST_RTP_INSTANCE_STAT_TXCOUNT, AST_RTP_INSTANCE_STAT_TXJITTER, AST_RTP_INSTANCE_STAT_TXMES, AST_RTP_INSTANCE_STAT_TXOCTETCOUNT, AST_RTP_INSTANCE_STAT_TXPLOSS, AST_RTP_STAT_SET, AST_RTP_STAT_STRCPY, AST_RTP_STAT_TERMINATOR, ast_rtp_instance_stats::channel_uniqueid, ast_rtcp::expected_prior, ast_rtp_instance_stats::local_maxjitter, ast_rtp_instance_stats::local_maxmes, ast_rtp_instance_stats::local_maxrxploss, ast_rtp_instance_stats::local_minjitter, ast_rtp_instance_stats::local_minmes, ast_rtp_instance_stats::local_minrxploss, ast_rtp_instance_stats::local_normdevjitter, ast_rtp_instance_stats::local_normdevmes, ast_rtp_instance_stats::local_normdevrxploss, ast_rtp_instance_stats::local_ssrc, ast_rtp_instance_stats::local_stdevjitter, ast_rtp_instance_stats::local_stdevmes, ast_rtp_instance_stats::local_stdevrxploss, ast_rtp_instance_stats::maxrtt, ast_rtcp::maxrtt, ast_rtcp::maxrxjitter, ast_rtcp::maxrxlost, ast_rtcp::maxrxmes, ast_rtp_instance_stats::minrtt, ast_rtcp::minrtt, ast_rtcp::minrxjitter, ast_rtcp::minrxlost, ast_rtcp::minrxmes, ast_rtcp::normdev_rxjitter, ast_rtcp::normdev_rxlost, ast_rtcp::normdev_rxmes, ast_rtp_instance_stats::normdevrtt, ast_rtcp::normdevrtt, ast_rtcp::received_prior, ast_rtp_instance_stats::remote_maxjitter, ast_rtp_instance_stats::remote_maxmes, ast_rtp_instance_stats::remote_maxrxploss, ast_rtp_instance_stats::remote_minjitter, ast_rtp_instance_stats::remote_minmes, ast_rtp_instance_stats::remote_minrxploss, ast_rtp_instance_stats::remote_normdevjitter, ast_rtp_instance_stats::remote_normdevmes, ast_rtp_instance_stats::remote_normdevrxploss, ast_rtp_instance_stats::remote_ssrc, ast_rtp_instance_stats::remote_stdevjitter, ast_rtp_instance_stats::remote_stdevmes, ast_rtp_instance_stats::remote_stdevrxploss, ast_rtcp::reported_jitter, ast_rtcp::reported_lost, ast_rtcp::reported_maxjitter, ast_rtcp::reported_maxlost, ast_rtcp::reported_maxmes, ast_rtcp::reported_mes, ast_rtcp::reported_minjitter, ast_rtcp::reported_minlost, ast_rtcp::reported_minmes, ast_rtcp::reported_normdev_jitter, ast_rtcp::reported_normdev_lost, ast_rtcp::reported_normdev_mes, ast_rtcp::reported_stdev_jitter, ast_rtcp::reported_stdev_lost, ast_rtcp::reported_stdev_mes, ast_rtp::rtcp, ast_rtp_instance_stats::rtt, ast_rtcp::rtt, ast_rtp_instance_stats::rxcount, ast_rtp::rxcount, ast_rtp_instance_stats::rxjitter, ast_rtp::rxjitter, ast_rtp_instance_stats::rxmes, ast_rtp::rxmes, ast_rtp_instance_stats::rxoctetcount, ast_rtp::rxoctetcount, ast_rtp_instance_stats::rxploss, ast_rtp::ssrc, ast_rtcp::stdev_rxjitter, ast_rtcp::stdev_rxlost, ast_rtp_instance_stats::stdevrtt, ast_rtcp::stdevrtt, ast_rtp::themssrc, ast_rtp_instance_stats::txcount, ast_rtp::txcount, ast_rtp_instance_stats::txjitter, ast_rtp_instance_stats::txmes, ast_rtp_instance_stats::txoctetcount, ast_rtp::txoctetcount, and ast_rtp_instance_stats::txploss.

◆ ast_rtp_interpret()

static struct ast_frame * ast_rtp_interpret ( struct ast_rtp_instance instance,
struct ast_srtp srtp,
const struct ast_sockaddr remote_address,
unsigned char *  read_area,
int  length,
int  prev_seqno,
unsigned int  bundled 
)
static

Definition at line 7673 of file res_rtp_asterisk.c.

7676{
7677 unsigned int *rtpheader = (unsigned int*)(read_area);
7678 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
7679 struct ast_rtp_instance *instance1;
7680 int res = length, hdrlen = 12, ssrc, seqno, payloadtype, padding, mark, ext, cc;
7681 unsigned int timestamp;
7682 RAII_VAR(struct ast_rtp_payload_type *, payload, NULL, ao2_cleanup);
7683 struct frame_list frames;
7684
7685 /* If this payload is encrypted then decrypt it using the given SRTP instance */
7686 if ((*read_area & 0xC0) && res_srtp && srtp && res_srtp->unprotect(
7687 srtp, read_area, &res, 0 | (srtp_replay_protection << 1)) < 0) {
7688 return &ast_null_frame;
7689 }
7690
7691 /* If we are currently sending DTMF to the remote party send a continuation packet */
7692 if (rtp->sending_digit) {
7693 ast_rtp_dtmf_continuation(instance);
7694 }
7695
7696 /* Pull out the various other fields we will need */
7697 ssrc = ntohl(rtpheader[2]);
7698 seqno = ntohl(rtpheader[0]);
7699 payloadtype = (seqno & 0x7f0000) >> 16;
7700 padding = seqno & (1 << 29);
7701 mark = seqno & (1 << 23);
7702 ext = seqno & (1 << 28);
7703 cc = (seqno & 0xF000000) >> 24;
7704 seqno &= 0xffff;
7705 timestamp = ntohl(rtpheader[1]);
7706
7708
7709 /* Remove any padding bytes that may be present */
7710 if (padding) {
7711 res -= read_area[res - 1];
7712 }
7713
7714 /* Skip over any CSRC fields */
7715 if (cc) {
7716 hdrlen += cc * 4;
7717 }
7718
7719 /* Look for any RTP extensions, currently we do not support any */
7720 if (ext) {
7721 int extensions_size = (ntohl(rtpheader[hdrlen/4]) & 0xffff) << 2;
7722 unsigned int profile;
7723 profile = (ntohl(rtpheader[3]) & 0xffff0000) >> 16;
7724
7725 if (profile == 0xbede) {
7726 /* We skip over the first 4 bytes as they are just for the one byte extension header */
7727 rtp_instance_parse_extmap_extensions(instance, rtp, read_area + hdrlen + 4, extensions_size);
7728 } else if (DEBUG_ATLEAST(1)) {
7729 if (profile == 0x505a) {
7730 ast_log(LOG_DEBUG, "Found Zfone extension in RTP stream - zrtp - not supported.\n");
7731 } else {
7732 /* SDP negotiated RTP extensions can not currently be output in logging */
7733 ast_log(LOG_DEBUG, "Found unknown RTP Extensions %x\n", profile);
7734 }
7735 }
7736
7737 hdrlen += extensions_size;
7738 hdrlen += 4;
7739 }
7740
7741 /* Make sure after we potentially mucked with the header length that it is once again valid */
7742 if (res < hdrlen) {
7743 ast_log(LOG_WARNING, "RTP Read too short (%d, expecting %d\n", res, hdrlen);
7745 }
7746
7747 /* Only non-bundled instances can change/learn the remote's SSRC implicitly. */
7748 if (!bundled) {
7749 /* Force a marker bit and change SSRC if the SSRC changes */
7750 if (rtp->themssrc_valid && rtp->themssrc != ssrc) {
7751 struct ast_frame *f, srcupdate = {
7753 .subclass.integer = AST_CONTROL_SRCCHANGE,
7754 };
7755
7756 if (!mark) {
7758 ast_debug(0, "(%p) RTP forcing Marker bit, because SSRC has changed\n", instance);
7759 }
7760 mark = 1;
7761 }
7762
7763 f = ast_frisolate(&srcupdate);
7765
7766 rtp->seedrxseqno = 0;
7767 rtp->rxcount = 0;
7768 rtp->rxoctetcount = 0;
7769 rtp->cycles = 0;
7770 prev_seqno = 0;
7771 rtp->last_seqno = 0;
7772 rtp->last_end_timestamp.ts = 0;
7773 rtp->last_end_timestamp.is_set = 0;
7774 if (rtp->rtcp) {
7775 rtp->rtcp->expected_prior = 0;
7776 rtp->rtcp->received_prior = 0;
7777 }
7778 }
7779
7780 rtp->themssrc = ssrc; /* Record their SSRC to put in future RR */
7781 rtp->themssrc_valid = 1;
7782 }
7783
7784 rtp->rxcount++;
7785 rtp->rxoctetcount += (res - hdrlen);
7786 if (rtp->rxcount == 1) {
7787 rtp->seedrxseqno = seqno;
7788 }
7789
7790 /* Do not schedule RR if RTCP isn't run */
7791 if (rtp->rtcp && !ast_sockaddr_isnull(&rtp->rtcp->them) && rtp->rtcp->schedid < 0) {
7792 /* Schedule transmission of Receiver Report */
7793 ao2_ref(instance, +1);
7795 if (rtp->rtcp->schedid < 0) {
7796 ao2_ref(instance, -1);
7797 ast_log(LOG_WARNING, "scheduling RTCP transmission failed.\n");
7798 }
7799 }
7800 if ((int)prev_seqno - (int)seqno > 100) /* if so it would indicate that the sender cycled; allow for misordering */
7801 rtp->cycles += RTP_SEQ_MOD;
7802
7803 /* If we are directly bridged to another instance send the audio directly out,
7804 * but only after updating core information about the received traffic so that
7805 * outgoing RTCP reflects it.
7806 */
7807 instance1 = ast_rtp_instance_get_bridged(instance);
7808 if (instance1
7809 && !bridge_p2p_rtp_write(instance, instance1, rtpheader, res, hdrlen)) {
7810 struct timeval rxtime;
7811 struct ast_frame *f;
7812
7813 /* Update statistics for jitter so they are correct in RTCP */
7814 calc_rxstamp_and_jitter(&rxtime, rtp, timestamp, mark);
7815
7816
7817 /* When doing P2P we don't need to raise any frames about SSRC change to the core */
7818 while ((f = AST_LIST_REMOVE_HEAD(&frames, frame_list)) != NULL) {
7819 ast_frfree(f);
7820 }
7821
7822 return &ast_null_frame;
7823 }
7824
7825 payload = ast_rtp_codecs_get_payload(ast_rtp_instance_get_codecs(instance), payloadtype);
7826 if (!payload) {
7827 /* Unknown payload type. */
7829 }
7830
7831 /* If the payload is not actually an Asterisk one but a special one pass it off to the respective handler */
7832 if (!payload->asterisk_format) {
7833 struct ast_frame *f = NULL;
7834 if (payload->rtp_code == AST_RTP_DTMF) {
7835 /* process_dtmf_rfc2833 may need to return multiple frames. We do this
7836 * by passing the pointer to the frame list to it so that the method
7837 * can append frames to the list as needed.
7838 */
7839 process_dtmf_rfc2833(instance, read_area + hdrlen, res - hdrlen, seqno, timestamp, payloadtype, mark, &frames);
7840 } else if (payload->rtp_code == AST_RTP_CISCO_DTMF) {
7841 f = process_dtmf_cisco(instance, read_area + hdrlen, res - hdrlen, seqno, timestamp, payloadtype, mark);
7842 } else if (payload->rtp_code == AST_RTP_CN) {
7843 f = process_cn_rfc3389(instance, read_area + hdrlen, res - hdrlen, seqno, timestamp, payloadtype, mark);
7844 } else {
7845 ast_log(LOG_NOTICE, "Unknown RTP codec %d received from '%s'\n",
7846 payloadtype,
7847 ast_sockaddr_stringify(remote_address));
7848 }
7849
7850 if (f) {
7852 }
7853 /* Even if no frame was returned by one of the above methods,
7854 * we may have a frame to return in our frame list
7855 */
7857 }
7858
7859 ao2_replace(rtp->lastrxformat, payload->format);
7860 ao2_replace(rtp->f.subclass.format, payload->format);
7861 switch (ast_format_get_type(rtp->f.subclass.format)) {
7864 break;
7867 break;
7869 rtp->f.frametype = AST_FRAME_TEXT;
7870 break;
7872 /* Fall through */
7873 default:
7874 ast_log(LOG_WARNING, "Unknown or unsupported media type: %s\n",
7876 return &ast_null_frame;
7877 }
7878
7879 if (rtp->dtmf_timeout && rtp->dtmf_timeout < timestamp) {
7880 rtp->dtmf_timeout = 0;
7881
7882 if (rtp->resp) {
7883 struct ast_frame *f;
7884 f = create_dtmf_frame(instance, AST_FRAME_DTMF_END, 0);
7886 rtp->resp = 0;
7887 rtp->dtmf_timeout = rtp->dtmf_duration = 0;
7889 return AST_LIST_FIRST(&frames);
7890 }
7891 }
7892
7893 rtp->f.src = "RTP";
7894 rtp->f.mallocd = 0;
7895 rtp->f.datalen = res - hdrlen;
7896 rtp->f.data.ptr = read_area + hdrlen;
7897 rtp->f.offset = hdrlen + AST_FRIENDLY_OFFSET;
7899 rtp->f.seqno = seqno;
7900 rtp->f.stream_num = rtp->stream_num;
7901
7903 && ((int)seqno - (prev_seqno + 1) > 0)
7904 && ((int)seqno - (prev_seqno + 1) < 10)) {
7905 unsigned char *data = rtp->f.data.ptr;
7906
7907 memmove(rtp->f.data.ptr+3, rtp->f.data.ptr, rtp->f.datalen);
7908 rtp->f.datalen +=3;
7909 *data++ = 0xEF;
7910 *data++ = 0xBF;
7911 *data = 0xBD;
7912 }
7913
7915 unsigned char *data = rtp->f.data.ptr;
7916 unsigned char *header_end;
7917 int num_generations;
7918 int header_length;
7919 int len;
7920 int diff =(int)seqno - (prev_seqno+1); /* if diff = 0, no drop*/
7921 int x;
7922
7924 header_end = memchr(data, ((*data) & 0x7f), rtp->f.datalen);
7925 if (header_end == NULL) {
7927 }
7928 header_end++;
7929
7930 header_length = header_end - data;
7931 num_generations = header_length / 4;
7932 len = header_length;
7933
7934 if (!diff) {
7935 for (x = 0; x < num_generations; x++)
7936 len += data[x * 4 + 3];
7937
7938 if (!(rtp->f.datalen - len))
7940
7941 rtp->f.data.ptr += len;
7942 rtp->f.datalen -= len;
7943 } else if (diff > num_generations && diff < 10) {
7944 len -= 3;
7945 rtp->f.data.ptr += len;
7946 rtp->f.datalen -= len;
7947
7948 data = rtp->f.data.ptr;
7949 *data++ = 0xEF;
7950 *data++ = 0xBF;
7951 *data = 0xBD;
7952 } else {
7953 for ( x = 0; x < num_generations - diff; x++)
7954 len += data[x * 4 + 3];
7955
7956 rtp->f.data.ptr += len;
7957 rtp->f.datalen -= len;
7958 }
7959 }
7960
7962 rtp->f.samples = ast_codec_samples_count(&rtp->f);
7964 ast_frame_byteswap_be(&rtp->f);
7965 }
7966 calc_rxstamp_and_jitter(&rtp->f.delivery, rtp, timestamp, mark);
7967 /* Add timing data to let ast_generic_bridge() put the frame into a jitterbuf */
7969 rtp->f.ts = timestamp / (ast_rtp_get_rate(rtp->f.subclass.format) / 1000);
7970 rtp->f.len = rtp->f.samples / ((ast_format_get_sample_rate(rtp->f.subclass.format) / 1000));
7972 /* Video -- samples is # of samples vs. 90000 */
7973 if (!rtp->lastividtimestamp)
7974 rtp->lastividtimestamp = timestamp;
7975 calc_rxstamp_and_jitter(&rtp->f.delivery, rtp, timestamp, mark);
7977 rtp->f.ts = timestamp / (ast_rtp_get_rate(rtp->f.subclass.format) / 1000);
7978 rtp->f.samples = timestamp - rtp->lastividtimestamp;
7979 rtp->lastividtimestamp = timestamp;
7980 rtp->f.delivery.tv_sec = 0;
7981 rtp->f.delivery.tv_usec = 0;
7982 /* Pass the RTP marker bit as bit */
7983 rtp->f.subclass.frame_ending = mark ? 1 : 0;
7985 /* TEXT -- samples is # of samples vs. 1000 */
7986 if (!rtp->lastitexttimestamp)
7987 rtp->lastitexttimestamp = timestamp;
7988 rtp->f.samples = timestamp - rtp->lastitexttimestamp;
7989 rtp->lastitexttimestamp = timestamp;
7990 rtp->f.delivery.tv_sec = 0;
7991 rtp->f.delivery.tv_usec = 0;
7992 } else {
7993 ast_log(LOG_WARNING, "Unknown or unsupported media type: %s\n",
7995 return &ast_null_frame;
7996 }
7997
7999 return AST_LIST_FIRST(&frames);
8000}
#define ao2_replace(dst, src)
Replace one object reference with another cleaning up the original.
Definition: astobj2.h:501
@ AST_MEDIA_TYPE_AUDIO
Definition: codec.h:32
@ AST_MEDIA_TYPE_VIDEO
Definition: codec.h:33
@ AST_MEDIA_TYPE_IMAGE
Definition: codec.h:34
@ AST_MEDIA_TYPE_TEXT
Definition: codec.h:35
unsigned int ast_codec_samples_count(struct ast_frame *frame)
Get the number of samples contained within a frame.
Definition: codec.c:379
const char * ast_codec_media_type2str(enum ast_media_type type)
Conversion function to take a media type and turn it into a string.
Definition: codec.c:348
enum ast_media_type ast_format_get_type(const struct ast_format *format)
Get the media type of a format.
Definition: format.c:354
unsigned int ast_format_get_sample_rate(const struct ast_format *format)
Get the sample rate of a media format.
Definition: format.c:379
enum ast_format_cmp_res ast_format_cmp(const struct ast_format *format1, const struct ast_format *format2)
Compare two formats.
Definition: format.c:201
@ AST_FORMAT_CMP_EQUAL
Definition: format.h:36
int ast_format_cache_is_slinear(struct ast_format *format)
Determines if a format is one of the cached slin formats.
Definition: format_cache.c:534
struct ast_format * ast_format_t140_red
Built-in cached t140 red format.
Definition: format_cache.c:236
struct ast_format * ast_format_t140
Built-in cached t140 format.
Definition: format_cache.c:231
const char * ext
Definition: http.c:150
@ AST_FRFLAG_HAS_SEQUENCE_NUMBER
@ AST_FRFLAG_HAS_TIMING_INFO
#define ast_frame_byteswap_be(fr)
#define ast_frisolate(fr)
Makes a frame independent of any static storage.
#define ast_frfree(fr)
@ AST_FRAME_VIDEO
@ AST_FRAME_DTMF_END
@ AST_FRAME_VOICE
@ AST_FRAME_TEXT
#define DEBUG_ATLEAST(level)
#define LOG_DEBUG
#define LOG_NOTICE
#define AST_LIST_HEAD_INIT_NOLOCK(head)
Initializes a list head structure.
Definition: linkedlists.h:681
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:731
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:833
#define AST_LIST_FIRST(head)
Returns the first entry contained in a list.
Definition: linkedlists.h:421
static int frames
Definition: parser.c:51
static int bridge_p2p_rtp_write(struct ast_rtp_instance *instance, struct ast_rtp_instance *instance1, unsigned int *rtpheader, int len, int hdrlen)
static void calc_rxstamp_and_jitter(struct timeval *tv, struct ast_rtp *rtp, unsigned int rx_rtp_ts, int mark)
static int ast_rtp_dtmf_continuation(struct ast_rtp_instance *instance)
static void process_dtmf_rfc2833(struct ast_rtp_instance *instance, unsigned char *data, int len, unsigned int seqno, unsigned int timestamp, int payloadtype, int mark, struct frame_list *frames)
static struct ast_frame * process_cn_rfc3389(struct ast_rtp_instance *instance, unsigned char *data, int len, unsigned int seqno, unsigned int timestamp, int payloadtype, int mark)
static void rtp_instance_parse_extmap_extensions(struct ast_rtp_instance *instance, struct ast_rtp *rtp, unsigned char *extension, int len)
static unsigned int ast_rtcp_calc_interval(struct ast_rtp *rtp)
static struct ast_frame * process_dtmf_cisco(struct ast_rtp_instance *instance, unsigned char *data, int len, unsigned int seqno, unsigned int timestamp, int payloadtype, int mark)
#define RTP_SEQ_MOD
static struct ast_frame * create_dtmf_frame(struct ast_rtp_instance *instance, enum ast_frame_type type, int compensate)
static int ast_rtcp_write(const void *data)
Write a RTCP packet to the far end.
struct ast_rtp_payload_type * ast_rtp_codecs_get_payload(struct ast_rtp_codecs *codecs, int payload)
Retrieve rx payload mapped information by payload type.
Definition: rtp_engine.c:1492
struct ast_rtp_instance * ast_rtp_instance_get_bridged(struct ast_rtp_instance *instance)
Get the other RTP instance that an instance is bridged to.
Definition: rtp_engine.c:2247
#define AST_RTP_CN
Definition: rtp_engine.h:293
#define AST_RTP_CISCO_DTMF
Definition: rtp_engine.h:295
int ast_sched_add(struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data) attribute_warn_unused_result
Adds a scheduled event.
Definition: sched.c:567
unsigned int lastividtimestamp
unsigned int dtmf_duration
unsigned short seedrxseqno
unsigned int last_seqno
unsigned int dtmf_timeout
optional_ts last_end_timestamp
unsigned int lastitexttimestamp
unsigned int ts
unsigned char is_set
struct timeval ast_samp2tv(unsigned int _nsamp, unsigned int _rate)
Returns a timeval corresponding to the duration of n samples at rate r. Useful to convert samples to ...
Definition: time.h:282
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:107

References ao2_cleanup, ao2_ref, ao2_replace, ast_codec_media_type2str(), ast_codec_samples_count(), AST_CONTROL_SRCCHANGE, ast_debug, ast_debug_rtp_packet_is_allowed, ast_format_cache_is_slinear(), ast_format_cmp(), AST_FORMAT_CMP_EQUAL, ast_format_get_sample_rate(), ast_format_get_type(), ast_format_t140, ast_format_t140_red, ast_frame_byteswap_be, AST_FRAME_CONTROL, AST_FRAME_DTMF_END, AST_FRAME_TEXT, AST_FRAME_VIDEO, AST_FRAME_VOICE, AST_FRFLAG_HAS_SEQUENCE_NUMBER, AST_FRFLAG_HAS_TIMING_INFO, ast_frfree, AST_FRIENDLY_OFFSET, ast_frisolate, AST_LIST_FIRST, AST_LIST_HEAD_INIT_NOLOCK, AST_LIST_INSERT_TAIL, AST_LIST_REMOVE_HEAD, ast_log, AST_MEDIA_TYPE_AUDIO, AST_MEDIA_TYPE_IMAGE, AST_MEDIA_TYPE_TEXT, AST_MEDIA_TYPE_VIDEO, ast_null_frame, ast_rtcp_calc_interval(), ast_rtcp_write(), AST_RTP_CISCO_DTMF, AST_RTP_CN, ast_rtp_codecs_get_payload(), AST_RTP_DTMF, ast_rtp_dtmf_continuation(), ast_rtp_get_rate(), ast_rtp_instance_get_bridged(), ast_rtp_instance_get_codecs(), ast_rtp_instance_get_data(), ast_samp2tv(), ast_sched_add(), ast_set_flag, ast_sockaddr_isnull(), ast_sockaddr_stringify(), ast_tv(), ast_tvdiff_ms(), bridge_p2p_rtp_write(), calc_rxstamp_and_jitter(), create_dtmf_frame(), ast_rtp::cycles, ast_frame::data, ast_frame::datalen, DEBUG_ATLEAST, ast_frame::delivery, ast_rtp::dtmf_duration, ast_rtp::dtmf_timeout, ast_rtcp::expected_prior, ext, ast_rtp::f, ast_frame_subclass::format, ast_frame_subclass::frame_ending, frames, ast_frame::frametype, optional_ts::is_set, ast_rtp::last_end_timestamp, ast_rtp::last_seqno, ast_rtp::lastitexttimestamp, ast_rtp::lastividtimestamp, ast_rtp::lastrxformat, len(), ast_frame::len, LOG_DEBUG, LOG_NOTICE, LOG_WARNING, ast_frame::mallocd, NULL, ast_frame::offset, process_cn_rfc3389(), process_dtmf_cisco(), process_dtmf_rfc2833(), ast_frame::ptr, RAII_VAR, ast_rtcp::received_prior, res_srtp, ast_rtp::resp, ast_rtp::rtcp, rtp_instance_parse_extmap_extensions(), RTP_SEQ_MOD, ast_rtp::rxcount, ast_rtp::rxoctetcount, ast_frame::samples, ast_rtp::sched, ast_rtcp::schedid, ast_rtp::seedrxseqno, ast_rtp::sending_digit, ast_frame::seqno, ast_frame::src, srtp_replay_protection, ast_frame::stream_num, ast_rtp::stream_num, ast_frame::subclass, ast_rtcp::them, ast_rtp::themssrc, ast_rtp::themssrc_valid, ast_frame::ts, optional_ts::ts, and ast_srtp_res::unprotect.

Referenced by ast_rtp_read().

◆ ast_rtp_local_bridge()

static int ast_rtp_local_bridge ( struct ast_rtp_instance instance0,
struct ast_rtp_instance instance1 
)
static
Precondition
Neither instance0 nor instance1 are locked

Definition at line 9122 of file res_rtp_asterisk.c.

9123{
9124 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance0);
9125
9126 ao2_lock(instance0);
9128 if (rtp->smoother) {
9130 rtp->smoother = NULL;
9131 }
9132
9133 /* We must use a new SSRC when local bridge ends */
9134 if (!instance1) {
9135 rtp->ssrc = rtp->ssrc_orig;
9136 rtp->ssrc_orig = 0;
9137 rtp->ssrc_saved = 0;
9138 } else if (!rtp->ssrc_saved) {
9139 /* In case ast_rtp_local_bridge is called multiple times, only save the ssrc from before local bridge began */
9140 rtp->ssrc_orig = rtp->ssrc;
9141 rtp->ssrc_saved = 1;
9142 }
9143
9144 ao2_unlock(instance0);
9145
9146 return 0;
9147}
#define FLAG_REQ_LOCAL_BRIDGE_BIT
unsigned int ssrc_orig
unsigned char ssrc_saved

References ao2_lock, ao2_unlock, ast_rtp_instance_get_data(), ast_set_flag, ast_smoother_free(), FLAG_NEED_MARKER_BIT, FLAG_REQ_LOCAL_BRIDGE_BIT, NULL, ast_rtp::smoother, ast_rtp::ssrc, ast_rtp::ssrc_orig, and ast_rtp::ssrc_saved.

◆ ast_rtp_new()

static int ast_rtp_new ( struct ast_rtp_instance instance,
struct ast_sched_context sched,
struct ast_sockaddr addr,
void *  data 
)
static
Precondition
instance is locked

Definition at line 4139 of file res_rtp_asterisk.c.

4142{
4143 struct ast_rtp *rtp = NULL;
4144
4145 /* Create a new RTP structure to hold all of our data */
4146 if (!(rtp = ast_calloc(1, sizeof(*rtp)))) {
4147 return -1;
4148 }
4149 rtp->owner = instance;
4150 /* Set default parameters on the newly created RTP structure */
4151 rtp->ssrc = ast_random();
4152 ast_uuid_generate_str(rtp->cname, sizeof(rtp->cname));
4153 rtp->seqno = ast_random() & 0x7fff;
4154 rtp->expectedrxseqno = -1;
4155 rtp->expectedseqno = -1;
4156 rtp->rxstart = -1;
4157 rtp->sched = sched;
4158 ast_sockaddr_copy(&rtp->bind_address, addr);
4159 /* Transport creation operations can grab the RTP data from the instance, so set it */
4160 ast_rtp_instance_set_data(instance, rtp);
4161
4162 if (rtp_allocate_transport(instance, rtp)) {
4163 return -1;
4164 }
4165
4166 if (AST_VECTOR_INIT(&rtp->ssrc_mapping, 1)) {
4167 return -1;
4168 }
4169
4171 return -1;
4172 }
4173 rtp->transport_wide_cc.schedid = -1;
4174
4178 rtp->stream_num = -1;
4179
4180 return 0;
4181}
static struct ast_sched_context * sched
Definition: chan_ooh323.c:400
struct ast_format * ast_format_none
Built-in "null" format.
Definition: format_cache.c:246
void ast_rtp_instance_set_data(struct ast_rtp_instance *instance, void *data)
Set the data portion of an RTP instance.
Definition: rtp_engine.c:579
double rxstart
struct ast_sockaddr bind_address
char * ast_uuid_generate_str(char *buf, size_t size)
Generate a UUID string.
Definition: uuid.c:141
#define AST_VECTOR_INIT(vec, size)
Initialize a vector.
Definition: vector.h:113

References ao2_bump, ast_calloc, ast_format_none, ast_random(), ast_rtp_instance_set_data(), ast_sockaddr_copy(), ast_uuid_generate_str(), AST_VECTOR_INIT, ast_rtp::bind_address, ast_rtp::cname, ast_rtp::expectedrxseqno, ast_rtp::expectedseqno, ast_rtp::f, ast_frame_subclass::format, ast_rtp::lastrxformat, ast_rtp::lasttxformat, NULL, ast_rtp::owner, rtp_transport_wide_cc_statistics::packet_statistics, rtp_allocate_transport(), ast_rtp::rxstart, sched, ast_rtp::sched, rtp_transport_wide_cc_statistics::schedid, ast_rtp::seqno, ast_rtp::ssrc, ast_rtp::ssrc_mapping, ast_rtp::stream_num, ast_frame::subclass, and ast_rtp::transport_wide_cc.

◆ ast_rtp_prop_set()

static void ast_rtp_prop_set ( struct ast_rtp_instance instance,
enum ast_rtp_property  property,
int  value 
)
static
Precondition
instance is locked

Definition at line 8779 of file res_rtp_asterisk.c.

8780{
8781 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
8782
8783 if (property == AST_RTP_PROPERTY_RTCP) {
8784 if (value) {
8785 struct ast_sockaddr local_addr;
8786
8787 if (rtp->rtcp && rtp->rtcp->type == value) {
8788 ast_debug_rtcp(1, "(%p) RTCP ignoring duplicate property\n", instance);
8789 return;
8790 }
8791
8792 if (!rtp->rtcp) {
8793 rtp->rtcp = ast_calloc(1, sizeof(*rtp->rtcp));
8794 if (!rtp->rtcp) {
8795 return;
8796 }
8797 rtp->rtcp->s = -1;
8798#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
8799 rtp->rtcp->dtls.timeout_timer = -1;
8800#endif
8801 rtp->rtcp->schedid = -1;
8802 }
8803
8804 rtp->rtcp->type = value;
8805
8806 /* Grab the IP address and port we are going to use */
8807 ast_rtp_instance_get_local_address(instance, &rtp->rtcp->us);
8810 ast_sockaddr_port(&rtp->rtcp->us) + 1);
8811 }
8812
8813 ast_sockaddr_copy(&local_addr, &rtp->rtcp->us);
8814 if (!ast_find_ourip(&local_addr, &rtp->rtcp->us, 0)) {
8815 ast_sockaddr_set_port(&local_addr, ast_sockaddr_port(&rtp->rtcp->us));
8816 } else {
8817 /* Failed to get local address reset to use default. */
8818 ast_sockaddr_copy(&local_addr, &rtp->rtcp->us);
8819 }
8820
8823 if (!rtp->rtcp->local_addr_str) {
8824 ast_free(rtp->rtcp);
8825 rtp->rtcp = NULL;
8826 return;
8827 }
8828
8830 /* We're either setting up RTCP from scratch or
8831 * switching from MUX. Either way, we won't have
8832 * a socket set up, and we need to set it up
8833 */
8834 if ((rtp->rtcp->s =
8835 create_new_socket("RTCP",
8836 ast_sockaddr_is_ipv4(&rtp->rtcp->us) ?
8837 AF_INET :
8838 ast_sockaddr_is_ipv6(&rtp->rtcp->us) ?
8839 AF_INET6 : -1)) < 0) {
8840 ast_debug_rtcp(1, "(%p) RTCP failed to create a new socket\n", instance);
8842 ast_free(rtp->rtcp);
8843 rtp->rtcp = NULL;
8844 return;
8845 }
8846
8847 /* Try to actually bind to the IP address and port we are going to use for RTCP, if this fails we have to bail out */
8848 if (ast_bind(rtp->rtcp->s, &rtp->rtcp->us)) {
8849 ast_debug_rtcp(1, "(%p) RTCP failed to setup RTP instance\n", instance);
8850 close(rtp->rtcp->s);
8852 ast_free(rtp->rtcp);
8853 rtp->rtcp = NULL;
8854 return;
8855 }
8856#ifdef HAVE_PJPROJECT
8857 if (rtp->ice) {
8858 rtp_add_candidates_to_ice(instance, rtp, &rtp->rtcp->us, ast_sockaddr_port(&rtp->rtcp->us), AST_RTP_ICE_COMPONENT_RTCP, TRANSPORT_SOCKET_RTCP);
8859 }
8860#endif
8861#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
8862 dtls_setup_rtcp(instance);
8863#endif
8864 } else {
8865 struct ast_sockaddr addr;
8866 /* RTCPMUX uses the same socket as RTP. If we were previously using standard RTCP
8867 * then close the socket we previously created.
8868 *
8869 * It may seem as though there is a possible race condition here where we might try
8870 * to close the RTCP socket while it is being used to send data. However, this is not
8871 * a problem in practice since setting and adjusting of RTCP properties happens prior
8872 * to activating RTP. It is not until RTP is activated that timers start for RTCP
8873 * transmission
8874 */
8875 if (rtp->rtcp->s > -1 && rtp->rtcp->s != rtp->s) {
8876 close(rtp->rtcp->s);
8877 }
8878 rtp->rtcp->s = rtp->s;
8879 ast_rtp_instance_get_remote_address(instance, &addr);
8880 ast_sockaddr_copy(&rtp->rtcp->them, &addr);
8881#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
8882 if (rtp->rtcp->dtls.ssl && rtp->rtcp->dtls.ssl != rtp->dtls.ssl) {
8883 SSL_free(rtp->rtcp->dtls.ssl);
8884 }
8885 rtp->rtcp->dtls.ssl = rtp->dtls.ssl;
8886#endif
8887 }
8888
8889 ast_debug_rtcp(1, "(%s) RTCP setup on RTP instance\n",
8891 } else {
8892 if (rtp->rtcp) {
8893 if (rtp->rtcp->schedid > -1) {
8894 ao2_unlock(instance);
8895 if (!ast_sched_del(rtp->sched, rtp->rtcp->schedid)) {
8896 /* Successfully cancelled scheduler entry. */
8897 ao2_ref(instance, -1);
8898 } else {
8899 /* Unable to cancel scheduler entry */
8900 ast_debug_rtcp(1, "(%p) RTCP failed to tear down RTCP\n", instance);
8901 ao2_lock(instance);
8902 return;
8903 }
8904 ao2_lock(instance);
8905 rtp->rtcp->schedid = -1;
8906 }
8907 if (rtp->transport_wide_cc.schedid > -1) {
8908 ao2_unlock(instance);
8909 if (!ast_sched_del(rtp->sched, rtp->transport_wide_cc.schedid)) {
8910 ao2_ref(instance, -1);
8911 } else {
8912 ast_debug_rtcp(1, "(%p) RTCP failed to tear down transport-cc feedback\n", instance);
8913 ao2_lock(instance);
8914 return;
8915 }
8916 ao2_lock(instance);
8917 rtp->transport_wide_cc.schedid = -1;
8918 }
8919 if (rtp->rtcp->s > -1 && rtp->rtcp->s != rtp->s) {
8920 close(rtp->rtcp->s);
8921 }
8922#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
8923 ao2_unlock(instance);
8924 dtls_srtp_stop_timeout_timer(instance, rtp, 1);
8925 ao2_lock(instance);
8926
8927 if (rtp->rtcp->dtls.ssl && rtp->rtcp->dtls.ssl != rtp->dtls.ssl) {
8928 SSL_free(rtp->rtcp->dtls.ssl);
8929 }
8930#endif
8932 ast_free(rtp->rtcp);
8933 rtp->rtcp = NULL;
8934 ast_debug_rtcp(1, "(%s) RTCP torn down on RTP instance\n",
8936 }
8937 }
8938 } else if (property == AST_RTP_PROPERTY_ASYMMETRIC_CODEC) {
8939 rtp->asymmetric_codec = value;
8940 } else if (property == AST_RTP_PROPERTY_RETRANS_SEND) {
8941 if (value) {
8942 if (!rtp->send_buffer) {
8944 }
8945 } else {
8946 if (rtp->send_buffer) {
8948 rtp->send_buffer = NULL;
8949 }
8950 }
8951 } else if (property == AST_RTP_PROPERTY_RETRANS_RECV) {
8952 if (value) {
8953 if (!rtp->recv_buffer) {
8956 }
8957 } else {
8958 if (rtp->recv_buffer) {
8960 rtp->recv_buffer = NULL;
8962 }
8963 }
8964 }
8965}
int ast_find_ourip(struct ast_sockaddr *ourip, const struct ast_sockaddr *bindaddr, int family)
Find our IP address.
Definition: acl.c:1051
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
void ast_free_ptr(void *ptr)
free() wrapper
Definition: astmm.c:1739
struct ast_data_buffer * ast_data_buffer_alloc(ast_data_buffer_free_callback free_fn, size_t size)
Allocate a data buffer.
Definition: data_buffer.c:145
#define ast_sockaddr_port(addr)
Get the port number of a socket address.
Definition: netsock2.h:517
int ast_sockaddr_is_ipv6(const struct ast_sockaddr *addr)
Determine if this is an IPv6 address.
Definition: netsock2.c:524
int ast_bind(int sockfd, const struct ast_sockaddr *addr)
Wrapper around bind(2) that uses struct ast_sockaddr.
Definition: netsock2.c:590
#define ast_sockaddr_set_port(addr, port)
Sets the port number of a socket address.
Definition: netsock2.h:532
#define DEFAULT_RTP_RECV_BUFFER_SIZE
static int create_new_socket(const char *type, int af)
#define DEFAULT_RTP_SEND_BUFFER_SIZE
@ AST_RTP_INSTANCE_RTCP_STANDARD
Definition: rtp_engine.h:284
void ast_rtp_instance_get_local_address(struct ast_rtp_instance *instance, struct ast_sockaddr *address)
Get the local address that we are expecting RTP on.
Definition: rtp_engine.c:664
@ AST_RTP_PROPERTY_RETRANS_RECV
Definition: rtp_engine.h:127
@ AST_RTP_PROPERTY_RETRANS_SEND
Definition: rtp_engine.h:129
@ AST_RTP_PROPERTY_RTCP
Definition: rtp_engine.h:123
@ AST_RTP_PROPERTY_ASYMMETRIC_CODEC
Definition: rtp_engine.h:125
int ast_sched_del(struct ast_sched_context *con, int id) attribute_warn_unused_result
Deletes a scheduled event.
Definition: sched.c:614
enum ast_rtp_instance_rtcp type
struct ast_sockaddr us
unsigned int asymmetric_codec
int value
Definition: syslog.c:37

References ao2_lock, ao2_ref, ao2_unlock, ast_bind(), ast_calloc, ast_data_buffer_alloc(), ast_data_buffer_free(), ast_debug_rtcp, ast_find_ourip(), ast_free, ast_free_ptr(), AST_RTP_ICE_COMPONENT_RTCP, ast_rtp_instance_get_channel_id(), ast_rtp_instance_get_data(), ast_rtp_instance_get_local_address(), ast_rtp_instance_get_remote_address, AST_RTP_INSTANCE_RTCP_STANDARD, AST_RTP_PROPERTY_ASYMMETRIC_CODEC, AST_RTP_PROPERTY_RETRANS_RECV, AST_RTP_PROPERTY_RETRANS_SEND, AST_RTP_PROPERTY_RTCP, ast_sched_del(), ast_sockaddr_copy(), ast_sockaddr_is_ipv4(), ast_sockaddr_is_ipv6(), ast_sockaddr_port, ast_sockaddr_set_port, ast_sockaddr_stringify(), ast_strdup, AST_VECTOR_FREE, AST_VECTOR_INIT, ast_rtp::asymmetric_codec, create_new_socket(), DEFAULT_RTP_RECV_BUFFER_SIZE, DEFAULT_RTP_SEND_BUFFER_SIZE, ast_rtcp::local_addr_str, ast_rtp::missing_seqno, NULL, ast_rtp::recv_buffer, ast_rtp::rtcp, ast_rtp::s, ast_rtcp::s, ast_rtp::sched, rtp_transport_wide_cc_statistics::schedid, ast_rtcp::schedid, ast_rtp::send_buffer, ast_rtcp::them, TRANSPORT_SOCKET_RTCP, ast_rtp::transport_wide_cc, ast_rtcp::type, ast_rtcp::us, and value.

◆ ast_rtp_qos_set()

static int ast_rtp_qos_set ( struct ast_rtp_instance instance,
int  tos,
int  cos,
const char *  desc 
)
static
Precondition
instance is locked

Definition at line 9298 of file res_rtp_asterisk.c.

9299{
9300 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
9301
9302 return ast_set_qos(rtp->s, tos, cos, desc);
9303}
static const char desc[]
Definition: cdr_radius.c:84
unsigned int tos
Definition: chan_iax2.c:355
unsigned int cos
Definition: chan_iax2.c:356
int ast_set_qos(int sockfd, int tos, int cos, const char *desc)
Set type of service.
Definition: netsock2.c:621

References ast_rtp_instance_get_data(), ast_set_qos(), cos, desc, ast_rtp::s, and tos.

◆ ast_rtp_read()

static struct ast_frame * ast_rtp_read ( struct ast_rtp_instance instance,
int  rtcp 
)
static
Precondition
instance is locked

Definition at line 8112 of file res_rtp_asterisk.c.

8113{
8114 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
8115 struct ast_srtp *srtp;
8117 struct ast_sockaddr addr;
8118 int res, hdrlen = 12, version, payloadtype;
8119 unsigned char *read_area = rtp->rawdata + AST_FRIENDLY_OFFSET;
8120 size_t read_area_size = sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET;
8121 unsigned int *rtpheader = (unsigned int*)(read_area), seqno, ssrc, timestamp, prev_seqno;
8122 struct ast_sockaddr remote_address = { {0,} };
8123 struct frame_list frames;
8124 struct ast_frame *frame;
8125 unsigned int bundled;
8126
8127 /* If this is actually RTCP let's hop on over and handle it */
8128 if (rtcp) {
8129 if (rtp->rtcp && rtp->rtcp->type == AST_RTP_INSTANCE_RTCP_STANDARD) {
8130 return ast_rtcp_read(instance);
8131 }
8132 return &ast_null_frame;
8133 }
8134
8135 /* Actually read in the data from the socket */
8136 if ((res = rtp_recvfrom(instance, read_area, read_area_size, 0,
8137 &addr)) < 0) {
8138 if (res == RTP_DTLS_ESTABLISHED) {
8141 return &rtp->f;
8142 }
8143
8144 ast_assert(errno != EBADF);
8145 if (errno != EAGAIN) {
8146 ast_log(LOG_WARNING, "RTP Read error: %s. Hanging up.\n",
8147 (errno) ? strerror(errno) : "Unspecified");
8148 return NULL;
8149 }
8150 return &ast_null_frame;
8151 }
8152
8153 /* If this was handled by the ICE session don't do anything */
8154 if (!res) {
8155 return &ast_null_frame;
8156 }
8157
8158 /* This could be a multiplexed RTCP packet. If so, be sure to interpret it correctly */
8159 if (rtcp_mux(rtp, read_area)) {
8160 return ast_rtcp_interpret(instance, ast_rtp_instance_get_srtp(instance, 1), read_area, res, &addr);
8161 }
8162
8163 /* Make sure the data that was read in is actually enough to make up an RTP packet */
8164 if (res < hdrlen) {
8165 /* If this is a keepalive containing only nulls, don't bother with a warning */
8166 int i;
8167 for (i = 0; i < res; ++i) {
8168 if (read_area[i] != '\0') {
8169 ast_log(LOG_WARNING, "RTP Read too short\n");
8170 return &ast_null_frame;
8171 }
8172 }
8173 return &ast_null_frame;
8174 }
8175
8176 /* Get fields and verify this is an RTP packet */
8177 seqno = ntohl(rtpheader[0]);
8178
8179 ast_rtp_instance_get_remote_address(instance, &remote_address);
8180
8181 if (!(version = (seqno & 0xC0000000) >> 30)) {
8182 struct sockaddr_in addr_tmp;
8183 struct ast_sockaddr addr_v4;
8184 if (ast_sockaddr_is_ipv4(&addr)) {
8185 ast_sockaddr_to_sin(&addr, &addr_tmp);
8186 } else if (ast_sockaddr_ipv4_mapped(&addr, &addr_v4)) {
8187 ast_debug_stun(1, "(%p) STUN using IPv6 mapped address %s\n",
8188 instance, ast_sockaddr_stringify(&addr));
8189 ast_sockaddr_to_sin(&addr_v4, &addr_tmp);
8190 } else {
8191 ast_debug_stun(1, "(%p) STUN cannot do for non IPv4 address %s\n",
8192 instance, ast_sockaddr_stringify(&addr));
8193 return &ast_null_frame;
8194 }
8195 if ((ast_stun_handle_packet(rtp->s, &addr_tmp, read_area, res, NULL, NULL) == AST_STUN_ACCEPT) &&
8196 ast_sockaddr_isnull(&remote_address)) {
8197 ast_sockaddr_from_sin(&addr, &addr_tmp);
8198 ast_rtp_instance_set_remote_address(instance, &addr);
8199 }
8200 return &ast_null_frame;
8201 }
8202
8203 /* If the version is not what we expected by this point then just drop the packet */
8204 if (version != 2) {
8205 return &ast_null_frame;
8206 }
8207
8208 /* We use the SSRC to determine what RTP instance this packet is actually for */
8209 ssrc = ntohl(rtpheader[2]);
8210
8211 /* We use the SRTP data from the provided instance that it came in on, not the child */
8212 srtp = ast_rtp_instance_get_srtp(instance, 0);
8213
8214 /* Determine the appropriate instance for this */
8215 child = rtp_find_instance_by_packet_source_ssrc(instance, rtp, ssrc);
8216 if (!child) {
8217 /* Neither the bundled parent nor any child has this SSRC */
8218 return &ast_null_frame;
8219 }
8220 if (child != instance) {
8221 /* It is safe to hold the child lock while holding the parent lock, we guarantee that the locking order
8222 * is always parent->child or that the child lock is not held when acquiring the parent lock.
8223 */
8224 ao2_lock(child);
8225 instance = child;
8226 rtp = ast_rtp_instance_get_data(instance);
8227 } else {
8228 /* The child is the parent! We don't need to unlock it. */
8229 child = NULL;
8230 }
8231
8232 /* If strict RTP protection is enabled see if we need to learn the remote address or if we need to drop the packet */
8233 switch (rtp->strict_rtp_state) {
8234 case STRICT_RTP_LEARN:
8235 /*
8236 * Scenario setup:
8237 * PartyA -- Ast1 -- Ast2 -- PartyB
8238 *
8239 * The learning timeout is necessary for Ast1 to handle the above
8240 * setup where PartyA calls PartyB and Ast2 initiates direct media
8241 * between Ast1 and PartyB. Ast1 may lock onto the Ast2 stream and
8242 * never learn the PartyB stream when it starts. The timeout makes
8243 * Ast1 stay in the learning state long enough to see and learn the
8244 * RTP stream from PartyB.
8245 *
8246 * To mitigate against attack, the learning state cannot switch
8247 * streams while there are competing streams. The competing streams
8248 * interfere with each other's qualification. Once we accept a
8249 * stream and reach the timeout, an attacker cannot interfere
8250 * anymore.
8251 *
8252 * Here are a few scenarios and each one assumes that the streams
8253 * are continuous:
8254 *
8255 * 1) We already have a known stream source address and the known
8256 * stream wants to change to a new source address. An attacking
8257 * stream will block learning the new stream source. After the
8258 * timeout we re-lock onto the original stream source address which
8259 * likely went away. The result is one way audio.
8260 *
8261 * 2) We already have a known stream source address and the known
8262 * stream doesn't want to change source addresses. An attacking
8263 * stream will not be able to replace the known stream. After the
8264 * timeout we re-lock onto the known stream. The call is not
8265 * affected.
8266 *
8267 * 3) We don't have a known stream source address. This presumably
8268 * is the start of a call. Competing streams will result in staying
8269 * in learning mode until a stream becomes the victor and we reach
8270 * the timeout. We cannot exit learning if we have no known stream
8271 * to lock onto. The result is one way audio until there is a victor.
8272 *
8273 * If we learn a stream source address before the timeout we will be
8274 * in scenario 1) or 2) when a competing stream starts.
8275 */
8278 ast_verb(4, "%p -- Strict RTP learning complete - Locking on source address %s\n",
8280 ast_test_suite_event_notify("STRICT_RTP_LEARN", "Source: %s",
8283 } else {
8284 struct ast_sockaddr target_address;
8285
8286 if (!ast_sockaddr_cmp(&rtp->strict_rtp_address, &addr)) {
8287 /*
8288 * We are open to learning a new address but have received
8289 * traffic from the current address, accept it and reset
8290 * the learning counts for a new source. When no more
8291 * current source packets arrive a new source can take over
8292 * once sufficient traffic is received.
8293 */
8295 break;
8296 }
8297
8298 /*
8299 * We give preferential treatment to the requested target address
8300 * (negotiated SDP address) where we are to send our RTP. However,
8301 * the other end has no obligation to send from that address even
8302 * though it is practically a requirement when NAT is involved.
8303 */
8304 ast_rtp_instance_get_requested_target_address(instance, &target_address);
8305 if (!ast_sockaddr_cmp(&target_address, &addr)) {
8306 /* Accept the negotiated target RTP stream as the source */
8307 ast_verb(4, "%p -- Strict RTP switching to RTP target address %s as source\n",
8308 rtp, ast_sockaddr_stringify(&addr));
8311 break;
8312 }
8313
8314 /*
8315 * Trying to learn a new address. If we pass a probationary period
8316 * with it, that means we've stopped getting RTP from the original
8317 * source and we should switch to it.
8318 */
8321 struct ast_rtp_codecs *codecs;
8322
8326 ast_verb(4, "%p -- Strict RTP qualifying stream type: %s\n",
8328 }
8329 if (!rtp_learning_rtp_seq_update(&rtp->rtp_source_learn, seqno)) {
8330 /* Accept the new RTP stream */
8331 ast_verb(4, "%p -- Strict RTP switching source address to %s\n",
8332 rtp, ast_sockaddr_stringify(&addr));
8335 break;
8336 }
8337 /* Not ready to accept the RTP stream candidate */
8338 ast_debug_rtp(1, "(%p) RTP %p -- Received packet from %s, dropping due to strict RTP protection. Will switch to it in %d packets.\n",
8339 instance, rtp, ast_sockaddr_stringify(&addr), rtp->rtp_source_learn.packets);
8340 } else {
8341 /*
8342 * This is either an attacking stream or
8343 * the start of the expected new stream.
8344 */
8347 ast_debug_rtp(1, "(%p) RTP %p -- Received packet from %s, dropping due to strict RTP protection. Qualifying new stream.\n",
8348 instance, rtp, ast_sockaddr_stringify(&addr));
8349 }
8350 return &ast_null_frame;
8351 }
8352 /* Fall through */
8353 case STRICT_RTP_CLOSED:
8354 /*
8355 * We should not allow a stream address change if the SSRC matches
8356 * once strictrtp learning is closed. Any kind of address change
8357 * like this should have happened while we were in the learning
8358 * state. We do not want to allow the possibility of an attacker
8359 * interfering with the RTP stream after the learning period.
8360 * An attacker could manage to get an RTCP packet redirected to
8361 * them which can contain the SSRC value.
8362 */
8363 if (!ast_sockaddr_cmp(&rtp->strict_rtp_address, &addr)) {
8364 break;
8365 }
8366 ast_debug_rtp(1, "(%p) RTP %p -- Received packet from %s, dropping due to strict RTP protection.\n",
8367 instance, rtp, ast_sockaddr_stringify(&addr));
8368#ifdef TEST_FRAMEWORK
8369 {
8370 static int strict_rtp_test_event = 1;
8371 if (strict_rtp_test_event) {
8372 ast_test_suite_event_notify("STRICT_RTP_CLOSED", "Source: %s",
8373 ast_sockaddr_stringify(&addr));
8374 strict_rtp_test_event = 0; /* Only run this event once to prevent possible spam */
8375 }
8376 }
8377#endif
8378 return &ast_null_frame;
8379 case STRICT_RTP_OPEN:
8380 break;
8381 }
8382
8383 /* If symmetric RTP is enabled see if the remote side is not what we expected and change where we are sending audio */
8385 if (ast_sockaddr_cmp(&remote_address, &addr)) {
8386 /* do not update the originally given address, but only the remote */
8388 ast_sockaddr_copy(&remote_address, &addr);
8389 if (rtp->rtcp && rtp->rtcp->type == AST_RTP_INSTANCE_RTCP_STANDARD) {
8390 ast_sockaddr_copy(&rtp->rtcp->them, &addr);
8392 }
8395 ast_debug(0, "(%p) RTP NAT: Got audio from other end. Now sending to address %s\n",
8396 instance, ast_sockaddr_stringify(&remote_address));
8397 }
8398 }
8399
8400 /* Pull out the various other fields we will need */
8401 payloadtype = (seqno & 0x7f0000) >> 16;
8402 seqno &= 0xffff;
8403 timestamp = ntohl(rtpheader[1]);
8404
8405#ifdef AST_DEVMODE
8406 if (should_drop_packets(&addr)) {
8407 ast_debug(0, "(%p) RTP: drop received packet from %s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6d)\n",
8408 instance, ast_sockaddr_stringify(&addr), payloadtype, seqno, timestamp, res - hdrlen);
8409 return &ast_null_frame;
8410 }
8411#endif
8412
8413 if (rtp_debug_test_addr(&addr)) {
8414 ast_verbose("Got RTP packet from %s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6d)\n",
8416 payloadtype, seqno, timestamp, res - hdrlen);
8417 }
8418
8420
8421 bundled = (child || AST_VECTOR_SIZE(&rtp->ssrc_mapping)) ? 1 : 0;
8422
8423 prev_seqno = rtp->lastrxseqno;
8424 /* We need to save lastrxseqno for use by jitter before resetting it. */
8425 rtp->prevrxseqno = rtp->lastrxseqno;
8426 rtp->lastrxseqno = seqno;
8427
8428 if (!rtp->recv_buffer) {
8429 /* If there is no receive buffer then we can pass back the frame directly */
8430 frame = ast_rtp_interpret(instance, srtp, &addr, read_area, res, prev_seqno, bundled);
8432 return AST_LIST_FIRST(&frames);
8433 } else if (rtp->expectedrxseqno == -1 || seqno == rtp->expectedrxseqno) {
8434 rtp->expectedrxseqno = seqno + 1;
8435
8436 /* We've cycled over, so go back to 0 */
8437 if (rtp->expectedrxseqno == SEQNO_CYCLE_OVER) {
8438 rtp->expectedrxseqno = 0;
8439 }
8440
8441 /* If there are no buffered packets that will be placed after this frame then we can
8442 * return it directly without duplicating it.
8443 */
8445 frame = ast_rtp_interpret(instance, srtp, &addr, read_area, res, prev_seqno, bundled);
8447 return AST_LIST_FIRST(&frames);
8448 }
8449
8452 ast_debug_rtp(2, "(%p) RTP Packet with sequence number '%d' on instance is no longer missing\n",
8453 instance, seqno);
8454 }
8455
8456 /* If we don't have the next packet after this we can directly return the frame, as there is no
8457 * chance it will be overwritten.
8458 */
8460 frame = ast_rtp_interpret(instance, srtp, &addr, read_area, res, prev_seqno, bundled);
8462 return AST_LIST_FIRST(&frames);
8463 }
8464
8465 /* Otherwise we need to dupe the frame so that the potential processing of frames placed after
8466 * it do not overwrite the data. You may be thinking that we could just add the current packet
8467 * to the head of the frames list and avoid having to duplicate it but this would result in out
8468 * of order packet processing by libsrtp which we are trying to avoid.
8469 */
8470 frame = ast_frdup(ast_rtp_interpret(instance, srtp, &addr, read_area, res, prev_seqno, bundled));
8471 if (frame) {
8473 prev_seqno = seqno;
8474 }
8475
8476 /* Add any additional packets that we have buffered and that are available */
8477 while (ast_data_buffer_count(rtp->recv_buffer)) {
8478 struct ast_rtp_rtcp_nack_payload *payload;
8479
8481 if (!payload) {
8482 break;
8483 }
8484
8485 frame = ast_frdup(ast_rtp_interpret(instance, srtp, &addr, payload->buf, payload->size, prev_seqno, bundled));
8486 ast_free(payload);
8487
8488 if (!frame) {
8489 /* If this packet can't be interpreted due to being out of memory we return what we have and assume
8490 * that we will determine it is a missing packet later and NACK for it.
8491 */
8492 return AST_LIST_FIRST(&frames);
8493 }
8494
8495 ast_debug_rtp(2, "(%p) RTP pulled buffered packet with sequence number '%d' to additionally return\n",
8496 instance, frame->seqno);
8498 prev_seqno = rtp->expectedrxseqno;
8499 rtp->expectedrxseqno++;
8500 if (rtp->expectedrxseqno == SEQNO_CYCLE_OVER) {
8501 rtp->expectedrxseqno = 0;
8502 }
8503 }
8504
8505 return AST_LIST_FIRST(&frames);
8506 } else if ((((seqno - rtp->expectedrxseqno) > 100) && timestamp > rtp->lastividtimestamp) ||
8508 int inserted = 0;
8509
8510 /* We have a large number of outstanding buffered packets or we've jumped far ahead in time.
8511 * To compensate we dump what we have in the buffer and place the current packet in a logical
8512 * spot. In the case of video we also require a full frame to give the decoding side a fighting
8513 * chance.
8514 */
8515
8517 ast_debug_rtp(2, "(%p) RTP source has wild gap or packet loss, sending FIR\n",
8518 instance);
8519 rtp_write_rtcp_fir(instance, rtp, &remote_address);
8520 }
8521
8522 /* This works by going through the progression of the sequence number retrieving buffered packets
8523 * or inserting the current received packet until we've run out of packets. This ensures that the
8524 * packets are in the correct sequence number order.
8525 */
8526 while (ast_data_buffer_count(rtp->recv_buffer)) {
8527 struct ast_rtp_rtcp_nack_payload *payload;
8528
8529 /* If the packet we received is the one we are expecting at this point then add it in */
8530 if (rtp->expectedrxseqno == seqno) {
8531 frame = ast_frdup(ast_rtp_interpret(instance, srtp, &addr, read_area, res, prev_seqno, bundled));
8532 if (frame) {
8534 prev_seqno = seqno;
8535 ast_debug_rtp(2, "(%p) RTP inserted just received packet with sequence number '%d' in correct order\n",
8536 instance, seqno);
8537 }
8538 /* It is possible due to packet retransmission for this packet to also exist in the receive
8539 * buffer so we explicitly remove it in case this occurs, otherwise the receive buffer will
8540 * never be empty.
8541 */
8542 payload = (struct ast_rtp_rtcp_nack_payload *)ast_data_buffer_remove(rtp->recv_buffer, seqno);
8543 if (payload) {
8544 ast_free(payload);
8545 }
8546 rtp->expectedrxseqno++;
8547 if (rtp->expectedrxseqno == SEQNO_CYCLE_OVER) {
8548 rtp->expectedrxseqno = 0;
8549 }
8550 inserted = 1;
8551 continue;
8552 }
8553
8555 if (payload) {
8556 frame = ast_frdup(ast_rtp_interpret(instance, srtp, &addr, payload->buf, payload->size, prev_seqno, bundled));
8557 if (frame) {
8559 prev_seqno = rtp->expectedrxseqno;
8560 ast_debug_rtp(2, "(%p) RTP emptying queue and returning packet with sequence number '%d'\n",
8561 instance, frame->seqno);
8562 }
8563 ast_free(payload);
8564 }
8565
8566 rtp->expectedrxseqno++;
8567 if (rtp->expectedrxseqno == SEQNO_CYCLE_OVER) {
8568 rtp->expectedrxseqno = 0;
8569 }
8570 }
8571
8572 if (!inserted) {
8573 /* This current packet goes after them, and we assume that packets going forward will follow
8574 * that new sequence number increment. It is okay for this to not be duplicated as it is guaranteed
8575 * to be the last packet processed right now and it is also guaranteed that it will always return
8576 * non-NULL.
8577 */
8578 frame = ast_rtp_interpret(instance, srtp, &addr, read_area, res, prev_seqno, bundled);
8580 rtp->expectedrxseqno = seqno + 1;
8581 if (rtp->expectedrxseqno == SEQNO_CYCLE_OVER) {
8582 rtp->expectedrxseqno = 0;
8583 }
8584
8585 ast_debug_rtp(2, "(%p) RTP adding just received packet with sequence number '%d' to end of dumped queue\n",
8586 instance, seqno);
8587 }
8588
8589 /* When we flush increase our chance for next time by growing the receive buffer when possible
8590 * by how many packets we missed, to give ourselves a bit more breathing room.
8591 */
8594 ast_debug_rtp(2, "(%p) RTP receive buffer is now at maximum of %zu\n", instance, ast_data_buffer_max(rtp->recv_buffer));
8595
8596 /* As there is such a large gap we don't want to flood the order side with missing packets, so we
8597 * give up and start anew.
8598 */
8600
8601 return AST_LIST_FIRST(&frames);
8602 }
8603
8604 /* We're finished with the frames list */
8606
8607 /* Determine if the received packet is from the last OLD_PACKET_COUNT (1000 by default) packets or not.
8608 * For the case where the received sequence number exceeds that of the expected sequence number we calculate
8609 * the past sequence number that would be 1000 sequence numbers ago. If the received sequence number
8610 * exceeds or meets that then it is within OLD_PACKET_COUNT packets ago. For example if the expected
8611 * sequence number is 100 and we receive 65530, then it would be considered old. This is because
8612 * 65535 - 1000 + 100 = 64635 which gives us the sequence number at which we would consider the packets
8613 * old. Since 65530 is above that, it would be considered old.
8614 * For the case where the received sequence number is less than the expected sequence number we can do
8615 * a simple subtraction to see if it is 1000 packets ago or not.
8616 */
8617 if ((seqno < rtp->expectedrxseqno && ((rtp->expectedrxseqno - seqno) <= OLD_PACKET_COUNT)) ||
8618 (seqno > rtp->expectedrxseqno && (seqno >= (65535 - OLD_PACKET_COUNT + rtp->expectedrxseqno)))) {
8619 /* If this is a packet from the past then we have received a duplicate packet, so just drop it */
8620 ast_debug_rtp(2, "(%p) RTP received an old packet with sequence number '%d', dropping it\n",
8621 instance, seqno);
8622 return &ast_null_frame;
8623 } else if (ast_data_buffer_get(rtp->recv_buffer, seqno)) {
8624 /* If this is a packet we already have buffered then it is a duplicate, so just drop it */
8625 ast_debug_rtp(2, "(%p) RTP received a duplicate transmission of packet with sequence number '%d', dropping it\n",
8626 instance, seqno);
8627 return &ast_null_frame;
8628 } else {
8629 /* This is an out of order packet from the future */
8630 struct ast_rtp_rtcp_nack_payload *payload;
8631 int missing_seqno;
8632 int remove_failed;
8633 unsigned int missing_seqnos_added = 0;
8634
8635 ast_debug_rtp(2, "(%p) RTP received an out of order packet with sequence number '%d' while expecting '%d' from the future\n",
8636 instance, seqno, rtp->expectedrxseqno);
8637
8638 payload = ast_malloc(sizeof(*payload) + res);
8639 if (!payload) {
8640 /* If the payload can't be allocated then we can't defer this packet right now.
8641 * Instead of dumping what we have we pretend we lost this packet. It will then
8642 * get NACKed later or the existing buffer will be returned entirely. Well, we may
8643 * try since we're seemingly out of memory. It's a bad situation all around and
8644 * packets are likely to get lost anyway.
8645 */
8646 return &ast_null_frame;
8647 }
8648
8649 payload->size = res;
8650 memcpy(payload->buf, rtpheader, res);
8651 if (ast_data_buffer_put(rtp->recv_buffer, seqno, payload) == -1) {
8652 ast_free(payload);
8653 }
8654
8655 /* If this sequence number is removed that means we had a gap and this packet has filled it in
8656 * some. Since it was part of the gap we will have already added any other missing sequence numbers
8657 * before it (and possibly after it) to the vector so we don't need to do that again. Note that
8658 * remove_failed will be set to -1 if the sequence number isn't removed, and 0 if it is.
8659 */
8660 remove_failed = AST_VECTOR_REMOVE_CMP_ORDERED(&rtp->missing_seqno, seqno, find_by_value,
8662 if (!remove_failed) {
8663 ast_debug_rtp(2, "(%p) RTP packet with sequence number '%d' is no longer missing\n",
8664 instance, seqno);
8665 }
8666
8667 /* The missing sequence number code works by taking the sequence number of the
8668 * packet we've just received and going backwards until we hit the sequence number
8669 * of the last packet we've received. While doing so we check to make sure that the
8670 * sequence number is not already missing and that it is not already buffered.
8671 */
8672 missing_seqno = seqno;
8673 while (remove_failed) {
8674 missing_seqno -= 1;
8675
8676 /* If we've cycled backwards then start back at the top */
8677 if (missing_seqno < 0) {
8678 missing_seqno = 65535;
8679 }
8680
8681 /* We've gone backwards enough such that we've hit the previous sequence number */
8682 if (missing_seqno == prev_seqno) {
8683 break;
8684 }
8685
8686 /* We don't want missing sequence number duplicates. If, for some reason,
8687 * packets are really out of order, we could end up in this scenario:
8688 *
8689 * We are expecting sequence number 100
8690 * We receive sequence number 105
8691 * Sequence numbers 100 through 104 get added to the vector
8692 * We receive sequence number 101 (this section is skipped)
8693 * We receive sequence number 103
8694 * Sequence number 102 is added to the vector
8695 *
8696 * This will prevent the duplicate from being added.
8697 */
8698 if (AST_VECTOR_GET_CMP(&rtp->missing_seqno, missing_seqno,
8699 find_by_value)) {
8700 continue;
8701 }
8702
8703 /* If this packet has been buffered already then don't count it amongst the
8704 * missing.
8705 */
8706 if (ast_data_buffer_get(rtp->recv_buffer, missing_seqno)) {
8707 continue;
8708 }
8709
8710 ast_debug_rtp(2, "(%p) RTP added missing sequence number '%d'\n",
8711 instance, missing_seqno);
8712 AST_VECTOR_ADD_SORTED(&rtp->missing_seqno, missing_seqno,
8714 missing_seqnos_added++;
8715 }
8716
8717 /* When we add a large number of missing sequence numbers we assume there was a substantial
8718 * gap in reception so we trigger an immediate NACK. When our data buffer is 1/4 full we
8719 * assume that the packets aren't just out of order but have actually been lost. At 1/2
8720 * full we get more aggressive and ask for retransmission when we get a new packet.
8721 * To get them back we construct and send a NACK causing the sender to retransmit them.
8722 */
8723 if (missing_seqnos_added >= MISSING_SEQNOS_ADDED_TRIGGER ||
8726 int packet_len = 0;
8727 int res = 0;
8728 int ice;
8729 int sr;
8730 size_t data_size = AST_UUID_STR_LEN + 128 + (AST_VECTOR_SIZE(&rtp->missing_seqno) * 4);
8731 RAII_VAR(unsigned char *, rtcpheader, NULL, ast_free_ptr);
8732 RAII_VAR(struct ast_rtp_rtcp_report *, rtcp_report,
8734 ao2_cleanup);
8735
8736 /* Sufficient space for RTCP headers and report, SDES with CNAME, NACK header,
8737 * and worst case 4 bytes per missing sequence number.
8738 */
8739 rtcpheader = ast_malloc(sizeof(*rtcpheader) + data_size);
8740 if (!rtcpheader) {
8741 ast_debug_rtcp(1, "(%p) RTCP failed to allocate memory for NACK\n", instance);
8742 return &ast_null_frame;
8743 }
8744
8745 memset(rtcpheader, 0, data_size);
8746
8747 res = ast_rtcp_generate_compound_prefix(instance, rtcpheader, rtcp_report, &sr);
8748
8749 if (res == 0 || res == 1) {
8750 return &ast_null_frame;
8751 }
8752
8753 packet_len += res;
8754
8755 res = ast_rtcp_generate_nack(instance, rtcpheader + packet_len);
8756
8757 if (res == 0) {
8758 ast_debug_rtcp(1, "(%p) RTCP failed to construct NACK, stopping here\n", instance);
8759 return &ast_null_frame;
8760 }
8761
8762 packet_len += res;
8763
8764 res = rtcp_sendto(instance, rtcpheader, packet_len, 0, &remote_address, &ice);
8765 if (res < 0) {
8766 ast_debug_rtcp(1, "(%p) RTCP failed to send NACK request out\n", instance);
8767 } else {
8768 ast_debug_rtcp(2, "(%p) RTCP sending a NACK request to get missing packets\n", instance);
8769 /* Update RTCP SR/RR statistics */
8770 ast_rtcp_calculate_sr_rr_statistics(instance, rtcp_report, remote_address, ice, sr);
8771 }
8772 }
8773 }
8774
8775 return &ast_null_frame;
8776}
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
static char version[AST_MAX_EXTENSION]
Definition: chan_ooh323.c:391
static struct ao2_container * codecs
Registered codecs.
Definition: codec.c:48
@ AST_MEDIA_TYPE_UNKNOWN
Definition: codec.h:31
void * ast_data_buffer_get(const struct ast_data_buffer *buffer, size_t pos)
Retrieve a data payload from the data buffer.
Definition: data_buffer.c:269
size_t ast_data_buffer_count(const struct ast_data_buffer *buffer)
Return the number of payloads in a data buffer.
Definition: data_buffer.c:356
void ast_data_buffer_resize(struct ast_data_buffer *buffer, size_t size)
Resize a data buffer.
Definition: data_buffer.c:168
int ast_data_buffer_put(struct ast_data_buffer *buffer, size_t pos, void *payload)
Place a data payload at a position in the data buffer.
Definition: data_buffer.c:203
size_t ast_data_buffer_max(const struct ast_data_buffer *buffer)
Return the maximum number of payloads a data buffer can hold.
Definition: data_buffer.c:363
void * ast_data_buffer_remove(struct ast_data_buffer *buffer, size_t pos)
Remove a data payload from the data buffer.
Definition: data_buffer.c:299
void ast_frame_free(struct ast_frame *frame, int cache)
Frees a frame or list of frames.
Definition: main/frame.c:176
#define ast_frdup(fr)
Copies a frame.
#define ast_verb(level,...)
#define OLD_PACKET_COUNT
static int rtp_learning_rtp_seq_update(struct rtp_learning_info *info, uint16_t seq)
static struct ast_frame * ast_rtcp_read(struct ast_rtp_instance *instance)
static int ast_rtcp_generate_nack(struct ast_rtp_instance *instance, unsigned char *rtcpheader)
static int compare_by_value(int elem, int value)
Helper function to compare an elem in a vector by value.
#define MAXIMUM_RTP_RECV_BUFFER_SIZE
#define STRICT_RTP_LEARN_TIMEOUT
Strict RTP learning timeout time in milliseconds.
static void rtp_instance_unlock(struct ast_rtp_instance *instance)
static int rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa)
#define MISSING_SEQNOS_ADDED_TRIGGER
#define FLAG_NAT_ACTIVE
static int rtcp_mux(struct ast_rtp *rtp, const unsigned char *packet)
static void rtp_learning_seq_init(struct rtp_learning_info *info, uint16_t seq)
static struct ast_frame * ast_rtp_interpret(struct ast_rtp_instance *instance, struct ast_srtp *srtp, const struct ast_sockaddr *remote_address, unsigned char *read_area, int length, int prev_seqno, unsigned int bundled)
static void rtp_write_rtcp_fir(struct ast_rtp_instance *instance, struct ast_rtp *rtp, struct ast_sockaddr *remote_address)
enum ast_media_type ast_rtp_codecs_get_stream_type(struct ast_rtp_codecs *codecs)
Determine the type of RTP stream media from the codecs mapped.
Definition: rtp_engine.c:1473
void ast_rtp_instance_get_requested_target_address(struct ast_rtp_instance *instance, struct ast_sockaddr *address)
Get the requested target address of the remote endpoint.
Definition: rtp_engine.c:694
int ast_rtp_instance_set_incoming_source_address(struct ast_rtp_instance *instance, const struct ast_sockaddr *address)
Set the incoming source address of the remote endpoint that we are sending RTP to.
Definition: rtp_engine.c:627
Structure for storing RTP packets for retransmission.
struct ast_sockaddr strict_rtp_address
unsigned char rawdata[8192+AST_FRIENDLY_OFFSET]
struct rtp_learning_info rtp_source_learn
enum ast_media_type stream_type
struct ast_sockaddr proposed_address
struct timeval start
#define ast_test_suite_event_notify(s, f,...)
Definition: test.h:189
#define MIN(a, b)
Definition: utils.h:231
#define AST_VECTOR_RESET(vec, cleanup)
Reset vector.
Definition: vector.h:625
#define AST_VECTOR_REMOVE_CMP_ORDERED(vec, value, cmp, cleanup)
Remove an element from a vector that matches the given comparison while maintaining order.
Definition: vector.h:540
#define AST_VECTOR_ADD_SORTED(vec, elem, cmp)
Add an element into a sorted vector.
Definition: vector.h:371

References ao2_cleanup, ao2_lock, ast_assert, ast_codec_media_type2str(), AST_CONTROL_SRCCHANGE, ast_data_buffer_count(), ast_data_buffer_get(), ast_data_buffer_max(), ast_data_buffer_put(), ast_data_buffer_remove(), ast_data_buffer_resize(), ast_debug, ast_debug_rtcp, ast_debug_rtp, ast_debug_rtp_packet_is_allowed, ast_debug_stun, AST_FRAME_CONTROL, ast_frame_free(), ast_frdup, ast_free, ast_free_ptr(), AST_FRIENDLY_OFFSET, AST_LIST_FIRST, AST_LIST_HEAD_INIT_NOLOCK, AST_LIST_INSERT_TAIL, ast_log, ast_malloc, AST_MEDIA_TYPE_UNKNOWN, AST_MEDIA_TYPE_VIDEO, ast_null_frame, ast_rtcp_calculate_sr_rr_statistics(), ast_rtcp_generate_compound_prefix(), ast_rtcp_generate_nack(), ast_rtcp_interpret(), ast_rtcp_read(), ast_rtp_codecs_get_stream_type(), ast_rtp_instance_get_codecs(), ast_rtp_instance_get_data(), ast_rtp_instance_get_prop(), ast_rtp_instance_get_remote_address, ast_rtp_instance_get_requested_target_address(), ast_rtp_instance_get_srtp(), AST_RTP_INSTANCE_RTCP_STANDARD, ast_rtp_instance_set_incoming_source_address(), ast_rtp_instance_set_remote_address, ast_rtp_interpret(), AST_RTP_PROPERTY_NAT, ast_rtp_rtcp_report_alloc(), ast_set_flag, ast_sockaddr_cmp(), ast_sockaddr_copy(), ast_sockaddr_from_sin, ast_sockaddr_ipv4_mapped(), ast_sockaddr_is_ipv4(), ast_sockaddr_isnull(), ast_sockaddr_port, ast_sockaddr_set_port, ast_sockaddr_stringify(), ast_sockaddr_to_sin, AST_STUN_ACCEPT, ast_stun_handle_packet(), ast_test_suite_event_notify, ast_tvdiff_ms(), ast_tvnow(), AST_UUID_STR_LEN, AST_VECTOR_ADD_SORTED, AST_VECTOR_ELEM_CLEANUP_NOOP, AST_VECTOR_GET_CMP, AST_VECTOR_REMOVE_CMP_ORDERED, AST_VECTOR_RESET, AST_VECTOR_SIZE, ast_verb, ast_verbose(), ast_rtp_rtcp_nack_payload::buf, codecs, compare_by_value(), errno, ast_rtp::expectedrxseqno, ast_rtp::f, find_by_value(), FLAG_NAT_ACTIVE, frames, ast_frame::frametype, ast_frame_subclass::integer, ast_rtp::lastividtimestamp, ast_rtp::lastrxseqno, LOG_WARNING, MAXIMUM_RTP_RECV_BUFFER_SIZE, MIN, ast_rtp::missing_seqno, MISSING_SEQNOS_ADDED_TRIGGER, NULL, OLD_PACKET_COUNT, rtp_learning_info::packets, ast_rtp::prevrxseqno, rtp_learning_info::proposed_address, RAII_VAR, ast_rtp::rawdata, ast_rtp::recv_buffer, ast_rtp::rtcp, rtcp_mux(), rtcp_sendto(), rtp_debug_test_addr(), RTP_DTLS_ESTABLISHED, rtp_find_instance_by_packet_source_ssrc(), rtp_instance_unlock(), rtp_learning_rtp_seq_update(), rtp_learning_seq_init(), rtp_recvfrom(), ast_rtp::rtp_source_learn, rtp_write_rtcp_fir(), ast_rtp::s, ast_frame::seqno, SEQNO_CYCLE_OVER, ast_rtp_rtcp_nack_payload::size, ast_rtp::ssrc_mapping, rtp_learning_info::start, rtp_learning_info::stream_type, ast_rtp::strict_rtp_address, STRICT_RTP_CLOSED, STRICT_RTP_LEARN, STRICT_RTP_LEARN_TIMEOUT, STRICT_RTP_OPEN, ast_rtp::strict_rtp_state, ast_frame::subclass, ast_rtcp::them, ast_rtp::themssrc_valid, ast_rtcp::type, and version.

◆ ast_rtp_remote_address_set()

static void ast_rtp_remote_address_set ( struct ast_rtp_instance instance,
struct ast_sockaddr addr 
)
static
Precondition
instance is locked

Definition at line 8976 of file res_rtp_asterisk.c.

8977{
8978 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
8979 struct ast_sockaddr local;
8980 int index;
8981
8982 ast_rtp_instance_get_local_address(instance, &local);
8983 if (!ast_sockaddr_isnull(addr)) {
8984 /* Update the local RTP address with what is being used */
8985 if (ast_ouraddrfor(addr, &local)) {
8986 /* Failed to update our address so reuse old local address */
8987 ast_rtp_instance_get_local_address(instance, &local);
8988 } else {
8989 ast_rtp_instance_set_local_address(instance, &local);
8990 }
8991 }
8992
8993 if (rtp->rtcp && !ast_sockaddr_isnull(addr)) {
8994 ast_debug_rtcp(1, "(%p) RTCP setting address on RTP instance\n", instance);
8995 ast_sockaddr_copy(&rtp->rtcp->them, addr);
8996
8999
9000 /* Update the local RTCP address with what is being used */
9001 ast_sockaddr_set_port(&local, ast_sockaddr_port(&local) + 1);
9002 }
9003 ast_sockaddr_copy(&rtp->rtcp->us, &local);
9004
9007 }
9008
9009 /* Update any bundled RTP instances */
9010 for (index = 0; index < AST_VECTOR_SIZE(&rtp->ssrc_mapping); ++index) {
9011 struct rtp_ssrc_mapping *mapping = AST_VECTOR_GET_ADDR(&rtp->ssrc_mapping, index);
9012
9014 }
9015
9016 /* Need to reset the DTMF last sequence number and the timestamp of the last END packet */
9017 rtp->last_seqno = 0;
9018 rtp->last_end_timestamp.ts = 0;
9019 rtp->last_end_timestamp.is_set = 0;
9020
9022 && !ast_sockaddr_isnull(addr) && ast_sockaddr_cmp(addr, &rtp->strict_rtp_address)) {
9023 /* We only need to learn a new strict source address if we've been told the source is
9024 * changing to something different.
9025 */
9026 ast_verb(4, "%p -- Strict RTP learning after remote address set to: %s\n",
9027 rtp, ast_sockaddr_stringify(addr));
9028 rtp_learning_start(rtp);
9029 }
9030}
int ast_ouraddrfor(const struct ast_sockaddr *them, struct ast_sockaddr *us)
Get our local IP address when contacting a remote host.
Definition: acl.c:1004
static int strictrtp
static void rtp_learning_start(struct ast_rtp *rtp)
Start the strictrtp learning mode.
int ast_rtp_instance_set_local_address(struct ast_rtp_instance *instance, const struct ast_sockaddr *address)
Set the address that we are expecting to receive RTP on.
Definition: rtp_engine.c:609

References ast_debug_rtcp, ast_free, ast_ouraddrfor(), ast_rtp_instance_get_data(), ast_rtp_instance_get_local_address(), AST_RTP_INSTANCE_RTCP_STANDARD, ast_rtp_instance_set_local_address(), ast_rtp_instance_set_remote_address, ast_sockaddr_cmp(), ast_sockaddr_copy(), ast_sockaddr_isnull(), ast_sockaddr_port, ast_sockaddr_set_port, ast_sockaddr_stringify(), ast_strdup, AST_VECTOR_GET_ADDR, AST_VECTOR_SIZE, ast_verb, rtp_ssrc_mapping::instance, optional_ts::is_set, ast_rtp::last_end_timestamp, ast_rtp::last_seqno, ast_rtcp::local_addr_str, ast_rtp::rtcp, rtp_learning_start(), ast_rtp::ssrc_mapping, ast_rtp::strict_rtp_address, STRICT_RTP_OPEN, ast_rtp::strict_rtp_state, strictrtp, ast_rtcp::them, optional_ts::ts, ast_rtcp::type, and ast_rtcp::us.

◆ ast_rtp_rtcp_handle_nack()

static int ast_rtp_rtcp_handle_nack ( struct ast_rtp_instance instance,
unsigned int *  nackdata,
unsigned int  position,
unsigned int  length 
)
static
Precondition
instance is locked

Definition at line 6420 of file res_rtp_asterisk.c.

6422{
6423 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
6424 int res = 0;
6425 int blp_index;
6426 int packet_index;
6427 int ice;
6428 struct ast_rtp_rtcp_nack_payload *payload;
6429 unsigned int current_word;
6430 unsigned int pid; /* Packet ID which refers to seqno of lost packet */
6431 unsigned int blp; /* Bitmask of following lost packets */
6432 struct ast_sockaddr remote_address = { {0,} };
6433 int abs_send_time_id;
6434 unsigned int now_msw = 0;
6435 unsigned int now_lsw = 0;
6436 unsigned int packets_not_found = 0;
6437
6438 if (!rtp->send_buffer) {
6439 ast_debug_rtcp(1, "(%p) RTCP tried to handle NACK request, "
6440 "but we don't have a RTP packet storage!\n", instance);
6441 return res;
6442 }
6443
6445 if (abs_send_time_id != -1) {
6446 timeval2ntp(ast_tvnow(), &now_msw, &now_lsw);
6447 }
6448
6449 ast_rtp_instance_get_remote_address(instance, &remote_address);
6450
6451 /*
6452 * We use index 3 because with feedback messages, the FCI (Feedback Control Information)
6453 * does not begin until after the version, packet SSRC, and media SSRC words.
6454 */
6455 for (packet_index = 3; packet_index < length; packet_index++) {
6456 current_word = ntohl(nackdata[position + packet_index]);
6457 pid = current_word >> 16;
6458 /* We know the remote end is missing this packet. Go ahead and send it if we still have it. */
6459 payload = (struct ast_rtp_rtcp_nack_payload *)ast_data_buffer_get(rtp->send_buffer, pid);
6460 if (payload) {
6461 if (abs_send_time_id != -1) {
6462 /* On retransmission we need to update the timestamp within the packet, as it
6463 * is supposed to contain when the packet was actually sent.
6464 */
6465 put_unaligned_time24(payload->buf + 17, now_msw, now_lsw);
6466 }
6467 res += rtp_sendto(instance, payload->buf, payload->size, 0, &remote_address, &ice);
6468 } else {
6469 ast_debug_rtcp(1, "(%p) RTCP received NACK request for RTP packet with seqno %d, "
6470 "but we don't have it\n", instance, pid);
6471 packets_not_found++;
6472 }
6473 /*
6474 * The bitmask. Denoting the least significant bit as 1 and its most significant bit
6475 * as 16, then bit i of the bitmask is set to 1 if the receiver has not received RTP
6476 * packet (pid+i)(modulo 2^16). Otherwise, it is set to 0. We cannot assume bits set
6477 * to 0 after a bit set to 1 have actually been received.
6478 */
6479 blp = current_word & 0xffff;
6480 blp_index = 1;
6481 while (blp) {
6482 if (blp & 1) {
6483 /* Packet (pid + i)(modulo 2^16) is missing too. */
6484 unsigned int seqno = (pid + blp_index) % 65536;
6485 payload = (struct ast_rtp_rtcp_nack_payload *)ast_data_buffer_get(rtp->send_buffer, seqno);
6486 if (payload) {
6487 if (abs_send_time_id != -1) {
6488 put_unaligned_time24(payload->buf + 17, now_msw, now_lsw);
6489 }
6490 res += rtp_sendto(instance, payload->buf, payload->size, 0, &remote_address, &ice);
6491 } else {
6492 ast_debug_rtcp(1, "(%p) RTCP remote end also requested RTP packet with seqno %d, "
6493 "but we don't have it\n", instance, seqno);
6494 packets_not_found++;
6495 }
6496 }
6497 blp >>= 1;
6498 blp_index++;
6499 }
6500 }
6501
6502 if (packets_not_found) {
6503 /* Grow the send buffer based on how many packets were not found in the buffer, but
6504 * enforce a maximum.
6505 */
6507 ast_data_buffer_max(rtp->send_buffer) + packets_not_found));
6508 ast_debug_rtcp(2, "(%p) RTCP send buffer on RTP instance is now at maximum of %zu\n",
6509 instance, ast_data_buffer_max(rtp->send_buffer));
6510 }
6511
6512 return res;
6513}
static void put_unaligned_time24(void *p, uint32_t time_msw, uint32_t time_lsw)
#define MAXIMUM_RTP_SEND_BUFFER_SIZE
int ast_rtp_instance_extmap_get_id(struct ast_rtp_instance *instance, enum ast_rtp_extension extension)
Retrieve the id for an RTP extension.
Definition: rtp_engine.c:907

References ast_data_buffer_get(), ast_data_buffer_max(), ast_data_buffer_resize(), ast_debug_rtcp, AST_RTP_EXTENSION_ABS_SEND_TIME, ast_rtp_instance_extmap_get_id(), ast_rtp_instance_get_data(), ast_rtp_instance_get_remote_address, ast_tvnow(), ast_rtp_rtcp_nack_payload::buf, MAXIMUM_RTP_SEND_BUFFER_SIZE, MIN, put_unaligned_time24(), rtp_sendto(), ast_rtp::send_buffer, ast_rtp_rtcp_nack_payload::size, and timeval2ntp().

Referenced by ast_rtcp_interpret().

◆ ast_rtp_sendcng()

static int ast_rtp_sendcng ( struct ast_rtp_instance instance,
int  level 
)
static

generate comfort noice (CNG)

Precondition
instance is locked

Definition at line 9310 of file res_rtp_asterisk.c.

9311{
9312 unsigned int *rtpheader;
9313 int hdrlen = 12;
9314 int res, payload = 0;
9315 char data[256];
9316 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
9317 struct ast_sockaddr remote_address = { {0,} };
9318 int ice;
9319
9320 ast_rtp_instance_get_remote_address(instance, &remote_address);
9321
9322 if (ast_sockaddr_isnull(&remote_address)) {
9323 return -1;
9324 }
9325
9327
9328 level = 127 - (level & 0x7f);
9329
9330 rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
9331
9332 /* Get a pointer to the header */
9333 rtpheader = (unsigned int *)data;
9334 rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno));
9335 rtpheader[1] = htonl(rtp->lastts);
9336 rtpheader[2] = htonl(rtp->ssrc);
9337 data[12] = level;
9338
9339 res = rtp_sendto(instance, (void *) rtpheader, hdrlen + 1, 0, &remote_address, &ice);
9340
9341 if (res < 0) {
9342 ast_log(LOG_ERROR, "RTP Comfort Noise Transmission error to %s: %s\n", ast_sockaddr_stringify(&remote_address), strerror(errno));
9343 return res;
9344 }
9345
9346 if (rtp_debug_test_addr(&remote_address)) {
9347 ast_verbose("Sent Comfort Noise RTP packet to %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n",
9348 ast_sockaddr_stringify(&remote_address),
9349 ice ? " (via ICE)" : "",
9350 AST_RTP_CN, rtp->seqno, rtp->lastdigitts, res - hdrlen);
9351 }
9352
9353 rtp->seqno++;
9354
9355 return res;
9356}

References ast_log, AST_RTP_CN, ast_rtp_codecs_payload_code_tx(), ast_rtp_instance_get_codecs(), ast_rtp_instance_get_data(), ast_rtp_instance_get_remote_address, ast_sockaddr_isnull(), ast_sockaddr_stringify(), ast_tv(), ast_tvadd(), ast_tvnow(), ast_verbose(), ast_rtp::dtmfmute, errno, ast_rtp::lastdigitts, ast_rtp::lastts, LOG_ERROR, NULL, rtp_debug_test_addr(), rtp_sendto(), ast_rtp::seqno, and ast_rtp::ssrc.

◆ ast_rtp_set_remote_ssrc()

static void ast_rtp_set_remote_ssrc ( struct ast_rtp_instance instance,
unsigned int  ssrc 
)
static
Precondition
instance is locked

Definition at line 9375 of file res_rtp_asterisk.c.

9376{
9377 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
9378
9379 if (rtp->themssrc_valid && rtp->themssrc == ssrc) {
9380 return;
9381 }
9382
9383 rtp->themssrc = ssrc;
9384 rtp->themssrc_valid = 1;
9385
9386 /* If this is bundled we need to update the SSRC mapping */
9387 if (rtp->bundled) {
9388 struct ast_rtp *bundled_rtp;
9389 int index;
9390
9391 ao2_unlock(instance);
9392
9393 /* The child lock can't be held while accessing the parent */
9394 ao2_lock(rtp->bundled);
9395 bundled_rtp = ast_rtp_instance_get_data(rtp->bundled);
9396
9397 for (index = 0; index < AST_VECTOR_SIZE(&bundled_rtp->ssrc_mapping); ++index) {
9398 struct rtp_ssrc_mapping *mapping = AST_VECTOR_GET_ADDR(&bundled_rtp->ssrc_mapping, index);
9399
9400 if (mapping->instance == instance) {
9401 mapping->ssrc = ssrc;
9402 mapping->ssrc_valid = 1;
9403 break;
9404 }
9405 }
9406
9407 ao2_unlock(rtp->bundled);
9408
9410 }
9411}

References ao2_lock, ao2_unlock, ast_rtp_instance_get_data(), AST_VECTOR_GET_ADDR, AST_VECTOR_SIZE, ast_rtp::bundled, rtp_ssrc_mapping::instance, rtp_ssrc_mapping::ssrc, ast_rtp::ssrc, ast_rtp::ssrc_mapping, rtp_ssrc_mapping::ssrc_valid, ast_rtp::themssrc, and ast_rtp::themssrc_valid.

◆ ast_rtp_set_stream_num()

static void ast_rtp_set_stream_num ( struct ast_rtp_instance instance,
int  stream_num 
)
static

Definition at line 9413 of file res_rtp_asterisk.c.

9414{
9415 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
9416
9417 rtp->stream_num = stream_num;
9418}

References ast_rtp_instance_get_data(), and ast_rtp::stream_num.

◆ ast_rtp_stop()

static void ast_rtp_stop ( struct ast_rtp_instance instance)
static
Precondition
instance is locked

Definition at line 9247 of file res_rtp_asterisk.c.

9248{
9249 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
9250 struct ast_sockaddr addr = { {0,} };
9251
9252#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
9253 ao2_unlock(instance);
9254 AST_SCHED_DEL_UNREF(rtp->sched, rtp->rekeyid, ao2_ref(instance, -1));
9255
9256 dtls_srtp_stop_timeout_timer(instance, rtp, 0);
9257 if (rtp->rtcp) {
9258 dtls_srtp_stop_timeout_timer(instance, rtp, 1);
9259 }
9260 ao2_lock(instance);
9261#endif
9262 ast_debug_rtp(1, "(%s) RTP Stop\n",
9264
9265 if (rtp->rtcp && rtp->rtcp->schedid > -1) {
9266 ao2_unlock(instance);
9267 if (!ast_sched_del(rtp->sched, rtp->rtcp->schedid)) {
9268 /* successfully cancelled scheduler entry. */
9269 ao2_ref(instance, -1);
9270 }
9271 ao2_lock(instance);
9272 rtp->rtcp->schedid = -1;
9273 }
9274
9275 if (rtp->transport_wide_cc.schedid > -1) {
9276 ao2_unlock(instance);
9277 if (!ast_sched_del(rtp->sched, rtp->transport_wide_cc.schedid)) {
9278 ao2_ref(instance, -1);
9279 }
9280 ao2_lock(instance);
9281 rtp->transport_wide_cc.schedid = -1;
9282 }
9283
9284 if (rtp->red) {
9285 ao2_unlock(instance);
9286 AST_SCHED_DEL(rtp->sched, rtp->red->schedid);
9287 ao2_lock(instance);
9288 ast_free(rtp->red);
9289 rtp->red = NULL;
9290 }
9291
9292 ast_rtp_instance_set_remote_address(instance, &addr);
9293
9295}
#define AST_SCHED_DEL_UNREF(sched, id, refcall)
schedule task to get deleted and call unref function
Definition: sched.h:82

References ao2_lock, ao2_ref, ao2_unlock, ast_debug_rtp, ast_free, ast_rtp_instance_get_channel_id(), ast_rtp_instance_get_data(), ast_rtp_instance_set_remote_address, AST_SCHED_DEL, ast_sched_del(), AST_SCHED_DEL_UNREF, ast_set_flag, FLAG_NEED_MARKER_BIT, NULL, ast_rtp::red, ast_rtp::rtcp, ast_rtp::sched, rtp_transport_wide_cc_statistics::schedid, ast_rtcp::schedid, rtp_red::schedid, and ast_rtp::transport_wide_cc.

◆ ast_rtp_stun_request()

static void ast_rtp_stun_request ( struct ast_rtp_instance instance,
struct ast_sockaddr suggestion,
const char *  username 
)
static
Precondition
instance is NOT locked

Definition at line 9232 of file res_rtp_asterisk.c.

9233{
9234 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
9235 struct sockaddr_in suggestion_tmp;
9236
9237 /*
9238 * The instance should not be locked because we can block
9239 * waiting for a STUN respone.
9240 */
9241 ast_sockaddr_to_sin(suggestion, &suggestion_tmp);
9242 ast_stun_request(rtp->s, &suggestion_tmp, username, NULL);
9243 ast_sockaddr_from_sin(suggestion, &suggestion_tmp);
9244}
int ast_stun_request(int s, struct sockaddr_in *dst, const char *username, struct sockaddr_in *answer)
Generic STUN request.
Definition: stun.c:415

References ast_rtp_instance_get_data(), ast_sockaddr_from_sin, ast_sockaddr_to_sin, ast_stun_request(), NULL, and ast_rtp::s.

◆ ast_rtp_update_source()

static void ast_rtp_update_source ( struct ast_rtp_instance instance)
static
Precondition
instance is locked

Definition at line 4499 of file res_rtp_asterisk.c.

4500{
4501 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
4502
4503 /* We simply set this bit so that the next packet sent will have the marker bit turned on */
4505 ast_debug_rtp(3, "(%p) RTP setting the marker bit due to a source update\n", instance);
4506
4507 return;
4508}

References ast_debug_rtp, ast_rtp_instance_get_data(), ast_set_flag, and FLAG_NEED_MARKER_BIT.

◆ ast_rtp_write()

static int ast_rtp_write ( struct ast_rtp_instance instance,
struct ast_frame frame 
)
static
Precondition
instance is locked

Definition at line 5446 of file res_rtp_asterisk.c.

5447{
5448 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
5449 struct ast_sockaddr remote_address = { {0,} };
5450 struct ast_format *format;
5451 int codec;
5452
5453 ast_rtp_instance_get_remote_address(instance, &remote_address);
5454
5455 /* If we don't actually know the remote address don't even bother doing anything */
5456 if (ast_sockaddr_isnull(&remote_address)) {
5457 ast_debug_rtp(1, "(%p) RTP no remote address on instance, so dropping frame\n", instance);
5458 return 0;
5459 }
5460
5461 /* VP8: is this a request to send a RTCP FIR? */
5463 rtp_write_rtcp_fir(instance, rtp, &remote_address);
5464 return 0;
5465 } else if (frame->frametype == AST_FRAME_RTCP) {
5466 if (frame->subclass.integer == AST_RTP_RTCP_PSFB) {
5467 rtp_write_rtcp_psfb(instance, rtp, frame, &remote_address);
5468 }
5469 return 0;
5470 }
5471
5472 /* If there is no data length we can't very well send the packet */
5473 if (!frame->datalen) {
5474 ast_debug_rtp(1, "(%p) RTP received frame with no data for instance, so dropping frame\n", instance);
5475 return 0;
5476 }
5477
5478 /* If the packet is not one our RTP stack supports bail out */
5479 if (frame->frametype != AST_FRAME_VOICE && frame->frametype != AST_FRAME_VIDEO && frame->frametype != AST_FRAME_TEXT) {
5480 ast_log(LOG_WARNING, "RTP can only send voice, video, and text\n");
5481 return -1;
5482 }
5483
5484 if (rtp->red) {
5485 /* return 0; */
5486 /* no primary data or generations to send */
5487 if ((frame = red_t140_to_red(rtp->red)) == NULL)
5488 return 0;
5489 }
5490
5491 /* Grab the subclass and look up the payload we are going to use */
5493 1, frame->subclass.format, 0);
5494 if (codec < 0) {
5495 ast_log(LOG_WARNING, "Don't know how to send format %s packets with RTP\n",
5497 return -1;
5498 }
5499
5500 /* Note that we do not increase the ref count here as this pointer
5501 * will not be held by any thing explicitly. The format variable is
5502 * merely a convenience reference to frame->subclass.format */
5503 format = frame->subclass.format;
5505 /* Oh dear, if the format changed we will have to set up a new smoother */
5506 ast_debug_rtp(1, "(%s) RTP ooh, format changed from %s to %s\n",
5510 ao2_replace(rtp->lasttxformat, format);
5511 if (rtp->smoother) {
5513 rtp->smoother = NULL;
5514 }
5515 }
5516
5517 /* If no smoother is present see if we have to set one up */
5518 if (!rtp->smoother && ast_format_can_be_smoothed(format)) {
5519 unsigned int smoother_flags = ast_format_get_smoother_flags(format);
5520 unsigned int framing_ms = ast_rtp_codecs_get_framing(ast_rtp_instance_get_codecs(instance));
5521
5522 if (!framing_ms && (smoother_flags & AST_SMOOTHER_FLAG_FORCED)) {
5523 framing_ms = ast_format_get_default_ms(format);
5524 }
5525
5526 if (framing_ms) {
5528 if (!rtp->smoother) {
5529 ast_log(LOG_WARNING, "Unable to create smoother: format %s ms: %u len: %u\n",
5530 ast_format_get_name(format), framing_ms, ast_format_get_minimum_bytes(format));
5531 return -1;
5532 }
5533 ast_smoother_set_flags(rtp->smoother, smoother_flags);
5534 }
5535 }
5536
5537 /* Feed audio frames into the actual function that will create a frame and send it */
5538 if (rtp->smoother) {
5539 struct ast_frame *f;
5540
5542 ast_smoother_feed_be(rtp->smoother, frame);
5543 } else {
5544 ast_smoother_feed(rtp->smoother, frame);
5545 }
5546
5547 while ((f = ast_smoother_read(rtp->smoother)) && (f->data.ptr)) {
5548 rtp_raw_write(instance, f, codec);
5549 }
5550 } else {
5551 int hdrlen = 12;
5552 struct ast_frame *f = NULL;
5553
5554 if (frame->offset < hdrlen) {
5555 f = ast_frdup(frame);
5556 } else {
5557 f = frame;
5558 }
5559 if (f->data.ptr) {
5560 rtp_raw_write(instance, f, codec);
5561 }
5562 if (f != frame) {
5563 ast_frfree(f);
5564 }
5565
5566 }
5567
5568 return 0;
5569}
int ast_format_get_smoother_flags(const struct ast_format *format)
Get smoother flags for this format.
Definition: format.c:349
int ast_format_can_be_smoothed(const struct ast_format *format)
Get whether or not the format can be smoothed.
Definition: format.c:344
unsigned int ast_format_get_minimum_bytes(const struct ast_format *format)
Get the minimum number of bytes expected in a frame for this format.
Definition: format.c:374
unsigned int ast_format_get_minimum_ms(const struct ast_format *format)
Get the minimum amount of media carried in this format.
Definition: format.c:364
@ AST_FORMAT_CMP_NOT_EQUAL
Definition: format.h:38
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
unsigned int ast_format_get_default_ms(const struct ast_format *format)
Get the default framing size (in milliseconds) for a format.
Definition: format.c:359
static int rtp_raw_write(struct ast_rtp_instance *instance, struct ast_frame *frame, int codec)
static struct ast_frame * red_t140_to_red(struct rtp_red *red)
static void rtp_write_rtcp_psfb(struct ast_rtp_instance *instance, struct ast_rtp *rtp, struct ast_frame *frame, struct ast_sockaddr *remote_address)
#define AST_RTP_RTCP_PSFB
Definition: rtp_engine.h:324
unsigned int ast_rtp_codecs_get_framing(struct ast_rtp_codecs *codecs)
Get the framing used for a set of codecs.
Definition: rtp_engine.c:1582
void ast_smoother_set_flags(struct ast_smoother *smoother, int flags)
Definition: smoother.c:123
#define ast_smoother_feed_be(s, f)
Definition: smoother.h:80
int ast_smoother_test_flag(struct ast_smoother *s, int flag)
Definition: smoother.c:128
#define AST_SMOOTHER_FLAG_FORCED
Definition: smoother.h:36
struct ast_frame * ast_smoother_read(struct ast_smoother *s)
Definition: smoother.c:169
#define ast_smoother_feed(s, f)
Definition: smoother.h:75
struct ast_smoother * ast_smoother_new(int bytes)
Definition: smoother.c:108
#define AST_SMOOTHER_FLAG_BE
Definition: smoother.h:35
Definition of a media format.
Definition: format.c:43
struct ast_codec * codec
Pointer to the codec in use for this format.
Definition: format.c:47

References ao2_replace, AST_CONTROL_VIDUPDATE, ast_debug_rtp, ast_format_can_be_smoothed(), ast_format_cmp(), AST_FORMAT_CMP_NOT_EQUAL, ast_format_get_default_ms(), ast_format_get_minimum_bytes(), ast_format_get_minimum_ms(), ast_format_get_name(), ast_format_get_smoother_flags(), AST_FRAME_CONTROL, AST_FRAME_RTCP, AST_FRAME_TEXT, AST_FRAME_VIDEO, AST_FRAME_VOICE, ast_frdup, ast_frfree, ast_log, ast_rtp_codecs_get_framing(), ast_rtp_codecs_payload_code_tx(), ast_rtp_instance_get_channel_id(), ast_rtp_instance_get_codecs(), ast_rtp_instance_get_data(), ast_rtp_instance_get_remote_address, AST_RTP_RTCP_PSFB, ast_smoother_feed, ast_smoother_feed_be, AST_SMOOTHER_FLAG_BE, AST_SMOOTHER_FLAG_FORCED, ast_smoother_free(), ast_smoother_new(), ast_smoother_read(), ast_smoother_set_flags(), ast_smoother_test_flag(), ast_sockaddr_isnull(), ast_format::codec, ast_frame::data, ast_frame::datalen, ast_frame_subclass::format, ast_frame::frametype, ast_frame_subclass::integer, ast_rtp::lasttxformat, LOG_WARNING, NULL, ast_frame::offset, ast_frame::ptr, ast_rtp::red, red_t140_to_red(), rtp_raw_write(), rtp_write_rtcp_fir(), rtp_write_rtcp_psfb(), ast_rtp::smoother, and ast_frame::subclass.

Referenced by red_write(), and rtp_red_buffer().

◆ bridge_p2p_rtp_write()

static int bridge_p2p_rtp_write ( struct ast_rtp_instance instance,
struct ast_rtp_instance instance1,
unsigned int *  rtpheader,
int  len,
int  hdrlen 
)
static
Precondition
instance is locked

Definition at line 7122 of file res_rtp_asterisk.c.

7124{
7125 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
7126 struct ast_rtp *bridged;
7127 int res = 0, payload = 0, bridged_payload = 0, mark;
7128 RAII_VAR(struct ast_rtp_payload_type *, payload_type, NULL, ao2_cleanup);
7129 int reconstruct = ntohl(rtpheader[0]);
7130 struct ast_sockaddr remote_address = { {0,} };
7131 int ice;
7132 unsigned int timestamp = ntohl(rtpheader[1]);
7133
7134 /* Get fields from packet */
7135 payload = (reconstruct & 0x7f0000) >> 16;
7136 mark = (reconstruct & 0x800000) >> 23;
7137
7138 /* Check what the payload value should be */
7139 payload_type = ast_rtp_codecs_get_payload(ast_rtp_instance_get_codecs(instance), payload);
7140 if (!payload_type) {
7141 return -1;
7142 }
7143
7144 /* Otherwise adjust bridged payload to match */
7146 payload_type->asterisk_format, payload_type->format, payload_type->rtp_code);
7147
7148 /* If no codec could be matched between instance and instance1, then somehow things were made incompatible while we were still bridged. Bail. */
7149 if (bridged_payload < 0) {
7150 return -1;
7151 }
7152
7153 /* If the payload coming in is not one of the negotiated ones then send it to the core, this will cause formats to change and the bridge to break */
7154 if (ast_rtp_codecs_find_payload_code(ast_rtp_instance_get_codecs(instance1), bridged_payload) == -1) {
7155 ast_debug_rtp(1, "(%p, %p) RTP unsupported payload type received\n", instance, instance1);
7156 return -1;
7157 }
7158
7159 /*
7160 * Even if we are no longer in dtmf, we could still be receiving
7161 * re-transmissions of the last dtmf end still. Feed those to the
7162 * core so they can be filtered accordingly.
7163 */
7164 if (rtp->last_end_timestamp.is_set && rtp->last_end_timestamp.ts == timestamp) {
7165 ast_debug_rtp(1, "(%p, %p) RTP feeding packet with duplicate timestamp to core\n", instance, instance1);
7166 return -1;
7167 }
7168
7169 if (payload_type->asterisk_format) {
7170 ao2_replace(rtp->lastrxformat, payload_type->format);
7171 }
7172
7173 /*
7174 * We have now determined that we need to send the RTP packet
7175 * out the bridged instance to do local bridging so we must unlock
7176 * the receiving instance to prevent deadlock with the bridged
7177 * instance.
7178 *
7179 * Technically we should grab a ref to instance1 so it won't go
7180 * away on us. However, we should be safe because the bridged
7181 * instance won't change without both channels involved being
7182 * locked and we currently have the channel lock for the receiving
7183 * instance.
7184 */
7185 ao2_unlock(instance);
7186 ao2_lock(instance1);
7187
7188 /*
7189 * Get the peer rtp pointer now to emphasize that using it
7190 * must happen while instance1 is locked.
7191 */
7192 bridged = ast_rtp_instance_get_data(instance1);
7193
7194
7195 /* If bridged peer is in dtmf, feed all packets to core until it finishes to avoid infinite dtmf */
7196 if (bridged->sending_digit) {
7197 ast_debug_rtp(1, "(%p, %p) RTP Feeding packet to core until DTMF finishes\n", instance, instance1);
7198 ao2_unlock(instance1);
7199 ao2_lock(instance);
7200 return -1;
7201 }
7202
7203 if (payload_type->asterisk_format) {
7204 /*
7205 * If bridged peer has already received rtp, perform the asymmetric codec check
7206 * if that feature has been activated
7207 */
7208 if (!bridged->asymmetric_codec
7209 && bridged->lastrxformat != ast_format_none
7210 && ast_format_cmp(payload_type->format, bridged->lastrxformat) == AST_FORMAT_CMP_NOT_EQUAL) {
7211 ast_debug_rtp(1, "(%p, %p) RTP asymmetric RTP codecs detected (TX: %s, RX: %s) sending frame to core\n",
7212 instance, instance1, ast_format_get_name(payload_type->format),
7214 ao2_unlock(instance1);
7215 ao2_lock(instance);
7216 return -1;
7217 }
7218
7219 ao2_replace(bridged->lasttxformat, payload_type->format);
7220 }
7221
7222 ast_rtp_instance_get_remote_address(instance1, &remote_address);
7223
7224 if (ast_sockaddr_isnull(&remote_address)) {
7225 ast_debug_rtp(5, "(%p, %p) RTP remote address is null, most likely RTP has been stopped\n",
7226 instance, instance1);
7227 ao2_unlock(instance1);
7228 ao2_lock(instance);
7229 return 0;
7230 }
7231
7232 /* If the marker bit has been explicitly set turn it on */
7233 if (ast_test_flag(bridged, FLAG_NEED_MARKER_BIT)) {
7234 mark = 1;
7236 }
7237
7238 /* Set the marker bit for the first local bridged packet which has the first bridged peer's SSRC. */
7240 mark = 1;
7242 }
7243
7244 /* Reconstruct part of the packet */
7245 reconstruct &= 0xFF80FFFF;
7246 reconstruct |= (bridged_payload << 16);
7247 reconstruct |= (mark << 23);
7248 rtpheader[0] = htonl(reconstruct);
7249
7250 if (mark) {
7251 /* make this rtp instance aware of the new ssrc it is sending */
7252 bridged->ssrc = ntohl(rtpheader[2]);
7253 }
7254
7255 /* Send the packet back out */
7256 res = rtp_sendto(instance1, (void *)rtpheader, len, 0, &remote_address, &ice);
7257 if (res < 0) {
7260 "RTP Transmission error of packet to %s: %s\n",
7261 ast_sockaddr_stringify(&remote_address),
7262 strerror(errno));
7266 "RTP NAT: Can't write RTP to private "
7267 "address %s, waiting for other end to "
7268 "send audio...\n",
7269 ast_sockaddr_stringify(&remote_address));
7270 }
7272 }
7273 ao2_unlock(instance1);
7274 ao2_lock(instance);
7275 return 0;
7276 }
7277
7278 if (rtp_debug_test_addr(&remote_address)) {
7279 ast_verbose("Sent RTP P2P packet to %s%s (type %-2.2d, len %-6.6d)\n",
7280 ast_sockaddr_stringify(&remote_address),
7281 ice ? " (via ICE)" : "",
7282 bridged_payload, len - hdrlen);
7283 }
7284
7285 ao2_unlock(instance1);
7286 ao2_lock(instance);
7287 return 0;
7288}
static int reconstruct(int sign, int dqln, int y)
Definition: codec_g726.c:331
#define FLAG_NAT_INACTIVE
#define FLAG_NAT_INACTIVE_NOWARN
int ast_rtp_codecs_find_payload_code(struct ast_rtp_codecs *codecs, int payload)
Search for the tx payload type in the ast_rtp_codecs structure.
Definition: rtp_engine.c:1988
#define ast_test_flag(p, flag)
Definition: utils.h:63
#define ast_clear_flag(p, flag)
Definition: utils.h:77

References ao2_cleanup, ao2_lock, ao2_replace, ao2_unlock, ast_clear_flag, ast_debug_rtp, ast_debug_rtp_packet_is_allowed, ast_format_cmp(), AST_FORMAT_CMP_NOT_EQUAL, ast_format_get_name(), ast_format_none, ast_log, ast_rtp_codecs_find_payload_code(), ast_rtp_codecs_get_payload(), ast_rtp_codecs_payload_code_tx(), ast_rtp_instance_get_codecs(), ast_rtp_instance_get_data(), ast_rtp_instance_get_prop(), ast_rtp_instance_get_remote_address, AST_RTP_PROPERTY_NAT, ast_set_flag, ast_sockaddr_isnull(), ast_sockaddr_stringify(), ast_test_flag, ast_verbose(), ast_rtp::asymmetric_codec, DEBUG_ATLEAST, errno, FLAG_NAT_ACTIVE, FLAG_NAT_INACTIVE, FLAG_NAT_INACTIVE_NOWARN, FLAG_NEED_MARKER_BIT, FLAG_REQ_LOCAL_BRIDGE_BIT, optional_ts::is_set, ast_rtp::last_end_timestamp, ast_rtp::lastrxformat, ast_rtp::lasttxformat, len(), LOG_WARNING, NULL, RAII_VAR, reconstruct(), rtp_debug_test_addr(), rtp_sendto(), ast_rtp::sending_digit, ast_rtp::ssrc, and optional_ts::ts.

Referenced by ast_rtp_interpret().

◆ calc_mean_and_standard_deviation()

static void calc_mean_and_standard_deviation ( double  new_sample,
double *  mean,
double *  std_dev,
unsigned int *  count 
)
static

Definition at line 3470 of file res_rtp_asterisk.c.

3471{
3472 double delta1;
3473 double delta2;
3474
3475 /* First convert the standard deviation back into a sum of squares. */
3476 double last_sum_of_squares = (*std_dev) * (*std_dev) * (*count ?: 1);
3477
3478 if (++(*count) == 0) {
3479 /* Avoid potential divide by zero on an overflow */
3480 *count = 1;
3481 }
3482
3483 /*
3484 * Below is an implementation of Welford's online algorithm [1] for calculating
3485 * mean and variance in a single pass.
3486 *
3487 * [1] https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
3488 */
3489
3490 delta1 = new_sample - *mean;
3491 *mean += (delta1 / *count);
3492 delta2 = new_sample - *mean;
3493
3494 /* Now calculate the new variance, and subsequent standard deviation */
3495 *std_dev = sqrt((last_sum_of_squares + (delta1 * delta2)) / *count);
3496}

Referenced by calc_rxstamp_and_jitter(), calculate_lost_packet_statistics(), update_jitter_stats(), update_local_mes_stats(), update_lost_stats(), update_reported_mes_stats(), and update_rtt_stats().

◆ calc_media_experience_score()

static double calc_media_experience_score ( struct ast_rtp_instance instance,
double  normdevrtt,
double  normdev_rxjitter,
double  stdev_rxjitter,
double  normdev_rxlost 
)
static

Calculate a "media experience score" based on given data.

Technically, a mean opinion score (MOS) cannot be calculated without the involvement of human eyes (video) and ears (audio). Thus instead we'll approximate an opinion using the given parameters, and call it a media experience score.

The tallied score is based upon recommendations and formulas from ITU-T G.107, ITU-T G.109, ITU-T G.113, and other various internet sources.

Parameters
instanceRTP instance
normdevrttThe average round trip time
normdev_rxjitterThe smoothed jitter
stdev_rxjitterThe jitter standard deviation value
normdev_rxlostThe average number of packets lost since last check
Returns
A media experience score.
Note
The calculations in this function could probably be simplified but calculating a MOS using the information available publicly, then re-scaling it to 0.0 -> 100.0 makes the process clearer and easier to troubleshoot or change.

Definition at line 6196 of file res_rtp_asterisk.c.

6199{
6200 double r_value;
6201 double pseudo_mos;
6202 double mes = 0;
6203
6204 /*
6205 * While the media itself might be okay, a significant enough delay could make
6206 * for an unpleasant user experience.
6207 *
6208 * Calculate the effective latency by using the given round trip time, and adding
6209 * jitter scaled according to its standard deviation. The scaling is done in order
6210 * to increase jitter's weight since a higher deviation can result in poorer overall
6211 * quality.
6212 */
6213 double effective_latency = (normdevrtt * 1000)
6214 + ((normdev_rxjitter * 2) * (stdev_rxjitter / 3))
6215 + 10;
6216
6217 /*
6218 * Using the defaults for the standard transmission rating factor ("R" value)
6219 * one arrives at 93.2 (see ITU-T G.107 for more details), so we'll use that
6220 * as the starting value and subtract deficiencies that could affect quality.
6221 *
6222 * Calculate the impact of the effective latency. Influence increases with
6223 * values over 160 as the significant "lag" can degrade user experience.
6224 */
6225 if (effective_latency < 160) {
6226 r_value = 93.2 - (effective_latency / 40);
6227 } else {
6228 r_value = 93.2 - (effective_latency - 120) / 10;
6229 }
6230
6231 /* Next evaluate the impact of lost packets */
6232 r_value = r_value - (normdev_rxlost * 2.0);
6233
6234 /*
6235 * Finally convert the "R" value into a opinion/quality score between 1 (really anything
6236 * below 3 should be considered poor) and 4.5 (the highest achievable for VOIP).
6237 */
6238 if (r_value < 0) {
6239 pseudo_mos = 1.0;
6240 } else if (r_value > 100) {
6241 pseudo_mos = 4.5;
6242 } else {
6243 pseudo_mos = 1 + (0.035 * r_value) + (r_value * (r_value - 60) * (100 - r_value) * 0.000007);
6244 }
6245
6246 /*
6247 * We're going to rescale the 0.0->5.0 pseudo_mos to the 0.0->100.0 MES.
6248 * For those ranges, we could actually just multiply the pseudo_mos
6249 * by 20 but we may want to change the scale later.
6250 */
6251 mes = RESCALE(pseudo_mos, 0.0, 5.0, 0.0, 100.0);
6252
6253 return mes;
6254}
#define RESCALE(in, inmin, inmax, outmin, outmax)

References RESCALE.

Referenced by update_local_mes_stats(), and update_reported_mes_stats().

◆ calc_rxstamp_and_jitter()

static void calc_rxstamp_and_jitter ( struct timeval *  tv,
struct ast_rtp rtp,
unsigned int  rx_rtp_ts,
int  mark 
)
static

Definition at line 5571 of file res_rtp_asterisk.c.

5574{
5575 int rate = ast_rtp_get_rate(rtp->f.subclass.format);
5576
5577 double jitter = 0.0;
5578 double prev_jitter = 0.0;
5579 struct timeval now;
5580 struct timeval tmp;
5581 double rxnow;
5582 double arrival_sec;
5583 unsigned int arrival;
5584 int transit;
5585 int d;
5586
5587 gettimeofday(&now,NULL);
5588
5589 if (rtp->rxcount == 1 || mark) {
5590 rtp->rxstart = ast_tv2double(&now);
5591 rtp->remote_seed_rx_rtp_ts = rx_rtp_ts;
5592
5593 /*
5594 * "tv" is placed in the received frame's
5595 * "delivered" field and when this frame is
5596 * sent out again on the other side, it's
5597 * used to calculate the timestamp on the
5598 * outgoing RTP packets.
5599 *
5600 * NOTE: We need to do integer math here
5601 * because double math rounding issues can
5602 * generate incorrect timestamps.
5603 */
5604 rtp->rxcore = now;
5605 tmp = ast_samp2tv(rx_rtp_ts, rate);
5606 rtp->rxcore = ast_tvsub(rtp->rxcore, tmp);
5607 rtp->rxcore.tv_usec -= rtp->rxcore.tv_usec % 100;
5608 *tv = ast_tvadd(rtp->rxcore, tmp);
5609
5610 ast_debug_rtcp(3, "%s: "
5611 "Seed ts: %u current time: %f\n",
5613 , rx_rtp_ts
5614 , rtp->rxstart
5615 );
5616
5617 return;
5618 }
5619
5620 tmp = ast_samp2tv(rx_rtp_ts, rate);
5621 /* See the comment about "tv" above. Even if
5622 * we don't use this received packet for jitter
5623 * calculations, we still need to set tv so the
5624 * timestamp will be correct when this packet is
5625 * sent out again.
5626 */
5627 *tv = ast_tvadd(rtp->rxcore, tmp);
5628
5629 /*
5630 * The first few packets are generally unstable so let's
5631 * not use them in the calculations.
5632 */
5634 ast_debug_rtcp(3, "%s: Packet %d < %d. Ignoring\n",
5636 , rtp->rxcount
5638 );
5639
5640 return;
5641 }
5642
5643 /*
5644 * First good packet. Capture the start time and timestamp
5645 * but don't actually use this packet for calculation.
5646 */
5648 rtp->rxstart_stable = ast_tv2double(&now);
5649 rtp->remote_seed_rx_rtp_ts_stable = rx_rtp_ts;
5650 rtp->last_transit_time_samples = -rx_rtp_ts;
5651
5652 ast_debug_rtcp(3, "%s: "
5653 "pkt: %5u Stable Seed ts: %u current time: %f\n",
5655 , rtp->rxcount
5656 , rx_rtp_ts
5657 , rtp->rxstart_stable
5658 );
5659
5660 return;
5661 }
5662
5663 /*
5664 * If the current packet isn't in sequence, don't
5665 * use it in any calculations as remote_current_rx_rtp_ts
5666 * is not going to be correct.
5667 */
5668 if (rtp->lastrxseqno != rtp->prevrxseqno + 1) {
5669 ast_debug_rtcp(3, "%s: Current packet seq %d != last packet seq %d + 1. Ignoring\n",
5671 , rtp->lastrxseqno
5672 , rtp->prevrxseqno
5673 );
5674
5675 return;
5676 }
5677
5678 /*
5679 * The following calculations are taken from
5680 * https://www.rfc-editor.org/rfc/rfc3550#appendix-A.8
5681 *
5682 * The received rtp timestamp is the random "seed"
5683 * timestamp chosen by the sender when they sent the
5684 * first packet, plus the number of samples since then.
5685 *
5686 * To get our arrival time in the same units, we
5687 * calculate the time difference in seconds between
5688 * when we received the first packet and when we
5689 * received this packet and convert that to samples.
5690 */
5691 rxnow = ast_tv2double(&now);
5692 arrival_sec = rxnow - rtp->rxstart_stable;
5693 arrival = ast_sec2samp(arrival_sec, rate);
5694
5695 /*
5696 * Now we can use the exact formula in
5697 * https://www.rfc-editor.org/rfc/rfc3550#appendix-A.8 :
5698 *
5699 * int transit = arrival - r->ts;
5700 * int d = transit - s->transit;
5701 * s->transit = transit;
5702 * if (d < 0) d = -d;
5703 * s->jitter += (1./16.) * ((double)d - s->jitter);
5704 *
5705 * Our rx_rtp_ts is their r->ts.
5706 * Our rtp->last_transit_time_samples is their s->transit.
5707 * Our rtp->rxjitter is their s->jitter.
5708 */
5709 transit = arrival - rx_rtp_ts;
5710 d = transit - rtp->last_transit_time_samples;
5711
5712 if (d < 0) {
5713 d = -d;
5714 }
5715
5716 prev_jitter = rtp->rxjitter_samples;
5717 jitter = (1.0/16.0) * (((double)d) - prev_jitter);
5718 rtp->rxjitter_samples = prev_jitter + jitter;
5719
5720 /*
5721 * We need to hang on to jitter in both samples and seconds.
5722 */
5723 rtp->rxjitter = ast_samp2sec(rtp->rxjitter_samples, rate);
5724
5725 ast_debug_rtcp(3, "%s: pkt: %5u "
5726 "Arrival sec: %7.3f Arrival ts: %10u RX ts: %10u "
5727 "Transit samp: %6d Last transit samp: %6d d: %4d "
5728 "Curr jitter: %7.0f(%7.3f) Prev Jitter: %7.0f(%7.3f) New Jitter: %7.0f(%7.3f)\n",
5730 , rtp->rxcount
5731 , arrival_sec
5732 , arrival
5733 , rx_rtp_ts
5734 , transit
5736 , d
5737 , jitter
5738 , ast_samp2sec(jitter, rate)
5739 , prev_jitter
5740 , ast_samp2sec(prev_jitter, rate)
5741 , rtp->rxjitter_samples
5742 , rtp->rxjitter
5743 );
5744
5745 rtp->last_transit_time_samples = transit;
5746
5747 /*
5748 * Update all the stats.
5749 */
5750 if (rtp->rtcp) {
5751 if (rtp->rxjitter > rtp->rtcp->maxrxjitter)
5752 rtp->rtcp->maxrxjitter = rtp->rxjitter;
5753 if (rtp->rtcp->rxjitter_count == 1)
5754 rtp->rtcp->minrxjitter = rtp->rxjitter;
5755 if (rtp->rtcp && rtp->rxjitter < rtp->rtcp->minrxjitter)
5756 rtp->rtcp->minrxjitter = rtp->rxjitter;
5757
5760 &rtp->rtcp->rxjitter_count);
5761 }
5762
5763 return;
5764}
static int tmp()
Definition: bt_open.c:389
#define RTP_IGNORE_FIRST_PACKETS_COUNT
static void calc_mean_and_standard_deviation(double new_sample, double *mean, double *std_dev, unsigned int *count)
unsigned int rxjitter_count
unsigned int remote_seed_rx_rtp_ts_stable
double rxstart_stable
struct timeval rxcore
unsigned int last_transit_time_samples
unsigned int remote_seed_rx_rtp_ts
static struct test_val d
unsigned int ast_sec2samp(double _seconds, int _rate)
Returns the number of samples at _rate in the duration in _seconds.
Definition: time.h:333
struct timeval ast_tvsub(struct timeval a, struct timeval b)
Returns the difference of two timevals a - b.
Definition: extconf.c:2297
double ast_tv2double(const struct timeval *tv)
Returns a double corresponding to the number of seconds in the timeval tv.
Definition: time.h:270

References ast_debug_rtcp, ast_rtp_get_rate(), ast_rtp_instance_get_channel_id(), ast_samp2sec(), ast_samp2tv(), ast_sec2samp(), ast_tv2double(), ast_tvadd(), ast_tvsub(), calc_mean_and_standard_deviation(), d, ast_rtp::f, ast_frame_subclass::format, ast_rtp::last_transit_time_samples, ast_rtp::lastrxseqno, ast_rtcp::maxrxjitter, ast_rtcp::minrxjitter, ast_rtcp::normdev_rxjitter, NULL, ast_rtp::owner, ast_rtp::prevrxseqno, ast_rtp::remote_seed_rx_rtp_ts, ast_rtp::remote_seed_rx_rtp_ts_stable, ast_rtp::rtcp, RTP_IGNORE_FIRST_PACKETS_COUNT, ast_rtp::rxcore, ast_rtp::rxcount, ast_rtp::rxjitter, ast_rtcp::rxjitter_count, ast_rtp::rxjitter_samples, ast_rtp::rxstart, ast_rtp::rxstart_stable, ast_rtcp::stdev_rxjitter, ast_frame::subclass, and tmp().

Referenced by ast_rtp_interpret().

◆ calc_txstamp()

static unsigned int calc_txstamp ( struct ast_rtp rtp,
struct timeval *  delivery 
)
static

Definition at line 3855 of file res_rtp_asterisk.c.

3856{
3857 struct timeval t;
3858 long ms;
3859
3860 if (ast_tvzero(rtp->txcore)) {
3861 rtp->txcore = ast_tvnow();
3862 rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000;
3863 }
3864
3865 t = (delivery && !ast_tvzero(*delivery)) ? *delivery : ast_tvnow();
3866 if ((ms = ast_tvdiff_ms(t, rtp->txcore)) < 0) {
3867 ms = 0;
3868 }
3869 rtp->txcore = t;
3870
3871 return (unsigned int) ms;
3872}
struct timeval txcore

References ast_tvdiff_ms(), ast_tvnow(), ast_tvzero(), and ast_rtp::txcore.

Referenced by ast_rtp_dtmf_begin(), ast_rtp_dtmf_continuation(), ast_rtp_dtmf_end_with_duration(), and rtp_raw_write().

◆ calculate_lost_packet_statistics()

static void calculate_lost_packet_statistics ( struct ast_rtp rtp,
unsigned int *  lost_packets,
int *  fraction_lost 
)
static

Definition at line 4579 of file res_rtp_asterisk.c.

4582{
4583 unsigned int extended_seq_no;
4584 unsigned int expected_packets;
4585 unsigned int expected_interval;
4586 unsigned int received_interval;
4587 int lost_interval;
4588
4589 /* Compute statistics */
4590 extended_seq_no = rtp->cycles + rtp->lastrxseqno;
4591 expected_packets = extended_seq_no - rtp->seedrxseqno + 1;
4592 if (rtp->rxcount > expected_packets) {
4593 expected_packets += rtp->rxcount - expected_packets;
4594 }
4595 *lost_packets = expected_packets - rtp->rxcount;
4596 expected_interval = expected_packets - rtp->rtcp->expected_prior;
4597 received_interval = rtp->rxcount - rtp->rtcp->received_prior;
4598 if (received_interval > expected_interval) {
4599 /* If we receive some late packets it is possible for the packets
4600 * we received in this interval to exceed the number we expected.
4601 * We update the expected so that the packet loss calculations
4602 * show that no packets are lost.
4603 */
4604 expected_interval = received_interval;
4605 }
4606 lost_interval = expected_interval - received_interval;
4607 if (expected_interval == 0 || lost_interval <= 0) {
4608 *fraction_lost = 0;
4609 } else {
4610 *fraction_lost = (lost_interval << 8) / expected_interval;
4611 }
4612
4613 /* Update RTCP statistics */
4614 rtp->rtcp->received_prior = rtp->rxcount;
4615 rtp->rtcp->expected_prior = expected_packets;
4616
4617 /*
4618 * While rxlost represents the number of packets lost since the last report was sent, for
4619 * the calculations below it should be thought of as a single sample. Thus min/max are the
4620 * lowest/highest sample value seen, and the mean is the average number of packets lost
4621 * between each report. As such rxlost_count only needs to be incremented per report.
4622 */
4623 if (lost_interval <= 0) {
4624 rtp->rtcp->rxlost = 0;
4625 } else {
4626 rtp->rtcp->rxlost = lost_interval;
4627 }
4628 if (rtp->rtcp->rxlost_count == 0) {
4629 rtp->rtcp->minrxlost = rtp->rtcp->rxlost;
4630 }
4631 if (lost_interval && lost_interval < rtp->rtcp->minrxlost) {
4632 rtp->rtcp->minrxlost = rtp->rtcp->rxlost;
4633 }
4634 if (lost_interval > rtp->rtcp->maxrxlost) {
4635 rtp->rtcp->maxrxlost = rtp->rtcp->rxlost;
4636 }
4637
4639 &rtp->rtcp->stdev_rxlost, &rtp->rtcp->rxlost_count);
4640}
unsigned int rxlost_count

References calc_mean_and_standard_deviation(), and ast_srtp::rtp.

Referenced by ast_rtcp_generate_report().

◆ compare_by_value()

static int compare_by_value ( int  elem,
int  value 
)
static

Helper function to compare an elem in a vector by value.

Definition at line 3124 of file res_rtp_asterisk.c.

3125{
3126 return elem - value;
3127}

References value.

Referenced by ast_rtp_read().

◆ create_dtmf_frame()

static struct ast_frame * create_dtmf_frame ( struct ast_rtp_instance instance,
enum ast_frame_type  type,
int  compensate 
)
static

Definition at line 5766 of file res_rtp_asterisk.c.

5767{
5768 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
5769 struct ast_sockaddr remote_address = { {0,} };
5770
5771 ast_rtp_instance_get_remote_address(instance, &remote_address);
5772
5773 if (((compensate && type == AST_FRAME_DTMF_END) || (type == AST_FRAME_DTMF_BEGIN)) && ast_tvcmp(ast_tvnow(), rtp->dtmfmute) < 0) {
5774 ast_debug_rtp(1, "(%p) RTP ignore potential DTMF echo from '%s'\n",
5775 instance, ast_sockaddr_stringify(&remote_address));
5776 rtp->resp = 0;
5777 rtp->dtmfsamples = 0;
5778 return &ast_null_frame;
5779 } else if (type == AST_FRAME_DTMF_BEGIN && rtp->resp == 'X') {
5780 ast_debug_rtp(1, "(%p) RTP ignore flash begin from '%s'\n",
5781 instance, ast_sockaddr_stringify(&remote_address));
5782 rtp->resp = 0;
5783 rtp->dtmfsamples = 0;
5784 return &ast_null_frame;
5785 }
5786
5787 if (rtp->resp == 'X') {
5788 ast_debug_rtp(1, "(%p) RTP creating flash Frame at %s\n",
5789 instance, ast_sockaddr_stringify(&remote_address));
5792 } else {
5793 ast_debug_rtp(1, "(%p) RTP creating %s DTMF Frame: %d (%c), at %s\n",
5794 instance, type == AST_FRAME_DTMF_END ? "END" : "BEGIN",
5795 rtp->resp, rtp->resp,
5796 ast_sockaddr_stringify(&remote_address));
5797 rtp->f.frametype = type;
5798 rtp->f.subclass.integer = rtp->resp;
5799 }
5800 rtp->f.datalen = 0;
5801 rtp->f.samples = 0;
5802 rtp->f.mallocd = 0;
5803 rtp->f.src = "RTP";
5804 AST_LIST_NEXT(&rtp->f, frame_list) = NULL;
5805
5806 return &rtp->f;
5807}
static const char type[]
Definition: chan_ooh323.c:109
@ AST_FRAME_DTMF_BEGIN
@ AST_CONTROL_FLASH
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Definition: linkedlists.h:439
unsigned int dtmfsamples
int ast_tvcmp(struct timeval _a, struct timeval _b)
Compress two struct timeval instances returning -1, 0, 1 if the first arg is smaller,...
Definition: time.h:137

References AST_CONTROL_FLASH, ast_debug_rtp, AST_FRAME_CONTROL, AST_FRAME_DTMF_BEGIN, AST_FRAME_DTMF_END, AST_LIST_NEXT, ast_null_frame, ast_rtp_instance_get_data(), ast_rtp_instance_get_remote_address, ast_sockaddr_stringify(), ast_tvcmp(), ast_tvnow(), ast_frame::datalen, ast_rtp::dtmfmute, ast_rtp::dtmfsamples, ast_rtp::f, ast_frame::frametype, ast_frame_subclass::integer, ast_frame::mallocd, NULL, ast_rtp::resp, ast_frame::samples, ast_frame::src, ast_frame::subclass, and type.

Referenced by ast_rtp_interpret(), process_dtmf_cisco(), and process_dtmf_rfc2833().

◆ create_new_socket()

static int create_new_socket ( const char *  type,
int  af 
)
static

Definition at line 3498 of file res_rtp_asterisk.c.

3499{
3500 int sock = ast_socket_nonblock(af, SOCK_DGRAM, 0);
3501
3502 if (sock < 0) {
3503 ast_log(LOG_WARNING, "Unable to allocate %s socket: %s\n", type, strerror(errno));
3504 return sock;
3505 }
3506
3507#ifdef SO_NO_CHECK
3508 if (nochecksums) {
3509 setsockopt(sock, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
3510 }
3511#endif
3512
3513 return sock;
3514}
#define ast_socket_nonblock(domain, type, protocol)
Create a non-blocking socket.
Definition: utils.h:1073

References ast_log, ast_socket_nonblock, errno, LOG_WARNING, and type.

Referenced by ast_rtp_prop_set(), and rtp_allocate_transport().

◆ find_by_value()

static int find_by_value ( int  elem,
int  value 
)
static

Helper function to find an elem in a vector by value.

Definition at line 3130 of file res_rtp_asterisk.c.

3131{
3132 return elem == value;
3133}

References value.

Referenced by ast_rtcp_generate_nack(), and ast_rtp_read().

◆ handle_cli_rtcp_set_debug()

static char * handle_cli_rtcp_set_debug ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 9715 of file res_rtp_asterisk.c.

9716{
9717 switch (cmd) {
9718 case CLI_INIT:
9719 e->command = "rtcp set debug {on|off|ip}";
9720 e->usage =
9721 "Usage: rtcp set debug {on|off|ip host[:port]}\n"
9722 " Enable/Disable dumping of all RTCP packets. If 'ip' is\n"
9723 " specified, limit the dumped packets to those to and from\n"
9724 " the specified 'host' with optional port.\n";
9725 return NULL;
9726 case CLI_GENERATE:
9727 return NULL;
9728 }
9729
9730 if (a->argc == e->args) { /* set on or off */
9731 if (!strncasecmp(a->argv[e->args-1], "on", 2)) {
9733 memset(&rtcpdebugaddr, 0, sizeof(rtcpdebugaddr));
9734 ast_cli(a->fd, "RTCP Packet Debugging Enabled\n");
9735 return CLI_SUCCESS;
9736 } else if (!strncasecmp(a->argv[e->args-1], "off", 3)) {
9738 ast_cli(a->fd, "RTCP Packet Debugging Disabled\n");
9739 return CLI_SUCCESS;
9740 }
9741 } else if (a->argc == e->args +1) { /* ip */
9742 return rtcp_do_debug_ip(a);
9743 }
9744
9745 return CLI_SHOWUSAGE; /* default, failure */
9746}
#define CLI_SHOWUSAGE
Definition: cli.h:45
#define CLI_SUCCESS
Definition: cli.h:44
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
@ CLI_INIT
Definition: cli.h:152
@ CLI_GENERATE
Definition: cli.h:153
#define AST_LOG_CATEGORY_DISABLED
#define AST_LOG_CATEGORY_ENABLED
int ast_debug_category_set_sublevel(const char *name, int sublevel)
Set the debug category's sublevel.
static char * rtcp_do_debug_ip(struct ast_cli_args *a)
static struct ast_sockaddr rtcpdebugaddr
#define AST_LOG_CATEGORY_RTCP_PACKET
Definition: rtp_engine.h:2876
int args
This gets set in ast_cli_register()
Definition: cli.h:185
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
static struct test_val a

References a, ast_cli_entry::args, ast_cli(), ast_debug_category_set_sublevel(), AST_LOG_CATEGORY_DISABLED, AST_LOG_CATEGORY_ENABLED, AST_LOG_CATEGORY_RTCP_PACKET, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, NULL, rtcp_do_debug_ip(), rtcpdebugaddr, and ast_cli_entry::usage.

◆ handle_cli_rtcp_set_stats()

static char * handle_cli_rtcp_set_stats ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 9748 of file res_rtp_asterisk.c.

9749{
9750 switch (cmd) {
9751 case CLI_INIT:
9752 e->command = "rtcp set stats {on|off}";
9753 e->usage =
9754 "Usage: rtcp set stats {on|off}\n"
9755 " Enable/Disable dumping of RTCP stats.\n";
9756 return NULL;
9757 case CLI_GENERATE:
9758 return NULL;
9759 }
9760
9761 if (a->argc != e->args)
9762 return CLI_SHOWUSAGE;
9763
9764 if (!strncasecmp(a->argv[e->args-1], "on", 2))
9765 rtcpstats = 1;
9766 else if (!strncasecmp(a->argv[e->args-1], "off", 3))
9767 rtcpstats = 0;
9768 else
9769 return CLI_SHOWUSAGE;
9770
9771 ast_cli(a->fd, "RTCP Stats %s\n", rtcpstats ? "Enabled" : "Disabled");
9772 return CLI_SUCCESS;
9773}
static int rtcpstats

References a, ast_cli_entry::args, ast_cli(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, NULL, rtcpstats, and ast_cli_entry::usage.

◆ handle_cli_rtp_set_debug()

static char * handle_cli_rtp_set_debug ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 9634 of file res_rtp_asterisk.c.

9635{
9636 switch (cmd) {
9637 case CLI_INIT:
9638 e->command = "rtp set debug {on|off|ip}";
9639 e->usage =
9640 "Usage: rtp set debug {on|off|ip host[:port]}\n"
9641 " Enable/Disable dumping of all RTP packets. If 'ip' is\n"
9642 " specified, limit the dumped packets to those to and from\n"
9643 " the specified 'host' with optional port.\n";
9644 return NULL;
9645 case CLI_GENERATE:
9646 return NULL;
9647 }
9648
9649 if (a->argc == e->args) { /* set on or off */
9650 if (!strncasecmp(a->argv[e->args-1], "on", 2)) {
9652 memset(&rtpdebugaddr, 0, sizeof(rtpdebugaddr));
9653 ast_cli(a->fd, "RTP Packet Debugging Enabled\n");
9654 return CLI_SUCCESS;
9655 } else if (!strncasecmp(a->argv[e->args-1], "off", 3)) {
9657 ast_cli(a->fd, "RTP Packet Debugging Disabled\n");
9658 return CLI_SUCCESS;
9659 }
9660 } else if (a->argc == e->args +1) { /* ip */
9661 return rtp_do_debug_ip(a);
9662 }
9663
9664 return CLI_SHOWUSAGE; /* default, failure */
9665}
static struct ast_sockaddr rtpdebugaddr
static char * rtp_do_debug_ip(struct ast_cli_args *a)
#define AST_LOG_CATEGORY_RTP_PACKET
Definition: rtp_engine.h:2872

References a, ast_cli_entry::args, ast_cli(), ast_debug_category_set_sublevel(), AST_LOG_CATEGORY_DISABLED, AST_LOG_CATEGORY_ENABLED, AST_LOG_CATEGORY_RTP_PACKET, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, NULL, rtp_do_debug_ip(), rtpdebugaddr, and ast_cli_entry::usage.

◆ handle_cli_rtp_settings()

static char * handle_cli_rtp_settings ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 9668 of file res_rtp_asterisk.c.

9669{
9670#ifdef HAVE_PJPROJECT
9671 struct sockaddr_in stunaddr_copy;
9672#endif
9673 switch (cmd) {
9674 case CLI_INIT:
9675 e->command = "rtp show settings";
9676 e->usage =
9677 "Usage: rtp show settings\n"
9678 " Display RTP configuration settings\n";
9679 return NULL;
9680 case CLI_GENERATE:
9681 return NULL;
9682 }
9683
9684 if (a->argc != 3) {
9685 return CLI_SHOWUSAGE;
9686 }
9687
9688 ast_cli(a->fd, "\n\nGeneral Settings:\n");
9689 ast_cli(a->fd, "----------------\n");
9690 ast_cli(a->fd, " Port start: %d\n", rtpstart);
9691 ast_cli(a->fd, " Port end: %d\n", rtpend);
9692#ifdef SO_NO_CHECK
9693 ast_cli(a->fd, " Checksums: %s\n", AST_CLI_YESNO(nochecksums == 0));
9694#endif
9695 ast_cli(a->fd, " DTMF Timeout: %d\n", dtmftimeout);
9696 ast_cli(a->fd, " Strict RTP: %s\n", AST_CLI_YESNO(strictrtp));
9697
9698 if (strictrtp) {
9699 ast_cli(a->fd, " Probation: %d frames\n", learning_min_sequential);
9700 }
9701
9702 ast_cli(a->fd, " Replay Protect: %s\n", AST_CLI_YESNO(srtp_replay_protection));
9703#ifdef HAVE_PJPROJECT
9704 ast_cli(a->fd, " ICE support: %s\n", AST_CLI_YESNO(icesupport));
9705
9706 ast_rwlock_rdlock(&stunaddr_lock);
9707 memcpy(&stunaddr_copy, &stunaddr, sizeof(stunaddr));
9708 ast_rwlock_unlock(&stunaddr_lock);
9709 ast_cli(a->fd, " STUN address: %s:%d\n", ast_inet_ntoa(stunaddr_copy.sin_addr), htons(stunaddr_copy.sin_port));
9710#endif
9711 return CLI_SUCCESS;
9712}
#define AST_CLI_YESNO(x)
Return Yes or No depending on the argument.
Definition: cli.h:71
#define ast_rwlock_rdlock(a)
Definition: lock.h:235
#define ast_rwlock_unlock(a)
Definition: lock.h:234
const char * ast_inet_ntoa(struct in_addr ia)
thread-safe replacement for inet_ntoa().
Definition: utils.c:928
static int rtpend
static int learning_min_sequential
static int rtpstart
static int dtmftimeout

References a, ast_cli(), AST_CLI_YESNO, ast_inet_ntoa(), ast_rwlock_rdlock, ast_rwlock_unlock, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, dtmftimeout, learning_min_sequential, NULL, rtpend, rtpstart, srtp_replay_protection, strictrtp, and ast_cli_entry::usage.

◆ load_module()

static int load_module ( void  )
static

Definition at line 10227 of file res_rtp_asterisk.c.

10228{
10229#ifdef HAVE_PJPROJECT
10230 pj_lock_t *lock;
10231
10233
10235 if (pj_init() != PJ_SUCCESS) {
10237 }
10238
10239 if (pjlib_util_init() != PJ_SUCCESS) {
10240 rtp_terminate_pjproject();
10242 }
10243
10244 if (pjnath_init() != PJ_SUCCESS) {
10245 rtp_terminate_pjproject();
10247 }
10248
10249 ast_pjproject_caching_pool_init(&cachingpool, &pj_pool_factory_default_policy, 0);
10250
10251 pool = pj_pool_create(&cachingpool.factory, "timer", 512, 512, NULL);
10252
10253 if (pj_timer_heap_create(pool, 100, &timer_heap) != PJ_SUCCESS) {
10254 rtp_terminate_pjproject();
10256 }
10257
10258 if (pj_lock_create_recursive_mutex(pool, "rtp%p", &lock) != PJ_SUCCESS) {
10259 rtp_terminate_pjproject();
10261 }
10262
10263 pj_timer_heap_set_lock(timer_heap, lock, PJ_TRUE);
10264
10265 if (pj_thread_create(pool, "timer", &timer_worker_thread, NULL, 0, 0, &timer_thread) != PJ_SUCCESS) {
10266 rtp_terminate_pjproject();
10268 }
10269
10270#endif
10271
10272#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) && defined(HAVE_OPENSSL_BIO_METHOD)
10273 dtls_bio_methods = BIO_meth_new(BIO_TYPE_BIO, "rtp write");
10274 if (!dtls_bio_methods) {
10275#ifdef HAVE_PJPROJECT
10276 rtp_terminate_pjproject();
10277#endif
10279 }
10280 BIO_meth_set_write(dtls_bio_methods, dtls_bio_write);
10281 BIO_meth_set_ctrl(dtls_bio_methods, dtls_bio_ctrl);
10282 BIO_meth_set_create(dtls_bio_methods, dtls_bio_new);
10283 BIO_meth_set_destroy(dtls_bio_methods, dtls_bio_free);
10284#endif
10285
10287#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) && defined(HAVE_OPENSSL_BIO_METHOD)
10288 BIO_meth_free(dtls_bio_methods);
10289#endif
10290#ifdef HAVE_PJPROJECT
10291 rtp_terminate_pjproject();
10292#endif
10294 }
10295
10297#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) && defined(HAVE_OPENSSL_BIO_METHOD)
10298 BIO_meth_free(dtls_bio_methods);
10299#endif
10300#ifdef HAVE_PJPROJECT
10302 rtp_terminate_pjproject();
10303#endif
10305 }
10306
10307 rtp_reload(0, 0);
10308
10310}
ast_mutex_t lock
Definition: app_sla.c:331
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
int ast_sockaddr_parse(struct ast_sockaddr *addr, const char *str, int flags)
Parse an IPv4 or IPv6 address string.
Definition: netsock2.c:230
#define AST_PJPROJECT_INIT_LOG_LEVEL()
Get maximum log level pjproject was compiled with.
Definition: options.h:167
void ast_pjproject_caching_pool_init(pj_caching_pool *cp, const pj_pool_factory_policy *policy, pj_size_t max_capacity)
Initialize the caching pool factory.
static pj_caching_pool cachingpool
Pool factory used by pjlib to allocate memory.
static int rtp_reload(int reload, int by_external_config)
static struct ast_rtp_engine asterisk_rtp_engine
static struct ast_cli_entry cli_rtp[]
int ast_rtp_engine_unregister(struct ast_rtp_engine *engine)
Unregister an RTP engine.
Definition: rtp_engine.c:363
#define ast_rtp_engine_register(engine)
Definition: rtp_engine.h:840
#define ARRAY_LEN(a)
Definition: utils.h:666

References ARRAY_LEN, ast_cli_register_multiple, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, ast_pjproject_caching_pool_init(), AST_PJPROJECT_INIT_LOG_LEVEL, ast_rtp_engine_register, ast_rtp_engine_unregister(), ast_sockaddr_parse(), asterisk_rtp_engine, cachingpool, cli_rtp, lock, NULL, PARSE_PORT_IGNORE, and rtp_reload().

◆ ntp2timeval()

static void ntp2timeval ( unsigned int  msw,
unsigned int  lsw,
struct timeval *  tv 
)
static

Definition at line 4572 of file res_rtp_asterisk.c.

4573{
4574 tv->tv_sec = msw - 2208988800u;
4575 /* Reverse the sequence in timeval2ntp() */
4576 tv->tv_usec = ((((lsw >> 7) * 125) >> 7) * 125) >> 12;
4577}

Referenced by ast_rtcp_interpret().

◆ process_cn_rfc3389()

static struct ast_frame * process_cn_rfc3389 ( struct ast_rtp_instance instance,
unsigned char *  data,
int  len,
unsigned int  seqno,
unsigned int  timestamp,
int  payloadtype,
int  mark 
)
static

Definition at line 6027 of file res_rtp_asterisk.c.

6028{
6029 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
6030
6031 /* Convert comfort noise into audio with various codecs. Unfortunately this doesn't
6032 totally help us out because we don't have an engine to keep it going and we are not
6033 guaranteed to have it every 20ms or anything */
6035 ast_debug(0, "- RTP 3389 Comfort noise event: Format %s (len = %d)\n",
6037 }
6038
6039 if (!ast_test_flag(rtp, FLAG_3389_WARNING)) {
6040 struct ast_sockaddr remote_address = { {0,} };
6041
6042 ast_rtp_instance_get_remote_address(instance, &remote_address);
6043
6044 ast_log(LOG_NOTICE, "Comfort noise support incomplete in Asterisk (RFC 3389). Please turn off on client if possible. Client address: %s\n",
6045 ast_sockaddr_stringify(&remote_address));
6047 }
6048
6049 /* Must have at least one byte */
6050 if (!len) {
6051 return NULL;
6052 }
6053 if (len < 24) {
6054 rtp->f.data.ptr = rtp->rawdata + AST_FRIENDLY_OFFSET;
6055 rtp->f.datalen = len - 1;
6057 memcpy(rtp->f.data.ptr, data + 1, len - 1);
6058 } else {
6059 rtp->f.data.ptr = NULL;
6060 rtp->f.offset = 0;
6061 rtp->f.datalen = 0;
6062 }
6063 rtp->f.frametype = AST_FRAME_CNG;
6064 rtp->f.subclass.integer = data[0] & 0x7f;
6065 rtp->f.samples = 0;
6066 rtp->f.delivery.tv_usec = rtp->f.delivery.tv_sec = 0;
6067
6068 return &rtp->f;
6069}
#define FLAG_3389_WARNING

References ast_debug, ast_debug_rtp_packet_is_allowed, ast_format_get_name(), AST_FRAME_CNG, AST_FRIENDLY_OFFSET, ast_log, ast_rtp_instance_get_data(), ast_rtp_instance_get_remote_address, ast_set_flag, ast_sockaddr_stringify(), ast_test_flag, ast_frame::data, ast_frame::datalen, ast_frame::delivery, ast_rtp::f, FLAG_3389_WARNING, ast_frame::frametype, ast_frame_subclass::integer, ast_rtp::lastrxformat, len(), LOG_NOTICE, NULL, ast_frame::offset, ast_frame::ptr, ast_rtp::rawdata, ast_frame::samples, and ast_frame::subclass.

Referenced by ast_rtp_interpret().

◆ process_dtmf_cisco()

static struct ast_frame * process_dtmf_cisco ( struct ast_rtp_instance instance,
unsigned char *  data,
int  len,
unsigned int  seqno,
unsigned int  timestamp,
int  payloadtype,
int  mark 
)
static

Definition at line 5947 of file res_rtp_asterisk.c.

5948{
5949 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
5950 unsigned int event, flags, power;
5951 char resp = 0;
5952 unsigned char seq;
5953 struct ast_frame *f = NULL;
5954
5955 if (len < 4) {
5956 return NULL;
5957 }
5958
5959 /* The format of Cisco RTP DTMF packet looks like next:
5960 +0 - sequence number of DTMF RTP packet (begins from 1,
5961 wrapped to 0)
5962 +1 - set of flags
5963 +1 (bit 0) - flaps by different DTMF digits delimited by audio
5964 or repeated digit without audio???
5965 +2 (+4,+6,...) - power level? (rises from 0 to 32 at begin of tone
5966 then falls to 0 at its end)
5967 +3 (+5,+7,...) - detected DTMF digit (0..9,*,#,A-D,...)
5968 Repeated DTMF information (bytes 4/5, 6/7) is history shifted right
5969 by each new packet and thus provides some redundancy.
5970
5971 Sample of Cisco RTP DTMF packet is (all data in hex):
5972 19 07 00 02 12 02 20 02
5973 showing end of DTMF digit '2'.
5974
5975 The packets
5976 27 07 00 02 0A 02 20 02
5977 28 06 20 02 00 02 0A 02
5978 shows begin of new digit '2' with very short pause (20 ms) after
5979 previous digit '2'. Bit +1.0 flips at begin of new digit.
5980
5981 Cisco RTP DTMF packets comes as replacement of audio RTP packets
5982 so its uses the same sequencing and timestamping rules as replaced
5983 audio packets. Repeat interval of DTMF packets is 20 ms and not rely
5984 on audio framing parameters. Marker bit isn't used within stream of
5985 DTMFs nor audio stream coming immediately after DTMF stream. Timestamps
5986 are not sequential at borders between DTMF and audio streams,
5987 */
5988
5989 seq = data[0];
5990 flags = data[1];
5991 power = data[2];
5992 event = data[3] & 0x1f;
5993
5995 ast_debug(0, "Cisco DTMF Digit: %02x (len=%d, seq=%d, flags=%02x, power=%u, history count=%d)\n", event, len, seq, flags, power, (len - 4) / 2);
5996 if (event < 10) {
5997 resp = '0' + event;
5998 } else if (event < 11) {
5999 resp = '*';
6000 } else if (event < 12) {
6001 resp = '#';
6002 } else if (event < 16) {
6003 resp = 'A' + (event - 12);
6004 } else if (event < 17) {
6005 resp = 'X';
6006 }
6007 if ((!rtp->resp && power) || (rtp->resp && (rtp->resp != resp))) {
6008 rtp->resp = resp;
6009 /* Why we should care on DTMF compensation at reception? */
6011 f = create_dtmf_frame(instance, AST_FRAME_DTMF_BEGIN, 0);
6012 rtp->dtmfsamples = 0;
6013 }
6014 } else if ((rtp->resp == resp) && !power) {
6016 f->samples = rtp->dtmfsamples * (ast_rtp_get_rate(rtp->lastrxformat) / 1000);
6017 rtp->resp = 0;
6018 } else if (rtp->resp == resp) {
6019 rtp->dtmfsamples += 20 * (ast_rtp_get_rate(rtp->lastrxformat) / 1000);
6020 }
6021
6022 rtp->dtmf_timeout = 0;
6023
6024 return f;
6025}
static volatile unsigned int seq
Definition: app_sms.c:120
@ AST_RTP_PROPERTY_DTMF_COMPENSATE
Definition: rtp_engine.h:119
unsigned int flags
unsigned int flags
Definition: astman.c:222

References ast_debug, ast_debug_rtp_packet_is_allowed, AST_FRAME_DTMF_BEGIN, AST_FRAME_DTMF_END, ast_rtp_get_rate(), ast_rtp_instance_get_data(), ast_rtp_instance_get_prop(), AST_RTP_PROPERTY_DTMF_COMPENSATE, create_dtmf_frame(), ast_frame::data, ast_rtp::dtmf_timeout, ast_rtp::dtmfsamples, ast_frame::flags, ast_rtp::flags, ast_rtp::lastrxformat, len(), NULL, ast_rtp::resp, ast_frame::samples, and seq.

Referenced by ast_rtp_interpret().

◆ process_dtmf_rfc2833()

static void process_dtmf_rfc2833 ( struct ast_rtp_instance instance,
unsigned char *  data,
int  len,
unsigned int  seqno,
unsigned int  timestamp,
int  payloadtype,
int  mark,
struct frame_list frames 
)
static

Definition at line 5809 of file res_rtp_asterisk.c.

5810{
5811 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
5812 struct ast_sockaddr remote_address = { {0,} };
5813 unsigned int event, event_end, samples;
5814 char resp = 0;
5815 struct ast_frame *f = NULL;
5816
5817 ast_rtp_instance_get_remote_address(instance, &remote_address);
5818
5819 /* Figure out event, event end, and samples */
5820 event = ntohl(*((unsigned int *)(data)));
5821 event >>= 24;
5822 event_end = ntohl(*((unsigned int *)(data)));
5823 event_end <<= 8;
5824 event_end >>= 24;
5825 samples = ntohl(*((unsigned int *)(data)));
5826 samples &= 0xFFFF;
5827
5828 if (rtp_debug_test_addr(&remote_address)) {
5829 ast_verbose("Got RTP RFC2833 from %s (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6d, mark %d, event %08x, end %d, duration %-5.5u) \n",
5830 ast_sockaddr_stringify(&remote_address),
5831 payloadtype, seqno, timestamp, len, (mark?1:0), event, ((event_end & 0x80)?1:0), samples);
5832 }
5833
5834 /* Print out debug if turned on */
5836 ast_debug(0, "- RTP 2833 Event: %08x (len = %d)\n", event, len);
5837
5838 /* Figure out what digit was pressed */
5839 if (event < 10) {
5840 resp = '0' + event;
5841 } else if (event < 11) {
5842 resp = '*';
5843 } else if (event < 12) {
5844 resp = '#';
5845 } else if (event < 16) {
5846 resp = 'A' + (event - 12);
5847 } else if (event < 17) { /* Event 16: Hook flash */
5848 resp = 'X';
5849 } else {
5850 /* Not a supported event */
5851 ast_debug_rtp(1, "(%p) RTP ignoring RTP 2833 Event: %08x. Not a DTMF Digit.\n", instance, event);
5852 return;
5853 }
5854
5856 if (!rtp->last_end_timestamp.is_set || rtp->last_end_timestamp.ts != timestamp || (rtp->resp && rtp->resp != resp)) {
5857 rtp->resp = resp;
5858 rtp->dtmf_timeout = 0;
5860 f->len = 0;
5861 rtp->last_end_timestamp.ts = timestamp;
5862 rtp->last_end_timestamp.is_set = 1;
5864 }
5865 } else {
5866 /* The duration parameter measures the complete
5867 duration of the event (from the beginning) - RFC2833.
5868 Account for the fact that duration is only 16 bits long
5869 (about 8 seconds at 8000 Hz) and can wrap is digit
5870 is hold for too long. */
5871 unsigned int new_duration = rtp->dtmf_duration;
5872 unsigned int last_duration = new_duration & 0xFFFF;
5873
5874 if (last_duration > 64000 && samples < last_duration) {
5875 new_duration += 0xFFFF + 1;
5876 }
5877 new_duration = (new_duration & ~0xFFFF) | samples;
5878
5879 if (event_end & 0x80) {
5880 /* End event */
5881 if (rtp->last_seqno != seqno && (!rtp->last_end_timestamp.is_set || timestamp > rtp->last_end_timestamp.ts)) {
5882 rtp->last_end_timestamp.ts = timestamp;
5883 rtp->last_end_timestamp.is_set = 1;
5884 rtp->dtmf_duration = new_duration;
5885 rtp->resp = resp;
5888 rtp->resp = 0;
5889 rtp->dtmf_duration = rtp->dtmf_timeout = 0;
5892 ast_debug_rtp(1, "(%p) RTP dropping duplicate or out of order DTMF END frame (seqno: %u, ts %u, digit %c)\n",
5893 instance, seqno, timestamp, resp);
5894 }
5895 } else {
5896 /* Begin/continuation */
5897
5898 /* The second portion of the seqno check is to not mistakenly
5899 * stop accepting DTMF if the seqno rolls over beyond
5900 * 65535.
5901 */
5902 if ((rtp->last_seqno > seqno && rtp->last_seqno - seqno < 50)
5903 || (rtp->last_end_timestamp.is_set
5904 && timestamp <= rtp->last_end_timestamp.ts)) {
5905 /* Out of order frame. Processing this can cause us to
5906 * improperly duplicate incoming DTMF, so just drop
5907 * this.
5908 */
5910 ast_debug(0, "Dropping out of order DTMF frame (seqno %u, ts %u, digit %c)\n",
5911 seqno, timestamp, resp);
5912 }
5913 return;
5914 }
5915
5916 if (rtp->resp && rtp->resp != resp) {
5917 /* Another digit already began. End it */
5920 rtp->resp = 0;
5921 rtp->dtmf_duration = rtp->dtmf_timeout = 0;
5923 }
5924
5925 if (rtp->resp) {
5926 /* Digit continues */
5927 rtp->dtmf_duration = new_duration;
5928 } else {
5929 /* New digit began */
5930 rtp->resp = resp;
5932 rtp->dtmf_duration = samples;
5934 }
5935
5936 rtp->dtmf_timeout = timestamp + rtp->dtmf_duration + dtmftimeout;
5937 }
5938
5939 rtp->last_seqno = seqno;
5940 }
5941
5942 rtp->dtmfsamples = samples;
5943
5944 return;
5945}

References ast_debug, ast_debug_rtp, ast_debug_rtp_packet_is_allowed, AST_FRAME_DTMF_BEGIN, AST_FRAME_DTMF_END, ast_frdup, AST_LIST_INSERT_TAIL, ast_rtp_get_rate(), ast_rtp_instance_get_data(), ast_rtp_instance_get_prop(), ast_rtp_instance_get_remote_address, AST_RTP_PROPERTY_DTMF_COMPENSATE, ast_samp2tv(), ast_sockaddr_stringify(), ast_tv(), ast_tvdiff_ms(), ast_verbose(), create_dtmf_frame(), ast_frame::data, ast_rtp::dtmf_duration, ast_rtp::dtmf_timeout, ast_rtp::dtmfsamples, dtmftimeout, ast_frame_subclass::format, frames, optional_ts::is_set, ast_rtp::last_end_timestamp, ast_rtp::last_seqno, len(), ast_frame::len, NULL, ast_rtp::resp, rtp_debug_test_addr(), ast_frame::samples, ast_frame::seqno, ast_frame::subclass, and optional_ts::ts.

Referenced by ast_rtp_interpret().

◆ put_unaligned_time24()

static void put_unaligned_time24 ( void *  p,
uint32_t  time_msw,
uint32_t  time_lsw 
)
static

Definition at line 5034 of file res_rtp_asterisk.c.

5035{
5036 unsigned char *cp = p;
5037 uint32_t datum;
5038
5039 /* Convert the time to 6.18 format */
5040 datum = (time_msw << 18) & 0x00fc0000;
5041 datum |= (time_lsw >> 14) & 0x0003ffff;
5042
5043 cp[0] = datum >> 16;
5044 cp[1] = datum >> 8;
5045 cp[2] = datum;
5046}
Definition: ndbm.h:57

Referenced by ast_rtp_rtcp_handle_nack(), rtp_raw_write(), and rtp_transport_wide_cc_feedback_produce().

◆ red_t140_to_red()

static struct ast_frame * red_t140_to_red ( struct rtp_red red)
static

Definition at line 5279 of file res_rtp_asterisk.c.

5280{
5281 unsigned char *data = red->t140red.data.ptr;
5282 int len = 0;
5283 int i;
5284
5285 /* replace most aged generation */
5286 if (red->len[0]) {
5287 for (i = 1; i < red->num_gen+1; i++)
5288 len += red->len[i];
5289
5290 memmove(&data[red->hdrlen], &data[red->hdrlen+red->len[0]], len);
5291 }
5292
5293 /* Store length of each generation and primary data length*/
5294 for (i = 0; i < red->num_gen; i++)
5295 red->len[i] = red->len[i+1];
5296 red->len[i] = red->t140.datalen;
5297
5298 /* write each generation length in red header */
5299 len = red->hdrlen;
5300 for (i = 0; i < red->num_gen; i++) {
5301 len += data[i*4+3] = red->len[i];
5302 }
5303
5304 /* add primary data to buffer */
5305 memcpy(&data[len], red->t140.data.ptr, red->t140.datalen);
5306 red->t140red.datalen = len + red->t140.datalen;
5307
5308 /* no primary data and no generations to send */
5309 if (len == red->hdrlen && !red->t140.datalen) {
5310 return NULL;
5311 }
5312
5313 /* reset t.140 buffer */
5314 red->t140.datalen = 0;
5315
5316 return &red->t140red;
5317}
struct ast_frame t140
unsigned char len[AST_RED_MAX_GENERATION]
struct ast_frame t140red

References ast_frame::data, ast_frame::datalen, rtp_red::hdrlen, len(), rtp_red::len, NULL, rtp_red::num_gen, ast_frame::ptr, rtp_red::t140, and rtp_red::t140red.

Referenced by ast_rtp_write().

◆ red_write()

static int red_write ( const void *  data)
static

Write t140 redundancy frame.

Parameters
dataprimary data to be buffered

Scheduler callback

Definition at line 9039 of file res_rtp_asterisk.c.

9040{
9041 struct ast_rtp_instance *instance = (struct ast_rtp_instance*) data;
9042 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
9043
9044 ao2_lock(instance);
9045 if (rtp->red->t140.datalen > 0) {
9046 ast_rtp_write(instance, &rtp->red->t140);
9047 }
9048 ao2_unlock(instance);
9049
9050 return 1;
9051}
static int ast_rtp_write(struct ast_rtp_instance *instance, struct ast_frame *frame)

References ao2_lock, ao2_unlock, ast_rtp_instance_get_data(), ast_rtp_write(), ast_rtp_instance::data, ast_frame::datalen, ast_rtp::red, and rtp_red::t140.

Referenced by rtp_red_init().

◆ reload_module()

static int reload_module ( void  )
static

Definition at line 10195 of file res_rtp_asterisk.c.

10196{
10197 rtp_reload(1, 0);
10198 return 0;
10199}

References rtp_reload().

◆ rtcp_debug_test_addr()

static int rtcp_debug_test_addr ( struct ast_sockaddr addr)
inlinestatic

Definition at line 2847 of file res_rtp_asterisk.c.

2848{
2850 return 0;
2851 }
2853 if (rtcpdebugport) {
2854 return (ast_sockaddr_cmp(&rtcpdebugaddr, addr) == 0); /* look for RTCP packets from IP+Port */
2855 } else {
2856 return (ast_sockaddr_cmp_addr(&rtcpdebugaddr, addr) == 0); /* only look for RTCP packets from IP */
2857 }
2858 }
2859
2860 return 1;
2861}
int ast_sockaddr_cmp_addr(const struct ast_sockaddr *a, const struct ast_sockaddr *b)
Compares the addresses of two ast_sockaddr structures.
Definition: netsock2.c:413
static int rtcpdebugport
#define ast_debug_rtcp_packet_is_allowed
Definition: rtp_engine.h:2931

References ast_debug_rtcp_packet_is_allowed, ast_sockaddr_cmp(), ast_sockaddr_cmp_addr(), ast_sockaddr_isnull(), rtcpdebugaddr, and rtcpdebugport.

Referenced by ast_rtcp_calculate_sr_rr_statistics(), and ast_rtcp_interpret().

◆ rtcp_do_debug_ip()

static char * rtcp_do_debug_ip ( struct ast_cli_args a)
static

Definition at line 9617 of file res_rtp_asterisk.c.

9618{
9619 char *arg = ast_strdupa(a->argv[4]);
9620 char *debughost = NULL;
9621 char *debugport = NULL;
9622
9623 if (!ast_sockaddr_parse(&rtcpdebugaddr, arg, 0) || !ast_sockaddr_split_hostport(arg, &debughost, &debugport, 0)) {
9624 ast_cli(a->fd, "Lookup failed for '%s'\n", arg);
9625 return CLI_FAILURE;
9626 }
9627 rtcpdebugport = (!ast_strlen_zero(debugport) && debugport[0] != '0');
9628 ast_cli(a->fd, "RTCP Packet Debugging Enabled for address: %s\n",
9631 return CLI_SUCCESS;
9632}
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define CLI_FAILURE
Definition: cli.h:46
int ast_sockaddr_split_hostport(char *str, char **host, char **port, int flags)
Splits a string into its host and port components.
Definition: netsock2.c:164
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65

References a, ast_cli(), ast_debug_category_set_sublevel(), AST_LOG_CATEGORY_ENABLED, AST_LOG_CATEGORY_RTCP_PACKET, ast_sockaddr_parse(), ast_sockaddr_split_hostport(), ast_sockaddr_stringify(), ast_strdupa, ast_strlen_zero(), CLI_FAILURE, CLI_SUCCESS, NULL, rtcpdebugaddr, and rtcpdebugport.

Referenced by handle_cli_rtcp_set_debug().

◆ rtcp_mux()

static int rtcp_mux ( struct ast_rtp rtp,
const unsigned char *  packet 
)
static

Definition at line 3135 of file res_rtp_asterisk.c.

3136{
3137 uint8_t version;
3138 uint8_t pt;
3139 uint8_t m;
3140
3141 if (!rtp->rtcp || rtp->rtcp->type != AST_RTP_INSTANCE_RTCP_MUX) {
3142 return 0;
3143 }
3144
3145 version = (packet[0] & 0XC0) >> 6;
3146 if (version == 0) {
3147 /* version 0 indicates this is a STUN packet and shouldn't
3148 * be interpreted as a possible RTCP packet
3149 */
3150 return 0;
3151 }
3152
3153 /* The second octet of a packet will be one of the following:
3154 * For RTP: The marker bit (1 bit) and the RTP payload type (7 bits)
3155 * For RTCP: The payload type (8)
3156 *
3157 * RTP has a forbidden range of payload types (64-95) since these
3158 * will conflict with RTCP payload numbers if the marker bit is set.
3159 */
3160 m = packet[1] & 0x80;
3161 pt = packet[1] & 0x7F;
3162 if (m && pt >= 64 && pt <= 95) {
3163 return 1;
3164 }
3165 return 0;
3166}
@ AST_RTP_INSTANCE_RTCP_MUX
Definition: rtp_engine.h:286

References AST_RTP_INSTANCE_RTCP_MUX, ast_rtp::rtcp, ast_rtcp::type, and version.

Referenced by ast_rtp_read().

◆ rtcp_payload_subtype2str()

static const char * rtcp_payload_subtype2str ( unsigned int  pt,
unsigned int  subtype 
)
static

Definition at line 6399 of file res_rtp_asterisk.c.

6400{
6401 switch (pt) {
6402 case AST_RTP_RTCP_RTPFB:
6403 if (subtype == AST_RTP_RTCP_FMT_NACK) {
6404 return "NACK";
6405 }
6406 break;
6407 case RTCP_PT_PSFB:
6408 if (subtype == AST_RTP_RTCP_FMT_REMB) {
6409 return "REMB";
6410 }
6411 break;
6412 default:
6413 break;
6414 }
6415
6416 return NULL;
6417}

References AST_RTP_RTCP_FMT_NACK, AST_RTP_RTCP_FMT_REMB, AST_RTP_RTCP_RTPFB, NULL, and RTCP_PT_PSFB.

Referenced by ast_rtcp_interpret().

◆ rtcp_payload_type2str()

static const char * rtcp_payload_type2str ( unsigned int  pt)
static

Definition at line 6367 of file res_rtp_asterisk.c.

6368{
6369 const char *str;
6370
6371 switch (pt) {
6372 case RTCP_PT_SR:
6373 str = "Sender Report";
6374 break;
6375 case RTCP_PT_RR:
6376 str = "Receiver Report";
6377 break;
6378 case RTCP_PT_FUR:
6379 /* Full INTRA-frame Request / Fast Update Request */
6380 str = "H.261 FUR";
6381 break;
6382 case RTCP_PT_PSFB:
6383 /* Payload Specific Feed Back */
6384 str = "PSFB";
6385 break;
6386 case RTCP_PT_SDES:
6387 str = "Source Description";
6388 break;
6389 case RTCP_PT_BYE:
6390 str = "BYE";
6391 break;
6392 default:
6393 str = "Unknown";
6394 break;
6395 }
6396 return str;
6397}
const char * str
Definition: app_jack.c:147

References RTCP_PT_BYE, RTCP_PT_FUR, RTCP_PT_PSFB, RTCP_PT_RR, RTCP_PT_SDES, RTCP_PT_SR, and str.

Referenced by ast_rtcp_interpret().

◆ rtcp_recvfrom()

static int rtcp_recvfrom ( struct ast_rtp_instance instance,
void *  buf,
size_t  size,
int  flags,
struct ast_sockaddr sa 
)
static
Precondition
instance is locked

Definition at line 3373 of file res_rtp_asterisk.c.

3374{
3375 return __rtp_recvfrom(instance, buf, size, flags, sa, 1);
3376}
static int __rtp_recvfrom(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int rtcp)

References __rtp_recvfrom(), and buf.

Referenced by ast_rtcp_read().

◆ rtcp_sendto()

static int rtcp_sendto ( struct ast_rtp_instance instance,
void *  buf,
size_t  size,
int  flags,
struct ast_sockaddr sa,
int *  ice 
)
static
Precondition
instance is locked

Definition at line 3441 of file res_rtp_asterisk.c.

3442{
3443 return __rtp_sendto(instance, buf, size, flags, sa, 1, ice, 1);
3444}
static int __rtp_sendto(struct ast_rtp_instance *instance, void *buf, size_t size, int flags, struct ast_sockaddr *sa, int rtcp, int *via_ice, int use_srtp)

References __rtp_sendto(), and buf.

Referenced by ast_rtcp_write(), ast_rtp_read(), rtp_transport_wide_cc_feedback_produce(), rtp_write_rtcp_fir(), and rtp_write_rtcp_psfb().

◆ rtp_allocate_transport()

static int rtp_allocate_transport ( struct ast_rtp_instance instance,
struct ast_rtp rtp 
)
static

Definition at line 3967 of file res_rtp_asterisk.c.

3968{
3969 int x, startplace, i, maxloops;
3970
3972
3973 /* Create a new socket for us to listen on and use */
3974 if ((rtp->s =
3975 create_new_socket("RTP",
3976 ast_sockaddr_is_ipv4(&rtp->bind_address) ? AF_INET :
3977 ast_sockaddr_is_ipv6(&rtp->bind_address) ? AF_INET6 : -1)) < 0) {
3978 ast_log(LOG_WARNING, "Failed to create a new socket for RTP instance '%p'\n", instance);
3979 return -1;
3980 }
3981
3982 /* Now actually find a free RTP port to use */
3983 x = (ast_random() % (rtpend - rtpstart)) + rtpstart;
3984 x = x & ~1;
3985 startplace = x;
3986
3987 /* Protection against infinite loops in the case there is a potential case where the loop is not broken such as an odd
3988 start port sneaking in (even though this condition is checked at load.) */
3989 maxloops = rtpend - rtpstart;
3990 for (i = 0; i <= maxloops; i++) {
3992 /* Try to bind, this will tell us whether the port is available or not */
3993 if (!ast_bind(rtp->s, &rtp->bind_address)) {
3994 ast_debug_rtp(1, "(%p) RTP allocated port %d\n", instance, x);
3996 ast_test_suite_event_notify("RTP_PORT_ALLOCATED", "Port: %d", x);
3997 break;
3998 }
3999
4000 x += 2;
4001 if (x > rtpend) {
4002 x = (rtpstart + 1) & ~1;
4003 }
4004
4005 /* See if we ran out of ports or if the bind actually failed because of something other than the address being in use */
4006 if (x == startplace || (errno != EADDRINUSE && errno != EACCES)) {
4007 ast_log(LOG_ERROR, "Oh dear... we couldn't allocate a port for RTP instance '%p'\n", instance);
4008 close(rtp->s);
4009 rtp->s = -1;
4010 return -1;
4011 }
4012 }
4013
4014#ifdef HAVE_PJPROJECT
4015 /* Initialize synchronization aspects */
4016 ast_cond_init(&rtp->cond, NULL);
4017
4018 generate_random_string(rtp->local_ufrag, sizeof(rtp->local_ufrag));
4019 generate_random_string(rtp->local_passwd, sizeof(rtp->local_passwd));
4020
4021 /* Create an ICE session for ICE negotiation */
4022 if (icesupport) {
4023 rtp->ice_num_components = 2;
4024 ast_debug_ice(2, "(%p) ICE creating session %s (%d)\n", instance,
4026 if (ice_create(instance, &rtp->bind_address, x, 0)) {
4027 ast_log(LOG_NOTICE, "(%p) ICE failed to create session\n", instance);
4028 } else {
4029 rtp->ice_port = x;
4030 ast_sockaddr_copy(&rtp->ice_original_rtp_addr, &rtp->bind_address);
4031 }
4032 }
4033#endif
4034
4035#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
4036 rtp->rekeyid = -1;
4037 rtp->dtls.timeout_timer = -1;
4038#endif
4039
4040 return 0;
4041}
#define ast_cond_init(cond, attr)
Definition: lock.h:201
static char * generate_random_string(char *buf, size_t size)
Generate 32 byte random string (stolen from chan_sip.c)
Definition: res_calendar.c:719
#define ast_debug_ice(sublevel,...)
Log debug level ICE information.
Definition: rtp_engine.h:2952

References ast_bind(), ast_cond_init, ast_debug_ice, ast_debug_rtp, ast_log, ast_random(), ast_rtp_instance_set_local_address(), ast_sockaddr_copy(), ast_sockaddr_is_ipv4(), ast_sockaddr_is_ipv6(), ast_sockaddr_set_port, ast_sockaddr_stringify(), ast_test_suite_event_notify, ast_rtp::bind_address, create_new_socket(), errno, generate_random_string(), LOG_ERROR, LOG_NOTICE, LOG_WARNING, NULL, rtpend, rtpstart, ast_rtp::s, STRICT_RTP_CLOSED, STRICT_RTP_OPEN, ast_rtp::strict_rtp_state, and strictrtp.

Referenced by ast_rtp_bundle(), and ast_rtp_new().

◆ rtp_deallocate_transport()

static void rtp_deallocate_transport ( struct ast_rtp_instance instance,
struct ast_rtp rtp 
)
static

Definition at line 4043 of file res_rtp_asterisk.c.

4044{
4045 int saved_rtp_s = rtp->s;
4046#ifdef HAVE_PJPROJECT
4047 struct timeval wait = ast_tvadd(ast_tvnow(), ast_samp2tv(TURN_STATE_WAIT_TIME, 1000));
4048 struct timespec ts = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000, };
4049#endif
4050
4051#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
4052 ast_rtp_dtls_stop(instance);
4053#endif
4054
4055 /* Close our own socket so we no longer get packets */
4056 if (rtp->s > -1) {
4057 close(rtp->s);
4058 rtp->s = -1;
4059 }
4060
4061 /* Destroy RTCP if it was being used */
4062 if (rtp->rtcp && rtp->rtcp->s > -1) {
4063 if (saved_rtp_s != rtp->rtcp->s) {
4064 close(rtp->rtcp->s);
4065 }
4066 rtp->rtcp->s = -1;
4067 }
4068
4069#ifdef HAVE_PJPROJECT
4070 pj_thread_register_check();
4071
4072 /*
4073 * The instance lock is already held.
4074 *
4075 * Destroy the RTP TURN relay if being used
4076 */
4077 if (rtp->turn_rtp) {
4078 rtp->turn_state = PJ_TURN_STATE_NULL;
4079
4080 /* Release the instance lock to avoid deadlock with PJPROJECT group lock */
4081 ao2_unlock(instance);
4082 pj_turn_sock_destroy(rtp->turn_rtp);
4083 ao2_lock(instance);
4084 while (rtp->turn_state != PJ_TURN_STATE_DESTROYING) {
4085 ast_cond_timedwait(&rtp->cond, ao2_object_get_lockaddr(instance), &ts);
4086 }
4087 rtp->turn_rtp = NULL;
4088 }
4089
4090 /* Destroy the RTCP TURN relay if being used */
4091 if (rtp->turn_rtcp) {
4092 rtp->turn_state = PJ_TURN_STATE_NULL;
4093
4094 /* Release the instance lock to avoid deadlock with PJPROJECT group lock */
4095 ao2_unlock(instance);
4096 pj_turn_sock_destroy(rtp->turn_rtcp);
4097 ao2_lock(instance);
4098 while (rtp->turn_state != PJ_TURN_STATE_DESTROYING) {
4099 ast_cond_timedwait(&rtp->cond, ao2_object_get_lockaddr(instance), &ts);
4100 }
4101 rtp->turn_rtcp = NULL;
4102 }
4103
4104 ast_debug_ice(2, "(%p) ICE RTP transport deallocating\n", instance);
4105 /* Destroy any ICE session */
4106 ast_rtp_ice_stop(instance);
4107
4108 /* Destroy any candidates */
4109 if (rtp->ice_local_candidates) {
4110 ao2_ref(rtp->ice_local_candidates, -1);
4111 rtp->ice_local_candidates = NULL;
4112 }
4113
4114 if (rtp->ice_active_remote_candidates) {
4115 ao2_ref(rtp->ice_active_remote_candidates, -1);
4116 rtp->ice_active_remote_candidates = NULL;
4117 }
4118
4119 if (rtp->ice_proposed_remote_candidates) {
4120 ao2_ref(rtp->ice_proposed_remote_candidates, -1);
4121 rtp->ice_proposed_remote_candidates = NULL;
4122 }
4123
4124 if (rtp->ioqueue) {
4125 /*
4126 * We cannot hold the instance lock because we could wait
4127 * for the ioqueue thread to die and we might deadlock as
4128 * a result.
4129 */
4130 ao2_unlock(instance);
4131 rtp_ioqueue_thread_remove(rtp->ioqueue);
4132 ao2_lock(instance);
4133 rtp->ioqueue = NULL;
4134 }
4135#endif
4136}
void * ao2_object_get_lockaddr(void *obj)
Return the mutex lock address of an object.
Definition: astobj2.c:476
#define ast_cond_timedwait(cond, mutex, time)
Definition: lock.h:206
#define TURN_STATE_WAIT_TIME

References ao2_lock, ao2_object_get_lockaddr(), ao2_ref, ao2_unlock, ast_cond_timedwait, ast_debug_ice, ast_samp2tv(), ast_tvadd(), ast_tvnow(), NULL, ast_rtp::rtcp, ast_rtp::s, ast_rtcp::s, and TURN_STATE_WAIT_TIME.

Referenced by ast_rtp_bundle(), and ast_rtp_destroy().

◆ rtp_debug_test_addr()

static int rtp_debug_test_addr ( struct ast_sockaddr addr)
inlinestatic

Definition at line 2831 of file res_rtp_asterisk.c.

2832{
2834 return 0;
2835 }
2837 if (rtpdebugport) {
2838 return (ast_sockaddr_cmp(&rtpdebugaddr, addr) == 0); /* look for RTP packets from IP+Port */
2839 } else {
2840 return (ast_sockaddr_cmp_addr(&rtpdebugaddr, addr) == 0); /* only look for RTP packets from IP */
2841 }
2842 }
2843
2844 return 1;
2845}
static int rtpdebugport

References ast_debug_rtp_packet_is_allowed, ast_sockaddr_cmp(), ast_sockaddr_cmp_addr(), ast_sockaddr_isnull(), rtpdebugaddr, and rtpdebugport.

Referenced by ast_rtp_dtmf_begin(), ast_rtp_dtmf_continuation(), ast_rtp_dtmf_end_with_duration(), ast_rtp_read(), ast_rtp_sendcng(), bridge_p2p_rtp_write(), process_dtmf_rfc2833(), and rtp_raw_write().

◆ rtp_do_debug_ip()

static char * rtp_do_debug_ip ( struct ast_cli_args a)
static

Definition at line 9600 of file res_rtp_asterisk.c.

9601{
9602 char *arg = ast_strdupa(a->argv[4]);
9603 char *debughost = NULL;
9604 char *debugport = NULL;
9605
9606 if (!ast_sockaddr_parse(&rtpdebugaddr, arg, 0) || !ast_sockaddr_split_hostport(arg, &debughost, &debugport, 0)) {
9607 ast_cli(a->fd, "Lookup failed for '%s'\n", arg);
9608 return CLI_FAILURE;
9609 }
9610 rtpdebugport = (!ast_strlen_zero(debugport) && debugport[0] != '0');
9611 ast_cli(a->fd, "RTP Packet Debugging Enabled for address: %s\n",
9614 return CLI_SUCCESS;
9615}

References a, ast_cli(), ast_debug_category_set_sublevel(), AST_LOG_CATEGORY_ENABLED, AST_LOG_CATEGORY_RTP_PACKET, ast_sockaddr_parse(), ast_sockaddr_split_hostport(), ast_sockaddr_stringify(), ast_strdupa, ast_strlen_zero(), CLI_FAILURE, CLI_SUCCESS, NULL, rtpdebugaddr, and rtpdebugport.

Referenced by handle_cli_rtp_set_debug().

◆ rtp_find_instance_by_media_source_ssrc()

static struct ast_rtp_instance * rtp_find_instance_by_media_source_ssrc ( struct ast_rtp_instance instance,
struct ast_rtp rtp,
unsigned int  ssrc 
)
static
Precondition
instance is locked

Definition at line 6361 of file res_rtp_asterisk.c.

6363{
6364 return __rtp_find_instance_by_ssrc(instance, rtp, ssrc, 1);
6365}
static struct ast_rtp_instance * __rtp_find_instance_by_ssrc(struct ast_rtp_instance *instance, struct ast_rtp *rtp, unsigned int ssrc, int source)

References __rtp_find_instance_by_ssrc().

Referenced by ast_rtcp_interpret().

◆ rtp_find_instance_by_packet_source_ssrc()

static struct ast_rtp_instance * rtp_find_instance_by_packet_source_ssrc ( struct ast_rtp_instance instance,
struct ast_rtp rtp,
unsigned int  ssrc 
)
static
Precondition
instance is locked

Definition at line 6354 of file res_rtp_asterisk.c.

6356{
6357 return __rtp_find_instance_by_ssrc(instance, rtp, ssrc, 0);
6358}

References __rtp_find_instance_by_ssrc().

Referenced by ast_rtcp_interpret(), and ast_rtp_read().

◆ rtp_instance_parse_extmap_extensions()

static void rtp_instance_parse_extmap_extensions ( struct ast_rtp_instance instance,
struct ast_rtp rtp,
unsigned char *  extension,
int  len 
)
static

Definition at line 7615 of file res_rtp_asterisk.c.

7617{
7618 int transport_wide_cc_id = ast_rtp_instance_extmap_get_id(instance, AST_RTP_EXTENSION_TRANSPORT_WIDE_CC);
7619 int pos = 0;
7620
7621 /* We currently only care about the transport-cc extension, so if that's not negotiated then do nothing */
7622 if (transport_wide_cc_id == -1) {
7623 return;
7624 }
7625
7626 /* Only while we do not exceed available extension data do we continue */
7627 while (pos < len) {
7628 int id = extension[pos] >> 4;
7629 int extension_len = (extension[pos] & 0xF) + 1;
7630
7631 /* We've handled the first byte as it contains the extension id and length, so always
7632 * skip ahead now
7633 */
7634 pos += 1;
7635
7636 if (id == 0) {
7637 /* From the RFC:
7638 * In both forms, padding bytes have the value of 0 (zero). They may be
7639 * placed between extension elements, if desired for alignment, or after
7640 * the last extension element, if needed for padding. A padding byte
7641 * does not supply the ID of an element, nor the length field. When a
7642 * padding byte is found, it is ignored and the parser moves on to
7643 * interpreting the next byte.
7644 */
7645 continue;
7646 } else if (id == 15) {
7647 /* From the RFC:
7648 * The local identifier value 15 is reserved for future extension and
7649 * MUST NOT be used as an identifier. If the ID value 15 is
7650 * encountered, its length field should be ignored, processing of the
7651 * entire extension should terminate at that point, and only the
7652 * extension elements present prior to the element with ID 15
7653 * considered.
7654 */
7655 break;
7656 } else if ((pos + extension_len) > len) {
7657 /* The extension is corrupted and is stating that it contains more data than is
7658 * available in the extensions data.
7659 */
7660 break;
7661 }
7662
7663 /* If this is transport-cc then we need to parse it further */
7664 if (id == transport_wide_cc_id) {
7665 rtp_instance_parse_transport_wide_cc(instance, rtp, extension + pos, extension_len);
7666 }
7667
7668 /* Skip ahead to the next extension */
7669 pos += extension_len;
7670 }
7671}
static void rtp_instance_parse_transport_wide_cc(struct ast_rtp_instance *instance, struct ast_rtp *rtp, unsigned char *data, int len)

References AST_RTP_EXTENSION_TRANSPORT_WIDE_CC, ast_rtp_instance_extmap_get_id(), len(), and rtp_instance_parse_transport_wide_cc().

Referenced by ast_rtp_interpret().

◆ rtp_instance_parse_transport_wide_cc()

static void rtp_instance_parse_transport_wide_cc ( struct ast_rtp_instance instance,
struct ast_rtp rtp,
unsigned char *  data,
int  len 
)
static

Definition at line 7560 of file res_rtp_asterisk.c.

7562{
7563 uint16_t *seqno = (uint16_t *)data;
7565 struct ast_rtp_instance *transport = rtp->bundled ? rtp->bundled : instance;
7566 struct ast_rtp *transport_rtp = ast_rtp_instance_get_data(transport);
7567
7568 /* If the sequence number has cycled over then record it as such */
7569 if (((int)transport_rtp->transport_wide_cc.last_seqno - (int)ntohs(*seqno)) > 100) {
7570 transport_rtp->transport_wide_cc.cycles += RTP_SEQ_MOD;
7571 }
7572
7573 /* Populate the statistics information for this packet */
7574 statistics.seqno = transport_rtp->transport_wide_cc.cycles + ntohs(*seqno);
7575 statistics.received = ast_tvnow();
7576
7577 /* We allow at a maximum 1000 packet statistics in play at a time, if we hit the
7578 * limit we give up and start fresh.
7579 */
7580 if (AST_VECTOR_SIZE(&transport_rtp->transport_wide_cc.packet_statistics) > 1000) {
7582 }
7583
7584 if (!AST_VECTOR_SIZE(&transport_rtp->transport_wide_cc.packet_statistics) ||
7585 statistics.seqno > transport_rtp->transport_wide_cc.last_extended_seqno) {
7586 /* This is the expected path */
7588 return;
7589 }
7590
7591 transport_rtp->transport_wide_cc.last_extended_seqno = statistics.seqno;
7592 transport_rtp->transport_wide_cc.last_seqno = ntohs(*seqno);
7593 } else {
7594 /* This packet was out of order, so reorder it within the vector accordingly */
7597 return;
7598 }
7599 }
7600
7601 /* If we have not yet scheduled the periodic sending of feedback for this transport then do so */
7602 if (transport_rtp->transport_wide_cc.schedid < 0 && transport_rtp->rtcp) {
7603 ast_debug_rtcp(1, "(%p) RTCP starting transport-cc feedback transmission on RTP instance '%p'\n", instance, transport);
7604 ao2_ref(transport, +1);
7605 transport_rtp->transport_wide_cc.schedid = ast_sched_add(rtp->sched, 1000,
7607 if (transport_rtp->transport_wide_cc.schedid < 0) {
7608 ao2_ref(transport, -1);
7609 ast_log(LOG_WARNING, "Scheduling RTCP transport-cc feedback transmission failed on RTP instance '%p'\n",
7610 transport);
7611 }
7612 }
7613}
static int rtp_transport_wide_cc_feedback_produce(const void *data)
static int rtp_transport_wide_cc_packet_statistics_cmp(struct rtp_transport_wide_cc_packet_statistics a, struct rtp_transport_wide_cc_packet_statistics b)
Packet statistics (used for transport-cc)
static void statistics(void)
Definition: utils/frame.c:287

References ao2_ref, ast_debug_rtcp, ast_log, ast_rtp_instance_get_data(), ast_sched_add(), ast_tvnow(), AST_VECTOR_ADD_SORTED, AST_VECTOR_APPEND, AST_VECTOR_ELEM_CLEANUP_NOOP, AST_VECTOR_RESET, AST_VECTOR_SIZE, ast_rtp::bundled, rtp_transport_wide_cc_statistics::cycles, rtp_transport_wide_cc_statistics::last_extended_seqno, rtp_transport_wide_cc_statistics::last_seqno, LOG_WARNING, rtp_transport_wide_cc_statistics::packet_statistics, ast_rtp::rtcp, RTP_SEQ_MOD, rtp_transport_wide_cc_feedback_produce(), rtp_transport_wide_cc_packet_statistics_cmp(), ast_rtp::sched, rtp_transport_wide_cc_statistics::schedid, rtp_transport_wide_cc_packet_statistics::seqno, ast_rtp::seqno, statistics(), and ast_rtp::transport_wide_cc.

Referenced by rtp_instance_parse_extmap_extensions().

◆ rtp_instance_unlock()

static void rtp_instance_unlock ( struct ast_rtp_instance instance)
static

Definition at line 7290 of file res_rtp_asterisk.c.

7291{
7292 if (instance) {
7293 ao2_unlock(instance);
7294 }
7295}

References ao2_unlock.

Referenced by ast_rtp_read().

◆ rtp_learning_rtp_seq_update()

static int rtp_learning_rtp_seq_update ( struct rtp_learning_info info,
uint16_t  seq 
)
static

Definition at line 3541 of file res_rtp_asterisk.c.

3542{
3543 if (seq == (uint16_t) (info->max_seq + 1)) {
3544 /* packet is in sequence */
3545 info->packets--;
3546 } else {
3547 /* Sequence discontinuity; reset */
3548 info->packets = learning_min_sequential - 1;
3549 info->received = ast_tvnow();
3550 }
3551
3552 /* Only check time if strictrtp is set to yes. Otherwise, we only needed to check seqno */
3553 if (strictrtp == STRICT_RTP_YES) {
3554 switch (info->stream_type) {
3557 /*
3558 * Protect against packet floods by checking that we
3559 * received the packet sequence in at least the minimum
3560 * allowed time.
3561 */
3562 if (ast_tvzero(info->received)) {
3563 info->received = ast_tvnow();
3564 } else if (!info->packets
3566 /* Packet flood; reset */
3567 info->packets = learning_min_sequential - 1;
3568 info->received = ast_tvnow();
3569 }
3570 break;
3574 case AST_MEDIA_TYPE_END:
3575 break;
3576 }
3577 }
3578
3579 info->max_seq = seq;
3580
3581 return info->packets;
3582}
@ AST_MEDIA_TYPE_END
Definition: codec.h:36
def info(msg)
static int learning_min_duration

References AST_MEDIA_TYPE_AUDIO, AST_MEDIA_TYPE_END, AST_MEDIA_TYPE_IMAGE, AST_MEDIA_TYPE_TEXT, AST_MEDIA_TYPE_UNKNOWN, AST_MEDIA_TYPE_VIDEO, ast_tvdiff_ms(), ast_tvnow(), ast_tvzero(), sip_to_pjsip::info(), learning_min_duration, learning_min_sequential, seq, STRICT_RTP_YES, and strictrtp.

Referenced by ast_rtp_read().

◆ rtp_learning_seq_init()

static void rtp_learning_seq_init ( struct rtp_learning_info info,
uint16_t  seq 
)
static

Definition at line 3524 of file res_rtp_asterisk.c.

3525{
3526 info->max_seq = seq;
3527 info->packets = learning_min_sequential;
3528 memset(&info->received, 0, sizeof(info->received));
3529}

References sip_to_pjsip::info(), learning_min_sequential, and seq.

Referenced by ast_rtp_read(), and rtp_learning_start().

◆ rtp_learning_start()

static void rtp_learning_start ( struct ast_rtp rtp)
static

Start the strictrtp learning mode.

Parameters
rtpRTP session description

Definition at line 3589 of file res_rtp_asterisk.c.

3590{
3592 memset(&rtp->rtp_source_learn.proposed_address, 0,
3593 sizeof(rtp->rtp_source_learn.proposed_address));
3595 rtp_learning_seq_init(&rtp->rtp_source_learn, (uint16_t) rtp->lastrxseqno);
3596}

References ast_tvnow(), ast_rtp::lastrxseqno, rtp_learning_info::proposed_address, rtp_learning_seq_init(), ast_rtp::rtp_source_learn, rtp_learning_info::start, STRICT_RTP_LEARN, and ast_rtp::strict_rtp_state.

Referenced by ast_rtp_remote_address_set().

◆ rtp_raw_write()

static int rtp_raw_write ( struct ast_rtp_instance instance,
struct ast_frame frame,
int  codec 
)
static
Precondition
instance is locked

Definition at line 5049 of file res_rtp_asterisk.c.

5050{
5051 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
5052 int pred, mark = 0;
5053 unsigned int ms = calc_txstamp(rtp, &frame->delivery);
5054 struct ast_sockaddr remote_address = { {0,} };
5055 int rate = ast_rtp_get_rate(frame->subclass.format) / 1000;
5056 unsigned int seqno;
5057#ifdef TEST_FRAMEWORK
5058 struct ast_rtp_engine_test *test = ast_rtp_instance_get_test(instance);
5059#endif
5060
5062 frame->samples /= 2;
5063 }
5064
5065 if (rtp->sending_digit) {
5066 return 0;
5067 }
5068
5069#ifdef TEST_FRAMEWORK
5070 if (test && test->send_report) {
5071 test->send_report = 0;
5072 ast_rtcp_write(instance);
5073 return 0;
5074 }
5075#endif
5076
5077 if (frame->frametype == AST_FRAME_VOICE) {
5078 pred = rtp->lastts + frame->samples;
5079
5080 /* Re-calculate last TS */
5081 rtp->lastts = rtp->lastts + ms * rate;
5082 if (ast_tvzero(frame->delivery)) {
5083 /* If this isn't an absolute delivery time, Check if it is close to our prediction,
5084 and if so, go with our prediction */
5085 if (abs((int)rtp->lastts - pred) < MAX_TIMESTAMP_SKEW) {
5086 rtp->lastts = pred;
5087 } else {
5088 ast_debug_rtp(3, "(%p) RTP audio difference is %d, ms is %u\n",
5089 instance, abs((int)rtp->lastts - pred), ms);
5090 mark = 1;
5091 }
5092 }
5093 } else if (frame->frametype == AST_FRAME_VIDEO) {
5094 mark = frame->subclass.frame_ending;
5095 pred = rtp->lastovidtimestamp + frame->samples;
5096 /* Re-calculate last TS */
5097 rtp->lastts = rtp->lastts + ms * 90;
5098 /* If it's close to our prediction, go for it */
5099 if (ast_tvzero(frame->delivery)) {
5100 if (abs((int)rtp->lastts - pred) < 7200) {
5101 rtp->lastts = pred;
5102 rtp->lastovidtimestamp += frame->samples;
5103 } else {
5104 ast_debug_rtp(3, "(%p) RTP video difference is %d, ms is %u (%u), pred/ts/samples %u/%d/%d\n",
5105 instance, abs((int)rtp->lastts - pred), ms, ms * 90, rtp->lastts, pred, frame->samples);
5106 rtp->lastovidtimestamp = rtp->lastts;
5107 }
5108 }
5109 } else {
5110 pred = rtp->lastotexttimestamp + frame->samples;
5111 /* Re-calculate last TS */
5112 rtp->lastts = rtp->lastts + ms;
5113 /* If it's close to our prediction, go for it */
5114 if (ast_tvzero(frame->delivery)) {
5115 if (abs((int)rtp->lastts - pred) < 7200) {
5116 rtp->lastts = pred;
5117 rtp->lastotexttimestamp += frame->samples;
5118 } else {
5119 ast_debug_rtp(3, "(%p) RTP other difference is %d, ms is %u, pred/ts/samples %u/%d/%d\n",
5120 instance, abs((int)rtp->lastts - pred), ms, rtp->lastts, pred, frame->samples);
5121 rtp->lastotexttimestamp = rtp->lastts;
5122 }
5123 }
5124 }
5125
5126 /* If we have been explicitly told to set the marker bit then do so */
5128 mark = 1;
5130 }
5131
5132 /* If the timestamp for non-digt packets has moved beyond the timestamp for digits, update the digit timestamp */
5133 if (rtp->lastts > rtp->lastdigitts) {
5134 rtp->lastdigitts = rtp->lastts;
5135 }
5136
5137 /* Assume that the sequence number we expect to use is what will be used until proven otherwise */
5138 seqno = rtp->seqno;
5139
5140 /* If the frame contains sequence number information use it to influence our sequence number */
5142 if (rtp->expectedseqno != -1) {
5143 /* Determine where the frame from the core is in relation to where we expected */
5144 int difference = frame->seqno - rtp->expectedseqno;
5145
5146 /* If there is a substantial difference then we've either got packets really out
5147 * of order, or the source is RTP and it has cycled. If this happens we resync
5148 * the sequence number adjustments to this frame. If we also have packet loss
5149 * things won't be reflected correctly but it will sort itself out after a bit.
5150 */
5151 if (abs(difference) > 100) {
5152 difference = 0;
5153 }
5154
5155 /* Adjust the sequence number being used for this packet accordingly */
5156 seqno += difference;
5157
5158 if (difference >= 0) {
5159 /* This frame is on time or in the future */
5160 rtp->expectedseqno = frame->seqno + 1;
5161 rtp->seqno += difference;
5162 }
5163 } else {
5164 /* This is the first frame with sequence number we've seen, so start keeping track */
5165 rtp->expectedseqno = frame->seqno + 1;
5166 }
5167 } else {
5168 rtp->expectedseqno = -1;
5169 }
5170
5172 rtp->lastts = frame->ts * rate;
5173 }
5174
5175 ast_rtp_instance_get_remote_address(instance, &remote_address);
5176
5177 /* If we know the remote address construct a packet and send it out */
5178 if (!ast_sockaddr_isnull(&remote_address)) {
5179 int hdrlen = 12;
5180 int res;
5181 int ice;
5182 int ext = 0;
5183 int abs_send_time_id;
5184 int packet_len;
5185 unsigned char *rtpheader;
5186
5187 /* If the abs-send-time extension has been negotiated determine how much space we need */
5189 if (abs_send_time_id != -1) {
5190 /* 4 bytes for the shared information, 1 byte for identifier, 3 bytes for abs-send-time */
5191 hdrlen += 8;
5192 ext = 1;
5193 }
5194
5195 packet_len = frame->datalen + hdrlen;
5196 rtpheader = (unsigned char *)(frame->data.ptr - hdrlen);
5197
5198 put_unaligned_uint32(rtpheader, htonl((2 << 30) | (ext << 28) | (codec << 16) | (seqno) | (mark << 23)));
5199 put_unaligned_uint32(rtpheader + 4, htonl(rtp->lastts));
5200 put_unaligned_uint32(rtpheader + 8, htonl(rtp->ssrc));
5201
5202 /* We assume right now that we will only ever have the abs-send-time extension in the packet
5203 * which simplifies things a bit.
5204 */
5205 if (abs_send_time_id != -1) {
5206 unsigned int now_msw;
5207 unsigned int now_lsw;
5208
5209 /* This happens before being placed into the retransmission buffer so that when we
5210 * retransmit we only have to update the timestamp, not everything else.
5211 */
5212 put_unaligned_uint32(rtpheader + 12, htonl((0xBEDE << 16) | 1));
5213 rtpheader[16] = (abs_send_time_id << 4) | 2;
5214
5215 timeval2ntp(ast_tvnow(), &now_msw, &now_lsw);
5216 put_unaligned_time24(rtpheader + 17, now_msw, now_lsw);
5217 }
5218
5219 /* If retransmissions are enabled, we need to store this packet for future use */
5220 if (rtp->send_buffer) {
5221 struct ast_rtp_rtcp_nack_payload *payload;
5222
5223 payload = ast_malloc(sizeof(*payload) + packet_len);
5224 if (payload) {
5225 payload->size = packet_len;
5226 memcpy(payload->buf, rtpheader, packet_len);
5227 if (ast_data_buffer_put(rtp->send_buffer, rtp->seqno, payload) == -1) {
5228 ast_free(payload);
5229 }
5230 }
5231 }
5232
5233 res = rtp_sendto(instance, (void *)rtpheader, packet_len, 0, &remote_address, &ice);
5234 if (res < 0) {
5236 ast_debug_rtp(1, "(%p) RTP transmission error of packet %d to %s: %s\n",
5237 instance, rtp->seqno,
5238 ast_sockaddr_stringify(&remote_address),
5239 strerror(errno));
5241 /* Only give this error message once if we are not RTP debugging */
5243 ast_debug(0, "(%p) RTP NAT: Can't write RTP to private address %s, waiting for other end to send audio...\n",
5244 instance, ast_sockaddr_stringify(&remote_address));
5246 }
5247 } else {
5248 if (rtp->rtcp && rtp->rtcp->schedid < 0) {
5249 ast_debug_rtcp(2, "(%s) RTCP starting transmission in %u ms\n",
5251 ao2_ref(instance, +1);
5253 if (rtp->rtcp->schedid < 0) {
5254 ao2_ref(instance, -1);
5255 ast_log(LOG_WARNING, "scheduling RTCP transmission failed.\n");
5256 }
5257 }
5258 }
5259
5260 if (rtp_debug_test_addr(&remote_address)) {
5261 ast_verbose("Sent RTP packet to %s%s (type %-2.2d, seq %-6.6d, ts %-6.6u, len %-6.6d)\n",
5262 ast_sockaddr_stringify(&remote_address),
5263 ice ? " (via ICE)" : "",
5264 codec, rtp->seqno, rtp->lastts, res - hdrlen);
5265 }
5266 }
5267
5268 /* If the sequence number that has been used doesn't match what we expected then this is an out of
5269 * order late packet, so we don't need to increment as we haven't yet gotten the expected frame from
5270 * the core.
5271 */
5272 if (seqno == rtp->seqno) {
5273 rtp->seqno++;
5274 }
5275
5276 return 0;
5277}
#define abs(x)
Definition: f2c.h:195
struct ast_format * ast_format_g722
Built-in cached g722 format.
Definition: format_cache.c:106
#define MAX_TIMESTAMP_SKEW
unsigned int lastovidtimestamp
unsigned int lastotexttimestamp

References abs, ao2_ref, ast_clear_flag, ast_data_buffer_put(), ast_debug, ast_debug_rtcp, ast_debug_rtp, ast_debug_rtp_packet_is_allowed, ast_format_cmp(), AST_FORMAT_CMP_EQUAL, ast_format_g722, AST_FRAME_VIDEO, AST_FRAME_VOICE, ast_free, AST_FRFLAG_HAS_SEQUENCE_NUMBER, AST_FRFLAG_HAS_TIMING_INFO, ast_log, ast_malloc, ast_rtcp_calc_interval(), ast_rtcp_write(), AST_RTP_EXTENSION_ABS_SEND_TIME, ast_rtp_get_rate(), ast_rtp_instance_extmap_get_id(), ast_rtp_instance_get_channel_id(), ast_rtp_instance_get_data(), ast_rtp_instance_get_prop(), ast_rtp_instance_get_remote_address, AST_RTP_PROPERTY_NAT, ast_sched_add(), ast_set_flag, ast_sockaddr_isnull(), ast_sockaddr_stringify(), ast_test_flag, ast_tvnow(), ast_tvzero(), ast_verbose(), ast_rtp_rtcp_nack_payload::buf, calc_txstamp(), ast_frame::data, ast_frame::datalen, ast_frame::delivery, errno, ast_rtp::expectedseqno, ext, FLAG_NAT_ACTIVE, FLAG_NAT_INACTIVE, FLAG_NAT_INACTIVE_NOWARN, FLAG_NEED_MARKER_BIT, ast_frame_subclass::format, ast_frame_subclass::frame_ending, ast_frame::frametype, ast_rtp::lastdigitts, ast_rtp::lastotexttimestamp, ast_rtp::lastovidtimestamp, ast_rtp::lastts, LOG_WARNING, MAX_TIMESTAMP_SKEW, ast_frame::ptr, put_unaligned_time24(), put_unaligned_uint32(), ast_rtp::rtcp, rtp_debug_test_addr(), rtp_sendto(), ast_frame::samples, ast_rtp::sched, ast_rtcp::schedid, ast_rtp::send_buffer, ast_rtp::sending_digit, ast_frame::seqno, ast_rtp::seqno, ast_rtp_rtcp_nack_payload::size, ast_rtp::ssrc, ast_frame::subclass, timeval2ntp(), and ast_frame::ts.

Referenced by ast_rtp_write().

◆ rtp_recvfrom()

static int rtp_recvfrom ( struct ast_rtp_instance instance,
void *  buf,
size_t  size,
int  flags,
struct ast_sockaddr sa 
)
static
Precondition
instance is locked

Definition at line 3379 of file res_rtp_asterisk.c.

3380{
3381 return __rtp_recvfrom(instance, buf, size, flags, sa, 0);
3382}

References __rtp_recvfrom(), and buf.

Referenced by ast_rtp_read().

◆ rtp_red_buffer()

static int rtp_red_buffer ( struct ast_rtp_instance instance,
struct ast_frame frame 
)
static
Precondition
instance is locked

Definition at line 9087 of file res_rtp_asterisk.c.

9088{
9089 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
9090 struct rtp_red *red = rtp->red;
9091
9092 if (!red) {
9093 return 0;
9094 }
9095
9096 if (frame->datalen > 0) {
9097 if (red->t140.datalen > 0) {
9098 const unsigned char *primary = red->buf_data;
9099
9100 /* There is something already in the T.140 buffer */
9101 if (primary[0] == 0x08 || primary[0] == 0x0a || primary[0] == 0x0d) {
9102 /* Flush the previous T.140 packet if it is a command */
9103 ast_rtp_write(instance, &rtp->red->t140);
9104 } else {
9105 primary = frame->data.ptr;
9106 if (primary[0] == 0x08 || primary[0] == 0x0a || primary[0] == 0x0d) {
9107 /* Flush the previous T.140 packet if we are buffering a command now */
9108 ast_rtp_write(instance, &rtp->red->t140);
9109 }
9110 }
9111 }
9112
9113 memcpy(&red->buf_data[red->t140.datalen], frame->data.ptr, frame->datalen);
9114 red->t140.datalen += frame->datalen;
9115 red->t140.ts = frame->ts;
9116 }
9117
9118 return 0;
9119}
unsigned char buf_data[64000]

References ast_rtp_instance_get_data(), ast_rtp_write(), rtp_red::buf_data, ast_frame::data, ast_frame::datalen, ast_frame::ptr, ast_rtp::red, rtp_red::t140, and ast_frame::ts.

◆ rtp_red_init()

static int rtp_red_init ( struct ast_rtp_instance instance,
int  buffer_time,
int *  payloads,
int  generations 
)
static
Precondition
instance is locked

Definition at line 9054 of file res_rtp_asterisk.c.

9055{
9056 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
9057 int x;
9058
9059 rtp->red = ast_calloc(1, sizeof(*rtp->red));
9060 if (!rtp->red) {
9061 return -1;
9062 }
9063
9066 rtp->red->t140.data.ptr = &rtp->red->buf_data;
9067
9068 rtp->red->t140red = rtp->red->t140;
9069 rtp->red->t140red.data.ptr = &rtp->red->t140red_data;
9070
9071 rtp->red->ti = buffer_time;
9072 rtp->red->num_gen = generations;
9073 rtp->red->hdrlen = generations * 4 + 1;
9074
9075 for (x = 0; x < generations; x++) {
9076 rtp->red->pt[x] = payloads[x];
9077 rtp->red->pt[x] |= 1 << 7; /* mark redundant generations pt */
9078 rtp->red->t140red_data[x*4] = rtp->red->pt[x];
9079 }
9080 rtp->red->t140red_data[x*4] = rtp->red->pt[x] = payloads[x]; /* primary pt */
9081 rtp->red->schedid = ast_sched_add(rtp->sched, generations, red_write, instance);
9082
9083 return 0;
9084}
static int red_write(const void *data)
Write t140 redundancy frame.
unsigned char t140red_data[64000]
unsigned char pt[AST_RED_MAX_GENERATION]

References ast_calloc, ast_format_t140_red, AST_FRAME_TEXT, ast_rtp_instance_get_data(), ast_sched_add(), rtp_red::buf_data, ast_frame::data, ast_frame_subclass::format, ast_frame::frametype, rtp_red::hdrlen, rtp_red::num_gen, rtp_red::pt, ast_frame::ptr, ast_rtp::red, red_write(), ast_rtp::sched, rtp_red::schedid, ast_frame::subclass, rtp_red::t140, rtp_red::t140red, rtp_red::t140red_data, and rtp_red::ti.

◆ rtp_reload()

static int rtp_reload ( int  reload,
int  by_external_config 
)
static

This resource is not "reloaded" so much as unloaded and loaded again. In the case of the TURN related variables, the memory referenced by a previously loaded instance should have been released when the corresponding pool was destroyed. If at some point in the future this resource were to support ACTUAL live reconfiguration and did NOT release the pool this will cause a small memory leak.

Definition at line 9916 of file res_rtp_asterisk.c.

9917{
9918 struct ast_config *cfg;
9919 const char *s;
9920 struct ast_flags config_flags = { (reload && !by_external_config) ? CONFIG_FLAG_FILEUNCHANGED : 0 };
9921
9922#ifdef HAVE_PJPROJECT
9923 struct ast_variable *var;
9924 struct ast_ice_host_candidate *candidate;
9925 int acl_subscription_flag = 0;
9926#endif
9927
9928 cfg = ast_config_load2("rtp.conf", "rtp", config_flags);
9929 if (!cfg || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) {
9930 return 0;
9931 }
9932
9933#ifdef SO_NO_CHECK
9934 nochecksums = 0;
9935#endif
9936
9945
9946 /** This resource is not "reloaded" so much as unloaded and loaded again.
9947 * In the case of the TURN related variables, the memory referenced by a
9948 * previously loaded instance *should* have been released when the
9949 * corresponding pool was destroyed. If at some point in the future this
9950 * resource were to support ACTUAL live reconfiguration and did NOT release
9951 * the pool this will cause a small memory leak.
9952 */
9953
9954#ifdef HAVE_PJPROJECT
9955 icesupport = DEFAULT_ICESUPPORT;
9956 stun_software_attribute = DEFAULT_STUN_SOFTWARE_ATTRIBUTE;
9957 turnport = DEFAULT_TURN_PORT;
9958 clean_stunaddr();
9959 turnaddr = pj_str(NULL);
9960 turnusername = pj_str(NULL);
9961 turnpassword = pj_str(NULL);
9962 host_candidate_overrides_clear();
9963#endif
9964
9965#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
9966 dtls_mtu = DEFAULT_DTLS_MTU;
9967#endif
9968
9969 if ((s = ast_variable_retrieve(cfg, "general", "rtpstart"))) {
9970 rtpstart = atoi(s);
9975 }
9976 if ((s = ast_variable_retrieve(cfg, "general", "rtpend"))) {
9977 rtpend = atoi(s);
9982 }
9983 if ((s = ast_variable_retrieve(cfg, "general", "rtcpinterval"))) {
9984 rtcpinterval = atoi(s);
9985 if (rtcpinterval == 0)
9986 rtcpinterval = 0; /* Just so we're clear... it's zero */
9988 rtcpinterval = RTCP_MIN_INTERVALMS; /* This catches negative numbers too */
9991 }
9992 if ((s = ast_variable_retrieve(cfg, "general", "rtpchecksums"))) {
9993#ifdef SO_NO_CHECK
9994 nochecksums = ast_false(s) ? 1 : 0;
9995#else
9996 if (ast_false(s))
9997 ast_log(LOG_WARNING, "Disabling RTP checksums is not supported on this operating system!\n");
9998#endif
9999 }
10000 if ((s = ast_variable_retrieve(cfg, "general", "dtmftimeout"))) {
10001 dtmftimeout = atoi(s);
10002 if ((dtmftimeout < 0) || (dtmftimeout > 64000)) {
10003 ast_log(LOG_WARNING, "DTMF timeout of '%d' outside range, using default of '%d' instead\n",
10006 };
10007 }
10008 if ((s = ast_variable_retrieve(cfg, "general", "strictrtp"))) {
10009 if (ast_true(s)) {
10011 } else if (!strcasecmp(s, "seqno")) {
10013 } else {
10015 }
10016 }
10017 if ((s = ast_variable_retrieve(cfg, "general", "probation"))) {
10018 if ((sscanf(s, "%d", &learning_min_sequential) != 1) || learning_min_sequential <= 1) {
10019 ast_log(LOG_WARNING, "Value for 'probation' could not be read, using default of '%d' instead\n",
10022 }
10024 }
10025 if ((s = ast_variable_retrieve(cfg, "general", "srtpreplayprotection"))) {
10027 }
10028#ifdef HAVE_PJPROJECT
10029 if ((s = ast_variable_retrieve(cfg, "general", "icesupport"))) {
10030 icesupport = ast_true(s);
10031 }
10032 if ((s = ast_variable_retrieve(cfg, "general", "stun_software_attribute"))) {
10033 stun_software_attribute = ast_true(s);
10034 }
10035 if ((s = ast_variable_retrieve(cfg, "general", "stunaddr"))) {
10036 char *hostport, *host, *port;
10037 unsigned int port_parsed = STANDARD_STUN_PORT;
10038 struct ast_sockaddr stunaddr_parsed;
10039
10040 hostport = ast_strdupa(s);
10041
10042 if (!ast_parse_arg(hostport, PARSE_ADDR, &stunaddr_parsed)) {
10043 ast_debug_stun(3, "stunaddr = '%s' does not need name resolution\n",
10044 ast_sockaddr_stringify_host(&stunaddr_parsed));
10045 if (!ast_sockaddr_port(&stunaddr_parsed)) {
10046 ast_sockaddr_set_port(&stunaddr_parsed, STANDARD_STUN_PORT);
10047 }
10048 ast_rwlock_wrlock(&stunaddr_lock);
10049 ast_sockaddr_to_sin(&stunaddr_parsed, &stunaddr);
10050 ast_rwlock_unlock(&stunaddr_lock);
10051 } else if (ast_sockaddr_split_hostport(hostport, &host, &port, 0)) {
10052 if (port) {
10053 ast_parse_arg(port, PARSE_UINT32|PARSE_IN_RANGE, &port_parsed, 1, 65535);
10054 }
10055 stunaddr.sin_port = htons(port_parsed);
10056
10057 stunaddr_resolver = ast_dns_resolve_recurring(host, T_A, C_IN,
10058 &stunaddr_resolve_callback, NULL);
10059 if (!stunaddr_resolver) {
10060 ast_log(LOG_ERROR, "Failed to setup recurring DNS resolution of stunaddr '%s'",
10061 host);
10062 }
10063 } else {
10064 ast_log(LOG_ERROR, "Failed to parse stunaddr '%s'", hostport);
10065 }
10066 }
10067 if ((s = ast_variable_retrieve(cfg, "general", "turnaddr"))) {
10068 struct sockaddr_in addr;
10069 addr.sin_port = htons(DEFAULT_TURN_PORT);
10070 if (ast_parse_arg(s, PARSE_INADDR, &addr)) {
10071 ast_log(LOG_WARNING, "Invalid TURN server address: %s\n", s);
10072 } else {
10073 pj_strdup2_with_null(pool, &turnaddr, ast_inet_ntoa(addr.sin_addr));
10074 /* ntohs() is not a bug here. The port number is used in host byte order with
10075 * a pjnat API. */
10076 turnport = ntohs(addr.sin_port);
10077 }
10078 }
10079 if ((s = ast_variable_retrieve(cfg, "general", "turnusername"))) {
10080 pj_strdup2_with_null(pool, &turnusername, s);
10081 }
10082 if ((s = ast_variable_retrieve(cfg, "general", "turnpassword"))) {
10083 pj_strdup2_with_null(pool, &turnpassword, s);
10084 }
10085
10086 AST_RWLIST_WRLOCK(&host_candidates);
10087 for (var = ast_variable_browse(cfg, "ice_host_candidates"); var; var = var->next) {
10088 struct ast_sockaddr local_addr, advertised_addr;
10089 unsigned int include_local_address = 0;
10090 char *sep;
10091
10092 ast_sockaddr_setnull(&local_addr);
10093 ast_sockaddr_setnull(&advertised_addr);
10094
10095 if (ast_parse_arg(var->name, PARSE_ADDR | PARSE_PORT_IGNORE, &local_addr)) {
10096 ast_log(LOG_WARNING, "Invalid local ICE host address: %s\n", var->name);
10097 continue;
10098 }
10099
10100 sep = strchr(var->value,',');
10101 if (sep) {
10102 *sep = '\0';
10103 sep++;
10104 sep = ast_skip_blanks(sep);
10105 include_local_address = strcmp(sep, "include_local_address") == 0;
10106 }
10107
10108 if (ast_parse_arg(var->value, PARSE_ADDR | PARSE_PORT_IGNORE, &advertised_addr)) {
10109 ast_log(LOG_WARNING, "Invalid advertised ICE host address: %s\n", var->value);
10110 continue;
10111 }
10112
10113 if (!(candidate = ast_calloc(1, sizeof(*candidate)))) {
10114 ast_log(LOG_ERROR, "Failed to allocate ICE host candidate mapping.\n");
10115 break;
10116 }
10117
10118 candidate->include_local = include_local_address;
10119
10120 ast_sockaddr_copy(&candidate->local, &local_addr);
10121 ast_sockaddr_copy(&candidate->advertised, &advertised_addr);
10122
10123 AST_RWLIST_INSERT_TAIL(&host_candidates, candidate, next);
10124 }
10125 AST_RWLIST_UNLOCK(&host_candidates);
10126
10127 ast_rwlock_wrlock(&ice_acl_lock);
10128 ast_rwlock_wrlock(&stun_acl_lock);
10129
10130 ice_acl = ast_free_acl_list(ice_acl);
10131 stun_acl = ast_free_acl_list(stun_acl);
10132
10133 for (var = ast_variable_browse(cfg, "general"); var; var = var->next) {
10134 const char* sense = NULL;
10135 struct ast_acl_list **acl = NULL;
10136 if (strncasecmp(var->name, "ice_", 4) == 0) {
10137 sense = var->name + 4;
10138 acl = &ice_acl;
10139 } else if (strncasecmp(var->name, "stun_", 5) == 0) {
10140 sense = var->name + 5;
10141 acl = &stun_acl;
10142 } else {
10143 continue;
10144 }
10145
10146 if (strcasecmp(sense, "blacklist") == 0) {
10147 sense = "deny";
10148 }
10149
10150 if (strcasecmp(sense, "acl") && strcasecmp(sense, "permit") && strcasecmp(sense, "deny")) {
10151 continue;
10152 }
10153
10154 ast_append_acl(sense, var->value, acl, NULL, &acl_subscription_flag);
10155 }
10156 ast_rwlock_unlock(&ice_acl_lock);
10157 ast_rwlock_unlock(&stun_acl_lock);
10158
10159 if (acl_subscription_flag && !acl_change_sub) {
10163 } else if (!acl_subscription_flag && acl_change_sub) {
10165 }
10166#endif
10167#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP)
10168 if ((s = ast_variable_retrieve(cfg, "general", "dtls_mtu"))) {
10169 if ((sscanf(s, "%d", &dtls_mtu) != 1) || dtls_mtu < 256) {
10170 ast_log(LOG_WARNING, "Value for 'dtls_mtu' could not be read, using default of '%d' instead\n",
10172 dtls_mtu = DEFAULT_DTLS_MTU;
10173 }
10174 }
10175#endif
10176
10177 ast_config_destroy(cfg);
10178
10179 /* Choosing an odd start port casues issues (like a potential infinite loop) and as odd parts are not
10180 chosen anyway, we are going to round up and issue a warning */
10181 if (rtpstart & 1) {
10182 rtpstart++;
10183 ast_log(LOG_WARNING, "Odd start value for RTP port in rtp.conf, rounding up to %d\n", rtpstart);
10184 }
10185
10186 if (rtpstart >= rtpend) {
10187 ast_log(LOG_WARNING, "Unreasonable values for RTP start/end port in rtp.conf\n");
10190 }
10191 ast_verb(2, "RTP Allocating from port range %d -> %d\n", rtpstart, rtpend);
10192 return 0;
10193}
struct stasis_message_type * ast_named_acl_change_type(void)
a stasis_message_type for changes against a named ACL or the set of all named ACLs
void ast_append_acl(const char *sense, const char *stuff, struct ast_acl_list **path, int *error, int *named_acl_flag)
Add a rule to an ACL struct.
Definition: acl.c:429
struct ast_acl_list * ast_free_acl_list(struct ast_acl_list *acl)
Free a list of ACLs.
Definition: acl.c:233
#define var
Definition: ast_expr2f.c:605
static struct stasis_subscription * acl_change_sub
Definition: chan_iax2.c:328
static void acl_change_stasis_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
Definition: chan_iax2.c:1558
struct ast_dns_query_recurring * ast_dns_resolve_recurring(const char *name, int rr_type, int rr_class, ast_dns_resolve_callback callback, void *data)
Asynchronously resolve a DNS query, and continue resolving it according to the lowest TTL available.
struct ast_config * ast_config_load2(const char *filename, const char *who_asked, struct ast_flags flags)
Load a config file.
Definition: main/config.c:3321
#define CONFIG_STATUS_FILEUNCHANGED
#define CONFIG_STATUS_FILEINVALID
int ast_parse_arg(const char *arg, enum ast_parse_flags flags, void *p_result,...)
The argument parsing routine.
Definition: main/config.c:3827
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
Definition: extconf.c:1289
const char * ast_variable_retrieve(struct ast_config *config, const char *category, const char *variable)
Definition: main/config.c:783
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
Definition: extconf.c:1215
@ CONFIG_FLAG_FILEUNCHANGED
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
#define AST_RWLIST_INSERT_TAIL
Definition: linkedlists.h:741
#define ast_rwlock_wrlock(a)
Definition: lock.h:236
static char * ast_sockaddr_stringify_host(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() to return an address only, suitable for a URL (with brack...
Definition: netsock2.h:327
static void ast_sockaddr_setnull(struct ast_sockaddr *addr)
Sets address addr to null.
Definition: netsock2.h:138
static int reload(void)
#define DEFAULT_ICESUPPORT
#define DEFAULT_LEARNING_MIN_SEQUENTIAL
#define DEFAULT_RTP_END
#define RTCP_DEFAULT_INTERVALMS
#define DEFAULT_DTMF_TIMEOUT
#define RTCP_MAX_INTERVALMS
#define DEFAULT_SRTP_REPLAY_PROTECTION
#define DEFAULT_DTLS_MTU
#define DEFAULT_RTP_START
#define MINIMUM_RTP_PORT
#define RTCP_MIN_INTERVALMS
#define CALC_LEARNING_MIN_DURATION(count)
Calculate the min learning duration in ms.
#define MAXIMUM_RTP_PORT
#define DEFAULT_STRICT_RTP
#define DEFAULT_TURN_PORT
#define DEFAULT_STUN_SOFTWARE_ATTRIBUTE
#define DEFAULT_LEARNING_MIN_DURATION
struct stasis_topic * ast_security_topic(void)
A stasis_topic which publishes messages for security related issues.
@ STASIS_SUBSCRIPTION_FILTER_SELECTIVE
Definition: stasis.h:297
int stasis_subscription_accept_message_type(struct stasis_subscription *subscription, const struct stasis_message_type *type)
Indicate to a subscription that we are interested in a message type.
Definition: stasis.c:1023
int stasis_subscription_set_filter(struct stasis_subscription *subscription, enum stasis_subscription_message_filter filter)
Set the message type filtering level on a subscription.
Definition: stasis.c:1077
struct stasis_subscription * stasis_unsubscribe_and_join(struct stasis_subscription *subscription)
Cancel a subscription, blocking until the last message is processed.
Definition: stasis.c:1134
#define stasis_subscribe(topic, callback, data)
Definition: stasis.h:649
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true"....
Definition: utils.c:2199
int attribute_pure ast_false(const char *val)
Make sure something is false. Determine if a string containing a boolean value is "false"....
Definition: utils.c:2216
char * ast_skip_blanks(const char *str)
Gets a pointer to the first non-whitespace character in a string.
Definition: strings.h:161
Definition: test_acl.c:111
Wrapper for an ast_acl linked list.
Definition: acl.h:76
Structure used to handle boolean flags.
Definition: utils.h:199
Structure for variables, used for configurations and for channel variables.
static const int STANDARD_STUN_PORT
Definition: stun.h:61

References acl_change_stasis_cb(), acl_change_sub, ast_append_acl(), ast_calloc, ast_config_destroy(), ast_config_load2(), ast_debug_stun, ast_dns_resolve_recurring(), ast_false(), ast_free_acl_list(), ast_inet_ntoa(), ast_log, ast_named_acl_change_type(), ast_parse_arg(), AST_RWLIST_INSERT_TAIL, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_rwlock_unlock, ast_rwlock_wrlock, ast_security_topic(), ast_skip_blanks(), ast_sockaddr_copy(), ast_sockaddr_port, ast_sockaddr_set_port, ast_sockaddr_setnull(), ast_sockaddr_split_hostport(), ast_sockaddr_stringify_host(), ast_sockaddr_to_sin, ast_strdupa, ast_true(), ast_variable_browse(), ast_variable_retrieve(), ast_verb, CALC_LEARNING_MIN_DURATION, CONFIG_FLAG_FILEUNCHANGED, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEUNCHANGED, DEFAULT_DTLS_MTU, DEFAULT_DTMF_TIMEOUT, DEFAULT_ICESUPPORT, DEFAULT_LEARNING_MIN_DURATION, DEFAULT_LEARNING_MIN_SEQUENTIAL, DEFAULT_RTP_END, DEFAULT_RTP_START, DEFAULT_SRTP_REPLAY_PROTECTION, DEFAULT_STRICT_RTP, DEFAULT_STUN_SOFTWARE_ATTRIBUTE, DEFAULT_TURN_PORT, dtmftimeout, learning_min_duration, learning_min_sequential, LOG_ERROR, LOG_WARNING, MAXIMUM_RTP_PORT, MINIMUM_RTP_PORT, NULL, PARSE_ADDR, PARSE_IN_RANGE, PARSE_INADDR, PARSE_PORT_IGNORE, PARSE_UINT32, reload(), RTCP_DEFAULT_INTERVALMS, RTCP_MAX_INTERVALMS, RTCP_MIN_INTERVALMS, rtcpinterval, rtpend, rtpstart, srtp_replay_protection, STANDARD_STUN_PORT, stasis_subscribe, stasis_subscription_accept_message_type(), STASIS_SUBSCRIPTION_FILTER_SELECTIVE, stasis_subscription_set_filter(), stasis_unsubscribe_and_join(), STRICT_RTP_NO, STRICT_RTP_SEQNO, STRICT_RTP_YES, strictrtp, and var.

Referenced by load_module(), and reload_module().

◆ rtp_sendto()

static int rtp_sendto ( struct ast_rtp_instance instance,
void *  buf,
size_t  size,
int  flags,
struct ast_sockaddr sa,
int *  ice 
)
static
Precondition
instance is locked

Definition at line 3447 of file res_rtp_asterisk.c.

3448{
3449 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
3450 int hdrlen = 12;
3451 int res;
3452
3453 if ((res = __rtp_sendto(instance, buf, size, flags, sa, 0, ice, 1)) > 0) {
3454 rtp->txcount++;
3455 rtp->txoctetcount += (res - hdrlen);
3456 }
3457
3458 return res;
3459}

References __rtp_sendto(), ast_rtp_instance_get_data(), buf, ast_rtp::flags, ast_rtp::txcount, and ast_rtp::txoctetcount.

Referenced by ast_rtp_dtmf_begin(), ast_rtp_dtmf_continuation(), ast_rtp_dtmf_end_with_duration(), ast_rtp_rtcp_handle_nack(), ast_rtp_sendcng(), bridge_p2p_rtp_write(), and rtp_raw_write().

◆ rtp_transport_wide_cc_feedback_produce()

static int rtp_transport_wide_cc_feedback_produce ( const void *  data)
static

Definition at line 7373 of file res_rtp_asterisk.c.

7374{
7375 struct ast_rtp_instance *instance = (struct ast_rtp_instance *) data;
7376 struct ast_rtp *rtp = ast_rtp_instance_get_data(instance);
7377 unsigned char *rtcpheader;
7378 char bdata[1024];
7379 struct rtp_transport_wide_cc_packet_statistics *first_packet;
7380 struct rtp_transport_wide_cc_packet_statistics *previous_packet;
7381 int i;
7382 int status_vector_chunk_bits = 14;
7383 uint16_t status_vector_chunk = (1 << 15) | (1 << 14);
7384 int run_length_chunk_count = 0;
7385 int run_length_chunk_status = -1;
7386 int packet_len = 20;
7387 int delta_len = 0;
7388 int packet_count = 0;
7389 unsigned int received_msw;
7390 unsigned int received_lsw;
7391 struct ast_sockaddr remote_address = { { 0, } };
7392 int res;
7393 int ice;
7394 unsigned int large_delta_count = 0;
7395 unsigned int small_delta_count = 0;
7396 unsigned int lost_count = 0;
7397
7398 if (!rtp || !rtp->rtcp || rtp->transport_wide_cc.schedid == -1) {
7399 ao2_ref(instance, -1);
7400 return 0;
7401 }
7402
7403 ao2_lock(instance);
7404
7405 /* If no packets have been received then do nothing */
7407 ao2_unlock(instance);
7408 return 1000;
7409 }
7410
7411 rtcpheader = (unsigned char *)bdata;
7412
7413 /* The first packet in the vector acts as our base sequence number and reference time */
7415 previous_packet = first_packet;
7416
7417 /* We go through each packet that we have statistics for, adding it either to a status
7418 * vector chunk or a run length chunk. The code tries to be as efficient as possible to
7419 * reduce packet size and will favor run length chunks when it makes sense.
7420 */
7421 for (i = 0; i < AST_VECTOR_SIZE(&rtp->transport_wide_cc.packet_statistics); ++i) {
7423 int lost = 0;
7424 int res = 0;
7425
7427
7428 packet_count++;
7429
7430 if (first_packet != statistics) {
7431 /* The vector stores statistics in a sorted fashion based on the sequence
7432 * number. This ensures we can detect any packets that have been lost/not
7433 * received by comparing the sequence numbers.
7434 */
7435 lost = statistics->seqno - (previous_packet->seqno + 1);
7436 lost_count += lost;
7437 }
7438
7439 while (lost) {
7440 /* We append a not received status until all the lost packets have been accounted for */
7441 rtp_transport_wide_cc_feedback_status_append(rtcpheader, &packet_len, &status_vector_chunk_bits,
7442 &status_vector_chunk, &run_length_chunk_count, &run_length_chunk_status, 0);
7443 packet_count++;
7444
7445 /* If there is no more room left for storing packets stop now, we leave 20
7446 * extra bits at the end just in case.
7447 */
7448 if (packet_len + delta_len + 20 > sizeof(bdata)) {
7449 res = -1;
7450 break;
7451 }
7452
7453 lost--;
7454 }
7455
7456 /* If the lost packet appending bailed out because we have no more space, then exit here too */
7457 if (res) {
7458 break;
7459 }
7460
7461 /* Per the spec the delta is in increments of 250 */
7462 statistics->delta = ast_tvdiff_us(statistics->received, previous_packet->received) / 250;
7463
7464 /* Based on the delta determine the status of this packet */
7465 if (statistics->delta < 0 || statistics->delta > 127) {
7466 /* Large or negative delta */
7467 rtp_transport_wide_cc_feedback_status_append(rtcpheader, &packet_len, &status_vector_chunk_bits,
7468 &status_vector_chunk, &run_length_chunk_count, &run_length_chunk_status, 2);
7469 delta_len += 2;
7470 large_delta_count++;
7471 } else {
7472 /* Small delta */
7473 rtp_transport_wide_cc_feedback_status_append(rtcpheader, &packet_len, &status_vector_chunk_bits,
7474 &status_vector_chunk, &run_length_chunk_count, &run_length_chunk_status, 1);
7475 delta_len += 1;
7476 small_delta_count++;
7477 }
7478
7479 previous_packet = statistics;
7480
7481 /* If there is no more room left in the packet stop handling of any subsequent packets */
7482 if (packet_len + delta_len + 20 > sizeof(bdata)) {
7483 break;
7484 }
7485 }
7486
7487 if (status_vector_chunk_bits != 14) {
7488 /* If the status vector chunk has packets in it then place it in the RTCP packet */
7489 put_unaligned_uint16(rtcpheader + packet_len, htons(status_vector_chunk));
7490 packet_len += 2;
7491 } else if (run_length_chunk_count) {
7492 /* If there is a run length chunk in progress then place it in the RTCP packet */
7493 put_unaligned_uint16(rtcpheader + packet_len, htons((0 << 15) | (run_length_chunk_status << 13) | run_length_chunk_count));
7494 packet_len += 2;
7495 }
7496
7497 /* We iterate again to build delta chunks */
7498 for (i = 0; i < AST_VECTOR_SIZE(&rtp->transport_wide_cc.packet_statistics); ++i) {
7500
7502
7503 if (statistics->delta < 0 || statistics->delta > 127) {
7504 /* We need 2 bytes to store this delta */
7505 put_unaligned_uint16(rtcpheader + packet_len, htons(statistics->delta));
7506 packet_len += 2;
7507 } else {
7508 /* We can store this delta in 1 byte */
7509 rtcpheader[packet_len] = statistics->delta;
7510 packet_len += 1;
7511 }
7512
7513 /* If this is the last packet handled by the run length chunk or status vector chunk code
7514 * then we can go no further.
7515 */
7516 if (statistics == previous_packet) {
7517 break;
7518 }
7519 }
7520
7521 /* Zero pad the end of the packet */
7522 while (packet_len % 4) {
7523 rtcpheader[packet_len++] = 0;
7524 }
7525
7526 /* Add the general RTCP header information */
7527 put_unaligned_uint32(rtcpheader, htonl((2 << 30) | (AST_RTP_RTCP_FMT_TRANSPORT_WIDE_CC << 24)
7528 | (AST_RTP_RTCP_RTPFB << 16) | ((packet_len / 4) - 1)));
7529 put_unaligned_uint32(rtcpheader + 4, htonl(rtp->ssrc));
7530 put_unaligned_uint32(rtcpheader + 8, htonl(rtp->themssrc));
7531
7532 /* Add the transport-cc specific header information */
7533 put_unaligned_uint32(rtcpheader + 12, htonl((first_packet->seqno << 16) | packet_count));
7534
7535 timeval2ntp(first_packet->received, &received_msw, &received_lsw);
7536 put_unaligned_time24(rtcpheader + 16, received_msw, received_lsw);
7537 rtcpheader[19] = rtp->transport_wide_cc.feedback_count;
7538
7539 /* The packet is now fully constructed so send it out */
7540 ast_sockaddr_copy(&remote_address, &rtp->rtcp->them);
7541
7542 ast_debug_rtcp(2, "(%p) RTCP sending transport-cc feedback packet of size '%d' on '%s' with packet count of %d (small = %d, large = %d, lost = %d)\n",
7543 instance, packet_len, ast_rtp_instance_get_channel_id(instance), packet_count, small_delta_count, large_delta_count, lost_count);
7544
7545 res = rtcp_sendto(instance, (unsigned int *)rtcpheader, packet_len, 0, &remote_address, &ice);
7546 if (res < 0) {
7547 ast_log(LOG_ERROR, "RTCP transport-cc feedback error to %s due to %s\n",
7548 ast_sockaddr_stringify(&remote_address), strerror(errno));
7549 }
7550
7552
7554
7555 ao2_unlock(instance);
7556
7557 return 1000;
7558}
static void rtp_transport_wide_cc_feedback_status_append(unsigned char *rtcpheader, int *packet_len, int *status_vector_chunk_bits, uint16_t *status_vector_chunk, int *run_length_chunk_count, int *run_length_chunk_status, int status)
#define AST_RTP_RTCP_FMT_TRANSPORT_WIDE_CC
Definition: rtp_engine.h:336
int64_t ast_tvdiff_us(struct timeval end, struct timeval start)
Computes the difference (in microseconds) between two struct timeval instances.
Definition: time.h:87
static void put_unaligned_uint16(void *p, unsigned short datum)
Definition: unaligned.h:65

References ao2_lock, ao2_ref, ao2_unlock, ast_debug_rtcp, ast_log, ast_rtp_instance_get_channel_id(), ast_rtp_instance_get_data(), AST_RTP_RTCP_FMT_TRANSPORT_WIDE_CC, AST_RTP_RTCP_RTPFB, ast_sockaddr_copy(), ast_sockaddr_stringify(), ast_tvdiff_us(), AST_VECTOR_ELEM_CLEANUP_NOOP, AST_VECTOR_GET_ADDR, AST_VECTOR_RESET, AST_VECTOR_SIZE, ast_rtp_instance::data, errno, rtp_transport_wide_cc_statistics::feedback_count, LOG_ERROR, rtp_transport_wide_cc_statistics::packet_statistics, put_unaligned_time24(), put_unaligned_uint16(), put_unaligned_uint32(), rtp_transport_wide_cc_packet_statistics::received, ast_rtp::rtcp, rtcp_sendto(), rtp_transport_wide_cc_feedback_status_append(), rtp_transport_wide_cc_statistics::schedid, rtp_transport_wide_cc_packet_statistics::seqno, ast_rtp::ssrc, statistics(), ast_rtcp::them, ast_rtp::themssrc, timeval2ntp(), and ast_rtp::transport_wide_cc.

Referenced by rtp_instance_parse_transport_wide_cc().

◆ rtp_transport_wide_cc_feedback_status_append()

static void rtp_transport_wide_cc_feedback_status_append ( unsigned char *  rtcpheader,
int *  packet_len,
int *  status_vector_chunk_bits,
uint16_t *  status_vector_chunk,
int *  run_length_chunk_count,
int *  run_length_chunk_status,
int  status 
)
static

Definition at line 7332 of file res_rtp_asterisk.c.

7334{
7335 if (*run_length_chunk_status != status) {
7336 while (*run_length_chunk_count > 0 && *run_length_chunk_count < 8) {
7337 /* Realistically it only makes sense to use a run length chunk if there were 8 or more
7338 * consecutive packets of the same type, otherwise we could end up making the packet larger
7339 * if we have lots of small blocks of the same type. To help with this we backfill the status
7340 * vector (since it always represents 7 packets). Best case we end up with only that single
7341 * status vector and the rest are run length chunks.
7342 */
7343 rtp_transport_wide_cc_feedback_status_vector_append(rtcpheader, packet_len, status_vector_chunk_bits,
7344 status_vector_chunk, *run_length_chunk_status);
7345 *run_length_chunk_count -= 1;
7346 }
7347
7348 if (*run_length_chunk_count) {
7349 /* There is a run length chunk which needs to be written out */
7350 put_unaligned_uint16(rtcpheader + *packet_len, htons((0 << 15) | (*run_length_chunk_status << 13) | *run_length_chunk_count));
7351 *packet_len += 2;
7352 }
7353
7354 /* In all cases the run length chunk has to be reset */
7355 *run_length_chunk_count = 0;
7356 *run_length_chunk_status = -1;
7357
7358 if (*status_vector_chunk_bits == 14) {
7359 /* We aren't in the middle of a status vector so we can try for a run length chunk */
7360 *run_length_chunk_status = status;
7361 *run_length_chunk_count = 1;
7362 } else {
7363 /* We're doing a status vector so populate it accordingly */
7364 rtp_transport_wide_cc_feedback_status_vector_append(rtcpheader, packet_len, status_vector_chunk_bits,
7365 status_vector_chunk, status);
7366 }
7367 } else {
7368 /* This is easy, the run length chunk count can just get bumped up */
7369 *run_length_chunk_count += 1;
7370 }
7371}
static void rtp_transport_wide_cc_feedback_status_vector_append(unsigned char *rtcpheader, int *packet_len, int *status_vector_chunk_bits, uint16_t *status_vector_chunk, int status)

References put_unaligned_uint16(), rtp_transport_wide_cc_feedback_status_vector_append(), and status.

Referenced by rtp_transport_wide_cc_feedback_produce().

◆ rtp_transport_wide_cc_feedback_status_vector_append()

static void rtp_transport_wide_cc_feedback_status_vector_append ( unsigned char *  rtcpheader,
int *  packet_len,
int *  status_vector_chunk_bits,
uint16_t *  status_vector_chunk,
int  status 
)
static

Definition at line 7303 of file res_rtp_asterisk.c.

7305{
7306 /* Appending this status will use up 2 bits */
7307 *status_vector_chunk_bits -= 2;
7308
7309 /* We calculate which bits we want to update the status of. Since a status vector
7310 * is 16 bits we take away 2 (for the header), and then we take away any that have
7311 * already been used.
7312 */
7313 *status_vector_chunk |= (status << (16 - 2 - (14 - *status_vector_chunk_bits)));
7314
7315 /* If there are still bits available we can return early */
7316 if (*status_vector_chunk_bits) {
7317 return;
7318 }
7319
7320 /* Otherwise we have to place this chunk into the packet */
7321 put_unaligned_uint16(rtcpheader + *packet_len, htons(*status_vector_chunk));
7322 *status_vector_chunk_bits = 14;
7323
7324 /* The first bit being 1 indicates that this is a status vector chunk and the second
7325 * bit being 1 indicates that we are using 2 bits to represent each status for a
7326 * packet.
7327 */
7328 *status_vector_chunk = (1 << 15) | (1 << 14);
7329 *packet_len += 2;
7330}

References put_unaligned_uint16(), and status.

Referenced by rtp_transport_wide_cc_feedback_status_append().

◆ rtp_transport_wide_cc_packet_statistics_cmp()

static int rtp_transport_wide_cc_packet_statistics_cmp ( struct rtp_transport_wide_cc_packet_statistics  a,
struct rtp_transport_wide_cc_packet_statistics  b 
)
static

Definition at line 7297 of file res_rtp_asterisk.c.

7299{
7300 return a.seqno - b.seqno;
7301}
static struct test_val b

References a, and b.

Referenced by rtp_instance_parse_transport_wide_cc().

◆ rtp_write_rtcp_fir()

static void rtp_write_rtcp_fir ( struct ast_rtp_instance instance,
struct ast_rtp rtp,
struct ast_sockaddr remote_address 
)
static

Definition at line 5319 of file res_rtp_asterisk.c.

5320{
5321 unsigned char *rtcpheader;
5322 unsigned char bdata[1024];
5323 int packet_len = 0;
5324 int fir_len = 20;
5325 int ice;
5326 int res;
5327 int sr;
5328 RAII_VAR(struct ast_rtp_rtcp_report *, rtcp_report, NULL, ao2_cleanup);
5329
5330 if (!rtp || !rtp->rtcp) {
5331 return;
5332 }
5333
5334 if (ast_sockaddr_isnull(&rtp->rtcp->them) || rtp->rtcp->schedid < 0) {
5335 /*
5336 * RTCP was stopped.
5337 */
5338 return;
5339 }
5340
5341 if (!rtp->themssrc_valid) {
5342 /* We don't know their SSRC value so we don't know who to update. */
5343 return;
5344 }
5345
5346 /* Prepare RTCP FIR (PT=206, FMT=4) */
5347 rtp->rtcp->firseq++;
5348 if(rtp->rtcp->firseq == 256) {
5349 rtp->rtcp->firseq = 0;
5350 }
5351
5352 rtcpheader = bdata;
5353
5354 ao2_lock(instance);
5355 rtcp_report = ast_rtp_rtcp_report_alloc(rtp->themssrc_valid ? 1 : 0);
5356 res = ast_rtcp_generate_compound_prefix(instance, rtcpheader, rtcp_report, &sr);
5357
5358 if (res == 0 || res == 1) {
5359 ao2_unlock(instance);
5360 return;
5361 }
5362
5363 packet_len += res;
5364
5365 put_unaligned_uint32(rtcpheader + packet_len + 0, htonl((2 << 30) | (4 << 24) | (RTCP_PT_PSFB << 16) | ((fir_len/4)-1)));
5366 put_unaligned_uint32(rtcpheader + packet_len + 4, htonl(rtp->ssrc));
5367 put_unaligned_uint32(rtcpheader + packet_len + 8, htonl(rtp->themssrc));
5368 put_unaligned_uint32(rtcpheader + packet_len + 12, htonl(rtp->themssrc)); /* FCI: SSRC */
5369 put_unaligned_uint32(rtcpheader + packet_len + 16, htonl(rtp->rtcp->firseq << 24)); /* FCI: Sequence number */
5370 res = rtcp_sendto(instance, (unsigned int *)rtcpheader, packet_len + fir_len, 0, rtp->bundled ? remote_address : &rtp->rtcp->them, &ice);
5371 if (res < 0) {
5372 ast_log(LOG_ERROR, "RTCP FIR transmission error: %s\n", strerror(errno));
5373 } else {
5374 ast_rtcp_calculate_sr_rr_statistics(instance, rtcp_report, rtp->bundled ? *remote_address : rtp->rtcp->them, ice, sr);
5375 }
5376
5377 ao2_unlock(instance);
5378}

References ao2_cleanup, ao2_lock, ao2_unlock, ast_log, ast_rtcp_calculate_sr_rr_statistics(), ast_rtcp_generate_compound_prefix(), ast_rtp_rtcp_report_alloc(), ast_sockaddr_isnull(), ast_rtp::bundled, errno, ast_rtcp::firseq, LOG_ERROR, NULL, put_unaligned_uint32(), RAII_VAR, ast_rtp::rtcp, RTCP_PT_PSFB, rtcp_sendto(), ast_rtcp::schedid, ast_rtp::ssrc, ast_rtcp::them, ast_rtp::themssrc, and ast_rtp::themssrc_valid.

Referenced by ast_rtp_read(), and ast_rtp_write().

◆ rtp_write_rtcp_psfb()

static void rtp_write_rtcp_psfb ( struct ast_rtp_instance instance,
struct ast_rtp rtp,
struct ast_frame frame,
struct ast_sockaddr remote_address 
)
static

Definition at line 5380 of file res_rtp_asterisk.c.

5381{
5382 struct ast_rtp_rtcp_feedback *feedback = frame->data.ptr;
5383 unsigned char *rtcpheader;
5384 unsigned char bdata[1024];
5385 int remb_len = 24;
5386 int ice;
5387 int res;
5388 int sr = 0;
5389 int packet_len = 0;
5390 RAII_VAR(struct ast_rtp_rtcp_report *, rtcp_report, NULL, ao2_cleanup);
5391
5392 if (feedback->fmt != AST_RTP_RTCP_FMT_REMB) {
5393 ast_debug_rtcp(1, "(%p) RTCP provided feedback frame of format %d to write, but only REMB is supported\n",
5394 instance, feedback->fmt);
5395 return;
5396 }
5397
5398 if (!rtp || !rtp->rtcp) {
5399 return;
5400 }
5401
5402 /* If REMB support is not enabled don't send this RTCP packet */
5404 ast_debug_rtcp(1, "(%p) RTCP provided feedback REMB report to write, but REMB support not enabled\n",
5405 instance);
5406 return;
5407 }
5408
5409 if (ast_sockaddr_isnull(&rtp->rtcp->them) || rtp->rtcp->schedid < 0) {
5410 /*
5411 * RTCP was stopped.
5412 */
5413 return;
5414 }
5415
5416 rtcpheader = bdata;
5417
5418 ao2_lock(instance);
5419 rtcp_report = ast_rtp_rtcp_report_alloc(rtp->themssrc_valid ? 1 : 0);
5420 res = ast_rtcp_generate_compound_prefix(instance, rtcpheader, rtcp_report, &sr);
5421
5422 if (res == 0 || res == 1) {
5423 ao2_unlock(instance);
5424 return;
5425 }
5426
5427 packet_len += res;
5428
5429 put_unaligned_uint32(rtcpheader + packet_len + 0, htonl((2 << 30) | (AST_RTP_RTCP_FMT_REMB << 24) | (RTCP_PT_PSFB << 16) | ((remb_len/4)-1)));
5430 put_unaligned_uint32(rtcpheader + packet_len + 4, htonl(rtp->ssrc));
5431 put_unaligned_uint32(rtcpheader + packet_len + 8, htonl(0)); /* Per the draft, this should always be 0 */
5432 put_unaligned_uint32(rtcpheader + packet_len + 12, htonl(('R' << 24) | ('E' << 16) | ('M' << 8) | ('B'))); /* Unique identifier 'R' 'E' 'M' 'B' */
5433 put_unaligned_uint32(rtcpheader + packet_len + 16, htonl((1 << 24) | (feedback->remb.br_exp << 18) | (feedback->remb.br_mantissa))); /* Number of SSRCs / BR Exp / BR Mantissa */
5434 put_unaligned_uint32(rtcpheader + packet_len + 20, htonl(rtp->ssrc)); /* The SSRC this feedback message applies to */
5435 res = rtcp_sendto(instance, (unsigned int *)rtcpheader, packet_len + remb_len, 0, rtp->bundled ? remote_address : &rtp->rtcp->them, &ice);
5436 if (res < 0) {
5437 ast_log(LOG_ERROR, "RTCP PSFB transmission error: %s\n", strerror(errno));
5438 } else {
5439 ast_rtcp_calculate_sr_rr_statistics(instance, rtcp_report, rtp->bundled ? *remote_address : rtp->rtcp->them, ice, sr);
5440 }
5441
5442 ao2_unlock(instance);
5443}

References ao2_cleanup, ao2_lock, ao2_unlock, ast_debug_rtcp, ast_log, ast_rtcp_calculate_sr_rr_statistics(), ast_rtcp_generate_compound_prefix(), ast_rtp_instance_get_prop(), AST_RTP_PROPERTY_REMB, AST_RTP_RTCP_FMT_REMB, ast_rtp_rtcp_report_alloc(), ast_sockaddr_isnull(), ast_rtp_rtcp_feedback_remb::br_exp, ast_rtp_rtcp_feedback_remb::br_mantissa, ast_rtp::bundled, ast_frame::data, errno, ast_rtp_rtcp_feedback::fmt, LOG_ERROR, NULL, ast_frame::ptr, put_unaligned_uint32(), RAII_VAR, ast_rtp_rtcp_feedback::remb, ast_rtp::rtcp, RTCP_PT_PSFB, rtcp_sendto(), ast_rtcp::schedid, ast_rtp::ssrc, ast_rtcp::them, and ast_rtp::themssrc_valid.

Referenced by ast_rtp_write().

◆ timeval2ntp()

static void timeval2ntp ( struct timeval  tv,
unsigned int *  msw,
unsigned int *  lsw 
)
static

Definition at line 4543 of file res_rtp_asterisk.c.

4544{
4545 unsigned int sec, usec, frac;
4546 sec = tv.tv_sec + 2208988800u; /* Sec between 1900 and 1970 */
4547 usec = tv.tv_usec;
4548 /*
4549 * Convert usec to 0.32 bit fixed point without overflow.
4550 *
4551 * = usec * 2^32 / 10^6
4552 * = usec * 2^32 / (2^6 * 5^6)
4553 * = usec * 2^26 / 5^6
4554 *
4555 * The usec value needs 20 bits to represent 999999 usec. So
4556 * splitting the 2^26 to get the most precision using 32 bit
4557 * values gives:
4558 *
4559 * = ((usec * 2^12) / 5^6) * 2^14
4560 *
4561 * Splitting the division into two stages preserves all the
4562 * available significant bits of usec over doing the division
4563 * all at once.
4564 *
4565 * = ((((usec * 2^12) / 5^3) * 2^7) / 5^3) * 2^7
4566 */
4567 frac = ((((usec << 12) / 125) << 7) / 125) << 7;
4568 *msw = sec;
4569 *lsw = frac;
4570}

Referenced by ast_rtcp_generate_report(), ast_rtcp_interpret(), ast_rtp_rtcp_handle_nack(), rtp_raw_write(), rtp_transport_wide_cc_feedback_produce(), and update_rtt_stats().

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 10312 of file res_rtp_asterisk.c.

10313{
10316
10317#if defined(HAVE_OPENSSL) && (OPENSSL_VERSION_NUMBER >= 0x10001000L) && !defined(OPENSSL_NO_SRTP) && defined(HAVE_OPENSSL_BIO_METHOD)
10318 if (dtls_bio_methods) {
10319 BIO_meth_free(dtls_bio_methods);
10320 }
10321#endif
10322
10323#ifdef HAVE_PJPROJECT
10324 host_candidate_overrides_clear();
10325 pj_thread_register_check();
10326 rtp_terminate_pjproject();
10327
10329 rtp_unload_acl(&ice_acl_lock, &ice_acl);
10330 rtp_unload_acl(&stun_acl_lock, &stun_acl);
10331 clean_stunaddr();
10332#endif
10333
10334 return 0;
10335}
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30

References acl_change_sub, ARRAY_LEN, ast_cli_unregister_multiple(), ast_rtp_engine_unregister(), asterisk_rtp_engine, cli_rtp, and stasis_unsubscribe_and_join().

◆ update_jitter_stats()

static void update_jitter_stats ( struct ast_rtp rtp,
unsigned int  ia_jitter 
)
static

◆ update_local_mes_stats()

static void update_local_mes_stats ( struct ast_rtp rtp)
static

Definition at line 6296 of file res_rtp_asterisk.c.

6297{
6299 rtp->rtcp->normdevrtt,
6300 rtp->rxjitter,
6301 rtp->rtcp->stdev_rxjitter,
6302 rtp->rtcp->normdev_rxlost);
6303
6304 if (rtp->rtcp->rxmes_count == 0) {
6305 rtp->rtcp->minrxmes = rtp->rxmes;
6306 }
6307 if (rtp->rxmes < rtp->rtcp->minrxmes) {
6308 rtp->rtcp->minrxmes = rtp->rxmes;
6309 }
6310 if (rtp->rxmes > rtp->rtcp->maxrxmes) {
6311 rtp->rtcp->maxrxmes = rtp->rxmes;
6312 }
6313
6315 &rtp->rtcp->stdev_rxmes, &rtp->rtcp->rxmes_count);
6316
6317 ast_debug_rtcp(2, " %s: rtt: %.9f j: %.9f sjh: %.9f lost: %.9f mes: %4.1f\n",
6319 rtp->rtcp->normdevrtt,
6320 rtp->rxjitter,
6321 rtp->rtcp->stdev_rxjitter,
6322 rtp->rtcp->normdev_rxlost, rtp->rxmes);
6323}
static double calc_media_experience_score(struct ast_rtp_instance *instance, double normdevrtt, double normdev_rxjitter, double stdev_rxjitter, double normdev_rxlost)
Calculate a "media experience score" based on given data.
unsigned int rxmes_count
double stdev_rxmes

References ast_debug_rtcp, ast_rtp_instance_get_channel_id(), calc_mean_and_standard_deviation(), calc_media_experience_score(), ast_rtcp::maxrxmes, ast_rtcp::minrxmes, ast_rtcp::normdev_rxlost, ast_rtcp::normdev_rxmes, ast_rtcp::normdevrtt, ast_rtp::owner, ast_rtp::rtcp, ast_rtp::rxjitter, ast_rtp::rxmes, ast_rtcp::rxmes_count, ast_rtcp::stdev_rxjitter, and ast_rtcp::stdev_rxmes.

Referenced by ast_rtcp_generate_report().

◆ update_lost_stats()

static void update_lost_stats ( struct ast_rtp rtp,
unsigned int  lost_packets 
)
static

Definition at line 6152 of file res_rtp_asterisk.c.

6153{
6154 double reported_lost;
6155
6156 rtp->rtcp->reported_lost = lost_packets;
6157 reported_lost = (double)rtp->rtcp->reported_lost;
6158 if (rtp->rtcp->reported_lost_count == 0) {
6159 rtp->rtcp->reported_minlost = reported_lost;
6160 }
6161 if (reported_lost < rtp->rtcp->reported_minlost) {
6162 rtp->rtcp->reported_minlost = reported_lost;
6163 }
6164 if (reported_lost > rtp->rtcp->reported_maxlost) {
6165 rtp->rtcp->reported_maxlost = reported_lost;
6166 }
6167
6170}
unsigned int reported_lost_count

References calc_mean_and_standard_deviation(), if(), ast_rtcp::reported_lost, ast_rtcp::reported_lost_count, ast_rtcp::reported_maxlost, ast_rtcp::reported_minlost, ast_rtcp::reported_normdev_lost, ast_rtcp::reported_stdev_lost, and ast_rtp::rtcp.

Referenced by ast_rtcp_interpret().

◆ update_reported_mes_stats()

static void update_reported_mes_stats ( struct ast_rtp rtp)
static

Definition at line 6261 of file res_rtp_asterisk.c.

6262{
6263 double mes = calc_media_experience_score(rtp->owner,
6264 rtp->rtcp->normdevrtt,
6265 rtp->rtcp->reported_jitter,
6268
6269 rtp->rtcp->reported_mes = mes;
6270 if (rtp->rtcp->reported_mes_count == 0) {
6271 rtp->rtcp->reported_minmes = mes;
6272 }
6273 if (mes < rtp->rtcp->reported_minmes) {
6274 rtp->rtcp->reported_minmes = mes;
6275 }
6276 if (mes > rtp->rtcp->reported_maxmes) {
6277 rtp->rtcp->reported_maxmes = mes;
6278 }
6279
6282
6283 ast_debug_rtcp(2, "%s: rtt: %.9f j: %.9f sjh: %.9f lost: %.9f mes: %4.1f\n",
6285 rtp->rtcp->normdevrtt,
6286 rtp->rtcp->reported_jitter,
6288 rtp->rtcp->reported_normdev_lost, mes);
6289}
unsigned int reported_mes_count

References ast_debug_rtcp, ast_rtp_instance_get_channel_id(), calc_mean_and_standard_deviation(), calc_media_experience_score(), ast_rtcp::normdevrtt, ast_rtp::owner, ast_rtcp::reported_jitter, ast_rtcp::reported_maxmes, ast_rtcp::reported_mes, ast_rtcp::reported_mes_count, ast_rtcp::reported_minmes, ast_rtcp::reported_normdev_lost, ast_rtcp::reported_normdev_mes, ast_rtcp::reported_stdev_jitter, ast_rtcp::reported_stdev_mes, and ast_rtp::rtcp.

Referenced by ast_rtcp_interpret().

◆ update_rtt_stats()

static int update_rtt_stats ( struct ast_rtp rtp,
unsigned int  lsr,
unsigned int  dlsr 
)
static

Definition at line 6071 of file res_rtp_asterisk.c.

6072{
6073 struct timeval now;
6074 struct timeval rtt_tv;
6075 unsigned int msw;
6076 unsigned int lsw;
6077 unsigned int rtt_msw;
6078 unsigned int rtt_lsw;
6079 unsigned int lsr_a;
6080 unsigned int rtt;
6081
6082 gettimeofday(&now, NULL);
6083 timeval2ntp(now, &msw, &lsw);
6084
6085 lsr_a = ((msw & 0x0000ffff) << 16) | ((lsw & 0xffff0000) >> 16);
6086 rtt = lsr_a - lsr - dlsr;
6087 rtt_msw = (rtt & 0xffff0000) >> 16;
6088 rtt_lsw = (rtt & 0x0000ffff);
6089 rtt_tv.tv_sec = rtt_msw;
6090 /*
6091 * Convert 16.16 fixed point rtt_lsw to usec without
6092 * overflow.
6093 *
6094 * = rtt_lsw * 10^6 / 2^16
6095 * = rtt_lsw * (2^6 * 5^6) / 2^16
6096 * = rtt_lsw * 5^6 / 2^10
6097 *
6098 * The rtt_lsw value is in 16.16 fixed point format and 5^6
6099 * requires 14 bits to represent. We have enough space to
6100 * directly do the conversion because there is no integer
6101 * component in rtt_lsw.
6102 */
6103 rtt_tv.tv_usec = (rtt_lsw * 15625) >> 10;
6104 rtp->rtcp->rtt = (double)rtt_tv.tv_sec + ((double)rtt_tv.tv_usec / 1000000);
6105 if (lsr_a - dlsr < lsr) {
6106 return 1;
6107 }
6108
6109 rtp->rtcp->accumulated_transit += rtp->rtcp->rtt;
6110 if (rtp->rtcp->rtt_count == 0 || rtp->rtcp->minrtt > rtp->rtcp->rtt) {
6111 rtp->rtcp->minrtt = rtp->rtcp->rtt;
6112 }
6113 if (rtp->rtcp->maxrtt < rtp->rtcp->rtt) {
6114 rtp->rtcp->maxrtt = rtp->rtcp->rtt;
6115 }
6116
6118 &rtp->rtcp->stdevrtt, &rtp->rtcp->rtt_count);
6119
6120 return 0;
6121}
double accumulated_transit
unsigned int rtt_count

References ast_rtcp::accumulated_transit, calc_mean_and_standard_deviation(), ast_rtcp::maxrtt, ast_rtcp::minrtt, ast_rtcp::normdevrtt, NULL, ast_rtp::rtcp, ast_rtcp::rtt, ast_rtcp::rtt_count, ast_rtcp::stdevrtt, and timeval2ntp().

Referenced by ast_rtcp_interpret().

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Asterisk RTP Stack" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .reload = reload_module, .load_pri = AST_MODPRI_CHANNEL_DEPEND, #ifdef HAVE_PJPROJECT .requires = "res_pjproject", #endif }
static

Definition at line 10346 of file res_rtp_asterisk.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 10346 of file res_rtp_asterisk.c.

◆ asterisk_rtp_engine

struct ast_rtp_engine asterisk_rtp_engine
static

Definition at line 2569 of file res_rtp_asterisk.c.

Referenced by load_module(), and unload_module().

◆ cli_rtp

struct ast_cli_entry cli_rtp[]
static

Definition at line 9906 of file res_rtp_asterisk.c.

Referenced by load_module(), and unload_module().

◆ dtmftimeout

int dtmftimeout = DEFAULT_DTMF_TIMEOUT
static

Definition at line 209 of file res_rtp_asterisk.c.

Referenced by handle_cli_rtp_settings(), process_dtmf_rfc2833(), and rtp_reload().

◆ learning_min_duration

int learning_min_duration = DEFAULT_LEARNING_MIN_DURATION
static

Lowest acceptable timeout between the first and the last sequential RTP frame.

Definition at line 224 of file res_rtp_asterisk.c.

Referenced by rtp_learning_rtp_seq_update(), and rtp_reload().

◆ learning_min_sequential

int learning_min_sequential = DEFAULT_LEARNING_MIN_SEQUENTIAL
static

Number of sequential RTP frames needed from a single source during learning mode to accept new source.

Definition at line 223 of file res_rtp_asterisk.c.

Referenced by handle_cli_rtp_settings(), rtp_learning_rtp_seq_update(), rtp_learning_seq_init(), and rtp_reload().

◆ res_srtp

struct ast_srtp_res* res_srtp
extern

◆ res_srtp_policy

struct ast_srtp_policy_res* res_srtp_policy
extern

◆ rtcpdebugaddr

struct ast_sockaddr rtcpdebugaddr
static

Debug RTCP packets to/from this host

Definition at line 216 of file res_rtp_asterisk.c.

Referenced by handle_cli_rtcp_set_debug(), rtcp_debug_test_addr(), and rtcp_do_debug_ip().

◆ rtcpdebugport

int rtcpdebugport
static

Debug only RTCP packets from IP or IP+Port if port is > 0

Definition at line 218 of file res_rtp_asterisk.c.

Referenced by rtcp_debug_test_addr(), and rtcp_do_debug_ip().

◆ rtcpinterval

int rtcpinterval = RTCP_DEFAULT_INTERVALMS
static

Time between rtcp reports in millisecs

Definition at line 214 of file res_rtp_asterisk.c.

Referenced by ast_rtcp_calc_interval(), and rtp_reload().

◆ rtcpstats

int rtcpstats
static

Are we debugging RTCP?

Definition at line 213 of file res_rtp_asterisk.c.

Referenced by handle_cli_rtcp_set_stats().

◆ rtpdebugaddr

struct ast_sockaddr rtpdebugaddr
static

Debug packets to/from this host

Definition at line 215 of file res_rtp_asterisk.c.

Referenced by handle_cli_rtp_set_debug(), rtp_debug_test_addr(), and rtp_do_debug_ip().

◆ rtpdebugport

int rtpdebugport
static

Debug only RTP packets from IP or IP+Port if port is > 0

Definition at line 217 of file res_rtp_asterisk.c.

Referenced by rtp_debug_test_addr(), and rtp_do_debug_ip().

◆ rtpend

int rtpend = DEFAULT_RTP_END
static

Last port for RTP sessions (set in rtp.conf)

Definition at line 212 of file res_rtp_asterisk.c.

Referenced by handle_cli_rtp_settings(), rtp_allocate_transport(), and rtp_reload().

◆ rtpstart

int rtpstart = DEFAULT_RTP_START
static

First port for RTP sessions (set in rtp.conf)

Definition at line 211 of file res_rtp_asterisk.c.

Referenced by handle_cli_rtp_settings(), rtp_allocate_transport(), and rtp_reload().

◆ srtp_replay_protection

int srtp_replay_protection = DEFAULT_SRTP_REPLAY_PROTECTION
static

◆ strictrtp

int strictrtp = DEFAULT_STRICT_RTP
static

Only accept RTP frames from a defined source. If we receive an indication of a changing source, enter learning mode.

Definition at line 222 of file res_rtp_asterisk.c.

Referenced by ast_rtp_remote_address_set(), handle_cli_rtp_settings(), rtp_allocate_transport(), rtp_learning_rtp_seq_update(), and rtp_reload().