00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "asterisk.h"
00029
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 103249 $")
00031
00032 #include "asterisk/pbx.h"
00033 #include "asterisk/app.h"
00034 #include "asterisk/module.h"
00035
00036 static const char *app = "ControlPlayback";
00037
00038 static const char *synopsis = "Play a file with fast forward and rewind";
00039
00040 static const char *descrip =
00041 " ControlPlayback(file[,skipms[,ff[,rew[,stop[,pause[,restart,options]]]]]]]):\n"
00042 "This application will play back the given filename. By default, the '*' key\n"
00043 "can be used to rewind, and the '#' key can be used to fast-forward.\n"
00044 "Parameters:\n"
00045 " skipms - This is number of milliseconds to skip when rewinding or\n"
00046 " fast-forwarding.\n"
00047 " ff - Fast-forward when this DTMF digit is received.\n"
00048 " rew - Rewind when this DTMF digit is received.\n"
00049 " stop - Stop playback when this DTMF digit is received.\n"
00050 " pause - Pause playback when this DTMF digit is received.\n"
00051 " restart - Restart playback when this DTMF digit is received.\n"
00052 "Options:\n"
00053 " o(#) - Start at # ms from the beginning of the file.\n"
00054 "This application sets the following channel variables upon completion:\n"
00055 " CPLAYBACKSTATUS - This variable contains the status of the attempt as a text\n"
00056 " string, one of: SUCCESS | USERSTOPPED | ERROR\n"
00057 " CPLAYBACKOFFSET - This contains the offset in ms into the file where\n"
00058 " playback was at when it stopped. -1 is end of file.\n"
00059 " CPLAYBACKSTOPKEY - If the playback is stopped by the user this variable contains\n"
00060 " the key that was pressed.\n";
00061
00062 enum {
00063 OPT_OFFSET = (1 << 1),
00064 };
00065
00066 enum {
00067 OPT_ARG_OFFSET = 0,
00068
00069 OPT_ARG_ARRAY_LEN,
00070 };
00071
00072 AST_APP_OPTIONS(cpb_opts, BEGIN_OPTIONS
00073 AST_APP_OPTION_ARG('o', OPT_OFFSET, OPT_ARG_OFFSET),
00074 END_OPTIONS
00075 );
00076
00077 static int is_on_phonepad(char key)
00078 {
00079 return key == 35 || key == 42 || (key >= 48 && key <= 57);
00080 }
00081
00082 static int is_argument(const char *haystack, int needle)
00083 {
00084 if (ast_strlen_zero(haystack))
00085 return 0;
00086
00087 if (strchr(haystack, needle))
00088 return -1;
00089
00090 return 0;
00091 }
00092
00093 static int controlplayback_exec(struct ast_channel *chan, void *data)
00094 {
00095 int res = 0;
00096 int skipms = 0;
00097 long offsetms = 0;
00098 char offsetbuf[20];
00099 char stopkeybuf[2];
00100 char *tmp;
00101 struct ast_flags opts = { 0, };
00102 char *opt_args[OPT_ARG_ARRAY_LEN];
00103 AST_DECLARE_APP_ARGS(args,
00104 AST_APP_ARG(filename);
00105 AST_APP_ARG(skip);
00106 AST_APP_ARG(fwd);
00107 AST_APP_ARG(rev);
00108 AST_APP_ARG(stop);
00109 AST_APP_ARG(pause);
00110 AST_APP_ARG(restart);
00111 AST_APP_ARG(options);
00112 );
00113
00114 if (ast_strlen_zero(data)) {
00115 ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n");
00116 return -1;
00117 }
00118
00119 tmp = ast_strdupa(data);
00120 AST_STANDARD_APP_ARGS(args, tmp);
00121
00122 if (args.argc < 1) {
00123 ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n");
00124 return -1;
00125 }
00126
00127 skipms = args.skip ? (atoi(args.skip) ? atoi(args.skip) : 3000) : 3000;
00128
00129 if (!args.fwd || !is_on_phonepad(*args.fwd)) {
00130 char *digit = "#";
00131 if (!is_argument(args.rev, *digit) && !is_argument(args.stop, *digit) && !is_argument(args.pause, *digit) && !is_argument(args.restart, *digit))
00132 args.fwd = digit;
00133 else
00134 args.fwd = NULL;
00135 }
00136 if (!args.rev || !is_on_phonepad(*args.rev)) {
00137 char *digit = "*";
00138 if (!is_argument(args.fwd, *digit) && !is_argument(args.stop, *digit) && !is_argument(args.pause, *digit) && !is_argument(args.restart, *digit))
00139 args.rev = digit;
00140 else
00141 args.rev = NULL;
00142 }
00143 ast_log(LOG_WARNING, "args.fwd = %s, args.rew = %s\n", args.fwd, args.rev);
00144 if (args.stop && !is_on_phonepad(*args.stop))
00145 args.stop = NULL;
00146 if (args.pause && !is_on_phonepad(*args.pause))
00147 args.pause = NULL;
00148 if (args.restart && !is_on_phonepad(*args.restart))
00149 args.restart = NULL;
00150
00151 if (args.options) {
00152 ast_app_parse_options(cpb_opts, &opts, opt_args, args.options);
00153 if (ast_test_flag(&opts, OPT_OFFSET))
00154 offsetms = atol(opt_args[OPT_ARG_OFFSET]);
00155 }
00156
00157 res = ast_control_streamfile(chan, args.filename, args.fwd, args.rev, args.stop, args.pause, args.restart, skipms, &offsetms);
00158
00159
00160 if (res > 0 && args.stop && strchr(args.stop, res)) {
00161 pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "USERSTOPPED");
00162 snprintf(stopkeybuf, sizeof(stopkeybuf), "%c", res);
00163 pbx_builtin_setvar_helper(chan, "CPLAYBACKSTOPKEY", stopkeybuf);
00164 res = 0;
00165 } else {
00166 if (res < 0) {
00167 res = 0;
00168 pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "ERROR");
00169 } else
00170 pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "SUCCESS");
00171 }
00172
00173 snprintf(offsetbuf, sizeof(offsetbuf), "%ld", offsetms);
00174 pbx_builtin_setvar_helper(chan, "CPLAYBACKOFFSET", offsetbuf);
00175
00176 return res;
00177 }
00178
00179 static int unload_module(void)
00180 {
00181 int res;
00182 res = ast_unregister_application(app);
00183 return res;
00184 }
00185
00186 static int load_module(void)
00187 {
00188 return ast_register_application(app, controlplayback_exec, synopsis, descrip);
00189 }
00190
00191 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Control Playback Application");