app_playback.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Trivial application to 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/module.h>
  19. #include <asterisk/translate.h>
  20. #include <asterisk/utils.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. static char *tdesc = "Trivial Playback Application";
  24. static char *app = "Playback";
  25. static char *synopsis = "Play a file";
  26. static char *descrip =
  27. " Playback(filename[|option]): Plays back a given filename (do not put\n"
  28. "extension). Options may also be included following a pipe symbol. The 'skip'\n"
  29. "option causes the playback of the message to be skipped if the channel\n"
  30. "is not in the 'up' state (i.e. it hasn't been answered yet. If 'skip' is \n"
  31. "specified, the application will return immediately should the channel not be\n"
  32. "off hook. Otherwise, unless 'noanswer' is specified, the channel channel will\n"
  33. "be answered before the sound is played. Not all channels support playing\n"
  34. "messages while still hook. Returns -1 if the channel was hung up, or if the\n"
  35. "file does not exist. Returns 0 otherwise.\n";
  36. STANDARD_LOCAL_USER;
  37. LOCAL_USER_DECL;
  38. static int playback_exec(struct ast_channel *chan, void *data)
  39. {
  40. int res = 0;
  41. struct localuser *u;
  42. char tmp[256];
  43. char *options;
  44. int option_skip=0;
  45. int option_noanswer = 0;
  46. char *stringp;
  47. if (!data || ast_strlen_zero((char *)data)) {
  48. ast_log(LOG_WARNING, "Playback requires an argument (filename)\n");
  49. return -1;
  50. }
  51. strncpy(tmp, (char *)data, sizeof(tmp)-1);
  52. stringp=tmp;
  53. strsep(&stringp, "|");
  54. options = strsep(&stringp, "|");
  55. if (options && !strcasecmp(options, "skip"))
  56. option_skip = 1;
  57. if (options && !strcasecmp(options, "noanswer"))
  58. option_noanswer = 1;
  59. LOCAL_USER_ADD(u);
  60. if (chan->_state != AST_STATE_UP) {
  61. if (option_skip) {
  62. /* At the user's option, skip if the line is not up */
  63. LOCAL_USER_REMOVE(u);
  64. return 0;
  65. } else if (!option_noanswer)
  66. /* Otherwise answer unless we're supposed to send this while on-hook */
  67. res = ast_answer(chan);
  68. }
  69. if (!res) {
  70. ast_stopstream(chan);
  71. res = ast_streamfile(chan, tmp, chan->language);
  72. if (!res)
  73. res = ast_waitstream(chan, "");
  74. else {
  75. ast_log(LOG_WARNING, "ast_streamfile failed on %s for %s\n", chan->name, (char *)data);
  76. res = 0;
  77. }
  78. ast_stopstream(chan);
  79. }
  80. LOCAL_USER_REMOVE(u);
  81. return res;
  82. }
  83. int unload_module(void)
  84. {
  85. STANDARD_HANGUP_LOCALUSERS;
  86. return ast_unregister_application(app);
  87. }
  88. int load_module(void)
  89. {
  90. return ast_register_application(app, playback_exec, synopsis, descrip);
  91. }
  92. char *description(void)
  93. {
  94. return tdesc;
  95. }
  96. int usecount(void)
  97. {
  98. int res;
  99. STANDARD_USECOUNT(res);
  100. return res;
  101. }
  102. char *key()
  103. {
  104. return ASTERISK_GPL_KEY;
  105. }