Index: apps/app_meetme.c
===================================================================
RCS file: /usr/cvsroot/asterisk/apps/app_meetme.c,v
retrieving revision 1.80
diff -u -r1.80 app_meetme.c
--- apps/app_meetme.c	16 Feb 2005 03:50:41 -0000	1.80
+++ apps/app_meetme.c	16 Feb 2005 10:20:14 -0000
@@ -128,6 +128,11 @@
 	char *recordingfilename;	/* Filename to record the Conference into */
 	char *recordingformat;		/* Format to record the Conference in */
 	char pin[AST_MAX_EXTENSION];			/* If protected by a PIN */
+	pthread_t playthread;		/* thread for announcements */
+	ast_mutex_t playlock;		/* announcement mutex */
+	pthread_cond_t playcond;	/* announcement conditional */
+	struct ast_conf_play *playhead;	/* head of announcement queue */
+	struct ast_conf_play *playtail;	/* tail of announcement queue */
 	struct ast_conference *next;
 } *confs;
 
@@ -151,6 +156,12 @@
 
 AST_MUTEX_DEFINE_STATIC(conflock);
 
+struct ast_conf_play {
+	int sound;			/* ENTER or LEAVE sound, if no file */
+	char *file;			/* name of announcement file, if any */
+	struct ast_conf_play *next;	/* next announcement in queue */
+};
+
 static int admin_exec(struct ast_channel *chan, void *data);
 
 static void *recordthread(void *args);
@@ -158,8 +169,10 @@
 #include "enter.h"
 #include "leave.h"
 
-#define ENTER	0
-#define LEAVE	1
+#define ENTER		0
+#define LEAVE		1
+#define FILEKEEP	2
+#define FILEDELETE	3
 
 #define MEETME_RECORD_OFF	0
 #define MEETME_RECORD_ACTIVE	1
@@ -216,32 +229,98 @@
 	return 0;
 }
 
-static void conf_play(struct ast_channel *chan, struct ast_conference *conf, int sound)
+static void *playthread(void *this)
 {
-	unsigned char *data;
-	int len;
-	int res=-1;
-	if (!chan->_softhangup)
-		res = ast_autoservice_start(chan);
-	ast_mutex_lock(&conflock);
-	switch(sound) {
-	case ENTER:
-		data = enter;
-		len = sizeof(enter);
-		break;
-	case LEAVE:
-		data = leave;
-		len = sizeof(leave);
-		break;
-	default:
-		data = NULL;
-		len = 0;
+	struct ast_conference *conf = this;
+	struct ast_channel *chan = conf->chan;
+	struct ast_conf_play *req;
+
+	ast_mutex_lock(&conf->playlock);
+	while ((req = conf->playhead)) {
+		conf->playhead = req->next;
+		ast_mutex_unlock(&conf->playlock);
+
+		if (req->file) {
+			if (!chan)
+				ast_log(LOG_ERROR, "Can't stream - no channel on MeetMe conference %s\n", conf->confno);
+			else if (!ast_streamfile(chan, req->file, chan->language))
+				ast_waitstream(conf->chan, "");
+			else
+				ast_log(LOG_WARNING, "ast_streamfile of '%s' failed on %s\n", req->file, chan->name);
+
+			if (req->sound == FILEDELETE)
+				ast_filedelete(req->file, NULL);
+
+			free(req->file);
+		} else if (req->sound == ENTER)
+			careful_write(conf->fd, enter, sizeof(enter));
+		else if (req->sound == LEAVE)
+			careful_write(conf->fd, leave, sizeof(leave));
+
+		free(req);
+		ast_mutex_lock(&conf->playlock);
+	}
+	conf->playthread = 0;
+	pthread_cond_broadcast(&conf->playcond);	/* tell anyone waiting that we've finished */
+	ast_mutex_unlock(&conf->playlock);
+
+	pthread_exit(NULL);
+}
+
+static void conf_play(struct ast_conference *conf, int sound, char *file)
+{
+	pthread_attr_t attr;
+	struct ast_conf_play *req;
+
+	req = malloc(sizeof(struct ast_conf_play));
+	if (!req) {
+		ast_log(LOG_ERROR, "Can't allocate memory\n");
+		return;
+	}
+
+	req->sound = sound;
+	req->file = file ? strdup(file) : NULL;
+	req->next = NULL;
+
+	ast_mutex_lock(&conf->playlock);
+	if (conf->playhead)
+		conf->playtail = conf->playtail->next = req;
+	else {
+		conf->playtail = conf->playhead = req;
+
+		pthread_attr_init(&attr);
+		pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+
+		ast_pthread_create(&conf->playthread, &attr, playthread, conf);
 	}
-	if (data) 
-		careful_write(conf->fd, data, len);
-	ast_mutex_unlock(&conflock);
-	if (!res) 
-		ast_autoservice_stop(chan);
+	ast_mutex_unlock(&conf->playlock);
+}
+
+static void conf_play_abort(struct ast_conference *conf)
+{
+	struct ast_conf_play *req;
+
+	ast_mutex_lock(&conf->playlock);
+	/* discard any pending announcements */
+	while ((req = conf->playhead)) {
+		conf->playhead = req->next;
+		if (req->file) {
+			if (req->sound == FILEDELETE)
+				ast_filedelete(req->file, NULL);
+			free(req->file);
+		}
+		free(req);
+	}
+
+	/* stop the current announcement */
+	if (conf->chan)
+		ast_stopstream(conf->chan);
+
+	/* wait for the thread to finish */
+	while (conf->playthread)
+		pthread_cond_wait(&conf->playcond, &conf->playlock);
+
+	ast_mutex_unlock(&conf->playlock);
 }
 
 static struct ast_conference *build_conf(char *confno, char *pin, int make, int dynamic)
@@ -276,6 +355,11 @@
 					goto cnfout;
 				}
 			}
+
+			ast_mutex_init(&cnf->playlock);
+			pthread_cond_init(&cnf->playcond, NULL);
+			cnf->playhead = cnf->playtail = NULL;
+
 			memset(&ztc, 0, sizeof(ztc));
 			/* Setup a new zap conference */
 			ztc.chan = 0;
@@ -762,10 +846,8 @@
 	ast_mutex_lock(&conflock);
 	if (!(confflags & CONFFLAG_QUIET) && (confflags & CONFFLAG_INTROUSER) && conf->users > 1) {
 		if (conf->chan && ast_fileexists(user->namerecloc, NULL, NULL)) {
-			if (!ast_streamfile(conf->chan, user->namerecloc, chan->language))
-				ast_waitstream(conf->chan, "");
-			if (!ast_streamfile(conf->chan, "conf-hasjoin", chan->language))
-				ast_waitstream(conf->chan, "");
+			conf_play(conf, FILEKEEP, user->namerecloc);
+			conf_play(conf, FILEKEEP, "conf-hasjoin");
 		}
 	}
 
@@ -794,7 +876,7 @@
 	if (!firstpass && !(confflags & CONFFLAG_MONITOR) && !(confflags & CONFFLAG_ADMIN)) {
 		firstpass = 1;
 		if (!(confflags & CONFFLAG_QUIET))
-			conf_play(chan, conf, ENTER);
+			conf_play(conf, ENTER, NULL);
 	}
 	ast_mutex_unlock(&conflock);
 	if (confflags & CONFFLAG_AGI) {
@@ -1110,17 +1192,16 @@
 
 	ast_mutex_lock(&conflock);
 	if (!(confflags & CONFFLAG_QUIET) && !(confflags & CONFFLAG_MONITOR) && !(confflags & CONFFLAG_ADMIN))
-		conf_play(chan, conf, LEAVE);
+		if (conf->users > 1)
+			conf_play(conf, LEAVE, NULL);
 
 	if (!(confflags & CONFFLAG_QUIET) && (confflags & CONFFLAG_INTROUSER)) {
 		if (ast_fileexists(user->namerecloc, NULL, NULL)) {
 			if ((conf->chan) && (conf->users > 1)) {
-				if (!ast_streamfile(conf->chan, user->namerecloc, chan->language))
-					ast_waitstream(conf->chan, "");
-				if (!ast_streamfile(conf->chan, "conf-hasleft", chan->language))
-					ast_waitstream(conf->chan, "");
-			}
-			ast_filedelete(user->namerecloc, NULL);
+				conf_play(conf, FILEDELETE, user->namerecloc);
+				conf_play(conf, FILEKEEP, "conf-hasleft");
+			} else
+				ast_filedelete(user->namerecloc, NULL);
 		}
 	}
 	ast_mutex_unlock(&conflock);
@@ -1168,6 +1249,9 @@
 					ast_mutex_unlock(&conflock);
 				}
 			}
+
+			conf_play_abort(conf);
+
 			if (conf->chan)
 				ast_hangup(conf->chan);
 			else
