app_queue: Cleanup queue_ref / queue_unref routines.
[asterisk/asterisk.git] / apps / app_record.c
index 69bbd53..c4de981 100644 (file)
  *
  * \ingroup applications
  */
-#include "asterisk.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+/*** MODULEINFO
+       <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
 
 #include "asterisk/file.h"
 #include "asterisk/pbx.h"
@@ -35,6 +37,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/app.h"
 #include "asterisk/channel.h"
 #include "asterisk/dsp.h"      /* use dsp routines for silence detection */
+#include "asterisk/format_cache.h"
+#include "asterisk/paths.h"
 
 /*** DOCUMENTATION
        <application name="Record" language="en_US">
@@ -63,6 +67,10 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                                        <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>
@@ -72,12 +80,18 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                                        <option name="t">
                                                <para>use alternate '*' terminator key (DTMF) instead of default '#'</para>
                                        </option>
+                                       <option name="u">
+                                               <para>Don't truncate recorded silence.</para>
+                                       </option>
                                        <option name="x">
                                                <para>Ignore all terminator keys (DTMF) and keep recording until hangup.</para>
                                        </option>
                                        <option name="k">
-                                               <para>Keep recording if channel hangs up.</para>
-                                       </option>       
+                                               <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>
@@ -89,7 +103,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
                        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>
+                                       <para>Will be set to the final filename of the recording, without an extension.</para>
                                </variable>
                                <variable name="RECORD_STATUS">
                                        <para>This is the final status of the command</para>
@@ -106,6 +120,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
  ***/
 
+#define OPERATOR_KEY '0'
+
 static char *app = "Record";
 
 enum {
@@ -116,42 +132,114 @@ enum {
        OPTION_STAR_TERMINATE = (1 << 4),
        OPTION_IGNORE_TERMINATE = (1 << 5),
        OPTION_KEEP = (1 << 6),
-       FLAG_HAS_PERCENT = (1 << 7),
+       OPTION_ANY_TERMINATE = (1 << 7),
+       OPTION_OPERATOR_EXIT = (1 << 8),
+       OPTION_NO_TRUNCATE = (1 << 9),
+};
+
+enum dtmf_response {
+       RESPONSE_NO_MATCH = 0,
+       RESPONSE_OPERATOR,
+       RESPONSE_DTMF,
 };
 
 AST_APP_OPTIONS(app_opts,{
        AST_APP_OPTION('a', OPTION_APPEND),
-       AST_APP_OPTION('k', OPTION_KEEP),       
+       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('u', OPTION_NO_TRUNCATE),
+       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
+ *
+ * \returns One of enum dtmf_response
+ */
+static enum dtmf_response 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))) {
+               return RESPONSE_OPERATOR;
+       }
+
+       if ((dtmf_integer == terminator) ||
+               (ast_test_flag(flags, OPTION_ANY_TERMINATE))) {
+               return RESPONSE_DTMF;
+       }
+
+       return RESPONSE_NO_MATCH;
+}
+
+static int create_destination_directory(const char *path)
+{
+       int res;
+       char directory[PATH_MAX], *file_sep;
+
+       if (!(file_sep = strrchr(path, '/'))) {
+               /* No directory to create */
+               return 0;
+       }
+
+       /* Overwrite temporarily */
+       *file_sep = '\0';
+
+       /* Absolute path? */
+       if (path[0] == '/') {
+               res = ast_mkdir(path, 0777);
+               *file_sep = '/';
+               return res;
+       }
+
+       /* Relative path */
+       res = snprintf(directory, sizeof(directory), "%s/sounds/%s",
+                                  ast_config_AST_DATA_DIR, path);
+
+       *file_sep = '/';
+
+       if (res >= sizeof(directory)) {
+               /* We truncated, so we fail */
+               return -1;
+       }
+
+       return ast_mkdir(directory, 0777);
+}
+
+static int record_exec(struct ast_channel *chan, const char *data)
 {
        int res = 0;
-       int count = 0;
        char *ext = NULL, *opts[0];
-       char *parse, *dir, *file;
+       char *parse;
        int i = 0;
-       char tmp[256];
+       char tmp[PATH_MAX];
 
        struct ast_filestream *s = NULL;
        struct ast_frame *f = NULL;
-       
+
        struct ast_dsp *sildet = NULL;          /* silence detector dsp */
        int totalsilence = 0;
        int dspsilence = 0;
        int silence = 0;                /* amount of silence to allow */
        int gotsilence = 0;             /* did we timeout for silence? */
+       int truncate_silence = 1;       /* truncate on complete silence recording */
        int maxduration = 0;            /* max duration of recording in milliseconds */
        int gottimeout = 0;             /* did we timeout for maxduration exceeded? */
        int terminator = '#';
-       int rfmt = 0;
+       RAII_VAR(struct ast_format *, rfmt, NULL, ao2_cleanup);
        int ioflags;
-       int waitres;
        struct ast_silence_generator *silgen = NULL;
        struct ast_flags flags = { 0, };
        AST_DECLARE_APP_ARGS(args,
@@ -160,7 +248,10 @@ static int record_exec(struct ast_channel *chan, void *data)
                AST_APP_ARG(maxduration);
                AST_APP_ARG(options);
        );
-       
+       int ms;
+       struct timeval start;
+       const char *status_response = "ERROR";
+
        /* 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");
@@ -174,8 +265,6 @@ static int record_exec(struct ast_channel *chan, void *data)
                ast_app_parse_options(app_opts, &flags, opts, args.options);
 
        if (!ast_strlen_zero(args.filename)) {
-               if (strstr(args.filename, "%d"))
-                       ast_set_flag(&flags, FLAG_HAS_PERCENT);
                ext = strrchr(args.filename, '.'); /* to support filename with a . in the filename, not format */
                if (!ext)
                        ext = strchr(args.filename, ':');
@@ -190,15 +279,18 @@ static int record_exec(struct ast_channel *chan, void *data)
                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);
                }
        }
-       
+
+       if (ast_test_flag(&flags, OPTION_NO_TRUNCATE))
+               truncate_silence = 0;
+
        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))
@@ -210,47 +302,40 @@ static int record_exec(struct ast_channel *chan, void *data)
        if (ast_test_flag(&flags, OPTION_IGNORE_TERMINATE))
                terminator = '\0';
 
-       /* done parsing */
+       /*
+         If a '%d' is specified as part of the filename, we replace that token with
+         sequentially incrementing numbers until we find a unique filename.
+       */
+       if (strchr(args.filename, '%')) {
+               size_t src, dst, count = 0;
+               size_t src_len = strlen(args.filename);
+               size_t dst_len = sizeof(tmp) - 1;
 
-       /* these are to allow the use of the %d in the config file for a wild card of sort to
-         create a new file with the inputed name scheme */
-       if (ast_test_flag(&flags, FLAG_HAS_PERCENT)) {
-               AST_DECLARE_APP_ARGS(fname,
-                       AST_APP_ARG(piece)[100];
-               );
-               char *tmp2 = ast_strdupa(args.filename);
-               char countstring[15];
-               int idx;
-
-               /* Separate each piece out by the format specifier */
-               AST_NONSTANDARD_APP_ARGS(fname, tmp2, '%');
                do {
-                       int tmplen;
-                       /* First piece has no leading percent, so it's copied verbatim */
-                       ast_copy_string(tmp, fname.piece[0], sizeof(tmp));
-                       tmplen = strlen(tmp);
-                       for (idx = 1; idx < fname.argc; idx++) {
-                               if (fname.piece[idx][0] == 'd') {
-                                       /* Substitute the count */
-                                       snprintf(countstring, sizeof(countstring), "%d", count);
-                                       ast_copy_string(tmp + tmplen, countstring, sizeof(tmp) - tmplen);
-                                       tmplen += strlen(countstring);
-                               } else if (tmplen + 2 < sizeof(tmp)) {
-                                       /* Unknown format specifier - just copy it verbatim */
-                                       tmp[tmplen++] = '%';
-                                       tmp[tmplen++] = fname.piece[idx][0];
+                       for (src = 0, dst = 0; src < src_len && dst < dst_len; src++) {
+                               if (!strncmp(&args.filename[src], "%d", 2)) {
+                                       int s = snprintf(&tmp[dst], PATH_MAX - dst, "%zu", count);
+                                       if (s >= PATH_MAX - dst) {
+                                               /* We truncated, so we need to bail */
+                                               ast_log(LOG_WARNING, "Failed to create unique filename from template: %s\n", args.filename);
+                                               pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
+                                               return -1;
+                                       }
+                                       dst += s;
+                                       src++;
+                               } else {
+                                       tmp[dst] = args.filename[src];
+                                       tmp[++dst] = '\0';
                                }
-                               /* Copy the remaining portion of the piece */
-                               ast_copy_string(tmp + tmplen, &(fname.piece[idx][1]), sizeof(tmp) - tmplen);
                        }
                        count++;
-               } while (ast_fileexists(tmp, ext, chan->language) > 0);
-               pbx_builtin_setvar_helper(chan, "RECORDED_FILE", tmp);
+               } while (ast_fileexists(tmp, ext, ast_channel_language(chan)) > 0);
        } else
                ast_copy_string(tmp, args.filename, sizeof(tmp));
-       /* end of routine mentioned */
 
-       if (chan->_state != AST_STATE_UP) {
+       pbx_builtin_setvar_helper(chan, "RECORDED_FILE", tmp);
+
+       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");
@@ -262,18 +347,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);
-               pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
+               ast_log(LOG_WARNING, "Could not answer channel '%s'\n", ast_channel_name(chan));
+               status_response = "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);
        }
@@ -281,8 +366,8 @@ 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);
+               rfmt = ao2_bump(ast_channel_readformat(chan));
+               res = ast_set_read_format(chan, ast_format_slin);
                if (res < 0) {
                        ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n");
                        pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
@@ -295,20 +380,20 @@ static int record_exec(struct ast_channel *chan, void *data)
                        return -1;
                }
                ast_dsp_set_threshold(sildet, ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE));
-       } 
+       }
 
-       /* Create the directory if it does not exist. */
-       dir = ast_strdupa(tmp);
-       if ((file = strrchr(dir, '/')))
-               *file++ = '\0';
-       ast_mkdir (dir, 0777);
+       if (create_destination_directory(tmp)) {
+               ast_log(LOG_WARNING, "Could not create directory for file %s\n", args.filename);
+               status_response = "ERROR";
+               goto out;
+       }
 
        ioflags = ast_test_flag(&flags, OPTION_APPEND) ? O_CREAT|O_APPEND|O_WRONLY : O_CREAT|O_TRUNC|O_WRONLY;
        s = ast_writefile(tmp, ext, NULL, ioflags, 0, AST_FILE_MODE);
 
        if (!s) {
                ast_log(LOG_WARNING, "Could not create file %s\n", args.filename);
-               pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "ERROR");
+               status_response = "ERROR";
                goto out;
        }
 
@@ -321,14 +406,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;
-                               pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "TIMEOUT");
-                               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);
@@ -342,7 +428,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");
+                               status_response = "ERROR";
                                break;
                        }
 
@@ -358,7 +444,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");
+                                       status_response = "SILENCE";
                                        break;
                                }
                        }
@@ -367,32 +453,57 @@ 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");
+                               status_response = "ERROR";
+                               ast_frfree(f);
+                               break;
+                       }
+               } else if (f->frametype == AST_FRAME_DTMF) {
+                       enum dtmf_response rc =
+                               record_dtmf_response(chan, &flags, f->subclass.integer, terminator);
+                       switch(rc)      {
+                       case RESPONSE_NO_MATCH:
+                               break;
+                       case RESPONSE_OPERATOR:
+                               status_response = "OPERATOR";
+                               ast_debug(1, "Got OPERATOR\n");
+                               break;
+                       case RESPONSE_DTMF:
+                               status_response = "DTMF";
+                               ast_debug(1, "Got DTMF\n");
+                               break;
+                       }
+                       if (rc != RESPONSE_NO_MATCH) {
                                ast_frfree(f);
                                break;
                        }
-               } else if ((f->frametype == AST_FRAME_DTMF) &&
-                   (f->subclass == terminator)) {
-                       ast_frfree(f);
-                       pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "DTMF");
-                       break;
                }
                ast_frfree(f);
        }
+
+       if (maxduration > 0 && !ms) {
+               gottimeout = 1;
+               status_response = "TIMEOUT";
+       }
+
        if (!f) {
                ast_debug(1, "Got hangup\n");
                res = -1;
-               pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "HANGUP");
+               status_response = "HANGUP";
                if (!ast_test_flag(&flags, OPTION_KEEP)) {
                        ast_filedelete(args.filename, NULL);
                }
        }
 
-       if (gotsilence) {
+       if (gotsilence && truncate_silence) {
                ast_stream_rewind(s, silence - 1000);
                ast_truncstream(s);
-       } else if (!gottimeout) {
-               /* Strip off the last 1/4 second of it */
+       } else if (!gottimeout && f) {
+               /*
+                * Strip off the last 1/4 second of it, if we didn't end because of a timeout,
+                * or a hangup.  This must mean we ended because of a DTMF tone and while this
+                * 1/4 second stripping is very old code the most likely explanation is that it
+                * relates to stripping a partial DTMF tone.
+                */
                ast_stream_rewind(s, 250);
                ast_truncstream(s);
        }
@@ -404,12 +515,17 @@ static int record_exec(struct ast_channel *chan, void *data)
 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 (res) {
+                       ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", ast_channel_name(chan));
+               }
+       }
+
+       if (sildet) {
+               ast_dsp_free(sildet);
        }
 
+       pbx_builtin_setvar_helper(chan, "RECORD_STATUS", status_response);
+
        return res;
 }