Hi, The original app_mp3.c allows the user to exit the application by pressing a key (aka sending DTMF tone). For my application I needed the mp3 functionality but without allowing the user to interrupt.
Therfore I changed my source code (from 1.0.7) to allow the following in extensions.conf: MP3Player(mymp3file.mp3|N) Where the option N means : No exit when user pressed key, it is backwards compatible with the previous function because when '|N' is left out, it works the same as previous. Now my problem: I thought, okay I want to share this code with the Asterisk community but to do so , it would cost me a lot of time, I need first to download the lastest HEAD branch , then try to fix my code to work for that, then send a disclaimer by fax to digium, then I have to find out how and to whom I have to send the updated code etc. etc. Unfortunately I don't have enough time for all that. So I will try a shortcut by posting my code here, I hope that this will have the same status as giving it away to anybody including Digium. Let me know when it is adopted in the source tree, then I will update the Wiki page for use with new option. Here is the diff for the code against the 1.0.7 version of app_mp3.c -sh-2.05b$ diff app_mp3.c original1.0.7/app_mp3.c 17d16 < #include <asterisk/options.h> 41c40 < " MP3Player(location[|options]) Executes mpg123 to play the given location\n" --- > " MP3Player(location) Executes mpg123 to play the given location\n" 43,45c42 < "hangup or 0 otherwise. User can exit by pressing any key\n." < "The option string may contain zero or more of the following characters:\n" < " 'N' -- do NOT exit when a user pressed a key\n"; --- > "hangup or 0 otherwise. User can exit by pressing any key\n."; 110,112d106 < int exitOnKeyPress = 1; /* 0=Dot NOT exit on keypress, 1=Exit on keypress*/ < char *argumentLine,*filename,*options; < 124,139d117 < if (!(argumentLine = ast_strdupa(data))) { < ast_log(LOG_WARNING, "Unable to dupe data :(\n"); < return -1; < } < < filename=argumentLine; < options = strchr(argumentLine, '|'); < if ( options ) { < *options = '\0'; /* filename string is now terminated on pos of | */ < options++; /* options now points to start of options string */ < if ( strchr( options, 'N' ) != NULL ) { < /* N found, so no exit on KeyPress */ < exitOnKeyPress = 0; < } < } < 155,156c133,134 < res = mp3play( filename, fds[1]); < if (!strncasecmp( filename, "http://", 7)) { --- > res = mp3play((char *)data, fds[1]); > if (!strncasecmp((char *)data, "http://", 7)) { 226,235c204,208 < if (f->frametype == AST_FRAME_DTMF ) { < if ( exitOnKeyPress ) { < ast_verbose(VERBOSE_PREFIX_3 "app_mp3: Exit on keypress\n"); < ast_frfree(f); < res = 0; < break; < } < else { < ast_verbose(VERBOSE_PREFIX_3 "app_mp3: No exit on keypress\n"); < } --- > if (f->frametype == AST_FRAME_DTMF) { > ast_log(LOG_DEBUG, "User > pressed a key\n"); > ast_frfree(f); > res = 0; > break; If this doesn't work, here is the full app_mp3.c : ========================================== /* * Asterisk -- A telephony toolkit for Linux. * * Silly application to play an MP3 file -- uses mpg123 * * Copyright (C) 1999-2004, Digium, Inc. * * Mark Spencer <[EMAIL PROTECTED]> * * This program is free software, distributed under the terms of * the GNU General Public License */ #include <asterisk/lock.h> #include <asterisk/file.h> #include <asterisk/logger.h> #include <asterisk/options.h> #include <asterisk/channel.h> #include <asterisk/frame.h> #include <asterisk/pbx.h> #include <asterisk/module.h> #include <asterisk/translate.h> #include <string.h> #include <stdio.h> #include <signal.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/time.h> #define LOCAL_MPG_123 "/usr/local/bin/mpg123" #define MPG_123 "/usr/bin/mpg123" static char *tdesc = "Silly MP3 Application"; static char *app = "MP3Player"; static char *synopsis = "Play an MP3 file or stream"; static char *descrip = " MP3Player(location[|options]) Executes mpg123 to play the given location\n" "which typically would be a filename or a URL. Returns -1 on\n" "hangup or 0 otherwise. User can exit by pressing any key\n." "The option string may contain zero or more of the following characters:\n" " 'N' -- do NOT exit when a user pressed a key\n"; STANDARD_LOCAL_USER; LOCAL_USER_DECL; static int mp3play(char *filename, int fd) { int res; int x; res = fork(); if (res < 0) ast_log(LOG_WARNING, "Fork failed\n"); if (res) return res; dup2(fd, STDOUT_FILENO); for (x=0;x<256;x++) { if (x != STDOUT_FILENO) close(x); } /* Execute mpg123, but buffer if it's a net connection */ if (!strncasecmp(filename, "http://", 7)) { /* Most commonly installed in /usr/local/bin */ execl(LOCAL_MPG_123, "mpg123", "-q", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL); /* But many places has it in /usr/bin */ execl(MPG_123, "mpg123", "-q", "-s", "-b", "1024","-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL); /* As a last-ditch effort, try to use PATH */ execlp("mpg123", "mpg123", "-q", "-s", "-b", "1024", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL); } else { /* Most commonly installed in /usr/local/bin */ execl(MPG_123, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL); /* But many places has it in /usr/bin */ execl(LOCAL_MPG_123, "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL); /* As a last-ditch effort, try to use PATH */ execlp("mpg123", "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL); } ast_log(LOG_WARNING, "Execute of mpg123 failed\n"); return -1; } static int timed_read(int fd, void *data, int datalen, int timeout) { int res; struct pollfd fds[1]; fds[0].fd = fd; fds[0].events = POLLIN; res = poll(fds, 1, timeout); if (res < 1) { ast_log(LOG_NOTICE, "Poll timed out/errored out with %d\n", res); return -1; } return read(fd, data, datalen); } static int mp3_exec(struct ast_channel *chan, void *data) { int res=0; struct localuser *u; int fds[2]; int ms = -1; int pid = -1; int owriteformat; int timeout = 2000; int exitOnKeyPress = 1; /* 0=Dot NOT exit on keypress, 1=Exit on keypress*/ char *argumentLine,*filename,*options; struct timeval now, next; struct ast_frame *f; struct myframe { struct ast_frame f; char offset[AST_FRIENDLY_OFFSET]; short frdata[160]; } myf; if (!data) { ast_log(LOG_WARNING, "MP3 Playback requires an argument (filename)\n"); return -1; } if (!(argumentLine = ast_strdupa(data))) { ast_log(LOG_WARNING, "Unable to dupe data :(\n"); return -1; } filename=argumentLine; options = strchr(argumentLine, '|'); if ( options ) { *options = '\0'; /* filename string is now terminated on pos of | */ options++; /* options now points to start of options string */ if ( strchr( options, 'N' ) != NULL ) { /* N found, so no exit on KeyPress */ exitOnKeyPress = 0; } } if (pipe(fds)) { ast_log(LOG_WARNING, "Unable to create pipe\n"); return -1; } LOCAL_USER_ADD(u); ast_stopstream(chan); owriteformat = chan->writeformat; res = ast_set_write_format(chan, AST_FORMAT_SLINEAR); if (res < 0) { ast_log(LOG_WARNING, "Unable to set write format to signed linear\n"); return -1; } gettimeofday(&now, NULL); res = mp3play( filename, fds[1]); if (!strncasecmp( filename, "http://", 7)) { timeout = 10000; } /* Wait 1000 ms first */ next = now; next.tv_sec += 1; if (res >= 0) { pid = res; /* Order is important -- there's almost always going to be mp3... we want to prioritize the user */ for (;;) { gettimeofday(&now, NULL); ms = (next.tv_sec - now.tv_sec) * 1000; ms += (next.tv_usec - now.tv_usec) / 1000; #if 0 printf("ms: %d\n", ms); #endif if (ms <= 0) { #if 0 { static struct timeval last; struct timeval tv; gettimeofday(&tv, NULL); printf("Since last: %ld\n", (tv.tv_sec - last.tv_sec) * 1000 + (tv.tv_usec - last.tv_usec) / 1000); last = tv; } #endif res = timed_read(fds[0], myf.frdata, sizeof(myf.frdata), timeout); if (res > 0) { myf.f.frametype = AST_FRAME_VOICE; myf.f.subclass = AST_FORMAT_SLINEAR; myf.f.datalen = res; myf.f.samples = res / 2; myf.f.mallocd = 0; myf.f.offset = AST_FRIENDLY_OFFSET; myf.f.src = __PRETTY_FUNCTION__; myf.f.delivery.tv_sec = 0; myf.f.delivery.tv_usec = 0; myf.f.data = myf.frdata; if (ast_write(chan, &myf.f) < 0) { res = -1; break; } } else { ast_log(LOG_DEBUG, "No more mp3\n"); res = 0; break; } next.tv_usec += res / 2 * 125; if (next.tv_usec >= 1000000) { next.tv_usec -= 1000000; next.tv_sec++; } #if 0 printf("Next: %d\n", ms); #endif } else { ms = ast_waitfor(chan, ms); if (ms < 0) { ast_log(LOG_DEBUG, "Hangup detected\n"); res = -1; break; } if (ms) { f = ast_read(chan); if (!f) { ast_log(LOG_DEBUG, "Null frame == hangup() detected\n"); res = -1; break; } if (f->frametype == AST_FRAME_DTMF ) { if ( exitOnKeyPress ) { ast_verbose(VERBOSE_PREFIX_3 "app_mp3: Exit on keypress\n"); ast_frfree(f); res = 0; break; } else { ast_verbose(VERBOSE_PREFIX_3 "app_mp3: No exit on keypress\n"); } } ast_frfree(f); } } } } close(fds[0]); close(fds[1]); LOCAL_USER_REMOVE(u); if (pid > -1) kill(pid, SIGKILL); if (!res && owriteformat) ast_set_write_format(chan, owriteformat); return res; } int unload_module(void) { STANDARD_HANGUP_LOCALUSERS; return ast_unregister_application(app); } int load_module(void) { return ast_register_application(app, mp3_exec, synopsis, descrip); } char *description(void) { return tdesc; } int usecount(void) { int res; STANDARD_USECOUNT(res); return res; } char *key() { return ASTERISK_GPL_KEY; } ================================== Best regards, Michel Koenen _______________________________________________ Asterisk-Dev mailing list Asterisk-Dev@lists.digium.com http://lists.digium.com/mailman/listinfo/asterisk-dev To UNSUBSCRIBE or update options visit: http://lists.digium.com/mailman/listinfo/asterisk-dev