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: 24019 $")
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 res = fork();
00071 if (res < 0)
00072 ast_log(LOG_WARNING, "Fork failed\n");
00073 if (res)
00074 return res;
00075 if (option_highpriority)
00076 ast_set_priority(0);
00077 dup2(fd, STDOUT_FILENO);
00078 for (x=0;x<256;x++) {
00079 if (x != STDOUT_FILENO)
00080 close(x);
00081 }
00082
00083 if (!strncasecmp(filename, "http://", 7)) {
00084
00085 execl(LOCAL_MPG_123, "mpg123", "-q", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
00086
00087 execl(MPG_123, "mpg123", "-q", "-s", "-b", "1024","-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
00088
00089 execlp("mpg123", "mpg123", "-q", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
00090 }
00091 else {
00092
00093 execl(MPG_123, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
00094
00095 execl(LOCAL_MPG_123, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
00096
00097 execlp("mpg123", "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL);
00098 }
00099 ast_log(LOG_WARNING, "Execute of mpg123 failed\n");
00100 return -1;
00101 }
00102
00103 static int timed_read(int fd, void *data, int datalen, int timeout)
00104 {
00105 int res;
00106 struct pollfd fds[1];
00107 fds[0].fd = fd;
00108 fds[0].events = POLLIN;
00109 res = poll(fds, 1, timeout);
00110 if (res < 1) {
00111 ast_log(LOG_NOTICE, "Poll timed out/errored out with %d\n", res);
00112 return -1;
00113 }
00114 return read(fd, data, datalen);
00115
00116 }
00117
00118 static int mp3_exec(struct ast_channel *chan, void *data)
00119 {
00120 int res=0;
00121 struct localuser *u;
00122 int fds[2];
00123 int ms = -1;
00124 int pid = -1;
00125 int owriteformat;
00126 int timeout = 2000;
00127 struct timeval next;
00128 struct ast_frame *f;
00129 struct myframe {
00130 struct ast_frame f;
00131 char offset[AST_FRIENDLY_OFFSET];
00132 short frdata[160];
00133 } myf;
00134
00135 if (ast_strlen_zero(data)) {
00136 ast_log(LOG_WARNING, "MP3 Playback requires an argument (filename)\n");
00137 return -1;
00138 }
00139
00140 LOCAL_USER_ADD(u);
00141
00142 if (pipe(fds)) {
00143 ast_log(LOG_WARNING, "Unable to create pipe\n");
00144 LOCAL_USER_REMOVE(u);
00145 return -1;
00146 }
00147
00148 ast_stopstream(chan);
00149
00150 owriteformat = chan->writeformat;
00151 res = ast_set_write_format(chan, AST_FORMAT_SLINEAR);
00152 if (res < 0) {
00153 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
00154 LOCAL_USER_REMOVE(u);
00155 return -1;
00156 }
00157
00158 res = mp3play((char *)data, fds[1]);
00159 if (!strncasecmp((char *)data, "http://", 7)) {
00160 timeout = 10000;
00161 }
00162
00163 next = ast_tvnow();
00164 next.tv_sec += 1;
00165 if (res >= 0) {
00166 pid = res;
00167
00168
00169 for (;;) {
00170 ms = ast_tvdiff_ms(next, ast_tvnow());
00171 if (ms <= 0) {
00172 res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata), timeout);
00173 if (res > 0) {
00174 myf.f.frametype = AST_FRAME_VOICE;
00175 myf.f.subclass = AST_FORMAT_SLINEAR;
00176 myf.f.datalen = res;
00177 myf.f.samples = res / 2;
00178 myf.f.mallocd = 0;
00179 myf.f.offset = AST_FRIENDLY_OFFSET;
00180 myf.f.src = __PRETTY_FUNCTION__;
00181 myf.f.delivery.tv_sec = 0;
00182 myf.f.delivery.tv_usec = 0;
00183 myf.f.data = myf.frdata;
00184 if (ast_write(chan, &myf.f) < 0) {
00185 res = -1;
00186 break;
00187 }
00188 } else {
00189 ast_log(LOG_DEBUG, "No more mp3\n");
00190 res = 0;
00191 break;
00192 }
00193 next = ast_tvadd(next, ast_samp2tv(myf.f.samples, 8000));
00194 } else {
00195 ms = ast_waitfor(chan, ms);
00196 if (ms < 0) {
00197 ast_log(LOG_DEBUG, "Hangup detected\n");
00198 res = -1;
00199 break;
00200 }
00201 if (ms) {
00202 f = ast_read(chan);
00203 if (!f) {
00204 ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
00205 res = -1;
00206 break;
00207 }
00208 if (f->frametype == AST_FRAME_DTMF) {
00209 ast_log(LOG_DEBUG, "User pressed a key\n");
00210 ast_frfree(f);
00211 res = 0;
00212 break;
00213 }
00214 ast_frfree(f);
00215 }
00216 }
00217 }
00218 }
00219 close(fds[0]);
00220 close(fds[1]);
00221
00222 if (pid > -1)
00223 kill(pid, SIGKILL);
00224 if (!res && owriteformat)
00225 ast_set_write_format(chan, owriteformat);
00226
00227 LOCAL_USER_REMOVE(u);
00228
00229 return res;
00230 }
00231
00232 int unload_module(void)
00233 {
00234 int res;
00235
00236 res = ast_unregister_application(app);
00237
00238 STANDARD_HANGUP_LOCALUSERS;
00239
00240 return res;
00241 }
00242
00243 int load_module(void)
00244 {
00245 return ast_register_application(app, mp3_exec, synopsis, descrip);
00246 }
00247
00248 char *description(void)
00249 {
00250 return tdesc;
00251 }
00252
00253 int usecount(void)
00254 {
00255 int res;
00256 STANDARD_USECOUNT(res);
00257 return res;
00258 }
00259
00260 char *key()
00261 {
00262 return ASTERISK_GPL_KEY;
00263 }