add session heartbeat feature

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@9882 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Anthony Minessale 2008-10-07 21:03:37 +00:00
parent 1d9f205098
commit fc0c89c642
7 changed files with 76 additions and 0 deletions

View File

@ -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 {

View File

@ -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

View File

@ -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;

View File

@ -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)) {

View File

@ -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;

View File

@ -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;

View File

@ -165,6 +165,7 @@ static char *EVENT_NAMES[] = {
"CHANNEL_DATA",
"GENERAL",
"COMMAND",
"SESSION_HEARTBEAT",
"ALL"
};