app_controlplayback.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Trivial application to control playback of a sound file
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * \ingroup applications
  25. */
  26. #include "asterisk.h"
  27. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  28. #include "asterisk/pbx.h"
  29. #include "asterisk/app.h"
  30. #include "asterisk/module.h"
  31. static const char *app = "ControlPlayback";
  32. static const char *synopsis = "Play a file with fast forward and rewind";
  33. static const char *descrip =
  34. " ControlPlayback(file[,skipms[,ff[,rew[,stop[,pause[,restart,options]]]]]]]):\n"
  35. "This application will play back the given filename. By default, the '*' key\n"
  36. "can be used to rewind, and the '#' key can be used to fast-forward.\n"
  37. "Parameters:\n"
  38. " skipms - This is number of milliseconds to skip when rewinding or\n"
  39. " fast-forwarding.\n"
  40. " ff - Fast-forward when this DTMF digit is received.\n"
  41. " rew - Rewind when this DTMF digit is received.\n"
  42. " stop - Stop playback when this DTMF digit is received.\n"
  43. " pause - Pause playback when this DTMF digit is received.\n"
  44. " restart - Restart playback when this DTMF digit is received.\n"
  45. "Options:\n"
  46. " o(#) - Start at # ms from the beginning of the file.\n"
  47. "This application sets the following channel variables upon completion:\n"
  48. " CPLAYBACKSTATUS - This variable contains the status of the attempt as a text\n"
  49. " string, one of: SUCCESS | USERSTOPPED | ERROR\n"
  50. " CPLAYBACKOFFSET - This contains the offset in ms into the file where\n"
  51. " playback was at when it stopped. -1 is end of file.\n"
  52. " CPLAYBACKSTOPKEY - If the playback is stopped by the user this variable contains\n"
  53. " the key that was pressed.\n";
  54. enum {
  55. OPT_OFFSET = (1 << 1),
  56. };
  57. enum {
  58. OPT_ARG_OFFSET = 0,
  59. /* must stay as the last entry ... */
  60. OPT_ARG_ARRAY_LEN,
  61. };
  62. AST_APP_OPTIONS(cpb_opts, BEGIN_OPTIONS
  63. AST_APP_OPTION_ARG('o', OPT_OFFSET, OPT_ARG_OFFSET),
  64. END_OPTIONS
  65. );
  66. static int is_on_phonepad(char key)
  67. {
  68. return key == 35 || key == 42 || (key >= 48 && key <= 57);
  69. }
  70. static int is_argument(const char *haystack, int needle)
  71. {
  72. if (ast_strlen_zero(haystack))
  73. return 0;
  74. if (strchr(haystack, needle))
  75. return -1;
  76. return 0;
  77. }
  78. static int controlplayback_exec(struct ast_channel *chan, void *data)
  79. {
  80. int res = 0;
  81. int skipms = 0;
  82. long offsetms = 0;
  83. char offsetbuf[20];
  84. char stopkeybuf[2];
  85. char *tmp;
  86. struct ast_flags opts = { 0, };
  87. char *opt_args[OPT_ARG_ARRAY_LEN];
  88. AST_DECLARE_APP_ARGS(args,
  89. AST_APP_ARG(filename);
  90. AST_APP_ARG(skip);
  91. AST_APP_ARG(fwd);
  92. AST_APP_ARG(rev);
  93. AST_APP_ARG(stop);
  94. AST_APP_ARG(pause);
  95. AST_APP_ARG(restart);
  96. AST_APP_ARG(options);
  97. );
  98. if (ast_strlen_zero(data)) {
  99. ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n");
  100. return -1;
  101. }
  102. tmp = ast_strdupa(data);
  103. AST_STANDARD_APP_ARGS(args, tmp);
  104. if (args.argc < 1) {
  105. ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n");
  106. return -1;
  107. }
  108. skipms = args.skip ? (atoi(args.skip) ? atoi(args.skip) : 3000) : 3000;
  109. if (!args.fwd || !is_on_phonepad(*args.fwd)) {
  110. char *digit = "#";
  111. if (!is_argument(args.rev, *digit) && !is_argument(args.stop, *digit) && !is_argument(args.pause, *digit) && !is_argument(args.restart, *digit))
  112. args.fwd = digit;
  113. else
  114. args.fwd = NULL;
  115. }
  116. if (!args.rev || !is_on_phonepad(*args.rev)) {
  117. char *digit = "*";
  118. if (!is_argument(args.fwd, *digit) && !is_argument(args.stop, *digit) && !is_argument(args.pause, *digit) && !is_argument(args.restart, *digit))
  119. args.rev = digit;
  120. else
  121. args.rev = NULL;
  122. }
  123. ast_debug(1, "Forward key = %s, Rewind key = %s\n", args.fwd, args.rev);
  124. if (args.stop && !is_on_phonepad(*args.stop))
  125. args.stop = NULL;
  126. if (args.pause && !is_on_phonepad(*args.pause))
  127. args.pause = NULL;
  128. if (args.restart && !is_on_phonepad(*args.restart))
  129. args.restart = NULL;
  130. if (args.options) {
  131. ast_app_parse_options(cpb_opts, &opts, opt_args, args.options);
  132. if (ast_test_flag(&opts, OPT_OFFSET))
  133. offsetms = atol(opt_args[OPT_ARG_OFFSET]);
  134. }
  135. res = ast_control_streamfile(chan, args.filename, args.fwd, args.rev, args.stop, args.pause, args.restart, skipms, &offsetms);
  136. /* If we stopped on one of our stop keys, return 0 */
  137. if (res > 0 && args.stop && strchr(args.stop, res)) {
  138. pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "USERSTOPPED");
  139. snprintf(stopkeybuf, sizeof(stopkeybuf), "%c", res);
  140. pbx_builtin_setvar_helper(chan, "CPLAYBACKSTOPKEY", stopkeybuf);
  141. res = 0;
  142. } else {
  143. if (res < 0) {
  144. res = 0;
  145. pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "ERROR");
  146. } else
  147. pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "SUCCESS");
  148. }
  149. snprintf(offsetbuf, sizeof(offsetbuf), "%ld", offsetms);
  150. pbx_builtin_setvar_helper(chan, "CPLAYBACKOFFSET", offsetbuf);
  151. return res;
  152. }
  153. static int unload_module(void)
  154. {
  155. int res;
  156. res = ast_unregister_application(app);
  157. return res;
  158. }
  159. static int load_module(void)
  160. {
  161. return ast_register_application(app, controlplayback_exec, synopsis, descrip);
  162. }
  163. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Control Playback Application");