[asterisk-commits] oej: branch oej/silence-detection-games-1.8 r411805 - in /team/oej/silence-de...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Apr 7 09:29:29 CDT 2014


Author: oej
Date: Mon Apr  7 09:29:25 2014
New Revision: 411805

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=411805
Log:
I admit, Josh was right. 

Added:
    team/oej/silence-detection-games-1.8/README.silencegames.txt   (with props)
    team/oej/silence-detection-games-1.8/include/asterisk/silencedetection.h   (with props)
    team/oej/silence-detection-games-1.8/main/silencedetection.c   (with props)
Modified:
    team/oej/silence-detection-games-1.8/channels/chan_sip.c
    team/oej/silence-detection-games-1.8/channels/sip/include/sip.h

Added: team/oej/silence-detection-games-1.8/README.silencegames.txt
URL: http://svnview.digium.com/svn/asterisk/team/oej/silence-detection-games-1.8/README.silencegames.txt?view=auto&rev=411805
==============================================================================
--- team/oej/silence-detection-games-1.8/README.silencegames.txt (added)
+++ team/oej/silence-detection-games-1.8/README.silencegames.txt Mon Apr  7 09:29:25 2014
@@ -1,0 +1,4 @@
+
+Where to hook in this stuff
+- sip_new() for calls out
+- answer somewhere for incoming calls

Propchange: team/oej/silence-detection-games-1.8/README.silencegames.txt
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/oej/silence-detection-games-1.8/README.silencegames.txt
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/oej/silence-detection-games-1.8/README.silencegames.txt
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/oej/silence-detection-games-1.8/channels/chan_sip.c
URL: http://svnview.digium.com/svn/asterisk/team/oej/silence-detection-games-1.8/channels/chan_sip.c?view=diff&rev=411805&r1=411804&r2=411805
==============================================================================
--- team/oej/silence-detection-games-1.8/channels/chan_sip.c (original)
+++ team/oej/silence-detection-games-1.8/channels/chan_sip.c Mon Apr  7 09:29:25 2014
@@ -273,6 +273,7 @@
 #include "sip/include/sdp_crypto.h"
 #include "asterisk/ccss.h"
 #include "asterisk/xml.h"
+#include "asterisk/silencedetection.h"
 #include "sip/include/dialog.h"
 #include "sip/include/dialplan_functions.h"
 
@@ -6130,11 +6131,6 @@
 		ast_free(p->stimer);
 		p->stimer = NULL;
 	}
-	/* Destroy the silence detector */
-	if (p->sildet) {
-		ast_dsp_free(p->sildet);
-		p->sildet = NULL;
-	}
 
 	if (sip_debug_test_pvt(p))
 		ast_verbose("Really destroying SIP dialog '%s' Method: %s\n", p->callid, sip_methods[p->method].text);
@@ -7476,11 +7472,6 @@
 		return FALSE;
 	}
 	/* Allocate a new DSP */
-	if (!(dialog->sildet = ast_dsp_new())) {
-		ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
-		return FALSE;
-	}
-	ast_dsp_set_threshold(dialog->sildet, ast_dsp_get_threshold_from_settings(dialog->silencelevel));
 
 	/* Create the silence detector */
 	/* Put channel in the right codec mode: SLINEAR
@@ -7490,10 +7481,9 @@
 	 */
 	if ((res = ast_set_read_format(dialog->owner, AST_FORMAT_SLINEAR)) < 0) {
 		ast_log(LOG_WARNING, "Unable to set channel to linear mode, giving up\n");
-		ast_dsp_free(dialog->sildet);
-		dialog->sildet = NULL;
 		return FALSE;
 	}
+	ast_sildet_activate(dialog->owner, dialog->silencelevel, dialog->silenceframes);
 
 	/* We now have a call where we have a DSP. The rest of the magic is happening somewhere else in chan_sip. */
 	ast_debug(3, "Activated silence detection on call %s\n", dialog->callid);
@@ -8018,23 +8008,6 @@
 		ast_frfree(fr);
 		fr = &ast_null_frame;
 	}
-	if (p->sildet && fr->frametype == AST_FRAME_VOICE && ast->_state == AST_STATE_UP) {
-		ast_dsp_silence(p->sildet, fr, &dsptime);
-		if (p->silencecounter == 0 && dsptime) {
-			ast_debug(8, " ++++ Silence started ++++ on chan %s\n", ast->name);
-		}
-		if (!dsptime) {
-			p->silencecounter = 0;		/* No more silence */
-			ast_debug(8, " ++++ Silence stopped ++++ on chan %s\n", ast->name);
-		} else {
-			if (p->silencecounter > 0) {
-				ast_debug(8, "          ++++ Silence continues ++++ on chan %s\n", ast->name);
-			}
-			p->silencecounter++;
-		}
-	}
-
-
 	sip_pvt_unlock(p);
 
 	return fr;

Modified: team/oej/silence-detection-games-1.8/channels/sip/include/sip.h
URL: http://svnview.digium.com/svn/asterisk/team/oej/silence-detection-games-1.8/channels/sip/include/sip.h?view=diff&rev=411805&r1=411804&r2=411805
==============================================================================
--- team/oej/silence-detection-games-1.8/channels/sip/include/sip.h (original)
+++ team/oej/silence-detection-games-1.8/channels/sip/include/sip.h Mon Apr  7 09:29:25 2014
@@ -1093,7 +1093,6 @@
 	uint32_t dialogver;                 /*!< SUBSCRIBE: Version for subscription dialog-info */
 
 	struct ast_dsp *dsp;                /*!< Inband DTMF or Fax CNG tone Detection dsp */
-	struct ast_dsp *sildet;             /*!< DSP used for silence detection */
 	unsigned int silencelevel;	    /*!< Silence treshold */
 	unsigned int silenceperiod;	    /*!< How many frames to wait for silence before activating silence
 						 support and sending CNG */

Added: team/oej/silence-detection-games-1.8/include/asterisk/silencedetection.h
URL: http://svnview.digium.com/svn/asterisk/team/oej/silence-detection-games-1.8/include/asterisk/silencedetection.h?view=auto&rev=411805
==============================================================================
--- team/oej/silence-detection-games-1.8/include/asterisk/silencedetection.h (added)
+++ team/oej/silence-detection-games-1.8/include/asterisk/silencedetection.h Mon Apr  7 09:29:25 2014
@@ -1,0 +1,40 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2014, Olle E. Johansson
+ *
+ * Olle E. Johansson <oej at edvina.net>
+ *
+ * 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 Audiohook for silnce detection
+ */
+
+#ifndef _ASTERISK_SILENCEDETECTION_H
+#define _ASTERISK_SILENCEDETECTION_H
+
+#if defined(__cplusplus) || defined(c_plusplus)
+extern "C" {
+#endif
+
+/*! \brief Activation of silence detection 
+	\param silencelevel 	Audio treshold for silence
+	\param silenceframes	Number of frames before we react
+*/
+int ast_sildet_activate(struct ast_channel *chan, int silencelevel, int silenceframes)
+
+#if defined(__cplusplus) || defined(c_plusplus)
+}
+#endif
+
+#endif /* _ASTERISK_SILENCEDETECTION_H */

Propchange: team/oej/silence-detection-games-1.8/include/asterisk/silencedetection.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/oej/silence-detection-games-1.8/include/asterisk/silencedetection.h
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/oej/silence-detection-games-1.8/include/asterisk/silencedetection.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: team/oej/silence-detection-games-1.8/main/silencedetection.c
URL: http://svnview.digium.com/svn/asterisk/team/oej/silence-detection-games-1.8/main/silencedetection.c?view=auto&rev=411805
==============================================================================
--- team/oej/silence-detection-games-1.8/main/silencedetection.c (added)
+++ team/oej/silence-detection-games-1.8/main/silencedetection.c Mon Apr  7 09:29:25 2014
@@ -1,0 +1,248 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2014, Olle E. Johansson
+ *
+ * Olle E. Johansson <oej at edvina.net>
+ *
+ * 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 Silence Detection audiohooks
+ *
+ * \author Olle E. Johansson <oej at edvina.net>
+ *
+ *
+ * This is an internal API and have no functions, applications or other cool stuff to expose.
+ *
+ * \note This module only handles audio streams 
+ * 	For silence in video, check Ingmar Bergman movies on Wikipedia. We have
+ *	no current way to detect video "silence" so we can't optimize that type of movies.
+ */
+
+/*** MODULEINFO
+	<support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+//#include <time.h>
+//#include <string.h>
+//#include <stdio.h>
+//#include <stdlib.h>
+//#include <unistd.h>
+//#include <errno.h>
+
+#include "asterisk/options.h"
+#include "asterisk/logger.h"
+#include "asterisk/channel.h"
+#include "asterisk/module.h"
+#include "asterisk/config.h"
+#include "asterisk/file.h"
+#include "asterisk/pbx.h"
+#include "asterisk/frame.h"
+#include "asterisk/utils.h"
+#include "asterisk/audiohook.h"
+#include "asterisk/manager.h"
+
+
+/*! Our own datastore */
+struct silence_detection_info {
+	struct ast_audiohook audiohook;
+	struct ast_dsp *dsp;			/*!< DSP used for silence detection */
+	unsigned int silencelevel;		/*!< Silence treshold */
+        unsigned int silenceframes;		/*!< How many frames to wait for silence before activating silence
+							support and sending CNG */
+        unsigned int silencecounter;		/*!< Frame Counter used for silence detection. */
+	int detect;				/*!< Silence detected */
+	int active;
+};
+
+
+#define TRUE 1
+#define FALSE 0
+
+/*! Datastore destroy audiohook callback */
+static void destroy_callback(void *data)
+{
+	struct silence_detection_info *sildet = data;
+
+	ast_dsp_free(sildet->dsp);
+	p->sildet = NULL;
+
+	/* Destroy the audiohook, and destroy ourselves */
+	ast_audiohook_destroy(&sil->audiohook);
+	ast_free(sil);
+	ast_module_unref(ast_module_info->self);
+
+	return;
+}
+
+/*! \brief Static structure for datastore information */
+static const struct ast_datastore_info sildet_datastore = {
+	.type = "sildet",
+	.destroy = destroy_callback
+};
+
+/*! \brief The callback from the audiohook subsystem. We basically get a frame to have fun with */
+static int silence_detection_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
+{
+	struct ast_datastore *datastore = NULL;
+	struct silence_detection_info *sildet = 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;
+	}
+
+	ast_channel_lock(chan);
+	/* Grab datastore which contains our mute information */
+	if (!(datastore = ast_channel_datastore_find(chan, &sildet_datastore, NULL))) {
+		ast_channel_unlock(chan);
+		ast_debug(2, "Can't find any datastore to use. Bad. \n");
+		return 0;
+	}
+
+	sildet = datastore->data;
+
+
+	/* If this is audio then allow them to increase/decrease the gains */
+	if (frame->frametype == AST_FRAME_VOICE) {
+		int dsptime = 0;
+
+		/* Based on direction of frame grab the gain, and confirm it is applicable */
+		if ((direction == AST_AUDIOHOOK_DIRECTION_READ) {
+			/* DETECT SILENCE HERE */
+			ast_dsp_silence(sildet->dsp, frame, &dsptime);
+			if (option_debug && sildet->silencecounter == 0 && dsptime) {
+				ast_debug(8, " ++++ Silence started ++++ on chan %s\n", chan->name);
+			}
+			if (!dsptime) {
+				if (option_debug && p->silencecounter > 0) {
+					ast_debug(8, " ++++ Silence stopped ++++ on chan %s\n", chan->name);
+				}
+				p->silencecounter = 0;		/* No more silence */
+			} else {
+				if (option_debug && p->silencecounter > 0) {
+					ast_debug(8, "          ++++ Silence continues ++++ on chan %s\n", chan->name);
+				}
+				p->silencecounter++;
+			}
+			/* Ok, we just want to reset all audio in this frame. Keep NOTHING, thanks. */
+			ast_frame_clear(frame);
+		}
+	}
+	ast_channel_unlock(chan);
+
+	return 0;
+}
+
+/*! \brief Initialize mute hook on channel, but don't activate it
+	\pre Assumes that the channel is locked
+*/
+static struct ast_datastore *initialize_sildethook(struct ast_channel *chan)
+{
+	struct ast_datastore *datastore = NULL;
+	struct silence_detection_info *sildet = NULL;
+
+	ast_debug(2, "Initializing new Silence Detection Audiohook \n");
+
+	/* Allocate a new datastore to hold the reference to this sildet_datastore and audiohook information */
+	if (!(datastore = ast_datastore_alloc(&sildet_datastore, NULL))) {
+		return NULL;
+	}
+
+	if (!(sildet = ast_calloc(1, sizeof(*sildet)))) {
+		ast_datastore_free(datastore);
+		return NULL;
+	}
+	if (!(sildet>dsp = ast_dsp_new())) {
+		/* We failed to create a DSP */
+		ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
+		ast_free(sildet);
+		ast_datastore_free(datastore);
+		return NULL;
+	}
+	ast_dsp_set_threshold(sildet->dsp, ast_dsp_get_threshold_from_settings(sildet->silencelevel));
+	ast_audiohook_init(&sildet->audiohook, AST_AUDIOHOOK_TYPE_SILDET, "Sildet");
+	sildet->audiohook.manipulate_callback = silence_detection_callback;
+	sildet->active = 0;
+	sildet->silencecounter = 0;
+	sildet->detect = 0;
+	datastore->data = sildet;
+	return datastore;
+}
+
+/*! \brief Add or activate mute audiohook on channel
+	Assumes channel is locked
+*/
+static int sildet_add_audiohook(struct ast_channel *chan, struct silence_detection_info *sildet, struct ast_datastore *datastore)
+{
+	/* Activate the settings */
+	ast_channel_datastore_add(chan, datastore);
+	if (ast_audiohook_attach(chan, &sildet->audiohook)) {
+		ast_log(LOG_ERROR, "Failed to attach audiohook for silence detection on channel %s\n", chan->name);
+		return -1;
+	}
+	ast_module_ref(ast_module_info->self);
+	ast_debug(2, "Initialized audiohook for silence detection on channel %s\n", chan->name);
+	return 0;
+}
+
+/*! \brief Activation of silence detection */
+int ast_sildet_activate(struct ast_channel *chan, int silencelevel, int silenceframes)
+{
+	struct ast_datastore *datastore = NULL;
+	struct silence_detection_info *sildet = NULL;
+
+	int is_new = 0;
+
+	if (!chan) {
+		ast_log(LOG_WARNING, "No channel was provided.\n", cmd);
+		return -1;
+	}
+
+	ast_channel_lock(chan);
+	if (!(datastore = ast_channel_datastore_find(chan, &sildet_datastore, NULL))) {
+		if (!(datastore = initialize_sildethook(chan))) {
+			ast_channel_unlock(chan);
+			return 0;
+		}
+		is_new = 1;
+	}
+
+	/* Configure the silence detection */
+	sildet = datastore->data;
+	sildet->silencelevel = silencelevel;
+	sildet->silenceframes = silenceframes;
+	sildet->active = 1;
+	sildet->silencecounter = 0;
+	sildet->detect = 0;
+
+	/* Reset the DSP */
+	ast_dsp_set_threshold(sildet->dsp, ast_dsp_get_threshold_from_settings(sildet->silencelevel));
+
+	if (is_new) {
+		if (sildet_add_audiohook(chan, sildet, datastore)) {
+			/* Can't add audiohook - already printed error message */
+			ast_datastore_free(datastore);
+			ast_free(mute);
+		}
+	}
+	ast_channel_unlock(chan);
+
+	return 0;
+}

Propchange: team/oej/silence-detection-games-1.8/main/silencedetection.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/oej/silence-detection-games-1.8/main/silencedetection.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/oej/silence-detection-games-1.8/main/silencedetection.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain




More information about the asterisk-commits mailing list