From 772bc3e68c83aed2b141619a01248f4ec542322f Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Thu, 19 Mar 2009 18:30:56 +0000 Subject: [PATCH] fix audio issue in portaudio git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@12669 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- .../endpoints/mod_portaudio/mod_portaudio.c | 61 +++++++++++++------ src/mod/endpoints/mod_portaudio/pablio.c | 57 +++++++++-------- src/switch_rtp.c | 4 +- 3 files changed, 72 insertions(+), 50 deletions(-) diff --git a/src/mod/endpoints/mod_portaudio/mod_portaudio.c b/src/mod/endpoints/mod_portaudio/mod_portaudio.c index 7e3d2f7194..3c4c603e99 100644 --- a/src/mod/endpoints/mod_portaudio/mod_portaudio.c +++ b/src/mod/endpoints/mod_portaudio/mod_portaudio.c @@ -124,7 +124,8 @@ static struct { 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 @@ static switch_status_t channel_on_init(switch_core_session_t *session) } 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 @@ static switch_status_t channel_on_init(switch_core_session_t *session) } 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 @@ static switch_status_t channel_on_init(switch_core_session_t *session) } 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 @@ static void destroy_codecs(void) 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 @@ static switch_status_t channel_read_frame(switch_core_session_t *session, switch 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 @@ static switch_status_t channel_write_frame(switch_core_session_t *session, switc 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 @@ static switch_status_t engage_device(int restart) 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,8 +1318,8 @@ static switch_status_t engage_device(int restart) } } - 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"); @@ -1321,6 +1329,19 @@ static switch_status_t engage_device(int restart) } } + + 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; + } + } + if (!globals.hold_timer.timer_interface) { if (switch_core_timer_init(&globals.hold_timer, globals.timer_name, codec_ms, globals.read_codec.implementation->samples_per_packet, @@ -1328,7 +1349,8 @@ static switch_status_t engage_device(int restart) 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 @@ static switch_status_t engage_device(int restart) 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 @@ SWITCH_STANDARD_API(pa_cmd) 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 @@ SWITCH_STANDARD_API(pa_cmd) 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); diff --git a/src/mod/endpoints/mod_portaudio/pablio.c b/src/mod/endpoints/mod_portaudio/pablio.c index af4b0016c4..12eab8ff0d 100644 --- a/src/mod/endpoints/mod_portaudio/pablio.c +++ b/src/mod/endpoints/mod_portaudio/pablio.c @@ -146,17 +146,15 @@ long WriteAudioStream(PABLIO_Stream * aStream, void *data, long numFrames, switc 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 ReadAudioStream(PABLIO_Stream * aStream, void *data, long numFrames, switch long bytesRead = 0; char *p = (char *) data; long avail, totalBytes = 0, neededBytes = aStream->bytesPerFrame * numFrames; + int max = 5000; + + switch_core_timer_next(timer); + + while(totalBytes < neededBytes && --max > 0) { - for (;;) { avail = PaUtil_GetRingBufferReadAvailable(&aStream->inFIFO); - - if (switch_core_timer_check(timer, SWITCH_TRUE) == SWITCH_STATUS_SUCCESS) { - break; - } - + //printf("AVAILABLE BYTES %ld pass %d\n", avail, 5000 - max); if (avail >= neededBytes * 6) { PaUtil_FlushRingBuffer(&aStream->inFIFO); avail = 0; - } - - bytesRead = 0; - - if (totalBytes < neededBytes && avail >= neededBytes) { - bytesRead = PaUtil_ReadRingBuffer(&aStream->inFIFO, p, neededBytes); - totalBytes += bytesRead; - } - - if (bytesRead) { - p += bytesRead; } else { - switch_cond_next(); + + bytesRead = 0; + + if (totalBytes < neededBytes && avail >= neededBytes) { + bytesRead = PaUtil_ReadRingBuffer(&aStream->inFIFO, p, neededBytes); + totalBytes += bytesRead; + } + + if (bytesRead) { + p += bytesRead; + } else { + switch_cond_next(); + } } } diff --git a/src/switch_rtp.c b/src/switch_rtp.c index 69aae18a4b..026f1ecea2 100644 --- a/src/switch_rtp.c +++ b/src/switch_rtp.c @@ -1990,12 +1990,12 @@ static int rtp_common_write(switch_rtp_t *rtp_session, } 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++; }