app_controlplayback.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/pbx.h"
  32. #include "asterisk/app.h"
  33. #include "asterisk/module.h"
  34. /*** DOCUMENTATION
  35. <application name="ControlPlayback" language="en_US">
  36. <synopsis>
  37. Play a file with fast forward and rewind.
  38. </synopsis>
  39. <syntax>
  40. <parameter name="filename" required="true" />
  41. <parameter name="skipms">
  42. <para>This is number of milliseconds to skip when rewinding or
  43. fast-forwarding.</para>
  44. </parameter>
  45. <parameter name="ff">
  46. <para>Fast-forward when this DTMF digit is received. (defaults to <literal>#</literal>)</para>
  47. </parameter>
  48. <parameter name="rew">
  49. <para>Rewind when this DTMF digit is received. (defaults to <literal>*</literal>)</para>
  50. </parameter>
  51. <parameter name="stop">
  52. <para>Stop playback when this DTMF digit is received.</para>
  53. </parameter>
  54. <parameter name="pause">
  55. <para>Pause playback when this DTMF digit is received.</para>
  56. </parameter>
  57. <parameter name="restart">
  58. <para>Restart playback when this DTMF digit is received.</para>
  59. </parameter>
  60. <parameter name="options">
  61. <optionlist>
  62. <option name="o">
  63. <argument name="time" required="true">
  64. <para>Start at <replaceable>time</replaceable> ms from the
  65. beginning of the file.</para>
  66. </argument>
  67. </option>
  68. </optionlist>
  69. </parameter>
  70. </syntax>
  71. <description>
  72. <para>This application will play back the given <replaceable>filename</replaceable>.</para>
  73. <para>It sets the following channel variables upon completion:</para>
  74. <variablelist>
  75. <variable name="CPLAYBACKSTATUS">
  76. <para>Contains the status of the attempt as a text string</para>
  77. <value name="SUCCESS" />
  78. <value name="USERSTOPPED" />
  79. <value name="ERROR" />
  80. </variable>
  81. <variable name="CPLAYBACKOFFSET">
  82. <para>Contains the offset in ms into the file where playback
  83. was at when it stopped. <literal>-1</literal> is end of file.</para>
  84. </variable>
  85. <variable name="CPLAYBACKSTOPKEY">
  86. <para>If the playback is stopped by the user this variable contains
  87. the key that was pressed.</para>
  88. </variable>
  89. </variablelist>
  90. </description>
  91. </application>
  92. ***/
  93. static const char app[] = "ControlPlayback";
  94. enum {
  95. OPT_OFFSET = (1 << 1),
  96. };
  97. enum {
  98. OPT_ARG_OFFSET = 0,
  99. /* must stay as the last entry ... */
  100. OPT_ARG_ARRAY_LEN,
  101. };
  102. AST_APP_OPTIONS(cpb_opts, BEGIN_OPTIONS
  103. AST_APP_OPTION_ARG('o', OPT_OFFSET, OPT_ARG_OFFSET),
  104. END_OPTIONS
  105. );
  106. static int is_on_phonepad(char key)
  107. {
  108. return key == 35 || key == 42 || (key >= 48 && key <= 57);
  109. }
  110. static int is_argument(const char *haystack, int needle)
  111. {
  112. if (ast_strlen_zero(haystack))
  113. return 0;
  114. if (strchr(haystack, needle))
  115. return -1;
  116. return 0;
  117. }
  118. static int controlplayback_exec(struct ast_channel *chan, const char *data)
  119. {
  120. int res = 0;
  121. int skipms = 0;
  122. long offsetms = 0;
  123. char offsetbuf[20];
  124. char stopkeybuf[2];
  125. char *tmp;
  126. struct ast_flags opts = { 0, };
  127. char *opt_args[OPT_ARG_ARRAY_LEN];
  128. AST_DECLARE_APP_ARGS(args,
  129. AST_APP_ARG(filename);
  130. AST_APP_ARG(skip);
  131. AST_APP_ARG(fwd);
  132. AST_APP_ARG(rev);
  133. AST_APP_ARG(stop);
  134. AST_APP_ARG(pause);
  135. AST_APP_ARG(restart);
  136. AST_APP_ARG(options);
  137. );
  138. if (ast_strlen_zero(data)) {
  139. ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n");
  140. return -1;
  141. }
  142. tmp = ast_strdupa(data);
  143. AST_STANDARD_APP_ARGS(args, tmp);
  144. if (args.argc < 1) {
  145. ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n");
  146. return -1;
  147. }
  148. skipms = args.skip ? (atoi(args.skip) ? atoi(args.skip) : 3000) : 3000;
  149. if (!args.fwd || !is_on_phonepad(*args.fwd)) {
  150. char *digit = "#";
  151. if (!is_argument(args.rev, *digit) && !is_argument(args.stop, *digit) && !is_argument(args.pause, *digit) && !is_argument(args.restart, *digit))
  152. args.fwd = digit;
  153. else
  154. args.fwd = NULL;
  155. }
  156. if (!args.rev || !is_on_phonepad(*args.rev)) {
  157. char *digit = "*";
  158. if (!is_argument(args.fwd, *digit) && !is_argument(args.stop, *digit) && !is_argument(args.pause, *digit) && !is_argument(args.restart, *digit))
  159. args.rev = digit;
  160. else
  161. args.rev = NULL;
  162. }
  163. ast_debug(1, "Forward key = %s, Rewind key = %s\n", args.fwd, args.rev);
  164. if (args.stop && !is_on_phonepad(*args.stop))
  165. args.stop = NULL;
  166. if (args.pause && !is_on_phonepad(*args.pause))
  167. args.pause = NULL;
  168. if (args.restart && !is_on_phonepad(*args.restart))
  169. args.restart = NULL;
  170. if (args.options) {
  171. ast_app_parse_options(cpb_opts, &opts, opt_args, args.options);
  172. if (ast_test_flag(&opts, OPT_OFFSET))
  173. offsetms = atol(opt_args[OPT_ARG_OFFSET]);
  174. }
  175. res = ast_control_streamfile(chan, args.filename, args.fwd, args.rev, args.stop, args.pause, args.restart, skipms, &offsetms);
  176. /* If we stopped on one of our stop keys, return 0 */
  177. if (res > 0 && args.stop && strchr(args.stop, res)) {
  178. pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "USERSTOPPED");
  179. snprintf(stopkeybuf, sizeof(stopkeybuf), "%c", res);
  180. pbx_builtin_setvar_helper(chan, "CPLAYBACKSTOPKEY", stopkeybuf);
  181. res = 0;
  182. } else {
  183. if (res < 0) {
  184. res = 0;
  185. pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "ERROR");
  186. } else
  187. pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "SUCCESS");
  188. }
  189. snprintf(offsetbuf, sizeof(offsetbuf), "%ld", offsetms);
  190. pbx_builtin_setvar_helper(chan, "CPLAYBACKOFFSET", offsetbuf);
  191. return res;
  192. }
  193. static int unload_module(void)
  194. {
  195. int res;
  196. res = ast_unregister_application(app);
  197. return res;
  198. }
  199. static int load_module(void)
  200. {
  201. return ast_register_application_xml(app, controlplayback_exec);
  202. }
  203. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Control Playback Application");