]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
add session heartbeat feature
authorAnthony Minessale <anthony.minessale@gmail.com>
Tue, 7 Oct 2008 21:03:37 +0000 (21:03 +0000)
committerAnthony Minessale <anthony.minessale@gmail.com>
Tue, 7 Oct 2008 21:03:37 +0000 (21:03 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@9882 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/include/private/switch_core_pvt.h
src/include/switch_core.h
src/include/switch_types.h
src/switch_channel.c
src/switch_core_io.c
src/switch_core_session.c
src/switch_event.c

index 49d21a6d641435f19ce5abe3f415b22a075184cc..52817475b0432992f7343f2d1adde64454824533 100644 (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 {
index a40fec8cbdd2b74f0b2493c65866c7a3181703fe..f279a73a53726dcb32ed0638ee0387b0accdc518 100644 (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
index 761a027e1acfb4be45ce54e07c1d33acdb78c81f..a9b283f4a8f79a075a9cdc9fc368a053dbbc069a 100644 (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;
 
index fa6b2a0c03197ad7f02d42c26799396e49cf4246..42ae7864ba1f0862d8727b3dc8f03713ad0d1d2d 100644 (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)) {
index 459131f691210e79407c869e019be9d8e47a186c..25733f21e5907e0b88e52ffe142015bb2322082f 100644 (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;
index e3ad866c288c1b7da08ad98fb204714b24612151..531d19a5042d97a694a3aa06c116f2aabd551daf 100644 (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;
index 26b4f5e8e312737a2b0f987d73cd0afe73d40d80..88666a94f0d296e2d8dec4185f67aac9f3c3a2fb 100644 (file)
@@ -165,6 +165,7 @@ static char *EVENT_NAMES[] = {
        "CHANNEL_DATA",
        "GENERAL",
        "COMMAND",
+       "SESSION_HEARTBEAT",
        "ALL"
 };