[Freeswitch-svn] [commit] r12669 - in freeswitch/trunk/src: . mod/endpoints/mod_portaudio

FreeSWITCH SVN anthm at freeswitch.org
Thu Mar 19 11:30:56 PDT 2009


Author: anthm
Date: Thu Mar 19 13:30:56 2009
New Revision: 12669

Log:
fix audio issue in portaudio

Modified:
   freeswitch/trunk/src/mod/endpoints/mod_portaudio/mod_portaudio.c
   freeswitch/trunk/src/mod/endpoints/mod_portaudio/pablio.c
   freeswitch/trunk/src/switch_rtp.c

Modified: freeswitch/trunk/src/mod/endpoints/mod_portaudio/mod_portaudio.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_portaudio/mod_portaudio.c	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_portaudio/mod_portaudio.c	Thu Mar 19 13:30:56 2009
@@ -124,7 +124,8 @@
 	private_t *call_list;
 	int ring_interval;
 	GFLAGS flags;
-	switch_timer_t timer;
+	switch_timer_t read_timer;
+	switch_timer_t write_timer;
 	switch_timer_t hold_timer;
 	int dual_streams;
 	time_t deactivate_timer;
@@ -249,7 +250,7 @@
 		}
 
 		while (switch_channel_get_state(channel) == CS_INIT && !switch_test_flag(tech_pvt, TFLAG_ANSWER)) {
-			switch_size_t olen = globals.timer.samples;
+			switch_size_t olen = globals.read_timer.samples;
 
 			if (switch_micro_time_now() - last >= waitsec) {
 				char buf[512];
@@ -268,7 +269,7 @@
 			}
 
 			if (ring_file) {
-				if (switch_core_timer_next(&globals.timer) != SWITCH_STATUS_SUCCESS) {
+				if (switch_core_timer_next(&globals.read_timer) != SWITCH_STATUS_SUCCESS) {
 					switch_core_file_close(&fh);
 					break;
 				}
@@ -279,7 +280,7 @@
 				}
 
 				if (globals.ring_stream) {
-					WriteAudioStream(globals.ring_stream, abuf, (long) olen, &globals.timer);
+					WriteAudioStream(globals.ring_stream, abuf, (long) olen, &globals.write_timer);
 				}
 			} else {
 				switch_yield(10000);
@@ -355,8 +356,12 @@
 		switch_core_codec_destroy(&globals.write_codec);
 	}
 
-	if (globals.timer.timer_interface) {
-		switch_core_timer_destroy(&globals.timer);
+	if (globals.read_timer.timer_interface) {
+		switch_core_timer_destroy(&globals.read_timer);
+	}
+
+	if (globals.write_timer.timer_interface) {
+		switch_core_timer_destroy(&globals.write_timer);
 	}
 
 	if (globals.hold_timer.timer_interface) {
@@ -597,14 +602,13 @@
 	
 	switch_mutex_lock(globals.device_lock);
 	samples = ReadAudioStream(globals.audio_stream, globals.read_frame.data,
-							  globals.read_codec.implementation->samples_per_packet, &globals.timer);
+							  globals.read_codec.implementation->samples_per_packet, &globals.read_timer);
 	switch_mutex_unlock(globals.device_lock);
 	
 	if (samples) {
 		globals.read_frame.datalen = samples * 2;
 		globals.read_frame.samples = samples;
 
-		//switch_core_timer_check(&globals.timer, SWITCH_TRUE);
 		*frame = &globals.read_frame;
 
 		if (!switch_test_flag((&globals), GFLAG_MOUTH)) {
@@ -649,7 +653,7 @@
 
 	if (globals.audio_stream) {
 		if (switch_test_flag((&globals), GFLAG_EAR)) {
-			WriteAudioStream(globals.audio_stream, (short *) frame->data, (int) (frame->datalen / sizeof(SAMPLE)), &globals.timer);
+			WriteAudioStream(globals.audio_stream, (short *) frame->data, (int) (frame->datalen / sizeof(SAMPLE)), &globals.write_timer);
 		}
 		status = SWITCH_STATUS_SUCCESS;
 	}
@@ -1278,8 +1282,12 @@
 		destroy_codecs();
 	}
 
-	if (globals.timer.timer_interface) {
-		switch_core_timer_sync(&globals.timer);
+	if (globals.read_timer.timer_interface) {
+		switch_core_timer_sync(&globals.read_timer);
+	}
+
+	if (globals.write_timer.timer_interface) {
+		switch_core_timer_sync(&globals.write_timer);
 	}
 
 	if (globals.audio_stream) {
@@ -1310,13 +1318,26 @@
 		}
 	}
 
-	if (!globals.timer.timer_interface) {
-		if (switch_core_timer_init(&globals.timer,
+	if (!globals.read_timer.timer_interface) {
+		if (switch_core_timer_init(&globals.read_timer,
+								   globals.timer_name, codec_ms, globals.read_codec.implementation->samples_per_packet,
+								   module_pool) != SWITCH_STATUS_SUCCESS) {
+			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "setup timer failed!\n");
+			switch_core_codec_destroy(&globals.read_codec);
+			switch_core_codec_destroy(&globals.write_codec);
+			return SWITCH_STATUS_FALSE;
+		}
+	}
+
+
+	if (!globals.write_timer.timer_interface) {
+		if (switch_core_timer_init(&globals.write_timer,
 								   globals.timer_name, codec_ms, globals.read_codec.implementation->samples_per_packet,
 								   module_pool) != SWITCH_STATUS_SUCCESS) {
 			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "setup timer failed!\n");
 			switch_core_codec_destroy(&globals.read_codec);
 			switch_core_codec_destroy(&globals.write_codec);
+			switch_core_timer_destroy(&globals.read_timer);
 			return SWITCH_STATUS_FALSE;
 		}
 	}
@@ -1328,7 +1349,8 @@
 			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "setup hold timer failed!\n");
 			switch_core_codec_destroy(&globals.read_codec);
 			switch_core_codec_destroy(&globals.write_codec);
-			switch_core_timer_destroy(&globals.timer);
+			switch_core_timer_destroy(&globals.read_timer);
+			switch_core_timer_destroy(&globals.write_timer);
 			return SWITCH_STATUS_FALSE;
 		}
 	}
@@ -1366,7 +1388,8 @@
 		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Can't open audio device\n");
 		switch_core_codec_destroy(&globals.read_codec);
 		switch_core_codec_destroy(&globals.write_codec);
-		switch_core_timer_destroy(&globals.timer);
+		switch_core_timer_destroy(&globals.read_timer);
+		switch_core_timer_destroy(&globals.write_timer);
 		switch_core_timer_destroy(&globals.hold_timer);
 		return SWITCH_STATUS_FALSE;
 	}
@@ -1963,8 +1986,8 @@
 									   playfile, seconds, samples, globals.read_codec.implementation->actual_samples_per_second);
 				
 				while (switch_core_file_read(&fh, abuf, &olen) == SWITCH_STATUS_SUCCESS) {
-					WriteAudioStream(globals.audio_stream, abuf, (long) olen, &globals.timer);
-					switch_core_timer_next(&globals.timer);
+					WriteAudioStream(globals.audio_stream, abuf, (long) olen, &globals.read_timer);
+					switch_core_timer_next(&globals.read_timer);
 					samples -= (int) olen;
 					if (samples <= 0) {
 						break;
@@ -1993,8 +2016,8 @@
 			int i;
 			for(i = 0; i < 400; i++) {
 				if ((samples = ReadAudioStream(globals.audio_stream, globals.read_frame.data,
-						globals.read_codec.implementation->samples_per_packet, &globals.timer))) {
-					WriteAudioStream(globals.audio_stream, globals.read_frame.data, (long) samples, &globals.timer);
+						globals.read_codec.implementation->samples_per_packet, &globals.read_timer))) {
+					WriteAudioStream(globals.audio_stream, globals.read_frame.data, (long) samples, &globals.write_timer);
 					success = 1;
 				}
 				switch_yield(10000);

Modified: freeswitch/trunk/src/mod/endpoints/mod_portaudio/pablio.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_portaudio/pablio.c	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_portaudio/pablio.c	Thu Mar 19 13:30:56 2009
@@ -146,17 +146,15 @@
 	char *p = (char *) data;
 	long numBytes = aStream->bytesPerFrame * numFrames;
 
-	while (numBytes > 0) {
-		bytesWritten = PaUtil_WriteRingBuffer(&aStream->outFIFO, p, numBytes);
-		numBytes -= bytesWritten;
-		p += bytesWritten;
-		if (numBytes > 0) {
-			if (switch_core_timer_check(timer, SWITCH_TRUE) == SWITCH_STATUS_SUCCESS) {
-				PaUtil_FlushRingBuffer(&aStream->outFIFO);
-				return 0;
-			}
-			switch_cond_next();
-		}
+	switch_core_timer_next(timer);
+
+	bytesWritten = PaUtil_WriteRingBuffer(&aStream->outFIFO, p, numBytes);
+	numBytes -= bytesWritten;
+	p += bytesWritten;
+	
+	if (numBytes > 0) {
+		PaUtil_FlushRingBuffer(&aStream->outFIFO);
+		return 0;
 	}
 	return numFrames;
 }
@@ -170,30 +168,31 @@
 	long bytesRead = 0;
 	char *p = (char *) data;
 	long avail, totalBytes = 0, neededBytes = aStream->bytesPerFrame * numFrames;
+	int max = 5000;
 
-	for (;;) {
-		avail = PaUtil_GetRingBufferReadAvailable(&aStream->inFIFO);
-		
-		if (switch_core_timer_check(timer, SWITCH_TRUE) == SWITCH_STATUS_SUCCESS) {
-			break;
-		}
+	switch_core_timer_next(timer);
+
+	while(totalBytes < neededBytes && --max > 0) {
 
+		avail = PaUtil_GetRingBufferReadAvailable(&aStream->inFIFO);
+		//printf("AVAILABLE BYTES %ld pass %d\n", avail, 5000 - max);
 		if (avail >= neededBytes * 6) {
 			PaUtil_FlushRingBuffer(&aStream->inFIFO);
 			avail = 0;
-		}
-
-		bytesRead = 0;
+		} else {
 
-		if (totalBytes < neededBytes && avail >= neededBytes) {
-			bytesRead = PaUtil_ReadRingBuffer(&aStream->inFIFO, p, neededBytes);
-			totalBytes += bytesRead;
-		}
+			bytesRead = 0;
+			
+			if (totalBytes < neededBytes && avail >= neededBytes) {
+				bytesRead = PaUtil_ReadRingBuffer(&aStream->inFIFO, p, neededBytes);
+				totalBytes += bytesRead;
+			}
 
-		if (bytesRead) {
-			p += bytesRead;
-		} else {
-			switch_cond_next();
+			if (bytesRead) {
+				p += bytesRead;
+			} else {
+				switch_cond_next();
+			}
 		}
 	}
 

Modified: freeswitch/trunk/src/switch_rtp.c
==============================================================================
--- freeswitch/trunk/src/switch_rtp.c	(original)
+++ freeswitch/trunk/src/switch_rtp.c	Thu Mar 19 13:30:56 2009
@@ -1990,12 +1990,12 @@
 		}
 
 		if (rtp_session->timer.interval && 
-			(rtp_session->timer.samplecount - rtp_session->last_write_samplecount) > rtp_session->samples_per_interval * 2) {
+			(rtp_session->timer.samplecount - rtp_session->last_write_samplecount) > rtp_session->samples_per_interval * 10) {
 			m++;
 		}
 
 		if (!rtp_session->timer.interval && 
-			((unsigned)((switch_micro_time_now() - rtp_session->last_write_timestamp))) > (rtp_session->ms_per_packet *2)) {
+			((unsigned)((switch_micro_time_now() - rtp_session->last_write_timestamp))) > (rtp_session->ms_per_packet * 10)) {
 			m++;
 		}
 



More information about the Freeswitch-svn mailing list