[asterisk-commits] dvossel: branch dvossel/kill_the_echo r317383 - /team/dvossel/kill_the_echo/f...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Thu May 5 15:53:58 CDT 2011
Author: dvossel
Date: Thu May 5 15:53:55 2011
New Revision: 317383
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=317383
Log:
Addition of func_echocancel
I have no idea what this will do.
Added:
team/dvossel/kill_the_echo/funcs/func_echocancel.c (with props)
Added: team/dvossel/kill_the_echo/funcs/func_echocancel.c
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/kill_the_echo/funcs/func_echocancel.c?view=auto&rev=317383
==============================================================================
--- team/dvossel/kill_the_echo/funcs/func_echocancel.c (added)
+++ team/dvossel/kill_the_echo/funcs/func_echocancel.c Thu May 5 15:53:55 2011
@@ -1,0 +1,176 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2011, Digium, Inc.
+ *
+ * David Vossel <dvossel 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 Echo Cancel
+ *
+ * \author David Vossel <dvossel at digium.com>
+ *
+ * \ingroup functions
+ */
+
+/*** MODULEINFO
+ <depend>speex</depend>
+ <depend>speex_preprocess</depend>
+ <use>speexdsp</use>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include <speex/speex_echo.h>
+#include "asterisk/module.h"
+#include "asterisk/channel.h"
+#include "asterisk/pbx.h"
+#include "asterisk/utils.h"
+#include "asterisk/audiohook.h"
+
+struct echocancel_data {
+ int16_t buf[4048];
+ struct ast_audiohook audiohook;
+ unsigned int samplerate;
+ SpeexEchoState *echo_state;
+};
+
+static void destroy_callback(void *data)
+{
+ struct echocancel_data *echodata = data;
+
+ ast_audiohook_destroy(&echodata->audiohook);
+
+ if (echodata->echo_state) {
+ speex_echo_state_destroy(echodata->echo_state);
+ }
+ ast_free(echodata);
+};
+
+static const struct ast_datastore_info echocancel_datastore = {
+ .type = "echocancel",
+ .destroy = destroy_callback
+};
+
+static int echo_data_set(unsigned int samplerate, struct echocancel_data *echodata)
+{
+ int init_state = echodata->echo_state ? 0 : 1;
+
+ if (samplerate != echodata->samplerate) {
+ echodata->samplerate = samplerate;
+ init_state = 1;
+ }
+
+ if (init_state) {
+ if (echodata->echo_state) {
+ speex_echo_state_destroy(echodata->echo_state);
+ }
+ echodata->echo_state = speex_echo_state_init((samplerate/50), (samplerate/2));
+ }
+ return 0;
+}
+
+static int echocancel_cb(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *f, enum ast_audiohook_direction direction)
+{
+ struct ast_datastore *datastore = NULL;
+ struct echocancel_data *echodata = NULL;
+
+ if (!f) {
+ return 0;
+ }
+ if ((audiohook->status == AST_AUDIOHOOK_STATUS_DONE) ||
+ (f->frametype != AST_FRAME_VOICE) ||
+ !(ast_format_is_slinear(&f->subclass.format))) {
+ return -1;
+ }
+ if (!(datastore = ast_channel_datastore_find(chan, &echocancel_datastore, NULL))) {
+ return -1;
+ }
+
+ echodata = datastore->data;
+
+ echo_data_set(ast_format_rate(&f->subclass.format), echodata);
+
+ if (direction == AST_AUDIOHOOK_DIRECTION_WRITE) {
+ speex_echo_playback(echodata->echo_state, (int16_t *) f->data.ptr);
+ } else {
+ memcpy(echodata->buf, f->data.ptr, f->datalen);
+ speex_echo_capture(echodata->echo_state, echodata->buf, (int16_t *) f->data.ptr);
+ }
+
+ return 0;
+}
+
+static int echocancel_on(struct ast_channel *chan)
+{
+ struct ast_datastore *datastore = NULL;
+ struct echocancel_data *echodata = NULL;
+
+ ast_channel_lock(chan);
+ if (!(datastore = ast_channel_datastore_find(chan, &echocancel_datastore, NULL))) {
+ ast_channel_unlock(chan);
+
+ if (!(datastore = ast_datastore_alloc(&echocancel_datastore, NULL))) {
+ return -1;
+ }
+ if (!(echodata = ast_calloc(1, sizeof(*echodata)))) {
+ ast_datastore_free(datastore);
+ return -1;
+ }
+
+ ast_audiohook_init(&echodata->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "echocancel", AST_AUDIOHOOK_MANIPULATE_ALL_RATES);
+ echodata->audiohook.manipulate_callback = echocancel_cb;
+ datastore->data = echodata;
+
+ ast_channel_lock(chan);
+ ast_channel_datastore_add(chan, datastore);
+ ast_channel_unlock(chan);
+ ast_audiohook_attach(chan, &echodata->audiohook);
+ }
+
+ return 0;
+}
+
+static int echocancel_helper(struct ast_channel *chan, const char *cmd, char *data, const char *value)
+{
+ if (!strcasecmp(value, "on")) {
+ return echocancel_on(chan);
+ } else if (!strcasecmp(value, "off")) {
+ /* TODO */
+ ast_log(LOG_WARNING, "Turning off echo cancel is not implemented yet");
+ }
+
+ return 0;
+}
+
+static struct ast_custom_function echocancel_function = {
+ .name = "ECHO_CANCEL",
+ .write = echocancel_helper,
+};
+
+static int unload_module(void)
+{
+ return ast_custom_function_unregister(&echocancel_function);
+}
+
+static int load_module(void)
+{
+ int res = ast_custom_function_register(&echocancel_function);
+ return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Audio Effects Dialplan Functions");
Propchange: team/dvossel/kill_the_echo/funcs/func_echocancel.c
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: team/dvossel/kill_the_echo/funcs/func_echocancel.c
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Propchange: team/dvossel/kill_the_echo/funcs/func_echocancel.c
------------------------------------------------------------------------------
svn:mime-type = text/plain
More information about the asterisk-commits
mailing list