Changeset 215
- Timestamp:
- 05/19/08 12:22:08
(3 months ago)
- Author:
- sip
- Message:
Added more digits to mp4 and variable set (Ticket #12) Warning: Untested.
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r213 |
r215 |
|
| 26 | 26 | * |
|---|
| 27 | 27 | * \brief MP4 application -- save and play mp4 files |
|---|
| 28 | | * |
|---|
| | 28 | * |
|---|
| 29 | 29 | * \ingroup applications |
|---|
| 30 | 30 | */ |
|---|
| … | … | |
| 44 | 44 | #include <asterisk/pbx.h> |
|---|
| 45 | 45 | #include <asterisk/module.h> |
|---|
| | 46 | #include "asterisk/options.h" |
|---|
| | 47 | #include "asterisk/config.h" |
|---|
| | 48 | #include "asterisk/utils.h" |
|---|
| | 49 | #include "asterisk/app.h" |
|---|
| 46 | 50 | |
|---|
| 47 | 51 | #ifndef AST_FORMAT_AMRNB |
|---|
| 48 | 52 | #define AST_FORMAT_AMRNB (1 << 13) |
|---|
| 49 | | #endif |
|---|
| | 53 | #endif |
|---|
| 50 | 54 | |
|---|
| 51 | 55 | static char *app_play = "mp4play"; |
|---|
| 52 | 56 | static char *syn_play = "MP4 file playblack"; |
|---|
| 53 | | static char *des_play = " mp4play(filename): Play mp4 file to user. \n" |
|---|
| | 57 | static char *des_play = " mp4play(filename,[options]): Play mp4 file to user. \n" |
|---|
| | 58 | "\n" |
|---|
| | 59 | "Available options:\n" |
|---|
| | 60 | " 'n(x)': number of digits (x) to wait for \n" |
|---|
| | 61 | " 'S(x)': set variable of name x (with DTMFs) rather than go to extension \n" |
|---|
| | 62 | " 's(x)': set digits, which should stop playback \n" |
|---|
| 54 | 63 | "\n" |
|---|
| 55 | 64 | "Examples:\n" |
|---|
| 56 | | " mp4play(/tmp/video.mp4) play video file to user\n"; |
|---|
| | 65 | " mp4play(/tmp/video.mp4) play video file to user\n" |
|---|
| | 66 | " mp4play(/tmp/test.mp4,'n(3)') play video file to user and wait for 3 digits \n" |
|---|
| | 67 | " mp4play(/tmp/test.mp4,'n(3)S(DTMF_INPUT)') play video file to user and wait for 3 digits and \n" |
|---|
| | 68 | " set them as value of variable DTMF_INPUT\n" |
|---|
| | 69 | " mp4play(/tmp/test.mp4,'n(3)s(#)') play video file, wait for 3 digits or break on '#' \n"; |
|---|
| 57 | 70 | |
|---|
| 58 | 71 | |
|---|
| … | … | |
| 90 | 103 | }; |
|---|
| 91 | 104 | |
|---|
| | 105 | |
|---|
| | 106 | enum { |
|---|
| | 107 | OPT_DFTMINTOVAR = (1 << 0), |
|---|
| | 108 | OPT_NOOFDTMF = (1 << 1), |
|---|
| | 109 | OPT_STOPDTMF = (1 << 2), |
|---|
| | 110 | } mp4play_exec_option_flags; |
|---|
| | 111 | |
|---|
| | 112 | enum { |
|---|
| | 113 | OPT_ARG_DFTMINTOVAR = 0, |
|---|
| | 114 | OPT_ARG_NOOFDTMF, |
|---|
| | 115 | OPT_ARG_STOPDTMF, |
|---|
| | 116 | /* note: this entry _MUST_ be the last one in the enum */ |
|---|
| | 117 | OPT_ARG_ARRAY_SIZE, |
|---|
| | 118 | } mp4play_exec_option_args; |
|---|
| | 119 | |
|---|
| | 120 | AST_APP_OPTIONS(mp4play_exec_options, { |
|---|
| | 121 | AST_APP_OPTION_ARG('S', OPT_DFTMINTOVAR, OPT_ARG_DFTMINTOVAR), |
|---|
| | 122 | AST_APP_OPTION_ARG('n', OPT_NOOFDTMF, OPT_ARG_NOOFDTMF), |
|---|
| | 123 | AST_APP_OPTION_ARG('s', OPT_STOPDTMF, OPT_ARG_STOPDTMF), |
|---|
| | 124 | }); |
|---|
| | 125 | |
|---|
| | 126 | |
|---|
| | 127 | #define MAX_DTMF_BUFFER_SIZE 25 |
|---|
| | 128 | |
|---|
| 92 | 129 | //void dump_buffer_hex(const unsigned char * text, const unsigned char * buff, int len) |
|---|
| 93 | 130 | //{ |
|---|
| … | … | |
| 257 | 294 | /* Set first flag */ |
|---|
| 258 | 295 | first = 1; |
|---|
| 259 | | } |
|---|
| | 296 | } |
|---|
| 260 | 297 | |
|---|
| 261 | 298 | /* if it's the last */ |
|---|
| … | … | |
| 354 | 391 | int res = 0; |
|---|
| 355 | 392 | |
|---|
| | 393 | char *parse; |
|---|
| | 394 | int numberofDigits = -1; |
|---|
| | 395 | char *varName = NULL; |
|---|
| | 396 | char *stopChars = NULL; |
|---|
| | 397 | char dtmfBuffer[MAX_DTMF_BUFFER_SIZE]; |
|---|
| | 398 | struct ast_flags opts = { 0, }; |
|---|
| | 399 | char *opt_args[OPT_ARG_ARRAY_SIZE]; |
|---|
| | 400 | |
|---|
| | 401 | AST_DECLARE_APP_ARGS(args, AST_APP_ARG(filename); AST_APP_ARG(options);); |
|---|
| | 402 | |
|---|
| 356 | 403 | /* Check for data */ |
|---|
| 357 | | if (!data) |
|---|
| 358 | | return -1; |
|---|
| | 404 | if (!data || ast_strlen_zero(data)) { |
|---|
| | 405 | ast_log(LOG_WARNING, "mp4play requires an argument (filename)\n"); |
|---|
| | 406 | return -1; |
|---|
| | 407 | } |
|---|
| 359 | 408 | |
|---|
| 360 | 409 | ast_log(LOG_DEBUG, "mp4play %s\n", (char *)data); |
|---|
| … | … | |
| 366 | 415 | video.src = src; |
|---|
| 367 | 416 | |
|---|
| | 417 | /* Reset dtmf buffer */ |
|---|
| | 418 | memset(dtmfBuffer,0,MAX_DTMF_BUFFER_SIZE); |
|---|
| | 419 | |
|---|
| 368 | 420 | /* Lock module */ |
|---|
| 369 | 421 | u = ast_module_user_add(chan); |
|---|
| 370 | 422 | |
|---|
| | 423 | /* Duplicate input */ |
|---|
| | 424 | parse = ast_strdup(data); |
|---|
| | 425 | |
|---|
| | 426 | /* Get input data */ |
|---|
| | 427 | AST_STANDARD_APP_ARGS(args, parse); |
|---|
| | 428 | |
|---|
| | 429 | /* Parse input data */ |
|---|
| | 430 | if (!ast_strlen_zero(args.options) && |
|---|
| | 431 | ast_app_parse_options(mp4play_exec_options, &opts, opt_args, args.options)) { |
|---|
| | 432 | ast_log(LOG_WARNING, "mp4play cannot partse options\n"); |
|---|
| | 433 | res = -1; |
|---|
| | 434 | goto clean; |
|---|
| | 435 | } |
|---|
| | 436 | |
|---|
| | 437 | /* Check filename */ |
|---|
| | 438 | if (ast_strlen_zero(args.filename)) { |
|---|
| | 439 | ast_log(LOG_WARNING, "mp4play requires an argument (filename)\n"); |
|---|
| | 440 | res = -1; |
|---|
| | 441 | goto clean; |
|---|
| | 442 | } |
|---|
| | 443 | |
|---|
| | 444 | /* If we have DTMF number of digits options chek it */ |
|---|
| | 445 | if (ast_test_flag(&opts, OPT_NOOFDTMF) && !ast_strlen_zero(opt_args[OPT_ARG_NOOFDTMF])) { |
|---|
| | 446 | |
|---|
| | 447 | /* Get number of digits to wait for */ |
|---|
| | 448 | numberofDigits = atoi(opt_args[OPT_ARG_NOOFDTMF]); |
|---|
| | 449 | |
|---|
| | 450 | /* Check valid number */ |
|---|
| | 451 | if (numberofDigits<=0) { |
|---|
| | 452 | ast_log(LOG_WARNING, "mp4play does not accept n(%s), hanging up.\n", opt_args[OPT_ARG_NOOFDTMF]); |
|---|
| | 453 | res = -1; |
|---|
| | 454 | goto clean; |
|---|
| | 455 | } |
|---|
| | 456 | |
|---|
| | 457 | /* Check valid sizei */ |
|---|
| | 458 | if (numberofDigits>MAX_DTMF_BUFFER_SIZE-1) { |
|---|
| | 459 | numberofDigits = MAX_DTMF_BUFFER_SIZE-1; |
|---|
| | 460 | ast_log(LOG_WARNING, "mp4play does not accept n(%s), buffer is too short cutting to %d .\n", opt_args[OPT_ARG_NOOFDTMF],MAX_DTMF_BUFFER_SIZE-1); |
|---|
| | 461 | } |
|---|
| | 462 | |
|---|
| | 463 | if (option_verbose > 2) |
|---|
| | 464 | ast_verbose(VERBOSE_PREFIX_3 "Setting number of digits to %d seconds.\n", numberofDigits); |
|---|
| | 465 | } |
|---|
| | 466 | |
|---|
| | 467 | /* If we have DTMF set variable otpion chekc it */ |
|---|
| | 468 | if (ast_test_flag(&opts, OPT_DFTMINTOVAR) && !ast_strlen_zero(opt_args[OPT_ARG_DFTMINTOVAR])) { |
|---|
| | 469 | |
|---|
| | 470 | /* Get variable name */ |
|---|
| | 471 | varName = opt_args[OPT_ARG_DFTMINTOVAR]; |
|---|
| | 472 | |
|---|
| | 473 | if (option_verbose > 2) |
|---|
| | 474 | ast_verbose(VERBOSE_PREFIX_3 "Setting variable name to %s .\n", varName); |
|---|
| | 475 | } |
|---|
| | 476 | |
|---|
| | 477 | /* If we have DTMF stop digit optiont check it */ |
|---|
| | 478 | if (ast_test_flag(&opts, OPT_STOPDTMF) && !ast_strlen_zero(opt_args[OPT_ARG_STOPDTMF])) { |
|---|
| | 479 | |
|---|
| | 480 | /* Get stop digits */ |
|---|
| | 481 | stopChars = opt_args[OPT_ARG_STOPDTMF]; |
|---|
| | 482 | |
|---|
| | 483 | if (option_verbose > 2) |
|---|
| | 484 | ast_verbose(VERBOSE_PREFIX_3 "Stop chars are %s.\n",stopChars); |
|---|
| | 485 | } |
|---|
| | 486 | |
|---|
| 371 | 487 | /* Open mp4 file */ |
|---|
| 372 | | mp4 = MP4Read((char *) data, 9); |
|---|
| | 488 | mp4 = MP4Read((char *) args.filename, 9); |
|---|
| 373 | 489 | |
|---|
| 374 | 490 | /* If not valid */ |
|---|
| 375 | 491 | if (mp4 == MP4_INVALID_FILE_HANDLE) |
|---|
| 376 | 492 | { |
|---|
| 377 | | /* Unlock module*/ |
|---|
| 378 | | ast_module_user_remove(u); |
|---|
| 379 | 493 | /* exit */ |
|---|
| 380 | | return -1; |
|---|
| | 494 | res = -1; |
|---|
| | 495 | goto clean; |
|---|
| 381 | 496 | } |
|---|
| 382 | 497 | |
|---|
| … | … | |
| 519 | 634 | /* If it's a dtmf */ |
|---|
| 520 | 635 | if (f->frametype == AST_FRAME_DTMF) { |
|---|
| | 636 | |
|---|
| | 637 | /* Stop flag */ |
|---|
| | 638 | bool stop = false; |
|---|
| | 639 | |
|---|
| | 640 | /* Get DTMF char */ |
|---|
| 521 | 641 | char dtmf[2]; |
|---|
| 522 | | /* Get dtmf number */ |
|---|
| 523 | 642 | dtmf[0] = f->subclass; |
|---|
| 524 | 643 | dtmf[1] = 0; |
|---|
| 525 | 644 | |
|---|
| | 645 | /* Check if it's in the stop char digits */ |
|---|
| | 646 | if (stopChars && strchr(stopChars,dtmf)) { |
|---|
| | 647 | /* Clean DMTF input */ |
|---|
| | 648 | strcpy(dtmfBuffer,dtmf); |
|---|
| | 649 | /* Stop */ |
|---|
| | 650 | stop = true; |
|---|
| | 651 | /* Continue after exit */ |
|---|
| | 652 | res = 0; |
|---|
| | 653 | /* Log */ |
|---|
| | 654 | ast_log( LOG_WARNING, "mp4play - found stop char %s\n",dtmf); |
|---|
| | 655 | /* Check if we have to append the DTMF and wait for more than one digit */ |
|---|
| | 656 | } else if (numberofDigits>0) { |
|---|
| | 657 | /* Append to the DTMF buffer */ |
|---|
| | 658 | strcat(dtmfBuffer,dtmf); |
|---|
| | 659 | /* Check length */ |
|---|
| | 660 | if (strlen(dtmfBuffer)>=numberofDigits) { |
|---|
| | 661 | /* Continue after exit */ |
|---|
| | 662 | res = 0; |
|---|
| | 663 | /* Stop */ |
|---|
| | 664 | stop = true; |
|---|
| | 665 | } |
|---|
| 526 | 666 | /* Check for dtmf extension in context */ |
|---|
| 527 | | if (ast_exists_extension(chan, chan->context, dtmf, 1, NULL)) { |
|---|
| | 667 | } else if (ast_exists_extension(chan, chan->context, dtmf, 1, NULL)) { |
|---|
| 528 | 668 | /* Set extension to jump */ |
|---|
| 529 | 669 | res = f->subclass; |
|---|
| | 670 | /* Clean DMTF input */ |
|---|
| | 671 | strcpy(dtmfBuffer,dtmf); |
|---|
| | 672 | /* End */ |
|---|
| | 673 | stop = true; |
|---|
| | 674 | } |
|---|
| | 675 | |
|---|
| | 676 | /* If we have to stop */ |
|---|
| | 677 | if (stop) { |
|---|
| | 678 | /* Check DTMF variable`option*/ |
|---|
| | 679 | if (varName) |
|---|
| | 680 | /* Build variable */ |
|---|
| | 681 | pbx_builtin_setvar_helper(chan, varName, dtmfBuffer); |
|---|
| 530 | 682 | /* Free frame */ |
|---|
| 531 | 683 | ast_frfree(f); |
|---|
| … | … | |
| 568 | 720 | |
|---|
| 569 | 721 | end: |
|---|
| | 722 | /* Log end */ |
|---|
| 570 | 723 | ast_log(LOG_DEBUG, "<app_mp4"); |
|---|
| 571 | 724 | |
|---|
| … | … | |
| 573 | 726 | MP4Close(mp4); |
|---|
| 574 | 727 | |
|---|
| | 728 | clean: |
|---|
| 575 | 729 | /* Unlock module*/ |
|---|
| 576 | 730 | ast_module_user_remove(u); |
|---|
| | 731 | |
|---|
| | 732 | /* Free datra*/ |
|---|
| | 733 | free(parse); |
|---|
| 577 | 734 | |
|---|
| 578 | 735 | /* Exit */ |
|---|
| … | … | |
| 608 | 765 | /* Remove from file name */ |
|---|
| 609 | 766 | *params = 0; |
|---|
| 610 | | |
|---|
| | 767 | |
|---|
| 611 | 768 | /* Increase pointer */ |
|---|
| 612 | 769 | params++; |
|---|
| … | … | |
| 658 | 815 | |
|---|
| 659 | 816 | /* Check if we have to wait for video */ |
|---|
| 660 | | if (f->frametype == AST_FRAME_VOICE && !waitVideo) |
|---|
| | 817 | if (f->frametype == AST_FRAME_VOICE && !waitVideo) |
|---|
| 661 | 818 | { |
|---|
| 662 | 819 | /* Check if we have the audio track */ |
|---|
| 663 | | if (audio == -1) |
|---|
| | 820 | if (audio == -1) |
|---|
| 664 | 821 | { |
|---|
| 665 | 822 | /* Check codec */ |
|---|
| 666 | | if (f->subclass & AST_FORMAT_ULAW) |
|---|
| | 823 | if (f->subclass & AST_FORMAT_ULAW) |
|---|
| 667 | 824 | { |
|---|
| 668 | 825 | /* Create audio track */ |
|---|
| … | … | |
| 725 | 882 | |
|---|
| 726 | 883 | /* Check codec */ |
|---|
| 727 | | if (f->subclass & AST_FORMAT_H263) |
|---|
| | 884 | if (f->subclass & AST_FORMAT_H263) |
|---|
| 728 | 885 | { |
|---|
| 729 | 886 | /* Check if it's an intra frame */ |
|---|
| … | … | |
| 755 | 912 | prependBuffer = "\0\0"; |
|---|
| 756 | 913 | prependLength = 2; |
|---|
| 757 | | } |
|---|
| | 914 | } |
|---|
| 758 | 915 | } else if (f->subclass & AST_FORMAT_H264) { |
|---|
| 759 | 916 | /* Get packet type */ |
|---|
| … | … | |
| 797 | 954 | |
|---|
| 798 | 955 | /* Check if we have the video track */ |
|---|
| 799 | | if (video == -1) |
|---|
| | 956 | if (video == -1) |
|---|
| 800 | 957 | { |
|---|
| 801 | 958 | /* Check codec */ |
|---|
| 802 | | if (f->subclass & AST_FORMAT_H263) |
|---|
| | 959 | if (f->subclass & AST_FORMAT_H263) |
|---|
| 803 | 960 | { |
|---|
| 804 | 961 | /* Create video track */ |
|---|
| … | … | |
| 831 | 988 | MP4SetHintTrackRtpPayload(mp4, hintVideo, "H264", &type, 0, NULL, 1, 0); |
|---|
| 832 | 989 | |
|---|
| 833 | | } |
|---|
| | 990 | } |
|---|
| 834 | 991 | |
|---|
| 835 | 992 | /* Set struct info */ |
|---|
| … | … | |
| 865 | 1022 | break; |
|---|
| 866 | 1023 | } |
|---|
| 867 | | |
|---|
| | 1024 | |
|---|
| 868 | 1025 | } |
|---|
| 869 | 1026 | |
|---|
Download in other formats:
|
|