[asterisk-commits] mmichelson: branch mmichelson/chan_fixup r173314 - in /team/mmichelson/chan_f...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Feb 3 19:08:17 CST 2009


Author: mmichelson
Date: Tue Feb  3 19:08:17 2009
New Revision: 173314

URL: http://svn.digium.com/svn-view/asterisk?view=rev&rev=173314
Log:
Initial commit of new "autochan" work

What is an autochan? Well, I'm sure glad you asked! It's a means
by which one may have a pointer to a channel which is "owned" by
a separate thread from your own and which will automatically update
should the channel that you were pointing to be masqueraded.

This commit introduces the basic API as well as changes to
app_mixmonitor to make use of it.

The following still needs to be done:

1. Add doxygen to the autochan API
2. Convert app_chanspy to use an autochan
3. Find other areas in the code which may benefit from
   the use of an autochan


Added:
    team/mmichelson/chan_fixup/include/asterisk/autochan.h   (with props)
    team/mmichelson/chan_fixup/main/autochan.c   (with props)
Modified:
    team/mmichelson/chan_fixup/apps/app_mixmonitor.c
    team/mmichelson/chan_fixup/main/Makefile
    team/mmichelson/chan_fixup/main/channel.c

Modified: team/mmichelson/chan_fixup/apps/app_mixmonitor.c
URL: http://svn.digium.com/svn-view/asterisk/team/mmichelson/chan_fixup/apps/app_mixmonitor.c?view=diff&rev=173314&r1=173313&r2=173314
==============================================================================
--- team/mmichelson/chan_fixup/apps/app_mixmonitor.c (original)
+++ team/mmichelson/chan_fixup/apps/app_mixmonitor.c Tue Feb  3 19:08:17 2009
@@ -54,6 +54,7 @@
 #include "asterisk/app.h"
 #include "asterisk/linkedlists.h"
 #include "asterisk/utils.h"
+#include "asterisk/autochan.h"
 
 #define get_volfactor(x) x ? ((x > 0) ? (1 << x) : ((1 << abs(x)) * -1)) : 0
 
@@ -98,7 +99,7 @@
 	char *post_process;
 	char *name;
 	unsigned int flags;
-	struct ast_channel *chan;
+	struct ast_autochan *autochan;
 };
 
 enum {
@@ -152,31 +153,33 @@
 
 	if (option_verbose > 1)
 		ast_verbose(VERBOSE_PREFIX_2 "Begin MixMonitor Recording %s\n", mixmonitor->name);
-	
+
 	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;
-		
+
 		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)) {
+
+		ast_autochan_lock(mixmonitor->autochan);
+		if (!ast_test_flag(mixmonitor, MUXFLAG_BRIDGED) || ast_bridged_channel(mixmonitor->autochan->chan)) {
+			ast_autochan_unlock(mixmonitor->autochan);
 			/* 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;
@@ -186,6 +189,8 @@
 			/* Write out the frame */
 			if (fs)
 				ast_writestream(fs, fr);
+		} else {
+			ast_autochan_unlock(mixmonitor->autochan);
 		}
 
 		/* All done! free it. */
@@ -195,7 +200,7 @@
 	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);
 
@@ -208,8 +213,9 @@
 		ast_safe_system(mixmonitor->post_process);
 	}
 
+	ast_autochan_destroy(mixmonitor->autochan);
+
 	free(mixmonitor);
-
 
 	return NULL;
 }
@@ -248,7 +254,7 @@
 
 	/* Copy over flags and channel name */
 	mixmonitor->flags = flags;
-	mixmonitor->chan = chan;
+	mixmonitor->autochan = ast_autochan_setup(chan);
 	mixmonitor->name = (char *) mixmonitor + sizeof(*mixmonitor);
 	strcpy(mixmonitor->name, chan->name);
 	if (!ast_strlen_zero(postprocess2)) {

Added: team/mmichelson/chan_fixup/include/asterisk/autochan.h
URL: http://svn.digium.com/svn-view/asterisk/team/mmichelson/chan_fixup/include/asterisk/autochan.h?view=auto&rev=173314
==============================================================================
--- team/mmichelson/chan_fixup/include/asterisk/autochan.h (added)
+++ team/mmichelson/chan_fixup/include/asterisk/autochan.h Tue Feb  3 19:08:17 2009
@@ -1,0 +1,49 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) <Year>, <Your Name Here>
+ *
+ * Mark Michelson <mmichelson 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 "smart" channels
+ *
+ * \author Mark Michelson <mmichelson at digium.com>
+ * 
+ */
+
+#include "asterisk.h"
+#include "asterisk/lock.h"
+#include "asterisk/linkedlists.h"
+
+#ifndef _ASTERISK_AUTOCHAN_H
+#define _ASTERISK_AUTOCHAN_H
+
+struct ast_autochan {
+	struct ast_channel *chan;
+	ast_mutex_t lock;
+	AST_LIST_ENTRY(ast_autochan) list;
+};
+
+struct ast_autochan *ast_autochan_setup(struct ast_channel *chan);
+
+void ast_autochan_destroy(struct ast_autochan *autochan);
+
+void ast_autochan_new_channel(struct ast_channel *old, struct ast_channel *new);
+
+#define ast_autochan_lock(a) ast_mutex_lock(&a->lock)
+#define ast_autochan_unlock(a) ast_mutex_unlock(&a->lock)
+
+#endif /* _ASTERISK_AUTOCHAN_H */

Propchange: team/mmichelson/chan_fixup/include/asterisk/autochan.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/mmichelson/chan_fixup/include/asterisk/autochan.h
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/mmichelson/chan_fixup/include/asterisk/autochan.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/mmichelson/chan_fixup/main/Makefile
URL: http://svn.digium.com/svn-view/asterisk/team/mmichelson/chan_fixup/main/Makefile?view=diff&rev=173314&r1=173313&r2=173314
==============================================================================
--- team/mmichelson/chan_fixup/main/Makefile (original)
+++ team/mmichelson/chan_fixup/main/Makefile Tue Feb  3 19:08:17 2009
@@ -27,7 +27,7 @@
 	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 \
-	audiohook.o
+	audiohook.o autochan.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: team/mmichelson/chan_fixup/main/autochan.c
URL: http://svn.digium.com/svn-view/asterisk/team/mmichelson/chan_fixup/main/autochan.c?view=auto&rev=173314
==============================================================================
--- team/mmichelson/chan_fixup/main/autochan.c (added)
+++ team/mmichelson/chan_fixup/main/autochan.c Tue Feb  3 19:08:17 2009
@@ -1,0 +1,97 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) <Year>, <Your Name Here>
+ *
+ * Mark Michelson <mmichelson 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 "smart" channels
+ *
+ * \author Mark Michelson <mmichelson at digium.com>
+ * 
+ */
+
+#include "asterisk.h"
+#include "asterisk/autochan.h"
+#include "asterisk/utils.h"
+#include "asterisk/linkedlists.h"
+#include "asterisk/options.h"
+#include "asterisk/channel.h"
+
+AST_LIST_HEAD_STATIC(autochans, ast_autochan);
+
+struct ast_autochan *ast_autochan_setup(struct ast_channel *chan)
+{
+	struct ast_autochan *autochan;
+
+	if (!(autochan = ast_calloc(1, sizeof(*autochan)))) {
+		return NULL;
+	}
+
+	ast_mutex_init(&autochan->lock);
+	autochan->chan = chan;
+
+	AST_LIST_LOCK(&autochans);
+	AST_LIST_INSERT_TAIL(&autochans, autochan, list);
+	AST_LIST_UNLOCK(&autochans);
+
+	if (option_debug > 1) {
+		ast_log(LOG_DEBUG, "Created autochan %p to hold channel %s (%p)\n", autochan, chan->name, chan);
+	}
+
+	return autochan;
+}
+
+void ast_autochan_destroy(struct ast_autochan *autochan)
+{
+	struct ast_autochan *autochan_iter;
+
+	AST_LIST_LOCK(&autochans);
+	AST_LIST_TRAVERSE_SAFE_BEGIN(&autochans, autochan_iter, list) {
+		if (autochan_iter == autochan) {
+			AST_LIST_REMOVE_CURRENT(&autochans, list);
+			if (option_debug > 1) {
+				ast_log(LOG_DEBUG, "Removed autochan %p from the list, about to free it\n", autochan);
+			}
+			break;
+		}
+	}
+	AST_LIST_TRAVERSE_SAFE_END;
+	AST_LIST_UNLOCK(&autochans);
+	/* I'm probably just being paranoid */
+	ast_autochan_lock(autochan);
+	ast_autochan_unlock(autochan);
+	ast_mutex_destroy(&autochan->lock);
+	ast_free(autochan);
+}
+
+void ast_autochan_new_channel(struct ast_channel *old, struct ast_channel *new)
+{
+	struct ast_autochan *autochan;
+
+	AST_LIST_LOCK(&autochans);
+	AST_LIST_TRAVERSE(&autochans, autochan, list) {
+		ast_autochan_lock(autochan);
+		if (autochan->chan == old) {
+			autochan->chan = new;
+			if (option_debug > 1) {
+				ast_log(LOG_DEBUG, "Autochan %p used to hold channel %s (%p) but now holds channel %s (%p)\n", autochan, old->name, old, new->name, new);
+			}
+		}
+		ast_autochan_unlock(autochan);
+	}
+	AST_LIST_UNLOCK(&autochans);
+}

Propchange: team/mmichelson/chan_fixup/main/autochan.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/mmichelson/chan_fixup/main/autochan.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/mmichelson/chan_fixup/main/autochan.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/mmichelson/chan_fixup/main/channel.c
URL: http://svn.digium.com/svn-view/asterisk/team/mmichelson/chan_fixup/main/channel.c?view=diff&rev=173314&r1=173313&r2=173314
==============================================================================
--- team/mmichelson/chan_fixup/main/channel.c (original)
+++ team/mmichelson/chan_fixup/main/channel.c Tue Feb  3 19:08:17 2009
@@ -69,6 +69,7 @@
 #include "asterisk/sha1.h"
 #include "asterisk/threadstorage.h"
 #include "asterisk/slinfactory.h"
+#include "asterisk/autochan.h"
 
 /* uncomment if you have problems with 'monitoring' synchronized files */
 #if 0
@@ -3676,6 +3677,8 @@
 		AST_LIST_APPEND_LIST(&original->datastores, &clone->datastores, entry);
 	}
 
+	ast_autochan_new_channel(clone, original);
+
 	clone_variables(original, clone);
 	/* Presense of ADSI capable CPE follows clone */
 	original->adsicpe = clone->adsicpe;




More information about the asterisk-commits mailing list