app_nbscat.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. <support_level>extended</support_level>
  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. #include "asterisk/lock.h"
  36. #include "asterisk/file.h"
  37. #include "asterisk/channel.h"
  38. #include "asterisk/frame.h"
  39. #include "asterisk/pbx.h"
  40. #include "asterisk/module.h"
  41. #include "asterisk/translate.h"
  42. #include "asterisk/app.h"
  43. #include "asterisk/format_cache.h"
  44. /*** DOCUMENTATION
  45. <application name="NBScat" language="en_US">
  46. <synopsis>
  47. Play an NBS local stream.
  48. </synopsis>
  49. <syntax />
  50. <description>
  51. <para>Executes nbscat to listen to the local NBS stream.
  52. User can exit by pressing any key.</para>
  53. </description>
  54. </application>
  55. ***/
  56. #define LOCAL_NBSCAT "/usr/local/bin/nbscat8k"
  57. #define NBSCAT "/usr/bin/nbscat8k"
  58. #ifndef AF_LOCAL
  59. #define AF_LOCAL AF_UNIX
  60. #endif
  61. static char *app = "NBScat";
  62. static int NBScatplay(int fd)
  63. {
  64. int res;
  65. res = ast_safe_fork(0);
  66. if (res < 0) {
  67. ast_log(LOG_WARNING, "Fork failed\n");
  68. }
  69. if (res) {
  70. return res;
  71. }
  72. if (ast_opt_high_priority)
  73. ast_set_priority(0);
  74. dup2(fd, STDOUT_FILENO);
  75. ast_close_fds_above_n(STDERR_FILENO);
  76. /* Most commonly installed in /usr/local/bin */
  77. execl(NBSCAT, "nbscat8k", "-d", (char *)NULL);
  78. execl(LOCAL_NBSCAT, "nbscat8k", "-d", (char *)NULL);
  79. fprintf(stderr, "Execute of nbscat8k failed\n");
  80. _exit(0);
  81. }
  82. static int timed_read(int fd, void *data, int datalen)
  83. {
  84. int res;
  85. struct pollfd fds[1];
  86. fds[0].fd = fd;
  87. fds[0].events = POLLIN;
  88. res = ast_poll(fds, 1, 2000);
  89. if (res < 1) {
  90. ast_log(LOG_NOTICE, "Selected timed out/errored out with %d\n", res);
  91. return -1;
  92. }
  93. return read(fd, data, datalen);
  94. }
  95. static int NBScat_exec(struct ast_channel *chan, const char *data)
  96. {
  97. int res=0;
  98. int fds[2];
  99. int ms = -1;
  100. int pid = -1;
  101. struct ast_format *owriteformat;
  102. struct timeval next;
  103. struct ast_frame *f;
  104. struct myframe {
  105. struct ast_frame f;
  106. char offset[AST_FRIENDLY_OFFSET];
  107. short frdata[160];
  108. } myf;
  109. if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds)) {
  110. ast_log(LOG_WARNING, "Unable to create socketpair\n");
  111. return -1;
  112. }
  113. ast_stopstream(chan);
  114. owriteformat = ao2_bump(ast_channel_writeformat(chan));
  115. res = ast_set_write_format(chan, ast_format_slin);
  116. if (res < 0) {
  117. ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
  118. ao2_cleanup(owriteformat);
  119. return -1;
  120. }
  121. myf.f.frametype = AST_FRAME_VOICE;
  122. myf.f.subclass.format = ast_format_slin;
  123. myf.f.mallocd = 0;
  124. myf.f.offset = AST_FRIENDLY_OFFSET;
  125. myf.f.src = __PRETTY_FUNCTION__;
  126. myf.f.delivery.tv_sec = 0;
  127. myf.f.delivery.tv_usec = 0;
  128. myf.f.data.ptr = myf.frdata;
  129. res = NBScatplay(fds[1]);
  130. /* Wait 1000 ms first */
  131. next = ast_tvnow();
  132. next.tv_sec += 1;
  133. if (res >= 0) {
  134. pid = res;
  135. /* Order is important -- there's almost always going to be mp3... we want to prioritize the
  136. user */
  137. for (;;) {
  138. ms = ast_tvdiff_ms(next, ast_tvnow());
  139. if (ms <= 0) {
  140. res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata));
  141. if (res > 0) {
  142. myf.f.datalen = res;
  143. myf.f.samples = res / 2;
  144. if (ast_write(chan, &myf.f) < 0) {
  145. res = -1;
  146. break;
  147. }
  148. } else {
  149. ast_debug(1, "No more mp3\n");
  150. res = 0;
  151. break;
  152. }
  153. next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
  154. } else {
  155. ms = ast_waitfor(chan, ms);
  156. if (ms < 0) {
  157. ast_debug(1, "Hangup detected\n");
  158. res = -1;
  159. break;
  160. }
  161. if (ms) {
  162. f = ast_read(chan);
  163. if (!f) {
  164. ast_debug(1, "Null frame == hangup() detected\n");
  165. res = -1;
  166. break;
  167. }
  168. if (f->frametype == AST_FRAME_DTMF) {
  169. ast_debug(1, "User pressed a key\n");
  170. ast_frfree(f);
  171. res = 0;
  172. break;
  173. }
  174. ast_frfree(f);
  175. }
  176. }
  177. }
  178. }
  179. close(fds[0]);
  180. close(fds[1]);
  181. ast_frfree(&myf.f);
  182. if (pid > -1)
  183. kill(pid, SIGKILL);
  184. if (!res && owriteformat)
  185. ast_set_write_format(chan, owriteformat);
  186. ao2_cleanup(owriteformat);
  187. return res;
  188. }
  189. static int unload_module(void)
  190. {
  191. return ast_unregister_application(app);
  192. }
  193. static int load_module(void)
  194. {
  195. return ast_register_application_xml(app, NBScat_exec);
  196. }
  197. AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Silly NBS Stream Application");