Wed Jan 8 2020 09:49:40

Asterisk developer's documentation


app_mp3.c
Go to the documentation of this file.
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 
19 /*! \file
20  *
21  * \brief Silly application to play an MP3 file -- uses mpg123
22  *
23  * \author Mark Spencer <markster@digium.com>
24  *
25  * \note Add feature to play local M3U playlist file
26  * Vincent Li <mchun.li@gmail.com>
27  *
28  * \ingroup applications
29  */
30 
31 /*** MODULEINFO
32  <support_level>extended</support_level>
33  ***/
34 
35 #include "asterisk.h"
36 
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 336716 $")
38 
39 #include <sys/time.h>
40 #include <signal.h>
41 
42 #include "asterisk/lock.h"
43 #include "asterisk/file.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/frame.h"
46 #include "asterisk/pbx.h"
47 #include "asterisk/module.h"
48 #include "asterisk/translate.h"
49 #include "asterisk/app.h"
50 
51 #define LOCAL_MPG_123 "/usr/local/bin/mpg123"
52 #define MPG_123 "/usr/bin/mpg123"
53 
54 /*** DOCUMENTATION
55  <application name="MP3Player" language="en_US">
56  <synopsis>
57  Play an MP3 file or M3U playlist file or stream.
58  </synopsis>
59  <syntax>
60  <parameter name="Location" required="true">
61  <para>Location of the file to be played.
62  (argument passed to mpg123)</para>
63  </parameter>
64  </syntax>
65  <description>
66  <para>Executes mpg123 to play the given location, which typically would be a mp3 filename
67  or m3u playlist filename or a URL. Please read http://en.wikipedia.org/wiki/M3U
68  to see how M3U playlist file format is like, Example usage would be
69  exten => 1234,1,MP3Player(/var/lib/asterisk/playlist.m3u)
70  User can exit by pressing any key on the dialpad, or by hanging up.</para>
71  <para>This application does not automatically answer and should be preceeded by an
72  application such as Answer() or Progress().</para>
73  </description>
74  </application>
75 
76  ***/
77 static char *app = "MP3Player";
78 
79 static int mp3play(const char *filename, int fd)
80 {
81  int res;
82 
83  res = ast_safe_fork(0);
84  if (res < 0)
85  ast_log(LOG_WARNING, "Fork failed\n");
86  if (res) {
87  return res;
88  }
91 
92  dup2(fd, STDOUT_FILENO);
93  ast_close_fds_above_n(STDERR_FILENO);
94 
95  /* Execute mpg123, but buffer if it's a net connection */
96  if (!strncasecmp(filename, "http://", 7)) {
97  /* Most commonly installed in /usr/local/bin */
98  execl(LOCAL_MPG_123, "mpg123", "-q", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
99  /* But many places has it in /usr/bin */
100  execl(MPG_123, "mpg123", "-q", "-s", "-b", "1024","-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
101  /* As a last-ditch effort, try to use PATH */
102  execlp("mpg123", "mpg123", "-q", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
103  }
104  else if (strstr(filename, ".m3u")) {
105  /* Most commonly installed in /usr/local/bin */
106  execl(LOCAL_MPG_123, "mpg123", "-q", "-z", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", "-@", filename, (char *)NULL);
107  /* But many places has it in /usr/bin */
108  execl(MPG_123, "mpg123", "-q", "-z", "-s", "-b", "1024","-f", "8192", "--mono", "-r", "8000", "-@", filename, (char *)NULL);
109  /* As a last-ditch effort, try to use PATH */
110  execlp("mpg123", "mpg123", "-q", "-z", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", "-@", filename, (char *)NULL);
111  }
112  else {
113  /* Most commonly installed in /usr/local/bin */
114  execl(MPG_123, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
115  /* But many places has it in /usr/bin */
116  execl(LOCAL_MPG_123, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
117  /* As a last-ditch effort, try to use PATH */
118  execlp("mpg123", "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
119  }
120  /* Can't use ast_log since FD's are closed */
121  fprintf(stderr, "Execute of mpg123 failed\n");
122  _exit(0);
123 }
124 
125 static int timed_read(int fd, void *data, int datalen, int timeout)
126 {
127  int res;
128  struct pollfd fds[1];
129  fds[0].fd = fd;
130  fds[0].events = POLLIN;
131  res = ast_poll(fds, 1, timeout);
132  if (res < 1) {
133  ast_log(LOG_NOTICE, "Poll timed out/errored out with %d\n", res);
134  return -1;
135  }
136  return read(fd, data, datalen);
137 
138 }
139 
140 static int mp3_exec(struct ast_channel *chan, const char *data)
141 {
142  int res=0;
143  int fds[2];
144  int ms = -1;
145  int pid = -1;
146  int owriteformat;
147  int timeout = 2000;
148  struct timeval next;
149  struct ast_frame *f;
150  struct myframe {
151  struct ast_frame f;
153  short frdata[160];
154  } myf = {
155  .f = { 0, },
156  };
157 
158  if (ast_strlen_zero(data)) {
159  ast_log(LOG_WARNING, "MP3 Playback requires an argument (filename)\n");
160  return -1;
161  }
162 
163  if (pipe(fds)) {
164  ast_log(LOG_WARNING, "Unable to create pipe\n");
165  return -1;
166  }
167 
168  ast_stopstream(chan);
169 
170  owriteformat = chan->writeformat;
172  if (res < 0) {
173  ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
174  return -1;
175  }
176 
177  res = mp3play(data, fds[1]);
178  if (!strncasecmp(data, "http://", 7)) {
179  timeout = 10000;
180  }
181  /* Wait 1000 ms first */
182  next = ast_tvnow();
183  next.tv_sec += 1;
184  if (res >= 0) {
185  pid = res;
186  /* Order is important -- there's almost always going to be mp3... we want to prioritize the
187  user */
188  for (;;) {
189  ms = ast_tvdiff_ms(next, ast_tvnow());
190  if (ms <= 0) {
191  res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata), timeout);
192  if (res > 0) {
193  myf.f.frametype = AST_FRAME_VOICE;
194  myf.f.subclass.codec = AST_FORMAT_SLINEAR;
195  myf.f.datalen = res;
196  myf.f.samples = res / 2;
197  myf.f.mallocd = 0;
198  myf.f.offset = AST_FRIENDLY_OFFSET;
199  myf.f.src = __PRETTY_FUNCTION__;
200  myf.f.delivery.tv_sec = 0;
201  myf.f.delivery.tv_usec = 0;
202  myf.f.data.ptr = myf.frdata;
203  if (ast_write(chan, &myf.f) < 0) {
204  res = -1;
205  break;
206  }
207  } else {
208  ast_debug(1, "No more mp3\n");
209  res = 0;
210  break;
211  }
212  next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
213  } else {
214  ms = ast_waitfor(chan, ms);
215  if (ms < 0) {
216  ast_debug(1, "Hangup detected\n");
217  res = -1;
218  break;
219  }
220  if (ms) {
221  f = ast_read(chan);
222  if (!f) {
223  ast_debug(1, "Null frame == hangup() detected\n");
224  res = -1;
225  break;
226  }
227  if (f->frametype == AST_FRAME_DTMF) {
228  ast_debug(1, "User pressed a key\n");
229  ast_frfree(f);
230  res = 0;
231  break;
232  }
233  ast_frfree(f);
234  }
235  }
236  }
237  }
238  close(fds[0]);
239  close(fds[1]);
240 
241  if (pid > -1)
242  kill(pid, SIGKILL);
243  if (!res && owriteformat)
244  ast_set_write_format(chan, owriteformat);
245 
246  return res;
247 }
248 
249 static int unload_module(void)
250 {
251  return ast_unregister_application(app);
252 }
253 
254 static int load_module(void)
255 {
257 }
258 
259 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Silly MP3 Application");
Main Channel structure associated with a channel.
Definition: channel.h:742
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:396
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
int offset
Definition: frame.h:156
int ast_safe_fork(int stop_reaper)
Common routine to safely fork without a chance of a signal handler firing badly in the child...
Definition: app.c:2242
format_t writeformat
Definition: channel.h:854
Support for translation of data formats. translate.c.
#define LOG_WARNING
Definition: logger.h:144
static int timed_read(int fd, void *data, int datalen, int timeout)
Definition: app_mp3.c:125
#define AST_FRAME_DTMF
Definition: frame.h:128
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4383
static int unload_module(void)
Definition: app_mp3.c:249
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:142
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:90
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx.c:7705
int ast_set_write_format(struct ast_channel *chan, format_t format)
Sets write format on channel chan Set write format for channel to whichever component of &quot;format&quot; is ...
Definition: channel.c:5307
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
General Asterisk PBX channel definitions.
#define AST_FRIENDLY_OFFSET
Offset into a frame&#39;s data buffer.
Definition: frame.h:204
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
#define ast_poll(a, b, c)
Definition: poll-compat.h:88
Asterisk internal frame definitions.
struct timeval ast_samp2tv(unsigned int _nsamp, unsigned int _rate)
Returns a timeval corresponding to the duration of n samples at rate r. Useful to convert samples to ...
Definition: time.h:191
Core PBX routines and definitions.
int ast_set_priority(int)
We set ourselves to a high priority, that we might pre-empt everything else. If your PBX has heavy ac...
Definition: asterisk.c:1650
struct timeval ast_tvadd(struct timeval a, struct timeval b)
Returns the sum of two timevals a + b.
Definition: utils.c:1587
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define LOG_NOTICE
Definition: logger.h:133
void ast_close_fds_above_n(int n)
Common routine for child processes, to close all fds prior to exec(2)
Definition: app.c:2237
static struct ast_format f[]
Definition: format_g726.c:181
int ast_write(struct ast_channel *chan, struct ast_frame *frame)
Write a frame to a channel This function writes the given frame to the indicated channel.
Definition: channel.c:4916
static int mp3play(const char *filename, int fd)
Definition: app_mp3.c:79
#define LOCAL_MPG_123
Definition: app_mp3.c:51
#define AST_FORMAT_SLINEAR
Definition: frame.h:254
int ast_waitfor(struct ast_channel *chan, int ms)
Wait for input on a channel.
Definition: channel.c:3539
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
static char * app
Definition: app_mp3.c:77
Data structure associated with a single frame of data.
Definition: frame.h:142
static int load_module(void)
Definition: app_mp3.c:254
#define MPG_123
Definition: app_mp3.c:52
enum ast_frame_type frametype
Definition: frame.h:144
#define ast_frfree(fr)
Definition: frame.h:583
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
static int mp3_exec(struct ast_channel *chan, const char *data)
Definition: app_mp3.c:140
Asterisk module definitions.
int ast_stopstream(struct ast_channel *c)
Stops a stream.
Definition: file.c:128
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:437
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
#define ast_opt_high_priority
Definition: options.h:108