| 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 |
|
| 31 |
|
| 32 |
|
| 33 |
#include "asterisk.h" |
| 34 |
|
| 35 |
#include "asterisk/file.h" |
| 36 |
#include "asterisk/channel.h" |
| 37 |
#include "asterisk/pbx.h" |
| 38 |
#include "asterisk/module.h" |
| 39 |
#include "asterisk/lock.h" |
| 40 |
#include "asterisk/app.h" |
| 41 |
#include "asterisk/speech.h" |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
static void destroy_callback(void *data) |
| 320 |
{ |
| 321 |
struct ast_speech *speech = (struct ast_speech*)data; |
| 322 |
|
| 323 |
if (speech == NULL) { |
| 324 |
return; |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
ast_speech_destroy(speech); |
| 329 |
|
| 330 |
return; |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
static const struct ast_datastore_info speech_datastore = { |
| 335 |
.type = "speech", |
| 336 |
.destroy = destroy_callback |
| 337 |
}; |
| 338 |
|
| 339 |
|
| 340 |
static struct ast_speech *find_speech(struct ast_channel *chan) |
| 341 |
{ |
| 342 |
struct ast_speech *speech = NULL; |
| 343 |
struct ast_datastore *datastore = NULL; |
| 344 |
|
| 345 |
if (!chan) { |
| 346 |
return NULL; |
| 347 |
} |
| 348 |
|
| 349 |
ast_channel_lock(chan); |
| 350 |
datastore = ast_channel_datastore_find(chan, &speech_datastore, NULL); |
| 351 |
ast_channel_unlock(chan); |
| 352 |
if (datastore == NULL) { |
| 353 |
return NULL; |
| 354 |
} |
| 355 |
speech = datastore->data; |
| 356 |
|
| 357 |
return speech; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
static int speech_datastore_destroy(struct ast_channel *chan) |
| 370 |
{ |
| 371 |
struct ast_datastore *datastore; |
| 372 |
int res; |
| 373 |
|
| 374 |
ast_channel_lock(chan); |
| 375 |
datastore = ast_channel_datastore_find(chan, &speech_datastore, NULL); |
| 376 |
if (datastore) { |
| 377 |
ast_channel_datastore_remove(chan, datastore); |
| 378 |
} |
| 379 |
ast_channel_unlock(chan); |
| 380 |
if (datastore) { |
| 381 |
ast_datastore_free(datastore); |
| 382 |
res = 0; |
| 383 |
} else { |
| 384 |
res = -1; |
| 385 |
} |
| 386 |
return res; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
static struct ast_speech_result *find_result(struct ast_speech_result *results, char *result_num) |
| 391 |
{ |
| 392 |
struct ast_speech_result *result = results; |
| 393 |
char *tmp = NULL; |
| 394 |
int nbest_num = 0, wanted_num = 0, i = 0; |
| 395 |
|
| 396 |
if (!result) { |
| 397 |
return NULL; |
| 398 |
} |
| 399 |
|
| 400 |
if ((tmp = strchr(result_num, '/'))) { |
| 401 |
*tmp++ = '\0'; |
| 402 |
nbest_num = atoi(result_num); |
| 403 |
wanted_num = atoi(tmp); |
| 404 |
} else { |
| 405 |
wanted_num = atoi(result_num); |
| 406 |
} |
| 407 |
|
| 408 |
do { |
| 409 |
if (result->nbest_num != nbest_num) |
| 410 |
continue; |
| 411 |
if (i == wanted_num) |
| 412 |
break; |
| 413 |
i++; |
| 414 |
} while ((result = AST_LIST_NEXT(result, list))); |
| 415 |
|
| 416 |
return result; |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
static int speech_score(struct ast_channel *chan, const char *cmd, char *data, |
| 421 |
char *buf, size_t len) |
| 422 |
{ |
| 423 |
struct ast_speech_result *result = NULL; |
| 424 |
struct ast_speech *speech = find_speech(chan); |
| 425 |
char tmp[128] = ""; |
| 426 |
|
| 427 |
if (data == NULL || speech == NULL || !(result = find_result(speech->results, data))) { |
| 428 |
return -1; |
| 429 |
} |
| 430 |
|
| 431 |
snprintf(tmp, sizeof(tmp), "%d", result->score); |
| 432 |
|
| 433 |
ast_copy_string(buf, tmp, len); |
| 434 |
|
| 435 |
return 0; |
| 436 |
} |
| 437 |
|
| 438 |
static struct ast_custom_function speech_score_function = { |
| 439 |
.name = "SPEECH_SCORE", |
| 440 |
.read = speech_score, |
| 441 |
.write = NULL, |
| 442 |
}; |
| 443 |
|
| 444 |
|
| 445 |
static int speech_text(struct ast_channel *chan, const char *cmd, char *data, |
| 446 |
char *buf, size_t len) |
| 447 |
{ |
| 448 |
struct ast_speech_result *result = NULL; |
| 449 |
struct ast_speech *speech = find_speech(chan); |
| 450 |
|
| 451 |
if (data == NULL || speech == NULL || !(result = find_result(speech->results, data))) { |
| 452 |
return -1; |
| 453 |
} |
| 454 |
|
| 455 |
if (result->text != NULL) { |
| 456 |
ast_copy_string(buf, result->text, len); |
| 457 |
} else { |
| 458 |
buf[0] = '\0'; |
| 459 |
} |
| 460 |
|
| 461 |
return 0; |
| 462 |
} |
| 463 |
|
| 464 |
static struct ast_custom_function speech_text_function = { |
| 465 |
.name = "SPEECH_TEXT", |
| 466 |
.read = speech_text, |
| 467 |
.write = NULL, |
| 468 |
}; |
| 469 |
|
| 470 |
|
| 471 |
static int speech_grammar(struct ast_channel *chan, const char *cmd, char *data, |
| 472 |
char *buf, size_t len) |
| 473 |
{ |
| 474 |
struct ast_speech_result *result = NULL; |
| 475 |
struct ast_speech *speech = find_speech(chan); |
| 476 |
|
| 477 |
if (data == NULL || speech == NULL || !(result = find_result(speech->results, data))) { |
| 478 |
return -1; |
| 479 |
} |
| 480 |
|
| 481 |
if (result->grammar != NULL) { |
| 482 |
ast_copy_string(buf, result->grammar, len); |
| 483 |
} else { |
| 484 |
buf[0] = '\0'; |
| 485 |
} |
| 486 |
|
| 487 |
return 0; |
| 488 |
} |
| 489 |
|
| 490 |
static struct ast_custom_function speech_grammar_function = { |
| 491 |
.name = "SPEECH_GRAMMAR", |
| 492 |
.read = speech_grammar, |
| 493 |
.write = NULL, |
| 494 |
}; |
| 495 |
|
| 496 |
|
| 497 |
static int speech_engine_write(struct ast_channel *chan, const char *cmd, char *data, const char *value) |
| 498 |
{ |
| 499 |
struct ast_speech *speech = find_speech(chan); |
| 500 |
|
| 501 |
if (data == NULL || speech == NULL) { |
| 502 |
return -1; |
| 503 |
} |
| 504 |
|
| 505 |
ast_speech_change(speech, data, value); |
| 506 |
|
| 507 |
return 0; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
static int speech_engine_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 512 |
{ |
| 513 |
struct ast_speech *speech = find_speech(chan); |
| 514 |
|
| 515 |
if (!data || !speech) { |
| 516 |
return -1; |
| 517 |
} |
| 518 |
|
| 519 |
return ast_speech_get_setting(speech, data, buf, len); |
| 520 |
} |
| 521 |
|
| 522 |
static struct ast_custom_function speech_engine_function = { |
| 523 |
.name = "SPEECH_ENGINE", |
| 524 |
.read = speech_engine_read, |
| 525 |
.write = speech_engine_write, |
| 526 |
}; |
| 527 |
|
| 528 |
|
| 529 |
static int speech_results_type_write(struct ast_channel *chan, const char *cmd, char *data, const char *value) |
| 530 |
{ |
| 531 |
struct ast_speech *speech = find_speech(chan); |
| 532 |
|
| 533 |
if (data == NULL || speech == NULL) |
| 534 |
return -1; |
| 535 |
|
| 536 |
if (!strcasecmp(value, "normal")) |
| 537 |
ast_speech_change_results_type(speech, AST_SPEECH_RESULTS_TYPE_NORMAL); |
| 538 |
else if (!strcasecmp(value, "nbest")) |
| 539 |
ast_speech_change_results_type(speech, AST_SPEECH_RESULTS_TYPE_NBEST); |
| 540 |
|
| 541 |
return 0; |
| 542 |
} |
| 543 |
|
| 544 |
static struct ast_custom_function speech_results_type_function = { |
| 545 |
.name = "SPEECH_RESULTS_TYPE", |
| 546 |
.read = NULL, |
| 547 |
.write = speech_results_type_write, |
| 548 |
}; |
| 549 |
|
| 550 |
|
| 551 |
static int speech_read(struct ast_channel *chan, const char *cmd, char *data, |
| 552 |
char *buf, size_t len) |
| 553 |
{ |
| 554 |
int results = 0; |
| 555 |
struct ast_speech_result *result = NULL; |
| 556 |
struct ast_speech *speech = find_speech(chan); |
| 557 |
char tmp[128] = ""; |
| 558 |
|
| 559 |
|
| 560 |
if (!strcasecmp(data, "status")) { |
| 561 |
if (speech != NULL) |
| 562 |
ast_copy_string(buf, "1", len); |
| 563 |
else |
| 564 |
ast_copy_string(buf, "0", len); |
| 565 |
return 0; |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
if (speech == NULL) { |
| 570 |
return -1; |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
if (!strcasecmp(data, "spoke")) { |
| 575 |
if (ast_test_flag(speech, AST_SPEECH_SPOKE)) |
| 576 |
ast_copy_string(buf, "1", len); |
| 577 |
else |
| 578 |
ast_copy_string(buf, "0", len); |
| 579 |
} else if (!strcasecmp(data, "results")) { |
| 580 |
|
| 581 |
for (result = speech->results; result; result = AST_LIST_NEXT(result, list)) |
| 582 |
results++; |
| 583 |
snprintf(tmp, sizeof(tmp), "%d", results); |
| 584 |
ast_copy_string(buf, tmp, len); |
| 585 |
} else { |
| 586 |
buf[0] = '\0'; |
| 587 |
} |
| 588 |
|
| 589 |
return 0; |
| 590 |
} |
| 591 |
|
| 592 |
static struct ast_custom_function speech_function = { |
| 593 |
.name = "SPEECH", |
| 594 |
.read = speech_read, |
| 595 |
.write = NULL, |
| 596 |
}; |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
static int speech_create(struct ast_channel *chan, const char *data) |
| 602 |
{ |
| 603 |
struct ast_speech *speech = NULL; |
| 604 |
struct ast_datastore *datastore = NULL; |
| 605 |
|
| 606 |
|
| 607 |
speech = ast_speech_new(data, ast_channel_nativeformats(chan)); |
| 608 |
if (speech == NULL) { |
| 609 |
|
| 610 |
pbx_builtin_setvar_helper(chan, "ERROR", "1"); |
| 611 |
return 0; |
| 612 |
} |
| 613 |
|
| 614 |
datastore = ast_datastore_alloc(&speech_datastore, NULL); |
| 615 |
if (datastore == NULL) { |
| 616 |
ast_speech_destroy(speech); |
| 617 |
pbx_builtin_setvar_helper(chan, "ERROR", "1"); |
| 618 |
return 0; |
| 619 |
} |
| 620 |
pbx_builtin_setvar_helper(chan, "ERROR", NULL); |
| 621 |
datastore->data = speech; |
| 622 |
ast_channel_lock(chan); |
| 623 |
ast_channel_datastore_add(chan, datastore); |
| 624 |
ast_channel_unlock(chan); |
| 625 |
|
| 626 |
return 0; |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
static int speech_load(struct ast_channel *chan, const char *vdata) |
| 631 |
{ |
| 632 |
int res = 0; |
| 633 |
struct ast_speech *speech = find_speech(chan); |
| 634 |
char *data; |
| 635 |
AST_DECLARE_APP_ARGS(args, |
| 636 |
AST_APP_ARG(grammar); |
| 637 |
AST_APP_ARG(path); |
| 638 |
); |
| 639 |
|
| 640 |
data = ast_strdupa(vdata); |
| 641 |
AST_STANDARD_APP_ARGS(args, data); |
| 642 |
|
| 643 |
if (speech == NULL) |
| 644 |
return -1; |
| 645 |
|
| 646 |
if (args.argc != 2) |
| 647 |
return -1; |
| 648 |
|
| 649 |
|
| 650 |
res = ast_speech_grammar_load(speech, args.grammar, args.path); |
| 651 |
|
| 652 |
return res; |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
static int speech_unload(struct ast_channel *chan, const char *data) |
| 657 |
{ |
| 658 |
int res = 0; |
| 659 |
struct ast_speech *speech = find_speech(chan); |
| 660 |
|
| 661 |
if (speech == NULL) |
| 662 |
return -1; |
| 663 |
|
| 664 |
|
| 665 |
res = ast_speech_grammar_unload(speech, data); |
| 666 |
|
| 667 |
return res; |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
static int speech_deactivate(struct ast_channel *chan, const char *data) |
| 672 |
{ |
| 673 |
int res = 0; |
| 674 |
struct ast_speech *speech = find_speech(chan); |
| 675 |
|
| 676 |
if (speech == NULL) |
| 677 |
return -1; |
| 678 |
|
| 679 |
|
| 680 |
res = ast_speech_grammar_deactivate(speech, data); |
| 681 |
|
| 682 |
return res; |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
static int speech_activate(struct ast_channel *chan, const char *data) |
| 687 |
{ |
| 688 |
int res = 0; |
| 689 |
struct ast_speech *speech = find_speech(chan); |
| 690 |
|
| 691 |
if (speech == NULL) |
| 692 |
return -1; |
| 693 |
|
| 694 |
|
| 695 |
res = ast_speech_grammar_activate(speech, data); |
| 696 |
|
| 697 |
return res; |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
static int speech_start(struct ast_channel *chan, const char *data) |
| 702 |
{ |
| 703 |
int res = 0; |
| 704 |
struct ast_speech *speech = find_speech(chan); |
| 705 |
|
| 706 |
if (speech == NULL) |
| 707 |
return -1; |
| 708 |
|
| 709 |
ast_speech_start(speech); |
| 710 |
|
| 711 |
return res; |
| 712 |
} |
| 713 |
|
| 714 |
|
| 715 |
static int speech_processing_sound(struct ast_channel *chan, const char *data) |
| 716 |
{ |
| 717 |
int res = 0; |
| 718 |
struct ast_speech *speech = find_speech(chan); |
| 719 |
|
| 720 |
if (speech == NULL) |
| 721 |
return -1; |
| 722 |
|
| 723 |
if (speech->processing_sound != NULL) { |
| 724 |
ast_free(speech->processing_sound); |
| 725 |
speech->processing_sound = NULL; |
| 726 |
} |
| 727 |
|
| 728 |
speech->processing_sound = ast_strdup(data); |
| 729 |
|
| 730 |
return res; |
| 731 |
} |
| 732 |
|
| 733 |
|
| 734 |
static int speech_streamfile(struct ast_channel *chan, const char *filename, const char *preflang) |
| 735 |
{ |
| 736 |
struct ast_filestream *fs = NULL; |
| 737 |
|
| 738 |
if (!(fs = ast_openstream(chan, filename, preflang))) |
| 739 |
return -1; |
| 740 |
|
| 741 |
if (ast_applystream(chan, fs)) |
| 742 |
return -1; |
| 743 |
|
| 744 |
ast_playstream(fs); |
| 745 |
|
| 746 |
return 0; |
| 747 |
} |
| 748 |
|
| 749 |
enum { |
| 750 |
SB_OPT_NOANSWER = (1 << 0), |
| 751 |
SB_OPT_PARTIALRESULTS = (1 << 1), |
| 752 |
}; |
| 753 |
|
| 754 |
AST_APP_OPTIONS(speech_background_options, BEGIN_OPTIONS |
| 755 |
AST_APP_OPTION('n', SB_OPT_NOANSWER), |
| 756 |
AST_APP_OPTION('p', SB_OPT_PARTIALRESULTS), |
| 757 |
END_OPTIONS ); |
| 758 |
|
| 759 |
|
| 760 |
static int speech_background(struct ast_channel *chan, const char *data) |
| 761 |
{ |
| 762 |
unsigned int timeout = 0; |
| 763 |
int res = 0, done = 0, started = 0, quieted = 0, max_dtmf_len = 0; |
| 764 |
struct ast_speech *speech = find_speech(chan); |
| 765 |
struct ast_frame *f = NULL; |
| 766 |
RAII_VAR(struct ast_format *, oldreadformat, NULL, ao2_cleanup); |
| 767 |
char dtmf[AST_MAX_EXTENSION] = ""; |
| 768 |
struct timeval start = { 0, 0 }, current; |
| 769 |
char *parse, *filename_tmp = NULL, *filename = NULL, tmp[2] = "", dtmf_terminator = '#'; |
| 770 |
const char *tmp2 = NULL; |
| 771 |
struct ast_flags options = { 0 }; |
| 772 |
AST_DECLARE_APP_ARGS(args, |
| 773 |
AST_APP_ARG(soundfile); |
| 774 |
AST_APP_ARG(timeout); |
| 775 |
AST_APP_ARG(options); |
| 776 |
); |
| 777 |
|
| 778 |
parse = ast_strdupa(data); |
| 779 |
AST_STANDARD_APP_ARGS(args, parse); |
| 780 |
|
| 781 |
if (speech == NULL) |
| 782 |
return -1; |
| 783 |
|
| 784 |
if (!ast_strlen_zero(args.options)) { |
| 785 |
char *options_buf = ast_strdupa(args.options); |
| 786 |
ast_app_parse_options(speech_background_options, &options, NULL, options_buf); |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
if (ast_channel_state(chan) != AST_STATE_UP && !ast_test_flag(&options, SB_OPT_NOANSWER) |
| 791 |
&& ast_answer(chan)) { |
| 792 |
return -1; |
| 793 |
} |
| 794 |
|
| 795 |
|
| 796 |
oldreadformat = ao2_bump(ast_channel_readformat(chan)); |
| 797 |
|
| 798 |
|
| 799 |
if (ast_set_read_format(chan, speech->format)) |
| 800 |
return -1; |
| 801 |
|
| 802 |
if (!ast_strlen_zero(args.soundfile)) { |
| 803 |
|
| 804 |
filename_tmp = ast_strdupa(args.soundfile); |
| 805 |
if (!ast_strlen_zero(args.timeout)) { |
| 806 |
if ((timeout = atof(args.timeout) * 1000.0) == 0) |
| 807 |
timeout = -1; |
| 808 |
} else |
| 809 |
timeout = 0; |
| 810 |
} |
| 811 |
|
| 812 |
|
| 813 |
ast_channel_lock(chan); |
| 814 |
if ((tmp2 = pbx_builtin_getvar_helper(chan, "SPEECH_DTMF_MAXLEN")) && !ast_strlen_zero(tmp2)) { |
| 815 |
max_dtmf_len = atoi(tmp2); |
| 816 |
} |
| 817 |
|
| 818 |
|
| 819 |
if ((tmp2 = pbx_builtin_getvar_helper(chan, "SPEECH_DTMF_TERMINATOR"))) { |
| 820 |
if (ast_strlen_zero(tmp2)) |
| 821 |
dtmf_terminator = '\0'; |
| 822 |
else |
| 823 |
dtmf_terminator = tmp2[0]; |
| 824 |
} |
| 825 |
ast_channel_unlock(chan); |
| 826 |
|
| 827 |
|
| 828 |
if (speech->state == AST_SPEECH_STATE_NOT_READY || speech->state == AST_SPEECH_STATE_DONE) { |
| 829 |
ast_speech_change_state(speech, AST_SPEECH_STATE_NOT_READY); |
| 830 |
ast_speech_start(speech); |
| 831 |
} |
| 832 |
|
| 833 |
|
| 834 |
ast_stopstream(chan); |
| 835 |
|
| 836 |
|
| 837 |
while (done == 0) { |
| 838 |
|
| 839 |
if (!quieted |
| 840 |
&& ast_channel_streamid(chan) == -1 |
| 841 |
&& ast_channel_timingfunc(chan) == NULL |
| 842 |
&& (filename = ast_strsep(&filename_tmp, '&', AST_STRSEP_STRIP | AST_STRSEP_TRIM))) { |
| 843 |
|
| 844 |
ast_stopstream(chan); |
| 845 |
|
| 846 |
speech_streamfile(chan, filename, ast_channel_language(chan)); |
| 847 |
} |
| 848 |
|
| 849 |
|
| 850 |
ast_sched_runq(ast_channel_sched(chan)); |
| 851 |
|
| 852 |
|
| 853 |
res = ast_sched_wait(ast_channel_sched(chan)); |
| 854 |
if (res < 0) |
| 855 |
res = 1000; |
| 856 |
|
| 857 |
|
| 858 |
if (ast_waitfor(chan, res) > 0) { |
| 859 |
f = ast_read(chan); |
| 860 |
if (f == NULL) { |
| 861 |
|
| 862 |
done = 3; |
| 863 |
break; |
| 864 |
} |
| 865 |
} |
| 866 |
|
| 867 |
|
| 868 |
if ((!quieted || strlen(dtmf)) && started == 1) { |
| 869 |
current = ast_tvnow(); |
| 870 |
if ((ast_tvdiff_ms(current, start)) >= timeout) { |
| 871 |
done = 1; |
| 872 |
if (f) |
| 873 |
ast_frfree(f); |
| 874 |
break; |
| 875 |
} |
| 876 |
} |
| 877 |
|
| 878 |
|
| 879 |
ast_mutex_lock(&speech->lock); |
| 880 |
if (ast_test_flag(speech, AST_SPEECH_QUIET)) { |
| 881 |
if (ast_channel_stream(chan)) |
| 882 |
ast_stopstream(chan); |
| 883 |
ast_clear_flag(speech, AST_SPEECH_QUIET); |
| 884 |
quieted = 1; |
| 885 |
} |
| 886 |
|
| 887 |
switch (speech->state) { |
| 888 |
case AST_SPEECH_STATE_READY: |
| 889 |
|
| 890 |
if (ast_channel_streamid(chan) == -1 && ast_channel_timingfunc(chan) == NULL) |
| 891 |
ast_stopstream(chan); |
| 892 |
if (!quieted && ast_channel_stream(chan) == NULL && timeout && started == 0 && !filename_tmp) { |
| 893 |
if (timeout == -1) { |
| 894 |
done = 1; |
| 895 |
if (f) |
| 896 |
ast_frfree(f); |
| 897 |
break; |
| 898 |
} |
| 899 |
start = ast_tvnow(); |
| 900 |
started = 1; |
| 901 |
} |
| 902 |
|
| 903 |
if (!strlen(dtmf) && f != NULL && f->frametype == AST_FRAME_VOICE) { |
| 904 |
ast_speech_write(speech, f->data.ptr, f->datalen); |
| 905 |
} |
| 906 |
break; |
| 907 |
case AST_SPEECH_STATE_WAIT: |
| 908 |
|
| 909 |
if (!strlen(dtmf)) { |
| 910 |
if (ast_channel_stream(chan) == NULL) { |
| 911 |
if (speech->processing_sound != NULL) { |
| 912 |
if (strlen(speech->processing_sound) > 0 && strcasecmp(speech->processing_sound, "none")) { |
| 913 |
speech_streamfile(chan, speech->processing_sound, ast_channel_language(chan)); |
| 914 |
} |
| 915 |
} |
| 916 |
} else if (ast_channel_streamid(chan) == -1 && ast_channel_timingfunc(chan) == NULL) { |
| 917 |
ast_stopstream(chan); |
| 918 |
if (speech->processing_sound != NULL) { |
| 919 |
if (strlen(speech->processing_sound) > 0 && strcasecmp(speech->processing_sound, "none")) { |
| 920 |
speech_streamfile(chan, speech->processing_sound, ast_channel_language(chan)); |
| 921 |
} |
| 922 |
} |
| 923 |
} |
| 924 |
} |
| 925 |
break; |
| 926 |
case AST_SPEECH_STATE_DONE: |
| 927 |
|
| 928 |
ast_speech_change_state(speech, AST_SPEECH_STATE_NOT_READY); |
| 929 |
if (!strlen(dtmf)) { |
| 930 |
|
| 931 |
speech->results = ast_speech_results_get(speech); |
| 932 |
|
| 933 |
done = 1; |
| 934 |
|
| 935 |
if (ast_channel_stream(chan) != NULL) { |
| 936 |
ast_stopstream(chan); |
| 937 |
} |
| 938 |
} |
| 939 |
break; |
| 940 |
default: |
| 941 |
break; |
| 942 |
} |
| 943 |
ast_mutex_unlock(&speech->lock); |
| 944 |
|
| 945 |
|
| 946 |
if (f != NULL) { |
| 947 |
|
| 948 |
switch (f->frametype) { |
| 949 |
case AST_FRAME_DTMF: |
| 950 |
if (dtmf_terminator != '\0' && f->subclass.integer == dtmf_terminator) { |
| 951 |
done = 1; |
| 952 |
} else { |
| 953 |
quieted = 1; |
| 954 |
if (ast_channel_stream(chan) != NULL) { |
| 955 |
ast_stopstream(chan); |
| 956 |
} |
| 957 |
if (!started) { |
| 958 |
|
| 959 |
timeout = (ast_channel_pbx(chan) && ast_channel_pbx(chan)->dtimeoutms) ? ast_channel_pbx(chan)->dtimeoutms : 5000; |
| 960 |
started = 1; |
| 961 |
} |
| 962 |
start = ast_tvnow(); |
| 963 |
snprintf(tmp, sizeof(tmp), "%c", f->subclass.integer); |
| 964 |
strncat(dtmf, tmp, sizeof(dtmf) - strlen(dtmf) - 1); |
| 965 |
|
| 966 |
if (max_dtmf_len && strlen(dtmf) == max_dtmf_len) |
| 967 |
done = 1; |
| 968 |
} |
| 969 |
break; |
| 970 |
case AST_FRAME_CONTROL: |
| 971 |
switch (f->subclass.integer) { |
| 972 |
case AST_CONTROL_HANGUP: |
| 973 |
|
| 974 |
done = 3; |
| 975 |
default: |
| 976 |
break; |
| 977 |
} |
| 978 |
default: |
| 979 |
break; |
| 980 |
} |
| 981 |
ast_frfree(f); |
| 982 |
f = NULL; |
| 983 |
} |
| 984 |
} |
| 985 |
|
| 986 |
if (ast_strlen_zero(dtmf) && speech->state == AST_SPEECH_STATE_READY && ast_test_flag(&options, SB_OPT_PARTIALRESULTS)) { |
| 987 |
|
| 988 |
speech->results = ast_speech_results_get(speech); |
| 989 |
} else if (!ast_strlen_zero(dtmf)) { |
| 990 |
|
| 991 |
speech->results = ast_calloc(1, sizeof(*speech->results)); |
| 992 |
if (speech->results != NULL) { |
| 993 |
ast_speech_dtmf(speech, dtmf); |
| 994 |
speech->results->score = 1000; |
| 995 |
speech->results->text = ast_strdup(dtmf); |
| 996 |
speech->results->grammar = ast_strdup("dtmf"); |
| 997 |
} |
| 998 |
ast_speech_change_state(speech, AST_SPEECH_STATE_NOT_READY); |
| 999 |
} |
| 1000 |
|
| 1001 |
|
| 1002 |
if (done == 3) { |
| 1003 |
speech_datastore_destroy(chan); |
| 1004 |
} else { |
| 1005 |
|
| 1006 |
ast_set_read_format(chan, oldreadformat); |
| 1007 |
} |
| 1008 |
|
| 1009 |
return 0; |
| 1010 |
} |
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
static int speech_destroy(struct ast_channel *chan, const char *data) |
| 1015 |
{ |
| 1016 |
if (!chan) { |
| 1017 |
return -1; |
| 1018 |
} |
| 1019 |
return speech_datastore_destroy(chan); |
| 1020 |
} |
| 1021 |
|
| 1022 |
static int unload_module(void) |
| 1023 |
{ |
| 1024 |
int res = 0; |
| 1025 |
|
| 1026 |
res = ast_unregister_application("SpeechCreate"); |
| 1027 |
res |= ast_unregister_application("SpeechLoadGrammar"); |
| 1028 |
res |= ast_unregister_application("SpeechUnloadGrammar"); |
| 1029 |
res |= ast_unregister_application("SpeechActivateGrammar"); |
| 1030 |
res |= ast_unregister_application("SpeechDeactivateGrammar"); |
| 1031 |
res |= ast_unregister_application("SpeechStart"); |
| 1032 |
res |= ast_unregister_application("SpeechBackground"); |
| 1033 |
res |= ast_unregister_application("SpeechDestroy"); |
| 1034 |
res |= ast_unregister_application("SpeechProcessingSound"); |
| 1035 |
res |= ast_custom_function_unregister(&speech_function); |
| 1036 |
res |= ast_custom_function_unregister(&speech_score_function); |
| 1037 |
res |= ast_custom_function_unregister(&speech_text_function); |
| 1038 |
res |= ast_custom_function_unregister(&speech_grammar_function); |
| 1039 |
res |= ast_custom_function_unregister(&speech_engine_function); |
| 1040 |
res |= ast_custom_function_unregister(&speech_results_type_function); |
| 1041 |
|
| 1042 |
return res; |
| 1043 |
} |
| 1044 |
|
| 1045 |
static int load_module(void) |
| 1046 |
{ |
| 1047 |
int res = 0; |
| 1048 |
|
| 1049 |
res = ast_register_application_xml("SpeechCreate", speech_create); |
| 1050 |
res |= ast_register_application_xml("SpeechLoadGrammar", speech_load); |
| 1051 |
res |= ast_register_application_xml("SpeechUnloadGrammar", speech_unload); |
| 1052 |
res |= ast_register_application_xml("SpeechActivateGrammar", speech_activate); |
| 1053 |
res |= ast_register_application_xml("SpeechDeactivateGrammar", speech_deactivate); |
| 1054 |
res |= ast_register_application_xml("SpeechStart", speech_start); |
| 1055 |
res |= ast_register_application_xml("SpeechBackground", speech_background); |
| 1056 |
res |= ast_register_application_xml("SpeechDestroy", speech_destroy); |
| 1057 |
res |= ast_register_application_xml("SpeechProcessingSound", speech_processing_sound); |
| 1058 |
res |= ast_custom_function_register(&speech_function); |
| 1059 |
res |= ast_custom_function_register(&speech_score_function); |
| 1060 |
res |= ast_custom_function_register(&speech_text_function); |
| 1061 |
res |= ast_custom_function_register(&speech_grammar_function); |
| 1062 |
res |= ast_custom_function_register(&speech_engine_function); |
| 1063 |
res |= ast_custom_function_register(&speech_results_type_function); |
| 1064 |
|
| 1065 |
return res; |
| 1066 |
} |
| 1067 |
|
| 1068 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Dialplan Speech Applications", |
| 1069 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 1070 |
.load = load_module, |
| 1071 |
.unload = unload_module, |
| 1072 |
.requires = "res_speech", |
| 1073 |
); |