RTS API Documentation  1.10.11
Data Structures | Macros | Typedefs | Enumerations | Functions | Variables
switch_rtp.c File Reference
#include <switch.h>
#include <switch_private.h>
#include <switch_stun.h>
#include <fspr_network_io.h>
#include <srtp.h>
#include <srtp_priv.h>
#include <switch_ssl.h>
#include <switch_jitterbuffer.h>
+ Include dependency graph for switch_rtp.c:

Go to the source code of this file.

Data Structures

struct  rtp_msg_t
 
struct  rtcp_fir_t
 
struct  switch_rtcp_sdes_unit_s
 
struct  rtcp_tmmbx_t
 
struct  switch_rtcp_ext_hdr_t
 
struct  rtcp_ext_msg_t
 
struct  rtcp_msg_t
 
struct  sdes_ssrc_t
 
struct  switch_rtp_vad_data
 
struct  switch_rtp_rfc2833_data
 
struct  switch_rtp_ice_t
 
struct  switch_dtls_s
 
struct  ts_normalize_s
 
struct  switch_rtp
 
struct  switch_rtcp_report_block
 
struct  switch_rtcp_sr_head
 
struct  switch_rtcp_sender_info
 
struct  switch_rtcp_sender_report
 
struct  switch_rtcp_receiver_report
 
struct  packet_list_s
 
struct  dtls_bio_filter
 

Macros

#define DEBUG_ESTIMATORS_
 
#define JITTER_LEAD_FRAMES   10
 
#define READ_INC(rtp_session)   switch_mutex_lock(rtp_session->read_mutex); rtp_session->reading++
 
#define READ_DEC(rtp_session)   rtp_session->reading--; switch_mutex_unlock(rtp_session->read_mutex)
 
#define WRITE_INC(rtp_session)   switch_mutex_lock(rtp_session->write_mutex); rtp_session->writing++
 
#define WRITE_DEC(rtp_session)   rtp_session->writing--; switch_mutex_unlock(rtp_session->write_mutex)
 
#define RTP_STUN_FREQ   1000000
 
#define rtp_header_len   12
 
#define RTP_START_PORT   16384
 
#define RTP_END_PORT   32768
 
#define MASTER_KEY_LEN   30
 
#define RTP_MAGIC_NUMBER   42
 
#define WARN_SRTP_ERRS   10
 
#define MAX_SRTP_ERRS   100
 
#define NTP_TIME_OFFSET   2208988800UL
 
#define DTMF_SANITY   (rtp_session->one_second * 30)
 
#define rtp_session_name(_rtp_session)   _rtp_session->session ? switch_core_session_get_name(_rtp_session->session) : "-"
 
#define STUN_USERNAME_MAX_SIZE   513 /* From RFC5389: "It MUST contain a UTF-8 [RFC3629] encoded sequence of less than 513 bytes" */
 
#define SDP_UFRAG_MAX_SIZE
 
#define RTP_BODY(_s)   (char *) (_s->recv_msg.ebody ? _s->recv_msg.ebody : _s->recv_msg.body)
 
#define KALMAN_SYSTEM_MODELS   3 /*loss, jitter, rtt*/
 
#define EST_LOSS   0
 
#define EST_JITTER   1
 
#define EST_RTT   2
 
#define MAX_DTLS_MTU   4096
 
#define rtp_type(rtp_session)   rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")
 
#define MEDIA_TOO_LONG   2000
 
#define STUN_TOO_LONG   20000
 
#define ADJ_TOO_LONG   1000
 
#define MAX_NACK   10
 
#define dtls_set_state(_dtls, _state)   switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "Changing %s DTLS state from %s to %s\n", rtp_type(rtp_session), dtls_state_names(_dtls->state), dtls_state_names(_state)); _dtls->new_state = 1; _dtls->last_state = _dtls->state; _dtls->state = _state
 
#define cr_keylen   16
 
#define cr_saltlen   14
 
#define cr_kslen   30
 
#define return_cng_frame()   do_cng = 1; goto timer_check
 

Typedefs

typedef srtp_hdr_t rtp_hdr_t
 
typedef struct switch_rtcp_sdes_unit_s switch_rtcp_sdes_unit_t
 
typedef struct switch_dtls_s switch_dtls_t
 
typedef int(* dtls_state_handler_t) (switch_rtp_t *, switch_dtls_t *)
 
typedef struct ts_normalize_s ts_normalize_t
 
typedef struct packet_list_s packet_list_t
 
typedef struct dtls_bio_filter dtls_bio_filter
 

Enumerations

enum  vad_talk_mask_t { VAD_FIRE_TALK = (1 << 0), VAD_FIRE_NOT_TALK = (1 << 1) }
 
enum  handle_rfc2833_result_t { RESULT_CONTINUE, RESULT_GOTO_END, RESULT_GOTO_RECVFROM, RESULT_GOTO_TIMERCHECK }
 

Functions

static switch_size_t do_flush (switch_rtp_t *rtp_session, int force, switch_size_t bytes_in)
 
static void switch_rtp_dtls_init (void)
 
static void switch_rtp_dtls_destroy (void)
 
static int dtls_state_handshake (switch_rtp_t *rtp_session, switch_dtls_t *dtls)
 
static int dtls_state_ready (switch_rtp_t *rtp_session, switch_dtls_t *dtls)
 
static int dtls_state_setup (switch_rtp_t *rtp_session, switch_dtls_t *dtls)
 
static int dtls_state_fail (switch_rtp_t *rtp_session, switch_dtls_t *dtls)
 
static void do_2833 (switch_rtp_t *rtp_session)
 
static void switch_rtp_change_ice_dest (switch_rtp_t *rtp_session, switch_rtp_ice_t *ice, const char *host, switch_port_t port)
 
static handle_rfc2833_result_t handle_rfc2833 (switch_rtp_t *rtp_session, switch_size_t bytes, int *do_cng)
 
static int rtp_write_ready (switch_rtp_t *rtp_session, uint32_t bytes, int line)
 
static int rtp_common_write (switch_rtp_t *rtp_session, rtp_msg_t *send_msg, void *data, uint32_t datalen, switch_payload_t payload, uint32_t timestamp, switch_frame_flag_t *flags)
 
static void calc_elapsed (switch_rtp_t *rtp_session, switch_rtp_ice_t *ice)
 
static switch_status_t ice_out (switch_rtp_t *rtp_session, switch_rtp_ice_t *ice, switch_bool_t force)
 
int icecmp (const char *them, switch_rtp_ice_t *ice)
 
static void handle_ice (switch_rtp_t *rtp_session, switch_rtp_ice_t *ice, void *data, switch_size_t len)
 
void switch_rtp_init (switch_memory_pool_t *pool)
 Initilize the RTP System. More...
 
static uint8_t get_next_write_ts (switch_rtp_t *rtp_session, uint32_t timestamp)
 
static void do_mos (switch_rtp_t *rtp_session)
 
void burstr_calculate (int loss[], int received, double *burstr, double *lossr)
 
static void reset_jitter_seq (switch_rtp_t *rtp_session)
 
static void check_jitter (switch_rtp_t *rtp_session)
 
static void rtcp_generate_sender_info (switch_rtp_t *rtp_session, struct switch_rtcp_sender_info *sr)
 
static uint32_t calc_local_lsr_now (void)
 
static void rtcp_generate_report_block (switch_rtp_t *rtp_session, struct switch_rtcp_report_block *rtcp_report_block, int16_t extra_expected)
 
static void rtcp_stats_init (switch_rtp_t *rtp_session)
 
static int rtcp_stats (switch_rtp_t *rtp_session)
 
static void calc_bw_exp (uint32_t bps, uint8_t bits, rtcp_tmmbx_t *tmmbx)
 
static int using_ice (switch_rtp_t *rtp_session)
 
static void switch_send_rtcp_event (switch_rtp_t *rtp_session, struct switch_rtcp_sender_report *sr, struct switch_rtcp_report_block *rtcp_report_block)
 
static int check_rtcp_and_ice (switch_rtp_t *rtp_session)
 
void switch_rtp_ping (switch_rtp_t *rtp_session)
 
void switch_rtp_get_random (void *buf, uint32_t len)
 
void switch_rtp_shutdown (void)
 
switch_port_t switch_rtp_set_start_port (switch_port_t port)
 Set/Get RTP start port. More...
 
switch_port_t switch_rtp_set_end_port (switch_port_t port)
 Set/Get RTP end port. More...
 
void switch_rtp_release_port (const char *ip, switch_port_t port)
 
switch_port_t switch_rtp_request_port (const char *ip)
 Request a new port to be used for media. More...
 
switch_status_t switch_rtp_set_payload_map (switch_rtp_t *rtp_session, payload_map_t **pmap)
 
void switch_rtp_intentional_bugs (switch_rtp_t *rtp_session, switch_rtp_bug_flag_t bugs)
 
static switch_status_t enable_remote_rtcp_socket (switch_rtp_t *rtp_session, const char **err)
 
static switch_status_t enable_local_rtcp_socket (switch_rtp_t *rtp_session, const char **err)
 
switch_status_t switch_rtp_set_local_address (switch_rtp_t *rtp_session, const char *host, switch_port_t port, const char **err)
 Assign a local address to the RTP session. More...
 
void switch_rtp_set_media_timeout (switch_rtp_t *rtp_session, uint32_t ms)
 
void switch_rtp_set_max_missed_packets (switch_rtp_t *rtp_session, uint32_t max)
 
void switch_rtp_reset_jb (switch_rtp_t *rtp_session)
 
void switch_rtp_reset_vb (switch_rtp_t *rtp_session)
 
void switch_rtp_reset (switch_rtp_t *rtp_session)
 
void switch_rtp_reset_media_timer (switch_rtp_t *rtp_session)
 
char * switch_rtp_get_remote_host (switch_rtp_t *rtp_session)
 
switch_port_t switch_rtp_get_remote_port (switch_rtp_t *rtp_session)
 
static void ping_socket (switch_rtp_t *rtp_session)
 
switch_status_t switch_rtp_udptl_mode (switch_rtp_t *rtp_session)
 
switch_status_t switch_rtp_set_remote_address (switch_rtp_t *rtp_session, const char *host, switch_port_t port, switch_port_t remote_rtcp_port, switch_bool_t change_adv_addr, const char **err)
 Assign a remote address to the RTP session. More...
 
static const char * dtls_state_names (dtls_state_t s)
 
static void free_dtls (switch_dtls_t **dtlsp)
 
static int do_dtls (switch_rtp_t *rtp_session, switch_dtls_t *dtls)
 
BIO_METHOD * BIO_dtls_filter (void)
 
static int dtls_bio_filter_new (BIO *bio)
 
static int dtls_bio_filter_free (BIO *bio)
 
static int dtls_bio_filter_write (BIO *bio, const char *in, int inl)
 
static long dtls_bio_filter_ctrl (BIO *bio, int cmd, long num, void *ptr)
 
int switch_rtp_has_dtls (void)
 
dtls_state_t switch_rtp_dtls_state (switch_rtp_t *rtp_session, dtls_type_t type)
 
switch_status_t switch_rtp_del_dtls (switch_rtp_t *rtp_session, dtls_type_t type)
 
switch_status_t switch_rtp_add_dtls (switch_rtp_t *rtp_session, dtls_fingerprint_t *local_fp, dtls_fingerprint_t *remote_fp, dtls_type_t type, uint8_t want_DTLSv1_2)
 
switch_status_t switch_rtp_add_crypto_key (switch_rtp_t *rtp_session, switch_rtp_crypto_direction_t direction, uint32_t index, switch_secure_settings_t *ssec)
 
switch_status_t switch_rtp_set_interval (switch_rtp_t *rtp_session, uint32_t ms_per_packet, uint32_t samples_per_interval)
 
switch_status_t switch_rtp_change_interval (switch_rtp_t *rtp_session, uint32_t ms_per_packet, uint32_t samples_per_interval)
 
switch_status_t switch_rtp_set_ssrc (switch_rtp_t *rtp_session, uint32_t ssrc)
 
switch_status_t switch_rtp_set_remote_ssrc (switch_rtp_t *rtp_session, uint32_t ssrc)
 
switch_status_t switch_rtp_create (switch_rtp_t **new_rtp_session, switch_payload_t payload, uint32_t samples_per_interval, uint32_t ms_per_packet, switch_rtp_flag_t flags[SWITCH_RTP_FLAG_INVALID], char *timer_name, const char **err, switch_memory_pool_t *pool)
 create a new RTP session handle More...
 
switch_rtp_tswitch_rtp_new (const char *rx_host, switch_port_t rx_port, const char *tx_host, switch_port_t tx_port, switch_payload_t payload, uint32_t samples_per_interval, uint32_t ms_per_packet, switch_rtp_flag_t flags[SWITCH_RTP_FLAG_INVALID], char *timer_name, const char **err, switch_memory_pool_t *pool, switch_port_t bundle_internal_port, switch_port_t bundle_external_port)
 prepare a new RTP session handle and fully initilize it More...
 
void switch_rtp_set_telephony_event (switch_rtp_t *rtp_session, switch_payload_t te)
 Set the payload type to consider RFC2833 DTMF. More...
 
void switch_rtp_set_telephony_recv_event (switch_rtp_t *rtp_session, switch_payload_t te)
 
void switch_rtp_set_cng_pt (switch_rtp_t *rtp_session, switch_payload_t pt)
 Set the payload type for comfort noise. More...
 
switch_timer_tswitch_rtp_get_media_timer (switch_rtp_t *rtp_session)
 
switch_jb_tswitch_rtp_get_jitter_buffer (switch_rtp_t *rtp_session)
 
switch_status_t switch_rtp_pause_jitter_buffer (switch_rtp_t *rtp_session, switch_bool_t pause)
 
switch_status_t switch_rtp_deactivate_jitter_buffer (switch_rtp_t *rtp_session)
 
switch_status_t switch_rtp_get_video_buffer_size (switch_rtp_t *rtp_session, uint32_t *min_frame_len, uint32_t *max_frame_len, uint32_t *cur_frame_len, uint32_t *highest_frame_len)
 
switch_status_t switch_rtp_set_video_buffer_size (switch_rtp_t *rtp_session, uint32_t frames, uint32_t max_frames)
 
switch_status_t switch_rtp_debug_jitter_buffer (switch_rtp_t *rtp_session, const char *name)
 
switch_status_t switch_rtp_activate_jitter_buffer (switch_rtp_t *rtp_session, uint32_t queue_frames, uint32_t max_queue_frames, uint32_t samples_per_packet, uint32_t samples_per_second)
 Acvite a jitter buffer on an RTP session. More...
 
switch_status_t switch_rtp_activate_rtcp (switch_rtp_t *rtp_session, int send_rate, switch_port_t remote_port, switch_bool_t mux)
 Activate sending RTCP Sender Reports (SR's) More...
 
switch_status_t switch_rtp_activate_ice (switch_rtp_t *rtp_session, char *login, char *rlogin, const char *password, const char *rpassword, ice_proto_t proto, switch_core_media_ice_type_t type, ice_t *ice_params)
 Acvite ICE on an RTP session. More...
 
void switch_rtp_flush (switch_rtp_t *rtp_session)
 
switch_status_t switch_rtp_req_bitrate (switch_rtp_t *rtp_session, uint32_t bps)
 
switch_status_t switch_rtp_ack_bitrate (switch_rtp_t *rtp_session, uint32_t bps)
 
void switch_rtp_video_refresh (switch_rtp_t *rtp_session)
 
void switch_rtp_video_loss (switch_rtp_t *rtp_session)
 
void switch_rtp_break (switch_rtp_t *rtp_session)
 
void switch_rtp_kill_socket (switch_rtp_t *rtp_session)
 Kill the socket on an existing RTP session. More...
 
uint8_t switch_rtp_ready (switch_rtp_t *rtp_session)
 Test if an RTP session is ready. More...
 
switch_status_t switch_rtp_sync_stats (switch_rtp_t *rtp_session)
 
void switch_rtp_destroy (switch_rtp_t **rtp_session)
 Destroy an RTP session. More...
 
void switch_rtp_set_interdigit_delay (switch_rtp_t *rtp_session, uint32_t delay)
 
switch_socket_tswitch_rtp_get_rtp_socket (switch_rtp_t *rtp_session)
 Retrieve the socket from an existing RTP session. More...
 
uint32_t switch_rtp_get_default_samples_per_interval (switch_rtp_t *rtp_session)
 Get the default samples per interval for a given RTP session. More...
 
void switch_rtp_set_default_payload (switch_rtp_t *rtp_session, switch_payload_t payload)
 Set the default payload number for a given RTP session. More...
 
uint32_t switch_rtp_get_default_payload (switch_rtp_t *rtp_session)
 Get the default payload number for a given RTP session. More...
 
void switch_rtp_set_invalid_handler (switch_rtp_t *rtp_session, switch_rtp_invalid_handler_t on_invalid)
 Set a callback function to execute when an invalid RTP packet is encountered. More...
 
void switch_rtp_set_flags (switch_rtp_t *rtp_session, switch_rtp_flag_t flags[SWITCH_RTP_FLAG_INVALID])
 
void switch_rtp_clear_flags (switch_rtp_t *rtp_session, switch_rtp_flag_t flags[SWITCH_RTP_FLAG_INVALID])
 
void switch_rtp_set_flag (switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
 Set an RTP Flag. More...
 
uint32_t switch_rtp_test_flag (switch_rtp_t *rtp_session, switch_rtp_flag_t flags)
 Test an RTP Flag. More...
 
void switch_rtp_clear_flag (switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
 Clear an RTP Flag. More...
 
static void set_dtmf_delay (switch_rtp_t *rtp_session, uint32_t ms, uint32_t max_ms)
 
void rtp_flush_read_buffer (switch_rtp_t *rtp_session, switch_rtp_flush_t flush)
 
static int jb_valid (switch_rtp_t *rtp_session)
 
static int check_recv_payload (switch_rtp_t *rtp_session)
 
static int get_recv_payload (switch_rtp_t *rtp_session)
 
static switch_status_t read_bundle_rtp_packet (switch_rtp_t *rtp_session, switch_size_t *bytes, switch_frame_flag_t *flags)
 
static switch_status_t read_rtp_packet (switch_rtp_t *rtp_session, switch_size_t *bytes, switch_frame_flag_t *flags, payload_map_t **pmapP, switch_status_t poll_status, switch_bool_t return_jb_packet)
 
static void handle_nack (switch_rtp_t *rtp_session, uint32_t nack)
 
static switch_status_t process_rtcp_report (switch_rtp_t *rtp_session, rtcp_msg_t *msg, switch_size_t bytes)
 
static switch_status_t process_rtcp_packet (switch_rtp_t *rtp_session, switch_size_t *bytes)
 
static switch_status_t read_rtcp_packet (switch_rtp_t *rtp_session, switch_size_t *bytes, switch_frame_flag_t *flags)
 
static void check_timeout (switch_rtp_t *rtp_session)
 
static int rtp_common_read (switch_rtp_t *rtp_session, switch_payload_t *payload_type, payload_map_t **pmapP, switch_frame_flag_t *flags, switch_io_flag_t io_flags)
 
switch_byte_t switch_rtp_check_auto_adj (switch_rtp_t *rtp_session)
 
switch_size_t switch_rtp_has_dtmf (switch_rtp_t *rtp_session)
 Test for presence of DTMF on a given RTP session. More...
 
switch_size_t switch_rtp_dequeue_dtmf (switch_rtp_t *rtp_session, switch_dtmf_t *dtmf)
 Retrieve DTMF digits from a given RTP session. More...
 
switch_status_t switch_rtp_queue_rfc2833 (switch_rtp_t *rtp_session, const switch_dtmf_t *dtmf)
 Queue RFC2833 DTMF data into an RTP Session. More...
 
switch_status_t switch_rtp_queue_rfc2833_in (switch_rtp_t *rtp_session, const switch_dtmf_t *dtmf)
 Queue RFC2833 DTMF data into an RTP Session. More...
 
switch_status_t switch_rtp_read (switch_rtp_t *rtp_session, void *data, uint32_t *datalen, switch_payload_t *payload_type, switch_frame_flag_t *flags, switch_io_flag_t io_flags)
 Read data from a given RTP session. More...
 
switch_status_t switch_rtcp_zerocopy_read_frame (switch_rtp_t *rtp_session, switch_rtcp_frame_t *frame)
 Read RTCP data from a given RTP session without copying. More...
 
switch_status_t switch_rtp_zerocopy_read_frame (switch_rtp_t *rtp_session, switch_frame_t *frame, switch_io_flag_t io_flags)
 Read data from a given RTP session without copying. More...
 
switch_status_t switch_rtp_zerocopy_read (switch_rtp_t *rtp_session, void **data, uint32_t *datalen, switch_payload_t *payload_type, switch_frame_flag_t *flags, switch_io_flag_t io_flags)
 Read data from a given RTP session without copying. More...
 
switch_status_t switch_rtp_disable_vad (switch_rtp_t *rtp_session)
 Disable VAD on an RTP Session. More...
 
switch_status_t switch_rtp_enable_vad (switch_rtp_t *rtp_session, switch_core_session_t *session, switch_codec_t *codec, switch_vad_flag_t flags)
 Enable VAD on an RTP Session. More...
 
int switch_rtp_write_frame (switch_rtp_t *rtp_session, switch_frame_t *frame)
 Write data to a given RTP session. More...
 
switch_rtp_stats_tswitch_rtp_get_stats (switch_rtp_t *rtp_session, switch_memory_pool_t *pool)
 
int switch_rtp_write_manual (switch_rtp_t *rtp_session, void *data, uint32_t datalen, uint8_t m, switch_payload_t payload, uint32_t ts, switch_frame_flag_t *flags)
 Write data with a specified payload and sequence number to a given RTP session. More...
 
switch_status_t switch_rtp_write_raw (switch_rtp_t *rtp_session, void *data, switch_size_t *bytes, switch_bool_t process_encryption)
 
uint32_t switch_rtp_get_ssrc (switch_rtp_t *rtp_session)
 Retrieve the SSRC from a given RTP session. More...
 
void switch_rtp_set_private (switch_rtp_t *rtp_session, void *private_data)
 Associate an arbitrary data pointer with and RTP session. More...
 
void * switch_rtp_get_private (switch_rtp_t *rtp_session)
 Retrieve the private data from a given RTP session. More...
 
switch_core_session_tswitch_rtp_get_core_session (switch_rtp_t *rtp_session)
 

Variables

static const switch_payload_t INVALID_PT = 255
 
static switch_port_t START_PORT = RTP_START_PORT
 
static switch_port_t END_PORT = RTP_END_PORT
 
static switch_mutex_tport_lock = NULL
 
static switch_hash_talloc_hash = NULL
 
dtls_state_handler_t dtls_states [DS_INVALID] = {NULL, dtls_state_handshake, dtls_state_setup, dtls_state_ready, dtls_state_fail}
 
static int global_init = 0
 
static const char * dtls_state_names_t [] = {"OFF", "HANDSHAKE", "SETUP", "READY", "FAIL", "INVALID"}
 
static BIO_METHOD dtls_bio_filter_methods
 

Macro Definition Documentation

◆ ADJ_TOO_LONG

#define ADJ_TOO_LONG   1000

Definition at line 825 of file switch_rtp.c.

Referenced by calc_elapsed().

◆ cr_keylen

#define cr_keylen   16

Definition at line 3214 of file switch_rtp.c.

Referenced by dtls_state_setup().

◆ cr_kslen

#define cr_kslen   30

Definition at line 3216 of file switch_rtp.c.

Referenced by dtls_state_setup().

◆ cr_saltlen

#define cr_saltlen   14

Definition at line 3215 of file switch_rtp.c.

Referenced by dtls_state_setup().

◆ DEBUG_ESTIMATORS_

#define DEBUG_ESTIMATORS_

Definition at line 63 of file switch_rtp.c.

◆ dtls_set_state

#define dtls_set_state (   _dtls,
  _state 
)    switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "Changing %s DTLS state from %s to %s\n", rtp_type(rtp_session), dtls_state_names(_dtls->state), dtls_state_names(_state)); _dtls->new_state = 1; _dtls->last_state = _dtls->state; _dtls->state = _state

◆ DTMF_SANITY

#define DTMF_SANITY   (rtp_session->one_second * 30)

Definition at line 83 of file switch_rtp.c.

Referenced by handle_rfc2833().

◆ EST_JITTER

#define EST_JITTER   1

Definition at line 175 of file switch_rtp.c.

◆ EST_LOSS

#define EST_LOSS   0

Definition at line 174 of file switch_rtp.c.

Referenced by process_rtcp_report(), and rtcp_stats_init().

◆ EST_RTT

#define EST_RTT   2

Definition at line 176 of file switch_rtp.c.

Referenced by process_rtcp_report(), and rtcp_stats_init().

◆ JITTER_LEAD_FRAMES

#define JITTER_LEAD_FRAMES   10

Definition at line 66 of file switch_rtp.c.

Referenced by check_jitter(), and rtp_common_read().

◆ KALMAN_SYSTEM_MODELS

#define KALMAN_SYSTEM_MODELS   3 /*loss, jitter, rtt*/

Definition at line 173 of file switch_rtp.c.

Referenced by rtcp_stats_init().

◆ MASTER_KEY_LEN

#define MASTER_KEY_LEN   30

Definition at line 76 of file switch_rtp.c.

◆ MAX_DTLS_MTU

#define MAX_DTLS_MTU   4096

Definition at line 273 of file switch_rtp.c.

Referenced by do_dtls(), and switch_rtp_add_dtls().

◆ MAX_NACK

#define MAX_NACK   10

Definition at line 2136 of file switch_rtp.c.

Referenced by check_rtcp_and_ice().

◆ MAX_SRTP_ERRS

#define MAX_SRTP_ERRS   100

Definition at line 79 of file switch_rtp.c.

Referenced by read_bundle_rtp_packet(), and read_rtp_packet().

◆ MEDIA_TOO_LONG

#define MEDIA_TOO_LONG   2000

Definition at line 823 of file switch_rtp.c.

Referenced by calc_elapsed(), and handle_ice().

◆ NTP_TIME_OFFSET

#define NTP_TIME_OFFSET   2208988800UL

Definition at line 80 of file switch_rtp.c.

Referenced by calc_local_lsr_now(), and rtcp_generate_sender_info().

◆ READ_DEC

#define READ_DEC (   rtp_session)    rtp_session->reading--; switch_mutex_unlock(rtp_session->read_mutex)

◆ READ_INC

#define READ_INC (   rtp_session)    switch_mutex_lock(rtp_session->read_mutex); rtp_session->reading++

◆ return_cng_frame

#define return_cng_frame ( )    do_cng = 1; goto timer_check

Definition at line 5848 of file switch_rtp.c.

Referenced by rtp_common_read().

◆ RTP_BODY

#define RTP_BODY (   _s)    (char *) (_s->recv_msg.ebody ? _s->recv_msg.ebody : _s->recv_msg.body)

◆ RTP_END_PORT

#define RTP_END_PORT   32768

Definition at line 75 of file switch_rtp.c.

◆ rtp_header_len

#define rtp_header_len   12

◆ RTP_MAGIC_NUMBER

#define RTP_MAGIC_NUMBER   42

Definition at line 77 of file switch_rtp.c.

◆ rtp_session_name

#define rtp_session_name (   _rtp_session)    _rtp_session->session ? switch_core_session_get_name(_rtp_session->session) : "-"

◆ RTP_START_PORT

#define RTP_START_PORT   16384

Definition at line 74 of file switch_rtp.c.

◆ RTP_STUN_FREQ

#define RTP_STUN_FREQ   1000000

Definition at line 72 of file switch_rtp.c.

Referenced by ice_out().

◆ rtp_type

#define rtp_type (   rtp_session)    rtp_session->flags[SWITCH_RTP_FLAG_TEXT] ? "text" : (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio")

◆ SDP_UFRAG_MAX_SIZE

#define SDP_UFRAG_MAX_SIZE
Value:
256 /* From draft-ietf-mmusic-ice-sip-sdp-24: "the ice-ufrag attribute MUST NOT be longer than 32
* characters when sending, but an implementation MUST accept up to 256
* characters when receiving." */

Definition at line 88 of file switch_rtp.c.

Referenced by switch_rtp_activate_ice().

◆ STUN_TOO_LONG

#define STUN_TOO_LONG   20000

Definition at line 824 of file switch_rtp.c.

Referenced by handle_ice().

◆ STUN_USERNAME_MAX_SIZE

#define STUN_USERNAME_MAX_SIZE   513 /* From RFC5389: "It MUST contain a UTF-8 [RFC3629] encoded sequence of less than 513 bytes" */

Definition at line 87 of file switch_rtp.c.

Referenced by handle_ice(), and switch_rtp_activate_ice().

◆ WARN_SRTP_ERRS

#define WARN_SRTP_ERRS   10

Definition at line 78 of file switch_rtp.c.

Referenced by read_bundle_rtp_packet(), and read_rtp_packet().

◆ WRITE_DEC

#define WRITE_DEC (   rtp_session)    rtp_session->writing--; switch_mutex_unlock(rtp_session->write_mutex)

◆ WRITE_INC

#define WRITE_INC (   rtp_session)    switch_mutex_lock(rtp_session->write_mutex); rtp_session->writing++

Typedef Documentation

◆ dtls_bio_filter

◆ dtls_state_handler_t

typedef int(* dtls_state_handler_t) (switch_rtp_t *, switch_dtls_t *)

Definition at line 300 of file switch_rtp.c.

◆ packet_list_t

typedef struct packet_list_s packet_list_t

◆ rtp_hdr_t

typedef srtp_hdr_t rtp_hdr_t

Definition at line 97 of file switch_rtp.c.

◆ switch_dtls_t

typedef struct switch_dtls_s switch_dtls_t

◆ switch_rtcp_sdes_unit_t

◆ ts_normalize_t

Enumeration Type Documentation

◆ handle_rfc2833_result_t

Enumerator
RESULT_CONTINUE 
RESULT_GOTO_END 
RESULT_GOTO_RECVFROM 
RESULT_GOTO_TIMERCHECK 

Definition at line 534 of file switch_rtp.c.

◆ vad_talk_mask_t

Enumerator
VAD_FIRE_TALK 
VAD_FIRE_NOT_TALK 

Definition at line 193 of file switch_rtp.c.

193  {
194  VAD_FIRE_TALK = (1 << 0),
195  VAD_FIRE_NOT_TALK = (1 << 1)
vad_talk_mask_t
Definition: switch_rtp.c:193

Function Documentation

◆ BIO_dtls_filter()

BIO_METHOD* BIO_dtls_filter ( void  )

Definition at line 3475 of file switch_rtp.c.

Referenced by switch_rtp_add_dtls().

3475  {
3476 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3477  return(&dtls_bio_filter_methods);
3478 #else
3479  return(dtls_bio_filter_methods);
3480 #endif
3481 }
static BIO_METHOD dtls_bio_filter_methods
Definition: switch_rtp.c:3470

◆ burstr_calculate()

void burstr_calculate ( int  loss[],
int  received,
double *  burstr,
double *  lossr 
)

Definition at line 1625 of file switch_rtp.c.

References switch_rtcp_report_block_frame::lost, and LOST_BURST_ANALYZE.

Referenced by check_jitter().

1626 {
1627  int lost = 0;
1628  int bursts = 0;
1629  int i;
1630 
1631  for ( i = 0; i < LOST_BURST_ANALYZE; i++ ) {
1632  lost += i * loss[i];
1633  bursts += loss[i];
1634  }
1635  if (received > 0 && bursts > 0) {
1636  *burstr = (double)((double)lost / (double)bursts) / (double)(1.0 / ( 1.0 - (double)lost / (double)received ));
1637  if (*burstr < 0) {
1638  *burstr = - *burstr;
1639  }
1640  } else {
1641  *burstr = 0;
1642  }
1643  if (received > 0) {
1644  *lossr = (double)((double)lost / (double)received);
1645  } else {
1646  *lossr = 0;
1647  }
1648 }
#define LOST_BURST_ANALYZE
Definition: switch_types.h:252

◆ calc_bw_exp()

static void calc_bw_exp ( uint32_t  bps,
uint8_t  bits,
rtcp_tmmbx_t tmmbx 
)
static

Definition at line 2028 of file switch_rtp.c.

References rtcp_tmmbx_t::parts, and switch_assert.

Referenced by check_rtcp_and_ice().

2029 {
2030  uint32_t mantissa_max, i = 0;
2031  uint8_t exp = 0;
2032  uint32_t mantissa = 0;
2033  uint16_t overhead = 60;
2034 
2035  switch_assert(bits<=32);
2036 
2037  mantissa_max = (1 << bits) - 1;
2038 
2039  for (i = 0; i < 32; ++i) {
2040  if (bps <= (mantissa_max << i)) {
2041  exp = i;
2042  break;
2043  }
2044  }
2045 
2046  mantissa = (bps >> exp);
2047 
2048  tmmbx->parts[0] = (uint8_t) ((exp << 2) + ((mantissa >> 15) & 0x03));
2049  tmmbx->parts[1] = (uint8_t) (mantissa >> 7);
2050  tmmbx->parts[2] = (uint8_t) ((mantissa >> 1) + ((overhead >> 8) & 0x01));
2051  tmmbx->parts[3] = (uint8_t) (overhead);
2052 }
uint8_t parts[4]
Definition: switch_rtp.c:140
#define switch_assert(expr)

◆ calc_elapsed()

static void calc_elapsed ( switch_rtp_t rtp_session,
switch_rtp_ice_t ice 
)
static

Definition at line 827 of file switch_rtp.c.

References ADJ_TOO_LONG, DS_READY, switch_rtp::dtls, switch_rtp::elapsed_adj, switch_rtp::elapsed_media, switch_rtp::elapsed_stun, switch_rtp::first_stun, switch_rtp::last_adj, switch_rtp::last_media, switch_rtp_ice_t::last_ok, MEDIA_TOO_LONG, switch_dtls_s::state, and switch_micro_time_now().

Referenced by handle_ice().

828 {
829  switch_time_t ref_point;
830  switch_time_t now;
831 
832  now = switch_micro_time_now();
833 
834  if (ice->last_ok && (!rtp_session->dtls || rtp_session->dtls->state == DS_READY)) {
835  ref_point = ice->last_ok;
836  } else {
837  ref_point = rtp_session->first_stun;
838  }
839 
840  if (!ref_point) ref_point = now;
841 
842  rtp_session->elapsed_stun = (unsigned int) ((now - ref_point) / 1000);
843 
844  if (rtp_session->last_media) {
845  rtp_session->elapsed_media = (unsigned int) ((now - rtp_session->last_media) / 1000);
846  } else {
847  rtp_session->elapsed_media = MEDIA_TOO_LONG + 1;
848  }
849 
850  if (rtp_session->last_adj) {
851  rtp_session->elapsed_adj = (unsigned int) ((now - rtp_session->last_adj) / 1000);
852  } else {
853  rtp_session->elapsed_adj = ADJ_TOO_LONG + 1;
854  }
855 }
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
Definition: switch_time.c:311
uint32_t elapsed_adj
Definition: switch_rtp.c:485
switch_time_t last_ok
Definition: switch_rtp.c:264
uint32_t elapsed_stun
Definition: switch_rtp.c:483
#define ADJ_TOO_LONG
Definition: switch_rtp.c:825
switch_time_t last_media
Definition: switch_rtp.c:443
dtls_state_t state
Definition: switch_rtp.c:284
int64_t switch_time_t
Definition: switch_apr.h:188
uint32_t elapsed_media
Definition: switch_rtp.c:484
switch_dtls_t * dtls
Definition: switch_rtp.c:370
switch_time_t last_adj
Definition: switch_rtp.c:481
#define MEDIA_TOO_LONG
Definition: switch_rtp.c:823
switch_time_t first_stun
Definition: switch_rtp.c:407

◆ calc_local_lsr_now()

static uint32_t calc_local_lsr_now ( void  )
inlinestatic

Definition at line 1823 of file switch_rtp.c.

References NTP_TIME_OFFSET, and switch_micro_time_now().

Referenced by process_rtcp_report(), and rtcp_generate_report_block().

1824 {
1825  switch_time_t now;
1826  uint32_t ntp_sec, ntp_usec, lsr_now, sec;
1827  now = switch_micro_time_now();
1828  sec = (uint32_t)(now/1000000); /* convert to seconds */
1829  ntp_sec = sec+NTP_TIME_OFFSET; /* convert to NTP seconds */
1830  ntp_usec = (uint32_t)(now - ((switch_time_t) sec*1000000)); /* remove seconds to keep only the microseconds */
1831 
1832  lsr_now = (uint32_t)(ntp_usec*0.065536) | (ntp_sec&0x0000ffff)<<16; /* 0.065536 is used for convertion from useconds to fraction of 65536 (x65536/1000000) */
1833 
1834  return lsr_now;
1835 }
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
Definition: switch_time.c:311
#define NTP_TIME_OFFSET
Definition: switch_rtp.c:80
int64_t switch_time_t
Definition: switch_apr.h:188

◆ check_jitter()

static void check_jitter ( switch_rtp_t rtp_session)
static

Definition at line 1659 of file switch_rtp.c.

References switch_rtp::bad_stream, burstr_calculate(), switch_rtp_numbers_t::burstrate, switch_rtp::clean_stream, do_mos(), switch_rtp::dtmf_data, switch_rtp_numbers_t::error_log, switch_rtp::flags, error_period::flaws, switch_rtp_numbers_t::flaws, switch_rtp_rfc2833_data::in_digit_ts, switch_rtp_stats_t::inbound, IPDV_THRESHOLD, switch_rtp_numbers_t::jitter_add, switch_rtp_numbers_t::jitter_addsq, switch_rtp::jitter_lead, JITTER_LEAD_FRAMES, switch_rtp_numbers_t::jitter_n, switch_rtp_numbers_t::last_loss, switch_rtp_numbers_t::last_proc_time, switch_rtp_numbers_t::last_processed_seq, switch_rtp::last_rtp_hdr, switch_rtp_numbers_t::loss, switch_rtp_numbers_t::lossrate, switch_rtcp_report_block_frame::lost, LOST_BURST_CAPTURE, switch_rtp_numbers_t::max_variance, switch_rtp_numbers_t::mean_interval, switch_rtp_numbers_t::min_variance, error_period::next, switch_rtp::next_stat_check_time, switch_rtp::old_mean, switch_rtp::one_second, switch_rtp::recovering_stream, switch_rtp_numbers_t::recved, reset_jitter_seq(), rtp_session_name, rtp_type, switch_rtp::session, error_period::start, switch_rtp::stats, error_period::stop, SWITCH_CHANNEL_LOG, SWITCH_CHANNEL_SESSION_LOG, switch_core_session_alloc, switch_core_session_request_video_refresh, SWITCH_LOG_DEBUG1, SWITCH_LOG_DEBUG3, switch_log_printf(), switch_micro_time_now(), SWITCH_RTP_FLAG_DTMF_ON, SWITCH_RTP_FLAG_PAUSE, SWITCH_RTP_FLAG_VIDEO, and switch_rtp_numbers_t::variance.

Referenced by read_rtp_packet().

1660 {
1661  switch_time_t current_time;
1662  int64_t diff_time = 0, cur_diff = 0;
1663  int seq;
1664 
1665  current_time = switch_micro_time_now() / 1000;
1666 
1667  if (rtp_session->flags[SWITCH_RTP_FLAG_PAUSE] || rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] || rtp_session->dtmf_data.in_digit_ts) {
1668  reset_jitter_seq(rtp_session);
1669  return;
1670  }
1671 
1672  if (++rtp_session->jitter_lead < JITTER_LEAD_FRAMES || !rtp_session->stats.inbound.last_proc_time) {
1673  rtp_session->stats.inbound.last_proc_time = current_time;
1674  return;
1675  }
1676 
1677  diff_time = (current_time - rtp_session->stats.inbound.last_proc_time);
1678  seq = (int)(uint16_t) ntohs((uint16_t) rtp_session->last_rtp_hdr.seq);
1679 
1680  /* Burst and Packet Loss */
1681  rtp_session->stats.inbound.recved++;
1682 
1683  if (rtp_session->stats.inbound.last_processed_seq > 0 && seq > (int)(rtp_session->stats.inbound.last_processed_seq + 1)) {
1684  int lost = (seq - rtp_session->stats.inbound.last_processed_seq - 1);
1685 
1686  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "%s Got: %s seq %d but expected: %d lost: %d\n",
1687  rtp_session_name(rtp_session),
1688  rtp_type(rtp_session),
1689  seq,
1690  (rtp_session->stats.inbound.last_processed_seq + 1), lost);
1691  rtp_session->stats.inbound.last_loss++;
1692 
1693  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
1695  }
1696 
1697  if (rtp_session->stats.inbound.last_loss > 0 && rtp_session->stats.inbound.last_loss < LOST_BURST_CAPTURE) {
1698  rtp_session->stats.inbound.loss[rtp_session->stats.inbound.last_loss] += lost;
1699  }
1700 
1701  rtp_session->bad_stream++;
1702  rtp_session->stats.inbound.flaws += lost;
1703 
1704  if (rtp_session->stats.inbound.error_log) {
1705  rtp_session->stats.inbound.error_log->flaws += lost;
1706  }
1707 
1708  } else {
1709  rtp_session->stats.inbound.last_loss = 0;
1710  }
1711 
1712  rtp_session->stats.inbound.last_processed_seq = seq;
1713 
1714  /* Burst and Packet Loss */
1715 
1716  if (current_time > rtp_session->next_stat_check_time) {
1717  rtp_session->next_stat_check_time = current_time + 5000;
1718  burstr_calculate(rtp_session->stats.inbound.loss, rtp_session->stats.inbound.recved,
1719  &(rtp_session->stats.inbound.burstrate), &(rtp_session->stats.inbound.lossrate));
1720  do_mos(rtp_session);
1721  } else {
1722  do_mos(rtp_session);
1723  }
1724 
1725  if (rtp_session->stats.inbound.last_loss || rtp_session->bad_stream) {
1726  if (rtp_session->session && (!rtp_session->stats.inbound.error_log || rtp_session->stats.inbound.error_log->stop)) {
1727  struct error_period *error = switch_core_session_alloc(rtp_session->session, sizeof(*error));
1728  error->start = switch_micro_time_now();
1729  error->next = rtp_session->stats.inbound.error_log;
1730  rtp_session->stats.inbound.error_log = error;
1731  }
1732 
1733  if (!rtp_session->stats.inbound.last_loss) {
1734  if (++rtp_session->recovering_stream > (rtp_session->one_second * 3)) {
1735  if (rtp_session->session && rtp_session->stats.inbound.error_log) {
1736  rtp_session->stats.inbound.error_log->stop = switch_micro_time_now();
1737  }
1738 
1739  rtp_session->bad_stream = 0;
1740  }
1741  } else {
1742  rtp_session->recovering_stream = 0;
1743  rtp_session->bad_stream++;
1744  }
1745  } else {
1746  rtp_session->recovering_stream = 0;
1747  rtp_session->clean_stream++;
1748  }
1749 
1750 
1751  if ( diff_time < 0 ) {
1752  diff_time = -diff_time;
1753  }
1754 
1755  rtp_session->stats.inbound.jitter_n++;
1756  rtp_session->stats.inbound.jitter_add += diff_time;
1757 
1758  if (rtp_session->stats.inbound.mean_interval) {
1759  cur_diff = (int64_t)(diff_time - rtp_session->stats.inbound.mean_interval);
1760  } else {
1761  cur_diff = 0;
1762  }
1763 
1764  rtp_session->stats.inbound.jitter_addsq += (cur_diff * cur_diff);
1765  rtp_session->stats.inbound.last_proc_time = current_time;
1766 
1767  if (rtp_session->stats.inbound.jitter_n > 0) {
1768  double ipdv;
1769 
1770  rtp_session->stats.inbound.mean_interval = (double)rtp_session->stats.inbound.jitter_add / (double)rtp_session->stats.inbound.jitter_n;
1771 
1772  if (!rtp_session->old_mean) {
1773  rtp_session->old_mean = rtp_session->stats.inbound.mean_interval;
1774  }
1775 
1776  rtp_session->stats.inbound.variance = (double)rtp_session->stats.inbound.jitter_addsq / (double)rtp_session->stats.inbound.jitter_n;
1777 
1778  //printf("CHECK %d +%ld +%ld %f %f\n", rtp_session->write_timer.samplecount, diff_time, (diff_time * diff_time), rtp_session->stats.inbound.mean_interval, rtp_session->stats.inbound.variance);
1779 
1780  ipdv = rtp_session->old_mean - rtp_session->stats.inbound.mean_interval;
1781 
1782  if ( ipdv > IPDV_THRESHOLD ) { /* It shows Increasing Delays */
1783  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG3, "Calculated Instantaneous Packet Delay Variation: %s packet %lf\n",
1784  rtp_type(rtp_session), ipdv);
1785  }
1786 
1787  if ( rtp_session->stats.inbound.variance < rtp_session->stats.inbound.min_variance || rtp_session->stats.inbound.min_variance == 0 ) {
1788  rtp_session->stats.inbound.min_variance = rtp_session->stats.inbound.variance;
1789  }
1790 
1791  if ( rtp_session->stats.inbound.variance > rtp_session->stats.inbound.max_variance ) {
1792  rtp_session->stats.inbound.max_variance = rtp_session->stats.inbound.variance;
1793  }
1794 
1795  rtp_session->old_mean = rtp_session->stats.inbound.mean_interval;
1796  }
1797 }
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
Definition: switch_time.c:311
switch_size_t flaws
Definition: switch_types.h:723
int loss[LOST_BURST_CAPTURE]
Definition: switch_types.h:719
#define SWITCH_CHANNEL_SESSION_LOG(x)
switch_rtp_numbers_t inbound
Definition: switch_types.h:771
#define SWITCH_CHANNEL_LOG
#define IPDV_THRESHOLD
Definition: switch_types.h:250
struct error_period * next
Definition: switch_types.h:688
#define rtp_session_name(_rtp_session)
Definition: switch_rtp.c:85
uint32_t flaws
Definition: switch_types.h:686
struct switch_rtp_rfc2833_data dtmf_data
Definition: switch_rtp.c:426
#define rtp_type(rtp_session)
Definition: switch_rtp.c:544
#define LOST_BURST_CAPTURE
Definition: switch_types.h:254
Definition: cJSON.c:68
int64_t switch_time_t
Definition: switch_apr.h:188
int64_t stop
Definition: switch_types.h:685
#define JITTER_LEAD_FRAMES
Definition: switch_rtp.c:66
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:396
#define switch_core_session_request_video_refresh(_s)
Definition: switch_core.h:2892
uint32_t clean_stream
Definition: switch_rtp.c:456
uint32_t recovering_stream
Definition: switch_rtp.c:458
int64_t start
Definition: switch_types.h:684
switch_rtp_stats_t stats
Definition: switch_rtp.c:454
struct error_period * error_log
Definition: switch_types.h:727
switch_time_t next_stat_check_time
Definition: switch_rtp.c:419
static void reset_jitter_seq(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:1650
switch_core_session_t * session
Definition: switch_rtp.c:476
void burstr_calculate(int loss[], int received, double *burstr, double *lossr)
Definition: switch_rtp.c:1625
#define switch_core_session_alloc(_session, _memory)
Allocate memory from a session&#39;s pool.
Definition: switch_core.h:696
uint32_t jitter_lead
Definition: switch_rtp.c:417
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
double old_mean
Definition: switch_rtp.c:418
uint32_t bad_stream
Definition: switch_rtp.c:457
rtp_hdr_t last_rtp_hdr
Definition: switch_rtp.c:373
static void do_mos(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:1575
uint32_t one_second
Definition: switch_rtp.c:415

◆ check_recv_payload()

static int check_recv_payload ( switch_rtp_t rtp_session)
static

Definition at line 5802 of file switch_rtp.c.

References payload_map_s::allocated, switch_rtp::flag_mutex, switch_rtp::last_rtp_hdr, payload_map_s::negotiated, payload_map_s::next, switch_rtp::pmaps, payload_map_s::pt, RTP_BUG_ACCEPT_ANY_PAYLOAD, switch_rtp::rtp_bugs, switch_mutex_lock(), and switch_mutex_unlock().

Referenced by read_rtp_packet(), and rtp_common_read().

5803 {
5804  int ok = 1;
5805 
5806  if (!(rtp_session->rtp_bugs & RTP_BUG_ACCEPT_ANY_PAYLOAD) && rtp_session->pmaps && *rtp_session->pmaps) {
5807  payload_map_t *pmap;
5808  ok = 0;
5809 
5810  switch_mutex_lock(rtp_session->flag_mutex);
5811 
5812  for (pmap = *rtp_session->pmaps; pmap && pmap->allocated; pmap = pmap->next) {
5813  if (!pmap->negotiated) {
5814  continue;
5815  }
5816 
5817  if (rtp_session->last_rtp_hdr.pt == pmap->pt) {
5818  ok = 1;
5819  }
5820  }
5821  switch_mutex_unlock(rtp_session->flag_mutex);
5822  }
5823 
5824  return ok;
5825 }
uint8_t allocated
switch_mutex_t * flag_mutex
Definition: switch_rtp.c:430
struct payload_map_s * next
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
uint8_t negotiated
switch_payload_t pt
payload_map_t ** pmaps
Definition: switch_rtp.c:477
switch_rtp_bug_flag_t rtp_bugs
Definition: switch_rtp.c:453
rtp_hdr_t last_rtp_hdr
Definition: switch_rtp.c:373

◆ check_rtcp_and_ice()

static int check_rtcp_and_ice ( switch_rtp_t rtp_session)
static

Definition at line 2137 of file switch_rtp.c.

References _RTCP_PSFB_FIR, _RTCP_PSFB_PLI, _RTCP_PT_FIR, _RTCP_PT_PSFB, _RTCP_PT_RR, _RTCP_PT_RTPFB, _RTCP_PT_SDES, _RTCP_PT_SR, _RTCP_RTPFB_NACK, _RTCP_RTPFB_TMMBN, _RTCP_RTPFB_TMMBR, _RTCP_SDES_CNAME, _RTCP_SDES_NOTE, rtcp_msg_t::body, calc_bw_exp(), switch_rtp::cn, switch_rtp::cng_pt, switch_rtcp_hdr_s::count, switch_rtcp_numbers_t::cycle, FALSE, switch_rtp::fir_count, switch_rtcp_video_counters_t::fir_count, switch_rtp::fir_seq, switch_rtp::flags, switch_rtcp_ext_hdr_t::fmt, get_next_write_ts(), rtp_msg_t::header, rtcp_msg_t::header, sdes_ssrc_t::header, switch_rtcp_numbers_t::high_ext_seq_recv, switch_rtp::ice, switch_rtp::ice_mutex, ice_out(), switch_rtp_ice_t::ice_user, switch_rtp_stats_t::inbound, INVALID_PT, switch_rtcp_numbers_t::last_rpt_cycle, switch_rtcp_numbers_t::last_rpt_ext_seq, switch_rtcp_numbers_t::last_rpt_ts, switch_rtp::last_write_samplecount, switch_rtcp_sdes_unit_s::length, switch_rtcp_ext_hdr_t::length, switch_rtcp_hdr_s::length, MAX_NACK, switch_rtcp_video_counters_t::nack_count, switch_rtcp_ext_hdr_t::p, switch_rtcp_hdr_s::p, switch_rtp_numbers_t::period_packet_count, switch_rtcp_numbers_t::period_pkt_count, switch_rtp::pli_count, switch_rtcp_video_counters_t::pli_count, switch_rtp::prev_nacks_inflight, switch_rtcp_ext_hdr_t::pt, rtcp_fir_t::r1, rtcp_fir_t::r2, rtcp_fir_t::r3, switch_rtcp_ext_hdr_t::recv_ssrc, switch_rtp::remote_ssrc, switch_rtcp_sender_report::report_block, switch_rtcp_receiver_report::report_block, switch_rtp_ice_t::rready, switch_rtp_stats_t::rtcp, rtcp_generate_report_block(), rtcp_generate_sender_info(), switch_rtp::rtcp_ice, switch_rtp::rtcp_interval, switch_rtp::rtcp_last_sent, switch_rtp::rtcp_remote_addr, switch_rtp::rtcp_send_msg, switch_rtp::rtcp_sent_packets, switch_rtp::rtcp_sock_output, switch_rtp::rtcp_vstats, rtp_session_name, rtp_type, switch_timer::samplecount, switch_rtp::samples_per_interval, switch_rtp::send_ctx, switch_rtp::send_msg, switch_rtp::send_rr, switch_rtcp_ext_hdr_t::send_ssrc, switch_rtcp_sender_report::sender_info, switch_rtcp_numbers_t::sent_pkt_count, rtcp_fir_t::seq, switch_rtp::session, SFF_NONE, switch_rtp::srtp_idx_rtcp, rtcp_fir_t::ssrc, rtcp_tmmbx_t::ssrc, sdes_ssrc_t::ssrc, switch_rtp::ssrc, switch_rtcp_sender_report::ssrc, switch_rtcp_receiver_report::ssrc, switch_rtp::stats, SWITCH_CHANNEL_SESSION_LOG, switch_core_session_get_name, switch_core_session_media_flow(), SWITCH_CRYPTO_MKI_INDEX, SWITCH_FALSE, switch_get_addr(), switch_jb_pop_nack(), SWITCH_LOG_CRIT, SWITCH_LOG_DEBUG, SWITCH_LOG_DEBUG1, SWITCH_LOG_DEBUG2, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_MEDIA_FLOW_RECVONLY, SWITCH_MEDIA_TYPE_AUDIO, SWITCH_MEDIA_TYPE_VIDEO, switch_micro_time_now(), switch_mutex_lock(), switch_mutex_unlock(), SWITCH_RTP_FLAG_AUDIO_FIRE_SEND_RTCP_EVENT, SWITCH_RTP_FLAG_AUTO_CNG, SWITCH_RTP_FLAG_ENABLE_RTCP, SWITCH_RTP_FLAG_NACK, SWITCH_RTP_FLAG_OLD_FIR, SWITCH_RTP_FLAG_RTCP_MUX, SWITCH_RTP_FLAG_RTCP_PASSTHRU, SWITCH_RTP_FLAG_SECURE_SEND, SWITCH_RTP_FLAG_SECURE_SEND_MKI, SWITCH_RTP_FLAG_UDPTL, SWITCH_RTP_FLAG_USE_TIMER, SWITCH_RTP_FLAG_VIDEO, SWITCH_RTP_FLAG_VIDEO_FIRE_SEND_RTCP_EVENT, switch_rtp_test_flag(), switch_rtp_write_manual(), switch_send_rtcp_event(), switch_sockaddr_get_port(), switch_socket_sendto(), SWITCH_STATUS_GENERR, SWITCH_STATUS_SUCCESS, switch_rtp::tmmbn, switch_rtp::tmmbr, TRUE, switch_rtp::ts, switch_rtcp_sdes_unit_s::type, switch_rtcp_hdr_s::type, using_ice(), switch_rtcp_sdes_unit_s::value, switch_rtp::vb, switch_rtcp_ext_hdr_t::version, switch_rtcp_hdr_s::version, switch_rtcp_video_stats_t::video_in, and switch_rtp::write_timer.

Referenced by read_rtp_packet(), rtp_common_read(), and switch_rtp_ping().

2138 {
2139  int ret = 0;
2140  int rtcp_ok = 0, rtcp_cyclic = 0, rtcp_fb = 0, force_send_rr = 0;
2142  int rate = 0, nack_ttl = 0, nack_dup = 0;
2143  uint32_t cur_nack[MAX_NACK] = { 0 };
2144  uint16_t seq = 0;
2145 
2146  if (!rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] &&
2147  rtp_session->flags[SWITCH_RTP_FLAG_AUTO_CNG] &&
2148  rtp_session->send_msg.header.ts &&
2149  rtp_session->cng_pt != INVALID_PT &&
2150  (rtp_session->write_timer.samplecount - rtp_session->last_write_samplecount >= rtp_session->samples_per_interval * 60)) {
2151  uint8_t data[10] = { 0 };
2152  switch_frame_flag_t frame_flags = SFF_NONE;
2153  data[0] = 65;
2154  rtp_session->cn++;
2155 
2156  get_next_write_ts(rtp_session, 0);
2157  rtp_session->send_msg.header.ts = htonl(rtp_session->ts);
2158 
2159  switch_rtp_write_manual(rtp_session, (void *) data, 2, 0, rtp_session->cng_pt, ntohl(rtp_session->send_msg.header.ts), &frame_flags);
2160 
2161  if (switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER)) {
2162  rtp_session->last_write_samplecount = rtp_session->write_timer.samplecount;
2163  }
2164  }
2165 
2166  rate = rtp_session->rtcp_interval;
2167 
2168  if (rtp_session->flags[SWITCH_RTP_FLAG_NACK] && rtp_session->vb) {
2169  int n;
2170  for (n = 0; n < MAX_NACK; n++) {
2171  uint32_t nack = switch_jb_pop_nack(rtp_session->vb);
2172 
2173  if (!nack) break;
2174 
2175  seq = ntohs(nack & 0xFFFF);
2176 
2177  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "%s Got NACK [%u][0x%x] for seq %u\n",
2178  switch_core_session_get_name(rtp_session->session), nack, nack, seq);
2179 
2180  cur_nack[nack_ttl++] = nack;
2181  }
2182  if (nack_ttl) {
2183  rtcp_ok = 1;
2184  rtcp_fb = 1;
2185  }
2186  }
2187 
2188 
2189 
2190  if (rtp_session->rtcp_sent_packets < 4) {
2191  rate = 4000;
2192  } else {
2193  if (rtp_session->pli_count || rtp_session->fir_count || rtp_session->tmmbr || rtp_session->tmmbn) {
2194  //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "MARK BW/FIR ETC %d %d\n", rtp_session->pli_count, rtp_session->fir_count);
2195  rtcp_ok = 1;
2196  rtcp_fb = 1;
2197  }
2198  }
2199 
2200  if (rtp_session->send_rr) {
2201  rtp_session->send_rr = 0;
2202  rtcp_ok = 1;
2203  force_send_rr = 1;
2204  }
2205 
2206  //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "TIME CHECK %d > %d\n", (int)((now - rtp_session->rtcp_last_sent) / 1000), rate);
2207 
2208  if (!rtcp_ok && (!rtp_session->rtcp_last_sent || (int)((now - rtp_session->rtcp_last_sent) / 1000) > rate)) {
2209  //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "TIME UP\n");
2210  rtcp_cyclic = 1;
2211  rtcp_ok = 1;
2212  }
2213 
2214  if (rtcp_ok && using_ice(rtp_session)) {
2215  if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
2216  if (!rtp_session->ice.rready) {
2217  rtcp_ok = 0;
2218  }
2219  } else {
2220  if (!rtp_session->rtcp_ice.rready) {
2221  rtcp_ok = 0;
2222  }
2223  }
2224  }
2225 
2226  //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "WTF %d %d %d %d\n", rate, rtp_session->rtcp_sent_packets, rtcp_ok, nack_ttl);
2227 
2228  if (rtp_session->rtcp_sock_output && rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] && !rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU] && rtcp_ok) {
2229  switch_rtcp_numbers_t * stats = &rtp_session->stats.rtcp;
2230  struct switch_rtcp_receiver_report *rr;
2231  struct switch_rtcp_sender_report *sr;
2232  struct switch_rtcp_report_block *rtcp_report_block = NULL;
2233  switch_size_t rtcp_bytes = sizeof(struct switch_rtcp_hdr_s)+sizeof(uint32_t); /* add size of the packet header and the ssrc */
2234  switch_rtcp_hdr_t *sdes;
2235  sdes_ssrc_t *sdes_ssrc;
2236  uint8_t *p;
2237  switch_size_t sdes_bytes = sizeof(struct switch_rtcp_hdr_s);
2239  switch_bool_t is_only_receiver = FALSE;
2240 
2241  if (!rtcp_fb) {
2242  rtp_session->rtcp_last_sent = now;
2243  rtp_session->rtcp_sent_packets++;
2244  }
2245 
2246  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] &&
2247  rtp_session->vb && rtcp_cyclic) {
2248  nack_dup = rtp_session->prev_nacks_inflight;
2249  rtp_session->prev_nacks_inflight = 0;
2250  }
2251 
2252  rtp_session->rtcp_send_msg.header.version = 2;
2253  rtp_session->rtcp_send_msg.header.p = 0;
2254 
2257  is_only_receiver = TRUE;
2258  }
2259  if (!rtp_session->stats.rtcp.sent_pkt_count || is_only_receiver || force_send_rr) {
2260  rtp_session->rtcp_send_msg.header.type = _RTCP_PT_RR; /* Receiver report */
2261  rr=(struct switch_rtcp_receiver_report*) rtp_session->rtcp_send_msg.body;
2262  rr->ssrc = htonl(rtp_session->ssrc);
2263  rtcp_report_block = &rr->report_block;
2264  rtcp_bytes += sizeof(struct switch_rtcp_report_block);
2265  rtcp_generate_report_block(rtp_session, rtcp_report_block, nack_dup);
2266  rtp_session->rtcp_send_msg.header.count = 1; /* reception report block count */
2267  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "Sending RTCP RR (ssrc=%u)\n", rtp_session->ssrc);
2268  } else {
2269  struct switch_rtcp_sender_info *rtcp_sender_info;
2270  rtp_session->rtcp_send_msg.header.type = _RTCP_PT_SR; /* Sender report */
2271  sr = (struct switch_rtcp_sender_report*) rtp_session->rtcp_send_msg.body;
2272  sr->ssrc = htonl(rtp_session->ssrc);
2273  rtcp_sender_info = &sr->sender_info;
2274  rtcp_generate_sender_info(rtp_session, rtcp_sender_info);
2275  rtcp_bytes += sizeof(struct switch_rtcp_sender_info);
2276  if (!rtcp_cyclic && rtcp_fb) {
2277  /* rtcp-fb only, don't send receive report block */
2278  rtp_session->rtcp_send_msg.header.count = 0;
2279  } else {
2280  rtcp_report_block = &sr->report_block;
2281  rtcp_bytes += sizeof(struct switch_rtcp_report_block);
2282  rtcp_generate_report_block(rtp_session, rtcp_report_block, nack_dup);
2283  rtp_session->rtcp_send_msg.header.count = 1; /* reception report block count */
2284  stats->sent_pkt_count = 0;
2285  if ((!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->flags[SWITCH_RTP_FLAG_AUDIO_FIRE_SEND_RTCP_EVENT]) ||
2287  switch_send_rtcp_event(rtp_session, sr, rtcp_report_block);
2288  }
2289  }
2290  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "Sending RTCP SR (ssrc=%u)\n", rtp_session->ssrc);
2291  }
2292 
2293  rtp_session->rtcp_send_msg.header.length = htons((uint16_t)(rtcp_bytes / 4) - 1);
2294 
2295  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
2296  if (rtp_session->pli_count) {
2297  switch_rtcp_ext_hdr_t *ext_hdr;
2298 
2299  p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes;
2300  ext_hdr = (switch_rtcp_ext_hdr_t *) p;
2301 
2302  ext_hdr->version = 2;
2303  ext_hdr->p = 0;
2304  ext_hdr->fmt = _RTCP_PSFB_PLI;
2305  ext_hdr->pt = _RTCP_PT_PSFB;
2306 
2307  ext_hdr->send_ssrc = htonl(rtp_session->ssrc);
2308  ext_hdr->recv_ssrc = htonl(rtp_session->remote_ssrc);
2309  rtp_session->rtcp_vstats.video_in.pli_count++;
2310  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "Sending RTCP PLI %u %u [%u]\n",
2311  rtp_session->ssrc, rtp_session->remote_ssrc, rtp_session->rtcp_vstats.video_in.pli_count);
2312 
2313  ext_hdr->length = htons((uint8_t)(sizeof(switch_rtcp_ext_hdr_t) / 4) - 1);
2314  rtcp_bytes += sizeof(switch_rtcp_ext_hdr_t);
2315  rtp_session->pli_count = 0;
2316  }
2317 
2318  if (rtp_session->flags[SWITCH_RTP_FLAG_NACK] && nack_ttl > 0) {
2319  int n = 0;
2320 
2321  rtp_session->rtcp_vstats.video_in.nack_count++;
2322  for (n = 0; n < nack_ttl; n++) {
2323  switch_rtcp_ext_hdr_t *ext_hdr;
2324  uint32_t *nack;
2325  p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes;
2326  ext_hdr = (switch_rtcp_ext_hdr_t *) p;
2327 
2328  ext_hdr->version = 2;
2329  ext_hdr->p = 0;
2330  ext_hdr->fmt = _RTCP_RTPFB_NACK;
2331  ext_hdr->pt = _RTCP_PT_RTPFB;
2332  ext_hdr->send_ssrc = htonl(rtp_session->ssrc);
2333  ext_hdr->recv_ssrc = htonl(rtp_session->remote_ssrc);
2334  ext_hdr->length = htons(3);
2335  p += sizeof(switch_rtcp_ext_hdr_t);
2336  nack = (uint32_t *) p;
2337  *nack = cur_nack[n];
2338 
2339  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "Sending RTCP NACK %u [%d]\n",
2340  ntohs(*nack & 0xFFFF), rtp_session->rtcp_vstats.video_in.nack_count);
2341 
2342  rtcp_bytes += sizeof(switch_rtcp_ext_hdr_t) + sizeof(cur_nack[n]);
2343  cur_nack[n] = 0;
2344  }
2345  rtp_session->prev_nacks_inflight = n;
2346  }
2347 
2348  if (rtp_session->fir_count) {
2349  switch_rtcp_ext_hdr_t *ext_hdr;
2350  rtcp_fir_t *fir;
2351 
2352  if (switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_OLD_FIR)) {
2353  p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes;
2354  ext_hdr = (switch_rtcp_ext_hdr_t *) p;
2355 
2356  ext_hdr->version = 2;
2357  ext_hdr->pt = _RTCP_PT_FIR;
2358  rtcp_bytes += sizeof(switch_rtcp_ext_hdr_t);
2359  }
2360 
2361 
2362  p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes;
2363  ext_hdr = (switch_rtcp_ext_hdr_t *) p;
2364 
2365  p += sizeof(switch_rtcp_ext_hdr_t);
2366  fir = (rtcp_fir_t *) p;
2367 
2368  ext_hdr->version = 2;
2369  ext_hdr->p = 0;
2370  ext_hdr->fmt = _RTCP_PSFB_FIR;
2371  ext_hdr->pt = _RTCP_PT_PSFB;
2372 
2373  ext_hdr->send_ssrc = htonl(rtp_session->ssrc);
2374  ext_hdr->recv_ssrc = htonl(rtp_session->remote_ssrc);
2375 
2376  fir->ssrc = htonl(rtp_session->remote_ssrc);
2377  fir->seq = rtp_session->fir_seq;
2378  fir->r1 = fir->r2 = fir->r3 = 0;
2379 
2380  rtp_session->rtcp_vstats.video_in.fir_count++;
2381  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "Sending RTCP FIR SEQ %d [%u]\n", rtp_session->fir_seq, rtp_session->rtcp_vstats.video_in.fir_count);
2382 
2383  rtp_session->fir_seq++;
2384 
2385  ext_hdr->length = htons((uint8_t)((sizeof(switch_rtcp_ext_hdr_t) + sizeof(rtcp_fir_t)) / 4) - 1);
2386  rtcp_bytes += sizeof(switch_rtcp_ext_hdr_t) + sizeof(rtcp_fir_t);
2387  rtp_session->fir_count = 0;
2388  }
2389 
2390  //if (!rtp_session->tmmbr && rtp_session->cur_tmmbr) {
2391  // rtp_session->tmmbr = rtp_session->cur_tmmbr;
2392  //}
2393 
2394  while (rtp_session->tmmbr || rtp_session->tmmbn) {
2395  switch_rtcp_ext_hdr_t *ext_hdr;
2396  rtcp_tmmbx_t *tmmbx;
2397  uint32_t bps = 0;
2398  p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes;
2399  ext_hdr = (switch_rtcp_ext_hdr_t *) p;
2400 
2401  p += sizeof(switch_rtcp_ext_hdr_t);
2402  tmmbx = (rtcp_tmmbx_t *) p;
2403 
2404  ext_hdr->version = 2;
2405  ext_hdr->p = 0;
2406  ext_hdr->pt = _RTCP_PT_RTPFB;
2407  ext_hdr->send_ssrc = htonl(rtp_session->ssrc);
2408  ext_hdr->recv_ssrc = 0;
2409 
2410  if (rtp_session->tmmbr) {
2411  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "Sending RTCP TMMBR %u\n", rtp_session->tmmbr);
2412  ext_hdr->fmt = _RTCP_RTPFB_TMMBR;
2413  bps = rtp_session->tmmbr;
2414  rtp_session->tmmbr = 0;
2415  } else {
2416  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "Sending RTCP TMMBN %u\n", rtp_session->tmmbr);
2417  ext_hdr->fmt = _RTCP_RTPFB_TMMBN;
2418  bps = rtp_session->tmmbn;
2419  rtp_session->tmmbn = 0;
2420  }
2421 
2422  tmmbx->ssrc = htonl(rtp_session->remote_ssrc);
2423  calc_bw_exp(bps, 17, tmmbx);
2424 
2425  ext_hdr->length = htons((uint8_t)((sizeof(switch_rtcp_ext_hdr_t) + sizeof(rtcp_tmmbx_t)) / 4) - 1);
2426  rtcp_bytes += sizeof(switch_rtcp_ext_hdr_t) + sizeof(rtcp_tmmbx_t);
2427  }
2428 
2429  }
2430 
2431  //SDES + CNAME
2432  p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes;
2433  sdes_ssrc = (sdes_ssrc_t *) p;
2434  sdes = &sdes_ssrc->header;
2435  sdes->version = 2;
2436  sdes->type = _RTCP_PT_SDES;
2437  sdes->count = 1;
2438  sdes->p = 0;
2439  sdes_ssrc->ssrc = htonl(rtp_session->ssrc);
2440  sdes_bytes += sizeof(uint32_t);
2441 
2442 
2443  p = (uint8_t *) (sdes) + sdes_bytes;
2444  unit = (switch_rtcp_sdes_unit_t *) p;
2445  unit->type = _RTCP_SDES_CNAME;
2446  snprintf((char *)unit->value, 80, "%x", rtp_session->ssrc);
2447  unit->length = strlen((char *)unit->value);
2448  sdes_bytes += sizeof(switch_rtcp_sdes_unit_t) + unit->length;
2449 
2450 
2451  p += sizeof(switch_rtcp_sdes_unit_t) + unit->length;
2452  unit = (switch_rtcp_sdes_unit_t *) p;
2453  unit->type = _RTCP_SDES_NOTE;
2454  snprintf((char *)unit->value, 80, "FreeSWITCH.org -- Come to ClueCon.com");
2455  unit->length = strlen((char *)unit->value);
2456  sdes_bytes += sizeof(switch_rtcp_sdes_unit_t) + unit->length;
2457 
2458  sdes_bytes ++;//END
2459 
2460  sdes_bytes += 4 - (sdes_bytes % 4);
2461 
2462  sdes->length = htons((uint16_t)(sdes_bytes / 4) - 1);
2463  rtcp_bytes += sdes_bytes;
2464 
2465  /* Prepare next report */
2466  if (rtp_session->rtcp_send_msg.header.count) {
2467  stats->last_rpt_cycle = stats->cycle;
2468  stats->last_rpt_ext_seq = stats->high_ext_seq_recv;
2469  stats->last_rpt_ts = rtp_session->write_timer.samplecount;
2470  stats->period_pkt_count = 0;
2471  }
2472 
2473 
2474 
2475 #ifdef ENABLE_SRTP
2476  switch_mutex_lock(rtp_session->ice_mutex);
2477  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND]) {
2478  int stat = 0;
2479  int sbytes = (int) rtcp_bytes;
2480 
2481  if (!rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_MKI]) {
2482  stat = srtp_protect_rtcp(rtp_session->send_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_send_msg.header, &sbytes);
2483  } else {
2484  stat = srtp_protect_rtcp_mki(rtp_session->send_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_send_msg.header, &sbytes, 1, SWITCH_CRYPTO_MKI_INDEX);
2485  }
2486 
2487  if (stat) {
2488  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error: SRTP RTCP protection failed with code %d\n", stat);
2489  switch_mutex_unlock(rtp_session->ice_mutex);
2490  goto end;
2491  } else {
2492  rtcp_bytes = sbytes;
2493  }
2494  }
2495  switch_mutex_unlock(rtp_session->ice_mutex);
2496 #endif
2497 
2498  //#define DEBUG_EXTRA
2499 #ifdef DEBUG_EXTRA
2500  {
2501  const char *old_host;
2502  char bufb[50];
2503  old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->rtcp_remote_addr);
2504  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_CRIT, "%s SEND %s RTCP %s:%d %ld\n",
2505  rtp_session_name(rtp_session),
2506  rtp_type(rtp_session),
2507  old_host,
2509  rtcp_bytes);
2510  }
2511 #endif
2512  if (switch_socket_sendto(rtp_session->rtcp_sock_output, rtp_session->rtcp_remote_addr, 0, (void *)&rtp_session->rtcp_send_msg, &rtcp_bytes ) != SWITCH_STATUS_SUCCESS) {
2513  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG,"RTCP packet not written\n");
2514  } else {
2515  rtp_session->stats.inbound.period_packet_count = 0;
2516  }
2517  }
2518 
2519  if (rtp_session->ice.ice_user) {
2520  if (ice_out(rtp_session, &rtp_session->ice, SWITCH_FALSE) == SWITCH_STATUS_GENERR) {
2521  ret = -1;
2522  goto end;
2523  }
2524  }
2525 
2526  if (!rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
2527  if (rtp_session->rtcp_ice.ice_user) {
2528  if (ice_out(rtp_session, &rtp_session->rtcp_ice, SWITCH_FALSE) == SWITCH_STATUS_GENERR) {
2529  ret = -1;
2530  goto end;
2531  }
2532  }
2533  }
2534 
2535  end:
2536 
2537  return ret;
2538 }
switch_time_t rtcp_last_sent
Definition: switch_rtp.c:412
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
Definition: switch_time.c:311
switch_size_t period_packet_count
Definition: switch_types.h:696
#define SWITCH_CHANNEL_SESSION_LOG(x)
uint16_t switch_sockaddr_get_port(switch_sockaddr_t *sa)
Definition: switch_apr.c:1001
switch_rtp_numbers_t inbound
Definition: switch_types.h:771
static void rtcp_generate_report_block(switch_rtp_t *rtp_session, struct switch_rtcp_report_block *rtcp_report_block, int16_t extra_expected)
Definition: switch_rtp.c:1839
static void rtcp_generate_sender_info(switch_rtp_t *rtp_session, struct switch_rtcp_sender_info *sr)
Definition: switch_rtp.c:1799
struct switch_rtcp_report_block report_block
Definition: switch_rtp.c:531
switch_bool_t
Definition: switch_types.h:441
uint8_t r1
Definition: switch_rtp.c:123
int srtp_idx_rtcp
Definition: switch_rtp.c:368
uint8_t r3
Definition: switch_rtp.c:125
#define SWITCH_CRYPTO_MKI_INDEX
uint32_t prev_nacks_inflight
Definition: switch_rtp.c:493
uint8_t fir_seq
Definition: switch_rtp.c:336
#define rtp_session_name(_rtp_session)
Definition: switch_rtp.c:85
unsigned char type
Definition: switch_rtp.c:133
#define switch_core_session_get_name(_s)
Definition: switch_core.h:265
switch_rtcp_hdr_t header
Definition: switch_rtp.c:184
uint32_t ssrc
Definition: switch_rtp.c:190
uint16_t pli_count
Definition: switch_rtp.c:338
#define rtp_type(rtp_session)
Definition: switch_rtp.c:544
switch_payload_t cng_pt
Definition: switch_rtp.c:429
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
srtp_ctx_t * send_ctx[2]
Definition: switch_rtp.c:357
int64_t switch_time_t
Definition: switch_apr.h:188
switch_rtcp_numbers_t rtcp
Definition: switch_types.h:773
static void switch_send_rtcp_event(switch_rtp_t *rtp_session, struct switch_rtcp_sender_report *sr, struct switch_rtcp_report_block *rtcp_report_block)
Definition: switch_rtp.c:2063
uint32_t tmmbn
Definition: switch_rtp.c:341
struct switch_rtcp_sender_info sender_info
Definition: switch_rtp.c:525
struct switch_rtcp_sdes_unit_s switch_rtcp_sdes_unit_t
switch_status_t switch_socket_sendto(switch_socket_t *sock, switch_sockaddr_t *where, int32_t flags, const char *buf, switch_size_t *len)
Definition: switch_apr.c:796
uint32_t ts
Definition: switch_rtp.c:384
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:396
uint8_t seq
Definition: switch_rtp.c:122
uint16_t fir_count
Definition: switch_rtp.c:337
rtcp_msg_t rtcp_send_msg
Definition: switch_rtp.c:332
uintptr_t switch_size_t
switch_rtp_ice_t rtcp_ice
Definition: switch_rtp.c:402
switch_jb_t * vb
Definition: switch_rtp.c:439
uint8_t r2
Definition: switch_rtp.c:124
uint32_t switch_frame_flag_t
switch_rtp_stats_t stats
Definition: switch_rtp.c:454
switch_media_flow_t switch_core_session_media_flow(switch_core_session_t *session, switch_media_type_t type)
switch_socket_t * rtcp_sock_output
Definition: switch_rtp.c:326
uint8_t cn
Definition: switch_rtp.c:437
int rtcp_interval
Definition: switch_rtp.c:462
uint8_t send_rr
Definition: switch_rtp.c:335
static int using_ice(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:2054
uint32_t samples_per_interval
Definition: switch_rtp.c:409
srtp_hdr_t header
Definition: switch_rtp.c:112
switch_rtcp_video_counters_t video_in
Definition: switch_types.h:765
switch_mutex_t * ice_mutex
Definition: switch_rtp.c:433
#define FALSE
switch_core_session_t * session
Definition: switch_rtp.c:476
switch_timer_t write_timer
Definition: switch_rtp.c:435
uint32_t switch_jb_pop_nack(switch_jb_t *jb)
uint32_t remote_ssrc
Definition: switch_rtp.c:377
switch_rtp_ice_t ice
Definition: switch_rtp.c:401
struct switch_rtcp_report_block report_block
Definition: switch_rtp.c:526
switch_rtcp_video_stats_t rtcp_vstats
Definition: switch_rtp.c:455
unsigned char length
Definition: switch_rtp.c:134
uint32_t ssrc
Definition: switch_rtp.c:376
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
uint32_t switch_rtp_test_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flags)
Test an RTP Flag.
Definition: switch_rtp.c:5429
char body[SWITCH_RTCP_MAX_BUF_LEN]
Definition: switch_rtp.c:185
uint32_t last_write_samplecount
Definition: switch_rtp.c:390
const char * switch_get_addr(char *buf, switch_size_t len, switch_sockaddr_t *in)
Definition: switch_apr.c:979
#define TRUE
rtp_msg_t send_msg
Definition: switch_rtp.c:331
static const switch_payload_t INVALID_PT
Definition: switch_rtp.c:81
switch_sockaddr_t * rtcp_remote_addr
Definition: switch_rtp.c:344
static switch_status_t ice_out(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice, switch_bool_t force)
Definition: switch_rtp.c:857
uint32_t ssrc
Definition: switch_rtp.c:121
#define MAX_NACK
Definition: switch_rtp.c:2136
static void calc_bw_exp(uint32_t bps, uint8_t bits, rtcp_tmmbx_t *tmmbx)
Definition: switch_rtp.c:2028
uint32_t ssrc
Definition: switch_rtp.c:139
static uint8_t get_next_write_ts(switch_rtp_t *rtp_session, uint32_t timestamp)
Definition: switch_rtp.c:1547
int rtcp_sent_packets
Definition: switch_rtp.c:463
uint32_t tmmbr
Definition: switch_rtp.c:340
int switch_rtp_write_manual(switch_rtp_t *rtp_session, void *data, uint32_t datalen, uint8_t m, switch_payload_t payload, uint32_t ts, switch_frame_flag_t *flags)
Write data with a specified payload and sequence number to a given RTP session.
Definition: switch_rtp.c:9110
switch_rtcp_hdr_t header
Definition: switch_rtp.c:189

◆ check_timeout()

static void check_timeout ( switch_rtp_t rtp_session)
static

Definition at line 7282 of file switch_rtp.c.

References switch_rtp::last_media, switch_rtp::media_timeout, rtp_type, switch_rtp::session, SWITCH_CAUSE_MEDIA_TIMEOUT, switch_channel_execute_on(), switch_channel_hangup, SWITCH_CHANNEL_SESSION_LOG, switch_core_session_get_channel(), switch_core_session_get_name, SWITCH_LOG_DEBUG10, switch_log_printf(), and switch_micro_time_now().

Referenced by rtp_common_read().

7283 {
7284 
7286  uint32_t elapsed = 0;
7287 
7288  if (now >= rtp_session->last_media) {
7289  elapsed = (now - rtp_session->last_media) / 1000;
7290  }
7291 
7293  "%s MEDIA TIMEOUT %s %d/%d\n", switch_core_session_get_name(rtp_session->session), rtp_type(rtp_session),
7294  elapsed, rtp_session->media_timeout);
7295 
7296  if (elapsed > rtp_session->media_timeout) {
7297  switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
7298 
7299  switch_channel_execute_on(channel, "execute_on_media_timeout");
7301  }
7302 }
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
Definition: switch_time.c:311
#define switch_channel_hangup(channel, hangup_cause)
Hangup a channel flagging it&#39;s state machine to end.
switch_status_t switch_channel_execute_on(switch_channel_t *channel, const char *variable_prefix)
#define SWITCH_CHANNEL_SESSION_LOG(x)
uint32_t media_timeout
Definition: switch_rtp.c:444
#define switch_core_session_get_name(_s)
Definition: switch_core.h:265
switch_time_t last_media
Definition: switch_rtp.c:443
#define rtp_type(rtp_session)
Definition: switch_rtp.c:544
_Ret_ switch_channel_t * switch_core_session_get_channel(_In_ switch_core_session_t *session)
Retrieve a pointer to the channel object associated with a given session.
int64_t switch_time_t
Definition: switch_apr.h:188
switch_core_session_t * session
Definition: switch_rtp.c:476
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.

◆ do_2833()

static void do_2833 ( switch_rtp_t rtp_session)
static

Definition at line 5477 of file switch_rtp.c.

References switch_rtp::delay_samples, switch_dtmf_t::digit, switch_rtp::dtmf_data, switch_rtp_numbers_t::dtmf_packet_count, switch_rtp_rfc2833_data::dtmf_queue, switch_dtmf_t::duration, switch_rtp::flags, switch_rtp::interdigit_delay, switch_rtp::last_write_samplecount, switch_rtp::last_write_ts, switch_rtp::max_next_write_samplecount, memset(), switch_rtp::need_mark, switch_rtp::next_write_samplecount, switch_rtp_rfc2833_data::out_digit, switch_rtp_rfc2833_data::out_digit_dur, switch_rtp_rfc2833_data::out_digit_packet, switch_rtp_rfc2833_data::out_digit_sofar, switch_rtp_rfc2833_data::out_digit_sub_sofar, switch_rtp_stats_t::outbound, switch_rtp::queue_delay, switch_rtp_numbers_t::raw_bytes, RTP_BUG_CISCO_SKIP_MARK_BIT_2833, RTP_BUG_SONUS_SEND_INVALID_TIMESTAMP_2833, switch_rtp::rtp_bugs, switch_timer::samplecount, switch_rtp::samples_per_interval, switch_rtp::sending_dtmf, switch_rtp::seq, switch_rtp::session, set_dtmf_delay(), switch_rtp::stats, SWITCH_CHANNEL_SESSION_LOG, switch_char_to_rfc2833(), SWITCH_LOG_DEBUG, switch_log_printf(), switch_queue_size(), switch_queue_trypop(), SWITCH_RTP_FLAG_RESET, SWITCH_RTP_FLAG_USE_TIMER, switch_rtp_write_manual(), SWITCH_STATUS_SUCCESS, switch_rtp::te, switch_rtp::timer, switch_timer::timer_interface, switch_rtp_rfc2833_data::timestamp_dtmf, and switch_rtp::write_timer.

Referenced by rtp_common_read().

5478 {
5479  switch_frame_flag_t flags = 0;
5480  uint32_t samples = rtp_session->samples_per_interval;
5481 
5482  if (rtp_session->dtmf_data.out_digit_dur > 0) {
5483  int x, loops = 1;
5484 
5485  if (!rtp_session->last_write_ts) {
5486  if (rtp_session->timer.timer_interface) {
5487  //switch_core_timer_sync(&rtp_session->write_timer);
5488  rtp_session->last_write_ts = rtp_session->write_timer.samplecount;
5489  } else {
5490  rtp_session->last_write_ts = rtp_session->samples_per_interval;
5491  }
5492  }
5493 
5494  rtp_session->dtmf_data.out_digit_sofar += samples;
5495  rtp_session->dtmf_data.out_digit_sub_sofar += samples;
5496 
5497  if (rtp_session->dtmf_data.out_digit_sub_sofar > 0xFFFF) {
5498  rtp_session->dtmf_data.out_digit_sub_sofar = samples;
5499  rtp_session->dtmf_data.timestamp_dtmf += 0xFFFF;
5500  }
5501 
5502  if (rtp_session->dtmf_data.out_digit_sofar >= rtp_session->dtmf_data.out_digit_dur) {
5503  rtp_session->dtmf_data.out_digit_packet[1] |= 0x80;
5504  loops = 3;
5505  }
5506 
5507  rtp_session->dtmf_data.out_digit_packet[2] = (unsigned char) (rtp_session->dtmf_data.out_digit_sub_sofar >> 8);
5508  rtp_session->dtmf_data.out_digit_packet[3] = (unsigned char) rtp_session->dtmf_data.out_digit_sub_sofar;
5509 
5510  for (x = 0; x < loops; x++) {
5511  switch_size_t wrote = switch_rtp_write_manual(rtp_session,
5512  rtp_session->dtmf_data.out_digit_packet, 4, 0,
5513  rtp_session->te, rtp_session->dtmf_data.timestamp_dtmf, &flags);
5514 
5515  rtp_session->stats.outbound.raw_bytes += wrote;
5516  rtp_session->stats.outbound.dtmf_packet_count++;
5517 
5518  if (loops == 1) {
5519  rtp_session->last_write_ts += samples;
5520 
5522  rtp_session->dtmf_data.timestamp_dtmf = rtp_session->last_write_ts;
5523  }
5524  }
5525 
5526  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Send %s packet for [%c] ts=%u dur=%d/%d/%d seq=%d lw=%u\n",
5527  loops == 1 ? "middle" : "end", rtp_session->dtmf_data.out_digit,
5528  rtp_session->dtmf_data.timestamp_dtmf,
5529  rtp_session->dtmf_data.out_digit_sofar,
5530  rtp_session->dtmf_data.out_digit_sub_sofar, rtp_session->dtmf_data.out_digit_dur, rtp_session->seq, rtp_session->last_write_ts);
5531  }
5532 
5533  if (loops != 1) {
5534  rtp_session->sending_dtmf = 0;
5535  rtp_session->need_mark = 1;
5536 
5537  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
5538  //switch_core_timer_sync(&rtp_session->write_timer);
5539  rtp_session->last_write_samplecount = rtp_session->write_timer.samplecount;
5540  }
5541 
5542  rtp_session->dtmf_data.out_digit_dur = 0;
5543 
5544  if (rtp_session->interdigit_delay) {
5545  set_dtmf_delay(rtp_session, rtp_session->interdigit_delay, rtp_session->interdigit_delay * 10);
5546  }
5547 
5548  return;
5549  }
5550  }
5551 
5552  if (!rtp_session->dtmf_data.out_digit_dur && rtp_session->dtmf_data.dtmf_queue && switch_queue_size(rtp_session->dtmf_data.dtmf_queue)) {
5553  void *pop;
5554 
5555  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
5556  //switch_core_timer_sync(&rtp_session->write_timer);
5557  if (rtp_session->timer.samplecount < rtp_session->next_write_samplecount) {
5558  return;
5559  }
5560 
5561  if (rtp_session->timer.samplecount >= rtp_session->max_next_write_samplecount) {
5562  rtp_session->queue_delay = 0;
5563  }
5564 
5565  } else if (rtp_session->queue_delay) {
5566  if (rtp_session->delay_samples >= rtp_session->samples_per_interval) {
5567  rtp_session->delay_samples -= rtp_session->samples_per_interval;
5568  } else {
5569  rtp_session->delay_samples = 0;
5570  }
5571 
5572  if (!rtp_session->delay_samples) {
5573  rtp_session->queue_delay = 0;
5574  }
5575  }
5576 
5577  if (rtp_session->queue_delay) {
5578  return;
5579  }
5580 
5581 
5582  if (!rtp_session->sending_dtmf) {
5583  rtp_session->sending_dtmf = 1;
5584  }
5585 
5586  if (switch_queue_trypop(rtp_session->dtmf_data.dtmf_queue, &pop) == SWITCH_STATUS_SUCCESS) {
5587  switch_dtmf_t *rdigit = pop;
5588  switch_size_t wrote;
5589 
5590  if (rdigit->digit == 'w') {
5591  set_dtmf_delay(rtp_session, 500, 0);
5592  free(rdigit);
5593  return;
5594  }
5595 
5596  if (rdigit->digit == 'W') {
5597  set_dtmf_delay(rtp_session, 1000, 0);
5598  free(rdigit);
5599  return;
5600  }
5601 
5602  memset(rtp_session->dtmf_data.out_digit_packet, 0, 4);
5603  rtp_session->dtmf_data.out_digit_sofar = samples;
5604  rtp_session->dtmf_data.out_digit_sub_sofar = samples;
5605  rtp_session->dtmf_data.out_digit_dur = rdigit->duration;
5606  rtp_session->dtmf_data.out_digit = rdigit->digit;
5607  rtp_session->dtmf_data.out_digit_packet[0] = (unsigned char) switch_char_to_rfc2833(rdigit->digit);
5608  rtp_session->dtmf_data.out_digit_packet[1] = 13;
5609  rtp_session->dtmf_data.out_digit_packet[2] = (unsigned char) (rtp_session->dtmf_data.out_digit_sub_sofar >> 8);
5610  rtp_session->dtmf_data.out_digit_packet[3] = (unsigned char) rtp_session->dtmf_data.out_digit_sub_sofar;
5611 
5612 
5613  rtp_session->dtmf_data.timestamp_dtmf = rtp_session->last_write_ts + samples;
5614  rtp_session->last_write_ts = rtp_session->dtmf_data.timestamp_dtmf;
5615  rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 0;
5616 
5617  wrote = switch_rtp_write_manual(rtp_session,
5618  rtp_session->dtmf_data.out_digit_packet,
5619  4,
5620  rtp_session->rtp_bugs & RTP_BUG_CISCO_SKIP_MARK_BIT_2833 ? 0 : 1,
5621  rtp_session->te, rtp_session->dtmf_data.timestamp_dtmf, &flags);
5622 
5623 
5624  rtp_session->stats.outbound.raw_bytes += wrote;
5625  rtp_session->stats.outbound.dtmf_packet_count++;
5626 
5627  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Send start packet for [%c] ts=%u dur=%d/%d/%d seq=%d lw=%u\n",
5628  rtp_session->dtmf_data.out_digit,
5629  rtp_session->dtmf_data.timestamp_dtmf,
5630  rtp_session->dtmf_data.out_digit_sofar,
5631  rtp_session->dtmf_data.out_digit_sub_sofar, rtp_session->dtmf_data.out_digit_dur, rtp_session->seq, rtp_session->last_write_ts);
5632 
5633  free(rdigit);
5634  }
5635  }
5636 }
uint32_t queue_delay
Definition: switch_rtp.c:394
unsigned int switch_queue_size(switch_queue_t *queue)
Definition: switch_apr.c:1238
#define SWITCH_CHANNEL_SESSION_LOG(x)
unsigned char out_digit_packet[4]
Definition: switch_rtp.c:227
uint32_t delay_samples
Definition: switch_rtp.c:391
switch_status_t switch_queue_trypop(switch_queue_t *queue, void **data)
Definition: switch_apr.c:1264
static void set_dtmf_delay(switch_rtp_t *rtp_session, uint32_t ms, uint32_t max_ms)
Definition: switch_rtp.c:5455
uint32_t duration
Definition: switch_types.h:302
switch_size_t dtmf_packet_count
Definition: switch_types.h:700
uint32_t max_next_write_samplecount
Definition: switch_rtp.c:393
struct switch_rtp_rfc2833_data dtmf_data
Definition: switch_rtp.c:426
switch_size_t raw_bytes
Definition: switch_types.h:693
switch_timer_interface_t * timer_interface
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:396
switch_rtp_numbers_t outbound
Definition: switch_types.h:772
uintptr_t switch_size_t
uint16_t seq
Definition: switch_rtp.c:375
uint8_t need_mark
Definition: switch_rtp.c:380
uint32_t switch_frame_flag_t
switch_rtp_stats_t stats
Definition: switch_rtp.c:454
unsigned char switch_char_to_rfc2833(char key)
Return the RFC2833 event based on an key character.
uint32_t samples_per_interval
Definition: switch_rtp.c:409
uint32_t interdigit_delay
Definition: switch_rtp.c:475
switch_timer_t timer
Definition: switch_rtp.c:434
uint32_t last_write_ts
Definition: switch_rtp.c:386
switch_core_session_t * session
Definition: switch_rtp.c:476
switch_timer_t write_timer
Definition: switch_rtp.c:435
int8_t sending_dtmf
Definition: switch_rtp.c:379
unsigned int out_digit_sofar
Definition: switch_rtp.c:228
switch_rtp_bug_flag_t rtp_bugs
Definition: switch_rtp.c:453
switch_payload_t te
Definition: switch_rtp.c:427
switch_queue_t * dtmf_queue
Definition: switch_rtp.c:225
uint32_t next_write_samplecount
Definition: switch_rtp.c:392
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
unsigned int out_digit_dur
Definition: switch_rtp.c:230
uint32_t last_write_samplecount
Definition: switch_rtp.c:390
unsigned int out_digit_sub_sofar
Definition: switch_rtp.c:229
memset(buf, 0, buflen)
int switch_rtp_write_manual(switch_rtp_t *rtp_session, void *data, uint32_t datalen, uint8_t m, switch_payload_t payload, uint32_t ts, switch_frame_flag_t *flags)
Write data with a specified payload and sequence number to a given RTP session.
Definition: switch_rtp.c:9110

◆ do_dtls()

static int do_dtls ( switch_rtp_t rtp_session,
switch_dtls_t dtls 
)
static

Definition at line 3367 of file switch_rtp.c.

References switch_rtp_ice_t::addr, buf, switch_dtls_s::bytes, switch_rtp_ice_t::cand_responsive, cert, CF_AUDIO_VIDEO_BUNDLE, switch_dtls_s::data, switch_dtls_s::filter_bio, switch_rtp::from_addr, switch_rtp::ice, ICE_LITE, switch_rtp_ice_t::ice_user, MAX_DTLS_MTU, switch_dtls_s::read_bio, switch_rtp_ice_t::ready, switch_dtls_s::remote_addr, switch_dtls_s::remote_fp, switch_rtp_ice_t::rready, switch_dtls_s::rtp_session, rtp_type, switch_rtp::session, switch_dtls_s::sock_output, switch_dtls_s::ssl, switch_dtls_s::state, switch_assert, SWITCH_CHANNEL_SESSION_LOG, switch_channel_test_flag(), switch_cmp_addr(), switch_core_cert_extract_fingerprint(), switch_core_cert_verify(), switch_core_session_get_channel(), switch_get_addr(), SWITCH_LOG_DEBUG5, SWITCH_LOG_DEBUG6, SWITCH_LOG_ERROR, switch_log_printf(), switch_socket_sendto(), SWITCH_STATUS_SUCCESS, SWITCH_TRUE, switch_rtp_ice_t::type, and switch_dtls_s::write_bio.

Referenced by read_rtcp_packet(), and read_rtp_packet().

3368 {
3369  int r = 0, ret = 0, len;
3370  switch_size_t bytes;
3371  unsigned char buf[MAX_DTLS_MTU] = "";
3372  uint8_t is_ice = rtp_session->ice.ice_user ? 1 : 0;
3373  int ready = is_ice ? (rtp_session->ice.rready && rtp_session->ice.ready) : 1;
3374  int pending;
3375 
3377  if (rtp_session->ice.ice_user && rtp_session->ice.rready == 1) ready = 1;
3378  }
3379 
3380  if (!dtls->bytes && !ready) {
3381  return 0;
3382  }
3383 
3384  if (is_ice && !(rtp_session->ice.type & ICE_LITE) && !rtp_session->ice.cand_responsive) {
3385  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG6, "Got DTLS packet but candidate is not responsive\n");
3386 
3387  return 0;
3388  }
3389 
3390  if (is_ice && !switch_cmp_addr(rtp_session->from_addr, rtp_session->ice.addr, SWITCH_TRUE)) {
3391  char tmp_buf1[80] = "";
3392  char tmp_buf2[80] = "";
3393  const char *host_from = switch_get_addr(tmp_buf1, sizeof(tmp_buf1), rtp_session->from_addr);
3394  const char *host_ice_cur_addr = switch_get_addr(tmp_buf2, sizeof(tmp_buf2), rtp_session->ice.addr);
3395 
3396  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG5, "Got DTLS packet from [%s] whilst current ICE negotiated address is [%s]. Ignored.\n", host_from, host_ice_cur_addr);
3397 
3398  return 0;
3399  }
3400 
3401  if (dtls->bytes > 0 && dtls->data) {
3402  ret = BIO_write(dtls->read_bio, dtls->data, (int)dtls->bytes);
3403  if (ret <= 0) {
3404  ret = SSL_get_error(dtls->ssl, ret);
3405  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "%s DTLS packet decode err: SSL err %d\n", rtp_type(rtp_session), ret);
3406  } else if (ret != (int)dtls->bytes) {
3407  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "%s DTLS packet decode err: read %d bytes instead of %d\n", rtp_type(rtp_session), ret, (int)dtls->bytes);
3408  }
3409  }
3410 
3411  if (dtls_states[dtls->state]) {
3412  r = dtls_states[dtls->state](rtp_session, dtls);
3413  }
3414 
3415  while ((pending = BIO_ctrl_pending(dtls->filter_bio)) > 0) {
3416  switch_assert(pending <= sizeof(buf));
3417 
3418  len = BIO_read(dtls->write_bio, buf, pending);
3419  if (len > 0) {
3420  bytes = len;
3421  ret = switch_socket_sendto(dtls->sock_output, dtls->remote_addr, 0, (void *)buf, &bytes);
3422 
3423  if (ret != SWITCH_STATUS_SUCCESS) {
3424  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "%s DTLS packet not written to socket: %d\n", rtp_type(rtp_session), ret);
3425  } else if (bytes != len) {
3426  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "%s DTLS packet write err: written %d bytes instead of %d\n", rtp_type(rtp_session), (int)bytes, len);
3427  }
3428  } else {
3429  ret = SSL_get_error(dtls->ssl, len);
3430  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "%s DTLS packet encode err: SSL err %d\n", rtp_type(rtp_session), ret);
3431  }
3432  }
3433 
3434  return r;
3435 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
BIO * write_bio
Definition: switch_rtp.c:280
switch_sockaddr_t * remote_addr
Definition: switch_rtp.c:291
BIO * filter_bio
Definition: switch_rtp.c:281
switch_sockaddr_t * addr
Definition: switch_rtp.c:252
BIO * read_bio
Definition: switch_rtp.c:279
uint32_t switch_channel_test_flag(switch_channel_t *channel, switch_channel_flag_t flag)
Test for presence of given flag on a given channel.
dtls_state_t state
Definition: switch_rtp.c:284
#define rtp_type(rtp_session)
Definition: switch_rtp.c:544
_Ret_ switch_channel_t * switch_core_session_get_channel(_In_ switch_core_session_t *session)
Retrieve a pointer to the channel object associated with a given session.
switch_socket_t * sock_output
Definition: switch_rtp.c:290
switch_core_media_ice_type_t type
Definition: switch_rtp.c:255
switch_byte_t switch_byte_t * buf
switch_status_t switch_socket_sendto(switch_socket_t *sock, switch_sockaddr_t *where, int32_t flags, const char *buf, switch_size_t *len)
Definition: switch_apr.c:796
int switch_cmp_addr(switch_sockaddr_t *sa1, switch_sockaddr_t *sa2, switch_bool_t ip_only)
uint8_t cand_responsive
Definition: switch_rtp.c:265
dtls_state_handler_t dtls_states[DS_INVALID]
Definition: switch_rtp.c:308
uintptr_t switch_size_t
switch_core_session_t * session
Definition: switch_rtp.c:476
switch_rtp_ice_t ice
Definition: switch_rtp.c:401
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
const char * switch_get_addr(char *buf, switch_size_t len, switch_sockaddr_t *in)
Definition: switch_apr.c:979
switch_sockaddr_t * from_addr
Definition: switch_rtp.c:398
#define switch_assert(expr)
switch_size_t bytes
Definition: switch_rtp.c:288
#define MAX_DTLS_MTU
Definition: switch_rtp.c:273

◆ do_flush()

static switch_size_t do_flush ( switch_rtp_t rtp_session,
int  force,
switch_size_t  bytes_in 
)
static

Definition at line 5681 of file switch_rtp.c.

References FALSE, switch_rtp::flags, switch_rtp_numbers_t::flush_packet_count, switch_rtp::from_addr, handle_rfc2833(), rtp_msg_t::header, switch_rtp_stats_t::inbound, switch_rtp::jb, jb_valid(), switch_rtp::last_media, switch_rtp::last_rtp_hdr, switch_rtp::media_timeout, switch_rtp_numbers_t::packet_count, switch_rtp::pause_jb, switch_rtp_numbers_t::raw_bytes, READ_DEC, READ_INC, switch_rtp::recv_msg, switch_rtp::recv_te, reset_jitter_seq(), rtp_header_len, switch_rtp::session, switch_rtp::sock_input, switch_rtp::stats, switch_channel_get_name(), SWITCH_CHANNEL_LOG, SWITCH_CHANNEL_SESSION_LOG, switch_core_session_get_channel(), switch_core_session_request_video_refresh, switch_jb_reset(), SWITCH_LOG_CONSOLE, SWITCH_LOG_ERROR, switch_log_printf(), switch_micro_time_now(), switch_rtp_clear_flag(), SWITCH_RTP_FLAG_DEBUG_RTP_READ, SWITCH_RTP_FLAG_DTMF_ON, SWITCH_RTP_FLAG_NOBLOCK, SWITCH_RTP_FLAG_PROXY_MEDIA, SWITCH_RTP_FLAG_UDPTL, SWITCH_RTP_FLAG_VIDEO, switch_rtp_ready(), switch_rtp_set_flag(), SWITCH_SO_NONBLOCK, switch_socket_opt_set(), switch_socket_recvfrom(), TRUE, switch_rtp::vb, and switch_rtp::vbw.

Referenced by rtp_common_read().

5682 {
5683  int was_blocking = 0;
5684  switch_size_t bytes;
5685  switch_size_t bytes_out = 0;
5686 
5687  if (!switch_rtp_ready(rtp_session)) {
5688  return 0;
5689  }
5690 
5691  reset_jitter_seq(rtp_session);
5692 
5693  if (!force) {
5694  if ((rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) ||
5695  rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] ||
5696  rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] ||
5697  rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON]
5698  ) {
5699  return bytes_in;
5700  }
5701  }
5702 
5703  READ_INC(rtp_session);
5704 
5705  if (switch_rtp_ready(rtp_session) ) {
5706 
5707  if (rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) {
5708  //switch_jb_reset(rtp_session->jb);
5709  bytes_out = bytes_in;
5710  goto end;
5711  }
5712 
5713  if (rtp_session->vbw) {
5714  switch_jb_reset(rtp_session->vbw);
5715  }
5716 
5717  if (rtp_session->vb) {
5718  //switch_jb_reset(rtp_session->vb);
5719  bytes_out = bytes_in;
5720  goto end;
5721  }
5722 
5723  if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_READ]) {
5725  SWITCH_LOG_CONSOLE, "%s FLUSH\n",
5726  rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "NoName"
5727  );
5728  }
5729 
5730  if (!rtp_session->flags[SWITCH_RTP_FLAG_NOBLOCK]) {
5731  was_blocking = 1;
5734  }
5735 
5736  // before processing/flushing packets, if current packet is rfc2833, handle it (else it would be lost)
5737  if (bytes_in > rtp_header_len && rtp_session->last_rtp_hdr.version == 2 && rtp_session->last_rtp_hdr.pt == rtp_session->recv_te) {
5738  int do_cng = 0;
5739 #ifdef DEBUG_2833
5740  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "*** Handling current RTP packet before flushing. seq=%u ***\n", ntohs(rtp_session->last_rtp_hdr.seq));
5741 #endif
5742  handle_rfc2833(rtp_session, bytes_in, &do_cng);
5743  }
5744 
5745  do {
5746  if (switch_rtp_ready(rtp_session)) {
5747  bytes = sizeof(rtp_msg_t);
5748  switch_socket_recvfrom(rtp_session->from_addr, rtp_session->sock_input, 0, (void *) &rtp_session->recv_msg, &bytes);
5749 
5750  if (bytes) {
5751  int do_cng = 0;
5752 
5753  if (rtp_session->media_timeout) {
5754  rtp_session->last_media = switch_micro_time_now();
5755  }
5756 
5757  /* Make sure to handle RFC2833 packets, even if we're flushing the packets */
5758  if (bytes > rtp_header_len && rtp_session->recv_msg.header.version == 2 && rtp_session->recv_msg.header.pt == rtp_session->recv_te) {
5759  rtp_session->last_rtp_hdr = rtp_session->recv_msg.header;
5760  handle_rfc2833(rtp_session, bytes, &do_cng);
5761 #ifdef DEBUG_2833
5762  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "*** RTP packet handled in flush loop %d ***\n", do_cng);
5763 #endif
5764  }
5765 
5766  rtp_session->stats.inbound.raw_bytes += bytes;
5767  rtp_session->stats.inbound.flush_packet_count++;
5768  rtp_session->stats.inbound.packet_count++;
5769  }
5770  } else {
5771  break;
5772  }
5773  } while (bytes > 0);
5774 
5775 #ifdef DEBUG_2833
5776  if (flushed) {
5777  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "*** do_flush: total flushed packets: %ld ***\n",(long)flushed);
5778  }
5779 #endif
5780 
5781 
5782  if (was_blocking && switch_rtp_ready(rtp_session)) {
5785  }
5786 
5787 
5788  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->session) {
5789  //int type = 1; // sum flags: 1 encoder; 2; decoder
5790  //switch_core_media_codec_control(rtp_session->session, SWITCH_MEDIA_TYPE_VIDEO, SWITCH_IO_READ, SCC_VIDEO_RESET, SCCT_INT, (void *)&type, NULL, NULL);
5792  }
5793  }
5794 
5795  end:
5796 
5797  READ_DEC(rtp_session);
5798 
5799  return bytes_out;
5800 }
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
Definition: switch_time.c:311
#define SWITCH_CHANNEL_SESSION_LOG(x)
switch_rtp_numbers_t inbound
Definition: switch_types.h:771
uint8_t switch_rtp_ready(switch_rtp_t *rtp_session)
Test if an RTP session is ready.
Definition: switch_rtp.c:5150
#define SWITCH_CHANNEL_LOG
switch_socket_t * sock_input
Definition: switch_rtp.c:326
uint32_t media_timeout
Definition: switch_rtp.c:444
switch_jb_t * vbw
Definition: switch_rtp.c:440
switch_status_t switch_socket_recvfrom(switch_sockaddr_t *from, switch_socket_t *sock, int32_t flags, char *buf, size_t *len)
Definition: switch_apr.c:1021
switch_time_t last_media
Definition: switch_rtp.c:443
#define READ_DEC(rtp_session)
Definition: switch_rtp.c:68
_Ret_ switch_channel_t * switch_core_session_get_channel(_In_ switch_core_session_t *session)
Retrieve a pointer to the channel object associated with a given session.
switch_size_t raw_bytes
Definition: switch_types.h:693
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:396
#define switch_core_session_request_video_refresh(_s)
Definition: switch_core.h:2892
#define READ_INC(rtp_session)
Definition: switch_rtp.c:67
switch_status_t switch_socket_opt_set(switch_socket_t *sock, int32_t opt, int32_t on)
Definition: switch_apr.c:907
uintptr_t switch_size_t
switch_size_t packet_count
Definition: switch_types.h:695
switch_jb_t * vb
Definition: switch_rtp.c:439
switch_rtp_stats_t stats
Definition: switch_rtp.c:454
void switch_rtp_clear_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Clear an RTP Flag.
Definition: switch_rtp.c:5434
void switch_rtp_set_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Set an RTP Flag.
Definition: switch_rtp.c:5375
switch_jb_t * jb
Definition: switch_rtp.c:438
srtp_hdr_t header
Definition: switch_rtp.c:112
#define SWITCH_SO_NONBLOCK
Definition: switch_apr.h:994
rtp_msg_t recv_msg
Definition: switch_rtp.c:345
#define FALSE
static void reset_jitter_seq(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:1650
switch_core_session_t * session
Definition: switch_rtp.c:476
switch_size_t flush_packet_count
Definition: switch_types.h:702
void switch_jb_reset(switch_jb_t *jb)
uint8_t pause_jb
Definition: switch_rtp.c:469
static int jb_valid(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:5665
switch_payload_t recv_te
Definition: switch_rtp.c:428
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
static handle_rfc2833_result_t handle_rfc2833(switch_rtp_t *rtp_session, switch_size_t bytes, int *do_cng)
Definition: switch_rtp.c:586
#define TRUE
switch_sockaddr_t * from_addr
Definition: switch_rtp.c:398
rtp_hdr_t last_rtp_hdr
Definition: switch_rtp.c:373
char * switch_channel_get_name(switch_channel_t *channel)
Retrieve the name of a given channel.
#define rtp_header_len
Definition: switch_rtp.c:73

◆ do_mos()

static void do_mos ( switch_rtp_t rtp_session)
static

Definition at line 1575 of file switch_rtp.c.

References switch_rtp::bad_stream, switch_rtp::consecutive_flaws, error_period::consecutive_flaws, switch_rtp_numbers_t::error_log, error_period::flaws, switch_rtp_numbers_t::flaws, switch_rtp_stats_t::inbound, switch_rtp_numbers_t::last_flaw, switch_rtp_numbers_t::max_variance, switch_rtp_numbers_t::min_variance, switch_rtp_numbers_t::mos, switch_rtp_numbers_t::R, switch_rtp_numbers_t::recved, rtp_session_name, rtp_type, switch_rtp::session, switch_rtp::stats, SWITCH_CHANNEL_SESSION_LOG, SWITCH_LOG_DEBUG1, SWITCH_LOG_DEBUG3, switch_log_printf(), and switch_rtp_numbers_t::variance.

Referenced by check_jitter(), rtp_common_read(), switch_rtp_get_stats(), and switch_rtp_sync_stats().

1575  {
1576  int R;
1577 
1578  if ((switch_size_t)rtp_session->stats.inbound.recved < rtp_session->stats.inbound.flaws) {
1579  rtp_session->stats.inbound.flaws = 0;
1580  }
1581 
1582  if (rtp_session->stats.inbound.recved > 0 &&
1583  rtp_session->stats.inbound.flaws && (rtp_session->stats.inbound.last_flaw != rtp_session->stats.inbound.flaws)) {
1584 
1585  if (rtp_session->consecutive_flaws++) {
1586  int penalty = rtp_session->consecutive_flaws;
1587 
1588  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "%s %s %d consecutive flaws, adding %d flaw penalty\n",
1589  rtp_session_name(rtp_session), rtp_type(rtp_session),
1590  rtp_session->consecutive_flaws, penalty);
1591  rtp_session->bad_stream++;
1592  rtp_session->stats.inbound.flaws += penalty;
1593  rtp_session->stats.inbound.last_flaw = rtp_session->stats.inbound.flaws;
1594 
1595  if (rtp_session->stats.inbound.error_log) {
1596  rtp_session->stats.inbound.error_log->flaws += penalty;
1597  rtp_session->stats.inbound.error_log->consecutive_flaws++;
1598  }
1599  }
1600  } else {
1601  rtp_session->consecutive_flaws = 0;
1602  }
1603 
1604  R = (int)((double)((double)(rtp_session->stats.inbound.recved - rtp_session->stats.inbound.flaws) / (double)rtp_session->stats.inbound.recved) * 100.0);
1605 
1606  if (R < 0 || R > 100) R = 100;
1607 
1608  rtp_session->stats.inbound.R = R;
1609  rtp_session->stats.inbound.mos = 1 + (0.035) * R + (.000007) * R * (R-60) * (100-R);
1610 
1611  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "%s %s stat %0.2f %ld/%d flaws: %ld mos: %0.2f v: %0.2f %0.2f/%0.2f\n",
1612  rtp_session_name(rtp_session),
1613  rtp_type(rtp_session),
1614  rtp_session->stats.inbound.R,
1615  (long int)(rtp_session->stats.inbound.recved - rtp_session->stats.inbound.flaws), rtp_session->stats.inbound.recved,
1616  (long int)rtp_session->stats.inbound.flaws,
1617  rtp_session->stats.inbound.mos,
1618  rtp_session->stats.inbound.variance,
1619  rtp_session->stats.inbound.min_variance,
1620  rtp_session->stats.inbound.max_variance
1621  );
1622 
1623 }
switch_size_t flaws
Definition: switch_types.h:723
#define SWITCH_CHANNEL_SESSION_LOG(x)
switch_rtp_numbers_t inbound
Definition: switch_types.h:771
uint32_t consecutive_flaws
Definition: switch_rtp.c:416
#define rtp_session_name(_rtp_session)
Definition: switch_rtp.c:85
switch_size_t last_flaw
Definition: switch_types.h:724
uint32_t flaws
Definition: switch_types.h:686
#define rtp_type(rtp_session)
Definition: switch_rtp.c:544
uintptr_t switch_size_t
uint32_t consecutive_flaws
Definition: switch_types.h:687
switch_rtp_stats_t stats
Definition: switch_rtp.c:454
struct error_period * error_log
Definition: switch_types.h:727
switch_core_session_t * session
Definition: switch_rtp.c:476
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
uint32_t bad_stream
Definition: switch_rtp.c:457

◆ dtls_bio_filter_ctrl()

static long dtls_bio_filter_ctrl ( BIO *  bio,
int  cmd,
long  num,
void *  ptr 
)
static

Definition at line 3608 of file switch_rtp.c.

References dtls_bio_filter_free(), dtls_bio_filter_new(), dtls_bio_filter_write(), dtls_bio_filter::mtu, dtls_bio_filter::mutex, packet_list_s::next, dtls_bio_filter::packets, packet_list_s::size, SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), dtls_bio_filter::tail, and dtls_bio_filter::unused.

Referenced by switch_rtp_dtls_init().

3608  {
3609 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3610  dtls_bio_filter *filter = (dtls_bio_filter *)bio->ptr;
3611 #else
3612  dtls_bio_filter *filter = (dtls_bio_filter *)BIO_get_data(bio);
3613 #endif
3614 
3615  switch(cmd) {
3616  case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
3617  return 1200;
3618  case BIO_CTRL_DGRAM_SET_MTU:
3619  filter->mtu = num;
3620  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Setting MTU: %ld\n", filter->mtu);
3621  return num;
3622  case BIO_CTRL_FLUSH:
3623  return 1;
3624  case BIO_CTRL_DGRAM_QUERY_MTU:
3625  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Advertizing MTU: %ld\n", filter->mtu);
3626  return filter->mtu;
3627  case BIO_CTRL_WPENDING:
3628  return 0L;
3629  case BIO_CTRL_PENDING: {
3630  int pending = 0;
3631  packet_list_t *top;
3632 
3633  if (filter == NULL) {
3634  return 0;
3635  }
3636 
3637  switch_mutex_lock(filter->mutex);
3638  if ((top = filter->packets)) {
3639  filter->packets = filter->packets->next;
3640 
3641  if (top == filter->tail || !filter->packets) {
3642  filter->tail = NULL;
3643  }
3644 
3645  pending = top->size;
3646  top->next = filter->unused;
3647  filter->unused = top;
3648  }
3649  switch_mutex_unlock(filter->mutex);
3650 
3651  return pending;
3652  }
3653  default:
3654  //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "dtls_bio_filter_ctrl: %d\n", cmd);
3655  break;
3656  }
3657  return 0;
3658 }
#define SWITCH_CHANNEL_LOG
struct packet_list_s * next
Definition: switch_rtp.c:3486
switch_mutex_t * mutex
Definition: switch_rtp.c:3494
packet_list_t * packets
Definition: switch_rtp.c:3491
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
packet_list_t * tail
Definition: switch_rtp.c:3493
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
packet_list_t * unused
Definition: switch_rtp.c:3492

◆ dtls_bio_filter_free()

static int dtls_bio_filter_free ( BIO *  bio)
static

Definition at line 3526 of file switch_rtp.c.

References pool, dtls_bio_filter::pool, and switch_core_destroy_memory_pool.

Referenced by dtls_bio_filter_ctrl(), and switch_rtp_dtls_init().

3526  {
3527  dtls_bio_filter *filter;
3528 
3529  if (bio == NULL) {
3530  return 0;
3531  }
3532 
3533  /* Get rid of the filter state */
3534 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3535  filter = (dtls_bio_filter *)bio->ptr;
3536 #else
3537  filter = (dtls_bio_filter *)BIO_get_data(bio);
3538 #endif
3539 
3540  if (filter != NULL) {
3541  switch_memory_pool_t *pool = filter->pool;
3543  pool = NULL;
3544  filter = NULL;
3545  }
3546 
3547 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3548  bio->ptr = NULL;
3549  bio->init = 0;
3550  bio->flags = 0;
3551 #else
3552  BIO_set_init(bio, 0);
3553  BIO_set_data(bio, NULL);
3554  BIO_clear_flags(bio, ~0);
3555 #endif
3556  return 1;
3557 }
#define switch_core_destroy_memory_pool(p)
Returns a subpool back to the main pool.
Definition: switch_core.h:642
switch_memory_pool_t * pool
struct fspr_pool_t switch_memory_pool_t
switch_memory_pool_t * pool
Definition: switch_rtp.c:3495

◆ dtls_bio_filter_new()

static int dtls_bio_filter_new ( BIO *  bio)
static

Definition at line 3500 of file switch_rtp.c.

References dtls_bio_filter::mutex, dtls_bio_filter::packets, pool, dtls_bio_filter::pool, switch_core_alloc, switch_core_new_memory_pool, switch_mutex_init(), and SWITCH_MUTEX_NESTED.

Referenced by dtls_bio_filter_ctrl(), and switch_rtp_dtls_init().

3500  {
3501  /* Create a filter state struct */
3502  dtls_bio_filter *filter;
3504 
3506  filter = switch_core_alloc(pool, sizeof(*filter));
3507  filter->pool = pool;
3508 
3509  filter->packets = NULL;
3510  switch_mutex_init(&filter->mutex, SWITCH_MUTEX_NESTED, filter->pool);
3511 
3512  /* Set the BIO as initialized */
3513 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3514  bio->init = 1;
3515  bio->ptr = filter;
3516  bio->flags = 0;
3517 #else
3518  BIO_set_init(bio, 1);
3519  BIO_set_data(bio, filter);
3520  BIO_clear_flags(bio, ~0);
3521 #endif
3522 
3523  return 1;
3524 }
#define switch_core_new_memory_pool(p)
Create a new sub memory pool from the core&#39;s master pool.
Definition: switch_core.h:633
switch_mutex_t * mutex
Definition: switch_rtp.c:3494
switch_memory_pool_t * pool
packet_list_t * packets
Definition: switch_rtp.c:3491
#define SWITCH_MUTEX_NESTED
Definition: switch_apr.h:318
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:684
switch_status_t switch_mutex_init(switch_mutex_t **lock, unsigned int flags, switch_memory_pool_t *pool)
Definition: switch_apr.c:293
struct fspr_pool_t switch_memory_pool_t
switch_memory_pool_t * pool
Definition: switch_rtp.c:3495

◆ dtls_bio_filter_write()

static int dtls_bio_filter_write ( BIO *  bio,
const char *  in,
int  inl 
)
static

Definition at line 3559 of file switch_rtp.c.

References dtls_bio_filter::mutex, packet_list_s::next, dtls_bio_filter::packets, dtls_bio_filter::pool, packet_list_s::size, SWITCH_CHANNEL_LOG, switch_core_alloc, SWITCH_LOG_DEBUG1, switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), dtls_bio_filter::tail, and dtls_bio_filter::unused.

Referenced by dtls_bio_filter_ctrl(), and switch_rtp_dtls_init().

3559  {
3560  long ret;
3561  dtls_bio_filter *filter;
3562 
3563  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "dtls_bio_filter_write: %p, %d\n", (void *)in, inl);
3564  /* Forward data to the write BIO */
3565 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3566  ret = BIO_write(bio->next_bio, in, inl);
3567 #else
3568  ret = BIO_write(BIO_next(bio), in, inl);
3569 #endif
3570 
3572 
3573  /* Keep track of the packet, as we'll advertize them one by one after a pending check */
3574 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3575  filter = (dtls_bio_filter *)bio->ptr;
3576 #else
3577  filter = (dtls_bio_filter *)BIO_get_data(bio);
3578 #endif
3579 
3580  if (filter != NULL) {
3581  packet_list_t *node;
3582 
3583  switch_mutex_lock(filter->mutex);
3584  if (filter->unused) {
3585  node = filter->unused;
3586  node->next = NULL;
3587  filter->unused = filter->unused->next;
3588  } else {
3589  node = switch_core_alloc(filter->pool, sizeof(*node));
3590  }
3591 
3592  node->size = ret;
3593 
3594  if (filter->tail) {
3595  filter->tail->next = node;
3596  } else {
3597  filter->packets = node;
3598  }
3599 
3600  filter->tail = node;
3601 
3602  switch_mutex_unlock(filter->mutex);
3603  //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "New list length: %d\n", g_list_length(filter->packets));
3604  }
3605  return ret;
3606 }
#define SWITCH_CHANNEL_LOG
struct packet_list_s * next
Definition: switch_rtp.c:3486
switch_mutex_t * mutex
Definition: switch_rtp.c:3494
packet_list_t * packets
Definition: switch_rtp.c:3491
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
packet_list_t * tail
Definition: switch_rtp.c:3493
switch_byte_t in
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:684
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
packet_list_t * unused
Definition: switch_rtp.c:3492
switch_memory_pool_t * pool
Definition: switch_rtp.c:3495

◆ dtls_state_fail()

static int dtls_state_fail ( switch_rtp_t rtp_session,
switch_dtls_t dtls 
)
static

Definition at line 3312 of file switch_rtp.c.

References switch_rtp::session, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER, switch_channel_hangup, and switch_core_session_get_channel().

3313 {
3314  if (rtp_session->session) {
3315  switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
3317  }
3318 
3319  return -1;
3320 }
#define switch_channel_hangup(channel, hangup_cause)
Hangup a channel flagging it&#39;s state machine to end.
_Ret_ switch_channel_t * switch_core_session_get_channel(_In_ switch_core_session_t *session)
Retrieve a pointer to the channel object associated with a given session.
switch_core_session_t * session
Definition: switch_rtp.c:476

◆ dtls_state_handshake()

static int dtls_state_handshake ( switch_rtp_t rtp_session,
switch_dtls_t dtls 
)
static

Definition at line 3323 of file switch_rtp.c.

References DS_FAIL, DS_SETUP, dtls_set_state, rtp_type, switch_rtp::session, switch_dtls_s::ssl, SWITCH_CHANNEL_SESSION_LOG, switch_log_printf(), and SWITCH_LOG_WARNING.

3324 {
3325  int ret;
3326 
3327  if ((ret = SSL_do_handshake(dtls->ssl)) != 1){
3328  switch((ret = SSL_get_error(dtls->ssl, ret))){
3329  case SSL_ERROR_WANT_READ:
3330  case SSL_ERROR_WANT_WRITE:
3331  case SSL_ERROR_NONE:
3332  break;
3333  default:
3334  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "%s Handshake failure %d. This may happen when you use legacy DTLS v1.0 (legacyDTLS channel var is set) but endpoint requires DTLS v1.2.\n", rtp_type(rtp_session), ret);
3335  dtls_set_state(dtls, DS_FAIL);
3336  return -1;
3337  }
3338  }
3339 
3340  if (SSL_is_init_finished(dtls->ssl)) {
3341  dtls_set_state(dtls, DS_SETUP);
3342  }
3343 
3344  return 0;
3345 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
#define rtp_type(rtp_session)
Definition: switch_rtp.c:544
#define dtls_set_state(_dtls, _state)
Definition: switch_rtp.c:3212
switch_core_session_t * session
Definition: switch_rtp.c:476
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.

◆ dtls_state_names()

static const char* dtls_state_names ( dtls_state_t  s)
static

Definition at line 3203 of file switch_rtp.c.

References DS_INVALID.

3204 {
3205  if (s > DS_INVALID) {
3206  s = DS_INVALID;
3207  }
3208 
3209  return dtls_state_names_t[s];
3210 }
static const char * dtls_state_names_t[]
Definition: switch_rtp.c:3202

◆ dtls_state_ready()

static int dtls_state_ready ( switch_rtp_t rtp_session,
switch_dtls_t dtls 
)
static

Definition at line 3295 of file switch_rtp.c.

References switch_rtp::flags, switch_dtls_s::new_state, switch_rtp::session, switch_core_session_get_partner, switch_core_session_request_video_refresh, switch_core_session_rwunlock(), SWITCH_RTP_FLAG_VIDEO, and SWITCH_STATUS_SUCCESS.

3296 {
3297 
3298  if (dtls->new_state) {
3299  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
3300  switch_core_session_t *other_session;
3301 
3302  if (rtp_session->session && switch_core_session_get_partner(rtp_session->session, &other_session) == SWITCH_STATUS_SUCCESS) {
3304  switch_core_session_rwunlock(other_session);
3305  }
3306  }
3307  dtls->new_state = 0;
3308  }
3309  return 0;
3310 }
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:396
#define switch_core_session_request_video_refresh(_s)
Definition: switch_core.h:2892
#define switch_core_session_get_partner(_session, _partner)
Definition: switch_core.h:1028
void switch_core_session_rwunlock(_In_ switch_core_session_t *session)
Unlock a read or write lock on as given session.
switch_core_session_t * session
Definition: switch_rtp.c:476
uint8_t new_state
Definition: switch_rtp.c:286

◆ dtls_state_setup()

static int dtls_state_setup ( switch_rtp_t rtp_session,
switch_dtls_t dtls 
)
static

Definition at line 3218 of file switch_rtp.c.

References AES_CM_128_HMAC_SHA1_80, cert, cr_keylen, cr_kslen, cr_saltlen, secure_settings_s::crypto_type, DS_FAIL, DS_READY, switch_rtp::dtls, dtls_set_state, DTLS_TYPE_CLIENT, DTLS_TYPE_SERVER, secure_settings_s::local_raw_key, memset(), switch_dtls_s::remote_fp, secure_settings_s::remote_raw_key, switch_rtp::rtcp_dtls, rtp_type, switch_rtp::session, switch_dtls_s::ssl, SWITCH_CHANNEL_SESSION_LOG, switch_core_cert_extract_fingerprint(), switch_core_cert_verify(), SWITCH_LOG_ERROR, SWITCH_LOG_INFO, switch_log_printf(), switch_rtp_add_crypto_key(), SWITCH_RTP_CRYPTO_RECV, SWITCH_RTP_CRYPTO_RECV_RTCP, SWITCH_RTP_CRYPTO_SEND, SWITCH_RTP_CRYPTO_SEND_RTCP, and switch_dtls_s::type.

3219 {
3220  X509 *cert;
3221  switch_secure_settings_t ssec; /* Used just to wrap over params in a call to switch_rtp_add_crypto_key. */
3222  int r = 0;
3223 
3224  uint8_t raw_key_data[cr_kslen * 2];
3225  unsigned char local_key_buf[cr_kslen];
3226  unsigned char remote_key_buf[cr_kslen];
3227 
3228  memset(&ssec, 0, sizeof(ssec));
3229  memset(&raw_key_data, 0, cr_kslen * 2 * sizeof(uint8_t));
3230  memset(&local_key_buf, 0, cr_kslen * sizeof(unsigned char));
3231  memset(&remote_key_buf, 0, cr_kslen * sizeof(unsigned char));
3232 
3233  if ((dtls->type & DTLS_TYPE_SERVER)) {
3234  r = 1;
3235  } else if ((cert = SSL_get_peer_certificate(dtls->ssl))) {
3238  X509_free(cert);
3239  }
3240 
3241  if (!r) {
3242  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "%s Fingerprint Verification Failed!\n", rtp_type(rtp_session));
3243  dtls_set_state(dtls, DS_FAIL);
3244  return -1;
3245  } else {
3246  unsigned char *local_key, *remote_key, *local_salt, *remote_salt;
3247 
3248  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "%s Fingerprint Verified.\n", rtp_type(rtp_session));
3249 
3250 #ifdef HAVE_OPENSSL_DTLS_SRTP
3251  if (!SSL_export_keying_material(dtls->ssl, raw_key_data, sizeof(raw_key_data), "EXTRACTOR-dtls_srtp", 19, NULL, 0, 0)) {
3252  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "%s Key material export failure\n", rtp_type(rtp_session));
3253  dtls_set_state(dtls, DS_FAIL);
3254  return -1;
3255  }
3256 #else
3257  return -1;
3258 #endif
3259 
3260  if ((dtls->type & DTLS_TYPE_CLIENT)) {
3261  local_key = raw_key_data;
3262  remote_key = local_key + cr_keylen;
3263  local_salt = remote_key + cr_keylen;
3264  remote_salt = local_salt + cr_saltlen;
3265 
3266  } else {
3267  remote_key = raw_key_data;
3268  local_key = remote_key + cr_keylen;
3269  remote_salt = local_key + cr_keylen;
3270  local_salt = remote_salt + cr_saltlen;
3271  }
3272 
3273  memcpy(ssec.local_raw_key, local_key, cr_keylen);
3274  memcpy(ssec.local_raw_key + cr_keylen, local_salt, cr_saltlen);
3275 
3276  memcpy(ssec.remote_raw_key, remote_key, cr_keylen);
3277  memcpy(ssec.remote_raw_key + cr_keylen, remote_salt, cr_saltlen);
3278 
3280 
3281  if (dtls == rtp_session->rtcp_dtls && rtp_session->rtcp_dtls != rtp_session->dtls) {
3284  } else {
3285  switch_rtp_add_crypto_key(rtp_session, SWITCH_RTP_CRYPTO_SEND, 0, &ssec);
3286  switch_rtp_add_crypto_key(rtp_session, SWITCH_RTP_CRYPTO_RECV, 0, &ssec);
3287  }
3288  }
3289 
3290  dtls_set_state(dtls, DS_READY);
3291 
3292  return 0;
3293 }
dtls_fingerprint_t * remote_fp
Definition: switch_rtp.c:283
unsigned char remote_raw_key[SWITCH_RTP_MAX_CRYPTO_LEN]
#define SWITCH_CHANNEL_SESSION_LOG(x)
switch_status_t switch_rtp_add_crypto_key(switch_rtp_t *rtp_session, switch_rtp_crypto_direction_t direction, uint32_t index, switch_secure_settings_t *ssec)
Definition: switch_rtp.c:4062
int switch_core_cert_verify(dtls_fingerprint_t *fp)
#define cr_kslen
Definition: switch_rtp.c:3216
#define rtp_type(rtp_session)
Definition: switch_rtp.c:544
unsigned char local_raw_key[SWITCH_RTP_MAX_CRYPTO_LEN]
switch_dtls_t * dtls
Definition: switch_rtp.c:370
#define dtls_set_state(_dtls, _state)
Definition: switch_rtp.c:3212
int switch_core_cert_extract_fingerprint(X509 *x509, dtls_fingerprint_t *fp)
char * cert
Definition: switch_msrp.c:63
switch_core_session_t * session
Definition: switch_rtp.c:476
switch_dtls_t * rtcp_dtls
Definition: switch_rtp.c:371
dtls_type_t type
Definition: switch_rtp.c:287
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
#define cr_saltlen
Definition: switch_rtp.c:3215
switch_rtp_crypto_key_type_t crypto_type
memset(buf, 0, buflen)
#define cr_keylen
Definition: switch_rtp.c:3214

◆ enable_local_rtcp_socket()

static switch_status_t enable_local_rtcp_socket ( switch_rtp_t rtp_session,
const char **  err 
)
static

Definition at line 2720 of file switch_rtp.c.

References switch_rtp::flags, switch_rtp::from_addr, switch_rtp::local_host_str, switch_rtp::local_port, switch_rtp::pool, switch_rtp::rtcp_from_addr, switch_rtp::rtcp_local_addr, switch_rtp::rtcp_read_pollfd, switch_rtp::rtcp_sock_input, switch_rtp::session, SWITCH_CHANNEL_SESSION_LOG, switch_get_addr(), SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_POLLERR, SWITCH_POLLIN, SWITCH_RTP_FLAG_ENABLE_RTCP, SWITCH_SO_REUSEADDR, switch_sockaddr_get_family(), switch_sockaddr_get_port(), switch_sockaddr_info_get(), switch_socket_bind(), switch_socket_close(), switch_socket_create(), switch_socket_create_pollset(), switch_socket_opt_set(), SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, and SWITCH_UNSPEC.

Referenced by switch_rtp_activate_rtcp(), and switch_rtp_set_local_address().

2720  {
2721 
2722  const char *host = rtp_session->local_host_str;
2723  switch_port_t port = rtp_session->local_port;
2724  switch_socket_t *rtcp_new_sock = NULL, *rtcp_old_sock = NULL;
2726  char bufa[50];
2727 
2728  if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
2729  if (switch_sockaddr_info_get(&rtp_session->rtcp_local_addr, host, SWITCH_UNSPEC, port+1, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS) {
2730  *err = "RTCP Local Address Error!";
2731  goto done;
2732  }
2733 
2734  if (switch_socket_create(&rtcp_new_sock, switch_sockaddr_get_family(rtp_session->rtcp_local_addr), SOCK_DGRAM, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS) {
2735  *err = "RTCP Socket Error!";
2736  goto done;
2737  }
2738 
2740  *err = "RTCP Socket Error!";
2741  goto done;
2742  }
2743 
2744  if (switch_socket_bind(rtcp_new_sock, rtp_session->rtcp_local_addr) != SWITCH_STATUS_SUCCESS) {
2745  *err = "RTCP Bind Error!";
2746  goto done;
2747  }
2748 
2749  if (switch_sockaddr_info_get(&rtp_session->rtcp_from_addr, switch_get_addr(bufa, sizeof(bufa), rtp_session->from_addr),
2750  SWITCH_UNSPEC, switch_sockaddr_get_port(rtp_session->from_addr) + 1, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS) {
2751  *err = "RTCP From Address Error!";
2752  goto done;
2753  }
2754 
2755  rtcp_old_sock = rtp_session->rtcp_sock_input;
2756  rtp_session->rtcp_sock_input = rtcp_new_sock;
2757  rtcp_new_sock = NULL;
2758 
2759  switch_socket_create_pollset(&rtp_session->rtcp_read_pollfd, rtp_session->rtcp_sock_input, SWITCH_POLLIN | SWITCH_POLLERR, rtp_session->pool);
2760 
2761  done:
2762 
2763  if (*err) {
2764 
2765  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error allocating rtcp [%s]\n", *err);
2766  status = SWITCH_STATUS_FALSE;
2767  }
2768 
2769  if (rtcp_new_sock) {
2770  switch_socket_close(rtcp_new_sock);
2771  }
2772 
2773  if (rtcp_old_sock) {
2774  switch_socket_close(rtcp_old_sock);
2775  }
2776  } else {
2777  status = SWITCH_STATUS_FALSE;
2778  }
2779 
2780  return status;
2781 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
uint16_t switch_sockaddr_get_port(switch_sockaddr_t *sa)
Definition: switch_apr.c:1001
switch_sockaddr_t * rtcp_from_addr
Definition: switch_rtp.c:398
switch_status_t switch_socket_bind(switch_socket_t *sock, switch_sockaddr_t *sa)
Definition: switch_apr.c:742
switch_sockaddr_t * rtcp_local_addr
Definition: switch_rtp.c:330
switch_socket_t * rtcp_sock_input
Definition: switch_rtp.c:326
int32_t switch_sockaddr_get_family(switch_sockaddr_t *sa)
Definition: switch_apr.c:1006
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:396
switch_status_t switch_socket_create_pollset(switch_pollfd_t **poll, switch_socket_t *sock, int16_t flags, switch_memory_pool_t *pool)
Create a set of file descriptors to poll from a socket.
Definition: switch_apr.c:1117
char * local_host_str
Definition: switch_rtp.c:404
switch_status_t switch_socket_opt_set(switch_socket_t *sock, int32_t opt, int32_t on)
Definition: switch_apr.c:907
#define SWITCH_POLLERR
Definition: switch_apr.h:1339
uint16_t switch_port_t
#define SWITCH_POLLIN
Definition: switch_apr.h:1336
switch_memory_pool_t * pool
Definition: switch_rtp.c:397
switch_status_t switch_socket_create(switch_socket_t **new_sock, int family, int type, int protocol, switch_memory_pool_t *pool)
Definition: switch_apr.c:727
switch_status_t
Common return values.
switch_status_t switch_socket_close(switch_socket_t *sock)
Definition: switch_apr.c:737
#define SWITCH_UNSPEC
Definition: switch_apr.h:1022
switch_core_session_t * session
Definition: switch_rtp.c:476
#define SWITCH_SO_REUSEADDR
Definition: switch_apr.h:995
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_pollfd_t * rtcp_read_pollfd
Definition: switch_rtp.c:327
const char * switch_get_addr(char *buf, switch_size_t len, switch_sockaddr_t *in)
Definition: switch_apr.c:979
switch_port_t local_port
Definition: switch_rtp.c:420
struct fspr_socket_t switch_socket_t
Definition: switch_apr.h:1026
switch_sockaddr_t * from_addr
Definition: switch_rtp.c:398
switch_status_t switch_sockaddr_info_get(switch_sockaddr_t **sa, const char *hostname, int32_t family, switch_port_t port, int32_t flags, switch_memory_pool_t *pool)
Definition: switch_apr.c:839

◆ enable_remote_rtcp_socket()

static switch_status_t enable_remote_rtcp_socket ( switch_rtp_t rtp_session,
const char **  err 
)
static

Definition at line 2676 of file switch_rtp.c.

References switch_rtp::eff_remote_host_str, switch_rtp::flags, switch_rtp::pool, switch_rtp::remote_rtcp_port, switch_rtp::rtcp_local_addr, switch_rtp::rtcp_remote_addr, switch_rtp::rtcp_sock_input, switch_rtp::rtcp_sock_output, switch_rtp::session, SWITCH_CHANNEL_SESSION_LOG, switch_get_addr(), SWITCH_LOG_DEBUG, switch_log_printf(), SWITCH_RTP_FLAG_ENABLE_RTCP, switch_sockaddr_get_family(), switch_sockaddr_info_get(), switch_socket_close(), switch_socket_create(), SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, and SWITCH_UNSPEC.

Referenced by read_rtcp_packet(), and switch_rtp_activate_rtcp().

2676  {
2677 
2679 
2680  if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
2681 
2683  rtp_session->remote_rtcp_port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS || !rtp_session->rtcp_remote_addr) {
2684  *err = "RTCP Remote Address Error!";
2685  return SWITCH_STATUS_FALSE;
2686  } else {
2687  const char *host;
2688  char bufa[50];
2689 
2690  host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtcp_remote_addr);
2691 
2693  "Setting RTCP remote addr to %s:%d %d\n", host, rtp_session->remote_rtcp_port, rtp_session->rtcp_remote_addr->family);
2694  }
2695 
2696  if (rtp_session->rtcp_sock_input && switch_sockaddr_get_family(rtp_session->rtcp_remote_addr) ==
2698  rtp_session->rtcp_sock_output = rtp_session->rtcp_sock_input;
2699  } else {
2700 
2701  if (rtp_session->rtcp_sock_output && rtp_session->rtcp_sock_output != rtp_session->rtcp_sock_input) {
2702  switch_socket_close(rtp_session->rtcp_sock_output);
2703  }
2704 
2705  if ((status = switch_socket_create(&rtp_session->rtcp_sock_output,
2707  SOCK_DGRAM, 0, rtp_session->pool)) != SWITCH_STATUS_SUCCESS) {
2708  *err = "RTCP Socket Error!";
2709  }
2710  }
2711 
2712  } else {
2713  *err = "RTCP NOT ACTIVE!";
2714  }
2715 
2716  return status;
2717 
2718 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
switch_sockaddr_t * rtcp_local_addr
Definition: switch_rtp.c:330
switch_port_t remote_rtcp_port
Definition: switch_rtp.c:423
switch_socket_t * rtcp_sock_input
Definition: switch_rtp.c:326
int32_t switch_sockaddr_get_family(switch_sockaddr_t *sa)
Definition: switch_apr.c:1006
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:396
switch_memory_pool_t * pool
Definition: switch_rtp.c:397
switch_socket_t * rtcp_sock_output
Definition: switch_rtp.c:326
switch_status_t switch_socket_create(switch_socket_t **new_sock, int family, int type, int protocol, switch_memory_pool_t *pool)
Definition: switch_apr.c:727
switch_status_t
Common return values.
switch_status_t switch_socket_close(switch_socket_t *sock)
Definition: switch_apr.c:737
#define SWITCH_UNSPEC
Definition: switch_apr.h:1022
switch_core_session_t * session
Definition: switch_rtp.c:476
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
const char * switch_get_addr(char *buf, switch_size_t len, switch_sockaddr_t *in)
Definition: switch_apr.c:979
switch_sockaddr_t * rtcp_remote_addr
Definition: switch_rtp.c:344
switch_status_t switch_sockaddr_info_get(switch_sockaddr_t **sa, const char *hostname, int32_t family, switch_port_t port, int32_t flags, switch_memory_pool_t *pool)
Definition: switch_apr.c:839
char * eff_remote_host_str
Definition: switch_rtp.c:406

◆ free_dtls()

static void free_dtls ( switch_dtls_t **  dtlsp)
static

Definition at line 3347 of file switch_rtp.c.

References switch_dtls_s::ssl, and switch_dtls_s::ssl_ctx.

Referenced by switch_rtp_del_dtls(), and switch_rtp_destroy().

3348 {
3349  switch_dtls_t *dtls;
3350 
3351  if (!dtlsp) {
3352  return;
3353  }
3354 
3355  dtls = *dtlsp;
3356  *dtlsp = NULL;
3357 
3358  if (dtls->ssl) {
3359  SSL_free(dtls->ssl);
3360  }
3361 
3362  if (dtls->ssl_ctx) {
3363  SSL_CTX_free(dtls->ssl_ctx);
3364  }
3365 }
SSL_CTX * ssl_ctx
Definition: switch_rtp.c:277

◆ get_next_write_ts()

static uint8_t get_next_write_ts ( switch_rtp_t rtp_session,
uint32_t  timestamp 
)
static

Definition at line 1547 of file switch_rtp.c.

References switch_rtp::last_write_ts, RTP_BUG_NEVER_SEND_MARKER, RTP_BUG_SEND_LINEAR_TIMESTAMPS, switch_rtp::rtp_bugs, switch_timer::samplecount, switch_rtp::samples_per_interval, switch_core_timer_next(), SWITCH_RTP_FLAG_USE_TIMER, switch_rtp_test_flag(), switch_rtp::ts, and switch_rtp::write_timer.

Referenced by check_rtcp_and_ice(), and rtp_common_write().

1548 {
1549  uint8_t m = 0, changed = 0;
1550 
1551  if (!(rtp_session->rtp_bugs & RTP_BUG_SEND_LINEAR_TIMESTAMPS)) {
1552  if (timestamp) {
1553  rtp_session->ts = (uint32_t) timestamp;
1554  changed++;
1555  } else if (switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER)) {
1556  switch_core_timer_next(&rtp_session->write_timer);
1557  rtp_session->ts = rtp_session->write_timer.samplecount;
1558  changed++;
1559  }
1560  }
1561 
1562  if (!changed) {
1563  rtp_session->ts = rtp_session->last_write_ts + rtp_session->samples_per_interval;
1564  } else {
1565  /* Send marker bit if timestamp is lower/same as before (resetted/new timer) */
1566  if (abs((int32_t)(rtp_session->ts - rtp_session->last_write_ts)) > rtp_session->samples_per_interval
1567  && !(rtp_session->rtp_bugs & RTP_BUG_NEVER_SEND_MARKER)) {
1568  m++;
1569  }
1570  }
1571 
1572  return m;
1573 }
uint32_t ts
Definition: switch_rtp.c:384
switch_status_t switch_core_timer_next(switch_timer_t *timer)
Wait for one cycle on an existing timer.
uint32_t samples_per_interval
Definition: switch_rtp.c:409
uint32_t last_write_ts
Definition: switch_rtp.c:386
switch_timer_t write_timer
Definition: switch_rtp.c:435
switch_rtp_bug_flag_t rtp_bugs
Definition: switch_rtp.c:453
uint32_t switch_rtp_test_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flags)
Test an RTP Flag.
Definition: switch_rtp.c:5429

◆ get_recv_payload()

static int get_recv_payload ( switch_rtp_t rtp_session)
static

Definition at line 5827 of file switch_rtp.c.

References payload_map_s::allocated, switch_rtp::flag_mutex, payload_map_s::negotiated, payload_map_s::next, switch_rtp::pmaps, payload_map_s::pt, switch_mutex_lock(), and switch_mutex_unlock().

Referenced by read_rtp_packet().

5828 {
5829  int r = -1;
5830 
5831  if (rtp_session->pmaps && *rtp_session->pmaps) {
5832  payload_map_t *pmap;
5833 
5834  switch_mutex_lock(rtp_session->flag_mutex);
5835 
5836  for (pmap = *rtp_session->pmaps; pmap && pmap->allocated; pmap = pmap->next) {
5837  if (pmap->negotiated) {
5838  r = pmap->pt;
5839  break;
5840  }
5841  }
5842  switch_mutex_unlock(rtp_session->flag_mutex);
5843  }
5844 
5845  return r;
5846 }
uint8_t allocated
switch_mutex_t * flag_mutex
Definition: switch_rtp.c:430
struct payload_map_s * next
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
uint8_t negotiated
switch_payload_t pt
payload_map_t ** pmaps
Definition: switch_rtp.c:477

◆ handle_ice()

static void handle_ice ( switch_rtp_t rtp_session,
switch_rtp_ice_t ice,
void *  data,
switch_size_t  len 
)
static

Definition at line 956 of file switch_rtp.c.

References switch_rtp_ice_t::addr, switch_rtp::adj_window, switch_rtp::auto_adj_used, buf, calc_elapsed(), ice_s::cand_idx, switch_rtp_ice_t::cand_responsive, icand_s::cand_type, ice_s::cands, ice_s::chosen, switch_stun_error_code_t::code, icand_s::con_addr, icand_s::con_port, DS_READY, switch_rtp::dtls, switch_rtp::elapsed_adj, switch_rtp::elapsed_media, switch_rtp::elapsed_stun, switch_rtp::first_stun, switch_rtp::flags, switch_core_session_message::from, switch_rtp::from_addr, switch_stun_packet_t::header, switch_rtp::ice, ICE_CONTROLLED, ICE_GOOGLE_JINGLE, switch_rtp::ice_mutex, ice_out(), switch_rtp_ice_t::ice_params, switch_rtp_ice_t::ice_user, ICE_VANILLA, icecmp(), switch_stun_packet_header_t::id, switch_rtp_ice_t::initializing, ip, IPR_RTP, switch_rtp::last_adj, switch_rtp_ice_t::last_ok, switch_rtp_ice_t::last_sent_id, switch_rtp::last_stun, switch_stun_packet_header_t::length, MEDIA_TOO_LONG, memset(), switch_core_session_message::message_id, switch_rtp_ice_t::missed_count, switch_stun_error_code_t::number, switch_core_session_message::numeric_arg, switch_rtp_ice_t::pass, switch_core_session_message::pointer_arg, switch_rtp::pool, icand_s::priority, switch_rtp_ice_t::proto, READ_DEC, READ_INC, switch_rtp_ice_t::ready, switch_rtp::remote_addr, switch_rtp::remote_port, icand_s::responsive, switch_rtp_ice_t::rready, switch_rtp::rtcp_from_addr, switch_rtp::rtcp_ice, switch_rtp::rtcp_sock_output, RTP_BUG_ALWAYS_AUTO_ADJUST, switch_rtp::rtp_bugs, rtp_session_name, rtp_type, switch_rtp::session, switch_rtp::sock_output, switch_dtls_s::state, STUN_TOO_LONG, STUN_USERNAME_MAX_SIZE, switch_channel_get_name(), switch_channel_get_variable, SWITCH_CHANNEL_SESSION_LOG, switch_channel_set_variable, switch_cmp_addr(), switch_core_session_get_channel(), switch_core_session_receive_message, switch_core_session_video_reinit(), SWITCH_DECLARE, SWITCH_FALSE, switch_get_addr(), SWITCH_LOG_DEBUG, SWITCH_LOG_DEBUG5, SWITCH_LOG_DEBUG6, SWITCH_LOG_DEBUG8, SWITCH_LOG_ERROR, SWITCH_LOG_INFO, SWITCH_LOG_NOTICE, switch_log_printf(), SWITCH_LOG_WARNING, SWITCH_MESSAGE_INDICATE_STUN_ERROR, switch_micro_time_now(), switch_mutex_lock(), switch_mutex_unlock(), switch_rtp_change_ice_dest(), switch_rtp_clear_flag(), SWITCH_RTP_FLAG_AUTOADJ, SWITCH_RTP_FLAG_RTCP_MUX, SWITCH_RTP_FLAG_VIDEO, switch_rtp_ready(), switch_rtp_set_flag(), switch_rtp_set_remote_address(), switch_snprintf(), switch_sockaddr_get_port(), switch_sockaddr_info_get(), switch_socket_sendto(), SWITCH_STATUS_SUCCESS, SWITCH_STUN_ATTR_ERROR_CODE, SWITCH_STUN_ATTR_MAPPED_ADDRESS, SWITCH_STUN_ATTR_PRIORITY, SWITCH_STUN_ATTR_USE_CAND, SWITCH_STUN_ATTR_USERNAME, SWITCH_STUN_ATTR_XOR_MAPPED_ADDRESS, switch_stun_attribute_padded_length, SWITCH_STUN_BINDING_ERROR_RESPONSE, SWITCH_STUN_BINDING_REQUEST, SWITCH_STUN_BINDING_RESPONSE, switch_stun_packet_attribute_add_fingerprint(), switch_stun_packet_attribute_add_integrity(), switch_stun_packet_attribute_add_username(), switch_stun_packet_attribute_add_xor_binded_address(), switch_stun_packet_attribute_get_mapped_address(), switch_stun_packet_attribute_get_username(), switch_stun_packet_attribute_get_xor_mapped_address(), switch_stun_packet_build_header(), switch_stun_packet_first_attribute, switch_stun_packet_length, switch_stun_packet_next_attribute, switch_stun_packet_parse(), SWITCH_STUN_TYPE_ATTRIBUTE, SWITCH_STUN_TYPE_PACKET_TYPE, switch_stun_value_to_name(), SWITCH_TRUE, SWITCH_UNSPEC, switch_stun_packet_header_t::type, switch_stun_packet_attribute_t::type, switch_rtp_ice_t::type, icand_s::use_candidate, switch_rtp_ice_t::user_ice, switch_stun_packet_attribute_t::value, WRITE_DEC, WRITE_INC, and zstr.

Referenced by process_rtcp_packet(), read_rtcp_packet(), and read_rtp_packet().

957 {
958  switch_stun_packet_t *packet;
960  void *end_buf;
961  char username[STUN_USERNAME_MAX_SIZE] = { 0 };
962  unsigned char buf[1500] = { 0 };
963  switch_size_t cpylen = len;
964  int xlen = 0;
965  int ok = 1;
966  uint32_t *pri = NULL;
967  int is_rtcp = ice == &rtp_session->rtcp_ice;
968  switch_channel_t *channel;
969  int i;
970  switch_sockaddr_t *from_addr = rtp_session->from_addr;
971  const char *from_host = NULL;
972  switch_port_t from_port = 0;
973  char faddr_buf[80] = "";
974 
975  if (is_rtcp) {
976  from_addr = rtp_session->rtcp_from_addr;
977  }
978 
979  from_host = switch_get_addr(faddr_buf, sizeof(faddr_buf), from_addr);
980  from_port = switch_sockaddr_get_port(from_addr);
981 
982  //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
983  // switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "WTF OK %s CALL\n", rtp_type(rtp_session));
984  //}
985 
986  if (!switch_rtp_ready(rtp_session) || zstr(ice->user_ice) || zstr(ice->ice_user)) {
987  return;
988  }
989 
990  READ_INC(rtp_session);
991  WRITE_INC(rtp_session);
992 
993  switch_mutex_lock(rtp_session->ice_mutex);
994 
995  if (!switch_rtp_ready(rtp_session)) {
996  goto end;
997  }
998 
999  if (cpylen > sizeof(buf)) {
1000  cpylen = sizeof(buf);
1001  }
1002 
1003  channel = switch_core_session_get_channel(rtp_session->session);
1004 
1005  memcpy(buf, data, cpylen);
1006  packet = switch_stun_packet_parse(buf, (uint32_t)cpylen);
1007  if (!packet) {
1008  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Invalid STUN/ICE packet received %ld bytes\n", (long)cpylen);
1009  goto end;
1010 
1011  }
1012 
1013  rtp_session->last_stun = switch_micro_time_now();
1014 
1015  if (!rtp_session->first_stun) {
1016  rtp_session->first_stun = rtp_session->last_stun;
1017  }
1018 
1019  calc_elapsed(rtp_session, ice);
1020 
1021  end_buf = buf + ((sizeof(buf) > packet->header.length) ? packet->header.length : sizeof(buf));
1022 
1023  switch_stun_packet_first_attribute(packet, attr);
1024  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG8, "%s STUN PACKET TYPE: %s\n",
1026  do {
1027  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG8, "|---: %s STUN ATTR %d %x %s\n", rtp_type(rtp_session), attr->type, attr->type,
1029 
1030  switch (attr->type) {
1032  {
1033  ice->rready = 1;
1034  for (i = 0; i < ice->ice_params->cand_idx[ice->proto]; i++) {
1035  if (!strcmp(ice->ice_params->cands[i][ice->proto].con_addr, from_host) && ice->ice_params->cands[i][ice->proto].con_port == from_port) {
1036  ice->ice_params->cands[i][ice->proto].use_candidate = 1;
1037  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG6, "Got USE-CANDIDATE on %s:%d\n", ice->ice_params->cands[i][ice->proto].con_addr, ice->ice_params->cands[i][ice->proto].con_port);
1038  }
1039  }
1040  }
1041  break;
1043  {
1045  uint32_t code = (err->code * 100) + err->number;
1046 
1047  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "%s got %s stun binding response %u\n",
1048  rtp_session_name(rtp_session),
1049  rtp_type(rtp_session),
1050  code
1051  );
1052 
1053  if ((ice->type & ICE_VANILLA) && code == 487) {
1054  if ((ice->type & ICE_CONTROLLED)) {
1055  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "%s STUN Changing role to CONTROLLING\n", rtp_type(rtp_session));
1056  ice->type &= ~ICE_CONTROLLED;
1057  } else {
1058  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "%s STUN Changing role to CONTROLLED\n", rtp_type(rtp_session));
1059  ice->type |= ICE_CONTROLLED;
1060  }
1062  }
1063 
1064  }
1065  break;
1067  {
1068  char ip[50];
1069  uint16_t port;
1070  switch_stun_packet_attribute_get_mapped_address(attr, ip, sizeof(ip), &port);
1071  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG8, "|------: %s:%d\n", ip, port);
1072  }
1073  break;
1075  {
1076  char ip[50];
1077  uint16_t port;
1078  switch_stun_packet_attribute_get_xor_mapped_address(attr, &packet->header, ip, sizeof(ip), &port);
1079  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG8, "|------: %s:%d\n", ip, port);
1080  }
1081  break;
1083  {
1084  switch_stun_packet_attribute_get_username(attr, username, sizeof(username));
1085  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG8, "|------: %s\n", username);
1086  }
1087  break;
1088 
1090  {
1091  uint32_t priority = 0;
1092  pri = (uint32_t *) attr->value;
1093  priority = ntohl(*pri);
1094  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG8, "|------: %u\n", priority);
1095  ok = priority == ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].priority;
1096  }
1097  break;
1098  }
1099 
1100  if (!switch_stun_packet_next_attribute(attr, end_buf)) {
1101  break;
1102  }
1103 
1104  xlen += 4 + switch_stun_attribute_padded_length(attr);
1105  } while (xlen <= packet->header.length);
1106 
1107  if ((ice->type & ICE_GOOGLE_JINGLE) && ok) {
1108  ok = !strcmp(ice->user_ice, username);
1109  }
1110 
1112  goto end;
1113  }
1114 
1115  if ((ice->type & ICE_VANILLA)) {
1116  if (!ok) ok = !memcmp(packet->header.id, ice->last_sent_id, 12);
1117 
1118  if (packet->header.type == SWITCH_STUN_BINDING_RESPONSE) {
1119  ok = 1;
1120 
1121  if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
1122  rtp_session->ice.rready = 1;
1123  rtp_session->rtcp_ice.rready = 1;
1124  } else {
1125  ice->rready = 1;
1126  }
1127 
1128  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG6, "Received STUN Binding Response from %s\n", from_host);
1129 
1130  if (ice->ice_params) {
1131  for (i = 0; i < ice->ice_params->cand_idx[ice->proto]; i++) {
1132  if (!strcmp(ice->ice_params->cands[i][ice->proto].con_addr, from_host) && ice->ice_params->cands[i][ice->proto].con_port == from_port) {
1133  ice->ice_params->cands[i][ice->proto].responsive = 1;
1134  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG5, "Marked ICE candidate %s:%d as responsive\n", ice->ice_params->cands[i][ice->proto].con_addr, ice->ice_params->cands[i][ice->proto].con_port);
1135  if (!strcmp(ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_addr, from_host) && ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port == from_port) {
1136  ice->cand_responsive = 1;
1137  ice->initializing = 0;
1138  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG5, "Chosen ICE candidate %s:%d is responsive\n", ice->ice_params->cands[i][ice->proto].con_addr, ice->ice_params->cands[i][ice->proto].con_port);
1139  }
1140  }
1141  }
1142  }
1143 
1144  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
1146  }
1147  }
1148 
1149  if (!ok && ice == &rtp_session->ice && rtp_session->rtcp_ice.ice_params && pri &&
1150  *pri == rtp_session->rtcp_ice.ice_params->cands[rtp_session->rtcp_ice.ice_params->chosen[1]][1].priority) {
1151  ice = &rtp_session->rtcp_ice;
1152  ok = 1;
1153  }
1154 
1155  if (!zstr(username)) {
1156  if (!icecmp(username, ice)) {
1157  ok = 1;
1158  } else if(!zstr(rtp_session->rtcp_ice.user_ice) && !icecmp(username, &rtp_session->rtcp_ice)) {
1159  ice = &rtp_session->rtcp_ice;
1160  ok = 1;
1161  }
1162  }
1163 
1164  if (ok) {
1165  ice->missed_count = 0;
1166  } else {
1167  switch_rtp_ice_t *icep[2] = { &rtp_session->ice, &rtp_session->rtcp_ice };
1168  switch_port_t port = 0;
1169  char *host = NULL;
1170 
1171  if (rtp_session->elapsed_stun > STUN_TOO_LONG && pri) {
1172  int i, j;
1173  uint32_t old;
1174  //const char *tx_host;
1175  const char *old_host, *err = NULL;
1176  //char bufa[50];
1177  char bufb[50];
1178  char adj_port[6];
1179  switch_channel_t *channel = NULL;
1180 
1181 
1182  ice->missed_count++;
1183  //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "missed %d\n", ice->missed_count);
1184 
1185 
1186  if (rtp_session->session) {
1187  channel = switch_core_session_get_channel(rtp_session->session);
1188  }
1189 
1190  //ice->ice_params->cands[ice->ice_params->chosen][ice->proto].priority;
1191  for (j = 0; j < 2; j++) {
1192  if (!icep[j] || !icep[j]->ice_params) {
1193  continue;
1194  }
1195  for (i = 0; i < icep[j]->ice_params->cand_idx[icep[j]->proto]; i++) {
1196  if (icep[j]->ice_params && icep[j]->ice_params->cands[i][icep[j]->proto].priority == *pri) {
1197  if (j == IPR_RTP) {
1198  icep[j]->ice_params->chosen[j] = i;
1199  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "Change candidate index to %d\n", i);
1200  }
1201 
1202  ice = icep[j];
1203  ok = 1;
1204 
1205  if (j != IPR_RTP) {
1206  break;
1207  }
1208 
1209  old = rtp_session->remote_port;
1210 
1211  //tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->from_addr);
1212  old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
1213 
1214  host = ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_addr;
1215  port = ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port;
1216 
1217  if (!host || !port) {
1218  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error setting remote host!\n");
1219  goto end;
1220  }
1221 
1223  "%s ICE Auto Changing port from %s:%u to %s:%u\n", rtp_type(rtp_session), old_host, old, host, port);
1224 
1225 
1226  if (channel) {
1227  switch_channel_set_variable(channel, "remote_media_ip_reported", switch_channel_get_variable(channel, "remote_media_ip"));
1228  switch_channel_set_variable(channel, "remote_media_ip", host);
1229  switch_channel_set_variable(channel, "rtp_auto_adjust_ip", host);
1230  switch_snprintf(adj_port, sizeof(adj_port), "%u", port);
1231  switch_channel_set_variable(channel, "remote_media_port_reported", switch_channel_get_variable(channel, "remote_media_port"));
1232  switch_channel_set_variable(channel, "remote_media_port", adj_port);
1233  switch_channel_set_variable(channel, "rtp_auto_adjust_port", adj_port);
1234  switch_channel_set_variable(channel, "rtp_auto_candidate_adjust", "true");
1235  }
1236  rtp_session->auto_adj_used = 1;
1237 
1238 
1239  switch_rtp_set_remote_address(rtp_session, host, port, 0, SWITCH_FALSE, &err);
1240  if (switch_sockaddr_info_get(&ice->addr, host, SWITCH_UNSPEC, port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS ||
1241  !ice->addr) {
1242  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error setting remote host!\n");
1243  goto end;
1244  }
1245 
1246  if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) {
1248  } else {
1250  }
1251 
1252  }
1253  }
1254  }
1255  }
1256  }
1257  }
1258 
1259  if (ice->missed_count > 5 && !(ice->type & ICE_GOOGLE_JINGLE)) {
1260  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "missed too many: %d, looking for new ICE dest.\n",
1261  ice->missed_count);
1262  ice->rready = 0;
1263  ice->cand_responsive = 0;
1264  ok = 1;
1265  }
1266 
1267 
1268  //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] || 1) {
1269  // switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "WTF OK %s %d\n", rtp_type(rtp_session), ok);
1270  //}
1271 
1272  if (ok) {
1273  const char *host2 = NULL;
1274  switch_port_t port2 = 0;
1275  char buf2[80] = "";
1276 
1277  if (packet->header.type == SWITCH_STUN_BINDING_REQUEST) {
1278  uint8_t stunbuf[512];
1279  switch_stun_packet_t *rpacket;
1280  const char *remote_ip;
1281  switch_size_t bytes;
1282  char ipbuf[50];
1283  switch_socket_t *sock_output = rtp_session->sock_output;
1284  uint8_t do_adj = 0;
1286  int cmp = 0;
1287  int cur_idx = -1, is_relay = 0, is_responsive = 0, use_candidate = 0;
1288 
1289  if (is_rtcp) {
1290  sock_output = rtp_session->rtcp_sock_output;
1291  }
1292 
1293  if (!ice->ready) {
1294  ice->ready = 1;
1295  }
1296 
1297  memset(stunbuf, 0, sizeof(stunbuf));
1299 
1300  if ((ice->type & ICE_GOOGLE_JINGLE)) {
1301  switch_stun_packet_attribute_add_username(rpacket, username, (uint16_t)strlen(username));
1302  }
1303 
1304  remote_ip = switch_get_addr(ipbuf, sizeof(ipbuf), from_addr);
1305 
1306  switch_stun_packet_attribute_add_xor_binded_address(rpacket, (char *) remote_ip, switch_sockaddr_get_port(from_addr), from_addr->family);
1307 
1308  if ((ice->type & ICE_VANILLA)) {
1311  }
1312 
1313  bytes = switch_stun_packet_length(rpacket);
1314 
1315  host2 = switch_get_addr(buf2, sizeof(buf2), ice->addr);
1316  port2 = switch_sockaddr_get_port(ice->addr);
1317  cmp = switch_cmp_addr(from_addr, ice->addr, SWITCH_FALSE);
1318 
1319  for (i = 0; i < ice->ice_params->cand_idx[ice->proto]; i++) {
1320  if (!strcmp(ice->ice_params->cands[i][ice->proto].con_addr, from_host) && ice->ice_params->cands[i][ice->proto].con_port == from_port) {
1321  if (!strcasecmp(ice->ice_params->cands[i][ice->proto].cand_type, "relay")) {
1322  is_relay = 1;
1323  }
1324 
1325  if (ice->ice_params->cands[i][ice->proto].responsive) {
1326  is_responsive = 1;
1327  }
1328 
1329  if (ice->ice_params->cands[i][ice->proto].use_candidate) {
1330  use_candidate = 1;
1331  }
1332  }
1333  }
1334 
1336  "%s %s STUN from %s:%d %s is_relay: %d is_responsive: %d use_candidate: %d ready: %d, rready: %d\n", switch_channel_get_name(channel), rtp_type(rtp_session), from_host, from_port, cmp ? "EXPECTED" : "IGNORED",
1337  is_relay, is_responsive, use_candidate, ice->ready, ice->rready);
1338 
1339  if (ice->initializing && !cmp) {
1340  if (!rtp_session->adj_window && (!ice->ready || !ice->rready || (!rtp_session->dtls || rtp_session->dtls->state != DS_READY))) {
1341  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG5, "%s %s %s ICE set ADJUST window to 10 seconds on binding request from %s:%d (is_relay: %d, is_responsivie: %d, use_candidate: %d) Current cand: %s:%d typ: %s\n",
1342  switch_channel_get_name(channel), rtp_type(rtp_session), is_rtcp ? "rtcp" : "rtp", from_host, from_port, is_relay, is_responsive, use_candidate,
1343  ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_addr, ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port, ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].cand_type);
1344 
1345  rtp_session->adj_window = now + 10000000;
1346  }
1347 
1348  if (rtp_session->adj_window) {
1349  if (rtp_session->adj_window > now) {
1350  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG5, "%s %s %s ICE check: %d >= 3000 or window closed and not from relay on binding request from %s:%d (is_relay: %d, is_responsive: %d, use_candidate: %d) Current cand: %s:%d typ: %s\n",
1351  switch_channel_get_name(channel), rtp_type(rtp_session), is_rtcp ? "rtcp" : "rtp", rtp_session->elapsed_stun, from_host, from_port, is_relay, is_responsive, use_candidate,
1352  ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_addr, ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port, ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].cand_type);
1353 
1354  if (!is_relay && (rtp_session->elapsed_stun >= 3000 || rtp_session->adj_window == (now + 10000000))) {
1355  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG5, "%s %s %s ICE ADJUST HIT 1 on binding request from %s:%d (is_relay: %d, is_responsive: %d, use_candidate: %d) Current cand: %s:%d typ: %s\n",
1356  switch_channel_get_name(channel), rtp_type(rtp_session), is_rtcp ? "rtcp" : "rtp", from_host, from_port, is_relay, is_responsive, use_candidate,
1357  ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_addr, ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port, ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].cand_type);
1358 
1359  do_adj++;
1360  rtp_session->last_adj = now;
1361  }
1362  } else {
1363  rtp_session->adj_window = 0;
1364  }
1365  }
1366 
1367  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG5, "%s %s %s ICE CHECK SAME IP DIFFT PORT %d %d on binding request from %s:%d (is_relay: %d, is_responsive: %d, use_candidate: %d) Current cand: %s:%d typ: %s\n",
1368  switch_channel_get_name(channel), rtp_type(rtp_session), is_rtcp ? "rtcp" : "rtp",ice->initializing, switch_cmp_addr(from_addr, ice->addr, SWITCH_TRUE), from_host, from_port, is_relay, is_responsive, use_candidate,
1369  ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_addr, ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port, ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].cand_type);
1370 
1371  if (!do_adj && (switch_cmp_addr(from_addr, ice->addr, SWITCH_TRUE) || use_candidate)) {
1372  do_adj++;
1373  rtp_session->last_adj = now;
1374 
1375  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG5, "%s %s %s ICE ADJUST HIT 2 on binding request from %s:%d (is_relay: %d, is_responsive: %d, use_candidate: %d) Current cand: %s:%d typ: %s\n",
1376  switch_channel_get_name(channel), rtp_type(rtp_session), is_rtcp ? "rtcp" : "rtp", from_host, from_port, is_relay, is_responsive, use_candidate,
1377  ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_addr, ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port, ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].cand_type);
1378  }
1379  }
1380 
1381  if (cmp) {
1382  ice->last_ok = now;
1383  } else if (!do_adj) {
1384  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG5, "ICE %d/%d dt:%d i:%d i2:%d cmp:%d\n", rtp_session->elapsed_stun, rtp_session->elapsed_media, (rtp_session->dtls && rtp_session->dtls->state != DS_READY), !ice->ready, !ice->rready, switch_cmp_addr(from_addr, ice->addr, SWITCH_TRUE));
1385 
1386  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG5, "%s %s %s ICE ADJUST ELAPSED vs 1000 %d on binding request from %s:%d (is_relay: %d, is_responsive: %d, use_candidate: %d) Current cand: %s:%d typ: %s\n",
1387  switch_channel_get_name(channel), rtp_type(rtp_session), is_rtcp ? "rtcp" : "rtp" ,rtp_session->elapsed_adj, from_host, from_port, is_relay, is_responsive, use_candidate,
1388  ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_addr, ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port, ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].cand_type);
1389 
1390  if (rtp_session->elapsed_adj > 1000) {
1391  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG5, "%s %s %s ICE IF DTLS NOT READY or %d >= 3000 or media too long %d or stun too long %d on binding request from %s:%d (is_relay: %d, is_responsive: %d, use_candidate: %d) Current cand: %s:%d typ: %s\n",
1392  switch_channel_get_name(channel), rtp_type(rtp_session), is_rtcp ? "rtcp" : "rtp", rtp_session->elapsed_stun, rtp_session->elapsed_media >= MEDIA_TOO_LONG,
1393  rtp_session->elapsed_stun >= STUN_TOO_LONG, from_host, from_port, is_relay, is_responsive, use_candidate,
1394  ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_addr, ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port, ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].cand_type);
1395 
1396  if (!is_relay && ((rtp_session->dtls && rtp_session->dtls->state != DS_READY) ||
1397  ((!ice->ready || !ice->rready) && (rtp_session->elapsed_stun >= 3000 || switch_cmp_addr(from_addr, ice->addr, SWITCH_TRUE))))) {
1398 
1399  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG5, "%s %s %s ICE ADJUST HIT 3 on binding request from %s:%d (is_relay: %d, is_responsive: %d, use_candidate: %d) Current cand: %s:%d typ: %s\n",
1400  switch_channel_get_name(channel), rtp_type(rtp_session), is_rtcp ? "rtcp" : "rtp", from_host, from_port, is_relay, is_responsive, use_candidate,
1401  ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_addr, ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port, ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].cand_type);
1402 
1403  do_adj++;
1404  rtp_session->last_adj = now;
1405  } else if (is_relay && ice->initializing && rtp_session->elapsed_stun >= 1000) {
1406 
1407  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG5, "%s %s %s ICE ADJUST HIT 4 (FLIP TO TURN) on binding request from %s:%d (is_relay: %d, is_responsive: %d, use_candidate: %d) Current cand: %s:%d typ: %s\n",
1408  switch_channel_get_name(channel), rtp_type(rtp_session), is_rtcp ? "rtcp" : "rtp", from_host, from_port, is_relay, is_responsive, use_candidate,
1409  ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_addr, ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port, ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].cand_type);
1410 
1411  do_adj++;
1412  rtp_session->last_adj = now;
1413  } else if ((ice->initializing && rtp_session->elapsed_stun >= 3000) ||
1414  (rtp_session->elapsed_media >= MEDIA_TOO_LONG || rtp_session->elapsed_stun >= STUN_TOO_LONG)) {
1415 
1416  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG5, "%s %s %s ICE ADJUST HIT 5 on binding request from %s:%d (is_relay: %d, is_responsive: %d, use_candidate: %d) Current cand: %s:%d typ: %s\n",
1417  switch_channel_get_name(channel), rtp_type(rtp_session), is_rtcp ? "rtcp" : "rtp", from_host, from_port, is_relay, is_responsive, use_candidate,
1418  ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_addr, ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port, ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].cand_type);
1419 
1420  do_adj++;
1421  rtp_session->last_adj = now;
1422  }
1423 
1424  for (i = 0; i < ice->ice_params->cand_idx[ice->proto]; i++) {
1425  if (!strcmp(ice->ice_params->cands[i][ice->proto].con_addr, from_host)) {
1426  cur_idx = i;
1427  }
1428  }
1429  }
1430  }
1431 
1432  if ((ice->type & ICE_VANILLA) && ice->ice_params && do_adj) {
1433  ice->missed_count = 0;
1434  ice->rready = 1;
1435 
1436  if (cur_idx > -1) {
1437  ice->ice_params->chosen[ice->proto] = cur_idx;
1438  }
1439 
1441  "Auto Changing %s stun/%s/dtls port from %s:%u to %s:%u idx:%d\n", rtp_type(rtp_session), is_rtcp ? "rtcp" : "rtp",
1442  host2, port2,
1443  from_host, from_port, cur_idx);
1444 
1445  switch_rtp_change_ice_dest(rtp_session, ice, from_host, from_port);
1446 
1447  ice->cand_responsive = is_responsive;
1448  if (ice->cand_responsive) {
1449  ice->initializing = 0;
1450  }
1451 
1452  ice->last_ok = now;
1453  }
1454  //if (cmp) {
1455  switch_socket_sendto(sock_output, from_addr, 0, (void *) rpacket, &bytes);
1456  //}
1457 
1458  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG6, "Send STUN Binding Response to %s:%u\n", from_host, from_port);
1459 
1460  if (ice->initializing && !is_responsive) {
1461  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG5, "Send STUN Binding Request on ICE candidate still unresponsive to %s:%u\n", from_host, from_port);
1462  if (ice_out(rtp_session, ice, SWITCH_TRUE) != SWITCH_STATUS_SUCCESS) {
1463  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "Error sending STUN Binding Request on ICE candidate still unresponsive to %s:%u\n", from_host, from_port);
1464  }
1465  }
1466  }
1467  } else if (packet->header.type == SWITCH_STUN_BINDING_ERROR_RESPONSE) {
1468 
1469  if (rtp_session->session) {
1470  switch_core_session_message_t msg = { 0 };
1471  msg.from = __FILE__;
1472  msg.numeric_arg = packet->header.type;
1473  msg.pointer_arg = packet;
1475  switch_core_session_receive_message(rtp_session->session, &msg);
1477  "STUN/ICE binding error received on %s channel\n", rtp_type(rtp_session));
1478  }
1479 
1480  }
1481 
1482 
1483 
1484 
1485  end:
1486  switch_mutex_unlock(rtp_session->ice_mutex);
1487  WRITE_DEC(rtp_session);
1488  READ_DEC(rtp_session);
1489 }
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
Definition: switch_time.c:311
#define SWITCH_CHANNEL_SESSION_LOG(x)
uint16_t switch_sockaddr_get_port(switch_sockaddr_t *sa)
Definition: switch_apr.c:1001
uint8_t switch_rtp_ready(switch_rtp_t *rtp_session)
Test if an RTP session is ready.
Definition: switch_rtp.c:5150
switch_sockaddr_t * rtcp_from_addr
Definition: switch_rtp.c:398
switch_core_session_message_types_t message_id
Definition: switch_core.h:183
uint8_t switch_stun_packet_attribute_add_fingerprint(switch_stun_packet_t *packet)
Definition: switch_stun.c:563
switch_sockaddr_t * remote_addr
Definition: switch_rtp.c:344
uint32_t elapsed_adj
Definition: switch_rtp.c:485
switch_sockaddr_t * addr
Definition: switch_rtp.c:252
#define WRITE_INC(rtp_session)
Definition: switch_rtp.c:69
uint8_t responsive
Definition: switch_rtp.h:106
switch_port_t remote_port
Definition: switch_rtp.c:421
ice_t * ice_params
Definition: switch_rtp.c:256
switch_time_t last_ok
Definition: switch_rtp.c:264
#define rtp_session_name(_rtp_session)
Definition: switch_rtp.c:85
uint32_t elapsed_stun
Definition: switch_rtp.c:483
uint8_t initializing
Definition: switch_rtp.c:261
#define STUN_TOO_LONG
Definition: switch_rtp.c:824
icand_t cands[MAX_CAND][MAX_CAND_IDX_COUNT]
Definition: switch_rtp.h:114
uint8_t use_candidate
Definition: switch_rtp.h:107
int switch_snprintf(_Out_z_cap_(len) char *buf, _In_ switch_size_t len, _In_z_ _Printf_format_string_ const char *format,...)
#define READ_DEC(rtp_session)
Definition: switch_rtp.c:68
switch_stun_packet_t * switch_stun_packet_build_header(switch_stun_message_t type, char *id, uint8_t *buf)
Prepare a new outbound packet of a certian type and id.
Definition: switch_stun.c:442
A message object designed to allow unlike technologies to exchange data.
Definition: switch_core.h:179
#define zstr(x)
Definition: switch_utils.h:314
dtls_state_t state
Definition: switch_rtp.c:284
#define rtp_type(rtp_session)
Definition: switch_rtp.c:544
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
_Ret_ switch_channel_t * switch_core_session_get_channel(_In_ switch_core_session_t *session)
Retrieve a pointer to the channel object associated with a given session.
uint8_t switch_stun_packet_attribute_add_username(switch_stun_packet_t *packet, char *username, uint16_t ulen)
Add a username packet attribute.
Definition: switch_stun.c:627
int64_t switch_time_t
Definition: switch_apr.h:188
switch_core_media_ice_type_t type
Definition: switch_rtp.c:255
switch_byte_t switch_byte_t * buf
#define WRITE_DEC(rtp_session)
Definition: switch_rtp.c:70
int icecmp(const char *them, switch_rtp_ice_t *ice)
Definition: switch_rtp.c:947
static void calc_elapsed(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice)
Definition: switch_rtp.c:827
switch_status_t switch_socket_sendto(switch_socket_t *sock, switch_sockaddr_t *where, int32_t flags, const char *buf, switch_size_t *len)
Definition: switch_apr.c:796
int switch_cmp_addr(switch_sockaddr_t *sa1, switch_sockaddr_t *sa2, switch_bool_t ip_only)
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:396
#define switch_stun_packet_first_attribute(packet, attribute)
set a switch_stun_packet_attribute_t pointer to point at the first attribute in a packet ...
Definition: switch_stun.h:293
#define switch_channel_get_variable(_c, _v)
#define switch_stun_attribute_padded_length(attribute)
Obtain the padded length of an attribute&#39;s value.
Definition: switch_stun.h:284
uint8_t cand_responsive
Definition: switch_rtp.c:265
char * con_addr
Definition: switch_rtp.h:99
uint32_t elapsed_media
Definition: switch_rtp.c:484
#define READ_INC(rtp_session)
Definition: switch_rtp.c:67
switch_byte_t auto_adj_used
Definition: switch_rtp.c:467
#define switch_stun_packet_length(packet)
Obtain the correct length in bytes of a stun packet.
Definition: switch_stun.h:311
char last_sent_id[13]
Definition: switch_rtp.c:263
char * cand_type
Definition: switch_rtp.h:101
uintptr_t switch_size_t
uint16_t switch_port_t
switch_dtls_t * dtls
Definition: switch_rtp.c:370
switch_rtp_ice_t rtcp_ice
Definition: switch_rtp.c:402
switch_status_t switch_rtp_set_remote_address(switch_rtp_t *rtp_session, const char *host, switch_port_t port, switch_port_t remote_rtcp_port, switch_bool_t change_adv_addr, const char **err)
Assign a remote address to the RTP session.
Definition: switch_rtp.c:3128
int cand_idx[MAX_CAND_IDX_COUNT]
Definition: switch_rtp.h:115
#define switch_core_session_receive_message(_session, _message)
Definition: switch_core.h:1247
switch_memory_pool_t * pool
Definition: switch_rtp.c:397
struct fspr_sockaddr_t switch_sockaddr_t
Definition: switch_apr.h:1029
switch_time_t last_adj
Definition: switch_rtp.c:481
switch_socket_t * rtcp_sock_output
Definition: switch_rtp.c:326
void switch_rtp_clear_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Clear an RTP Flag.
Definition: switch_rtp.c:5434
switch_stun_packet_header_t header
Definition: switch_stun.h:133
char * ip
Definition: switch_msrp.c:60
#define switch_stun_packet_next_attribute(attribute, end)
Increment an attribute pointer to the next attribute in it&#39;s packet.
Definition: switch_stun.h:302
void switch_rtp_set_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Set an RTP Flag.
Definition: switch_rtp.c:5375
#define STUN_USERNAME_MAX_SIZE
Definition: switch_rtp.c:87
char * switch_stun_packet_attribute_get_username(switch_stun_packet_attribute_t *attribute, char *username, uint16_t len)
Extract a username from a packet attribute.
Definition: switch_stun.c:434
ice_proto_t proto
Definition: switch_rtp.c:257
#define SWITCH_UNSPEC
Definition: switch_apr.h:1022
uint8_t switch_stun_packet_attribute_get_xor_mapped_address(switch_stun_packet_attribute_t *attribute, switch_stun_packet_header_t *header, char *ipstr, switch_size_t iplen, uint16_t *port)
Definition: switch_stun.c:401
switch_mutex_t * ice_mutex
Definition: switch_rtp.c:433
#define MEDIA_TOO_LONG
Definition: switch_rtp.c:823
void switch_core_session_video_reinit(switch_core_session_t *session)
switch_core_session_t * session
Definition: switch_rtp.c:476
switch_stun_packet_t * switch_stun_packet_parse(uint8_t *buf, uint32_t len)
Prepare a raw packet for parsing.
Definition: switch_stun.c:144
switch_time_t adj_window
Definition: switch_rtp.c:482
switch_rtp_ice_t ice
Definition: switch_rtp.c:401
switch_rtp_bug_flag_t rtp_bugs
Definition: switch_rtp.c:453
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
uint8_t switch_stun_packet_attribute_add_xor_binded_address(switch_stun_packet_t *packet, char *ipstr, uint16_t port, int family)
Definition: switch_stun.c:496
uint32_t priority
Definition: switch_rtp.h:98
const char * switch_get_addr(char *buf, switch_size_t len, switch_sockaddr_t *in)
Definition: switch_apr.c:979
const char * switch_stun_value_to_name(int32_t type, uint32_t value)
Obtain a printable string form of a given value.
Definition: switch_stun.c:354
int chosen[MAX_CAND_IDX_COUNT]
Definition: switch_rtp.h:116
switch_time_t last_stun
Definition: switch_rtp.c:408
struct fspr_socket_t switch_socket_t
Definition: switch_apr.h:1026
switch_sockaddr_t * from_addr
Definition: switch_rtp.c:398
switch_time_t first_stun
Definition: switch_rtp.c:407
#define switch_channel_set_variable(_channel, _var, _val)
static switch_status_t ice_out(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice, switch_bool_t force)
Definition: switch_rtp.c:857
switch_status_t switch_sockaddr_info_get(switch_sockaddr_t **sa, const char *hostname, int32_t family, switch_port_t port, int32_t flags, switch_memory_pool_t *pool)
Definition: switch_apr.c:839
char * switch_channel_get_name(switch_channel_t *channel)
Retrieve the name of a given channel.
static void switch_rtp_change_ice_dest(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice, const char *host, switch_port_t port)
Definition: switch_rtp.c:547
uint8_t switch_stun_packet_attribute_get_mapped_address(switch_stun_packet_attribute_t *attribute, char *ipstr, switch_size_t iplen, uint16_t *port)
Extract a mapped address (IP:PORT) from a packet attribute.
Definition: switch_stun.c:384
memset(buf, 0, buflen)
switch_socket_t * sock_output
Definition: switch_rtp.c:326
uint8_t switch_stun_packet_attribute_add_integrity(switch_stun_packet_t *packet, const char *pass)
Definition: switch_stun.c:546
switch_port_t con_port
Definition: switch_rtp.h:100

◆ handle_nack()

static void handle_nack ( switch_rtp_t rtp_session,
uint32_t  nack 
)
static

Definition at line 6692 of file switch_rtp.c.

References switch_rtp::flags, switch_rtp_packet_t::header, switch_rtp::local_addr, switch_rtp_hdr_t::m, switch_rtp_hdr_t::pt, switch_rtp::remote_addr, switch_rtp::remote_port, switch_rtp::rtcp_from_addr, switch_rtp_hdr_t::seq, switch_rtp::session, switch_channel_get_name(), SWITCH_CHANNEL_SESSION_LOG, SWITCH_CHANNEL_SESSION_LOG_CLEAN, switch_core_session_get_channel(), switch_core_session_get_name, SWITCH_FALSE, switch_get_addr(), switch_jb_get_packet_by_seq(), SWITCH_LOG_CONSOLE, SWITCH_LOG_DEBUG2, switch_log_printf(), SWITCH_RTP_FLAG_DEBUG_RTP_WRITE, SWITCH_RTP_FLAG_NACK, switch_rtp_write_raw(), switch_sockaddr_get_port(), SWITCH_STATUS_SUCCESS, switch_rtp_hdr_t::ts, and switch_rtp::vbw.

Referenced by process_rtcp_report().

6693 {
6694  switch_size_t bytes = 0;
6695  rtp_msg_t send_msg[1] = {{{0}}};
6696  uint16_t seq = (uint16_t) (nack & 0xFFFF);
6697  uint16_t blp = (uint16_t) (nack >> 16);
6698  int i;
6699  const char *tx_host = NULL;
6700  const char *old_host = NULL;
6701  const char *my_host = NULL;
6702  char bufa[50], bufb[50], bufc[50];
6703 
6704  if (!(rtp_session->flags[SWITCH_RTP_FLAG_NACK] && rtp_session->vbw)) {
6705  return; /* not enabled */
6706  }
6707 
6708  if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]) {
6709  tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtcp_from_addr);
6710  old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
6711  my_host = switch_get_addr(bufc, sizeof(bufc), rtp_session->local_addr);
6712  }
6713 
6714  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "%s Got NACK [%u][0x%x] for seq %u\n",
6715  switch_core_session_get_name(rtp_session->session), nack, nack, ntohs(seq));
6716 
6717  if (switch_jb_get_packet_by_seq(rtp_session->vbw, seq, (switch_rtp_packet_t *) send_msg, &bytes) == SWITCH_STATUS_SUCCESS) {
6718 
6719  if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]) {
6721  "X %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n",
6722  rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "NoName",
6723  (long) bytes,
6724  my_host, switch_sockaddr_get_port(rtp_session->local_addr),
6725  old_host, rtp_session->remote_port,
6726  tx_host, switch_sockaddr_get_port(rtp_session->rtcp_from_addr),
6727  send_msg->header.pt, ntohl(send_msg->header.ts), ntohs(send_msg->header.seq), send_msg->header.m);
6728 
6729  }
6730  //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG2, "RE----SEND %u\n", ntohs(send_msg->header.seq));
6731  switch_rtp_write_raw(rtp_session, (void *) send_msg, &bytes, SWITCH_FALSE);
6732  } else {
6733  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "Cannot send NACK for seq %u\n", ntohs(seq));
6734  }
6735 
6736  blp = ntohs(blp);
6737  for (i = 0; i < 16; i++) {
6738  if (blp & (1 << i)) {
6739  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "%s Also Got NACK for seq %u\n",
6740  switch_core_session_get_name(rtp_session->session), ntohs(seq) + i + 1);
6741  /* If they are missing more than one, may as well gen a key frame for good measure */
6742  //switch_core_media_gen_key_frame(rtp_session->session);
6743  if (switch_jb_get_packet_by_seq(rtp_session->vbw, htons(ntohs(seq) + i + 1), (switch_rtp_packet_t *) &send_msg, &bytes) == SWITCH_STATUS_SUCCESS) {
6744  if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]) {
6746  "X %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n",
6747  rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "NoName",
6748  (long) bytes,
6749  my_host, switch_sockaddr_get_port(rtp_session->local_addr),
6750  old_host, rtp_session->remote_port,
6751  tx_host, switch_sockaddr_get_port(rtp_session->rtcp_from_addr),
6752  send_msg->header.pt, ntohl(send_msg->header.ts), ntohs(send_msg->header.seq), send_msg->header.m);
6753 
6754  }
6755  //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "RE----SEND %u\n", ntohs(send_msg->header.seq));
6756 
6757  switch_rtp_write_raw(rtp_session, (void *) &send_msg, &bytes, SWITCH_FALSE);
6758  } else {
6759  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "Cannot send NACK for seq %u\n", ntohs(seq) + i);
6760  }
6761  }
6762  }
6763 }
switch_status_t switch_jb_get_packet_by_seq(switch_jb_t *jb, uint16_t seq, switch_rtp_packet_t *packet, switch_size_t *len)
#define SWITCH_CHANNEL_SESSION_LOG(x)
uint16_t switch_sockaddr_get_port(switch_sockaddr_t *sa)
Definition: switch_apr.c:1001
switch_sockaddr_t * rtcp_from_addr
Definition: switch_rtp.c:398
switch_sockaddr_t * remote_addr
Definition: switch_rtp.c:344
switch_rtp_hdr_t header
Definition: switch_rtp.h:56
switch_port_t remote_port
Definition: switch_rtp.c:421
switch_jb_t * vbw
Definition: switch_rtp.c:440
#define switch_core_session_get_name(_s)
Definition: switch_core.h:265
_Ret_ switch_channel_t * switch_core_session_get_channel(_In_ switch_core_session_t *session)
Retrieve a pointer to the channel object associated with a given session.
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:396
uintptr_t switch_size_t
switch_sockaddr_t * local_addr
Definition: switch_rtp.c:330
switch_status_t switch_rtp_write_raw(switch_rtp_t *rtp_session, void *data, switch_size_t *bytes, switch_bool_t process_encryption)
Definition: switch_rtp.c:9161
#define SWITCH_CHANNEL_SESSION_LOG_CLEAN(x)
switch_core_session_t * session
Definition: switch_rtp.c:476
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
const char * switch_get_addr(char *buf, switch_size_t len, switch_sockaddr_t *in)
Definition: switch_apr.c:979
char * switch_channel_get_name(switch_channel_t *channel)
Retrieve the name of a given channel.

◆ handle_rfc2833()

static handle_rfc2833_result_t handle_rfc2833 ( switch_rtp_t rtp_session,
switch_size_t  bytes,
int *  do_cng 
)
static

Definition at line 586 of file switch_rtp.c.

References switch_dtmf_t::digit, switch_rtp::dtmf_data, DTMF_SANITY, switch_dtmf_t::duration, switch_rtp_rfc2833_data::first_digit, switch_rtp::flags, switch_rtp_rfc2833_data::flip, switch_rtp_rfc2833_data::in_digit_queued, switch_rtp_rfc2833_data::in_digit_sanity, switch_rtp_rfc2833_data::in_digit_seq, switch_rtp_rfc2833_data::in_digit_ts, switch_rtp_rfc2833_data::in_interleaved, switch_rtp_stats_t::inbound, switch_rtp::jb, key, switch_rtp_rfc2833_data::last_digit, switch_rtp_rfc2833_data::last_duration, switch_rtp_rfc2833_data::last_in_digit_ts, switch_rtp_numbers_t::last_processed_seq, switch_rtp::last_rtp_hdr, switch_rtp::pool, switch_rtp::recv_te, RESULT_CONTINUE, RESULT_GOTO_END, RESULT_GOTO_RECVFROM, RESULT_GOTO_TIMERCHECK, RTP_BODY, RTP_BUG_FLUSH_JB_ON_DTMF, RTP_BUG_IGNORE_DTMF_DURATION, switch_rtp::rtp_bugs, rtp_header_len, rtp_write_ready(), switch_rtp::session, switch_rtp::stats, SWITCH_CHANNEL_LOG, SWITCH_CHANNEL_SESSION_LOG, switch_cond_next(), switch_core_memory_pool_get_data(), switch_core_min_dtmf_duration(), SWITCH_DTMF_RTP, switch_jb_reset(), SWITCH_LOG_DEBUG, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_LOG_WARNING, switch_rfc2833_to_char(), SWITCH_RTP_FLAG_DTMF_ON, SWITCH_RTP_FLAG_PASS_RFC2833, SWITCH_RTP_FLAG_PROXY_MEDIA, switch_rtp_queue_rfc2833_in(), and switch_rtp_ready().

Referenced by do_flush(), and rtp_common_read().

587 {
588 
589  if (rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON]) {
590  rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON]++;
591 
592  if (rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] > DTMF_SANITY) {
593  rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] = 0;
594  } else {
595  rtp_session->stats.inbound.last_processed_seq = 0;
596  }
597  }
598 
599 
600 #ifdef DEBUG_2833
601  if (rtp_session->dtmf_data.in_digit_sanity && !(rtp_session->dtmf_data.in_digit_sanity % 100)) {
602  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "sanity %d %ld\n", rtp_session->dtmf_data.in_digit_sanity, bytes);
603  }
604 #endif
605 
606  if (rtp_session->dtmf_data.in_digit_sanity && !--rtp_session->dtmf_data.in_digit_sanity) {
607 
608  rtp_session->dtmf_data.last_digit = 0;
609  rtp_session->dtmf_data.in_digit_ts = 0;
610  rtp_session->dtmf_data.in_digit_queued = 0;
611  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Failed DTMF sanity check.\n");
612  }
613 
614  if (!bytes) return RESULT_CONTINUE;
615 
616 
617  /* RFC2833 ... like all RFC RE: VoIP, guaranteed to drive you to insanity!
618  We know the real rules here, but if we enforce them, it's an interop nightmare so,
619  we put up with as much as we can so we don't have to deal with being punished for
620  doing it right. Nice guys finish last!
621  */
622 
623  if (bytes > rtp_header_len && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] &&
624  rtp_session->last_rtp_hdr.pt == rtp_session->recv_te) {
625  switch_size_t len = bytes - rtp_header_len;
626  unsigned char *packet = (unsigned char *) RTP_BODY(rtp_session);
627  int end;
628  uint16_t duration;
629  char key;
630  uint16_t in_digit_seq;
631  uint32_t ts;
632 
633  rtp_session->stats.inbound.last_processed_seq = 0;
634 
635  if (!(packet[0] || packet[1] || packet[2] || packet[3]) && len >= 8) {
636  packet += 4;
637  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "DTMF payload offset by 4 bytes.\n");
638  }
639 
640  if (!(packet[0] || packet[1] || packet[2] || packet[3]) && rtp_session->dtmf_data.in_digit_ts) {
641  switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
642  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Failed DTMF payload check.\n");
643  rtp_session->dtmf_data.last_digit = 0;
644  rtp_session->dtmf_data.in_digit_ts = 0;
645  rtp_session->dtmf_data.in_digit_sanity = 0;
646  rtp_session->dtmf_data.in_digit_queued = 0;
647  }
648 
649  end = packet[1] & 0x80 ? 1 : 0;
650  duration = (packet[2] << 8) + packet[3];
651  key = switch_rfc2833_to_char(packet[0]);
652  in_digit_seq = ntohs((uint16_t) rtp_session->last_rtp_hdr.seq);
653  ts = htonl(rtp_session->last_rtp_hdr.ts);
654 
655  if (rtp_session->flags[SWITCH_RTP_FLAG_PASS_RFC2833]) {
656 
657  if (end) {
658  rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] = DTMF_SANITY - 3;
659  } else if (!rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON]) {
660  rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] = 1;
661  }
662 
663  return RESULT_CONTINUE;
664  }
665 
666  if (in_digit_seq < rtp_session->dtmf_data.in_digit_seq) {
667  if (rtp_session->dtmf_data.in_digit_seq - in_digit_seq > 100) {
668  rtp_session->dtmf_data.in_digit_seq = 0;
669  }
670  }
671 #ifdef DEBUG_2833
672  if (!(packet[0] || packet[1] || packet[2] || packet[3]) && len >= 8) {
673  len -= 4;
674  }
675  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "packet[%d]: %02x %02x %02x %02x\n", (int) len, (unsigned char) packet[0], (unsigned char) packet[1], (unsigned char) packet[2], (unsigned char) packet[3]);
676 #endif
677 
678  if (in_digit_seq > rtp_session->dtmf_data.in_digit_seq) {
679 
680  rtp_session->dtmf_data.in_digit_seq = in_digit_seq;
681 #ifdef DEBUG_2833
682 
683  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "read: %c %u %u %u %u %d %d %s\n",
684  key, in_digit_seq, rtp_session->dtmf_data.in_digit_seq,
685  ts, duration, rtp_session->last_rtp_hdr.m, end, end && !rtp_session->dtmf_data.in_digit_ts ? "ignored" : "");
686 #endif
687 
688 
689  if (rtp_session->dtmf_data.in_digit_ts && rtp_session->dtmf_data.in_digit_ts != ts) {
690  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "TS changed from last packet, resetting....\n");
691  rtp_session->dtmf_data.last_digit = 0;
692  rtp_session->dtmf_data.in_digit_ts = 0;
693  rtp_session->dtmf_data.in_digit_sanity = 0;
694  rtp_session->dtmf_data.in_digit_queued = 0;
695  }
696 
697 
698  if (!rtp_session->dtmf_data.in_digit_queued && rtp_session->dtmf_data.in_digit_ts) {
699  if ((rtp_session->rtp_bugs & RTP_BUG_IGNORE_DTMF_DURATION)) {
701 #ifdef DEBUG_2833
702  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Early Queuing digit %c:%d\n", dtmf.digit, dtmf.duration / 8);
703 #endif
704  switch_rtp_queue_rfc2833_in(rtp_session, &dtmf);
705  rtp_session->dtmf_data.in_digit_queued = 1;
706  }
707 
708  if (rtp_session->jb && (rtp_session->rtp_bugs & RTP_BUG_FLUSH_JB_ON_DTMF)) {
709  switch_jb_reset(rtp_session->jb);
710  }
711 
712  }
713 
714  /* only set sanity if we do NOT ignore the packet */
715  if (rtp_session->dtmf_data.in_digit_ts) {
716  rtp_session->dtmf_data.in_digit_sanity = 2000;
717  }
718 
719  if (rtp_session->dtmf_data.last_duration > duration &&
720  rtp_session->dtmf_data.last_duration > 0xFC17 && ts == rtp_session->dtmf_data.in_digit_ts) {
721  rtp_session->dtmf_data.flip++;
722  }
723 
724  if (end) {
725  if (!rtp_session->dtmf_data.in_digit_ts && rtp_session->dtmf_data.last_in_digit_ts != ts) {
726 #ifdef DEBUG_2833
727  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "start with end packet %d\n", ts);
728 #endif
729  rtp_session->dtmf_data.last_in_digit_ts = ts;
730  rtp_session->dtmf_data.in_digit_ts = ts;
731  rtp_session->dtmf_data.first_digit = key;
732  rtp_session->dtmf_data.in_digit_sanity = 2000;
733  }
734  if (rtp_session->dtmf_data.in_digit_ts) {
735  switch_dtmf_t dtmf = { key, duration, 0, SWITCH_DTMF_RTP };
736 
737  if (ts > rtp_session->dtmf_data.in_digit_ts) {
738  dtmf.duration += (ts - rtp_session->dtmf_data.in_digit_ts);
739  }
740  if (rtp_session->dtmf_data.flip) {
741  dtmf.duration += rtp_session->dtmf_data.flip * 0xFFFF;
742  rtp_session->dtmf_data.flip = 0;
743 #ifdef DEBUG_2833
745 #endif
746  }
747 #ifdef DEBUG_2833
748  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "done digit=%c ts=%u start_ts=%u dur=%u ddur=%u\n",
749  dtmf.digit, ts, rtp_session->dtmf_data.in_digit_ts, duration, dtmf.duration);
750 #endif
751 
752  if (!(rtp_session->rtp_bugs & RTP_BUG_IGNORE_DTMF_DURATION) && !rtp_session->dtmf_data.in_digit_queued) {
753 #ifdef DEBUG_2833
754  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Queuing digit %c:%d\n", dtmf.digit, dtmf.duration / 8);
755 #endif
756  switch_rtp_queue_rfc2833_in(rtp_session, &dtmf);
757  }
758 
759  rtp_session->dtmf_data.last_digit = rtp_session->dtmf_data.first_digit;
760 
761  rtp_session->dtmf_data.in_digit_ts = 0;
762  rtp_session->dtmf_data.in_digit_sanity = 0;
763  rtp_session->dtmf_data.in_digit_queued = 0;
764  *do_cng = 1;
765  } else {
766  if (!switch_rtp_ready(rtp_session)) {
767  return RESULT_GOTO_END;
768  }
770  return RESULT_GOTO_RECVFROM;
771  }
772 
773  } else if (!rtp_session->dtmf_data.in_digit_ts) {
774 #ifdef DEBUG_2833
775  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "start %d [%c]\n", ts, key);
776 #endif
777  rtp_session->dtmf_data.in_digit_ts = ts;
778  rtp_session->dtmf_data.last_in_digit_ts = ts;
779  rtp_session->dtmf_data.first_digit = key;
780  rtp_session->dtmf_data.in_digit_sanity = 2000;
781  }
782 
783  rtp_session->dtmf_data.last_duration = duration;
784  } else {
785 #ifdef DEBUG_2833
786  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "drop: %c %u %u %u %u %d %d\n",
787  key, in_digit_seq, rtp_session->dtmf_data.in_digit_seq, ts, duration, rtp_session->last_rtp_hdr.m, end);
788 #endif
790  return RESULT_GOTO_RECVFROM;
791  }
792  }
793 
794  if (rtp_session->dtmf_data.in_digit_ts) {
795  if (!switch_rtp_ready(rtp_session)) {
796  return RESULT_GOTO_END;
797  }
798 
799  if (!rtp_session->dtmf_data.in_interleaved && rtp_session->last_rtp_hdr.pt != rtp_session->recv_te) {
800  /* Drat, they are sending audio still as well as DTMF ok fine..... *sigh* */
801  rtp_session->dtmf_data.in_interleaved = 1;
802  }
803 
804  if (rtp_session->dtmf_data.in_interleaved || (rtp_session->rtp_bugs & RTP_BUG_IGNORE_DTMF_DURATION)) {
805  if (rtp_session->last_rtp_hdr.pt == rtp_session->recv_te) {
806  return RESULT_GOTO_RECVFROM;
807  }
808  } else {
809  *do_cng = 1;
810  return RESULT_GOTO_TIMERCHECK;
811  }
812  }
813 
814  return RESULT_CONTINUE;
815 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
switch_rtp_numbers_t inbound
Definition: switch_types.h:771
uint8_t switch_rtp_ready(switch_rtp_t *rtp_session)
Test if an RTP session is ready.
Definition: switch_rtp.c:5150
#define SWITCH_CHANNEL_LOG
#define DTMF_SANITY
Definition: switch_rtp.c:83
void * switch_core_memory_pool_get_data(switch_memory_pool_t *pool, const char *key)
uint32_t duration
Definition: switch_types.h:302
struct switch_rtp_rfc2833_data dtmf_data
Definition: switch_rtp.c:426
uint32_t switch_core_min_dtmf_duration(uint32_t duration)
Definition: switch_core.c:1744
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:396
uintptr_t switch_size_t
char switch_rfc2833_to_char(int event)
Return the RFC2833 character based on an event id.
void switch_cond_next(void)
Definition: switch_time.c:658
switch_rtp_stats_t stats
Definition: switch_rtp.c:454
switch_memory_pool_t * pool
Definition: switch_rtp.c:397
#define RTP_BODY(_s)
Definition: switch_rtp.c:118
switch_jb_t * jb
Definition: switch_rtp.c:438
switch_core_session_t * session
Definition: switch_rtp.c:476
void switch_jb_reset(switch_jb_t *jb)
switch_rtp_bug_flag_t rtp_bugs
Definition: switch_rtp.c:453
char * key
Definition: switch_msrp.c:64
switch_payload_t recv_te
Definition: switch_rtp.c:428
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_status_t switch_rtp_queue_rfc2833_in(switch_rtp_t *rtp_session, const switch_dtmf_t *dtmf)
Queue RFC2833 DTMF data into an RTP Session.
Definition: switch_rtp.c:8120
rtp_hdr_t last_rtp_hdr
Definition: switch_rtp.c:373
#define rtp_header_len
Definition: switch_rtp.c:73

◆ ice_out()

static switch_status_t ice_out ( switch_rtp_t rtp_session,
switch_rtp_ice_t ice,
switch_bool_t  force 
)
static

Definition at line 857 of file switch_rtp.c.

References switch_rtp_ice_t::addr, buf, ice_s::cands, ice_s::chosen, switch_stun_packet_t::header, ICE_CONTROLLED, ICE_LITE, switch_rtp_ice_t::ice_params, switch_rtp_ice_t::ice_user, ICE_VANILLA, switch_stun_packet_header_t::id, switch_rtp_ice_t::last_sent_id, switch_rtp::last_stun, switch_rtp_ice_t::next_run, icand_s::priority, switch_rtp_ice_t::proto, READ_DEC, READ_INC, switch_rtp_ice_t::rpass, switch_rtp::rtcp_ice, switch_rtp::rtcp_sock_output, rtp_session_name, RTP_STUN_FREQ, rtp_type, switch_rtp_ice_t::sending, switch_rtp::session, switch_rtp::sock_output, switch_assert, SWITCH_CHANNEL_SESSION_LOG, SWITCH_LOG_CRIT, switch_log_printf(), SWITCH_LOG_WARNING, switch_micro_time_now(), switch_snprintf(), switch_socket_sendto(), SWITCH_STATUS_BREAK, SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, SWITCH_STUN_BINDING_REQUEST, switch_stun_packet_attribute_add_controlled(), switch_stun_packet_attribute_add_controlling(), switch_stun_packet_attribute_add_fingerprint(), switch_stun_packet_attribute_add_integrity(), switch_stun_packet_attribute_add_priority(), switch_stun_packet_attribute_add_software(), switch_stun_packet_attribute_add_use_candidate(), switch_stun_packet_attribute_add_username(), switch_stun_packet_build_header(), switch_stun_packet_length, switch_version_revision_human(), and switch_rtp_ice_t::type.

Referenced by check_rtcp_and_ice(), generate_m(), handle_ice(), and switch_core_media_gen_local_sdp().

858 {
859  uint8_t buf[256] = { 0 };
860  switch_stun_packet_t *packet;
861  unsigned int elapsed;
862  switch_size_t bytes;
864  //switch_sockaddr_t *remote_addr = rtp_session->remote_addr;
865  switch_socket_t *sock_output = rtp_session->sock_output;
867 
868  if (ice->type & ICE_LITE) {
869  // no connectivity checks for ICE-Lite
870  return SWITCH_STATUS_BREAK;
871  }
872 
873  if (!force && ice->next_run && ice->next_run >= now) {
874  return SWITCH_STATUS_BREAK;
875  }
876 
877  ice->next_run = now + RTP_STUN_FREQ;
878 
879  if (ice == &rtp_session->rtcp_ice && rtp_session->rtcp_sock_output) {
880  sock_output = rtp_session->rtcp_sock_output;
881  }
882 
883  if (!sock_output) {
884  return SWITCH_STATUS_FALSE;
885  }
886 
887  switch_assert(rtp_session != NULL);
888  switch_assert(ice->ice_user != NULL);
889 
890  READ_INC(rtp_session);
891 
892  if (rtp_session->last_stun) {
893  elapsed = (unsigned int) ((switch_micro_time_now() - rtp_session->last_stun) / 1000);
894 
895  if (elapsed > 30000) {
896  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "No %s stun for a long time!\n", rtp_type(rtp_session));
897  rtp_session->last_stun = switch_micro_time_now();
898  //status = SWITCH_STATUS_GENERR;
899  //goto end;
900  }
901  }
902 
904  switch_stun_packet_attribute_add_username(packet, ice->ice_user, (uint16_t)strlen(ice->ice_user));
905 
906  memcpy(ice->last_sent_id, packet->header.id, 12);
907 
908  //if (ice->pass && ice->type == ICE_GOOGLE_JINGLE) {
909  // switch_stun_packet_attribute_add_password(packet, ice->pass, (uint16_t)strlen(ice->pass));
910  //}
911 
912  if ((ice->type & ICE_VANILLA)) {
913  char sw[128] = "";
914 
916 
917  switch_snprintf(sw, sizeof(sw), "FreeSWITCH (%s)", switch_version_revision_human());
918  switch_stun_packet_attribute_add_software(packet, sw, (uint16_t)strlen(sw));
919 
920  if ((ice->type & ICE_CONTROLLED)) {
922  } else {
925  }
926 
929  }
930 
931 
932  bytes = switch_stun_packet_length(packet);
933 
934 #ifdef DEBUG_EXTRA
935  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_CRIT, "%s send %s stun\n", rtp_session_name(rtp_session), rtp_type(rtp_session));
936 #endif
937  switch_socket_sendto(sock_output, ice->addr, 0, (void *) packet, &bytes);
938 
939  ice->sending = 3;
940 
941  // end:
942  READ_DEC(rtp_session);
943 
944  return status;
945 }
uint8_t switch_stun_packet_attribute_add_software(switch_stun_packet_t *packet, char *software, uint16_t ulen)
Definition: switch_stun.c:651
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
Definition: switch_time.c:311
#define SWITCH_CHANNEL_SESSION_LOG(x)
uint8_t switch_stun_packet_attribute_add_priority(switch_stun_packet_t *packet, uint32_t priority)
Definition: switch_stun.c:532
#define RTP_STUN_FREQ
Definition: switch_rtp.c:72
uint8_t switch_stun_packet_attribute_add_fingerprint(switch_stun_packet_t *packet)
Definition: switch_stun.c:563
uint8_t switch_stun_packet_attribute_add_controlling(switch_stun_packet_t *packet)
Definition: switch_stun.c:596
switch_sockaddr_t * addr
Definition: switch_rtp.c:252
ice_t * ice_params
Definition: switch_rtp.c:256
#define rtp_session_name(_rtp_session)
Definition: switch_rtp.c:85
icand_t cands[MAX_CAND][MAX_CAND_IDX_COUNT]
Definition: switch_rtp.h:114
int switch_snprintf(_Out_z_cap_(len) char *buf, _In_ switch_size_t len, _In_z_ _Printf_format_string_ const char *format,...)
switch_time_t next_run
Definition: switch_rtp.c:254
#define READ_DEC(rtp_session)
Definition: switch_rtp.c:68
switch_stun_packet_t * switch_stun_packet_build_header(switch_stun_message_t type, char *id, uint8_t *buf)
Prepare a new outbound packet of a certian type and id.
Definition: switch_stun.c:442
#define rtp_type(rtp_session)
Definition: switch_rtp.c:544
uint8_t switch_stun_packet_attribute_add_use_candidate(switch_stun_packet_t *packet)
Definition: switch_stun.c:585
uint8_t switch_stun_packet_attribute_add_username(switch_stun_packet_t *packet, char *username, uint16_t ulen)
Add a username packet attribute.
Definition: switch_stun.c:627
int64_t switch_time_t
Definition: switch_apr.h:188
switch_core_media_ice_type_t type
Definition: switch_rtp.c:255
switch_byte_t switch_byte_t * buf
switch_status_t switch_socket_sendto(switch_socket_t *sock, switch_sockaddr_t *where, int32_t flags, const char *buf, switch_size_t *len)
Definition: switch_apr.c:796
#define READ_INC(rtp_session)
Definition: switch_rtp.c:67
#define switch_stun_packet_length(packet)
Obtain the correct length in bytes of a stun packet.
Definition: switch_stun.h:311
char last_sent_id[13]
Definition: switch_rtp.c:263
uintptr_t switch_size_t
switch_rtp_ice_t rtcp_ice
Definition: switch_rtp.c:402
switch_socket_t * rtcp_sock_output
Definition: switch_rtp.c:326
switch_stun_packet_header_t header
Definition: switch_stun.h:133
switch_status_t
Common return values.
ice_proto_t proto
Definition: switch_rtp.c:257
switch_core_session_t * session
Definition: switch_rtp.c:476
uint8_t switch_stun_packet_attribute_add_controlled(switch_stun_packet_t *packet)
Definition: switch_stun.c:611
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
uint32_t priority
Definition: switch_rtp.h:98
int chosen[MAX_CAND_IDX_COUNT]
Definition: switch_rtp.h:116
switch_time_t last_stun
Definition: switch_rtp.c:408
struct fspr_socket_t switch_socket_t
Definition: switch_apr.h:1026
const char * switch_version_revision_human(void)
#define switch_assert(expr)
switch_socket_t * sock_output
Definition: switch_rtp.c:326
uint8_t switch_stun_packet_attribute_add_integrity(switch_stun_packet_t *packet, const char *pass)
Definition: switch_stun.c:546

◆ icecmp()

int icecmp ( const char *  them,
switch_rtp_ice_t ice 
)

Definition at line 947 of file switch_rtp.c.

References switch_rtp_ice_t::luser_ice, and switch_rtp_ice_t::user_ice.

Referenced by handle_ice().

948 {
949  if (strchr(them, ':')) {
950  return strcmp(them, ice->user_ice);
951  }
952 
953  return strcmp(them, ice->luser_ice);
954 }

◆ jb_valid()

static int jb_valid ( switch_rtp_t rtp_session)
static

Definition at line 5665 of file switch_rtp.c.

References DS_READY, switch_rtp::dtls, switch_rtp::ice, switch_rtp_ice_t::ice_user, switch_rtp_ice_t::ready, switch_rtp_ice_t::rready, and switch_dtls_s::state.

Referenced by do_flush(), read_bundle_rtp_packet(), read_rtp_packet(), and rtp_common_read().

5666 {
5667  if (rtp_session->ice.ice_user) {
5668  if (!rtp_session->ice.ready && rtp_session->ice.rready) {
5669  return 0;
5670  }
5671  }
5672 
5673  if (rtp_session->dtls && rtp_session->dtls->state != DS_READY) {
5674  return 0;
5675  }
5676 
5677  return 1;
5678 }
dtls_state_t state
Definition: switch_rtp.c:284
switch_dtls_t * dtls
Definition: switch_rtp.c:370
switch_rtp_ice_t ice
Definition: switch_rtp.c:401

◆ ping_socket()

static void ping_socket ( switch_rtp_t rtp_session)
static

Definition at line 3047 of file switch_rtp.c.

References switch_rtp::flags, switch_rtp::local_addr, switch_rtp::rtcp_local_addr, switch_rtp::rtcp_sock_input, switch_rtp::sock_input, SWITCH_RTP_FLAG_ENABLE_RTCP, and switch_socket_sendto().

Referenced by switch_rtp_break(), switch_rtp_kill_socket(), and switch_rtp_udptl_mode().

3048 {
3049  uint32_t o = UINT_MAX;
3050  switch_size_t len = sizeof(o);
3051  switch_socket_sendto(rtp_session->sock_input, rtp_session->local_addr, 0, (void *) &o, &len);
3052 
3053  if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] && rtp_session->rtcp_sock_input && rtp_session->rtcp_sock_input != rtp_session->sock_input) {
3054  switch_socket_sendto(rtp_session->rtcp_sock_input, rtp_session->rtcp_local_addr, 0, (void *) &o, &len);
3055  }
3056 }
switch_sockaddr_t * rtcp_local_addr
Definition: switch_rtp.c:330
switch_socket_t * sock_input
Definition: switch_rtp.c:326
switch_socket_t * rtcp_sock_input
Definition: switch_rtp.c:326
switch_status_t switch_socket_sendto(switch_socket_t *sock, switch_sockaddr_t *where, int32_t flags, const char *buf, switch_size_t *len)
Definition: switch_apr.c:796
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:396
uintptr_t switch_size_t
switch_sockaddr_t * local_addr
Definition: switch_rtp.c:330

◆ process_rtcp_packet()

static switch_status_t process_rtcp_packet ( switch_rtp_t rtp_session,
switch_size_t bytes 
)
static

Definition at line 7102 of file switch_rtp.c.

References handle_ice(), rtcp_msg_t::header, switch_rtp::ice, switch_rtp_ice_t::ice_user, switch_rtcp_hdr_s::length, process_rtcp_report(), switch_rtp::rtcp_ice, switch_rtp::rtcp_recv_msg_p, switch_rtp::session, SWITCH_CHANNEL_SESSION_LOG, switch_log_printf(), SWITCH_LOG_WARNING, SWITCH_SIZE_T_FMT, SWITCH_STATUS_BREAK, SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, switch_rtcp_hdr_s::type, and switch_rtcp_hdr_s::version.

Referenced by read_rtcp_packet(), and rtp_common_read().

7103 {
7104  switch_size_t len;
7105  switch_size_t remain = *bytes;
7107  rtcp_msg_t *msg = rtp_session->rtcp_recv_msg_p;
7108 
7109  if (remain < sizeof(switch_rtcp_ext_hdr_t) || remain > sizeof(rtcp_msg_t)) {
7110  return status;
7111  }
7112  if (msg->header.version != 2) {
7113  if (msg->header.version == 0) {
7114  if (rtp_session->ice.ice_user) {
7115  handle_ice(rtp_session, &rtp_session->rtcp_ice, (void *) msg, *bytes);
7116  }
7117  return SWITCH_STATUS_SUCCESS;
7118  } else {
7120  SWITCH_LOG_WARNING, "Received an unsupported RTCP packet version %d\n", msg->header.version);
7121  return SWITCH_STATUS_FALSE;
7122  }
7123  }
7124 
7125  do {
7126  len = ((switch_size_t)ntohs(msg->header.length) * 4) + 4;
7127 
7128  if (msg->header.version != 2 || !(msg->header.type > 191 && msg->header.type < 210)) {
7130  "INVALID RTCP PACKET TYPE %d VER %d LEN %" SWITCH_SIZE_T_FMT "\n", msg->header.type,
7131  msg->header.version, len);
7132  status = SWITCH_STATUS_BREAK;
7133  break;
7134  }
7135 
7136  //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_CRIT,
7137  //"WTF BYTES %ld REMAIN %ld PACKET TYPE %d LEN %ld\n", *bytes, remain, msg->header.type, len);
7138 
7139  if (len > remain) {
7141  "RTCP INVALID LENGTH %" SWITCH_SIZE_T_FMT "\n", len);
7142  len = remain;
7143  }
7144 
7145  status = process_rtcp_report(rtp_session, msg, len);
7146 
7147  if (remain > len) {
7148  unsigned char *p = (unsigned char *) msg;
7149  p += len;
7150  msg = (rtcp_msg_t *) p;
7151  }
7152 
7153  remain -= len;
7154 
7155  } while (remain >= 4);
7156 
7157  return status;
7158 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
switch_rtcp_hdr_t header
Definition: switch_rtp.c:184
uintptr_t switch_size_t
switch_rtp_ice_t rtcp_ice
Definition: switch_rtp.c:402
static void handle_ice(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice, void *data, switch_size_t len)
Definition: switch_rtp.c:956
#define SWITCH_SIZE_T_FMT
switch_status_t
Common return values.
switch_core_session_t * session
Definition: switch_rtp.c:476
switch_rtp_ice_t ice
Definition: switch_rtp.c:401
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
rtcp_msg_t * rtcp_recv_msg_p
Definition: switch_rtp.c:347
static switch_status_t process_rtcp_report(switch_rtp_t *rtp_session, rtcp_msg_t *msg, switch_size_t bytes)
Definition: switch_rtp.c:6765

◆ process_rtcp_report()

static switch_status_t process_rtcp_report ( switch_rtp_t rtp_session,
rtcp_msg_t msg,
switch_size_t  bytes 
)
static

Definition at line 6765 of file switch_rtp.c.

References __BIG_ENDIAN, _RTCP_PSFB_FIR, _RTCP_PSFB_PLI, _RTCP_PT_FIR, _RTCP_PT_PSFB, _RTCP_PT_RR, _RTCP_PT_RTPFB, _RTCP_PT_SR, _RTCP_RTPFB_NACK, rtcp_ext_msg_t::body, rtcp_msg_t::body, calc_local_lsr_now(), CF_VIDEO_REFRESH_REQ, switch_rtcp_hdr_s::count, switch_rtp::detectors, switch_rtcp_report_block_frame::dlsr, switch_rtcp_report_block::dlsr, EST_LOSS, EST_RTT, switch_rtp::estimators, switch_rtp::flags, switch_rtcp_ext_hdr_t::fmt, switch_rtcp_report_block_frame::fraction, switch_rtcp_report_block::fraction, handle_nack(), rtcp_ext_msg_t::header, rtcp_msg_t::header, switch_rtcp_report_block_frame::highest_sequence_number_received, switch_rtcp_report_block::highest_sequence_number_received, switch_rtcp_report_block_frame::jitter, switch_rtcp_report_block::jitter, switch_rtcp_numbers_t::last_recv_lsr_local, switch_rtcp_numbers_t::last_recv_lsr_peer, switch_rtcp_ext_hdr_t::length, switch_rtcp_hdr_s::length, switch_rtcp_report_block_frame::loss_avg, switch_rtcp_report_block_frame::lost, switch_rtcp_report_block::lost, switch_rtcp_report_block_frame::lsr, switch_rtcp_report_block::lsr, MAX_REPORT_BLOCKS, switch_rtcp_frame::ntp_lsw, switch_rtcp_sender_info::ntp_lsw, switch_rtcp_frame::ntp_msw, switch_rtcp_sender_info::ntp_msw, switch_rtcp_sender_info::oc, switch_rtcp_frame::octect_count, switch_rtcp_numbers_t::octet_count, switch_rtcp_hdr_s::p, switch_rtcp_frame::packet_count, switch_rtcp_numbers_t::packet_count, switch_rtcp_frame::packet_type, switch_rtcp_sender_info::pc, switch_rtcp_numbers_t::peer_ssrc, switch_rtcp_sender_report::report_block, switch_rtcp_receiver_report::report_block, switch_rtcp_frame::report_count, switch_rtcp_frame::reports, switch_rtp_stats_t::rtcp, switch_rtp::rtcp_frame, switch_rtp::rtcp_fresh_frame, switch_rtp::rtcp_recv_msg_p, rtp_type, switch_rtcp_report_block_frame::rtt_avg, SCC_AUDIO_ADJUST_BITRATE, SCC_AUDIO_PACKET_LOSS, SCCT_INT, SCCT_NONE, SCCT_STRING, switch_rtcp_sender_report::sender_info, switch_rtp::session, switch_rtcp_report_block_frame::ssrc, switch_rtcp_frame::ssrc, switch_rtcp_report_block::ssrc, switch_rtcp_sender_report::ssrc, switch_rtcp_receiver_report::ssrc, switch_rtp::stats, SWITCH_CHANNEL_SESSION_LOG, switch_channel_set_flag, switch_core_media_codec_control(), switch_core_media_gen_key_frame, switch_core_session_get_channel(), switch_core_session_get_name, switch_core_session_media_flow(), switch_core_session_request_video_refresh, SWITCH_IO_WRITE, switch_kalman_cusum_detect_change(), switch_kalman_estimate(), switch_kalman_is_slow_link(), SWITCH_LOG_DEBUG2, SWITCH_LOG_DEBUG3, switch_log_printf(), SWITCH_LOG_WARNING, SWITCH_MEDIA_FLOW_RECVONLY, SWITCH_MEDIA_TYPE_AUDIO, SWITCH_MEDIA_TYPE_VIDEO, switch_micro_time_now(), SWITCH_RTP_FLAG_ADJ_BITRATE_CAP, SWITCH_RTP_FLAG_ESTIMATORS, SWITCH_RTP_FLAG_OLD_FIR, SWITCH_RTP_FLAG_RTCP_PASSTHRU, SWITCH_RTP_FLAG_VIDEO, switch_rtp_set_flag(), switch_rtp_test_flag(), SWITCH_SIZE_T_FMT, SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, switch_time_exp_gmt(), switch_rtcp_frame::timestamp, switch_time_exp_t::tm_hour, switch_time_exp_t::tm_mday, switch_time_exp_t::tm_min, switch_time_exp_t::tm_mon, switch_time_exp_t::tm_sec, switch_time_exp_t::tm_usec, switch_time_exp_t::tm_year, switch_rtcp_sender_info::ts, switch_rtcp_hdr_s::type, and kalman_estimator_s::val_estimate_last.

Referenced by process_rtcp_packet().

6766 {
6768 
6770  "RTCP packet bytes %" SWITCH_SIZE_T_FMT " type %d pad %d\n",
6771  bytes, msg->header.type, msg->header.p);
6772 
6773  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && (msg->header.type == _RTCP_PT_RTPFB || msg->header.type == _RTCP_PT_PSFB || msg->header.type < 200)) {
6774  rtcp_ext_msg_t *extp = (rtcp_ext_msg_t *) msg;
6775 
6776  if (extp->header.fmt != 15) { // <---- REMOVE WHEN BRIA STOPS SENDING UNSOLICITED REMB
6777  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "%s PICKED UP %s XRTCP type: %d fmt: %d\n",
6778  switch_core_session_get_name(rtp_session->session), rtp_type(rtp_session), msg->header.type, extp->header.fmt);
6779  }
6780 
6781  if (msg->header.type == _RTCP_PT_FIR ||
6782  (msg->header.type == _RTCP_PT_PSFB && (extp->header.fmt == _RTCP_PSFB_FIR || extp->header.fmt == _RTCP_PSFB_PLI))) {
6783 #if 0
6784  if (msg->header.type == _RTCP_PT_FIR) {
6785  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "Ancient FIR Received. Hello from 1996!\n");
6786 
6787  if (!switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_OLD_FIR)) {
6790  }
6791  }
6792 #endif
6793 
6795  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "%s Ignoring FIR/PLI from a sendonly stream.\n",
6796  switch_core_session_get_name(rtp_session->session));
6797  } else {
6800  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "%s Got FIR/PLI\n",
6801  switch_core_session_get_name(rtp_session->session));
6803  }
6804  }
6805 
6806  if (msg->header.type == _RTCP_PT_RTPFB && extp->header.fmt == _RTCP_RTPFB_NACK) {
6807  uint32_t *nack = (uint32_t *) extp->body;
6808  int i;
6809 
6810  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "%s Got NACK count %d\n",
6811  switch_core_session_get_name(rtp_session->session), ntohs(extp->header.length) - 2);
6812 
6813 
6814  for (i = 0; i < ntohs(extp->header.length) - 2; i++) {
6815  handle_nack(rtp_session, nack[i]);
6816  }
6817 
6818  //switch_core_media_gen_key_frame(rtp_session->session);
6819  }
6820 
6821  } else {
6822  struct switch_rtcp_report_block *report;
6823 
6824  if (msg->header.type == _RTCP_PT_SR || msg->header.type == _RTCP_PT_RR) {
6825  int i;
6826 #ifdef DEBUG_RTCP
6828 #endif
6829  uint32_t lsr_now;
6830  uint32_t lsr;
6831  uint32_t packet_ssrc;
6832  double rtt_now = 0;
6833  uint8_t rtt_valid = 0;
6834  int rtt_increase = 0, packet_loss_increase=0;
6835 
6836  //if (msg->header.type == _RTCP_PT_SR && rtp_session->ice.ice_user) {
6837  // rtp_session->send_rr = 1;
6838  //}
6839 
6840  lsr_now = calc_local_lsr_now();
6841 
6842  if (msg->header.type == _RTCP_PT_SR) { /* Sender report */
6843  struct switch_rtcp_sender_report* sr = (struct switch_rtcp_sender_report*)msg->body;
6844 
6845  rtp_session->stats.rtcp.packet_count = ntohl(sr->sender_info.pc);
6846  rtp_session->stats.rtcp.octet_count = ntohl(sr->sender_info.oc);
6847  packet_ssrc = sr->ssrc;
6848  /* Extracting LSR from NTP timestamp and save it */
6849  lsr = (ntohl(sr->sender_info.ntp_lsw)&0xffff0000)>>16 | (ntohl(sr->sender_info.ntp_msw)&0x0000ffff)<<16; /* The middle 32 bits out of 64 in the NTP timestamp */
6850  rtp_session->stats.rtcp.last_recv_lsr_peer = htonl(lsr); /* Save it include it in the next SR */
6851  rtp_session->stats.rtcp.last_recv_lsr_local = lsr_now; /* Save it to calculate DLSR when generating next SR */
6852  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3,"Received a SR with %d report blocks, " \
6853  "length in words = %d, " \
6854  "SSRC = 0x%X, " \
6855  "NTP MSW = %u, " \
6856  "NTP LSW = %u, " \
6857  "RTP timestamp = %u, " \
6858  "Sender Packet Count = %u, " \
6859  "Sender Octet Count = %u\n",
6860  msg->header.count,
6861  ntohs((uint16_t)msg->header.length),
6862  ntohl(sr->ssrc),
6863  ntohl(sr->sender_info.ntp_msw),
6864  ntohl(sr->sender_info.ntp_lsw),
6865  ntohl(sr->sender_info.ts),
6866  ntohl(sr->sender_info.pc),
6867  ntohl(sr->sender_info.oc));
6868 
6869 
6870  rtp_session->rtcp_frame.ssrc = ntohl(sr->ssrc);
6871  rtp_session->rtcp_frame.packet_type = (uint16_t)rtp_session->rtcp_recv_msg_p->header.type;
6872  rtp_session->rtcp_frame.ntp_msw = ntohl(sr->sender_info.ntp_msw);
6873  rtp_session->rtcp_frame.ntp_lsw = ntohl(sr->sender_info.ntp_lsw);
6874  rtp_session->rtcp_frame.timestamp = ntohl(sr->sender_info.ts);
6875  rtp_session->rtcp_frame.packet_count = ntohl(sr->sender_info.pc);
6876  rtp_session->rtcp_frame.octect_count = ntohl(sr->sender_info.oc);
6877 
6878  report = &sr->report_block;
6879  } else { /* Receiver report */
6881  packet_ssrc = rr->ssrc;
6882  //memset(&rtp_session->rtcp_frame, 0, sizeof(rtp_session->rtcp_frame));
6883  report = &rr->report_block;
6884 
6885  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3,"Received a RR with %d report blocks, " \
6886  "length in words = %d, " \
6887  "SSRC = 0x%X, ",
6888  msg->header.count,
6889  ntohs((uint16_t)msg->header.length),
6890  ntohl(rr->ssrc));
6891 
6892  }
6893 
6894 
6895  for (i = 0; i < (int)msg->header.count && i < MAX_REPORT_BLOCKS ; i++) {
6896  uint32_t old_avg = rtp_session->rtcp_frame.reports[i].loss_avg;
6897  uint8_t percent_fraction = (uint8_t)((uint16_t/* prevent overflow when '* 100' */)(uint8_t)report->fraction * 100 / 255);
6898  if (!rtp_session->rtcp_frame.reports[i].loss_avg) {
6899  rtp_session->rtcp_frame.reports[i].loss_avg = percent_fraction;
6900  } else {
6901  rtp_session->rtcp_frame.reports[i].loss_avg = (uint32_t)(((float)rtp_session->rtcp_frame.reports[i].loss_avg * .7) +
6902  ((float)percent_fraction * .3));
6903  }
6904 
6905  rtp_session->rtcp_frame.reports[i].ssrc = ntohl(report->ssrc);
6906  rtp_session->rtcp_frame.reports[i].fraction = (uint8_t)report->fraction;
6907 #if SWITCH_BYTE_ORDER == __BIG_ENDIAN
6908  rtp_session->rtcp_frame.reports[i].lost = report->lost; // signed 24bit will extended signess to int32_t automatically
6909 #else
6910  rtp_session->rtcp_frame.reports[i].lost = ntohl(report->lost)>>8; // signed 24bit casted to uint32_t need >>8 after ntohl()...
6911  rtp_session->rtcp_frame.reports[i].lost = rtp_session->rtcp_frame.reports[i].lost | ((rtp_session->rtcp_frame.reports[i].lost & 0x00800000) ? 0xff000000 : 0x00000000); // ...and signess compensation
6912 #endif
6914  rtp_session->rtcp_frame.reports[i].jitter = ntohl(report->jitter);
6915  rtp_session->rtcp_frame.reports[i].lsr = ntohl(report->lsr);
6916  rtp_session->rtcp_frame.reports[i].dlsr = ntohl(report->dlsr);
6917 
6918  if (rtp_session->rtcp_frame.reports[i].lsr && !rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU]) {
6919 
6920  /* Calculating RTT = A - DLSR - LSR */
6921  rtt_now = ((double)(((int64_t)lsr_now) - rtp_session->rtcp_frame.reports[i].dlsr - rtp_session->rtcp_frame.reports[i].lsr))/65536;
6922 
6923  /* Only account RTT if it didn't overflow. */
6924  if (lsr_now > rtp_session->rtcp_frame.reports[i].dlsr + rtp_session->rtcp_frame.reports[i].lsr) {
6925 #ifdef DEBUG_RTCP
6926  switch_time_exp_t now_hr;
6927  switch_time_exp_gmt(&now_hr,now);
6929  "Receiving an RTCP packet\n[%04d-%02d-%02d %02d:%02d:%02d.%d] SSRC[0x%x]\n"
6930  "RTT[%f] = A[%u] - DLSR[%u] - LSR[%u]\n",
6931  1900 + now_hr.tm_year, now_hr.tm_mday, now_hr.tm_mon, now_hr.tm_hour, now_hr.tm_min, now_hr.tm_sec, now_hr.tm_usec,
6932  rtp_session->rtcp_frame.reports[i].ssrc, rtt_now,
6933  lsr_now, rtp_session->rtcp_frame.reports[i].dlsr, rtp_session->rtcp_frame.reports[i].lsr);
6934 #endif
6935  rtt_valid = 1;
6936  if (!rtp_session->rtcp_frame.reports[i].rtt_avg) {
6937  rtp_session->rtcp_frame.reports[i].rtt_avg = rtt_now;
6938  } else {
6939  rtp_session->rtcp_frame.reports[i].rtt_avg = (double)((rtp_session->rtcp_frame.reports[i].rtt_avg * .7) + (rtt_now * .3 ));
6940  }
6941  } else {
6942 #ifdef DEBUG_RTCP
6943  switch_time_exp_t now_hr;
6944  switch_time_exp_gmt(&now_hr,now);
6946  "Receiving RTCP packet\n[%04d-%02d-%02d %02d:%02d:%02d.%d] SSRC[0x%x]\n"
6947  "Ignoring erroneous RTT[%f] = A[%u] - DLSR[%u] - LSR[%u]\n",
6948  1900 + now_hr.tm_year, now_hr.tm_mday, now_hr.tm_mon, now_hr.tm_hour, now_hr.tm_min, now_hr.tm_sec, now_hr.tm_usec,
6949  rtp_session->rtcp_frame.reports[i].ssrc, rtt_now,
6950  lsr_now, rtp_session->rtcp_frame.reports[i].dlsr, rtp_session->rtcp_frame.reports[i].lsr);
6951 #endif
6952  rtt_valid = 0;
6953  rtt_now = 0;
6954  }
6955 
6956 
6957  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "RTT average %f\n",
6958  rtp_session->rtcp_frame.reports[i].rtt_avg);
6959  }
6960 
6961  if (rtp_session->flags[SWITCH_RTP_FLAG_ADJ_BITRATE_CAP] && rtp_session->flags[SWITCH_RTP_FLAG_ESTIMATORS] && !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
6962 
6963  /* SWITCH_RTP_FLAG_ADJ_BITRATE_CAP : Can the codec change its bitrate on the fly per API command ? */
6964 #ifdef DEBUG_ESTIMATORS_
6965  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "Current packet loss: [%d %%] Current RTT: [%f ms]\n", percent_fraction, rtt_now);
6966 #endif
6967 
6968  if (rtt_valid) {
6969 
6970  switch_kalman_estimate(rtp_session->estimators[EST_RTT], rtt_now, EST_RTT);
6971 
6972  if (switch_kalman_cusum_detect_change(rtp_session->detectors[EST_RTT], rtt_now, rtp_session->estimators[EST_RTT]->val_estimate_last)) {
6973  /* sudden change in the mean value of RTT */
6974 #ifdef DEBUG_ESTIMATORS_
6975  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3,"Sudden change in the mean value of RTT !\n");
6976 #endif
6977  rtt_increase = 1;
6978  }
6979  }
6980 
6981  switch_kalman_estimate(rtp_session->estimators[EST_LOSS], percent_fraction, EST_LOSS);
6982 
6983  if (switch_kalman_cusum_detect_change(rtp_session->detectors[EST_LOSS], percent_fraction, rtp_session->estimators[EST_LOSS]->val_estimate_last)){
6984  /* sudden change in the mean value of packet loss */
6985 #ifdef DEBUG_ESTIMATORS_
6986  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3,"Sudden change in the mean value of packet loss!\n");
6987 #endif
6988  packet_loss_increase = 1;
6989  }
6990 #ifdef DEBUG_ESTIMATORS_
6991  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "ESTIMATORS: Packet loss will be: [%f] RTT will be: [%f ms]\n",
6992  rtp_session->estimators[EST_LOSS]->val_estimate_last, rtp_session->estimators[EST_RTT]->val_estimate_last);
6993 #endif
6994 
6995  if (rtp_session->rtcp_frame.reports[i].loss_avg != old_avg) {
6996  /*getting bad*/
6998  rtp_session->estimators[EST_RTT])) {
6999  /* going to minimum bitrate */
7000 #ifdef DEBUG_ESTIMATORS_
7001  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "Slow link conditions: Loss average: [%d %%], Previous loss: [%d %%]. \
7002  Going to minimum bitrate!",rtp_session->rtcp_frame.reports[i].loss_avg, old_avg);
7003 #endif
7005  SWITCH_IO_WRITE, SCC_AUDIO_ADJUST_BITRATE, SCCT_STRING, "minimum", SCCT_NONE, NULL, NULL, NULL);
7006  /* if after going to minimum bitrate we still have packet loss then we increase ptime. TODO */
7007 
7008  } else if (packet_loss_increase && (rtp_session->estimators[EST_LOSS]->val_estimate_last >= 5)) {
7009  /* sudden change in the mean value of packet loss percentage */
7012  SCCT_STRING, "decrease",
7013  SCCT_NONE, NULL, NULL, NULL);
7014 #ifdef DEBUG_ESTIMATORS_
7015  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3,"Sudden change in the mean value of packet loss percentage !\n");
7016 #endif
7019  (void *)&rtp_session->rtcp_frame.reports[i].loss_avg,
7020  SCCT_NONE, NULL, NULL, NULL);
7021 
7022  } else if (rtt_valid && !rtt_increase && rtp_session->estimators[EST_LOSS]->val_estimate_last >= rtp_session->rtcp_frame.reports[i].loss_avg ) {
7023  /* lossy because of congestion (queues full somewhere -> some packets are dropped , but RTT is good ), packet loss with many small gaps */
7024 #ifdef DEBUG_ESTIMATORS_
7025  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "packet loss, but RTT is not bad\n");
7026 #endif
7029  (void *)&rtp_session->rtcp_frame.reports[i].loss_avg,
7030  SCCT_NONE, NULL, NULL, NULL);
7031 
7032  } else if ((rtp_session->estimators[EST_LOSS]->val_estimate_last < 1) && packet_loss_increase) {
7033 #ifdef DEBUG_ESTIMATORS_
7034  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "small packet loss average\n");
7035 #endif
7036  /*small loss_avg*/
7039  SCCT_STRING, "default",
7040  SCCT_NONE, NULL, NULL, NULL);
7041 
7044  (void *)&rtp_session->rtcp_frame.reports[i].loss_avg,
7045  SCCT_NONE, NULL, NULL, NULL);
7046 
7047  } else if ((rtp_session->estimators[EST_LOSS]->val_estimate_last < 5) &&
7048  (rtp_session->rtcp_frame.reports[i].rtt_avg < rtp_session->estimators[EST_RTT]->val_estimate_last)) {
7049 
7050  /* estimate that packet loss will decrease, we can increase the bitrate */
7053  SCCT_STRING, "increase",
7054  SCCT_NONE, NULL, NULL, NULL);
7055 
7058  (void *)&rtp_session->rtcp_frame.reports[i].loss_avg,
7059  SCCT_NONE, NULL, NULL, NULL);
7060 
7061  } else {
7062  /* *do nothing about bitrate, just pass the packet loss to the codec */
7063 #ifdef DEBUG_ESTIMATORS_
7064  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3,"do nothing about bitrate, just pass the packet loss to the codec\n");
7065 #endif
7068  (void *)&rtp_session->rtcp_frame.reports[i].loss_avg,
7069  SCCT_NONE, NULL, NULL, NULL);
7070  }
7071  }
7072  } else {
7073  if (!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->rtcp_frame.reports[i].loss_avg != old_avg) {
7076  (void *)&rtp_session->rtcp_frame.reports[i].loss_avg,
7077  SCCT_NONE, NULL, NULL, NULL);
7078  }
7079  }
7080 
7081  report++;
7082  }
7083  rtp_session->rtcp_frame.report_count = (uint16_t)i;
7084 
7085 
7086 
7087 
7088 
7089  rtp_session->rtcp_fresh_frame = 1;
7090  rtp_session->stats.rtcp.peer_ssrc = ntohl(packet_ssrc);
7091  }
7092  }
7093 
7094  if (msg->header.type > 194 && msg->header.type < 255) {
7095  status = SWITCH_STATUS_SUCCESS;
7096  }
7097 
7098  return status;
7099 }
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
Definition: switch_time.c:311
#define switch_core_media_gen_key_frame(_session)
#define SWITCH_CHANNEL_SESSION_LOG(x)
switch_status_t switch_time_exp_gmt(switch_time_exp_t *result, switch_time_t input)
Definition: switch_apr.c:356
struct switch_rtcp_report_block report_block
Definition: switch_rtp.c:531
switch_bool_t switch_kalman_is_slow_link(kalman_estimator_t *est_loss, kalman_estimator_t *est_rtt)
#define __BIG_ENDIAN
struct switch_rtcp_report_block_frame reports[MAX_REPORT_BLOCKS]
switch_bool_t rtcp_fresh_frame
Definition: switch_rtp.c:464
#define switch_core_session_get_name(_s)
Definition: switch_core.h:265
switch_rtcp_hdr_t header
Definition: switch_rtp.c:184
switch_rtcp_ext_hdr_t header
Definition: switch_rtp.c:179
uint32_t highest_sequence_number_received
Definition: switch_rtp.c:500
#define rtp_type(rtp_session)
Definition: switch_rtp.c:544
_Ret_ switch_channel_t * switch_core_session_get_channel(_In_ switch_core_session_t *session)
Retrieve a pointer to the channel object associated with a given session.
char body[SWITCH_RTCP_MAX_BUF_LEN]
Definition: switch_rtp.c:180
int64_t switch_time_t
Definition: switch_apr.h:188
switch_rtcp_numbers_t rtcp
Definition: switch_types.h:773
struct switch_rtcp_sender_info sender_info
Definition: switch_rtp.c:525
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:396
#define switch_core_session_request_video_refresh(_s)
Definition: switch_core.h:2892
#define EST_RTT
Definition: switch_rtp.c:176
#define EST_LOSS
Definition: switch_rtp.c:174
kalman_estimator_t * estimators[KALMAN_SYSTEM_MODELS]
Definition: switch_rtp.c:479
switch_rtp_stats_t stats
Definition: switch_rtp.c:454
switch_status_t switch_core_media_codec_control(switch_core_session_t *session, switch_media_type_t mtype, switch_io_type_t iotype, switch_codec_control_command_t cmd, switch_codec_control_type_t ctype, void *cmd_data, switch_codec_control_type_t atype, void *cmd_arg, switch_codec_control_type_t *rtype, void **ret_data)
switch_media_flow_t switch_core_session_media_flow(switch_core_session_t *session, switch_media_type_t type)
switch_rtcp_frame_t rtcp_frame
Definition: switch_rtp.c:333
void switch_rtp_set_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Set an RTP Flag.
Definition: switch_rtp.c:5375
static void handle_nack(switch_rtp_t *rtp_session, uint32_t nack)
Definition: switch_rtp.c:6692
#define SWITCH_SIZE_T_FMT
switch_status_t
Common return values.
#define MAX_REPORT_BLOCKS
uint32_t last_recv_lsr_local
Definition: switch_types.h:750
switch_core_session_t * session
Definition: switch_rtp.c:476
#define switch_channel_set_flag(_c, _f)
struct switch_rtcp_report_block report_block
Definition: switch_rtp.c:526
static uint32_t calc_local_lsr_now(void)
Definition: switch_rtp.c:1823
switch_bool_t switch_kalman_cusum_detect_change(cusum_kalman_detector_t *detector, float measurement, float rtt_avg)
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
uint32_t switch_rtp_test_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flags)
Test an RTP Flag.
Definition: switch_rtp.c:5429
char body[SWITCH_RTCP_MAX_BUF_LEN]
Definition: switch_rtp.c:185
switch_bool_t switch_kalman_estimate(kalman_estimator_t *est, float measurement, int system_model)
cusum_kalman_detector_t * detectors[KALMAN_SYSTEM_MODELS]
Definition: switch_rtp.c:480
rtcp_msg_t * rtcp_recv_msg_p
Definition: switch_rtp.c:347

◆ read_bundle_rtp_packet()

static switch_status_t read_bundle_rtp_packet ( switch_rtp_t rtp_session,
switch_size_t bytes,
switch_frame_flag_t flags 
)
static

Definition at line 5850 of file switch_rtp.c.

References switch_rtp::cng_pt, DS_READY, switch_rtp::dtls, dtls_set_state, switch_rtp::flags, switch_rtp::has_rtp, rtp_msg_t::header, switch_rtp::ice_mutex, switch_rtp::jb, jb_valid(), switch_rtp::last_rtp_hdr, switch_rtp::local_addr, MAX_SRTP_ERRS, switch_rtp::pause_jb, switch_rtp::recv_ctx, switch_rtp::recv_msg, switch_rtp::recv_policy, switch_rtp::recv_te, switch_rtp::remote_addr, switch_rtp::remote_port, switch_rtp::rtp_from_addr, rtp_type, switch_rtp::session, SFF_PLC, switch_rtp::srtp_errs, switch_rtp::srtp_idx_rtp, SWITCH_CAUSE_SRTP_READ_ERROR, switch_channel_get_name(), switch_channel_hangup, SWITCH_CHANNEL_SESSION_LOG, SWITCH_CHANNEL_SESSION_LOG_CLEAN, switch_core_media_get_rtp_session(), switch_core_session_get_channel(), switch_get_addr(), switch_jb_put_packet(), SWITCH_LOG_CONSOLE, SWITCH_LOG_ERROR, SWITCH_LOG_INFO, switch_log_printf(), SWITCH_LOG_WARNING, SWITCH_MEDIA_TYPE_AUDIO, switch_mutex_lock(), switch_mutex_unlock(), switch_rtp_clear_flag(), SWITCH_RTP_FLAG_DEBUG_RTP_READ, SWITCH_RTP_FLAG_PROXY_MEDIA, SWITCH_RTP_FLAG_SECURE_RECV, SWITCH_RTP_FLAG_SECURE_RECV_MKI, SWITCH_RTP_FLAG_SECURE_RECV_RESET, SWITCH_RTP_FLAG_SRTP_HANGUP_ON_ERROR, SWITCH_RTP_FLAG_UDPTL, switch_sockaddr_get_port(), SWITCH_STATUS_FALSE, SWITCH_STATUS_GENERR, and WARN_SRTP_ERRS.

Referenced by read_rtp_packet().

5851 {
5854 
5855  if (!rtp_session) {
5856  return SWITCH_STATUS_GENERR;
5857  }
5858 
5859  if (*bytes && rtp_bundle_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_READ]) {
5860  const char *tx_host;
5861  const char *old_host;
5862  const char *my_host;
5863 
5864  char bufa[50], bufb[50], bufc[50];
5865 
5866 
5867  tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtp_from_addr);
5868  old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
5869  my_host = switch_get_addr(bufc, sizeof(bufc), rtp_session->local_addr);
5870 
5872  "R %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n",
5873  rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "No-Name",
5874  (long) *bytes,
5875  my_host, switch_sockaddr_get_port(rtp_session->local_addr),
5876  old_host, rtp_session->remote_port,
5877  tx_host, switch_sockaddr_get_port(rtp_session->rtp_from_addr),
5878  rtp_session->last_rtp_hdr.pt, ntohl(rtp_session->last_rtp_hdr.ts), ntohs(rtp_session->last_rtp_hdr.seq),
5879  rtp_session->last_rtp_hdr.m);
5880  }
5881 
5882  if (!rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
5883 #ifdef ENABLE_SRTP
5884  switch_mutex_lock(rtp_session->ice_mutex);
5885  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && rtp_session->has_rtp) {
5886  //if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && (!rtp_session->ice.ice_user || rtp_session->has_rtp)) {
5887  int sbytes = (int) *bytes;
5888  srtp_err_status_t stat = 0;
5889 
5890  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_RESET] || !rtp_session->recv_ctx[rtp_session->srtp_idx_rtp]) {
5892  srtp_dealloc(rtp_session->recv_ctx[rtp_session->srtp_idx_rtp]);
5893  rtp_session->recv_ctx[rtp_session->srtp_idx_rtp] = NULL;
5894  if ((stat = srtp_create(&rtp_session->recv_ctx[rtp_session->srtp_idx_rtp],
5895  &rtp_session->recv_policy[rtp_session->srtp_idx_rtp])) || !rtp_session->recv_ctx[rtp_session->srtp_idx_rtp]) {
5896 
5897  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] = 0;
5898  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error! RE-Activating Secure RTP RECV\n");
5899  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] = 0;
5900  switch_mutex_unlock(rtp_session->ice_mutex);
5901  return SWITCH_STATUS_FALSE;
5902  } else {
5903 
5904  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "RE-Activating Secure RTP RECV\n");
5905  rtp_session->srtp_errs[rtp_session->srtp_idx_rtp] = 0;
5906  }
5907  }
5908 
5909  if (!(*flags & SFF_PLC) && rtp_session->recv_ctx[rtp_session->srtp_idx_rtp]) {
5910  if (!rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_MKI]) {
5911  stat = srtp_unprotect(rtp_session->recv_ctx[rtp_session->srtp_idx_rtp], &rtp_session->recv_msg.header, &sbytes);
5912  } else {
5913  stat = srtp_unprotect_mki(rtp_session->recv_ctx[rtp_session->srtp_idx_rtp], &rtp_session->recv_msg.header, &sbytes, 1);
5914  }
5915 
5916  // if (rtp_session->flags[SWITCH_RTP_FLAG_NACK] && stat == srtp_err_status_replay_fail) {
5917  // /* false alarm nack */
5918  // switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "REPLAY ERR, FALSE NACK\n");
5919  // sbytes = 0;
5920  // *bytes = 0;
5921  // if (rtp_session->stats.rtcp.pkt_count) {
5922  // rtp_session->stats.rtcp.period_pkt_count--;
5923  // rtp_session->stats.rtcp.pkt_count--;
5924  // }
5925  // switch_mutex_unlock(rtp_session->ice_mutex);
5926  // goto more;
5927  // }
5928  }
5929 
5930  if (stat && rtp_session->recv_msg.header.pt != rtp_session->recv_te && rtp_session->recv_msg.header.pt != rtp_session->cng_pt) {
5931  int errs = ++rtp_session->srtp_errs[rtp_session->srtp_idx_rtp];
5932  if (rtp_session->flags[SWITCH_RTP_FLAG_SRTP_HANGUP_ON_ERROR] && stat != srtp_err_status_replay_old) {
5933  char *msg;
5934  switch_srtp_err_to_txt(stat, &msg);
5935  if (errs >= MAX_SRTP_ERRS) {
5936  switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
5938  "SRTP %s unprotect failed with code %d (%s) %ld bytes %d errors\n",
5939  rtp_type(rtp_session), stat, msg, (long)*bytes, errs);
5941  "Ending call due to SRTP error\n");
5943  } else if (errs >= WARN_SRTP_ERRS && !(errs % WARN_SRTP_ERRS)) {
5945  "SRTP %s unprotect failed with code %d (%s) %ld bytes %d errors\n",
5946  rtp_type(rtp_session), stat, msg, (long)*bytes, errs);
5947  }
5948  }
5949  sbytes = 0;
5950  } else {
5951  rtp_session->srtp_errs[rtp_session->srtp_idx_rtp] = 0;
5952  }
5953 
5954  *bytes = sbytes;
5955  }
5956  switch_mutex_unlock(rtp_session->ice_mutex);
5957 #endif
5958  }
5959 
5960  if (!jb_valid(rtp_bundle_session)) {
5961  dtls_set_state(rtp_bundle_session->dtls, DS_READY);
5962  }
5963 
5964  if (rtp_bundle_session->jb && !rtp_bundle_session->pause_jb && jb_valid(rtp_bundle_session)) {
5965  status = switch_jb_put_packet(rtp_bundle_session->jb, (switch_rtp_packet_t *) &rtp_session->recv_msg, *bytes);
5966  }
5967 
5968  *bytes = 0;
5969 
5970  return status;
5971 }
#define switch_channel_hangup(channel, hangup_cause)
Hangup a channel flagging it&#39;s state machine to end.
#define SWITCH_CHANNEL_SESSION_LOG(x)
uint16_t switch_sockaddr_get_port(switch_sockaddr_t *sa)
Definition: switch_apr.c:1001
#define WARN_SRTP_ERRS
Definition: switch_rtp.c:78
switch_sockaddr_t * remote_addr
Definition: switch_rtp.c:344
uint32_t srtp_errs[2]
Definition: switch_rtp.c:363
switch_port_t remote_port
Definition: switch_rtp.c:421
uint8_t has_rtp
Definition: switch_rtp.c:486
#define MAX_SRTP_ERRS
Definition: switch_rtp.c:79
switch_rtp_t * switch_core_media_get_rtp_session(switch_core_session_t *session, switch_media_type_t type)
switch_status_t switch_jb_put_packet(switch_jb_t *jb, switch_rtp_packet_t *packet, switch_size_t len)
#define rtp_type(rtp_session)
Definition: switch_rtp.c:544
switch_payload_t cng_pt
Definition: switch_rtp.c:429
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
_Ret_ switch_channel_t * switch_core_session_get_channel(_In_ switch_core_session_t *session)
Retrieve a pointer to the channel object associated with a given session.
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:396
srtp_ctx_t * recv_ctx[2]
Definition: switch_rtp.c:358
switch_sockaddr_t * rtp_from_addr
Definition: switch_rtp.c:398
switch_sockaddr_t * local_addr
Definition: switch_rtp.c:330
switch_dtls_t * dtls
Definition: switch_rtp.c:370
#define dtls_set_state(_dtls, _state)
Definition: switch_rtp.c:3212
#define SWITCH_CHANNEL_SESSION_LOG_CLEAN(x)
void switch_rtp_clear_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Clear an RTP Flag.
Definition: switch_rtp.c:5434
switch_jb_t * jb
Definition: switch_rtp.c:438
switch_status_t
Common return values.
srtp_hdr_t header
Definition: switch_rtp.c:112
rtp_msg_t recv_msg
Definition: switch_rtp.c:345
srtp_policy_t recv_policy[2]
Definition: switch_rtp.c:361
switch_mutex_t * ice_mutex
Definition: switch_rtp.c:433
switch_core_session_t * session
Definition: switch_rtp.c:476
uint8_t pause_jb
Definition: switch_rtp.c:469
int srtp_idx_rtp
Definition: switch_rtp.c:367
static int jb_valid(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:5665
switch_payload_t recv_te
Definition: switch_rtp.c:428
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
const char * switch_get_addr(char *buf, switch_size_t len, switch_sockaddr_t *in)
Definition: switch_apr.c:979
rtp_hdr_t last_rtp_hdr
Definition: switch_rtp.c:373
char * switch_channel_get_name(switch_channel_t *channel)
Retrieve the name of a given channel.

◆ read_rtcp_packet()

static switch_status_t read_rtcp_packet ( switch_rtp_t rtp_session,
switch_size_t bytes,
switch_frame_flag_t flags 
)
static

Definition at line 7160 of file switch_rtp.c.

References b, switch_dtls_s::bytes, switch_dtls_s::data, do_dtls(), switch_rtp::eff_remote_host_str, enable_remote_rtcp_socket(), switch_rtp::flags, handle_ice(), rtcp_msg_t::header, switch_rtp::ice_mutex, switch_rtp_ice_t::ice_user, if(), switch_rtp::pool, process_rtcp_packet(), switch_rtp::recv_ctx, switch_rtp::remote_rtcp_port, switch_rtp::rtcp_auto_adj_used, switch_rtp::rtcp_autoadj_tally, switch_rtp::rtcp_autoadj_threshold, switch_rtp::rtcp_dtls, switch_rtp::rtcp_from_addr, switch_rtp::rtcp_ice, switch_rtp::rtcp_recv_msg_p, switch_rtp::rtcp_remote_addr, switch_rtp::rtcp_sock_input, RTP_BUG_ALWAYS_AUTO_ADJUST, switch_rtp::rtp_bugs, rtp_type, switch_rtp::session, switch_rtp::srtp_idx_rtcp, switch_assert, SWITCH_CHANNEL_SESSION_LOG, switch_cmp_addr(), switch_core_strdup, SWITCH_FALSE, switch_get_addr(), SWITCH_LOG_DEBUG, SWITCH_LOG_ERROR, SWITCH_LOG_INFO, switch_log_printf(), switch_mutex_lock(), switch_mutex_unlock(), switch_rtp_clear_flag(), SWITCH_RTP_FLAG_ENABLE_RTCP, SWITCH_RTP_FLAG_RTCP_AUTOADJ, SWITCH_RTP_FLAG_SECURE_RECV, SWITCH_RTP_FLAG_SECURE_RECV_MKI, switch_rtp_set_flag(), switch_sockaddr_get_port(), switch_socket_recvfrom(), SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, and switch_rtcp_hdr_s::version.

Referenced by rtp_common_read().

7161 {
7163 
7164  if (!rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
7165  return SWITCH_STATUS_FALSE;
7166  }
7167 
7168  switch_assert(bytes);
7169 
7170  *bytes = sizeof(rtcp_msg_t);
7171 
7172  if ((status = switch_socket_recvfrom(rtp_session->rtcp_from_addr, rtp_session->rtcp_sock_input, 0, (void *) rtp_session->rtcp_recv_msg_p, bytes))
7173  != SWITCH_STATUS_SUCCESS) {
7174  *bytes = 0;
7175  }
7176 
7177  switch_mutex_lock(rtp_session->ice_mutex);
7178  if (rtp_session->rtcp_dtls) {
7179  char *b = (char *) rtp_session->rtcp_recv_msg_p;
7180 
7181  if (*b == 0 || *b == 1) {
7182  if (rtp_session->rtcp_ice.ice_user) {
7183  handle_ice(rtp_session, &rtp_session->rtcp_ice, (void *) rtp_session->rtcp_recv_msg_p, *bytes);
7184  }
7185  *bytes = 0;
7186  }
7187 
7188  if (*bytes && (*b >= 20) && (*b <= 64)) {
7189  rtp_session->rtcp_dtls->bytes = *bytes;
7190  rtp_session->rtcp_dtls->data = (void *) rtp_session->rtcp_recv_msg_p;
7191  } else {
7192  rtp_session->rtcp_dtls->bytes = 0;
7193  rtp_session->rtcp_dtls->data = NULL;
7194  }
7195 
7196  do_dtls(rtp_session, rtp_session->rtcp_dtls);
7197 
7198 
7199  if (rtp_session->rtcp_dtls->bytes) {
7200  *bytes = 0;
7201  }
7202  }
7203 
7204 #ifdef ENABLE_SRTP
7205  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && rtp_session->rtcp_recv_msg_p->header.version == 2) {
7206  //if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && (!rtp_session->ice.ice_user || rtp_session->rtcp_recv_msg_p->header.version == 2)) {
7207  int sbytes = (int) *bytes;
7208  srtp_err_status_t stat = 0;
7209 
7210 
7211  if (!rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_MKI]) {
7212  stat = srtp_unprotect_rtcp(rtp_session->recv_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_recv_msg_p->header, &sbytes);
7213  } else {
7214  stat = srtp_unprotect_rtcp_mki(rtp_session->recv_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_recv_msg_p->header, &sbytes, 1);
7215  }
7216 
7217  if (stat) {
7218  //++rtp_session->srtp_errs[rtp_session->srtp_idx_rtp]++;
7219  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "RTCP UNPROTECT ERR\n");
7220  } else {
7221  //rtp_session->srtp_errs[rtp_session->srtp_idx_rtp] = 0;
7222  }
7223 
7224  *bytes = sbytes;
7225 
7226  }
7227 #endif
7228 
7229  switch_mutex_unlock(rtp_session->ice_mutex);
7230 
7231 
7232  /* RTCP Auto ADJ */
7233  if (*bytes && rtp_session->flags[SWITCH_RTP_FLAG_RTCP_AUTOADJ] && switch_sockaddr_get_port(rtp_session->rtcp_from_addr)) {
7234  if (!switch_cmp_addr(rtp_session->rtcp_from_addr, rtp_session->rtcp_remote_addr, SWITCH_FALSE)) {
7235  if (++rtp_session->rtcp_autoadj_tally >= rtp_session->rtcp_autoadj_threshold) {
7236  const char *err;
7237  uint32_t old = rtp_session->remote_rtcp_port;
7238  const char *tx_host;
7239  const char *old_host;
7240  char bufa[50], bufb[50];
7241 
7242  tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtcp_from_addr);
7243  old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->rtcp_remote_addr);
7244 
7246  "Auto Changing %s RTCP port from %s:%u to %s:%u\n", rtp_type(rtp_session), old_host, old, tx_host,
7248 
7249 
7250  rtp_session->eff_remote_host_str = switch_core_strdup(rtp_session->pool, tx_host);
7251  rtp_session->remote_rtcp_port = switch_sockaddr_get_port(rtp_session->rtcp_from_addr);
7252  status = enable_remote_rtcp_socket(rtp_session, &err);
7253  rtp_session->rtcp_auto_adj_used = 1;
7254 
7255  if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) {
7257  } else {
7259  }
7260  }
7261  } else {
7262 
7263  if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) {
7265  } else {
7267  SWITCH_LOG_DEBUG, "Correct %s RTCP ip/port confirmed.\n", rtp_type(rtp_session));
7269  }
7270  rtp_session->rtcp_auto_adj_used = 0;
7271 
7272  }
7273  }
7274 
7275  if (*bytes) {
7276  return process_rtcp_packet(rtp_session, bytes);
7277  }
7278 
7279  return status;
7280 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
uint16_t switch_sockaddr_get_port(switch_sockaddr_t *sa)
Definition: switch_apr.c:1001
switch_sockaddr_t * rtcp_from_addr
Definition: switch_rtp.c:398
uint32_t rtcp_autoadj_tally
Definition: switch_rtp.c:355
switch_port_t remote_rtcp_port
Definition: switch_rtp.c:423
#define switch_core_strdup(_pool, _todup)
Copy a string using memory allocation from a given pool.
Definition: switch_core.h:733
const cJSON *const b
Definition: switch_cJSON.h:243
int srtp_idx_rtcp
Definition: switch_rtp.c:368
switch_socket_t * rtcp_sock_input
Definition: switch_rtp.c:326
switch_rtcp_hdr_t header
Definition: switch_rtp.c:184
switch_status_t switch_socket_recvfrom(switch_sockaddr_t *from, switch_socket_t *sock, int32_t flags, char *buf, size_t *len)
Definition: switch_apr.c:1021
static int do_dtls(switch_rtp_t *rtp_session, switch_dtls_t *dtls)
Definition: switch_rtp.c:3367
uint32_t rtcp_autoadj_threshold
Definition: switch_rtp.c:354
#define rtp_type(rtp_session)
Definition: switch_rtp.c:544
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
if((uint32_t)(unpack->cur - unpack->buf) > unpack->buflen)
int switch_cmp_addr(switch_sockaddr_t *sa1, switch_sockaddr_t *sa2, switch_bool_t ip_only)
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:396
srtp_ctx_t * recv_ctx[2]
Definition: switch_rtp.c:358
switch_byte_t rtcp_auto_adj_used
Definition: switch_rtp.c:468
switch_rtp_ice_t rtcp_ice
Definition: switch_rtp.c:402
switch_memory_pool_t * pool
Definition: switch_rtp.c:397
void switch_rtp_clear_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Clear an RTP Flag.
Definition: switch_rtp.c:5434
static void handle_ice(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice, void *data, switch_size_t len)
Definition: switch_rtp.c:956
void switch_rtp_set_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Set an RTP Flag.
Definition: switch_rtp.c:5375
switch_status_t
Common return values.
static switch_status_t enable_remote_rtcp_socket(switch_rtp_t *rtp_session, const char **err)
Definition: switch_rtp.c:2676
switch_mutex_t * ice_mutex
Definition: switch_rtp.c:433
switch_core_session_t * session
Definition: switch_rtp.c:476
switch_dtls_t * rtcp_dtls
Definition: switch_rtp.c:371
switch_rtp_bug_flag_t rtp_bugs
Definition: switch_rtp.c:453
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
const char * switch_get_addr(char *buf, switch_size_t len, switch_sockaddr_t *in)
Definition: switch_apr.c:979
static switch_status_t process_rtcp_packet(switch_rtp_t *rtp_session, switch_size_t *bytes)
Definition: switch_rtp.c:7102
#define switch_assert(expr)
switch_sockaddr_t * rtcp_remote_addr
Definition: switch_rtp.c:344
switch_size_t bytes
Definition: switch_rtp.c:288
char * eff_remote_host_str
Definition: switch_rtp.c:406
rtcp_msg_t * rtcp_recv_msg_p
Definition: switch_rtp.c:347

◆ read_rtp_packet()

static switch_status_t read_rtp_packet ( switch_rtp_t rtp_session,
switch_size_t bytes,
switch_frame_flag_t flags,
payload_map_t **  pmapP,
switch_status_t  poll_status,
switch_bool_t  return_jb_packet 
)
static

Definition at line 5973 of file switch_rtp.c.

References payload_map_s::allocated, b, switch_dtls_s::bytes, CF_AUDIO_VIDEO_BUNDLE, check_jitter(), check_recv_payload(), check_rtcp_and_ice(), switch_rtp::clean, switch_rtp_numbers_t::cng_packet_count, switch_rtp::cng_pt, switch_dtls_s::data, do_dtls(), switch_rtp::dtls, switch_rtp_numbers_t::dtmf_packet_count, rtp_msg_t::ebody, rtp_msg_t::ext, switch_rtp::flag_mutex, switch_rtp::flags, switch_rtp_numbers_t::flush_packet_count, switch_rtp::from_addr, get_recv_payload(), handle_ice(), switch_rtp::has_ice, switch_rtp::has_rtcp, switch_rtp::has_rtp, rtp_msg_t::header, rtcp_msg_t::header, switch_rtp::hot_hits, switch_rtp::ice, switch_rtp::ice_mutex, switch_rtp_ice_t::ice_user, switch_rtp_stats_t::inbound, switch_timer::interval, switch_rtp::jb, switch_rtp_numbers_t::jb_packet_count, jb_valid(), switch_rtp::last_cng_ts, switch_rtp::last_flush_packet_count, switch_rtp::last_jb_read_ssrc, switch_rtp::last_media, switch_rtp::last_read_time, switch_rtp::last_read_ts, switch_rtp::last_rtp_hdr, switch_rtp::last_seq, length, switch_rtp_hdr_ext_t::length, switch_rtp::local_addr, MAX_SRTP_ERRS, switch_rtp_numbers_t::media_bytes, switch_rtp_numbers_t::media_packet_count, switch_rtp::media_timeout, memset(), switch_rtp::missed_count, payload_map_s::negotiated, payload_map_s::next, switch_rtp_numbers_t::packet_count, switch_rtp::pause_jb, switch_rtcp_numbers_t::period_pkt_count, switch_rtcp_numbers_t::pkt_count, switch_rtp::pmaps, switch_rtp::prev_read_ts, payload_map_s::pt, switch_rtp::punts, switch_rtp_numbers_t::raw_bytes, read_bundle_rtp_packet(), switch_rtp::read_pollfd, switch_rtp::recv_ctx, switch_rtp::recv_msg, switch_rtp::recv_policy, switch_rtp::recv_te, switch_rtp::remote_addr, switch_rtp::remote_port, switch_rtp::remote_ssrc, switch_rtp_stats_t::rtcp, switch_rtp::rtcp_dtls, switch_rtp::rtcp_recv_msg_p, rtcp_stats(), RTP_BODY, RTP_BUG_ACCEPT_ANY_PACKETS, RTP_BUG_ACCEPT_ANY_PAYLOAD, RTP_BUG_IGNORE_MARK_BIT, switch_rtp::rtp_bugs, switch_rtp::rtp_from_addr, rtp_header_len, rtp_type, rtp_write_ready(), switch_rtp::samples_per_interval, switch_rtp::session, SFF_PLC, SFF_PROXY_PACKET, SFF_RTCP, switch_rtp::skip_timer, switch_rtp::sock_input, switch_rtp::srtp_errs, switch_rtp::srtp_idx_rtcp, switch_rtp::srtp_idx_rtp, switch_rtp::stats, switch_assert, SWITCH_CAUSE_SRTP_READ_ERROR, switch_channel_get_name(), switch_channel_hangup, SWITCH_CHANNEL_LOG, SWITCH_CHANNEL_SESSION_LOG, SWITCH_CHANNEL_SESSION_LOG_CLEAN, switch_channel_test_flag(), switch_core_session_get_channel(), switch_core_session_get_rtp_pt(), switch_core_session_request_video_refresh, switch_core_timer_sync(), switch_cp_addr(), switch_get_addr(), switch_ivr_parse_all_messages(), switch_jb_destroy(), switch_jb_get_last_read_len(), switch_jb_get_packet(), switch_jb_put_packet(), switch_jb_reset(), SWITCH_LOG_ALERT, SWITCH_LOG_CONSOLE, SWITCH_LOG_DEBUG1, SWITCH_LOG_ERROR, SWITCH_LOG_INFO, switch_log_printf(), SWITCH_LOG_WARNING, SWITCH_MEDIA_TYPE_AUDIO, switch_micro_time_now(), switch_mutex_lock(), switch_mutex_unlock(), switch_poll(), switch_rtp_clear_flag(), SWITCH_RTP_FLAG_BYTESWAP, SWITCH_RTP_FLAG_DEBUG_RTP_READ, SWITCH_RTP_FLAG_DETECT_SSRC, SWITCH_RTP_FLAG_ENABLE_RTCP, SWITCH_RTP_FLAG_FLUSH, SWITCH_RTP_FLAG_KILL_JB, SWITCH_RTP_FLAG_NACK, SWITCH_RTP_FLAG_PROXY_MEDIA, SWITCH_RTP_FLAG_RTCP_MUX, SWITCH_RTP_FLAG_SECURE_RECV, SWITCH_RTP_FLAG_SECURE_RECV_MKI, SWITCH_RTP_FLAG_SECURE_RECV_RESET, SWITCH_RTP_FLAG_SRTP_HANGUP_ON_ERROR, SWITCH_RTP_FLAG_TEXT, SWITCH_RTP_FLAG_UDPTL, SWITCH_RTP_FLAG_USE_TIMER, SWITCH_RTP_FLAG_VIDEO, SWITCH_RTP_MAX_BUF_LEN_WORDS, switch_rtp_set_flag(), switch_rtp_video_refresh(), switch_sockaddr_get_port(), switch_socket_recvfrom(), SWITCH_STATUS_BREAK, SWITCH_STATUS_FALSE, SWITCH_STATUS_GENERR, SWITCH_STATUS_MORE_DATA, SWITCH_STATUS_NOTFOUND, SWITCH_STATUS_RESTART, SWITCH_STATUS_SUCCESS, SWITCH_STATUS_TIMEOUT, SWITCH_STATUS_TOO_LATE, switch_swap_linear(), switch_rtp::timer, to, switch_rtcp_hdr_s::type, switch_rtp::vb, switch_rtp::vbw, switch_rtcp_hdr_s::version, and WARN_SRTP_ERRS.

Referenced by rtp_common_read().

5975 {
5977  uint32_t ts = 0;
5978  unsigned char *b = NULL;
5979  int sync = 0;
5980  switch_time_t now;
5981  switch_size_t xcheck_jitter = 0;
5982  int tries = 0;
5983  int block = 0;
5984  uint8_t audio_pt = 0;
5985  switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
5986 
5987  switch_assert(bytes);
5988  more:
5989 
5990  tries++;
5991 
5992  if (tries > 20) {
5993  if (rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) {
5994  switch_jb_reset(rtp_session->jb);
5995  }
5996  rtp_session->punts++;
5997  rtp_session->clean = 0;
5998  *bytes = 0;
5999  return SWITCH_STATUS_BREAK;
6000  }
6001 
6002  if (block) {
6003  int to = 20000;
6004  int fdr = 0;
6005 
6006  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
6007  to = 100000;
6008  } else {
6009  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && rtp_session->timer.interval) {
6010  to = rtp_session->timer.interval * 1000;
6011  }
6012  }
6013 
6014  poll_status = switch_poll(rtp_session->read_pollfd, 1, &fdr, to);
6015 
6016  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && rtp_session->timer.interval) {
6017  switch_core_timer_sync(&rtp_session->timer);
6018  }
6019 
6020  if (rtp_session->session) {
6021  switch_ivr_parse_all_messages(rtp_session->session);
6022  }
6023 
6024  block = 0;
6025  }
6026 
6027  *bytes = sizeof(rtp_msg_t);
6028  sync = 0;
6029 
6030  rtp_session->has_rtp = 0;
6031  rtp_session->has_ice = 0;
6032  rtp_session->has_rtcp = 0;
6033  if (rtp_session->dtls) {
6034  rtp_session->dtls->bytes = 0;
6035  rtp_session->dtls->data = NULL;
6036  }
6037  memset(&rtp_session->last_rtp_hdr, 0, sizeof(rtp_session->last_rtp_hdr));
6038 
6039  if (poll_status == SWITCH_STATUS_SUCCESS) {
6040  status = switch_socket_recvfrom(rtp_session->from_addr, rtp_session->sock_input, 0, (void *) &rtp_session->recv_msg, bytes);
6041  } else {
6042  *bytes = 0;
6043  }
6044 
6045  if (*bytes) {
6046  b = (unsigned char *) &rtp_session->recv_msg;
6047 
6048  /* version 2 probably rtp */
6049  rtp_session->has_rtp = (rtp_session->recv_msg.header.version == 2);
6050 
6051  if (rtp_session->media_timeout || rtp_session->ice.ice_user) {
6052  rtp_session->last_media = switch_micro_time_now();
6053  }
6054 
6055  if ((*b >= 20) && (*b <= 64)) {
6056  if (rtp_session->dtls) {
6057  rtp_session->dtls->bytes = *bytes;
6058  rtp_session->dtls->data = (void *) &rtp_session->recv_msg;
6059  }
6060  rtp_session->has_ice = 0;
6061  rtp_session->has_rtp = 0;
6062  rtp_session->has_rtcp = 0;
6063  } else if (*b == 0 || *b == 1) {
6064  rtp_session->has_ice = 1;
6065  rtp_session->has_rtp = 0;
6066  rtp_session->has_rtcp = 0;
6067  } else {
6068  if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
6069  switch(rtp_session->recv_msg.header.pt) {
6070  case 64: // 192 Full INTRA-frame request.
6071  case 72: // 200 Sender report.
6072  case 73: // 201 Receiver report.
6073  case 74: // 202 Source description.
6074  case 75: // 203 Goodbye.
6075  case 76: // 204 Application-defined.
6076  case 77: // 205 Transport layer FB message.
6077  case 78: // 206 Payload-specific FB message.
6078  case 79: // 207 Extended report.
6079  rtp_session->has_rtcp = 1;
6080  rtp_session->has_rtp = 0;
6081  rtp_session->has_ice = 0;
6082  break;
6083  default:
6084  if (rtp_session->rtcp_recv_msg_p->header.version == 2 &&
6085  rtp_session->rtcp_recv_msg_p->header.type > 199 && rtp_session->rtcp_recv_msg_p->header.type < 208) {
6086  rtp_session->has_rtcp = 1;
6087  rtp_session->has_rtp = 0;
6088  rtp_session->has_ice = 0;
6089  }
6090  break;
6091  }
6092  }
6093  }
6094 
6095  if (rtp_session->has_rtp || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
6096  rtp_session->missed_count = 0;
6097  switch_cp_addr(rtp_session->rtp_from_addr, rtp_session->from_addr);
6098  }
6099 
6100  if (rtp_session->has_rtp) {
6101  rtp_session->last_rtp_hdr = rtp_session->recv_msg.header;
6102 
6103 
6104  if (!rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] &&
6105  rtp_session->last_rtp_hdr.pt != 13 &&
6106  rtp_session->last_rtp_hdr.pt != rtp_session->recv_te &&
6107  rtp_session->last_rtp_hdr.pt != rtp_session->cng_pt) {
6108  int accept_packet = 1;
6109 
6110 
6111  if (!(rtp_session->rtp_bugs & RTP_BUG_ACCEPT_ANY_PAYLOAD) &&
6112  !(rtp_session->rtp_bugs & RTP_BUG_ACCEPT_ANY_PACKETS) && rtp_session->pmaps && *rtp_session->pmaps) {
6113  payload_map_t *pmap;
6114  accept_packet = 0;
6115 
6116  switch_mutex_lock(rtp_session->flag_mutex);
6117  for (pmap = *rtp_session->pmaps; pmap && pmap->allocated; pmap = pmap->next) {
6118 
6119  if (!pmap->negotiated) {
6120  continue;
6121  }
6122 
6123  if (rtp_session->last_rtp_hdr.pt == pmap->pt) {
6124  accept_packet = 1;
6125  if (pmapP) {
6126  *pmapP = pmap;
6127  }
6128  break;
6129  }
6130  }
6131  switch_mutex_unlock(rtp_session->flag_mutex);
6132  }
6133 
6136  if (rtp_session->last_rtp_hdr.pt == audio_pt) {
6137  accept_packet = 1;
6138 
6139  return read_bundle_rtp_packet(rtp_session, bytes, flags);
6140  }
6141  }
6142 
6143  if (!accept_packet) {
6145  "Invalid Packet SEQ: %d TS: %d PT:%d ignored\n",
6146  ntohs(rtp_session->recv_msg.header.seq), ntohl(rtp_session->last_rtp_hdr.ts), rtp_session->last_rtp_hdr.pt);
6147  *bytes = 0;
6148  }
6149  }
6150 
6151  if (rtp_session->flags[SWITCH_RTP_FLAG_DETECT_SSRC]) {
6152  //if (rtp_session->remote_ssrc != rtp_session->stats.rtcp.peer_ssrc && rtp_session->stats.rtcp.peer_ssrc) {
6153  // rtp_session->remote_ssrc = rtp_session->stats.rtcp.peer_ssrc;
6154  //}
6155 
6156  if (rtp_session->remote_ssrc != rtp_session->last_rtp_hdr.ssrc && rtp_session->last_rtp_hdr.ssrc) {
6157  rtp_session->remote_ssrc = ntohl(rtp_session->last_rtp_hdr.ssrc);
6158  }
6159  }
6160  }
6161  }
6162 
6163  if (!rtp_session->vb && (!rtp_session->jb || rtp_session->pause_jb || !jb_valid(rtp_session))) {
6164  if (*bytes > rtp_header_len && (rtp_session->has_rtp && check_recv_payload(rtp_session))) {
6165  xcheck_jitter = *bytes;
6166  check_jitter(rtp_session);
6167  }
6168  }
6169 
6170  if (check_rtcp_and_ice(rtp_session) == -1) {
6171  //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
6172  //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "WTF CHECK FAIL\n");
6173  //}
6174  return SWITCH_STATUS_GENERR;
6175  }
6176 
6177  if (rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
6178  goto udptl;
6179  }
6180 
6181 
6182  if (*bytes) {
6183  *flags &= ~SFF_PROXY_PACKET;
6184 
6185  //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
6186  // switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "WTF BYTES %ld b=%d\n", *bytes, *b);
6187  //}
6188 
6189 
6190  if (rtp_session->has_ice) {
6191  if (rtp_session->ice.ice_user) {
6192  handle_ice(rtp_session, &rtp_session->ice, (void *) &rtp_session->recv_msg, *bytes);
6193  }
6194  *bytes = 0;
6195  sync = 1;
6196  }
6197  }
6198 
6199  switch_mutex_lock(rtp_session->ice_mutex);
6200 
6201  if (rtp_session->dtls) {
6202 
6203  if (rtp_session->rtcp_dtls && rtp_session->rtcp_dtls != rtp_session->dtls) {
6204  rtp_session->rtcp_dtls->bytes = 0;
6205  rtp_session->rtcp_dtls->data = NULL;
6206  do_dtls(rtp_session, rtp_session->rtcp_dtls);
6207  }
6208 
6209  do_dtls(rtp_session, rtp_session->dtls);
6210 
6211  if (rtp_session->dtls && rtp_session->dtls->bytes) {
6212  *bytes = 0;
6213  sync = 1;
6214  }
6215  }
6216 
6217 
6218 
6219  if (status == SWITCH_STATUS_SUCCESS && *bytes) {
6220  if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
6221  *flags &= ~SFF_RTCP;
6222  if (rtp_session->has_rtcp) {
6223  *flags |= SFF_RTCP;
6224 
6225 #ifdef ENABLE_SRTP
6226  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV]) {
6227  int sbytes = (int) *bytes;
6228  srtp_err_status_t stat = 0;
6229 
6230  if (!rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_MKI]) {
6231  stat = srtp_unprotect_rtcp(rtp_session->recv_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_recv_msg_p->header, &sbytes);
6232  } else {
6233  stat = srtp_unprotect_rtcp_mki(rtp_session->recv_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_recv_msg_p->header, &sbytes, 1);
6234  }
6235 
6236  if (stat) {
6237  //++rtp_session->srtp_errs[rtp_session->srtp_idx_rtp]++;
6238  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "RTCP UNPROTECT ERR\n");
6239  } else {
6240  //rtp_session->srtp_errs[rtp_session->srtp_idx_rtp] = 0;
6241  }
6242 
6243  *bytes = sbytes;
6244  }
6245 #endif
6246  switch_mutex_unlock(rtp_session->ice_mutex);
6247  return SWITCH_STATUS_SUCCESS;
6248  }
6249  }
6250  }
6251 
6252  switch_mutex_unlock(rtp_session->ice_mutex);
6253 
6254  if ((*bytes && (!rtp_write_ready(rtp_session, *bytes, __LINE__) || !rtp_session->has_rtp || rtp_session->has_rtcp)) || sync) {
6255  rtp_session->hot_hits = 0;
6256  block = 1;
6257  *bytes = 0;
6258  goto more;
6259  }
6260 
6261  if (*bytes && rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_READ]) {
6262  const char *tx_host;
6263  const char *old_host;
6264  const char *my_host;
6265 
6266  char bufa[50], bufb[50], bufc[50];
6267 
6268 
6269  tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtp_from_addr);
6270  old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
6271  my_host = switch_get_addr(bufc, sizeof(bufc), rtp_session->local_addr);
6272 
6274  "R %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n",
6275  rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "No-Name",
6276  (long) *bytes,
6277  my_host, switch_sockaddr_get_port(rtp_session->local_addr),
6278  old_host, rtp_session->remote_port,
6279  tx_host, switch_sockaddr_get_port(rtp_session->rtp_from_addr),
6280  rtp_session->last_rtp_hdr.pt, ntohl(rtp_session->last_rtp_hdr.ts), ntohs(rtp_session->last_rtp_hdr.seq),
6281  rtp_session->last_rtp_hdr.m);
6282 
6283  }
6284 
6285 #ifdef RTP_READ_PLOSS
6286  {
6287  int r = (rand() % 10000) + 1;
6288  if (r <= 200) {
6290  "Simulate dropped packet ......... ts: %u seq: %u\n", ntohl(rtp_session->last_rtp_hdr.ts), ntohs(rtp_session->last_rtp_hdr.seq));
6291  *bytes = 0;
6292  }
6293  }
6294 #endif
6295 
6296 
6297 
6298  udptl:
6299 
6300  ts = 0;
6301  rtp_session->recv_msg.ebody = NULL;
6302  now = switch_micro_time_now();
6303 
6304  if (*bytes) {
6305  uint16_t seq = ntohs((uint16_t) rtp_session->last_rtp_hdr.seq);
6306  ts = ntohl(rtp_session->last_rtp_hdr.ts);
6307 
6308 #ifdef DEBUG_MISSED_SEQ
6309  if (rtp_session->last_seq && rtp_session->last_seq+1 != seq) {
6310  //2012-11-28 18:33:11.799070 [ERR] switch_rtp.c:2883 Missed -65536 RTP frames from sequence [65536] to [-1] (missed). Time since last read [20021]
6311  switch_size_t flushed_packets_diff = rtp_session->stats.inbound.flush_packet_count - rtp_session->last_flush_packet_count;
6312  switch_size_t num_missed = (switch_size_t)seq - (rtp_session->last_seq+1);
6313 
6314  if (num_missed == 1) { /* We missed one packet */
6315  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missed one RTP frame with sequence [%d]%s. Time since last read [%ld]\n",
6316  rtp_session->last_seq+1, (flushed_packets_diff == 1) ? " (flushed by FS)" : " (missed)",
6317  rtp_session->last_read_time ? now-rtp_session->last_read_time : 0);
6318  } else { /* We missed multiple packets */
6319  if (flushed_packets_diff == 0) {
6321  "Missed %ld RTP frames from sequence [%d] to [%d] (missed). Time since last read [%ld]\n",
6322  num_missed, rtp_session->last_seq+1, seq-1,
6323  rtp_session->last_read_time ? now-rtp_session->last_read_time : 0);
6324  } else if (flushed_packets_diff == num_missed) {
6326  "Missed %ld RTP frames from sequence [%d] to [%d] (flushed by FS). Time since last read [%ld]\n",
6327  num_missed, rtp_session->last_seq+1, seq-1,
6328  rtp_session->last_read_time ? now-rtp_session->last_read_time : 0);
6329  } else if (num_missed > flushed_packets_diff) {
6331  "Missed %ld RTP frames from sequence [%d] to [%d] (%ld packets flushed by FS, %ld packets missed)."
6332  " Time since last read [%ld]\n",
6333  num_missed, rtp_session->last_seq+1, seq-1,
6334  flushed_packets_diff, num_missed-flushed_packets_diff,
6335  rtp_session->last_read_time ? now-rtp_session->last_read_time : 0);
6336  } else {
6338  "Missed %ld RTP frames from sequence [%d] to [%d] (%ld packets flushed by FS). Time since last read [%ld]\n",
6339  num_missed, rtp_session->last_seq+1, seq-1,
6340  flushed_packets_diff, rtp_session->last_read_time ? now-rtp_session->last_read_time : 0);
6341  }
6342  }
6343 
6344  }
6345 #endif
6346  rtp_session->last_seq = seq;
6347 
6348 
6349  rtp_session->last_flush_packet_count = rtp_session->stats.inbound.flush_packet_count;
6350 
6351 
6352  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && now - rtp_session->last_read_time > 5000000) {
6353  switch_rtp_video_refresh(rtp_session);
6354  }
6355 
6356  rtp_session->last_read_time = now;
6357  }
6358 
6359  if (*bytes && rtp_session->has_rtp && rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]){
6360  rtcp_stats(rtp_session);
6361  }
6362 
6363 
6364  if (!rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] && !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] &&
6365  *bytes && rtp_session->last_rtp_hdr.pt != rtp_session->recv_te &&
6366  ts && !rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session) && ts == rtp_session->last_cng_ts) {
6367  /* we already sent this frame..... */
6368  *bytes = 0;
6369  return SWITCH_STATUS_SUCCESS;
6370  }
6371 
6372  if (*bytes) {
6373  if (!rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
6374 
6375 #ifdef ENABLE_SRTP
6376  switch_mutex_lock(rtp_session->ice_mutex);
6377  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && rtp_session->has_rtp &&
6378  (check_recv_payload(rtp_session) ||
6379  rtp_session->last_rtp_hdr.pt == rtp_session->recv_te ||
6380  rtp_session->last_rtp_hdr.pt == rtp_session->cng_pt)) {
6381  //if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && (!rtp_session->ice.ice_user || rtp_session->has_rtp)) {
6382  int sbytes = (int) *bytes;
6383  srtp_err_status_t stat = 0;
6384 
6385  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_RESET] || !rtp_session->recv_ctx[rtp_session->srtp_idx_rtp]) {
6387  srtp_dealloc(rtp_session->recv_ctx[rtp_session->srtp_idx_rtp]);
6388  rtp_session->recv_ctx[rtp_session->srtp_idx_rtp] = NULL;
6389  if ((stat = srtp_create(&rtp_session->recv_ctx[rtp_session->srtp_idx_rtp],
6390  &rtp_session->recv_policy[rtp_session->srtp_idx_rtp])) || !rtp_session->recv_ctx[rtp_session->srtp_idx_rtp]) {
6391 
6392  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] = 0;
6393  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error! RE-Activating Secure RTP RECV\n");
6394  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] = 0;
6395  switch_mutex_unlock(rtp_session->ice_mutex);
6396  return SWITCH_STATUS_FALSE;
6397  } else {
6398 
6399  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "RE-Activating Secure RTP RECV\n");
6400  rtp_session->srtp_errs[rtp_session->srtp_idx_rtp] = 0;
6401  }
6402  }
6403 
6404  if (!(*flags & SFF_PLC) && rtp_session->recv_ctx[rtp_session->srtp_idx_rtp]) {
6405  if (!rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_MKI]) {
6406  stat = srtp_unprotect(rtp_session->recv_ctx[rtp_session->srtp_idx_rtp], &rtp_session->recv_msg.header, &sbytes);
6407  } else {
6408  stat = srtp_unprotect_mki(rtp_session->recv_ctx[rtp_session->srtp_idx_rtp], &rtp_session->recv_msg.header, &sbytes, 1);
6409  }
6410 
6411  if (rtp_session->flags[SWITCH_RTP_FLAG_NACK] && stat == srtp_err_status_replay_fail) {
6412  /* false alarm nack */
6413  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "REPLAY ERR, FALSE NACK\n");
6414  sbytes = 0;
6415  *bytes = 0;
6416  if (rtp_session->stats.rtcp.pkt_count) {
6417  rtp_session->stats.rtcp.period_pkt_count--;
6418  rtp_session->stats.rtcp.pkt_count--;
6419  }
6420  switch_mutex_unlock(rtp_session->ice_mutex);
6421  goto more;
6422  }
6423  }
6424 
6425  if (stat && rtp_session->recv_msg.header.pt != rtp_session->recv_te && rtp_session->recv_msg.header.pt != rtp_session->cng_pt) {
6426  int errs = ++rtp_session->srtp_errs[rtp_session->srtp_idx_rtp];
6427  if (rtp_session->flags[SWITCH_RTP_FLAG_SRTP_HANGUP_ON_ERROR] && stat != srtp_err_status_replay_old) {
6428  char *msg;
6429  switch_srtp_err_to_txt(stat, &msg);
6430  if (errs >= MAX_SRTP_ERRS) {
6431  switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
6433  "SRTP %s unprotect failed with code %d (%s) %ld bytes %d errors\n",
6434  rtp_type(rtp_session), stat, msg, (long)*bytes, errs);
6436  "Ending call due to SRTP error\n");
6438  } else if (errs >= WARN_SRTP_ERRS && !(errs % WARN_SRTP_ERRS)) {
6440  "SRTP %s unprotect failed with code %d (%s) %ld bytes %d errors\n",
6441  rtp_type(rtp_session), stat, msg, (long)*bytes, errs);
6442  }
6443  }
6444  sbytes = 0;
6445  } else {
6446  rtp_session->srtp_errs[rtp_session->srtp_idx_rtp] = 0;
6447  }
6448 
6449  *bytes = sbytes;
6450  }
6451  switch_mutex_unlock(rtp_session->ice_mutex);
6452 #endif
6453  }
6454 
6455 
6456  if (rtp_session->has_rtp) {
6457  if (rtp_session->recv_msg.header.cc > 0) { /* Contributing Source Identifiers (4 bytes = sizeof CSRC header)*/
6458  rtp_session->recv_msg.ebody = RTP_BODY(rtp_session) + (rtp_session->recv_msg.header.cc * 4);
6459  }
6460 
6461  /* recalculate body length in case rtp extension used */
6462  if (!rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] &&
6463  rtp_session->recv_msg.header.x) { /* header extensions */
6464  uint16_t length;
6465 
6466  rtp_session->recv_msg.ext = (switch_rtp_hdr_ext_t *) RTP_BODY(rtp_session);
6467  length = ntohs((uint16_t)rtp_session->recv_msg.ext->length);
6468 
6469  if (length < SWITCH_RTP_MAX_BUF_LEN_WORDS) {
6470  rtp_session->recv_msg.ebody = (char *)rtp_session->recv_msg.ext + (length * 4) + 4;
6471  if (*bytes > (length * 4 + 4)) {
6472  *bytes -= (length * 4 + 4);
6473  } else {
6474  *bytes = 0;
6475  }
6476  }
6477  }
6478 
6479 
6480 #ifdef DEBUG_CHROME
6481 
6482  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->has_rtp) {
6483 
6485  "VIDEO: seq: %d ts: %u len: %ld %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x mark: %d\n",
6486  ntohs(rtp_session->last_rtp_hdr.seq), ntohl(rtp_session->last_rtp_hdr.ts), *bytes,
6487  *((uint8_t *)RTP_BODY(rtp_session)), *((uint8_t *)RTP_BODY(rtp_session) + 1),
6488  *((uint8_t *)RTP_BODY(rtp_session) + 2), *((uint8_t *)RTP_BODY(rtp_session) + 3),
6489  *((uint8_t *)RTP_BODY(rtp_session) + 4), *((uint8_t *)RTP_BODY(rtp_session) + 5),
6490  *((uint8_t *)RTP_BODY(rtp_session) + 6), *((uint8_t *)RTP_BODY(rtp_session) + 7),
6491  *((uint8_t *)RTP_BODY(rtp_session) + 8), *((uint8_t *)RTP_BODY(rtp_session) + 9),
6492  *((uint8_t *)RTP_BODY(rtp_session) + 10), rtp_session->last_rtp_hdr.m);
6493 
6494  }
6495 #endif
6496 
6497 
6498 
6499  }
6500 
6501 
6502  rtp_session->stats.inbound.raw_bytes += *bytes;
6503 
6504  if (rtp_session->last_rtp_hdr.pt == rtp_session->recv_te) {
6505  rtp_session->stats.inbound.dtmf_packet_count++;
6506  } else if (rtp_session->last_rtp_hdr.pt == rtp_session->cng_pt || rtp_session->last_rtp_hdr.pt == 13) {
6507  rtp_session->stats.inbound.cng_packet_count++;
6508  } else {
6509  rtp_session->stats.inbound.media_packet_count++;
6510  rtp_session->stats.inbound.media_bytes += *bytes;
6511  }
6512 
6513  rtp_session->stats.inbound.packet_count++;
6514  }
6515 
6516  if (!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] &&
6517  ((rtp_session->recv_te && rtp_session->last_rtp_hdr.pt == rtp_session->recv_te) ||
6518  (*bytes < rtp_header_len && *bytes > 0 && !(rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL])))) {
6519  return SWITCH_STATUS_BREAK;
6520  }
6521 
6522  if (ts) {
6523  rtp_session->prev_read_ts = rtp_session->last_read_ts;
6524  rtp_session->last_read_ts = ts;
6525  }
6526 
6527  if (rtp_session->flags[SWITCH_RTP_FLAG_BYTESWAP] && check_recv_payload(rtp_session)) {
6528  switch_swap_linear((int16_t *)RTP_BODY(rtp_session), (int) *bytes - rtp_header_len);
6529  }
6530 
6531  if (rtp_session->flags[SWITCH_RTP_FLAG_KILL_JB]) {
6532  rtp_session->flags[SWITCH_RTP_FLAG_KILL_JB] = 0;
6533 
6534  if (rtp_session->jb) {
6535  switch_jb_destroy(&rtp_session->jb);
6536  }
6537 
6538  if (rtp_session->vb) {
6539  switch_jb_destroy(&rtp_session->vb);
6540  }
6541 
6542  if (rtp_session->vbw) {
6543  switch_jb_destroy(&rtp_session->vbw);
6544  }
6545 
6546  }
6547 
6548  if (rtp_session->has_rtp && *bytes) {
6549  uint32_t read_ssrc = ntohl(rtp_session->last_rtp_hdr.ssrc);
6550 
6551  if (rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
6552  return SWITCH_STATUS_SUCCESS;
6553  }
6554 
6555  if (rtp_session->vb && !rtp_session->pause_jb && jb_valid(rtp_session)) {
6556  status = switch_jb_put_packet(rtp_session->vb, (switch_rtp_packet_t *) &rtp_session->recv_msg, *bytes);
6557 
6558  if (status == SWITCH_STATUS_TOO_LATE) {
6559  goto more;
6560  }
6561 
6562  status = SWITCH_STATUS_FALSE;
6563  *bytes = 0;
6564 
6565  if (!return_jb_packet) {
6566  return status;
6567  }
6568  }
6569 
6570  if (rtp_session->jb && jb_valid(rtp_session)) {
6571  if (rtp_session->last_jb_read_ssrc && rtp_session->last_jb_read_ssrc != read_ssrc) {
6572  switch_jb_reset(rtp_session->jb);
6573  }
6574 
6575  rtp_session->last_jb_read_ssrc = read_ssrc;
6576  }
6577 
6578  if (rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) {
6579 
6580  status = switch_jb_put_packet(rtp_session->jb, (switch_rtp_packet_t *) &rtp_session->recv_msg, *bytes);
6581  if (status == SWITCH_STATUS_TOO_LATE) {
6582  goto more;
6583  }
6584 
6585 
6586  status = SWITCH_STATUS_FALSE;
6587  *bytes = 0;
6588 
6589  if (!return_jb_packet) {
6590  return status;
6591  }
6592  } else {
6593  if (rtp_session->last_rtp_hdr.m && rtp_session->last_rtp_hdr.pt != rtp_session->recv_te &&
6594  !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && !(rtp_session->rtp_bugs & RTP_BUG_IGNORE_MARK_BIT) &&
6595  rtp_session->last_read_ts - rtp_session->prev_read_ts < rtp_session->samples_per_interval * 3) {
6597  } else if (rtp_session->last_jb_read_ssrc && rtp_session->last_jb_read_ssrc != read_ssrc) {
6599  }
6600  }
6601  }
6602 
6603  if (!*bytes || rtp_session->has_rtp) {
6604 
6605  if (rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) {
6606  switch_status_t jstatus = switch_jb_get_packet(rtp_session->jb, (switch_rtp_packet_t *) &rtp_session->recv_msg, bytes);
6607 
6608  status = jstatus;
6609 
6610  switch(jstatus) {
6612  if (rtp_session->punts < 4) {
6613  block = 1;
6614  goto more;
6615  }
6616  *bytes = 0;
6617  break;
6619  {
6620  int pt = get_recv_payload(rtp_session);
6621  (*flags) |= SFF_PLC;
6622  status = SWITCH_STATUS_SUCCESS;
6623  *bytes = switch_jb_get_last_read_len(rtp_session->jb);
6624  rtp_session->last_rtp_hdr = rtp_session->recv_msg.header;
6625  rtp_session->last_rtp_hdr.pt = pt;
6626  }
6627  break;
6628  case SWITCH_STATUS_BREAK:
6629  break;
6630  case SWITCH_STATUS_SUCCESS:
6631  case SWITCH_STATUS_TIMEOUT:
6632  default:
6633  {
6634  if (status == SWITCH_STATUS_TIMEOUT) {
6635  rtp_session->skip_timer = 1;
6636  }
6637  rtp_session->stats.inbound.jb_packet_count++;
6638  status = SWITCH_STATUS_SUCCESS;
6639  rtp_session->last_rtp_hdr = rtp_session->recv_msg.header;
6640  if (++rtp_session->clean > 200) {
6641  rtp_session->punts = 0;
6642  }
6643  if (!xcheck_jitter) {
6644  check_jitter(rtp_session);
6645  }
6646  }
6647  break;
6648  }
6649  }
6650 
6651  if (rtp_session->vb && !rtp_session->pause_jb && jb_valid(rtp_session)) {
6652  switch_status_t vstatus = switch_jb_get_packet(rtp_session->vb, (switch_rtp_packet_t *) &rtp_session->recv_msg, bytes);
6653  status = vstatus;
6654 
6655  switch(vstatus) {
6656  case SWITCH_STATUS_RESTART:
6658  status = SWITCH_STATUS_BREAK;
6659  break;
6661  status = SWITCH_STATUS_BREAK;
6662  break;
6663  case SWITCH_STATUS_BREAK:
6664  default:
6665  break;
6666  }
6667 
6668  if (vstatus == SWITCH_STATUS_NOTFOUND && rtp_session->flags[SWITCH_RTP_FLAG_TEXT]) {
6669  int pt = get_recv_payload(rtp_session);
6670  (*flags) |= SFF_PLC;
6671  status = SWITCH_STATUS_SUCCESS;
6672  *bytes = switch_jb_get_last_read_len(rtp_session->vb);
6673  rtp_session->last_rtp_hdr = rtp_session->recv_msg.header;
6674  if (pt > -1) {
6675  rtp_session->last_rtp_hdr.pt = pt;
6676  }
6677  }
6678 
6679  if (vstatus == SWITCH_STATUS_SUCCESS) {
6680  rtp_session->last_rtp_hdr = rtp_session->recv_msg.header;
6681 
6682  if (!xcheck_jitter) {
6683  check_jitter(rtp_session);
6684  }
6685  }
6686  }
6687  }
6688 
6689  return status;
6690 }
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
Definition: switch_time.c:311
switch_pollfd_t * read_pollfd
Definition: switch_rtp.c:327
switch_status_t switch_jb_get_packet(switch_jb_t *jb, switch_rtp_packet_t *packet, switch_size_t *len)
#define switch_channel_hangup(channel, hangup_cause)
Hangup a channel flagging it&#39;s state machine to end.
uint8_t has_ice
Definition: switch_rtp.c:488
char * ebody
Definition: switch_rtp.c:115
uint8_t allocated
#define SWITCH_CHANNEL_SESSION_LOG(x)
uint16_t switch_sockaddr_get_port(switch_sockaddr_t *sa)
Definition: switch_apr.c:1001
switch_rtp_numbers_t inbound
Definition: switch_types.h:771
#define WARN_SRTP_ERRS
Definition: switch_rtp.c:78
#define SWITCH_CHANNEL_LOG
int skip_timer
Definition: switch_rtp.c:492
switch_sockaddr_t * remote_addr
Definition: switch_rtp.c:344
switch_mutex_t * flag_mutex
Definition: switch_rtp.c:430
uint32_t srtp_errs[2]
Definition: switch_rtp.c:363
cJSON *const to
switch_socket_t * sock_input
Definition: switch_rtp.c:326
const cJSON *const b
Definition: switch_cJSON.h:243
switch_port_t remote_port
Definition: switch_rtp.c:421
int srtp_idx_rtcp
Definition: switch_rtp.c:368
uint32_t last_jb_read_ssrc
Definition: switch_rtp.c:378
uint16_t last_seq
Definition: switch_rtp.c:470
switch_size_t switch_jb_get_last_read_len(switch_jb_t *jb)
uint32_t media_timeout
Definition: switch_rtp.c:444
uint8_t has_rtp
Definition: switch_rtp.c:486
#define MAX_SRTP_ERRS
Definition: switch_rtp.c:79
switch_jb_t * vbw
Definition: switch_rtp.c:440
switch_status_t switch_jb_put_packet(switch_jb_t *jb, switch_rtp_packet_t *packet, switch_size_t len)
switch_rtcp_hdr_t header
Definition: switch_rtp.c:184
switch_status_t switch_jb_destroy(switch_jb_t **jbp)
switch_size_t media_bytes
Definition: switch_types.h:694
switch_size_t dtmf_packet_count
Definition: switch_types.h:700
uint32_t switch_channel_test_flag(switch_channel_t *channel, switch_channel_flag_t flag)
Test for presence of given flag on a given channel.
switch_status_t switch_socket_recvfrom(switch_sockaddr_t *from, switch_socket_t *sock, int32_t flags, char *buf, size_t *len)
Definition: switch_apr.c:1021
switch_time_t last_media
Definition: switch_rtp.c:443
static int do_dtls(switch_rtp_t *rtp_session, switch_dtls_t *dtls)
Definition: switch_rtp.c:3367
struct payload_map_s * next
#define rtp_type(rtp_session)
Definition: switch_rtp.c:544
switch_payload_t cng_pt
Definition: switch_rtp.c:429
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
_Ret_ switch_channel_t * switch_core_session_get_channel(_In_ switch_core_session_t *session)
Retrieve a pointer to the channel object associated with a given session.
int64_t switch_time_t
Definition: switch_apr.h:188
uint32_t prev_read_ts
Definition: switch_rtp.c:388
switch_rtcp_numbers_t rtcp
Definition: switch_types.h:773
static int get_recv_payload(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:5827
switch_size_t raw_bytes
Definition: switch_types.h:693
static int rtcp_stats(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:1956
switch_time_t last_read_time
Definition: switch_rtp.c:473
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:396
#define switch_core_session_request_video_refresh(_s)
Definition: switch_core.h:2892
uint8_t switch_core_session_get_rtp_pt(switch_core_session_t *session, switch_media_type_t type)
srtp_ctx_t * recv_ctx[2]
Definition: switch_rtp.c:358
switch_sockaddr_t * rtp_from_addr
Definition: switch_rtp.c:398
static switch_status_t read_bundle_rtp_packet(switch_rtp_t *rtp_session, switch_size_t *bytes, switch_frame_flag_t *flags)
Definition: switch_rtp.c:5850
uint32_t last_read_ts
Definition: switch_rtp.c:387
char const int length
Definition: switch_cJSON.h:153
static int rtp_write_ready(switch_rtp_t *rtp_session, uint32_t bytes, int line)
Definition: switch_rtp.c:8273
uintptr_t switch_size_t
uint8_t negotiated
switch_sockaddr_t * local_addr
Definition: switch_rtp.c:330
switch_payload_t pt
void switch_swap_linear(int16_t *buf, int len)
Perform a byteswap on a buffer of 16 bit samples.
switch_dtls_t * dtls
Definition: switch_rtp.c:370
switch_size_t packet_count
Definition: switch_types.h:695
switch_jb_t * vb
Definition: switch_rtp.c:439
switch_rtp_stats_t stats
Definition: switch_rtp.c:454
switch_status_t switch_poll(switch_pollfd_t *aprset, int32_t numsock, int32_t *nsds, switch_interval_time_t timeout)
Definition: switch_apr.c:1100
switch_size_t jb_packet_count
Definition: switch_types.h:699
#define SWITCH_CHANNEL_SESSION_LOG_CLEAN(x)
uint8_t punts
Definition: switch_rtp.c:489
#define RTP_BODY(_s)
Definition: switch_rtp.c:118
void switch_rtp_clear_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Clear an RTP Flag.
Definition: switch_rtp.c:5434
payload_map_t ** pmaps
Definition: switch_rtp.c:477
static void handle_ice(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice, void *data, switch_size_t len)
Definition: switch_rtp.c:956
void switch_rtp_set_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Set an RTP Flag.
Definition: switch_rtp.c:5375
switch_rtp_hdr_ext_t * ext
Definition: switch_rtp.c:114
switch_jb_t * jb
Definition: switch_rtp.c:438
switch_status_t
Common return values.
uint32_t missed_count
Definition: switch_rtp.c:442
uint32_t samples_per_interval
Definition: switch_rtp.c:409
srtp_hdr_t header
Definition: switch_rtp.c:112
switch_size_t last_flush_packet_count
Definition: switch_rtp.c:474
switch_timer_t timer
Definition: switch_rtp.c:434
rtp_msg_t recv_msg
Definition: switch_rtp.c:345
srtp_policy_t recv_policy[2]
Definition: switch_rtp.c:361
switch_mutex_t * ice_mutex
Definition: switch_rtp.c:433
switch_core_session_t * session
Definition: switch_rtp.c:476
#define SWITCH_RTP_MAX_BUF_LEN_WORDS
Definition: switch_rtp.h:47
switch_size_t flush_packet_count
Definition: switch_types.h:702
static int check_rtcp_and_ice(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:2137
uint8_t has_rtcp
Definition: switch_rtp.c:487
void switch_jb_reset(switch_jb_t *jb)
uint32_t remote_ssrc
Definition: switch_rtp.c:377
switch_dtls_t * rtcp_dtls
Definition: switch_rtp.c:371
uint8_t pause_jb
Definition: switch_rtp.c:469
switch_rtp_ice_t ice
Definition: switch_rtp.c:401
switch_size_t media_packet_count
Definition: switch_types.h:697
switch_rtp_bug_flag_t rtp_bugs
Definition: switch_rtp.c:453
int srtp_idx_rtp
Definition: switch_rtp.c:367
static int jb_valid(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:5665
switch_payload_t recv_te
Definition: switch_rtp.c:428
uint32_t hot_hits
Definition: switch_rtp.c:460
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
static void check_jitter(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:1659
const char * switch_get_addr(char *buf, switch_size_t len, switch_sockaddr_t *in)
Definition: switch_apr.c:979
uint32_t last_cng_ts
Definition: switch_rtp.c:389
switch_sockaddr_t * from_addr
Definition: switch_rtp.c:398
#define switch_assert(expr)
void switch_rtp_video_refresh(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:5059
rtp_hdr_t last_rtp_hdr
Definition: switch_rtp.c:373
char * switch_channel_get_name(switch_channel_t *channel)
Retrieve the name of a given channel.
static int check_recv_payload(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:5802
switch_size_t bytes
Definition: switch_rtp.c:288
memset(buf, 0, buflen)
uint8_t clean
Definition: switch_rtp.c:490
#define rtp_header_len
Definition: switch_rtp.c:73
rtcp_msg_t * rtcp_recv_msg_p
Definition: switch_rtp.c:347
int switch_cp_addr(switch_sockaddr_t *sa1, switch_sockaddr_t *sa2)
switch_status_t switch_core_timer_sync(switch_timer_t *timer)
switch_size_t cng_packet_count
Definition: switch_types.h:701
switch_status_t switch_ivr_parse_all_messages(switch_core_session_t *session)
Definition: switch_ivr.c:847

◆ reset_jitter_seq()

static void reset_jitter_seq ( switch_rtp_t rtp_session)
static

◆ rtcp_generate_report_block()

static void rtcp_generate_report_block ( switch_rtp_t rtp_session,
struct switch_rtcp_report_block rtcp_report_block,
int16_t  extra_expected 
)
static

Definition at line 1839 of file switch_rtp.c.

References switch_rtcp_numbers_t::base_seq, calc_local_lsr_now(), switch_rtcp_numbers_t::cum_lost, switch_rtcp_numbers_t::cycle, switch_rtcp_report_block_frame::dlsr, switch_rtcp_report_block::dlsr, switch_rtp::flags, switch_rtcp_report_block::fraction, switch_rtcp_numbers_t::high_ext_seq_recv, switch_rtcp_report_block::highest_sequence_number_received, switch_rtcp_numbers_t::inter_jitter, switch_rtcp_report_block::jitter, switch_rtcp_numbers_t::last_recv_lsr_local, switch_rtcp_numbers_t::last_recv_lsr_peer, switch_rtcp_numbers_t::last_rpt_cycle, switch_rtcp_numbers_t::last_rpt_ext_seq, switch_rtcp_report_block::lost, switch_rtcp_report_block::lsr, switch_rtcp_numbers_t::peer_ssrc, switch_rtcp_numbers_t::period_pkt_count, switch_rtp::pool, switch_rtp::remote_ssrc, switch_rtp_stats_t::rtcp, switch_rtcp_numbers_t::rtcp_rtp_count, switch_rtcp_report_block::ssrc, switch_rtp::stats, SWITCH_CHANNEL_SESSION_LOG, switch_core_memory_pool_get_data(), SWITCH_LOG_CRIT, switch_log_printf(), and SWITCH_RTP_FLAG_VIDEO.

Referenced by check_rtcp_and_ice().

1841 {
1842 #ifdef DEBUG_RTCP
1843  switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
1844 #endif
1845  switch_rtcp_numbers_t * stats=&rtp_session->stats.rtcp;
1846  uint32_t expected_pkt, dlsr = 0;
1847  int32_t pkt_lost;
1848 
1849  /* Packet loss */
1850  if (stats->rtcp_rtp_count == 0) {
1851  expected_pkt = stats->high_ext_seq_recv - stats->base_seq + 1;
1852  } else {
1853  expected_pkt = stats->high_ext_seq_recv - stats->last_rpt_ext_seq + extra_expected;
1854  }
1855 
1856  pkt_lost = expected_pkt - stats->period_pkt_count;
1857  if (pkt_lost < 0) pkt_lost = 0;
1858 
1859  stats->cum_lost=stats->cum_lost+pkt_lost;
1860  if (expected_pkt > 0 && pkt_lost > 0) {
1861  rtcp_report_block->fraction = (pkt_lost == expected_pkt ? 255 : (uint8_t) (pkt_lost * 256 / expected_pkt)); /* if X packets were expected and X was lost, we want 0xff to be reported, not 0 */
1862  } else {
1863  rtcp_report_block->fraction = 0;
1864  }
1865 #if SWITCH_BYTE_ORDER == __BIG_ENDIAN
1866  rtcp_report_block->lost = stats->cum_lost;
1867 #else
1868  /* Reversing byte order for 24bits */
1869  rtcp_report_block->lost = htonl(stats->cum_lost) >> 8;
1870 #endif
1871 
1872 #ifdef DEBUG_RTCP
1873  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO])
1874  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, "rtcp_generate_sr: stats_ssrc[%u]\nreceived[%d]\nexpected[%d]\ncum[%d]\nlost[%d|%d/256]pkt\nlast_seq[%d]\ncyc[%d]\nlast_rpt_seq[%d]\ncyc[%d]\nssrc[%d]\n",
1875  rtp_session->remote_ssrc, stats->period_pkt_count, expected_pkt,
1876  stats->cum_lost, pkt_lost, rtcp_report_block->fraction, stats->high_ext_seq_recv&0x0000ffff,
1877  stats->cycle, stats->last_rpt_ext_seq&0x0000ffff, stats->last_rpt_cycle, rtp_session->stats.rtcp.peer_ssrc
1878  );
1879 #endif
1880  rtcp_report_block->highest_sequence_number_received = htonl(stats->high_ext_seq_recv);
1881 
1882  /* Jitter */
1883  rtcp_report_block->jitter = htonl((uint32_t)stats->inter_jitter);
1884 
1885  /* Delay since Last Sender Report (DLSR) : 32bits, 1/65536 seconds */
1886  if (stats->last_recv_lsr_local) {
1887  uint32_t lsr_now = calc_local_lsr_now();
1888  /* check lsr_now: what we just read from clock may be in the past (race cond), don't send huge dlsr due to uint wrap around */
1889  if (lsr_now > stats->last_recv_lsr_local) {
1890  dlsr = lsr_now - stats->last_recv_lsr_local;
1891  }
1892  }
1893  rtcp_report_block->lsr = stats->last_recv_lsr_peer;
1894  rtcp_report_block->dlsr = htonl(dlsr);
1895  if (rtp_session->stats.rtcp.peer_ssrc) {
1896  rtcp_report_block->ssrc = htonl(rtp_session->stats.rtcp.peer_ssrc);
1897  } else {
1898  /* if remote is not sending rtcp reports, take ssrc as assigned from rtp */
1899  rtcp_report_block->ssrc = htonl(rtp_session->remote_ssrc);
1900  }
1901 
1902  stats->rtcp_rtp_count++;
1903 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
void * switch_core_memory_pool_get_data(switch_memory_pool_t *pool, const char *key)
uint32_t highest_sequence_number_received
Definition: switch_rtp.c:500
switch_rtcp_numbers_t rtcp
Definition: switch_types.h:773
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:396
switch_rtp_stats_t stats
Definition: switch_rtp.c:454
switch_memory_pool_t * pool
Definition: switch_rtp.c:397
uint32_t last_recv_lsr_local
Definition: switch_types.h:750
uint32_t remote_ssrc
Definition: switch_rtp.c:377
static uint32_t calc_local_lsr_now(void)
Definition: switch_rtp.c:1823
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.

◆ rtcp_generate_sender_info()

static void rtcp_generate_sender_info ( switch_rtp_t rtp_session,
struct switch_rtcp_sender_info sr 
)
static

Definition at line 1799 of file switch_rtp.c.

References switch_rtp::last_write_ts, switch_rtcp_sender_info::ntp_lsw, switch_rtcp_sender_info::ntp_msw, NTP_TIME_OFFSET, switch_rtcp_sender_info::oc, switch_rtp_stats_t::outbound, switch_rtp_numbers_t::packet_count, switch_rtcp_sender_info::pc, switch_rtp::pool, switch_rtp_numbers_t::raw_bytes, switch_rtp_stats_t::rtcp, switch_rtcp_numbers_t::ssrc, switch_rtp::stats, SWITCH_CHANNEL_SESSION_LOG, switch_core_memory_pool_get_data(), SWITCH_LOG_DEBUG10, switch_log_printf(), switch_micro_time_now(), switch_time_exp_gmt(), switch_time_exp_t::tm_hour, switch_time_exp_t::tm_mday, switch_time_exp_t::tm_min, switch_time_exp_t::tm_mon, switch_time_exp_t::tm_sec, switch_time_exp_t::tm_usec, switch_time_exp_t::tm_year, and switch_rtcp_sender_info::ts.

Referenced by check_rtcp_and_ice().

1799  {
1800  switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
1801  switch_time_t now;
1802  uint32_t sec, ntp_sec, ntp_usec;
1803  switch_time_exp_t now_hr;
1804  now = switch_micro_time_now();
1805  sec = (uint32_t)(now/1000000); /* convert to seconds */
1806  ntp_sec = sec+NTP_TIME_OFFSET; /* convert to NTP seconds */
1807  sr->ntp_msw = htonl(ntp_sec); /* store result in "most significant word" */
1808  ntp_usec = (uint32_t)(now - (sec*1000000)); /* remove seconds to keep only the microseconds */
1809  sr->ntp_lsw = htonl((u_long)(ntp_usec*(double)(((uint64_t)1)<<32)*1.0e-6)); /* convert microseconds to fraction of 32bits and store result in "least significatn word" */
1810 
1811  sr->ts = htonl(rtp_session->last_write_ts);
1812  sr->pc = htonl(rtp_session->stats.outbound.packet_count);
1813  sr->oc = htonl(rtp_session->stats.outbound.raw_bytes - rtp_session->stats.outbound.packet_count * sizeof(srtp_hdr_t));
1814 
1815  switch_time_exp_gmt(&now_hr,now);
1816  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG10,"Sending an RTCP packet[%04d-%02d-%02d %02d:%02d:%02d.%d] lsr[%u] msw[%u] lsw[%u] stats_ssrc[%u] packet_count[%u] OC[%u]\n",
1817  1900 + now_hr.tm_year, now_hr.tm_mday, now_hr.tm_mon, now_hr.tm_hour, now_hr.tm_min, now_hr.tm_sec, now_hr.tm_usec,
1818  (ntohl(sr->ntp_lsw)&0xffff0000)>>16 | (ntohl(sr->ntp_msw)&0x0000ffff)<<16,
1819  ntohl(sr->ntp_msw),ntohl(sr->ntp_lsw), rtp_session->stats.rtcp.ssrc, ntohl(sr->pc), ntohl(sr->oc)
1820  );
1821 }
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
Definition: switch_time.c:311
#define SWITCH_CHANNEL_SESSION_LOG(x)
switch_status_t switch_time_exp_gmt(switch_time_exp_t *result, switch_time_t input)
Definition: switch_apr.c:356
#define NTP_TIME_OFFSET
Definition: switch_rtp.c:80
void * switch_core_memory_pool_get_data(switch_memory_pool_t *pool, const char *key)
int64_t switch_time_t
Definition: switch_apr.h:188
switch_rtcp_numbers_t rtcp
Definition: switch_types.h:773
switch_size_t raw_bytes
Definition: switch_types.h:693
switch_rtp_numbers_t outbound
Definition: switch_types.h:772
switch_size_t packet_count
Definition: switch_types.h:695
switch_rtp_stats_t stats
Definition: switch_rtp.c:454
switch_memory_pool_t * pool
Definition: switch_rtp.c:397
uint32_t last_write_ts
Definition: switch_rtp.c:386
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.

◆ rtcp_stats()

static int rtcp_stats ( switch_rtp_t rtp_session)
static

Definition at line 1956 of file switch_rtp.c.

References switch_rtcp_numbers_t::bad_seq, switch_rtcp_numbers_t::cycle, switch_rtp::flags, switch_rtcp_numbers_t::high_ext_seq_recv, switch_rtcp_numbers_t::init, switch_rtcp_numbers_t::inter_jitter, switch_rtcp_numbers_t::last_pkt_tsdiff, switch_rtp::last_rtp_hdr, switch_rtcp_numbers_t::period_pkt_count, switch_rtcp_numbers_t::pkt_count, switch_rtp::pool, switch_rtp_stats_t::rtcp, switch_rtp::rtcp_interval, switch_rtp::rtcp_sock_output, rtcp_stats_init(), switch_timer::samplecount, switch_rtcp_numbers_t::ssrc, switch_rtp::stats, SWITCH_CHANNEL_SESSION_LOG, switch_core_memory_pool_get_data(), SWITCH_LOG_DEBUG, SWITCH_LOG_DEBUG10, switch_log_printf(), SWITCH_RTP_FLAG_ENABLE_RTCP, SWITCH_RTP_FLAG_RTCP_PASSTHRU, switch_rtp::timer, and switch_rtp::write_timer.

Referenced by read_rtp_packet().

1957 {
1958  switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
1959  srtp_hdr_t * hdr = &rtp_session->last_rtp_hdr;
1960  switch_rtcp_numbers_t * stats = &rtp_session->stats.rtcp;
1961  uint32_t packet_spacing_diff = 0, pkt_tsdiff, pkt_extended_seq;
1962  uint16_t pkt_seq, seq_diff, max_seq;
1963  const int MAX_DROPOUT = 3000;
1964  const int MAX_MISORDER = 100;
1965  const int RTP_SEQ_MOD = (1<<16);
1966 
1967  if(!rtp_session->rtcp_sock_output || !rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] || rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU] || !rtp_session->rtcp_interval)
1968  return 0; /* do not process RTCP in current state */
1969 
1970  pkt_seq = (uint16_t) ntohs((uint16_t) rtp_session->last_rtp_hdr.seq);
1971 
1972  /* Detect sequence number cycle change */
1973  max_seq = stats->high_ext_seq_recv&0x0000ffff;
1974  seq_diff = pkt_seq - max_seq;
1975 
1976  if (seq_diff < MAX_DROPOUT) { /* in order, with permissible gap */
1977  if (pkt_seq < max_seq) {
1978  stats->cycle++;
1979  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "rtcp_stats:[cycle change] pkt_seq[%d] cycle[%d] max_seq[%d] stats_ssrc[%u] local_ts[%u]\n",
1980  pkt_seq, stats->cycle, max_seq, stats->ssrc, rtp_session->timer.samplecount);
1981  }
1982  pkt_extended_seq = stats->cycle << 16 | pkt_seq; /* getting the extended packet extended sequence ID */
1983  if (pkt_extended_seq > stats->high_ext_seq_recv) {
1984  stats->high_ext_seq_recv = pkt_extended_seq;
1985  }
1986  }
1987  else if (seq_diff <= (RTP_SEQ_MOD - MAX_MISORDER)) { /* the sequence number made a very large jump */
1988  if (pkt_seq == stats->bad_seq) {
1989  rtcp_stats_init(rtp_session);
1990  } else {
1991  stats->bad_seq = (pkt_seq + 1) & (RTP_SEQ_MOD-1);
1992  }
1993  return 0; /* no stats, packet is out of sync and will be accounted as lost */
1994  } else {
1995  /* duplicate or reordered packet */
1996  }
1997 
1998  /* Verify that we are on the same stream source (we do not support multiple sources) */
1999  if (ntohl(hdr->ssrc) != stats->ssrc || !stats->init) {
2000  rtcp_stats_init(rtp_session);
2001  }
2002 
2003  stats->period_pkt_count++;
2004  stats->pkt_count++;
2005 #ifdef DEBUG_RTCP
2006  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG10, "rtcp_stats: period_pkt_count[%d]last_seq[%d]cycle[%d]stats_ssrc[%u]local_ts[%u]\n",
2007  stats->period_pkt_count, pkt_seq, stats->cycle, stats->ssrc, rtp_session->write_timer.samplecount);
2008 #endif
2009  /* Interarrival jitter calculation */
2010  pkt_tsdiff = abs((int32_t)(rtp_session->timer.samplecount - ntohl(hdr->ts))); /* relative transit times for this packet */
2011  if (stats->pkt_count < 2) { /* Can not compute Jitter with only one packet */
2012  stats->last_pkt_tsdiff = pkt_tsdiff;
2013  } else {
2014  /* Jitter : difference of relative transit times for the two packets */
2015  packet_spacing_diff = abs((int32_t)(pkt_tsdiff - stats->last_pkt_tsdiff));
2016  stats->last_pkt_tsdiff = pkt_tsdiff;
2017  /* Interarrival jitter estimation, "J(i) = J(i-1) + ( |D(i-1,i)| - J(i-1) )/16" */
2018  stats->inter_jitter = (stats->inter_jitter + (((double)packet_spacing_diff - stats->inter_jitter) /16.));
2019  }
2020 
2021 #ifdef DEBUG_RTCP
2022  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG10, "rtcp_stats: pkt_ts[%d]local_ts[%d]diff[%d]pkt_spacing[%d]inter_jitter[%f]seq[%d]stats_ssrc[%d]",
2023  ntohl(hdr->ts), rtp_session->timer.samplecount, pkt_tsdiff, packet_spacing_diff, stats->inter_jitter, ntohs(hdr->seq), stats->ssrc);
2024 #endif
2025  return 1;
2026 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
void * switch_core_memory_pool_get_data(switch_memory_pool_t *pool, const char *key)
static void rtcp_stats_init(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:1905
switch_rtcp_numbers_t rtcp
Definition: switch_types.h:773
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:396
switch_rtp_stats_t stats
Definition: switch_rtp.c:454
switch_memory_pool_t * pool
Definition: switch_rtp.c:397
switch_socket_t * rtcp_sock_output
Definition: switch_rtp.c:326
int rtcp_interval
Definition: switch_rtp.c:462
switch_timer_t timer
Definition: switch_rtp.c:434
switch_timer_t write_timer
Definition: switch_rtp.c:435
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
rtp_hdr_t last_rtp_hdr
Definition: switch_rtp.c:373

◆ rtcp_stats_init()

static void rtcp_stats_init ( switch_rtp_t rtp_session)
static

Definition at line 1905 of file switch_rtp.c.

References switch_rtcp_numbers_t::bad_seq, switch_rtcp_numbers_t::base_seq, switch_rtcp_numbers_t::cum_lost, switch_rtcp_numbers_t::cycle, switch_rtp::detectors, EST_LOSS, EST_RTT, switch_rtp::estimators, switch_rtp::flags, switch_rtcp_numbers_t::high_ext_seq_recv, switch_rtcp_numbers_t::init, switch_rtcp_numbers_t::inter_jitter, KALMAN_SYSTEM_MODELS, switch_rtcp_numbers_t::last_pkt_tsdiff, switch_rtcp_numbers_t::last_rpt_cycle, switch_rtcp_numbers_t::last_rpt_ext_seq, switch_rtcp_numbers_t::last_rpt_ts, switch_rtp::last_rtp_hdr, switch_rtcp_numbers_t::period_pkt_count, switch_rtcp_numbers_t::pkt_count, switch_rtp::pool, switch_rtp_stats_t::rtcp, switch_rtcp_numbers_t::rtcp_rtp_count, switch_rtp::rtcp_sock_output, rtp_type, switch_timer::samplecount, switch_rtcp_numbers_t::sent_pkt_count, switch_rtp::session, switch_rtcp_numbers_t::ssrc, switch_rtp::stats, SWITCH_CHANNEL_SESSION_LOG, SWITCH_CODEC_FLAG_HAS_ADJ_BITRATE, switch_core_alloc, switch_core_media_codec_get_cap(), switch_core_memory_pool_get_data(), switch_kalman_cusum_init(), switch_kalman_init(), SWITCH_LOG_DEBUG, switch_log_printf(), SWITCH_LOG_WARNING, SWITCH_MEDIA_TYPE_AUDIO, SWITCH_RTP_FLAG_ADJ_BITRATE_CAP, SWITCH_RTP_FLAG_ENABLE_RTCP, SWITCH_RTP_FLAG_ESTIMATORS, SWITCH_RTP_FLAG_RTCP_PASSTHRU, and switch_rtp::write_timer.

Referenced by rtcp_stats(), and switch_rtp_reset().

1906 {
1907  switch_rtcp_numbers_t * stats = &rtp_session->stats.rtcp;
1908  srtp_hdr_t * hdr = &rtp_session->last_rtp_hdr;
1909  switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
1910  stats->ssrc = ntohl(hdr->ssrc);
1911  stats->last_rpt_ts = rtp_session->write_timer.samplecount;
1912  stats->init = 1;
1913  stats->last_rpt_ext_seq = 0;
1914  stats->last_rpt_cycle = 0;
1915  stats->last_pkt_tsdiff = 0;
1916  stats->inter_jitter = 0;
1917  stats->cycle = 0;
1918  stats->high_ext_seq_recv = ntohs((uint16_t)hdr->seq);
1919  stats->base_seq = ntohs((uint16_t)hdr->seq);
1920  stats->bad_seq = (1<<16) + 1; /* Make sure we wont missmatch 2 consecutive packets, so seq == bad_seq is false */
1921  stats->cum_lost = 0;
1922  stats->period_pkt_count = 0;
1923  stats->sent_pkt_count = 0;
1924  stats->pkt_count = 0;
1925  stats->rtcp_rtp_count = 0;
1926 
1927  if (!rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
1928  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "rtcp_stats_init: %s rtcp disabled\n", rtp_type(rtp_session));
1929  } else if (!rtp_session->rtcp_sock_output) {
1930  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "rtcp_stats_init: %s no rtcp socket\n", rtp_type(rtp_session));
1931  } else if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU]) {
1932  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "rtcp_stats_init: %s rtcp passthru\n", rtp_type(rtp_session));
1933  } else {
1934  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "rtcp_stats_init: %s ssrc[%u] base_seq[%u]\n", rtp_type(rtp_session), stats->ssrc, stats->base_seq);
1935  }
1936 
1937  if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] && (switch_core_media_codec_get_cap(rtp_session->session,
1941 
1942  rtp_session->flags[SWITCH_RTP_FLAG_ADJ_BITRATE_CAP] = 1;
1943  rtp_session->flags[SWITCH_RTP_FLAG_ESTIMATORS] = 1;
1944 
1945  rtp_session->estimators[EST_LOSS] = switch_core_alloc(rtp_session->pool, sizeof(*estimators[0]));
1946  switch_kalman_init(rtp_session->estimators[EST_LOSS],0.1,0.1);
1947  rtp_session->estimators[EST_RTT] = switch_core_alloc(rtp_session->pool, sizeof(*estimators[0]));
1948  switch_kalman_init(rtp_session->estimators[EST_RTT],0.03,1);
1949  rtp_session->detectors[EST_RTT] = switch_core_alloc(rtp_session->pool, sizeof(*detectors[0]));
1950  switch_kalman_cusum_init(rtp_session->detectors[EST_RTT],0.005,0.5);
1951  rtp_session->detectors[EST_LOSS] = switch_core_alloc(rtp_session->pool, sizeof(*detectors[0]));
1952  switch_kalman_cusum_init(rtp_session->detectors[EST_LOSS], 0.5, 1);
1953  }
1954 }
#define KALMAN_SYSTEM_MODELS
Definition: switch_rtp.c:173
#define SWITCH_CHANNEL_SESSION_LOG(x)
void * switch_core_memory_pool_get_data(switch_memory_pool_t *pool, const char *key)
switch_bool_t switch_core_media_codec_get_cap(switch_core_session_t *session, switch_media_type_t mtype, switch_codec_flag_t flag)
#define rtp_type(rtp_session)
Definition: switch_rtp.c:544
switch_rtcp_numbers_t rtcp
Definition: switch_types.h:773
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:396
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:684
#define EST_RTT
Definition: switch_rtp.c:176
#define EST_LOSS
Definition: switch_rtp.c:174
kalman_estimator_t * estimators[KALMAN_SYSTEM_MODELS]
Definition: switch_rtp.c:479
switch_rtp_stats_t stats
Definition: switch_rtp.c:454
switch_memory_pool_t * pool
Definition: switch_rtp.c:397
switch_socket_t * rtcp_sock_output
Definition: switch_rtp.c:326
switch_core_session_t * session
Definition: switch_rtp.c:476
switch_timer_t write_timer
Definition: switch_rtp.c:435
void switch_kalman_init(kalman_estimator_t *est, float Q, float R)
switch_bool_t switch_kalman_cusum_init(cusum_kalman_detector_t *detect_change, float epsilon, float h)
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
rtp_hdr_t last_rtp_hdr
Definition: switch_rtp.c:373
cusum_kalman_detector_t * detectors[KALMAN_SYSTEM_MODELS]
Definition: switch_rtp.c:480

◆ rtp_common_read()

static int rtp_common_read ( switch_rtp_t rtp_session,
switch_payload_t payload_type,
payload_map_t **  pmapP,
switch_frame_flag_t flags,
switch_io_flag_t  io_flags 
)
static

NOOP

Definition at line 7304 of file switch_rtp.c.

References switch_rtp_ice_t::addr, switch_rtp::auto_adj_used, switch_rtp::autoadj_tally, switch_rtp::autoadj_threshold, switch_rtp::autoadj_window, switch_rtp::bad_stream, CF_AUDIO_VIDEO_BUNDLE, check_recv_payload(), check_rtcp_and_ice(), check_timeout(), switch_rtp::cng_count, switch_rtp::cng_pt, do_2833(), do_flush(), do_mos(), switch_rtp::dtmf_data, switch_rtp_rfc2833_data::dtmf_inqueue, switch_rtp_rfc2833_data::dtmf_queue, switch_rtp_numbers_t::error_log, switch_rtp::flags, error_period::flaws, switch_rtp_numbers_t::flaws, switch_rtp::from_addr, handle_rfc2833(), switch_rtp::has_rtp, rtp_msg_t::header, rtcp_msg_t::header, switch_rtp::hot_hits, switch_rtp::ice, switch_rtp::ice_mutex, switch_rtp_ice_t::ice_user, if(), switch_rtp_rfc2833_data::in_digit_sanity, switch_rtp_rfc2833_data::in_digit_ts, switch_rtp_stats_t::inbound, switch_timer::interval, switch_rtp::invalid_handler, INVALID_PT, switch_rtp::jb, jb_valid(), switch_rtp::jitter_lead, JITTER_LEAD_FRAMES, switch_rtp::last_cng_ts, switch_rtp::last_media, switch_rtp::last_read_ts, switch_rtp::last_rtp_hdr, switch_rtp::max_missed_packets, switch_rtp::media_timeout, memset(), switch_rtp::missed_count, switch_rtp::ms_per_packet, switch_rtp::one_second, switch_rtp_rfc2833_data::out_digit_dur, switch_rtp::pause_jb, switch_rtp_numbers_t::period_packet_count, process_rtcp_packet(), switch_rtp_stats_t::read_count, READ_DEC, READ_INC, switch_rtp::read_pollfd, read_rtcp_packet(), read_rtp_packet(), switch_rtp::recv_msg, switch_rtp::recv_te, switch_rtp::remote_addr, switch_rtp::remote_port, reset_jitter_seq(), RESULT_CONTINUE, RESULT_GOTO_END, RESULT_GOTO_RECVFROM, RESULT_GOTO_TIMERCHECK, return_cng_frame, switch_rtp::rtcp_from_addr, switch_rtp::rtcp_read_pollfd, switch_rtp::rtcp_recv_msg, switch_rtp::rtcp_recv_msg_p, switch_rtp::rtcp_remote_addr, switch_rtp::rtcp_send_msg, switch_rtp::rtcp_sock_output, RTP_BODY, RTP_BUG_ACCEPT_ANY_PACKETS, RTP_BUG_ALWAYS_AUTO_ADJUST, RTP_BUG_IGNORE_MARK_BIT, switch_rtp::rtp_bugs, rtp_flush_read_buffer(), switch_rtp::rtp_from_addr, rtp_header_len, rtp_session_name, rtp_type, switch_rtp::samples_per_interval, switch_rtp::send_ctx, switch_rtp::sending_dtmf, switch_rtp::session, SFF_CNG, SFF_NOT_AUDIO, SFF_PROXY_PACKET, SFF_RTCP, SFF_UDPTL_PACKET, switch_rtp_numbers_t::skip_packet_count, switch_rtp::skip_timer, switch_rtp::sock_input, switch_rtp::srtp_idx_rtcp, switch_rtp::stats, switch_channel_get_partner_uuid(), switch_channel_get_private(), switch_channel_get_variable, SWITCH_CHANNEL_SESSION_LOG, switch_channel_set_variable, switch_channel_test_flag(), switch_cmp_addr(), switch_cond_next(), switch_core_session_get_channel(), switch_core_session_locate, switch_core_session_rwunlock(), switch_core_timer_next(), switch_core_timer_sync(), SWITCH_CRYPTO_MKI_INDEX, SWITCH_FALSE, switch_get_addr(), SWITCH_IO_FLAG_NOBLOCK, SWITCH_IO_FLAG_SINGLE_READ, switch_jb_poll(), SWITCH_LOG_DEBUG, SWITCH_LOG_DEBUG1, SWITCH_LOG_DEBUG10, SWITCH_LOG_DEBUG3, SWITCH_LOG_ERROR, SWITCH_LOG_INFO, switch_log_printf(), SWITCH_LOG_WARNING, switch_mutex_lock(), switch_mutex_unlock(), switch_poll(), switch_queue_size(), switch_rtp_clear_flag(), SWITCH_RTP_CNG_PAYLOAD, SWITCH_RTP_FLAG_AUTOADJ, SWITCH_RTP_FLAG_AUTOFLUSH, SWITCH_RTP_FLAG_BREAK, SWITCH_RTP_FLAG_DATAWAIT, SWITCH_RTP_FLAG_DTMF_ON, SWITCH_RTP_FLAG_ENABLE_RTCP, SWITCH_RTP_FLAG_FLUSH, SWITCH_RTP_FLAG_GOOGLEHACK, SWITCH_RTP_FLAG_MUTE, SWITCH_RTP_FLAG_NOBLOCK, SWITCH_RTP_FLAG_PAUSE, SWITCH_RTP_FLAG_PROXY_MEDIA, SWITCH_RTP_FLAG_RTCP_AUTOADJ, SWITCH_RTP_FLAG_RTCP_MUX, SWITCH_RTP_FLAG_RTCP_PASSTHRU, SWITCH_RTP_FLAG_SECURE_SEND, SWITCH_RTP_FLAG_SECURE_SEND_MKI, SWITCH_RTP_FLAG_STICKY_FLUSH, SWITCH_RTP_FLAG_TEXT, SWITCH_RTP_FLAG_UDPTL, SWITCH_RTP_FLAG_USE_TIMER, SWITCH_RTP_FLAG_VIDEO, SWITCH_RTP_FLUSH_ONCE, switch_rtp_ready(), switch_rtp_reset_media_timer(), switch_rtp_set_flag(), switch_rtp_set_remote_address(), switch_rtp_test_flag(), switch_snprintf(), switch_sockaddr_get_port(), switch_socket_sendto(), SWITCH_STATUS_BREAK, SWITCH_STATUS_FALSE, SWITCH_STATUS_GENERR, SWITCH_STATUS_IS_BREAK, SWITCH_STATUS_SUCCESS, SWITCH_STATUS_TIMEOUT, switch_strerror_r(), SWITCH_TRUE, switch_yield, switch_rtp::sync_packets, switch_rtp::timer, timer_check(), using_ice(), switch_rtp::vb, and switch_rtcp_hdr_s::version.

Referenced by switch_rtp_read(), switch_rtp_zerocopy_read(), and switch_rtp_zerocopy_read_frame().

7306 {
7307 
7308  switch_channel_t *channel = NULL;
7309  switch_size_t bytes = 0;
7310  switch_size_t rtcp_bytes = 0;
7312  switch_status_t rtcp_status = SWITCH_STATUS_SUCCESS, rtcp_poll_status = SWITCH_STATUS_SUCCESS;
7313  int check = 0;
7314  int ret = -1;
7315  int sleep_mss = 1000;
7316  int poll_sec = 5;
7317  int poll_loop = 0;
7318  int fdr = 0;
7319  int rtcp_fdr = 0;
7320  int hot_socket = 0;
7321  int read_loops = 0;
7322  int slept = 0;
7323  switch_bool_t got_jb = SWITCH_FALSE;
7324 
7325  if (!switch_rtp_ready(rtp_session)) {
7326  return -1;
7327  }
7328 
7329  if (rtp_session->session) {
7330  channel = switch_core_session_get_channel(rtp_session->session);
7331  }
7332 
7333  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
7334  sleep_mss = rtp_session->timer.interval * 1000;
7335  }
7336 
7337  READ_INC(rtp_session);
7338 
7339 
7340 
7341  while (switch_rtp_ready(rtp_session)) {
7342  int do_cng = 0;
7343  int read_pretriggered = 0;
7344  int has_rtcp = 0;
7345  int got_rtp_poll = 0;
7346 
7347  bytes = 0;
7348 
7349  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] &&
7350  !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] &&
7351  !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] &&
7352  !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] &&
7353  rtp_session->read_pollfd) {
7354 
7355  if (rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) {
7356  while (switch_poll(rtp_session->read_pollfd, 1, &fdr, 0) == SWITCH_STATUS_SUCCESS) {
7357  status = read_rtp_packet(rtp_session, &bytes, flags, pmapP, SWITCH_STATUS_SUCCESS, SWITCH_FALSE);
7358 
7359  if (status == SWITCH_STATUS_GENERR) {
7360  ret = -1;
7361  goto end;
7362  }
7363 
7364  if ((*flags & SFF_RTCP)) {
7365  *flags &= ~SFF_RTCP;
7366  has_rtcp = 1;
7367  read_pretriggered = 0;
7368  goto rtcp;
7369  }
7370 
7371  if (status == SWITCH_STATUS_BREAK) {
7372  read_pretriggered = 1;
7373  break;
7374  }
7375  }
7376 
7377  } else if ((rtp_session->flags[SWITCH_RTP_FLAG_AUTOFLUSH] || rtp_session->flags[SWITCH_RTP_FLAG_STICKY_FLUSH])) {
7378 
7379  if (switch_poll(rtp_session->read_pollfd, 1, &fdr, 0) == SWITCH_STATUS_SUCCESS) {
7380  status = read_rtp_packet(rtp_session, &bytes, flags, pmapP, SWITCH_STATUS_SUCCESS, SWITCH_FALSE);
7381  if (status == SWITCH_STATUS_GENERR) {
7382  ret = -1;
7383  goto end;
7384  }
7385  if ((*flags & SFF_RTCP)) {
7386  *flags &= ~SFF_RTCP;
7387  has_rtcp = 1;
7388  read_pretriggered = 0;
7389  goto rtcp;
7390  }
7391 
7392  /* switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Initial (%i) %d\n", status, bytes); */
7393  if (status != SWITCH_STATUS_FALSE) {
7394  read_pretriggered = 1;
7395  }
7396 
7397  if (bytes) {
7398  if (switch_poll(rtp_session->read_pollfd, 1, &fdr, 0) == SWITCH_STATUS_SUCCESS) {
7399  rtp_session->hot_hits++;//+= rtp_session->samples_per_interval;
7400 
7401  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG10, "%s Hot Hit %d\n",
7402  rtp_session_name(rtp_session),
7403  rtp_session->hot_hits);
7404  } else {
7405  rtp_session->hot_hits = 0;
7406  }
7407  }
7408 
7409  if (rtp_session->hot_hits > 1 && !rtp_session->sync_packets) {// >= (rtp_session->samples_per_second * 30)) {
7410  hot_socket = 1;
7411  }
7412  } else {
7413  rtp_session->hot_hits = 0;
7414  }
7415  }
7416 
7417  if (rtp_session->flags[SWITCH_RTP_FLAG_TEXT]) {
7418  ///NOOP
7419  } else if (hot_socket && (rtp_session->hot_hits % 10) != 0) {
7420  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG10, "%s timer while HOT\n", rtp_session_name(rtp_session));
7421  switch_core_timer_next(&rtp_session->timer);
7422  } else if (hot_socket) {
7423  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG10, "%s skip timer once\n", rtp_session_name(rtp_session));
7424  rtp_session->sync_packets++;
7425  switch_core_timer_sync(&rtp_session->timer);
7426  reset_jitter_seq(rtp_session);
7427  } else {
7428 
7429  if (rtp_session->sync_packets) {
7430 
7432  "%s Auto-Flush catching up %d packets (%d)ms.\n",
7433  rtp_session_name(rtp_session),
7434  rtp_session->sync_packets, (rtp_session->ms_per_packet * rtp_session->sync_packets) / 1000);
7435  if (!rtp_session->flags[SWITCH_RTP_FLAG_PAUSE]) {
7436  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "%s syncing %d %s packet(s)\n",
7437  rtp_session_name(rtp_session),
7438  rtp_session->sync_packets, rtp_type(rtp_session));
7439 
7440  rtp_session->bad_stream++;
7441  rtp_session->stats.inbound.flaws += rtp_session->sync_packets;
7442 
7443  if (rtp_session->stats.inbound.error_log) {
7444  rtp_session->stats.inbound.error_log->flaws += rtp_session->sync_packets;
7445  }
7446  }
7447 
7448  switch_core_timer_sync(&rtp_session->timer);
7449  reset_jitter_seq(rtp_session);
7450  rtp_session->hot_hits = 0;
7451  } else {
7452  if (slept) {
7453  switch_cond_next();
7454  } else {
7455  if (rtp_session->skip_timer) {
7456  rtp_session->skip_timer = 0;
7457  switch_cond_next();
7458  } else {
7459  switch_core_timer_next(&rtp_session->timer);
7460  }
7461  slept++;
7462  }
7463 
7464  }
7465 
7466  rtp_session->sync_packets = 0;
7467  }
7468  }
7469 
7470  rtp_session->stats.read_count++;
7471 
7472  recvfrom:
7473 
7474  if (!read_pretriggered) {
7475  bytes = 0;
7476  }
7477  read_loops++;
7478  //poll_loop = 0;
7479 
7480  if (!switch_rtp_ready(rtp_session)) {
7481  break;
7482  }
7483 
7484  if (!rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && rtp_session->read_pollfd) {
7485  int pt = poll_sec * 1000000;
7486 
7487  do_2833(rtp_session);
7488 
7489  if (rtp_session->dtmf_data.out_digit_dur > 0 || rtp_session->dtmf_data.in_digit_sanity || rtp_session->sending_dtmf ||
7491  pt = 20000;
7492  }
7493 
7494  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) {
7495  pt = 100000;
7496  }
7497 
7498  if (rtp_session->vb && !rtp_session->pause_jb) {
7499  if (switch_jb_poll(rtp_session->vb)) {
7500  pt = 1000;
7501  }
7502  }
7503 
7504  if ((io_flags & SWITCH_IO_FLAG_NOBLOCK)) {
7505  pt = 0;
7506  }
7507 
7508  poll_status = switch_poll(rtp_session->read_pollfd, 1, &fdr, pt);
7509 
7510  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && poll_status != SWITCH_STATUS_SUCCESS && rtp_session->media_timeout && rtp_session->last_media) {
7511  check_timeout(rtp_session);
7512  }
7513 
7514  if (!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->dtmf_data.out_digit_dur > 0) {
7515  return_cng_frame();
7516  }
7517 
7518  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->flags[SWITCH_RTP_FLAG_BREAK]) {
7520  bytes = 0;
7521  reset_jitter_seq(rtp_session);
7522  return_cng_frame();
7523  }
7524 
7525  }
7526 
7527  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
7528  got_jb = (rtp_session->vb && !rtp_session->pause_jb && switch_jb_poll(rtp_session->vb));
7529  } else {
7530  got_jb = SWITCH_TRUE;
7531  }
7532 
7533  if (poll_status == SWITCH_STATUS_SUCCESS || got_jb) {
7534 
7535  got_rtp_poll = 1;
7536 
7537  if (read_pretriggered) {
7538  read_pretriggered = 0;
7539  } else {
7540 
7541 
7542  status = read_rtp_packet(rtp_session, &bytes, flags, pmapP, poll_status, got_jb);
7543 
7544  if (status == SWITCH_STATUS_GENERR) {
7545  ret = -1;
7546  goto end;
7547  }
7548 
7549  if (rtp_session->max_missed_packets && read_loops == 1 && !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] &&
7550  !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
7551  if (bytes && status == SWITCH_STATUS_SUCCESS) {
7552  rtp_session->missed_count = 0;
7553  } else {
7554  if (rtp_session->media_timeout && rtp_session->last_media) {
7555  check_timeout(rtp_session);
7556  } else {
7557  if (++rtp_session->missed_count >= rtp_session->max_missed_packets) {
7558  ret = -2;
7559  goto end;
7560  }
7561  }
7562  }
7563  }
7564 
7565  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
7566  //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_CRIT, "Read bytes (%i) %ld\n", status, bytes);
7567 
7568  if (bytes == 0) {
7569  if (check_rtcp_and_ice(rtp_session) == -1) {
7570  ret = -1;
7571  goto end;
7572  }
7573  // This is dumb
7574  //switch_rtp_video_refresh(rtp_session);
7575  goto rtcp;
7576  }
7577  }
7578 
7579  if ((*flags & SFF_PROXY_PACKET)) {
7580  ret = (int) bytes;
7581  goto end;
7582  }
7583 
7584  if ((*flags & SFF_RTCP)) {
7585  *flags &= ~SFF_RTCP;
7586  has_rtcp = 1;
7587  goto rtcp;
7588  }
7589 
7590 
7591  }
7592  poll_loop = 0;
7593  } else {
7594 
7595  if (!switch_rtp_ready(rtp_session)) {
7596  ret = -1;
7597  goto end;
7598  }
7599 
7600  if (!SWITCH_STATUS_IS_BREAK(poll_status) && poll_status != SWITCH_STATUS_TIMEOUT) {
7601  char tmp[128] = "";
7602  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Poll failed with error: %d [%s]\n",
7603  poll_status, switch_strerror_r(poll_status, tmp, sizeof(tmp)));
7604  ret = -1;
7605  goto end;
7606  }
7607 
7608  if (!rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] && !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
7609  rtp_session->missed_count += (poll_sec * 1000) / (rtp_session->ms_per_packet ? rtp_session->ms_per_packet / 1000 : 20);
7610  bytes = 0;
7611 
7612  if (rtp_session->media_timeout && rtp_session->last_media) {
7613  check_timeout(rtp_session);
7614  } else if (rtp_session->max_missed_packets) {
7615  if (rtp_session->missed_count >= rtp_session->max_missed_packets) {
7616  ret = -2;
7617  goto end;
7618  }
7619  }
7620  }
7621 
7622 
7623  if (check_rtcp_and_ice(rtp_session) == -1) {
7624  ret = -1;
7625  goto end;
7626  }
7627 
7628 
7629  if ((!(io_flags & SWITCH_IO_FLAG_NOBLOCK)) &&
7630  (rtp_session->dtmf_data.out_digit_dur == 0) && !rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
7631  return_cng_frame();
7632  }
7633  }
7634 
7635  rtcp:
7636 
7637  if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
7638  rtcp_poll_status = SWITCH_STATUS_FALSE;
7639 
7640  if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX] && has_rtcp) {
7641  if (rtp_session->rtcp_recv_msg_p->header.version == 2) { //rtcp muxed
7642  rtp_session->rtcp_from_addr = rtp_session->from_addr;
7643  rtcp_status = rtcp_poll_status = SWITCH_STATUS_SUCCESS;
7644  rtcp_bytes = bytes;
7645  }
7646 
7647  has_rtcp = 0;
7648 
7649  } else if (rtp_session->rtcp_read_pollfd) {
7650  rtcp_poll_status = switch_poll(rtp_session->rtcp_read_pollfd, 1, &rtcp_fdr, 0);
7651  }
7652 
7653  if (rtcp_poll_status == SWITCH_STATUS_SUCCESS) {
7654 
7655  if (!rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
7656  rtcp_status = read_rtcp_packet(rtp_session, &rtcp_bytes, flags);
7657  }
7658 
7659  if (rtcp_status == SWITCH_STATUS_SUCCESS) {
7660  switch_rtp_reset_media_timer(rtp_session);
7661 
7662  if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU]) {
7663  switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
7664  const char *uuid = switch_channel_get_partner_uuid(channel);
7665 
7666  if (uuid) {
7667  switch_core_session_t *other_session;
7668  switch_rtp_t *other_rtp_session = NULL;
7669 
7670  if ((other_session = switch_core_session_locate(uuid))) {
7671  switch_channel_t *other_channel = switch_core_session_get_channel(other_session);
7672  if ((other_rtp_session = switch_channel_get_private(other_channel, "__rtcp_audio_rtp_session")) &&
7673  other_rtp_session->rtcp_sock_output &&
7674  switch_rtp_test_flag(other_rtp_session, SWITCH_RTP_FLAG_ENABLE_RTCP)) {
7675  other_rtp_session->rtcp_send_msg = rtp_session->rtcp_recv_msg;
7676 
7677 #ifdef ENABLE_SRTP
7678  switch_mutex_lock(other_rtp_session->ice_mutex);
7679  if (switch_rtp_test_flag(other_rtp_session, SWITCH_RTP_FLAG_SECURE_SEND)) {
7680  int stat = 0;
7681  int sbytes = (int) rtcp_bytes;
7682 
7683  if (!other_rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_MKI]) {
7684  stat = srtp_protect_rtcp(other_rtp_session->send_ctx[rtp_session->srtp_idx_rtcp], &other_rtp_session->rtcp_send_msg.header, &sbytes);
7685  } else {
7686  stat = srtp_protect_rtcp_mki(other_rtp_session->send_ctx[other_rtp_session->srtp_idx_rtcp], &other_rtp_session->rtcp_send_msg.header, &sbytes, 1, SWITCH_CRYPTO_MKI_INDEX);
7687  }
7688 
7689  if (stat) {
7690  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error: SRTP RTCP protection failed with code %d\n", stat);
7691  }
7692  rtcp_bytes = sbytes;
7693  }
7694  switch_mutex_unlock(other_rtp_session->ice_mutex);
7695 #endif
7696 
7697  if (switch_socket_sendto(other_rtp_session->rtcp_sock_output, other_rtp_session->rtcp_remote_addr, 0,
7698  (const char*)&other_rtp_session->rtcp_send_msg, &rtcp_bytes ) != SWITCH_STATUS_SUCCESS) {
7699  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG,"RTCP packet not written\n");
7700  }
7701 
7702 
7703  }
7704  switch_core_session_rwunlock(other_session);
7705  }
7706  }
7707 
7708  }
7709 
7710  if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
7711  process_rtcp_packet(rtp_session, &rtcp_bytes);
7712  ret = 1;
7713 
7714  continue;
7715  }
7716  }
7717  }
7718  }
7719 
7720  if ((!(io_flags & SWITCH_IO_FLAG_NOBLOCK)) &&
7721  (rtp_session->dtmf_data.out_digit_dur == 0) && !got_rtp_poll) {
7722  return_cng_frame();
7723  }
7724 
7725  if (!bytes && (io_flags & SWITCH_IO_FLAG_NOBLOCK)) {
7726  rtp_session->missed_count = 0;
7727  ret = 0;
7728  goto end;
7729  }
7730 
7731  check = !bytes;
7732 
7733  if (rtp_session->flags[SWITCH_RTP_FLAG_FLUSH]) {
7734  bytes = do_flush(rtp_session, SWITCH_FALSE, bytes);
7736  }
7737 
7738  if ((!bytes && rtp_session->flags[SWITCH_RTP_FLAG_BREAK]) || (bytes && bytes == 4 && *((int *) &rtp_session->recv_msg) == UINT_MAX)) {
7740 
7741  if (!rtp_session->flags[SWITCH_RTP_FLAG_NOBLOCK] || !rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] ||
7742  rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] ||
7743  (bytes && bytes < 5) || (!bytes && poll_loop)) {
7744  bytes = 0;
7745  reset_jitter_seq(rtp_session);
7746  return_cng_frame();
7747  }
7748  }
7749 
7750  if (bytes && bytes < 5) {
7751  continue;
7752  }
7753 
7754  if (!bytes && poll_loop) {
7755  goto recvfrom;
7756  }
7757 
7758  if (bytes && rtp_session->last_rtp_hdr.m && rtp_session->last_rtp_hdr.pt != rtp_session->recv_te &&
7759  !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] &&
7760  !rtp_session->flags[SWITCH_RTP_FLAG_TEXT] &&
7761  !(rtp_session->rtp_bugs & RTP_BUG_IGNORE_MARK_BIT)) {
7763  }
7764 
7765  if (rtp_session->last_rtp_hdr.pt == rtp_session->cng_pt || rtp_session->last_rtp_hdr.pt == 13) {
7766  *flags |= SFF_NOT_AUDIO;
7767  } else {
7768  *flags &= ~SFF_NOT_AUDIO; /* If this flag was already set, make sure to remove it when we get real audio */
7769  }
7770 
7771  /* ignore packets not meant for us unless the auto-adjust window is open (ice mode has its own alternatives to this) */
7772  /* ------------------- BUNDLE audio skip pt and addr check begin ------------------- */
7774  if (!using_ice(rtp_session) && bytes) {
7775  if (rtp_session->flags[SWITCH_RTP_FLAG_AUTOADJ]) {
7776  if (rtp_session->last_rtp_hdr.pt == rtp_session->cng_pt || rtp_session->last_rtp_hdr.pt == 13) {
7777  goto recvfrom;
7778 
7779  }
7780  } else if (!(rtp_session->rtp_bugs & RTP_BUG_ACCEPT_ANY_PACKETS) && !switch_cmp_addr(rtp_session->rtp_from_addr, rtp_session->remote_addr, SWITCH_FALSE)) {
7781  goto recvfrom;
7782  }
7783  }
7784 
7785  if (bytes && rtp_session->flags[SWITCH_RTP_FLAG_AUTOADJ] && switch_sockaddr_get_port(rtp_session->rtp_from_addr)) {
7786  if (!switch_cmp_addr(rtp_session->rtp_from_addr, rtp_session->remote_addr, SWITCH_FALSE)) {
7787  if (++rtp_session->autoadj_tally >= rtp_session->autoadj_threshold) {
7788  const char *err;
7789  uint32_t old = rtp_session->remote_port;
7790  const char *tx_host;
7791  const char *old_host;
7792  char bufa[50], bufb[50];
7793  char adj_port[6];
7794 
7795  tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtp_from_addr);
7796  old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
7797 
7799  "Auto Changing %s port from %s:%u to %s:%u\n", rtp_type(rtp_session), old_host, old, tx_host,
7800  switch_sockaddr_get_port(rtp_session->rtp_from_addr));
7801 
7802  if (channel) {
7803  char varname[80] = "";
7804 
7805  switch_snprintf(varname, sizeof(varname), "remote_%s_ip_reported", rtp_type(rtp_session));
7806  switch_channel_set_variable(channel, varname, switch_channel_get_variable(channel, "remote_media_ip"));
7807 
7808  switch_snprintf(varname, sizeof(varname), "remote_%s_ip", rtp_type(rtp_session));
7809  switch_channel_set_variable(channel, varname, tx_host);
7810 
7811  switch_snprintf(varname, sizeof(varname), "remote_%s_port_reported", rtp_type(rtp_session));
7812  switch_snprintf(adj_port, sizeof(adj_port), "%u", switch_sockaddr_get_port(rtp_session->rtp_from_addr));
7813  switch_channel_set_variable(channel, varname, switch_channel_get_variable(channel, "remote_media_port"));
7814 
7815  switch_snprintf(varname, sizeof(varname), "remote_%s_port", rtp_type(rtp_session));
7816  switch_channel_set_variable(channel, varname, adj_port);
7817 
7818  switch_snprintf(varname, sizeof(varname), "rtp_auto_adjust_%s", rtp_type(rtp_session));
7819  switch_channel_set_variable(channel, varname, "true");
7820  }
7821  rtp_session->auto_adj_used = 1;
7822  switch_rtp_set_remote_address(rtp_session, tx_host, switch_sockaddr_get_port(rtp_session->rtp_from_addr), 0, SWITCH_FALSE, &err);
7823  if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) {
7826  } else {
7828  }
7829  if (rtp_session->ice.ice_user) {
7830  rtp_session->ice.addr = rtp_session->remote_addr;
7831  }
7832  }
7833  } else {
7834  if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) {
7837  } else {
7838  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Correct %s ip/port confirmed.\n", rtp_type(rtp_session));
7840  }
7841  rtp_session->auto_adj_used = 0;
7842  }
7843  }
7844 
7845  if (bytes && !(rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST) && rtp_session->autoadj_window) {
7846  if (--rtp_session->autoadj_window == 0) {
7848  }
7849  }
7850  } /* ------------------- BUNDLE audio skip pt and addr check end ------------------- */
7851 
7852  if (rtp_session->flags[SWITCH_RTP_FLAG_TEXT]) {
7853  if (!bytes) {
7854  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
7855  switch_core_timer_next(&rtp_session->timer);
7856  }
7857  return_cng_frame();
7858  } else {
7859  *payload_type = rtp_session->last_rtp_hdr.pt;
7860  ret = (int) bytes;
7861  goto end;
7862  }
7863  }
7864 
7865  if (bytes && (rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL])) {
7866  /* Fast PASS! */
7867  *flags |= SFF_PROXY_PACKET;
7868 
7869  if (rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
7870 #if 0
7871  if (rtp_session->has_rtp && check_recv_payload(rtp_session)) {
7873  "Ignoring udptl packet of size of %ld bytes that looks strikingly like a RTP packet.\n", (long)bytes);
7874  bytes = 0;
7875  goto do_continue;
7876  }
7877 #endif
7878  *flags |= SFF_UDPTL_PACKET;
7879  }
7880 
7881  ret = (int) bytes;
7882  goto end;
7883  }
7884 
7885  if (bytes) {
7886  rtp_session->missed_count = 0;
7887 
7888  if (bytes < rtp_header_len) {
7889  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "Ignoring invalid RTP packet size of %ld bytes.\n", (long)bytes);
7890  bytes = 0;
7891  goto do_continue;
7892  }
7893 
7894  if (rtp_session->last_rtp_hdr.pt && (rtp_session->last_rtp_hdr.pt == rtp_session->cng_pt || rtp_session->last_rtp_hdr.pt == 13)) {
7895  return_cng_frame();
7896  }
7897  }
7898 
7899  if (check || bytes) {
7900  do_2833(rtp_session);
7901  }
7902 
7903  if (bytes && rtp_session->recv_msg.header.version != 2) {
7904  uint8_t *data = (uint8_t *) RTP_BODY(rtp_session);
7905 
7906  //if (rtp_session->recv_msg.header.version == 0) {
7907  // if (rtp_session->ice.ice_user) {
7908  // handle_ice(rtp_session, &rtp_session->ice, (void *) &rtp_session->recv_msg, bytes);
7909  // goto recvfrom;
7910  // }
7911  //}
7912 
7913  if (rtp_session->invalid_handler) {
7914  rtp_session->invalid_handler(rtp_session, rtp_session->sock_input, (void *) &rtp_session->recv_msg, bytes, rtp_session->rtp_from_addr);
7915  }
7916 
7917  memset(data, 0, 2);
7918  data[0] = 65;
7919 
7920  rtp_session->last_rtp_hdr.pt = rtp_session->cng_pt != INVALID_PT ? rtp_session->cng_pt : SWITCH_RTP_CNG_PAYLOAD;
7921  *flags |= SFF_CNG;
7922  *payload_type = (switch_payload_t) rtp_session->last_rtp_hdr.pt;
7923  ret = 2 + rtp_header_len;
7924  goto end;
7925  } else if (bytes) {
7926  rtp_session->stats.inbound.period_packet_count++;
7927  }
7928 
7929 
7930  /* Handle incoming RFC2833 packets */
7931  switch (handle_rfc2833(rtp_session, bytes, &do_cng)) {
7932  case RESULT_GOTO_END:
7933  goto end;
7934  case RESULT_GOTO_RECVFROM:
7935  goto recvfrom;
7937  goto timer_check;
7938  case RESULT_CONTINUE:
7939  status = SWITCH_STATUS_SUCCESS;
7940  goto result_continue;
7941  }
7942 
7943  result_continue:
7944  timer_check:
7945 
7946  if (!rtp_session->media_timeout && rtp_session->flags[SWITCH_RTP_FLAG_MUTE]) {
7947  do_cng++;
7948  }
7949 
7950  if (do_cng) {
7951  uint8_t *data = (uint8_t *) RTP_BODY(rtp_session);
7952 
7953  do_2833(rtp_session);
7954 
7955  if (rtp_session->last_cng_ts == rtp_session->last_read_ts + rtp_session->samples_per_interval) {
7956  rtp_session->last_cng_ts = 0;
7957  } else {
7958  rtp_session->last_cng_ts = rtp_session->last_read_ts + rtp_session->samples_per_interval;
7959  }
7960 
7961  memset(data, 0, 2);
7962  data[0] = 65;
7963  rtp_session->last_rtp_hdr.pt = rtp_session->cng_pt != INVALID_PT ? rtp_session->cng_pt : SWITCH_RTP_CNG_PAYLOAD;
7964  *flags |= SFF_CNG;
7965  *payload_type = (switch_payload_t) rtp_session->last_rtp_hdr.pt;
7966  ret = 2 + rtp_header_len;
7967  rtp_session->stats.inbound.skip_packet_count++;
7968  goto end;
7969  }
7970 
7971 
7972  if (check || (bytes && !rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER])) {
7973  if (!bytes && rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) { /* We're late! We're Late! */
7974  if (!rtp_session->flags[SWITCH_RTP_FLAG_NOBLOCK] && status == SWITCH_STATUS_BREAK) {
7975  switch_cond_next();
7976  continue;
7977  }
7978 
7979 
7980 
7981  if (!rtp_session->flags[SWITCH_RTP_FLAG_PAUSE] && !rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] && !rtp_session->dtmf_data.in_digit_ts
7982  && rtp_session->cng_count > (rtp_session->one_second * 2) && rtp_session->jitter_lead > JITTER_LEAD_FRAMES) {
7983 
7984  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "%s %s timeout\n",
7985  rtp_session_name(rtp_session), rtp_type(rtp_session));
7986 
7987  if (rtp_session->media_timeout && rtp_session->last_media) {
7988  check_timeout(rtp_session);
7989  }
7990 
7991  if (rtp_session->stats.inbound.error_log) {
7992  rtp_session->stats.inbound.error_log->flaws++;
7993  }
7994  rtp_session->stats.inbound.flaws++;
7995  do_mos(rtp_session);
7996  }
7997 
7998  rtp_session->cng_count++;
7999  return_cng_frame();
8000  }
8001  }
8002 
8003  rtp_session->cng_count = 0;
8004 
8005  if (status == SWITCH_STATUS_BREAK || bytes == 0) {
8006  if (!(io_flags & SWITCH_IO_FLAG_SINGLE_READ) && rtp_session->flags[SWITCH_RTP_FLAG_DATAWAIT]) {
8007  goto do_continue;
8008  }
8009  return_cng_frame();
8010  }
8011 
8012  if (rtp_session->flags[SWITCH_RTP_FLAG_GOOGLEHACK] && rtp_session->last_rtp_hdr.pt == 102) {
8013  rtp_session->last_rtp_hdr.pt = 97;
8014  }
8015 
8016  break;
8017 
8018  do_continue:
8019 
8020  if (!bytes && !rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && !switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO)) {
8021 
8022  if (sleep_mss) {
8023  switch_yield(sleep_mss);
8024  }
8025  }
8026 
8027  }
8028 
8029  if (switch_rtp_ready(rtp_session)) {
8030  *payload_type = (switch_payload_t) rtp_session->last_rtp_hdr.pt;
8031 
8032  if (*payload_type == SWITCH_RTP_CNG_PAYLOAD) {
8033  *flags |= SFF_CNG;
8034  }
8035 
8036  ret = (int) bytes;
8037  } else {
8038  ret = -1;
8039  }
8040 
8041  end:
8042 
8043  READ_DEC(rtp_session);
8044 
8045  return ret;
8046 }
switch_pollfd_t * read_pollfd
Definition: switch_rtp.c:327
unsigned int switch_queue_size(switch_queue_t *queue)
Definition: switch_apr.c:1238
switch_size_t flaws
Definition: switch_types.h:723
static switch_status_t read_rtcp_packet(switch_rtp_t *rtp_session, switch_size_t *bytes, switch_frame_flag_t *flags)
Definition: switch_rtp.c:7160
static switch_size_t do_flush(switch_rtp_t *rtp_session, int force, switch_size_t bytes_in)
Definition: switch_rtp.c:5681
switch_size_t period_packet_count
Definition: switch_types.h:696
switch_rtp_invalid_handler_t invalid_handler
Definition: switch_rtp.c:382
#define SWITCH_CHANNEL_SESSION_LOG(x)
uint16_t switch_sockaddr_get_port(switch_sockaddr_t *sa)
Definition: switch_apr.c:1001
switch_rtp_numbers_t inbound
Definition: switch_types.h:771
uint8_t switch_rtp_ready(switch_rtp_t *rtp_session)
Test if an RTP session is ready.
Definition: switch_rtp.c:5150
uint32_t max_missed_packets
Definition: switch_rtp.c:441
switch_sockaddr_t * rtcp_from_addr
Definition: switch_rtp.c:398
const char * switch_channel_get_partner_uuid(switch_channel_t *channel)
#define SWITCH_STATUS_IS_BREAK(x)
Definition: switch_utils.h:633
int skip_timer
Definition: switch_rtp.c:492
switch_sockaddr_t * remote_addr
Definition: switch_rtp.c:344
static void check_timeout(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:7282
switch_sockaddr_t * addr
Definition: switch_rtp.c:252
switch_bool_t
Definition: switch_types.h:441
switch_socket_t * sock_input
Definition: switch_rtp.c:326
switch_port_t remote_port
Definition: switch_rtp.c:421
int srtp_idx_rtcp
Definition: switch_rtp.c:368
uint32_t media_timeout
Definition: switch_rtp.c:444
uint8_t has_rtp
Definition: switch_rtp.c:486
#define SWITCH_CRYPTO_MKI_INDEX
#define rtp_session_name(_rtp_session)
Definition: switch_rtp.c:85
switch_rtcp_hdr_t header
Definition: switch_rtp.c:184
uint32_t cng_count
Definition: switch_rtp.c:452
int switch_snprintf(_Out_z_cap_(len) char *buf, _In_ switch_size_t len, _In_z_ _Printf_format_string_ const char *format,...)
uint32_t switch_channel_test_flag(switch_channel_t *channel, switch_channel_flag_t flag)
Test for presence of given flag on a given channel.
switch_time_t last_media
Definition: switch_rtp.c:443
#define READ_DEC(rtp_session)
Definition: switch_rtp.c:68
uint32_t flaws
Definition: switch_types.h:686
struct switch_rtp_rfc2833_data dtmf_data
Definition: switch_rtp.c:426
void rtp_flush_read_buffer(switch_rtp_t *rtp_session, switch_rtp_flush_t flush)
Definition: switch_rtp.c:5638
rtcp_msg_t rtcp_recv_msg
Definition: switch_rtp.c:346
#define rtp_type(rtp_session)
Definition: switch_rtp.c:544
void switch_rtp_reset_media_timer(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:3031
switch_payload_t cng_pt
Definition: switch_rtp.c:429
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
_Ret_ switch_channel_t * switch_core_session_get_channel(_In_ switch_core_session_t *session)
Retrieve a pointer to the channel object associated with a given session.
srtp_ctx_t * send_ctx[2]
Definition: switch_rtp.c:357
switch_queue_t * dtmf_inqueue
Definition: switch_rtp.c:241
if((uint32_t)(unpack->cur - unpack->buf) > unpack->buflen)
#define switch_yield(ms)
Wait a desired number of microseconds and yield the CPU.
Definition: switch_utils.h:998
switch_status_t switch_socket_sendto(switch_socket_t *sock, switch_sockaddr_t *where, int32_t flags, const char *buf, switch_size_t *len)
Definition: switch_apr.c:796
int switch_cmp_addr(switch_sockaddr_t *sa1, switch_sockaddr_t *sa2, switch_bool_t ip_only)
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
#define JITTER_LEAD_FRAMES
Definition: switch_rtp.c:66
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:396
#define switch_channel_get_variable(_c, _v)
static switch_status_t read_rtp_packet(switch_rtp_t *rtp_session, switch_size_t *bytes, switch_frame_flag_t *flags, payload_map_t **pmapP, switch_status_t poll_status, switch_bool_t return_jb_packet)
Definition: switch_rtp.c:5973
switch_sockaddr_t * rtp_from_addr
Definition: switch_rtp.c:398
#define READ_INC(rtp_session)
Definition: switch_rtp.c:67
uint32_t autoadj_threshold
Definition: switch_rtp.c:350
switch_byte_t auto_adj_used
Definition: switch_rtp.c:467
int switch_jb_poll(switch_jb_t *jb)
uint32_t last_read_ts
Definition: switch_rtp.c:387
uint32_t autoadj_window
Definition: switch_rtp.c:349
rtcp_msg_t rtcp_send_msg
Definition: switch_rtp.c:332
char * switch_strerror_r(int errnum, char *buf, switch_size_t buflen)
uintptr_t switch_size_t
void switch_cond_next(void)
Definition: switch_time.c:658
switch_jb_t * vb
Definition: switch_rtp.c:439
switch_status_t switch_rtp_set_remote_address(switch_rtp_t *rtp_session, const char *host, switch_port_t port, switch_port_t remote_rtcp_port, switch_bool_t change_adv_addr, const char **err)
Assign a remote address to the RTP session.
Definition: switch_rtp.c:3128
uint32_t sync_packets
Definition: switch_rtp.c:461
switch_rtp_stats_t stats
Definition: switch_rtp.c:454
void switch_core_session_rwunlock(_In_ switch_core_session_t *session)
Unlock a read or write lock on as given session.
switch_status_t switch_poll(switch_pollfd_t *aprset, int32_t numsock, int32_t *nsds, switch_interval_time_t timeout)
Definition: switch_apr.c:1100
switch_size_t skip_packet_count
Definition: switch_types.h:698
static switch_status_t timer_check(switch_timer_t *timer, switch_bool_t step)
Definition: switch_time.c:903
switch_status_t switch_core_timer_next(switch_timer_t *timer)
Wait for one cycle on an existing timer.
switch_socket_t * rtcp_sock_output
Definition: switch_rtp.c:326
#define RTP_BODY(_s)
Definition: switch_rtp.c:118
void switch_rtp_clear_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Clear an RTP Flag.
Definition: switch_rtp.c:5434
void switch_rtp_set_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Set an RTP Flag.
Definition: switch_rtp.c:5375
switch_jb_t * jb
Definition: switch_rtp.c:438
switch_status_t
Common return values.
void * switch_channel_get_private(switch_channel_t *channel, const char *key)
Retrieve private from a given channel.
uint32_t missed_count
Definition: switch_rtp.c:442
struct error_period * error_log
Definition: switch_types.h:727
static int using_ice(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:2054
uint32_t samples_per_interval
Definition: switch_rtp.c:409
srtp_hdr_t header
Definition: switch_rtp.c:112
switch_timer_t timer
Definition: switch_rtp.c:434
rtp_msg_t recv_msg
Definition: switch_rtp.c:345
#define switch_core_session_locate(uuid_str)
Locate a session based on it&#39;s uuid.
Definition: switch_core.h:932
switch_mutex_t * ice_mutex
Definition: switch_rtp.c:433
static void reset_jitter_seq(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:1650
switch_core_session_t * session
Definition: switch_rtp.c:476
static int check_rtcp_and_ice(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:2137
uint8_t pause_jb
Definition: switch_rtp.c:469
switch_rtp_ice_t ice
Definition: switch_rtp.c:401
int8_t sending_dtmf
Definition: switch_rtp.c:379
switch_rtp_bug_flag_t rtp_bugs
Definition: switch_rtp.c:453
static void do_2833(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:5477
uint32_t ms_per_packet
Definition: switch_rtp.c:414
static int jb_valid(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:5665
switch_payload_t recv_te
Definition: switch_rtp.c:428
switch_queue_t * dtmf_queue
Definition: switch_rtp.c:225
uint32_t hot_hits
Definition: switch_rtp.c:460
uint32_t jitter_lead
Definition: switch_rtp.c:417
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
unsigned int out_digit_dur
Definition: switch_rtp.c:230
uint32_t switch_rtp_test_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flags)
Test an RTP Flag.
Definition: switch_rtp.c:5429
switch_pollfd_t * rtcp_read_pollfd
Definition: switch_rtp.c:327
uint32_t bad_stream
Definition: switch_rtp.c:457
static handle_rfc2833_result_t handle_rfc2833(switch_rtp_t *rtp_session, switch_size_t bytes, int *do_cng)
Definition: switch_rtp.c:586
const char * switch_get_addr(char *buf, switch_size_t len, switch_sockaddr_t *in)
Definition: switch_apr.c:979
uint32_t last_cng_ts
Definition: switch_rtp.c:389
#define SWITCH_RTP_CNG_PAYLOAD
Definition: switch_types.h:783
#define return_cng_frame()
Definition: switch_rtp.c:5848
static switch_status_t process_rtcp_packet(switch_rtp_t *rtp_session, switch_size_t *bytes)
Definition: switch_rtp.c:7102
switch_sockaddr_t * from_addr
Definition: switch_rtp.c:398
static const switch_payload_t INVALID_PT
Definition: switch_rtp.c:81
#define switch_channel_set_variable(_channel, _var, _val)
switch_sockaddr_t * rtcp_remote_addr
Definition: switch_rtp.c:344
rtp_hdr_t last_rtp_hdr
Definition: switch_rtp.c:373
static void do_mos(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:1575
static int check_recv_payload(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:5802
memset(buf, 0, buflen)
uint32_t autoadj_tally
Definition: switch_rtp.c:351
#define rtp_header_len
Definition: switch_rtp.c:73
rtcp_msg_t * rtcp_recv_msg_p
Definition: switch_rtp.c:347
uint32_t one_second
Definition: switch_rtp.c:415
uint8_t switch_payload_t
switch_status_t switch_core_timer_sync(switch_timer_t *timer)

◆ rtp_common_write()

static int rtp_common_write ( switch_rtp_t rtp_session,
rtp_msg_t send_msg,
void *  data,
uint32_t  datalen,
switch_payload_t  payload,
uint32_t  timestamp,
switch_frame_flag_t flags 
)
static

Definition at line 8293 of file switch_rtp.c.

References switch_codec_implementation::actual_samples_per_second, switch_rtp_vad_data::bg_count, switch_rtp_vad_data::bg_len, switch_rtp_vad_data::bg_level, rtp_msg_t::body, CF_AUDIO_VIDEO_BUNDLE, switch_rtp::cn, switch_rtp_vad_data::cng_count, switch_rtp_numbers_t::cng_packet_count, switch_rtp::cng_pt, switch_rtp::delay_samples, ts_normalize_s::delta, ts_normalize_s::delta_ttl, switch_rtp_vad_data::diff_level, switch_rtp_vad_data::fire_events, switch_rtp::flags, get_next_write_ts(), switch_rtp_vad_data::hangover, switch_rtp_vad_data::hangover_hits, switch_rtp_vad_data::hangunder, switch_rtp_vad_data::hangunder_hits, rtp_msg_t::header, switch_codec_implementation::ianacode, switch_rtp::ice_mutex, switch_codec::implementation, INVALID_PT, ts_normalize_s::last_external, ts_normalize_s::last_frame, switch_rtp::last_rtp_hdr, ts_normalize_s::last_ssrc, switch_rtp::last_write_samplecount, switch_rtp::last_write_seq, switch_rtp::last_write_timestamp, switch_rtp::last_write_ts, switch_rtp::local_addr, switch_rtp_numbers_t::media_bytes, switch_rtp_numbers_t::media_packet_count, switch_rtp::ms_per_packet, switch_rtp::need_mark, switch_rtp_vad_data::next_scan, switch_codec_implementation::number_of_channels, switch_rtp_stats_t::outbound, switch_rtp_numbers_t::packet_count, switch_rtp::payload, switch_rtp::pool, switch_rtp::queue_delay, switch_rtp_numbers_t::raw_bytes, switch_rtp_vad_data::read_codec, switch_rtp::remote_addr, switch_rtp::remote_port, switch_rtp_stats_t::rtcp, RTP_BUG_CHANGE_SSRC_ON_MARKER, RTP_BUG_NEVER_SEND_MARKER, switch_rtp::rtp_bugs, switch_rtp::rtp_from_addr, rtp_header_len, rtp_type, rtp_write_ready(), switch_timer::samplecount, switch_rtp::samples_per_interval, switch_rtp::samples_per_second, switch_rtp_vad_data::scan_freq, switch_rtp::send_ctx, switch_rtp::send_msg, switch_rtp::send_policy, switch_rtp::send_time, switch_rtp::sending_dtmf, switch_rtcp_numbers_t::sent_pkt_count, switch_rtp::seq, switch_rtp_vad_data::session, switch_rtp::session, SFF_EXTERNAL, SFF_RFC2833, SJB_QUEUE_ONLY, SJB_VIDEO, switch_rtp::sock_output, switch_rtp::srtp_idx_rtp, switch_rtp::ssrc, switch_rtp_vad_data::start, switch_rtp_vad_data::start_count, switch_rtp_vad_data::start_talking, switch_rtp::stats, switch_rtp_vad_data::stop_talking, switch_channel_event_set_data(), switch_channel_get_name(), switch_channel_get_variable, SWITCH_CHANNEL_LOG, SWITCH_CHANNEL_SESSION_LOG, SWITCH_CHANNEL_SESSION_LOG_CLEAN, switch_channel_test_flag(), switch_clear_flag, switch_core_codec_decode(), switch_core_media_gen_key_frame, switch_core_media_get_rtp_session(), switch_core_session_get_channel(), switch_core_session_get_partner, switch_core_session_request_video_refresh, switch_core_session_rwunlock(), SWITCH_CRYPTO_MKI_INDEX, switch_epoch_time_now(), switch_event_create, switch_event_fire, SWITCH_EVENT_NOTALK, SWITCH_EVENT_TALK, switch_get_addr(), switch_jb_create(), switch_jb_put_packet(), switch_jb_set_flag(), SWITCH_LOG_ALERT, SWITCH_LOG_CONSOLE, SWITCH_LOG_DEBUG1, SWITCH_LOG_ERROR, SWITCH_LOG_INFO, switch_log_printf(), SWITCH_MEDIA_TYPE_VIDEO, switch_micro_time_now(), switch_mutex_lock(), switch_mutex_unlock(), switch_rand(), SWITCH_RECOMMENDED_BUFFER_SIZE, switch_rtp_clear_flag(), SWITCH_RTP_FLAG_BYTESWAP, SWITCH_RTP_FLAG_DEBUG_RTP_WRITE, SWITCH_RTP_FLAG_ENABLE_RTCP, SWITCH_RTP_FLAG_GOOGLEHACK, SWITCH_RTP_FLAG_NACK, SWITCH_RTP_FLAG_PASSTHRU, SWITCH_RTP_FLAG_PAUSE, SWITCH_RTP_FLAG_RESET, SWITCH_RTP_FLAG_SECURE_SEND, SWITCH_RTP_FLAG_SECURE_SEND_MKI, SWITCH_RTP_FLAG_SECURE_SEND_RESET, SWITCH_RTP_FLAG_UDPTL, SWITCH_RTP_FLAG_USE_TIMER, SWITCH_RTP_FLAG_VAD, SWITCH_RTP_FLAG_VIDEO, switch_rtp_ready(), switch_rtp_set_ssrc(), switch_rtp_test_flag(), switch_set_flag, switch_sockaddr_get_port(), switch_socket_sendto(), SWITCH_STATUS_SUCCESS, switch_swap_linear(), switch_test_flag, SWITCH_VAD_FLAG_EVENTS_NOTALK, SWITCH_VAD_FLAG_EVENTS_TALK, SWITCH_VAD_FLAG_TALKING, switch_rtp::te, switch_rtp_vad_data::total_talk_time, ts_normalize_s::ts, switch_rtp::ts, switch_rtp::ts_norm, switch_rtp_vad_data::vad_codec, switch_rtp::vad_data, VAD_FIRE_NOT_TALK, VAD_FIRE_TALK, switch_rtp::vbw, switch_rtp::video_delta_mode, WRITE_DEC, WRITE_INC, and switch_rtp::write_timer.

Referenced by switch_rtp_write_frame().

8295 {
8296  switch_size_t bytes;
8297  uint8_t send = 1;
8298  uint32_t this_ts = 0;
8299  int ret;
8300  switch_time_t now;
8301  uint8_t m = 0;
8302  switch_rtp_t *rtp_bundle_session = NULL;
8303  switch_rtp_t *tmp_session = NULL;
8304 
8305  if (!switch_rtp_ready(rtp_session)) {
8306  return -1;
8307  }
8308 
8309  if (!switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO) &&
8311  rtp_bundle_session = switch_core_media_get_rtp_session(rtp_session->session, SWITCH_MEDIA_TYPE_VIDEO);
8312  }
8313 
8314  if (!rtp_bundle_session && !rtp_write_ready(rtp_session, datalen, __LINE__)) {
8315  return 0;
8316  }
8317 
8318  WRITE_INC(rtp_session);
8319 
8320  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
8321  //switch_core_timer_sync(&rtp_session->write_timer);
8322  }
8323 
8324  if (send_msg) {
8325  bytes = datalen;
8326 
8327  m = (uint8_t) send_msg->header.m;
8328  rtp_session->ts = ntohl(send_msg->header.ts);
8329 
8330  if (flags && *flags & SFF_RFC2833) {
8331  if (rtp_session->te == INVALID_PT) {
8332  ret = 0;
8333  goto end;
8334  }
8335  send_msg->header.pt = rtp_session->te;
8336  }
8337  data = send_msg->body;
8338  if (datalen > rtp_header_len) {
8339  datalen -= rtp_header_len;
8340  }
8341  } else {
8342  if (*flags & SFF_RFC2833) {
8343  if (rtp_session->te == INVALID_PT) {
8344  ret = 0;
8345  goto end;
8346  }
8347  payload = rtp_session->te;
8348  }
8349 
8350  send_msg = &rtp_session->send_msg;
8351  send_msg->header.pt = payload;
8352 
8353  m = get_next_write_ts(rtp_session, timestamp);
8354 
8355  rtp_session->send_msg.header.ts = htonl(rtp_session->ts);
8356 
8357  memcpy(send_msg->body, data, datalen);
8358  bytes = datalen + rtp_header_len;
8359  }
8360 
8361  if (!switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO)) {
8362 
8363  if ((rtp_session->rtp_bugs & RTP_BUG_NEVER_SEND_MARKER)) {
8364  m = 0;
8365  } else {
8366  int delta = rtp_session->ts - rtp_session->last_write_ts;
8367 
8368  if (!rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] &&
8369  ((!rtp_session->flags[SWITCH_RTP_FLAG_RESET] && (abs(delta) > rtp_session->samples_per_interval * 10))
8370  || rtp_session->ts == rtp_session->samples_per_interval)) {
8371  m++;
8372  }
8373 
8374  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
8375  //switch_core_timer_sync(&rtp_session->write_timer);
8376  }
8377 
8378  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] &&
8379  (rtp_session->write_timer.samplecount - rtp_session->last_write_samplecount) > rtp_session->samples_per_interval * 10) {
8380  m++;
8381  }
8382 
8383  if (!rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] &&
8384  ((unsigned) ((switch_micro_time_now() - rtp_session->last_write_timestamp))) > (rtp_session->ms_per_packet * 10)) {
8385  m++;
8386  }
8387 
8388  if (rtp_session->cn && payload != rtp_session->cng_pt) {
8389  rtp_session->cn = 0;
8390  m++;
8391  }
8392 
8393  if (rtp_session->need_mark && !rtp_session->sending_dtmf) {
8394  m++;
8395  rtp_session->need_mark = 0;
8396  }
8397  }
8398 
8399  if (m) {
8400  rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 1;
8401  rtp_session->ts = 0;
8402  }
8403 
8404  /* If the marker was set, and the timestamp seems to have started over - set a new SSRC, to indicate this is a new stream */
8405  if (m && !switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_SEND) && (rtp_session->rtp_bugs & RTP_BUG_CHANGE_SSRC_ON_MARKER) &&
8406  (rtp_session->flags[SWITCH_RTP_FLAG_RESET] || (rtp_session->ts <= rtp_session->last_write_ts && rtp_session->last_write_ts > 0))) {
8407  switch_rtp_set_ssrc(rtp_session, (uint32_t) ((intptr_t) rtp_session + (switch_time_t) switch_epoch_time_now(NULL)));
8408  }
8409 
8411  send_msg->header.m = (m && !(rtp_session->rtp_bugs & RTP_BUG_NEVER_SEND_MARKER)) ? 1 : 0;
8412  }
8413  }
8414 
8415  if (switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO)) {
8416  int external = (flags && *flags & SFF_EXTERNAL);
8417  /* Normalize the timestamps to our own base by generating a made up starting point then adding the measured deltas to that base
8418  so if the timestamps and ssrc of the source change, it will not break the other end's jitter buffer / decoder etc *cough* CHROME *cough*
8419  */
8420 
8421  if (!rtp_session->ts_norm.ts) {
8422  rtp_session->ts_norm.ts = (uint32_t) switch_rand() % 1000000 + 1;
8423  }
8424 
8425  if (!rtp_session->ts_norm.last_ssrc || send_msg->header.ssrc != rtp_session->ts_norm.last_ssrc || rtp_session->ts_norm.last_external != external) {
8426  switch_core_session_t *other_session;
8427 
8430 
8431  if (switch_core_session_get_partner(rtp_session->session, &other_session) == SWITCH_STATUS_SUCCESS) {
8433  switch_core_media_gen_key_frame(other_session);
8434  switch_core_session_rwunlock(other_session);
8435  }
8436 
8437  if (rtp_session->ts_norm.last_ssrc) {
8438  rtp_session->ts_norm.delta_ttl = 0;
8439  rtp_session->ts_norm.ts++;
8440  }
8441 
8442  rtp_session->ts_norm.last_ssrc = send_msg->header.ssrc;
8443  rtp_session->ts_norm.last_frame = ntohl(send_msg->header.ts);
8444  }
8445 
8446  rtp_session->ts_norm.last_external = external;
8447 
8448  if (ntohl(send_msg->header.ts) != rtp_session->ts_norm.last_frame) {
8449  int32_t delta = ntohl(send_msg->header.ts) - rtp_session->ts_norm.last_frame;
8450 
8451  if (delta < 0 || delta > 90000) {
8454  "Timestamp shift detected last: %d this: %d delta: %d stick with prev delta: %d\n",
8455  rtp_session->ts_norm.last_frame, ntohl(send_msg->header.ts), delta, rtp_session->ts_norm.delta);
8456  } else {
8457  rtp_session->ts_norm.delta = delta;
8458  }
8459 
8460  rtp_session->ts_norm.ts += rtp_session->ts_norm.delta;
8461 
8462  }
8463 
8464  rtp_session->ts_norm.last_frame = ntohl(send_msg->header.ts);
8465  send_msg->header.ts = htonl(rtp_session->ts_norm.ts);
8466  this_ts = rtp_session->ts_norm.ts;
8467  }
8468 
8469  send_msg->header.ssrc = htonl(rtp_session->ssrc);
8470 
8471  if (rtp_session->flags[SWITCH_RTP_FLAG_GOOGLEHACK] && rtp_session->send_msg.header.pt == 97) {
8472  rtp_session->last_rtp_hdr.pt = 102;
8473  }
8474 
8475  if (rtp_session->flags[SWITCH_RTP_FLAG_VAD] &&
8476  rtp_session->last_rtp_hdr.pt == rtp_session->vad_data.read_codec->implementation->ianacode) {
8477 
8478  int16_t decoded[SWITCH_RECOMMENDED_BUFFER_SIZE / sizeof(int16_t)] = { 0 };
8479  uint32_t rate = 0;
8480  uint32_t codec_flags = 0;
8481  uint32_t len = sizeof(decoded);
8482  time_t now = switch_epoch_time_now(NULL);
8483  send = 0;
8484 
8485  if (rtp_session->vad_data.scan_freq && rtp_session->vad_data.next_scan <= now) {
8486  rtp_session->vad_data.bg_count = rtp_session->vad_data.bg_level = 0;
8487  rtp_session->vad_data.next_scan = now + rtp_session->vad_data.scan_freq;
8488  }
8489 
8490  if (switch_core_codec_decode(&rtp_session->vad_data.vad_codec,
8491  rtp_session->vad_data.read_codec,
8492  data,
8493  datalen,
8495  decoded, &len, &rate, &codec_flags) == SWITCH_STATUS_SUCCESS) {
8496 
8497  uint32_t energy = 0;
8498  uint32_t x, y = 0, z = len / sizeof(int16_t);
8499  uint32_t score = 0;
8500  int divisor = 0;
8501  if (z) {
8502 
8503  if (!(divisor = rtp_session->vad_data.read_codec->implementation->actual_samples_per_second / 8000)) {
8504  divisor = 1;
8505  }
8506 
8507  for (x = 0; x < z; x++) {
8508  energy += abs(decoded[y]);
8510  }
8511 
8512  if (++rtp_session->vad_data.start_count < rtp_session->vad_data.start) {
8513  send = 1;
8514  } else {
8515  score = (energy / (z / divisor));
8516  if (score && (rtp_session->vad_data.bg_count < rtp_session->vad_data.bg_len)) {
8517  rtp_session->vad_data.bg_level += score;
8518  if (++rtp_session->vad_data.bg_count == rtp_session->vad_data.bg_len) {
8519  rtp_session->vad_data.bg_level /= rtp_session->vad_data.bg_len;
8520  }
8521  send = 1;
8522  } else {
8523  if (score > rtp_session->vad_data.bg_level && !switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)) {
8524  uint32_t diff = score - rtp_session->vad_data.bg_level;
8525 
8526  if (rtp_session->vad_data.hangover_hits) {
8527  rtp_session->vad_data.hangover_hits--;
8528  }
8529 
8530  if (diff >= rtp_session->vad_data.diff_level || ++rtp_session->vad_data.hangunder_hits >= rtp_session->vad_data.hangunder) {
8531 
8533 
8534  rtp_session->vad_data.start_talking = switch_micro_time_now();
8535 
8536  if (!(rtp_session->rtp_bugs & RTP_BUG_NEVER_SEND_MARKER)) {
8537  send_msg->header.m = 1;
8538  }
8539  rtp_session->vad_data.hangover_hits = rtp_session->vad_data.hangunder_hits = rtp_session->vad_data.cng_count = 0;
8541 
8542  if ((rtp_session->vad_data.fire_events & VAD_FIRE_TALK)) {
8543  switch_event_t *event;
8546  switch_event_fire(&event);
8547  }
8548  }
8549  }
8550  }
8551  } else {
8552  if (rtp_session->vad_data.hangunder_hits) {
8553  rtp_session->vad_data.hangunder_hits--;
8554  }
8555  if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)) {
8556  if (++rtp_session->vad_data.hangover_hits >= rtp_session->vad_data.hangover) {
8557  rtp_session->vad_data.stop_talking = switch_micro_time_now();
8558  rtp_session->vad_data.total_talk_time += (rtp_session->vad_data.stop_talking - rtp_session->vad_data.start_talking);
8559 
8561 
8562  rtp_session->vad_data.hangover_hits = rtp_session->vad_data.hangunder_hits = rtp_session->vad_data.cng_count = 0;
8564 
8565  if ((rtp_session->vad_data.fire_events & VAD_FIRE_NOT_TALK)) {
8566  switch_event_t *event;
8569  switch_event_fire(&event);
8570  }
8571  }
8572  }
8573  }
8574  }
8575  }
8576  }
8577  }
8578 
8579  if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)) {
8580  send = 1;
8581  }
8582  }
8583  } else {
8584  ret = -1;
8585  goto end;
8586  }
8587  }
8588 
8589  if (!switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO)) {
8590  uint32_t ts_delta;
8591 
8592  this_ts = ntohl(send_msg->header.ts);
8593 
8594  ts_delta = abs((int32_t)(this_ts - rtp_session->last_write_ts));
8595 
8596  if (ts_delta > rtp_session->samples_per_second * 2) {
8597  rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 1;
8598  }
8599 #ifdef DEBUG_TS_ROLLOVER
8600  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "WRITE TS LAST:%u THIS:%u DELTA:%u\n", rtp_session->last_write_ts, this_ts, ts_delta);
8601 #endif
8602  if ((!(flags && *flags & SFF_RFC2833) && ts_delta == 0) || !switch_rtp_ready(rtp_session) || rtp_session->sending_dtmf) {
8603  send = 0;
8604  }
8605  }
8606 
8607  if (rtp_session->flags[SWITCH_RTP_FLAG_PAUSE]) {
8608  send = 0;
8609  }
8610 
8611  if (send) {
8612  int delta = 1;
8613 
8614  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && (*flags & SFF_EXTERNAL) &&
8615  rtp_session->stats.outbound.packet_count && rtp_session->flags[SWITCH_RTP_FLAG_PASSTHRU]) {
8616  int32_t x = rtp_session->last_write_seq;
8617  int32_t y = ntohs(send_msg->header.seq);
8618 
8619  if (!rtp_session->video_delta_mode) {
8620  rtp_session->video_delta_mode = 1;
8621  } else {
8622  if (x > UINT16_MAX / 2 && y < UINT16_MAX / 2) {
8623  x -= (int32_t)UINT16_MAX+1;
8624  }
8625 
8626  delta = y-x;
8627  }
8628 
8629  rtp_session->last_write_seq = y;
8630  }
8631 
8632  if (!rtp_session->flags[SWITCH_RTP_FLAG_PASSTHRU]) {
8633  rtp_session->video_delta_mode = 0;
8634  }
8635 
8636  rtp_session->seq += delta;
8637 
8638  send_msg->header.seq = htons(rtp_session->seq);
8639 
8640  if (rtp_session->flags[SWITCH_RTP_FLAG_BYTESWAP] && send_msg->header.pt == rtp_session->payload) {
8641  switch_swap_linear((int16_t *)send_msg->body, (int) datalen);
8642  }
8643 
8644  if (rtp_bundle_session) {
8645  tmp_session = rtp_session;
8646  rtp_session = rtp_bundle_session;
8647  }
8648 
8649 #ifdef ENABLE_SRTP
8650  switch_mutex_lock(rtp_session->ice_mutex);
8651  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND]) {
8652  int sbytes = (int) bytes;
8653  srtp_err_status_t stat;
8654 
8655 
8656  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_RESET] || !rtp_session->send_ctx[rtp_session->srtp_idx_rtp]) {
8657 
8659  srtp_dealloc(rtp_session->send_ctx[rtp_session->srtp_idx_rtp]);
8660  rtp_session->send_ctx[rtp_session->srtp_idx_rtp] = NULL;
8661  if (srtp_create(&rtp_session->send_ctx[rtp_session->srtp_idx_rtp],
8662  &rtp_session->send_policy[rtp_session->srtp_idx_rtp]) || !rtp_session->send_ctx[rtp_session->srtp_idx_rtp]) {
8664  "Error! RE-Activating %s Secure RTP SEND\n", rtp_type(rtp_session));
8665  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND] = 0;
8666  ret = -1;
8667  switch_mutex_unlock(rtp_session->ice_mutex);
8668  goto end;
8669  } else {
8671  "RE-Activating %s Secure RTP SEND\n", rtp_type(rtp_session));
8672  }
8673  }
8674 
8675  if (!rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_MKI]) {
8676  stat = srtp_protect(rtp_session->send_ctx[rtp_session->srtp_idx_rtp], send_msg, &sbytes);
8677  } else {
8678  stat = srtp_protect_mki(rtp_session->send_ctx[rtp_session->srtp_idx_rtp], send_msg, &sbytes, 1, SWITCH_CRYPTO_MKI_INDEX);
8679  }
8680 
8681  if (stat) {
8683  "Error: %s SRTP protection failed with code %d\n", rtp_type(rtp_session), stat);
8684  }
8685 
8686  bytes = sbytes;
8687  }
8688  switch_mutex_unlock(rtp_session->ice_mutex);
8689 #endif
8690 
8691  now = switch_micro_time_now();
8692 #ifdef RTP_DEBUG_WRITE_DELTA
8693  {
8694  int delta = (int) (now - rtp_session->send_time) / 1000;
8695  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "WRITE %d delta %d\n", (int) bytes, delta);
8696  }
8697 #endif
8698  rtp_session->send_time = now;
8699 
8700  if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]) {
8701  const char *tx_host;
8702  const char *old_host;
8703  const char *my_host;
8704 
8705  char bufa[50], bufb[50], bufc[50];
8706 
8707 
8708  tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtp_from_addr);
8709  old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
8710  my_host = switch_get_addr(bufc, sizeof(bufc), rtp_session->local_addr);
8711 
8713  "W %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n",
8714  rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "NoName",
8715  (long) bytes,
8716  my_host, switch_sockaddr_get_port(rtp_session->local_addr),
8717  old_host, rtp_session->remote_port,
8718  tx_host, switch_sockaddr_get_port(rtp_session->rtp_from_addr),
8719  send_msg->header.pt, ntohl(send_msg->header.ts), ntohs(send_msg->header.seq), send_msg->header.m);
8720 
8721  }
8722 
8723  if (rtp_session->flags[SWITCH_RTP_FLAG_NACK]) {
8724  switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
8725 
8726  if (!rtp_session->vbw) {
8727  int nack_size = 100;
8728  const char *var;
8729 
8730  if ((var = switch_channel_get_variable(channel, "rtp_nack_buffer_size"))) {
8731  int tmp = atoi(var);
8732 
8733  if (tmp > 0 && tmp < 500) {
8734  nack_size = tmp;
8735  }
8736  }
8737 
8738  switch_jb_create(&rtp_session->vbw, SJB_VIDEO, nack_size, nack_size, rtp_session->pool);
8739 
8740  if (rtp_session->vbw) {
8741  switch_jb_set_flag(rtp_session->vbw, SJB_QUEUE_ONLY);
8742  //switch_jb_debug_level(rtp_session->vbw, 10);
8743  }
8744  }
8745  switch_jb_put_packet(rtp_session->vbw, (switch_rtp_packet_t *)send_msg, bytes);
8746  }
8747 
8748 #ifdef RTP_WRITE_PLOSS
8749  {
8750  int r = (rand() % 10000) + 1;
8751 
8752  if (r <= 200) {
8754  "Simulate dropping packet ......... ts: %u seq: %u\n", ntohl(send_msg->header.ts), ntohs(send_msg->header.seq));
8755  } else {
8756  if (switch_socket_sendto(rtp_session->sock_output, rtp_session->remote_addr, 0, (void *) send_msg, &bytes) != SWITCH_STATUS_SUCCESS) {
8757  rtp_session->seq--;
8758  ret = -1;
8759  goto end;
8760  }
8761  }
8762  }
8763 #else
8764  //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
8765  //
8766  // rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_READ]++;
8767  //
8768  // //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "SEND %u\n", ntohs(send_msg->header.seq));
8769  //}
8770  if (switch_socket_sendto(rtp_session->sock_output, rtp_session->remote_addr, 0, (void *) send_msg, &bytes) != SWITCH_STATUS_SUCCESS) {
8771  rtp_session->seq -= delta;
8772 
8773  ret = -1;
8774  goto end;
8775  }
8776 #endif
8777  if (rtp_bundle_session) rtp_session = tmp_session;
8778 
8779  rtp_session->last_write_ts = this_ts;
8780  rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 0;
8781 
8782  if (rtp_session->queue_delay) {
8783  rtp_session->delay_samples = rtp_session->queue_delay;
8784  rtp_session->queue_delay = 0;
8785  }
8786 
8787  rtp_session->stats.outbound.raw_bytes += bytes;
8788  rtp_session->stats.outbound.packet_count++;
8789 
8790  if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
8791  rtp_session->stats.rtcp.sent_pkt_count++;
8792  }
8793 
8794  if (send_msg->header.pt == rtp_session->cng_pt) {
8795  rtp_session->stats.outbound.cng_packet_count++;
8796  } else {
8797  rtp_session->stats.outbound.media_packet_count++;
8798  rtp_session->stats.outbound.media_bytes += bytes;
8799  }
8800 
8801  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
8802  //switch_core_timer_sync(&rtp_session->write_timer);
8803  rtp_session->last_write_samplecount = rtp_session->write_timer.samplecount;
8804  }
8805 
8807  }
8808 
8809  ret = (int) bytes;
8810 
8811  end:
8812 
8813  WRITE_DEC(rtp_session);
8814 
8815  return ret;
8816 }
uint32_t queue_delay
Definition: switch_rtp.c:394
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
Definition: switch_time.c:311
#define switch_event_fire(event)
Fire an event filling in most of the arguements with obvious values.
Definition: switch_event.h:413
#define switch_core_media_gen_key_frame(_session)
switch_time_t total_talk_time
Definition: switch_rtp.c:220
#define SWITCH_CHANNEL_SESSION_LOG(x)
uint16_t switch_sockaddr_get_port(switch_sockaddr_t *sa)
Definition: switch_apr.c:1001
#define switch_set_flag(obj, flag)
Set a flag on an arbitrary object.
Definition: switch_utils.h:700
uint8_t switch_rtp_ready(switch_rtp_t *rtp_session)
Test if an RTP session is ready.
Definition: switch_rtp.c:5150
#define SWITCH_CHANNEL_LOG
switch_sockaddr_t * remote_addr
Definition: switch_rtp.c:344
uint32_t ts
Definition: switch_rtp.c:313
uint32_t delay_samples
Definition: switch_rtp.c:391
#define SWITCH_RECOMMENDED_BUFFER_SIZE
Definition: switch_types.h:594
void switch_channel_event_set_data(_In_ switch_channel_t *channel, _In_ switch_event_t *event)
Add information about a given channel to an event object.
#define WRITE_INC(rtp_session)
Definition: switch_rtp.c:69
uint8_t video_delta_mode
Definition: switch_rtp.c:472
switch_port_t remote_port
Definition: switch_rtp.c:421
Representation of an event.
Definition: switch_event.h:80
srtp_policy_t send_policy[2]
Definition: switch_rtp.c:360
struct switch_rtp_vad_data vad_data
Definition: switch_rtp.c:425
#define SWITCH_CRYPTO_MKI_INDEX
uint32_t last_ssrc
Definition: switch_rtp.c:311
switch_rtp_t * switch_core_media_get_rtp_session(switch_core_session_t *session, switch_media_type_t type)
switch_status_t switch_core_codec_decode(switch_codec_t *codec, switch_codec_t *other_codec, void *encoded_data, uint32_t encoded_data_len, uint32_t encoded_rate, void *decoded_data, uint32_t *decoded_data_len, uint32_t *decoded_rate, unsigned int *flag)
Decode data using a codec handle.
switch_jb_t * vbw
Definition: switch_rtp.c:440
switch_status_t switch_jb_put_packet(switch_jb_t *jb, switch_rtp_packet_t *packet, switch_size_t len)
switch_size_t media_bytes
Definition: switch_types.h:694
uint32_t switch_channel_test_flag(switch_channel_t *channel, switch_channel_flag_t flag)
Test for presence of given flag on a given channel.
switch_codec_t vad_codec
Definition: switch_rtp.c:200
switch_core_session_t * session
Definition: switch_rtp.c:199
uint16_t last_write_seq
Definition: switch_rtp.c:471
#define rtp_type(rtp_session)
Definition: switch_rtp.c:544
switch_payload_t cng_pt
Definition: switch_rtp.c:429
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
_Ret_ switch_channel_t * switch_core_session_get_channel(_In_ switch_core_session_t *session)
Retrieve a pointer to the channel object associated with a given session.
srtp_ctx_t * send_ctx[2]
Definition: switch_rtp.c:357
#define switch_clear_flag(obj, flag)
Clear a flag on an arbitrary object while locked.
Definition: switch_utils.h:724
int64_t switch_time_t
Definition: switch_apr.h:188
const switch_codec_implementation_t * implementation
switch_rtcp_numbers_t rtcp
Definition: switch_types.h:773
#define WRITE_DEC(rtp_session)
Definition: switch_rtp.c:70
SWITCH_BEGIN_EXTERN_C switch_status_t switch_jb_create(switch_jb_t **jbp, switch_jb_type_t type, uint32_t min_frame_len, uint32_t max_frame_len, switch_memory_pool_t *pool)
switch_size_t raw_bytes
Definition: switch_types.h:693
switch_status_t switch_socket_sendto(switch_socket_t *sock, switch_sockaddr_t *where, int32_t flags, const char *buf, switch_size_t *len)
Definition: switch_apr.c:796
uint32_t ts
Definition: switch_rtp.c:384
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:396
#define switch_core_session_request_video_refresh(_s)
Definition: switch_core.h:2892
#define switch_channel_get_variable(_c, _v)
int switch_rand(void)
switch_sockaddr_t * rtp_from_addr
Definition: switch_rtp.c:398
switch_rtp_numbers_t outbound
Definition: switch_types.h:772
uint32_t samples_per_second
Definition: switch_rtp.c:410
char body[SWITCH_RTP_MAX_BUF_LEN+4+sizeof(char *)]
Definition: switch_rtp.c:113
switch_status_t switch_rtp_set_ssrc(switch_rtp_t *rtp_session, uint32_t ssrc)
Definition: switch_rtp.c:4461
void switch_jb_set_flag(switch_jb_t *jb, switch_jb_flag_t flag)
static int rtp_write_ready(switch_rtp_t *rtp_session, uint32_t bytes, int line)
Definition: switch_rtp.c:8273
uintptr_t switch_size_t
switch_sockaddr_t * local_addr
Definition: switch_rtp.c:330
uint16_t seq
Definition: switch_rtp.c:375
void switch_swap_linear(int16_t *buf, int len)
Perform a byteswap on a buffer of 16 bit samples.
switch_time_t start_talking
Definition: switch_rtp.c:218
#define switch_core_session_get_partner(_session, _partner)
Definition: switch_core.h:1028
uint8_t need_mark
Definition: switch_rtp.c:380
switch_size_t packet_count
Definition: switch_types.h:695
uint32_t delta
Definition: switch_rtp.c:314
switch_rtp_stats_t stats
Definition: switch_rtp.c:454
void switch_core_session_rwunlock(_In_ switch_core_session_t *session)
Unlock a read or write lock on as given session.
switch_memory_pool_t * pool
Definition: switch_rtp.c:397
#define SWITCH_CHANNEL_SESSION_LOG_CLEAN(x)
void switch_rtp_clear_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Clear an RTP Flag.
Definition: switch_rtp.c:5434
switch_time_t stop_talking
Definition: switch_rtp.c:219
uint8_t cn
Definition: switch_rtp.c:437
uint32_t samples_per_interval
Definition: switch_rtp.c:409
switch_time_t send_time
Definition: switch_rtp.c:466
srtp_hdr_t header
Definition: switch_rtp.c:112
uint32_t last_write_ts
Definition: switch_rtp.c:386
switch_mutex_t * ice_mutex
Definition: switch_rtp.c:433
switch_core_session_t * session
Definition: switch_rtp.c:476
#define switch_event_create(event, id)
Create a new event assuming it will not be custom event and therefore hiding the unused parameters...
Definition: switch_event.h:384
switch_timer_t write_timer
Definition: switch_rtp.c:435
switch_time_t last_write_timestamp
Definition: switch_rtp.c:395
int8_t sending_dtmf
Definition: switch_rtp.c:379
switch_size_t media_packet_count
Definition: switch_types.h:697
switch_rtp_bug_flag_t rtp_bugs
Definition: switch_rtp.c:453
int srtp_idx_rtp
Definition: switch_rtp.c:367
uint32_t ms_per_packet
Definition: switch_rtp.c:414
time_t switch_epoch_time_now(time_t *t)
Get the current epoch time.
Definition: switch_time.c:322
switch_payload_t te
Definition: switch_rtp.c:427
uint32_t ssrc
Definition: switch_rtp.c:376
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:693
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
uint32_t switch_rtp_test_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flags)
Test an RTP Flag.
Definition: switch_rtp.c:5429
uint32_t last_write_samplecount
Definition: switch_rtp.c:390
const char * switch_get_addr(char *buf, switch_size_t len, switch_sockaddr_t *in)
Definition: switch_apr.c:979
uint8_t hangunder_hits
Definition: switch_rtp.c:207
rtp_msg_t send_msg
Definition: switch_rtp.c:331
static const switch_payload_t INVALID_PT
Definition: switch_rtp.c:81
uint32_t last_frame
Definition: switch_rtp.c:312
rtp_hdr_t last_rtp_hdr
Definition: switch_rtp.c:373
char * switch_channel_get_name(switch_channel_t *channel)
Retrieve the name of a given channel.
switch_payload_t payload
Definition: switch_rtp.c:381
switch_codec_t * read_codec
Definition: switch_rtp.c:201
static uint8_t get_next_write_ts(switch_rtp_t *rtp_session, uint32_t timestamp)
Definition: switch_rtp.c:1547
#define rtp_header_len
Definition: switch_rtp.c:73
uint32_t delta_ttl
Definition: switch_rtp.c:315
switch_socket_t * sock_output
Definition: switch_rtp.c:326
ts_normalize_t ts_norm
Definition: switch_rtp.c:343
switch_size_t cng_packet_count
Definition: switch_types.h:701

◆ rtp_write_ready()

static int rtp_write_ready ( switch_rtp_t rtp_session,
uint32_t  bytes,
int  line 
)
static

Definition at line 8273 of file switch_rtp.c.

References DS_READY, switch_rtp::dtls, switch_rtp::ice, switch_rtp_ice_t::ice_user, switch_rtp_ice_t::ready, switch_rtp_ice_t::rready, rtp_type, switch_rtp::session, switch_dtls_s::state, SWITCH_CHANNEL_SESSION_LOG, SWITCH_LOG_DEBUG3, and switch_log_printf().

Referenced by handle_rfc2833(), read_rtp_packet(), rtp_common_write(), switch_rtp_ack_bitrate(), switch_rtp_req_bitrate(), switch_rtp_video_loss(), switch_rtp_video_refresh(), switch_rtp_write_frame(), switch_rtp_write_manual(), and switch_rtp_write_raw().

8274 {
8275  if (!rtp_session) return 0;
8276 
8277  if (rtp_session->ice.ice_user && !(rtp_session->ice.rready || rtp_session->ice.ready)) {
8278  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "Skip sending %s packet %ld bytes (ice not ready @ line %d!)\n",
8279  rtp_type(rtp_session), (long)bytes, line);
8280  return 0;
8281  }
8282 
8283  if (rtp_session->dtls && rtp_session->dtls->state != DS_READY) {
8284  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "Skip sending %s packet %ld bytes (dtls not ready @ line %d!)\n",
8285  rtp_type(rtp_session), (long)bytes, line);
8286  return 0;
8287  }
8288 
8289  return 1;
8290 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
dtls_state_t state
Definition: switch_rtp.c:284
#define rtp_type(rtp_session)
Definition: switch_rtp.c:544
switch_dtls_t * dtls
Definition: switch_rtp.c:370
switch_core_session_t * session
Definition: switch_rtp.c:476
switch_rtp_ice_t ice
Definition: switch_rtp.c:401
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.

◆ set_dtmf_delay()

static void set_dtmf_delay ( switch_rtp_t rtp_session,
uint32_t  ms,
uint32_t  max_ms 
)
static

Definition at line 5455 of file switch_rtp.c.

References switch_rtp::flags, switch_rtp::last_write_ts, switch_rtp::max_next_write_samplecount, switch_rtp::next_write_samplecount, switch_rtp::queue_delay, switch_timer::samplecount, switch_rtp::samples_per_second, switch_rtp::sending_dtmf, switch_rtp::session, SWITCH_CHANNEL_SESSION_LOG, SWITCH_LOG_DEBUG, switch_log_printf(), SWITCH_RTP_FLAG_USE_TIMER, and switch_rtp::timer.

Referenced by do_2833().

5456 {
5457  int upsamp, max_upsamp;
5458 
5459 
5460  if (!max_ms) max_ms = ms;
5461 
5462  upsamp = ms * (rtp_session->samples_per_second / 1000);
5463  max_upsamp = max_ms * (rtp_session->samples_per_second / 1000);
5464 
5465  rtp_session->sending_dtmf = 0;
5466  rtp_session->queue_delay = upsamp;
5467 
5468  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
5469  rtp_session->max_next_write_samplecount = rtp_session->timer.samplecount + max_upsamp;
5470  rtp_session->next_write_samplecount = rtp_session->timer.samplecount + upsamp;
5471  rtp_session->last_write_ts += upsamp;
5472  }
5473 
5474  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Queue digit delay of %dms\n", ms);
5475 }
uint32_t queue_delay
Definition: switch_rtp.c:394
#define SWITCH_CHANNEL_SESSION_LOG(x)
uint32_t max_next_write_samplecount
Definition: switch_rtp.c:393
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:396
uint32_t samples_per_second
Definition: switch_rtp.c:410
switch_timer_t timer
Definition: switch_rtp.c:434
uint32_t last_write_ts
Definition: switch_rtp.c:386
switch_core_session_t * session
Definition: switch_rtp.c:476
int8_t sending_dtmf
Definition: switch_rtp.c:379
uint32_t next_write_samplecount
Definition: switch_rtp.c:392
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.

◆ switch_rtp_add_crypto_key()

switch_status_t switch_rtp_add_crypto_key ( switch_rtp_t rtp_session,
switch_rtp_crypto_direction_t  direction,
uint32_t  index,
switch_secure_settings_t ssec 
)

Definition at line 4062 of file switch_rtp.c.

References AEAD_AES_128_GCM, AEAD_AES_128_GCM_8, AEAD_AES_256_GCM, AEAD_AES_256_GCM_8, AES_CM_128_HMAC_SHA1_32, AES_CM_128_HMAC_SHA1_80, AES_CM_128_NULL_AUTH, AES_CM_192_HMAC_SHA1_32, AES_CM_192_HMAC_SHA1_80, AES_CM_256_HMAC_SHA1_32, AES_CM_256_HMAC_SHA1_80, switch_rtp::crypto_keys, secure_settings_s::crypto_type, switch_rtp::dtls, switch_rtp::flags, switch_rtp_crypto_key::index, index, switch_rtp_crypto_key::keysalt, secure_settings_s::local_key_material_n, secure_settings_s::local_key_material_next, secure_settings_s::local_raw_key, memset(), switch_crypto_key_material_s::mki_id, switch_crypto_key_material_s::mki_size, switch_rtp_crypto_key::next, switch_crypto_key_material_s::next, switch_rtp::pool, switch_crypto_key_material_s::raw_key, switch_rtp::recv_ctx, switch_rtp::recv_policy, secure_settings_s::remote_key_material_n, secure_settings_s::remote_key_material_next, secure_settings_s::remote_raw_key, rtp_type, switch_rtp::send_ctx, switch_rtp::send_policy, switch_rtp::session, switch_rtp::srtp_idx_rtcp, switch_b64_encode(), SWITCH_CALL_DIRECTION_OUTBOUND, switch_channel_direction(), switch_channel_get_uuid(), switch_channel_get_variable, SWITCH_CHANNEL_SESSION_LOG, switch_channel_set_variable, switch_core_alloc, switch_core_get_variable(), switch_core_media_crypto_keysalt_len(), switch_core_session_get_channel(), switch_event_add_header(), switch_event_add_header_string(), SWITCH_EVENT_CALL_SECURE, switch_event_create, switch_event_fire, SWITCH_LOG_CRIT, SWITCH_LOG_ERROR, SWITCH_LOG_INFO, switch_log_printf(), SWITCH_RTP_CRYPTO_MAX, SWITCH_RTP_CRYPTO_RECV, SWITCH_RTP_CRYPTO_RECV_RTCP, SWITCH_RTP_CRYPTO_SEND, SWITCH_RTP_CRYPTO_SEND_RTCP, SWITCH_RTP_FLAG_SECURE_RECV, SWITCH_RTP_FLAG_SECURE_RECV_MKI, SWITCH_RTP_FLAG_SECURE_RECV_RESET, SWITCH_RTP_FLAG_SECURE_SEND, SWITCH_RTP_FLAG_SECURE_SEND_MKI, SWITCH_RTP_FLAG_SECURE_SEND_RESET, SWITCH_RTP_FLAG_VIDEO, SWITCH_RTP_MAX_CRYPTO_LEN, SWITCH_STACK_BOTTOM, SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, switch_true(), and switch_rtp_crypto_key::type.

Referenced by dtls_state_setup(), switch_core_media_recover_session(), switch_core_session_apply_crypto(), and switch_core_session_check_incoming_crypto().

4063 {
4064 #ifndef ENABLE_SRTP
4065  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_CRIT, "SRTP NOT SUPPORTED IN THIS BUILD!\n");
4066  return SWITCH_STATUS_FALSE;
4067 #else
4068 
4069  switch_rtp_crypto_key_t *crypto_key;
4070  srtp_policy_t *policy;
4071  srtp_err_status_t stat;
4073 
4074  switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
4075  switch_event_t *fsevent = NULL;
4076  int idx = 0;
4077  const char *var;
4078  unsigned char b64_key[512] = "";
4079 
4080  unsigned char *keysalt = NULL;
4081  switch_size_t keysalt_len = 0;
4082 
4083  switch_crypto_key_material_t *key_material = NULL;
4084  unsigned long *key_material_n = NULL;
4085  srtp_master_key_t **mkis = NULL;
4086  srtp_master_key_t *mki = NULL;
4087  int mki_idx = 0;
4088 
4090 
4091  if (direction >= SWITCH_RTP_CRYPTO_MAX || keysalt_len > SWITCH_RTP_MAX_CRYPTO_LEN) {
4092  return SWITCH_STATUS_FALSE;
4093  }
4094 
4095  if (direction == SWITCH_RTP_CRYPTO_RECV_RTCP) {
4096  direction = SWITCH_RTP_CRYPTO_RECV;
4097  rtp_session->srtp_idx_rtcp = idx = 1;
4098  } else if (direction == SWITCH_RTP_CRYPTO_SEND_RTCP) {
4099  direction = SWITCH_RTP_CRYPTO_SEND;
4100  rtp_session->srtp_idx_rtcp = idx = 1;
4101  }
4102 
4103  if (direction == SWITCH_RTP_CRYPTO_RECV) {
4104  policy = &rtp_session->recv_policy[idx];
4105  keysalt = ssec->remote_raw_key;
4106  key_material = ssec->remote_key_material_next;
4107  key_material_n = &ssec->remote_key_material_n;
4108  } else {
4109  policy = &rtp_session->send_policy[idx];
4110  keysalt = ssec->local_raw_key;
4111  key_material = ssec->local_key_material_next;
4112  key_material_n = &ssec->local_key_material_n;
4113  }
4114 
4115  switch_b64_encode(keysalt, keysalt_len, b64_key, sizeof(b64_key));
4116 
4117  if (switch_true(switch_core_get_variable("rtp_retain_crypto_keys"))) {
4118  switch(direction) {
4120  switch_channel_set_variable(channel, "srtp_local_crypto_key", (const char *)b64_key);
4121  break;
4123  switch_channel_set_variable(channel, "srtp_remote_crypto_key", (const char *)b64_key);
4124  break;
4125  default:
4126  break;
4127  }
4128 
4129  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
4130  switch(direction) {
4132  switch_channel_set_variable(channel, "srtp_local_video_crypto_key", (const char *)b64_key);
4133  break;
4135  switch_channel_set_variable(channel, "srtp_remote_video_crypto_key", (const char *)b64_key);
4136  break;
4137  default:
4138  break;
4139  }
4140 
4141  } else {
4142  switch(direction) {
4144  switch_channel_set_variable(channel, "srtp_local_audio_crypto_key", (const char *)b64_key);
4145  break;
4147  switch_channel_set_variable(channel, "srtp_remote_audio_crypto_key", (const char *)b64_key);
4148  break;
4149  default:
4150  break;
4151  }
4152  }
4153  }
4154 
4155  crypto_key = switch_core_alloc(rtp_session->pool, sizeof(*crypto_key));
4156 
4157  crypto_key->type = ssec->crypto_type;
4158  crypto_key->index = index;
4159  memcpy(crypto_key->keysalt, keysalt, keysalt_len);
4160  crypto_key->next = rtp_session->crypto_keys[direction];
4161  rtp_session->crypto_keys[direction] = crypto_key;
4162 
4163  memset(policy, 0, sizeof(*policy));
4164 
4165  /* many devices can't handle gaps in SRTP streams */
4166  if (!((var = switch_channel_get_variable(channel, "srtp_allow_idle_gaps"))
4167  && switch_true(var))
4168  && (!(var = switch_channel_get_variable(channel, "send_silence_when_idle"))
4169  || !(atoi(var)))) {
4170  switch_channel_set_variable(channel, "send_silence_when_idle", "-1");
4171  }
4172 
4173  switch (crypto_key->type) {
4175  srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy->rtp);
4176  srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy->rtcp);
4177 
4179  switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_128_HMAC_SHA1_80");
4180  }
4181  break;
4183  srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(&policy->rtp);
4184  srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(&policy->rtcp);
4185 
4186 
4188  switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_128_HMAC_SHA1_32");
4189  }
4190  break;
4191 
4192  case AEAD_AES_256_GCM_8:
4193  srtp_crypto_policy_set_aes_gcm_256_8_auth(&policy->rtp);
4194  srtp_crypto_policy_set_aes_gcm_256_8_auth(&policy->rtcp);
4195 
4197  switch_channel_set_variable(channel, "rtp_has_crypto", "AEAD_AES_256_GCM_8");
4198  }
4199  break;
4200 
4201  case AEAD_AES_256_GCM:
4202  srtp_crypto_policy_set_aes_gcm_256_16_auth(&policy->rtp);
4203  srtp_crypto_policy_set_aes_gcm_256_16_auth(&policy->rtcp);
4204 
4206  switch_channel_set_variable(channel, "rtp_has_crypto", "AEAD_AES_256_GCM");
4207  }
4208  break;
4209 
4210  case AEAD_AES_128_GCM_8:
4211  srtp_crypto_policy_set_aes_gcm_128_8_auth(&policy->rtp);
4212  srtp_crypto_policy_set_aes_gcm_128_8_auth(&policy->rtcp);
4213 
4215  switch_channel_set_variable(channel, "rtp_has_crypto", "AEAD_AES_128_GCM_8");
4216  }
4217  break;
4218 
4219  case AEAD_AES_128_GCM:
4220  srtp_crypto_policy_set_aes_gcm_128_16_auth(&policy->rtp);
4221  srtp_crypto_policy_set_aes_gcm_128_16_auth(&policy->rtcp);
4222 
4224  switch_channel_set_variable(channel, "rtp_has_crypto", "AEAD_AES_128_GCM");
4225  }
4226  break;
4227 
4229  srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy->rtp);
4230  srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy->rtcp);
4232  switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_256_HMAC_SHA1_80");
4233  }
4234  break;
4236  srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32(&policy->rtp);
4237  srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32(&policy->rtcp);
4239  switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_256_HMAC_SHA1_32");
4240  }
4241  break;
4243  srtp_crypto_policy_set_aes_cm_192_hmac_sha1_80(&policy->rtp);
4244  srtp_crypto_policy_set_aes_cm_192_hmac_sha1_80(&policy->rtcp);
4246  switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_192_HMAC_SHA1_80");
4247  }
4248  break;
4250  srtp_crypto_policy_set_aes_cm_192_hmac_sha1_32(&policy->rtp);
4251  srtp_crypto_policy_set_aes_cm_192_hmac_sha1_32(&policy->rtcp);
4253  switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_192_HMAC_SHA1_32");
4254  }
4255  break;
4256  case AES_CM_128_NULL_AUTH:
4257  srtp_crypto_policy_set_aes_cm_128_null_auth(&policy->rtp);
4258  srtp_crypto_policy_set_aes_cm_128_null_auth(&policy->rtcp);
4259 
4261  switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_128_NULL_AUTH");
4262  }
4263  break;
4264  default:
4265  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Missing crypto type!\n");
4266  break;
4267  }
4268 
4269  /* Setup the policy with MKI if they are used. Number of key materials must be positive to use MKI. */
4270  if (key_material && (*key_material_n > 0)) {
4271 
4272  if (direction == SWITCH_RTP_CRYPTO_RECV) {
4273  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_MKI] = 1; /* tell the rest of the environment MKI is used */
4274  } else {
4275  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_MKI] = 1; /* tell the rest of the environment MKI is used */
4276  }
4277 
4278  /* key must be NULL for libsrtp to work correctly with MKI. */
4279  policy->key = NULL;
4280 
4281  /* Allocate array for MKIs. */
4282  mkis = switch_core_alloc(rtp_session->pool, *key_material_n * sizeof(srtp_master_key_t*));
4283  if (!mkis) {
4284  return SWITCH_STATUS_FALSE;
4285  }
4286 
4287  /* Build array of MKIs. */
4288  mki_idx = 0;
4289 
4290  while (key_material && (mki_idx < *key_material_n)) {
4291 
4292  if (key_material->mki_size < 1) {
4293  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "MKI bad key size at MKI %d\n", mki_idx);
4294  return SWITCH_STATUS_FALSE;
4295  }
4296 
4297  mki = switch_core_alloc(rtp_session->pool, sizeof(srtp_master_key_t));
4298  if (!mki) {
4299  return SWITCH_STATUS_FALSE;
4300  }
4301 
4302  /* Setup MKI. */
4303  mki->mki_id = switch_core_alloc(rtp_session->pool, sizeof(key_material->mki_size));
4304  if (!mki->mki_id) {
4305  return SWITCH_STATUS_FALSE;
4306  }
4307 
4308  mki->key = switch_core_alloc(rtp_session->pool, keysalt_len);
4309  if (!mki->key) {
4310  return SWITCH_STATUS_FALSE;
4311  }
4312 
4313  memcpy(mki->mki_id, &key_material->mki_id, key_material->mki_size);
4314  mki->mki_size = key_material->mki_size;
4315  memcpy(mki->key, key_material->raw_key, keysalt_len);
4316 
4317  mkis[mki_idx] = mki;
4318 
4319  key_material = key_material->next;
4320  ++mki_idx;
4321  }
4322 
4323  /* And pass the array of MKIs to libsrtp. */
4324  policy->keys = mkis;
4325  policy->num_master_keys = mki_idx;
4326 
4327  } else {
4328  policy->key = (uint8_t *) crypto_key->keysalt;
4329  }
4330 
4331  policy->next = NULL;
4332 
4333  policy->window_size = 1024;
4334  policy->allow_repeat_tx = 1;
4335 
4336  //policy->rtp.sec_serv = sec_serv_conf_and_auth;
4337  //policy->rtcp.sec_serv = sec_serv_conf_and_auth;
4338 
4339  switch (direction) {
4341  policy->ssrc.type = ssrc_any_inbound;
4342 
4343  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && idx == 0 && rtp_session->recv_ctx[idx]) {
4344  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_RESET] = 1;
4345  } else {
4346  if ((stat = srtp_create(&rtp_session->recv_ctx[idx], policy)) || !rtp_session->recv_ctx[idx]) {
4347  status = SWITCH_STATUS_FALSE;
4348  }
4349 
4350  if (status == SWITCH_STATUS_SUCCESS) {
4351  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "Activating %s Secure %s RECV%s\n",
4352  rtp_type(rtp_session), idx ? "RTCP" : "RTP", rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_MKI] ? " (with MKI)" : "");
4353  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] = 1;
4354  } else {
4355  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error allocating srtp [%d]\n", stat);
4356  return status;
4357  }
4358  }
4359  break;
4361  policy->ssrc.type = ssrc_any_outbound;
4362  //policy->ssrc.type = ssrc_specific;
4363  //policy->ssrc.value = rtp_session->ssrc;
4364 
4365  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND] && idx == 0 && rtp_session->send_ctx[idx]) {
4366  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_RESET] = 1;
4367  } else {
4368  if ((stat = srtp_create(&rtp_session->send_ctx[idx], policy)) || !rtp_session->send_ctx[idx]) {
4369  status = SWITCH_STATUS_FALSE;
4370  }
4371 
4372  if (status == SWITCH_STATUS_SUCCESS) {
4373  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "Activating %s Secure %s SEND%s\n",
4374  rtp_type(rtp_session), idx ? "RTCP" : "RTP", rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_MKI] ? " (with MKI)" : "");
4375  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND] = 1;
4376  } else {
4377  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error allocating SRTP [%d]\n", stat);
4378  return status;
4379  }
4380  }
4381 
4382  break;
4383  default:
4384  abort();
4385  break;
4386  }
4387 
4389  if (rtp_session->dtls) {
4390  switch_event_add_header(fsevent, SWITCH_STACK_BOTTOM, "secure_type", "srtp:dtls:AES_CM_128_HMAC_SHA1_80");
4391  switch_channel_set_variable(channel, "rtp_has_crypto", "srtp:dtls:AES_CM_128_HMAC_SHA1_80");
4392  } else {
4393  switch_event_add_header(fsevent, SWITCH_STACK_BOTTOM, "secure_type", "srtp:sdes:%s", switch_channel_get_variable(channel, "rtp_has_crypto"));
4394  }
4395  switch_event_add_header_string(fsevent, SWITCH_STACK_BOTTOM, "caller-unique-id", switch_channel_get_uuid(channel));
4396  switch_event_fire(&fsevent);
4397  }
4398 
4399 
4400  return SWITCH_STATUS_SUCCESS;
4401 #endif
4402 }
#define switch_event_fire(event)
Fire an event filling in most of the arguements with obvious values.
Definition: switch_event.h:413
unsigned char remote_raw_key[SWITCH_RTP_MAX_CRYPTO_LEN]
static switch_bool_t switch_true(const char *expr)
Evaluate the truthfullness of a string expression.
Definition: switch_utils.h:519
#define SWITCH_CHANNEL_SESSION_LOG(x)
unsigned char keysalt[SWITCH_RTP_MAX_CRYPTO_LEN]
Definition: switch_rtp.h:81
int srtp_idx_rtcp
Definition: switch_rtp.c:368
switch_status_t switch_event_add_header(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *fmt,...) PRINTF_FUNCTION(4
Add a header to an event.
Representation of an event.
Definition: switch_event.h:80
srtp_policy_t send_policy[2]
Definition: switch_rtp.c:360
switch_rtp_crypto_key_t * crypto_keys[SWITCH_RTP_CRYPTO_MAX]
Definition: switch_rtp.c:446
struct switch_crypto_key_material_s * local_key_material_next
struct switch_crypto_key_material_s * next
#define rtp_type(rtp_session)
Definition: switch_rtp.c:544
int switch_core_media_crypto_keysalt_len(switch_rtp_crypto_key_type_t type)
_Ret_ switch_channel_t * switch_core_session_get_channel(_In_ switch_core_session_t *session)
Retrieve a pointer to the channel object associated with a given session.
srtp_ctx_t * send_ctx[2]
Definition: switch_rtp.c:357
unsigned char local_raw_key[SWITCH_RTP_MAX_CRYPTO_LEN]
switch_status_t switch_b64_encode(unsigned char *in, switch_size_t ilen, unsigned char *out, switch_size_t olen)
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:396
unsigned long local_key_material_n
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:684
#define switch_channel_get_variable(_c, _v)
int index
Definition: switch_cJSON.h:160
srtp_ctx_t * recv_ctx[2]
Definition: switch_rtp.c:358
switch_status_t switch_event_add_header_string(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *data)
Add a string header to an event.
#define SWITCH_RTP_MAX_CRYPTO_LEN
switch_rtp_crypto_key_type_t type
Definition: switch_rtp.h:80
uintptr_t switch_size_t
switch_dtls_t * dtls
Definition: switch_rtp.c:370
char * switch_core_get_variable(_In_z_ const char *varname)
Retrieve a global variable from the core.
unsigned long remote_key_material_n
char * switch_channel_get_uuid(switch_channel_t *channel)
Retrieve the given channel&#39;s unique id.
switch_memory_pool_t * pool
Definition: switch_rtp.c:397
switch_status_t
Common return values.
srtp_policy_t recv_policy[2]
Definition: switch_rtp.c:361
switch_core_session_t * session
Definition: switch_rtp.c:476
#define switch_event_create(event, id)
Create a new event assuming it will not be custom event and therefore hiding the unused parameters...
Definition: switch_event.h:384
switch_call_direction_t switch_channel_direction(switch_channel_t *channel)
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
unsigned char raw_key[SWITCH_RTP_MAX_CRYPTO_LEN]
#define switch_channel_set_variable(_channel, _var, _val)
switch_rtp_crypto_key_type_t crypto_type
struct switch_rtp_crypto_key * next
Definition: switch_rtp.h:83
memset(buf, 0, buflen)
struct switch_crypto_key_material_s * remote_key_material_next

◆ switch_rtp_change_ice_dest()

static void switch_rtp_change_ice_dest ( switch_rtp_t rtp_session,
switch_rtp_ice_t ice,
const char *  host,
switch_port_t  port 
)
static

Definition at line 547 of file switch_rtp.c.

References switch_rtp_ice_t::addr, ice_s::cand_idx, ice_s::cands, ice_s::chosen, icand_s::con_addr, icand_s::con_port, switch_rtp::flags, switch_rtp_ice_t::ice_params, switch_rtp_ice_t::missed_count, switch_rtp::pool, switch_rtp_ice_t::proto, switch_rtp::remote_addr, switch_rtp::rtcp_ice, switch_rtp::rtcp_remote_addr, switch_rtp::session, SWITCH_CHANNEL_SESSION_LOG, switch_core_strdup, SWITCH_FALSE, SWITCH_LOG_DEBUG5, switch_log_printf(), SWITCH_RTP_FLAG_RTCP_MUX, and switch_rtp_set_remote_address().

Referenced by handle_ice().

548 {
549  int is_rtcp = ice == &rtp_session->rtcp_ice;
550  const char *err = "";
551  int i;
552  uint8_t ice_cand_found_idx = 0;
553 
554  for (i = 0; i < ice->ice_params->cand_idx[ice->proto]; i++) {
555  if (!strcmp(host, ice->ice_params->cands[i][ice->proto].con_addr) && port == ice->ice_params->cands[i][ice->proto].con_port) {
556  ice_cand_found_idx = i;
557  }
558  }
559 
560  if (!ice_cand_found_idx) {
561  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG5, "ICE candidate [%s:%d] replaced with [%s:%d]\n",
562  ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_addr, ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port, host, port);
563  ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_addr = switch_core_strdup(rtp_session->pool, host);
564  ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port = port;
565  } else {
566  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG5, "ICE chosen candidate [%s:%d] set to idx [%d]\n", host, port, ice_cand_found_idx);
567  ice->ice_params->chosen[ice->proto] = ice_cand_found_idx;
568  }
569 
570  ice->missed_count = 0;
571 
572  if (is_rtcp) {
573  ice->addr = rtp_session->rtcp_remote_addr;
574  } else {
575  switch_rtp_set_remote_address(rtp_session, host, port, 0, SWITCH_FALSE, &err);
576 
577  if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
578  ice->addr = rtp_session->remote_addr;
579  }
580  }
581 
582 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
switch_sockaddr_t * remote_addr
Definition: switch_rtp.c:344
switch_sockaddr_t * addr
Definition: switch_rtp.c:252
#define switch_core_strdup(_pool, _todup)
Copy a string using memory allocation from a given pool.
Definition: switch_core.h:733
ice_t * ice_params
Definition: switch_rtp.c:256
icand_t cands[MAX_CAND][MAX_CAND_IDX_COUNT]
Definition: switch_rtp.h:114
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:396
char * con_addr
Definition: switch_rtp.h:99
switch_rtp_ice_t rtcp_ice
Definition: switch_rtp.c:402
switch_status_t switch_rtp_set_remote_address(switch_rtp_t *rtp_session, const char *host, switch_port_t port, switch_port_t remote_rtcp_port, switch_bool_t change_adv_addr, const char **err)
Assign a remote address to the RTP session.
Definition: switch_rtp.c:3128
int cand_idx[MAX_CAND_IDX_COUNT]
Definition: switch_rtp.h:115
switch_memory_pool_t * pool
Definition: switch_rtp.c:397
ice_proto_t proto
Definition: switch_rtp.c:257
switch_core_session_t * session
Definition: switch_rtp.c:476
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
int chosen[MAX_CAND_IDX_COUNT]
Definition: switch_rtp.h:116
switch_sockaddr_t * rtcp_remote_addr
Definition: switch_rtp.c:344
switch_port_t con_port
Definition: switch_rtp.h:100

◆ switch_rtp_dtls_destroy()

static void switch_rtp_dtls_destroy ( void  )
static

Definition at line 3687 of file switch_rtp.c.

Referenced by switch_rtp_shutdown().

3687  {
3688 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
3690  BIO_meth_free(dtls_bio_filter_methods);
3691  dtls_bio_filter_methods = NULL;
3692  }
3693 #endif
3694 }
static BIO_METHOD dtls_bio_filter_methods
Definition: switch_rtp.c:3470

◆ switch_rtp_dtls_init()

static void switch_rtp_dtls_init ( void  )
static

Definition at line 3677 of file switch_rtp.c.

References dtls_bio_filter_ctrl(), dtls_bio_filter_free(), dtls_bio_filter_new(), and dtls_bio_filter_write().

Referenced by switch_rtp_init().

3677  {
3678 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
3679  dtls_bio_filter_methods = BIO_meth_new(BIO_TYPE_FILTER | BIO_get_new_index(), "DTLS filter");
3680  BIO_meth_set_write(dtls_bio_filter_methods, dtls_bio_filter_write);
3681  BIO_meth_set_ctrl(dtls_bio_filter_methods, dtls_bio_filter_ctrl);
3682  BIO_meth_set_create(dtls_bio_filter_methods, dtls_bio_filter_new);
3683  BIO_meth_set_destroy(dtls_bio_filter_methods, dtls_bio_filter_free);
3684 #endif
3685 }
static int dtls_bio_filter_free(BIO *bio)
Definition: switch_rtp.c:3526
static long dtls_bio_filter_ctrl(BIO *bio, int cmd, long num, void *ptr)
Definition: switch_rtp.c:3608
static BIO_METHOD dtls_bio_filter_methods
Definition: switch_rtp.c:3470
static int dtls_bio_filter_new(BIO *bio)
Definition: switch_rtp.c:3500
static int dtls_bio_filter_write(BIO *bio, const char *in, int inl)
Definition: switch_rtp.c:3559

◆ switch_rtp_reset_vb()

void switch_rtp_reset_vb ( switch_rtp_t rtp_session)

Definition at line 2991 of file switch_rtp.c.

References switch_jb_reset(), switch_rtp::vb, and switch_rtp::vbw.

Referenced by switch_rtp_reset().

2992 {
2993 
2994  if (rtp_session->vb) {
2995  switch_jb_reset(rtp_session->vb);
2996  }
2997 
2998  if (rtp_session->vbw) {
2999  switch_jb_reset(rtp_session->vbw);
3000  }
3001 }
switch_jb_t * vbw
Definition: switch_rtp.c:440
switch_jb_t * vb
Definition: switch_rtp.c:439
void switch_jb_reset(switch_jb_t *jb)

◆ switch_send_rtcp_event()

static void switch_send_rtcp_event ( switch_rtp_t rtp_session,
struct switch_rtcp_sender_report sr,
struct switch_rtcp_report_block rtcp_report_block 
)
static

Definition at line 2063 of file switch_rtp.c.

References switch_rtcp_report_block::dlsr, switch_rtcp_report_block::fraction, switch_rtcp_report_block::highest_sequence_number_received, switch_rtcp_report_block::jitter, switch_rtcp_report_block::lost, switch_rtcp_report_block::lsr, switch_rtcp_sender_info::ntp_lsw, switch_rtcp_sender_info::ntp_msw, switch_rtcp_sender_info::oc, switch_rtcp_sender_info::pc, switch_rtcp_numbers_t::peer_ssrc, switch_rtp_stats_t::rtcp, switch_rtcp_sender_report::sender_info, switch_rtp::session, switch_rtcp_numbers_t::ssrc, switch_rtp::stats, SWITCH_CHANNEL_SESSION_LOG, switch_core_session_get_uuid(), switch_event_add_header_string(), switch_event_create, switch_event_fire, SWITCH_EVENT_SEND_RTCP_MESSAGE, SWITCH_LOG_DEBUG10, switch_log_printf(), SWITCH_STACK_BOTTOM, SWITCH_STATUS_SUCCESS, switch_time_now(), SWITCH_TIME_T_FMT, switch_rtcp_sender_info::ts, and value.

Referenced by check_rtcp_and_ice().

2064 {
2065  if (sr && rtcp_report_block) {
2066  switch_event_t *event;
2067 
2069  char value[30];
2070  char header[50];
2071  uint32_t tmpLost;
2072  char *uuid = switch_core_session_get_uuid(rtp_session->session);
2073  if (uuid) {
2075  }
2076 
2077  snprintf(value, sizeof(value), "%.8x", rtp_session->stats.rtcp.ssrc);
2078  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "SSRC", value);
2079 
2080  snprintf(value, sizeof(value), "%u", ntohl(sr->sender_info.ntp_msw));
2081  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "NTP-Most-Significant-Word", value);
2082 
2083  snprintf(value, sizeof(value), "%u", ntohl(sr->sender_info.ntp_lsw));
2084  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "NTP-Least-Significant-Word", value);
2085 
2086  snprintf(value, sizeof(value), "%u", ntohl(sr->sender_info.ts));
2087  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "RTP-Timestamp", value);
2088 
2089  snprintf(value, sizeof(value), "%u", ntohl(sr->sender_info.pc));
2090  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Sender-Packet-Count", value);
2091 
2092  snprintf(value, sizeof(value), "%u", ntohl(sr->sender_info.oc));
2093  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Octect-Packet-Count", value);
2094 
2095  snprintf(value, sizeof(value), "%u", ntohl(sr->sender_info.ts));
2096  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Last-RTP-Timestamp", value);
2097 
2098  snprintf(value, sizeof(value), "%" SWITCH_TIME_T_FMT, switch_time_now());
2099  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Capture-Time", value);
2100 
2101  /* Add sources info */
2102  snprintf(header, sizeof(header), "Source-SSRC");
2103  snprintf(value, sizeof(value), "%.8x", rtp_session->stats.rtcp.peer_ssrc);
2104  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value);
2105  snprintf(header, sizeof(header), "Source-Fraction");
2106  snprintf(value, sizeof(value), "%u", (uint8_t)rtcp_report_block->fraction);
2107  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value);
2108  snprintf(header, sizeof(header), "Source-Lost");
2109 #if SWITCH_BYTE_ORDER == __BIG_ENDIAN
2110  tmpLost = rtcp_report_block->lost; /* signed 24bit will extended signess to int32_t automatically */
2111 #else
2112  tmpLost = ntohl(rtcp_report_block->lost)>>8;
2113  tmpLost = tmpLost | ((tmpLost & 0x00800000) ? 0xff000000 : 0x00000000); /* ...and signess compensation */
2114 #endif
2115  snprintf(value, sizeof(value), "%u", tmpLost);
2116  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value);
2117  snprintf(header, sizeof(header), "Source-Highest-Sequence-Number-Received");
2118  snprintf(value, sizeof(value), "%u", ntohl(rtcp_report_block->highest_sequence_number_received));
2119  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value);
2120  snprintf(header, sizeof(header), "Source-Jitter");
2121  snprintf(value, sizeof(value), "%u", ntohl(rtcp_report_block->jitter));
2122  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value);
2123  snprintf(header, sizeof(header), "Source-LSR");
2124  snprintf(value, sizeof(value), "%u", ntohl(rtcp_report_block->lsr));
2125  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value);
2126  snprintf(header, sizeof(header), "Source-DLSR");
2127  snprintf(value, sizeof(value), "%u", ntohl(rtcp_report_block->dlsr));
2128  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value);
2129 
2130  switch_event_fire(&event);
2131  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG10, "Dispatched RTCP SEND event\n");
2132  }
2133  }
2134 }
#define switch_event_fire(event)
Fire an event filling in most of the arguements with obvious values.
Definition: switch_event.h:413
#define SWITCH_CHANNEL_SESSION_LOG(x)
Representation of an event.
Definition: switch_event.h:80
const char *const const char *const const cJSON *const value
uint32_t highest_sequence_number_received
Definition: switch_rtp.c:500
switch_rtcp_numbers_t rtcp
Definition: switch_types.h:773
struct switch_rtcp_sender_info sender_info
Definition: switch_rtp.c:525
switch_status_t switch_event_add_header_string(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *data)
Add a string header to an event.
#define SWITCH_TIME_T_FMT
switch_rtp_stats_t stats
Definition: switch_rtp.c:454
char * switch_core_session_get_uuid(_In_ switch_core_session_t *session)
Retrieve the unique identifier from a session.
switch_core_session_t * session
Definition: switch_rtp.c:476
#define switch_event_create(event, id)
Create a new event assuming it will not be custom event and therefore hiding the unused parameters...
Definition: switch_event.h:384
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_time_t switch_time_now(void)
Definition: switch_apr.c:325

◆ using_ice()

static int using_ice ( switch_rtp_t rtp_session)
static

Definition at line 2054 of file switch_rtp.c.

References switch_rtp::ice, switch_rtp_ice_t::ice_user, and switch_rtp::rtcp_ice.

Referenced by check_rtcp_and_ice(), and rtp_common_read().

2055 {
2056  if (rtp_session->ice.ice_user || rtp_session->rtcp_ice.ice_user) {
2057  return 1;
2058  }
2059 
2060  return 0;
2061 }
switch_rtp_ice_t rtcp_ice
Definition: switch_rtp.c:402
switch_rtp_ice_t ice
Definition: switch_rtp.c:401

Variable Documentation

◆ alloc_hash

switch_hash_t* alloc_hash = NULL
static

Definition at line 109 of file switch_rtp.c.

◆ dtls_bio_filter_methods

static BIO_METHOD dtls_bio_filter_methods
static
Initial value:
= {
BIO_TYPE_FILTER,
"DTLS filter",
NULL,
NULL,
NULL,
NULL
}
static int dtls_bio_filter_free(BIO *bio)
Definition: switch_rtp.c:3526
static long dtls_bio_filter_ctrl(BIO *bio, int cmd, long num, void *ptr)
Definition: switch_rtp.c:3608
static int dtls_bio_filter_new(BIO *bio)
Definition: switch_rtp.c:3500
static int dtls_bio_filter_write(BIO *bio, const char *in, int inl)
Definition: switch_rtp.c:3559

Definition at line 3470 of file switch_rtp.c.

◆ dtls_state_names_t

const char* dtls_state_names_t[] = {"OFF", "HANDSHAKE", "SETUP", "READY", "FAIL", "INVALID"}
static

Definition at line 3202 of file switch_rtp.c.

◆ dtls_states

Definition at line 308 of file switch_rtp.c.

◆ END_PORT

switch_port_t END_PORT = RTP_END_PORT
static

Definition at line 93 of file switch_rtp.c.

Referenced by switch_rtp_request_port(), and switch_rtp_set_end_port().

◆ global_init

int global_init = 0
static

Definition at line 818 of file switch_rtp.c.

◆ INVALID_PT

const switch_payload_t INVALID_PT = 255
static

◆ port_lock

switch_mutex_t* port_lock = NULL
static

◆ START_PORT

switch_port_t START_PORT = RTP_START_PORT
static

Definition at line 92 of file switch_rtp.c.

Referenced by switch_rtp_request_port(), and switch_rtp_set_start_port().