diff --git a/src/hostapi/pulseaudio/pa_linux_pulseaudio.c b/src/hostapi/pulseaudio/pa_linux_pulseaudio.c index 32c22c494..31e39ca84 100644 --- a/src/hostapi/pulseaudio/pa_linux_pulseaudio.c +++ b/src/hostapi/pulseaudio/pa_linux_pulseaudio.c @@ -649,9 +649,15 @@ PaError PaPulseAudio_Initialize( PaUtilHostApiRepresentation ** hostApi, PaPulseAudio_ServerInfoCb, pulseaudioHostApi ); + PaPulseAudio_UnLock( pulseaudioHostApi->mainloop ); + lockTaken = 0; + PaPulseAudio_ReleaseOperation( pulseaudioHostApi, &pulseaudioOperation ); + PaPulseAudio_Lock( pulseaudioHostApi->mainloop ); + lockTaken = 1; + /* Add the "Default" sink at index 0 */ if( _PaPulseAudio_AddAudioDevice( pulseaudioHostApi, "Default Sink", @@ -696,18 +702,30 @@ PaError PaPulseAudio_Initialize( PaUtilHostApiRepresentation ** hostApi, PaPulseAudio_SinkListCb, pulseaudioHostApi ); + PaPulseAudio_UnLock( pulseaudioHostApi->mainloop ); + lockTaken = 0; + PaPulseAudio_ReleaseOperation( pulseaudioHostApi, &pulseaudioOperation ); + PaPulseAudio_Lock( pulseaudioHostApi->mainloop ); + lockTaken = 1; + /* List PulseAudio sources. If found callback: PaPulseAudio_SourceListCb */ pulseaudioOperation = pa_context_get_source_info_list( pulseaudioHostApi->context, PaPulseAudio_SourceListCb, pulseaudioHostApi ); + PaPulseAudio_UnLock( pulseaudioHostApi->mainloop ); + lockTaken = 0; + PaPulseAudio_ReleaseOperation( pulseaudioHostApi, &pulseaudioOperation ); + PaPulseAudio_Lock( pulseaudioHostApi->mainloop ); + lockTaken = 1; + (*hostApi)->info.deviceCount = pulseaudioHostApi->deviceCount; if( pulseaudioHostApi->deviceCount > 0 ) diff --git a/src/hostapi/pulseaudio/pa_linux_pulseaudio_cb.c b/src/hostapi/pulseaudio/pa_linux_pulseaudio_cb.c index e05ec0a13..b298cf713 100644 --- a/src/hostapi/pulseaudio/pa_linux_pulseaudio_cb.c +++ b/src/hostapi/pulseaudio/pa_linux_pulseaudio_cb.c @@ -109,22 +109,38 @@ int PaPulseAudio_updateTimeInfo( pa_stream * s, void PaPulseAudio_ReleaseOperation(PaPulseAudio_HostApiRepresentation *hostapi, pa_operation **operation) { - unsigned int wait = 0; + unsigned int waitOperation = 1000; pa_operation *localOperation = (*operation); + pa_operation_state_t localOperationState = PA_OPERATION_RUNNING; + + // As mainly operation is done when running locally + // done after 1-3 then 1000 is enough to wait if + // something goes wrong - while( pa_operation_get_state( localOperation ) == PA_OPERATION_RUNNING ) + while( waitOperation > 0 ) { + PaPulseAudio_Lock( hostapi->mainloop ); pa_threaded_mainloop_wait( hostapi->mainloop ); + PaPulseAudio_UnLock( hostapi->mainloop ); - wait ++; - usleep( 1000 ); + localOperationState = pa_operation_get_state( localOperation ); - if( wait > 2000 ) + // No wait if operation have been DONE or CANCELLED + if( localOperationState != PA_OPERATION_RUNNING) { - PA_DEBUG( ( "Portaudio %s: Operation still running %d!\n", - __FUNCTION__, pa_operation_get_state( localOperation ) ) ); break; } + + waitOperation --; + + usleep( 1000 ); + } + + // No wait if operation have been DONE or CANCELLED + if( localOperationState == PA_OPERATION_RUNNING) + { + PA_DEBUG( ( "Portaudio %s: Operation still running %d!\n", + __FUNCTION__, localOperationState ) ); } PaPulseAudio_Lock( hostapi->mainloop ); @@ -895,7 +911,6 @@ static PaError RequestStop( PaPulseAudio_Stream * stream, PaError ret = paNoError; PaPulseAudio_HostApiRepresentation *pulseaudioHostApi = stream->hostapi; pa_operation *pulseaudioOperation = NULL; - int waitLoop = 0; PaPulseAudio_Lock( pulseaudioHostApi->mainloop ); @@ -918,8 +933,10 @@ static PaError RequestStop( PaPulseAudio_Stream * stream, PaPulseAudio_CorkSuccessCb, stream ); + PaPulseAudio_UnLock( pulseaudioHostApi->mainloop ); PaPulseAudio_ReleaseOperation( pulseaudioHostApi, &pulseaudioOperation ); + PaPulseAudio_Lock( pulseaudioHostApi->mainloop ); } requeststop_error: