From 2d9b95f9992659150fc7dabf92e161327c56a099 Mon Sep 17 00:00:00 2001 From: Michal Szwaj Date: Sat, 17 Aug 2019 16:25:56 +0200 Subject: [PATCH] Fix high CPU usage by PulseAudio backend when stream is stopped --- RtAudio.cpp | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/RtAudio.cpp b/RtAudio.cpp index e0713a0..1df152b 100644 --- a/RtAudio.cpp +++ b/RtAudio.cpp @@ -8679,15 +8679,18 @@ void RtApiPulse::stopStream( void ) stream_.state = STREAM_STOPPED; MUTEX_LOCK( &stream_.mutex ); - if ( pah && pah->s_play ) { - int pa_error; - if ( pa_simple_drain( pah->s_play, &pa_error ) < 0 ) { - errorStream_ << "RtApiPulse::stopStream: error draining output device, " << - pa_strerror( pa_error ) << "."; - errorText_ = errorStream_.str(); - MUTEX_UNLOCK( &stream_.mutex ); - error( RtAudioError::SYSTEM_ERROR ); - return; + if ( pah ) { + pah->runnable = false; + if ( pah->s_play ) { + int pa_error; + if ( pa_simple_drain( pah->s_play, &pa_error ) < 0 ) { + errorStream_ << "RtApiPulse::stopStream: error draining output device, " << + pa_strerror( pa_error ) << "."; + errorText_ = errorStream_.str(); + MUTEX_UNLOCK( &stream_.mutex ); + error( RtAudioError::SYSTEM_ERROR ); + return; + } } } @@ -8713,15 +8716,18 @@ void RtApiPulse::abortStream( void ) stream_.state = STREAM_STOPPED; MUTEX_LOCK( &stream_.mutex ); - if ( pah && pah->s_play ) { - int pa_error; - if ( pa_simple_flush( pah->s_play, &pa_error ) < 0 ) { - errorStream_ << "RtApiPulse::abortStream: error flushing output device, " << - pa_strerror( pa_error ) << "."; - errorText_ = errorStream_.str(); - MUTEX_UNLOCK( &stream_.mutex ); - error( RtAudioError::SYSTEM_ERROR ); - return; + if ( pah ) { + pah->runnable = false; + if ( pah->s_play ) { + int pa_error; + if ( pa_simple_flush( pah->s_play, &pa_error ) < 0 ) { + errorStream_ << "RtApiPulse::abortStream: error flushing output device, " << + pa_strerror( pa_error ) << "."; + errorText_ = errorStream_.str(); + MUTEX_UNLOCK( &stream_.mutex ); + error( RtAudioError::SYSTEM_ERROR ); + return; + } } }