app_nbscat.c 4.9 KB

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