Commit bdf34ccb authored by Ghislain MARY's avatar Ghislain MARY

Add more TURN statistics + add a mode to have short TURN refresh timings for tests.

parent 87141f0a
......@@ -127,6 +127,7 @@ typedef struct _IceSession {
uint8_t keepalive_timeout; /**< Configuration parameter to define the timeout between each keepalive packets (default is 15s) */
bool_t forced_relay; /**< Force use of relay by modifying the local and reflexive candidates */
bool_t turn_enabled; /**< TURN protocol enabled */
bool_t short_turn_refresh; /**< Short TURN refresh for tests */
} IceSession;
typedef struct _IceStunServerRequestTransaction {
......@@ -520,6 +521,14 @@ MS2_PUBLIC int ice_session_gathering_duration(IceSession *session);
*/
MS2_PUBLIC void ice_session_enable_forced_relay(IceSession *session, bool_t enable);
/**
* Enable short TURN refresh for tests.
* This changes the delay to send allocation refresh, create permission, and channel bind requests.
* @param session A pointer to a session
* @param enable A boolean value telling whether to use short turn refresh.
*/
MS2_PUBLIC void ice_session_enable_short_turn_refresh(IceSession *session, bool_t enable);
/**
* Enable TURN protol.
* @param session A pointer to a session
......
......@@ -184,6 +184,17 @@ typedef enum {
MS_TURN_CONTEXT_TYPE_RTCP
} MSTurnContextType;
typedef struct {
uint32_t nb_send_indication;
uint32_t nb_data_indication;
uint32_t nb_received_channel_msg;
uint32_t nb_sent_channel_msg;
uint16_t nb_successful_allocate;
uint16_t nb_successful_refresh;
uint16_t nb_successful_create_permission;
uint16_t nb_successful_channel_bind;
} MSTurnContextStatistics;
typedef struct {
RtpSession *rtp_session;
RtpTransport *endpoint;
......@@ -201,10 +212,7 @@ typedef struct {
struct sockaddr *turn_server_addr;
socklen_t turn_server_addrlen;
bool_t force_rtp_sending_via_relay;
uint32_t stats_nb_send_indication;
uint32_t stats_nb_data_indication;
uint32_t stats_nb_received_channel_msg;
uint32_t stats_nb_sent_channel_msg;
MSTurnContextStatistics stats;
} MSTurnContext;
......
......@@ -1063,11 +1063,14 @@ int ice_session_gathering_duration(IceSession *session)
+ ((session->gathering_end_ts.tv_nsec - session->gathering_start_ts.tv_nsec) / 1000000.0));
}
void ice_session_enable_forced_relay(IceSession *session, bool_t enable)
{
void ice_session_enable_forced_relay(IceSession *session, bool_t enable) {
session->forced_relay = enable;
}
void ice_session_enable_short_turn_refresh(IceSession *session, bool_t enable) {
session->short_turn_refresh = enable;
}
static void ice_check_list_create_turn_contexts(IceCheckList *cl) {
cl->rtp_turn_context = ms_turn_context_new(MS_TURN_CONTEXT_TYPE_RTP, cl->rtp_session);
cl->rtcp_turn_context = ms_turn_context_new(MS_TURN_CONTEXT_TYPE_RTCP, cl->rtp_session);
......@@ -2203,6 +2206,7 @@ static void ice_schedule_turn_allocation_refresh(IceCheckList *cl, int component
RtpTransport *rtptp = NULL;
const OrtpStream *stream = NULL;
struct sockaddr *sa;
uint32_t ms = (uint32_t)((lifetime * .9f) * 1000); /* 90% of the lifetime */
turn_context = ice_get_turn_context_from_check_list_componentID(cl, componentID);
ice_get_transport_from_rtp_session_and_componentID(cl->rtp_session, componentID, &rtptp);
......@@ -2210,7 +2214,8 @@ static void ice_schedule_turn_allocation_refresh(IceCheckList *cl, int component
sa = (struct sockaddr *)&stream->loc_addr;
bctbx_sockaddr_to_ip_address(sa, stream->loc_addrlen, source_addr_str, sizeof(source_addr_str), &source_port);
request = ice_stun_server_request_new(cl, turn_context, rtptp, sa->sa_family, source_addr_str, source_port, MS_TURN_METHOD_REFRESH);
request->next_transmission_time = ice_add_ms(ice_current_time(), (uint32_t)((lifetime * .9f) * 1000));
if (cl->session->short_turn_refresh == TRUE) ms = 5000; /* 5 seconds */
request->next_transmission_time = ice_add_ms(ice_current_time(), ms);
ice_check_list_add_stun_server_request(cl, request);
}
......@@ -2222,6 +2227,7 @@ static void ice_schedule_turn_permission_refresh(IceCheckList *cl, int component
RtpTransport *rtptp = NULL;
const OrtpStream *stream = NULL;
struct sockaddr *sa;
uint32_t ms = 240000; /* 4 minutes */
turn_context = ice_get_turn_context_from_check_list_componentID(cl, componentID);
ice_get_transport_from_rtp_session_and_componentID(cl->rtp_session, componentID, &rtptp);
......@@ -2230,7 +2236,8 @@ static void ice_schedule_turn_permission_refresh(IceCheckList *cl, int component
bctbx_sockaddr_to_ip_address(sa, stream->loc_addrlen, source_addr_str, sizeof(source_addr_str), &source_port);
request = ice_stun_server_request_new(cl, turn_context, rtptp, sa->sa_family, source_addr_str, source_port, MS_TURN_METHOD_CREATE_PERMISSION);
request->peer_address = peer_address;
request->next_transmission_time = ice_add_ms(ice_current_time(), 240000); /* 4 minutes */
if (cl->session->short_turn_refresh == TRUE) ms = 5000; /* 5 seconds */
request->next_transmission_time = ice_add_ms(ice_current_time(), ms);
ice_check_list_add_stun_server_request(cl, request);
}
......@@ -2242,6 +2249,7 @@ static void ice_schedule_turn_channel_bind_refresh(IceCheckList *cl, int compone
RtpTransport *rtptp = NULL;
const OrtpStream *stream = NULL;
struct sockaddr *sa;
uint32_t ms = 540000; /* 9 minutes */
turn_context = ice_get_turn_context_from_check_list_componentID(cl, componentID);
ice_get_transport_from_rtp_session_and_componentID(cl->rtp_session, componentID, &rtptp);
......@@ -2251,7 +2259,8 @@ static void ice_schedule_turn_channel_bind_refresh(IceCheckList *cl, int compone
request = ice_stun_server_request_new(cl, turn_context, rtptp, sa->sa_family, source_addr_str, source_port, MS_TURN_METHOD_CHANNEL_BIND);
request->channel_number = channel_number;
request->peer_address = peer_address;
request->next_transmission_time = ice_add_ms(ice_current_time(), 540000); /* 9 minutes */
if (cl->session->short_turn_refresh == TRUE) ms = 5000; /* 5 seconds */
request->next_transmission_time = ice_add_ms(ice_current_time(), ms);
ice_check_list_add_stun_server_request(cl, request);
}
......@@ -2289,6 +2298,7 @@ static bool_t ice_handle_received_turn_allocate_success_response(IceCheckList *c
ms_message("ice: Add candidate obtained by STUN/TURN: %s:srflx", srflx_addr_str);
ms_stun_address_to_ip_address(&relay_addr, relay_addr_str, sizeof(relay_addr_str), &relay_port);
if (cl->session->turn_enabled) {
request->turn_context->stats.nb_successful_allocate++;
ice_schedule_turn_allocation_refresh(cl, componentID, ms_stun_message_get_lifetime(msg));
}
if (relay_port != 0) {
......@@ -2349,6 +2359,7 @@ static void ice_handle_received_turn_refresh_success_response(IceCheckList *cl,
ms_turn_context_set_state(context, MS_TURN_CONTEXT_STATE_IDLE);
} else {
ice_schedule_turn_allocation_refresh(cl, componentID, ms_stun_message_get_lifetime(msg));
context->stats.nb_successful_refresh++;
}
}
......
......@@ -1319,6 +1319,8 @@ MSTurnContextState ms_turn_context_get_state(const MSTurnContext *context) {
void ms_turn_context_set_state(MSTurnContext *context, MSTurnContextState state) {
context->state = state;
if (state == MS_TURN_CONTEXT_STATE_ALLOCATION_CREATED) context->stats.nb_successful_allocate++;
else if (state == MS_TURN_CONTEXT_STATE_CHANNEL_BOUND) context->stats.nb_successful_channel_bind++;
}
const char * ms_turn_context_get_realm(const MSTurnContext *context) {
......@@ -1400,6 +1402,7 @@ void ms_turn_context_allow_peer_address(MSTurnContext *context, const MSStunAddr
MSStunAddress *new_peer = ms_malloc(sizeof(MSStunAddress));
memcpy(new_peer, peer_address, sizeof(MSStunAddress));
context->allowed_peer_addresses = ms_list_append(context->allowed_peer_addresses, new_peer);
context->stats.nb_successful_create_permission++;
}
}
......@@ -1416,7 +1419,7 @@ static int ms_turn_rtp_endpoint_recvfrom(RtpTransport *rtptp, mblk_t *msg, int f
uint16_t datasize = ntohs(*(((uint16_t *)msg->b_rptr) + 1));
if ((channel == ms_turn_context_get_channel_number(context)) && (msgsize >= (datasize + 4))) {
msg->b_rptr += 4; /* Unpack the TURN ChannelData message */
context->stats_nb_received_channel_msg++;
context->stats.nb_received_channel_msg++;
}
} else {
/* This is not a RTP packet and not a TURN ChannelData message, try to see if it is a STUN one */
......@@ -1454,7 +1457,7 @@ static int ms_turn_rtp_endpoint_recvfrom(RtpTransport *rtptp, mblk_t *msg, int f
}
/* Overwrite the source address of the packet so that it uses the peer address instead of the TURN server one */
ms_stun_address_to_sockaddr(stun_addr, from, fromlen);
if (msgsize > 0) context->stats_nb_data_indication++;
if (msgsize > 0) context->stats.nb_data_indication++;
}
}
}
......@@ -1505,7 +1508,7 @@ static int ms_turn_rtp_endpoint_sendto(RtpTransport *rtptp, mblk_t *msg, int fla
mblk_meta_copy(msg, new_msg);
concatb(new_msg, msg);
msg = new_msg;
context->stats_nb_sent_channel_msg++;
context->stats.nb_sent_channel_msg++;
} else {
/* Use a TURN send indication to encapsulate the data to be sent */
MSStunAddress stun_addr;
......@@ -1524,7 +1527,7 @@ static int ms_turn_rtp_endpoint_sendto(RtpTransport *rtptp, mblk_t *msg, int fla
len = ms_stun_message_encode(stun_msg, &buf);
msgappend(msg, buf, len, FALSE);
ms_free(buf);
context->stats_nb_send_indication++;
context->stats.nb_send_indication++;
}
to = (const struct sockaddr *)context->turn_server_addr;
tolen = context->turn_server_addrlen;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment