[asterisk-commits] file: branch 1.4 r98972 - in /branches/1.4: apps/ include/asterisk/ main/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Jan 16 14:33:47 CST 2008


Author: file
Date: Wed Jan 16 14:33:47 2008
New Revision: 98972

URL: http://svn.digium.com/view/asterisk?view=rev&rev=98972
Log:
Replace current spy architecture with backport of audiohooks. This should take care of current known spy issues.

Added:
    branches/1.4/include/asterisk/audiohook.h   (with props)
    branches/1.4/main/audiohook.c   (with props)
Removed:
    branches/1.4/include/asterisk/chanspy.h
Modified:
    branches/1.4/apps/app_chanspy.c
    branches/1.4/apps/app_meetme.c
    branches/1.4/apps/app_mixmonitor.c
    branches/1.4/include/asterisk/channel.h
    branches/1.4/main/Makefile
    branches/1.4/main/channel.c
    branches/1.4/main/rtp.c

Modified: branches/1.4/apps/app_chanspy.c
URL: http://svn.digium.com/view/asterisk/branches/1.4/apps/app_chanspy.c?view=diff&rev=98972&r1=98971&r2=98972
==============================================================================
--- branches/1.4/apps/app_chanspy.c (original)
+++ branches/1.4/apps/app_chanspy.c Wed Jan 16 14:33:47 2008
@@ -40,7 +40,7 @@
 #include "asterisk/file.h"
 #include "asterisk/logger.h"
 #include "asterisk/channel.h"
-#include "asterisk/chanspy.h"
+#include "asterisk/audiohook.h"
 #include "asterisk/features.h"
 #include "asterisk/options.h"
 #include "asterisk/app.h"
@@ -143,7 +143,8 @@
 
 struct chanspy_translation_helper {
 	/* spy data */
-	struct ast_channel_spy spy;
+	struct ast_audiohook spy_audiohook;
+	struct ast_audiohook whisper_audiohook;
 	int fd;
 	int volfactor;
 };
@@ -163,15 +164,17 @@
 {
 	struct chanspy_translation_helper *csth = data;
 	struct ast_frame *f;
-		
-	if (csth->spy.status != CHANSPY_RUNNING)
-		/* Channel is already gone more than likely */
+
+	ast_audiohook_lock(&csth->spy_audiohook);
+	if (csth->spy_audiohook.status != AST_AUDIOHOOK_STATUS_RUNNING) {
+		ast_audiohook_unlock(&csth->spy_audiohook);
 		return -1;
-
-	ast_mutex_lock(&csth->spy.lock);
-	f = ast_channel_spy_read_frame(&csth->spy, samples);
-	ast_mutex_unlock(&csth->spy.lock);
-		
+	}
+
+	f = ast_audiohook_read_frame(&csth->spy_audiohook, samples, AST_AUDIOHOOK_DIRECTION_BOTH, AST_FORMAT_SLINEAR);
+
+	ast_audiohook_unlock(&csth->spy_audiohook);
+
 	if (!f)
 		return 0;
 		
@@ -194,50 +197,19 @@
 	.generate = spy_generate, 
 };
 
-static int start_spying(struct ast_channel *chan, struct ast_channel *spychan, struct ast_channel_spy *spy) 
+static int start_spying(struct ast_channel *chan, struct ast_channel *spychan, struct ast_audiohook *audiohook) 
 {
 	int res;
 	struct ast_channel *peer;
 
 	ast_log(LOG_NOTICE, "Attaching %s to %s\n", spychan->name, chan->name);
 
-	ast_channel_lock(chan);
-	res = ast_channel_spy_add(chan, spy);
-	ast_channel_unlock(chan);
+	res = ast_audiohook_attach(chan, audiohook);
 
 	if (!res && ast_test_flag(chan, AST_FLAG_NBRIDGE) && (peer = ast_bridged_channel(chan)))
 		ast_softhangup(peer, AST_SOFTHANGUP_UNBRIDGE);	
 
 	return res;
-}
-
-/* Map 'volume' levels from -4 through +4 into
-   decibel (dB) settings for channel drivers
-*/
-static signed char volfactor_map[] = {
-	-24,
-	-18,
-	-12,
-	-6,
-	0,
-	6,
-	12,
-	18,
-	24,
-};
-
-/* attempt to set the desired gain adjustment via the channel driver;
-   if successful, clear it out of the csth structure so the
-   generator will not attempt to do the adjustment itself
-*/
-static void set_volume(struct ast_channel *chan, struct chanspy_translation_helper *csth)
-{
-	signed char volume_adjust = volfactor_map[csth->volfactor + 4];
-
-	if (!ast_channel_setoption(chan, AST_OPTION_TXGAIN, &volume_adjust, sizeof(volume_adjust), 0))
-		csth->volfactor = 0;
-	csth->spy.read_vol_adjustment = csth->volfactor;
-	csth->spy.write_vol_adjustment = csth->volfactor;
 }
 
 static int channel_spy(struct ast_channel *chan, struct ast_channel *spyee, int *volfactor, int fd,
@@ -258,49 +230,27 @@
 		ast_verbose(VERBOSE_PREFIX_2 "Spying on channel %s\n", name);
 
 	memset(&csth, 0, sizeof(csth));
-	ast_set_flag(&csth.spy, CHANSPY_FORMAT_AUDIO);
-	ast_set_flag(&csth.spy, CHANSPY_TRIGGER_NONE);
-	ast_set_flag(&csth.spy, CHANSPY_MIXAUDIO);
-	csth.spy.type = "ChanSpy";
-	csth.spy.status = CHANSPY_RUNNING;
-	csth.spy.read_queue.format = AST_FORMAT_SLINEAR;
-	csth.spy.write_queue.format = AST_FORMAT_SLINEAR;
-	ast_mutex_init(&csth.spy.lock);
+	
+	ast_audiohook_init(&csth.spy_audiohook, AST_AUDIOHOOK_TYPE_SPY, "ChanSpy");
+	
+	if (start_spying(spyee, chan, &csth.spy_audiohook)) {
+		ast_audiohook_destroy(&csth.spy_audiohook);
+		return 0;
+	}
+	
+	if (ast_test_flag(flags, OPTION_WHISPER)) {
+		ast_audiohook_init(&csth.whisper_audiohook, AST_AUDIOHOOK_TYPE_WHISPER, "ChanSpy");
+		start_spying(spyee, chan, &csth.whisper_audiohook);
+	}
+	
 	csth.volfactor = *volfactor;
-	set_volume(chan, &csth);
+	
 	if (csth.volfactor) {
-		ast_set_flag(&csth.spy, CHANSPY_READ_VOLADJUST);
-		csth.spy.read_vol_adjustment = csth.volfactor;
-		ast_set_flag(&csth.spy, CHANSPY_WRITE_VOLADJUST);
-		csth.spy.write_vol_adjustment = csth.volfactor;
-	}
+		csth.spy_audiohook.options.read_volume = csth.volfactor;
+		csth.spy_audiohook.options.write_volume = csth.volfactor;
+	}
+	
 	csth.fd = fd;
-	
-	if (start_spying(spyee, chan, &csth.spy)) {
-		ast_mutex_destroy(&csth.spy.lock);
-		return 0;
-	}
-
-	if (ast_test_flag(flags, OPTION_WHISPER)) {
-		struct ast_filestream *beepstream;
-		int old_write_format = 0;
-
-		ast_channel_whisper_start(csth.spy.chan);
-		old_write_format = chan->writeformat;
-		if ((beepstream = ast_openstream_full(chan, "beep", chan->language, 1))) {
-			struct ast_frame *f;
-
-			while ((f = ast_readframe(beepstream))) {
-				ast_channel_whisper_feed(csth.spy.chan, f);
-				ast_frfree(f);
-			}
-
-			ast_closestream(beepstream);
-			chan->stream = NULL;
-		}
-		if (old_write_format)
-			ast_set_write_format(chan, old_write_format);
-	}
 
 	if (ast_test_flag(flags, OPTION_PRIVATE))
 		silgen = ast_channel_start_silence_generator(chan);
@@ -321,17 +271,16 @@
 	   has arrived, since the spied-on channel could have gone away while
 	   we were waiting
 	*/
-	while ((res = ast_waitfor(chan, -1) > -1) &&
-	       csth.spy.status == CHANSPY_RUNNING &&
-	       csth.spy.chan) {
+	while ((res = ast_waitfor(chan, -1) > -1) && csth.spy_audiohook.status == AST_AUDIOHOOK_STATUS_RUNNING) {
 		if (!(f = ast_read(chan)) || ast_check_hangup(chan)) {
 			running = -1;
 			break;
 		}
 
-		if (ast_test_flag(flags, OPTION_WHISPER) &&
-		    (f->frametype == AST_FRAME_VOICE)) {
-			ast_channel_whisper_feed(csth.spy.chan, f);
+		if (ast_test_flag(flags, OPTION_WHISPER) && (f->frametype == AST_FRAME_VOICE)) {
+			ast_audiohook_lock(&csth.whisper_audiohook);
+			ast_audiohook_write_frame(&csth.whisper_audiohook, AST_AUDIOHOOK_DIRECTION_WRITE, f);
+			ast_audiohook_unlock(&csth.whisper_audiohook);
 			ast_frfree(f);
 			continue;
 		}
@@ -364,38 +313,29 @@
 			if (option_verbose > 2)
 				ast_verbose(VERBOSE_PREFIX_3 "Setting spy volume on %s to %d\n", chan->name, *volfactor);
 			csth.volfactor = *volfactor;
-			set_volume(chan, &csth);
-			if (csth.volfactor) {
-				ast_set_flag(&csth.spy, CHANSPY_READ_VOLADJUST);
-				csth.spy.read_vol_adjustment = csth.volfactor;
-				ast_set_flag(&csth.spy, CHANSPY_WRITE_VOLADJUST);
-				csth.spy.write_vol_adjustment = csth.volfactor;
-			} else {
-				ast_clear_flag(&csth.spy, CHANSPY_READ_VOLADJUST);
-				ast_clear_flag(&csth.spy, CHANSPY_WRITE_VOLADJUST);
-			}
+			csth.spy_audiohook.options.read_volume = csth.volfactor;
+			csth.spy_audiohook.options.write_volume = csth.volfactor;
 		} else if (res >= '0' && res <= '9') {
 			inp[x++] = res;
 		}
 	}
-
-	if (ast_test_flag(flags, OPTION_WHISPER) && csth.spy.chan)
-		ast_channel_whisper_stop(csth.spy.chan);
 
 	if (ast_test_flag(flags, OPTION_PRIVATE))
 		ast_channel_stop_silence_generator(chan, silgen);
 	else
 		ast_deactivate_generator(chan);
 
-	csth.spy.status = CHANSPY_DONE;
-
-	/* If a channel still exists on our spy structure then we need to remove ourselves */
-	if (csth.spy.chan) {
-		ast_channel_lock(csth.spy.chan);
-		ast_channel_spy_remove(csth.spy.chan, &csth.spy);
-		ast_channel_unlock(csth.spy.chan);
-	}
-	ast_channel_spy_free(&csth.spy);
+	if (ast_test_flag(flags, OPTION_WHISPER)) {
+		ast_audiohook_lock(&csth.whisper_audiohook);
+		ast_audiohook_detach(&csth.whisper_audiohook);
+		ast_audiohook_unlock(&csth.whisper_audiohook);
+		ast_audiohook_destroy(&csth.whisper_audiohook);
+	}
+	
+	ast_audiohook_lock(&csth.spy_audiohook);
+	ast_audiohook_detach(&csth.spy_audiohook);
+	ast_audiohook_unlock(&csth.spy_audiohook);
+	ast_audiohook_destroy(&csth.spy_audiohook);
 	
 	if (option_verbose >= 2)
 		ast_verbose(VERBOSE_PREFIX_2 "Done Spying on channel %s\n", name);

Modified: branches/1.4/apps/app_meetme.c
URL: http://svn.digium.com/view/asterisk/branches/1.4/apps/app_meetme.c?view=diff&rev=98972&r1=98971&r2=98972
==============================================================================
--- branches/1.4/apps/app_meetme.c (original)
+++ branches/1.4/apps/app_meetme.c Wed Jan 16 14:33:47 2008
@@ -1578,7 +1578,7 @@
 		goto outrun;
 	}
 
-	retryzap = (strcasecmp(chan->tech->type, "Zap") || (chan->spies || chan->monitor) ? 1 : 0);
+	retryzap = (strcasecmp(chan->tech->type, "Zap") || (chan->audiohooks || chan->monitor) ? 1 : 0);
 	user->zapchannel = !retryzap;
 
  zapretry:
@@ -1896,14 +1896,14 @@
 				break;
 
 			if (c) {
-				if (c->fds[0] != origfd || (user->zapchannel && (c->spies || c->monitor))) {
+				if (c->fds[0] != origfd || (user->zapchannel && (c->audiohooks || c->monitor))) {
 					if (using_pseudo) {
 						/* Kill old pseudo */
 						close(fd);
 						using_pseudo = 0;
 					}
 					ast_log(LOG_DEBUG, "Ooh, something swapped out under us, starting over\n");
-					retryzap = (strcasecmp(c->tech->type, "Zap") || (c->spies || c->monitor) ? 1 : 0);
+					retryzap = (strcasecmp(c->tech->type, "Zap") || (c->audiohooks || c->monitor) ? 1 : 0);
 					user->zapchannel = !retryzap;
 					goto zapretry;
 				}

Modified: branches/1.4/apps/app_mixmonitor.c
URL: http://svn.digium.com/view/asterisk/branches/1.4/apps/app_mixmonitor.c?view=diff&rev=98972&r1=98971&r2=98972
==============================================================================
--- branches/1.4/apps/app_mixmonitor.c (original)
+++ branches/1.4/apps/app_mixmonitor.c Wed Jan 16 14:33:47 2008
@@ -45,7 +45,7 @@
 #include "asterisk/file.h"
 #include "asterisk/logger.h"
 #include "asterisk/channel.h"
-#include "asterisk/chanspy.h"
+#include "asterisk/audiohook.h"
 #include "asterisk/pbx.h"
 #include "asterisk/module.h"
 #include "asterisk/lock.h"
@@ -93,11 +93,12 @@
 static const char *mixmonitor_spy_type = "MixMonitor";
 
 struct mixmonitor {
-	struct ast_channel_spy spy;
+	struct ast_audiohook audiohook;
 	char *filename;
 	char *post_process;
 	char *name;
 	unsigned int flags;
+	struct ast_channel *chan;
 };
 
 enum {
@@ -123,7 +124,7 @@
 	AST_APP_OPTION_ARG('W', MUXFLAG_VOLUME, OPT_ARG_VOLUME),
 });
 
-static int startmon(struct ast_channel *chan, struct ast_channel_spy *spy) 
+static int startmon(struct ast_channel *chan, struct ast_audiohook *audiohook) 
 {
 	struct ast_channel *peer;
 	int res;
@@ -131,9 +132,7 @@
 	if (!chan)
 		return -1;
 
-	ast_channel_lock(chan);
-	res = ast_channel_spy_add(chan, spy);
-	ast_channel_unlock(chan);
+	res = ast_audiohook_attach(chan, audiohook);
 
 	if (!res && ast_test_flag(chan, AST_FLAG_NBRIDGE) && (peer = ast_bridged_channel(chan)))
 		ast_softhangup(peer, AST_SOFTHANGUP_UNBRIDGE);	
@@ -146,7 +145,6 @@
 static void *mixmonitor_thread(void *obj) 
 {
 	struct mixmonitor *mixmonitor = obj;
-	struct ast_frame *f = NULL;
 	struct ast_filestream *fs = NULL;
 	unsigned int oflags;
 	char *ext;
@@ -155,58 +153,48 @@
 	if (option_verbose > 1)
 		ast_verbose(VERBOSE_PREFIX_2 "Begin MixMonitor Recording %s\n", mixmonitor->name);
 	
-	ast_mutex_lock(&mixmonitor->spy.lock);
-
-	while (mixmonitor->spy.chan) {
-		struct ast_frame *next;
-		int write;
-
-		ast_channel_spy_trigger_wait(&mixmonitor->spy);
-		
-		if (!mixmonitor->spy.chan || mixmonitor->spy.status != CHANSPY_RUNNING)
+	ast_audiohook_lock(&mixmonitor->audiohook);
+
+	while (mixmonitor->audiohook.status == AST_AUDIOHOOK_STATUS_RUNNING) {
+		struct ast_frame *fr = NULL;
+		
+		ast_audiohook_trigger_wait(&mixmonitor->audiohook);
+		
+		if (mixmonitor->audiohook.status != AST_AUDIOHOOK_STATUS_RUNNING)
 			break;
 		
-		while (1) {
-			if (!(f = ast_channel_spy_read_frame(&mixmonitor->spy, SAMPLES_PER_FRAME)))
-				break;
-
-			write = (!ast_test_flag(mixmonitor, MUXFLAG_BRIDGED) ||
-				 ast_bridged_channel(mixmonitor->spy.chan));
-
-			/* it is possible for ast_channel_spy_read_frame() to return a chain
-			   of frames if a queue flush was necessary, so process them
-			*/
-			for (; f; f = next) {
-				next = AST_LIST_NEXT(f, frame_list);
-				if (write && errflag == 0) {
-					if (!fs) {
-						/* Determine creation flags and filename plus extension for filestream */
-						oflags = O_CREAT | O_WRONLY;
-						oflags |= ast_test_flag(mixmonitor, MUXFLAG_APPEND) ? O_APPEND : O_TRUNC;
-
-						if ((ext = strrchr(mixmonitor->filename, '.')))
-							*(ext++) = '\0';
-						else
-							ext = "raw";
-
-						/* Move onto actually creating the filestream */
-						if (!(fs = ast_writefile(mixmonitor->filename, ext, NULL, oflags, 0, 0644))) {
-							ast_log(LOG_ERROR, "Cannot open %s.%s\n", mixmonitor->filename, ext);
-							errflag = 1;
-						}
-
-					}
-					if (fs)
-						ast_writestream(fs, f);
+		if (!(fr = ast_audiohook_read_frame(&mixmonitor->audiohook, SAMPLES_PER_FRAME, AST_AUDIOHOOK_DIRECTION_BOTH, AST_FORMAT_SLINEAR)))
+			continue;
+		
+		if (!ast_test_flag(mixmonitor, MUXFLAG_BRIDGED) || ast_bridged_channel(mixmonitor->chan)) {
+			/* Initialize the file if not already done so */
+			if (!fs && !errflag) {
+				oflags = O_CREAT | O_WRONLY;
+				oflags |= ast_test_flag(mixmonitor, MUXFLAG_APPEND) ? O_APPEND : O_TRUNC;
+				
+				if ((ext = strrchr(mixmonitor->filename, '.')))
+					*(ext++) = '\0';
+				else
+					ext = "raw";
+				
+				if (!(fs = ast_writefile(mixmonitor->filename, ext, NULL, oflags, 0, 0644))) {
+					ast_log(LOG_ERROR, "Cannot open %s.%s\n", mixmonitor->filename, ext);
+					errflag = 1;
 				}
-				ast_frame_free(f, 0);
 			}
+
+			/* Write out the frame */
+			if (fs)
+				ast_writestream(fs, fr);
 		}
-	}
-
-	ast_mutex_unlock(&mixmonitor->spy.lock);
-
-	ast_channel_spy_free(&mixmonitor->spy);
+
+		/* All done! free it. */
+		ast_frame_free(fr, 0);
+	}
+
+	ast_audiohook_detach(&mixmonitor->audiohook);
+	ast_audiohook_unlock(&mixmonitor->audiohook);
+	ast_audiohook_destroy(&mixmonitor->audiohook);
 	
 	if (option_verbose > 1)
 		ast_verbose(VERBOSE_PREFIX_2 "End MixMonitor Recording %s\n", mixmonitor->name);
@@ -271,27 +259,23 @@
 	strcpy(mixmonitor->filename, filename);
 
 	/* Setup the actual spy before creating our thread */
-	ast_set_flag(&mixmonitor->spy, CHANSPY_FORMAT_AUDIO);
-	ast_set_flag(&mixmonitor->spy, CHANSPY_MIXAUDIO);
-	mixmonitor->spy.type = mixmonitor_spy_type;
-	mixmonitor->spy.status = CHANSPY_RUNNING;
-	mixmonitor->spy.read_queue.format = AST_FORMAT_SLINEAR;
-	mixmonitor->spy.write_queue.format = AST_FORMAT_SLINEAR;
-	if (readvol) {
-		ast_set_flag(&mixmonitor->spy, CHANSPY_READ_VOLADJUST);
-		mixmonitor->spy.read_vol_adjustment = readvol;
-	}
-	if (writevol) {
-		ast_set_flag(&mixmonitor->spy, CHANSPY_WRITE_VOLADJUST);
-		mixmonitor->spy.write_vol_adjustment = writevol;
-	}
-	ast_mutex_init(&mixmonitor->spy.lock);
-
-	if (startmon(chan, &mixmonitor->spy)) {
+	if (ast_audiohook_init(&mixmonitor->audiohook, AST_AUDIOHOOK_TYPE_SPY, mixmonitor_spy_type)) {
+		free(mixmonitor);
+		return;
+	}
+	
+	ast_set_flag(&mixmonitor->audiohook, AST_AUDIOHOOK_TRIGGER_WRITE);
+	
+	if (readvol)
+		mixmonitor->audiohook.options.read_volume = readvol;
+	if (writevol)
+		mixmonitor->audiohook.options.write_volume = writevol;
+
+	if (startmon(chan, &mixmonitor->audiohook)) {
 		ast_log(LOG_WARNING, "Unable to add '%s' spy to channel '%s'\n",
-			mixmonitor->spy.type, chan->name);
+			mixmonitor_spy_type, chan->name);
 		/* Since we couldn't add ourselves - bail out! */
-		ast_mutex_destroy(&mixmonitor->spy.lock);
+		ast_audiohook_destroy(&mixmonitor->audiohook);
 		free(mixmonitor);
 		return;
 	}
@@ -391,9 +375,7 @@
 
 	u = ast_module_user_add(chan);
 
-	ast_channel_lock(chan);
-	ast_channel_spy_stop_by_type(chan, mixmonitor_spy_type);
-	ast_channel_unlock(chan);
+	ast_audiohook_detach_source(chan, mixmonitor_spy_type);
 
 	ast_module_user_remove(u);
 
@@ -415,7 +397,7 @@
 	if (!strcasecmp(argv[1], "start"))
 		mixmonitor_exec(chan, argv[3]);
 	else if (!strcasecmp(argv[1], "stop"))
-		ast_channel_spy_stop_by_type(chan, mixmonitor_spy_type);
+		ast_audiohook_detach_source(chan, mixmonitor_spy_type);
 
 	ast_channel_unlock(chan);
 

Added: branches/1.4/include/asterisk/audiohook.h
URL: http://svn.digium.com/view/asterisk/branches/1.4/include/asterisk/audiohook.h?view=auto&rev=98972
==============================================================================
--- branches/1.4/include/asterisk/audiohook.h (added)
+++ branches/1.4/include/asterisk/audiohook.h Wed Jan 16 14:33:47 2008
@@ -1,0 +1,358 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 1999 - 2007, Digium, Inc.
+ *
+ * Joshua Colp <jcolp at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ * \brief Audiohooks Architecture
+ */
+
+#ifndef _ASTERISK_AUDIOHOOK_H
+#define _ASTERISK_AUDIOHOOK_H
+
+#if defined(__cplusplus) || defined(c_plusplus)
+extern "C" {
+#endif
+
+#include "asterisk/slinfactory.h"
+
+enum ast_audiohook_type {
+	AST_AUDIOHOOK_TYPE_SPY = 0,    /*!< Audiohook wants to receive audio */
+	AST_AUDIOHOOK_TYPE_WHISPER,    /*!< Audiohook wants to provide audio to be mixed with existing audio */
+	AST_AUDIOHOOK_TYPE_MANIPULATE, /*!< Audiohook wants to manipulate the audio */
+};
+
+enum ast_audiohook_status {
+	AST_AUDIOHOOK_STATUS_NEW = 0,  /*!< Audiohook was just created, not in use yet */
+	AST_AUDIOHOOK_STATUS_RUNNING,  /*!< Audiohook is running on a channel */
+	AST_AUDIOHOOK_STATUS_SHUTDOWN, /*!< Audiohook is being shutdown */
+	AST_AUDIOHOOK_STATUS_DONE,     /*!< Audiohook has shutdown and is not running on a channel any longer */
+};
+
+enum ast_audiohook_direction {
+	AST_AUDIOHOOK_DIRECTION_READ = 0, /*!< Reading audio in */
+	AST_AUDIOHOOK_DIRECTION_WRITE,    /*!< Writing audio out */
+	AST_AUDIOHOOK_DIRECTION_BOTH,     /*!< Both reading audio in and writing audio out */
+};
+
+enum ast_audiohook_flags {
+	AST_AUDIOHOOK_TRIGGER_MODE = (3 << 0),  /*!< When audiohook should be triggered to do something */
+	AST_AUDIOHOOK_TRIGGER_READ = (1 << 0),  /*!< Audiohook wants to be triggered when reading audio in */
+	AST_AUDIOHOOK_TRIGGER_WRITE = (2 << 0), /*!< Audiohook wants to be triggered when writing audio out */
+	AST_AUDIOHOOK_WANTS_DTMF = (1 << 1),    /*!< Audiohook also wants to receive DTMF frames */
+};
+
+struct ast_audiohook;
+
+/*! \brief Callback function for manipulate audiohook type
+ * \param audiohook Audiohook structure
+ * \param chan Channel
+ * \param frame Frame of audio to manipulate
+ * \param direction Direction frame came from
+ * \return Returns 0 on success, -1 on failure
+ * \note An audiohook does not have any reference to a private data structure for manipulate types. It is up to the manipulate callback to store this data
+ *       via it's own method. An example would be datastores.
+ */
+typedef int (*ast_audiohook_manipulate_callback)(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction);
+
+struct ast_audiohook_options {
+	int read_volume;  /*!< Volume adjustment on frames read from the channel the hook is on */
+	int write_volume; /*!< Volume adjustment on frames written to the channel the hook is on */
+};
+
+struct ast_audiohook {
+	ast_mutex_t lock;                                      /*!< Lock that protects the audiohook structure */
+	ast_cond_t trigger;                                    /*!< Trigger condition (if enabled) */
+	enum ast_audiohook_type type;                          /*!< Type of audiohook */
+	enum ast_audiohook_status status;                      /*!< Status of the audiohook */
+	const char *source;                                    /*!< Who this audiohook ultimately belongs to */
+	unsigned int flags;                                    /*!< Flags on the audiohook */
+	struct ast_slinfactory read_factory;                   /*!< Factory where frames read from the channel, or read from the whisper source will go through */
+	struct ast_slinfactory write_factory;                  /*!< Factory where frames written to the channel will go through */
+	int format;                                            /*!< Format translation path is setup as */
+	struct ast_trans_pvt *trans_pvt;                       /*!< Translation path for reading frames */
+	ast_audiohook_manipulate_callback manipulate_callback; /*!< Manipulation callback */
+	struct ast_audiohook_options options;                  /*!< Applicable options */
+	AST_LIST_ENTRY(ast_audiohook) list;                    /*!< Linked list information */
+};
+
+struct ast_audiohook_list;
+
+/*! \brief Initialize an audiohook structure
+ * \param audiohook Audiohook structure
+ * \param type Type of audiohook to initialize this as
+ * \param source Who is initializing this audiohook
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source);
+
+/*! \brief Destroys an audiohook structure
+ * \param audiohook Audiohook structure
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_audiohook_destroy(struct ast_audiohook *audiohook);
+
+/*! \brief Writes a frame into the audiohook structure
+ * \param audiohook Audiohook structure
+ * \param direction Direction the audio frame came from
+ * \param frame Frame to write in
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_audiohook_write_frame(struct ast_audiohook *audiohook, enum ast_audiohook_direction direction, struct ast_frame *frame);
+
+/*! \brief Reads a frame in from the audiohook structure
+ * \param audiohook Audiohook structure
+ * \param samples Number of samples wanted
+ * \param direction Direction the audio frame came from
+ * \param format Format of frame remote side wants back
+ * \return Returns frame on success, NULL on failure
+ */
+struct ast_frame *ast_audiohook_read_frame(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction, int format);
+
+/*! \brief Attach audiohook to channel
+ * \param chan Channel
+ * \param audiohook Audiohook structure
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook);
+
+/*! \brief Detach audiohook from channel
+ * \param audiohook Audiohook structure
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_audiohook_detach(struct ast_audiohook *audiohook);
+
+/*! \brief Detach audiohooks from list and destroy said list
+ * \param audiohook_list List of audiohooks
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_audiohook_detach_list(struct ast_audiohook_list *audiohook_list);
+
+/*! \brief Detach specified source audiohook from channel
+ * \param chan Channel to detach from
+ * \param source Name of source to detach
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_audiohook_detach_source(struct ast_channel *chan, const char *source);
+
+/*! \brief Pass a frame off to be handled by the audiohook core
+ * \param chan Channel that the list is coming off of
+ * \param audiohook_list List of audiohooks
+ * \param direction Direction frame is coming in from
+ * \param frame The frame itself
+ * \return Return frame on success, NULL on failure
+ */
+struct ast_frame *ast_audiohook_write_list(struct ast_channel *chan, struct ast_audiohook_list *audiohook_list, enum ast_audiohook_direction direction, struct ast_frame *frame);
+
+/*! \brief Wait for audiohook trigger to be triggered
+ * \param audiohook Audiohook to wait on
+ */
+void ast_audiohook_trigger_wait(struct ast_audiohook *audiohook);
+
+/*! \brief Lock an audiohook
+ * \param ah Audiohook structure
+ */
+#define ast_audiohook_lock(ah) ast_mutex_lock(&(ah)->lock)
+
+/*! \brief Unlock an audiohook
+ * \param ah Audiohook structure
+ */
+#define ast_audiohook_unlock(ah) ast_mutex_unlock(&(ah)->lock)
+
+#if defined(__cplusplus) || defined(c_plusplus)
+}
+#endif
+
+#endif /* _ASTERISK_AUDIOHOOK_H */
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 1999 - 2007, Digium, Inc.
+ *
+ * Joshua Colp <jcolp at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ * \brief Audiohooks Architecture
+ */
+
+#ifndef _ASTERISK_AUDIOHOOK_H
+#define _ASTERISK_AUDIOHOOK_H
+
+#if defined(__cplusplus) || defined(c_plusplus)
+extern "C" {
+#endif
+
+#include "asterisk/slinfactory.h"
+
+enum ast_audiohook_type {
+	AST_AUDIOHOOK_TYPE_SPY = 0,    /*!< Audiohook wants to receive audio */
+	AST_AUDIOHOOK_TYPE_WHISPER,    /*!< Audiohook wants to provide audio to be mixed with existing audio */
+	AST_AUDIOHOOK_TYPE_MANIPULATE, /*!< Audiohook wants to manipulate the audio */
+};
+
+enum ast_audiohook_status {
+	AST_AUDIOHOOK_STATUS_NEW = 0,  /*!< Audiohook was just created, not in use yet */
+	AST_AUDIOHOOK_STATUS_RUNNING,  /*!< Audiohook is running on a channel */
+	AST_AUDIOHOOK_STATUS_SHUTDOWN, /*!< Audiohook is being shutdown */
+	AST_AUDIOHOOK_STATUS_DONE,     /*!< Audiohook has shutdown and is not running on a channel any longer */
+};
+
+enum ast_audiohook_direction {
+	AST_AUDIOHOOK_DIRECTION_READ = 0, /*!< Reading audio in */
+	AST_AUDIOHOOK_DIRECTION_WRITE,    /*!< Writing audio out */
+	AST_AUDIOHOOK_DIRECTION_BOTH,     /*!< Both reading audio in and writing audio out */
+};
+
+enum ast_audiohook_flags {
+	AST_AUDIOHOOK_TRIGGER_MODE = (3 << 0),  /*!< When audiohook should be triggered to do something */
+	AST_AUDIOHOOK_TRIGGER_READ = (1 << 0),  /*!< Audiohook wants to be triggered when reading audio in */
+	AST_AUDIOHOOK_TRIGGER_WRITE = (2 << 0), /*!< Audiohook wants to be triggered when writing audio out */
+	AST_AUDIOHOOK_WANTS_DTMF = (1 << 1),    /*!< Audiohook also wants to receive DTMF frames */
+};
+
+struct ast_audiohook;
+
+/*! \brief Callback function for manipulate audiohook type
+ * \param audiohook Audiohook structure
+ * \param chan Channel
+ * \param frame Frame of audio to manipulate
+ * \param direction Direction frame came from
+ * \return Returns 0 on success, -1 on failure
+ * \note An audiohook does not have any reference to a private data structure for manipulate types. It is up to the manipulate callback to store this data
+ *       via it's own method. An example would be datastores.
+ */
+typedef int (*ast_audiohook_manipulate_callback)(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction);
+
+struct ast_audiohook_options {
+	int read_volume;  /*!< Volume adjustment on frames read from the channel the hook is on */
+	int write_volume; /*!< Volume adjustment on frames written to the channel the hook is on */
+};
+
+struct ast_audiohook {
+	ast_mutex_t lock;                                      /*!< Lock that protects the audiohook structure */
+	ast_cond_t trigger;                                    /*!< Trigger condition (if enabled) */
+	enum ast_audiohook_type type;                          /*!< Type of audiohook */
+	enum ast_audiohook_status status;                      /*!< Status of the audiohook */
+	const char *source;                                    /*!< Who this audiohook ultimately belongs to */
+	unsigned int flags;                                    /*!< Flags on the audiohook */
+	struct ast_slinfactory read_factory;                   /*!< Factory where frames read from the channel, or read from the whisper source will go through */
+	struct ast_slinfactory write_factory;                  /*!< Factory where frames written to the channel will go through */
+	int format;                                            /*!< Format translation path is setup as */
+	struct ast_trans_pvt *trans_pvt;                       /*!< Translation path for reading frames */
+	ast_audiohook_manipulate_callback manipulate_callback; /*!< Manipulation callback */
+	struct ast_audiohook_options options;                  /*!< Applicable options */
+	AST_LIST_ENTRY(ast_audiohook) list;                    /*!< Linked list information */
+};
+
+struct ast_audiohook_list;
+
+/*! \brief Initialize an audiohook structure
+ * \param audiohook Audiohook structure
+ * \param type Type of audiohook to initialize this as
+ * \param source Who is initializing this audiohook
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source);
+
+/*! \brief Destroys an audiohook structure
+ * \param audiohook Audiohook structure
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_audiohook_destroy(struct ast_audiohook *audiohook);
+
+/*! \brief Writes a frame into the audiohook structure
+ * \param audiohook Audiohook structure
+ * \param direction Direction the audio frame came from
+ * \param frame Frame to write in
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_audiohook_write_frame(struct ast_audiohook *audiohook, enum ast_audiohook_direction direction, struct ast_frame *frame);
+
+/*! \brief Reads a frame in from the audiohook structure
+ * \param audiohook Audiohook structure
+ * \param samples Number of samples wanted
+ * \param direction Direction the audio frame came from
+ * \param format Format of frame remote side wants back
+ * \return Returns frame on success, NULL on failure
+ */
+struct ast_frame *ast_audiohook_read_frame(struct ast_audiohook *audiohook, size_t samples, enum ast_audiohook_direction direction, int format);
+
+/*! \brief Attach audiohook to channel
+ * \param chan Channel
+ * \param audiohook Audiohook structure
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook);
+
+/*! \brief Detach audiohook from channel
+ * \param audiohook Audiohook structure
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_audiohook_detach(struct ast_audiohook *audiohook);
+
+/*! \brief Detach audiohooks from list and destroy said list
+ * \param audiohook_list List of audiohooks
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_audiohook_detach_list(struct ast_audiohook_list *audiohook_list);
+
+/*! \brief Detach specified source audiohook from channel
+ * \param chan Channel to detach from
+ * \param source Name of source to detach
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_audiohook_detach_source(struct ast_channel *chan, const char *source);
+
+/*! \brief Pass a frame off to be handled by the audiohook core
+ * \param chan Channel that the list is coming off of
+ * \param audiohook_list List of audiohooks
+ * \param direction Direction frame is coming in from
+ * \param frame The frame itself
+ * \return Return frame on success, NULL on failure
+ */
+struct ast_frame *ast_audiohook_write_list(struct ast_channel *chan, struct ast_audiohook_list *audiohook_list, enum ast_audiohook_direction direction, struct ast_frame *frame);
+
+/*! \brief Wait for audiohook trigger to be triggered
+ * \param audiohook Audiohook to wait on
+ */
+void ast_audiohook_trigger_wait(struct ast_audiohook *audiohook);
+
+/*! \brief Lock an audiohook
+ * \param ah Audiohook structure
+ */
+#define ast_audiohook_lock(ah) ast_mutex_lock(&(ah)->lock)
+
+/*! \brief Unlock an audiohook
+ * \param ah Audiohook structure
+ */
+#define ast_audiohook_unlock(ah) ast_mutex_unlock(&(ah)->lock)
+
+#if defined(__cplusplus) || defined(c_plusplus)
+}
+#endif
+
+#endif /* _ASTERISK_AUDIOHOOK_H */

Propchange: branches/1.4/include/asterisk/audiohook.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: branches/1.4/include/asterisk/audiohook.h
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: branches/1.4/include/asterisk/audiohook.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: branches/1.4/include/asterisk/channel.h
URL: http://svn.digium.com/view/asterisk/branches/1.4/include/asterisk/channel.h?view=diff&rev=98972&r1=98971&r2=98972
==============================================================================
--- branches/1.4/include/asterisk/channel.h (original)
+++ branches/1.4/include/asterisk/channel.h Wed Jan 16 14:33:47 2008
@@ -276,9 +276,6 @@
 	int (* set_base_channel)(struct ast_channel *chan, struct ast_channel *base);
 };
 
-struct ast_channel_spy_list;
-struct ast_channel_whisper_buffer;
-
 #define	DEBUGCHAN_FLAG  0x80000000
 #define	FRAMECOUNT_INC(x)	( ((x) & DEBUGCHAN_FLAG) | (((x)+1) & ~DEBUGCHAN_FLAG) )
 
@@ -430,8 +427,8 @@
 	int rawreadformat;				/*!< Raw read format */
 	int rawwriteformat;				/*!< Raw write format */
 
-	struct ast_channel_spy_list *spies;		/*!< Chan Spy stuff */
-	struct ast_channel_whisper_buffer *whisper;	/*!< Whisper Paging buffer */
+	struct ast_audiohook_list *audiohooks;
+
 	AST_LIST_ENTRY(ast_channel) chan_list;		/*!< For easy linking */
 	
 	struct ast_jb jb;				/*!< The jitterbuffer state  */

Modified: branches/1.4/main/Makefile
URL: http://svn.digium.com/view/asterisk/branches/1.4/main/Makefile?view=diff&rev=98972&r1=98971&r2=98972
==============================================================================
--- branches/1.4/main/Makefile (original)
+++ branches/1.4/main/Makefile Wed Jan 16 14:33:47 2008
@@ -26,7 +26,8 @@
 	utils.o plc.o jitterbuf.o dnsmgr.o devicestate.o \
 	netsock.o slinfactory.o ast_expr2.o ast_expr2f.o \
 	cryptostub.o sha1.o http.o fixedjitterbuf.o abstract_jb.o \
-	strcompat.o threadstorage.o dial.o astobj2.o global_datastores.o
+	strcompat.o threadstorage.o dial.o astobj2.o global_datastores.o \
+	audiohook.o
 
 # we need to link in the objects statically, not as a library, because
 # otherwise modules will not have them available if none of the static

Added: branches/1.4/main/audiohook.c
URL: http://svn.digium.com/view/asterisk/branches/1.4/main/audiohook.c?view=auto&rev=98972
==============================================================================
--- branches/1.4/main/audiohook.c (added)
+++ branches/1.4/main/audiohook.c Wed Jan 16 14:33:47 2008
@@ -1,0 +1,626 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 1999 - 2007, Digium, Inc.
+ *
+ * Joshua Colp <jcolp at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Audiohooks Architecture
+ *
+ * \author Joshua Colp <jcolp at digium.com>
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <signal.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include "asterisk/logger.h"
+#include "asterisk/channel.h"
+#include "asterisk/options.h"
+#include "asterisk/utils.h"
+#include "asterisk/lock.h"
+#include "asterisk/linkedlists.h"
+#include "asterisk/audiohook.h"
+#include "asterisk/slinfactory.h"
+#include "asterisk/frame.h"
+#include "asterisk/translate.h"
+
+struct ast_audiohook_translate {
+	struct ast_trans_pvt *trans_pvt;
+	int format;
+};
+
+struct ast_audiohook_list {
+	struct ast_audiohook_translate in_translate[2];
+	struct ast_audiohook_translate out_translate[2];
+	AST_LIST_HEAD_NOLOCK(, ast_audiohook) spy_list;
+	AST_LIST_HEAD_NOLOCK(, ast_audiohook) whisper_list;
+	AST_LIST_HEAD_NOLOCK(, ast_audiohook) manipulate_list;
+};
+
+/*! \brief Initialize an audiohook structure
+ * \param audiohook Audiohook structure
+ * \param type
+ * \param source
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source)
+{
+	/* Need to keep the type and source */
+	audiohook->type = type;
+	audiohook->source = source;
+
+	/* Initialize lock that protects our audiohook */
+	ast_mutex_init(&audiohook->lock);
+	ast_cond_init(&audiohook->trigger, NULL);
+
+	/* Setup the factories that are needed for this audiohook type */
+	switch (type) {
+	case AST_AUDIOHOOK_TYPE_SPY:
+		ast_slinfactory_init(&audiohook->read_factory);
+	case AST_AUDIOHOOK_TYPE_WHISPER:
+		ast_slinfactory_init(&audiohook->write_factory);
+		break;
+	default:
+		break;
+	}
+
+	/* Since we are just starting out... this audiohook is new */
+	audiohook->status = AST_AUDIOHOOK_STATUS_NEW;
+
+	return 0;
+}
+
+/*! \brief Destroys an audiohook structure
+ * \param audiohook Audiohook structure
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_audiohook_destroy(struct ast_audiohook *audiohook)
+{
+	/* Drop the factories used by this audiohook type */
+	switch (audiohook->type) {
+	case AST_AUDIOHOOK_TYPE_SPY:
+		ast_slinfactory_destroy(&audiohook->read_factory);
+	case AST_AUDIOHOOK_TYPE_WHISPER:
+		ast_slinfactory_destroy(&audiohook->write_factory);
+		break;
+	default:
+		break;
+	}
+
+	/* Destroy translation path if present */
+	if (audiohook->trans_pvt)
+		ast_translator_free_path(audiohook->trans_pvt);
+
+	/* Lock and trigger be gone! */
+	ast_cond_destroy(&audiohook->trigger);
+	ast_mutex_destroy(&audiohook->lock);
+
+	return 0;
+}
+
+/*! \brief Writes a frame into the audiohook structure
+ * \param audiohook Audiohook structure
+ * \param direction Direction the audio frame came from
+ * \param frame Frame to write in
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_audiohook_write_frame(struct ast_audiohook *audiohook, enum ast_audiohook_direction direction, struct ast_frame *frame)
+{
+	struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
+
+	/* Write frame out to respective factory */
+	ast_slinfactory_feed(factory, frame);
+
+	/* If we need to notify the respective handler of this audiohook, do so */
+	switch (ast_test_flag(audiohook, AST_AUDIOHOOK_TRIGGER_MODE)) {
+	case AST_AUDIOHOOK_TRIGGER_READ:
+		if (direction == AST_AUDIOHOOK_DIRECTION_READ)
+			ast_cond_signal(&audiohook->trigger);
+		break;
+	case AST_AUDIOHOOK_TRIGGER_WRITE:
+		if (direction == AST_AUDIOHOOK_DIRECTION_WRITE)
+			ast_cond_signal(&audiohook->trigger);
+		break;
+	default:
+		break;
+	}
+
+	return 0;
+}
+

[... 1305 lines stripped ...]



More information about the asterisk-commits mailing list