[Asterisk-Dev] working mpg321 patch

Steven Pritchard steve at silug.org
Mon May 12 18:36:30 MST 2003


Below is a patch that Works For Me(TM).  Compile with -DMPG321_HACK to
turn it on.  Make sure you have /usr/bin/sox.

I'll leave it to the experts to decide how best to enable/disable
this.  (Presumably mpg123 will use less CPU than mpg321 + sox.)

Steve
-- 
Steven Pritchard - K&S Pritchard Enterprises, Inc.
Email: steve at kspei.com             http://www.kspei.com/
Phone: (618)398-7360               Mobile: (618)567-7320

Fight recession!  Spend money today!

Index: res/res_musiconhold.c
===================================================================
RCS file: /usr/cvsroot/asterisk/res/res_musiconhold.c,v
retrieving revision 1.1.1.5
diff -u -r1.1.1.5 res_musiconhold.c
--- res/res_musiconhold.c	18 Mar 2003 06:00:18 -0000	1.1.1.5
+++ res/res_musiconhold.c	13 May 2003 01:47:30 -0000
@@ -93,7 +93,11 @@
 
 static pthread_mutex_t moh_lock = AST_MUTEX_INITIALIZER;
 
+#ifdef MPG321_HACK
+#define MPG_321 "/usr/bin/mpg321"
+#else
 #define MPG_123 "/usr/bin/mpg123"
+#endif /* MPG321_HACK */
 #define MAX_MP3S 256
 
 static void child_handler(int sig)
@@ -120,6 +124,15 @@
 		ast_log(LOG_WARNING, "%s is not a valid directory\n", class->dir);
 		return -1;
  	}
+#ifdef MPG321_HACK
+	argv[0] = MPG_321;
+	argv[1] = "-q";
+	argv[2] = "-w";
+	argv[3] = "-";
+	argv[4] = "-b";
+	argv[5] = "2048";
+	argc = 6;
+#else
 	argv[0] = MPG_123;
 	argv[1] = "-q";
 	argv[2] = "-s";
@@ -133,6 +146,7 @@
 		argv[argc++] = "-f";
 		argv[argc++] = "8192";
 	}
+#endif /* MPG321_HACK */
 
 	/* Look for extra arguments and add them to the list */
 	strncpy(xargs, class->miscargs, sizeof(xargs) - 1);
@@ -189,7 +203,18 @@
 			close(x);
 		/* Child */
 		chdir(class->dir);
+#ifdef MPG321_HACK
+		/* Shift our arguments down by 3. */
+		for (x=argc;x>=0;x--) {
+			argv[x+3]=argv[x];
+		}
+		argv[0]="/bin/sh";
+		argv[1]="-c";
+		argv[2]="$0 \"$@\" | /usr/bin/sox -t wav - -r 8000 -c 1 -s -w -t raw -";
+		execv(argv[0], argv);
+#else
 		execv(MPG_123, argv);
+#endif /* MPG321_HACK */
 		ast_log(LOG_WARNING, "Exec failed: %s\n", strerror(errno));
 		exit(1);
 	} else {
Index: apps/app_mp3.c
===================================================================
RCS file: /usr/cvsroot/asterisk/apps/app_mp3.c,v
retrieving revision 1.4
diff -u -r1.4 app_mp3.c
--- apps/app_mp3.c	27 Apr 2003 18:13:11 -0000	1.4
+++ apps/app_mp3.c	13 May 2003 01:47:30 -0000
@@ -28,7 +28,11 @@
 #include <pthread.h>
 #include <sys/time.h>
 
+#ifdef MPG321_HACK
+#define MPG_321 "/usr/bin/mpg321"
+#else
 #define MPG_123 "/usr/bin/mpg123"
+#endif /* MPG321_HACK */
 
 static char *tdesc = "Silly MP3 Application";
 
@@ -61,9 +65,23 @@
 	}
 	/* Execute mpg123, but buffer if it's a net connection */
 	if (strncmp(filename, "http://", 7)) 
+#ifdef MPG321_HACK
+	    execl("/bin/sh", "/bin/sh", "-c",
+                  MPG_321 " -q -b 1024 -w - \"$0\" "
+                  "| /usr/bin/sox -t wav - -r 8000 -c 1 -s -w -t raw -",
+		  filename, NULL);
+#else
 	    execl(MPG_123, MPG_123, "-q", "-s", "-b", "1024", "--mono", "-r", "8000", filename, NULL);
+#endif /* MPG321_HACK */
 	else
+#ifdef MPG321_HACK
+	    execl("/bin/sh", "/bin/sh", "-c",
+                  MPG_321 " -q -w - \"$0\" "
+                  "| /usr/bin/sox -t wav - -r 8000 -c 1 -s -w -t raw -",
+		  filename, NULL);
+#else
 	    execl(MPG_123, MPG_123, "-q", "-s", "--mono", "-r", "8000", filename, NULL);
+#endif /* MPG321_HACK */
 	ast_log(LOG_WARNING, "Execute of mpg123 failed\n");
 	return -1;
 }



More information about the asterisk-dev mailing list