| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include "asterisk.h" |
| 33 |
|
| 34 |
#include "asterisk/pbx.h" |
| 35 |
#include "asterisk/app.h" |
| 36 |
#include "asterisk/module.h" |
| 37 |
#include "asterisk/manager.h" |
| 38 |
#include "asterisk/utils.h" |
| 39 |
#include "asterisk/astobj2.h" |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
static const char app[] = "ControlPlayback"; |
| 171 |
|
| 172 |
enum { |
| 173 |
OPT_OFFSET = (1 << 1), |
| 174 |
}; |
| 175 |
|
| 176 |
enum { |
| 177 |
OPT_ARG_OFFSET = 0, |
| 178 |
|
| 179 |
OPT_ARG_ARRAY_LEN, |
| 180 |
}; |
| 181 |
|
| 182 |
AST_APP_OPTIONS(cpb_opts, BEGIN_OPTIONS |
| 183 |
AST_APP_OPTION_ARG('o', OPT_OFFSET, OPT_ARG_OFFSET), |
| 184 |
END_OPTIONS |
| 185 |
); |
| 186 |
|
| 187 |
static int is_on_phonepad(char key) |
| 188 |
{ |
| 189 |
return key == 35 || key == 42 || (key >= 48 && key <= 57); |
| 190 |
} |
| 191 |
|
| 192 |
static int is_argument(const char *haystack, int needle) |
| 193 |
{ |
| 194 |
if (ast_strlen_zero(haystack)) |
| 195 |
return 0; |
| 196 |
|
| 197 |
if (strchr(haystack, needle)) |
| 198 |
return -1; |
| 199 |
|
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
static int controlplayback_exec(struct ast_channel *chan, const char *data) |
| 204 |
{ |
| 205 |
int res = 0; |
| 206 |
int skipms = 0; |
| 207 |
long offsetms = 0; |
| 208 |
char offsetbuf[20]; |
| 209 |
char stopkeybuf[2]; |
| 210 |
char *tmp; |
| 211 |
struct ast_flags opts = { 0, }; |
| 212 |
char *opt_args[OPT_ARG_ARRAY_LEN]; |
| 213 |
AST_DECLARE_APP_ARGS(args, |
| 214 |
AST_APP_ARG(filename); |
| 215 |
AST_APP_ARG(skip); |
| 216 |
AST_APP_ARG(fwd); |
| 217 |
AST_APP_ARG(rev); |
| 218 |
AST_APP_ARG(stop); |
| 219 |
AST_APP_ARG(pause); |
| 220 |
AST_APP_ARG(restart); |
| 221 |
AST_APP_ARG(options); |
| 222 |
); |
| 223 |
|
| 224 |
if (ast_strlen_zero(data)) { |
| 225 |
ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n"); |
| 226 |
return -1; |
| 227 |
} |
| 228 |
|
| 229 |
tmp = ast_strdupa(data); |
| 230 |
AST_STANDARD_APP_ARGS(args, tmp); |
| 231 |
|
| 232 |
if (args.argc < 1) { |
| 233 |
ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n"); |
| 234 |
return -1; |
| 235 |
} |
| 236 |
|
| 237 |
skipms = args.skip ? (atoi(args.skip) ? atoi(args.skip) : 3000) : 3000; |
| 238 |
|
| 239 |
if (!args.fwd || !is_on_phonepad(*args.fwd)) { |
| 240 |
char *digit = "#"; |
| 241 |
if (!is_argument(args.rev, *digit) && !is_argument(args.stop, *digit) && !is_argument(args.pause, *digit) && !is_argument(args.restart, *digit)) |
| 242 |
args.fwd = digit; |
| 243 |
else |
| 244 |
args.fwd = NULL; |
| 245 |
} |
| 246 |
if (!args.rev || !is_on_phonepad(*args.rev)) { |
| 247 |
char *digit = "*"; |
| 248 |
if (!is_argument(args.fwd, *digit) && !is_argument(args.stop, *digit) && !is_argument(args.pause, *digit) && !is_argument(args.restart, *digit)) |
| 249 |
args.rev = digit; |
| 250 |
else |
| 251 |
args.rev = NULL; |
| 252 |
} |
| 253 |
ast_debug(1, "Forward key = %s, Rewind key = %s\n", args.fwd, args.rev); |
| 254 |
if (args.stop && !is_on_phonepad(*args.stop)) |
| 255 |
args.stop = NULL; |
| 256 |
if (args.pause && !is_on_phonepad(*args.pause)) |
| 257 |
args.pause = NULL; |
| 258 |
if (args.restart && !is_on_phonepad(*args.restart)) |
| 259 |
args.restart = NULL; |
| 260 |
|
| 261 |
if (args.options) { |
| 262 |
ast_app_parse_options(cpb_opts, &opts, opt_args, args.options); |
| 263 |
if (ast_test_flag(&opts, OPT_OFFSET)) |
| 264 |
offsetms = atol(opt_args[OPT_ARG_OFFSET]); |
| 265 |
} |
| 266 |
|
| 267 |
res = ast_control_streamfile(chan, args.filename, args.fwd, args.rev, args.stop, args.pause, args.restart, skipms, &offsetms); |
| 268 |
|
| 269 |
|
| 270 |
if (res > 0 && args.stop && strchr(args.stop, res)) { |
| 271 |
pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "USERSTOPPED"); |
| 272 |
snprintf(stopkeybuf, sizeof(stopkeybuf), "%c", res); |
| 273 |
pbx_builtin_setvar_helper(chan, "CPLAYBACKSTOPKEY", stopkeybuf); |
| 274 |
res = 0; |
| 275 |
} else if (res > 0 && res == AST_CONTROL_STREAM_STOP) { |
| 276 |
pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "REMOTESTOPPED"); |
| 277 |
res = 0; |
| 278 |
} else { |
| 279 |
if (res < 0) { |
| 280 |
res = 0; |
| 281 |
pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "ERROR"); |
| 282 |
} else |
| 283 |
pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "SUCCESS"); |
| 284 |
} |
| 285 |
|
| 286 |
snprintf(offsetbuf, sizeof(offsetbuf), "%ld", offsetms); |
| 287 |
pbx_builtin_setvar_helper(chan, "CPLAYBACKOFFSET", offsetbuf); |
| 288 |
|
| 289 |
return res; |
| 290 |
} |
| 291 |
|
| 292 |
static int controlplayback_manager(struct mansession *s, const struct message *m) |
| 293 |
{ |
| 294 |
const char *channel_name = astman_get_header(m, "Channel"); |
| 295 |
const char *control_type = astman_get_header(m, "Control"); |
| 296 |
struct ast_channel *chan; |
| 297 |
|
| 298 |
if (ast_strlen_zero(channel_name)) { |
| 299 |
astman_send_error(s, m, "Channel not specified"); |
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
if (ast_strlen_zero(control_type)) { |
| 304 |
astman_send_error(s, m, "Control not specified"); |
| 305 |
return 0; |
| 306 |
} |
| 307 |
|
| 308 |
chan = ast_channel_get_by_name(channel_name); |
| 309 |
if (!chan) { |
| 310 |
astman_send_error(s, m, "No such channel"); |
| 311 |
return 0; |
| 312 |
} |
| 313 |
|
| 314 |
if (!strcasecmp(control_type, "stop")) { |
| 315 |
ast_queue_control(chan, AST_CONTROL_STREAM_STOP); |
| 316 |
} else if (!strcasecmp(control_type, "forward")) { |
| 317 |
ast_queue_control(chan, AST_CONTROL_STREAM_FORWARD); |
| 318 |
} else if (!strcasecmp(control_type, "reverse")) { |
| 319 |
ast_queue_control(chan, AST_CONTROL_STREAM_REVERSE); |
| 320 |
} else if (!strcasecmp(control_type, "pause")) { |
| 321 |
ast_queue_control(chan, AST_CONTROL_STREAM_SUSPEND); |
| 322 |
} else if (!strcasecmp(control_type, "restart")) { |
| 323 |
ast_queue_control(chan, AST_CONTROL_STREAM_RESTART); |
| 324 |
} else { |
| 325 |
astman_send_error(s, m, "Unknown control type"); |
| 326 |
chan = ast_channel_unref(chan); |
| 327 |
return 0; |
| 328 |
} |
| 329 |
|
| 330 |
chan = ast_channel_unref(chan); |
| 331 |
astman_send_ack(s, m, NULL); |
| 332 |
return 0; |
| 333 |
} |
| 334 |
|
| 335 |
static int unload_module(void) |
| 336 |
{ |
| 337 |
int res = 0; |
| 338 |
|
| 339 |
res |= ast_unregister_application(app); |
| 340 |
res |= ast_manager_unregister("ControlPlayback"); |
| 341 |
|
| 342 |
return res; |
| 343 |
} |
| 344 |
|
| 345 |
static int load_module(void) |
| 346 |
{ |
| 347 |
int res = 0; |
| 348 |
|
| 349 |
res |= ast_register_application_xml(app, controlplayback_exec); |
| 350 |
res |= ast_manager_register_xml("ControlPlayback", EVENT_FLAG_CALL, controlplayback_manager); |
| 351 |
|
| 352 |
return res; |
| 353 |
} |
| 354 |
|
| 355 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Control Playback Application"); |