Asterisk - The Open Source Telephony Project  GIT-master-9ed6387
Enumerations | Functions | Variables
app_controlplayback.c File Reference

Trivial application to control playback of a sound file. More...

#include "asterisk.h"
#include "asterisk/pbx.h"
#include "asterisk/app.h"
#include "asterisk/module.h"
#include "asterisk/manager.h"
#include "asterisk/utils.h"
#include "asterisk/astobj2.h"

Go to the source code of this file.

Enumerations

enum  { OPT_OFFSET = (1 << 1) }
 
enum  { OPT_ARG_OFFSET = 0, OPT_ARG_ARRAY_LEN }
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int controlplayback_exec (struct ast_channel *chan, const char *data)
 
static int controlplayback_manager (struct mansession *s, const struct message *m)
 
static int is_argument (const char *haystack, int needle)
 
static int is_on_phonepad (char key)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Control Playback Application" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const char app [] = "ControlPlayback"
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static const struct ast_app_option cpb_opts [128] = { [ 'o' ] = { .flag = OPT_OFFSET , .arg_index = OPT_ARG_OFFSET + 1 }, }
 

Detailed Description

Trivial application to control playback of a sound file.

Author
Mark Spencer marks.nosp@m.ter@.nosp@m.digiu.nosp@m.m.co.nosp@m.m

Definition in file app_controlplayback.c.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
OPT_OFFSET 

Definition at line 166 of file app_controlplayback.c.

166  {
167  OPT_OFFSET = (1 << 1),
168 };

◆ anonymous enum

anonymous enum
Enumerator
OPT_ARG_OFFSET 
OPT_ARG_ARRAY_LEN 

Definition at line 170 of file app_controlplayback.c.

170  {
171  OPT_ARG_OFFSET = 0,
172  /* must stay as the last entry ... */
174 };

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 349 of file app_controlplayback.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 349 of file app_controlplayback.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 349 of file app_controlplayback.c.

◆ controlplayback_exec()

static int controlplayback_exec ( struct ast_channel chan,
const char *  data 
)
static

Definition at line 197 of file app_controlplayback.c.

References args, AST_APP_ARG, ast_app_parse_options(), AST_CONTROL_STREAM_STOP, ast_control_streamfile(), ast_debug, AST_DECLARE_APP_ARGS, ast_log, AST_STANDARD_APP_ARGS, ast_strdupa, ast_strlen_zero, ast_test_flag, cpb_opts, digit, is_argument(), is_on_phonepad(), LOG_WARNING, NULL, OPT_ARG_ARRAY_LEN, OPT_ARG_OFFSET, OPT_OFFSET, options, pbx_builtin_setvar_helper(), skipms, stop, and tmp().

Referenced by load_module().

198 {
199  int res = 0;
200  int skipms = 0;
201  long offsetms = 0;
202  char offsetbuf[20];
203  char stopkeybuf[2];
204  char *tmp;
205  struct ast_flags opts = { 0, };
206  char *opt_args[OPT_ARG_ARRAY_LEN];
208  AST_APP_ARG(filename);
209  AST_APP_ARG(skip);
210  AST_APP_ARG(fwd);
211  AST_APP_ARG(rev);
212  AST_APP_ARG(stop);
213  AST_APP_ARG(pause);
214  AST_APP_ARG(restart);
216  );
217 
218  if (ast_strlen_zero(data)) {
219  ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n");
220  return -1;
221  }
222 
223  tmp = ast_strdupa(data);
225 
226  if (args.argc < 1) {
227  ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n");
228  return -1;
229  }
230 
231  skipms = args.skip ? (atoi(args.skip) ? atoi(args.skip) : 3000) : 3000;
232 
233  if (!args.fwd || !is_on_phonepad(*args.fwd)) {
234  char *digit = "#";
235  if (!is_argument(args.rev, *digit) && !is_argument(args.stop, *digit) && !is_argument(args.pause, *digit) && !is_argument(args.restart, *digit))
236  args.fwd = digit;
237  else
238  args.fwd = NULL;
239  }
240  if (!args.rev || !is_on_phonepad(*args.rev)) {
241  char *digit = "*";
242  if (!is_argument(args.fwd, *digit) && !is_argument(args.stop, *digit) && !is_argument(args.pause, *digit) && !is_argument(args.restart, *digit))
243  args.rev = digit;
244  else
245  args.rev = NULL;
246  }
247  ast_debug(1, "Forward key = %s, Rewind key = %s\n", args.fwd, args.rev);
248  if (args.stop && !is_on_phonepad(*args.stop))
249  args.stop = NULL;
250  if (args.pause && !is_on_phonepad(*args.pause))
251  args.pause = NULL;
252  if (args.restart && !is_on_phonepad(*args.restart))
253  args.restart = NULL;
254 
255  if (args.options) {
256  ast_app_parse_options(cpb_opts, &opts, opt_args, args.options);
257  if (ast_test_flag(&opts, OPT_OFFSET))
258  offsetms = atol(opt_args[OPT_ARG_OFFSET]);
259  }
260 
261  res = ast_control_streamfile(chan, args.filename, args.fwd, args.rev, args.stop, args.pause, args.restart, skipms, &offsetms);
262 
263  /* If we stopped on one of our stop keys, return 0 */
264  if (res > 0 && args.stop && strchr(args.stop, res)) {
265  pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "USERSTOPPED");
266  snprintf(stopkeybuf, sizeof(stopkeybuf), "%c", res);
267  pbx_builtin_setvar_helper(chan, "CPLAYBACKSTOPKEY", stopkeybuf);
268  res = 0;
269  } else if (res > 0 && res == AST_CONTROL_STREAM_STOP) {
270  pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "REMOTESTOPPED");
271  res = 0;
272  } else {
273  if (res < 0) {
274  res = 0;
275  pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "ERROR");
276  } else
277  pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "SUCCESS");
278  }
279 
280  snprintf(offsetbuf, sizeof(offsetbuf), "%ld", offsetms);
281  pbx_builtin_setvar_helper(chan, "CPLAYBACKOFFSET", offsetbuf);
282 
283  return res;
284 }
char digit
#define ast_test_flag(p, flag)
Definition: utils.h:63
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
#define LOG_WARNING
Definition: logger.h:274
static int tmp()
Definition: bt_open.c:389
unsigned int stop
Definition: app_meetme.c:1096
const char * args
#define NULL
Definition: resample.c:96
int ast_control_streamfile(struct ast_channel *chan, const char *file, const char *fwd, const char *rev, const char *stop, const char *pause, const char *restart, int skipms, long *offsetms)
Stream a file with fast forward, pause, reverse, restart.
Definition: main/app.c:1242
static int is_argument(const char *haystack, int needle)
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:444
#define ast_log
Definition: astobj2.c:42
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
static int is_on_phonepad(char key)
int ast_app_parse_options(const struct ast_app_option *options, struct ast_flags *flags, char **args, char *optstr)
Parses a string containing application options and sets flags/arguments.
Definition: main/app.c:2829
static int skipms
#define ast_strlen_zero(a)
Definition: muted.c:73
Structure used to handle boolean flags.
Definition: utils.h:199
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name...
static const struct ast_app_option cpb_opts[128]
static struct test_options options
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
#define AST_APP_ARG(name)
Define an application argument.

◆ controlplayback_manager()

static int controlplayback_manager ( struct mansession s,
const struct message m 
)
static

Definition at line 286 of file app_controlplayback.c.

References ast_channel_get_by_name(), ast_channel_unref, AST_CONTROL_STREAM_FORWARD, AST_CONTROL_STREAM_RESTART, AST_CONTROL_STREAM_REVERSE, AST_CONTROL_STREAM_STOP, AST_CONTROL_STREAM_SUSPEND, ast_queue_control(), ast_strlen_zero, astman_get_header(), astman_send_ack(), astman_send_error(), and NULL.

Referenced by load_module().

287 {
288  const char *channel_name = astman_get_header(m, "Channel");
289  const char *control_type = astman_get_header(m, "Control");
290  struct ast_channel *chan;
291 
292  if (ast_strlen_zero(channel_name)) {
293  astman_send_error(s, m, "Channel not specified");
294  return 0;
295  }
296 
297  if (ast_strlen_zero(control_type)) {
298  astman_send_error(s, m, "Control not specified");
299  return 0;
300  }
301 
302  chan = ast_channel_get_by_name(channel_name);
303  if (!chan) {
304  astman_send_error(s, m, "No such channel");
305  return 0;
306  }
307 
308  if (!strcasecmp(control_type, "stop")) {
310  } else if (!strcasecmp(control_type, "forward")) {
312  } else if (!strcasecmp(control_type, "reverse")) {
314  } else if (!strcasecmp(control_type, "pause")) {
316  } else if (!strcasecmp(control_type, "restart")) {
318  } else {
319  astman_send_error(s, m, "Unknown control type");
320  chan = ast_channel_unref(chan);
321  return 0;
322  }
323 
324  chan = ast_channel_unref(chan);
325  astman_send_ack(s, m, NULL);
326  return 0;
327 }
Main Channel structure associated with a channel.
int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control)
Queue a control frame without payload.
Definition: channel.c:1227
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2873
void astman_send_ack(struct mansession *s, const struct message *m, char *msg)
Send ack in manager transaction.
Definition: manager.c:3191
#define NULL
Definition: resample.c:96
const char * astman_get_header(const struct message *m, char *var)
Get header from mananger transaction.
Definition: manager.c:2820
#define ast_strlen_zero(a)
Definition: muted.c:73
struct ast_channel * ast_channel_get_by_name(const char *name)
Find a channel by name.
Definition: channel.c:1439
void astman_send_error(struct mansession *s, const struct message *m, char *error)
Send error in manager transaction.
Definition: manager.c:3159

◆ is_argument()

static int is_argument ( const char *  haystack,
int  needle 
)
static

Definition at line 186 of file app_controlplayback.c.

References ast_strlen_zero.

Referenced by controlplayback_exec().

187 {
188  if (ast_strlen_zero(haystack))
189  return 0;
190 
191  if (strchr(haystack, needle))
192  return -1;
193 
194  return 0;
195 }
#define ast_strlen_zero(a)
Definition: muted.c:73

◆ is_on_phonepad()

static int is_on_phonepad ( char  key)
static

Definition at line 181 of file app_controlplayback.c.

Referenced by controlplayback_exec().

182 {
183  return key == 35 || key == 42 || (key >= 48 && key <= 57);
184 }

◆ load_module()

static int load_module ( void  )
static

Definition at line 339 of file app_controlplayback.c.

References app, ast_manager_register_xml, ast_register_application_xml, controlplayback_exec(), controlplayback_manager(), and EVENT_FLAG_CALL.

340 {
341  int res = 0;
342 
345 
346  return res;
347 }
static int controlplayback_manager(struct mansession *s, const struct message *m)
#define EVENT_FLAG_CALL
Definition: manager.h:72
static const char app[]
#define ast_manager_register_xml(action, authority, func)
Register a manager callback using XML documentation to describe the manager.
Definition: manager.h:186
static int controlplayback_exec(struct ast_channel *chan, const char *data)
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:626

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 329 of file app_controlplayback.c.

References app, ast_manager_unregister(), and ast_unregister_application().

330 {
331  int res = 0;
332 
334  res |= ast_manager_unregister("ControlPlayback");
335 
336  return res;
337 }
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
int ast_manager_unregister(const char *action)
Unregister a registered manager command.
Definition: manager.c:7258
static const char app[]

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Control Playback Application" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 349 of file app_controlplayback.c.

◆ app

const char app[] = "ControlPlayback"
static

Definition at line 164 of file app_controlplayback.c.

Referenced by load_module(), and unload_module().

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 349 of file app_controlplayback.c.

◆ cpb_opts

const struct ast_app_option cpb_opts[128] = { [ 'o' ] = { .flag = OPT_OFFSET , .arg_index = OPT_ARG_OFFSET + 1 }, }
static

Definition at line 179 of file app_controlplayback.c.

Referenced by controlplayback_exec().