Merge "astobj2: Create function to copy weak proxied objects from container."
[asterisk/asterisk.git] / apps / app_dictate.c
index 816f15a..d184512 100644 (file)
@@ -5,7 +5,7 @@
  *
  * Anthony Minessale II <anthmct@yahoo.com>
  *
- * Donated by Sangoma Technologies <http://www.samgoma.com>
+ * Donated by Sangoma Technologies <http://www.sangoma.com>
  *
  * See http://www.asterisk.org for more information about
  * the Asterisk project. Please do not directly contact
  * \ingroup applications
  */
 
-#include "asterisk.h"
+/*** MODULEINFO
+       <support_level>extended</support_level>
+ ***/
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+#include "asterisk.h"
 
 #include <sys/stat.h>
 
@@ -39,6 +41,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/module.h"
 #include "asterisk/say.h"
 #include "asterisk/app.h"
+#include "asterisk/format_cache.h"
 
 /*** DOCUMENTATION
        <application name="Dictate" language="en_US">
@@ -75,7 +78,7 @@ typedef enum {
 static int play_and_wait(struct ast_channel *chan, char *file, char *digits)
 {
        int res = -1;
-       if (!ast_streamfile(chan, file, chan->language)) {
+       if (!ast_streamfile(chan, file, ast_channel_language(chan))) {
                res = ast_waitstream(chan, digits);
        }
        return res;
@@ -97,7 +100,6 @@ static int dictate_exec(struct ast_channel *chan, const char *data)
        int ffactor = 320 * 80,
                res = 0,
                done = 0,
-               oldr = 0,
                lastop = 0,
                samples = 0,
                speed = 1,
@@ -105,6 +107,7 @@ static int dictate_exec(struct ast_channel *chan, const char *data)
                len = 0,
                maxlen = 0,
                mode = 0;
+       struct ast_format *oldr;
 
        snprintf(dftbase, sizeof(dftbase), "%s/dictate", ast_config_AST_SPOOL_DIR);
        if (!ast_strlen_zero(data)) {
@@ -121,13 +124,14 @@ static int dictate_exec(struct ast_channel *chan, const char *data)
        if (args.argc > 1 && args.filename) {
                filename = args.filename;
        }
-       oldr = chan->readformat;
-       if ((res = ast_set_read_format(chan, AST_FORMAT_SLINEAR)) < 0) {
+       oldr = ao2_bump(ast_channel_readformat(chan));
+       if ((res = ast_set_read_format(chan, ast_format_slin)) < 0) {
                ast_log(LOG_WARNING, "Unable to set to linear mode.\n");
+               ao2_cleanup(oldr);
                return -1;
        }
 
-       if (chan->_state != AST_STATE_UP) {
+       if (ast_channel_state(chan) != AST_STATE_UP) {
                ast_answer(chan);
        }
        ast_safe_sleep(chan, 200);
@@ -145,7 +149,7 @@ static int dictate_exec(struct ast_channel *chan, const char *data)
                ast_mkdir(base, 0755);
                len = strlen(base) + strlen(filein) + 2;
                if (!path || len > maxlen) {
-                       path = alloca(len);
+                       path = ast_alloca(len);
                        memset(path, 0, len);
                        maxlen = len;
                } else {
@@ -165,15 +169,15 @@ static int dictate_exec(struct ast_channel *chan, const char *data)
                samples = 0;
                while (!done && ((res = ast_waitfor(chan, -1)) > -1) && fs && (f = ast_read(chan))) {
                        if (digit) {
-                               struct ast_frame fr = {AST_FRAME_DTMF, digit};
+                               struct ast_frame fr = {AST_FRAME_DTMF, { .integer = digit } };
                                ast_queue_frame(chan, &fr);
                                digit = 0;
                        }
-                       if ((f->frametype == AST_FRAME_DTMF)) {
+                       if (f->frametype == AST_FRAME_DTMF) {
                                int got = 1;
                                switch(mode) {
                                case DMODE_PLAY:
-                                       switch(f->subclass) {
+                                       switch (f->subclass.integer) {
                                        case '1':
                                                ast_set_flag(&flags, DFLAG_PAUSE);
                                                mode = DMODE_RECORD;
@@ -183,7 +187,7 @@ static int dictate_exec(struct ast_channel *chan, const char *data)
                                                if (speed > 4) {
                                                        speed = 1;
                                                }
-                                               res = ast_say_number(chan, speed, AST_DIGIT_ANY, chan->language, NULL);
+                                               res = ast_say_number(chan, speed, AST_DIGIT_ANY, ast_channel_language(chan), NULL);
                                                break;
                                        case '7':
                                                samples -= ffactor;
@@ -196,13 +200,13 @@ static int dictate_exec(struct ast_channel *chan, const char *data)
                                                samples += ffactor;
                                                ast_seekstream(fs, samples, SEEK_SET);
                                                break;
-                                               
+
                                        default:
                                                got = 0;
                                        }
                                        break;
                                case DMODE_RECORD:
-                                       switch(f->subclass) {
+                                       switch (f->subclass.integer) {
                                        case '1':
                                                ast_set_flag(&flags, DFLAG_PAUSE);
                                                mode = DMODE_PLAY;
@@ -219,7 +223,7 @@ static int dictate_exec(struct ast_channel *chan, const char *data)
                                        got = 0;
                                }
                                if (!got) {
-                                       switch(f->subclass) {
+                                       switch (f->subclass.integer) {
                                        case '#':
                                                done = 1;
                                                continue;
@@ -252,7 +256,7 @@ static int dictate_exec(struct ast_channel *chan, const char *data)
                                                break;
                                        }
                                }
-                               
+
                        } else if (f->frametype == AST_FRAME_VOICE) {
                                switch(mode) {
                                        struct ast_frame *fr;
@@ -270,10 +274,10 @@ static int dictate_exec(struct ast_channel *chan, const char *data)
                                                if (lastop != DFLAG_PLAY) {
                                                        lastop = DFLAG_PLAY;
                                                        ast_closestream(fs);
-                                                       if (!(fs = ast_openstream(chan, path, chan->language)))
+                                                       if (!(fs = ast_openstream(chan, path, ast_channel_language(chan))))
                                                                break;
                                                        ast_seekstream(fs, samples, SEEK_SET);
-                                                       chan->stream = NULL;
+                                                       ast_channel_stream_set(chan, NULL);
                                                }
                                                lastop = DMODE_PLAY;
                                        }
@@ -324,7 +328,7 @@ static int dictate_exec(struct ast_channel *chan, const char *data)
                                        }
                                        break;
                                }
-                               
+
                        }
 
                        ast_frfree(f);
@@ -332,6 +336,7 @@ static int dictate_exec(struct ast_channel *chan, const char *data)
        }
        if (oldr) {
                ast_set_read_format(chan, oldr);
+               ao2_ref(oldr, -1);
        }
        return 0;
 }
@@ -348,4 +353,4 @@ static int load_module(void)
        return ast_register_application_xml(app, dictate_exec);
 }
 
-AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Virtual Dictation Machine");
+AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Virtual Dictation Machine");