| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#include "asterisk.h" |
| 31 |
|
| 32 |
#include "asterisk/channel.h" |
| 33 |
#include "asterisk/module.h" |
| 34 |
#include "asterisk/lock.h" |
| 35 |
#include "asterisk/linkedlists.h" |
| 36 |
#include "asterisk/cli.h" |
| 37 |
#include "asterisk/term.h" |
| 38 |
#include "asterisk/speech.h" |
| 39 |
#include "asterisk/format_cache.h" |
| 40 |
#include "asterisk/translate.h" |
| 41 |
|
| 42 |
static AST_RWLIST_HEAD_STATIC(engines, ast_speech_engine); |
| 43 |
static struct ast_speech_engine *default_engine = NULL; |
| 44 |
|
| 45 |
|
| 46 |
struct ast_speech_engine *ast_speech_find_engine(const char *engine_name) |
| 47 |
{ |
| 48 |
struct ast_speech_engine *engine = NULL; |
| 49 |
|
| 50 |
|
| 51 |
if (ast_strlen_zero(engine_name)) |
| 52 |
return default_engine; |
| 53 |
|
| 54 |
AST_RWLIST_RDLOCK(&engines); |
| 55 |
AST_RWLIST_TRAVERSE(&engines, engine, list) { |
| 56 |
if (!strcasecmp(engine->name, engine_name)) { |
| 57 |
break; |
| 58 |
} |
| 59 |
} |
| 60 |
AST_RWLIST_UNLOCK(&engines); |
| 61 |
|
| 62 |
return engine; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
int ast_speech_grammar_activate(struct ast_speech *speech, const char *grammar_name) |
| 67 |
{ |
| 68 |
return (speech->engine->activate ? speech->engine->activate(speech, grammar_name) : -1); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
int ast_speech_grammar_deactivate(struct ast_speech *speech, const char *grammar_name) |
| 73 |
{ |
| 74 |
return (speech->engine->deactivate ? speech->engine->deactivate(speech, grammar_name) : -1); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
int ast_speech_grammar_load(struct ast_speech *speech, const char *grammar_name, const char *grammar) |
| 79 |
{ |
| 80 |
return (speech->engine->load ? speech->engine->load(speech, grammar_name, grammar) : -1); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
int ast_speech_grammar_unload(struct ast_speech *speech, const char *grammar_name) |
| 85 |
{ |
| 86 |
return (speech->engine->unload ? speech->engine->unload(speech, grammar_name) : -1); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
struct ast_speech_result *ast_speech_results_get(struct ast_speech *speech) |
| 91 |
{ |
| 92 |
return (speech->engine->get ? speech->engine->get(speech) : NULL); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
int ast_speech_results_free(struct ast_speech_result *result) |
| 97 |
{ |
| 98 |
struct ast_speech_result *current_result = result, *prev_result = NULL; |
| 99 |
int res = 0; |
| 100 |
|
| 101 |
while (current_result != NULL) { |
| 102 |
prev_result = current_result; |
| 103 |
|
| 104 |
if (current_result->text != NULL) { |
| 105 |
ast_free(current_result->text); |
| 106 |
current_result->text = NULL; |
| 107 |
} |
| 108 |
if (current_result->grammar != NULL) { |
| 109 |
ast_free(current_result->grammar); |
| 110 |
current_result->grammar = NULL; |
| 111 |
} |
| 112 |
|
| 113 |
current_result = AST_LIST_NEXT(current_result, list); |
| 114 |
ast_free(prev_result); |
| 115 |
prev_result = NULL; |
| 116 |
} |
| 117 |
|
| 118 |
return res; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
void ast_speech_start(struct ast_speech *speech) |
| 123 |
{ |
| 124 |
|
| 125 |
|
| 126 |
ast_clear_flag(speech, AST_SPEECH_SPOKE); |
| 127 |
ast_clear_flag(speech, AST_SPEECH_QUIET); |
| 128 |
ast_clear_flag(speech, AST_SPEECH_HAVE_RESULTS); |
| 129 |
|
| 130 |
|
| 131 |
if (speech->results) { |
| 132 |
ast_speech_results_free(speech->results); |
| 133 |
speech->results = NULL; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
if (speech->engine->start) |
| 138 |
speech->engine->start(speech); |
| 139 |
|
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
int ast_speech_write(struct ast_speech *speech, void *data, int len) |
| 145 |
{ |
| 146 |
|
| 147 |
if (speech->state != AST_SPEECH_STATE_READY) |
| 148 |
return -1; |
| 149 |
|
| 150 |
return speech->engine->write(speech, data, len); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
int ast_speech_dtmf(struct ast_speech *speech, const char *dtmf) |
| 155 |
{ |
| 156 |
int res = 0; |
| 157 |
|
| 158 |
if (speech->state != AST_SPEECH_STATE_READY) |
| 159 |
return -1; |
| 160 |
|
| 161 |
if (speech->engine->dtmf != NULL) { |
| 162 |
res = speech->engine->dtmf(speech, dtmf); |
| 163 |
} |
| 164 |
|
| 165 |
return res; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
int ast_speech_change(struct ast_speech *speech, const char *name, const char *value) |
| 170 |
{ |
| 171 |
return (speech->engine->change ? speech->engine->change(speech, name, value) : -1); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
int ast_speech_get_setting(struct ast_speech *speech, const char *name, char *buf, size_t len) |
| 176 |
{ |
| 177 |
return (speech->engine->get_setting ? speech->engine->get_setting(speech, name, buf, len) : -1); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
struct ast_speech *ast_speech_new(const char *engine_name, const struct ast_format_cap *cap) |
| 182 |
{ |
| 183 |
struct ast_speech_engine *engine = NULL; |
| 184 |
struct ast_speech *new_speech = NULL; |
| 185 |
struct ast_format_cap *joint; |
| 186 |
RAII_VAR(struct ast_format *, best, NULL, ao2_cleanup); |
| 187 |
RAII_VAR(struct ast_format *, best_translated, NULL, ao2_cleanup); |
| 188 |
|
| 189 |
|
| 190 |
if (!(engine = ast_speech_find_engine(engine_name))) |
| 191 |
return NULL; |
| 192 |
|
| 193 |
joint = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT); |
| 194 |
if (!joint) { |
| 195 |
return NULL; |
| 196 |
} |
| 197 |
|
| 198 |
ast_format_cap_get_compatible(engine->formats, cap, joint); |
| 199 |
best = ast_format_cap_get_format(joint, 0); |
| 200 |
ao2_ref(joint, -1); |
| 201 |
|
| 202 |
if (!best) { |
| 203 |
if (ast_format_cap_iscompatible_format(engine->formats, ast_format_slin) != AST_FORMAT_CMP_NOT_EQUAL) { |
| 204 |
best = ao2_bump(ast_format_slin); |
| 205 |
} else { |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
if (ast_translator_best_choice(engine->formats, (struct ast_format_cap *)cap, &best, &best_translated)) { |
| 213 |
|
| 214 |
return NULL; |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
if (!(new_speech = ast_calloc(1, sizeof(*new_speech)))) { |
| 221 |
return NULL; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
ast_mutex_init(&new_speech->lock); |
| 226 |
|
| 227 |
|
| 228 |
new_speech->results = NULL; |
| 229 |
|
| 230 |
|
| 231 |
new_speech->engine = engine; |
| 232 |
|
| 233 |
|
| 234 |
new_speech->format = ao2_bump(best); |
| 235 |
|
| 236 |
|
| 237 |
ast_speech_change_state(new_speech, AST_SPEECH_STATE_NOT_READY); |
| 238 |
|
| 239 |
|
| 240 |
if (engine->create(new_speech, new_speech->format)) { |
| 241 |
ast_mutex_destroy(&new_speech->lock); |
| 242 |
ao2_ref(new_speech->format, -1); |
| 243 |
ast_free(new_speech); |
| 244 |
return NULL; |
| 245 |
} |
| 246 |
|
| 247 |
return new_speech; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
int ast_speech_destroy(struct ast_speech *speech) |
| 252 |
{ |
| 253 |
int res = 0; |
| 254 |
|
| 255 |
|
| 256 |
speech->engine->destroy(speech); |
| 257 |
|
| 258 |
|
| 259 |
ast_mutex_destroy(&speech->lock); |
| 260 |
|
| 261 |
|
| 262 |
if (speech->results) |
| 263 |
ast_speech_results_free(speech->results); |
| 264 |
|
| 265 |
|
| 266 |
if (speech->processing_sound) |
| 267 |
ast_free(speech->processing_sound); |
| 268 |
|
| 269 |
ao2_ref(speech->format, -1); |
| 270 |
|
| 271 |
|
| 272 |
ast_free(speech); |
| 273 |
|
| 274 |
return res; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
int ast_speech_change_state(struct ast_speech *speech, int state) |
| 279 |
{ |
| 280 |
int res = 0; |
| 281 |
|
| 282 |
switch (state) { |
| 283 |
case AST_SPEECH_STATE_WAIT: |
| 284 |
|
| 285 |
ast_set_flag(speech, AST_SPEECH_SPOKE); |
| 286 |
default: |
| 287 |
speech->state = state; |
| 288 |
break; |
| 289 |
} |
| 290 |
|
| 291 |
return res; |
| 292 |
} |
| 293 |
|
| 294 |
const char *ast_speech_results_type_to_string(enum ast_speech_results_type type) |
| 295 |
{ |
| 296 |
switch (type) { |
| 297 |
case AST_SPEECH_RESULTS_TYPE_NORMAL: |
| 298 |
return "normal"; |
| 299 |
case AST_SPEECH_RESULTS_TYPE_NBEST: |
| 300 |
return "nbest"; |
| 301 |
default: |
| 302 |
ast_assert(0); |
| 303 |
return "unknown"; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
int ast_speech_change_results_type(struct ast_speech *speech, enum ast_speech_results_type results_type) |
| 309 |
{ |
| 310 |
speech->results_type = results_type; |
| 311 |
|
| 312 |
return (speech->engine->change_results_type ? speech->engine->change_results_type(speech, results_type) : 0); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
int ast_speech_register(struct ast_speech_engine *engine) |
| 317 |
{ |
| 318 |
int res = 0; |
| 319 |
|
| 320 |
|
| 321 |
if (!engine->create || !engine->write || !engine->destroy) { |
| 322 |
ast_log(LOG_WARNING, "Speech recognition engine '%s' did not meet minimum API requirements.\n", engine->name); |
| 323 |
return -1; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
if (ast_speech_find_engine(engine->name)) { |
| 328 |
ast_log(LOG_WARNING, "Speech recognition engine '%s' already exists.\n", engine->name); |
| 329 |
return -1; |
| 330 |
} |
| 331 |
|
| 332 |
ast_verb(5, "Registered speech recognition engine '%s'\n", engine->name); |
| 333 |
|
| 334 |
|
| 335 |
AST_RWLIST_WRLOCK(&engines); |
| 336 |
AST_RWLIST_INSERT_HEAD(&engines, engine, list); |
| 337 |
if (!default_engine) { |
| 338 |
default_engine = engine; |
| 339 |
ast_verb(5, "Made '%s' the default speech recognition engine\n", engine->name); |
| 340 |
} |
| 341 |
AST_RWLIST_UNLOCK(&engines); |
| 342 |
|
| 343 |
return res; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
int ast_speech_unregister(const char *engine_name) |
| 348 |
{ |
| 349 |
return ast_speech_unregister2(engine_name) == NULL ? -1 : 0; |
| 350 |
} |
| 351 |
|
| 352 |
struct ast_speech_engine *ast_speech_unregister2(const char *engine_name) |
| 353 |
{ |
| 354 |
struct ast_speech_engine *engine = NULL; |
| 355 |
|
| 356 |
if (ast_strlen_zero(engine_name)) { |
| 357 |
return NULL; |
| 358 |
} |
| 359 |
|
| 360 |
AST_RWLIST_WRLOCK(&engines); |
| 361 |
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&engines, engine, list) { |
| 362 |
if (!strcasecmp(engine->name, engine_name)) { |
| 363 |
|
| 364 |
AST_RWLIST_REMOVE_CURRENT(list); |
| 365 |
|
| 366 |
if (engine == default_engine) { |
| 367 |
default_engine = AST_RWLIST_FIRST(&engines); |
| 368 |
} |
| 369 |
ast_verb(5, "Unregistered speech recognition engine '%s'\n", engine_name); |
| 370 |
|
| 371 |
break; |
| 372 |
} |
| 373 |
} |
| 374 |
AST_RWLIST_TRAVERSE_SAFE_END; |
| 375 |
AST_RWLIST_UNLOCK(&engines); |
| 376 |
|
| 377 |
return engine; |
| 378 |
} |
| 379 |
|
| 380 |
void ast_speech_unregister_engines( |
| 381 |
int (*should_unregister)(const struct ast_speech_engine *engine, void *data), void *data, |
| 382 |
void (*on_unregistered)(void *obj)) |
| 383 |
{ |
| 384 |
struct ast_speech_engine *engine = NULL; |
| 385 |
|
| 386 |
if (!should_unregister) { |
| 387 |
return; |
| 388 |
} |
| 389 |
|
| 390 |
AST_RWLIST_WRLOCK(&engines); |
| 391 |
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&engines, engine, list) { |
| 392 |
if (should_unregister(engine, data)) { |
| 393 |
|
| 394 |
AST_RWLIST_REMOVE_CURRENT(list); |
| 395 |
|
| 396 |
if (engine == default_engine) { |
| 397 |
default_engine = AST_RWLIST_FIRST(&engines); |
| 398 |
} |
| 399 |
ast_verb(5, "Unregistered speech recognition engine '%s'\n", engine->name); |
| 400 |
|
| 401 |
if (on_unregistered) { |
| 402 |
on_unregistered(engine); |
| 403 |
} |
| 404 |
} |
| 405 |
} |
| 406 |
AST_RWLIST_TRAVERSE_SAFE_END; |
| 407 |
AST_RWLIST_UNLOCK(&engines); |
| 408 |
} |
| 409 |
|
| 410 |
static int unload_module(void) |
| 411 |
{ |
| 412 |
|
| 413 |
return -1; |
| 414 |
} |
| 415 |
|
| 416 |
static int load_module(void) |
| 417 |
{ |
| 418 |
return AST_MODULE_LOAD_SUCCESS; |
| 419 |
} |
| 420 |
|
| 421 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Generic Speech Recognition API", |
| 422 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 423 |
.load = load_module, |
| 424 |
.unload = unload_module, |
| 425 |
.load_pri = AST_MODPRI_APP_DEPEND - 1, |
| 426 |
); |