<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][14960] </title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=14960">14960</a></dd>
<dt>Author</dt> <dd>anthm</dd>
<dt>Date</dt> <dd>2009-09-23 17:39:00 -0500 (Wed, 23 Sep 2009)</dd>
</dl>
<h3>Log Message</h3>
<pre>add preprocess framework for agc ec etc, ALPHA, needs work</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcincludeswitch_coreh">freeswitch/trunk/src/include/switch_core.h</a></li>
<li><a href="#freeswitchtrunksrcmodapplicationsmod_commandsmod_commandsc">freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c</a></li>
<li><a href="#freeswitchtrunksrcmodapplicationsmod_dptoolsmod_dptoolsc">freeswitch/trunk/src/mod/applications/mod_dptools/mod_dptools.c</a></li>
<li><a href="#freeswitchtrunksrcswitch_ivr_asyncc">freeswitch/trunk/src/switch_ivr_async.c</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunksrcincludeswitch_coreh"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/include/switch_core.h (14959 => 14960)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/include/switch_core.h        2009-09-23 17:44:41 UTC (rev 14959)
+++ freeswitch/trunk/src/include/switch_core.h        2009-09-23 22:39:00 UTC (rev 14960)
</span><span class="lines">@@ -1900,6 +1900,9 @@
</span><span class="cx"> SWITCH_DECLARE(void) switch_cond_next(void);
</span><span class="cx"> SWITCH_DECLARE(switch_status_t) switch_core_chat_send(const char *name, const char *proto, const char *from, const char *to,
</span><span class="cx">                                                                                                          const char *subject, const char *body, const char *type, const char *hint);
</span><ins>+
+SWITCH_DECLARE(switch_status_t) switch_ivr_preprocess_session(switch_core_session_t *session, const char *cmds);
+
</ins><span class="cx"> ///\}
</span><span class="cx">
</span><span class="cx"> /*!
</span></span></pre></div>
<a id="freeswitchtrunksrcmodapplicationsmod_commandsmod_commandsc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c (14959 => 14960)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c        2009-09-23 17:44:41 UTC (rev 14959)
+++ freeswitch/trunk/src/mod/applications/mod_commands/mod_commands.c        2009-09-23 22:39:00 UTC (rev 14960)
</span><span class="lines">@@ -1529,6 +1529,29 @@
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+#define PREPROCESS_SYNTAX "<>"
+SWITCH_STANDARD_API(preprocess_function)
+{
+        switch_core_session_t *ksession = NULL;
+        char *mycmd = NULL;
+
+        if (switch_strlen_zero(cmd)) {
+                stream->write_function(stream, "-USAGE: %s\n", KILL_SYNTAX);
+                return SWITCH_STATUS_SUCCESS;
+        }
+
+        if (!(ksession = switch_core_session_locate(cmd))) {
+                stream->write_function(stream, "-ERR No Such Channel!\n");
+        } else {
+                switch_ivr_preprocess_session(session, (char *)cmd);
+                switch_core_session_rwunlock(ksession);
+                stream->write_function(stream, "+OK\n");
+        }
+
+        switch_safe_free(mycmd);
+        return SWITCH_STATUS_SUCCESS;
+}
+
</ins><span class="cx"> #define PARK_SYNTAX "<uuid>"
</span><span class="cx"> SWITCH_STANDARD_API(park_function)
</span><span class="cx"> {
</span><span class="lines">@@ -3580,6 +3603,7 @@
</span><span class="cx">         SWITCH_ADD_API(commands_api_interface, "originate", "Originate a Call", originate_function, ORIGINATE_SYNTAX);
</span><span class="cx">         SWITCH_ADD_API(commands_api_interface, "tone_detect", "Start Tone Detection on a channel", tone_detect_session_function, TONE_DETECT_SYNTAX);
</span><span class="cx">         SWITCH_ADD_API(commands_api_interface, "uuid_kill", "Kill Channel", kill_function, KILL_SYNTAX);
</span><ins>+        SWITCH_ADD_API(commands_api_interface, "uuid_preprocess", "Pre-process Channel", preprocess_function, PREPROCESS_SYNTAX);
</ins><span class="cx">         SWITCH_ADD_API(commands_api_interface, "uuid_park", "Park Channel", park_function, PARK_SYNTAX);
</span><span class="cx">         SWITCH_ADD_API(commands_api_interface, "reloadacl", "Reload ACL", reload_acl_function, "[reloadxml]");
</span><span class="cx">         switch_console_set_complete("add reloadacl reloadxml");
</span></span></pre></div>
<a id="freeswitchtrunksrcmodapplicationsmod_dptoolsmod_dptoolsc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/applications/mod_dptools/mod_dptools.c (14959 => 14960)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/applications/mod_dptools/mod_dptools.c        2009-09-23 17:44:41 UTC (rev 14959)
+++ freeswitch/trunk/src/mod/applications/mod_dptools/mod_dptools.c        2009-09-23 22:39:00 UTC (rev 14960)
</span><span class="lines">@@ -2022,6 +2022,11 @@
</span><span class="cx">         }
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+SWITCH_STANDARD_APP(preprocess_session_function)
+{
+        switch_ivr_preprocess_session(session, (char *)data);
+}
+
</ins><span class="cx"> SWITCH_STANDARD_APP(record_session_function)
</span><span class="cx"> {
</span><span class="cx">         char *path = NULL;
</span><span class="lines">@@ -2806,6 +2811,7 @@
</span><span class="cx">         SWITCH_ADD_APP(app_interface, "record_session", "Record Session", SESS_REC_DESC, record_session_function, "<path> [+<timeout>]", SAF_NONE);
</span><span class="cx">         SWITCH_ADD_APP(app_interface, "record", "Record File", "Record a file from the channels input", record_function,
</span><span class="cx">                                  "<path> [<time_limit_secs>] [<silence_thresh>] [<silence_hits>]", SAF_NONE);
</span><ins>+        SWITCH_ADD_APP(app_interface, "preprocess", "pre-process", "pre-process", preprocess_session_function, "", SAF_NONE);
</ins><span class="cx">         SWITCH_ADD_APP(app_interface, "stop_displace_session", "Stop Displace File", "Stop Displacing to a file", stop_displace_session_function, "<path>",
</span><span class="cx">                                  SAF_NONE);
</span><span class="cx">         SWITCH_ADD_APP(app_interface, "displace_session", "Displace File", DISPLACE_DESC, displace_session_function, "<path> [<flags>] [+time_limit_ms]",
</span></span></pre></div>
<a id="freeswitchtrunksrcswitch_ivr_asyncc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/switch_ivr_async.c (14959 => 14960)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/switch_ivr_async.c        2009-09-23 17:44:41 UTC (rev 14959)
+++ freeswitch/trunk/src/switch_ivr_async.c        2009-09-23 22:39:00 UTC (rev 14960)
</span><span class="lines">@@ -32,6 +32,8 @@
</span><span class="cx"> */
</span><span class="cx">
</span><span class="cx"> #include <switch.h>
</span><ins>+#include <speex/speex_preprocess.h>
+#include <speex/speex_echo.h>
</ins><span class="cx">
</span><span class="cx"> #ifdef SWITCH_VIDEO_IN_THREADS
</span><span class="cx"> struct echo_helper {
</span><span class="lines">@@ -1026,6 +1028,295 @@
</span><span class="cx">         return SWITCH_STATUS_SUCCESS;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+typedef struct {
+        SpeexPreprocessState *read_st;
+        SpeexPreprocessState *write_st;
+        SpeexEchoState *read_ec;
+        SpeexEchoState *write_ec;
+        switch_byte_t read_data[2048];
+        switch_byte_t write_data[2048];
+        switch_byte_t read_out[2048];
+        switch_byte_t write_out[2048];
+        switch_mutex_t *read_mutex;
+        switch_mutex_t *write_mutex;
+        int done;
+} pp_cb_t;
+
+static switch_bool_t preprocess_callback(switch_media_bug_t *bug, void *user_data, switch_abc_type_t type)
+{
+        switch_core_session_t *session = switch_core_media_bug_get_session(bug);
+        switch_channel_t *channel = switch_core_session_get_channel(session);
+        pp_cb_t *cb = (pp_cb_t *) user_data;
+        switch_codec_implementation_t read_impl = {0};
+
+ switch_core_session_get_read_impl(session, &read_impl);
+        switch_frame_t *frame = NULL;
+        int y;
+        
+        switch (type) {
+        case SWITCH_ABC_TYPE_INIT:
+                {
+                        switch_mutex_init(&cb->read_mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(session));
+                        switch_mutex_init(&cb->write_mutex, SWITCH_MUTEX_NESTED, switch_core_session_get_pool(session));
+                }
+                break;
+        case SWITCH_ABC_TYPE_CLOSE:
+                {
+                        if (cb->read_st) {
+                                speex_preprocess_state_destroy(cb->read_st);
+                        }
+
+                        if (cb->write_st) {
+                                speex_preprocess_state_destroy(cb->write_st);
+                        }
+
+                        if (cb->read_ec) {
+                                speex_echo_state_destroy(cb->read_ec);
+                        }
+
+                        if (cb->write_ec) {
+                                speex_echo_state_destroy(cb->write_ec);
+                        }
+
+                        switch_channel_set_private(channel, "_preprocess", NULL);
+                }
+                break;
+        case SWITCH_ABC_TYPE_READ_REPLACE:
+                {
+                        if (cb->done) return SWITCH_FALSE;
+                        frame = switch_core_media_bug_get_read_replace_frame(bug);
+
+                        if (cb->read_st) {
+
+                                if (cb->read_ec) {
+                                        speex_echo_cancellation(cb->read_ec, (int16_t *)frame->data, (int16_t *)cb->write_data, (int16_t *)cb->read_out);
+                                        memcpy(frame->data, cb->read_out, frame->datalen);
+                                }
+
+                                y = speex_preprocess_run(cb->read_st, frame->data);                                
+                        }
+
+                        if (cb->write_ec) {
+                                memcpy(cb->read_data, frame->data, frame->datalen);
+                        }
+                }
+                break;
+        case SWITCH_ABC_TYPE_WRITE_REPLACE:
+                {
+                        if (cb->done) return SWITCH_FALSE;
+                        frame = switch_core_media_bug_get_write_replace_frame(bug);
+
+                        if (cb->write_st) {
+
+                                if (cb->write_ec) {
+                                        speex_echo_cancellation(cb->write_ec, (int16_t *)frame->data, (int16_t *)cb->read_data, (int16_t *)cb->write_out);
+                                        memcpy(frame->data, cb->write_out, frame->datalen);
+                                }
+                                
+                                y = speex_preprocess_run(cb->write_st, frame->data);
+                        }
+
+                        if (cb->read_ec) {
+                                memcpy(cb->write_data, frame->data, frame->datalen);
+                        }
+                }
+                break;
+        default:
+                break;
+        }
+
+        return SWITCH_TRUE;
+}
+
+SWITCH_DECLARE(switch_status_t) switch_ivr_preprocess_session(switch_core_session_t *session, const char *cmds)
+{
+        switch_channel_t *channel = switch_core_session_get_channel(session);
+        switch_media_bug_t *bug;
+        switch_status_t status;
+        time_t to = 0;
+        switch_media_bug_flag_t flags = 0;
+        switch_codec_implementation_t read_impl = {0};
+        pp_cb_t *cb;
+        int update = 0;
+        int argc;
+        char *mydata = NULL, *argv[5];
+        int i = 0;
+
+ switch_core_session_get_read_impl(session, &read_impl);
+
+        if ((cb = switch_channel_get_private(channel, "_preprocess"))) {
+                update = 1;
+        } else {
+                cb = switch_core_session_alloc(session, sizeof(*cb));                
+        }
+
+
+        if (update) {
+                if (!strcasecmp(cmds, "stop")) {
+                        cb->done = 1;
+                        return SWITCH_STATUS_SUCCESS;
+                }
+        }
+
+        mydata = strdup(cmds);
+        argc = switch_separate_string(mydata, ',', argv, (sizeof(argv) / sizeof(argv[0])));
+        
+        for(i = 0; i < argc; i++) {
+                char *var = argv[i];
+                char *val = NULL;
+                char rw;
+                int tr;
+                int err = 1;
+                SpeexPreprocessState *st = NULL;
+                SpeexEchoState *ec;
+                switch_mutex_t *mutex = NULL;
+                int r = 0;
+
+                if (var) {
+                        if ((val = strchr(var, '='))) {
+                                *val++ = '\0';
+                                
+                                rw = *var++;
+                                while(*var == '.' || *var == '_') {
+                                        var++;
+                                }
+                                
+                                if (rw == 'r') {
+                                        if (!cb->read_st) {
+                                                cb->read_st = speex_preprocess_state_init(read_impl.samples_per_packet, read_impl.samples_per_second);
+                                                flags |= SMBF_READ_REPLACE;
+                                        }
+                                        st = cb->read_st;
+                                        ec = cb->read_ec;
+                                        mutex = cb->read_mutex;
+                                } else if (rw == 'w') {
+                                        if (!cb->write_st) {
+                                                cb->write_st = speex_preprocess_state_init(read_impl.samples_per_packet, read_impl.samples_per_second);
+                                                flags |= SMBF_WRITE_REPLACE;
+                                        }
+                                        st = cb->write_st;
+                                        ec = cb->write_ec;
+                                        mutex = cb->write_mutex;
+                                }
+
+                                if (mutex) switch_mutex_lock(mutex);
+                                
+                                if (st) {
+                                        err = 0;
+                                        tr = switch_true(val);
+                                        if (!strcasecmp(var, "agc")) {
+                                                int l = read_impl.samples_per_second;
+                                                int tmp = atoi(val);
+
+                                                if (!tr) {
+                                                        l = tmp;
+                                                }
+
+                                                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Setting AGC on %c to %d\n", rw, tr);
+                                                speex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_AGC, &tr);
+                                                speex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_AGC_LEVEL, &l);
+
+                                        } else if (!strcasecmp(var, "noise_supress")) {
+                                                int db = atoi(val);
+                                                if (db < 0) {
+                                                        r = speex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_NOISE_SUPPRESS, &db);
+                                                        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Setting NOISE_SUPRESS on %c to %d [%d]\n", rw, db, r);
+                                                } else {
+                                                        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Syntax error noise_supress should be in -db\n");
+                                                }
+                                        } else if (!strcasecmp(var, "echo_cancel")) {
+                                                int tail = 1024;
+                                                int tmp = atoi(val);
+
+                                                if (!tr && tmp > 0) {
+                                                        tail = tmp;
+                                                } else if (!tr) {
+                                                        if (ec) {
+                                                                if (rw == 'r') {
+                                                                        speex_echo_state_destroy(cb->read_ec);
+                                                                        cb->read_ec = NULL;
+                                                                } else {
+                                                                        speex_echo_state_destroy(cb->write_ec);
+                                                                        cb->write_ec = NULL;
+                                                                }
+                                                        }
+                                                        
+                                                        ec = NULL;
+                                                }
+                                                
+                                                if (!ec) {
+                                                        if (rw == 'r') {
+                                                                ec = cb->read_ec = speex_echo_state_init(read_impl.samples_per_packet, tail);
+                                                                speex_echo_ctl(ec, SPEEX_ECHO_SET_SAMPLING_RATE, &read_impl.samples_per_second);
+                                                                flags |= SMBF_WRITE_REPLACE;
+                                                        } else {
+                                                                ec = cb->write_ec = speex_echo_state_init(read_impl.samples_per_packet, tail);
+                                                                speex_echo_ctl(ec, SPEEX_ECHO_SET_SAMPLING_RATE, &read_impl.samples_per_second);
+                                                                flags |= SMBF_READ_REPLACE;
+                                                        }
+                                                        speex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_ECHO_STATE, ec);
+                                                }
+
+
+                                        } else if (!strcasecmp(var, "echo_supress")) {
+                                                int db = atoi(val);
+                                                if (db < 0) {
+                                                        speex_preprocess_ctl(st, SPEEX_PREPROCESS_SET_ECHO_SUPPRESS, &db);
+                                                        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Setting ECHO_SUPRESS on %c to %d [%d]\n", rw, db, r);
+                                                } else {
+                                                        switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Syntax error echo_supress should be in -db\n");
+                                                }
+                                        } else {
+                                                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Warning unknown parameter [%s] \n", var);
+                                        }
+                                }
+                        }
+
+                        if (mutex) switch_mutex_unlock(mutex);
+
+                        if (err) {
+                                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Syntax error parsing preprossor commands\n");
+                        }
+
+                } else {
+                        break;
+                }
+        }
+
+
+        switch_safe_free(mydata);
+
+        if (update) {
+                return SWITCH_STATUS_SUCCESS;
+        }
+
+
+        if ((status = switch_core_media_bug_add(session, preprocess_callback, cb, to, flags, &bug)) != SWITCH_STATUS_SUCCESS) {
+                switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Error adding media bug.\n");
+                if (cb->read_st) {
+                        speex_preprocess_state_destroy(cb->read_st);
+                }
+                
+                if (cb->write_st) {
+                        speex_preprocess_state_destroy(cb->write_st);
+                }
+
+                if (cb->read_ec) {
+                        speex_echo_state_destroy(cb->read_ec);
+                }
+                
+                if (cb->write_ec) {
+                        speex_echo_state_destroy(cb->write_ec);
+                }
+                
+                return status;
+        }
+
+        switch_channel_set_private(channel, "_preprocess", cb);
+
+        return SWITCH_STATUS_SUCCESS;
+}
+
</ins><span class="cx"> typedef struct {
</span><span class="cx">         switch_core_session_t *session;
</span><span class="cx">         teletone_dtmf_detect_state_t dtmf_detect;
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>
</body>
</html>