Changeset 215

Show
Ignore:
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
  • app_mp4/app_mp4.c

    r213 r215  
    2626 * 
    2727 * \brief MP4 application -- save and play mp4 files 
    28  *  
     28 * 
    2929 * \ingroup applications 
    3030 */ 
     
    4444#include <asterisk/pbx.h> 
    4545#include <asterisk/module.h> 
     46#include "asterisk/options.h" 
     47#include "asterisk/config.h" 
     48#include "asterisk/utils.h" 
     49#include "asterisk/app.h" 
    4650 
    4751#ifndef AST_FORMAT_AMRNB 
    4852#define AST_FORMAT_AMRNB        (1 << 13) 
    49 #endif  
     53#endif 
    5054 
    5155static char *app_play = "mp4play"; 
    5256static char *syn_play = "MP4 file playblack"; 
    53 static char *des_play = "  mp4play(filename):  Play mp4 file to user. \n" 
     57static 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" 
    5463        "\n" 
    5564        "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"; 
    5770 
    5871 
     
    90103}; 
    91104 
     105 
     106enum { 
     107        OPT_DFTMINTOVAR         =       (1 << 0), 
     108        OPT_NOOFDTMF            =       (1 << 1), 
     109        OPT_STOPDTMF            =       (1 << 2), 
     110} mp4play_exec_option_flags; 
     111 
     112enum { 
     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 
     120AST_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 
    92129//void dump_buffer_hex(const unsigned char * text, const unsigned char * buff, int len) 
    93130//{ 
     
    257294                /* Set first flag */ 
    258295                first = 1; 
    259         }  
     296        } 
    260297 
    261298        /* if it's the last */ 
     
    354391        int res = 0; 
    355392 
     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 
    356403        /* 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        } 
    359408 
    360409        ast_log(LOG_DEBUG, "mp4play %s\n", (char *)data); 
     
    366415        video.src = src; 
    367416 
     417        /* Reset dtmf buffer */ 
     418        memset(dtmfBuffer,0,MAX_DTMF_BUFFER_SIZE); 
     419 
    368420        /* Lock module */ 
    369421        u = ast_module_user_add(chan); 
    370422 
     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 
    371487        /* Open mp4 file */ 
    372         mp4 = MP4Read((char *) data, 9); 
     488        mp4 = MP4Read((char *) args.filename, 9); 
    373489 
    374490        /* If not valid */ 
    375491        if (mp4 == MP4_INVALID_FILE_HANDLE) 
    376492        { 
    377                 /* Unlock module*/ 
    378                 ast_module_user_remove(u); 
    379493                /* exit */ 
    380                 return -1; 
     494                res = -1; 
     495                goto clean; 
    381496        } 
    382497 
     
    519634                                /* If it's a dtmf */ 
    520635                                if (f->frametype == AST_FRAME_DTMF) { 
     636 
     637                                        /* Stop flag */ 
     638                                        bool stop = false; 
     639 
     640                                        /* Get DTMF char */ 
    521641                                        char dtmf[2]; 
    522                                         /* Get dtmf number */ 
    523642                                        dtmf[0] = f->subclass; 
    524643                                        dtmf[1] = 0; 
    525644 
     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                                                } 
    526666                                        /* 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)) { 
    528668                                                /* Set extension to jump */ 
    529669                                                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); 
    530682                                                /* Free frame */ 
    531683                                                ast_frfree(f); 
     
    568720 
    569721end: 
     722        /* Log end */ 
    570723        ast_log(LOG_DEBUG, "<app_mp4"); 
    571724 
     
    573726        MP4Close(mp4); 
    574727 
     728clean: 
    575729        /* Unlock module*/ 
    576730        ast_module_user_remove(u); 
     731 
     732        /* Free datra*/ 
     733        free(parse); 
    577734 
    578735        /* Exit */ 
     
    608765                /* Remove from file name */ 
    609766                *params = 0; 
    610                  
     767 
    611768                /* Increase pointer */ 
    612769                params++; 
     
    658815 
    659816                /* Check if we have to wait for video */ 
    660                 if (f->frametype == AST_FRAME_VOICE && !waitVideo)  
     817                if (f->frametype == AST_FRAME_VOICE && !waitVideo) 
    661818                { 
    662819                        /* Check if we have the audio track */ 
    663                         if (audio == -1)  
     820                        if (audio == -1) 
    664821                        { 
    665822                                /* Check codec */ 
    666                                 if (f->subclass & AST_FORMAT_ULAW)  
     823                                if (f->subclass & AST_FORMAT_ULAW) 
    667824                                { 
    668825                                        /* Create audio track */ 
     
    725882 
    726883                        /* Check codec */ 
    727                         if (f->subclass & AST_FORMAT_H263)  
     884                        if (f->subclass & AST_FORMAT_H263) 
    728885                        { 
    729886                                /* Check if it's an intra frame */ 
     
    755912                                        prependBuffer = "\0\0"; 
    756913                                        prependLength = 2; 
    757                                 }  
     914                                } 
    758915                        } else if (f->subclass & AST_FORMAT_H264) { 
    759916                                /* Get packet type */ 
     
    797954 
    798955                        /* Check if we have the video track */ 
    799                         if (video == -1)  
     956                        if (video == -1) 
    800957                        { 
    801958                                /* Check codec */ 
    802                                 if (f->subclass & AST_FORMAT_H263)  
     959                                if (f->subclass & AST_FORMAT_H263) 
    803960                                { 
    804961                                        /* Create video track */ 
     
    831988                                        MP4SetHintTrackRtpPayload(mp4, hintVideo, "H264", &type, 0, NULL, 1, 0); 
    832989 
    833                                 }  
     990                                } 
    834991 
    835992                                /* Set struct info */ 
     
    8651022                                break; 
    8661023                        } 
    867                                  
     1024 
    8681025                } 
    8691026 
Copyright 2006 - Sergio García Murillo
Powered by Trac - Edgewall Software