00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <string.h>
00027 #include <stdio.h>
00028 #include <signal.h>
00029 #include <stdlib.h>
00030 #include <unistd.h>
00031 #include <fcntl.h>
00032 #include <sys/time.h>
00033
00034 #include "asterisk.h"
00035
00036 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 48374 $")
00037
00038 #include "asterisk/lock.h"
00039 #include "asterisk/file.h"
00040 #include "asterisk/logger.h"
00041 #include "asterisk/channel.h"
00042 #include "asterisk/frame.h"
00043 #include "asterisk/pbx.h"
00044 #include "asterisk/module.h"
00045 #include "asterisk/translate.h"
00046 #include "asterisk/options.h"
00047
00048 #define LOCAL_MPG_123 "/usr/local/bin/mpg123"
00049 #define MPG_123 "/usr/bin/mpg123"
00050
00051 static char *tdesc = "Silly MP3 Application";
00052
00053 static char *app = "MP3Player";
00054
00055 static char *synopsis = "Play an MP3 file or stream";
00056
00057 static char *descrip =
00058 " MP3Player(location) Executes mpg123 to play the given location,\n"
00059 "which typically would be a filename or a URL. User can exit by pressing\n"
00060 "any key on the dialpad, or by hanging up.";
00061
00062 STANDARD_LOCAL_USER;
00063
00064 LOCAL_USER_DECL;
00065
00066 static int mp3play(char *filename, int fd)
00067 {
00068 int res;
00069 int x;
00070 sigset_t fullset, oldset;
00071
00072 sigfillset(&fullset);
00073 pthread_sigmask(SIG_BLOCK, &fullset, &oldset);
00074
00075 res = fork();
00076 if (res < 0)
00077 ast_log(LOG_WARNING, "Fork failed\n");
00078 if (res) {
00079 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
00080 return res;
00081 }
00082 if (option_highpriority)
00083 ast_set_priority(0);
00084 signal(SIGPIPE, SIG_DFL);
00085 pthread_sigmask(SIG_UNBLOCK, &fullset, NULL);
00086
00087 dup2(fd, STDOUT_FILENO);
00088 for (x=STDERR_FILENO + 1;x<256;x++) {
00089 if (x != STDOUT_FILENO)
00090 close(x);
00091 }
00092
00093 if (!strncasecmp(filename, "http://", 7)) {
00094
00095 execl(LOCAL_MPG_123, "mpg123", "-q", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
00096
00097 execl(MPG_123, "mpg123", "-q", "-s", "-b", "1024","-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
00098
00099 execlp("mpg123", "mpg123", "-q", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
00100 }
00101 else {
00102
00103 execl(MPG_123, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
00104
00105 execl(LOCAL_MPG_123, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
00106
00107 execlp("mpg123", "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
00108 }
00109 ast_log(LOG_WARNING, "Execute of mpg123 failed\n");
00110 _exit(0);
00111 }
00112
00113 static int timed_read(int fd, void *data, int datalen, int timeout)
00114 {
00115 int res;
00116 struct pollfd fds[1];
00117 fds[0].fd = fd;
00118 fds[0].events = POLLIN;
00119 res = poll(fds, 1, timeout);
00120 if (res < 1) {
00121 ast_log(LOG_NOTICE, "Poll timed out/errored out with %d\n", res);
00122 return -1;
00123 }
00124 return read(fd, data, datalen);
00125
00126 }
00127
00128 static int mp3_exec(struct ast_channel *chan, void *data)
00129 {
00130 int res=0;
00131 struct localuser *u;
00132 int fds[2];
00133 int ms = -1;
00134 int pid = -1;
00135 int owriteformat;
00136 int timeout = 2000;
00137 struct timeval next;
00138 struct ast_frame *f;
00139 struct myframe {
00140 struct ast_frame f;
00141 char offset[AST_FRIENDLY_OFFSET];
00142 short frdata[160];
00143 } myf;
00144
00145 if (ast_strlen_zero(data)) {
00146 ast_log(LOG_WARNING, "MP3 Playback requires an argument (filename)\n");
00147 return -1;
00148 }
00149
00150 LOCAL_USER_ADD(u);
00151
00152 if (pipe(fds)) {
00153 ast_log(LOG_WARNING, "Unable to create pipe\n");
00154 LOCAL_USER_REMOVE(u);
00155 return -1;
00156 }
00157
00158 ast_stopstream(chan);
00159
00160 owriteformat = chan->writeformat;
00161 res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
00162 if (res < 0) {
00163 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
00164 LOCAL_USER_REMOVE(u);
00165 return -1;
00166 }
00167
00168 res = mp3play((char *)data, fds[1]);
00169 if (!strncasecmp((char *)data, "http://", 7)) {
00170 timeout = 10000;
00171 }
00172
00173 next = ast_tvnow();
00174 next.tv_sec += 1;
00175 if (res >= 0) {
00176 pid = res;
00177
00178
00179 for (;;) {
00180 ms = ast_tvdiff_ms(next, ast_tvnow());
00181 if (ms <= 0) {
00182 res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata), timeout);
00183 if (res > 0) {
00184 myf.f.frametype = AST_FRAME_VOICE;
00185 myf.f.subclass = AST_FORMAT_SLINEAR;
00186 myf.f.datalen = res;
00187 myf.f.samples = res / 2;
00188 myf.f.mallocd = 0;
00189 myf.f.offset = AST_FRIENDLY_OFFSET;
00190 myf.f.src = __PRETTY_FUNCTION__;
00191 myf.f.delivery.tv_sec = 0;
00192 myf.f.delivery.tv_usec = 0;
00193 myf.f.data = myf.frdata;
00194 if (ast_write(chan, &myf.f) < 0) {
00195 res = -1;
00196 break;
00197 }
00198 } else {
00199 ast_log(LOG_DEBUG, "No more mp3\n");
00200 res = 0;
00201 break;
00202 }
00203 next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
00204 } else {
00205 ms = ast_waitfor(chan, ms);
00206 if (ms < 0) {
00207 ast_log(LOG_DEBUG, "Hangup detected\n");
00208 res = -1;
00209 break;
00210 }
00211 if (ms) {
00212 f = ast_read(chan);
00213 if (!f) {
00214 ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
00215 res = -1;
00216 break;
00217 }
00218 if (f->frametype == AST_FRAME_DTMF) {
00219 ast_log(LOG_DEBUG, "User pressed a key\n");
00220 ast_frfree(f);
00221 res = 0;
00222 break;
00223 }
00224 ast_frfree(f);
00225 }
00226 }
00227 }
00228 }
00229 close(fds[0]);
00230 close(fds[1]);
00231
00232 if (pid > -1)
00233 kill(pid, SIGKILL);
00234 if (!res && owriteformat)
00235 ast_set_write_format(chan, owriteformat);
00236
00237 LOCAL_USER_REMOVE(u);
00238
00239 return res;
00240 }
00241
00242 int unload_module(void)
00243 {
00244 int res;
00245
00246 res = ast_unregister_application(app);
00247
00248 STANDARD_HANGUP_LOCALUSERS;
00249
00250 return res;
00251 }
00252
00253 int load_module(void)
00254 {
00255 return ast_register_application(app, mp3_exec, synopsis, descrip);
00256 }
00257
00258 char *description(void)
00259 {
00260 return tdesc;
00261 }
00262
00263 int usecount(void)
00264 {
00265 int res;
00266 STANDARD_USECOUNT(res);
00267 return res;
00268 }
00269
00270 char *key()
00271 {
00272 return ASTERISK_GPL_KEY;
00273 }