[Freeswitch-svn] [commit] r4849 - in freeswitch/trunk/src: . include mod/languages/mod_spidermonkey_teletone

Freeswitch SVN anthm at freeswitch.org
Wed Apr 4 14:22:59 EDT 2007


Author: anthm
Date: Wed Apr  4 14:22:59 2007
New Revision: 4849

Modified:
   freeswitch/trunk/src/include/switch_buffer.h
   freeswitch/trunk/src/mod/languages/mod_spidermonkey_teletone/mod_spidermonkey_teletone.c
   freeswitch/trunk/src/switch_buffer.c
   freeswitch/trunk/src/switch_ivr_originate.c

Log:
add looping buffers

Modified: freeswitch/trunk/src/include/switch_buffer.h
==============================================================================
--- freeswitch/trunk/src/include/switch_buffer.h	(original)
+++ freeswitch/trunk/src/include/switch_buffer.h	Wed Apr  4 14:22:59 2007
@@ -96,6 +96,21 @@
  */
 SWITCH_DECLARE(switch_size_t) switch_buffer_read(switch_buffer_t *buffer, void *data, switch_size_t datalen);
 
+/*! \brief Read data endlessly from a switch_buffer_t 
+ * \param buffer any buffer of type switch_buffer_t
+ * \param data pointer to the read data to be returned
+ * \param datalen amount of data to be returned
+ * \return int ammount of data actually read
+ * \note Once you have read all the data from the buffer it will loop around.
+ */
+SWITCH_DECLARE(switch_size_t) switch_buffer_read_loop(switch_buffer_t *buffer, void *data, switch_size_t datalen);
+
+/*! \brief Assign a number of loops to read
+ * \param buffer any buffer of type switch_buffer_t
+ * \param loops the number of loops (-1 for infinite)
+ */
+SWITCH_DECLARE(void) switch_buffer_set_loops(switch_buffer_t *buffer, int32_t loops);
+
 /*! \brief Write data into a switch_buffer_t up to the length of datalen
  * \param buffer any buffer of type switch_buffer_t
  * \param data pointer to the data to be written

Modified: freeswitch/trunk/src/mod/languages/mod_spidermonkey_teletone/mod_spidermonkey_teletone.c
==============================================================================
--- freeswitch/trunk/src/mod/languages/mod_spidermonkey_teletone/mod_spidermonkey_teletone.c	(original)
+++ freeswitch/trunk/src/mod/languages/mod_spidermonkey_teletone/mod_spidermonkey_teletone.c	Wed Apr  4 14:22:59 2007
@@ -46,7 +46,6 @@
 	switch_core_session_t *session;
 	switch_codec_t codec;
 	switch_buffer_t *audio_buffer;
-	switch_buffer_t *loop_buffer;
 	switch_memory_pool_t *pool;
 	switch_timer_t *timer;
 	switch_timer_t timer_base;
@@ -158,7 +157,6 @@
 		}
 		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;
@@ -221,17 +219,12 @@
 				return JS_FALSE;
 			}
 			loops--;
-			if (!tto->loop_buffer) {
-				switch_buffer_create_dynamic(&tto->loop_buffer, JS_BLOCK_SIZE, JS_BUFFER_SIZE, 0);
-			}
 		}
 
 		if (tto->audio_buffer) {
 			switch_buffer_zero(tto->audio_buffer);
 		}
-		if (tto->loop_buffer) {
-			switch_buffer_zero(tto->loop_buffer);
-		}
+
 		tto->ts.debug = 1;
 		tto->ts.debug_stream = switch_core_get_console();
 
@@ -250,12 +243,16 @@
 			}
 		}
 
+		if (loops) {
+			switch_buffer_set_loops(tto->audio_buffer, loops);
+		}
+
 		for (;;) {
 
 			if (switch_test_flag(tto, TTF_DTMF)) {
 				char dtmf[128];
 				char *ret;
-
+				
 				if (switch_channel_has_dtmf(channel)) {
 					uintN aargc = 0;
 					jsval aargv[4];
@@ -284,23 +281,9 @@
 					break;
 				}
 			}
-			if ((write_frame.datalen = (uint32_t) switch_buffer_read(tto->audio_buffer, fdata, write_frame.codec->implementation->bytes_per_frame)) <= 0) {
-				if (loops) {
-					switch_buffer_t *tmp;
-
-					/* Switcharoo */
-					tmp = tto->audio_buffer;
-					tto->audio_buffer = tto->loop_buffer;
-					tto->loop_buffer = tmp;
-					loops--;
-					/* try again */
-					if ((write_frame.datalen =
-						 (uint32_t) switch_buffer_read(tto->audio_buffer, fdata, write_frame.codec->implementation->bytes_per_frame)) <= 0) {
-						break;
-					}
-				} else {
-					break;
-				}
+			if ((write_frame.datalen = (uint32_t) switch_buffer_read_loop(tto->audio_buffer,
+																		  fdata, write_frame.codec->implementation->bytes_per_frame)) <= 0) {
+				break;
 			}
 
 			write_frame.samples = write_frame.datalen / 2;
@@ -310,9 +293,6 @@
 					break;
 				}
 			}
-			if (tto->loop_buffer && loops) {
-				switch_buffer_write(tto->loop_buffer, write_frame.data, write_frame.datalen);
-			}
 		}
 
 		if (tto->timer) {

Modified: freeswitch/trunk/src/switch_buffer.c
==============================================================================
--- freeswitch/trunk/src/switch_buffer.c	(original)
+++ freeswitch/trunk/src/switch_buffer.c	Wed Apr  4 14:22:59 2007
@@ -48,6 +48,7 @@
 	switch_size_t blocksize;
 	uint32_t flags;
 	uint32_t id;
+	int32_t loops;
 };
 
 SWITCH_DECLARE(switch_status_t) switch_buffer_create(switch_memory_pool_t *pool, switch_buffer_t **buffer, switch_size_t max_len)
@@ -147,6 +148,26 @@
 	return buffer->used;
 }
 
+SWITCH_DECLARE(void) switch_buffer_set_loops(switch_buffer_t *buffer, int32_t loops)
+{
+	buffer->loops = loops;
+}
+
+SWITCH_DECLARE(switch_size_t) switch_buffer_read_loop(switch_buffer_t *buffer, void *data, switch_size_t datalen)
+{
+	switch_size_t len;
+	if ((len = switch_buffer_read(buffer, data, datalen)) == 0) {
+		if (buffer->loops == 0) {
+			return 0;
+		}
+		buffer->head = buffer->data;
+		buffer->used = buffer->actually_used;
+		len = switch_buffer_read(buffer, data, datalen);
+		buffer->loops--;
+	}
+	return len;
+}
+
 SWITCH_DECLARE(switch_size_t) switch_buffer_read(switch_buffer_t *buffer, void *data, switch_size_t datalen)
 {
 	switch_size_t reading = 0;

Modified: freeswitch/trunk/src/switch_ivr_originate.c
==============================================================================
--- freeswitch/trunk/src/switch_ivr_originate.c	(original)
+++ freeswitch/trunk/src/switch_ivr_originate.c	Wed Apr  4 14:22:59 2007
@@ -210,7 +210,6 @@
 
 struct ringback {
 	switch_buffer_t *audio_buffer;
-	switch_buffer_t *loop_buffer;
 	teletone_generation_session_t ts;
 	switch_file_handle_t fhb;
 	switch_file_handle_t *fh;
@@ -623,7 +622,7 @@
 							char *tmp_data = NULL;
 
 							switch_buffer_create_dynamic(&ringback.audio_buffer, 512, 1024, 0);
-							switch_buffer_create_dynamic(&ringback.loop_buffer, 512, 1024, 0);
+							switch_buffer_set_loops(ringback.audio_buffer, -1);
 
 							if (*ringback_data == '/') {
 								char *ext;
@@ -665,7 +664,6 @@
 									switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Playing Tone\n");
 									teletone_destroy_session(&ringback.ts);
 									switch_buffer_destroy(&ringback.audio_buffer);
-									switch_buffer_destroy(&ringback.loop_buffer);
 									ringback_data = NULL;
 								}
 							}
@@ -747,27 +745,16 @@
 								break;
 							}
 						} else if (ringback.audio_buffer) {
-							if ((write_frame.datalen = (uint32_t) switch_buffer_read(ringback.audio_buffer,
-																					 write_frame.data,
-																					 write_frame.codec->implementation->bytes_per_frame)) <= 0) {
-								switch_buffer_t *tmp;
-								tmp = ringback.audio_buffer;
-								ringback.audio_buffer = ringback.loop_buffer;
-								ringback.loop_buffer = tmp;
-								if ((write_frame.datalen = (uint32_t) switch_buffer_read(ringback.audio_buffer,
-																						 write_frame.data,
-																						 write_frame.codec->implementation->bytes_per_frame)) <= 0) {
-									break;
-								}
+							if ((write_frame.datalen = (uint32_t) switch_buffer_read_loop(ringback.audio_buffer,
+																						  write_frame.data,
+																						  write_frame.codec->implementation->bytes_per_frame)) <= 0) {
+								break;
 							}
 						}
 
 						if (switch_core_session_write_frame(session, &write_frame, 1000, 0) != SWITCH_STATUS_SUCCESS) {
 							break;
 						}
-						if (ringback.loop_buffer) {
-							switch_buffer_write(ringback.loop_buffer, write_frame.data, write_frame.datalen);
-						}
 					}
 
 				} else {
@@ -908,7 +895,6 @@
 			} else if (ringback.audio_buffer) {
 				teletone_destroy_session(&ringback.ts);
 				switch_buffer_destroy(&ringback.audio_buffer);
-				switch_buffer_destroy(&ringback.loop_buffer);
 			}
 
 			for (i = 0; i < and_argc; i++) {



More information about the Freeswitch-svn mailing list