[asterisk-commits] russell: branch russell/sandbox r81681 - in /team/russell/sandbox: funcs/ inc...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Sep 6 09:48:33 CDT 2007


Author: russell
Date: Thu Sep  6 09:48:33 2007
New Revision: 81681

URL: http://svn.digium.com/view/asterisk?view=rev&rev=81681
Log:
Add silly func_static.  When you use it, you can press * and # to 
increase/decrease the static in your call.  "I'm losing you, bad connection,
have to go now!"

Added:
    team/russell/sandbox/funcs/func_static.c   (with props)
Modified:
    team/russell/sandbox/include/asterisk/frame.h
    team/russell/sandbox/main/frame.c

Added: team/russell/sandbox/funcs/func_static.c
URL: http://svn.digium.com/view/asterisk/team/russell/sandbox/funcs/func_static.c?view=auto&rev=81681
==============================================================================
--- team/russell/sandbox/funcs/func_static.c (added)
+++ team/russell/sandbox/funcs/func_static.c Thu Sep  6 09:48:33 2007
@@ -1,0 +1,171 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2007, Digium, Inc.
+ *
+ * Joshua Colp <jcolp at digium.com> 
+ * Russell Bryant <russell 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 Technology independent static control
+ *
+ * \author Joshua Colp <jcolp at digium.com> 
+ * \author Russell Bryant <russell at digium.com>
+ *
+ * \ingroup functions
+ *
+ * This function is basically a copy of func_volume, except that it performs
+ * a different operation on the audio.  It introduces static instead of changing
+ * the volume.  This would be useful if you want to end a phone call by blaming
+ * it on a bad connection.
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include <stdlib.h>
+
+#include "asterisk/module.h"
+#include "asterisk/channel.h"
+#include "asterisk/pbx.h"
+#include "asterisk/utils.h"
+#include "asterisk/linkedlists.h"
+#include "asterisk/audiohook.h"
+
+struct static_information {
+	struct ast_audiohook audiohook;
+	unsigned int tx_static;
+	unsigned int rx_static;
+};
+
+static void destroy_callback(void *data)
+{
+	struct static_information *vi = data;
+
+	/* Destroy the audiohook, and destroy ourselves */
+	ast_audiohook_destroy(&vi->audiohook);
+	free(vi);
+
+	return;
+}
+
+/*! \brief Static structure for datastore information */
+static const struct ast_datastore_info static_datastore = {
+        .type = "static",
+        .destroy = destroy_callback
+};
+
+static int static_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
+{
+	struct ast_datastore *datastore = NULL;
+	struct static_information *vi = NULL;
+	unsigned int *amount = NULL;
+
+	/* If the audiohook is stopping it means the channel is shutting down.... but we let the datastore destroy take care of it */
+	if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE)
+		return 0;
+
+	/* Grab datastore which contains our amount information */
+	if (!(datastore = ast_channel_datastore_find(chan, &static_datastore, NULL)))
+		return 0;
+
+	vi = datastore->data;
+
+	/* If this is DTMF then allow them to increase/decrease the amounts */
+	if (frame->frametype == AST_FRAME_DTMF) {
+		/* Only use DTMF coming from the source... not going to it */
+		if (direction != AST_AUDIOHOOK_DIRECTION_READ)
+			return 0;
+		if (frame->subclass == '*' && vi->tx_static < UINT_MAX) {
+			ast_log(LOG_DEBUG, "Increasing static\n");
+			vi->tx_static += 100;
+			vi->rx_static += 100;
+		} else if (frame->subclass == '#' && vi->tx_static) {
+			ast_log(LOG_DEBUG, "Decreasing static\n");
+			vi->tx_static -= 100;
+			vi->rx_static -= 100;
+		}
+	} else if (frame->frametype == AST_FRAME_VOICE) {
+		/* Based on direction of frame grab the amount, and confirm it is applicable */
+		if (!(amount = (direction == AST_AUDIOHOOK_DIRECTION_READ) ? &vi->rx_static : &vi->tx_static) || !*amount)
+			return 0;
+
+		ast_frame_add_static(frame, *amount);
+	}
+
+	return 0;
+}
+
+static int static_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
+{
+	struct ast_datastore *datastore = NULL;
+	struct static_information *vi = NULL;
+	int is_new = 0;
+
+	if (!(datastore = ast_channel_datastore_find(chan, &static_datastore, NULL))) {
+		/* Allocate a new datastore to hold the reference to this static and audiohook information */
+		if (!(datastore = ast_channel_datastore_alloc(&static_datastore, NULL)))
+			return 0;
+		if (!(vi = ast_calloc(1, sizeof(*vi)))) {
+			ast_channel_datastore_free(datastore);
+			return 0;
+		}
+		ast_audiohook_init(&vi->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Volume");
+		vi->audiohook.manipulate_callback = static_callback;
+		ast_set_flag(&vi->audiohook, AST_AUDIOHOOK_WANTS_DTMF);
+		is_new = 1;
+	} else {
+		vi = datastore->data;
+	}
+
+	/* Adjust amount on static information structure */
+	if (!strcasecmp(data, "tx"))
+		vi->tx_static = atoi(value);
+	else if (!strcasecmp(data, "rx"))
+		vi->rx_static = atoi(value);
+
+	if (is_new) {
+		datastore->data = vi;
+		ast_channel_datastore_add(chan, datastore);
+		ast_audiohook_attach(chan, &vi->audiohook);
+	}
+
+	return 0;
+}
+
+static struct ast_custom_function static_function = {
+	.name = "STATIC",
+	.synopsis = "Set the TX or RX static of a channel",
+	.syntax = "STATIC(TX|RX)",
+	.desc =
+	"  The STATIC function can be used to increase or decrease the tx or\n"
+	"rx static on any channel.  For example:\n"
+	"  Set(STATIC(TX)=3)\n"
+	"  Set(STATIC(RX)=2)\n",
+	.write = static_write,
+};
+
+static int unload_module(void)
+{
+	return ast_custom_function_unregister(&static_function);
+}
+
+static int load_module(void)
+{
+	return ast_custom_function_register(&static_function);
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Technology independent static control");

Propchange: team/russell/sandbox/funcs/func_static.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/russell/sandbox/funcs/func_static.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/russell/sandbox/funcs/func_static.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/russell/sandbox/include/asterisk/frame.h
URL: http://svn.digium.com/view/asterisk/team/russell/sandbox/include/asterisk/frame.h?view=diff&rev=81681&r1=81680&r2=81681
==============================================================================
--- team/russell/sandbox/include/asterisk/frame.h (original)
+++ team/russell/sandbox/include/asterisk/frame.h Thu Sep  6 09:48:33 2007
@@ -577,6 +577,8 @@
  */
 int ast_frame_adjust_volume(struct ast_frame *f, int adjustment);
 
+int ast_frame_add_static(struct ast_frame *f, unsigned int amount);
+
 /*!
   \brief Sums two frames of audio samples.
   \param f1 The first frame (which will contain the result)

Modified: team/russell/sandbox/main/frame.c
URL: http://svn.digium.com/view/asterisk/team/russell/sandbox/main/frame.c?view=diff&rev=81681&r1=81680&r2=81681
==============================================================================
--- team/russell/sandbox/main/frame.c (original)
+++ team/russell/sandbox/main/frame.c Thu Sep  6 09:48:33 2007
@@ -1471,3 +1471,23 @@
 
 	return 0;
 }
+
+int ast_frame_add_static(struct ast_frame *f, unsigned int amount)
+{
+	short *cur_data;
+	unsigned int cur_sample;
+
+	if ((f->frametype != AST_FRAME_VOICE) || (f->subclass != AST_FORMAT_SLINEAR))
+		return -1;
+
+	if (!amount)
+		return 0;
+
+	for (cur_sample = 0, cur_data = f->data; cur_sample < f->samples; 
+		cur_sample++, cur_data++) {
+		/* Apply a random offset within +/- amount */
+		*cur_data += ( (ast_random() % 2) ? -1 : 1 ) * (ast_random() % amount);
+	}
+
+	return 0;
+}




More information about the asterisk-commits mailing list