From 81094b3a0cde103b04153ab505b8a74074a4ed2a Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Wed, 13 May 2015 11:50:04 -0500 Subject: [PATCH] FS-7499 adding some more refactoring towards better rtcp --- src/include/switch_types.h | 3 +- src/include/switch_utils.h | 28 +++-- src/mod/codecs/mod_avcodec/mod_avcodec.c | 2 +- src/mod/codecs/mod_openh264/mod_openh264.cpp | 2 +- src/mod/codecs/mod_vpx/mod_vpx.c | 2 +- src/switch_core_media.c | 2 +- src/switch_rtp.c | 107 +++++++++++-------- 7 files changed, 88 insertions(+), 58 deletions(-) diff --git a/src/include/switch_types.h b/src/include/switch_types.h index 108b03e33c..df4e5a4198 100644 --- a/src/include/switch_types.h +++ b/src/include/switch_types.h @@ -229,7 +229,7 @@ SWITCH_BEGIN_EXTERN_C #define SWITCH_CORE_SESSION_MAX_PRIVATES 2 #define SWITCH_DEFAULT_VIDEO_SIZE 1200 #define SWITCH_RTCP_AUDIO_INTERVAL_MSEC "5000" -#define SWITCH_RTCP_VIDEO_INTERVAL_MSEC "1000" +#define SWITCH_RTCP_VIDEO_INTERVAL_MSEC "2000" /* Jitter */ #define JITTER_VARIANCE_THRESHOLD 400.0 @@ -661,6 +661,7 @@ typedef struct { uint16_t last_rpt_cycle; /* Packet loss calculation, sequence number cycle at the begining of the current RTCP report interval */ uint16_t period_pkt_count; /* Packet loss calculation, packet count received during this RTCP report interval */ uint16_t pkt_count; /* Packet loss calculation, packet count received during this session */ + uint16_t sent_pkt_count; uint32_t rtcp_rtp_count; /* RTCP report generated count */ uint32_t high_ext_seq_recv; /* Packet loss calculation, highest extended sequence number received and processed for stats */ uint16_t cycle; /* Packet loss calculation, sequence number cycle of the current RTCP report interval */ diff --git a/src/include/switch_utils.h b/src/include/switch_utils.h index 2c9e0a2aaa..5d85353669 100644 --- a/src/include/switch_utils.h +++ b/src/include/switch_utils.h @@ -40,6 +40,7 @@ #define SWITCH_UTILS_H #include +#include SWITCH_BEGIN_EXTERN_C @@ -993,18 +994,31 @@ SWITCH_DECLARE(char *) switch_util_quote_shell_arg_pool(const char *string, swit #define SWITCH_READ_ACCEPTABLE(status) (status == SWITCH_STATUS_SUCCESS || status == SWITCH_STATUS_BREAK || status == SWITCH_STATUS_INUSE) + +static inline int32_t switch_calc_bitrate(int w, int h, int quality, int fps) +{ + /* KUSH GAUGE*/ + + if (!fps) fps = 15; + if (!quality) quality = 2; + + return (int32_t)((float)(w * h * fps * quality) * 0.07) / 1000; +} + static inline int32_t switch_parse_bandwidth_string(const char *bwv) { - int32_t bw = 0; + float bw = 0; if (!bwv) return 0; - - if (bwv && (bw = (int32_t) atol(bwv))) { + + if (!strcasecmp(bwv, "auto")) { + return -1; + } + + if ((bw = (float) atof(bwv))) { if (bw < 0) return 0; - if (!strcasecmp(bwv, "auto")) { - return -1; - } else if (strstr(bwv, "KB")) { + if (strstr(bwv, "KB")) { bw *= 8; } else if (strstr(bwv, "mb")) { bw *= 1024; @@ -1013,7 +1027,7 @@ static inline int32_t switch_parse_bandwidth_string(const char *bwv) } } - return bw; + return (int32_t) roundf(bw); } static inline int switch_needs_url_encode(const char *s) diff --git a/src/mod/codecs/mod_avcodec/mod_avcodec.c b/src/mod/codecs/mod_avcodec/mod_avcodec.c index 334f43b9f4..ac6a370b0f 100644 --- a/src/mod/codecs/mod_avcodec/mod_avcodec.c +++ b/src/mod/codecs/mod_avcodec/mod_avcodec.c @@ -132,7 +132,7 @@ static switch_status_t init_x264(h264_codec_context_t *context, uint32_t width, if (context->codec_settings.video.bandwidth) { context->bandwidth = context->codec_settings.video.bandwidth; } else { - context->bandwidth = context->codec_settings.video.width * context->codec_settings.video.height / 1024; + context->bandwidth = switch_calc_bitrate(context->codec_settings.video.width, context->codec_settings.video.height, 0, 0); } if (context->bandwidth > 5120) { diff --git a/src/mod/codecs/mod_openh264/mod_openh264.cpp b/src/mod/codecs/mod_openh264/mod_openh264.cpp index 5467a798ea..e357e0a67d 100644 --- a/src/mod/codecs/mod_openh264/mod_openh264.cpp +++ b/src/mod/codecs/mod_openh264/mod_openh264.cpp @@ -95,7 +95,7 @@ int FillSpecificParameters(h264_codec_context_t *context) { if (context->codec_settings.video.bandwidth) { context->bandwidth = context->codec_settings.video.bandwidth; } else { - context->bandwidth = context->codec_settings.video.width * context->codec_settings.video.height / 1024; + context->bandwidth = switch_calc_bitrate(context->codec_settings.video.width, context->codec_settings.video.height, 0, 0); } if (context->bandwidth > 5120) { diff --git a/src/mod/codecs/mod_vpx/mod_vpx.c b/src/mod/codecs/mod_vpx/mod_vpx.c index 3e6deecdbf..f90f0abd21 100644 --- a/src/mod/codecs/mod_vpx/mod_vpx.c +++ b/src/mod/codecs/mod_vpx/mod_vpx.c @@ -312,7 +312,7 @@ static switch_status_t init_encoder(switch_codec_t *codec) if (context->codec_settings.video.bandwidth) { context->bandwidth = context->codec_settings.video.bandwidth; } else { - context->bandwidth = ((context->codec_settings.video.width * context->codec_settings.video.height) / 900); + context->bandwidth = switch_calc_bitrate(context->codec_settings.video.width, context->codec_settings.video.height, 0, 0); } if (context->bandwidth > 40960) { diff --git a/src/switch_core_media.c b/src/switch_core_media.c index e437a2347d..f9532e5e35 100644 --- a/src/switch_core_media.c +++ b/src/switch_core_media.c @@ -48,7 +48,7 @@ static void gen_ice(switch_core_session_t *session, switch_media_type_t type, co #define MAX_CODEC_CHECK_FRAMES 50//x:mod_sofia.h #define MAX_MISMATCH_FRAMES 5//x:mod_sofia.h #define type2str(type) type == SWITCH_MEDIA_TYPE_VIDEO ? "video" : "audio" -#define VIDEO_REFRESH_FREQ 100000 +#define VIDEO_REFRESH_FREQ 1000000 typedef enum { SMF_INIT = (1 << 0), diff --git a/src/switch_rtp.c b/src/switch_rtp.c index d8392ae980..596bc92b49 100644 --- a/src/switch_rtp.c +++ b/src/switch_rtp.c @@ -375,7 +375,6 @@ struct switch_rtp { uint32_t samples_per_interval; uint32_t samples_per_second; uint32_t conf_samples_per_interval; - uint16_t rtcp_send_rate; switch_time_t rtcp_last_sent; uint32_t rsamples_per_interval; uint32_t ms_per_packet; @@ -419,7 +418,7 @@ struct switch_rtp { uint32_t hot_hits; uint32_t sync_packets; int rtcp_interval; - switch_time_t next_rtcp_send; + int rtcp_sent_packets; switch_bool_t rtcp_fresh_frame; switch_time_t send_time; @@ -1757,6 +1756,7 @@ static void rtcp_stats_init(switch_rtp_t *rtp_session) stats->bad_seq = (1<<16) + 1; /* Make sure we wont missmatch 2 consecutive packets, so seq == bad_seq is false */ stats->cum_lost = 0; stats->period_pkt_count = 0; + stats->sent_pkt_count = 0; stats->pkt_count = 0; stats->rtcp_rtp_count = 0; @@ -1847,7 +1847,7 @@ static void calc_bw_exp(uint32_t bps, uint8_t bits, rtcp_tmmbx_t *tmmbx) uint32_t mantissa_max, i = 0; uint8_t exp = 0; uint32_t mantissa = 0; - uint16_t overhead = 0; + uint16_t overhead = 60; mantissa_max = (1 << bits) - 1; @@ -1866,12 +1866,21 @@ static void calc_bw_exp(uint32_t bps, uint8_t bits, rtcp_tmmbx_t *tmmbx) tmmbx->parts[3] = (uint8_t) (overhead); } +static int using_ice(switch_rtp_t *rtp_session) +{ + if (rtp_session->ice.ice_user || rtp_session->rtcp_ice.ice_user) { + return 1; + } + + return 0; +} static int check_rtcp_and_ice(switch_rtp_t *rtp_session) { int ret = 0; - int rtcp_ok = 1; + int rtcp_ok = 0; switch_time_t now = switch_micro_time_now(); + int rate = 0; if (rtp_session->flags[SWITCH_RTP_FLAG_AUTO_CNG] && rtp_session->send_msg.header.ts && rtp_session->cng_pt && rtp_session->timer.samplecount >= (rtp_session->last_write_samplecount + (rtp_session->samples_per_interval * 60))) { @@ -1890,24 +1899,37 @@ static int check_rtcp_and_ice(switch_rtp_t *rtp_session) } } - if (rtp_session->rtcp_interval && rtp_session->next_rtcp_send > now) { - rtcp_ok = 0; - } else { - rtp_session->next_rtcp_send = now + (rtp_session->rtcp_interval * 1000); - } + rate = rtp_session->rtcp_interval; - if (rtcp_ok && rtp_session->rtcp_ice.ice_user && !rtp_session->rtcp_ice.rready) { - rtcp_ok = 0; + if (rtp_session->rtcp_sent_packets < 4) { + rate = 4000; + } else { + if (rtp_session->pli_count || rtp_session->fir_count || rtp_session->cur_nack || rtp_session->tmmbr || rtp_session->tmmbn) { + rtcp_ok = 1; + } + } + + if (!rtcp_ok && (int)((now - rtp_session->rtcp_last_sent) / 1000) > rate) { + rtcp_ok = 1; + } + + if (rtcp_ok && using_ice(rtp_session)) { + if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) { + if (!rtp_session->ice.rready) { + rtcp_ok = 0; + } + } else { + if (!rtp_session->rtcp_ice.rready) { + rtcp_ok = 0; + } + } } if (rtp_session->flags[SWITCH_RTP_FLAG_NACK] && rtp_session->vb) { rtp_session->cur_nack = switch_vb_pop_nack(rtp_session->vb); } - if (rtp_session->rtcp_sock_output && rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] && //rtp_session->remote_ssrc && - !rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU] && - ((now - rtp_session->rtcp_last_sent) > rtp_session->rtcp_send_rate * 1000000 || - rtp_session->pli_count || rtp_session->fir_count || rtp_session->cur_nack || rtp_session->tmmbr || rtp_session->tmmbn)) { + if (rtp_session->rtcp_sock_output && rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP] && !rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU] && rtcp_ok) { switch_rtcp_numbers_t * stats = &rtp_session->stats.rtcp; struct switch_rtcp_receiver_report *rr; struct switch_rtcp_sender_report *sr; @@ -1923,8 +1945,9 @@ static int check_rtcp_and_ice(switch_rtp_t *rtp_session) rtp_session->rtcp_send_msg.header.version = 2; rtp_session->rtcp_send_msg.header.p = 0; rtp_session->rtcp_send_msg.header.count = 1; - - if (!rtp_session->stats.outbound.packet_count) { + rtp_session->rtcp_sent_packets++; + + if (!rtp_session->stats.rtcp.sent_pkt_count) { rtp_session->rtcp_send_msg.header.type = RTCP_PT_RR; /* Receiver report */ rr=(struct switch_rtcp_receiver_report*) rtp_session->rtcp_send_msg.body; rr->ssrc = htonl(rtp_session->ssrc); @@ -1972,7 +1995,7 @@ static int check_rtcp_and_ice(switch_rtp_t *rtp_session) ext_hdr->length = htons((uint8_t)(sizeof(switch_rtcp_ext_hdr_t) / 4) - 1); rtcp_bytes += sizeof(switch_rtcp_ext_hdr_t); - rtp_session->pli_count--; + rtp_session->pli_count = 0; } if (rtp_session->flags[SWITCH_RTP_FLAG_NACK] && rtp_session->cur_nack) { @@ -2028,7 +2051,7 @@ static int check_rtcp_and_ice(switch_rtp_t *rtp_session) ext_hdr->length = htons((uint8_t)((sizeof(switch_rtcp_ext_hdr_t) + sizeof(rtcp_fir_t)) / 4) - 1); rtcp_bytes += sizeof(switch_rtcp_ext_hdr_t) + sizeof(rtcp_fir_t); - rtp_session->fir_count--; + rtp_session->fir_count = 0; } //if (!rtp_session->tmmbr && rtp_session->cur_tmmbr) { @@ -2112,7 +2135,7 @@ static int check_rtcp_and_ice(switch_rtp_t *rtp_session) stats->last_rpt_ext_seq = stats->high_ext_seq_recv; stats->last_rpt_ts = rtp_session->timer.samplecount; stats->period_pkt_count = 0; - + stats->sent_pkt_count = 0; #ifdef ENABLE_SRTP @@ -4053,8 +4076,6 @@ SWITCH_DECLARE(switch_status_t) switch_rtp_activate_rtcp(switch_rtp_t *rtp_sessi switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG, "RTCP send rate is: %d and packet rate is: %d Remote Port: %d\n", send_rate, rtp_session->ms_per_packet, rtp_session->remote_rtcp_port); rtp_session->rtcp_interval = send_rate; - rtp_session->rtcp_send_rate = (uint16_t)(send_rate/1000); - rtp_session->next_rtcp_send = switch_time_now() + (rtp_session->rtcp_interval * 1000); } if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_MUX]) { @@ -4211,7 +4232,7 @@ SWITCH_DECLARE(void) switch_rtp_video_refresh(switch_rtp_t *rtp_session) } if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && (rtp_session->ice.ice_user || rtp_session->flags[SWITCH_RTP_FLAG_FIR])) { - rtp_session->fir_count++; + rtp_session->fir_count = 1; } } @@ -4222,7 +4243,7 @@ SWITCH_DECLARE(void) switch_rtp_video_loss(switch_rtp_t *rtp_session) } if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && (rtp_session->ice.ice_user || rtp_session->flags[SWITCH_RTP_FLAG_PLI])) { - rtp_session->pli_count++; + rtp_session->pli_count = 1; } } @@ -4956,6 +4977,7 @@ static switch_status_t read_rtp_packet(switch_rtp_t *rtp_session, switch_size_t rtp_session->missed_count = 0; if (rtp_session->recv_msg.header.version == 2) { switch_cp_addr(rtp_session->rtp_from_addr, rtp_session->from_addr); + rtp_session->last_rtp_hdr = rtp_session->recv_msg.header; } } @@ -5300,6 +5322,14 @@ static switch_status_t read_rtp_packet(switch_rtp_t *rtp_session, switch_size_t } #endif } + + + if (rtp_session->recv_msg.header.version == 2) { + if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) { + rtcp_stats(rtp_session); + } + } + } if ((rtp_session->recv_te && rtp_session->recv_msg.header.pt == rtp_session->recv_te) || @@ -5362,10 +5392,6 @@ static switch_status_t read_rtp_packet(switch_rtp_t *rtp_session, switch_size_t } } - - if (*bytes) { - rtp_session->last_rtp_hdr = rtp_session->recv_msg.header; - } } if (!*bytes || rtp_session->recv_msg.header.version == 2) { @@ -5425,10 +5451,6 @@ static switch_status_t read_rtp_packet(switch_rtp_t *rtp_session, switch_size_t } } - if (status == SWITCH_STATUS_SUCCESS && *bytes && rtp_session->last_rtp_hdr.version == 2) { - rtcp_stats(rtp_session); - } - /* recalculate body length in case rtp extension used */ if (!rtp_session->flags[SWITCH_RTP_FLAG_PROXY_MEDIA] && !rtp_session->flags[SWITCH_RTP_FLAG_UDPTL] && rtp_session->recv_msg.header.version == 2 && rtp_session->recv_msg.header.x) { /* header extensions */ @@ -5530,11 +5552,9 @@ static switch_status_t process_rtcp_report(switch_rtp_t *rtp_session, rtcp_msg_t if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] && (msg->header.type == RTCP_PT_RTPFB || msg->header.type == RTCP_PT_PSFB)) { rtcp_ext_msg_t *extp = (rtcp_ext_msg_t *) msg; - if (extp->header.fmt != 15) { /* https://code.google.com/p/webrtc/issues/detail?id=4626 */ - switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "PICKED UP XRTCP type: %d fmt: %d\n", - msg->header.type, extp->header.fmt); - } - + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "PICKED UP XRTCP type: %d fmt: %d\n", + msg->header.type, extp->header.fmt); + if (msg->header.type == RTCP_PT_PSFB && (extp->header.fmt == RTCP_PSFB_FIR || extp->header.fmt == RTCP_PSFB_PLI)) { switch_core_media_gen_key_frame(rtp_session->session); if (rtp_session->vbw) { @@ -5789,15 +5809,6 @@ static switch_status_t read_rtcp_packet(switch_rtp_t *rtp_session, switch_size_t return status; } -static int using_ice(switch_rtp_t *rtp_session) -{ - if (rtp_session->ice.ice_user || rtp_session->rtcp_ice.ice_user) { - return 1; - } - - return 0; -} - static int rtp_common_read(switch_rtp_t *rtp_session, switch_payload_t *payload_type, payload_map_t **pmapP, switch_frame_flag_t *flags, switch_io_flag_t io_flags) { @@ -7271,6 +7282,10 @@ static int rtp_common_write(switch_rtp_t *rtp_session, rtp_session->stats.outbound.raw_bytes += bytes; rtp_session->stats.outbound.packet_count++; + + if (rtp_session->flags[SWITCH_RTP_FLAG_ENABLE_RTCP]) { + rtp_session->stats.rtcp.sent_pkt_count++; + } if (rtp_session->cng_pt && send_msg->header.pt == rtp_session->cng_pt) { rtp_session->stats.outbound.cng_packet_count++;