CHANGES: Update changes log to include r403414 entry
[asterisk/asterisk.git] / apps / app_record.c
index 8afafc9..45f1d86 100644 (file)
  *
  * \ingroup applications
  */
+
+/*** MODULEINFO
+       <support_level>core</support_level>
+ ***/
  
 #include "asterisk.h"
 
@@ -36,35 +40,87 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/channel.h"
 #include "asterisk/dsp.h"      /* use dsp routines for silence detection */
 
+/*** DOCUMENTATION
+       <application name="Record" language="en_US">
+               <synopsis>
+                       Record to a file.
+               </synopsis>
+               <syntax>
+                       <parameter name="filename" required="true" argsep=".">
+                               <argument name="filename" required="true" />
+                               <argument name="format" required="true">
+                                       <para>Is the format of the file type to be recorded (wav, gsm, etc).</para>
+                               </argument>
+                       </parameter>
+                       <parameter name="silence">
+                               <para>Is the number of seconds of silence to allow before returning.</para>
+                       </parameter>
+                       <parameter name="maxduration">
+                               <para>Is the maximum recording duration in seconds. If missing
+                               or 0 there is no maximum.</para>
+                       </parameter>
+                       <parameter name="options">
+                               <optionlist>
+                                       <option name="a">
+                                               <para>Append to existing recording rather than replacing.</para>
+                                       </option>
+                                       <option name="n">
+                                               <para>Do not answer, but record anyway if line not yet answered.</para>
+                                       </option>
+                                       <option name="o">
+                                               <para>Exit when 0 is pressed, setting the variable <variable>RECORD_STATUS</variable>
+                                               to <literal>OPERATOR</literal> instead of <literal>DTMF</literal></para>
+                                       </option>
+                                       <option name="q">
+                                               <para>quiet (do not play a beep tone).</para>
+                                       </option>
+                                       <option name="s">
+                                               <para>skip recording if the line is not yet answered.</para>
+                                       </option>
+                                       <option name="t">
+                                               <para>use alternate '*' terminator key (DTMF) instead of default '#'</para>
+                                       </option>
+                                       <option name="x">
+                                               <para>Ignore all terminator keys (DTMF) and keep recording until hangup.</para>
+                                       </option>
+                                       <option name="k">
+                                               <para>Keep recorded file upon hangup.</para>
+                                       </option>
+                                       <option name="y">
+                                               <para>Terminate recording if *any* DTMF digit is received.</para>
+                                       </option>
+                               </optionlist>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>If filename contains <literal>%d</literal>, these characters will be replaced with a number
+                       incremented by one each time the file is recorded.
+                       Use <astcli>core show file formats</astcli> to see the available formats on your system
+                       User can press <literal>#</literal> to terminate the recording and continue to the next priority.
+                       If the user hangs up during a recording, all data will be lost and the application will terminate.</para>
+                       <variablelist>
+                               <variable name="RECORDED_FILE">
+                                       <para>Will be set to the final filename of the recording.</para>
+                               </variable>
+                               <variable name="RECORD_STATUS">
+                                       <para>This is the final status of the command</para>
+                                       <value name="DTMF">A terminating DTMF was received ('#' or '*', depending upon option 't')</value>
+                                       <value name="SILENCE">The maximum silence occurred in the recording.</value>
+                                       <value name="SKIP">The line was not yet answered and the 's' option was specified.</value>
+                                       <value name="TIMEOUT">The maximum length was reached.</value>
+                                       <value name="HANGUP">The channel was hung up.</value>
+                                       <value name="ERROR">An unrecoverable error occurred, which resulted in a WARNING to the logs.</value>
+                               </variable>
+                       </variablelist>
+               </description>
+       </application>
+
+ ***/
+
+#define OPERATOR_KEY '0'
 
 static char *app = "Record";
 
-static char *synopsis = "Record to a file";
-
-static char *descrip = 
-"  Record(filename.format,silence[,maxduration][,options])\n\n"
-"Records from the channel into a given filename. If the file exists it will\n"
-"be overwritten.\n"
-"- 'format' is the format of the file type to be recorded (wav, gsm, etc).\n"
-"- 'silence' is the number of seconds of silence to allow before returning.\n"
-"- 'maxduration' is the maximum recording duration in seconds. If missing\n"
-"or 0 there is no maximum.\n"
-"- 'options' may contain any of the following letters:\n"
-"     'a' : append to existing recording rather than replacing\n"
-"     'n' : do not answer, but record anyway if line not yet answered\n"
-"     'q' : quiet (do not play a beep tone)\n"
-"     's' : skip recording if the line is not yet answered\n"
-"     't' : use alternate '*' terminator key (DTMF) instead of default '#'\n"
-"     'x' : ignore all terminator keys (DTMF) and keep recording until hangup\n"
-"\n"
-"If filename contains '%d', these characters will be replaced with a number\n"
-"incremented by one each time the file is recorded. A channel variable\n"
-"named RECORDED_FILE will also be set, which contains the final filemname.\n\n"
-"Use 'core show file formats' to see the available formats on your system\n\n"
-"User can press '#' to terminate the recording and continue to the next priority.\n\n"
-"If the user should hangup during a recording, all data will be lost and the\n"
-"application will teminate. \n";
-
 enum {
        OPTION_APPEND = (1 << 0),
        OPTION_NOANSWER = (1 << 1),
@@ -72,19 +128,55 @@ enum {
        OPTION_SKIP = (1 << 3),
        OPTION_STAR_TERMINATE = (1 << 4),
        OPTION_IGNORE_TERMINATE = (1 << 5),
-       FLAG_HAS_PERCENT = (1 << 6),
+       OPTION_KEEP = (1 << 6),
+       FLAG_HAS_PERCENT = (1 << 7),
+       OPTION_ANY_TERMINATE = (1 << 8),
+       OPTION_OPERATOR_EXIT = (1 << 9),
 };
 
 AST_APP_OPTIONS(app_opts,{
        AST_APP_OPTION('a', OPTION_APPEND),
+       AST_APP_OPTION('k', OPTION_KEEP),
        AST_APP_OPTION('n', OPTION_NOANSWER),
+       AST_APP_OPTION('o', OPTION_OPERATOR_EXIT),
        AST_APP_OPTION('q', OPTION_QUIET),
        AST_APP_OPTION('s', OPTION_SKIP),
        AST_APP_OPTION('t', OPTION_STAR_TERMINATE),
+       AST_APP_OPTION('y', OPTION_ANY_TERMINATE),
        AST_APP_OPTION('x', OPTION_IGNORE_TERMINATE),
 });
 
-static int record_exec(struct ast_channel *chan, void *data)
+/*!
+ * \internal
+ * \brief Used to determine what action to take when DTMF is received while recording
+ * \since 13.0.0
+ *
+ * \param chan channel being recorded
+ * \param flags option flags in use by the record application
+ * \param dtmf_integer the integer value of the DTMF key received
+ * \param terminator key currently set to be pressed for normal termination
+ *
+ * \retval 0 do not exit
+ * \retval -1 do exit
+ */
+static int record_dtmf_response(struct ast_channel *chan, struct ast_flags *flags, int dtmf_integer, int terminator)
+{
+       if ((dtmf_integer == OPERATOR_KEY) &&
+               (ast_test_flag(flags, OPTION_OPERATOR_EXIT))) {
+               pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "OPERATOR");
+               return -1;
+       }
+
+       if ((dtmf_integer == terminator) ||
+               (ast_test_flag(flags, OPTION_ANY_TERMINATE))) {
+               pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "DTMF");
+               return -1;
+       }
+
+       return 0;
+}
+
+static int record_exec(struct ast_channel *chan, const char *data)
 {
        int res = 0;
        int count = 0;
@@ -104,9 +196,8 @@ static int record_exec(struct ast_channel *chan, void *data)
        int maxduration = 0;            /* max duration of recording in milliseconds */
        int gottimeout = 0;             /* did we timeout for maxduration exceeded? */
        int terminator = '#';
-       int rfmt = 0;
+       struct ast_format rfmt;
        int ioflags;
-       int waitres;
        struct ast_silence_generator *silgen = NULL;
        struct ast_flags flags = { 0, };
        AST_DECLARE_APP_ARGS(args,
@@ -115,10 +206,15 @@ static int record_exec(struct ast_channel *chan, void *data)
                AST_APP_ARG(maxduration);
                AST_APP_ARG(options);
        );
-       
+       int ms;
+       struct timeval start;
+
+       ast_format_clear(&rfmt);
+
        /* The next few lines of code parse out the filename and header from the input string */
        if (ast_strlen_zero(data)) { /* no data implies no filename or anything is present */
                ast_log(LOG_WARNING, "Record requires an argument (filename)\n");
+               pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
                return -1;
        }
 
@@ -140,10 +236,11 @@ static int record_exec(struct ast_channel *chan, void *data)
        }
        if (!ext) {
                ast_log(LOG_WARNING, "No extension specified to filename!\n");
+               pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
                return -1;
        }
        if (args.silence) {
-               if ((sscanf(args.silence, "%d", &i) == 1) && (i > -1)) {
+               if ((sscanf(args.silence, "%30d", &i) == 1) && (i > -1)) {
                        silence = i * 1000;
                } else if (!ast_strlen_zero(args.silence)) {
                        ast_log(LOG_WARNING, "'%s' is not a valid silence duration\n", args.silence);
@@ -151,7 +248,7 @@ static int record_exec(struct ast_channel *chan, void *data)
        }
        
        if (args.maxduration) {
-               if ((sscanf(args.maxduration, "%d", &i) == 1) && (i > -1))
+               if ((sscanf(args.maxduration, "%30d", &i) == 1) && (i > -1))
                        /* Convert duration to milliseconds */
                        maxduration = i * 1000;
                else if (!ast_strlen_zero(args.maxduration))
@@ -197,15 +294,16 @@ static int record_exec(struct ast_channel *chan, void *data)
                                ast_copy_string(tmp + tmplen, &(fname.piece[idx][1]), sizeof(tmp) - tmplen);
                        }
                        count++;
-               } while (ast_fileexists(tmp, ext, chan->language) > 0);
+               } while (ast_fileexists(tmp, ext, ast_channel_language(chan)) > 0);
                pbx_builtin_setvar_helper(chan, "RECORDED_FILE", tmp);
        } else
                ast_copy_string(tmp, args.filename, sizeof(tmp));
        /* end of routine mentioned */
 
-       if (chan->_state != AST_STATE_UP) {
+       if (ast_channel_state(chan) != AST_STATE_UP) {
                if (ast_test_flag(&flags, OPTION_SKIP)) {
                        /* At the user's option, skip if the line is not up */
+                       pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "SKIP");
                        return 0;
                } else if (!ast_test_flag(&flags, OPTION_NOANSWER)) {
                        /* Otherwise answer unless we're supposed to record while on-hook */
@@ -214,17 +312,18 @@ static int record_exec(struct ast_channel *chan, void *data)
        }
 
        if (res) {
-               ast_log(LOG_WARNING, "Could not answer channel '%s'\n", chan->name);
+               ast_log(LOG_WARNING, "Could not answer channel '%s'\n", ast_channel_name(chan));
+               pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
                goto out;
        }
 
        if (!ast_test_flag(&flags, OPTION_QUIET)) {
                /* Some code to play a nice little beep to signify the start of the record operation */
-               res = ast_streamfile(chan, "beep", chan->language);
+               res = ast_streamfile(chan, "beep", ast_channel_language(chan));
                if (!res) {
                        res = ast_waitstream(chan, "");
                } else {
-                       ast_log(LOG_WARNING, "ast_streamfile failed on %s\n", chan->name);
+                       ast_log(LOG_WARNING, "ast_streamfile failed on %s\n", ast_channel_name(chan));
                }
                ast_stopstream(chan);
        }
@@ -232,15 +331,17 @@ static int record_exec(struct ast_channel *chan, void *data)
        /* The end of beep code.  Now the recording starts */
 
        if (silence > 0) {
-               rfmt = chan->readformat;
-               res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
+               ast_format_copy(&rfmt, ast_channel_readformat(chan));
+               res = ast_set_read_format_by_id(chan, AST_FORMAT_SLINEAR);
                if (res < 0) {
                        ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
+                       pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
                        return -1;
                }
                sildet = ast_dsp_new();
                if (!sildet) {
                        ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
+                       pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
                        return -1;
                }
                ast_dsp_set_threshold(sildet, ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE));
@@ -257,6 +358,7 @@ static int record_exec(struct ast_channel *chan, void *data)
 
        if (!s) {
                ast_log(LOG_WARNING, "Could not create file %s\n", args.filename);
+               pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
                goto out;
        }
 
@@ -269,13 +371,15 @@ static int record_exec(struct ast_channel *chan, void *data)
        if (maxduration <= 0)
                maxduration = -1;
 
-       while ((waitres = ast_waitfor(chan, maxduration)) > -1) {
-               if (maxduration > 0) {
-                       if (waitres == 0) {
-                               gottimeout = 1;
-                               break;
-                       }
-                       maxduration = waitres;
+       start = ast_tvnow();
+       while ((ms = ast_remaining_ms(start, maxduration))) {
+               ms = ast_waitfor(chan, ms);
+               if (ms < 0) {
+                       break;
+               }
+
+               if (maxduration > 0 && ms == 0) {
+                       break;
                }
 
                f = ast_read(chan);
@@ -289,6 +393,7 @@ static int record_exec(struct ast_channel *chan, void *data)
                        if (res) {
                                ast_log(LOG_WARNING, "Problem writing frame\n");
                                ast_frfree(f);
+                               pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
                                break;
                        }
 
@@ -304,6 +409,7 @@ static int record_exec(struct ast_channel *chan, void *data)
                                        /* Ended happily with silence */
                                        ast_frfree(f);
                                        gotsilence = 1;
+                                       pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "SILENCE");
                                        break;
                                }
                        }
@@ -312,19 +418,31 @@ static int record_exec(struct ast_channel *chan, void *data)
 
                        if (res) {
                                ast_log(LOG_WARNING, "Problem writing frame\n");
+                               pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
+                               ast_frfree(f);
+                               break;
+                       }
+               } else if (f->frametype == AST_FRAME_DTMF) {
+                       if (record_dtmf_response(chan, &flags, f->subclass.integer, terminator)) {
                                ast_frfree(f);
                                break;
                        }
-               } else if ((f->frametype == AST_FRAME_DTMF) &&
-                   (f->subclass == terminator)) {
-                       ast_frfree(f);
-                       break;
                }
                ast_frfree(f);
        }
+
+       if (maxduration > 0 && !ms) {
+               gottimeout = 1;
+               pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "TIMEOUT");
+       }
+
        if (!f) {
                ast_debug(1, "Got hangup\n");
                res = -1;
+               pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "HANGUP");
+               if (!ast_test_flag(&flags, OPTION_KEEP)) {
+                       ast_filedelete(args.filename, NULL);
+               }
        }
 
        if (gotsilence) {
@@ -341,14 +459,16 @@ static int record_exec(struct ast_channel *chan, void *data)
                ast_channel_stop_silence_generator(chan, silgen);
 
 out:
-       if ((silence > 0) && rfmt) {
-               res = ast_set_read_format(chan, rfmt);
-               if (res)
-                       ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", chan->name);
-               if (sildet)
-                       ast_dsp_free(sildet);
+       if ((silence > 0) && rfmt.id) {
+               res = ast_set_read_format(chan, &rfmt);
+               if (res) {
+                       ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", ast_channel_name(chan));
+               }
        }
 
+       if (sildet) {
+               ast_dsp_free(sildet);
+       }
        return res;
 }
 
@@ -359,7 +479,7 @@ static int unload_module(void)
 
 static int load_module(void)
 {
-       return ast_register_application(app, record_exec, synopsis, descrip);
+       return ast_register_application_xml(app, record_exec);
 }
 
 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Trivial Record Application");