app_nbscat.c 4.6 KB

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