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  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 821 of file switch_rtp.c.

Referenced by calc_elapsed().

◆ cr_keylen

#define cr_keylen   16

Definition at line 3211 of file switch_rtp.c.

Referenced by dtls_state_setup().

◆ cr_kslen

#define cr_kslen   30

Definition at line 3213 of file switch_rtp.c.

Referenced by dtls_state_setup().

◆ cr_saltlen

#define cr_saltlen   14

Definition at line 3212 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 269 of file switch_rtp.c.

Referenced by do_dtls(), and switch_rtp_add_dtls().

◆ MAX_NACK

#define MAX_NACK   10

Definition at line 2132 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 819 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 5845 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 820 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 296 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 530 of file switch_rtp.c.

◆ vad_talk_mask_t

Enumerator
VAD_FIRE_TALK 
VAD_FIRE_NOT_TALK 

Definition at line 189 of file switch_rtp.c.

189  {
190  VAD_FIRE_TALK = (1 << 0),
191  VAD_FIRE_NOT_TALK = (1 << 1)
vad_talk_mask_t
Definition: switch_rtp.c:189

Function Documentation

◆ BIO_dtls_filter()

BIO_METHOD* BIO_dtls_filter ( void  )

Definition at line 3472 of file switch_rtp.c.

Referenced by switch_rtp_add_dtls().

3472  {
3473 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3474  return(&dtls_bio_filter_methods);
3475 #else
3476  return(dtls_bio_filter_methods);
3477 #endif
3478 }
static BIO_METHOD dtls_bio_filter_methods
Definition: switch_rtp.c:3467

◆ burstr_calculate()

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

Definition at line 1621 of file switch_rtp.c.

References switch_rtcp_report_block_frame::lost, and LOST_BURST_ANALYZE.

Referenced by check_jitter().

1622 {
1623  int lost = 0;
1624  int bursts = 0;
1625  int i;
1626 
1627  for ( i = 0; i < LOST_BURST_ANALYZE; i++ ) {
1628  lost += i * loss[i];
1629  bursts += loss[i];
1630  }
1631  if (received > 0 && bursts > 0) {
1632  *burstr = (double)((double)lost / (double)bursts) / (double)(1.0 / ( 1.0 - (double)lost / (double)received ));
1633  if (*burstr < 0) {
1634  *burstr = - *burstr;
1635  }
1636  } else {
1637  *burstr = 0;
1638  }
1639  if (received > 0) {
1640  *lossr = (double)((double)lost / (double)received);
1641  } else {
1642  *lossr = 0;
1643  }
1644 }
#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 2024 of file switch_rtp.c.

References rtcp_tmmbx_t::parts, and switch_assert.

Referenced by check_rtcp_and_ice().

2025 {
2026  uint32_t mantissa_max, i = 0;
2027  uint8_t exp = 0;
2028  uint32_t mantissa = 0;
2029  uint16_t overhead = 60;
2030 
2031  switch_assert(bits<=32);
2032 
2033  mantissa_max = (1 << bits) - 1;
2034 
2035  for (i = 0; i < 32; ++i) {
2036  if (bps <= (mantissa_max << i)) {
2037  exp = i;
2038  break;
2039  }
2040  }
2041 
2042  mantissa = (bps >> exp);
2043 
2044  tmmbx->parts[0] = (uint8_t) ((exp << 2) + ((mantissa >> 15) & 0x03));
2045  tmmbx->parts[1] = (uint8_t) (mantissa >> 7);
2046  tmmbx->parts[2] = (uint8_t) ((mantissa >> 1) + ((overhead >> 8) & 0x01));
2047  tmmbx->parts[3] = (uint8_t) (overhead);
2048 }
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 823 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().

824 {
825  switch_time_t ref_point;
826  switch_time_t now;
827 
828  now = switch_micro_time_now();
829 
830  if (ice->last_ok && (!rtp_session->dtls || rtp_session->dtls->state == DS_READY)) {
831  ref_point = ice->last_ok;
832  } else {
833  ref_point = rtp_session->first_stun;
834  }
835 
836  if (!ref_point) ref_point = now;
837 
838  rtp_session->elapsed_stun = (unsigned int) ((now - ref_point) / 1000);
839 
840  if (rtp_session->last_media) {
841  rtp_session->elapsed_media = (unsigned int) ((now - rtp_session->last_media) / 1000);
842  } else {
843  rtp_session->elapsed_media = MEDIA_TOO_LONG + 1;
844  }
845 
846  if (rtp_session->last_adj) {
847  rtp_session->elapsed_adj = (unsigned int) ((now - rtp_session->last_adj) / 1000);
848  } else {
849  rtp_session->elapsed_adj = ADJ_TOO_LONG + 1;
850  }
851 }
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:481
switch_time_t last_ok
Definition: switch_rtp.c:260
uint32_t elapsed_stun
Definition: switch_rtp.c:479
#define ADJ_TOO_LONG
Definition: switch_rtp.c:821
switch_time_t last_media
Definition: switch_rtp.c:439
dtls_state_t state
Definition: switch_rtp.c:280
int64_t switch_time_t
Definition: switch_apr.h:188
uint32_t elapsed_media
Definition: switch_rtp.c:480
switch_dtls_t * dtls
Definition: switch_rtp.c:366
switch_time_t last_adj
Definition: switch_rtp.c:477
#define MEDIA_TOO_LONG
Definition: switch_rtp.c:819
switch_time_t first_stun
Definition: switch_rtp.c:403

◆ calc_local_lsr_now()

static uint32_t calc_local_lsr_now ( void  )
inlinestatic

Definition at line 1819 of file switch_rtp.c.

References NTP_TIME_OFFSET, and switch_micro_time_now().

Referenced by process_rtcp_report(), and rtcp_generate_report_block().

1820 {
1821  switch_time_t now;
1822  uint32_t ntp_sec, ntp_usec, lsr_now, sec;
1823  now = switch_micro_time_now();
1824  sec = (uint32_t)(now/1000000); /* convert to seconds */
1825  ntp_sec = sec+NTP_TIME_OFFSET; /* convert to NTP seconds */
1826  ntp_usec = (uint32_t)(now - ((switch_time_t) sec*1000000)); /* remove seconds to keep only the microseconds */
1827 
1828  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) */
1829 
1830  return lsr_now;
1831 }
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 1655 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().

1656 {
1657  switch_time_t current_time;
1658  int64_t diff_time = 0, cur_diff = 0;
1659  int seq;
1660 
1661  current_time = switch_micro_time_now() / 1000;
1662 
1663  if (rtp_session->flags[SWITCH_RTP_FLAG_PAUSE] || rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] || rtp_session->dtmf_data.in_digit_ts) {
1664  reset_jitter_seq(rtp_session);
1665  return;
1666  }
1667 
1668  if (++rtp_session->jitter_lead < JITTER_LEAD_FRAMES || !rtp_session->stats.inbound.last_proc_time) {
1669  rtp_session->stats.inbound.last_proc_time = current_time;
1670  return;
1671  }
1672 
1673  diff_time = (current_time - rtp_session->stats.inbound.last_proc_time);
1674  seq = (int)(uint16_t) ntohs((uint16_t) rtp_session->last_rtp_hdr.seq);
1675 
1676  /* Burst and Packet Loss */
1677  rtp_session->stats.inbound.recved++;
1678 
1679  if (rtp_session->stats.inbound.last_processed_seq > 0 && seq > (int)(rtp_session->stats.inbound.last_processed_seq + 1)) {
1680  int lost = (seq - rtp_session->stats.inbound.last_processed_seq - 1);
1681 
1682  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "%s Got: %s seq %d but expected: %d lost: %d\n",
1683  rtp_session_name(rtp_session),
1684  rtp_type(rtp_session),
1685  seq,
1686  (rtp_session->stats.inbound.last_processed_seq + 1), lost);
1687  rtp_session->stats.inbound.last_loss++;
1688 
1689  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
1691  }
1692 
1693  if (rtp_session->stats.inbound.last_loss > 0 && rtp_session->stats.inbound.last_loss < LOST_BURST_CAPTURE) {
1694  rtp_session->stats.inbound.loss[rtp_session->stats.inbound.last_loss] += lost;
1695  }
1696 
1697  rtp_session->bad_stream++;
1698  rtp_session->stats.inbound.flaws += lost;
1699 
1700  if (rtp_session->stats.inbound.error_log) {
1701  rtp_session->stats.inbound.error_log->flaws += lost;
1702  }
1703 
1704  } else {
1705  rtp_session->stats.inbound.last_loss = 0;
1706  }
1707 
1708  rtp_session->stats.inbound.last_processed_seq = seq;
1709 
1710  /* Burst and Packet Loss */
1711 
1712  if (current_time > rtp_session->next_stat_check_time) {
1713  rtp_session->next_stat_check_time = current_time + 5000;
1714  burstr_calculate(rtp_session->stats.inbound.loss, rtp_session->stats.inbound.recved,
1715  &(rtp_session->stats.inbound.burstrate), &(rtp_session->stats.inbound.lossrate));
1716  do_mos(rtp_session);
1717  } else {
1718  do_mos(rtp_session);
1719  }
1720 
1721  if (rtp_session->stats.inbound.last_loss || rtp_session->bad_stream) {
1722  if (rtp_session->session && (!rtp_session->stats.inbound.error_log || rtp_session->stats.inbound.error_log->stop)) {
1723  struct error_period *error = switch_core_session_alloc(rtp_session->session, sizeof(*error));
1724  error->start = switch_micro_time_now();
1725  error->next = rtp_session->stats.inbound.error_log;
1726  rtp_session->stats.inbound.error_log = error;
1727  }
1728 
1729  if (!rtp_session->stats.inbound.last_loss) {
1730  if (++rtp_session->recovering_stream > (rtp_session->one_second * 3)) {
1731  if (rtp_session->session && rtp_session->stats.inbound.error_log) {
1732  rtp_session->stats.inbound.error_log->stop = switch_micro_time_now();
1733  }
1734 
1735  rtp_session->bad_stream = 0;
1736  }
1737  } else {
1738  rtp_session->recovering_stream = 0;
1739  rtp_session->bad_stream++;
1740  }
1741  } else {
1742  rtp_session->recovering_stream = 0;
1743  rtp_session->clean_stream++;
1744  }
1745 
1746 
1747  if ( diff_time < 0 ) {
1748  diff_time = -diff_time;
1749  }
1750 
1751  rtp_session->stats.inbound.jitter_n++;
1752  rtp_session->stats.inbound.jitter_add += diff_time;
1753 
1754  if (rtp_session->stats.inbound.mean_interval) {
1755  cur_diff = (int64_t)(diff_time - rtp_session->stats.inbound.mean_interval);
1756  } else {
1757  cur_diff = 0;
1758  }
1759 
1760  rtp_session->stats.inbound.jitter_addsq += (cur_diff * cur_diff);
1761  rtp_session->stats.inbound.last_proc_time = current_time;
1762 
1763  if (rtp_session->stats.inbound.jitter_n > 0) {
1764  double ipdv;
1765 
1766  rtp_session->stats.inbound.mean_interval = (double)rtp_session->stats.inbound.jitter_add / (double)rtp_session->stats.inbound.jitter_n;
1767 
1768  if (!rtp_session->old_mean) {
1769  rtp_session->old_mean = rtp_session->stats.inbound.mean_interval;
1770  }
1771 
1772  rtp_session->stats.inbound.variance = (double)rtp_session->stats.inbound.jitter_addsq / (double)rtp_session->stats.inbound.jitter_n;
1773 
1774  //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);
1775 
1776  ipdv = rtp_session->old_mean - rtp_session->stats.inbound.mean_interval;
1777 
1778  if ( ipdv > IPDV_THRESHOLD ) { /* It shows Increasing Delays */
1779  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG3, "Calculated Instantaneous Packet Delay Variation: %s packet %lf\n",
1780  rtp_type(rtp_session), ipdv);
1781  }
1782 
1783  if ( rtp_session->stats.inbound.variance < rtp_session->stats.inbound.min_variance || rtp_session->stats.inbound.min_variance == 0 ) {
1784  rtp_session->stats.inbound.min_variance = rtp_session->stats.inbound.variance;
1785  }
1786 
1787  if ( rtp_session->stats.inbound.variance > rtp_session->stats.inbound.max_variance ) {
1788  rtp_session->stats.inbound.max_variance = rtp_session->stats.inbound.variance;
1789  }
1790 
1791  rtp_session->old_mean = rtp_session->stats.inbound.mean_interval;
1792  }
1793 }
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:712
int loss[LOST_BURST_CAPTURE]
Definition: switch_types.h:708
#define SWITCH_CHANNEL_SESSION_LOG(x)
switch_rtp_numbers_t inbound
Definition: switch_types.h:760
#define SWITCH_CHANNEL_LOG
#define IPDV_THRESHOLD
Definition: switch_types.h:250
struct error_period * next
Definition: switch_types.h:677
#define rtp_session_name(_rtp_session)
Definition: switch_rtp.c:85
uint32_t flaws
Definition: switch_types.h:675
struct switch_rtp_rfc2833_data dtmf_data
Definition: switch_rtp.c:422
#define rtp_type(rtp_session)
Definition: switch_rtp.c:540
#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:674
#define JITTER_LEAD_FRAMES
Definition: switch_rtp.c:66
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:392
#define switch_core_session_request_video_refresh(_s)
Definition: switch_core.h:2881
uint32_t clean_stream
Definition: switch_rtp.c:452
uint32_t recovering_stream
Definition: switch_rtp.c:454
int64_t start
Definition: switch_types.h:673
switch_rtp_stats_t stats
Definition: switch_rtp.c:450
struct error_period * error_log
Definition: switch_types.h:716
switch_time_t next_stat_check_time
Definition: switch_rtp.c:415
static void reset_jitter_seq(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:1646
switch_core_session_t * session
Definition: switch_rtp.c:472
void burstr_calculate(int loss[], int received, double *burstr, double *lossr)
Definition: switch_rtp.c:1621
#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:413
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:414
uint32_t bad_stream
Definition: switch_rtp.c:453
rtp_hdr_t last_rtp_hdr
Definition: switch_rtp.c:369
static void do_mos(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:1571
uint32_t one_second
Definition: switch_rtp.c:411

◆ check_recv_payload()

static int check_recv_payload ( switch_rtp_t rtp_session)
static

Definition at line 5799 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().

5800 {
5801  int ok = 1;
5802 
5803  if (!(rtp_session->rtp_bugs & RTP_BUG_ACCEPT_ANY_PAYLOAD) && rtp_session->pmaps && *rtp_session->pmaps) {
5804  payload_map_t *pmap;
5805  ok = 0;
5806 
5807  switch_mutex_lock(rtp_session->flag_mutex);
5808 
5809  for (pmap = *rtp_session->pmaps; pmap && pmap->allocated; pmap = pmap->next) {
5810  if (!pmap->negotiated) {
5811  continue;
5812  }
5813 
5814  if (rtp_session->last_rtp_hdr.pt == pmap->pt) {
5815  ok = 1;
5816  }
5817  }
5818  switch_mutex_unlock(rtp_session->flag_mutex);
5819  }
5820 
5821  return ok;
5822 }
uint8_t allocated
switch_mutex_t * flag_mutex
Definition: switch_rtp.c:426
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:473
switch_rtp_bug_flag_t rtp_bugs
Definition: switch_rtp.c:449
rtp_hdr_t last_rtp_hdr
Definition: switch_rtp.c:369

◆ check_rtcp_and_ice()

static int check_rtcp_and_ice ( switch_rtp_t rtp_session)
static

Definition at line 2133 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, 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, switch_rtcp_report_block_frame::ssrc, rtcp_fir_t::ssrc, rtcp_tmmbx_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().

2134 {
2135  int ret = 0;
2136  int rtcp_ok = 0, rtcp_cyclic = 0, rtcp_fb = 0, force_send_rr = 0;
2138  int rate = 0, nack_ttl = 0, nack_dup = 0;
2139  uint32_t cur_nack[MAX_NACK] = { 0 };
2140  uint16_t seq = 0;
2141 
2142  if (!rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] &&
2143  rtp_session->flags[SWITCH_RTP_FLAG_AUTO_CNG] &&
2144  rtp_session->send_msg.header.ts &&
2145  rtp_session->cng_pt != INVALID_PT &&
2146  (rtp_session->write_timer.samplecount - rtp_session->last_write_samplecount >= rtp_session->samples_per_interval * 60)) {
2147  uint8_t data[10] = { 0 };
2148  switch_frame_flag_t frame_flags = SFF_NONE;
2149  data[0] = 65;
2150  rtp_session->cn++;
2151 
2152  get_next_write_ts(rtp_session, 0);
2153  rtp_session->send_msg.header.ts = htonl(rtp_session->ts);
2154 
2155  switch_rtp_write_manual(rtp_session, (void *) data, 2, 0, rtp_session->cng_pt, ntohl(rtp_session->send_msg.header.ts), &frame_flags);
2156 
2157  if (switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER)) {
2158  rtp_session->last_write_samplecount = rtp_session->write_timer.samplecount;
2159  }
2160  }
2161 
2162  rate = rtp_session->rtcp_interval;
2163 
2164  if (rtp_session->flags[SWITCH_RTP_FLAG_NACK] && rtp_session->vb) {
2165  int n;
2166  for (n = 0; n < MAX_NACK; n++) {
2167  uint32_t nack = switch_jb_pop_nack(rtp_session->vb);
2168 
2169  if (!nack) break;
2170 
2171  seq = ntohs(nack & 0xFFFF);
2172 
2173  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "%s Got NACK [%u][0x%x] for seq %u\n",
2174  switch_core_session_get_name(rtp_session->session), nack, nack, seq);
2175 
2176  cur_nack[nack_ttl++] = nack;
2177  }
2178  if (nack_ttl) {
2179  rtcp_ok = 1;
2180  rtcp_fb = 1;
2181  }
2182  }
2183 
2184 
2185 
2186  if (rtp_session->rtcp_sent_packets < 4) {
2187  rate = 4000;
2188  } else {
2189  if (rtp_session->pli_count || rtp_session->fir_count || rtp_session->tmmbr || rtp_session->tmmbn) {
2190  //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);
2191  rtcp_ok = 1;
2192  rtcp_fb = 1;
2193  }
2194  }
2195 
2196  if (rtp_session->send_rr) {
2197  rtp_session->send_rr = 0;
2198  rtcp_ok = 1;
2199  force_send_rr = 1;
2200  }
2201 
2202  //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);
2203 
2204  if (!rtcp_ok && (!rtp_session->rtcp_last_sent || (int)((now - rtp_session->rtcp_last_sent) / 1000) > rate)) {
2205  //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "TIME UP\n");
2206  rtcp_cyclic = 1;
2207  rtcp_ok = 1;
2208  }
2209 
2210  if (rtcp_ok && using_ice(rtp_session)) {
2211  if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
2212  if (!rtp_session->ice.rready) {
2213  rtcp_ok = 0;
2214  }
2215  } else {
2216  if (!rtp_session->rtcp_ice.rready) {
2217  rtcp_ok = 0;
2218  }
2219  }
2220  }
2221 
2222  //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);
2223 
2224  if (rtp_session->rtcp_sock_output && rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] && !rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU] && rtcp_ok) {
2225  switch_rtcp_numbers_t * stats = &rtp_session->stats.rtcp;
2226  struct switch_rtcp_receiver_report *rr;
2227  struct switch_rtcp_sender_report *sr;
2228  struct switch_rtcp_report_block *rtcp_report_block = NULL;
2229  switch_size_t rtcp_bytes = sizeof(struct switch_rtcp_hdr_s)+sizeof(uint32_t); /* add size of the packet header and the ssrc */
2230  switch_rtcp_hdr_t *sdes;
2231  uint8_t *p;
2232  switch_size_t sdes_bytes = sizeof(struct switch_rtcp_hdr_s);
2233  uint32_t *ssrc;
2235  switch_bool_t is_only_receiver = FALSE;
2236 
2237  if (!rtcp_fb) {
2238  rtp_session->rtcp_last_sent = now;
2239  rtp_session->rtcp_sent_packets++;
2240  }
2241 
2242  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] &&
2243  rtp_session->vb && rtcp_cyclic) {
2244  nack_dup = rtp_session->prev_nacks_inflight;
2245  rtp_session->prev_nacks_inflight = 0;
2246  }
2247 
2248  rtp_session->rtcp_send_msg.header.version = 2;
2249  rtp_session->rtcp_send_msg.header.p = 0;
2250 
2253  is_only_receiver = TRUE;
2254  }
2255  if (!rtp_session->stats.rtcp.sent_pkt_count || is_only_receiver || force_send_rr) {
2256  rtp_session->rtcp_send_msg.header.type = _RTCP_PT_RR; /* Receiver report */
2257  rr=(struct switch_rtcp_receiver_report*) rtp_session->rtcp_send_msg.body;
2258  rr->ssrc = htonl(rtp_session->ssrc);
2259  rtcp_report_block = &rr->report_block;
2260  rtcp_bytes += sizeof(struct switch_rtcp_report_block);
2261  rtcp_generate_report_block(rtp_session, rtcp_report_block, nack_dup);
2262  rtp_session->rtcp_send_msg.header.count = 1; /* reception report block count */
2263  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "Sending RTCP RR (ssrc=%u)\n", rtp_session->ssrc);
2264  } else {
2265  struct switch_rtcp_sender_info *rtcp_sender_info;
2266  rtp_session->rtcp_send_msg.header.type = _RTCP_PT_SR; /* Sender report */
2267  sr = (struct switch_rtcp_sender_report*) rtp_session->rtcp_send_msg.body;
2268  sr->ssrc = htonl(rtp_session->ssrc);
2269  rtcp_sender_info = &sr->sender_info;
2270  rtcp_generate_sender_info(rtp_session, rtcp_sender_info);
2271  rtcp_bytes += sizeof(struct switch_rtcp_sender_info);
2272  if (!rtcp_cyclic && rtcp_fb) {
2273  /* rtcp-fb only, don't send receive report block */
2274  rtp_session->rtcp_send_msg.header.count = 0;
2275  } else {
2276  rtcp_report_block = &sr->report_block;
2277  rtcp_bytes += sizeof(struct switch_rtcp_report_block);
2278  rtcp_generate_report_block(rtp_session, rtcp_report_block, nack_dup);
2279  rtp_session->rtcp_send_msg.header.count = 1; /* reception report block count */
2280  stats->sent_pkt_count = 0;
2281  if ((!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->flags[SWITCH_RTP_FLAG_AUDIO_FIRE_SEND_RTCP_EVENT]) ||
2283  switch_send_rtcp_event(rtp_session, sr, rtcp_report_block);
2284  }
2285  }
2286  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "Sending RTCP SR (ssrc=%u)\n", rtp_session->ssrc);
2287  }
2288 
2289  rtp_session->rtcp_send_msg.header.length = htons((uint16_t)(rtcp_bytes / 4) - 1);
2290 
2291  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
2292  if (rtp_session->pli_count) {
2293  switch_rtcp_ext_hdr_t *ext_hdr;
2294 
2295  p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes;
2296  ext_hdr = (switch_rtcp_ext_hdr_t *) p;
2297 
2298  ext_hdr->version = 2;
2299  ext_hdr->p = 0;
2300  ext_hdr->fmt = _RTCP_PSFB_PLI;
2301  ext_hdr->pt = _RTCP_PT_PSFB;
2302 
2303  ext_hdr->send_ssrc = htonl(rtp_session->ssrc);
2304  ext_hdr->recv_ssrc = htonl(rtp_session->remote_ssrc);
2305  rtp_session->rtcp_vstats.video_in.pli_count++;
2306  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "Sending RTCP PLI %u %u [%u]\n",
2307  rtp_session->ssrc, rtp_session->remote_ssrc, rtp_session->rtcp_vstats.video_in.pli_count);
2308 
2309  ext_hdr->length = htons((uint8_t)(sizeof(switch_rtcp_ext_hdr_t) / 4) - 1);
2310  rtcp_bytes += sizeof(switch_rtcp_ext_hdr_t);
2311  rtp_session->pli_count = 0;
2312  }
2313 
2314  if (rtp_session->flags[SWITCH_RTP_FLAG_NACK] && nack_ttl > 0) {
2315  int n = 0;
2316 
2317  rtp_session->rtcp_vstats.video_in.nack_count++;
2318  for (n = 0; n < nack_ttl; n++) {
2319  switch_rtcp_ext_hdr_t *ext_hdr;
2320  uint32_t *nack;
2321  p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes;
2322  ext_hdr = (switch_rtcp_ext_hdr_t *) p;
2323 
2324  ext_hdr->version = 2;
2325  ext_hdr->p = 0;
2326  ext_hdr->fmt = _RTCP_RTPFB_NACK;
2327  ext_hdr->pt = _RTCP_PT_RTPFB;
2328  ext_hdr->send_ssrc = htonl(rtp_session->ssrc);
2329  ext_hdr->recv_ssrc = htonl(rtp_session->remote_ssrc);
2330  ext_hdr->length = htons(3);
2331  p += sizeof(switch_rtcp_ext_hdr_t);
2332  nack = (uint32_t *) p;
2333  *nack = cur_nack[n];
2334 
2335  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "Sending RTCP NACK %u [%d]\n",
2336  ntohs(*nack & 0xFFFF), rtp_session->rtcp_vstats.video_in.nack_count);
2337 
2338  rtcp_bytes += sizeof(switch_rtcp_ext_hdr_t) + sizeof(cur_nack[n]);
2339  cur_nack[n] = 0;
2340  }
2341  rtp_session->prev_nacks_inflight = n;
2342  }
2343 
2344  if (rtp_session->fir_count) {
2345  switch_rtcp_ext_hdr_t *ext_hdr;
2346  rtcp_fir_t *fir;
2347 
2348  if (switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_OLD_FIR)) {
2349  p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes;
2350  ext_hdr = (switch_rtcp_ext_hdr_t *) p;
2351 
2352  ext_hdr->version = 2;
2353  ext_hdr->pt = _RTCP_PT_FIR;
2354  rtcp_bytes += sizeof(switch_rtcp_ext_hdr_t);
2355  }
2356 
2357 
2358  p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes;
2359  ext_hdr = (switch_rtcp_ext_hdr_t *) p;
2360 
2361  p += sizeof(switch_rtcp_ext_hdr_t);
2362  fir = (rtcp_fir_t *) p;
2363 
2364  ext_hdr->version = 2;
2365  ext_hdr->p = 0;
2366  ext_hdr->fmt = _RTCP_PSFB_FIR;
2367  ext_hdr->pt = _RTCP_PT_PSFB;
2368 
2369  ext_hdr->send_ssrc = htonl(rtp_session->ssrc);
2370  ext_hdr->recv_ssrc = htonl(rtp_session->remote_ssrc);
2371 
2372  fir->ssrc = htonl(rtp_session->remote_ssrc);
2373  fir->seq = rtp_session->fir_seq;
2374  fir->r1 = fir->r2 = fir->r3 = 0;
2375 
2376  rtp_session->rtcp_vstats.video_in.fir_count++;
2377  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);
2378 
2379  rtp_session->fir_seq++;
2380 
2381  ext_hdr->length = htons((uint8_t)((sizeof(switch_rtcp_ext_hdr_t) + sizeof(rtcp_fir_t)) / 4) - 1);
2382  rtcp_bytes += sizeof(switch_rtcp_ext_hdr_t) + sizeof(rtcp_fir_t);
2383  rtp_session->fir_count = 0;
2384  }
2385 
2386  //if (!rtp_session->tmmbr && rtp_session->cur_tmmbr) {
2387  // rtp_session->tmmbr = rtp_session->cur_tmmbr;
2388  //}
2389 
2390  while (rtp_session->tmmbr || rtp_session->tmmbn) {
2391  switch_rtcp_ext_hdr_t *ext_hdr;
2392  rtcp_tmmbx_t *tmmbx;
2393  uint32_t bps = 0;
2394  p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes;
2395  ext_hdr = (switch_rtcp_ext_hdr_t *) p;
2396 
2397  p += sizeof(switch_rtcp_ext_hdr_t);
2398  tmmbx = (rtcp_tmmbx_t *) p;
2399 
2400  ext_hdr->version = 2;
2401  ext_hdr->p = 0;
2402  ext_hdr->pt = _RTCP_PT_RTPFB;
2403  ext_hdr->send_ssrc = htonl(rtp_session->ssrc);
2404  ext_hdr->recv_ssrc = 0;
2405 
2406  if (rtp_session->tmmbr) {
2407  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "Sending RTCP TMMBR %u\n", rtp_session->tmmbr);
2408  ext_hdr->fmt = _RTCP_RTPFB_TMMBR;
2409  bps = rtp_session->tmmbr;
2410  rtp_session->tmmbr = 0;
2411  } else {
2412  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "Sending RTCP TMMBN %u\n", rtp_session->tmmbr);
2413  ext_hdr->fmt = _RTCP_RTPFB_TMMBN;
2414  bps = rtp_session->tmmbn;
2415  rtp_session->tmmbn = 0;
2416  }
2417 
2418  tmmbx->ssrc = htonl(rtp_session->remote_ssrc);
2419  calc_bw_exp(bps, 17, tmmbx);
2420 
2421  ext_hdr->length = htons((uint8_t)((sizeof(switch_rtcp_ext_hdr_t) + sizeof(rtcp_tmmbx_t)) / 4) - 1);
2422  rtcp_bytes += sizeof(switch_rtcp_ext_hdr_t) + sizeof(rtcp_tmmbx_t);
2423  }
2424 
2425  }
2426 
2427  //SDES + CNAME
2428  p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes;
2429  sdes = (switch_rtcp_hdr_t *) p;
2430  sdes->version = 2;
2431  sdes->type = _RTCP_PT_SDES;
2432  sdes->count = 1;
2433  sdes->p = 0;
2434  p = (uint8_t *) (sdes) + sdes_bytes;
2435  ssrc = (uint32_t *) p;
2436  *ssrc = htonl(rtp_session->ssrc);
2437  sdes_bytes += sizeof(uint32_t);
2438 
2439 
2440  p = (uint8_t *) (sdes) + sdes_bytes;
2441  unit = (switch_rtcp_sdes_unit_t *) p;
2442  unit->type = _RTCP_SDES_CNAME;
2443  snprintf((char *)unit->value, 80, "%x", rtp_session->ssrc);
2444  unit->length = strlen((char *)unit->value);
2445  sdes_bytes += sizeof(switch_rtcp_sdes_unit_t) + unit->length;
2446 
2447 
2448  p += sizeof(switch_rtcp_sdes_unit_t) + unit->length;
2449  unit = (switch_rtcp_sdes_unit_t *) p;
2450  unit->type = _RTCP_SDES_NOTE;
2451  snprintf((char *)unit->value, 80, "FreeSWITCH.org -- Come to ClueCon.com");
2452  unit->length = strlen((char *)unit->value);
2453  sdes_bytes += sizeof(switch_rtcp_sdes_unit_t) + unit->length;
2454 
2455  sdes_bytes ++;//END
2456 
2457  sdes_bytes += 4 - (sdes_bytes % 4);
2458 
2459  sdes->length = htons((uint16_t)(sdes_bytes / 4) - 1);
2460  rtcp_bytes += sdes_bytes;
2461 
2462  /* Prepare next report */
2463  if (rtp_session->rtcp_send_msg.header.count) {
2464  stats->last_rpt_cycle = stats->cycle;
2465  stats->last_rpt_ext_seq = stats->high_ext_seq_recv;
2466  stats->last_rpt_ts = rtp_session->write_timer.samplecount;
2467  stats->period_pkt_count = 0;
2468  }
2469 
2470 
2471 
2472 #ifdef ENABLE_SRTP
2473  switch_mutex_lock(rtp_session->ice_mutex);
2474  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND]) {
2475  int stat = 0;
2476  int sbytes = (int) rtcp_bytes;
2477 
2478  if (!rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_MKI]) {
2479  stat = srtp_protect_rtcp(rtp_session->send_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_send_msg.header, &sbytes);
2480  } else {
2481  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);
2482  }
2483 
2484  if (stat) {
2485  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error: SRTP RTCP protection failed with code %d\n", stat);
2486  switch_mutex_unlock(rtp_session->ice_mutex);
2487  goto end;
2488  } else {
2489  rtcp_bytes = sbytes;
2490  }
2491  }
2492  switch_mutex_unlock(rtp_session->ice_mutex);
2493 #endif
2494 
2495  //#define DEBUG_EXTRA
2496 #ifdef DEBUG_EXTRA
2497  {
2498  const char *old_host;
2499  char bufb[50];
2500  old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->rtcp_remote_addr);
2501  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_CRIT, "%s SEND %s RTCP %s:%d %ld\n",
2502  rtp_session_name(rtp_session),
2503  rtp_type(rtp_session),
2504  old_host,
2506  rtcp_bytes);
2507  }
2508 #endif
2509  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) {
2510  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG,"RTCP packet not written\n");
2511  } else {
2512  rtp_session->stats.inbound.period_packet_count = 0;
2513  }
2514  }
2515 
2516  if (rtp_session->ice.ice_user) {
2517  if (ice_out(rtp_session, &rtp_session->ice, SWITCH_FALSE) == SWITCH_STATUS_GENERR) {
2518  ret = -1;
2519  goto end;
2520  }
2521  }
2522 
2523  if (!rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
2524  if (rtp_session->rtcp_ice.ice_user) {
2525  if (ice_out(rtp_session, &rtp_session->rtcp_ice, SWITCH_FALSE) == SWITCH_STATUS_GENERR) {
2526  ret = -1;
2527  goto end;
2528  }
2529  }
2530  }
2531 
2532  end:
2533 
2534  return ret;
2535 }
switch_time_t rtcp_last_sent
Definition: switch_rtp.c:408
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:685
#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:760
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:1835
static void rtcp_generate_sender_info(switch_rtp_t *rtp_session, struct switch_rtcp_sender_info *sr)
Definition: switch_rtp.c:1795
struct switch_rtcp_report_block report_block
Definition: switch_rtp.c:527
switch_bool_t
Definition: switch_types.h:437
uint8_t r1
Definition: switch_rtp.c:123
int srtp_idx_rtcp
Definition: switch_rtp.c:364
uint8_t r3
Definition: switch_rtp.c:125
#define SWITCH_CRYPTO_MKI_INDEX
uint32_t prev_nacks_inflight
Definition: switch_rtp.c:489
uint8_t fir_seq
Definition: switch_rtp.c:332
#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
uint16_t pli_count
Definition: switch_rtp.c:334
#define rtp_type(rtp_session)
Definition: switch_rtp.c:540
switch_payload_t cng_pt
Definition: switch_rtp.c:425
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:353
int64_t switch_time_t
Definition: switch_apr.h:188
switch_rtcp_numbers_t rtcp
Definition: switch_types.h:762
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:2059
uint32_t tmmbn
Definition: switch_rtp.c:337
struct switch_rtcp_sender_info sender_info
Definition: switch_rtp.c:521
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:380
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:392
uint8_t seq
Definition: switch_rtp.c:122
uint16_t fir_count
Definition: switch_rtp.c:333
rtcp_msg_t rtcp_send_msg
Definition: switch_rtp.c:328
uintptr_t switch_size_t
switch_rtp_ice_t rtcp_ice
Definition: switch_rtp.c:398
switch_jb_t * vb
Definition: switch_rtp.c:435
uint8_t r2
Definition: switch_rtp.c:124
uint32_t switch_frame_flag_t
switch_rtp_stats_t stats
Definition: switch_rtp.c:450
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:322
uint8_t cn
Definition: switch_rtp.c:433
int rtcp_interval
Definition: switch_rtp.c:458
uint8_t send_rr
Definition: switch_rtp.c:331
static int using_ice(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:2050
uint32_t samples_per_interval
Definition: switch_rtp.c:405
srtp_hdr_t header
Definition: switch_rtp.c:112
switch_rtcp_video_counters_t video_in
Definition: switch_types.h:754
switch_mutex_t * ice_mutex
Definition: switch_rtp.c:429
#define FALSE
switch_core_session_t * session
Definition: switch_rtp.c:472
switch_timer_t write_timer
Definition: switch_rtp.c:431
uint32_t switch_jb_pop_nack(switch_jb_t *jb)
uint32_t remote_ssrc
Definition: switch_rtp.c:373
switch_rtp_ice_t ice
Definition: switch_rtp.c:397
struct switch_rtcp_report_block report_block
Definition: switch_rtp.c:522
switch_rtcp_video_stats_t rtcp_vstats
Definition: switch_rtp.c:451
unsigned char length
Definition: switch_rtp.c:134
uint32_t ssrc
Definition: switch_rtp.c:372
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:5426
char body[SWITCH_RTCP_MAX_BUF_LEN]
Definition: switch_rtp.c:185
uint32_t last_write_samplecount
Definition: switch_rtp.c:386
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:327
static const switch_payload_t INVALID_PT
Definition: switch_rtp.c:81
switch_sockaddr_t * rtcp_remote_addr
Definition: switch_rtp.c:340
static switch_status_t ice_out(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice, switch_bool_t force)
Definition: switch_rtp.c:853
uint32_t ssrc
Definition: switch_rtp.c:121
#define MAX_NACK
Definition: switch_rtp.c:2132
static void calc_bw_exp(uint32_t bps, uint8_t bits, rtcp_tmmbx_t *tmmbx)
Definition: switch_rtp.c:2024
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:1543
int rtcp_sent_packets
Definition: switch_rtp.c:459
uint32_t tmmbr
Definition: switch_rtp.c:336
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:9107

◆ check_timeout()

static void check_timeout ( switch_rtp_t rtp_session)
static

Definition at line 7279 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().

7280 {
7281 
7283  uint32_t elapsed = 0;
7284 
7285  if (now >= rtp_session->last_media) {
7286  elapsed = (now - rtp_session->last_media) / 1000;
7287  }
7288 
7290  "%s MEDIA TIMEOUT %s %d/%d\n", switch_core_session_get_name(rtp_session->session), rtp_type(rtp_session),
7291  elapsed, rtp_session->media_timeout);
7292 
7293  if (elapsed > rtp_session->media_timeout) {
7294  switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
7295 
7296  switch_channel_execute_on(channel, "execute_on_media_timeout");
7298  }
7299 }
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:440
#define switch_core_session_get_name(_s)
Definition: switch_core.h:265
switch_time_t last_media
Definition: switch_rtp.c:439
#define rtp_type(rtp_session)
Definition: switch_rtp.c:540
_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:472
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 5474 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().

5475 {
5476  switch_frame_flag_t flags = 0;
5477  uint32_t samples = rtp_session->samples_per_interval;
5478 
5479  if (rtp_session->dtmf_data.out_digit_dur > 0) {
5480  int x, loops = 1;
5481 
5482  if (!rtp_session->last_write_ts) {
5483  if (rtp_session->timer.timer_interface) {
5484  //switch_core_timer_sync(&rtp_session->write_timer);
5485  rtp_session->last_write_ts = rtp_session->write_timer.samplecount;
5486  } else {
5487  rtp_session->last_write_ts = rtp_session->samples_per_interval;
5488  }
5489  }
5490 
5491  rtp_session->dtmf_data.out_digit_sofar += samples;
5492  rtp_session->dtmf_data.out_digit_sub_sofar += samples;
5493 
5494  if (rtp_session->dtmf_data.out_digit_sub_sofar > 0xFFFF) {
5495  rtp_session->dtmf_data.out_digit_sub_sofar = samples;
5496  rtp_session->dtmf_data.timestamp_dtmf += 0xFFFF;
5497  }
5498 
5499  if (rtp_session->dtmf_data.out_digit_sofar >= rtp_session->dtmf_data.out_digit_dur) {
5500  rtp_session->dtmf_data.out_digit_packet[1] |= 0x80;
5501  loops = 3;
5502  }
5503 
5504  rtp_session->dtmf_data.out_digit_packet[2] = (unsigned char) (rtp_session->dtmf_data.out_digit_sub_sofar >> 8);
5505  rtp_session->dtmf_data.out_digit_packet[3] = (unsigned char) rtp_session->dtmf_data.out_digit_sub_sofar;
5506 
5507  for (x = 0; x < loops; x++) {
5508  switch_size_t wrote = switch_rtp_write_manual(rtp_session,
5509  rtp_session->dtmf_data.out_digit_packet, 4, 0,
5510  rtp_session->te, rtp_session->dtmf_data.timestamp_dtmf, &flags);
5511 
5512  rtp_session->stats.outbound.raw_bytes += wrote;
5513  rtp_session->stats.outbound.dtmf_packet_count++;
5514 
5515  if (loops == 1) {
5516  rtp_session->last_write_ts += samples;
5517 
5519  rtp_session->dtmf_data.timestamp_dtmf = rtp_session->last_write_ts;
5520  }
5521  }
5522 
5523  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",
5524  loops == 1 ? "middle" : "end", rtp_session->dtmf_data.out_digit,
5525  rtp_session->dtmf_data.timestamp_dtmf,
5526  rtp_session->dtmf_data.out_digit_sofar,
5527  rtp_session->dtmf_data.out_digit_sub_sofar, rtp_session->dtmf_data.out_digit_dur, rtp_session->seq, rtp_session->last_write_ts);
5528  }
5529 
5530  if (loops != 1) {
5531  rtp_session->sending_dtmf = 0;
5532  rtp_session->need_mark = 1;
5533 
5534  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
5535  //switch_core_timer_sync(&rtp_session->write_timer);
5536  rtp_session->last_write_samplecount = rtp_session->write_timer.samplecount;
5537  }
5538 
5539  rtp_session->dtmf_data.out_digit_dur = 0;
5540 
5541  if (rtp_session->interdigit_delay) {
5542  set_dtmf_delay(rtp_session, rtp_session->interdigit_delay, rtp_session->interdigit_delay * 10);
5543  }
5544 
5545  return;
5546  }
5547  }
5548 
5549  if (!rtp_session->dtmf_data.out_digit_dur && rtp_session->dtmf_data.dtmf_queue && switch_queue_size(rtp_session->dtmf_data.dtmf_queue)) {
5550  void *pop;
5551 
5552  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
5553  //switch_core_timer_sync(&rtp_session->write_timer);
5554  if (rtp_session->timer.samplecount < rtp_session->next_write_samplecount) {
5555  return;
5556  }
5557 
5558  if (rtp_session->timer.samplecount >= rtp_session->max_next_write_samplecount) {
5559  rtp_session->queue_delay = 0;
5560  }
5561 
5562  } else if (rtp_session->queue_delay) {
5563  if (rtp_session->delay_samples >= rtp_session->samples_per_interval) {
5564  rtp_session->delay_samples -= rtp_session->samples_per_interval;
5565  } else {
5566  rtp_session->delay_samples = 0;
5567  }
5568 
5569  if (!rtp_session->delay_samples) {
5570  rtp_session->queue_delay = 0;
5571  }
5572  }
5573 
5574  if (rtp_session->queue_delay) {
5575  return;
5576  }
5577 
5578 
5579  if (!rtp_session->sending_dtmf) {
5580  rtp_session->sending_dtmf = 1;
5581  }
5582 
5583  if (switch_queue_trypop(rtp_session->dtmf_data.dtmf_queue, &pop) == SWITCH_STATUS_SUCCESS) {
5584  switch_dtmf_t *rdigit = pop;
5585  switch_size_t wrote;
5586 
5587  if (rdigit->digit == 'w') {
5588  set_dtmf_delay(rtp_session, 500, 0);
5589  free(rdigit);
5590  return;
5591  }
5592 
5593  if (rdigit->digit == 'W') {
5594  set_dtmf_delay(rtp_session, 1000, 0);
5595  free(rdigit);
5596  return;
5597  }
5598 
5599  memset(rtp_session->dtmf_data.out_digit_packet, 0, 4);
5600  rtp_session->dtmf_data.out_digit_sofar = samples;
5601  rtp_session->dtmf_data.out_digit_sub_sofar = samples;
5602  rtp_session->dtmf_data.out_digit_dur = rdigit->duration;
5603  rtp_session->dtmf_data.out_digit = rdigit->digit;
5604  rtp_session->dtmf_data.out_digit_packet[0] = (unsigned char) switch_char_to_rfc2833(rdigit->digit);
5605  rtp_session->dtmf_data.out_digit_packet[1] = 13;
5606  rtp_session->dtmf_data.out_digit_packet[2] = (unsigned char) (rtp_session->dtmf_data.out_digit_sub_sofar >> 8);
5607  rtp_session->dtmf_data.out_digit_packet[3] = (unsigned char) rtp_session->dtmf_data.out_digit_sub_sofar;
5608 
5609 
5610  rtp_session->dtmf_data.timestamp_dtmf = rtp_session->last_write_ts + samples;
5611  rtp_session->last_write_ts = rtp_session->dtmf_data.timestamp_dtmf;
5612  rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 0;
5613 
5614  wrote = switch_rtp_write_manual(rtp_session,
5615  rtp_session->dtmf_data.out_digit_packet,
5616  4,
5617  rtp_session->rtp_bugs & RTP_BUG_CISCO_SKIP_MARK_BIT_2833 ? 0 : 1,
5618  rtp_session->te, rtp_session->dtmf_data.timestamp_dtmf, &flags);
5619 
5620 
5621  rtp_session->stats.outbound.raw_bytes += wrote;
5622  rtp_session->stats.outbound.dtmf_packet_count++;
5623 
5624  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",
5625  rtp_session->dtmf_data.out_digit,
5626  rtp_session->dtmf_data.timestamp_dtmf,
5627  rtp_session->dtmf_data.out_digit_sofar,
5628  rtp_session->dtmf_data.out_digit_sub_sofar, rtp_session->dtmf_data.out_digit_dur, rtp_session->seq, rtp_session->last_write_ts);
5629 
5630  free(rdigit);
5631  }
5632  }
5633 }
uint32_t queue_delay
Definition: switch_rtp.c:390
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:223
uint32_t delay_samples
Definition: switch_rtp.c:387
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:5452
uint32_t duration
Definition: switch_types.h:298
switch_size_t dtmf_packet_count
Definition: switch_types.h:689
uint32_t max_next_write_samplecount
Definition: switch_rtp.c:389
struct switch_rtp_rfc2833_data dtmf_data
Definition: switch_rtp.c:422
switch_size_t raw_bytes
Definition: switch_types.h:682
switch_timer_interface_t * timer_interface
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:392
switch_rtp_numbers_t outbound
Definition: switch_types.h:761
uintptr_t switch_size_t
uint16_t seq
Definition: switch_rtp.c:371
uint8_t need_mark
Definition: switch_rtp.c:376
uint32_t switch_frame_flag_t
switch_rtp_stats_t stats
Definition: switch_rtp.c:450
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:405
uint32_t interdigit_delay
Definition: switch_rtp.c:471
switch_timer_t timer
Definition: switch_rtp.c:430
uint32_t last_write_ts
Definition: switch_rtp.c:382
switch_core_session_t * session
Definition: switch_rtp.c:472
switch_timer_t write_timer
Definition: switch_rtp.c:431
int8_t sending_dtmf
Definition: switch_rtp.c:375
unsigned int out_digit_sofar
Definition: switch_rtp.c:224
switch_rtp_bug_flag_t rtp_bugs
Definition: switch_rtp.c:449
switch_payload_t te
Definition: switch_rtp.c:423
switch_queue_t * dtmf_queue
Definition: switch_rtp.c:221
uint32_t next_write_samplecount
Definition: switch_rtp.c:388
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:226
uint32_t last_write_samplecount
Definition: switch_rtp.c:386
unsigned int out_digit_sub_sofar
Definition: switch_rtp.c:225
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:9107

◆ do_dtls()

static int do_dtls ( switch_rtp_t rtp_session,
switch_dtls_t dtls 
)
static

Definition at line 3364 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().

3365 {
3366  int r = 0, ret = 0, len;
3367  switch_size_t bytes;
3368  unsigned char buf[MAX_DTLS_MTU] = "";
3369  uint8_t is_ice = rtp_session->ice.ice_user ? 1 : 0;
3370  int ready = is_ice ? (rtp_session->ice.rready && rtp_session->ice.ready) : 1;
3371  int pending;
3372 
3374  if (rtp_session->ice.ice_user && rtp_session->ice.rready == 1) ready = 1;
3375  }
3376 
3377  if (!dtls->bytes && !ready) {
3378  return 0;
3379  }
3380 
3381  if (is_ice && !(rtp_session->ice.type & ICE_LITE) && !rtp_session->ice.cand_responsive) {
3382  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG6, "Got DTLS packet but candidate is not responsive\n");
3383 
3384  return 0;
3385  }
3386 
3387  if (is_ice && !switch_cmp_addr(rtp_session->from_addr, rtp_session->ice.addr, SWITCH_TRUE)) {
3388  char tmp_buf1[80] = "";
3389  char tmp_buf2[80] = "";
3390  const char *host_from = switch_get_addr(tmp_buf1, sizeof(tmp_buf1), rtp_session->from_addr);
3391  const char *host_ice_cur_addr = switch_get_addr(tmp_buf2, sizeof(tmp_buf2), rtp_session->ice.addr);
3392 
3393  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);
3394 
3395  return 0;
3396  }
3397 
3398  if (dtls->bytes > 0 && dtls->data) {
3399  ret = BIO_write(dtls->read_bio, dtls->data, (int)dtls->bytes);
3400  if (ret <= 0) {
3401  ret = SSL_get_error(dtls->ssl, ret);
3402  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);
3403  } else if (ret != (int)dtls->bytes) {
3404  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);
3405  }
3406  }
3407 
3408  if (dtls_states[dtls->state]) {
3409  r = dtls_states[dtls->state](rtp_session, dtls);
3410  }
3411 
3412  while ((pending = BIO_ctrl_pending(dtls->filter_bio)) > 0) {
3413  switch_assert(pending <= sizeof(buf));
3414 
3415  len = BIO_read(dtls->write_bio, buf, pending);
3416  if (len > 0) {
3417  bytes = len;
3418  ret = switch_socket_sendto(dtls->sock_output, dtls->remote_addr, 0, (void *)buf, &bytes);
3419 
3420  if (ret != SWITCH_STATUS_SUCCESS) {
3421  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);
3422  } else if (bytes != len) {
3423  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);
3424  }
3425  } else {
3426  ret = SSL_get_error(dtls->ssl, len);
3427  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);
3428  }
3429  }
3430 
3431  return r;
3432 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
BIO * write_bio
Definition: switch_rtp.c:276
switch_sockaddr_t * remote_addr
Definition: switch_rtp.c:287
BIO * filter_bio
Definition: switch_rtp.c:277
switch_sockaddr_t * addr
Definition: switch_rtp.c:248
BIO * read_bio
Definition: switch_rtp.c:275
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:280
#define rtp_type(rtp_session)
Definition: switch_rtp.c:540
_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:286
switch_core_media_ice_type_t type
Definition: switch_rtp.c:251
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:261
dtls_state_handler_t dtls_states[DS_INVALID]
Definition: switch_rtp.c:304
uintptr_t switch_size_t
switch_core_session_t * session
Definition: switch_rtp.c:472
switch_rtp_ice_t ice
Definition: switch_rtp.c:397
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:394
#define switch_assert(expr)
switch_size_t bytes
Definition: switch_rtp.c:284
#define MAX_DTLS_MTU
Definition: switch_rtp.c:269

◆ do_flush()

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

Definition at line 5678 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().

5679 {
5680  int was_blocking = 0;
5681  switch_size_t bytes;
5682  switch_size_t bytes_out = 0;
5683 
5684  if (!switch_rtp_ready(rtp_session)) {
5685  return 0;
5686  }
5687 
5688  reset_jitter_seq(rtp_session);
5689 
5690  if (!force) {
5691  if ((rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) ||
5692  rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] ||
5693  rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] ||
5694  rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON]
5695  ) {
5696  return bytes_in;
5697  }
5698  }
5699 
5700  READ_INC(rtp_session);
5701 
5702  if (switch_rtp_ready(rtp_session) ) {
5703 
5704  if (rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) {
5705  //switch_jb_reset(rtp_session->jb);
5706  bytes_out = bytes_in;
5707  goto end;
5708  }
5709 
5710  if (rtp_session->vbw) {
5711  switch_jb_reset(rtp_session->vbw);
5712  }
5713 
5714  if (rtp_session->vb) {
5715  //switch_jb_reset(rtp_session->vb);
5716  bytes_out = bytes_in;
5717  goto end;
5718  }
5719 
5720  if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_READ]) {
5722  SWITCH_LOG_CONSOLE, "%s FLUSH\n",
5723  rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "NoName"
5724  );
5725  }
5726 
5727  if (!rtp_session->flags[SWITCH_RTP_FLAG_NOBLOCK]) {
5728  was_blocking = 1;
5731  }
5732 
5733  // before processing/flushing packets, if current packet is rfc2833, handle it (else it would be lost)
5734  if (bytes_in > rtp_header_len && rtp_session->last_rtp_hdr.version == 2 && rtp_session->last_rtp_hdr.pt == rtp_session->recv_te) {
5735  int do_cng = 0;
5736 #ifdef DEBUG_2833
5737  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));
5738 #endif
5739  handle_rfc2833(rtp_session, bytes_in, &do_cng);
5740  }
5741 
5742  do {
5743  if (switch_rtp_ready(rtp_session)) {
5744  bytes = sizeof(rtp_msg_t);
5745  switch_socket_recvfrom(rtp_session->from_addr, rtp_session->sock_input, 0, (void *) &rtp_session->recv_msg, &bytes);
5746 
5747  if (bytes) {
5748  int do_cng = 0;
5749 
5750  if (rtp_session->media_timeout) {
5751  rtp_session->last_media = switch_micro_time_now();
5752  }
5753 
5754  /* Make sure to handle RFC2833 packets, even if we're flushing the packets */
5755  if (bytes > rtp_header_len && rtp_session->recv_msg.header.version == 2 && rtp_session->recv_msg.header.pt == rtp_session->recv_te) {
5756  rtp_session->last_rtp_hdr = rtp_session->recv_msg.header;
5757  handle_rfc2833(rtp_session, bytes, &do_cng);
5758 #ifdef DEBUG_2833
5759  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "*** RTP packet handled in flush loop %d ***\n", do_cng);
5760 #endif
5761  }
5762 
5763  rtp_session->stats.inbound.raw_bytes += bytes;
5764  rtp_session->stats.inbound.flush_packet_count++;
5765  rtp_session->stats.inbound.packet_count++;
5766  }
5767  } else {
5768  break;
5769  }
5770  } while (bytes > 0);
5771 
5772 #ifdef DEBUG_2833
5773  if (flushed) {
5774  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "*** do_flush: total flushed packets: %ld ***\n",(long)flushed);
5775  }
5776 #endif
5777 
5778 
5779  if (was_blocking && switch_rtp_ready(rtp_session)) {
5782  }
5783 
5784 
5785  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->session) {
5786  //int type = 1; // sum flags: 1 encoder; 2; decoder
5787  //switch_core_media_codec_control(rtp_session->session, SWITCH_MEDIA_TYPE_VIDEO, SWITCH_IO_READ, SCC_VIDEO_RESET, SCCT_INT, (void *)&type, NULL, NULL);
5789  }
5790  }
5791 
5792  end:
5793 
5794  READ_DEC(rtp_session);
5795 
5796  return bytes_out;
5797 }
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:760
uint8_t switch_rtp_ready(switch_rtp_t *rtp_session)
Test if an RTP session is ready.
Definition: switch_rtp.c:5147
#define SWITCH_CHANNEL_LOG
switch_socket_t * sock_input
Definition: switch_rtp.c:322
uint32_t media_timeout
Definition: switch_rtp.c:440
switch_jb_t * vbw
Definition: switch_rtp.c:436
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:439
#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:682
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:392
#define switch_core_session_request_video_refresh(_s)
Definition: switch_core.h:2881
#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:684
switch_jb_t * vb
Definition: switch_rtp.c:435
switch_rtp_stats_t stats
Definition: switch_rtp.c:450
void switch_rtp_clear_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Clear an RTP Flag.
Definition: switch_rtp.c:5431
void switch_rtp_set_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Set an RTP Flag.
Definition: switch_rtp.c:5372
switch_jb_t * jb
Definition: switch_rtp.c:434
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:341
#define FALSE
static void reset_jitter_seq(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:1646
switch_core_session_t * session
Definition: switch_rtp.c:472
switch_size_t flush_packet_count
Definition: switch_types.h:691
void switch_jb_reset(switch_jb_t *jb)
uint8_t pause_jb
Definition: switch_rtp.c:465
static int jb_valid(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:5662
switch_payload_t recv_te
Definition: switch_rtp.c:424
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:582
#define TRUE
switch_sockaddr_t * from_addr
Definition: switch_rtp.c:394
rtp_hdr_t last_rtp_hdr
Definition: switch_rtp.c:369
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 1571 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().

1571  {
1572  int R;
1573 
1574  if ((switch_size_t)rtp_session->stats.inbound.recved < rtp_session->stats.inbound.flaws) {
1575  rtp_session->stats.inbound.flaws = 0;
1576  }
1577 
1578  if (rtp_session->stats.inbound.recved > 0 &&
1579  rtp_session->stats.inbound.flaws && (rtp_session->stats.inbound.last_flaw != rtp_session->stats.inbound.flaws)) {
1580 
1581  if (rtp_session->consecutive_flaws++) {
1582  int penalty = rtp_session->consecutive_flaws;
1583 
1584  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "%s %s %d consecutive flaws, adding %d flaw penalty\n",
1585  rtp_session_name(rtp_session), rtp_type(rtp_session),
1586  rtp_session->consecutive_flaws, penalty);
1587  rtp_session->bad_stream++;
1588  rtp_session->stats.inbound.flaws += penalty;
1589  rtp_session->stats.inbound.last_flaw = rtp_session->stats.inbound.flaws;
1590 
1591  if (rtp_session->stats.inbound.error_log) {
1592  rtp_session->stats.inbound.error_log->flaws += penalty;
1593  rtp_session->stats.inbound.error_log->consecutive_flaws++;
1594  }
1595  }
1596  } else {
1597  rtp_session->consecutive_flaws = 0;
1598  }
1599 
1600  R = (int)((double)((double)(rtp_session->stats.inbound.recved - rtp_session->stats.inbound.flaws) / (double)rtp_session->stats.inbound.recved) * 100.0);
1601 
1602  if (R < 0 || R > 100) R = 100;
1603 
1604  rtp_session->stats.inbound.R = R;
1605  rtp_session->stats.inbound.mos = 1 + (0.035) * R + (.000007) * R * (R-60) * (100-R);
1606 
1607  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",
1608  rtp_session_name(rtp_session),
1609  rtp_type(rtp_session),
1610  rtp_session->stats.inbound.R,
1611  (long int)(rtp_session->stats.inbound.recved - rtp_session->stats.inbound.flaws), rtp_session->stats.inbound.recved,
1612  (long int)rtp_session->stats.inbound.flaws,
1613  rtp_session->stats.inbound.mos,
1614  rtp_session->stats.inbound.variance,
1615  rtp_session->stats.inbound.min_variance,
1616  rtp_session->stats.inbound.max_variance
1617  );
1618 
1619 }
switch_size_t flaws
Definition: switch_types.h:712
#define SWITCH_CHANNEL_SESSION_LOG(x)
switch_rtp_numbers_t inbound
Definition: switch_types.h:760
uint32_t consecutive_flaws
Definition: switch_rtp.c:412
#define rtp_session_name(_rtp_session)
Definition: switch_rtp.c:85
switch_size_t last_flaw
Definition: switch_types.h:713
uint32_t flaws
Definition: switch_types.h:675
#define rtp_type(rtp_session)
Definition: switch_rtp.c:540
uintptr_t switch_size_t
uint32_t consecutive_flaws
Definition: switch_types.h:676
switch_rtp_stats_t stats
Definition: switch_rtp.c:450
struct error_period * error_log
Definition: switch_types.h:716
switch_core_session_t * session
Definition: switch_rtp.c:472
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:453

◆ dtls_bio_filter_ctrl()

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

Definition at line 3605 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().

3605  {
3606 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3607  dtls_bio_filter *filter = (dtls_bio_filter *)bio->ptr;
3608 #else
3609  dtls_bio_filter *filter = (dtls_bio_filter *)BIO_get_data(bio);
3610 #endif
3611 
3612  switch(cmd) {
3613  case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
3614  return 1200;
3615  case BIO_CTRL_DGRAM_SET_MTU:
3616  filter->mtu = num;
3617  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Setting MTU: %ld\n", filter->mtu);
3618  return num;
3619  case BIO_CTRL_FLUSH:
3620  return 1;
3621  case BIO_CTRL_DGRAM_QUERY_MTU:
3622  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Advertizing MTU: %ld\n", filter->mtu);
3623  return filter->mtu;
3624  case BIO_CTRL_WPENDING:
3625  return 0L;
3626  case BIO_CTRL_PENDING: {
3627  int pending = 0;
3628  packet_list_t *top;
3629 
3630  if (filter == NULL) {
3631  return 0;
3632  }
3633 
3634  switch_mutex_lock(filter->mutex);
3635  if ((top = filter->packets)) {
3636  filter->packets = filter->packets->next;
3637 
3638  if (top == filter->tail || !filter->packets) {
3639  filter->tail = NULL;
3640  }
3641 
3642  pending = top->size;
3643  top->next = filter->unused;
3644  filter->unused = top;
3645  }
3646  switch_mutex_unlock(filter->mutex);
3647 
3648  return pending;
3649  }
3650  default:
3651  //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "dtls_bio_filter_ctrl: %d\n", cmd);
3652  break;
3653  }
3654  return 0;
3655 }
#define SWITCH_CHANNEL_LOG
struct packet_list_s * next
Definition: switch_rtp.c:3483
switch_mutex_t * mutex
Definition: switch_rtp.c:3491
packet_list_t * packets
Definition: switch_rtp.c:3488
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
packet_list_t * tail
Definition: switch_rtp.c:3490
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:3489

◆ dtls_bio_filter_free()

static int dtls_bio_filter_free ( BIO *  bio)
static

Definition at line 3523 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().

3523  {
3524  dtls_bio_filter *filter;
3525 
3526  if (bio == NULL) {
3527  return 0;
3528  }
3529 
3530  /* Get rid of the filter state */
3531 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3532  filter = (dtls_bio_filter *)bio->ptr;
3533 #else
3534  filter = (dtls_bio_filter *)BIO_get_data(bio);
3535 #endif
3536 
3537  if (filter != NULL) {
3538  switch_memory_pool_t *pool = filter->pool;
3540  pool = NULL;
3541  filter = NULL;
3542  }
3543 
3544 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3545  bio->ptr = NULL;
3546  bio->init = 0;
3547  bio->flags = 0;
3548 #else
3549  BIO_set_init(bio, 0);
3550  BIO_set_data(bio, NULL);
3551  BIO_clear_flags(bio, ~0);
3552 #endif
3553  return 1;
3554 }
#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:3492

◆ dtls_bio_filter_new()

static int dtls_bio_filter_new ( BIO *  bio)
static

Definition at line 3497 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().

3497  {
3498  /* Create a filter state struct */
3499  dtls_bio_filter *filter;
3501 
3503  filter = switch_core_alloc(pool, sizeof(*filter));
3504  filter->pool = pool;
3505 
3506  filter->packets = NULL;
3507  switch_mutex_init(&filter->mutex, SWITCH_MUTEX_NESTED, filter->pool);
3508 
3509  /* Set the BIO as initialized */
3510 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3511  bio->init = 1;
3512  bio->ptr = filter;
3513  bio->flags = 0;
3514 #else
3515  BIO_set_init(bio, 1);
3516  BIO_set_data(bio, filter);
3517  BIO_clear_flags(bio, ~0);
3518 #endif
3519 
3520  return 1;
3521 }
#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:3491
switch_memory_pool_t * pool
packet_list_t * packets
Definition: switch_rtp.c:3488
#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:3492

◆ dtls_bio_filter_write()

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

Definition at line 3556 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().

3556  {
3557  long ret;
3558  dtls_bio_filter *filter;
3559 
3560  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "dtls_bio_filter_write: %p, %d\n", (void *)in, inl);
3561  /* Forward data to the write BIO */
3562 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3563  ret = BIO_write(bio->next_bio, in, inl);
3564 #else
3565  ret = BIO_write(BIO_next(bio), in, inl);
3566 #endif
3567 
3569 
3570  /* Keep track of the packet, as we'll advertize them one by one after a pending check */
3571 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3572  filter = (dtls_bio_filter *)bio->ptr;
3573 #else
3574  filter = (dtls_bio_filter *)BIO_get_data(bio);
3575 #endif
3576 
3577  if (filter != NULL) {
3578  packet_list_t *node;
3579 
3580  switch_mutex_lock(filter->mutex);
3581  if (filter->unused) {
3582  node = filter->unused;
3583  node->next = NULL;
3584  filter->unused = filter->unused->next;
3585  } else {
3586  node = switch_core_alloc(filter->pool, sizeof(*node));
3587  }
3588 
3589  node->size = ret;
3590 
3591  if (filter->tail) {
3592  filter->tail->next = node;
3593  } else {
3594  filter->packets = node;
3595  }
3596 
3597  filter->tail = node;
3598 
3599  switch_mutex_unlock(filter->mutex);
3600  //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "New list length: %d\n", g_list_length(filter->packets));
3601  }
3602  return ret;
3603 }
#define SWITCH_CHANNEL_LOG
struct packet_list_s * next
Definition: switch_rtp.c:3483
switch_mutex_t * mutex
Definition: switch_rtp.c:3491
packet_list_t * packets
Definition: switch_rtp.c:3488
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
packet_list_t * tail
Definition: switch_rtp.c:3490
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:3489
switch_memory_pool_t * pool
Definition: switch_rtp.c:3492

◆ dtls_state_fail()

static int dtls_state_fail ( switch_rtp_t rtp_session,
switch_dtls_t dtls 
)
static

Definition at line 3309 of file switch_rtp.c.

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

3310 {
3311  if (rtp_session->session) {
3312  switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
3314  }
3315 
3316  return -1;
3317 }
#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:472

◆ dtls_state_handshake()

static int dtls_state_handshake ( switch_rtp_t rtp_session,
switch_dtls_t dtls 
)
static

Definition at line 3320 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.

3321 {
3322  int ret;
3323 
3324  if ((ret = SSL_do_handshake(dtls->ssl)) != 1){
3325  switch((ret = SSL_get_error(dtls->ssl, ret))){
3326  case SSL_ERROR_WANT_READ:
3327  case SSL_ERROR_WANT_WRITE:
3328  case SSL_ERROR_NONE:
3329  break;
3330  default:
3331  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);
3332  dtls_set_state(dtls, DS_FAIL);
3333  return -1;
3334  }
3335  }
3336 
3337  if (SSL_is_init_finished(dtls->ssl)) {
3338  dtls_set_state(dtls, DS_SETUP);
3339  }
3340 
3341  return 0;
3342 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
#define rtp_type(rtp_session)
Definition: switch_rtp.c:540
#define dtls_set_state(_dtls, _state)
Definition: switch_rtp.c:3209
switch_core_session_t * session
Definition: switch_rtp.c:472
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 3200 of file switch_rtp.c.

References DS_INVALID.

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

◆ dtls_state_ready()

static int dtls_state_ready ( switch_rtp_t rtp_session,
switch_dtls_t dtls 
)
static

Definition at line 3292 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.

3293 {
3294 
3295  if (dtls->new_state) {
3296  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
3297  switch_core_session_t *other_session;
3298 
3299  if (rtp_session->session && switch_core_session_get_partner(rtp_session->session, &other_session) == SWITCH_STATUS_SUCCESS) {
3301  switch_core_session_rwunlock(other_session);
3302  }
3303  }
3304  dtls->new_state = 0;
3305  }
3306  return 0;
3307 }
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:392
#define switch_core_session_request_video_refresh(_s)
Definition: switch_core.h:2881
#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:472
uint8_t new_state
Definition: switch_rtp.c:282

◆ dtls_state_setup()

static int dtls_state_setup ( switch_rtp_t rtp_session,
switch_dtls_t dtls 
)
static

Definition at line 3215 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.

3216 {
3217  X509 *cert;
3218  switch_secure_settings_t ssec; /* Used just to wrap over params in a call to switch_rtp_add_crypto_key. */
3219  int r = 0;
3220 
3221  uint8_t raw_key_data[cr_kslen * 2];
3222  unsigned char local_key_buf[cr_kslen];
3223  unsigned char remote_key_buf[cr_kslen];
3224 
3225  memset(&ssec, 0, sizeof(ssec));
3226  memset(&raw_key_data, 0, cr_kslen * 2 * sizeof(uint8_t));
3227  memset(&local_key_buf, 0, cr_kslen * sizeof(unsigned char));
3228  memset(&remote_key_buf, 0, cr_kslen * sizeof(unsigned char));
3229 
3230  if ((dtls->type & DTLS_TYPE_SERVER)) {
3231  r = 1;
3232  } else if ((cert = SSL_get_peer_certificate(dtls->ssl))) {
3235  X509_free(cert);
3236  }
3237 
3238  if (!r) {
3239  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "%s Fingerprint Verification Failed!\n", rtp_type(rtp_session));
3240  dtls_set_state(dtls, DS_FAIL);
3241  return -1;
3242  } else {
3243  unsigned char *local_key, *remote_key, *local_salt, *remote_salt;
3244 
3245  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "%s Fingerprint Verified.\n", rtp_type(rtp_session));
3246 
3247 #ifdef HAVE_OPENSSL_DTLS_SRTP
3248  if (!SSL_export_keying_material(dtls->ssl, raw_key_data, sizeof(raw_key_data), "EXTRACTOR-dtls_srtp", 19, NULL, 0, 0)) {
3249  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "%s Key material export failure\n", rtp_type(rtp_session));
3250  dtls_set_state(dtls, DS_FAIL);
3251  return -1;
3252  }
3253 #else
3254  return -1;
3255 #endif
3256 
3257  if ((dtls->type & DTLS_TYPE_CLIENT)) {
3258  local_key = raw_key_data;
3259  remote_key = local_key + cr_keylen;
3260  local_salt = remote_key + cr_keylen;
3261  remote_salt = local_salt + cr_saltlen;
3262 
3263  } else {
3264  remote_key = raw_key_data;
3265  local_key = remote_key + cr_keylen;
3266  remote_salt = local_key + cr_keylen;
3267  local_salt = remote_salt + cr_saltlen;
3268  }
3269 
3270  memcpy(ssec.local_raw_key, local_key, cr_keylen);
3271  memcpy(ssec.local_raw_key + cr_keylen, local_salt, cr_saltlen);
3272 
3273  memcpy(ssec.remote_raw_key, remote_key, cr_keylen);
3274  memcpy(ssec.remote_raw_key + cr_keylen, remote_salt, cr_saltlen);
3275 
3277 
3278  if (dtls == rtp_session->rtcp_dtls && rtp_session->rtcp_dtls != rtp_session->dtls) {
3281  } else {
3282  switch_rtp_add_crypto_key(rtp_session, SWITCH_RTP_CRYPTO_SEND, 0, &ssec);
3283  switch_rtp_add_crypto_key(rtp_session, SWITCH_RTP_CRYPTO_RECV, 0, &ssec);
3284  }
3285  }
3286 
3287  dtls_set_state(dtls, DS_READY);
3288 
3289  return 0;
3290 }
dtls_fingerprint_t * remote_fp
Definition: switch_rtp.c:279
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:4059
int switch_core_cert_verify(dtls_fingerprint_t *fp)
#define cr_kslen
Definition: switch_rtp.c:3213
#define rtp_type(rtp_session)
Definition: switch_rtp.c:540
unsigned char local_raw_key[SWITCH_RTP_MAX_CRYPTO_LEN]
switch_dtls_t * dtls
Definition: switch_rtp.c:366
#define dtls_set_state(_dtls, _state)
Definition: switch_rtp.c:3209
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:472
switch_dtls_t * rtcp_dtls
Definition: switch_rtp.c:367
dtls_type_t type
Definition: switch_rtp.c:283
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:3212
switch_rtp_crypto_key_type_t crypto_type
memset(buf, 0, buflen)
#define cr_keylen
Definition: switch_rtp.c:3211

◆ enable_local_rtcp_socket()

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

Definition at line 2717 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().

2717  {
2718 
2719  const char *host = rtp_session->local_host_str;
2720  switch_port_t port = rtp_session->local_port;
2721  switch_socket_t *rtcp_new_sock = NULL, *rtcp_old_sock = NULL;
2723  char bufa[50];
2724 
2725  if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
2726  if (switch_sockaddr_info_get(&rtp_session->rtcp_local_addr, host, SWITCH_UNSPEC, port+1, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS) {
2727  *err = "RTCP Local Address Error!";
2728  goto done;
2729  }
2730 
2731  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) {
2732  *err = "RTCP Socket Error!";
2733  goto done;
2734  }
2735 
2737  *err = "RTCP Socket Error!";
2738  goto done;
2739  }
2740 
2741  if (switch_socket_bind(rtcp_new_sock, rtp_session->rtcp_local_addr) != SWITCH_STATUS_SUCCESS) {
2742  *err = "RTCP Bind Error!";
2743  goto done;
2744  }
2745 
2746  if (switch_sockaddr_info_get(&rtp_session->rtcp_from_addr, switch_get_addr(bufa, sizeof(bufa), rtp_session->from_addr),
2747  SWITCH_UNSPEC, switch_sockaddr_get_port(rtp_session->from_addr) + 1, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS) {
2748  *err = "RTCP From Address Error!";
2749  goto done;
2750  }
2751 
2752  rtcp_old_sock = rtp_session->rtcp_sock_input;
2753  rtp_session->rtcp_sock_input = rtcp_new_sock;
2754  rtcp_new_sock = NULL;
2755 
2756  switch_socket_create_pollset(&rtp_session->rtcp_read_pollfd, rtp_session->rtcp_sock_input, SWITCH_POLLIN | SWITCH_POLLERR, rtp_session->pool);
2757 
2758  done:
2759 
2760  if (*err) {
2761 
2762  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error allocating rtcp [%s]\n", *err);
2763  status = SWITCH_STATUS_FALSE;
2764  }
2765 
2766  if (rtcp_new_sock) {
2767  switch_socket_close(rtcp_new_sock);
2768  }
2769 
2770  if (rtcp_old_sock) {
2771  switch_socket_close(rtcp_old_sock);
2772  }
2773  } else {
2774  status = SWITCH_STATUS_FALSE;
2775  }
2776 
2777  return status;
2778 }
#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:394
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:326
switch_socket_t * rtcp_sock_input
Definition: switch_rtp.c:322
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:392
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:400
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:393
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:472
#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:323
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:416
struct fspr_socket_t switch_socket_t
Definition: switch_apr.h:1026
switch_sockaddr_t * from_addr
Definition: switch_rtp.c:394
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 2673 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().

2673  {
2674 
2676 
2677  if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
2678 
2680  rtp_session->remote_rtcp_port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS || !rtp_session->rtcp_remote_addr) {
2681  *err = "RTCP Remote Address Error!";
2682  return SWITCH_STATUS_FALSE;
2683  } else {
2684  const char *host;
2685  char bufa[50];
2686 
2687  host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtcp_remote_addr);
2688 
2690  "Setting RTCP remote addr to %s:%d %d\n", host, rtp_session->remote_rtcp_port, rtp_session->rtcp_remote_addr->family);
2691  }
2692 
2693  if (rtp_session->rtcp_sock_input && switch_sockaddr_get_family(rtp_session->rtcp_remote_addr) ==
2695  rtp_session->rtcp_sock_output = rtp_session->rtcp_sock_input;
2696  } else {
2697 
2698  if (rtp_session->rtcp_sock_output && rtp_session->rtcp_sock_output != rtp_session->rtcp_sock_input) {
2699  switch_socket_close(rtp_session->rtcp_sock_output);
2700  }
2701 
2702  if ((status = switch_socket_create(&rtp_session->rtcp_sock_output,
2704  SOCK_DGRAM, 0, rtp_session->pool)) != SWITCH_STATUS_SUCCESS) {
2705  *err = "RTCP Socket Error!";
2706  }
2707  }
2708 
2709  } else {
2710  *err = "RTCP NOT ACTIVE!";
2711  }
2712 
2713  return status;
2714 
2715 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
switch_sockaddr_t * rtcp_local_addr
Definition: switch_rtp.c:326
switch_port_t remote_rtcp_port
Definition: switch_rtp.c:419
switch_socket_t * rtcp_sock_input
Definition: switch_rtp.c:322
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:392
switch_memory_pool_t * pool
Definition: switch_rtp.c:393
switch_socket_t * rtcp_sock_output
Definition: switch_rtp.c:322
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:472
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:340
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:402

◆ free_dtls()

static void free_dtls ( switch_dtls_t **  dtlsp)
static

Definition at line 3344 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().

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

◆ get_next_write_ts()

static uint8_t get_next_write_ts ( switch_rtp_t rtp_session,
uint32_t  timestamp 
)
static

Definition at line 1543 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().

1544 {
1545  uint8_t m = 0, changed = 0;
1546 
1547  if (!(rtp_session->rtp_bugs & RTP_BUG_SEND_LINEAR_TIMESTAMPS)) {
1548  if (timestamp) {
1549  rtp_session->ts = (uint32_t) timestamp;
1550  changed++;
1551  } else if (switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER)) {
1552  switch_core_timer_next(&rtp_session->write_timer);
1553  rtp_session->ts = rtp_session->write_timer.samplecount;
1554  changed++;
1555  }
1556  }
1557 
1558  if (!changed) {
1559  rtp_session->ts = rtp_session->last_write_ts + rtp_session->samples_per_interval;
1560  } else {
1561  /* Send marker bit if timestamp is lower/same as before (resetted/new timer) */
1562  if (abs((int32_t)(rtp_session->ts - rtp_session->last_write_ts)) > rtp_session->samples_per_interval
1563  && !(rtp_session->rtp_bugs & RTP_BUG_NEVER_SEND_MARKER)) {
1564  m++;
1565  }
1566  }
1567 
1568  return m;
1569 }
uint32_t ts
Definition: switch_rtp.c:380
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:405
uint32_t last_write_ts
Definition: switch_rtp.c:382
switch_timer_t write_timer
Definition: switch_rtp.c:431
switch_rtp_bug_flag_t rtp_bugs
Definition: switch_rtp.c:449
uint32_t switch_rtp_test_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flags)
Test an RTP Flag.
Definition: switch_rtp.c:5426

◆ get_recv_payload()

static int get_recv_payload ( switch_rtp_t rtp_session)
static

Definition at line 5824 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().

5825 {
5826  int r = -1;
5827 
5828  if (rtp_session->pmaps && *rtp_session->pmaps) {
5829  payload_map_t *pmap;
5830 
5831  switch_mutex_lock(rtp_session->flag_mutex);
5832 
5833  for (pmap = *rtp_session->pmaps; pmap && pmap->allocated; pmap = pmap->next) {
5834  if (pmap->negotiated) {
5835  r = pmap->pt;
5836  break;
5837  }
5838  }
5839  switch_mutex_unlock(rtp_session->flag_mutex);
5840  }
5841 
5842  return r;
5843 }
uint8_t allocated
switch_mutex_t * flag_mutex
Definition: switch_rtp.c:426
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:473

◆ 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 952 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().

953 {
954  switch_stun_packet_t *packet;
956  void *end_buf;
957  char username[STUN_USERNAME_MAX_SIZE] = { 0 };
958  unsigned char buf[1500] = { 0 };
959  switch_size_t cpylen = len;
960  int xlen = 0;
961  int ok = 1;
962  uint32_t *pri = NULL;
963  int is_rtcp = ice == &rtp_session->rtcp_ice;
964  switch_channel_t *channel;
965  int i;
966  switch_sockaddr_t *from_addr = rtp_session->from_addr;
967  const char *from_host = NULL;
968  switch_port_t from_port = 0;
969  char faddr_buf[80] = "";
970 
971  if (is_rtcp) {
972  from_addr = rtp_session->rtcp_from_addr;
973  }
974 
975  from_host = switch_get_addr(faddr_buf, sizeof(faddr_buf), from_addr);
976  from_port = switch_sockaddr_get_port(from_addr);
977 
978  //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
979  // switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "WTF OK %s CALL\n", rtp_type(rtp_session));
980  //}
981 
982  if (!switch_rtp_ready(rtp_session) || zstr(ice->user_ice) || zstr(ice->ice_user)) {
983  return;
984  }
985 
986  READ_INC(rtp_session);
987  WRITE_INC(rtp_session);
988 
989  switch_mutex_lock(rtp_session->ice_mutex);
990 
991  if (!switch_rtp_ready(rtp_session)) {
992  goto end;
993  }
994 
995  if (cpylen > sizeof(buf)) {
996  cpylen = sizeof(buf);
997  }
998 
999  channel = switch_core_session_get_channel(rtp_session->session);
1000 
1001  memcpy(buf, data, cpylen);
1002  packet = switch_stun_packet_parse(buf, (uint32_t)cpylen);
1003  if (!packet) {
1004  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Invalid STUN/ICE packet received %ld bytes\n", (long)cpylen);
1005  goto end;
1006 
1007  }
1008 
1009  rtp_session->last_stun = switch_micro_time_now();
1010 
1011  if (!rtp_session->first_stun) {
1012  rtp_session->first_stun = rtp_session->last_stun;
1013  }
1014 
1015  calc_elapsed(rtp_session, ice);
1016 
1017  end_buf = buf + ((sizeof(buf) > packet->header.length) ? packet->header.length : sizeof(buf));
1018 
1019  switch_stun_packet_first_attribute(packet, attr);
1020  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG8, "%s STUN PACKET TYPE: %s\n",
1022  do {
1023  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,
1025 
1026  switch (attr->type) {
1028  {
1029  ice->rready = 1;
1030  for (i = 0; i < ice->ice_params->cand_idx[ice->proto]; i++) {
1031  if (!strcmp(ice->ice_params->cands[i][ice->proto].con_addr, from_host) && ice->ice_params->cands[i][ice->proto].con_port == from_port) {
1032  ice->ice_params->cands[i][ice->proto].use_candidate = 1;
1033  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);
1034  }
1035  }
1036  }
1037  break;
1039  {
1041  uint32_t code = (err->code * 100) + err->number;
1042 
1043  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "%s got %s stun binding response %u\n",
1044  rtp_session_name(rtp_session),
1045  rtp_type(rtp_session),
1046  code
1047  );
1048 
1049  if ((ice->type & ICE_VANILLA) && code == 487) {
1050  if ((ice->type & ICE_CONTROLLED)) {
1051  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "%s STUN Changing role to CONTROLLING\n", rtp_type(rtp_session));
1052  ice->type &= ~ICE_CONTROLLED;
1053  } else {
1054  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "%s STUN Changing role to CONTROLLED\n", rtp_type(rtp_session));
1055  ice->type |= ICE_CONTROLLED;
1056  }
1058  }
1059 
1060  }
1061  break;
1063  {
1064  char ip[50];
1065  uint16_t port;
1066  switch_stun_packet_attribute_get_mapped_address(attr, ip, sizeof(ip), &port);
1067  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG8, "|------: %s:%d\n", ip, port);
1068  }
1069  break;
1071  {
1072  char ip[50];
1073  uint16_t port;
1074  switch_stun_packet_attribute_get_xor_mapped_address(attr, &packet->header, ip, sizeof(ip), &port);
1075  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG8, "|------: %s:%d\n", ip, port);
1076  }
1077  break;
1079  {
1080  switch_stun_packet_attribute_get_username(attr, username, sizeof(username));
1081  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG8, "|------: %s\n", username);
1082  }
1083  break;
1084 
1086  {
1087  uint32_t priority = 0;
1088  pri = (uint32_t *) attr->value;
1089  priority = ntohl(*pri);
1090  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG8, "|------: %u\n", priority);
1091  ok = priority == ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].priority;
1092  }
1093  break;
1094  }
1095 
1096  if (!switch_stun_packet_next_attribute(attr, end_buf)) {
1097  break;
1098  }
1099 
1100  xlen += 4 + switch_stun_attribute_padded_length(attr);
1101  } while (xlen <= packet->header.length);
1102 
1103  if ((ice->type & ICE_GOOGLE_JINGLE) && ok) {
1104  ok = !strcmp(ice->user_ice, username);
1105  }
1106 
1108  goto end;
1109  }
1110 
1111  if ((ice->type & ICE_VANILLA)) {
1112  if (!ok) ok = !memcmp(packet->header.id, ice->last_sent_id, 12);
1113 
1114  if (packet->header.type == SWITCH_STUN_BINDING_RESPONSE) {
1115  ok = 1;
1116 
1117  if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
1118  rtp_session->ice.rready = 1;
1119  rtp_session->rtcp_ice.rready = 1;
1120  } else {
1121  ice->rready = 1;
1122  }
1123 
1124  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG6, "Received STUN Binding Response from %s\n", from_host);
1125 
1126  if (ice->ice_params) {
1127  for (i = 0; i < ice->ice_params->cand_idx[ice->proto]; i++) {
1128  if (!strcmp(ice->ice_params->cands[i][ice->proto].con_addr, from_host) && ice->ice_params->cands[i][ice->proto].con_port == from_port) {
1129  ice->ice_params->cands[i][ice->proto].responsive = 1;
1130  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);
1131  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) {
1132  ice->cand_responsive = 1;
1133  ice->initializing = 0;
1134  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);
1135  }
1136  }
1137  }
1138  }
1139 
1140  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
1142  }
1143  }
1144 
1145  if (!ok && ice == &rtp_session->ice && rtp_session->rtcp_ice.ice_params && pri &&
1146  *pri == rtp_session->rtcp_ice.ice_params->cands[rtp_session->rtcp_ice.ice_params->chosen[1]][1].priority) {
1147  ice = &rtp_session->rtcp_ice;
1148  ok = 1;
1149  }
1150 
1151  if (!zstr(username)) {
1152  if (!icecmp(username, ice)) {
1153  ok = 1;
1154  } else if(!zstr(rtp_session->rtcp_ice.user_ice) && !icecmp(username, &rtp_session->rtcp_ice)) {
1155  ice = &rtp_session->rtcp_ice;
1156  ok = 1;
1157  }
1158  }
1159 
1160  if (ok) {
1161  ice->missed_count = 0;
1162  } else {
1163  switch_rtp_ice_t *icep[2] = { &rtp_session->ice, &rtp_session->rtcp_ice };
1164  switch_port_t port = 0;
1165  char *host = NULL;
1166 
1167  if (rtp_session->elapsed_stun > STUN_TOO_LONG && pri) {
1168  int i, j;
1169  uint32_t old;
1170  //const char *tx_host;
1171  const char *old_host, *err = NULL;
1172  //char bufa[50];
1173  char bufb[50];
1174  char adj_port[6];
1175  switch_channel_t *channel = NULL;
1176 
1177 
1178  ice->missed_count++;
1179  //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "missed %d\n", ice->missed_count);
1180 
1181 
1182  if (rtp_session->session) {
1183  channel = switch_core_session_get_channel(rtp_session->session);
1184  }
1185 
1186  //ice->ice_params->cands[ice->ice_params->chosen][ice->proto].priority;
1187  for (j = 0; j < 2; j++) {
1188  if (!icep[j] || !icep[j]->ice_params) {
1189  continue;
1190  }
1191  for (i = 0; i < icep[j]->ice_params->cand_idx[icep[j]->proto]; i++) {
1192  if (icep[j]->ice_params && icep[j]->ice_params->cands[i][icep[j]->proto].priority == *pri) {
1193  if (j == IPR_RTP) {
1194  icep[j]->ice_params->chosen[j] = i;
1195  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "Change candidate index to %d\n", i);
1196  }
1197 
1198  ice = icep[j];
1199  ok = 1;
1200 
1201  if (j != IPR_RTP) {
1202  break;
1203  }
1204 
1205  old = rtp_session->remote_port;
1206 
1207  //tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->from_addr);
1208  old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
1209 
1210  host = ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_addr;
1211  port = ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port;
1212 
1213  if (!host || !port) {
1214  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error setting remote host!\n");
1215  goto end;
1216  }
1217 
1219  "%s ICE Auto Changing port from %s:%u to %s:%u\n", rtp_type(rtp_session), old_host, old, host, port);
1220 
1221 
1222  if (channel) {
1223  switch_channel_set_variable(channel, "remote_media_ip_reported", switch_channel_get_variable(channel, "remote_media_ip"));
1224  switch_channel_set_variable(channel, "remote_media_ip", host);
1225  switch_channel_set_variable(channel, "rtp_auto_adjust_ip", host);
1226  switch_snprintf(adj_port, sizeof(adj_port), "%u", port);
1227  switch_channel_set_variable(channel, "remote_media_port_reported", switch_channel_get_variable(channel, "remote_media_port"));
1228  switch_channel_set_variable(channel, "remote_media_port", adj_port);
1229  switch_channel_set_variable(channel, "rtp_auto_adjust_port", adj_port);
1230  switch_channel_set_variable(channel, "rtp_auto_candidate_adjust", "true");
1231  }
1232  rtp_session->auto_adj_used = 1;
1233 
1234 
1235  switch_rtp_set_remote_address(rtp_session, host, port, 0, SWITCH_FALSE, &err);
1236  if (switch_sockaddr_info_get(&ice->addr, host, SWITCH_UNSPEC, port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS ||
1237  !ice->addr) {
1238  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error setting remote host!\n");
1239  goto end;
1240  }
1241 
1242  if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) {
1244  } else {
1246  }
1247 
1248  }
1249  }
1250  }
1251  }
1252  }
1253  }
1254 
1255  if (ice->missed_count > 5 && !(ice->type & ICE_GOOGLE_JINGLE)) {
1256  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "missed too many: %d, looking for new ICE dest.\n",
1257  ice->missed_count);
1258  ice->rready = 0;
1259  ice->cand_responsive = 0;
1260  ok = 1;
1261  }
1262 
1263 
1264  //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] || 1) {
1265  // switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "WTF OK %s %d\n", rtp_type(rtp_session), ok);
1266  //}
1267 
1268  if (ok) {
1269  const char *host2 = NULL;
1270  switch_port_t port2 = 0;
1271  char buf2[80] = "";
1272 
1273  if (packet->header.type == SWITCH_STUN_BINDING_REQUEST) {
1274  uint8_t stunbuf[512];
1275  switch_stun_packet_t *rpacket;
1276  const char *remote_ip;
1277  switch_size_t bytes;
1278  char ipbuf[50];
1279  switch_socket_t *sock_output = rtp_session->sock_output;
1280  uint8_t do_adj = 0;
1282  int cmp = 0;
1283  int cur_idx = -1, is_relay = 0, is_responsive = 0, use_candidate = 0;
1284 
1285  if (is_rtcp) {
1286  sock_output = rtp_session->rtcp_sock_output;
1287  }
1288 
1289  if (!ice->ready) {
1290  ice->ready = 1;
1291  }
1292 
1293  memset(stunbuf, 0, sizeof(stunbuf));
1295 
1296  if ((ice->type & ICE_GOOGLE_JINGLE)) {
1297  switch_stun_packet_attribute_add_username(rpacket, username, (uint16_t)strlen(username));
1298  }
1299 
1300  remote_ip = switch_get_addr(ipbuf, sizeof(ipbuf), from_addr);
1301 
1302  switch_stun_packet_attribute_add_xor_binded_address(rpacket, (char *) remote_ip, switch_sockaddr_get_port(from_addr), from_addr->family);
1303 
1304  if ((ice->type & ICE_VANILLA)) {
1307  }
1308 
1309  bytes = switch_stun_packet_length(rpacket);
1310 
1311  host2 = switch_get_addr(buf2, sizeof(buf2), ice->addr);
1312  port2 = switch_sockaddr_get_port(ice->addr);
1313  cmp = switch_cmp_addr(from_addr, ice->addr, SWITCH_FALSE);
1314 
1315  for (i = 0; i < ice->ice_params->cand_idx[ice->proto]; i++) {
1316  if (!strcmp(ice->ice_params->cands[i][ice->proto].con_addr, from_host) && ice->ice_params->cands[i][ice->proto].con_port == from_port) {
1317  if (!strcasecmp(ice->ice_params->cands[i][ice->proto].cand_type, "relay")) {
1318  is_relay = 1;
1319  }
1320 
1321  if (ice->ice_params->cands[i][ice->proto].responsive) {
1322  is_responsive = 1;
1323  }
1324 
1325  if (ice->ice_params->cands[i][ice->proto].use_candidate) {
1326  use_candidate = 1;
1327  }
1328  }
1329  }
1330 
1332  "%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",
1333  is_relay, is_responsive, use_candidate, ice->ready, ice->rready);
1334 
1335  if (ice->initializing && !cmp) {
1336  if (!rtp_session->adj_window && (!ice->ready || !ice->rready || (!rtp_session->dtls || rtp_session->dtls->state != DS_READY))) {
1337  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",
1338  switch_channel_get_name(channel), rtp_type(rtp_session), is_rtcp ? "rtcp" : "rtp", from_host, from_port, is_relay, is_responsive, use_candidate,
1339  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);
1340 
1341  rtp_session->adj_window = now + 10000000;
1342  }
1343 
1344  if (rtp_session->adj_window) {
1345  if (rtp_session->adj_window > now) {
1346  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",
1347  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,
1348  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);
1349 
1350  if (!is_relay && (rtp_session->elapsed_stun >= 3000 || rtp_session->adj_window == (now + 10000000))) {
1351  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",
1352  switch_channel_get_name(channel), rtp_type(rtp_session), is_rtcp ? "rtcp" : "rtp", from_host, from_port, is_relay, is_responsive, use_candidate,
1353  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);
1354 
1355  do_adj++;
1356  rtp_session->last_adj = now;
1357  }
1358  } else {
1359  rtp_session->adj_window = 0;
1360  }
1361  }
1362 
1363  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",
1364  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,
1365  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);
1366 
1367  if (!do_adj && (switch_cmp_addr(from_addr, ice->addr, SWITCH_TRUE) || use_candidate)) {
1368  do_adj++;
1369  rtp_session->last_adj = now;
1370 
1371  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",
1372  switch_channel_get_name(channel), rtp_type(rtp_session), is_rtcp ? "rtcp" : "rtp", from_host, from_port, is_relay, is_responsive, use_candidate,
1373  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);
1374  }
1375  }
1376 
1377  if (cmp) {
1378  ice->last_ok = now;
1379  } else if (!do_adj) {
1380  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));
1381 
1382  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",
1383  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,
1384  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);
1385 
1386  if (rtp_session->elapsed_adj > 1000) {
1387  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",
1388  switch_channel_get_name(channel), rtp_type(rtp_session), is_rtcp ? "rtcp" : "rtp", rtp_session->elapsed_stun, rtp_session->elapsed_media >= MEDIA_TOO_LONG,
1389  rtp_session->elapsed_stun >= STUN_TOO_LONG, from_host, from_port, is_relay, is_responsive, use_candidate,
1390  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);
1391 
1392  if (!is_relay && ((rtp_session->dtls && rtp_session->dtls->state != DS_READY) ||
1393  ((!ice->ready || !ice->rready) && (rtp_session->elapsed_stun >= 3000 || switch_cmp_addr(from_addr, ice->addr, SWITCH_TRUE))))) {
1394 
1395  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",
1396  switch_channel_get_name(channel), rtp_type(rtp_session), is_rtcp ? "rtcp" : "rtp", from_host, from_port, is_relay, is_responsive, use_candidate,
1397  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);
1398 
1399  do_adj++;
1400  rtp_session->last_adj = now;
1401  } else if (is_relay && ice->initializing && rtp_session->elapsed_stun >= 1000) {
1402 
1403  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",
1404  switch_channel_get_name(channel), rtp_type(rtp_session), is_rtcp ? "rtcp" : "rtp", from_host, from_port, is_relay, is_responsive, use_candidate,
1405  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);
1406 
1407  do_adj++;
1408  rtp_session->last_adj = now;
1409  } else if ((ice->initializing && rtp_session->elapsed_stun >= 3000) ||
1410  (rtp_session->elapsed_media >= MEDIA_TOO_LONG || rtp_session->elapsed_stun >= STUN_TOO_LONG)) {
1411 
1412  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",
1413  switch_channel_get_name(channel), rtp_type(rtp_session), is_rtcp ? "rtcp" : "rtp", from_host, from_port, is_relay, is_responsive, use_candidate,
1414  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);
1415 
1416  do_adj++;
1417  rtp_session->last_adj = now;
1418  }
1419 
1420  for (i = 0; i < ice->ice_params->cand_idx[ice->proto]; i++) {
1421  if (!strcmp(ice->ice_params->cands[i][ice->proto].con_addr, from_host)) {
1422  cur_idx = i;
1423  }
1424  }
1425  }
1426  }
1427 
1428  if ((ice->type & ICE_VANILLA) && ice->ice_params && do_adj) {
1429  ice->missed_count = 0;
1430  ice->rready = 1;
1431 
1432  if (cur_idx > -1) {
1433  ice->ice_params->chosen[ice->proto] = cur_idx;
1434  }
1435 
1437  "Auto Changing %s stun/%s/dtls port from %s:%u to %s:%u idx:%d\n", rtp_type(rtp_session), is_rtcp ? "rtcp" : "rtp",
1438  host2, port2,
1439  from_host, from_port, cur_idx);
1440 
1441  switch_rtp_change_ice_dest(rtp_session, ice, from_host, from_port);
1442 
1443  ice->cand_responsive = is_responsive;
1444  if (ice->cand_responsive) {
1445  ice->initializing = 0;
1446  }
1447 
1448  ice->last_ok = now;
1449  }
1450  //if (cmp) {
1451  switch_socket_sendto(sock_output, from_addr, 0, (void *) rpacket, &bytes);
1452  //}
1453 
1454  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);
1455 
1456  if (ice->initializing && !is_responsive) {
1457  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);
1458  if (ice_out(rtp_session, ice, SWITCH_TRUE) != SWITCH_STATUS_SUCCESS) {
1459  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);
1460  }
1461  }
1462  }
1463  } else if (packet->header.type == SWITCH_STUN_BINDING_ERROR_RESPONSE) {
1464 
1465  if (rtp_session->session) {
1466  switch_core_session_message_t msg = { 0 };
1467  msg.from = __FILE__;
1468  msg.numeric_arg = packet->header.type;
1469  msg.pointer_arg = packet;
1471  switch_core_session_receive_message(rtp_session->session, &msg);
1473  "STUN/ICE binding error received on %s channel\n", rtp_type(rtp_session));
1474  }
1475 
1476  }
1477 
1478 
1479 
1480 
1481  end:
1482  switch_mutex_unlock(rtp_session->ice_mutex);
1483  WRITE_DEC(rtp_session);
1484  READ_DEC(rtp_session);
1485 }
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:5147
switch_sockaddr_t * rtcp_from_addr
Definition: switch_rtp.c:394
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:559
switch_sockaddr_t * remote_addr
Definition: switch_rtp.c:340
uint32_t elapsed_adj
Definition: switch_rtp.c:481
switch_sockaddr_t * addr
Definition: switch_rtp.c:248
#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:417
ice_t * ice_params
Definition: switch_rtp.c:252
switch_time_t last_ok
Definition: switch_rtp.c:260
#define rtp_session_name(_rtp_session)
Definition: switch_rtp.c:85
uint32_t elapsed_stun
Definition: switch_rtp.c:479
uint8_t initializing
Definition: switch_rtp.c:257
#define STUN_TOO_LONG
Definition: switch_rtp.c:820
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:438
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:280
#define rtp_type(rtp_session)
Definition: switch_rtp.c:540
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:623
int64_t switch_time_t
Definition: switch_apr.h:188
switch_core_media_ice_type_t type
Definition: switch_rtp.c:251
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:943
static void calc_elapsed(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice)
Definition: switch_rtp.c:823
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:392
#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:286
#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:277
uint8_t cand_responsive
Definition: switch_rtp.c:261
char * con_addr
Definition: switch_rtp.h:99
uint32_t elapsed_media
Definition: switch_rtp.c:480
#define READ_INC(rtp_session)
Definition: switch_rtp.c:67
switch_byte_t auto_adj_used
Definition: switch_rtp.c:463
#define switch_stun_packet_length(packet)
Obtain the correct length in bytes of a stun packet.
Definition: switch_stun.h:304
char last_sent_id[13]
Definition: switch_rtp.c:259
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:366
switch_rtp_ice_t rtcp_ice
Definition: switch_rtp.c:398
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:3125
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:393
struct fspr_sockaddr_t switch_sockaddr_t
Definition: switch_apr.h:1029
switch_time_t last_adj
Definition: switch_rtp.c:477
switch_socket_t * rtcp_sock_output
Definition: switch_rtp.c:322
void switch_rtp_clear_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Clear an RTP Flag.
Definition: switch_rtp.c:5431
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:295
void switch_rtp_set_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Set an RTP Flag.
Definition: switch_rtp.c:5372
#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:430
ice_proto_t proto
Definition: switch_rtp.c:253
#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:429
#define MEDIA_TOO_LONG
Definition: switch_rtp.c:819
void switch_core_session_video_reinit(switch_core_session_t *session)
switch_core_session_t * session
Definition: switch_rtp.c:472
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:478
switch_rtp_ice_t ice
Definition: switch_rtp.c:397
switch_rtp_bug_flag_t rtp_bugs
Definition: switch_rtp.c:449
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:492
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:404
struct fspr_socket_t switch_socket_t
Definition: switch_apr.h:1026
switch_sockaddr_t * from_addr
Definition: switch_rtp.c:394
switch_time_t first_stun
Definition: switch_rtp.c:403
#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:853
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:543
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:322
uint8_t switch_stun_packet_attribute_add_integrity(switch_stun_packet_t *packet, const char *pass)
Definition: switch_stun.c:542
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 6689 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().

6690 {
6691  switch_size_t bytes = 0;
6692  rtp_msg_t send_msg[1] = {{{0}}};
6693  uint16_t seq = (uint16_t) (nack & 0xFFFF);
6694  uint16_t blp = (uint16_t) (nack >> 16);
6695  int i;
6696  const char *tx_host = NULL;
6697  const char *old_host = NULL;
6698  const char *my_host = NULL;
6699  char bufa[50], bufb[50], bufc[50];
6700 
6701  if (!(rtp_session->flags[SWITCH_RTP_FLAG_NACK] && rtp_session->vbw)) {
6702  return; /* not enabled */
6703  }
6704 
6705  if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]) {
6706  tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtcp_from_addr);
6707  old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
6708  my_host = switch_get_addr(bufc, sizeof(bufc), rtp_session->local_addr);
6709  }
6710 
6711  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "%s Got NACK [%u][0x%x] for seq %u\n",
6712  switch_core_session_get_name(rtp_session->session), nack, nack, ntohs(seq));
6713 
6714  if (switch_jb_get_packet_by_seq(rtp_session->vbw, seq, (switch_rtp_packet_t *) send_msg, &bytes) == SWITCH_STATUS_SUCCESS) {
6715 
6716  if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]) {
6718  "X %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n",
6719  rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "NoName",
6720  (long) bytes,
6721  my_host, switch_sockaddr_get_port(rtp_session->local_addr),
6722  old_host, rtp_session->remote_port,
6723  tx_host, switch_sockaddr_get_port(rtp_session->rtcp_from_addr),
6724  send_msg->header.pt, ntohl(send_msg->header.ts), ntohs(send_msg->header.seq), send_msg->header.m);
6725 
6726  }
6727  //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG2, "RE----SEND %u\n", ntohs(send_msg->header.seq));
6728  switch_rtp_write_raw(rtp_session, (void *) send_msg, &bytes, SWITCH_FALSE);
6729  } else {
6730  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "Cannot send NACK for seq %u\n", ntohs(seq));
6731  }
6732 
6733  blp = ntohs(blp);
6734  for (i = 0; i < 16; i++) {
6735  if (blp & (1 << i)) {
6736  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "%s Also Got NACK for seq %u\n",
6737  switch_core_session_get_name(rtp_session->session), ntohs(seq) + i + 1);
6738  /* If they are missing more than one, may as well gen a key frame for good measure */
6739  //switch_core_media_gen_key_frame(rtp_session->session);
6740  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) {
6741  if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]) {
6743  "X %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n",
6744  rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "NoName",
6745  (long) bytes,
6746  my_host, switch_sockaddr_get_port(rtp_session->local_addr),
6747  old_host, rtp_session->remote_port,
6748  tx_host, switch_sockaddr_get_port(rtp_session->rtcp_from_addr),
6749  send_msg->header.pt, ntohl(send_msg->header.ts), ntohs(send_msg->header.seq), send_msg->header.m);
6750 
6751  }
6752  //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "RE----SEND %u\n", ntohs(send_msg->header.seq));
6753 
6754  switch_rtp_write_raw(rtp_session, (void *) &send_msg, &bytes, SWITCH_FALSE);
6755  } else {
6756  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "Cannot send NACK for seq %u\n", ntohs(seq) + i);
6757  }
6758  }
6759  }
6760 }
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:394
switch_sockaddr_t * remote_addr
Definition: switch_rtp.c:340
switch_rtp_hdr_t header
Definition: switch_rtp.h:56
switch_port_t remote_port
Definition: switch_rtp.c:417
switch_jb_t * vbw
Definition: switch_rtp.c:436
#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:392
uintptr_t switch_size_t
switch_sockaddr_t * local_addr
Definition: switch_rtp.c:326
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:9158
#define SWITCH_CHANNEL_SESSION_LOG_CLEAN(x)
switch_core_session_t * session
Definition: switch_rtp.c:472
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 582 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().

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

854 {
855  uint8_t buf[256] = { 0 };
856  switch_stun_packet_t *packet;
857  unsigned int elapsed;
858  switch_size_t bytes;
860  //switch_sockaddr_t *remote_addr = rtp_session->remote_addr;
861  switch_socket_t *sock_output = rtp_session->sock_output;
863 
864  if (ice->type & ICE_LITE) {
865  // no connectivity checks for ICE-Lite
866  return SWITCH_STATUS_BREAK;
867  }
868 
869  if (!force && ice->next_run && ice->next_run >= now) {
870  return SWITCH_STATUS_BREAK;
871  }
872 
873  ice->next_run = now + RTP_STUN_FREQ;
874 
875  if (ice == &rtp_session->rtcp_ice && rtp_session->rtcp_sock_output) {
876  sock_output = rtp_session->rtcp_sock_output;
877  }
878 
879  if (!sock_output) {
880  return SWITCH_STATUS_FALSE;
881  }
882 
883  switch_assert(rtp_session != NULL);
884  switch_assert(ice->ice_user != NULL);
885 
886  READ_INC(rtp_session);
887 
888  if (rtp_session->last_stun) {
889  elapsed = (unsigned int) ((switch_micro_time_now() - rtp_session->last_stun) / 1000);
890 
891  if (elapsed > 30000) {
892  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));
893  rtp_session->last_stun = switch_micro_time_now();
894  //status = SWITCH_STATUS_GENERR;
895  //goto end;
896  }
897  }
898 
900  switch_stun_packet_attribute_add_username(packet, ice->ice_user, (uint16_t)strlen(ice->ice_user));
901 
902  memcpy(ice->last_sent_id, packet->header.id, 12);
903 
904  //if (ice->pass && ice->type == ICE_GOOGLE_JINGLE) {
905  // switch_stun_packet_attribute_add_password(packet, ice->pass, (uint16_t)strlen(ice->pass));
906  //}
907 
908  if ((ice->type & ICE_VANILLA)) {
909  char sw[128] = "";
910 
912 
913  switch_snprintf(sw, sizeof(sw), "FreeSWITCH (%s)", switch_version_revision_human());
914  switch_stun_packet_attribute_add_software(packet, sw, (uint16_t)strlen(sw));
915 
916  if ((ice->type & ICE_CONTROLLED)) {
918  } else {
921  }
922 
925  }
926 
927 
928  bytes = switch_stun_packet_length(packet);
929 
930 #ifdef DEBUG_EXTRA
931  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));
932 #endif
933  switch_socket_sendto(sock_output, ice->addr, 0, (void *) packet, &bytes);
934 
935  ice->sending = 3;
936 
937  // end:
938  READ_DEC(rtp_session);
939 
940  return status;
941 }
uint8_t switch_stun_packet_attribute_add_software(switch_stun_packet_t *packet, char *software, uint16_t ulen)
Definition: switch_stun.c:647
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:528
#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:559
uint8_t switch_stun_packet_attribute_add_controlling(switch_stun_packet_t *packet)
Definition: switch_stun.c:592
switch_sockaddr_t * addr
Definition: switch_rtp.c:248
ice_t * ice_params
Definition: switch_rtp.c:252
#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:250
#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:438
#define rtp_type(rtp_session)
Definition: switch_rtp.c:540
uint8_t switch_stun_packet_attribute_add_use_candidate(switch_stun_packet_t *packet)
Definition: switch_stun.c:581
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:623
int64_t switch_time_t
Definition: switch_apr.h:188
switch_core_media_ice_type_t type
Definition: switch_rtp.c:251
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:304
char last_sent_id[13]
Definition: switch_rtp.c:259
uintptr_t switch_size_t
switch_rtp_ice_t rtcp_ice
Definition: switch_rtp.c:398
switch_socket_t * rtcp_sock_output
Definition: switch_rtp.c:322
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:253
switch_core_session_t * session
Definition: switch_rtp.c:472
uint8_t switch_stun_packet_attribute_add_controlled(switch_stun_packet_t *packet)
Definition: switch_stun.c:607
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:404
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:322
uint8_t switch_stun_packet_attribute_add_integrity(switch_stun_packet_t *packet, const char *pass)
Definition: switch_stun.c:542

◆ icecmp()

int icecmp ( const char *  them,
switch_rtp_ice_t ice 
)

Definition at line 943 of file switch_rtp.c.

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

Referenced by handle_ice().

944 {
945  if (strchr(them, ':')) {
946  return strcmp(them, ice->user_ice);
947  }
948 
949  return strcmp(them, ice->luser_ice);
950 }

◆ jb_valid()

static int jb_valid ( switch_rtp_t rtp_session)
static

Definition at line 5662 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().

5663 {
5664  if (rtp_session->ice.ice_user) {
5665  if (!rtp_session->ice.ready && rtp_session->ice.rready) {
5666  return 0;
5667  }
5668  }
5669 
5670  if (rtp_session->dtls && rtp_session->dtls->state != DS_READY) {
5671  return 0;
5672  }
5673 
5674  return 1;
5675 }
dtls_state_t state
Definition: switch_rtp.c:280
switch_dtls_t * dtls
Definition: switch_rtp.c:366
switch_rtp_ice_t ice
Definition: switch_rtp.c:397

◆ ping_socket()

static void ping_socket ( switch_rtp_t rtp_session)
static

Definition at line 3044 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().

3045 {
3046  uint32_t o = UINT_MAX;
3047  switch_size_t len = sizeof(o);
3048  switch_socket_sendto(rtp_session->sock_input, rtp_session->local_addr, 0, (void *) &o, &len);
3049 
3050  if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] && rtp_session->rtcp_sock_input && rtp_session->rtcp_sock_input != rtp_session->sock_input) {
3051  switch_socket_sendto(rtp_session->rtcp_sock_input, rtp_session->rtcp_local_addr, 0, (void *) &o, &len);
3052  }
3053 }
switch_sockaddr_t * rtcp_local_addr
Definition: switch_rtp.c:326
switch_socket_t * sock_input
Definition: switch_rtp.c:322
switch_socket_t * rtcp_sock_input
Definition: switch_rtp.c:322
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:392
uintptr_t switch_size_t
switch_sockaddr_t * local_addr
Definition: switch_rtp.c:326

◆ process_rtcp_packet()

static switch_status_t process_rtcp_packet ( switch_rtp_t rtp_session,
switch_size_t bytes 
)
static

Definition at line 7099 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().

7100 {
7101  switch_size_t len;
7102  switch_size_t remain = *bytes;
7104  rtcp_msg_t *msg = rtp_session->rtcp_recv_msg_p;
7105 
7106  if (remain < sizeof(switch_rtcp_ext_hdr_t) || remain > sizeof(rtcp_msg_t)) {
7107  return status;
7108  }
7109  if (msg->header.version != 2) {
7110  if (msg->header.version == 0) {
7111  if (rtp_session->ice.ice_user) {
7112  handle_ice(rtp_session, &rtp_session->rtcp_ice, (void *) msg, *bytes);
7113  }
7114  return SWITCH_STATUS_SUCCESS;
7115  } else {
7117  SWITCH_LOG_WARNING, "Received an unsupported RTCP packet version %d\n", msg->header.version);
7118  return SWITCH_STATUS_FALSE;
7119  }
7120  }
7121 
7122  do {
7123  len = ((switch_size_t)ntohs(msg->header.length) * 4) + 4;
7124 
7125  if (msg->header.version != 2 || !(msg->header.type > 191 && msg->header.type < 210)) {
7127  "INVALID RTCP PACKET TYPE %d VER %d LEN %" SWITCH_SIZE_T_FMT "\n", msg->header.type,
7128  msg->header.version, len);
7129  status = SWITCH_STATUS_BREAK;
7130  break;
7131  }
7132 
7133  //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_CRIT,
7134  //"WTF BYTES %ld REMAIN %ld PACKET TYPE %d LEN %ld\n", *bytes, remain, msg->header.type, len);
7135 
7136  if (len > remain) {
7138  "RTCP INVALID LENGTH %" SWITCH_SIZE_T_FMT "\n", len);
7139  len = remain;
7140  }
7141 
7142  status = process_rtcp_report(rtp_session, msg, len);
7143 
7144  if (remain > len) {
7145  unsigned char *p = (unsigned char *) msg;
7146  p += len;
7147  msg = (rtcp_msg_t *) p;
7148  }
7149 
7150  remain -= len;
7151 
7152  } while (remain >= 4);
7153 
7154  return status;
7155 }
#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:398
static void handle_ice(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice, void *data, switch_size_t len)
Definition: switch_rtp.c:952
#define SWITCH_SIZE_T_FMT
switch_status_t
Common return values.
switch_core_session_t * session
Definition: switch_rtp.c:472
switch_rtp_ice_t ice
Definition: switch_rtp.c:397
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:343
static switch_status_t process_rtcp_report(switch_rtp_t *rtp_session, rtcp_msg_t *msg, switch_size_t bytes)
Definition: switch_rtp.c:6762

◆ 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 6762 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().

6763 {
6765 
6767  "RTCP packet bytes %" SWITCH_SIZE_T_FMT " type %d pad %d\n",
6768  bytes, msg->header.type, msg->header.p);
6769 
6770  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && (msg->header.type == _RTCP_PT_RTPFB || msg->header.type == _RTCP_PT_PSFB || msg->header.type < 200)) {
6771  rtcp_ext_msg_t *extp = (rtcp_ext_msg_t *) msg;
6772 
6773  if (extp->header.fmt != 15) { // <---- REMOVE WHEN BRIA STOPS SENDING UNSOLICITED REMB
6774  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "%s PICKED UP %s XRTCP type: %d fmt: %d\n",
6775  switch_core_session_get_name(rtp_session->session), rtp_type(rtp_session), msg->header.type, extp->header.fmt);
6776  }
6777 
6778  if (msg->header.type == _RTCP_PT_FIR ||
6779  (msg->header.type == _RTCP_PT_PSFB && (extp->header.fmt == _RTCP_PSFB_FIR || extp->header.fmt == _RTCP_PSFB_PLI))) {
6780 #if 0
6781  if (msg->header.type == _RTCP_PT_FIR) {
6782  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "Ancient FIR Received. Hello from 1996!\n");
6783 
6784  if (!switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_OLD_FIR)) {
6787  }
6788  }
6789 #endif
6790 
6792  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "%s Ignoring FIR/PLI from a sendonly stream.\n",
6793  switch_core_session_get_name(rtp_session->session));
6794  } else {
6797  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "%s Got FIR/PLI\n",
6798  switch_core_session_get_name(rtp_session->session));
6800  }
6801  }
6802 
6803  if (msg->header.type == _RTCP_PT_RTPFB && extp->header.fmt == _RTCP_RTPFB_NACK) {
6804  uint32_t *nack = (uint32_t *) extp->body;
6805  int i;
6806 
6807  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG2, "%s Got NACK count %d\n",
6808  switch_core_session_get_name(rtp_session->session), ntohs(extp->header.length) - 2);
6809 
6810 
6811  for (i = 0; i < ntohs(extp->header.length) - 2; i++) {
6812  handle_nack(rtp_session, nack[i]);
6813  }
6814 
6815  //switch_core_media_gen_key_frame(rtp_session->session);
6816  }
6817 
6818  } else {
6819  struct switch_rtcp_report_block *report;
6820 
6821  if (msg->header.type == _RTCP_PT_SR || msg->header.type == _RTCP_PT_RR) {
6822  int i;
6823 #ifdef DEBUG_RTCP
6825 #endif
6826  uint32_t lsr_now;
6827  uint32_t lsr;
6828  uint32_t packet_ssrc;
6829  double rtt_now = 0;
6830  uint8_t rtt_valid = 0;
6831  int rtt_increase = 0, packet_loss_increase=0;
6832 
6833  //if (msg->header.type == _RTCP_PT_SR && rtp_session->ice.ice_user) {
6834  // rtp_session->send_rr = 1;
6835  //}
6836 
6837  lsr_now = calc_local_lsr_now();
6838 
6839  if (msg->header.type == _RTCP_PT_SR) { /* Sender report */
6840  struct switch_rtcp_sender_report* sr = (struct switch_rtcp_sender_report*)msg->body;
6841 
6842  rtp_session->stats.rtcp.packet_count = ntohl(sr->sender_info.pc);
6843  rtp_session->stats.rtcp.octet_count = ntohl(sr->sender_info.oc);
6844  packet_ssrc = sr->ssrc;
6845  /* Extracting LSR from NTP timestamp and save it */
6846  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 */
6847  rtp_session->stats.rtcp.last_recv_lsr_peer = htonl(lsr); /* Save it include it in the next SR */
6848  rtp_session->stats.rtcp.last_recv_lsr_local = lsr_now; /* Save it to calculate DLSR when generating next SR */
6849  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3,"Received a SR with %d report blocks, " \
6850  "length in words = %d, " \
6851  "SSRC = 0x%X, " \
6852  "NTP MSW = %u, " \
6853  "NTP LSW = %u, " \
6854  "RTP timestamp = %u, " \
6855  "Sender Packet Count = %u, " \
6856  "Sender Octet Count = %u\n",
6857  msg->header.count,
6858  ntohs((uint16_t)msg->header.length),
6859  ntohl(sr->ssrc),
6860  ntohl(sr->sender_info.ntp_msw),
6861  ntohl(sr->sender_info.ntp_lsw),
6862  ntohl(sr->sender_info.ts),
6863  ntohl(sr->sender_info.pc),
6864  ntohl(sr->sender_info.oc));
6865 
6866 
6867  rtp_session->rtcp_frame.ssrc = ntohl(sr->ssrc);
6868  rtp_session->rtcp_frame.packet_type = (uint16_t)rtp_session->rtcp_recv_msg_p->header.type;
6869  rtp_session->rtcp_frame.ntp_msw = ntohl(sr->sender_info.ntp_msw);
6870  rtp_session->rtcp_frame.ntp_lsw = ntohl(sr->sender_info.ntp_lsw);
6871  rtp_session->rtcp_frame.timestamp = ntohl(sr->sender_info.ts);
6872  rtp_session->rtcp_frame.packet_count = ntohl(sr->sender_info.pc);
6873  rtp_session->rtcp_frame.octect_count = ntohl(sr->sender_info.oc);
6874 
6875  report = &sr->report_block;
6876  } else { /* Receiver report */
6878  packet_ssrc = rr->ssrc;
6879  //memset(&rtp_session->rtcp_frame, 0, sizeof(rtp_session->rtcp_frame));
6880  report = &rr->report_block;
6881 
6882  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3,"Received a RR with %d report blocks, " \
6883  "length in words = %d, " \
6884  "SSRC = 0x%X, ",
6885  msg->header.count,
6886  ntohs((uint16_t)msg->header.length),
6887  ntohl(rr->ssrc));
6888 
6889  }
6890 
6891 
6892  for (i = 0; i < (int)msg->header.count && i < MAX_REPORT_BLOCKS ; i++) {
6893  uint32_t old_avg = rtp_session->rtcp_frame.reports[i].loss_avg;
6894  uint8_t percent_fraction = (uint8_t)((uint16_t/* prevent overflow when '* 100' */)(uint8_t)report->fraction * 100 / 255);
6895  if (!rtp_session->rtcp_frame.reports[i].loss_avg) {
6896  rtp_session->rtcp_frame.reports[i].loss_avg = percent_fraction;
6897  } else {
6898  rtp_session->rtcp_frame.reports[i].loss_avg = (uint32_t)(((float)rtp_session->rtcp_frame.reports[i].loss_avg * .7) +
6899  ((float)percent_fraction * .3));
6900  }
6901 
6902  rtp_session->rtcp_frame.reports[i].ssrc = ntohl(report->ssrc);
6903  rtp_session->rtcp_frame.reports[i].fraction = (uint8_t)report->fraction;
6904 #if SWITCH_BYTE_ORDER == __BIG_ENDIAN
6905  rtp_session->rtcp_frame.reports[i].lost = report->lost; // signed 24bit will extended signess to int32_t automatically
6906 #else
6907  rtp_session->rtcp_frame.reports[i].lost = ntohl(report->lost)>>8; // signed 24bit casted to uint32_t need >>8 after ntohl()...
6908  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
6909 #endif
6911  rtp_session->rtcp_frame.reports[i].jitter = ntohl(report->jitter);
6912  rtp_session->rtcp_frame.reports[i].lsr = ntohl(report->lsr);
6913  rtp_session->rtcp_frame.reports[i].dlsr = ntohl(report->dlsr);
6914 
6915  if (rtp_session->rtcp_frame.reports[i].lsr && !rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU]) {
6916 
6917  /* Calculating RTT = A - DLSR - LSR */
6918  rtt_now = ((double)(((int64_t)lsr_now) - rtp_session->rtcp_frame.reports[i].dlsr - rtp_session->rtcp_frame.reports[i].lsr))/65536;
6919 
6920  /* Only account RTT if it didn't overflow. */
6921  if (lsr_now > rtp_session->rtcp_frame.reports[i].dlsr + rtp_session->rtcp_frame.reports[i].lsr) {
6922 #ifdef DEBUG_RTCP
6923  switch_time_exp_t now_hr;
6924  switch_time_exp_gmt(&now_hr,now);
6926  "Receiving an RTCP packet\n[%04d-%02d-%02d %02d:%02d:%02d.%d] SSRC[0x%x]\n"
6927  "RTT[%f] = A[%u] - DLSR[%u] - LSR[%u]\n",
6928  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,
6929  rtp_session->rtcp_frame.reports[i].ssrc, rtt_now,
6930  lsr_now, rtp_session->rtcp_frame.reports[i].dlsr, rtp_session->rtcp_frame.reports[i].lsr);
6931 #endif
6932  rtt_valid = 1;
6933  if (!rtp_session->rtcp_frame.reports[i].rtt_avg) {
6934  rtp_session->rtcp_frame.reports[i].rtt_avg = rtt_now;
6935  } else {
6936  rtp_session->rtcp_frame.reports[i].rtt_avg = (double)((rtp_session->rtcp_frame.reports[i].rtt_avg * .7) + (rtt_now * .3 ));
6937  }
6938  } else {
6939 #ifdef DEBUG_RTCP
6940  switch_time_exp_t now_hr;
6941  switch_time_exp_gmt(&now_hr,now);
6943  "Receiving RTCP packet\n[%04d-%02d-%02d %02d:%02d:%02d.%d] SSRC[0x%x]\n"
6944  "Ignoring erroneous RTT[%f] = A[%u] - DLSR[%u] - LSR[%u]\n",
6945  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,
6946  rtp_session->rtcp_frame.reports[i].ssrc, rtt_now,
6947  lsr_now, rtp_session->rtcp_frame.reports[i].dlsr, rtp_session->rtcp_frame.reports[i].lsr);
6948 #endif
6949  rtt_valid = 0;
6950  rtt_now = 0;
6951  }
6952 
6953 
6954  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "RTT average %f\n",
6955  rtp_session->rtcp_frame.reports[i].rtt_avg);
6956  }
6957 
6958  if (rtp_session->flags[SWITCH_RTP_FLAG_ADJ_BITRATE_CAP] && rtp_session->flags[SWITCH_RTP_FLAG_ESTIMATORS] && !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
6959 
6960  /* SWITCH_RTP_FLAG_ADJ_BITRATE_CAP : Can the codec change its bitrate on the fly per API command ? */
6961 #ifdef DEBUG_ESTIMATORS_
6962  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);
6963 #endif
6964 
6965  if (rtt_valid) {
6966 
6967  switch_kalman_estimate(rtp_session->estimators[EST_RTT], rtt_now, EST_RTT);
6968 
6969  if (switch_kalman_cusum_detect_change(rtp_session->detectors[EST_RTT], rtt_now, rtp_session->estimators[EST_RTT]->val_estimate_last)) {
6970  /* sudden change in the mean value of RTT */
6971 #ifdef DEBUG_ESTIMATORS_
6972  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3,"Sudden change in the mean value of RTT !\n");
6973 #endif
6974  rtt_increase = 1;
6975  }
6976  }
6977 
6978  switch_kalman_estimate(rtp_session->estimators[EST_LOSS], percent_fraction, EST_LOSS);
6979 
6980  if (switch_kalman_cusum_detect_change(rtp_session->detectors[EST_LOSS], percent_fraction, rtp_session->estimators[EST_LOSS]->val_estimate_last)){
6981  /* sudden change in the mean value of packet loss */
6982 #ifdef DEBUG_ESTIMATORS_
6983  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3,"Sudden change in the mean value of packet loss!\n");
6984 #endif
6985  packet_loss_increase = 1;
6986  }
6987 #ifdef DEBUG_ESTIMATORS_
6988  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",
6989  rtp_session->estimators[EST_LOSS]->val_estimate_last, rtp_session->estimators[EST_RTT]->val_estimate_last);
6990 #endif
6991 
6992  if (rtp_session->rtcp_frame.reports[i].loss_avg != old_avg) {
6993  /*getting bad*/
6995  rtp_session->estimators[EST_RTT])) {
6996  /* going to minimum bitrate */
6997 #ifdef DEBUG_ESTIMATORS_
6998  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "Slow link conditions: Loss average: [%d %%], Previous loss: [%d %%]. \
6999  Going to minimum bitrate!",rtp_session->rtcp_frame.reports[i].loss_avg, old_avg);
7000 #endif
7002  SWITCH_IO_WRITE, SCC_AUDIO_ADJUST_BITRATE, SCCT_STRING, "minimum", SCCT_NONE, NULL, NULL, NULL);
7003  /* if after going to minimum bitrate we still have packet loss then we increase ptime. TODO */
7004 
7005  } else if (packet_loss_increase && (rtp_session->estimators[EST_LOSS]->val_estimate_last >= 5)) {
7006  /* sudden change in the mean value of packet loss percentage */
7009  SCCT_STRING, "decrease",
7010  SCCT_NONE, NULL, NULL, NULL);
7011 #ifdef DEBUG_ESTIMATORS_
7012  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3,"Sudden change in the mean value of packet loss percentage !\n");
7013 #endif
7016  (void *)&rtp_session->rtcp_frame.reports[i].loss_avg,
7017  SCCT_NONE, NULL, NULL, NULL);
7018 
7019  } else if (rtt_valid && !rtt_increase && rtp_session->estimators[EST_LOSS]->val_estimate_last >= rtp_session->rtcp_frame.reports[i].loss_avg ) {
7020  /* lossy because of congestion (queues full somewhere -> some packets are dropped , but RTT is good ), packet loss with many small gaps */
7021 #ifdef DEBUG_ESTIMATORS_
7022  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "packet loss, but RTT is not bad\n");
7023 #endif
7026  (void *)&rtp_session->rtcp_frame.reports[i].loss_avg,
7027  SCCT_NONE, NULL, NULL, NULL);
7028 
7029  } else if ((rtp_session->estimators[EST_LOSS]->val_estimate_last < 1) && packet_loss_increase) {
7030 #ifdef DEBUG_ESTIMATORS_
7031  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "small packet loss average\n");
7032 #endif
7033  /*small loss_avg*/
7036  SCCT_STRING, "default",
7037  SCCT_NONE, NULL, NULL, NULL);
7038 
7041  (void *)&rtp_session->rtcp_frame.reports[i].loss_avg,
7042  SCCT_NONE, NULL, NULL, NULL);
7043 
7044  } else if ((rtp_session->estimators[EST_LOSS]->val_estimate_last < 5) &&
7045  (rtp_session->rtcp_frame.reports[i].rtt_avg < rtp_session->estimators[EST_RTT]->val_estimate_last)) {
7046 
7047  /* estimate that packet loss will decrease, we can increase the bitrate */
7050  SCCT_STRING, "increase",
7051  SCCT_NONE, NULL, NULL, NULL);
7052 
7055  (void *)&rtp_session->rtcp_frame.reports[i].loss_avg,
7056  SCCT_NONE, NULL, NULL, NULL);
7057 
7058  } else {
7059  /* *do nothing about bitrate, just pass the packet loss to the codec */
7060 #ifdef DEBUG_ESTIMATORS_
7061  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");
7062 #endif
7065  (void *)&rtp_session->rtcp_frame.reports[i].loss_avg,
7066  SCCT_NONE, NULL, NULL, NULL);
7067  }
7068  }
7069  } else {
7070  if (!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->rtcp_frame.reports[i].loss_avg != old_avg) {
7073  (void *)&rtp_session->rtcp_frame.reports[i].loss_avg,
7074  SCCT_NONE, NULL, NULL, NULL);
7075  }
7076  }
7077 
7078  report++;
7079  }
7080  rtp_session->rtcp_frame.report_count = (uint16_t)i;
7081 
7082 
7083 
7084 
7085 
7086  rtp_session->rtcp_fresh_frame = 1;
7087  rtp_session->stats.rtcp.peer_ssrc = ntohl(packet_ssrc);
7088  }
7089  }
7090 
7091  if (msg->header.type > 194 && msg->header.type < 255) {
7092  status = SWITCH_STATUS_SUCCESS;
7093  }
7094 
7095  return status;
7096 }
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:527
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:460
#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:496
#define rtp_type(rtp_session)
Definition: switch_rtp.c:540
_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:762
struct switch_rtcp_sender_info sender_info
Definition: switch_rtp.c:521
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:392
#define switch_core_session_request_video_refresh(_s)
Definition: switch_core.h:2881
#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:475
switch_rtp_stats_t stats
Definition: switch_rtp.c:450
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:329
void switch_rtp_set_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Set an RTP Flag.
Definition: switch_rtp.c:5372
static void handle_nack(switch_rtp_t *rtp_session, uint32_t nack)
Definition: switch_rtp.c:6689
#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:739
switch_core_session_t * session
Definition: switch_rtp.c:472
#define switch_channel_set_flag(_c, _f)
struct switch_rtcp_report_block report_block
Definition: switch_rtp.c:522
static uint32_t calc_local_lsr_now(void)
Definition: switch_rtp.c:1819
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:5426
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:476
rtcp_msg_t * rtcp_recv_msg_p
Definition: switch_rtp.c:343

◆ 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 5847 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().

5848 {
5851 
5852  if (!rtp_session) {
5853  return SWITCH_STATUS_GENERR;
5854  }
5855 
5856  if (*bytes && rtp_bundle_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_READ]) {
5857  const char *tx_host;
5858  const char *old_host;
5859  const char *my_host;
5860 
5861  char bufa[50], bufb[50], bufc[50];
5862 
5863 
5864  tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtp_from_addr);
5865  old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
5866  my_host = switch_get_addr(bufc, sizeof(bufc), rtp_session->local_addr);
5867 
5869  "R %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n",
5870  rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "No-Name",
5871  (long) *bytes,
5872  my_host, switch_sockaddr_get_port(rtp_session->local_addr),
5873  old_host, rtp_session->remote_port,
5874  tx_host, switch_sockaddr_get_port(rtp_session->rtp_from_addr),
5875  rtp_session->last_rtp_hdr.pt, ntohl(rtp_session->last_rtp_hdr.ts), ntohs(rtp_session->last_rtp_hdr.seq),
5876  rtp_session->last_rtp_hdr.m);
5877  }
5878 
5879  if (!rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
5880 #ifdef ENABLE_SRTP
5881  switch_mutex_lock(rtp_session->ice_mutex);
5882  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && rtp_session->has_rtp) {
5883  //if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && (!rtp_session->ice.ice_user || rtp_session->has_rtp)) {
5884  int sbytes = (int) *bytes;
5885  srtp_err_status_t stat = 0;
5886 
5887  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_RESET] || !rtp_session->recv_ctx[rtp_session->srtp_idx_rtp]) {
5889  srtp_dealloc(rtp_session->recv_ctx[rtp_session->srtp_idx_rtp]);
5890  rtp_session->recv_ctx[rtp_session->srtp_idx_rtp] = NULL;
5891  if ((stat = srtp_create(&rtp_session->recv_ctx[rtp_session->srtp_idx_rtp],
5892  &rtp_session->recv_policy[rtp_session->srtp_idx_rtp])) || !rtp_session->recv_ctx[rtp_session->srtp_idx_rtp]) {
5893 
5894  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] = 0;
5895  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error! RE-Activating Secure RTP RECV\n");
5896  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] = 0;
5897  switch_mutex_unlock(rtp_session->ice_mutex);
5898  return SWITCH_STATUS_FALSE;
5899  } else {
5900 
5901  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "RE-Activating Secure RTP RECV\n");
5902  rtp_session->srtp_errs[rtp_session->srtp_idx_rtp] = 0;
5903  }
5904  }
5905 
5906  if (!(*flags & SFF_PLC) && rtp_session->recv_ctx[rtp_session->srtp_idx_rtp]) {
5907  if (!rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_MKI]) {
5908  stat = srtp_unprotect(rtp_session->recv_ctx[rtp_session->srtp_idx_rtp], &rtp_session->recv_msg.header, &sbytes);
5909  } else {
5910  stat = srtp_unprotect_mki(rtp_session->recv_ctx[rtp_session->srtp_idx_rtp], &rtp_session->recv_msg.header, &sbytes, 1);
5911  }
5912 
5913  // if (rtp_session->flags[SWITCH_RTP_FLAG_NACK] && stat == srtp_err_status_replay_fail) {
5914  // /* false alarm nack */
5915  // switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "REPLAY ERR, FALSE NACK\n");
5916  // sbytes = 0;
5917  // *bytes = 0;
5918  // if (rtp_session->stats.rtcp.pkt_count) {
5919  // rtp_session->stats.rtcp.period_pkt_count--;
5920  // rtp_session->stats.rtcp.pkt_count--;
5921  // }
5922  // switch_mutex_unlock(rtp_session->ice_mutex);
5923  // goto more;
5924  // }
5925  }
5926 
5927  if (stat && rtp_session->recv_msg.header.pt != rtp_session->recv_te && rtp_session->recv_msg.header.pt != rtp_session->cng_pt) {
5928  int errs = ++rtp_session->srtp_errs[rtp_session->srtp_idx_rtp];
5929  if (rtp_session->flags[SWITCH_RTP_FLAG_SRTP_HANGUP_ON_ERROR] && stat != srtp_err_status_replay_old) {
5930  char *msg;
5931  switch_srtp_err_to_txt(stat, &msg);
5932  if (errs >= MAX_SRTP_ERRS) {
5933  switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
5935  "SRTP %s unprotect failed with code %d (%s) %ld bytes %d errors\n",
5936  rtp_type(rtp_session), stat, msg, (long)*bytes, errs);
5938  "Ending call due to SRTP error\n");
5940  } else if (errs >= WARN_SRTP_ERRS && !(errs % WARN_SRTP_ERRS)) {
5942  "SRTP %s unprotect failed with code %d (%s) %ld bytes %d errors\n",
5943  rtp_type(rtp_session), stat, msg, (long)*bytes, errs);
5944  }
5945  }
5946  sbytes = 0;
5947  } else {
5948  rtp_session->srtp_errs[rtp_session->srtp_idx_rtp] = 0;
5949  }
5950 
5951  *bytes = sbytes;
5952  }
5953  switch_mutex_unlock(rtp_session->ice_mutex);
5954 #endif
5955  }
5956 
5957  if (!jb_valid(rtp_bundle_session)) {
5958  dtls_set_state(rtp_bundle_session->dtls, DS_READY);
5959  }
5960 
5961  if (rtp_bundle_session->jb && !rtp_bundle_session->pause_jb && jb_valid(rtp_bundle_session)) {
5962  status = switch_jb_put_packet(rtp_bundle_session->jb, (switch_rtp_packet_t *) &rtp_session->recv_msg, *bytes);
5963  }
5964 
5965  *bytes = 0;
5966 
5967  return status;
5968 }
#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:340
uint32_t srtp_errs[2]
Definition: switch_rtp.c:359
switch_port_t remote_port
Definition: switch_rtp.c:417
uint8_t has_rtp
Definition: switch_rtp.c:482
#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:540
switch_payload_t cng_pt
Definition: switch_rtp.c:425
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:392
srtp_ctx_t * recv_ctx[2]
Definition: switch_rtp.c:354
switch_sockaddr_t * rtp_from_addr
Definition: switch_rtp.c:394
switch_sockaddr_t * local_addr
Definition: switch_rtp.c:326
switch_dtls_t * dtls
Definition: switch_rtp.c:366
#define dtls_set_state(_dtls, _state)
Definition: switch_rtp.c:3209
#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:5431
switch_jb_t * jb
Definition: switch_rtp.c:434
switch_status_t
Common return values.
srtp_hdr_t header
Definition: switch_rtp.c:112
rtp_msg_t recv_msg
Definition: switch_rtp.c:341
srtp_policy_t recv_policy[2]
Definition: switch_rtp.c:357
switch_mutex_t * ice_mutex
Definition: switch_rtp.c:429
switch_core_session_t * session
Definition: switch_rtp.c:472
uint8_t pause_jb
Definition: switch_rtp.c:465
int srtp_idx_rtp
Definition: switch_rtp.c:363
static int jb_valid(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:5662
switch_payload_t recv_te
Definition: switch_rtp.c:424
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:369
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 7157 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().

7158 {
7160 
7161  if (!rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
7162  return SWITCH_STATUS_FALSE;
7163  }
7164 
7165  switch_assert(bytes);
7166 
7167  *bytes = sizeof(rtcp_msg_t);
7168 
7169  if ((status = switch_socket_recvfrom(rtp_session->rtcp_from_addr, rtp_session->rtcp_sock_input, 0, (void *) rtp_session->rtcp_recv_msg_p, bytes))
7170  != SWITCH_STATUS_SUCCESS) {
7171  *bytes = 0;
7172  }
7173 
7174  switch_mutex_lock(rtp_session->ice_mutex);
7175  if (rtp_session->rtcp_dtls) {
7176  char *b = (char *) rtp_session->rtcp_recv_msg_p;
7177 
7178  if (*b == 0 || *b == 1) {
7179  if (rtp_session->rtcp_ice.ice_user) {
7180  handle_ice(rtp_session, &rtp_session->rtcp_ice, (void *) rtp_session->rtcp_recv_msg_p, *bytes);
7181  }
7182  *bytes = 0;
7183  }
7184 
7185  if (*bytes && (*b >= 20) && (*b <= 64)) {
7186  rtp_session->rtcp_dtls->bytes = *bytes;
7187  rtp_session->rtcp_dtls->data = (void *) rtp_session->rtcp_recv_msg_p;
7188  } else {
7189  rtp_session->rtcp_dtls->bytes = 0;
7190  rtp_session->rtcp_dtls->data = NULL;
7191  }
7192 
7193  do_dtls(rtp_session, rtp_session->rtcp_dtls);
7194 
7195 
7196  if (rtp_session->rtcp_dtls->bytes) {
7197  *bytes = 0;
7198  }
7199  }
7200 
7201 #ifdef ENABLE_SRTP
7202  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && rtp_session->rtcp_recv_msg_p->header.version == 2) {
7203  //if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && (!rtp_session->ice.ice_user || rtp_session->rtcp_recv_msg_p->header.version == 2)) {
7204  int sbytes = (int) *bytes;
7205  srtp_err_status_t stat = 0;
7206 
7207 
7208  if (!rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_MKI]) {
7209  stat = srtp_unprotect_rtcp(rtp_session->recv_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_recv_msg_p->header, &sbytes);
7210  } else {
7211  stat = srtp_unprotect_rtcp_mki(rtp_session->recv_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_recv_msg_p->header, &sbytes, 1);
7212  }
7213 
7214  if (stat) {
7215  //++rtp_session->srtp_errs[rtp_session->srtp_idx_rtp]++;
7216  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "RTCP UNPROTECT ERR\n");
7217  } else {
7218  //rtp_session->srtp_errs[rtp_session->srtp_idx_rtp] = 0;
7219  }
7220 
7221  *bytes = sbytes;
7222 
7223  }
7224 #endif
7225 
7226  switch_mutex_unlock(rtp_session->ice_mutex);
7227 
7228 
7229  /* RTCP Auto ADJ */
7230  if (*bytes && rtp_session->flags[SWITCH_RTP_FLAG_RTCP_AUTOADJ] && switch_sockaddr_get_port(rtp_session->rtcp_from_addr)) {
7231  if (!switch_cmp_addr(rtp_session->rtcp_from_addr, rtp_session->rtcp_remote_addr, SWITCH_FALSE)) {
7232  if (++rtp_session->rtcp_autoadj_tally >= rtp_session->rtcp_autoadj_threshold) {
7233  const char *err;
7234  uint32_t old = rtp_session->remote_rtcp_port;
7235  const char *tx_host;
7236  const char *old_host;
7237  char bufa[50], bufb[50];
7238 
7239  tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtcp_from_addr);
7240  old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->rtcp_remote_addr);
7241 
7243  "Auto Changing %s RTCP port from %s:%u to %s:%u\n", rtp_type(rtp_session), old_host, old, tx_host,
7245 
7246 
7247  rtp_session->eff_remote_host_str = switch_core_strdup(rtp_session->pool, tx_host);
7248  rtp_session->remote_rtcp_port = switch_sockaddr_get_port(rtp_session->rtcp_from_addr);
7249  status = enable_remote_rtcp_socket(rtp_session, &err);
7250  rtp_session->rtcp_auto_adj_used = 1;
7251 
7252  if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) {
7254  } else {
7256  }
7257  }
7258  } else {
7259 
7260  if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) {
7262  } else {
7264  SWITCH_LOG_DEBUG, "Correct %s RTCP ip/port confirmed.\n", rtp_type(rtp_session));
7266  }
7267  rtp_session->rtcp_auto_adj_used = 0;
7268 
7269  }
7270  }
7271 
7272  if (*bytes) {
7273  return process_rtcp_packet(rtp_session, bytes);
7274  }
7275 
7276  return status;
7277 }
#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:394
uint32_t rtcp_autoadj_tally
Definition: switch_rtp.c:351
switch_port_t remote_rtcp_port
Definition: switch_rtp.c:419
#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:364
switch_socket_t * rtcp_sock_input
Definition: switch_rtp.c:322
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:3364
uint32_t rtcp_autoadj_threshold
Definition: switch_rtp.c:350
#define rtp_type(rtp_session)
Definition: switch_rtp.c:540
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:392
srtp_ctx_t * recv_ctx[2]
Definition: switch_rtp.c:354
switch_byte_t rtcp_auto_adj_used
Definition: switch_rtp.c:464
switch_rtp_ice_t rtcp_ice
Definition: switch_rtp.c:398
switch_memory_pool_t * pool
Definition: switch_rtp.c:393
void switch_rtp_clear_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Clear an RTP Flag.
Definition: switch_rtp.c:5431
static void handle_ice(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice, void *data, switch_size_t len)
Definition: switch_rtp.c:952
void switch_rtp_set_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Set an RTP Flag.
Definition: switch_rtp.c:5372
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:2673
switch_mutex_t * ice_mutex
Definition: switch_rtp.c:429
switch_core_session_t * session
Definition: switch_rtp.c:472
switch_dtls_t * rtcp_dtls
Definition: switch_rtp.c:367
switch_rtp_bug_flag_t rtp_bugs
Definition: switch_rtp.c:449
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:7099
#define switch_assert(expr)
switch_sockaddr_t * rtcp_remote_addr
Definition: switch_rtp.c:340
switch_size_t bytes
Definition: switch_rtp.c:284
char * eff_remote_host_str
Definition: switch_rtp.c:402
rtcp_msg_t * rtcp_recv_msg_p
Definition: switch_rtp.c:343

◆ 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 5970 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().

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

1837 {
1838 #ifdef DEBUG_RTCP
1839  switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
1840 #endif
1841  switch_rtcp_numbers_t * stats=&rtp_session->stats.rtcp;
1842  uint32_t expected_pkt, dlsr = 0;
1843  int32_t pkt_lost;
1844 
1845  /* Packet loss */
1846  if (stats->rtcp_rtp_count == 0) {
1847  expected_pkt = stats->high_ext_seq_recv - stats->base_seq + 1;
1848  } else {
1849  expected_pkt = stats->high_ext_seq_recv - stats->last_rpt_ext_seq + extra_expected;
1850  }
1851 
1852  pkt_lost = expected_pkt - stats->period_pkt_count;
1853  if (pkt_lost < 0) pkt_lost = 0;
1854 
1855  stats->cum_lost=stats->cum_lost+pkt_lost;
1856  if (expected_pkt > 0 && pkt_lost > 0) {
1857  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 */
1858  } else {
1859  rtcp_report_block->fraction = 0;
1860  }
1861 #if SWITCH_BYTE_ORDER == __BIG_ENDIAN
1862  rtcp_report_block->lost = stats->cum_lost;
1863 #else
1864  /* Reversing byte order for 24bits */
1865  rtcp_report_block->lost = htonl(stats->cum_lost) >> 8;
1866 #endif
1867 
1868 #ifdef DEBUG_RTCP
1869  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO])
1870  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",
1871  rtp_session->remote_ssrc, stats->period_pkt_count, expected_pkt,
1872  stats->cum_lost, pkt_lost, rtcp_report_block->fraction, stats->high_ext_seq_recv&0x0000ffff,
1873  stats->cycle, stats->last_rpt_ext_seq&0x0000ffff, stats->last_rpt_cycle, rtp_session->stats.rtcp.peer_ssrc
1874  );
1875 #endif
1876  rtcp_report_block->highest_sequence_number_received = htonl(stats->high_ext_seq_recv);
1877 
1878  /* Jitter */
1879  rtcp_report_block->jitter = htonl((uint32_t)stats->inter_jitter);
1880 
1881  /* Delay since Last Sender Report (DLSR) : 32bits, 1/65536 seconds */
1882  if (stats->last_recv_lsr_local) {
1883  uint32_t lsr_now = calc_local_lsr_now();
1884  /* 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 */
1885  if (lsr_now > stats->last_recv_lsr_local) {
1886  dlsr = lsr_now - stats->last_recv_lsr_local;
1887  }
1888  }
1889  rtcp_report_block->lsr = stats->last_recv_lsr_peer;
1890  rtcp_report_block->dlsr = htonl(dlsr);
1891  if (rtp_session->stats.rtcp.peer_ssrc) {
1892  rtcp_report_block->ssrc = htonl(rtp_session->stats.rtcp.peer_ssrc);
1893  } else {
1894  /* if remote is not sending rtcp reports, take ssrc as assigned from rtp */
1895  rtcp_report_block->ssrc = htonl(rtp_session->remote_ssrc);
1896  }
1897 
1898  stats->rtcp_rtp_count++;
1899 }
#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:496
switch_rtcp_numbers_t rtcp
Definition: switch_types.h:762
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:392
switch_rtp_stats_t stats
Definition: switch_rtp.c:450
switch_memory_pool_t * pool
Definition: switch_rtp.c:393
uint32_t last_recv_lsr_local
Definition: switch_types.h:739
uint32_t remote_ssrc
Definition: switch_rtp.c:373
static uint32_t calc_local_lsr_now(void)
Definition: switch_rtp.c:1819
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 1795 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().

1795  {
1796  switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
1797  switch_time_t now;
1798  uint32_t sec, ntp_sec, ntp_usec;
1799  switch_time_exp_t now_hr;
1800  now = switch_micro_time_now();
1801  sec = (uint32_t)(now/1000000); /* convert to seconds */
1802  ntp_sec = sec+NTP_TIME_OFFSET; /* convert to NTP seconds */
1803  sr->ntp_msw = htonl(ntp_sec); /* store result in "most significant word" */
1804  ntp_usec = (uint32_t)(now - (sec*1000000)); /* remove seconds to keep only the microseconds */
1805  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" */
1806 
1807  sr->ts = htonl(rtp_session->last_write_ts);
1808  sr->pc = htonl(rtp_session->stats.outbound.packet_count);
1809  sr->oc = htonl(rtp_session->stats.outbound.raw_bytes - rtp_session->stats.outbound.packet_count * sizeof(srtp_hdr_t));
1810 
1811  switch_time_exp_gmt(&now_hr,now);
1812  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",
1813  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,
1814  (ntohl(sr->ntp_lsw)&0xffff0000)>>16 | (ntohl(sr->ntp_msw)&0x0000ffff)<<16,
1815  ntohl(sr->ntp_msw),ntohl(sr->ntp_lsw), rtp_session->stats.rtcp.ssrc, ntohl(sr->pc), ntohl(sr->oc)
1816  );
1817 }
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:762
switch_size_t raw_bytes
Definition: switch_types.h:682
switch_rtp_numbers_t outbound
Definition: switch_types.h:761
switch_size_t packet_count
Definition: switch_types.h:684
switch_rtp_stats_t stats
Definition: switch_rtp.c:450
switch_memory_pool_t * pool
Definition: switch_rtp.c:393
uint32_t last_write_ts
Definition: switch_rtp.c:382
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 1952 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().

1953 {
1954  switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
1955  srtp_hdr_t * hdr = &rtp_session->last_rtp_hdr;
1956  switch_rtcp_numbers_t * stats = &rtp_session->stats.rtcp;
1957  uint32_t packet_spacing_diff = 0, pkt_tsdiff, pkt_extended_seq;
1958  uint16_t pkt_seq, seq_diff, max_seq;
1959  const int MAX_DROPOUT = 3000;
1960  const int MAX_MISORDER = 100;
1961  const int RTP_SEQ_MOD = (1<<16);
1962 
1963  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)
1964  return 0; /* do not process RTCP in current state */
1965 
1966  pkt_seq = (uint16_t) ntohs((uint16_t) rtp_session->last_rtp_hdr.seq);
1967 
1968  /* Detect sequence number cycle change */
1969  max_seq = stats->high_ext_seq_recv&0x0000ffff;
1970  seq_diff = pkt_seq - max_seq;
1971 
1972  if (seq_diff < MAX_DROPOUT) { /* in order, with permissible gap */
1973  if (pkt_seq < max_seq) {
1974  stats->cycle++;
1975  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",
1976  pkt_seq, stats->cycle, max_seq, stats->ssrc, rtp_session->timer.samplecount);
1977  }
1978  pkt_extended_seq = stats->cycle << 16 | pkt_seq; /* getting the extended packet extended sequence ID */
1979  if (pkt_extended_seq > stats->high_ext_seq_recv) {
1980  stats->high_ext_seq_recv = pkt_extended_seq;
1981  }
1982  }
1983  else if (seq_diff <= (RTP_SEQ_MOD - MAX_MISORDER)) { /* the sequence number made a very large jump */
1984  if (pkt_seq == stats->bad_seq) {
1985  rtcp_stats_init(rtp_session);
1986  } else {
1987  stats->bad_seq = (pkt_seq + 1) & (RTP_SEQ_MOD-1);
1988  }
1989  return 0; /* no stats, packet is out of sync and will be accounted as lost */
1990  } else {
1991  /* duplicate or reordered packet */
1992  }
1993 
1994  /* Verify that we are on the same stream source (we do not support multiple sources) */
1995  if (ntohl(hdr->ssrc) != stats->ssrc || !stats->init) {
1996  rtcp_stats_init(rtp_session);
1997  }
1998 
1999  stats->period_pkt_count++;
2000  stats->pkt_count++;
2001 #ifdef DEBUG_RTCP
2002  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",
2003  stats->period_pkt_count, pkt_seq, stats->cycle, stats->ssrc, rtp_session->write_timer.samplecount);
2004 #endif
2005  /* Interarrival jitter calculation */
2006  pkt_tsdiff = abs((int32_t)(rtp_session->timer.samplecount - ntohl(hdr->ts))); /* relative transit times for this packet */
2007  if (stats->pkt_count < 2) { /* Can not compute Jitter with only one packet */
2008  stats->last_pkt_tsdiff = pkt_tsdiff;
2009  } else {
2010  /* Jitter : difference of relative transit times for the two packets */
2011  packet_spacing_diff = abs((int32_t)(pkt_tsdiff - stats->last_pkt_tsdiff));
2012  stats->last_pkt_tsdiff = pkt_tsdiff;
2013  /* Interarrival jitter estimation, "J(i) = J(i-1) + ( |D(i-1,i)| - J(i-1) )/16" */
2014  stats->inter_jitter = (stats->inter_jitter + (((double)packet_spacing_diff - stats->inter_jitter) /16.));
2015  }
2016 
2017 #ifdef DEBUG_RTCP
2018  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]",
2019  ntohl(hdr->ts), rtp_session->timer.samplecount, pkt_tsdiff, packet_spacing_diff, stats->inter_jitter, ntohs(hdr->seq), stats->ssrc);
2020 #endif
2021  return 1;
2022 }
#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:1901
switch_rtcp_numbers_t rtcp
Definition: switch_types.h:762
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:392
switch_rtp_stats_t stats
Definition: switch_rtp.c:450
switch_memory_pool_t * pool
Definition: switch_rtp.c:393
switch_socket_t * rtcp_sock_output
Definition: switch_rtp.c:322
int rtcp_interval
Definition: switch_rtp.c:458
switch_timer_t timer
Definition: switch_rtp.c:430
switch_timer_t write_timer
Definition: switch_rtp.c:431
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:369

◆ rtcp_stats_init()

static void rtcp_stats_init ( switch_rtp_t rtp_session)
static

Definition at line 1901 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().

1902 {
1903  switch_rtcp_numbers_t * stats = &rtp_session->stats.rtcp;
1904  srtp_hdr_t * hdr = &rtp_session->last_rtp_hdr;
1905  switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
1906  stats->ssrc = ntohl(hdr->ssrc);
1907  stats->last_rpt_ts = rtp_session->write_timer.samplecount;
1908  stats->init = 1;
1909  stats->last_rpt_ext_seq = 0;
1910  stats->last_rpt_cycle = 0;
1911  stats->last_pkt_tsdiff = 0;
1912  stats->inter_jitter = 0;
1913  stats->cycle = 0;
1914  stats->high_ext_seq_recv = ntohs((uint16_t)hdr->seq);
1915  stats->base_seq = ntohs((uint16_t)hdr->seq);
1916  stats->bad_seq = (1<<16) + 1; /* Make sure we wont missmatch 2 consecutive packets, so seq == bad_seq is false */
1917  stats->cum_lost = 0;
1918  stats->period_pkt_count = 0;
1919  stats->sent_pkt_count = 0;
1920  stats->pkt_count = 0;
1921  stats->rtcp_rtp_count = 0;
1922 
1923  if (!rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
1924  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "rtcp_stats_init: %s rtcp disabled\n", rtp_type(rtp_session));
1925  } else if (!rtp_session->rtcp_sock_output) {
1926  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "rtcp_stats_init: %s no rtcp socket\n", rtp_type(rtp_session));
1927  } else if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU]) {
1928  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "rtcp_stats_init: %s rtcp passthru\n", rtp_type(rtp_session));
1929  } else {
1930  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);
1931  }
1932 
1933  if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] && (switch_core_media_codec_get_cap(rtp_session->session,
1937 
1938  rtp_session->flags[SWITCH_RTP_FLAG_ADJ_BITRATE_CAP] = 1;
1939  rtp_session->flags[SWITCH_RTP_FLAG_ESTIMATORS] = 1;
1940 
1941  rtp_session->estimators[EST_LOSS] = switch_core_alloc(rtp_session->pool, sizeof(*estimators[0]));
1942  switch_kalman_init(rtp_session->estimators[EST_LOSS],0.1,0.1);
1943  rtp_session->estimators[EST_RTT] = switch_core_alloc(rtp_session->pool, sizeof(*estimators[0]));
1944  switch_kalman_init(rtp_session->estimators[EST_RTT],0.03,1);
1945  rtp_session->detectors[EST_RTT] = switch_core_alloc(rtp_session->pool, sizeof(*detectors[0]));
1946  switch_kalman_cusum_init(rtp_session->detectors[EST_RTT],0.005,0.5);
1947  rtp_session->detectors[EST_LOSS] = switch_core_alloc(rtp_session->pool, sizeof(*detectors[0]));
1948  switch_kalman_cusum_init(rtp_session->detectors[EST_LOSS], 0.5, 1);
1949  }
1950 }
#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:540
switch_rtcp_numbers_t rtcp
Definition: switch_types.h:762
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:392
#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:475
switch_rtp_stats_t stats
Definition: switch_rtp.c:450
switch_memory_pool_t * pool
Definition: switch_rtp.c:393
switch_socket_t * rtcp_sock_output
Definition: switch_rtp.c:322
switch_core_session_t * session
Definition: switch_rtp.c:472
switch_timer_t write_timer
Definition: switch_rtp.c:431
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:369
cusum_kalman_detector_t * detectors[KALMAN_SYSTEM_MODELS]
Definition: switch_rtp.c:476

◆ 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 7301 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().

7303 {
7304 
7305  switch_channel_t *channel = NULL;
7306  switch_size_t bytes = 0;
7307  switch_size_t rtcp_bytes = 0;
7309  switch_status_t rtcp_status = SWITCH_STATUS_SUCCESS, rtcp_poll_status = SWITCH_STATUS_SUCCESS;
7310  int check = 0;
7311  int ret = -1;
7312  int sleep_mss = 1000;
7313  int poll_sec = 5;
7314  int poll_loop = 0;
7315  int fdr = 0;
7316  int rtcp_fdr = 0;
7317  int hot_socket = 0;
7318  int read_loops = 0;
7319  int slept = 0;
7320  switch_bool_t got_jb = SWITCH_FALSE;
7321 
7322  if (!switch_rtp_ready(rtp_session)) {
7323  return -1;
7324  }
7325 
7326  if (rtp_session->session) {
7327  channel = switch_core_session_get_channel(rtp_session->session);
7328  }
7329 
7330  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
7331  sleep_mss = rtp_session->timer.interval * 1000;
7332  }
7333 
7334  READ_INC(rtp_session);
7335 
7336 
7337 
7338  while (switch_rtp_ready(rtp_session)) {
7339  int do_cng = 0;
7340  int read_pretriggered = 0;
7341  int has_rtcp = 0;
7342  int got_rtp_poll = 0;
7343 
7344  bytes = 0;
7345 
7346  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] &&
7347  !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] &&
7348  !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] &&
7349  !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] &&
7350  rtp_session->read_pollfd) {
7351 
7352  if (rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) {
7353  while (switch_poll(rtp_session->read_pollfd, 1, &fdr, 0) == SWITCH_STATUS_SUCCESS) {
7354  status = read_rtp_packet(rtp_session, &bytes, flags, pmapP, SWITCH_STATUS_SUCCESS, SWITCH_FALSE);
7355 
7356  if (status == SWITCH_STATUS_GENERR) {
7357  ret = -1;
7358  goto end;
7359  }
7360 
7361  if ((*flags & SFF_RTCP)) {
7362  *flags &= ~SFF_RTCP;
7363  has_rtcp = 1;
7364  read_pretriggered = 0;
7365  goto rtcp;
7366  }
7367 
7368  if (status == SWITCH_STATUS_BREAK) {
7369  read_pretriggered = 1;
7370  break;
7371  }
7372  }
7373 
7374  } else if ((rtp_session->flags[SWITCH_RTP_FLAG_AUTOFLUSH] || rtp_session->flags[SWITCH_RTP_FLAG_STICKY_FLUSH])) {
7375 
7376  if (switch_poll(rtp_session->read_pollfd, 1, &fdr, 0) == SWITCH_STATUS_SUCCESS) {
7377  status = read_rtp_packet(rtp_session, &bytes, flags, pmapP, SWITCH_STATUS_SUCCESS, SWITCH_FALSE);
7378  if (status == SWITCH_STATUS_GENERR) {
7379  ret = -1;
7380  goto end;
7381  }
7382  if ((*flags & SFF_RTCP)) {
7383  *flags &= ~SFF_RTCP;
7384  has_rtcp = 1;
7385  read_pretriggered = 0;
7386  goto rtcp;
7387  }
7388 
7389  /* switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Initial (%i) %d\n", status, bytes); */
7390  if (status != SWITCH_STATUS_FALSE) {
7391  read_pretriggered = 1;
7392  }
7393 
7394  if (bytes) {
7395  if (switch_poll(rtp_session->read_pollfd, 1, &fdr, 0) == SWITCH_STATUS_SUCCESS) {
7396  rtp_session->hot_hits++;//+= rtp_session->samples_per_interval;
7397 
7398  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG10, "%s Hot Hit %d\n",
7399  rtp_session_name(rtp_session),
7400  rtp_session->hot_hits);
7401  } else {
7402  rtp_session->hot_hits = 0;
7403  }
7404  }
7405 
7406  if (rtp_session->hot_hits > 1 && !rtp_session->sync_packets) {// >= (rtp_session->samples_per_second * 30)) {
7407  hot_socket = 1;
7408  }
7409  } else {
7410  rtp_session->hot_hits = 0;
7411  }
7412  }
7413 
7414  if (rtp_session->flags[SWITCH_RTP_FLAG_TEXT]) {
7415  ///NOOP
7416  } else if (hot_socket && (rtp_session->hot_hits % 10) != 0) {
7417  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG10, "%s timer while HOT\n", rtp_session_name(rtp_session));
7418  switch_core_timer_next(&rtp_session->timer);
7419  } else if (hot_socket) {
7420  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG10, "%s skip timer once\n", rtp_session_name(rtp_session));
7421  rtp_session->sync_packets++;
7422  switch_core_timer_sync(&rtp_session->timer);
7423  reset_jitter_seq(rtp_session);
7424  } else {
7425 
7426  if (rtp_session->sync_packets) {
7427 
7429  "%s Auto-Flush catching up %d packets (%d)ms.\n",
7430  rtp_session_name(rtp_session),
7431  rtp_session->sync_packets, (rtp_session->ms_per_packet * rtp_session->sync_packets) / 1000);
7432  if (!rtp_session->flags[SWITCH_RTP_FLAG_PAUSE]) {
7433  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "%s syncing %d %s packet(s)\n",
7434  rtp_session_name(rtp_session),
7435  rtp_session->sync_packets, rtp_type(rtp_session));
7436 
7437  rtp_session->bad_stream++;
7438  rtp_session->stats.inbound.flaws += rtp_session->sync_packets;
7439 
7440  if (rtp_session->stats.inbound.error_log) {
7441  rtp_session->stats.inbound.error_log->flaws += rtp_session->sync_packets;
7442  }
7443  }
7444 
7445  switch_core_timer_sync(&rtp_session->timer);
7446  reset_jitter_seq(rtp_session);
7447  rtp_session->hot_hits = 0;
7448  } else {
7449  if (slept) {
7450  switch_cond_next();
7451  } else {
7452  if (rtp_session->skip_timer) {
7453  rtp_session->skip_timer = 0;
7454  switch_cond_next();
7455  } else {
7456  switch_core_timer_next(&rtp_session->timer);
7457  }
7458  slept++;
7459  }
7460 
7461  }
7462 
7463  rtp_session->sync_packets = 0;
7464  }
7465  }
7466 
7467  rtp_session->stats.read_count++;
7468 
7469  recvfrom:
7470 
7471  if (!read_pretriggered) {
7472  bytes = 0;
7473  }
7474  read_loops++;
7475  //poll_loop = 0;
7476 
7477  if (!switch_rtp_ready(rtp_session)) {
7478  break;
7479  }
7480 
7481  if (!rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && rtp_session->read_pollfd) {
7482  int pt = poll_sec * 1000000;
7483 
7484  do_2833(rtp_session);
7485 
7486  if (rtp_session->dtmf_data.out_digit_dur > 0 || rtp_session->dtmf_data.in_digit_sanity || rtp_session->sending_dtmf ||
7488  pt = 20000;
7489  }
7490 
7491  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) {
7492  pt = 100000;
7493  }
7494 
7495  if (rtp_session->vb && !rtp_session->pause_jb) {
7496  if (switch_jb_poll(rtp_session->vb)) {
7497  pt = 1000;
7498  }
7499  }
7500 
7501  if ((io_flags & SWITCH_IO_FLAG_NOBLOCK)) {
7502  pt = 0;
7503  }
7504 
7505  poll_status = switch_poll(rtp_session->read_pollfd, 1, &fdr, pt);
7506 
7507  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && poll_status != SWITCH_STATUS_SUCCESS && rtp_session->media_timeout && rtp_session->last_media) {
7508  check_timeout(rtp_session);
7509  }
7510 
7511  if (!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->dtmf_data.out_digit_dur > 0) {
7512  return_cng_frame();
7513  }
7514 
7515  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->flags[SWITCH_RTP_FLAG_BREAK]) {
7517  bytes = 0;
7518  reset_jitter_seq(rtp_session);
7519  return_cng_frame();
7520  }
7521 
7522  }
7523 
7524  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
7525  got_jb = (rtp_session->vb && !rtp_session->pause_jb && switch_jb_poll(rtp_session->vb));
7526  } else {
7527  got_jb = SWITCH_TRUE;
7528  }
7529 
7530  if (poll_status == SWITCH_STATUS_SUCCESS || got_jb) {
7531 
7532  got_rtp_poll = 1;
7533 
7534  if (read_pretriggered) {
7535  read_pretriggered = 0;
7536  } else {
7537 
7538 
7539  status = read_rtp_packet(rtp_session, &bytes, flags, pmapP, poll_status, got_jb);
7540 
7541  if (status == SWITCH_STATUS_GENERR) {
7542  ret = -1;
7543  goto end;
7544  }
7545 
7546  if (rtp_session->max_missed_packets && read_loops == 1 && !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] &&
7547  !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
7548  if (bytes && status == SWITCH_STATUS_SUCCESS) {
7549  rtp_session->missed_count = 0;
7550  } else {
7551  if (rtp_session->media_timeout && rtp_session->last_media) {
7552  check_timeout(rtp_session);
7553  } else {
7554  if (++rtp_session->missed_count >= rtp_session->max_missed_packets) {
7555  ret = -2;
7556  goto end;
7557  }
7558  }
7559  }
7560  }
7561 
7562  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
7563  //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_CRIT, "Read bytes (%i) %ld\n", status, bytes);
7564 
7565  if (bytes == 0) {
7566  if (check_rtcp_and_ice(rtp_session) == -1) {
7567  ret = -1;
7568  goto end;
7569  }
7570  // This is dumb
7571  //switch_rtp_video_refresh(rtp_session);
7572  goto rtcp;
7573  }
7574  }
7575 
7576  if ((*flags & SFF_PROXY_PACKET)) {
7577  ret = (int) bytes;
7578  goto end;
7579  }
7580 
7581  if ((*flags & SFF_RTCP)) {
7582  *flags &= ~SFF_RTCP;
7583  has_rtcp = 1;
7584  goto rtcp;
7585  }
7586 
7587 
7588  }
7589  poll_loop = 0;
7590  } else {
7591 
7592  if (!switch_rtp_ready(rtp_session)) {
7593  ret = -1;
7594  goto end;
7595  }
7596 
7597  if (!SWITCH_STATUS_IS_BREAK(poll_status) && poll_status != SWITCH_STATUS_TIMEOUT) {
7598  char tmp[128] = "";
7599  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Poll failed with error: %d [%s]\n",
7600  poll_status, switch_strerror_r(poll_status, tmp, sizeof(tmp)));
7601  ret = -1;
7602  goto end;
7603  }
7604 
7605  if (!rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] && !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
7606  rtp_session->missed_count += (poll_sec * 1000) / (rtp_session->ms_per_packet ? rtp_session->ms_per_packet / 1000 : 20);
7607  bytes = 0;
7608 
7609  if (rtp_session->media_timeout && rtp_session->last_media) {
7610  check_timeout(rtp_session);
7611  } else if (rtp_session->max_missed_packets) {
7612  if (rtp_session->missed_count >= rtp_session->max_missed_packets) {
7613  ret = -2;
7614  goto end;
7615  }
7616  }
7617  }
7618 
7619 
7620  if (check_rtcp_and_ice(rtp_session) == -1) {
7621  ret = -1;
7622  goto end;
7623  }
7624 
7625 
7626  if ((!(io_flags & SWITCH_IO_FLAG_NOBLOCK)) &&
7627  (rtp_session->dtmf_data.out_digit_dur == 0) && !rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
7628  return_cng_frame();
7629  }
7630  }
7631 
7632  rtcp:
7633 
7634  if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
7635  rtcp_poll_status = SWITCH_STATUS_FALSE;
7636 
7637  if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX] && has_rtcp) {
7638  if (rtp_session->rtcp_recv_msg_p->header.version == 2) { //rtcp muxed
7639  rtp_session->rtcp_from_addr = rtp_session->from_addr;
7640  rtcp_status = rtcp_poll_status = SWITCH_STATUS_SUCCESS;
7641  rtcp_bytes = bytes;
7642  }
7643 
7644  has_rtcp = 0;
7645 
7646  } else if (rtp_session->rtcp_read_pollfd) {
7647  rtcp_poll_status = switch_poll(rtp_session->rtcp_read_pollfd, 1, &rtcp_fdr, 0);
7648  }
7649 
7650  if (rtcp_poll_status == SWITCH_STATUS_SUCCESS) {
7651 
7652  if (!rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
7653  rtcp_status = read_rtcp_packet(rtp_session, &rtcp_bytes, flags);
7654  }
7655 
7656  if (rtcp_status == SWITCH_STATUS_SUCCESS) {
7657  switch_rtp_reset_media_timer(rtp_session);
7658 
7659  if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU]) {
7660  switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
7661  const char *uuid = switch_channel_get_partner_uuid(channel);
7662 
7663  if (uuid) {
7664  switch_core_session_t *other_session;
7665  switch_rtp_t *other_rtp_session = NULL;
7666 
7667  if ((other_session = switch_core_session_locate(uuid))) {
7668  switch_channel_t *other_channel = switch_core_session_get_channel(other_session);
7669  if ((other_rtp_session = switch_channel_get_private(other_channel, "__rtcp_audio_rtp_session")) &&
7670  other_rtp_session->rtcp_sock_output &&
7671  switch_rtp_test_flag(other_rtp_session, SWITCH_RTP_FLAG_ENABLE_RTCP)) {
7672  other_rtp_session->rtcp_send_msg = rtp_session->rtcp_recv_msg;
7673 
7674 #ifdef ENABLE_SRTP
7675  switch_mutex_lock(other_rtp_session->ice_mutex);
7676  if (switch_rtp_test_flag(other_rtp_session, SWITCH_RTP_FLAG_SECURE_SEND)) {
7677  int stat = 0;
7678  int sbytes = (int) rtcp_bytes;
7679 
7680  if (!other_rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_MKI]) {
7681  stat = srtp_protect_rtcp(other_rtp_session->send_ctx[rtp_session->srtp_idx_rtcp], &other_rtp_session->rtcp_send_msg.header, &sbytes);
7682  } else {
7683  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);
7684  }
7685 
7686  if (stat) {
7687  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error: SRTP RTCP protection failed with code %d\n", stat);
7688  }
7689  rtcp_bytes = sbytes;
7690  }
7691  switch_mutex_unlock(other_rtp_session->ice_mutex);
7692 #endif
7693 
7694  if (switch_socket_sendto(other_rtp_session->rtcp_sock_output, other_rtp_session->rtcp_remote_addr, 0,
7695  (const char*)&other_rtp_session->rtcp_send_msg, &rtcp_bytes ) != SWITCH_STATUS_SUCCESS) {
7696  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG,"RTCP packet not written\n");
7697  }
7698 
7699 
7700  }
7701  switch_core_session_rwunlock(other_session);
7702  }
7703  }
7704 
7705  }
7706 
7707  if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
7708  process_rtcp_packet(rtp_session, &rtcp_bytes);
7709  ret = 1;
7710 
7711  continue;
7712  }
7713  }
7714  }
7715  }
7716 
7717  if ((!(io_flags & SWITCH_IO_FLAG_NOBLOCK)) &&
7718  (rtp_session->dtmf_data.out_digit_dur == 0) && !got_rtp_poll) {
7719  return_cng_frame();
7720  }
7721 
7722  if (!bytes && (io_flags & SWITCH_IO_FLAG_NOBLOCK)) {
7723  rtp_session->missed_count = 0;
7724  ret = 0;
7725  goto end;
7726  }
7727 
7728  check = !bytes;
7729 
7730  if (rtp_session->flags[SWITCH_RTP_FLAG_FLUSH]) {
7731  bytes = do_flush(rtp_session, SWITCH_FALSE, bytes);
7733  }
7734 
7735  if ((!bytes && rtp_session->flags[SWITCH_RTP_FLAG_BREAK]) || (bytes && bytes == 4 && *((int *) &rtp_session->recv_msg) == UINT_MAX)) {
7737 
7738  if (!rtp_session->flags[SWITCH_RTP_FLAG_NOBLOCK] || !rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] ||
7739  rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] ||
7740  (bytes && bytes < 5) || (!bytes && poll_loop)) {
7741  bytes = 0;
7742  reset_jitter_seq(rtp_session);
7743  return_cng_frame();
7744  }
7745  }
7746 
7747  if (bytes && bytes < 5) {
7748  continue;
7749  }
7750 
7751  if (!bytes && poll_loop) {
7752  goto recvfrom;
7753  }
7754 
7755  if (bytes && rtp_session->last_rtp_hdr.m && rtp_session->last_rtp_hdr.pt != rtp_session->recv_te &&
7756  !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] &&
7757  !rtp_session->flags[SWITCH_RTP_FLAG_TEXT] &&
7758  !(rtp_session->rtp_bugs & RTP_BUG_IGNORE_MARK_BIT)) {
7760  }
7761 
7762  if (rtp_session->last_rtp_hdr.pt == rtp_session->cng_pt || rtp_session->last_rtp_hdr.pt == 13) {
7763  *flags |= SFF_NOT_AUDIO;
7764  } else {
7765  *flags &= ~SFF_NOT_AUDIO; /* If this flag was already set, make sure to remove it when we get real audio */
7766  }
7767 
7768  /* ignore packets not meant for us unless the auto-adjust window is open (ice mode has its own alternatives to this) */
7769  /* ------------------- BUNDLE audio skip pt and addr check begin ------------------- */
7771  if (!using_ice(rtp_session) && bytes) {
7772  if (rtp_session->flags[SWITCH_RTP_FLAG_AUTOADJ]) {
7773  if (rtp_session->last_rtp_hdr.pt == rtp_session->cng_pt || rtp_session->last_rtp_hdr.pt == 13) {
7774  goto recvfrom;
7775 
7776  }
7777  } 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)) {
7778  goto recvfrom;
7779  }
7780  }
7781 
7782  if (bytes && rtp_session->flags[SWITCH_RTP_FLAG_AUTOADJ] && switch_sockaddr_get_port(rtp_session->rtp_from_addr)) {
7783  if (!switch_cmp_addr(rtp_session->rtp_from_addr, rtp_session->remote_addr, SWITCH_FALSE)) {
7784  if (++rtp_session->autoadj_tally >= rtp_session->autoadj_threshold) {
7785  const char *err;
7786  uint32_t old = rtp_session->remote_port;
7787  const char *tx_host;
7788  const char *old_host;
7789  char bufa[50], bufb[50];
7790  char adj_port[6];
7791 
7792  tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtp_from_addr);
7793  old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
7794 
7796  "Auto Changing %s port from %s:%u to %s:%u\n", rtp_type(rtp_session), old_host, old, tx_host,
7797  switch_sockaddr_get_port(rtp_session->rtp_from_addr));
7798 
7799  if (channel) {
7800  char varname[80] = "";
7801 
7802  switch_snprintf(varname, sizeof(varname), "remote_%s_ip_reported", rtp_type(rtp_session));
7803  switch_channel_set_variable(channel, varname, switch_channel_get_variable(channel, "remote_media_ip"));
7804 
7805  switch_snprintf(varname, sizeof(varname), "remote_%s_ip", rtp_type(rtp_session));
7806  switch_channel_set_variable(channel, varname, tx_host);
7807 
7808  switch_snprintf(varname, sizeof(varname), "remote_%s_port_reported", rtp_type(rtp_session));
7809  switch_snprintf(adj_port, sizeof(adj_port), "%u", switch_sockaddr_get_port(rtp_session->rtp_from_addr));
7810  switch_channel_set_variable(channel, varname, switch_channel_get_variable(channel, "remote_media_port"));
7811 
7812  switch_snprintf(varname, sizeof(varname), "remote_%s_port", rtp_type(rtp_session));
7813  switch_channel_set_variable(channel, varname, adj_port);
7814 
7815  switch_snprintf(varname, sizeof(varname), "rtp_auto_adjust_%s", rtp_type(rtp_session));
7816  switch_channel_set_variable(channel, varname, "true");
7817  }
7818  rtp_session->auto_adj_used = 1;
7819  switch_rtp_set_remote_address(rtp_session, tx_host, switch_sockaddr_get_port(rtp_session->rtp_from_addr), 0, SWITCH_FALSE, &err);
7820  if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) {
7823  } else {
7825  }
7826  if (rtp_session->ice.ice_user) {
7827  rtp_session->ice.addr = rtp_session->remote_addr;
7828  }
7829  }
7830  } else {
7831  if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) {
7834  } else {
7835  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Correct %s ip/port confirmed.\n", rtp_type(rtp_session));
7837  }
7838  rtp_session->auto_adj_used = 0;
7839  }
7840  }
7841 
7842  if (bytes && !(rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST) && rtp_session->autoadj_window) {
7843  if (--rtp_session->autoadj_window == 0) {
7845  }
7846  }
7847  } /* ------------------- BUNDLE audio skip pt and addr check end ------------------- */
7848 
7849  if (rtp_session->flags[SWITCH_RTP_FLAG_TEXT]) {
7850  if (!bytes) {
7851  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
7852  switch_core_timer_next(&rtp_session->timer);
7853  }
7854  return_cng_frame();
7855  } else {
7856  *payload_type = rtp_session->last_rtp_hdr.pt;
7857  ret = (int) bytes;
7858  goto end;
7859  }
7860  }
7861 
7862  if (bytes && (rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL])) {
7863  /* Fast PASS! */
7864  *flags |= SFF_PROXY_PACKET;
7865 
7866  if (rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
7867 #if 0
7868  if (rtp_session->has_rtp && check_recv_payload(rtp_session)) {
7870  "Ignoring udptl packet of size of %ld bytes that looks strikingly like a RTP packet.\n", (long)bytes);
7871  bytes = 0;
7872  goto do_continue;
7873  }
7874 #endif
7875  *flags |= SFF_UDPTL_PACKET;
7876  }
7877 
7878  ret = (int) bytes;
7879  goto end;
7880  }
7881 
7882  if (bytes) {
7883  rtp_session->missed_count = 0;
7884 
7885  if (bytes < rtp_header_len) {
7886  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "Ignoring invalid RTP packet size of %ld bytes.\n", (long)bytes);
7887  bytes = 0;
7888  goto do_continue;
7889  }
7890 
7891  if (rtp_session->last_rtp_hdr.pt && (rtp_session->last_rtp_hdr.pt == rtp_session->cng_pt || rtp_session->last_rtp_hdr.pt == 13)) {
7892  return_cng_frame();
7893  }
7894  }
7895 
7896  if (check || bytes) {
7897  do_2833(rtp_session);
7898  }
7899 
7900  if (bytes && rtp_session->recv_msg.header.version != 2) {
7901  uint8_t *data = (uint8_t *) RTP_BODY(rtp_session);
7902 
7903  //if (rtp_session->recv_msg.header.version == 0) {
7904  // if (rtp_session->ice.ice_user) {
7905  // handle_ice(rtp_session, &rtp_session->ice, (void *) &rtp_session->recv_msg, bytes);
7906  // goto recvfrom;
7907  // }
7908  //}
7909 
7910  if (rtp_session->invalid_handler) {
7911  rtp_session->invalid_handler(rtp_session, rtp_session->sock_input, (void *) &rtp_session->recv_msg, bytes, rtp_session->rtp_from_addr);
7912  }
7913 
7914  memset(data, 0, 2);
7915  data[0] = 65;
7916 
7917  rtp_session->last_rtp_hdr.pt = rtp_session->cng_pt != INVALID_PT ? rtp_session->cng_pt : SWITCH_RTP_CNG_PAYLOAD;
7918  *flags |= SFF_CNG;
7919  *payload_type = (switch_payload_t) rtp_session->last_rtp_hdr.pt;
7920  ret = 2 + rtp_header_len;
7921  goto end;
7922  } else if (bytes) {
7923  rtp_session->stats.inbound.period_packet_count++;
7924  }
7925 
7926 
7927  /* Handle incoming RFC2833 packets */
7928  switch (handle_rfc2833(rtp_session, bytes, &do_cng)) {
7929  case RESULT_GOTO_END:
7930  goto end;
7931  case RESULT_GOTO_RECVFROM:
7932  goto recvfrom;
7934  goto timer_check;
7935  case RESULT_CONTINUE:
7936  status = SWITCH_STATUS_SUCCESS;
7937  goto result_continue;
7938  }
7939 
7940  result_continue:
7941  timer_check:
7942 
7943  if (!rtp_session->media_timeout && rtp_session->flags[SWITCH_RTP_FLAG_MUTE]) {
7944  do_cng++;
7945  }
7946 
7947  if (do_cng) {
7948  uint8_t *data = (uint8_t *) RTP_BODY(rtp_session);
7949 
7950  do_2833(rtp_session);
7951 
7952  if (rtp_session->last_cng_ts == rtp_session->last_read_ts + rtp_session->samples_per_interval) {
7953  rtp_session->last_cng_ts = 0;
7954  } else {
7955  rtp_session->last_cng_ts = rtp_session->last_read_ts + rtp_session->samples_per_interval;
7956  }
7957 
7958  memset(data, 0, 2);
7959  data[0] = 65;
7960  rtp_session->last_rtp_hdr.pt = rtp_session->cng_pt != INVALID_PT ? rtp_session->cng_pt : SWITCH_RTP_CNG_PAYLOAD;
7961  *flags |= SFF_CNG;
7962  *payload_type = (switch_payload_t) rtp_session->last_rtp_hdr.pt;
7963  ret = 2 + rtp_header_len;
7964  rtp_session->stats.inbound.skip_packet_count++;
7965  goto end;
7966  }
7967 
7968 
7969  if (check || (bytes && !rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER])) {
7970  if (!bytes && rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) { /* We're late! We're Late! */
7971  if (!rtp_session->flags[SWITCH_RTP_FLAG_NOBLOCK] && status == SWITCH_STATUS_BREAK) {
7972  switch_cond_next();
7973  continue;
7974  }
7975 
7976 
7977 
7978  if (!rtp_session->flags[SWITCH_RTP_FLAG_PAUSE] && !rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] && !rtp_session->dtmf_data.in_digit_ts
7979  && rtp_session->cng_count > (rtp_session->one_second * 2) && rtp_session->jitter_lead > JITTER_LEAD_FRAMES) {
7980 
7981  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "%s %s timeout\n",
7982  rtp_session_name(rtp_session), rtp_type(rtp_session));
7983 
7984  if (rtp_session->media_timeout && rtp_session->last_media) {
7985  check_timeout(rtp_session);
7986  }
7987 
7988  if (rtp_session->stats.inbound.error_log) {
7989  rtp_session->stats.inbound.error_log->flaws++;
7990  }
7991  rtp_session->stats.inbound.flaws++;
7992  do_mos(rtp_session);
7993  }
7994 
7995  rtp_session->cng_count++;
7996  return_cng_frame();
7997  }
7998  }
7999 
8000  rtp_session->cng_count = 0;
8001 
8002  if (status == SWITCH_STATUS_BREAK || bytes == 0) {
8003  if (!(io_flags & SWITCH_IO_FLAG_SINGLE_READ) && rtp_session->flags[SWITCH_RTP_FLAG_DATAWAIT]) {
8004  goto do_continue;
8005  }
8006  return_cng_frame();
8007  }
8008 
8009  if (rtp_session->flags[SWITCH_RTP_FLAG_GOOGLEHACK] && rtp_session->last_rtp_hdr.pt == 102) {
8010  rtp_session->last_rtp_hdr.pt = 97;
8011  }
8012 
8013  break;
8014 
8015  do_continue:
8016 
8017  if (!bytes && !rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && !switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO)) {
8018 
8019  if (sleep_mss) {
8020  switch_yield(sleep_mss);
8021  }
8022  }
8023 
8024  }
8025 
8026  if (switch_rtp_ready(rtp_session)) {
8027  *payload_type = (switch_payload_t) rtp_session->last_rtp_hdr.pt;
8028 
8029  if (*payload_type == SWITCH_RTP_CNG_PAYLOAD) {
8030  *flags |= SFF_CNG;
8031  }
8032 
8033  ret = (int) bytes;
8034  } else {
8035  ret = -1;
8036  }
8037 
8038  end:
8039 
8040  READ_DEC(rtp_session);
8041 
8042  return ret;
8043 }
switch_pollfd_t * read_pollfd
Definition: switch_rtp.c:323
unsigned int switch_queue_size(switch_queue_t *queue)
Definition: switch_apr.c:1238
switch_size_t flaws
Definition: switch_types.h:712
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:7157
static switch_size_t do_flush(switch_rtp_t *rtp_session, int force, switch_size_t bytes_in)
Definition: switch_rtp.c:5678
switch_size_t period_packet_count
Definition: switch_types.h:685
switch_rtp_invalid_handler_t invalid_handler
Definition: switch_rtp.c:378
#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:760
uint8_t switch_rtp_ready(switch_rtp_t *rtp_session)
Test if an RTP session is ready.
Definition: switch_rtp.c:5147
uint32_t max_missed_packets
Definition: switch_rtp.c:437
switch_sockaddr_t * rtcp_from_addr
Definition: switch_rtp.c:394
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:488
switch_sockaddr_t * remote_addr
Definition: switch_rtp.c:340
static void check_timeout(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:7279
switch_sockaddr_t * addr
Definition: switch_rtp.c:248
switch_bool_t
Definition: switch_types.h:437
switch_socket_t * sock_input
Definition: switch_rtp.c:322
switch_port_t remote_port
Definition: switch_rtp.c:417
int srtp_idx_rtcp
Definition: switch_rtp.c:364
uint32_t media_timeout
Definition: switch_rtp.c:440
uint8_t has_rtp
Definition: switch_rtp.c:482
#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:448
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:439
#define READ_DEC(rtp_session)
Definition: switch_rtp.c:68
uint32_t flaws
Definition: switch_types.h:675
struct switch_rtp_rfc2833_data dtmf_data
Definition: switch_rtp.c:422
void rtp_flush_read_buffer(switch_rtp_t *rtp_session, switch_rtp_flush_t flush)
Definition: switch_rtp.c:5635
rtcp_msg_t rtcp_recv_msg
Definition: switch_rtp.c:342
#define rtp_type(rtp_session)
Definition: switch_rtp.c:540
void switch_rtp_reset_media_timer(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:3028
switch_payload_t cng_pt
Definition: switch_rtp.c:425
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:353
switch_queue_t * dtmf_inqueue
Definition: switch_rtp.c:237
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:392
#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:5970
switch_sockaddr_t * rtp_from_addr
Definition: switch_rtp.c:394
#define READ_INC(rtp_session)
Definition: switch_rtp.c:67
uint32_t autoadj_threshold
Definition: switch_rtp.c:346
switch_byte_t auto_adj_used
Definition: switch_rtp.c:463
int switch_jb_poll(switch_jb_t *jb)
uint32_t last_read_ts
Definition: switch_rtp.c:383
uint32_t autoadj_window
Definition: switch_rtp.c:345
rtcp_msg_t rtcp_send_msg
Definition: switch_rtp.c:328
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:435
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:3125
uint32_t sync_packets
Definition: switch_rtp.c:457
switch_rtp_stats_t stats
Definition: switch_rtp.c:450
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:687
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:322
#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:5431
void switch_rtp_set_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Set an RTP Flag.
Definition: switch_rtp.c:5372
switch_jb_t * jb
Definition: switch_rtp.c:434
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:438
struct error_period * error_log
Definition: switch_types.h:716
static int using_ice(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:2050
uint32_t samples_per_interval
Definition: switch_rtp.c:405
srtp_hdr_t header
Definition: switch_rtp.c:112
switch_timer_t timer
Definition: switch_rtp.c:430
rtp_msg_t recv_msg
Definition: switch_rtp.c:341
#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:429
static void reset_jitter_seq(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:1646
switch_core_session_t * session
Definition: switch_rtp.c:472
static int check_rtcp_and_ice(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:2133
uint8_t pause_jb
Definition: switch_rtp.c:465
switch_rtp_ice_t ice
Definition: switch_rtp.c:397
int8_t sending_dtmf
Definition: switch_rtp.c:375
switch_rtp_bug_flag_t rtp_bugs
Definition: switch_rtp.c:449
static void do_2833(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:5474
uint32_t ms_per_packet
Definition: switch_rtp.c:410
static int jb_valid(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:5662
switch_payload_t recv_te
Definition: switch_rtp.c:424
switch_queue_t * dtmf_queue
Definition: switch_rtp.c:221
uint32_t hot_hits
Definition: switch_rtp.c:456
uint32_t jitter_lead
Definition: switch_rtp.c:413
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:226
uint32_t switch_rtp_test_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flags)
Test an RTP Flag.
Definition: switch_rtp.c:5426
switch_pollfd_t * rtcp_read_pollfd
Definition: switch_rtp.c:323
uint32_t bad_stream
Definition: switch_rtp.c:453
static handle_rfc2833_result_t handle_rfc2833(switch_rtp_t *rtp_session, switch_size_t bytes, int *do_cng)
Definition: switch_rtp.c:582
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:385
#define SWITCH_RTP_CNG_PAYLOAD
Definition: switch_types.h:772
#define return_cng_frame()
Definition: switch_rtp.c:5845
static switch_status_t process_rtcp_packet(switch_rtp_t *rtp_session, switch_size_t *bytes)
Definition: switch_rtp.c:7099
switch_sockaddr_t * from_addr
Definition: switch_rtp.c:394
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:340
rtp_hdr_t last_rtp_hdr
Definition: switch_rtp.c:369
static void do_mos(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:1571
static int check_recv_payload(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:5799
memset(buf, 0, buflen)
uint32_t autoadj_tally
Definition: switch_rtp.c:347
#define rtp_header_len
Definition: switch_rtp.c:73
rtcp_msg_t * rtcp_recv_msg_p
Definition: switch_rtp.c:343
uint32_t one_second
Definition: switch_rtp.c:411
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 8290 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_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().

8292 {
8293  switch_size_t bytes;
8294  uint8_t send = 1;
8295  uint32_t this_ts = 0;
8296  int ret;
8297  switch_time_t now;
8298  uint8_t m = 0;
8299  switch_rtp_t *rtp_bundle_session = NULL;
8300  switch_rtp_t *tmp_session = NULL;
8301 
8302  if (!switch_rtp_ready(rtp_session)) {
8303  return -1;
8304  }
8305 
8306  if (!switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO) &&
8308  rtp_bundle_session = switch_core_media_get_rtp_session(rtp_session->session, SWITCH_MEDIA_TYPE_VIDEO);
8309  }
8310 
8311  if (!rtp_bundle_session && !rtp_write_ready(rtp_session, datalen, __LINE__)) {
8312  return 0;
8313  }
8314 
8315  WRITE_INC(rtp_session);
8316 
8317  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
8318  //switch_core_timer_sync(&rtp_session->write_timer);
8319  }
8320 
8321  if (send_msg) {
8322  bytes = datalen;
8323 
8324  m = (uint8_t) send_msg->header.m;
8325  rtp_session->ts = ntohl(send_msg->header.ts);
8326 
8327  if (flags && *flags & SFF_RFC2833) {
8328  if (rtp_session->te == INVALID_PT) {
8329  ret = 0;
8330  goto end;
8331  }
8332  send_msg->header.pt = rtp_session->te;
8333  }
8334  data = send_msg->body;
8335  if (datalen > rtp_header_len) {
8336  datalen -= rtp_header_len;
8337  }
8338  } else {
8339  if (*flags & SFF_RFC2833) {
8340  if (rtp_session->te == INVALID_PT) {
8341  ret = 0;
8342  goto end;
8343  }
8344  payload = rtp_session->te;
8345  }
8346 
8347  send_msg = &rtp_session->send_msg;
8348  send_msg->header.pt = payload;
8349 
8350  m = get_next_write_ts(rtp_session, timestamp);
8351 
8352  rtp_session->send_msg.header.ts = htonl(rtp_session->ts);
8353 
8354  memcpy(send_msg->body, data, datalen);
8355  bytes = datalen + rtp_header_len;
8356  }
8357 
8358  if (!switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO)) {
8359 
8360  if ((rtp_session->rtp_bugs & RTP_BUG_NEVER_SEND_MARKER)) {
8361  m = 0;
8362  } else {
8363  int delta = rtp_session->ts - rtp_session->last_write_ts;
8364 
8365  if (!rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] &&
8366  ((!rtp_session->flags[SWITCH_RTP_FLAG_RESET] && (abs(delta) > rtp_session->samples_per_interval * 10))
8367  || rtp_session->ts == rtp_session->samples_per_interval)) {
8368  m++;
8369  }
8370 
8371  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
8372  //switch_core_timer_sync(&rtp_session->write_timer);
8373  }
8374 
8375  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] &&
8376  (rtp_session->write_timer.samplecount - rtp_session->last_write_samplecount) > rtp_session->samples_per_interval * 10) {
8377  m++;
8378  }
8379 
8380  if (!rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] &&
8381  ((unsigned) ((switch_micro_time_now() - rtp_session->last_write_timestamp))) > (rtp_session->ms_per_packet * 10)) {
8382  m++;
8383  }
8384 
8385  if (rtp_session->cn && payload != rtp_session->cng_pt) {
8386  rtp_session->cn = 0;
8387  m++;
8388  }
8389 
8390  if (rtp_session->need_mark && !rtp_session->sending_dtmf) {
8391  m++;
8392  rtp_session->need_mark = 0;
8393  }
8394  }
8395 
8396  if (m) {
8397  rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 1;
8398  rtp_session->ts = 0;
8399  }
8400 
8401  /* If the marker was set, and the timestamp seems to have started over - set a new SSRC, to indicate this is a new stream */
8402  if (m && !switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_SEND) && (rtp_session->rtp_bugs & RTP_BUG_CHANGE_SSRC_ON_MARKER) &&
8403  (rtp_session->flags[SWITCH_RTP_FLAG_RESET] || (rtp_session->ts <= rtp_session->last_write_ts && rtp_session->last_write_ts > 0))) {
8404  switch_rtp_set_ssrc(rtp_session, (uint32_t) ((intptr_t) rtp_session + (switch_time_t) switch_epoch_time_now(NULL)));
8405  }
8406 
8408  send_msg->header.m = (m && !(rtp_session->rtp_bugs & RTP_BUG_NEVER_SEND_MARKER)) ? 1 : 0;
8409  }
8410  }
8411 
8412  if (switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO)) {
8413  int external = (flags && *flags & SFF_EXTERNAL);
8414  /* Normalize the timestamps to our own base by generating a made up starting point then adding the measured deltas to that base
8415  so if the timestamps and ssrc of the source change, it will not break the other end's jitter bufffer / decoder etc *cough* CHROME *cough*
8416  */
8417 
8418  if (!rtp_session->ts_norm.ts) {
8419  rtp_session->ts_norm.ts = (uint32_t) rand() % 1000000 + 1;
8420  }
8421 
8422  if (!rtp_session->ts_norm.last_ssrc || send_msg->header.ssrc != rtp_session->ts_norm.last_ssrc || rtp_session->ts_norm.last_external != external) {
8423  switch_core_session_t *other_session;
8424 
8427 
8428  if (switch_core_session_get_partner(rtp_session->session, &other_session) == SWITCH_STATUS_SUCCESS) {
8430  switch_core_media_gen_key_frame(other_session);
8431  switch_core_session_rwunlock(other_session);
8432  }
8433 
8434  if (rtp_session->ts_norm.last_ssrc) {
8435  rtp_session->ts_norm.delta_ttl = 0;
8436  rtp_session->ts_norm.ts++;
8437  }
8438 
8439  rtp_session->ts_norm.last_ssrc = send_msg->header.ssrc;
8440  rtp_session->ts_norm.last_frame = ntohl(send_msg->header.ts);
8441  }
8442 
8443  rtp_session->ts_norm.last_external = external;
8444 
8445  if (ntohl(send_msg->header.ts) != rtp_session->ts_norm.last_frame) {
8446  int32_t delta = ntohl(send_msg->header.ts) - rtp_session->ts_norm.last_frame;
8447 
8448  if (delta < 0 || delta > 90000) {
8451  "Timestamp shift detected last: %d this: %d delta: %d stick with prev delta: %d\n",
8452  rtp_session->ts_norm.last_frame, ntohl(send_msg->header.ts), delta, rtp_session->ts_norm.delta);
8453  } else {
8454  rtp_session->ts_norm.delta = delta;
8455  }
8456 
8457  rtp_session->ts_norm.ts += rtp_session->ts_norm.delta;
8458 
8459  }
8460 
8461  rtp_session->ts_norm.last_frame = ntohl(send_msg->header.ts);
8462  send_msg->header.ts = htonl(rtp_session->ts_norm.ts);
8463  this_ts = rtp_session->ts_norm.ts;
8464  }
8465 
8466  send_msg->header.ssrc = htonl(rtp_session->ssrc);
8467 
8468  if (rtp_session->flags[SWITCH_RTP_FLAG_GOOGLEHACK] && rtp_session->send_msg.header.pt == 97) {
8469  rtp_session->last_rtp_hdr.pt = 102;
8470  }
8471 
8472  if (rtp_session->flags[SWITCH_RTP_FLAG_VAD] &&
8473  rtp_session->last_rtp_hdr.pt == rtp_session->vad_data.read_codec->implementation->ianacode) {
8474 
8475  int16_t decoded[SWITCH_RECOMMENDED_BUFFER_SIZE / sizeof(int16_t)] = { 0 };
8476  uint32_t rate = 0;
8477  uint32_t codec_flags = 0;
8478  uint32_t len = sizeof(decoded);
8479  time_t now = switch_epoch_time_now(NULL);
8480  send = 0;
8481 
8482  if (rtp_session->vad_data.scan_freq && rtp_session->vad_data.next_scan <= now) {
8483  rtp_session->vad_data.bg_count = rtp_session->vad_data.bg_level = 0;
8484  rtp_session->vad_data.next_scan = now + rtp_session->vad_data.scan_freq;
8485  }
8486 
8487  if (switch_core_codec_decode(&rtp_session->vad_data.vad_codec,
8488  rtp_session->vad_data.read_codec,
8489  data,
8490  datalen,
8492  decoded, &len, &rate, &codec_flags) == SWITCH_STATUS_SUCCESS) {
8493 
8494  uint32_t energy = 0;
8495  uint32_t x, y = 0, z = len / sizeof(int16_t);
8496  uint32_t score = 0;
8497  int divisor = 0;
8498  if (z) {
8499 
8500  if (!(divisor = rtp_session->vad_data.read_codec->implementation->actual_samples_per_second / 8000)) {
8501  divisor = 1;
8502  }
8503 
8504  for (x = 0; x < z; x++) {
8505  energy += abs(decoded[y]);
8507  }
8508 
8509  if (++rtp_session->vad_data.start_count < rtp_session->vad_data.start) {
8510  send = 1;
8511  } else {
8512  score = (energy / (z / divisor));
8513  if (score && (rtp_session->vad_data.bg_count < rtp_session->vad_data.bg_len)) {
8514  rtp_session->vad_data.bg_level += score;
8515  if (++rtp_session->vad_data.bg_count == rtp_session->vad_data.bg_len) {
8516  rtp_session->vad_data.bg_level /= rtp_session->vad_data.bg_len;
8517  }
8518  send = 1;
8519  } else {
8520  if (score > rtp_session->vad_data.bg_level && !switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)) {
8521  uint32_t diff = score - rtp_session->vad_data.bg_level;
8522 
8523  if (rtp_session->vad_data.hangover_hits) {
8524  rtp_session->vad_data.hangover_hits--;
8525  }
8526 
8527  if (diff >= rtp_session->vad_data.diff_level || ++rtp_session->vad_data.hangunder_hits >= rtp_session->vad_data.hangunder) {
8528 
8530 
8531  rtp_session->vad_data.start_talking = switch_micro_time_now();
8532 
8533  if (!(rtp_session->rtp_bugs & RTP_BUG_NEVER_SEND_MARKER)) {
8534  send_msg->header.m = 1;
8535  }
8536  rtp_session->vad_data.hangover_hits = rtp_session->vad_data.hangunder_hits = rtp_session->vad_data.cng_count = 0;
8538 
8539  if ((rtp_session->vad_data.fire_events & VAD_FIRE_TALK)) {
8540  switch_event_t *event;
8543  switch_event_fire(&event);
8544  }
8545  }
8546  }
8547  }
8548  } else {
8549  if (rtp_session->vad_data.hangunder_hits) {
8550  rtp_session->vad_data.hangunder_hits--;
8551  }
8552  if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)) {
8553  if (++rtp_session->vad_data.hangover_hits >= rtp_session->vad_data.hangover) {
8554  rtp_session->vad_data.stop_talking = switch_micro_time_now();
8555  rtp_session->vad_data.total_talk_time += (rtp_session->vad_data.stop_talking - rtp_session->vad_data.start_talking);
8556 
8558 
8559  rtp_session->vad_data.hangover_hits = rtp_session->vad_data.hangunder_hits = rtp_session->vad_data.cng_count = 0;
8561 
8562  if ((rtp_session->vad_data.fire_events & VAD_FIRE_NOT_TALK)) {
8563  switch_event_t *event;
8566  switch_event_fire(&event);
8567  }
8568  }
8569  }
8570  }
8571  }
8572  }
8573  }
8574  }
8575 
8576  if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)) {
8577  send = 1;
8578  }
8579  }
8580  } else {
8581  ret = -1;
8582  goto end;
8583  }
8584  }
8585 
8586  if (!switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO)) {
8587  uint32_t ts_delta;
8588 
8589  this_ts = ntohl(send_msg->header.ts);
8590 
8591  ts_delta = abs((int32_t)(this_ts - rtp_session->last_write_ts));
8592 
8593  if (ts_delta > rtp_session->samples_per_second * 2) {
8594  rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 1;
8595  }
8596 #ifdef DEBUG_TS_ROLLOVER
8597  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);
8598 #endif
8599  if ((!(flags && *flags & SFF_RFC2833) && ts_delta == 0) || !switch_rtp_ready(rtp_session) || rtp_session->sending_dtmf) {
8600  send = 0;
8601  }
8602  }
8603 
8604  if (rtp_session->flags[SWITCH_RTP_FLAG_PAUSE]) {
8605  send = 0;
8606  }
8607 
8608  if (send) {
8609  int delta = 1;
8610 
8611  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && (*flags & SFF_EXTERNAL) &&
8612  rtp_session->stats.outbound.packet_count && rtp_session->flags[SWITCH_RTP_FLAG_PASSTHRU]) {
8613  int32_t x = rtp_session->last_write_seq;
8614  int32_t y = ntohs(send_msg->header.seq);
8615 
8616  if (!rtp_session->video_delta_mode) {
8617  rtp_session->video_delta_mode = 1;
8618  } else {
8619  if (x > UINT16_MAX / 2 && y < UINT16_MAX / 2) {
8620  x -= (int32_t)UINT16_MAX+1;
8621  }
8622 
8623  delta = y-x;
8624  }
8625 
8626  rtp_session->last_write_seq = y;
8627  }
8628 
8629  if (!rtp_session->flags[SWITCH_RTP_FLAG_PASSTHRU]) {
8630  rtp_session->video_delta_mode = 0;
8631  }
8632 
8633  rtp_session->seq += delta;
8634 
8635  send_msg->header.seq = htons(rtp_session->seq);
8636 
8637  if (rtp_session->flags[SWITCH_RTP_FLAG_BYTESWAP] && send_msg->header.pt == rtp_session->payload) {
8638  switch_swap_linear((int16_t *)send_msg->body, (int) datalen);
8639  }
8640 
8641  if (rtp_bundle_session) {
8642  tmp_session = rtp_session;
8643  rtp_session = rtp_bundle_session;
8644  }
8645 
8646 #ifdef ENABLE_SRTP
8647  switch_mutex_lock(rtp_session->ice_mutex);
8648  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND]) {
8649  int sbytes = (int) bytes;
8650  srtp_err_status_t stat;
8651 
8652 
8653  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_RESET] || !rtp_session->send_ctx[rtp_session->srtp_idx_rtp]) {
8654 
8656  srtp_dealloc(rtp_session->send_ctx[rtp_session->srtp_idx_rtp]);
8657  rtp_session->send_ctx[rtp_session->srtp_idx_rtp] = NULL;
8658  if (srtp_create(&rtp_session->send_ctx[rtp_session->srtp_idx_rtp],
8659  &rtp_session->send_policy[rtp_session->srtp_idx_rtp]) || !rtp_session->send_ctx[rtp_session->srtp_idx_rtp]) {
8661  "Error! RE-Activating %s Secure RTP SEND\n", rtp_type(rtp_session));
8662  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND] = 0;
8663  ret = -1;
8664  switch_mutex_unlock(rtp_session->ice_mutex);
8665  goto end;
8666  } else {
8668  "RE-Activating %s Secure RTP SEND\n", rtp_type(rtp_session));
8669  }
8670  }
8671 
8672  if (!rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_MKI]) {
8673  stat = srtp_protect(rtp_session->send_ctx[rtp_session->srtp_idx_rtp], &send_msg->header, &sbytes);
8674  } else {
8675  stat = srtp_protect_mki(rtp_session->send_ctx[rtp_session->srtp_idx_rtp], &send_msg->header, &sbytes, 1, SWITCH_CRYPTO_MKI_INDEX);
8676  }
8677 
8678  if (stat) {
8680  "Error: %s SRTP protection failed with code %d\n", rtp_type(rtp_session), stat);
8681  }
8682 
8683  bytes = sbytes;
8684  }
8685  switch_mutex_unlock(rtp_session->ice_mutex);
8686 #endif
8687 
8688  now = switch_micro_time_now();
8689 #ifdef RTP_DEBUG_WRITE_DELTA
8690  {
8691  int delta = (int) (now - rtp_session->send_time) / 1000;
8692  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "WRITE %d delta %d\n", (int) bytes, delta);
8693  }
8694 #endif
8695  rtp_session->send_time = now;
8696 
8697  if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]) {
8698  const char *tx_host;
8699  const char *old_host;
8700  const char *my_host;
8701 
8702  char bufa[50], bufb[50], bufc[50];
8703 
8704 
8705  tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtp_from_addr);
8706  old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
8707  my_host = switch_get_addr(bufc, sizeof(bufc), rtp_session->local_addr);
8708 
8710  "W %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n",
8711  rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "NoName",
8712  (long) bytes,
8713  my_host, switch_sockaddr_get_port(rtp_session->local_addr),
8714  old_host, rtp_session->remote_port,
8715  tx_host, switch_sockaddr_get_port(rtp_session->rtp_from_addr),
8716  send_msg->header.pt, ntohl(send_msg->header.ts), ntohs(send_msg->header.seq), send_msg->header.m);
8717 
8718  }
8719 
8720  if (rtp_session->flags[SWITCH_RTP_FLAG_NACK]) {
8721  switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
8722 
8723  if (!rtp_session->vbw) {
8724  int nack_size = 100;
8725  const char *var;
8726 
8727  if ((var = switch_channel_get_variable(channel, "rtp_nack_buffer_size"))) {
8728  int tmp = atoi(var);
8729 
8730  if (tmp > 0 && tmp < 500) {
8731  nack_size = tmp;
8732  }
8733  }
8734 
8735  switch_jb_create(&rtp_session->vbw, SJB_VIDEO, nack_size, nack_size, rtp_session->pool);
8736 
8737  if (rtp_session->vbw) {
8738  switch_jb_set_flag(rtp_session->vbw, SJB_QUEUE_ONLY);
8739  //switch_jb_debug_level(rtp_session->vbw, 10);
8740  }
8741  }
8742  switch_jb_put_packet(rtp_session->vbw, (switch_rtp_packet_t *)send_msg, bytes);
8743  }
8744 
8745 #ifdef RTP_WRITE_PLOSS
8746  {
8747  int r = (rand() % 10000) + 1;
8748 
8749  if (r <= 200) {
8751  "Simulate dropping packet ......... ts: %u seq: %u\n", ntohl(send_msg->header.ts), ntohs(send_msg->header.seq));
8752  } else {
8753  if (switch_socket_sendto(rtp_session->sock_output, rtp_session->remote_addr, 0, (void *) send_msg, &bytes) != SWITCH_STATUS_SUCCESS) {
8754  rtp_session->seq--;
8755  ret = -1;
8756  goto end;
8757  }
8758  }
8759  }
8760 #else
8761  //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
8762  //
8763  // rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_READ]++;
8764  //
8765  // //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "SEND %u\n", ntohs(send_msg->header.seq));
8766  //}
8767  if (switch_socket_sendto(rtp_session->sock_output, rtp_session->remote_addr, 0, (void *) send_msg, &bytes) != SWITCH_STATUS_SUCCESS) {
8768  rtp_session->seq -= delta;
8769 
8770  ret = -1;
8771  goto end;
8772  }
8773 #endif
8774  if (rtp_bundle_session) rtp_session = tmp_session;
8775 
8776  rtp_session->last_write_ts = this_ts;
8777  rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 0;
8778 
8779  if (rtp_session->queue_delay) {
8780  rtp_session->delay_samples = rtp_session->queue_delay;
8781  rtp_session->queue_delay = 0;
8782  }
8783 
8784  rtp_session->stats.outbound.raw_bytes += bytes;
8785  rtp_session->stats.outbound.packet_count++;
8786 
8787  if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
8788  rtp_session->stats.rtcp.sent_pkt_count++;
8789  }
8790 
8791  if (send_msg->header.pt == rtp_session->cng_pt) {
8792  rtp_session->stats.outbound.cng_packet_count++;
8793  } else {
8794  rtp_session->stats.outbound.media_packet_count++;
8795  rtp_session->stats.outbound.media_bytes += bytes;
8796  }
8797 
8798  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
8799  //switch_core_timer_sync(&rtp_session->write_timer);
8800  rtp_session->last_write_samplecount = rtp_session->write_timer.samplecount;
8801  }
8802 
8804  }
8805 
8806  ret = (int) bytes;
8807 
8808  end:
8809 
8810  WRITE_DEC(rtp_session);
8811 
8812  return ret;
8813 }
uint32_t queue_delay
Definition: switch_rtp.c:390
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:216
#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:5147
#define SWITCH_CHANNEL_LOG
switch_sockaddr_t * remote_addr
Definition: switch_rtp.c:340
uint32_t ts
Definition: switch_rtp.c:309
uint32_t delay_samples
Definition: switch_rtp.c:387
#define SWITCH_RECOMMENDED_BUFFER_SIZE
Definition: switch_types.h:590
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:468
switch_port_t remote_port
Definition: switch_rtp.c:417
Representation of an event.
Definition: switch_event.h:80
srtp_policy_t send_policy[2]
Definition: switch_rtp.c:356
struct switch_rtp_vad_data vad_data
Definition: switch_rtp.c:421
#define SWITCH_CRYPTO_MKI_INDEX
uint32_t last_ssrc
Definition: switch_rtp.c:307
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:436
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:683
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:196
switch_core_session_t * session
Definition: switch_rtp.c:195
uint16_t last_write_seq
Definition: switch_rtp.c:467
#define rtp_type(rtp_session)
Definition: switch_rtp.c:540
switch_payload_t cng_pt
Definition: switch_rtp.c:425
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:353
#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:762
#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:682
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:380
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:392
#define switch_core_session_request_video_refresh(_s)
Definition: switch_core.h:2881
#define switch_channel_get_variable(_c, _v)
switch_sockaddr_t * rtp_from_addr
Definition: switch_rtp.c:394
switch_rtp_numbers_t outbound
Definition: switch_types.h:761
uint32_t samples_per_second
Definition: switch_rtp.c:406
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:4458
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:8270
uintptr_t switch_size_t
switch_sockaddr_t * local_addr
Definition: switch_rtp.c:326
uint16_t seq
Definition: switch_rtp.c:371
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:214
#define switch_core_session_get_partner(_session, _partner)
Definition: switch_core.h:1028
uint8_t need_mark
Definition: switch_rtp.c:376
switch_size_t packet_count
Definition: switch_types.h:684
uint32_t delta
Definition: switch_rtp.c:310
switch_rtp_stats_t stats
Definition: switch_rtp.c:450
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:393
#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:5431
switch_time_t stop_talking
Definition: switch_rtp.c:215
uint8_t cn
Definition: switch_rtp.c:433
uint32_t samples_per_interval
Definition: switch_rtp.c:405
switch_time_t send_time
Definition: switch_rtp.c:462
srtp_hdr_t header
Definition: switch_rtp.c:112
uint32_t last_write_ts
Definition: switch_rtp.c:382
switch_mutex_t * ice_mutex
Definition: switch_rtp.c:429
switch_core_session_t * session
Definition: switch_rtp.c:472
#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:431
switch_time_t last_write_timestamp
Definition: switch_rtp.c:391
int8_t sending_dtmf
Definition: switch_rtp.c:375
switch_size_t media_packet_count
Definition: switch_types.h:686
switch_rtp_bug_flag_t rtp_bugs
Definition: switch_rtp.c:449
int srtp_idx_rtp
Definition: switch_rtp.c:363
uint32_t ms_per_packet
Definition: switch_rtp.c:410
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:423
uint32_t ssrc
Definition: switch_rtp.c:372
#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:5426
uint32_t last_write_samplecount
Definition: switch_rtp.c:386
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:203
rtp_msg_t send_msg
Definition: switch_rtp.c:327
static const switch_payload_t INVALID_PT
Definition: switch_rtp.c:81
uint32_t last_frame
Definition: switch_rtp.c:308
rtp_hdr_t last_rtp_hdr
Definition: switch_rtp.c:369
char * switch_channel_get_name(switch_channel_t *channel)
Retrieve the name of a given channel.
switch_payload_t payload
Definition: switch_rtp.c:377
switch_codec_t * read_codec
Definition: switch_rtp.c:197
static uint8_t get_next_write_ts(switch_rtp_t *rtp_session, uint32_t timestamp)
Definition: switch_rtp.c:1543
#define rtp_header_len
Definition: switch_rtp.c:73
uint32_t delta_ttl
Definition: switch_rtp.c:311
switch_socket_t * sock_output
Definition: switch_rtp.c:322
ts_normalize_t ts_norm
Definition: switch_rtp.c:339
switch_size_t cng_packet_count
Definition: switch_types.h:690

◆ rtp_write_ready()

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

Definition at line 8270 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().

8271 {
8272  if (!rtp_session) return 0;
8273 
8274  if (rtp_session->ice.ice_user && !(rtp_session->ice.rready || rtp_session->ice.ready)) {
8275  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",
8276  rtp_type(rtp_session), (long)bytes, line);
8277  return 0;
8278  }
8279 
8280  if (rtp_session->dtls && rtp_session->dtls->state != DS_READY) {
8281  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",
8282  rtp_type(rtp_session), (long)bytes, line);
8283  return 0;
8284  }
8285 
8286  return 1;
8287 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
dtls_state_t state
Definition: switch_rtp.c:280
#define rtp_type(rtp_session)
Definition: switch_rtp.c:540
switch_dtls_t * dtls
Definition: switch_rtp.c:366
switch_core_session_t * session
Definition: switch_rtp.c:472
switch_rtp_ice_t ice
Definition: switch_rtp.c:397
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 5452 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().

5453 {
5454  int upsamp, max_upsamp;
5455 
5456 
5457  if (!max_ms) max_ms = ms;
5458 
5459  upsamp = ms * (rtp_session->samples_per_second / 1000);
5460  max_upsamp = max_ms * (rtp_session->samples_per_second / 1000);
5461 
5462  rtp_session->sending_dtmf = 0;
5463  rtp_session->queue_delay = upsamp;
5464 
5465  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
5466  rtp_session->max_next_write_samplecount = rtp_session->timer.samplecount + max_upsamp;
5467  rtp_session->next_write_samplecount = rtp_session->timer.samplecount + upsamp;
5468  rtp_session->last_write_ts += upsamp;
5469  }
5470 
5471  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Queue digit delay of %dms\n", ms);
5472 }
uint32_t queue_delay
Definition: switch_rtp.c:390
#define SWITCH_CHANNEL_SESSION_LOG(x)
uint32_t max_next_write_samplecount
Definition: switch_rtp.c:389
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:392
uint32_t samples_per_second
Definition: switch_rtp.c:406
switch_timer_t timer
Definition: switch_rtp.c:430
uint32_t last_write_ts
Definition: switch_rtp.c:382
switch_core_session_t * session
Definition: switch_rtp.c:472
int8_t sending_dtmf
Definition: switch_rtp.c:375
uint32_t next_write_samplecount
Definition: switch_rtp.c:388
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 4059 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().

4060 {
4061 #ifndef ENABLE_SRTP
4062  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_CRIT, "SRTP NOT SUPPORTED IN THIS BUILD!\n");
4063  return SWITCH_STATUS_FALSE;
4064 #else
4065 
4066  switch_rtp_crypto_key_t *crypto_key;
4067  srtp_policy_t *policy;
4068  srtp_err_status_t stat;
4070 
4071  switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
4072  switch_event_t *fsevent = NULL;
4073  int idx = 0;
4074  const char *var;
4075  unsigned char b64_key[512] = "";
4076 
4077  unsigned char *keysalt = NULL;
4078  switch_size_t keysalt_len = 0;
4079 
4080  switch_crypto_key_material_t *key_material = NULL;
4081  unsigned long *key_material_n = NULL;
4082  srtp_master_key_t **mkis = NULL;
4083  srtp_master_key_t *mki = NULL;
4084  int mki_idx = 0;
4085 
4087 
4088  if (direction >= SWITCH_RTP_CRYPTO_MAX || keysalt_len > SWITCH_RTP_MAX_CRYPTO_LEN) {
4089  return SWITCH_STATUS_FALSE;
4090  }
4091 
4092  if (direction == SWITCH_RTP_CRYPTO_RECV_RTCP) {
4093  direction = SWITCH_RTP_CRYPTO_RECV;
4094  rtp_session->srtp_idx_rtcp = idx = 1;
4095  } else if (direction == SWITCH_RTP_CRYPTO_SEND_RTCP) {
4096  direction = SWITCH_RTP_CRYPTO_SEND;
4097  rtp_session->srtp_idx_rtcp = idx = 1;
4098  }
4099 
4100  if (direction == SWITCH_RTP_CRYPTO_RECV) {
4101  policy = &rtp_session->recv_policy[idx];
4102  keysalt = ssec->remote_raw_key;
4103  key_material = ssec->remote_key_material_next;
4104  key_material_n = &ssec->remote_key_material_n;
4105  } else {
4106  policy = &rtp_session->send_policy[idx];
4107  keysalt = ssec->local_raw_key;
4108  key_material = ssec->local_key_material_next;
4109  key_material_n = &ssec->local_key_material_n;
4110  }
4111 
4112  switch_b64_encode(keysalt, keysalt_len, b64_key, sizeof(b64_key));
4113 
4114  if (switch_true(switch_core_get_variable("rtp_retain_crypto_keys"))) {
4115  switch(direction) {
4117  switch_channel_set_variable(channel, "srtp_local_crypto_key", (const char *)b64_key);
4118  break;
4120  switch_channel_set_variable(channel, "srtp_remote_crypto_key", (const char *)b64_key);
4121  break;
4122  default:
4123  break;
4124  }
4125 
4126  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
4127  switch(direction) {
4129  switch_channel_set_variable(channel, "srtp_local_video_crypto_key", (const char *)b64_key);
4130  break;
4132  switch_channel_set_variable(channel, "srtp_remote_video_crypto_key", (const char *)b64_key);
4133  break;
4134  default:
4135  break;
4136  }
4137 
4138  } else {
4139  switch(direction) {
4141  switch_channel_set_variable(channel, "srtp_local_audio_crypto_key", (const char *)b64_key);
4142  break;
4144  switch_channel_set_variable(channel, "srtp_remote_audio_crypto_key", (const char *)b64_key);
4145  break;
4146  default:
4147  break;
4148  }
4149  }
4150  }
4151 
4152  crypto_key = switch_core_alloc(rtp_session->pool, sizeof(*crypto_key));
4153 
4154  crypto_key->type = ssec->crypto_type;
4155  crypto_key->index = index;
4156  memcpy(crypto_key->keysalt, keysalt, keysalt_len);
4157  crypto_key->next = rtp_session->crypto_keys[direction];
4158  rtp_session->crypto_keys[direction] = crypto_key;
4159 
4160  memset(policy, 0, sizeof(*policy));
4161 
4162  /* many devices can't handle gaps in SRTP streams */
4163  if (!((var = switch_channel_get_variable(channel, "srtp_allow_idle_gaps"))
4164  && switch_true(var))
4165  && (!(var = switch_channel_get_variable(channel, "send_silence_when_idle"))
4166  || !(atoi(var)))) {
4167  switch_channel_set_variable(channel, "send_silence_when_idle", "-1");
4168  }
4169 
4170  switch (crypto_key->type) {
4172  srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy->rtp);
4173  srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy->rtcp);
4174 
4176  switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_128_HMAC_SHA1_80");
4177  }
4178  break;
4180  srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(&policy->rtp);
4181  srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(&policy->rtcp);
4182 
4183 
4185  switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_128_HMAC_SHA1_32");
4186  }
4187  break;
4188 
4189  case AEAD_AES_256_GCM_8:
4190  srtp_crypto_policy_set_aes_gcm_256_8_auth(&policy->rtp);
4191  srtp_crypto_policy_set_aes_gcm_256_8_auth(&policy->rtcp);
4192 
4194  switch_channel_set_variable(channel, "rtp_has_crypto", "AEAD_AES_256_GCM_8");
4195  }
4196  break;
4197 
4198  case AEAD_AES_256_GCM:
4199  srtp_crypto_policy_set_aes_gcm_256_16_auth(&policy->rtp);
4200  srtp_crypto_policy_set_aes_gcm_256_16_auth(&policy->rtcp);
4201 
4203  switch_channel_set_variable(channel, "rtp_has_crypto", "AEAD_AES_256_GCM");
4204  }
4205  break;
4206 
4207  case AEAD_AES_128_GCM_8:
4208  srtp_crypto_policy_set_aes_gcm_128_8_auth(&policy->rtp);
4209  srtp_crypto_policy_set_aes_gcm_128_8_auth(&policy->rtcp);
4210 
4212  switch_channel_set_variable(channel, "rtp_has_crypto", "AEAD_AES_128_GCM_8");
4213  }
4214  break;
4215 
4216  case AEAD_AES_128_GCM:
4217  srtp_crypto_policy_set_aes_gcm_128_16_auth(&policy->rtp);
4218  srtp_crypto_policy_set_aes_gcm_128_16_auth(&policy->rtcp);
4219 
4221  switch_channel_set_variable(channel, "rtp_has_crypto", "AEAD_AES_128_GCM");
4222  }
4223  break;
4224 
4226  srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy->rtp);
4227  srtp_crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy->rtcp);
4229  switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_256_HMAC_SHA1_80");
4230  }
4231  break;
4233  srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32(&policy->rtp);
4234  srtp_crypto_policy_set_aes_cm_256_hmac_sha1_32(&policy->rtcp);
4236  switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_256_HMAC_SHA1_32");
4237  }
4238  break;
4240  srtp_crypto_policy_set_aes_cm_192_hmac_sha1_80(&policy->rtp);
4241  srtp_crypto_policy_set_aes_cm_192_hmac_sha1_80(&policy->rtcp);
4243  switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_192_HMAC_SHA1_80");
4244  }
4245  break;
4247  srtp_crypto_policy_set_aes_cm_192_hmac_sha1_32(&policy->rtp);
4248  srtp_crypto_policy_set_aes_cm_192_hmac_sha1_32(&policy->rtcp);
4250  switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_192_HMAC_SHA1_32");
4251  }
4252  break;
4253  case AES_CM_128_NULL_AUTH:
4254  srtp_crypto_policy_set_aes_cm_128_null_auth(&policy->rtp);
4255  srtp_crypto_policy_set_aes_cm_128_null_auth(&policy->rtcp);
4256 
4258  switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_128_NULL_AUTH");
4259  }
4260  break;
4261  default:
4262  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Missing crypto type!\n");
4263  break;
4264  }
4265 
4266  /* Setup the policy with MKI if they are used. Number of key materials must be positive to use MKI. */
4267  if (key_material && (*key_material_n > 0)) {
4268 
4269  if (direction == SWITCH_RTP_CRYPTO_RECV) {
4270  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_MKI] = 1; /* tell the rest of the environment MKI is used */
4271  } else {
4272  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_MKI] = 1; /* tell the rest of the environment MKI is used */
4273  }
4274 
4275  /* key must be NULL for libsrtp to work correctly with MKI. */
4276  policy->key = NULL;
4277 
4278  /* Allocate array for MKIs. */
4279  mkis = switch_core_alloc(rtp_session->pool, *key_material_n * sizeof(srtp_master_key_t*));
4280  if (!mkis) {
4281  return SWITCH_STATUS_FALSE;
4282  }
4283 
4284  /* Build array of MKIs. */
4285  mki_idx = 0;
4286 
4287  while (key_material && (mki_idx < *key_material_n)) {
4288 
4289  if (key_material->mki_size < 1) {
4290  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "MKI bad key size at MKI %d\n", mki_idx);
4291  return SWITCH_STATUS_FALSE;
4292  }
4293 
4294  mki = switch_core_alloc(rtp_session->pool, sizeof(srtp_master_key_t));
4295  if (!mki) {
4296  return SWITCH_STATUS_FALSE;
4297  }
4298 
4299  /* Setup MKI. */
4300  mki->mki_id = switch_core_alloc(rtp_session->pool, sizeof(key_material->mki_size));
4301  if (!mki->mki_id) {
4302  return SWITCH_STATUS_FALSE;
4303  }
4304 
4305  mki->key = switch_core_alloc(rtp_session->pool, keysalt_len);
4306  if (!mki->key) {
4307  return SWITCH_STATUS_FALSE;
4308  }
4309 
4310  memcpy(mki->mki_id, &key_material->mki_id, key_material->mki_size);
4311  mki->mki_size = key_material->mki_size;
4312  memcpy(mki->key, key_material->raw_key, keysalt_len);
4313 
4314  mkis[mki_idx] = mki;
4315 
4316  key_material = key_material->next;
4317  ++mki_idx;
4318  }
4319 
4320  /* And pass the array of MKIs to libsrtp. */
4321  policy->keys = mkis;
4322  policy->num_master_keys = mki_idx;
4323 
4324  } else {
4325  policy->key = (uint8_t *) crypto_key->keysalt;
4326  }
4327 
4328  policy->next = NULL;
4329 
4330  policy->window_size = 1024;
4331  policy->allow_repeat_tx = 1;
4332 
4333  //policy->rtp.sec_serv = sec_serv_conf_and_auth;
4334  //policy->rtcp.sec_serv = sec_serv_conf_and_auth;
4335 
4336  switch (direction) {
4338  policy->ssrc.type = ssrc_any_inbound;
4339 
4340  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && idx == 0 && rtp_session->recv_ctx[idx]) {
4341  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_RESET] = 1;
4342  } else {
4343  if ((stat = srtp_create(&rtp_session->recv_ctx[idx], policy)) || !rtp_session->recv_ctx[idx]) {
4344  status = SWITCH_STATUS_FALSE;
4345  }
4346 
4347  if (status == SWITCH_STATUS_SUCCESS) {
4348  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "Activating %s Secure %s RECV%s\n",
4349  rtp_type(rtp_session), idx ? "RTCP" : "RTP", rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_MKI] ? " (with MKI)" : "");
4350  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] = 1;
4351  } else {
4352  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error allocating srtp [%d]\n", stat);
4353  return status;
4354  }
4355  }
4356  break;
4358  policy->ssrc.type = ssrc_any_outbound;
4359  //policy->ssrc.type = ssrc_specific;
4360  //policy->ssrc.value = rtp_session->ssrc;
4361 
4362  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND] && idx == 0 && rtp_session->send_ctx[idx]) {
4363  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_RESET] = 1;
4364  } else {
4365  if ((stat = srtp_create(&rtp_session->send_ctx[idx], policy)) || !rtp_session->send_ctx[idx]) {
4366  status = SWITCH_STATUS_FALSE;
4367  }
4368 
4369  if (status == SWITCH_STATUS_SUCCESS) {
4370  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "Activating %s Secure %s SEND%s\n",
4371  rtp_type(rtp_session), idx ? "RTCP" : "RTP", rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_MKI] ? " (with MKI)" : "");
4372  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND] = 1;
4373  } else {
4374  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error allocating SRTP [%d]\n", stat);
4375  return status;
4376  }
4377  }
4378 
4379  break;
4380  default:
4381  abort();
4382  break;
4383  }
4384 
4386  if (rtp_session->dtls) {
4387  switch_event_add_header(fsevent, SWITCH_STACK_BOTTOM, "secure_type", "srtp:dtls:AES_CM_128_HMAC_SHA1_80");
4388  switch_channel_set_variable(channel, "rtp_has_crypto", "srtp:dtls:AES_CM_128_HMAC_SHA1_80");
4389  } else {
4390  switch_event_add_header(fsevent, SWITCH_STACK_BOTTOM, "secure_type", "srtp:sdes:%s", switch_channel_get_variable(channel, "rtp_has_crypto"));
4391  }
4392  switch_event_add_header_string(fsevent, SWITCH_STACK_BOTTOM, "caller-unique-id", switch_channel_get_uuid(channel));
4393  switch_event_fire(&fsevent);
4394  }
4395 
4396 
4397  return SWITCH_STATUS_SUCCESS;
4398 #endif
4399 }
#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:364
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:356
switch_rtp_crypto_key_t * crypto_keys[SWITCH_RTP_CRYPTO_MAX]
Definition: switch_rtp.c:442
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:540
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:353
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:392
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:354
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:366
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:393
switch_status_t
Common return values.
srtp_policy_t recv_policy[2]
Definition: switch_rtp.c:357
switch_core_session_t * session
Definition: switch_rtp.c:472
#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 543 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().

544 {
545  int is_rtcp = ice == &rtp_session->rtcp_ice;
546  const char *err = "";
547  int i;
548  uint8_t ice_cand_found_idx = 0;
549 
550  for (i = 0; i < ice->ice_params->cand_idx[ice->proto]; i++) {
551  if (!strcmp(host, ice->ice_params->cands[i][ice->proto].con_addr) && port == ice->ice_params->cands[i][ice->proto].con_port) {
552  ice_cand_found_idx = i;
553  }
554  }
555 
556  if (!ice_cand_found_idx) {
557  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG5, "ICE candidate [%s:%d] replaced with [%s:%d]\n",
558  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);
559  ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_addr = switch_core_strdup(rtp_session->pool, host);
560  ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port = port;
561  } else {
562  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);
563  ice->ice_params->chosen[ice->proto] = ice_cand_found_idx;
564  }
565 
566  ice->missed_count = 0;
567 
568  if (is_rtcp) {
569  ice->addr = rtp_session->rtcp_remote_addr;
570  } else {
571  switch_rtp_set_remote_address(rtp_session, host, port, 0, SWITCH_FALSE, &err);
572 
573  if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
574  ice->addr = rtp_session->remote_addr;
575  }
576  }
577 
578 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
switch_sockaddr_t * remote_addr
Definition: switch_rtp.c:340
switch_sockaddr_t * addr
Definition: switch_rtp.c:248
#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:252
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:392
char * con_addr
Definition: switch_rtp.h:99
switch_rtp_ice_t rtcp_ice
Definition: switch_rtp.c:398
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:3125
int cand_idx[MAX_CAND_IDX_COUNT]
Definition: switch_rtp.h:115
switch_memory_pool_t * pool
Definition: switch_rtp.c:393
ice_proto_t proto
Definition: switch_rtp.c:253
switch_core_session_t * session
Definition: switch_rtp.c:472
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:340
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 3684 of file switch_rtp.c.

Referenced by switch_rtp_shutdown().

3684  {
3685 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
3687  BIO_meth_free(dtls_bio_filter_methods);
3688  dtls_bio_filter_methods = NULL;
3689  }
3690 #endif
3691 }
static BIO_METHOD dtls_bio_filter_methods
Definition: switch_rtp.c:3467

◆ switch_rtp_dtls_init()

static void switch_rtp_dtls_init ( void  )
static

Definition at line 3674 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().

3674  {
3675 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
3676  dtls_bio_filter_methods = BIO_meth_new(BIO_TYPE_FILTER | BIO_get_new_index(), "DTLS filter");
3677  BIO_meth_set_write(dtls_bio_filter_methods, dtls_bio_filter_write);
3678  BIO_meth_set_ctrl(dtls_bio_filter_methods, dtls_bio_filter_ctrl);
3679  BIO_meth_set_create(dtls_bio_filter_methods, dtls_bio_filter_new);
3680  BIO_meth_set_destroy(dtls_bio_filter_methods, dtls_bio_filter_free);
3681 #endif
3682 }
static int dtls_bio_filter_free(BIO *bio)
Definition: switch_rtp.c:3523
static long dtls_bio_filter_ctrl(BIO *bio, int cmd, long num, void *ptr)
Definition: switch_rtp.c:3605
static BIO_METHOD dtls_bio_filter_methods
Definition: switch_rtp.c:3467
static int dtls_bio_filter_new(BIO *bio)
Definition: switch_rtp.c:3497
static int dtls_bio_filter_write(BIO *bio, const char *in, int inl)
Definition: switch_rtp.c:3556

◆ switch_rtp_reset_vb()

void switch_rtp_reset_vb ( switch_rtp_t rtp_session)

Definition at line 2988 of file switch_rtp.c.

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

Referenced by switch_rtp_reset().

2989 {
2990 
2991  if (rtp_session->vb) {
2992  switch_jb_reset(rtp_session->vb);
2993  }
2994 
2995  if (rtp_session->vbw) {
2996  switch_jb_reset(rtp_session->vbw);
2997  }
2998 }
switch_jb_t * vbw
Definition: switch_rtp.c:436
switch_jb_t * vb
Definition: switch_rtp.c:435
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 2059 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().

2060 {
2061  if (sr && rtcp_report_block) {
2062  switch_event_t *event;
2063 
2065  char value[30];
2066  char header[50];
2067  uint32_t tmpLost;
2068  char *uuid = switch_core_session_get_uuid(rtp_session->session);
2069  if (uuid) {
2071  }
2072 
2073  snprintf(value, sizeof(value), "%.8x", rtp_session->stats.rtcp.ssrc);
2074  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "SSRC", value);
2075 
2076  snprintf(value, sizeof(value), "%u", ntohl(sr->sender_info.ntp_msw));
2077  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "NTP-Most-Significant-Word", value);
2078 
2079  snprintf(value, sizeof(value), "%u", ntohl(sr->sender_info.ntp_lsw));
2080  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "NTP-Least-Significant-Word", value);
2081 
2082  snprintf(value, sizeof(value), "%u", ntohl(sr->sender_info.ts));
2083  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "RTP-Timestamp", value);
2084 
2085  snprintf(value, sizeof(value), "%u", ntohl(sr->sender_info.pc));
2086  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Sender-Packet-Count", value);
2087 
2088  snprintf(value, sizeof(value), "%u", ntohl(sr->sender_info.oc));
2089  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Octect-Packet-Count", value);
2090 
2091  snprintf(value, sizeof(value), "%u", ntohl(sr->sender_info.ts));
2092  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Last-RTP-Timestamp", value);
2093 
2094  snprintf(value, sizeof(value), "%" SWITCH_TIME_T_FMT, switch_time_now());
2095  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Capture-Time", value);
2096 
2097  /* Add sources info */
2098  snprintf(header, sizeof(header), "Source-SSRC");
2099  snprintf(value, sizeof(value), "%.8x", rtp_session->stats.rtcp.peer_ssrc);
2100  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value);
2101  snprintf(header, sizeof(header), "Source-Fraction");
2102  snprintf(value, sizeof(value), "%u", (uint8_t)rtcp_report_block->fraction);
2103  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value);
2104  snprintf(header, sizeof(header), "Source-Lost");
2105 #if SWITCH_BYTE_ORDER == __BIG_ENDIAN
2106  tmpLost = rtcp_report_block->lost; /* signed 24bit will extended signess to int32_t automatically */
2107 #else
2108  tmpLost = ntohl(rtcp_report_block->lost)>>8;
2109  tmpLost = tmpLost | ((tmpLost & 0x00800000) ? 0xff000000 : 0x00000000); /* ...and signess compensation */
2110 #endif
2111  snprintf(value, sizeof(value), "%u", tmpLost);
2112  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value);
2113  snprintf(header, sizeof(header), "Source-Highest-Sequence-Number-Received");
2114  snprintf(value, sizeof(value), "%u", ntohl(rtcp_report_block->highest_sequence_number_received));
2115  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value);
2116  snprintf(header, sizeof(header), "Source-Jitter");
2117  snprintf(value, sizeof(value), "%u", ntohl(rtcp_report_block->jitter));
2118  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value);
2119  snprintf(header, sizeof(header), "Source-LSR");
2120  snprintf(value, sizeof(value), "%u", ntohl(rtcp_report_block->lsr));
2121  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value);
2122  snprintf(header, sizeof(header), "Source-DLSR");
2123  snprintf(value, sizeof(value), "%u", ntohl(rtcp_report_block->dlsr));
2124  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value);
2125 
2126  switch_event_fire(&event);
2127  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG10, "Dispatched RTCP SEND event\n");
2128  }
2129  }
2130 }
#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:496
switch_rtcp_numbers_t rtcp
Definition: switch_types.h:762
struct switch_rtcp_sender_info sender_info
Definition: switch_rtp.c:521
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:450
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:472
#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 2050 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().

2051 {
2052  if (rtp_session->ice.ice_user || rtp_session->rtcp_ice.ice_user) {
2053  return 1;
2054  }
2055 
2056  return 0;
2057 }
switch_rtp_ice_t rtcp_ice
Definition: switch_rtp.c:398
switch_rtp_ice_t ice
Definition: switch_rtp.c:397

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:3523
static long dtls_bio_filter_ctrl(BIO *bio, int cmd, long num, void *ptr)
Definition: switch_rtp.c:3605
static int dtls_bio_filter_new(BIO *bio)
Definition: switch_rtp.c:3497
static int dtls_bio_filter_write(BIO *bio, const char *in, int inl)
Definition: switch_rtp.c:3556

Definition at line 3467 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 3199 of file switch_rtp.c.

◆ dtls_states

Definition at line 304 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 814 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().