<!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][14111] </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=14111">14111</a></dd>
<dt>Author</dt> <dd>crienzo</dd>
<dt>Date</dt> <dd>2009-07-02 10:12:01 -0500 (Thu, 02 Jul 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre>Allow multiple grammars to be loaded in a session, added support for decoupling recognition from load_grammar if the core is changed</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcmodasr_ttsmod_unimrcpmod_unimrcpc">freeswitch/trunk/src/mod/asr_tts/mod_unimrcp/mod_unimrcp.c</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunksrcmodasr_ttsmod_unimrcpmod_unimrcpc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/asr_tts/mod_unimrcp/mod_unimrcp.c (14110 => 14111)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/asr_tts/mod_unimrcp/mod_unimrcp.c        2009-07-02 08:45:29 UTC (rev 14110)
+++ freeswitch/trunk/src/mod/asr_tts/mod_unimrcp/mod_unimrcp.c        2009-07-02 15:12:01 UTC (rev 14111)
</span><span class="lines">@@ -399,8 +399,10 @@
</span><span class="cx">  * Data specific to the recognizer
</span><span class="cx">  */
</span><span class="cx"> struct recognizer_data {
</span><del>-        /** the speech recognition grammar */
-        grammar_t *grammar;
</del><ins>+        /** the available grammars */
+        switch_hash_t *grammars;
+        /** the last grammar used (for pause/resume) */
+        grammar_t *last_grammar;
</ins><span class="cx">         /** recognize result */
</span><span class="cx">         char *result;
</span><span class="cx">         /** true, if voice has started */
</span><span class="lines">@@ -419,6 +421,9 @@
</span><span class="cx"> static switch_status_t recog_asr_unload_grammar(switch_asr_handle_t *ah, const char *name);
</span><span class="cx"> static switch_status_t recog_asr_close(switch_asr_handle_t *ah, switch_asr_flag_t *flags);
</span><span class="cx"> static switch_status_t recog_asr_feed(switch_asr_handle_t *ah, void *data, unsigned int len, switch_asr_flag_t *flags);
</span><ins>+#if 0
+static switch_status_t recog_asr_start(switch_asr_handle_t *ah, const char *name);
+#endif
</ins><span class="cx"> static switch_status_t recog_asr_resume(switch_asr_handle_t *ah);
</span><span class="cx"> static switch_status_t recog_asr_pause(switch_asr_handle_t *ah);
</span><span class="cx"> static switch_status_t recog_asr_check_results(switch_asr_handle_t *ah, switch_asr_flag_t *flags);
</span><span class="lines">@@ -434,7 +439,7 @@
</span><span class="cx"> static apt_bool_t recog_stream_read(mpf_audio_stream_t *stream, mpf_frame_t *frame);
</span><span class="cx"> 
</span><span class="cx"> /* recognizer specific speech_channel_funcs */
</span><del>-static switch_status_t recog_channel_start(speech_channel_t *schannel);
</del><ins>+static switch_status_t recog_channel_start(speech_channel_t *schannel, const char *name);
</ins><span class="cx"> static switch_status_t recog_channel_load_grammar(speech_channel_t *schannel, const char *name, grammar_type_t type, const char *data);
</span><span class="cx"> static switch_status_t recog_channel_unload_grammar(speech_channel_t *schannel, const char *name);
</span><span class="cx"> static switch_status_t recog_channel_check_results(speech_channel_t *schannel);
</span><span class="lines">@@ -1865,9 +1870,10 @@
</span><span class="cx">  * Start RECOGNIZE request
</span><span class="cx">  *
</span><span class="cx">  * @param schannel the channel to start
</span><ins>+ * @param name the name of the grammar to use or NULL if to reuse the last grammar
</ins><span class="cx">  * @return SWITCH_STATUS_SUCCESS if successful
</span><span class="cx">  */
</span><del>-static switch_status_t recog_channel_start(speech_channel_t *schannel)
</del><ins>+static switch_status_t recog_channel_start(speech_channel_t *schannel, const char *name)
</ins><span class="cx"> {
</span><span class="cx">         switch_status_t status = SWITCH_STATUS_SUCCESS;
</span><span class="cx">         mrcp_message_t *mrcp_message;
</span><span class="lines">@@ -1876,6 +1882,7 @@
</span><span class="cx">         recognizer_data_t *r;
</span><span class="cx">         char *start_input_timers;
</span><span class="cx">         const char *mime_type;
</span><ins>+        grammar_t *grammar = NULL;
</ins><span class="cx"> 
</span><span class="cx">         switch_mutex_lock(schannel-&gt;mutex);
</span><span class="cx">         if (schannel-&gt;state != SPEECH_CHANNEL_READY) {
</span><span class="lines">@@ -1894,7 +1901,24 @@
</span><span class="cx">         /* input timers are started by default unless the start-input-timers=false param is set */
</span><span class="cx">         start_input_timers = (char *)switch_core_hash_find(schannel-&gt;params, &quot;start-input-timers&quot;);
</span><span class="cx">         r-&gt;timers_started = switch_strlen_zero(start_input_timers) || strcasecmp(start_input_timers, &quot;false&quot;);
</span><del>-                
</del><ins>+        
+        /* get the cached grammar */
+        if (switch_strlen_zero(name)) {
+                grammar = r-&gt;last_grammar;
+        } else {
+                grammar = (grammar_t *)switch_core_hash_find(r-&gt;grammars, name);
+                r-&gt;last_grammar = grammar;
+        }
+        if (grammar == NULL) {
+                if (name) {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;(%s) Undefined grammar, %s\n&quot;, schannel-&gt;name, name);
+                } else {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;(%s) No grammar specified\n&quot;, schannel-&gt;name);
+                }
+                status = SWITCH_STATUS_FALSE;
+                goto done;
+        }
+
</ins><span class="cx">         /* create MRCP message */
</span><span class="cx">         mrcp_message = mrcp_application_message_create(schannel-&gt;unimrcp_session, schannel-&gt;unimrcp_channel, RECOGNIZER_RECOGNIZE);
</span><span class="cx">         if (mrcp_message == NULL) {
</span><span class="lines">@@ -1910,7 +1934,7 @@
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         /* set Content-Type */
</span><del>-        mime_type = grammar_type_to_mime(r-&gt;grammar-&gt;type, schannel-&gt;profile);
</del><ins>+        mime_type = grammar_type_to_mime(grammar-&gt;type, schannel-&gt;profile);
</ins><span class="cx">         if (switch_strlen_zero(mime_type)) {
</span><span class="cx">                 status = SWITCH_STATUS_FALSE;
</span><span class="cx">                 goto done;
</span><span class="lines">@@ -1919,8 +1943,8 @@
</span><span class="cx">         mrcp_generic_header_property_add(mrcp_message, GENERIC_HEADER_CONTENT_TYPE);
</span><span class="cx"> 
</span><span class="cx">         /* set Content-ID for inline grammars */
</span><del>-        if (r-&gt;grammar-&gt;type != GRAMMAR_TYPE_URI) {
-                apt_string_assign(&amp;generic_header-&gt;content_id, r-&gt;grammar-&gt;name, mrcp_message-&gt;pool);
</del><ins>+        if (grammar-&gt;type != GRAMMAR_TYPE_URI) {
+                apt_string_assign(&amp;generic_header-&gt;content_id, grammar-&gt;name, mrcp_message-&gt;pool);
</ins><span class="cx">                 mrcp_generic_header_property_add(mrcp_message, GENERIC_HEADER_CONTENT_ID);
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -1941,7 +1965,7 @@
</span><span class="cx">         recog_channel_set_params(schannel, mrcp_message, generic_header, recog_header);
</span><span class="cx"> 
</span><span class="cx">         /* set message body */
</span><del>-        apt_string_assign(&amp;mrcp_message-&gt;body, r-&gt;grammar-&gt;data, mrcp_message-&gt;pool);
</del><ins>+        apt_string_assign(&amp;mrcp_message-&gt;body, grammar-&gt;data, mrcp_message-&gt;pool);
</ins><span class="cx"> 
</span><span class="cx">         /* Empty audio queue and send RECOGNIZE to MRCP server */
</span><span class="cx">         audio_queue_clear(schannel-&gt;audio_queue);
</span><span class="lines">@@ -2038,7 +2062,7 @@
</span><span class="cx">         /* Create the grammar and save it */
</span><span class="cx">         if ((status = grammar_create(&amp;g, name, type, data, schannel-&gt;memory_pool)) == SWITCH_STATUS_SUCCESS) {
</span><span class="cx">                 recognizer_data_t *r = (recognizer_data_t *)schannel-&gt;data;
</span><del>-                r-&gt;grammar = g;
</del><ins>+                switch_core_hash_insert(r-&gt;grammars, name, g);
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">  done:
</span><span class="lines">@@ -2065,7 +2089,7 @@
</span><span class="cx">         } else {
</span><span class="cx">                 recognizer_data_t *r = (recognizer_data_t *)schannel-&gt;data;
</span><span class="cx">                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;(%s) Unloading grammar %s\n&quot;, schannel-&gt;name, grammar_name);
</span><del>-                r-&gt;grammar = NULL;
</del><ins>+                switch_core_hash_delete(r-&gt;grammars, grammar_name);
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         return status;
</span><span class="lines">@@ -2485,6 +2509,7 @@
</span><span class="cx">         char name[200] = { 0 };
</span><span class="cx">         const char *profile_name;
</span><span class="cx">         profile_t *profile = NULL;
</span><ins>+        recognizer_data_t *r = NULL;
</ins><span class="cx"> 
</span><span class="cx">         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, &quot;asr_handle: name = %s, codec = %s, rate = %d, grammar = %s, param = %s\n&quot;,
</span><span class="cx">                 ah-&gt;name, ah-&gt;codec, ah-&gt;rate, ah-&gt;grammar, ah-&gt;param);
</span><span class="lines">@@ -2500,9 +2525,12 @@
</span><span class="cx">                 goto done;
</span><span class="cx">         }
</span><span class="cx">         ah-&gt;private_info = schannel;
</span><del>-        schannel-&gt;data = switch_core_alloc(ah-&gt;memory_pool, sizeof(recognizer_data_t));
-        memset(schannel-&gt;data, 0, sizeof(recognizer_data_t));
</del><span class="cx"> 
</span><ins>+        r = (recognizer_data_t *)switch_core_alloc(ah-&gt;memory_pool, sizeof(recognizer_data_t));
+        schannel-&gt;data = r;
+        memset(r, 0, sizeof(recognizer_data_t));
+        switch_core_hash_init(&amp;r-&gt;grammars, ah-&gt;memory_pool); 
+
</ins><span class="cx">         /* try to open an MRCP channel */
</span><span class="cx">         profile_name = switch_strlen_zero(dest) ? globals.unimrcp_default_recog_profile : dest;
</span><span class="cx">         profile = (profile_t *)switch_core_hash_find(globals.profiles, profile_name);
</span><span class="lines">@@ -2521,10 +2549,10 @@
</span><span class="cx"> /**
</span><span class="cx">  * Process asr_load_grammar request from FreeSWITCH.
</span><span class="cx">  *
</span><del>- * FreeSWITCH sends this request to start recognition.
</del><ins>+ * FreeSWITCH sends this request to load a grammar
</ins><span class="cx">  * @param ah the FreeSWITCH speech recognition handle
</span><span class="cx">  * @param grammar the grammar data.  This can be an absolute file path, a URI, or the grammar text.
</span><del>- * @param name used to reference grammar for unloading or for subsequent recognition requests
</del><ins>+ * @param name used to reference grammar for unloading or for recognition requests
</ins><span class="cx">  */
</span><span class="cx"> static switch_status_t recog_asr_load_grammar(switch_asr_handle_t *ah, const char *grammar, const char *name)
</span><span class="cx"> {
</span><span class="lines">@@ -2535,11 +2563,10 @@
</span><span class="cx">         switch_file_t *grammar_file = NULL;
</span><span class="cx">         switch_size_t grammar_file_size = 0, to_read = 0;
</span><span class="cx">         grammar_type_t type = GRAMMAR_TYPE_UNKNOWN;
</span><del>-        const char *lname = switch_strlen_zero(name) ? &quot;grammar&quot; : name;
</del><span class="cx">         char *filename = NULL;
</span><span class="cx">         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;(%s) grammar = %s, name = %s\n&quot;, schannel-&gt;name, grammar, name);
</span><span class="cx"> 
</span><del>-        if (switch_strlen_zero(grammar)) {
</del><ins>+        if (switch_strlen_zero(grammar) || switch_strlen_zero(name)) {
</ins><span class="cx">                 status = SWITCH_STATUS_FALSE;
</span><span class="cx">                 goto done;
</span><span class="cx">         }
</span><span class="lines">@@ -2612,14 +2639,14 @@
</span><span class="cx">         }
</span><span class="cx">         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;(%s) grammar is %s\n&quot;, schannel-&gt;name, grammar_type_to_mime(type, schannel-&gt;profile));
</span><span class="cx"> 
</span><del>-        /* load the grammar and start recognition */
-        if (recog_channel_load_grammar(schannel, lname, type, grammar_data) != SWITCH_STATUS_SUCCESS) {
</del><ins>+        /* load the grammar */
+        if (recog_channel_load_grammar(schannel, name, type, grammar_data) != SWITCH_STATUS_SUCCESS) {
</ins><span class="cx">                 status = SWITCH_STATUS_FALSE;
</span><span class="cx">                 goto done;
</span><span class="cx">         }
</span><del>-        
-        status = recog_channel_start(schannel);
</del><span class="cx"> 
</span><ins>+        status = recog_channel_start(schannel, name);
+
</ins><span class="cx">  done:
</span><span class="cx"> 
</span><span class="cx">         switch_safe_free(filename);
</span><span class="lines">@@ -2640,9 +2667,8 @@
</span><span class="cx"> {
</span><span class="cx">         switch_status_t status = SWITCH_STATUS_SUCCESS;
</span><span class="cx">         speech_channel_t *schannel = (speech_channel_t *)ah-&gt;private_info;
</span><del>-        const char *lname = switch_strlen_zero(name) ? &quot;grammar&quot; : name;
-        if (speech_channel_stop(schannel) != SWITCH_STATUS_SUCCESS ||
-                recog_channel_unload_grammar(schannel, lname) != SWITCH_STATUS_SUCCESS) {
</del><ins>+        if (switch_strlen_zero(name) || speech_channel_stop(schannel) != SWITCH_STATUS_SUCCESS ||
+                recog_channel_unload_grammar(schannel, name) != SWITCH_STATUS_SUCCESS) {
</ins><span class="cx">                 status = SWITCH_STATUS_FALSE;
</span><span class="cx">         }
</span><span class="cx">         return status;
</span><span class="lines">@@ -2652,12 +2678,16 @@
</span><span class="cx">  * Process asr_close request from FreeSWITCH
</span><span class="cx">  *
</span><span class="cx">  * @param ah the FreeSWITCH speech recognition handle
</span><ins>+ * @param flags speech recognition flags (unused)
+ * @return SWITCH_STATUS_SUCCESS
</ins><span class="cx">  */
</span><span class="cx"> static switch_status_t recog_asr_close(switch_asr_handle_t *ah, switch_asr_flag_t *flags)
</span><span class="cx"> {
</span><span class="cx">         speech_channel_t *schannel = (speech_channel_t *)ah-&gt;private_info;
</span><ins>+        recognizer_data_t *r = (recognizer_data_t *)schannel-&gt;data;
</ins><span class="cx">         speech_channel_stop(schannel);
</span><span class="cx">         speech_channel_destroy(schannel);
</span><ins>+        switch_core_hash_destroy(&amp;r-&gt;grammars);
</ins><span class="cx"> 
</span><span class="cx">         /* this lets FreeSWITCH's speech_thread know the handle is closed */
</span><span class="cx">     switch_set_flag(ah, SWITCH_ASR_FLAG_CLOSED);
</span><span class="lines">@@ -2669,6 +2699,7 @@
</span><span class="cx">  * Process asr_feed request from FreeSWITCH
</span><span class="cx">  *
</span><span class="cx">  * @param ah the FreeSWITCH speech recognition handle
</span><ins>+ * @return SWITCH_STATUS_SUCCESS if successful 
</ins><span class="cx">  */
</span><span class="cx"> static switch_status_t recog_asr_feed(switch_asr_handle_t *ah, void *data, unsigned int len, switch_asr_flag_t *flags)
</span><span class="cx"> {
</span><span class="lines">@@ -2677,7 +2708,23 @@
</span><span class="cx">         return speech_channel_write(schannel, data, &amp;slen);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+#if 0
</ins><span class="cx"> /**
</span><ins>+ * Process asr_start request from FreeSWITCH
+ * @param ah the FreeSWITCH speech recognition handle
+ * @param name name of the grammar to use
+ * @return SWITCH_STATUS_SUCCESS if successful
+ */ 
+static switch_status_t recog_asr_start(switch_asr_handle_t *ah, const char *name)
+{
+        switch_status_t status;
+        speech_channel_t *schannel = (speech_channel_t *)ah-&gt;private_info;
+        status = recog_channel_start(schannel, name);
+        return status;
+}
+#endif
+
+/**
</ins><span class="cx">  * Process asr_resume request from FreeSWITCH
</span><span class="cx">  *
</span><span class="cx">  * @param ah the FreeSWITCH speech recognition handle
</span><span class="lines">@@ -2685,7 +2732,7 @@
</span><span class="cx"> static switch_status_t recog_asr_resume(switch_asr_handle_t *ah)
</span><span class="cx"> {
</span><span class="cx">         speech_channel_t *schannel = (speech_channel_t *)ah-&gt;private_info;
</span><del>-        return recog_channel_start(schannel);
</del><ins>+        return recog_channel_start(schannel, NULL);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><span class="lines">@@ -2935,6 +2982,9 @@
</span><span class="cx">         asr_interface-&gt;asr_unload_grammar = recog_asr_unload_grammar;
</span><span class="cx">         asr_interface-&gt;asr_close = recog_asr_close;
</span><span class="cx">         asr_interface-&gt;asr_feed = recog_asr_feed;
</span><ins>+#if 0
+        asr_interface-&gt;asr_start = recog_asr_start;
+#endif
</ins><span class="cx">         asr_interface-&gt;asr_resume = recog_asr_resume;
</span><span class="cx">         asr_interface-&gt;asr_pause = recog_asr_pause;
</span><span class="cx">         asr_interface-&gt;asr_check_results = recog_asr_check_results;
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>

</body>
</html>