From d24280c700fc8201211e27693741a2d4cf7f417d Mon Sep 17 00:00:00 2001 From: Noah Mehl Date: Thu, 18 Jun 2020 09:53:49 -0400 Subject: [PATCH] [core] Add SEND_RTCP_MESSAGE We utilize: https://github.com/sipcapture/hepipe.js to monitor RTCP for our calls. As such, it's super helpful to get the sent RTCP data along with the received RTCP data. The new: SEND_RTCP_MESSAGE basically mirrors the existing RECV_RTCP_MESSAGE. --- libs/esl/src/esl_event.c | 1 + libs/esl/src/include/esl_event.h | 1 + src/include/switch_types.h | 3 ++ src/switch_core_media.c | 4 ++ src/switch_event.c | 1 + src/switch_rtp.c | 91 ++++++++++++++++++++++++++++++-- 6 files changed, 98 insertions(+), 3 deletions(-) diff --git a/libs/esl/src/esl_event.c b/libs/esl/src/esl_event.c index 68e0688114..8a88395ec2 100644 --- a/libs/esl/src/esl_event.c +++ b/libs/esl/src/esl_event.c @@ -132,6 +132,7 @@ static const char *EVENT_NAMES[] = { "SEND_INFO", "RECV_INFO", "RECV_RTCP_MESSAGE", + "SEND_RTCP_MESSAGE", "CALL_SECURE", "NAT", "RECORD_START", diff --git a/libs/esl/src/include/esl_event.h b/libs/esl/src/include/esl_event.h index de070ad723..01e7ada07e 100644 --- a/libs/esl/src/include/esl_event.h +++ b/libs/esl/src/include/esl_event.h @@ -120,6 +120,7 @@ typedef enum { ESL_EVENT_SEND_INFO, ESL_EVENT_RECV_INFO, ESL_EVENT_RECV_RTCP_MESSAGE, + ESL_EVENT_SEND_RTCP_MESSAGE, ESL_EVENT_CALL_SECURE, ESL_EVENT_NAT, ESL_EVENT_RECORD_START, diff --git a/src/include/switch_types.h b/src/include/switch_types.h index fafc508823..958f2b136a 100644 --- a/src/include/switch_types.h +++ b/src/include/switch_types.h @@ -843,6 +843,7 @@ typedef enum { SWITCH_RTP_FLAG_PASSTHRU, SWITCH_RTP_FLAG_SECURE_SEND_MKI, SWITCH_RTP_FLAG_SECURE_RECV_MKI, + SWITCH_RTP_FLAG_VIDEO_FIRE_SEND_RTCP_EVENT, SWITCH_RTP_FLAG_INVALID } switch_rtp_flag_t; @@ -2037,6 +2038,7 @@ typedef uint32_t switch_io_flag_t; SWITCH_EVENT_SEND_INFO SWITCH_EVENT_RECV_INFO SWITCH_EVENT_RECV_RTCP_MESSAGE + SWITCH_EVENT_SEND_RTCP_MESSAGE SWITCH_EVENT_CALL_SECURE SWITCH_EVENT_NAT - NAT Management (new/del/status) SWITCH_EVENT_RECORD_START @@ -2132,6 +2134,7 @@ typedef enum { SWITCH_EVENT_SEND_INFO, SWITCH_EVENT_RECV_INFO, SWITCH_EVENT_RECV_RTCP_MESSAGE, + SWITCH_EVENT_SEND_RTCP_MESSAGE, SWITCH_EVENT_CALL_SECURE, SWITCH_EVENT_NAT, SWITCH_EVENT_RECORD_START, diff --git a/src/switch_core_media.c b/src/switch_core_media.c index bfacc704df..57273d96f1 100644 --- a/src/switch_core_media.c +++ b/src/switch_core_media.c @@ -9510,6 +9510,10 @@ SWITCH_DECLARE(switch_status_t) switch_core_media_activate_rtp(switch_core_sessi if (v_engine->tmmbr) { flags[SWITCH_RTP_FLAG_TMMBR]++; } + + if (switch_channel_var_true(session->channel, "enable_send_rtcp_message_event_video")) { + flags[SWITCH_RTP_FLAG_VIDEO_FIRE_SEND_RTCP_EVENT] = 1; + } v_engine->rtp_session = switch_rtp_new(a_engine->local_sdp_ip, v_engine->local_sdp_port, diff --git a/src/switch_event.c b/src/switch_event.c index 709ea95e7e..0a0e4b3c6c 100644 --- a/src/switch_event.c +++ b/src/switch_event.c @@ -207,6 +207,7 @@ static char *EVENT_NAMES[] = { "SEND_INFO", "RECV_INFO", "RECV_RTCP_MESSAGE", + "SEND_RTCP_MESSAGE", "CALL_SECURE", "NAT", "RECORD_START", diff --git a/src/switch_rtp.c b/src/switch_rtp.c index d3f43faef1..59882a122a 100644 --- a/src/switch_rtp.c +++ b/src/switch_rtp.c @@ -26,6 +26,7 @@ * Anthony Minessale II * Marcel Barbulescu * Seven Du + * Noah Mehl - Open Telecom Foundation * * switch_rtp.c -- RTP * @@ -1816,10 +1817,10 @@ static void rtcp_generate_sender_info(switch_rtp_t *rtp_session, struct switch_r sr->oc = htonl(rtp_session->stats.outbound.raw_bytes - rtp_session->stats.outbound.packet_count * sizeof(srtp_hdr_t)); switch_time_exp_gmt(&now_hr,now); - switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG10,"Sending an RTCP packet[%04d-%02d-%02d %02d:%02d:%02d.%d] lsr[%u] msw[%u] lsw[%u] stats_ssrc[%u]\n", + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG10,"Sending an RTCP packet[%04d-%02d-%02d %02d:%02d:%02d.%d] lsr[%u] msw[%u] lsw[%u] stats_ssrc[%u] packet_count[%u] OC[%u]\n", 1900 + now_hr.tm_year, now_hr.tm_mday, now_hr.tm_mon, now_hr.tm_hour, now_hr.tm_min, now_hr.tm_sec, now_hr.tm_usec, (ntohl(sr->ntp_lsw)&0xffff0000)>>16 | (ntohl(sr->ntp_msw)&0x0000ffff)<<16, - ntohl(sr->ntp_msw),ntohl(sr->ntp_lsw), rtp_session->stats.rtcp.ssrc + ntohl(sr->ntp_msw),ntohl(sr->ntp_lsw), rtp_session->stats.rtcp.ssrc, ntohl(sr->pc), ntohl(sr->oc) ); } @@ -1895,7 +1896,13 @@ static void rtcp_generate_report_block(switch_rtp_t *rtp_session, struct switch_ } rtcp_report_block->lsr = stats->last_recv_lsr_peer; rtcp_report_block->dlsr = htonl(dlsr); - rtcp_report_block->ssrc = htonl(rtp_session->stats.rtcp.peer_ssrc); + if (rtp_session->stats.rtcp.peer_ssrc) { + rtcp_report_block->ssrc = htonl(rtp_session->stats.rtcp.peer_ssrc); + } else { + /* if remote is not sending rtcp reports, take ssrc as assigned from rtp */ + rtcp_report_block->ssrc = htonl(rtp_session->remote_ssrc); + } + stats->rtcp_rtp_count++; } @@ -2057,6 +2064,79 @@ static int using_ice(switch_rtp_t *rtp_session) return 0; } +static void switch_send_rtcp_event(switch_rtp_t *rtp_session ,struct switch_rtcp_sender_report *sr,struct switch_rtcp_report_block *rtcp_report_block) +{ + if (sr && rtcp_report_block) { + switch_event_t *event; + + if (switch_event_create(&event, SWITCH_EVENT_SEND_RTCP_MESSAGE) == SWITCH_STATUS_SUCCESS) { + char value[30]; + char header[50]; + uint32_t tmpLost; + char *uuid = switch_core_session_get_uuid(rtp_session->session); + if (uuid) { + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Unique-ID", switch_core_session_get_uuid(rtp_session->session)); + } + + snprintf(value, sizeof(value), "%.8x", rtp_session->stats.rtcp.ssrc); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "SSRC", value); + + snprintf(value, sizeof(value), "%u", ntohl(sr->sender_info.ntp_msw)); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "NTP-Most-Significant-Word", value); + + snprintf(value, sizeof(value), "%u", ntohl(sr->sender_info.ntp_lsw)); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "NTP-Least-Significant-Word", value); + + snprintf(value, sizeof(value), "%u", ntohl(sr->sender_info.ts)); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "RTP-Timestamp", value); + + snprintf(value, sizeof(value), "%u", ntohl(sr->sender_info.pc)); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Sender-Packet-Count", value); + + snprintf(value, sizeof(value), "%u", ntohl(sr->sender_info.oc)); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Octect-Packet-Count", value); + + snprintf(value, sizeof(value), "%u", ntohl(sr->sender_info.ts)); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Last-RTP-Timestamp", value); + + snprintf(value, sizeof(value), "%" SWITCH_TIME_T_FMT, switch_time_now()); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Capture-Time", value); + + /* Add sources info */ + snprintf(header, sizeof(header), "Source-SSRC"); + snprintf(value, sizeof(value), "%.8x", rtp_session->stats.rtcp.peer_ssrc); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value); + snprintf(header, sizeof(header), "Source-Fraction"); + snprintf(value, sizeof(value), "%u", (uint8_t)rtcp_report_block->fraction); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value); + snprintf(header, sizeof(header), "Source-Lost"); +#if SWITCH_BYTE_ORDER == __BIG_ENDIAN + tmpLost = report->lost; /* signed 24bit will extended signess to int32_t automatically */ +#else + tmpLost = ntohl(rtcp_report_block->lost)>>8; + tmpLost = tmpLost | ((tmpLost & 0x00800000) ? 0xff000000 : 0x00000000); /* ...and signess compensation */ +#endif + snprintf(value, sizeof(value), "%u", tmpLost); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value); + snprintf(header, sizeof(header), "Source-Highest-Sequence-Number-Received"); + snprintf(value, sizeof(value), "%u", ntohl(rtcp_report_block->highest_sequence_number_received)); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value); + snprintf(header, sizeof(header), "Source-Jitter"); + snprintf(value, sizeof(value), "%u", ntohl(rtcp_report_block->jitter)); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value); + snprintf(header, sizeof(header), "Source-LSR"); + snprintf(value, sizeof(value), "%u", ntohl(rtcp_report_block->lsr)); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value); + snprintf(header, sizeof(header), "Source-DLSR"); + snprintf(value, sizeof(value), "%u", ntohl(rtcp_report_block->dlsr)); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header, value); + + switch_event_fire(&event); + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG10, "Dispatched RTCP SEND event\n"); + } + } +} + #define MAX_NACK 10 static int check_rtcp_and_ice(switch_rtp_t *rtp_session) { @@ -2206,6 +2286,11 @@ static int check_rtcp_and_ice(switch_rtp_t *rtp_session) rtcp_generate_report_block(rtp_session, rtcp_report_block, nack_dup); rtp_session->rtcp_send_msg.header.count = 1; /* reception report block count */ stats->sent_pkt_count = 0; + /* send event for audio, or video if flag set */ + if (rtp_session->flags[SWITCH_RTP_FLAG_VIDEO_FIRE_SEND_RTCP_EVENT] || + !(rtp_session->flags[SWITCH_RTP_FLAG_VIDEO] || rtp_session->flags[SWITCH_RTP_FLAG_TEXT])) { + switch_send_rtcp_event(rtp_session, sr, rtcp_report_block); + } } switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(rtp_session->session), SWITCH_LOG_DEBUG1, "Sending RTCP SR (ssrc=%u)\n", rtp_session->ssrc); }