From a495ce7c9ab6c260ad251ef890a88045eee0f236 Mon Sep 17 00:00:00 2001 From: Daniel Slavetskiy Date: Wed, 19 Feb 2025 16:18:53 +0100 Subject: [PATCH 1/5] add conditions for concurrent grammar handling --- mod_unimrcp.c | 51 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/mod_unimrcp.c b/mod_unimrcp.c index fb876fa..0edd887 100644 --- a/mod_unimrcp.c +++ b/mod_unimrcp.c @@ -2428,9 +2428,11 @@ static switch_status_t recog_channel_load_grammar(speech_channel_t *schannel, co } /* Create the grammar and save it */ - if ((status = grammar_create(&g, name, type, data, schannel->memory_pool)) == SWITCH_STATUS_SUCCESS) { - recognizer_data_t *r = (recognizer_data_t *) schannel->data; - switch_core_hash_insert(r->grammars, g->name, g); + recognizer_data_t *r = (recognizer_data_t *) schannel->data; + if (r->grammars) { + if ((status = grammar_create(&g, name, type, data, schannel->memory_pool)) == SWITCH_STATUS_SUCCESS) { + switch_core_hash_insert(r->grammars, g->name, g); + } } done: @@ -2459,8 +2461,12 @@ static switch_status_t recog_channel_unload_grammar(speech_channel_t *schannel, switch_log_printf(SWITCH_CHANNEL_UUID_LOG(schannel->session_uuid), SWITCH_LOG_DEBUG, "(%s) Unloading grammar %s\n", schannel->name, grammar_name); switch_mutex_lock(schannel->mutex); - switch_core_hash_delete(r->enabled_grammars, grammar_name); - switch_core_hash_delete(r->grammars, grammar_name); + if (r->enabled_grammars) { + switch_core_hash_delete(r->enabled_grammars, grammar_name); + } + if (r->grammars) { + switch_core_hash_delete(r->grammars, grammar_name); + } switch_mutex_unlock(schannel->mutex); } @@ -2485,15 +2491,19 @@ static switch_status_t recog_channel_enable_grammar(speech_channel_t *schannel, grammar_t *grammar; switch_mutex_lock(schannel->mutex); - grammar = (grammar_t *) switch_core_hash_find(r->grammars, grammar_name); - if (grammar == NULL) - { - switch_log_printf(SWITCH_CHANNEL_UUID_LOG(schannel->session_uuid), SWITCH_LOG_ERROR, "(%s) Undefined grammar, %s\n", schannel->name, grammar_name); - status = SWITCH_STATUS_FALSE; - } - else { - switch_log_printf(SWITCH_CHANNEL_UUID_LOG(schannel->session_uuid), SWITCH_LOG_DEBUG, "(%s) Enabling grammar %s\n", schannel->name, grammar_name); - switch_core_hash_insert(r->enabled_grammars, grammar_name, grammar); + if (r->grammars) { + grammar = (grammar_t *) switch_core_hash_find(r->grammars, grammar_name); + if (grammar == NULL) + { + switch_log_printf(SWITCH_CHANNEL_UUID_LOG(schannel->session_uuid), SWITCH_LOG_ERROR, "(%s) Undefined grammar, %s\n", schannel->name, grammar_name); + status = SWITCH_STATUS_FALSE; + } + else { + if (r->enabled_grammars) { + switch_log_printf(SWITCH_CHANNEL_UUID_LOG(schannel->session_uuid), SWITCH_LOG_DEBUG, "(%s) Enabling grammar %s\n", schannel->name, grammar_name); + switch_core_hash_insert(r->enabled_grammars, grammar_name, grammar); + } + } } switch_mutex_unlock(schannel->mutex); } @@ -2519,7 +2529,9 @@ static switch_status_t recog_channel_disable_grammar(speech_channel_t *schannel, switch_log_printf(SWITCH_CHANNEL_UUID_LOG(schannel->session_uuid), SWITCH_LOG_DEBUG, "(%s) Disabling grammar %s\n", schannel->name, grammar_name); switch_mutex_lock(schannel->mutex); - switch_core_hash_delete(r->enabled_grammars, grammar_name); + if (r->enabled_grammars) { + switch_core_hash_delete(r->enabled_grammars, grammar_name); + } switch_mutex_unlock(schannel->mutex); } @@ -2534,14 +2546,17 @@ static switch_status_t recog_channel_disable_grammar(speech_channel_t *schannel, */ static switch_status_t recog_channel_disable_all_grammars(speech_channel_t *schannel) { - switch_status_t status = SWITCH_STATUS_SUCCESS; + switch_status_t status = SWITCH_STATUS_FALSE; recognizer_data_t *r = (recognizer_data_t *) schannel->data; switch_log_printf(SWITCH_CHANNEL_UUID_LOG(schannel->session_uuid), SWITCH_LOG_DEBUG, "(%s) Disabling all grammars\n", schannel->name); switch_mutex_lock(schannel->mutex); - switch_core_hash_destroy(&r->enabled_grammars); - switch_core_hash_init(&r->enabled_grammars); + if (r->enabled_grammars) { + switch_core_hash_destroy(&r->enabled_grammars); + switch_core_hash_init(&r->enabled_grammars); + status = SWITCH_STATUS_SUCCESS; + } switch_mutex_unlock(schannel->mutex); return status; From 937dd9a551017366351eb4b8a7367c046705db24 Mon Sep 17 00:00:00 2001 From: Daniel Slavetskiy Date: Wed, 19 Feb 2025 16:23:14 +0100 Subject: [PATCH 2/5] add conditions for concurrent grammar handling --- mod_unimrcp.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/mod_unimrcp.c b/mod_unimrcp.c index 0edd887..fa599f4 100644 --- a/mod_unimrcp.c +++ b/mod_unimrcp.c @@ -2493,18 +2493,19 @@ static switch_status_t recog_channel_enable_grammar(speech_channel_t *schannel, switch_mutex_lock(schannel->mutex); if (r->grammars) { grammar = (grammar_t *) switch_core_hash_find(r->grammars, grammar_name); - if (grammar == NULL) - { - switch_log_printf(SWITCH_CHANNEL_UUID_LOG(schannel->session_uuid), SWITCH_LOG_ERROR, "(%s) Undefined grammar, %s\n", schannel->name, grammar_name); - status = SWITCH_STATUS_FALSE; - } - else { - if (r->enabled_grammars) { - switch_log_printf(SWITCH_CHANNEL_UUID_LOG(schannel->session_uuid), SWITCH_LOG_DEBUG, "(%s) Enabling grammar %s\n", schannel->name, grammar_name); - switch_core_hash_insert(r->enabled_grammars, grammar_name, grammar); - } + } + if (grammar == NULL) + { + switch_log_printf(SWITCH_CHANNEL_UUID_LOG(schannel->session_uuid), SWITCH_LOG_ERROR, "(%s) Undefined grammar, %s\n", schannel->name, grammar_name); + status = SWITCH_STATUS_FALSE; + } + else { + if (r->enabled_grammars) { + switch_log_printf(SWITCH_CHANNEL_UUID_LOG(schannel->session_uuid), SWITCH_LOG_DEBUG, "(%s) Enabling grammar %s\n", schannel->name, grammar_name); + switch_core_hash_insert(r->enabled_grammars, grammar_name, grammar); } } + switch_mutex_unlock(schannel->mutex); } From a231761f9d18b72a33809b6b2bcbd2031c25c8d1 Mon Sep 17 00:00:00 2001 From: Daniel Slavetskiy Date: Fri, 21 Feb 2025 15:55:22 +0100 Subject: [PATCH 3/5] check if schannel->params exist, as thay could have been destroyed in speech_channel_destroy --- mod_unimrcp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mod_unimrcp.c b/mod_unimrcp.c index fa599f4..25e5f18 100644 --- a/mod_unimrcp.c +++ b/mod_unimrcp.c @@ -1459,7 +1459,7 @@ static const char *speech_channel_type_to_string(speech_channel_type_t type) static switch_status_t speech_channel_set_param(speech_channel_t *schannel, const char *param, const char *val) { switch_mutex_lock(schannel->mutex); - if (!zstr(param) && val != NULL) { + if (!zstr(param) && val != NULL && schannel->params) { /* check if this is a FreeSWITCH param that needs to be translated to an MRCP param: e.g. voice ==> voice-name */ const char *v; const char *p = switch_core_hash_find(schannel->application->fs_param_map, param); From 7c5cef3d762284fb518adcfc8083150e6034f7e2 Mon Sep 17 00:00:00 2001 From: Daniel Slavetskiy Date: Fri, 21 Feb 2025 16:05:26 +0100 Subject: [PATCH 4/5] improve --- mod_unimrcp.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/mod_unimrcp.c b/mod_unimrcp.c index 25e5f18..fd7b01e 100644 --- a/mod_unimrcp.c +++ b/mod_unimrcp.c @@ -2429,10 +2429,8 @@ static switch_status_t recog_channel_load_grammar(speech_channel_t *schannel, co /* Create the grammar and save it */ recognizer_data_t *r = (recognizer_data_t *) schannel->data; - if (r->grammars) { - if ((status = grammar_create(&g, name, type, data, schannel->memory_pool)) == SWITCH_STATUS_SUCCESS) { - switch_core_hash_insert(r->grammars, g->name, g); - } + if (r->grammars && (status = grammar_create(&g, name, type, data, schannel->memory_pool)) == SWITCH_STATUS_SUCCESS) { + switch_core_hash_insert(r->grammars, g->name, g); } done: @@ -2491,21 +2489,16 @@ static switch_status_t recog_channel_enable_grammar(speech_channel_t *schannel, grammar_t *grammar; switch_mutex_lock(schannel->mutex); - if (r->grammars) { - grammar = (grammar_t *) switch_core_hash_find(r->grammars, grammar_name); - } - if (grammar == NULL) + if (r->grammars && (grammar = (grammar_t *) switch_core_hash_find(r->grammars, grammar_name)) == NULL) { switch_log_printf(SWITCH_CHANNEL_UUID_LOG(schannel->session_uuid), SWITCH_LOG_ERROR, "(%s) Undefined grammar, %s\n", schannel->name, grammar_name); status = SWITCH_STATUS_FALSE; } - else { - if (r->enabled_grammars) { - switch_log_printf(SWITCH_CHANNEL_UUID_LOG(schannel->session_uuid), SWITCH_LOG_DEBUG, "(%s) Enabling grammar %s\n", schannel->name, grammar_name); - switch_core_hash_insert(r->enabled_grammars, grammar_name, grammar); - } + else if (r->enabled_grammars) { + switch_log_printf(SWITCH_CHANNEL_UUID_LOG(schannel->session_uuid), SWITCH_LOG_DEBUG, "(%s) Enabling grammar %s\n", schannel->name, grammar_name); + switch_core_hash_insert(r->enabled_grammars, grammar_name, grammar); } - + switch_mutex_unlock(schannel->mutex); } From 789a6f6c8be44c851e1bc1329b9cb42e0707199c Mon Sep 17 00:00:00 2001 From: Daniel Slavetskiy Date: Fri, 21 Feb 2025 16:07:35 +0100 Subject: [PATCH 5/5] improve --- mod_unimrcp.c | 1 - 1 file changed, 1 deletion(-) diff --git a/mod_unimrcp.c b/mod_unimrcp.c index fd7b01e..9c7f71e 100644 --- a/mod_unimrcp.c +++ b/mod_unimrcp.c @@ -2498,7 +2498,6 @@ static switch_status_t recog_channel_enable_grammar(speech_channel_t *schannel, switch_log_printf(SWITCH_CHANNEL_UUID_LOG(schannel->session_uuid), SWITCH_LOG_DEBUG, "(%s) Enabling grammar %s\n", schannel->name, grammar_name); switch_core_hash_insert(r->enabled_grammars, grammar_name, grammar); } - switch_mutex_unlock(schannel->mutex); }