app_mp3.c 8.9 KB

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