Index: pablio.c =================================================================== --- pablio.c (revision 8218) +++ pablio.c (working copy) @@ -50,7 +50,12 @@ #include #include +#include +LARGE_INTEGER startx, last_startx, freq; +long halfNumFramesInBuffer; + + /************************************************************************/ /******** Prototypes ****************************************************/ /************************************************************************/ @@ -147,17 +152,37 @@ char *p = (char *) data; long numBytes = aStream->bytesPerFrame * numFrames; + QueryPerformanceCounter(&startx); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%lld ms : Beginning (%ld Bytes readable) ...\n", startx.QuadPart*1000/freq.QuadPart, GetAudioStreamReadable(aStream) * aStream->bytesPerFrame); + while (numBytes > 0) { + bytesRead = PaUtil_ReadRingBuffer(&aStream->inFIFO, p, numBytes); numBytes -= bytesRead; + QueryPerformanceCounter(&startx); + if (numBytes > 0) { if (switch_core_timer_check(timer, SWITCH_TRUE) == SWITCH_STATUS_SUCCESS) { PaUtil_FlushRingBuffer(&aStream->inFIFO); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%lld ms : timer timed out (%ld Bytes readable)!\n", startx.QuadPart*1000/freq.QuadPart, GetAudioStreamReadable(aStream) * aStream->bytesPerFrame); return 0; } + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%lld ms : before Wait...\n", startx.QuadPart*1000/freq.QuadPart); switch_yield(1000); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%lld ms : after Wait... (%ld Bytes readable)\n", startx.QuadPart*1000/freq.QuadPart, GetAudioStreamReadable(aStream) * aStream->bytesPerFrame); } } + + // if last call was < 4 ms ago we wait, but only if buffer is not filled too much + while( ((startx.QuadPart - last_startx.QuadPart) < freq.QuadPart* 4 /1000) + && (GetAudioStreamReadable(aStream) < halfNumFramesInBuffer) ) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%lld ms : last call was %lld ms ago, we wait 19 ms!\n", startx.QuadPart*1000/freq.QuadPart, ((startx.QuadPart - last_startx.QuadPart)*1000/freq.QuadPart) ); + Pa_Sleep(19); + QueryPerformanceCounter(&startx); + } + + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%lld ms : all %ld Bytes read (%ld Bytes readable), exit.\n", startx.QuadPart*1000/freq.QuadPart, aStream->bytesPerFrame * numFrames, GetAudioStreamReadable(aStream) * aStream->bytesPerFrame ); + last_startx = startx; return numFrames; } @@ -215,6 +240,15 @@ //long numBytes; int channels = 1; + // if system supports QueryPerformanceFrequency() freq is number of ticks per second + if( QueryPerformanceFrequency(&freq)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "QueryPerformanceFrequency(freq) == true, freq=%lld\n", freq.QuadPart); + } + else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "QueryPerformanceFrequency(freq) == false!\n" ); + freq.QuadPart = 0LL; + } + /* Allocate PABLIO_Stream structure for caller. */ aStream = (PABLIO_Stream *) malloc(sizeof(PABLIO_Stream)); if (aStream == NULL) @@ -235,6 +269,9 @@ numFrames = RoundUpToNextPowerOf2(samples_per_frame * 5); aStream->bytesPerFrame = bytesPerSample; + // we need this fill level later + halfNumFramesInBuffer = numFrames / 2; + /* Initialize Ring Buffers */ if (inputParameters) {