app_controlplayback.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Trivial application to control playback a sound file
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <asterisk/lock.h>
  14. #include <asterisk/file.h>
  15. #include <asterisk/logger.h>
  16. #include <asterisk/channel.h>
  17. #include <asterisk/pbx.h>
  18. #include <asterisk/app.h>
  19. #include <asterisk/module.h>
  20. #include <asterisk/translate.h>
  21. #include <asterisk/utils.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. static char *tdesc = "Control Playback Application";
  25. static char *app = "ControlPlayback";
  26. static char *synopsis = "Play a file with fast forward and rewind";
  27. static char *descrip =
  28. "ControlPlayback(filename[|skipms[|ffchar[|rewchar[|stopchar[|pausechr]]]]]):\n"
  29. " Plays back a given filename (do not put extension). Options may also\n"
  30. " be included following a pipe symbol. You can use * and # to rewind and\n"
  31. " fast forward the playback specified. If 'stopchar' is added the file will\n"
  32. " terminate playback when 'stopchar' is pressed. Returns -1 if the channel\n"
  33. " was hung up, or if the file does not exist. Returns 0 otherwise.\n\n"
  34. " Example: exten => 1234,1,ControlPlayback(file|4000|*|#|1|0)\n\n";
  35. STANDARD_LOCAL_USER;
  36. LOCAL_USER_DECL;
  37. static int is_on_phonepad(char key)
  38. {
  39. return (key == 35 || key == 42 || (key >= 48 && key <= 57)) ? 1 : 0;
  40. }
  41. static int controlplayback_exec(struct ast_channel *chan, void *data)
  42. {
  43. int res = 0;
  44. int skipms = 0;
  45. struct localuser *u;
  46. char tmp[256];
  47. char *skip = NULL, *fwd = NULL, *rev = NULL, *stop = NULL, *pause = NULL, *file = NULL;
  48. if (!data || ast_strlen_zero((char *)data)) {
  49. ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n");
  50. return -1;
  51. }
  52. strncpy(tmp, (char *)data, sizeof(tmp)-1);
  53. file = tmp;
  54. if ((skip=strchr(tmp,'|'))) {
  55. *skip++ = '\0';
  56. fwd=strchr(skip,'|');
  57. if (fwd) {
  58. *fwd++ = '\0';
  59. rev = strchr(fwd,'|');
  60. if (rev) {
  61. *rev++ = '\0';
  62. stop = strchr(rev,'|');
  63. if (stop) {
  64. *stop++ = '\0';
  65. pause = strchr(stop,'|');
  66. if (pause) {
  67. *pause++ = '\0';
  68. }
  69. }
  70. }
  71. }
  72. }
  73. skipms = skip ? atoi(skip) : 3000;
  74. if (!skipms)
  75. skipms = 3000;
  76. if (!fwd || !is_on_phonepad(*fwd))
  77. fwd = "#";
  78. if (!rev || !is_on_phonepad(*rev))
  79. rev = "*";
  80. if (stop && !is_on_phonepad(*stop))
  81. stop = NULL;
  82. if (pause && !is_on_phonepad(*pause))
  83. pause = NULL;
  84. LOCAL_USER_ADD(u);
  85. res = ast_control_streamfile(chan, file, fwd, rev, stop, pause, skipms);
  86. LOCAL_USER_REMOVE(u);
  87. /* If we stopped on one of our stop keys, return 0 */
  88. if(stop && strchr(stop, res))
  89. res = 0;
  90. return res;
  91. }
  92. int unload_module(void)
  93. {
  94. STANDARD_HANGUP_LOCALUSERS;
  95. return ast_unregister_application(app);
  96. }
  97. int load_module(void)
  98. {
  99. return ast_register_application(app, controlplayback_exec, synopsis, descrip);
  100. }
  101. char *description(void)
  102. {
  103. return tdesc;
  104. }
  105. int usecount(void)
  106. {
  107. int res;
  108. STANDARD_USECOUNT(res);
  109. return res;
  110. }
  111. char *key()
  112. {
  113. return ASTERISK_GPL_KEY;
  114. }