app_mp3.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 MP3 file -- uses mpg123
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * \note Add feature to play local M3U playlist file
  25. * Vincent Li <mchun.li@gmail.com>
  26. *
  27. * \ingroup applications
  28. */
  29. /*** MODULEINFO
  30. <support_level>extended</support_level>
  31. ***/
  32. #include "asterisk.h"
  33. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  34. #include <sys/time.h>
  35. #include <signal.h>
  36. #include "asterisk/lock.h"
  37. #include "asterisk/file.h"
  38. #include "asterisk/channel.h"
  39. #include "asterisk/frame.h"
  40. #include "asterisk/pbx.h"
  41. #include "asterisk/module.h"
  42. #include "asterisk/translate.h"
  43. #include "asterisk/app.h"
  44. #include "asterisk/format_cache.h"
  45. #define LOCAL_MPG_123 "/usr/local/bin/mpg123"
  46. #define MPG_123 "/usr/bin/mpg123"
  47. /*** DOCUMENTATION
  48. <application name="MP3Player" language="en_US">
  49. <synopsis>
  50. Play an MP3 file or M3U playlist file or stream.
  51. </synopsis>
  52. <syntax>
  53. <parameter name="Location" required="true">
  54. <para>Location of the file to be played.
  55. (argument passed to mpg123)</para>
  56. </parameter>
  57. </syntax>
  58. <description>
  59. <para>Executes mpg123 to play the given location, which typically would be a mp3 filename
  60. or m3u playlist filename or a URL. Please read http://en.wikipedia.org/wiki/M3U
  61. to see how M3U playlist file format is like, Example usage would be
  62. exten => 1234,1,MP3Player(/var/lib/asterisk/playlist.m3u)
  63. User can exit by pressing any key on the dialpad, or by hanging up.</para>
  64. <para>This application does not automatically answer and should be preceeded by an
  65. application such as Answer() or Progress().</para>
  66. </description>
  67. </application>
  68. ***/
  69. static char *app = "MP3Player";
  70. static int mp3play(const char *filename, int fd)
  71. {
  72. int res;
  73. res = ast_safe_fork(0);
  74. if (res < 0)
  75. ast_log(LOG_WARNING, "Fork failed\n");
  76. if (res) {
  77. return res;
  78. }
  79. if (ast_opt_high_priority)
  80. ast_set_priority(0);
  81. dup2(fd, STDOUT_FILENO);
  82. ast_close_fds_above_n(STDERR_FILENO);
  83. /* Execute mpg123, but buffer if it's a net connection */
  84. if (!strncasecmp(filename, "http://", 7)) {
  85. /* Most commonly installed in /usr/local/bin */
  86. execl(LOCAL_MPG_123, "mpg123", "-q", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
  87. /* But many places has it in /usr/bin */
  88. execl(MPG_123, "mpg123", "-q", "-s", "-b", "1024","-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
  89. /* As a last-ditch effort, try to use PATH */
  90. execlp("mpg123", "mpg123", "-q", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
  91. }
  92. else if (strstr(filename, ".m3u")) {
  93. /* Most commonly installed in /usr/local/bin */
  94. execl(LOCAL_MPG_123, "mpg123", "-q", "-z", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", "-@", filename, (char *)NULL);
  95. /* But many places has it in /usr/bin */
  96. execl(MPG_123, "mpg123", "-q", "-z", "-s", "-b", "1024","-f", "8192", "--mono", "-r", "8000", "-@", filename, (char *)NULL);
  97. /* As a last-ditch effort, try to use PATH */
  98. execlp("mpg123", "mpg123", "-q", "-z", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", "-@", filename, (char *)NULL);
  99. }
  100. else {
  101. /* Most commonly installed in /usr/local/bin */
  102. execl(MPG_123, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
  103. /* But many places has it in /usr/bin */
  104. execl(LOCAL_MPG_123, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
  105. /* As a last-ditch effort, try to use PATH */
  106. execlp("mpg123", "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
  107. }
  108. /* Can't use ast_log since FD's are closed */
  109. fprintf(stderr, "Execute of mpg123 failed\n");
  110. _exit(0);
  111. }
  112. static int timed_read(int fd, void *data, int datalen, int timeout)
  113. {
  114. int res;
  115. struct pollfd fds[1];
  116. fds[0].fd = fd;
  117. fds[0].events = POLLIN;
  118. res = ast_poll(fds, 1, timeout);
  119. if (res < 1) {
  120. ast_log(LOG_NOTICE, "Poll timed out/errored out with %d\n", res);
  121. return -1;
  122. }
  123. return read(fd, data, datalen);
  124. }
  125. static int mp3_exec(struct ast_channel *chan, const char *data)
  126. {
  127. int res=0;
  128. int fds[2];
  129. int ms = -1;
  130. int pid = -1;
  131. RAII_VAR(struct ast_format *, owriteformat, NULL, ao2_cleanup);
  132. int timeout = 2000;
  133. struct timeval next;
  134. struct ast_frame *f;
  135. struct myframe {
  136. struct ast_frame f;
  137. char offset[AST_FRIENDLY_OFFSET];
  138. short frdata[160];
  139. } myf = {
  140. .f = { 0, },
  141. };
  142. if (ast_strlen_zero(data)) {
  143. ast_log(LOG_WARNING, "MP3 Playback requires an argument (filename)\n");
  144. return -1;
  145. }
  146. if (pipe(fds)) {
  147. ast_log(LOG_WARNING, "Unable to create pipe\n");
  148. return -1;
  149. }
  150. ast_stopstream(chan);
  151. owriteformat = ao2_bump(ast_channel_writeformat(chan));
  152. res = ast_set_write_format(chan, ast_format_slin);
  153. if (res < 0) {
  154. ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
  155. return -1;
  156. }
  157. myf.f.frametype = AST_FRAME_VOICE;
  158. myf.f.subclass.format = ast_format_slin;
  159. myf.f.mallocd = 0;
  160. myf.f.offset = AST_FRIENDLY_OFFSET;
  161. myf.f.src = __PRETTY_FUNCTION__;
  162. myf.f.delivery.tv_sec = 0;
  163. myf.f.delivery.tv_usec = 0;
  164. myf.f.data.ptr = myf.frdata;
  165. res = mp3play(data, fds[1]);
  166. if (!strncasecmp(data, "http://", 7)) {
  167. timeout = 10000;
  168. }
  169. /* Wait 1000 ms first */
  170. next = ast_tvnow();
  171. next.tv_sec += 1;
  172. if (res >= 0) {
  173. pid = res;
  174. /* Order is important -- there's almost always going to be mp3... we want to prioritize the
  175. user */
  176. for (;;) {
  177. ms = ast_tvdiff_ms(next, ast_tvnow());
  178. if (ms <= 0) {
  179. res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata), timeout);
  180. if (res > 0) {
  181. myf.f.datalen = res;
  182. myf.f.samples = res / 2;
  183. if (ast_write(chan, &myf.f) < 0) {
  184. res = -1;
  185. break;
  186. }
  187. } else {
  188. ast_debug(1, "No more mp3\n");
  189. res = 0;
  190. break;
  191. }
  192. next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
  193. } else {
  194. ms = ast_waitfor(chan, ms);
  195. if (ms < 0) {
  196. ast_debug(1, "Hangup detected\n");
  197. res = -1;
  198. break;
  199. }
  200. if (ms) {
  201. f = ast_read(chan);
  202. if (!f) {
  203. ast_debug(1, "Null frame == hangup() detected\n");
  204. res = -1;
  205. break;
  206. }
  207. if (f->frametype == AST_FRAME_DTMF) {
  208. ast_debug(1, "User pressed a key\n");
  209. ast_frfree(f);
  210. res = 0;
  211. break;
  212. }
  213. ast_frfree(f);
  214. }
  215. }
  216. }
  217. }
  218. close(fds[0]);
  219. close(fds[1]);
  220. if (pid > -1)
  221. kill(pid, SIGKILL);
  222. if (!res && owriteformat)
  223. ast_set_write_format(chan, owriteformat);
  224. ast_frfree(&myf.f);
  225. return res;
  226. }
  227. static int unload_module(void)
  228. {
  229. return ast_unregister_application(app);
  230. }
  231. static int load_module(void)
  232. {
  233. return ast_register_application_xml(app, mp3_exec);
  234. }
  235. AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Silly MP3 Application");