Skip to content

Commit

Permalink
pulseaudio: Add macro for handling Pulseaudio pa_operation
Browse files Browse the repository at this point in the history
Now pa_operation are handled multiple way. Add macro to make
sure that they are handled always same way
  • Loading branch information
illuusio committed Jun 9, 2024
1 parent 18a606e commit 7272950
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 74 deletions.
47 changes: 18 additions & 29 deletions src/hostapi/pulseaudio/pa_linux_pulseaudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,13 @@
*/


#include <string.h> /* strlen() */

#include "pa_linux_pulseaudio_cb_internal.h"
#include "pa_linux_pulseaudio_block_internal.h"

/* PulseAudio headers */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pulse/pulseaudio.h>

/* This is used to identify process name for PulseAudio. */
Expand Down Expand Up @@ -650,12 +649,9 @@ PaError PaPulseAudio_Initialize( PaUtilHostApiRepresentation ** hostApi,
PaPulseAudio_ServerInfoCb,
pulseaudioHostApi );

while( pa_operation_get_state( pulseaudioOperation ) == PA_OPERATION_RUNNING )
{
pa_threaded_mainloop_wait( pulseaudioHostApi->mainloop );
}

pa_operation_unref( pulseaudioOperation );
PA_PULSEAUDIO_WAIT_FOR_OPERATION( pulseaudioHostApi,
pulseaudioOperation,
i );

/* Add the "Default" sink at index 0 */
if( _PaPulseAudio_AddAudioDevice( pulseaudioHostApi,
Expand Down Expand Up @@ -701,25 +697,19 @@ PaError PaPulseAudio_Initialize( PaUtilHostApiRepresentation ** hostApi,
PaPulseAudio_SinkListCb,
pulseaudioHostApi );

while( pa_operation_get_state( pulseaudioOperation ) == PA_OPERATION_RUNNING )
{
pa_threaded_mainloop_wait( pulseaudioHostApi->mainloop );
}

pa_operation_unref( pulseaudioOperation );
PA_PULSEAUDIO_WAIT_FOR_OPERATION( pulseaudioHostApi,
pulseaudioOperation,
i );

/* List PulseAudio sources. If found callback: PaPulseAudio_SourceListCb */
pulseaudioOperation =
pa_context_get_source_info_list( pulseaudioHostApi->context,
PaPulseAudio_SourceListCb,
pulseaudioHostApi );

while( pa_operation_get_state( pulseaudioOperation ) == PA_OPERATION_RUNNING )
{
pa_threaded_mainloop_wait( pulseaudioHostApi->mainloop );
}

pa_operation_unref( pulseaudioOperation );
PA_PULSEAUDIO_WAIT_FOR_OPERATION( pulseaudioHostApi,
pulseaudioOperation,
i );

(*hostApi)->info.deviceCount = pulseaudioHostApi->deviceCount;

Expand Down Expand Up @@ -1439,6 +1429,7 @@ PaError PaPulseAudio_RenameSource( PaStream *s, const char *streamName )
PaPulseAudio_Stream *stream = (PaPulseAudio_Stream *) s;
PaError result = paNoError;
pa_operation *op = NULL;
int waitLoop = 0;

if ( stream->inputStream == NULL )
{
Expand All @@ -1461,11 +1452,9 @@ PaError PaPulseAudio_RenameSource( PaStream *s, const char *streamName )
op = pa_stream_set_name( stream->inputStream, streamName, RenameStreamCb, stream );
PaPulseAudio_UnLock( stream->mainloop );

/* Wait for completion. */
while (pa_operation_get_state( op ) == PA_OPERATION_RUNNING)
{
pa_threaded_mainloop_wait( stream->mainloop );
}
PA_PULSEAUDIO_WAIT_FOR_OPERATION( stream,
op,
waitLoop );

return result;
}
Expand All @@ -1475,6 +1464,7 @@ PaError PaPulseAudio_RenameSink( PaStream *s, const char *streamName )
PaPulseAudio_Stream *stream = (PaPulseAudio_Stream *) s;
PaError result = paNoError;
pa_operation *op = NULL;
int waitLoop = 0;

if ( stream->outputStream == NULL )
{
Expand All @@ -1498,10 +1488,9 @@ PaError PaPulseAudio_RenameSink( PaStream *s, const char *streamName )
PaPulseAudio_UnLock( stream->mainloop );

/* Wait for completion. */
while (pa_operation_get_state( op ) == PA_OPERATION_RUNNING)
{
pa_threaded_mainloop_wait( stream->mainloop );
}
PA_PULSEAUDIO_WAIT_FOR_OPERATION( stream,
op,
waitLoop );

return result;
}
56 changes: 13 additions & 43 deletions src/hostapi/pulseaudio/pa_linux_pulseaudio_cb.c
Original file line number Diff line number Diff line change
Expand Up @@ -488,22 +488,11 @@ PaError PaPulseAudio_CloseStreamCb( PaStream * s )
stream );
PaPulseAudio_UnLock( stream->mainloop );

while( pa_operation_get_state( pulseaudioOperation ) == PA_OPERATION_RUNNING )
{
pa_threaded_mainloop_wait( pulseaudioHostApi->mainloop );
waitLoop ++;

if(waitLoop > 256)
{
break;
}
}

waitLoop = 0;
PA_PULSEAUDIO_WAIT_FOR_OPERATION( stream,
pulseaudioOperation,
waitLoop );

PaPulseAudio_Lock(stream->mainloop);
pa_operation_unref( pulseaudioOperation );
pulseaudioOperation = NULL;

pa_stream_disconnect( stream->outputStream );
PaPulseAudio_UnLock( stream->mainloop );
Expand All @@ -520,22 +509,11 @@ PaError PaPulseAudio_CloseStreamCb( PaStream * s )
stream );
PaPulseAudio_UnLock( stream->mainloop );

while( pa_operation_get_state( pulseaudioOperation ) == PA_OPERATION_RUNNING )
{
pa_threaded_mainloop_wait( pulseaudioHostApi->mainloop );
waitLoop ++;

if(waitLoop > 256)
{
break;
}
}

waitLoop = 0;
PA_PULSEAUDIO_WAIT_FOR_OPERATION( stream,
pulseaudioOperation,
waitLoop );

PaPulseAudio_Lock( stream->mainloop );
pa_operation_unref( pulseaudioOperation );
pulseaudioOperation = NULL;

/* Then we disconnect stream and wait for
* Termination
Expand Down Expand Up @@ -769,13 +747,9 @@ PaError PaPulseAudio_StartStreamCb( PaStream * s )
stream );
PaPulseAudio_UnLock( pulseaudioHostApi->mainloop );

while( pa_operation_get_state( pulseaudioOperation ) == PA_OPERATION_RUNNING)
{
pa_threaded_mainloop_wait( pulseaudioHostApi->mainloop );
}

pa_operation_unref( pulseaudioOperation );
pulseaudioOperation = NULL;
PA_PULSEAUDIO_WAIT_FOR_OPERATION( stream,
pulseaudioOperation,
waitLoop );
}
else
{
Expand Down Expand Up @@ -897,6 +871,7 @@ 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 @@ -919,14 +894,9 @@ static PaError RequestStop( PaPulseAudio_Stream * stream,
PaPulseAudio_CorkSuccessCb,
stream );

while( pa_operation_get_state( pulseaudioOperation ) == PA_OPERATION_RUNNING )
{
pa_threaded_mainloop_wait( pulseaudioHostApi->mainloop );
}

pa_operation_unref( pulseaudioOperation );

pulseaudioOperation = NULL;
PA_PULSEAUDIO_WAIT_FOR_OPERATION( stream,
pulseaudioOperation,
waitLoop );
}

requeststop_error:
Expand Down
23 changes: 21 additions & 2 deletions src/hostapi/pulseaudio/pa_linux_pulseaudio_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,30 @@ PaPulseAudio_Stream;
return errorCode; \
} \
} \
if( !pastream->isActive || pastream->isStopped ) \
if( !(pastream)->isActive || (pastream)->isStopped ) \
{ \
return paStreamIsStopped; \
return paStreamIsStopped; \
}

#define PA_PULSEAUDIO_WAIT_FOR_OPERATION(pulseaudiostream, operation, waitOperation) \
waitOperation = 0; \
while( pa_operation_get_state( operation ) == PA_OPERATION_RUNNING ) \
{ \
pa_threaded_mainloop_wait( pulseaudiostream->mainloop ); \
waitOperation ++; \
usleep(2000); \
if(waitOperation > 2000) \
{ \
break; \
} \
} \
waitOperation = 0; \
PaPulseAudio_Lock( pulseaudiostream->mainloop ); \
pa_operation_unref( operation ); \
operation = NULL; \
PaPulseAudio_UnLock( pulseaudiostream->mainloop ); \


void PaPulseAudio_Lock( pa_threaded_mainloop *mainloop );

void PaPulseAudio_UnLock( pa_threaded_mainloop *mainloop );
Expand Down

0 comments on commit 7272950

Please sign in to comment.