[Freeswitch-svn] [commit] r2583 - in freeswitch/trunk/src: . include mod/applications/mod_conference mod/asr_tts/mod_cepstral mod/endpoints/mod_wanpipe mod/languages/mod_spidermonkey

Freeswitch SVN anthm at freeswitch.org
Fri Sep 8 14:57:25 EDT 2006


Author: anthm
Date: Fri Sep  8 14:57:24 2006
New Revision: 2583

Modified:
   freeswitch/trunk/src/include/switch_buffer.h
   freeswitch/trunk/src/include/switch_channel.h
   freeswitch/trunk/src/mod/applications/mod_conference/mod_conference.c
   freeswitch/trunk/src/mod/asr_tts/mod_cepstral/mod_cepstral.c
   freeswitch/trunk/src/mod/endpoints/mod_wanpipe/mod_wanpipe.c
   freeswitch/trunk/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c
   freeswitch/trunk/src/switch_buffer.c
   freeswitch/trunk/src/switch_channel.c
   freeswitch/trunk/src/switch_core.c

Log:
add dynamic buffers

Modified: freeswitch/trunk/src/include/switch_buffer.h
==============================================================================
--- freeswitch/trunk/src/include/switch_buffer.h	(original)
+++ freeswitch/trunk/src/include/switch_buffer.h	Fri Sep  8 14:57:24 2006
@@ -62,6 +62,18 @@
  */
 SWITCH_DECLARE(switch_status_t) switch_buffer_create(switch_memory_pool_t *pool, switch_buffer_t **buffer, switch_size_t max_len);
 
+/*! \brief Allocate a new dynamic switch_buffer 
+ * \param buffer returned pointer to the new buffer
+ * \param blocksize length to realloc by as data is added
+ * \param start_len ammount of memory to reserve initially
+ * \param max_len length the buffer is allowed to grow to
+ * \return status
+ */
+SWITCH_DECLARE(switch_status_t) switch_buffer_create_dynamic(switch_buffer_t **buffer,
+															 switch_size_t blocksize,
+															 switch_size_t start_len,
+															 switch_size_t max_len);
+
 /*! \brief Get the length of a switch_buffer_t 
  * \param buffer any buffer of type switch_buffer_t
  * \return int size of the buffer.
@@ -107,6 +119,13 @@
  * \param buffer any buffer of type switch_buffer_t
  */
 SWITCH_DECLARE(void) switch_buffer_zero(switch_buffer_t *buffer);
+
+/*! \brief Destroy the buffer
+ * \param buffer buffer to destroy
+ * \note only neccessary on dynamic buffers (noop on pooled ones)
+ */
+SWITCH_DECLARE(void) switch_buffer_destroy(switch_buffer_t **buffer);
+
 /** @} */
 
 SWITCH_END_EXTERN_C

Modified: freeswitch/trunk/src/include/switch_channel.h
==============================================================================
--- freeswitch/trunk/src/include/switch_channel.h	(original)
+++ freeswitch/trunk/src/include/switch_channel.h	Fri Sep  8 14:57:24 2006
@@ -137,6 +137,12 @@
 								uint32_t flags);
 
 /*!
+  \brief Uninitalize a channel
+  \param channel the channel to uninit
+*/
+SWITCH_DECLARE(void) switch_channel_uninit(switch_channel_t *channel);
+
+/*!
   \brief Set the given channel's caller profile
   \param channel channel to assign the profile to
   \param caller_profile the profile to assign

Modified: freeswitch/trunk/src/mod/applications/mod_conference/mod_conference.c
==============================================================================
--- freeswitch/trunk/src/mod/applications/mod_conference/mod_conference.c	(original)
+++ freeswitch/trunk/src/mod/applications/mod_conference/mod_conference.c	Fri Sep  8 14:57:24 2006
@@ -40,6 +40,10 @@
 #define CONF_EVENT_MAINT "conference::maintenence"
 #define CONF_DEFAULT_LEADIN 20
 
+#define CONF_DBLOCK_SIZE CONF_BUFFER_SIZE
+#define CONF_DBUFFER_SIZE CONF_BUFFER_SIZE
+#define CONF_DBUFFER_MAX 0
+
 typedef enum {
 	FILE_STOP_CURRENT,
 	FILE_STOP_ALL
@@ -2355,7 +2359,7 @@
 							   switch_core_session_get_pool(session));
 		
 		/* Setup an audio buffer for the resampled audio */
-		if (switch_buffer_create(pool, &member.resample_buffer, CONF_BUFFER_SIZE) != SWITCH_STATUS_SUCCESS) {
+		if (switch_buffer_create_dynamic(&member.resample_buffer, CONF_DBLOCK_SIZE, CONF_DBUFFER_SIZE, CONF_DBUFFER_MAX) != SWITCH_STATUS_SUCCESS) {
 			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error Creating Audio Buffer!\n");
 			goto done;
 		}
@@ -2381,13 +2385,13 @@
 	}
 	
 	/* Setup an audio buffer for the incoming audio */
-	if (switch_buffer_create(pool, &member.audio_buffer, CONF_BUFFER_SIZE) != SWITCH_STATUS_SUCCESS) {
+	if (switch_buffer_create_dynamic(&member.audio_buffer, CONF_DBLOCK_SIZE, CONF_DBUFFER_SIZE, CONF_DBUFFER_MAX) != SWITCH_STATUS_SUCCESS) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error Creating Audio Buffer!\n");
 		goto codec_done1;
 	}
 
 	/* Setup an audio buffer for the outgoing audio */
-	if (switch_buffer_create(pool, &member.mux_buffer, CONF_BUFFER_SIZE) != SWITCH_STATUS_SUCCESS) {
+	if (switch_buffer_create_dynamic(&member.mux_buffer, CONF_DBLOCK_SIZE, CONF_DBUFFER_SIZE, CONF_DBUFFER_MAX) != SWITCH_STATUS_SUCCESS) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error Creating Audio Buffer!\n");
 		goto codec_done1;
 	}
@@ -2435,6 +2439,10 @@
 	switch_core_codec_destroy(&member.write_codec);
  done:
 
+	switch_buffer_destroy(&member.resample_buffer);
+	switch_buffer_destroy(&member.audio_buffer);
+	switch_buffer_destroy(&member.mux_buffer);
+	
 	/* Release the config registry handle */
 	if (cxml) {
 		switch_xml_free(cxml);

Modified: freeswitch/trunk/src/mod/asr_tts/mod_cepstral/mod_cepstral.c
==============================================================================
--- freeswitch/trunk/src/mod/asr_tts/mod_cepstral/mod_cepstral.c	(original)
+++ freeswitch/trunk/src/mod/asr_tts/mod_cepstral/mod_cepstral.c	Fri Sep  8 14:57:24 2006
@@ -42,7 +42,8 @@
 #include <swift.h>
 #include <switch.h>
 
-#define MY_BUF_LEN 1024 * 256
+#define MY_BUF_LEN 1024 * 32
+#define MY_BLOCK_SIZE MY_BUF_LEN
 
 static const char modname[] = "mod_cepstral";
 
@@ -119,7 +120,7 @@
 			return SWITCH_STATUS_MEMERR;
 		}
 
-		if (switch_buffer_create(sh->memory_pool, &cepstral->audio_buffer, MY_BUF_LEN) != SWITCH_STATUS_SUCCESS) {
+		if (switch_buffer_create_dynamic(&cepstral->audio_buffer, MY_BLOCK_SIZE, MY_BUF_LEN, 0) != SWITCH_STATUS_SUCCESS) {
 			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Write Buffer Failed!\n");
 			return SWITCH_STATUS_MEMERR;
 		}
@@ -195,6 +196,8 @@
 	cepstral->port = NULL;
 	//cepstral->engine = NULL;
 	
+	switch_buffer_destroy(&cepstral->audio_buffer);
+
 	return SWITCH_STATUS_SUCCESS;
 }
 

Modified: freeswitch/trunk/src/mod/endpoints/mod_wanpipe/mod_wanpipe.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_wanpipe/mod_wanpipe.c	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_wanpipe/mod_wanpipe.c	Fri Sep  8 14:57:24 2006
@@ -370,6 +370,8 @@
 
 	teletone_destroy_session(&tech_pvt->tone_session);
 
+	switch_buffer_destroy(&tech_pvt->dtmf_buffer);
+	
 	return SWITCH_STATUS_SUCCESS;
 }
 
@@ -623,7 +625,7 @@
 
 	if (!tech_pvt->dtmf_buffer) {
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Allocate DTMF Buffer....");
-		if (switch_buffer_create(switch_core_session_get_pool(session), &tech_pvt->dtmf_buffer, 3192) != SWITCH_STATUS_SUCCESS) {
+		if (switch_buffer_create_dynamic(&tech_pvt->dtmf_buffer, 1024, 3192, 0) != SWITCH_STATUS_SUCCESS) {
 			switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN, SWITCH_LOG_ERROR, "Failed to allocate DTMF Buffer!\n");
 			return SWITCH_STATUS_FALSE;
 		} else {

Modified: freeswitch/trunk/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c
==============================================================================
--- freeswitch/trunk/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c	(original)
+++ freeswitch/trunk/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c	Fri Sep  8 14:57:24 2006
@@ -32,7 +32,8 @@
 #ifndef HAVE_CURL
 #define HAVE_CURL
 #endif
-#define JS_BUFFER_SIZE 131072
+#define JS_BUFFER_SIZE 1024 * 32
+#define JS_BLOCK_SIZE JS_BUFFER_SIZE
 #ifdef __ICC
 #pragma warning (disable:310 193 1418)
 #endif
@@ -2007,7 +2008,7 @@
 		}
 	}
 
-	switch_buffer_create(pool, &tto->audio_buffer, JS_BUFFER_SIZE);
+	switch_buffer_create_dynamic(&tto->audio_buffer, JS_BLOCK_SIZE, JS_BUFFER_SIZE, 0);
 	tto->pool = pool;
 	tto->obj = obj;
 	tto->cx = cx;
@@ -2027,6 +2028,8 @@
 			switch_core_timer_destroy(tto->timer);
 		}
 		teletone_destroy_session(&tto->ts);
+		switch_buffer_destroy(&tto->audio_buffer);
+		switch_buffer_destroy(&tto->loop_buffer);
 		switch_core_codec_destroy(&tto->codec);
 		pool = tto->pool;
 		tto->pool = NULL;
@@ -2088,7 +2091,7 @@
 			}
 			loops--;
 			if (!tto->loop_buffer) {
-				switch_buffer_create(tto->pool, &tto->loop_buffer, JS_BUFFER_SIZE);
+				switch_buffer_create_dynamic(&tto->loop_buffer, JS_BLOCK_SIZE, JS_BUFFER_SIZE, 0);
 			}
 		} 
 

Modified: freeswitch/trunk/src/switch_buffer.c
==============================================================================
--- freeswitch/trunk/src/switch_buffer.c	(original)
+++ freeswitch/trunk/src/switch_buffer.c	Fri Sep  8 14:57:24 2006
@@ -34,10 +34,17 @@
 
 static uint32_t buffer_id = 0;
 
+typedef enum {
+	SWITCH_BUFFER_FLAG_DYNAMIC = (1 <<  0)
+} switch_buffer_flag_t;
+
 struct switch_buffer {
 	unsigned char *data;
 	switch_size_t used;
 	switch_size_t datalen;
+	switch_size_t max_len;
+	switch_size_t blocksize;
+	uint32_t flags;
 	uint32_t id;
 };
 
@@ -55,6 +62,37 @@
 	return SWITCH_STATUS_MEMERR;
 }
 
+SWITCH_DECLARE(switch_status_t) switch_buffer_create_dynamic(switch_buffer_t **buffer,
+															 switch_size_t blocksize,
+															 switch_size_t start_len,
+															 switch_size_t max_len)
+{
+	switch_buffer_t *new_buffer;
+
+	if ((new_buffer = malloc(sizeof(*new_buffer)))) {
+		memset(new_buffer, 0, sizeof(*new_buffer));
+
+		if (start_len) {
+			if (!(new_buffer->data = malloc(start_len))) {
+				free(new_buffer);
+				return SWITCH_STATUS_MEMERR;
+			}
+			memset(new_buffer->data, 0, start_len);
+		}
+
+		new_buffer->max_len = max_len;
+		new_buffer->datalen = start_len;
+		new_buffer->id = buffer_id++;
+		new_buffer->blocksize = blocksize;
+		switch_set_flag(new_buffer, SWITCH_BUFFER_FLAG_DYNAMIC);
+		
+		*buffer = new_buffer;
+		return SWITCH_STATUS_SUCCESS;
+	}
+
+	return SWITCH_STATUS_MEMERR;
+}
+
 SWITCH_DECLARE(switch_size_t) switch_buffer_len(switch_buffer_t *buffer)
 {
 
@@ -69,7 +107,15 @@
 {
 	assert(buffer != NULL);
 
+	if (switch_test_flag(buffer, SWITCH_BUFFER_FLAG_DYNAMIC)) {
+		if (buffer->max_len) {
+			return (switch_size_t) (buffer->max_len - buffer->used);
+		}
+		return 1000000;
+	}
+
 	return (switch_size_t) (buffer->datalen - buffer->used);
+
 }
 
 SWITCH_DECLARE(switch_size_t) switch_buffer_inuse(switch_buffer_t *buffer)
@@ -131,8 +177,30 @@
 	assert(buffer != NULL);
 	assert(data != NULL);
 	assert(buffer->data != NULL);
+
+	freespace = buffer->datalen - buffer->used;
+
+	if (switch_test_flag(buffer, SWITCH_BUFFER_FLAG_DYNAMIC)) {
+		if (freespace < datalen) {
+			switch_size_t new_size, new_block_size;
+
+			new_size = buffer->datalen + datalen;
+			new_block_size = buffer->datalen + buffer->blocksize;
+
+			if (new_block_size > new_size) {
+				new_size = new_block_size;
+			}
+
+			if (!(buffer->data = realloc(buffer->data, new_size))) {
+				return 0;
+			}
+			
+			buffer->datalen = new_size;
+		}
+	}
 	
 	freespace = buffer->datalen - buffer->used;
+
 	if (freespace < datalen) {
 		return 0;
 	} else {
@@ -151,4 +219,14 @@
 
 	buffer->used = 0;
 	
+}
+
+SWITCH_DECLARE(void) switch_buffer_destroy(switch_buffer_t **buffer)
+{
+	if (*buffer && switch_test_flag((*buffer), SWITCH_BUFFER_FLAG_DYNAMIC)) {
+		free((*buffer)->data);
+		free(*buffer);
+	}
+
+	*buffer = NULL;
 }

Modified: freeswitch/trunk/src/switch_channel.c
==============================================================================
--- freeswitch/trunk/src/switch_channel.c	(original)
+++ freeswitch/trunk/src/switch_channel.c	Fri Sep  8 14:57:24 2006
@@ -164,7 +164,8 @@
 
 	switch_core_hash_init(&(*channel)->variables, pool);
 	switch_core_hash_init(&(*channel)->private_hash, pool);
-	switch_buffer_create(pool, &(*channel)->dtmf_buffer, 128);
+	switch_buffer_create_dynamic(&(*channel)->dtmf_buffer, 128, 128, 0);
+
 	switch_mutex_init(&(*channel)->dtmf_mutex, SWITCH_MUTEX_NESTED, pool);
 	switch_mutex_init(&(*channel)->flag_mutex, SWITCH_MUTEX_NESTED, pool);
 	switch_mutex_init(&(*channel)->profile_mutex, SWITCH_MUTEX_NESTED, pool);
@@ -280,6 +281,11 @@
 
 	return bytes;
 
+}
+
+SWITCH_DECLARE(void) switch_channel_uninit(switch_channel_t *channel)
+{
+	switch_buffer_destroy(&channel->dtmf_buffer);
 }
 
 SWITCH_DECLARE(switch_status_t) switch_channel_init(switch_channel_t *channel,

Modified: freeswitch/trunk/src/switch_core.c
==============================================================================
--- freeswitch/trunk/src/switch_core.c	(original)
+++ freeswitch/trunk/src/switch_core.c	Fri Sep  8 14:57:24 2006
@@ -1465,9 +1465,9 @@
 				perfect = TRUE;
 			} else {
 				if (!session->raw_read_buffer) {
-					switch_size_t bytes = session->read_codec->implementation->bytes_per_frame * 10;
+					switch_size_t bytes = session->read_codec->implementation->bytes_per_frame * 2;
 					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Engaging Read Buffer at %u bytes\n", bytes);
-					switch_buffer_create(session->pool, &session->raw_read_buffer, bytes);
+					switch_buffer_create_dynamic(&session->raw_read_buffer, bytes, bytes, 0);
 				}
 				if (!switch_buffer_write(session->raw_read_buffer, read_frame->data, read_frame->datalen)) {
 					status = SWITCH_STATUS_MEMERR;
@@ -1671,13 +1671,13 @@
 				perfect = TRUE;
 			} else {
 				if (!session->raw_write_buffer) {
-					switch_size_t bytes = session->write_codec->implementation->bytes_per_frame * 10;
+					switch_size_t bytes = session->write_codec->implementation->bytes_per_frame * 2;
 					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG,
 										  "Engaging Write Buffer at %u bytes to accomodate %u->%u\n",
 										  bytes,
 										  write_frame->datalen, session->write_codec->implementation->bytes_per_frame);
 					if ((status =
-						 switch_buffer_create(session->pool, &session->raw_write_buffer, bytes)) != SWITCH_STATUS_SUCCESS) {
+						 switch_buffer_create_dynamic(&session->raw_write_buffer, bytes, bytes, 0)) != SWITCH_STATUS_SUCCESS) {
 						switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Write Buffer Failed!\n");
 						return status;
 					}
@@ -2642,6 +2642,10 @@
 		switch_channel_event_set_data((*session)->channel, event);
 		switch_event_fire(&event);
 	}
+
+	switch_buffer_destroy(&(*session)->raw_read_buffer);
+	switch_buffer_destroy(&(*session)->raw_write_buffer);
+	switch_channel_uninit((*session)->channel);
 
 	pool = (*session)->pool;
 	*session = NULL;



More information about the Freeswitch-svn mailing list