From 79980f07677da8a8e73fe6fa54c240c0b40a0949 Mon Sep 17 00:00:00 2001 From: Dragos Oancea Date: Fri, 1 Oct 2021 18:17:51 +0000 Subject: [PATCH] [unit-tests] add SEND_RTCP_MESSAGE event/rtcp unit-test. --- tests/unit/conf/freeswitch.xml | 18 ++++ tests/unit/switch_rtp.c | 178 ++++++++++++++++++++++++++++++++- 2 files changed, 195 insertions(+), 1 deletion(-) diff --git a/tests/unit/conf/freeswitch.xml b/tests/unit/conf/freeswitch.xml index 3dcb33198b..99609824b5 100644 --- a/tests/unit/conf/freeswitch.xml +++ b/tests/unit/conf/freeswitch.xml @@ -5,6 +5,7 @@ +
@@ -23,6 +24,23 @@ + + + + + + + + + + + + + + + + + diff --git a/tests/unit/switch_rtp.c b/tests/unit/switch_rtp.c index d411fe20fc..35de9d8630 100644 --- a/tests/unit/switch_rtp.c +++ b/tests/unit/switch_rtp.c @@ -3,7 +3,7 @@ #include static const char *rx_host = "127.0.0.1"; -static switch_port_t rx_port = 12346; +static switch_port_t rx_port = 1234; static const char *tx_host = "127.0.0.1"; static switch_port_t tx_port = 54320; static switch_memory_pool_t *pool = NULL; @@ -15,6 +15,28 @@ switch_rtp_packet_t rtp_packet; switch_frame_flag_t *frame_flags; switch_io_flag_t io_flags; switch_payload_t read_pt; +int send_rtcp_test_success = 0; + +static void show_event(switch_event_t *event) { + char *str; + /*print the event*/ + switch_event_serialize_json(event, &str); + if (str) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s\n", str); + switch_safe_free(str); + } +} + +static void send_rtcp_event_handler(switch_event_t *event) +{ + const char *new_ev = switch_event_get_header(event, "Event-Name"); + + if (new_ev && !strcmp(new_ev, "SEND_RTCP_MESSAGE")) { + send_rtcp_test_success = 1; + } + + show_event(event); +} FST_CORE_BEGIN("./conf") { @@ -97,6 +119,160 @@ FST_TEARDOWN_END() switch_core_destroy_memory_pool(&pool); } FST_TEST_END() + FST_TEST_BEGIN(test_send_rtcp_event_audio) + { + switch_core_session_t *session = NULL; + switch_channel_t *channel = NULL; + switch_status_t status; + switch_call_cause_t cause; + switch_stream_handle_t stream = { 0 }; + const unsigned char packet[]="\x80\x00\xcd\x15\xfd\x86\x00\x00\x61\x5a\xe1\x37"; + uint32_t plen = 12; + char rpacket[SWITCH_RECOMMENDED_BUFFER_SIZE]; + switch_payload_t pt = { 0 }; + switch_frame_flag_t frameflags = { 0 }; + static switch_port_t audio_rx_port = 1234; + switch_media_handle_t *media_handle; + switch_core_media_params_t *mparams; + char *r_sdp; + uint8_t match = 0, p = 0; + struct sockaddr_in sin; + socklen_t len = sizeof(sin); + int x; + struct sockaddr_in servaddr_rtp; + int sockfd_rtp; + struct hostent *server; + int ret; + switch_frame_t *read_frame, *write_frame; + + switch_event_bind("", SWITCH_EVENT_ALL, SWITCH_EVENT_SUBCLASS_ANY, send_rtcp_event_handler, NULL); + + status = switch_ivr_originate(NULL, &session, &cause, "null/+15553334444", 2, NULL, NULL, NULL, NULL, NULL, SOF_NONE, NULL, NULL); + fst_requires(session); + fst_check(status == SWITCH_STATUS_SUCCESS); + + channel = switch_core_session_get_channel(session); + fst_requires(channel); + mparams = switch_core_session_alloc(session, sizeof(switch_core_media_params_t)); + mparams->num_codecs = 1; + mparams->inbound_codec_string = switch_core_session_strdup(session, "PCMU"); + mparams->outbound_codec_string = switch_core_session_strdup(session, "PCMU"); + mparams->rtpip = switch_core_session_strdup(session, (char *)rx_host); + + status = switch_media_handle_create(&media_handle, session, mparams); + fst_requires(status == SWITCH_STATUS_SUCCESS); + + switch_channel_set_variable(channel, "absolute_codec_string", "PCMU"); + switch_channel_set_variable(channel, "fire_rtcp_events", "true"); + switch_channel_set_variable(channel, "send_silence_when_idle", "-1"); + + switch_channel_set_variable(channel, SWITCH_LOCAL_MEDIA_IP_VARIABLE, rx_host); + switch_channel_set_variable_printf(channel, SWITCH_LOCAL_MEDIA_PORT_VARIABLE, "%d", audio_rx_port); + + r_sdp = switch_core_session_sprintf(session, + "v=0\n" + "o=FreeSWITCH 1632033305 1632033306 IN IP4 %s\n" + "s=-\n" + "c=IN IP4 %s\n" + "t=0 0\n" + "m=audio 11114 RTP/AVP 0 101\n" + "a=rtpmap:0 PCMU/8000\n" + "a=rtpmap:101 telephone-event/8000\n" + "a=rtcp:11115\n", + tx_host, tx_host); + + switch_core_media_prepare_codecs(session, SWITCH_FALSE); + + match = switch_core_media_negotiate_sdp(session, r_sdp, &p, SDP_TYPE_REQUEST); + fst_requires(match == 1); + + status = switch_core_media_choose_ports(session, SWITCH_TRUE, SWITCH_FALSE); + fst_requires(status == SWITCH_STATUS_SUCCESS); + + status = switch_core_media_activate_rtp(session); + fst_requires(status == SWITCH_STATUS_SUCCESS); + + switch_core_media_set_rtp_flag(session, SWITCH_MEDIA_TYPE_AUDIO, SWITCH_RTP_FLAG_DEBUG_RTP_READ); + switch_core_media_set_rtp_flag(session, SWITCH_MEDIA_TYPE_AUDIO, SWITCH_RTP_FLAG_DEBUG_RTP_WRITE); + switch_core_media_set_rtp_flag(session, SWITCH_MEDIA_TYPE_AUDIO, SWITCH_RTP_FLAG_AUDIO_FIRE_SEND_RTCP_EVENT); + switch_core_media_set_rtp_flag(session, SWITCH_MEDIA_TYPE_AUDIO, SWITCH_RTP_FLAG_ENABLE_RTCP); + + + switch_frame_alloc(&write_frame, SWITCH_RECOMMENDED_BUFFER_SIZE); + write_frame->codec = switch_core_session_get_write_codec(session); + + SWITCH_STANDARD_STREAM(stream); + switch_api_execute("fsctl", "debug_level 9", session, &stream); + switch_safe_free(stream.data); + + if ((sockfd_rtp = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + perror("socket creation failed"); + fst_requires(0); /*exit*/ + } + + memset(&servaddr_rtp, 0, sizeof(servaddr_rtp)); + + servaddr_rtp.sin_family = AF_INET; + servaddr_rtp.sin_port = htons(audio_rx_port); + server = gethostbyname(rx_host); + bcopy((char *)server->h_addr, (char *)&servaddr_rtp.sin_addr.s_addr, server->h_length); + + /*get local UDP port (tx side) to trick FS into accepting our packets*/ + ret = sendto(sockfd_rtp, NULL, 0, MSG_CONFIRM, (const struct sockaddr *) &servaddr_rtp, sizeof(servaddr_rtp)); + if (ret < 0){ + perror("sendto"); + fst_requires(0); + } + + rtp_session = switch_core_media_get_rtp_session(session, SWITCH_MEDIA_TYPE_AUDIO); + len = sizeof(sin); + if (getsockname(sockfd_rtp, (struct sockaddr *)&sin, &len) == -1) { + perror("getsockname"); + fst_requires(0); + } else { + switch_rtp_set_remote_address(rtp_session, tx_host, ntohs(sin.sin_port), 0, SWITCH_FALSE, &err); + switch_rtp_reset(rtp_session); + } + + write_frame->datalen = plen; + memcpy(write_frame->data, &packet, plen); + + switch_rtp_clear_flag(rtp_session, SWITCH_RTP_FLAG_PAUSE); + + for (x = 0; x < 3; x++) { + + switch_rtp_write_frame(rtp_session, write_frame); /* rtp_session->stats.rtcp.sent_pkt_count++; */ + + switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Sent RTP. Packet size = [%u]\n", plen); + ret = sendto(sockfd_rtp, (const char *) &packet, plen, MSG_CONFIRM, (const struct sockaddr *) &servaddr_rtp, sizeof(servaddr_rtp)); + if (ret < 0){ + perror("sendto"); + fst_requires(0); + } + + status = switch_rtp_read(rtp_session, (void *)&rpacket, &plen, &pt, &frameflags, io_flags); + fst_requires(status == SWITCH_STATUS_SUCCESS); + plen = 12; + if (pt == SWITCH_RTP_CNG_PAYLOAD /*timeout*/) continue; + + status = switch_core_session_read_frame(session, &read_frame, frameflags, 0); + fst_requires(status == SWITCH_STATUS_SUCCESS); + } + switch_sleep(3000 * 1000); + + fst_requires(send_rtcp_test_success); + switch_channel_hangup(channel, SWITCH_CAUSE_NORMAL_CLEARING); + + if (write_frame) switch_frame_free(&write_frame); + + switch_rtp_destroy(&rtp_session); + + switch_media_handle_destroy(session); + + switch_core_session_rwunlock(session); + } + FST_TEST_END() + } FST_SUITE_END() }