CHANGES: Update changes log to include r403414 entry
[asterisk/asterisk.git] / apps / app_record.c
index 6098ca7..45f1d86 100644 (file)
@@ -67,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>
@@ -113,6 +117,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
  ***/
 
+#define OPERATOR_KEY '0'
+
 static char *app = "Record";
 
 enum {
@@ -125,12 +131,14 @@ enum {
        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('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),
@@ -138,6 +146,36 @@ AST_APP_OPTIONS(app_opts,{
        AST_APP_OPTION('x', OPTION_IGNORE_TERMINATE),
 });
 
+/*!
+ * \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;
@@ -160,7 +198,6 @@ static int record_exec(struct ast_channel *chan, const char *data)
        int terminator = '#';
        struct ast_format rfmt;
        int ioflags;
-       int waitres;
        struct ast_silence_generator *silgen = NULL;
        struct ast_flags flags = { 0, };
        AST_DECLARE_APP_ARGS(args,
@@ -169,8 +206,11 @@ static int record_exec(struct ast_channel *chan, const char *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");
@@ -254,13 +294,13 @@ static int record_exec(struct ast_channel *chan, const char *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");
@@ -272,18 +312,18 @@ static int record_exec(struct ast_channel *chan, const char *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);
        }
@@ -291,7 +331,7 @@ static int record_exec(struct ast_channel *chan, const char *data)
        /* The end of beep code.  Now the recording starts */
 
        if (silence > 0) {
-               ast_format_copy(&rfmt, &chan->readformat);
+               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");
@@ -331,14 +371,15 @@ static int record_exec(struct ast_channel *chan, const char *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);
@@ -381,15 +422,20 @@ static int record_exec(struct ast_channel *chan, const char *data)
                                ast_frfree(f);
                                break;
                        }
-               } else if ((f->frametype == AST_FRAME_DTMF) &&
-                          ((f->subclass.integer == terminator) ||
-                           (ast_test_flag(&flags, OPTION_ANY_TERMINATE)))) {
-                       ast_frfree(f);
-                       pbx_builtin_setvar_helper(chan, "RECORD_STATUS", "DTMF");
-                       break;
+               } else if (f->frametype == AST_FRAME_DTMF) {
+                       if (record_dtmf_response(chan, &flags, f->subclass.integer, 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;
@@ -415,12 +461,14 @@ static int record_exec(struct ast_channel *chan, const char *data)
 out:
        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", 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);
+       }
        return res;
 }