app_nbscat.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 Silly application to play an NBScat file -- uses nbscat8k
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * \ingroup applications
  25. */
  26. /*** MODULEINFO
  27. <depend>working_fork</depend>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include <fcntl.h>
  32. #include <sys/time.h>
  33. #include <sys/socket.h>
  34. #include <signal.h>
  35. #ifdef HAVE_CAP
  36. #include <sys/capability.h>
  37. #endif /* HAVE_CAP */
  38. #include "asterisk/lock.h"
  39. #include "asterisk/file.h"
  40. #include "asterisk/channel.h"
  41. #include "asterisk/frame.h"
  42. #include "asterisk/pbx.h"
  43. #include "asterisk/module.h"
  44. #include "asterisk/translate.h"
  45. #define LOCAL_NBSCAT "/usr/local/bin/nbscat8k"
  46. #define NBSCAT "/usr/bin/nbscat8k"
  47. #ifndef AF_LOCAL
  48. #define AF_LOCAL AF_UNIX
  49. #endif
  50. static char *app = "NBScat";
  51. static char *synopsis = "Play an NBS local stream";
  52. static char *descrip =
  53. " NBScat(): Executes nbscat to listen to the local NBS stream.\n"
  54. "User can exit by pressing any key.\n";
  55. static int NBScatplay(int fd)
  56. {
  57. int res;
  58. int x;
  59. sigset_t fullset, oldset;
  60. #ifdef HAVE_CAP
  61. cap_t cap;
  62. #endif
  63. sigfillset(&fullset);
  64. pthread_sigmask(SIG_BLOCK, &fullset, &oldset);
  65. res = fork();
  66. if (res < 0)
  67. ast_log(LOG_WARNING, "Fork failed\n");
  68. if (res) {
  69. pthread_sigmask(SIG_SETMASK, &oldset, NULL);
  70. return res;
  71. }
  72. signal(SIGPIPE, SIG_DFL);
  73. pthread_sigmask(SIG_UNBLOCK, &fullset, NULL);
  74. #ifdef HAVE_CAP
  75. cap = cap_from_text("cap_net_admin-eip");
  76. if (cap_set_proc(cap)) {
  77. /* Careful with order! Logging cannot happen after we close FDs */
  78. ast_log(LOG_WARNING, "Unable to remove capabilities.\n");
  79. }
  80. cap_free(cap);
  81. #endif
  82. if (ast_opt_high_priority)
  83. ast_set_priority(0);
  84. dup2(fd, STDOUT_FILENO);
  85. for (x = STDERR_FILENO + 1; x < 1024; x++) {
  86. if (x != STDOUT_FILENO)
  87. close(x);
  88. }
  89. /* Most commonly installed in /usr/local/bin */
  90. execl(NBSCAT, "nbscat8k", "-d", (char *)NULL);
  91. execl(LOCAL_NBSCAT, "nbscat8k", "-d", (char *)NULL);
  92. ast_log(LOG_WARNING, "Execute of nbscat8k failed\n");
  93. _exit(0);
  94. }
  95. static int timed_read(int fd, void *data, int datalen)
  96. {
  97. int res;
  98. struct pollfd fds[1];
  99. fds[0].fd = fd;
  100. fds[0].events = POLLIN;
  101. res = ast_poll(fds, 1, 2000);
  102. if (res < 1) {
  103. ast_log(LOG_NOTICE, "Selected timed out/errored out with %d\n", res);
  104. return -1;
  105. }
  106. return read(fd, data, datalen);
  107. }
  108. static int NBScat_exec(struct ast_channel *chan, void *data)
  109. {
  110. int res=0;
  111. int fds[2];
  112. int ms = -1;
  113. int pid = -1;
  114. int owriteformat;
  115. struct timeval next;
  116. struct ast_frame *f;
  117. struct myframe {
  118. struct ast_frame f;
  119. char offset[AST_FRIENDLY_OFFSET];
  120. short frdata[160];
  121. } myf;
  122. if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds)) {
  123. ast_log(LOG_WARNING, "Unable to create socketpair\n");
  124. return -1;
  125. }
  126. ast_stopstream(chan);
  127. owriteformat = chan->writeformat;
  128. res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
  129. if (res < 0) {
  130. ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
  131. return -1;
  132. }
  133. res = NBScatplay(fds[1]);
  134. /* Wait 1000 ms first */
  135. next = ast_tvnow();
  136. next.tv_sec += 1;
  137. if (res >= 0) {
  138. pid = res;
  139. /* Order is important -- there's almost always going to be mp3... we want to prioritize the
  140. user */
  141. for (;;) {
  142. ms = ast_tvdiff_ms(next, ast_tvnow());
  143. if (ms <= 0) {
  144. res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata));
  145. if (res > 0) {
  146. myf.f.frametype = AST_FRAME_VOICE;
  147. myf.f.subclass = AST_FORMAT_SLINEAR;
  148. myf.f.datalen = res;
  149. myf.f.samples = res / 2;
  150. myf.f.mallocd = 0;
  151. myf.f.offset = AST_FRIENDLY_OFFSET;
  152. myf.f.src = __PRETTY_FUNCTION__;
  153. myf.f.delivery.tv_sec = 0;
  154. myf.f.delivery.tv_usec = 0;
  155. myf.f.data = myf.frdata;
  156. if (ast_write(chan, &myf.f) < 0) {
  157. res = -1;
  158. break;
  159. }
  160. } else {
  161. ast_debug(1, "No more mp3\n");
  162. res = 0;
  163. break;
  164. }
  165. next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
  166. } else {
  167. ms = ast_waitfor(chan, ms);
  168. if (ms < 0) {
  169. ast_debug(1, "Hangup detected\n");
  170. res = -1;
  171. break;
  172. }
  173. if (ms) {
  174. f = ast_read(chan);
  175. if (!f) {
  176. ast_debug(1, "Null frame == hangup() detected\n");
  177. res = -1;
  178. break;
  179. }
  180. if (f->frametype == AST_FRAME_DTMF) {
  181. ast_debug(1, "User pressed a key\n");
  182. ast_frfree(f);
  183. res = 0;
  184. break;
  185. }
  186. ast_frfree(f);
  187. }
  188. }
  189. }
  190. }
  191. close(fds[0]);
  192. close(fds[1]);
  193. if (pid > -1)
  194. kill(pid, SIGKILL);
  195. if (!res && owriteformat)
  196. ast_set_write_format(chan, owriteformat);
  197. return res;
  198. }
  199. static int unload_module(void)
  200. {
  201. return ast_unregister_application(app);
  202. }
  203. static int load_module(void)
  204. {
  205. return ast_register_application(app, NBScat_exec, synopsis, descrip);
  206. }
  207. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Silly NBS Stream Application");