FreeSWITCH API Documentation  1.7.0
Data Structures | Macros | Typedefs | Enumerations | Functions | Variables
switch_rtp.c File Reference
#include <switch.h>
#include <switch_private.h>
#include <switch_stun.h>
#include <apr_network_io.h>
#include <datatypes.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
 

Macros

#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)   switch_mutex_unlock(rtp_session->read_mutex); rtp_session->reading--
 
#define WRITE_INC(rtp_session)   switch_mutex_lock(rtp_session->write_mutex); rtp_session->writing++
 
#define WRITE_DEC(rtp_session)   switch_mutex_unlock(rtp_session->write_mutex); rtp_session->writing--
 
#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 ZRTP_MAGIC_COOKIE   0x5a525450
 
#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 RTP_BODY(_s)   (char *) (_s->recv_msg.ebody ? _s->recv_msg.ebody : _s->recv_msg.body)
 
#define rtp_type(rtp_session)   rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio"
 
#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
 

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 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 switch_status_t ice_out (switch_rtp_t *rtp_session, switch_rtp_ice_t *ice)
 
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, int force)
 
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 void rtcp_generate_report_block (switch_rtp_t *rtp_session, struct switch_rtcp_report_block *rtcp_report_block)
 
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 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_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)
 
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)
 
switch_status_t switch_rtp_add_crypto_key (switch_rtp_t *rtp_session, switch_rtp_crypto_direction_t direction, uint32_t index, switch_rtp_crypto_key_type_t type, unsigned char *key, switch_size_t keylen)
 
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)
 
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)
 
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_rtp_packet (switch_rtp_t *rtp_session, switch_size_t *bytes, switch_frame_flag_t *flags, 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 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...
 

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"}
 

Macro Definition Documentation

#define cr_keylen   16

Definition at line 3001 of file switch_rtp.c.

Referenced by dtls_state_setup().

#define cr_kslen   30

Definition at line 3003 of file switch_rtp.c.

Referenced by dtls_state_setup().

#define cr_saltlen   14

Definition at line 3002 of file switch_rtp.c.

Referenced by dtls_state_setup().

#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

Definition at line 2999 of file switch_rtp.c.

Referenced by dtls_state_handshake(), dtls_state_setup(), and switch_rtp_add_dtls().

#define DTMF_SANITY   (rtp_session->one_second * 30)

Definition at line 77 of file switch_rtp.c.

Referenced by handle_rfc2833().

#define JITTER_LEAD_FRAMES   10

Definition at line 59 of file switch_rtp.c.

Referenced by check_jitter(), and rtp_common_read().

#define MASTER_KEY_LEN   30

Definition at line 69 of file switch_rtp.c.

#define MAX_NACK   10

Definition at line 1955 of file switch_rtp.c.

Referenced by check_rtcp_and_ice().

#define MAX_SRTP_ERRS   100

Definition at line 72 of file switch_rtp.c.

Referenced by read_rtp_packet().

#define NTP_TIME_OFFSET   2208988800UL
#define READ_DEC (   rtp_session)    switch_mutex_unlock(rtp_session->read_mutex); rtp_session->reading--
#define READ_INC (   rtp_session)    switch_mutex_lock(rtp_session->read_mutex); rtp_session->reading++
#define return_cng_frame ( )    do_cng = 1; goto timer_check

Definition at line 5131 of file switch_rtp.c.

Referenced by rtp_common_read().

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

Definition at line 68 of file switch_rtp.c.

#define rtp_header_len   12
#define RTP_MAGIC_NUMBER   42

Definition at line 70 of file switch_rtp.c.

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

Definition at line 67 of file switch_rtp.c.

#define RTP_STUN_FREQ   1000000

Definition at line 65 of file switch_rtp.c.

Referenced by ice_out().

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

Definition at line 71 of file switch_rtp.c.

Referenced by read_rtp_packet().

#define WRITE_DEC (   rtp_session)    switch_mutex_unlock(rtp_session->write_mutex); rtp_session->writing--
#define WRITE_INC (   rtp_session)    switch_mutex_lock(rtp_session->write_mutex); rtp_session->writing++
#define ZRTP_MAGIC_COOKIE   0x5a525450

Definition at line 74 of file switch_rtp.c.

Referenced by read_rtp_packet().

Typedef Documentation

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

Definition at line 283 of file switch_rtp.c.

typedef srtp_hdr_t rtp_hdr_t

Definition at line 86 of file switch_rtp.c.

typedef struct switch_dtls_s switch_dtls_t

Enumeration Type Documentation

Enumerator
RESULT_CONTINUE 
RESULT_GOTO_END 
RESULT_GOTO_RECVFROM 
RESULT_GOTO_TIMERCHECK 

Definition at line 503 of file switch_rtp.c.

Enumerator
VAD_FIRE_TALK 
VAD_FIRE_NOT_TALK 

Definition at line 185 of file switch_rtp.c.

185  {
186  VAD_FIRE_TALK = (1 << 0),
187  VAD_FIRE_NOT_TALK = (1 << 1)
vad_talk_mask_t
Definition: switch_rtp.c:185

Function Documentation

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

Definition at line 1570 of file switch_rtp.c.

References switch_rtcp_report_block::lost, and LOST_BURST_ANALYZE.

Referenced by check_jitter().

1571 {
1572  int lost = 0;
1573  int bursts = 0;
1574  int i;
1575 
1576  for ( i = 0; i < LOST_BURST_ANALYZE; i++ ) {
1577  lost += i * loss[i];
1578  bursts += loss[i];
1579  }
1580  if (received > 0 && bursts > 0) {
1581  *burstr = (double)((double)lost / (double)bursts) / (double)(1.0 / ( 1.0 - (double)lost / (double)received ));
1582  if (*burstr < 0) {
1583  *burstr = - *burstr;
1584  }
1585  } else {
1586  *burstr = 0;
1587  }
1588  if (received > 0) {
1589  *lossr = (double)((double)lost / (double)received);
1590  } else {
1591  *lossr = 0;
1592  }
1593 }
#define LOST_BURST_ANALYZE
Definition: switch_types.h:242
static void calc_bw_exp ( uint32_t  bps,
uint8_t  bits,
rtcp_tmmbx_t tmmbx 
)
static

Definition at line 1920 of file switch_rtp.c.

References rtcp_tmmbx_t::parts, and switch_assert.

Referenced by check_rtcp_and_ice().

1921 {
1922  uint32_t mantissa_max, i = 0;
1923  uint8_t exp = 0;
1924  uint32_t mantissa = 0;
1925  uint16_t overhead = 60;
1926 
1927  switch_assert(bits<=32);
1928 
1929  mantissa_max = (1 << bits) - 1;
1930 
1931  for (i = 0; i < 32; ++i) {
1932  if (bps <= (mantissa_max << i)) {
1933  exp = i;
1934  break;
1935  }
1936  }
1937 
1938  mantissa = (bps >> exp);
1939 
1940  tmmbx->parts[0] = (uint8_t) ((exp << 2) + ((mantissa >> 15) & 0x03));
1941  tmmbx->parts[1] = (uint8_t) (mantissa >> 7);
1942  tmmbx->parts[2] = (uint8_t) ((mantissa >> 1) + ((overhead >> 8) & 0x01));
1943  tmmbx->parts[3] = (uint8_t) (overhead);
1944 }
uint8_t parts[4]
Definition: switch_rtp.c:140
#define switch_assert(expr)
static void check_jitter ( switch_rtp_t rtp_session)
static

Definition at line 1604 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, 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::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_FALSE, SWITCH_LOG_DEBUG1, SWITCH_LOG_DEBUG3, switch_log_printf(), switch_micro_time_now(), SWITCH_RTP_FLAG_DTMF_ON, SWITCH_RTP_FLAG_PAUSE, SWITCH_TRUE, and switch_rtp_numbers_t::variance.

Referenced by read_rtp_packet().

1605 {
1606  switch_time_t current_time;
1607  int64_t diff_time = 0, cur_diff = 0;
1608  int seq;
1609 
1610  current_time = switch_micro_time_now() / 1000;
1611 
1612  if (rtp_session->flags[SWITCH_RTP_FLAG_PAUSE] || rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] || rtp_session->dtmf_data.in_digit_ts) {
1613  reset_jitter_seq(rtp_session);
1614  return;
1615  }
1616 
1617  if (++rtp_session->jitter_lead < JITTER_LEAD_FRAMES || !rtp_session->stats.inbound.last_proc_time) {
1618  rtp_session->stats.inbound.last_proc_time = current_time;
1619  return;
1620  }
1621 
1622  diff_time = (current_time - rtp_session->stats.inbound.last_proc_time);
1623  seq = (int)(uint16_t) ntohs((uint16_t) rtp_session->last_rtp_hdr.seq);
1624 
1625  /* Burst and Packet Loss */
1626  rtp_session->stats.inbound.recved++;
1627 
1628  if (rtp_session->stats.inbound.last_processed_seq > 0 && seq > (int)(rtp_session->stats.inbound.last_processed_seq + 1)) {
1629  int lost = (seq - rtp_session->stats.inbound.last_processed_seq - 1);
1630 
1631  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "%s Got: %s seq %d but expected: %d lost: %d\n",
1632  rtp_session_name(rtp_session),
1633  rtp_type(rtp_session),
1634  seq,
1635  (rtp_session->stats.inbound.last_processed_seq + 1), lost);
1636  rtp_session->stats.inbound.last_loss++;
1637 
1638  if (rtp_session->stats.inbound.last_loss > 0 && rtp_session->stats.inbound.last_loss < LOST_BURST_CAPTURE) {
1639  rtp_session->stats.inbound.loss[rtp_session->stats.inbound.last_loss] += lost;
1640  }
1641 
1642  rtp_session->bad_stream++;
1643  rtp_session->stats.inbound.flaws += lost;
1644  } else {
1645  rtp_session->stats.inbound.last_loss = 0;
1646  }
1647 
1648  rtp_session->stats.inbound.last_processed_seq = seq;
1649 
1650  /* Burst and Packet Loss */
1651 
1652  if (current_time > rtp_session->next_stat_check_time) {
1653  rtp_session->next_stat_check_time = current_time + 5000;
1654  burstr_calculate(rtp_session->stats.inbound.loss, rtp_session->stats.inbound.recved,
1655  &(rtp_session->stats.inbound.burstrate), &(rtp_session->stats.inbound.lossrate));
1656  do_mos(rtp_session, SWITCH_TRUE);
1657  } else {
1658  do_mos(rtp_session, SWITCH_FALSE);
1659  }
1660 
1661  if (rtp_session->stats.inbound.last_loss || rtp_session->bad_stream) {
1662  if (rtp_session->session && (!rtp_session->stats.inbound.error_log || rtp_session->stats.inbound.error_log->stop)) {
1663  struct error_period *error = switch_core_session_alloc(rtp_session->session, sizeof(*error));
1664  error->start = switch_micro_time_now();
1665  error->next = rtp_session->stats.inbound.error_log;
1666  rtp_session->stats.inbound.error_log = error;
1667  }
1668 
1669  if (!rtp_session->stats.inbound.last_loss) {
1670  if (++rtp_session->recovering_stream > (rtp_session->one_second * 3)) {
1671  if (rtp_session->session && rtp_session->stats.inbound.error_log) {
1672  rtp_session->stats.inbound.error_log->stop = switch_micro_time_now();
1673  }
1674 
1675  rtp_session->bad_stream = 0;
1676  }
1677  } else {
1678  rtp_session->recovering_stream = 0;
1679  rtp_session->bad_stream++;
1680  }
1681  } else {
1682  rtp_session->recovering_stream = 0;
1683  rtp_session->clean_stream++;
1684  }
1685 
1686 
1687  if ( diff_time < 0 ) {
1688  diff_time = -diff_time;
1689  }
1690 
1691  rtp_session->stats.inbound.jitter_n++;
1692  rtp_session->stats.inbound.jitter_add += diff_time;
1693 
1694  cur_diff = (int64_t)(diff_time - rtp_session->stats.inbound.mean_interval);
1695 
1696  rtp_session->stats.inbound.jitter_addsq += (cur_diff * cur_diff);
1697  rtp_session->stats.inbound.last_proc_time = current_time;
1698 
1699  if (rtp_session->stats.inbound.jitter_n > 0) {
1700  double ipdv;
1701 
1702  rtp_session->stats.inbound.mean_interval = (double)rtp_session->stats.inbound.jitter_add / (double)rtp_session->stats.inbound.jitter_n;
1703 
1704  if (!rtp_session->old_mean) {
1705  rtp_session->old_mean = rtp_session->stats.inbound.mean_interval;
1706  }
1707 
1708  rtp_session->stats.inbound.variance = (double)rtp_session->stats.inbound.jitter_addsq / (double)rtp_session->stats.inbound.jitter_n;
1709 
1710  //printf("CHECK %d +%ld +%ld %f %f\n", rtp_session->timer.samplecount, diff_time, (diff_time * diff_time), rtp_session->stats.inbound.mean_interval, rtp_session->stats.inbound.variance);
1711 
1712  ipdv = rtp_session->old_mean - rtp_session->stats.inbound.mean_interval;
1713 
1714  if ( ipdv > IPDV_THRESHOLD ) { /* It shows Increasing Delays */
1715  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG3, "Calculated Instantaneous Packet Delay Variation: %s packet %lf\n",
1716  rtp_type(rtp_session), ipdv);
1717  }
1718 
1719  if ( rtp_session->stats.inbound.variance < rtp_session->stats.inbound.min_variance || rtp_session->stats.inbound.min_variance == 0 ) {
1720  rtp_session->stats.inbound.min_variance = rtp_session->stats.inbound.variance;
1721  }
1722 
1723  if ( rtp_session->stats.inbound.variance > rtp_session->stats.inbound.max_variance ) {
1724  rtp_session->stats.inbound.max_variance = rtp_session->stats.inbound.variance;
1725  }
1726 
1727  rtp_session->old_mean = rtp_session->stats.inbound.mean_interval;
1728  }
1729 }
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
Definition: switch_time.c:310
switch_size_t flaws
Definition: switch_types.h:657
int loss[LOST_BURST_CAPTURE]
Definition: switch_types.h:653
#define SWITCH_CHANNEL_SESSION_LOG(x)
switch_rtp_numbers_t inbound
Definition: switch_types.h:690
#define SWITCH_CHANNEL_LOG
#define IPDV_THRESHOLD
Definition: switch_types.h:240
struct error_period * next
Definition: switch_types.h:622
#define rtp_session_name(_rtp_session)
Definition: switch_rtp.c:79
struct switch_rtp_rfc2833_data dtmf_data
Definition: switch_rtp.c:403
#define rtp_type(rtp_session)
Definition: switch_rtp.c:513
#define LOST_BURST_CAPTURE
Definition: switch_types.h:244
int64_t switch_time_t
Definition: switch_apr.h:188
int64_t stop
Definition: switch_types.h:621
#define JITTER_LEAD_FRAMES
Definition: switch_rtp.c:59
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:372
uint32_t clean_stream
Definition: switch_rtp.c:429
uint32_t recovering_stream
Definition: switch_rtp.c:431
int64_t start
Definition: switch_types.h:620
switch_rtp_stats_t stats
Definition: switch_rtp.c:428
struct error_period * error_log
Definition: switch_types.h:661
switch_time_t next_stat_check_time
Definition: switch_rtp.c:396
static void reset_jitter_seq(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:1595
switch_core_session_t * session
Definition: switch_rtp.c:446
void burstr_calculate(int loss[], int received, double *burstr, double *lossr)
Definition: switch_rtp.c:1570
#define switch_core_session_alloc(_session, _memory)
Allocate memory from a session's pool.
Definition: switch_core.h:694
uint32_t jitter_lead
Definition: switch_rtp.c:394
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:395
uint32_t bad_stream
Definition: switch_rtp.c:430
rtp_hdr_t last_rtp_hdr
Definition: switch_rtp.c:351
uint32_t one_second
Definition: switch_rtp.c:392
static void do_mos(switch_rtp_t *rtp_session, int force)
Definition: switch_rtp.c:1521
static int check_recv_payload ( switch_rtp_t rtp_session)
static

Definition at line 5085 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, switch_mutex_lock(), and switch_mutex_unlock().

Referenced by read_rtp_packet(), and rtp_common_read().

5086 {
5087  int ok = 1;
5088 
5089  if (rtp_session->pmaps && *rtp_session->pmaps) {
5090  payload_map_t *pmap;
5091  ok = 0;
5092 
5093  switch_mutex_lock(rtp_session->flag_mutex);
5094 
5095  for (pmap = *rtp_session->pmaps; pmap && pmap->allocated; pmap = pmap->next) {
5096  if (!pmap->negotiated) {
5097  continue;
5098  }
5099 
5100  if (rtp_session->last_rtp_hdr.pt == pmap->pt) {
5101  ok = 1;
5102  }
5103  }
5104  switch_mutex_unlock(rtp_session->flag_mutex);
5105  }
5106 
5107  return ok;
5108 }
uint8_t allocated
switch_mutex_t * flag_mutex
Definition: switch_rtp.c:407
struct payload_map_s * next
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
uint8_t negotiated
switch_payload_t pt
payload_map_t ** pmaps
Definition: switch_rtp.c:447
rtp_hdr_t last_rtp_hdr
Definition: switch_rtp.c:351
static int check_rtcp_and_ice ( switch_rtp_t rtp_session)
static

Definition at line 1956 of file switch_rtp.c.

References _RTCP_PSFB_FIR, _RTCP_PSFB_PLI, _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, switch_rtp::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, 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_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_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, rtp_session_name, rtp_type, switch_timer::samplecount, switch_rtp::samples_per_interval, switch_rtp::send_ctx, switch_rtp::send_msg, switch_rtcp_ext_hdr_t::send_ssrc, switch_rtcp_sender_report::sender_info, switch_rtcp_numbers_t::sent_pkt_count, rtcp_fir_t::seq, switch_rtp::session, SFF_NONE, switch_rtp::srtp_idx_rtcp, rtcp_fir_t::ssrc, rtcp_tmmbx_t::ssrc, switch_rtp::ssrc, switch_rtcp_sender_report::ssrc, switch_rtcp_receiver_report::ssrc, switch_rtp::stats, SWITCH_CHANNEL_LOG, SWITCH_CHANNEL_SESSION_LOG, switch_get_addr(), switch_jb_pop_nack(), SWITCH_LOG_CRIT, SWITCH_LOG_DEBUG, SWITCH_LOG_DEBUG1, SWITCH_LOG_ERROR, switch_log_printf(), switch_micro_time_now(), SWITCH_RTP_FLAG_AUTO_CNG, SWITCH_RTP_FLAG_ENABLE_RTCP, SWITCH_RTP_FLAG_NACK, SWITCH_RTP_FLAG_PROXY_MEDIA, SWITCH_RTP_FLAG_RTCP_MUX, SWITCH_RTP_FLAG_RTCP_PASSTHRU, SWITCH_RTP_FLAG_SECURE_SEND, SWITCH_RTP_FLAG_UDPTL, SWITCH_RTP_FLAG_USE_TIMER, SWITCH_RTP_FLAG_VIDEO, switch_rtp_test_flag(), switch_rtp_write_manual(), switch_sockaddr_get_port(), switch_socket_sendto(), SWITCH_STATUS_GENERR, SWITCH_STATUS_SUCCESS, switch_rtp::timer, switch_rtp::tmmbn, switch_rtp::tmmbr, 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, and switch_rtcp_hdr_s::version.

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

1957 {
1958  int ret = 0;
1959  int rtcp_ok = 0, rtcp_fb = 0;
1961  int rate = 0, nack_ttl = 0;
1962  uint32_t cur_nack[MAX_NACK] = { 0 };
1963 
1964  if (!rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] &&
1965  rtp_session->flags[SWITCH_RTP_FLAG_AUTO_CNG] &&
1966  rtp_session->send_msg.header.ts &&
1967  rtp_session->cng_pt != INVALID_PT &&
1968  (rtp_session->timer.samplecount - rtp_session->last_write_samplecount >= rtp_session->samples_per_interval * 60)) {
1969  uint8_t data[10] = { 0 };
1970  switch_frame_flag_t frame_flags = SFF_NONE;
1971  data[0] = 65;
1972  rtp_session->cn++;
1973 
1974  get_next_write_ts(rtp_session, 0);
1975  rtp_session->send_msg.header.ts = htonl(rtp_session->ts);
1976 
1977  switch_rtp_write_manual(rtp_session, (void *) data, 2, 0, rtp_session->cng_pt, ntohl(rtp_session->send_msg.header.ts), &frame_flags);
1978 
1979  if (switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER)) {
1980  rtp_session->last_write_samplecount = rtp_session->timer.samplecount;
1981  }
1982  }
1983 
1984  rate = rtp_session->rtcp_interval;
1985 
1986  if (rtp_session->flags[SWITCH_RTP_FLAG_NACK] && rtp_session->vb) {
1987  int n;
1988  for (n = 0; n < MAX_NACK; n++) {
1989  uint32_t nack = switch_jb_pop_nack(rtp_session->vb);
1990 
1991  if (!nack) break;
1992 
1993  cur_nack[nack_ttl++] = nack;
1994  }
1995  }
1996 
1997 
1998 
1999  if (rtp_session->rtcp_sent_packets < 4) {
2000  rate = 4000;
2001  } else {
2002  if (rtp_session->pli_count || rtp_session->fir_count || nack_ttl || rtp_session->tmmbr || rtp_session->tmmbn) {
2003  //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);
2004  rtcp_ok = 1;
2005  rtcp_fb = 1;
2006  }
2007  }
2008 
2009  //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);
2010 
2011  if (!rtcp_ok && (!rtp_session->rtcp_last_sent || (int)((now - rtp_session->rtcp_last_sent) / 1000) > rate)) {
2012  //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "TIME UP\n");
2013  rtcp_ok = 1;
2014  }
2015 
2016  if (rtcp_ok && using_ice(rtp_session)) {
2017  if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
2018  if (!rtp_session->ice.rready) {
2019  rtcp_ok = 0;
2020  }
2021  } else {
2022  if (!rtp_session->rtcp_ice.rready) {
2023  rtcp_ok = 0;
2024  }
2025  }
2026  }
2027 
2028  //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);
2029 
2030  if (rtp_session->rtcp_sock_output && rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] && !rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU] && rtcp_ok) {
2031  switch_rtcp_numbers_t * stats = &rtp_session->stats.rtcp;
2032  struct switch_rtcp_receiver_report *rr;
2033  struct switch_rtcp_sender_report *sr;
2034  struct switch_rtcp_report_block *rtcp_report_block;
2035  switch_size_t rtcp_bytes = sizeof(struct switch_rtcp_hdr_s)+sizeof(uint32_t); /* add size of the packet header and the ssrc */
2036  switch_rtcp_hdr_t *sdes;
2037  uint8_t *p;
2038  switch_size_t sdes_bytes = sizeof(struct switch_rtcp_hdr_s);
2039  uint32_t *ssrc;
2041 
2042  if (!rtcp_fb) {
2043  rtp_session->rtcp_last_sent = now;
2044  rtp_session->rtcp_sent_packets++;
2045  }
2046 
2047  rtp_session->rtcp_send_msg.header.version = 2;
2048  rtp_session->rtcp_send_msg.header.p = 0;
2049  rtp_session->rtcp_send_msg.header.count = 1;
2050 
2051 
2052  if (!rtp_session->stats.rtcp.sent_pkt_count) {
2053  rtp_session->rtcp_send_msg.header.type = _RTCP_PT_RR; /* Receiver report */
2054  rr=(struct switch_rtcp_receiver_report*) rtp_session->rtcp_send_msg.body;
2055  rr->ssrc = htonl(rtp_session->ssrc);
2056  rtcp_report_block = &rr->report_block;
2057  rtcp_bytes += sizeof(struct switch_rtcp_report_block);
2058  } else {
2059  struct switch_rtcp_sender_info *rtcp_sender_info;
2060  rtp_session->rtcp_send_msg.header.type = _RTCP_PT_SR; /* Sender report */
2061  sr = (struct switch_rtcp_sender_report*) rtp_session->rtcp_send_msg.body;
2062  sr->ssrc = htonl(rtp_session->ssrc);
2063  rtcp_sender_info = &sr->sender_info;
2064  rtcp_generate_sender_info(rtp_session, rtcp_sender_info);
2065  rtcp_report_block = &sr->report_block;
2066  rtcp_bytes += sizeof(struct switch_rtcp_sender_info) + sizeof(struct switch_rtcp_report_block);
2067  }
2068  rtcp_generate_report_block(rtp_session, rtcp_report_block);
2069 
2070  rtp_session->rtcp_send_msg.header.length = htons((uint16_t)(rtcp_bytes / 4) - 1);
2071 
2072  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
2073  //if (rtp_session->remote_ssrc == 0) {
2074  // rtp_session->remote_ssrc = rtp_session->stats.rtcp.peer_ssrc;
2075  //}
2076 
2077  //if (rtp_session->remote_ssrc == 0) {
2078  // rtp_session->remote_ssrc = ntohl(rtp_session->last_rtp_hdr.ssrc);
2079  //}
2080 
2081 
2082  if (rtp_session->pli_count) {
2083  switch_rtcp_ext_hdr_t *ext_hdr;
2084 
2085  p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes;
2086  ext_hdr = (switch_rtcp_ext_hdr_t *) p;
2087 
2088  ext_hdr->version = 2;
2089  ext_hdr->p = 0;
2090  ext_hdr->fmt = _RTCP_PSFB_PLI;
2091  ext_hdr->pt = _RTCP_PT_PSFB;
2092 
2093  ext_hdr->send_ssrc = htonl(rtp_session->ssrc);
2094  ext_hdr->recv_ssrc = htonl(rtp_session->remote_ssrc);
2095 
2096  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "Sending RTCP PLI\n");
2097 
2098  ext_hdr->length = htons((uint8_t)(sizeof(switch_rtcp_ext_hdr_t) / 4) - 1);
2099  rtcp_bytes += sizeof(switch_rtcp_ext_hdr_t);
2100  rtp_session->pli_count = 0;
2101  }
2102 
2103  if (rtp_session->flags[SWITCH_RTP_FLAG_NACK] && nack_ttl > 0) {
2104  int n = 0;
2105 
2106  for (n = 0; n < nack_ttl; n++) {
2107  switch_rtcp_ext_hdr_t *ext_hdr;
2108  uint32_t *nack;
2109  p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes;
2110  ext_hdr = (switch_rtcp_ext_hdr_t *) p;
2111 
2112  ext_hdr->version = 2;
2113  ext_hdr->p = 0;
2114  ext_hdr->fmt = _RTCP_RTPFB_NACK;
2115  ext_hdr->pt = _RTCP_PT_RTPFB;
2116  ext_hdr->send_ssrc = htonl(rtp_session->ssrc);
2117  ext_hdr->recv_ssrc = htonl(rtp_session->remote_ssrc);
2118  ext_hdr->length = htons(3);
2119  p += sizeof(switch_rtcp_ext_hdr_t);
2120  nack = (uint32_t *) p;
2121  *nack = cur_nack[n];
2122 
2123  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "Sending RTCP NACK %u\n",
2124  ntohs(*nack & 0xFFFF));
2125 
2126  rtcp_bytes += sizeof(switch_rtcp_ext_hdr_t) + sizeof(cur_nack[n]);
2127  cur_nack[n] = 0;
2128  }
2129 
2130  nack_ttl = 0;
2131  }
2132 
2133  if (rtp_session->fir_count) {
2134  switch_rtcp_ext_hdr_t *ext_hdr;
2135  rtcp_fir_t *fir;
2136 
2137  p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes;
2138  ext_hdr = (switch_rtcp_ext_hdr_t *) p;
2139 
2140  p += sizeof(switch_rtcp_ext_hdr_t);
2141  fir = (rtcp_fir_t *) p;
2142 
2143  ext_hdr->version = 2;
2144  ext_hdr->p = 0;
2145  ext_hdr->fmt = _RTCP_PSFB_FIR;
2146  ext_hdr->pt = _RTCP_PT_PSFB;
2147 
2148  ext_hdr->send_ssrc = htonl(rtp_session->ssrc);
2149  ext_hdr->recv_ssrc = 0;
2150 
2151  fir->ssrc = htonl(rtp_session->remote_ssrc);
2152  fir->seq = rtp_session->fir_seq;
2153  fir->r1 = fir->r2 = fir->r3 = 0;
2154 
2155  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "Sending RTCP FIR SEQ %d\n", rtp_session->fir_seq);
2156 
2157  rtp_session->fir_seq++;
2158 
2159  ext_hdr->length = htons((uint8_t)((sizeof(switch_rtcp_ext_hdr_t) + sizeof(rtcp_fir_t)) / 4) - 1);
2160  rtcp_bytes += sizeof(switch_rtcp_ext_hdr_t) + sizeof(rtcp_fir_t);
2161  rtp_session->fir_count = 0;
2162  }
2163 
2164  //if (!rtp_session->tmmbr && rtp_session->cur_tmmbr) {
2165  // rtp_session->tmmbr = rtp_session->cur_tmmbr;
2166  //}
2167 
2168  while (rtp_session->tmmbr || rtp_session->tmmbn) {
2169  switch_rtcp_ext_hdr_t *ext_hdr;
2170  rtcp_tmmbx_t *tmmbx;
2171  uint32_t bps = 0;
2172  p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes;
2173  ext_hdr = (switch_rtcp_ext_hdr_t *) p;
2174 
2175  p += sizeof(switch_rtcp_ext_hdr_t);
2176  tmmbx = (rtcp_tmmbx_t *) p;
2177 
2178  ext_hdr->version = 2;
2179  ext_hdr->p = 0;
2180  ext_hdr->pt = _RTCP_PT_RTPFB;
2181  ext_hdr->send_ssrc = htonl(rtp_session->ssrc);
2182  ext_hdr->recv_ssrc = 0;
2183 
2184  if (rtp_session->tmmbr) {
2185  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "Sending RTCP TMMBR %u\n", rtp_session->tmmbr);
2186  ext_hdr->fmt = _RTCP_RTPFB_TMMBR;
2187  bps = rtp_session->tmmbr;
2188  rtp_session->tmmbr = 0;
2189  } else {
2190  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "Sending RTCP TMMBN %u\n", rtp_session->tmmbr);
2191  ext_hdr->fmt = _RTCP_RTPFB_TMMBN;
2192  bps = rtp_session->tmmbn;
2193  rtp_session->tmmbn = 0;
2194  }
2195 
2196  tmmbx->ssrc = htonl(rtp_session->remote_ssrc);
2197  calc_bw_exp(bps, 17, tmmbx);
2198 
2199  ext_hdr->length = htons((uint8_t)((sizeof(switch_rtcp_ext_hdr_t) + sizeof(rtcp_tmmbx_t)) / 4) - 1);
2200  rtcp_bytes += sizeof(switch_rtcp_ext_hdr_t) + sizeof(rtcp_tmmbx_t);
2201  }
2202 
2203  }
2204 
2205  //SDES + CNAME
2206  p = (uint8_t *) (&rtp_session->rtcp_send_msg) + rtcp_bytes;
2207  sdes = (switch_rtcp_hdr_t *) p;
2208  sdes->version = 2;
2209  sdes->type = _RTCP_PT_SDES;
2210  sdes->count = 1;
2211  sdes->p = 0;
2212  p = (uint8_t *) (sdes) + sdes_bytes;
2213  ssrc = (uint32_t *) p;
2214  *ssrc = htonl(rtp_session->ssrc);
2215  sdes_bytes += sizeof(uint32_t);
2216 
2217 
2218  p = (uint8_t *) (sdes) + sdes_bytes;
2219  unit = (switch_rtcp_sdes_unit_t *) p;
2220  unit->type = _RTCP_SDES_CNAME;
2221  snprintf((char *)unit->value, 80, "%x", rtp_session->ssrc);
2222  unit->length = strlen((char *)unit->value);
2223  sdes_bytes += sizeof(switch_rtcp_sdes_unit_t) + unit->length;
2224 
2225 
2226  p += sizeof(switch_rtcp_sdes_unit_t) + unit->length;
2227  unit = (switch_rtcp_sdes_unit_t *) p;
2228  unit->type = _RTCP_SDES_NOTE;
2229  snprintf((char *)unit->value, 80, "FreeSWITCH.org -- Come to ClueCon.com");
2230  unit->length = strlen((char *)unit->value);
2231  sdes_bytes += sizeof(switch_rtcp_sdes_unit_t) + unit->length;
2232 
2233  sdes_bytes ++;//END
2234 
2235  sdes_bytes += 4 - (sdes_bytes % 4);
2236 
2237  sdes->length = htons((uint16_t)(sdes_bytes / 4) - 1);
2238  rtcp_bytes += sdes_bytes;
2239 
2240  /* Prepare next report */
2241 
2242  stats->last_rpt_cycle = stats->cycle;
2243  stats->last_rpt_ext_seq = stats->high_ext_seq_recv;
2244  stats->last_rpt_ts = rtp_session->timer.samplecount;
2245  stats->period_pkt_count = 0;
2246  stats->sent_pkt_count = 0;
2247 
2248 
2249 
2250 #ifdef ENABLE_SRTP
2251  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND]) {
2252  int sbytes = (int) rtcp_bytes;
2253  int stat = srtp_protect_rtcp(rtp_session->send_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_send_msg.header, &sbytes);
2254 
2255  if (stat) {
2256  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error: SRTP RTCP protection failed with code %d\n", stat);
2257  goto end;
2258  } else {
2259  rtcp_bytes = sbytes;
2260  }
2261 
2262  }
2263 #endif
2264 
2265 #ifdef ENABLE_ZRTP
2266  /* ZRTP Send */
2267  if (zrtp_on && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) {
2268  unsigned int sbytes = (int) rtcp_bytes;
2269  zrtp_status_t stat = zrtp_status_fail;
2270 
2271  stat = zrtp_process_rtcp(rtp_session->zrtp_stream, (void *) &rtp_session->rtcp_send_msg, &sbytes);
2272 
2273  switch (stat) {
2274  case zrtp_status_ok:
2275  break;
2276  case zrtp_status_drop:
2277  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error: zRTP protection drop with code %d\n", stat);
2278  ret = (int)rtcp_bytes;
2279  goto end;
2280  break;
2281  case zrtp_status_fail:
2282  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error: zRTP protection fail with code %d\n", stat);
2283  break;
2284  default:
2285  break;
2286  }
2287 
2288  rtcp_bytes = sbytes;
2289  }
2290 #endif
2291  //#define DEBUG_EXTRA
2292 #ifdef DEBUG_EXTRA
2293  {
2294  const char *old_host;
2295  char bufb[50];
2296  old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->rtcp_remote_addr);
2297  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_CRIT, "%s SEND %s RTCP %s:%d %ld\n",
2298  rtp_session_name(rtp_session),
2299  rtp_type(rtp_session),
2300  old_host,
2302  rtcp_bytes);
2303  }
2304 #endif
2305  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) {
2306  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG,"RTCP packet not written\n");
2307  } else {
2308  rtp_session->stats.inbound.period_packet_count = 0;
2309  }
2310  }
2311 
2312  if (rtp_session->ice.ice_user) {
2313  if (ice_out(rtp_session, &rtp_session->ice) == SWITCH_STATUS_GENERR) {
2314  ret = -1;
2315  goto end;
2316  }
2317  }
2318 
2319  if (!rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
2320  if (rtp_session->rtcp_ice.ice_user) {
2321  if (ice_out(rtp_session, &rtp_session->rtcp_ice) == SWITCH_STATUS_GENERR) {
2322  ret = -1;
2323  goto end;
2324  }
2325  }
2326  }
2327 
2328  end:
2329 
2330  return ret;
2331 }
switch_time_t rtcp_last_sent
Definition: switch_rtp.c:389
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
Definition: switch_time.c:310
static void rtcp_generate_report_block(switch_rtp_t *rtp_session, struct switch_rtcp_report_block *rtcp_report_block)
Definition: switch_rtp.c:1756
switch_size_t period_packet_count
Definition: switch_types.h:630
#define SWITCH_CHANNEL_SESSION_LOG(x)
uint16_t switch_sockaddr_get_port(switch_sockaddr_t *sa)
Definition: switch_apr.c:916
switch_rtp_numbers_t inbound
Definition: switch_types.h:690
#define SWITCH_CHANNEL_LOG
static void rtcp_generate_sender_info(switch_rtp_t *rtp_session, struct switch_rtcp_sender_info *sr)
Definition: switch_rtp.c:1731
struct switch_rtcp_report_block report_block
Definition: switch_rtp.c:500
uint8_t r1
Definition: switch_rtp.c:123
int srtp_idx_rtcp
Definition: switch_rtp.c:346
uint8_t r3
Definition: switch_rtp.c:125
uint8_t fir_seq
Definition: switch_rtp.c:318
#define rtp_session_name(_rtp_session)
Definition: switch_rtp.c:79
unsigned char type
Definition: switch_rtp.c:133
switch_rtcp_hdr_t header
Definition: switch_rtp.c:180
uint16_t pli_count
Definition: switch_rtp.c:320
#define rtp_type(rtp_session)
Definition: switch_rtp.c:513
switch_payload_t cng_pt
Definition: switch_rtp.c:406
static switch_status_t ice_out(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice)
Definition: switch_rtp.c:771
srtp_ctx_t * send_ctx[2]
Definition: switch_rtp.c:335
int64_t switch_time_t
Definition: switch_apr.h:188
switch_rtcp_numbers_t rtcp
Definition: switch_types.h:692
uint32_t tmmbn
Definition: switch_rtp.c:323
struct switch_rtcp_sender_info sender_info
Definition: switch_rtp.c:494
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:773
uint32_t ts
Definition: switch_rtp.c:362
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:372
uint8_t seq
Definition: switch_rtp.c:122
uint16_t fir_count
Definition: switch_rtp.c:319
rtcp_msg_t rtcp_send_msg
Definition: switch_rtp.c:315
uintptr_t switch_size_t
switch_rtp_ice_t rtcp_ice
Definition: switch_rtp.c:378
switch_jb_t * vb
Definition: switch_rtp.c:415
uint8_t r2
Definition: switch_rtp.c:124
uint32_t switch_frame_flag_t
switch_rtp_stats_t stats
Definition: switch_rtp.c:428
switch_socket_t * rtcp_sock_output
Definition: switch_rtp.c:309
uint8_t cn
Definition: switch_rtp.c:413
int rtcp_interval
Definition: switch_rtp.c:435
static int using_ice(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:1946
uint32_t samples_per_interval
Definition: switch_rtp.c:386
srtp_hdr_t header
Definition: switch_rtp.c:112
switch_timer_t timer
Definition: switch_rtp.c:411
switch_core_session_t * session
Definition: switch_rtp.c:446
uint32_t switch_jb_pop_nack(switch_jb_t *jb)
uint32_t remote_ssrc
Definition: switch_rtp.c:355
switch_rtp_ice_t ice
Definition: switch_rtp.c:377
struct switch_rtcp_report_block report_block
Definition: switch_rtp.c:495
unsigned char length
Definition: switch_rtp.c:134
uint32_t ssrc
Definition: switch_rtp.c:354
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:4742
char body[SWITCH_RTCP_MAX_BUF_LEN]
Definition: switch_rtp.c:181
uint32_t last_write_samplecount
Definition: switch_rtp.c:366
const char * switch_get_addr(char *buf, switch_size_t len, switch_sockaddr_t *in)
Definition: switch_apr.c:894
rtp_msg_t send_msg
Definition: switch_rtp.c:314
static const switch_payload_t INVALID_PT
Definition: switch_rtp.c:75
switch_sockaddr_t * rtcp_remote_addr
Definition: switch_rtp.c:326
uint32_t ssrc
Definition: switch_rtp.c:121
#define MAX_NACK
Definition: switch_rtp.c:1955
static void calc_bw_exp(uint32_t bps, uint8_t bits, rtcp_tmmbx_t *tmmbx)
Definition: switch_rtp.c:1920
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:1490
int rtcp_sent_packets
Definition: switch_rtp.c:436
uint32_t tmmbr
Definition: switch_rtp.c:322
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:7955
static void do_2833 ( switch_rtp_t rtp_session)
static

Definition at line 4784 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, and switch_rtp_rfc2833_data::timestamp_dtmf.

Referenced by rtp_common_read().

4785 {
4786  switch_frame_flag_t flags = 0;
4787  uint32_t samples = rtp_session->samples_per_interval;
4788 
4789  if (!rtp_session->last_write_ts) {
4790  return;
4791  }
4792 
4793  if (rtp_session->dtmf_data.out_digit_dur > 0) {
4794  int x, loops = 1;
4795 
4796  rtp_session->dtmf_data.out_digit_sofar += samples;
4797  rtp_session->dtmf_data.out_digit_sub_sofar += samples;
4798 
4799  if (rtp_session->dtmf_data.out_digit_sub_sofar > 0xFFFF) {
4800  rtp_session->dtmf_data.out_digit_sub_sofar = samples;
4801  rtp_session->dtmf_data.timestamp_dtmf += 0xFFFF;
4802  }
4803 
4804  if (rtp_session->dtmf_data.out_digit_sofar >= rtp_session->dtmf_data.out_digit_dur) {
4805  rtp_session->dtmf_data.out_digit_packet[1] |= 0x80;
4806  loops = 3;
4807  }
4808 
4809  rtp_session->dtmf_data.out_digit_packet[2] = (unsigned char) (rtp_session->dtmf_data.out_digit_sub_sofar >> 8);
4810  rtp_session->dtmf_data.out_digit_packet[3] = (unsigned char) rtp_session->dtmf_data.out_digit_sub_sofar;
4811 
4812  for (x = 0; x < loops; x++) {
4813  switch_size_t wrote = switch_rtp_write_manual(rtp_session,
4814  rtp_session->dtmf_data.out_digit_packet, 4, 0,
4815  rtp_session->te, rtp_session->dtmf_data.timestamp_dtmf, &flags);
4816 
4817  rtp_session->stats.outbound.raw_bytes += wrote;
4818  rtp_session->stats.outbound.dtmf_packet_count++;
4819 
4820  if (loops == 1) {
4821  rtp_session->last_write_ts += samples;
4822 
4824  rtp_session->dtmf_data.timestamp_dtmf = rtp_session->last_write_ts;
4825  }
4826  }
4827 
4828  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",
4829  loops == 1 ? "middle" : "end", rtp_session->dtmf_data.out_digit,
4830  rtp_session->dtmf_data.timestamp_dtmf,
4831  rtp_session->dtmf_data.out_digit_sofar,
4832  rtp_session->dtmf_data.out_digit_sub_sofar, rtp_session->dtmf_data.out_digit_dur, rtp_session->seq, rtp_session->last_write_ts);
4833  }
4834 
4835  if (loops != 1) {
4836  rtp_session->sending_dtmf = 0;
4837  rtp_session->need_mark = 1;
4838 
4839  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
4840  rtp_session->last_write_samplecount = rtp_session->timer.samplecount;
4841  }
4842 
4843  rtp_session->dtmf_data.out_digit_dur = 0;
4844 
4845  if (rtp_session->interdigit_delay) {
4846  set_dtmf_delay(rtp_session, rtp_session->interdigit_delay, rtp_session->interdigit_delay * 10);
4847  }
4848 
4849  return;
4850  }
4851  }
4852 
4853  if (!rtp_session->dtmf_data.out_digit_dur && rtp_session->dtmf_data.dtmf_queue && switch_queue_size(rtp_session->dtmf_data.dtmf_queue)) {
4854  void *pop;
4855 
4856  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
4857  if (rtp_session->timer.samplecount < rtp_session->next_write_samplecount) {
4858  return;
4859  }
4860 
4861  if (rtp_session->timer.samplecount >= rtp_session->max_next_write_samplecount) {
4862  rtp_session->queue_delay = 0;
4863  }
4864 
4865  } else if (rtp_session->queue_delay) {
4866  if (rtp_session->delay_samples >= rtp_session->samples_per_interval) {
4867  rtp_session->delay_samples -= rtp_session->samples_per_interval;
4868  } else {
4869  rtp_session->delay_samples = 0;
4870  }
4871 
4872  if (!rtp_session->delay_samples) {
4873  rtp_session->queue_delay = 0;
4874  }
4875  }
4876 
4877  if (rtp_session->queue_delay) {
4878  return;
4879  }
4880 
4881 
4882  if (!rtp_session->sending_dtmf) {
4883  rtp_session->sending_dtmf = 1;
4884  }
4885 
4886  if (switch_queue_trypop(rtp_session->dtmf_data.dtmf_queue, &pop) == SWITCH_STATUS_SUCCESS) {
4887  switch_dtmf_t *rdigit = pop;
4888  switch_size_t wrote;
4889 
4890  if (rdigit->digit == 'w') {
4891  set_dtmf_delay(rtp_session, 500, 0);
4892  free(rdigit);
4893  return;
4894  }
4895 
4896  if (rdigit->digit == 'W') {
4897  set_dtmf_delay(rtp_session, 1000, 0);
4898  free(rdigit);
4899  return;
4900  }
4901 
4902 
4903 
4904  memset(rtp_session->dtmf_data.out_digit_packet, 0, 4);
4905  rtp_session->dtmf_data.out_digit_sofar = samples;
4906  rtp_session->dtmf_data.out_digit_sub_sofar = samples;
4907  rtp_session->dtmf_data.out_digit_dur = rdigit->duration;
4908  rtp_session->dtmf_data.out_digit = rdigit->digit;
4909  rtp_session->dtmf_data.out_digit_packet[0] = (unsigned char) switch_char_to_rfc2833(rdigit->digit);
4910  rtp_session->dtmf_data.out_digit_packet[1] = 13;
4911  rtp_session->dtmf_data.out_digit_packet[2] = (unsigned char) (rtp_session->dtmf_data.out_digit_sub_sofar >> 8);
4912  rtp_session->dtmf_data.out_digit_packet[3] = (unsigned char) rtp_session->dtmf_data.out_digit_sub_sofar;
4913 
4914 
4915  rtp_session->dtmf_data.timestamp_dtmf = rtp_session->last_write_ts + samples;
4916  rtp_session->last_write_ts = rtp_session->dtmf_data.timestamp_dtmf;
4917  rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 0;
4918 
4919  wrote = switch_rtp_write_manual(rtp_session,
4920  rtp_session->dtmf_data.out_digit_packet,
4921  4,
4922  rtp_session->rtp_bugs & RTP_BUG_CISCO_SKIP_MARK_BIT_2833 ? 0 : 1,
4923  rtp_session->te, rtp_session->dtmf_data.timestamp_dtmf, &flags);
4924 
4925 
4926  rtp_session->stats.outbound.raw_bytes += wrote;
4927  rtp_session->stats.outbound.dtmf_packet_count++;
4928 
4929  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",
4930  rtp_session->dtmf_data.out_digit,
4931  rtp_session->dtmf_data.timestamp_dtmf,
4932  rtp_session->dtmf_data.out_digit_sofar,
4933  rtp_session->dtmf_data.out_digit_sub_sofar, rtp_session->dtmf_data.out_digit_dur, rtp_session->seq, rtp_session->last_write_ts);
4934 
4935  free(rdigit);
4936  }
4937  }
4938 }
uint32_t queue_delay
Definition: switch_rtp.c:370
unsigned int switch_queue_size(switch_queue_t *queue)
Definition: switch_apr.c:1114
#define SWITCH_CHANNEL_SESSION_LOG(x)
unsigned char out_digit_packet[4]
Definition: switch_rtp.c:219
uint32_t delay_samples
Definition: switch_rtp.c:367
switch_status_t switch_queue_trypop(switch_queue_t *queue, void **data)
Definition: switch_apr.c:1140
static void set_dtmf_delay(switch_rtp_t *rtp_session, uint32_t ms, uint32_t max_ms)
Definition: switch_rtp.c:4763
uint32_t duration
Definition: switch_types.h:288
switch_size_t dtmf_packet_count
Definition: switch_types.h:634
uint32_t max_next_write_samplecount
Definition: switch_rtp.c:369
struct switch_rtp_rfc2833_data dtmf_data
Definition: switch_rtp.c:403
switch_size_t raw_bytes
Definition: switch_types.h:627
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:372
switch_rtp_numbers_t outbound
Definition: switch_types.h:691
uintptr_t switch_size_t
uint16_t seq
Definition: switch_rtp.c:353
uint8_t need_mark
Definition: switch_rtp.c:358
uint32_t switch_frame_flag_t
switch_rtp_stats_t stats
Definition: switch_rtp.c:428
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:386
uint32_t interdigit_delay
Definition: switch_rtp.c:445
switch_timer_t timer
Definition: switch_rtp.c:411
uint32_t last_write_ts
Definition: switch_rtp.c:363
switch_core_session_t * session
Definition: switch_rtp.c:446
int8_t sending_dtmf
Definition: switch_rtp.c:357
unsigned int out_digit_sofar
Definition: switch_rtp.c:220
switch_rtp_bug_flag_t rtp_bugs
Definition: switch_rtp.c:427
switch_payload_t te
Definition: switch_rtp.c:404
switch_queue_t * dtmf_queue
Definition: switch_rtp.c:217
uint32_t next_write_samplecount
Definition: switch_rtp.c:368
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:222
uint32_t last_write_samplecount
Definition: switch_rtp.c:366
unsigned int out_digit_sub_sofar
Definition: switch_rtp.c:221
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:7955
static int do_dtls ( switch_rtp_t rtp_session,
switch_dtls_t dtls 
)
static

Definition at line 3144 of file switch_rtp.c.

References buf, switch_dtls_s::bytes, switch_dtls_s::data, switch_rtp::ice, switch_rtp_ice_t::ice_user, switch_dtls_s::read_bio, switch_rtp_ice_t::ready, switch_dtls_s::remote_addr, switch_rtp_ice_t::rready, rtp_type, switch_rtp::session, switch_dtls_s::sock_output, switch_dtls_s::ssl, switch_dtls_s::state, SWITCH_CHANNEL_SESSION_LOG, SWITCH_LOG_ERROR, switch_log_printf(), switch_socket_sendto(), SWITCH_STATUS_SUCCESS, and switch_dtls_s::write_bio.

Referenced by read_rtcp_packet(), and read_rtp_packet().

3145 {
3146  int r = 0, ret = 0, len;
3147  switch_size_t bytes;
3148  unsigned char buf[4096] = "";
3149  int ready = rtp_session->ice.ice_user ? (rtp_session->ice.rready && rtp_session->ice.ready) : 1;
3150 
3151 
3152  if (!dtls->bytes && !ready) {
3153  return 0;
3154  }
3155 
3156  if ((ret = BIO_write(dtls->read_bio, dtls->data, (int)dtls->bytes)) != (int)dtls->bytes && dtls->bytes > 0) {
3157  ret = SSL_get_error(dtls->ssl, ret);
3158  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "%s DTLS packet read err %d\n", rtp_type(rtp_session), ret);
3159  }
3160 
3161  if (dtls_states[dtls->state]) {
3162  r = dtls_states[dtls->state](rtp_session, dtls);
3163  }
3164 
3165  if ((len = BIO_read(dtls->write_bio, buf, sizeof(buf))) > 0) {
3166  bytes = len;
3167 
3168  if (switch_socket_sendto(dtls->sock_output, dtls->remote_addr, 0, (void *)buf, &bytes ) != SWITCH_STATUS_SUCCESS) {
3169  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "%s DTLS packet not written\n", rtp_type(rtp_session));
3170  }
3171  }
3172 
3173 
3174 
3175  return r;
3176 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
BIO * write_bio
Definition: switch_rtp.c:265
switch_sockaddr_t * remote_addr
Definition: switch_rtp.c:275
BIO * read_bio
Definition: switch_rtp.c:264
dtls_state_t state
Definition: switch_rtp.c:268
#define rtp_type(rtp_session)
Definition: switch_rtp.c:513
switch_socket_t * sock_output
Definition: switch_rtp.c:274
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:773
dtls_state_handler_t dtls_states[DS_INVALID]
Definition: switch_rtp.c:291
uintptr_t switch_size_t
switch_core_session_t * session
Definition: switch_rtp.c:446
switch_rtp_ice_t ice
Definition: switch_rtp.c:377
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_size_t bytes
Definition: switch_rtp.c:272
static switch_size_t do_flush ( switch_rtp_t rtp_session,
int  force,
switch_size_t  bytes_in 
)
static

Definition at line 4983 of file switch_rtp.c.

References FALSE, switch_rtp::flags, switch_rtp_numbers_t::flush_packet_count, switch_rtp::from_addr, handle_rfc2833(), switch_rtp_stats_t::inbound, switch_rtp::jb, jb_valid(), switch_rtp::last_rtp_hdr, 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_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().

4984 {
4985  int was_blocking = 0;
4986  switch_size_t bytes;
4987  uint32_t flushed = 0;
4988  switch_size_t bytes_out = 0;
4989 
4990  if (!switch_rtp_ready(rtp_session)) {
4991  return 0;
4992  }
4993 
4994  reset_jitter_seq(rtp_session);
4995 
4996  if (!force) {
4997  if (rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] ||
4998  rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] ||
4999  rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON]
5000  ) {
5001  return 0;
5002  }
5003  }
5004 
5005  READ_INC(rtp_session);
5006 
5007  if (switch_rtp_ready(rtp_session) ) {
5008 
5009  if (rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) {
5010  //switch_jb_reset(rtp_session->jb);
5011  bytes_out = bytes_in;
5012  goto end;
5013  }
5014 
5015  if (rtp_session->vbw) {
5016  switch_jb_reset(rtp_session->vbw);
5017  }
5018 
5019  if (rtp_session->vb) {
5020  //switch_jb_reset(rtp_session->vb);
5021  bytes_out = bytes_in;
5022  goto end;
5023  }
5024 
5025  if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_READ]) {
5027  SWITCH_LOG_CONSOLE, "%s FLUSH\n",
5028  rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "NoName"
5029  );
5030  }
5031 
5032  if (!rtp_session->flags[SWITCH_RTP_FLAG_NOBLOCK]) {
5033  was_blocking = 1;
5036  }
5037 
5038  do {
5039  if (switch_rtp_ready(rtp_session)) {
5040  bytes = sizeof(rtp_msg_t);
5041  switch_socket_recvfrom(rtp_session->from_addr, rtp_session->sock_input, 0, (void *) &rtp_session->recv_msg, &bytes);
5042 
5043  if (bytes) {
5044  int do_cng = 0;
5045 
5046  /* Make sure to handle RFC2833 packets, even if we're flushing the packets */
5047  if (bytes > rtp_header_len && rtp_session->last_rtp_hdr.pt == rtp_session->recv_te) {
5048  handle_rfc2833(rtp_session, bytes, &do_cng);
5049 #ifdef DEBUG_2833
5050  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "*** RTP packet handled in flush loop %d ***\n", do_cng);
5051 #endif
5052  }
5053 
5054  flushed++;
5055 
5056  rtp_session->stats.inbound.raw_bytes += bytes;
5057  rtp_session->stats.inbound.flush_packet_count++;
5058  rtp_session->stats.inbound.packet_count++;
5059  }
5060  } else {
5061  break;
5062  }
5063  } while (bytes > 0);
5064 
5065  if (was_blocking && switch_rtp_ready(rtp_session)) {
5068  }
5069 
5070 
5071  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->session) {
5072  //int type = 1; // sum flags: 1 encoder; 2; decoder
5073  //switch_core_media_codec_control(rtp_session->session, SWITCH_MEDIA_TYPE_VIDEO, SWITCH_IO_READ, SCC_VIDEO_RESET, SCCT_INT, (void *)&type, NULL, NULL);
5075  }
5076  }
5077 
5078  end:
5079 
5080  READ_DEC(rtp_session);
5081 
5082  return bytes_out;
5083 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
switch_rtp_numbers_t inbound
Definition: switch_types.h:690
uint8_t switch_rtp_ready(switch_rtp_t *rtp_session)
Test if an RTP session is ready.
Definition: switch_rtp.c:4452
#define SWITCH_CHANNEL_LOG
switch_socket_t * sock_input
Definition: switch_rtp.c:309
switch_jb_t * vbw
Definition: switch_rtp.c:416
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:936
#define READ_DEC(rtp_session)
Definition: switch_rtp.c:61
_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:627
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:372
#define READ_INC(rtp_session)
Definition: switch_rtp.c:60
switch_status_t switch_socket_opt_set(switch_socket_t *sock, int32_t opt, int32_t on)
Definition: switch_apr.c:823
uintptr_t switch_size_t
switch_size_t packet_count
Definition: switch_types.h:629
switch_jb_t * vb
Definition: switch_rtp.c:415
switch_rtp_stats_t stats
Definition: switch_rtp.c:428
void switch_rtp_clear_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Clear an RTP Flag.
Definition: switch_rtp.c:4747
void switch_rtp_set_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Set an RTP Flag.
Definition: switch_rtp.c:4702
switch_status_t switch_core_session_request_video_refresh(switch_core_session_t *session)
switch_jb_t * jb
Definition: switch_rtp.c:414
#define SWITCH_SO_NONBLOCK
Definition: switch_apr.h:994
rtp_msg_t recv_msg
Definition: switch_rtp.c:327
#define FALSE
static void reset_jitter_seq(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:1595
switch_core_session_t * session
Definition: switch_rtp.c:446
switch_size_t flush_packet_count
Definition: switch_types.h:636
void switch_jb_reset(switch_jb_t *jb)
uint8_t pause_jb
Definition: switch_rtp.c:441
static int jb_valid(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:4967
switch_payload_t recv_te
Definition: switch_rtp.c:405
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:539
#define TRUE
switch_sockaddr_t * from_addr
Definition: switch_rtp.c:374
rtp_hdr_t last_rtp_hdr
Definition: switch_rtp.c:351
char * switch_channel_get_name(switch_channel_t *channel)
Retrieve the name of a given channel.
#define rtp_header_len
Definition: switch_rtp.c:66
static void do_mos ( switch_rtp_t rtp_session,
int  force 
)
static

Definition at line 1521 of file switch_rtp.c.

References switch_rtp::bad_stream, switch_rtp::consecutive_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_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().

1521  {
1522 
1523  if ((switch_size_t)rtp_session->stats.inbound.recved < rtp_session->stats.inbound.flaws) {
1524  rtp_session->stats.inbound.flaws = 0;
1525  }
1526 
1527  if (rtp_session->stats.inbound.recved > 0 &&
1528  rtp_session->stats.inbound.flaws && (force || rtp_session->stats.inbound.last_flaw != rtp_session->stats.inbound.flaws)) {
1529  int R;
1530 
1531  if (rtp_session->consecutive_flaws++) {
1532  int diff, penalty;
1533 
1534  diff = (rtp_session->stats.inbound.flaws - rtp_session->stats.inbound.last_flaw);
1535 
1536  if (diff < 1) diff = 1;
1537 
1538  penalty = diff * 2;
1539 
1540  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "%s %s %d consecutive flaws, adding %d flaw penalty\n",
1541  rtp_session_name(rtp_session), rtp_type(rtp_session),
1542  rtp_session->consecutive_flaws, penalty);
1543  rtp_session->bad_stream++;
1544  rtp_session->stats.inbound.flaws += penalty;
1545  }
1546 
1547  R = (int)((double)((double)(rtp_session->stats.inbound.recved - rtp_session->stats.inbound.flaws) / (double)rtp_session->stats.inbound.recved) * 100.0);
1548 
1549  if (R < 0 || R > 100) R = 100;
1550 
1551  rtp_session->stats.inbound.R = R;
1552  rtp_session->stats.inbound.mos = 1 + (0.035) * R + (.000007) * R * (R-60) * (100-R);
1553  rtp_session->stats.inbound.last_flaw = rtp_session->stats.inbound.flaws;
1554  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",
1555  rtp_session_name(rtp_session),
1556  rtp_type(rtp_session),
1557  rtp_session->stats.inbound.R,
1558  (long int)(rtp_session->stats.inbound.recved - rtp_session->stats.inbound.flaws), rtp_session->stats.inbound.recved,
1559  (long int)rtp_session->stats.inbound.flaws,
1560  rtp_session->stats.inbound.mos,
1561  rtp_session->stats.inbound.variance,
1562  rtp_session->stats.inbound.min_variance,
1563  rtp_session->stats.inbound.max_variance
1564  );
1565  } else {
1566  rtp_session->consecutive_flaws = 0;
1567  }
1568 }
switch_size_t flaws
Definition: switch_types.h:657
#define SWITCH_CHANNEL_SESSION_LOG(x)
switch_rtp_numbers_t inbound
Definition: switch_types.h:690
uint32_t consecutive_flaws
Definition: switch_rtp.c:393
#define rtp_session_name(_rtp_session)
Definition: switch_rtp.c:79
switch_size_t last_flaw
Definition: switch_types.h:658
#define rtp_type(rtp_session)
Definition: switch_rtp.c:513
uintptr_t switch_size_t
switch_rtp_stats_t stats
Definition: switch_rtp.c:428
switch_core_session_t * session
Definition: switch_rtp.c:446
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:430
static int dtls_state_fail ( switch_rtp_t rtp_session,
switch_dtls_t dtls 
)
static

Definition at line 3089 of file switch_rtp.c.

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

3090 {
3091  if (rtp_session->session) {
3092  switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
3094  }
3095 
3096  return -1;
3097 }
#define switch_channel_hangup(channel, hangup_cause)
Hangup a channel flagging it'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:446
static int dtls_state_handshake ( switch_rtp_t rtp_session,
switch_dtls_t dtls 
)
static

Definition at line 3100 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_ERROR, and switch_log_printf().

3101 {
3102  int ret;
3103 
3104  if ((ret = SSL_do_handshake(dtls->ssl)) != 1){
3105  switch((ret = SSL_get_error(dtls->ssl, ret))){
3106  case SSL_ERROR_WANT_READ:
3107  case SSL_ERROR_WANT_WRITE:
3108  case SSL_ERROR_NONE:
3109  break;
3110  default:
3111  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "%s Handshake failure %d\n", rtp_type(rtp_session), ret);
3112  dtls_set_state(dtls, DS_FAIL);
3113  return -1;
3114  }
3115  }
3116 
3117  if (SSL_is_init_finished(dtls->ssl)) {
3118  dtls_set_state(dtls, DS_SETUP);
3119  }
3120 
3121  return 0;
3122 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
#define rtp_type(rtp_session)
Definition: switch_rtp.c:513
#define dtls_set_state(_dtls, _state)
Definition: switch_rtp.c:2999
switch_core_session_t * session
Definition: switch_rtp.c:446
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 const char* dtls_state_names ( dtls_state_t  s)
static

Definition at line 2989 of file switch_rtp.c.

References DS_INVALID.

2990 {
2991  if (s > DS_INVALID) {
2992  s = DS_INVALID;
2993  }
2994 
2995  return dtls_state_names_t[s];
2996 }
static const char * dtls_state_names_t[]
Definition: switch_rtp.c:2988
static int dtls_state_ready ( switch_rtp_t rtp_session,
switch_dtls_t dtls 
)
static

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

3073 {
3074 
3075  if (dtls->new_state) {
3076  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
3077  switch_core_session_t *other_session;
3078 
3079  if (rtp_session->session && switch_core_session_get_partner(rtp_session->session, &other_session) == SWITCH_STATUS_SUCCESS) {
3081  switch_core_session_rwunlock(other_session);
3082  }
3083  }
3084  dtls->new_state = 0;
3085  }
3086  return 0;
3087 }
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:372
#define switch_core_session_get_partner(_session, _partner)
Definition: switch_core.h:1002
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_core_session_request_video_refresh(switch_core_session_t *session)
switch_core_session_t * session
Definition: switch_rtp.c:446
uint8_t new_state
Definition: switch_rtp.c:270
static int dtls_state_setup ( switch_rtp_t rtp_session,
switch_dtls_t dtls 
)
static

Definition at line 3005 of file switch_rtp.c.

References AES_CM_128_HMAC_SHA1_80, cr_keylen, cr_kslen, cr_saltlen, DS_FAIL, DS_READY, switch_rtp::dtls, dtls_set_state, DTLS_TYPE_CLIENT, DTLS_TYPE_SERVER, switch_dtls_s::remote_fp, 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.

3006 {
3007  X509 *cert;
3008  int r = 0;
3009 
3010  if ((dtls->type & DTLS_TYPE_SERVER)) {
3011  r = 1;
3012  } else if ((cert = SSL_get_peer_certificate(dtls->ssl))) {
3015  X509_free(cert);
3016  }
3017 
3018  if (!r) {
3019  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "%s Fingerprint Verification Failed!\n", rtp_type(rtp_session));
3020  dtls_set_state(dtls, DS_FAIL);
3021  return -1;
3022  } else {
3023  uint8_t raw_key_data[cr_kslen*2] = { 0 };
3024  unsigned char *local_key, *remote_key, *local_salt, *remote_salt;
3025  unsigned char local_key_buf[cr_kslen] = {0}, remote_key_buf[cr_kslen] = {0};
3026 
3027  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "%s Fingerprint Verified.\n", rtp_type(rtp_session));
3028 
3029 #ifdef HAVE_OPENSSL_DTLS_SRTP
3030  if (!SSL_export_keying_material(dtls->ssl, raw_key_data, sizeof(raw_key_data), "EXTRACTOR-dtls_srtp", 19, NULL, 0, 0)) {
3031  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "%s Key material export failure\n", rtp_type(rtp_session));
3032  dtls_set_state(dtls, DS_FAIL);
3033  return -1;
3034  }
3035 #else
3036  return -1;
3037 #endif
3038 
3039  if ((dtls->type & DTLS_TYPE_CLIENT)) {
3040  local_key = raw_key_data;
3041  remote_key = local_key + cr_keylen;
3042  local_salt = remote_key + cr_keylen;
3043  remote_salt = local_salt + cr_saltlen;
3044 
3045  } else {
3046  remote_key = raw_key_data;
3047  local_key = remote_key + cr_keylen;
3048  remote_salt = local_key + cr_keylen;
3049  local_salt = remote_salt + cr_saltlen;
3050  }
3051 
3052  memcpy(local_key_buf, local_key, cr_keylen);
3053  memcpy(local_key_buf + cr_keylen, local_salt, cr_saltlen);
3054 
3055  memcpy(remote_key_buf, remote_key, cr_keylen);
3056  memcpy(remote_key_buf + cr_keylen, remote_salt, cr_saltlen);
3057 
3058  if (dtls == rtp_session->rtcp_dtls && rtp_session->rtcp_dtls != rtp_session->dtls) {
3061  } else {
3064  }
3065  }
3066 
3067  dtls_set_state(dtls, DS_READY);
3068 
3069  return 0;
3070 }
dtls_fingerprint_t * remote_fp
Definition: switch_rtp.c:267
switch_status_t switch_rtp_add_crypto_key(switch_rtp_t *rtp_session, switch_rtp_crypto_direction_t direction, uint32_t index, switch_rtp_crypto_key_type_t type, unsigned char *key, switch_size_t keylen)
Definition: switch_rtp.c:3468
#define SWITCH_CHANNEL_SESSION_LOG(x)
int switch_core_cert_verify(dtls_fingerprint_t *fp)
#define cr_kslen
Definition: switch_rtp.c:3003
#define rtp_type(rtp_session)
Definition: switch_rtp.c:513
switch_dtls_t * dtls
Definition: switch_rtp.c:348
#define dtls_set_state(_dtls, _state)
Definition: switch_rtp.c:2999
int switch_core_cert_extract_fingerprint(X509 *x509, dtls_fingerprint_t *fp)
switch_core_session_t * session
Definition: switch_rtp.c:446
switch_dtls_t * rtcp_dtls
Definition: switch_rtp.c:349
dtls_type_t type
Definition: switch_rtp.c:271
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:3002
#define cr_keylen
Definition: switch_rtp.c:3001
static switch_status_t enable_local_rtcp_socket ( switch_rtp_t rtp_session,
const char **  err 
)
static

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

2522  {
2523 
2524  const char *host = rtp_session->local_host_str;
2525  switch_port_t port = rtp_session->local_port;
2526  switch_socket_t *rtcp_new_sock = NULL, *rtcp_old_sock = NULL;
2528  char bufa[50];
2529 
2530  if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
2531  if (switch_sockaddr_info_get(&rtp_session->rtcp_local_addr, host, SWITCH_UNSPEC, port+1, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS) {
2532  *err = "RTCP Local Address Error!";
2533  goto done;
2534  }
2535 
2536  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) {
2537  *err = "RTCP Socket Error!";
2538  goto done;
2539  }
2540 
2542  *err = "RTCP Socket Error!";
2543  goto done;
2544  }
2545 
2546  if (switch_socket_bind(rtcp_new_sock, rtp_session->rtcp_local_addr) != SWITCH_STATUS_SUCCESS) {
2547  *err = "RTCP Bind Error!";
2548  goto done;
2549  }
2550 
2551  if (switch_sockaddr_info_get(&rtp_session->rtcp_from_addr, switch_get_addr(bufa, sizeof(bufa), rtp_session->from_addr),
2552  SWITCH_UNSPEC, switch_sockaddr_get_port(rtp_session->from_addr) + 1, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS) {
2553  *err = "RTCP From Address Error!";
2554  goto done;
2555  }
2556 
2557  rtcp_old_sock = rtp_session->rtcp_sock_input;
2558  rtp_session->rtcp_sock_input = rtcp_new_sock;
2559  rtcp_new_sock = NULL;
2560 
2561  switch_socket_create_pollset(&rtp_session->rtcp_read_pollfd, rtp_session->rtcp_sock_input, SWITCH_POLLIN | SWITCH_POLLERR, rtp_session->pool);
2562 
2563  done:
2564 
2565  if (*err) {
2566 
2567  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error allocating rtcp [%s]\n", *err);
2568  status = SWITCH_STATUS_FALSE;
2569  }
2570 
2571  if (rtcp_new_sock) {
2572  switch_socket_close(rtcp_new_sock);
2573  }
2574 
2575  if (rtcp_old_sock) {
2576  switch_socket_close(rtcp_old_sock);
2577  }
2578  } else {
2579  status = SWITCH_STATUS_FALSE;
2580  }
2581 
2582  return status;
2583 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
uint16_t switch_sockaddr_get_port(switch_sockaddr_t *sa)
Definition: switch_apr.c:916
switch_sockaddr_t * rtcp_from_addr
Definition: switch_rtp.c:374
switch_status_t switch_socket_bind(switch_socket_t *sock, switch_sockaddr_t *sa)
Definition: switch_apr.c:719
switch_sockaddr_t * rtcp_local_addr
Definition: switch_rtp.c:313
switch_socket_t * rtcp_sock_input
Definition: switch_rtp.c:309
int32_t switch_sockaddr_get_family(switch_sockaddr_t *sa)
Definition: switch_apr.c:921
#define SWITCH_POLLIN
Definition: switch_apr.h:1321
#define SWITCH_POLLERR
Definition: switch_apr.h:1324
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:372
char * local_host_str
Definition: switch_rtp.c:380
switch_status_t switch_socket_opt_set(switch_socket_t *sock, int32_t opt, int32_t on)
Definition: switch_apr.c:823
uint16_t switch_port_t
switch_memory_pool_t * pool
Definition: switch_rtp.c:373
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:704
switch_status_t
Common return values.
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:1032
switch_status_t switch_socket_close(switch_socket_t *sock)
Definition: switch_apr.c:714
#define SWITCH_UNSPEC
Definition: switch_apr.h:1022
switch_core_session_t * session
Definition: switch_rtp.c:446
#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:310
const char * switch_get_addr(char *buf, switch_size_t len, switch_sockaddr_t *in)
Definition: switch_apr.c:894
switch_port_t local_port
Definition: switch_rtp.c:397
switch_sockaddr_t * from_addr
Definition: switch_rtp.c:374
struct apr_socket_t switch_socket_t
Definition: switch_apr.h:1026
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:817
static switch_status_t enable_remote_rtcp_socket ( switch_rtp_t rtp_session,
const char **  err 
)
static

Definition at line 2478 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 switch_rtp_activate_rtcp(), and switch_rtp_set_remote_address().

2478  {
2479 
2481 
2482  if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
2483 
2485  rtp_session->remote_rtcp_port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS || !rtp_session->rtcp_remote_addr) {
2486  *err = "RTCP Remote Address Error!";
2487  return SWITCH_STATUS_FALSE;
2488  } else {
2489  const char *host;
2490  char bufa[50];
2491 
2492  host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtcp_remote_addr);
2493 
2495  "Setting RTCP remote addr to %s:%d %d\n", host, rtp_session->remote_rtcp_port, rtp_session->rtcp_remote_addr->family);
2496  }
2497 
2498  if (rtp_session->rtcp_sock_input && switch_sockaddr_get_family(rtp_session->rtcp_remote_addr) ==
2500  rtp_session->rtcp_sock_output = rtp_session->rtcp_sock_input;
2501  } else {
2502 
2503  if (rtp_session->rtcp_sock_output && rtp_session->rtcp_sock_output != rtp_session->rtcp_sock_input) {
2504  switch_socket_close(rtp_session->rtcp_sock_output);
2505  }
2506 
2507  if ((status = switch_socket_create(&rtp_session->rtcp_sock_output,
2509  SOCK_DGRAM, 0, rtp_session->pool)) != SWITCH_STATUS_SUCCESS) {
2510  *err = "RTCP Socket Error!";
2511  }
2512  }
2513 
2514  } else {
2515  *err = "RTCP NOT ACTIVE!";
2516  }
2517 
2518  return status;
2519 
2520 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
switch_sockaddr_t * rtcp_local_addr
Definition: switch_rtp.c:313
switch_port_t remote_rtcp_port
Definition: switch_rtp.c:400
switch_socket_t * rtcp_sock_input
Definition: switch_rtp.c:309
int32_t switch_sockaddr_get_family(switch_sockaddr_t *sa)
Definition: switch_apr.c:921
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:372
switch_memory_pool_t * pool
Definition: switch_rtp.c:373
switch_socket_t * rtcp_sock_output
Definition: switch_rtp.c:309
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:704
switch_status_t
Common return values.
switch_status_t switch_socket_close(switch_socket_t *sock)
Definition: switch_apr.c:714
#define SWITCH_UNSPEC
Definition: switch_apr.h:1022
switch_core_session_t * session
Definition: switch_rtp.c:446
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:894
switch_sockaddr_t * rtcp_remote_addr
Definition: switch_rtp.c:326
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:817
char * eff_remote_host_str
Definition: switch_rtp.c:382
static void free_dtls ( switch_dtls_t **  dtlsp)
static

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

3125 {
3126  switch_dtls_t *dtls;
3127 
3128  if (!dtlsp) {
3129  return;
3130  }
3131 
3132  dtls = *dtlsp;
3133  *dtlsp = NULL;
3134 
3135  if (dtls->ssl) {
3136  SSL_free(dtls->ssl);
3137  }
3138 
3139  if (dtls->ssl_ctx) {
3140  SSL_CTX_free(dtls->ssl_ctx);
3141  }
3142 }
SSL_CTX * ssl_ctx
Definition: switch_rtp.c:262
static uint8_t get_next_write_ts ( switch_rtp_t rtp_session,
uint32_t  timestamp 
)
static

Definition at line 1490 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_RTP_FLAG_USE_TIMER, switch_rtp_test_flag(), switch_rtp::timer, and switch_rtp::ts.

Referenced by check_rtcp_and_ice(), and rtp_common_write().

1491 {
1492  uint8_t m = 0;
1493 
1494  if (rtp_session->rtp_bugs & RTP_BUG_SEND_LINEAR_TIMESTAMPS) {
1495  rtp_session->ts += rtp_session->samples_per_interval;
1496  if (rtp_session->ts <= rtp_session->last_write_ts && rtp_session->ts > 0) {
1497  rtp_session->ts = rtp_session->last_write_ts + rtp_session->samples_per_interval;
1498  }
1499  } else if (timestamp) {
1500  rtp_session->ts = (uint32_t) timestamp;
1501  /* Send marker bit if timestamp is lower/same as before (resetted/new timer) */
1502  if (rtp_session->ts <= rtp_session->last_write_ts && !(rtp_session->rtp_bugs & RTP_BUG_NEVER_SEND_MARKER)) {
1503  m++;
1504  }
1505  } else if (switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_USE_TIMER)) {
1506  rtp_session->ts = rtp_session->timer.samplecount;
1507 
1508  if (rtp_session->ts <= rtp_session->last_write_ts && rtp_session->ts > 0) {
1509  rtp_session->ts = rtp_session->last_write_ts + rtp_session->samples_per_interval;
1510  }
1511  } else {
1512  rtp_session->ts += rtp_session->samples_per_interval;
1513  if (rtp_session->ts <= rtp_session->last_write_ts && rtp_session->ts > 0) {
1514  rtp_session->ts = rtp_session->last_write_ts + rtp_session->samples_per_interval;
1515  }
1516  }
1517 
1518  return m;
1519 }
uint32_t ts
Definition: switch_rtp.c:362
uint32_t samples_per_interval
Definition: switch_rtp.c:386
switch_timer_t timer
Definition: switch_rtp.c:411
uint32_t last_write_ts
Definition: switch_rtp.c:363
switch_rtp_bug_flag_t rtp_bugs
Definition: switch_rtp.c:427
uint32_t switch_rtp_test_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flags)
Test an RTP Flag.
Definition: switch_rtp.c:4742
static int get_recv_payload ( switch_rtp_t rtp_session)
static

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

5111 {
5112  int r = -1;
5113 
5114  if (rtp_session->pmaps && *rtp_session->pmaps) {
5115  payload_map_t *pmap;
5116 
5117  switch_mutex_lock(rtp_session->flag_mutex);
5118 
5119  for (pmap = *rtp_session->pmaps; pmap && pmap->allocated; pmap = pmap->next) {
5120  if (pmap->negotiated) {
5121  r = pmap->pt;
5122  break;
5123  }
5124  }
5125  switch_mutex_unlock(rtp_session->flag_mutex);
5126  }
5127 
5128  return r;
5129 }
uint8_t allocated
switch_mutex_t * flag_mutex
Definition: switch_rtp.c:407
struct payload_map_s * next
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
uint8_t negotiated
switch_payload_t pt
payload_map_t ** pmaps
Definition: switch_rtp.c:447
static void handle_ice ( switch_rtp_t rtp_session,
switch_rtp_ice_t ice,
void *  data,
switch_size_t  len 
)
static

Definition at line 865 of file switch_rtp.c.

References switch_rtp_ice_t::addr, switch_rtp::auto_adj_used, buf, ice_s::cand_idx, 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::first_stun, switch_rtp::flags, switch_core_session_message::from, switch_rtp::from_addr, switch_stun_packet_t::header, switch_rtp::ice, switch_rtp::ice_adj, ICE_CONTROLLED, ICE_GOOGLE_JINGLE, switch_rtp::ice_mutex, switch_rtp_ice_t::ice_params, switch_rtp_ice_t::ice_user, ICE_VANILLA, icecmp(), switch_stun_packet_header_t::id, IPR_RTP, switch_rtp_ice_t::last_ok, switch_rtp_ice_t::last_sent_id, switch_rtp::last_stun, switch_stun_packet_header_t::length, 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_stun_error_code_t::reason, switch_rtp::remote_addr, switch_rtp::remote_port, 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, 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_FALSE, switch_get_addr(), SWITCH_LOG_DEBUG, SWITCH_LOG_DEBUG4, 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_UNSPEC, switch_stun_packet_header_t::type, switch_stun_packet_attribute_t::type, switch_rtp_ice_t::type, switch_rtp_ice_t::user_ice, switch_stun_packet_attribute_t::value, WRITE_DEC, WRITE_INC, switch_rtp::wrong_addrs, and zstr.

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

866 {
867  switch_stun_packet_t *packet;
869  void *end_buf;
870  char username[34] = { 0 };
871  unsigned char buf[512] = { 0 };
872  switch_size_t cpylen = len;
873  int xlen = 0;
874  int ok = 1;
875  uint32_t *pri = NULL;
876  int is_rtcp = ice == &rtp_session->rtcp_ice;
877  uint32_t elapsed;
878  switch_time_t ref_point;
879 
880  //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
881  // switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "WTF OK %s CALL\n", rtp_type(rtp_session));
882  //}
883 
884  if (!switch_rtp_ready(rtp_session) || zstr(ice->user_ice) || zstr(ice->ice_user)) {
885  return;
886  }
887 
888  switch_mutex_lock(rtp_session->ice_mutex);
889 
890  READ_INC(rtp_session);
891  WRITE_INC(rtp_session);
892 
893  if (!switch_rtp_ready(rtp_session)) {
894  goto end;
895  }
896 
897  if (cpylen > sizeof(buf)) {
898  cpylen = sizeof(buf);
899  }
900 
901 
902  memcpy(buf, data, cpylen);
903  packet = switch_stun_packet_parse(buf, (uint32_t)cpylen);
904  if (!packet) {
905  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Invalid STUN/ICE packet received %ld bytes\n", (long)cpylen);
906  goto end;
907 
908  }
909 
910  rtp_session->last_stun = switch_micro_time_now();
911 
912  if (!rtp_session->first_stun) {
913  rtp_session->first_stun = rtp_session->last_stun;
914  }
915 
916  if (ice->last_ok) {
917  ref_point = ice->last_ok;
918  } else {
919  ref_point = rtp_session->first_stun;
920  }
921 
922  elapsed = (unsigned int) ((switch_micro_time_now() - ref_point) / 1000);
923 
924 
925  end_buf = buf + ((sizeof(buf) > packet->header.length) ? packet->header.length : sizeof(buf));
926 
928  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG8, "%s STUN PACKET TYPE: %s\n",
930  do {
931  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,
933 
934  switch (attr->type) {
936  {
937  ice->rready = 1;
938  }
939  break;
941  {
943  uint32_t code = (err->code * 100) + err->number;
944 
945  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "%s got %s stun binding response %u %s\n",
946  rtp_session_name(rtp_session),
947  rtp_type(rtp_session),
948  code,
949  err->reason
950  );
951 
952  if ((ice->type & ICE_VANILLA) && code == 487) {
953  if ((ice->type & ICE_CONTROLLED)) {
954  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "%s STUN Changing role to CONTROLLING\n", rtp_type(rtp_session));
955  ice->type &= ~ICE_CONTROLLED;
956  } else {
957  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "%s STUN Changing role to CONTROLLED\n", rtp_type(rtp_session));
958  ice->type |= ICE_CONTROLLED;
959  }
961  }
962 
963  }
964  break;
966  if (attr->type) {
967  char ip[50];
968  uint16_t port;
969  switch_stun_packet_attribute_get_mapped_address(attr, ip, sizeof(ip), &port);
970  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG8, "|------: %s:%d\n", ip, port);
971  }
972  break;
974  if (attr->type) {
975  char ip[50];
976  uint16_t port;
977  switch_stun_packet_attribute_get_xor_mapped_address(attr, &packet->header, ip, sizeof(ip), &port);
978  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG8, "|------: %s:%d\n", ip, port);
979  }
980  break;
982  if (attr->type) {
983  switch_stun_packet_attribute_get_username(attr, username, sizeof(username));
984  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG8, "|------: %s\n", username);
985  }
986  break;
987 
989  {
990  uint32_t priority = 0;
991  pri = (uint32_t *) attr->value;
992  priority = ntohl(*pri);
993  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG8, "|------: %u\n", priority);
994  ok = priority == ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].priority;
995  }
996  break;
997  }
998 
999  if (!switch_stun_packet_next_attribute(attr, end_buf)) {
1000  break;
1001  }
1002 
1003  xlen += 4 + switch_stun_attribute_padded_length(attr);
1004  } while (xlen <= packet->header.length);
1005 
1006  if ((ice->type & ICE_GOOGLE_JINGLE) && ok) {
1007  ok = !strcmp(ice->user_ice, username);
1008  }
1009 
1011  goto end;
1012  }
1013 
1014  if ((ice->type & ICE_VANILLA)) {
1015  char foo1[13] = "", foo2[13] = "";
1016  if (!ok) ok = !memcmp(packet->header.id, ice->last_sent_id, 12);
1017 
1018  if (packet->header.type == SWITCH_STUN_BINDING_RESPONSE) {
1019  ok = 1;
1020  if (!ice->rready) {
1021  if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
1022  rtp_session->ice.rready = 1;
1023  rtp_session->rtcp_ice.rready = 1;
1024  } else {
1025  ice->rready = 1;
1026  }
1027 
1028  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
1030  }
1031  }
1032  }
1033 
1034  memcpy(foo1, packet->header.id, 12);
1035  memcpy(foo2, ice->last_sent_id, 12);
1036 
1037  if (!ok && ice == &rtp_session->ice && rtp_session->rtcp_ice.ice_params && pri &&
1038  *pri == rtp_session->rtcp_ice.ice_params->cands[rtp_session->rtcp_ice.ice_params->chosen[1]][1].priority) {
1039  ice = &rtp_session->rtcp_ice;
1040  ok = 1;
1041  }
1042 
1043  if (!zstr(username)) {
1044  if (!icecmp(username, ice)) {
1045  ok = 1;
1046  } else if(!zstr(rtp_session->rtcp_ice.user_ice) && !icecmp(username, &rtp_session->rtcp_ice)) {
1047  ice = &rtp_session->rtcp_ice;
1048  ok = 1;
1049  }
1050  }
1051 
1052  if (ok) {
1053  ice->missed_count = 0;
1054  } else {
1055  switch_rtp_ice_t *icep[2] = { &rtp_session->ice, &rtp_session->rtcp_ice };
1056  switch_port_t port = 0;
1057  char *host = NULL;
1058 
1059  if (elapsed > 20000 && pri) {
1060  int i, j;
1061  uint32_t old;
1062  //const char *tx_host;
1063  const char *old_host, *err = NULL;
1064  //char bufa[50];
1065  char bufb[50];
1066  char adj_port[6];
1067  switch_channel_t *channel = NULL;
1068 
1069 
1070  ice->missed_count++;
1072 
1073 
1074  if (rtp_session->session) {
1075  channel = switch_core_session_get_channel(rtp_session->session);
1076  }
1077 
1078  //ice->ice_params->cands[ice->ice_params->chosen][ice->proto].priority;
1079  for (j = 0; j < 2; j++) {
1080  if (!icep[j] || !icep[j]->ice_params) {
1081  continue;
1082  }
1083  for (i = 0; i < icep[j]->ice_params->cand_idx[icep[j]->proto]; i++) {
1084  if (icep[j]->ice_params && icep[j]->ice_params->cands[i][icep[j]->proto].priority == *pri) {
1085  if (j == IPR_RTP) {
1086  icep[j]->ice_params->chosen[j] = i;
1087  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "Change candidate index to %d\n", i);
1088  }
1089 
1090  ice = icep[j];
1091  ok = 1;
1092 
1093  if (j != IPR_RTP) {
1094  break;
1095  }
1096 
1097  old = rtp_session->remote_port;
1098 
1099  //tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->from_addr);
1100  old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
1101 
1102  host = ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_addr;
1103  port = ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port;
1104 
1105  if (!host || !port) {
1106  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error setting remote host!\n");
1107  return;
1108  }
1109 
1111  "%s ICE Auto Changing port from %s:%u to %s:%u\n", rtp_type(rtp_session), old_host, old, host, port);
1112 
1113 
1114  if (channel) {
1115  switch_channel_set_variable(channel, "remote_media_ip_reported", switch_channel_get_variable(channel, "remote_media_ip"));
1116  switch_channel_set_variable(channel, "remote_media_ip", host);
1117  switch_channel_set_variable(channel, "rtp_auto_adjust_ip", host);
1118  switch_snprintf(adj_port, sizeof(adj_port), "%u", port);
1119  switch_channel_set_variable(channel, "remote_media_port_reported", switch_channel_get_variable(channel, "remote_media_port"));
1120  switch_channel_set_variable(channel, "remote_media_port", adj_port);
1121  switch_channel_set_variable(channel, "rtp_auto_adjust_port", adj_port);
1122  switch_channel_set_variable(channel, "rtp_auto_candidate_adjust", "true");
1123  }
1124  rtp_session->auto_adj_used = 1;
1125 
1126 
1127  switch_rtp_set_remote_address(rtp_session, host, port, 0, SWITCH_FALSE, &err);
1128  if (switch_sockaddr_info_get(&ice->addr, host, SWITCH_UNSPEC, port, 0, rtp_session->pool) != SWITCH_STATUS_SUCCESS ||
1129  !ice->addr) {
1130  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error setting remote host!\n");
1131  return;
1132  }
1133 
1134  if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) {
1136  } else {
1138  }
1139 
1140  }
1141  }
1142  }
1143  }
1144  }
1145  }
1146 
1147  if (ice->missed_count > 5 && !(ice->type & ICE_GOOGLE_JINGLE)) {
1148  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "missed too many: %d, looking for new ICE dest.\n",
1149  ice->missed_count);
1150  ice->rready = 0;
1151  ok = 1;
1152  }
1153 
1154 
1155  //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] || 1) {
1156  // switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "WTF OK %s %d\n", rtp_type(rtp_session), ok);
1157  //}
1158 
1159  if (ok) {
1160  const char *host = NULL, *host2 = NULL;
1161  switch_port_t port = 0, port2 = 0;
1162  char buf[80] = "";
1163  char buf2[80] = "";
1164 
1165  if (packet->header.type == SWITCH_STUN_BINDING_REQUEST) {
1166  uint8_t stunbuf[512];
1167  switch_stun_packet_t *rpacket;
1168  const char *remote_ip;
1169  switch_size_t bytes;
1170  char ipbuf[50];
1171  switch_sockaddr_t *from_addr = rtp_session->from_addr;
1172  switch_socket_t *sock_output = rtp_session->sock_output;
1173  uint8_t do_adj = 0;
1175  int cmp = 0;
1176 
1177  if (is_rtcp) {
1178  from_addr = rtp_session->rtcp_from_addr;
1179  sock_output = rtp_session->rtcp_sock_output;
1180  }
1181 
1182  if (!ice->ready) {
1183  ice->ready = 1;
1184  }
1185 
1186  memset(stunbuf, 0, sizeof(stunbuf));
1188 
1189  if ((ice->type & ICE_GOOGLE_JINGLE)) {
1190  switch_stun_packet_attribute_add_username(rpacket, username, (uint16_t)strlen(username));
1191  }
1192 
1193  remote_ip = switch_get_addr(ipbuf, sizeof(ipbuf), from_addr);
1194 
1195  switch_stun_packet_attribute_add_xor_binded_address(rpacket, (char *) remote_ip, switch_sockaddr_get_port(from_addr), from_addr->family);
1196 
1197  if ((ice->type & ICE_VANILLA)) {
1200  }
1201 
1202  bytes = switch_stun_packet_length(rpacket);
1203 
1204  host = switch_get_addr(buf, sizeof(buf), from_addr);
1205  port = switch_sockaddr_get_port(from_addr);
1206  host2 = switch_get_addr(buf2, sizeof(buf2), ice->addr);
1207  port2 = switch_sockaddr_get_port(ice->addr);
1208  cmp = switch_cmp_addr(from_addr, ice->addr);
1209 
1211  "STUN from %s:%d %s\n", host, port, cmp ? "EXPECTED" : "IGNORED");
1212 
1213  if (cmp) {
1214  ice->last_ok = now;
1215  rtp_session->wrong_addrs = 0;
1216  } else {
1217  if (((rtp_session->dtls && rtp_session->dtls->state != DS_READY) || !ice->ready || !ice->rready) &&
1218  rtp_session->wrong_addrs > 2 && rtp_session->ice_adj == 0) {
1219  do_adj++;
1220  rtp_session->ice_adj = 1;
1221  rtp_session->wrong_addrs = 0;
1222  } else if (rtp_session->wrong_addrs > 10 || elapsed >= 10000) {
1223  do_adj++;
1224  }
1225 
1226  if (!do_adj) {
1227  rtp_session->wrong_addrs++;
1228  }
1229  }
1230 
1231  if ((ice->type & ICE_VANILLA) && ice->ice_params && do_adj) {
1232  int i = 0;
1233 
1234  ice->missed_count = 0;
1235  ice->rready = 1;
1236 
1237  for (i = 0; i <= ice->ice_params->cand_idx[ice->proto]; i++) {
1238  if (ice->ice_params->cands[i][ice->proto].con_port == port) {
1239  if (!strcmp(ice->ice_params->cands[i][ice->proto].con_addr, host) &&
1240  !strcmp(ice->ice_params->cands[i][ice->proto].cand_type, "relay")) {
1241 
1242  if (elapsed < 1000) {
1244  "Skiping RELAY stun/%s/dtls port change from %s:%u to %s:%u\n", is_rtcp ? "rtcp" : "rtp",
1245  host2, port2,
1246  host, port);
1247 
1248  goto end;
1249  }
1250 
1251  break;
1252  }
1253  }
1254  }
1255 
1257  "Auto Changing %s stun/%s/dtls port from %s:%u to %s:%u\n", rtp_type(rtp_session), is_rtcp ? "rtcp" : "rtp",
1258  host2, port2,
1259  host, port);
1260 
1261  switch_rtp_change_ice_dest(rtp_session, ice, host, port);
1262  ice->last_ok = now;
1263  rtp_session->wrong_addrs = 0;
1264  }
1265  if (cmp) {
1266  switch_socket_sendto(sock_output, from_addr, 0, (void *) rpacket, &bytes);
1267  }
1268  }
1269  } else if (packet->header.type == SWITCH_STUN_BINDING_ERROR_RESPONSE) {
1270 
1271  if (rtp_session->session) {
1272  switch_core_session_message_t msg = { 0 };
1273  msg.from = __FILE__;
1274  msg.numeric_arg = packet->header.type;
1275  msg.pointer_arg = packet;
1277  switch_core_session_receive_message(rtp_session->session, &msg);
1279  "STUN/ICE binding error received on %s channel\n", rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "video" : "audio");
1280  }
1281 
1282  }
1283 
1284 
1285 
1286 
1287  end:
1288  switch_mutex_unlock(rtp_session->ice_mutex);
1289  READ_DEC(rtp_session);
1290  WRITE_DEC(rtp_session);
1291 }
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
Definition: switch_time.c:310
int cand_idx[2]
Definition: switch_rtp.h:106
#define SWITCH_CHANNEL_SESSION_LOG(x)
uint16_t switch_sockaddr_get_port(switch_sockaddr_t *sa)
Definition: switch_apr.c:916
uint8_t switch_rtp_ready(switch_rtp_t *rtp_session)
Test if an RTP session is ready.
Definition: switch_rtp.c:4452
switch_sockaddr_t * rtcp_from_addr
Definition: switch_rtp.c:374
switch_core_session_message_types_t message_id
Definition: switch_core.h:181
uint8_t switch_stun_packet_attribute_add_fingerprint(switch_stun_packet_t *packet)
Definition: switch_stun.c:552
switch_sockaddr_t * remote_addr
Definition: switch_rtp.c:326
int chosen[2]
Definition: switch_rtp.h:107
switch_sockaddr_t * addr
Definition: switch_rtp.c:244
#define WRITE_INC(rtp_session)
Definition: switch_rtp.c:62
switch_port_t remote_port
Definition: switch_rtp.c:398
ice_t * ice_params
Definition: switch_rtp.c:248
switch_time_t last_ok
Definition: switch_rtp.c:255
#define rtp_session_name(_rtp_session)
Definition: switch_rtp.c:79
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:61
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:431
A message object designed to allow unlike technologies to exchange data.
Definition: switch_core.h:177
#define zstr(x)
Definition: switch_utils.h:281
dtls_state_t state
Definition: switch_rtp.c:268
int switch_cmp_addr(switch_sockaddr_t *sa1, switch_sockaddr_t *sa2)
#define rtp_type(rtp_session)
Definition: switch_rtp.c:513
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
_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:616
int64_t switch_time_t
Definition: switch_apr.h:188
switch_core_media_ice_type_t type
Definition: switch_rtp.c:247
switch_byte_t switch_byte_t * buf
#define WRITE_DEC(rtp_session)
Definition: switch_rtp.c:63
int icecmp(const char *them, switch_rtp_ice_t *ice)
Definition: switch_rtp.c:856
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:773
struct apr_sockaddr_t switch_sockaddr_t
Definition: switch_apr.h:1029
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:372
#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:277
#define switch_channel_get_variable(_c, _v)
#define switch_stun_attribute_padded_length(attribute)
Obtain the padded length of an attribute's value.
Definition: switch_stun.h:268
char * con_addr
Definition: switch_rtp.h:93
#define READ_INC(rtp_session)
Definition: switch_rtp.c:60
switch_byte_t auto_adj_used
Definition: switch_rtp.c:440
#define switch_stun_packet_length(packet)
Obtain the correct length in bytes of a stun packet.
Definition: switch_stun.h:295
char last_sent_id[13]
Definition: switch_rtp.c:254
char * cand_type
Definition: switch_rtp.h:95
uintptr_t switch_size_t
uint16_t switch_port_t
switch_dtls_t * dtls
Definition: switch_rtp.c:348
switch_rtp_ice_t rtcp_ice
Definition: switch_rtp.c:378
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:2913
#define switch_core_session_receive_message(_session, _message)
Definition: switch_core.h:1217
switch_memory_pool_t * pool
Definition: switch_rtp.c:373
switch_socket_t * rtcp_sock_output
Definition: switch_rtp.c:309
void switch_rtp_clear_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Clear an RTP Flag.
Definition: switch_rtp.c:4747
switch_stun_packet_header_t header
Definition: switch_stun.h:133
#define switch_stun_packet_next_attribute(attribute, end)
Increment an attribute pointer to the next attribute in it's packet.
Definition: switch_stun.h:286
void switch_rtp_set_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Set an RTP Flag.
Definition: switch_rtp.c:4702
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:423
ice_proto_t proto
Definition: switch_rtp.c:249
#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:394
switch_mutex_t * ice_mutex
Definition: switch_rtp.c:410
void switch_core_session_video_reinit(switch_core_session_t *session)
switch_core_session_t * session
Definition: switch_rtp.c:446
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_rtp_ice_t ice
Definition: switch_rtp.c:377
switch_rtp_bug_flag_t rtp_bugs
Definition: switch_rtp.c:427
icand_t cands[MAX_CAND][2]
Definition: switch_rtp.h:105
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:485
uint32_t priority
Definition: switch_rtp.h:92
const char * switch_get_addr(char *buf, switch_size_t len, switch_sockaddr_t *in)
Definition: switch_apr.c:894
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:347
switch_time_t last_stun
Definition: switch_rtp.c:384
switch_sockaddr_t * from_addr
Definition: switch_rtp.c:374
struct apr_socket_t switch_socket_t
Definition: switch_apr.h:1026
switch_time_t first_stun
Definition: switch_rtp.c:383
#define switch_channel_set_variable(_channel, _var, _val)
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:817
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:516
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:377
uint32_t wrong_addrs
Definition: switch_rtp.c:385
memset(buf, 0, buflen)
switch_socket_t * sock_output
Definition: switch_rtp.c:309
uint8_t switch_stun_packet_attribute_add_integrity(switch_stun_packet_t *packet, const char *pass)
Definition: switch_stun.c:535
switch_port_t con_port
Definition: switch_rtp.h:94
static void handle_nack ( switch_rtp_t rtp_session,
uint32_t  nack 
)
static

Definition at line 5763 of file switch_rtp.c.

References switch_rtp::flags, rtp_msg_t::header, switch_rtp::local_addr, switch_rtp::remote_addr, switch_rtp::remote_port, switch_rtp::rtcp_from_addr, switch_rtp::session, switch_channel_get_name(), SWITCH_CHANNEL_SESSION_LOG, SWITCH_CHANNEL_SESSION_LOG_CLEAN, switch_core_media_gen_key_frame, switch_core_session_get_channel(), SWITCH_FALSE, switch_get_addr(), switch_jb_get_packet_by_seq(), SWITCH_LOG_CONSOLE, SWITCH_LOG_DEBUG1, switch_log_printf(), SWITCH_RTP_FLAG_DEBUG_RTP_WRITE, SWITCH_RTP_FLAG_NACK, switch_rtp_write_raw(), switch_sockaddr_get_port(), SWITCH_STATUS_SUCCESS, and switch_rtp::vbw.

Referenced by process_rtcp_report().

5764 {
5765  switch_size_t bytes = 0;
5766  rtp_msg_t send_msg[1] = {{{0}}};
5767  uint16_t seq = (uint16_t) (nack & 0xFFFF);
5768  uint16_t blp = (uint16_t) (nack >> 16);
5769  int i;
5770  const char *tx_host = NULL;
5771  const char *old_host = NULL;
5772  const char *my_host = NULL;
5773  char bufa[50], bufb[50], bufc[50];
5774 
5775  if (!(rtp_session->flags[SWITCH_RTP_FLAG_NACK] && rtp_session->vbw)) {
5776  return; /* not enabled */
5777  }
5778 
5779  if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]) {
5780  tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtcp_from_addr);
5781  old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
5782  my_host = switch_get_addr(bufc, sizeof(bufc), rtp_session->local_addr);
5783  }
5784 
5785  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "Got NACK [%u][0x%x] for seq %u\n", nack, nack, ntohs(seq));
5786 
5787  if (switch_jb_get_packet_by_seq(rtp_session->vbw, seq, (switch_rtp_packet_t *) send_msg, &bytes) == SWITCH_STATUS_SUCCESS) {
5788 
5789  if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]) {
5791  "X %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n",
5792  rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "NoName",
5793  (long) bytes,
5794  my_host, switch_sockaddr_get_port(rtp_session->local_addr),
5795  old_host, rtp_session->remote_port,
5796  tx_host, switch_sockaddr_get_port(rtp_session->rtcp_from_addr),
5797  send_msg->header.pt, ntohl(send_msg->header.ts), ntohs(send_msg->header.seq), send_msg->header.m);
5798 
5799  }
5800  //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "RE----SEND %u\n", ntohs(send_msg->header.seq));
5801  switch_rtp_write_raw(rtp_session, (void *) send_msg, &bytes, SWITCH_FALSE);
5802  } else {
5803  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "Cannot send NACK for seq %u\n", ntohs(seq));
5804  }
5805 
5806  blp = ntohs(blp);
5807  for (i = 0; i < 16; i++) {
5808  if (blp & (1 << i)) {
5809  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "Also Got NACK for seq %u\n", ntohs(seq) + i + 1);
5810  /* If they are missing more than one, may as well gen a key frame for good measure */
5812  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) {
5813  if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]) {
5815  "X %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n",
5816  rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "NoName",
5817  (long) bytes,
5818  my_host, switch_sockaddr_get_port(rtp_session->local_addr),
5819  old_host, rtp_session->remote_port,
5820  tx_host, switch_sockaddr_get_port(rtp_session->rtcp_from_addr),
5821  send_msg->header.pt, ntohl(send_msg->header.ts), ntohs(send_msg->header.seq), send_msg->header.m);
5822 
5823  }
5824  //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "RE----SEND %u\n", ntohs(send_msg->header.seq));
5825  switch_rtp_write_raw(rtp_session, (void *) &send_msg, &bytes, SWITCH_FALSE);
5826  } else {
5827  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "Cannot send NACK for seq %u\n", ntohs(seq) + i);
5828  }
5829  }
5830  }
5831 }
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_core_media_gen_key_frame(_session)
#define SWITCH_CHANNEL_SESSION_LOG(x)
uint16_t switch_sockaddr_get_port(switch_sockaddr_t *sa)
Definition: switch_apr.c:916
switch_sockaddr_t * rtcp_from_addr
Definition: switch_rtp.c:374
switch_sockaddr_t * remote_addr
Definition: switch_rtp.c:326
switch_port_t remote_port
Definition: switch_rtp.c:398
switch_jb_t * vbw
Definition: switch_rtp.c:416
_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:372
uintptr_t switch_size_t
switch_sockaddr_t * local_addr
Definition: switch_rtp.c:313
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:8006
#define SWITCH_CHANNEL_SESSION_LOG_CLEAN(x)
srtp_hdr_t header
Definition: switch_rtp.c:112
switch_core_session_t * session
Definition: switch_rtp.c:446
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:894
char * switch_channel_get_name(switch_channel_t *channel)
Retrieve the name of a given channel.
static handle_rfc2833_result_t handle_rfc2833 ( switch_rtp_t rtp_session,
switch_size_t  bytes,
int *  do_cng 
)
static

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

540 {
541 
542  if (rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON]) {
543  rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON]++;
544 
545  if (rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] > DTMF_SANITY) {
546  rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] = 0;
547  } else {
548  rtp_session->stats.inbound.last_processed_seq = 0;
549  }
550  }
551 
552 
553 #ifdef DEBUG_2833
554  if (rtp_session->dtmf_data.in_digit_sanity && !(rtp_session->dtmf_data.in_digit_sanity % 100)) {
555  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "sanity %d %ld\n", rtp_session->dtmf_data.in_digit_sanity, bytes);
556  }
557 #endif
558 
559  if (rtp_session->dtmf_data.in_digit_sanity && !--rtp_session->dtmf_data.in_digit_sanity) {
560 
561  rtp_session->dtmf_data.last_digit = 0;
562  rtp_session->dtmf_data.in_digit_ts = 0;
563  rtp_session->dtmf_data.in_digit_queued = 0;
564  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Failed DTMF sanity check.\n");
565  }
566 
567  /* RFC2833 ... like all RFC RE: VoIP, guaranteed to drive you to insanity!
568  We know the real rules here, but if we enforce them, it's an interop nightmare so,
569  we put up with as much as we can so we don't have to deal with being punished for
570  doing it right. Nice guys finish last!
571  */
572 
573  if (bytes > rtp_header_len && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] &&
574  rtp_session->last_rtp_hdr.pt == rtp_session->recv_te) {
575  switch_size_t len = bytes - rtp_header_len;
576  unsigned char *packet = (unsigned char *) RTP_BODY(rtp_session);
577  int end;
578  uint16_t duration;
579  char key;
580  uint16_t in_digit_seq;
581  uint32_t ts;
582 
583  rtp_session->stats.inbound.last_processed_seq = 0;
584 
585  if (!(packet[0] || packet[1] || packet[2] || packet[3]) && len >= 8) {
586  packet += 4;
587  len -= 4;
588  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "DTMF payload offset by 4 bytes.\n");
589  }
590 
591  if (!(packet[0] || packet[1] || packet[2] || packet[3]) && rtp_session->dtmf_data.in_digit_ts) {
592  switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
593  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Failed DTMF payload check.\n");
594  rtp_session->dtmf_data.last_digit = 0;
595  rtp_session->dtmf_data.in_digit_ts = 0;
596  rtp_session->dtmf_data.in_digit_sanity = 0;
597  rtp_session->dtmf_data.in_digit_queued = 0;
598  }
599 
600  end = packet[1] & 0x80 ? 1 : 0;
601  duration = (packet[2] << 8) + packet[3];
602  key = switch_rfc2833_to_char(packet[0]);
603  in_digit_seq = ntohs((uint16_t) rtp_session->last_rtp_hdr.seq);
604  ts = htonl(rtp_session->last_rtp_hdr.ts);
605 
606  if (rtp_session->flags[SWITCH_RTP_FLAG_PASS_RFC2833]) {
607 
608  if (end) {
609  rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] = DTMF_SANITY - 3;
610  } else if (!rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON]) {
611  rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] = 1;
612  }
613 
614  return RESULT_CONTINUE;
615  }
616 
617  if (in_digit_seq < rtp_session->dtmf_data.in_digit_seq) {
618  if (rtp_session->dtmf_data.in_digit_seq - in_digit_seq > 100) {
619  rtp_session->dtmf_data.in_digit_seq = 0;
620  }
621  }
622 #ifdef DEBUG_2833
623  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]);
624 #endif
625 
626  if (in_digit_seq > rtp_session->dtmf_data.in_digit_seq) {
627 
628  rtp_session->dtmf_data.in_digit_seq = in_digit_seq;
629 #ifdef DEBUG_2833
630 
631  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "read: %c %u %u %u %u %d %d %s\n",
632  key, in_digit_seq, rtp_session->dtmf_data.in_digit_seq,
633  ts, duration, rtp_session->last_rtp_hdr.m, end, end && !rtp_session->dtmf_data.in_digit_ts ? "ignored" : "");
634 #endif
635 
636 
637  if (rtp_session->dtmf_data.in_digit_ts && rtp_session->dtmf_data.in_digit_ts != ts) {
638  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "TS changed from last packet, resetting....\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 
646  if (!rtp_session->dtmf_data.in_digit_queued && rtp_session->dtmf_data.in_digit_ts) {
647  if ((rtp_session->rtp_bugs & RTP_BUG_IGNORE_DTMF_DURATION)) {
649 #ifdef DEBUG_2833
650  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Early Queuing digit %c:%d\n", dtmf.digit, dtmf.duration / 8);
651 #endif
652  switch_rtp_queue_rfc2833_in(rtp_session, &dtmf);
653  rtp_session->dtmf_data.in_digit_queued = 1;
654  }
655 
656  if (rtp_session->jb && (rtp_session->rtp_bugs & RTP_BUG_FLUSH_JB_ON_DTMF)) {
657  switch_jb_reset(rtp_session->jb);
658  }
659 
660  }
661 
662  /* only set sanity if we do NOT ignore the packet */
663  if (rtp_session->dtmf_data.in_digit_ts) {
664  rtp_session->dtmf_data.in_digit_sanity = 2000;
665  }
666 
667  if (rtp_session->dtmf_data.last_duration > duration &&
668  rtp_session->dtmf_data.last_duration > 0xFC17 && ts == rtp_session->dtmf_data.in_digit_ts) {
669  rtp_session->dtmf_data.flip++;
670  }
671 
672  if (end) {
673  if (!rtp_session->dtmf_data.in_digit_ts && rtp_session->dtmf_data.last_in_digit_ts != ts) {
674 #ifdef DEBUG_2833
675  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "start with end packet %d\n", ts);
676 #endif
677  rtp_session->dtmf_data.last_in_digit_ts = ts;
678  rtp_session->dtmf_data.in_digit_ts = ts;
679  rtp_session->dtmf_data.first_digit = key;
680  rtp_session->dtmf_data.in_digit_sanity = 2000;
681  }
682  if (rtp_session->dtmf_data.in_digit_ts) {
683  switch_dtmf_t dtmf = { key, duration, 0, SWITCH_DTMF_RTP };
684 
685  if (ts > rtp_session->dtmf_data.in_digit_ts) {
686  dtmf.duration += (ts - rtp_session->dtmf_data.in_digit_ts);
687  }
688  if (rtp_session->dtmf_data.flip) {
689  dtmf.duration += rtp_session->dtmf_data.flip * 0xFFFF;
690  rtp_session->dtmf_data.flip = 0;
691 #ifdef DEBUG_2833
693 #endif
694  }
695 #ifdef DEBUG_2833
696  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "done digit=%c ts=%u start_ts=%u dur=%u ddur=%u\n",
697  dtmf.digit, ts, rtp_session->dtmf_data.in_digit_ts, duration, dtmf.duration);
698 #endif
699 
700  if (!(rtp_session->rtp_bugs & RTP_BUG_IGNORE_DTMF_DURATION) && !rtp_session->dtmf_data.in_digit_queued) {
701 #ifdef DEBUG_2833
702  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Queuing digit %c:%d\n", dtmf.digit, dtmf.duration / 8);
703 #endif
704  switch_rtp_queue_rfc2833_in(rtp_session, &dtmf);
705  }
706 
707  rtp_session->dtmf_data.last_digit = rtp_session->dtmf_data.first_digit;
708 
709  rtp_session->dtmf_data.in_digit_ts = 0;
710  rtp_session->dtmf_data.in_digit_sanity = 0;
711  rtp_session->dtmf_data.in_digit_queued = 0;
712  *do_cng = 1;
713  } else {
714  if (!switch_rtp_ready(rtp_session)) {
715  return RESULT_GOTO_END;
716  }
718  return RESULT_GOTO_RECVFROM;
719  }
720 
721  } else if (!rtp_session->dtmf_data.in_digit_ts) {
722 #ifdef DEBUG_2833
723  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "start %d [%c]\n", ts, key);
724 #endif
725  rtp_session->dtmf_data.in_digit_ts = ts;
726  rtp_session->dtmf_data.last_in_digit_ts = ts;
727  rtp_session->dtmf_data.first_digit = key;
728  rtp_session->dtmf_data.in_digit_sanity = 2000;
729  }
730 
731  rtp_session->dtmf_data.last_duration = duration;
732  } else {
733 #ifdef DEBUG_2833
734  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "drop: %c %u %u %u %u %d %d\n",
735  key, in_digit_seq, rtp_session->dtmf_data.in_digit_seq, ts, duration, rtp_session->last_rtp_hdr.m, end);
736 #endif
738  return RESULT_GOTO_RECVFROM;
739  }
740  }
741 
742  if (bytes && rtp_session->dtmf_data.in_digit_ts) {
743  if (!switch_rtp_ready(rtp_session)) {
744  return RESULT_GOTO_END;
745  }
746 
747  if (!rtp_session->dtmf_data.in_interleaved && rtp_session->last_rtp_hdr.pt != rtp_session->recv_te) {
748  /* Drat, they are sending audio still as well as DTMF ok fine..... *sigh* */
749  rtp_session->dtmf_data.in_interleaved = 1;
750  }
751 
752  if (rtp_session->dtmf_data.in_interleaved || (rtp_session->rtp_bugs & RTP_BUG_IGNORE_DTMF_DURATION)) {
753  if (rtp_session->last_rtp_hdr.pt == rtp_session->recv_te) {
754  return RESULT_GOTO_RECVFROM;
755  }
756  } else {
757  *do_cng = 1;
758  return RESULT_GOTO_TIMERCHECK;
759  }
760  }
761 
762  return RESULT_CONTINUE;
763 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
switch_rtp_numbers_t inbound
Definition: switch_types.h:690
uint8_t switch_rtp_ready(switch_rtp_t *rtp_session)
Test if an RTP session is ready.
Definition: switch_rtp.c:4452
#define SWITCH_CHANNEL_LOG
#define DTMF_SANITY
Definition: switch_rtp.c:77
void * switch_core_memory_pool_get_data(switch_memory_pool_t *pool, const char *key)
uint32_t duration
Definition: switch_types.h:288
struct switch_rtp_rfc2833_data dtmf_data
Definition: switch_rtp.c:403
uint32_t switch_core_min_dtmf_duration(uint32_t duration)
Definition: switch_core.c:1704
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:372
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:638
switch_rtp_stats_t stats
Definition: switch_rtp.c:428
switch_memory_pool_t * pool
Definition: switch_rtp.c:373
#define RTP_BODY(_s)
Definition: switch_rtp.c:118
switch_jb_t * jb
Definition: switch_rtp.c:414
switch_core_session_t * session
Definition: switch_rtp.c:446
void switch_jb_reset(switch_jb_t *jb)
switch_rtp_bug_flag_t rtp_bugs
Definition: switch_rtp.c:427
switch_payload_t recv_te
Definition: switch_rtp.c:405
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:6961
rtp_hdr_t last_rtp_hdr
Definition: switch_rtp.c:351
#define rtp_header_len
Definition: switch_rtp.c:66
static switch_status_t ice_out ( switch_rtp_t rtp_session,
switch_rtp_ice_t ice 
)
static

Definition at line 771 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, 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_ERROR, switch_log_printf(), 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(), and switch_core_media_gen_local_sdp().

772 {
773  uint8_t buf[256] = { 0 };
774  switch_stun_packet_t *packet;
775  unsigned int elapsed;
776  switch_size_t bytes;
778  //switch_sockaddr_t *remote_addr = rtp_session->remote_addr;
779  switch_socket_t *sock_output = rtp_session->sock_output;
781 
782  if (ice->next_run && ice->next_run > now) {
783  return SWITCH_STATUS_BREAK;
784  }
785 
786  ice->next_run = now + RTP_STUN_FREQ;
787 
788  if (ice == &rtp_session->rtcp_ice && rtp_session->rtcp_sock_output) {
789  sock_output = rtp_session->rtcp_sock_output;
790  }
791 
792  if (!sock_output) {
793  return SWITCH_STATUS_FALSE;
794  }
795 
796  switch_assert(rtp_session != NULL);
797  switch_assert(ice->ice_user != NULL);
798 
799  READ_INC(rtp_session);
800 
801  if (rtp_session->last_stun) {
802  elapsed = (unsigned int) ((switch_micro_time_now() - rtp_session->last_stun) / 1000);
803 
804  if (elapsed > 30000) {
805  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "No %s stun for a long time!\n", rtp_type(rtp_session));
806  rtp_session->last_stun = switch_micro_time_now();
807  //status = SWITCH_STATUS_GENERR;
808  //goto end;
809  }
810  }
811 
813  switch_stun_packet_attribute_add_username(packet, ice->ice_user, (uint16_t)strlen(ice->ice_user));
814 
815  memcpy(ice->last_sent_id, packet->header.id, 12);
816 
817  //if (ice->pass && ice->type == ICE_GOOGLE_JINGLE) {
818  // switch_stun_packet_attribute_add_password(packet, ice->pass, (uint16_t)strlen(ice->pass));
819  //}
820 
821  if ((ice->type & ICE_VANILLA)) {
822  char sw[128] = "";
823 
825 
826  switch_snprintf(sw, sizeof(sw), "FreeSWITCH (%s)", switch_version_revision_human());
827  switch_stun_packet_attribute_add_software(packet, sw, (uint16_t)strlen(sw));
828 
829  if ((ice->type & ICE_CONTROLLED)) {
831  } else {
834  }
835 
838  }
839 
840 
841  bytes = switch_stun_packet_length(packet);
842 
843 #ifdef DEBUG_EXTRA
844  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));
845 #endif
846  switch_socket_sendto(sock_output, ice->addr, 0, (void *) packet, &bytes);
847 
848  ice->sending = 3;
849 
850  // end:
851  READ_DEC(rtp_session);
852 
853  return status;
854 }
uint8_t switch_stun_packet_attribute_add_software(switch_stun_packet_t *packet, char *software, uint16_t ulen)
Definition: switch_stun.c:640
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
Definition: switch_time.c:310
#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:521
#define RTP_STUN_FREQ
Definition: switch_rtp.c:65
uint8_t switch_stun_packet_attribute_add_fingerprint(switch_stun_packet_t *packet)
Definition: switch_stun.c:552
uint8_t switch_stun_packet_attribute_add_controlling(switch_stun_packet_t *packet)
Definition: switch_stun.c:585
int chosen[2]
Definition: switch_rtp.h:107
switch_sockaddr_t * addr
Definition: switch_rtp.c:244
ice_t * ice_params
Definition: switch_rtp.c:248
#define rtp_session_name(_rtp_session)
Definition: switch_rtp.c:79
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:246
#define READ_DEC(rtp_session)
Definition: switch_rtp.c:61
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:431
#define rtp_type(rtp_session)
Definition: switch_rtp.c:513
uint8_t switch_stun_packet_attribute_add_use_candidate(switch_stun_packet_t *packet)
Definition: switch_stun.c:574
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:616
int64_t switch_time_t
Definition: switch_apr.h:188
switch_core_media_ice_type_t type
Definition: switch_rtp.c:247
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:773
#define READ_INC(rtp_session)
Definition: switch_rtp.c:60
#define switch_stun_packet_length(packet)
Obtain the correct length in bytes of a stun packet.
Definition: switch_stun.h:295
char last_sent_id[13]
Definition: switch_rtp.c:254
uintptr_t switch_size_t
switch_rtp_ice_t rtcp_ice
Definition: switch_rtp.c:378
switch_socket_t * rtcp_sock_output
Definition: switch_rtp.c:309
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:249
switch_core_session_t * session
Definition: switch_rtp.c:446
icand_t cands[MAX_CAND][2]
Definition: switch_rtp.h:105
uint8_t switch_stun_packet_attribute_add_controlled(switch_stun_packet_t *packet)
Definition: switch_stun.c:600
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:92
switch_time_t last_stun
Definition: switch_rtp.c:384
const char * switch_version_revision_human(void)
struct apr_socket_t switch_socket_t
Definition: switch_apr.h:1026
#define switch_assert(expr)
switch_socket_t * sock_output
Definition: switch_rtp.c:309
uint8_t switch_stun_packet_attribute_add_integrity(switch_stun_packet_t *packet, const char *pass)
Definition: switch_stun.c:535
int icecmp ( const char *  them,
switch_rtp_ice_t ice 
)

Definition at line 856 of file switch_rtp.c.

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

Referenced by handle_ice().

857 {
858  if (strchr(them, ':')) {
859  return strcmp(them, ice->user_ice);
860  }
861 
862  return strcmp(them, ice->luser_ice);
863 }
static int jb_valid ( switch_rtp_t rtp_session)
static

Definition at line 4967 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_rtp_packet(), and rtp_common_read().

4968 {
4969  if (rtp_session->ice.ice_user) {
4970  if (!rtp_session->ice.ready && rtp_session->ice.rready) {
4971  return 0;
4972  }
4973  }
4974 
4975  if (rtp_session->dtls && rtp_session->dtls->state != DS_READY) {
4976  return 0;
4977  }
4978 
4979  return 1;
4980 }
dtls_state_t state
Definition: switch_rtp.c:268
switch_dtls_t * dtls
Definition: switch_rtp.c:348
switch_rtp_ice_t ice
Definition: switch_rtp.c:377
static void ping_socket ( switch_rtp_t rtp_session)
static

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

2833 {
2834  uint32_t o = UINT_MAX;
2835  switch_size_t len = sizeof(o);
2836  switch_socket_sendto(rtp_session->sock_input, rtp_session->local_addr, 0, (void *) &o, &len);
2837 
2838  if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] && rtp_session->rtcp_sock_input) {
2839  switch_socket_sendto(rtp_session->rtcp_sock_input, rtp_session->rtcp_local_addr, 0, (void *) &o, &len);
2840  }
2841 }
switch_sockaddr_t * rtcp_local_addr
Definition: switch_rtp.c:313
switch_socket_t * sock_input
Definition: switch_rtp.c:309
switch_socket_t * rtcp_sock_input
Definition: switch_rtp.c:309
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:773
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:372
uintptr_t switch_size_t
switch_sockaddr_t * local_addr
Definition: switch_rtp.c:313
static switch_status_t process_rtcp_packet ( switch_rtp_t rtp_session,
switch_size_t bytes 
)
static

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

5985 {
5986  switch_size_t len;
5987  switch_size_t remain = *bytes;
5989  rtcp_msg_t *msg = rtp_session->rtcp_recv_msg_p;
5990 
5991  if (msg->header.version != 2) {
5992  if (msg->header.version == 0) {
5993  if (rtp_session->ice.ice_user) {
5994  handle_ice(rtp_session, &rtp_session->rtcp_ice, (void *) msg, *bytes);
5995  }
5996  return SWITCH_STATUS_SUCCESS;
5997  } else {
5999  SWITCH_LOG_WARNING, "Received an unsupported RTCP packet version %d\n", msg->header.version);
6000  return SWITCH_STATUS_FALSE;
6001  }
6002  }
6003 
6004  do {
6005  len = ((switch_size_t)ntohs(msg->header.length) * 4) + 4;
6006 
6007  if (msg->header.version != 2 || !(msg->header.type > 199 && msg->header.type < 208)) {
6009  "INVALID RTCP PACKET TYPE %d VER %d LEN %" SWITCH_SIZE_T_FMT "\n", msg->header.type,
6010  msg->header.version, len);
6011  status = SWITCH_STATUS_BREAK;
6012  break;
6013  }
6014 
6015  //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_CRIT,
6016  //"WTF BYTES %ld REMAIN %ld PACKET TYPE %d LEN %ld\n", *bytes, remain, msg->header.type, len);
6017 
6018  if (len > remain) {
6020  "RTCP INVALID LENGTH %" SWITCH_SIZE_T_FMT "\n", len);
6021  len = remain;
6022  }
6023 
6024  status = process_rtcp_report(rtp_session, msg, len);
6025 
6026  if (remain > len) {
6027  unsigned char *p = (unsigned char *) msg;
6028  p += len;
6029  msg = (rtcp_msg_t *) p;
6030  }
6031 
6032  remain -= len;
6033 
6034  } while (remain >= 4);
6035 
6036  return status;
6037 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
switch_rtcp_hdr_t header
Definition: switch_rtp.c:180
uintptr_t switch_size_t
switch_rtp_ice_t rtcp_ice
Definition: switch_rtp.c:378
static void handle_ice(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice, void *data, switch_size_t len)
Definition: switch_rtp.c:865
switch_status_t
Common return values.
switch_core_session_t * session
Definition: switch_rtp.c:446
switch_rtp_ice_t ice
Definition: switch_rtp.c:377
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 SWITCH_SIZE_T_FMT
rtcp_msg_t * rtcp_recv_msg_p
Definition: switch_rtp.c:329
static switch_status_t process_rtcp_report(switch_rtp_t *rtp_session, rtcp_msg_t *msg, switch_size_t bytes)
Definition: switch_rtp.c:5833
static switch_status_t process_rtcp_report ( switch_rtp_t rtp_session,
rtcp_msg_t msg,
switch_size_t  bytes 
)
static

Definition at line 5833 of file switch_rtp.c.

References _RTCP_PSFB_FIR, _RTCP_PSFB_PLI, _RTCP_PT_PSFB, _RTCP_PT_RR, _RTCP_PT_RTPFB, _RTCP_PT_SR, _RTCP_RTPFB_NACK, rtcp_ext_msg_t::body, rtcp_msg_t::body, switch_rtcp_hdr_s::count, switch_rtcp_report_block_frame::dlsr, switch_rtcp_report_block::dlsr, 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, if(), 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, memset(), switch_rtcp_frame::ntp_lsw, switch_rtcp_sender_info::ntp_lsw, switch_rtcp_frame::ntp_msw, switch_rtcp_sender_info::ntp_msw, NTP_TIME_OFFSET, 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_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, switch_rtcp_report_block_frame::rtt_avg, SCC_AUDIO_PACKET_LOSS, SCCT_INT, SCCT_NONE, 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_core_media_codec_control(), switch_core_media_gen_key_frame, SWITCH_IO_WRITE, switch_jb_reset(), SWITCH_LOG_DEBUG1, SWITCH_LOG_DEBUG10, SWITCH_LOG_DEBUG3, switch_log_printf(), SWITCH_MEDIA_TYPE_AUDIO, SWITCH_RTP_FLAG_RTCP_PASSTHRU, SWITCH_RTP_FLAG_VIDEO, SWITCH_SIZE_T_FMT, SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, switch_time_exp_gmt(), switch_time_now(), 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 switch_rtp::vbw.

Referenced by process_rtcp_packet().

5834 {
5836  int i;
5837 
5839  "RTCP packet bytes %" SWITCH_SIZE_T_FMT " type %d pad %d\n",
5840  bytes, msg->header.type, msg->header.p);
5841 
5842  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && (msg->header.type == _RTCP_PT_RTPFB || msg->header.type == _RTCP_PT_PSFB)) {
5843  rtcp_ext_msg_t *extp = (rtcp_ext_msg_t *) msg;
5844 
5845  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "PICKED UP XRTCP type: %d fmt: %d\n",
5846  msg->header.type, extp->header.fmt);
5847 
5848  if (msg->header.type == _RTCP_PT_PSFB && (extp->header.fmt == _RTCP_PSFB_FIR || extp->header.fmt == _RTCP_PSFB_PLI)) {
5850  if (rtp_session->vbw) {
5851  switch_jb_reset(rtp_session->vbw);
5852  }
5853  }
5854 
5855  if (msg->header.type == _RTCP_PT_RTPFB && extp->header.fmt == _RTCP_RTPFB_NACK) {
5856  uint32_t *nack = (uint32_t *) extp->body;
5857  int i;
5858 
5859  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "Got NACK count %d\n", ntohs(extp->header.length) - 2);
5860 
5861 
5862  for (i = 0; i < ntohs(extp->header.length) - 2; i++) {
5863  handle_nack(rtp_session, *nack);
5864  nack++;
5865  }
5866 
5868  }
5869 
5870  } else
5871 
5872  if (msg->header.type == _RTCP_PT_SR || msg->header.type == _RTCP_PT_RR) {
5873  switch_time_t now;
5874  switch_time_exp_t now_hr;
5875  uint32_t sec, ntp_sec, ntp_usec, lsr_now;
5876  uint32_t lsr;
5877  uint32_t packet_ssrc;
5878 
5879  now = switch_time_now(); /* number of microseconds since 00:00:00 january 1, 1970 UTC */
5880  sec = (uint32_t)(now/1000000); /* converted to second (NTP most significant bits) */
5881  ntp_sec = sec+NTP_TIME_OFFSET; /* 32bits most significant */
5882  ntp_usec = (uint32_t)(now - (sec*1000000)); /* micro seconds */
5883  lsr_now = (uint32_t)(ntp_usec*0.065536) | (ntp_sec&0x0000ffff)<<16; // 0.065536 is used for convertion from useconds
5884 
5885  if (msg->header.type == _RTCP_PT_SR) { /* Sender report */
5886  struct switch_rtcp_sender_report* sr = (struct switch_rtcp_sender_report*)msg->body;
5887 
5888  rtp_session->stats.rtcp.packet_count += ntohl(sr->sender_info.pc);
5889  rtp_session->stats.rtcp.octet_count += ntohl(sr->sender_info.oc);
5890  packet_ssrc = sr->ssrc;
5891  /* Extracting LSR from NTP timestamp and save it */
5892  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 */
5893  rtp_session->stats.rtcp.last_recv_lsr_peer = htonl(lsr); /* Save it include it in the next SR */
5894  rtp_session->stats.rtcp.last_recv_lsr_local = lsr_now; /* Save it to calculate DLSR when generating next SR */
5895  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG10,"Received a SR with %d report blocks, " \
5896  "length in words = %d, " \
5897  "SSRC = 0x%X, " \
5898  "NTP MSW = %u, " \
5899  "NTP LSW = %u, " \
5900  "RTP timestamp = %u, " \
5901  "Sender Packet Count = %u, " \
5902  "Sender Octet Count = %u\n",
5903  msg->header.count,
5904  ntohs((uint16_t)msg->header.length),
5905  ntohl(sr->ssrc),
5906  ntohl(sr->sender_info.ntp_msw),
5907  ntohl(sr->sender_info.ntp_lsw),
5908  ntohl(sr->sender_info.ts),
5909  ntohl(sr->sender_info.pc),
5910  ntohl(sr->sender_info.oc));
5911 
5912 
5913  rtp_session->rtcp_frame.ssrc = ntohl(sr->ssrc);
5914  rtp_session->rtcp_frame.packet_type = (uint16_t)rtp_session->rtcp_recv_msg_p->header.type;
5915  rtp_session->rtcp_frame.ntp_msw = ntohl(sr->sender_info.ntp_msw);
5916  rtp_session->rtcp_frame.ntp_lsw = ntohl(sr->sender_info.ntp_lsw);
5917  rtp_session->rtcp_frame.timestamp = ntohl(sr->sender_info.ts);
5918  rtp_session->rtcp_frame.packet_count = ntohl(sr->sender_info.pc);
5919  rtp_session->rtcp_frame.octect_count = ntohl(sr->sender_info.oc);
5920 
5921  for (i = 0; i < (int)msg->header.count && i < MAX_REPORT_BLOCKS ; i++) {
5922  struct switch_rtcp_report_block *report = (struct switch_rtcp_report_block *) (msg->body + (sizeof(struct switch_rtcp_sr_head) + (i * sizeof(struct switch_rtcp_report_block))));
5923  uint32_t old_avg = rtp_session->rtcp_frame.reports[i].loss_avg;
5924  uint8_t percent_fraction = (uint8_t)report->fraction * 100 / 256 ;
5925  if (!rtp_session->rtcp_frame.reports[i].loss_avg) {
5926  rtp_session->rtcp_frame.reports[i].loss_avg = (uint8_t)percent_fraction;
5927  } else {
5928  rtp_session->rtcp_frame.reports[i].loss_avg = (uint32_t)(((float)rtp_session->rtcp_frame.reports[i].loss_avg * .7) +
5929  ((float)(uint8_t)percent_fraction * .3));
5930  }
5931 
5932  if (!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->rtcp_frame.reports[i].loss_avg != old_avg) {
5934  }
5935 
5936  rtp_session->rtcp_frame.reports[i].ssrc = ntohl(report->ssrc);
5937  rtp_session->rtcp_frame.reports[i].fraction = (uint8_t)report->fraction;
5938  rtp_session->rtcp_frame.reports[i].lost = ntohl(report->lost);
5940  rtp_session->rtcp_frame.reports[i].jitter = ntohl(report->jitter);
5941  rtp_session->rtcp_frame.reports[i].lsr = ntohl(report->lsr);
5942  rtp_session->rtcp_frame.reports[i].dlsr = ntohl(report->dlsr);
5943  if (rtp_session->rtcp_frame.reports[i].lsr && !rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU]) {
5944  double rtt_now;
5945  switch_time_exp_gmt(&now_hr,now);
5946  /* Calculating RTT = A - DLSR - LSR */
5947  rtt_now = (double)(lsr_now - rtp_session->rtcp_frame.reports[i].dlsr - rtp_session->rtcp_frame.reports[i].lsr)/65536;
5949  "Receiving an RTCP packet\n[%04d-%02d-%02d %02d:%02d:%02d.%d] SSRC[0x%x]\n"
5950  "RTT[%f] = A[%u] - DLSR[%u] - LSR[%u]\n",
5951  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,
5952  rtp_session->rtcp_frame.reports[i].ssrc, rtt_now,
5953  lsr_now, rtp_session->rtcp_frame.reports[i].dlsr, rtp_session->rtcp_frame.reports[i].lsr);
5954  if (!rtp_session->rtcp_frame.reports[i].rtt_avg) {
5955  rtp_session->rtcp_frame.reports[i].rtt_avg = rtt_now;
5956  } else {
5957  rtp_session->rtcp_frame.reports[i].rtt_avg = (double)((rtp_session->rtcp_frame.reports[i].rtt_avg * .7) + (rtt_now * .3 ));
5958  }
5959  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "RTT average %f\n",
5960  rtp_session->rtcp_frame.reports[i].rtt_avg);
5961  }
5962  }
5963  rtp_session->rtcp_frame.report_count = (uint16_t)i;
5964 
5965  } else { /* Receiver report */
5967  packet_ssrc = rr->ssrc;
5968  memset(&rtp_session->rtcp_frame, 0, sizeof(rtp_session->rtcp_frame));
5969  }
5970 
5971  rtp_session->rtcp_fresh_frame = 1;
5972  rtp_session->stats.rtcp.peer_ssrc = ntohl(packet_ssrc);
5973  }
5974 
5975 
5976  if (msg->header.type > 194 && msg->header.type < 255) {
5977  status = SWITCH_STATUS_SUCCESS;
5978  }
5979 
5980  return status;
5981 }
#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:333
#define NTP_TIME_OFFSET
Definition: switch_rtp.c:73
struct switch_rtcp_report_block_frame reports[MAX_REPORT_BLOCKS]
switch_bool_t rtcp_fresh_frame
Definition: switch_rtp.c:437
switch_jb_t * vbw
Definition: switch_rtp.c:416
switch_rtcp_hdr_t header
Definition: switch_rtp.c:180
switch_rtcp_ext_hdr_t header
Definition: switch_rtp.c:175
uint32_t highest_sequence_number_received
Definition: switch_rtp.c:469
char body[SWITCH_RTCP_MAX_BUF_LEN]
Definition: switch_rtp.c:176
int64_t switch_time_t
Definition: switch_apr.h:188
if((uint32_t)(unpack->cur-unpack->buf) > unpack->buflen)
switch_rtcp_numbers_t rtcp
Definition: switch_types.h:692
struct switch_rtcp_sender_info sender_info
Definition: switch_rtp.c:494
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:372
switch_rtp_stats_t stats
Definition: switch_rtp.c:428
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_rtcp_frame_t rtcp_frame
Definition: switch_rtp.c:316
static void handle_nack(switch_rtp_t *rtp_session, uint32_t nack)
Definition: switch_rtp.c:5763
switch_status_t
Common return values.
#define MAX_REPORT_BLOCKS
uint32_t last_recv_lsr_local
Definition: switch_types.h:684
switch_core_session_t * session
Definition: switch_rtp.c:446
void switch_jb_reset(switch_jb_t *jb)
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.
char body[SWITCH_RTCP_MAX_BUF_LEN]
Definition: switch_rtp.c:181
switch_time_t switch_time_now(void)
Definition: switch_apr.c:302
memset(buf, 0, buflen)
#define SWITCH_SIZE_T_FMT
rtcp_msg_t * rtcp_recv_msg_p
Definition: switch_rtp.c:329
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 6039 of file switch_rtp.c.

References switch_dtls_s::bytes, switch_dtls_s::data, do_dtls(), switch_rtp::flags, handle_ice(), rtcp_msg_t::header, switch_rtp_ice_t::ice_user, if(), process_rtcp_packet(), switch_rtp::recv_ctx, switch_rtp::rtcp_dtls, switch_rtp::rtcp_from_addr, switch_rtp::rtcp_ice, switch_rtp::rtcp_recv_msg_p, switch_rtp::rtcp_sock_input, switch_rtp::session, switch_rtp::srtp_idx_rtcp, switch_assert, SWITCH_CHANNEL_LOG, SWITCH_CHANNEL_SESSION_LOG, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_RTP_FLAG_ENABLE_RTCP, SWITCH_RTP_FLAG_PROXY_MEDIA, SWITCH_RTP_FLAG_SECURE_RECV, switch_socket_recvfrom(), SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, and switch_rtcp_hdr_s::version.

Referenced by rtp_common_read().

6040 {
6042 
6043  if (!rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
6044  return SWITCH_STATUS_FALSE;
6045  }
6046 
6047  switch_assert(bytes);
6048 
6049  *bytes = sizeof(rtcp_msg_t);
6050 
6051  if ((status = switch_socket_recvfrom(rtp_session->rtcp_from_addr, rtp_session->rtcp_sock_input, 0, (void *) rtp_session->rtcp_recv_msg_p, bytes))
6052  != SWITCH_STATUS_SUCCESS) {
6053  *bytes = 0;
6054  }
6055 
6056  if (rtp_session->rtcp_dtls) {
6057  char *b = (char *) rtp_session->rtcp_recv_msg_p;
6058 
6059  if (*b == 0 || *b == 1) {
6060  if (rtp_session->rtcp_ice.ice_user) {
6061  handle_ice(rtp_session, &rtp_session->rtcp_ice, (void *) rtp_session->rtcp_recv_msg_p, *bytes);
6062  }
6063  *bytes = 0;
6064  }
6065 
6066  if (*bytes && (*b >= 20) && (*b <= 64)) {
6067  rtp_session->rtcp_dtls->bytes = *bytes;
6068  rtp_session->rtcp_dtls->data = (void *) rtp_session->rtcp_recv_msg_p;
6069  } else {
6070  rtp_session->rtcp_dtls->bytes = 0;
6071  rtp_session->rtcp_dtls->data = NULL;
6072  }
6073 
6074  do_dtls(rtp_session, rtp_session->rtcp_dtls);
6075 
6076 
6077  if (rtp_session->rtcp_dtls->bytes) {
6078  *bytes = 0;
6079  }
6080  }
6081 
6082 
6083 
6084 #ifdef ENABLE_SRTP
6085  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && rtp_session->rtcp_recv_msg_p->header.version == 2) {
6086  //if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && (!rtp_session->ice.ice_user || rtp_session->rtcp_recv_msg_p->header.version == 2)) {
6087  int sbytes = (int) *bytes;
6088  err_status_t stat = 0;
6089 
6090 
6091  if ((stat = srtp_unprotect_rtcp(rtp_session->recv_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_recv_msg_p->header, &sbytes))) {
6092  //++rtp_session->srtp_errs[rtp_session->srtp_idx_rtp]++;
6093  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "RTCP UNPROTECT ERR\n");
6094  } else {
6095  //rtp_session->srtp_errs[rtp_session->srtp_idx_rtp] = 0;
6096  }
6097 
6098  *bytes = sbytes;
6099 
6100  }
6101 #endif
6102 
6103 
6104 #ifdef ENABLE_ZRTP
6105  if (zrtp_on && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && rtp_session->rtcp_recv_msg_p->header.version == 2) {
6106  /* ZRTP Recv */
6107  if (bytes) {
6108  unsigned int sbytes = (int) *bytes;
6109  zrtp_status_t stat = 0;
6110 
6111  stat = zrtp_process_srtcp(rtp_session->zrtp_stream, (void *) rtp_session->rtcp_recv_msg_p, &sbytes);
6112 
6113  switch (stat) {
6114  case zrtp_status_ok:
6115  *bytes = sbytes;
6116  break;
6117  case zrtp_status_drop:
6118  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error: zRTP protection drop with code %d\n", stat);
6119  *bytes = 0;
6120  break;
6121  case zrtp_status_fail:
6122  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error: zRTP protection fail with code %d\n", stat);
6123  *bytes = 0;
6124  break;
6125  default:
6126  break;
6127  }
6128  }
6129  }
6130 #endif
6131 
6132 
6133  if (*bytes) {
6134  return process_rtcp_packet(rtp_session, bytes);
6135  }
6136 
6137  return status;
6138 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
switch_sockaddr_t * rtcp_from_addr
Definition: switch_rtp.c:374
#define SWITCH_CHANNEL_LOG
int srtp_idx_rtcp
Definition: switch_rtp.c:346
switch_socket_t * rtcp_sock_input
Definition: switch_rtp.c:309
switch_rtcp_hdr_t header
Definition: switch_rtp.c:180
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:936
static int do_dtls(switch_rtp_t *rtp_session, switch_dtls_t *dtls)
Definition: switch_rtp.c:3144
if((uint32_t)(unpack->cur-unpack->buf) > unpack->buflen)
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:372
srtp_ctx_t * recv_ctx[2]
Definition: switch_rtp.c:336
switch_rtp_ice_t rtcp_ice
Definition: switch_rtp.c:378
static void handle_ice(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice, void *data, switch_size_t len)
Definition: switch_rtp.c:865
switch_status_t
Common return values.
switch_core_session_t * session
Definition: switch_rtp.c:446
switch_dtls_t * rtcp_dtls
Definition: switch_rtp.c:349
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 switch_status_t process_rtcp_packet(switch_rtp_t *rtp_session, switch_size_t *bytes)
Definition: switch_rtp.c:5984
#define switch_assert(expr)
switch_size_t bytes
Definition: switch_rtp.c:272
rtcp_msg_t * rtcp_recv_msg_p
Definition: switch_rtp.c:329
static switch_status_t read_rtp_packet ( switch_rtp_t rtp_session,
switch_size_t bytes,
switch_frame_flag_t flags,
switch_status_t  poll_status,
switch_bool_t  return_jb_packet 
)
static

Definition at line 5133 of file switch_rtp.c.

References switch_dtls_s::bytes, 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::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_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_read_time, switch_rtp::last_read_ts, switch_rtp::last_rtp_hdr, switch_rtp::last_seq, 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, memset(), switch_rtp::missed_count, switch_rtp_numbers_t::packet_count, switch_rtp::pause_jb, switch_rtp::punts, switch_rtp_numbers_t::raw_bytes, 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, reset_jitter_seq(), switch_rtp::rtcp_dtls, switch_rtp::rtcp_recv_msg_p, rtcp_stats(), RTP_BODY, RTP_BUG_IGNORE_MARK_BIT, switch_rtp::rtp_bugs, switch_rtp::rtp_from_addr, rtp_header_len, rtp_type, rtp_write_ready(), switch_rtp::session, SFF_PLC, SFF_PROXY_PACKET, SFF_RTCP, 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_core_session_get_channel(), switch_core_session_request_video_refresh(), switch_core_timer_sync(), switch_cp_addr(), switch_get_addr(), 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_micro_time_now(), switch_poll(), switch_rtp_clear_flag(), SWITCH_RTP_FLAG_BYTESWAP, SWITCH_RTP_FLAG_DEBUG_RTP_READ, 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_RESET, 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_TOO_LATE, switch_swap_linear(), switch_rtp::timer, switch_rtcp_sender_info::ts, switch_rtcp_hdr_s::type, switch_rtp::vb, switch_rtcp_hdr_s::version, WARN_SRTP_ERRS, and ZRTP_MAGIC_COOKIE.

Referenced by rtp_common_read().

5135 {
5137  uint32_t ts = 0;
5138  unsigned char *b = NULL;
5139  int sync = 0;
5140  switch_time_t now;
5141  switch_size_t xcheck_jitter = 0;
5142  int tries = 0;
5143  int block = 0;
5144 
5145  switch_assert(bytes);
5146  more:
5147 
5148  tries++;
5149 
5150  if (tries > 20) {
5151  if (rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) {
5152  switch_jb_reset(rtp_session->jb);
5153  }
5154  rtp_session->punts++;
5155  rtp_session->clean = 0;
5156  *bytes = 0;
5157  return SWITCH_STATUS_BREAK;
5158  }
5159 
5160  if (block) {
5161  int to = 20000;
5162  int fdr = 0;
5163 
5164  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
5165  to = 100000;
5166  } else {
5167  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && rtp_session->timer.interval) {
5168  to = rtp_session->timer.interval * 1000;
5169  }
5170  }
5171 
5172  poll_status = switch_poll(rtp_session->read_pollfd, 1, &fdr, to);
5173 
5174  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && rtp_session->timer.interval) {
5175  switch_core_timer_sync(&rtp_session->timer);
5176  }
5177 
5178  block = 0;
5179  }
5180 
5181  *bytes = sizeof(rtp_msg_t);
5182  sync = 0;
5183 
5184  rtp_session->has_rtp = 0;
5185  rtp_session->has_ice = 0;
5186  rtp_session->has_rtcp = 0;
5187  if (rtp_session->dtls) {
5188  rtp_session->dtls->bytes = 0;
5189  rtp_session->dtls->data = NULL;
5190  }
5191  memset(&rtp_session->last_rtp_hdr, 0, sizeof(rtp_session->last_rtp_hdr));
5192 
5193  if (poll_status == SWITCH_STATUS_SUCCESS) {
5194  status = switch_socket_recvfrom(rtp_session->from_addr, rtp_session->sock_input, 0, (void *) &rtp_session->recv_msg, bytes);
5195  } else {
5196  *bytes = 0;
5197  }
5198 
5199  if (*bytes) {
5200  b = (unsigned char *) &rtp_session->recv_msg;
5201 
5202  /* version 2 probably rtp, zrtp cookie present means zrtp */
5203  rtp_session->has_rtp = (rtp_session->recv_msg.header.version == 2 || ntohl(*(int *)(b+4)) == ZRTP_MAGIC_COOKIE);
5204 
5205  if ((*b >= 20) && (*b <= 64)) {
5206  if (rtp_session->dtls) {
5207  rtp_session->dtls->bytes = *bytes;
5208  rtp_session->dtls->data = (void *) &rtp_session->recv_msg;
5209  }
5210  rtp_session->has_ice = 0;
5211  rtp_session->has_rtp = 0;
5212  rtp_session->has_rtcp = 0;
5213  } else if (*b == 0 || *b == 1) {
5214  rtp_session->has_ice = 1;
5215  rtp_session->has_rtp = 0;
5216  rtp_session->has_rtcp = 0;
5217  } else {
5218  if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
5219  switch(rtp_session->recv_msg.header.pt) {
5220  case 64: // 192 Full INTRA-frame request.
5221  case 72: // 200 Sender report.
5222  case 73: // 201 Receiver report.
5223  case 74: // 202 Source description.
5224  case 75: // 203 Goodbye.
5225  case 76: // 204 Application-defined.
5226  case 77: // 205 Transport layer FB message.
5227  case 78: // 206 Payload-specific FB message.
5228  case 79: // 207 Extended report.
5229  rtp_session->has_rtcp = 1;
5230  rtp_session->has_rtp = 0;
5231  rtp_session->has_ice = 0;
5232  break;
5233  default:
5234  if (rtp_session->rtcp_recv_msg_p->header.version == 2 &&
5235  rtp_session->rtcp_recv_msg_p->header.type > 199 && rtp_session->rtcp_recv_msg_p->header.type < 208) {
5236  rtp_session->has_rtcp = 1;
5237  rtp_session->has_rtp = 0;
5238  rtp_session->has_ice = 0;
5239  }
5240  break;
5241  }
5242  }
5243  }
5244 
5245  if (rtp_session->has_rtp) {
5246  rtp_session->missed_count = 0;
5247  switch_cp_addr(rtp_session->rtp_from_addr, rtp_session->from_addr);
5248  rtp_session->last_rtp_hdr = rtp_session->recv_msg.header;
5249  }
5250  }
5251 
5252  if (!rtp_session->vb && (!rtp_session->jb || rtp_session->pause_jb || !jb_valid(rtp_session))) {
5253  if (*bytes > rtp_header_len && (rtp_session->has_rtp && check_recv_payload(rtp_session))) {
5254  xcheck_jitter = *bytes;
5255  check_jitter(rtp_session);
5256  }
5257  }
5258 
5259  if (check_rtcp_and_ice(rtp_session) == -1) {
5260  //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
5261  //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "WTF CHECK FAIL\n");
5262  //}
5263  return SWITCH_STATUS_GENERR;
5264  }
5265 
5266  if (rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
5267  goto udptl;
5268  }
5269 
5270 
5271  if (*bytes) {
5272  *flags &= ~SFF_PROXY_PACKET;
5273 
5274  //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
5275  // switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "WTF BYTES %ld b=%d\n", *bytes, *b);
5276  //}
5277 
5278 
5279  if (rtp_session->has_ice) {
5280  if (rtp_session->ice.ice_user) {
5281  handle_ice(rtp_session, &rtp_session->ice, (void *) &rtp_session->recv_msg, *bytes);
5282  }
5283  *bytes = 0;
5284  sync = 1;
5285  }
5286  }
5287 
5288  if (rtp_session->dtls) {
5289 
5290  if (rtp_session->rtcp_dtls && rtp_session->rtcp_dtls != rtp_session->dtls) {
5291  rtp_session->rtcp_dtls->bytes = 0;
5292  rtp_session->rtcp_dtls->data = NULL;
5293  do_dtls(rtp_session, rtp_session->rtcp_dtls);
5294  }
5295 
5296  do_dtls(rtp_session, rtp_session->dtls);
5297 
5298  if (rtp_session->dtls && rtp_session->dtls->bytes) {
5299  *bytes = 0;
5300  sync = 1;
5301  }
5302  }
5303 
5304  if (status == SWITCH_STATUS_SUCCESS && *bytes) {
5305  if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
5306  *flags &= ~SFF_RTCP;
5307  if (rtp_session->has_rtcp) {
5308  *flags |= SFF_RTCP;
5309 
5310 #ifdef ENABLE_SRTP
5311  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV]) {
5312  int sbytes = (int) *bytes;
5313  err_status_t stat = 0;
5314 
5315 
5316  if ((stat = srtp_unprotect_rtcp(rtp_session->recv_ctx[rtp_session->srtp_idx_rtcp], &rtp_session->rtcp_recv_msg_p->header, &sbytes))) {
5317  //++rtp_session->srtp_errs[rtp_session->srtp_idx_rtp]++;
5318  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "RTCP UNPROTECT ERR\n");
5319  } else {
5320  //rtp_session->srtp_errs[rtp_session->srtp_idx_rtp] = 0;
5321  }
5322 
5323  *bytes = sbytes;
5324  }
5325 #endif
5326  return SWITCH_STATUS_SUCCESS;
5327  }
5328  }
5329  }
5330 
5331 
5332 
5333  if ((*bytes && (!rtp_write_ready(rtp_session, *bytes, __LINE__) || !rtp_session->has_rtp || rtp_session->has_rtcp)) || sync) {
5334  rtp_session->hot_hits = 0;
5335  block = 1;
5336  *bytes = 0;
5337  goto more;
5338  }
5339 
5340  if (*bytes && rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_READ]) {
5341  const char *tx_host;
5342  const char *old_host;
5343  const char *my_host;
5344 
5345  char bufa[50], bufb[50], bufc[50];
5346 
5347 
5348  tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtp_from_addr);
5349  old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
5350  my_host = switch_get_addr(bufc, sizeof(bufc), rtp_session->local_addr);
5351 
5353  "R %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n",
5354  rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "No-Name",
5355  (long) *bytes,
5356  my_host, switch_sockaddr_get_port(rtp_session->local_addr),
5357  old_host, rtp_session->remote_port,
5358  tx_host, switch_sockaddr_get_port(rtp_session->rtp_from_addr),
5359  rtp_session->last_rtp_hdr.pt, ntohl(rtp_session->last_rtp_hdr.ts), ntohs(rtp_session->last_rtp_hdr.seq),
5360  rtp_session->last_rtp_hdr.m);
5361 
5362  }
5363 
5364 #ifdef RTP_READ_PLOSS
5365  {
5366  int r = (rand() % 10000) + 1;
5367  if (r <= 200) {
5369  "Simulate dropped packet ......... ts: %u seq: %u\n", ntohl(rtp_session->last_rtp_hdr.ts), ntohs(rtp_session->last_rtp_hdr.seq));
5370  *bytes = 0;
5371  }
5372  }
5373 #endif
5374 
5375 
5376 
5377  udptl:
5378 
5379  ts = 0;
5380  rtp_session->recv_msg.ebody = NULL;
5381  now = switch_micro_time_now();
5382 
5383  if (*bytes) {
5384  uint16_t seq = ntohs((uint16_t) rtp_session->last_rtp_hdr.seq);
5385  ts = ntohl(rtp_session->last_rtp_hdr.ts);
5386 
5387 #ifdef DEBUG_MISSED_SEQ
5388  if (rtp_session->last_seq && rtp_session->last_seq+1 != seq) {
5389  //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]
5390  switch_size_t flushed_packets_diff = rtp_session->stats.inbound.flush_packet_count - rtp_session->last_flush_packet_count;
5391  switch_size_t num_missed = (switch_size_t)seq - (rtp_session->last_seq+1);
5392 
5393  if (num_missed == 1) { /* We missed one packet */
5394  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missed one RTP frame with sequence [%d]%s. Time since last read [%ld]\n",
5395  rtp_session->last_seq+1, (flushed_packets_diff == 1) ? " (flushed by FS)" : " (missed)",
5396  rtp_session->last_read_time ? now-rtp_session->last_read_time : 0);
5397  } else { /* We missed multiple packets */
5398  if (flushed_packets_diff == 0) {
5400  "Missed %ld RTP frames from sequence [%d] to [%d] (missed). Time since last read [%ld]\n",
5401  num_missed, rtp_session->last_seq+1, seq-1,
5402  rtp_session->last_read_time ? now-rtp_session->last_read_time : 0);
5403  } else if (flushed_packets_diff == num_missed) {
5405  "Missed %ld RTP frames from sequence [%d] to [%d] (flushed by FS). Time since last read [%ld]\n",
5406  num_missed, rtp_session->last_seq+1, seq-1,
5407  rtp_session->last_read_time ? now-rtp_session->last_read_time : 0);
5408  } else if (num_missed > flushed_packets_diff) {
5410  "Missed %ld RTP frames from sequence [%d] to [%d] (%ld packets flushed by FS, %ld packets missed)."
5411  " Time since last read [%ld]\n",
5412  num_missed, rtp_session->last_seq+1, seq-1,
5413  flushed_packets_diff, num_missed-flushed_packets_diff,
5414  rtp_session->last_read_time ? now-rtp_session->last_read_time : 0);
5415  } else {
5417  "Missed %ld RTP frames from sequence [%d] to [%d] (%ld packets flushed by FS). Time since last read [%ld]\n",
5418  num_missed, rtp_session->last_seq+1, seq-1,
5419  flushed_packets_diff, rtp_session->last_read_time ? now-rtp_session->last_read_time : 0);
5420  }
5421  }
5422 
5423  }
5424 #endif
5425  rtp_session->last_seq = seq;
5426 
5427 
5428  rtp_session->last_flush_packet_count = rtp_session->stats.inbound.flush_packet_count;
5429 
5430 
5431  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && now - rtp_session->last_read_time > 5000000) {
5432  switch_rtp_video_refresh(rtp_session);
5433  }
5434 
5435  rtp_session->last_read_time = now;
5436  }
5437 
5438  if (!rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] && !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] &&
5439  *bytes && rtp_session->last_rtp_hdr.pt != rtp_session->recv_te &&
5440  ts && !rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session) && ts == rtp_session->last_cng_ts) {
5441  /* we already sent this frame..... */
5442  *bytes = 0;
5443  return SWITCH_STATUS_SUCCESS;
5444  }
5445 
5446  if (*bytes) {
5447  if (!rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
5448 #ifdef ENABLE_ZRTP
5449  /* ZRTP Recv */
5450  if (zrtp_on) {
5451 
5452  unsigned int sbytes = (int) *bytes;
5453  zrtp_status_t stat = 0;
5454 
5455  stat = zrtp_process_srtp(rtp_session->zrtp_stream, (void *) &rtp_session->recv_msg, &sbytes);
5456 
5457  switch (stat) {
5458  case zrtp_status_ok:
5459  *bytes = sbytes;
5460  break;
5461  case zrtp_status_drop:
5462  /* switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Error: zRTP protection drop with code %d\n", stat); */
5463  *bytes = 0;
5464  return SWITCH_STATUS_SUCCESS;
5465  case zrtp_status_fail:
5466  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error: zRTP protection fail with code %d\n", stat);
5467  return SWITCH_STATUS_FALSE;
5468  default:
5469  break;
5470  }
5471  }
5472 #endif
5473 
5474 #ifdef ENABLE_SRTP
5475  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && rtp_session->has_rtp &&
5476  (check_recv_payload(rtp_session) ||
5477  rtp_session->last_rtp_hdr.pt == rtp_session->recv_te ||
5478  rtp_session->last_rtp_hdr.pt == rtp_session->cng_pt)) {
5479  //if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && (!rtp_session->ice.ice_user || rtp_session->has_rtp)) {
5480  int sbytes = (int) *bytes;
5481  err_status_t stat = 0;
5482 
5483  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_RESET] || !rtp_session->recv_ctx[rtp_session->srtp_idx_rtp]) {
5485  srtp_dealloc(rtp_session->recv_ctx[rtp_session->srtp_idx_rtp]);
5486  rtp_session->recv_ctx[rtp_session->srtp_idx_rtp] = NULL;
5487  if ((stat = srtp_create(&rtp_session->recv_ctx[rtp_session->srtp_idx_rtp],
5488  &rtp_session->recv_policy[rtp_session->srtp_idx_rtp])) || !rtp_session->recv_ctx[rtp_session->srtp_idx_rtp]) {
5489 
5490  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] = 0;
5491  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error! RE-Activating Secure RTP RECV\n");
5492  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] = 0;
5493  return SWITCH_STATUS_FALSE;
5494  } else {
5495 
5496  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "RE-Activating Secure RTP RECV\n");
5497  rtp_session->srtp_errs[rtp_session->srtp_idx_rtp] = 0;
5498  }
5499  }
5500 
5501  if (!(*flags & SFF_PLC)) {
5502  stat = srtp_unprotect(rtp_session->recv_ctx[rtp_session->srtp_idx_rtp], &rtp_session->recv_msg.header, &sbytes);
5503  if (rtp_session->flags[SWITCH_RTP_FLAG_NACK] && stat == err_status_replay_fail) {
5504  /* false alarm nack */
5505  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "REPLAY ERR, FALSE NACK\n");
5506  stat = 0;
5507  sbytes = 0;
5508  *bytes = 0;
5509  goto more;
5510  }
5511  }
5512 
5513  if (stat && rtp_session->recv_msg.header.pt != rtp_session->recv_te && rtp_session->recv_msg.header.pt != rtp_session->cng_pt) {
5514  int errs = ++rtp_session->srtp_errs[rtp_session->srtp_idx_rtp];
5515  if (stat != 10) {
5516  char *msg;
5517  if (stat == err_status_replay_fail) msg="replay check failed";
5518  else if (stat == err_status_auth_fail) msg="auth check failed";
5519  else msg="";
5520  if (errs >= MAX_SRTP_ERRS) {
5521  switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
5523  "SRTP %s unprotect failed with code %d (%s) %ld bytes %d errors\n",
5524  rtp_type(rtp_session), stat, msg, (long)*bytes, errs);
5526  "Ending call due to SRTP error\n");
5528  } else if (errs >= WARN_SRTP_ERRS && !(errs % WARN_SRTP_ERRS)) {
5530  "SRTP %s unprotect failed with code %d (%s) %ld bytes %d errors\n",
5531  rtp_type(rtp_session), stat, msg, (long)*bytes, errs);
5532  }
5533  }
5534  sbytes = 0;
5535  } else {
5536  rtp_session->srtp_errs[rtp_session->srtp_idx_rtp] = 0;
5537  }
5538 
5539  *bytes = sbytes;
5540  }
5541 #endif
5542  }
5543 
5544 
5545  if (rtp_session->has_rtp) {
5546  if (rtp_session->recv_msg.header.cc > 0) { /* Contributing Source Identifiers (4 bytes = sizeof CSRC header)*/
5547  rtp_session->recv_msg.ebody = RTP_BODY(rtp_session) + (rtp_session->recv_msg.header.cc * 4);
5548  }
5549 
5550  /* recalculate body length in case rtp extension used */
5551  if (!rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] &&
5552  rtp_session->has_rtp && rtp_session->recv_msg.header.x) { /* header extensions */
5553  uint16_t length;
5554 
5555  rtp_session->recv_msg.ext = (switch_rtp_hdr_ext_t *) RTP_BODY(rtp_session);
5556  length = ntohs((uint16_t)rtp_session->recv_msg.ext->length);
5557 
5558  if (length < SWITCH_RTP_MAX_BUF_LEN_WORDS) {
5559  rtp_session->recv_msg.ebody = (char *)rtp_session->recv_msg.ext + (length * 4) + 4;
5560  if (*bytes > (length * 4 + 4)) {
5561  *bytes -= (length * 4 + 4);
5562  } else {
5563  *bytes = 0;
5564  }
5565  }
5566  }
5567 
5568 
5569 #ifdef DEBUG_CHROME
5570 
5571  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->has_rtp) {
5572 
5574  "VIDEO: seq: %d ts: %u len: %ld %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x mark: %d\n",
5575  ntohs(rtp_session->last_rtp_hdr.seq), ntohl(rtp_session->last_rtp_hdr.ts), *bytes,
5576  *((uint8_t *)RTP_BODY(rtp_session)), *((uint8_t *)RTP_BODY(rtp_session) + 1),
5577  *((uint8_t *)RTP_BODY(rtp_session) + 2), *((uint8_t *)RTP_BODY(rtp_session) + 3),
5578  *((uint8_t *)RTP_BODY(rtp_session) + 4), *((uint8_t *)RTP_BODY(rtp_session) + 5),
5579  *((uint8_t *)RTP_BODY(rtp_session) + 6), *((uint8_t *)RTP_BODY(rtp_session) + 7),
5580  *((uint8_t *)RTP_BODY(rtp_session) + 8), *((uint8_t *)RTP_BODY(rtp_session) + 9),
5581  *((uint8_t *)RTP_BODY(rtp_session) + 10), rtp_session->last_rtp_hdr.m);
5582 
5583  }
5584 #endif
5585 
5586 
5587 
5588  if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
5589  rtcp_stats(rtp_session);
5590  }
5591  }
5592 
5593 
5594  rtp_session->stats.inbound.raw_bytes += *bytes;
5595 
5596  if (rtp_session->last_rtp_hdr.pt == rtp_session->recv_te) {
5597  rtp_session->stats.inbound.dtmf_packet_count++;
5598  } else if (rtp_session->last_rtp_hdr.pt == rtp_session->cng_pt || rtp_session->last_rtp_hdr.pt == 13) {
5599  rtp_session->stats.inbound.cng_packet_count++;
5600  } else {
5601  rtp_session->stats.inbound.media_packet_count++;
5602  rtp_session->stats.inbound.media_bytes += *bytes;
5603  }
5604 
5605  rtp_session->stats.inbound.packet_count++;
5606  }
5607 
5608  if (rtp_session->last_rtp_hdr.pt == rtp_session->recv_te ||
5609  (*bytes < rtp_header_len && *bytes > 0) ||
5610  rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
5611  return SWITCH_STATUS_BREAK;
5612  }
5613 
5614  if (ts) {
5615  rtp_session->last_read_ts = ts;
5616  }
5617 
5618  if (rtp_session->flags[SWITCH_RTP_FLAG_BYTESWAP] && check_recv_payload(rtp_session)) {
5619  switch_swap_linear((int16_t *)RTP_BODY(rtp_session), (int) *bytes - rtp_header_len);
5620  }
5621 
5622  if (rtp_session->flags[SWITCH_RTP_FLAG_KILL_JB]) {
5623  rtp_session->flags[SWITCH_RTP_FLAG_KILL_JB] = 0;
5624 
5625  if (rtp_session->jb) {
5626  switch_jb_destroy(&rtp_session->jb);
5627  }
5628 
5629  if (rtp_session->vb) {
5630  switch_jb_destroy(&rtp_session->vb);
5631  }
5632  }
5633 
5634  if (rtp_session->has_rtp && *bytes) {
5635  uint32_t read_ssrc = ntohl(rtp_session->last_rtp_hdr.ssrc);
5636 
5637  if (rtp_session->vb && jb_valid(rtp_session)) {
5638  status = switch_jb_put_packet(rtp_session->vb, (switch_rtp_packet_t *) &rtp_session->recv_msg, *bytes);
5639 
5640  if (status == SWITCH_STATUS_TOO_LATE) {
5641  goto more;
5642  }
5643 
5644  status = SWITCH_STATUS_FALSE;
5645  *bytes = 0;
5646 
5647  if (!return_jb_packet) {
5648  return status;
5649  }
5650  }
5651 
5652  if (rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) {
5653 
5654  if (rtp_session->last_jb_read_ssrc && rtp_session->last_jb_read_ssrc != read_ssrc) {
5655  switch_jb_reset(rtp_session->jb);
5656  }
5657 
5658  rtp_session->last_jb_read_ssrc = read_ssrc;
5659 
5660  if (!rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && rtp_session->timer.interval) {
5661  switch_core_timer_sync(&rtp_session->timer);
5662  reset_jitter_seq(rtp_session);
5663  }
5664 
5665  status = switch_jb_put_packet(rtp_session->jb, (switch_rtp_packet_t *) &rtp_session->recv_msg, *bytes);
5666  if (status == SWITCH_STATUS_TOO_LATE) {
5667  goto more;
5668  }
5669 
5670 
5671  status = SWITCH_STATUS_FALSE;
5672  *bytes = 0;
5673 
5674  if (!return_jb_packet) {
5675  return status;
5676  }
5677  } else {
5678  if (rtp_session->last_rtp_hdr.m && rtp_session->last_rtp_hdr.pt != rtp_session->recv_te &&
5679  !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && !(rtp_session->rtp_bugs & RTP_BUG_IGNORE_MARK_BIT)) {
5681  } else if (rtp_session->last_jb_read_ssrc && rtp_session->last_jb_read_ssrc != read_ssrc) {
5683  }
5684  }
5685  }
5686 
5687  if (!*bytes || rtp_session->has_rtp) {
5688 
5689  if (rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) {
5690  switch_status_t jstatus = switch_jb_get_packet(rtp_session->jb, (switch_rtp_packet_t *) &rtp_session->recv_msg, bytes);
5691 
5692  status = jstatus;
5693 
5694  switch(jstatus) {
5696  if (rtp_session->punts < 4) {
5697  block = 1;
5698  goto more;
5699  }
5700  *bytes = 0;
5701  break;
5703  {
5704  int pt = get_recv_payload(rtp_session);
5705  (*flags) |= SFF_PLC;
5706  status = SWITCH_STATUS_SUCCESS;
5707  *bytes = switch_jb_get_last_read_len(rtp_session->jb);
5708  rtp_session->last_rtp_hdr = rtp_session->recv_msg.header;
5709  if (pt > -1) {
5710  rtp_session->last_rtp_hdr.pt = pt;
5711  }
5712  }
5713  break;
5714  case SWITCH_STATUS_SUCCESS:
5715  default:
5716  {
5717  rtp_session->stats.inbound.jb_packet_count++;
5718  status = SWITCH_STATUS_SUCCESS;
5719  rtp_session->last_rtp_hdr = rtp_session->recv_msg.header;
5720  if (++rtp_session->clean > 200) {
5721  rtp_session->punts = 0;
5722  }
5723  if (!xcheck_jitter) {
5724  check_jitter(rtp_session);
5725  xcheck_jitter = *bytes;
5726  }
5727  }
5728  break;
5729  }
5730  }
5731 
5732  if (rtp_session->vb && jb_valid(rtp_session)) {
5733  switch_status_t vstatus = switch_jb_get_packet(rtp_session->vb, (switch_rtp_packet_t *) &rtp_session->recv_msg, bytes);
5734  status = vstatus;
5735 
5736  switch(vstatus) {
5737  case SWITCH_STATUS_RESTART:
5739  status = SWITCH_STATUS_BREAK;
5740  break;
5742  status = SWITCH_STATUS_BREAK;
5743  break;
5744  case SWITCH_STATUS_BREAK:
5746  default:
5747  break;
5748  }
5749 
5750  if (vstatus == SWITCH_STATUS_SUCCESS) {
5751  rtp_session->last_rtp_hdr = rtp_session->recv_msg.header;
5752  if (!xcheck_jitter) {
5753  check_jitter(rtp_session);
5754  xcheck_jitter = *bytes;
5755  }
5756  }
5757  }
5758  }
5759 
5760  return status;
5761 }
#define ZRTP_MAGIC_COOKIE
Definition: switch_rtp.c:74
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
Definition: switch_time.c:310
switch_pollfd_t * read_pollfd
Definition: switch_rtp.c:310
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's state machine to end.
uint8_t has_ice
Definition: switch_rtp.c:452
char * ebody
Definition: switch_rtp.c:115
#define SWITCH_CHANNEL_SESSION_LOG(x)
uint16_t switch_sockaddr_get_port(switch_sockaddr_t *sa)
Definition: switch_apr.c:916
switch_rtp_numbers_t inbound
Definition: switch_types.h:690
#define WARN_SRTP_ERRS
Definition: switch_rtp.c:71
#define SWITCH_CHANNEL_LOG
switch_sockaddr_t * remote_addr
Definition: switch_rtp.c:326
uint32_t srtp_errs[2]
Definition: switch_rtp.c:341
switch_status_t switch_poll(switch_pollfd_t *aprset, int32_t numsock, int32_t *nsds, switch_interval_time_t timeout)
Definition: switch_apr.c:1015
switch_socket_t * sock_input
Definition: switch_rtp.c:309
switch_port_t remote_port
Definition: switch_rtp.c:398
int srtp_idx_rtcp
Definition: switch_rtp.c:346
uint32_t last_jb_read_ssrc
Definition: switch_rtp.c:356
uint16_t last_seq
Definition: switch_rtp.c:442
switch_size_t switch_jb_get_last_read_len(switch_jb_t *jb)
uint8_t has_rtp
Definition: switch_rtp.c:450
#define MAX_SRTP_ERRS
Definition: switch_rtp.c:72
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:180
switch_status_t switch_jb_destroy(switch_jb_t **jbp)
switch_size_t media_bytes
Definition: switch_types.h:628
switch_size_t dtmf_packet_count
Definition: switch_types.h:634
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:936
static int do_dtls(switch_rtp_t *rtp_session, switch_dtls_t *dtls)
Definition: switch_rtp.c:3144
#define rtp_type(rtp_session)
Definition: switch_rtp.c:513
switch_payload_t cng_pt
Definition: switch_rtp.c:406
_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
static int get_recv_payload(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:5110
switch_size_t raw_bytes
Definition: switch_types.h:627
static int rtcp_stats(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:1849
switch_time_t last_read_time
Definition: switch_rtp.c:443
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:372
srtp_ctx_t * recv_ctx[2]
Definition: switch_rtp.c:336
switch_sockaddr_t * rtp_from_addr
Definition: switch_rtp.c:374
uint32_t last_read_ts
Definition: switch_rtp.c:364
static int rtp_write_ready(switch_rtp_t *rtp_session, uint32_t bytes, int line)
Definition: switch_rtp.c:7161
uintptr_t switch_size_t
switch_sockaddr_t * local_addr
Definition: switch_rtp.c:313
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:348
switch_size_t packet_count
Definition: switch_types.h:629
switch_jb_t * vb
Definition: switch_rtp.c:415
switch_rtp_stats_t stats
Definition: switch_rtp.c:428
switch_size_t jb_packet_count
Definition: switch_types.h:633
#define SWITCH_CHANNEL_SESSION_LOG_CLEAN(x)
uint8_t punts
Definition: switch_rtp.c:453
#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:4747
static void handle_ice(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice, void *data, switch_size_t len)
Definition: switch_rtp.c:865
void switch_rtp_set_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Set an RTP Flag.
Definition: switch_rtp.c:4702
switch_status_t switch_core_session_request_video_refresh(switch_core_session_t *session)
switch_rtp_hdr_ext_t * ext
Definition: switch_rtp.c:114
switch_jb_t * jb
Definition: switch_rtp.c:414
switch_status_t
Common return values.
uint32_t missed_count
Definition: switch_rtp.c:418
srtp_hdr_t header
Definition: switch_rtp.c:112
switch_size_t last_flush_packet_count
Definition: switch_rtp.c:444
switch_timer_t timer
Definition: switch_rtp.c:411
rtp_msg_t recv_msg
Definition: switch_rtp.c:327
srtp_policy_t recv_policy[2]
Definition: switch_rtp.c:339
static void reset_jitter_seq(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:1595
switch_core_session_t * session
Definition: switch_rtp.c:446
#define SWITCH_RTP_MAX_BUF_LEN_WORDS
Definition: switch_rtp.h:46
switch_size_t flush_packet_count
Definition: switch_types.h:636
static int check_rtcp_and_ice(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:1956
uint8_t has_rtcp
Definition: switch_rtp.c:451
void switch_jb_reset(switch_jb_t *jb)
switch_dtls_t * rtcp_dtls
Definition: switch_rtp.c:349
uint8_t pause_jb
Definition: switch_rtp.c:441
switch_rtp_ice_t ice
Definition: switch_rtp.c:377
switch_size_t media_packet_count
Definition: switch_types.h:631
switch_rtp_bug_flag_t rtp_bugs
Definition: switch_rtp.c:427
int srtp_idx_rtp
Definition: switch_rtp.c:345
static int jb_valid(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:4967
switch_payload_t recv_te
Definition: switch_rtp.c:405
uint32_t hot_hits
Definition: switch_rtp.c:433
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:1604
const char * switch_get_addr(char *buf, switch_size_t len, switch_sockaddr_t *in)
Definition: switch_apr.c:894
uint32_t last_cng_ts
Definition: switch_rtp.c:365
switch_sockaddr_t * from_addr
Definition: switch_rtp.c:374
#define switch_assert(expr)
void switch_rtp_video_refresh(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:4362
rtp_hdr_t last_rtp_hdr
Definition: switch_rtp.c:351
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:5085
switch_size_t bytes
Definition: switch_rtp.c:272
memset(buf, 0, buflen)
uint8_t clean
Definition: switch_rtp.c:454
#define rtp_header_len
Definition: switch_rtp.c:66
rtcp_msg_t * rtcp_recv_msg_p
Definition: switch_rtp.c:329
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:635
static void reset_jitter_seq ( switch_rtp_t rtp_session)
static
static void rtcp_generate_report_block ( switch_rtp_t rtp_session,
struct switch_rtcp_report_block rtcp_report_block 
)
static

Definition at line 1756 of file switch_rtp.c.

References switch_rtcp_numbers_t::base_seq, switch_rtcp_numbers_t::cum_lost, switch_rtcp_numbers_t::cycle, 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, NTP_TIME_OFFSET, 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(), SWITCH_RTP_FLAG_VIDEO, and switch_time_now().

Referenced by check_rtcp_and_ice().

1756  {
1757 #ifdef DEBUG_RTCP
1758  switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
1759 #endif
1760  switch_rtcp_numbers_t * stats=&rtp_session->stats.rtcp;
1761  switch_time_t now;
1762  uint32_t expected_pkt, dlsr;
1763  int32_t pkt_lost;
1764  uint32_t ntp_sec, ntp_usec, lsr_now, sec;
1765  now = switch_time_now();
1766  sec = (uint32_t)(now/1000000); /* convert to seconds */
1767  ntp_sec = sec+NTP_TIME_OFFSET; /* convert to NTP seconds */
1768  ntp_usec = (uint32_t)(now - (sec*1000000)); /* remove seconds to keep only the microseconds */
1769 
1770  /* Packet loss */
1771  if (stats->rtcp_rtp_count == 0) {
1772  expected_pkt = stats->high_ext_seq_recv - stats->base_seq + 1;
1773  } else {
1774  expected_pkt = stats->high_ext_seq_recv - stats->last_rpt_ext_seq;
1775  }
1776 
1777  pkt_lost = expected_pkt - stats->period_pkt_count;
1778  stats->cum_lost=stats->cum_lost+pkt_lost;
1779  if (expected_pkt > 0 && pkt_lost > 0) {
1780  rtcp_report_block->fraction = (uint8_t) (pkt_lost * 256 / expected_pkt);
1781  } else {
1782  rtcp_report_block->fraction = 0;
1783  }
1784 #if SWITCH_BYTE_ORDER != __BIG_ENDIAN
1785  /* Reversing byte order for 24bits */
1786  rtcp_report_block->lost = (((stats->cum_lost&0x0000FF)<<16) | ((stats->cum_lost&0x00FF00)) | ((stats->cum_lost&0xFF0000)>>16));
1787 #endif
1788 
1789 #ifdef DEBUG_RTCP
1790  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO])
1791  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",
1792  rtp_session->remote_ssrc, stats->period_pkt_count, expected_pkt,
1793  stats->cum_lost, pkt_lost, rtcp_report_block->fraction, stats->high_ext_seq_recv&0x0000ffff,
1794  stats->cycle, stats->last_rpt_ext_seq&0x0000ffff, stats->last_rpt_cycle, rtp_session->stats.rtcp.peer_ssrc
1795  );
1796 #endif
1797  rtcp_report_block->highest_sequence_number_received = htonl(stats->high_ext_seq_recv);
1798 
1799  /* Jitter */
1800  rtcp_report_block->jitter = htonl((uint32_t)stats->inter_jitter);
1801 
1802  /* Delay since Last Sender Report (DLSR) : 32bits, 1/65536 seconds */
1803  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) */
1804 
1805  if (stats->last_recv_lsr_local) {
1806  dlsr = lsr_now - stats->last_recv_lsr_local;
1807  } else {
1808  dlsr = 0;
1809  }
1810  rtcp_report_block->lsr = stats->last_recv_lsr_peer;
1811  rtcp_report_block->dlsr = htonl(dlsr);
1812  rtcp_report_block->ssrc = htonl(rtp_session->stats.rtcp.peer_ssrc);
1813  stats->rtcp_rtp_count++;
1814 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
#define NTP_TIME_OFFSET
Definition: switch_rtp.c:73
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:469
int64_t switch_time_t
Definition: switch_apr.h:188
switch_rtcp_numbers_t rtcp
Definition: switch_types.h:692
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:372
switch_rtp_stats_t stats
Definition: switch_rtp.c:428
switch_memory_pool_t * pool
Definition: switch_rtp.c:373
uint32_t last_recv_lsr_local
Definition: switch_types.h:684
uint32_t remote_ssrc
Definition: switch_rtp.c:355
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:302
static void rtcp_generate_sender_info ( switch_rtp_t rtp_session,
struct switch_rtcp_sender_info sr 
)
static

Definition at line 1731 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_time_exp_gmt(), switch_time_now(), 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().

1731  {
1732  switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
1733  switch_time_t now;
1734  uint32_t sec, ntp_sec, ntp_usec;
1735  switch_time_exp_t now_hr;
1736  now = switch_time_now();
1737  sec = (uint32_t)(now/1000000); /* convert to seconds */
1738  ntp_sec = sec+NTP_TIME_OFFSET; /* convert to NTP seconds */
1739  sr->ntp_msw = htonl(ntp_sec); /* store result in "most significant word" */
1740  ntp_usec = (uint32_t)(now - (sec*1000000)); /* remove seconds to keep only the microseconds */
1741  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" */
1742 
1743  sr->ts = htonl(rtp_session->last_write_ts);
1744  sr->pc = htonl(rtp_session->stats.outbound.packet_count);
1745  sr->oc = htonl((rtp_session->stats.outbound.raw_bytes - rtp_session->stats.outbound.packet_count * sizeof(srtp_hdr_t)));
1746 
1747  switch_time_exp_gmt(&now_hr,now);
1748  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]\n",
1749  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,
1750  (ntohl(sr->ntp_lsw)&0xffff0000)>>16 | (ntohl(sr->ntp_msw)&0x0000ffff)<<16,
1751  ntohl(sr->ntp_msw),ntohl(sr->ntp_lsw), rtp_session->stats.rtcp.ssrc
1752  );
1753 }
#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:333
#define NTP_TIME_OFFSET
Definition: switch_rtp.c:73
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:692
switch_size_t raw_bytes
Definition: switch_types.h:627
switch_rtp_numbers_t outbound
Definition: switch_types.h:691
switch_size_t packet_count
Definition: switch_types.h:629
switch_rtp_stats_t stats
Definition: switch_rtp.c:428
switch_memory_pool_t * pool
Definition: switch_rtp.c:373
uint32_t last_write_ts
Definition: switch_rtp.c:363
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:302
static int rtcp_stats ( switch_rtp_t rtp_session)
static

Definition at line 1849 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, and switch_rtp::timer.

Referenced by read_rtp_packet().

1850 {
1851  switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
1852  srtp_hdr_t * hdr = &rtp_session->last_rtp_hdr;
1853  switch_rtcp_numbers_t * stats = &rtp_session->stats.rtcp;
1854  uint32_t packet_spacing_diff = 0, pkt_tsdiff, pkt_extended_seq;
1855  uint16_t pkt_seq, seq_diff, max_seq;
1856  const int MAX_DROPOUT = 3000;
1857  const int MAX_MISORDER = 100;
1858  const int RTP_SEQ_MOD = (1<<16);
1859 
1860  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)
1861  return 0; /* do not process RTCP in current state */
1862 
1863  pkt_seq = (uint16_t) ntohs((uint16_t) rtp_session->last_rtp_hdr.seq);
1864 
1865  /* Detect sequence number cycle change */
1866  max_seq = stats->high_ext_seq_recv&0x0000ffff;
1867  seq_diff = pkt_seq - max_seq;
1868 
1869  if (seq_diff < MAX_DROPOUT) { /* in order, with permissible gap */
1870  if (pkt_seq < max_seq) {
1871  stats->cycle++;
1872  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",
1873  pkt_seq, stats->cycle, max_seq, stats->ssrc, rtp_session->timer.samplecount);
1874  }
1875  pkt_extended_seq = stats->cycle << 16 | pkt_seq; /* getting the extended packet extended sequence ID */
1876  if (pkt_extended_seq > stats->high_ext_seq_recv) {
1877  stats->high_ext_seq_recv = pkt_extended_seq;
1878  }
1879  }
1880  else if (seq_diff <= (RTP_SEQ_MOD - MAX_MISORDER)) { /* the sequence number made a very large jump */
1881  if (pkt_seq == stats->bad_seq) {
1882  rtcp_stats_init(rtp_session);
1883  } else {
1884  stats->bad_seq = (pkt_seq + 1) & (RTP_SEQ_MOD-1);
1885  }
1886  return 0; /* no stats, packet is out of sync and will be accounted as lost */
1887  } else {
1888  /* duplicate or reordered packet */
1889  }
1890 
1891  /* Verify that we are on the same stream source (we do not support multiple sources) */
1892  if (ntohl(hdr->ssrc) != stats->ssrc || !stats->init) {
1893  rtcp_stats_init(rtp_session);
1894  }
1895 
1896  stats->period_pkt_count++;
1897  stats->pkt_count++;
1898 #ifdef DEBUG_RTCP
1899  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",
1900  stats->period_pkt_count, pkt_seq, stats->cycle, stats->ssrc, rtp_session->timer.samplecount);
1901 #endif
1902  /* Interarrival jitter calculation */
1903  pkt_tsdiff = abs((int)rtp_session->timer.samplecount - (int)ntohl(hdr->ts)); /* relative transit times for this packet */
1904  if (stats->pkt_count < 2) { /* Can not compute Jitter with only one packet */
1905  stats->last_pkt_tsdiff = pkt_tsdiff;
1906  } else {
1907  packet_spacing_diff = abs((int)pkt_tsdiff - (int)stats->last_pkt_tsdiff); /* Jitter : difference of relative transit times for the two packets */
1908  stats->last_pkt_tsdiff = pkt_tsdiff;
1909  /* Interarrival jitter estimation, "J(i) = J(i-1) + ( |D(i-1,i)| - J(i-1) )/16" */
1910  stats->inter_jitter = (stats->inter_jitter + (((double)packet_spacing_diff - stats->inter_jitter) /16.));
1911  }
1912 
1913 #ifdef DEBUG_RTCP
1914  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]",
1915  ntohl(hdr->ts), rtp_session->timer.samplecount, pkt_tsdiff, packet_spacing_diff, stats->inter_jitter, ntohs(hdr->seq), stats->ssrc);
1916 #endif
1917  return 1;
1918 }
#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:1816
switch_rtcp_numbers_t rtcp
Definition: switch_types.h:692
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:372
switch_rtp_stats_t stats
Definition: switch_rtp.c:428
switch_memory_pool_t * pool
Definition: switch_rtp.c:373
switch_socket_t * rtcp_sock_output
Definition: switch_rtp.c:309
int rtcp_interval
Definition: switch_rtp.c:435
switch_timer_t timer
Definition: switch_rtp.c:411
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:351
static void rtcp_stats_init ( switch_rtp_t rtp_session)
static

Definition at line 1816 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::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_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_rtcp_numbers_t::ssrc, switch_rtp::stats, SWITCH_CHANNEL_SESSION_LOG, switch_core_memory_pool_get_data(), SWITCH_LOG_DEBUG, switch_log_printf(), SWITCH_LOG_WARNING, SWITCH_RTP_FLAG_ENABLE_RTCP, SWITCH_RTP_FLAG_RTCP_PASSTHRU, and switch_rtp::timer.

Referenced by rtcp_stats(), and switch_rtp_reset().

1817 {
1818  switch_rtcp_numbers_t * stats = &rtp_session->stats.rtcp;
1819  srtp_hdr_t * hdr = &rtp_session->last_rtp_hdr;
1820  switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
1821  stats->ssrc = ntohl(hdr->ssrc);
1822  stats->last_rpt_ts = rtp_session->timer.samplecount;
1823  stats->init = 1;
1824  stats->last_rpt_ext_seq = 0;
1825  stats->last_rpt_cycle = 0;
1826  stats->last_pkt_tsdiff = 0;
1827  stats->inter_jitter = 0;
1828  stats->cycle = 0;
1829  stats->high_ext_seq_recv = ntohs((uint16_t)hdr->seq);
1830  stats->base_seq = ntohs((uint16_t)hdr->seq);
1831  stats->bad_seq = (1<<16) + 1; /* Make sure we wont missmatch 2 consecutive packets, so seq == bad_seq is false */
1832  stats->cum_lost = 0;
1833  stats->period_pkt_count = 0;
1834  stats->sent_pkt_count = 0;
1835  stats->pkt_count = 0;
1836  stats->rtcp_rtp_count = 0;
1837 
1838  if (!rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
1839  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "rtcp_stats_init: %s rtcp disabled\n", rtp_type(rtp_session));
1840  } else if (!rtp_session->rtcp_sock_output) {
1841  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "rtcp_stats_init: %s no rtcp socket\n", rtp_type(rtp_session));
1842  } else if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU]) {
1843  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "rtcp_stats_init: %s rtcp passthru\n", rtp_type(rtp_session));
1844  } else {
1845  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);
1846  }
1847 }
#define SWITCH_CHANNEL_SESSION_LOG(x)
void * switch_core_memory_pool_get_data(switch_memory_pool_t *pool, const char *key)
#define rtp_type(rtp_session)
Definition: switch_rtp.c:513
switch_rtcp_numbers_t rtcp
Definition: switch_types.h:692
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:372
switch_rtp_stats_t stats
Definition: switch_rtp.c:428
switch_memory_pool_t * pool
Definition: switch_rtp.c:373
switch_socket_t * rtcp_sock_output
Definition: switch_rtp.c:309
switch_timer_t timer
Definition: switch_rtp.c:411
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:351
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

Definition at line 6140 of file switch_rtp.c.

References switch_rtp_ice_t::addr, payload_map_s::allocated, switch_rtp::auto_adj_used, switch_rtp::autoadj_tally, switch_rtp::autoadj_threshold, switch_rtp::autoadj_window, switch_rtp::bad_stream, check_recv_payload(), check_rtcp_and_ice(), 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::flag_mutex, switch_rtp::flags, 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_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_read_ts, switch_rtp::last_rtp_hdr, switch_rtp::max_missed_packets, memset(), switch_rtp::missed_count, switch_rtp::ms_per_packet, payload_map_s::negotiated, payload_map_s::next, switch_rtp::one_second, switch_rtp_rfc2833_data::out_digit_dur, switch_rtp::pause_jb, switch_rtp_numbers_t::period_packet_count, switch_rtp::pmaps, process_rtcp_packet(), payload_map_s::pt, 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_ACCEPT_ANY_PAYLOAD, 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::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_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_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_MUX, SWITCH_RTP_FLAG_RTCP_PASSTHRU, SWITCH_RTP_FLAG_SECURE_SEND, SWITCH_RTP_FLAG_STICKY_FLUSH, 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().

6142 {
6143 
6144  switch_channel_t *channel = NULL;
6145  switch_size_t bytes = 0;
6146  switch_size_t rtcp_bytes = 0;
6148  switch_status_t rtcp_status = SWITCH_STATUS_SUCCESS, rtcp_poll_status = SWITCH_STATUS_SUCCESS;
6149  int check = 0;
6150  int ret = -1;
6151  int sleep_mss = 1000;
6152  int poll_sec = 5;
6153  int poll_loop = 0;
6154  int fdr = 0;
6155  int rtcp_fdr = 0;
6156  int hot_socket = 0;
6157  int read_loops = 0;
6158  int slept = 0;
6159 
6160  if (!switch_rtp_ready(rtp_session)) {
6161  return -1;
6162  }
6163 
6164  if (rtp_session->session) {
6165  channel = switch_core_session_get_channel(rtp_session->session);
6166  }
6167 
6168  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
6169  sleep_mss = rtp_session->timer.interval * 1000;
6170  }
6171 
6172  READ_INC(rtp_session);
6173 
6174 
6175 
6176  while (switch_rtp_ready(rtp_session)) {
6177  int do_cng = 0;
6178  int read_pretriggered = 0;
6179  int has_rtcp = 0;
6180  int got_rtp_poll = 0;
6181 
6182  bytes = 0;
6183 
6184  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] &&
6185  !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] &&
6186  !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] &&
6187  !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] &&
6188  rtp_session->read_pollfd) {
6189 
6190  if (rtp_session->jb && !rtp_session->pause_jb && jb_valid(rtp_session)) {
6191  while (switch_poll(rtp_session->read_pollfd, 1, &fdr, 0) == SWITCH_STATUS_SUCCESS) {
6192  status = read_rtp_packet(rtp_session, &bytes, flags, SWITCH_STATUS_SUCCESS, SWITCH_FALSE);
6193 
6194  if (status == SWITCH_STATUS_GENERR) {
6195  ret = -1;
6196  goto end;
6197  }
6198 
6199  if ((*flags & SFF_RTCP)) {
6200  *flags &= ~SFF_RTCP;
6201  has_rtcp = 1;
6202  read_pretriggered = 0;
6203  goto rtcp;
6204  }
6205 
6206  if (status == SWITCH_STATUS_BREAK) {
6207  read_pretriggered = 1;
6208  break;
6209  }
6210  }
6211 
6212  } else if ((rtp_session->flags[SWITCH_RTP_FLAG_AUTOFLUSH] || rtp_session->flags[SWITCH_RTP_FLAG_STICKY_FLUSH])) {
6213 
6214  if (switch_poll(rtp_session->read_pollfd, 1, &fdr, 0) == SWITCH_STATUS_SUCCESS) {
6215  status = read_rtp_packet(rtp_session, &bytes, flags, SWITCH_STATUS_SUCCESS, SWITCH_FALSE);
6216  if (status == SWITCH_STATUS_GENERR) {
6217  ret = -1;
6218  goto end;
6219  }
6220  if ((*flags & SFF_RTCP)) {
6221  *flags &= ~SFF_RTCP;
6222  has_rtcp = 1;
6223  read_pretriggered = 0;
6224  goto rtcp;
6225  }
6226 
6227  /* switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Initial (%i) %d\n", status, bytes); */
6228  if (status != SWITCH_STATUS_FALSE) {
6229  read_pretriggered = 1;
6230  }
6231 
6232  if (bytes) {
6233  if (switch_poll(rtp_session->read_pollfd, 1, &fdr, 0) == SWITCH_STATUS_SUCCESS) {
6234  rtp_session->hot_hits++;//+= rtp_session->samples_per_interval;
6235 
6236  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG10, "%s Hot Hit %d\n",
6237  rtp_session_name(rtp_session),
6238  rtp_session->hot_hits);
6239  } else {
6240  rtp_session->hot_hits = 0;
6241  }
6242  }
6243 
6244  if (rtp_session->hot_hits > 1 && !rtp_session->sync_packets) {// >= (rtp_session->samples_per_second * 30)) {
6245  hot_socket = 1;
6246  }
6247  } else {
6248  rtp_session->hot_hits = 0;
6249  }
6250  }
6251 
6252  if (hot_socket && (rtp_session->hot_hits % 10) != 0) {
6253  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG10, "%s timer while HOT\n", rtp_session_name(rtp_session));
6254  switch_core_timer_next(&rtp_session->timer);
6255  } else if (hot_socket) {
6256  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG10, "%s skip timer once\n", rtp_session_name(rtp_session));
6257  rtp_session->sync_packets++;
6258  switch_core_timer_sync(&rtp_session->timer);
6259  reset_jitter_seq(rtp_session);
6260  } else {
6261 
6262  if (rtp_session->sync_packets) {
6263 
6265  "%s Auto-Flush catching up %d packets (%d)ms.\n",
6266  rtp_session_name(rtp_session),
6267  rtp_session->sync_packets, (rtp_session->ms_per_packet * rtp_session->sync_packets) / 1000);
6268  if (!rtp_session->flags[SWITCH_RTP_FLAG_PAUSE]) {
6269  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG3, "%s syncing %d %s packet(s)\n",
6270  rtp_session_name(rtp_session),
6271  rtp_session->sync_packets, rtp_type(rtp_session));
6272 
6273  rtp_session->bad_stream++;
6274  rtp_session->stats.inbound.flaws += rtp_session->sync_packets;
6275  }
6276 
6277  switch_core_timer_sync(&rtp_session->timer);
6278  reset_jitter_seq(rtp_session);
6279  rtp_session->hot_hits = 0;
6280  } else {
6281  if (slept) {
6282  switch_cond_next();
6283  } else {
6284  switch_core_timer_next(&rtp_session->timer);
6285  slept++;
6286  }
6287 
6288  }
6289 
6290  rtp_session->sync_packets = 0;
6291  }
6292  }
6293 
6294  rtp_session->stats.read_count++;
6295 
6296  recvfrom:
6297 
6298  if (!read_pretriggered) {
6299  bytes = 0;
6300  }
6301  read_loops++;
6302  //poll_loop = 0;
6303 
6304  if (!switch_rtp_ready(rtp_session)) {
6305  break;
6306  }
6307 
6308  if (!rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && rtp_session->read_pollfd) {
6309  int pt = poll_sec * 1000000;
6310 
6311  do_2833(rtp_session);
6312 
6313  if (rtp_session->dtmf_data.out_digit_dur > 0 || rtp_session->dtmf_data.in_digit_sanity || rtp_session->sending_dtmf ||
6315  pt = 20000;
6316  }
6317 
6318  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) {
6319  pt = 200000;
6320  }
6321 
6322  if (rtp_session->vb) {
6323  if (switch_jb_poll(rtp_session->vb)) {
6324  pt = 0;
6325  }
6326  }
6327 
6328  if ((io_flags & SWITCH_IO_FLAG_NOBLOCK)) {
6329  pt = 0;
6330  }
6331 
6332  poll_status = switch_poll(rtp_session->read_pollfd, 1, &fdr, pt);
6333 
6334 
6335  //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
6336  // switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "WTF Poll %d\n", poll_status);
6337  //}
6338 
6339  if (!rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->dtmf_data.out_digit_dur > 0) {
6340  return_cng_frame();
6341  }
6342 
6343  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && rtp_session->flags[SWITCH_RTP_FLAG_BREAK]) {
6345  bytes = 0;
6346  reset_jitter_seq(rtp_session);
6347  return_cng_frame();
6348  }
6349 
6350  }
6351 
6352 
6353  if (poll_status == SWITCH_STATUS_SUCCESS || (rtp_session->vb && switch_jb_poll(rtp_session->vb))) {
6354 
6355  got_rtp_poll = 1;
6356 
6357  if (read_pretriggered) {
6358  read_pretriggered = 0;
6359  } else {
6360 
6361  status = read_rtp_packet(rtp_session, &bytes, flags, poll_status, SWITCH_TRUE);
6362 
6363  if (status == SWITCH_STATUS_GENERR) {
6364  ret = -1;
6365  goto end;
6366  }
6367 
6368  if (rtp_session->max_missed_packets && read_loops == 1 && !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] &&
6369  !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
6370  if (bytes && status == SWITCH_STATUS_SUCCESS) {
6371  rtp_session->missed_count = 0;
6372  } else if (++rtp_session->missed_count >= rtp_session->max_missed_packets) {
6373  ret = -2;
6374  goto end;
6375  }
6376  }
6377 
6378  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
6379  //switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_CRIT, "Read bytes (%i) %ld\n", status, bytes);
6380 
6381  if (bytes == 0) {
6382  if (check_rtcp_and_ice(rtp_session) == -1) {
6383  ret = -1;
6384  goto end;
6385  }
6386  // This is dumb
6387  //switch_rtp_video_refresh(rtp_session);
6388  goto rtcp;
6389  }
6390  }
6391 
6392  if ((*flags & SFF_PROXY_PACKET)) {
6393  ret = (int) bytes;
6394  goto end;
6395  }
6396 
6397  if ((*flags & SFF_RTCP)) {
6398  *flags &= ~SFF_RTCP;
6399  has_rtcp = 1;
6400  goto rtcp;
6401  }
6402 
6403 
6404  }
6405  poll_loop = 0;
6406  } else {
6407 
6408  if (!switch_rtp_ready(rtp_session)) {
6409  ret = -1;
6410  goto end;
6411  }
6412 
6413  if (!SWITCH_STATUS_IS_BREAK(poll_status) && poll_status != SWITCH_STATUS_TIMEOUT) {
6414  char tmp[128] = "";
6415  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Poll failed with error: %d [%s]\n",
6416  poll_status, switch_strerror_r(poll_status, tmp, sizeof(tmp)));
6417  ret = -1;
6418  goto end;
6419  }
6420 
6421  if (!rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] && !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
6422  rtp_session->missed_count += (poll_sec * 1000) / (rtp_session->ms_per_packet ? rtp_session->ms_per_packet / 1000 : 20);
6423  bytes = 0;
6424 
6425  if (rtp_session->max_missed_packets) {
6426  if (rtp_session->missed_count >= rtp_session->max_missed_packets) {
6427  ret = -2;
6428  goto end;
6429  }
6430  }
6431  }
6432 
6433 
6434  if (check_rtcp_and_ice(rtp_session) == -1) {
6435  ret = -1;
6436  goto end;
6437  }
6438 
6439 
6440  if ((!(io_flags & SWITCH_IO_FLAG_NOBLOCK)) &&
6441  (rtp_session->dtmf_data.out_digit_dur == 0) && !rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
6442  return_cng_frame();
6443  }
6444  }
6445 
6446  rtcp:
6447 
6448  if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
6449  rtcp_poll_status = SWITCH_STATUS_FALSE;
6450 
6451  if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX] && has_rtcp) {
6452  if (rtp_session->rtcp_recv_msg_p->header.version == 2) { //rtcp muxed
6453  rtp_session->rtcp_from_addr = rtp_session->from_addr;
6454  rtcp_status = rtcp_poll_status = SWITCH_STATUS_SUCCESS;
6455  rtcp_bytes = bytes;
6456  }
6457 
6458  has_rtcp = 0;
6459 
6460  } else if (rtp_session->rtcp_read_pollfd) {
6461  rtcp_poll_status = switch_poll(rtp_session->rtcp_read_pollfd, 1, &rtcp_fdr, 0);
6462  }
6463 
6464  if (rtcp_poll_status == SWITCH_STATUS_SUCCESS) {
6465 
6466  if (!rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
6467  rtcp_status = read_rtcp_packet(rtp_session, &rtcp_bytes, flags);
6468  }
6469 
6470  if (rtcp_status == SWITCH_STATUS_SUCCESS) {
6471  switch_rtp_reset_media_timer(rtp_session);
6472 
6473  if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU]) {
6474  switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
6475  const char *uuid = switch_channel_get_partner_uuid(channel);
6476 
6477  if (uuid) {
6478  switch_core_session_t *other_session;
6479  switch_rtp_t *other_rtp_session = NULL;
6480 
6481  if ((other_session = switch_core_session_locate(uuid))) {
6482  switch_channel_t *other_channel = switch_core_session_get_channel(other_session);
6483  if ((other_rtp_session = switch_channel_get_private(other_channel, "__rtcp_audio_rtp_session")) &&
6484  other_rtp_session->rtcp_sock_output &&
6485  switch_rtp_test_flag(other_rtp_session, SWITCH_RTP_FLAG_ENABLE_RTCP)) {
6486  other_rtp_session->rtcp_send_msg = rtp_session->rtcp_recv_msg;
6487 
6488 #ifdef ENABLE_SRTP
6489  if (switch_rtp_test_flag(other_rtp_session, SWITCH_RTP_FLAG_SECURE_SEND)) {
6490  int sbytes = (int) rtcp_bytes;
6491  int stat = srtp_protect_rtcp(other_rtp_session->send_ctx[rtp_session->srtp_idx_rtcp], &other_rtp_session->rtcp_send_msg.header, &sbytes);
6492  if (stat) {
6493  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error: SRTP RTCP protection failed with code %d\n", stat);
6494  }
6495  rtcp_bytes = sbytes;
6496 
6497  }
6498 #endif
6499 
6500 #ifdef ENABLE_ZRTP
6501  /* ZRTP Send */
6502  if (zrtp_on && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) {
6503  unsigned int sbytes = (unsigned int) bytes;
6504  zrtp_status_t stat = zrtp_status_fail;
6505 
6506  stat = zrtp_process_rtcp(other_rtp_session->zrtp_stream, (void *) &other_rtp_session->rtcp_send_msg, &sbytes);
6507 
6508  switch (stat) {
6509  case zrtp_status_ok:
6510  break;
6511  case zrtp_status_drop:
6512  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error: zRTP protection drop with code %d\n", stat);
6513  ret = (int) bytes;
6514  goto end;
6515  break;
6516  case zrtp_status_fail:
6517  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error: zRTP protection fail with code %d\n", stat);
6518  break;
6519  default:
6520  break;
6521  }
6522 
6523  bytes = sbytes;
6524  }
6525 #endif
6526  if (switch_socket_sendto(other_rtp_session->rtcp_sock_output, other_rtp_session->rtcp_remote_addr, 0,
6527  (const char*)&other_rtp_session->rtcp_send_msg, &rtcp_bytes ) != SWITCH_STATUS_SUCCESS) {
6528  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG,"RTCP packet not written\n");
6529  }
6530 
6531 
6532  }
6533  switch_core_session_rwunlock(other_session);
6534  }
6535  }
6536 
6537  }
6538 
6539  if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
6540  process_rtcp_packet(rtp_session, &rtcp_bytes);
6541  ret = 1;
6542 
6543  if (!rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && rtp_session->timer.interval) {
6544  switch_core_timer_sync(&rtp_session->timer);
6545  reset_jitter_seq(rtp_session);
6546  }
6547  goto recvfrom;
6548  }
6549  }
6550  }
6551  }
6552 
6553  if ((!(io_flags & SWITCH_IO_FLAG_NOBLOCK)) &&
6554  (rtp_session->dtmf_data.out_digit_dur == 0) && !got_rtp_poll) {
6555  return_cng_frame();
6556  }
6557 
6558 
6559  if (bytes && rtp_session->has_rtp &&
6560  !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] &&
6561  rtp_session->last_rtp_hdr.pt != 13 &&
6562  rtp_session->last_rtp_hdr.pt != rtp_session->recv_te &&
6563  rtp_session->last_rtp_hdr.pt != rtp_session->cng_pt) {
6564  int accept_packet = 1;
6565 
6566 
6567  if (rtp_session->pmaps && *rtp_session->pmaps) {
6568  payload_map_t *pmap;
6569  accept_packet = 0;
6570 
6571  switch_mutex_lock(rtp_session->flag_mutex);
6572  for (pmap = *rtp_session->pmaps; pmap && pmap->allocated; pmap = pmap->next) {
6573 
6574  if (!pmap->negotiated) {
6575  continue;
6576  }
6577 
6578  if (rtp_session->last_rtp_hdr.pt == pmap->pt) {
6579  accept_packet = 1;
6580  if (pmapP) {
6581  *pmapP = pmap;
6582  }
6583  break;
6584  }
6585  }
6586  switch_mutex_unlock(rtp_session->flag_mutex);
6587  }
6588 
6589  if (!accept_packet &&
6590  !(rtp_session->rtp_bugs & RTP_BUG_ACCEPT_ANY_PAYLOAD) && !(rtp_session->rtp_bugs & RTP_BUG_ACCEPT_ANY_PACKETS)) {
6591  /* drop frames of incorrect payload number and return CNG frame instead */
6592 
6593  return_cng_frame();
6594  }
6595  }
6596 
6597  if (!bytes && (io_flags & SWITCH_IO_FLAG_NOBLOCK)) {
6598  rtp_session->missed_count = 0;
6599  ret = 0;
6600  goto end;
6601  }
6602 
6603  check = !bytes;
6604 
6605  if (rtp_session->flags[SWITCH_RTP_FLAG_FLUSH]) {
6606  bytes = do_flush(rtp_session, SWITCH_FALSE, bytes);
6608  }
6609 
6610  if (rtp_session->flags[SWITCH_RTP_FLAG_BREAK] || (bytes && bytes == 4 && *((int *) &rtp_session->recv_msg) == UINT_MAX)) {
6612 
6613  if (!rtp_session->flags[SWITCH_RTP_FLAG_NOBLOCK] || !rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] ||
6614  rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] ||
6615  (bytes && bytes < 5) || (!bytes && poll_loop)) {
6616  bytes = 0;
6617  reset_jitter_seq(rtp_session);
6618  return_cng_frame();
6619  }
6620  }
6621 
6622  if (bytes && bytes < 5) {
6623  continue;
6624  }
6625 
6626  if (!bytes && poll_loop) {
6627  goto recvfrom;
6628  }
6629 
6630  if (bytes && rtp_session->last_rtp_hdr.m && rtp_session->last_rtp_hdr.pt != rtp_session->recv_te &&
6631  !rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] &&
6632  !(rtp_session->rtp_bugs & RTP_BUG_IGNORE_MARK_BIT)) {
6634  }
6635 
6636 
6637  if (rtp_session->last_rtp_hdr.pt == rtp_session->cng_pt || rtp_session->last_rtp_hdr.pt == 13) {
6638  *flags |= SFF_NOT_AUDIO;
6639  } else {
6640  *flags &= ~SFF_NOT_AUDIO; /* If this flag was already set, make sure to remove it when we get real audio */
6641  }
6642 
6643  /* ignore packets not meant for us unless the auto-adjust window is open (ice mode has its own alternatives to this) */
6644  if (!using_ice(rtp_session) && bytes) {
6645  if (rtp_session->flags[SWITCH_RTP_FLAG_AUTOADJ]) {
6646  if (rtp_session->last_rtp_hdr.pt == rtp_session->cng_pt || rtp_session->last_rtp_hdr.pt == 13) {
6647  goto recvfrom;
6648 
6649  }
6650  } else if (!(rtp_session->rtp_bugs & RTP_BUG_ACCEPT_ANY_PACKETS) && !switch_cmp_addr(rtp_session->rtp_from_addr, rtp_session->remote_addr)) {
6651  goto recvfrom;
6652  }
6653  }
6654 
6655  if (bytes && rtp_session->flags[SWITCH_RTP_FLAG_AUTOADJ] && switch_sockaddr_get_port(rtp_session->rtp_from_addr)) {
6656  if (!switch_cmp_addr(rtp_session->rtp_from_addr, rtp_session->remote_addr)) {
6657  if (++rtp_session->autoadj_tally >= rtp_session->autoadj_threshold) {
6658  const char *err;
6659  uint32_t old = rtp_session->remote_port;
6660  const char *tx_host;
6661  const char *old_host;
6662  char bufa[50], bufb[50];
6663  char adj_port[6];
6664 
6665  tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtp_from_addr);
6666  old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
6667 
6669  "Auto Changing %s port from %s:%u to %s:%u\n", rtp_type(rtp_session), old_host, old, tx_host,
6670  switch_sockaddr_get_port(rtp_session->rtp_from_addr));
6671 
6672  if (channel) {
6673  char varname[80] = "";
6674 
6675  switch_snprintf(varname, sizeof(varname), "remote_%s_ip_reported", rtp_type(rtp_session));
6676  switch_channel_set_variable(channel, varname, switch_channel_get_variable(channel, "remote_media_ip"));
6677 
6678  switch_snprintf(varname, sizeof(varname), "remote_%s_ip", rtp_type(rtp_session));
6679  switch_channel_set_variable(channel, varname, tx_host);
6680 
6681  switch_snprintf(varname, sizeof(varname), "remote_%s_port_reported", rtp_type(rtp_session));
6682  switch_snprintf(adj_port, sizeof(adj_port), "%u", switch_sockaddr_get_port(rtp_session->rtp_from_addr));
6683  switch_channel_set_variable(channel, varname, switch_channel_get_variable(channel, "remote_media_port"));
6684 
6685  switch_snprintf(varname, sizeof(varname), "remote_%s_port", rtp_type(rtp_session));
6686  switch_channel_set_variable(channel, varname, adj_port);
6687 
6688  switch_snprintf(varname, sizeof(varname), "rtp_auto_adjust_%s", rtp_type(rtp_session));
6689  switch_channel_set_variable(channel, varname, "true");
6690  }
6691  rtp_session->auto_adj_used = 1;
6692  switch_rtp_set_remote_address(rtp_session, tx_host, switch_sockaddr_get_port(rtp_session->rtp_from_addr), 0, SWITCH_FALSE, &err);
6693  if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) {
6695  } else {
6697  }
6698  if (rtp_session->ice.ice_user) {
6699  rtp_session->ice.addr = rtp_session->remote_addr;
6700  }
6701  }
6702  } else {
6703  if ((rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST)) {
6705  } else {
6706  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Correct %s ip/port confirmed.\n", rtp_type(rtp_session));
6708  }
6709  rtp_session->auto_adj_used = 0;
6710  }
6711  }
6712 
6713  if (bytes && !(rtp_session->rtp_bugs & RTP_BUG_ALWAYS_AUTO_ADJUST) && rtp_session->autoadj_window) {
6714  if (--rtp_session->autoadj_window == 0) {
6716  }
6717  }
6718 
6719  if (bytes && (rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] || rtp_session->flags[SWITCH_RTP_FLAG_UDPTL])) {
6720  /* Fast PASS! */
6721  *flags |= SFF_PROXY_PACKET;
6722 
6723  if (rtp_session->flags[SWITCH_RTP_FLAG_UDPTL]) {
6724 #if 0
6725  if (rtp_session->has_rtp && check_recv_payload(rtp_session)) {
6727  "Ignoring udptl packet of size of %ld bytes that looks strikingly like a RTP packet.\n", (long)bytes);
6728  bytes = 0;
6729  goto do_continue;
6730  }
6731 #endif
6732  *flags |= SFF_UDPTL_PACKET;
6733  }
6734 
6735  ret = (int) bytes;
6736  goto end;
6737  }
6738 
6739  if (bytes) {
6740  rtp_session->missed_count = 0;
6741 
6742  if (bytes < rtp_header_len) {
6743  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_WARNING, "Ignoring invalid RTP packet size of %ld bytes.\n", (long)bytes);
6744  bytes = 0;
6745  goto do_continue;
6746  }
6747 
6748  if (rtp_session->last_rtp_hdr.pt && (rtp_session->last_rtp_hdr.pt == rtp_session->cng_pt || rtp_session->last_rtp_hdr.pt == 13)) {
6749  return_cng_frame();
6750  }
6751  }
6752 
6753  if (check || bytes) {
6754  do_2833(rtp_session);
6755  }
6756 
6757  if (bytes && rtp_session->recv_msg.header.version != 2) {
6758  uint8_t *data = (uint8_t *) RTP_BODY(rtp_session);
6759 
6760  //if (rtp_session->recv_msg.header.version == 0) {
6761  // if (rtp_session->ice.ice_user) {
6762  // handle_ice(rtp_session, &rtp_session->ice, (void *) &rtp_session->recv_msg, bytes);
6763  // goto recvfrom;
6764  // }
6765  //}
6766 
6767  if (rtp_session->invalid_handler) {
6768  rtp_session->invalid_handler(rtp_session, rtp_session->sock_input, (void *) &rtp_session->recv_msg, bytes, rtp_session->rtp_from_addr);
6769  }
6770 
6771  memset(data, 0, 2);
6772  data[0] = 65;
6773 
6774  rtp_session->last_rtp_hdr.pt = rtp_session->cng_pt != INVALID_PT ? rtp_session->cng_pt : SWITCH_RTP_CNG_PAYLOAD;
6775  *flags |= SFF_CNG;
6776  *payload_type = (switch_payload_t) rtp_session->last_rtp_hdr.pt;
6777  ret = 2 + rtp_header_len;
6778  goto end;
6779  } else if (bytes) {
6780  rtp_session->stats.inbound.period_packet_count++;
6781  }
6782 
6783 
6784  /* Handle incoming RFC2833 packets */
6785  switch (handle_rfc2833(rtp_session, bytes, &do_cng)) {
6786  case RESULT_GOTO_END:
6787  goto end;
6788  case RESULT_GOTO_RECVFROM:
6789  goto recvfrom;
6791  goto timer_check;
6792  case RESULT_CONTINUE:
6793  status = SWITCH_STATUS_SUCCESS;
6794  goto result_continue;
6795  }
6796 
6797  result_continue:
6798  timer_check:
6799 
6800  if (rtp_session->flags[SWITCH_RTP_FLAG_MUTE]) {
6801  do_cng++;
6802  }
6803 
6804  if (do_cng) {
6805  uint8_t *data = (uint8_t *) RTP_BODY(rtp_session);
6806 
6807  do_2833(rtp_session);
6808 
6809  if (rtp_session->last_cng_ts == rtp_session->last_read_ts + rtp_session->samples_per_interval) {
6810  rtp_session->last_cng_ts = 0;
6811  } else {
6812  rtp_session->last_cng_ts = rtp_session->last_read_ts + rtp_session->samples_per_interval;
6813  }
6814 
6815  memset(data, 0, 2);
6816  data[0] = 65;
6817  rtp_session->last_rtp_hdr.pt = rtp_session->cng_pt != INVALID_PT ? rtp_session->cng_pt : SWITCH_RTP_CNG_PAYLOAD;
6818  *flags |= SFF_CNG;
6819  *payload_type = (switch_payload_t) rtp_session->last_rtp_hdr.pt;
6820  ret = 2 + rtp_header_len;
6821  rtp_session->stats.inbound.skip_packet_count++;
6822  goto end;
6823  }
6824 
6825 
6826  if (check || (bytes && !rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER])) {
6827  if (!bytes && rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) { /* We're late! We're Late! */
6828  if (!rtp_session->flags[SWITCH_RTP_FLAG_NOBLOCK] && status == SWITCH_STATUS_BREAK) {
6829  switch_cond_next();
6830  continue;
6831  }
6832 
6833 
6834 
6835  if (!rtp_session->flags[SWITCH_RTP_FLAG_PAUSE] && !rtp_session->flags[SWITCH_RTP_FLAG_DTMF_ON] && !rtp_session->dtmf_data.in_digit_ts
6836  && rtp_session->cng_count > (rtp_session->one_second * 2) && rtp_session->jitter_lead > JITTER_LEAD_FRAMES) {
6837 
6838  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "%s %s timeout\n",
6839  rtp_session_name(rtp_session), rtp_type(rtp_session));
6840 
6841  rtp_session->stats.inbound.flaws++;
6842  do_mos(rtp_session, SWITCH_FALSE);
6843  }
6844 
6845  rtp_session->cng_count++;
6846  return_cng_frame();
6847  }
6848  }
6849 
6850  rtp_session->cng_count = 0;
6851 
6852  if (status == SWITCH_STATUS_BREAK || bytes == 0) {
6853  if (!(io_flags & SWITCH_IO_FLAG_SINGLE_READ) && rtp_session->flags[SWITCH_RTP_FLAG_DATAWAIT]) {
6854  goto do_continue;
6855  }
6856  return_cng_frame();
6857  }
6858 
6859  if (rtp_session->flags[SWITCH_RTP_FLAG_GOOGLEHACK] && rtp_session->last_rtp_hdr.pt == 102) {
6860  rtp_session->last_rtp_hdr.pt = 97;
6861  }
6862 
6863  break;
6864 
6865  do_continue:
6866 
6867  if (!bytes && !rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
6868  switch_yield(sleep_mss);
6869  }
6870 
6871  }
6872 
6873  if (switch_rtp_ready(rtp_session)) {
6874  *payload_type = (switch_payload_t) rtp_session->last_rtp_hdr.pt;
6875 
6876  if (*payload_type == SWITCH_RTP_CNG_PAYLOAD) {
6877  *flags |= SFF_CNG;
6878  }
6879 
6880  ret = (int) bytes;
6881  } else {
6882  ret = -1;
6883  }
6884 
6885  end:
6886 
6887  READ_DEC(rtp_session);
6888 
6889  return ret;
6890 }
switch_pollfd_t * read_pollfd
Definition: switch_rtp.c:310
unsigned int switch_queue_size(switch_queue_t *queue)
Definition: switch_apr.c:1114
switch_size_t flaws
Definition: switch_types.h:657
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:6039
static switch_size_t do_flush(switch_rtp_t *rtp_session, int force, switch_size_t bytes_in)
Definition: switch_rtp.c:4983
uint8_t allocated
switch_size_t period_packet_count
Definition: switch_types.h:630
switch_rtp_invalid_handler_t invalid_handler
Definition: switch_rtp.c:360
#define SWITCH_CHANNEL_SESSION_LOG(x)
uint16_t switch_sockaddr_get_port(switch_sockaddr_t *sa)
Definition: switch_apr.c:916
switch_rtp_numbers_t inbound
Definition: switch_types.h:690
uint8_t switch_rtp_ready(switch_rtp_t *rtp_session)
Test if an RTP session is ready.
Definition: switch_rtp.c:4452
uint32_t max_missed_packets
Definition: switch_rtp.c:417
switch_sockaddr_t * rtcp_from_addr
Definition: switch_rtp.c:374
const char * switch_channel_get_partner_uuid(switch_channel_t *channel)
#define SWITCH_STATUS_IS_BREAK(x)
Definition: switch_utils.h:564
switch_sockaddr_t * remote_addr
Definition: switch_rtp.c:326
switch_mutex_t * flag_mutex
Definition: switch_rtp.c:407
switch_sockaddr_t * addr
Definition: switch_rtp.c:244
switch_status_t switch_poll(switch_pollfd_t *aprset, int32_t numsock, int32_t *nsds, switch_interval_time_t timeout)
Definition: switch_apr.c:1015
switch_socket_t * sock_input
Definition: switch_rtp.c:309
switch_port_t remote_port
Definition: switch_rtp.c:398
int srtp_idx_rtcp
Definition: switch_rtp.c:346
uint8_t has_rtp
Definition: switch_rtp.c:450
#define rtp_session_name(_rtp_session)
Definition: switch_rtp.c:79
switch_rtcp_hdr_t header
Definition: switch_rtp.c:180
uint32_t cng_count
Definition: switch_rtp.c:426
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:61
struct switch_rtp_rfc2833_data dtmf_data
Definition: switch_rtp.c:403
struct payload_map_s * next
void rtp_flush_read_buffer(switch_rtp_t *rtp_session, switch_rtp_flush_t flush)
Definition: switch_rtp.c:4940
rtcp_msg_t rtcp_recv_msg
Definition: switch_rtp.c:328
int switch_cmp_addr(switch_sockaddr_t *sa1, switch_sockaddr_t *sa2)
#define rtp_type(rtp_session)
Definition: switch_rtp.c:513
void switch_rtp_reset_media_timer(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:2817
switch_payload_t cng_pt
Definition: switch_rtp.c:406
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
_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:335
if((uint32_t)(unpack->cur-unpack->buf) > unpack->buflen)
switch_queue_t * dtmf_inqueue
Definition: switch_rtp.c:233
#define switch_yield(ms)
Wait a desired number of microseconds and yield the CPU.
Definition: switch_utils.h:908
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:773
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
#define JITTER_LEAD_FRAMES
Definition: switch_rtp.c:59
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:372
#define switch_channel_get_variable(_c, _v)
switch_sockaddr_t * rtp_from_addr
Definition: switch_rtp.c:374
static switch_status_t read_rtp_packet(switch_rtp_t *rtp_session, switch_size_t *bytes, switch_frame_flag_t *flags, switch_status_t poll_status, switch_bool_t return_jb_packet)
Definition: switch_rtp.c:5133
#define READ_INC(rtp_session)
Definition: switch_rtp.c:60
uint32_t autoadj_threshold
Definition: switch_rtp.c:332
switch_byte_t auto_adj_used
Definition: switch_rtp.c:440
int switch_jb_poll(switch_jb_t *jb)
uint32_t last_read_ts
Definition: switch_rtp.c:364
uint32_t autoadj_window
Definition: switch_rtp.c:331
rtcp_msg_t rtcp_send_msg
Definition: switch_rtp.c:315
char * switch_strerror_r(int errnum, char *buf, switch_size_t buflen)
uintptr_t switch_size_t
uint8_t negotiated
switch_payload_t pt
void switch_cond_next(void)
Definition: switch_time.c:638
switch_jb_t * vb
Definition: switch_rtp.c:415
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:2913
uint32_t sync_packets
Definition: switch_rtp.c:434
switch_rtp_stats_t stats
Definition: switch_rtp.c:428
void switch_core_session_rwunlock(_In_ switch_core_session_t *session)
Unlock a read or write lock on as given session.
switch_size_t skip_packet_count
Definition: switch_types.h:632
static switch_status_t timer_check(switch_timer_t *timer, switch_bool_t step)
Definition: switch_time.c:884
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:309
#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:4747
payload_map_t ** pmaps
Definition: switch_rtp.c:447
void switch_rtp_set_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Set an RTP Flag.
Definition: switch_rtp.c:4702
switch_jb_t * jb
Definition: switch_rtp.c:414
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:418
static int using_ice(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:1946
uint32_t samples_per_interval
Definition: switch_rtp.c:386
srtp_hdr_t header
Definition: switch_rtp.c:112
switch_timer_t timer
Definition: switch_rtp.c:411
rtp_msg_t recv_msg
Definition: switch_rtp.c:327
#define switch_core_session_locate(uuid_str)
Locate a session based on it's uuid.
Definition: switch_core.h:916
static void reset_jitter_seq(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:1595
switch_core_session_t * session
Definition: switch_rtp.c:446
static int check_rtcp_and_ice(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:1956
uint8_t pause_jb
Definition: switch_rtp.c:441
switch_rtp_ice_t ice
Definition: switch_rtp.c:377
int8_t sending_dtmf
Definition: switch_rtp.c:357
switch_rtp_bug_flag_t rtp_bugs
Definition: switch_rtp.c:427
static void do_2833(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:4784
uint32_t ms_per_packet
Definition: switch_rtp.c:391
static int jb_valid(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:4967
switch_payload_t recv_te
Definition: switch_rtp.c:405
switch_queue_t * dtmf_queue
Definition: switch_rtp.c:217
uint32_t hot_hits
Definition: switch_rtp.c:433
uint32_t jitter_lead
Definition: switch_rtp.c:394
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:222
uint32_t switch_rtp_test_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flags)
Test an RTP Flag.
Definition: switch_rtp.c:4742
switch_pollfd_t * rtcp_read_pollfd
Definition: switch_rtp.c:310
uint32_t bad_stream
Definition: switch_rtp.c:430
static handle_rfc2833_result_t handle_rfc2833(switch_rtp_t *rtp_session, switch_size_t bytes, int *do_cng)
Definition: switch_rtp.c:539
const char * switch_get_addr(char *buf, switch_size_t len, switch_sockaddr_t *in)
Definition: switch_apr.c:894
uint32_t last_cng_ts
Definition: switch_rtp.c:365
#define SWITCH_RTP_CNG_PAYLOAD
Definition: switch_types.h:702
#define return_cng_frame()
Definition: switch_rtp.c:5131
static switch_status_t process_rtcp_packet(switch_rtp_t *rtp_session, switch_size_t *bytes)
Definition: switch_rtp.c:5984
switch_sockaddr_t * from_addr
Definition: switch_rtp.c:374
static const switch_payload_t INVALID_PT
Definition: switch_rtp.c:75
#define switch_channel_set_variable(_channel, _var, _val)
switch_sockaddr_t * rtcp_remote_addr
Definition: switch_rtp.c:326
rtp_hdr_t last_rtp_hdr
Definition: switch_rtp.c:351
static int check_recv_payload(switch_rtp_t *rtp_session)
Definition: switch_rtp.c:5085
memset(buf, 0, buflen)
uint32_t autoadj_tally
Definition: switch_rtp.c:333
#define rtp_header_len
Definition: switch_rtp.c:66
rtcp_msg_t * rtcp_recv_msg_p
Definition: switch_rtp.c:329
uint32_t one_second
Definition: switch_rtp.c:392
uint8_t switch_payload_t
static void do_mos(switch_rtp_t *rtp_session, int force)
Definition: switch_rtp.c:1521
switch_status_t switch_core_timer_sync(switch_timer_t *timer)
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 7183 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, 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_ct, 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_codec::implementation, INVALID_PT, ts_normalize_s::last_frame, switch_rtp::last_rtp_hdr, ts_normalize_s::last_ssrc, switch_rtp::last_write_samplecount, 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::one_second, 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_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_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_LOG, SWITCH_CHANNEL_SESSION_LOG, SWITCH_CHANNEL_SESSION_LOG_CLEAN, switch_clear_flag, switch_core_codec_decode(), switch_core_session_get_channel(), switch_core_timer_sync(), 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_ERROR, SWITCH_LOG_INFO, switch_log_printf(), switch_micro_time_now(), 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_GEN_TS_DELTA, SWITCH_RTP_FLAG_GOOGLEHACK, SWITCH_RTP_FLAG_NACK, SWITCH_RTP_FLAG_PAUSE, SWITCH_RTP_FLAG_PROXY_MEDIA, SWITCH_RTP_FLAG_RESET, SWITCH_RTP_FLAG_SECURE_SEND, 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_time_now(), SWITCH_VAD_FLAG_EVENTS_NOTALK, SWITCH_VAD_FLAG_EVENTS_TALK, SWITCH_VAD_FLAG_TALKING, switch_rtp::te, switch_rtp::timer, 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, WRITE_DEC, and WRITE_INC.

Referenced by switch_rtp_write_frame().

7185 {
7186  switch_size_t bytes;
7187  uint8_t send = 1;
7188  uint32_t this_ts = 0;
7189  int ret;
7190  switch_time_t now;
7191  uint8_t m = 0;
7192 
7193  if (!switch_rtp_ready(rtp_session)) {
7194  return -1;
7195  }
7196 
7197  if (!rtp_write_ready(rtp_session, datalen, __LINE__)) {
7198  return 0;
7199  }
7200 
7201  WRITE_INC(rtp_session);
7202 
7203  if (send_msg) {
7204  bytes = datalen;
7205 
7206  m = (uint8_t) send_msg->header.m;
7207  rtp_session->ts = ntohl(send_msg->header.ts);
7208 
7209  if (flags && *flags & SFF_RFC2833) {
7210  if (rtp_session->te == INVALID_PT) {
7211  ret = 0;
7212  goto end;
7213  }
7214  send_msg->header.pt = rtp_session->te;
7215  }
7216  data = send_msg->body;
7217  if (datalen > rtp_header_len) {
7218  datalen -= rtp_header_len;
7219  }
7220  } else {
7221  if (*flags & SFF_RFC2833) {
7222  if (rtp_session->te == INVALID_PT) {
7223  ret = 0;
7224  goto end;
7225  }
7226  payload = rtp_session->te;
7227  }
7228 
7229  send_msg = &rtp_session->send_msg;
7230  send_msg->header.pt = payload;
7231 
7232  m = get_next_write_ts(rtp_session, timestamp);
7233 
7234  rtp_session->send_msg.header.ts = htonl(rtp_session->ts);
7235 
7236  memcpy(send_msg->body, data, datalen);
7237  bytes = datalen + rtp_header_len;
7238  }
7239 
7240  if (!switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO)) {
7241 
7242  if ((rtp_session->rtp_bugs & RTP_BUG_NEVER_SEND_MARKER)) {
7243  m = 0;
7244  } else {
7245  if (!rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] &&
7246  ((!rtp_session->flags[SWITCH_RTP_FLAG_RESET] && (rtp_session->ts - rtp_session->last_write_ts > rtp_session->samples_per_interval * 10))
7247  || rtp_session->ts == rtp_session->samples_per_interval)) {
7248  m++;
7249  }
7250 
7251  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] &&
7252  (rtp_session->timer.samplecount - rtp_session->last_write_samplecount) > rtp_session->samples_per_interval * 10) {
7253  m++;
7254  }
7255 
7256  if (!rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] &&
7257  ((unsigned) ((switch_micro_time_now() - rtp_session->last_write_timestamp))) > (rtp_session->ms_per_packet * 10)) {
7258  m++;
7259  }
7260 
7261  if (rtp_session->cn && payload != rtp_session->cng_pt) {
7262  rtp_session->cn = 0;
7263  m++;
7264  }
7265 
7266  if (rtp_session->need_mark && !rtp_session->sending_dtmf) {
7267  m++;
7268  rtp_session->need_mark = 0;
7269  }
7270  }
7271 
7272  if (m) {
7273  rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 1;
7274  rtp_session->ts = 0;
7275  }
7276 
7277  /* If the marker was set, and the timestamp seems to have started over - set a new SSRC, to indicate this is a new stream */
7278  if (m && !switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_SEND) && (rtp_session->rtp_bugs & RTP_BUG_CHANGE_SSRC_ON_MARKER) &&
7279  (rtp_session->flags[SWITCH_RTP_FLAG_RESET] || (rtp_session->ts <= rtp_session->last_write_ts && rtp_session->last_write_ts > 0))) {
7280  switch_rtp_set_ssrc(rtp_session, (uint32_t) ((intptr_t) rtp_session + (uint32_t) switch_epoch_time_now(NULL)));
7281  }
7282 
7284  send_msg->header.m = (m && !(rtp_session->rtp_bugs & RTP_BUG_NEVER_SEND_MARKER)) ? 1 : 0;
7285  }
7286  }
7287 
7288 
7290  /* Normalize the timestamps to our own base by generating a made up starting point then adding the measured deltas to that base
7291  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*
7292  */
7293 
7294  if (!rtp_session->ts_norm.ts) {
7296  rtp_session->ts_norm.ts = (uint32_t) rand() % 1000000 + 1;
7297  } else {
7298  switch_core_timer_sync(&rtp_session->timer);
7299  rtp_session->ts_norm.ts = rtp_session->timer.samplecount;
7300  }
7301  }
7302 
7303  if (!rtp_session->ts_norm.last_ssrc || send_msg->header.ssrc != rtp_session->ts_norm.last_ssrc) {
7305  if (rtp_session->ts_norm.last_ssrc) {
7306  rtp_session->ts_norm.delta_ct = 1;
7307  rtp_session->ts_norm.delta_ttl = 0;
7308  if (rtp_session->ts_norm.delta) {
7309  rtp_session->ts_norm.ts += rtp_session->ts_norm.delta;
7310  }
7311  }
7312  }
7313 
7314  rtp_session->ts_norm.last_ssrc = send_msg->header.ssrc;
7315  rtp_session->ts_norm.last_frame = ntohl(send_msg->header.ts);
7316  }
7317 
7318 
7319  if (ntohl(send_msg->header.ts) != rtp_session->ts_norm.last_frame) {
7321  int32_t delta = (int32_t) (ntohl(send_msg->header.ts) - rtp_session->ts_norm.last_frame);
7322 
7323  if (switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO) && delta > 0 && delta < 90000) {
7324  rtp_session->ts_norm.delta = delta;
7325  }
7326 
7327  rtp_session->ts_norm.ts += rtp_session->ts_norm.delta;
7328  } else {
7329  switch_core_timer_sync(&rtp_session->timer);
7330  rtp_session->ts_norm.ts = rtp_session->timer.samplecount;
7331  }
7332  }
7333 
7334  rtp_session->ts_norm.last_frame = ntohl(send_msg->header.ts);
7335  send_msg->header.ts = htonl(rtp_session->ts_norm.ts);
7336  }
7337 
7338  send_msg->header.ssrc = htonl(rtp_session->ssrc);
7339 
7340  if (rtp_session->flags[SWITCH_RTP_FLAG_GOOGLEHACK] && rtp_session->send_msg.header.pt == 97) {
7341  rtp_session->last_rtp_hdr.pt = 102;
7342  }
7343 
7344  if (rtp_session->flags[SWITCH_RTP_FLAG_VAD] &&
7345  rtp_session->last_rtp_hdr.pt == rtp_session->vad_data.read_codec->implementation->ianacode) {
7346 
7347  int16_t decoded[SWITCH_RECOMMENDED_BUFFER_SIZE / sizeof(int16_t)] = { 0 };
7348  uint32_t rate = 0;
7349  uint32_t codec_flags = 0;
7350  uint32_t len = sizeof(decoded);
7351  time_t now = switch_epoch_time_now(NULL);
7352  send = 0;
7353 
7354  if (rtp_session->vad_data.scan_freq && rtp_session->vad_data.next_scan <= now) {
7355  rtp_session->vad_data.bg_count = rtp_session->vad_data.bg_level = 0;
7356  rtp_session->vad_data.next_scan = now + rtp_session->vad_data.scan_freq;
7357  }
7358 
7359  if (switch_core_codec_decode(&rtp_session->vad_data.vad_codec,
7360  rtp_session->vad_data.read_codec,
7361  data,
7362  datalen,
7364  decoded, &len, &rate, &codec_flags) == SWITCH_STATUS_SUCCESS) {
7365 
7366  uint32_t energy = 0;
7367  uint32_t x, y = 0, z = len / sizeof(int16_t);
7368  uint32_t score = 0;
7369  int divisor = 0;
7370  if (z) {
7371 
7372  if (!(divisor = rtp_session->vad_data.read_codec->implementation->actual_samples_per_second / 8000)) {
7373  divisor = 1;
7374  }
7375 
7376  for (x = 0; x < z; x++) {
7377  energy += abs(decoded[y]);
7379  }
7380 
7381  if (++rtp_session->vad_data.start_count < rtp_session->vad_data.start) {
7382  send = 1;
7383  } else {
7384  score = (energy / (z / divisor));
7385  if (score && (rtp_session->vad_data.bg_count < rtp_session->vad_data.bg_len)) {
7386  rtp_session->vad_data.bg_level += score;
7387  if (++rtp_session->vad_data.bg_count == rtp_session->vad_data.bg_len) {
7388  rtp_session->vad_data.bg_level /= rtp_session->vad_data.bg_len;
7389  }
7390  send = 1;
7391  } else {
7392  if (score > rtp_session->vad_data.bg_level && !switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)) {
7393  uint32_t diff = score - rtp_session->vad_data.bg_level;
7394 
7395  if (rtp_session->vad_data.hangover_hits) {
7396  rtp_session->vad_data.hangover_hits--;
7397  }
7398 
7399  if (diff >= rtp_session->vad_data.diff_level || ++rtp_session->vad_data.hangunder_hits >= rtp_session->vad_data.hangunder) {
7400 
7402 
7403  rtp_session->vad_data.start_talking = switch_time_now();
7404 
7405  if (!(rtp_session->rtp_bugs & RTP_BUG_NEVER_SEND_MARKER)) {
7406  send_msg->header.m = 1;
7407  }
7408  rtp_session->vad_data.hangover_hits = rtp_session->vad_data.hangunder_hits = rtp_session->vad_data.cng_count = 0;
7410 
7411  if ((rtp_session->vad_data.fire_events & VAD_FIRE_TALK)) {
7412  switch_event_t *event;
7415  switch_event_fire(&event);
7416  }
7417  }
7418  }
7419  }
7420  } else {
7421  if (rtp_session->vad_data.hangunder_hits) {
7422  rtp_session->vad_data.hangunder_hits--;
7423  }
7424  if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)) {
7425  if (++rtp_session->vad_data.hangover_hits >= rtp_session->vad_data.hangover) {
7426  rtp_session->vad_data.stop_talking = switch_time_now();
7427  rtp_session->vad_data.total_talk_time += (rtp_session->vad_data.stop_talking - rtp_session->vad_data.start_talking);
7428 
7430 
7431  rtp_session->vad_data.hangover_hits = rtp_session->vad_data.hangunder_hits = rtp_session->vad_data.cng_count = 0;
7433 
7434  if ((rtp_session->vad_data.fire_events & VAD_FIRE_NOT_TALK)) {
7435  switch_event_t *event;
7438  switch_event_fire(&event);
7439  }
7440  }
7441  }
7442  }
7443  }
7444  }
7445  }
7446  }
7447 
7448  if (switch_test_flag(&rtp_session->vad_data, SWITCH_VAD_FLAG_TALKING)) {
7449  send = 1;
7450  }
7451  }
7452  } else {
7453  ret = -1;
7454  goto end;
7455  }
7456  }
7457 
7458  if (!switch_rtp_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO)) {
7459  this_ts = ntohl(send_msg->header.ts);
7460 
7461  if ((this_ts - rtp_session->last_write_ts) > 16000) {
7462  rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 1;
7463  }
7464 
7465  if (!switch_rtp_ready(rtp_session) || rtp_session->sending_dtmf ||
7466  (!rtp_session->flags[SWITCH_RTP_FLAG_RESET] && this_ts > rtp_session->one_second && this_ts < rtp_session->last_write_ts)) {
7467  send = 0;
7468  }
7469  }
7470 
7471  if (rtp_session->flags[SWITCH_RTP_FLAG_PAUSE]) {
7472  send = 0;
7473  }
7474 
7475  if (send) {
7476  send_msg->header.seq = htons(++rtp_session->seq);
7477 
7478  if (rtp_session->flags[SWITCH_RTP_FLAG_BYTESWAP] && send_msg->header.pt == rtp_session->payload) {
7479  switch_swap_linear((int16_t *)send_msg->body, (int) datalen);
7480  }
7481 
7482 #ifdef ENABLE_SRTP
7483  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND]) {
7484  int sbytes = (int) bytes;
7485  err_status_t stat;
7486 
7487 
7488  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_RESET] || !rtp_session->send_ctx[rtp_session->srtp_idx_rtp]) {
7489 
7491  srtp_dealloc(rtp_session->send_ctx[rtp_session->srtp_idx_rtp]);
7492  rtp_session->send_ctx[rtp_session->srtp_idx_rtp] = NULL;
7493  if ((stat = srtp_create(&rtp_session->send_ctx[rtp_session->srtp_idx_rtp],
7494  &rtp_session->send_policy[rtp_session->srtp_idx_rtp])) || !rtp_session->send_ctx[rtp_session->srtp_idx_rtp]) {
7496  "Error! RE-Activating %s Secure RTP SEND\n", rtp_type(rtp_session));
7497  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND] = 0;
7498  ret = -1;
7499  goto end;
7500  } else {
7502  "RE-Activating %s Secure RTP SEND\n", rtp_type(rtp_session));
7503  }
7504  }
7505 
7506 
7507  stat = srtp_protect(rtp_session->send_ctx[rtp_session->srtp_idx_rtp], &send_msg->header, &sbytes);
7508 
7509  if (stat) {
7511  "Error: %s SRTP protection failed with code %d\n", rtp_type(rtp_session), stat);
7512  }
7513 
7514  bytes = sbytes;
7515  }
7516 #endif
7517 #ifdef ENABLE_ZRTP
7518  /* ZRTP Send */
7519  if (zrtp_on && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) {
7520  unsigned int sbytes = (int) bytes;
7521  zrtp_status_t stat = zrtp_status_fail;
7522 
7523 
7524  stat = zrtp_process_rtp(rtp_session->zrtp_stream, (void *) send_msg, &sbytes);
7525 
7526  switch (stat) {
7527  case zrtp_status_ok:
7528  break;
7529  case zrtp_status_drop:
7530  /* switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Error: zRTP protection drop with code %d\n", stat); */
7531  ret = (int) bytes;
7532  goto end;
7533  break;
7534  case zrtp_status_fail:
7535  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error: zRTP protection fail with code %d\n", stat);
7536  break;
7537  default:
7538  break;
7539  }
7540 
7541  bytes = sbytes;
7542  }
7543 #endif
7544 
7545  now = switch_micro_time_now();
7546 #ifdef RTP_DEBUG_WRITE_DELTA
7547  {
7548  int delta = (int) (now - rtp_session->send_time) / 1000;
7549  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "WRITE %d delta %d\n", (int) bytes, delta);
7550  }
7551 #endif
7552  rtp_session->send_time = now;
7553 
7554  if (rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_WRITE]) {
7555  const char *tx_host;
7556  const char *old_host;
7557  const char *my_host;
7558 
7559  char bufa[50], bufb[50], bufc[50];
7560 
7561 
7562  tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->rtp_from_addr);
7563  old_host = switch_get_addr(bufb, sizeof(bufb), rtp_session->remote_addr);
7564  my_host = switch_get_addr(bufc, sizeof(bufc), rtp_session->local_addr);
7565 
7567  "W %s b=%4ld %s:%u %s:%u %s:%u pt=%d ts=%u seq=%u m=%d\n",
7568  rtp_session->session ? switch_channel_get_name(switch_core_session_get_channel(rtp_session->session)) : "NoName",
7569  (long) bytes,
7570  my_host, switch_sockaddr_get_port(rtp_session->local_addr),
7571  old_host, rtp_session->remote_port,
7572  tx_host, switch_sockaddr_get_port(rtp_session->rtp_from_addr),
7573  send_msg->header.pt, ntohl(send_msg->header.ts), ntohs(send_msg->header.seq), send_msg->header.m);
7574 
7575  }
7576 
7577  if (rtp_session->flags[SWITCH_RTP_FLAG_NACK]) {
7578  if (!rtp_session->vbw) {
7579  switch_jb_create(&rtp_session->vbw, SJB_VIDEO, 30, 30, rtp_session->pool);
7580  if (rtp_session->vbw) {
7581  switch_jb_set_flag(rtp_session->vbw, SJB_QUEUE_ONLY);
7582  //switch_jb_debug_level(rtp_session->vbw, 10);
7583  }
7584  }
7585  switch_jb_put_packet(rtp_session->vbw, (switch_rtp_packet_t *)send_msg, bytes);
7586  }
7587 
7588 #ifdef RTP_WRITE_PLOSS
7589  {
7590  int r = (rand() % 10000) + 1;
7591 
7592  if (r <= 200) {
7594  "Simulate dropping packet ......... ts: %u seq: %u\n", ntohl(send_msg->header.ts), ntohs(send_msg->header.seq));
7595  } else {
7596  if (switch_socket_sendto(rtp_session->sock_output, rtp_session->remote_addr, 0, (void *) send_msg, &bytes) != SWITCH_STATUS_SUCCESS) {
7597  rtp_session->seq--;
7598  ret = -1;
7599  goto end;
7600  }
7601  }
7602  }
7603 #else
7604  //if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
7605  //
7606  // rtp_session->flags[SWITCH_RTP_FLAG_DEBUG_RTP_READ]++;
7607  //
7608  // //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "SEND %u\n", ntohs(send_msg->header.seq));
7609  //}
7610  if (switch_socket_sendto(rtp_session->sock_output, rtp_session->remote_addr, 0, (void *) send_msg, &bytes) != SWITCH_STATUS_SUCCESS) {
7611  rtp_session->seq--;
7612  ret = -1;
7613  goto end;
7614  }
7615 #endif
7616  rtp_session->last_write_ts = this_ts;
7617  rtp_session->flags[SWITCH_RTP_FLAG_RESET] = 0;
7618 
7619  if (rtp_session->queue_delay) {
7620  rtp_session->delay_samples = rtp_session->queue_delay;
7621  rtp_session->queue_delay = 0;
7622  }
7623 
7624  rtp_session->stats.outbound.raw_bytes += bytes;
7625  rtp_session->stats.outbound.packet_count++;
7626 
7627  if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
7628  rtp_session->stats.rtcp.sent_pkt_count++;
7629  }
7630 
7631  if (send_msg->header.pt == rtp_session->cng_pt) {
7632  rtp_session->stats.outbound.cng_packet_count++;
7633  } else {
7634  rtp_session->stats.outbound.media_packet_count++;
7635  rtp_session->stats.outbound.media_bytes += bytes;
7636  }
7637 
7638  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
7639  rtp_session->last_write_samplecount = rtp_session->timer.samplecount;
7640  }
7641 
7643  }
7644 
7645  ret = (int) bytes;
7646 
7647  end:
7648 
7649  WRITE_DEC(rtp_session);
7650 
7651  return ret;
7652 }
uint32_t queue_delay
Definition: switch_rtp.c:370
char body[SWITCH_RTP_MAX_BUF_LEN]
Definition: switch_rtp.c:113
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
Definition: switch_time.c:310
#define switch_event_fire(event)
Fire an event filling in most of the arguements with obvious values.
Definition: switch_event.h:412
switch_time_t total_talk_time
Definition: switch_rtp.c:212
#define SWITCH_CHANNEL_SESSION_LOG(x)
uint16_t switch_sockaddr_get_port(switch_sockaddr_t *sa)
Definition: switch_apr.c:916
#define switch_set_flag(obj, flag)
Set a flag on an arbitrary object.
Definition: switch_utils.h:631
uint8_t switch_rtp_ready(switch_rtp_t *rtp_session)
Test if an RTP session is ready.
Definition: switch_rtp.c:4452
#define SWITCH_CHANNEL_LOG
uint32_t delta_ct
Definition: switch_rtp.c:298
switch_sockaddr_t * remote_addr
Definition: switch_rtp.c:326
uint32_t ts
Definition: switch_rtp.c:296
uint32_t delay_samples
Definition: switch_rtp.c:367
#define SWITCH_RECOMMENDED_BUFFER_SIZE
Definition: switch_types.h:557
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:62
switch_port_t remote_port
Definition: switch_rtp.c:398
Representation of an event.
Definition: switch_event.h:80
srtp_policy_t send_policy[2]
Definition: switch_rtp.c:338
struct switch_rtp_vad_data vad_data
Definition: switch_rtp.c:402
uint32_t last_ssrc
Definition: switch_rtp.c:294
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:416
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:628
switch_codec_t vad_codec
Definition: switch_rtp.c:192
switch_core_session_t * session
Definition: switch_rtp.c:191
#define rtp_type(rtp_session)
Definition: switch_rtp.c:513
switch_payload_t cng_pt
Definition: switch_rtp.c:406
_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:335
#define switch_clear_flag(obj, flag)
Clear a flag on an arbitrary object while locked.
Definition: switch_utils.h:655
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:692
#define WRITE_DEC(rtp_session)
Definition: switch_rtp.c:63
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:627
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:773
uint32_t ts
Definition: switch_rtp.c:362
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:372
switch_sockaddr_t * rtp_from_addr
Definition: switch_rtp.c:374
switch_rtp_numbers_t outbound
Definition: switch_types.h:691
switch_status_t switch_rtp_set_ssrc(switch_rtp_t *rtp_session, uint32_t ssrc)
Definition: switch_rtp.c:3725
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:7161
uintptr_t switch_size_t
switch_sockaddr_t * local_addr
Definition: switch_rtp.c:313
uint16_t seq
Definition: switch_rtp.c:353
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:210
uint8_t need_mark
Definition: switch_rtp.c:358
switch_size_t packet_count
Definition: switch_types.h:629
uint32_t delta
Definition: switch_rtp.c:297
switch_rtp_stats_t stats
Definition: switch_rtp.c:428
switch_memory_pool_t * pool
Definition: switch_rtp.c:373
#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:4747
switch_time_t stop_talking
Definition: switch_rtp.c:211
uint8_t cn
Definition: switch_rtp.c:413
uint32_t samples_per_interval
Definition: switch_rtp.c:386
switch_time_t send_time
Definition: switch_rtp.c:439
srtp_hdr_t header
Definition: switch_rtp.c:112
switch_timer_t timer
Definition: switch_rtp.c:411
uint32_t last_write_ts
Definition: switch_rtp.c:363
switch_core_session_t * session
Definition: switch_rtp.c:446
#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:383
switch_time_t last_write_timestamp
Definition: switch_rtp.c:371
int8_t sending_dtmf
Definition: switch_rtp.c:357
switch_size_t media_packet_count
Definition: switch_types.h:631
switch_rtp_bug_flag_t rtp_bugs
Definition: switch_rtp.c:427
int srtp_idx_rtp
Definition: switch_rtp.c:345
uint32_t ms_per_packet
Definition: switch_rtp.c:391
time_t switch_epoch_time_now(time_t *t)
Get the current epoch time.
Definition: switch_time.c:321
switch_payload_t te
Definition: switch_rtp.c:404
uint32_t ssrc
Definition: switch_rtp.c:354
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:624
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:4742
uint32_t last_write_samplecount
Definition: switch_rtp.c:366
const char * switch_get_addr(char *buf, switch_size_t len, switch_sockaddr_t *in)
Definition: switch_apr.c:894
uint8_t hangunder_hits
Definition: switch_rtp.c:199
rtp_msg_t send_msg
Definition: switch_rtp.c:314
static const switch_payload_t INVALID_PT
Definition: switch_rtp.c:75
uint32_t last_frame
Definition: switch_rtp.c:295
switch_time_t switch_time_now(void)
Definition: switch_apr.c:302
rtp_hdr_t last_rtp_hdr
Definition: switch_rtp.c:351
char * switch_channel_get_name(switch_channel_t *channel)
Retrieve the name of a given channel.
switch_payload_t payload
Definition: switch_rtp.c:359
switch_codec_t * read_codec
Definition: switch_rtp.c:193
static uint8_t get_next_write_ts(switch_rtp_t *rtp_session, uint32_t timestamp)
Definition: switch_rtp.c:1490
#define rtp_header_len
Definition: switch_rtp.c:66
uint32_t delta_ttl
Definition: switch_rtp.c:299
uint32_t one_second
Definition: switch_rtp.c:392
switch_socket_t * sock_output
Definition: switch_rtp.c:309
ts_normalize_t ts_norm
Definition: switch_rtp.c:325
switch_status_t switch_core_timer_sync(switch_timer_t *timer)
switch_size_t cng_packet_count
Definition: switch_types.h:635
static int rtp_write_ready ( switch_rtp_t rtp_session,
uint32_t  bytes,
int  line 
)
static

Definition at line 7161 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_dtls_s::state, SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG3, and switch_log_printf().

Referenced by 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().

7162 {
7163  if (!rtp_session) return 0;
7164 
7165  if (rtp_session->ice.ice_user && !(rtp_session->ice.rready || rtp_session->ice.ready)) {
7166  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG3, "Skip sending %s packet %ld bytes (ice not ready @ line %d!)\n",
7167  rtp_type(rtp_session), (long)bytes, line);
7168  return 0;
7169  }
7170 
7171  if (rtp_session->dtls && rtp_session->dtls->state != DS_READY) {
7172  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG3, "Skip sending %s packet %ld bytes (dtls not ready @ line %d!)\n",
7173  rtp_type(rtp_session), (long)bytes, line);
7174  return 0;
7175  }
7176 
7177  return 1;
7178 }
#define SWITCH_CHANNEL_LOG
dtls_state_t state
Definition: switch_rtp.c:268
#define rtp_type(rtp_session)
Definition: switch_rtp.c:513
switch_dtls_t * dtls
Definition: switch_rtp.c:348
switch_rtp_ice_t ice
Definition: switch_rtp.c:377
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 set_dtmf_delay ( switch_rtp_t rtp_session,
uint32_t  ms,
uint32_t  max_ms 
)
static

Definition at line 4763 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::session, SWITCH_CHANNEL_SESSION_LOG, SWITCH_LOG_DEBUG, switch_log_printf(), SWITCH_RTP_FLAG_USE_TIMER, and switch_rtp::timer.

Referenced by do_2833().

4764 {
4765  int upsamp, max_upsamp;
4766 
4767 
4768  if (!max_ms) max_ms = ms;
4769 
4770  upsamp = ms * (rtp_session->samples_per_second / 1000);
4771  max_upsamp = max_ms * (rtp_session->samples_per_second / 1000);
4772 
4773  rtp_session->queue_delay = upsamp;
4774 
4775  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER]) {
4776  rtp_session->max_next_write_samplecount = rtp_session->timer.samplecount + max_upsamp;
4777  rtp_session->next_write_samplecount = rtp_session->timer.samplecount + upsamp;
4778  rtp_session->last_write_ts += upsamp;
4779  }
4780 
4781  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Queue digit delay of %dms\n", ms);
4782 }
uint32_t queue_delay
Definition: switch_rtp.c:370
#define SWITCH_CHANNEL_SESSION_LOG(x)
uint32_t max_next_write_samplecount
Definition: switch_rtp.c:369
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:372
uint32_t samples_per_second
Definition: switch_rtp.c:387
switch_timer_t timer
Definition: switch_rtp.c:411
uint32_t last_write_ts
Definition: switch_rtp.c:363
switch_core_session_t * session
Definition: switch_rtp.c:446
uint32_t next_write_samplecount
Definition: switch_rtp.c:368
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_add_crypto_key ( switch_rtp_t rtp_session,
switch_rtp_crypto_direction_t  direction,
uint32_t  index,
switch_rtp_crypto_key_type_t  type,
unsigned char *  key,
switch_size_t  keylen 
)

Definition at line 3468 of file switch_rtp.c.

References AEAD_AES_128_GCM_8, AEAD_AES_256_GCM_8, AES_CM_128_HMAC_SHA1_32, AES_CM_128_HMAC_SHA1_80, AES_CM_128_NULL_AUTH, AES_CM_256_HMAC_SHA1_80, switch_rtp_crypto_key::index, switch_rtp_crypto_key::key, memset(), switch_rtp_crypto_key::next, 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_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_RESET, SWITCH_RTP_FLAG_SECURE_SEND, 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().

3471 {
3472 #ifndef ENABLE_SRTP
3473  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_CRIT, "SRTP NOT SUPPORTED IN THIS BUILD!\n");
3474  return SWITCH_STATUS_FALSE;
3475 #else
3476  switch_rtp_crypto_key_t *crypto_key;
3477  srtp_policy_t *policy;
3478  err_status_t stat;
3480 
3481  switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
3482  switch_event_t *fsevent = NULL;
3483  int idx = 0;
3484  const char *var;
3485  unsigned char b64_key[512] = "";
3486 
3487  if (direction >= SWITCH_RTP_CRYPTO_MAX || keylen > SWITCH_RTP_MAX_CRYPTO_LEN) {
3488  return SWITCH_STATUS_FALSE;
3489  }
3490 
3491  switch_b64_encode(key, keylen, b64_key, sizeof(b64_key));
3492 
3493  if (switch_true(switch_core_get_variable("rtp_retain_crypto_keys"))) {
3494  switch(direction) {
3496  switch_channel_set_variable(channel, "srtp_local_crypto_key", (const char *)b64_key);
3497  break;
3499  switch_channel_set_variable(channel, "srtp_remote_crypto_key", (const char *)b64_key);
3500  break;
3502  switch_channel_set_variable(channel, "srtcp_local_crypto_key", (const char *)b64_key);
3503  break;
3505  switch_channel_set_variable(channel, "srtcp_remote_crypto_key", (const char *)b64_key);
3506  break;
3507  default:
3508  break;
3509  }
3510  }
3511 
3512  crypto_key = switch_core_alloc(rtp_session->pool, sizeof(*crypto_key));
3513 
3514  if (direction == SWITCH_RTP_CRYPTO_RECV_RTCP) {
3515  direction = SWITCH_RTP_CRYPTO_RECV;
3516  rtp_session->srtp_idx_rtcp = idx = 1;
3517  } else if (direction == SWITCH_RTP_CRYPTO_SEND_RTCP) {
3518  direction = SWITCH_RTP_CRYPTO_SEND;
3519  rtp_session->srtp_idx_rtcp = idx = 1;
3520  }
3521 
3522  if (direction == SWITCH_RTP_CRYPTO_RECV) {
3523  policy = &rtp_session->recv_policy[idx];
3524  } else {
3525  policy = &rtp_session->send_policy[idx];
3526  }
3527 
3528  crypto_key->type = type;
3529  crypto_key->index = index;
3530  memcpy(crypto_key->key, key, keylen);
3531  crypto_key->next = rtp_session->crypto_keys[direction];
3532  rtp_session->crypto_keys[direction] = crypto_key;
3533 
3534  memset(policy, 0, sizeof(*policy));
3535 
3536  /* many devices can't handle gaps in SRTP streams */
3537  if (!((var = switch_channel_get_variable(channel, "srtp_allow_idle_gaps"))
3538  && switch_true(var))
3539  && (!(var = switch_channel_get_variable(channel, "send_silence_when_idle"))
3540  || !(atoi(var)))) {
3541  switch_channel_set_variable(channel, "send_silence_when_idle", "-1");
3542  }
3543 
3544  switch (crypto_key->type) {
3546  crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy->rtp);
3547  crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy->rtcp);
3548 
3550  switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_128_HMAC_SHA1_80");
3551  }
3552  break;
3554  crypto_policy_set_aes_cm_128_hmac_sha1_32(&policy->rtp);
3555  crypto_policy_set_aes_cm_128_hmac_sha1_80(&policy->rtcp);
3556 
3557 
3559  switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_128_HMAC_SHA1_32");
3560  }
3561  break;
3562 
3563  case AEAD_AES_256_GCM_8:
3564  crypto_policy_set_aes_gcm_256_8_auth(&policy->rtp);
3565  crypto_policy_set_aes_gcm_256_8_auth(&policy->rtcp);
3566 
3568  switch_channel_set_variable(channel, "rtp_has_crypto", "AEAD_AES_256_GCM_8");
3569  }
3570  break;
3571 
3572  case AEAD_AES_128_GCM_8:
3573  crypto_policy_set_aes_gcm_128_8_auth(&policy->rtp);
3574  crypto_policy_set_aes_gcm_128_8_auth(&policy->rtcp);
3575 
3577  switch_channel_set_variable(channel, "rtp_has_crypto", "AEAD_AES_128_GCM_8");
3578  }
3579  break;
3580 
3582  crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy->rtp);
3583  crypto_policy_set_aes_cm_256_hmac_sha1_80(&policy->rtcp);
3585  switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_256_HMAC_SHA1_80");
3586  }
3587  break;
3588  case AES_CM_128_NULL_AUTH:
3589  crypto_policy_set_aes_cm_128_null_auth(&policy->rtp);
3590  crypto_policy_set_aes_cm_128_null_auth(&policy->rtcp);
3591 
3593  switch_channel_set_variable(channel, "rtp_has_crypto", "AES_CM_128_NULL_AUTH");
3594  }
3595  break;
3596  default:
3597  break;
3598  }
3599 
3600  policy->key = (uint8_t *) crypto_key->key;
3601  policy->next = NULL;
3602 
3603  policy->window_size = 1024;
3604  policy->allow_repeat_tx = 1;
3605 
3606  //policy->rtp.sec_serv = sec_serv_conf_and_auth;
3607  //policy->rtcp.sec_serv = sec_serv_conf_and_auth;
3608 
3609  switch (direction) {
3611  policy->ssrc.type = ssrc_any_inbound;
3612 
3613  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] && idx == 0 && rtp_session->recv_ctx[idx]) {
3614  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV_RESET] = 1;
3615  } else {
3616  if ((stat = srtp_create(&rtp_session->recv_ctx[idx], policy)) || !rtp_session->recv_ctx[idx]) {
3617  status = SWITCH_STATUS_FALSE;
3618  }
3619 
3620  if (status == SWITCH_STATUS_SUCCESS) {
3621  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "Activating %s Secure %s RECV\n",
3622  rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "Video" : "Audio", idx ? "RTCP" : "RTP");
3623  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_RECV] = 1;
3624  } else {
3625  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error allocating srtp [%d]\n", stat);
3626  return status;
3627  }
3628  }
3629  break;
3631  policy->ssrc.type = ssrc_any_outbound;
3632  //policy->ssrc.type = ssrc_specific;
3633  //policy->ssrc.value = rtp_session->ssrc;
3634 
3635  if (rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND] && idx == 0 && rtp_session->send_ctx[idx]) {
3636  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND_RESET] = 1;
3637  } else {
3638  if ((stat = srtp_create(&rtp_session->send_ctx[idx], policy)) || !rtp_session->send_ctx[idx]) {
3639  status = SWITCH_STATUS_FALSE;
3640  }
3641 
3642  if (status == SWITCH_STATUS_SUCCESS) {
3643  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_INFO, "Activating %s Secure %s SEND\n",
3644  rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] ? "Video" : "Audio", idx ? "RTCP" : "RTP");
3645  rtp_session->flags[SWITCH_RTP_FLAG_SECURE_SEND] = 1;
3646  } else {
3647  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error allocating SRTP [%d]\n", stat);
3648  return status;
3649  }
3650  }
3651 
3652  break;
3653  default:
3654  abort();
3655  break;
3656  }
3657 
3659  if (rtp_session->dtls) {
3660  switch_event_add_header(fsevent, SWITCH_STACK_BOTTOM, "secure_type", "srtp:dtls:AES_CM_128_HMAC_SHA1_80");
3661  switch_channel_set_variable(channel, "rtp_has_crypto", "srtp:dtls:AES_CM_128_HMAC_SHA1_80");
3662  } else {
3663  switch_event_add_header(fsevent, SWITCH_STACK_BOTTOM, "secure_type", "srtp:sdes:%s", switch_channel_get_variable(channel, "rtp_has_crypto"));
3664  }
3665  switch_event_add_header_string(fsevent, SWITCH_STACK_BOTTOM, "caller-unique-id", switch_channel_get_uuid(channel));
3666  switch_event_fire(&fsevent);
3667  }
3668 
3669 
3670  return SWITCH_STATUS_SUCCESS;
3671 #endif
3672 }
#define switch_event_fire(event)
Fire an event filling in most of the arguements with obvious values.
Definition: switch_event.h:412
#define SWITCH_CHANNEL_SESSION_LOG(x)
int srtp_idx_rtcp
Definition: switch_rtp.c:346
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:338
switch_rtp_crypto_key_t * crypto_keys[SWITCH_RTP_CRYPTO_MAX]
Definition: switch_rtp.c:420
static int switch_true(const char *expr)
Evaluate the truthfullness of a string expression.
Definition: switch_utils.h:450
_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:335
#define SWITCH_RTP_MAX_CRYPTO_LEN
Definition: switch_rtp.h:47
switch_status_t switch_b64_encode(unsigned char *in, switch_size_t ilen, unsigned char *out, switch_size_t olen)
Definition: switch_utils.c:890
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:372
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:682
#define switch_channel_get_variable(_c, _v)
srtp_ctx_t * recv_ctx[2]
Definition: switch_rtp.c:336
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.
switch_rtp_crypto_key_type_t type
Definition: switch_rtp.h:74
switch_dtls_t * dtls
Definition: switch_rtp.c:348
char * switch_core_get_variable(_In_z_ const char *varname)
Retrieve a global variable from the core.
char * switch_channel_get_uuid(switch_channel_t *channel)
Retrieve the given channel's unique id.
switch_memory_pool_t * pool
Definition: switch_rtp.c:373
switch_status_t
Common return values.
srtp_policy_t recv_policy[2]
Definition: switch_rtp.c:339
unsigned char key[SWITCH_RTP_MAX_CRYPTO_LEN]
Definition: switch_rtp.h:75
switch_core_session_t * session
Definition: switch_rtp.c:446
#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:383
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.
#define switch_channel_set_variable(_channel, _var, _val)
struct switch_rtp_crypto_key * next
Definition: switch_rtp.h:77
memset(buf, 0, buflen)
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 516 of file switch_rtp.c.

References switch_rtp_ice_t::addr, 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_core_strdup, SWITCH_FALSE, SWITCH_RTP_FLAG_RTCP_MUX, and switch_rtp_set_remote_address().

Referenced by handle_ice().

517 {
518  int is_rtcp = ice == &rtp_session->rtcp_ice;
519  const char *err = "";
520 
521  ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_addr = switch_core_strdup(rtp_session->pool, host);
522  ice->ice_params->cands[ice->ice_params->chosen[ice->proto]][ice->proto].con_port = port;
523  ice->missed_count = 0;
524 
525  if (is_rtcp) {
526  ice->addr = rtp_session->rtcp_remote_addr;
527  } else {
528  switch_rtp_set_remote_address(rtp_session, host, port, 0, SWITCH_FALSE, &err);
529 
530  if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) {
531  ice->addr = rtp_session->remote_addr;
532  }
533  }
534 
535 }
switch_sockaddr_t * remote_addr
Definition: switch_rtp.c:326
int chosen[2]
Definition: switch_rtp.h:107
switch_sockaddr_t * addr
Definition: switch_rtp.c:244
#define switch_core_strdup(_pool, _todup)
Copy a string using memory allocation from a given pool.
Definition: switch_core.h:729
ice_t * ice_params
Definition: switch_rtp.c:248
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:372
char * con_addr
Definition: switch_rtp.h:93
switch_rtp_ice_t rtcp_ice
Definition: switch_rtp.c:378
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:2913
switch_memory_pool_t * pool
Definition: switch_rtp.c:373
ice_proto_t proto
Definition: switch_rtp.c:249
icand_t cands[MAX_CAND][2]
Definition: switch_rtp.h:105
switch_sockaddr_t * rtcp_remote_addr
Definition: switch_rtp.c:326
switch_port_t con_port
Definition: switch_rtp.h:94
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 
)

Definition at line 3740 of file switch_rtp.c.

References switch_rtp_numbers_t::burstrate, CF_SECURE, switch_rtp::cng_pt, switch_rtp::conf_samples_per_interval, switch_rtp::dtmf_data, switch_rtp_rfc2833_data::dtmf_inqueue, switch_rtp_rfc2833_data::dtmf_mutex, switch_rtp_rfc2833_data::dtmf_queue, switch_rtp::flag_mutex, switch_rtp::flags, switch_rtp::from_addr, rtp_msg_t::header, switch_rtp::ice_mutex, switch_rtp_stats_t::inbound, INVALID_PT, switch_rtp_numbers_t::jitter_add, switch_rtp_numbers_t::jitter_addsq, 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_numbers_t::loss, switch_rtp_numbers_t::lossrate, switch_rtp_numbers_t::max_variance, memset(), switch_rtp_numbers_t::min_variance, switch_rtp_numbers_t::mos, switch_rtp::payload, pool, switch_rtp::pool, switch_rtp_numbers_t::R, switch_rtp::read_mutex, switch_rtp::ready, switch_rtp::recv_msg, switch_rtp::recv_te, switch_rtp::rtcp_from_addr, switch_rtp::rtcp_last_sent, switch_rtp::rtp_from_addr, switch_rtp::send_msg, switch_rtp::seq, switch_rtp::session, switch_rtp::ssrc, switch_rtp::stats, SWITCH_CALL_DIRECTION_OUTBOUND, switch_channel_direction(), switch_channel_get_private(), switch_channel_get_variable, SWITCH_CHANNEL_SESSION_LOG, switch_channel_set_private(), switch_channel_set_variable, switch_channel_test_flag(), switch_core_alloc, switch_core_memory_pool_get_data(), switch_core_session_get_channel(), switch_core_strdup, switch_core_timer_init(), switch_epoch_time_now(), SWITCH_LOG_DEBUG, SWITCH_LOG_ERROR, switch_log_printf(), SWITCH_LOG_WARNING, switch_micro_time_now(), switch_mutex_init(), SWITCH_MUTEX_NESTED, switch_queue_create(), switch_rtp_clear_flag(), SWITCH_RTP_FLAG_ENABLE_RTCP, SWITCH_RTP_FLAG_NOBLOCK, SWITCH_RTP_FLAG_PROXY_MEDIA, SWITCH_RTP_FLAG_USE_TIMER, SWITCH_RTP_FLAG_VIDEO, SWITCH_RTP_MAX_BUF_LEN, switch_rtp_set_flag(), switch_rtp_set_flags(), switch_rtp_set_interval(), switch_sockaddr_create(), SWITCH_STATUS_FALSE, SWITCH_STATUS_MEMERR, SWITCH_STATUS_SUCCESS, switch_time_now(), switch_true(), switch_rtp::te, switch_rtp::timer, switch_rtp::timer_name, switch_rtp::write_mutex, and zstr.

Referenced by switch_rtp_new().

3745 {
3746  switch_rtp_t *rtp_session = NULL;
3748  switch_channel_t *channel = NULL;
3749 
3750  if (session) channel = switch_core_session_get_channel(session);
3751 
3752  *new_rtp_session = NULL;
3753 
3754  if (samples_per_interval > SWITCH_RTP_MAX_BUF_LEN) {
3755  *err = "Packet Size Too Large!";
3756  return SWITCH_STATUS_FALSE;
3757  }
3758 
3759  if (!(rtp_session = switch_core_alloc(pool, sizeof(*rtp_session)))) {
3760  *err = "Memory Error!";
3761  return SWITCH_STATUS_MEMERR;
3762  }
3763 
3764  rtp_session->pool = pool;
3765  rtp_session->te = INVALID_PT;
3766  rtp_session->recv_te = INVALID_PT;
3767  rtp_session->cng_pt = INVALID_PT;
3768  rtp_session->session = session;
3769 
3775  switch_queue_create(&rtp_session->dtmf_data.dtmf_queue, 100, rtp_session->pool);
3776  switch_queue_create(&rtp_session->dtmf_data.dtmf_inqueue, 100, rtp_session->pool);
3777 
3778  switch_rtp_set_flags(rtp_session, flags);
3779 
3780  /* for from address on recvfrom calls */
3781  switch_sockaddr_create(&rtp_session->from_addr, pool);
3782  switch_sockaddr_create(&rtp_session->rtp_from_addr, pool);
3783 
3784  if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) {
3785  switch_sockaddr_create(&rtp_session->rtcp_from_addr, pool);
3786  }
3787  rtp_session->seq = (uint16_t) rand();
3788  rtp_session->ssrc = (uint32_t) ((intptr_t) rtp_session + (uint32_t) switch_epoch_time_now(NULL));
3789 
3790  rtp_session->stats.inbound.R = 100.0;
3791  rtp_session->stats.inbound.mos = 4.5;
3792  rtp_session->send_msg.header.ssrc = htonl(rtp_session->ssrc);
3793  rtp_session->send_msg.header.ts = 0;
3794  rtp_session->send_msg.header.m = 0;
3795  rtp_session->send_msg.header.pt = (switch_payload_t) htonl(payload);
3796  rtp_session->send_msg.header.version = 2;
3797  rtp_session->send_msg.header.p = 0;
3798  rtp_session->send_msg.header.x = 0;
3799  rtp_session->send_msg.header.cc = 0;
3800 
3801  rtp_session->recv_msg.header.ssrc = 0;
3802  rtp_session->recv_msg.header.ts = 0;
3803  rtp_session->recv_msg.header.seq = 0;
3804  rtp_session->recv_msg.header.m = 0;
3805  rtp_session->recv_msg.header.pt = (switch_payload_t) htonl(payload);
3806  rtp_session->recv_msg.header.version = 2;
3807  rtp_session->recv_msg.header.p = 0;
3808  rtp_session->recv_msg.header.x = 0;
3809  rtp_session->recv_msg.header.cc = 0;
3810 
3811  rtp_session->payload = payload;
3812  rtp_session->rtcp_last_sent = switch_micro_time_now();
3813 
3814  switch_rtp_set_interval(rtp_session, ms_per_packet, samples_per_interval);
3815  rtp_session->conf_samples_per_interval = samples_per_interval;
3816 
3817  if (rtp_session->flags[SWITCH_RTP_FLAG_USE_TIMER] && zstr(timer_name)) {
3818  timer_name = "soft";
3819  }
3820 
3821  if (!zstr(timer_name) && !strcasecmp(timer_name, "none")) {
3822  timer_name = NULL;
3823  }
3824 
3825  if (!zstr(timer_name)) {
3826  rtp_session->timer_name = switch_core_strdup(pool, timer_name);
3829 
3830  if (switch_core_timer_init(&rtp_session->timer, timer_name, ms_per_packet / 1000, samples_per_interval, pool) == SWITCH_STATUS_SUCCESS) {
3832  "Starting timer [%s] %d bytes per %dms\n", timer_name, samples_per_interval, ms_per_packet / 1000);
3833  } else {
3834  memset(&rtp_session->timer, 0, sizeof(rtp_session->timer));
3836  "Error Starting timer [%s] %d bytes per %dms, async RTP disabled\n", timer_name, samples_per_interval, ms_per_packet / 1000);
3838  }
3839  } else {
3840  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
3841  if (switch_core_timer_init(&rtp_session->timer, "soft", 1, 90, pool) == SWITCH_STATUS_SUCCESS) {
3842  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Starting video timer.\n");
3843  }
3844 
3845  //switch_jb_create(&rtp_session->vb, 3, 10, rtp_session->pool);
3846  //switch_jb_debug_level(rtp_session->vb, 10);
3847 
3848  } else {
3849  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "Not using a timer\n");
3850  }
3851 
3854  }
3855 
3856 
3857  if (channel) {
3858  switch_channel_set_private(channel, "__rtcp_audio_rtp_session", rtp_session);
3859  }
3860 
3861 #ifdef ENABLE_ZRTP
3862  if (zrtp_on && session && channel && !rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA]) {
3863  switch_rtp_t *master_rtp_session = NULL;
3864 
3865  int initiator = 0;
3866  const char *zrtp_enabled = switch_channel_get_variable(channel, "zrtp_secure_media");
3867  int srtp_enabled = switch_channel_test_flag(channel, CF_SECURE);
3868 
3869  if (srtp_enabled && switch_true(zrtp_enabled)) {
3871  "You can not have ZRTP and SRTP enabled simultaneously, ZRTP will be disabled for this call!\n");
3872  switch_channel_set_variable(channel, "zrtp_secure_media", NULL);
3873  zrtp_enabled = NULL;
3874  }
3875 
3876 
3877  if (switch_true(zrtp_enabled)) {
3878  if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO]) {
3879  switch_channel_set_private(channel, "__zrtp_video_rtp_session", rtp_session);
3880  master_rtp_session = switch_channel_get_private(channel, "__zrtp_audio_rtp_session");
3881  } else {
3882  switch_channel_set_private(channel, "__zrtp_audio_rtp_session", rtp_session);
3883  master_rtp_session = rtp_session;
3884  }
3885 
3886 
3888  initiator = 1;
3889  }
3890 
3891  if (rtp_session == master_rtp_session) {
3892  rtp_session->zrtp_profile = switch_core_alloc(rtp_session->pool, sizeof(*rtp_session->zrtp_profile));
3893  zrtp_profile_defaults(rtp_session->zrtp_profile, zrtp_global);
3894 
3895  rtp_session->zrtp_profile->allowclear = 0;
3896  rtp_session->zrtp_profile->disclose_bit = 0;
3897  rtp_session->zrtp_profile->cache_ttl = (uint32_t) -1;
3898 
3899  if (zrtp_status_ok != zrtp_session_init(zrtp_global, rtp_session->zrtp_profile, zid, initiator, &rtp_session->zrtp_session)) {
3900  switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_ERROR, "Error! zRTP INIT Failed\n");
3901  zrtp_session_down(rtp_session->zrtp_session);
3902  rtp_session->zrtp_session = NULL;
3903  goto end;
3904  }
3905 
3906  zrtp_session_set_userdata(rtp_session->zrtp_session, session);
3907 
3908 
3909  if (zrtp_status_ok != zrtp_stream_attach(master_rtp_session->zrtp_session, &rtp_session->zrtp_stream)) {
3910  abort();
3911  }
3912 
3913  zrtp_stream_set_userdata(rtp_session->zrtp_stream, rtp_session);
3914 
3915  if (switch_true(switch_channel_get_variable(channel, "zrtp_enrollment"))) {
3916  zrtp_stream_registration_start(rtp_session->zrtp_stream, rtp_session->ssrc);
3917  } else {
3918  zrtp_stream_start(rtp_session->zrtp_stream, rtp_session->ssrc);
3919  }
3920  }
3921 
3922  }
3923  }
3924 
3925  end:
3926 
3927 #endif
3928 
3929  /* Jitter */
3930  rtp_session->stats.inbound.last_proc_time = switch_time_now() / 1000;
3931  rtp_session->stats.inbound.jitter_n = 0;
3932  rtp_session->stats.inbound.jitter_add = 0;
3933  rtp_session->stats.inbound.jitter_addsq = 0;
3934  rtp_session->stats.inbound.min_variance = 0;
3935  rtp_session->stats.inbound.max_variance = 0;
3936 
3937  /* Burst and Packet Loss */
3938  rtp_session->stats.inbound.lossrate = 0;
3939  rtp_session->stats.inbound.burstrate = 0;
3940  memset(rtp_session->stats.inbound.loss, 0, sizeof(rtp_session->stats.inbound.loss));
3941  rtp_session->stats.inbound.last_loss = 0;
3942  rtp_session->stats.inbound.last_processed_seq = -1;
3943 
3944  rtp_session->ready = 1;
3945  *new_rtp_session = rtp_session;
3946 
3947  return SWITCH_STATUS_SUCCESS;
3948 }
switch_time_t rtcp_last_sent
Definition: switch_rtp.c:389
switch_time_t switch_micro_time_now(void)
Get the current epoch time in microseconds.
Definition: switch_time.c:310
int loss[LOST_BURST_CAPTURE]
Definition: switch_types.h:653
#define SWITCH_CHANNEL_SESSION_LOG(x)
switch_rtp_numbers_t inbound
Definition: switch_types.h:690
switch_sockaddr_t * rtcp_from_addr
Definition: switch_rtp.c:374
switch_status_t switch_core_timer_init(switch_timer_t *timer, const char *timer_name, int interval, int samples, switch_memory_pool_t *pool)
Request a timer handle using given time module.
switch_status_t switch_channel_set_private(switch_channel_t *channel, const char *key, const void *private_info)
Set private data on channel.
switch_mutex_t * write_mutex
Definition: switch_rtp.c:409
switch_mutex_t * flag_mutex
Definition: switch_rtp.c:407
#define switch_core_strdup(_pool, _todup)
Copy a string using memory allocation from a given pool.
Definition: switch_core.h:729
switch_memory_pool_t * pool
void * switch_core_memory_pool_get_data(switch_memory_pool_t *pool, const char *key)
#define SWITCH_RTP_MAX_BUF_LEN
Definition: switch_rtp.h:44
static int switch_true(const char *expr)
Evaluate the truthfullness of a string expression.
Definition: switch_utils.h:450
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.
struct switch_rtp_rfc2833_data dtmf_data
Definition: switch_rtp.c:403
#define zstr(x)
Definition: switch_utils.h:281
switch_payload_t cng_pt
Definition: switch_rtp.c:406
_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.
#define SWITCH_MUTEX_NESTED
Definition: switch_apr.h:318
switch_queue_t * dtmf_inqueue
Definition: switch_rtp.c:233
uint8_t ready
Definition: switch_rtp.c:412
uint32_t flags[SWITCH_RTP_FLAG_INVALID]
Definition: switch_rtp.c:372
switch_mutex_t * dtmf_mutex
Definition: switch_rtp.c:234
switch_status_t switch_sockaddr_create(switch_sockaddr_t **sa, switch_memory_pool_t *pool)
Definition: switch_apr.c:795
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:682
#define switch_channel_get_variable(_c, _v)
void switch_rtp_set_flags(switch_rtp_t *rtp_session, switch_rtp_flag_t flags[SWITCH_RTP_FLAG_INVALID])
Definition: switch_rtp.c:4660
switch_sockaddr_t * rtp_from_addr
Definition: switch_rtp.c:374
switch_status_t switch_mutex_init(switch_mutex_t **lock, unsigned int flags, switch_memory_pool_t *pool)
Definition: switch_apr.c:270
uint16_t seq
Definition: switch_rtp.c:353
char * timer_name
Definition: switch_rtp.c:379
switch_rtp_stats_t stats
Definition: switch_rtp.c:428
switch_memory_pool_t * pool
Definition: switch_rtp.c:373
void switch_rtp_clear_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Clear an RTP Flag.
Definition: switch_rtp.c:4747
void switch_rtp_set_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Set an RTP Flag.
Definition: switch_rtp.c:4702
switch_mutex_t * read_mutex
Definition: switch_rtp.c:408
void * switch_channel_get_private(switch_channel_t *channel, const char *key)
Retrieve private from a given channel.
srtp_hdr_t header
Definition: switch_rtp.c:112
switch_timer_t timer
Definition: switch_rtp.c:411
rtp_msg_t recv_msg
Definition: switch_rtp.c:327
switch_mutex_t * ice_mutex
Definition: switch_rtp.c:410
switch_status_t switch_rtp_set_interval(switch_rtp_t *rtp_session, uint32_t ms_per_packet, uint32_t samples_per_interval)
Definition: switch_rtp.c:3674
switch_core_session_t * session
Definition: switch_rtp.c:446
switch_call_direction_t switch_channel_direction(switch_channel_t *channel)
time_t switch_epoch_time_now(time_t *t)
Get the current epoch time.
Definition: switch_time.c:321
switch_payload_t te
Definition: switch_rtp.c:404
switch_payload_t recv_te
Definition: switch_rtp.c:405
switch_queue_t * dtmf_queue
Definition: switch_rtp.c:217
uint32_t ssrc
Definition: switch_rtp.c:354
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 conf_samples_per_interval
Definition: switch_rtp.c:388
switch_status_t switch_queue_create(switch_queue_t **queue, unsigned int queue_capacity, switch_memory_pool_t *pool)
Definition: switch_apr.c:1109
rtp_msg_t send_msg
Definition: switch_rtp.c:314
switch_sockaddr_t * from_addr
Definition: switch_rtp.c:374
static const switch_payload_t INVALID_PT
Definition: switch_rtp.c:75
#define switch_channel_set_variable(_channel, _var, _val)
switch_time_t switch_time_now(void)
Definition: switch_apr.c:302
switch_payload_t payload
Definition: switch_rtp.c:359
memset(buf, 0, buflen)
uint8_t switch_payload_t
switch_rtp_t* switch_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 
)

Definition at line 3950 of file switch_rtp.c.

References switch_rtp::flag_mutex, switch_rtp::pool, switch_rtp::ready, switch_rtp::rx_host, switch_rtp::rx_port, switch_core_strdup, switch_mutex_lock(), switch_mutex_unlock(), switch_rtp_create(), SWITCH_RTP_FLAG_FLUSH, switch_rtp_release_port(), switch_rtp_set_flag(), switch_rtp_set_local_address(), switch_rtp_set_remote_address(), SWITCH_STATUS_SUCCESS, SWITCH_TRUE, and zstr.

3958 {
3959  switch_rtp_t *rtp_session = NULL;
3960 
3961  if (zstr(rx_host)) {
3962  *err = "Missing local host";
3963  goto end;
3964  }
3965 
3966  if (!rx_port) {
3967  *err = "Missing local port";
3968  goto end;
3969  }
3970 
3971  if (zstr(tx_host)) {
3972  *err = "Missing remote host";
3973  goto end;
3974  }
3975 
3976  if (!tx_port) {
3977  *err = "Missing remote port";
3978  goto end;
3979  }
3980 
3981  if (switch_rtp_create(&rtp_session, payload, samples_per_interval, ms_per_packet, flags, timer_name, err, pool) != SWITCH_STATUS_SUCCESS) {
3982  goto end;
3983  }
3984 
3985  switch_mutex_lock(rtp_session->flag_mutex);
3986 
3987  if (switch_rtp_set_local_address(rtp_session, rx_host, rx_port, err) != SWITCH_STATUS_SUCCESS) {
3988  switch_mutex_unlock(rtp_session->flag_mutex);
3989  rtp_session = NULL;
3990  goto end;
3991  }
3992 
3993  if (switch_rtp_set_remote_address(rtp_session, tx_host, tx_port, 0, SWITCH_TRUE, err) != SWITCH_STATUS_SUCCESS) {
3994  switch_mutex_unlock(rtp_session->flag_mutex);
3995  rtp_session = NULL;
3996  goto end;
3997  }
3998 
3999  end:
4000 
4001  if (rtp_session) {
4002  switch_mutex_unlock(rtp_session->flag_mutex);
4003  rtp_session->ready = 2;
4004  rtp_session->rx_host = switch_core_strdup(rtp_session->pool, rx_host);
4005  rtp_session->rx_port = rx_port;
4007  } else {
4008  switch_rtp_release_port(rx_host, rx_port);
4009  }
4010 
4011  return rtp_session;
4012 }
switch_port_t rx_port
Definition: switch_rtp.c:376
switch_mutex_t * flag_mutex
Definition: switch_rtp.c:407
#define switch_core_strdup(_pool, _todup)
Copy a string using memory allocation from a given pool.
Definition: switch_core.h:729
switch_memory_pool_t * pool
#define zstr(x)
Definition: switch_utils.h:281
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:290
uint8_t ready
Definition: switch_rtp.c:412
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:285
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:2913
switch_memory_pool_t * pool
Definition: switch_rtp.c:373
void switch_rtp_set_flag(switch_rtp_t *rtp_session, switch_rtp_flag_t flag)
Set an RTP Flag.
Definition: switch_rtp.c:4702
char * rx_host
Definition: switch_rtp.c:375
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.
Definition: switch_rtp.c:2585
void switch_rtp_release_port(const char *ip, switch_port_t port)
Definition: switch_rtp.c:2415
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)
Definition: switch_rtp.c:3740
void switch_rtp_reset_vb ( switch_rtp_t rtp_session)

Definition at line 2778 of file switch_rtp.c.

References switch_jb_reset().

Referenced by switch_rtp_reset().

2779 {
2780  if (rtp_session->vb) {
2781  switch_jb_reset(rtp_session->vb);
2782  }
2783 
2784  if (rtp_session->vbw) {
2785  switch_jb_reset(rtp_session->vbw);
2786  }
2787 }
switch_jb_t * vbw
Definition: switch_rtp.c:416
switch_jb_t * vb
Definition: switch_rtp.c:415
void switch_jb_reset(switch_jb_t *jb)
static int using_ice ( switch_rtp_t rtp_session)
static

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

1947 {
1948  if (rtp_session->ice.ice_user || rtp_session->rtcp_ice.ice_user) {
1949  return 1;
1950  }
1951 
1952  return 0;
1953 }
switch_rtp_ice_t rtcp_ice
Definition: switch_rtp.c:378
switch_rtp_ice_t ice
Definition: switch_rtp.c:377

Variable Documentation

switch_hash_t* alloc_hash = NULL
static

Definition at line 109 of file switch_rtp.c.

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

Definition at line 2988 of file switch_rtp.c.

Definition at line 291 of file switch_rtp.c.

switch_port_t END_PORT = RTP_END_PORT
static

Definition at line 82 of file switch_rtp.c.

Referenced by switch_rtp_request_port(), and switch_rtp_set_end_port().

int global_init = 0
static

Definition at line 766 of file switch_rtp.c.

const switch_payload_t INVALID_PT = 255
static
switch_mutex_t* port_lock = NULL
static
switch_port_t START_PORT = RTP_START_PORT
static

Definition at line 81 of file switch_rtp.c.

Referenced by switch_rtp_request_port(), and switch_rtp_set_start_port().