[asterisk-commits] mmichelson: branch mmichelson/queue-random-announce r104129 - /team/mmichelso...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Feb 26 09:33:01 CST 2008


Author: mmichelson
Date: Tue Feb 26 09:33:01 2008
New Revision: 104129

URL: http://svn.digium.com/view/asterisk?view=rev&rev=104129
Log:
This is the state the patch was in on issue 6681 prior to starting this branch. The way this works
is to allow a directory to be specified and play random sound files from that directory. There are some
drawbacks to this right now:

1. There is some somewhat expensive disk operations that are performed every time a periodic announcement is
   made. This is because the directory must be stat'd and a file randomly chosen. Some of this can be safely
   moved to configuration load time instead.

2. It is assumed right now that every file in the directory specified is a sound file that can be played as a
   periodic announcement. It may serve us well to scan the directory's contents at configuration load time to
   see which files are playable sound files and keep the pathnames of those in an array.


Modified:
    team/mmichelson/queue-random-announce/apps/app_queue.c

Modified: team/mmichelson/queue-random-announce/apps/app_queue.c
URL: http://svn.digium.com/view/asterisk/team/mmichelson/queue-random-announce/apps/app_queue.c?view=diff&rev=104129&r1=104128&r2=104129
==============================================================================
--- team/mmichelson/queue-random-announce/apps/app_queue.c (original)
+++ team/mmichelson/queue-random-announce/apps/app_queue.c Tue Feb 26 09:33:01 2008
@@ -67,6 +67,7 @@
 #include <sys/time.h>
 #include <sys/signal.h>
 #include <netinet/in.h>
+#include <dirent.h>
 
 #include "asterisk/lock.h"
 #include "asterisk/file.h"
@@ -541,9 +542,12 @@
 	int x;
 
 	for (x = 0; x < sizeof(strategies) / sizeof(strategies[0]); x++) {
-		if (!strcasecmp(strategy, strategies[x].name))
+		if (!strcasecmp(strategy, strategies[x].name)) {
 			return strategies[x].strategy;
-	}
+		}
+	}
+
+	ast_log(LOG_DEBUG, "Returning -1\n");
 
 	return -1;
 }
@@ -2311,6 +2315,8 @@
 {
 	int res = 0;
 	time_t now;
+	struct stat statinfo;
+	int filefound = 0;
 
 	/* Get the current time */
 	time(&now);
@@ -2334,8 +2340,39 @@
 		qe->last_periodic_announce_sound = 0;
 	}
 	
-	/* play the announcement */
-	res = play_file(qe->chan, qe->parent->sound_periodicannounce[qe->last_periodic_announce_sound]->str);
+ 	/* Get file stats for directory based random periodic announcements */
+ 	if (stat(qe->parent->sound_periodicannounce[0]->str, &statinfo) != -1)
+ 		filefound = 1;
+ 	else
+ 		ast_log(LOG_ERROR, "Couldn't stat '%s': %s\n", qe->parent->sound_periodicannounce[0]->str, strerror(errno));
+ 
+ 	if (filefound == 1 && (statinfo.st_mode & S_IFMT) == S_IFDIR) {
+ 		int filenum;
+ 		struct dirent **namelist;
+ 		filenum = scandir(qe->parent->sound_periodicannounce[0]->str, &namelist, 0, alphasort);
+ 		if (filenum < 0) {
+ 			ast_log(LOG_ERROR, "scandir: Periodic Announce scandir on (%s) FAILED: %s\n", qe->parent->sound_periodicannounce[0]->str, strerror(errno));
+ 		} else {
+ 			char *filename, *dot;
+ 			size_t left = PATH_MAX;
+ 			int randnum;
+ 			struct ast_str *randname = ast_str_create(left);
+ 			randnum = (ast_random() % (filenum-2))+2;
+ 			ast_str_append(&randname, left, "%s", qe->parent->sound_periodicannounce[0]->str);
+ 			if(randname->str[strlen(randname->str)-1] != '/')
+ 				ast_str_append(&randname, left, "%c", '/');
+ 			filename = ast_strdupa(namelist[randnum]->d_name);
+ 			if((dot = strrchr(filename, '.')))
+ 				*dot = '\0';
+ 			ast_str_append(&randname, left, "%s", filename);
+ 			res = play_file(qe->chan, randname->str);
+ 			free(namelist);
+ 			free(randname);
+ 		}
+ 	} else {
+ 		/* play the announcement */
+ 		res = play_file(qe->chan, qe->parent->sound_periodicannounce[qe->last_periodic_announce_sound]->str);
+ 	}
 
 	if ((res > 0 && !valid_exit(qe, res)) || res < 0)
 		res = 0;
@@ -2353,7 +2390,7 @@
 
 	/* Update the current periodic announcement to the next announcement */
 	qe->last_periodic_announce_sound++;
-	
+
 	return res;
 }
 




More information about the asterisk-commits mailing list