Skip to content

Commit

Permalink
Pulseaudio: mainloop should be locked when using pa_threaded_mainloop…
Browse files Browse the repository at this point in the history
…_wait

Mainloop should be locked when using pa_threaded_mainloop_wait
make changes that it happens
  • Loading branch information
illuusio committed Sep 27, 2024
1 parent 6e2e98e commit cdde14d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
18 changes: 18 additions & 0 deletions src/hostapi/pulseaudio/pa_linux_pulseaudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 )
Expand Down
33 changes: 25 additions & 8 deletions src/hostapi/pulseaudio/pa_linux_pulseaudio_cb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );

Expand All @@ -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:
Expand Down

0 comments on commit cdde14d

Please sign in to comment.