diff --git a/src/include/private/switch_core_pvt.h b/src/include/private/switch_core_pvt.h index 49d21a6d64..52817475b0 100644 --- a/src/include/private/switch_core_pvt.h +++ b/src/include/private/switch_core_pvt.h @@ -146,6 +146,8 @@ struct switch_core_session { uint8_t raw_read_buf[SWITCH_RECOMMENDED_BUFFER_SIZE]; uint8_t enc_read_buf[SWITCH_RECOMMENDED_BUFFER_SIZE]; switch_codec_t bug_codec; + uint32_t read_frame_count; + uint32_t track_duration; }; struct switch_media_bug { diff --git a/src/include/switch_core.h b/src/include/switch_core.h index a40fec8cbd..f279a73a53 100644 --- a/src/include/switch_core.h +++ b/src/include/switch_core.h @@ -119,6 +119,9 @@ struct switch_core_port_allocator; ///\{ +SWITCH_DECLARE(void) switch_core_session_enable_heartbeat(switch_core_session_t *session, uint32_t seconds); +SWITCH_DECLARE(void) switch_core_session_disable_heartbeat(switch_core_session_t *session); + /*! \brief Add a media bug to the session \param session the session to add the bug to diff --git a/src/include/switch_types.h b/src/include/switch_types.h index 761a027e1a..a9b283f4a8 100644 --- a/src/include/switch_types.h +++ b/src/include/switch_types.h @@ -110,6 +110,7 @@ SWITCH_BEGIN_EXTERN_C #define SWITCH_PATH_SEPARATOR "/" #endif #define SWITCH_URL_SEPARATOR "://" +#define SWITCH_ENABLE_HEARTBEAT_EVENTS_VARIABLE "enable_heartbeat_events" #define SWITCH_BYPASS_MEDIA_AFTER_BRIDGE_VARIABLE "bypass_media_after_bridge" #define SWITCH_READ_RESULT_VARIABLE "read_result" #define SWITCH_COPY_XML_CDR_VARIABLE "copy_xml_cdr" @@ -1133,6 +1134,7 @@ typedef enum { SWITCH_EVENT_CHANNEL_DATA, SWITCH_EVENT_GENERAL, SWITCH_EVENT_COMMAND, + SWITCH_EVENT_SESSION_HEARTBEAT, SWITCH_EVENT_ALL } switch_event_types_t; diff --git a/src/switch_channel.c b/src/switch_channel.c index fa6b2a0c03..42ae7864ba 100644 --- a/src/switch_channel.c +++ b/src/switch_channel.c @@ -1627,6 +1627,24 @@ SWITCH_DECLARE(switch_status_t) switch_channel_perform_mark_answered(switch_chan switch_core_session_rwunlock(other_session); } + if ((var = switch_channel_get_variable(channel, SWITCH_ENABLE_HEARTBEAT_EVENTS_VARIABLE))) { + uint32_t seconds = 60; + int tmp; + + if (switch_is_number(var)) { + tmp = atoi(var); + if (tmp > 10) { + seconds = tmp; + } + } else if (!switch_true(var)) { + seconds = 0; + } + + if (seconds) { + switch_core_session_enable_heartbeat(channel->session, seconds); + } + } + switch_channel_set_variable(channel, "endpoint_disposition", "ANSWER"); switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, func, line, NULL, SWITCH_LOG_NOTICE, "Channel [%s] has been answered\n", channel->name); if ((var = switch_channel_get_variable(channel, SWITCH_CHANNEL_EXECUTE_ON_ANSWER_VARIABLE)) && !switch_strlen_zero(var)) { diff --git a/src/switch_core_io.c b/src/switch_core_io.c index 459131f691..25733f21e5 100644 --- a/src/switch_core_io.c +++ b/src/switch_core_io.c @@ -106,6 +106,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi switch_status_t status; int need_codec, perfect, do_bugs = 0, do_resample = 0, is_cng = 0; unsigned int flag = 0; + switch_event_header_t *hi; switch_assert(session != NULL); @@ -122,6 +123,41 @@ SWITCH_DECLARE(switch_status_t) switch_core_session_read_frame(switch_core_sessi *frame = NULL; + if (session->read_codec && session->track_duration) { + if (session->read_frame_count == 0) { + switch_event_t *event; + session->read_frame_count = (session->read_codec->implementation->samples_per_second / + session->read_codec->implementation->samples_per_frame) * session->track_duration; + + switch_event_create(&event, SWITCH_EVENT_SESSION_HEARTBEAT); + switch_channel_event_set_data(session->channel, event); + if (!switch_channel_test_flag(session->channel, CF_VERBOSE_EVENTS)) { + if ((hi = switch_channel_variable_first(session->channel))) { + for (; hi; hi = hi->next) { + char buf[1024] = ""; + char *vvar = NULL, *vval = NULL; + + if (strncasecmp(hi->name, "hb_", 3)) { + continue; + } + + vvar = (char *) hi->name; + vval = (char *) hi->value; + + switch_assert(vvar && vval); + switch_snprintf(buf, sizeof(buf), "variable_%s", vvar); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, buf, vval); + } + switch_channel_variable_last(session->channel); + } + } + switch_event_fire(&event); + } else { + session->read_frame_count--; + } + } + + if (switch_channel_test_flag(session->channel, CF_HOLD)) { status = SWITCH_STATUS_BREAK; goto even_more_done; diff --git a/src/switch_core_session.c b/src/switch_core_session.c index e3ad866c28..531d19a504 100644 --- a/src/switch_core_session.c +++ b/src/switch_core_session.c @@ -780,6 +780,20 @@ SWITCH_DECLARE(void) switch_core_session_perform_destroy(switch_core_session_t * } +SWITCH_DECLARE(void) switch_core_session_enable_heartbeat(switch_core_session_t *session, uint32_t seconds) +{ + switch_assert(session != NULL); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s setting session heartbeat to %u second(s).", + switch_channel_get_name(session->channel), seconds); + session->track_duration = seconds; +} + +SWITCH_DECLARE(void) switch_core_session_disable_heartbeat(switch_core_session_t *session) +{ + switch_assert(session != NULL); + session->track_duration = 0; +} + static void *SWITCH_THREAD_FUNC switch_core_session_thread(switch_thread_t *thread, void *obj) { switch_core_session_t *session = obj; diff --git a/src/switch_event.c b/src/switch_event.c index 26b4f5e8e3..88666a94f0 100644 --- a/src/switch_event.c +++ b/src/switch_event.c @@ -165,6 +165,7 @@ static char *EVENT_NAMES[] = { "CHANNEL_DATA", "GENERAL", "COMMAND", + "SESSION_HEARTBEAT", "ALL" };