2006-04-03 21:00:13 +00:00
/*
* FreeSWITCH Modular Media Switching Software Library / Soft - Switch Application
2012-04-18 16:51:48 +00:00
* Copyright ( C ) 2005 - 2012 , Anthony Minessale II < anthm @ freeswitch . org >
2006-04-03 21:00:13 +00:00
*
* Version : MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 ( the " License " ) ; you may not use this file except in compliance with
* the License . You may obtain a copy of the License at
* http : //www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an " AS IS " basis ,
* WITHOUT WARRANTY OF ANY KIND , either express or implied . See the License
* for the specific language governing rights and limitations under the
* License .
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft - Switch Application
*
* The Initial Developer of the Original Code is
2009-02-04 21:20:54 +00:00
* Anthony Minessale II < anthm @ freeswitch . org >
2006-04-03 21:00:13 +00:00
* Portions created by the Initial Developer are Copyright ( C )
* the Initial Developer . All Rights Reserved .
*
* Contributor ( s ) :
*
2009-02-04 21:20:54 +00:00
* Anthony Minessale II < anthm @ freeswitch . org >
2007-04-07 03:07:43 +00:00
* Marcel Barbulescu < marcelbarbulescu @ gmail . com >
2006-04-03 21:00:13 +00:00
*
*
* switch_rtp . c - - RTP
*
*/
2009-01-16 20:29:56 +00:00
//#define DEBUG_2833
2009-04-26 22:10:28 +00:00
//#define RTP_DEBUG_WRITE_DELTA
2011-01-21 21:03:19 +00:00
//#define DEBUG_MISSED_SEQ
2011-09-09 17:02:15 +00:00
2006-04-03 21:00:13 +00:00
# include <switch.h>
2007-06-20 09:02:30 +00:00
# include <switch_stun.h>
2011-12-16 00:13:09 +00:00
# include <apr_network_io.h>
2006-04-03 21:00:13 +00:00
# undef PACKAGE_NAME
# undef PACKAGE_STRING
# undef PACKAGE_TARNAME
# undef PACKAGE_VERSION
# undef PACKAGE_BUGREPORT
2006-10-02 20:49:26 +00:00
# undef VERSION
# undef PACKAGE
2006-12-15 15:33:43 +00:00
# undef inline
2006-04-03 21:00:13 +00:00
# include <datatypes.h>
# include <srtp.h>
2008-01-16 06:01:53 +00:00
2008-01-18 18:14:53 +00:00
# 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--
2008-01-16 06:01:53 +00:00
2007-05-04 23:29:35 +00:00
# include "stfu.h"
2008-01-16 06:01:53 +00:00
2006-04-03 21:00:13 +00:00
# define rtp_header_len 12
2006-04-05 20:17:22 +00:00
# define RTP_START_PORT 16384
# define RTP_END_PORT 32768
2006-04-10 16:11:24 +00:00
# define MASTER_KEY_LEN 30
2006-05-05 13:35:33 +00:00
# define RTP_MAGIC_NUMBER 42
2008-01-16 06:01:53 +00:00
# define MAX_SRTP_ERRS 10
2011-09-09 21:08:47 +00:00
# define RTP_TS_RESET 1
2006-04-05 20:17:22 +00:00
2007-04-07 03:07:43 +00:00
static switch_port_t START_PORT = RTP_START_PORT ;
static switch_port_t END_PORT = RTP_END_PORT ;
2006-04-05 20:17:22 +00:00
static switch_port_t NEXT_PORT = RTP_START_PORT ;
static switch_mutex_t * port_lock = NULL ;
2006-04-03 21:00:13 +00:00
typedef srtp_hdr_t rtp_hdr_t ;
2009-05-20 23:04:05 +00:00
# ifdef ENABLE_ZRTP
2012-04-05 04:10:55 +00:00
# include "zrtp.h"
2009-05-20 23:04:05 +00:00
static zrtp_global_t * zrtp_global ;
2012-05-17 06:13:32 +00:00
# ifndef WIN32
2009-05-20 23:04:05 +00:00
static zrtp_zid_t zid = { " FreeSWITCH01 " } ;
2012-05-17 06:13:32 +00:00
# else
2012-05-17 13:38:11 +00:00
static zrtp_zid_t zid = { " FreeSWITCH0 " } ;
2012-05-17 06:13:32 +00:00
# endif
2009-05-20 23:04:05 +00:00
static int zrtp_on = 0 ;
2009-09-10 14:47:27 +00:00
# define ZRTP_MITM_TRIES 100
2009-05-20 23:04:05 +00:00
# endif
2006-05-04 20:19:23 +00:00
# ifdef _MSC_VER
2006-05-05 13:35:33 +00:00
# pragma pack(4)
2006-05-04 20:19:23 +00:00
# endif
# ifdef _MSC_VER
# pragma pack()
2011-05-02 16:33:57 +00:00
# define ENABLE_SRTP
2006-05-04 20:19:23 +00:00
# endif
2007-12-11 19:15:02 +00:00
static switch_hash_t * alloc_hash = NULL ;
2006-05-04 20:19:23 +00:00
2006-04-03 21:00:13 +00:00
typedef struct {
2007-03-29 22:31:56 +00:00
srtp_hdr_t header ;
char body [ SWITCH_RTP_MAX_BUF_LEN ] ;
2006-04-03 21:00:13 +00:00
} rtp_msg_t ;
2010-04-20 00:07:23 +00:00
typedef struct {
switch_rtcp_hdr_t header ;
char body [ SWITCH_RTCP_MAX_BUF_LEN ] ;
} rtcp_msg_t ;
2011-12-16 15:16:37 +00:00
typedef enum {
VAD_FIRE_TALK = ( 1 < < 0 ) ,
VAD_FIRE_NOT_TALK = ( 1 < < 1 )
} vad_talk_mask_t ;
2006-04-21 22:31:08 +00:00
struct switch_rtp_vad_data {
2006-04-29 06:05:03 +00:00
switch_core_session_t * session ;
switch_codec_t vad_codec ;
switch_codec_t * read_codec ;
2006-04-21 22:31:08 +00:00
uint32_t bg_level ;
uint32_t bg_count ;
uint32_t bg_len ;
uint32_t diff_level ;
uint8_t hangunder ;
uint8_t hangunder_hits ;
uint8_t hangover ;
uint8_t hangover_hits ;
uint8_t cng_freq ;
uint8_t cng_count ;
switch_vad_flag_t flags ;
uint32_t ts ;
uint8_t start ;
uint8_t start_count ;
uint8_t scan_freq ;
time_t next_scan ;
2011-12-16 15:16:37 +00:00
int fire_events ;
2006-04-21 22:31:08 +00:00
} ;
2006-05-04 17:51:53 +00:00
struct switch_rtp_rfc2833_data {
switch_queue_t * dtmf_queue ;
char out_digit ;
unsigned char out_digit_packet [ 4 ] ;
unsigned int out_digit_sofar ;
2008-01-11 21:59:17 +00:00
unsigned int out_digit_sub_sofar ;
2006-05-04 17:51:53 +00:00
unsigned int out_digit_dur ;
2007-03-22 20:55:46 +00:00
uint16_t in_digit_seq ;
2008-01-11 21:59:17 +00:00
uint32_t in_digit_ts ;
2011-02-18 22:54:51 +00:00
uint32_t last_in_digit_ts ;
2009-01-12 22:18:48 +00:00
uint32_t in_digit_sanity ;
2011-01-07 23:53:09 +00:00
uint32_t in_interleaved ;
2008-01-11 01:27:24 +00:00
uint32_t timestamp_dtmf ;
2008-01-11 21:59:17 +00:00
uint16_t last_duration ;
uint32_t flip ;
char first_digit ;
2006-05-04 17:51:53 +00:00
char last_digit ;
2007-12-22 00:32:20 +00:00
switch_queue_t * dtmf_inqueue ;
2006-05-04 17:51:53 +00:00
switch_mutex_t * dtmf_mutex ;
2011-01-07 22:04:24 +00:00
uint8_t in_digit_queued ;
2006-05-04 17:51:53 +00:00
} ;
2012-06-22 23:08:59 +00:00
typedef struct {
char * ice_user ;
char * user_ice ;
char * pass ;
uint32_t stuncount ;
uint32_t funny_stun ;
uint32_t default_stuncount ;
} switch_rtp_ice_t ;
2006-04-03 21:00:13 +00:00
struct switch_rtp {
2008-07-03 18:50:15 +00:00
/*
* Two sockets are needed because we might be transcoding protocol families
* ( e . g . receive over IPv4 and send over IPv6 ) . In case the protocol
* families are equal , sock_input = = sock_output and only one socket is
* used .
*/
2010-04-20 00:07:23 +00:00
switch_socket_t * sock_input , * sock_output , * rtcp_sock_input , * rtcp_sock_output ;
switch_pollfd_t * read_pollfd , * rtcp_read_pollfd ;
2009-03-30 21:12:06 +00:00
switch_pollfd_t * jb_pollfd ;
2010-02-06 03:38:24 +00:00
2010-04-20 00:07:23 +00:00
switch_sockaddr_t * local_addr , * rtcp_local_addr ;
2006-04-03 21:00:13 +00:00
rtp_msg_t send_msg ;
2010-04-20 00:07:23 +00:00
rtcp_msg_t rtcp_send_msg ;
2006-04-03 21:00:13 +00:00
2010-04-20 00:07:23 +00:00
switch_sockaddr_t * remote_addr , * rtcp_remote_addr ;
2006-04-03 21:00:13 +00:00
rtp_msg_t recv_msg ;
2010-04-20 00:07:23 +00:00
rtcp_msg_t rtcp_recv_msg ;
2008-07-18 16:18:31 +00:00
switch_sockaddr_t * remote_stun_addr ;
2007-05-16 17:47:24 +00:00
uint32_t autoadj_window ;
uint32_t autoadj_tally ;
2007-03-29 22:31:56 +00:00
2008-01-16 06:01:53 +00:00
srtp_ctx_t * send_ctx ;
srtp_ctx_t * recv_ctx ;
srtp_policy_t send_policy ;
srtp_policy_t recv_policy ;
uint32_t srtp_errs ;
2008-05-27 04:30:03 +00:00
2006-04-04 04:15:39 +00:00
uint16_t seq ;
2008-01-12 04:56:20 +00:00
uint32_t ssrc ;
2011-08-31 17:03:57 +00:00
int8_t sending_dtmf ;
2011-08-30 20:00:07 +00:00
uint8_t need_mark ;
2006-04-18 16:50:34 +00:00
switch_payload_t payload ;
2006-05-04 20:19:23 +00:00
switch_payload_t rpayload ;
2006-04-29 23:43:28 +00:00
switch_rtp_invalid_handler_t invalid_handler ;
2006-04-03 21:00:13 +00:00
void * private_data ;
uint32_t ts ;
2007-02-12 18:22:30 +00:00
uint32_t last_write_ts ;
2010-12-22 18:21:52 +00:00
uint32_t last_read_ts ;
2010-12-23 02:38:57 +00:00
uint32_t last_cng_ts ;
2007-03-17 01:52:05 +00:00
uint32_t last_write_samplecount ;
2011-11-14 18:37:45 +00:00
uint32_t delay_samples ;
2012-04-18 14:55:58 +00:00
uint32_t next_write_samplecount ;
2011-09-01 00:30:51 +00:00
uint32_t max_next_write_samplecount ;
uint32_t queue_delay ;
2008-12-09 00:32:51 +00:00
switch_time_t last_write_timestamp ;
2006-04-03 21:00:13 +00:00
uint32_t flags ;
2006-04-29 01:00:52 +00:00
switch_memory_pool_t * pool ;
2010-04-20 00:07:23 +00:00
switch_sockaddr_t * from_addr , * rtcp_from_addr ;
2007-12-11 19:15:02 +00:00
char * rx_host ;
switch_port_t rx_port ;
2012-06-22 23:08:59 +00:00
switch_rtp_ice_t ice ;
switch_rtp_ice_t rtcp_ice ;
2007-03-29 22:31:56 +00:00
char * timer_name ;
2010-04-20 00:07:23 +00:00
char * local_host_str ;
2008-09-18 21:50:18 +00:00
char * remote_host_str ;
2010-06-24 21:01:55 +00:00
char * eff_remote_host_str ;
2006-04-04 17:22:06 +00:00
switch_time_t last_stun ;
2007-03-09 16:30:34 +00:00
uint32_t samples_per_interval ;
2009-09-10 00:46:24 +00:00
uint32_t samples_per_second ;
2007-03-09 16:30:34 +00:00
uint32_t conf_samples_per_interval ;
uint32_t rsamples_per_interval ;
2006-04-07 17:32:14 +00:00
uint32_t ms_per_packet ;
2010-04-20 00:07:23 +00:00
switch_port_t local_port ;
2008-09-22 18:28:21 +00:00
switch_port_t remote_port ;
2010-06-24 21:01:55 +00:00
switch_port_t eff_remote_port ;
2010-04-20 21:15:37 +00:00
switch_port_t remote_rtcp_port ;
2012-06-22 23:08:59 +00:00
2006-04-21 22:31:08 +00:00
struct switch_rtp_vad_data vad_data ;
2006-05-04 17:51:53 +00:00
struct switch_rtp_rfc2833_data dtmf_data ;
2006-05-27 00:24:32 +00:00
switch_payload_t te ;
2010-03-16 22:49:54 +00:00
switch_payload_t recv_te ;
2007-03-07 18:34:22 +00:00
switch_payload_t cng_pt ;
2006-08-28 23:05:26 +00:00
switch_mutex_t * flag_mutex ;
2008-01-18 18:14:53 +00:00
switch_mutex_t * read_mutex ;
switch_mutex_t * write_mutex ;
2006-09-12 22:23:45 +00:00
switch_timer_t timer ;
2006-09-14 00:15:03 +00:00
uint8_t ready ;
2007-03-17 01:52:05 +00:00
uint8_t cn ;
2007-05-04 23:29:35 +00:00
stfu_instance_t * jb ;
2007-10-31 02:12:30 +00:00
uint32_t max_missed_packets ;
uint32_t missed_count ;
2007-12-18 17:33:29 +00:00
rtp_msg_t write_msg ;
2008-01-16 06:01:53 +00:00
switch_rtp_crypto_key_t * crypto_keys [ SWITCH_RTP_CRYPTO_MAX ] ;
2008-01-18 18:14:53 +00:00
int reading ;
int writing ;
2008-07-18 16:18:31 +00:00
char * stun_ip ;
2008-07-18 20:25:39 +00:00
switch_port_t stun_port ;
2008-09-29 23:03:27 +00:00
int from_auto ;
2008-10-01 18:02:46 +00:00
uint32_t cng_count ;
2008-12-29 21:23:17 +00:00
switch_rtp_bug_flag_t rtp_bugs ;
2009-04-03 21:48:52 +00:00
switch_rtp_stats_t stats ;
2010-08-05 22:56:18 +00:00
uint32_t hot_hits ;
2009-09-10 18:26:50 +00:00
uint32_t sync_packets ;
2010-04-20 00:07:23 +00:00
int rtcp_interval ;
switch_bool_t rtcp_fresh_frame ;
2012-04-18 14:55:58 +00:00
switch_time_t send_time ;
switch_byte_t auto_adj_used ;
uint8_t pause_jb ;
uint16_t last_seq ;
switch_time_t last_read_time ;
switch_size_t last_flush_packet_count ;
uint32_t interdigit_delay ;
2009-05-20 23:04:05 +00:00
# ifdef ENABLE_ZRTP
zrtp_session_t * zrtp_session ;
zrtp_profile_t * zrtp_profile ;
2010-02-03 19:19:10 +00:00
zrtp_stream_t * zrtp_stream ;
2009-05-23 01:50:56 +00:00
int zrtp_mitm_tries ;
2010-02-03 19:19:10 +00:00
int zinit ;
2009-05-20 23:04:05 +00:00
# endif
2012-04-18 14:55:58 +00:00
2010-04-20 00:07:23 +00:00
} ;
2009-04-26 22:10:05 +00:00
2010-04-20 00:07:23 +00:00
struct switch_rtcp_senderinfo {
unsigned ssrc : 32 ;
unsigned ntp_msw : 32 ;
unsigned ntp_lsw : 32 ;
unsigned ts : 32 ;
unsigned pc : 32 ;
unsigned oc : 32 ;
2006-04-03 21:00:13 +00:00
} ;
2011-01-21 21:03:19 +00:00
typedef enum {
RESULT_CONTINUE ,
RESULT_GOTO_END ,
RESULT_GOTO_RECVFROM ,
RESULT_GOTO_TIMERCHECK
} handle_rfc2833_result_t ;
2011-03-10 02:06:27 +00:00
static void do_2833 ( switch_rtp_t * rtp_session , switch_core_session_t * session ) ;
2011-01-21 21:03:19 +00:00
static handle_rfc2833_result_t handle_rfc2833 ( switch_rtp_t * rtp_session , switch_size_t bytes , int * do_cng )
{
# ifdef DEBUG_2833
if ( rtp_session - > dtmf_data . in_digit_sanity & & ! ( rtp_session - > dtmf_data . in_digit_sanity % 100 ) ) {
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " sanity %d \n " , rtp_session - > dtmf_data . in_digit_sanity ) ;
2011-01-21 21:03:19 +00:00
}
# endif
if ( rtp_session - > dtmf_data . in_digit_sanity & & ! - - rtp_session - > dtmf_data . in_digit_sanity ) {
2012-04-10 21:17:16 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
2011-01-21 21:03:19 +00:00
rtp_session - > dtmf_data . last_digit = 0 ;
rtp_session - > dtmf_data . in_digit_ts = 0 ;
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " Failed DTMF sanity check. \n " ) ;
2011-01-21 21:03:19 +00:00
}
/* RFC2833 ... like all RFC RE: VoIP, guaranteed to drive you to insanity!
We know the real rules here , but if we enforce them , it ' s an interop nightmare so ,
we put up with as much as we can so we don ' t have to deal with being punished for
doing it right . Nice guys finish last !
*/
if ( bytes > rtp_header_len & & ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_PROXY_MEDIA ) & &
! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_PASS_RFC2833 ) & & rtp_session - > recv_te & & rtp_session - > recv_msg . header . pt = = rtp_session - > recv_te ) {
switch_size_t len = bytes - rtp_header_len ;
unsigned char * packet = ( unsigned char * ) rtp_session - > recv_msg . body ;
int end ;
uint16_t duration ;
char key ;
uint16_t in_digit_seq ;
uint32_t ts ;
if ( ! ( packet [ 0 ] | | packet [ 1 ] | | packet [ 2 ] | | packet [ 3 ] ) & & len > = 8 ) {
2012-04-10 21:17:16 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
2011-01-21 21:03:19 +00:00
packet + = 4 ;
len - = 4 ;
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_WARNING , " DTMF payload offset by 4 bytes. \n " ) ;
2011-01-21 21:03:19 +00:00
}
if ( ! ( packet [ 0 ] | | packet [ 1 ] | | packet [ 2 ] | | packet [ 3 ] ) ) {
2012-04-10 21:17:16 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " Failed DTMF payload check. \n " ) ;
2011-01-21 21:03:19 +00:00
rtp_session - > dtmf_data . last_digit = 0 ;
rtp_session - > dtmf_data . in_digit_ts = 0 ;
2011-02-18 22:54:51 +00:00
rtp_session - > dtmf_data . in_digit_sanity = 0 ;
2011-01-21 21:03:19 +00:00
}
end = packet [ 1 ] & 0x80 ? 1 : 0 ;
duration = ( packet [ 2 ] < < 8 ) + packet [ 3 ] ;
key = switch_rfc2833_to_char ( packet [ 0 ] ) ;
in_digit_seq = ntohs ( ( uint16_t ) rtp_session - > recv_msg . header . seq ) ;
ts = htonl ( rtp_session - > recv_msg . header . ts ) ;
if ( in_digit_seq < rtp_session - > dtmf_data . in_digit_seq ) {
if ( rtp_session - > dtmf_data . in_digit_seq - in_digit_seq > 100 ) {
rtp_session - > dtmf_data . in_digit_seq = 0 ;
}
}
# ifdef DEBUG_2833
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " packet[%d]: %02x %02x %02x %02x \n " , ( int ) len , ( unsigned ) packet [ 0 ] , ( unsigned )
2011-01-21 21:03:19 +00:00
packet [ 1 ] , ( unsigned ) packet [ 2 ] , ( unsigned ) packet [ 3 ] ) ;
# endif
if ( in_digit_seq > rtp_session - > dtmf_data . in_digit_seq ) {
rtp_session - > dtmf_data . in_digit_seq = in_digit_seq ;
# ifdef DEBUG_2833
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " read: %c %u %u %u %u %d %d %s \n " ,
2011-01-21 21:03:19 +00:00
key , in_digit_seq , rtp_session - > dtmf_data . in_digit_seq ,
ts , duration , rtp_session - > recv_msg . header . m , end , end & & ! rtp_session - > dtmf_data . in_digit_ts ? " ignored " : " " ) ;
# endif
if ( ! rtp_session - > dtmf_data . in_digit_queued & & ( rtp_session - > rtp_bugs & RTP_BUG_IGNORE_DTMF_DURATION ) & &
rtp_session - > dtmf_data . in_digit_ts ) {
2011-08-25 14:27:39 +00:00
switch_dtmf_t dtmf = { key , switch_core_min_dtmf_duration ( 0 ) , 0 , SWITCH_DTMF_RTP } ;
2011-01-21 21:03:19 +00:00
# ifdef DEBUG_2833
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " Early Queuing digit %c:%d \n " , dtmf . digit , dtmf . duration / 8 ) ;
2011-01-21 21:03:19 +00:00
# endif
switch_rtp_queue_rfc2833_in ( rtp_session , & dtmf ) ;
rtp_session - > dtmf_data . in_digit_queued = 1 ;
}
/* only set sanity if we do NOT ignore the packet */
if ( rtp_session - > dtmf_data . in_digit_ts ) {
rtp_session - > dtmf_data . in_digit_sanity = 2000 ;
}
if ( rtp_session - > dtmf_data . last_duration > duration & &
rtp_session - > dtmf_data . last_duration > 0xFC17 & & ts = = rtp_session - > dtmf_data . in_digit_ts ) {
rtp_session - > dtmf_data . flip + + ;
}
if ( end ) {
2011-02-18 22:54:51 +00:00
if ( ! rtp_session - > dtmf_data . in_digit_ts & & rtp_session - > dtmf_data . last_in_digit_ts ! = ts ) {
# ifdef DEBUG_2833
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " start with end packet %d \n " , ts ) ;
2011-02-18 22:54:51 +00:00
# endif
rtp_session - > dtmf_data . last_in_digit_ts = ts ;
rtp_session - > dtmf_data . in_digit_ts = ts ;
rtp_session - > dtmf_data . first_digit = key ;
rtp_session - > dtmf_data . in_digit_sanity = 2000 ;
}
2011-01-21 21:03:19 +00:00
if ( rtp_session - > dtmf_data . in_digit_ts ) {
2011-08-25 14:27:39 +00:00
switch_dtmf_t dtmf = { key , duration , 0 , SWITCH_DTMF_RTP } ;
2011-01-21 21:03:19 +00:00
if ( ts > rtp_session - > dtmf_data . in_digit_ts ) {
dtmf . duration + = ( ts - rtp_session - > dtmf_data . in_digit_ts ) ;
}
if ( rtp_session - > dtmf_data . flip ) {
dtmf . duration + = rtp_session - > dtmf_data . flip * 0xFFFF ;
rtp_session - > dtmf_data . flip = 0 ;
# ifdef DEBUG_2833
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " you're welcome! \n " ) ;
2011-01-21 21:03:19 +00:00
# endif
}
# ifdef DEBUG_2833
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " done digit=%c ts=%u start_ts=%u dur=%u ddur=%u \n " ,
2011-01-21 21:03:19 +00:00
dtmf . digit , ts , rtp_session - > dtmf_data . in_digit_ts , duration , dtmf . duration ) ;
# endif
if ( ! ( rtp_session - > rtp_bugs & RTP_BUG_IGNORE_DTMF_DURATION ) & & ! rtp_session - > dtmf_data . in_digit_queued ) {
# ifdef DEBUG_2833
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " Queuing digit %c:%d \n " , dtmf . digit , dtmf . duration / 8 ) ;
2011-01-21 21:03:19 +00:00
# endif
switch_rtp_queue_rfc2833_in ( rtp_session , & dtmf ) ;
}
rtp_session - > dtmf_data . last_digit = rtp_session - > dtmf_data . first_digit ;
rtp_session - > dtmf_data . in_digit_ts = 0 ;
rtp_session - > dtmf_data . in_digit_sanity = 0 ;
rtp_session - > dtmf_data . in_digit_queued = 0 ;
* do_cng = 1 ;
} else {
if ( ! switch_rtp_ready ( rtp_session ) ) {
return RESULT_GOTO_END ;
}
switch_cond_next ( ) ;
return RESULT_GOTO_RECVFROM ;
}
} else if ( ! rtp_session - > dtmf_data . in_digit_ts ) {
2011-02-18 22:54:51 +00:00
# ifdef DEBUG_2833
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " start %d \n " , ts ) ;
2011-02-18 22:54:51 +00:00
# endif
2011-01-21 21:03:19 +00:00
rtp_session - > dtmf_data . in_digit_ts = ts ;
2011-02-18 22:54:51 +00:00
rtp_session - > dtmf_data . last_in_digit_ts = ts ;
2011-01-21 21:03:19 +00:00
rtp_session - > dtmf_data . first_digit = key ;
rtp_session - > dtmf_data . in_digit_sanity = 2000 ;
}
rtp_session - > dtmf_data . last_duration = duration ;
} else {
# ifdef DEBUG_2833
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " drop: %c %u %u %u %u %d %d \n " ,
2011-01-21 21:03:19 +00:00
key , in_digit_seq , rtp_session - > dtmf_data . in_digit_seq , ts , duration , rtp_session - > recv_msg . header . m , end ) ;
# endif
switch_cond_next ( ) ;
return RESULT_GOTO_RECVFROM ;
}
}
if ( bytes & & rtp_session - > dtmf_data . in_digit_ts ) {
if ( ! switch_rtp_ready ( rtp_session ) ) {
return RESULT_GOTO_END ;
}
if ( ! rtp_session - > dtmf_data . in_interleaved & & rtp_session - > recv_msg . header . pt ! = rtp_session - > recv_te ) {
/* Drat, they are sending audio still as well as DTMF ok fine..... *sigh* */
rtp_session - > dtmf_data . in_interleaved = 1 ;
}
if ( rtp_session - > dtmf_data . in_interleaved | | ( rtp_session - > rtp_bugs & RTP_BUG_IGNORE_DTMF_DURATION ) ) {
if ( rtp_session - > recv_msg . header . pt = = rtp_session - > recv_te ) {
return RESULT_GOTO_RECVFROM ;
}
} else {
* do_cng = 1 ;
return RESULT_GOTO_TIMERCHECK ;
}
}
return RESULT_CONTINUE ;
}
2011-12-16 00:13:09 +00:00
struct switch_rtcp_report_block {
uint32_t ssrc ; /* The SSRC identifier of the source to which the information in this reception report block pertains. */
unsigned int fraction : 8 ; /* The fraction of RTP data packets from source SSRC_n lost since the previous SR or RR packet was sent */
int lost : 24 ; /* The total number of RTP data packets from source SSRC_n that have been lost since the beginning of reception */
uint32_t highest_sequence_number_received ;
uint32_t jitter ; /* An estimate of the statistical variance of the RTP data packet interarrival time, measured in timestamp units and expressed as an unsigned integer. */
uint32_t lsr ; /* The middle 32 bits out of 64 in the NTP timestamp */
uint32_t dlsr ; /* The delay, expressed in units of 1/65536 seconds, between receiving the last SR packet from source SSRC_n and sending this reception report block */
} ;
2006-04-03 21:00:13 +00:00
static int global_init = 0 ;
2008-05-27 04:30:03 +00:00
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 ) ;
2008-01-11 04:23:09 +00:00
2006-04-03 21:00:13 +00:00
2008-07-18 16:18:31 +00:00
static switch_status_t do_stun_ping ( switch_rtp_t * rtp_session )
{
uint8_t buf [ 256 ] = { 0 } ;
uint8_t * start = buf ;
switch_stun_packet_t * packet ;
2008-07-18 22:30:22 +00:00
//unsigned int elapsed;
2008-07-18 16:18:31 +00:00
switch_size_t bytes ;
switch_status_t status = SWITCH_STATUS_SUCCESS ;
switch_assert ( rtp_session ! = NULL ) ;
WRITE_INC ( rtp_session ) ;
2012-06-22 23:08:59 +00:00
if ( rtp_session - > ice . stuncount ! = 0 ) {
rtp_session - > ice . stuncount - - ;
2008-07-18 16:18:31 +00:00
goto end ;
}
2008-07-18 22:30:22 +00:00
#if 0
2008-07-18 16:18:31 +00:00
if ( rtp_session - > last_stun ) {
2009-01-25 21:23:07 +00:00
elapsed = ( unsigned int ) ( ( switch_micro_time_now ( ) - rtp_session - > last_stun ) / 1000 ) ;
2010-02-06 03:38:24 +00:00
2008-07-18 16:18:31 +00:00
if ( elapsed > 30000 ) {
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " No stun for a long time (PUNT!) \n " ) ;
2008-07-18 16:18:31 +00:00
status = SWITCH_STATUS_FALSE ;
goto end ;
}
}
2008-07-18 22:30:22 +00:00
# endif
2008-07-18 16:18:31 +00:00
2012-06-22 23:08:59 +00:00
if ( rtp_session - > ice . funny_stun ) {
2008-07-18 16:18:31 +00:00
* start + + = 0 ;
* start + + = 0 ;
* start + + = 0x22 ;
* start + + = 0x22 ;
}
packet = switch_stun_packet_build_header ( SWITCH_STUN_BINDING_REQUEST , NULL , start ) ;
bytes = switch_stun_packet_length ( packet ) ;
2008-07-18 22:30:22 +00:00
2012-06-22 23:08:59 +00:00
if ( rtp_session - > ice . funny_stun ) {
2008-07-18 22:30:22 +00:00
packet = ( switch_stun_packet_t * ) buf ;
bytes + = 4 ;
}
2008-07-18 16:18:31 +00:00
switch_socket_sendto ( rtp_session - > sock_output , rtp_session - > remote_stun_addr , 0 , ( void * ) packet , & bytes ) ;
2012-06-22 23:08:59 +00:00
rtp_session - > ice . stuncount = rtp_session - > ice . default_stuncount ;
2010-02-06 03:38:24 +00:00
2010-04-20 00:07:23 +00:00
end :
2008-07-18 16:18:31 +00:00
WRITE_DEC ( rtp_session ) ;
return status ;
}
2012-06-22 23:08:59 +00:00
static switch_status_t ice_out ( switch_rtp_t * rtp_session , switch_rtp_ice_t * ice )
2006-04-04 17:22:06 +00:00
{
2007-12-11 03:57:15 +00:00
uint8_t buf [ 256 ] = { 0 } ;
switch_stun_packet_t * packet ;
unsigned int elapsed ;
switch_size_t bytes ;
2008-01-18 18:14:53 +00:00
switch_status_t status = SWITCH_STATUS_SUCCESS ;
2012-06-22 23:08:59 +00:00
switch_sockaddr_t * remote_addr = rtp_session - > remote_addr ;
switch_socket_t * sock_output = rtp_session - > sock_output ;
if ( ice = = & rtp_session - > rtcp_ice ) {
sock_output = rtp_session - > rtcp_sock_output ;
remote_addr = rtp_session - > rtcp_remote_addr ;
}
2008-05-27 04:30:03 +00:00
2007-12-11 19:23:57 +00:00
switch_assert ( rtp_session ! = NULL ) ;
2012-06-22 23:08:59 +00:00
switch_assert ( ice - > ice_user ! = NULL ) ;
2006-04-04 17:22:06 +00:00
2012-06-22 23:08:59 +00:00
READ_INC ( rtp_session ) ;
2008-05-16 19:11:44 +00:00
2012-06-22 23:08:59 +00:00
if ( ice - > stuncount ! = 0 ) {
ice - > stuncount - - ;
2008-01-18 18:14:53 +00:00
goto end ;
2007-12-11 03:57:15 +00:00
}
2006-04-04 17:22:06 +00:00
2007-12-11 03:57:15 +00:00
if ( rtp_session - > last_stun ) {
2009-01-25 21:23:07 +00:00
elapsed = ( unsigned int ) ( ( switch_micro_time_now ( ) - rtp_session - > last_stun ) / 1000 ) ;
2006-04-04 17:22:06 +00:00
2007-12-11 03:57:15 +00:00
if ( elapsed > 30000 ) {
2012-04-10 21:17:16 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " No stun for a long time (PUNT!) \n " ) ;
2008-01-18 18:14:53 +00:00
status = SWITCH_STATUS_FALSE ;
goto end ;
2006-04-04 17:22:06 +00:00
}
}
2007-12-11 03:57:15 +00:00
packet = switch_stun_packet_build_header ( SWITCH_STUN_BINDING_REQUEST , NULL , buf ) ;
2012-06-22 23:08:59 +00:00
switch_stun_packet_attribute_add_username ( packet , ice - > ice_user , 32 ) ;
if ( ice - > pass ) {
switch_stun_packet_attribute_add_password ( packet , ice - > pass , strlen ( ice - > pass ) ) ;
}
2007-12-11 03:57:15 +00:00
bytes = switch_stun_packet_length ( packet ) ;
2012-06-22 23:08:59 +00:00
switch_socket_sendto ( sock_output , remote_addr , 0 , ( void * ) packet , & bytes ) ;
ice - > stuncount = ice - > default_stuncount ;
2007-12-11 03:57:15 +00:00
2010-04-20 00:07:23 +00:00
end :
2012-06-22 23:08:59 +00:00
READ_DEC ( rtp_session ) ;
2008-05-27 04:30:03 +00:00
2008-07-18 16:18:31 +00:00
return status ;
}
static void handle_stun_ping_reply ( switch_rtp_t * rtp_session , void * data , switch_size_t len )
{
if ( ! switch_rtp_ready ( rtp_session ) ) {
return ;
}
2009-01-25 21:23:07 +00:00
rtp_session - > last_stun = switch_micro_time_now ( ) ;
2006-04-04 17:22:06 +00:00
}
2012-06-22 23:08:59 +00:00
static void handle_ice ( switch_rtp_t * rtp_session , switch_rtp_ice_t * ice , void * data , switch_size_t len )
2006-04-04 17:22:06 +00:00
{
switch_stun_packet_t * packet ;
switch_stun_packet_attribute_t * attr ;
2008-05-10 21:10:44 +00:00
void * end_buf ;
2007-03-29 22:31:56 +00:00
char username [ 33 ] = { 0 } ;
unsigned char buf [ 512 ] = { 0 } ;
2007-12-17 23:43:15 +00:00
switch_size_t cpylen = len ;
2008-01-18 18:14:53 +00:00
2012-06-22 23:08:59 +00:00
if ( ! switch_rtp_ready ( rtp_session ) | | zstr ( ice - > user_ice ) | | zstr ( ice - > ice_user ) ) {
2008-02-25 16:35:19 +00:00
return ;
}
2008-01-18 18:14:53 +00:00
READ_INC ( rtp_session ) ;
WRITE_INC ( rtp_session ) ;
2008-05-27 04:30:03 +00:00
2008-02-25 16:35:19 +00:00
if ( ! switch_rtp_ready ( rtp_session ) ) {
goto end ;
}
2007-12-17 23:43:15 +00:00
if ( cpylen > 512 ) {
cpylen = 512 ;
}
2006-04-04 17:22:06 +00:00
2012-06-22 23:08:59 +00:00
2007-12-17 23:43:15 +00:00
memcpy ( buf , data , cpylen ) ;
2006-04-04 17:22:06 +00:00
packet = switch_stun_packet_parse ( buf , sizeof ( buf ) ) ;
2008-05-13 00:17:58 +00:00
if ( ! packet ) {
2012-04-10 21:17:16 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
2012-06-22 23:08:59 +00:00
//int sbytes = (int) cpylen;
//int stat;
//if ((stat = srtp_unprotect(rtp_session->recv_ctx, buf, &sbytes)) || !(packet = switch_stun_packet_parse(buf, sizeof(buf)))) {
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " Invalid STUN/ICE packet received \n " ) ;
goto end ;
//}
2008-05-13 00:17:58 +00:00
}
end_buf = buf + ( ( sizeof ( buf ) > packet - > header . length ) ? packet - > header . length : sizeof ( buf ) ) ;
2009-01-25 21:23:07 +00:00
rtp_session - > last_stun = switch_micro_time_now ( ) ;
2007-03-29 22:31:56 +00:00
2006-04-04 17:22:06 +00:00
switch_stun_packet_first_attribute ( packet , attr ) ;
do {
2007-03-29 22:31:56 +00:00
switch ( attr - > type ) {
2006-04-04 17:22:06 +00:00
case SWITCH_STUN_ATTR_MAPPED_ADDRESS :
if ( attr - > type ) {
char ip [ 16 ] ;
uint16_t port ;
switch_stun_packet_attribute_get_mapped_address ( attr , ip , & port ) ;
}
break ;
case SWITCH_STUN_ATTR_USERNAME :
2007-03-29 22:31:56 +00:00
if ( attr - > type ) {
2006-04-04 17:22:06 +00:00
switch_stun_packet_attribute_get_username ( attr , username , 32 ) ;
}
break ;
}
2008-05-10 21:10:44 +00:00
} while ( switch_stun_packet_next_attribute ( attr , end_buf ) ) ;
2006-04-04 17:22:06 +00:00
2012-06-22 23:08:59 +00:00
if ( ( packet - > header . type = = SWITCH_STUN_BINDING_REQUEST ) & & ! strcmp ( ice - > user_ice , username ) ) {
2007-12-17 23:19:10 +00:00
uint8_t stunbuf [ 512 ] ;
2006-04-04 17:22:06 +00:00
switch_stun_packet_t * rpacket ;
2007-12-06 22:26:37 +00:00
const char * remote_ip ;
2006-04-04 17:22:06 +00:00
switch_size_t bytes ;
2006-11-15 03:17:28 +00:00
char ipbuf [ 25 ] ;
2012-06-22 23:08:59 +00:00
//int rtcp = 0;
switch_sockaddr_t * from_addr = rtp_session - > from_addr ;
switch_socket_t * sock_output = rtp_session - > sock_output ;
if ( ice = = & rtp_session - > rtcp_ice ) {
//rtcp = 1;
from_addr = rtp_session - > rtcp_from_addr ;
sock_output = rtp_session - > rtcp_sock_output ;
}
2007-03-29 22:31:56 +00:00
2007-12-17 23:19:10 +00:00
memset ( stunbuf , 0 , sizeof ( stunbuf ) ) ;
rpacket = switch_stun_packet_build_header ( SWITCH_STUN_BINDING_RESPONSE , packet - > header . id , stunbuf ) ;
2006-04-04 17:22:06 +00:00
switch_stun_packet_attribute_add_username ( rpacket , username , 32 ) ;
2012-06-22 23:08:59 +00:00
remote_ip = switch_get_addr ( ipbuf , sizeof ( ipbuf ) , from_addr ) ;
switch_stun_packet_attribute_add_binded_address ( rpacket , ( char * ) remote_ip , switch_sockaddr_get_port ( from_addr ) ) ;
2006-04-04 17:22:06 +00:00
bytes = switch_stun_packet_length ( rpacket ) ;
2012-06-22 23:08:59 +00:00
switch_socket_sendto ( sock_output , from_addr , 0 , ( void * ) rpacket , & bytes ) ;
} else if ( ( packet - > header . type = = SWITCH_STUN_BINDING_ERROR_RESPONSE ) ) {
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
ice_out ( rtp_session , ice ) ;
if ( session ) {
switch_core_session_message_t msg = { 0 } ;
msg . from = __FILE__ ;
msg . numeric_arg = packet - > header . type ;
msg . pointer_arg = packet ;
msg . message_id = SWITCH_MESSAGE_INDICATE_STUN_ERROR ;
switch_core_session_receive_message ( session , & msg ) ;
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_DEBUG ,
" STUN/ICE binding error received on %s channel \n " , switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_VIDEO ) ? " video " : " audio " ) ;
}
2006-04-04 17:22:06 +00:00
}
2008-01-18 18:14:53 +00:00
2010-04-20 00:07:23 +00:00
end :
2008-02-25 16:35:19 +00:00
2008-01-18 18:14:53 +00:00
READ_DEC ( rtp_session ) ;
WRITE_DEC ( rtp_session ) ;
2006-04-04 17:22:06 +00:00
}
2009-05-20 23:04:05 +00:00
# ifdef ENABLE_ZRTP
2009-05-22 05:33:21 +00:00
SWITCH_STANDARD_SCHED_FUNC ( zrtp_cache_save_callback )
2009-05-20 23:04:05 +00:00
{
2009-05-22 05:33:21 +00:00
zrtp_status_t status = zrtp_status_ok ;
2009-05-20 23:04:05 +00:00
2009-05-22 05:33:21 +00:00
status = zrtp_def_cache_store ( zrtp_global ) ;
2012-04-11 14:10:56 +00:00
switch_log_printf ( SWITCH_CHANNEL_LOG , SWITCH_LOG_DEBUG , " Saving ZRTP cache: %s \n " , zrtp_status_ok = = status ? " OK " : " FAIL " ) ;
2009-05-22 05:33:21 +00:00
task - > runtime = switch_epoch_time_now ( NULL ) + 900 ;
2009-05-20 23:04:05 +00:00
}
2010-02-06 03:38:24 +00:00
static int zrtp_send_rtp_callback ( const zrtp_stream_t * stream , char * rtp_packet , unsigned int rtp_packet_length )
2009-05-20 23:04:05 +00:00
{
switch_rtp_t * rtp_session = zrtp_stream_get_userdata ( stream ) ;
switch_size_t len = rtp_packet_length ;
zrtp_status_t status = zrtp_status_ok ;
switch_socket_sendto ( rtp_session - > sock_output , rtp_session - > remote_addr , 0 , rtp_packet , & len ) ;
return status ;
}
2012-05-24 00:44:32 +00:00
static void zrtp_event_callback ( zrtp_stream_t * stream , unsigned event )
2009-05-20 23:04:05 +00:00
{
switch_rtp_t * rtp_session = zrtp_stream_get_userdata ( stream ) ;
2010-02-03 19:19:10 +00:00
zrtp_session_info_t zrtp_session_info ;
2009-05-23 01:50:56 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
switch_channel_t * channel = switch_core_session_get_channel ( session ) ;
2009-05-29 02:08:24 +00:00
switch_event_t * fsevent = NULL ;
2010-02-03 19:19:10 +00:00
const char * type ;
2010-02-06 03:38:24 +00:00
2010-02-03 19:19:10 +00:00
type = switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_VIDEO ) ? " video " : " audio " ;
2010-02-06 03:38:24 +00:00
2009-05-20 23:04:05 +00:00
switch ( event ) {
case ZRTP_EVENT_IS_SECURE :
2009-05-28 22:54:51 +00:00
{
switch_set_flag ( rtp_session , SWITCH_ZRTP_FLAG_SECURE_SEND ) ;
switch_set_flag ( rtp_session , SWITCH_ZRTP_FLAG_SECURE_RECV ) ;
2010-02-03 19:19:10 +00:00
if ( ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_VIDEO ) ) {
switch_set_flag ( rtp_session , SWITCH_ZRTP_FLAG_SECURE_MITM_SEND ) ;
switch_set_flag ( rtp_session , SWITCH_ZRTP_FLAG_SECURE_MITM_RECV ) ;
}
if ( zrtp_status_ok = = zrtp_session_get ( stream - > session , & zrtp_session_info ) ) {
2009-05-28 22:54:51 +00:00
if ( zrtp_session_info . sas_is_ready ) {
2010-02-06 03:38:24 +00:00
2010-02-03 19:19:10 +00:00
switch_channel_set_variable_name_printf ( channel , " true " , " zrtp_secure_media_confirmed_%s " , type ) ;
switch_channel_set_variable_name_printf ( channel , stream - > session - > sas1 . buffer , " zrtp_sas1_string_%s " , type ) ;
switch_channel_set_variable_name_printf ( channel , stream - > session - > sas2 . buffer , " zrtp_sas2_string " , type ) ;
2010-02-06 03:38:24 +00:00
2012-05-17 05:33:44 +00:00
zrtp_verified_set ( zrtp_global , & stream - > session - > zid , & stream - > session - > peer_zid , ( uint8_t ) ( zrtp_session_info . sas_is_verified ^ 1 ) ) ;
2010-02-06 03:38:24 +00:00
2009-05-28 22:54:51 +00:00
}
2009-05-22 21:33:15 +00:00
}
2010-02-03 19:19:10 +00:00
if ( ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_VIDEO ) ) {
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
2010-02-06 03:38:24 +00:00
2010-02-03 19:19:10 +00:00
if ( session ) {
switch_channel_t * channel = switch_core_session_get_channel ( session ) ;
switch_rtp_t * video_rtp_session = switch_channel_get_private ( channel , " __zrtp_video_rtp_session " ) ;
2010-02-06 03:38:24 +00:00
2010-02-03 19:19:10 +00:00
if ( ! video_rtp_session ) {
video_rtp_session = switch_channel_get_private_partner ( channel , " __zrtp_video_rtp_session " ) ;
}
if ( video_rtp_session ) {
if ( zrtp_status_ok ! = zrtp_stream_attach ( stream - > session , & video_rtp_session - > zrtp_stream ) ) {
abort ( ) ;
}
zrtp_stream_set_userdata ( video_rtp_session - > zrtp_stream , video_rtp_session ) ;
if ( switch_true ( switch_channel_get_variable ( channel , " zrtp_enrollment " ) ) ) {
zrtp_stream_registration_start ( video_rtp_session - > zrtp_stream , video_rtp_session - > ssrc ) ;
} else {
zrtp_stream_start ( video_rtp_session - > zrtp_stream , video_rtp_session - > ssrc ) ;
}
}
2010-02-06 03:38:24 +00:00
}
2010-02-03 19:19:10 +00:00
}
if ( switch_event_create ( & fsevent , SWITCH_EVENT_CALL_SECURE ) = = SWITCH_STATUS_SUCCESS ) {
switch_event_add_header ( fsevent , SWITCH_STACK_BOTTOM , " secure_media_type " , " %s " , type ) ;
2010-02-06 03:38:24 +00:00
switch_event_add_header ( fsevent , SWITCH_STACK_BOTTOM , " secure_type " , " zrtp:%s:%s " , stream - > session - > sas1 . buffer ,
stream - > session - > sas2 . buffer ) ;
2010-02-03 19:19:10 +00:00
switch_event_add_header_string ( fsevent , SWITCH_STACK_BOTTOM , " caller-unique-id " , switch_channel_get_uuid ( channel ) ) ;
switch_event_fire ( & fsevent ) ;
}
2009-05-29 02:08:24 +00:00
}
2009-05-20 23:04:05 +00:00
break ;
2009-05-30 05:24:52 +00:00
#if 0
2009-05-30 05:01:28 +00:00
case ZRTP_EVENT_NO_ZRTP_QUICK :
{
2010-02-03 19:19:10 +00:00
if ( stream ! = NULL ) {
zrtp_stream_stop ( stream ) ;
2009-05-30 05:01:28 +00:00
}
}
break ;
2009-05-30 05:24:52 +00:00
# endif
2009-05-28 22:54:51 +00:00
case ZRTP_EVENT_IS_CLIENT_ENROLLMENT :
{
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_DEBUG , " Enrolled complete! \n " ) ;
2010-02-03 19:19:10 +00:00
switch_channel_set_variable_name_printf ( channel , " true " , " zrtp_enroll_complete_%s " , type ) ;
2009-05-28 22:54:51 +00:00
}
2009-05-22 21:33:15 +00:00
break ;
2009-05-23 01:50:56 +00:00
2009-05-28 22:54:51 +00:00
case ZRTP_EVENT_USER_ALREADY_ENROLLED :
{
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_DEBUG , " User already enrolled! \n " ) ;
2010-02-03 19:19:10 +00:00
switch_channel_set_variable_name_printf ( channel , " true " , " zrtp_already_enrolled_%s " , type ) ;
2010-02-06 03:38:24 +00:00
2010-02-03 19:19:10 +00:00
if ( zrtp_status_ok = = zrtp_session_get ( stream - > session , & zrtp_session_info ) ) {
2009-05-28 22:54:51 +00:00
if ( zrtp_session_info . sas_is_ready ) {
2012-05-17 05:33:44 +00:00
zrtp_verified_set ( zrtp_global , & stream - > session - > zid , & stream - > session - > peer_zid , ( uint8_t ) ( zrtp_session_info . sas_is_verified ^ 1 ) ) ;
2009-05-28 22:54:51 +00:00
}
2009-05-23 01:50:56 +00:00
}
}
2009-05-22 21:33:15 +00:00
break ;
2009-05-23 01:50:56 +00:00
2009-05-28 22:54:51 +00:00
case ZRTP_EVENT_NEW_USER_ENROLLED :
{
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_DEBUG , " New user enrolled! \n " ) ;
2010-02-03 19:19:10 +00:00
switch_channel_set_variable_name_printf ( channel , " true " , " zrtp_new_user_enrolled_%s " , type ) ;
2009-05-28 22:54:51 +00:00
2010-02-03 19:19:10 +00:00
if ( zrtp_status_ok = = zrtp_session_get ( stream - > session , & zrtp_session_info ) ) {
2009-05-28 22:54:51 +00:00
if ( zrtp_session_info . sas_is_ready ) {
2012-05-17 05:33:44 +00:00
zrtp_verified_set ( zrtp_global , & stream - > session - > zid , & stream - > session - > peer_zid , ( uint8_t ) ( zrtp_session_info . sas_is_verified ^ 1 ) ) ;
2009-05-28 22:54:51 +00:00
}
2009-05-23 01:50:56 +00:00
}
}
2009-05-22 21:33:15 +00:00
break ;
2010-02-06 03:38:24 +00:00
2009-05-22 21:33:15 +00:00
case ZRTP_EVENT_USER_UNENROLLED :
2009-05-28 22:54:51 +00:00
{
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_DEBUG , " User unenrolled! \n " ) ;
2010-02-03 19:19:10 +00:00
switch_channel_set_variable_name_printf ( channel , " true " , " zrtp_user_unenrolled_%s " , type ) ;
2010-02-06 03:38:24 +00:00
2010-02-03 19:19:10 +00:00
if ( zrtp_status_ok = = zrtp_session_get ( stream - > session , & zrtp_session_info ) ) {
2009-05-28 22:54:51 +00:00
if ( zrtp_session_info . sas_is_ready ) {
2012-05-17 05:33:44 +00:00
zrtp_verified_set ( zrtp_global , & stream - > session - > zid , & stream - > session - > peer_zid , ( uint8_t ) ( zrtp_session_info . sas_is_verified ^ 1 ) ) ;
2009-05-28 22:54:51 +00:00
}
2009-05-23 01:50:56 +00:00
}
}
2009-05-21 22:37:15 +00:00
break ;
2009-05-28 22:54:51 +00:00
2009-05-20 23:04:05 +00:00
case ZRTP_EVENT_IS_PENDINGCLEAR :
2009-05-28 22:54:51 +00:00
{
2010-02-03 19:19:10 +00:00
switch_channel_set_variable_name_printf ( channel , " false " , " zrtp_secure_media_confirmed_%s " , type ) ;
2009-05-28 22:54:51 +00:00
switch_clear_flag ( rtp_session , SWITCH_ZRTP_FLAG_SECURE_SEND ) ;
switch_clear_flag ( rtp_session , SWITCH_ZRTP_FLAG_SECURE_RECV ) ;
switch_clear_flag ( rtp_session , SWITCH_ZRTP_FLAG_SECURE_MITM_SEND ) ;
switch_clear_flag ( rtp_session , SWITCH_ZRTP_FLAG_SECURE_MITM_RECV ) ;
rtp_session - > zrtp_mitm_tries = 0 ;
}
2009-05-21 22:37:15 +00:00
break ;
2009-05-28 22:54:51 +00:00
2009-05-20 23:04:05 +00:00
case ZRTP_EVENT_NO_ZRTP :
2009-05-28 22:54:51 +00:00
{
2010-02-03 19:19:10 +00:00
switch_channel_set_variable_name_printf ( channel , " false " , " zrtp_secure_media_confirmed_%s " , type ) ;
2009-05-28 22:54:51 +00:00
}
2009-05-21 22:37:15 +00:00
break ;
2009-05-28 22:54:51 +00:00
2009-05-20 23:04:05 +00:00
default :
break ;
}
}
2009-12-17 18:33:45 +00:00
static void zrtp_logger ( int level , const char * data , int len , int offset )
2009-05-20 23:04:05 +00:00
{
2012-04-11 14:10:56 +00:00
switch_log_printf ( SWITCH_CHANNEL_LOG , SWITCH_LOG_DEBUG , " %s " , data ) ;
2009-05-20 23:04:05 +00:00
}
# endif
2006-04-04 17:22:06 +00:00
2007-03-30 00:15:25 +00:00
SWITCH_DECLARE ( void ) switch_rtp_init ( switch_memory_pool_t * pool )
2006-04-03 21:00:13 +00:00
{
2009-05-20 23:04:05 +00:00
# ifdef ENABLE_ZRTP
2011-02-02 21:43:26 +00:00
const char * zid_string = switch_core_get_variable_pdup ( " switch_serial " , pool ) ;
const char * zrtp_enabled = switch_core_get_variable_pdup ( " zrtp_enabled " , pool ) ;
2009-05-20 23:04:05 +00:00
zrtp_config_t zrtp_config ;
char zrtp_cache_path [ 256 ] = " " ;
2009-12-03 14:05:03 +00:00
zrtp_on = zrtp_enabled ? switch_true ( zrtp_enabled ) : 0 ;
2009-05-20 23:04:05 +00:00
# endif
2006-04-03 21:00:13 +00:00
if ( global_init ) {
return ;
}
2007-12-11 19:15:02 +00:00
switch_core_hash_init ( & alloc_hash , pool ) ;
2009-05-20 23:04:05 +00:00
# ifdef ENABLE_ZRTP
if ( zrtp_on ) {
zrtp_config_defaults ( & zrtp_config ) ;
strcpy ( zrtp_config . client_id , " FreeSWITCH " ) ;
2009-05-22 05:33:21 +00:00
zrtp_config . is_mitm = 1 ;
2009-05-20 23:04:05 +00:00
zrtp_config . lic_mode = ZRTP_LICENSE_MODE_ACTIVE ;
switch_snprintf ( zrtp_cache_path , sizeof ( zrtp_cache_path ) , " %s%szrtp.dat " , SWITCH_GLOBAL_dirs . db_dir , SWITCH_PATH_SEPARATOR ) ;
2012-01-25 05:38:53 +00:00
zrtp_zstrcpyc ( ( zrtp_stringn_t * ) zrtp_config . def_cache_path . buffer , zrtp_cache_path ) ;
2012-05-16 21:05:41 +00:00
zrtp_config . def_cache_path . length = ( uint16_t ) strlen ( zrtp_cache_path ) ;
2012-01-25 05:38:53 +00:00
zrtp_config . def_cache_path . max_length = 255 ;
2012-05-17 16:24:56 +00:00
zrtp_config . cb . event_cb . on_zrtp_protocol_event = ( void ( * ) ( zrtp_stream_t * , zrtp_protocol_event_t ) ) zrtp_event_callback ;
2009-05-20 23:04:05 +00:00
zrtp_config . cb . misc_cb . on_send_packet = zrtp_send_rtp_callback ;
2012-05-17 16:24:56 +00:00
zrtp_config . cb . event_cb . on_zrtp_security_event = ( void ( * ) ( zrtp_stream_t * , zrtp_security_event_t ) ) zrtp_event_callback ;
2010-02-06 03:38:24 +00:00
zrtp_log_set_log_engine ( ( zrtp_log_engine * ) zrtp_logger ) ;
2009-05-20 23:04:05 +00:00
zrtp_log_set_level ( 4 ) ;
2009-09-02 18:44:33 +00:00
if ( zrtp_status_ok = = zrtp_init ( & zrtp_config , & zrtp_global ) ) {
2010-02-06 03:38:24 +00:00
2009-09-02 18:44:33 +00:00
memcpy ( zid , zid_string , 12 ) ;
2010-02-06 03:38:24 +00:00
switch_scheduler_add_task ( switch_epoch_time_now ( NULL ) + 900 , zrtp_cache_save_callback , " zrtp_cache_save " , " core " , 0 , NULL ,
SSHF_NONE | SSHF_NO_DEL ) ;
2009-09-02 18:44:33 +00:00
} else {
switch_core_set_variable ( " zrtp_enabled " , NULL ) ;
zrtp_on = 0 ;
2012-04-11 14:10:56 +00:00
switch_log_printf ( SWITCH_CHANNEL_LOG , SWITCH_LOG_CRIT , " ZRTP init failed! \n " ) ;
2009-05-20 23:04:05 +00:00
}
2010-02-06 03:38:24 +00:00
2009-05-20 23:04:05 +00:00
}
# endif
2011-05-02 16:33:57 +00:00
# ifdef ENABLE_SRTP
2007-03-29 22:31:56 +00:00
srtp_init ( ) ;
2011-05-02 16:33:57 +00:00
# endif
2007-03-29 22:31:56 +00:00
switch_mutex_init ( & port_lock , SWITCH_MUTEX_NESTED , pool ) ;
global_init = 1 ;
2006-04-03 21:00:13 +00:00
}
2012-06-22 23:08:59 +00:00
static int check_srtp_and_ice ( switch_rtp_t * rtp_session )
{
int ret = 0 ;
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_AUTO_CNG ) & &
rtp_session - > timer . samplecount > = ( rtp_session - > last_write_samplecount + ( rtp_session - > samples_per_interval * 50 ) ) ) {
uint8_t data [ 10 ] = { 0 } ;
switch_frame_flag_t frame_flags = SFF_NONE ;
data [ 0 ] = 65 ;
rtp_session - > cn + + ;
rtp_common_write ( rtp_session , NULL , ( void * ) data , 2 , rtp_session - > cng_pt , 0 , & frame_flags ) ;
}
if ( rtp_session - > rtcp_sock_output & &
switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_ENABLE_RTCP ) & & ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_RTCP_PASSTHRU ) & &
rtp_session - > rtcp_interval & & ( rtp_session - > stats . read_count % rtp_session - > rtcp_interval ) = = 0 ) {
struct switch_rtcp_senderinfo * sr = ( struct switch_rtcp_senderinfo * ) rtp_session - > rtcp_send_msg . body ;
//rtp_msg_t *send_msg = &rtp_session->send_msg;
switch_size_t rtcp_bytes ;
switch_byte_t * ptr = ( switch_byte_t * ) rtp_session - > rtcp_send_msg . body ;
struct switch_rtcp_report_block * rb ;
rtp_session - > rtcp_send_msg . header . version = 2 ;
rtp_session - > rtcp_send_msg . header . p = 0 ;
rtp_session - > rtcp_send_msg . header . count = 1 ;
sr - > ssrc = htonl ( rtp_session - > ssrc ) ;
if ( ! rtp_session - > stats . inbound . period_packet_count ) {
rtp_session - > rtcp_send_msg . header . type = 201 ;
rtcp_bytes = sizeof ( switch_rtcp_hdr_t ) + 4 ;
ptr + = 4 ;
} else {
switch_time_t when ;
rtp_session - > rtcp_send_msg . header . type = 200 ;
if ( rtp_session - > send_time ) {
when = rtp_session - > send_time ;
} else {
when = switch_time_now ( ) ;
}
sr - > ntp_msw = htonl ( ( u_long ) when / 1000000 + 2208988800UL ) ;
sr - > ntp_lsw = htonl ( ( u_long ) ( when % 1000000 * ( ( UINT_MAX * 1.0 ) / 1000000.0 ) ) ) ;
sr - > ts = htonl ( rtp_session - > last_write_ts ) ;
sr - > pc = htonl ( rtp_session - > stats . outbound . packet_count ) ;
sr - > oc = htonl ( ( rtp_session - > stats . outbound . raw_bytes - rtp_session - > stats . outbound . packet_count * sizeof ( srtp_hdr_t ) ) ) ;
rtcp_bytes = sizeof ( switch_rtcp_hdr_t ) + sizeof ( struct switch_rtcp_senderinfo ) ;
ptr + = sizeof ( struct switch_rtcp_senderinfo ) ;
}
/* TBD need to put more accurate stats here. */
rb = ( struct switch_rtcp_report_block * ) ptr ;
rb - > ssrc = rtp_session - > recv_msg . header . ssrc ;
rb - > fraction = 0 ;
rb - > lost = rtp_session - > stats . inbound . skip_packet_count ;
rb - > highest_sequence_number_received = rtp_session - > recv_msg . header . seq ;
rb - > jitter = 0 ;
rb - > lsr = 0 ;
rb - > dlsr = 0 ;
rtcp_bytes + = sizeof ( struct switch_rtcp_report_block ) ;
rtp_session - > rtcp_send_msg . header . length = htons ( ( rtcp_bytes / 4 ) - 1 ) ;
# ifdef ENABLE_SRTP
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_SECURE_SEND ) ) {
int sbytes = ( int ) rtcp_bytes ;
int stat = srtp_protect_rtcp ( rtp_session - > send_ctx , & rtp_session - > rtcp_send_msg . header , & sbytes ) ;
if ( stat ) {
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " Error: SRTP RTCP protection failed with code %d \n " , stat ) ;
}
rtcp_bytes = sbytes ;
}
# endif
# ifdef ENABLE_ZRTP
/* ZRTP Send */
if ( zrtp_on & & ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_PROXY_MEDIA ) ) {
2012-06-23 19:03:06 +00:00
unsigned int sbytes = ( int ) rtcp_bytes ;
2012-06-22 23:08:59 +00:00
zrtp_status_t stat = zrtp_status_fail ;
stat = zrtp_process_rtcp ( rtp_session - > zrtp_stream , ( void * ) & rtp_session - > rtcp_send_msg , & sbytes ) ;
switch ( stat ) {
case zrtp_status_ok :
break ;
case zrtp_status_drop :
switch_log_printf ( SWITCH_CHANNEL_LOG , SWITCH_LOG_ERROR , " Error: zRTP protection drop with code %d \n " , stat ) ;
2012-06-23 19:03:06 +00:00
ret = ( int ) rtcp_bytes ;
2012-06-22 23:08:59 +00:00
goto end ;
break ;
case zrtp_status_fail :
switch_log_printf ( SWITCH_CHANNEL_LOG , SWITCH_LOG_ERROR , " Error: zRTP protection fail with code %d \n " , stat ) ;
break ;
default :
break ;
}
2012-06-23 19:03:06 +00:00
rtcp_bytes = sbytes ;
2012-06-22 23:08:59 +00:00
}
# endif
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 ) {
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_DEBUG , " RTCP packet not written \n " ) ;
} else {
rtp_session - > stats . inbound . period_packet_count = 0 ;
}
if ( rtp_session - > rtcp_ice . ice_user ) {
ice_out ( rtp_session , & rtp_session - > rtcp_ice ) ;
}
}
if ( rtp_session - > remote_stun_addr ) {
do_stun_ping ( rtp_session ) ;
}
if ( rtp_session - > ice . ice_user ) {
if ( ice_out ( rtp_session , & rtp_session - > ice ) ! = SWITCH_STATUS_SUCCESS ) {
ret = - 1 ;
goto end ;
}
}
if ( rtp_session - > rtcp_ice . ice_user ) {
if ( ice_out ( rtp_session , & rtp_session - > rtcp_ice ) ! = SWITCH_STATUS_SUCCESS ) {
ret = - 1 ;
goto end ;
}
}
end :
return ret ;
}
SWITCH_DECLARE ( void ) switch_rtp_ping ( switch_rtp_t * rtp_session )
{
check_srtp_and_ice ( rtp_session ) ;
}
2008-01-16 06:01:53 +00:00
SWITCH_DECLARE ( void ) switch_rtp_get_random ( void * buf , uint32_t len )
{
2011-05-02 16:33:57 +00:00
# ifdef ENABLE_SRTP
2008-01-16 06:01:53 +00:00
crypto_get_random ( buf , len ) ;
2011-05-02 16:33:57 +00:00
# else
switch_stun_random_string ( buf , len , NULL ) ;
# endif
2008-01-16 06:01:53 +00:00
}
2007-12-11 19:15:02 +00:00
SWITCH_DECLARE ( void ) switch_rtp_shutdown ( void )
{
2009-03-03 20:16:05 +00:00
switch_core_port_allocator_t * alloc = NULL ;
switch_hash_index_t * hi ;
2010-02-06 03:38:24 +00:00
const void * var ;
void * val ;
2009-03-03 20:16:05 +00:00
2010-09-14 16:48:43 +00:00
if ( ! global_init ) {
return ;
}
2009-03-03 20:16:05 +00:00
switch_mutex_lock ( port_lock ) ;
for ( hi = switch_hash_first ( NULL , alloc_hash ) ; hi ; hi = switch_hash_next ( hi ) ) {
switch_hash_this ( hi , & var , NULL , & val ) ;
if ( ( alloc = ( switch_core_port_allocator_t * ) val ) ) {
2010-02-06 03:38:24 +00:00
switch_log_printf ( SWITCH_CHANNEL_LOG , SWITCH_LOG_DEBUG , " Destroy port allocator for %s \n " , ( char * ) var ) ;
2009-03-03 20:16:05 +00:00
switch_core_port_allocator_destroy ( & alloc ) ;
}
}
2007-12-11 19:15:02 +00:00
switch_core_hash_destroy ( & alloc_hash ) ;
2009-03-03 20:16:05 +00:00
switch_mutex_unlock ( port_lock ) ;
2009-05-20 23:04:05 +00:00
# ifdef ENABLE_ZRTP
2009-05-20 23:53:44 +00:00
if ( zrtp_on ) {
2009-05-22 05:33:21 +00:00
zrtp_status_t status = zrtp_status_ok ;
status = zrtp_def_cache_store ( zrtp_global ) ;
switch_log_printf ( SWITCH_CHANNEL_LOG , SWITCH_LOG_DEBUG , " Saving ZRTP cache: %s \n " , zrtp_status_ok = = status ? " OK " : " FAIL " ) ;
2009-05-20 23:53:44 +00:00
zrtp_down ( zrtp_global ) ;
}
2009-05-20 23:04:05 +00:00
# endif
2011-05-02 16:33:57 +00:00
# ifdef ENABLE_SRTP
2009-03-03 20:16:05 +00:00
crypto_kernel_shutdown ( ) ;
2011-05-02 16:33:57 +00:00
# endif
2009-03-03 20:16:05 +00:00
2007-12-11 19:15:02 +00:00
}
2007-04-07 03:07:43 +00:00
SWITCH_DECLARE ( switch_port_t ) switch_rtp_set_start_port ( switch_port_t port )
{
if ( port ) {
if ( port_lock ) {
switch_mutex_lock ( port_lock ) ;
}
if ( NEXT_PORT = = START_PORT ) {
NEXT_PORT = port ;
}
START_PORT = port ;
if ( NEXT_PORT < START_PORT ) {
NEXT_PORT = START_PORT ;
}
if ( port_lock ) {
switch_mutex_unlock ( port_lock ) ;
}
2008-05-27 04:30:03 +00:00
}
return START_PORT ;
2007-04-07 03:07:43 +00:00
}
SWITCH_DECLARE ( switch_port_t ) switch_rtp_set_end_port ( switch_port_t port )
{
if ( port ) {
if ( port_lock ) {
switch_mutex_lock ( port_lock ) ;
}
END_PORT = port ;
if ( NEXT_PORT > END_PORT ) {
NEXT_PORT = START_PORT ;
}
if ( port_lock ) {
switch_mutex_unlock ( port_lock ) ;
}
2008-05-27 04:30:03 +00:00
}
return END_PORT ;
2007-04-07 03:07:43 +00:00
}
2007-12-20 15:41:45 +00:00
SWITCH_DECLARE ( void ) switch_rtp_release_port ( const char * ip , switch_port_t port )
2006-04-05 20:17:22 +00:00
{
2007-12-11 19:15:02 +00:00
switch_core_port_allocator_t * alloc = NULL ;
2007-12-20 15:41:45 +00:00
if ( ! ip ) {
2007-12-11 19:15:02 +00:00
return ;
}
2008-05-27 04:30:03 +00:00
switch_mutex_lock ( port_lock ) ;
if ( ( alloc = switch_core_hash_find ( alloc_hash , ip ) ) ) {
2007-12-13 15:10:55 +00:00
switch_core_port_allocator_free_port ( alloc , port ) ;
2007-12-11 19:15:02 +00:00
}
switch_mutex_unlock ( port_lock ) ;
}
2006-04-05 20:17:22 +00:00
2007-12-11 19:15:02 +00:00
SWITCH_DECLARE ( switch_port_t ) switch_rtp_request_port ( const char * ip )
{
switch_port_t port = 0 ;
switch_core_port_allocator_t * alloc = NULL ;
2008-05-27 04:30:03 +00:00
2006-04-05 20:17:22 +00:00
switch_mutex_lock ( port_lock ) ;
2007-12-11 19:15:02 +00:00
alloc = switch_core_hash_find ( alloc_hash , ip ) ;
if ( ! alloc ) {
if ( switch_core_port_allocator_new ( START_PORT , END_PORT , SPF_EVEN , & alloc ) ! = SWITCH_STATUS_SUCCESS ) {
2007-12-13 17:25:33 +00:00
abort ( ) ;
2007-12-11 19:15:02 +00:00
}
switch_core_hash_insert ( alloc_hash , ip , alloc ) ;
2006-04-05 20:17:22 +00:00
}
2008-05-27 04:30:03 +00:00
2007-12-11 19:15:02 +00:00
if ( switch_core_port_allocator_request_port ( alloc , & port ) ! = SWITCH_STATUS_SUCCESS ) {
port = 0 ;
}
2006-04-05 20:17:22 +00:00
switch_mutex_unlock ( port_lock ) ;
return port ;
}
2008-12-29 21:23:17 +00:00
SWITCH_DECLARE ( void ) switch_rtp_intentional_bugs ( switch_rtp_t * rtp_session , switch_rtp_bug_flag_t bugs )
{
rtp_session - > rtp_bugs = bugs ;
2010-11-11 00:08:55 +00:00
if ( ( rtp_session - > rtp_bugs & RTP_BUG_START_SEQ_AT_ZERO ) ) {
rtp_session - > seq = 0 ;
}
2008-12-29 21:23:17 +00:00
}
2010-04-20 00:07:23 +00:00
static switch_status_t enable_remote_rtcp_socket ( switch_rtp_t * rtp_session , const char * * err ) {
switch_status_t status = SWITCH_STATUS_SUCCESS ;
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_ENABLE_RTCP ) ) {
2010-06-24 21:01:55 +00:00
if ( switch_sockaddr_info_get ( & rtp_session - > rtcp_remote_addr , rtp_session - > eff_remote_host_str , SWITCH_UNSPEC ,
2010-04-20 21:15:37 +00:00
rtp_session - > remote_rtcp_port , 0 , rtp_session - > pool ) ! = SWITCH_STATUS_SUCCESS | | ! rtp_session - > rtcp_remote_addr ) {
2010-04-20 00:07:23 +00:00
* err = " RTCP Remote Address Error! " ;
return SWITCH_STATUS_FALSE ;
2010-04-22 15:22:28 +00:00
} else {
const char * host ;
char bufa [ 30 ] ;
2012-04-10 21:17:16 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
2010-04-22 15:22:28 +00:00
host = switch_get_addr ( bufa , sizeof ( bufa ) , rtp_session - > rtcp_remote_addr ) ;
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_DEBUG , " Setting RTCP remote addr to %s:%d \n " , host , rtp_session - > remote_rtcp_port ) ;
2010-04-20 00:07:23 +00:00
}
2010-04-22 15:22:28 +00:00
if ( ! ( rtp_session - > rtcp_sock_input & & rtp_session - > rtcp_sock_output ) ) {
if ( rtp_session - > rtcp_sock_input & & switch_sockaddr_get_family ( rtp_session - > rtcp_remote_addr ) = =
switch_sockaddr_get_family ( rtp_session - > rtcp_local_addr ) ) {
rtp_session - > rtcp_sock_output = rtp_session - > rtcp_sock_input ;
} else {
if ( rtp_session - > rtcp_sock_output & & rtp_session - > rtcp_sock_output ! = rtp_session - > rtcp_sock_input ) {
switch_socket_close ( rtp_session - > rtcp_sock_output ) ;
}
if ( ( status = switch_socket_create ( & rtp_session - > rtcp_sock_output ,
switch_sockaddr_get_family ( rtp_session - > rtcp_remote_addr ) ,
SOCK_DGRAM , 0 , rtp_session - > pool ) ) ! = SWITCH_STATUS_SUCCESS ) {
* err = " RTCP Socket Error! " ;
}
2010-04-20 00:07:23 +00:00
}
}
} else {
* err = " RTCP NOT ACTIVE! " ;
}
return status ;
}
static switch_status_t enable_local_rtcp_socket ( switch_rtp_t * rtp_session , const char * * err ) {
const char * host = rtp_session - > local_host_str ;
switch_port_t port = rtp_session - > local_port ;
switch_socket_t * rtcp_new_sock = NULL , * rtcp_old_sock = NULL ;
switch_status_t status = SWITCH_STATUS_SUCCESS ;
char bufa [ 30 ] ;
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_ENABLE_RTCP ) ) {
if ( switch_sockaddr_info_get ( & rtp_session - > rtcp_local_addr , host , SWITCH_UNSPEC , port + 1 , 0 , rtp_session - > pool ) ! = SWITCH_STATUS_SUCCESS ) {
* err = " RTCP Local Address Error! " ;
goto done ;
}
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 ) {
* err = " RTCP Socket Error! " ;
goto done ;
}
if ( switch_socket_opt_set ( rtcp_new_sock , SWITCH_SO_REUSEADDR , 1 ) ! = SWITCH_STATUS_SUCCESS ) {
* err = " RTCP Socket Error! " ;
goto done ;
}
if ( switch_socket_bind ( rtcp_new_sock , rtp_session - > rtcp_local_addr ) ! = SWITCH_STATUS_SUCCESS ) {
* err = " RTCP Bind Error! " ;
goto done ;
}
if ( switch_sockaddr_info_get ( & rtp_session - > rtcp_from_addr , switch_get_addr ( bufa , sizeof ( bufa ) , rtp_session - > from_addr ) ,
SWITCH_UNSPEC , switch_sockaddr_get_port ( rtp_session - > from_addr ) + 1 , 0 , rtp_session - > pool ) ! = SWITCH_STATUS_SUCCESS ) {
* err = " RTCP From Address Error! " ;
goto done ;
}
rtcp_old_sock = rtp_session - > rtcp_sock_input ;
rtp_session - > rtcp_sock_input = rtcp_new_sock ;
rtcp_new_sock = NULL ;
switch_socket_create_pollset ( & rtp_session - > rtcp_read_pollfd , rtp_session - > rtcp_sock_input , SWITCH_POLLIN | SWITCH_POLLERR , rtp_session - > pool ) ;
done :
if ( * err ) {
2012-04-10 21:17:16 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " Error allocating rtcp [%s] \n " , * err ) ;
2010-04-20 00:07:23 +00:00
status = SWITCH_STATUS_FALSE ;
}
if ( rtcp_new_sock ) {
switch_socket_close ( rtcp_new_sock ) ;
}
if ( rtcp_old_sock ) {
switch_socket_close ( rtcp_old_sock ) ;
}
} else {
status = SWITCH_STATUS_FALSE ;
}
return status ;
}
2007-05-12 14:48:14 +00:00
SWITCH_DECLARE ( switch_status_t ) switch_rtp_set_local_address ( switch_rtp_t * rtp_session , const char * host , switch_port_t port , const char * * err )
2006-04-06 20:56:29 +00:00
{
2007-02-17 22:13:19 +00:00
switch_socket_t * new_sock = NULL , * old_sock = NULL ;
switch_status_t status = SWITCH_STATUS_FALSE ;
2007-12-17 18:24:26 +00:00
# ifndef WIN32
2007-04-20 00:29:18 +00:00
char o [ 5 ] = " TEST " , i [ 5 ] = " " ;
2007-12-04 21:55:48 +00:00
switch_size_t len , ilen = 0 ;
int x ;
2007-12-17 18:24:26 +00:00
# endif
2007-12-04 21:55:48 +00:00
2008-02-25 16:35:19 +00:00
if ( rtp_session - > ready ! = 1 ) {
if ( ! switch_rtp_ready ( rtp_session ) ) {
return SWITCH_STATUS_FALSE ;
}
WRITE_INC ( rtp_session ) ;
READ_INC ( rtp_session ) ;
if ( ! switch_rtp_ready ( rtp_session ) ) {
goto done ;
}
}
2008-01-18 18:14:53 +00:00
2007-04-20 20:53:11 +00:00
* err = NULL ;
2007-03-29 22:31:56 +00:00
2009-10-23 16:03:42 +00:00
if ( zstr ( host ) | | ! port ) {
2008-03-07 20:13:40 +00:00
* err = " Address Error " ;
goto done ;
}
2010-04-20 00:07:23 +00:00
rtp_session - > local_host_str = switch_core_strdup ( rtp_session - > pool , host ) ;
rtp_session - > local_port = port ;
2007-03-30 00:13:31 +00:00
if ( switch_sockaddr_info_get ( & rtp_session - > local_addr , host , SWITCH_UNSPEC , port , 0 , rtp_session - > pool ) ! = SWITCH_STATUS_SUCCESS ) {
2006-04-06 20:56:29 +00:00
* err = " Local Address Error! " ;
2007-02-17 22:13:19 +00:00
goto done ;
2006-04-04 02:28:11 +00:00
}
2010-04-20 00:07:23 +00:00
2008-07-03 18:50:15 +00:00
if ( rtp_session - > sock_input ) {
2006-08-30 19:46:43 +00:00
switch_rtp_kill_socket ( rtp_session ) ;
2006-04-03 21:00:13 +00:00
}
2007-03-29 22:31:56 +00:00
2008-07-03 18:50:15 +00:00
if ( switch_socket_create ( & new_sock , switch_sockaddr_get_family ( rtp_session - > local_addr ) , SOCK_DGRAM , 0 , rtp_session - > pool ) ! = SWITCH_STATUS_SUCCESS ) {
2006-04-04 02:28:11 +00:00
* err = " Socket Error! " ;
2007-02-17 22:13:19 +00:00
goto done ;
2006-04-04 02:28:11 +00:00
}
2006-04-03 21:00:13 +00:00
2007-02-17 22:13:19 +00:00
if ( switch_socket_opt_set ( new_sock , SWITCH_SO_REUSEADDR , 1 ) ! = SWITCH_STATUS_SUCCESS ) {
2006-10-06 22:39:49 +00:00
* err = " Socket Error! " ;
2007-02-17 22:13:19 +00:00
goto done ;
2006-10-06 22:39:49 +00:00
}
2010-04-20 00:07:23 +00:00
2007-02-17 22:13:19 +00:00
if ( switch_socket_bind ( new_sock , rtp_session - > local_addr ) ! = SWITCH_STATUS_SUCCESS ) {
2011-08-02 04:55:58 +00:00
char * em = switch_core_sprintf ( rtp_session - > pool , " Bind Error! %s:%d " , host , port ) ;
* err = em ;
2007-02-17 22:13:19 +00:00
goto done ;
2006-04-04 02:28:11 +00:00
}
2010-04-20 00:07:23 +00:00
2007-12-17 18:24:26 +00:00
# ifndef WIN32
2007-04-20 00:29:18 +00:00
len = sizeof ( i ) ;
2007-12-04 21:55:48 +00:00
switch_socket_opt_set ( new_sock , SWITCH_SO_NONBLOCK , TRUE ) ;
2007-04-20 00:29:18 +00:00
switch_socket_sendto ( new_sock , rtp_session - > local_addr , 0 , ( void * ) o , & len ) ;
2007-12-04 21:55:48 +00:00
x = 0 ;
2008-05-27 04:30:03 +00:00
while ( ! ilen ) {
2007-12-04 23:06:12 +00:00
switch_status_t status ;
2007-12-04 21:55:48 +00:00
ilen = len ;
2007-12-04 23:06:12 +00:00
status = switch_socket_recvfrom ( rtp_session - > from_addr , new_sock , 0 , ( void * ) i , & ilen ) ;
2007-12-04 21:55:48 +00:00
if ( status ! = SWITCH_STATUS_SUCCESS & & status ! = SWITCH_STATUS_BREAK ) {
break ;
}
2008-03-07 20:13:40 +00:00
if ( + + x > 1000 ) {
2007-12-04 21:55:48 +00:00
break ;
}
2008-11-14 23:31:21 +00:00
switch_cond_next ( ) ;
2007-12-04 21:55:48 +00:00
}
switch_socket_opt_set ( new_sock , SWITCH_SO_NONBLOCK , FALSE ) ;
2010-04-28 22:34:57 +00:00
# endif
2007-12-17 18:24:26 +00:00
2008-07-03 18:50:15 +00:00
old_sock = rtp_session - > sock_input ;
rtp_session - > sock_input = new_sock ;
2007-02-17 22:13:19 +00:00
new_sock = NULL ;
2007-04-19 21:40:50 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_USE_TIMER ) | | switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_NOBLOCK ) ) {
2008-07-03 18:50:15 +00:00
switch_socket_opt_set ( rtp_session - > sock_input , SWITCH_SO_NONBLOCK , TRUE ) ;
2006-08-28 23:05:26 +00:00
switch_set_flag_locked ( rtp_session , SWITCH_RTP_FLAG_NOBLOCK ) ;
2006-04-07 17:32:14 +00:00
}
2007-02-17 22:13:19 +00:00
2010-02-17 22:36:07 +00:00
switch_socket_create_pollset ( & rtp_session - > read_pollfd , rtp_session - > sock_input , SWITCH_POLLIN | SWITCH_POLLERR , rtp_session - > pool ) ;
2009-03-31 19:10:43 +00:00
2010-04-20 00:07:23 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_ENABLE_RTCP ) ) {
if ( ( status = enable_local_rtcp_socket ( rtp_session , err ) ) = = SWITCH_STATUS_SUCCESS ) {
* err = " Success " ;
}
} else {
status = SWITCH_STATUS_SUCCESS ;
* err = " Success " ;
}
2006-08-28 23:05:26 +00:00
switch_set_flag_locked ( rtp_session , SWITCH_RTP_FLAG_IO ) ;
2007-02-17 22:13:19 +00:00
2010-04-20 00:07:23 +00:00
done :
2007-02-17 22:13:19 +00:00
if ( new_sock ) {
switch_socket_close ( new_sock ) ;
}
if ( old_sock ) {
switch_socket_close ( old_sock ) ;
}
2008-05-27 04:30:03 +00:00
2010-04-20 00:07:23 +00:00
2008-02-25 16:35:19 +00:00
if ( rtp_session - > ready ! = 1 ) {
WRITE_DEC ( rtp_session ) ;
READ_DEC ( rtp_session ) ;
}
2007-02-17 22:13:19 +00:00
return status ;
2006-04-06 20:56:29 +00:00
}
2007-10-31 02:12:30 +00:00
SWITCH_DECLARE ( void ) switch_rtp_set_max_missed_packets ( switch_rtp_t * rtp_session , uint32_t max )
{
2009-03-19 01:06:21 +00:00
if ( rtp_session - > missed_count > = max ) {
2012-04-10 21:17:16 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_WARNING ,
2010-02-06 03:38:24 +00:00
" new max missed packets(%d->%d) greater than current missed packets(%d). RTP will timeout. \n " ,
2009-03-19 01:06:21 +00:00
rtp_session - > missed_count , max , rtp_session - > missed_count ) ;
}
2009-03-18 20:36:30 +00:00
2007-10-31 02:12:30 +00:00
rtp_session - > max_missed_packets = max ;
}
2009-01-13 19:02:48 +00:00
SWITCH_DECLARE ( void ) switch_rtp_reset_media_timer ( switch_rtp_t * rtp_session )
{
rtp_session - > missed_count = 0 ;
}
2008-09-18 21:50:18 +00:00
SWITCH_DECLARE ( char * ) switch_rtp_get_remote_host ( switch_rtp_t * rtp_session )
{
2009-10-23 16:03:42 +00:00
return zstr ( rtp_session - > remote_host_str ) ? " 0.0.0.0 " : rtp_session - > remote_host_str ;
2008-09-18 21:50:18 +00:00
}
SWITCH_DECLARE ( switch_port_t ) switch_rtp_get_remote_port ( switch_rtp_t * rtp_session )
{
return rtp_session - > remote_port ;
}
2010-06-17 15:23:37 +00:00
static void ping_socket ( switch_rtp_t * rtp_session )
{
uint32_t o = UINT_MAX ;
switch_size_t len = sizeof ( o ) ;
switch_socket_sendto ( rtp_session - > sock_input , rtp_session - > local_addr , 0 , ( void * ) & o , & len ) ;
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_ENABLE_RTCP ) & & rtp_session - > rtcp_sock_input ) {
switch_socket_sendto ( rtp_session - > rtcp_sock_input , rtp_session - > rtcp_local_addr , 0 , ( void * ) & o , & len ) ;
}
}
2008-09-18 21:50:18 +00:00
2010-05-13 02:25:54 +00:00
SWITCH_DECLARE ( switch_status_t ) switch_rtp_udptl_mode ( switch_rtp_t * rtp_session )
{
2010-06-17 15:23:37 +00:00
switch_socket_t * sock ;
2010-05-13 02:25:54 +00:00
READ_INC ( rtp_session ) ;
WRITE_INC ( rtp_session ) ;
2011-05-12 15:32:39 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_USE_TIMER ) | | rtp_session - > timer . timer_interface ) {
2010-05-13 02:25:54 +00:00
switch_core_timer_destroy ( & rtp_session - > timer ) ;
memset ( & rtp_session - > timer , 0 , sizeof ( rtp_session - > timer ) ) ;
2011-05-12 15:32:39 +00:00
switch_clear_flag_locked ( rtp_session , SWITCH_RTP_FLAG_USE_TIMER ) ;
2010-05-13 02:25:54 +00:00
}
2010-06-17 15:23:37 +00:00
switch_clear_flag ( rtp_session , SWITCH_RTP_FLAG_ENABLE_RTCP ) ;
if ( rtp_session - > rtcp_sock_input ) {
ping_socket ( rtp_session ) ;
switch_socket_shutdown ( rtp_session - > rtcp_sock_input , SWITCH_SHUTDOWN_READWRITE ) ;
}
if ( rtp_session - > rtcp_sock_output & & rtp_session - > rtcp_sock_output ! = rtp_session - > rtcp_sock_input ) {
switch_socket_shutdown ( rtp_session - > rtcp_sock_output , SWITCH_SHUTDOWN_READWRITE ) ;
}
if ( ( sock = rtp_session - > rtcp_sock_input ) ) {
rtp_session - > rtcp_sock_input = NULL ;
switch_socket_close ( sock ) ;
if ( rtp_session - > rtcp_sock_output & & rtp_session - > rtcp_sock_output ! = sock ) {
if ( ( sock = rtp_session - > rtcp_sock_output ) ) {
rtp_session - > rtcp_sock_output = NULL ;
switch_socket_close ( sock ) ;
}
}
}
2010-05-13 02:25:54 +00:00
switch_set_flag_locked ( rtp_session , SWITCH_RTP_FLAG_UDPTL ) ;
switch_set_flag_locked ( rtp_session , SWITCH_RTP_FLAG_PROXY_MEDIA ) ;
switch_socket_opt_set ( rtp_session - > sock_input , SWITCH_SO_NONBLOCK , FALSE ) ;
2011-05-12 15:32:39 +00:00
switch_clear_flag_locked ( rtp_session , SWITCH_RTP_FLAG_NOBLOCK ) ;
2010-05-13 02:25:54 +00:00
WRITE_DEC ( rtp_session ) ;
READ_DEC ( rtp_session ) ;
2010-05-27 01:12:54 +00:00
switch_clear_flag_locked ( rtp_session , SWITCH_RTP_FLAG_STICKY_FLUSH ) ;
switch_clear_flag_locked ( rtp_session , SWITCH_RTP_FLAG_FLUSH ) ;
2010-05-13 02:25:54 +00:00
2010-05-27 01:12:54 +00:00
switch_rtp_break ( rtp_session ) ;
2010-05-13 02:25:54 +00:00
return SWITCH_STATUS_SUCCESS ;
}
2010-04-22 15:22:28 +00:00
SWITCH_DECLARE ( 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 ,
2008-09-29 23:03:27 +00:00
switch_bool_t change_adv_addr , const char * * err )
2006-04-06 20:56:29 +00:00
{
2008-01-19 02:27:26 +00:00
switch_sockaddr_t * remote_addr ;
2008-07-03 18:50:15 +00:00
switch_status_t status = SWITCH_STATUS_SUCCESS ;
2006-04-06 20:56:29 +00:00
* err = " Success " ;
2008-05-27 04:30:03 +00:00
2008-01-19 02:27:26 +00:00
if ( switch_sockaddr_info_get ( & remote_addr , host , SWITCH_UNSPEC , port , 0 , rtp_session - > pool ) ! = SWITCH_STATUS_SUCCESS | | ! remote_addr ) {
2006-04-06 20:56:29 +00:00
* err = " Remote Address Error! " ;
return SWITCH_STATUS_FALSE ;
}
2010-04-20 00:07:23 +00:00
2008-01-19 02:27:26 +00:00
switch_mutex_lock ( rtp_session - > write_mutex ) ;
2008-07-03 18:50:15 +00:00
2008-01-19 02:27:26 +00:00
rtp_session - > remote_addr = remote_addr ;
2008-09-29 23:03:27 +00:00
if ( change_adv_addr ) {
rtp_session - > remote_host_str = switch_core_strdup ( rtp_session - > pool , host ) ;
rtp_session - > remote_port = port ;
}
2008-07-03 18:50:15 +00:00
2010-06-24 21:01:55 +00:00
rtp_session - > eff_remote_host_str = switch_core_strdup ( rtp_session - > pool , host ) ;
rtp_session - > eff_remote_port = port ;
2010-02-06 03:38:24 +00:00
if ( rtp_session - > sock_input & & switch_sockaddr_get_family ( rtp_session - > remote_addr ) = = switch_sockaddr_get_family ( rtp_session - > local_addr ) ) {
rtp_session - > sock_output = rtp_session - > sock_input ;
} else {
if ( rtp_session - > sock_output & & rtp_session - > sock_output ! = rtp_session - > sock_input ) {
switch_socket_close ( rtp_session - > sock_output ) ;
2008-07-03 18:50:15 +00:00
}
if ( ( status = switch_socket_create ( & rtp_session - > sock_output ,
2010-02-06 03:38:24 +00:00
switch_sockaddr_get_family ( rtp_session - > remote_addr ) ,
SOCK_DGRAM , 0 , rtp_session - > pool ) ) ! = SWITCH_STATUS_SUCCESS ) {
2008-07-03 18:50:15 +00:00
* err = " Socket Error! " ;
}
}
2010-04-20 00:07:23 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_ENABLE_RTCP ) ) {
2010-04-22 15:22:28 +00:00
if ( remote_rtcp_port ) {
rtp_session - > remote_rtcp_port = remote_rtcp_port ;
} else {
2010-06-24 21:27:11 +00:00
rtp_session - > remote_rtcp_port = rtp_session - > eff_remote_port + 1 ;
2010-04-22 15:22:28 +00:00
}
2010-04-20 00:07:23 +00:00
status = enable_remote_rtcp_socket ( rtp_session , err ) ;
}
2008-01-19 02:27:26 +00:00
switch_mutex_unlock ( rtp_session - > write_mutex ) ;
2006-04-17 18:25:43 +00:00
2008-07-03 18:50:15 +00:00
return status ;
2006-04-06 20:56:29 +00:00
}
2008-01-16 06:01:53 +00:00
SWITCH_DECLARE ( switch_status_t ) switch_rtp_add_crypto_key ( switch_rtp_t * rtp_session ,
switch_rtp_crypto_direction_t direction ,
2008-05-27 04:30:03 +00:00
uint32_t index , switch_rtp_crypto_key_type_t type , unsigned char * key , switch_size_t keylen )
2008-01-16 06:01:53 +00:00
{
2011-05-02 16:33:57 +00:00
# ifndef ENABLE_SRTP
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_CRIT , " SRTP NOT SUPPORTED IN THIS BUILD! \n " ) ;
2011-05-02 16:33:57 +00:00
return SWITCH_STATUS_FALSE ;
# else
2008-01-16 06:01:53 +00:00
switch_rtp_crypto_key_t * crypto_key ;
srtp_policy_t * policy ;
err_status_t stat ;
2008-01-17 04:16:04 +00:00
switch_status_t status = SWITCH_STATUS_SUCCESS ;
2009-05-29 02:08:24 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
switch_channel_t * channel = switch_core_session_get_channel ( session ) ;
switch_event_t * fsevent = NULL ;
2008-01-16 06:01:53 +00:00
2008-01-17 06:07:17 +00:00
if ( direction > = SWITCH_RTP_CRYPTO_MAX | | keylen > SWITCH_RTP_MAX_CRYPTO_LEN ) {
return SWITCH_STATUS_FALSE ;
2008-05-27 04:30:03 +00:00
}
2008-01-17 06:07:17 +00:00
2008-01-16 06:01:53 +00:00
crypto_key = switch_core_alloc ( rtp_session - > pool , sizeof ( * crypto_key ) ) ;
2008-05-27 04:30:03 +00:00
2008-01-17 04:16:04 +00:00
if ( direction = = SWITCH_RTP_CRYPTO_RECV ) {
policy = & rtp_session - > recv_policy ;
} else {
policy = & rtp_session - > send_policy ;
}
2008-01-16 06:01:53 +00:00
crypto_key - > type = type ;
crypto_key - > index = index ;
memcpy ( crypto_key - > key , key , keylen ) ;
crypto_key - > next = rtp_session - > crypto_keys [ direction ] ;
rtp_session - > crypto_keys [ direction ] = crypto_key ;
memset ( policy , 0 , sizeof ( * policy ) ) ;
2010-02-06 03:38:24 +00:00
2008-05-27 04:30:03 +00:00
switch ( crypto_key - > type ) {
2008-01-16 06:01:53 +00:00
case AES_CM_128_HMAC_SHA1_80 :
crypto_policy_set_aes_cm_128_hmac_sha1_80 ( & policy - > rtp ) ;
2009-06-09 23:17:50 +00:00
if ( switch_channel_direction ( channel ) = = SWITCH_CALL_DIRECTION_OUTBOUND ) {
switch_channel_set_variable ( channel , " sip_has_crypto " , " AES_CM_128_HMAC_SHA1_80 " ) ;
}
2008-01-16 06:01:53 +00:00
break ;
case AES_CM_128_HMAC_SHA1_32 :
crypto_policy_set_aes_cm_128_hmac_sha1_32 ( & policy - > rtp ) ;
2009-06-09 23:17:50 +00:00
if ( switch_channel_direction ( channel ) = = SWITCH_CALL_DIRECTION_OUTBOUND ) {
switch_channel_set_variable ( channel , " sip_has_crypto " , " AES_CM_128_HMAC_SHA1_32 " ) ;
}
2008-01-16 06:01:53 +00:00
break ;
2008-07-07 17:56:16 +00:00
case AES_CM_128_NULL_AUTH :
crypto_policy_set_aes_cm_128_null_auth ( & policy - > rtp ) ;
2009-06-09 23:17:50 +00:00
if ( switch_channel_direction ( channel ) = = SWITCH_CALL_DIRECTION_OUTBOUND ) {
switch_channel_set_variable ( channel , " sip_has_crypto " , " AES_CM_128_NULL_AUTH " ) ;
}
2008-07-07 17:56:16 +00:00
break ;
2008-01-16 06:01:53 +00:00
default :
break ;
}
2008-05-27 04:30:03 +00:00
policy - > next = NULL ;
2008-01-16 06:01:53 +00:00
policy - > key = ( uint8_t * ) crypto_key - > key ;
2010-04-20 00:07:23 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_ENABLE_RTCP ) ) {
crypto_policy_set_rtcp_default ( & policy - > rtcp ) ;
policy - > rtcp . sec_serv = sec_serv_none ;
}
2008-01-16 06:01:53 +00:00
policy - > rtp . sec_serv = sec_serv_conf_and_auth ;
2008-05-27 04:30:03 +00:00
switch ( direction ) {
2008-01-16 06:01:53 +00:00
case SWITCH_RTP_CRYPTO_RECV :
policy - > ssrc . type = ssrc_any_inbound ;
2008-01-17 04:16:04 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_SECURE_RECV ) ) {
switch_set_flag ( rtp_session , SWITCH_RTP_FLAG_SECURE_RECV_RESET ) ;
} else {
if ( ( stat = srtp_create ( & rtp_session - > recv_ctx , policy ) ) ) {
status = SWITCH_STATUS_FALSE ;
}
if ( status = = SWITCH_STATUS_SUCCESS ) {
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_INFO , " Activating Secure RTP RECV \n " ) ;
2008-01-17 04:16:04 +00:00
switch_set_flag ( rtp_session , SWITCH_RTP_FLAG_SECURE_RECV ) ;
} else {
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " Error allocating srtp [%d] \n " , stat ) ;
2008-01-17 04:16:04 +00:00
return status ;
}
2008-01-16 06:01:53 +00:00
}
break ;
case SWITCH_RTP_CRYPTO_SEND :
policy - > ssrc . type = ssrc_specific ;
policy - > ssrc . value = rtp_session - > ssrc ;
2008-01-17 04:16:04 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_SECURE_SEND ) ) {
switch_set_flag ( rtp_session , SWITCH_RTP_FLAG_SECURE_SEND_RESET ) ;
} else {
if ( ( stat = srtp_create ( & rtp_session - > send_ctx , policy ) ) ) {
status = SWITCH_STATUS_FALSE ;
}
if ( status = = SWITCH_STATUS_SUCCESS ) {
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_INFO , " Activating Secure RTP SEND \n " ) ;
2008-01-17 04:16:04 +00:00
switch_set_flag ( rtp_session , SWITCH_RTP_FLAG_SECURE_SEND ) ;
} else {
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " Error allocating SRTP [%d] \n " , stat ) ;
2008-01-17 04:16:04 +00:00
return status ;
}
2008-01-16 06:01:53 +00:00
}
2008-01-17 04:16:04 +00:00
2008-01-16 06:01:53 +00:00
break ;
default :
abort ( ) ;
break ;
}
2009-05-29 02:08:24 +00:00
if ( switch_event_create ( & fsevent , SWITCH_EVENT_CALL_SECURE ) = = SWITCH_STATUS_SUCCESS ) {
switch_event_add_header ( fsevent , SWITCH_STACK_BOTTOM , " secure_type " , " srtp:%s " , switch_channel_get_variable ( channel , " sip_has_crypto " ) ) ;
switch_event_add_header_string ( fsevent , SWITCH_STACK_BOTTOM , " caller-unique-id " , switch_channel_get_uuid ( channel ) ) ;
switch_event_fire ( & fsevent ) ;
}
2008-01-16 06:01:53 +00:00
return SWITCH_STATUS_SUCCESS ;
2011-05-02 16:33:57 +00:00
# endif
2008-01-16 06:01:53 +00:00
}
2009-09-10 00:46:24 +00:00
SWITCH_DECLARE ( switch_status_t ) switch_rtp_set_interval ( switch_rtp_t * rtp_session , uint32_t ms_per_packet , uint32_t samples_per_interval )
2008-11-10 18:26:41 +00:00
{
rtp_session - > ms_per_packet = ms_per_packet ;
rtp_session - > samples_per_interval = rtp_session - > conf_samples_per_interval = samples_per_interval ;
2008-11-18 20:40:44 +00:00
rtp_session - > missed_count = 0 ;
2010-02-06 03:38:24 +00:00
rtp_session - > samples_per_second =
( uint32_t ) ( ( double ) ( 1000.0f / ( double ) ( rtp_session - > ms_per_packet / 1000 ) ) * ( double ) rtp_session - > samples_per_interval ) ;
2009-09-10 00:46:24 +00:00
return SWITCH_STATUS_SUCCESS ;
}
SWITCH_DECLARE ( switch_status_t ) switch_rtp_change_interval ( switch_rtp_t * rtp_session , uint32_t ms_per_packet , uint32_t samples_per_interval )
{
2009-12-03 22:51:50 +00:00
switch_status_t status = SWITCH_STATUS_SUCCESS ;
int change_timer = 0 ;
2010-02-06 03:38:24 +00:00
2009-12-03 22:51:50 +00:00
if ( rtp_session - > ms_per_packet & & rtp_session - > ms_per_packet ! = ms_per_packet ) {
change_timer = 1 ;
}
2008-11-18 20:40:44 +00:00
2009-09-10 00:46:24 +00:00
switch_rtp_set_interval ( rtp_session , ms_per_packet , samples_per_interval ) ;
2010-02-06 03:38:24 +00:00
2009-12-03 22:51:50 +00:00
if ( change_timer & & rtp_session - > timer_name ) {
READ_INC ( rtp_session ) ;
WRITE_INC ( rtp_session ) ;
2008-11-10 18:26:41 +00:00
if ( rtp_session - > timer . timer_interface ) {
switch_core_timer_destroy ( & rtp_session - > timer ) ;
}
2010-02-06 03:38:24 +00:00
if ( ( status = switch_core_timer_init ( & rtp_session - > timer ,
rtp_session - > timer_name , ms_per_packet / 1000 ,
2009-12-03 22:51:50 +00:00
samples_per_interval , rtp_session - > pool ) ) = = SWITCH_STATUS_SUCCESS ) {
2012-04-10 21:17:16 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_DEBUG ,
2010-05-13 02:25:54 +00:00
" RE-Starting timer [%s] %d bytes per %dms \n " , rtp_session - > timer_name , samples_per_interval , ms_per_packet / 1000 ) ;
2008-11-18 23:26:25 +00:00
} else {
2012-04-10 21:17:16 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
2008-11-18 23:26:25 +00:00
memset ( & rtp_session - > timer , 0 , sizeof ( rtp_session - > timer ) ) ;
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR ,
2008-11-18 23:44:41 +00:00
" Problem RE-Starting timer [%s] %d bytes per %dms \n " , rtp_session - > timer_name , samples_per_interval , ms_per_packet / 1000 ) ;
2008-11-18 23:26:25 +00:00
}
2009-12-03 22:51:50 +00:00
WRITE_DEC ( rtp_session ) ;
READ_DEC ( rtp_session ) ;
2008-11-10 18:26:41 +00:00
}
2010-02-06 03:38:24 +00:00
2009-12-03 22:51:50 +00:00
return status ;
2008-11-10 18:26:41 +00:00
}
2010-02-06 03:38:24 +00:00
SWITCH_DECLARE ( switch_status_t ) switch_rtp_set_ssrc ( switch_rtp_t * rtp_session , uint32_t ssrc )
{
rtp_session - > ssrc = ssrc ;
rtp_session - > send_msg . header . ssrc = htonl ( rtp_session - > ssrc ) ;
return SWITCH_STATUS_SUCCESS ;
}
2006-04-29 23:43:28 +00:00
SWITCH_DECLARE ( switch_status_t ) switch_rtp_create ( switch_rtp_t * * new_rtp_session ,
2006-09-12 22:23:45 +00:00
switch_payload_t payload ,
2007-03-09 16:30:34 +00:00
uint32_t samples_per_interval ,
2006-09-12 22:23:45 +00:00
uint32_t ms_per_packet ,
2008-05-27 04:30:03 +00:00
switch_rtp_flag_t flags , char * timer_name , const char * * err , switch_memory_pool_t * pool )
2006-04-06 20:56:29 +00:00
{
2006-04-29 01:00:52 +00:00
switch_rtp_t * rtp_session = NULL ;
2009-08-20 17:39:13 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( pool , " __session " ) ;
2010-04-20 21:15:37 +00:00
switch_channel_t * channel = NULL ;
if ( session ) channel = switch_core_session_get_channel ( session ) ;
2006-04-06 20:56:29 +00:00
* new_rtp_session = NULL ;
2007-03-29 22:31:56 +00:00
2007-03-09 16:30:34 +00:00
if ( samples_per_interval > SWITCH_RTP_MAX_BUF_LEN ) {
2006-04-07 17:32:14 +00:00
* err = " Packet Size Too Large! " ;
return SWITCH_STATUS_FALSE ;
}
2006-04-03 21:00:13 +00:00
if ( ! ( rtp_session = switch_core_alloc ( pool , sizeof ( * rtp_session ) ) ) ) {
* err = " Memory Error! " ;
2006-04-06 20:56:29 +00:00
return SWITCH_STATUS_MEMERR ;
2006-04-03 21:00:13 +00:00
}
2006-04-04 02:28:11 +00:00
rtp_session - > pool = pool ;
2006-05-27 00:24:32 +00:00
rtp_session - > te = 101 ;
2010-03-16 22:49:54 +00:00
rtp_session - > recv_te = 101 ;
2006-08-28 23:05:26 +00:00
2007-12-05 17:16:50 +00:00
switch_mutex_init ( & rtp_session - > flag_mutex , SWITCH_MUTEX_NESTED , pool ) ;
2008-01-18 18:14:53 +00:00
switch_mutex_init ( & rtp_session - > read_mutex , SWITCH_MUTEX_NESTED , pool ) ;
switch_mutex_init ( & rtp_session - > write_mutex , SWITCH_MUTEX_NESTED , pool ) ;
2007-12-05 17:16:50 +00:00
switch_mutex_init ( & rtp_session - > dtmf_data . dtmf_mutex , SWITCH_MUTEX_NESTED , pool ) ;
2007-12-22 00:32:20 +00:00
switch_queue_create ( & rtp_session - > dtmf_data . dtmf_queue , 100 , rtp_session - > pool ) ;
switch_queue_create ( & rtp_session - > dtmf_data . dtmf_inqueue , 100 , rtp_session - > pool ) ;
2007-05-16 17:47:24 +00:00
switch_rtp_set_flag ( rtp_session , flags ) ;
2006-04-07 17:32:14 +00:00
/* for from address on recvfrom calls */
2010-12-11 17:41:19 +00:00
switch_sockaddr_create ( & rtp_session - > from_addr , pool ) ;
2010-04-20 00:07:23 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_ENABLE_RTCP ) ) {
2010-12-11 17:41:19 +00:00
switch_sockaddr_create ( & rtp_session - > rtcp_from_addr , pool ) ;
2010-04-20 00:07:23 +00:00
}
2010-11-11 00:08:55 +00:00
rtp_session - > seq = ( uint16_t ) rand ( ) ;
2010-02-03 19:19:10 +00:00
rtp_session - > ssrc = ( uint32_t ) ( ( intptr_t ) rtp_session + ( uint32_t ) switch_epoch_time_now ( NULL ) ) ;
2008-01-16 06:01:53 +00:00
rtp_session - > send_msg . header . ssrc = htonl ( rtp_session - > ssrc ) ;
2007-03-29 22:31:56 +00:00
rtp_session - > send_msg . header . ts = 0 ;
rtp_session - > send_msg . header . m = 0 ;
rtp_session - > send_msg . header . pt = ( switch_payload_t ) htonl ( payload ) ;
2006-04-03 21:00:13 +00:00
rtp_session - > send_msg . header . version = 2 ;
2007-03-29 22:31:56 +00:00
rtp_session - > send_msg . header . p = 0 ;
rtp_session - > send_msg . header . x = 0 ;
rtp_session - > send_msg . header . cc = 0 ;
2008-01-16 06:01:53 +00:00
rtp_session - > recv_msg . header . ssrc = 0 ;
2007-03-29 22:31:56 +00:00
rtp_session - > recv_msg . header . ts = 0 ;
rtp_session - > recv_msg . header . seq = 0 ;
rtp_session - > recv_msg . header . m = 0 ;
rtp_session - > recv_msg . header . pt = ( switch_payload_t ) htonl ( payload ) ;
2006-04-03 21:00:13 +00:00
rtp_session - > recv_msg . header . version = 2 ;
2007-03-29 22:31:56 +00:00
rtp_session - > recv_msg . header . p = 0 ;
rtp_session - > recv_msg . header . x = 0 ;
rtp_session - > recv_msg . header . cc = 0 ;
2006-04-03 21:00:13 +00:00
rtp_session - > payload = payload ;
2011-02-15 22:09:48 +00:00
rtp_session - > rpayload = payload ;
2010-04-20 21:15:37 +00:00
2012-06-22 23:08:59 +00:00
2010-04-20 21:15:37 +00:00
2010-04-20 00:07:23 +00:00
2009-09-10 00:46:24 +00:00
switch_rtp_set_interval ( rtp_session , ms_per_packet , samples_per_interval ) ;
rtp_session - > conf_samples_per_interval = samples_per_interval ;
2006-04-10 19:55:53 +00:00
2009-10-23 16:03:42 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_USE_TIMER ) & & zstr ( timer_name ) ) {
2006-09-16 20:46:52 +00:00
timer_name = " soft " ;
}
2009-10-23 16:03:42 +00:00
if ( ! zstr ( timer_name ) & & ! strcasecmp ( timer_name , " none " ) ) {
2008-11-10 18:26:41 +00:00
timer_name = NULL ;
}
2009-10-23 16:03:42 +00:00
if ( ! zstr ( timer_name ) ) {
2008-09-09 16:31:53 +00:00
rtp_session - > timer_name = switch_core_strdup ( pool , timer_name ) ;
switch_set_flag_locked ( rtp_session , SWITCH_RTP_FLAG_USE_TIMER ) ;
switch_set_flag_locked ( rtp_session , SWITCH_RTP_FLAG_NOBLOCK ) ;
2008-05-27 04:30:03 +00:00
if ( switch_core_timer_init ( & rtp_session - > timer , timer_name , ms_per_packet / 1000 , samples_per_interval , pool ) = = SWITCH_STATUS_SUCCESS ) {
2009-08-20 17:39:13 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_DEBUG ,
2009-02-19 14:41:20 +00:00
" Starting timer [%s] %d bytes per %dms \n " , timer_name , samples_per_interval , ms_per_packet / 1000 ) ;
2006-09-17 18:03:32 +00:00
} else {
memset ( & rtp_session - > timer , 0 , sizeof ( rtp_session - > timer ) ) ;
2009-08-20 17:39:13 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " Error starting timer [%s], async RTP disabled \n " , timer_name ) ;
2006-09-17 18:03:32 +00:00
switch_clear_flag_locked ( rtp_session , SWITCH_RTP_FLAG_USE_TIMER ) ;
}
2008-09-09 16:31:53 +00:00
} else {
2009-08-20 17:39:13 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_DEBUG , " Not using a timer \n " ) ;
2008-09-09 16:31:53 +00:00
switch_clear_flag_locked ( rtp_session , SWITCH_RTP_FLAG_USE_TIMER ) ;
switch_clear_flag_locked ( rtp_session , SWITCH_RTP_FLAG_NOBLOCK ) ;
2006-09-16 20:46:52 +00:00
}
2010-06-04 12:16:51 +00:00
if ( channel ) {
switch_channel_set_private ( channel , " __rtcp_audio_rtp_session " , rtp_session ) ;
}
2010-04-20 21:15:37 +00:00
2009-05-20 23:04:05 +00:00
# ifdef ENABLE_ZRTP
2012-01-25 05:38:53 +00:00
if ( zrtp_on & & ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_PROXY_MEDIA ) ) {
2010-02-03 19:19:10 +00:00
switch_rtp_t * master_rtp_session = NULL ;
2009-05-23 01:50:56 +00:00
int initiator = 0 ;
2009-05-27 05:07:19 +00:00
const char * zrtp_enabled = switch_channel_get_variable ( channel , " zrtp_secure_media " ) ;
2009-05-28 13:53:23 +00:00
const char * srtp_enabled = switch_channel_get_variable ( channel , " sip_secure_media " ) ;
2009-05-28 22:54:51 +00:00
if ( switch_true ( srtp_enabled ) & & switch_true ( zrtp_enabled ) ) {
2010-02-06 03:38:24 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_WARNING ,
" You can not have ZRTP and SRTP enabled simultaneously, ZRTP will be disabled for this call! \n " ) ;
2009-05-28 13:53:23 +00:00
switch_channel_set_variable ( channel , " zrtp_secure_media " , NULL ) ;
zrtp_enabled = NULL ;
}
2009-05-23 01:50:56 +00:00
2010-02-06 03:38:24 +00:00
2009-05-27 05:07:19 +00:00
if ( switch_true ( zrtp_enabled ) ) {
2010-02-06 03:38:24 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_VIDEO ) ) {
2010-02-03 19:19:10 +00:00
switch_channel_set_private ( channel , " __zrtp_video_rtp_session " , rtp_session ) ;
master_rtp_session = switch_channel_get_private ( channel , " __zrtp_audio_rtp_session " ) ;
} else {
switch_channel_set_private ( channel , " __zrtp_audio_rtp_session " , rtp_session ) ;
master_rtp_session = rtp_session ;
}
2009-05-27 05:07:19 +00:00
if ( switch_channel_direction ( channel ) = = SWITCH_CALL_DIRECTION_OUTBOUND ) {
initiator = 1 ;
}
2010-02-03 19:19:10 +00:00
if ( rtp_session = = master_rtp_session ) {
rtp_session - > zrtp_profile = switch_core_alloc ( rtp_session - > pool , sizeof ( * rtp_session - > zrtp_profile ) ) ;
zrtp_profile_defaults ( rtp_session - > zrtp_profile , zrtp_global ) ;
2010-02-06 03:38:24 +00:00
2010-02-03 19:19:10 +00:00
rtp_session - > zrtp_profile - > allowclear = 0 ;
rtp_session - > zrtp_profile - > disclose_bit = 0 ;
2012-05-16 21:05:41 +00:00
rtp_session - > zrtp_profile - > cache_ttl = ( uint32_t ) - 1 ;
2010-02-06 03:38:24 +00:00
2010-02-03 19:19:10 +00:00
if ( zrtp_status_ok ! = zrtp_session_init ( zrtp_global , rtp_session - > zrtp_profile , zid , initiator , & rtp_session - > zrtp_session ) ) {
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " Error! zRTP INIT Failed \n " ) ;
zrtp_session_down ( rtp_session - > zrtp_session ) ;
rtp_session - > zrtp_session = NULL ;
goto end ;
}
2010-02-06 03:38:24 +00:00
2010-02-03 19:19:10 +00:00
zrtp_session_set_userdata ( rtp_session - > zrtp_session , session ) ;
2010-02-06 03:38:24 +00:00
2010-02-03 19:19:10 +00:00
if ( zrtp_status_ok ! = zrtp_stream_attach ( master_rtp_session - > zrtp_session , & rtp_session - > zrtp_stream ) ) {
abort ( ) ;
}
2010-02-06 03:38:24 +00:00
2010-02-03 19:19:10 +00:00
zrtp_stream_set_userdata ( rtp_session - > zrtp_stream , rtp_session ) ;
if ( switch_true ( switch_channel_get_variable ( channel , " zrtp_enrollment " ) ) ) {
zrtp_stream_registration_start ( rtp_session - > zrtp_stream , rtp_session - > ssrc ) ;
} else {
zrtp_stream_start ( rtp_session - > zrtp_stream , rtp_session - > ssrc ) ;
}
2009-05-27 05:07:19 +00:00
}
2010-02-06 03:38:24 +00:00
2009-05-23 01:50:56 +00:00
}
2009-05-20 23:04:05 +00:00
}
2010-02-03 19:19:10 +00:00
2010-04-20 00:07:23 +00:00
end :
2010-02-03 19:19:10 +00:00
2010-02-06 03:38:24 +00:00
# endif
2006-04-06 20:56:29 +00:00
2009-05-21 22:37:15 +00:00
rtp_session - > ready = 1 ;
* new_rtp_session = rtp_session ;
2006-04-06 20:56:29 +00:00
return SWITCH_STATUS_SUCCESS ;
}
2007-05-12 14:48:14 +00:00
SWITCH_DECLARE ( switch_rtp_t * ) switch_rtp_new ( const char * rx_host ,
2007-03-29 22:31:56 +00:00
switch_port_t rx_port ,
2007-05-12 14:48:14 +00:00
const char * tx_host ,
2007-03-29 22:31:56 +00:00
switch_port_t tx_port ,
switch_payload_t payload ,
uint32_t samples_per_interval ,
uint32_t ms_per_packet ,
2008-05-27 04:30:03 +00:00
switch_rtp_flag_t flags , char * timer_name , const char * * err , switch_memory_pool_t * pool )
2006-04-06 20:56:29 +00:00
{
2007-12-13 15:10:55 +00:00
switch_rtp_t * rtp_session = NULL ;
2008-03-07 20:13:40 +00:00
2009-10-23 16:03:42 +00:00
if ( zstr ( rx_host ) ) {
2008-03-07 20:13:40 +00:00
* err = " Missing local host " ;
goto end ;
}
if ( ! rx_port ) {
* err = " Missing local port " ;
goto end ;
}
2009-10-23 16:03:42 +00:00
if ( zstr ( tx_host ) ) {
2008-03-07 20:13:40 +00:00
* err = " Missing remote host " ;
goto end ;
}
if ( ! tx_port ) {
2008-05-24 01:27:19 +00:00
* err = " Missing remote port " ;
2007-12-13 15:10:55 +00:00
goto end ;
2006-04-06 20:56:29 +00:00
}
2008-05-27 04:30:03 +00:00
2008-03-07 20:13:40 +00:00
if ( switch_rtp_create ( & rtp_session , payload , samples_per_interval , ms_per_packet , flags , timer_name , err , pool ) ! = SWITCH_STATUS_SUCCESS ) {
2007-12-13 15:10:55 +00:00
goto end ;
2006-04-06 20:56:29 +00:00
}
2008-03-08 02:11:21 +00:00
2008-05-27 04:30:03 +00:00
switch_mutex_lock ( rtp_session - > flag_mutex ) ;
2008-03-08 02:11:21 +00:00
2006-04-06 20:56:29 +00:00
if ( switch_rtp_set_local_address ( rtp_session , rx_host , rx_port , err ) ! = SWITCH_STATUS_SUCCESS ) {
2008-05-27 04:30:03 +00:00
switch_mutex_unlock ( rtp_session - > flag_mutex ) ;
2007-12-13 15:10:55 +00:00
rtp_session = NULL ;
2008-03-08 02:11:21 +00:00
goto end ;
2007-12-13 15:10:55 +00:00
}
2010-04-22 15:22:28 +00:00
if ( switch_rtp_set_remote_address ( rtp_session , tx_host , tx_port , 0 , SWITCH_TRUE , err ) ! = SWITCH_STATUS_SUCCESS ) {
2008-05-27 04:30:03 +00:00
switch_mutex_unlock ( rtp_session - > flag_mutex ) ;
2008-03-07 20:13:40 +00:00
rtp_session = NULL ;
goto end ;
}
2008-05-27 04:30:03 +00:00
2010-04-20 00:07:23 +00:00
end :
2007-12-13 15:10:55 +00:00
if ( rtp_session ) {
2008-03-08 02:11:21 +00:00
switch_mutex_unlock ( rtp_session - > flag_mutex ) ;
2008-01-18 18:14:53 +00:00
rtp_session - > ready = 2 ;
2007-12-13 15:10:55 +00:00
rtp_session - > rx_host = switch_core_strdup ( rtp_session - > pool , rx_host ) ;
rtp_session - > rx_port = rx_port ;
} else {
2007-12-20 15:41:45 +00:00
switch_rtp_release_port ( rx_host , rx_port ) ;
2006-04-06 20:56:29 +00:00
}
2007-03-29 22:31:56 +00:00
2006-04-03 21:00:13 +00:00
return rtp_session ;
}
2006-05-27 00:24:32 +00:00
SWITCH_DECLARE ( void ) switch_rtp_set_telephony_event ( switch_rtp_t * rtp_session , switch_payload_t te )
{
2007-05-21 14:45:35 +00:00
if ( te > 95 ) {
2006-05-27 00:24:32 +00:00
rtp_session - > te = te ;
}
}
2006-04-06 20:56:29 +00:00
2010-03-16 22:49:54 +00:00
SWITCH_DECLARE ( void ) switch_rtp_set_telephony_recv_event ( switch_rtp_t * rtp_session , switch_payload_t te )
{
if ( te > 95 ) {
rtp_session - > recv_te = te ;
}
}
2011-02-15 22:09:48 +00:00
SWITCH_DECLARE ( void ) switch_rtp_set_recv_pt ( switch_rtp_t * rtp_session , switch_payload_t pt )
{
rtp_session - > rpayload = pt ;
}
2007-03-07 18:34:22 +00:00
SWITCH_DECLARE ( void ) switch_rtp_set_cng_pt ( switch_rtp_t * rtp_session , switch_payload_t pt )
{
rtp_session - > cng_pt = pt ;
2008-09-18 13:03:58 +00:00
switch_set_flag ( rtp_session , SWITCH_RTP_FLAG_AUTO_CNG ) ;
2007-03-07 18:34:22 +00:00
}
2008-07-18 20:25:39 +00:00
SWITCH_DECLARE ( switch_status_t ) switch_rtp_activate_stun_ping ( switch_rtp_t * rtp_session , const char * stun_ip , switch_port_t stun_port ,
uint32_t packet_count , switch_bool_t funny )
2008-07-18 16:18:31 +00:00
{
2010-02-06 03:38:24 +00:00
if ( switch_sockaddr_info_get ( & rtp_session - > remote_stun_addr , stun_ip , SWITCH_UNSPEC ,
2008-07-18 23:16:07 +00:00
stun_port , 0 , rtp_session - > pool ) ! = SWITCH_STATUS_SUCCESS | | ! rtp_session - > remote_stun_addr ) {
2010-02-06 03:38:24 +00:00
2012-04-10 21:17:16 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " Error resolving stun ping addr \n " ) ;
2008-07-18 16:18:31 +00:00
return SWITCH_STATUS_FALSE ;
}
if ( funny ) {
2012-06-22 23:08:59 +00:00
rtp_session - > ice . funny_stun + + ;
2008-07-18 16:18:31 +00:00
}
2008-07-18 20:25:39 +00:00
rtp_session - > stun_port = stun_port ;
2012-06-22 23:08:59 +00:00
rtp_session - > ice . default_stuncount = packet_count ;
2008-07-18 16:18:31 +00:00
rtp_session - > stun_ip = switch_core_strdup ( rtp_session - > pool , stun_ip ) ;
return SWITCH_STATUS_SUCCESS ;
}
2010-12-10 23:47:24 +00:00
static void jb_callback ( stfu_instance_t * i , void * udata )
2007-05-04 23:29:35 +00:00
{
2010-12-10 23:47:24 +00:00
switch_core_session_t * session = ( switch_core_session_t * ) udata ;
stfu_report_t r = { 0 } ;
2009-03-30 21:12:06 +00:00
2010-12-10 23:47:24 +00:00
stfu_n_report ( i , & r ) ;
2009-03-30 21:12:06 +00:00
2011-02-21 19:28:41 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_DEBUG8 ,
2010-12-10 23:47:24 +00:00
" %s JB REPORT: \n len: %u \n in: %u \n clean: %u \n good: %u \n bad: %u \n " ,
switch_core_session_get_name ( session ) ,
r . qlen ,
r . packet_in_count ,
r . clean_count ,
r . consecutive_good_count ,
r . consecutive_bad_count
) ;
}
2011-01-05 22:25:07 +00:00
SWITCH_DECLARE ( switch_status_t ) switch_rtp_pause_jitter_buffer ( switch_rtp_t * rtp_session , switch_bool_t pause )
{
if ( ! switch_rtp_ready ( rtp_session ) | | ! rtp_session - > jb ) {
return SWITCH_STATUS_FALSE ;
}
if ( ! ! pause = = ! ! rtp_session - > pause_jb ) {
return SWITCH_STATUS_FALSE ;
}
if ( rtp_session - > pause_jb & & ! pause ) {
stfu_n_reset ( rtp_session - > jb ) ;
}
rtp_session - > pause_jb = pause ? 1 : 0 ;
return SWITCH_STATUS_SUCCESS ;
}
2010-12-10 23:47:24 +00:00
SWITCH_DECLARE ( switch_status_t ) switch_rtp_deactivate_jitter_buffer ( switch_rtp_t * rtp_session )
{
if ( ! switch_rtp_ready ( rtp_session ) | | ! rtp_session - > jb ) {
return SWITCH_STATUS_FALSE ;
}
READ_INC ( rtp_session ) ;
stfu_n_destroy ( & rtp_session - > jb ) ;
READ_DEC ( rtp_session ) ;
2007-05-04 23:29:35 +00:00
return SWITCH_STATUS_SUCCESS ;
}
2010-12-15 05:46:26 +00:00
static void jb_logger ( const char * file , const char * func , int line , int level , const char * fmt , . . . )
{
int ret ;
char * data ;
va_list ap ;
va_start ( ap , fmt ) ;
2010-12-15 16:22:45 +00:00
ret = switch_vasprintf ( & data , fmt , ap ) ;
2010-12-15 05:46:26 +00:00
if ( ret ! = - 1 ) {
switch_log_printf ( SWITCH_CHANNEL_LOG_CLEAN , SWITCH_LOG_CONSOLE , " %s " , data ) ;
free ( data ) ;
}
//switch_log_printf(SWITCH_CHANNEL_ID_LOG_CLEAN, file, func, line, NULL, level, fmt, ap);
va_end ( ap ) ;
}
SWITCH_DECLARE ( switch_status_t ) switch_rtp_debug_jitter_buffer ( switch_rtp_t * rtp_session , const char * name )
{
2011-02-12 02:15:06 +00:00
if ( ! switch_rtp_ready ( rtp_session ) | | ! rtp_session - > jb ) {
2011-02-04 16:58:26 +00:00
return SWITCH_STATUS_FALSE ;
}
2010-12-15 05:46:26 +00:00
stfu_n_debug ( rtp_session - > jb , name ) ;
stfu_global_set_logger ( jb_logger ) ;
return SWITCH_STATUS_SUCCESS ;
}
2010-12-10 23:47:24 +00:00
SWITCH_DECLARE ( 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 ,
2011-02-25 17:59:45 +00:00
uint32_t samples_per_second ,
uint32_t max_drift )
2010-12-10 23:47:24 +00:00
{
if ( ! switch_rtp_ready ( rtp_session ) ) {
return SWITCH_STATUS_FALSE ;
}
2011-03-18 03:29:16 +00:00
if ( queue_frames < 1 ) {
queue_frames = 3 ;
}
if ( max_queue_frames < queue_frames ) {
max_queue_frames = queue_frames * 3 ;
}
2010-12-10 23:47:24 +00:00
READ_INC ( rtp_session ) ;
if ( rtp_session - > jb ) {
stfu_n_resize ( rtp_session - > jb , queue_frames ) ;
} else {
2011-02-25 17:59:45 +00:00
rtp_session - > jb = stfu_n_init ( queue_frames , max_queue_frames ? max_queue_frames : 50 , samples_per_packet , samples_per_second , max_drift ) ;
2010-12-10 23:47:24 +00:00
}
READ_DEC ( rtp_session ) ;
if ( rtp_session - > jb ) {
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
stfu_n_call_me ( rtp_session - > jb , jb_callback , session ) ;
2010-12-15 05:46:26 +00:00
2010-12-10 23:47:24 +00:00
return SWITCH_STATUS_SUCCESS ;
}
return SWITCH_STATUS_FALSE ;
}
2010-04-20 21:15:37 +00:00
SWITCH_DECLARE ( switch_status_t ) switch_rtp_activate_rtcp ( switch_rtp_t * rtp_session , int send_rate , switch_port_t remote_port )
2010-04-20 00:07:23 +00:00
{
const char * err = NULL ;
2010-09-29 19:14:41 +00:00
if ( ! rtp_session - > ms_per_packet ) {
return SWITCH_STATUS_FALSE ;
}
2010-04-20 00:07:23 +00:00
switch_set_flag ( rtp_session , SWITCH_RTP_FLAG_ENABLE_RTCP ) ;
2010-04-20 21:15:37 +00:00
if ( ! ( rtp_session - > remote_rtcp_port = remote_port ) ) {
rtp_session - > remote_rtcp_port = rtp_session - > remote_port + 1 ;
}
if ( send_rate = = - 1 ) {
2012-04-10 21:17:16 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
2010-04-20 21:15:37 +00:00
switch_set_flag ( rtp_session , SWITCH_RTP_FLAG_RTCP_PASSTHRU ) ;
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_DEBUG , " RTCP passthru enabled. Remote Port: %d \n " , rtp_session - > remote_rtcp_port ) ;
2010-04-20 21:15:37 +00:00
} else {
2012-04-10 21:17:16 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_DEBUG , " RTCP send rate is: %d and packet rate is: %d Remote Port: %d \n " ,
2010-04-20 21:15:37 +00:00
send_rate , rtp_session - > ms_per_packet , rtp_session - > remote_rtcp_port ) ;
rtp_session - > rtcp_interval = send_rate / ( rtp_session - > ms_per_packet / 1000 ) ;
}
2010-04-20 00:07:23 +00:00
return enable_local_rtcp_socket ( rtp_session , & err ) | | enable_remote_rtcp_socket ( rtp_session , & err ) ;
}
2012-06-22 23:08:59 +00:00
SWITCH_DECLARE ( switch_status_t ) switch_rtp_activate_ice ( switch_rtp_t * rtp_session , char * login , char * rlogin , const char * password )
2006-04-04 17:22:06 +00:00
{
char ice_user [ 80 ] ;
char user_ice [ 80 ] ;
2007-12-12 21:53:32 +00:00
switch_snprintf ( ice_user , sizeof ( ice_user ) , " %s%s " , login , rlogin ) ;
switch_snprintf ( user_ice , sizeof ( user_ice ) , " %s%s " , rlogin , login ) ;
2006-04-04 17:22:06 +00:00
2012-06-22 23:08:59 +00:00
rtp_session - > ice . ice_user = switch_core_strdup ( rtp_session - > pool , ice_user ) ;
rtp_session - > ice . user_ice = switch_core_strdup ( rtp_session - > pool , user_ice ) ;
if ( password ) {
rtp_session - > ice . pass = switch_core_strdup ( rtp_session - > pool , password ) ;
}
rtp_session - > ice . default_stuncount = 25 ;
rtp_session - > ice . stuncount = 0 ;
if ( rtp_session - > ice . ice_user ) {
if ( ice_out ( rtp_session , & rtp_session - > ice ) ! = SWITCH_STATUS_SUCCESS ) {
return SWITCH_STATUS_FALSE ;
}
}
return SWITCH_STATUS_SUCCESS ;
}
SWITCH_DECLARE ( switch_status_t ) switch_rtp_activate_rtcp_ice ( switch_rtp_t * rtp_session , char * login , char * rlogin , const char * password )
{
char ice_user [ 80 ] ;
char user_ice [ 80 ] ;
switch_snprintf ( ice_user , sizeof ( ice_user ) , " %s%s " , login , rlogin ) ;
switch_snprintf ( user_ice , sizeof ( user_ice ) , " %s%s " , rlogin , login ) ;
rtp_session - > rtcp_ice . ice_user = switch_core_strdup ( rtp_session - > pool , ice_user ) ;
rtp_session - > rtcp_ice . user_ice = switch_core_strdup ( rtp_session - > pool , user_ice ) ;
if ( password ) {
rtp_session - > rtcp_ice . pass = switch_core_strdup ( rtp_session - > pool , password ) ;
}
rtp_session - > rtcp_ice . default_stuncount = 25 ;
rtp_session - > rtcp_ice . stuncount = 0 ;
if ( rtp_session - > rtcp_ice . ice_user ) {
if ( ice_out ( rtp_session , & rtp_session - > rtcp_ice ) ! = SWITCH_STATUS_SUCCESS ) {
2008-11-27 02:09:46 +00:00
return SWITCH_STATUS_FALSE ;
2006-04-05 20:17:22 +00:00
}
}
2006-04-04 17:22:06 +00:00
return SWITCH_STATUS_SUCCESS ;
}
2012-06-22 23:08:59 +00:00
2012-03-26 15:53:34 +00:00
SWITCH_DECLARE ( void ) switch_rtp_flush ( switch_rtp_t * rtp_session )
{
if ( ! switch_rtp_ready ( rtp_session ) ) {
return ;
}
switch_set_flag_locked ( rtp_session , SWITCH_RTP_FLAG_FLUSH ) ;
}
2008-03-07 18:21:08 +00:00
SWITCH_DECLARE ( void ) switch_rtp_break ( switch_rtp_t * rtp_session )
{
2008-12-03 16:52:58 +00:00
if ( ! switch_rtp_ready ( rtp_session ) ) {
return ;
}
2008-05-27 04:30:03 +00:00
switch_mutex_lock ( rtp_session - > flag_mutex ) ;
2008-10-10 23:09:15 +00:00
switch_set_flag ( rtp_session , SWITCH_RTP_FLAG_BREAK ) ;
2010-02-06 03:38:24 +00:00
2008-12-03 16:52:58 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_NOBLOCK ) ) {
switch_mutex_unlock ( rtp_session - > flag_mutex ) ;
return ;
}
2008-10-10 23:09:15 +00:00
2008-07-03 18:50:15 +00:00
if ( rtp_session - > sock_input ) {
2009-01-13 17:56:35 +00:00
ping_socket ( rtp_session ) ;
2008-03-07 18:21:08 +00:00
}
switch_mutex_unlock ( rtp_session - > flag_mutex ) ;
}
2006-04-29 01:00:52 +00:00
SWITCH_DECLARE ( void ) switch_rtp_kill_socket ( switch_rtp_t * rtp_session )
2006-04-03 21:00:13 +00:00
{
2007-12-11 19:23:57 +00:00
switch_assert ( rtp_session ! = NULL ) ;
2006-08-30 19:46:43 +00:00
switch_mutex_lock ( rtp_session - > flag_mutex ) ;
2006-09-14 01:37:55 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_IO ) ) {
2008-01-18 18:14:53 +00:00
switch_clear_flag ( rtp_session , SWITCH_RTP_FLAG_IO ) ;
2008-07-03 18:50:15 +00:00
if ( rtp_session - > sock_input ) {
2009-01-13 17:56:35 +00:00
ping_socket ( rtp_session ) ;
2008-07-03 18:50:15 +00:00
switch_socket_shutdown ( rtp_session - > sock_input , SWITCH_SHUTDOWN_READWRITE ) ;
}
if ( rtp_session - > sock_output & & rtp_session - > sock_output ! = rtp_session - > sock_input ) {
switch_socket_shutdown ( rtp_session - > sock_output , SWITCH_SHUTDOWN_READWRITE ) ;
2008-03-07 18:21:08 +00:00
}
2010-04-20 00:07:23 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_ENABLE_RTCP ) ) {
if ( rtp_session - > rtcp_sock_input ) {
ping_socket ( rtp_session ) ;
switch_socket_shutdown ( rtp_session - > rtcp_sock_input , SWITCH_SHUTDOWN_READWRITE ) ;
}
if ( rtp_session - > rtcp_sock_output & & rtp_session - > rtcp_sock_output ! = rtp_session - > rtcp_sock_input ) {
switch_socket_shutdown ( rtp_session - > rtcp_sock_output , SWITCH_SHUTDOWN_READWRITE ) ;
}
}
2006-09-14 01:37:55 +00:00
}
2006-08-30 19:46:43 +00:00
switch_mutex_unlock ( rtp_session - > flag_mutex ) ;
2006-04-03 21:00:13 +00:00
}
2006-08-28 23:05:26 +00:00
SWITCH_DECLARE ( uint8_t ) switch_rtp_ready ( switch_rtp_t * rtp_session )
{
2008-02-25 16:35:19 +00:00
uint8_t ret ;
2008-04-02 20:53:13 +00:00
if ( ! rtp_session | | ! rtp_session - > flag_mutex | | switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_SHUTDOWN ) ) {
2008-02-25 16:35:19 +00:00
return 0 ;
}
switch_mutex_lock ( rtp_session - > flag_mutex ) ;
2010-02-06 03:38:24 +00:00
ret = ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_IO ) & & rtp_session - > sock_input & & rtp_session - > sock_output & & rtp_session - > remote_addr
& & rtp_session - > ready = = 2 ) ? 1 : 0 ;
2008-02-25 16:35:19 +00:00
switch_mutex_unlock ( rtp_session - > flag_mutex ) ;
return ret ;
2006-08-28 23:05:26 +00:00
}
2006-04-03 21:00:13 +00:00
2006-04-29 01:00:52 +00:00
SWITCH_DECLARE ( void ) switch_rtp_destroy ( switch_rtp_t * * rtp_session )
2006-04-03 21:00:13 +00:00
{
2007-12-22 00:32:20 +00:00
void * pop ;
2008-01-18 00:10:23 +00:00
switch_socket_t * sock ;
2008-02-25 16:35:19 +00:00
2008-01-18 18:14:53 +00:00
if ( ! rtp_session | | ! * rtp_session | | ! ( * rtp_session ) - > ready ) {
2006-08-28 23:05:26 +00:00
return ;
}
2008-04-02 20:53:13 +00:00
switch_set_flag_locked ( ( * rtp_session ) , SWITCH_RTP_FLAG_SHUTDOWN ) ;
2008-03-11 03:45:16 +00:00
2008-02-25 16:35:19 +00:00
READ_INC ( ( * rtp_session ) ) ;
WRITE_INC ( ( * rtp_session ) ) ;
2008-01-18 18:14:53 +00:00
2008-02-25 16:35:19 +00:00
( * rtp_session ) - > ready = 0 ;
2008-01-18 18:14:53 +00:00
2008-02-25 16:35:19 +00:00
READ_DEC ( ( * rtp_session ) ) ;
WRITE_DEC ( ( * rtp_session ) ) ;
2008-05-27 04:30:03 +00:00
2008-04-02 20:53:13 +00:00
switch_mutex_lock ( ( * rtp_session ) - > flag_mutex ) ;
2008-01-18 18:14:53 +00:00
switch_rtp_kill_socket ( * rtp_session ) ;
2008-05-27 04:30:03 +00:00
while ( switch_queue_trypop ( ( * rtp_session ) - > dtmf_data . dtmf_inqueue , & pop ) = = SWITCH_STATUS_SUCCESS ) {
2008-02-25 16:41:28 +00:00
switch_safe_free ( pop ) ;
2007-12-22 00:32:20 +00:00
}
2008-05-27 04:30:03 +00:00
while ( switch_queue_trypop ( ( * rtp_session ) - > dtmf_data . dtmf_queue , & pop ) = = SWITCH_STATUS_SUCCESS ) {
2008-02-25 16:41:28 +00:00
switch_safe_free ( pop ) ;
2007-12-22 00:32:20 +00:00
}
2007-03-29 22:31:56 +00:00
2007-05-04 23:29:35 +00:00
if ( ( * rtp_session ) - > jb ) {
stfu_n_destroy ( & ( * rtp_session ) - > jb ) ;
}
2008-07-03 18:50:15 +00:00
sock = ( * rtp_session ) - > sock_input ;
( * rtp_session ) - > sock_input = NULL ;
2008-01-18 00:10:23 +00:00
switch_socket_close ( sock ) ;
2010-02-06 03:38:24 +00:00
if ( ( * rtp_session ) - > sock_output ! = sock ) {
2008-07-03 18:50:15 +00:00
sock = ( * rtp_session ) - > sock_output ;
( * rtp_session ) - > sock_output = NULL ;
switch_socket_close ( sock ) ;
}
2006-09-19 20:04:35 +00:00
2010-06-17 15:23:37 +00:00
if ( ( sock = ( * rtp_session ) - > rtcp_sock_input ) ) {
( * rtp_session ) - > rtcp_sock_input = NULL ;
switch_socket_close ( sock ) ;
if ( ( * rtp_session ) - > rtcp_sock_output & & ( * rtp_session ) - > rtcp_sock_output ! = sock ) {
if ( ( sock = ( * rtp_session ) - > rtcp_sock_output ) ) {
( * rtp_session ) - > rtcp_sock_output = NULL ;
switch_socket_close ( sock ) ;
}
}
}
2006-04-21 22:31:08 +00:00
if ( switch_test_flag ( ( * rtp_session ) , SWITCH_RTP_FLAG_VAD ) ) {
switch_rtp_disable_vad ( * rtp_session ) ;
}
2006-08-28 23:05:26 +00:00
2011-05-02 16:33:57 +00:00
# ifdef ENABLE_SRTP
2008-01-17 04:16:04 +00:00
if ( switch_test_flag ( ( * rtp_session ) , SWITCH_RTP_FLAG_SECURE_SEND ) ) {
2006-04-10 19:55:53 +00:00
srtp_dealloc ( ( * rtp_session ) - > send_ctx ) ;
2008-01-17 04:16:04 +00:00
( * rtp_session ) - > send_ctx = NULL ;
switch_clear_flag ( ( * rtp_session ) , SWITCH_RTP_FLAG_SECURE_SEND ) ;
}
if ( switch_test_flag ( ( * rtp_session ) , SWITCH_RTP_FLAG_SECURE_RECV ) ) {
srtp_dealloc ( ( * rtp_session ) - > recv_ctx ) ;
( * rtp_session ) - > recv_ctx = NULL ;
switch_clear_flag ( ( * rtp_session ) , SWITCH_RTP_FLAG_SECURE_RECV ) ;
2006-04-10 19:55:53 +00:00
}
2011-05-02 16:33:57 +00:00
# endif
2009-05-20 23:04:05 +00:00
# ifdef ENABLE_ZRTP
/* ZRTP */
2012-01-25 05:38:53 +00:00
if ( zrtp_on & & ! switch_test_flag ( ( * rtp_session ) , SWITCH_RTP_FLAG_PROXY_MEDIA ) ) {
2009-09-03 14:48:02 +00:00
2010-02-03 19:19:10 +00:00
if ( ( * rtp_session ) - > zrtp_stream ! = NULL ) {
zrtp_stream_stop ( ( * rtp_session ) - > zrtp_stream ) ;
2009-09-03 14:48:02 +00:00
}
2009-05-20 23:04:05 +00:00
if ( switch_test_flag ( ( * rtp_session ) , SWITCH_ZRTP_FLAG_SECURE_SEND ) ) {
switch_clear_flag ( ( * rtp_session ) , SWITCH_ZRTP_FLAG_SECURE_SEND ) ;
}
2006-04-10 19:55:53 +00:00
2009-05-20 23:04:05 +00:00
if ( switch_test_flag ( ( * rtp_session ) , SWITCH_ZRTP_FLAG_SECURE_RECV ) ) {
switch_clear_flag ( ( * rtp_session ) , SWITCH_ZRTP_FLAG_SECURE_RECV ) ;
}
if ( ( * rtp_session ) - > zrtp_session ) {
zrtp_session_down ( ( * rtp_session ) - > zrtp_session ) ;
( * rtp_session ) - > zrtp_session = NULL ;
}
}
# endif
2006-09-12 22:23:45 +00:00
if ( ( * rtp_session ) - > timer . timer_interface ) {
switch_core_timer_destroy ( & ( * rtp_session ) - > timer ) ;
}
2007-12-20 15:41:45 +00:00
switch_rtp_release_port ( ( * rtp_session ) - > rx_host , ( * rtp_session ) - > rx_port ) ;
2006-09-19 20:04:35 +00:00
switch_mutex_unlock ( ( * rtp_session ) - > flag_mutex ) ;
2008-01-18 18:14:53 +00:00
2006-04-03 21:00:13 +00:00
return ;
}
2012-04-18 14:55:58 +00:00
SWITCH_DECLARE ( void ) switch_rtp_set_interdigit_delay ( switch_rtp_t * rtp_session , uint32_t delay )
{
rtp_session - > interdigit_delay = delay ;
}
2007-03-29 22:31:56 +00:00
SWITCH_DECLARE ( switch_socket_t * ) switch_rtp_get_rtp_socket ( switch_rtp_t * rtp_session )
2006-04-03 21:00:13 +00:00
{
2008-07-03 18:50:15 +00:00
return rtp_session - > sock_input ;
2006-04-03 21:00:13 +00:00
}
2007-03-09 16:30:34 +00:00
SWITCH_DECLARE ( uint32_t ) switch_rtp_get_default_samples_per_interval ( switch_rtp_t * rtp_session )
2006-04-07 17:32:14 +00:00
{
2007-03-09 16:30:34 +00:00
return rtp_session - > samples_per_interval ;
2006-04-07 17:32:14 +00:00
}
2006-04-29 01:00:52 +00:00
SWITCH_DECLARE ( void ) switch_rtp_set_default_payload ( switch_rtp_t * rtp_session , switch_payload_t payload )
2006-04-06 21:08:30 +00:00
{
rtp_session - > payload = payload ;
}
2006-04-29 01:00:52 +00:00
SWITCH_DECLARE ( uint32_t ) switch_rtp_get_default_payload ( switch_rtp_t * rtp_session )
2006-04-06 21:08:30 +00:00
{
return rtp_session - > payload ;
}
2006-04-29 23:43:28 +00:00
SWITCH_DECLARE ( void ) switch_rtp_set_invald_handler ( switch_rtp_t * rtp_session , switch_rtp_invalid_handler_t on_invalid )
2006-04-03 21:00:13 +00:00
{
rtp_session - > invalid_handler = on_invalid ;
}
2007-03-29 22:31:56 +00:00
SWITCH_DECLARE ( void ) switch_rtp_set_flag ( switch_rtp_t * rtp_session , switch_rtp_flag_t flags )
2006-04-17 18:25:43 +00:00
{
2006-08-28 23:05:26 +00:00
switch_set_flag_locked ( rtp_session , flags ) ;
2007-05-16 17:47:24 +00:00
if ( flags & SWITCH_RTP_FLAG_AUTOADJ ) {
rtp_session - > autoadj_window = 20 ;
rtp_session - > autoadj_tally = 0 ;
2009-10-30 00:16:36 +00:00
rtp_flush_read_buffer ( rtp_session , SWITCH_RTP_FLUSH_ONCE ) ;
2011-03-09 21:17:09 +00:00
} else if ( flags & SWITCH_RTP_FLAG_NOBLOCK ) {
switch_socket_opt_set ( rtp_session - > sock_input , SWITCH_SO_NONBLOCK , TRUE ) ;
2007-05-16 17:47:24 +00:00
}
2011-03-09 21:17:09 +00:00
2006-04-17 18:25:43 +00:00
}
2008-05-13 20:36:23 +00:00
SWITCH_DECLARE ( uint32_t ) switch_rtp_test_flag ( switch_rtp_t * rtp_session , switch_rtp_flag_t flags )
2006-04-17 18:25:43 +00:00
{
2008-05-13 20:36:23 +00:00
return ( uint32_t ) switch_test_flag ( rtp_session , flags ) ;
2006-04-17 18:25:43 +00:00
}
2007-03-29 22:31:56 +00:00
SWITCH_DECLARE ( void ) switch_rtp_clear_flag ( switch_rtp_t * rtp_session , switch_rtp_flag_t flags )
2006-04-17 18:25:43 +00:00
{
2006-08-28 23:05:26 +00:00
switch_clear_flag_locked ( rtp_session , flags ) ;
2011-03-09 21:17:09 +00:00
if ( flags & SWITCH_RTP_FLAG_NOBLOCK ) {
switch_socket_opt_set ( rtp_session - > sock_input , SWITCH_SO_NONBLOCK , FALSE ) ;
}
2006-04-17 18:25:43 +00:00
}
2006-04-07 17:32:14 +00:00
2011-09-01 00:30:51 +00:00
static void set_dtmf_delay ( switch_rtp_t * rtp_session , uint32_t ms , uint32_t max_ms )
2011-08-29 16:04:38 +00:00
{
2011-09-01 00:30:51 +00:00
int upsamp , max_upsamp ;
2012-04-10 21:17:16 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
2011-09-01 00:30:51 +00:00
if ( ! max_ms ) max_ms = ms ;
2011-08-29 16:04:38 +00:00
2011-09-01 00:30:51 +00:00
upsamp = ms * ( rtp_session - > samples_per_second / 1000 ) ;
max_upsamp = max_ms * ( rtp_session - > samples_per_second / 1000 ) ;
rtp_session - > queue_delay = upsamp ;
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_USE_TIMER ) ) {
rtp_session - > max_next_write_samplecount = rtp_session - > timer . samplecount + max_upsamp ;
2012-04-18 14:55:58 +00:00
rtp_session - > next_write_samplecount = rtp_session - > timer . samplecount + upsamp ;
2012-05-08 17:52:47 +00:00
rtp_session - > last_write_ts + = upsamp ;
2011-08-29 16:04:38 +00:00
}
2011-09-01 00:30:51 +00:00
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_DEBUG , " Queue digit delay of %dms \n " , ms ) ;
2011-08-29 16:04:38 +00:00
}
2010-10-23 02:16:12 +00:00
static void do_2833 ( switch_rtp_t * rtp_session , switch_core_session_t * session )
2006-05-04 17:51:53 +00:00
{
switch_frame_flag_t flags = 0 ;
2007-03-09 16:30:34 +00:00
uint32_t samples = rtp_session - > samples_per_interval ;
2006-05-04 17:51:53 +00:00
2011-09-01 00:30:51 +00:00
if ( ! rtp_session - > last_write_ts ) {
return ;
}
2006-05-04 17:51:53 +00:00
if ( rtp_session - > dtmf_data . out_digit_dur > 0 ) {
2008-01-08 22:30:52 +00:00
int x , loops = 1 ;
2011-03-10 02:06:27 +00:00
2006-05-04 17:51:53 +00:00
rtp_session - > dtmf_data . out_digit_sofar + = samples ;
2008-01-11 21:59:17 +00:00
rtp_session - > dtmf_data . out_digit_sub_sofar + = samples ;
2006-05-04 17:51:53 +00:00
2008-01-11 21:59:17 +00:00
if ( rtp_session - > dtmf_data . out_digit_sub_sofar > 0xFFFF ) {
rtp_session - > dtmf_data . out_digit_sub_sofar = samples ;
2008-01-08 21:43:09 +00:00
rtp_session - > dtmf_data . timestamp_dtmf + = 0xFFFF ;
}
2006-05-04 17:51:53 +00:00
if ( rtp_session - > dtmf_data . out_digit_sofar > = rtp_session - > dtmf_data . out_digit_dur ) {
rtp_session - > dtmf_data . out_digit_packet [ 1 ] | = 0x80 ;
loops = 3 ;
}
2008-05-27 04:30:03 +00:00
2008-01-11 21:59:17 +00:00
rtp_session - > dtmf_data . out_digit_packet [ 2 ] = ( unsigned char ) ( rtp_session - > dtmf_data . out_digit_sub_sofar > > 8 ) ;
rtp_session - > dtmf_data . out_digit_packet [ 3 ] = ( unsigned char ) rtp_session - > dtmf_data . out_digit_sub_sofar ;
2008-01-08 21:43:09 +00:00
2006-05-04 17:51:53 +00:00
for ( x = 0 ; x < loops ; x + + ) {
2009-04-03 21:48:52 +00:00
switch_size_t wrote = switch_rtp_write_manual ( rtp_session ,
2010-02-06 03:38:24 +00:00
rtp_session - > dtmf_data . out_digit_packet , 4 , 0 ,
2009-04-03 21:48:52 +00:00
rtp_session - > te , rtp_session - > dtmf_data . timestamp_dtmf , & flags ) ;
2009-04-15 15:08:21 +00:00
rtp_session - > stats . outbound . raw_bytes + = wrote ;
rtp_session - > stats . outbound . dtmf_packet_count + + ;
2009-04-03 21:48:52 +00:00
2011-09-01 00:30:51 +00:00
if ( loops = = 1 ) {
rtp_session - > last_write_ts + = samples ;
if ( rtp_session - > rtp_bugs & RTP_BUG_SONUS_SEND_INVALID_TIMESTAMP_2833 ) {
rtp_session - > dtmf_data . timestamp_dtmf = rtp_session - > last_write_ts ;
}
}
2009-04-03 21:48:52 +00:00
2012-05-08 17:52:47 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_DEBUG , " Send %s packet for [%c] ts=%u dur=%d/%d/%d seq=%d lw=%u \n " ,
2008-05-27 04:30:03 +00:00
loops = = 1 ? " middle " : " end " , rtp_session - > dtmf_data . out_digit ,
2008-01-11 21:59:17 +00:00
rtp_session - > dtmf_data . timestamp_dtmf ,
2008-05-27 04:30:03 +00:00
rtp_session - > dtmf_data . out_digit_sofar ,
2011-09-01 00:30:51 +00:00
rtp_session - > dtmf_data . out_digit_sub_sofar , rtp_session - > dtmf_data . out_digit_dur , rtp_session - > seq , rtp_session - > last_write_ts ) ;
2006-05-04 17:51:53 +00:00
}
2007-03-08 22:18:33 +00:00
2007-07-06 16:19:40 +00:00
if ( loops ! = 1 ) {
2011-09-01 00:30:51 +00:00
rtp_session - > sending_dtmf = 0 ;
2011-08-30 20:00:07 +00:00
rtp_session - > need_mark = 1 ;
2011-03-09 21:17:09 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_USE_TIMER ) ) {
2008-01-11 21:59:17 +00:00
rtp_session - > last_write_samplecount = rtp_session - > timer . samplecount ;
}
2011-09-01 00:30:51 +00:00
2008-01-11 21:59:17 +00:00
rtp_session - > dtmf_data . out_digit_dur = 0 ;
2012-04-18 14:55:58 +00:00
if ( rtp_session - > interdigit_delay ) {
set_dtmf_delay ( rtp_session , rtp_session - > interdigit_delay , rtp_session - > interdigit_delay * 10 ) ;
}
2011-08-24 20:36:59 +00:00
return ;
2007-03-08 22:18:33 +00:00
}
2006-05-04 17:51:53 +00:00
}
2007-03-30 00:13:31 +00:00
if ( ! rtp_session - > dtmf_data . out_digit_dur & & rtp_session - > dtmf_data . dtmf_queue & & switch_queue_size ( rtp_session - > dtmf_data . dtmf_queue ) ) {
2006-05-04 17:51:53 +00:00
void * pop ;
2011-09-01 00:30:51 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_USE_TIMER ) ) {
2012-04-18 14:55:58 +00:00
if ( rtp_session - > timer . samplecount < rtp_session - > next_write_samplecount ) {
2011-09-01 00:30:51 +00:00
return ;
}
2011-11-14 18:37:45 +00:00
2011-09-01 00:30:51 +00:00
if ( rtp_session - > timer . samplecount > = rtp_session - > max_next_write_samplecount ) {
rtp_session - > queue_delay = 0 ;
}
2011-11-14 18:37:45 +00:00
} else if ( rtp_session - > queue_delay ) {
if ( rtp_session - > delay_samples > = rtp_session - > samples_per_interval ) {
rtp_session - > delay_samples - = rtp_session - > samples_per_interval ;
} else {
rtp_session - > delay_samples = 0 ;
}
if ( ! rtp_session - > delay_samples ) {
rtp_session - > queue_delay = 0 ;
2011-09-01 00:30:51 +00:00
}
}
if ( rtp_session - > queue_delay ) {
return ;
}
2011-08-31 17:03:57 +00:00
if ( ! rtp_session - > sending_dtmf ) {
2012-04-20 13:03:03 +00:00
rtp_session - > sending_dtmf = 1 ;
2008-01-14 21:51:31 +00:00
}
2008-05-27 04:30:03 +00:00
2009-12-12 00:04:07 +00:00
if ( switch_queue_trypop ( rtp_session - > dtmf_data . dtmf_queue , & pop ) = = SWITCH_STATUS_SUCCESS ) {
2007-12-22 00:32:20 +00:00
switch_dtmf_t * rdigit = pop ;
2009-04-03 21:48:52 +00:00
switch_size_t wrote ;
2011-08-29 16:04:38 +00:00
if ( rdigit - > digit = = ' w ' ) {
2011-09-01 00:30:51 +00:00
set_dtmf_delay ( rtp_session , 500 , 0 ) ;
2011-08-29 16:04:38 +00:00
free ( rdigit ) ;
return ;
}
if ( rdigit - > digit = = ' W ' ) {
2011-09-01 00:30:51 +00:00
set_dtmf_delay ( rtp_session , 1000 , 0 ) ;
2011-08-29 16:04:38 +00:00
free ( rdigit ) ;
return ;
}
2008-01-09 17:20:05 +00:00
2011-08-31 17:03:57 +00:00
2006-05-04 17:51:53 +00:00
memset ( rtp_session - > dtmf_data . out_digit_packet , 0 , 4 ) ;
2008-01-08 21:43:09 +00:00
rtp_session - > dtmf_data . out_digit_sofar = samples ;
2008-01-11 21:59:17 +00:00
rtp_session - > dtmf_data . out_digit_sub_sofar = samples ;
2006-05-04 17:51:53 +00:00
rtp_session - > dtmf_data . out_digit_dur = rdigit - > duration ;
rtp_session - > dtmf_data . out_digit = rdigit - > digit ;
2007-03-29 22:31:56 +00:00
rtp_session - > dtmf_data . out_digit_packet [ 0 ] = ( unsigned char ) switch_char_to_rfc2833 ( rdigit - > digit ) ;
2006-05-04 17:51:53 +00:00
rtp_session - > dtmf_data . out_digit_packet [ 1 ] = 7 ;
2008-03-03 23:13:12 +00:00
rtp_session - > dtmf_data . out_digit_packet [ 2 ] = ( unsigned char ) ( rtp_session - > dtmf_data . out_digit_sub_sofar > > 8 ) ;
rtp_session - > dtmf_data . out_digit_packet [ 3 ] = ( unsigned char ) rtp_session - > dtmf_data . out_digit_sub_sofar ;
2006-05-04 17:51:53 +00:00
2010-02-06 03:38:24 +00:00
2011-09-01 00:30:51 +00:00
rtp_session - > dtmf_data . timestamp_dtmf = rtp_session - > last_write_ts + samples ;
rtp_session - > last_write_ts = rtp_session - > dtmf_data . timestamp_dtmf ;
2009-05-20 23:04:05 +00:00
wrote = switch_rtp_write_manual ( rtp_session ,
rtp_session - > dtmf_data . out_digit_packet ,
4 ,
rtp_session - > rtp_bugs & RTP_BUG_CISCO_SKIP_MARK_BIT_2833 ? 0 : 1 ,
rtp_session - > te , rtp_session - > dtmf_data . timestamp_dtmf , & flags ) ;
2010-02-06 03:38:24 +00:00
2011-09-01 00:30:51 +00:00
2009-04-15 15:08:21 +00:00
rtp_session - > stats . outbound . raw_bytes + = wrote ;
rtp_session - > stats . outbound . dtmf_packet_count + + ;
2011-09-01 00:30:51 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_DEBUG , " Send start packet for [%c] ts=%u dur=%d/%d/%d seq=%d lw=%d \n " ,
2008-05-27 04:30:03 +00:00
rtp_session - > dtmf_data . out_digit ,
2008-01-11 21:59:17 +00:00
rtp_session - > dtmf_data . timestamp_dtmf ,
2008-05-27 04:30:03 +00:00
rtp_session - > dtmf_data . out_digit_sofar ,
2011-09-01 00:30:51 +00:00
rtp_session - > dtmf_data . out_digit_sub_sofar , rtp_session - > dtmf_data . out_digit_dur , rtp_session - > seq , rtp_session - > last_write_ts ) ;
2008-01-11 21:59:17 +00:00
2006-05-04 17:51:53 +00:00
free ( rdigit ) ;
}
}
}
2009-04-16 22:13:55 +00:00
SWITCH_DECLARE ( void ) rtp_flush_read_buffer ( switch_rtp_t * rtp_session , switch_rtp_flush_t flush )
2008-12-02 21:33:01 +00:00
{
2011-09-06 16:30:28 +00:00
if ( switch_rtp_ready ( rtp_session ) ) {
2011-09-09 17:02:15 +00:00
rtp_session - > last_write_ts = RTP_TS_RESET ;
2011-09-06 16:30:28 +00:00
if ( ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_PROXY_MEDIA ) & &
! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_VIDEO ) ) {
switch_set_flag_locked ( rtp_session , SWITCH_RTP_FLAG_FLUSH ) ;
switch ( flush ) {
case SWITCH_RTP_FLUSH_STICK :
switch_set_flag_locked ( rtp_session , SWITCH_RTP_FLAG_STICKY_FLUSH ) ;
break ;
case SWITCH_RTP_FLUSH_UNSTICK :
switch_clear_flag_locked ( rtp_session , SWITCH_RTP_FLAG_STICKY_FLUSH ) ;
break ;
default :
break ;
}
2009-04-16 22:13:55 +00:00
}
2008-12-03 16:52:58 +00:00
}
2008-12-02 21:33:01 +00:00
}
static void do_flush ( switch_rtp_t * rtp_session )
2008-11-25 17:53:35 +00:00
{
int was_blocking = 0 ;
switch_size_t bytes ;
2010-12-10 23:47:24 +00:00
if ( ! switch_rtp_ready ( rtp_session ) | |
switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_PROXY_MEDIA ) | |
switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_VIDEO )
) {
2008-11-25 17:53:35 +00:00
return ;
}
2010-02-06 03:38:24 +00:00
2008-11-25 17:53:35 +00:00
READ_INC ( rtp_session ) ;
2008-11-26 15:22:26 +00:00
if ( switch_rtp_ready ( rtp_session ) ) {
2010-12-10 23:47:24 +00:00
uint32_t flushed = 0 ;
2009-10-29 17:52:52 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_DEBUG_RTP_READ ) ) {
2009-10-29 17:29:05 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
if ( ! session ) {
2009-10-29 17:52:52 +00:00
switch_clear_flag ( rtp_session , SWITCH_RTP_FLAG_DEBUG_RTP_READ ) ;
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_CRIT , " RTP HAS NO SESSION! \n " ) ;
2009-10-29 17:29:05 +00:00
} else {
2010-02-06 03:38:24 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) ,
SWITCH_LOG_CONSOLE , " %s FLUSH \n " , switch_channel_get_name ( switch_core_session_get_channel ( session ) )
2010-04-20 00:07:23 +00:00
) ;
2009-10-29 17:29:05 +00:00
}
}
2008-11-26 15:22:26 +00:00
if ( ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_NOBLOCK ) ) {
was_blocking = 1 ;
2010-02-06 03:38:24 +00:00
switch_set_flag_locked ( rtp_session , SWITCH_RTP_FLAG_NOBLOCK ) ;
2008-11-26 15:22:26 +00:00
switch_socket_opt_set ( rtp_session - > sock_input , SWITCH_SO_NONBLOCK , TRUE ) ;
}
2010-02-06 03:38:24 +00:00
2008-11-26 15:22:26 +00:00
do {
if ( switch_rtp_ready ( rtp_session ) ) {
bytes = sizeof ( rtp_msg_t ) ;
2011-04-22 21:43:29 +00:00
switch_socket_recvfrom ( rtp_session - > from_addr , rtp_session - > sock_input , 0 , ( void * ) & rtp_session - > recv_msg , & bytes ) ;
2009-04-03 21:48:52 +00:00
if ( bytes ) {
2011-01-21 21:03:19 +00:00
int do_cng = 0 ;
/* Make sure to handle RFC2833 packets, even if we're flushing the packets */
if ( bytes > rtp_header_len & & rtp_session - > recv_te & & rtp_session - > recv_msg . header . pt = = rtp_session - > recv_te ) {
handle_rfc2833 ( rtp_session , bytes , & do_cng ) ;
# ifdef DEBUG_2833
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " *** RTP packet handled in flush loop %d *** \n " , do_cng ) ;
2011-01-21 21:03:19 +00:00
# endif
}
2010-12-10 23:47:24 +00:00
flushed + + ;
2009-04-15 15:08:21 +00:00
rtp_session - > stats . inbound . raw_bytes + = bytes ;
rtp_session - > stats . inbound . flush_packet_count + + ;
rtp_session - > stats . inbound . packet_count + + ;
2009-04-03 21:48:52 +00:00
}
2008-11-26 15:22:26 +00:00
} else {
break ;
}
2010-02-06 03:38:24 +00:00
} while ( bytes > 0 ) ;
2010-12-10 23:47:24 +00:00
if ( rtp_session - > jb & & flushed ) {
stfu_n_sync ( rtp_session - > jb , flushed ) ;
}
2008-11-26 15:22:26 +00:00
if ( was_blocking & & switch_rtp_ready ( rtp_session ) ) {
switch_clear_flag_locked ( rtp_session , SWITCH_RTP_FLAG_NOBLOCK ) ;
switch_socket_opt_set ( rtp_session - > sock_input , SWITCH_SO_NONBLOCK , FALSE ) ;
}
2008-11-25 17:53:35 +00:00
}
2010-02-06 03:38:24 +00:00
2008-11-25 17:53:35 +00:00
READ_DEC ( rtp_session ) ;
}
2008-10-01 18:02:46 +00:00
# define return_cng_frame() do_cng = 1; goto timer_check
2011-02-21 16:51:26 +00:00
static switch_status_t read_rtp_packet ( switch_rtp_t * rtp_session , switch_size_t * bytes , switch_frame_flag_t * flags , switch_bool_t return_jb_packet )
2009-03-31 19:10:43 +00:00
{
switch_status_t status = SWITCH_STATUS_FALSE ;
stfu_frame_t * jb_frame ;
2010-12-23 02:38:57 +00:00
uint32_t ts ;
2010-02-06 03:38:24 +00:00
2009-03-31 19:10:43 +00:00
switch_assert ( bytes ) ;
2011-02-25 17:59:45 +00:00
more :
2010-08-05 22:56:18 +00:00
* bytes = sizeof ( rtp_msg_t ) ;
status = switch_socket_recvfrom ( rtp_session - > from_addr , rtp_session - > sock_input , 0 , ( void * ) & rtp_session - > recv_msg , bytes ) ;
2010-12-23 02:38:57 +00:00
ts = ntohl ( rtp_session - > recv_msg . header . ts ) ;
2011-01-21 21:03:19 +00:00
2011-03-21 19:31:10 +00:00
if ( * bytes ) {
2011-01-21 21:03:19 +00:00
uint16_t seq = ntohs ( ( uint16_t ) rtp_session - > recv_msg . header . seq ) ;
2011-03-21 19:31:10 +00:00
2011-01-21 21:03:19 +00:00
if ( rtp_session - > last_seq & & rtp_session - > last_seq + 1 ! = seq ) {
# ifdef DEBUG_MISSED_SEQ
switch_size_t flushed_packets_diff = rtp_session - > stats . inbound . flush_packet_count - rtp_session - > last_flush_packet_count ;
switch_size_t num_missed = ( switch_size_t ) seq - ( rtp_session - > last_seq + 1 ) ;
if ( num_missed = = 1 ) { /* We missed one packet */
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " Missed one RTP frame with sequence [%d]%s. Time since last read [%d] \n " ,
2011-01-21 21:03:19 +00:00
rtp_session - > last_seq + 1 , ( flushed_packets_diff = = 1 ) ? " (flushed by FS) " : " (missed) " ,
rtp_session - > last_read_time ? switch_micro_time_now ( ) - rtp_session - > last_read_time : 0 ) ;
} else { /* We missed multiple packets */
if ( flushed_packets_diff = = 0 ) {
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR ,
2011-01-21 21:03:19 +00:00
" Missed %d RTP frames from sequence [%d] to [%d] (missed). Time since last read [%d] \n " ,
num_missed , rtp_session - > last_seq + 1 , seq - 1 ,
rtp_session - > last_read_time ? switch_micro_time_now ( ) - rtp_session - > last_read_time : 0 ) ;
} else if ( flushed_packets_diff = = num_missed ) {
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR ,
2011-01-21 21:03:19 +00:00
" Missed %d RTP frames from sequence [%d] to [%d] (flushed by FS). Time since last read [%d] \n " ,
num_missed , rtp_session - > last_seq + 1 , seq - 1 ,
rtp_session - > last_read_time ? switch_micro_time_now ( ) - rtp_session - > last_read_time : 0 ) ;
} else if ( num_missed > flushed_packets_diff ) {
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR ,
2011-01-21 21:03:19 +00:00
" Missed %d RTP frames from sequence [%d] to [%d] (%d packets flushed by FS, %d packets missed). "
" Time since last read [%d] \n " ,
num_missed , rtp_session - > last_seq + 1 , seq - 1 ,
flushed_packets_diff , num_missed - flushed_packets_diff ,
rtp_session - > last_read_time ? switch_micro_time_now ( ) - rtp_session - > last_read_time : 0 ) ;
} else {
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR ,
2011-01-21 21:03:19 +00:00
" Missed %d RTP frames from sequence [%d] to [%d] (%d packets flushed by FS). Time since last read [%d] \n " ,
num_missed , rtp_session - > last_seq + 1 , seq - 1 ,
flushed_packets_diff , rtp_session - > last_read_time ? switch_micro_time_now ( ) - rtp_session - > last_read_time : 0 ) ;
}
}
# endif
}
rtp_session - > last_seq = seq ;
}
rtp_session - > last_flush_packet_count = rtp_session - > stats . inbound . flush_packet_count ;
rtp_session - > last_read_time = switch_micro_time_now ( ) ;
2010-12-23 19:18:41 +00:00
if ( * bytes & & ( ! rtp_session - > recv_te | | rtp_session - > recv_msg . header . pt ! = rtp_session - > recv_te ) & &
2011-01-05 22:25:07 +00:00
ts & & ! rtp_session - > jb & & ! rtp_session - > pause_jb & & ts = = rtp_session - > last_cng_ts ) {
2010-12-23 02:38:57 +00:00
/* we already sent this frame..... */
* bytes = 0 ;
return SWITCH_STATUS_SUCCESS ;
}
2009-03-31 19:10:43 +00:00
2009-04-03 21:48:52 +00:00
if ( * bytes ) {
2009-04-15 15:08:21 +00:00
rtp_session - > stats . inbound . raw_bytes + = * bytes ;
2010-03-16 22:49:54 +00:00
if ( rtp_session - > recv_te & & rtp_session - > recv_msg . header . pt = = rtp_session - > recv_te ) {
2009-04-15 15:08:21 +00:00
rtp_session - > stats . inbound . dtmf_packet_count + + ;
2009-04-03 21:48:52 +00:00
} else if ( rtp_session - > cng_pt & & ( rtp_session - > recv_msg . header . pt = = rtp_session - > cng_pt | | rtp_session - > recv_msg . header . pt = = 13 ) ) {
2009-04-15 15:08:21 +00:00
rtp_session - > stats . inbound . cng_packet_count + + ;
2009-04-03 21:48:52 +00:00
} else {
2009-04-15 15:08:21 +00:00
rtp_session - > stats . inbound . media_packet_count + + ;
rtp_session - > stats . inbound . media_bytes + = * bytes ;
2009-04-03 21:48:52 +00:00
}
2010-02-06 03:38:24 +00:00
2009-04-15 15:08:21 +00:00
rtp_session - > stats . inbound . packet_count + + ;
2010-01-08 00:45:09 +00:00
2012-06-22 23:08:59 +00:00
2011-03-22 17:07:06 +00:00
if ( ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_PROXY_MEDIA ) & & ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_UDPTL ) ) {
2011-03-18 21:58:52 +00:00
# ifdef ENABLE_ZRTP
2011-03-22 17:07:06 +00:00
/* ZRTP Recv */
2012-01-25 05:38:53 +00:00
if ( zrtp_on ) {
unsigned int sbytes = ( int ) * bytes ;
zrtp_status_t stat = 0 ;
stat = zrtp_process_srtp ( rtp_session - > zrtp_stream , ( void * ) & rtp_session - > recv_msg , & sbytes ) ;
switch ( stat ) {
case zrtp_status_ok :
* bytes = sbytes ;
break ;
case zrtp_status_drop :
2012-05-24 03:58:26 +00:00
/* switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Error: zRTP protection drop with code %d\n", stat); */
2012-01-25 05:38:53 +00:00
* bytes = 0 ;
return SWITCH_STATUS_SUCCESS ;
case zrtp_status_fail :
switch_log_printf ( SWITCH_CHANNEL_LOG , SWITCH_LOG_ERROR , " Error: zRTP protection fail with code %d \n " , stat ) ;
return SWITCH_STATUS_FALSE ;
default :
break ;
}
2011-03-18 21:58:52 +00:00
}
# endif
2011-05-02 16:33:57 +00:00
# ifdef ENABLE_SRTP
2012-06-22 23:08:59 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_SECURE_RECV ) & & rtp_session - > rtcp_recv_msg . header . version = = 2 ) {
2011-03-22 17:07:06 +00:00
int sbytes = ( int ) * bytes ;
err_status_t stat = 0 ;
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_SECURE_RECV_RESET ) ) {
switch_clear_flag_locked ( rtp_session , SWITCH_RTP_FLAG_SECURE_RECV_RESET ) ;
srtp_dealloc ( rtp_session - > recv_ctx ) ;
rtp_session - > recv_ctx = NULL ;
if ( ( stat = srtp_create ( & rtp_session - > recv_ctx , & rtp_session - > recv_policy ) ) ) {
2012-04-10 21:17:16 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " Error! RE-Activating Secure RTP RECV \n " ) ;
2011-03-22 17:07:06 +00:00
return SWITCH_STATUS_FALSE ;
} else {
2012-04-10 21:17:16 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_INFO , " RE-Activating Secure RTP RECV \n " ) ;
2011-03-22 17:07:06 +00:00
rtp_session - > srtp_errs = 0 ;
}
2011-03-18 21:58:52 +00:00
}
2011-03-22 17:07:06 +00:00
if ( ! ( * flags & SFF_PLC ) ) {
stat = srtp_unprotect ( rtp_session - > recv_ctx , & rtp_session - > recv_msg . header , & sbytes ) ;
}
2011-03-18 21:58:52 +00:00
2011-03-22 17:07:06 +00:00
if ( stat & & rtp_session - > recv_msg . header . pt ! = rtp_session - > recv_te & & rtp_session - > recv_msg . header . pt ! = rtp_session - > cng_pt ) {
if ( + + rtp_session - > srtp_errs > = MAX_SRTP_ERRS ) {
2012-04-10 21:17:16 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR ,
2011-03-22 17:07:06 +00:00
" Error: SRTP unprotect failed with code %d%s \n " , stat ,
stat = = err_status_replay_fail ? " (replay check failed) " : stat = =
err_status_auth_fail ? " (auth check failed) " : " " ) ;
return SWITCH_STATUS_FALSE ;
} else {
sbytes = 0 ;
}
2011-03-18 21:58:52 +00:00
} else {
2011-03-22 17:07:06 +00:00
rtp_session - > srtp_errs = 0 ;
2011-03-18 21:58:52 +00:00
}
2011-03-22 17:07:06 +00:00
* bytes = sbytes ;
}
2011-05-02 16:33:57 +00:00
# endif
2011-03-18 21:58:52 +00:00
}
}
2011-03-22 17:07:06 +00:00
if ( ( rtp_session - > recv_te & & rtp_session - > recv_msg . header . pt = = rtp_session - > recv_te ) | |
( * bytes < rtp_header_len & & * bytes > 0 ) | |
switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_PROXY_MEDIA ) | | switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_UDPTL ) ) {
return SWITCH_STATUS_SUCCESS ;
}
rtp_session - > last_read_ts = ts ;
2011-03-21 20:03:44 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_BYTESWAP ) & & rtp_session - > recv_msg . header . pt = = rtp_session - > rpayload ) {
switch_swap_linear ( ( int16_t * ) rtp_session - > recv_msg . body , ( int ) * bytes - rtp_header_len ) ;
}
2011-03-18 21:58:52 +00:00
2011-01-05 22:25:07 +00:00
if ( rtp_session - > jb & & ! rtp_session - > pause_jb & & rtp_session - > recv_msg . header . version = = 2 & & * bytes ) {
2010-03-27 00:29:54 +00:00
if ( rtp_session - > recv_msg . header . m & & rtp_session - > recv_msg . header . pt ! = rtp_session - > recv_te & &
2010-06-07 19:06:41 +00:00
! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_VIDEO ) & & ! ( rtp_session - > rtp_bugs & RTP_BUG_IGNORE_MARK_BIT ) ) {
2009-03-31 19:10:43 +00:00
stfu_n_reset ( rtp_session - > jb ) ;
}
2010-02-06 03:38:24 +00:00
2011-03-09 21:17:09 +00:00
if ( ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_USE_TIMER ) & & rtp_session - > timer . interval ) {
switch_core_timer_sync ( & rtp_session - > timer ) ;
}
2011-02-25 17:59:45 +00:00
if ( stfu_n_eat ( rtp_session - > jb , rtp_session - > last_read_ts ,
rtp_session - > recv_msg . header . pt ,
rtp_session - > recv_msg . body , * bytes - rtp_header_len , rtp_session - > timer . samplecount ) = = STFU_ITS_TOO_LATE ) {
2011-03-09 21:17:09 +00:00
2011-02-25 17:59:45 +00:00
goto more ;
}
2009-03-31 19:10:43 +00:00
status = SWITCH_STATUS_FALSE ;
2011-02-21 16:51:26 +00:00
if ( ! return_jb_packet ) {
return status ;
}
* bytes = 0 ;
2009-03-31 19:10:43 +00:00
}
2011-02-24 21:29:22 +00:00
if ( rtp_session - > jb & & ! rtp_session - > pause_jb ) {
2009-03-31 19:10:43 +00:00
if ( ( jb_frame = stfu_n_read_a_frame ( rtp_session - > jb ) ) ) {
memcpy ( rtp_session - > recv_msg . body , jb_frame - > data , jb_frame - > dlen ) ;
2010-12-21 21:35:13 +00:00
2009-03-31 19:10:43 +00:00
if ( jb_frame - > plc ) {
2010-12-10 23:47:24 +00:00
( * flags ) | = SFF_PLC ;
2009-04-03 21:48:52 +00:00
} else {
2009-04-15 15:08:21 +00:00
rtp_session - > stats . inbound . jb_packet_count + + ;
2009-03-31 19:10:43 +00:00
}
* bytes = jb_frame - > dlen + rtp_header_len ;
rtp_session - > recv_msg . header . ts = htonl ( jb_frame - > ts ) ;
2010-01-08 00:45:09 +00:00
rtp_session - > recv_msg . header . pt = jb_frame - > pt ;
2009-03-31 19:10:43 +00:00
status = SWITCH_STATUS_SUCCESS ;
}
}
return status ;
}
2010-04-20 00:07:23 +00:00
static switch_status_t read_rtcp_packet ( switch_rtp_t * rtp_session , switch_size_t * bytes , switch_frame_flag_t * flags )
{
switch_status_t status = SWITCH_STATUS_FALSE ;
if ( ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_ENABLE_RTCP ) ) {
return SWITCH_STATUS_FALSE ;
}
switch_assert ( bytes ) ;
* bytes = sizeof ( rtcp_msg_t ) ;
if ( ( status = switch_socket_recvfrom ( rtp_session - > rtcp_from_addr , rtp_session - > rtcp_sock_input , 0 , ( void * ) & rtp_session - > rtcp_recv_msg , bytes ) )
! = SWITCH_STATUS_SUCCESS ) {
* bytes = 0 ;
}
2011-05-02 16:33:57 +00:00
# ifdef ENABLE_SRTP
2012-06-22 23:08:59 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_SECURE_RECV ) & & rtp_session - > rtcp_recv_msg . header . version = = 2 ) {
2010-04-20 00:07:23 +00:00
int sbytes = ( int ) * bytes ;
err_status_t stat = 0 ;
stat = srtp_unprotect_rtcp ( rtp_session - > recv_ctx , & rtp_session - > rtcp_recv_msg . header , & sbytes ) ;
if ( stat ) {
if ( + + rtp_session - > srtp_errs > = MAX_SRTP_ERRS ) {
2012-04-10 21:17:16 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR ,
2010-04-20 00:07:23 +00:00
" Error: SRTP RTCP unprotect failed with code %d%s \n " , stat ,
stat = = err_status_replay_fail ? " (replay check failed) " : stat = =
err_status_auth_fail ? " (auth check failed) " : " " ) ;
return SWITCH_STATUS_FALSE ;
} else {
sbytes = 0 ;
}
} else {
rtp_session - > srtp_errs = 0 ;
}
* bytes = sbytes ;
}
2011-05-02 16:33:57 +00:00
# endif
2010-04-20 00:07:23 +00:00
# ifdef ENABLE_ZRTP
2012-01-25 05:38:53 +00:00
if ( zrtp_on & & ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_PROXY_MEDIA ) ) {
/* ZRTP Recv */
if ( bytes ) {
unsigned int sbytes = ( int ) * bytes ;
zrtp_status_t stat = 0 ;
stat = zrtp_process_srtcp ( rtp_session - > zrtp_stream , ( void * ) & rtp_session - > rtcp_recv_msg , & sbytes ) ;
switch ( stat ) {
case zrtp_status_ok :
* bytes = sbytes ;
break ;
case zrtp_status_drop :
switch_log_printf ( SWITCH_CHANNEL_LOG , SWITCH_LOG_ERROR , " Error: zRTP protection drop with code %d \n " , stat ) ;
* bytes = 0 ;
break ;
case zrtp_status_fail :
switch_log_printf ( SWITCH_CHANNEL_LOG , SWITCH_LOG_ERROR , " Error: zRTP protection fail with code %d \n " , stat ) ;
* bytes = 0 ;
break ;
default :
break ;
}
2010-04-20 00:07:23 +00:00
}
}
# endif
if ( * bytes ) {
2012-04-10 21:17:16 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_DEBUG10 , " Received an RTCP packet of length % " SWITCH_SIZE_T_FMT " bytes \n " , * bytes ) ;
2010-04-20 00:07:23 +00:00
if ( rtp_session - > rtcp_recv_msg . header . version = = 2 ) {
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_DEBUG10 , " RTCP packet type is %d \n " , rtp_session - > rtcp_recv_msg . header . type ) ;
2010-04-20 00:07:23 +00:00
if ( rtp_session - > rtcp_recv_msg . header . type = = 200 ) {
struct switch_rtcp_senderinfo * sr = ( struct switch_rtcp_senderinfo * ) rtp_session - > rtcp_recv_msg . body ;
rtp_session - > rtcp_fresh_frame = 1 ;
2010-09-10 16:59:47 +00:00
rtp_session - > stats . rtcp . packet_count + = sr - > pc ;
rtp_session - > stats . rtcp . octet_count + = sr - > oc ;
2010-04-20 00:07:23 +00:00
/* sender report */
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_DEBUG10 , " Received a SR with %d report blocks, " \
2010-04-20 00:07:23 +00:00
" length in words = %d, " \
" SSRC = 0x%X, " \
" NTP MSW = %u, " \
" NTP LSW = %u, " \
" RTP timestamp = %u, " \
" Sender Packet Count = %u, " \
" Sender Octet Count = %u \n " ,
rtp_session - > rtcp_recv_msg . header . count ,
2010-04-26 15:55:46 +00:00
ntohs ( ( uint16_t ) rtp_session - > rtcp_recv_msg . header . length ) ,
2010-04-20 00:07:23 +00:00
ntohl ( sr - > ssrc ) ,
ntohl ( sr - > ntp_msw ) ,
ntohl ( sr - > ntp_lsw ) ,
ntohl ( sr - > ts ) ,
ntohl ( sr - > pc ) ,
ntohl ( sr - > oc ) ) ;
}
} else {
2012-06-22 23:08:59 +00:00
if ( rtp_session - > rtcp_recv_msg . header . version ! = 2 ) {
if ( rtp_session - > rtcp_recv_msg . header . version = = 0 ) {
if ( rtp_session - > ice . ice_user ) {
handle_ice ( rtp_session , & rtp_session - > rtcp_ice , ( void * ) & rtp_session - > rtcp_recv_msg , * bytes ) ;
}
} else {
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) ,
SWITCH_LOG_DEBUG , " Received an unsupported RTCP packet version %d \n n " , rtp_session - > rtcp_recv_msg . header . version ) ;
}
}
2010-04-20 00:07:23 +00:00
2012-06-22 23:08:59 +00:00
status = SWITCH_STATUS_SUCCESS ;
}
2010-04-20 00:07:23 +00:00
}
return status ;
}
2012-06-22 23:08:59 +00:00
2008-05-08 19:19:47 +00:00
static int rtp_common_read ( switch_rtp_t * rtp_session , switch_payload_t * payload_type , switch_frame_flag_t * flags , switch_io_flag_t io_flags )
2006-04-03 21:00:13 +00:00
{
2009-11-24 21:40:28 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
switch_channel_t * channel = NULL ;
2006-11-16 18:48:53 +00:00
switch_size_t bytes = 0 ;
2010-04-20 00:07:23 +00:00
switch_size_t rtcp_bytes = 0 ;
2009-03-30 21:12:06 +00:00
switch_status_t status = SWITCH_STATUS_SUCCESS , poll_status = SWITCH_STATUS_SUCCESS ;
2010-04-20 00:07:23 +00:00
switch_status_t rtcp_status = SWITCH_STATUS_SUCCESS , rtcp_poll_status = SWITCH_STATUS_SUCCESS ;
2008-11-12 10:42:55 +00:00
int check = 0 ;
2008-01-18 18:14:53 +00:00
int ret = - 1 ;
2008-09-29 23:03:27 +00:00
int sleep_mss = 1000 ;
2009-03-30 21:12:06 +00:00
int poll_sec = 5 ;
int poll_loop = 0 ;
int fdr = 0 ;
2010-04-20 00:07:23 +00:00
int rtcp_fdr = 0 ;
2009-03-31 19:10:43 +00:00
int hot_socket = 0 ;
2010-08-05 23:43:30 +00:00
int read_loops = 0 ;
2008-10-01 18:02:46 +00:00
2009-11-24 21:40:28 +00:00
if ( session ) {
channel = switch_core_session_get_channel ( session ) ;
}
2008-02-25 16:35:19 +00:00
if ( ! switch_rtp_ready ( rtp_session ) ) {
return - 1 ;
}
2011-03-09 21:17:09 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_USE_TIMER ) ) {
2008-09-30 17:24:04 +00:00
sleep_mss = rtp_session - > timer . interval * 1000 ;
2006-09-16 20:46:52 +00:00
}
2008-01-18 18:14:53 +00:00
READ_INC ( rtp_session ) ;
2012-06-22 23:08:59 +00:00
2007-03-29 22:31:56 +00:00
while ( switch_rtp_ready ( rtp_session ) ) {
2008-02-13 00:32:09 +00:00
int do_cng = 0 ;
2011-02-18 23:26:58 +00:00
int read_pretriggered = 0 ;
2009-12-12 00:04:07 +00:00
bytes = 0 ;
2008-02-13 00:32:09 +00:00
2011-03-09 21:17:09 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_USE_TIMER ) ) {
2010-08-05 22:56:18 +00:00
if ( ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_AUTOFLUSH ) | | switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_STICKY_FLUSH ) ) & &
rtp_session - > read_pollfd ) {
if ( switch_poll ( rtp_session - > read_pollfd , 1 , & fdr , 0 ) = = SWITCH_STATUS_SUCCESS ) {
2011-02-21 16:51:26 +00:00
status = read_rtp_packet ( rtp_session , & bytes , flags , SWITCH_FALSE ) ;
2012-04-10 21:17:16 +00:00
/* switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Initial (%i) %d\n", status, bytes); */
2011-02-21 16:51:26 +00:00
if ( status ! = SWITCH_STATUS_FALSE ) {
read_pretriggered = 1 ;
}
if ( bytes ) {
if ( switch_poll ( rtp_session - > read_pollfd , 1 , & fdr , 0 ) = = SWITCH_STATUS_SUCCESS ) {
2012-04-10 21:17:16 +00:00
/* switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Trigger %d\n", rtp_session->hot_hits); */
2011-02-21 16:51:26 +00:00
rtp_session - > hot_hits + = rtp_session - > samples_per_interval ;
} else {
rtp_session - > hot_hits = 0 ;
}
2011-02-18 23:26:58 +00:00
}
2010-12-21 21:35:13 +00:00
2010-08-05 22:56:18 +00:00
if ( rtp_session - > hot_hits > = rtp_session - > samples_per_second * 5 ) {
switch_set_flag ( rtp_session , SWITCH_RTP_FLAG_FLUSH ) ;
hot_socket = 1 ;
}
} else {
rtp_session - > hot_hits = 0 ;
}
2009-03-30 21:12:06 +00:00
}
2010-08-05 22:56:18 +00:00
2009-06-04 18:56:34 +00:00
if ( hot_socket ) {
2009-09-10 00:46:24 +00:00
rtp_session - > sync_packets + + ;
2009-06-04 18:56:34 +00:00
switch_core_timer_sync ( & rtp_session - > timer ) ;
} else {
2009-09-10 00:46:24 +00:00
if ( rtp_session - > sync_packets ) {
#if 0
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_DEBUG ,
2010-02-06 03:38:24 +00:00
" Auto-Flush catching up %d packets (%d)ms. \n " ,
2009-09-14 15:19:53 +00:00
rtp_session - > sync_packets , ( rtp_session - > ms_per_packet * rtp_session - > sync_packets ) / 1000 ) ;
2009-09-10 00:46:24 +00:00
# endif
rtp_session - > sync_packets = 0 ;
}
2009-03-30 21:12:06 +00:00
switch_core_timer_next ( & rtp_session - > timer ) ;
}
2008-11-10 18:26:41 +00:00
}
2010-04-20 00:07:23 +00:00
recvfrom :
2012-06-22 23:08:59 +00:00
rtp_session - > stats . read_count + + ;
2011-02-18 23:26:58 +00:00
if ( ! read_pretriggered ) {
bytes = 0 ;
}
2010-08-05 23:43:30 +00:00
read_loops + + ;
2012-06-22 23:08:59 +00:00
//poll_loop = 0;
2009-01-12 22:18:48 +00:00
2009-06-04 18:56:34 +00:00
if ( ! switch_rtp_ready ( rtp_session ) ) {
break ;
}
2011-03-09 21:17:09 +00:00
if ( ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_USE_TIMER ) & & rtp_session - > read_pollfd ) {
2009-09-02 19:55:08 +00:00
int pt = poll_sec * 1000000 ;
2011-03-10 02:06:27 +00:00
do_2833 ( rtp_session , session ) ;
2012-06-22 23:08:59 +00:00
if ( ( rtp_session - > ice . ice_user & & switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_VIDEO ) ) | | rtp_session - > dtmf_data . out_digit_dur > 0 | | rtp_session - > dtmf_data . in_digit_sanity | | rtp_session - > sending_dtmf | |
2011-03-10 02:06:27 +00:00
switch_queue_size ( rtp_session - > dtmf_data . dtmf_queue ) | | switch_queue_size ( rtp_session - > dtmf_data . dtmf_inqueue ) ) {
2009-09-02 19:55:08 +00:00
pt = 20000 ;
}
2010-05-27 01:12:54 +00:00
2010-09-25 14:19:52 +00:00
if ( ( io_flags & SWITCH_IO_FLAG_NOBLOCK ) ) {
pt = 0 ;
}
2009-09-02 19:55:08 +00:00
poll_status = switch_poll ( rtp_session - > read_pollfd , 1 , & fdr , pt ) ;
2011-02-18 22:54:51 +00:00
2012-06-22 23:08:59 +00:00
2009-09-02 19:55:08 +00:00
if ( rtp_session - > dtmf_data . out_digit_dur > 0 ) {
2011-02-18 22:54:51 +00:00
return_cng_frame ( ) ;
2009-09-02 19:55:08 +00:00
}
2010-02-06 03:38:24 +00:00
}
2009-03-30 21:12:06 +00:00
if ( poll_status = = SWITCH_STATUS_SUCCESS ) {
2011-02-21 16:51:26 +00:00
if ( read_pretriggered ) {
read_pretriggered = 0 ;
} else {
status = read_rtp_packet ( rtp_session , & bytes , flags , SWITCH_TRUE ) ;
2012-06-22 23:08:59 +00:00
//switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Read bytes (%i) %ld\n", status, bytes);
2011-02-18 23:26:58 +00:00
}
2012-06-22 23:08:59 +00:00
poll_loop = 0 ;
2009-03-30 21:12:06 +00:00
} else {
2012-06-22 23:08:59 +00:00
int vid_cng = 1 , ice = 0 ;
2009-03-30 21:12:06 +00:00
if ( ! SWITCH_STATUS_IS_BREAK ( poll_status ) & & poll_status ! = SWITCH_STATUS_TIMEOUT ) {
2011-01-11 21:09:03 +00:00
char tmp [ 128 ] = " " ;
strerror_r ( poll_status , tmp , sizeof ( tmp ) ) ;
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " Poll failed with error: %d [%s] \n " , poll_status , tmp ) ;
2009-03-30 21:12:06 +00:00
ret = - 1 ;
goto end ;
}
2012-06-22 23:08:59 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_VIDEO ) ) {
if ( rtp_session - > rtcp_ice . ice_user ) {
if ( ice_out ( rtp_session , & rtp_session - > rtcp_ice ) ! = SWITCH_STATUS_SUCCESS ) {
ret = - 1 ;
goto end ;
}
vid_cng = 0 ;
ice = 1 ;
}
if ( rtp_session - > ice . ice_user ) {
if ( ice_out ( rtp_session , & rtp_session - > ice ) ! = SWITCH_STATUS_SUCCESS ) {
ret = - 1 ;
goto end ;
}
vid_cng = 0 ;
ice = 1 ;
}
if ( ice ) {
if ( check_srtp_and_ice ( rtp_session ) ) {
ret = - 1 ;
goto end ;
}
if ( poll_loop < 50 ) {
poll_loop + + ;
goto recvfrom ;
}
}
}
poll_loop + + ;
2009-12-30 23:10:38 +00:00
2011-01-11 21:09:03 +00:00
if ( ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_UDPTL ) ) {
rtp_session - > missed_count + = ( poll_sec * 1000 ) / ( rtp_session - > ms_per_packet ? rtp_session - > ms_per_packet / 1000 : 20 ) ;
bytes = 0 ;
if ( rtp_session - > max_missed_packets ) {
if ( rtp_session - > missed_count > = rtp_session - > max_missed_packets ) {
ret = - 2 ;
goto end ;
}
2009-12-30 23:10:38 +00:00
}
}
2011-01-11 21:09:03 +00:00
2011-01-07 22:04:24 +00:00
if ( ( ! ( io_flags & SWITCH_IO_FLAG_NOBLOCK ) ) & &
2012-06-22 23:08:59 +00:00
( rtp_session - > dtmf_data . out_digit_dur = = 0 | | ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_VIDEO ) & & vid_cng ) ) ) {
2009-11-19 18:22:05 +00:00
return_cng_frame ( ) ;
}
2009-03-30 21:12:06 +00:00
}
2011-01-13 02:02:49 +00:00
2010-04-20 00:07:23 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_ENABLE_RTCP ) & & rtp_session - > rtcp_read_pollfd ) {
rtcp_poll_status = switch_poll ( rtp_session - > rtcp_read_pollfd , 1 , & rtcp_fdr , 0 ) ;
if ( rtcp_poll_status = = SWITCH_STATUS_SUCCESS ) {
rtcp_status = read_rtcp_packet ( rtp_session , & rtcp_bytes , flags ) ;
2010-04-20 21:15:37 +00:00
2012-03-27 20:53:05 +00:00
if ( rtcp_status = = SWITCH_STATUS_SUCCESS ) {
switch_rtp_reset_media_timer ( rtp_session ) ;
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_RTCP_PASSTHRU ) ) {
2012-06-22 23:08:59 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
switch_channel_t * channel = switch_core_session_get_channel ( session ) ;
2010-04-20 21:15:37 +00:00
2012-06-22 23:08:59 +00:00
const char * uuid = switch_channel_get_partner_uuid ( channel ) ;
if ( uuid ) {
switch_core_session_t * other_session ;
switch_rtp_t * other_rtp_session = NULL ;
2010-04-20 21:15:37 +00:00
2012-06-22 23:08:59 +00:00
if ( ( other_session = switch_core_session_locate ( uuid ) ) ) {
switch_channel_t * other_channel = switch_core_session_get_channel ( other_session ) ;
if ( ( other_rtp_session = switch_channel_get_private ( other_channel , " __rtcp_audio_rtp_session " ) ) & &
other_rtp_session - > rtcp_sock_output & &
switch_test_flag ( other_rtp_session , SWITCH_RTP_FLAG_ENABLE_RTCP ) ) {
* other_rtp_session - > rtcp_send_msg . body = * rtp_session - > rtcp_recv_msg . body ;
2010-04-20 21:15:37 +00:00
2011-05-02 16:33:57 +00:00
# ifdef ENABLE_SRTP
2012-06-22 23:08:59 +00:00
if ( switch_test_flag ( other_rtp_session , SWITCH_RTP_FLAG_SECURE_SEND ) ) {
int sbytes = ( int ) rtcp_bytes ;
int stat = srtp_protect_rtcp ( other_rtp_session - > send_ctx , & other_rtp_session - > rtcp_send_msg . header , & sbytes ) ;
if ( stat ) {
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " Error: SRTP RTCP protection failed with code %d \n " , stat ) ;
}
rtcp_bytes = sbytes ;
2010-04-20 21:15:37 +00:00
}
2011-05-02 16:33:57 +00:00
# endif
2010-04-20 21:15:37 +00:00
# ifdef ENABLE_ZRTP
2012-06-22 23:08:59 +00:00
/* ZRTP Send */
if ( zrtp_on & & ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_PROXY_MEDIA ) ) {
unsigned int sbytes = ( unsigned int ) bytes ;
zrtp_status_t stat = zrtp_status_fail ;
2012-01-25 05:38:53 +00:00
2012-06-22 23:08:59 +00:00
stat = zrtp_process_rtcp ( other_rtp_session - > zrtp_stream , ( void * ) & other_rtp_session - > rtcp_send_msg , & sbytes ) ;
2012-01-25 05:38:53 +00:00
2012-06-22 23:08:59 +00:00
switch ( stat ) {
case zrtp_status_ok :
break ;
case zrtp_status_drop :
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " Error: zRTP protection drop with code %d \n " , stat ) ;
ret = ( int ) bytes ;
goto end ;
break ;
case zrtp_status_fail :
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " Error: zRTP protection fail with code %d \n " , stat ) ;
break ;
default :
break ;
}
2012-01-25 05:38:53 +00:00
2012-06-22 23:08:59 +00:00
bytes = sbytes ;
}
2010-04-20 21:15:37 +00:00
# endif
2012-06-22 23:08:59 +00:00
if ( switch_socket_sendto ( other_rtp_session - > rtcp_sock_output , other_rtp_session - > rtcp_remote_addr , 0 ,
( const char * ) & other_rtp_session - > rtcp_send_msg , & rtcp_bytes ) ! = SWITCH_STATUS_SUCCESS ) {
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_DEBUG , " RTCP packet not written \n " ) ;
}
2010-04-20 21:15:37 +00:00
2012-06-22 23:08:59 +00:00
}
switch_core_session_rwunlock ( other_session ) ;
2010-04-20 21:15:37 +00:00
}
}
2012-06-22 23:08:59 +00:00
}
2010-04-20 21:15:37 +00:00
}
2010-04-20 00:07:23 +00:00
}
}
2009-03-30 21:12:06 +00:00
2011-02-25 22:15:40 +00:00
2011-02-14 20:07:37 +00:00
if ( bytes & & rtp_session - > recv_msg . header . version = = 2 & &
! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_PROXY_MEDIA ) & & ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_UDPTL ) & &
2011-01-21 21:08:26 +00:00
rtp_session - > recv_msg . header . pt ! = 13 & &
rtp_session - > recv_msg . header . pt ! = rtp_session - > recv_te & &
( ! rtp_session - > cng_pt | | rtp_session - > recv_msg . header . pt ! = rtp_session - > cng_pt ) & &
2011-02-25 22:15:40 +00:00
rtp_session - > recv_msg . header . pt ! = rtp_session - > rpayload ) {
2011-01-21 21:08:26 +00:00
/* drop frames of incorrect payload number and return CNG frame instead */
return_cng_frame ( ) ;
}
2010-08-05 23:43:30 +00:00
if ( ! bytes & & ( io_flags & SWITCH_IO_FLAG_NOBLOCK ) ) {
rtp_session - > missed_count = 0 ;
ret = 0 ;
goto end ;
}
if ( rtp_session - > max_missed_packets & & read_loops = = 1 ) {
2008-11-18 22:36:31 +00:00
if ( bytes ) {
rtp_session - > missed_count = 0 ;
} else if ( + + rtp_session - > missed_count > = rtp_session - > max_missed_packets ) {
ret = - 2 ;
goto end ;
}
}
2008-11-10 18:26:41 +00:00
check = ! bytes ;
2010-02-06 03:38:24 +00:00
2012-03-26 15:53:34 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_FLUSH ) ) {
if ( ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_VIDEO ) ) {
do_flush ( rtp_session ) ;
bytes = 0 ;
}
switch_clear_flag_locked ( rtp_session , SWITCH_RTP_FLAG_FLUSH ) ;
}
2010-02-06 03:38:24 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_BREAK ) | | ( bytes & & bytes = = 4 & & * ( ( int * ) & rtp_session - > recv_msg ) = = UINT_MAX ) ) {
2008-10-09 22:44:48 +00:00
switch_clear_flag_locked ( rtp_session , SWITCH_RTP_FLAG_BREAK ) ;
2012-02-27 19:26:50 +00:00
2012-03-26 15:53:34 +00:00
if ( ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_NOBLOCK ) | | ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_USE_TIMER ) | |
switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_PROXY_MEDIA ) | | switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_UDPTL ) | |
( bytes & & bytes < 5 ) | | ( ! bytes & & poll_loop ) ) {
2012-02-27 19:26:50 +00:00
do_2833 ( rtp_session , session ) ;
bytes = 0 ;
return_cng_frame ( ) ;
}
2008-10-09 22:44:48 +00:00
}
2010-02-06 03:38:24 +00:00
2008-10-21 18:04:20 +00:00
if ( bytes & & bytes < 5 ) {
2008-10-10 15:47:58 +00:00
continue ;
}
2009-03-30 21:12:06 +00:00
if ( ! bytes & & poll_loop ) {
goto recvfrom ;
}
2010-02-03 19:19:10 +00:00
2010-03-27 00:29:54 +00:00
if ( bytes & & rtp_session - > recv_msg . header . m & & rtp_session - > recv_msg . header . pt ! = rtp_session - > recv_te & &
2010-06-07 19:06:41 +00:00
! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_VIDEO ) & & ! ( rtp_session - > rtp_bugs & RTP_BUG_IGNORE_MARK_BIT ) ) {
2009-04-16 22:13:55 +00:00
rtp_flush_read_buffer ( rtp_session , SWITCH_RTP_FLUSH_ONCE ) ;
2008-11-25 17:53:35 +00:00
}
2009-10-29 17:29:05 +00:00
2009-10-29 17:52:52 +00:00
if ( bytes & & switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_DEBUG_RTP_READ ) ) {
2009-10-29 17:29:05 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
if ( ! session ) {
2009-10-29 17:52:52 +00:00
switch_clear_flag ( rtp_session , SWITCH_RTP_FLAG_DEBUG_RTP_READ ) ;
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_CRIT , " RTP HAS NO SESSION! \n " ) ;
2009-10-29 17:29:05 +00:00
} else {
const char * tx_host ;
const char * old_host ;
const char * my_host ;
char bufa [ 30 ] , bufb [ 30 ] , bufc [ 30 ] ;
2010-02-06 03:38:24 +00:00
2009-10-29 17:29:05 +00:00
tx_host = switch_get_addr ( bufa , sizeof ( bufa ) , rtp_session - > from_addr ) ;
old_host = switch_get_addr ( bufb , sizeof ( bufb ) , rtp_session - > remote_addr ) ;
my_host = switch_get_addr ( bufc , sizeof ( bufc ) , rtp_session - > local_addr ) ;
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG_CLEAN ( session ) , SWITCH_LOG_CONSOLE ,
2010-02-06 03:38:24 +00:00
" R %s b=%ld %s:%u %s:%u %s:%u pt=%d ts=%u m=%d \n " ,
2009-10-29 17:29:05 +00:00
switch_channel_get_name ( switch_core_session_get_channel ( session ) ) ,
2010-02-06 03:38:24 +00:00
( long ) bytes ,
2009-10-29 17:29:05 +00:00
my_host , switch_sockaddr_get_port ( rtp_session - > local_addr ) ,
2010-02-06 03:38:24 +00:00
old_host , rtp_session - > remote_port ,
2009-10-29 17:29:05 +00:00
tx_host , switch_sockaddr_get_port ( rtp_session - > from_addr ) ,
rtp_session - > recv_msg . header . pt , ntohl ( rtp_session - > recv_msg . header . ts ) , rtp_session - > recv_msg . header . m ) ;
2010-02-06 03:38:24 +00:00
2009-10-29 17:29:05 +00:00
}
}
2010-02-06 03:38:24 +00:00
2011-09-09 18:56:04 +00:00
if ( ( ( rtp_session - > cng_pt & & rtp_session - > recv_msg . header . pt = = rtp_session - > cng_pt ) | | rtp_session - > recv_msg . header . pt = = 13 ) ) {
* flags | = SFF_NOT_AUDIO ;
}
2009-10-30 14:26:17 +00:00
/* ignore packets not meant for us unless the auto-adjust window is open */
2009-10-31 22:13:15 +00:00
if ( bytes ) {
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_AUTOADJ ) ) {
if ( ( ( rtp_session - > cng_pt & & rtp_session - > recv_msg . header . pt = = rtp_session - > cng_pt ) | | rtp_session - > recv_msg . header . pt = = 13 ) ) {
2010-02-06 03:38:24 +00:00
goto recvfrom ;
}
2011-09-08 13:57:28 +00:00
} else if ( ! ( rtp_session - > rtp_bugs & RTP_BUG_ACCEPT_ANY_PACKETS ) & & ! switch_cmp_addr ( rtp_session - > from_addr , rtp_session - > remote_addr ) ) {
2010-02-06 03:38:24 +00:00
goto recvfrom ;
2009-10-31 22:13:15 +00:00
}
2009-10-27 21:46:05 +00:00
}
2009-10-30 14:26:17 +00:00
if ( bytes & & switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_AUTOADJ ) & & switch_sockaddr_get_port ( rtp_session - > from_addr ) ) {
if ( ! switch_cmp_addr ( rtp_session - > from_addr , rtp_session - > remote_addr ) ) {
if ( + + rtp_session - > autoadj_tally > = 10 ) {
const char * err ;
uint32_t old = rtp_session - > remote_port ;
const char * tx_host ;
const char * old_host ;
char bufa [ 30 ] , bufb [ 30 ] ;
2009-11-24 21:40:28 +00:00
char adj_port [ 5 ] ;
2009-10-30 14:26:17 +00:00
tx_host = switch_get_addr ( bufa , sizeof ( bufa ) , rtp_session - > from_addr ) ;
2010-02-06 03:38:24 +00:00
old_host = switch_get_addr ( bufb , sizeof ( bufb ) , rtp_session - > remote_addr ) ;
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_INFO ,
2009-10-27 21:46:05 +00:00
" Auto Changing port from %s:%u to %s:%u \n " , old_host , old , tx_host ,
switch_sockaddr_get_port ( rtp_session - > from_addr ) ) ;
2009-11-24 21:40:28 +00:00
if ( channel ) {
switch_channel_set_variable ( channel , " remote_media_ip_reported " , switch_channel_get_variable ( channel , " remote_media_ip " ) ) ;
switch_channel_set_variable ( channel , " remote_media_ip " , tx_host ) ;
2010-02-06 03:38:24 +00:00
switch_snprintf ( adj_port , sizeof ( adj_port ) , " %u " , switch_sockaddr_get_port ( rtp_session - > from_addr ) ) ;
2009-11-24 21:40:28 +00:00
switch_channel_set_variable ( channel , " remote_media_port_reported " , switch_channel_get_variable ( channel , " remote_media_port " ) ) ;
switch_channel_set_variable ( channel , " remote_media_port " , adj_port ) ;
switch_channel_set_variable ( channel , " rtp_auto_adjust " , " true " ) ;
}
2010-09-29 19:14:41 +00:00
rtp_session - > auto_adj_used = 1 ;
2010-04-22 15:22:28 +00:00
switch_rtp_set_remote_address ( rtp_session , tx_host , switch_sockaddr_get_port ( rtp_session - > from_addr ) , 0 , SWITCH_FALSE , & err ) ;
2009-10-27 21:46:05 +00:00
switch_clear_flag_locked ( rtp_session , SWITCH_RTP_FLAG_AUTOADJ ) ;
2008-05-23 23:53:10 +00:00
}
2009-10-27 21:46:05 +00:00
} else {
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_DEBUG , " Correct ip/port confirmed. \n " ) ;
2009-10-27 21:46:05 +00:00
switch_clear_flag_locked ( rtp_session , SWITCH_RTP_FLAG_AUTOADJ ) ;
2010-09-29 19:14:41 +00:00
rtp_session - > auto_adj_used = 0 ;
2008-05-23 23:53:10 +00:00
}
}
2010-02-06 03:38:24 +00:00
2008-05-23 23:53:10 +00:00
if ( bytes & & rtp_session - > autoadj_window ) {
if ( - - rtp_session - > autoadj_window = = 0 ) {
switch_clear_flag_locked ( rtp_session , SWITCH_RTP_FLAG_AUTOADJ ) ;
}
}
2010-05-13 02:25:54 +00:00
if ( bytes & & ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_PROXY_MEDIA ) | | switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_UDPTL ) ) ) {
2008-10-22 17:12:04 +00:00
/* Fast PASS! */
* flags | = SFF_PROXY_PACKET ;
2010-05-13 02:25:54 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_UDPTL ) ) {
* flags | = SFF_UDPTL_PACKET ;
}
2010-08-05 22:56:18 +00:00
2012-06-22 23:08:59 +00:00
check_srtp_and_ice ( rtp_session ) ;
2008-10-22 17:12:04 +00:00
ret = ( int ) bytes ;
goto end ;
}
2008-05-23 23:53:10 +00:00
if ( bytes ) {
2008-04-06 23:47:33 +00:00
rtp_session - > missed_count = 0 ;
2008-10-01 18:02:46 +00:00
2010-08-05 23:43:30 +00:00
if ( bytes < rtp_header_len ) {
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_WARNING , " Ignoring invalid RTP packet size of %ld bytes. \n " , ( long ) bytes ) ;
2010-08-05 23:43:30 +00:00
bytes = 0 ;
goto do_continue ;
}
2008-10-01 18:52:00 +00:00
if ( rtp_session - > recv_msg . header . pt & & ( rtp_session - > recv_msg . header . pt = = rtp_session - > cng_pt | | rtp_session - > recv_msg . header . pt = = 13 ) ) {
2008-11-10 18:26:41 +00:00
return_cng_frame ( ) ;
2010-02-06 03:38:24 +00:00
}
2008-04-06 23:47:33 +00:00
}
2010-02-06 03:38:24 +00:00
2012-06-22 23:08:59 +00:00
if ( check_srtp_and_ice ( rtp_session ) ) {
ret = - 1 ;
goto end ;
2008-03-13 01:08:42 +00:00
}
2012-06-22 23:08:59 +00:00
2008-05-27 04:30:03 +00:00
2008-03-19 17:56:27 +00:00
if ( check | | bytes ) {
2010-10-23 02:16:12 +00:00
do_2833 ( rtp_session , session ) ;
2006-09-12 22:23:45 +00:00
}
2010-02-06 03:38:24 +00:00
2008-02-15 20:31:10 +00:00
if ( bytes & & rtp_session - > recv_msg . header . version ! = 2 ) {
uint8_t * data = ( uint8_t * ) rtp_session - > recv_msg . body ;
2010-02-03 19:19:10 +00:00
2008-07-18 16:18:31 +00:00
if ( rtp_session - > recv_msg . header . version = = 0 ) {
2012-06-22 23:08:59 +00:00
if ( rtp_session - > ice . ice_user ) {
handle_ice ( rtp_session , & rtp_session - > ice , ( void * ) & rtp_session - > recv_msg , bytes ) ;
2012-01-23 16:01:24 +00:00
goto recvfrom ;
2010-02-03 19:19:10 +00:00
} else if ( rtp_session - > remote_stun_addr ) {
handle_stun_ping_reply ( rtp_session , ( void * ) & rtp_session - > recv_msg , bytes ) ;
2012-01-23 16:01:24 +00:00
goto recvfrom ;
2008-07-18 16:18:31 +00:00
}
2008-02-15 20:31:10 +00:00
}
2010-02-06 03:38:24 +00:00
2008-02-15 20:31:10 +00:00
if ( rtp_session - > invalid_handler ) {
2008-07-03 18:50:15 +00:00
rtp_session - > invalid_handler ( rtp_session , rtp_session - > sock_input , ( void * ) & rtp_session - > recv_msg , bytes , rtp_session - > from_addr ) ;
2008-02-15 20:31:10 +00:00
}
2008-05-27 04:30:03 +00:00
2008-02-15 20:31:10 +00:00
memset ( data , 0 , 2 ) ;
data [ 0 ] = 65 ;
rtp_session - > recv_msg . header . pt = ( uint32_t ) rtp_session - > cng_pt ? rtp_session - > cng_pt : SWITCH_RTP_CNG_PAYLOAD ;
* flags | = SFF_CNG ;
2008-05-27 04:30:03 +00:00
* payload_type = ( switch_payload_t ) rtp_session - > recv_msg . header . pt ;
2008-02-15 20:31:10 +00:00
ret = 2 + rtp_header_len ;
goto end ;
2012-06-22 23:08:59 +00:00
} else if ( bytes ) {
rtp_session - > stats . inbound . period_packet_count + + ;
2008-02-15 20:31:10 +00:00
}
2011-01-07 23:53:09 +00:00
2011-01-21 21:03:19 +00:00
/* Handle incoming RFC2833 packets */
switch ( handle_rfc2833 ( rtp_session , bytes , & do_cng ) ) {
case RESULT_GOTO_END :
goto end ;
case RESULT_GOTO_RECVFROM :
goto recvfrom ;
case RESULT_GOTO_TIMERCHECK :
goto timer_check ;
case RESULT_CONTINUE :
goto result_continue ;
2009-01-19 21:12:20 +00:00
}
2011-01-21 21:03:19 +00:00
result_continue :
2010-04-20 00:07:23 +00:00
timer_check :
2008-10-01 18:02:46 +00:00
2008-03-07 18:21:08 +00:00
if ( do_cng ) {
2008-10-01 18:02:46 +00:00
uint8_t * data = ( uint8_t * ) rtp_session - > recv_msg . body ;
2010-12-23 19:18:41 +00:00
if ( rtp_session - > last_cng_ts = = rtp_session - > last_read_ts + rtp_session - > samples_per_interval ) {
rtp_session - > last_cng_ts = 0 ;
} else {
rtp_session - > last_cng_ts = rtp_session - > last_read_ts + rtp_session - > samples_per_interval ;
}
2008-10-01 18:02:46 +00:00
memset ( data , 0 , 2 ) ;
data [ 0 ] = 65 ;
rtp_session - > recv_msg . header . pt = ( uint32_t ) rtp_session - > cng_pt ? rtp_session - > cng_pt : SWITCH_RTP_CNG_PAYLOAD ;
2007-03-29 22:31:56 +00:00
* flags | = SFF_CNG ;
2008-10-01 18:02:46 +00:00
* payload_type = ( switch_payload_t ) rtp_session - > recv_msg . header . pt ;
2008-01-18 18:14:53 +00:00
ret = 2 + rtp_header_len ;
2009-04-15 15:08:21 +00:00
rtp_session - > stats . inbound . skip_packet_count + + ;
2008-01-18 18:14:53 +00:00
goto end ;
2006-05-04 01:15:38 +00:00
}
2011-03-09 21:17:09 +00:00
if ( check | | ( bytes & & ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_USE_TIMER ) ) ) {
2009-03-31 19:10:43 +00:00
if ( ! bytes & & switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_USE_TIMER ) ) { /* We're late! We're Late! */
2006-05-05 16:52:19 +00:00
if ( ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_NOBLOCK ) & & status = = SWITCH_STATUS_BREAK ) {
2008-11-14 23:31:21 +00:00
switch_cond_next ( ) ;
2006-05-05 16:52:19 +00:00
continue ;
}
2010-08-05 23:43:30 +00:00
2008-10-01 18:02:46 +00:00
return_cng_frame ( ) ;
2007-04-07 01:57:54 +00:00
}
}
2010-08-05 23:43:30 +00:00
2006-04-10 17:25:59 +00:00
if ( status = = SWITCH_STATUS_BREAK | | bytes = = 0 ) {
2010-08-05 23:43:30 +00:00
if ( ! ( io_flags & SWITCH_IO_FLAG_SINGLE_READ ) & & switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_DATAWAIT ) ) {
2007-06-23 03:26:12 +00:00
goto do_continue ;
2006-09-20 20:25:26 +00:00
}
2010-08-05 23:43:30 +00:00
return_cng_frame ( ) ;
2006-04-10 17:25:59 +00:00
}
2008-05-27 04:30:03 +00:00
2007-04-07 01:57:54 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_GOOGLEHACK ) & & rtp_session - > recv_msg . header . pt = = 102 ) {
rtp_session - > recv_msg . header . pt = 97 ;
2006-04-03 21:00:13 +00:00
}
2006-04-07 17:32:14 +00:00
2006-05-05 16:03:15 +00:00
break ;
2007-06-23 03:26:12 +00:00
2010-04-20 00:07:23 +00:00
do_continue :
2010-02-06 03:38:24 +00:00
2011-03-09 21:17:09 +00:00
if ( ! bytes & & ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_USE_TIMER ) ) {
2008-11-10 18:26:41 +00:00
switch_yield ( sleep_mss ) ;
}
2008-10-01 18:02:46 +00:00
2006-05-04 17:51:53 +00:00
}
2008-02-25 16:35:19 +00:00
if ( switch_rtp_ready ( rtp_session ) ) {
* payload_type = ( switch_payload_t ) rtp_session - > recv_msg . header . pt ;
2006-05-04 17:51:53 +00:00
2008-02-25 16:35:19 +00:00
if ( * payload_type = = SWITCH_RTP_CNG_PAYLOAD ) {
* flags | = SFF_CNG ;
}
2007-03-29 22:31:56 +00:00
2008-02-25 16:35:19 +00:00
ret = ( int ) bytes ;
} else {
ret = - 1 ;
}
2008-01-18 18:14:53 +00:00
2010-04-20 00:07:23 +00:00
end :
2010-08-05 22:56:18 +00:00
2008-01-18 18:14:53 +00:00
READ_DEC ( rtp_session ) ;
return ret ;
2006-04-03 21:00:13 +00:00
}
2010-09-29 19:14:41 +00:00
SWITCH_DECLARE ( switch_byte_t ) switch_rtp_check_auto_adj ( switch_rtp_t * rtp_session )
{
return rtp_session - > auto_adj_used ;
}
2006-05-04 17:51:53 +00:00
SWITCH_DECLARE ( switch_size_t ) switch_rtp_has_dtmf ( switch_rtp_t * rtp_session )
{
2007-03-07 23:24:09 +00:00
switch_size_t has = 0 ;
2006-05-04 17:51:53 +00:00
2007-03-07 23:24:09 +00:00
if ( switch_rtp_ready ( rtp_session ) ) {
switch_mutex_lock ( rtp_session - > dtmf_data . dtmf_mutex ) ;
2007-12-22 00:32:20 +00:00
has = switch_queue_size ( rtp_session - > dtmf_data . dtmf_inqueue ) ;
2007-03-07 23:24:09 +00:00
switch_mutex_unlock ( rtp_session - > dtmf_data . dtmf_mutex ) ;
}
2006-05-04 17:51:53 +00:00
return has ;
}
2007-12-22 00:32:20 +00:00
SWITCH_DECLARE ( switch_size_t ) switch_rtp_dequeue_dtmf ( switch_rtp_t * rtp_session , switch_dtmf_t * dtmf )
{
switch_size_t bytes = 0 ;
switch_dtmf_t * _dtmf = NULL ;
void * pop ;
2008-05-27 04:30:03 +00:00
2007-03-07 23:24:09 +00:00
if ( ! switch_rtp_ready ( rtp_session ) ) {
2007-12-22 00:32:20 +00:00
return bytes ;
2007-03-07 23:24:09 +00:00
}
2006-05-04 17:51:53 +00:00
switch_mutex_lock ( rtp_session - > dtmf_data . dtmf_mutex ) ;
2007-12-22 00:32:20 +00:00
if ( switch_queue_trypop ( rtp_session - > dtmf_data . dtmf_inqueue , & pop ) = = SWITCH_STATUS_SUCCESS ) {
2010-10-23 02:16:12 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
2007-12-22 00:32:20 +00:00
_dtmf = ( switch_dtmf_t * ) pop ;
* dtmf = * _dtmf ;
2010-10-23 02:16:12 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_DEBUG , " RTP RECV DTMF %c:%d \n " , dtmf - > digit , dtmf - > duration ) ;
2007-12-22 00:32:20 +00:00
bytes + + ;
2009-02-27 18:52:15 +00:00
free ( pop ) ;
2006-05-04 17:51:53 +00:00
}
switch_mutex_unlock ( rtp_session - > dtmf_data . dtmf_mutex ) ;
2007-12-22 00:32:20 +00:00
return bytes ;
2006-05-04 17:51:53 +00:00
}
2007-12-22 00:32:20 +00:00
SWITCH_DECLARE ( switch_status_t ) switch_rtp_queue_rfc2833 ( switch_rtp_t * rtp_session , const switch_dtmf_t * dtmf )
2006-05-04 17:51:53 +00:00
{
2007-12-22 00:32:20 +00:00
switch_dtmf_t * rdigit ;
2006-05-04 17:51:53 +00:00
2007-03-07 23:24:09 +00:00
if ( ! switch_rtp_ready ( rtp_session ) ) {
2007-12-22 00:32:20 +00:00
return SWITCH_STATUS_FALSE ;
2007-03-07 23:24:09 +00:00
}
2006-05-04 17:51:53 +00:00
2007-12-22 00:32:20 +00:00
if ( ( rdigit = malloc ( sizeof ( * rdigit ) ) ) ! = 0 ) {
* rdigit = * dtmf ;
2009-09-16 21:24:22 +00:00
if ( rdigit - > duration < switch_core_min_dtmf_duration ( 0 ) ) {
rdigit - > duration = switch_core_min_dtmf_duration ( 0 ) ;
2008-02-21 21:38:49 +00:00
}
2008-05-27 04:30:03 +00:00
2008-01-14 21:27:40 +00:00
if ( ( switch_queue_trypush ( rtp_session - > dtmf_data . dtmf_queue , rdigit ) ) ! = SWITCH_STATUS_SUCCESS ) {
free ( rdigit ) ;
return SWITCH_STATUS_FALSE ;
}
2007-12-22 00:32:20 +00:00
} else {
abort ( ) ;
2006-05-04 17:51:53 +00:00
}
2008-05-27 04:30:03 +00:00
2007-12-22 00:32:20 +00:00
return SWITCH_STATUS_SUCCESS ;
2006-05-04 17:51:53 +00:00
}
2007-12-22 00:32:20 +00:00
SWITCH_DECLARE ( switch_status_t ) switch_rtp_queue_rfc2833_in ( switch_rtp_t * rtp_session , const switch_dtmf_t * dtmf )
2006-05-04 17:51:53 +00:00
{
2007-12-22 00:32:20 +00:00
switch_dtmf_t * rdigit ;
2006-05-04 17:51:53 +00:00
2007-03-07 23:24:09 +00:00
if ( ! switch_rtp_ready ( rtp_session ) ) {
return SWITCH_STATUS_FALSE ;
}
2008-05-27 04:30:03 +00:00
2007-12-22 00:32:20 +00:00
if ( ( rdigit = malloc ( sizeof ( * rdigit ) ) ) ! = 0 ) {
* rdigit = * dtmf ;
2009-09-16 21:24:22 +00:00
if ( rdigit - > duration < switch_core_min_dtmf_duration ( 0 ) ) {
rdigit - > duration = switch_core_min_dtmf_duration ( 0 ) ;
2008-02-21 21:38:49 +00:00
}
2008-01-14 21:27:40 +00:00
if ( ( switch_queue_trypush ( rtp_session - > dtmf_data . dtmf_inqueue , rdigit ) ) ! = SWITCH_STATUS_SUCCESS ) {
free ( rdigit ) ;
return SWITCH_STATUS_FALSE ;
}
2007-12-22 00:32:20 +00:00
} else {
abort ( ) ;
2006-05-04 17:51:53 +00:00
}
2008-05-27 04:30:03 +00:00
2006-05-04 17:51:53 +00:00
return SWITCH_STATUS_SUCCESS ;
}
2008-05-27 04:30:03 +00:00
SWITCH_DECLARE ( switch_status_t ) switch_rtp_read ( switch_rtp_t * rtp_session , void * data , uint32_t * datalen ,
2008-05-08 19:19:47 +00:00
switch_payload_t * payload_type , switch_frame_flag_t * flags , switch_io_flag_t io_flags )
2006-04-03 21:00:13 +00:00
{
2007-03-07 23:24:09 +00:00
int bytes = 0 ;
2006-04-03 21:00:13 +00:00
2007-03-07 23:24:09 +00:00
if ( ! switch_rtp_ready ( rtp_session ) ) {
return SWITCH_STATUS_FALSE ;
}
2008-05-08 19:19:47 +00:00
bytes = rtp_common_read ( rtp_session , payload_type , flags , io_flags ) ;
2007-03-29 22:31:56 +00:00
2006-04-12 20:48:47 +00:00
if ( bytes < 0 ) {
2006-04-10 18:28:46 +00:00
* datalen = 0 ;
2007-10-31 13:08:45 +00:00
return bytes = = - 2 ? SWITCH_STATUS_TIMEOUT : SWITCH_STATUS_GENERR ;
2006-05-04 01:15:38 +00:00
} else if ( bytes = = 0 ) {
* datalen = 0 ;
return SWITCH_STATUS_BREAK ;
2006-04-18 00:24:52 +00:00
} else {
2010-08-05 23:43:30 +00:00
if ( bytes > rtp_header_len ) {
bytes - = rtp_header_len ;
}
2006-04-03 21:00:13 +00:00
}
2006-04-10 18:28:46 +00:00
* datalen = bytes ;
2006-04-07 17:32:14 +00:00
memcpy ( data , rtp_session - > recv_msg . body , bytes ) ;
2006-04-10 18:28:46 +00:00
2007-03-29 22:31:56 +00:00
return SWITCH_STATUS_SUCCESS ;
2006-04-07 17:32:14 +00:00
}
2006-04-03 21:00:13 +00:00
2010-04-20 00:07:23 +00:00
SWITCH_DECLARE ( switch_status_t ) switch_rtcp_zerocopy_read_frame ( switch_rtp_t * rtp_session , switch_rtcp_frame_t * frame )
{
if ( ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_ENABLE_RTCP ) ) {
return SWITCH_STATUS_FALSE ;
}
/* A fresh frame has been found! */
if ( rtp_session - > rtcp_fresh_frame ) {
struct switch_rtcp_senderinfo * sr = ( struct switch_rtcp_senderinfo * ) rtp_session - > rtcp_recv_msg . body ;
2011-12-16 00:13:09 +00:00
/* we remove the header lenght because with directly have a pointer on the body */
unsigned packet_length = ( ntohs ( ( uint16_t ) rtp_session - > rtcp_recv_msg . header . length ) + 1 ) * 4 - sizeof ( switch_rtcp_hdr_t ) ;
unsigned int reportsOffset = sizeof ( struct switch_rtcp_senderinfo ) ;
int i = 0 ;
2011-12-16 12:35:05 +00:00
unsigned int offset ;
2011-12-16 00:13:09 +00:00
2010-04-20 00:07:23 +00:00
/* turn the flag off! */
rtp_session - > rtcp_fresh_frame = 0 ;
frame - > ssrc = ntohl ( sr - > ssrc ) ;
2010-04-26 15:55:46 +00:00
frame - > packet_type = ( uint16_t ) rtp_session - > rtcp_recv_msg . header . type ;
2010-04-20 00:07:23 +00:00
frame - > ntp_msw = ntohl ( sr - > ntp_msw ) ;
frame - > ntp_lsw = ntohl ( sr - > ntp_lsw ) ;
frame - > timestamp = ntohl ( sr - > ts ) ;
frame - > packet_count = ntohl ( sr - > pc ) ;
frame - > octect_count = ntohl ( sr - > oc ) ;
2011-12-16 12:35:05 +00:00
for ( offset = reportsOffset ; offset < packet_length ; offset + = sizeof ( struct switch_rtcp_report_block ) ) {
2011-12-16 00:13:09 +00:00
struct switch_rtcp_report_block * report = ( struct switch_rtcp_report_block * ) ( rtp_session - > rtcp_recv_msg . body + offset ) ;
frame - > reports [ i ] . ssrc = ntohl ( report - > ssrc ) ;
2011-12-16 12:35:05 +00:00
frame - > reports [ i ] . fraction = ( uint8_t ) ntohl ( report - > fraction ) ;
2011-12-16 00:13:09 +00:00
frame - > reports [ i ] . lost = ntohl ( report - > lost ) ;
frame - > reports [ i ] . highest_sequence_number_received = ntohl ( report - > highest_sequence_number_received ) ;
frame - > reports [ i ] . jitter = ntohl ( report - > jitter ) ;
frame - > reports [ i ] . lsr = ntohl ( report - > lsr ) ;
frame - > reports [ i ] . dlsr = ntohl ( report - > dlsr ) ;
i + + ;
if ( i > = MAX_REPORT_BLOCKS ) {
break ;
}
}
2011-12-16 12:35:05 +00:00
frame - > report_count = ( uint16_t ) i ;
2011-12-16 00:13:09 +00:00
2010-04-20 00:07:23 +00:00
return SWITCH_STATUS_SUCCESS ;
}
return SWITCH_STATUS_TIMEOUT ;
}
2008-05-08 19:19:47 +00:00
SWITCH_DECLARE ( switch_status_t ) switch_rtp_zerocopy_read_frame ( switch_rtp_t * rtp_session , switch_frame_t * frame , switch_io_flag_t io_flags )
2006-04-18 00:24:52 +00:00
{
2007-03-07 23:24:09 +00:00
int bytes = 0 ;
if ( ! switch_rtp_ready ( rtp_session ) ) {
return SWITCH_STATUS_FALSE ;
}
2008-05-08 19:19:47 +00:00
bytes = rtp_common_read ( rtp_session , & frame - > payload , & frame - > flags , io_flags ) ;
2008-02-13 00:32:09 +00:00
2006-04-18 00:24:52 +00:00
frame - > data = rtp_session - > recv_msg . body ;
frame - > packet = & rtp_session - > recv_msg ;
frame - > packetlen = bytes ;
frame - > source = __FILE__ ;
2010-12-10 23:47:24 +00:00
2008-11-25 18:21:44 +00:00
switch_set_flag ( frame , SFF_RAW_RTP ) ;
2010-03-16 22:49:54 +00:00
if ( frame - > payload = = rtp_session - > recv_te ) {
2008-11-25 18:21:44 +00:00
switch_set_flag ( frame , SFF_RFC2833 ) ;
2007-10-23 21:32:17 +00:00
}
2007-03-29 22:31:56 +00:00
frame - > timestamp = ntohl ( rtp_session - > recv_msg . header . ts ) ;
2009-05-15 17:55:53 +00:00
frame - > seq = ( uint16_t ) ntohs ( ( uint16_t ) rtp_session - > recv_msg . header . seq ) ;
2007-04-19 21:40:50 +00:00
frame - > ssrc = ntohl ( rtp_session - > recv_msg . header . ssrc ) ;
frame - > m = rtp_session - > recv_msg . header . m ? SWITCH_TRUE : SWITCH_FALSE ;
2006-04-18 00:24:52 +00:00
2009-05-23 01:50:56 +00:00
# ifdef ENABLE_ZRTP
2009-05-27 01:40:11 +00:00
if ( zrtp_on & & switch_test_flag ( rtp_session , SWITCH_ZRTP_FLAG_SECURE_MITM_RECV ) ) {
zrtp_session_info_t zrtp_session_info ;
2010-02-06 03:38:24 +00:00
if ( rtp_session - > zrtp_session & & ( zrtp_status_ok = = zrtp_session_get ( rtp_session - > zrtp_session , & zrtp_session_info ) ) ) {
if ( zrtp_session_info . sas_is_ready ) {
2010-01-07 23:29:06 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
switch_channel_t * channel = switch_core_session_get_channel ( session ) ;
2012-05-29 18:10:15 +00:00
const char * uuid = switch_channel_get_partner_uuid ( channel ) ;
2010-02-06 03:38:24 +00:00
if ( uuid ) {
2010-01-07 23:29:06 +00:00
switch_core_session_t * other_session ;
if ( ( other_session = switch_core_session_locate ( uuid ) ) ) {
switch_channel_t * other_channel = switch_core_session_get_channel ( other_session ) ;
2010-02-03 19:19:10 +00:00
switch_rtp_t * other_rtp_session = switch_channel_get_private ( other_channel , " __zrtp_audio_rtp_session " ) ;
2010-01-07 23:29:06 +00:00
if ( other_rtp_session ) {
2011-01-11 21:31:33 +00:00
if ( switch_channel_direction ( channel ) = = SWITCH_CALL_DIRECTION_INBOUND ) {
switch_mutex_lock ( other_rtp_session - > read_mutex ) ;
if ( zrtp_status_ok = = zrtp_session_get ( other_rtp_session - > zrtp_session , & zrtp_session_info ) ) {
if ( rtp_session - > zrtp_mitm_tries > ZRTP_MITM_TRIES ) {
switch_clear_flag ( other_rtp_session , SWITCH_ZRTP_FLAG_SECURE_MITM_RECV ) ;
switch_clear_flag ( other_rtp_session , SWITCH_ZRTP_FLAG_SECURE_MITM_SEND ) ;
switch_clear_flag ( rtp_session , SWITCH_ZRTP_FLAG_SECURE_MITM_RECV ) ;
switch_clear_flag ( rtp_session , SWITCH_ZRTP_FLAG_SECURE_MITM_SEND ) ;
} else if ( zrtp_status_ok = = zrtp_resolve_mitm_call ( other_rtp_session - > zrtp_stream , rtp_session - > zrtp_stream ) ) {
switch_clear_flag ( rtp_session , SWITCH_ZRTP_FLAG_SECURE_MITM_RECV ) ;
switch_clear_flag ( rtp_session , SWITCH_ZRTP_FLAG_SECURE_MITM_SEND ) ;
switch_clear_flag ( other_rtp_session , SWITCH_ZRTP_FLAG_SECURE_MITM_RECV ) ;
switch_clear_flag ( other_rtp_session , SWITCH_ZRTP_FLAG_SECURE_MITM_SEND ) ;
zrtp_verified_set ( zrtp_global , & rtp_session - > zrtp_session - > zid ,
2012-05-17 05:33:44 +00:00
& rtp_session - > zrtp_session - > peer_zid , ( uint8_t ) ( zrtp_session_info . sas_is_verified ^ 1 ) ) ;
2011-01-11 21:31:33 +00:00
rtp_session - > zrtp_mitm_tries + + ;
}
2010-01-07 23:29:06 +00:00
}
2011-01-11 21:31:33 +00:00
switch_mutex_unlock ( other_rtp_session - > read_mutex ) ;
2010-01-07 23:29:06 +00:00
}
}
2010-02-06 03:38:24 +00:00
2010-01-07 23:29:06 +00:00
switch_core_session_rwunlock ( other_session ) ;
}
2009-05-27 01:40:11 +00:00
}
}
2010-02-03 19:19:10 +00:00
} else {
switch_clear_flag ( rtp_session , SWITCH_ZRTP_FLAG_SECURE_MITM_RECV ) ;
switch_clear_flag ( rtp_session , SWITCH_ZRTP_FLAG_SECURE_MITM_SEND ) ;
2009-05-23 01:50:56 +00:00
}
}
# endif
2006-04-18 00:24:52 +00:00
if ( bytes < 0 ) {
frame - > datalen = 0 ;
2007-10-31 13:08:45 +00:00
return bytes = = - 2 ? SWITCH_STATUS_TIMEOUT : SWITCH_STATUS_GENERR ;
2010-08-05 23:43:30 +00:00
} else if ( bytes < rtp_header_len ) {
2006-04-18 16:20:47 +00:00
frame - > datalen = 0 ;
return SWITCH_STATUS_BREAK ;
2006-04-18 00:24:52 +00:00
} else {
bytes - = rtp_header_len ;
}
2008-05-27 04:30:03 +00:00
2006-04-18 00:24:52 +00:00
frame - > datalen = bytes ;
return SWITCH_STATUS_SUCCESS ;
}
2006-05-04 17:51:53 +00:00
SWITCH_DECLARE ( switch_status_t ) switch_rtp_zerocopy_read ( switch_rtp_t * rtp_session ,
2008-05-27 04:30:03 +00:00
void * * data , uint32_t * datalen , switch_payload_t * payload_type , switch_frame_flag_t * flags ,
switch_io_flag_t io_flags )
2006-04-07 17:32:14 +00:00
{
2007-03-07 23:24:09 +00:00
int bytes = 0 ;
if ( ! switch_rtp_ready ( rtp_session ) ) {
return SWITCH_STATUS_FALSE ;
}
2008-05-08 19:19:47 +00:00
bytes = rtp_common_read ( rtp_session , payload_type , flags , io_flags ) ;
2006-04-10 18:09:02 +00:00
* data = rtp_session - > recv_msg . body ;
2006-04-12 20:48:47 +00:00
if ( bytes < 0 ) {
2006-04-10 18:28:46 +00:00
* datalen = 0 ;
return SWITCH_STATUS_GENERR ;
2006-04-18 00:24:52 +00:00
} else {
2010-08-05 23:43:30 +00:00
if ( bytes > rtp_header_len ) {
bytes - = rtp_header_len ;
}
2006-04-03 21:00:13 +00:00
}
2006-04-10 18:09:02 +00:00
2006-04-10 18:28:46 +00:00
* datalen = bytes ;
return SWITCH_STATUS_SUCCESS ;
2006-04-07 17:32:14 +00:00
}
2006-04-04 17:22:06 +00:00
2008-05-27 04:30:03 +00:00
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 )
2006-04-07 17:32:14 +00:00
{
2012-06-22 23:08:59 +00:00
switch_size_t bytes ;
2006-04-21 22:31:08 +00:00
uint8_t send = 1 ;
2008-01-11 04:23:09 +00:00
uint32_t this_ts = 0 ;
2008-01-18 18:14:53 +00:00
int ret ;
2010-04-20 00:07:23 +00:00
switch_time_t now ;
2006-04-10 16:56:11 +00:00
2007-03-07 23:24:09 +00:00
if ( ! switch_rtp_ready ( rtp_session ) ) {
return SWITCH_STATUS_FALSE ;
}
2008-01-18 18:14:53 +00:00
WRITE_INC ( rtp_session ) ;
2008-01-11 04:23:09 +00:00
if ( send_msg ) {
2006-04-18 00:24:52 +00:00
bytes = datalen ;
2007-12-17 23:19:10 +00:00
if ( flags & & * flags & SFF_RFC2833 ) {
2007-10-23 21:32:17 +00:00
send_msg - > header . pt = rtp_session - > te ;
}
2008-05-27 04:30:03 +00:00
data = send_msg - > body ;
2010-08-05 23:43:30 +00:00
if ( datalen > rtp_header_len ) {
datalen - = rtp_header_len ;
}
2006-04-18 00:24:52 +00:00
} else {
2007-03-17 01:52:05 +00:00
uint8_t m = 0 ;
2008-05-27 04:30:03 +00:00
2007-10-23 21:32:17 +00:00
if ( * flags & SFF_RFC2833 ) {
payload = rtp_session - > te ;
}
2007-06-11 22:12:10 +00:00
2008-01-11 04:23:09 +00:00
send_msg = & rtp_session - > send_msg ;
send_msg - > header . pt = payload ;
2010-11-10 22:55:56 +00:00
if ( rtp_session - > rtp_bugs & RTP_BUG_SEND_LINEAR_TIMESTAMPS ) {
rtp_session - > ts + = rtp_session - > samples_per_interval ;
if ( rtp_session - > ts < = rtp_session - > last_write_ts & & rtp_session - > ts > 0 ) {
rtp_session - > ts = rtp_session - > last_write_ts + rtp_session - > samples_per_interval ;
}
} else if ( timestamp ) {
2008-01-11 04:23:09 +00:00
rtp_session - > ts = ( uint32_t ) timestamp ;
2010-08-05 23:43:30 +00:00
/* Send marker bit if timestamp is lower/same as before (resetted/new timer) */
2010-11-10 22:55:56 +00:00
if ( rtp_session - > ts < = rtp_session - > last_write_ts & & ! ( rtp_session - > rtp_bugs & RTP_BUG_NEVER_SEND_MARKER ) ) {
2010-08-05 23:43:30 +00:00
m + + ;
}
2011-03-09 21:17:09 +00:00
} else if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_USE_TIMER ) ) {
2008-01-11 04:23:09 +00:00
rtp_session - > ts = rtp_session - > timer . samplecount ;
2010-02-06 03:38:24 +00:00
2010-08-05 23:43:30 +00:00
if ( rtp_session - > ts < = rtp_session - > last_write_ts & & rtp_session - > ts > 0 ) {
2008-01-23 16:23:01 +00:00
rtp_session - > ts = rtp_session - > last_write_ts + rtp_session - > samples_per_interval ;
}
2008-01-11 04:23:09 +00:00
} else {
rtp_session - > ts + = rtp_session - > samples_per_interval ;
}
2008-05-27 04:30:03 +00:00
2008-01-11 04:23:09 +00:00
rtp_session - > send_msg . header . ts = htonl ( rtp_session - > ts ) ;
2008-12-09 00:32:51 +00:00
2011-10-05 01:17:05 +00:00
if ( ( rtp_session - > last_write_ts ! = RTP_TS_RESET & & rtp_session - > ts > ( rtp_session - > last_write_ts + ( rtp_session - > samples_per_interval * 10 ) ) )
2007-06-11 22:12:10 +00:00
| | rtp_session - > ts = = rtp_session - > samples_per_interval ) {
2007-03-17 01:52:05 +00:00
m + + ;
}
2007-06-11 22:12:10 +00:00
2011-03-09 21:17:09 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_USE_TIMER ) & &
( rtp_session - > timer . samplecount - rtp_session - > last_write_samplecount ) > rtp_session - > samples_per_interval * 10 ) {
2008-12-09 00:32:51 +00:00
m + + ;
}
2011-03-09 21:17:09 +00:00
if ( ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_USE_TIMER ) & &
2010-02-06 03:38:24 +00:00
( ( unsigned ) ( ( switch_micro_time_now ( ) - rtp_session - > last_write_timestamp ) ) ) > ( rtp_session - > ms_per_packet * 10 ) ) {
2008-12-09 00:32:51 +00:00
m + + ;
}
2007-03-17 01:52:05 +00:00
if ( rtp_session - > cn & & payload ! = rtp_session - > cng_pt ) {
rtp_session - > cn = 0 ;
2007-06-11 22:12:10 +00:00
m + + ;
2007-03-17 01:52:05 +00:00
}
2010-11-10 22:55:56 +00:00
2011-08-30 20:00:07 +00:00
if ( rtp_session - > need_mark & & ! rtp_session - > sending_dtmf ) {
m + + ;
rtp_session - > need_mark = 0 ;
}
2010-11-10 22:55:56 +00:00
send_msg - > header . m = ( m & & ! ( rtp_session - > rtp_bugs & RTP_BUG_NEVER_SEND_MARKER ) ) ? 1 : 0 ;
2008-01-11 16:01:07 +00:00
2007-03-29 22:31:56 +00:00
memcpy ( send_msg - > body , data , datalen ) ;
bytes = datalen + rtp_header_len ;
2006-04-03 21:00:13 +00:00
}
2006-04-18 00:24:52 +00:00
2008-01-16 06:01:53 +00:00
send_msg - > header . ssrc = htonl ( rtp_session - > ssrc ) ;
2006-04-03 21:00:13 +00:00
2006-04-19 20:38:02 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_GOOGLEHACK ) & & rtp_session - > send_msg . header . pt = = 97 ) {
rtp_session - > recv_msg . header . pt = 102 ;
}
2006-12-13 22:58:32 +00:00
2007-03-29 22:31:56 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_VAD ) & &
2007-04-07 01:57:54 +00:00
rtp_session - > recv_msg . header . pt = = rtp_session - > vad_data . read_codec - > implementation - > ianacode ) {
2010-11-19 00:24:32 +00:00
2008-05-27 04:30:03 +00:00
int16_t decoded [ SWITCH_RECOMMENDED_BUFFER_SIZE / sizeof ( int16_t ) ] = { 0 } ;
2007-04-07 01:57:54 +00:00
uint32_t rate = 0 ;
2007-12-17 23:19:10 +00:00
uint32_t codec_flags = 0 ;
2006-04-21 22:31:08 +00:00
uint32_t len = sizeof ( decoded ) ;
2009-01-25 21:23:07 +00:00
time_t now = switch_epoch_time_now ( NULL ) ;
2006-04-24 14:53:54 +00:00
send = 0 ;
2006-12-13 22:58:32 +00:00
2006-04-21 22:31:08 +00:00
if ( rtp_session - > vad_data . scan_freq & & rtp_session - > vad_data . next_scan < = now ) {
rtp_session - > vad_data . bg_count = rtp_session - > vad_data . bg_level = 0 ;
rtp_session - > vad_data . next_scan = now + rtp_session - > vad_data . scan_freq ;
}
if ( switch_core_codec_decode ( & rtp_session - > vad_data . vad_codec ,
rtp_session - > vad_data . read_codec ,
data ,
datalen ,
2007-10-19 15:31:02 +00:00
rtp_session - > vad_data . read_codec - > implementation - > actual_samples_per_second ,
2007-12-17 23:19:10 +00:00
decoded , & len , & rate , & codec_flags ) = = SWITCH_STATUS_SUCCESS ) {
2006-04-21 22:31:08 +00:00
2006-04-24 14:53:54 +00:00
uint32_t energy = 0 ;
2006-04-21 22:31:08 +00:00
uint32_t x , y = 0 , z = len / sizeof ( int16_t ) ;
uint32_t score = 0 ;
2007-11-24 21:48:25 +00:00
int divisor = 0 ;
2006-04-21 22:31:08 +00:00
if ( z ) {
2008-05-27 04:30:03 +00:00
2007-11-24 21:48:25 +00:00
if ( ! ( divisor = rtp_session - > vad_data . read_codec - > implementation - > actual_samples_per_second / 8000 ) ) {
divisor = 1 ;
}
2006-04-21 22:31:08 +00:00
for ( x = 0 ; x < z ; x + + ) {
2006-04-24 14:53:54 +00:00
energy + = abs ( decoded [ y ] ) ;
2006-04-21 22:31:08 +00:00
y + = rtp_session - > vad_data . read_codec - > implementation - > number_of_channels ;
}
2008-05-27 04:30:03 +00:00
2006-04-21 22:31:08 +00:00
if ( + + rtp_session - > vad_data . start_count < rtp_session - > vad_data . start ) {
send = 1 ;
} else {
2007-11-24 21:48:25 +00:00
score = ( energy / ( z / divisor ) ) ;
2006-04-21 22:31:08 +00:00
if ( score & & ( rtp_session - > vad_data . bg_count < rtp_session - > vad_data . bg_len ) ) {
rtp_session - > vad_data . bg_level + = score ;
if ( + + rtp_session - > vad_data . bg_count = = rtp_session - > vad_data . bg_len ) {
rtp_session - > vad_data . bg_level / = rtp_session - > vad_data . bg_len ;
}
send = 1 ;
} else {
2007-06-09 19:01:43 +00:00
if ( score > rtp_session - > vad_data . bg_level & & ! switch_test_flag ( & rtp_session - > vad_data , SWITCH_VAD_FLAG_TALKING ) ) {
2006-04-21 22:31:08 +00:00
uint32_t diff = score - rtp_session - > vad_data . bg_level ;
if ( rtp_session - > vad_data . hangover_hits ) {
rtp_session - > vad_data . hangover_hits - - ;
}
2007-03-30 00:13:31 +00:00
if ( diff > = rtp_session - > vad_data . diff_level | | + + rtp_session - > vad_data . hangunder_hits > = rtp_session - > vad_data . hangunder ) {
2007-06-09 19:01:43 +00:00
2006-04-21 22:31:08 +00:00
switch_set_flag ( & rtp_session - > vad_data , SWITCH_VAD_FLAG_TALKING ) ;
2010-11-10 22:55:56 +00:00
if ( ! ( rtp_session - > rtp_bugs & RTP_BUG_NEVER_SEND_MARKER ) ) {
send_msg - > header . m = 1 ;
}
2007-03-30 00:13:31 +00:00
rtp_session - > vad_data . hangover_hits = rtp_session - > vad_data . hangunder_hits = rtp_session - > vad_data . cng_count = 0 ;
2006-04-21 22:31:08 +00:00
if ( switch_test_flag ( & rtp_session - > vad_data , SWITCH_VAD_FLAG_EVENTS_TALK ) ) {
2011-12-16 15:16:37 +00:00
if ( ( rtp_session - > vad_data . fire_events & VAD_FIRE_TALK ) ) {
2011-12-15 19:44:25 +00:00
switch_event_t * event ;
if ( switch_event_create ( & event , SWITCH_EVENT_TALK ) = = SWITCH_STATUS_SUCCESS ) {
switch_channel_event_set_data ( switch_core_session_get_channel ( rtp_session - > vad_data . session ) , event ) ;
switch_event_fire ( & event ) ;
}
2006-04-21 22:31:08 +00:00
}
}
2007-03-29 22:31:56 +00:00
}
2006-04-21 22:31:08 +00:00
} else {
if ( rtp_session - > vad_data . hangunder_hits ) {
rtp_session - > vad_data . hangunder_hits - - ;
}
if ( switch_test_flag ( & rtp_session - > vad_data , SWITCH_VAD_FLAG_TALKING ) ) {
if ( + + rtp_session - > vad_data . hangover_hits > = rtp_session - > vad_data . hangover ) {
switch_clear_flag ( & rtp_session - > vad_data , SWITCH_VAD_FLAG_TALKING ) ;
2007-03-30 00:13:31 +00:00
rtp_session - > vad_data . hangover_hits = rtp_session - > vad_data . hangunder_hits = rtp_session - > vad_data . cng_count = 0 ;
2006-04-21 22:31:08 +00:00
if ( switch_test_flag ( & rtp_session - > vad_data , SWITCH_VAD_FLAG_EVENTS_NOTALK ) ) {
2011-12-16 15:16:37 +00:00
if ( ( rtp_session - > vad_data . fire_events & VAD_FIRE_NOT_TALK ) ) {
2011-12-15 19:44:25 +00:00
switch_event_t * event ;
if ( switch_event_create ( & event , SWITCH_EVENT_NOTALK ) = = SWITCH_STATUS_SUCCESS ) {
switch_channel_event_set_data ( switch_core_session_get_channel ( rtp_session - > vad_data . session ) , event ) ;
switch_event_fire ( & event ) ;
}
2007-03-29 22:31:56 +00:00
}
2006-04-21 22:31:08 +00:00
}
}
}
}
}
}
2007-03-29 22:31:56 +00:00
2006-04-21 22:31:08 +00:00
if ( switch_test_flag ( & rtp_session - > vad_data , SWITCH_VAD_FLAG_TALKING ) ) {
send = 1 ;
2008-05-27 04:30:03 +00:00
}
2006-04-21 22:31:08 +00:00
}
} else {
2008-01-18 18:14:53 +00:00
ret = - 1 ;
goto end ;
2006-04-21 22:31:08 +00:00
}
}
2008-01-11 05:42:50 +00:00
this_ts = ntohl ( send_msg - > header . ts ) ;
2011-09-06 16:30:28 +00:00
if ( ( this_ts < rtp_session - > last_write_ts ) & & ( ( rtp_session - > last_write_ts - this_ts ) > 16000 ) ) {
2011-09-09 17:02:15 +00:00
rtp_session - > last_write_ts = RTP_TS_RESET ;
2011-09-06 16:30:28 +00:00
}
2011-09-09 17:02:15 +00:00
if ( ! switch_rtp_ready ( rtp_session ) | | rtp_session - > sending_dtmf | | ! this_ts | |
( rtp_session - > last_write_ts > RTP_TS_RESET & & this_ts < rtp_session - > last_write_ts ) ) {
2008-01-11 04:23:09 +00:00
send = 0 ;
}
2007-04-07 01:57:54 +00:00
2011-08-30 20:00:07 +00:00
2008-01-11 04:23:09 +00:00
if ( send ) {
2008-01-11 16:01:07 +00:00
send_msg - > header . seq = htons ( + + rtp_session - > seq ) ;
2008-05-27 04:30:03 +00:00
2011-03-21 19:31:10 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_BYTESWAP ) & & send_msg - > header . pt = = rtp_session - > payload ) {
switch_swap_linear ( ( int16_t * ) send_msg - > body , ( int ) datalen ) ;
}
2011-05-02 16:33:57 +00:00
# ifdef ENABLE_SRTP
2008-01-17 04:16:04 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_SECURE_SEND ) ) {
2008-01-16 06:01:53 +00:00
int sbytes = ( int ) bytes ;
err_status_t stat ;
2008-05-27 04:30:03 +00:00
2008-01-17 04:16:04 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_SECURE_SEND_RESET ) ) {
2012-04-10 21:17:16 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
2008-01-17 04:16:04 +00:00
switch_clear_flag_locked ( rtp_session , SWITCH_RTP_FLAG_SECURE_SEND_RESET ) ;
srtp_dealloc ( rtp_session - > send_ctx ) ;
rtp_session - > send_ctx = NULL ;
if ( ( stat = srtp_create ( & rtp_session - > send_ctx , & rtp_session - > send_policy ) ) ) {
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " Error! RE-Activating Secure RTP SEND \n " ) ;
2008-01-18 18:14:53 +00:00
ret = - 1 ;
goto end ;
2008-01-17 04:16:04 +00:00
} else {
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_INFO , " RE-Activating Secure RTP SEND \n " ) ;
2008-01-17 04:16:04 +00:00
}
}
2008-05-27 04:30:03 +00:00
2008-01-17 04:16:04 +00:00
2008-01-16 06:01:53 +00:00
stat = srtp_protect ( rtp_session - > send_ctx , & send_msg - > header , & sbytes ) ;
if ( stat ) {
2012-04-10 21:17:16 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " Error: SRTP protection failed with code %d \n " , stat ) ;
2008-01-16 06:01:53 +00:00
}
2008-05-27 04:30:03 +00:00
2008-01-16 06:01:53 +00:00
bytes = sbytes ;
}
2011-05-02 16:33:57 +00:00
# endif
2009-05-20 23:04:05 +00:00
# ifdef ENABLE_ZRTP
/* ZRTP Send */
2012-01-25 05:38:53 +00:00
if ( zrtp_on & & ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_PROXY_MEDIA ) ) {
2009-05-20 23:04:05 +00:00
unsigned int sbytes = ( int ) bytes ;
zrtp_status_t stat = zrtp_status_fail ;
2012-04-10 21:17:16 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
2010-02-06 03:38:24 +00:00
stat = zrtp_process_rtp ( rtp_session - > zrtp_stream , ( void * ) send_msg , & sbytes ) ;
2009-05-20 23:04:05 +00:00
switch ( stat ) {
case zrtp_status_ok :
break ;
case zrtp_status_drop :
2012-05-24 03:52:51 +00:00
/* switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Error: zRTP protection drop with code %d\n", stat); */
2010-02-03 19:19:10 +00:00
ret = ( int ) bytes ;
goto end ;
2009-05-21 22:37:15 +00:00
break ;
2009-05-20 23:04:05 +00:00
case zrtp_status_fail :
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " Error: zRTP protection fail with code %d \n " , stat ) ;
2009-05-21 22:37:15 +00:00
break ;
2009-05-20 23:04:05 +00:00
default :
break ;
}
2010-02-06 03:38:24 +00:00
2009-05-20 23:04:05 +00:00
bytes = sbytes ;
}
# endif
2009-04-26 22:10:28 +00:00
2010-04-20 00:07:23 +00:00
now = switch_time_now ( ) ;
2009-04-26 22:10:05 +00:00
# ifdef RTP_DEBUG_WRITE_DELTA
{
int delta = ( int ) ( now - rtp_session - > send_time ) / 1000 ;
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " WRITE %d delta %d \n " , ( int ) bytes , delta ) ;
2009-04-26 22:10:05 +00:00
}
# endif
2010-04-20 00:07:23 +00:00
rtp_session - > send_time = now ;
2009-04-26 22:10:05 +00:00
2009-10-29 17:52:52 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_DEBUG_RTP_WRITE ) ) {
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
if ( ! session ) {
switch_clear_flag ( rtp_session , SWITCH_RTP_FLAG_DEBUG_RTP_WRITE ) ;
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_CRIT , " RTP HAS NO SESSION! \n " ) ;
2009-10-29 17:52:52 +00:00
} else {
const char * tx_host ;
const char * old_host ;
const char * my_host ;
char bufa [ 30 ] , bufb [ 30 ] , bufc [ 30 ] ;
2010-02-06 03:38:24 +00:00
2009-10-29 17:52:52 +00:00
tx_host = switch_get_addr ( bufa , sizeof ( bufa ) , rtp_session - > from_addr ) ;
old_host = switch_get_addr ( bufb , sizeof ( bufb ) , rtp_session - > remote_addr ) ;
my_host = switch_get_addr ( bufc , sizeof ( bufc ) , rtp_session - > local_addr ) ;
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG_CLEAN ( session ) , SWITCH_LOG_CONSOLE ,
2010-02-06 03:38:24 +00:00
" W %s b=%ld %s:%u %s:%u %s:%u pt=%d ts=%u m=%d \n " ,
2009-10-29 17:52:52 +00:00
switch_channel_get_name ( switch_core_session_get_channel ( session ) ) ,
2010-02-06 03:38:24 +00:00
( long ) bytes ,
2009-10-29 17:52:52 +00:00
my_host , switch_sockaddr_get_port ( rtp_session - > local_addr ) ,
2010-02-06 03:38:24 +00:00
old_host , rtp_session - > remote_port ,
2009-10-29 17:52:52 +00:00
tx_host , switch_sockaddr_get_port ( rtp_session - > from_addr ) ,
send_msg - > header . pt , ntohl ( send_msg - > header . ts ) , send_msg - > header . m ) ;
2010-02-06 03:38:24 +00:00
2009-10-29 17:52:52 +00:00
}
}
2008-07-03 18:50:15 +00:00
if ( switch_socket_sendto ( rtp_session - > sock_output , rtp_session - > remote_addr , 0 , ( void * ) send_msg , & bytes ) ! = SWITCH_STATUS_SUCCESS ) {
2008-01-11 04:23:09 +00:00
rtp_session - > seq - - ;
2008-01-18 18:14:53 +00:00
ret = - 1 ;
goto end ;
2008-01-11 01:27:24 +00:00
}
2011-08-31 17:03:57 +00:00
rtp_session - > last_write_ts = this_ts ;
2008-05-27 04:30:03 +00:00
2011-09-01 00:30:51 +00:00
if ( rtp_session - > queue_delay ) {
2011-11-14 18:37:45 +00:00
rtp_session - > delay_samples = rtp_session - > queue_delay ;
2011-09-01 00:30:51 +00:00
rtp_session - > queue_delay = 0 ;
}
2009-04-15 15:08:21 +00:00
rtp_session - > stats . outbound . raw_bytes + = bytes ;
rtp_session - > stats . outbound . packet_count + + ;
2009-04-03 21:48:52 +00:00
2009-11-17 15:26:09 +00:00
if ( rtp_session - > cng_pt & & send_msg - > header . pt = = rtp_session - > cng_pt ) {
2009-04-15 15:08:21 +00:00
rtp_session - > stats . outbound . cng_packet_count + + ;
2009-04-03 21:48:52 +00:00
} else {
2009-04-15 15:08:21 +00:00
rtp_session - > stats . outbound . media_packet_count + + ;
rtp_session - > stats . outbound . media_bytes + = bytes ;
2009-04-03 21:48:52 +00:00
}
2011-03-09 21:17:09 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_USE_TIMER ) ) {
2007-03-17 01:52:05 +00:00
rtp_session - > last_write_samplecount = rtp_session - > timer . samplecount ;
2008-12-09 00:32:51 +00:00
} else {
2011-05-12 15:32:39 +00:00
rtp_session - > last_write_timestamp = switch_micro_time_now ( ) ;
2007-03-17 01:52:05 +00:00
}
2010-04-20 00:07:23 +00:00
2006-04-21 22:31:08 +00:00
}
2012-06-22 23:08:59 +00:00
#if 0
if ( rtp_session - > ice . ice_user ) {
if ( ice_out ( rtp_session , & rtp_session - > ice ) ! = SWITCH_STATUS_SUCCESS ) {
2008-01-18 18:14:53 +00:00
ret = - 1 ;
goto end ;
2006-04-07 17:32:14 +00:00
}
2006-04-06 19:50:53 +00:00
}
2012-06-22 23:08:59 +00:00
# endif
2006-04-06 19:50:53 +00:00
2008-01-18 18:14:53 +00:00
ret = ( int ) bytes ;
2010-04-20 00:07:23 +00:00
end :
2008-01-18 18:14:53 +00:00
WRITE_DEC ( rtp_session ) ;
return ret ;
2006-04-03 21:00:13 +00:00
}
2006-04-29 23:43:28 +00:00
SWITCH_DECLARE ( switch_status_t ) switch_rtp_disable_vad ( switch_rtp_t * rtp_session )
2006-04-21 22:31:08 +00:00
{
2007-03-07 23:24:09 +00:00
if ( ! switch_rtp_ready ( rtp_session ) ) {
return SWITCH_STATUS_FALSE ;
}
2006-04-21 22:31:08 +00:00
if ( ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_VAD ) ) {
return SWITCH_STATUS_GENERR ;
}
switch_core_codec_destroy ( & rtp_session - > vad_data . vad_codec ) ;
2006-08-28 23:05:26 +00:00
switch_clear_flag_locked ( rtp_session , SWITCH_RTP_FLAG_VAD ) ;
2006-04-21 22:31:08 +00:00
return SWITCH_STATUS_SUCCESS ;
}
2007-03-30 00:15:25 +00:00
SWITCH_DECLARE ( 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 )
2006-04-21 22:31:08 +00:00
{
2007-03-07 23:24:09 +00:00
if ( ! switch_rtp_ready ( rtp_session ) ) {
return SWITCH_STATUS_FALSE ;
}
2006-04-21 22:31:08 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_VAD ) ) {
return SWITCH_STATUS_GENERR ;
}
2011-12-28 21:27:39 +00:00
2006-04-21 22:31:08 +00:00
memset ( & rtp_session - > vad_data , 0 , sizeof ( rtp_session - > vad_data ) ) ;
2007-03-29 22:31:56 +00:00
2011-12-28 21:27:39 +00:00
if ( switch_true ( switch_channel_get_variable ( switch_core_session_get_channel ( session ) , " fire_talk_events " ) ) ) {
2011-12-16 15:16:37 +00:00
rtp_session - > vad_data . fire_events | = VAD_FIRE_TALK ;
}
2011-12-28 21:27:39 +00:00
if ( switch_true ( switch_channel_get_variable ( switch_core_session_get_channel ( session ) , " fire_not_talk_events " ) ) ) {
2011-12-16 15:16:37 +00:00
rtp_session - > vad_data . fire_events | = VAD_FIRE_NOT_TALK ;
}
2006-04-21 22:31:08 +00:00
if ( switch_core_codec_init ( & rtp_session - > vad_data . vad_codec ,
2006-06-23 20:14:29 +00:00
codec - > implementation - > iananame ,
2006-10-09 02:24:43 +00:00
NULL ,
2006-04-21 22:31:08 +00:00
codec - > implementation - > samples_per_second ,
2008-10-20 17:48:42 +00:00
codec - > implementation - > microseconds_per_packet / 1000 ,
2006-04-21 22:31:08 +00:00
codec - > implementation - > number_of_channels ,
2007-03-30 00:13:31 +00:00
SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE , NULL , rtp_session - > pool ) ! = SWITCH_STATUS_SUCCESS ) {
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " Can't load codec? \n " ) ;
2006-04-21 22:31:08 +00:00
return SWITCH_STATUS_FALSE ;
}
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_DEBUG , " Activate VAD codec %s %dms \n " , codec - > implementation - > iananame ,
2008-10-20 17:48:42 +00:00
codec - > implementation - > microseconds_per_packet / 1000 ) ;
2006-04-21 22:31:08 +00:00
rtp_session - > vad_data . diff_level = 400 ;
rtp_session - > vad_data . hangunder = 15 ;
rtp_session - > vad_data . hangover = 40 ;
2006-04-22 16:25:33 +00:00
rtp_session - > vad_data . bg_len = 5 ;
rtp_session - > vad_data . bg_count = 5 ;
rtp_session - > vad_data . bg_level = 300 ;
2006-04-21 22:31:08 +00:00
rtp_session - > vad_data . read_codec = codec ;
rtp_session - > vad_data . session = session ;
rtp_session - > vad_data . flags = flags ;
rtp_session - > vad_data . cng_freq = 50 ;
rtp_session - > vad_data . ts = 1 ;
2006-04-22 16:25:33 +00:00
rtp_session - > vad_data . start = 0 ;
2009-01-25 21:23:07 +00:00
rtp_session - > vad_data . next_scan = switch_epoch_time_now ( NULL ) ;
2006-04-21 22:31:08 +00:00
rtp_session - > vad_data . scan_freq = 0 ;
2006-08-28 23:05:26 +00:00
switch_set_flag_locked ( rtp_session , SWITCH_RTP_FLAG_VAD ) ;
2006-04-22 16:12:03 +00:00
switch_set_flag ( & rtp_session - > vad_data , SWITCH_VAD_FLAG_CNG ) ;
2006-04-21 22:31:08 +00:00
return SWITCH_STATUS_SUCCESS ;
}
2008-01-11 04:23:09 +00:00
SWITCH_DECLARE ( int ) switch_rtp_write_frame ( switch_rtp_t * rtp_session , switch_frame_t * frame )
2006-04-18 16:20:47 +00:00
{
2007-03-07 23:24:09 +00:00
uint8_t fwd = 0 ;
2008-01-11 04:23:09 +00:00
void * data = NULL ;
uint32_t len , ts = 0 ;
2009-11-18 19:22:45 +00:00
switch_payload_t payload = 0 ;
2008-01-11 04:23:09 +00:00
rtp_msg_t * send_msg = NULL ;
2010-02-06 03:38:24 +00:00
2008-01-18 18:14:53 +00:00
if ( ! switch_rtp_ready ( rtp_session ) | | ! rtp_session - > remote_addr ) {
2007-03-07 23:24:09 +00:00
return - 1 ;
}
2010-02-06 03:38:24 +00:00
2010-05-13 02:25:54 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_PROXY_MEDIA ) | | switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_UDPTL ) ) {
2008-02-21 17:48:41 +00:00
switch_size_t bytes ;
2011-04-22 21:43:29 +00:00
//char bufa[30];
2008-02-21 17:48:41 +00:00
/* Fast PASS! */
2010-05-13 02:25:54 +00:00
if ( ! switch_test_flag ( frame , SFF_PROXY_PACKET ) & & ! switch_test_flag ( frame , SFF_UDPTL_PACKET ) ) {
2008-02-21 17:48:41 +00:00
return 0 ;
}
bytes = frame - > packetlen ;
2011-04-22 21:43:29 +00:00
//tx_host = switch_get_addr(bufa, sizeof(bufa), rtp_session->remote_addr);
2010-02-06 03:38:24 +00:00
2009-11-18 19:22:45 +00:00
send_msg = frame - > packet ;
2010-03-11 17:37:08 +00:00
/*
2010-04-20 00:07:23 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_VIDEO ) ) {
send_msg - > header . pt = rtp_session - > payload ;
}
2010-03-11 17:37:08 +00:00
*/
2009-11-18 19:22:45 +00:00
2009-11-23 16:32:25 +00:00
if ( switch_socket_sendto ( rtp_session - > sock_output , rtp_session - > remote_addr , 0 , frame - > packet , & bytes ) ! = SWITCH_STATUS_SUCCESS ) {
return - 1 ;
2008-02-21 17:48:41 +00:00
}
2010-02-06 03:38:24 +00:00
2009-04-03 21:48:52 +00:00
2009-04-15 15:08:21 +00:00
rtp_session - > stats . outbound . raw_bytes + = bytes ;
rtp_session - > stats . outbound . media_bytes + = bytes ;
rtp_session - > stats . outbound . media_packet_count + + ;
rtp_session - > stats . outbound . packet_count + + ;
2008-02-21 17:48:41 +00:00
return ( int ) bytes ;
}
2009-05-23 01:50:56 +00:00
# ifdef ENABLE_ZRTP
2010-01-07 23:29:06 +00:00
if ( zrtp_on & & switch_test_flag ( rtp_session , SWITCH_ZRTP_FLAG_SECURE_MITM_SEND ) ) {
2009-05-27 01:40:11 +00:00
zrtp_session_info_t zrtp_session_info ;
2010-02-06 03:38:24 +00:00
if ( zrtp_status_ok = = zrtp_session_get ( rtp_session - > zrtp_session , & zrtp_session_info ) ) {
if ( zrtp_session_info . sas_is_ready ) {
2010-01-07 23:29:06 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
switch_channel_t * channel = switch_core_session_get_channel ( session ) ;
2012-05-29 18:10:15 +00:00
const char * uuid = switch_channel_get_partner_uuid ( channel ) ;
2010-02-06 03:38:24 +00:00
if ( uuid ) {
2010-01-07 23:29:06 +00:00
switch_core_session_t * other_session ;
if ( ( other_session = switch_core_session_locate ( uuid ) ) ) {
switch_channel_t * other_channel = switch_core_session_get_channel ( other_session ) ;
2010-02-03 19:19:10 +00:00
switch_rtp_t * other_rtp_session = switch_channel_get_private ( other_channel , " __zrtp_audio_rtp_session " ) ;
2010-01-07 23:29:06 +00:00
if ( other_rtp_session ) {
if ( zrtp_status_ok = = zrtp_session_get ( other_rtp_session - > zrtp_session , & zrtp_session_info ) ) {
if ( rtp_session - > zrtp_mitm_tries > ZRTP_MITM_TRIES ) {
switch_clear_flag ( rtp_session , SWITCH_ZRTP_FLAG_SECURE_MITM_RECV ) ;
switch_clear_flag ( rtp_session , SWITCH_ZRTP_FLAG_SECURE_MITM_SEND ) ;
switch_clear_flag ( other_rtp_session , SWITCH_ZRTP_FLAG_SECURE_MITM_RECV ) ;
switch_clear_flag ( other_rtp_session , SWITCH_ZRTP_FLAG_SECURE_MITM_SEND ) ;
2010-02-03 19:19:10 +00:00
} else if ( zrtp_status_ok = = zrtp_resolve_mitm_call ( other_rtp_session - > zrtp_stream , rtp_session - > zrtp_stream ) ) {
2010-01-07 23:29:06 +00:00
switch_clear_flag ( rtp_session , SWITCH_ZRTP_FLAG_SECURE_MITM_RECV ) ;
switch_clear_flag ( rtp_session , SWITCH_ZRTP_FLAG_SECURE_MITM_SEND ) ;
switch_clear_flag ( other_rtp_session , SWITCH_ZRTP_FLAG_SECURE_MITM_RECV ) ;
switch_clear_flag ( other_rtp_session , SWITCH_ZRTP_FLAG_SECURE_MITM_SEND ) ;
2010-02-06 03:38:24 +00:00
zrtp_verified_set ( zrtp_global , & rtp_session - > zrtp_session - > zid ,
2012-05-17 05:33:44 +00:00
& rtp_session - > zrtp_session - > peer_zid , ( uint8_t ) ( zrtp_session_info . sas_is_verified ^ 1 ) ) ;
2010-01-07 23:29:06 +00:00
rtp_session - > zrtp_mitm_tries + + ;
}
rtp_session - > zrtp_mitm_tries + + ;
}
}
switch_core_session_rwunlock ( other_session ) ;
}
2009-05-27 01:40:11 +00:00
}
}
2009-05-24 19:11:36 +00:00
}
2009-05-23 01:50:56 +00:00
}
# endif
2008-02-21 17:48:41 +00:00
2007-03-07 23:24:09 +00:00
fwd = ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_RAW_WRITE ) & & switch_test_flag ( frame , SFF_RAW_RTP ) ) ? 1 : 0 ;
2011-11-14 18:37:45 +00:00
if ( ! fwd & & ! rtp_session - > sending_dtmf & & ! rtp_session - > queue_delay & &
switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_RAW_WRITE ) & & ( rtp_session - > rtp_bugs & RTP_BUG_GEN_ONE_GEN_ALL ) ) {
2012-04-10 21:17:16 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_WARNING , " Generating RTP locally but timestamp passthru is configured, disabling.... \n " ) ;
2011-10-07 15:40:30 +00:00
switch_clear_flag ( rtp_session , SWITCH_RTP_FLAG_RAW_WRITE ) ;
2011-11-14 18:37:45 +00:00
rtp_session - > last_write_ts = RTP_TS_RESET ;
2011-10-07 15:40:30 +00:00
}
2007-12-11 19:23:57 +00:00
switch_assert ( frame ! = NULL ) ;
2007-03-07 21:21:42 +00:00
if ( switch_test_flag ( frame , SFF_CNG ) ) {
2009-11-18 19:22:45 +00:00
if ( rtp_session - > cng_pt ) {
payload = rtp_session - > cng_pt ;
} else {
2010-02-06 03:38:24 +00:00
return ( int ) frame - > packetlen ;
2009-11-18 19:22:45 +00:00
}
2007-03-07 21:21:42 +00:00
} else {
2007-03-10 00:43:53 +00:00
payload = rtp_session - > payload ;
2007-03-07 21:21:42 +00:00
}
2007-04-19 21:40:50 +00:00
if ( switch_test_flag ( frame , SFF_RTP_HEADER ) ) {
2010-02-06 03:38:24 +00:00
switch_size_t wrote = switch_rtp_write_manual ( rtp_session , frame - > data , frame - > datalen ,
2009-04-03 21:48:52 +00:00
frame - > m , frame - > payload , ( uint32_t ) ( frame - > timestamp ) , & frame - > flags ) ;
2009-04-15 15:08:21 +00:00
rtp_session - > stats . outbound . raw_bytes + = wrote ;
rtp_session - > stats . outbound . media_bytes + = wrote ;
rtp_session - > stats . outbound . media_packet_count + + ;
rtp_session - > stats . outbound . packet_count + + ;
2007-04-19 21:40:50 +00:00
}
2007-04-05 19:29:48 +00:00
if ( fwd ) {
2008-01-11 04:23:09 +00:00
send_msg = frame - > packet ;
2006-04-18 16:20:47 +00:00
len = frame - > packetlen ;
2008-01-11 04:23:09 +00:00
ts = 0 ;
2010-08-25 00:12:24 +00:00
// Trying this based on http://jira.freeswitch.org/browse/MODSOFIA-90
//if (frame->codec && frame->codec->agreed_pt == frame->payload) {
send_msg - > header . pt = payload ;
//}
2006-04-18 16:20:47 +00:00
} else {
data = frame - > data ;
len = frame - > datalen ;
2008-12-09 23:31:12 +00:00
ts = switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_RAW_WRITE ) ? ( uint32_t ) frame - > timestamp : 0 ;
2006-04-18 16:20:47 +00:00
}
2010-03-11 17:37:08 +00:00
/*
2010-04-20 00:07:23 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_VIDEO ) ) {
send_msg - > header . pt = rtp_session - > payload ;
}
2010-03-11 17:37:08 +00:00
*/
2010-02-06 03:38:24 +00:00
2008-01-11 04:23:09 +00:00
return rtp_common_write ( rtp_session , send_msg , data , len , payload , ts , & frame - > flags ) ;
2006-04-18 16:20:47 +00:00
}
2009-04-03 21:48:52 +00:00
SWITCH_DECLARE ( switch_rtp_stats_t * ) switch_rtp_get_stats ( switch_rtp_t * rtp_session , switch_memory_pool_t * pool )
{
switch_rtp_stats_t * s ;
if ( pool ) {
s = switch_core_alloc ( pool , sizeof ( * s ) ) ;
* s = rtp_session - > stats ;
} else {
s = & rtp_session - > stats ;
}
2011-05-06 20:25:00 +00:00
if ( rtp_session - > jb ) {
s - > inbound . largest_jb_size = stfu_n_get_most_qlen ( rtp_session - > jb ) ;
}
2009-04-03 21:48:52 +00:00
return s ;
}
2007-02-13 17:13:57 +00:00
SWITCH_DECLARE ( int ) switch_rtp_write_manual ( switch_rtp_t * rtp_session ,
2008-05-27 04:30:03 +00:00
void * data , uint32_t datalen , uint8_t m , switch_payload_t payload , uint32_t ts , switch_frame_flag_t * flags )
2006-04-03 21:00:13 +00:00
{
2007-03-08 22:18:33 +00:00
switch_size_t bytes ;
2008-01-18 18:14:53 +00:00
int ret = - 1 ;
2006-04-03 21:00:13 +00:00
2008-01-18 18:14:53 +00:00
if ( ! switch_rtp_ready ( rtp_session ) | | ! rtp_session - > remote_addr | | datalen > SWITCH_RTP_MAX_BUF_LEN ) {
2007-03-07 23:24:09 +00:00
return - 1 ;
}
2008-01-18 18:14:53 +00:00
WRITE_INC ( rtp_session ) ;
2007-12-17 23:43:15 +00:00
2007-12-18 17:33:29 +00:00
rtp_session - > write_msg = rtp_session - > send_msg ;
2008-01-11 16:01:07 +00:00
rtp_session - > write_msg . header . seq = htons ( + + rtp_session - > seq ) ;
2007-12-18 17:33:29 +00:00
rtp_session - > write_msg . header . ts = htonl ( ts ) ;
rtp_session - > write_msg . header . pt = payload ;
2012-05-08 17:52:47 +00:00
rtp_session - > write_msg . header . m = m ;
2007-12-18 17:33:29 +00:00
memcpy ( rtp_session - > write_msg . body , data , datalen ) ;
2006-04-03 21:00:13 +00:00
2007-03-08 22:18:33 +00:00
bytes = rtp_header_len + datalen ;
2011-05-02 16:33:57 +00:00
# ifdef ENABLE_SRTP
2008-01-17 04:16:04 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_SECURE_SEND ) ) {
2012-04-10 21:17:16 +00:00
switch_core_session_t * session = switch_core_memory_pool_get_data ( rtp_session - > pool , " __session " ) ;
2007-03-29 22:31:56 +00:00
int sbytes = ( int ) bytes ;
2007-03-08 22:18:33 +00:00
err_status_t stat ;
2008-01-17 04:16:04 +00:00
if ( switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_SECURE_SEND_RESET ) ) {
switch_clear_flag_locked ( rtp_session , SWITCH_RTP_FLAG_SECURE_SEND_RESET ) ;
srtp_dealloc ( rtp_session - > send_ctx ) ;
rtp_session - > send_ctx = NULL ;
if ( ( stat = srtp_create ( & rtp_session - > send_ctx , & rtp_session - > send_policy ) ) ) {
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " Error! RE-Activating Secure RTP SEND \n " ) ;
2008-01-18 18:14:53 +00:00
ret = - 1 ;
goto end ;
2008-01-17 04:16:04 +00:00
} else {
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_INFO , " RE-Activating Secure RTP SEND \n " ) ;
2008-01-17 04:16:04 +00:00
}
}
2007-12-18 17:33:29 +00:00
stat = srtp_protect ( rtp_session - > send_ctx , & rtp_session - > write_msg . header , & sbytes ) ;
2007-03-08 22:18:33 +00:00
if ( stat ) {
2012-04-10 21:17:16 +00:00
switch_log_printf ( SWITCH_CHANNEL_SESSION_LOG ( session ) , SWITCH_LOG_ERROR , " Error: SRTP protection failed with code %d \n " , stat ) ;
2007-03-08 22:18:33 +00:00
}
bytes = sbytes ;
}
2011-05-02 16:33:57 +00:00
# endif
2009-05-20 23:04:05 +00:00
# ifdef ENABLE_ZRTP
2009-05-27 22:48:16 +00:00
/* ZRTP Send */
2012-01-25 05:38:53 +00:00
if ( zrtp_on & & ! switch_test_flag ( rtp_session , SWITCH_RTP_FLAG_PROXY_MEDIA ) ) {
2009-05-27 22:48:16 +00:00
unsigned int sbytes = ( int ) bytes ;
zrtp_status_t stat = zrtp_status_fail ;
2010-02-06 03:38:24 +00:00
stat = zrtp_process_rtp ( rtp_session - > zrtp_stream , ( void * ) & rtp_session - > write_msg , & sbytes ) ;
2009-05-27 22:48:16 +00:00
switch ( stat ) {
case zrtp_status_ok :
break ;
case zrtp_status_drop :
2012-04-11 14:10:56 +00:00
switch_log_printf ( SWITCH_CHANNEL_LOG , SWITCH_LOG_ERROR , " Error: zRTP protection drop with code %d \n " , stat ) ;
2010-02-03 19:19:10 +00:00
ret = ( int ) bytes ;
goto end ;
2009-05-27 22:48:16 +00:00
break ;
case zrtp_status_fail :
2012-04-11 14:10:56 +00:00
switch_log_printf ( SWITCH_CHANNEL_LOG , SWITCH_LOG_ERROR , " Error: zRTP protection fail with code %d \n " , stat ) ;
2009-05-27 22:48:16 +00:00
break ;
default :
break ;
2009-05-20 23:04:05 +00:00
}
2010-02-06 03:38:24 +00:00
2009-05-27 22:48:16 +00:00
bytes = sbytes ;
}
2009-05-20 23:04:05 +00:00
# endif
2010-02-06 03:38:24 +00:00
2008-07-03 18:50:15 +00:00
if ( switch_socket_sendto ( rtp_session - > sock_output , rtp_session - > remote_addr , 0 , ( void * ) & rtp_session - > write_msg , & bytes ) ! = SWITCH_STATUS_SUCCESS ) {
2008-01-10 03:10:58 +00:00
rtp_session - > seq - - ;
2008-01-18 18:14:53 +00:00
ret = - 1 ;
goto end ;
2007-03-08 22:18:33 +00:00
}
2008-01-10 03:10:58 +00:00
2011-08-31 18:52:21 +00:00
if ( ( ( * flags ) & SFF_RTP_HEADER ) ) {
2011-08-31 17:03:57 +00:00
rtp_session - > last_write_ts = ts ;
}
2008-01-10 03:10:58 +00:00
2008-01-18 18:14:53 +00:00
ret = ( int ) bytes ;
2008-05-27 04:30:03 +00:00
2010-04-20 00:07:23 +00:00
end :
2008-01-18 18:14:53 +00:00
WRITE_DEC ( rtp_session ) ;
return ret ;
2006-04-03 21:00:13 +00:00
}
2006-04-29 01:00:52 +00:00
SWITCH_DECLARE ( uint32_t ) switch_rtp_get_ssrc ( switch_rtp_t * rtp_session )
2006-04-03 21:00:13 +00:00
{
return rtp_session - > send_msg . header . ssrc ;
}
2006-04-29 01:00:52 +00:00
SWITCH_DECLARE ( void ) switch_rtp_set_private ( switch_rtp_t * rtp_session , void * private_data )
2006-04-03 21:00:13 +00:00
{
rtp_session - > private_data = private_data ;
}
2007-03-29 22:31:56 +00:00
SWITCH_DECLARE ( void * ) switch_rtp_get_private ( switch_rtp_t * rtp_session )
2006-04-03 21:00:13 +00:00
{
return rtp_session - > private_data ;
}
2006-11-27 22:30:48 +00:00
/* For Emacs:
* Local Variables :
* mode : c
2008-02-03 22:14:57 +00:00
* indent - tabs - mode : t
2006-11-27 22:30:48 +00:00
* tab - width : 4
* c - basic - offset : 4
* End :
* For VIM :
2008-07-03 19:12:26 +00:00
* vim : set softtabstop = 4 shiftwidth = 4 tabstop = 4 :
2006-11-27 22:30:48 +00:00
*/