[Freeswitch-svn] [commit] r9882 - in freeswitch/trunk/src: . include include/private

Freeswitch SVN anthm at freeswitch.org
Tue Oct 7 17:03:37 EDT 2008


Author: anthm
Date: Tue Oct  7 17:03:37 2008
New Revision: 9882

Modified:
   freeswitch/trunk/src/include/private/switch_core_pvt.h
   freeswitch/trunk/src/include/switch_core.h
   freeswitch/trunk/src/include/switch_types.h
   freeswitch/trunk/src/switch_channel.c
   freeswitch/trunk/src/switch_core_io.c
   freeswitch/trunk/src/switch_core_session.c
   freeswitch/trunk/src/switch_event.c

Log:
add session heartbeat feature

Modified: freeswitch/trunk/src/include/private/switch_core_pvt.h
==============================================================================
--- freeswitch/trunk/src/include/private/switch_core_pvt.h	(original)
+++ freeswitch/trunk/src/include/private/switch_core_pvt.h	Tue Oct  7 17:03:37 2008
@@ -146,6 +146,8 @@
 	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 {

Modified: freeswitch/trunk/src/include/switch_core.h
==============================================================================
--- freeswitch/trunk/src/include/switch_core.h	(original)
+++ freeswitch/trunk/src/include/switch_core.h	Tue Oct  7 17:03:37 2008
@@ -119,6 +119,9 @@
 ///\{
 
 
+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

Modified: freeswitch/trunk/src/include/switch_types.h
==============================================================================
--- freeswitch/trunk/src/include/switch_types.h	(original)
+++ freeswitch/trunk/src/include/switch_types.h	Tue Oct  7 17:03:37 2008
@@ -110,6 +110,7 @@
 #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 @@
 	SWITCH_EVENT_CHANNEL_DATA,
 	SWITCH_EVENT_GENERAL,
 	SWITCH_EVENT_COMMAND,
+	SWITCH_EVENT_SESSION_HEARTBEAT,
 	SWITCH_EVENT_ALL
 } switch_event_types_t;
 

Modified: freeswitch/trunk/src/switch_channel.c
==============================================================================
--- freeswitch/trunk/src/switch_channel.c	(original)
+++ freeswitch/trunk/src/switch_channel.c	Tue Oct  7 17:03:37 2008
@@ -1627,6 +1627,24 @@
 		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)) {

Modified: freeswitch/trunk/src/switch_core_io.c
==============================================================================
--- freeswitch/trunk/src/switch_core_io.c	(original)
+++ freeswitch/trunk/src/switch_core_io.c	Tue Oct  7 17:03:37 2008
@@ -106,6 +106,7 @@
 	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 @@
 
 	*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;

Modified: freeswitch/trunk/src/switch_core_session.c
==============================================================================
--- freeswitch/trunk/src/switch_core_session.c	(original)
+++ freeswitch/trunk/src/switch_core_session.c	Tue Oct  7 17:03:37 2008
@@ -780,6 +780,20 @@
 
 }
 
+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;

Modified: freeswitch/trunk/src/switch_event.c
==============================================================================
--- freeswitch/trunk/src/switch_event.c	(original)
+++ freeswitch/trunk/src/switch_event.c	Tue Oct  7 17:03:37 2008
@@ -165,6 +165,7 @@
 	"CHANNEL_DATA",
 	"GENERAL",
 	"COMMAND",
+	"SESSION_HEARTBEAT",
 	"ALL"
 };
 



More information about the Freeswitch-svn mailing list