pa_mac_core_blocking: Fix timeout.

Change alignment of comment.
This commit is contained in:
Phil Burk 2016-09-21 15:46:30 -07:00
commit a38df79293
5 changed files with 28 additions and 17 deletions

View file

@ -2790,7 +2790,7 @@ static PaError StopStream( PaStream *s )
stream->state = STOPPING;
VDBUG( ("Waiting for BLIO.\n") );
paErr = waitUntilBlioWriteBufferIsEmpty( &stream->blio, stream->sampleRate );
paErr = waitUntilBlioWriteBufferIsEmpty( &stream->blio, stream->sampleRate, stream->outputFramesPerBuffer );
VDBUG( ( "waitUntilBlioWriteBufferIsEmpty returned %d\n", paErr ) );
return FinishStoppingStream( stream );

View file

@ -587,27 +587,36 @@ PaError WriteStream( PaStream* stream,
/*
* Wait until the data in the buffer has finished playing.
*/
PaError waitUntilBlioWriteBufferIsEmpty( PaMacBlio *blio, double sampleRate )
PaError waitUntilBlioWriteBufferIsEmpty( PaMacBlio *blio, double sampleRate,
size_t framesPerBuffer )
{
PaError result = paNoError;
if( blio->outputRingBuffer.buffer ) {
int timeout = 5; // don't wait forever
ring_buffer_size_t framesInBuffer = PaUtil_GetRingBufferReadAvailable( &blio->outputRingBuffer );
while( framesInBuffer > 0 && timeout-- > 0 ) {
long msecEstimated = 1 + (long)( 1000.0 * framesInBuffer / sampleRate);
VDBUG(( "waitUntilBlioWriteBufferIsFlushed: framesInBuffer = %d, msecEstimated = %ld\n", framesInBuffer, msecEstimated ));
Pa_Sleep( msecEstimated );
framesInBuffer = PaUtil_GetRingBufferReadAvailable( &blio->outputRingBuffer );
ring_buffer_size_t framesLeft = PaUtil_GetRingBufferReadAvailable( &blio->outputRingBuffer );
/* Calculate when we should give up waiting. To be safe wait for two extra periods. */
PaTime now = PaUtil_GetTime();
PaTime startTime = now;
PaTime timeoutTime = startTime + (framesLeft + (2 * framesPerBuffer)) / sampleRate;
long msecPerBuffer = 1 + (long)( 1000.0 * framesPerBuffer / sampleRate);
while( framesLeft > 0 && now < timeoutTime ) {
VDBUG(( "waitUntilBlioWriteBufferIsFlushed: framesLeft = %d, framesPerBuffer = %ld\n",
framesLeft, framesPerBuffer ));
Pa_Sleep( msecPerBuffer );
framesLeft = PaUtil_GetRingBufferReadAvailable( &blio->outputRingBuffer );
now = PaUtil_GetTime();
}
if( framesInBuffer > 0 )
if( framesLeft > 0 )
{
VDBUG(( "waitUntilBlioWriteBufferIsFlushed: TIMED OUT - framesLeft = %d\n", framesLeft ));
result = paTimedOut;
}
}
return result;
}
signed long GetStreamReadAvailable( PaStream* stream )
{
PaMacBlio *blio = & ((PaMacCoreStream*)stream) -> blio;

View file

@ -128,6 +128,7 @@ int BlioCallback(
PaStreamCallbackFlags statusFlags,
void *userData );
PaError waitUntilBlioWriteBufferIsEmpty( PaMacBlio *blio, double sampleRate );
PaError waitUntilBlioWriteBufferIsEmpty( PaMacBlio *blio, double sampleRate,
size_t framesPerBuffer );
#endif /*PA_MAC_CORE_BLOCKING_H_*/