FS-3659 and some refactor for rtcp ice and video ice and rtcp

This commit is contained in:
Anthony Minessale 2012-06-22 18:08:59 -05:00
parent c30940462a
commit 8b4c9dc42c
4 changed files with 410 additions and 174 deletions

View File

@ -215,7 +215,8 @@ SWITCH_DECLARE(void) switch_rtp_destroy(switch_rtp_t **rtp_session);
\brief Acvite ICE on an RTP session
\return SWITCH_STATUS_SUCCESS
*/
SWITCH_DECLARE(switch_status_t) switch_rtp_activate_ice(switch_rtp_t *rtp_session, char *login, char *rlogin);
SWITCH_DECLARE(switch_status_t) switch_rtp_activate_ice(switch_rtp_t *rtp_session, char *login, char *rlogin, const char *password);
SWITCH_DECLARE(switch_status_t) switch_rtp_activate_rtcp_ice(switch_rtp_t *rtp_session, char *login, char *rlogin, const char *password);
/*!
\brief Activate sending RTCP Sender Reports (SR's)
@ -268,7 +269,7 @@ SWITCH_DECLARE(void) switch_rtp_clear_flag(switch_rtp_t *rtp_session, switch_rtp
\return the socket from the RTP session
*/
SWITCH_DECLARE(switch_socket_t *) switch_rtp_get_rtp_socket(switch_rtp_t *rtp_session);
SWITCH_DECLARE(void) switch_rtp_ping(switch_rtp_t *rtp_session);
/*!
\brief Get the default samples per interval for a given RTP session
\param rtp_session the RTP session to get the samples per interval from

View File

@ -539,6 +539,7 @@ typedef struct {
switch_size_t raw_bytes;
switch_size_t media_bytes;
switch_size_t packet_count;
switch_size_t period_packet_count;
switch_size_t media_packet_count;
switch_size_t skip_packet_count;
switch_size_t jb_packet_count;
@ -558,6 +559,7 @@ typedef struct {
switch_rtp_numbers_t inbound;
switch_rtp_numbers_t outbound;
switch_rtcp_numbers_t rtcp;
uint32_t read_count;
} switch_rtp_stats_t;
typedef enum {
@ -890,6 +892,7 @@ typedef enum {
SWITCH_MESSAGE_INDICATE_INFO,
SWITCH_MESSAGE_INDICATE_AUDIO_DATA,
SWITCH_MESSAGE_INDICATE_BLIND_TRANSFER_RESPONSE,
SWITCH_MESSAGE_INDICATE_STUN_ERROR,
SWITCH_MESSAGE_INVALID
} switch_core_session_message_types_t;

View File

@ -645,6 +645,7 @@ static const char *message_names[] = {
"INFO",
"AUDIO_DATA",
"BLIND_TRANSFER_RESPONSE",
"STUN_ERROR",
"INVALID"
};

View File

@ -153,6 +153,15 @@ struct switch_rtp_rfc2833_data {
uint8_t in_digit_queued;
};
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;
struct switch_rtp {
/*
* Two sockets are needed because we might be transcoding protocol families
@ -206,8 +215,8 @@ struct switch_rtp {
switch_sockaddr_t *from_addr, *rtcp_from_addr;
char *rx_host;
switch_port_t rx_port;
char *ice_user;
char *user_ice;
switch_rtp_ice_t ice;
switch_rtp_ice_t rtcp_ice;
char *timer_name;
char *local_host_str;
char *remote_host_str;
@ -222,9 +231,7 @@ struct switch_rtp {
switch_port_t remote_port;
switch_port_t eff_remote_port;
switch_port_t remote_rtcp_port;
uint32_t stuncount;
uint32_t funny_stun;
uint32_t default_stuncount;
struct switch_rtp_vad_data vad_data;
struct switch_rtp_rfc2833_data dtmf_data;
switch_payload_t te;
@ -503,8 +510,8 @@ static switch_status_t do_stun_ping(switch_rtp_t *rtp_session)
WRITE_INC(rtp_session);
if (rtp_session->stuncount != 0) {
rtp_session->stuncount--;
if (rtp_session->ice.stuncount != 0) {
rtp_session->ice.stuncount--;
goto end;
}
#if 0
@ -519,7 +526,7 @@ static switch_status_t do_stun_ping(switch_rtp_t *rtp_session)
}
#endif
if (rtp_session->funny_stun) {
if (rtp_session->ice.funny_stun) {
*start++ = 0;
*start++ = 0;
*start++ = 0x22;
@ -529,14 +536,14 @@ static switch_status_t do_stun_ping(switch_rtp_t *rtp_session)
packet = switch_stun_packet_build_header(SWITCH_STUN_BINDING_REQUEST, NULL, start);
bytes = switch_stun_packet_length(packet);
if (rtp_session->funny_stun) {
if (rtp_session->ice.funny_stun) {
packet = (switch_stun_packet_t *) buf;
bytes += 4;
}
switch_socket_sendto(rtp_session->sock_output, rtp_session->remote_stun_addr, 0, (void *) packet, &bytes);
rtp_session->stuncount = rtp_session->default_stuncount;
rtp_session->ice.stuncount = rtp_session->ice.default_stuncount;
end:
WRITE_DEC(rtp_session);
@ -544,21 +551,28 @@ static switch_status_t do_stun_ping(switch_rtp_t *rtp_session)
return status;
}
static switch_status_t ice_out(switch_rtp_t *rtp_session)
static switch_status_t ice_out(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice)
{
uint8_t buf[256] = { 0 };
switch_stun_packet_t *packet;
unsigned int elapsed;
switch_size_t bytes;
switch_status_t status = SWITCH_STATUS_SUCCESS;
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;
}
switch_assert(rtp_session != NULL);
switch_assert(rtp_session->ice_user != NULL);
switch_assert(ice->ice_user != NULL);
WRITE_INC(rtp_session);
READ_INC(rtp_session);
if (rtp_session->stuncount != 0) {
rtp_session->stuncount--;
if (ice->stuncount != 0) {
ice->stuncount--;
goto end;
}
@ -574,13 +588,19 @@ static switch_status_t ice_out(switch_rtp_t *rtp_session)
}
packet = switch_stun_packet_build_header(SWITCH_STUN_BINDING_REQUEST, NULL, buf);
switch_stun_packet_attribute_add_username(packet, rtp_session->ice_user, 32);
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));
}
bytes = switch_stun_packet_length(packet);
switch_socket_sendto(rtp_session->sock_output, rtp_session->remote_addr, 0, (void *) packet, &bytes);
rtp_session->stuncount = rtp_session->default_stuncount;
switch_socket_sendto(sock_output, remote_addr, 0, (void *) packet, &bytes);
ice->stuncount = ice->default_stuncount;
end:
WRITE_DEC(rtp_session);
READ_DEC(rtp_session);
return status;
}
@ -595,7 +615,7 @@ static void handle_stun_ping_reply(switch_rtp_t *rtp_session, void *data, switch
rtp_session->last_stun = switch_micro_time_now();
}
static void handle_ice(switch_rtp_t *rtp_session, void *data, switch_size_t len)
static void handle_ice(switch_rtp_t *rtp_session, switch_rtp_ice_t *ice, void *data, switch_size_t len)
{
switch_stun_packet_t *packet;
switch_stun_packet_attribute_t *attr;
@ -604,7 +624,7 @@ static void handle_ice(switch_rtp_t *rtp_session, void *data, switch_size_t len)
unsigned char buf[512] = { 0 };
switch_size_t cpylen = len;
if (!switch_rtp_ready(rtp_session)) {
if (!switch_rtp_ready(rtp_session) || zstr(ice->user_ice) || zstr(ice->ice_user)) {
return;
}
@ -620,12 +640,18 @@ static void handle_ice(switch_rtp_t *rtp_session, void *data, switch_size_t len)
cpylen = 512;
}
memcpy(buf, data, cpylen);
packet = switch_stun_packet_parse(buf, sizeof(buf));
if (!packet) {
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, "Invalid STUN/ICE packet received\n");
goto end;
//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;
//}
}
end_buf = buf + ((sizeof(buf) > packet->header.length) ? packet->header.length : sizeof(buf));
@ -650,20 +676,48 @@ static void handle_ice(switch_rtp_t *rtp_session, void *data, switch_size_t len)
}
} while (switch_stun_packet_next_attribute(attr, end_buf));
if ((packet->header.type == SWITCH_STUN_BINDING_REQUEST) && !strcmp(rtp_session->user_ice, username)) {
if ((packet->header.type == SWITCH_STUN_BINDING_REQUEST) && !strcmp(ice->user_ice, username)) {
uint8_t stunbuf[512];
switch_stun_packet_t *rpacket;
const char *remote_ip;
switch_size_t bytes;
char ipbuf[25];
//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;
}
memset(stunbuf, 0, sizeof(stunbuf));
rpacket = switch_stun_packet_build_header(SWITCH_STUN_BINDING_RESPONSE, packet->header.id, stunbuf);
switch_stun_packet_attribute_add_username(rpacket, username, 32);
remote_ip = switch_get_addr(ipbuf, sizeof(ipbuf), rtp_session->from_addr);
switch_stun_packet_attribute_add_binded_address(rpacket, (char *) remote_ip, switch_sockaddr_get_port(rtp_session->from_addr));
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));
bytes = switch_stun_packet_length(rpacket);
switch_socket_sendto(rtp_session->sock_output, rtp_session->from_addr, 0, (void *) rpacket, &bytes);
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");
}
}
end:
@ -890,6 +944,157 @@ SWITCH_DECLARE(void) switch_rtp_init(switch_memory_pool_t *pool)
global_init = 1;
}
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)) {
unsigned int sbytes = (int) bytes;
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);
ret = (int) bytes;
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;
}
bytes = sbytes;
}
#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);
}
SWITCH_DECLARE(void) switch_rtp_get_random(void *buf, uint32_t len)
{
#ifdef ENABLE_SRTP
@ -1653,11 +1858,7 @@ SWITCH_DECLARE(switch_status_t) switch_rtp_create(switch_rtp_t **new_rtp_session
rtp_session->payload = payload;
rtp_session->rpayload = payload;
rtp_session->rtcp_send_msg.header.version = 2;
rtp_session->rtcp_send_msg.header.p = 0;
rtp_session->rtcp_send_msg.header.type = 200;
rtp_session->rtcp_send_msg.header.count = 0;
rtp_session->rtcp_send_msg.header.length = htons(6);
switch_rtp_set_interval(rtp_session, ms_per_packet, samples_per_interval);
@ -1871,12 +2072,12 @@ SWITCH_DECLARE(switch_status_t) switch_rtp_activate_stun_ping(switch_rtp_t *rtp_
}
if (funny) {
rtp_session->funny_stun++;
rtp_session->ice.funny_stun++;
}
rtp_session->stun_port = stun_port;
rtp_session->default_stuncount = packet_count;
rtp_session->ice.default_stuncount = packet_count;
rtp_session->stun_ip = switch_core_strdup(rtp_session->pool, stun_ip);
return SWITCH_STATUS_SUCCESS;
@ -2032,19 +2233,25 @@ SWITCH_DECLARE(switch_status_t) switch_rtp_activate_rtcp(switch_rtp_t *rtp_sessi
}
SWITCH_DECLARE(switch_status_t) switch_rtp_activate_ice(switch_rtp_t *rtp_session, char *login, char *rlogin)
SWITCH_DECLARE(switch_status_t) switch_rtp_activate_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->ice_user = switch_core_strdup(rtp_session->pool, ice_user);
rtp_session->user_ice = switch_core_strdup(rtp_session->pool, user_ice);
rtp_session->default_stuncount = 25;
if (rtp_session->ice_user) {
if (ice_out(rtp_session) != SWITCH_STATUS_SUCCESS) {
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;
}
}
@ -2052,6 +2259,32 @@ SWITCH_DECLARE(switch_status_t) switch_rtp_activate_ice(switch_rtp_t *rtp_sessio
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) {
return SWITCH_STATUS_FALSE;
}
}
return SWITCH_STATUS_SUCCESS;
}
SWITCH_DECLARE(void) switch_rtp_flush(switch_rtp_t *rtp_session)
{
if (!switch_rtp_ready(rtp_session)) {
@ -2641,6 +2874,7 @@ static switch_status_t read_rtp_packet(switch_rtp_t *rtp_session, switch_size_t
rtp_session->stats.inbound.packet_count++;
if (!switch_test_flag(rtp_session, SWITCH_RTP_FLAG_PROXY_MEDIA) && !switch_test_flag(rtp_session, SWITCH_RTP_FLAG_UDPTL)) {
#ifdef ENABLE_ZRTP
/* ZRTP Recv */
@ -2669,7 +2903,7 @@ static switch_status_t read_rtp_packet(switch_rtp_t *rtp_session, switch_size_t
#endif
#ifdef ENABLE_SRTP
if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_RECV)) {
if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_RECV) && rtp_session->rtcp_recv_msg.header.version == 2) {
int sbytes = (int) *bytes;
err_status_t stat = 0;
@ -2788,7 +3022,7 @@ static switch_status_t read_rtcp_packet(switch_rtp_t *rtp_session, switch_size_t
}
#ifdef ENABLE_SRTP
if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_RECV)) {
if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_SECURE_RECV) && rtp_session->rtcp_recv_msg.header.version == 2) {
int sbytes = (int) *bytes;
err_status_t stat = 0;
@ -2876,15 +3110,27 @@ static switch_status_t read_rtcp_packet(switch_rtp_t *rtp_session, switch_size_t
ntohl(sr->oc));
}
} else {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Received an unsupported RTCP packet version %d\nn", rtp_session->rtcp_recv_msg.header.version);
}
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\nn", rtp_session->rtcp_recv_msg.header.version);
}
}
status = SWITCH_STATUS_SUCCESS;
status = SWITCH_STATUS_SUCCESS;
}
}
return status;
}
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)
{
switch_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
@ -2917,6 +3163,8 @@ static int rtp_common_read(switch_rtp_t *rtp_session, switch_payload_t *payload_
READ_INC(rtp_session);
while (switch_rtp_ready(rtp_session)) {
int do_cng = 0;
int read_pretriggered = 0;
@ -2967,11 +3215,14 @@ static int rtp_common_read(switch_rtp_t *rtp_session, switch_payload_t *payload_
}
recvfrom:
rtp_session->stats.read_count++;
if (!read_pretriggered) {
bytes = 0;
}
read_loops++;
poll_loop = 0;
//poll_loop = 0;
if (!switch_rtp_ready(rtp_session)) {
break;
@ -2982,7 +3233,7 @@ static int rtp_common_read(switch_rtp_t *rtp_session, switch_payload_t *payload_
do_2833(rtp_session, session);
if (rtp_session->dtmf_data.out_digit_dur > 0 || rtp_session->dtmf_data.in_digit_sanity || rtp_session->sending_dtmf ||
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 ||
switch_queue_size(rtp_session->dtmf_data.dtmf_queue) || switch_queue_size(rtp_session->dtmf_data.dtmf_inqueue)) {
pt = 20000;
}
@ -2993,6 +3244,7 @@ static int rtp_common_read(switch_rtp_t *rtp_session, switch_payload_t *payload_
poll_status = switch_poll(rtp_session->read_pollfd, 1, &fdr, pt);
if (rtp_session->dtmf_data.out_digit_dur > 0) {
return_cng_frame();
}
@ -3003,9 +3255,12 @@ static int rtp_common_read(switch_rtp_t *rtp_session, switch_payload_t *payload_
read_pretriggered = 0;
} else {
status = read_rtp_packet(rtp_session, &bytes, flags, SWITCH_TRUE);
/* switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Read bytes (%i) %d\n", status, bytes); */
//switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Read bytes (%i) %ld\n", status, bytes);
}
poll_loop = 0;
} else {
int vid_cng = 1, ice = 0;
if (!SWITCH_STATUS_IS_BREAK(poll_status) && poll_status != SWITCH_STATUS_TIMEOUT) {
char tmp[128] = "";
strerror_r(poll_status, tmp, sizeof(tmp));
@ -3014,7 +3269,42 @@ static int rtp_common_read(switch_rtp_t *rtp_session, switch_payload_t *payload_
goto end;
}
poll_loop = 1;
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++;
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);
@ -3029,7 +3319,7 @@ static int rtp_common_read(switch_rtp_t *rtp_session, switch_payload_t *payload_
}
if ((!(io_flags & SWITCH_IO_FLAG_NOBLOCK)) &&
(rtp_session->dtmf_data.out_digit_dur == 0 || switch_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO))) {
(rtp_session->dtmf_data.out_digit_dur == 0 || (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_VIDEO) && vid_cng))) {
return_cng_frame();
}
}
@ -3045,73 +3335,73 @@ static int rtp_common_read(switch_rtp_t *rtp_session, switch_payload_t *payload_
switch_rtp_reset_media_timer(rtp_session);
if (switch_test_flag(rtp_session, SWITCH_RTP_FLAG_RTCP_PASSTHRU)) {
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_core_session_t *session = switch_core_memory_pool_get_data(rtp_session->pool, "__session");
switch_channel_t *channel = switch_core_session_get_channel(session);
const char *uuid = switch_channel_get_partner_uuid(channel);
if (uuid) {
switch_core_session_t *other_session;
switch_rtp_t *other_rtp_session = NULL;
const char *uuid = switch_channel_get_partner_uuid(channel);
if (uuid) {
switch_core_session_t *other_session;
switch_rtp_t *other_rtp_session = NULL;
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;
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;
#ifdef ENABLE_SRTP
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);
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;
}
rtcp_bytes = sbytes;
}
#endif
#ifdef ENABLE_ZRTP
/* 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;
/* 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;
stat = zrtp_process_rtcp(other_rtp_session->zrtp_stream, (void *) &other_rtp_session->rtcp_send_msg, &sbytes);
stat = zrtp_process_rtcp(other_rtp_session->zrtp_stream, (void *) &other_rtp_session->rtcp_send_msg, &sbytes);
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;
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;
}
bytes = sbytes;
}
bytes = sbytes;
}
#endif
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");
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");
}
}
switch_core_session_rwunlock(other_session);
}
switch_core_session_rwunlock(other_session);
}
}
}
}
}
}
}
if (bytes && rtp_session->recv_msg.header.version == 2 &&
@ -3274,6 +3564,8 @@ static int rtp_common_read(switch_rtp_t *rtp_session, switch_payload_t *payload_
*flags |= SFF_UDPTL_PACKET;
}
check_srtp_and_ice(rtp_session);
ret = (int) bytes;
goto end;
}
@ -3293,14 +3585,11 @@ static int rtp_common_read(switch_rtp_t *rtp_session, switch_payload_t *payload_
}
if (check && 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 (check_srtp_and_ice(rtp_session)) {
ret = -1;
goto end;
}
if (check || bytes) {
do_2833(rtp_session, session);
@ -3310,8 +3599,8 @@ static int rtp_common_read(switch_rtp_t *rtp_session, switch_payload_t *payload_
uint8_t *data = (uint8_t *) rtp_session->recv_msg.body;
if (rtp_session->recv_msg.header.version == 0) {
if (rtp_session->ice_user) {
handle_ice(rtp_session, (void *) &rtp_session->recv_msg, bytes);
if (rtp_session->ice.ice_user) {
handle_ice(rtp_session, &rtp_session->ice, (void *) &rtp_session->recv_msg, bytes);
goto recvfrom;
} else if (rtp_session->remote_stun_addr) {
handle_stun_ping_reply(rtp_session, (void *) &rtp_session->recv_msg, bytes);
@ -3331,6 +3620,8 @@ static int rtp_common_read(switch_rtp_t *rtp_session, switch_payload_t *payload_
*payload_type = (switch_payload_t) rtp_session->recv_msg.header.pt;
ret = 2 + rtp_header_len;
goto end;
} else if (bytes) {
rtp_session->stats.inbound.period_packet_count++;
}
@ -3711,7 +4002,7 @@ SWITCH_DECLARE(switch_status_t) switch_rtp_zerocopy_read(switch_rtp_t *rtp_sessi
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)
{
switch_size_t bytes, rtcp_bytes;
switch_size_t bytes;
uint8_t send = 1;
uint32_t this_ts = 0;
int ret;
@ -4057,76 +4348,16 @@ static int rtp_common_write(switch_rtp_t *rtp_session,
rtp_session->last_write_timestamp = switch_micro_time_now();
}
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.outbound.packet_count % rtp_session->rtcp_interval) == 0) {
struct switch_rtcp_senderinfo* sr = (struct switch_rtcp_senderinfo*)rtp_session->rtcp_send_msg.body;
sr->ssrc = send_msg->header.ssrc;
sr->ntp_msw = htonl((u_long)rtp_session->send_time / 1000000 + 2208988800UL);
sr->ntp_lsw = htonl((u_long)(rtp_session->send_time % 1000000 * ((UINT_MAX * 1.0)/ 1000000.0)));
sr->ts = send_msg->header.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);
#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)) {
unsigned int sbytes = (int) bytes;
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);
ret = (int) bytes;
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;
}
bytes = sbytes;
}
#endif
if (switch_socket_sendto(rtp_session->rtcp_sock_output, rtp_session->rtcp_remote_addr, 0,
(const char*)&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");
}
}
}
if (rtp_session->remote_stun_addr) {
do_stun_ping(rtp_session);
}
if (rtp_session->ice_user) {
if (ice_out(rtp_session) != SWITCH_STATUS_SUCCESS) {
#if 0
if (rtp_session->ice.ice_user) {
if (ice_out(rtp_session, &rtp_session->ice) != SWITCH_STATUS_SUCCESS) {
ret = -1;
goto end;
}
}
#endif
ret = (int) bytes;